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