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