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