pciide.c revision 1.18 1 /* $NetBSD: pciide.c,v 1.18 1998/11/21 15:55:31 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 channel_softc *wdc_cp = arg;
690
691 #ifdef DIAGNOSTIC
692 struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
693 struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
694 /* should only be called for a compat channel */
695 if (cp->compat == 0)
696 panic("pciide compat intr called for non-compat chan %p\n", cp);
697 #endif
698 return (wdcintr(wdc_cp));
699 }
700
701 int
702 pciide_pci_intr(arg)
703 void *arg;
704 {
705 struct pciide_softc *sc = arg;
706 struct pciide_channel *cp;
707 struct channel_softc *wdc_cp;
708 int i, rv, crv;
709
710 rv = 0;
711 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
712 cp = &sc->pciide_channels[i];
713 wdc_cp = &cp->wdc_channel;
714
715 /* If a compat channel skip. */
716 if (cp->compat)
717 continue;
718 /* if this channel not waiting for intr, skip */
719 if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
720 continue;
721
722 crv = wdcintr(wdc_cp);
723 if (crv == 0)
724 ; /* leave rv alone */
725 else if (crv == 1)
726 rv = 1; /* claim the intr */
727 else if (rv == 0) /* crv should be -1 in this case */
728 rv = crv; /* if we've done no better, take it */
729 }
730 return (rv);
731 }
732
733 int
734 pciide_dma_table_setup(sc, channel, drive)
735 struct pciide_softc *sc;
736 int channel, drive;
737 {
738 bus_dma_segment_t seg;
739 int error, rseg;
740 const bus_size_t dma_table_size =
741 sizeof(struct idedma_table) * NIDEDMA_TABLES;
742 struct pciide_dma_maps *dma_maps =
743 &sc->pciide_channels[channel].dma_maps[drive];
744
745 /* Allocate memory for the DMA tables and map it */
746 if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
747 IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
748 BUS_DMA_NOWAIT)) != 0) {
749 printf("%s:%d: unable to allocate table DMA for "
750 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
751 channel, drive, error);
752 return error;
753 }
754 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
755 dma_table_size,
756 (caddr_t *)&dma_maps->dma_table,
757 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
758 printf("%s:%d: unable to map table DMA for"
759 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
760 channel, drive, error);
761 return error;
762 }
763 WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
764 "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
765 seg.ds_addr), DEBUG_PROBE);
766
767 /* Create and load table DMA map for this disk */
768 if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
769 1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
770 &dma_maps->dmamap_table)) != 0) {
771 printf("%s:%d: unable to create table DMA map for "
772 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
773 channel, drive, error);
774 return error;
775 }
776 if ((error = bus_dmamap_load(sc->sc_dmat,
777 dma_maps->dmamap_table,
778 dma_maps->dma_table,
779 dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
780 printf("%s:%d: unable to load table DMA map for "
781 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
782 channel, drive, error);
783 return error;
784 }
785 WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
786 dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
787 /* Create a xfer DMA map for this drive */
788 if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
789 NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
790 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
791 &dma_maps->dmamap_xfer)) != 0) {
792 printf("%s:%d: unable to create xfer DMA map for "
793 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
794 channel, drive, error);
795 return error;
796 }
797 return 0;
798 }
799
800 int
801 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
802 void *v;
803 int channel, drive;
804 void *databuf;
805 size_t datalen;
806 int flags;
807 {
808 struct pciide_softc *sc = v;
809 int error, seg;
810 struct pciide_dma_maps *dma_maps =
811 &sc->pciide_channels[channel].dma_maps[drive];
812
813 error = bus_dmamap_load(sc->sc_dmat,
814 dma_maps->dmamap_xfer,
815 databuf, datalen, NULL, BUS_DMA_NOWAIT);
816 if (error) {
817 printf("%s:%d: unable to load xfer DMA map for"
818 "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
819 channel, drive, error);
820 return error;
821 }
822
823 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
824 dma_maps->dmamap_xfer->dm_mapsize,
825 (flags & WDC_DMA_READ) ?
826 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
827
828 for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
829 #ifdef DIAGNOSTIC
830 /* A segment must not cross a 64k boundary */
831 {
832 u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
833 u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
834 if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
835 ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
836 printf("pciide_dma: segment %d physical addr 0x%lx"
837 " len 0x%lx not properly aligned\n",
838 seg, phys, len);
839 panic("pciide_dma: buf align");
840 }
841 }
842 #endif
843 dma_maps->dma_table[seg].base_addr =
844 dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
845 dma_maps->dma_table[seg].byte_count =
846 dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
847 IDEDMA_BYTE_COUNT_MASK;
848 WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
849 seg, dma_maps->dma_table[seg].byte_count,
850 dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
851
852 }
853 dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
854 IDEDMA_BYTE_COUNT_EOT;
855
856 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
857 dma_maps->dmamap_table->dm_mapsize,
858 BUS_DMASYNC_PREWRITE);
859
860 /* Maps are ready. Start DMA function */
861 #ifdef DIAGNOSTIC
862 if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
863 printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
864 dma_maps->dmamap_table->dm_segs[0].ds_addr);
865 panic("pciide_dma_init: table align");
866 }
867 #endif
868
869 /* Clear status bits */
870 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
871 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
872 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
873 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
874 /* Write table addr */
875 bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
876 IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
877 dma_maps->dmamap_table->dm_segs[0].ds_addr);
878 /* set read/write */
879 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
880 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
881 (flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE: 0);
882 return 0;
883 }
884
885 void
886 pciide_dma_start(v, channel, drive, flags)
887 void *v;
888 int channel, drive, flags;
889 {
890 struct pciide_softc *sc = v;
891
892 WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
893 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
894 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
895 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
896 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
897 }
898
899 int
900 pciide_dma_finish(v, channel, drive, flags)
901 void *v;
902 int channel, drive;
903 int flags;
904 {
905 struct pciide_softc *sc = v;
906 u_int8_t status;
907 struct pciide_dma_maps *dma_maps =
908 &sc->pciide_channels[channel].dma_maps[drive];
909
910 /* Unload the map of the data buffer */
911 bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
912 dma_maps->dmamap_xfer->dm_mapsize,
913 (flags & WDC_DMA_READ) ?
914 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
915 bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
916
917 status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
918 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
919 WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
920 DEBUG_XFERS);
921
922 /* stop DMA channel */
923 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
924 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
925 bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
926 IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
927
928 /* Clear status bits */
929 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
930 IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
931 status);
932
933 if ((status & IDEDMA_CTL_ERR) != 0) {
934 printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
935 sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
936 return -1;
937 }
938
939 if ((flags & WDC_DMA_POLL) == 0 && (status & IDEDMA_CTL_INTR) == 0) {
940 printf("%s:%d:%d: Bus-Master DMA error: missing interrupt, "
941 "status=0x%x\n", sc->sc_wdcdev.sc_dev.dv_xname, channel,
942 drive, status);
943 return -1;
944 }
945
946 if ((status & IDEDMA_CTL_ACT) != 0) {
947 /* data underrun, may be a valid condition for ATAPI */
948 return 1;
949 }
950
951 return 0;
952 }
953
954 /* some common code used by several chip channel_map */
955 void
956 pciide_mapchan(sc, pa, cp, interface, cmdsizep, ctlsizep)
957 struct pciide_softc *sc;
958 struct pci_attach_args *pa;
959 int interface;
960 struct pciide_channel *cp;
961 bus_size_t *cmdsizep, *ctlsizep;
962 {
963 struct channel_softc *wdc_cp = &cp->wdc_channel;
964
965 if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
966 cp->hw_ok = pciide_mapregs_native(sc, pa, cp,
967 cmdsizep, ctlsizep);
968 else
969 cp->hw_ok = pciide_mapregs_compat(sc, pa, cp, wdc_cp->channel,
970 cmdsizep, ctlsizep);
971 if (cp->hw_ok == 0)
972 return;
973 wdc_cp->data32iot = wdc_cp->cmd_iot;
974 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
975 wdcattach(wdc_cp);
976 }
977
978 /*
979 * Generic code to call to know if a channel can be disabled. Return 1
980 * if channel can be disabled, 0 if not
981 */
982 int
983 pciiide_chan_candisable(sc, pa, cp, cmdsize, ctlsize)
984 struct pciide_softc *sc;
985 struct pci_attach_args *pa;
986 struct pciide_channel *cp;
987 bus_size_t cmdsize, ctlsize;
988 {
989 struct channel_softc *wdc_cp = &cp->wdc_channel;
990
991 if ((wdc_cp->ch_drive[0].drive_flags & DRIVE) == 0 &&
992 (wdc_cp->ch_drive[1].drive_flags & DRIVE) == 0) {
993 printf("%s: disabling %s channel (no drives)\n",
994 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
995 cp->hw_ok = 0;
996 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, cmdsize);
997 bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh, ctlsize);
998 return 1;
999 }
1000 return 0;
1001 }
1002
1003 /*
1004 * generic code to map the compat intr if hw_ok=1 and it is a compat channel.
1005 * Set hw_ok=0 on failure
1006 */
1007 void
1008 pciide_map_compat_intr(sc, pa, cp, compatchan, interface)
1009 struct pciide_softc *sc;
1010 struct pci_attach_args *pa;
1011 struct pciide_channel *cp;
1012 int compatchan, interface;
1013 {
1014 struct channel_softc *wdc_cp = &cp->wdc_channel;
1015
1016 if (cp->hw_ok == 0)
1017 return;
1018 if ((interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel)) != 0)
1019 return;
1020
1021 cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
1022 pa, compatchan, pciide_compat_intr, wdc_cp);
1023 if (cp->ih == NULL) {
1024 printf("%s: no compatibility interrupt for use by %s "
1025 "channel\n", sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1026 cp->hw_ok = 0;
1027 }
1028 }
1029
1030 void
1031 pciide_print_modes(sc)
1032 struct pciide_softc *sc;
1033 {
1034 int channel, drive;
1035 struct channel_softc *chp;
1036 struct ata_drive_datas *drvp;
1037
1038 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1039 chp = &sc->pciide_channels[channel].wdc_channel;
1040 for (drive = 0; drive < 2; drive++) {
1041 drvp = &chp->ch_drive[drive];
1042 if ((drvp->drive_flags & DRIVE) == 0)
1043 continue;
1044 printf("%s(%s:%d:%d): using PIO mode %d",
1045 drvp->drv_softc->dv_xname,
1046 sc->sc_wdcdev.sc_dev.dv_xname,
1047 channel, drive, drvp->PIO_mode);
1048 if (drvp->drive_flags & DRIVE_DMA)
1049 printf(", DMA mode %d", drvp->DMA_mode);
1050 if (drvp->drive_flags & DRIVE_UDMA)
1051 printf(", Ultra-DMA mode %d", drvp->UDMA_mode);
1052 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
1053 printf(" (using DMA data transfers)");
1054 printf("\n");
1055 }
1056 }
1057 }
1058
1059 void
1060 default_setup_cap(sc)
1061 struct pciide_softc *sc;
1062 {
1063 if (sc->sc_dma_ok)
1064 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
1065 sc->sc_wdcdev.pio_mode = 0;
1066 sc->sc_wdcdev.dma_mode = 0;
1067 }
1068
1069 void
1070 default_setup_chip(sc, pc, tag)
1071 struct pciide_softc *sc;
1072 pci_chipset_tag_t pc;
1073 pcitag_t tag;
1074 {
1075 int channel, drive, idedma_ctl;
1076 struct channel_softc *chp;
1077 struct ata_drive_datas *drvp;
1078
1079 if (sc->sc_dma_ok == 0)
1080 return; /* nothing to do */
1081
1082 /* Allocate DMA maps */
1083 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1084 idedma_ctl = 0;
1085 chp = &sc->pciide_channels[channel].wdc_channel;
1086 for (drive = 0; drive < 2; drive++) {
1087 drvp = &chp->ch_drive[drive];
1088 /* If no drive, skip */
1089 if ((drvp->drive_flags & DRIVE) == 0)
1090 continue;
1091 if ((drvp->drive_flags & DRIVE_DMA) == 0)
1092 continue;
1093 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1094 /* Abort DMA setup */
1095 printf("%s:%d:%d: can't allocate DMA maps, "
1096 "using PIO transfers\n",
1097 sc->sc_wdcdev.sc_dev.dv_xname,
1098 channel, drive);
1099 drvp->drive_flags &= ~DRIVE_DMA;
1100 }
1101 printf("%s:%d:%d: using DMA data tranferts\n",
1102 sc->sc_wdcdev.sc_dev.dv_xname,
1103 channel, drive);
1104 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1105 }
1106 if (idedma_ctl != 0) {
1107 /* Add software bits in status register */
1108 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1109 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1110 idedma_ctl);
1111 }
1112 }
1113
1114 }
1115
1116 void
1117 default_channel_map(sc, pa, cp)
1118 struct pciide_softc *sc;
1119 struct pci_attach_args *pa;
1120 struct pciide_channel *cp;
1121 {
1122 bus_size_t cmdsize, ctlsize;
1123 pcireg_t csr;
1124 const char *failreason = NULL;
1125 struct channel_softc *wdc_cp = &cp->wdc_channel;
1126 int interface =
1127 PCI_INTERFACE(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
1128
1129 if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->channel))
1130 cp->hw_ok = pciide_mapregs_native(sc, pa, cp,
1131 &cmdsize, &ctlsize);
1132 else
1133 cp->hw_ok = pciide_mapregs_compat(sc, pa, cp, wdc_cp->channel,
1134 &cmdsize, &ctlsize);
1135 if (cp->hw_ok == 0)
1136 return;
1137
1138 /*
1139 * Check to see if something appears to be there.
1140 */
1141 if (!wdcprobe(wdc_cp)) {
1142 failreason = "not responding; disabled or no drives?";
1143 goto out;
1144 }
1145
1146 /*
1147 * Now, make sure it's actually attributable to this PCI IDE
1148 * channel by trying to access the channel again while the
1149 * PCI IDE controller's I/O space is disabled. (If the
1150 * channel no longer appears to be there, it belongs to
1151 * this controller.) YUCK!
1152 */
1153 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1154 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1155 csr & ~PCI_COMMAND_IO_ENABLE);
1156 if (wdcprobe(wdc_cp))
1157 failreason = "other hardware responding at addresses";
1158 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
1159
1160 out:
1161 if (failreason) {
1162 printf("%s: %s channel ignored (%s)\n",
1163 sc->sc_wdcdev.sc_dev.dv_xname, cp->name,
1164 failreason);
1165 cp->hw_ok = 0;
1166 bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh, cmdsize);
1167 bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh, ctlsize);
1168 }
1169 pciide_map_compat_intr(sc, pa, cp, wdc_cp->channel, interface);
1170 if (cp->hw_ok) {
1171 wdc_cp->data32iot = wdc_cp->cmd_iot;
1172 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1173 wdcattach(wdc_cp);
1174 }
1175 }
1176
1177 void
1178 piix_setup_cap(sc)
1179 struct pciide_softc *sc;
1180 {
1181 if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
1182 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
1183 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1184 WDC_CAPABILITY_DMA;
1185 sc->sc_wdcdev.pio_mode = 4;
1186 sc->sc_wdcdev.dma_mode = 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_mode = 4;
1552 sc->sc_wdcdev.dma_mode = 2;
1553
1554 }
1555 void
1556 apollo_setup_chip(sc, pc, tag)
1557 struct pciide_softc *sc;
1558 pci_chipset_tag_t pc;
1559 pcitag_t tag;
1560 {
1561 u_int32_t udmatim_reg, datatim_reg;
1562 u_int8_t idedma_ctl;
1563 int mode;
1564 int channel, drive;
1565 struct channel_softc *chp;
1566 struct ata_drive_datas *drvp;
1567
1568 WDCDEBUG_PRINT(("apollo_setup_chip: old APO_IDECONF=0x%x, "
1569 "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1570 pci_conf_read(pc, tag, APO_IDECONF),
1571 pci_conf_read(pc, tag, APO_CTLMISC),
1572 pci_conf_read(pc, tag, APO_DATATIM),
1573 pci_conf_read(pc, tag, APO_UDMA)),
1574 DEBUG_PROBE);
1575
1576 datatim_reg = 0;
1577 udmatim_reg = 0;
1578 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1579 chp = &sc->pciide_channels[channel].wdc_channel;
1580 idedma_ctl = 0;
1581 for (drive = 0; drive < 2; drive++) {
1582 drvp = &chp->ch_drive[drive];
1583 /* If no drive, skip */
1584 if ((drvp->drive_flags & DRIVE) == 0)
1585 continue;
1586 /* add timing values, setup DMA if needed */
1587 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1588 (drvp->drive_flags & DRIVE_UDMA) == 0) ||
1589 sc->sc_dma_ok == 0) {
1590 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1591 mode = drvp->PIO_mode;
1592 goto pio;
1593 }
1594 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1595 /* Abort DMA setup */
1596 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1597 mode = drvp->PIO_mode;
1598 goto pio;
1599 }
1600 if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
1601 (drvp->drive_flags & DRIVE_UDMA)) {
1602 /* use Ultra/DMA */
1603 drvp->drive_flags &= ~DRIVE_DMA;
1604 udmatim_reg |= APO_UDMA_EN(channel, drive) |
1605 APO_UDMA_EN_MTH(channel, drive) |
1606 APO_UDMA_TIME(channel, drive,
1607 apollo_udma_tim[drvp->UDMA_mode]);
1608 /* can use PIO timings, MW DMA unused */
1609 mode = drvp->PIO_mode;
1610 } else {
1611 /* use Multiword DMA */
1612 drvp->drive_flags &= ~DRIVE_UDMA;
1613 /* mode = min(pio, dma+2) */
1614 if (drvp->PIO_mode <= (drvp->DMA_mode +2))
1615 mode = drvp->PIO_mode;
1616 else
1617 mode = drvp->DMA_mode;
1618 }
1619 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1620
1621 pio: /* setup PIO mode */
1622 datatim_reg |=
1623 APO_DATATIM_PULSE(channel, drive,
1624 apollo_pio_set[mode]) |
1625 APO_DATATIM_RECOV(channel, drive,
1626 apollo_pio_rec[mode]);
1627 drvp->PIO_mode = mode;
1628 drvp->DMA_mode = mode - 2;
1629 }
1630 if (idedma_ctl != 0) {
1631 /* Add software bits in status register */
1632 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1633 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1634 idedma_ctl);
1635 }
1636 }
1637 pciide_print_modes(sc);
1638 WDCDEBUG_PRINT(("apollo_setup_chip: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
1639 datatim_reg, udmatim_reg), DEBUG_PROBE);
1640 pci_conf_write(pc, tag, APO_DATATIM, datatim_reg);
1641 pci_conf_write(pc, tag, APO_UDMA, udmatim_reg);
1642 }
1643
1644 void
1645 apollo_channel_map(sc, pa, cp)
1646 struct pciide_softc *sc;
1647 struct pci_attach_args *pa;
1648 struct pciide_channel *cp;
1649 {
1650 bus_size_t cmdsize, ctlsize;
1651 struct channel_softc *wdc_cp = &cp->wdc_channel;
1652 u_int32_t ideconf = pci_conf_read(pa->pa_pc, pa->pa_tag, APO_IDECONF);
1653 int interface =
1654 PCI_INTERFACE(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
1655
1656 if ((ideconf & APO_IDECONF_EN(wdc_cp->channel)) == 0) {
1657 printf("%s: %s channel ignored (disabled)\n",
1658 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1659 return;
1660 }
1661
1662 pciide_mapchan(sc, pa, cp, interface, &cmdsize, &ctlsize);
1663 if (cp->hw_ok == 0)
1664 return;
1665 if (pciiide_chan_candisable(sc, pa, cp, cmdsize, ctlsize)) {
1666 ideconf &= ~APO_IDECONF_EN(wdc_cp->channel);
1667 pci_conf_write(pa->pa_pc, pa->pa_tag, APO_IDECONF, ideconf);
1668 }
1669 pciide_map_compat_intr(sc, pa, cp, wdc_cp->channel, interface);
1670 }
1671
1672 void
1673 cmd_channel_map(sc, pa, cp)
1674 struct pciide_softc *sc;
1675 struct pci_attach_args *pa;
1676 struct pciide_channel *cp;
1677 {
1678 bus_size_t cmdsize, ctlsize;
1679 struct channel_softc *wdc_cp = &cp->wdc_channel;
1680 u_int8_t ctrl = pciide_pci_read(pa->pa_pc, pa->pa_tag, CMD_CTRL);
1681 int interface =
1682 PCI_INTERFACE(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
1683
1684 /*
1685 * with a CMD PCI64x, if we get here, the first channel is enabled:
1686 * there's no way to disable the first channel without disabling
1687 * the whole device
1688 */
1689 if (wdc_cp->channel != 0 && (ctrl & CMD_CTRL_2PORT) == 0) {
1690 printf("%s: %s channel ignored (disabled)\n",
1691 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
1692 return;
1693 }
1694
1695 pciide_mapchan(sc, pa, cp, interface, &cmdsize, &ctlsize);
1696 if (cp->hw_ok == 0)
1697 return;
1698 if (wdc_cp->channel == 1) {
1699 if (pciiide_chan_candisable(sc, pa, cp, cmdsize, ctlsize)) {
1700 ctrl &= ~CMD_CTRL_2PORT;
1701 pciide_pci_write(pa->pa_pc, pa->pa_tag,
1702 CMD_CTRL_2PORT, ctrl);
1703 }
1704 }
1705 pciide_map_compat_intr(sc, pa, cp, wdc_cp->channel, interface);
1706 }
1707
1708 void
1709 cmd0643_6_setup_cap(sc)
1710 struct pciide_softc *sc;
1711 {
1712 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1713 WDC_CAPABILITY_DMA;
1714 sc->sc_wdcdev.pio_mode = 4;
1715 sc->sc_wdcdev.dma_mode = 2;
1716 }
1717
1718 void
1719 cmd0643_6_setup_chip(sc, pc, tag)
1720 struct pciide_softc *sc;
1721 pci_chipset_tag_t pc;
1722 pcitag_t tag;
1723 {
1724 struct channel_softc *chp;
1725 struct ata_drive_datas *drvp;
1726 int channel, drive;
1727 u_int8_t tim;
1728 u_int32_t idedma_ctl;
1729
1730 WDCDEBUG_PRINT(("cmd0643_6_setup_chip: old timings reg 0x%x 0x%x\n",
1731 pci_conf_read(pc, tag, 0x54), pci_conf_read(pc, tag, 0x58)),
1732 DEBUG_PROBE);
1733 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1734 chp = &sc->pciide_channels[channel].wdc_channel;
1735 idedma_ctl = 0;
1736 for (drive = 0; drive < 2; drive++) {
1737 drvp = &chp->ch_drive[drive];
1738 /* If no drive, skip */
1739 if ((drvp->drive_flags & DRIVE) == 0)
1740 continue;
1741 /* add timing values, setup DMA if needed */
1742 tim = cmd0643_6_data_tim_pio[drvp->PIO_mode];
1743 if ((drvp->drive_flags & DRIVE_DMA) == 0 ||
1744 sc->sc_dma_ok == 0) {
1745 drvp->drive_flags &= ~DRIVE_DMA;
1746 goto end;
1747 }
1748 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1749 /* Abort DMA setup */
1750 drvp->drive_flags &= ~DRIVE_DMA;
1751 goto end;
1752 }
1753 /*
1754 * use Multiword DMA.
1755 * Timings will be used for both PIO and DMA, so adjust
1756 * DMA mode if needed
1757 */
1758 if (drvp->PIO_mode >= 3 &&
1759 (drvp->DMA_mode + 2) > drvp->PIO_mode) {
1760 drvp->DMA_mode = drvp->PIO_mode - 2;
1761 }
1762 tim = cmd0643_6_data_tim_dma[drvp->DMA_mode];
1763 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1764
1765 end: pciide_pci_write(pc, tag,
1766 CMD_DATA_TIM(channel, drive), tim);
1767 printf("%s(%s:%d:%d): using PIO mode %d",
1768 drvp->drv_softc->dv_xname,
1769 sc->sc_wdcdev.sc_dev.dv_xname,
1770 channel, drive, drvp->PIO_mode);
1771 if (drvp->drive_flags & DRIVE_DMA)
1772 printf(", DMA mode %d", drvp->DMA_mode);
1773 printf("\n");
1774 }
1775 if (idedma_ctl != 0) {
1776 /* Add software bits in status register */
1777 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1778 IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
1779 idedma_ctl);
1780 }
1781 }
1782 WDCDEBUG_PRINT(("cmd0643_6_setup_chip: timings reg now 0x%x 0x%x\n",
1783 pci_conf_read(pc, tag, 0x54), pci_conf_read(pc, tag, 0x58)),
1784 DEBUG_PROBE);
1785 }
1786
1787 void
1788 cy693_setup_cap(sc)
1789 struct pciide_softc *sc;
1790 {
1791 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1792 WDC_CAPABILITY_DMA;
1793 sc->sc_wdcdev.pio_mode = 4;
1794 sc->sc_wdcdev.dma_mode = 2;
1795 }
1796
1797 void
1798 cy693_setup_chip(sc, pc, tag)
1799 struct pciide_softc *sc;
1800 pci_chipset_tag_t pc;
1801 pcitag_t tag;
1802 {
1803 struct channel_softc *chp;
1804 struct ata_drive_datas *drvp;
1805 int drive;
1806 u_int32_t cy_cmd_ctrl;
1807 u_int32_t idedma_ctl;
1808
1809 WDCDEBUG_PRINT(("cy693_setup_chip: old timings reg 0x%x\n",
1810 pci_conf_read(pc, tag, CY_CMD_CTRL)), DEBUG_PROBE);
1811 cy_cmd_ctrl = idedma_ctl = 0;
1812 chp = &sc->pciide_channels[0].wdc_channel; /* Only one channel */
1813 for (drive = 0; drive < 2; drive++) {
1814 drvp = &chp->ch_drive[drive];
1815 /* If no drive, skip */
1816 if ((drvp->drive_flags & DRIVE) == 0)
1817 continue;
1818 /* add timing values, setup DMA if needed */
1819 if ((drvp->drive_flags & DRIVE_DMA) == 0 ||
1820 sc->sc_dma_ok == 0) {
1821 drvp->drive_flags &= ~DRIVE_DMA;
1822 goto pio;
1823 }
1824 if (pciide_dma_table_setup(sc, 0, drive) != 0) {
1825 /* Abort DMA setup */
1826 drvp->drive_flags &= ~DRIVE_DMA;
1827 goto pio;
1828 }
1829 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1830 /*
1831 * use Multiword DMA
1832 * Timings will be used for both PIO and DMA, so adjust
1833 * DMA mode if needed
1834 */
1835 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
1836 drvp->PIO_mode = drvp->DMA_mode + 2;
1837 if (drvp->DMA_mode == 0)
1838 drvp->PIO_mode = 0;
1839 pio: cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
1840 CY_CMD_CTRL_IOW_PULSE_OFF(drive));
1841 cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
1842 CY_CMD_CTRL_IOW_REC_OFF(drive));
1843 }
1844 WDCDEBUG_PRINT(("cy693_setup_chip: new timings reg 0x%x\n",
1845 cy_cmd_ctrl), DEBUG_PROBE);
1846 pci_conf_write(pc, tag, CY_CMD_CTRL, cy_cmd_ctrl);
1847 pciide_print_modes(sc);
1848 if (idedma_ctl != 0) {
1849 /* Add software bits in status register */
1850 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1851 IDEDMA_CTL, idedma_ctl);
1852 }
1853 }
1854
1855 void
1856 cy693_channel_map(sc, pa, cp)
1857 struct pciide_softc *sc;
1858 struct pci_attach_args *pa;
1859 struct pciide_channel *cp;
1860 {
1861 bus_size_t cmdsize, ctlsize;
1862 struct channel_softc *wdc_cp = &cp->wdc_channel;
1863 int interface =
1864 PCI_INTERFACE(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
1865 int compatchan;
1866
1867 #ifdef DIAGNOSTIC
1868 if (wdc_cp->channel != 0)
1869 panic("cy693_channel_map: channel %d", wdc_cp->channel);
1870 #endif
1871
1872 /*
1873 * this chip has 2 PCI IDE functions, one for primary and one for
1874 * secondary. So we need to call pciide_mapregs_compat() with
1875 * the real channel
1876 */
1877 if (pa->pa_function == 1) {
1878 compatchan = 0;
1879 } else if (pa->pa_function == 2) {
1880 compatchan = 1;
1881 } else {
1882 printf("%s: unexpected PCI function %d\n",
1883 sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function);
1884 cp->hw_ok = 0;
1885 return;
1886 }
1887
1888 /* Only one channel for this chip; if we are here it's enabled */
1889 if (interface & PCIIDE_INTERFACE_PCI(0))
1890 cp->hw_ok = pciide_mapregs_native(sc, pa, cp,
1891 &cmdsize, &ctlsize);
1892 else
1893 cp->hw_ok = pciide_mapregs_compat(sc, pa, cp, compatchan,
1894 &cmdsize, &ctlsize);
1895 if (cp->hw_ok == 0)
1896 return;
1897 wdc_cp->data32iot = wdc_cp->cmd_iot;
1898 wdc_cp->data32ioh = wdc_cp->cmd_ioh;
1899 wdcattach(wdc_cp);
1900 if (pciiide_chan_candisable(sc, pa, cp, cmdsize, ctlsize)) {
1901 pci_conf_write(pa->pa_pc, pa->pa_tag,
1902 PCI_COMMAND_STATUS_REG, 0);
1903 }
1904 pciide_map_compat_intr(sc, pa, cp, compatchan, interface);
1905 }
1906
1907 void
1908 sis_setup_cap(sc)
1909 struct pciide_softc *sc;
1910 {
1911 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_MODE |
1912 WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
1913 sc->sc_wdcdev.pio_mode = 4;
1914 sc->sc_wdcdev.dma_mode = 2;
1915 }
1916
1917 void
1918 sis_setup_chip(sc, pc, tag)
1919 struct pciide_softc *sc;
1920 pci_chipset_tag_t pc;
1921 pcitag_t tag;
1922 {
1923 struct channel_softc *chp;
1924 struct ata_drive_datas *drvp;
1925 int channel, drive;
1926 u_int32_t sis_tim;
1927 u_int32_t idedma_ctl;
1928
1929 idedma_ctl = 0;
1930 for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
1931 chp = &sc->pciide_channels[0].wdc_channel; /* Only one channel */
1932 WDCDEBUG_PRINT(("sis_setup_chip: old timings reg for "
1933 "channel %d 0x%x\n", channel,
1934 pci_conf_read(pc, tag, SIS_TIM(channel))), DEBUG_PROBE);
1935 sis_tim = 0;
1936 for (drive = 0; drive < 2; drive++) {
1937 drvp = &chp->ch_drive[drive];
1938 /* If no drive, skip */
1939 if ((drvp->drive_flags & DRIVE) == 0)
1940 continue;
1941 /* add timing values, setup DMA if needed */
1942 if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
1943 (drvp->drive_flags & DRIVE_DMA) == 0) ||
1944 sc->sc_dma_ok == 0) {
1945 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1946 goto pio;
1947 }
1948 if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1949 /* Abort DMA setup */
1950 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1951 goto pio;
1952 }
1953 if (drvp->drive_flags & DRIVE_UDMA) {
1954 /* use Ultra/DMA */
1955 drvp->drive_flags &= ~DRIVE_DMA;
1956 sis_tim |= sis_udma_tim[drvp->UDMA_mode] <<
1957 SIS_TIM_UDMA_TIME_OFF(drive);
1958 sis_tim |= SIS_TIM_UDMA_EN(drive);
1959 } else {
1960 /*
1961 * use Multiword DMA
1962 * Timings will be used for both PIO and DMA,
1963 * so adjust DMA mode if needed
1964 */
1965 if (drvp->PIO_mode > (drvp->DMA_mode + 2))
1966 drvp->PIO_mode = drvp->DMA_mode + 2;
1967 if (drvp->DMA_mode == 0)
1968 drvp->PIO_mode = 0;
1969 }
1970 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1971 pio: sis_tim |= sis_pio_act[drvp->PIO_mode] <<
1972 SIS_TIM_ACT_OFF(drive);
1973 sis_tim |= sis_pio_rec[drvp->PIO_mode] <<
1974 SIS_TIM_REC_OFF(drive);
1975 }
1976 WDCDEBUG_PRINT(("sis_setup_chip: new timings reg for "
1977 "channel %d 0x%x\n", channel, sis_tim), DEBUG_PROBE);
1978 pci_conf_write(pc, tag, SIS_TIM(channel), sis_tim);
1979 }
1980 pciide_print_modes(sc);
1981 pciide_pci_write(pc, tag, SIS_MISC,
1982 pciide_pci_read(pc, tag, SIS_MISC) |
1983 SIS_MISC_TIM_SEL | SIS_MISC_FIFO_SIZE);
1984 if (idedma_ctl != 0) {
1985 /* Add software bits in status register */
1986 bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
1987 IDEDMA_CTL, idedma_ctl);
1988 }
1989 }
1990
1991 void
1992 sis_channel_map(sc, pa, cp)
1993 struct pciide_softc *sc;
1994 struct pci_attach_args *pa;
1995 struct pciide_channel *cp;
1996 {
1997 bus_size_t cmdsize, ctlsize;
1998 struct channel_softc *wdc_cp = &cp->wdc_channel;
1999 u_int32_t sis_ctr0 = pciide_pci_read(pa->pa_pc, pa->pa_tag, SIS_CTRL0);
2000 int interface =
2001 PCI_INTERFACE(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
2002
2003 if ((wdc_cp->channel == 0 && (sis_ctr0 & SIS_CTRL0_CHAN0_EN) == 0) ||
2004 (wdc_cp->channel == 1 && (sis_ctr0 & SIS_CTRL0_CHAN1_EN) == 0)) {
2005 printf("%s: %s channel ignored (disabled)\n",
2006 sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
2007 return;
2008 }
2009
2010 pciide_mapchan(sc, pa, cp, interface, &cmdsize, &ctlsize);
2011 if (cp->hw_ok == 0)
2012 return;
2013 if (pciiide_chan_candisable(sc, pa, cp, cmdsize, ctlsize)) {
2014 if (wdc_cp->channel == 0)
2015 sis_ctr0 &= ~SIS_CTRL0_CHAN0_EN;
2016 else
2017 sis_ctr0 &= ~SIS_CTRL0_CHAN1_EN;
2018 pciide_pci_write(pa->pa_pc, pa->pa_tag, SIS_CTRL0, sis_ctr0);
2019 }
2020 pciide_map_compat_intr(sc, pa, cp, wdc_cp->channel, interface);
2021 }
2022