if_awi_pcmcia.c revision 1.20 1 /* $NetBSD: if_awi_pcmcia.c,v 1.20 2001/09/18 09:10:00 onoe Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
41 * based on the AMD 79c930 802.11 controller chip.
42 *
43 * This attachment can probably be trivally adapted for other FH and
44 * DS cards based on the same chipset.
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/errno.h>
53 #include <sys/syslog.h>
54 #include <sys/select.h>
55 #include <sys/device.h>
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_ether.h>
60 #include <net/if_media.h>
61 #include <net/if_ieee80211.h>
62
63 #include <machine/cpu.h>
64 #include <machine/bus.h>
65 #include <machine/intr.h>
66
67 #include <dev/ic/am79c930reg.h>
68 #include <dev/ic/am79c930var.h>
69 #include <dev/ic/awireg.h>
70 #include <dev/ic/awivar.h>
71
72 #include <dev/pcmcia/pcmciareg.h>
73 #include <dev/pcmcia/pcmciavar.h>
74 #include <dev/pcmcia/pcmciadevs.h>
75
76 static int awi_pcmcia_match(struct device *, struct cfdata *, void *);
77 static void awi_pcmcia_attach(struct device *, struct device *, void *);
78 static int awi_pcmcia_detach(struct device *, int);
79 static int awi_pcmcia_enable(struct awi_softc *);
80 static void awi_pcmcia_disable(struct awi_softc *);
81
82 struct awi_pcmcia_softc {
83 struct awi_softc sc_awi; /* real "awi" softc */
84
85 /* PCMCIA-specific goo */
86 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
87 struct pcmcia_mem_handle sc_memh; /* PCMCIA memory space info */
88 int sc_io_window; /* our i/o window */
89 int sc_mem_window; /* our memory window */
90 struct pcmcia_function *sc_pf; /* our PCMCIA function */
91 void *sc_ih; /* interrupt handler */
92 };
93
94 static int awi_pcmcia_find __P((struct awi_pcmcia_softc *,
95 struct pcmcia_attach_args *, struct pcmcia_config_entry *));
96
97 struct cfattach awi_pcmcia_ca = {
98 sizeof(struct awi_pcmcia_softc), awi_pcmcia_match, awi_pcmcia_attach,
99 awi_pcmcia_detach, awi_activate
100 };
101
102 static const struct awi_pcmcia_product {
103 u_int32_t app_vendor; /* vendor ID */
104 u_int32_t app_product; /* product ID */
105 const char *app_cisinfo[4]; /* CIS information */
106 const char *app_name; /* product name */
107 } awi_pcmcia_products[] = {
108 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_650,
109 PCMCIA_CIS_BAY_STACK_650, PCMCIA_STR_BAY_STACK_650 },
110
111 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_660,
112 PCMCIA_CIS_BAY_STACK_660, PCMCIA_STR_BAY_STACK_660 },
113
114 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_SURFER_PRO,
115 PCMCIA_CIS_BAY_SURFER_PRO, PCMCIA_STR_BAY_SURFER_PRO },
116
117 { PCMCIA_VENDOR_AMD, PCMCIA_PRODUCT_AMD_AM79C930,
118 PCMCIA_CIS_AMD_AM79C930, PCMCIA_STR_AMD_AM79C930 },
119
120 { PCMCIA_VENDOR_ICOM, PCMCIA_PRODUCT_ICOM_SL200,
121 PCMCIA_CIS_ICOM_SL200, PCMCIA_STR_ICOM_SL200 },
122
123 { PCMCIA_VENDOR_NOKIA, PCMCIA_PRODUCT_NOKIA_C020_WLAN,
124 PCMCIA_CIS_NOKIA_C020_WLAN, PCMCIA_STR_NOKIA_C020_WLAN },
125
126 { PCMCIA_VENDOR_FARALLON, PCMCIA_PRODUCT_FARALLON_SKYLINE,
127 PCMCIA_CIS_FARALLON_SKYLINE, PCMCIA_STR_FARALLON_SKYLINE },
128
129 { 0, 0,
130 { NULL, NULL, NULL, NULL }, NULL },
131 };
132
133 static const struct awi_pcmcia_product *
134 awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
135
136 static const struct awi_pcmcia_product *
137 awi_pcmcia_lookup(pa)
138 struct pcmcia_attach_args *pa;
139 {
140 const struct awi_pcmcia_product *app;
141
142 for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
143 /* match by vendor/product id */
144 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
145 pa->manufacturer == app->app_vendor &&
146 pa->product != PCMCIA_PRODUCT_INVALID &&
147 pa->product == app->app_product)
148 return (app);
149
150 /* match by CIS information */
151 if (pa->card->cis1_info[0] != NULL &&
152 app->app_cisinfo[0] != NULL &&
153 strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
154 pa->card->cis1_info[1] != NULL &&
155 app->app_cisinfo[1] != NULL &&
156 strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
157 return (app);
158 }
159
160 return (NULL);
161 }
162
163 static int
164 awi_pcmcia_enable(sc)
165 struct awi_softc *sc;
166 {
167 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
168 struct pcmcia_function *pf = psc->sc_pf;
169
170 /* establish the interrupt. */
171 psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
172 if (psc->sc_ih == NULL) {
173 printf("%s: couldn't establish interrupt\n",
174 sc->sc_dev.dv_xname);
175 return (1);
176 }
177
178 if (pcmcia_function_enable(pf)) {
179 pcmcia_intr_disestablish(pf, psc->sc_ih);
180 return (1);
181 }
182 DELAY(1000);
183
184 return (0);
185 }
186
187 static void
188 awi_pcmcia_disable(sc)
189 struct awi_softc *sc;
190 {
191 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
192 struct pcmcia_function *pf = psc->sc_pf;
193
194 pcmcia_function_disable(pf);
195 pcmcia_intr_disestablish(pf, psc->sc_ih);
196 }
197
198 static int
199 awi_pcmcia_match(parent, match, aux)
200 struct device *parent;
201 struct cfdata *match;
202 void *aux;
203 {
204 struct pcmcia_attach_args *pa = aux;
205
206 if (awi_pcmcia_lookup(pa) != NULL)
207 return (1);
208
209 return (0);
210 }
211
212 static int
213 awi_pcmcia_find(psc, pa, cfe)
214 struct awi_pcmcia_softc *psc;
215 struct pcmcia_attach_args *pa;
216 struct pcmcia_config_entry *cfe;
217 {
218 struct awi_softc *sc = &psc->sc_awi;
219 int fail = 0;
220
221 /*
222 * see if we can read the firmware version sanely
223 * through the i/o ports.
224 * if not, try a different CIS string..
225 */
226 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
227 cfe->iospace[0].length, AM79C930_IO_ALIGN,
228 &psc->sc_pcioh) != 0)
229 goto fail;
230
231 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
232 &psc->sc_pcioh, &psc->sc_io_window))
233 goto fail_io_free;
234
235 /* Enable the card. */
236 pcmcia_function_init(psc->sc_pf, cfe);
237 if (pcmcia_function_enable(psc->sc_pf))
238 goto fail_io_unmap;
239
240 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
241 sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
242 sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
243 am79c930_chip_init(&sc->sc_chip, 0);
244
245 DELAY(1000);
246
247 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
248
249 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) == 0)
250 return (0);
251
252 fail++;
253 pcmcia_function_disable(psc->sc_pf);
254
255 fail_io_unmap:
256 fail++;
257 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
258
259 fail_io_free:
260 fail++;
261 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
262 fail:
263 fail++;
264 psc->sc_io_window = -1;
265 return (fail);
266 }
267
268 static void
269 awi_pcmcia_attach(parent, self, aux)
270 struct device *parent, *self;
271 void *aux;
272 {
273 struct awi_pcmcia_softc *psc = (void *)self;
274 struct awi_softc *sc = &psc->sc_awi;
275 const struct awi_pcmcia_product *app;
276 struct pcmcia_attach_args *pa = aux;
277 struct pcmcia_config_entry *cfe;
278 bus_addr_t memoff;
279
280 app = awi_pcmcia_lookup(pa);
281 if (app == NULL)
282 panic("awi_pcmcia_attach: impossible");
283
284 psc->sc_pf = pa->pf;
285
286 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
287 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
288 if (cfe->iftype != PCMCIA_IFTYPE_IO)
289 continue;
290 if (cfe->num_iospace < 1)
291 continue;
292 if (cfe->iospace[0].length < AM79C930_IO_SIZE)
293 continue;
294
295 if (awi_pcmcia_find(psc, pa, cfe) == 0)
296 break;
297 }
298 if (cfe == NULL) {
299 printf(": no suitable CIS info found\n");
300 goto no_config_entry;
301 }
302
303 sc->sc_enabled = 1;
304 printf(": %s\n", app->app_name);
305
306 psc->sc_mem_window = -1;
307 if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE,
308 &psc->sc_memh) != 0) {
309 printf("%s: unable to allocate memory space; using i/o only\n",
310 sc->sc_dev.dv_xname);
311 } else if (pcmcia_mem_map(psc->sc_pf,
312 PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
313 AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
314 printf("%s: unable to map memory space; using i/o only\n",
315 sc->sc_dev.dv_xname);
316 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
317 } else {
318 sc->sc_chip.sc_memt = psc->sc_memh.memt;
319 sc->sc_chip.sc_memh = psc->sc_memh.memh;
320 am79c930_chip_init(&sc->sc_chip, 1);
321 }
322
323 sc->sc_enable = awi_pcmcia_enable;
324 sc->sc_disable = awi_pcmcia_disable;
325
326 /* establish the interrupt. */
327 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, awi_intr, sc);
328 if (psc->sc_ih == NULL) {
329 printf("%s: couldn't establish interrupt\n",
330 sc->sc_dev.dv_xname);
331 goto no_interrupt;
332 }
333 sc->sc_cansleep = 1;
334
335 if (awi_attach(sc) != 0) {
336 printf("%s: failed to attach controller\n",
337 sc->sc_dev.dv_xname);
338 goto attach_failed;
339 }
340
341 sc->sc_enabled = 0;
342 /* disable device and disestablish the interrupt */
343 awi_pcmcia_disable(sc);
344 return;
345
346 attach_failed:
347 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
348
349 no_interrupt:
350 /* Unmap our memory window and space */
351 if (psc->sc_mem_window != -1) {
352 pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
353 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
354 }
355
356 /* Unmap our i/o window and space */
357 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
358 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
359
360 /* Disable the function */
361 pcmcia_function_disable(psc->sc_pf);
362
363 no_config_entry:
364 psc->sc_io_window = -1;
365 }
366
367
368 static int
369 awi_pcmcia_detach(self, flags)
370 struct device *self;
371 int flags;
372 {
373 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
374 int error;
375
376 if (psc->sc_io_window == -1)
377 /* Nothing to detach. */
378 return (0);
379
380 error = awi_detach(&psc->sc_awi);
381 if (error != 0)
382 return (error);
383
384 /* Unmap our memory window and free memory space */
385 if (psc->sc_mem_window != -1) {
386 pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
387 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
388 }
389
390 /* Unmap our i/o window. */
391 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
392
393 /* Free our i/o space. */
394 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
395 return (0);
396 }
397