Home | History | Annotate | Line # | Download | only in pci
iteide.c revision 1.12
      1  1.12    dyoung /*	$NetBSD: iteide.c,v 1.12 2011/04/04 20:37:56 dyoung 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.12    dyoung __KERNEL_RCSID(0, "$NetBSD: iteide.c,v 1.12 2011/04/04 20:37:56 dyoung 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.9      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     91   1.1     grant 
     92   1.1     grant 	pciide_common_attach(sc, pa,
     93   1.1     grant 	    pciide_lookup_product(pa->pa_id, pciide_ite_products));
     94   1.1     grant }
     95   1.1     grant 
     96   1.1     grant static void
     97  1.12    dyoung ite_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
     98   1.1     grant {
     99   1.1     grant 	struct pciide_channel *cp;
    100   1.1     grant 	int channel;
    101   1.1     grant 	pcireg_t interface;
    102   1.1     grant 	pcireg_t cfg, modectl;
    103   1.1     grant 
    104   1.1     grant 	/* fake interface since IT8212 claims to be a RAID device */
    105   1.1     grant 	interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
    106   1.1     grant 	    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    107   1.1     grant 
    108   1.1     grant 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    109   1.1     grant 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    110   1.1     grant 	ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
    111   1.9      cube 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cfg & IT_CFG_MASK,
    112   1.1     grant 	    modectl & IT_MODE_MASK), DEBUG_PROBE);
    113   1.1     grant 
    114   1.1     grant 	if (pciide_chipen(sc, pa) == 0)
    115   1.1     grant 		return;
    116   1.1     grant 
    117   1.9      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    118   1.9      cube 	    "bus-master DMA support present");
    119   1.1     grant 	pciide_mapreg_dma(sc, pa);
    120   1.8        ad 	aprint_verbose("\n");
    121   1.1     grant 
    122   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    123   1.1     grant 
    124   1.1     grant 	if (sc->sc_dma_ok) {
    125   1.1     grant 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    126   1.1     grant 		sc->sc_wdcdev.irqack = pciide_irqack;
    127   1.1     grant 	}
    128   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    129   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    130   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    131   1.1     grant 
    132   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_set_modes = ite_setup_channel;
    133   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    134   1.1     grant 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    135   1.1     grant 
    136   1.1     grant 	wdc_allocate_regs(&sc->sc_wdcdev);
    137   1.1     grant 
    138   1.1     grant 	/* Disable RAID */
    139   1.1     grant 	modectl &= ~IT_MODE_RAID1;
    140   1.1     grant 	/* Disable CPU firmware mode */
    141   1.1     grant 	modectl &= ~IT_MODE_CPU;
    142   1.1     grant 
    143   1.1     grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
    144   1.1     grant 
    145   1.1     grant 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; channel++) {
    146   1.1     grant 		cp = &sc->pciide_channels[channel];
    147   1.1     grant 
    148   1.1     grant 		if (pciide_chansetup(sc, channel, interface) == 0)
    149   1.1     grant 			continue;
    150   1.1     grant 
    151  1.11  jakllsch 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    152   1.1     grant 	}
    153   1.1     grant 	/* Re-read configuration registers after channels setup */
    154   1.1     grant 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    155   1.1     grant 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    156   1.1     grant 	ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
    157   1.9      cube 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cfg & IT_CFG_MASK,
    158   1.1     grant 	    modectl & IT_MODE_MASK), DEBUG_PROBE);
    159   1.1     grant }
    160   1.1     grant 
    161   1.1     grant static void
    162   1.1     grant ite_setup_channel(struct ata_channel *chp)
    163   1.1     grant {
    164   1.1     grant 	struct ata_drive_datas *drvp;
    165   1.1     grant 	int drive, mode = 0;
    166   1.1     grant 	u_int32_t idedma_ctl;
    167   1.1     grant 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    168   1.1     grant 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    169   1.1     grant 	int channel = chp->ch_channel;
    170   1.1     grant 	pcireg_t cfg, modectl;
    171   1.1     grant 	pcireg_t tim;
    172   1.1     grant 
    173   1.1     grant 	cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
    174   1.1     grant 	modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
    175   1.1     grant 	tim = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_TIM(channel));
    176   1.1     grant 	ATADEBUG_PRINT(("%s:%d: tim=0x%x\n",
    177   1.9      cube 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
    178   1.1     grant 	    channel, tim), DEBUG_PROBE);
    179   1.1     grant 
    180   1.1     grant 	/* Setup DMA if needed */
    181   1.1     grant 	pciide_channel_dma_setup(cp);
    182   1.1     grant 
    183   1.1     grant 	/* Clear all bits for this channel */
    184   1.1     grant 	idedma_ctl = 0;
    185   1.1     grant 
    186   1.1     grant 	/* Per channel settings */
    187   1.1     grant 	for (drive = 0; drive < 2; drive++) {
    188   1.1     grant 		drvp = &chp->ch_drive[drive];
    189   1.1     grant 
    190   1.1     grant 		/* If no drive, skip */
    191   1.1     grant 		if ((drvp->drive_flags & DRIVE) == 0)
    192   1.1     grant 			continue;
    193   1.1     grant 
    194   1.1     grant 		if ((chp->ch_atac->atac_cap & ATAC_CAP_UDMA) != 0 &&
    195   1.1     grant 		    (drvp->drive_flags & DRIVE_UDMA) != 0) {
    196   1.1     grant 			/* Setup UltraDMA mode */
    197   1.1     grant 			drvp->drive_flags &= ~DRIVE_DMA;
    198   1.1     grant 			modectl &= ~IT_MODE_DMA(channel, drive);
    199   1.1     grant 
    200   1.1     grant #if 0
    201   1.1     grant 			/* Check cable, only works in CPU firmware mode */
    202   1.1     grant 			if (drvp->UDMA_mode > 2 &&
    203   1.1     grant 			    (cfg & IT_CFG_CABLE(channel, drive)) == 0) {
    204   1.1     grant 				ATADEBUG_PRINT(("(%s:%d:%d): "
    205   1.1     grant 				    "80-wire cable not detected\n",
    206  1.10    cegger 				    device_xname(&sc->sc_wdcdev.sc_atac.atac_dev),
    207   1.1     grant 				    channel, drive), DEBUG_PROBE);
    208   1.1     grant 				drvp->UDMA_mode = 2;
    209   1.1     grant 			}
    210   1.1     grant #endif
    211   1.1     grant 
    212   1.1     grant 			if (drvp->UDMA_mode >= 5)
    213   1.1     grant 				tim |= IT_TIM_UDMA5(drive);
    214   1.1     grant 			else
    215   1.1     grant 				tim &= ~IT_TIM_UDMA5(drive);
    216   1.1     grant 
    217   1.1     grant 			mode = drvp->PIO_mode;
    218   1.1     grant 		} else if ((chp->ch_atac->atac_cap & ATAC_CAP_DMA) != 0 &&
    219   1.1     grant 		    (drvp->drive_flags & DRIVE_DMA) != 0) {
    220   1.1     grant 			/* Setup multiword DMA mode */
    221   1.1     grant 			drvp->drive_flags &= ~DRIVE_UDMA;
    222   1.1     grant 			modectl |= IT_MODE_DMA(channel, drive);
    223   1.1     grant 
    224   1.1     grant 			/* mode = min(pio, dma + 2) */
    225   1.1     grant 			if (drvp->PIO_mode <= (drvp->DMA_mode + 2))
    226   1.1     grant 				mode = drvp->PIO_mode;
    227   1.1     grant 			else
    228   1.1     grant 				mode = drvp->DMA_mode + 2;
    229   1.1     grant 		} else {
    230   1.1     grant 			goto pio;
    231   1.1     grant 		}
    232   1.1     grant 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    233   1.1     grant 
    234   1.1     grant pio:
    235   1.1     grant 		/* Setup PIO mode */
    236   1.1     grant 		if (mode <= 2) {
    237   1.1     grant 			drvp->DMA_mode = 0;
    238   1.1     grant 			drvp->PIO_mode = 0;
    239   1.1     grant 			mode = 0;
    240   1.1     grant 		} else {
    241   1.1     grant 			drvp->PIO_mode = mode;
    242   1.1     grant 			drvp->DMA_mode = mode - 2;
    243   1.1     grant 		}
    244   1.1     grant 
    245   1.1     grant 		/* Enable IORDY if PIO mode >= 3 */
    246   1.1     grant 		if (drvp->PIO_mode >= 3)
    247   1.1     grant 			cfg |= IT_CFG_IORDY(channel);
    248   1.1     grant 	}
    249   1.1     grant 
    250   1.1     grant 	ATADEBUG_PRINT(("%s: tim=0x%x\n",
    251   1.9      cube 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), tim), DEBUG_PROBE);
    252   1.1     grant 
    253   1.1     grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_CFG, cfg);
    254   1.1     grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
    255   1.1     grant 	pci_conf_write(sc->sc_pc, sc->sc_tag, IT_TIM(channel), tim);
    256   1.1     grant 
    257   1.1     grant 	if (idedma_ctl != 0) {
    258   1.1     grant 		/* Add software bits in status register */
    259   1.1     grant 		bus_space_write_1(sc->sc_dma_iot,
    260   1.1     grant 		    cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
    261   1.1     grant 	}
    262   1.1     grant }
    263