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