Home | History | Annotate | Line # | Download | only in pci
stpcide.c revision 1.26.4.1
      1  1.26.4.1     rmind /*	$NetBSD: stpcide.c,v 1.26.4.1 2014/05/18 17:45:44 rmind Exp $	*/
      2       1.1  nisimura 
      3      1.19  nisimura /*-
      4      1.19  nisimura  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5      1.19  nisimura  * All rights reserved.
      6      1.19  nisimura  *
      7      1.19  nisimura  * This code is derived from software contributed to The NetBSD Foundation
      8      1.19  nisimura  * by Tohru Nishimura.
      9       1.1  nisimura  *
     10       1.1  nisimura  * Redistribution and use in source and binary forms, with or without
     11       1.1  nisimura  * modification, are permitted provided that the following conditions
     12       1.1  nisimura  * are met:
     13       1.1  nisimura  * 1. Redistributions of source code must retain the above copyright
     14       1.1  nisimura  *    notice, this list of conditions and the following disclaimer.
     15       1.1  nisimura  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  nisimura  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  nisimura  *    documentation and/or other materials provided with the distribution.
     18       1.1  nisimura  *
     19      1.19  nisimura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.19  nisimura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.19  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.19  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.19  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.19  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.19  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.19  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.19  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.19  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.19  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  nisimura  */
     31       1.1  nisimura 
     32      1.13     lukem #include <sys/cdefs.h>
     33  1.26.4.1     rmind __KERNEL_RCSID(0, "$NetBSD: stpcide.c,v 1.26.4.1 2014/05/18 17:45:44 rmind Exp $");
     34      1.13     lukem 
     35       1.1  nisimura #include <sys/param.h>
     36       1.1  nisimura #include <sys/systm.h>
     37       1.1  nisimura 
     38       1.1  nisimura #include <dev/pci/pcivar.h>
     39       1.1  nisimura #include <dev/pci/pcidevs.h>
     40       1.1  nisimura #include <dev/pci/pciidereg.h>
     41       1.1  nisimura #include <dev/pci/pciidevar.h>
     42       1.1  nisimura 
     43      1.21    dyoung static void stpc_chip_map(struct pciide_softc *,
     44      1.21    dyoung     const struct pci_attach_args *);
     45       1.6   thorpej static void stpc_setup_channel(struct ata_channel *);
     46       1.1  nisimura 
     47      1.18      cube static int  stpcide_match(device_t, cfdata_t, void *);
     48      1.18      cube static void stpcide_attach(device_t, device_t, void *);
     49       1.1  nisimura 
     50       1.1  nisimura const struct pciide_product_desc pciide_stpc_products[] = {
     51       1.1  nisimura 	{ 0x0228,
     52       1.1  nisimura 	  0,
     53       1.1  nisimura 	  "STMicroelectronics STPC IDE Controller",
     54       1.1  nisimura 	  stpc_chip_map,
     55       1.1  nisimura 	},
     56       1.1  nisimura 	{ 0, 0, NULL, NULL },
     57       1.1  nisimura };
     58       1.1  nisimura 
     59      1.18      cube CFATTACH_DECL_NEW(stpcide, sizeof(struct pciide_softc),
     60  1.26.4.1     rmind     stpcide_match, stpcide_attach, pciide_detach, NULL);
     61       1.1  nisimura 
     62       1.1  nisimura static int
     63      1.18      cube stpcide_match(device_t parent, cfdata_t match, void *aux)
     64       1.1  nisimura {
     65       1.1  nisimura 	struct pci_attach_args *pa = aux;
     66       1.1  nisimura 
     67       1.1  nisimura 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SGSTHOMSON) {
     68       1.1  nisimura 		if (pciide_lookup_product(pa->pa_id, pciide_stpc_products))
     69       1.1  nisimura 			return (2);
     70       1.1  nisimura 	}
     71       1.1  nisimura 	return (0);
     72       1.1  nisimura }
     73       1.1  nisimura 
     74       1.1  nisimura static void
     75      1.18      cube stpcide_attach(device_t parent, device_t self, void *aux)
     76       1.1  nisimura {
     77       1.1  nisimura 	struct pci_attach_args *pa = aux;
     78      1.18      cube 	struct pciide_softc *sc = device_private(self);
     79      1.18      cube 
     80      1.18      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     81       1.1  nisimura 
     82       1.1  nisimura 	pciide_common_attach(sc, pa,
     83       1.1  nisimura 	    pciide_lookup_product(pa->pa_id, pciide_stpc_products));
     84       1.1  nisimura 
     85       1.1  nisimura }
     86       1.1  nisimura 
     87       1.1  nisimura static void
     88      1.21    dyoung stpc_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
     89       1.1  nisimura {
     90       1.1  nisimura 	struct pciide_channel *cp;
     91       1.1  nisimura 	int channel;
     92       1.1  nisimura 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
     93       1.1  nisimura 
     94       1.1  nisimura 	if (pciide_chipen(sc, pa) == 0)
     95       1.1  nisimura 		return;
     96       1.1  nisimura 
     97      1.18      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
     98      1.18      cube 	    "bus-master DMA support present");
     99       1.1  nisimura 	pciide_mapreg_dma(sc, pa);
    100      1.17        ad 	aprint_verbose("\n");
    101       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    102       1.1  nisimura 	if (sc->sc_dma_ok) {
    103       1.8   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    104       1.1  nisimura 		sc->sc_wdcdev.irqack = pciide_irqack;
    105       1.1  nisimura 	}
    106       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    107       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    108       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 0;
    109       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = stpc_setup_channel;
    110       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    111       1.8   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    112      1.26    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
    113       1.1  nisimura 
    114       1.6   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    115       1.6   thorpej 
    116       1.8   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    117       1.8   thorpej 	     channel++) {
    118       1.1  nisimura 		cp = &sc->pciide_channels[channel];
    119       1.1  nisimura 		if (pciide_chansetup(sc, channel, interface) == 0)
    120       1.1  nisimura 			continue;
    121      1.20  jakllsch 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    122       1.1  nisimura 	}
    123       1.1  nisimura }
    124       1.1  nisimura 
    125       1.1  nisimura /*
    126       1.1  nisimura  * IDE timing register (0x40, 0x42, 0x44, and 0x46) assignment.
    127       1.1  nisimura  * 33MHz PCI system will have;
    128       1.1  nisimura  *	DMA0 01-11-11
    129       1.1  nisimura  *	DMA1 00-01-10
    130       1.1  nisimura  *	DMA2 00-00-10
    131       1.1  nisimura  *	PIO0          111-100
    132       1.1  nisimura  *	PIO1          100-011
    133       1.1  nisimura  *	PIO2          011-010
    134       1.1  nisimura  *	PIO3          010-001
    135       1.1  nisimura  *	PIO4          000-001
    136       1.1  nisimura  *	MISC                  XYZW
    137       1.1  nisimura  */
    138       1.1  nisimura static const u_int16_t dmatbl[] = { 0x7C00, 0x1800, 0x0800 };
    139       1.1  nisimura static const u_int16_t piotbl[] = { 0x03C0, 0x0230, 0x01A0, 0x0110, 0x0010 };
    140       1.1  nisimura 
    141       1.1  nisimura static void
    142       1.6   thorpej stpc_setup_channel(struct ata_channel *chp)
    143       1.1  nisimura {
    144       1.8   thorpej 	struct atac_softc *atac = chp->ch_atac;
    145       1.7   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    146       1.7   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    147       1.4   thorpej 	int channel = chp->ch_channel;
    148       1.1  nisimura 	struct ata_drive_datas *drvp;
    149       1.1  nisimura 	u_int32_t idedma_ctl, idetim;
    150       1.9   thorpej 	int drive, bits[2], s;
    151       1.1  nisimura 
    152       1.1  nisimura 	/* setup DMA if needed */
    153       1.1  nisimura 	pciide_channel_dma_setup(cp);
    154       1.1  nisimura 
    155       1.1  nisimura 	idedma_ctl = 0;
    156       1.1  nisimura 	bits[0] = bits[1] = 0x7F60; /* assume PIO2/DMA0 */
    157       1.1  nisimura 
    158       1.1  nisimura 	/* Per drive settings */
    159       1.1  nisimura 	for (drive = 0; drive < 2; drive++) {
    160       1.1  nisimura 		drvp = &chp->ch_drive[drive];
    161       1.1  nisimura 		/* If no drive, skip */
    162      1.26    bouyer 		if (drvp->drive_type == ATA_DRIVET_NONE)
    163       1.1  nisimura 			continue;
    164       1.1  nisimura 		/* add timing values, setup DMA if needed */
    165       1.8   thorpej 		if ((atac->atac_cap & ATAC_CAP_DMA) &&
    166      1.26    bouyer 		    (drvp->drive_flags & ATA_DRIVE_DMA)) {
    167       1.1  nisimura 			/* use Multiword DMA */
    168       1.9   thorpej 			s = splbio();
    169      1.26    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_UDMA;
    170       1.9   thorpej 			splx(s);
    171       1.1  nisimura 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    172       1.1  nisimura 			bits[drive] = 0xe; /* IOCHRDY,wr/post,rd/prefetch */
    173       1.1  nisimura 		}
    174       1.1  nisimura 		else {
    175       1.1  nisimura 			/* PIO only */
    176       1.9   thorpej 			s = splbio();
    177      1.26    bouyer 			drvp->drive_flags &= ~(ATA_DRIVE_UDMA | ATA_DRIVE_DMA);
    178       1.9   thorpej 			splx(s);
    179       1.1  nisimura 			bits[drive] = 0x8; /* IOCHRDY */
    180       1.1  nisimura 		}
    181       1.1  nisimura 		bits[drive] |= dmatbl[drvp->DMA_mode] | piotbl[drvp->PIO_mode];
    182       1.1  nisimura 	}
    183       1.1  nisimura #if 0
    184       1.1  nisimura 	idetim = pci_conf_read(sc->sc_pc, sc->sc_tag,
    185       1.1  nisimura 	    (channel == 0) ? 0x40 : 0x44);
    186       1.1  nisimura 	aprint_normal("wdc%d: IDETIM %08x -> %08x\n",
    187       1.1  nisimura 	    channel, idetim, (bits[1] << 16) | bits[0]);
    188       1.1  nisimura #endif
    189       1.1  nisimura 	idetim = (bits[1] << 16) | bits[0];
    190       1.1  nisimura 	pci_conf_write(sc->sc_pc, sc->sc_tag,
    191       1.1  nisimura 	    (channel == 0) ? 0x40 : 0x44, idetim);
    192       1.1  nisimura 
    193       1.1  nisimura 	if (idedma_ctl != 0) {
    194       1.1  nisimura 		/* Add software bits in status register */
    195       1.2      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    196       1.2      fvdl 		    idedma_ctl);
    197       1.1  nisimura 	}
    198       1.1  nisimura }
    199