Home | History | Annotate | Line # | Download | only in cardbus
if_bwi_cardbus.c revision 1.3
      1  1.3   thorpej /*	$NetBSD: if_bwi_cardbus.c,v 1.3 2022/09/25 17:33:19 thorpej 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.3   thorpej __KERNEL_RCSID(0, "$NetBSD: if_bwi_cardbus.c,v 1.3 2022/09/25 17:33:19 thorpej 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/mbuf.h>
     36  1.1  christos #include <sys/socket.h>
     37  1.1  christos #include <sys/sockio.h>
     38  1.1  christos #include <sys/systm.h>
     39  1.1  christos #include <sys/errno.h>
     40  1.1  christos 
     41  1.1  christos #include <machine/endian.h>
     42  1.1  christos 
     43  1.1  christos #include <net/if.h>
     44  1.1  christos #include <net/if_dl.h>
     45  1.1  christos #include <net/if_ether.h>
     46  1.1  christos #include <net/if_media.h>
     47  1.1  christos 
     48  1.1  christos #include <net80211/ieee80211_var.h>
     49  1.1  christos #include <net80211/ieee80211_amrr.h>
     50  1.1  christos #include <net80211/ieee80211_radiotap.h>
     51  1.1  christos 
     52  1.1  christos #include <dev/pci/pcireg.h>
     53  1.1  christos #include <dev/pci/pcivar.h>
     54  1.2  nakayama #include <dev/pci/pcidevs.h>
     55  1.1  christos 
     56  1.1  christos #include <dev/cardbus/cardbusvar.h>
     57  1.1  christos 
     58  1.1  christos #include <dev/ic/bwireg.h>
     59  1.1  christos #include <dev/ic/bwivar.h>
     60  1.1  christos 
     61  1.1  christos struct bwi_cardbus_softc {
     62  1.1  christos 	struct bwi_softc	 csc_bwi;
     63  1.1  christos 
     64  1.1  christos 	/* cardbus specific goo */
     65  1.1  christos 	cardbus_devfunc_t	 csc_ct;
     66  1.1  christos 	pcitag_t		 csc_tag;
     67  1.1  christos 
     68  1.1  christos 	bus_size_t		 csc_mapsize;
     69  1.1  christos 	pcireg_t		 csc_bar_val;
     70  1.1  christos };
     71  1.1  christos 
     72  1.2  nakayama static int	bwi_cardbus_match(device_t, cfdata_t, void *);
     73  1.2  nakayama static void	bwi_cardbus_attach(device_t, device_t, void *);
     74  1.2  nakayama static int	bwi_cardbus_detach(device_t, int);
     75  1.2  nakayama static void	bwi_cardbus_setup(struct bwi_cardbus_softc *);
     76  1.2  nakayama static int	bwi_cardbus_enable(struct bwi_softc *, int);
     77  1.2  nakayama static void	bwi_cardbus_disable(struct bwi_softc *, int);
     78  1.2  nakayama static void	bwi_cardbus_conf_write(void *, uint32_t, uint32_t);
     79  1.2  nakayama static uint32_t	bwi_cardbus_conf_read(void *, uint32_t);
     80  1.1  christos 
     81  1.1  christos CFATTACH_DECL_NEW(bwi_cardbus, sizeof (struct bwi_cardbus_softc),
     82  1.1  christos     bwi_cardbus_match, bwi_cardbus_attach, bwi_cardbus_detach, NULL);
     83  1.1  christos 
     84  1.2  nakayama static int
     85  1.1  christos bwi_cardbus_match(device_t parent, cfdata_t match, void *aux)
     86  1.1  christos {
     87  1.1  christos 	struct cardbus_attach_args *ca = aux;
     88  1.1  christos 
     89  1.1  christos 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_BROADCOM) {
     90  1.1  christos 		switch (PCI_PRODUCT(ca->ca_id)) {
     91  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4303:
     92  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4306:
     93  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4306_2:
     94  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4307:
     95  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4309:
     96  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4311:
     97  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4312:
     98  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4318:
     99  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4319:
    100  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4322:
    101  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM43XG:
    102  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4328:
    103  1.1  christos 			return (1);
    104  1.1  christos 		default:
    105  1.1  christos 			return (0);
    106  1.1  christos 		}
    107  1.1  christos 	}
    108  1.1  christos 
    109  1.1  christos 	return (0);
    110  1.1  christos }
    111  1.1  christos 
    112  1.2  nakayama static void
    113  1.1  christos bwi_cardbus_attach(device_t parent, device_t self, void *aux)
    114  1.1  christos {
    115  1.1  christos 	struct bwi_cardbus_softc *csc = device_private(self);
    116  1.1  christos 	struct cardbus_attach_args *ca = aux;
    117  1.1  christos 	struct bwi_softc *sc = &csc->csc_bwi;
    118  1.1  christos 	cardbus_devfunc_t ct = ca->ca_ct;
    119  1.1  christos 	pcireg_t reg;
    120  1.1  christos 	bus_addr_t base;
    121  1.1  christos 	int error;
    122  1.1  christos 
    123  1.2  nakayama 	aprint_naive("\n");
    124  1.2  nakayama 	aprint_normal(": Broadcom Wireless\n");
    125  1.2  nakayama 
    126  1.1  christos 	sc->sc_dev = self;
    127  1.1  christos 	sc->sc_dmat = ca->ca_dmat;
    128  1.1  christos 	csc->csc_ct = ct;
    129  1.1  christos 	csc->csc_tag = ca->ca_tag;
    130  1.1  christos 
    131  1.1  christos 	/* power management hooks */
    132  1.1  christos 	sc->sc_enable = bwi_cardbus_enable;
    133  1.1  christos 	sc->sc_disable = bwi_cardbus_disable;
    134  1.1  christos 
    135  1.1  christos 	/* map control/status registers */
    136  1.1  christos 	error = Cardbus_mapreg_map(ct, BWI_PCIR_BAR,
    137  1.1  christos 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem_bt,
    138  1.1  christos 	    &sc->sc_mem_bh, &base, &csc->csc_mapsize);
    139  1.1  christos 	if (error != 0) {
    140  1.2  nakayama 		aprint_error_dev(self, "can't map mem space\n");
    141  1.1  christos 		return;
    142  1.1  christos 	}
    143  1.1  christos 	csc->csc_bar_val = base | PCI_MAPREG_TYPE_MEM;
    144  1.1  christos 
    145  1.1  christos 	/* set up the PCI configuration registers */
    146  1.1  christos 	bwi_cardbus_setup(csc);
    147  1.1  christos 
    148  1.1  christos 	/* we need to access Cardbus config space from the driver */
    149  1.1  christos 	sc->sc_conf_read = bwi_cardbus_conf_read;
    150  1.1  christos 	sc->sc_conf_write = bwi_cardbus_conf_write;
    151  1.1  christos 
    152  1.1  christos 	reg = (sc->sc_conf_read)(sc, PCI_SUBSYS_ID_REG);
    153  1.1  christos 
    154  1.1  christos 	sc->sc_pci_revid = PCI_REVISION(ca->ca_class);
    155  1.1  christos 	sc->sc_pci_did = PCI_PRODUCT(ca->ca_id);
    156  1.1  christos 	sc->sc_pci_subvid = PCI_VENDOR(reg);
    157  1.1  christos 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
    158  1.1  christos 
    159  1.2  nakayama 	if (pmf_device_register(self, bwi_suspend, bwi_resume))
    160  1.2  nakayama 		pmf_class_network_register(self, &sc->sc_if);
    161  1.2  nakayama 	else
    162  1.2  nakayama 		aprint_error_dev(self, "couldn't establish power handler\n");
    163  1.2  nakayama 
    164  1.1  christos 	error = bwi_attach(sc);
    165  1.1  christos 	if (error != 0)
    166  1.2  nakayama 		bwi_cardbus_detach(self, 0);
    167  1.1  christos 
    168  1.1  christos 	Cardbus_function_disable(ct);
    169  1.1  christos }
    170  1.1  christos 
    171  1.2  nakayama static int
    172  1.1  christos bwi_cardbus_detach(device_t self, int flags)
    173  1.1  christos {
    174  1.1  christos 	struct bwi_cardbus_softc *csc = device_private(self);
    175  1.1  christos 	struct bwi_softc *sc = &csc->csc_bwi;
    176  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    177  1.1  christos 
    178  1.1  christos #ifdef DIAGNOSTIC
    179  1.1  christos 	if (ct == NULL)
    180  1.1  christos 		panic("%s: data structure lacks", device_xname(self));
    181  1.2  nakayama #endif
    182  1.1  christos 
    183  1.2  nakayama 	pmf_device_deregister(self);
    184  1.1  christos 
    185  1.1  christos 	bwi_detach(sc);
    186  1.1  christos 
    187  1.1  christos 	/* unhook the interrupt handler */
    188  1.2  nakayama 	if (sc->sc_ih != NULL) {
    189  1.2  nakayama 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    190  1.2  nakayama 		sc->sc_ih = NULL;
    191  1.1  christos 	}
    192  1.1  christos 
    193  1.1  christos 	/* release bus space and close window */
    194  1.1  christos 	Cardbus_mapreg_unmap(ct, BWI_PCIR_BAR, sc->sc_mem_bt,
    195  1.1  christos 	    sc->sc_mem_bh, csc->csc_mapsize);
    196  1.1  christos 
    197  1.1  christos 	return (0);
    198  1.1  christos }
    199  1.1  christos 
    200  1.2  nakayama static void
    201  1.1  christos bwi_cardbus_setup(struct bwi_cardbus_softc *csc)
    202  1.1  christos {
    203  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    204  1.1  christos 	pcireg_t reg;
    205  1.1  christos 
    206  1.1  christos 	/* program the BAR */
    207  1.2  nakayama 	Cardbus_conf_write(ct, csc->csc_tag, BWI_PCIR_BAR, csc->csc_bar_val);
    208  1.1  christos 
    209  1.1  christos 	/* enable the appropriate bits in the PCI CSR */
    210  1.2  nakayama 	reg = Cardbus_conf_read(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG);
    211  1.1  christos 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    212  1.2  nakayama 	Cardbus_conf_write(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG, reg);
    213  1.1  christos }
    214  1.1  christos 
    215  1.2  nakayama static int
    216  1.2  nakayama bwi_cardbus_enable(struct bwi_softc *sc, int pmf)
    217  1.1  christos {
    218  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    219  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    220  1.1  christos 
    221  1.2  nakayama 	if (!pmf) {
    222  1.2  nakayama 		/* power on the socket */
    223  1.2  nakayama 		Cardbus_function_enable(ct);
    224  1.1  christos 
    225  1.2  nakayama 		/* setup the PCI configuration registers */
    226  1.2  nakayama 		bwi_cardbus_setup(csc);
    227  1.2  nakayama 	}
    228  1.1  christos 
    229  1.1  christos 	/* map and establish the interrupt handler */
    230  1.2  nakayama 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, bwi_intr, sc);
    231  1.2  nakayama 	if (sc->sc_ih == NULL) {
    232  1.1  christos 		aprint_error_dev(sc->sc_dev,
    233  1.1  christos 		    "unable to establish interrupt\n");
    234  1.2  nakayama 		if (!pmf)
    235  1.2  nakayama 			Cardbus_function_disable(ct);
    236  1.2  nakayama 		return (1);
    237  1.1  christos 	}
    238  1.1  christos 
    239  1.1  christos 	return (0);
    240  1.1  christos }
    241  1.1  christos 
    242  1.2  nakayama static void
    243  1.2  nakayama bwi_cardbus_disable(struct bwi_softc *sc, int pmf)
    244  1.1  christos {
    245  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    246  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    247  1.1  christos 
    248  1.1  christos 	/* unhook the interrupt handler */
    249  1.2  nakayama 	if (sc->sc_ih != NULL) {
    250  1.2  nakayama 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    251  1.2  nakayama 		sc->sc_ih = NULL;
    252  1.2  nakayama 	}
    253  1.1  christos 
    254  1.2  nakayama 	if (!pmf) {
    255  1.2  nakayama 		/* power down the socket */
    256  1.2  nakayama 		Cardbus_function_disable(ct);
    257  1.2  nakayama 	}
    258  1.1  christos }
    259  1.1  christos 
    260  1.2  nakayama static void
    261  1.2  nakayama bwi_cardbus_conf_write(void *sc, uint32_t reg, uint32_t val)
    262  1.1  christos {
    263  1.2  nakayama 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    264  1.1  christos 
    265  1.2  nakayama 	Cardbus_conf_write(csc->csc_ct, csc->csc_tag, reg, val);
    266  1.1  christos }
    267  1.1  christos 
    268  1.2  nakayama static uint32_t
    269  1.2  nakayama bwi_cardbus_conf_read(void *sc, uint32_t reg)
    270  1.1  christos {
    271  1.2  nakayama 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    272  1.1  christos 
    273  1.2  nakayama 	return Cardbus_conf_read(csc->csc_ct, csc->csc_tag, reg);
    274  1.1  christos }
    275