1 /* $NetBSD: wdc.c,v 1.280 2016/01/18 04:46:47 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2001, 2003 Manuel Bouyer. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /*- 28 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc. 29 * All rights reserved. 30 * 31 * This code is derived from software contributed to The NetBSD Foundation 32 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 53 * POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 /* 57 * CODE UNTESTED IN THE CURRENT REVISION: 58 */ 59 60 #include <sys/cdefs.h> 61 __KERNEL_RCSID(0, "$NetBSD: wdc.c,v 1.280 2016/01/18 04:46:47 msaitoh Exp $"); 62 63 #include "opt_ata.h" 64 #include "opt_wdc.h" 65 66 #include <sys/param.h> 67 #include <sys/systm.h> 68 #include <sys/kernel.h> 69 #include <sys/conf.h> 70 #include <sys/buf.h> 71 #include <sys/device.h> 72 #include <sys/malloc.h> 73 #include <sys/syslog.h> 74 #include <sys/proc.h> 75 76 #include <sys/intr.h> 77 #include <sys/bus.h> 78 79 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 80 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 81 #define bus_space_write_multi_stream_4 bus_space_write_multi_4 82 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 83 #define bus_space_read_multi_stream_4 bus_space_read_multi_4 84 #define bus_space_read_stream_2 bus_space_read_2 85 #define bus_space_read_stream_4 bus_space_read_4 86 #define bus_space_write_stream_2 bus_space_write_2 87 #define bus_space_write_stream_4 bus_space_write_4 88 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 89 90 #include <dev/ata/atavar.h> 91 #include <dev/ata/atareg.h> 92 #include <dev/ata/satareg.h> 93 #include <dev/ata/satavar.h> 94 #include <dev/ic/wdcreg.h> 95 #include <dev/ic/wdcvar.h> 96 97 #include "locators.h" 98 99 #include "atapibus.h" 100 #include "wd.h" 101 #include "sata.h" 102 103 #define WDCDELAY 100 /* 100 microseconds */ 104 #define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY) 105 #if 0 106 /* If you enable this, it will report any delays more than WDCDELAY * N long. */ 107 #define WDCNDELAY_DEBUG 50 108 #endif 109 110 /* When polling wait that much and then tsleep for 1/hz seconds */ 111 #define WDCDELAY_POLL 1 /* ms */ 112 113 /* timeout for the control commands */ 114 #define WDC_CTRL_DELAY 10000 /* 10s, for the recall command */ 115 116 /* 117 * timeout when waiting for BSY to deassert when probing. 118 * set to 5s. From the standards this could be up to 31, but we can't 119 * wait that much at boot time, and 5s seems to be enough. 120 */ 121 #define WDC_PROBE_WAIT 5 122 123 124 #if NWD > 0 125 extern const struct ata_bustype wdc_ata_bustype; /* in ata_wdc.c */ 126 #else 127 /* A fake one, the autoconfig will print "wd at foo ... not configured */ 128 const struct ata_bustype wdc_ata_bustype = { 129 SCSIPI_BUSTYPE_ATA, 130 NULL, /* wdc_ata_bio */ 131 NULL, /* wdc_reset_drive */ 132 wdc_reset_channel, 133 wdc_exec_command, 134 NULL, /* ata_get_params */ 135 NULL, /* wdc_ata_addref */ 136 NULL, /* wdc_ata_delref */ 137 NULL /* ata_kill_pending */ 138 }; 139 #endif 140 141 /* Flags to wdcreset(). */ 142 #define RESET_POLL 1 143 #define RESET_SLEEP 0 /* wdcreset() will use tsleep() */ 144 145 static int wdcprobe1(struct ata_channel *, int); 146 static int wdcreset(struct ata_channel *, int); 147 static void __wdcerror(struct ata_channel *, const char *); 148 static int __wdcwait_reset(struct ata_channel *, int, int); 149 static void __wdccommand_done(struct ata_channel *, struct ata_xfer *); 150 static void __wdccommand_done_end(struct ata_channel *, struct ata_xfer *); 151 static void __wdccommand_kill_xfer(struct ata_channel *, 152 struct ata_xfer *, int); 153 static void __wdccommand_start(struct ata_channel *, struct ata_xfer *); 154 static int __wdccommand_intr(struct ata_channel *, struct ata_xfer *, int); 155 static int __wdcwait(struct ata_channel *, int, int, int); 156 157 static void wdc_datain_pio(struct ata_channel *, int, void *, size_t); 158 static void wdc_dataout_pio(struct ata_channel *, int, void *, size_t); 159 #define DEBUG_INTR 0x01 160 #define DEBUG_XFERS 0x02 161 #define DEBUG_STATUS 0x04 162 #define DEBUG_FUNCS 0x08 163 #define DEBUG_PROBE 0x10 164 #define DEBUG_DETACH 0x20 165 #define DEBUG_DELAY 0x40 166 #ifdef ATADEBUG 167 extern int atadebug_mask; /* init'ed in ata.c */ 168 int wdc_nxfer = 0; 169 #define ATADEBUG_PRINT(args, level) if (atadebug_mask & (level)) printf args 170 #else 171 #define ATADEBUG_PRINT(args, level) 172 #endif 173 174 /* 175 * Initialize the "shadow register" handles for a standard wdc controller. 176 */ 177 void 178 wdc_init_shadow_regs(struct ata_channel *chp) 179 { 180 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 181 182 wdr->cmd_iohs[wd_status] = wdr->cmd_iohs[wd_command]; 183 wdr->cmd_iohs[wd_features] = wdr->cmd_iohs[wd_error]; 184 } 185 186 /* 187 * Allocate a wdc_regs array, based on the number of channels. 188 */ 189 void 190 wdc_allocate_regs(struct wdc_softc *wdc) 191 { 192 193 wdc->regs = malloc(wdc->sc_atac.atac_nchannels * 194 sizeof(struct wdc_regs), M_DEVBUF, M_WAITOK); 195 } 196 197 #if NSATA > 0 198 /* 199 * probe drives on SATA controllers with standard SATA registers: 200 * bring the PHYs online, read the drive signature and set drive flags 201 * appropriately. 202 */ 203 void 204 wdc_sataprobe(struct ata_channel *chp) 205 { 206 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 207 uint8_t st = 0, sc __unused, sn __unused, cl, ch; 208 int i, s; 209 210 KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL); 211 212 /* reset the PHY and bring online */ 213 switch (sata_reset_interface(chp, wdr->sata_iot, wdr->sata_control, 214 wdr->sata_status, AT_WAIT)) { 215 case SStatus_DET_DEV: 216 /* wait 5s for BSY to clear */ 217 for (i = 0; i < WDC_PROBE_WAIT * hz; i++) { 218 bus_space_write_1(wdr->cmd_iot, 219 wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM); 220 delay(10); /* 400ns delay */ 221 st = bus_space_read_1(wdr->cmd_iot, 222 wdr->cmd_iohs[wd_status], 0); 223 if ((st & WDCS_BSY) == 0) 224 break; 225 tsleep(&chp, PRIBIO, "sataprb", 1); 226 } 227 if (i == WDC_PROBE_WAIT * hz) 228 aprint_error_dev(chp->ch_atac->atac_dev, 229 "BSY never cleared, status 0x%02x\n", st); 230 sc = bus_space_read_1(wdr->cmd_iot, 231 wdr->cmd_iohs[wd_seccnt], 0); 232 sn = bus_space_read_1(wdr->cmd_iot, 233 wdr->cmd_iohs[wd_sector], 0); 234 cl = bus_space_read_1(wdr->cmd_iot, 235 wdr->cmd_iohs[wd_cyl_lo], 0); 236 ch = bus_space_read_1(wdr->cmd_iot, 237 wdr->cmd_iohs[wd_cyl_hi], 0); 238 ATADEBUG_PRINT(("%s: port %d: sc=0x%x sn=0x%x " 239 "cl=0x%x ch=0x%x\n", 240 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 241 sc, sn, cl, ch), DEBUG_PROBE); 242 if (atabus_alloc_drives(chp, 1) != 0) 243 return; 244 /* 245 * sc and sn are supposed to be 0x1 for ATAPI, but in some 246 * cases we get wrong values here, so ignore it. 247 */ 248 s = splbio(); 249 if (cl == 0x14 && ch == 0xeb) 250 chp->ch_drive[0].drive_type = ATA_DRIVET_ATAPI; 251 else 252 chp->ch_drive[0].drive_type = ATA_DRIVET_ATA; 253 splx(s); 254 255 /* 256 * issue a reset in case only the interface part of the drive 257 * is up 258 */ 259 if (wdcreset(chp, RESET_SLEEP) != 0) 260 chp->ch_drive[0].drive_type = ATA_DRIVET_NONE; 261 break; 262 263 default: 264 break; 265 } 266 } 267 #endif /* NSATA > 0 */ 268 269 270 /* Test to see controller with at last one attached drive is there. 271 * Returns a bit for each possible drive found (0x01 for drive 0, 272 * 0x02 for drive 1). 273 * Logic: 274 * - If a status register is at 0xff, assume there is no drive here 275 * (ISA has pull-up resistors). Similarly if the status register has 276 * the value we last wrote to the bus (for IDE interfaces without pullups). 277 * If no drive at all -> return. 278 * - reset the controller, wait for it to complete (may take up to 31s !). 279 * If timeout -> return. 280 * - test ATA/ATAPI signatures. If at last one drive found -> return. 281 * - try an ATA command on the master. 282 */ 283 284 void 285 wdc_drvprobe(struct ata_channel *chp) 286 { 287 struct ataparams params; /* XXX: large struct */ 288 struct atac_softc *atac = chp->ch_atac; 289 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 290 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 291 u_int8_t st0 = 0, st1 = 0; 292 int i, j, error, s; 293 294 if (atabus_alloc_drives(chp, wdc->wdc_maxdrives) != 0) 295 return; 296 if (wdcprobe1(chp, 0) == 0) { 297 /* No drives, abort the attach here. */ 298 atabus_free_drives(chp); 299 return; 300 } 301 302 s = splbio(); 303 /* for ATA/OLD drives, wait for DRDY, 3s timeout */ 304 for (i = 0; i < mstohz(3000); i++) { 305 /* 306 * select drive 1 first, so that master is selected on 307 * exit from the loop 308 */ 309 if (chp->ch_ndrives > 1 && 310 chp->ch_drive[1].drive_type == ATA_DRIVET_ATA) { 311 if (wdc->select) 312 wdc->select(chp,1); 313 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 314 0, WDSD_IBM | 0x10); 315 delay(10); /* 400ns delay */ 316 st1 = bus_space_read_1(wdr->cmd_iot, 317 wdr->cmd_iohs[wd_status], 0); 318 } 319 if (chp->ch_drive[0].drive_type == ATA_DRIVET_ATA) { 320 if (wdc->select) 321 wdc->select(chp,0); 322 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 323 0, WDSD_IBM); 324 delay(10); /* 400ns delay */ 325 st0 = bus_space_read_1(wdr->cmd_iot, 326 wdr->cmd_iohs[wd_status], 0); 327 } 328 329 330 if ((chp->ch_drive[0].drive_type != ATA_DRIVET_ATA || 331 (st0 & WDCS_DRDY)) && 332 (chp->ch_ndrives < 2 || 333 chp->ch_drive[1].drive_type != ATA_DRIVET_ATA || 334 (st1 & WDCS_DRDY))) 335 break; 336 #ifdef WDC_NO_IDS 337 /* cannot tsleep here (can't enable IPL_BIO interrups), 338 * delay instead 339 */ 340 delay(1000000 / hz); 341 #else 342 tsleep(¶ms, PRIBIO, "atadrdy", 1); 343 #endif 344 } 345 if ((st0 & WDCS_DRDY) == 0 && 346 chp->ch_drive[0].drive_type != ATA_DRIVET_ATAPI) 347 chp->ch_drive[0].drive_type = ATA_DRIVET_NONE; 348 if (chp->ch_ndrives > 1 && (st1 & WDCS_DRDY) == 0 && 349 chp->ch_drive[1].drive_type != ATA_DRIVET_ATAPI) 350 chp->ch_drive[1].drive_type = ATA_DRIVET_NONE; 351 splx(s); 352 353 ATADEBUG_PRINT(("%s:%d: wait DRDY st0 0x%x st1 0x%x\n", 354 device_xname(atac->atac_dev), 355 chp->ch_channel, st0, st1), DEBUG_PROBE); 356 357 /* Wait a bit, some devices are weird just after a reset. */ 358 delay(5000); 359 360 for (i = 0; i < chp->ch_ndrives; i++) { 361 #if NATA_DMA 362 /* 363 * Init error counter so that an error withing the first xfers 364 * will trigger a downgrade 365 */ 366 chp->ch_drive[i].n_dmaerrs = NERRS_MAX-1; 367 #endif 368 369 /* If controller can't do 16bit flag the drives as 32bit */ 370 if ((atac->atac_cap & 371 (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) == ATAC_CAP_DATA32) { 372 s = splbio(); 373 chp->ch_drive[i].drive_flags |= ATA_DRIVE_CAP32; 374 splx(s); 375 } 376 if (chp->ch_drive[i].drive_type == ATA_DRIVET_NONE) 377 continue; 378 379 /* Shortcut in case we've been shutdown */ 380 if (chp->ch_flags & ATACH_SHUTDOWN) 381 return; 382 383 /* 384 * Issue an identify, to try to detect ghosts. 385 * Note that we can't use interrupts here, because if there 386 * is no devices, we will get a command aborted without 387 * interrupts. 388 */ 389 error = ata_get_params(&chp->ch_drive[i], 390 AT_WAIT | AT_POLL, ¶ms); 391 if (error != CMD_OK) { 392 tsleep(¶ms, PRIBIO, "atacnf", mstohz(1000)); 393 394 /* Shortcut in case we've been shutdown */ 395 if (chp->ch_flags & ATACH_SHUTDOWN) 396 return; 397 398 error = ata_get_params(&chp->ch_drive[i], 399 AT_WAIT | AT_POLL, ¶ms); 400 } 401 if (error != CMD_OK) { 402 ATADEBUG_PRINT(("%s:%d:%d: IDENTIFY failed (%d)\n", 403 device_xname(atac->atac_dev), 404 chp->ch_channel, i, error), DEBUG_PROBE); 405 s = splbio(); 406 if (chp->ch_drive[i].drive_type != ATA_DRIVET_ATA || 407 (wdc->cap & WDC_CAPABILITY_PREATA) == 0) { 408 chp->ch_drive[i].drive_type = ATA_DRIVET_NONE; 409 splx(s); 410 continue; 411 } 412 splx(s); 413 /* 414 * Pre-ATA drive ? 415 * Test registers writability (Error register not 416 * writable, but cyllo is), then try an ATA command. 417 */ 418 if (wdc->select) 419 wdc->select(chp,i); 420 bus_space_write_1(wdr->cmd_iot, 421 wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM | (i << 4)); 422 delay(10); /* 400ns delay */ 423 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 424 0, 0x58); 425 bus_space_write_1(wdr->cmd_iot, 426 wdr->cmd_iohs[wd_cyl_lo], 0, 0xa5); 427 if (bus_space_read_1(wdr->cmd_iot, 428 wdr->cmd_iohs[wd_error], 0) == 0x58 || 429 bus_space_read_1(wdr->cmd_iot, 430 wdr->cmd_iohs[wd_cyl_lo], 0) != 0xa5) { 431 ATADEBUG_PRINT(("%s:%d:%d: register " 432 "writability failed\n", 433 device_xname(atac->atac_dev), 434 chp->ch_channel, i), DEBUG_PROBE); 435 s = splbio(); 436 chp->ch_drive[i].drive_type = ATA_DRIVET_NONE; 437 splx(s); 438 continue; 439 } 440 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) { 441 ATADEBUG_PRINT(("%s:%d:%d: not ready\n", 442 device_xname(atac->atac_dev), 443 chp->ch_channel, i), DEBUG_PROBE); 444 s = splbio(); 445 chp->ch_drive[i].drive_type = ATA_DRIVET_NONE; 446 splx(s); 447 continue; 448 } 449 bus_space_write_1(wdr->cmd_iot, 450 wdr->cmd_iohs[wd_command], 0, WDCC_RECAL); 451 delay(10); /* 400ns delay */ 452 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) { 453 ATADEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n", 454 device_xname(atac->atac_dev), 455 chp->ch_channel, i), DEBUG_PROBE); 456 s = splbio(); 457 chp->ch_drive[i].drive_type = ATA_DRIVET_NONE; 458 splx(s); 459 } else { 460 s = splbio(); 461 for (j = 0; j < chp->ch_ndrives; j++) { 462 if (chp->ch_drive[i].drive_type != 463 ATA_DRIVET_NONE) { 464 chp->ch_drive[j].drive_type = 465 ATA_DRIVET_OLD; 466 } 467 } 468 splx(s); 469 } 470 } 471 } 472 } 473 474 int 475 wdcprobe(struct ata_channel *chp) 476 { 477 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 478 /* default reset method */ 479 if (wdc->reset == NULL) 480 wdc->reset = wdc_do_reset; 481 482 return (wdcprobe1(chp, 1)); 483 } 484 485 static int 486 wdcprobe1(struct ata_channel *chp, int poll) 487 { 488 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 489 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 490 u_int8_t st0 = 0, st1 = 0, sc __unused, sn __unused, cl, ch; 491 u_int8_t ret_value = 0x03; 492 u_int8_t drive; 493 int s; 494 /* XXX if poll, wdc_probe_count is 0. */ 495 int wdc_probe_count = 496 poll ? (WDC_PROBE_WAIT / WDCDELAY) 497 : (WDC_PROBE_WAIT * hz); 498 499 /* 500 * Sanity check to see if the wdc channel responds at all. 501 */ 502 503 s = splbio(); 504 if ((wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) { 505 while (wdc_probe_count-- > 0) { 506 if (wdc->select) 507 wdc->select(chp,0); 508 509 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 510 0, WDSD_IBM); 511 delay(10); /* 400ns delay */ 512 st0 = bus_space_read_1(wdr->cmd_iot, 513 wdr->cmd_iohs[wd_status], 0); 514 515 if (wdc->select) 516 wdc->select(chp,1); 517 518 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 519 0, WDSD_IBM | 0x10); 520 delay(10); /* 400ns delay */ 521 st1 = bus_space_read_1(wdr->cmd_iot, 522 wdr->cmd_iohs[wd_status], 0); 523 if ((st0 & WDCS_BSY) == 0) 524 break; 525 } 526 527 ATADEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n", 528 __func__, chp->ch_channel, st0, st1), DEBUG_PROBE); 529 530 if (st0 == 0xff || st0 == WDSD_IBM) 531 ret_value &= ~0x01; 532 if (st1 == 0xff || st1 == (WDSD_IBM | 0x10)) 533 ret_value &= ~0x02; 534 /* Register writability test, drive 0. */ 535 if (ret_value & 0x01) { 536 if (wdc->select) 537 wdc->select(chp,0); 538 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 539 0, WDSD_IBM); 540 bus_space_write_1(wdr->cmd_iot, 541 wdr->cmd_iohs[wd_cyl_lo], 0, 0x02); 542 cl = bus_space_read_1(wdr->cmd_iot, 543 wdr->cmd_iohs[wd_cyl_lo], 0); 544 if (cl != 0x02) { 545 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: " 546 "got 0x%x != 0x02\n", 547 __func__, chp->ch_channel, cl), 548 DEBUG_PROBE); 549 ret_value &= ~0x01; 550 } 551 bus_space_write_1(wdr->cmd_iot, 552 wdr->cmd_iohs[wd_cyl_lo], 0, 0x01); 553 cl = bus_space_read_1(wdr->cmd_iot, 554 wdr->cmd_iohs[wd_cyl_lo], 0); 555 if (cl != 0x01) { 556 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: " 557 "got 0x%x != 0x01\n", 558 __func__, chp->ch_channel, cl), 559 DEBUG_PROBE); 560 ret_value &= ~0x01; 561 } 562 bus_space_write_1(wdr->cmd_iot, 563 wdr->cmd_iohs[wd_sector], 0, 0x01); 564 cl = bus_space_read_1(wdr->cmd_iot, 565 wdr->cmd_iohs[wd_sector], 0); 566 if (cl != 0x01) { 567 ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: " 568 "got 0x%x != 0x01\n", 569 __func__, chp->ch_channel, cl), 570 DEBUG_PROBE); 571 ret_value &= ~0x01; 572 } 573 bus_space_write_1(wdr->cmd_iot, 574 wdr->cmd_iohs[wd_sector], 0, 0x02); 575 cl = bus_space_read_1(wdr->cmd_iot, 576 wdr->cmd_iohs[wd_sector], 0); 577 if (cl != 0x02) { 578 ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: " 579 "got 0x%x != 0x02\n", 580 __func__, chp->ch_channel, cl), 581 DEBUG_PROBE); 582 ret_value &= ~0x01; 583 } 584 cl = bus_space_read_1(wdr->cmd_iot, 585 wdr->cmd_iohs[wd_cyl_lo], 0); 586 if (cl != 0x01) { 587 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo(2): " 588 "got 0x%x != 0x01\n", 589 __func__, chp->ch_channel, cl), 590 DEBUG_PROBE); 591 ret_value &= ~0x01; 592 } 593 } 594 /* Register writability test, drive 1. */ 595 if (ret_value & 0x02) { 596 if (wdc->select) 597 wdc->select(chp,1); 598 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 599 0, WDSD_IBM | 0x10); 600 bus_space_write_1(wdr->cmd_iot, 601 wdr->cmd_iohs[wd_cyl_lo], 0, 0x02); 602 cl = bus_space_read_1(wdr->cmd_iot, 603 wdr->cmd_iohs[wd_cyl_lo], 0); 604 if (cl != 0x02) { 605 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: " 606 "got 0x%x != 0x02\n", 607 __func__, chp->ch_channel, cl), 608 DEBUG_PROBE); 609 ret_value &= ~0x02; 610 } 611 bus_space_write_1(wdr->cmd_iot, 612 wdr->cmd_iohs[wd_cyl_lo], 0, 0x01); 613 cl = bus_space_read_1(wdr->cmd_iot, 614 wdr->cmd_iohs[wd_cyl_lo], 0); 615 if (cl != 0x01) { 616 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: " 617 "got 0x%x != 0x01\n", 618 __func__, chp->ch_channel, cl), 619 DEBUG_PROBE); 620 ret_value &= ~0x02; 621 } 622 bus_space_write_1(wdr->cmd_iot, 623 wdr->cmd_iohs[wd_sector], 0, 0x01); 624 cl = bus_space_read_1(wdr->cmd_iot, 625 wdr->cmd_iohs[wd_sector], 0); 626 if (cl != 0x01) { 627 ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: " 628 "got 0x%x != 0x01\n", 629 __func__, chp->ch_channel, cl), 630 DEBUG_PROBE); 631 ret_value &= ~0x02; 632 } 633 bus_space_write_1(wdr->cmd_iot, 634 wdr->cmd_iohs[wd_sector], 0, 0x02); 635 cl = bus_space_read_1(wdr->cmd_iot, 636 wdr->cmd_iohs[wd_sector], 0); 637 if (cl != 0x02) { 638 ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: " 639 "got 0x%x != 0x02\n", 640 __func__, chp->ch_channel, cl), 641 DEBUG_PROBE); 642 ret_value &= ~0x02; 643 } 644 cl = bus_space_read_1(wdr->cmd_iot, 645 wdr->cmd_iohs[wd_cyl_lo], 0); 646 if (cl != 0x01) { 647 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo(2): " 648 "got 0x%x != 0x01\n", 649 __func__, chp->ch_channel, cl), 650 DEBUG_PROBE); 651 ret_value &= ~0x02; 652 } 653 } 654 655 if (ret_value == 0) { 656 splx(s); 657 return 0; 658 } 659 } 660 661 #if 0 /* XXX this break some ATA or ATAPI devices */ 662 /* 663 * reset bus. Also send an ATAPI_RESET to devices, in case there are 664 * ATAPI device out there which don't react to the bus reset 665 */ 666 if (ret_value & 0x01) { 667 if (wdc->select) 668 wdc->select(chp,0); 669 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 670 0, WDSD_IBM); 671 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, 672 ATAPI_SOFT_RESET); 673 } 674 if (ret_value & 0x02) { 675 if (wdc->select) 676 wdc->select(chp,0); 677 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 678 0, WDSD_IBM | 0x10); 679 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, 680 ATAPI_SOFT_RESET); 681 } 682 683 delay(5000); 684 #endif 685 686 wdc->reset(chp, RESET_POLL); 687 DELAY(2000); 688 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0); 689 690 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) 691 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 692 WDCTL_4BIT); 693 694 #ifdef WDC_NO_IDS 695 ret_value = __wdcwait_reset(chp, ret_value, RESET_POLL); 696 #else 697 splx(s); 698 ret_value = __wdcwait_reset(chp, ret_value, poll); 699 s = splbio(); 700 #endif 701 ATADEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n", 702 __func__, chp->ch_channel, ret_value), DEBUG_PROBE); 703 704 /* if reset failed, there's nothing here */ 705 if (ret_value == 0) { 706 splx(s); 707 return 0; 708 } 709 710 /* 711 * Test presence of drives. First test register signatures looking 712 * for ATAPI devices. If it's not an ATAPI and reset said there may 713 * be something here assume it's ATA or OLD. Ghost will be killed 714 * later in attach routine. 715 */ 716 for (drive = 0; drive < wdc->wdc_maxdrives; drive++) { 717 if ((ret_value & (0x01 << drive)) == 0) 718 continue; 719 if (wdc->select) 720 wdc->select(chp,drive); 721 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 722 WDSD_IBM | (drive << 4)); 723 delay(10); /* 400ns delay */ 724 /* Save registers contents */ 725 sc = bus_space_read_1(wdr->cmd_iot, 726 wdr->cmd_iohs[wd_seccnt], 0); 727 sn = bus_space_read_1(wdr->cmd_iot, 728 wdr->cmd_iohs[wd_sector], 0); 729 cl = bus_space_read_1(wdr->cmd_iot, 730 wdr->cmd_iohs[wd_cyl_lo], 0); 731 ch = bus_space_read_1(wdr->cmd_iot, 732 wdr->cmd_iohs[wd_cyl_hi], 0); 733 734 ATADEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x " 735 "cl=0x%x ch=0x%x\n", __func__, chp->ch_channel, drive, sc, 736 sn, cl, ch), DEBUG_PROBE); 737 /* 738 * sc & sn are supposed to be 0x1 for ATAPI but in some cases 739 * we get wrong values here, so ignore it. 740 */ 741 if (chp->ch_drive != NULL) { 742 if (cl == 0x14 && ch == 0xeb) { 743 chp->ch_drive[drive].drive_type = ATA_DRIVET_ATAPI; 744 } else { 745 chp->ch_drive[drive].drive_type = ATA_DRIVET_ATA; 746 } 747 } 748 } 749 /* 750 * Select an existing drive before lowering spl, some WDC_NO_IDS 751 * devices incorrectly assert IRQ on nonexistent slave 752 */ 753 if (ret_value & 0x01) { 754 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 755 WDSD_IBM); 756 (void)bus_space_read_1(wdr->cmd_iot, 757 wdr->cmd_iohs[wd_status], 0); 758 } 759 splx(s); 760 return (ret_value); 761 } 762 763 void 764 wdcattach(struct ata_channel *chp) 765 { 766 struct atac_softc *atac = chp->ch_atac; 767 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 768 769 KASSERT(wdc->wdc_maxdrives > 0 && wdc->wdc_maxdrives <= WDC_MAXDRIVES); 770 771 /* default data transfer methods */ 772 if (wdc->datain_pio == NULL) 773 wdc->datain_pio = wdc_datain_pio; 774 if (wdc->dataout_pio == NULL) 775 wdc->dataout_pio = wdc_dataout_pio; 776 /* default reset method */ 777 if (wdc->reset == NULL) 778 wdc->reset = wdc_do_reset; 779 780 /* initialise global data */ 781 if (atac->atac_bustype_ata == NULL) 782 atac->atac_bustype_ata = &wdc_ata_bustype; 783 if (atac->atac_probe == NULL) 784 atac->atac_probe = wdc_drvprobe; 785 #if NATAPIBUS > 0 786 if (atac->atac_atapibus_attach == NULL) 787 atac->atac_atapibus_attach = wdc_atapibus_attach; 788 #endif 789 790 ata_channel_attach(chp); 791 } 792 793 void 794 wdc_childdetached(device_t self, device_t child) 795 { 796 struct atac_softc *atac = device_private(self); 797 struct ata_channel *chp; 798 int i; 799 800 for (i = 0; i < atac->atac_nchannels; i++) { 801 chp = atac->atac_channels[i]; 802 if (child == chp->atabus) { 803 chp->atabus = NULL; 804 return; 805 } 806 } 807 } 808 809 int 810 wdcdetach(device_t self, int flags) 811 { 812 struct atac_softc *atac = device_private(self); 813 struct ata_channel *chp; 814 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 815 int i, error = 0; 816 817 for (i = 0; i < atac->atac_nchannels; i++) { 818 chp = atac->atac_channels[i]; 819 if (chp->atabus == NULL) 820 continue; 821 ATADEBUG_PRINT(("wdcdetach: %s: detaching %s\n", 822 device_xname(atac->atac_dev), device_xname(chp->atabus)), 823 DEBUG_DETACH); 824 if ((error = config_detach(chp->atabus, flags)) != 0) 825 return error; 826 } 827 if (adapt->adapt_refcnt != 0) 828 return EBUSY; 829 return 0; 830 } 831 832 /* restart an interrupted I/O */ 833 void 834 wdcrestart(void *v) 835 { 836 struct ata_channel *chp = v; 837 int s; 838 839 s = splbio(); 840 atastart(chp); 841 splx(s); 842 } 843 844 845 /* 846 * Interrupt routine for the controller. Acknowledge the interrupt, check for 847 * errors on the current operation, mark it done if necessary, and start the 848 * next request. Also check for a partially done transfer, and continue with 849 * the next chunk if so. 850 */ 851 int 852 wdcintr(void *arg) 853 { 854 struct ata_channel *chp = arg; 855 struct atac_softc *atac = chp->ch_atac; 856 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 857 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 858 struct ata_xfer *xfer; 859 int ret; 860 861 if (!device_is_active(atac->atac_dev)) { 862 ATADEBUG_PRINT(("wdcintr: deactivated controller\n"), 863 DEBUG_INTR); 864 return (0); 865 } 866 if ((chp->ch_flags & ATACH_IRQ_WAIT) == 0) { 867 ATADEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR); 868 /* try to clear the pending interrupt anyway */ 869 (void)bus_space_read_1(wdr->cmd_iot, 870 wdr->cmd_iohs[wd_status], 0); 871 return (0); 872 } 873 874 ATADEBUG_PRINT(("wdcintr\n"), DEBUG_INTR); 875 xfer = chp->ch_queue->active_xfer; 876 #ifdef DIAGNOSTIC 877 if (xfer == NULL) 878 panic("wdcintr: no xfer"); 879 if (xfer->c_chp != chp) { 880 printf("channel %d expected %d\n", xfer->c_chp->ch_channel, 881 chp->ch_channel); 882 panic("wdcintr: wrong channel"); 883 } 884 #endif 885 #if NATA_DMA || NATA_PIOBM 886 if (chp->ch_flags & ATACH_DMA_WAIT) { 887 wdc->dma_status = 888 (*wdc->dma_finish)(wdc->dma_arg, chp->ch_channel, 889 xfer->c_drive, WDC_DMAEND_END); 890 if (wdc->dma_status & WDC_DMAST_NOIRQ) { 891 /* IRQ not for us, not detected by DMA engine */ 892 return 0; 893 } 894 chp->ch_flags &= ~ATACH_DMA_WAIT; 895 } 896 #endif 897 chp->ch_flags &= ~ATACH_IRQ_WAIT; 898 KASSERT(xfer->c_intr != NULL); 899 ret = xfer->c_intr(chp, xfer, 1); 900 if (ret == 0) /* irq was not for us, still waiting for irq */ 901 chp->ch_flags |= ATACH_IRQ_WAIT; 902 return (ret); 903 } 904 905 /* Put all disk in RESET state */ 906 void 907 wdc_reset_drive(struct ata_drive_datas *drvp, int flags, uint32_t *sigp) 908 { 909 struct ata_channel *chp = drvp->chnl_softc; 910 911 KASSERT(sigp == NULL); 912 913 ATADEBUG_PRINT(("wdc_reset_drive %s:%d for drive %d\n", 914 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 915 drvp->drive), DEBUG_FUNCS); 916 917 ata_reset_channel(chp, flags); 918 } 919 920 void 921 wdc_reset_channel(struct ata_channel *chp, int flags) 922 { 923 TAILQ_HEAD(, ata_xfer) reset_xfer; 924 struct ata_xfer *xfer, *next_xfer; 925 #if NATA_DMA || NATA_PIOBM 926 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 927 #endif 928 TAILQ_INIT(&reset_xfer); 929 930 chp->ch_flags &= ~ATACH_IRQ_WAIT; 931 932 /* 933 * if the current command if on an ATAPI device, issue a 934 * ATAPI_SOFT_RESET 935 */ 936 xfer = chp->ch_queue->active_xfer; 937 if (xfer && xfer->c_chp == chp && (xfer->c_flags & C_ATAPI)) { 938 wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET); 939 if (flags & AT_WAIT) 940 tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1); 941 else 942 delay(1000); 943 } 944 945 /* reset the channel */ 946 if (flags & AT_WAIT) 947 (void) wdcreset(chp, RESET_SLEEP); 948 else 949 (void) wdcreset(chp, RESET_POLL); 950 951 /* 952 * wait a bit after reset; in case the DMA engines needs some time 953 * to recover. 954 */ 955 if (flags & AT_WAIT) 956 tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1); 957 else 958 delay(1000); 959 /* 960 * look for pending xfers. If we have a shared queue, we'll also reset 961 * the other channel if the current xfer is running on it. 962 * Then we'll dequeue only the xfers for this channel. 963 */ 964 if ((flags & AT_RST_NOCMD) == 0) { 965 /* 966 * move all xfers queued for this channel to the reset queue, 967 * and then process the current xfer and then the reset queue. 968 * We have to use a temporary queue because c_kill_xfer() 969 * may requeue commands. 970 */ 971 for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer); 972 xfer != NULL; xfer = next_xfer) { 973 next_xfer = TAILQ_NEXT(xfer, c_xferchain); 974 if (xfer->c_chp != chp) 975 continue; 976 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, 977 xfer, c_xferchain); 978 TAILQ_INSERT_TAIL(&reset_xfer, xfer, c_xferchain); 979 } 980 xfer = chp->ch_queue->active_xfer; 981 if (xfer) { 982 if (xfer->c_chp != chp) 983 ata_reset_channel(xfer->c_chp, flags); 984 else { 985 callout_stop(&chp->ch_callout); 986 #if NATA_DMA || NATA_PIOBM 987 /* 988 * If we're waiting for DMA, stop the 989 * DMA engine 990 */ 991 if (chp->ch_flags & ATACH_DMA_WAIT) { 992 (*wdc->dma_finish)( 993 wdc->dma_arg, 994 chp->ch_channel, 995 xfer->c_drive, 996 WDC_DMAEND_ABRT_QUIET); 997 chp->ch_flags &= ~ATACH_DMA_WAIT; 998 } 999 #endif 1000 chp->ch_queue->active_xfer = NULL; 1001 if ((flags & AT_RST_EMERG) == 0) 1002 xfer->c_kill_xfer( 1003 chp, xfer, KILL_RESET); 1004 } 1005 } 1006 1007 for (xfer = TAILQ_FIRST(&reset_xfer); 1008 xfer != NULL; xfer = next_xfer) { 1009 next_xfer = TAILQ_NEXT(xfer, c_xferchain); 1010 TAILQ_REMOVE(&reset_xfer, xfer, c_xferchain); 1011 if ((flags & AT_RST_EMERG) == 0) 1012 xfer->c_kill_xfer(chp, xfer, KILL_RESET); 1013 } 1014 } 1015 } 1016 1017 static int 1018 wdcreset(struct ata_channel *chp, int poll) 1019 { 1020 struct atac_softc *atac = chp->ch_atac; 1021 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1022 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1023 int drv_mask1, drv_mask2; 1024 1025 #ifdef WDC_NO_IDS 1026 poll = RESET_POLL; 1027 #endif 1028 wdc->reset(chp, poll); 1029 1030 drv_mask1 = (chp->ch_drive[0].drive_type != ATA_DRIVET_NONE) ? 0x01:0x00; 1031 if (chp->ch_ndrives > 1) 1032 drv_mask1 |= 1033 (chp->ch_drive[1].drive_type != ATA_DRIVET_NONE) ? 0x02:0x00; 1034 drv_mask2 = __wdcwait_reset(chp, drv_mask1, 1035 (poll == RESET_SLEEP) ? 0 : 1); 1036 if (drv_mask2 != drv_mask1) { 1037 aprint_error("%s channel %d: reset failed for", 1038 device_xname(atac->atac_dev), chp->ch_channel); 1039 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0) 1040 aprint_normal(" drive 0"); 1041 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0) 1042 aprint_normal(" drive 1"); 1043 aprint_normal("\n"); 1044 } 1045 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) 1046 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1047 WDCTL_4BIT); 1048 1049 return (drv_mask1 != drv_mask2) ? 1 : 0; 1050 } 1051 1052 void 1053 wdc_do_reset(struct ata_channel *chp, int poll) 1054 { 1055 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1056 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1057 int s = 0; 1058 1059 if (poll != RESET_SLEEP) 1060 s = splbio(); 1061 if (wdc->select) 1062 wdc->select(chp,0); 1063 /* master */ 1064 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM); 1065 delay(10); /* 400ns delay */ 1066 /* assert SRST, wait for reset to complete */ 1067 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) { 1068 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1069 WDCTL_RST | WDCTL_IDS | WDCTL_4BIT); 1070 delay(2000); 1071 } 1072 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0); 1073 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) 1074 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1075 WDCTL_4BIT | WDCTL_IDS); 1076 delay(10); /* 400ns delay */ 1077 if (poll != RESET_SLEEP) { 1078 /* ACK interrupt in case there is one pending left */ 1079 if (wdc->irqack) 1080 wdc->irqack(chp); 1081 splx(s); 1082 } 1083 } 1084 1085 static int 1086 __wdcwait_reset(struct ata_channel *chp, int drv_mask, int poll) 1087 { 1088 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1089 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1090 int timeout, nloop; 1091 u_int8_t st0 = 0, st1 = 0; 1092 #ifdef ATADEBUG 1093 u_int8_t sc0 = 0, sn0 = 0, cl0 = 0, ch0 = 0; 1094 u_int8_t sc1 = 0, sn1 = 0, cl1 = 0, ch1 = 0; 1095 #endif 1096 if (poll) 1097 nloop = WDCNDELAY_RST; 1098 else 1099 nloop = WDC_RESET_WAIT * hz / 1000; 1100 /* wait for BSY to deassert */ 1101 for (timeout = 0; timeout < nloop; timeout++) { 1102 if ((drv_mask & 0x01) != 0) { 1103 if (wdc->select) 1104 wdc->select(chp,0); 1105 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 1106 0, WDSD_IBM); /* master */ 1107 delay(10); 1108 st0 = bus_space_read_1(wdr->cmd_iot, 1109 wdr->cmd_iohs[wd_status], 0); 1110 #ifdef ATADEBUG 1111 sc0 = bus_space_read_1(wdr->cmd_iot, 1112 wdr->cmd_iohs[wd_seccnt], 0); 1113 sn0 = bus_space_read_1(wdr->cmd_iot, 1114 wdr->cmd_iohs[wd_sector], 0); 1115 cl0 = bus_space_read_1(wdr->cmd_iot, 1116 wdr->cmd_iohs[wd_cyl_lo], 0); 1117 ch0 = bus_space_read_1(wdr->cmd_iot, 1118 wdr->cmd_iohs[wd_cyl_hi], 0); 1119 #endif 1120 } 1121 if ((drv_mask & 0x02) != 0) { 1122 if (wdc->select) 1123 wdc->select(chp,1); 1124 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 1125 0, WDSD_IBM | 0x10); /* slave */ 1126 delay(10); 1127 st1 = bus_space_read_1(wdr->cmd_iot, 1128 wdr->cmd_iohs[wd_status], 0); 1129 #ifdef ATADEBUG 1130 sc1 = bus_space_read_1(wdr->cmd_iot, 1131 wdr->cmd_iohs[wd_seccnt], 0); 1132 sn1 = bus_space_read_1(wdr->cmd_iot, 1133 wdr->cmd_iohs[wd_sector], 0); 1134 cl1 = bus_space_read_1(wdr->cmd_iot, 1135 wdr->cmd_iohs[wd_cyl_lo], 0); 1136 ch1 = bus_space_read_1(wdr->cmd_iot, 1137 wdr->cmd_iohs[wd_cyl_hi], 0); 1138 #endif 1139 } 1140 1141 if ((drv_mask & 0x01) == 0) { 1142 /* no master */ 1143 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) { 1144 /* No master, slave is ready, it's done */ 1145 goto end; 1146 } 1147 if ((drv_mask & 0x02) == 0) { 1148 /* No master, no slave: it's done */ 1149 goto end; 1150 } 1151 } else if ((drv_mask & 0x02) == 0) { 1152 /* no slave */ 1153 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) { 1154 /* No slave, master is ready, it's done */ 1155 goto end; 1156 } 1157 } else { 1158 /* Wait for both master and slave to be ready */ 1159 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) { 1160 goto end; 1161 } 1162 } 1163 if (poll) 1164 delay(WDCDELAY); 1165 else 1166 tsleep(&nloop, PRIBIO, "atarst", 1); 1167 } 1168 /* Reset timed out. Maybe it's because drv_mask was not right */ 1169 if (st0 & WDCS_BSY) 1170 drv_mask &= ~0x01; 1171 if (st1 & WDCS_BSY) 1172 drv_mask &= ~0x02; 1173 end: 1174 ATADEBUG_PRINT(("%s:%d:0: after reset, sc=0x%x sn=0x%x " 1175 "cl=0x%x ch=0x%x\n", 1176 device_xname(chp->ch_atac->atac_dev), 1177 chp->ch_channel, sc0, sn0, cl0, ch0), DEBUG_PROBE); 1178 ATADEBUG_PRINT(("%s:%d:1: after reset, sc=0x%x sn=0x%x " 1179 "cl=0x%x ch=0x%x\n", 1180 device_xname(chp->ch_atac->atac_dev), 1181 chp->ch_channel, sc1, sn1, cl1, ch1), DEBUG_PROBE); 1182 1183 ATADEBUG_PRINT(("%s:%d: wdcwait_reset() end, st0=0x%x st1=0x%x\n", 1184 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1185 st0, st1), DEBUG_PROBE); 1186 1187 return drv_mask; 1188 } 1189 1190 /* 1191 * Wait for a drive to be !BSY, and have mask in its status register. 1192 * return -1 for a timeout after "timeout" ms. 1193 */ 1194 static int 1195 __wdcwait(struct ata_channel *chp, int mask, int bits, int timeout) 1196 { 1197 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1198 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1199 u_char status; 1200 int xtime = 0; 1201 1202 ATADEBUG_PRINT(("__wdcwait %s:%d\n", 1203 device_xname(chp->ch_atac->atac_dev), 1204 chp->ch_channel), DEBUG_STATUS); 1205 chp->ch_error = 0; 1206 1207 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */ 1208 1209 for (;;) { 1210 chp->ch_status = status = 1211 bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0); 1212 if ((status & (WDCS_BSY | mask)) == bits) 1213 break; 1214 if (++xtime > timeout) { 1215 ATADEBUG_PRINT(("__wdcwait: timeout (time=%d), " 1216 "status %x error %x (mask 0x%x bits 0x%x)\n", 1217 xtime, status, 1218 bus_space_read_1(wdr->cmd_iot, 1219 wdr->cmd_iohs[wd_error], 0), mask, bits), 1220 DEBUG_STATUS | DEBUG_PROBE | DEBUG_DELAY); 1221 return(WDCWAIT_TOUT); 1222 } 1223 delay(WDCDELAY); 1224 } 1225 #ifdef ATADEBUG 1226 if (xtime > 0 && (atadebug_mask & DEBUG_DELAY)) 1227 printf("__wdcwait: did busy-wait, time=%d\n", xtime); 1228 #endif 1229 if (status & WDCS_ERR) 1230 chp->ch_error = bus_space_read_1(wdr->cmd_iot, 1231 wdr->cmd_iohs[wd_error], 0); 1232 #ifdef WDCNDELAY_DEBUG 1233 /* After autoconfig, there should be no long delays. */ 1234 if (!cold && xtime > WDCNDELAY_DEBUG) { 1235 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1236 if (xfer == NULL) 1237 printf("%s channel %d: warning: busy-wait took %dus\n", 1238 device_xname(chp->ch_atac->atac_dev), 1239 chp->ch_channel, WDCDELAY * xtime); 1240 else 1241 printf("%s:%d:%d: warning: busy-wait took %dus\n", 1242 device_xname(chp->ch_atac->atac_dev), 1243 chp->ch_channel, xfer->c_drive, 1244 WDCDELAY * xtime); 1245 } 1246 #endif 1247 return(WDCWAIT_OK); 1248 } 1249 1250 /* 1251 * Call __wdcwait(), polling using tsleep() or waking up the kernel 1252 * thread if possible 1253 */ 1254 int 1255 wdcwait(struct ata_channel *chp, int mask, int bits, int timeout, int flags) 1256 { 1257 int error, i, timeout_hz = mstohz(timeout); 1258 1259 if (timeout_hz == 0 || 1260 (flags & (AT_WAIT | AT_POLL)) == AT_POLL) 1261 error = __wdcwait(chp, mask, bits, timeout); 1262 else { 1263 error = __wdcwait(chp, mask, bits, WDCDELAY_POLL); 1264 if (error != 0) { 1265 if ((chp->ch_flags & ATACH_TH_RUN) || 1266 (flags & AT_WAIT)) { 1267 /* 1268 * we're running in the channel thread 1269 * or some userland thread context 1270 */ 1271 for (i = 0; i < timeout_hz; i++) { 1272 if (__wdcwait(chp, mask, bits, 1273 WDCDELAY_POLL) == 0) { 1274 error = 0; 1275 break; 1276 } 1277 tsleep(&chp, PRIBIO, "atapoll", 1); 1278 } 1279 } else { 1280 /* 1281 * we're probably in interrupt context, 1282 * ask the thread to come back here 1283 */ 1284 #ifdef DIAGNOSTIC 1285 if (chp->ch_queue->queue_freeze > 0) 1286 panic("wdcwait: queue_freeze"); 1287 #endif 1288 chp->ch_queue->queue_freeze++; 1289 wakeup(&chp->ch_thread); 1290 return(WDCWAIT_THR); 1291 } 1292 } 1293 } 1294 return (error); 1295 } 1296 1297 1298 #if NATA_DMA 1299 /* 1300 * Busy-wait for DMA to complete 1301 */ 1302 int 1303 wdc_dmawait(struct ata_channel *chp, struct ata_xfer *xfer, int timeout) 1304 { 1305 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1306 int xtime; 1307 1308 for (xtime = 0; xtime < timeout * 1000 / WDCDELAY; xtime++) { 1309 wdc->dma_status = 1310 (*wdc->dma_finish)(wdc->dma_arg, 1311 chp->ch_channel, xfer->c_drive, WDC_DMAEND_END); 1312 if ((wdc->dma_status & WDC_DMAST_NOIRQ) == 0) 1313 return 0; 1314 delay(WDCDELAY); 1315 } 1316 /* timeout, force a DMA halt */ 1317 wdc->dma_status = (*wdc->dma_finish)(wdc->dma_arg, 1318 chp->ch_channel, xfer->c_drive, WDC_DMAEND_ABRT); 1319 return 1; 1320 } 1321 #endif 1322 1323 void 1324 wdctimeout(void *arg) 1325 { 1326 struct ata_channel *chp = (struct ata_channel *)arg; 1327 #if NATA_DMA || NATA_PIOBM 1328 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1329 #endif 1330 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1331 int s; 1332 1333 ATADEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS); 1334 1335 s = splbio(); 1336 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) { 1337 __wdcerror(chp, "lost interrupt"); 1338 printf("\ttype: %s tc_bcount: %d tc_skip: %d\n", 1339 (xfer->c_flags & C_ATAPI) ? "atapi" : "ata", 1340 xfer->c_bcount, 1341 xfer->c_skip); 1342 #if NATA_DMA || NATA_PIOBM 1343 if (chp->ch_flags & ATACH_DMA_WAIT) { 1344 wdc->dma_status = 1345 (*wdc->dma_finish)(wdc->dma_arg, 1346 chp->ch_channel, xfer->c_drive, 1347 WDC_DMAEND_ABRT); 1348 chp->ch_flags &= ~ATACH_DMA_WAIT; 1349 } 1350 #endif 1351 /* 1352 * Call the interrupt routine. If we just missed an interrupt, 1353 * it will do what's needed. Else, it will take the needed 1354 * action (reset the device). 1355 * Before that we need to reinstall the timeout callback, 1356 * in case it will miss another irq while in this transfer 1357 * We arbitray chose it to be 1s 1358 */ 1359 callout_reset(&chp->ch_callout, hz, wdctimeout, chp); 1360 xfer->c_flags |= C_TIMEOU; 1361 chp->ch_flags &= ~ATACH_IRQ_WAIT; 1362 KASSERT(xfer->c_intr != NULL); 1363 xfer->c_intr(chp, xfer, 1); 1364 } else 1365 __wdcerror(chp, "missing untimeout"); 1366 splx(s); 1367 } 1368 1369 int 1370 wdc_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c) 1371 { 1372 struct ata_channel *chp = drvp->chnl_softc; 1373 struct ata_xfer *xfer; 1374 int s, ret; 1375 1376 ATADEBUG_PRINT(("wdc_exec_command %s:%d:%d\n", 1377 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1378 drvp->drive), DEBUG_FUNCS); 1379 1380 /* set up an xfer and queue. Wait for completion */ 1381 xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP : 1382 ATAXF_NOSLEEP); 1383 if (xfer == NULL) { 1384 return ATACMD_TRY_AGAIN; 1385 } 1386 1387 if (chp->ch_atac->atac_cap & ATAC_CAP_NOIRQ) 1388 ata_c->flags |= AT_POLL; 1389 if (ata_c->flags & AT_POLL) 1390 xfer->c_flags |= C_POLL; 1391 if (ata_c->flags & AT_WAIT) 1392 xfer->c_flags |= C_WAIT; 1393 xfer->c_drive = drvp->drive; 1394 xfer->c_databuf = ata_c->data; 1395 xfer->c_bcount = ata_c->bcount; 1396 xfer->c_cmd = ata_c; 1397 xfer->c_start = __wdccommand_start; 1398 xfer->c_intr = __wdccommand_intr; 1399 xfer->c_kill_xfer = __wdccommand_kill_xfer; 1400 1401 s = splbio(); 1402 ata_exec_xfer(chp, xfer); 1403 #ifdef DIAGNOSTIC 1404 if ((ata_c->flags & AT_POLL) != 0 && 1405 (ata_c->flags & AT_DONE) == 0) 1406 panic("wdc_exec_command: polled command not done"); 1407 #endif 1408 if (ata_c->flags & AT_DONE) { 1409 ret = ATACMD_COMPLETE; 1410 } else { 1411 if (ata_c->flags & AT_WAIT) { 1412 while ((ata_c->flags & AT_DONE) == 0) { 1413 tsleep(ata_c, PRIBIO, "wdccmd", 0); 1414 } 1415 ret = ATACMD_COMPLETE; 1416 } else { 1417 ret = ATACMD_QUEUED; 1418 } 1419 } 1420 splx(s); 1421 return ret; 1422 } 1423 1424 static void 1425 __wdccommand_start(struct ata_channel *chp, struct ata_xfer *xfer) 1426 { 1427 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1428 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1429 int drive = xfer->c_drive; 1430 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0; 1431 struct ata_command *ata_c = xfer->c_cmd; 1432 1433 ATADEBUG_PRINT(("__wdccommand_start %s:%d:%d\n", 1434 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1435 xfer->c_drive), 1436 DEBUG_FUNCS); 1437 1438 if (wdc->select) 1439 wdc->select(chp,drive); 1440 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1441 WDSD_IBM | (drive << 4)); 1442 switch(wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ, 1443 ata_c->r_st_bmask, ata_c->timeout, wait_flags)) { 1444 case WDCWAIT_OK: 1445 break; 1446 case WDCWAIT_TOUT: 1447 ata_c->flags |= AT_TIMEOU; 1448 __wdccommand_done(chp, xfer); 1449 return; 1450 case WDCWAIT_THR: 1451 return; 1452 } 1453 if (ata_c->flags & AT_POLL) { 1454 /* polled command, disable interrupts */ 1455 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) 1456 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, 1457 wd_aux_ctlr, WDCTL_4BIT | WDCTL_IDS); 1458 } 1459 if ((ata_c->flags & AT_LBA48) != 0) { 1460 wdccommandext(chp, drive, ata_c->r_command, 1461 ata_c->r_lba, ata_c->r_count, ata_c->r_features, 1462 ata_c->r_device & ~0x10); 1463 } else { 1464 wdccommand(chp, drive, ata_c->r_command, 1465 (ata_c->r_lba >> 8) & 0xffff, 1466 WDSD_IBM | (drive << 4) | 1467 (((ata_c->flags & AT_LBA) != 0) ? WDSD_LBA : 0) | 1468 ((ata_c->r_lba >> 24) & 0x0f), 1469 ata_c->r_lba & 0xff, 1470 ata_c->r_count & 0xff, 1471 ata_c->r_features & 0xff); 1472 } 1473 1474 if ((ata_c->flags & AT_POLL) == 0) { 1475 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 1476 callout_reset(&chp->ch_callout, ata_c->timeout / 1000 * hz, 1477 wdctimeout, chp); 1478 return; 1479 } 1480 /* 1481 * Polled command. Wait for drive ready or drq. Done in intr(). 1482 * Wait for at last 400ns for status bit to be valid. 1483 */ 1484 delay(10); /* 400ns delay */ 1485 __wdccommand_intr(chp, xfer, 0); 1486 } 1487 1488 static int 1489 __wdccommand_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq) 1490 { 1491 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1492 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1493 struct ata_command *ata_c = xfer->c_cmd; 1494 int bcount = ata_c->bcount; 1495 char *data = ata_c->data; 1496 int wflags; 1497 int drive_flags; 1498 1499 if (ata_c->r_command == WDCC_IDENTIFY || 1500 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) { 1501 /* 1502 * The IDENTIFY data has been designed as an array of 1503 * u_int16_t, so we can byteswap it on the fly. 1504 * Historically it's what we have always done so keeping it 1505 * here ensure binary backward compatibility. 1506 */ 1507 drive_flags = ATA_DRIVE_NOSTREAM | 1508 chp->ch_drive[xfer->c_drive].drive_flags; 1509 } else { 1510 /* 1511 * Other data structure are opaque and should be transfered 1512 * as is. 1513 */ 1514 drive_flags = chp->ch_drive[xfer->c_drive].drive_flags; 1515 } 1516 1517 #ifdef WDC_NO_IDS 1518 wflags = AT_POLL; 1519 #else 1520 if ((ata_c->flags & (AT_WAIT | AT_POLL)) == (AT_WAIT | AT_POLL)) { 1521 /* both wait and poll, we can tsleep here */ 1522 wflags = AT_WAIT | AT_POLL; 1523 } else { 1524 wflags = AT_POLL; 1525 } 1526 #endif 1527 1528 again: 1529 ATADEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n", 1530 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1531 xfer->c_drive), DEBUG_INTR); 1532 /* 1533 * after a ATAPI_SOFT_RESET, the device will have released the bus. 1534 * Reselect again, it doesn't hurt for others commands, and the time 1535 * penalty for the extra register write is acceptable, 1536 * wdc_exec_command() isn't called often (mostly for autoconfig) 1537 */ 1538 if ((xfer->c_flags & C_ATAPI) != 0) { 1539 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1540 WDSD_IBM | (xfer->c_drive << 4)); 1541 } 1542 if ((ata_c->flags & AT_XFDONE) != 0) { 1543 /* 1544 * We have completed a data xfer. The drive should now be 1545 * in its initial state 1546 */ 1547 if (wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ, 1548 ata_c->r_st_bmask, (irq == 0) ? ata_c->timeout : 0, 1549 wflags) == WDCWAIT_TOUT) { 1550 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 1551 return 0; /* IRQ was not for us */ 1552 ata_c->flags |= AT_TIMEOU; 1553 } 1554 goto out; 1555 } 1556 if (wdcwait(chp, ata_c->r_st_pmask, ata_c->r_st_pmask, 1557 (irq == 0) ? ata_c->timeout : 0, wflags) == WDCWAIT_TOUT) { 1558 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 1559 return 0; /* IRQ was not for us */ 1560 ata_c->flags |= AT_TIMEOU; 1561 goto out; 1562 } 1563 if (wdc->irqack) 1564 wdc->irqack(chp); 1565 if (ata_c->flags & AT_READ) { 1566 if ((chp->ch_status & WDCS_DRQ) == 0) { 1567 ata_c->flags |= AT_TIMEOU; 1568 goto out; 1569 } 1570 wdc->datain_pio(chp, drive_flags, data, bcount); 1571 /* at this point the drive should be in its initial state */ 1572 ata_c->flags |= AT_XFDONE; 1573 /* 1574 * XXX checking the status register again here cause some 1575 * hardware to timeout. 1576 */ 1577 } else if (ata_c->flags & AT_WRITE) { 1578 if ((chp->ch_status & WDCS_DRQ) == 0) { 1579 ata_c->flags |= AT_TIMEOU; 1580 goto out; 1581 } 1582 wdc->dataout_pio(chp, drive_flags, data, bcount); 1583 ata_c->flags |= AT_XFDONE; 1584 if ((ata_c->flags & AT_POLL) == 0) { 1585 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 1586 callout_reset(&chp->ch_callout, 1587 mstohz(ata_c->timeout), wdctimeout, chp); 1588 return 1; 1589 } else { 1590 goto again; 1591 } 1592 } 1593 out: 1594 __wdccommand_done(chp, xfer); 1595 return 1; 1596 } 1597 1598 static void 1599 __wdccommand_done(struct ata_channel *chp, struct ata_xfer *xfer) 1600 { 1601 struct atac_softc *atac = chp->ch_atac; 1602 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1603 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1604 struct ata_command *ata_c = xfer->c_cmd; 1605 1606 ATADEBUG_PRINT(("__wdccommand_done %s:%d:%d flags 0x%x\n", 1607 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 1608 ata_c->flags), DEBUG_FUNCS); 1609 1610 1611 if (chp->ch_status & WDCS_DWF) 1612 ata_c->flags |= AT_DF; 1613 if (chp->ch_status & WDCS_ERR) { 1614 ata_c->flags |= AT_ERROR; 1615 ata_c->r_error = chp->ch_error; 1616 } 1617 if ((ata_c->flags & AT_READREG) != 0 && 1618 device_is_active(atac->atac_dev) && 1619 (ata_c->flags & (AT_ERROR | AT_DF)) == 0) { 1620 ata_c->r_status = bus_space_read_1(wdr->cmd_iot, 1621 wdr->cmd_iohs[wd_status], 0); 1622 ata_c->r_error = bus_space_read_1(wdr->cmd_iot, 1623 wdr->cmd_iohs[wd_error], 0); 1624 ata_c->r_count = bus_space_read_1(wdr->cmd_iot, 1625 wdr->cmd_iohs[wd_seccnt], 0); 1626 ata_c->r_lba = (uint64_t)bus_space_read_1(wdr->cmd_iot, 1627 wdr->cmd_iohs[wd_sector], 0) << 0; 1628 ata_c->r_lba |= (uint64_t)bus_space_read_1(wdr->cmd_iot, 1629 wdr->cmd_iohs[wd_cyl_lo], 0) << 8; 1630 ata_c->r_lba |= (uint64_t)bus_space_read_1(wdr->cmd_iot, 1631 wdr->cmd_iohs[wd_cyl_hi], 0) << 16; 1632 ata_c->r_device = bus_space_read_1(wdr->cmd_iot, 1633 wdr->cmd_iohs[wd_sdh], 0); 1634 1635 if ((ata_c->flags & AT_LBA48) != 0) { 1636 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) { 1637 if ((ata_c->flags & AT_POLL) != 0) 1638 bus_space_write_1(wdr->ctl_iot, 1639 wdr->ctl_ioh, wd_aux_ctlr, 1640 WDCTL_HOB|WDCTL_4BIT|WDCTL_IDS); 1641 else 1642 bus_space_write_1(wdr->ctl_iot, 1643 wdr->ctl_ioh, wd_aux_ctlr, 1644 WDCTL_HOB|WDCTL_4BIT); 1645 } 1646 ata_c->r_count |= bus_space_read_1(wdr->cmd_iot, 1647 wdr->cmd_iohs[wd_seccnt], 0) << 8; 1648 ata_c->r_lba |= (uint64_t)bus_space_read_1(wdr->cmd_iot, 1649 wdr->cmd_iohs[wd_sector], 0) << 24; 1650 ata_c->r_lba |= (uint64_t)bus_space_read_1(wdr->cmd_iot, 1651 wdr->cmd_iohs[wd_cyl_lo], 0) << 32; 1652 ata_c->r_lba |= (uint64_t)bus_space_read_1(wdr->cmd_iot, 1653 wdr->cmd_iohs[wd_cyl_hi], 0) << 40; 1654 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) { 1655 if ((ata_c->flags & AT_POLL) != 0) 1656 bus_space_write_1(wdr->ctl_iot, 1657 wdr->ctl_ioh, wd_aux_ctlr, 1658 WDCTL_4BIT|WDCTL_IDS); 1659 else 1660 bus_space_write_1(wdr->ctl_iot, 1661 wdr->ctl_ioh, wd_aux_ctlr, 1662 WDCTL_4BIT); 1663 } 1664 } else { 1665 ata_c->r_lba |= 1666 (uint64_t)(ata_c->r_device & 0x0f) << 24; 1667 } 1668 ata_c->r_device &= 0xf0; 1669 } 1670 callout_stop(&chp->ch_callout); 1671 chp->ch_queue->active_xfer = NULL; 1672 if (ata_c->flags & AT_POLL) { 1673 /* enable interrupts */ 1674 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL)) 1675 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, 1676 wd_aux_ctlr, WDCTL_4BIT); 1677 delay(10); /* some drives need a little delay here */ 1678 } 1679 if (chp->ch_drive[xfer->c_drive].drive_flags & ATA_DRIVE_WAITDRAIN) { 1680 __wdccommand_kill_xfer(chp, xfer, KILL_GONE); 1681 chp->ch_drive[xfer->c_drive].drive_flags &= ~ATA_DRIVE_WAITDRAIN; 1682 wakeup(&chp->ch_queue->active_xfer); 1683 } else 1684 __wdccommand_done_end(chp, xfer); 1685 } 1686 1687 static void 1688 __wdccommand_done_end(struct ata_channel *chp, struct ata_xfer *xfer) 1689 { 1690 struct ata_command *ata_c = xfer->c_cmd; 1691 1692 ata_c->flags |= AT_DONE; 1693 ata_free_xfer(chp, xfer); 1694 if (ata_c->flags & AT_WAIT) 1695 wakeup(ata_c); 1696 else if (ata_c->callback) 1697 ata_c->callback(ata_c->callback_arg); 1698 atastart(chp); 1699 return; 1700 } 1701 1702 static void 1703 __wdccommand_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, 1704 int reason) 1705 { 1706 struct ata_command *ata_c = xfer->c_cmd; 1707 1708 switch (reason) { 1709 case KILL_GONE: 1710 ata_c->flags |= AT_GONE; 1711 break; 1712 case KILL_RESET: 1713 ata_c->flags |= AT_RESET; 1714 break; 1715 default: 1716 printf("__wdccommand_kill_xfer: unknown reason %d\n", 1717 reason); 1718 panic("__wdccommand_kill_xfer"); 1719 } 1720 __wdccommand_done_end(chp, xfer); 1721 } 1722 1723 /* 1724 * Send a command. The drive should be ready. 1725 * Assumes interrupts are blocked. 1726 */ 1727 void 1728 wdccommand(struct ata_channel *chp, u_int8_t drive, u_int8_t command, 1729 u_int16_t cylin, u_int8_t head, u_int8_t sector, u_int8_t count, 1730 u_int8_t features) 1731 { 1732 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1733 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1734 1735 ATADEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d " 1736 "sector=%d count=%d features=%d\n", 1737 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, drive, 1738 command, cylin, head, sector, count, features), DEBUG_FUNCS); 1739 1740 if (wdc->select) 1741 wdc->select(chp,drive); 1742 1743 /* Select drive, head, and addressing mode. */ 1744 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1745 WDSD_IBM | (drive << 4) | head); 1746 /* Load parameters into the wd_features register. */ 1747 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0, 1748 features); 1749 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 0, count); 1750 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sector], 0, sector); 1751 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0, cylin); 1752 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 1753 0, cylin >> 8); 1754 1755 /* Send command. */ 1756 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command); 1757 return; 1758 } 1759 1760 /* 1761 * Send a 48-bit addressing command. The drive should be ready. 1762 * Assumes interrupts are blocked. 1763 */ 1764 void 1765 wdccommandext(struct ata_channel *chp, u_int8_t drive, u_int8_t command, 1766 u_int64_t blkno, u_int16_t count, u_int16_t features, u_int8_t device) 1767 { 1768 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1769 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1770 1771 ATADEBUG_PRINT(("wdccommandext %s:%d:%d: command=0x%02x " 1772 "blkno=0x%012"PRIx64" count=0x%04x features=0x%04x " 1773 "device=0x%02x\n", device_xname(chp->ch_atac->atac_dev), 1774 chp->ch_channel, drive, command, blkno, count, features, device), 1775 DEBUG_FUNCS); 1776 1777 KASSERT(drive < wdc->wdc_maxdrives); 1778 1779 if (wdc->select) 1780 wdc->select(chp,drive); 1781 1782 /* Select drive, head, and addressing mode. */ 1783 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1784 (drive << 4) | device); 1785 1786 if (wdc->cap & WDC_CAPABILITY_WIDEREGS) { 1787 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 1788 0, features); 1789 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 1790 0, count); 1791 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 1792 0, (((blkno >> 16) & 0xff00) | (blkno & 0x00ff))); 1793 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi], 1794 0, (((blkno >> 24) & 0xff00) | ((blkno >> 8) & 0x00ff))); 1795 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi], 1796 0, (((blkno >> 32) & 0xff00) | ((blkno >> 16) & 0x00ff))); 1797 } else { 1798 /* previous */ 1799 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 1800 0, features >> 8); 1801 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 1802 0, count >> 8); 1803 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 1804 0, blkno >> 24); 1805 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi], 1806 0, blkno >> 32); 1807 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi], 1808 0, blkno >> 40); 1809 1810 /* current */ 1811 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 1812 0, features); 1813 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 1814 0, count); 1815 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 1816 0, blkno); 1817 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi], 1818 0, blkno >> 8); 1819 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi], 1820 0, blkno >> 16); 1821 } 1822 1823 /* Send command. */ 1824 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command); 1825 return; 1826 } 1827 1828 /* 1829 * Simplified version of wdccommand(). Unbusy/ready/drq must be 1830 * tested by the caller. 1831 */ 1832 void 1833 wdccommandshort(struct ata_channel *chp, int drive, int command) 1834 { 1835 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1836 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1837 1838 ATADEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n", 1839 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, drive, 1840 command), DEBUG_FUNCS); 1841 1842 if (wdc->select) 1843 wdc->select(chp,drive); 1844 1845 /* Select drive. */ 1846 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1847 WDSD_IBM | (drive << 4)); 1848 1849 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command); 1850 } 1851 1852 static void 1853 __wdcerror(struct ata_channel *chp, const char *msg) 1854 { 1855 struct atac_softc *atac = chp->ch_atac; 1856 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1857 1858 if (xfer == NULL) 1859 aprint_error("%s:%d: %s\n", device_xname(atac->atac_dev), 1860 chp->ch_channel, msg); 1861 else 1862 aprint_error("%s:%d:%d: %s\n", device_xname(atac->atac_dev), 1863 chp->ch_channel, xfer->c_drive, msg); 1864 } 1865 1866 /* 1867 * the bit bucket 1868 */ 1869 void 1870 wdcbit_bucket(struct ata_channel *chp, int size) 1871 { 1872 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 1873 1874 for (; size >= 2; size -= 2) 1875 (void)bus_space_read_2(wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0); 1876 if (size) 1877 (void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0); 1878 } 1879 1880 static void 1881 wdc_datain_pio(struct ata_channel *chp, int flags, void *bf, size_t len) 1882 { 1883 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 1884 1885 #ifndef __NO_STRICT_ALIGNMENT 1886 if ((uintptr_t)bf & 1) 1887 goto unaligned; 1888 if ((flags & ATA_DRIVE_CAP32) && ((uintptr_t)bf & 3)) 1889 goto unaligned; 1890 #endif 1891 1892 if (flags & ATA_DRIVE_NOSTREAM) { 1893 if ((flags & ATA_DRIVE_CAP32) && len > 3) { 1894 bus_space_read_multi_4(wdr->data32iot, 1895 wdr->data32ioh, 0, bf, len >> 2); 1896 bf = (char *)bf + (len & ~3); 1897 len &= 3; 1898 } 1899 if (len > 1) { 1900 bus_space_read_multi_2(wdr->cmd_iot, 1901 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1902 bf = (char *)bf + (len & ~1); 1903 len &= 1; 1904 } 1905 } else { 1906 if ((flags & ATA_DRIVE_CAP32) && len > 3) { 1907 bus_space_read_multi_stream_4(wdr->data32iot, 1908 wdr->data32ioh, 0, bf, len >> 2); 1909 bf = (char *)bf + (len & ~3); 1910 len &= 3; 1911 } 1912 if (len > 1) { 1913 bus_space_read_multi_stream_2(wdr->cmd_iot, 1914 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1915 bf = (char *)bf + (len & ~1); 1916 len &= 1; 1917 } 1918 } 1919 if (len) 1920 *((uint8_t *)bf) = bus_space_read_1(wdr->cmd_iot, 1921 wdr->cmd_iohs[wd_data], 0); 1922 return; 1923 1924 #ifndef __NO_STRICT_ALIGNMENT 1925 unaligned: 1926 if (flags & ATA_DRIVE_NOSTREAM) { 1927 if (flags & ATA_DRIVE_CAP32) { 1928 while (len > 3) { 1929 uint32_t val; 1930 1931 val = bus_space_read_4(wdr->data32iot, 1932 wdr->data32ioh, 0); 1933 memcpy(bf, &val, 4); 1934 bf = (char *)bf + 4; 1935 len -= 4; 1936 } 1937 } 1938 while (len > 1) { 1939 uint16_t val; 1940 1941 val = bus_space_read_2(wdr->cmd_iot, 1942 wdr->cmd_iohs[wd_data], 0); 1943 memcpy(bf, &val, 2); 1944 bf = (char *)bf + 2; 1945 len -= 2; 1946 } 1947 } else { 1948 if (flags & ATA_DRIVE_CAP32) { 1949 while (len > 3) { 1950 uint32_t val; 1951 1952 val = bus_space_read_stream_4(wdr->data32iot, 1953 wdr->data32ioh, 0); 1954 memcpy(bf, &val, 4); 1955 bf = (char *)bf + 4; 1956 len -= 4; 1957 } 1958 } 1959 while (len > 1) { 1960 uint16_t val; 1961 1962 val = bus_space_read_stream_2(wdr->cmd_iot, 1963 wdr->cmd_iohs[wd_data], 0); 1964 memcpy(bf, &val, 2); 1965 bf = (char *)bf + 2; 1966 len -= 2; 1967 } 1968 } 1969 #endif 1970 } 1971 1972 static void 1973 wdc_dataout_pio(struct ata_channel *chp, int flags, void *bf, size_t len) 1974 { 1975 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 1976 1977 #ifndef __NO_STRICT_ALIGNMENT 1978 if ((uintptr_t)bf & 1) 1979 goto unaligned; 1980 if ((flags & ATA_DRIVE_CAP32) && ((uintptr_t)bf & 3)) 1981 goto unaligned; 1982 #endif 1983 1984 if (flags & ATA_DRIVE_NOSTREAM) { 1985 if (flags & ATA_DRIVE_CAP32) { 1986 bus_space_write_multi_4(wdr->data32iot, 1987 wdr->data32ioh, 0, bf, len >> 2); 1988 bf = (char *)bf + (len & ~3); 1989 len &= 3; 1990 } 1991 if (len) { 1992 bus_space_write_multi_2(wdr->cmd_iot, 1993 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1994 } 1995 } else { 1996 if (flags & ATA_DRIVE_CAP32) { 1997 bus_space_write_multi_stream_4(wdr->data32iot, 1998 wdr->data32ioh, 0, bf, len >> 2); 1999 bf = (char *)bf + (len & ~3); 2000 len &= 3; 2001 } 2002 if (len) { 2003 bus_space_write_multi_stream_2(wdr->cmd_iot, 2004 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 2005 } 2006 } 2007 return; 2008 2009 #ifndef __NO_STRICT_ALIGNMENT 2010 unaligned: 2011 if (flags & ATA_DRIVE_NOSTREAM) { 2012 if (flags & ATA_DRIVE_CAP32) { 2013 while (len > 3) { 2014 uint32_t val; 2015 2016 memcpy(&val, bf, 4); 2017 bus_space_write_4(wdr->data32iot, 2018 wdr->data32ioh, 0, val); 2019 bf = (char *)bf + 4; 2020 len -= 4; 2021 } 2022 } 2023 while (len > 1) { 2024 uint16_t val; 2025 2026 memcpy(&val, bf, 2); 2027 bus_space_write_2(wdr->cmd_iot, 2028 wdr->cmd_iohs[wd_data], 0, val); 2029 bf = (char *)bf + 2; 2030 len -= 2; 2031 } 2032 } else { 2033 if (flags & ATA_DRIVE_CAP32) { 2034 while (len > 3) { 2035 uint32_t val; 2036 2037 memcpy(&val, bf, 4); 2038 bus_space_write_stream_4(wdr->data32iot, 2039 wdr->data32ioh, 0, val); 2040 bf = (char *)bf + 4; 2041 len -= 4; 2042 } 2043 } 2044 while (len > 1) { 2045 uint16_t val; 2046 2047 memcpy(&val, bf, 2); 2048 bus_space_write_stream_2(wdr->cmd_iot, 2049 wdr->cmd_iohs[wd_data], 0, val); 2050 bf = (char *)bf + 2; 2051 len -= 2; 2052 } 2053 } 2054 #endif 2055 } 2056