Home | History | Annotate | Line # | Download | only in pci
pdcide.c revision 1.14
      1 /*	$NetBSD: pdcide.c,v 1.14 2004/08/13 04:10:49 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
      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 Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <dev/pci/pcivar.h>
     36 #include <dev/pci/pcidevs.h>
     37 #include <dev/pci/pciidereg.h>
     38 #include <dev/pci/pciidevar.h>
     39 #include <dev/pci/pciide_pdc202xx_reg.h>
     40 
     41 static void pdc202xx_chip_map(struct pciide_softc *, struct pci_attach_args *);
     42 static void pdc202xx_setup_channel(struct wdc_channel *);
     43 static void pdc20268_setup_channel(struct wdc_channel *);
     44 static int  pdc202xx_pci_intr(void *);
     45 static int  pdc20265_pci_intr(void *);
     46 static void pdc20262_dma_start(void *, int, int);
     47 static int  pdc20262_dma_finish(void *, int, int, int);
     48 
     49 static int  pdcide_match(struct device *, struct cfdata *, void *);
     50 static void pdcide_attach(struct device *, struct device *, void *);
     51 
     52 CFATTACH_DECL(pdcide, sizeof(struct pciide_softc),
     53     pdcide_match, pdcide_attach, NULL, NULL);
     54 
     55 static const struct pciide_product_desc pciide_promise_products[] =  {
     56 	{ PCI_PRODUCT_PROMISE_PDC20246,
     57 	  0,
     58 	  "Promise Ultra33/ATA Bus Master IDE Accelerator",
     59 	  pdc202xx_chip_map,
     60 	},
     61 	{ PCI_PRODUCT_PROMISE_PDC20262,
     62 	  0,
     63 	  "Promise Ultra66/ATA Bus Master IDE Accelerator",
     64 	  pdc202xx_chip_map,
     65 	},
     66 	{ PCI_PRODUCT_PROMISE_PDC20267,
     67 	  0,
     68 	  "Promise Ultra100/ATA Bus Master IDE Accelerator",
     69 	  pdc202xx_chip_map,
     70 	},
     71 	{ PCI_PRODUCT_PROMISE_PDC20265,
     72 	  0,
     73 	  "Promise Ultra100/ATA Bus Master IDE Accelerator",
     74 	  pdc202xx_chip_map,
     75 	},
     76 	{ PCI_PRODUCT_PROMISE_PDC20268,
     77 	  0,
     78 	  "Promise Ultra100TX2/ATA Bus Master IDE Accelerator",
     79 	  pdc202xx_chip_map,
     80 	},
     81 	{ PCI_PRODUCT_PROMISE_PDC20270,
     82 	  0,
     83 	  "Promise Ultra100TX2v2/ATA Bus Master IDE Accelerator",
     84 	  pdc202xx_chip_map,
     85 	},
     86 	{ PCI_PRODUCT_PROMISE_PDC20269,
     87 	  0,
     88 	  "Promise Ultra133/ATA Bus Master IDE Accelerator",
     89 	  pdc202xx_chip_map,
     90 	},
     91 	{ PCI_PRODUCT_PROMISE_PDC20276,
     92 	  0,
     93 	  "Promise Ultra133TX2/ATA Bus Master IDE Accelerator",
     94 	  pdc202xx_chip_map,
     95 	},
     96 	{ PCI_PRODUCT_PROMISE_PDC20275,
     97 	  0,
     98 	  "Promise Ultra133/ATA Bus Master IDE Accelerator (MB)",
     99 	  pdc202xx_chip_map,
    100 	},
    101 	{ PCI_PRODUCT_PROMISE_PDC20271,
    102 	  0,
    103 	  "Promise Ultra133TX2v2/ATA Bus Master IDE Accelerator",
    104 	  pdc202xx_chip_map,
    105 	},
    106 	{ PCI_PRODUCT_PROMISE_PDC20277,
    107 	  0,
    108 	  "Promise Fasttrak133 Lite Bus Master IDE Accelerator",
    109 	  pdc202xx_chip_map,
    110 	},
    111 	{ 0,
    112 	  0,
    113 	  NULL,
    114 	  NULL
    115 	}
    116 };
    117 
    118 static int
    119 pdcide_match(struct device *parent, struct cfdata *match, void *aux)
    120 {
    121 	struct pci_attach_args *pa = aux;
    122 
    123 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_PROMISE) {
    124 		if (pciide_lookup_product(pa->pa_id, pciide_promise_products))
    125 			return (2);
    126 	}
    127 	return (0);
    128 }
    129 
    130 static void
    131 pdcide_attach(struct device *parent, struct device *self, void *aux)
    132 {
    133 	struct pci_attach_args *pa = aux;
    134 	struct pciide_softc *sc = (struct pciide_softc *)self;
    135 
    136 	pciide_common_attach(sc, pa,
    137 	    pciide_lookup_product(pa->pa_id, pciide_promise_products));
    138 
    139 }
    140 
    141 /* Macros to test product */
    142 #define PDC_IS_262(sc)							\
    143 	((sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20262 ||	\
    144 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20267 ||	\
    145 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20265 ||	\
    146 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20268 ||	\
    147 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20270 || \
    148 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20269 ||	\
    149 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20276 ||	\
    150 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20275 || \
    151 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20271 || \
    152 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20277)
    153 #define PDC_IS_265(sc)							\
    154 	((sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20267 ||	\
    155 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20265 ||	\
    156 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20268 ||	\
    157 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20270 || \
    158 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20269 ||	\
    159 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20276 ||	\
    160 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20275 || \
    161 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20271 || \
    162 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20277)
    163 #define PDC_IS_268(sc)							\
    164 	((sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20268 ||	\
    165 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20270 || \
    166 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20269 ||	\
    167 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20276 ||	\
    168 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20275 || \
    169 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20271 || \
    170 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20277)
    171 #define PDC_IS_276(sc)							\
    172 	((sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20269 ||	\
    173 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20276 ||	\
    174 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20275 || \
    175 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20271 || \
    176 	(sc)->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20277)
    177 
    178 static void
    179 pdc202xx_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    180 {
    181 	struct pciide_channel *cp;
    182 	int channel;
    183 	pcireg_t interface, st, mode;
    184 	bus_size_t cmdsize, ctlsize;
    185 
    186 	if (!PDC_IS_268(sc)) {
    187 		st = pci_conf_read(sc->sc_pc, sc->sc_tag, PDC2xx_STATE);
    188 		ATADEBUG_PRINT(("pdc202xx_setup_chip: controller state 0x%x\n",
    189 		    st), DEBUG_PROBE);
    190 		/* turn off  RAID mode */
    191 		if (st & PDC2xx_STATE_IDERAID) {
    192 			ATADEBUG_PRINT(("pdc202xx_setup_chip: turning off RAID mode\n"), DEBUG_PROBE);
    193 			st &= ~PDC2xx_STATE_IDERAID;
    194 			pci_conf_write(sc->sc_pc, sc->sc_tag, PDC2xx_STATE, st);
    195 		}
    196 	} else
    197 		st = PDC2xx_STATE_NATIVE | PDC262_STATE_EN(0) | PDC262_STATE_EN(1);
    198 
    199 	if (pciide_chipen(sc, pa) == 0)
    200 		return;
    201 
    202 	/*
    203 	 * can't rely on the PCI_CLASS_REG content if the chip was in raid
    204 	 * mode. We have to fake interface
    205 	 */
    206 	interface = PCIIDE_INTERFACE_SETTABLE(0) | PCIIDE_INTERFACE_SETTABLE(1);
    207 	if (st & PDC2xx_STATE_NATIVE)
    208 		interface |= PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    209 
    210 	aprint_normal("%s: bus-master DMA support present",
    211 	    sc->sc_wdcdev.sc_dev.dv_xname);
    212 	pciide_mapreg_dma(sc, pa);
    213 	aprint_normal("\n");
    214 	sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    215 	if (sc->sc_dma_ok) {
    216 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
    217 		sc->sc_wdcdev.irqack = pciide_irqack;
    218 	}
    219 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    220 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID)
    221 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_RAID;
    222 	sc->sc_wdcdev.PIO_cap = 4;
    223 	sc->sc_wdcdev.DMA_cap = 2;
    224 	if (PDC_IS_276(sc))
    225 		sc->sc_wdcdev.UDMA_cap = 6;
    226 	else if (PDC_IS_265(sc))
    227 		sc->sc_wdcdev.UDMA_cap = 5;
    228 	else if (PDC_IS_262(sc))
    229 		sc->sc_wdcdev.UDMA_cap = 4;
    230 	else
    231 		sc->sc_wdcdev.UDMA_cap = 2;
    232 	sc->sc_wdcdev.set_modes = PDC_IS_268(sc) ?
    233 			pdc20268_setup_channel : pdc202xx_setup_channel;
    234 	sc->sc_wdcdev.channels = sc->wdc_chanarray;
    235 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    236 
    237 	if (sc->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20262 ||
    238 	    sc->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20267 ||
    239 	    sc->sc_pp->ide_product == PCI_PRODUCT_PROMISE_PDC20265) {
    240 		sc->sc_wdcdev.dma_start = pdc20262_dma_start;
    241 		sc->sc_wdcdev.dma_finish = pdc20262_dma_finish;
    242 	}
    243 
    244 	if (!PDC_IS_268(sc)) {
    245 		/* setup failsafe defaults */
    246 		mode = 0;
    247 		mode = PDC2xx_TIM_SET_PA(mode, pdc2xx_pa[0]);
    248 		mode = PDC2xx_TIM_SET_PB(mode, pdc2xx_pb[0]);
    249 		mode = PDC2xx_TIM_SET_MB(mode, pdc2xx_dma_mb[0]);
    250 		mode = PDC2xx_TIM_SET_MC(mode, pdc2xx_dma_mc[0]);
    251 		for (channel = 0;
    252 		     channel < sc->sc_wdcdev.nchannels;
    253 		     channel++) {
    254 			ATADEBUG_PRINT(("pdc202xx_setup_chip: channel %d "
    255 			    "drive 0 initial timings  0x%x, now 0x%x\n",
    256 			    channel, pci_conf_read(sc->sc_pc, sc->sc_tag,
    257 			    PDC2xx_TIM(channel, 0)), mode | PDC2xx_TIM_IORDYp),
    258 			    DEBUG_PROBE);
    259 			pci_conf_write(sc->sc_pc, sc->sc_tag,
    260 			    PDC2xx_TIM(channel, 0), mode | PDC2xx_TIM_IORDYp);
    261 			ATADEBUG_PRINT(("pdc202xx_setup_chip: channel %d "
    262 			    "drive 1 initial timings  0x%x, now 0x%x\n",
    263 			    channel, pci_conf_read(sc->sc_pc, sc->sc_tag,
    264 			    PDC2xx_TIM(channel, 1)), mode), DEBUG_PROBE);
    265 			pci_conf_write(sc->sc_pc, sc->sc_tag,
    266 			    PDC2xx_TIM(channel, 1), mode);
    267 		}
    268 
    269 		mode = PDC2xx_SCR_DMA;
    270 		if (PDC_IS_265(sc)) {
    271 			mode = PDC2xx_SCR_SET_GEN(mode, PDC265_SCR_GEN_LAT);
    272 		} else if (PDC_IS_262(sc)) {
    273 			mode = PDC2xx_SCR_SET_GEN(mode, PDC262_SCR_GEN_LAT);
    274 		} else {
    275 			/* the BIOS set it up this way */
    276 			mode = PDC2xx_SCR_SET_GEN(mode, 0x1);
    277 		}
    278 		mode = PDC2xx_SCR_SET_I2C(mode, 0x3); /* ditto */
    279 		mode = PDC2xx_SCR_SET_POLL(mode, 0x1); /* ditto */
    280 		ATADEBUG_PRINT(("pdc202xx_setup_chip: initial SCR  0x%x, "
    281 		    "now 0x%x\n",
    282 		    bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    283 			PDC2xx_SCR),
    284 		    mode), DEBUG_PROBE);
    285 		bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    286 		    PDC2xx_SCR, mode);
    287 
    288 		/* controller initial state register is OK even without BIOS */
    289 		/* Set DMA mode to IDE DMA compatibility */
    290 		mode =
    291 		    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_PM);
    292 		ATADEBUG_PRINT(("pdc202xx_setup_chip: primary mode 0x%x", mode),
    293 		    DEBUG_PROBE);
    294 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_PM,
    295 		    mode | 0x1);
    296 		mode =
    297 		    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SM);
    298 		ATADEBUG_PRINT((", secondary mode 0x%x\n", mode ), DEBUG_PROBE);
    299 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SM,
    300 		    mode | 0x1);
    301 	}
    302 
    303 	for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
    304 		cp = &sc->pciide_channels[channel];
    305 		if (pciide_chansetup(sc, channel, interface) == 0)
    306 			continue;
    307 		if ((st & (PDC_IS_262(sc) ?
    308 		    PDC262_STATE_EN(channel):PDC246_STATE_EN(channel))) == 0) {
    309 			aprint_normal("%s: %s channel ignored (disabled)\n",
    310 			    sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
    311 			cp->wdc_channel.ch_flags |= WDCF_DISABLED;
    312 			continue;
    313 		}
    314 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
    315 		    PDC_IS_265(sc) ? pdc20265_pci_intr : pdc202xx_pci_intr);
    316 		/* clear interrupt, in case there is one pending */
    317 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    318 		    IDEDMA_CTL_INTR);
    319 	}
    320 	return;
    321 }
    322 
    323 static void
    324 pdc202xx_setup_channel(struct wdc_channel *chp)
    325 {
    326 	struct ata_drive_datas *drvp;
    327 	int drive;
    328 	pcireg_t mode, st;
    329 	u_int32_t idedma_ctl, scr, atapi;
    330 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    331 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.ch_wdc;
    332 	int channel = chp->ch_channel;
    333 
    334 	/* setup DMA if needed */
    335 	pciide_channel_dma_setup(cp);
    336 
    337 	idedma_ctl = 0;
    338 	ATADEBUG_PRINT(("pdc202xx_setup_channel %s: scr 0x%x\n",
    339 	    sc->sc_wdcdev.sc_dev.dv_xname,
    340 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, PDC262_U66)),
    341 	    DEBUG_PROBE);
    342 
    343 	/* Per channel settings */
    344 	if (PDC_IS_262(sc)) {
    345 		scr = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    346 		    PDC262_U66);
    347 		st = pci_conf_read(sc->sc_pc, sc->sc_tag, PDC2xx_STATE);
    348 		/* Trim UDMA mode */
    349 		if ((st & PDC262_STATE_80P(channel)) != 0 ||
    350 		    (chp->ch_drive[0].drive_flags & DRIVE_UDMA &&
    351 		    chp->ch_drive[0].UDMA_mode <= 2) ||
    352 		    (chp->ch_drive[1].drive_flags & DRIVE_UDMA &&
    353 		    chp->ch_drive[1].UDMA_mode <= 2)) {
    354 			if (chp->ch_drive[0].UDMA_mode > 2)
    355 				chp->ch_drive[0].UDMA_mode = 2;
    356 			if (chp->ch_drive[1].UDMA_mode > 2)
    357 				chp->ch_drive[1].UDMA_mode = 2;
    358 		}
    359 		/* Set U66 if needed */
    360 		if ((chp->ch_drive[0].drive_flags & DRIVE_UDMA &&
    361 		    chp->ch_drive[0].UDMA_mode > 2) ||
    362 		    (chp->ch_drive[1].drive_flags & DRIVE_UDMA &&
    363 		    chp->ch_drive[1].UDMA_mode > 2))
    364 			scr |= PDC262_U66_EN(channel);
    365 		else
    366 			scr &= ~PDC262_U66_EN(channel);
    367 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    368 		    PDC262_U66, scr);
    369 		ATADEBUG_PRINT(("pdc202xx_setup_channel %s:%d: ATAPI 0x%x\n",
    370 		    sc->sc_wdcdev.sc_dev.dv_xname, channel,
    371 		    bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    372 		    PDC262_ATAPI(channel))), DEBUG_PROBE);
    373 		if (chp->ch_drive[0].drive_flags & DRIVE_ATAPI ||
    374 			chp->ch_drive[1].drive_flags & DRIVE_ATAPI) {
    375 			if (((chp->ch_drive[0].drive_flags & DRIVE_UDMA) &&
    376 			    !(chp->ch_drive[1].drive_flags & DRIVE_UDMA) &&
    377 			    (chp->ch_drive[1].drive_flags & DRIVE_DMA)) ||
    378 			    ((chp->ch_drive[1].drive_flags & DRIVE_UDMA) &&
    379 			    !(chp->ch_drive[0].drive_flags & DRIVE_UDMA) &&
    380 			    (chp->ch_drive[0].drive_flags & DRIVE_DMA)))
    381 				atapi = 0;
    382 			else
    383 				atapi = PDC262_ATAPI_UDMA;
    384 			bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    385 			    PDC262_ATAPI(channel), atapi);
    386 		}
    387 	}
    388 	for (drive = 0; drive < 2; drive++) {
    389 		drvp = &chp->ch_drive[drive];
    390 		/* If no drive, skip */
    391 		if ((drvp->drive_flags & DRIVE) == 0)
    392 			continue;
    393 		mode = 0;
    394 		if (drvp->drive_flags & DRIVE_UDMA) {
    395 			/* use Ultra/DMA */
    396 			drvp->drive_flags &= ~DRIVE_DMA;
    397 			mode = PDC2xx_TIM_SET_MB(mode,
    398 			    pdc2xx_udma_mb[drvp->UDMA_mode]);
    399 			mode = PDC2xx_TIM_SET_MC(mode,
    400 			    pdc2xx_udma_mc[drvp->UDMA_mode]);
    401 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    402 		} else if (drvp->drive_flags & DRIVE_DMA) {
    403 			mode = PDC2xx_TIM_SET_MB(mode,
    404 			    pdc2xx_dma_mb[drvp->DMA_mode]);
    405 			mode = PDC2xx_TIM_SET_MC(mode,
    406 			    pdc2xx_dma_mc[drvp->DMA_mode]);
    407 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    408 		} else {
    409 			mode = PDC2xx_TIM_SET_MB(mode,
    410 			    pdc2xx_dma_mb[0]);
    411 			mode = PDC2xx_TIM_SET_MC(mode,
    412 			    pdc2xx_dma_mc[0]);
    413 		}
    414 		mode = PDC2xx_TIM_SET_PA(mode, pdc2xx_pa[drvp->PIO_mode]);
    415 		mode = PDC2xx_TIM_SET_PB(mode, pdc2xx_pb[drvp->PIO_mode]);
    416 		if (drvp->drive_flags & DRIVE_ATA)
    417 			mode |= PDC2xx_TIM_PRE;
    418 		mode |= PDC2xx_TIM_SYNC | PDC2xx_TIM_ERRDY;
    419 		if (drvp->PIO_mode >= 3) {
    420 			mode |= PDC2xx_TIM_IORDY;
    421 			if (drive == 0)
    422 				mode |= PDC2xx_TIM_IORDYp;
    423 		}
    424 		ATADEBUG_PRINT(("pdc202xx_setup_channel: %s:%d:%d "
    425 		    "timings 0x%x\n",
    426 		    sc->sc_wdcdev.sc_dev.dv_xname,
    427 		    chp->ch_channel, drive, mode), DEBUG_PROBE);
    428 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    429 		    PDC2xx_TIM(chp->ch_channel, drive), mode);
    430 	}
    431 	if (idedma_ctl != 0) {
    432 		/* Add software bits in status register */
    433 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL],
    434 		    0, idedma_ctl);
    435 	}
    436 }
    437 
    438 static void
    439 pdc20268_setup_channel(struct wdc_channel *chp)
    440 {
    441 	struct ata_drive_datas *drvp;
    442 	int drive;
    443 	u_int32_t idedma_ctl;
    444 	struct pciide_channel *cp = (struct pciide_channel*)chp;
    445 	struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.ch_wdc;
    446 	int u100;
    447 
    448 	/* setup DMA if needed */
    449 	pciide_channel_dma_setup(cp);
    450 
    451 	idedma_ctl = 0;
    452 
    453 	/* I don't know what this is for, FreeBSD does it ... */
    454 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    455 	    IDEDMA_CMD + 0x1 + IDEDMA_SCH_OFFSET * chp->ch_channel, 0x0b);
    456 
    457 	/*
    458 	 * cable type detect, from FreeBSD
    459 	 */
    460 	u100 = (bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    461 	    IDEDMA_CMD + 0x3 + IDEDMA_SCH_OFFSET * chp->ch_channel) & 0x04) ?
    462 	    0 : 1;
    463 
    464 	for (drive = 0; drive < 2; drive++) {
    465 		drvp = &chp->ch_drive[drive];
    466 		/* If no drive, skip */
    467 		if ((drvp->drive_flags & DRIVE) == 0)
    468 			continue;
    469 		if (drvp->drive_flags & DRIVE_UDMA) {
    470 			/* use Ultra/DMA */
    471 			drvp->drive_flags &= ~DRIVE_DMA;
    472 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    473 			if (drvp->UDMA_mode > 2 && u100 == 0)
    474 				drvp->UDMA_mode = 2;
    475 		} else if (drvp->drive_flags & DRIVE_DMA) {
    476 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    477 		}
    478 	}
    479 	/* nothing to do to setup modes, the controller snoop SET_FEATURE cmd */
    480 	if (idedma_ctl != 0) {
    481 		/* Add software bits in status register */
    482 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL],
    483 		    0, idedma_ctl);
    484 	}
    485 }
    486 
    487 static int
    488 pdc202xx_pci_intr(void *arg)
    489 {
    490 	struct pciide_softc *sc = arg;
    491 	struct pciide_channel *cp;
    492 	struct wdc_channel *wdc_cp;
    493 	int i, rv, crv;
    494 	u_int32_t scr;
    495 
    496 	rv = 0;
    497 	scr = bus_space_read_4(sc->sc_dma_iot, sc->sc_dma_ioh, PDC2xx_SCR);
    498 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    499 		cp = &sc->pciide_channels[i];
    500 		wdc_cp = &cp->wdc_channel;
    501 		/* If a compat channel skip. */
    502 		if (cp->compat)
    503 			continue;
    504 		if (scr & PDC2xx_SCR_INT(i)) {
    505 			crv = wdcintr(wdc_cp);
    506 			if (crv == 0) {
    507 				printf("%s:%d: bogus intr (reg 0x%x)\n",
    508 				    sc->sc_wdcdev.sc_dev.dv_xname, i, scr);
    509 				pciide_irqack(wdc_cp);
    510 			} else
    511 				rv = 1;
    512 		}
    513 	}
    514 	return rv;
    515 }
    516 
    517 static int
    518 pdc20265_pci_intr(void *arg)
    519 {
    520 	struct pciide_softc *sc = arg;
    521 	struct pciide_channel *cp;
    522 	struct wdc_channel *wdc_cp;
    523 	int i, rv, crv;
    524 	u_int32_t dmastat;
    525 
    526 	rv = 0;
    527 	for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
    528 		cp = &sc->pciide_channels[i];
    529 		wdc_cp = &cp->wdc_channel;
    530 		/* If a compat channel skip. */
    531 		if (cp->compat)
    532 			continue;
    533 #if 0
    534 		bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh, IDEDMA_CMD + 0x1 + IDEDMA_SCH_OFFSET * i, 0x0b);
    535 		if ((bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh, IDEDMA_CMD + 0x3 + IDEDMA_SCH_OFFSET * i) & 0x20) == 0)
    536 			continue;
    537 #endif
    538 		/*
    539 		 * The Ultra/100 seems to assert PDC2xx_SCR_INT * spuriously,
    540 		 * however it asserts INT in IDEDMA_CTL even for non-DMA ops.
    541 		 * So use it instead (requires 2 reg reads instead of 1,
    542 		 * but we can't do it another way).
    543 		 */
    544 		dmastat = bus_space_read_1(sc->sc_dma_iot,
    545 		    cp->dma_iohs[IDEDMA_CTL], 0);
    546 		if((dmastat & IDEDMA_CTL_INTR) == 0)
    547 			continue;
    548 		crv = wdcintr(wdc_cp);
    549 		if (crv == 0)
    550 			printf("%s:%d: bogus intr\n",
    551 			    sc->sc_wdcdev.sc_dev.dv_xname, i);
    552 		else
    553 			rv = 1;
    554 	}
    555 	return rv;
    556 }
    557 
    558 static void
    559 pdc20262_dma_start(void *v, int channel, int drive)
    560 {
    561 	struct pciide_softc *sc = v;
    562 	struct pciide_dma_maps *dma_maps =
    563 	    &sc->pciide_channels[channel].dma_maps[drive];
    564 	int atapi;
    565 
    566 	if (dma_maps->dma_flags & WDC_DMA_LBA48) {
    567 		atapi = (dma_maps->dma_flags & WDC_DMA_READ) ?
    568 		    PDC262_ATAPI_LBA48_READ : PDC262_ATAPI_LBA48_WRITE;
    569 		atapi |= dma_maps->dmamap_xfer->dm_mapsize >> 1;
    570 		bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    571 		    PDC262_ATAPI(channel), atapi);
    572 	}
    573 
    574 	pciide_dma_start(v, channel, drive);
    575 }
    576 
    577 static int
    578 pdc20262_dma_finish(void *v, int channel, int drive, int force)
    579 {
    580 	struct pciide_softc *sc = v;
    581 	struct pciide_dma_maps *dma_maps =
    582 	    &sc->pciide_channels[channel].dma_maps[drive];
    583 	struct wdc_channel *chp;
    584 	int atapi, error;
    585 
    586 	error = pciide_dma_finish(v, channel, drive, force);
    587 
    588 	if (dma_maps->dma_flags & WDC_DMA_LBA48) {
    589 		chp = sc->wdc_chanarray[channel];
    590 		atapi = 0;
    591 		if (chp->ch_drive[0].drive_flags & DRIVE_ATAPI ||
    592 		    chp->ch_drive[1].drive_flags & DRIVE_ATAPI) {
    593 			if ((!(chp->ch_drive[0].drive_flags & DRIVE_UDMA) ||
    594 			    (chp->ch_drive[1].drive_flags & DRIVE_UDMA) ||
    595 			    !(chp->ch_drive[1].drive_flags & DRIVE_DMA)) &&
    596 			    (!(chp->ch_drive[1].drive_flags & DRIVE_UDMA) ||
    597 			    (chp->ch_drive[0].drive_flags & DRIVE_UDMA) ||
    598 			    !(chp->ch_drive[0].drive_flags & DRIVE_DMA)))
    599 				atapi = PDC262_ATAPI_UDMA;
    600 		}
    601 		bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    602 		    PDC262_ATAPI(channel), atapi);
    603 	}
    604 
    605 	return error;
    606 }
    607