pciide.c revision 1.33.2.3 1 /* $NetBSD: pciide.c,v 1.33.2.3 1999/05/05 17:13:24 perry Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * PCI IDE controller driver.
35 *
36 * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
37 * sys/dev/pci/ppb.c, revision 1.16).
38 *
39 * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
40 * "Programming Interface for Bus Master IDE Controller, Revision 1.0
41 * 5/16/94" from the PCI SIG.
42 *
43 */
44
45 #define WDCDEBUG
46
47 #define DEBUG_DMA 0x01
48 #define DEBUG_XFERS 0x02
49 #define DEBUG_FUNCS 0x08
50 #define DEBUG_PROBE 0x10
51 #ifdef WDCDEBUG
52 int wdcdebug_pciide_mask = 0;
53 #define WDCDEBUG_PRINT(args, level) \
54 if (wdcdebug_pciide_mask & (level)) printf args
55 #else
56 #define WDCDEBUG_PRINT(args, level)
57 #endif
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcidevs.h>
70 #include <dev/pci/pciidereg.h>
71 #include <dev/pci/pciidevar.h>
72 #include <dev/pci/pciide_piix_reg.h>
73 #include <dev/pci/pciide_apollo_reg.h>
74 #include <dev/pci/pciide_cmd_reg.h>
75 #include <dev/pci/pciide_cy693_reg.h>
76 #include <dev/pci/pciide_sis_reg.h>
77 #include <dev/pci/pciide_acer_reg.h>
78 #include <dev/ata/atavar.h>
79 #include <dev/ic/wdcreg.h>
80 #include <dev/ic/wdcvar.h>
81
82 #if BYTE_ORDER == BIG_ENDIAN
83 #include <machine/bswap.h>
84 #define htopci(x) bswap32(x)
85 #define pcitoh(x) bswap32(x)
86 #else
87 #define htopci(x) (x)
88 #define pcitoh(x) (x)
89 #endif
90
91 /* inlines for reading/writing 8-bit PCI registers */
92 static __inline u_int8_t pciide_pci_read __P((pci_chipset_tag_t, pcitag_t,
93 int));
94 static __inline u_int8_t
95 pciide_pci_read(pc, pa, reg)
96 pci_chipset_tag_t pc;
97 pcitag_t pa;
98 int reg;
99 {
100 return (
101 pci_conf_read(pc, pa, (reg & ~0x03)) >> ((reg & 0x03) * 8) & 0xff);
102 }
103
104
105 static __inline void pciide_pci_write __P((pci_chipset_tag_t, pcitag_t,
106 int, u_int8_t));
107 static __inline void
108 pciide_pci_write(pc, pa, reg, val)
109 pci_chipset_tag_t pc;
110 pcitag_t pa;
111 int reg;
112 u_int8_t val;
113 {
114 pcireg_t pcival;
115
116 pcival = pci_conf_read(pc, pa, (reg & ~0x03));
117 pcival &= ~(0xff << ((reg & 0x03) * 8));
118 pcival |= (val << ((reg & 0x03) * 8));
119 pci_conf_write(pc, pa, (reg & ~0x03), pcival);
120 }
121
122 struct pciide_softc {
123 struct wdc_softc sc_wdcdev; /* common wdc definitions */
124 pci_chipset_tag_t sc_pc; /* PCI registers info */
125 pcitag_t sc_tag;
126 void *sc_pci_ih; /* PCI interrupt handle */
127 int sc_dma_ok; /* bus-master DMA info */
128 bus_space_tag_t sc_dma_iot;
129 bus_space_handle_t sc_dma_ioh;
130 bus_dma_tag_t sc_dmat;
131 /* Chip description */
132 const struct pciide_product_desc *sc_pp;
133 /* common definitions */
134 struct channel_softc *wdc_chanarray[PCIIDE_NUM_CHANNELS];
135 /* internal bookkeeping */
136 struct pciide_channel { /* per-channel data */
137 struct channel_softc wdc_channel; /* generic part */
138 char *name;
139 int hw_ok; /* hardware mapped & OK? */
140 int compat; /* is it compat? */
141 void *ih; /* compat or pci handle */
142 /* DMA tables and DMA map for xfer, for each drive */
143 struct pciide_dma_maps {
144 bus_dmamap_t dmamap_table;
145 struct idedma_table *dma_table;
146 bus_dmamap_t dmamap_xfer;
147 } dma_maps[2];
148 } pciide_channels[PCIIDE_NUM_CHANNELS];
149 };
150
151 void default_setup_cap __P((struct pciide_softc*));
152 void default_setup_chip __P((struct pciide_softc*));
153 void default_channel_map __P((struct pci_attach_args *,
154 struct pciide_channel *));
155
156 void piix_setup_cap __P((struct pciide_softc*));
157 void piix_setup_chip __P((struct pciide_softc*));
158 void piix_setup_channel __P((struct channel_softc*));
159 void piix3_4_setup_chip __P((struct pciide_softc*));
160 void piix3_4_setup_channel __P((struct channel_softc*));
161 void piix_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
162 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
163 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
164 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
165
166 void apollo_setup_cap __P((struct pciide_softc*));
167 void apollo_setup_chip __P((struct pciide_softc*));
168 void apollo_setup_channel __P((struct channel_softc*));
169 void apollo_channel_map __P((struct pci_attach_args *,
170 struct pciide_channel *));
171
172 void cmd0643_6_setup_cap __P((struct pciide_softc*));
173 void cmd0643_6_setup_chip __P((struct pciide_softc*));
174 void cmd0643_6_setup_channel __P((struct channel_softc*));
175 void cmd_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
176
177 void cy693_setup_cap __P((struct pciide_softc*));
178 void cy693_setup_chip __P((struct pciide_softc*));
179 void cy693_setup_channel __P((struct channel_softc*));
180 void cy693_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
181
182 void sis_setup_cap __P((struct pciide_softc*));
183 void sis_setup_chip __P((struct pciide_softc*));
184 void sis_setup_channel __P((struct channel_softc*));
185 void sis_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
186
187 void acer_setup_cap __P((struct pciide_softc*));
188 void acer_setup_chip __P((struct pciide_softc*));
189 void acer_setup_channel __P((struct channel_softc*));
190 void acer_channel_map __P((struct pci_attach_args *, struct pciide_channel *));
191
192 void pciide_channel_dma_setup __P((struct pciide_channel *));
193 int pciide_dma_table_setup __P((struct pciide_softc*, int, int));
194 int pciide_dma_init __P((void*, int, int, void *, size_t, int));
195 void pciide_dma_start __P((void*, int, int, int));
196 int pciide_dma_finish __P((void*, int, int, int));
197 void pciide_print_modes __P((struct pciide_channel *));
198
199 struct pciide_product_desc {
200 u_int32_t ide_product;
201 int ide_flags;
202 int ide_num_channels;
203 const char *ide_name;
204 /* init controller's capabilities for drives probe */
205 void (*setup_cap) __P((struct pciide_softc*));
206 /* init controller after drives probe */
207 void (*setup_chip) __P((struct pciide_softc*));
208 /* map channel if possible/necessary */
209 void (*channel_map) __P((struct pci_attach_args *,
210 struct pciide_channel *));
211 };
212
213 /* Flags for ide_flags */
214 #define CMD_PCI064x_IOEN 0x01 /* CMD-style PCI_COMMAND_IO_ENABLE */
215 #define ONE_QUEUE 0x02 /* device need serialised access */
216
217 /* Default product description for devices not known from this controller */
218 const struct pciide_product_desc default_product_desc = {
219 0,
220 0,
221 PCIIDE_NUM_CHANNELS,
222 "Generic PCI IDE controller",
223 default_setup_cap,
224 default_setup_chip,
225 default_channel_map
226 };
227
228
229 const struct pciide_product_desc pciide_intel_products[] = {
230 { PCI_PRODUCT_INTEL_82092AA,
231 0,
232 PCIIDE_NUM_CHANNELS,
233 "Intel 82092AA IDE controller",
234 default_setup_cap,
235 default_setup_chip,
236 default_channel_map
237 },
238 { PCI_PRODUCT_INTEL_82371FB_IDE,
239 0,
240 PCIIDE_NUM_CHANNELS,
241 "Intel 82371FB IDE controller (PIIX)",
242 piix_setup_cap,
243 piix_setup_chip,
244 piix_channel_map
245 },
246 { PCI_PRODUCT_INTEL_82371SB_IDE,
247 0,
248 PCIIDE_NUM_CHANNELS,
249 "Intel 82371SB IDE Interface (PIIX3)",
250 piix_setup_cap,
251 piix3_4_setup_chip,
252 piix_channel_map
253 },
254 { PCI_PRODUCT_INTEL_82371AB_IDE,
255 0,
256 PCIIDE_NUM_CHANNELS,
257 "Intel 82371AB IDE controller (PIIX4)",
258 piix_setup_cap,
259 piix3_4_setup_chip,
260 piix_channel_map
261 },
262 { 0,
263 0,
264 0,
265 NULL,
266 }
267 };
268 const struct pciide_product_desc pciide_cmd_products[] = {
269 { PCI_PRODUCT_CMDTECH_640,
270 ONE_QUEUE | CMD_PCI064x_IOEN,
271 PCIIDE_NUM_CHANNELS,
272 "CMD Technology PCI0640",
273 default_setup_cap,
274 default_setup_chip,
275 cmd_channel_map
276 },
277 { PCI_PRODUCT_CMDTECH_643,
278 ONE_QUEUE | CMD_PCI064x_IOEN,
279 PCIIDE_NUM_CHANNELS,
280 "CMD Technology PCI0643",
281 cmd0643_6_setup_cap,
282 cmd0643_6_setup_chip,
283 cmd_channel_map
284 },
285 { PCI_PRODUCT_CMDTECH_646,
286 ONE_QUEUE | CMD_PCI064x_IOEN,
287 PCIIDE_NUM_CHANNELS,
288 "CMD Technology PCI0646",
289 cmd0643_6_setup_cap,
290 cmd0643_6_setup_chip,
291 cmd_channel_map
292 },
293 { 0,
294 0,
295 0,
296 NULL,
297 }
298 };
299
300 const struct pciide_product_desc pciide_via_products[] = {
301 { PCI_PRODUCT_VIATECH_VT82C586_IDE,
302 0,
303 PCIIDE_NUM_CHANNELS,
304 "VIA Technologies VT82C586 (Apollo VP) IDE Controller",
305 apollo_setup_cap,
306 apollo_setup_chip,
307 apollo_channel_map
308 },
309 { PCI_PRODUCT_VIATECH_VT82C586A_IDE,
310 0,
311 PCIIDE_NUM_CHANNELS,
312 "VIA Technologies VT82C586A IDE Controller",
313 apollo_setup_cap,
314 apollo_setup_chip,
315 apollo_channel_map
316 },
317 { 0,
318 0,
319 0,
320 NULL,
321 }
322 };
323
324 const struct pciide_product_desc pciide_cypress_products[] = {
325 { PCI_PRODUCT_CONTAQ_82C693,
326 0,
327 1,
328 "Contaq Microsystems CY82C693 IDE Controller",
329 cy693_setup_cap,
330 cy693_setup_chip,
331 cy693_channel_map
332 },
333 { 0,
334 0,
335 0,
336 NULL,
337 }
338 };
339
340 const struct pciide_product_desc pciide_sis_products[] = {
341 { PCI_PRODUCT_SIS_5597_IDE,
342 0,
343 PCIIDE_NUM_CHANNELS,
344 "Silicon Integrated System 5597/5598 IDE controller",
345 sis_setup_cap,
346 sis_setup_chip,
347 sis_channel_map
348 },
349 { 0,
350 0,
351 0,
352 NULL,
353 }
354 };
355
356 const struct pciide_product_desc pciide_acer_products[] = {
357 { PCI_PRODUCT_ALI_M5229,
358 0,
359 PCIIDE_NUM_CHANNELS,
360 "Acer Labs M5229 UDMA IDE Controller",
361 acer_setup_cap,
362 acer_setup_chip,
363 acer_channel_map
364 },
365 { 0,
366 0,
367 0,
368 NULL,
369 }
370 };
371
372 struct pciide_vendor_desc {
373 u_int32_t ide_vendor;
374 const struct pciide_product_desc *ide_products;
375 };
376
377 const struct pciide_vendor_desc pciide_vendors[] = {
378 { PCI_VENDOR_INTEL, pciide_intel_products },
379 { PCI_VENDOR_CMDTECH, pciide_cmd_products },
380 { PCI_VENDOR_VIATECH, pciide_via_products },
381 { PCI_VENDOR_CONTAQ, pciide_cypress_products },
382 { PCI_VENDOR_SIS, pciide_sis_products },
383 { PCI_VENDOR_ALI, pciide_acer_products },
384 { 0, NULL }
385 };
386
387
388 #define PCIIDE_CHANNEL_NAME(chan) ((chan) == 0 ? "primary" : "secondary")
389
390 /* options passed via the 'flags' config keyword */
391 #define PCIIDE_OPTIONS_DMA 0x01
392
393 int pciide_match __P((struct device *, struct cfdata *, void *));
394 void pciide_attach __P((struct device *, struct device *, void *));
395
396 struct cfattach pciide_ca = {
397 sizeof(struct pciide_softc), pciide_match, pciide_attach
398 };
399
400 int pciide_mapregs_compat __P(( struct pci_attach_args *,
401 struct pciide_channel *, int, bus_size_t *, bus_size_t*));
402 int pciide_mapregs_native __P((struct pci_attach_args *,
403 struct pciide_channel *, bus_size_t *, bus_size_t *));
404 void pciide_mapchan __P((struct pci_attach_args *,
405 struct pciide_channel *, int, bus_size_t *, bus_size_t *));
406 int pciiide_chan_candisable __P((struct pciide_channel *));
407 void pciide_map_compat_intr __P(( struct pci_attach_args *,
408 struct pciide_channel *, int, int));
409 int pciide_print __P((void *, const char *pnp));
410 int pciide_compat_intr __P((void *));
411 int pciide_pci_intr __P((void *));
412 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
413
414 const struct pciide_product_desc*
415 pciide_lookup_product(id)
416 u_int32_t id;
417 {
418 const struct pciide_product_desc *pp;
419 const struct pciide_vendor_desc *vp;
420
421 for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
422 if (PCI_VENDOR(id) == vp->ide_vendor)
423 break;
424
425 if ((pp = vp->ide_products) == NULL)
426 return NULL;
427
428 for (; pp->ide_name != NULL; pp++)
429 if (PCI_PRODUCT(id) == pp->ide_product)
430 break;
431
432 if (pp->ide_name == NULL)
433 return NULL;
434 return pp;
435 }
436
437 int
438 pciide_match(parent, match, aux)
439 struct device *parent;
440 struct cfdata *match;
441 void *aux;
442 {
443 struct pci_attach_args *pa = aux;
444
445 /*
446 * Check the ID register to see that it's a PCI IDE controller.
447 * If it is, we assume that we can deal with it; it _should_
448 * work in a standardized way...
449 */
450 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
451 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
452 return (1);
453 }
454
455 return (0);
456 }
457
458 void
459 pciide_attach(parent, self, aux)
460 struct device *parent, *self;
461 void *aux;
462 {
463 struct pci_attach_args *pa = aux;
464 pci_chipset_tag_t pc = pa->pa_pc;
465 pcitag_t tag = pa->pa_tag;
466 struct pciide_softc *sc = (struct pciide_softc *)self;
467 struct pciide_channel *cp;
468 pcireg_t class, interface, csr;
469 char devinfo[256];
470 int i;
471
472 sc->sc_pp = pciide_lookup_product(pa->pa_id);
473 if (sc->sc_pp == NULL) {
474 sc->sc_pp = &default_product_desc;
475 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
476 printf(": %s (rev. 0x%02x)\n", devinfo,
477 PCI_REVISION(pa->pa_class));
478 } else {
479 printf(": %s\n", sc->sc_pp->ide_name);
480 }
481
482 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
483 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
484 /*
485 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
486 * and base adresses registers can be disabled at
487 * hardware level. In this case, the device is wired
488 * in compat mode and its first channel is always enabled,
489 * but we can't rely on PCI_COMMAND_IO_ENABLE.
490 * In fact, it seems that the first channel of the CMD PCI0640
491 * can't be disabled.
492 */
493 #ifndef PCIIDE_CMD064x_DISABLE
494 if ((sc->sc_pp->ide_flags & CMD_PCI064x_IOEN) == 0) {
495 #else
496 if (1) {
497 #endif
498 printf("%s: device disabled (at %s)\n",
499 sc->sc_wdcdev.sc_dev.dv_xname,
500 (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
501 "device" : "bridge");
502 return;
503 }
504 }
505
506 sc->sc_pc = pa->pa_pc;
507 sc->sc_tag = pa->pa_tag;
508
509 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
510 interface = PCI_INTERFACE(class);
511
512 /*
513 * Map DMA registers, if DMA is supported.
514 *
515 * Note that sc_dma_ok is the right variable to test to see if
516 * DMA can be done. If the interface doesn't support DMA,
517 * sc_dma_ok will never be non-zero. If the DMA regs couldn't
518 * be mapped, it'll be zero. I.e., sc_dma_ok will only be
519 * non-zero if the interface supports DMA and the registers
520 * could be mapped.
521 *
522 * XXX Note that despite the fact that the Bus Master IDE specs
523 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
524 * XXX space," some controllers (at least the United
525 * XXX Microelectronics UM8886BF) place it in memory space.
526 * XXX eventually, we should probably read the register and check
527 * XXX which type it is. Either that or 'quirk' certain devices.
528 */
529 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
530 printf("%s: bus-master DMA support present",
531 sc->sc_wdcdev.sc_dev.dv_xname);
532 if (sc->sc_pp == &default_product_desc &&
533 (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
534 PCIIDE_OPTIONS_DMA) == 0) {
535 printf(", but unused (no driver support)");
536 sc->sc_dma_ok = 0;
537 } else {
538 sc->sc_dma_ok = (pci_mapreg_map(pa,
539 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
540 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
541 sc->sc_dmat = pa->pa_dmat;
542 if (sc->sc_dma_ok == 0) {
543 printf(", but unused (couldn't map registers)");
544 } else {
545 if (sc->sc_pp == &default_product_desc)
546 printf(", used without full driver "
547 "support");
548 sc->sc_wdcdev.dma_arg = sc;
549 sc->sc_wdcdev.dma_init = pciide_dma_init;
550 sc->sc_wdcdev.dma_start = pciide_dma_start;
551 sc->sc_wdcdev.dma_finish = pciide_dma_finish;
552 }
553 }
554 } else {
555 printf("%s: hardware does not support DMA",
556 sc->sc_wdcdev.sc_dev.dv_xname);
557 }
558 printf("\n");
559 sc->sc_pp->setup_cap(sc);
560 sc->sc_wdcdev.channels = sc->wdc_chanarray;
561 sc->sc_wdcdev.nchannels = sc->sc_pp->ide_num_channels;;
562 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
563
564 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
565 cp = &sc->pciide_channels[i];
566 sc->wdc_chanarray[i] = &cp->wdc_channel;
567
568 cp->name = PCIIDE_CHANNEL_NAME(i);
569
570 cp->wdc_channel.channel = i;
571 cp->wdc_channel.wdc = &sc->sc_wdcdev;
572 if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
573 cp->wdc_channel.ch_queue =
574 sc->pciide_channels[0].wdc_channel.ch_queue;
575 } else {
576 cp->wdc_channel.ch_queue =
577 malloc(sizeof(struct channel_queue), M_DEVBUF,
578 M_NOWAIT);
579 }
580 if (cp->wdc_channel.ch_queue == NULL) {
581 printf("%s %s channel: "
582 "can't allocate memory for command queue",
583 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
584 continue;
585 }
586 printf("%s: %s channel %s to %s mode\n",
587 sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
588 (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
589 "configured" : "wired",
590 (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
591 "compatibility");
592
593 /*
594 * sc->sc_pp->channel_map() will also call wdcattach.
595 * Eventually the channel will be disabled if there's no
596 * drive present. sc->hw_ok will be updated accordingly.
597 */
598 sc->sc_pp->channel_map(pa, cp);
599
600 }
601 /* Now that all drives are know, setup DMA, etc ...*/
602 sc->sc_pp->setup_chip(sc);
603 if (sc->sc_dma_ok) {
604 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
605 csr |= PCI_COMMAND_MASTER_ENABLE;
606 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
607 }
608 WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
609 pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
610 }
611
612 int
613 pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
614 struct pci_attach_args *pa;
615 struct pciide_channel *cp;
616 int compatchan;
617 bus_size_t *cmdsizep, *ctlsizep;
618 {
619 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
620 struct channel_softc *wdc_cp = &cp->wdc_channel;
621 int rv = 1;
622
623 cp->compat = 1;
624 *cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
625 *ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
626
627 wdc_cp->cmd_iot = pa->pa_iot;
628 if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
629 PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
630 printf("%s: couldn't map %s channel cmd regs\n",
631 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
632 rv = 0;
633 }
634
635 wdc_cp->ctl_iot = pa->pa_iot;
636 if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
637 PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
638 printf("%s: couldn't map %s channel ctl regs\n",
639 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
640 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
641 PCIIDE_COMPAT_CMD_SIZE);
642 rv = 0;
643 }
644
645 return (rv);
646 }
647
648 int
649 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep)
650 struct pci_attach_args * pa;
651 struct pciide_channel *cp;
652 bus_size_t *cmdsizep, *ctlsizep;
653 {
654 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
655 struct channel_softc *wdc_cp = &cp->wdc_channel;
656 const char *intrstr;
657 pci_intr_handle_t intrhandle;
658
659 cp->compat = 0;
660
661 if (sc->sc_pci_ih == NULL) {
662 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
663 pa->pa_intrline, &intrhandle) != 0) {
664 printf("%s: couldn't map native-PCI interrupt\n",
665 sc->sc_wdcdev.sc_dev.dv_xname);
666 return 0;
667 }
668 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
669 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
670 intrhandle, IPL_BIO, pciide_pci_intr, sc);
671 if (sc->sc_pci_ih != NULL) {
672 printf("%s: using %s for native-PCI interrupt\n",
673 sc->sc_wdcdev.sc_dev.dv_xname,
674 intrstr ? intrstr : "unknown interrupt");
675 } else {
676 printf("%s: couldn't establish native-PCI interrupt",
677 sc->sc_wdcdev.sc_dev.dv_xname);
678 if (intrstr != NULL)
679 printf(" at %s", intrstr);
680 printf("\n");
681 return 0;
682 }
683 }
684 cp->ih = sc->sc_pci_ih;
685 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->channel),
686 PCI_MAPREG_TYPE_IO, 0,
687 &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, cmdsizep) != 0) {
688 printf("%s: couldn't map %s channel cmd regs\n",
689 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
690 return 0;
691 }
692
693 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->channel),
694 PCI_MAPREG_TYPE_IO, 0,
695 &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, ctlsizep) != 0) {
696 printf("%s: couldn't map %s channel ctl regs\n",
697 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
698 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
699 return 0;
700 }
701 return (1);
702 }
703
704 int
705 pciide_compat_intr(arg)
706 void *arg;
707 {
708 struct pciide_channel *cp = arg;
709
710 #ifdef DIAGNOSTIC
711 /* should only be called for a compat channel */
712 if (cp->compat == 0)
713 panic("pciide compat intr called for non-compat chan %p\n", cp);
714 #endif
715 return (wdcintr(&cp->wdc_channel));
716 }
717
718 int
719 pciide_pci_intr(arg)
720 void *arg;
721 {
722 struct pciide_softc *sc = arg;
723 struct pciide_channel *cp;
724 struct channel_softc *wdc_cp;
725 int i, rv, crv;
726
727 rv = 0;
728 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
729 cp = &sc->pciide_channels[i];
730 wdc_cp = &cp->wdc_channel;
731
732 /* If a compat channel skip. */
733 if (cp->compat)
734 continue;
735 /* if this channel not waiting for intr, skip */
736 if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
737 continue;
738
739 crv = wdcintr(wdc_cp);
740 if (crv == 0)
741 ; /* leave rv alone */
742 else if (crv == 1)
743 rv = 1; /* claim the intr */
744 else if (rv == 0) /* crv should be -1 in this case */
745 rv = crv; /* if we've done no better, take it */
746 }
747 return (rv);
748 }
749
750 void
751 pciide_channel_dma_setup(cp)
752 struct pciide_channel *cp;
753 {
754 int drive;
755 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
756 struct ata_drive_datas *drvp;
757
758 for (drive = 0; drive < 2; drive++) {
759 drvp = &cp->wdc_channel.ch_drive[drive];
760 /* If no drive, skip */
761 if ((drvp->drive_flags & DRIVE) == 0)
762 continue;
763 /* setup DMA if needed */
764 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
765 (drvp->drive_flags & DRIVE_UDMA) == 0) ||
766 sc->sc_dma_ok == 0) {
767 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
768 continue;
769 }
770 if (pciide_dma_table_setup(sc, cp->wdc_channel.channel, drive)
771 != 0) {
772 /* Abort DMA setup */
773 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
774 continue;
775 }
776 }
777 }
778
779 int
780 pciide_dma_table_setup(sc, channel, drive)
781 struct pciide_softc *sc;
782 int channel, drive;
783 {
784 bus_dma_segment_t seg;
785 int error, rseg;
786 const bus_size_t dma_table_size =
787 sizeof(struct idedma_table) * NIDEDMA_TABLES;
788 struct pciide_dma_maps *dma_maps =
789 &sc->pciide_channels[channel].dma_maps[drive];
790
791 /* If table was already allocated, just return */
792 if (dma_maps->dma_table)
793 return 0;
794
795 /* Allocate memory for the DMA tables and map it */
796 if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
797 IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
798 BUS_DMA_NOWAIT)) != 0) {
799 printf("%s:%d: unable to allocate table DMA for "
800 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
801 channel, drive, error);
802 return error;
803 }
804 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
805 dma_table_size,
806 (caddr_t *)&dma_maps->dma_table,
807 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
808 printf("%s:%d: unable to map table DMA for"
809 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
810 channel, drive, error);
811 return error;
812 }
813 WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
814 "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
815 seg.ds_addr), DEBUG_PROBE);
816
817 /* Create and load table DMA map for this disk */
818 if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
819 1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
820 &dma_maps->dmamap_table)) != 0) {
821 printf("%s:%d: unable to create table DMA map for "
822 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
823 channel, drive, error);
824 return error;
825 }
826 if ((error = bus_dmamap_load(sc->sc_dmat,
827 dma_maps->dmamap_table,
828 dma_maps->dma_table,
829 dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
830 printf("%s:%d: unable to load table DMA map for "
831 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
832 channel, drive, error);
833 return error;
834 }
835 WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
836 dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
837 /* Create a xfer DMA map for this drive */
838 if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
839 NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
840 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
841 &dma_maps->dmamap_xfer)) != 0) {
842 printf("%s:%d: unable to create xfer DMA map for "
843 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
844 channel, drive, error);
845 return error;
846 }
847 return 0;
848 }
849
850 int
851 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
852 void *v;
853 int channel, drive;
854 void *databuf;
855 size_t datalen;
856 int flags;
857 {
858 struct pciide_softc *sc = v;
859 int error, seg;
860 struct pciide_dma_maps *dma_maps =
861 &sc->pciide_channels[channel].dma_maps[drive];
862
863 error = bus_dmamap_load(sc->sc_dmat,
864 dma_maps->dmamap_xfer,
865 databuf, datalen, NULL, BUS_DMA_NOWAIT);
866 if (error) {
867 printf("%s:%d: unable to load xfer DMA map for"
868 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
869 channel, drive, error);
870 return error;
871 }
872
873 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
874 dma_maps->dmamap_xfer->dm_mapsize,
875 (flags & WDC_DMA_READ) ?
876 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
877
878 for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
879 #ifdef DIAGNOSTIC
880 /* A segment must not cross a 64k boundary */
881 {
882 u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
883 u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
884 if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
885 ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
886 printf("pciide_dma: segment %d physical addr 0x%lx"
887 " len 0x%lx not properly aligned\n",
888 seg, phys, len);
889 panic("pciide_dma: buf align");
890 }
891 }
892 #endif
893 dma_maps->dma_table[seg].base_addr =
894 htopci(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
895 dma_maps->dma_table[seg].byte_count =
896 htopci(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
897 IDEDMA_BYTE_COUNT_MASK);
898 WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
899 seg, pcitoh(dma_maps->dma_table[seg].byte_count),
900 pcitoh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
901
902 }
903 dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
904 htopci(IDEDMA_BYTE_COUNT_EOT);
905
906 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
907 dma_maps->dmamap_table->dm_mapsize,
908 BUS_DMASYNC_PREWRITE);
909
910 /* Maps are ready. Start DMA function */
911 #ifdef DIAGNOSTIC
912 if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
913 printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
914 dma_maps->dmamap_table->dm_segs[0].ds_addr);
915 panic("pciide_dma_init: table align");
916 }
917 #endif
918
919 /* Clear status bits */
920 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
921 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
922 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
923 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
924 /* Write table addr */
925 bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
926 IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
927 dma_maps->dmamap_table->dm_segs[0].ds_addr);
928 /* set read/write */
929 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
930 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
931 (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
932 return 0;
933 }
934
935 void
936 pciide_dma_start(v, channel, drive, flags)
937 void *v;
938 int channel, drive, flags;
939 {
940 struct pciide_softc *sc = v;
941
942 WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
943 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
944 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
945 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
946 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
947 }
948
949 int
950 pciide_dma_finish(v, channel, drive, flags)
951 void *v;
952 int channel, drive;
953 int flags;
954 {
955 struct pciide_softc *sc = v;
956 u_int8_t status;
957 struct pciide_dma_maps *dma_maps =
958 &sc->pciide_channels[channel].dma_maps[drive];
959
960 /* Unload the map of the data buffer */
961 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
962 dma_maps->dmamap_xfer->dm_mapsize,
963 (flags & WDC_DMA_READ) ?
964 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
965 bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
966
967 status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
968 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
969 WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
970 DEBUG_XFERS);
971
972 /* stop DMA channel */
973 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
974 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
975 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
976 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
977
978 /* Clear status bits */
979 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
980 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
981 status);
982
983 if ((status & IDEDMA_CTL_ERR) != 0) {
984 printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
985 sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
986 return -1;
987 }
988
989 if ((flags & WDC_DMA_POLL) == 0 && (status & IDEDMA_CTL_INTR) == 0) {
990 printf("%s:%d:%d: Bus-Master DMA error: missing interrupt, "
991 "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
992 drive, status);
993 return -1;
994 }
995
996 if ((status & IDEDMA_CTL_ACT) != 0) {
997 /* data underrun, may be a valid condition for ATAPI */
998 return 1;
999 }
1000 return 0;
1001 }
1002
1003 /* some common code used by several chip channel_map */
1004 void
1005 pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep)
1006 struct pci_attach_args *pa;
1007 int interface;
1008 struct pciide_channel *cp;
1009 bus_size_t *cmdsizep, *ctlsizep;
1010 {
1011 struct channel_softc *wdc_cp = &cp->wdc_channel;
1012
1013 if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
1014 cp->hw_ok = pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep);
1015 else
1016 cp->hw_ok = pciide_mapregs_compat(pa, cp,
1017 wdc_cp->channel, cmdsizep, ctlsizep);
1018 if (cp->hw_ok == 0)
1019 return;
1020 wdc_cp->data32iot = wdc_cp->cmd_iot;
1021 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1022 wdcattach(wdc_cp);
1023 }
1024
1025 /*
1026 * Generic code to call to know if a channel can be disabled. Return 1
1027 * if channel can be disabled, 0 if not
1028 */
1029 int
1030 pciiide_chan_candisable(cp)
1031 struct pciide_channel *cp;
1032 {
1033 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1034 struct channel_softc *wdc_cp = &cp->wdc_channel;
1035
1036 if ((wdc_cp->ch_drive[0].drive_flags & DRIVE) == 0 &&
1037 (wdc_cp->ch_drive[1].drive_flags & DRIVE) == 0) {
1038 printf("%s: disabling %s channel (no drives)\n",
1039 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1040 cp->hw_ok = 0;
1041 return 1;
1042 }
1043 return 0;
1044 }
1045
1046 /*
1047 * generic code to map the compat intr if hw_ok=1 and it is a compat channel.
1048 * Set hw_ok=0 on failure
1049 */
1050 void
1051 pciide_map_compat_intr(pa, cp, compatchan, interface)
1052 struct pci_attach_args *pa;
1053 struct pciide_channel *cp;
1054 int compatchan, interface;
1055 {
1056 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1057 struct channel_softc *wdc_cp = &cp->wdc_channel;
1058
1059 if (cp->hw_ok == 0)
1060 return;
1061 if ((interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel)) != 0)
1062 return;
1063
1064 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
1065 pa, compatchan, pciide_compat_intr, cp);
1066 if (cp->ih == NULL) {
1067 printf("%s: no compatibility interrupt for use by %s "
1068 "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1069 cp->hw_ok = 0;
1070 }
1071 }
1072
1073 void
1074 pciide_print_modes(cp)
1075 struct pciide_channel *cp;
1076 {
1077 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1078 int drive;
1079 struct channel_softc *chp;
1080 struct ata_drive_datas *drvp;
1081
1082 chp = &cp->wdc_channel;
1083 for (drive = 0; drive < 2; drive++) {
1084 drvp = &chp->ch_drive[drive];
1085 if ((drvp->drive_flags & DRIVE) == 0)
1086 continue;
1087 printf("%s(%s:%d:%d): using PIO mode %d",
1088 drvp->drv_softc->dv_xname,
1089 sc->sc_wdcdev.sc_dev.dv_xname,
1090 chp->channel, drive, drvp->PIO_mode);
1091 if (drvp->drive_flags & DRIVE_DMA)
1092 printf(", DMA mode %d", drvp->DMA_mode);
1093 if (drvp->drive_flags & DRIVE_UDMA)
1094 printf(", Ultra-DMA mode %d", drvp->UDMA_mode);
1095 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
1096 printf(" (using DMA data transfers)");
1097 printf("\n");
1098 }
1099 }
1100
1101 void
1102 default_setup_cap(sc)
1103 struct pciide_softc *sc;
1104 {
1105 if (sc->sc_dma_ok)
1106 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
1107 sc->sc_wdcdev.PIO_cap = 0;
1108 sc->sc_wdcdev.DMA_cap = 0;
1109 }
1110
1111 void
1112 default_setup_chip(sc)
1113 struct pciide_softc *sc;
1114 {
1115 int channel, drive, idedma_ctl;
1116 struct channel_softc *chp;
1117 struct ata_drive_datas *drvp;
1118
1119 if (sc->sc_dma_ok == 0)
1120 return; /* nothing to do */
1121
1122 /* Allocate DMA maps */
1123 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1124 idedma_ctl = 0;
1125 chp = &sc->pciide_channels[channel].wdc_channel;
1126 for (drive = 0; drive < 2; drive++) {
1127 drvp = &chp->ch_drive[drive];
1128 /* If no drive, skip */
1129 if ((drvp->drive_flags & DRIVE) == 0)
1130 continue;
1131 if ((drvp->drive_flags & DRIVE_DMA) == 0)
1132 continue;
1133 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1134 /* Abort DMA setup */
1135 printf("%s:%d:%d: can't allocate DMA maps, "
1136 "using PIO transfers\n",
1137 sc->sc_wdcdev.sc_dev.dv_xname,
1138 channel, drive);
1139 drvp->drive_flags &= ~DRIVE_DMA;
1140 }
1141 printf("%s:%d:%d: using DMA data tranferts\n",
1142 sc->sc_wdcdev.sc_dev.dv_xname,
1143 channel, drive);
1144 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1145 }
1146 if (idedma_ctl != 0) {
1147 /* Add software bits in status register */
1148 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1149 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1150 idedma_ctl);
1151 }
1152 }
1153
1154 }
1155
1156 void
1157 default_channel_map(pa, cp)
1158 struct pci_attach_args *pa;
1159 struct pciide_channel *cp;
1160 {
1161 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1162 bus_size_t cmdsize, ctlsize;
1163 pcireg_t csr;
1164 const char *failreason = NULL;
1165 struct channel_softc *wdc_cp = &cp->wdc_channel;
1166 int interface =
1167 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1168
1169 if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
1170 cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
1171 else
1172 cp->hw_ok = pciide_mapregs_compat(pa, cp, wdc_cp->channel,
1173 &cmdsize, &ctlsize);
1174 if (cp->hw_ok == 0)
1175 return;
1176
1177 /*
1178 * Check to see if something appears to be there.
1179 */
1180 if (!wdcprobe(wdc_cp)) {
1181 failreason = "not responding; disabled or no drives?";
1182 goto out;
1183 }
1184
1185 /*
1186 * Now, make sure it's actually attributable to this PCI IDE
1187 * channel by trying to access the channel again while the
1188 * PCI IDE controller's I/O space is disabled. (If the
1189 * channel no longer appears to be there, it belongs to
1190 * this controller.) YUCK!
1191 */
1192 csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
1193 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
1194 csr & ~PCI_COMMAND_IO_ENABLE);
1195 if (wdcprobe(wdc_cp))
1196 failreason = "other hardware responding at addresses";
1197 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
1198
1199 out:
1200 if (failreason) {
1201 printf("%s: %s channel ignored (%s)\n",
1202 sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
1203 failreason);
1204 cp->hw_ok = 0;
1205 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, cmdsize);
1206 bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh, ctlsize);
1207 }
1208 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
1209 if (cp->hw_ok) {
1210 wdc_cp->data32iot = wdc_cp->cmd_iot;
1211 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1212 wdcattach(wdc_cp);
1213 }
1214 }
1215
1216 void
1217 piix_setup_cap(sc)
1218 struct pciide_softc *sc;
1219 {
1220 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
1221 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1222 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1223 WDC_CAPABILITY_DMA;
1224 sc->sc_wdcdev.PIO_cap = 4;
1225 sc->sc_wdcdev.DMA_cap = 2;
1226 sc->sc_wdcdev.UDMA_cap = 2;
1227 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371SB_IDE ||
1228 sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
1229 sc->sc_wdcdev.set_modes = piix3_4_setup_channel;
1230 else
1231 sc->sc_wdcdev.set_modes = piix_setup_channel;
1232 }
1233
1234 void
1235 piix_setup_chip(sc)
1236 struct pciide_softc *sc;
1237 {
1238 u_int8_t channel;
1239
1240
1241 WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x\n",
1242 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)), DEBUG_PROBE);
1243
1244 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1245 piix_setup_channel(&sc->pciide_channels[channel].wdc_channel);
1246 }
1247 WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x\n",
1248 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)), DEBUG_PROBE);
1249 }
1250
1251 void
1252 piix_setup_channel(chp)
1253 struct channel_softc *chp;
1254 {
1255 u_int8_t mode[2], drive;
1256 u_int32_t oidetim, idetim, idedma_ctl;
1257 struct pciide_channel *cp = (struct pciide_channel*)chp;
1258 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1259 struct ata_drive_datas *drvp = cp->wdc_channel.ch_drive;
1260
1261 oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1262 idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
1263 idedma_ctl = 0;
1264
1265 /* set up new idetim: Enable IDE registers decode */
1266 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
1267 chp->channel);
1268
1269 /* setup DMA */
1270 pciide_channel_dma_setup(cp);
1271
1272 /*
1273 * Here we have to mess up with drives mode: PIIX can't have
1274 * different timings for master and slave drives.
1275 * We need to find the best combination.
1276 */
1277
1278 /* If both drives supports DMA, take the lower mode */
1279 if ((drvp[0].drive_flags & DRIVE_DMA) &&
1280 (drvp[1].drive_flags & DRIVE_DMA)) {
1281 mode[0] = mode[1] =
1282 min(drvp[0].DMA_mode, drvp[1].DMA_mode);
1283 drvp[0].DMA_mode = mode[0];
1284 goto ok;
1285 }
1286 /*
1287 * If only one drive supports DMA, use its mode, and
1288 * put the other one in PIO mode 0 if mode not compatible
1289 */
1290 if (drvp[0].drive_flags & DRIVE_DMA) {
1291 mode[0] = drvp[0].DMA_mode;
1292 mode[1] = drvp[1].PIO_mode;
1293 if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
1294 piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
1295 mode[1] = 0;
1296 goto ok;
1297 }
1298 if (drvp[1].drive_flags & DRIVE_DMA) {
1299 mode[1] = drvp[1].DMA_mode;
1300 mode[0] = drvp[0].PIO_mode;
1301 if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
1302 piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
1303 mode[0] = 0;
1304 goto ok;
1305 }
1306 /*
1307 * If both drives are not DMA, takes the lower mode, unless
1308 * one of them is PIO mode < 2
1309 */
1310 if (drvp[0].PIO_mode < 2) {
1311 mode[0] = 0;
1312 mode[1] = drvp[1].PIO_mode;
1313 } else if (drvp[1].PIO_mode < 2) {
1314 mode[1] = 0;
1315 mode[0] = drvp[0].PIO_mode;
1316 } else {
1317 mode[0] = mode[1] =
1318 min(drvp[1].PIO_mode, drvp[0].PIO_mode);
1319 }
1320 ok: /* The modes are setup */
1321 for (drive = 0; drive < 2; drive++) {
1322 if (drvp[drive].drive_flags & DRIVE_DMA) {
1323 drvp[drive].DMA_mode = mode[drive];
1324 idetim |= piix_setup_idetim_timings(
1325 mode[drive], 1, chp->channel);
1326 goto end;
1327 } else
1328 drvp[drive].PIO_mode = mode[drive];
1329 }
1330 /* If we are there, none of the drives are DMA */
1331 if (mode[0] >= 2)
1332 idetim |= piix_setup_idetim_timings(
1333 mode[0], 0, chp->channel);
1334 else
1335 idetim |= piix_setup_idetim_timings(
1336 mode[1], 0, chp->channel);
1337 end: /*
1338 * timing mode is now set up in the controller. Enable
1339 * it per-drive
1340 */
1341 for (drive = 0; drive < 2; drive++) {
1342 /* If no drive, skip */
1343 if ((drvp[drive].drive_flags & DRIVE) == 0)
1344 continue;
1345 idetim |= piix_setup_idetim_drvs(&drvp[drive]);
1346 if (drvp[drive].drive_flags & DRIVE_DMA)
1347 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1348 }
1349 if (idedma_ctl != 0) {
1350 /* Add software bits in status register */
1351 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1352 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1353 idedma_ctl);
1354 }
1355 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1356 pciide_print_modes(cp);
1357 }
1358
1359 void
1360 piix3_4_setup_chip(sc)
1361 struct pciide_softc *sc;
1362 {
1363 int channel;
1364
1365 WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
1366 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
1367 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
1368 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
1369 WDCDEBUG_PRINT((", udamreg 0x%x",
1370 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
1371 DEBUG_PROBE);
1372 }
1373 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
1374
1375 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1376 piix3_4_setup_channel(
1377 &sc->pciide_channels[channel].wdc_channel);
1378 }
1379
1380 WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
1381 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
1382 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
1383 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
1384 WDCDEBUG_PRINT((", udmareg=0x%x",
1385 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
1386 DEBUG_PROBE);
1387 }
1388 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
1389 }
1390
1391 void
1392 piix3_4_setup_channel(chp)
1393 struct channel_softc *chp;
1394 {
1395 struct ata_drive_datas *drvp;
1396 u_int32_t oidetim, idetim, sidetim, udmareg, idedma_ctl;
1397 struct pciide_channel *cp = (struct pciide_channel*)chp;
1398 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1399 int drive;
1400
1401 oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1402 sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
1403 udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
1404 idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
1405 sidetim &= ~(PIIX_SIDETIM_ISP_MASK(chp->channel) |
1406 PIIX_SIDETIM_RTC_MASK(chp->channel));
1407
1408 idedma_ctl = 0;
1409 /* If channel disabled, no need to go further */
1410 if ((PIIX_IDETIM_READ(oidetim, chp->channel) & PIIX_IDETIM_IDE) == 0)
1411 return;
1412 /* set up new idetim: Enable IDE registers decode */
1413 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, chp->channel);
1414
1415 /* setup DMA if needed */
1416 pciide_channel_dma_setup(cp);
1417
1418 for (drive = 0; drive < 2; drive++) {
1419 udmareg &= ~(PIIX_UDMACTL_DRV_EN(chp->channel, drive) |
1420 PIIX_UDMATIM_SET(0x3, chp->channel, drive));
1421 drvp = &chp->ch_drive[drive];
1422 /* If no drive, skip */
1423 if ((drvp->drive_flags & DRIVE) == 0)
1424 continue;
1425 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1426 (drvp->drive_flags & DRIVE_UDMA) == 0))
1427 goto pio;
1428
1429 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1430 (drvp->drive_flags & DRIVE_UDMA)) {
1431 /* use Ultra/DMA */
1432 drvp->drive_flags &= ~DRIVE_DMA;
1433 udmareg |= PIIX_UDMACTL_DRV_EN(
1434 chp->channel, drive);
1435 udmareg |= PIIX_UDMATIM_SET(
1436 piix4_sct_udma[drvp->UDMA_mode],
1437 chp->channel, drive);
1438 } else {
1439 /* use Multiword DMA */
1440 drvp->drive_flags &= ~DRIVE_UDMA;
1441 if (drive == 0) {
1442 idetim |= piix_setup_idetim_timings(
1443 drvp->DMA_mode, 1, chp->channel);
1444 } else {
1445 sidetim |= piix_setup_sidetim_timings(
1446 drvp->DMA_mode, 1, chp->channel);
1447 idetim =PIIX_IDETIM_SET(idetim,
1448 PIIX_IDETIM_SITRE, chp->channel);
1449 }
1450 }
1451 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1452
1453 pio: /* use PIO mode */
1454 idetim |= piix_setup_idetim_drvs(drvp);
1455 if (drive == 0) {
1456 idetim |= piix_setup_idetim_timings(
1457 drvp->PIO_mode, 0, chp->channel);
1458 } else {
1459 sidetim |= piix_setup_sidetim_timings(
1460 drvp->PIO_mode, 0, chp->channel);
1461 idetim =PIIX_IDETIM_SET(idetim,
1462 PIIX_IDETIM_SITRE, chp->channel);
1463 }
1464 }
1465 if (idedma_ctl != 0) {
1466 /* Add software bits in status register */
1467 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1468 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1469 idedma_ctl);
1470 }
1471 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1472 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
1473 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
1474 pciide_print_modes(cp);
1475 }
1476
1477
1478 /* setup ISP and RTC fields, based on mode */
1479 static u_int32_t
1480 piix_setup_idetim_timings(mode, dma, channel)
1481 u_int8_t mode;
1482 u_int8_t dma;
1483 u_int8_t channel;
1484 {
1485
1486 if (dma)
1487 return PIIX_IDETIM_SET(0,
1488 PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
1489 PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
1490 channel);
1491 else
1492 return PIIX_IDETIM_SET(0,
1493 PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
1494 PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
1495 channel);
1496 }
1497
1498 /* setup DTE, PPE, IE and TIME field based on PIO mode */
1499 static u_int32_t
1500 piix_setup_idetim_drvs(drvp)
1501 struct ata_drive_datas *drvp;
1502 {
1503 u_int32_t ret = 0;
1504 struct channel_softc *chp = drvp->chnl_softc;
1505 u_int8_t channel = chp->channel;
1506 u_int8_t drive = drvp->drive;
1507
1508 /*
1509 * If drive is using UDMA, timings setups are independant
1510 * So just check DMA and PIO here.
1511 */
1512 if (drvp->drive_flags & DRIVE_DMA) {
1513 /* if mode = DMA mode 0, use compatible timings */
1514 if ((drvp->drive_flags & DRIVE_DMA) &&
1515 drvp->DMA_mode == 0) {
1516 drvp->PIO_mode = 0;
1517 return ret;
1518 }
1519 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1520 /*
1521 * PIO and DMA timings are the same, use fast timings for PIO
1522 * too, else use compat timings.
1523 */
1524 if ((piix_isp_pio[drvp->PIO_mode] !=
1525 piix_isp_dma[drvp->DMA_mode]) ||
1526 (piix_rtc_pio[drvp->PIO_mode] !=
1527 piix_rtc_dma[drvp->DMA_mode]))
1528 drvp->PIO_mode = 0;
1529 /* if PIO mode <= 2, use compat timings for PIO */
1530 if (drvp->PIO_mode <= 2) {
1531 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
1532 channel);
1533 return ret;
1534 }
1535 }
1536
1537 /*
1538 * Now setup PIO modes. If mode < 2, use compat timings.
1539 * Else enable fast timings. Enable IORDY and prefetch/post
1540 * if PIO mode >= 3.
1541 */
1542
1543 if (drvp->PIO_mode < 2)
1544 return ret;
1545
1546 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1547 if (drvp->PIO_mode >= 3) {
1548 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
1549 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
1550 }
1551 return ret;
1552 }
1553
1554 /* setup values in SIDETIM registers, based on mode */
1555 static u_int32_t
1556 piix_setup_sidetim_timings(mode, dma, channel)
1557 u_int8_t mode;
1558 u_int8_t dma;
1559 u_int8_t channel;
1560 {
1561 if (dma)
1562 return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
1563 PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
1564 else
1565 return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
1566 PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
1567 }
1568
1569 void
1570 piix_channel_map(pa, cp)
1571 struct pci_attach_args *pa;
1572 struct pciide_channel *cp;
1573 {
1574 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1575 bus_size_t cmdsize, ctlsize;
1576 struct channel_softc *wdc_cp = &cp->wdc_channel;
1577 u_int32_t idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1578
1579 if ((PIIX_IDETIM_READ(idetim, wdc_cp->channel) &
1580 PIIX_IDETIM_IDE) == 0) {
1581 printf("%s: %s channel ignored (disabled)\n",
1582 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1583 return;
1584 }
1585
1586 /* PIIX are compat-only pciide devices */
1587 pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize);
1588 if (cp->hw_ok == 0)
1589 return;
1590 if (pciiide_chan_candisable(cp)) {
1591 idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
1592 wdc_cp->channel);
1593 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1594 }
1595 pciide_map_compat_intr(pa, cp, wdc_cp->channel, 0);
1596 }
1597
1598 void
1599 apollo_setup_cap(sc)
1600 struct pciide_softc *sc;
1601 {
1602 if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
1603 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1604 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1605 WDC_CAPABILITY_DMA;
1606 sc->sc_wdcdev.PIO_cap = 4;
1607 sc->sc_wdcdev.DMA_cap = 2;
1608 sc->sc_wdcdev.UDMA_cap = 2;
1609 sc->sc_wdcdev.set_modes = apollo_setup_channel;
1610
1611 }
1612
1613 void
1614 apollo_setup_chip(sc)
1615 struct pciide_softc *sc;
1616 {
1617 int channel;
1618
1619 WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
1620 "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1621 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
1622 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
1623 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
1624 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
1625 DEBUG_PROBE);
1626
1627 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1628 apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
1629 }
1630 WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1631 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
1632 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
1633 }
1634
1635 void
1636 apollo_setup_channel(chp)
1637 struct channel_softc *chp;
1638 {
1639 u_int32_t udmatim_reg, datatim_reg;
1640 u_int8_t idedma_ctl;
1641 int mode, drive;
1642 struct ata_drive_datas *drvp;
1643 struct pciide_channel *cp = (struct pciide_channel*)chp;
1644 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1645
1646 idedma_ctl = 0;
1647 datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
1648 udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
1649 datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
1650 udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
1651
1652 /* setup DMA if needed */
1653 pciide_channel_dma_setup(cp);
1654
1655 for (drive = 0; drive < 2; drive++) {
1656 drvp = &chp->ch_drive[drive];
1657 /* If no drive, skip */
1658 if ((drvp->drive_flags & DRIVE) == 0)
1659 continue;
1660 /* add timing values, setup DMA if needed */
1661 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1662 (drvp->drive_flags & DRIVE_UDMA) == 0)) {
1663 mode = drvp->PIO_mode;
1664 goto pio;
1665 }
1666 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1667 (drvp->drive_flags & DRIVE_UDMA)) {
1668 /* use Ultra/DMA */
1669 drvp->drive_flags &= ~DRIVE_DMA;
1670 udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
1671 APO_UDMA_EN_MTH(chp->channel, drive) |
1672 APO_UDMA_TIME(chp->channel, drive,
1673 apollo_udma_tim[drvp->UDMA_mode]);
1674 /* can use PIO timings, MW DMA unused */
1675 mode = drvp->PIO_mode;
1676 } else {
1677 /* use Multiword DMA */
1678 drvp->drive_flags &= ~DRIVE_UDMA;
1679 /* mode = min(pio, dma+2) */
1680 if (drvp->PIO_mode <= (drvp->DMA_mode +2))
1681 mode = drvp->PIO_mode;
1682 else
1683 mode = drvp->DMA_mode + 2;
1684 }
1685 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1686
1687 pio: /* setup PIO mode */
1688 if (mode <= 2) {
1689 drvp->DMA_mode = 0;
1690 drvp->PIO_mode = 0;
1691 mode = 0;
1692 } else {
1693 drvp->PIO_mode = mode;
1694 drvp->DMA_mode = mode - 2;
1695 }
1696 datatim_reg |=
1697 APO_DATATIM_PULSE(chp->channel, drive,
1698 apollo_pio_set[mode]) |
1699 APO_DATATIM_RECOV(chp->channel, drive,
1700 apollo_pio_rec[mode]);
1701 }
1702 if (idedma_ctl != 0) {
1703 /* Add software bits in status register */
1704 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1705 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1706 idedma_ctl);
1707 }
1708 pciide_print_modes(cp);
1709 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
1710 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
1711 }
1712
1713 void
1714 apollo_channel_map(pa, cp)
1715 struct pci_attach_args *pa;
1716 struct pciide_channel *cp;
1717 {
1718 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1719 bus_size_t cmdsize, ctlsize;
1720 struct channel_softc *wdc_cp = &cp->wdc_channel;
1721 u_int32_t ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
1722 int interface =
1723 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1724
1725 if ((ideconf & APO_IDECONF_EN(wdc_cp->channel)) == 0) {
1726 printf("%s: %s channel ignored (disabled)\n",
1727 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1728 return;
1729 }
1730
1731 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
1732 if (cp->hw_ok == 0)
1733 return;
1734 if (pciiide_chan_candisable(cp)) {
1735 ideconf &= ~APO_IDECONF_EN(wdc_cp->channel);
1736 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF, ideconf);
1737 }
1738 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
1739 }
1740
1741 void
1742 cmd_channel_map(pa, cp)
1743 struct pci_attach_args *pa;
1744 struct pciide_channel *cp;
1745 {
1746 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1747 bus_size_t cmdsize, ctlsize;
1748 struct channel_softc *wdc_cp = &cp->wdc_channel;
1749 u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
1750 int interface =
1751 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1752
1753 /*
1754 * with a CMD PCI64x, if we get here, the first channel is enabled:
1755 * there's no way to disable the first channel without disabling
1756 * the whole device
1757 */
1758 if (wdc_cp->channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
1759 printf("%s: %s channel ignored (disabled)\n",
1760 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1761 return;
1762 }
1763
1764 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
1765 if (cp->hw_ok == 0)
1766 return;
1767 if (wdc_cp->channel == 1) {
1768 if (pciiide_chan_candisable(cp)) {
1769 ctrl &= ~CMD_CTRL_2PORT;
1770 pciide_pci_write(pa->pa_pc, pa->pa_tag,
1771 CMD_CTRL, ctrl);
1772 }
1773 }
1774 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
1775 }
1776
1777 void
1778 cmd0643_6_setup_cap(sc)
1779 struct pciide_softc *sc;
1780 {
1781 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1782 WDC_CAPABILITY_DMA;
1783 sc->sc_wdcdev.PIO_cap = 4;
1784 sc->sc_wdcdev.DMA_cap = 2;
1785 sc->sc_wdcdev.set_modes = cmd0643_6_setup_channel;
1786 }
1787
1788 void
1789 cmd0643_6_setup_chip(sc)
1790 struct pciide_softc *sc;
1791 {
1792 int channel;
1793
1794 WDCDEBUG_PRINT(("cmd0643_6_setup_chip: old timings reg 0x%x 0x%x\n",
1795 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
1796 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
1797 DEBUG_PROBE);
1798 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1799 cmd0643_6_setup_channel(
1800 &sc->pciide_channels[channel].wdc_channel);
1801 }
1802 /* configure for DMA read multiple */
1803 pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
1804 WDCDEBUG_PRINT(("cmd0643_6_setup_chip: timings reg now 0x%x 0x%x\n",
1805 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
1806 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
1807 DEBUG_PROBE);
1808 }
1809
1810 void
1811 cmd0643_6_setup_channel(chp)
1812 struct channel_softc *chp;
1813 {
1814 struct ata_drive_datas *drvp;
1815 u_int8_t tim;
1816 u_int32_t idedma_ctl;
1817 int drive;
1818 struct pciide_channel *cp = (struct pciide_channel*)chp;
1819 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1820
1821 idedma_ctl = 0;
1822 /* setup DMA if needed */
1823 pciide_channel_dma_setup(cp);
1824
1825 for (drive = 0; drive < 2; drive++) {
1826 drvp = &chp->ch_drive[drive];
1827 /* If no drive, skip */
1828 if ((drvp->drive_flags & DRIVE) == 0)
1829 continue;
1830 /* add timing values, setup DMA if needed */
1831 tim = cmd0643_6_data_tim_pio[drvp->PIO_mode];
1832 if (drvp->drive_flags & DRIVE_DMA) {
1833 /*
1834 * use Multiword DMA.
1835 * Timings will be used for both PIO and DMA, so adjust
1836 * DMA mode if needed
1837 */
1838 if (drvp->PIO_mode >= 3 &&
1839 (drvp->DMA_mode + 2) > drvp->PIO_mode) {
1840 drvp->DMA_mode = drvp->PIO_mode - 2;
1841 }
1842 tim = cmd0643_6_data_tim_dma[drvp->DMA_mode];
1843 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1844 }
1845 pciide_pci_write(sc->sc_pc, sc->sc_tag,
1846 CMD_DATA_TIM(chp->channel, drive), tim);
1847 }
1848 if (idedma_ctl != 0) {
1849 /* Add software bits in status register */
1850 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1851 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1852 idedma_ctl);
1853 }
1854 pciide_print_modes(cp);
1855 }
1856
1857 void
1858 cy693_setup_cap(sc)
1859 struct pciide_softc *sc;
1860 {
1861 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1862 WDC_CAPABILITY_DMA;
1863 sc->sc_wdcdev.PIO_cap = 4;
1864 sc->sc_wdcdev.DMA_cap = 2;
1865 sc->sc_wdcdev.set_modes = cy693_setup_channel;
1866 }
1867
1868 void
1869 cy693_setup_chip(sc)
1870 struct pciide_softc *sc;
1871 {
1872 WDCDEBUG_PRINT(("cy693_setup_chip: old timings reg 0x%x\n",
1873 pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),
1874 DEBUG_PROBE);
1875 cy693_setup_channel(&sc->pciide_channels[0].wdc_channel);
1876 WDCDEBUG_PRINT(("cy693_setup_chip: new timings reg 0x%x\n",
1877 pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
1878 }
1879
1880 void
1881 cy693_setup_channel(chp)
1882 struct channel_softc *chp;
1883 {
1884 struct ata_drive_datas *drvp;
1885 int drive;
1886 u_int32_t cy_cmd_ctrl;
1887 u_int32_t idedma_ctl;
1888 struct pciide_channel *cp = (struct pciide_channel*)chp;
1889 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1890
1891 cy_cmd_ctrl = idedma_ctl = 0;
1892
1893 /* setup DMA if needed */
1894 pciide_channel_dma_setup(cp);
1895
1896 for (drive = 0; drive < 2; drive++) {
1897 drvp = &chp->ch_drive[drive];
1898 /* If no drive, skip */
1899 if ((drvp->drive_flags & DRIVE) == 0)
1900 continue;
1901 /* add timing values, setup DMA if needed */
1902 if (drvp->drive_flags & DRIVE_DMA) {
1903 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1904 /*
1905 * use Multiword DMA
1906 * Timings will be used for both PIO and DMA, so adjust
1907 * DMA mode if needed
1908 */
1909 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
1910 drvp->PIO_mode = drvp->DMA_mode + 2;
1911 if (drvp->DMA_mode == 0)
1912 drvp->PIO_mode = 0;
1913 }
1914 cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
1915 CY_CMD_CTRL_IOW_PULSE_OFF(drive));
1916 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
1917 CY_CMD_CTRL_IOW_REC_OFF(drive));
1918 cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
1919 CY_CMD_CTRL_IOR_PULSE_OFF(drive));
1920 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
1921 CY_CMD_CTRL_IOR_REC_OFF(drive));
1922 }
1923 pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
1924 pciide_print_modes(cp);
1925 if (idedma_ctl != 0) {
1926 /* Add software bits in status register */
1927 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1928 IDEDMA_CTL, idedma_ctl);
1929 }
1930 }
1931
1932 void
1933 cy693_channel_map(pa, cp)
1934 struct pci_attach_args *pa;
1935 struct pciide_channel *cp;
1936 {
1937 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1938 bus_size_t cmdsize, ctlsize;
1939 struct channel_softc *wdc_cp = &cp->wdc_channel;
1940 int interface =
1941 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1942 int compatchan;
1943
1944 #ifdef DIAGNOSTIC
1945 if (wdc_cp->channel != 0)
1946 panic("cy693_channel_map: channel %d", wdc_cp->channel);
1947 #endif
1948
1949 /*
1950 * this chip has 2 PCI IDE functions, one for primary and one for
1951 * secondary. So we need to call pciide_mapregs_compat() with
1952 * the real channel
1953 */
1954 if (pa->pa_function == 1) {
1955 compatchan = 0;
1956 } else if (pa->pa_function == 2) {
1957 compatchan = 1;
1958 } else {
1959 printf("%s: unexpected PCI function %d\n",
1960 sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
1961 cp->hw_ok = 0;
1962 return;
1963 }
1964
1965 /* Only one channel for this chip; if we are here it's enabled */
1966 if (interface & PCIIDE_INTERFACE_PCI(0))
1967 cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
1968 else
1969 cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
1970 &cmdsize, &ctlsize);
1971 if (cp->hw_ok == 0)
1972 return;
1973 wdc_cp->data32iot = wdc_cp->cmd_iot;
1974 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1975 wdcattach(wdc_cp);
1976 if (pciiide_chan_candisable(cp)) {
1977 pci_conf_write(sc->sc_pc, sc->sc_tag,
1978 PCI_COMMAND_STATUS_REG, 0);
1979 }
1980 pciide_map_compat_intr(pa, cp, compatchan, interface);
1981 }
1982
1983 void
1984 sis_setup_cap(sc)
1985 struct pciide_softc *sc;
1986 {
1987 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1988 WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
1989 sc->sc_wdcdev.PIO_cap = 4;
1990 sc->sc_wdcdev.DMA_cap = 2;
1991 sc->sc_wdcdev.UDMA_cap = 2;
1992 sc->sc_wdcdev.set_modes = sis_setup_channel;
1993 }
1994
1995 void
1996 sis_setup_chip(sc)
1997 struct pciide_softc *sc;
1998 {
1999 int channel;
2000
2001 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2002 sis_setup_channel(&sc->pciide_channels[channel].wdc_channel);
2003 }
2004 pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
2005 pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
2006 SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
2007 }
2008
2009 void
2010 sis_setup_channel(chp)
2011 struct channel_softc *chp;
2012 {
2013 struct ata_drive_datas *drvp;
2014 int drive;
2015 u_int32_t sis_tim;
2016 u_int32_t idedma_ctl;
2017 struct pciide_channel *cp = (struct pciide_channel*)chp;
2018 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2019
2020 WDCDEBUG_PRINT(("sis_setup_chip: old timings reg for "
2021 "channel %d 0x%x\n", chp->channel,
2022 pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
2023 DEBUG_PROBE);
2024 sis_tim = 0;
2025 idedma_ctl = 0;
2026 /* setup DMA if needed */
2027 pciide_channel_dma_setup(cp);
2028
2029 for (drive = 0; drive < 2; drive++) {
2030 drvp = &chp->ch_drive[drive];
2031 /* If no drive, skip */
2032 if ((drvp->drive_flags & DRIVE) == 0)
2033 continue;
2034 /* add timing values, setup DMA if needed */
2035 if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
2036 (drvp->drive_flags & DRIVE_UDMA) == 0)
2037 goto pio;
2038
2039 if (drvp->drive_flags & DRIVE_UDMA) {
2040 /* use Ultra/DMA */
2041 drvp->drive_flags &= ~DRIVE_DMA;
2042 sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
2043 SIS_TIM_UDMA_TIME_OFF(drive);
2044 sis_tim |= SIS_TIM_UDMA_EN(drive);
2045 } else {
2046 /*
2047 * use Multiword DMA
2048 * Timings will be used for both PIO and DMA,
2049 * so adjust DMA mode if needed
2050 */
2051 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
2052 drvp->PIO_mode = drvp->DMA_mode + 2;
2053 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
2054 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
2055 drvp->PIO_mode - 2 : 0;
2056 if (drvp->DMA_mode == 0)
2057 drvp->PIO_mode = 0;
2058 }
2059 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2060 pio: sis_tim |= sis_pio_act[drvp->PIO_mode] <<
2061 SIS_TIM_ACT_OFF(drive);
2062 sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
2063 SIS_TIM_REC_OFF(drive);
2064 }
2065 WDCDEBUG_PRINT(("sis_setup_chip: new timings reg for "
2066 "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
2067 pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
2068 if (idedma_ctl != 0) {
2069 /* Add software bits in status register */
2070 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2071 IDEDMA_CTL, idedma_ctl);
2072 }
2073 pciide_print_modes(cp);
2074 }
2075
2076 void
2077 sis_channel_map(pa, cp)
2078 struct pci_attach_args *pa;
2079 struct pciide_channel *cp;
2080 {
2081 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2082 bus_size_t cmdsize, ctlsize;
2083 struct channel_softc *wdc_cp = &cp->wdc_channel;
2084 u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
2085 int interface =
2086 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
2087
2088 if ((wdc_cp->channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
2089 (wdc_cp->channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
2090 printf("%s: %s channel ignored (disabled)\n",
2091 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2092 return;
2093 }
2094
2095 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
2096 if (cp->hw_ok == 0)
2097 return;
2098 if (pciiide_chan_candisable(cp)) {
2099 if (wdc_cp->channel == 0)
2100 sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
2101 else
2102 sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
2103 pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0, sis_ctr0);
2104 }
2105 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
2106 }
2107
2108 void
2109 acer_setup_cap(sc)
2110 struct pciide_softc *sc;
2111 {
2112 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
2113 WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
2114 sc->sc_wdcdev.PIO_cap = 4;
2115 sc->sc_wdcdev.DMA_cap = 2;
2116 sc->sc_wdcdev.UDMA_cap = 2;
2117 sc->sc_wdcdev.set_modes = acer_setup_channel;
2118 }
2119
2120 void
2121 acer_setup_chip(sc)
2122 struct pciide_softc *sc;
2123 {
2124 int channel;
2125
2126 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
2127 (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
2128 ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
2129
2130 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2131 acer_setup_channel(&sc->pciide_channels[channel].wdc_channel);
2132 }
2133 }
2134
2135 void
2136 acer_setup_channel(chp)
2137 struct channel_softc *chp;
2138 {
2139 struct ata_drive_datas *drvp;
2140 int drive;
2141 u_int32_t acer_fifo_udma;
2142 u_int32_t idedma_ctl;
2143 struct pciide_channel *cp = (struct pciide_channel*)chp;
2144 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2145
2146 idedma_ctl = 0;
2147 acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
2148 WDCDEBUG_PRINT(("acer_setup_chip: old fifo/udma reg 0x%x\n",
2149 acer_fifo_udma), DEBUG_PROBE);
2150 /* setup DMA if needed */
2151 pciide_channel_dma_setup(cp);
2152
2153 for (drive = 0; drive < 2; drive++) {
2154 drvp = &chp->ch_drive[drive];
2155 /* If no drive, skip */
2156 if ((drvp->drive_flags & DRIVE) == 0)
2157 continue;
2158 WDCDEBUG_PRINT(("acer_setup_chip: old timings reg for "
2159 "channel %d drive %d 0x%x\n", chp->channel, drive,
2160 pciide_pci_read(sc->sc_pc, sc->sc_tag,
2161 ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
2162 /* clear FIFO/DMA mode */
2163 acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
2164 ACER_UDMA_EN(chp->channel, drive) |
2165 ACER_UDMA_TIM(chp->channel, drive, 0x7));
2166
2167 /* add timing values, setup DMA if needed */
2168 if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
2169 (drvp->drive_flags & DRIVE_UDMA) == 0) {
2170 acer_fifo_udma |=
2171 ACER_FTH_OPL(chp->channel, drive, 0x1);
2172 goto pio;
2173 }
2174
2175 acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
2176 if (drvp->drive_flags & DRIVE_UDMA) {
2177 /* use Ultra/DMA */
2178 drvp->drive_flags &= ~DRIVE_DMA;
2179 acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
2180 acer_fifo_udma |=
2181 ACER_UDMA_TIM(chp->channel, drive,
2182 acer_udma[drvp->UDMA_mode]);
2183 } else {
2184 /*
2185 * use Multiword DMA
2186 * Timings will be used for both PIO and DMA,
2187 * so adjust DMA mode if needed
2188 */
2189 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
2190 drvp->PIO_mode = drvp->DMA_mode + 2;
2191 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
2192 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
2193 drvp->PIO_mode - 2 : 0;
2194 if (drvp->DMA_mode == 0)
2195 drvp->PIO_mode = 0;
2196 }
2197 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2198 pio: pciide_pci_write(sc->sc_pc, sc->sc_tag,
2199 ACER_IDETIM(chp->channel, drive),
2200 acer_pio[drvp->PIO_mode]);
2201 }
2202 WDCDEBUG_PRINT(("acer_setup_chip: new fifo/udma reg 0x%x\n",
2203 acer_fifo_udma), DEBUG_PROBE);
2204 pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
2205 if (idedma_ctl != 0) {
2206 /* Add software bits in status register */
2207 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2208 IDEDMA_CTL, idedma_ctl);
2209 }
2210 pciide_print_modes(cp);
2211 }
2212
2213 void
2214 acer_channel_map(pa, cp)
2215 struct pci_attach_args *pa;
2216 struct pciide_channel *cp;
2217 {
2218 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2219 bus_size_t cmdsize, ctlsize;
2220 struct channel_softc *wdc_cp = &cp->wdc_channel;
2221 u_int32_t cr;
2222 int interface;
2223
2224 /*
2225 * Enable "microsoft register bits" R/W. Will be done 2 times
2226 * (one for each channel) but should'nt be a problem. There's no
2227 * better place where to put this.
2228 */
2229 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
2230 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
2231 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
2232 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
2233 ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
2234 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
2235 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
2236 ~ACER_CHANSTATUSREGS_RO);
2237 cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
2238 cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
2239 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
2240 /* Don't use cr, re-read the real register content instead */
2241 interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
2242 PCI_CLASS_REG));
2243
2244 if ((interface & PCIIDE_CHAN_EN(wdc_cp->channel)) == 0) {
2245 printf("%s: %s channel ignored (disabled)\n",
2246 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2247 return;
2248 }
2249
2250 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
2251 if (cp->hw_ok == 0)
2252 return;
2253 if (pciiide_chan_candisable(cp)) {
2254 cr &= ~(PCIIDE_CHAN_EN(wdc_cp->channel) << PCI_INTERFACE_SHIFT);
2255 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
2256 }
2257 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
2258 }
2259