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