Home | History | Annotate | Line # | Download | only in pci
rccide.c revision 1.26.2.2
      1  1.26.2.2       tls /*	$NetBSD: rccide.c,v 1.26.2.2 2014/08/20 00:03:48 tls Exp $	*/
      2       1.1   mycroft 
      3       1.1   mycroft /*
      4       1.1   mycroft  * Copyright (c) 2003 By Noon Software, Inc.  All rights reserved.
      5       1.1   mycroft  *
      6       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      7       1.1   mycroft  * modification, are permitted provided that the following conditions
      8       1.1   mycroft  * are met:
      9       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     10       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     11       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     14       1.1   mycroft  * 3. The names of the authors may not be used to endorse or promote products
     15       1.1   mycroft  *    derived from this software without specific prior written permission.
     16       1.1   mycroft  *
     17       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     18       1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1   mycroft  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26       1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1   mycroft  */
     28       1.1   mycroft 
     29       1.1   mycroft #include <sys/cdefs.h>
     30  1.26.2.2       tls __KERNEL_RCSID(0, "$NetBSD: rccide.c,v 1.26.2.2 2014/08/20 00:03:48 tls Exp $");
     31       1.1   mycroft 
     32       1.1   mycroft #include <sys/param.h>
     33       1.1   mycroft #include <sys/systm.h>
     34       1.1   mycroft 
     35       1.1   mycroft #include <dev/pci/pcivar.h>
     36       1.1   mycroft #include <dev/pci/pcidevs.h>
     37       1.1   mycroft #include <dev/pci/pciidereg.h>
     38       1.1   mycroft #include <dev/pci/pciidevar.h>
     39       1.1   mycroft 
     40       1.5   thorpej static void serverworks_chip_map(struct pciide_softc *,
     41      1.21    dyoung 				 const struct pci_attach_args *);
     42       1.9   thorpej static void serverworks_setup_channel(struct ata_channel *);
     43       1.5   thorpej static int  serverworks_pci_intr(void *);
     44       1.5   thorpej static int  serverworkscsb6_pci_intr(void *);
     45       1.1   mycroft 
     46      1.18      cube static int  rccide_match(device_t, cfdata_t, void *);
     47      1.18      cube static void rccide_attach(device_t, device_t, void *);
     48       1.1   mycroft 
     49      1.18      cube CFATTACH_DECL_NEW(rccide, sizeof(struct pciide_softc),
     50  1.26.2.2       tls     rccide_match, rccide_attach, pciide_detach, NULL);
     51       1.1   mycroft 
     52       1.5   thorpej static const struct pciide_product_desc pciide_serverworks_products[] =  {
     53       1.1   mycroft 	{ PCI_PRODUCT_SERVERWORKS_OSB4_IDE,
     54       1.1   mycroft 	  0,
     55       1.1   mycroft 	  "ServerWorks OSB4 IDE Controller",
     56       1.1   mycroft 	  serverworks_chip_map,
     57       1.1   mycroft 	},
     58       1.1   mycroft 	{ PCI_PRODUCT_SERVERWORKS_CSB5_IDE,
     59       1.1   mycroft 	  0,
     60       1.1   mycroft 	  "ServerWorks CSB5 IDE Controller",
     61       1.1   mycroft 	  serverworks_chip_map,
     62       1.1   mycroft 	},
     63       1.1   mycroft 	{ PCI_PRODUCT_SERVERWORKS_CSB6_IDE,
     64       1.1   mycroft 	  0,
     65       1.1   mycroft 	  "ServerWorks CSB6 RAID/IDE Controller",
     66       1.1   mycroft 	  serverworks_chip_map,
     67       1.1   mycroft 	},
     68       1.2     enami 	{ PCI_PRODUCT_SERVERWORKS_CSB6_RAID,
     69       1.2     enami 	  0,
     70       1.2     enami 	  "ServerWorks CSB6 RAID/IDE Controller",
     71       1.2     enami 	  serverworks_chip_map,
     72       1.2     enami 	},
     73      1.17   xtraeme 	{ PCI_PRODUCT_SERVERWORKS_HT1000_IDE,
     74      1.17   xtraeme 	  0,
     75      1.17   xtraeme 	  "ServerWorks HT-1000 IDE Controller",
     76      1.17   xtraeme 	  serverworks_chip_map,
     77      1.17   xtraeme 	},
     78       1.1   mycroft 	{ 0,
     79       1.1   mycroft 	  0,
     80       1.1   mycroft 	  NULL,
     81       1.1   mycroft 	  NULL,
     82       1.1   mycroft 	}
     83       1.1   mycroft };
     84       1.1   mycroft 
     85       1.5   thorpej static int
     86      1.18      cube rccide_match(device_t parent, cfdata_t match, void *aux)
     87       1.1   mycroft {
     88       1.1   mycroft 	struct pci_attach_args *pa = aux;
     89       1.1   mycroft 
     90       1.2     enami 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SERVERWORKS &&
     91       1.2     enami 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
     92       1.2     enami 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
     93       1.2     enami 		if (pciide_lookup_product(pa->pa_id,
     94       1.2     enami 		    pciide_serverworks_products))
     95       1.1   mycroft 			return (2);
     96       1.1   mycroft 	}
     97       1.1   mycroft 	return (0);
     98       1.1   mycroft }
     99       1.1   mycroft 
    100       1.5   thorpej static void
    101      1.18      cube rccide_attach(device_t parent, device_t self, void *aux)
    102       1.1   mycroft {
    103       1.1   mycroft 	struct pci_attach_args *pa = aux;
    104      1.18      cube 	struct pciide_softc *sc = device_private(self);
    105      1.18      cube 
    106  1.26.2.1    bouyer 	self->dv_maxphys = MIN(parent->dv_maxphys, MACHINE_MAXPHYS);
    107  1.26.2.1    bouyer 
    108      1.18      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    109       1.1   mycroft 
    110       1.1   mycroft 	pciide_common_attach(sc, pa,
    111       1.1   mycroft 	    pciide_lookup_product(pa->pa_id, pciide_serverworks_products));
    112       1.1   mycroft }
    113       1.1   mycroft 
    114       1.5   thorpej static void
    115      1.21    dyoung serverworks_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    116       1.1   mycroft {
    117       1.1   mycroft 	struct pciide_channel *cp;
    118       1.1   mycroft 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    119       1.1   mycroft 	pcitag_t pcib_tag;
    120       1.1   mycroft 	int channel;
    121       1.1   mycroft 
    122       1.1   mycroft 	if (pciide_chipen(sc, pa) == 0)
    123       1.1   mycroft 		return;
    124       1.1   mycroft 
    125      1.18      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    126      1.18      cube 	    "bus-master DMA support present");
    127       1.1   mycroft 	pciide_mapreg_dma(sc, pa);
    128      1.16        ad 	aprint_verbose("\n");
    129      1.11   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    130       1.1   mycroft 
    131       1.1   mycroft 	if (sc->sc_dma_ok) {
    132      1.11   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    133       1.1   mycroft 		sc->sc_wdcdev.irqack = pciide_irqack;
    134       1.1   mycroft 	}
    135      1.11   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    136      1.11   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    137       1.1   mycroft 	switch (sc->sc_pp->ide_product) {
    138       1.1   mycroft 	case PCI_PRODUCT_SERVERWORKS_OSB4_IDE:
    139      1.11   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    140       1.1   mycroft 		break;
    141       1.1   mycroft 	case PCI_PRODUCT_SERVERWORKS_CSB5_IDE:
    142       1.1   mycroft 		if (PCI_REVISION(pa->pa_class) < 0x92)
    143      1.11   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    144       1.1   mycroft 		else
    145      1.11   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    146       1.1   mycroft 		break;
    147       1.1   mycroft 	case PCI_PRODUCT_SERVERWORKS_CSB6_IDE:
    148       1.2     enami 	case PCI_PRODUCT_SERVERWORKS_CSB6_RAID:
    149      1.17   xtraeme 	case PCI_PRODUCT_SERVERWORKS_HT1000_IDE:
    150      1.11   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    151       1.1   mycroft 		break;
    152       1.1   mycroft 	}
    153       1.1   mycroft 
    154      1.11   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = serverworks_setup_channel;
    155      1.11   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    156      1.11   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = 2;
    157      1.26    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
    158       1.1   mycroft 
    159       1.9   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    160       1.9   thorpej 
    161      1.11   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    162      1.11   thorpej 	     channel++) {
    163       1.1   mycroft 		cp = &sc->pciide_channels[channel];
    164       1.1   mycroft 		if (pciide_chansetup(sc, channel, interface) == 0)
    165       1.1   mycroft 			continue;
    166       1.4    bouyer 		switch (sc->sc_pp->ide_product) {
    167       1.4    bouyer 		case PCI_PRODUCT_SERVERWORKS_CSB6_IDE:
    168       1.4    bouyer 		case PCI_PRODUCT_SERVERWORKS_CSB6_RAID:
    169      1.20  jakllsch 			pciide_mapchan(pa, cp, interface,
    170       1.4    bouyer 			    serverworkscsb6_pci_intr);
    171       1.4    bouyer 			break;
    172       1.4    bouyer 		default:
    173      1.20  jakllsch 			pciide_mapchan(pa, cp, interface,
    174       1.4    bouyer 			    serverworks_pci_intr);
    175       1.4    bouyer 		}
    176       1.1   mycroft 	}
    177       1.1   mycroft 
    178       1.1   mycroft 	pcib_tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 0);
    179       1.1   mycroft 	pci_conf_write(pa->pa_pc, pcib_tag, 0x64,
    180       1.1   mycroft 	    (pci_conf_read(pa->pa_pc, pcib_tag, 0x64) & ~0x2000) | 0x4000);
    181       1.1   mycroft }
    182       1.1   mycroft 
    183       1.5   thorpej static void
    184       1.9   thorpej serverworks_setup_channel(struct ata_channel *chp)
    185       1.1   mycroft {
    186       1.1   mycroft 	struct ata_drive_datas *drvp;
    187      1.11   thorpej 	struct atac_softc *atac = chp->ch_atac;
    188      1.10   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    189      1.10   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    190       1.7   thorpej 	int channel = chp->ch_channel;
    191      1.12   thorpej 	int drive, unit, s;
    192       1.1   mycroft 	u_int32_t pio_time, dma_time, pio_mode, udma_mode;
    193       1.1   mycroft 	u_int32_t idedma_ctl;
    194       1.1   mycroft 	static const u_int8_t pio_modes[5] = {0x5d, 0x47, 0x34, 0x22, 0x20};
    195       1.1   mycroft 	static const u_int8_t dma_modes[3] = {0x77, 0x21, 0x20};
    196       1.1   mycroft 
    197       1.1   mycroft 	/* setup DMA if needed */
    198       1.1   mycroft 	pciide_channel_dma_setup(cp);
    199       1.1   mycroft 
    200       1.1   mycroft 	pio_time = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x40);
    201       1.1   mycroft 	dma_time = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x44);
    202       1.1   mycroft 	pio_mode = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x48);
    203       1.1   mycroft 	udma_mode = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54);
    204       1.1   mycroft 
    205       1.1   mycroft 	pio_time &= ~(0xffff << (16 * channel));
    206       1.1   mycroft 	dma_time &= ~(0xffff << (16 * channel));
    207       1.1   mycroft 	pio_mode &= ~(0xff << (8 * channel + 16));
    208       1.1   mycroft 	udma_mode &= ~(0xff << (8 * channel + 16));
    209       1.1   mycroft 	udma_mode &= ~(3 << (2 * channel));
    210       1.1   mycroft 
    211       1.1   mycroft 	idedma_ctl = 0;
    212       1.1   mycroft 
    213       1.1   mycroft 	/* Per drive settings */
    214       1.1   mycroft 	for (drive = 0; drive < 2; drive++) {
    215       1.1   mycroft 		drvp = &chp->ch_drive[drive];
    216       1.1   mycroft 		/* If no drive, skip */
    217      1.26    bouyer 		if (drvp->drive_type == ATA_DRIVET_NONE)
    218       1.1   mycroft 			continue;
    219       1.1   mycroft 		unit = drive + 2 * channel;
    220       1.1   mycroft 		/* add timing values, setup DMA if needed */
    221       1.1   mycroft 		pio_time |= pio_modes[drvp->PIO_mode] << (8 * (unit^1));
    222       1.1   mycroft 		pio_mode |= drvp->PIO_mode << (4 * unit + 16);
    223      1.11   thorpej 		if ((atac->atac_cap & ATAC_CAP_UDMA) &&
    224      1.26    bouyer 		    (drvp->drive_flags & ATA_DRIVE_UDMA)) {
    225       1.1   mycroft 			/* use Ultra/DMA, check for 80-pin cable */
    226       1.1   mycroft 			if (drvp->UDMA_mode > 2 &&
    227       1.5   thorpej 			    (PCI_PRODUCT(pci_conf_read(sc->sc_pc, sc->sc_tag,
    228       1.5   thorpej 			    			       PCI_SUBSYS_ID_REG))
    229       1.5   thorpej 			     & (1 << (14 + channel))) == 0)
    230       1.1   mycroft 				drvp->UDMA_mode = 2;
    231       1.1   mycroft 			dma_time |= dma_modes[drvp->DMA_mode] << (8 * (unit^1));
    232       1.1   mycroft 			udma_mode |= drvp->UDMA_mode << (4 * unit + 16);
    233       1.1   mycroft 			udma_mode |= 1 << unit;
    234       1.1   mycroft 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    235      1.11   thorpej 		} else if ((atac->atac_cap & ATAC_CAP_DMA) &&
    236      1.26    bouyer 		    (drvp->drive_flags & ATA_DRIVE_DMA)) {
    237       1.1   mycroft 			/* use Multiword DMA */
    238      1.12   thorpej 			s = splbio();
    239      1.26    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_UDMA;
    240      1.12   thorpej 			splx(s);
    241       1.1   mycroft 			dma_time |= dma_modes[drvp->DMA_mode] << (8 * (unit^1));
    242       1.1   mycroft 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    243       1.1   mycroft 		} else {
    244       1.1   mycroft 			/* PIO only */
    245      1.12   thorpej 			s = splbio();
    246      1.26    bouyer 			drvp->drive_flags &= ~(ATA_DRIVE_UDMA | ATA_DRIVE_DMA);
    247      1.12   thorpej 			splx(s);
    248       1.1   mycroft 		}
    249       1.1   mycroft 	}
    250       1.1   mycroft 
    251       1.1   mycroft 	pci_conf_write(sc->sc_pc, sc->sc_tag, 0x40, pio_time);
    252       1.1   mycroft 	pci_conf_write(sc->sc_pc, sc->sc_tag, 0x44, dma_time);
    253       1.1   mycroft 	if (sc->sc_pp->ide_product != PCI_PRODUCT_SERVERWORKS_OSB4_IDE)
    254       1.1   mycroft 		pci_conf_write(sc->sc_pc, sc->sc_tag, 0x48, pio_mode);
    255       1.1   mycroft 	pci_conf_write(sc->sc_pc, sc->sc_tag, 0x54, udma_mode);
    256       1.1   mycroft 
    257       1.1   mycroft 	if (idedma_ctl != 0) {
    258       1.1   mycroft 		/* Add software bits in status register */
    259       1.3      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    260       1.3      fvdl 		    idedma_ctl);
    261       1.1   mycroft 	}
    262       1.1   mycroft }
    263       1.1   mycroft 
    264       1.5   thorpej static int
    265      1.19       dsl serverworks_pci_intr(void *arg)
    266       1.1   mycroft {
    267       1.1   mycroft 	struct pciide_softc *sc = arg;
    268       1.1   mycroft 	struct pciide_channel *cp;
    269       1.9   thorpej 	struct ata_channel *wdc_cp;
    270       1.1   mycroft 	int rv = 0;
    271       1.1   mycroft 	int dmastat, i, crv;
    272       1.1   mycroft 
    273      1.11   thorpej 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    274       1.3      fvdl 		cp = &sc->pciide_channels[i];
    275       1.3      fvdl 		dmastat = bus_space_read_1(sc->sc_dma_iot,
    276       1.3      fvdl 		    cp->dma_iohs[IDEDMA_CTL], 0);
    277       1.1   mycroft 		if ((dmastat & (IDEDMA_CTL_ACT | IDEDMA_CTL_INTR)) !=
    278       1.1   mycroft 		    IDEDMA_CTL_INTR)
    279       1.1   mycroft 			continue;
    280       1.9   thorpej 		wdc_cp = &cp->ata_channel;
    281       1.1   mycroft 		crv = wdcintr(wdc_cp);
    282       1.1   mycroft 		if (crv == 0) {
    283      1.18      cube 			aprint_error("%s:%d: bogus intr\n",
    284      1.18      cube 			    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), i);
    285       1.3      fvdl 			bus_space_write_1(sc->sc_dma_iot,
    286       1.3      fvdl 			    cp->dma_iohs[IDEDMA_CTL], 0, dmastat);
    287       1.1   mycroft 		} else
    288       1.4    bouyer 			rv = 1;
    289       1.4    bouyer 	}
    290       1.4    bouyer 	return rv;
    291       1.4    bouyer }
    292       1.4    bouyer 
    293       1.5   thorpej static int
    294      1.19       dsl serverworkscsb6_pci_intr(void *arg)
    295       1.4    bouyer {
    296       1.4    bouyer 	struct pciide_softc *sc = arg;
    297       1.4    bouyer 	struct pciide_channel *cp;
    298       1.9   thorpej 	struct ata_channel *wdc_cp;
    299       1.4    bouyer 	int rv = 0;
    300       1.4    bouyer 	int i, crv;
    301       1.4    bouyer 
    302      1.11   thorpej 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    303       1.4    bouyer 		cp = &sc->pciide_channels[i];
    304       1.9   thorpej 		wdc_cp = &cp->ata_channel;
    305       1.4    bouyer 		/*
    306       1.4    bouyer 		 * The CSB6 doesn't assert IDEDMA_CTL_INTR for non-DMA commands.
    307       1.4    bouyer 		 * Until we find a way to know if the controller posted an
    308       1.4    bouyer 		 * interrupt, always call wdcintr(), which will try to guess
    309       1.4    bouyer 		 * if the interrupt was for us or not (and checks
    310       1.4    bouyer 		 * IDEDMA_CTL_INTR for DMA commands only).
    311       1.4    bouyer 		 */
    312       1.4    bouyer 		crv = wdcintr(wdc_cp);
    313       1.4    bouyer 		if (crv != 0)
    314       1.1   mycroft 			rv = 1;
    315       1.1   mycroft 	}
    316       1.1   mycroft 	return rv;
    317       1.1   mycroft }
    318