Home | History | Annotate | Line # | Download | only in pci
iteide.c revision 1.1
      1  1.1  grant /*	$NetBSD: iteide.c,v 1.1 2004/12/01 22:27:45 grant Exp $	*/
      2  1.1  grant 
      3  1.1  grant /*
      4  1.1  grant  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  1.1  grant  *
      6  1.1  grant  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  grant  * by Grant Beattie.
      8  1.1  grant  *
      9  1.1  grant  * Redistribution and use in source and binary forms, with or without
     10  1.1  grant  * modification, are permitted provided that the following conditions
     11  1.1  grant  * are met:
     12  1.1  grant  * 1. Redistributions of source code must retain the above copyright
     13  1.1  grant  *    notice, this list of conditions and the following disclaimer.
     14  1.1  grant  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  grant  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  grant  *    documentation and/or other materials provided with the distribution.
     17  1.1  grant  * 3. The name of the author may not be used to endorse or promote products
     18  1.1  grant  *    derived from this software without specific prior written permission.
     19  1.1  grant  *
     20  1.1  grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1  grant  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1  grant  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1  grant  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1  grant  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1  grant  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1  grant  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1  grant  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1  grant  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1  grant  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  grant  *
     31  1.1  grant  */
     32  1.1  grant 
     33  1.1  grant #include <sys/param.h>
     34  1.1  grant #include <sys/systm.h>
     35  1.1  grant 
     36  1.1  grant #include <dev/pci/pcivar.h>
     37  1.1  grant #include <dev/pci/pcidevs.h>
     38  1.1  grant #include <dev/pci/pciidereg.h>
     39  1.1  grant #include <dev/pci/pciidevar.h>
     40  1.1  grant #include <dev/pci/pciide_ite_reg.h>
     41  1.1  grant 
     42  1.1  grant static void ite_chip_map(struct pciide_softc*, struct pci_attach_args*);
     43  1.1  grant static void ite_setup_channel(struct ata_channel*);
     44  1.1  grant 
     45  1.1  grant static int  iteide_match(struct device *, struct cfdata *, void *);
     46  1.1  grant static void iteide_attach(struct device *, struct device *, void *);
     47  1.1  grant 
     48  1.1  grant CFATTACH_DECL(iteide, sizeof(struct pciide_softc),
     49  1.1  grant     iteide_match, iteide_attach, NULL, NULL);
     50  1.1  grant 
     51  1.1  grant static const struct pciide_product_desc pciide_ite_products[] =  {
     52  1.1  grant 	{ PCI_PRODUCT_ITE_IT8212,
     53  1.1  grant 	  0,
     54  1.1  grant 	  "Integrated Technology Express IDE controller",
     55  1.1  grant 	  ite_chip_map,
     56  1.1  grant 	},
     57  1.1  grant 	{ 0,
     58  1.1  grant 	  0,
     59  1.1  grant 	  NULL,
     60  1.1  grant 	  NULL
     61  1.1  grant 	}
     62  1.1  grant };
     63  1.1  grant 
     64  1.1  grant static int
     65  1.1  grant iteide_match(struct device *parent, struct cfdata *match, void *aux)
     66  1.1  grant {
     67  1.1  grant 	struct pci_attach_args *pa = aux;
     68  1.1  grant 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ITE &&
     69  1.1  grant 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE) {
     70  1.1  grant 		if (pciide_lookup_product(pa->pa_id, pciide_ite_products))
     71  1.1  grant 			return (2);
     72  1.1  grant 	}
     73  1.1  grant 	return (0);
     74  1.1  grant }
     75  1.1  grant 
     76  1.1  grant static void
     77  1.1  grant iteide_attach(struct device *parent, struct device *self, void *aux)
     78  1.1  grant {
     79  1.1  grant 	struct pci_attach_args *pa = aux;
     80  1.1  grant 	struct pciide_softc *sc = (struct pciide_softc *)self;
     81  1.1  grant 
     82  1.1  grant 	pciide_common_attach(sc, pa,
     83  1.1  grant 	    pciide_lookup_product(pa->pa_id, pciide_ite_products));
     84  1.1  grant }
     85  1.1  grant 
     86  1.1  grant static void
     87  1.1  grant ite_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
     88  1.1  grant {
     89  1.1  grant 	struct pciide_channel *cp;
     90  1.1  grant 	int channel;
     91  1.1  grant 	pcireg_t interface;
     92  1.1  grant 	bus_size_t cmdsize, ctlsize;
     93  1.1  grant 	pcireg_t cfg, modectl;
     94  1.1  grant 
     95  1.1  grant 	/* fake interface since IT8212 claims to be a RAID device */
     96  1.1  grant 	interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
     97  1.1  grant 	    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
     98  1.1  grant 
     99  1.1  grant 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    100  1.1  grant 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    101  1.1  grant 	ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
    102  1.1  grant 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cfg & IT_CFG_MASK,
    103  1.1  grant 	    modectl & IT_MODE_MASK), DEBUG_PROBE);
    104  1.1  grant 
    105  1.1  grant 	if (pciide_chipen(sc, pa) == 0)
    106  1.1  grant 		return;
    107  1.1  grant 
    108  1.1  grant 	aprint_normal("%s: bus-master DMA support present",
    109  1.1  grant 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    110  1.1  grant 	pciide_mapreg_dma(sc, pa);
    111  1.1  grant 	aprint_normal("\n");
    112  1.1  grant 
    113  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    114  1.1  grant 
    115  1.1  grant 	if (sc->sc_dma_ok) {
    116  1.1  grant 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    117  1.1  grant 		sc->sc_wdcdev.irqack = pciide_irqack;
    118  1.1  grant 	}
    119  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    120  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    121  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    122  1.1  grant 
    123  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_set_modes = ite_setup_channel;
    124  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    125  1.1  grant 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    126  1.1  grant 
    127  1.1  grant 	wdc_allocate_regs(&sc->sc_wdcdev);
    128  1.1  grant 
    129  1.1  grant 	/* Disable RAID */
    130  1.1  grant 	modectl &= ~IT_MODE_RAID1;
    131  1.1  grant 	/* Disable CPU firmware mode */
    132  1.1  grant 	modectl &= ~IT_MODE_CPU;
    133  1.1  grant 
    134  1.1  grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
    135  1.1  grant 
    136  1.1  grant 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; channel++) {
    137  1.1  grant 		cp = &sc->pciide_channels[channel];
    138  1.1  grant 
    139  1.1  grant 		if (pciide_chansetup(sc, channel, interface) == 0)
    140  1.1  grant 			continue;
    141  1.1  grant 
    142  1.1  grant 		pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
    143  1.1  grant 		    pciide_pci_intr);
    144  1.1  grant 	}
    145  1.1  grant 	/* Re-read configuration registers after channels setup */
    146  1.1  grant 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    147  1.1  grant 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    148  1.1  grant 	ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
    149  1.1  grant 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cfg & IT_CFG_MASK,
    150  1.1  grant 	    modectl & IT_MODE_MASK), DEBUG_PROBE);
    151  1.1  grant }
    152  1.1  grant 
    153  1.1  grant static void
    154  1.1  grant ite_setup_channel(struct ata_channel *chp)
    155  1.1  grant {
    156  1.1  grant 	struct ata_drive_datas *drvp;
    157  1.1  grant 	int drive, mode = 0;
    158  1.1  grant 	u_int32_t idedma_ctl;
    159  1.1  grant 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    160  1.1  grant 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    161  1.1  grant 	int channel = chp->ch_channel;
    162  1.1  grant 	pcireg_t cfg, modectl;
    163  1.1  grant 	pcireg_t tim;
    164  1.1  grant 
    165  1.1  grant 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    166  1.1  grant 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    167  1.1  grant 	tim = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_TIM(channel));
    168  1.1  grant 	ATADEBUG_PRINT(("%s:%d: tim=0x%x\n",
    169  1.1  grant 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    170  1.1  grant 	    channel, tim), DEBUG_PROBE);
    171  1.1  grant 
    172  1.1  grant 	/* Setup DMA if needed */
    173  1.1  grant 	pciide_channel_dma_setup(cp);
    174  1.1  grant 
    175  1.1  grant 	/* Clear all bits for this channel */
    176  1.1  grant 	idedma_ctl = 0;
    177  1.1  grant 
    178  1.1  grant 	/* Per channel settings */
    179  1.1  grant 	for (drive = 0; drive < 2; drive++) {
    180  1.1  grant 		drvp = &chp->ch_drive[drive];
    181  1.1  grant 
    182  1.1  grant 		/* If no drive, skip */
    183  1.1  grant 		if ((drvp->drive_flags & DRIVE) == 0)
    184  1.1  grant 			continue;
    185  1.1  grant 
    186  1.1  grant 		if ((chp->ch_atac->atac_cap & ATAC_CAP_UDMA) != 0 &&
    187  1.1  grant 		    (drvp->drive_flags & DRIVE_UDMA) != 0) {
    188  1.1  grant 			/* Setup UltraDMA mode */
    189  1.1  grant 			drvp->drive_flags &= ~DRIVE_DMA;
    190  1.1  grant 			modectl &= ~IT_MODE_DMA(channel, drive);
    191  1.1  grant 
    192  1.1  grant #if 0
    193  1.1  grant 			/* Check cable, only works in CPU firmware mode */
    194  1.1  grant 			if (drvp->UDMA_mode > 2 &&
    195  1.1  grant 			    (cfg & IT_CFG_CABLE(channel, drive)) == 0) {
    196  1.1  grant 				ATADEBUG_PRINT(("(%s:%d:%d): "
    197  1.1  grant 				    "80-wire cable not detected\n",
    198  1.1  grant 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    199  1.1  grant 				    channel, drive), DEBUG_PROBE);
    200  1.1  grant 				drvp->UDMA_mode = 2;
    201  1.1  grant 			}
    202  1.1  grant #endif
    203  1.1  grant 
    204  1.1  grant 			if (drvp->UDMA_mode >= 5)
    205  1.1  grant 				tim |= IT_TIM_UDMA5(drive);
    206  1.1  grant 			else
    207  1.1  grant 				tim &= ~IT_TIM_UDMA5(drive);
    208  1.1  grant 
    209  1.1  grant 			mode = drvp->PIO_mode;
    210  1.1  grant 		} else if ((chp->ch_atac->atac_cap & ATAC_CAP_DMA) != 0 &&
    211  1.1  grant 		    (drvp->drive_flags & DRIVE_DMA) != 0) {
    212  1.1  grant 			/* Setup multiword DMA mode */
    213  1.1  grant 			drvp->drive_flags &= ~DRIVE_UDMA;
    214  1.1  grant 			modectl |= IT_MODE_DMA(channel, drive);
    215  1.1  grant 
    216  1.1  grant 			/* mode = min(pio, dma + 2) */
    217  1.1  grant 			if (drvp->PIO_mode <= (drvp->DMA_mode + 2))
    218  1.1  grant 				mode = drvp->PIO_mode;
    219  1.1  grant 			else
    220  1.1  grant 				mode = drvp->DMA_mode + 2;
    221  1.1  grant 		} else {
    222  1.1  grant 			goto pio;
    223  1.1  grant 		}
    224  1.1  grant 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    225  1.1  grant 
    226  1.1  grant pio:
    227  1.1  grant 		/* Setup PIO mode */
    228  1.1  grant 		if (mode <= 2) {
    229  1.1  grant 			drvp->DMA_mode = 0;
    230  1.1  grant 			drvp->PIO_mode = 0;
    231  1.1  grant 			mode = 0;
    232  1.1  grant 		} else {
    233  1.1  grant 			drvp->PIO_mode = mode;
    234  1.1  grant 			drvp->DMA_mode = mode - 2;
    235  1.1  grant 		}
    236  1.1  grant 
    237  1.1  grant 		/* Enable IORDY if PIO mode >= 3 */
    238  1.1  grant 		if (drvp->PIO_mode >= 3)
    239  1.1  grant 			cfg |= IT_CFG_IORDY(channel);
    240  1.1  grant 	}
    241  1.1  grant 
    242  1.1  grant 	ATADEBUG_PRINT(("%s: tim=0x%x\n",
    243  1.1  grant 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, tim), DEBUG_PROBE);
    244  1.1  grant 
    245  1.1  grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_CFG, cfg);
    246  1.1  grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
    247  1.1  grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_TIM(channel), tim);
    248  1.1  grant 
    249  1.1  grant 	if (idedma_ctl != 0) {
    250  1.1  grant 		/* Add software bits in status register */
    251  1.1  grant 		bus_space_write_1(sc->sc_dma_iot,
    252  1.1  grant 		    cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
    253  1.1  grant 	}
    254  1.1  grant }
    255