wdc.c revision 1.58 1 /* $NetBSD: wdc.c,v 1.58 1999/02/18 14:45:31 bouyer Exp $ */
2
3
4 /*
5 * Copyright (c) 1998 Manuel Bouyer. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Manuel Bouyer.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1998 The NetBSD Foundation, Inc.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * CODE UNTESTED IN THE CURRENT REVISION:
71 *
72 */
73
74 #define WDCDEBUG
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/conf.h>
80 #include <sys/buf.h>
81 #include <sys/device.h>
82 #include <sys/malloc.h>
83 #include <sys/syslog.h>
84 #include <sys/proc.h>
85
86 #include <vm/vm.h>
87
88 #include <machine/intr.h>
89 #include <machine/bus.h>
90
91 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
92 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
93 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
94 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
95 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
96 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
97
98 #include <dev/ata/atavar.h>
99 #include <dev/ata/atareg.h>
100 #include <dev/ic/wdcreg.h>
101 #include <dev/ic/wdcvar.h>
102
103 #include "atapibus.h"
104
105 #define WDCDELAY 100 /* 100 microseconds */
106 #define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY)
107 #if 0
108 /* If you enable this, it will report any delays more than WDCDELAY * N long. */
109 #define WDCNDELAY_DEBUG 50
110 #endif
111
112 LIST_HEAD(xfer_free_list, wdc_xfer) xfer_free_list;
113
114 static void __wdcerror __P((struct channel_softc*, char *));
115 static int __wdcwait_reset __P((struct channel_softc *, int));
116 void __wdccommand_done __P((struct channel_softc *, struct wdc_xfer *));
117 void __wdccommand_start __P((struct channel_softc *, struct wdc_xfer *));
118 int __wdccommand_intr __P((struct channel_softc *, struct wdc_xfer *));
119 int wdprint __P((void *, const char *));
120
121
122 #define DEBUG_INTR 0x01
123 #define DEBUG_XFERS 0x02
124 #define DEBUG_STATUS 0x04
125 #define DEBUG_FUNCS 0x08
126 #define DEBUG_PROBE 0x10
127 #ifdef WDCDEBUG
128 int wdcdebug_mask = 0;
129 int wdc_nxfer = 0;
130 #define WDCDEBUG_PRINT(args, level) if (wdcdebug_mask & (level)) printf args
131 #else
132 #define WDCDEBUG_PRINT(args, level)
133 #endif
134
135 int
136 wdprint(aux, pnp)
137 void *aux;
138 const char *pnp;
139 {
140 struct ata_atapi_attach *aa_link = aux;
141 if (pnp)
142 printf("drive at %s", pnp);
143 printf(" channel %d drive %d", aa_link->aa_channel,
144 aa_link->aa_drv_data->drive);
145 return (UNCONF);
146 }
147
148 int
149 atapi_print(aux, pnp)
150 void *aux;
151 const char *pnp;
152 {
153 struct ata_atapi_attach *aa_link = aux;
154 if (pnp)
155 printf("atapibus at %s", pnp);
156 printf(" channel %d", aa_link->aa_channel);
157 return (UNCONF);
158 }
159
160 /* Test to see controller with at last one attached drive is there.
161 * Returns a bit for each possible drive found (0x01 for drive 0,
162 * 0x02 for drive 1).
163 * Logic:
164 * - If a status register is at 0xff, assume there is no drive here
165 * (ISA has pull-up resistors). If no drive at all -> return.
166 * - reset the controller, wait for it to complete (may take up to 31s !).
167 * If timeout -> return.
168 * - test ATA/ATAPI signatures. If at last one drive found -> return.
169 * - try an ATA command on the master.
170 */
171
172 int
173 wdcprobe(chp)
174 struct channel_softc *chp;
175 {
176 u_int8_t st0, st1, sc, sn, cl, ch;
177 u_int8_t ret_value = 0x03;
178 u_int8_t drive;
179
180 /*
181 * Sanity check to see if the wdc channel responds at all.
182 */
183
184 if (chp->wdc == NULL ||
185 (chp->wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
186 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
187 WDSD_IBM);
188 delay(1);
189 st0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
190 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
191 WDSD_IBM | 0x10);
192 delay(1);
193 st1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
194
195 WDCDEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n",
196 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
197 chp->channel, st0, st1), DEBUG_PROBE);
198
199 if (st0 == 0xff)
200 ret_value &= ~0x01;
201 if (st1 == 0xff)
202 ret_value &= ~0x02;
203 if (ret_value == 0)
204 return 0;
205 }
206
207 /* assert SRST, wait for reset to complete */
208 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
209 WDSD_IBM);
210 delay(1);
211 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
212 WDCTL_RST | WDCTL_IDS);
213 DELAY(1000);
214 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
215 WDCTL_IDS);
216 delay(1000);
217 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
218 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
219 delay(1);
220
221 ret_value = __wdcwait_reset(chp, ret_value);
222 WDCDEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n",
223 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", chp->channel,
224 ret_value), DEBUG_PROBE);
225
226 /* if reset failed, there's nothing here */
227 if (ret_value == 0)
228 return 0;
229
230 /*
231 * Test presence of drives. First test register signatures looking for
232 * ATAPI devices , then rescan and try an ATA command, in case it's an
233 * old drive.
234 * Fill in drive_flags accordingly
235 */
236 for (drive = 0; drive < 2; drive++) {
237 if ((ret_value & (0x01 << drive)) == 0)
238 continue;
239 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
240 WDSD_IBM | (drive << 4));
241 delay(1);
242 /* Save registers contents */
243 sc = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt);
244 sn = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_sector);
245 cl = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo);
246 ch = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
247
248 WDCDEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x "
249 "cl=0x%x ch=0x%x\n",
250 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
251 chp->channel, drive, sc, sn, cl, ch), DEBUG_PROBE);
252 /*
253 * sc is supposted to be 0x1 for ATAPI but at last one drive
254 * set it to 0x0.
255 */
256 if ((sc == 0x00 || sc == 0x01) && sn == 0x01 &&
257 cl == 0x14 && ch == 0xeb) {
258 chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI;
259 }
260 }
261 for (drive = 0; drive < 2; drive++) {
262 if ((ret_value & (0x01 << drive)) == 0 ||
263 (chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) != 0)
264 continue;
265 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
266 WDSD_IBM | (drive << 4));
267 delay(1);
268 /*
269 * Maybe it's an old device, so don't rely on ATA sig.
270 * Test registers writability (Error register not writable,
271 * but cyllo is), then try an ATA command.
272 */
273 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_error, 0x58);
274 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo, 0xa5);
275 if (bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error) ==
276 0x58 ||
277 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) !=
278 0xa5) {
279 WDCDEBUG_PRINT(("%s:%d:%d: register writability "
280 "failed\n",
281 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
282 chp->channel, drive), DEBUG_PROBE);
283 ret_value &= ~(0x01 << drive);
284 continue;
285 }
286 if (wait_for_ready(chp, 10000) != 0) {
287 WDCDEBUG_PRINT(("%s:%d:%d: not ready\n",
288 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
289 chp->channel, drive), DEBUG_PROBE);
290 ret_value &= ~(0x01 << drive);
291 continue;
292 }
293 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command,
294 WDCC_DIAGNOSE);
295 if (wait_for_ready(chp, 10000) == 0) {
296 chp->ch_drive[drive].drive_flags |=
297 DRIVE_ATA;
298 } else {
299 WDCDEBUG_PRINT(("%s:%d:%d: WDCC_DIAGNOSE failed\n",
300 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe",
301 chp->channel, drive), DEBUG_PROBE);
302 ret_value &= ~(0x01 << drive);
303 }
304 }
305 return (ret_value);
306 }
307
308 void
309 wdcattach(chp)
310 struct channel_softc *chp;
311 {
312 int channel_flags, ctrl_flags, i, error;
313 struct ata_atapi_attach aa_link;
314
315 LIST_INIT(&xfer_free_list);
316 for (i = 0; i < 2; i++) {
317 chp->ch_drive[i].chnl_softc = chp;
318 chp->ch_drive[i].drive = i;
319 /* If controller can't do 16bit flag the drives as 32bit */
320 if ((chp->wdc->cap &
321 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
322 WDC_CAPABILITY_DATA32)
323 chp->ch_drive[i].drive_flags |= DRIVE_CAP32;
324 }
325
326 if ((error = wdc_addref(chp)) != 0) {
327 printf("%s: unable to enable controller\n",
328 chp->wdc->sc_dev.dv_xname);
329 return;
330 }
331
332 if (wdcprobe(chp) == 0) {
333 /* If no drives, abort attach here. */
334 wdc_delref(chp);
335 return;
336 }
337
338 TAILQ_INIT(&chp->ch_queue->sc_xfer);
339 ctrl_flags = chp->wdc->sc_dev.dv_cfdata->cf_flags;
340 channel_flags = (ctrl_flags >> (NBBY * chp->channel)) & 0xff;
341
342 WDCDEBUG_PRINT(("wdcattach: ch_drive_flags 0x%x 0x%x\n",
343 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
344 DEBUG_PROBE);
345
346 /*
347 * Attach an ATAPI bus, if needed.
348 */
349 if ((chp->ch_drive[0].drive_flags & DRIVE_ATAPI) ||
350 (chp->ch_drive[1].drive_flags & DRIVE_ATAPI)) {
351 #if NATAPIBUS > 0
352 wdc_atapibus_attach(chp);
353 #else
354 /*
355 * Fills in a fake aa_link and call config_found, so that
356 * the config machinery will print
357 * "atapibus at xxx not configured"
358 */
359 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
360 aa_link.aa_type = T_ATAPI;
361 aa_link.aa_channel = chp->channel;
362 aa_link.aa_openings = 1;
363 aa_link.aa_drv_data = 0;
364 aa_link.aa_bus_private = NULL;
365 (void)config_found(&chp->wdc->sc_dev, (void *)&aa_link,
366 atapi_print);
367 #endif
368 }
369
370 for (i = 0; i < 2; i++) {
371 if ((chp->ch_drive[i].drive_flags & DRIVE_ATA) == 0) {
372 continue;
373 }
374 memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
375 aa_link.aa_type = T_ATA;
376 aa_link.aa_channel = chp->channel;
377 aa_link.aa_openings = 1;
378 aa_link.aa_drv_data = &chp->ch_drive[i];
379 if (config_found(&chp->wdc->sc_dev, (void *)&aa_link, wdprint))
380 wdc_probe_caps(&chp->ch_drive[i]);
381 }
382
383 /*
384 * reset drive_flags for unnatached devices, reset state for attached
385 * ones
386 */
387 for (i = 0; i < 2; i++) {
388 if (chp->ch_drive[i].drv_softc == NULL)
389 chp->ch_drive[i].drive_flags = 0;
390 else
391 chp->ch_drive[i].state = 0;
392 }
393
394 /*
395 * Reset channel. The probe, with some combinations of ATA/ATAPI
396 * devices keep it in a mostly working, but strange state (with busy
397 * led on)
398 */
399 if ((chp->wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
400 wdcreset(chp, VERBOSE);
401 /*
402 * Read status registers to avoid spurious interrupts.
403 */
404 for (i = 1; i >= 0; i--) {
405 if (chp->ch_drive[i].drive_flags & DRIVE) {
406 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh,
407 wd_sdh, WDSD_IBM | (i << 4));
408 if (wait_for_unbusy(chp, 10000) < 0)
409 printf("%s:%d:%d: device busy\n",
410 chp->wdc->sc_dev.dv_xname,
411 chp->channel, i);
412 }
413 }
414 }
415 wdc_delref(chp);
416 }
417
418 /*
419 * Start I/O on a controller, for the given channel.
420 * The first xfer may be not for our channel if the channel queues
421 * are shared.
422 */
423 void
424 wdcstart(chp)
425 struct channel_softc *chp;
426 {
427 struct wdc_xfer *xfer;
428
429 #ifdef WDC_DIAGNOSTIC
430 int spl1, spl2;
431
432 spl1 = splbio();
433 spl2 = splbio();
434 if (spl2 != spl1) {
435 printf("wdcstart: not at splbio()\n");
436 panic("wdcstart");
437 }
438 splx(spl2);
439 splx(spl1);
440 #endif /* WDC_DIAGNOSTIC */
441
442 /* is there a xfer ? */
443 if ((xfer = chp->ch_queue->sc_xfer.tqh_first) == NULL)
444 return;
445
446 /* adjust chp, in case we have a shared queue */
447 chp = xfer->chp;
448
449 if ((chp->ch_flags & WDCF_ACTIVE) != 0 ) {
450 return; /* channel aleady active */
451 }
452 #ifdef DIAGNOSTIC
453 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0)
454 panic("wdcstart: channel waiting for irq\n");
455 #endif
456 if (chp->wdc->cap & WDC_CAPABILITY_HWLOCK)
457 if (!(*chp->wdc->claim_hw)(chp, 0))
458 return;
459
460 WDCDEBUG_PRINT(("wdcstart: xfer %p channel %d drive %d\n", xfer,
461 chp->channel, xfer->drive), DEBUG_XFERS);
462 chp->ch_flags |= WDCF_ACTIVE;
463 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_RESET) {
464 chp->ch_drive[xfer->drive].drive_flags &= ~DRIVE_RESET;
465 chp->ch_drive[xfer->drive].state = 0;
466 }
467 xfer->c_start(chp, xfer);
468 }
469
470 /* restart an interrupted I/O */
471 void
472 wdcrestart(v)
473 void *v;
474 {
475 struct channel_softc *chp = v;
476 int s;
477
478 s = splbio();
479 wdcstart(chp);
480 splx(s);
481 }
482
483
484 /*
485 * Interrupt routine for the controller. Acknowledge the interrupt, check for
486 * errors on the current operation, mark it done if necessary, and start the
487 * next request. Also check for a partially done transfer, and continue with
488 * the next chunk if so.
489 */
490 int
491 wdcintr(arg)
492 void *arg;
493 {
494 struct channel_softc *chp = arg;
495 struct wdc_xfer *xfer;
496
497 if ((chp->ch_flags & WDCF_IRQ_WAIT) == 0) {
498 #if 0
499 /* Clear the pending interrupt and abort. */
500 u_int8_t s =
501 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
502 #ifdef WDCDEBUG
503 u_int8_t e =
504 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
505 u_int8_t i =
506 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt);
507 #else
508 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
509 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt);
510 #endif
511
512 WDCDEBUG_PRINT(("wdcintr: inactive controller, "
513 "punting st=%02x er=%02x irr=%02x\n", s, e, i), DEBUG_INTR);
514
515 if (s & WDCS_DRQ) {
516 int len;
517 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
518 wd_cyl_lo) + 256 * bus_space_read_1(chp->cmd_iot,
519 chp->cmd_ioh, wd_cyl_hi);
520 WDCDEBUG_PRINT(("wdcintr: clearing up %d bytes\n",
521 len), DEBUG_INTR);
522 wdcbit_bucket (chp, len);
523 }
524 #else
525 WDCDEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR);
526 #endif
527 return 0;
528 }
529
530 WDCDEBUG_PRINT(("wdcintr\n"), DEBUG_INTR);
531 untimeout(wdctimeout, chp);
532 chp->ch_flags &= ~WDCF_IRQ_WAIT;
533 xfer = chp->ch_queue->sc_xfer.tqh_first;
534 return xfer->c_intr(chp, xfer);
535 }
536
537 /* Put all disk in RESET state */
538 void wdc_reset_channel(drvp)
539 struct ata_drive_datas *drvp;
540 {
541 struct channel_softc *chp = drvp->chnl_softc;
542 int drive;
543 WDCDEBUG_PRINT(("ata_reset_channel %s:%d for drive %d\n",
544 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive),
545 DEBUG_FUNCS);
546 (void) wdcreset(chp, VERBOSE);
547 for (drive = 0; drive < 2; drive++) {
548 chp->ch_drive[drive].state = 0;
549 }
550 }
551
552 int
553 wdcreset(chp, verb)
554 struct channel_softc *chp;
555 int verb;
556 {
557 int drv_mask1, drv_mask2;
558
559 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
560 WDSD_IBM); /* master */
561 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
562 WDCTL_RST | WDCTL_IDS);
563 delay(1000);
564 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
565 WDCTL_IDS);
566 delay(1000);
567 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error);
568 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
569 WDCTL_4BIT);
570
571 drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00;
572 drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00;
573 drv_mask2 = __wdcwait_reset(chp, drv_mask1);
574 if (verb && drv_mask2 != drv_mask1) {
575 printf("%s channel %d: reset failed for",
576 chp->wdc->sc_dev.dv_xname, chp->channel);
577 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0)
578 printf(" drive 0");
579 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0)
580 printf(" drive 1");
581 printf("\n");
582 }
583 return (drv_mask1 != drv_mask2) ? 1 : 0;
584 }
585
586 static int
587 __wdcwait_reset(chp, drv_mask)
588 struct channel_softc *chp;
589 int drv_mask;
590 {
591 int timeout;
592 u_int8_t st0, st1;
593 /* wait for BSY to deassert */
594 for (timeout = 0; timeout < WDCNDELAY_RST;timeout++) {
595 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
596 WDSD_IBM); /* master */
597 delay(1);
598 st0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
599 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
600 WDSD_IBM | 0x10); /* slave */
601 delay(1);
602 st1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
603
604 if ((drv_mask & 0x01) == 0) {
605 /* no master */
606 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
607 /* No master, slave is ready, it's done */
608 return drv_mask;
609 }
610 } else if ((drv_mask & 0x02) == 0) {
611 /* no slave */
612 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
613 /* No slave, master is ready, it's done */
614 return drv_mask;
615 }
616 } else {
617 /* Wait for both master and slave to be ready */
618 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
619 return drv_mask;
620 }
621 }
622 delay(WDCDELAY);
623 }
624 /* Reset timed out. Maybe it's because drv_mask was not rigth */
625 if (st0 & WDCS_BSY)
626 drv_mask &= ~0x01;
627 if (st1 & WDCS_BSY)
628 drv_mask &= ~0x02;
629 return drv_mask;
630 }
631
632 /*
633 * Wait for a drive to be !BSY, and have mask in its status register.
634 * return -1 for a timeout after "timeout" ms.
635 */
636 int
637 wdcwait(chp, mask, bits, timeout)
638 struct channel_softc *chp;
639 int mask, bits, timeout;
640 {
641 u_char status;
642 int time = 0;
643 #ifdef WDCNDELAY_DEBUG
644 extern int cold;
645 #endif
646 WDCDEBUG_PRINT(("wdcwait %s:%d\n", chp->wdc->sc_dev.dv_xname,
647 chp->channel), DEBUG_STATUS);
648 chp->ch_error = 0;
649
650 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */
651
652 for (;;) {
653 chp->ch_status = status =
654 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
655 if ((status & WDCS_BSY) == 0 && (status & mask) == bits)
656 break;
657 if (++time > timeout) {
658 WDCDEBUG_PRINT(("wdcwait: timeout, status %x "
659 "error %x\n", status,
660 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
661 wd_error)),
662 DEBUG_STATUS);
663 return -1;
664 }
665 delay(WDCDELAY);
666 }
667 if (status & WDCS_ERR)
668 chp->ch_error = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
669 wd_error);
670 #ifdef WDCNDELAY_DEBUG
671 /* After autoconfig, there should be no long delays. */
672 if (!cold && time > WDCNDELAY_DEBUG) {
673 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first;
674 if (xfer == NULL)
675 printf("%s channel %d: warning: busy-wait took %dus\n",
676 chp->wdc->sc_dev.dv_xname, chp->channel,
677 WDCDELAY * time);
678 else
679 printf("%s:%d:%d: warning: busy-wait took %dus\n",
680 chp->wdc->sc_dev.dv_xname, chp->channel,
681 xfer->drive,
682 WDCDELAY * time);
683 }
684 #endif
685 return 0;
686 }
687
688 void
689 wdctimeout(arg)
690 void *arg;
691 {
692 struct channel_softc *chp = (struct channel_softc *)arg;
693 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first;
694 int s;
695
696 WDCDEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS);
697
698 s = splbio();
699 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0) {
700 __wdcerror(chp, "lost interrupt");
701 printf("\ttype: %s\n", (xfer->c_flags & C_ATAPI) ?
702 "atapi":"ata");
703 printf("\tc_bcount: %d\n", xfer->c_bcount);
704 printf("\tc_skip: %d\n", xfer->c_skip);
705 /*
706 * Call the interrupt routine. If we just missed and interrupt,
707 * it will do what's needed. Else, it will take the needed
708 * action (reset the device).
709 */
710 xfer->c_flags |= C_TIMEOU;
711 chp->ch_flags &= ~WDCF_IRQ_WAIT;
712 xfer->c_intr(chp, xfer);
713 } else
714 __wdcerror(chp, "missing untimeout");
715 splx(s);
716 }
717
718 /*
719 * Probe drive's capabilites, for use by the controller later
720 * Assumes drvp points to an existing drive.
721 * XXX this should be a controller-indep function
722 */
723 void
724 wdc_probe_caps(drvp)
725 struct ata_drive_datas *drvp;
726 {
727 struct ataparams params, params2;
728 struct channel_softc *chp = drvp->chnl_softc;
729 struct device *drv_dev = drvp->drv_softc;
730 struct wdc_softc *wdc = chp->wdc;
731 int i, printed;
732 char *sep = "";
733 int cf_flags;
734
735 if (ata_get_params(drvp, AT_POLL, ¶ms) != CMD_OK) {
736 /* IDENTIFY failed. Can't tell more about the device */
737 return;
738 }
739 if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
740 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) {
741 /*
742 * Controller claims 16 and 32 bit transfers.
743 * Re-do an IDENTIFY with 32-bit transfers,
744 * and compare results.
745 */
746 drvp->drive_flags |= DRIVE_CAP32;
747 ata_get_params(drvp, AT_POLL, ¶ms2);
748 if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) {
749 /* Not good. fall back to 16bits */
750 drvp->drive_flags &= ~DRIVE_CAP32;
751 } else {
752 printf("%s: 32-bits data port", drv_dev->dv_xname);
753 }
754 }
755 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
756 if (params.atap_ata_major > 0x01 &&
757 params.atap_ata_major != 0xffff) {
758 for (i = 14; i > 0; i--) {
759 if (params.atap_ata_major & (1 << i)) {
760 if ((drvp->drive_flags & DRIVE_CAP32) == 0)
761 printf("%s: ", drv_dev->dv_xname);
762 else
763 printf(", ");
764 printf("ATA version %d\n", i);
765 drvp->ata_vers = i;
766 break;
767 }
768 }
769 } else
770 #endif
771 if (drvp->drive_flags & DRIVE_CAP32)
772 printf("\n");
773
774 /* An ATAPI device is at last PIO mode 3 */
775 if (drvp->drive_flags & DRIVE_ATAPI)
776 drvp->PIO_mode = 3;
777
778 /*
779 * It's not in the specs, but it seems that some drive
780 * returns 0xffff in atap_extensions when this field is invalid
781 */
782 if (params.atap_extensions != 0xffff &&
783 (params.atap_extensions & WDC_EXT_MODES)) {
784 printed = 0;
785 /*
786 * XXX some drives report something wrong here (they claim to
787 * support PIO mode 8 !). As mode is coded on 3 bits in
788 * SET FEATURE, limit it to 7 (so limit i to 4).
789 * If higther mode than 7 is found, abort.
790 */
791 for (i = 7; i >= 0; i--) {
792 if ((params.atap_piomode_supp & (1 << i)) == 0)
793 continue;
794 if (i > 4)
795 return;
796 /*
797 * See if mode is accepted.
798 * If the controller can't set its PIO mode,
799 * assume the defaults are good, so don't try
800 * to set it
801 */
802 if ((wdc->cap & WDC_CAPABILITY_MODE) != 0)
803 if (ata_set_mode(drvp, 0x08 | (i + 3),
804 AT_POLL) != CMD_OK)
805 continue;
806 if (!printed) {
807 printf("%s: drive supports PIO mode %d",
808 drv_dev->dv_xname, i + 3);
809 sep = ",";
810 printed = 1;
811 }
812 /*
813 * If controller's driver can't set its PIO mode,
814 * get the highter one for the drive.
815 */
816 if ((wdc->cap & WDC_CAPABILITY_MODE) == 0 ||
817 wdc->PIO_cap >= i + 3) {
818 drvp->PIO_mode = i + 3;
819 drvp->PIO_cap = i + 3;
820 break;
821 }
822 }
823 if (!printed) {
824 /*
825 * We didn't find a valid PIO mode.
826 * Assume the values returned for DMA are buggy too
827 */
828 return;
829 }
830 drvp->drive_flags |= DRIVE_MODE;
831 printed = 0;
832 for (i = 7; i >= 0; i--) {
833 if ((params.atap_dmamode_supp & (1 << i)) == 0)
834 continue;
835 if ((wdc->cap & WDC_CAPABILITY_DMA) &&
836 (wdc->cap & WDC_CAPABILITY_MODE))
837 if (ata_set_mode(drvp, 0x20 | i, AT_POLL)
838 != CMD_OK)
839 continue;
840 if (!printed) {
841 printf("%s DMA mode %d", sep, i);
842 sep = ",";
843 printed = 1;
844 }
845 if (wdc->cap & WDC_CAPABILITY_DMA) {
846 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
847 wdc->DMA_cap < i)
848 continue;
849 drvp->DMA_mode = i;
850 drvp->DMA_cap = i;
851 drvp->drive_flags |= DRIVE_DMA;
852 }
853 break;
854 }
855 if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
856 for (i = 7; i >= 0; i--) {
857 if ((params.atap_udmamode_supp & (1 << i))
858 == 0)
859 continue;
860 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
861 (wdc->cap & WDC_CAPABILITY_UDMA))
862 if (ata_set_mode(drvp, 0x40 | i,
863 AT_POLL) != CMD_OK)
864 continue;
865 printf("%s Ultra-DMA mode %d", sep, i);
866 sep = ",";
867 if (wdc->cap & WDC_CAPABILITY_UDMA) {
868 if ((wdc->cap & WDC_CAPABILITY_MODE) &&
869 wdc->UDMA_cap < i)
870 continue;
871 drvp->UDMA_mode = i;
872 drvp->UDMA_cap = i;
873 drvp->drive_flags |= DRIVE_UDMA;
874 }
875 break;
876 }
877 }
878 printf("\n");
879 }
880
881 /* Try to guess ATA version here, if it didn't get reported */
882 if (drvp->ata_vers == 0) {
883 if (drvp->drive_flags & DRIVE_UDMA)
884 drvp->ata_vers = 4; /* should be at last ATA-4 */
885 else if (drvp->PIO_cap > 2)
886 drvp->ata_vers = 2; /* should be at last ATA-2 */
887 }
888 cf_flags = drv_dev->dv_cfdata->cf_flags;
889 if (cf_flags & ATA_CONFIG_PIO_SET) {
890 drvp->PIO_mode =
891 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
892 drvp->drive_flags |= DRIVE_MODE;
893 }
894 if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) {
895 /* don't care about DMA modes */
896 return;
897 }
898 if (cf_flags & ATA_CONFIG_DMA_SET) {
899 if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
900 ATA_CONFIG_DMA_DISABLE) {
901 drvp->drive_flags &= ~DRIVE_DMA;
902 } else {
903 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
904 ATA_CONFIG_DMA_OFF;
905 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
906 }
907 }
908 if (cf_flags & ATA_CONFIG_UDMA_SET) {
909 if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
910 ATA_CONFIG_UDMA_DISABLE) {
911 drvp->drive_flags &= ~DRIVE_UDMA;
912 } else {
913 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
914 ATA_CONFIG_UDMA_OFF;
915 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
916 }
917 }
918 }
919
920 /*
921 * downgrade the transfer mode of a drive after an error. return 1 if
922 * downgrade was possible, 0 otherwise.
923 */
924 int
925 wdc_downgrade_mode(drvp)
926 struct ata_drive_datas *drvp;
927 {
928 struct channel_softc *chp = drvp->chnl_softc;
929 struct device *drv_dev = drvp->drv_softc;
930 struct wdc_softc *wdc = chp->wdc;
931 int cf_flags = drv_dev->dv_cfdata->cf_flags;
932
933 /* if drive or controller don't know its mode, we can't do much */
934 if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
935 (wdc->cap & WDC_CAPABILITY_MODE) == 0)
936 return 0;
937 /* current drive mode was set by a config flag, let it this way */
938 if ((cf_flags & ATA_CONFIG_PIO_SET) ||
939 (cf_flags & ATA_CONFIG_DMA_SET) ||
940 (cf_flags & ATA_CONFIG_UDMA_SET))
941 return 0;
942
943 if (drvp->drive_flags & DRIVE_UDMA) {
944 drvp->drive_flags &= ~DRIVE_UDMA;
945 drvp->drive_flags |= DRIVE_DMA;
946 drvp->DMA_mode = drvp->DMA_cap;
947 printf("%s: transfer error, downgrading to DMA mode %d\n",
948 drv_dev->dv_xname, drvp->DMA_mode);
949 } else if (drvp->drive_flags & DRIVE_DMA) {
950 drvp->drive_flags &= ~DRIVE_DMA;
951 drvp->PIO_mode = drvp->PIO_cap;
952 printf("%s: transfer error, downgrading to PIO mode %d\n",
953 drv_dev->dv_xname, drvp->PIO_mode);
954 } else /* already using PIO, can't downgrade */
955 return 0;
956
957 wdc->set_modes(chp);
958 /* reset the channel, which will shedule all drives for setup */
959 wdc_reset_channel(drvp);
960 return 1;
961 }
962
963 int
964 wdc_exec_command(drvp, wdc_c)
965 struct ata_drive_datas *drvp;
966 struct wdc_command *wdc_c;
967 {
968 struct channel_softc *chp = drvp->chnl_softc;
969 struct wdc_xfer *xfer;
970 int s, ret;
971
972 WDCDEBUG_PRINT(("wdc_exec_command %s:%d:%d\n",
973 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive),
974 DEBUG_FUNCS);
975
976 /* set up an xfer and queue. Wait for completion */
977 xfer = wdc_get_xfer(wdc_c->flags & AT_WAIT ? WDC_CANSLEEP :
978 WDC_NOSLEEP);
979 if (xfer == NULL) {
980 return WDC_TRY_AGAIN;
981 }
982
983 if (wdc_c->flags & AT_POLL)
984 xfer->c_flags |= C_POLL;
985 xfer->drive = drvp->drive;
986 xfer->databuf = wdc_c->data;
987 xfer->c_bcount = wdc_c->bcount;
988 xfer->cmd = wdc_c;
989 xfer->c_start = __wdccommand_start;
990 xfer->c_intr = __wdccommand_intr;
991
992 s = splbio();
993 wdc_exec_xfer(chp, xfer);
994 #ifdef DIAGNOSTIC
995 if ((wdc_c->flags & AT_POLL) != 0 &&
996 (wdc_c->flags & AT_DONE) == 0)
997 panic("wdc_exec_command: polled command not done\n");
998 #endif
999 if (wdc_c->flags & AT_DONE) {
1000 ret = WDC_COMPLETE;
1001 } else {
1002 if (wdc_c->flags & AT_WAIT) {
1003 tsleep(wdc_c, PRIBIO, "wdccmd", 0);
1004 ret = WDC_COMPLETE;
1005 } else {
1006 ret = WDC_QUEUED;
1007 }
1008 }
1009 splx(s);
1010 return ret;
1011 }
1012
1013 void
1014 __wdccommand_start(chp, xfer)
1015 struct channel_softc *chp;
1016 struct wdc_xfer *xfer;
1017 {
1018 int drive = xfer->drive;
1019 struct wdc_command *wdc_c = xfer->cmd;
1020
1021 WDCDEBUG_PRINT(("__wdccommand_start %s:%d:%d\n",
1022 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
1023 DEBUG_FUNCS);
1024
1025 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
1026 WDSD_IBM | (drive << 4));
1027 if (wdcwait(chp, wdc_c->r_st_bmask, wdc_c->r_st_bmask,
1028 wdc_c->timeout) != 0) {
1029 wdc_c->flags |= AT_TIMEOU;
1030 __wdccommand_done(chp, xfer);
1031 return;
1032 }
1033 wdccommand(chp, drive, wdc_c->r_command, wdc_c->r_cyl, wdc_c->r_head,
1034 wdc_c->r_sector, wdc_c->r_count, wdc_c->r_precomp);
1035 if ((wdc_c->flags & AT_POLL) == 0) {
1036 chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */
1037 timeout(wdctimeout, chp, wdc_c->timeout / 1000 * hz);
1038 return;
1039 }
1040 /*
1041 * Polled command. Wait for drive ready or drq. Done in intr().
1042 * Wait for at last 400ns for status bit to be valid.
1043 */
1044 delay(10);
1045 if (__wdccommand_intr(chp, xfer) == 0) {
1046 wdc_c->flags |= AT_TIMEOU;
1047 __wdccommand_done(chp, xfer);
1048 }
1049 }
1050
1051 int
1052 __wdccommand_intr(chp, xfer)
1053 struct channel_softc *chp;
1054 struct wdc_xfer *xfer;
1055 {
1056 struct wdc_command *wdc_c = xfer->cmd;
1057 int bcount = wdc_c->bcount;
1058 char *data = wdc_c->data;
1059
1060 WDCDEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n",
1061 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), DEBUG_INTR);
1062 if (wdcwait(chp, wdc_c->r_st_pmask, wdc_c->r_st_pmask,
1063 wdc_c->timeout)) {
1064 wdc_c->flags |= AT_ERROR;
1065 __wdccommand_done(chp, xfer);
1066 return 1;
1067 }
1068 if (wdc_c->flags & AT_READ) {
1069 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_CAP32) {
1070 bus_space_read_multi_4(chp->data32iot, chp->data32ioh,
1071 0, (u_int32_t*)data, bcount >> 2);
1072 data += bcount & 0xfffffffc;
1073 bcount = bcount & 0x03;
1074 }
1075 if (bcount > 0)
1076 bus_space_read_multi_2(chp->cmd_iot, chp->cmd_ioh,
1077 wd_data, (u_int16_t *)data, bcount >> 1);
1078 } else if (wdc_c->flags & AT_WRITE) {
1079 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_CAP32) {
1080 bus_space_write_multi_4(chp->data32iot, chp->data32ioh,
1081 0, (u_int32_t*)data, bcount >> 2);
1082 data += bcount & 0xfffffffc;
1083 bcount = bcount & 0x03;
1084 }
1085 if (bcount > 0)
1086 bus_space_write_multi_2(chp->cmd_iot, chp->cmd_ioh,
1087 wd_data, (u_int16_t *)data, bcount >> 1);
1088 }
1089 __wdccommand_done(chp, xfer);
1090 return 1;
1091 }
1092
1093 void
1094 __wdccommand_done(chp, xfer)
1095 struct channel_softc *chp;
1096 struct wdc_xfer *xfer;
1097 {
1098 int needdone = xfer->c_flags & C_NEEDDONE;
1099 struct wdc_command *wdc_c = xfer->cmd;
1100
1101 WDCDEBUG_PRINT(("__wdccommand_done %s:%d:%d\n",
1102 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), DEBUG_FUNCS);
1103 if (chp->ch_status & WDCS_DWF)
1104 wdc_c->flags |= AT_DF;
1105 if (chp->ch_status & WDCS_ERR) {
1106 wdc_c->flags |= AT_ERROR;
1107 wdc_c->r_error = chp->ch_error;
1108 }
1109 wdc_c->flags |= AT_DONE;
1110 if (wdc_c->flags & AT_READREG && (wdc_c->flags & (AT_ERROR | AT_DF))
1111 == 0) {
1112 wdc_c->r_head = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1113 wd_sdh);
1114 wdc_c->r_cyl = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1115 wd_cyl_hi) << 8;
1116 wdc_c->r_cyl |= bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1117 wd_cyl_lo);
1118 wdc_c->r_sector = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1119 wd_sector);
1120 wdc_c->r_count = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1121 wd_seccnt);
1122 wdc_c->r_error = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1123 wd_error);
1124 wdc_c->r_precomp = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh,
1125 wd_precomp);
1126 }
1127 wdc_free_xfer(chp, xfer);
1128 if (needdone) {
1129 if (wdc_c->flags & AT_WAIT)
1130 wakeup(wdc_c);
1131 else
1132 wdc_c->callback(wdc_c->callback_arg);
1133 }
1134 wdcstart(chp);
1135 return;
1136 }
1137
1138 /*
1139 * Send a command. The drive should be ready.
1140 * Assumes interrupts are blocked.
1141 */
1142 void
1143 wdccommand(chp, drive, command, cylin, head, sector, count, precomp)
1144 struct channel_softc *chp;
1145 u_int8_t drive;
1146 u_int8_t command;
1147 u_int16_t cylin;
1148 u_int8_t head, sector, count, precomp;
1149 {
1150 WDCDEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d "
1151 "sector=%d count=%d precomp=%d\n", chp->wdc->sc_dev.dv_xname,
1152 chp->channel, drive, command, cylin, head, sector, count, precomp),
1153 DEBUG_FUNCS);
1154
1155 /* Select drive, head, and addressing mode. */
1156 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
1157 WDSD_IBM | (drive << 4) | head);
1158 /* Load parameters. wd_features(ATA/ATAPI) = wd_precomp(ST506) */
1159 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_precomp,
1160 precomp);
1161 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo, cylin);
1162 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi, cylin >> 8);
1163 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sector, sector);
1164 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt, count);
1165
1166 /* Send command. */
1167 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command, command);
1168 return;
1169 }
1170
1171 /*
1172 * Simplified version of wdccommand(). Unbusy/ready/drq must be
1173 * tested by the caller.
1174 */
1175 void
1176 wdccommandshort(chp, drive, command)
1177 struct channel_softc *chp;
1178 int drive;
1179 int command;
1180 {
1181
1182 WDCDEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n",
1183 chp->wdc->sc_dev.dv_xname, chp->channel, drive, command),
1184 DEBUG_FUNCS);
1185
1186 /* Select drive. */
1187 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
1188 WDSD_IBM | (drive << 4));
1189
1190 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command, command);
1191 }
1192
1193 /* Add a command to the queue and start controller. Must be called at splbio */
1194
1195 void
1196 wdc_exec_xfer(chp, xfer)
1197 struct channel_softc *chp;
1198 struct wdc_xfer *xfer;
1199 {
1200 WDCDEBUG_PRINT(("wdc_exec_xfer %p channel %d drive %d\n", xfer,
1201 chp->channel, xfer->drive), DEBUG_XFERS);
1202
1203 /* complete xfer setup */
1204 xfer->chp = chp;
1205
1206 /*
1207 * If we are a polled command, and the list is not empty,
1208 * we are doing a dump. Drop the list to allow the polled command
1209 * to complete, we're going to reboot soon anyway.
1210 */
1211 if ((xfer->c_flags & C_POLL) != 0 &&
1212 chp->ch_queue->sc_xfer.tqh_first != NULL) {
1213 TAILQ_INIT(&chp->ch_queue->sc_xfer);
1214 }
1215 /* insert at the end of command list */
1216 TAILQ_INSERT_TAIL(&chp->ch_queue->sc_xfer,xfer , c_xferchain);
1217 WDCDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
1218 chp->ch_flags), DEBUG_XFERS);
1219 wdcstart(chp);
1220 xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
1221 }
1222
1223 struct wdc_xfer *
1224 wdc_get_xfer(flags)
1225 int flags;
1226 {
1227 struct wdc_xfer *xfer;
1228 int s;
1229
1230 s = splbio();
1231 if ((xfer = xfer_free_list.lh_first) != NULL) {
1232 LIST_REMOVE(xfer, free_list);
1233 splx(s);
1234 #ifdef DIAGNOSTIC
1235 if ((xfer->c_flags & C_INUSE) != 0)
1236 panic("wdc_get_xfer: xfer already in use\n");
1237 #endif
1238 } else {
1239 splx(s);
1240 WDCDEBUG_PRINT(("wdc:making xfer %d\n",wdc_nxfer), DEBUG_XFERS);
1241 xfer = malloc(sizeof(*xfer), M_DEVBUF,
1242 ((flags & WDC_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
1243 if (xfer == NULL)
1244 return 0;
1245 #ifdef DIAGNOSTIC
1246 xfer->c_flags &= ~C_INUSE;
1247 #endif
1248 #ifdef WDCDEBUG
1249 wdc_nxfer++;
1250 #endif
1251 }
1252 #ifdef DIAGNOSTIC
1253 if ((xfer->c_flags & C_INUSE) != 0)
1254 panic("wdc_get_xfer: xfer already in use\n");
1255 #endif
1256 memset(xfer, 0, sizeof(struct wdc_xfer));
1257 xfer->c_flags = C_INUSE;
1258 return xfer;
1259 }
1260
1261 void
1262 wdc_free_xfer(chp, xfer)
1263 struct channel_softc *chp;
1264 struct wdc_xfer *xfer;
1265 {
1266 struct wdc_softc *wdc = chp->wdc;
1267 int s;
1268
1269 if (wdc->cap & WDC_CAPABILITY_HWLOCK)
1270 (*wdc->free_hw)(chp);
1271 s = splbio();
1272 chp->ch_flags &= ~WDCF_ACTIVE;
1273 TAILQ_REMOVE(&chp->ch_queue->sc_xfer, xfer, c_xferchain);
1274 xfer->c_flags &= ~C_INUSE;
1275 LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
1276 splx(s);
1277 }
1278
1279 static void
1280 __wdcerror(chp, msg)
1281 struct channel_softc *chp;
1282 char *msg;
1283 {
1284 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first;
1285 if (xfer == NULL)
1286 printf("%s:%d: %s\n", chp->wdc->sc_dev.dv_xname, chp->channel,
1287 msg);
1288 else
1289 printf("%s:%d:%d: %s\n", chp->wdc->sc_dev.dv_xname,
1290 chp->channel, xfer->drive, msg);
1291 }
1292
1293 /*
1294 * the bit bucket
1295 */
1296 void
1297 wdcbit_bucket(chp, size)
1298 struct channel_softc *chp;
1299 int size;
1300 {
1301
1302 for (; size >= 2; size -= 2)
1303 (void)bus_space_read_2(chp->cmd_iot, chp->cmd_ioh, wd_data);
1304 if (size)
1305 (void)bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_data);
1306 }
1307
1308 int
1309 wdc_addref(chp)
1310 struct channel_softc *chp;
1311 {
1312 struct wdc_softc *wdc = chp->wdc;
1313 struct scsipi_adapter *adapter = &wdc->sc_atapi_adapter;
1314 int s, error = 0;
1315
1316 s = splbio();
1317 if (adapter->scsipi_refcnt++ == 0 &&
1318 adapter->scsipi_enable != NULL) {
1319 error = (*adapter->scsipi_enable)(wdc, 1);
1320 if (error)
1321 adapter->scsipi_refcnt--;
1322 }
1323 splx(s);
1324 return (error);
1325 }
1326
1327 void
1328 wdc_delref(chp)
1329 struct channel_softc *chp;
1330 {
1331 struct wdc_softc *wdc = chp->wdc;
1332 struct scsipi_adapter *adapter = &wdc->sc_atapi_adapter;
1333 int s;
1334
1335 s = splbio();
1336 if (adapter->scsipi_refcnt-- == 1 &&
1337 adapter->scsipi_enable != NULL)
1338 (void) (*adapter->scsipi_enable)(wdc, 0);
1339 splx(s);
1340 }
1341