1 /* $NetBSD: if_bwi_pci.c,v 1.18 2023/12/20 05:08:34 thorpej 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.18 2023/12/20 05:08:34 thorpej 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/mbuf.h> 34 #include <sys/socket.h> 35 #include <sys/sockio.h> 36 #include <sys/systm.h> 37 38 #include <sys/bus.h> 39 40 #include <net/if.h> 41 #include <net/if_dl.h> 42 #include <net/if_ether.h> 43 #include <net/if_media.h> 44 45 #include <net80211/ieee80211_var.h> 46 #include <net80211/ieee80211_amrr.h> 47 #include <net80211/ieee80211_radiotap.h> 48 49 #include <dev/ic/bwivar.h> 50 51 #include <dev/pci/pcireg.h> 52 #include <dev/pci/pcivar.h> 53 #include <dev/pci/pcidevs.h> 54 55 /* Base Address Register */ 56 #define BWI_PCI_BAR0 PCI_BAR(0) 57 58 static int bwi_pci_match(device_t, cfdata_t, void *); 59 static void bwi_pci_attach(device_t, device_t, void *); 60 static int bwi_pci_detach(device_t, int); 61 static void bwi_pci_conf_write(void *, uint32_t, uint32_t); 62 static uint32_t bwi_pci_conf_read(void *, uint32_t); 63 64 struct bwi_pci_softc { 65 struct bwi_softc psc_bwi; 66 67 pci_chipset_tag_t psc_pc; 68 pcitag_t psc_pcitag; 69 70 bus_size_t psc_mapsize; 71 }; 72 73 CFATTACH_DECL_NEW(bwi_pci, sizeof(struct bwi_pci_softc), 74 bwi_pci_match, bwi_pci_attach, bwi_pci_detach, NULL); 75 76 static const struct device_compatible_entry compat_data[] = { 77 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 78 PCI_PRODUCT_BROADCOM_BCM4303), }, 79 80 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 81 PCI_PRODUCT_BROADCOM_BCM4306), }, 82 83 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 84 PCI_PRODUCT_BROADCOM_BCM4306_2), }, 85 86 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 87 PCI_PRODUCT_BROADCOM_BCM4307), }, 88 89 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 90 PCI_PRODUCT_BROADCOM_BCM4309), }, 91 92 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 93 PCI_PRODUCT_BROADCOM_BCM4311), }, 94 95 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 96 PCI_PRODUCT_BROADCOM_BCM4312), }, 97 98 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 99 PCI_PRODUCT_BROADCOM_BCM4318), }, 100 101 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 102 PCI_PRODUCT_BROADCOM_BCM4319), }, 103 104 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 105 PCI_PRODUCT_BROADCOM_BCM4322), }, 106 107 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 108 PCI_PRODUCT_BROADCOM_BCM43XG), }, 109 110 { .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM, 111 PCI_PRODUCT_BROADCOM_BCM4328), }, 112 113 PCI_COMPAT_EOL 114 }; 115 116 static int 117 bwi_pci_match(device_t parent, cfdata_t match, void *aux) 118 { 119 struct pci_attach_args *pa = aux; 120 121 return pci_compatible_match(pa, compat_data); 122 } 123 124 static void 125 bwi_pci_attach(device_t parent, device_t self, void *aux) 126 { 127 struct bwi_pci_softc *psc = device_private(self); 128 struct pci_attach_args *pa = aux; 129 struct bwi_softc *sc = &psc->psc_bwi; 130 const char *intrstr = NULL; 131 pci_intr_handle_t ih; 132 pcireg_t memtype, reg; 133 int error = 0; 134 char intrbuf[PCI_INTRSTR_LEN]; 135 136 aprint_naive("\n"); 137 aprint_normal(": Broadcom Wireless\n"); 138 139 sc->sc_dev = self; 140 sc->sc_dmat = pa->pa_dmat; 141 psc->psc_pc = pa->pa_pc; 142 psc->psc_pcitag = pa->pa_tag; 143 144 /* map control / status registers */ 145 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BWI_PCI_BAR0); 146 switch (memtype) { 147 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 148 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 149 break; 150 default: 151 aprint_error_dev(self, "invalid base address register\n"); 152 return; 153 } 154 155 if (pci_mapreg_map(pa, BWI_PCI_BAR0, memtype, 0, &sc->sc_mem_bt, 156 &sc->sc_mem_bh, NULL, &psc->psc_mapsize) != 0) { 157 aprint_error_dev(self, "could not map mem space\n"); 158 return; 159 } 160 161 /* map interrupt */ 162 if (pci_intr_map(pa, &ih) != 0) { 163 aprint_error_dev(self, "could not map interrupt\n"); 164 goto fail; 165 } 166 167 /* establish interrupt */ 168 intrstr = pci_intr_string(psc->psc_pc, ih, intrbuf, sizeof(intrbuf)); 169 sc->sc_ih = pci_intr_establish_xname(psc->psc_pc, ih, IPL_NET, bwi_intr, 170 sc, device_xname(self)); 171 if (sc->sc_ih == NULL) { 172 aprint_error_dev(self, "could not establish interrupt"); 173 if (intrstr != NULL) 174 aprint_error(" at %s", intrstr); 175 aprint_error("\n"); 176 goto fail; 177 } 178 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 179 180 /* we need to access PCI config space from the driver */ 181 sc->sc_conf_write = bwi_pci_conf_write; 182 sc->sc_conf_read = bwi_pci_conf_read; 183 184 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 185 186 sc->sc_pci_revid = PCI_REVISION(pa->pa_class); 187 sc->sc_pci_did = PCI_PRODUCT(pa->pa_id); 188 sc->sc_pci_subvid = PCI_VENDOR(reg); 189 sc->sc_pci_subdid = PCI_PRODUCT(reg); 190 191 if (!pmf_device_register(self, bwi_suspend, bwi_resume)) 192 aprint_error_dev(self, "couldn't establish power handler\n"); 193 194 error = bwi_attach(sc); 195 if (error) 196 goto fail; 197 return; 198 199 fail: 200 if (sc->sc_ih) { 201 pci_intr_disestablish(psc->psc_pc, sc->sc_ih); 202 sc->sc_ih = NULL; 203 } 204 if (psc->psc_mapsize) { 205 bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, psc->psc_mapsize); 206 psc->psc_mapsize = 0; 207 } 208 return; 209 } 210 211 int 212 bwi_pci_detach(device_t self, int flags) 213 { 214 struct bwi_pci_softc *psc = device_private(self); 215 struct bwi_softc *sc = &psc->psc_bwi; 216 217 pmf_device_deregister(self); 218 219 bwi_detach(sc); 220 221 if (sc->sc_ih != NULL) { 222 pci_intr_disestablish(psc->psc_pc, sc->sc_ih); 223 sc->sc_ih = NULL; 224 } 225 226 return (0); 227 } 228 229 static void 230 bwi_pci_conf_write(void *sc, uint32_t reg, uint32_t val) 231 { 232 struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc; 233 234 pci_conf_write(psc->psc_pc, psc->psc_pcitag, reg, val); 235 } 236 237 static uint32_t 238 bwi_pci_conf_read(void *sc, uint32_t reg) 239 { 240 struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc; 241 242 return (pci_conf_read(psc->psc_pc, psc->psc_pcitag, reg)); 243 } 244