if_wi_pci.c revision 1.2.2.3 1 1.2.2.3 jdolecek /* $NetBSD: if_wi_pci.c,v 1.2.2.3 2002/02/11 20:10:00 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.3 jdolecek __KERNEL_RCSID(0, "$NetBSD: if_wi_pci.c,v 1.2.2.3 2002/02/11 20:10:00 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.2 thorpej static void wi_pci_powerhook __P((int, void *));
95 1.2.2.2 thorpej
96 1.2.2.2 thorpej static const struct wi_pci_product
97 1.2.2.2 thorpej *wi_pci_lookup __P((struct pci_attach_args *));
98 1.2.2.2 thorpej
99 1.2.2.2 thorpej struct cfattach wi_pci_ca = {
100 1.2.2.2 thorpej sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
101 1.2.2.2 thorpej };
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.2 thorpej static const struct wi_pci_product *
156 1.2.2.2 thorpej wi_pci_lookup(pa)
157 1.2.2.2 thorpej struct pci_attach_args *pa;
158 1.2.2.2 thorpej {
159 1.2.2.2 thorpej const struct wi_pci_product *wpp;
160 1.2.2.2 thorpej
161 1.2.2.3 jdolecek for (wpp = wi_pci_products; wpp->wpp_vendor != 0; wpp++) {
162 1.2.2.2 thorpej if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
163 1.2.2.2 thorpej PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
164 1.2.2.2 thorpej return (wpp);
165 1.2.2.2 thorpej }
166 1.2.2.2 thorpej return (NULL);
167 1.2.2.2 thorpej }
168 1.2.2.2 thorpej
169 1.2.2.2 thorpej static int
170 1.2.2.2 thorpej wi_pci_match(parent, match, aux)
171 1.2.2.2 thorpej struct device *parent;
172 1.2.2.2 thorpej struct cfdata *match;
173 1.2.2.2 thorpej void *aux;
174 1.2.2.2 thorpej {
175 1.2.2.2 thorpej struct pci_attach_args *pa = aux;
176 1.2.2.2 thorpej
177 1.2.2.2 thorpej if (wi_pci_lookup(pa) != NULL)
178 1.2.2.2 thorpej return (1);
179 1.2.2.2 thorpej return (0);
180 1.2.2.2 thorpej }
181 1.2.2.2 thorpej
182 1.2.2.2 thorpej static void
183 1.2.2.2 thorpej wi_pci_attach(parent, self, aux)
184 1.2.2.2 thorpej struct device *parent, *self;
185 1.2.2.2 thorpej void *aux;
186 1.2.2.2 thorpej {
187 1.2.2.2 thorpej struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
188 1.2.2.2 thorpej struct wi_softc *sc = &psc->psc_wi;
189 1.2.2.2 thorpej struct pci_attach_args *pa = aux;
190 1.2.2.2 thorpej pci_chipset_tag_t pc = pa->pa_pc;
191 1.2.2.2 thorpej const char *intrstr;
192 1.2.2.2 thorpej const struct wi_pci_product *wpp;
193 1.2.2.2 thorpej pci_intr_handle_t ih;
194 1.2.2.3 jdolecek bus_space_tag_t memt, iot;
195 1.2.2.3 jdolecek bus_space_handle_t memh, ioh;
196 1.2.2.2 thorpej
197 1.2.2.2 thorpej psc->psc_pa = pa;
198 1.2.2.2 thorpej
199 1.2.2.2 thorpej wpp = wi_pci_lookup(pa);
200 1.2.2.3 jdolecek #ifdef DIAGNOSTIC
201 1.2.2.2 thorpej if (wpp == NULL) {
202 1.2.2.2 thorpej printf("\n");
203 1.2.2.2 thorpej panic("wi_pci_attach: impossible");
204 1.2.2.2 thorpej }
205 1.2.2.3 jdolecek #endif
206 1.2.2.2 thorpej
207 1.2.2.3 jdolecek if (wpp->wpp_plx) {
208 1.2.2.3 jdolecek /* Map memory and I/O registers. */
209 1.2.2.3 jdolecek if (pci_mapreg_map(pa, WI_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
210 1.2.2.3 jdolecek &memt, &memh, NULL, NULL) != 0) {
211 1.2.2.3 jdolecek printf(": can't map mem space\n");
212 1.2.2.3 jdolecek return;
213 1.2.2.3 jdolecek }
214 1.2.2.3 jdolecek if (pci_mapreg_map(pa, WI_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
215 1.2.2.3 jdolecek &iot, &ioh, NULL, NULL) != 0) {
216 1.2.2.3 jdolecek printf(": can't map I/O space\n");
217 1.2.2.3 jdolecek return;
218 1.2.2.3 jdolecek }
219 1.2.2.3 jdolecek } else {
220 1.2.2.3 jdolecek if (pci_mapreg_map(pa, WI_PCI_CBMA,
221 1.2.2.3 jdolecek PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
222 1.2.2.3 jdolecek 0, &iot, &ioh, NULL, NULL) != 0) {
223 1.2.2.3 jdolecek printf(": can't map mem space\n");
224 1.2.2.3 jdolecek return;
225 1.2.2.3 jdolecek }
226 1.2.2.3 jdolecek
227 1.2.2.3 jdolecek memt = iot;
228 1.2.2.3 jdolecek memh = ioh;
229 1.2.2.3 jdolecek sc->sc_pci = 1;
230 1.2.2.3 jdolecek }
231 1.2.2.3 jdolecek
232 1.2.2.3 jdolecek if (wpp->wpp_name != NULL) {
233 1.2.2.3 jdolecek printf(": %s Wireless Lan\n", wpp->wpp_name);
234 1.2.2.3 jdolecek } else {
235 1.2.2.3 jdolecek char devinfo[256];
236 1.2.2.3 jdolecek
237 1.2.2.3 jdolecek pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
238 1.2.2.3 jdolecek printf(": %s (rev. 0x%02x)\n", devinfo,
239 1.2.2.3 jdolecek PCI_REVISION(pa->pa_class));
240 1.2.2.3 jdolecek }
241 1.2.2.2 thorpej
242 1.2.2.2 thorpej sc->sc_enabled = 1;
243 1.2.2.2 thorpej sc->sc_enable = wi_pci_enable;
244 1.2.2.2 thorpej sc->sc_disable = wi_pci_disable;
245 1.2.2.2 thorpej
246 1.2.2.2 thorpej /* Enable the card. */
247 1.2.2.2 thorpej pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
248 1.2.2.2 thorpej pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
249 1.2.2.2 thorpej PCI_COMMAND_MASTER_ENABLE);
250 1.2.2.2 thorpej
251 1.2.2.3 jdolecek sc->sc_iot = iot;
252 1.2.2.3 jdolecek sc->sc_ioh = ioh;
253 1.2.2.3 jdolecek /* Make sure interrupts are disabled. */
254 1.2.2.3 jdolecek CSR_WRITE_2(sc, WI_INT_EN, 0);
255 1.2.2.3 jdolecek CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
256 1.2.2.2 thorpej
257 1.2.2.2 thorpej /* Map and establish the interrupt. */
258 1.2.2.2 thorpej if (pci_intr_map(pa, &ih)) {
259 1.2.2.2 thorpej printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
260 1.2.2.2 thorpej return;
261 1.2.2.2 thorpej }
262 1.2.2.2 thorpej intrstr = pci_intr_string(pc, ih);
263 1.2.2.2 thorpej
264 1.2.2.2 thorpej psc->psc_ih = ih;
265 1.2.2.2 thorpej sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
266 1.2.2.2 thorpej if (sc->sc_ih == NULL) {
267 1.2.2.2 thorpej printf("%s: couldn't establish interrupt",
268 1.2.2.2 thorpej sc->sc_dev.dv_xname);
269 1.2.2.2 thorpej if (intrstr != NULL)
270 1.2.2.2 thorpej printf(" at %s", intrstr);
271 1.2.2.2 thorpej printf("\n");
272 1.2.2.2 thorpej return;
273 1.2.2.2 thorpej }
274 1.2.2.2 thorpej
275 1.2.2.2 thorpej printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
276 1.2.2.2 thorpej
277 1.2.2.3 jdolecek if (wpp->wpp_plx) {
278 1.2.2.3 jdolecek /*
279 1.2.2.3 jdolecek * Setup the PLX chip for level interrupts and config index 1
280 1.2.2.3 jdolecek * XXX - should really reset the PLX chip too.
281 1.2.2.3 jdolecek */
282 1.2.2.3 jdolecek bus_space_write_1(memt, memh,
283 1.2.2.3 jdolecek WI_PLX_COR_OFFSET, WI_PLX_COR_VALUE);
284 1.2.2.3 jdolecek } else {
285 1.2.2.3 jdolecek /* reset HFA3842 MAC core */
286 1.2.2.3 jdolecek wi_pci_reset(sc);
287 1.2.2.3 jdolecek }
288 1.2.2.2 thorpej
289 1.2.2.3 jdolecek printf("%s:", sc->sc_dev.dv_xname);
290 1.2.2.2 thorpej sc->sc_ifp = &sc->sc_ethercom.ec_if;
291 1.2.2.2 thorpej if (wi_attach(sc) != 0) {
292 1.2.2.3 jdolecek printf(" failed to attach controller\n");
293 1.2.2.2 thorpej pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
294 1.2.2.2 thorpej return;
295 1.2.2.2 thorpej }
296 1.2.2.2 thorpej
297 1.2.2.2 thorpej /* Add a suspend hook to restore PCI config state */
298 1.2.2.2 thorpej psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
299 1.2.2.2 thorpej if (psc->sc_powerhook == NULL)
300 1.2.2.2 thorpej printf ("%s: WARNING: unable to establish pci power hook\n",
301 1.2.2.2 thorpej sc->sc_dev.dv_xname);
302 1.2.2.2 thorpej }
303 1.2.2.2 thorpej
304 1.2.2.2 thorpej static void
305 1.2.2.2 thorpej wi_pci_powerhook(why, arg)
306 1.2.2.2 thorpej int why;
307 1.2.2.2 thorpej void *arg;
308 1.2.2.2 thorpej {
309 1.2.2.2 thorpej struct wi_pci_softc *psc = arg;
310 1.2.2.2 thorpej struct wi_softc *sc = &psc->psc_wi;
311 1.2.2.2 thorpej
312 1.2.2.2 thorpej wi_power(sc, why);
313 1.2.2.2 thorpej }
314