1 /* $OpenBSD: if_athn_pci.c,v 1.14 2013/12/06 21:03:03 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * PCI front-end for Atheros 802.11a/g/n chipsets. 21 */ 22 23 #include "bpfilter.h" 24 25 #include <sys/param.h> 26 #include <sys/sockio.h> 27 #include <sys/mbuf.h> 28 #include <sys/kernel.h> 29 #include <sys/socket.h> 30 #include <sys/systm.h> 31 #include <sys/malloc.h> 32 #include <sys/timeout.h> 33 #include <sys/device.h> 34 35 #include <machine/bus.h> 36 #include <machine/intr.h> 37 38 #include <net/if.h> 39 #include <net/if_dl.h> 40 #include <net/if_media.h> 41 42 #include <netinet/in.h> 43 #include <netinet/if_ether.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/athnreg.h> 50 #include <dev/ic/athnvar.h> 51 52 #include <dev/pci/pcireg.h> 53 #include <dev/pci/pcivar.h> 54 #include <dev/pci/pcidevs.h> 55 56 #define PCI_SUBSYSID_ATHEROS_COEX2WIRE 0x309b 57 #define PCI_SUBSYSID_ATHEROS_COEX3WIRE_SA 0x30aa 58 #define PCI_SUBSYSID_ATHEROS_COEX3WIRE_DA 0x30ab 59 60 struct athn_pci_softc { 61 struct athn_softc sc_sc; 62 63 /* PCI specific goo. */ 64 pci_chipset_tag_t sc_pc; 65 pcitag_t sc_tag; 66 void *sc_ih; 67 bus_space_tag_t sc_st; 68 bus_space_handle_t sc_sh; 69 bus_size_t sc_mapsize; 70 int sc_cap_off; 71 }; 72 73 int athn_pci_match(struct device *, void *, void *); 74 void athn_pci_attach(struct device *, struct device *, void *); 75 int athn_pci_detach(struct device *, int); 76 int athn_pci_activate(struct device *, int); 77 void athn_pci_wakeup(struct athn_pci_softc *); 78 uint32_t athn_pci_read(struct athn_softc *, uint32_t); 79 void athn_pci_write(struct athn_softc *, uint32_t, uint32_t); 80 void athn_pci_write_barrier(struct athn_softc *); 81 void athn_pci_disable_aspm(struct athn_softc *); 82 83 struct cfattach athn_pci_ca = { 84 sizeof (struct athn_pci_softc), 85 athn_pci_match, 86 athn_pci_attach, 87 athn_pci_detach, 88 athn_pci_activate 89 }; 90 91 static const struct pci_matchid athn_pci_devices[] = { 92 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5416 }, 93 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5418 }, 94 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9160 }, 95 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9280 }, 96 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9281 }, 97 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9285 }, 98 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR2427 }, 99 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9227 }, 100 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9287 }, 101 { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9300 } 102 }; 103 104 int 105 athn_pci_match(struct device *parent, void *match, void *aux) 106 { 107 return (pci_matchbyid(aux, athn_pci_devices, 108 nitems(athn_pci_devices))); 109 } 110 111 void 112 athn_pci_attach(struct device *parent, struct device *self, void *aux) 113 { 114 struct athn_pci_softc *psc = (struct athn_pci_softc *)self; 115 struct athn_softc *sc = &psc->sc_sc; 116 struct pci_attach_args *pa = aux; 117 const char *intrstr; 118 pci_intr_handle_t ih; 119 pcireg_t memtype, reg; 120 pci_product_id_t subsysid; 121 int error; 122 123 sc->sc_dmat = pa->pa_dmat; 124 psc->sc_pc = pa->pa_pc; 125 psc->sc_tag = pa->pa_tag; 126 127 sc->ops.read = athn_pci_read; 128 sc->ops.write = athn_pci_write; 129 sc->ops.write_barrier = athn_pci_write_barrier; 130 131 /* 132 * Get the offset of the PCI Express Capability Structure in PCI 133 * Configuration Space (Linux hardcodes it as 0x60.) 134 */ 135 error = pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PCIEXPRESS, 136 &psc->sc_cap_off, NULL); 137 if (error != 0) { /* Found. */ 138 sc->sc_disable_aspm = athn_pci_disable_aspm; 139 sc->flags |= ATHN_FLAG_PCIE; 140 } 141 /* 142 * Noone knows why this shit is necessary but there are claims that 143 * not doing this may cause very frequent PCI FATAL interrupts from 144 * the card: http://bugzilla.kernel.org/show_bug.cgi?id=13483 145 */ 146 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40); 147 if (reg & 0xff00) 148 pci_conf_write(pa->pa_pc, pa->pa_tag, 0x40, reg & ~0xff00); 149 150 /* Change latency timer; default value yields poor results. */ 151 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG); 152 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT); 153 reg |= 168 << PCI_LATTIMER_SHIFT; 154 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, reg); 155 156 /* Determine if bluetooth is also supported (combo chip.) */ 157 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 158 subsysid = PCI_PRODUCT(reg); 159 if (subsysid == PCI_SUBSYSID_ATHEROS_COEX3WIRE_SA || 160 subsysid == PCI_SUBSYSID_ATHEROS_COEX3WIRE_DA) 161 sc->flags |= ATHN_FLAG_BTCOEX3WIRE; 162 else if (subsysid == PCI_SUBSYSID_ATHEROS_COEX2WIRE) 163 sc->flags |= ATHN_FLAG_BTCOEX2WIRE; 164 165 /* Map control/status registers. */ 166 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START); 167 error = pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &psc->sc_st, 168 &psc->sc_sh, NULL, &psc->sc_mapsize, 0); 169 if (error != 0) { 170 printf(": can't map mem space\n"); 171 return; 172 } 173 174 if (pci_intr_map(pa, &ih) != 0) { 175 printf(": can't map interrupt\n"); 176 return; 177 } 178 intrstr = pci_intr_string(psc->sc_pc, ih); 179 psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, 180 athn_intr, sc, sc->sc_dev.dv_xname); 181 if (psc->sc_ih == NULL) { 182 printf(": can't establish interrupt"); 183 if (intrstr != NULL) 184 printf(" at %s", intrstr); 185 printf("\n"); 186 return; 187 } 188 printf(": %s\n", intrstr); 189 190 athn_attach(sc); 191 } 192 193 int 194 athn_pci_detach(struct device *self, int flags) 195 { 196 struct athn_pci_softc *psc = (struct athn_pci_softc *)self; 197 struct athn_softc *sc = &psc->sc_sc; 198 199 if (psc->sc_ih != NULL) { 200 athn_detach(sc); 201 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 202 } 203 if (psc->sc_mapsize > 0) 204 bus_space_unmap(psc->sc_st, psc->sc_sh, psc->sc_mapsize); 205 206 return (0); 207 } 208 209 int 210 athn_pci_activate(struct device *self, int act) 211 { 212 struct athn_pci_softc *psc = (struct athn_pci_softc *)self; 213 struct athn_softc *sc = &psc->sc_sc; 214 215 switch (act) { 216 case DVACT_SUSPEND: 217 athn_suspend(sc); 218 break; 219 case DVACT_WAKEUP: 220 athn_pci_wakeup(psc); 221 break; 222 } 223 224 return (0); 225 } 226 227 void 228 athn_pci_wakeup(struct athn_pci_softc *psc) 229 { 230 struct athn_softc *sc = &psc->sc_sc; 231 pcireg_t reg; 232 int s; 233 234 reg = pci_conf_read(psc->sc_pc, psc->sc_tag, 0x40); 235 if (reg & 0xff00) 236 pci_conf_write(psc->sc_pc, psc->sc_tag, 0x40, reg & ~0xff00); 237 238 s = splnet(); 239 athn_wakeup(sc); 240 splx(s); 241 } 242 243 uint32_t 244 athn_pci_read(struct athn_softc *sc, uint32_t addr) 245 { 246 struct athn_pci_softc *psc = (struct athn_pci_softc *)sc; 247 248 return (bus_space_read_4(psc->sc_st, psc->sc_sh, addr)); 249 } 250 251 void 252 athn_pci_write(struct athn_softc *sc, uint32_t addr, uint32_t val) 253 { 254 struct athn_pci_softc *psc = (struct athn_pci_softc *)sc; 255 256 bus_space_write_4(psc->sc_st, psc->sc_sh, addr, val); 257 } 258 259 void 260 athn_pci_write_barrier(struct athn_softc *sc) 261 { 262 struct athn_pci_softc *psc = (struct athn_pci_softc *)sc; 263 264 bus_space_barrier(psc->sc_st, psc->sc_sh, 0, psc->sc_mapsize, 265 BUS_SPACE_BARRIER_WRITE); 266 } 267 268 void 269 athn_pci_disable_aspm(struct athn_softc *sc) 270 { 271 struct athn_pci_softc *psc = (struct athn_pci_softc *)sc; 272 pcireg_t reg; 273 274 /* Disable PCIe Active State Power Management (ASPM). */ 275 reg = pci_conf_read(psc->sc_pc, psc->sc_tag, 276 psc->sc_cap_off + PCI_PCIE_LCSR); 277 reg &= ~(PCI_PCIE_LCSR_ASPM_L0S | PCI_PCIE_LCSR_ASPM_L1); 278 pci_conf_write(psc->sc_pc, psc->sc_tag, 279 psc->sc_cap_off + PCI_PCIE_LCSR, reg); 280 } 281