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