if_athn_pci.c revision 1.8.4.2 1 1.8.4.2 tls /* $NetBSD: if_athn_pci.c,v 1.8.4.2 2013/06/23 06:20:18 tls Exp $ */
2 1.8.4.2 tls /* $OpenBSD: if_athn_pci.c,v 1.11 2011/01/08 10:02:32 damien Exp $ */
3 1.8.4.2 tls
4 1.8.4.2 tls /*-
5 1.8.4.2 tls * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.8.4.2 tls *
7 1.8.4.2 tls * Permission to use, copy, modify, and distribute this software for any
8 1.8.4.2 tls * purpose with or without fee is hereby granted, provided that the above
9 1.8.4.2 tls * copyright notice and this permission notice appear in all copies.
10 1.8.4.2 tls *
11 1.8.4.2 tls * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.8.4.2 tls * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.8.4.2 tls * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.8.4.2 tls * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.8.4.2 tls * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.8.4.2 tls * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.8.4.2 tls * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.8.4.2 tls */
19 1.8.4.2 tls
20 1.8.4.2 tls /*
21 1.8.4.2 tls * PCI front-end for Atheros 802.11a/g/n chipsets.
22 1.8.4.2 tls */
23 1.8.4.2 tls
24 1.8.4.2 tls #include <sys/cdefs.h>
25 1.8.4.2 tls __KERNEL_RCSID(0, "$NetBSD: if_athn_pci.c,v 1.8.4.2 2013/06/23 06:20:18 tls Exp $");
26 1.8.4.2 tls
27 1.8.4.2 tls #include "opt_inet.h"
28 1.8.4.2 tls
29 1.8.4.2 tls #include <sys/param.h>
30 1.8.4.2 tls #include <sys/sockio.h>
31 1.8.4.2 tls #include <sys/mbuf.h>
32 1.8.4.2 tls #include <sys/kernel.h>
33 1.8.4.2 tls #include <sys/socket.h>
34 1.8.4.2 tls #include <sys/systm.h>
35 1.8.4.2 tls #include <sys/malloc.h>
36 1.8.4.2 tls #include <sys/callout.h>
37 1.8.4.2 tls #include <sys/device.h>
38 1.8.4.2 tls
39 1.8.4.2 tls #include <sys/bus.h>
40 1.8.4.2 tls #include <sys/intr.h>
41 1.8.4.2 tls
42 1.8.4.2 tls #include <net/if.h>
43 1.8.4.2 tls #include <net/if_ether.h>
44 1.8.4.2 tls #include <net/if_media.h>
45 1.8.4.2 tls
46 1.8.4.2 tls #include <net80211/ieee80211_var.h>
47 1.8.4.2 tls #include <net80211/ieee80211_amrr.h>
48 1.8.4.2 tls #include <net80211/ieee80211_radiotap.h>
49 1.8.4.2 tls
50 1.8.4.2 tls #include <dev/ic/athnreg.h>
51 1.8.4.2 tls #include <dev/ic/athnvar.h>
52 1.8.4.2 tls
53 1.8.4.2 tls #include <dev/pci/pcireg.h>
54 1.8.4.2 tls #include <dev/pci/pcivar.h>
55 1.8.4.2 tls #include <dev/pci/pcidevs.h>
56 1.8.4.2 tls
57 1.8.4.2 tls #define PCI_SUBSYSID_ATHEROS_COEX2WIRE 0x309b
58 1.8.4.2 tls #define PCI_SUBSYSID_ATHEROS_COEX3WIRE_SA 0x30aa
59 1.8.4.2 tls #define PCI_SUBSYSID_ATHEROS_COEX3WIRE_DA 0x30ab
60 1.8.4.2 tls
61 1.8.4.2 tls #define ATHN_PCI_MMBA PCI_BAR(0) /* memory mapped base */
62 1.8.4.2 tls
63 1.8.4.2 tls struct athn_pci_softc {
64 1.8.4.2 tls struct athn_softc psc_sc;
65 1.8.4.2 tls
66 1.8.4.2 tls /* PCI specific goo. */
67 1.8.4.2 tls pci_chipset_tag_t psc_pc;
68 1.8.4.2 tls pcitag_t psc_tag;
69 1.8.4.2 tls pci_intr_handle_t psc_pih;
70 1.8.4.2 tls void *psc_ih;
71 1.8.4.2 tls bus_space_tag_t psc_iot;
72 1.8.4.2 tls bus_space_handle_t psc_ioh;
73 1.8.4.2 tls bus_size_t psc_mapsz;
74 1.8.4.2 tls int psc_cap_off;
75 1.8.4.2 tls };
76 1.8.4.2 tls
77 1.8.4.2 tls #define Static static
78 1.8.4.2 tls
79 1.8.4.2 tls Static int athn_pci_match(device_t, cfdata_t, void *);
80 1.8.4.2 tls Static void athn_pci_attach(device_t, device_t, void *);
81 1.8.4.2 tls Static int athn_pci_detach(device_t, int);
82 1.8.4.2 tls Static int athn_pci_activate(device_t, enum devact);
83 1.8.4.2 tls
84 1.8.4.2 tls CFATTACH_DECL_NEW(athn_pci, sizeof(struct athn_pci_softc), athn_pci_match,
85 1.8.4.2 tls athn_pci_attach, athn_pci_detach, athn_pci_activate);
86 1.8.4.2 tls
87 1.8.4.2 tls Static bool athn_pci_resume(device_t, const pmf_qual_t *);
88 1.8.4.2 tls Static bool athn_pci_suspend(device_t, const pmf_qual_t *);
89 1.8.4.2 tls Static uint32_t athn_pci_read(struct athn_softc *, uint32_t);
90 1.8.4.2 tls Static void athn_pci_write(struct athn_softc *, uint32_t, uint32_t);
91 1.8.4.2 tls Static void athn_pci_write_barrier(struct athn_softc *);
92 1.8.4.2 tls Static void athn_pci_disable_aspm(struct athn_softc *);
93 1.8.4.2 tls
94 1.8.4.2 tls Static int
95 1.8.4.2 tls athn_pci_match(device_t parent, cfdata_t match, void *aux)
96 1.8.4.2 tls {
97 1.8.4.2 tls static const struct {
98 1.8.4.2 tls pci_vendor_id_t apd_vendor;
99 1.8.4.2 tls pci_product_id_t apd_product;
100 1.8.4.2 tls } athn_pci_devices[] = {
101 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5416 },
102 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5418 },
103 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9160 },
104 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9280 },
105 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9281 },
106 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9285 },
107 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR2427 },
108 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9227 },
109 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9287 },
110 1.8.4.2 tls { PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9300 }
111 1.8.4.2 tls };
112 1.8.4.2 tls struct pci_attach_args *pa = aux;
113 1.8.4.2 tls size_t i;
114 1.8.4.2 tls
115 1.8.4.2 tls for (i = 0; i < __arraycount(athn_pci_devices); i++) {
116 1.8.4.2 tls if (PCI_VENDOR(pa->pa_id) == athn_pci_devices[i].apd_vendor &&
117 1.8.4.2 tls PCI_PRODUCT(pa->pa_id) == athn_pci_devices[i].apd_product)
118 1.8.4.2 tls /*
119 1.8.4.2 tls * Match better than 1, we prefer this driver
120 1.8.4.2 tls * over ath(4)
121 1.8.4.2 tls */
122 1.8.4.2 tls return 10;
123 1.8.4.2 tls }
124 1.8.4.2 tls return 0;
125 1.8.4.2 tls }
126 1.8.4.2 tls
127 1.8.4.2 tls Static void
128 1.8.4.2 tls athn_pci_attach(device_t parent, device_t self, void *aux)
129 1.8.4.2 tls {
130 1.8.4.2 tls struct athn_pci_softc *psc = device_private(self);
131 1.8.4.2 tls struct athn_softc *sc = &psc->psc_sc;
132 1.8.4.2 tls struct ieee80211com *ic = &sc->sc_ic;
133 1.8.4.2 tls struct pci_attach_args *pa = aux;
134 1.8.4.2 tls const char *intrstr;
135 1.8.4.2 tls pcireg_t memtype, reg;
136 1.8.4.2 tls pci_product_id_t subsysid;
137 1.8.4.2 tls int error;
138 1.8.4.2 tls
139 1.8.4.2 tls sc->sc_dev = self;
140 1.8.4.2 tls sc->sc_dmat = pa->pa_dmat;
141 1.8.4.2 tls psc->psc_pc = pa->pa_pc;
142 1.8.4.2 tls psc->psc_tag = pa->pa_tag;
143 1.8.4.2 tls
144 1.8.4.2 tls sc->sc_ops.read = athn_pci_read;
145 1.8.4.2 tls sc->sc_ops.write = athn_pci_write;
146 1.8.4.2 tls sc->sc_ops.write_barrier = athn_pci_write_barrier;
147 1.8.4.2 tls
148 1.8.4.2 tls /*
149 1.8.4.2 tls * Get the offset of the PCI Express Capability Structure in PCI
150 1.8.4.2 tls * Configuration Space (Linux hardcodes it as 0x60.)
151 1.8.4.2 tls */
152 1.8.4.2 tls error = pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
153 1.8.4.2 tls &psc->psc_cap_off, NULL);
154 1.8.4.2 tls if (error != 0) { /* Found. */
155 1.8.4.2 tls sc->sc_disable_aspm = athn_pci_disable_aspm;
156 1.8.4.2 tls sc->sc_flags |= ATHN_FLAG_PCIE;
157 1.8.4.2 tls }
158 1.8.4.2 tls /*
159 1.8.4.2 tls * Noone knows why this shit is necessary but there are claims that
160 1.8.4.2 tls * not doing this may cause very frequent PCI FATAL interrupts from
161 1.8.4.2 tls * the card: http://bugzilla.kernel.org/show_bug.cgi?id=13483
162 1.8.4.2 tls */
163 1.8.4.2 tls reg = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40);
164 1.8.4.2 tls if (reg & 0xff00)
165 1.8.4.2 tls pci_conf_write(pa->pa_pc, pa->pa_tag, 0x40, reg & ~0xff00);
166 1.8.4.2 tls
167 1.8.4.2 tls /* Change latency timer; default value yields poor results. */
168 1.8.4.2 tls reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
169 1.8.4.2 tls reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
170 1.8.4.2 tls reg |= 168 << PCI_LATTIMER_SHIFT;
171 1.8.4.2 tls pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, reg);
172 1.8.4.2 tls
173 1.8.4.2 tls /* Determine if bluetooth is also supported (combo chip.) */
174 1.8.4.2 tls reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
175 1.8.4.2 tls subsysid = PCI_PRODUCT(reg);
176 1.8.4.2 tls if (subsysid == PCI_SUBSYSID_ATHEROS_COEX3WIRE_SA ||
177 1.8.4.2 tls subsysid == PCI_SUBSYSID_ATHEROS_COEX3WIRE_DA)
178 1.8.4.2 tls sc->sc_flags |= ATHN_FLAG_BTCOEX3WIRE;
179 1.8.4.2 tls else if (subsysid == PCI_SUBSYSID_ATHEROS_COEX2WIRE)
180 1.8.4.2 tls sc->sc_flags |= ATHN_FLAG_BTCOEX2WIRE;
181 1.8.4.2 tls
182 1.8.4.2 tls /*
183 1.8.4.2 tls * Setup memory-mapping of PCI registers.
184 1.8.4.2 tls */
185 1.8.4.2 tls memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ATHN_PCI_MMBA);
186 1.8.4.2 tls if (memtype != PCI_MAPREG_TYPE_MEM &&
187 1.8.4.2 tls memtype != PCI_MAPREG_MEM_TYPE_64BIT) {
188 1.8.4.2 tls aprint_error_dev(self, "bad pci register type %d\n",
189 1.8.4.2 tls (int)memtype);
190 1.8.4.2 tls goto fail;
191 1.8.4.2 tls }
192 1.8.4.2 tls error = pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &psc->psc_iot,
193 1.8.4.2 tls &psc->psc_ioh, NULL, &psc->psc_mapsz);
194 1.8.4.2 tls if (error != 0) {
195 1.8.4.2 tls aprint_error_dev(self, "cannot map register space\n");
196 1.8.4.2 tls goto fail;
197 1.8.4.2 tls }
198 1.8.4.2 tls
199 1.8.4.2 tls /*
200 1.8.4.2 tls * Arrange interrupt line.
201 1.8.4.2 tls */
202 1.8.4.2 tls if (pci_intr_map(pa, &psc->psc_pih) != 0) {
203 1.8.4.2 tls aprint_error_dev(self, "couldn't map interrupt\n");
204 1.8.4.2 tls goto fail1;
205 1.8.4.2 tls }
206 1.8.4.2 tls
207 1.8.4.2 tls intrstr = pci_intr_string(psc->psc_pc, psc->psc_pih);
208 1.8.4.2 tls psc->psc_ih = pci_intr_establish(psc->psc_pc, psc->psc_pih, IPL_NET,
209 1.8.4.2 tls athn_intr, sc);
210 1.8.4.2 tls if (psc->psc_ih == NULL) {
211 1.8.4.2 tls aprint_error_dev(self, "couldn't map interrupt\n");
212 1.8.4.2 tls goto fail1;
213 1.8.4.2 tls }
214 1.8.4.2 tls
215 1.8.4.2 tls ic->ic_ifp = &sc->sc_if;
216 1.8.4.2 tls if (athn_attach(sc) != 0)
217 1.8.4.2 tls goto fail2;
218 1.8.4.2 tls
219 1.8.4.2 tls aprint_verbose_dev(self, "interrupting at %s\n", intrstr);
220 1.8.4.2 tls
221 1.8.4.2 tls if (pmf_device_register(self, athn_pci_suspend, athn_pci_resume)) {
222 1.8.4.2 tls pmf_class_network_register(self, &sc->sc_if);
223 1.8.4.2 tls pmf_device_suspend(self, &sc->sc_qual);
224 1.8.4.2 tls }
225 1.8.4.2 tls else
226 1.8.4.2 tls aprint_error_dev(self, "couldn't establish power handler\n");
227 1.8.4.2 tls
228 1.8.4.2 tls ieee80211_announce(ic);
229 1.8.4.2 tls return;
230 1.8.4.2 tls
231 1.8.4.2 tls fail2:
232 1.8.4.2 tls pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
233 1.8.4.2 tls psc->psc_ih = NULL;
234 1.8.4.2 tls fail1:
235 1.8.4.2 tls bus_space_unmap(psc->psc_iot, psc->psc_ioh, psc->psc_mapsz);
236 1.8.4.2 tls psc->psc_mapsz = 0;
237 1.8.4.2 tls fail:
238 1.8.4.2 tls return;
239 1.8.4.2 tls }
240 1.8.4.2 tls
241 1.8.4.2 tls Static int
242 1.8.4.2 tls athn_pci_detach(device_t self, int flags)
243 1.8.4.2 tls {
244 1.8.4.2 tls struct athn_pci_softc *psc = device_private(self);
245 1.8.4.2 tls struct athn_softc *sc = &psc->psc_sc;
246 1.8.4.2 tls
247 1.8.4.2 tls if (psc->psc_ih != NULL) {
248 1.8.4.2 tls athn_detach(sc);
249 1.8.4.2 tls pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
250 1.8.4.2 tls psc->psc_ih = NULL;
251 1.8.4.2 tls }
252 1.8.4.2 tls if (psc->psc_mapsz > 0) {
253 1.8.4.2 tls bus_space_unmap(psc->psc_iot, psc->psc_ioh, psc->psc_mapsz);
254 1.8.4.2 tls psc->psc_mapsz = 0;
255 1.8.4.2 tls }
256 1.8.4.2 tls return 0;
257 1.8.4.2 tls }
258 1.8.4.2 tls
259 1.8.4.2 tls Static int
260 1.8.4.2 tls athn_pci_activate(device_t self, enum devact act)
261 1.8.4.2 tls {
262 1.8.4.2 tls struct athn_pci_softc *psc = device_private(self);
263 1.8.4.2 tls struct athn_softc *sc = &psc->psc_sc;
264 1.8.4.2 tls
265 1.8.4.2 tls switch (act) {
266 1.8.4.2 tls case DVACT_DEACTIVATE:
267 1.8.4.2 tls if_deactivate(sc->sc_ic.ic_ifp);
268 1.8.4.2 tls break;
269 1.8.4.2 tls }
270 1.8.4.2 tls return 0;
271 1.8.4.2 tls }
272 1.8.4.2 tls
273 1.8.4.2 tls Static bool
274 1.8.4.2 tls athn_pci_suspend(device_t self, const pmf_qual_t *qual)
275 1.8.4.2 tls {
276 1.8.4.2 tls struct athn_pci_softc *psc = device_private(self);
277 1.8.4.2 tls struct athn_softc *sc = &psc->psc_sc;
278 1.8.4.2 tls
279 1.8.4.2 tls athn_suspend(sc);
280 1.8.4.2 tls if (psc->psc_ih != NULL) {
281 1.8.4.2 tls pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
282 1.8.4.2 tls psc->psc_ih = NULL;
283 1.8.4.2 tls }
284 1.8.4.2 tls return true;
285 1.8.4.2 tls }
286 1.8.4.2 tls
287 1.8.4.2 tls Static bool
288 1.8.4.2 tls athn_pci_resume(device_t self, const pmf_qual_t *qual)
289 1.8.4.2 tls {
290 1.8.4.2 tls struct athn_pci_softc *psc = device_private(self);
291 1.8.4.2 tls struct athn_softc *sc = &psc->psc_sc;
292 1.8.4.2 tls pcireg_t reg;
293 1.8.4.2 tls
294 1.8.4.2 tls /*
295 1.8.4.2 tls * XXX: see comment in athn_attach().
296 1.8.4.2 tls */
297 1.8.4.2 tls reg = pci_conf_read(psc->psc_pc, psc->psc_tag, 0x40);
298 1.8.4.2 tls if (reg & 0xff00)
299 1.8.4.2 tls pci_conf_write(psc->psc_pc, psc->psc_tag, 0x40, reg & ~0xff00);
300 1.8.4.2 tls
301 1.8.4.2 tls psc->psc_ih = pci_intr_establish(psc->psc_pc, psc->psc_pih, IPL_NET,
302 1.8.4.2 tls athn_intr, sc);
303 1.8.4.2 tls if (psc->psc_ih == NULL) {
304 1.8.4.2 tls aprint_error_dev(self, "couldn't map interrupt\n");
305 1.8.4.2 tls return false;
306 1.8.4.2 tls }
307 1.8.4.2 tls return athn_resume(sc);
308 1.8.4.2 tls }
309 1.8.4.2 tls
310 1.8.4.2 tls Static uint32_t
311 1.8.4.2 tls athn_pci_read(struct athn_softc *sc, uint32_t addr)
312 1.8.4.2 tls {
313 1.8.4.2 tls struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
314 1.8.4.2 tls
315 1.8.4.2 tls return bus_space_read_4(psc->psc_iot, psc->psc_ioh, addr);
316 1.8.4.2 tls }
317 1.8.4.2 tls
318 1.8.4.2 tls Static void
319 1.8.4.2 tls athn_pci_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
320 1.8.4.2 tls {
321 1.8.4.2 tls struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
322 1.8.4.2 tls
323 1.8.4.2 tls bus_space_write_4(psc->psc_iot, psc->psc_ioh, addr, val);
324 1.8.4.2 tls }
325 1.8.4.2 tls
326 1.8.4.2 tls Static void
327 1.8.4.2 tls athn_pci_write_barrier(struct athn_softc *sc)
328 1.8.4.2 tls {
329 1.8.4.2 tls struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
330 1.8.4.2 tls
331 1.8.4.2 tls bus_space_barrier(psc->psc_iot, psc->psc_ioh, 0, psc->psc_mapsz,
332 1.8.4.2 tls BUS_SPACE_BARRIER_WRITE);
333 1.8.4.2 tls }
334 1.8.4.2 tls
335 1.8.4.2 tls Static void
336 1.8.4.2 tls athn_pci_disable_aspm(struct athn_softc *sc)
337 1.8.4.2 tls {
338 1.8.4.2 tls struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
339 1.8.4.2 tls pcireg_t reg;
340 1.8.4.2 tls
341 1.8.4.2 tls /* Disable PCIe Active State Power Management (ASPM). */
342 1.8.4.2 tls reg = pci_conf_read(psc->psc_pc, psc->psc_tag,
343 1.8.4.2 tls psc->psc_cap_off + PCIE_LCSR);
344 1.8.4.2 tls reg &= ~(PCIE_LCSR_ASPM_L0S | PCIE_LCSR_ASPM_L1);
345 1.8.4.2 tls pci_conf_write(psc->psc_pc, psc->psc_tag,
346 1.8.4.2 tls psc->psc_cap_off + PCIE_LCSR, reg);
347 1.8.4.2 tls }
348