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