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