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