Home | History | Annotate | Line # | Download | only in pci
toshide.c revision 1.9.2.2
      1 /*	$NetBSD: toshide.c,v 1.9.2.2 2014/08/20 00:03:48 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: toshide.c,v 1.9.2.2 2014/08/20 00:03:48 tls Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <dev/pci/pcivar.h>
     36 #include <dev/pci/pcidevs.h>
     37 #include <dev/pci/pciidereg.h>
     38 #include <dev/pci/pciidevar.h>
     39 #include <dev/pci/pciide_piccolo_reg.h>
     40 
     41 static void piccolo_chip_map(struct pciide_softc *,
     42     const struct pci_attach_args *);
     43 static void piccolo_setup_channel(struct ata_channel *);
     44 
     45 static int  piccolo_match(device_t, cfdata_t, void *);
     46 static void piccolo_attach(device_t, device_t, void *);
     47 
     48 CFATTACH_DECL_NEW(toshide, sizeof(struct pciide_softc),
     49     piccolo_match, piccolo_attach, pciide_detach, NULL);
     50 
     51 static const struct pciide_product_desc pciide_toshiba2_products[] = {
     52 	{
     53 		PCI_PRODUCT_TOSHIBA2_PICCOLO,
     54 		0,
     55 		"Toshiba Piccolo IDE controller",
     56 		piccolo_chip_map,
     57 	},
     58 	{
     59 		PCI_PRODUCT_TOSHIBA2_PICCOLO2,
     60 		0,
     61 		"Toshiba Piccolo 2 IDE controller",
     62 		piccolo_chip_map,
     63 	},
     64 	{
     65 		PCI_PRODUCT_TOSHIBA2_PICCOLO3,
     66 		0,
     67 		"Toshiba Piccolo 3 IDE controller",
     68 		piccolo_chip_map,
     69 	},
     70 	{
     71 		PCI_PRODUCT_TOSHIBA2_PICCOLO5,
     72 		0,
     73 		"Toshiba Piccolo 5 IDE controller",
     74 		piccolo_chip_map,
     75 	},
     76 	{
     77 		0,
     78 		0,
     79 		NULL,
     80 		NULL,
     81 	}
     82 };
     83 
     84 static int
     85 piccolo_match(device_t parent, cfdata_t match, void *aux)
     86 {
     87 	struct pci_attach_args *pa = aux;
     88 
     89 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2) {
     90 		if (pciide_lookup_product(pa->pa_id, pciide_toshiba2_products))
     91 			return 2;
     92 	}
     93 	return 0;
     94 }
     95 
     96 static void
     97 piccolo_attach(device_t parent, device_t self, void *aux)
     98 {
     99 	struct pci_attach_args *pa = aux;
    100 	struct pciide_softc *sc = device_private(self);
    101 	const struct pciide_product_desc *pp;
    102 
    103 	self->dv_maxphys = MIN(parent->dv_maxphys, MACHINE_MAXPHYS);
    104 
    105 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    106 
    107 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2)
    108 		pp = pciide_lookup_product(pa->pa_id, pciide_toshiba2_products);
    109 	else
    110 		pp = NULL;
    111 	if (pp == NULL)
    112 		panic("toshide_attach");
    113 	pciide_common_attach(sc, pa, pp);
    114 }
    115 
    116 static void
    117 piccolo_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    118 {
    119 	struct pciide_channel *cp;
    120 	pcireg_t interface;
    121 	int channel;
    122 
    123 	if (pciide_chipen(sc, pa) == 0)
    124 		return;
    125 
    126 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    127 	    "bus-master DMA support present");
    128 
    129 	pciide_mapreg_dma(sc, pa);
    130 	aprint_verbose("\n");
    131 
    132 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA32 | ATAC_CAP_DATA16;
    133 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 5;
    134 
    135 	if (sc->sc_dma_ok) {
    136 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    137 		sc->sc_wdcdev.irqack = pciide_irqack;
    138 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 3;
    139 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    140 	}
    141 
    142 	sc->sc_wdcdev.sc_atac.atac_set_modes = piccolo_setup_channel;
    143 
    144 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    145 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    146 	sc->sc_wdcdev.wdc_maxdrives = 2;
    147 	/*
    148 	 * XXX one for now. We'll figure out how to talk to the second channel
    149 	 * later, hopefully! Second interface config is via the
    150 	 * "alternate PCI Configuration Space" whatever that is!
    151 	 */
    152 
    153 	interface = PCI_INTERFACE(pa->pa_class);
    154 
    155 	wdc_allocate_regs(&sc->sc_wdcdev);
    156 
    157 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    158 	     channel++) {
    159 		cp = &sc->pciide_channels[channel];
    160 		if (pciide_chansetup(sc, channel, interface) == 0)
    161 			continue;
    162 
    163 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    164 	}
    165 }
    166 
    167 static void
    168 piccolo_setup_channel(struct ata_channel *chp)
    169 {
    170 	struct ata_drive_datas *drvp;
    171 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    172 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    173 	u_int32_t idedma_ctl;
    174 	int drive, s;
    175 	pcireg_t pxdx;
    176 #ifdef TOSHIDE_DEBUG
    177 	pcireg_t pxdx_prime;
    178 #endif
    179 
    180 	idedma_ctl = 0;
    181 
    182 	/* Set up DMA if needed. */
    183 	pciide_channel_dma_setup(cp);
    184 
    185 	for (drive = 0; drive < 2; drive++) {
    186 
    187 		drvp = &chp->ch_drive[drive];
    188 		/* If no drive, skip */
    189 		if (drvp->drive_type == ATA_DRIVET_NONE)
    190 			continue;
    191 
    192 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
    193 			/* use Ultra/DMA */
    194 			s = splbio();
    195 			drvp->drive_flags &= ~ATA_DRIVE_DMA;
    196 			splx(s);
    197 
    198 			/*
    199 			 * Use UDMA - we can go up to mode 2 so no need to
    200 			 * check anything since nearly all drives with UDMA
    201 			 * are mode 2 or faster
    202 			 */
    203 			pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag,
    204 			    PICCOLO_DMA_TIMING);
    205  		        pxdx &= PICCOLO_UDMA_MASK;
    206  		        pxdx |= piccolo_udma_times[2];
    207  		        pci_conf_write(sc->sc_pc, sc->sc_tag,
    208 			    PICCOLO_DMA_TIMING, pxdx);
    209 #ifdef TOSHIDE_DEBUG
    210 			/* XXX sanity check */
    211  			pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag,
    212 			    PICCOLO_DMA_TIMING);
    213 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    214 			    "UDMA want %x, set %x, got %x\n",
    215 			    piccolo_udma_times[2], pxdx, pxdx_prime);
    216 #endif
    217 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    218 
    219 		}
    220 		else if (drvp->drive_flags & ATA_DRIVE_DMA) {
    221 			/*
    222 			 * Use Multiword DMA
    223 			 */
    224 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
    225 				drvp->PIO_mode = drvp->DMA_mode + 2;
    226 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
    227 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
    228 				    drvp->PIO_mode - 2 : 0;
    229 
    230 			pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag,
    231 			    PICCOLO_DMA_TIMING);
    232  		        pxdx &= PICCOLO_DMA_MASK;
    233  			pxdx |= piccolo_mw_dma_times[drvp->DMA_mode];
    234  		        pci_conf_write(sc->sc_pc, sc->sc_tag,
    235 			    PICCOLO_DMA_TIMING, pxdx);
    236 #ifdef TOSHIDE_DEBUG
    237 			/* XXX sanity check */
    238  			pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag,
    239 			    PICCOLO_DMA_TIMING);
    240 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    241 			    "DMA %d want %x, set %x, got %x\n",
    242 			    drvp->DMA_mode,
    243 			    piccolo_mw_dma_times[drvp->DMA_mode], pxdx,
    244 			    pxdx_prime);
    245 #endif
    246 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    247 
    248 		}
    249 		else {
    250 			pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag,
    251 			    PICCOLO_PIO_TIMING);
    252  		        pxdx &= PICCOLO_PIO_MASK;
    253 			pxdx |= piccolo_pio_times[drvp->PIO_mode];
    254  		        pci_conf_write(sc->sc_pc, sc->sc_tag,
    255 			    PICCOLO_PIO_TIMING, pxdx);
    256 #ifdef TOSHIDE_DEBUG
    257 			/* XXX sanity check */
    258  			pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag,
    259 			    PICCOLO_PIO_TIMING);
    260 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    261 			    "PIO %d want %x, set %x, got %x\n", drvp->PIO_mode,
    262 			    piccolo_pio_times[drvp->PIO_mode], pxdx,
    263 			    pxdx_prime);
    264 #endif
    265 		}
    266 
    267 	}
    268 	if (idedma_ctl != 0) {
    269 		/* Add software bits in status register */
    270 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    271 		    idedma_ctl);
    272 	}
    273 }
    274