Home | History | Annotate | Line # | Download | only in pci
pdcsata.c revision 1.1
      1 /*	$NetBSD: pdcsata.c,v 1.1 2004/11/24 20:49:19 bouyer 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 #include <sys/types.h>
     33 #include <sys/malloc.h>
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 
     37 #include <dev/pci/pcivar.h>
     38 #include <dev/pci/pcidevs.h>
     39 #include <dev/pci/pciidereg.h>
     40 #include <dev/pci/pciidevar.h>
     41 
     42 #define PDC203xx_NCHANNELS 4
     43 
     44 #define PDC203xx_BAR_IDEREGS 0x1c /* BAR where the IDE registers are mapped */
     45 
     46 static void pdcsata_chip_map(struct pciide_softc *, struct pci_attach_args *);
     47 static void pdc203xx_setup_channel(struct ata_channel *);
     48 static int  pdc203xx_pci_intr(void *);
     49 static void pdc203xx_irqack(struct ata_channel *);
     50 static int  pdc203xx_dma_init(void *, int, int, void *, size_t, int);
     51 static void pdc203xx_dma_start(void *,int ,int);
     52 static int  pdc203xx_dma_finish(void *, int, int, int);
     53 
     54 static int  pdcsata_match(struct device *, struct cfdata *, void *);
     55 static void pdcsata_attach(struct device *, struct device *, void *);
     56 
     57 CFATTACH_DECL(pdcsata, sizeof(struct pciide_softc),
     58     pdcsata_match, pdcsata_attach, NULL, NULL);
     59 
     60 static const struct pciide_product_desc pciide_pdcsata_products[] =  {
     61 	{ PCI_PRODUCT_PROMISE_PDC20318,
     62 	  0,
     63 	  "Promise PDC20318 SATA150 controller",
     64 	  pdcsata_chip_map,
     65 	},
     66 	{ PCI_PRODUCT_PROMISE_PDC20319,
     67 	  0,
     68 	  "Promise PDC20319 SATA150 controller",
     69 	  pdcsata_chip_map,
     70 	},
     71 	{ PCI_PRODUCT_PROMISE_PDC20371,
     72 	  0,
     73 	  "Promise PDC20371 SATA150 controller",
     74 	  pdcsata_chip_map,
     75 	},
     76 	{ PCI_PRODUCT_PROMISE_PDC20375,
     77 	  0,
     78 	  "Promise PDC20375 SATA150 controller",
     79 	  pdcsata_chip_map,
     80 	},
     81 	{ PCI_PRODUCT_PROMISE_PDC20376,
     82 	  0,
     83 	  "Promise PDC20376 SATA150 controller",
     84 	  pdcsata_chip_map,
     85 	},
     86 	{ PCI_PRODUCT_PROMISE_PDC20377,
     87 	  0,
     88 	  "Promise PDC20377 SATA150 controller",
     89 	  pdcsata_chip_map,
     90 	},
     91 	{ PCI_PRODUCT_PROMISE_PDC20378,
     92 	  0,
     93 	  "Promise PDC20378 SATA150 controller",
     94 	  pdcsata_chip_map,
     95 	},
     96 	{ PCI_PRODUCT_PROMISE_PDC20379,
     97 	  0,
     98 	  "Promise PDC20379 SATA150 controller",
     99 	  pdcsata_chip_map,
    100 	},
    101 	{ 0,
    102 	  0,
    103 	  NULL,
    104 	  NULL
    105 	}
    106 };
    107 
    108 static int
    109 pdcsata_match(struct device *parent, struct cfdata *match, void *aux)
    110 {
    111 	struct pci_attach_args *pa = aux;
    112 
    113 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_PROMISE) {
    114 		if (pciide_lookup_product(pa->pa_id, pciide_pdcsata_products))
    115 			return (2);
    116 	}
    117 	return (0);
    118 }
    119 
    120 static void
    121 pdcsata_attach(struct device *parent, struct device *self, void *aux)
    122 {
    123 	struct pci_attach_args *pa = aux;
    124 	struct pciide_softc *sc = (struct pciide_softc *)self;
    125 
    126 	pciide_common_attach(sc, pa,
    127 	    pciide_lookup_product(pa->pa_id, pciide_pdcsata_products));
    128 }
    129 
    130 static void
    131 pdcsata_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    132 {
    133 	struct pciide_channel *cp;
    134 	struct ata_channel *wdc_cp;
    135 	struct wdc_regs *wdr;
    136 	int channel, i;
    137 	bus_size_t dmasize;
    138 	pci_intr_handle_t intrhandle;
    139 	const char *intrstr;
    140 
    141 	/*
    142 	 * Promise SATA controllers have 3 or 4 channels,
    143 	 * the usual IDE registers are mapped in I/O space, with offsets.
    144 	 */
    145 	if (pci_intr_map(pa, &intrhandle) != 0) {
    146 		aprint_error("%s: couldn't map interrupt\n",
    147 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    148 		return;
    149 	}
    150 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    151 	sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    152 	    intrhandle, IPL_BIO, pdc203xx_pci_intr, sc);
    153 	if (sc->sc_pci_ih == NULL) {
    154 		aprint_error("%s: couldn't establish native-PCI interrupt",
    155 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    156 		if (intrstr != NULL)
    157 		    aprint_normal(" at %s", intrstr);
    158 		aprint_normal("\n");
    159 		return;
    160 	}
    161 	aprint_normal("%s: interrupting at %s\n",
    162 		sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    163 		intrstr ? intrstr : "unknown interrupt");
    164 
    165 	sc->sc_dma_ok = (pci_mapreg_map(pa, PCIIDE_REG_BUS_MASTER_DMA,
    166 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_dma_iot,
    167 	    &sc->sc_dma_ioh, NULL, &dmasize) == 0);
    168 	if (!sc->sc_dma_ok) {
    169 		aprint_error("%s: couldn't map bus-master DMA registers\n",
    170 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    171 		pci_intr_disestablish(pa->pa_pc, sc->sc_pci_ih);
    172 		return;
    173 	}
    174 
    175 	sc->sc_dmat = pa->pa_dmat;
    176 
    177 	if (pci_mapreg_map(pa, PDC203xx_BAR_IDEREGS,
    178 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_ba5_st,
    179 	    &sc->sc_ba5_sh, NULL, NULL) != 0) {
    180 		aprint_error("%s: couldn't map IDE registers\n",
    181 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    182 		bus_space_unmap(sc->sc_dma_iot, sc->sc_dma_ioh, dmasize);
    183 		pci_intr_disestablish(pa->pa_pc, sc->sc_pci_ih);
    184 		return;
    185 	}
    186 
    187 	aprint_normal("%s: bus-master DMA support present\n",
    188 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    189 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
    190 	if (sc->sc_dma_ok) {
    191 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    192 	}
    193 	sc->sc_wdcdev.irqack = pdc203xx_irqack;
    194 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    195 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    196 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    197 	sc->sc_wdcdev.sc_atac.atac_set_modes = pdc203xx_setup_channel;
    198 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    199 	bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x06c, 0x00ff0033);
    200 	sc->sc_wdcdev.sc_atac.atac_nchannels =
    201 	    (bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x48) & 0x02) ?
    202 	    PDC203xx_NCHANNELS : 3;
    203 	wdc_allocate_regs(&sc->sc_wdcdev);
    204 
    205 	sc->sc_wdcdev.dma_arg = sc;
    206 	sc->sc_wdcdev.dma_init = pdc203xx_dma_init;
    207 	sc->sc_wdcdev.dma_start = pdc203xx_dma_start;
    208 	sc->sc_wdcdev.dma_finish = pdc203xx_dma_finish;
    209 
    210 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    211 	     channel++) {
    212 		cp = &sc->pciide_channels[channel];
    213 		sc->wdc_chanarray[channel] = &cp->ata_channel;
    214 
    215 		cp->ih = sc->sc_pci_ih;
    216 		cp->name = NULL;
    217 		cp->ata_channel.ch_channel = channel;
    218 		cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    219 		cp->ata_channel.ch_queue =
    220 		    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    221 		if (cp->ata_channel.ch_queue == NULL) {
    222 			aprint_error("%s channel %d: "
    223 			    "can't allocate memory for command queue\n",
    224 			sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel);
    225 			goto next_channel;
    226 		}
    227 		wdc_cp = &cp->ata_channel;
    228 		wdr = CHAN_TO_WDC_REGS(wdc_cp);
    229 
    230 		wdr->ctl_iot = sc->sc_ba5_st;
    231 		wdr->cmd_iot = sc->sc_ba5_st;
    232 
    233 		if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    234 		    0x0238 + (channel << 7), 1, &wdr->ctl_ioh) != 0) {
    235 			aprint_error("%s: couldn't map channel %d ctl regs\n",
    236 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    237 			    channel);
    238 			goto next_channel;
    239 		}
    240 		for (i = 0; i < WDC_NREG; i++) {
    241 			if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    242 			    0x0200 + (i << 2) + (channel << 7), i == 0 ? 4 : 1,
    243 			    &wdr->cmd_iohs[i]) != 0) {
    244 				aprint_error("%s: couldn't map channel %d cmd "
    245 				    "regs\n",
    246 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    247 				    channel);
    248 				goto next_channel;
    249 			}
    250 		}
    251 		wdc_init_shadow_regs(wdc_cp);
    252 
    253 		/*
    254 		 * subregion de busmaster registers. They're spread all over
    255 		 * the controller's register space :(. They are also 4 bytes
    256 		 * sized, with some specific extentions in the extra bits.
    257 		 * It also seems that the IDEDMA_CTL register isn't available.
    258 		 */
    259 		if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    260 		    0x260 + (channel << 7), 1,
    261 		    &cp->dma_iohs[IDEDMA_CMD]) != 0) {
    262 			aprint_normal("%s channel %d: can't subregion DMA "
    263 			    "registers\n",
    264 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel);
    265 			goto next_channel;
    266 		}
    267 		if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    268 		    0x244 + (channel << 7), 4,
    269 		    &cp->dma_iohs[IDEDMA_TBL]) != 0) {
    270 			aprint_normal("%s channel %d: can't subregion DMA "
    271 			    "registers\n",
    272 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel);
    273 			goto next_channel;
    274 		}
    275 
    276 		wdcattach(wdc_cp);
    277 		bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    278 		    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    279 			0) & ~0x00003f9f) | (channel + 1));
    280 		bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh,
    281 		    (channel + 1) << 2, 0x00000001);
    282 next_channel:
    283 	continue;
    284 	}
    285 	return;
    286 }
    287 
    288 static void
    289 pdc203xx_setup_channel(struct ata_channel *chp)
    290 {
    291 	struct ata_drive_datas *drvp;
    292 	int drive, s;
    293 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    294 
    295 	pciide_channel_dma_setup(cp);
    296 
    297 	for (drive = 0; drive < 2; drive++) {
    298 		drvp = &chp->ch_drive[drive];
    299 		if ((drvp->drive_flags & DRIVE) == 0)
    300 			continue;
    301 		if (drvp->drive_flags & DRIVE_UDMA) {
    302 			s = splbio();
    303 			drvp->drive_flags &= ~DRIVE_DMA;
    304 			splx(s);
    305 		}
    306 	}
    307 }
    308 
    309 static int
    310 pdc203xx_pci_intr(void *arg)
    311 {
    312 	struct pciide_softc *sc = arg;
    313 	struct pciide_channel *cp;
    314 	struct ata_channel *wdc_cp;
    315 	int i, rv, crv;
    316 	u_int32_t scr;
    317 
    318 	rv = 0;
    319 	scr = bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, 0x00040);
    320 
    321 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    322 		cp = &sc->pciide_channels[i];
    323 		wdc_cp = &cp->ata_channel;
    324 		if (scr & (1 << (i + 1))) {
    325 			crv = wdcintr(wdc_cp);
    326 			if (crv == 0) {
    327 				printf("%s:%d: bogus intr (reg 0x%x)\n",
    328 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    329 				    i, scr);
    330 			} else
    331 				rv = 1;
    332 		}
    333 	}
    334 	return rv;
    335 }
    336 
    337 static void
    338 pdc203xx_irqack(struct ata_channel *chp)
    339 {
    340 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    341 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    342 
    343 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    344 	    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    345 		0) & ~0x00003f9f) | (cp->ata_channel.ch_channel + 1));
    346 	bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh,
    347 	    (cp->ata_channel.ch_channel + 1) << 2, 0x00000001);
    348 }
    349 
    350 static int
    351 pdc203xx_dma_init(void *v, int channel, int drive, void *databuf,
    352     size_t datalen, int flags)
    353 {
    354 	struct pciide_softc *sc = v;
    355 
    356 	return pciide_dma_dmamap_setup(sc, channel, drive,
    357 	    databuf, datalen, flags);
    358 }
    359 
    360 static void
    361 pdc203xx_dma_start(void *v, int channel, int drive)
    362 {
    363 	struct pciide_softc *sc = v;
    364 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    365 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    366 
    367 	/* Write table addr */
    368 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0,
    369 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    370 	/* start DMA engine */
    371 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    372 	    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    373 	    0) & ~0xc0) | ((dma_maps->dma_flags & WDC_DMA_READ) ? 0x80 : 0xc0));
    374 }
    375 
    376 static int
    377 pdc203xx_dma_finish(void *v, int channel, int drive, int force)
    378 {
    379 	struct pciide_softc *sc = v;
    380 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    381 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    382 
    383 	/* stop DMA channel */
    384 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    385 	    (bus_space_read_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD],
    386 	    0) & ~0x80));
    387 
    388 	/* Unload the map of the data buffer */
    389 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    390 	    dma_maps->dmamap_xfer->dm_mapsize,
    391 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    392 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    393 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    394 
    395 	return 0;
    396 }
    397