Home | History | Annotate | Line # | Download | only in pci
geodeide.c revision 1.8.2.1
      1 /*	$NetBSD: geodeide.c,v 1.8.2.1 2005/07/06 22:02:48 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. 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 /*
     34  * Driver for the IDE part of the AMD Geode CS5530A companion chip
     35  * and AMD Geode SC1100.
     36  * Docs available from AMD's web site
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: geodeide.c,v 1.8.2.1 2005/07/06 22:02:48 tron Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 
     45 #include <uvm/uvm_extern.h>
     46 
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pcidevs.h>
     49 #include <dev/pci/pciidereg.h>
     50 #include <dev/pci/pciidevar.h>
     51 
     52 #include <dev/pci/pciide_geode_reg.h>
     53 
     54 static void geodeide_chip_map(struct pciide_softc *,
     55 				 struct pci_attach_args *);
     56 static void geodeide_setup_channel(struct ata_channel *);
     57 static int geodeide_dma_init(void *, int, int, void *, size_t, int);
     58 
     59 static int  geodeide_match(struct device *, struct cfdata *, void *);
     60 static void geodeide_attach(struct device *, struct device *, void *);
     61 
     62 CFATTACH_DECL(geodeide, sizeof(struct pciide_softc),
     63     geodeide_match, geodeide_attach, NULL, NULL);
     64 
     65 static const struct pciide_product_desc pciide_geode_products[] = {
     66 	{ PCI_PRODUCT_CYRIX_CX5530_IDE,
     67 	  0,
     68 	  "AMD Geode CX5530 IDE controller",
     69 	  geodeide_chip_map,
     70 	},
     71 	{ PCI_PRODUCT_NS_SC1100_IDE,
     72 	  0,
     73 	  "AMD Geode SC1100 IDE controller",
     74 	  geodeide_chip_map,
     75 	},
     76 	{ 0,
     77 	  0,
     78 	  NULL,
     79 	  NULL,
     80 	},
     81 };
     82 
     83 static int
     84 geodeide_match(struct device *parent, struct cfdata *match, void *aux)
     85 {
     86 	struct pci_attach_args *pa = aux;
     87 
     88 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CYRIX ||
     89 	     PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS) &&
     90 	     PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     91 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE &&
     92 	     pciide_lookup_product(pa->pa_id, pciide_geode_products))
     93 		return(2);
     94 	return (0);
     95 }
     96 
     97 static void
     98 geodeide_attach(struct device *parent, struct device *self, void *aux)
     99 {
    100 	struct pci_attach_args *pa = aux;
    101 	struct pciide_softc *sc = (void *)self;
    102 
    103 	pciide_common_attach(sc, pa,
    104 	    pciide_lookup_product(pa->pa_id, pciide_geode_products));
    105 }
    106 
    107 static void
    108 geodeide_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    109 {
    110 	struct pciide_channel *cp;
    111 	int channel;
    112 	bus_size_t cmdsize, ctlsize;
    113 
    114 	if (pciide_chipen(sc, pa) == 0)
    115 		return;
    116 
    117 	aprint_normal("%s: bus-master DMA support present",
    118 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    119 	pciide_mapreg_dma(sc, pa);
    120 	aprint_normal("\n");
    121 	if (sc->sc_dma_ok) {
    122 		sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DMA | ATAC_CAP_UDMA;
    123 		sc->sc_wdcdev.irqack = pciide_irqack;
    124 		/*
    125 		 * XXXJRT What chip revisions actually need the DMA
    126 		 * alignment work-around?
    127 		 */
    128 		sc->sc_wdcdev.dma_init = geodeide_dma_init;
    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 = 2;
    133 	sc->sc_wdcdev.sc_atac.atac_set_modes = geodeide_setup_channel;
    134 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    135 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    136 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    137 
    138 	/*
    139 	 * Soekris Engineering Issue #0003:
    140 	 * 	"The SC1100 built in busmaster IDE controller is pretty
    141 	 *	 standard, but have two bugs: data transfers need to be
    142 	 *	 dword aligned and it cannot do an exact 64Kbyte data
    143 	 *	 transfer."
    144 	 */
    145 	if (sc->sc_pp->ide_product == PCI_PRODUCT_NS_SC1100_IDE) {
    146 		if (sc->sc_dma_boundary == 0x10000)
    147 			sc->sc_dma_boundary -= PAGE_SIZE;
    148 
    149 		if (sc->sc_dma_maxsegsz == 0x10000)
    150 			sc->sc_dma_maxsegsz -= PAGE_SIZE;
    151 	}
    152 
    153 	wdc_allocate_regs(&sc->sc_wdcdev);
    154 
    155 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    156 	     channel++) {
    157 		cp = &sc->pciide_channels[channel];
    158 		/* controller is compat-only */
    159 		if (pciide_chansetup(sc, channel, 0) == 0)
    160 			continue;
    161 		pciide_mapchan(pa, cp, 0, &cmdsize, &ctlsize, pciide_pci_intr);
    162 	}
    163 }
    164 
    165 static void
    166 geodeide_setup_channel(struct ata_channel *chp)
    167 {
    168 	struct ata_drive_datas *drvp;
    169 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    170 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    171 	int channel = chp->ch_channel;
    172 	int drive, s;
    173 	u_int32_t dma_timing;
    174 	u_int8_t idedma_ctl;
    175 	const int32_t *geode_pio;
    176 	const int32_t *geode_dma;
    177 	const int32_t *geode_udma;
    178 	bus_size_t dmaoff, piooff;
    179 
    180 	switch (sc->sc_pp->ide_product) {
    181 	case PCI_PRODUCT_CYRIX_CX5530_IDE:
    182 		geode_pio = geode_cs5530_pio;
    183 		geode_dma = geode_cs5530_dma;
    184 		geode_udma = geode_cs5530_udma;
    185 		break;
    186 
    187 	case PCI_PRODUCT_NS_SC1100_IDE:
    188 	default: /* XXX gcc */
    189 		geode_pio = geode_sc1100_pio;
    190 		geode_dma = geode_sc1100_dma;
    191 		geode_udma = geode_sc1100_udma;
    192 		break;
    193 	}
    194 
    195 	/* setup DMA if needed */
    196 	pciide_channel_dma_setup(cp);
    197 
    198 	idedma_ctl = 0;
    199 
    200 	/* Per drive settings */
    201 	for (drive = 0; drive < 2; drive++) {
    202 		drvp = &chp->ch_drive[drive];
    203 		/* If no drive, skip */
    204 		if ((drvp->drive_flags & DRIVE) == 0)
    205 			continue;
    206 
    207 		switch (sc->sc_pp->ide_product) {
    208 		case PCI_PRODUCT_CYRIX_CX5530_IDE:
    209 			dmaoff = CS5530_DMA_REG(channel, drive);
    210 			piooff = CS5530_PIO_REG(channel, drive);
    211 			dma_timing = CS5530_DMA_REG_PIO_FORMAT;
    212 			break;
    213 
    214 		case PCI_PRODUCT_NS_SC1100_IDE:
    215 		default: /* XXX gcc */
    216 			dmaoff = SC1100_DMA_REG(channel, drive);
    217 			piooff = SC1100_PIO_REG(channel, drive);
    218 			dma_timing = 0;
    219 			break;
    220 		}
    221 
    222 		/* add timing values, setup DMA if needed */
    223 		if (drvp->drive_flags & DRIVE_UDMA) {
    224 			/* Use Ultra-DMA */
    225 			dma_timing |= geode_udma[drvp->UDMA_mode];
    226 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    227 		} else if (drvp->drive_flags & DRIVE_DMA) {
    228 			/* use Multiword DMA */
    229 			dma_timing |= geode_dma[drvp->DMA_mode];
    230 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    231 		} else {
    232 			/* PIO only */
    233 			s = splbio();
    234 			drvp->drive_flags &= ~(DRIVE_UDMA | DRIVE_DMA);
    235 			splx(s);
    236 		}
    237 
    238 		switch (sc->sc_pp->ide_product) {
    239 		case PCI_PRODUCT_CYRIX_CX5530_IDE:
    240 			bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    241 			    dmaoff, dma_timing);
    242 			bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
    243 			    piooff, geode_pio[drvp->PIO_mode]);
    244 			break;
    245 
    246 		case PCI_PRODUCT_NS_SC1100_IDE:
    247 			pci_conf_write(sc->sc_pc, sc->sc_tag, dmaoff,
    248 			    dma_timing);
    249 			pci_conf_write(sc->sc_pc, sc->sc_tag, piooff,
    250 			    geode_pio[drvp->PIO_mode]);
    251 			break;
    252 		}
    253 	}
    254 
    255 	if (idedma_ctl != 0) {
    256 		/* Add software bits in status register */
    257 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    258 		    idedma_ctl);
    259 	}
    260 }
    261 
    262 static int
    263 geodeide_dma_init(void *v, int channel, int drive, void *databuf,
    264     size_t datalen, int flags)
    265 {
    266 
    267 	/*
    268 	 * If the buffer is not properly aligned, we can't allow DMA
    269 	 * and need to fall back to PIO.
    270 	 */
    271 	if (((uintptr_t)databuf) & 0xf)
    272 		return (EINVAL);
    273 
    274 	return (pciide_dma_init(v, channel, drive, databuf, datalen, flags));
    275 }
    276