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