Home | History | Annotate | Line # | Download | only in cardbus
if_bwi_cardbus.c revision 1.2
      1  1.2  nakayama /*	$NetBSD: if_bwi_cardbus.c,v 1.2 2012/04/12 12:52:58 nakayama Exp $ */
      2  1.1  christos /*	$OpenBSD: if_bwi_cardbus.c,v 1.13 2010/08/06 05:26:24 mglocker Exp $ */
      3  1.1  christos 
      4  1.1  christos /*
      5  1.1  christos  * Copyright (c) 2007 Marcus Glocker <mglocker (at) openbsd.org>
      6  1.1  christos  * Copyright (c) 2006 Claudio Jeker <claudio (at) openbsd.org>
      7  1.1  christos  *
      8  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      9  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     10  1.1  christos  * copyright notice and this permission notice appear in all copies.
     11  1.1  christos  *
     12  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  1.1  christos  */
     20  1.1  christos 
     21  1.1  christos /*
     22  1.2  nakayama  * Broadcom AirForce BCM43xx IEEE 802.11b/g wireless network driver
     23  1.2  nakayama  * CardBus front end
     24  1.1  christos  */
     25  1.1  christos 
     26  1.1  christos #include <sys/cdefs.h>
     27  1.2  nakayama __KERNEL_RCSID(0, "$NetBSD: if_bwi_cardbus.c,v 1.2 2012/04/12 12:52:58 nakayama Exp $");
     28  1.1  christos 
     29  1.1  christos #include "opt_inet.h"
     30  1.1  christos 
     31  1.1  christos #include <sys/param.h>
     32  1.1  christos #include <sys/callout.h>
     33  1.1  christos #include <sys/device.h>
     34  1.1  christos #include <sys/kernel.h>
     35  1.1  christos #include <sys/malloc.h>
     36  1.1  christos #include <sys/mbuf.h>
     37  1.1  christos #include <sys/socket.h>
     38  1.1  christos #include <sys/sockio.h>
     39  1.1  christos #include <sys/systm.h>
     40  1.1  christos #include <sys/errno.h>
     41  1.1  christos 
     42  1.1  christos #include <machine/endian.h>
     43  1.1  christos 
     44  1.1  christos #include <net/if.h>
     45  1.1  christos #include <net/if_dl.h>
     46  1.1  christos #include <net/if_ether.h>
     47  1.1  christos #include <net/if_media.h>
     48  1.1  christos 
     49  1.1  christos #include <net80211/ieee80211_var.h>
     50  1.1  christos #include <net80211/ieee80211_amrr.h>
     51  1.1  christos #include <net80211/ieee80211_radiotap.h>
     52  1.1  christos 
     53  1.1  christos #include <dev/pci/pcireg.h>
     54  1.1  christos #include <dev/pci/pcivar.h>
     55  1.2  nakayama #include <dev/pci/pcidevs.h>
     56  1.1  christos 
     57  1.1  christos #include <dev/cardbus/cardbusvar.h>
     58  1.1  christos 
     59  1.1  christos #include <dev/ic/bwireg.h>
     60  1.1  christos #include <dev/ic/bwivar.h>
     61  1.1  christos 
     62  1.1  christos struct bwi_cardbus_softc {
     63  1.1  christos 	struct bwi_softc	 csc_bwi;
     64  1.1  christos 
     65  1.1  christos 	/* cardbus specific goo */
     66  1.1  christos 	cardbus_devfunc_t	 csc_ct;
     67  1.1  christos 	pcitag_t		 csc_tag;
     68  1.1  christos 
     69  1.1  christos 	bus_size_t		 csc_mapsize;
     70  1.1  christos 	pcireg_t		 csc_bar_val;
     71  1.1  christos };
     72  1.1  christos 
     73  1.2  nakayama static int	bwi_cardbus_match(device_t, cfdata_t, void *);
     74  1.2  nakayama static void	bwi_cardbus_attach(device_t, device_t, void *);
     75  1.2  nakayama static int	bwi_cardbus_detach(device_t, int);
     76  1.2  nakayama static void	bwi_cardbus_setup(struct bwi_cardbus_softc *);
     77  1.2  nakayama static int	bwi_cardbus_enable(struct bwi_softc *, int);
     78  1.2  nakayama static void	bwi_cardbus_disable(struct bwi_softc *, int);
     79  1.2  nakayama static void	bwi_cardbus_conf_write(void *, uint32_t, uint32_t);
     80  1.2  nakayama static uint32_t	bwi_cardbus_conf_read(void *, uint32_t);
     81  1.1  christos 
     82  1.1  christos CFATTACH_DECL_NEW(bwi_cardbus, sizeof (struct bwi_cardbus_softc),
     83  1.1  christos     bwi_cardbus_match, bwi_cardbus_attach, bwi_cardbus_detach, NULL);
     84  1.1  christos 
     85  1.2  nakayama static int
     86  1.1  christos bwi_cardbus_match(device_t parent, cfdata_t match, void *aux)
     87  1.1  christos {
     88  1.1  christos 	struct cardbus_attach_args *ca = aux;
     89  1.1  christos 
     90  1.1  christos 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_BROADCOM) {
     91  1.1  christos 		switch (PCI_PRODUCT(ca->ca_id)) {
     92  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4303:
     93  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4306:
     94  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4306_2:
     95  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4307:
     96  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4309:
     97  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4311:
     98  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4312:
     99  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4318:
    100  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4319:
    101  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4322:
    102  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM43XG:
    103  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4328:
    104  1.1  christos 			return (1);
    105  1.1  christos 		default:
    106  1.1  christos 			return (0);
    107  1.1  christos 		}
    108  1.1  christos 	}
    109  1.1  christos 
    110  1.1  christos 	return (0);
    111  1.1  christos }
    112  1.1  christos 
    113  1.2  nakayama static void
    114  1.1  christos bwi_cardbus_attach(device_t parent, device_t self, void *aux)
    115  1.1  christos {
    116  1.1  christos 	struct bwi_cardbus_softc *csc = device_private(self);
    117  1.1  christos 	struct cardbus_attach_args *ca = aux;
    118  1.1  christos 	struct bwi_softc *sc = &csc->csc_bwi;
    119  1.1  christos 	cardbus_devfunc_t ct = ca->ca_ct;
    120  1.1  christos 	pcireg_t reg;
    121  1.1  christos 	bus_addr_t base;
    122  1.1  christos 	int error;
    123  1.1  christos 
    124  1.2  nakayama 	aprint_naive("\n");
    125  1.2  nakayama 	aprint_normal(": Broadcom Wireless\n");
    126  1.2  nakayama 
    127  1.1  christos 	sc->sc_dev = self;
    128  1.1  christos 	sc->sc_dmat = ca->ca_dmat;
    129  1.1  christos 	csc->csc_ct = ct;
    130  1.1  christos 	csc->csc_tag = ca->ca_tag;
    131  1.1  christos 
    132  1.1  christos 	/* power management hooks */
    133  1.1  christos 	sc->sc_enable = bwi_cardbus_enable;
    134  1.1  christos 	sc->sc_disable = bwi_cardbus_disable;
    135  1.1  christos 
    136  1.1  christos 	/* map control/status registers */
    137  1.1  christos 	error = Cardbus_mapreg_map(ct, BWI_PCIR_BAR,
    138  1.1  christos 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem_bt,
    139  1.1  christos 	    &sc->sc_mem_bh, &base, &csc->csc_mapsize);
    140  1.1  christos 	if (error != 0) {
    141  1.2  nakayama 		aprint_error_dev(self, "can't map mem space\n");
    142  1.1  christos 		return;
    143  1.1  christos 	}
    144  1.1  christos 	csc->csc_bar_val = base | PCI_MAPREG_TYPE_MEM;
    145  1.1  christos 
    146  1.1  christos 	/* set up the PCI configuration registers */
    147  1.1  christos 	bwi_cardbus_setup(csc);
    148  1.1  christos 
    149  1.1  christos 	/* we need to access Cardbus config space from the driver */
    150  1.1  christos 	sc->sc_conf_read = bwi_cardbus_conf_read;
    151  1.1  christos 	sc->sc_conf_write = bwi_cardbus_conf_write;
    152  1.1  christos 
    153  1.1  christos 	reg = (sc->sc_conf_read)(sc, PCI_SUBSYS_ID_REG);
    154  1.1  christos 
    155  1.1  christos 	sc->sc_pci_revid = PCI_REVISION(ca->ca_class);
    156  1.1  christos 	sc->sc_pci_did = PCI_PRODUCT(ca->ca_id);
    157  1.1  christos 	sc->sc_pci_subvid = PCI_VENDOR(reg);
    158  1.1  christos 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
    159  1.1  christos 
    160  1.2  nakayama 	if (pmf_device_register(self, bwi_suspend, bwi_resume))
    161  1.2  nakayama 		pmf_class_network_register(self, &sc->sc_if);
    162  1.2  nakayama 	else
    163  1.2  nakayama 		aprint_error_dev(self, "couldn't establish power handler\n");
    164  1.2  nakayama 
    165  1.1  christos 	error = bwi_attach(sc);
    166  1.1  christos 	if (error != 0)
    167  1.2  nakayama 		bwi_cardbus_detach(self, 0);
    168  1.1  christos 
    169  1.1  christos 	Cardbus_function_disable(ct);
    170  1.1  christos }
    171  1.1  christos 
    172  1.2  nakayama static int
    173  1.1  christos bwi_cardbus_detach(device_t self, int flags)
    174  1.1  christos {
    175  1.1  christos 	struct bwi_cardbus_softc *csc = device_private(self);
    176  1.1  christos 	struct bwi_softc *sc = &csc->csc_bwi;
    177  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    178  1.1  christos 
    179  1.1  christos #ifdef DIAGNOSTIC
    180  1.1  christos 	if (ct == NULL)
    181  1.1  christos 		panic("%s: data structure lacks", device_xname(self));
    182  1.2  nakayama #endif
    183  1.1  christos 
    184  1.2  nakayama 	pmf_device_deregister(self);
    185  1.1  christos 
    186  1.1  christos 	bwi_detach(sc);
    187  1.1  christos 
    188  1.1  christos 	/* unhook the interrupt handler */
    189  1.2  nakayama 	if (sc->sc_ih != NULL) {
    190  1.2  nakayama 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    191  1.2  nakayama 		sc->sc_ih = NULL;
    192  1.1  christos 	}
    193  1.1  christos 
    194  1.1  christos 	/* release bus space and close window */
    195  1.1  christos 	Cardbus_mapreg_unmap(ct, BWI_PCIR_BAR, sc->sc_mem_bt,
    196  1.1  christos 	    sc->sc_mem_bh, csc->csc_mapsize);
    197  1.1  christos 
    198  1.1  christos 	return (0);
    199  1.1  christos }
    200  1.1  christos 
    201  1.2  nakayama static void
    202  1.1  christos bwi_cardbus_setup(struct bwi_cardbus_softc *csc)
    203  1.1  christos {
    204  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    205  1.1  christos 	pcireg_t reg;
    206  1.1  christos 
    207  1.1  christos 	/* program the BAR */
    208  1.2  nakayama 	Cardbus_conf_write(ct, csc->csc_tag, BWI_PCIR_BAR, csc->csc_bar_val);
    209  1.1  christos 
    210  1.1  christos 	/* enable the appropriate bits in the PCI CSR */
    211  1.2  nakayama 	reg = Cardbus_conf_read(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG);
    212  1.1  christos 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    213  1.2  nakayama 	Cardbus_conf_write(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG, reg);
    214  1.1  christos }
    215  1.1  christos 
    216  1.2  nakayama static int
    217  1.2  nakayama bwi_cardbus_enable(struct bwi_softc *sc, int pmf)
    218  1.1  christos {
    219  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    220  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    221  1.1  christos 
    222  1.2  nakayama 	if (!pmf) {
    223  1.2  nakayama 		/* power on the socket */
    224  1.2  nakayama 		Cardbus_function_enable(ct);
    225  1.1  christos 
    226  1.2  nakayama 		/* setup the PCI configuration registers */
    227  1.2  nakayama 		bwi_cardbus_setup(csc);
    228  1.2  nakayama 	}
    229  1.1  christos 
    230  1.1  christos 	/* map and establish the interrupt handler */
    231  1.2  nakayama 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, bwi_intr, sc);
    232  1.2  nakayama 	if (sc->sc_ih == NULL) {
    233  1.1  christos 		aprint_error_dev(sc->sc_dev,
    234  1.1  christos 		    "unable to establish interrupt\n");
    235  1.2  nakayama 		if (!pmf)
    236  1.2  nakayama 			Cardbus_function_disable(ct);
    237  1.2  nakayama 		return (1);
    238  1.1  christos 	}
    239  1.1  christos 
    240  1.1  christos 	return (0);
    241  1.1  christos }
    242  1.1  christos 
    243  1.2  nakayama static void
    244  1.2  nakayama bwi_cardbus_disable(struct bwi_softc *sc, int pmf)
    245  1.1  christos {
    246  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    247  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    248  1.1  christos 
    249  1.1  christos 	/* unhook the interrupt handler */
    250  1.2  nakayama 	if (sc->sc_ih != NULL) {
    251  1.2  nakayama 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    252  1.2  nakayama 		sc->sc_ih = NULL;
    253  1.2  nakayama 	}
    254  1.1  christos 
    255  1.2  nakayama 	if (!pmf) {
    256  1.2  nakayama 		/* power down the socket */
    257  1.2  nakayama 		Cardbus_function_disable(ct);
    258  1.2  nakayama 	}
    259  1.1  christos }
    260  1.1  christos 
    261  1.2  nakayama static void
    262  1.2  nakayama bwi_cardbus_conf_write(void *sc, uint32_t reg, uint32_t val)
    263  1.1  christos {
    264  1.2  nakayama 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    265  1.1  christos 
    266  1.2  nakayama 	Cardbus_conf_write(csc->csc_ct, csc->csc_tag, reg, val);
    267  1.1  christos }
    268  1.1  christos 
    269  1.2  nakayama static uint32_t
    270  1.2  nakayama bwi_cardbus_conf_read(void *sc, uint32_t reg)
    271  1.1  christos {
    272  1.2  nakayama 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    273  1.1  christos 
    274  1.2  nakayama 	return Cardbus_conf_read(csc->csc_ct, csc->csc_tag, reg);
    275  1.1  christos }
    276