if_atw_cardbus.c revision 1.23 1 /* $NetBSD: if_atw_cardbus.c,v 1.23 2008/06/24 19:44:52 drochner Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center. This code was adapted for the ADMtek ADM8211
10 * by David Young.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * CardBus bus front-end for the ADMtek ADM8211 802.11 MAC/BBP driver.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_atw_cardbus.c,v 1.23 2008/06/24 19:44:52 drochner Exp $");
40
41 #include "opt_inet.h"
42 #include "bpfilter.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53
54 #include <machine/endian.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_ether.h>
60
61 #include <net80211/ieee80211_netbsd.h>
62 #include <net80211/ieee80211_radiotap.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
75 #include <sys/bus.h>
76 #include <sys/intr.h>
77
78 #include <dev/ic/atwreg.h>
79 #include <dev/ic/rf3000reg.h>
80 #include <dev/ic/si4136reg.h>
81 #include <dev/ic/atwvar.h>
82
83 #include <dev/pci/pcivar.h>
84 #include <dev/pci/pcireg.h>
85 #include <dev/pci/pcidevs.h>
86
87 #include <dev/cardbus/cardbusvar.h>
88 #include <dev/pci/pcidevs.h>
89
90 /*
91 * PCI configuration space registers used by the ADM8211.
92 */
93 #define ATW_PCI_IOBA 0x10 /* i/o mapped base */
94 #define ATW_PCI_MMBA 0x14 /* memory mapped base */
95
96 struct atw_cardbus_softc {
97 struct atw_softc sc_atw; /* real ADM8211 softc */
98
99 /* CardBus-specific goo. */
100 void *sc_ih; /* interrupt handle */
101 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
102 cardbustag_t sc_tag; /* our CardBus tag */
103 int sc_csr; /* CSR bits */
104 bus_size_t sc_mapsize; /* the size of mapped bus space
105 region */
106
107 int sc_cben; /* CardBus enables */
108 int sc_bar_reg; /* which BAR to use */
109 pcireg_t sc_bar_val; /* value of the BAR */
110
111 cardbus_intr_line_t sc_intrline; /* interrupt line */
112 };
113
114 int atw_cardbus_match(struct device *, struct cfdata *, void *);
115 void atw_cardbus_attach(struct device *, struct device *, void *);
116 int atw_cardbus_detach(struct device *, int);
117
118 CFATTACH_DECL(atw_cardbus, sizeof(struct atw_cardbus_softc),
119 atw_cardbus_match, atw_cardbus_attach, atw_cardbus_detach, atw_activate);
120
121 void atw_cardbus_setup(struct atw_cardbus_softc *);
122
123 int atw_cardbus_enable(struct atw_softc *);
124 void atw_cardbus_disable(struct atw_softc *);
125
126 static void atw_cardbus_intr_ack(struct atw_softc *);
127
128 const struct atw_cardbus_product *atw_cardbus_lookup
129 (const struct cardbus_attach_args *);
130
131 const struct atw_cardbus_product {
132 u_int32_t acp_vendor; /* PCI vendor ID */
133 u_int32_t acp_product; /* PCI product ID */
134 const char *acp_product_name;
135 } atw_cardbus_products[] = {
136 { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_ADM8211,
137 "ADMtek ADM8211 802.11 MAC/BBP" },
138
139 { 0, 0, NULL },
140 };
141
142 const struct atw_cardbus_product *
143 atw_cardbus_lookup(const struct cardbus_attach_args *ca)
144 {
145 const struct atw_cardbus_product *acp;
146
147 for (acp = atw_cardbus_products;
148 acp->acp_product_name != NULL;
149 acp++) {
150 if (PCI_VENDOR(ca->ca_id) == acp->acp_vendor &&
151 PCI_PRODUCT(ca->ca_id) == acp->acp_product)
152 return (acp);
153 }
154 return (NULL);
155 }
156
157 int
158 atw_cardbus_match(struct device *parent, struct cfdata *match,
159 void *aux)
160 {
161 struct cardbus_attach_args *ca = aux;
162
163 if (atw_cardbus_lookup(ca) != NULL)
164 return (1);
165
166 return (0);
167 }
168
169 void
170 atw_cardbus_attach(struct device *parent, struct device *self,
171 void *aux)
172 {
173 struct atw_cardbus_softc *csc = device_private(self);
174 struct atw_softc *sc = &csc->sc_atw;
175 struct cardbus_attach_args *ca = aux;
176 cardbus_devfunc_t ct = ca->ca_ct;
177 const struct atw_cardbus_product *acp;
178 bus_addr_t adr;
179
180 sc->sc_dmat = ca->ca_dmat;
181 csc->sc_ct = ct;
182 csc->sc_tag = ca->ca_tag;
183
184 acp = atw_cardbus_lookup(ca);
185 if (acp == NULL) {
186 printf("\n");
187 panic("atw_cardbus_attach: impossible");
188 }
189
190 /*
191 * Power management hooks.
192 */
193 sc->sc_enable = atw_cardbus_enable;
194 sc->sc_disable = atw_cardbus_disable;
195
196 sc->sc_intr_ack = atw_cardbus_intr_ack;
197
198 /* Get revision info. */
199 sc->sc_rev = PCI_REVISION(ca->ca_class);
200
201 printf(": %s, revision %d.%d\n", acp->acp_product_name,
202 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
203
204 #if 0
205 printf("%s: signature %08x\n", device_xname(&sc->sc_dev),
206 (rev >> 4) & 0xf, rev & 0xf,
207 cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag, 0x80));
208 #endif
209
210 /*
211 * Map the device.
212 */
213 csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
214 if (Cardbus_mapreg_map(ct, ATW_PCI_MMBA,
215 CARDBUS_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
216 &csc->sc_mapsize) == 0) {
217 #if 0
218 printf("%s: atw_cardbus_attach mapped %d bytes mem space\n",
219 device_xname(&sc->sc_dev), csc->sc_mapsize);
220 #endif
221 #if rbus
222 #else
223 (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
224 #endif
225 csc->sc_cben = CARDBUS_MEM_ENABLE;
226 csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
227 csc->sc_bar_reg = ATW_PCI_MMBA;
228 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
229 } else if (Cardbus_mapreg_map(ct, ATW_PCI_IOBA,
230 CARDBUS_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
231 &csc->sc_mapsize) == 0) {
232 #if 0
233 printf("%s: atw_cardbus_attach mapped %d bytes I/O space\n",
234 device_xname(&sc->sc_dev), csc->sc_mapsize);
235 #endif
236 #if rbus
237 #else
238 (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
239 #endif
240 csc->sc_cben = CARDBUS_IO_ENABLE;
241 csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
242 csc->sc_bar_reg = ATW_PCI_IOBA;
243 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
244 } else {
245 aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
246 return;
247 }
248
249 /*
250 * Bring the chip out of powersave mode and initialize the
251 * configuration registers.
252 */
253 atw_cardbus_setup(csc);
254
255 /* Remember which interrupt line. */
256 csc->sc_intrline = ca->ca_intrline;
257
258 #if 0
259 /*
260 * The CardBus cards will make it to store-and-forward mode as
261 * soon as you put them under any kind of load, so just start
262 * out there.
263 */
264 sc->sc_txthresh = 3; /* TBD name constant */
265 #endif
266
267 /*
268 * Finish off the attach.
269 */
270 atw_attach(sc);
271
272 ATW_WRITE(sc, ATW_FER, ATW_FER_INTR);
273
274 /*
275 * Power down the socket.
276 */
277 Cardbus_function_disable(csc->sc_ct);
278 }
279
280 static void
281 atw_cardbus_intr_ack(struct atw_softc *sc)
282 {
283 ATW_WRITE(sc, ATW_FER, ATW_FER_INTR);
284 }
285
286 int
287 atw_cardbus_detach(struct device *self, int flags)
288 {
289 struct atw_cardbus_softc *csc = device_private(self);
290 struct atw_softc *sc = &csc->sc_atw;
291 struct cardbus_devfunc *ct = csc->sc_ct;
292 int rv;
293
294 #if defined(DIAGNOSTIC)
295 if (ct == NULL)
296 panic("%s: data structure lacks", device_xname(&sc->sc_dev));
297 #endif
298
299 rv = atw_detach(sc);
300 if (rv)
301 return (rv);
302
303 /*
304 * Unhook the interrupt handler.
305 */
306 if (csc->sc_ih != NULL)
307 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
308
309 /*
310 * Release bus space and close window.
311 */
312 if (csc->sc_bar_reg != 0)
313 Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
314 sc->sc_st, sc->sc_sh, csc->sc_mapsize);
315
316 return (0);
317 }
318
319 int
320 atw_cardbus_enable(struct atw_softc *sc)
321 {
322 struct atw_cardbus_softc *csc = (void *) sc;
323 cardbus_devfunc_t ct = csc->sc_ct;
324 cardbus_chipset_tag_t cc = ct->ct_cc;
325 cardbus_function_tag_t cf = ct->ct_cf;
326
327 /*
328 * Power on the socket.
329 */
330 Cardbus_function_enable(ct);
331
332 /*
333 * Set up the PCI configuration registers.
334 */
335 atw_cardbus_setup(csc);
336
337 /*
338 * Map and establish the interrupt.
339 */
340 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
341 atw_intr, sc);
342 if (csc->sc_ih == NULL) {
343 aprint_error_dev(&sc->sc_dev,
344 "unable to establish interrupt\n");
345 Cardbus_function_disable(csc->sc_ct);
346 return (1);
347 }
348
349 return (0);
350 }
351
352 void
353 atw_cardbus_disable(struct atw_softc *sc)
354 {
355 struct atw_cardbus_softc *csc = (void *) sc;
356 cardbus_devfunc_t ct = csc->sc_ct;
357 cardbus_chipset_tag_t cc = ct->ct_cc;
358 cardbus_function_tag_t cf = ct->ct_cf;
359
360 /* Unhook the interrupt handler. */
361 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
362 csc->sc_ih = NULL;
363
364 /* Power down the socket. */
365 Cardbus_function_disable(ct);
366 }
367
368 void
369 atw_cardbus_setup(struct atw_cardbus_softc *csc)
370 {
371 cardbus_devfunc_t ct = csc->sc_ct;
372 cardbus_chipset_tag_t cc = ct->ct_cc;
373 cardbus_function_tag_t cf = ct->ct_cf;
374 pcireg_t reg;
375
376 (void)cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0);
377
378 /* Program the BAR. */
379 cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
380 csc->sc_bar_val);
381
382 /* Make sure the right access type is on the CardBus bridge. */
383 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
384 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
385
386 /* Enable the appropriate bits in the PCI CSR. */
387 reg = cardbus_conf_read(cc, cf, csc->sc_tag,
388 CARDBUS_COMMAND_STATUS_REG);
389 reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
390 reg |= csc->sc_csr;
391 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
392 reg);
393
394 /*
395 * Make sure the latency timer is set to some reasonable
396 * value.
397 */
398 reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
399 if (CARDBUS_LATTIMER(reg) < 0x20) {
400 reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
401 reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
402 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
403 }
404 }
405