Home | History | Annotate | Line # | Download | only in pci
nside.c revision 1.7.2.1
      1 /*	$NetBSD: nside.c,v 1.7.2.1 2012/10/09 13:36:05 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000, 2001 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  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: nside.c,v 1.7.2.1 2012/10/09 13:36:05 bouyer Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 
     33 #include <dev/pci/pcivar.h>
     34 #include <dev/pci/pcidevs.h>
     35 #include <dev/pci/pciidereg.h>
     36 #include <dev/pci/pciidevar.h>
     37 #include <dev/pci/pciide_natsemi_reg.h>
     38 
     39 static void natsemi_chip_map(struct pciide_softc *,
     40     const struct pci_attach_args *);
     41 static void natsemi_setup_channel(struct ata_channel *);
     42 static int  natsemi_pci_intr(void *);
     43 static void natsemi_irqack(struct ata_channel *);
     44 
     45 static int  nside_match(device_t, cfdata_t, void *);
     46 static void nside_attach(device_t, device_t, void *);
     47 
     48 struct nside_softc {
     49 	struct pciide_softc pciide_sc;
     50 	struct pci_attach_args pcib_pa;
     51 };
     52 
     53 CFATTACH_DECL_NEW(nside, sizeof(struct nside_softc),
     54     nside_match, nside_attach, NULL, NULL);
     55 
     56 static const struct pciide_product_desc pciide_natsemi_products[] =  {
     57 	{ PCI_PRODUCT_NS_PC87415,       /* National Semi PC87415 IDE */
     58 	  0,
     59 	  "National Semiconductor PC87415 IDE Controller",
     60           natsemi_chip_map,
     61 	},
     62 	{ 0,
     63 	  0,
     64 	  NULL,
     65 	  NULL
     66 	}
     67 };
     68 
     69 static int
     70 nside_match(device_t parent, cfdata_t match, void *aux)
     71 {
     72 	struct pci_attach_args *pa = aux;
     73 
     74 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
     75 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     76 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
     77 		if (pciide_lookup_product(pa->pa_id, pciide_natsemi_products))
     78 			return 2;
     79 	}
     80 	return 0;
     81 }
     82 
     83 static void
     84 nside_attach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct pci_attach_args *pa = aux;
     87 	struct pciide_softc *sc = device_private(self);
     88 
     89 	self->dv_maxphys = MIN(parent->dv_maxphys, MACHINE_MAXPHYS);
     90 
     91 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     92 
     93 	pciide_common_attach(sc, pa,
     94 	    pciide_lookup_product(pa->pa_id, pciide_natsemi_products));
     95 }
     96 
     97 static void
     98 natsemi_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
     99 {
    100 	struct pciide_channel *cp;
    101 	int channel;
    102 	pcireg_t interface, ctl;
    103 
    104 	if (pciide_chipen(sc, pa) == 0)
    105 		return;
    106 
    107 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    108 	    "bus-master DMA support present");
    109 	pciide_mapreg_dma(sc, pa);
    110 	aprint_verbose("\n");
    111 
    112 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
    113 
    114 	if (sc->sc_dma_ok) {
    115 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    116 		sc->sc_wdcdev.irqack = natsemi_irqack;
    117 	}
    118 
    119 	pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CCBT, 0xb7);
    120 
    121 	/*
    122 	 * Mask off interrupts from both channels, appropriate channel(s)
    123 	 * will be unmasked later.
    124 	 */
    125 	pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2,
    126 	    pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2) |
    127 	    NATSEMI_CHMASK(0) | NATSEMI_CHMASK(1));
    128 
    129 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    130 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    131 	sc->sc_wdcdev.sc_atac.atac_set_modes = natsemi_setup_channel;
    132 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    133 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    134 	sc->sc_wdcdev.wdc_maxdrives = 2;
    135 
    136         interface = PCI_INTERFACE(pa->pa_class);
    137 	interface &= ~PCIIDE_CHANSTATUS_EN;	/* Reserved on PC87415 */
    138 
    139 	/* If we're in PCIIDE mode, unmask INTA, otherwise mask it. */
    140 	ctl = pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL1);
    141 	if (interface & (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1)))
    142 		ctl &= ~NATSEMI_CTRL1_INTAMASK;
    143 	else
    144 		ctl |= NATSEMI_CTRL1_INTAMASK;
    145 	pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL1, ctl);
    146 
    147 	wdc_allocate_regs(&sc->sc_wdcdev);
    148 
    149 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; channel++) {
    150 		cp = &sc->pciide_channels[channel];
    151 		if (pciide_chansetup(sc, channel, interface) == 0)
    152 			continue;
    153 
    154 		pciide_mapchan(pa, cp, interface, natsemi_pci_intr);
    155 
    156 		pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2,
    157 		    pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2) &
    158 		    ~(NATSEMI_CHMASK(channel)));
    159 	}
    160 }
    161 
    162 void
    163 natsemi_setup_channel(struct ata_channel *chp)
    164 {
    165 	struct ata_drive_datas *drvp;
    166 	int drive, ndrives = 0;
    167 	uint32_t idedma_ctl = 0;
    168         struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    169         struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    170 	uint8_t tim;
    171 
    172 	/* setup DMA if needed */
    173 	pciide_channel_dma_setup(cp);
    174 
    175 	for (drive = 0; drive < 2; drive++) {
    176 		drvp = &chp->ch_drive[drive];
    177 		/* If no drive, skip */
    178 		if (drvp->drive_type == ATA_DRIVET_NONE)
    179 			continue;
    180 
    181 		ndrives++;
    182 		/* add timing values, setup DMA if needed */
    183 		if ((drvp->drive_flags & ATA_DRIVE_DMA) == 0) {
    184 			tim = natsemi_pio_pulse[drvp->PIO_mode] |
    185 			    (natsemi_pio_recover[drvp->PIO_mode] << 4);
    186 		} else {
    187 			/*
    188 			 * use Multiword DMA
    189 			 * Timings will be used for both PIO and DMA,
    190 			 * so adjust DMA mode if needed
    191 			 */
    192 			if (drvp->PIO_mode >= 3 &&
    193 			    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
    194 				drvp->DMA_mode = drvp->PIO_mode - 2;
    195 			}
    196 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    197 			tim = natsemi_dma_pulse[drvp->DMA_mode] |
    198 			    (natsemi_dma_recover[drvp->DMA_mode] << 4);
    199 
    200 		}
    201 
    202 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
    203 		    NATSEMI_RTREG(chp->ch_channel, drive), tim);
    204 		pciide_pci_write(sc->sc_pc, sc->sc_tag,
    205 		    NATSEMI_WTREG(chp->ch_channel, drive), tim);
    206 	}
    207 
    208 	if (idedma_ctl != 0) {
    209 		/* Add software bits in status register */
    210 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    211 		    idedma_ctl);
    212 
    213 	}
    214 	/* Go ahead and ack interrupts generated during probe. */
    215 	natsemi_irqack(chp);
    216 }
    217 
    218 void
    219 natsemi_irqack(struct ata_channel *chp)
    220 {
    221         struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    222         struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    223 	uint8_t clr;
    224 
    225 	/* Errata: The "clear" bits are in the wrong register *sigh* */
    226 	clr = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0);
    227 	clr |= bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0) &
    228 	    (IDEDMA_CTL_ERR | IDEDMA_CTL_INTR);
    229 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0, clr);
    230 }
    231 
    232 int
    233 natsemi_pci_intr(void *arg)
    234 {
    235 	struct pciide_softc *sc = arg;
    236 	struct pciide_channel *cp;
    237 	struct ata_channel *wdc_cp;
    238 	int i, rv, crv;
    239 	uint8_t msk;
    240 
    241 	rv = 0;
    242 	msk = pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2);
    243 	for (i = 0; i <  sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    244 		cp = &sc->pciide_channels[i];
    245 		wdc_cp = &cp->ata_channel;
    246 
    247 		/* If a compat channel skip. */
    248 		if (cp->compat)
    249 			continue;
    250 
    251 		/* If this channel is masked, skip it. */
    252 		if (msk & NATSEMI_CHMASK(i))
    253 			continue;
    254 
    255 		crv = wdcintr(wdc_cp);
    256 		if (crv == 0)
    257 			;	/* leave alone */
    258 		else if (crv == 1)
    259 			rv = 1;		/* claim the intr */
    260 		else if (rv == 0)	/* crv should be -1 in this case */
    261 			rv = crv;	/* if we've done no better, take it */
    262 	}
    263 	return (rv);
    264 }
    265