if_wi_pci.c revision 1.27 1 /* $NetBSD: if_wi_pci.c,v 1.27 2003/10/13 08:07:22 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Hideaki Imaizumi <hiddy (at) sfc.wide.ad.jp>
9 * and Ichiro FUKUHARA (ichiro (at) ichiro.org).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * PCI bus front-end for the Intersil PCI WaveLan.
42 * Works with Prism2.5 Mini-PCI wavelan.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: if_wi_pci.c,v 1.27 2003/10/13 08:07:22 dyoung Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/syslog.h>
52 #include <sys/socket.h>
53 #include <sys/device.h>
54 #include <sys/callout.h>
55
56 #include <net/if.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
59
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_compat.h>
62
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65
66 #include <dev/pci/pcireg.h>
67 #include <dev/pci/pcivar.h>
68 #include <dev/pci/pcidevs.h>
69
70 #include <dev/ic/wi_ieee.h>
71 #include <dev/ic/wireg.h>
72 #include <dev/ic/wivar.h>
73
74 #define WI_PCI_CBMA 0x10 /* Configuration Base Memory Address */
75 #define WI_PCI_PLX_LOMEM 0x10 /* PLX chip membase */
76 #define WI_PCI_PLX_LOIO 0x14 /* PLX chip iobase */
77 #define WI_PCI_LOMEM 0x18 /* ISA membase */
78 #define WI_PCI_LOIO 0x1C /* ISA iobase */
79
80 #define CHIP_PLX_OTHER 0x01
81 #define CHIP_PLX_9052 0x02
82
83 #define WI_PLX_COR_OFFSET 0x3E0
84 #define WI_PLX_COR_VALUE 0x41
85
86 struct wi_pci_softc {
87 struct wi_softc psc_wi; /* real "wi" softc */
88
89 /* PCI-specific goo */
90 pci_intr_handle_t psc_ih;
91 pci_chipset_tag_t psc_pc;
92
93 void *sc_powerhook; /* power hook descriptor */
94 };
95
96 static int wi_pci_match __P((struct device *, struct cfdata *, void *));
97 static void wi_pci_attach __P((struct device *, struct device *, void *));
98 static int wi_pci_enable __P((struct wi_softc *));
99 static void wi_pci_disable __P((struct wi_softc *));
100 static void wi_pci_reset __P((struct wi_softc *));
101 static void wi_pci_powerhook __P((int, void *));
102
103 static const struct wi_pci_product
104 *wi_pci_lookup __P((struct pci_attach_args *));
105
106 CFATTACH_DECL(wi_pci, sizeof(struct wi_pci_softc),
107 wi_pci_match, wi_pci_attach, NULL, NULL);
108
109 const struct wi_pci_product {
110 pci_vendor_id_t wpp_vendor; /* vendor ID */
111 pci_product_id_t wpp_product; /* product ID */
112 const char *wpp_name; /* product name */
113 int wpp_plx; /* uses PLX chip */
114 } wi_pci_products[] = {
115 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P,
116 NULL, CHIP_PLX_OTHER },
117 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P02,
118 NULL, CHIP_PLX_OTHER },
119 { PCI_VENDOR_EUMITCOM, PCI_PRODUCT_EUMITCOM_WL11000P,
120 NULL, CHIP_PLX_OTHER },
121 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE777A,
122 NULL, CHIP_PLX_OTHER },
123 { PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_MA301,
124 NULL, CHIP_PLX_OTHER },
125 { PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
126 "Intersil Prism2.5", 0 },
127 { PCI_VENDOR_NDC, PCI_PRODUCT_NDC_NCP130,
128 NULL, CHIP_PLX_9052 },
129 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_2415,
130 NULL, CHIP_PLX_OTHER },
131 { 0, 0,
132 NULL, 0},
133 };
134
135 static int
136 wi_pci_enable(sc)
137 struct wi_softc *sc;
138 {
139 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
140
141 /* establish the interrupt. */
142 sc->sc_ih = pci_intr_establish(psc->psc_pc,
143 psc->psc_ih, IPL_NET, wi_intr, sc);
144 if (sc->sc_ih == NULL) {
145 printf("%s: couldn't establish interrupt\n",
146 sc->sc_dev.dv_xname);
147 return (EIO);
148 }
149
150 /* reset HFA3842 MAC core */
151 wi_pci_reset(sc);
152
153 return (0);
154 }
155
156 static void
157 wi_pci_disable(sc)
158 struct wi_softc *sc;
159 {
160 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
161
162 pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
163 }
164
165 static void
166 wi_pci_reset(sc)
167 struct wi_softc *sc;
168 {
169 int i, secs, usecs;
170
171 bus_space_write_2(sc->sc_iot, sc->sc_ioh,
172 WI_PCI_COR, WI_COR_SOFT_RESET);
173 DELAY(250*1000); /* 1/4 second */
174
175 bus_space_write_2(sc->sc_iot, sc->sc_ioh,
176 WI_PCI_COR, WI_COR_CLEAR);
177 DELAY(500*1000); /* 1/2 second */
178
179 /* wait 2 seconds for firmware to complete initialization. */
180
181 for (i = 200000; i--; DELAY(10))
182 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
183 break;
184
185 if (i < 0) {
186 printf("%s: PCI reset timed out\n", sc->sc_dev.dv_xname);
187 } else if (sc->sc_if.if_flags & IFF_DEBUG) {
188 usecs = (200000 - i) * 10;
189 secs = usecs / 1000000;
190 usecs %= 1000000;
191
192 printf("%s: PCI reset in %d.%06d seconds\n",
193 sc->sc_dev.dv_xname, secs, usecs);
194 }
195
196 return;
197 }
198
199 static const struct wi_pci_product *
200 wi_pci_lookup(pa)
201 struct pci_attach_args *pa;
202 {
203 const struct wi_pci_product *wpp;
204
205 for (wpp = wi_pci_products; wpp->wpp_vendor != 0; wpp++) {
206 if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
207 PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
208 return (wpp);
209 }
210 return (NULL);
211 }
212
213 static int
214 wi_pci_match(parent, match, aux)
215 struct device *parent;
216 struct cfdata *match;
217 void *aux;
218 {
219 struct pci_attach_args *pa = aux;
220
221 if (wi_pci_lookup(pa) != NULL)
222 return (1);
223 return (0);
224 }
225
226 static void
227 wi_pci_attach(parent, self, aux)
228 struct device *parent, *self;
229 void *aux;
230 {
231 struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
232 struct wi_softc *sc = &psc->psc_wi;
233 struct pci_attach_args *pa = aux;
234 pci_chipset_tag_t pc = pa->pa_pc;
235 const char *intrstr;
236 const struct wi_pci_product *wpp;
237 pci_intr_handle_t ih;
238 bus_space_tag_t memt, iot, plxt;
239 bus_space_handle_t memh, ioh, plxh;
240
241 psc->psc_pc = pc;
242
243 wpp = wi_pci_lookup(pa);
244 #ifdef DIAGNOSTIC
245 if (wpp == NULL) {
246 printf("\n");
247 panic("wi_pci_attach: impossible");
248 }
249 #endif
250
251 if (wpp->wpp_plx) {
252 /* Map memory and I/O registers. */
253 if (pci_mapreg_map(pa, WI_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
254 &memt, &memh, NULL, NULL) != 0) {
255 printf(": can't map mem space\n");
256 return;
257 }
258 if (pci_mapreg_map(pa, WI_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
259 &iot, &ioh, NULL, NULL) != 0) {
260 printf(": can't map I/O space\n");
261 return;
262 }
263
264 if (wpp->wpp_plx == CHIP_PLX_OTHER) {
265 /* The PLX 9052 doesn't have IO at 0x14. Perhaps
266 other chips have, so we'll make this conditional. */
267 if (pci_mapreg_map(pa, WI_PCI_PLX_LOIO,
268 PCI_MAPREG_TYPE_IO, 0, &plxt,
269 &plxh, NULL, NULL) != 0) {
270 printf(": can't map PLX\n");
271 return;
272 }
273 }
274 } else {
275 if (pci_mapreg_map(pa, WI_PCI_CBMA,
276 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
277 0, &iot, &ioh, NULL, NULL) != 0) {
278 printf(": can't map mem space\n");
279 return;
280 }
281
282 memt = iot;
283 memh = ioh;
284 sc->sc_pci = 1;
285 }
286
287 if (wpp->wpp_name != NULL) {
288 printf(": %s Wireless Lan\n", wpp->wpp_name);
289 } else {
290 char devinfo[256];
291
292 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
293 printf(": %s (rev. 0x%02x)\n", devinfo,
294 PCI_REVISION(pa->pa_class));
295 }
296
297 sc->sc_enabled = 1;
298 sc->sc_enable = wi_pci_enable;
299 sc->sc_disable = wi_pci_disable;
300
301 sc->sc_iot = iot;
302 sc->sc_ioh = ioh;
303 /* Make sure interrupts are disabled. */
304 CSR_WRITE_2(sc, WI_INT_EN, 0);
305 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
306
307 if (wpp->wpp_plx == CHIP_PLX_OTHER) {
308 uint32_t command;
309 #define WI_LOCAL_INTCSR 0x4c
310 #define WI_LOCAL_INTEN 0x40 /* poke this into INTCSR */
311
312 command = bus_space_read_4(plxt, plxh, WI_LOCAL_INTCSR);
313 command |= WI_LOCAL_INTEN;
314 bus_space_write_4(plxt, plxh, WI_LOCAL_INTCSR, command);
315 }
316
317 /* Map and establish the interrupt. */
318 if (pci_intr_map(pa, &ih)) {
319 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
320 return;
321 }
322 intrstr = pci_intr_string(pc, ih);
323
324 psc->psc_ih = ih;
325 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
326 if (sc->sc_ih == NULL) {
327 printf("%s: couldn't establish interrupt",
328 sc->sc_dev.dv_xname);
329 if (intrstr != NULL)
330 printf(" at %s", intrstr);
331 printf("\n");
332 return;
333 }
334
335 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
336
337 if (wpp->wpp_plx) {
338 /*
339 * Setup the PLX chip for level interrupts and config index 1
340 * XXX - should really reset the PLX chip too.
341 */
342 bus_space_write_1(memt, memh,
343 WI_PLX_COR_OFFSET, WI_PLX_COR_VALUE);
344 } else {
345 /* reset HFA3842 MAC core */
346 wi_pci_reset(sc);
347 }
348
349 printf("%s:", sc->sc_dev.dv_xname);
350 if (wi_attach(sc) != 0) {
351 printf("%s: failed to attach controller\n",
352 sc->sc_dev.dv_xname);
353 pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
354 return;
355 }
356
357 sc->sc_reset = wi_pci_reset;
358
359 /* Add a suspend hook to restore PCI config state */
360 psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
361 if (psc->sc_powerhook == NULL)
362 printf ("%s: WARNING: unable to establish pci power hook\n",
363 sc->sc_dev.dv_xname);
364 }
365
366 static void
367 wi_pci_powerhook(why, arg)
368 int why;
369 void *arg;
370 {
371 struct wi_pci_softc *psc = arg;
372 struct wi_softc *sc = &psc->psc_wi;
373
374 wi_power(sc, why);
375 }
376