Home | History | Annotate | Line # | Download | only in cardbus
if_atw_cardbus.c revision 1.32
      1 /* $NetBSD: if_atw_cardbus.c,v 1.32 2010/02/25 23:40:39 dyoung Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.  This code was adapted for the ADMtek ADM8211
     10  * by David Young.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * CardBus bus front-end for the ADMtek ADM8211 802.11 MAC/BBP driver.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: if_atw_cardbus.c,v 1.32 2010/02/25 23:40:39 dyoung Exp $");
     40 
     41 #include "opt_inet.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/malloc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/socket.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/errno.h>
     51 #include <sys/device.h>
     52 
     53 #include <machine/endian.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_media.h>
     58 #include <net/if_ether.h>
     59 
     60 #include <net80211/ieee80211_netbsd.h>
     61 #include <net80211/ieee80211_radiotap.h>
     62 #include <net80211/ieee80211_var.h>
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/if_inarp.h>
     67 #endif
     68 
     69 
     70 #include <sys/bus.h>
     71 #include <sys/intr.h>
     72 
     73 #include <dev/ic/atwreg.h>
     74 #include <dev/ic/rf3000reg.h>
     75 #include <dev/ic/si4136reg.h>
     76 #include <dev/ic/atwvar.h>
     77 
     78 #include <dev/pci/pcivar.h>
     79 #include <dev/pci/pcireg.h>
     80 #include <dev/pci/pcidevs.h>
     81 
     82 #include <dev/cardbus/cardbusvar.h>
     83 #include <dev/pci/pcidevs.h>
     84 
     85 /*
     86  * PCI configuration space registers used by the ADM8211.
     87  */
     88 #define	ATW_PCI_IOBA		0x10	/* i/o mapped base */
     89 #define	ATW_PCI_MMBA		0x14	/* memory mapped base */
     90 
     91 struct atw_cardbus_softc {
     92 	struct atw_softc sc_atw;
     93 
     94 	/* CardBus-specific goo. */
     95 	void			*sc_ih;		/* interrupt handle */
     96 	cardbus_devfunc_t	sc_ct;		/* our CardBus devfuncs */
     97 	pcitag_t		sc_tag;		/* our CardBus tag */
     98 	pcireg_t		sc_csr;		/* CSR bits */
     99 	bus_size_t		sc_mapsize;	/* the size of mapped bus space
    100 						 * region
    101 						 */
    102 
    103 	int			sc_bar_reg;	/* which BAR to use */
    104 	pcireg_t		sc_bar_val;	/* value of the BAR */
    105 
    106 	cardbus_intr_line_t sc_intrline; /* interrupt line */
    107 };
    108 
    109 static int	atw_cardbus_match(device_t, cfdata_t, void *);
    110 static void	atw_cardbus_attach(device_t, device_t, void *);
    111 static int	atw_cardbus_detach(device_t, int);
    112 
    113 CFATTACH_DECL3_NEW(atw_cardbus, sizeof(struct atw_cardbus_softc),
    114     atw_cardbus_match, atw_cardbus_attach, atw_cardbus_detach, atw_activate,
    115     NULL, NULL, DVF_DETACH_SHUTDOWN);
    116 
    117 static void	atw_cardbus_setup(struct atw_cardbus_softc *);
    118 
    119 static bool	atw_cardbus_suspend(device_t, const pmf_qual_t *);
    120 static bool	atw_cardbus_resume(device_t, const pmf_qual_t *);
    121 
    122 static const struct atw_cardbus_product *atw_cardbus_lookup
    123    (const struct cardbus_attach_args *);
    124 
    125 static const struct atw_cardbus_product {
    126 	u_int32_t	 acp_vendor;	/* PCI vendor ID */
    127 	u_int32_t	 acp_product;	/* PCI product ID */
    128 	const char	*acp_product_name;
    129 } atw_cardbus_products[] = {
    130 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_ADM8211,
    131 	  "ADMtek ADM8211 802.11 MAC/BBP" },
    132 
    133 	{ 0,				0,	NULL },
    134 };
    135 
    136 static const struct atw_cardbus_product *
    137 atw_cardbus_lookup(const struct cardbus_attach_args *ca)
    138 {
    139 	const struct atw_cardbus_product *acp;
    140 
    141 	for (acp = atw_cardbus_products; acp->acp_product_name != NULL; acp++) {
    142 		if (PCI_VENDOR(ca->ca_id) == acp->acp_vendor &&
    143 		    PCI_PRODUCT(ca->ca_id) == acp->acp_product)
    144 			return acp;
    145 	}
    146 	return NULL;
    147 }
    148 
    149 static int
    150 atw_cardbus_match(device_t parent, cfdata_t match, void *aux)
    151 {
    152 	struct cardbus_attach_args *ca = aux;
    153 
    154 	if (atw_cardbus_lookup(ca) != NULL)
    155 		return 1;
    156 
    157 	return 0;
    158 }
    159 
    160 static void
    161 atw_cardbus_attach(device_t parent, device_t self, void *aux)
    162 {
    163 	struct atw_cardbus_softc *csc = device_private(self);
    164 	struct atw_softc *sc = &csc->sc_atw;
    165 	struct cardbus_attach_args *ca = aux;
    166 	cardbus_devfunc_t ct = ca->ca_ct;
    167 	const struct atw_cardbus_product *acp;
    168 #if 0
    169 	int i;
    170 #define	FUNCREG(__x)	{#__x, (__x)}
    171 	struct {
    172 		const char *name;
    173 		bus_size_t ofs;
    174 	} funcregs[] = {
    175 		FUNCREG(ATW_FER), FUNCREG(ATW_FEMR), FUNCREG(ATW_FPSR),
    176 		FUNCREG(ATW_FFER)
    177 	};
    178 #undef FUNCREG
    179 #endif
    180 	bus_addr_t adr;
    181 
    182 	sc->sc_dev = self;
    183 	sc->sc_dmat = ca->ca_dmat;
    184 	csc->sc_ct = ct;
    185 	csc->sc_tag = ca->ca_tag;
    186 
    187 	acp = atw_cardbus_lookup(ca);
    188 	if (acp == NULL) {
    189 		printf("\n");
    190 		panic("atw_cardbus_attach: impossible");
    191 	}
    192 
    193 	/* Get revision info. */
    194 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    195 
    196 	printf(": %s, revision %d.%d\n", acp->acp_product_name,
    197 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    198 
    199 #if 0
    200 	printf("%s: signature %08x\n", device_xname(self),
    201 	    (rev >> 4) & 0xf, rev & 0xf,
    202 	    Cardbus_conf_read(ct, csc->sc_tag, 0x80));
    203 #endif
    204 
    205 	/*
    206 	 * Map the device.
    207 	 */
    208 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE |
    209 	              PCI_COMMAND_PARITY_ENABLE |
    210 		      PCI_COMMAND_SERR_ENABLE;
    211 	if (Cardbus_mapreg_map(ct, ATW_PCI_MMBA,
    212 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
    213 	    &csc->sc_mapsize) == 0) {
    214 #if 0
    215 		printf("%s: atw_cardbus_attach mapped %d bytes mem space\n",
    216 		    device_xname(self), csc->sc_mapsize);
    217 #endif
    218 #if rbus
    219 #else
    220 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    221 #endif
    222 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    223 		csc->sc_bar_reg = ATW_PCI_MMBA;
    224 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    225 	} else if (Cardbus_mapreg_map(ct, ATW_PCI_IOBA,
    226 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
    227 	    &csc->sc_mapsize) == 0) {
    228 #if 0
    229 		printf("%s: atw_cardbus_attach mapped %d bytes I/O space\n",
    230 		    device_xname(self), csc->sc_mapsize);
    231 #endif
    232 #if rbus
    233 #else
    234 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    235 #endif
    236 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    237 		csc->sc_bar_reg = ATW_PCI_IOBA;
    238 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
    239 	} else {
    240 		aprint_error_dev(self, "unable to map device registers\n");
    241 		return;
    242 	}
    243 
    244 	/*
    245 	 * Bring the chip out of powersave mode and initialize the
    246 	 * configuration registers.
    247 	 */
    248 	atw_cardbus_setup(csc);
    249 
    250 	/* Remember which interrupt line. */
    251 	csc->sc_intrline = ca->ca_intrline;
    252 
    253 #if 0
    254 	/*
    255 	 * The CardBus cards will make it to store-and-forward mode as
    256 	 * soon as you put them under any kind of load, so just start
    257 	 * out there.
    258 	 */
    259 	sc->sc_txthresh = 3; /* TBD name constant */
    260 #endif
    261 
    262 #if 0
    263 	for (i = 0; i < __arraycount(funcregs); i++) {
    264 		aprint_error_dev(sc->sc_dev, "%s %" PRIx32 "\n",
    265 		    funcregs[i].name, ATW_READ(sc, funcregs[i].ofs));
    266 	}
    267 #endif
    268 
    269 	ATW_WRITE(sc, ATW_FEMR, 0);
    270 	ATW_WRITE(sc, ATW_FER, ATW_READ(sc, ATW_FER));
    271 
    272 	/*
    273 	 * Bus-independent attach.
    274 	 */
    275 	atw_attach(sc);
    276 
    277 	if (pmf_device_register1(sc->sc_dev, atw_cardbus_suspend,
    278 	    atw_cardbus_resume, atw_shutdown))
    279 		pmf_class_network_register(sc->sc_dev, &sc->sc_if);
    280 	else
    281 		aprint_error_dev(sc->sc_dev,
    282 		    "couldn't establish power handler\n");
    283 
    284 	/*
    285 	 * Power down the socket.
    286 	 */
    287 	pmf_device_suspend(sc->sc_dev, &sc->sc_qual);
    288 }
    289 
    290 static int
    291 atw_cardbus_detach(device_t self, int flags)
    292 {
    293 	struct atw_cardbus_softc *csc = device_private(self);
    294 	struct atw_softc *sc = &csc->sc_atw;
    295 	struct cardbus_devfunc *ct = csc->sc_ct;
    296 	int rv;
    297 
    298 #if defined(DIAGNOSTIC)
    299 	if (ct == NULL)
    300 		panic("%s: data structure lacks", device_xname(self));
    301 #endif
    302 
    303 	rv = atw_detach(sc);
    304 	if (rv != 0)
    305 		return rv;
    306 
    307 	/*
    308 	 * Unhook the interrupt handler.
    309 	 */
    310 	if (csc->sc_ih != NULL)
    311 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    312 
    313 	/*
    314 	 * Release bus space and close window.
    315 	 */
    316 	if (csc->sc_bar_reg != 0)
    317 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    318 		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
    319 
    320 	return 0;
    321 }
    322 
    323 static bool
    324 atw_cardbus_resume(device_t self, const pmf_qual_t *qual)
    325 {
    326 	struct atw_cardbus_softc *csc = device_private(self);
    327 	struct atw_softc *sc = &csc->sc_atw;
    328 	cardbus_devfunc_t ct = csc->sc_ct;
    329 	cardbus_chipset_tag_t cc = ct->ct_cc;
    330 	cardbus_function_tag_t cf = ct->ct_cf;
    331 
    332 	/*
    333 	 * Map and establish the interrupt.
    334 	 */
    335 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    336 	    atw_intr, sc);
    337 	if (csc->sc_ih == NULL) {
    338 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    339 		return false;
    340 	}
    341 
    342 	return true;
    343 }
    344 
    345 static bool
    346 atw_cardbus_suspend(device_t self, const pmf_qual_t *qual)
    347 {
    348 	struct atw_cardbus_softc *csc = device_private(self);
    349 	cardbus_devfunc_t ct = csc->sc_ct;
    350 	cardbus_chipset_tag_t cc = ct->ct_cc;
    351 	cardbus_function_tag_t cf = ct->ct_cf;
    352 
    353 	/* Unhook the interrupt handler. */
    354 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    355 	csc->sc_ih = NULL;
    356 
    357 	return atw_suspend(self, qual);
    358 }
    359 
    360 static void
    361 atw_cardbus_setup(struct atw_cardbus_softc *csc)
    362 {
    363 	cardbus_devfunc_t ct = csc->sc_ct;
    364 	pcireg_t csr;
    365 	int rc;
    366 
    367 	if ((rc = cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0)) != 0)
    368 		aprint_debug("%s: cardbus_set_powerstate %d\n", __func__, rc);
    369 
    370 	/* Program the BAR. */
    371 	Cardbus_conf_write(ct, csc->sc_tag, csc->sc_bar_reg,
    372 	    csc->sc_bar_val);
    373 
    374 	/* Enable the appropriate bits in the PCI CSR. */
    375 	csr = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    376 	csr &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    377 	csr |= csc->sc_csr;
    378 	Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
    379 }
    380