if_awi_pcmcia.c revision 1.18.2.1 1 /* $NetBSD: if_awi_pcmcia.c,v 1.18.2.1 2001/08/24 00:10:26 nathanw 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 __P((struct device *, struct cfdata *, void *));
77 static void awi_pcmcia_attach __P((struct device *, struct device *, void *));
78 static int awi_pcmcia_detach __P((struct device *, int));
79 static int awi_pcmcia_enable __P((struct awi_softc *));
80 static void awi_pcmcia_disable __P((struct awi_softc *));
81 static void awi_pcmcia_powerhook __P((int, void *));
82
83 struct awi_pcmcia_softc {
84 struct awi_softc sc_awi; /* real "awi" softc */
85
86 /* PCMCIA-specific goo */
87 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
88 struct pcmcia_mem_handle sc_memh; /* PCMCIA memory space info */
89 int sc_io_window; /* our i/o window */
90 int sc_mem_window; /* our memory window */
91 struct pcmcia_function *sc_pf; /* our PCMCIA function */
92 void *sc_powerhook; /* power hook descriptor */
93 };
94
95 static int awi_pcmcia_find __P((struct awi_pcmcia_softc *,
96 struct pcmcia_attach_args *, struct pcmcia_config_entry *));
97
98 struct cfattach awi_pcmcia_ca = {
99 sizeof(struct awi_pcmcia_softc), awi_pcmcia_match, awi_pcmcia_attach,
100 awi_pcmcia_detach, awi_activate
101 };
102
103 static const struct awi_pcmcia_product {
104 u_int32_t app_vendor; /* vendor ID */
105 u_int32_t app_product; /* product ID */
106 const char *app_cisinfo[4]; /* CIS information */
107 const char *app_name; /* product name */
108 } awi_pcmcia_products[] = {
109 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_650,
110 PCMCIA_CIS_BAY_STACK_650, PCMCIA_STR_BAY_STACK_650 },
111
112 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_660,
113 PCMCIA_CIS_BAY_STACK_660, PCMCIA_STR_BAY_STACK_660 },
114
115 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_SURFER_PRO,
116 PCMCIA_CIS_BAY_SURFER_PRO, PCMCIA_STR_BAY_SURFER_PRO },
117
118 { PCMCIA_VENDOR_AMD, PCMCIA_PRODUCT_AMD_AM79C930,
119 PCMCIA_CIS_AMD_AM79C930, PCMCIA_STR_AMD_AM79C930 },
120
121 { PCMCIA_VENDOR_ICOM, PCMCIA_PRODUCT_ICOM_SL200,
122 PCMCIA_CIS_ICOM_SL200, PCMCIA_STR_ICOM_SL200 },
123
124 { PCMCIA_VENDOR_NOKIA, PCMCIA_PRODUCT_NOKIA_C020_WLAN,
125 PCMCIA_CIS_NOKIA_C020_WLAN, PCMCIA_STR_NOKIA_C020_WLAN },
126
127 { PCMCIA_VENDOR_FARALLON, PCMCIA_PRODUCT_FARALLON_SKYLINE,
128 PCMCIA_CIS_FARALLON_SKYLINE, PCMCIA_STR_FARALLON_SKYLINE },
129
130 { 0, 0,
131 { NULL, NULL, NULL, NULL }, NULL },
132 };
133
134 static const struct awi_pcmcia_product *
135 awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
136
137 static const struct awi_pcmcia_product *
138 awi_pcmcia_lookup(pa)
139 struct pcmcia_attach_args *pa;
140 {
141 const struct awi_pcmcia_product *app;
142
143 for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
144 /* match by vendor/product id */
145 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
146 pa->manufacturer == app->app_vendor &&
147 pa->product != PCMCIA_PRODUCT_INVALID &&
148 pa->product == app->app_product)
149 return (app);
150
151 /* match by CIS information */
152 if (pa->card->cis1_info[0] != NULL &&
153 app->app_cisinfo[0] != NULL &&
154 strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
155 pa->card->cis1_info[1] != NULL &&
156 app->app_cisinfo[1] != NULL &&
157 strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
158 return (app);
159 }
160
161 return (NULL);
162 }
163
164 static int
165 awi_pcmcia_enable(sc)
166 struct awi_softc *sc;
167 {
168 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
169 struct pcmcia_function *pf = psc->sc_pf;
170
171 /* establish the interrupt. */
172 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
173 if (sc->sc_ih == NULL) {
174 printf("%s: couldn't establish interrupt\n",
175 sc->sc_dev.dv_xname);
176 return (1);
177 }
178
179 if (pcmcia_function_enable(pf)) {
180 pcmcia_intr_disestablish(pf, sc->sc_ih);
181 return (1);
182 }
183 DELAY(1000);
184
185 return (0);
186 }
187
188 static void
189 awi_pcmcia_disable(sc)
190 struct awi_softc *sc;
191 {
192 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
193 struct pcmcia_function *pf = psc->sc_pf;
194
195 pcmcia_function_disable(pf);
196 pcmcia_intr_disestablish(pf, sc->sc_ih);
197 }
198
199 static int
200 awi_pcmcia_match(parent, match, aux)
201 struct device *parent;
202 struct cfdata *match;
203 void *aux;
204 {
205 struct pcmcia_attach_args *pa = aux;
206
207 if (awi_pcmcia_lookup(pa) != NULL)
208 return (1);
209
210 return (0);
211 }
212
213 static int
214 awi_pcmcia_find(psc, pa, cfe)
215 struct awi_pcmcia_softc *psc;
216 struct pcmcia_attach_args *pa;
217 struct pcmcia_config_entry *cfe;
218 {
219 struct awi_softc *sc = &psc->sc_awi;
220 int fail = 0;
221
222 /*
223 * see if we can read the firmware version sanely
224 * through the i/o ports.
225 * if not, try a different CIS string..
226 */
227 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
228 cfe->iospace[0].length, AM79C930_IO_ALIGN,
229 &psc->sc_pcioh) != 0)
230 goto fail;
231
232 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
233 &psc->sc_pcioh, &psc->sc_io_window))
234 goto fail_io_free;
235
236 /* Enable the card. */
237 pcmcia_function_init(psc->sc_pf, cfe);
238 if (pcmcia_function_enable(psc->sc_pf))
239 goto fail_io_unmap;
240
241 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
242 sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
243 sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
244 am79c930_chip_init(&sc->sc_chip, 0);
245
246 DELAY(1000);
247
248 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
249
250 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) == 0)
251 return (0);
252
253 fail++;
254 pcmcia_function_disable(psc->sc_pf);
255
256 fail_io_unmap:
257 fail++;
258 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
259
260 fail_io_free:
261 fail++;
262 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
263 fail:
264 fail++;
265 psc->sc_io_window = -1;
266 return (fail);
267 }
268
269 static void
270 awi_pcmcia_attach(parent, self, aux)
271 struct device *parent, *self;
272 void *aux;
273 {
274 struct awi_pcmcia_softc *psc = (void *)self;
275 struct awi_softc *sc = &psc->sc_awi;
276 const struct awi_pcmcia_product *app;
277 struct pcmcia_attach_args *pa = aux;
278 struct pcmcia_config_entry *cfe;
279 bus_addr_t memoff;
280
281 app = awi_pcmcia_lookup(pa);
282 if (app == NULL)
283 panic("awi_pcmcia_attach: impossible");
284
285 psc->sc_pf = pa->pf;
286
287 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
288 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
289 if (cfe->iftype != PCMCIA_IFTYPE_IO)
290 continue;
291 if (cfe->num_iospace < 1)
292 continue;
293 if (cfe->iospace[0].length < AM79C930_IO_SIZE)
294 continue;
295
296 if (awi_pcmcia_find(psc, pa, cfe) == 0)
297 break;
298 }
299 if (cfe == NULL) {
300 printf(": no suitable CIS info found\n");
301 goto no_config_entry;
302 }
303
304 sc->sc_enabled = 1;
305 printf(": %s\n", app->app_name);
306
307 psc->sc_mem_window = -1;
308 if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE,
309 &psc->sc_memh) != 0) {
310 printf("%s: unable to allocate memory space; using i/o only\n",
311 sc->sc_dev.dv_xname);
312 } else if (pcmcia_mem_map(psc->sc_pf,
313 PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
314 AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
315 printf("%s: unable to map memory space; using i/o only\n",
316 sc->sc_dev.dv_xname);
317 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
318 } else {
319 sc->sc_chip.sc_memt = psc->sc_memh.memt;
320 sc->sc_chip.sc_memh = psc->sc_memh.memh;
321 am79c930_chip_init(&sc->sc_chip, 1);
322 }
323
324 sc->sc_enable = awi_pcmcia_enable;
325 sc->sc_disable = awi_pcmcia_disable;
326
327 /* establish the interrupt. */
328 sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, awi_intr, sc);
329 if (sc->sc_ih == NULL) {
330 printf("%s: couldn't establish interrupt\n",
331 sc->sc_dev.dv_xname);
332 goto no_interrupt;
333 }
334 sc->sc_ifp = &sc->sc_ec.ec_if;
335 sc->sc_cansleep = 1;
336
337 if (awi_attach(sc) != 0) {
338 printf("%s: failed to attach controller\n",
339 sc->sc_dev.dv_xname);
340 goto attach_failed;
341 }
342 psc->sc_powerhook = powerhook_establish(awi_pcmcia_powerhook, psc);
343
344 sc->sc_enabled = 0;
345 /* disable device and disestablish the interrupt */
346 awi_pcmcia_disable(sc);
347 return;
348
349 attach_failed:
350 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
351
352 no_interrupt:
353 /* Unmap our memory window and space */
354 if (psc->sc_mem_window != -1) {
355 pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
356 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
357 }
358
359 /* Unmap our i/o window and space */
360 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
361 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
362
363 /* Disable the function */
364 pcmcia_function_disable(psc->sc_pf);
365
366 no_config_entry:
367 psc->sc_io_window = -1;
368 }
369
370
371 static int
372 awi_pcmcia_detach(self, flags)
373 struct device *self;
374 int flags;
375 {
376 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
377 int error;
378
379 if (psc->sc_io_window == -1)
380 /* Nothing to detach. */
381 return (0);
382
383 if (psc->sc_powerhook != NULL)
384 powerhook_disestablish(psc->sc_powerhook);
385
386 error = awi_detach(&psc->sc_awi);
387 if (error != 0)
388 return (error);
389
390 /* Unmap our memory window and free memory space */
391 if (psc->sc_mem_window != -1) {
392 pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
393 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
394 }
395
396 /* Unmap our i/o window. */
397 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
398
399 /* Free our i/o space. */
400 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
401 return (0);
402 }
403
404 static void
405 awi_pcmcia_powerhook(why, arg)
406 int why;
407 void *arg;
408 {
409 struct awi_pcmcia_softc *psc = arg;
410 struct awi_softc *sc = &psc->sc_awi;
411
412 awi_power(sc, why);
413 }
414