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