pciide.c revision 1.10 1 /* $NetBSD: pciide.c,v 1.10 1998/10/13 08:59:46 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 DEBUG_DMA 0x01
46 #define DEBUG_XFERS 0x02
47 #define DEBUG_FUNCS 0x08
48 #define DEBUG_PROBE 0x10
49 #ifdef WDCDEBUG
50 int wdcdebug_pciide_mask = DEBUG_PROBE;
51 #define WDCDEBUG_PRINT(args, level) \
52 if (wdcdebug_pciide_mask & (level)) printf args
53 #else
54 #define WDCDEBUG_PRINT(args, level)
55 #endif
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/device.h>
59 #include <sys/malloc.h>
60
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/vm_kern.h>
64
65 #include <dev/pci/pcireg.h>
66 #include <dev/pci/pcivar.h>
67 #include <dev/pci/pcidevs.h>
68 #include <dev/pci/pciidereg.h>
69 #include <dev/pci/pciidevar.h>
70 #include <dev/pci/pciide_piix_reg.h>
71 #include <dev/pci/pciide_apollo_reg.h>
72 #include <dev/pci/pciide_cmd_reg.h>
73 #include <dev/ata/atavar.h>
74 #include <dev/ic/wdcreg.h>
75 #include <dev/ic/wdcvar.h>
76
77 struct pciide_softc {
78 struct wdc_softc sc_wdcdev; /* common wdc definitions */
79
80 void *sc_pci_ih; /* PCI interrupt handle */
81 int sc_dma_ok; /* bus-master DMA info */
82 bus_space_tag_t sc_dma_iot;
83 bus_space_handle_t sc_dma_ioh;
84 bus_dma_tag_t sc_dmat;
85 /* Chip description */
86 const struct pciide_product_desc *sc_pp;
87 /* common definitions */
88 struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
89 /* internal bookkeeping */
90 struct pciide_channel { /* per-channel data */
91 int hw_ok; /* hardware mapped & OK? */
92 int compat; /* is it compat? */
93 void *ih; /* compat or pci handle */
94 /* DMA tables and DMA map for xfer, for each drive */
95 struct pciide_dma_maps {
96 bus_dmamap_t dmamap_table;
97 struct idedma_table *dma_table;
98 bus_dmamap_t dmamap_xfer;
99 } dma_maps[2];
100 } pciide_channels[PCIIDE_NUM_CHANNELS];
101 };
102
103 void default_setup_cap __P((struct pciide_softc*));
104 void default_setup_chip __P((struct pciide_softc*,
105 pci_chipset_tag_t, pcitag_t));
106 const char *default_channel_probe __P((struct pciide_softc *,
107 struct pci_attach_args *, int));
108 int default_channel_disable __P((struct pciide_softc *,
109 struct pci_attach_args *, int));
110
111
112 void piix_setup_cap __P((struct pciide_softc*));
113 void piix_setup_chip __P((struct pciide_softc*,
114 pci_chipset_tag_t, pcitag_t));
115 void piix3_4_setup_chip __P((struct pciide_softc*,
116 pci_chipset_tag_t, pcitag_t));
117 const char *piix_channel_probe __P((struct pciide_softc *,
118 struct pci_attach_args *, int));
119 int piix_channel_disable __P((struct pciide_softc *,
120 struct pci_attach_args *, int));
121 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
122 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
123 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
124
125 void apollo_setup_cap __P((struct pciide_softc*));
126 void apollo_setup_chip __P((struct pciide_softc*,
127 pci_chipset_tag_t, pcitag_t));
128 const char *apollo_channel_probe __P((struct pciide_softc *,
129 struct pci_attach_args *, int));
130 int apollo_channel_disable __P((struct pciide_softc *,
131 struct pci_attach_args *, int));
132
133 const char *cmd_channel_probe __P((struct pciide_softc *,
134 struct pci_attach_args *, int));
135 int cmd_channel_disable __P((struct pciide_softc *,
136 struct pci_attach_args *, int));
137
138 int pciide_dma_table_setup __P((struct pciide_softc*, int, int));
139 int pciide_dma_init __P((void*, int, int, void *, size_t, int));
140 void pciide_dma_start __P((void*, int, int, int));
141 int pciide_dma_finish __P((void*, int, int, int));
142
143 struct pciide_product_desc {
144 u_int32_t ide_product;
145 int ide_flags;
146 const char *ide_name;
147 /* init controller's capabilities for drives probe */
148 void (*setup_cap) __P((struct pciide_softc*));
149 /* init controller after drives probe */
150 void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
151 /* Probe for compat channel enabled/disabled */
152 const char * (*channel_probe) __P((struct pciide_softc *,
153 struct pci_attach_args *, int));
154 int (*channel_disable) __P((struct pciide_softc *,
155 struct pci_attach_args *, int));
156 };
157
158 /* Flags for ide_flags */
159 #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
160 #define ONE_QUEUE 0x02 /* device need serialised access */
161
162 /* Default product description for devices not known from this controller */
163 const struct pciide_product_desc default_product_desc = {
164 0,
165 0,
166 "Generic PCI IDE controller",
167 default_setup_cap,
168 default_setup_chip,
169 default_channel_probe,
170 default_channel_disable
171 };
172
173
174 const struct pciide_product_desc pciide_intel_products[] = {
175 { PCI_PRODUCT_INTEL_82092AA,
176 0,
177 "Intel 82092AA IDE controller",
178 default_setup_cap,
179 default_setup_chip,
180 default_channel_probe,
181 default_channel_disable
182 },
183 { PCI_PRODUCT_INTEL_82371FB_IDE,
184 0,
185 "Intel 82371FB IDE controller (PIIX)",
186 piix_setup_cap,
187 piix_setup_chip,
188 piix_channel_probe,
189 piix_channel_disable
190 },
191 { PCI_PRODUCT_INTEL_82371SB_IDE,
192 0,
193 "Intel 82371SB IDE Interface (PIIX3)",
194 piix_setup_cap,
195 piix3_4_setup_chip,
196 piix_channel_probe,
197 piix_channel_disable
198 },
199 { PCI_PRODUCT_INTEL_82371AB_IDE,
200 0,
201 "Intel 82371AB IDE controller (PIIX4)",
202 piix_setup_cap,
203 piix3_4_setup_chip,
204 piix_channel_probe,
205 piix_channel_disable
206 },
207 { 0,
208 0,
209 NULL,
210 }
211 };
212 const struct pciide_product_desc pciide_cmd_products[] = {
213 { PCI_PRODUCT_CMDTECH_640,
214 ONE_QUEUE | CMD_PCI064x_IOEN,
215 "CMD Technology PCI0640",
216 default_setup_cap,
217 default_setup_chip,
218 cmd_channel_probe,
219 cmd_channel_disable
220 },
221 { 0,
222 0,
223 NULL,
224 }
225 };
226
227 const struct pciide_product_desc pciide_via_products[] = {
228 { PCI_PRODUCT_VIATECH_VT82C586_IDE,
229 0,
230 "VT82C586 (Apollo VP) IDE Controller",
231 apollo_setup_cap,
232 apollo_setup_chip,
233 apollo_channel_probe,
234 apollo_channel_disable
235 },
236 { 0,
237 0,
238 NULL,
239 }
240 };
241
242 struct pciide_vendor_desc {
243 u_int32_t ide_vendor;
244 const struct pciide_product_desc *ide_products;
245 };
246
247 const struct pciide_vendor_desc pciide_vendors[] = {
248 { PCI_VENDOR_INTEL, pciide_intel_products },
249 { PCI_VENDOR_CMDTECH, pciide_cmd_products },
250 { PCI_VENDOR_VIATECH, pciide_via_products },
251 { 0, NULL }
252 };
253
254
255 #define PCIIDE_CHANNEL_NAME(chan) ((chan) == 0 ? "primary" : "secondary")
256
257 int pciide_match __P((struct device *, struct cfdata *, void *));
258 void pciide_attach __P((struct device *, struct device *, void *));
259
260 struct cfattach pciide_ca = {
261 sizeof(struct pciide_softc), pciide_match, pciide_attach
262 };
263
264 int pciide_map_channel_compat __P((struct pciide_softc *,
265 struct pci_attach_args *, int));
266 int pciide_map_channel_native __P((struct pciide_softc *,
267 struct pci_attach_args *, int));
268 int pciide_print __P((void *, const char *pnp));
269 int pciide_compat_intr __P((void *));
270 int pciide_pci_intr __P((void *));
271 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
272
273 const struct pciide_product_desc*
274 pciide_lookup_product(id)
275 u_int32_t id;
276 {
277 const struct pciide_product_desc *pp;
278 const struct pciide_vendor_desc *vp;
279
280 for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
281 if (PCI_VENDOR(id) == vp->ide_vendor)
282 break;
283
284 if ((pp = vp->ide_products) == NULL)
285 return NULL;
286
287 for (; pp->ide_name != NULL; pp++)
288 if (PCI_PRODUCT(id) == pp->ide_product)
289 break;
290
291 if (pp->ide_name == NULL)
292 return NULL;
293 return pp;
294 }
295
296 int
297 pciide_match(parent, match, aux)
298 struct device *parent;
299 struct cfdata *match;
300 void *aux;
301 {
302 struct pci_attach_args *pa = aux;
303
304 /*
305 * Check the ID register to see that it's a PCI IDE controller.
306 * If it is, we assume that we can deal with it; it _should_
307 * work in a standardized way...
308 */
309 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
310 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
311 return (1);
312 }
313
314 return (0);
315 }
316
317 void
318 pciide_attach(parent, self, aux)
319 struct device *parent, *self;
320 void *aux;
321 {
322 struct pci_attach_args *pa = aux;
323 pci_chipset_tag_t pc = pa->pa_pc;
324 pcitag_t tag = pa->pa_tag;
325 struct pciide_softc *sc = (struct pciide_softc *)self;
326 struct pciide_channel *cp;
327 pcireg_t class, interface, csr;
328 pci_intr_handle_t intrhandle;
329 const char *intrstr;
330 char devinfo[256];
331 int i;
332
333 sc->sc_pp = pciide_lookup_product(pa->pa_id);
334 if (sc->sc_pp == NULL) {
335 sc->sc_pp = &default_product_desc;
336 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
337 printf(": %s (rev. 0x%02x)\n", devinfo,
338 PCI_REVISION(pa->pa_class));
339 } else {
340 printf(": %s\n", sc->sc_pp->ide_name);
341 }
342
343 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
344 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
345 /*
346 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
347 * and base adresses registers can be disabled at
348 * hardware level. In this case, the device is wired
349 * in compat mode and its first channel is always enabled,
350 * but we can't rely on PCI_COMMAND_IO_ENABLE.
351 * In fact, it seems that the first channel of the CMD PCI0640
352 * can't be disabled.
353 */
354 if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
355 printf("%s: device disabled (at %s)\n",
356 sc->sc_wdcdev.sc_dev.dv_xname,
357 (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
358 "device" : "bridge");
359 return;
360 }
361 }
362
363 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
364 interface = PCI_INTERFACE(class);
365
366 /*
367 * Set up PCI interrupt only if at last one channel is in native mode.
368 * At last one device (CMD PCI0640) has a default value of 14, which
369 * will be mapped even if both channels are in compat-only mode.
370 */
371 if (interface & (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1))) {
372 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
373 pa->pa_intrline, &intrhandle) != 0) {
374 printf("%s: couldn't map native-PCI interrupt\n",
375 sc->sc_wdcdev.sc_dev.dv_xname);
376 } else {
377 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
378 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
379 intrhandle, IPL_BIO, pciide_pci_intr, sc);
380 if (sc->sc_pci_ih != NULL) {
381 printf("%s: using %s for native-PCI "
382 "interrupt\n",
383 sc->sc_wdcdev.sc_dev.dv_xname,
384 intrstr ? intrstr : "unknown interrupt");
385 } else {
386 printf("%s: couldn't establish native-PCI "
387 "interrupt",
388 sc->sc_wdcdev.sc_dev.dv_xname);
389 if (intrstr != NULL)
390 printf(" at %s", intrstr);
391 printf("\n");
392 }
393 }
394 }
395
396 /*
397 * Map DMA registers, if DMA is supported.
398 *
399 * Note that sc_dma_ok is the right variable to test to see if
400 * DMA can be done. If the interface doesn't support DMA,
401 * sc_dma_ok will never be non-zero. If the DMA regs couldn't
402 * be mapped, it'll be zero. I.e., sc_dma_ok will only be
403 * non-zero if the interface supports DMA and the registers
404 * could be mapped.
405 *
406 * XXX Note that despite the fact that the Bus Master IDE specs
407 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
408 * XXX space," some controllers (at least the United
409 * XXX Microelectronics UM8886BF) place it in memory space.
410 * XXX eventually, we should probably read the register and check
411 * XXX which type it is. Either that or 'quirk' certain devices.
412 */
413 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
414 sc->sc_dma_ok = (pci_mapreg_map(pa,
415 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
416 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
417 sc->sc_dmat = pa->pa_dmat;
418 printf("%s: bus-master DMA support present",
419 sc->sc_wdcdev.sc_dev.dv_xname);
420 if (sc->sc_dma_ok == 0) {
421 printf(", but unused (couldn't map registers)");
422 } else {
423 sc->sc_wdcdev.dma_arg = sc;
424 sc->sc_wdcdev.dma_init = pciide_dma_init;
425 sc->sc_wdcdev.dma_start = pciide_dma_start;
426 sc->sc_wdcdev.dma_finish = pciide_dma_finish;
427 }
428 printf("\n");
429 }
430 sc->sc_pp->setup_cap(sc);
431 sc->sc_wdcdev.channels = sc->wdc_channels;
432 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
433 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
434
435 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
436 cp = &sc->pciide_channels[i];
437
438 sc->wdc_channels[i].channel = i;
439 sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
440 if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
441 sc->wdc_channels[i].ch_queue =
442 sc->wdc_channels[0].ch_queue;
443 } else {
444 sc->wdc_channels[i].ch_queue =
445 malloc(sizeof(struct channel_queue), M_DEVBUF,
446 M_NOWAIT);
447 }
448 if (sc->wdc_channels[i].ch_queue == NULL) {
449 printf("%s %s channel: "
450 "can't allocate memory for command queue",
451 sc->sc_wdcdev.sc_dev.dv_xname,
452 PCIIDE_CHANNEL_NAME(i));
453 continue;
454 }
455 printf("%s: %s channel %s to %s mode\n",
456 sc->sc_wdcdev.sc_dev.dv_xname,
457 PCIIDE_CHANNEL_NAME(i),
458 (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
459 "configured" : "wired",
460 (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
461 "compatibility");
462
463 /*
464 * pciide_map_channel_native() and pciide_map_channel_compat()
465 * will also call wdcattach. Eventually the channel will be
466 * disabled if there's no drive present
467 */
468 if (interface & PCIIDE_INTERFACE_PCI(i))
469 cp->hw_ok = pciide_map_channel_native(sc, pa, i);
470 else
471 cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
472
473 }
474 sc->sc_pp->setup_chip(sc, pc, tag);
475 WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
476 pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
477 }
478
479 int
480 pciide_map_channel_compat(sc, pa, chan)
481 struct pciide_softc *sc;
482 struct pci_attach_args *pa;
483 int chan;
484 {
485 struct pciide_channel *cp = &sc->pciide_channels[chan];
486 struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
487 const char *probe_fail_reason;
488 int rv = 1;
489
490 cp->compat = 1;
491
492 wdc_cp->cmd_iot = pa->pa_iot;
493 if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
494 PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
495 printf("%s: couldn't map %s channel cmd regs\n",
496 sc->sc_wdcdev.sc_dev.dv_xname,
497 PCIIDE_CHANNEL_NAME(chan));
498 rv = 0;
499 }
500
501 wdc_cp->ctl_iot = pa->pa_iot;
502 if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
503 PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
504 printf("%s: couldn't map %s channel ctl regs\n",
505 sc->sc_wdcdev.sc_dev.dv_xname,
506 PCIIDE_CHANNEL_NAME(chan));
507 rv = 0;
508 }
509
510 /*
511 * If we weren't able to map the device successfully,
512 * we just give up now. Something else has already
513 * occupied those ports, indicating that the device has
514 * (probably) been completely disabled (by some nonstandard
515 * mechanism).
516 *
517 * XXX If we successfully map some ports, but not others,
518 * XXX it might make sense to unmap the ones that we mapped.
519 */
520 if (rv == 0)
521 goto out;
522
523 /*
524 * If we were able to map the device successfully, check if
525 * the channel is enabled. For "known" device, a chip-specific
526 * routine will be used (which read the rigth PCI register).
527 * For unknow device, a generic routine using "standart" wdc probe
528 * will try to guess it.
529 *
530 * If the channel has been disabled, other devices are free to use
531 * its ports.
532 */
533 probe_fail_reason = sc->sc_pp->channel_probe(sc, pa, chan);
534 if (probe_fail_reason != NULL) {
535 printf("%s: %s channel ignored (%s)\n",
536 sc->sc_wdcdev.sc_dev.dv_xname,
537 PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
538 rv = 0;
539
540 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
541 PCIIDE_COMPAT_CMD_SIZE);
542 bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
543 PCIIDE_COMPAT_CTL_SIZE);
544
545 goto out;
546 }
547 wdc_cp->data32iot = wdc_cp->cmd_iot;
548 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
549 wdcattach(&sc->wdc_channels[chan]);
550 /*
551 * If drive not present, try to disable the channel and
552 * free the resources.
553 */
554 if ((sc->wdc_channels[chan].ch_drive[0].drive_flags & DRIVE) == 0 &&
555 (sc->wdc_channels[chan].ch_drive[1].drive_flags & DRIVE) == 0) {
556 if (sc->sc_pp->channel_disable(sc, pa, chan)) {
557 printf("%s: disabling %s channel (no drives)\n",
558 sc->sc_wdcdev.sc_dev.dv_xname,
559 PCIIDE_CHANNEL_NAME(chan));
560 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
561 PCIIDE_COMPAT_CMD_SIZE);
562 bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
563 PCIIDE_COMPAT_CTL_SIZE);
564 rv = 0;
565 goto out;
566 }
567 }
568
569 /*
570 * If we're here, we were able to map the device successfully
571 * and it really looks like there's a controller there.
572 *
573 * Unless those conditions are true, we don't map the
574 * compatibility interrupt. The spec indicates that if a
575 * channel is configured for compatibility mode and the PCI
576 * device's I/O space is enabled, the channel will be enabled.
577 * Hoewver, some devices seem to be able to disable invididual
578 * compatibility channels (via non-standard mechanisms). If
579 * the channel is disabled, the interrupt line can (probably)
580 * be used by other devices (and may be assigned to other
581 * devices by the BIOS). If we mapped the interrupt we might
582 * conflict with another interrupt assignment.
583 */
584 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
585 pa, chan, pciide_compat_intr, wdc_cp);
586 if (cp->ih == NULL) {
587 printf("%s: no compatibility interrupt for use by %s channel\n",
588 sc->sc_wdcdev.sc_dev.dv_xname,
589 PCIIDE_CHANNEL_NAME(chan));
590 rv = 0;
591 }
592
593 out:
594 return (rv);
595 }
596
597 int
598 pciide_map_channel_native(sc, pa, chan)
599 struct pciide_softc *sc;
600 struct pci_attach_args *pa;
601 int chan;
602 {
603 struct pciide_channel *cp = &sc->pciide_channels[chan];
604 struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
605 int rv = 1;
606
607 cp->compat = 0;
608
609 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
610 0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
611 printf("%s: couldn't map %s channel cmd regs\n",
612 sc->sc_wdcdev.sc_dev.dv_xname,
613 PCIIDE_CHANNEL_NAME(chan));
614 rv = 0;
615 }
616
617 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
618 0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
619 printf("%s: couldn't map %s channel ctl regs\n",
620 sc->sc_wdcdev.sc_dev.dv_xname,
621 PCIIDE_CHANNEL_NAME(chan));
622 rv = 0;
623 }
624
625 if ((cp->ih = sc->sc_pci_ih) == NULL) {
626 printf("%s: no native-PCI interrupt for use by %s channel\n",
627 sc->sc_wdcdev.sc_dev.dv_xname,
628 PCIIDE_CHANNEL_NAME(chan));
629 rv = 0;
630 }
631 wdc_cp->data32iot = wdc_cp->cmd_iot;
632 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
633 if (rv) {
634 wdcattach(&sc->wdc_channels[chan]);
635 /*
636 * If drive not present, try to disable the channel and
637 * free the resources.
638 */
639 /* XXX How do we unmap regs mapped with pci_mapreg_map ?*/
640 #if 0
641 if ((sc->wdc_channels[chan].ch_drive[0].drive_flags & DRIVE)
642 == 0 &&
643 (sc->wdc_channels[chan].ch_drive[1].drive_flags & DRIVE)
644 == 0) {
645 if (sc->sc_pp->channel_disable(sc, pa, chan)) {
646 printf("%s: disabling %s channel (no drives)\n",
647 sc->sc_wdcdev.sc_dev.dv_xname,
648 PCIIDE_CHANNEL_NAME(chan));
649 pci_mapreg_map(xxx);
650 rv = 0;
651 }
652 }
653 #endif
654 }
655 return (rv);
656 }
657
658 int
659 pciide_compat_intr(arg)
660 void *arg;
661 {
662 struct channel_softc *wdc_cp = arg;
663
664 #ifdef DIAGNOSTIC
665 struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
666 struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
667 /* should only be called for a compat channel */
668 if (cp->compat == 0)
669 panic("pciide compat intr called for non-compat chan %p\n", cp);
670 #endif
671 return (wdcintr(wdc_cp));
672 }
673
674 int
675 pciide_pci_intr(arg)
676 void *arg;
677 {
678 struct pciide_softc *sc = arg;
679 struct pciide_channel *cp;
680 struct channel_softc *wdc_cp;
681 int i, rv, crv;
682
683 rv = 0;
684 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
685 cp = &sc->pciide_channels[i];
686 wdc_cp = &sc->wdc_channels[i];
687
688 /* If a compat channel skip. */
689 if (cp->compat)
690 continue;
691 /* if this channel not waiting for intr, skip */
692 if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
693 continue;
694
695 crv = wdcintr(wdc_cp);
696 if (crv == 0)
697 ; /* leave rv alone */
698 else if (crv == 1)
699 rv = 1; /* claim the intr */
700 else if (rv == 0) /* crv should be -1 in this case */
701 rv = crv; /* if we've done no better, take it */
702 }
703 return (rv);
704 }
705
706 void
707 default_setup_cap(sc)
708 struct pciide_softc *sc;
709 {
710 if (sc->sc_dma_ok)
711 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
712 sc->sc_wdcdev.pio_mode = 0;
713 sc->sc_wdcdev.dma_mode = 0;
714 }
715
716 void
717 default_setup_chip(sc, pc, tag)
718 struct pciide_softc *sc;
719 pci_chipset_tag_t pc;
720 pcitag_t tag;
721 {
722 int channel, drive, idedma_ctl;
723 struct channel_softc *chp;
724 struct ata_drive_datas *drvp;
725
726 if (sc->sc_dma_ok == 0)
727 return; /* nothing to do */
728
729 /* Allocate DMA maps */
730 for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
731 idedma_ctl = 0;
732 chp = &sc->wdc_channels[channel];
733 for (drive = 0; drive < 2; drive++) {
734 drvp = &chp->ch_drive[drive];
735 /* If no drive, skip */
736 if ((drvp->drive_flags & DRIVE) == 0)
737 continue;
738 if ((drvp->drive_flags & DRIVE_DMA) == 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_MODE |
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_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_MODE |
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