Home | History | Annotate | Line # | Download | only in cardbus
if_atw_cardbus.c revision 1.21
      1 /* $NetBSD: if_atw_cardbus.c,v 1.21 2008/04/06 07:54:17 cegger 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  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the NetBSD
     23  *	Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * CardBus bus front-end for the ADMtek ADM8211 802.11 MAC/BBP driver.
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: if_atw_cardbus.c,v 1.21 2008/04/06 07:54:17 cegger Exp $");
     47 
     48 #include "opt_inet.h"
     49 #include "bpfilter.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/malloc.h>
     55 #include <sys/kernel.h>
     56 #include <sys/socket.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/errno.h>
     59 #include <sys/device.h>
     60 
     61 #include <machine/endian.h>
     62 
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_media.h>
     66 #include <net/if_ether.h>
     67 
     68 #include <net80211/ieee80211_netbsd.h>
     69 #include <net80211/ieee80211_radiotap.h>
     70 #include <net80211/ieee80211_var.h>
     71 
     72 #if NBPFILTER > 0
     73 #include <net/bpf.h>
     74 #endif
     75 
     76 #ifdef INET
     77 #include <netinet/in.h>
     78 #include <netinet/if_inarp.h>
     79 #endif
     80 
     81 
     82 #include <sys/bus.h>
     83 #include <sys/intr.h>
     84 
     85 #include <dev/ic/atwreg.h>
     86 #include <dev/ic/rf3000reg.h>
     87 #include <dev/ic/si4136reg.h>
     88 #include <dev/ic/atwvar.h>
     89 
     90 #include <dev/pci/pcivar.h>
     91 #include <dev/pci/pcireg.h>
     92 #include <dev/pci/pcidevs.h>
     93 
     94 #include <dev/cardbus/cardbusvar.h>
     95 #include <dev/pci/pcidevs.h>
     96 
     97 /*
     98  * PCI configuration space registers used by the ADM8211.
     99  */
    100 #define	ATW_PCI_IOBA		0x10	/* i/o mapped base */
    101 #define	ATW_PCI_MMBA		0x14	/* memory mapped base */
    102 
    103 struct atw_cardbus_softc {
    104 	struct atw_softc sc_atw;	/* real ADM8211 softc */
    105 
    106 	/* CardBus-specific goo. */
    107 	void	*sc_ih;			/* interrupt handle */
    108 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    109 	cardbustag_t sc_tag;		/* our CardBus tag */
    110 	int	sc_csr;			/* CSR bits */
    111 	bus_size_t sc_mapsize;		/* the size of mapped bus space
    112 					   region */
    113 
    114 	int	sc_cben;		/* CardBus enables */
    115 	int	sc_bar_reg;		/* which BAR to use */
    116 	pcireg_t sc_bar_val;		/* value of the BAR */
    117 
    118 	int	sc_intrline;		/* interrupt line */
    119 };
    120 
    121 int	atw_cardbus_match(struct device *, struct cfdata *, void *);
    122 void	atw_cardbus_attach(struct device *, struct device *, void *);
    123 int	atw_cardbus_detach(struct device *, int);
    124 
    125 CFATTACH_DECL(atw_cardbus, sizeof(struct atw_cardbus_softc),
    126     atw_cardbus_match, atw_cardbus_attach, atw_cardbus_detach, atw_activate);
    127 
    128 void	atw_cardbus_setup(struct atw_cardbus_softc *);
    129 
    130 int	atw_cardbus_enable(struct atw_softc *);
    131 void	atw_cardbus_disable(struct atw_softc *);
    132 
    133 static void atw_cardbus_intr_ack(struct atw_softc *);
    134 
    135 const struct atw_cardbus_product *atw_cardbus_lookup
    136    (const struct cardbus_attach_args *);
    137 
    138 const struct atw_cardbus_product {
    139 	u_int32_t	 acp_vendor;	/* PCI vendor ID */
    140 	u_int32_t	 acp_product;	/* PCI product ID */
    141 	const char	*acp_product_name;
    142 } atw_cardbus_products[] = {
    143 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_ADM8211,
    144 	  "ADMtek ADM8211 802.11 MAC/BBP" },
    145 
    146 	{ 0,				0,	NULL },
    147 };
    148 
    149 const struct atw_cardbus_product *
    150 atw_cardbus_lookup(const struct cardbus_attach_args *ca)
    151 {
    152 	const struct atw_cardbus_product *acp;
    153 
    154 	for (acp = atw_cardbus_products;
    155 	     acp->acp_product_name != NULL;
    156 	     acp++) {
    157 		if (PCI_VENDOR(ca->ca_id) == acp->acp_vendor &&
    158 		    PCI_PRODUCT(ca->ca_id) == acp->acp_product)
    159 			return (acp);
    160 	}
    161 	return (NULL);
    162 }
    163 
    164 int
    165 atw_cardbus_match(struct device *parent, struct cfdata *match,
    166     void *aux)
    167 {
    168 	struct cardbus_attach_args *ca = aux;
    169 
    170 	if (atw_cardbus_lookup(ca) != NULL)
    171 		return (1);
    172 
    173 	return (0);
    174 }
    175 
    176 void
    177 atw_cardbus_attach(struct device *parent, struct device *self,
    178     void *aux)
    179 {
    180 	struct atw_cardbus_softc *csc = device_private(self);
    181 	struct atw_softc *sc = &csc->sc_atw;
    182 	struct cardbus_attach_args *ca = aux;
    183 	cardbus_devfunc_t ct = ca->ca_ct;
    184 	const struct atw_cardbus_product *acp;
    185 	bus_addr_t adr;
    186 
    187 	sc->sc_dmat = ca->ca_dmat;
    188 	csc->sc_ct = ct;
    189 	csc->sc_tag = ca->ca_tag;
    190 
    191 	acp = atw_cardbus_lookup(ca);
    192 	if (acp == NULL) {
    193 		printf("\n");
    194 		panic("atw_cardbus_attach: impossible");
    195 	}
    196 
    197 	/*
    198 	 * Power management hooks.
    199 	 */
    200 	sc->sc_enable = atw_cardbus_enable;
    201 	sc->sc_disable = atw_cardbus_disable;
    202 
    203 	sc->sc_intr_ack = atw_cardbus_intr_ack;
    204 
    205 	/* Get revision info. */
    206 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    207 
    208 	printf(": %s, revision %d.%d\n", acp->acp_product_name,
    209 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    210 
    211 #if 0
    212 	printf("%s: signature %08x\n", device_xname(&sc->sc_dev),
    213 	    (rev >> 4) & 0xf, rev & 0xf,
    214 	    cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag, 0x80));
    215 #endif
    216 
    217 	/*
    218 	 * Map the device.
    219 	 */
    220 	csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
    221 	if (Cardbus_mapreg_map(ct, ATW_PCI_MMBA,
    222 	    CARDBUS_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
    223 	    &csc->sc_mapsize) == 0) {
    224 #if 0
    225 		printf("%s: atw_cardbus_attach mapped %d bytes mem space\n",
    226 		    device_xname(&sc->sc_dev), csc->sc_mapsize);
    227 #endif
    228 #if rbus
    229 #else
    230 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    231 #endif
    232 		csc->sc_cben = CARDBUS_MEM_ENABLE;
    233 		csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
    234 		csc->sc_bar_reg = ATW_PCI_MMBA;
    235 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    236 	} else if (Cardbus_mapreg_map(ct, ATW_PCI_IOBA,
    237 	    CARDBUS_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
    238 	    &csc->sc_mapsize) == 0) {
    239 #if 0
    240 		printf("%s: atw_cardbus_attach mapped %d bytes I/O space\n",
    241 		    device_xname(&sc->sc_dev), csc->sc_mapsize);
    242 #endif
    243 #if rbus
    244 #else
    245 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    246 #endif
    247 		csc->sc_cben = CARDBUS_IO_ENABLE;
    248 		csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
    249 		csc->sc_bar_reg = ATW_PCI_IOBA;
    250 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
    251 	} else {
    252 		aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
    253 		return;
    254 	}
    255 
    256 	/*
    257 	 * Bring the chip out of powersave mode and initialize the
    258 	 * configuration registers.
    259 	 */
    260 	atw_cardbus_setup(csc);
    261 
    262 	/* Remember which interrupt line. */
    263 	csc->sc_intrline = ca->ca_intrline;
    264 
    265 	printf("%s: interrupting at %d\n", device_xname(&sc->sc_dev),
    266 	    csc->sc_intrline);
    267 #if 0
    268 	/*
    269 	 * The CardBus cards will make it to store-and-forward mode as
    270 	 * soon as you put them under any kind of load, so just start
    271 	 * out there.
    272 	 */
    273 	sc->sc_txthresh = 3; /* TBD name constant */
    274 #endif
    275 
    276 	/*
    277 	 * Finish off the attach.
    278 	 */
    279 	atw_attach(sc);
    280 
    281 	ATW_WRITE(sc, ATW_FER, ATW_FER_INTR);
    282 
    283 	/*
    284 	 * Power down the socket.
    285 	 */
    286 	Cardbus_function_disable(csc->sc_ct);
    287 }
    288 
    289 static void
    290 atw_cardbus_intr_ack(struct atw_softc *sc)
    291 {
    292 	ATW_WRITE(sc, ATW_FER, ATW_FER_INTR);
    293 }
    294 
    295 int
    296 atw_cardbus_detach(struct device *self, int flags)
    297 {
    298 	struct atw_cardbus_softc *csc = device_private(self);
    299 	struct atw_softc *sc = &csc->sc_atw;
    300 	struct cardbus_devfunc *ct = csc->sc_ct;
    301 	int rv;
    302 
    303 #if defined(DIAGNOSTIC)
    304 	if (ct == NULL)
    305 		panic("%s: data structure lacks", device_xname(&sc->sc_dev));
    306 #endif
    307 
    308 	rv = atw_detach(sc);
    309 	if (rv)
    310 		return (rv);
    311 
    312 	/*
    313 	 * Unhook the interrupt handler.
    314 	 */
    315 	if (csc->sc_ih != NULL)
    316 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    317 
    318 	/*
    319 	 * Release bus space and close window.
    320 	 */
    321 	if (csc->sc_bar_reg != 0)
    322 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    323 		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
    324 
    325 	return (0);
    326 }
    327 
    328 int
    329 atw_cardbus_enable(struct atw_softc *sc)
    330 {
    331 	struct atw_cardbus_softc *csc = (void *) sc;
    332 	cardbus_devfunc_t ct = csc->sc_ct;
    333 	cardbus_chipset_tag_t cc = ct->ct_cc;
    334 	cardbus_function_tag_t cf = ct->ct_cf;
    335 
    336 	/*
    337 	 * Power on the socket.
    338 	 */
    339 	Cardbus_function_enable(ct);
    340 
    341 	/*
    342 	 * Set up the PCI configuration registers.
    343 	 */
    344 	atw_cardbus_setup(csc);
    345 
    346 	/*
    347 	 * Map and establish the interrupt.
    348 	 */
    349 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    350 	    atw_intr, sc);
    351 	if (csc->sc_ih == NULL) {
    352 		aprint_error_dev(&sc->sc_dev, "unable to establish interrupt at %d\n",
    353 		    csc->sc_intrline);
    354 		Cardbus_function_disable(csc->sc_ct);
    355 		return (1);
    356 	}
    357 
    358 	return (0);
    359 }
    360 
    361 void
    362 atw_cardbus_disable(struct atw_softc *sc)
    363 {
    364 	struct atw_cardbus_softc *csc = (void *) sc;
    365 	cardbus_devfunc_t ct = csc->sc_ct;
    366 	cardbus_chipset_tag_t cc = ct->ct_cc;
    367 	cardbus_function_tag_t cf = ct->ct_cf;
    368 
    369 	/* Unhook the interrupt handler. */
    370 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    371 	csc->sc_ih = NULL;
    372 
    373 	/* Power down the socket. */
    374 	Cardbus_function_disable(ct);
    375 }
    376 
    377 void
    378 atw_cardbus_setup(struct atw_cardbus_softc *csc)
    379 {
    380 	cardbus_devfunc_t ct = csc->sc_ct;
    381 	cardbus_chipset_tag_t cc = ct->ct_cc;
    382 	cardbus_function_tag_t cf = ct->ct_cf;
    383 	pcireg_t reg;
    384 
    385 	(void)cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0);
    386 
    387 	/* Program the BAR. */
    388 	cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
    389 	    csc->sc_bar_val);
    390 
    391 	/* Make sure the right access type is on the CardBus bridge. */
    392 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
    393 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    394 
    395 	/* Enable the appropriate bits in the PCI CSR. */
    396 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    397 	    CARDBUS_COMMAND_STATUS_REG);
    398 	reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
    399 	reg |= csc->sc_csr;
    400 	cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
    401 	    reg);
    402 
    403 	/*
    404 	 * Make sure the latency timer is set to some reasonable
    405 	 * value.
    406 	 */
    407 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
    408 	if (CARDBUS_LATTIMER(reg) < 0x20) {
    409 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    410 		reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
    411 		cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
    412 	}
    413 }
    414