pciide.c revision 1.33.2.10 1 /* $NetBSD: pciide.c,v 1.33.2.10 2000/08/14 14:15:21 he Exp $ */
2
3
4 /*
5 * Copyright (c) 1999 Manuel Bouyer.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36
37 /*
38 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Christopher G. Demetriou
51 * for the NetBSD Project.
52 * 4. The name of the author may not be used to endorse or promote products
53 * derived from this software without specific prior written permission
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
56 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
59 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
60 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
64 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 */
66
67 /*
68 * PCI IDE controller driver.
69 *
70 * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
71 * sys/dev/pci/ppb.c, revision 1.16).
72 *
73 * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
74 * "Programming Interface for Bus Master IDE Controller, Revision 1.0
75 * 5/16/94" from the PCI SIG.
76 *
77 */
78
79 #ifndef WDCDEBUG
80 #define WDCDEBUG
81 #endif
82
83 #define DEBUG_DMA 0x01
84 #define DEBUG_XFERS 0x02
85 #define DEBUG_FUNCS 0x08
86 #define DEBUG_PROBE 0x10
87 #ifdef WDCDEBUG
88 int wdcdebug_pciide_mask = 0;
89 #define WDCDEBUG_PRINT(args, level) \
90 if (wdcdebug_pciide_mask & (level)) printf args
91 #else
92 #define WDCDEBUG_PRINT(args, level)
93 #endif
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/device.h>
97 #include <sys/malloc.h>
98
99 #include <machine/endian.h>
100
101 #include <vm/vm.h>
102 #include <vm/vm_param.h>
103 #include <vm/vm_kern.h>
104
105 #include <dev/pci/pcireg.h>
106 #include <dev/pci/pcivar.h>
107 #include <dev/pci/pcidevs.h>
108 #include <dev/pci/pciidereg.h>
109 #include <dev/pci/pciidevar.h>
110 #include <dev/pci/pciide_piix_reg.h>
111 #include <dev/pci/pciide_amd_reg.h>
112 #include <dev/pci/pciide_apollo_reg.h>
113 #include <dev/pci/pciide_cmd_reg.h>
114 #include <dev/pci/pciide_cy693_reg.h>
115 #include <dev/pci/pciide_sis_reg.h>
116 #include <dev/pci/pciide_acer_reg.h>
117 #include <dev/pci/pciide_pdc202xx_reg.h>
118 #include <dev/pci/pciide_opti_reg.h>
119 #include <dev/pci/pciide_hpt_reg.h>
120
121 #if BYTE_ORDER == BIG_ENDIAN
122 #define htole16(x) bswap16((u_int16_t)(x))
123 #define htole32(x) bswap32((u_int32_t)(x))
124 #define htole64(x) bswap64((u_int64_t)(x))
125 #else /* LITTLE_ENDIAN */
126 #define htole16(x) (x)
127 #define htole32(x) (x)
128 #define htole64(x) (x)
129 #endif
130 #define le16toh(x) htole16(x)
131 #define le32toh(x) htole32(x)
132 #define le64toh(x) htole64(x)
133
134 /* inlines for reading/writing 8-bit PCI registers */
135 static __inline u_int8_t pciide_pci_read __P((pci_chipset_tag_t, pcitag_t,
136 int));
137 static __inline void pciide_pci_write __P((pci_chipset_tag_t, pcitag_t,
138 int, u_int8_t));
139
140 static __inline u_int8_t
141 pciide_pci_read(pc, pa, reg)
142 pci_chipset_tag_t pc;
143 pcitag_t pa;
144 int reg;
145 {
146
147 return (pci_conf_read(pc, pa, (reg & ~0x03)) >>
148 ((reg & 0x03) * 8) & 0xff);
149 }
150
151 static __inline void
152 pciide_pci_write(pc, pa, reg, val)
153 pci_chipset_tag_t pc;
154 pcitag_t pa;
155 int reg;
156 u_int8_t val;
157 {
158 pcireg_t pcival;
159
160 pcival = pci_conf_read(pc, pa, (reg & ~0x03));
161 pcival &= ~(0xff << ((reg & 0x03) * 8));
162 pcival |= (val << ((reg & 0x03) * 8));
163 pci_conf_write(pc, pa, (reg & ~0x03), pcival);
164 }
165
166 void default_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
167
168 void piix_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
169 void piix_setup_channel __P((struct channel_softc*));
170 void piix3_4_setup_channel __P((struct channel_softc*));
171 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
172 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
173 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
174
175 void amd756_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
176 void amd756_setup_channel __P((struct channel_softc*));
177
178 void apollo_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
179 void apollo_setup_channel __P((struct channel_softc*));
180
181 void cmd_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
182 void cmd0643_9_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
183 void cmd0643_9_setup_channel __P((struct channel_softc*));
184 void cmd_channel_map __P((struct pci_attach_args *,
185 struct pciide_softc *, int));
186 int cmd_pci_intr __P((void *));
187 void cmd646_9_irqack __P((struct channel_softc *));
188
189 void cy693_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
190 void cy693_setup_channel __P((struct channel_softc*));
191
192 void sis_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
193 void sis_setup_channel __P((struct channel_softc*));
194
195 void acer_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
196 void acer_setup_channel __P((struct channel_softc*));
197 int acer_pci_intr __P((void *));
198
199 void pdc202xx_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
200 void pdc202xx_setup_channel __P((struct channel_softc*));
201 int pdc202xx_pci_intr __P((void *));
202
203 void opti_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
204 void opti_setup_channel __P((struct channel_softc*));
205
206 void hpt_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
207 void hpt_setup_channel __P((struct channel_softc*));
208 int hpt_pci_intr __P((void *));
209
210 void pciide_channel_dma_setup __P((struct pciide_channel *));
211 int pciide_dma_table_setup __P((struct pciide_softc*, int, int));
212 int pciide_dma_init __P((void*, int, int, void *, size_t, int));
213 void pciide_dma_start __P((void*, int, int));
214 int pciide_dma_finish __P((void*, int, int, int));
215 void pciide_irqack __P((struct channel_softc *));
216 void pciide_print_modes __P((struct pciide_channel *));
217
218 struct pciide_product_desc {
219 u_int32_t ide_product;
220 int ide_flags;
221 const char *ide_name;
222 /* map and setup chip, probe drives */
223 void (*chip_map) __P((struct pciide_softc*, struct pci_attach_args*));
224 };
225
226 /* Flags for ide_flags */
227 #define IDE_PCI_CLASS_OVERRIDE 0x0001 /* accept even if class != pciide */
228
229 /* Default product description for devices not known from this controller */
230 const struct pciide_product_desc default_product_desc = {
231 0,
232 0,
233 "Generic PCI IDE controller",
234 default_chip_map,
235 };
236
237 const struct pciide_product_desc pciide_intel_products[] = {
238 { PCI_PRODUCT_INTEL_82092AA,
239 0,
240 "Intel 82092AA IDE controller",
241 default_chip_map,
242 },
243 { PCI_PRODUCT_INTEL_82371FB_IDE,
244 0,
245 "Intel 82371FB IDE controller (PIIX)",
246 piix_chip_map,
247 },
248 { PCI_PRODUCT_INTEL_82371SB_IDE,
249 0,
250 "Intel 82371SB IDE Interface (PIIX3)",
251 piix_chip_map,
252 },
253 { PCI_PRODUCT_INTEL_82371AB_IDE,
254 0,
255 "Intel 82371AB IDE controller (PIIX4)",
256 piix_chip_map,
257 },
258 { PCI_PRODUCT_INTEL_82801AA_IDE,
259 0,
260 "Intel 82801AA IDE Controller (ICH)",
261 piix_chip_map,
262 },
263 { PCI_PRODUCT_INTEL_82801AB_IDE,
264 0,
265 "Intel 82801AB IDE Controller (ICH0)",
266 piix_chip_map,
267 },
268 { 0,
269 0,
270 NULL,
271 }
272 };
273
274 const struct pciide_product_desc pciide_amd_products[] = {
275 { PCI_PRODUCT_AMD_PBC756_IDE,
276 0,
277 "Advanced Micro Devices AMD756 IDE Controller",
278 amd756_chip_map
279 },
280 { 0,
281 0,
282 NULL,
283 }
284 };
285
286 const struct pciide_product_desc pciide_cmd_products[] = {
287 { PCI_PRODUCT_CMDTECH_640,
288 0,
289 "CMD Technology PCI0640",
290 cmd_chip_map
291 },
292 { PCI_PRODUCT_CMDTECH_643,
293 0,
294 "CMD Technology PCI0643",
295 cmd0643_9_chip_map,
296 },
297 { PCI_PRODUCT_CMDTECH_646,
298 0,
299 "CMD Technology PCI0646",
300 cmd0643_9_chip_map,
301 },
302 { PCI_PRODUCT_CMDTECH_648,
303 IDE_PCI_CLASS_OVERRIDE,
304 "CMD Technology PCI0648",
305 cmd0643_9_chip_map,
306 },
307 { PCI_PRODUCT_CMDTECH_649,
308 IDE_PCI_CLASS_OVERRIDE,
309 "CMD Technology PCI0649",
310 cmd0643_9_chip_map,
311 },
312 { 0,
313 0,
314 NULL,
315 }
316 };
317
318 const struct pciide_product_desc pciide_via_products[] = {
319 { PCI_PRODUCT_VIATECH_VT82C586_IDE,
320 0,
321 "VIA Tech VT82C586 IDE Controller",
322 apollo_chip_map,
323 },
324 { PCI_PRODUCT_VIATECH_VT82C586A_IDE,
325 0,
326 "VIA Tech VT82C586A IDE Controller",
327 apollo_chip_map,
328 },
329 { 0,
330 0,
331 NULL,
332 }
333 };
334
335 const struct pciide_product_desc pciide_cypress_products[] = {
336 { PCI_PRODUCT_CONTAQ_82C693,
337 0,
338 "Cypress 82C693 IDE Controller",
339 cy693_chip_map,
340 },
341 { 0,
342 0,
343 NULL,
344 }
345 };
346
347 const struct pciide_product_desc pciide_sis_products[] = {
348 { PCI_PRODUCT_SIS_5597_IDE,
349 0,
350 "Silicon Integrated System 5597/5598 IDE controller",
351 sis_chip_map,
352 },
353 { 0,
354 0,
355 NULL,
356 }
357 };
358
359 const struct pciide_product_desc pciide_acer_products[] = {
360 { PCI_PRODUCT_ALI_M5229,
361 0,
362 "Acer Labs M5229 UDMA IDE Controller",
363 acer_chip_map,
364 },
365 { 0,
366 0,
367 NULL,
368 }
369 };
370
371 const struct pciide_product_desc pciide_promise_products[] = {
372 { PCI_PRODUCT_PROMISE_ULTRA33,
373 IDE_PCI_CLASS_OVERRIDE,
374 "Promise Ultra33/ATA Bus Master IDE Accelerator",
375 pdc202xx_chip_map,
376 },
377 { PCI_PRODUCT_PROMISE_ULTRA66,
378 IDE_PCI_CLASS_OVERRIDE,
379 "Promise Ultra66/ATA Bus Master IDE Accelerator",
380 pdc202xx_chip_map,
381 },
382 { PCI_PRODUCT_PROMISE_ULTRA100,
383 IDE_PCI_CLASS_OVERRIDE,
384 "Promise Ultra100/ATA Bus Master IDE Accelerator",
385 pdc202xx_chip_map,
386 },
387 { 0,
388 0,
389 NULL,
390 }
391 };
392
393 const struct pciide_product_desc pciide_opti_products[] = {
394 { PCI_PRODUCT_OPTI_82C621,
395 0,
396 "OPTi 82c621 PCI IDE controller",
397 opti_chip_map,
398 },
399 { PCI_PRODUCT_OPTI_82C568,
400 0,
401 "OPTi 82c568 (82c621 compatible) PCI IDE controller",
402 opti_chip_map,
403 },
404 { PCI_PRODUCT_OPTI_82D568,
405 0,
406 "OPTi 82d568 (82c621 compatible) PCI IDE controller",
407 opti_chip_map,
408 },
409 { 0,
410 0,
411 NULL,
412 }
413 };
414
415 const struct pciide_product_desc pciide_triones_products[] = {
416 { PCI_PRODUCT_TRIONES_HPT366,
417 IDE_PCI_CLASS_OVERRIDE,
418 "Triones/Highpoint HPT366/370 IDE Controller",
419 hpt_chip_map,
420 },
421 { 0,
422 0,
423 NULL,
424 }
425 };
426
427 struct pciide_vendor_desc {
428 u_int32_t ide_vendor;
429 const struct pciide_product_desc *ide_products;
430 };
431
432 const struct pciide_vendor_desc pciide_vendors[] = {
433 { PCI_VENDOR_INTEL, pciide_intel_products },
434 { PCI_VENDOR_CMDTECH, pciide_cmd_products },
435 { PCI_VENDOR_VIATECH, pciide_via_products },
436 { PCI_VENDOR_CONTAQ, pciide_cypress_products },
437 { PCI_VENDOR_SIS, pciide_sis_products },
438 { PCI_VENDOR_ALI, pciide_acer_products },
439 { PCI_VENDOR_PROMISE, pciide_promise_products },
440 { PCI_VENDOR_AMD, pciide_amd_products },
441 { PCI_VENDOR_OPTI, pciide_opti_products },
442 { PCI_VENDOR_TRIONES, pciide_triones_products },
443 { 0, NULL }
444 };
445
446 /* options passed via the 'flags' config keyword */
447 #define PCIIDE_OPTIONS_DMA 0x01
448
449 int pciide_match __P((struct device *, struct cfdata *, void *));
450 void pciide_attach __P((struct device *, struct device *, void *));
451
452 struct cfattach pciide_ca = {
453 sizeof(struct pciide_softc), pciide_match, pciide_attach
454 };
455 int pciide_chipen __P((struct pciide_softc *, struct pci_attach_args *));
456 int pciide_mapregs_compat __P(( struct pci_attach_args *,
457 struct pciide_channel *, int, bus_size_t *, bus_size_t*));
458 int pciide_mapregs_native __P((struct pci_attach_args *,
459 struct pciide_channel *, bus_size_t *, bus_size_t *,
460 int (*pci_intr) __P((void *))));
461 void pciide_mapreg_dma __P((struct pciide_softc *,
462 struct pci_attach_args *));
463 int pciide_chansetup __P((struct pciide_softc *, int, pcireg_t));
464 void pciide_mapchan __P((struct pci_attach_args *,
465 struct pciide_channel *, pcireg_t, bus_size_t *, bus_size_t *,
466 int (*pci_intr) __P((void *))));
467 int pciide_chan_candisable __P((struct pciide_channel *));
468 void pciide_map_compat_intr __P(( struct pci_attach_args *,
469 struct pciide_channel *, int, int));
470 int pciide_print __P((void *, const char *pnp));
471 int pciide_compat_intr __P((void *));
472 int pciide_pci_intr __P((void *));
473 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
474
475 const struct pciide_product_desc *
476 pciide_lookup_product(id)
477 u_int32_t id;
478 {
479 const struct pciide_product_desc *pp;
480 const struct pciide_vendor_desc *vp;
481
482 for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
483 if (PCI_VENDOR(id) == vp->ide_vendor)
484 break;
485
486 if ((pp = vp->ide_products) == NULL)
487 return NULL;
488
489 for (; pp->ide_name != NULL; pp++)
490 if (PCI_PRODUCT(id) == pp->ide_product)
491 break;
492
493 if (pp->ide_name == NULL)
494 return NULL;
495 return pp;
496 }
497
498 int
499 pciide_match(parent, match, aux)
500 struct device *parent;
501 struct cfdata *match;
502 void *aux;
503 {
504 struct pci_attach_args *pa = aux;
505 const struct pciide_product_desc *pp;
506
507 /*
508 * Check the ID register to see that it's a PCI IDE controller.
509 * If it is, we assume that we can deal with it; it _should_
510 * work in a standardized way...
511 */
512 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
513 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
514 return (1);
515 }
516
517 /*
518 * Some controllers (e.g. promise Utra-33) don't claim to be PCI IDE
519 * controllers. Let see if we can deal with it anyway.
520 */
521 pp = pciide_lookup_product(pa->pa_id);
522 if (pp && (pp->ide_flags & IDE_PCI_CLASS_OVERRIDE)) {
523 return (1);
524 }
525
526 return (0);
527 }
528
529 void
530 pciide_attach(parent, self, aux)
531 struct device *parent, *self;
532 void *aux;
533 {
534 struct pci_attach_args *pa = aux;
535 pci_chipset_tag_t pc = pa->pa_pc;
536 pcitag_t tag = pa->pa_tag;
537 struct pciide_softc *sc = (struct pciide_softc *)self;
538 pcireg_t csr;
539 char devinfo[256];
540 const char *displaydev;
541
542 sc->sc_pp = pciide_lookup_product(pa->pa_id);
543 if (sc->sc_pp == NULL) {
544 sc->sc_pp = &default_product_desc;
545 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
546 displaydev = devinfo;
547 } else
548 displaydev = sc->sc_pp->ide_name;
549
550 printf(": %s (rev. 0x%02x)\n", displaydev, PCI_REVISION(pa->pa_class));
551
552 sc->sc_pc = pa->pa_pc;
553 sc->sc_tag = pa->pa_tag;
554 #ifdef WDCDEBUG
555 if (wdcdebug_pciide_mask & DEBUG_PROBE)
556 pci_conf_print(sc->sc_pc, sc->sc_tag, NULL);
557 #endif
558 sc->sc_pp->chip_map(sc, pa);
559
560 if (sc->sc_dma_ok) {
561 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
562 csr |= PCI_COMMAND_MASTER_ENABLE;
563 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
564 }
565 WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
566 pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
567 }
568
569 /* tell wether the chip is enabled or not */
570 int
571 pciide_chipen(sc, pa)
572 struct pciide_softc *sc;
573 struct pci_attach_args *pa;
574 {
575 pcireg_t csr;
576 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
577 csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
578 PCI_COMMAND_STATUS_REG);
579 printf("%s: device disabled (at %s)\n",
580 sc->sc_wdcdev.sc_dev.dv_xname,
581 (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
582 "device" : "bridge");
583 return 0;
584 }
585 return 1;
586 }
587
588 int
589 pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
590 struct pci_attach_args *pa;
591 struct pciide_channel *cp;
592 int compatchan;
593 bus_size_t *cmdsizep, *ctlsizep;
594 {
595 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
596 struct channel_softc *wdc_cp = &cp->wdc_channel;
597
598 cp->compat = 1;
599 *cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
600 *ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
601
602 wdc_cp->cmd_iot = pa->pa_iot;
603 if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
604 PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
605 printf("%s: couldn't map %s channel cmd regs\n",
606 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
607 return (0);
608 }
609
610 wdc_cp->ctl_iot = pa->pa_iot;
611 if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
612 PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
613 printf("%s: couldn't map %s channel ctl regs\n",
614 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
615 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
616 PCIIDE_COMPAT_CMD_SIZE);
617 return (0);
618 }
619
620 return (1);
621 }
622
623 int
624 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr)
625 struct pci_attach_args * pa;
626 struct pciide_channel *cp;
627 bus_size_t *cmdsizep, *ctlsizep;
628 int (*pci_intr) __P((void *));
629 {
630 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
631 struct channel_softc *wdc_cp = &cp->wdc_channel;
632 const char *intrstr;
633 pci_intr_handle_t intrhandle;
634
635 cp->compat = 0;
636
637 if (sc->sc_pci_ih == NULL) {
638 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
639 pa->pa_intrline, &intrhandle) != 0) {
640 printf("%s: couldn't map native-PCI interrupt\n",
641 sc->sc_wdcdev.sc_dev.dv_xname);
642 return 0;
643 }
644 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
645 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
646 intrhandle, IPL_BIO, pci_intr, sc);
647 if (sc->sc_pci_ih != NULL) {
648 printf("%s: using %s for native-PCI interrupt\n",
649 sc->sc_wdcdev.sc_dev.dv_xname,
650 intrstr ? intrstr : "unknown interrupt");
651 } else {
652 printf("%s: couldn't establish native-PCI interrupt",
653 sc->sc_wdcdev.sc_dev.dv_xname);
654 if (intrstr != NULL)
655 printf(" at %s", intrstr);
656 printf("\n");
657 return 0;
658 }
659 }
660 cp->ih = sc->sc_pci_ih;
661 if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->channel),
662 PCI_MAPREG_TYPE_IO, 0,
663 &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, cmdsizep) != 0) {
664 printf("%s: couldn't map %s channel cmd regs\n",
665 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
666 return 0;
667 }
668
669 if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->channel),
670 PCI_MAPREG_TYPE_IO, 0,
671 &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, ctlsizep) != 0) {
672 printf("%s: couldn't map %s channel ctl regs\n",
673 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
674 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, *cmdsizep);
675 return 0;
676 }
677 return (1);
678 }
679
680 void
681 pciide_mapreg_dma(sc, pa)
682 struct pciide_softc *sc;
683 struct pci_attach_args *pa;
684 {
685 /*
686 * Map DMA registers
687 *
688 * Note that sc_dma_ok is the right variable to test to see if
689 * DMA can be done. If the interface doesn't support DMA,
690 * sc_dma_ok will never be non-zero. If the DMA regs couldn't
691 * be mapped, it'll be zero. I.e., sc_dma_ok will only be
692 * non-zero if the interface supports DMA and the registers
693 * could be mapped.
694 *
695 * XXX Note that despite the fact that the Bus Master IDE specs
696 * XXX say that "The bus master IDE function uses 16 bytes of IO
697 * XXX space," some controllers (at least the United
698 * XXX Microelectronics UM8886BF) place it in memory space.
699 */
700 sc->sc_dma_ok = (pci_mapreg_map(pa,
701 PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
702 &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
703 sc->sc_dmat = pa->pa_dmat;
704 if (sc->sc_dma_ok == 0) {
705 printf(", but unused (couldn't map registers)");
706 } else {
707 sc->sc_wdcdev.dma_arg = sc;
708 sc->sc_wdcdev.dma_init = pciide_dma_init;
709 sc->sc_wdcdev.dma_start = pciide_dma_start;
710 sc->sc_wdcdev.dma_finish = pciide_dma_finish;
711 }
712 }
713
714 int
715 pciide_compat_intr(arg)
716 void *arg;
717 {
718 struct pciide_channel *cp = arg;
719
720 #ifdef DIAGNOSTIC
721 /* should only be called for a compat channel */
722 if (cp->compat == 0)
723 panic("pciide compat intr called for non-compat chan %p\n", cp);
724 #endif
725 return (wdcintr(&cp->wdc_channel));
726 }
727
728 int
729 pciide_pci_intr(arg)
730 void *arg;
731 {
732 struct pciide_softc *sc = arg;
733 struct pciide_channel *cp;
734 struct channel_softc *wdc_cp;
735 int i, rv, crv;
736
737 rv = 0;
738 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
739 cp = &sc->pciide_channels[i];
740 wdc_cp = &cp->wdc_channel;
741
742 /* If a compat channel skip. */
743 if (cp->compat)
744 continue;
745 /* if this channel not waiting for intr, skip */
746 if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
747 continue;
748
749 crv = wdcintr(wdc_cp);
750 if (crv == 0)
751 ; /* leave rv alone */
752 else if (crv == 1)
753 rv = 1; /* claim the intr */
754 else if (rv == 0) /* crv should be -1 in this case */
755 rv = crv; /* if we've done no better, take it */
756 }
757 return (rv);
758 }
759
760 void
761 pciide_channel_dma_setup(cp)
762 struct pciide_channel *cp;
763 {
764 int drive;
765 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
766 struct ata_drive_datas *drvp;
767
768 for (drive = 0; drive < 2; drive++) {
769 drvp = &cp->wdc_channel.ch_drive[drive];
770 /* If no drive, skip */
771 if ((drvp->drive_flags & DRIVE) == 0)
772 continue;
773 /* setup DMA if needed */
774 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
775 (drvp->drive_flags & DRIVE_UDMA) == 0) ||
776 sc->sc_dma_ok == 0) {
777 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
778 continue;
779 }
780 if (pciide_dma_table_setup(sc, cp->wdc_channel.channel, drive)
781 != 0) {
782 /* Abort DMA setup */
783 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
784 continue;
785 }
786 }
787 }
788
789 int
790 pciide_dma_table_setup(sc, channel, drive)
791 struct pciide_softc *sc;
792 int channel, drive;
793 {
794 bus_dma_segment_t seg;
795 int error, rseg;
796 const bus_size_t dma_table_size =
797 sizeof(struct idedma_table) * NIDEDMA_TABLES;
798 struct pciide_dma_maps *dma_maps =
799 &sc->pciide_channels[channel].dma_maps[drive];
800
801 /* If table was already allocated, just return */
802 if (dma_maps->dma_table)
803 return 0;
804
805 /* Allocate memory for the DMA tables and map it */
806 if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
807 IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
808 BUS_DMA_NOWAIT)) != 0) {
809 printf("%s:%d: unable to allocate table DMA for "
810 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
811 channel, drive, error);
812 return error;
813 }
814 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
815 dma_table_size,
816 (caddr_t *)&dma_maps->dma_table,
817 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
818 printf("%s:%d: unable to map table DMA for"
819 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
820 channel, drive, error);
821 return error;
822 }
823 WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
824 "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
825 seg.ds_addr), DEBUG_PROBE);
826
827 /* Create and load table DMA map for this disk */
828 if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
829 1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
830 &dma_maps->dmamap_table)) != 0) {
831 printf("%s:%d: unable to create table DMA map for "
832 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
833 channel, drive, error);
834 return error;
835 }
836 if ((error = bus_dmamap_load(sc->sc_dmat,
837 dma_maps->dmamap_table,
838 dma_maps->dma_table,
839 dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
840 printf("%s:%d: unable to load table DMA map for "
841 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
842 channel, drive, error);
843 return error;
844 }
845 WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
846 dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
847 /* Create a xfer DMA map for this drive */
848 if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
849 NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
850 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
851 &dma_maps->dmamap_xfer)) != 0) {
852 printf("%s:%d: unable to create xfer DMA map for "
853 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
854 channel, drive, error);
855 return error;
856 }
857 return 0;
858 }
859
860 int
861 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
862 void *v;
863 int channel, drive;
864 void *databuf;
865 size_t datalen;
866 int flags;
867 {
868 struct pciide_softc *sc = v;
869 int error, seg;
870 struct pciide_dma_maps *dma_maps =
871 &sc->pciide_channels[channel].dma_maps[drive];
872
873 error = bus_dmamap_load(sc->sc_dmat,
874 dma_maps->dmamap_xfer,
875 databuf, datalen, NULL, BUS_DMA_NOWAIT);
876 if (error) {
877 printf("%s:%d: unable to load xfer DMA map for"
878 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
879 channel, drive, error);
880 return error;
881 }
882
883 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
884 dma_maps->dmamap_xfer->dm_mapsize,
885 (flags & WDC_DMA_READ) ?
886 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
887
888 for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
889 #ifdef DIAGNOSTIC
890 /* A segment must not cross a 64k boundary */
891 {
892 u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
893 u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
894 if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
895 ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
896 printf("pciide_dma: segment %d physical addr 0x%lx"
897 " len 0x%lx not properly aligned\n",
898 seg, phys, len);
899 panic("pciide_dma: buf align");
900 }
901 }
902 #endif
903 dma_maps->dma_table[seg].base_addr =
904 htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
905 dma_maps->dma_table[seg].byte_count =
906 htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
907 IDEDMA_BYTE_COUNT_MASK);
908 WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
909 seg, le32toh(dma_maps->dma_table[seg].byte_count),
910 le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
911
912 }
913 dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
914 htole32(IDEDMA_BYTE_COUNT_EOT);
915
916 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
917 dma_maps->dmamap_table->dm_mapsize,
918 BUS_DMASYNC_PREWRITE);
919
920 /* Maps are ready. Start DMA function */
921 #ifdef DIAGNOSTIC
922 if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
923 printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
924 dma_maps->dmamap_table->dm_segs[0].ds_addr);
925 panic("pciide_dma_init: table align");
926 }
927 #endif
928
929 /* Clear status bits */
930 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
931 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
932 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
933 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
934 /* Write table addr */
935 bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
936 IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
937 dma_maps->dmamap_table->dm_segs[0].ds_addr);
938 /* set read/write */
939 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
940 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
941 (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
942 /* remember flags */
943 dma_maps->dma_flags = flags;
944 return 0;
945 }
946
947 void
948 pciide_dma_start(v, channel, drive)
949 void *v;
950 int channel, drive;
951 {
952 struct pciide_softc *sc = v;
953
954 WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
955 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
956 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
957 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
958 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
959 }
960
961 int
962 pciide_dma_finish(v, channel, drive, force)
963 void *v;
964 int channel, drive;
965 int force;
966 {
967 struct pciide_softc *sc = v;
968 u_int8_t status;
969 int error = 0;
970 struct pciide_dma_maps *dma_maps =
971 &sc->pciide_channels[channel].dma_maps[drive];
972
973 status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
974 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
975 WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
976 DEBUG_XFERS);
977
978 if (force == 0 && (status & IDEDMA_CTL_INTR) == 0)
979 return WDC_DMAST_NOIRQ;
980
981 /* stop DMA channel */
982 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
983 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
984 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
985 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
986
987 /* Unload the map of the data buffer */
988 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
989 dma_maps->dmamap_xfer->dm_mapsize,
990 (dma_maps->dma_flags & WDC_DMA_READ) ?
991 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
992 bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
993
994 if ((status & IDEDMA_CTL_ERR) != 0) {
995 printf("%s:%d:%d: bus-master DMA error: status=0x%x\n",
996 sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
997 error |= WDC_DMAST_ERR;
998 }
999
1000 if ((status & IDEDMA_CTL_INTR) == 0) {
1001 printf("%s:%d:%d: bus-master DMA error: missing interrupt, "
1002 "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
1003 drive, status);
1004 error |= WDC_DMAST_NOIRQ;
1005 }
1006
1007 if ((status & IDEDMA_CTL_ACT) != 0) {
1008 /* data underrun, may be a valid condition for ATAPI */
1009 error |= WDC_DMAST_UNDER;
1010 }
1011 return error;
1012 }
1013
1014 void
1015 pciide_irqack(chp)
1016 struct channel_softc *chp;
1017 {
1018 struct pciide_channel *cp = (struct pciide_channel*)chp;
1019 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1020
1021 /* clear status bits in IDE DMA registers */
1022 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1023 IDEDMA_CTL + IDEDMA_SCH_OFFSET * chp->channel,
1024 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1025 IDEDMA_CTL + IDEDMA_SCH_OFFSET * chp->channel));
1026 }
1027
1028 /* some common code used by several chip_map */
1029 int
1030 pciide_chansetup(sc, channel, interface)
1031 struct pciide_softc *sc;
1032 int channel;
1033 pcireg_t interface;
1034 {
1035 struct pciide_channel *cp = &sc->pciide_channels[channel];
1036 sc->wdc_chanarray[channel] = &cp->wdc_channel;
1037 cp->name = PCIIDE_CHANNEL_NAME(channel);
1038 cp->wdc_channel.channel = channel;
1039 cp->wdc_channel.wdc = &sc->sc_wdcdev;
1040 cp->wdc_channel.ch_queue =
1041 malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
1042 if (cp->wdc_channel.ch_queue == NULL) {
1043 printf("%s %s channel: "
1044 "can't allocate memory for command queue",
1045 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1046 return 0;
1047 }
1048 printf("%s: %s channel %s to %s mode\n",
1049 sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
1050 (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
1051 "configured" : "wired",
1052 (interface & PCIIDE_INTERFACE_PCI(channel)) ?
1053 "native-PCI" : "compatibility");
1054 return 1;
1055 }
1056
1057 /* some common code used by several chip channel_map */
1058 void
1059 pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep, pci_intr)
1060 struct pci_attach_args *pa;
1061 struct pciide_channel *cp;
1062 pcireg_t interface;
1063 bus_size_t *cmdsizep, *ctlsizep;
1064 int (*pci_intr) __P((void *));
1065 {
1066 struct channel_softc *wdc_cp = &cp->wdc_channel;
1067
1068 if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
1069 cp->hw_ok = pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep,
1070 pci_intr);
1071 else
1072 cp->hw_ok = pciide_mapregs_compat(pa, cp,
1073 wdc_cp->channel, cmdsizep, ctlsizep);
1074
1075 if (cp->hw_ok == 0)
1076 return;
1077 wdc_cp->data32iot = wdc_cp->cmd_iot;
1078 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1079 wdcattach(wdc_cp);
1080 }
1081
1082 /*
1083 * Generic code to call to know if a channel can be disabled. Return 1
1084 * if channel can be disabled, 0 if not
1085 */
1086 int
1087 pciide_chan_candisable(cp)
1088 struct pciide_channel *cp;
1089 {
1090 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1091 struct channel_softc *wdc_cp = &cp->wdc_channel;
1092
1093 if ((wdc_cp->ch_drive[0].drive_flags & DRIVE) == 0 &&
1094 (wdc_cp->ch_drive[1].drive_flags & DRIVE) == 0) {
1095 printf("%s: disabling %s channel (no drives)\n",
1096 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1097 cp->hw_ok = 0;
1098 return 1;
1099 }
1100 return 0;
1101 }
1102
1103 /*
1104 * generic code to map the compat intr if hw_ok=1 and it is a compat channel.
1105 * Set hw_ok=0 on failure
1106 */
1107 void
1108 pciide_map_compat_intr(pa, cp, compatchan, interface)
1109 struct pci_attach_args *pa;
1110 struct pciide_channel *cp;
1111 int compatchan, interface;
1112 {
1113 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1114 struct channel_softc *wdc_cp = &cp->wdc_channel;
1115
1116 if (cp->hw_ok == 0)
1117 return;
1118 if ((interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel)) != 0)
1119 return;
1120
1121 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
1122 pa, compatchan, pciide_compat_intr, cp);
1123 if (cp->ih == NULL) {
1124 printf("%s: no compatibility interrupt for use by %s "
1125 "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1126 cp->hw_ok = 0;
1127 }
1128 }
1129
1130 void
1131 pciide_print_modes(cp)
1132 struct pciide_channel *cp;
1133 {
1134 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1135 int drive;
1136 struct channel_softc *chp;
1137 struct ata_drive_datas *drvp;
1138
1139 chp = &cp->wdc_channel;
1140 for (drive = 0; drive < 2; drive++) {
1141 drvp = &chp->ch_drive[drive];
1142 if ((drvp->drive_flags & DRIVE) == 0)
1143 continue;
1144 printf("%s(%s:%d:%d): using PIO mode %d",
1145 drvp->drv_softc->dv_xname,
1146 sc->sc_wdcdev.sc_dev.dv_xname,
1147 chp->channel, drive, drvp->PIO_mode);
1148 if (drvp->drive_flags & DRIVE_DMA)
1149 printf(", DMA mode %d", drvp->DMA_mode);
1150 if (drvp->drive_flags & DRIVE_UDMA)
1151 printf(", Ultra-DMA mode %d", drvp->UDMA_mode);
1152 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
1153 printf(" (using DMA data transfers)");
1154 printf("\n");
1155 }
1156 }
1157
1158 void
1159 default_chip_map(sc, pa)
1160 struct pciide_softc *sc;
1161 struct pci_attach_args *pa;
1162 {
1163 struct pciide_channel *cp;
1164 pcireg_t interface = PCI_INTERFACE(pa->pa_class);
1165 pcireg_t csr;
1166 int channel, drive;
1167 struct ata_drive_datas *drvp;
1168 u_int8_t idedma_ctl;
1169 bus_size_t cmdsize, ctlsize;
1170 char *failreason;
1171
1172 if (pciide_chipen(sc, pa) == 0)
1173 return;
1174
1175 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
1176 printf("%s: bus-master DMA support present",
1177 sc->sc_wdcdev.sc_dev.dv_xname);
1178 if (sc->sc_pp == &default_product_desc &&
1179 (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags &
1180 PCIIDE_OPTIONS_DMA) == 0) {
1181 printf(", but unused (no driver support)");
1182 sc->sc_dma_ok = 0;
1183 } else {
1184 pciide_mapreg_dma(sc, pa);
1185 if (sc->sc_dma_ok != 0)
1186 printf(", used without full driver "
1187 "support");
1188 }
1189 } else {
1190 printf("%s: hardware does not support DMA",
1191 sc->sc_wdcdev.sc_dev.dv_xname);
1192 sc->sc_dma_ok = 0;
1193 }
1194 printf("\n");
1195 if (sc->sc_dma_ok) {
1196 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
1197 sc->sc_wdcdev.irqack = pciide_irqack;
1198 }
1199 sc->sc_wdcdev.PIO_cap = 0;
1200 sc->sc_wdcdev.DMA_cap = 0;
1201
1202 sc->sc_wdcdev.channels = sc->wdc_chanarray;
1203 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
1204 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
1205
1206 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1207 cp = &sc->pciide_channels[channel];
1208 if (pciide_chansetup(sc, channel, interface) == 0)
1209 continue;
1210 if (interface & PCIIDE_INTERFACE_PCI(channel)) {
1211 cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize,
1212 &ctlsize, pciide_pci_intr);
1213 } else {
1214 cp->hw_ok = pciide_mapregs_compat(pa, cp,
1215 channel, &cmdsize, &ctlsize);
1216 }
1217 if (cp->hw_ok == 0)
1218 continue;
1219 /*
1220 * Check to see if something appears to be there.
1221 */
1222 failreason = NULL;
1223 if (!wdcprobe(&cp->wdc_channel)) {
1224 failreason = "not responding; disabled or no drives?";
1225 goto next;
1226 }
1227 /*
1228 * Now, make sure it's actually attributable to this PCI IDE
1229 * channel by trying to access the channel again while the
1230 * PCI IDE controller's I/O space is disabled. (If the
1231 * channel no longer appears to be there, it belongs to
1232 * this controller.) YUCK!
1233 */
1234 csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
1235 PCI_COMMAND_STATUS_REG);
1236 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
1237 csr & ~PCI_COMMAND_IO_ENABLE);
1238 if (wdcprobe(&cp->wdc_channel))
1239 failreason = "other hardware responding at addresses";
1240 pci_conf_write(sc->sc_pc, sc->sc_tag,
1241 PCI_COMMAND_STATUS_REG, csr);
1242 next:
1243 if (failreason) {
1244 printf("%s: %s channel ignored (%s)\n",
1245 sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
1246 failreason);
1247 cp->hw_ok = 0;
1248 bus_space_unmap(cp->wdc_channel.cmd_iot,
1249 cp->wdc_channel.cmd_ioh, cmdsize);
1250 bus_space_unmap(cp->wdc_channel.ctl_iot,
1251 cp->wdc_channel.ctl_ioh, ctlsize);
1252 } else {
1253 pciide_map_compat_intr(pa, cp, channel, interface);
1254 }
1255 if (cp->hw_ok) {
1256 cp->wdc_channel.data32iot = cp->wdc_channel.cmd_iot;
1257 cp->wdc_channel.data32ioh = cp->wdc_channel.cmd_ioh;
1258 wdcattach(&cp->wdc_channel);
1259 }
1260 }
1261
1262 if (sc->sc_dma_ok == 0)
1263 return;
1264
1265 /* Allocate DMA maps */
1266 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1267 idedma_ctl = 0;
1268 cp = &sc->pciide_channels[channel];
1269 for (drive = 0; drive < 2; drive++) {
1270 drvp = &cp->wdc_channel.ch_drive[drive];
1271 /* If no drive, skip */
1272 if ((drvp->drive_flags & DRIVE) == 0)
1273 continue;
1274 if ((drvp->drive_flags & DRIVE_DMA) == 0)
1275 continue;
1276 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1277 /* Abort DMA setup */
1278 printf("%s:%d:%d: can't allocate DMA maps, "
1279 "using PIO transfers\n",
1280 sc->sc_wdcdev.sc_dev.dv_xname,
1281 channel, drive);
1282 drvp->drive_flags &= ~DRIVE_DMA;
1283 }
1284 printf("%s:%d:%d: using DMA data transfers\n",
1285 sc->sc_wdcdev.sc_dev.dv_xname,
1286 channel, drive);
1287 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1288 }
1289 if (idedma_ctl != 0) {
1290 /* Add software bits in status register */
1291 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1292 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1293 idedma_ctl);
1294 }
1295 }
1296 }
1297
1298 void
1299 piix_chip_map(sc, pa)
1300 struct pciide_softc *sc;
1301 struct pci_attach_args *pa;
1302 {
1303 struct pciide_channel *cp;
1304 int channel;
1305 u_int32_t idetim;
1306 bus_size_t cmdsize, ctlsize;
1307
1308 if (pciide_chipen(sc, pa) == 0)
1309 return;
1310
1311 printf("%s: bus-master DMA support present",
1312 sc->sc_wdcdev.sc_dev.dv_xname);
1313 pciide_mapreg_dma(sc, pa);
1314 printf("\n");
1315 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
1316 WDC_CAPABILITY_MODE;
1317 if (sc->sc_dma_ok) {
1318 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
1319 sc->sc_wdcdev.irqack = pciide_irqack;
1320 switch(sc->sc_pp->ide_product) {
1321 case PCI_PRODUCT_INTEL_82371AB_IDE:
1322 case PCI_PRODUCT_INTEL_82801AA_IDE:
1323 case PCI_PRODUCT_INTEL_82801AB_IDE:
1324 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1325 }
1326 }
1327 sc->sc_wdcdev.PIO_cap = 4;
1328 sc->sc_wdcdev.DMA_cap = 2;
1329 sc->sc_wdcdev.UDMA_cap =
1330 (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE) ? 4 : 2;
1331 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371FB_IDE)
1332 sc->sc_wdcdev.set_modes = piix_setup_channel;
1333 else
1334 sc->sc_wdcdev.set_modes = piix3_4_setup_channel;
1335 sc->sc_wdcdev.channels = sc->wdc_chanarray;
1336 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
1337
1338 WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x",
1339 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)),
1340 DEBUG_PROBE);
1341 if (sc->sc_pp->ide_product != PCI_PRODUCT_INTEL_82371FB_IDE) {
1342 WDCDEBUG_PRINT((", sidetim=0x%x",
1343 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)),
1344 DEBUG_PROBE);
1345 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
1346 WDCDEBUG_PRINT((", udamreg 0x%x",
1347 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
1348 DEBUG_PROBE);
1349 }
1350 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE ||
1351 sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AB_IDE) {
1352 WDCDEBUG_PRINT((", IDE_CONTROL 0x%x",
1353 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_CONFIG)),
1354 DEBUG_PROBE);
1355 }
1356
1357 }
1358 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
1359
1360 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1361 cp = &sc->pciide_channels[channel];
1362 /* PIIX is compat-only */
1363 if (pciide_chansetup(sc, channel, 0) == 0)
1364 continue;
1365 idetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1366 if ((PIIX_IDETIM_READ(idetim, channel) &
1367 PIIX_IDETIM_IDE) == 0) {
1368 printf("%s: %s channel ignored (disabled)\n",
1369 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1370 continue;
1371 }
1372 /* PIIX are compat-only pciide devices */
1373 pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize, pciide_pci_intr);
1374 if (cp->hw_ok == 0)
1375 continue;
1376 if (pciide_chan_candisable(cp)) {
1377 idetim = PIIX_IDETIM_CLEAR(idetim, PIIX_IDETIM_IDE,
1378 channel);
1379 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM,
1380 idetim);
1381 }
1382 pciide_map_compat_intr(pa, cp, channel, 0);
1383 if (cp->hw_ok == 0)
1384 continue;
1385 sc->sc_wdcdev.set_modes(&cp->wdc_channel);
1386 }
1387
1388 WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x",
1389 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM)),
1390 DEBUG_PROBE);
1391 if (sc->sc_pp->ide_product != PCI_PRODUCT_INTEL_82371FB_IDE) {
1392 WDCDEBUG_PRINT((", sidetim=0x%x",
1393 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM)),
1394 DEBUG_PROBE);
1395 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
1396 WDCDEBUG_PRINT((", udamreg 0x%x",
1397 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG)),
1398 DEBUG_PROBE);
1399 }
1400 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE ||
1401 sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AB_IDE) {
1402 WDCDEBUG_PRINT((", IDE_CONTROL 0x%x",
1403 pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_CONFIG)),
1404 DEBUG_PROBE);
1405 }
1406 }
1407 WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
1408 }
1409
1410 void
1411 piix_setup_channel(chp)
1412 struct channel_softc *chp;
1413 {
1414 u_int8_t mode[2], drive;
1415 u_int32_t oidetim, idetim, idedma_ctl;
1416 struct pciide_channel *cp = (struct pciide_channel*)chp;
1417 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1418 struct ata_drive_datas *drvp = cp->wdc_channel.ch_drive;
1419
1420 oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1421 idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, chp->channel);
1422 idedma_ctl = 0;
1423
1424 /* set up new idetim: Enable IDE registers decode */
1425 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
1426 chp->channel);
1427
1428 /* setup DMA */
1429 pciide_channel_dma_setup(cp);
1430
1431 /*
1432 * Here we have to mess up with drives mode: PIIX can't have
1433 * different timings for master and slave drives.
1434 * We need to find the best combination.
1435 */
1436
1437 /* If both drives supports DMA, take the lower mode */
1438 if ((drvp[0].drive_flags & DRIVE_DMA) &&
1439 (drvp[1].drive_flags & DRIVE_DMA)) {
1440 mode[0] = mode[1] =
1441 min(drvp[0].DMA_mode, drvp[1].DMA_mode);
1442 drvp[0].DMA_mode = mode[0];
1443 drvp[1].DMA_mode = mode[1];
1444 goto ok;
1445 }
1446 /*
1447 * If only one drive supports DMA, use its mode, and
1448 * put the other one in PIO mode 0 if mode not compatible
1449 */
1450 if (drvp[0].drive_flags & DRIVE_DMA) {
1451 mode[0] = drvp[0].DMA_mode;
1452 mode[1] = drvp[1].PIO_mode;
1453 if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
1454 piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
1455 mode[1] = drvp[1].PIO_mode = 0;
1456 goto ok;
1457 }
1458 if (drvp[1].drive_flags & DRIVE_DMA) {
1459 mode[1] = drvp[1].DMA_mode;
1460 mode[0] = drvp[0].PIO_mode;
1461 if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
1462 piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
1463 mode[0] = drvp[0].PIO_mode = 0;
1464 goto ok;
1465 }
1466 /*
1467 * If both drives are not DMA, takes the lower mode, unless
1468 * one of them is PIO mode < 2
1469 */
1470 if (drvp[0].PIO_mode < 2) {
1471 mode[0] = drvp[0].PIO_mode = 0;
1472 mode[1] = drvp[1].PIO_mode;
1473 } else if (drvp[1].PIO_mode < 2) {
1474 mode[1] = drvp[1].PIO_mode = 0;
1475 mode[0] = drvp[0].PIO_mode;
1476 } else {
1477 mode[0] = mode[1] =
1478 min(drvp[1].PIO_mode, drvp[0].PIO_mode);
1479 drvp[0].PIO_mode = mode[0];
1480 drvp[1].PIO_mode = mode[1];
1481 }
1482 ok: /* The modes are setup */
1483 for (drive = 0; drive < 2; drive++) {
1484 if (drvp[drive].drive_flags & DRIVE_DMA) {
1485 idetim |= piix_setup_idetim_timings(
1486 mode[drive], 1, chp->channel);
1487 goto end;
1488 }
1489 }
1490 /* If we are there, none of the drives are DMA */
1491 if (mode[0] >= 2)
1492 idetim |= piix_setup_idetim_timings(
1493 mode[0], 0, chp->channel);
1494 else
1495 idetim |= piix_setup_idetim_timings(
1496 mode[1], 0, chp->channel);
1497 end: /*
1498 * timing mode is now set up in the controller. Enable
1499 * it per-drive
1500 */
1501 for (drive = 0; drive < 2; drive++) {
1502 /* If no drive, skip */
1503 if ((drvp[drive].drive_flags & DRIVE) == 0)
1504 continue;
1505 idetim |= piix_setup_idetim_drvs(&drvp[drive]);
1506 if (drvp[drive].drive_flags & DRIVE_DMA)
1507 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1508 }
1509 if (idedma_ctl != 0) {
1510 /* Add software bits in status register */
1511 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1512 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1513 idedma_ctl);
1514 }
1515 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1516 pciide_print_modes(cp);
1517 }
1518
1519 void
1520 piix3_4_setup_channel(chp)
1521 struct channel_softc *chp;
1522 {
1523 struct ata_drive_datas *drvp;
1524 u_int32_t oidetim, idetim, sidetim, udmareg, ideconf, idedma_ctl;
1525 struct pciide_channel *cp = (struct pciide_channel*)chp;
1526 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1527 int drive;
1528 int channel = chp->channel;
1529
1530 oidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_IDETIM);
1531 sidetim = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM);
1532 udmareg = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG);
1533 ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, PIIX_CONFIG);
1534 idetim = PIIX_IDETIM_CLEAR(oidetim, 0xffff, channel);
1535 sidetim &= ~(PIIX_SIDETIM_ISP_MASK(channel) |
1536 PIIX_SIDETIM_RTC_MASK(channel));
1537
1538 idedma_ctl = 0;
1539 /* If channel disabled, no need to go further */
1540 if ((PIIX_IDETIM_READ(oidetim, channel) & PIIX_IDETIM_IDE) == 0)
1541 return;
1542 /* set up new idetim: Enable IDE registers decode */
1543 idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, channel);
1544
1545 /* setup DMA if needed */
1546 pciide_channel_dma_setup(cp);
1547
1548 for (drive = 0; drive < 2; drive++) {
1549 udmareg &= ~(PIIX_UDMACTL_DRV_EN(channel, drive) |
1550 PIIX_UDMATIM_SET(0x3, channel, drive));
1551 drvp = &chp->ch_drive[drive];
1552 /* If no drive, skip */
1553 if ((drvp->drive_flags & DRIVE) == 0)
1554 continue;
1555 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1556 (drvp->drive_flags & DRIVE_UDMA) == 0))
1557 goto pio;
1558
1559 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE ||
1560 sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AB_IDE) {
1561 ideconf |= PIIX_CONFIG_PINGPONG;
1562 }
1563 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82801AA_IDE) {
1564 /* setup Ultra/66 */
1565 if (drvp->UDMA_mode > 2 &&
1566 (ideconf & PIIX_CONFIG_CR(channel, drive)) == 0)
1567 drvp->UDMA_mode = 2;
1568 if (drvp->UDMA_mode > 2)
1569 ideconf |= PIIX_CONFIG_UDMA66(channel, drive);
1570 else
1571 ideconf &= ~PIIX_CONFIG_UDMA66(channel, drive);
1572 }
1573 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1574 (drvp->drive_flags & DRIVE_UDMA)) {
1575 /* use Ultra/DMA */
1576 drvp->drive_flags &= ~DRIVE_DMA;
1577 udmareg |= PIIX_UDMACTL_DRV_EN( channel, drive);
1578 udmareg |= PIIX_UDMATIM_SET(
1579 piix4_sct_udma[drvp->UDMA_mode], channel, drive);
1580 } else {
1581 /* use Multiword DMA */
1582 drvp->drive_flags &= ~DRIVE_UDMA;
1583 if (drive == 0) {
1584 idetim |= piix_setup_idetim_timings(
1585 drvp->DMA_mode, 1, channel);
1586 } else {
1587 sidetim |= piix_setup_sidetim_timings(
1588 drvp->DMA_mode, 1, channel);
1589 idetim =PIIX_IDETIM_SET(idetim,
1590 PIIX_IDETIM_SITRE, channel);
1591 }
1592 }
1593 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1594
1595 pio: /* use PIO mode */
1596 idetim |= piix_setup_idetim_drvs(drvp);
1597 if (drive == 0) {
1598 idetim |= piix_setup_idetim_timings(
1599 drvp->PIO_mode, 0, channel);
1600 } else {
1601 sidetim |= piix_setup_sidetim_timings(
1602 drvp->PIO_mode, 0, channel);
1603 idetim =PIIX_IDETIM_SET(idetim,
1604 PIIX_IDETIM_SITRE, channel);
1605 }
1606 }
1607 if (idedma_ctl != 0) {
1608 /* Add software bits in status register */
1609 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1610 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1611 idedma_ctl);
1612 }
1613 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_IDETIM, idetim);
1614 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_SIDETIM, sidetim);
1615 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_UDMAREG, udmareg);
1616 pci_conf_write(sc->sc_pc, sc->sc_tag, PIIX_CONFIG, ideconf);
1617 pciide_print_modes(cp);
1618 }
1619
1620
1621 /* setup ISP and RTC fields, based on mode */
1622 static u_int32_t
1623 piix_setup_idetim_timings(mode, dma, channel)
1624 u_int8_t mode;
1625 u_int8_t dma;
1626 u_int8_t channel;
1627 {
1628
1629 if (dma)
1630 return PIIX_IDETIM_SET(0,
1631 PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
1632 PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
1633 channel);
1634 else
1635 return PIIX_IDETIM_SET(0,
1636 PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
1637 PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
1638 channel);
1639 }
1640
1641 /* setup DTE, PPE, IE and TIME field based on PIO mode */
1642 static u_int32_t
1643 piix_setup_idetim_drvs(drvp)
1644 struct ata_drive_datas *drvp;
1645 {
1646 u_int32_t ret = 0;
1647 struct channel_softc *chp = drvp->chnl_softc;
1648 u_int8_t channel = chp->channel;
1649 u_int8_t drive = drvp->drive;
1650
1651 /*
1652 * If drive is using UDMA, timings setups are independant
1653 * So just check DMA and PIO here.
1654 */
1655 if (drvp->drive_flags & DRIVE_DMA) {
1656 /* if mode = DMA mode 0, use compatible timings */
1657 if ((drvp->drive_flags & DRIVE_DMA) &&
1658 drvp->DMA_mode == 0) {
1659 drvp->PIO_mode = 0;
1660 return ret;
1661 }
1662 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1663 /*
1664 * PIO and DMA timings are the same, use fast timings for PIO
1665 * too, else use compat timings.
1666 */
1667 if ((piix_isp_pio[drvp->PIO_mode] !=
1668 piix_isp_dma[drvp->DMA_mode]) ||
1669 (piix_rtc_pio[drvp->PIO_mode] !=
1670 piix_rtc_dma[drvp->DMA_mode]))
1671 drvp->PIO_mode = 0;
1672 /* if PIO mode <= 2, use compat timings for PIO */
1673 if (drvp->PIO_mode <= 2) {
1674 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
1675 channel);
1676 return ret;
1677 }
1678 }
1679
1680 /*
1681 * Now setup PIO modes. If mode < 2, use compat timings.
1682 * Else enable fast timings. Enable IORDY and prefetch/post
1683 * if PIO mode >= 3.
1684 */
1685
1686 if (drvp->PIO_mode < 2)
1687 return ret;
1688
1689 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
1690 if (drvp->PIO_mode >= 3) {
1691 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
1692 ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
1693 }
1694 return ret;
1695 }
1696
1697 /* setup values in SIDETIM registers, based on mode */
1698 static u_int32_t
1699 piix_setup_sidetim_timings(mode, dma, channel)
1700 u_int8_t mode;
1701 u_int8_t dma;
1702 u_int8_t channel;
1703 {
1704 if (dma)
1705 return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
1706 PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
1707 else
1708 return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
1709 PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
1710 }
1711
1712 void
1713 amd756_chip_map(sc, pa)
1714 struct pciide_softc *sc;
1715 struct pci_attach_args *pa;
1716 {
1717 struct pciide_channel *cp;
1718 pcireg_t interface = PCI_INTERFACE(pa->pa_class);
1719 int channel;
1720 pcireg_t chanenable;
1721 bus_size_t cmdsize, ctlsize;
1722
1723 if (pciide_chipen(sc, pa) == 0)
1724 return;
1725 printf("%s: bus-master DMA support present",
1726 sc->sc_wdcdev.sc_dev.dv_xname);
1727 pciide_mapreg_dma(sc, pa);
1728 printf("\n");
1729 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
1730 WDC_CAPABILITY_MODE;
1731 if (sc->sc_dma_ok) {
1732 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
1733 sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
1734 sc->sc_wdcdev.irqack = pciide_irqack;
1735 }
1736 sc->sc_wdcdev.PIO_cap = 4;
1737 sc->sc_wdcdev.DMA_cap = 2;
1738 sc->sc_wdcdev.UDMA_cap = 4;
1739 sc->sc_wdcdev.set_modes = amd756_setup_channel;
1740 sc->sc_wdcdev.channels = sc->wdc_chanarray;
1741 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
1742 chanenable = pci_conf_read(sc->sc_pc, sc->sc_tag, AMD756_CHANSTATUS_EN);
1743
1744 WDCDEBUG_PRINT(("amd756_chip_map: Channel enable=0x%x\n", chanenable),
1745 DEBUG_PROBE);
1746 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1747 cp = &sc->pciide_channels[channel];
1748 if (pciide_chansetup(sc, channel, interface) == 0)
1749 continue;
1750
1751 if ((chanenable & AMD756_CHAN_EN(channel)) == 0) {
1752 printf("%s: %s channel ignored (disabled)\n",
1753 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1754 continue;
1755 }
1756 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
1757 pciide_pci_intr);
1758
1759 if (pciide_chan_candisable(cp))
1760 chanenable &= ~AMD756_CHAN_EN(channel);
1761 pciide_map_compat_intr(pa, cp, channel, interface);
1762 if (cp->hw_ok == 0)
1763 continue;
1764
1765 amd756_setup_channel(&cp->wdc_channel);
1766 }
1767 pci_conf_write(sc->sc_pc, sc->sc_tag, AMD756_CHANSTATUS_EN,
1768 chanenable);
1769 return;
1770 }
1771
1772 void
1773 amd756_setup_channel(chp)
1774 struct channel_softc *chp;
1775 {
1776 u_int32_t udmatim_reg, datatim_reg;
1777 u_int8_t idedma_ctl;
1778 int mode, drive;
1779 struct ata_drive_datas *drvp;
1780 struct pciide_channel *cp = (struct pciide_channel*)chp;
1781 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1782 #ifndef PCIIDE_AMD756_ENABLEDMA
1783 int rev = PCI_REVISION(
1784 pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
1785 #endif
1786
1787 idedma_ctl = 0;
1788 datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, AMD756_DATATIM);
1789 udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, AMD756_UDMA);
1790 datatim_reg &= ~AMD756_DATATIM_MASK(chp->channel);
1791 udmatim_reg &= ~AMD756_UDMA_MASK(chp->channel);
1792
1793 /* setup DMA if needed */
1794 pciide_channel_dma_setup(cp);
1795
1796 for (drive = 0; drive < 2; drive++) {
1797 drvp = &chp->ch_drive[drive];
1798 /* If no drive, skip */
1799 if ((drvp->drive_flags & DRIVE) == 0)
1800 continue;
1801 /* add timing values, setup DMA if needed */
1802 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1803 (drvp->drive_flags & DRIVE_UDMA) == 0)) {
1804 mode = drvp->PIO_mode;
1805 goto pio;
1806 }
1807 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1808 (drvp->drive_flags & DRIVE_UDMA)) {
1809 /* use Ultra/DMA */
1810 drvp->drive_flags &= ~DRIVE_DMA;
1811 udmatim_reg |= AMD756_UDMA_EN(chp->channel, drive) |
1812 AMD756_UDMA_EN_MTH(chp->channel, drive) |
1813 AMD756_UDMA_TIME(chp->channel, drive,
1814 amd756_udma_tim[drvp->UDMA_mode]);
1815 /* can use PIO timings, MW DMA unused */
1816 mode = drvp->PIO_mode;
1817 } else {
1818 /* use Multiword DMA, but only if revision is OK */
1819 drvp->drive_flags &= ~DRIVE_UDMA;
1820 #ifndef PCIIDE_AMD756_ENABLEDMA
1821 /*
1822 * The workaround doesn't seem to be necessary
1823 * with all drives, so it can be disabled by
1824 * PCIIDE_AMD756_ENABLEDMA. It causes a hard hang if
1825 * triggered.
1826 */
1827 if (AMD756_CHIPREV_DISABLEDMA(rev)) {
1828 printf("%s:%d:%d: multi-word DMA disabled due "
1829 "to chip revision\n",
1830 sc->sc_wdcdev.sc_dev.dv_xname,
1831 chp->channel, drive);
1832 mode = drvp->PIO_mode;
1833 drvp->drive_flags &= ~DRIVE_DMA;
1834 goto pio;
1835 }
1836 #endif
1837 /* mode = min(pio, dma+2) */
1838 if (drvp->PIO_mode <= (drvp->DMA_mode +2))
1839 mode = drvp->PIO_mode;
1840 else
1841 mode = drvp->DMA_mode + 2;
1842 }
1843 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1844
1845 pio: /* setup PIO mode */
1846 if (mode <= 2) {
1847 drvp->DMA_mode = 0;
1848 drvp->PIO_mode = 0;
1849 mode = 0;
1850 } else {
1851 drvp->PIO_mode = mode;
1852 drvp->DMA_mode = mode - 2;
1853 }
1854 datatim_reg |=
1855 AMD756_DATATIM_PULSE(chp->channel, drive,
1856 amd756_pio_set[mode]) |
1857 AMD756_DATATIM_RECOV(chp->channel, drive,
1858 amd756_pio_rec[mode]);
1859 }
1860 if (idedma_ctl != 0) {
1861 /* Add software bits in status register */
1862 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1863 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
1864 idedma_ctl);
1865 }
1866 pciide_print_modes(cp);
1867 pci_conf_write(sc->sc_pc, sc->sc_tag, AMD756_DATATIM, datatim_reg);
1868 pci_conf_write(sc->sc_pc, sc->sc_tag, AMD756_UDMA, udmatim_reg);
1869 }
1870
1871 void
1872 apollo_chip_map(sc, pa)
1873 struct pciide_softc *sc;
1874 struct pci_attach_args *pa;
1875 {
1876 struct pciide_channel *cp;
1877 pcireg_t interface = PCI_INTERFACE(pa->pa_class);
1878 int channel;
1879 u_int32_t ideconf;
1880 bus_size_t cmdsize, ctlsize;
1881
1882 if (pciide_chipen(sc, pa) == 0)
1883 return;
1884 printf("%s: bus-master DMA support present",
1885 sc->sc_wdcdev.sc_dev.dv_xname);
1886 pciide_mapreg_dma(sc, pa);
1887 printf("\n");
1888 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
1889 WDC_CAPABILITY_MODE;
1890 if (sc->sc_dma_ok) {
1891 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
1892 sc->sc_wdcdev.irqack = pciide_irqack;
1893 if (sc->sc_pp->ide_product == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
1894 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1895 }
1896 sc->sc_wdcdev.PIO_cap = 4;
1897 sc->sc_wdcdev.DMA_cap = 2;
1898 sc->sc_wdcdev.UDMA_cap = 2;
1899 sc->sc_wdcdev.set_modes = apollo_setup_channel;
1900 sc->sc_wdcdev.channels = sc->wdc_chanarray;
1901 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
1902
1903 WDCDEBUG_PRINT(("apollo_chip_map: old APO_IDECONF=0x%x, "
1904 "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1905 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF),
1906 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC),
1907 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
1908 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)),
1909 DEBUG_PROBE);
1910
1911 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1912 cp = &sc->pciide_channels[channel];
1913 if (pciide_chansetup(sc, channel, interface) == 0)
1914 continue;
1915
1916 ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF);
1917 if ((ideconf & APO_IDECONF_EN(channel)) == 0) {
1918 printf("%s: %s channel ignored (disabled)\n",
1919 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1920 continue;
1921 }
1922 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
1923 pciide_pci_intr);
1924 if (cp->hw_ok == 0)
1925 continue;
1926 if (pciide_chan_candisable(cp)) {
1927 ideconf &= ~APO_IDECONF_EN(channel);
1928 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF,
1929 ideconf);
1930 }
1931 pciide_map_compat_intr(pa, cp, channel, interface);
1932
1933 if (cp->hw_ok == 0)
1934 continue;
1935 apollo_setup_channel(&sc->pciide_channels[channel].wdc_channel);
1936 }
1937 WDCDEBUG_PRINT(("apollo_chip_map: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1938 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM),
1939 pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA)), DEBUG_PROBE);
1940 }
1941
1942 void
1943 apollo_setup_channel(chp)
1944 struct channel_softc *chp;
1945 {
1946 u_int32_t udmatim_reg, datatim_reg;
1947 u_int8_t idedma_ctl;
1948 int mode, drive;
1949 struct ata_drive_datas *drvp;
1950 struct pciide_channel *cp = (struct pciide_channel*)chp;
1951 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
1952
1953 idedma_ctl = 0;
1954 datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM);
1955 udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA);
1956 datatim_reg &= ~APO_DATATIM_MASK(chp->channel);
1957 udmatim_reg &= ~AP0_UDMA_MASK(chp->channel);
1958
1959 /* setup DMA if needed */
1960 pciide_channel_dma_setup(cp);
1961
1962 for (drive = 0; drive < 2; drive++) {
1963 drvp = &chp->ch_drive[drive];
1964 /* If no drive, skip */
1965 if ((drvp->drive_flags & DRIVE) == 0)
1966 continue;
1967 /* add timing values, setup DMA if needed */
1968 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1969 (drvp->drive_flags & DRIVE_UDMA) == 0)) {
1970 mode = drvp->PIO_mode;
1971 goto pio;
1972 }
1973 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1974 (drvp->drive_flags & DRIVE_UDMA)) {
1975 /* use Ultra/DMA */
1976 drvp->drive_flags &= ~DRIVE_DMA;
1977 udmatim_reg |= APO_UDMA_EN(chp->channel, drive) |
1978 APO_UDMA_EN_MTH(chp->channel, drive) |
1979 APO_UDMA_TIME(chp->channel, drive,
1980 apollo_udma_tim[drvp->UDMA_mode]);
1981 /* can use PIO timings, MW DMA unused */
1982 mode = drvp->PIO_mode;
1983 } else {
1984 /* use Multiword DMA */
1985 drvp->drive_flags &= ~DRIVE_UDMA;
1986 /* mode = min(pio, dma+2) */
1987 if (drvp->PIO_mode <= (drvp->DMA_mode +2))
1988 mode = drvp->PIO_mode;
1989 else
1990 mode = drvp->DMA_mode + 2;
1991 }
1992 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1993
1994 pio: /* setup PIO mode */
1995 if (mode <= 2) {
1996 drvp->DMA_mode = 0;
1997 drvp->PIO_mode = 0;
1998 mode = 0;
1999 } else {
2000 drvp->PIO_mode = mode;
2001 drvp->DMA_mode = mode - 2;
2002 }
2003 datatim_reg |=
2004 APO_DATATIM_PULSE(chp->channel, drive,
2005 apollo_pio_set[mode]) |
2006 APO_DATATIM_RECOV(chp->channel, drive,
2007 apollo_pio_rec[mode]);
2008 }
2009 if (idedma_ctl != 0) {
2010 /* Add software bits in status register */
2011 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2012 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
2013 idedma_ctl);
2014 }
2015 pciide_print_modes(cp);
2016 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM, datatim_reg);
2017 pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA, udmatim_reg);
2018 }
2019
2020 void
2021 cmd_channel_map(pa, sc, channel)
2022 struct pci_attach_args *pa;
2023 struct pciide_softc *sc;
2024 int channel;
2025 {
2026 struct pciide_channel *cp = &sc->pciide_channels[channel];
2027 bus_size_t cmdsize, ctlsize;
2028 u_int8_t ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CTRL);
2029 int interface;
2030
2031 /*
2032 * The 0648/0649 can be told to identify as a RAID controller.
2033 * In this case, we have to fake interface
2034 */
2035 if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_IDE) {
2036 interface = PCIIDE_INTERFACE_SETTABLE(0) |
2037 PCIIDE_INTERFACE_SETTABLE(1);
2038 if (pciide_pci_read(pa->pa_pc, pa->pa_tag, CMD_CONF) &
2039 CMD_CONF_DSA1)
2040 interface |= PCIIDE_INTERFACE_PCI(0) |
2041 PCIIDE_INTERFACE_PCI(1);
2042 } else {
2043 interface = PCI_INTERFACE(pa->pa_class);
2044 }
2045
2046 sc->wdc_chanarray[channel] = &cp->wdc_channel;
2047 cp->name = PCIIDE_CHANNEL_NAME(channel);
2048 cp->wdc_channel.channel = channel;
2049 cp->wdc_channel.wdc = &sc->sc_wdcdev;
2050
2051 if (channel > 0) {
2052 cp->wdc_channel.ch_queue =
2053 sc->pciide_channels[0].wdc_channel.ch_queue;
2054 } else {
2055 cp->wdc_channel.ch_queue =
2056 malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
2057 }
2058 if (cp->wdc_channel.ch_queue == NULL) {
2059 printf("%s %s channel: "
2060 "can't allocate memory for command queue",
2061 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2062 return;
2063 }
2064
2065 printf("%s: %s channel %s to %s mode\n",
2066 sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
2067 (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
2068 "configured" : "wired",
2069 (interface & PCIIDE_INTERFACE_PCI(channel)) ?
2070 "native-PCI" : "compatibility");
2071
2072 /*
2073 * with a CMD PCI64x, if we get here, the first channel is enabled:
2074 * there's no way to disable the first channel without disabling
2075 * the whole device
2076 */
2077 if (channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
2078 printf("%s: %s channel ignored (disabled)\n",
2079 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2080 return;
2081 }
2082
2083 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize, cmd_pci_intr);
2084 if (cp->hw_ok == 0)
2085 return;
2086 if (channel == 1) {
2087 if (pciide_chan_candisable(cp)) {
2088 ctrl &= ~CMD_CTRL_2PORT;
2089 pciide_pci_write(pa->pa_pc, pa->pa_tag,
2090 CMD_CTRL, ctrl);
2091 }
2092 }
2093 pciide_map_compat_intr(pa, cp, channel, interface);
2094 }
2095
2096 int
2097 cmd_pci_intr(arg)
2098 void *arg;
2099 {
2100 struct pciide_softc *sc = arg;
2101 struct pciide_channel *cp;
2102 struct channel_softc *wdc_cp;
2103 int i, rv, crv;
2104 u_int32_t priirq, secirq;
2105
2106 rv = 0;
2107 priirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CONF);
2108 secirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23);
2109 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
2110 cp = &sc->pciide_channels[i];
2111 wdc_cp = &cp->wdc_channel;
2112 /* If a compat channel skip. */
2113 if (cp->compat)
2114 continue;
2115 if ((i == 0 && (priirq & CMD_CONF_DRV0_INTR)) ||
2116 (i == 1 && (secirq & CMD_ARTTIM23_IRQ))) {
2117 crv = wdcintr(wdc_cp);
2118 if (crv == 0)
2119 printf("%s:%d: bogus intr\n",
2120 sc->sc_wdcdev.sc_dev.dv_xname, i);
2121 else
2122 rv = 1;
2123 }
2124 }
2125 return rv;
2126 }
2127
2128 void
2129 cmd_chip_map(sc, pa)
2130 struct pciide_softc *sc;
2131 struct pci_attach_args *pa;
2132 {
2133 int channel;
2134
2135 /*
2136 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
2137 * and base adresses registers can be disabled at
2138 * hardware level. In this case, the device is wired
2139 * in compat mode and its first channel is always enabled,
2140 * but we can't rely on PCI_COMMAND_IO_ENABLE.
2141 * In fact, it seems that the first channel of the CMD PCI0640
2142 * can't be disabled.
2143 */
2144
2145 #ifdef PCIIDE_CMD064x_DISABLE
2146 if (pciide_chipen(sc, pa) == 0)
2147 return;
2148 #endif
2149
2150 printf("%s: hardware does not support DMA\n",
2151 sc->sc_wdcdev.sc_dev.dv_xname);
2152 sc->sc_dma_ok = 0;
2153
2154 sc->sc_wdcdev.channels = sc->wdc_chanarray;
2155 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
2156 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16;
2157
2158 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2159 cmd_channel_map(pa, sc, channel);
2160 }
2161 }
2162
2163 void
2164 cmd0643_9_chip_map(sc, pa)
2165 struct pciide_softc *sc;
2166 struct pci_attach_args *pa;
2167 {
2168 struct pciide_channel *cp;
2169 int channel;
2170 int rev = PCI_REVISION(
2171 pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
2172
2173 /*
2174 * For a CMD PCI064x, the use of PCI_COMMAND_IO_ENABLE
2175 * and base adresses registers can be disabled at
2176 * hardware level. In this case, the device is wired
2177 * in compat mode and its first channel is always enabled,
2178 * but we can't rely on PCI_COMMAND_IO_ENABLE.
2179 * In fact, it seems that the first channel of the CMD PCI0640
2180 * can't be disabled.
2181 */
2182
2183 #ifdef PCIIDE_CMD064x_DISABLE
2184 if (pciide_chipen(sc, pa) == 0)
2185 return;
2186 #endif
2187 printf("%s: bus-master DMA support present",
2188 sc->sc_wdcdev.sc_dev.dv_xname);
2189 pciide_mapreg_dma(sc, pa);
2190 printf("\n");
2191 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
2192 WDC_CAPABILITY_MODE;
2193 if (sc->sc_dma_ok) {
2194 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
2195 switch (sc->sc_pp->ide_product) {
2196 case PCI_PRODUCT_CMDTECH_649:
2197 case PCI_PRODUCT_CMDTECH_648:
2198 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
2199 sc->sc_wdcdev.UDMA_cap = 4;
2200 sc->sc_wdcdev.irqack = cmd646_9_irqack;
2201 break;
2202 case PCI_PRODUCT_CMDTECH_646:
2203 if (rev >= CMD0646U2_REV) {
2204 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
2205 sc->sc_wdcdev.UDMA_cap = 2;
2206 } else if (rev >= CMD0646U_REV) {
2207 /*
2208 * Linux's driver claims that the 646U is broken
2209 * with UDMA. Only enable it if we know what we're
2210 * doing
2211 */
2212 #ifdef PCIIDE_CMD0646U_UDMA
2213 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
2214 sc->sc_wdcdev.UDMA_cap = 2;
2215 #endif
2216 /* explicitely disable UDMA */
2217 pciide_pci_write(sc->sc_pc, sc->sc_tag,
2218 CMD_UDMATIM(0), 0);
2219 pciide_pci_write(sc->sc_pc, sc->sc_tag,
2220 CMD_UDMATIM(1), 0);
2221 }
2222 sc->sc_wdcdev.irqack = cmd646_9_irqack;
2223 break;
2224 default:
2225 sc->sc_wdcdev.irqack = pciide_irqack;
2226 }
2227 }
2228
2229 sc->sc_wdcdev.channels = sc->wdc_chanarray;
2230 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
2231 sc->sc_wdcdev.PIO_cap = 4;
2232 sc->sc_wdcdev.DMA_cap = 2;
2233 sc->sc_wdcdev.set_modes = cmd0643_9_setup_channel;
2234
2235 WDCDEBUG_PRINT(("cmd0643_9_chip_map: old timings reg 0x%x 0x%x\n",
2236 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
2237 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
2238 DEBUG_PROBE);
2239
2240 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2241 cp = &sc->pciide_channels[channel];
2242 cmd_channel_map(pa, sc, channel);
2243 if (cp->hw_ok == 0)
2244 continue;
2245 cmd0643_9_setup_channel(&cp->wdc_channel);
2246 }
2247 /* note - this also make sure we clear the irq disable and reset bits */
2248 pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_DMA_MODE, CMD_DMA_MULTIPLE);
2249 WDCDEBUG_PRINT(("cmd0643_9_chip_map: timings reg now 0x%x 0x%x\n",
2250 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54),
2251 pci_conf_read(sc->sc_pc, sc->sc_tag, 0x58)),
2252 DEBUG_PROBE);
2253 }
2254
2255 void
2256 cmd0643_9_setup_channel(chp)
2257 struct channel_softc *chp;
2258 {
2259 struct ata_drive_datas *drvp;
2260 u_int8_t tim;
2261 u_int32_t idedma_ctl, udma_reg;
2262 int drive;
2263 struct pciide_channel *cp = (struct pciide_channel*)chp;
2264 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2265
2266 idedma_ctl = 0;
2267 /* setup DMA if needed */
2268 pciide_channel_dma_setup(cp);
2269
2270 for (drive = 0; drive < 2; drive++) {
2271 drvp = &chp->ch_drive[drive];
2272 /* If no drive, skip */
2273 if ((drvp->drive_flags & DRIVE) == 0)
2274 continue;
2275 /* add timing values, setup DMA if needed */
2276 tim = cmd0643_9_data_tim_pio[drvp->PIO_mode];
2277 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
2278 if (drvp->drive_flags & DRIVE_UDMA) {
2279 /* UltraDMA on a 646U2, 0648 or 0649 */
2280 udma_reg = pciide_pci_read(sc->sc_pc,
2281 sc->sc_tag, CMD_UDMATIM(chp->channel));
2282 if (drvp->UDMA_mode > 2 &&
2283 (pciide_pci_read(sc->sc_pc, sc->sc_tag,
2284 CMD_BICSR) &
2285 CMD_BICSR_80(chp->channel)) == 0)
2286 drvp->UDMA_mode = 2;
2287 if (drvp->UDMA_mode > 2)
2288 udma_reg &= ~CMD_UDMATIM_UDMA33(drive);
2289 else if (sc->sc_wdcdev.UDMA_cap > 2)
2290 udma_reg |= CMD_UDMATIM_UDMA33(drive);
2291 udma_reg |= CMD_UDMATIM_UDMA(drive);
2292 udma_reg &= ~(CMD_UDMATIM_TIM_MASK <<
2293 CMD_UDMATIM_TIM_OFF(drive));
2294 udma_reg |=
2295 (cmd0646_9_tim_udma[drvp->UDMA_mode] <<
2296 CMD_UDMATIM_TIM_OFF(drive));
2297 pciide_pci_write(sc->sc_pc, sc->sc_tag,
2298 CMD_UDMATIM(chp->channel), udma_reg);
2299 } else {
2300 /*
2301 * use Multiword DMA.
2302 * Timings will be used for both PIO and DMA,
2303 * so adjust DMA mode if needed
2304 * if we have a 0646U2/8/9, turn off UDMA
2305 */
2306 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
2307 udma_reg = pciide_pci_read(sc->sc_pc,
2308 sc->sc_tag,
2309 CMD_UDMATIM(chp->channel));
2310 udma_reg &= ~CMD_UDMATIM_UDMA(drive);
2311 pciide_pci_write(sc->sc_pc, sc->sc_tag,
2312 CMD_UDMATIM(chp->channel),
2313 udma_reg);
2314 }
2315 if (drvp->PIO_mode >= 3 &&
2316 (drvp->DMA_mode + 2) > drvp->PIO_mode) {
2317 drvp->DMA_mode = drvp->PIO_mode - 2;
2318 }
2319 tim = cmd0643_9_data_tim_dma[drvp->DMA_mode];
2320 }
2321 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2322 }
2323 pciide_pci_write(sc->sc_pc, sc->sc_tag,
2324 CMD_DATA_TIM(chp->channel, drive), tim);
2325 }
2326 if (idedma_ctl != 0) {
2327 /* Add software bits in status register */
2328 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2329 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
2330 idedma_ctl);
2331 }
2332 pciide_print_modes(cp);
2333 }
2334
2335 void
2336 cmd646_9_irqack(chp)
2337 struct channel_softc *chp;
2338 {
2339 u_int32_t priirq, secirq;
2340 struct pciide_channel *cp = (struct pciide_channel*)chp;
2341 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2342
2343 if (chp->channel == 0) {
2344 priirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_CONF);
2345 pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_CONF, priirq);
2346 } else {
2347 secirq = pciide_pci_read(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23);
2348 pciide_pci_write(sc->sc_pc, sc->sc_tag, CMD_ARTTIM23, secirq);
2349 }
2350 pciide_irqack(chp);
2351 }
2352
2353 void
2354 cy693_chip_map(sc, pa)
2355 struct pciide_softc *sc;
2356 struct pci_attach_args *pa;
2357 {
2358 struct pciide_channel *cp;
2359 pcireg_t interface = PCI_INTERFACE(pa->pa_class);
2360 bus_size_t cmdsize, ctlsize;
2361
2362 if (pciide_chipen(sc, pa) == 0)
2363 return;
2364 /*
2365 * this chip has 2 PCI IDE functions, one for primary and one for
2366 * secondary. So we need to call pciide_mapregs_compat() with
2367 * the real channel
2368 */
2369 if (pa->pa_function == 1) {
2370 sc->sc_cy_compatchan = 0;
2371 } else if (pa->pa_function == 2) {
2372 sc->sc_cy_compatchan = 1;
2373 } else {
2374 printf("%s: unexpected PCI function %d\n",
2375 sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
2376 return;
2377 }
2378 if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
2379 printf("%s: bus-master DMA support present, "
2380 "but unused (no driver support)",
2381 sc->sc_wdcdev.sc_dev.dv_xname);
2382 } else {
2383 printf("%s: hardware does not support DMA",
2384 sc->sc_wdcdev.sc_dev.dv_xname);
2385 }
2386 sc->sc_dma_ok = 0;
2387 printf("\n");
2388
2389 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
2390 WDC_CAPABILITY_MODE;
2391 sc->sc_wdcdev.PIO_cap = 4;
2392 sc->sc_wdcdev.set_modes = cy693_setup_channel;
2393
2394 sc->sc_wdcdev.channels = sc->wdc_chanarray;
2395 sc->sc_wdcdev.nchannels = 1;
2396
2397 /* Only one channel for this chip; if we are here it's enabled */
2398 cp = &sc->pciide_channels[0];
2399 sc->wdc_chanarray[0] = &cp->wdc_channel;
2400 cp->name = PCIIDE_CHANNEL_NAME(0);
2401 cp->wdc_channel.channel = 0;
2402 cp->wdc_channel.wdc = &sc->sc_wdcdev;
2403 cp->wdc_channel.ch_queue =
2404 malloc(sizeof(struct channel_queue), M_DEVBUF, M_NOWAIT);
2405 if (cp->wdc_channel.ch_queue == NULL) {
2406 printf("%s primary channel: "
2407 "can't allocate memory for command queue",
2408 sc->sc_wdcdev.sc_dev.dv_xname);
2409 return;
2410 }
2411 printf("%s: primary channel %s to ",
2412 sc->sc_wdcdev.sc_dev.dv_xname,
2413 (interface & PCIIDE_INTERFACE_SETTABLE(0)) ?
2414 "configured" : "wired");
2415 if (interface & PCIIDE_INTERFACE_PCI(0)) {
2416 printf("native-PCI");
2417 cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize,
2418 pciide_pci_intr);
2419 } else {
2420 printf("compatibility");
2421 cp->hw_ok = pciide_mapregs_compat(pa, cp, sc->sc_cy_compatchan,
2422 &cmdsize, &ctlsize);
2423 }
2424 printf(" mode\n");
2425 cp->wdc_channel.data32iot = cp->wdc_channel.cmd_iot;
2426 cp->wdc_channel.data32ioh = cp->wdc_channel.cmd_ioh;
2427 wdcattach(&cp->wdc_channel);
2428 if (pciide_chan_candisable(cp)) {
2429 pci_conf_write(sc->sc_pc, sc->sc_tag,
2430 PCI_COMMAND_STATUS_REG, 0);
2431 }
2432 pciide_map_compat_intr(pa, cp, sc->sc_cy_compatchan, interface);
2433 if (cp->hw_ok == 0)
2434 return;
2435 WDCDEBUG_PRINT(("cy693_chip_map: old timings reg 0x%x\n",
2436 pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),DEBUG_PROBE);
2437 cy693_setup_channel(&cp->wdc_channel);
2438 WDCDEBUG_PRINT(("cy693_chip_map: new timings reg 0x%x\n",
2439 pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
2440 }
2441
2442 void
2443 cy693_setup_channel(chp)
2444 struct channel_softc *chp;
2445 {
2446 struct ata_drive_datas *drvp;
2447 int drive;
2448 u_int32_t cy_cmd_ctrl;
2449 u_int32_t idedma_ctl;
2450 struct pciide_channel *cp = (struct pciide_channel*)chp;
2451 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2452 cy_cmd_ctrl = idedma_ctl = 0;
2453
2454 for (drive = 0; drive < 2; drive++) {
2455 drvp = &chp->ch_drive[drive];
2456 /* If no drive, skip */
2457 if ((drvp->drive_flags & DRIVE) == 0)
2458 continue;
2459 cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
2460 CY_CMD_CTRL_IOW_PULSE_OFF(drive));
2461 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
2462 CY_CMD_CTRL_IOW_REC_OFF(drive));
2463 cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
2464 CY_CMD_CTRL_IOR_PULSE_OFF(drive));
2465 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
2466 CY_CMD_CTRL_IOR_REC_OFF(drive));
2467 }
2468 pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
2469
2470 pciide_print_modes(cp);
2471
2472 if (idedma_ctl != 0) {
2473 /* Add software bits in status register */
2474 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2475 IDEDMA_CTL, idedma_ctl);
2476 }
2477 }
2478
2479 void
2480 sis_chip_map(sc, pa)
2481 struct pciide_softc *sc;
2482 struct pci_attach_args *pa;
2483 {
2484 struct pciide_channel *cp;
2485 int channel;
2486 u_int8_t sis_ctr0 = pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_CTRL0);
2487 pcireg_t interface = PCI_INTERFACE(pa->pa_class);
2488 pcireg_t rev = PCI_REVISION(pa->pa_class);
2489 bus_size_t cmdsize, ctlsize;
2490
2491 if (pciide_chipen(sc, pa) == 0)
2492 return;
2493 printf("%s: bus-master DMA support present",
2494 sc->sc_wdcdev.sc_dev.dv_xname);
2495 pciide_mapreg_dma(sc, pa);
2496 printf("\n");
2497 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
2498 WDC_CAPABILITY_MODE;
2499 if (sc->sc_dma_ok) {
2500 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
2501 sc->sc_wdcdev.irqack = pciide_irqack;
2502 if (rev >= 0xd0)
2503 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
2504 }
2505
2506 sc->sc_wdcdev.PIO_cap = 4;
2507 sc->sc_wdcdev.DMA_cap = 2;
2508 if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA)
2509 sc->sc_wdcdev.UDMA_cap = 2;
2510 sc->sc_wdcdev.set_modes = sis_setup_channel;
2511
2512 sc->sc_wdcdev.channels = sc->wdc_chanarray;
2513 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
2514
2515 pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_MISC,
2516 pciide_pci_read(sc->sc_pc, sc->sc_tag, SIS_MISC) |
2517 SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
2518
2519 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2520 cp = &sc->pciide_channels[channel];
2521 if (pciide_chansetup(sc, channel, interface) == 0)
2522 continue;
2523 if ((channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
2524 (channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
2525 printf("%s: %s channel ignored (disabled)\n",
2526 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2527 continue;
2528 }
2529 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
2530 pciide_pci_intr);
2531 if (cp->hw_ok == 0)
2532 continue;
2533 if (pciide_chan_candisable(cp)) {
2534 if (channel == 0)
2535 sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
2536 else
2537 sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
2538 pciide_pci_write(sc->sc_pc, sc->sc_tag, SIS_CTRL0,
2539 sis_ctr0);
2540 }
2541 pciide_map_compat_intr(pa, cp, channel, interface);
2542 if (cp->hw_ok == 0)
2543 continue;
2544 sis_setup_channel(&cp->wdc_channel);
2545 }
2546 }
2547
2548 void
2549 sis_setup_channel(chp)
2550 struct channel_softc *chp;
2551 {
2552 struct ata_drive_datas *drvp;
2553 int drive;
2554 u_int32_t sis_tim;
2555 u_int32_t idedma_ctl;
2556 struct pciide_channel *cp = (struct pciide_channel*)chp;
2557 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2558
2559 WDCDEBUG_PRINT(("sis_setup_channel: old timings reg for "
2560 "channel %d 0x%x\n", chp->channel,
2561 pci_conf_read(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel))),
2562 DEBUG_PROBE);
2563 sis_tim = 0;
2564 idedma_ctl = 0;
2565 /* setup DMA if needed */
2566 pciide_channel_dma_setup(cp);
2567
2568 for (drive = 0; drive < 2; drive++) {
2569 drvp = &chp->ch_drive[drive];
2570 /* If no drive, skip */
2571 if ((drvp->drive_flags & DRIVE) == 0)
2572 continue;
2573 /* add timing values, setup DMA if needed */
2574 if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
2575 (drvp->drive_flags & DRIVE_UDMA) == 0)
2576 goto pio;
2577
2578 if (drvp->drive_flags & DRIVE_UDMA) {
2579 /* use Ultra/DMA */
2580 drvp->drive_flags &= ~DRIVE_DMA;
2581 sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
2582 SIS_TIM_UDMA_TIME_OFF(drive);
2583 sis_tim |= SIS_TIM_UDMA_EN(drive);
2584 } else {
2585 /*
2586 * use Multiword DMA
2587 * Timings will be used for both PIO and DMA,
2588 * so adjust DMA mode if needed
2589 */
2590 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
2591 drvp->PIO_mode = drvp->DMA_mode + 2;
2592 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
2593 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
2594 drvp->PIO_mode - 2 : 0;
2595 if (drvp->DMA_mode == 0)
2596 drvp->PIO_mode = 0;
2597 }
2598 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2599 pio: sis_tim |= sis_pio_act[drvp->PIO_mode] <<
2600 SIS_TIM_ACT_OFF(drive);
2601 sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
2602 SIS_TIM_REC_OFF(drive);
2603 }
2604 WDCDEBUG_PRINT(("sis_setup_channel: new timings reg for "
2605 "channel %d 0x%x\n", chp->channel, sis_tim), DEBUG_PROBE);
2606 pci_conf_write(sc->sc_pc, sc->sc_tag, SIS_TIM(chp->channel), sis_tim);
2607 if (idedma_ctl != 0) {
2608 /* Add software bits in status register */
2609 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2610 IDEDMA_CTL, idedma_ctl);
2611 }
2612 pciide_print_modes(cp);
2613 }
2614
2615 void
2616 acer_chip_map(sc, pa)
2617 struct pciide_softc *sc;
2618 struct pci_attach_args *pa;
2619 {
2620 struct pciide_channel *cp;
2621 int channel;
2622 pcireg_t cr, interface;
2623 bus_size_t cmdsize, ctlsize;
2624
2625 if (pciide_chipen(sc, pa) == 0)
2626 return;
2627 printf("%s: bus-master DMA support present",
2628 sc->sc_wdcdev.sc_dev.dv_xname);
2629 pciide_mapreg_dma(sc, pa);
2630 printf("\n");
2631 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
2632 WDC_CAPABILITY_MODE;
2633 if (sc->sc_dma_ok) {
2634 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
2635 sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
2636 sc->sc_wdcdev.irqack = pciide_irqack;
2637 }
2638
2639 sc->sc_wdcdev.PIO_cap = 4;
2640 sc->sc_wdcdev.DMA_cap = 2;
2641 sc->sc_wdcdev.UDMA_cap = 2;
2642 sc->sc_wdcdev.set_modes = acer_setup_channel;
2643 sc->sc_wdcdev.channels = sc->wdc_chanarray;
2644 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
2645
2646 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
2647 (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
2648 ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
2649
2650 /* Enable "microsoft register bits" R/W. */
2651 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
2652 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
2653 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
2654 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
2655 ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
2656 pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
2657 pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
2658 ~ACER_CHANSTATUSREGS_RO);
2659 cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
2660 cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
2661 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
2662 /* Don't use cr, re-read the real register content instead */
2663 interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
2664 PCI_CLASS_REG));
2665
2666 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
2667 cp = &sc->pciide_channels[channel];
2668 if (pciide_chansetup(sc, channel, interface) == 0)
2669 continue;
2670 if ((interface & PCIIDE_CHAN_EN(channel)) == 0) {
2671 printf("%s: %s channel ignored (disabled)\n",
2672 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2673 continue;
2674 }
2675 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
2676 acer_pci_intr);
2677 if (cp->hw_ok == 0)
2678 continue;
2679 if (pciide_chan_candisable(cp)) {
2680 cr &= ~(PCIIDE_CHAN_EN(channel) << PCI_INTERFACE_SHIFT);
2681 pci_conf_write(sc->sc_pc, sc->sc_tag,
2682 PCI_CLASS_REG, cr);
2683 }
2684 pciide_map_compat_intr(pa, cp, channel, interface);
2685 acer_setup_channel(&cp->wdc_channel);
2686 }
2687 }
2688
2689 void
2690 acer_setup_channel(chp)
2691 struct channel_softc *chp;
2692 {
2693 struct ata_drive_datas *drvp;
2694 int drive;
2695 u_int32_t acer_fifo_udma;
2696 u_int32_t idedma_ctl;
2697 struct pciide_channel *cp = (struct pciide_channel*)chp;
2698 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2699
2700 idedma_ctl = 0;
2701 acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
2702 WDCDEBUG_PRINT(("acer_setup_channel: old fifo/udma reg 0x%x\n",
2703 acer_fifo_udma), DEBUG_PROBE);
2704 /* setup DMA if needed */
2705 pciide_channel_dma_setup(cp);
2706
2707 for (drive = 0; drive < 2; drive++) {
2708 drvp = &chp->ch_drive[drive];
2709 /* If no drive, skip */
2710 if ((drvp->drive_flags & DRIVE) == 0)
2711 continue;
2712 WDCDEBUG_PRINT(("acer_setup_channel: old timings reg for "
2713 "channel %d drive %d 0x%x\n", chp->channel, drive,
2714 pciide_pci_read(sc->sc_pc, sc->sc_tag,
2715 ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
2716 /* clear FIFO/DMA mode */
2717 acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
2718 ACER_UDMA_EN(chp->channel, drive) |
2719 ACER_UDMA_TIM(chp->channel, drive, 0x7));
2720
2721 /* add timing values, setup DMA if needed */
2722 if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
2723 (drvp->drive_flags & DRIVE_UDMA) == 0) {
2724 acer_fifo_udma |=
2725 ACER_FTH_OPL(chp->channel, drive, 0x1);
2726 goto pio;
2727 }
2728
2729 acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
2730 if (drvp->drive_flags & DRIVE_UDMA) {
2731 /* use Ultra/DMA */
2732 drvp->drive_flags &= ~DRIVE_DMA;
2733 acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
2734 acer_fifo_udma |=
2735 ACER_UDMA_TIM(chp->channel, drive,
2736 acer_udma[drvp->UDMA_mode]);
2737 } else {
2738 /*
2739 * use Multiword DMA
2740 * Timings will be used for both PIO and DMA,
2741 * so adjust DMA mode if needed
2742 */
2743 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
2744 drvp->PIO_mode = drvp->DMA_mode + 2;
2745 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
2746 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
2747 drvp->PIO_mode - 2 : 0;
2748 if (drvp->DMA_mode == 0)
2749 drvp->PIO_mode = 0;
2750 }
2751 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2752 pio: pciide_pci_write(sc->sc_pc, sc->sc_tag,
2753 ACER_IDETIM(chp->channel, drive),
2754 acer_pio[drvp->PIO_mode]);
2755 }
2756 WDCDEBUG_PRINT(("acer_setup_channel: new fifo/udma reg 0x%x\n",
2757 acer_fifo_udma), DEBUG_PROBE);
2758 pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
2759 if (idedma_ctl != 0) {
2760 /* Add software bits in status register */
2761 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2762 IDEDMA_CTL, idedma_ctl);
2763 }
2764 pciide_print_modes(cp);
2765 }
2766
2767 int
2768 acer_pci_intr(arg)
2769 void *arg;
2770 {
2771 struct pciide_softc *sc = arg;
2772 struct pciide_channel *cp;
2773 struct channel_softc *wdc_cp;
2774 int i, rv, crv;
2775 u_int32_t chids;
2776
2777 rv = 0;
2778 chids = pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CHIDS);
2779 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
2780 cp = &sc->pciide_channels[i];
2781 wdc_cp = &cp->wdc_channel;
2782 /* If a compat channel skip. */
2783 if (cp->compat)
2784 continue;
2785 if (chids & ACER_CHIDS_INT(i)) {
2786 crv = wdcintr(wdc_cp);
2787 if (crv == 0)
2788 printf("%s:%d: bogus intr\n",
2789 sc->sc_wdcdev.sc_dev.dv_xname, i);
2790 else
2791 rv = 1;
2792 }
2793 }
2794 return rv;
2795 }
2796
2797 void
2798 hpt_chip_map(sc, pa)
2799 struct pciide_softc *sc;
2800 struct pci_attach_args *pa;
2801 {
2802 struct pciide_channel *cp;
2803 int i, compatchan, revision;
2804 pcireg_t interface;
2805 bus_size_t cmdsize, ctlsize;
2806
2807 if (pciide_chipen(sc, pa) == 0)
2808 return;
2809 revision = PCI_REVISION(pa->pa_class);
2810
2811 /*
2812 * when the chip is in native mode it identifies itself as a
2813 * 'misc mass storage'. Fake interface in this case.
2814 */
2815 if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
2816 interface = PCI_INTERFACE(pa->pa_class);
2817 } else {
2818 interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
2819 PCIIDE_INTERFACE_PCI(0);
2820 if (revision == HPT370_REV)
2821 interface |= PCIIDE_INTERFACE_PCI(1);
2822 }
2823
2824 printf("%s: bus-master DMA support present",
2825 sc->sc_wdcdev.sc_dev.dv_xname);
2826 pciide_mapreg_dma(sc, pa);
2827 printf("\n");
2828 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
2829 WDC_CAPABILITY_MODE;
2830 if (sc->sc_dma_ok) {
2831 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
2832 sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
2833 sc->sc_wdcdev.irqack = pciide_irqack;
2834 }
2835 sc->sc_wdcdev.PIO_cap = 4;
2836 sc->sc_wdcdev.DMA_cap = 2;
2837 sc->sc_wdcdev.UDMA_cap = 4;
2838
2839 sc->sc_wdcdev.set_modes = hpt_setup_channel;
2840 sc->sc_wdcdev.channels = sc->wdc_chanarray;
2841 if (revision == HPT366_REV) {
2842 /*
2843 * The 366 has 2 PCI IDE functions, one for primary and one
2844 * for secondary. So we need to call pciide_mapregs_compat()
2845 * with the real channel
2846 */
2847 if (pa->pa_function == 0) {
2848 compatchan = 0;
2849 } else if (pa->pa_function == 1) {
2850 compatchan = 1;
2851 } else {
2852 printf("%s: unexpected PCI function %d\n",
2853 sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
2854 return;
2855 }
2856 sc->sc_wdcdev.nchannels = 1;
2857 } else {
2858 sc->sc_wdcdev.nchannels = 2;
2859 }
2860 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
2861 cp = &sc->pciide_channels[i];
2862 if (sc->sc_wdcdev.nchannels > 1) {
2863 compatchan = i;
2864 if((pciide_pci_read(sc->sc_pc, sc->sc_tag,
2865 HPT370_CTRL1(i)) & HPT370_CTRL1_EN) == 0) {
2866 printf("%s: %s channel ignored (disabled)\n",
2867 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2868 continue;
2869 }
2870 }
2871 if (pciide_chansetup(sc, i, interface) == 0)
2872 continue;
2873 if (interface & PCIIDE_INTERFACE_PCI(i)) {
2874 cp->hw_ok = pciide_mapregs_native(pa, cp, &cmdsize,
2875 &ctlsize, hpt_pci_intr);
2876 } else {
2877 cp->hw_ok = pciide_mapregs_compat(pa, cp, compatchan,
2878 &cmdsize, &ctlsize);
2879 }
2880 if (cp->hw_ok == 0)
2881 return;
2882 cp->wdc_channel.data32iot = cp->wdc_channel.cmd_iot;
2883 cp->wdc_channel.data32ioh = cp->wdc_channel.cmd_ioh;
2884 wdcattach(&cp->wdc_channel);
2885 hpt_setup_channel(&cp->wdc_channel);
2886 }
2887 if (revision == HPT370_REV) {
2888 /*
2889 * HPT370_REV has a bit to disable interrupts, make sure
2890 * to clear it
2891 */
2892 pciide_pci_write(sc->sc_pc, sc->sc_tag, HPT_CSEL,
2893 pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL) &
2894 ~HPT_CSEL_IRQDIS);
2895 }
2896 return;
2897 }
2898
2899
2900 void
2901 hpt_setup_channel(chp)
2902 struct channel_softc *chp;
2903 {
2904 struct ata_drive_datas *drvp;
2905 int drive;
2906 int cable;
2907 u_int32_t before, after;
2908 u_int32_t idedma_ctl;
2909 struct pciide_channel *cp = (struct pciide_channel*)chp;
2910 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
2911
2912 cable = pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL);
2913
2914 /* setup DMA if needed */
2915 pciide_channel_dma_setup(cp);
2916
2917 idedma_ctl = 0;
2918
2919 /* Per drive settings */
2920 for (drive = 0; drive < 2; drive++) {
2921 drvp = &chp->ch_drive[drive];
2922 /* If no drive, skip */
2923 if ((drvp->drive_flags & DRIVE) == 0)
2924 continue;
2925 before = pci_conf_read(sc->sc_pc, sc->sc_tag,
2926 HPT_IDETIM(chp->channel, drive));
2927
2928 /* add timing values, setup DMA if needed */
2929 if (drvp->drive_flags & DRIVE_UDMA) {
2930 if ((cable & HPT_CSEL_CBLID(chp->channel)) != 0 &&
2931 drvp->UDMA_mode > 2)
2932 drvp->UDMA_mode = 2;
2933 after = (sc->sc_wdcdev.nchannels == 2) ?
2934 hpt370_udma[drvp->UDMA_mode] :
2935 hpt366_udma[drvp->UDMA_mode];
2936 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2937 } else if (drvp->drive_flags & DRIVE_DMA) {
2938 /*
2939 * use Multiword DMA.
2940 * Timings will be used for both PIO and DMA, so adjust
2941 * DMA mode if needed
2942 */
2943 if (drvp->PIO_mode >= 3 &&
2944 (drvp->DMA_mode + 2) > drvp->PIO_mode) {
2945 drvp->DMA_mode = drvp->PIO_mode - 2;
2946 }
2947 after = (sc->sc_wdcdev.nchannels == 2) ?
2948 hpt370_dma[drvp->DMA_mode] :
2949 hpt366_dma[drvp->DMA_mode];
2950 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
2951 } else {
2952 /* PIO only */
2953 after = (sc->sc_wdcdev.nchannels == 2) ?
2954 hpt370_pio[drvp->PIO_mode] :
2955 hpt366_pio[drvp->PIO_mode];
2956 }
2957 pci_conf_write(sc->sc_pc, sc->sc_tag,
2958 HPT_IDETIM(chp->channel, drive), after);
2959 WDCDEBUG_PRINT(("%s: bus speed register set to 0x%08x "
2960 "(BIOS 0x%08x)\n", drvp->drv_softc->dv_xname,
2961 after, before), DEBUG_PROBE);
2962 }
2963 if (idedma_ctl != 0) {
2964 /* Add software bits in status register */
2965 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2966 IDEDMA_CTL, idedma_ctl);
2967 }
2968 pciide_print_modes(cp);
2969 }
2970
2971 int
2972 hpt_pci_intr(arg)
2973 void *arg;
2974 {
2975 struct pciide_softc *sc = arg;
2976 struct pciide_channel *cp;
2977 struct channel_softc *wdc_cp;
2978 int rv = 0;
2979 int dmastat, i, crv;
2980
2981 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
2982 dmastat = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2983 IDEDMA_CTL + IDEDMA_SCH_OFFSET * i);
2984 if((dmastat & IDEDMA_CTL_INTR) == 0)
2985 continue;
2986 cp = &sc->pciide_channels[i];
2987 wdc_cp = &cp->wdc_channel;
2988 crv = wdcintr(wdc_cp);
2989 if (crv == 0) {
2990 printf("%s:%d: bogus intr\n",
2991 sc->sc_wdcdev.sc_dev.dv_xname, i);
2992 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
2993 IDEDMA_CTL + IDEDMA_SCH_OFFSET * i, dmastat);
2994 } else
2995 rv = 1;
2996 }
2997 return rv;
2998 }
2999
3000
3001 /* A macro to test product */
3002 #define PDC_IS_262(sc) (sc->sc_pp->ide_product == PCI_PRODUCT_PROMISE_ULTRA66)
3003
3004 void
3005 pdc202xx_chip_map(sc, pa)
3006 struct pciide_softc *sc;
3007 struct pci_attach_args *pa;
3008 {
3009 struct pciide_channel *cp;
3010 int channel;
3011 pcireg_t interface, st, mode;
3012 bus_size_t cmdsize, ctlsize;
3013
3014 st = pci_conf_read(sc->sc_pc, sc->sc_tag, PDC2xx_STATE);
3015 WDCDEBUG_PRINT(("pdc202xx_setup_chip: controller state 0x%x\n", st),
3016 DEBUG_PROBE);
3017 if (pciide_chipen(sc, pa) == 0)
3018 return;
3019
3020 /* turn off RAID mode */
3021 st &= ~PDC2xx_STATE_IDERAID;
3022
3023 /*
3024 * can't rely on the PCI_CLASS_REG content if the chip was in raid
3025 * mode. We have to fake interface
3026 */
3027 interface = PCIIDE_INTERFACE_SETTABLE(0) | PCIIDE_INTERFACE_SETTABLE(1);
3028 if (st & PDC2xx_STATE_NATIVE)
3029 interface |= PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
3030
3031 printf("%s: bus-master DMA support present",
3032 sc->sc_wdcdev.sc_dev.dv_xname);
3033 pciide_mapreg_dma(sc, pa);
3034 printf("\n");
3035 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
3036 WDC_CAPABILITY_MODE;
3037 if (sc->sc_dma_ok) {
3038 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
3039 sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
3040 sc->sc_wdcdev.irqack = pciide_irqack;
3041 }
3042 sc->sc_wdcdev.PIO_cap = 4;
3043 sc->sc_wdcdev.DMA_cap = 2;
3044 if (PDC_IS_262(sc))
3045 sc->sc_wdcdev.UDMA_cap = 4;
3046 else
3047 sc->sc_wdcdev.UDMA_cap = 2;
3048 sc->sc_wdcdev.set_modes = pdc202xx_setup_channel;
3049 sc->sc_wdcdev.channels = sc->wdc_chanarray;
3050 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
3051
3052 /* setup failsafe defaults */
3053 mode = 0;
3054 mode = PDC2xx_TIM_SET_PA(mode, pdc2xx_pa[0]);
3055 mode = PDC2xx_TIM_SET_PB(mode, pdc2xx_pb[0]);
3056 mode = PDC2xx_TIM_SET_MB(mode, pdc2xx_dma_mb[0]);
3057 mode = PDC2xx_TIM_SET_MC(mode, pdc2xx_dma_mc[0]);
3058 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
3059 WDCDEBUG_PRINT(("pdc202xx_setup_chip: channel %d drive 0 "
3060 "initial timings 0x%x, now 0x%x\n", channel,
3061 pci_conf_read(sc->sc_pc, sc->sc_tag,
3062 PDC2xx_TIM(channel, 0)), mode | PDC2xx_TIM_IORDYp),
3063 DEBUG_PROBE);
3064 pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_TIM(channel, 0),
3065 mode | PDC2xx_TIM_IORDYp);
3066 WDCDEBUG_PRINT(("pdc202xx_setup_chip: channel %d drive 1 "
3067 "initial timings 0x%x, now 0x%x\n", channel,
3068 pci_conf_read(sc->sc_pc, sc->sc_tag,
3069 PDC2xx_TIM(channel, 1)), mode), DEBUG_PROBE);
3070 pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_TIM(channel, 1),
3071 mode);
3072 }
3073
3074 mode = PDC2xx_SCR_DMA;
3075 if (PDC_IS_262(sc)) {
3076 mode = PDC2xx_SCR_SET_GEN(mode, PDC262_SCR_GEN_LAT);
3077 } else {
3078 /* the BIOS set it up this way */
3079 mode = PDC2xx_SCR_SET_GEN(mode, 0x1);
3080 }
3081 mode = PDC2xx_SCR_SET_I2C(mode, 0x3); /* ditto */
3082 mode = PDC2xx_SCR_SET_POLL(mode, 0x1); /* ditto */
3083 WDCDEBUG_PRINT(("pdc202xx_setup_chip: initial SCR 0x%x, now 0x%x\n",
3084 bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR), mode),
3085 DEBUG_PROBE);
3086 bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR, mode);
3087
3088 /* controller initial state register is OK even without BIOS */
3089 /* Set DMA mode to IDE DMA compatibility */
3090 mode = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_PM);
3091 WDCDEBUG_PRINT(("pdc202xx_setup_chip: primary mode 0x%x", mode ),
3092 DEBUG_PROBE);
3093 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_PM,
3094 mode | 0x1);
3095 mode = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SM);
3096 WDCDEBUG_PRINT((", secondary mode 0x%x\n", mode ), DEBUG_PROBE);
3097 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SM,
3098 mode | 0x1);
3099
3100 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
3101 cp = &sc->pciide_channels[channel];
3102 if (pciide_chansetup(sc, channel, interface) == 0)
3103 continue;
3104 if ((st & (PDC_IS_262(sc) ?
3105 PDC262_STATE_EN(channel):PDC246_STATE_EN(channel))) == 0) {
3106 printf("%s: %s channel ignored (disabled)\n",
3107 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
3108 continue;
3109 }
3110 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
3111 pdc202xx_pci_intr);
3112 if (cp->hw_ok == 0)
3113 continue;
3114 if (pciide_chan_candisable(cp))
3115 st &= ~(PDC_IS_262(sc) ?
3116 PDC262_STATE_EN(channel):PDC246_STATE_EN(channel));
3117 pciide_map_compat_intr(pa, cp, channel, interface);
3118 pdc202xx_setup_channel(&cp->wdc_channel);
3119 }
3120 WDCDEBUG_PRINT(("pdc202xx_setup_chip: new controller state 0x%x\n", st),
3121 DEBUG_PROBE);
3122 pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_STATE, st);
3123 return;
3124 }
3125
3126 void
3127 pdc202xx_setup_channel(chp)
3128 struct channel_softc *chp;
3129 {
3130 struct ata_drive_datas *drvp;
3131 int drive;
3132 pcireg_t mode, st;
3133 u_int32_t idedma_ctl, scr, atapi;
3134 struct pciide_channel *cp = (struct pciide_channel*)chp;
3135 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
3136 int channel = chp->channel;
3137
3138 /* setup DMA if needed */
3139 pciide_channel_dma_setup(cp);
3140
3141 idedma_ctl = 0;
3142
3143 /* Per channel settings */
3144 if (PDC_IS_262(sc)) {
3145 scr = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
3146 PDC262_U66);
3147 st = pci_conf_read(sc->sc_pc, sc->sc_tag, PDC2xx_STATE);
3148 /* Trimm UDMA mode */
3149 if ((st & PDC262_STATE_80P(channel)) != 0 ||
3150 (chp->ch_drive[0].drive_flags & DRIVE_UDMA &&
3151 chp->ch_drive[0].UDMA_mode <= 2) ||
3152 (chp->ch_drive[1].drive_flags & DRIVE_UDMA &&
3153 chp->ch_drive[1].UDMA_mode <= 2)) {
3154 if (chp->ch_drive[0].UDMA_mode > 2)
3155 chp->ch_drive[0].UDMA_mode = 2;
3156 if (chp->ch_drive[1].UDMA_mode > 2)
3157 chp->ch_drive[1].UDMA_mode = 2;
3158 }
3159 /* Set U66 if needed */
3160 if ((chp->ch_drive[0].drive_flags & DRIVE_UDMA &&
3161 chp->ch_drive[0].UDMA_mode > 2) ||
3162 (chp->ch_drive[1].drive_flags & DRIVE_UDMA &&
3163 chp->ch_drive[1].UDMA_mode > 2))
3164 scr |= PDC262_U66_EN(channel);
3165 else
3166 scr &= ~PDC262_U66_EN(channel);
3167 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
3168 PDC262_U66, scr);
3169 if (chp->ch_drive[0].drive_flags & DRIVE_ATAPI ||
3170 chp->ch_drive[1].drive_flags & DRIVE_ATAPI) {
3171 if (((chp->ch_drive[0].drive_flags & DRIVE_UDMA) &&
3172 !(chp->ch_drive[1].drive_flags & DRIVE_UDMA) &&
3173 (chp->ch_drive[1].drive_flags & DRIVE_DMA)) ||
3174 ((chp->ch_drive[1].drive_flags & DRIVE_UDMA) &&
3175 !(chp->ch_drive[0].drive_flags & DRIVE_UDMA) &&
3176 (chp->ch_drive[0].drive_flags & DRIVE_DMA)))
3177 atapi = 0;
3178 else
3179 atapi = PDC262_ATAPI_UDMA;
3180 bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
3181 PDC262_ATAPI(channel), atapi);
3182 }
3183 }
3184 for (drive = 0; drive < 2; drive++) {
3185 drvp = &chp->ch_drive[drive];
3186 /* If no drive, skip */
3187 if ((drvp->drive_flags & DRIVE) == 0)
3188 continue;
3189 mode = 0;
3190 if (drvp->drive_flags & DRIVE_UDMA) {
3191 mode = PDC2xx_TIM_SET_MB(mode,
3192 pdc2xx_udma_mb[drvp->UDMA_mode]);
3193 mode = PDC2xx_TIM_SET_MC(mode,
3194 pdc2xx_udma_mc[drvp->UDMA_mode]);
3195 drvp->drive_flags &= ~DRIVE_DMA;
3196 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
3197 } else if (drvp->drive_flags & DRIVE_DMA) {
3198 mode = PDC2xx_TIM_SET_MB(mode,
3199 pdc2xx_dma_mb[drvp->DMA_mode]);
3200 mode = PDC2xx_TIM_SET_MC(mode,
3201 pdc2xx_dma_mc[drvp->DMA_mode]);
3202 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
3203 } else {
3204 mode = PDC2xx_TIM_SET_MB(mode,
3205 pdc2xx_dma_mb[0]);
3206 mode = PDC2xx_TIM_SET_MC(mode,
3207 pdc2xx_dma_mc[0]);
3208 }
3209 mode = PDC2xx_TIM_SET_PA(mode, pdc2xx_pa[drvp->PIO_mode]);
3210 mode = PDC2xx_TIM_SET_PB(mode, pdc2xx_pb[drvp->PIO_mode]);
3211 if (drvp->drive_flags & DRIVE_ATA)
3212 mode |= PDC2xx_TIM_PRE;
3213 mode |= PDC2xx_TIM_SYNC | PDC2xx_TIM_ERRDY;
3214 if (drvp->PIO_mode >= 3) {
3215 mode |= PDC2xx_TIM_IORDY;
3216 if (drive == 0)
3217 mode |= PDC2xx_TIM_IORDYp;
3218 }
3219 WDCDEBUG_PRINT(("pdc202xx_setup_channel: %s:%d:%d "
3220 "timings 0x%x\n",
3221 sc->sc_wdcdev.sc_dev.dv_xname,
3222 chp->channel, drive, mode), DEBUG_PROBE);
3223 pci_conf_write(sc->sc_pc, sc->sc_tag,
3224 PDC2xx_TIM(chp->channel, drive), mode);
3225 }
3226 if (idedma_ctl != 0) {
3227 /* Add software bits in status register */
3228 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
3229 IDEDMA_CTL, idedma_ctl);
3230 }
3231 pciide_print_modes(cp);
3232 }
3233
3234 int
3235 pdc202xx_pci_intr(arg)
3236 void *arg;
3237 {
3238 struct pciide_softc *sc = arg;
3239 struct pciide_channel *cp;
3240 struct channel_softc *wdc_cp;
3241 int i, rv, crv;
3242 u_int32_t scr;
3243
3244 rv = 0;
3245 scr = bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR);
3246 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
3247 cp = &sc->pciide_channels[i];
3248 wdc_cp = &cp->wdc_channel;
3249 /* If a compat channel skip. */
3250 if (cp->compat)
3251 continue;
3252 if (scr & PDC2xx_SCR_INT(i)) {
3253 crv = wdcintr(wdc_cp);
3254 if (crv == 0)
3255 printf("%s:%d: bogus intr\n",
3256 sc->sc_wdcdev.sc_dev.dv_xname, i);
3257 else
3258 rv = 1;
3259 }
3260 }
3261 return rv;
3262 }
3263
3264 void
3265 opti_chip_map(sc, pa)
3266 struct pciide_softc *sc;
3267 struct pci_attach_args *pa;
3268 {
3269 struct pciide_channel *cp;
3270 bus_size_t cmdsize, ctlsize;
3271 pcireg_t interface;
3272 u_int8_t init_ctrl;
3273 int channel;
3274
3275 if (pciide_chipen(sc, pa) == 0)
3276 return;
3277 printf("%s: bus-master DMA support present",
3278 sc->sc_wdcdev.sc_dev.dv_xname);
3279 pciide_mapreg_dma(sc, pa);
3280 printf("\n");
3281
3282 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
3283 WDC_CAPABILITY_MODE;
3284 sc->sc_wdcdev.PIO_cap = 4;
3285 if (sc->sc_dma_ok) {
3286 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_IRQACK;
3287 sc->sc_wdcdev.irqack = pciide_irqack;
3288 sc->sc_wdcdev.DMA_cap = 2;
3289 }
3290 sc->sc_wdcdev.set_modes = opti_setup_channel;
3291
3292 sc->sc_wdcdev.channels = sc->wdc_chanarray;
3293 sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
3294
3295 init_ctrl = pciide_pci_read(sc->sc_pc, sc->sc_tag,
3296 OPTI_REG_INIT_CONTROL);
3297
3298 interface = PCI_INTERFACE(pa->pa_class);
3299
3300 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
3301 cp = &sc->pciide_channels[channel];
3302 if (pciide_chansetup(sc, channel, interface) == 0)
3303 continue;
3304 if (channel == 1 &&
3305 (init_ctrl & OPTI_INIT_CONTROL_CH2_DISABLE) != 0) {
3306 printf("%s: %s channel ignored (disabled)\n",
3307 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
3308 continue;
3309 }
3310 pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
3311 pciide_pci_intr);
3312 if (cp->hw_ok == 0)
3313 continue;
3314 pciide_map_compat_intr(pa, cp, channel, interface);
3315 if (cp->hw_ok == 0)
3316 continue;
3317 opti_setup_channel(&cp->wdc_channel);
3318 }
3319 }
3320
3321 void
3322 opti_setup_channel(chp)
3323 struct channel_softc *chp;
3324 {
3325 struct ata_drive_datas *drvp;
3326 struct pciide_channel *cp = (struct pciide_channel*)chp;
3327 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
3328 int drive, spd;
3329 int mode[2];
3330 u_int8_t rv, mr;
3331
3332 /*
3333 * The `Delay' and `Address Setup Time' fields of the
3334 * Miscellaneous Register are always zero initially.
3335 */
3336 mr = opti_read_config(chp, OPTI_REG_MISC) & ~OPTI_MISC_INDEX_MASK;
3337 mr &= ~(OPTI_MISC_DELAY_MASK |
3338 OPTI_MISC_ADDR_SETUP_MASK |
3339 OPTI_MISC_INDEX_MASK);
3340
3341 /* Prime the control register before setting timing values */
3342 opti_write_config(chp, OPTI_REG_CONTROL, OPTI_CONTROL_DISABLE);
3343
3344 /* Determine the clockrate of the PCIbus the chip is attached to */
3345 spd = (int) opti_read_config(chp, OPTI_REG_STRAP);
3346 spd &= OPTI_STRAP_PCI_SPEED_MASK;
3347
3348 /* setup DMA if needed */
3349 pciide_channel_dma_setup(cp);
3350
3351 for (drive = 0; drive < 2; drive++) {
3352 drvp = &chp->ch_drive[drive];
3353 /* If no drive, skip */
3354 if ((drvp->drive_flags & DRIVE) == 0) {
3355 mode[drive] = -1;
3356 continue;
3357 }
3358
3359 if ((drvp->drive_flags & DRIVE_DMA)) {
3360 /*
3361 * Timings will be used for both PIO and DMA,
3362 * so adjust DMA mode if needed
3363 */
3364 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
3365 drvp->PIO_mode = drvp->DMA_mode + 2;
3366 if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
3367 drvp->DMA_mode = (drvp->PIO_mode > 2) ?
3368 drvp->PIO_mode - 2 : 0;
3369 if (drvp->DMA_mode == 0)
3370 drvp->PIO_mode = 0;
3371
3372 mode[drive] = drvp->DMA_mode + 5;
3373 } else
3374 mode[drive] = drvp->PIO_mode;
3375
3376 if (drive && mode[0] >= 0 &&
3377 (opti_tim_as[spd][mode[0]] != opti_tim_as[spd][mode[1]])) {
3378 /*
3379 * Can't have two drives using different values
3380 * for `Address Setup Time'.
3381 * Slow down the faster drive to compensate.
3382 */
3383 int d = (opti_tim_as[spd][mode[0]] >
3384 opti_tim_as[spd][mode[1]]) ? 0 : 1;
3385
3386 mode[d] = mode[1-d];
3387 chp->ch_drive[d].PIO_mode = chp->ch_drive[1-d].PIO_mode;
3388 chp->ch_drive[d].DMA_mode = 0;
3389 chp->ch_drive[d].drive_flags &= DRIVE_DMA;
3390 }
3391 }
3392
3393 for (drive = 0; drive < 2; drive++) {
3394 int m;
3395 if ((m = mode[drive]) < 0)
3396 continue;
3397
3398 /* Set the Address Setup Time and select appropriate index */
3399 rv = opti_tim_as[spd][m] << OPTI_MISC_ADDR_SETUP_SHIFT;
3400 rv |= OPTI_MISC_INDEX(drive);
3401 opti_write_config(chp, OPTI_REG_MISC, mr | rv);
3402
3403 /* Set the pulse width and recovery timing parameters */
3404 rv = opti_tim_cp[spd][m] << OPTI_PULSE_WIDTH_SHIFT;
3405 rv |= opti_tim_rt[spd][m] << OPTI_RECOVERY_TIME_SHIFT;
3406 opti_write_config(chp, OPTI_REG_READ_CYCLE_TIMING, rv);
3407 opti_write_config(chp, OPTI_REG_WRITE_CYCLE_TIMING, rv);
3408
3409 /* Set the Enhanced Mode register appropriately */
3410 rv = pciide_pci_read(sc->sc_pc, sc->sc_tag, OPTI_REG_ENH_MODE);
3411 rv &= ~OPTI_ENH_MODE_MASK(chp->channel, drive);
3412 rv |= OPTI_ENH_MODE(chp->channel, drive, opti_tim_em[m]);
3413 pciide_pci_write(sc->sc_pc, sc->sc_tag, OPTI_REG_ENH_MODE, rv);
3414 }
3415
3416 /* Finally, enable the timings */
3417 opti_write_config(chp, OPTI_REG_CONTROL, OPTI_CONTROL_ENABLE);
3418
3419 pciide_print_modes(cp);
3420 }
3421