1 1.3 thorpej /* $NetBSD: if_athn_cardbus.c,v 1.3 2022/09/25 17:33:19 thorpej Exp $ */ 2 1.1 christos /* $OpenBSD: if_athn_cardbus.c,v 1.13 2011/01/08 10:02:32 damien Exp $ */ 3 1.1 christos 4 1.1 christos /*- 5 1.1 christos * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr> 6 1.1 christos * 7 1.1 christos * Permission to use, copy, modify, and distribute this software for any 8 1.1 christos * purpose with or without fee is hereby granted, provided that the above 9 1.1 christos * copyright notice and this permission notice appear in all copies. 10 1.1 christos * 11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 1.1 christos */ 19 1.1 christos 20 1.1 christos /* 21 1.1 christos * CardBus front-end for Atheros 802.11a/g/n chipsets. 22 1.1 christos */ 23 1.1 christos 24 1.1 christos #include <sys/cdefs.h> 25 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: if_athn_cardbus.c,v 1.3 2022/09/25 17:33:19 thorpej Exp $"); 26 1.1 christos 27 1.1 christos #include "opt_inet.h" 28 1.1 christos 29 1.1 christos #include <sys/param.h> 30 1.1 christos #include <sys/sockio.h> 31 1.1 christos #include <sys/mbuf.h> 32 1.1 christos #include <sys/kernel.h> 33 1.1 christos #include <sys/socket.h> 34 1.1 christos #include <sys/systm.h> 35 1.1 christos #include <sys/callout.h> 36 1.1 christos #include <sys/device.h> 37 1.1 christos 38 1.1 christos #include <sys/bus.h> 39 1.1 christos #include <sys/intr.h> 40 1.1 christos 41 1.1 christos #include <net/if.h> 42 1.2 christos #include <net/if_ether.h> 43 1.1 christos #include <net/if_media.h> 44 1.1 christos 45 1.1 christos #include <net80211/ieee80211_var.h> 46 1.1 christos #include <net80211/ieee80211_amrr.h> 47 1.1 christos #include <net80211/ieee80211_radiotap.h> 48 1.1 christos 49 1.1 christos #include <dev/ic/athnreg.h> 50 1.1 christos #include <dev/ic/athnvar.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.1 christos #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 /* 59 1.1 christos * PCI configuration space registers 60 1.1 christos */ 61 1.1 christos #define ATHN_PCI_MMBA PCI_BAR(0) /* memory mapped base */ 62 1.1 christos 63 1.1 christos struct athn_cardbus_softc { 64 1.1 christos struct athn_softc csc_sc; 65 1.1 christos 66 1.1 christos /* CardBus specific goo. */ 67 1.1 christos cardbus_devfunc_t csc_ct; 68 1.1 christos pcitag_t csc_tag; 69 1.1 christos void *csc_ih; 70 1.1 christos bus_space_tag_t csc_iot; 71 1.1 christos bus_space_handle_t csc_ioh; 72 1.1 christos bus_size_t csc_mapsz; 73 1.1 christos pcireg_t csc_bar_val; 74 1.1 christos }; 75 1.1 christos 76 1.1 christos #define Static static 77 1.1 christos 78 1.1 christos Static int athn_cardbus_match(device_t, cfdata_t, void *); 79 1.1 christos Static void athn_cardbus_attach(device_t, device_t, void *); 80 1.1 christos Static int athn_cardbus_detach(device_t, int); 81 1.1 christos 82 1.1 christos CFATTACH_DECL_NEW(athn_cardbus, sizeof(struct athn_cardbus_softc), 83 1.1 christos athn_cardbus_match, athn_cardbus_attach, athn_cardbus_detach, NULL); 84 1.1 christos 85 1.1 christos Static uint32_t athn_cardbus_read(struct athn_softc *, uint32_t); 86 1.1 christos Static bool athn_cardbus_resume(device_t, const pmf_qual_t *l); 87 1.1 christos Static void athn_cardbus_setup(struct athn_cardbus_softc *); 88 1.1 christos Static bool athn_cardbus_suspend(device_t, const pmf_qual_t *); 89 1.1 christos Static void athn_cardbus_write(struct athn_softc *, uint32_t, uint32_t); 90 1.1 christos Static void athn_cardbus_write_barrier(struct athn_softc *); 91 1.1 christos 92 1.1 christos #ifdef openbsd_power_management 93 1.1 christos Static int athn_cardbus_enable(struct athn_softc *); 94 1.1 christos Static void athn_cardbus_disable(struct athn_softc *); 95 1.1 christos Static void athn_cardbus_power(struct athn_softc *, int); 96 1.1 christos #endif /* openbsd_power_management */ 97 1.1 christos 98 1.1 christos Static int 99 1.1 christos athn_cardbus_match(device_t parent, cfdata_t match, void *aux) 100 1.1 christos { 101 1.1 christos static const struct { 102 1.1 christos pci_vendor_id_t vendor; 103 1.1 christos pci_product_id_t product; 104 1.1 christos } athn_cardbus_devices[] = { 105 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5416 }, 106 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5418 }, 107 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9160 }, 108 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9280 }, 109 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9281 }, 110 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9285 }, 111 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR2427 }, 112 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9227 }, 113 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9287 }, 114 1.1 christos { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9300 } 115 1.1 christos }; 116 1.1 christos struct cardbus_attach_args *ca = aux; 117 1.1 christos size_t i; 118 1.1 christos 119 1.1 christos for (i = 0; i < __arraycount(athn_cardbus_devices); i++) { 120 1.1 christos if (PCI_VENDOR(ca->ca_id) == athn_cardbus_devices[i].vendor && 121 1.1 christos PCI_VENDOR(ca->ca_id) == athn_cardbus_devices[i].product) 122 1.1 christos return 1; 123 1.1 christos } 124 1.1 christos return 0; 125 1.1 christos } 126 1.1 christos 127 1.1 christos Static void 128 1.1 christos athn_cardbus_attach(device_t parent, device_t self, void *aux) 129 1.1 christos { 130 1.1 christos struct athn_cardbus_softc *csc = device_private(self); 131 1.1 christos struct athn_softc *sc = &csc->csc_sc; 132 1.1 christos struct ieee80211com *ic = &sc->sc_ic; 133 1.1 christos struct cardbus_attach_args *ca = aux; 134 1.1 christos cardbus_devfunc_t ct = ca->ca_ct; 135 1.1 christos bus_addr_t base; 136 1.1 christos int error; 137 1.1 christos 138 1.1 christos sc->sc_dmat = ca->ca_dmat; 139 1.1 christos csc->csc_ct = ct; 140 1.1 christos csc->csc_tag = ca->ca_tag; 141 1.1 christos 142 1.1 christos aprint_normal("\n"); 143 1.1 christos aprint_naive("\n"); 144 1.1 christos 145 1.1 christos #ifdef openbsd_power_management 146 1.1 christos /* Power management hooks. */ 147 1.1 christos sc->sc_enable = athn_cardbus_enable; 148 1.1 christos sc->sc_disable = athn_cardbus_disable; 149 1.1 christos sc->sc_power = athn_cardbus_power; 150 1.1 christos #endif 151 1.1 christos sc->sc_ops.read = athn_cardbus_read; 152 1.1 christos sc->sc_ops.write = athn_cardbus_write; 153 1.1 christos sc->sc_ops.write_barrier = athn_cardbus_write_barrier; 154 1.1 christos 155 1.1 christos /* 156 1.1 christos * Map control/status registers. 157 1.1 christos */ 158 1.1 christos error = Cardbus_mapreg_map(ct, ATHN_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0, 159 1.1 christos &csc->csc_iot, &csc->csc_ioh, &base, &csc->csc_mapsz); 160 1.1 christos if (error != 0) { 161 1.1 christos aprint_error_dev(self, "unable to map device (%d)\n", error); 162 1.1 christos return; 163 1.1 christos } 164 1.1 christos csc->csc_bar_val = base | PCI_MAPREG_TYPE_MEM; 165 1.1 christos 166 1.1 christos /* 167 1.1 christos * Set up the PCI configuration registers. 168 1.1 christos */ 169 1.1 christos athn_cardbus_setup(csc); 170 1.1 christos 171 1.1 christos if (athn_attach(sc) != 0) { 172 1.1 christos Cardbus_mapreg_unmap(ct, ATHN_PCI_MMBA, csc->csc_iot, 173 1.1 christos csc->csc_ioh, csc->csc_mapsz); 174 1.1 christos return; 175 1.1 christos } 176 1.1 christos 177 1.1 christos if (pmf_device_register(self, 178 1.1 christos athn_cardbus_suspend, athn_cardbus_resume)) { 179 1.1 christos pmf_class_network_register(self, &sc->sc_if); 180 1.1 christos pmf_device_suspend(self, &sc->sc_qual); 181 1.1 christos } else 182 1.1 christos aprint_error_dev(self, "couldn't establish power handler\n"); 183 1.1 christos 184 1.1 christos // Cardbus_function_disable(ct); 185 1.1 christos 186 1.1 christos ieee80211_announce(ic); 187 1.1 christos } 188 1.1 christos 189 1.1 christos Static int 190 1.1 christos athn_cardbus_detach(device_t self, int flags) 191 1.1 christos { 192 1.1 christos struct athn_cardbus_softc *csc = device_private(self); 193 1.1 christos struct athn_softc *sc = &csc->csc_sc; 194 1.1 christos cardbus_devfunc_t ct = csc->csc_ct; 195 1.1 christos cardbus_chipset_tag_t cc = ct->ct_cc; 196 1.1 christos cardbus_function_tag_t cf = ct->ct_cf; 197 1.1 christos 198 1.1 christos athn_detach(sc); 199 1.1 christos 200 1.1 christos pmf_device_deregister(self); 201 1.1 christos 202 1.1 christos /* Unhook the interrupt handler. */ 203 1.1 christos if (csc->csc_ih != NULL) 204 1.1 christos cardbus_intr_disestablish(cc, cf, csc->csc_ih); 205 1.1 christos 206 1.1 christos /* Release bus space and close window. */ 207 1.1 christos Cardbus_mapreg_unmap(ct, ATHN_PCI_MMBA, csc->csc_iot, csc->csc_ioh, 208 1.1 christos csc->csc_mapsz); 209 1.1 christos 210 1.1 christos return 0; 211 1.1 christos } 212 1.1 christos 213 1.1 christos Static void 214 1.1 christos athn_cardbus_setup(struct athn_cardbus_softc *csc) 215 1.1 christos { 216 1.1 christos cardbus_devfunc_t ct = csc->csc_ct; 217 1.1 christos #ifdef unneeded /* XXX */ 218 1.1 christos pci_chipset_tag_t pc = csc->csc_pc; 219 1.1 christos cardbus_chipset_tag_t cc = ct->ct_cc; 220 1.1 christos cardbus_function_tag_t cf = ct->ct_cf; 221 1.1 christos #endif 222 1.1 christos pcireg_t reg; 223 1.1 christos int rc; 224 1.1 christos 225 1.1 christos if ((rc = cardbus_set_powerstate(ct, csc->csc_tag, PCI_PWR_D0)) != 0) 226 1.1 christos aprint_debug("%s: cardbus_set_powerstate %d\n", __func__, rc); 227 1.1 christos 228 1.1 christos /* Program the BAR. */ 229 1.1 christos Cardbus_conf_write(ct, csc->csc_tag, ATHN_PCI_MMBA, csc->csc_bar_val); 230 1.1 christos 231 1.1 christos #ifdef unneeded /* XXX */ 232 1.1 christos /* Make sure the right access type is on the cardbus bridge. */ 233 1.1 christos (*cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE); 234 1.1 christos (*cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE); 235 1.1 christos #endif 236 1.1 christos /* Enable the appropriate bits in the PCI CSR. */ 237 1.1 christos reg = Cardbus_conf_read(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG); 238 1.1 christos reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE; 239 1.1 christos Cardbus_conf_write(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG, reg); 240 1.1 christos 241 1.1 christos /* 242 1.1 christos * Noone knows why this shit is necessary but there are claims that 243 1.1 christos * not doing this may cause very frequent PCI FATAL interrupts from 244 1.1 christos * the card: http://bugzilla.kernel.org/show_bug.cgi?id=13483 245 1.1 christos */ 246 1.1 christos reg = Cardbus_conf_read(ct, csc->csc_tag, 0x40); 247 1.1 christos if (reg & 0xff00) 248 1.1 christos Cardbus_conf_write(ct, csc->csc_tag, 0x40, reg & ~0xff00); 249 1.1 christos 250 1.1 christos /* Change latency timer; default value yields poor results. */ 251 1.1 christos reg = Cardbus_conf_read(ct, csc->csc_tag, PCI_BHLC_REG); 252 1.1 christos reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT); 253 1.1 christos reg |= 168 << PCI_LATTIMER_SHIFT; 254 1.1 christos Cardbus_conf_write(ct, csc->csc_tag, PCI_BHLC_REG, reg); 255 1.1 christos } 256 1.1 christos 257 1.1 christos Static uint32_t 258 1.1 christos athn_cardbus_read(struct athn_softc *sc, uint32_t addr) 259 1.1 christos { 260 1.1 christos struct athn_cardbus_softc *csc = (struct athn_cardbus_softc *)sc; 261 1.1 christos 262 1.1 christos return bus_space_read_4(csc->csc_iot, csc->csc_ioh, addr); 263 1.1 christos } 264 1.1 christos 265 1.1 christos Static void 266 1.1 christos athn_cardbus_write(struct athn_softc *sc, uint32_t addr, uint32_t val) 267 1.1 christos { 268 1.1 christos struct athn_cardbus_softc *csc = (struct athn_cardbus_softc *)sc; 269 1.1 christos 270 1.1 christos bus_space_write_4(csc->csc_iot, csc->csc_ioh, addr, val); 271 1.1 christos } 272 1.1 christos 273 1.1 christos Static void 274 1.1 christos athn_cardbus_write_barrier(struct athn_softc *sc) 275 1.1 christos { 276 1.1 christos struct athn_cardbus_softc *csc = (struct athn_cardbus_softc *)sc; 277 1.1 christos 278 1.1 christos bus_space_barrier(csc->csc_iot, csc->csc_ioh, 0, csc->csc_mapsz, 279 1.1 christos BUS_SPACE_BARRIER_WRITE); 280 1.1 christos } 281 1.1 christos 282 1.1 christos Static bool 283 1.1 christos athn_cardbus_suspend(device_t self, const pmf_qual_t *qual) 284 1.1 christos { 285 1.1 christos struct athn_cardbus_softc *csc = device_private(self); 286 1.1 christos 287 1.1 christos athn_suspend(&csc->csc_sc); 288 1.1 christos if (csc->csc_ih != NULL) { 289 1.1 christos Cardbus_intr_disestablish(csc->csc_ct, csc->csc_ih); 290 1.1 christos csc->csc_ih = NULL; 291 1.1 christos } 292 1.1 christos return true; 293 1.1 christos } 294 1.1 christos 295 1.1 christos Static bool 296 1.1 christos athn_cardbus_resume(device_t self, const pmf_qual_t *qual) 297 1.1 christos { 298 1.1 christos struct athn_cardbus_softc *csc = device_private(self); 299 1.1 christos 300 1.1 christos csc->csc_ih = Cardbus_intr_establish(csc->csc_ct, IPL_NET, athn_intr, 301 1.1 christos &csc->csc_sc); 302 1.1 christos 303 1.1 christos if (csc->csc_ih == NULL) { 304 1.1 christos aprint_error_dev(self, 305 1.1 christos "unable to establish interrupt\n"); 306 1.1 christos return false; 307 1.1 christos } 308 1.1 christos return athn_resume(&csc->csc_sc); 309 1.1 christos } 310 1.1 christos 311 1.1 christos /************************************************************************ 312 1.1 christos * XXX: presumably the pmf_* stuff handles this. 313 1.1 christos */ 314 1.1 christos #ifdef openbsd_power_management 315 1.1 christos Static int 316 1.1 christos athn_cardbus_enable(struct athn_softc *sc) 317 1.1 christos { 318 1.1 christos struct athn_cardbus_softc *csc = (struct athn_cardbus_softc *)sc; 319 1.1 christos cardbus_devfunc_t ct = csc->csc_ct; 320 1.1 christos cardbus_chipset_tag_t cc = ct->ct_cc; 321 1.1 christos cardbus_function_tag_t cf = ct->ct_cf; 322 1.1 christos 323 1.1 christos /* Power on the socket. */ 324 1.1 christos Cardbus_function_enable(ct); 325 1.1 christos 326 1.1 christos /* Setup the PCI configuration registers. */ 327 1.1 christos athn_cardbus_setup(csc); 328 1.1 christos 329 1.1 christos /* Map and establish the interrupt handler. */ 330 1.1 christos csc->csc_ih = cardbus_intr_establish(cc, cf, IPL_NET, athn_intr, sc); 331 1.1 christos if (csc->csc_ih == NULL) { 332 1.1 christos printf("%s: could not establish interrupt at %d\n", 333 1.1 christos device_xname(sc->sc_dev), csc->csc_intrline); 334 1.1 christos Cardbus_function_disable(ct); 335 1.1 christos return 1; 336 1.1 christos } 337 1.1 christos return 0; 338 1.1 christos } 339 1.1 christos 340 1.1 christos Static void 341 1.1 christos athn_cardbus_disable(struct athn_softc *sc) 342 1.1 christos { 343 1.1 christos struct athn_cardbus_softc *csc = (struct athn_cardbus_softc *)sc; 344 1.1 christos cardbus_devfunc_t ct = csc->csc_ct; 345 1.1 christos cardbus_chipset_tag_t cc = ct->ct_cc; 346 1.1 christos cardbus_function_tag_t cf = ct->ct_cf; 347 1.1 christos 348 1.1 christos /* Unhook the interrupt handler. */ 349 1.1 christos cardbus_intr_disestablish(cc, cf, csc->csc_ih); 350 1.1 christos csc->csc_ih = NULL; 351 1.1 christos 352 1.1 christos /* Power down the socket. */ 353 1.1 christos Cardbus_function_disable(ct); 354 1.1 christos } 355 1.1 christos 356 1.1 christos Static void 357 1.1 christos athn_cardbus_power(struct athn_softc *sc, int why) 358 1.1 christos { 359 1.1 christos struct athn_cardbus_softc *csc = (struct athn_cardbus_softc *)sc; 360 1.1 christos 361 1.1 christos if (why == DVACT_RESUME) { 362 1.1 christos /* Restore the PCI configuration registers. */ 363 1.1 christos athn_cardbus_setup(csc); 364 1.1 christos } 365 1.1 christos } 366 1.1 christos #endif /* openbsd_power_management */ 367