Home | History | Annotate | Line # | Download | only in pci
acardide.c revision 1.23.18.1
      1  1.23.18.1  uebayasi /*	$NetBSD: acardide.c,v 1.23.18.1 2010/11/06 08:08:30 uebayasi Exp $	*/
      2        1.1    bouyer 
      3       1.23   tsutsui /*-
      4       1.23   tsutsui  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
      5        1.1    bouyer  *
      6        1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      7        1.1    bouyer  * modification, are permitted provided that the following conditions
      8        1.1    bouyer  * are met:
      9        1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     10        1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     11        1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13        1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     14        1.1    bouyer  *
     15        1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16        1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17        1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18        1.4   tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19        1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20        1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21        1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22        1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23        1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24        1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25        1.1    bouyer  */
     26        1.1    bouyer 
     27       1.16     lukem #include <sys/cdefs.h>
     28  1.23.18.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: acardide.c,v 1.23.18.1 2010/11/06 08:08:30 uebayasi Exp $");
     29       1.16     lukem 
     30        1.1    bouyer #include <sys/param.h>
     31        1.1    bouyer #include <sys/systm.h>
     32        1.1    bouyer 
     33        1.1    bouyer #include <dev/pci/pcivar.h>
     34        1.1    bouyer #include <dev/pci/pcidevs.h>
     35        1.1    bouyer #include <dev/pci/pciidereg.h>
     36        1.1    bouyer #include <dev/pci/pciidevar.h>
     37        1.1    bouyer #include <dev/pci/pciide_acard_reg.h>
     38        1.1    bouyer 
     39        1.2   thorpej static void acard_chip_map(struct pciide_softc*, struct pci_attach_args*);
     40       1.11   thorpej static void acard_setup_channel(struct ata_channel*);
     41        1.2   thorpej #if 0 /* XXX !! */
     42        1.2   thorpej static int  acard_pci_intr(void *);
     43        1.2   thorpej #endif
     44        1.1    bouyer 
     45       1.22      cube static int  acardide_match(device_t, cfdata_t, void *);
     46       1.22      cube static void acardide_attach(device_t, device_t, void *);
     47        1.1    bouyer 
     48       1.22      cube CFATTACH_DECL_NEW(acardide, sizeof(struct pciide_softc),
     49        1.1    bouyer     acardide_match, acardide_attach, NULL, NULL);
     50        1.1    bouyer 
     51        1.2   thorpej static const struct pciide_product_desc pciide_acard_products[] =  {
     52        1.1    bouyer 	{ PCI_PRODUCT_ACARD_ATP850U,
     53        1.3   mycroft 	  0,
     54        1.1    bouyer 	  "Acard ATP850U Ultra33 IDE Controller",
     55        1.1    bouyer 	  acard_chip_map,
     56        1.1    bouyer 	},
     57        1.1    bouyer 	{ PCI_PRODUCT_ACARD_ATP860,
     58        1.3   mycroft 	  0,
     59        1.1    bouyer 	  "Acard ATP860 Ultra66 IDE Controller",
     60        1.1    bouyer 	  acard_chip_map,
     61        1.1    bouyer 	},
     62        1.1    bouyer 	{ PCI_PRODUCT_ACARD_ATP860A,
     63        1.3   mycroft 	  0,
     64        1.1    bouyer 	  "Acard ATP860-A Ultra66 IDE Controller",
     65        1.1    bouyer 	  acard_chip_map,
     66        1.1    bouyer 	},
     67        1.5   tsutsui 	{ PCI_PRODUCT_ACARD_ATP865,
     68        1.5   tsutsui 	  0,
     69       1.17   tsutsui 	  "Acard ATP865 Ultra133 IDE Controller",
     70        1.5   tsutsui 	  acard_chip_map,
     71        1.5   tsutsui 	},
     72        1.5   tsutsui 	{ PCI_PRODUCT_ACARD_ATP865A,
     73        1.5   tsutsui 	  0,
     74       1.17   tsutsui 	  "Acard ATP865-A Ultra133 IDE Controller",
     75        1.5   tsutsui 	  acard_chip_map,
     76        1.5   tsutsui 	},
     77        1.1    bouyer 	{ 0,
     78        1.1    bouyer 	  0,
     79        1.1    bouyer 	  NULL,
     80        1.1    bouyer 	  NULL
     81        1.1    bouyer 	}
     82        1.1    bouyer };
     83        1.1    bouyer 
     84        1.2   thorpej static int
     85       1.22      cube acardide_match(device_t parent, cfdata_t match, void *aux)
     86        1.1    bouyer {
     87        1.1    bouyer 	struct pci_attach_args *pa = aux;
     88        1.1    bouyer 
     89        1.1    bouyer 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ACARD) {
     90        1.1    bouyer 		if (pciide_lookup_product(pa->pa_id, pciide_acard_products))
     91        1.1    bouyer 			return (2);
     92        1.1    bouyer 	}
     93        1.1    bouyer 	return (0);
     94        1.1    bouyer }
     95        1.1    bouyer 
     96        1.2   thorpej static void
     97       1.22      cube acardide_attach(device_t parent, device_t self, void *aux)
     98        1.1    bouyer {
     99        1.1    bouyer 	struct pci_attach_args *pa = aux;
    100       1.22      cube 	struct pciide_softc *sc = device_private(self);
    101       1.22      cube 
    102       1.22      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    103        1.1    bouyer 
    104        1.1    bouyer 	pciide_common_attach(sc, pa,
    105        1.1    bouyer 	    pciide_lookup_product(pa->pa_id, pciide_acard_products));
    106        1.1    bouyer 
    107        1.1    bouyer }
    108        1.1    bouyer 
    109        1.1    bouyer #define	ACARD_IS_850(sc)						\
    110        1.1    bouyer 	((sc)->sc_pp->ide_product == PCI_PRODUCT_ACARD_ATP850U)
    111        1.1    bouyer 
    112        1.2   thorpej static void
    113        1.2   thorpej acard_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    114        1.1    bouyer {
    115        1.1    bouyer 	struct pciide_channel *cp;
    116        1.1    bouyer 	int i;
    117        1.1    bouyer 	pcireg_t interface;
    118        1.1    bouyer 
    119        1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    120        1.1    bouyer 		return;
    121        1.1    bouyer 
    122        1.4   tsutsui 	/*
    123        1.1    bouyer 	 * when the chip is in native mode it identifies itself as a
    124        1.1    bouyer 	 * 'misc mass storage'. Fake interface in this case.
    125        1.1    bouyer 	 */
    126        1.1    bouyer 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    127        1.1    bouyer 		interface = PCI_INTERFACE(pa->pa_class);
    128        1.1    bouyer 	} else {
    129        1.1    bouyer 		interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
    130        1.1    bouyer 		    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    131        1.1    bouyer 	}
    132        1.1    bouyer 
    133       1.22      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    134       1.22      cube 	    "bus-master DMA support present");
    135        1.1    bouyer 	pciide_mapreg_dma(sc, pa);
    136       1.21        ad 	aprint_verbose("\n");
    137       1.13   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    138        1.1    bouyer 
    139        1.1    bouyer 	if (sc->sc_dma_ok) {
    140       1.13   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    141        1.1    bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    142        1.1    bouyer 	}
    143       1.13   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    144       1.13   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    145        1.5   tsutsui 	switch (sc->sc_pp->ide_product) {
    146        1.5   tsutsui 	case PCI_PRODUCT_ACARD_ATP860:
    147        1.5   tsutsui 	case PCI_PRODUCT_ACARD_ATP860A:
    148       1.13   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    149        1.5   tsutsui 		break;
    150        1.5   tsutsui 	case PCI_PRODUCT_ACARD_ATP865:
    151        1.5   tsutsui 	case PCI_PRODUCT_ACARD_ATP865A:
    152       1.15   tsutsui 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    153        1.5   tsutsui 		break;
    154        1.5   tsutsui 	default:
    155       1.13   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    156        1.5   tsutsui 		break;
    157        1.5   tsutsui 	}
    158        1.1    bouyer 
    159       1.13   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = acard_setup_channel;
    160       1.13   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    161       1.13   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = 2;
    162        1.1    bouyer 
    163       1.11   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    164       1.11   thorpej 
    165       1.13   thorpej 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    166        1.1    bouyer 		cp = &sc->pciide_channels[i];
    167        1.1    bouyer 		if (pciide_chansetup(sc, i, interface) == 0)
    168        1.1    bouyer 			continue;
    169  1.23.18.1  uebayasi 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    170        1.1    bouyer 	}
    171        1.1    bouyer 	if (!ACARD_IS_850(sc)) {
    172        1.1    bouyer 		u_int32_t reg;
    173        1.1    bouyer 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, ATP8x0_CTRL);
    174        1.1    bouyer 		reg &= ~ATP860_CTRL_INT;
    175        1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, ATP8x0_CTRL, reg);
    176        1.1    bouyer 	}
    177        1.1    bouyer }
    178        1.1    bouyer 
    179        1.2   thorpej static void
    180       1.11   thorpej acard_setup_channel(struct ata_channel *chp)
    181        1.1    bouyer {
    182        1.1    bouyer 	struct ata_drive_datas *drvp;
    183       1.13   thorpej 	struct atac_softc *atac = chp->ch_atac;
    184       1.12   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    185       1.12   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    186        1.8   thorpej 	int channel = chp->ch_channel;
    187       1.14   thorpej 	int drive, s;
    188        1.1    bouyer 	u_int32_t idetime, udma_mode;
    189        1.1    bouyer 	u_int32_t idedma_ctl;
    190        1.1    bouyer 
    191        1.1    bouyer 	/* setup DMA if needed */
    192        1.1    bouyer 	pciide_channel_dma_setup(cp);
    193        1.1    bouyer 
    194        1.1    bouyer 	if (ACARD_IS_850(sc)) {
    195        1.1    bouyer 		idetime = 0;
    196        1.1    bouyer 		udma_mode = pci_conf_read(sc->sc_pc, sc->sc_tag, ATP850_UDMA);
    197        1.1    bouyer 		udma_mode &= ~ATP850_UDMA_MASK(channel);
    198        1.1    bouyer 	} else {
    199        1.1    bouyer 		idetime = pci_conf_read(sc->sc_pc, sc->sc_tag, ATP860_IDETIME);
    200        1.1    bouyer 		idetime &= ~ATP860_SETTIME_MASK(channel);
    201        1.1    bouyer 		udma_mode = pci_conf_read(sc->sc_pc, sc->sc_tag, ATP860_UDMA);
    202        1.1    bouyer 		udma_mode &= ~ATP860_UDMA_MASK(channel);
    203        1.1    bouyer 
    204        1.1    bouyer 		/* check 80 pins cable */
    205        1.1    bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_UDMA) ||
    206        1.1    bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_UDMA)) {
    207        1.1    bouyer 			if (pci_conf_read(sc->sc_pc, sc->sc_tag, ATP8x0_CTRL)
    208        1.8   thorpej 			    & ATP860_CTRL_80P(chp->ch_channel)) {
    209        1.1    bouyer 				if (chp->ch_drive[0].UDMA_mode > 2)
    210        1.1    bouyer 					chp->ch_drive[0].UDMA_mode = 2;
    211        1.1    bouyer 				if (chp->ch_drive[1].UDMA_mode > 2)
    212        1.1    bouyer 					chp->ch_drive[1].UDMA_mode = 2;
    213        1.1    bouyer 			}
    214        1.1    bouyer 		}
    215        1.1    bouyer 	}
    216        1.1    bouyer 
    217        1.1    bouyer 	idedma_ctl = 0;
    218        1.1    bouyer 
    219        1.1    bouyer 	/* Per drive settings */
    220        1.1    bouyer 	for (drive = 0; drive < 2; drive++) {
    221        1.1    bouyer 		drvp = &chp->ch_drive[drive];
    222        1.1    bouyer 		/* If no drive, skip */
    223        1.1    bouyer 		if ((drvp->drive_flags & DRIVE) == 0)
    224        1.1    bouyer 			continue;
    225        1.1    bouyer 		/* add timing values, setup DMA if needed */
    226       1.13   thorpej 		if ((atac->atac_cap & ATAC_CAP_UDMA) &&
    227        1.1    bouyer 		    (drvp->drive_flags & DRIVE_UDMA)) {
    228        1.1    bouyer 			/* use Ultra/DMA */
    229        1.1    bouyer 			if (ACARD_IS_850(sc)) {
    230        1.1    bouyer 				idetime |= ATP850_SETTIME(drive,
    231        1.1    bouyer 				    acard_act_udma[drvp->UDMA_mode],
    232        1.1    bouyer 				    acard_rec_udma[drvp->UDMA_mode]);
    233        1.1    bouyer 				udma_mode |= ATP850_UDMA_MODE(channel, drive,
    234        1.1    bouyer 				    acard_udma_conf[drvp->UDMA_mode]);
    235        1.1    bouyer 			} else {
    236        1.1    bouyer 				idetime |= ATP860_SETTIME(channel, drive,
    237        1.1    bouyer 				    acard_act_udma[drvp->UDMA_mode],
    238        1.1    bouyer 				    acard_rec_udma[drvp->UDMA_mode]);
    239        1.1    bouyer 				udma_mode |= ATP860_UDMA_MODE(channel, drive,
    240        1.1    bouyer 				    acard_udma_conf[drvp->UDMA_mode]);
    241        1.1    bouyer 			}
    242        1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    243       1.13   thorpej 		} else if ((atac->atac_cap & ATAC_CAP_DMA) &&
    244        1.1    bouyer 		    (drvp->drive_flags & DRIVE_DMA)) {
    245        1.1    bouyer 			/* use Multiword DMA */
    246       1.14   thorpej 			s = splbio();
    247        1.1    bouyer 			drvp->drive_flags &= ~DRIVE_UDMA;
    248       1.14   thorpej 			splx(s);
    249        1.1    bouyer 			if (ACARD_IS_850(sc)) {
    250        1.1    bouyer 				idetime |= ATP850_SETTIME(drive,
    251        1.1    bouyer 				    acard_act_dma[drvp->DMA_mode],
    252        1.1    bouyer 				    acard_rec_dma[drvp->DMA_mode]);
    253        1.1    bouyer 			} else {
    254        1.1    bouyer 				idetime |= ATP860_SETTIME(channel, drive,
    255        1.1    bouyer 				    acard_act_dma[drvp->DMA_mode],
    256        1.1    bouyer 				    acard_rec_dma[drvp->DMA_mode]);
    257        1.1    bouyer 			}
    258        1.1    bouyer 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    259        1.1    bouyer 		} else {
    260        1.1    bouyer 			/* PIO only */
    261       1.14   thorpej 			s = splbio();
    262        1.1    bouyer 			drvp->drive_flags &= ~(DRIVE_UDMA | DRIVE_DMA);
    263       1.14   thorpej 			splx(s);
    264        1.1    bouyer 			if (ACARD_IS_850(sc)) {
    265        1.1    bouyer 				idetime |= ATP850_SETTIME(drive,
    266        1.1    bouyer 				    acard_act_pio[drvp->PIO_mode],
    267        1.1    bouyer 				    acard_rec_pio[drvp->PIO_mode]);
    268        1.1    bouyer 			} else {
    269        1.1    bouyer 				idetime |= ATP860_SETTIME(channel, drive,
    270        1.1    bouyer 				    acard_act_pio[drvp->PIO_mode],
    271        1.1    bouyer 				    acard_rec_pio[drvp->PIO_mode]);
    272        1.1    bouyer 			}
    273        1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, ATP8x0_CTRL,
    274        1.1    bouyer 		    pci_conf_read(sc->sc_pc, sc->sc_tag, ATP8x0_CTRL)
    275        1.1    bouyer 		    | ATP8x0_CTRL_EN(channel));
    276        1.1    bouyer 		}
    277        1.1    bouyer 	}
    278        1.1    bouyer 
    279        1.1    bouyer 	if (idedma_ctl != 0) {
    280        1.1    bouyer 		/* Add software bits in status register */
    281        1.6      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    282        1.6      fvdl 		    idedma_ctl);
    283        1.1    bouyer 	}
    284        1.1    bouyer 
    285        1.1    bouyer 	if (ACARD_IS_850(sc)) {
    286        1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    287        1.1    bouyer 		    ATP850_IDETIME(channel), idetime);
    288        1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, ATP850_UDMA, udma_mode);
    289        1.1    bouyer 	} else {
    290        1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, ATP860_IDETIME, idetime);
    291        1.1    bouyer 		pci_conf_write(sc->sc_pc, sc->sc_tag, ATP860_UDMA, udma_mode);
    292        1.1    bouyer 	}
    293        1.1    bouyer }
    294        1.1    bouyer 
    295        1.2   thorpej #if 0 /* XXX !! */
    296        1.2   thorpej static int
    297        1.2   thorpej acard_pci_intr(void *arg)
    298        1.1    bouyer {
    299        1.1    bouyer 	struct pciide_softc *sc = arg;
    300        1.1    bouyer 	struct pciide_channel *cp;
    301       1.11   thorpej 	struct ata_channel *wdc_cp;
    302        1.1    bouyer 	int rv = 0;
    303        1.1    bouyer 	int dmastat, i, crv;
    304        1.1    bouyer 
    305       1.13   thorpej 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    306        1.6      fvdl 		cp = &sc->pciide_channels[i];
    307        1.6      fvdl 		dmastat = bus_space_read_1(sc->sc_dma_iot,
    308        1.6      fvdl 		    cp->dma_iohs[IDEDMA_CTL], 0);
    309        1.1    bouyer 		if ((dmastat & IDEDMA_CTL_INTR) == 0)
    310        1.1    bouyer 			continue;
    311       1.11   thorpej 		wdc_cp = &cp->ata_channel;
    312       1.11   thorpej 		if ((wdc_cp->ch_flags & ATACH_IRQ_WAIT) == 0) {
    313        1.1    bouyer 			(void)wdcintr(wdc_cp);
    314        1.6      fvdl 			bus_space_write_1(sc->sc_dma_iot,
    315        1.6      fvdl 			    cp->dma_iohs[IDEDMA_CTL], 0, dmastat);
    316        1.1    bouyer 			continue;
    317        1.1    bouyer 		}
    318        1.1    bouyer 		crv = wdcintr(wdc_cp);
    319        1.9    bouyer 		if (crv == 0) {
    320        1.1    bouyer 			printf("%s:%d: bogus intr\n",
    321       1.22      cube 			    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), i);
    322        1.9    bouyer 			bus_space_write_1(sc->sc_dma_iot,
    323        1.9    bouyer 			    cp->dma_iohs[IDEDMA_CTL], 0, dmastat);
    324        1.9    bouyer 		} else if (crv == 1)
    325        1.1    bouyer 			rv = 1;
    326        1.1    bouyer 		else if (rv == 0)
    327        1.1    bouyer 			rv = crv;
    328        1.1    bouyer 	}
    329        1.1    bouyer 	return rv;
    330        1.1    bouyer }
    331        1.2   thorpej #endif
    332