Home | History | Annotate | Line # | Download | only in cardbus
siisata_cardbus.c revision 1.9
      1 /* $NetBSD: siisata_cardbus.c,v 1.9 2010/02/25 22:40:16 dyoung Exp $ */
      2 /* Id: siisata_pci.c,v 1.11 2008/05/21 16:20:11 jakllsch Exp  */
      3 
      4 /*
      5  * Copyright (c) 2006 Manuel Bouyer.
      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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 /*
     30  * Copyright (c) 2007, 2008 Jonathan A. Kollasch.
     31  * All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  *
     42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     43  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     45  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     46  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     47  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     48  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     49  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     51  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     52  */
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: siisata_cardbus.c,v 1.9 2010/02/25 22:40:16 dyoung Exp $");
     56 
     57 #include <sys/types.h>
     58 #include <sys/malloc.h>
     59 #include <sys/param.h>
     60 #include <sys/kernel.h>
     61 #include <sys/systm.h>
     62 
     63 #include <uvm/uvm_extern.h>
     64 
     65 #include <dev/cardbus/cardbusvar.h>
     66 #include <dev/pci/pcidevs.h>
     67 #include <dev/ic/siisatavar.h>
     68 
     69 #define cardbus_devinfo		pci_devinfo
     70 
     71 struct siisata_cardbus_softc {
     72 	struct siisata_softc si_sc;
     73 	cardbus_chipset_tag_t sc_cc;
     74 	cardbus_function_tag_t sc_cf;
     75 	cardbus_devfunc_t sc_ct;
     76 	pcitag_t sc_tag;
     77 	bus_space_tag_t sc_iot;		/* CardBus I/O space tag */
     78 	bus_space_tag_t sc_memt;	/* CardBus MEM space tag */
     79 #if rbus
     80 	rbus_tag_t sc_rbus_iot;		/* CardBus i/o rbus tag */
     81 	rbus_tag_t sc_rbus_memt;	/* CardBus mem rbus tag */
     82 #endif
     83 
     84 	bus_size_t sc_grsize;
     85 	bus_size_t sc_prsize;
     86 	void *sc_ih;
     87 };
     88 
     89 static int siisata_cardbus_match(device_t, cfdata_t, void *);
     90 static void siisata_cardbus_attach(device_t, device_t, void *);
     91 static int siisata_cardbus_detach(device_t, int);
     92 static bool siisata_cardbus_resume(device_t, const pmf_qual_t *);
     93 
     94 static const struct siisata_cardbus_product {
     95 	pci_vendor_id_t scp_vendor;
     96 	pci_product_id_t scp_product;
     97 	int scp_ports;
     98 	int scp_chip;
     99 
    100 } siisata_cardbus_products[] = {
    101 	{
    102 		PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3124,
    103 		4, 3124
    104 	},
    105 	{
    106 		0, 0,
    107 		0, 0
    108 	},
    109 };
    110 
    111 CFATTACH_DECL_NEW(siisata_cardbus, sizeof(struct siisata_cardbus_softc),
    112     siisata_cardbus_match, siisata_cardbus_attach, siisata_cardbus_detach,
    113     NULL);
    114 
    115 static const struct siisata_cardbus_product *
    116 siisata_cardbus_lookup(const struct cardbus_attach_args *ca)
    117 {
    118 	const struct siisata_cardbus_product *scp;
    119 
    120 	for (scp = siisata_cardbus_products; scp->scp_ports > 0; scp++) {
    121 		if (CARDBUS_VENDOR(ca->ca_id) == scp->scp_vendor &&
    122 		    CARDBUS_PRODUCT(ca->ca_id) == scp->scp_product)
    123 			return scp;
    124 	}
    125 	return NULL;
    126 }
    127 
    128 static int
    129 siisata_cardbus_match(device_t parent, cfdata_t match, void *aux)
    130 {
    131 	struct cardbus_attach_args *ca = aux;
    132 
    133 	if (siisata_cardbus_lookup(ca) != NULL)
    134 		return 3;
    135 
    136 	return 0;
    137 }
    138 
    139 static void
    140 siisata_cardbus_attach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct cardbus_attach_args *ca = aux;
    143 	struct siisata_cardbus_softc *csc = device_private(self);
    144 	struct siisata_softc *sc = &csc->si_sc;
    145 	cardbus_devfunc_t ct = ca->ca_ct;
    146 	cardbus_chipset_tag_t cc = ct->ct_cc;
    147 	cardbus_function_tag_t cf = ct->ct_cf;
    148 	pcireg_t csr;
    149 	const struct siisata_cardbus_product *scp;
    150 	bus_space_tag_t memt;
    151 	bus_space_handle_t memh;
    152 	bus_addr_t base;
    153 	bus_size_t grsize, prsize;
    154 	uint32_t gcreg;
    155 	char devinfo[256];
    156 
    157 	sc->sc_atac.atac_dev = self;
    158 
    159 	csc->sc_cc = cc;
    160 	csc->sc_cf = cf;
    161 	csc->sc_ct = ct;
    162 	csc->sc_tag = ca->ca_tag;
    163 
    164 	csc->sc_iot = ca->ca_iot;
    165 	csc->sc_memt = ca->ca_memt;
    166 #if rbus
    167 	csc->sc_rbus_iot = ca->ca_rbus_iot;
    168 	csc->sc_rbus_memt = ca->ca_rbus_memt;
    169 #endif
    170 
    171 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    172 	aprint_naive(": SATA-II HBA\n");
    173 	aprint_normal(": %s\n", devinfo);
    174 
    175 	/*
    176 	 * XXXX
    177 	 * Our BAR0/BAR1 type is 64bit Memory.  Cardbus_mapreg_map() don't
    178 	 * support 64bit Memory.  We map ourself...
    179 	 */
    180 	/* map bar0 */
    181 	{
    182 #define SIISATA_BAR0_SIZE	128
    183 		grsize = SIISATA_BAR0_SIZE;
    184 		csr =
    185 		    cardbus_conf_read(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR0);
    186 		base = PCI_MAPREG_MEM_ADDR(csr);
    187 		memt = csc->sc_memt;
    188 		if ((*cf->cardbus_space_alloc)(cc, csc->sc_rbus_memt, base,
    189 		    grsize, grsize - 1, grsize, 0, &base, &memh)) {
    190 			aprint_error(
    191 			    "%s: unable to map device global registers\n",
    192 			    SIISATANAME(sc));
    193 			return;
    194 		}
    195 		cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR0,
    196 		    base);
    197 	}
    198 	sc->sc_grt = memt;
    199 	sc->sc_grh = memh;
    200 	csc->sc_grsize = grsize;
    201 
    202 	/* map bar1 */
    203 	{
    204 #define SIISATA_BAR1_SIZE	(32 * 1024)
    205 		prsize = SIISATA_BAR1_SIZE;
    206 		base = PCI_MAPREG_MEM_ADDR(cardbus_conf_read(cc, cf, ca->ca_tag,
    207 		    SIISATA_CARDBUS_BAR1));
    208 		memt = csc->sc_memt;
    209 		if ((*cf->cardbus_space_alloc)(cc, csc->sc_rbus_memt, base,
    210 		    prsize, prsize - 1, prsize, 0, &base, &memh)) {
    211 			cardbus_conf_write(cc, cf, ca->ca_tag,
    212 			    SIISATA_CARDBUS_BAR0, 0);
    213 			(*cf->cardbus_space_free)(cc, csc->sc_rbus_memt,
    214 			    sc->sc_grh, grsize);
    215 			aprint_error(
    216 			    "%s: unable to map device port registers\n",
    217 			    SIISATANAME(sc));
    218 			return;
    219 		}
    220 		cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR1,
    221 		    base);
    222 	}
    223 	sc->sc_prt = memt;
    224 	sc->sc_prh = memh;
    225 	csc->sc_prsize = prsize;
    226 
    227 	sc->sc_dmat = ca->ca_dmat;
    228 
    229 	/* map interrupt */
    230 	csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
    231 	    siisata_intr, sc);
    232 	if (csc->sc_ih == NULL) {
    233 		cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR0, 0);
    234 		(*cf->cardbus_space_free)(cc, csc->sc_rbus_memt, sc->sc_grh,
    235 		    grsize);
    236 		cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR1, 0);
    237 		(*cf->cardbus_space_free)(cc, csc->sc_rbus_memt, sc->sc_prh,
    238 		    prsize);
    239 		aprint_error("%s: couldn't establish interrupt\n",
    240 		    SIISATANAME(sc));
    241 		return;
    242 	}
    243 
    244 	/* fill in number of ports on this device */
    245 	scp = siisata_cardbus_lookup(ca);
    246 	if (scp != NULL)
    247 		sc->sc_atac.atac_nchannels = scp->scp_ports;
    248 	else
    249 		/* _match() should prevent us from getting here */
    250 		panic("siisata: the universe might be falling apart!\n");
    251 
    252 	/* enable bus mastering in case the firmware didn't */
    253 	csr = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG);
    254 	csr |= CARDBUS_COMMAND_MASTER_ENABLE;
    255 	csr |= CARDBUS_COMMAND_MEM_ENABLE;
    256 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG, csr);
    257 
    258 	gcreg = GRREAD(sc, GR_GC);
    259 
    260 	/* CardBus supports only 32-bit 33MHz */
    261 	KASSERT(!(gcreg &
    262 	    (GR_GC_REQ64|GR_GC_DEVSEL|GR_GC_STOP|GR_GC_TRDY|GR_GC_M66EN)));
    263 
    264 	aprint_normal("%s: SiI%d on 32-bit, 33MHz PCI (CardBus).",
    265 	    SIISATANAME(sc), scp->scp_chip);
    266 	if (gcreg & GR_GC_3GBPS)
    267 		aprint_normal(" 3.0Gb/s capable.\n");
    268 	else
    269 		aprint_normal("\n");
    270 
    271 	siisata_attach(sc);
    272 
    273 	if (!pmf_device_register(self, NULL, siisata_cardbus_resume))
    274 		aprint_error_dev(self, "couldn't establish power handler\n");
    275 }
    276 
    277 static int
    278 siisata_cardbus_detach(device_t self, int flags)
    279 {
    280 	struct siisata_cardbus_softc *csc = device_private(self);
    281 	struct siisata_softc *sc = &csc->si_sc;
    282 	struct cardbus_devfunc *ct = csc->sc_ct;
    283 	cardbus_chipset_tag_t cc = ct->ct_cc;
    284 	cardbus_function_tag_t cf = ct->ct_cf;
    285 	pcitag_t ctag = csc->sc_tag;
    286 	int rv;
    287 
    288 	rv = siisata_detach(sc, flags);
    289 	if (rv)
    290 		return (rv);
    291 	if (csc->sc_ih != NULL) {
    292 		cardbus_intr_disestablish(csc->sc_cc, csc->sc_cf, csc->sc_ih);
    293 		csc->sc_ih = NULL;
    294 	}
    295 	if (csc->sc_grsize) {
    296 		cardbus_conf_write(cc, cf, ctag, SIISATA_CARDBUS_BAR0, 0);
    297 		(*cf->cardbus_space_free)(cc, csc->sc_rbus_memt, sc->sc_grh,
    298 		    csc->sc_grsize);
    299 		csc->sc_grsize = 0;
    300 	}
    301 	if (csc->sc_prsize) {
    302 		cardbus_conf_write(cc, cf, ctag, SIISATA_CARDBUS_BAR1, 0);
    303 		(*cf->cardbus_space_free)(cc, csc->sc_rbus_memt, sc->sc_prh,
    304 		    csc->sc_prsize);
    305 		csc->sc_prsize = 0;
    306 	}
    307 	return 0;
    308 }
    309 
    310 static bool
    311 siisata_cardbus_resume(device_t dv, const pmf_qual_t *qual)
    312 {
    313 	struct siisata_cardbus_softc *csc = device_private(dv);
    314 	struct siisata_softc *sc = &csc->si_sc;
    315 	int s;
    316 
    317 	s = splbio();
    318 	siisata_resume(sc);
    319 	splx(s);
    320 
    321 	return true;
    322 }
    323