if_wi_pci.c revision 1.5 1 /* $NetBSD: if_wi_pci.c,v 1.5 2002/01/15 13:50:59 augustss 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.5 2002/01/15 13:50:59 augustss 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 WI_PLX_COR_OFFSET 0x3E0
78 #define WI_PLX_COR_VALUE 0x41
79
80 struct wi_pci_softc {
81 struct wi_softc psc_wi; /* real "wi" softc */
82
83 /* PCI-specific goo */
84 pci_intr_handle_t psc_ih;
85 struct pci_attach_args *psc_pa;
86
87 void *sc_powerhook; /* power hook descriptor */
88 };
89
90 static int wi_pci_match __P((struct device *, struct cfdata *, void *));
91 static void wi_pci_attach __P((struct device *, struct device *, void *));
92 static int wi_pci_enable __P((struct wi_softc *));
93 static void wi_pci_disable __P((struct wi_softc *));
94 static void wi_pci_powerhook __P((int, void *));
95
96 static const struct wi_pci_product
97 *wi_pci_lookup __P((struct pci_attach_args *));
98
99 struct cfattach wi_pci_ca = {
100 sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
101 };
102
103 const struct wi_pci_product {
104 pci_vendor_id_t wpp_vendor; /* vendor ID */
105 pci_product_id_t wpp_product; /* product ID */
106 const char *wpp_name; /* product name */
107 int wpp_plx; /* uses PLX chip */
108 } wi_pci_products[] = {
109 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P,
110 NULL, 1 },
111 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P02,
112 NULL, 1 },
113 { PCI_VENDOR_EUMITCOM, PCI_PRODUCT_EUMITCOM_WL11000P,
114 NULL, 1 },
115 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE777A,
116 NULL, 1 },
117 { PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_MA301,
118 NULL, 1 },
119 { PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
120 "Intersil Prism2.5", 0 },
121 { 0, 0,
122 NULL, 0},
123 };
124
125 static int
126 wi_pci_enable(sc)
127 struct wi_softc *sc;
128 {
129 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
130
131 /* establish the interrupt. */
132 sc->sc_ih = pci_intr_establish(psc->psc_pa->pa_pc,
133 psc->psc_ih, IPL_NET, wi_intr, sc);
134 if (sc->sc_ih == NULL) {
135 printf("%s: couldn't establish interrupt\n",
136 sc->sc_dev.dv_xname);
137 return (EIO);
138 }
139
140 /* reset HFA3842 MAC core */
141 wi_pci_reset(sc);
142
143 return (0);
144 }
145
146 static void
147 wi_pci_disable(sc)
148 struct wi_softc *sc;
149 {
150 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
151
152 pci_intr_disestablish(psc->psc_pa->pa_pc, sc->sc_ih);
153 }
154
155 static const struct wi_pci_product *
156 wi_pci_lookup(pa)
157 struct pci_attach_args *pa;
158 {
159 const struct wi_pci_product *wpp;
160
161 for (wpp = wi_pci_products; wpp->wpp_vendor != 0; wpp++) {
162 if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
163 PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
164 return (wpp);
165 }
166 return (NULL);
167 }
168
169 static int
170 wi_pci_match(parent, match, aux)
171 struct device *parent;
172 struct cfdata *match;
173 void *aux;
174 {
175 struct pci_attach_args *pa = aux;
176
177 if (wi_pci_lookup(pa) != NULL)
178 return (1);
179 return (0);
180 }
181
182 static void
183 wi_pci_attach(parent, self, aux)
184 struct device *parent, *self;
185 void *aux;
186 {
187 struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
188 struct wi_softc *sc = &psc->psc_wi;
189 struct pci_attach_args *pa = aux;
190 pci_chipset_tag_t pc = pa->pa_pc;
191 const char *intrstr;
192 const struct wi_pci_product *wpp;
193 pci_intr_handle_t ih;
194 bus_space_tag_t memt, iot;
195 bus_space_handle_t memh, ioh;
196
197 psc->psc_pa = pa;
198
199 wpp = wi_pci_lookup(pa);
200 #ifdef DIAGNOSTIC
201 if (wpp == NULL) {
202 printf("\n");
203 panic("wi_pci_attach: impossible");
204 }
205 #endif
206
207 if (wpp->wpp_plx) {
208 /* Map memory and I/O registers. */
209 if (pci_mapreg_map(pa, WI_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
210 &memt, &memh, NULL, NULL) != 0) {
211 printf(": can't map mem space\n");
212 return;
213 }
214 if (pci_mapreg_map(pa, WI_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
215 &iot, &ioh, NULL, NULL) != 0) {
216 printf(": can't map I/O space\n");
217 return;
218 }
219 } else {
220 if (pci_mapreg_map(pa, WI_PCI_CBMA,
221 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
222 0, &iot, &ioh, NULL, NULL) != 0) {
223 printf(": can't map mem space\n");
224 return;
225 }
226
227 memt = iot;
228 memh = ioh;
229 sc->sc_pci = 1;
230 }
231
232 if (wpp->wpp_name != NULL) {
233 printf(": %s Wireless Lan\n", wpp->wpp_name);
234 } else {
235 char devinfo[256];
236
237 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
238 printf(": %s (rev. 0x%02x)\n", devinfo,
239 PCI_REVISION(pa->pa_class));
240 }
241
242 sc->sc_enabled = 1;
243 sc->sc_enable = wi_pci_enable;
244 sc->sc_disable = wi_pci_disable;
245
246 /* Enable the card. */
247 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
248 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
249 PCI_COMMAND_MASTER_ENABLE);
250
251 sc->sc_iot = iot;
252 sc->sc_ioh = ioh;
253 /* Make sure interrupts are disabled. */
254 CSR_WRITE_2(sc, WI_INT_EN, 0);
255 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
256
257 /* Map and establish the interrupt. */
258 if (pci_intr_map(pa, &ih)) {
259 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
260 return;
261 }
262 intrstr = pci_intr_string(pc, ih);
263
264 psc->psc_ih = ih;
265 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
266 if (sc->sc_ih == NULL) {
267 printf("%s: couldn't establish interrupt",
268 sc->sc_dev.dv_xname);
269 if (intrstr != NULL)
270 printf(" at %s", intrstr);
271 printf("\n");
272 return;
273 }
274
275 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
276
277 if (wpp->wpp_plx) {
278 /*
279 * Setup the PLX chip for level interrupts and config index 1
280 * XXX - should really reset the PLX chip too.
281 */
282 bus_space_write_1(memt, memh,
283 WI_PLX_COR_OFFSET, WI_PLX_COR_VALUE);
284 } else {
285 /* reset HFA3842 MAC core */
286 wi_pci_reset(sc);
287 }
288
289 printf("%s:", sc->sc_dev.dv_xname);
290 sc->sc_ifp = &sc->sc_ethercom.ec_if;
291 if (wi_attach(sc) != 0) {
292 printf(" failed to attach controller\n");
293 pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
294 return;
295 }
296
297 /* Add a suspend hook to restore PCI config state */
298 psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
299 if (psc->sc_powerhook == NULL)
300 printf ("%s: WARNING: unable to establish pci power hook\n",
301 sc->sc_dev.dv_xname);
302 }
303
304 static void
305 wi_pci_powerhook(why, arg)
306 int why;
307 void *arg;
308 {
309 struct wi_pci_softc *psc = arg;
310 struct wi_softc *sc = &psc->psc_wi;
311
312 wi_power(sc, why);
313 }
314