if_wi_pci.c revision 1.7.4.1 1 /* $NetBSD: if_wi_pci.c,v 1.7.4.1 2003/07/28 19:06:35 he 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.7.4.1 2003/07/28 19:06:35 he 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 #include <net/if_ieee80211.h>
60
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63
64 #include <dev/pci/pcireg.h>
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcidevs.h>
67
68 #include <dev/ic/wi_ieee.h>
69 #include <dev/ic/wireg.h>
70 #include <dev/ic/wivar.h>
71
72 #define WI_PCI_PLX_LOMEM 0x10 /* PLX chip membase */
73 #define WI_PCI_PLX_LOIO 0x14 /* PLX chip iobase */
74 #define WI_PCI_LOMEM 0x18 /* ISA membase */
75 #define WI_PCI_LOIO 0x1C /* ISA iobase */
76
77 #define CHIP_PLX_OTHER 0x01
78 #define CHIP_PLX_9052 0x02
79
80 #define WI_PLX_COR_OFFSET 0x3E0
81 #define WI_PLX_COR_VALUE 0x41
82
83 struct wi_pci_softc {
84 struct wi_softc psc_wi; /* real "wi" softc */
85
86 /* PCI-specific goo */
87 pci_intr_handle_t psc_ih;
88 struct pci_attach_args *psc_pa;
89
90 void *sc_powerhook; /* power hook descriptor */
91 };
92
93 static int wi_pci_match __P((struct device *, struct cfdata *, void *));
94 static void wi_pci_attach __P((struct device *, struct device *, void *));
95 static int wi_pci_enable __P((struct wi_softc *));
96 static void wi_pci_disable __P((struct wi_softc *));
97 static void wi_pci_reset __P((struct wi_softc *));
98 static void wi_pci_powerhook __P((int, void *));
99
100 static const struct wi_pci_product
101 *wi_pci_lookup __P((struct pci_attach_args *));
102
103 struct cfattach wi_pci_ca = {
104 sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
105 };
106
107 const struct wi_pci_product {
108 pci_vendor_id_t wpp_vendor; /* vendor ID */
109 pci_product_id_t wpp_product; /* product ID */
110 const char *wpp_name; /* product name */
111 int wpp_plx; /* uses PLX chip */
112 } wi_pci_products[] = {
113 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P,
114 NULL, CHIP_PLX_OTHER },
115 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P02,
116 NULL, CHIP_PLX_OTHER },
117 { PCI_VENDOR_EUMITCOM, PCI_PRODUCT_EUMITCOM_WL11000P,
118 NULL, CHIP_PLX_OTHER },
119 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE777A,
120 NULL, CHIP_PLX_OTHER },
121 { PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_MA301,
122 NULL, CHIP_PLX_OTHER },
123 { PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
124 "Intersil Prism2.5", 0 },
125 { 0, 0,
126 NULL, 0},
127 };
128
129 static int
130 wi_pci_enable(sc)
131 struct wi_softc *sc;
132 {
133 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
134
135 /* establish the interrupt. */
136 sc->sc_ih = pci_intr_establish(psc->psc_pa->pa_pc,
137 psc->psc_ih, IPL_NET, wi_intr, sc);
138 if (sc->sc_ih == NULL) {
139 printf("%s: couldn't establish interrupt\n",
140 sc->sc_dev.dv_xname);
141 return (EIO);
142 }
143
144 /* reset HFA3842 MAC core */
145 wi_pci_reset(sc);
146
147 return (0);
148 }
149
150 static void
151 wi_pci_disable(sc)
152 struct wi_softc *sc;
153 {
154 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
155
156 pci_intr_disestablish(psc->psc_pa->pa_pc, sc->sc_ih);
157 }
158
159 static void
160 wi_pci_reset(sc)
161 struct wi_softc *sc;
162 {
163 bus_space_write_2(sc->sc_iot, sc->sc_ioh,
164 WI_PCI_COR, WI_PCI_SOFT_RESET);
165 DELAY(100*1000); /* 100 m sec */
166
167 bus_space_write_2(sc->sc_iot, sc->sc_ioh, WI_PCI_COR, 0x0);
168 DELAY(100*1000); /* 100 m sec */
169
170 return;
171 }
172
173 static const struct wi_pci_product *
174 wi_pci_lookup(pa)
175 struct pci_attach_args *pa;
176 {
177 const struct wi_pci_product *wpp;
178
179 for (wpp = wi_pci_products; wpp->wpp_vendor != 0; wpp++) {
180 if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
181 PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
182 return (wpp);
183 }
184 return (NULL);
185 }
186
187 static int
188 wi_pci_match(parent, match, aux)
189 struct device *parent;
190 struct cfdata *match;
191 void *aux;
192 {
193 struct pci_attach_args *pa = aux;
194
195 if (wi_pci_lookup(pa) != NULL)
196 return (1);
197 return (0);
198 }
199
200 static void
201 wi_pci_attach(parent, self, aux)
202 struct device *parent, *self;
203 void *aux;
204 {
205 struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
206 struct wi_softc *sc = &psc->psc_wi;
207 struct pci_attach_args *pa = aux;
208 pci_chipset_tag_t pc = pa->pa_pc;
209 const char *intrstr;
210 const struct wi_pci_product *wpp;
211 pci_intr_handle_t ih;
212 bus_space_tag_t memt, iot, plxt;
213 bus_space_handle_t memh, ioh, plxh;
214
215 psc->psc_pa = pa;
216
217 wpp = wi_pci_lookup(pa);
218 #ifdef DIAGNOSTIC
219 if (wpp == NULL) {
220 printf("\n");
221 panic("wi_pci_attach: impossible");
222 }
223 #endif
224
225 if (wpp->wpp_plx) {
226 /* Map memory and I/O registers. */
227 if (pci_mapreg_map(pa, WI_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
228 &memt, &memh, NULL, NULL) != 0) {
229 printf(": can't map mem space\n");
230 return;
231 }
232 if (pci_mapreg_map(pa, WI_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
233 &iot, &ioh, NULL, NULL) != 0) {
234 printf(": can't map I/O space\n");
235 return;
236 }
237
238 if (wpp->wpp_plx == CHIP_PLX_OTHER) {
239 /* The PLX 9052 doesn't have IO at 0x14. Perhaps
240 other chips have, so we'll make this conditional. */
241 if (pci_mapreg_map(pa, WI_PCI_PLX_LOIO,
242 PCI_MAPREG_TYPE_IO, 0, &plxt,
243 &plxh, NULL, NULL) != 0) {
244 printf(": can't map PLX\n");
245 return;
246 }
247 }
248 } else {
249 if (pci_mapreg_map(pa, WI_PCI_CBMA,
250 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
251 0, &iot, &ioh, NULL, NULL) != 0) {
252 printf(": can't map mem space\n");
253 return;
254 }
255
256 memt = iot;
257 memh = ioh;
258 sc->sc_pci = 1;
259 }
260
261 if (wpp->wpp_name != NULL) {
262 printf(": %s Wireless Lan\n", wpp->wpp_name);
263 } else {
264 char devinfo[256];
265
266 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
267 printf(": %s (rev. 0x%02x)\n", devinfo,
268 PCI_REVISION(pa->pa_class));
269 }
270
271 sc->sc_enabled = 1;
272 sc->sc_enable = wi_pci_enable;
273 sc->sc_disable = wi_pci_disable;
274
275 /* Enable the card. */
276 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
277 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
278 PCI_COMMAND_MASTER_ENABLE);
279
280 sc->sc_iot = iot;
281 sc->sc_ioh = ioh;
282 /* Make sure interrupts are disabled. */
283 CSR_WRITE_2(sc, WI_INT_EN, 0);
284 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
285
286 if (wpp->wpp_plx == CHIP_PLX_OTHER) {
287 uint32_t command;
288 #define WI_LOCAL_INTCSR 0x4c
289 #define WI_LOCAL_INTEN 0x40 /* poke this into INTCSR */
290
291 command = bus_space_read_4(plxt, plxh, WI_LOCAL_INTCSR);
292 command |= WI_LOCAL_INTEN;
293 bus_space_write_4(plxt, plxh, WI_LOCAL_INTCSR, command);
294 }
295
296 /* Map and establish the interrupt. */
297 if (pci_intr_map(pa, &ih)) {
298 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
299 return;
300 }
301 intrstr = pci_intr_string(pc, ih);
302
303 psc->psc_ih = ih;
304 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
305 if (sc->sc_ih == NULL) {
306 printf("%s: couldn't establish interrupt",
307 sc->sc_dev.dv_xname);
308 if (intrstr != NULL)
309 printf(" at %s", intrstr);
310 printf("\n");
311 return;
312 }
313
314 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
315
316 if (wpp->wpp_plx) {
317 /*
318 * Setup the PLX chip for level interrupts and config index 1
319 * XXX - should really reset the PLX chip too.
320 */
321 bus_space_write_1(memt, memh,
322 WI_PLX_COR_OFFSET, WI_PLX_COR_VALUE);
323 } else {
324 /* reset HFA3842 MAC core */
325 wi_pci_reset(sc);
326 }
327
328 printf("%s:", sc->sc_dev.dv_xname);
329 sc->sc_ifp = &sc->sc_ethercom.ec_if;
330 if (wi_attach(sc) != 0) {
331 printf("%s: failed to attach controller\n",
332 sc->sc_dev.dv_xname);
333 pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
334 return;
335 }
336
337 /* Add a suspend hook to restore PCI config state */
338 psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
339 if (psc->sc_powerhook == NULL)
340 printf ("%s: WARNING: unable to establish pci power hook\n",
341 sc->sc_dev.dv_xname);
342 }
343
344 static void
345 wi_pci_powerhook(why, arg)
346 int why;
347 void *arg;
348 {
349 struct wi_pci_softc *psc = arg;
350 struct wi_softc *sc = &psc->psc_wi;
351
352 wi_power(sc, why);
353 }
354