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