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