if_ath_cardbus.c revision 1.13 1 /* $NetBSD: if_ath_cardbus.c,v 1.13 2006/06/05 05:15:31 gdamore Exp $ */
2 /*
3 * Copyright (c) 2003
4 * Ichiro FUKUHARA <ichiro (at) ichiro.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Ichiro FUKUHARA.
18 * 4. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 /*
35 * CardBus bus front-end for the AR5001 Wireless LAN 802.11a/b/g CardBus.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_ath_cardbus.c,v 1.13 2006/06/05 05:15:31 gdamore Exp $");
40
41 #include "opt_inet.h"
42 #include "opt_ns.h"
43 #include "bpfilter.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/errno.h>
53 #include <sys/device.h>
54
55 #include <machine/endian.h>
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
61
62 #include <net80211/ieee80211_netbsd.h>
63 #include <net80211/ieee80211_var.h>
64
65 #if NBPFILTER > 0
66 #include <net/bpf.h>
67 #endif
68
69 #ifdef INET
70 #include <netinet/in.h>
71 #include <netinet/if_inarp.h>
72 #endif
73
74 #ifdef NS
75 #include <netns/ns.h>
76 #include <netns/ns_if.h>
77 #endif
78
79 #include <machine/bus.h>
80 #include <machine/intr.h>
81
82 #include <dev/mii/miivar.h>
83 #include <dev/mii/mii_bitbang.h>
84
85 #include <dev/ic/ath_netbsd.h>
86 #include <dev/ic/athvar.h>
87 #include <contrib/dev/ath/ah.h>
88
89 #include <dev/pci/pcivar.h>
90 #include <dev/pci/pcireg.h>
91 #include <dev/pci/pcidevs.h>
92
93 #include <dev/cardbus/cardbusvar.h>
94 #include <dev/pci/pcidevs.h>
95
96 /*
97 * PCI configuration space registers
98 */
99 #define ATH_PCI_MMBA 0x10 /* memory mapped base */
100
101 struct ath_cardbus_softc {
102 struct ath_softc sc_ath;
103
104 /* CardBus-specific goo. */
105 void *sc_ih; /* interrupt handle */
106 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
107 cardbustag_t sc_tag; /* our CardBus tag */
108 bus_size_t sc_mapsize; /* the size of mapped bus space region */
109
110 pcireg_t sc_bar_val; /* value of the BAR */
111
112 int sc_intrline; /* interrupt line */
113 bus_space_tag_t sc_iot;
114 bus_space_handle_t sc_ioh;
115 };
116
117 int ath_cardbus_match(struct device *, struct cfdata *, void *);
118 void ath_cardbus_attach(struct device *, struct device *, void *);
119 int ath_cardbus_detach(struct device *, int);
120
121 CFATTACH_DECL(ath_cardbus, sizeof(struct ath_cardbus_softc),
122 ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, ath_activate);
123
124 void ath_cardbus_setup(struct ath_cardbus_softc *);
125
126 int ath_cardbus_enable(struct ath_softc *);
127 void ath_cardbus_disable(struct ath_softc *);
128 void ath_cardbus_power(struct ath_softc *, int);
129
130 int
131 ath_cardbus_match(struct device *parent, struct cfdata *match,
132 void *aux)
133 {
134 struct cardbus_attach_args *ca = aux;
135 const char* devname;
136
137 devname = ath_hal_probe(PCI_VENDOR(ca->ca_id),
138 PCI_PRODUCT(ca->ca_id));
139
140 if (devname)
141 return (1);
142
143 return (0);
144 }
145
146 void
147 ath_cardbus_attach(struct device *parent, struct device *self,
148 void *aux)
149 {
150 struct ath_cardbus_softc *csc = device_private(self);
151 struct ath_softc *sc = &csc->sc_ath;
152 struct cardbus_attach_args *ca = aux;
153 cardbus_devfunc_t ct = ca->ca_ct;
154 bus_addr_t adr;
155
156 sc->sc_dmat = ca->ca_dmat;
157 csc->sc_ct = ct;
158 csc->sc_tag = ca->ca_tag;
159
160 printf("\n");
161
162 /*
163 * Power management hooks.
164 */
165 sc->sc_enable = ath_cardbus_enable;
166 sc->sc_disable = ath_cardbus_disable;
167 sc->sc_power = ath_cardbus_power;
168
169 /*
170 * Map the device.
171 */
172 if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, CARDBUS_MAPREG_TYPE_MEM, 0,
173 &csc->sc_iot, &csc->sc_ioh, &adr, &csc->sc_mapsize) == 0) {
174 #if rbus
175 #else
176 (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
177 #endif
178 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
179 }
180
181 else {
182 printf("%s: unable to map device registers\n",
183 sc->sc_dev.dv_xname);
184 return;
185 }
186
187 sc->sc_st = HALTAG(csc->sc_iot);
188 sc->sc_sh = HALHANDLE(csc->sc_ioh);
189
190 /*
191 * Set up the PCI configuration registers.
192 */
193 ath_cardbus_setup(csc);
194
195 /* Remember which interrupt line. */
196 csc->sc_intrline = ca->ca_intrline;
197
198 /*
199 * Finish off the attach.
200 */
201 ath_attach(PCI_PRODUCT(ca->ca_id), sc);
202
203 #ifdef ath_powerdown
204 /*
205 * Power down the socket.
206 */
207 Cardbus_function_disable(csc->sc_ct);
208 #endif /* ath_powerdown */
209 }
210
211 int
212 ath_cardbus_detach(struct device *self, int flags)
213 {
214 struct ath_cardbus_softc *csc = device_private(self);
215 struct ath_softc *sc = &csc->sc_ath;
216 struct cardbus_devfunc *ct = csc->sc_ct;
217 int rv;
218
219 #if defined(DIAGNOSTIC)
220 if (ct == NULL)
221 panic("%s: data structure lacks", sc->sc_dev.dv_xname);
222 #endif
223
224 rv = ath_detach(sc);
225 if (rv)
226 return (rv);
227
228 /*
229 * Unhook the interrupt handler.
230 */
231 if (csc->sc_ih != NULL)
232 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
233 csc->sc_ih = NULL;
234
235 /*
236 * Release bus space and close window.
237 */
238 Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA, (bus_space_tag_t) sc->sc_st,
239 (bus_space_handle_t)sc->sc_sh, csc->sc_mapsize);
240
241 return (0);
242 }
243
244 int
245 ath_cardbus_enable(struct ath_softc *sc)
246 {
247 struct ath_cardbus_softc *csc = (void *) sc;
248 cardbus_devfunc_t ct = csc->sc_ct;
249 cardbus_chipset_tag_t cc = ct->ct_cc;
250 cardbus_function_tag_t cf = ct->ct_cf;
251
252 /*
253 * Power on the socket.
254 */
255 Cardbus_function_enable(ct);
256
257 /*
258 * Set up the PCI configuration registers.
259 */
260 ath_cardbus_setup(csc);
261
262 /*
263 * Map and establish the interrupt.
264 */
265 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
266 ath_intr, sc);
267 if (csc->sc_ih == NULL) {
268 printf("%s: unable to establish interrupt at %d\n",
269 sc->sc_dev.dv_xname, csc->sc_intrline);
270 Cardbus_function_disable(csc->sc_ct);
271 return (1);
272 }
273 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
274 csc->sc_intrline);
275
276 return (0);
277 }
278
279 void
280 ath_cardbus_disable(struct ath_softc *sc)
281 {
282 struct ath_cardbus_softc *csc = (void *) sc;
283 cardbus_devfunc_t ct = csc->sc_ct;
284 cardbus_chipset_tag_t cc = ct->ct_cc;
285 cardbus_function_tag_t cf = ct->ct_cf;
286
287 /* Unhook the interrupt handler. */
288 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
289 csc->sc_ih = NULL;
290
291 #ifdef ath_powerdown
292 /* Power down the socket. */
293 Cardbus_function_disable(ct);
294 #endif /* ath_powerdown */
295 }
296
297 void
298 ath_cardbus_power(struct ath_softc *sc, int why)
299 {
300 struct ath_cardbus_softc *csc = (void *) sc;
301
302 printf("%s: ath_cardbus_power\n", sc->sc_dev.dv_xname);
303
304 if (why == PWR_RESUME) {
305 /*
306 * Give the PCI configuration registers a kick
307 * in the head.
308 */
309 #ifdef DIAGNOSTIC
310 if (ATH_IS_ENABLED(sc) == 0)
311 panic("ath_cardbus_power");
312 #endif
313 ath_cardbus_setup(csc);
314 }
315 }
316
317 void
318 ath_cardbus_setup(struct ath_cardbus_softc *csc)
319 {
320 struct ath_softc *sc = &csc->sc_ath;
321 cardbus_devfunc_t ct = csc->sc_ct;
322 cardbus_chipset_tag_t cc = ct->ct_cc;
323 cardbus_function_tag_t cf = ct->ct_cf;
324 pcireg_t reg;
325
326 (void)cardbus_setpowerstate(sc->sc_dev.dv_xname, ct, csc->sc_tag,
327 PCI_PWR_D0);
328
329 /* Program the BAR. */
330 cardbus_conf_write(cc, cf, csc->sc_tag, ATH_PCI_MMBA,
331 csc->sc_bar_val);
332
333 /* Make sure the right access type is on the CardBus bridge. */
334 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
335 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
336
337 /* Enable the appropriate bits in the PCI CSR. */
338 reg = cardbus_conf_read(cc, cf, csc->sc_tag,
339 CARDBUS_COMMAND_STATUS_REG);
340 reg |= CARDBUS_COMMAND_MASTER_ENABLE | CARDBUS_COMMAND_MEM_ENABLE;
341 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
342 reg);
343
344 /*
345 * Make sure the latency timer is set to some reasonable
346 * value.
347 */
348 reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
349 if (CARDBUS_LATTIMER(reg) < 0x20) {
350 reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
351 reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
352 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
353 }
354 }
355