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