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