pciide.c revision 1.33.2.4 1 /* $NetBSD: pciide.c,v 1.33.2.4 1999/06/24 00:03:52 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 drvp[1].DMA_mode = mode[1];
1285 goto ok;
1286 }
1287 /*
1288 * If only one drive supports DMA, use its mode, and
1289 * put the other one in PIO mode 0 if mode not compatible
1290 */
1291 if (drvp[0].drive_flags & DRIVE_DMA) {
1292 mode[0] = drvp[0].DMA_mode;
1293 mode[1] = drvp[1].PIO_mode;
1294 if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
1295 piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
1296 mode[1] = drvp[1].PIO_mode = 0;
1297 goto ok;
1298 }
1299 if (drvp[1].drive_flags & DRIVE_DMA) {
1300 mode[1] = drvp[1].DMA_mode;
1301 mode[0] = drvp[0].PIO_mode;
1302 if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
1303 piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
1304 mode[0] = drvp[0].PIO_mode = 0;
1305 goto ok;
1306 }
1307 /*
1308 * If both drives are not DMA, takes the lower mode, unless
1309 * one of them is PIO mode < 2
1310 */
1311 if (drvp[0].PIO_mode < 2) {
1312 mode[0] = drvp[0].PIO_mode = 0;
1313 mode[1] = drvp[1].PIO_mode;
1314 } else if (drvp[1].PIO_mode < 2) {
1315 mode[1] = drvp[1].PIO_mode = 0;
1316 mode[0] = drvp[0].PIO_mode;
1317 } else {
1318 mode[0] = mode[1] =
1319 min(drvp[1].PIO_mode, drvp[0].PIO_mode);
1320 drvp[0].PIO_mode = mode[0];
1321 drvp[1].PIO_mode = mode[1];
1322 }
1323 ok: /* The modes are setup */
1324 for (drive = 0; drive < 2; drive++) {
1325 if (drvp[drive].drive_flags & DRIVE_DMA) {
1326 idetim |= piix_setup_idetim_timings(
1327 mode[drive], 1, chp->channel);
1328 goto end;
1329 }
1330 }
1331 /* If we are there, none of the drives are DMA */
1332 if (mode[0] >= 2)
1333 idetim |= piix_setup_idetim_timings(
1334 mode[0], 0, chp->channel);
1335 else
1336 idetim |= piix_setup_idetim_timings(
1337 mode[1], 0, chp->channel);
1338 end: /*
1339 * timing mode is now set up in the controller. Enable
1340 * it per-drive
1341 */
1342 for (drive = 0; drive < 2; drive++) {
1343 /* If no drive, skip */
1344 if ((drvp[drive].drive_flags & DRIVE) == 0)
1345 continue;
1346 idetim |= piix_setup_idetim_drvs(&drvp[drive]);
1347 if (drvp[drive].drive_flags & DRIVE_DMA)
1348 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1349 }
1350 if (idedma_ctl != 0) {
1351 /* Add software bits in status register */
1352 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1353 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1354 idedma_ctl);
1355 }
1356 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1357 pciide_print_modes(cp);
1358 }
1359
1360 void
1361 piix3_4_setup_chip(sc)
1362 struct pciide_softc *sc;
1363 {
1364 int channel;
1365
1366 WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
1367 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
1368 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
1369 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
1370 WDCDEBUG_PRINT((", udamreg 0x%x",
1371 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
1372 DEBUG_PROBE);
1373 }
1374 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
1375
1376 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1377 piix3_4_setup_channel(
1378 &sc->pciide_channels[channel].wdc_channel);
1379 }
1380
1381 WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
1382 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM),
1383 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)), DEBUG_PROBE);
1384 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
1385 WDCDEBUG_PRINT((", udmareg=0x%x",
1386 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
1387 DEBUG_PROBE);
1388 }
1389 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
1390 }
1391
1392 void
1393 piix3_4_setup_channel(chp)
1394 struct channel_softc *chp;
1395 {
1396 struct ata_drive_datas *drvp;
1397 u_int32_t oidetim, idetim, sidetim, udmareg, idedma_ctl;
1398 struct pciide_channel *cp = (struct pciide_channel*)chp;
1399 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1400 int drive;
1401
1402 oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1403 sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
1404 udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
1405 idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
1406 sidetim &= ~(PIIX_SIDETIM_ISP_MASK(chp->channel) |
1407 PIIX_SIDETIM_RTC_MASK(chp->channel));
1408
1409 idedma_ctl = 0;
1410 /* If channel disabled, no need to go further */
1411 if ((PIIX_IDETIM_READ(oidetim, chp->channel) & PIIX_IDETIM_IDE) == 0)
1412 return;
1413 /* set up new idetim: Enable IDE registers decode */
1414 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, chp->channel);
1415
1416 /* setup DMA if needed */
1417 pciide_channel_dma_setup(cp);
1418
1419 for (drive = 0; drive < 2; drive++) {
1420 udmareg &= ~(PIIX_UDMACTL_DRV_EN(chp->channel, drive) |
1421 PIIX_UDMATIM_SET(0x3, chp->channel, drive));
1422 drvp = &chp->ch_drive[drive];
1423 /* If no drive, skip */
1424 if ((drvp->drive_flags & DRIVE) == 0)
1425 continue;
1426 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1427 (drvp->drive_flags & DRIVE_UDMA) == 0))
1428 goto pio;
1429
1430 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1431 (drvp->drive_flags & DRIVE_UDMA)) {
1432 /* use Ultra/DMA */
1433 drvp->drive_flags &= ~DRIVE_DMA;
1434 udmareg |= PIIX_UDMACTL_DRV_EN(
1435 chp->channel, drive);
1436 udmareg |= PIIX_UDMATIM_SET(
1437 piix4_sct_udma[drvp->UDMA_mode],
1438 chp->channel, drive);
1439 } else {
1440 /* use Multiword DMA */
1441 drvp->drive_flags &= ~DRIVE_UDMA;
1442 if (drive == 0) {
1443 idetim |= piix_setup_idetim_timings(
1444 drvp->DMA_mode, 1, chp->channel);
1445 } else {
1446 sidetim |= piix_setup_sidetim_timings(
1447 drvp->DMA_mode, 1, chp->channel);
1448 idetim =PIIX_IDETIM_SET(idetim,
1449 PIIX_IDETIM_SITRE, chp->channel);
1450 }
1451 }
1452 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1453
1454 pio: /* use PIO mode */
1455 idetim |= piix_setup_idetim_drvs(drvp);
1456 if (drive == 0) {
1457 idetim |= piix_setup_idetim_timings(
1458 drvp->PIO_mode, 0, chp->channel);
1459 } else {
1460 sidetim |= piix_setup_sidetim_timings(
1461 drvp->PIO_mode, 0, chp->channel);
1462 idetim =PIIX_IDETIM_SET(idetim,
1463 PIIX_IDETIM_SITRE, chp->channel);
1464 }
1465 }
1466 if (idedma_ctl != 0) {
1467 /* Add software bits in status register */
1468 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1469 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1470 idedma_ctl);
1471 }
1472 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1473 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
1474 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
1475 pciide_print_modes(cp);
1476 }
1477
1478
1479 /* setup ISP and RTC fields, based on mode */
1480 static u_int32_t
1481 piix_setup_idetim_timings(mode, dma, channel)
1482 u_int8_t mode;
1483 u_int8_t dma;
1484 u_int8_t channel;
1485 {
1486
1487 if (dma)
1488 return PIIX_IDETIM_SET(0,
1489 PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
1490 PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
1491 channel);
1492 else
1493 return PIIX_IDETIM_SET(0,
1494 PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
1495 PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
1496 channel);
1497 }
1498
1499 /* setup DTE, PPE, IE and TIME field based on PIO mode */
1500 static u_int32_t
1501 piix_setup_idetim_drvs(drvp)
1502 struct ata_drive_datas *drvp;
1503 {
1504 u_int32_t ret = 0;
1505 struct channel_softc *chp = drvp->chnl_softc;
1506 u_int8_t channel = chp->channel;
1507 u_int8_t drive = drvp->drive;
1508
1509 /*
1510 * If drive is using UDMA, timings setups are independant
1511 * So just check DMA and PIO here.
1512 */
1513 if (drvp->drive_flags & DRIVE_DMA) {
1514 /* if mode = DMA mode 0, use compatible timings */
1515 if ((drvp->drive_flags & DRIVE_DMA) &&
1516 drvp->DMA_mode == 0) {
1517 drvp->PIO_mode = 0;
1518 return ret;
1519 }
1520 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1521 /*
1522 * PIO and DMA timings are the same, use fast timings for PIO
1523 * too, else use compat timings.
1524 */
1525 if ((piix_isp_pio[drvp->PIO_mode] !=
1526 piix_isp_dma[drvp->DMA_mode]) ||
1527 (piix_rtc_pio[drvp->PIO_mode] !=
1528 piix_rtc_dma[drvp->DMA_mode]))
1529 drvp->PIO_mode = 0;
1530 /* if PIO mode <= 2, use compat timings for PIO */
1531 if (drvp->PIO_mode <= 2) {
1532 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
1533 channel);
1534 return ret;
1535 }
1536 }
1537
1538 /*
1539 * Now setup PIO modes. If mode < 2, use compat timings.
1540 * Else enable fast timings. Enable IORDY and prefetch/post
1541 * if PIO mode >= 3.
1542 */
1543
1544 if (drvp->PIO_mode < 2)
1545 return ret;
1546
1547 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1548 if (drvp->PIO_mode >= 3) {
1549 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
1550 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
1551 }
1552 return ret;
1553 }
1554
1555 /* setup values in SIDETIM registers, based on mode */
1556 static u_int32_t
1557 piix_setup_sidetim_timings(mode, dma, channel)
1558 u_int8_t mode;
1559 u_int8_t dma;
1560 u_int8_t channel;
1561 {
1562 if (dma)
1563 return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
1564 PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
1565 else
1566 return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
1567 PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
1568 }
1569
1570 void
1571 piix_channel_map(pa, cp)
1572 struct pci_attach_args *pa;
1573 struct pciide_channel *cp;
1574 {
1575 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1576 bus_size_t cmdsize, ctlsize;
1577 struct channel_softc *wdc_cp = &cp->wdc_channel;
1578 u_int32_t idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1579
1580 if ((PIIX_IDETIM_READ(idetim, wdc_cp->channel) &
1581 PIIX_IDETIM_IDE) == 0) {
1582 printf("%s: %s channel ignored (disabled)\n",
1583 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1584 return;
1585 }
1586
1587 /* PIIX are compat-only pciide devices */
1588 pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize);
1589 if (cp->hw_ok == 0)
1590 return;
1591 if (pciiide_chan_candisable(cp)) {
1592 idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
1593 wdc_cp->channel);
1594 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1595 }
1596 pciide_map_compat_intr(pa, cp, wdc_cp->channel, 0);
1597 }
1598
1599 void
1600 apollo_setup_cap(sc)
1601 struct pciide_softc *sc;
1602 {
1603 if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
1604 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1605 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1606 WDC_CAPABILITY_DMA;
1607 sc->sc_wdcdev.PIO_cap = 4;
1608 sc->sc_wdcdev.DMA_cap = 2;
1609 sc->sc_wdcdev.UDMA_cap = 2;
1610 sc->sc_wdcdev.set_modes = apollo_setup_channel;
1611
1612 }
1613
1614 void
1615 apollo_setup_chip(sc)
1616 struct pciide_softc *sc;
1617 {
1618 int channel;
1619
1620 WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
1621 "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1622 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
1623 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
1624 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
1625 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
1626 DEBUG_PROBE);
1627
1628 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1629 apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
1630 }
1631 WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1632 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
1633 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
1634 }
1635
1636 void
1637 apollo_setup_channel(chp)
1638 struct channel_softc *chp;
1639 {
1640 u_int32_t udmatim_reg, datatim_reg;
1641 u_int8_t idedma_ctl;
1642 int mode, drive;
1643 struct ata_drive_datas *drvp;
1644 struct pciide_channel *cp = (struct pciide_channel*)chp;
1645 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1646
1647 idedma_ctl = 0;
1648 datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
1649 udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
1650 datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
1651 udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
1652
1653 /* setup DMA if needed */
1654 pciide_channel_dma_setup(cp);
1655
1656 for (drive = 0; drive < 2; drive++) {
1657 drvp = &chp->ch_drive[drive];
1658 /* If no drive, skip */
1659 if ((drvp->drive_flags & DRIVE) == 0)
1660 continue;
1661 /* add timing values, setup DMA if needed */
1662 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1663 (drvp->drive_flags & DRIVE_UDMA) == 0)) {
1664 mode = drvp->PIO_mode;
1665 goto pio;
1666 }
1667 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1668 (drvp->drive_flags & DRIVE_UDMA)) {
1669 /* use Ultra/DMA */
1670 drvp->drive_flags &= ~DRIVE_DMA;
1671 udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
1672 APO_UDMA_EN_MTH(chp->channel, drive) |
1673 APO_UDMA_TIME(chp->channel, drive,
1674 apollo_udma_tim[drvp->UDMA_mode]);
1675 /* can use PIO timings, MW DMA unused */
1676 mode = drvp->PIO_mode;
1677 } else {
1678 /* use Multiword DMA */
1679 drvp->drive_flags &= ~DRIVE_UDMA;
1680 /* mode = min(pio, dma+2) */
1681 if (drvp->PIO_mode <= (drvp->DMA_mode +2))
1682 mode = drvp->PIO_mode;
1683 else
1684 mode = drvp->DMA_mode + 2;
1685 }
1686 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1687
1688 pio: /* setup PIO mode */
1689 if (mode <= 2) {
1690 drvp->DMA_mode = 0;
1691 drvp->PIO_mode = 0;
1692 mode = 0;
1693 } else {
1694 drvp->PIO_mode = mode;
1695 drvp->DMA_mode = mode - 2;
1696 }
1697 datatim_reg |=
1698 APO_DATATIM_PULSE(chp->channel, drive,
1699 apollo_pio_set[mode]) |
1700 APO_DATATIM_RECOV(chp->channel, drive,
1701 apollo_pio_rec[mode]);
1702 }
1703 if (idedma_ctl != 0) {
1704 /* Add software bits in status register */
1705 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1706 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1707 idedma_ctl);
1708 }
1709 pciide_print_modes(cp);
1710 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
1711 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
1712 }
1713
1714 void
1715 apollo_channel_map(pa, cp)
1716 struct pci_attach_args *pa;
1717 struct pciide_channel *cp;
1718 {
1719 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1720 bus_size_t cmdsize, ctlsize;
1721 struct channel_softc *wdc_cp = &cp->wdc_channel;
1722 u_int32_t ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
1723 int interface =
1724 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1725
1726 if ((ideconf & APO_IDECONF_EN(wdc_cp->channel)) == 0) {
1727 printf("%s: %s channel ignored (disabled)\n",
1728 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1729 return;
1730 }
1731
1732 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
1733 if (cp->hw_ok == 0)
1734 return;
1735 if (pciiide_chan_candisable(cp)) {
1736 ideconf &= ~APO_IDECONF_EN(wdc_cp->channel);
1737 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF, ideconf);
1738 }
1739 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
1740 }
1741
1742 void
1743 cmd_channel_map(pa, cp)
1744 struct pci_attach_args *pa;
1745 struct pciide_channel *cp;
1746 {
1747 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1748 bus_size_t cmdsize, ctlsize;
1749 struct channel_softc *wdc_cp = &cp->wdc_channel;
1750 u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
1751 int interface =
1752 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1753
1754 /*
1755 * with a CMD PCI64x, if we get here, the first channel is enabled:
1756 * there's no way to disable the first channel without disabling
1757 * the whole device
1758 */
1759 if (wdc_cp->channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
1760 printf("%s: %s channel ignored (disabled)\n",
1761 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1762 return;
1763 }
1764
1765 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
1766 if (cp->hw_ok == 0)
1767 return;
1768 if (wdc_cp->channel == 1) {
1769 if (pciiide_chan_candisable(cp)) {
1770 ctrl &= ~CMD_CTRL_2PORT;
1771 pciide_pci_write(pa->pa_pc, pa->pa_tag,
1772 CMD_CTRL, ctrl);
1773 }
1774 }
1775 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
1776 }
1777
1778 void
1779 cmd0643_6_setup_cap(sc)
1780 struct pciide_softc *sc;
1781 {
1782 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1783 WDC_CAPABILITY_DMA;
1784 sc->sc_wdcdev.PIO_cap = 4;
1785 sc->sc_wdcdev.DMA_cap = 2;
1786 sc->sc_wdcdev.set_modes = cmd0643_6_setup_channel;
1787 }
1788
1789 void
1790 cmd0643_6_setup_chip(sc)
1791 struct pciide_softc *sc;
1792 {
1793 int channel;
1794
1795 WDCDEBUG_PRINT(("cmd0643_6_setup_chip: old timings reg 0x%x 0x%x\n",
1796 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
1797 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
1798 DEBUG_PROBE);
1799 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1800 cmd0643_6_setup_channel(
1801 &sc->pciide_channels[channel].wdc_channel);
1802 }
1803 /* configure for DMA read multiple */
1804 pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
1805 WDCDEBUG_PRINT(("cmd0643_6_setup_chip: timings reg now 0x%x 0x%x\n",
1806 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
1807 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
1808 DEBUG_PROBE);
1809 }
1810
1811 void
1812 cmd0643_6_setup_channel(chp)
1813 struct channel_softc *chp;
1814 {
1815 struct ata_drive_datas *drvp;
1816 u_int8_t tim;
1817 u_int32_t idedma_ctl;
1818 int drive;
1819 struct pciide_channel *cp = (struct pciide_channel*)chp;
1820 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1821
1822 idedma_ctl = 0;
1823 /* setup DMA if needed */
1824 pciide_channel_dma_setup(cp);
1825
1826 for (drive = 0; drive < 2; drive++) {
1827 drvp = &chp->ch_drive[drive];
1828 /* If no drive, skip */
1829 if ((drvp->drive_flags & DRIVE) == 0)
1830 continue;
1831 /* add timing values, setup DMA if needed */
1832 tim = cmd0643_6_data_tim_pio[drvp->PIO_mode];
1833 if (drvp->drive_flags & DRIVE_DMA) {
1834 /*
1835 * use Multiword DMA.
1836 * Timings will be used for both PIO and DMA, so adjust
1837 * DMA mode if needed
1838 */
1839 if (drvp->PIO_mode >= 3 &&
1840 (drvp->DMA_mode + 2) > drvp->PIO_mode) {
1841 drvp->DMA_mode = drvp->PIO_mode - 2;
1842 }
1843 tim = cmd0643_6_data_tim_dma[drvp->DMA_mode];
1844 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1845 }
1846 pciide_pci_write(sc->sc_pc, sc->sc_tag,
1847 CMD_DATA_TIM(chp->channel, drive), tim);
1848 }
1849 if (idedma_ctl != 0) {
1850 /* Add software bits in status register */
1851 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1852 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1853 idedma_ctl);
1854 }
1855 pciide_print_modes(cp);
1856 }
1857
1858 void
1859 cy693_setup_cap(sc)
1860 struct pciide_softc *sc;
1861 {
1862 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1863 WDC_CAPABILITY_DMA;
1864 sc->sc_wdcdev.PIO_cap = 4;
1865 sc->sc_wdcdev.DMA_cap = 2;
1866 sc->sc_wdcdev.set_modes = cy693_setup_channel;
1867 }
1868
1869 void
1870 cy693_setup_chip(sc)
1871 struct pciide_softc *sc;
1872 {
1873 WDCDEBUG_PRINT(("cy693_setup_chip: old timings reg 0x%x\n",
1874 pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),
1875 DEBUG_PROBE);
1876 cy693_setup_channel(&sc->pciide_channels[0].wdc_channel);
1877 WDCDEBUG_PRINT(("cy693_setup_chip: new timings reg 0x%x\n",
1878 pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
1879 }
1880
1881 void
1882 cy693_setup_channel(chp)
1883 struct channel_softc *chp;
1884 {
1885 struct ata_drive_datas *drvp;
1886 int drive;
1887 u_int32_t cy_cmd_ctrl;
1888 u_int32_t idedma_ctl;
1889 struct pciide_channel *cp = (struct pciide_channel*)chp;
1890 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1891
1892 cy_cmd_ctrl = idedma_ctl = 0;
1893
1894 /* setup DMA if needed */
1895 pciide_channel_dma_setup(cp);
1896
1897 for (drive = 0; drive < 2; drive++) {
1898 drvp = &chp->ch_drive[drive];
1899 /* If no drive, skip */
1900 if ((drvp->drive_flags & DRIVE) == 0)
1901 continue;
1902 /* add timing values, setup DMA if needed */
1903 if (drvp->drive_flags & DRIVE_DMA) {
1904 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1905 /*
1906 * use Multiword DMA
1907 * Timings will be used for both PIO and DMA, so adjust
1908 * DMA mode if needed
1909 */
1910 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
1911 drvp->PIO_mode = drvp->DMA_mode + 2;
1912 if (drvp->DMA_mode == 0)
1913 drvp->PIO_mode = 0;
1914 }
1915 cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
1916 CY_CMD_CTRL_IOW_PULSE_OFF(drive));
1917 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
1918 CY_CMD_CTRL_IOW_REC_OFF(drive));
1919 cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
1920 CY_CMD_CTRL_IOR_PULSE_OFF(drive));
1921 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
1922 CY_CMD_CTRL_IOR_REC_OFF(drive));
1923 }
1924 pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
1925 pciide_print_modes(cp);
1926 if (idedma_ctl != 0) {
1927 /* Add software bits in status register */
1928 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1929 IDEDMA_CTL, idedma_ctl);
1930 }
1931 }
1932
1933 void
1934 cy693_channel_map(pa, cp)
1935 struct pci_attach_args *pa;
1936 struct pciide_channel *cp;
1937 {
1938 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1939 bus_size_t cmdsize, ctlsize;
1940 struct channel_softc *wdc_cp = &cp->wdc_channel;
1941 int interface =
1942 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1943 int compatchan;
1944
1945 #ifdef DIAGNOSTIC
1946 if (wdc_cp->channel != 0)
1947 panic("cy693_channel_map: channel %d", wdc_cp->channel);
1948 #endif
1949
1950 /*
1951 * this chip has 2 PCI IDE functions, one for primary and one for
1952 * secondary. So we need to call pciide_mapregs_compat() with
1953 * the real channel
1954 */
1955 if (pa->pa_function == 1) {
1956 compatchan = 0;
1957 } else if (pa->pa_function == 2) {
1958 compatchan = 1;
1959 } else {
1960 printf("%s: unexpected PCI function %d\n",
1961 sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
1962 cp->hw_ok = 0;
1963 return;
1964 }
1965
1966 /* Only one channel for this chip; if we are here it's enabled */
1967 if (interface & PCIIDE_INTERFACE_PCI(0))
1968 cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize);
1969 else
1970 cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
1971 &cmdsize, &ctlsize);
1972 if (cp->hw_ok == 0)
1973 return;
1974 wdc_cp->data32iot = wdc_cp->cmd_iot;
1975 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1976 wdcattach(wdc_cp);
1977 if (pciiide_chan_candisable(cp)) {
1978 pci_conf_write(sc->sc_pc, sc->sc_tag,
1979 PCI_COMMAND_STATUS_REG, 0);
1980 }
1981 pciide_map_compat_intr(pa, cp, compatchan, interface);
1982 }
1983
1984 void
1985 sis_setup_cap(sc)
1986 struct pciide_softc *sc;
1987 {
1988 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1989 WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
1990 sc->sc_wdcdev.PIO_cap = 4;
1991 sc->sc_wdcdev.DMA_cap = 2;
1992 sc->sc_wdcdev.UDMA_cap = 2;
1993 sc->sc_wdcdev.set_modes = sis_setup_channel;
1994 }
1995
1996 void
1997 sis_setup_chip(sc)
1998 struct pciide_softc *sc;
1999 {
2000 int channel;
2001
2002 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2003 sis_setup_channel(&sc->pciide_channels[channel].wdc_channel);
2004 }
2005 pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
2006 pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
2007 SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
2008 }
2009
2010 void
2011 sis_setup_channel(chp)
2012 struct channel_softc *chp;
2013 {
2014 struct ata_drive_datas *drvp;
2015 int drive;
2016 u_int32_t sis_tim;
2017 u_int32_t idedma_ctl;
2018 struct pciide_channel *cp = (struct pciide_channel*)chp;
2019 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2020
2021 WDCDEBUG_PRINT(("sis_setup_chip: old timings reg for "
2022 "channel %d 0x%x\n", chp->channel,
2023 pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
2024 DEBUG_PROBE);
2025 sis_tim = 0;
2026 idedma_ctl = 0;
2027 /* setup DMA if needed */
2028 pciide_channel_dma_setup(cp);
2029
2030 for (drive = 0; drive < 2; drive++) {
2031 drvp = &chp->ch_drive[drive];
2032 /* If no drive, skip */
2033 if ((drvp->drive_flags & DRIVE) == 0)
2034 continue;
2035 /* add timing values, setup DMA if needed */
2036 if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
2037 (drvp->drive_flags & DRIVE_UDMA) == 0)
2038 goto pio;
2039
2040 if (drvp->drive_flags & DRIVE_UDMA) {
2041 /* use Ultra/DMA */
2042 drvp->drive_flags &= ~DRIVE_DMA;
2043 sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
2044 SIS_TIM_UDMA_TIME_OFF(drive);
2045 sis_tim |= SIS_TIM_UDMA_EN(drive);
2046 } else {
2047 /*
2048 * use Multiword DMA
2049 * Timings will be used for both PIO and DMA,
2050 * so adjust DMA mode if needed
2051 */
2052 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
2053 drvp->PIO_mode = drvp->DMA_mode + 2;
2054 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
2055 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
2056 drvp->PIO_mode - 2 : 0;
2057 if (drvp->DMA_mode == 0)
2058 drvp->PIO_mode = 0;
2059 }
2060 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2061 pio: sis_tim |= sis_pio_act[drvp->PIO_mode] <<
2062 SIS_TIM_ACT_OFF(drive);
2063 sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
2064 SIS_TIM_REC_OFF(drive);
2065 }
2066 WDCDEBUG_PRINT(("sis_setup_chip: new timings reg for "
2067 "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
2068 pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
2069 if (idedma_ctl != 0) {
2070 /* Add software bits in status register */
2071 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2072 IDEDMA_CTL, idedma_ctl);
2073 }
2074 pciide_print_modes(cp);
2075 }
2076
2077 void
2078 sis_channel_map(pa, cp)
2079 struct pci_attach_args *pa;
2080 struct pciide_channel *cp;
2081 {
2082 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2083 bus_size_t cmdsize, ctlsize;
2084 struct channel_softc *wdc_cp = &cp->wdc_channel;
2085 u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
2086 int interface =
2087 PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
2088
2089 if ((wdc_cp->channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
2090 (wdc_cp->channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
2091 printf("%s: %s channel ignored (disabled)\n",
2092 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2093 return;
2094 }
2095
2096 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
2097 if (cp->hw_ok == 0)
2098 return;
2099 if (pciiide_chan_candisable(cp)) {
2100 if (wdc_cp->channel == 0)
2101 sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
2102 else
2103 sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
2104 pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0, sis_ctr0);
2105 }
2106 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
2107 }
2108
2109 void
2110 acer_setup_cap(sc)
2111 struct pciide_softc *sc;
2112 {
2113 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
2114 WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
2115 sc->sc_wdcdev.PIO_cap = 4;
2116 sc->sc_wdcdev.DMA_cap = 2;
2117 sc->sc_wdcdev.UDMA_cap = 2;
2118 sc->sc_wdcdev.set_modes = acer_setup_channel;
2119 }
2120
2121 void
2122 acer_setup_chip(sc)
2123 struct pciide_softc *sc;
2124 {
2125 int channel;
2126
2127 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
2128 (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
2129 ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
2130
2131 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2132 acer_setup_channel(&sc->pciide_channels[channel].wdc_channel);
2133 }
2134 }
2135
2136 void
2137 acer_setup_channel(chp)
2138 struct channel_softc *chp;
2139 {
2140 struct ata_drive_datas *drvp;
2141 int drive;
2142 u_int32_t acer_fifo_udma;
2143 u_int32_t idedma_ctl;
2144 struct pciide_channel *cp = (struct pciide_channel*)chp;
2145 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2146
2147 idedma_ctl = 0;
2148 acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
2149 WDCDEBUG_PRINT(("acer_setup_chip: old fifo/udma reg 0x%x\n",
2150 acer_fifo_udma), DEBUG_PROBE);
2151 /* setup DMA if needed */
2152 pciide_channel_dma_setup(cp);
2153
2154 for (drive = 0; drive < 2; drive++) {
2155 drvp = &chp->ch_drive[drive];
2156 /* If no drive, skip */
2157 if ((drvp->drive_flags & DRIVE) == 0)
2158 continue;
2159 WDCDEBUG_PRINT(("acer_setup_chip: old timings reg for "
2160 "channel %d drive %d 0x%x\n", chp->channel, drive,
2161 pciide_pci_read(sc->sc_pc, sc->sc_tag,
2162 ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
2163 /* clear FIFO/DMA mode */
2164 acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
2165 ACER_UDMA_EN(chp->channel, drive) |
2166 ACER_UDMA_TIM(chp->channel, drive, 0x7));
2167
2168 /* add timing values, setup DMA if needed */
2169 if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
2170 (drvp->drive_flags & DRIVE_UDMA) == 0) {
2171 acer_fifo_udma |=
2172 ACER_FTH_OPL(chp->channel, drive, 0x1);
2173 goto pio;
2174 }
2175
2176 acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
2177 if (drvp->drive_flags & DRIVE_UDMA) {
2178 /* use Ultra/DMA */
2179 drvp->drive_flags &= ~DRIVE_DMA;
2180 acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
2181 acer_fifo_udma |=
2182 ACER_UDMA_TIM(chp->channel, drive,
2183 acer_udma[drvp->UDMA_mode]);
2184 } else {
2185 /*
2186 * use Multiword DMA
2187 * Timings will be used for both PIO and DMA,
2188 * so adjust DMA mode if needed
2189 */
2190 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
2191 drvp->PIO_mode = drvp->DMA_mode + 2;
2192 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
2193 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
2194 drvp->PIO_mode - 2 : 0;
2195 if (drvp->DMA_mode == 0)
2196 drvp->PIO_mode = 0;
2197 }
2198 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2199 pio: pciide_pci_write(sc->sc_pc, sc->sc_tag,
2200 ACER_IDETIM(chp->channel, drive),
2201 acer_pio[drvp->PIO_mode]);
2202 }
2203 WDCDEBUG_PRINT(("acer_setup_chip: new fifo/udma reg 0x%x\n",
2204 acer_fifo_udma), DEBUG_PROBE);
2205 pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
2206 if (idedma_ctl != 0) {
2207 /* Add software bits in status register */
2208 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2209 IDEDMA_CTL, idedma_ctl);
2210 }
2211 pciide_print_modes(cp);
2212 }
2213
2214 void
2215 acer_channel_map(pa, cp)
2216 struct pci_attach_args *pa;
2217 struct pciide_channel *cp;
2218 {
2219 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2220 bus_size_t cmdsize, ctlsize;
2221 struct channel_softc *wdc_cp = &cp->wdc_channel;
2222 u_int32_t cr;
2223 int interface;
2224
2225 /*
2226 * Enable "microsoft register bits" R/W. Will be done 2 times
2227 * (one for each channel) but should'nt be a problem. There's no
2228 * better place where to put this.
2229 */
2230 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
2231 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
2232 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
2233 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
2234 ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
2235 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
2236 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
2237 ~ACER_CHANSTATUSREGS_RO);
2238 cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
2239 cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
2240 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
2241 /* Don't use cr, re-read the real register content instead */
2242 interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
2243 PCI_CLASS_REG));
2244
2245 if ((interface & PCIIDE_CHAN_EN(wdc_cp->channel)) == 0) {
2246 printf("%s: %s channel ignored (disabled)\n",
2247 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2248 return;
2249 }
2250
2251 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize);
2252 if (cp->hw_ok == 0)
2253 return;
2254 if (pciiide_chan_candisable(cp)) {
2255 cr &= ~(PCIIDE_CHAN_EN(wdc_cp->channel) << PCI_INTERFACE_SHIFT);
2256 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
2257 }
2258 pciide_map_compat_intr(pa, cp, wdc_cp->channel, interface);
2259 }
2260