1 /* $NetBSD: if_bwi_pci.c,v 1.9 2010/01/19 22:07:00 pooka Exp $ */ 2 /* $OpenBSD: if_bwi_pci.c,v 1.6 2008/02/14 22:10:02 brad Exp $ */ 3 4 /* 5 * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * Broadcom AirForce BCM43xx IEEE 802.11b/g wireless network driver 22 * PCI front end 23 */ 24 25 26 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: if_bwi_pci.c,v 1.9 2010/01/19 22:07:00 pooka Exp $"); 28 29 #include <sys/param.h> 30 #include <sys/callout.h> 31 #include <sys/device.h> 32 #include <sys/kernel.h> 33 #include <sys/malloc.h> 34 #include <sys/mbuf.h> 35 #include <sys/socket.h> 36 #include <sys/sockio.h> 37 #include <sys/systm.h> 38 39 #include <machine/bus.h> 40 41 #include <net/if.h> 42 #include <net/if_dl.h> 43 #include <net/if_ether.h> 44 #include <net/if_media.h> 45 46 #include <net80211/ieee80211_var.h> 47 #include <net80211/ieee80211_amrr.h> 48 #include <net80211/ieee80211_radiotap.h> 49 50 #include <dev/ic/bwivar.h> 51 52 #include <dev/pci/pcireg.h> 53 #include <dev/pci/pcivar.h> 54 #include <dev/pci/pcidevs.h> 55 56 /* Base Address Register */ 57 #define BWI_PCI_BAR0 0x10 58 59 static int bwi_pci_match(device_t, cfdata_t, void *); 60 static void bwi_pci_attach(device_t, device_t, void *); 61 static int bwi_pci_detach(device_t, int); 62 static void bwi_pci_conf_write(void *, uint32_t, uint32_t); 63 static uint32_t bwi_pci_conf_read(void *, uint32_t); 64 65 struct bwi_pci_softc { 66 struct bwi_softc psc_bwi; 67 68 pci_chipset_tag_t psc_pc; 69 pcitag_t psc_pcitag; 70 71 bus_size_t psc_mapsize; 72 }; 73 74 CFATTACH_DECL_NEW(bwi_pci, sizeof(struct bwi_pci_softc), 75 bwi_pci_match, bwi_pci_attach, bwi_pci_detach, NULL); 76 77 static int 78 bwi_pci_match(device_t parent, cfdata_t match, void *aux) 79 { 80 struct pci_attach_args *pa = aux; 81 82 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BROADCOM) 83 return (0); 84 85 switch (PCI_PRODUCT(pa->pa_id)) { 86 case PCI_PRODUCT_BROADCOM_BCM4303: 87 case PCI_PRODUCT_BROADCOM_BCM4306: 88 case PCI_PRODUCT_BROADCOM_BCM4306_2: 89 case PCI_PRODUCT_BROADCOM_BCM4307: 90 case PCI_PRODUCT_BROADCOM_BCM4309: 91 case PCI_PRODUCT_BROADCOM_BCM4311: 92 case PCI_PRODUCT_BROADCOM_BCM4312: 93 case PCI_PRODUCT_BROADCOM_BCM4318: 94 case PCI_PRODUCT_BROADCOM_BCM4319: 95 case PCI_PRODUCT_BROADCOM_BCM4322: 96 case PCI_PRODUCT_BROADCOM_BCM43XG: 97 case PCI_PRODUCT_BROADCOM_BCM4328: 98 return (1); 99 } 100 101 return (0); 102 } 103 104 static void 105 bwi_pci_attach(device_t parent, device_t self, void *aux) 106 { 107 struct bwi_pci_softc *psc = device_private(self); 108 struct pci_attach_args *pa = aux; 109 struct bwi_softc *sc = &psc->psc_bwi; 110 const char *intrstr = NULL; 111 pci_intr_handle_t ih; 112 pcireg_t memtype, reg; 113 int error = 0; 114 115 aprint_naive("\n"); 116 aprint_normal(": Broadcom Wireless"); 117 118 sc->sc_dev = self; 119 sc->sc_dmat = pa->pa_dmat; 120 psc->psc_pc = pa->pa_pc; 121 psc->psc_pcitag = pa->pa_tag; 122 123 /* map control / status registers */ 124 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BWI_PCI_BAR0); 125 switch (memtype) { 126 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 127 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 128 break; 129 default: 130 aprint_error_dev(self, "invalid base address register\n"); 131 return; 132 } 133 134 if (pci_mapreg_map(pa, BWI_PCI_BAR0, 135 memtype, 0, &sc->sc_mem_bt, &sc->sc_mem_bh, 136 NULL, &psc->psc_mapsize) != 0) 137 { 138 aprint_error_dev(self, "could not map mem space\n"); 139 return; 140 } 141 142 /* map interrupt */ 143 if (pci_intr_map(pa, &ih) != 0) { 144 aprint_error_dev(self, "could not map interrupt\n"); 145 goto fail; 146 } 147 148 /* establish interrupt */ 149 intrstr = pci_intr_string(psc->psc_pc, ih); 150 sc->sc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_NET, bwi_intr, sc); 151 if (sc->sc_ih == NULL) { 152 aprint_error_dev(self, "could not establish interrupt"); 153 if (intrstr != NULL) 154 aprint_error(" at %s", intrstr); 155 aprint_error("\n"); 156 goto fail; 157 } 158 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 159 160 /* we need to access PCI config space from the driver */ 161 sc->sc_conf_write = bwi_pci_conf_write; 162 sc->sc_conf_read = bwi_pci_conf_read; 163 164 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 165 166 sc->sc_pci_revid = PCI_REVISION(pa->pa_class); 167 sc->sc_pci_did = PCI_PRODUCT(pa->pa_id); 168 sc->sc_pci_subvid = PCI_VENDOR(reg); 169 sc->sc_pci_subdid = PCI_PRODUCT(reg); 170 171 if (!pmf_device_register(self, bwi_suspend, bwi_resume)) 172 aprint_error_dev(self, "couldn't establish power handler\n"); 173 174 error = bwi_attach(sc); 175 if (error) 176 goto fail; 177 return; 178 179 fail: 180 if (sc->sc_ih) { 181 pci_intr_disestablish(psc->psc_pc, sc->sc_ih); 182 sc->sc_ih = NULL; 183 } 184 if (psc->psc_mapsize) { 185 bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, psc->psc_mapsize); 186 psc->psc_mapsize = 0; 187 } 188 return; 189 } 190 191 int 192 bwi_pci_detach(device_t self, int flags) 193 { 194 struct bwi_pci_softc *psc = device_private(self); 195 struct bwi_softc *sc = &psc->psc_bwi; 196 197 pmf_device_deregister(self); 198 199 bwi_detach(sc); 200 201 if (sc->sc_ih != NULL) { 202 pci_intr_disestablish(psc->psc_pc, sc->sc_ih); 203 sc->sc_ih = NULL; 204 } 205 206 return (0); 207 } 208 209 static void 210 bwi_pci_conf_write(void *sc, uint32_t reg, uint32_t val) 211 { 212 struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc; 213 214 pci_conf_write(psc->psc_pc, psc->psc_pcitag, reg, val); 215 } 216 217 static uint32_t 218 bwi_pci_conf_read(void *sc, uint32_t reg) 219 { 220 struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc; 221 222 return (pci_conf_read(psc->psc_pc, psc->psc_pcitag, reg)); 223 } 224