Home | History | Annotate | Line # | Download | only in pci
iteide.c revision 1.17.2.2
      1 /*	$NetBSD: iteide.c,v 1.17.2.2 2012/11/20 03:02:19 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Grant Beattie.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. 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 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: iteide.c,v 1.17.2.2 2012/11/20 03:02:19 tls Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 
     39 #include <dev/pci/pcivar.h>
     40 #include <dev/pci/pcidevs.h>
     41 #include <dev/pci/pciidereg.h>
     42 #include <dev/pci/pciidevar.h>
     43 #include <dev/pci/pciide_ite_reg.h>
     44 
     45 static void ite_chip_map(struct pciide_softc*, const struct pci_attach_args*);
     46 static void ite_setup_channel(struct ata_channel*);
     47 
     48 static int  iteide_match(device_t, cfdata_t, void *);
     49 static void iteide_attach(device_t, device_t, void *);
     50 
     51 CFATTACH_DECL_NEW(iteide, sizeof(struct pciide_softc),
     52     iteide_match, iteide_attach, NULL, NULL);
     53 
     54 static const struct pciide_product_desc pciide_ite_products[] =  {
     55 	{ PCI_PRODUCT_ITE_IT8211,
     56 	  0,
     57 	  "Integrated Technology Express IDE controller",
     58 	  ite_chip_map,
     59 	},
     60 	{ PCI_PRODUCT_ITE_IT8212,
     61 	  0,
     62 	  "Integrated Technology Express IDE controller",
     63 	  ite_chip_map,
     64 	},
     65 	{ 0,
     66 	  0,
     67 	  NULL,
     68 	  NULL
     69 	}
     70 };
     71 
     72 static int
     73 iteide_match(device_t parent, cfdata_t match, void *aux)
     74 {
     75 	struct pci_attach_args *pa = aux;
     76 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ITE &&
     77 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE) {
     78 		if (pciide_lookup_product(pa->pa_id, pciide_ite_products))
     79 			return (2);
     80 	}
     81 	return (0);
     82 }
     83 
     84 static void
     85 iteide_attach(device_t parent, device_t self, void *aux)
     86 {
     87 	struct pci_attach_args *pa = aux;
     88 	struct pciide_softc *sc = device_private(self);
     89 
     90 	self->dv_maxphys = MIN(parent->dv_maxphys, MACHINE_MAXPHYS);
     91 
     92 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     93 
     94 	pciide_common_attach(sc, pa,
     95 	    pciide_lookup_product(pa->pa_id, pciide_ite_products));
     96 }
     97 
     98 static void
     99 ite_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    100 {
    101 	struct pciide_channel *cp;
    102 	int channel;
    103 	pcireg_t interface;
    104 	pcireg_t cfg, modectl;
    105 
    106 	/* fake interface since IT8212 claims to be a RAID device */
    107 	interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
    108 	    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    109 
    110 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    111 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    112 	ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
    113 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cfg & IT_CFG_MASK,
    114 	    modectl & IT_MODE_MASK), DEBUG_PROBE);
    115 
    116 	if (pciide_chipen(sc, pa) == 0)
    117 		return;
    118 
    119 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    120 	    "bus-master DMA support present");
    121 	pciide_mapreg_dma(sc, pa);
    122 	aprint_verbose("\n");
    123 
    124 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    125 
    126 	if (sc->sc_dma_ok) {
    127 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    128 		sc->sc_wdcdev.irqack = pciide_irqack;
    129 	}
    130 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    131 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    132 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    133 
    134 	sc->sc_wdcdev.sc_atac.atac_set_modes = ite_setup_channel;
    135 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    136 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    137 	sc->sc_wdcdev.wdc_maxdrives = 2;
    138 
    139 	wdc_allocate_regs(&sc->sc_wdcdev);
    140 
    141 	/* Disable RAID */
    142 	modectl &= ~IT_MODE_RAID1;
    143 	/* Disable CPU firmware mode */
    144 	modectl &= ~IT_MODE_CPU;
    145 
    146 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
    147 
    148 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; channel++) {
    149 		cp = &sc->pciide_channels[channel];
    150 
    151 		if (pciide_chansetup(sc, channel, interface) == 0)
    152 			continue;
    153 
    154 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    155 	}
    156 	/* Re-read configuration registers after channels setup */
    157 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    158 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    159 	ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
    160 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cfg & IT_CFG_MASK,
    161 	    modectl & IT_MODE_MASK), DEBUG_PROBE);
    162 }
    163 
    164 static void
    165 ite_setup_channel(struct ata_channel *chp)
    166 {
    167 	struct ata_drive_datas *drvp;
    168 	int drive, mode = 0;
    169 	u_int32_t idedma_ctl;
    170 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    171 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    172 	int channel = chp->ch_channel;
    173 	pcireg_t cfg, modectl;
    174 	pcireg_t tim;
    175 
    176 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    177 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    178 	tim = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_TIM(channel));
    179 	ATADEBUG_PRINT(("%s:%d: tim=0x%x\n",
    180 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
    181 	    channel, tim), DEBUG_PROBE);
    182 
    183 	/* Setup DMA if needed */
    184 	pciide_channel_dma_setup(cp);
    185 
    186 	/* Clear all bits for this channel */
    187 	idedma_ctl = 0;
    188 
    189 	/* Per channel settings */
    190 	for (drive = 0; drive < 2; drive++) {
    191 		drvp = &chp->ch_drive[drive];
    192 
    193 		/* If no drive, skip */
    194 		if (drvp->drive_type == ATA_DRIVET_NONE)
    195 			continue;
    196 
    197 		if ((chp->ch_atac->atac_cap & ATAC_CAP_UDMA) != 0 &&
    198 		    (drvp->drive_flags & ATA_DRIVE_UDMA) != 0) {
    199 			/* Setup UltraDMA mode */
    200 			drvp->drive_flags &= ~ATA_DRIVE_DMA;
    201 			modectl &= ~IT_MODE_DMA(channel, drive);
    202 
    203 #if 0
    204 			/* Check cable, only works in CPU firmware mode */
    205 			if (drvp->UDMA_mode > 2 &&
    206 			    (cfg & IT_CFG_CABLE(channel, drive)) == 0) {
    207 				ATADEBUG_PRINT(("(%s:%d:%d): "
    208 				    "80-wire cable not detected\n",
    209 				    device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
    210 				    channel, drive), DEBUG_PROBE);
    211 				drvp->UDMA_mode = 2;
    212 			}
    213 #endif
    214 
    215 			if (drvp->UDMA_mode >= 5)
    216 				tim |= IT_TIM_UDMA5(drive);
    217 			else
    218 				tim &= ~IT_TIM_UDMA5(drive);
    219 
    220 			mode = drvp->PIO_mode;
    221 		} else if ((chp->ch_atac->atac_cap & ATAC_CAP_DMA) != 0 &&
    222 		    (drvp->drive_flags & ATA_DRIVE_DMA) != 0) {
    223 			/* Setup multiword DMA mode */
    224 			drvp->drive_flags &= ~ATA_DRIVE_UDMA;
    225 			modectl |= IT_MODE_DMA(channel, drive);
    226 
    227 			/* mode = min(pio, dma + 2) */
    228 			if (drvp->PIO_mode <= (drvp->DMA_mode + 2))
    229 				mode = drvp->PIO_mode;
    230 			else
    231 				mode = drvp->DMA_mode + 2;
    232 		} else {
    233 			goto pio;
    234 		}
    235 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    236 
    237 pio:
    238 		/* Setup PIO mode */
    239 		if (mode <= 2) {
    240 			drvp->DMA_mode = 0;
    241 			drvp->PIO_mode = 0;
    242 			mode = 0;
    243 		} else {
    244 			drvp->PIO_mode = mode;
    245 			drvp->DMA_mode = mode - 2;
    246 		}
    247 
    248 		/* Enable IORDY if PIO mode >= 3 */
    249 		if (drvp->PIO_mode >= 3)
    250 			cfg |= IT_CFG_IORDY(channel);
    251 	}
    252 
    253 	ATADEBUG_PRINT(("%s: tim=0x%x\n",
    254 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), tim), DEBUG_PROBE);
    255 
    256 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_CFG, cfg);
    257 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
    258 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_TIM(channel), tim);
    259 
    260 	if (idedma_ctl != 0) {
    261 		/* Add software bits in status register */
    262 		bus_space_write_1(sc->sc_dma_iot,
    263 		    cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
    264 	}
    265 }
    266