pciide.c revision 1.6.2.12 1 /* $NetBSD: pciide.c,v 1.6.2.12 1998/08/13 14:36:13 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
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 * PCI IDE controller driver.
35 *
36 * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
37 * sys/dev/pci/ppb.c, revision 1.16).
38 *
39 * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
40 * "Programming Interface for Bus Master IDE Controller, Revision 1.0
41 * 5/16/94" from the PCI SIG.
42 *
43 */
44
45 #define WDCDEBUG
46
47 #define DEBUG_DMA 0x01
48 #define DEBUG_XFERS 0x02
49 #define DEBUG_FUNCS 0x08
50 #define DEBUG_PROBE 0x10
51 #ifdef WDCDEBUG
52 int wdcdebug_pciide_mask = DEBUG_PROBE;
53 #define WDCDEBUG_PRINT(args, level) \
54 if (wdcdebug_pciide_mask & (level)) printf args
55 #else
56 #define WDCDEBUG_PRINT(args, level)
57 #endif
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcidevs.h>
70 #include <dev/pci/pciidereg.h>
71 #include <dev/pci/pciidevar.h>
72 #include <dev/pci/pciide_piix_reg.h>
73 #include <dev/pci/pciide_apollo_reg.h>
74 #include <dev/pci/pciide_cmd_reg.h>
75 #include <dev/ata/atavar.h>
76 #include <dev/ic/wdcreg.h>
77 #include <dev/ic/wdcvar.h>
78
79 struct pciide_softc {
80 struct wdc_softc sc_wdcdev; /* common wdc definitions */
81
82 void *sc_pci_ih; /* PCI interrupt handle */
83 int sc_dma_ok; /* bus-master DMA info */
84 bus_space_tag_t sc_dma_iot;
85 bus_space_handle_t sc_dma_ioh;
86 bus_dma_tag_t sc_dmat;
87 /* Chip description */
88 const struct pciide_product_desc *sc_pp;
89 /* common definitions */
90 struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
91 /* internal bookkeeping */
92 struct pciide_channel { /* per-channel data */
93 int hw_ok; /* hardware mapped & OK? */
94 int compat; /* is it compat? */
95 void *ih; /* compat or pci handle */
96 /* DMA tables and DMA map for xfer, for each drive */
97 struct pciide_dma_maps {
98 bus_dmamap_t dmamap_table;
99 struct idedma_table *dma_table;
100 bus_dmamap_t dmamap_xfer;
101 } dma_maps[2];
102 } pciide_channels[PCIIDE_NUM_CHANNELS];
103 };
104
105 void default_setup_cap __P((struct pciide_softc*));
106 void default_setup_chip __P((struct pciide_softc*,
107 pci_chipset_tag_t, pcitag_t));
108 const char *default_compat_channel_probe __P((struct pciide_softc *,
109 struct pci_attach_args *, int));
110 void piix_setup_cap __P((struct pciide_softc*));
111 void piix_setup_chip __P((struct pciide_softc*,
112 pci_chipset_tag_t, pcitag_t));
113 void piix3_4_setup_chip __P((struct pciide_softc*,
114 pci_chipset_tag_t, pcitag_t));
115
116 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
117 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
118 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
119
120 void apollo_setup_cap __P((struct pciide_softc*));
121 void apollo_setup_chip __P((struct pciide_softc*,
122 pci_chipset_tag_t, pcitag_t));
123
124 const char *cmd_compat_channel_probe __P((struct pciide_softc *,
125 struct pci_attach_args *, int));
126
127 int pciide_dma_table_setup __P((struct pciide_softc*, int, int));
128 int pciide_dma_init __P((void*, int, int, void *, size_t, int));
129 void pciide_dma_start __P((void*, int, int, int));
130 int pciide_dma_finish __P((void*, int, int, int));
131
132 struct pciide_product_desc {
133 u_int32_t ide_product;
134 int ide_flags;
135 const char *ide_name;
136 /* init controller's capabilities for drives probe */
137 void (*setup_cap) __P((struct pciide_softc*));
138 /* init controller after drives probe */
139 void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
140 /* Probe for compat channel enabled/disabled */
141 const char * (*compat_channel_probe) __P((struct pciide_softc *,
142 struct pci_attach_args *, int));
143 };
144
145 /* Flags for ide_flags */
146 #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
147 #define ONE_QUEUE 0x02 /* device need serialised access */
148
149 /* Default product description for devices not known from this controller */
150 const struct pciide_product_desc default_product_desc = {
151 0,
152 0,
153 "Generic PCI IDE controller",
154 default_setup_cap,
155 default_setup_chip,
156 default_compat_channel_probe
157 };
158
159
160 const struct pciide_product_desc pciide_intel_products[] = {
161 { PCI_PRODUCT_INTEL_82092AA,
162 0,
163 "Intel 82092AA IDE controller",
164 default_setup_cap,
165 default_setup_chip,
166 default_compat_channel_probe
167 },
168 { PCI_PRODUCT_INTEL_82371FB_IDE,
169 0,
170 "Intel 82371FB IDE controller (PIIX)",
171 piix_setup_cap,
172 piix_setup_chip,
173 default_compat_channel_probe
174 },
175 { PCI_PRODUCT_INTEL_82371SB_IDE,
176 0,
177 "Intel 82371SB IDE Interface (PIIX3)",
178 piix_setup_cap,
179 piix3_4_setup_chip,
180 default_compat_channel_probe
181 },
182 { PCI_PRODUCT_INTEL_82371AB_IDE,
183 0,
184 "Intel 82371AB IDE controller (PIIX4)",
185 piix_setup_cap,
186 piix3_4_setup_chip,
187 default_compat_channel_probe
188 },
189 { 0,
190 0,
191 NULL,
192 }
193 };
194 const struct pciide_product_desc pciide_cmd_products[] = {
195 { PCI_PRODUCT_CMDTECH_640,
196 ONE_QUEUE | CMD_PCI064x_IOEN,
197 "CMD Technology PCI0640",
198 default_setup_cap,
199 default_setup_chip,
200 cmd_compat_channel_probe
201 },
202 { 0,
203 0,
204 NULL,
205 }
206 };
207
208 const struct pciide_product_desc pciide_via_products[] = {
209 { PCI_PRODUCT_VIATECH_VT82C586_IDE,
210 0,
211 "VT82C586 (Apollo VP) IDE Controller",
212 apollo_setup_cap,
213 apollo_setup_chip,
214 default_compat_channel_probe
215 },
216 { 0,
217 0,
218 NULL,
219 }
220 };
221
222 struct pciide_vendor_desc {
223 u_int32_t ide_vendor;
224 const struct pciide_product_desc *ide_products;
225 };
226
227 const struct pciide_vendor_desc pciide_vendors[] = {
228 { PCI_VENDOR_INTEL, pciide_intel_products },
229 { PCI_VENDOR_CMDTECH, pciide_cmd_products },
230 { PCI_VENDOR_VIATECH, pciide_via_products },
231 { 0, NULL }
232 };
233
234
235 #define PCIIDE_CHANNEL_NAME(chan) ((chan) == 0 ? "primary" : "secondary")
236
237 int pciide_match __P((struct device *, struct cfdata *, void *));
238 void pciide_attach __P((struct device *, struct device *, void *));
239
240 struct cfattach pciide_ca = {
241 sizeof(struct pciide_softc), pciide_match, pciide_attach
242 };
243
244 int pciide_map_channel_compat __P((struct pciide_softc *,
245 struct pci_attach_args *, int));
246 int pciide_map_channel_native __P((struct pciide_softc *,
247 struct pci_attach_args *, int));
248 int pciide_print __P((void *, const char *pnp));
249 int pciide_compat_intr __P((void *));
250 int pciide_pci_intr __P((void *));
251 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
252
253 const struct pciide_product_desc*
254 pciide_lookup_product(id)
255 u_int32_t id;
256 {
257 const struct pciide_product_desc *pp;
258 const struct pciide_vendor_desc *vp;
259
260 for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
261 if (PCI_VENDOR(id) == vp->ide_vendor)
262 break;
263
264 if ((pp = vp->ide_products) == NULL)
265 return NULL;
266
267 for (; pp->ide_name != NULL; pp++)
268 if (PCI_PRODUCT(id) == pp->ide_product)
269 break;
270
271 if (pp->ide_name == NULL)
272 return NULL;
273 return pp;
274 }
275
276 int
277 pciide_match(parent, match, aux)
278 struct device *parent;
279 struct cfdata *match;
280 void *aux;
281 {
282 struct pci_attach_args *pa = aux;
283
284 /*
285 * Check the ID register to see that it's a PCI IDE controller.
286 * If it is, we assume that we can deal with it; it _should_
287 * work in a standardized way...
288 */
289 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
290 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
291 return (1);
292 }
293
294 return (0);
295 }
296
297 void
298 pciide_attach(parent, self, aux)
299 struct device *parent, *self;
300 void *aux;
301 {
302 struct pci_attach_args *pa = aux;
303 pci_chipset_tag_t pc = pa->pa_pc;
304 pcitag_t tag = pa->pa_tag;
305 struct pciide_softc *sc = (struct pciide_softc *)self;
306 struct pciide_channel *cp;
307 pcireg_t class, interface, csr;
308 pci_intr_handle_t intrhandle;
309 const char *intrstr;
310 char devinfo[256];
311 int i;
312
313 sc->sc_pp = pciide_lookup_product(pa->pa_id);
314 if (sc->sc_pp == NULL) {
315 sc->sc_pp = &default_product_desc;
316 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
317 printf(": %s (rev. 0x%02x)\n", devinfo,
318 PCI_REVISION(pa->pa_class));
319 } else {
320 printf(": %s\n", sc->sc_pp->ide_name);
321 }
322
323 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
324 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
325 /*
326 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
327 * and base adresses registers can be disabled at
328 * hardware level. In this case, the device is wired
329 * in compat mode and its first channel is always enabled,
330 * but we can't rely on PCI_COMMAND_IO_ENABLE.
331 * In fact, it seems that the first channel of the CMD PCI0640
332 * can't be disabled.
333 */
334 if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
335 printf("%s: device disabled (at %s)\n",
336 sc->sc_wdcdev.sc_dev.dv_xname,
337 (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
338 "device" : "bridge");
339 return;
340 }
341 }
342
343 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
344 interface = PCI_INTERFACE(class);
345
346 /*
347 * Set up PCI interrupt only if at last one channel is in native mode.
348 * At last one device (CMD PCI0640) has a default value of 14, which
349 * will be mapped even if both channels are in compat-only mode.
350 */
351 if (interface & (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1))) {
352 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
353 pa->pa_intrline, &intrhandle) != 0) {
354 printf("%s: couldn't map native-PCI interrupt\n",
355 sc->sc_wdcdev.sc_dev.dv_xname);
356 } else {
357 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
358 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
359 intrhandle, IPL_BIO, pciide_pci_intr, sc);
360 if (sc->sc_pci_ih != NULL) {
361 printf("%s: using %s for native-PCI "
362 "interrupt\n",
363 sc->sc_wdcdev.sc_dev.dv_xname,
364 intrstr ? intrstr : "unknown interrupt");
365 } else {
366 printf("%s: couldn't establish native-PCI "
367 "interrupt",
368 sc->sc_wdcdev.sc_dev.dv_xname);
369 if (intrstr != NULL)
370 printf(" at %s", intrstr);
371 printf("\n");
372 }
373 }
374 }
375
376 /*
377 * Map DMA registers, if DMA is supported.
378 *
379 * Note that sc_dma_ok is the right variable to test to see if
380 * DMA can be done. If the interface doesn't support DMA,
381 * sc_dma_ok will never be non-zero. If the DMA regs couldn't
382 * be mapped, it'll be zero. I.e., sc_dma_ok will only be
383 * non-zero if the interface supports DMA and the registers
384 * could be mapped.
385 *
386 * XXX Note that despite the fact that the Bus Master IDE specs
387 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
388 * XXX space," some controllers (at least the United
389 * XXX Microelectronics UM8886BF) place it in memory space.
390 * XXX eventually, we should probably read the register and check
391 * XXX which type it is. Either that or 'quirk' certain devices.
392 */
393 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
394 sc->sc_dma_ok = (pci_mapreg_map(pa,
395 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
396 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
397 sc->sc_dmat = pa->pa_dmat;
398 printf("%s: bus-master DMA support present",
399 sc->sc_wdcdev.sc_dev.dv_xname);
400 if (sc->sc_dma_ok == 0) {
401 printf(", but unused (couldn't map registers)");
402 } else if (sc->sc_pp == 0) {
403 printf(", but unused (no driver support)");
404 } else {
405 sc->sc_wdcdev.dma_arg = sc;
406 sc->sc_wdcdev.dma_init = pciide_dma_init;
407 sc->sc_wdcdev.dma_start = pciide_dma_start;
408 sc->sc_wdcdev.dma_finish = pciide_dma_finish;
409 }
410 printf("\n");
411 }
412 if (sc->sc_pp == NULL)
413 default_setup_cap(sc);
414 else
415 sc->sc_pp->setup_cap(sc);
416 sc->sc_wdcdev.channels = sc->wdc_channels;
417 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
418
419 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
420 cp = &sc->pciide_channels[i];
421
422 sc->wdc_channels[i].channel = i;
423 sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
424 if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
425 sc->wdc_channels[i].ch_queue =
426 sc->wdc_channels[0].ch_queue;
427 } else {
428 sc->wdc_channels[i].ch_queue =
429 malloc(sizeof(struct channel_queue), M_DEVBUF,
430 M_NOWAIT);
431 }
432 if (sc->wdc_channels[i].ch_queue == NULL) {
433 printf("%s %s channel: "
434 "can't allocate memory for command queue",
435 sc->sc_wdcdev.sc_dev.dv_xname,
436 PCIIDE_CHANNEL_NAME(i));
437 continue;
438 }
439 printf("%s: %s channel %s to %s mode\n",
440 sc->sc_wdcdev.sc_dev.dv_xname,
441 PCIIDE_CHANNEL_NAME(i),
442 (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
443 "configured" : "wired",
444 (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
445 "compatibility");
446
447 if (interface & PCIIDE_INTERFACE_PCI(i))
448 cp->hw_ok = pciide_map_channel_native(sc, pa, i);
449 else
450 cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
451 if (!cp->hw_ok)
452 continue;
453 /* Now call common attach routine */
454 wdcattach(&sc->wdc_channels[i]);
455 }
456 if (sc->sc_pp == NULL)
457 default_setup_chip(sc, pc, tag);
458 else
459 sc->sc_pp->setup_chip(sc, pc, tag);
460 WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
461 pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
462 }
463
464 int
465 pciide_map_channel_compat(sc, pa, chan)
466 struct pciide_softc *sc;
467 struct pci_attach_args *pa;
468 int chan;
469 {
470 struct pciide_channel *cp = &sc->pciide_channels[chan];
471 struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
472 const char *probe_fail_reason;
473 int rv = 1;
474
475 cp->compat = 1;
476
477 wdc_cp->cmd_iot = pa->pa_iot;
478 if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
479 PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
480 printf("%s: couldn't map %s channel cmd regs\n",
481 sc->sc_wdcdev.sc_dev.dv_xname,
482 PCIIDE_CHANNEL_NAME(chan));
483 rv = 0;
484 }
485
486 wdc_cp->ctl_iot = pa->pa_iot;
487 if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
488 PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
489 printf("%s: couldn't map %s channel ctl regs\n",
490 sc->sc_wdcdev.sc_dev.dv_xname,
491 PCIIDE_CHANNEL_NAME(chan));
492 rv = 0;
493 }
494
495 /*
496 * If we weren't able to map the device successfully,
497 * we just give up now. Something else has already
498 * occupied those ports, indicating that the device has
499 * (probably) been completely disabled (by some nonstandard
500 * mechanism).
501 *
502 * XXX If we successfully map some ports, but not others,
503 * XXX it might make sense to unmap the ones that we mapped.
504 */
505 if (rv == 0)
506 goto out;
507
508 /*
509 * If we were able to map the device successfully, try to
510 * make sure that there's a wdc there and that it's
511 * attributable to us.
512 *
513 * If there's not, then we assume that there's the device
514 * has been disabled and that other devices are free to use
515 * its ports.
516 */
517 probe_fail_reason = sc->sc_pp->compat_channel_probe(sc, pa, chan);
518 if (probe_fail_reason != NULL) {
519 printf("%s: %s channel ignored (%s)\n",
520 sc->sc_wdcdev.sc_dev.dv_xname,
521 PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
522 rv = 0;
523
524 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
525 PCIIDE_COMPAT_CMD_SIZE);
526 bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
527 PCIIDE_COMPAT_CTL_SIZE);
528
529 goto out;
530 }
531
532 /*
533 * If we're here, we were able to map the device successfully
534 * and it really looks like there's a controller there.
535 *
536 * Unless those conditions are true, we don't map the
537 * compatibility interrupt. The spec indicates that if a
538 * channel is configured for compatibility mode and the PCI
539 * device's I/O space is enabled, the channel will be enabled.
540 * Hoewver, some devices seem to be able to disable invididual
541 * compatibility channels (via non-standard mechanisms). If
542 * the channel is disabled, the interrupt line can (probably)
543 * be used by other devices (and may be assigned to other
544 * devices by the BIOS). If we mapped the interrupt we might
545 * conflict with another interrupt assignment.
546 */
547 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
548 pa, chan, pciide_compat_intr, wdc_cp);
549 if (cp->ih == NULL) {
550 printf("%s: no compatibility interrupt for use by %s channel\n",
551 sc->sc_wdcdev.sc_dev.dv_xname,
552 PCIIDE_CHANNEL_NAME(chan));
553 rv = 0;
554 }
555
556 out:
557 return (rv);
558 }
559
560 int
561 pciide_map_channel_native(sc, pa, chan)
562 struct pciide_softc *sc;
563 struct pci_attach_args *pa;
564 int chan;
565 {
566 struct pciide_channel *cp = &sc->pciide_channels[chan];
567 struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
568 int rv = 1;
569
570 cp->compat = 0;
571
572 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
573 0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
574 printf("%s: couldn't map %s channel cmd regs\n",
575 sc->sc_wdcdev.sc_dev.dv_xname,
576 PCIIDE_CHANNEL_NAME(chan));
577 rv = 0;
578 }
579
580 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
581 0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
582 printf("%s: couldn't map %s channel ctl regs\n",
583 sc->sc_wdcdev.sc_dev.dv_xname,
584 PCIIDE_CHANNEL_NAME(chan));
585 rv = 0;
586 }
587
588 if ((cp->ih = sc->sc_pci_ih) == NULL) {
589 printf("%s: no native-PCI interrupt for use by %s channel\n",
590 sc->sc_wdcdev.sc_dev.dv_xname,
591 PCIIDE_CHANNEL_NAME(chan));
592 rv = 0;
593 }
594
595 return (rv);
596 }
597
598 int
599 pciide_compat_intr(arg)
600 void *arg;
601 {
602 struct channel_softc *wdc_cp = arg;
603
604 #ifdef DIAGNOSTIC
605 struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
606 struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
607 /* should only be called for a compat channel */
608 if (cp->compat == 0)
609 panic("pciide compat intr called for non-compat chan %p\n", cp);
610 #endif
611 return (wdcintr(wdc_cp));
612 }
613
614 int
615 pciide_pci_intr(arg)
616 void *arg;
617 {
618 struct pciide_softc *sc = arg;
619 struct pciide_channel *cp;
620 struct channel_softc *wdc_cp;
621 int i, rv, crv;
622
623 rv = 0;
624 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
625 cp = &sc->pciide_channels[i];
626 wdc_cp = &sc->wdc_channels[i];
627
628 /* If a compat channel skip. */
629 if (cp->compat)
630 continue;
631 /* if this channel not waiting for intr, skip */
632 if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
633 continue;
634
635 crv = wdcintr(wdc_cp);
636 if (crv == 0)
637 ; /* leave rv alone */
638 else if (crv == 1)
639 rv = 1; /* claim the intr */
640 else if (rv == 0) /* crv should be -1 in this case */
641 rv = crv; /* if we've done no better, take it */
642 }
643 return (rv);
644 }
645
646 void
647 default_setup_cap(sc)
648 struct pciide_softc *sc;
649 {
650 if (sc->sc_dma_ok)
651 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
652 sc->sc_wdcdev.pio_mode = 0;
653 sc->sc_wdcdev.dma_mode = 0;
654 }
655
656 void
657 default_setup_chip(sc, pc, tag)
658 struct pciide_softc *sc;
659 pci_chipset_tag_t pc;
660 pcitag_t tag;
661 {
662 int channel, drive, idedma_ctl;
663 struct channel_softc *chp;
664 struct ata_drive_datas *drvp;
665
666 if (sc->sc_dma_ok == 0)
667 return; /* nothing to do */
668
669 /* Allocate DMA maps */
670 for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
671 idedma_ctl = 0;
672 chp = &sc->wdc_channels[channel];
673 for (drive = 0; drive < 2; drive++) {
674 drvp = &chp->ch_drive[drive];
675 /* If no drive, skip */
676 if ((drvp->drive_flags & DRIVE) == 0)
677 continue;
678 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
679 /* Abort DMA setup */
680 printf("%s:%d:%d: can't allocate DMA maps, "
681 "using PIO transferts\n",
682 sc->sc_wdcdev.sc_dev.dv_xname,
683 channel, drive);
684 drvp->drive_flags &= ~DRIVE_DMA;
685 }
686 printf("%s:%d:%d: using DMA mode %d\n",
687 sc->sc_wdcdev.sc_dev.dv_xname,
688 channel, drive,
689 drvp->DMA_mode);
690 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
691 }
692 if (idedma_ctl != 0) {
693 /* Add software bits in status register */
694 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
695 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
696 idedma_ctl);
697 }
698 }
699
700 }
701
702 const char *
703 default_compat_channel_probe(sc, pa, chan)
704 struct pciide_softc *sc;
705 struct pci_attach_args *pa;
706 {
707 pcireg_t csr;
708 const char *failreason = NULL;
709
710 /*
711 * Check to see if something appears to be there.
712 */
713 if (!wdcprobe(&sc->wdc_channels[chan])) {
714 failreason = "not responding; disabled or no drives?";
715 goto out;
716 }
717
718 /*
719 * Now, make sure it's actually attributable to this PCI IDE
720 * channel by trying to access the channel again while the
721 * PCI IDE controller's I/O space is disabled. (If the
722 * channel no longer appears to be there, it belongs to
723 * this controller.) YUCK!
724 */
725 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
726 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
727 csr & ~PCI_COMMAND_IO_ENABLE);
728 if (wdcprobe(&sc->wdc_channels[chan]))
729 failreason = "other hardware responding at addresses";
730 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
731
732 out:
733 return (failreason);
734 }
735
736 void
737 piix_setup_cap(sc)
738 struct pciide_softc *sc;
739 {
740 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
741 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
742 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
743 WDC_CAPABILITY_DMA;
744 sc->sc_wdcdev.pio_mode = 4;
745 sc->sc_wdcdev.dma_mode = 2;
746 }
747
748 void
749 piix_setup_chip(sc, pc, tag)
750 struct pciide_softc *sc;
751 pci_chipset_tag_t pc;
752 pcitag_t tag;
753 {
754 struct channel_softc *chp;
755 u_int8_t mode[2];
756 u_int8_t channel, drive;
757 u_int32_t idetim, sidetim, idedma_ctl;
758 struct ata_drive_datas *drvp;
759
760 idetim = sidetim = 0;
761
762 WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
763 pci_conf_read(pc, tag, PIIX_IDETIM),
764 pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
765
766 for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
767 chp = &sc->wdc_channels[channel];
768 drvp = chp->ch_drive;
769 idedma_ctl = 0;
770 /* Enable IDE registers decode */
771 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
772 channel);
773
774 /* setup DMA if needed */
775 for (drive = 0; drive < 2; drive++) {
776 if (drvp[drive].drive_flags & DRIVE_DMA &&
777 pciide_dma_table_setup(sc, channel, drive) != 0) {
778 drvp[drive].drive_flags &= ~DRIVE_DMA;
779 }
780 }
781
782 /*
783 * Here we have to mess up with drives mode: PIIX can't have
784 * different timings for master and slave drives.
785 * We need to find the best combination.
786 */
787
788 /* If both drives supports DMA, takes the lower mode */
789 if ((drvp[0].drive_flags & DRIVE_DMA) &&
790 (drvp[1].drive_flags & DRIVE_DMA)) {
791 mode[0] = mode[1] =
792 min(drvp[0].DMA_mode, drvp[1].DMA_mode);
793 drvp[0].DMA_mode = mode[0];
794 goto ok;
795 }
796 /*
797 * If only one drive supports DMA, use its mode, and
798 * put the other one in PIO mode 0 if mode not compatible
799 */
800 if (drvp[0].drive_flags & DRIVE_DMA) {
801 mode[0] = drvp[0].DMA_mode;
802 mode[1] = drvp[1].PIO_mode;
803 if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
804 piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
805 mode[1] = 0;
806 goto ok;
807 }
808 if (drvp[1].drive_flags & DRIVE_DMA) {
809 mode[1] = drvp[1].DMA_mode;
810 mode[0] = drvp[0].PIO_mode;
811 if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
812 piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
813 mode[0] = 0;
814 goto ok;
815 }
816 /*
817 * If both drives are not DMA, takes the lower mode, unless
818 * one of them is PIO mode < 2
819 */
820 if (drvp[0].PIO_mode < 2) {
821 mode[0] = 0;
822 mode[1] = drvp[1].PIO_mode;
823 } else if (drvp[1].PIO_mode < 2) {
824 mode[1] = 0;
825 mode[0] = drvp[0].PIO_mode;
826 } else {
827 mode[0] = mode[1] =
828 min(drvp[1].PIO_mode, drvp[0].PIO_mode);
829 }
830 ok: /* The modes are setup */
831 for (drive = 0; drive < 2; drive++) {
832 if (drvp[drive].drive_flags & DRIVE_DMA) {
833 drvp[drive].DMA_mode = mode[drive];
834 idetim |= piix_setup_idetim_timings(
835 mode[drive], 1, channel);
836 goto end;
837 } else
838 drvp[drive].PIO_mode = mode[drive];
839 }
840 /* If we are there, none of the drives are DMA */
841 if (mode[0] >= 2)
842 idetim |= piix_setup_idetim_timings(
843 mode[0], 0, channel);
844 else
845 idetim |= piix_setup_idetim_timings(
846 mode[1], 0, channel);
847 end: /*
848 * timing mode is now set up in the controller. Enable
849 * it per-drive
850 */
851 for (drive = 0; drive < 2; drive++) {
852 /* If no drive, skip */
853 if ((drvp[drive].drive_flags & DRIVE) == 0)
854 continue;
855 idetim |= piix_setup_idetim_drvs(&drvp[drive]);
856 printf("%s:%d:%d: using PIO mode %d",
857 sc->sc_wdcdev.sc_dev.dv_xname,
858 channel, drive, drvp[drive].PIO_mode);
859 if (drvp[drive].drive_flags & DRIVE_DMA) {
860 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
861 printf(", DMA mode %d", drvp[drive].DMA_mode);
862 }
863 printf("\n");
864 }
865 if (idedma_ctl != 0) {
866 /* Add software bits in status register */
867 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
868 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
869 idedma_ctl);
870 }
871 }
872 WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x, sidetim=0x%x\n",
873 idetim, sidetim), DEBUG_PROBE);
874 pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
875 pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
876 }
877
878 void
879 piix3_4_setup_chip(sc, pc, tag)
880 struct pciide_softc *sc;
881 pci_chipset_tag_t pc;
882 pcitag_t tag;
883 {
884 int channel, drive;
885 struct channel_softc *chp;
886 struct ata_drive_datas *drvp;
887 u_int32_t idetim, sidetim, udmareg, idedma_ctl;
888
889 idetim = sidetim = udmareg = 0;
890
891 WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
892 pci_conf_read(pc, tag, PIIX_IDETIM),
893 pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
894 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
895 WDCDEBUG_PRINT((", udamreg 0x%x",
896 pci_conf_read(pc, tag, PIIX_UDMAREG)),
897 DEBUG_PROBE);
898 }
899 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
900
901 for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
902 chp = &sc->wdc_channels[channel];
903 idedma_ctl = 0;
904 /* Enable IDE registers decode */
905 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
906 channel);
907 for (drive = 0; drive < 2; drive++) {
908 drvp = &chp->ch_drive[drive];
909 /* If no drive, skip */
910 if ((drvp->drive_flags & DRIVE) == 0)
911 continue;
912 /* add timing values, setup DMA if needed */
913 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
914 (drvp->drive_flags & DRIVE_UDMA) == 0) ||
915 sc->sc_dma_ok == 0) {
916 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
917 goto pio;
918 }
919 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
920 /* Abort DMA setup */
921 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
922 goto pio;
923 }
924 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
925 (drvp->drive_flags & DRIVE_UDMA)) {
926 /* use Ultra/DMA */
927 drvp->drive_flags &= ~DRIVE_DMA;
928 udmareg |= PIIX_UDMACTL_DRV_EN(
929 channel, drive);
930 udmareg |= PIIX_UDMATIM_SET(
931 piix4_sct_udma[drvp->UDMA_mode],
932 channel, drive);
933 } else {
934 /* use Multiword DMA */
935 drvp->drive_flags &= ~DRIVE_UDMA;
936 if (drive == 0) {
937 idetim |= piix_setup_idetim_timings(
938 drvp->DMA_mode, 1, channel);
939 } else {
940 sidetim |= piix_setup_sidetim_timings(
941 drvp->DMA_mode, 1, channel);
942 idetim =PIIX_IDETIM_SET(idetim,
943 PIIX_IDETIM_SITRE, channel);
944 }
945 }
946 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
947
948 pio: /* use PIO mode */
949 idetim |= piix_setup_idetim_drvs(drvp);
950 if (drive == 0) {
951 idetim |= piix_setup_idetim_timings(
952 drvp->PIO_mode, 0, channel);
953 } else {
954 sidetim |= piix_setup_sidetim_timings(
955 drvp->PIO_mode, 0, channel);
956 idetim =PIIX_IDETIM_SET(idetim,
957 PIIX_IDETIM_SITRE, channel);
958 }
959 printf("%s:%d:%d: using PIO mode %d",
960 sc->sc_wdcdev.sc_dev.dv_xname,
961 channel, drive, drvp->PIO_mode);
962 if (drvp[drive].drive_flags & DRIVE_DMA)
963 printf(", DMA mode %d", drvp->DMA_mode);
964 if (drvp->drive_flags & DRIVE_UDMA)
965 printf(", UDMA mode %d", drvp->UDMA_mode);
966 printf("\n");
967 }
968 if (idedma_ctl != 0) {
969 /* Add software bits in status register */
970 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
971 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
972 idedma_ctl);
973 }
974 }
975
976 WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
977 idetim, sidetim), DEBUG_PROBE);
978 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
979 WDCDEBUG_PRINT((", udmareg=0x%x", udmareg), DEBUG_PROBE);
980 pci_conf_write(pc, tag, PIIX_UDMAREG, udmareg);
981 }
982 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
983 pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
984 pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
985 }
986
987 /* setup ISP and RTC fields, based on mode */
988 static u_int32_t
989 piix_setup_idetim_timings(mode, dma, channel)
990 u_int8_t mode;
991 u_int8_t dma;
992 u_int8_t channel;
993 {
994
995 if (dma)
996 return PIIX_IDETIM_SET(0,
997 PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
998 PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
999 channel);
1000 else
1001 return PIIX_IDETIM_SET(0,
1002 PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
1003 PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
1004 channel);
1005 }
1006
1007 /* setup DTE, PPE, IE and TIME field based on PIO mode */
1008 static u_int32_t
1009 piix_setup_idetim_drvs(drvp)
1010 struct ata_drive_datas *drvp;
1011 {
1012 u_int32_t ret = 0;
1013 struct channel_softc *chp = drvp->chnl_softc;
1014 u_int8_t channel = chp->channel;
1015 u_int8_t drive = drvp->drive;
1016
1017 /*
1018 * If drive is using UDMA, timings setups are independant
1019 * So just check DMA and PIO here.
1020 */
1021 if (drvp->drive_flags & DRIVE_DMA) {
1022 /* if mode = DMA mode 0, use compatible timings */
1023 if ((drvp->drive_flags & DRIVE_DMA) &&
1024 drvp->DMA_mode == 0) {
1025 drvp->PIO_mode = 0;
1026 return ret;
1027 }
1028 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1029 /*
1030 * PIO and DMA timings are the same, use fast timings for PIO
1031 * too, else use compat timings.
1032 */
1033 if ((piix_isp_pio[drvp->PIO_mode] !=
1034 piix_isp_dma[drvp->DMA_mode]) ||
1035 (piix_rtc_pio[drvp->PIO_mode] !=
1036 piix_rtc_dma[drvp->DMA_mode]))
1037 drvp->PIO_mode = 0;
1038 /* if PIO mode <= 2, use compat timings for PIO */
1039 if (drvp->PIO_mode <= 2) {
1040 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
1041 channel);
1042 return ret;
1043 }
1044 }
1045
1046 /*
1047 * Now setup PIO modes. If mode < 2, use compat timings.
1048 * Else enable fast timings. Enable IORDY and prefetch/post
1049 * if PIO mode >= 3.
1050 */
1051
1052 if (drvp->PIO_mode < 2)
1053 return ret;
1054
1055 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1056 if (drvp->PIO_mode >= 3) {
1057 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
1058 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
1059 }
1060 return ret;
1061 }
1062
1063 /* setup values in SIDETIM registers, based on mode */
1064 static u_int32_t
1065 piix_setup_sidetim_timings(mode, dma, channel)
1066 u_int8_t mode;
1067 u_int8_t dma;
1068 u_int8_t channel;
1069 {
1070 if (dma)
1071 return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
1072 PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
1073 else
1074 return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
1075 PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
1076 }
1077
1078 void
1079 apollo_setup_cap(sc)
1080 struct pciide_softc *sc;
1081 {
1082 if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586_IDE)
1083 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1084 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
1085 WDC_CAPABILITY_DMA;
1086 sc->sc_wdcdev.pio_mode = 4;
1087 sc->sc_wdcdev.dma_mode = 2;
1088
1089 }
1090 void
1091 apollo_setup_chip(sc, pc, tag)
1092 struct pciide_softc *sc;
1093 pci_chipset_tag_t pc;
1094 pcitag_t tag;
1095 {
1096 u_int32_t udmatim_reg, ideconf_reg, ctlmisc_reg, datatim_reg;
1097 u_int8_t idedma_ctl;
1098 int mode;
1099 int channel, drive;
1100 struct channel_softc *chp;
1101 struct ata_drive_datas *drvp;
1102
1103 ideconf_reg = pci_conf_read(pc, tag, APO_IDECONF);
1104 ctlmisc_reg = pci_conf_read(pc, tag, APO_CTLMISC);
1105
1106 WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
1107 "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1108 ideconf_reg, ctlmisc_reg,
1109 pci_conf_read(pc, tag, APO_DATATIM),
1110 pci_conf_read(pc, tag, APO_UDMA)),
1111 DEBUG_PROBE);
1112
1113 datatim_reg = 0;
1114 udmatim_reg = 0;
1115 for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
1116 chp = &sc->wdc_channels[channel];
1117 idedma_ctl = 0;
1118 for (drive = 0; drive < 2; drive++) {
1119 drvp = &chp->ch_drive[drive];
1120 /* If no drive, skip */
1121 if ((drvp->drive_flags & DRIVE) == 0)
1122 continue;
1123 /* add timing values, setup DMA if needed */
1124 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1125 (drvp->drive_flags & DRIVE_UDMA) == 0) ||
1126 sc->sc_dma_ok == 0) {
1127 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1128 mode = drvp->PIO_mode;
1129 goto pio;
1130 }
1131 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1132 /* Abort DMA setup */
1133 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1134 mode = drvp->PIO_mode;
1135 goto pio;
1136 }
1137 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1138 (drvp->drive_flags & DRIVE_UDMA)) {
1139 /* use Ultra/DMA */
1140 drvp->drive_flags &= ~DRIVE_DMA;
1141 udmatim_reg |= APO_UDMA_EN(channel, drive) |
1142 APO_UDMA_EN_MTH(channel, drive) |
1143 APO_UDMA_TIME(channel, drive,
1144 apollo_udma_tim[drvp->UDMA_mode]);
1145 /* can use PIO timings, MW DMA unused */
1146 mode = drvp->PIO_mode;
1147 } else {
1148 /* use Multiword DMA */
1149 drvp->drive_flags &= ~DRIVE_UDMA;
1150 /* mode = min(pio, dma+2) */
1151 if (drvp->PIO_mode <= (drvp->DMA_mode +2))
1152 mode = drvp->PIO_mode;
1153 else
1154 mode = drvp->DMA_mode;
1155 }
1156 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1157
1158 pio: /* setup PIO mode */
1159 datatim_reg |=
1160 APO_DATATIM_PULSE(channel, drive,
1161 apollo_pio_set[mode]) |
1162 APO_DATATIM_RECOV(channel, drive,
1163 apollo_pio_rec[mode]);
1164 drvp->PIO_mode = mode;
1165 drvp->DMA_mode = mode + 2;
1166 printf("%s:%d:%d: using PIO mode %d",
1167 sc->sc_wdcdev.sc_dev.dv_xname,
1168 channel, drive, drvp->PIO_mode);
1169 if (drvp[drive].drive_flags & DRIVE_DMA)
1170 printf(", DMA mode %d", drvp->DMA_mode);
1171 if (drvp->drive_flags & DRIVE_UDMA)
1172 printf(", UDMA mode %d", drvp->UDMA_mode);
1173 printf("\n");
1174 }
1175 if (idedma_ctl != 0) {
1176 /* Add software bits in status register */
1177 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1178 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1179 idedma_ctl);
1180 }
1181 }
1182 WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1183 datatim_reg, udmatim_reg), DEBUG_PROBE);
1184 pci_conf_write(pc, tag, APO_DATATIM, datatim_reg);
1185 pci_conf_write(pc, tag, APO_UDMA, udmatim_reg);
1186 }
1187
1188 /*
1189 * The default channel probe may not work for a CMD device, as the use of
1190 * I/O register may be disabled by hardware. Look at the specific registers
1191 */
1192
1193 const char*
1194 cmd_compat_channel_probe(sc, pa, chan)
1195 struct pciide_softc *sc;
1196 struct pci_attach_args *pa;
1197 int chan;
1198 {
1199
1200 /*
1201 * with a CMD PCI64x, if we get here, the first channel is enabled:
1202 * there's no way to disable the first channel without disabling
1203 * the whole device
1204 */
1205 if (chan == 0)
1206 return NULL;
1207
1208 /* Second channel is enabled if CMD_CONF_2PORT is set */
1209 if ((pci_conf_read(pa->pa_pc, pa->pa_tag, CMD_CONF_CTRL0) &
1210 CMD_CONF_2PORT) == 0)
1211 return "disabled";
1212
1213 return NULL;
1214 }
1215
1216 int
1217 pciide_dma_table_setup(sc, channel, drive)
1218 struct pciide_softc *sc;
1219 int channel, drive;
1220 {
1221 bus_dma_segment_t seg;
1222 int error, rseg;
1223 const bus_size_t dma_table_size =
1224 sizeof(struct idedma_table) * NIDEDMA_TABLES;
1225 struct pciide_dma_maps *dma_maps =
1226 &sc->pciide_channels[channel].dma_maps[drive];
1227
1228 /* Allocate memory for the DMA tables and map it */
1229 if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
1230 IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
1231 BUS_DMA_NOWAIT)) != 0) {
1232 printf("%s:%d: unable to allocate table DMA for"
1233 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
1234 channel, drive, error);
1235 return error;
1236 }
1237 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
1238 dma_table_size,
1239 (caddr_t *)&dma_maps->dma_table,
1240 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
1241 printf("%s:%d: unable to map table DMA for"
1242 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
1243 channel, drive, error);
1244 return error;
1245 }
1246 WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
1247 "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
1248 seg.ds_addr), DEBUG_PROBE);
1249
1250 /* Create and load table DMA map for this disk */
1251 if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
1252 1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
1253 &dma_maps->dmamap_table)) != 0) {
1254 printf("%s:%d: unable to create table DMA map for"
1255 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
1256 channel, drive, error);
1257 return error;
1258 }
1259 if ((error = bus_dmamap_load(sc->sc_dmat,
1260 dma_maps->dmamap_table,
1261 dma_maps->dma_table,
1262 dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
1263 printf("%s:%d: unable to load table DMA map for"
1264 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
1265 channel, drive, error);
1266 return error;
1267 }
1268 WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
1269 dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
1270 /* Create a xfer DMA map for this drive */
1271 if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
1272 NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
1273 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
1274 &dma_maps->dmamap_xfer)) != 0) {
1275 printf("%s:%d: unable to create xfer DMA map for"
1276 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
1277 channel, drive, error);
1278 return error;
1279 }
1280 return 0;
1281 }
1282
1283 int
1284 pciide_dma_init(v, channel, drive, databuf, datalen, read)
1285 void *v;
1286 int channel, drive;
1287 void *databuf;
1288 size_t datalen;
1289 int read;
1290 {
1291 struct pciide_softc *sc = v;
1292 int error, seg;
1293 struct pciide_dma_maps *dma_maps =
1294 &sc->pciide_channels[channel].dma_maps[drive];
1295
1296 error = bus_dmamap_load(sc->sc_dmat,
1297 dma_maps->dmamap_xfer,
1298 databuf, datalen, NULL, BUS_DMA_NOWAIT);
1299 if (error) {
1300 printf("%s:%d: unable to load xfer DMA map for"
1301 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
1302 channel, drive, error);
1303 return error;
1304 }
1305
1306 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
1307 dma_maps->dmamap_xfer->dm_mapsize,
1308 (read) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1309
1310 WDCDEBUG_PRINT(("pciide_dma_init: %d segs for %p len %d (phy 0x%x)\n",
1311 dma_maps->dmamap_xfer->dm_nsegs, databuf, datalen,
1312 vtophys(databuf)), DEBUG_DMA|DEBUG_XFERS);
1313 for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
1314 #ifdef DIAGNOSTIC
1315 /* A segment must not cross a 64k boundary */
1316 {
1317 u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
1318 u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
1319 if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
1320 ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
1321 printf("pciide_dma: segment %d physical addr 0x%lx"
1322 " len 0x%lx not properly aligned\n",
1323 seg, phys, len);
1324 panic("pciide_dma: buf align");
1325 }
1326 }
1327 #endif
1328 dma_maps->dma_table[seg].base_addr =
1329 dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
1330 dma_maps->dma_table[seg].byte_count =
1331 dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
1332 IDEDMA_BYTE_COUNT_MASK;
1333 WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
1334 seg, dma_maps->dma_table[seg].byte_count,
1335 dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
1336
1337 }
1338 dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
1339 IDEDMA_BYTE_COUNT_EOT;
1340
1341 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
1342 dma_maps->dmamap_table->dm_mapsize,
1343 BUS_DMASYNC_PREWRITE);
1344
1345 /* Maps are ready. Start DMA function */
1346 #ifdef DIAGNOSTIC
1347 if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
1348 printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
1349 dma_maps->dmamap_table->dm_segs[0].ds_addr);
1350 panic("pciide_dma_init: table align");
1351 }
1352 #endif
1353
1354 WDCDEBUG_PRINT(("phy addr of table at %p = 0x%lx len %ld (%d segs, "
1355 "phys 0x%x)\n",
1356 dma_maps->dma_table,
1357 dma_maps->dmamap_table->dm_segs[0].ds_addr,
1358 dma_maps->dmamap_table->dm_segs[0].ds_len,
1359 dma_maps->dmamap_table->dm_nsegs,
1360 vtophys(dma_maps->dma_table)), DEBUG_DMA);
1361 /* Clear status bits */
1362 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1363 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
1364 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1365 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
1366 /* Write table addr */
1367 bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
1368 IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
1369 dma_maps->dmamap_table->dm_segs[0].ds_addr);
1370 /* set read/write */
1371 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1372 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
1373 (read) ? IDEDMA_CMD_WRITE: 0);
1374 return 0;
1375 }
1376
1377 void
1378 pciide_dma_start(v, channel, drive, read)
1379 void *v;
1380 int channel, drive;
1381 {
1382 struct pciide_softc *sc = v;
1383
1384 WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
1385 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1386 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
1387 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1388 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
1389 }
1390
1391 int
1392 pciide_dma_finish(v, channel, drive, read)
1393 void *v;
1394 int channel, drive;
1395 int read;
1396 {
1397 struct pciide_softc *sc = v;
1398 u_int8_t status;
1399 struct pciide_dma_maps *dma_maps =
1400 &sc->pciide_channels[channel].dma_maps[drive];
1401
1402 /* Unload the map of the data buffer */
1403 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
1404 dma_maps->dmamap_xfer->dm_mapsize,
1405 (read) ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1406 bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
1407
1408 status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1409 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
1410 WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
1411 DEBUG_XFERS);
1412
1413 /* stop DMA channel */
1414 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1415 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
1416 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1417 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
1418
1419 /* Clear status bits */
1420 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1421 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
1422 status);
1423
1424 if ((status & (IDEDMA_CTL_INTR | IDEDMA_CTL_ERR | IDEDMA_CTL_ACT)) !=
1425 IDEDMA_CTL_INTR) {
1426 printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
1427 sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
1428 return 1;
1429 }
1430 return 0;
1431 }
1432