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