if_wi_pci.c revision 1.1.2.2 1 1.1.2.2 nathanw /* $NetBSD: if_wi_pci.c,v 1.1.2.2 2001/10/22 20:41:24 nathanw Exp $ */
2 1.1.2.2 nathanw
3 1.1.2.2 nathanw /*-
4 1.1.2.2 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.2.2 nathanw * All rights reserved.
6 1.1.2.2 nathanw *
7 1.1.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 nathanw * by Hideaki Imaizumi <hiddy (at) sfc.wide.ad.jp>
9 1.1.2.2 nathanw * and Ichiro FUKUHARA (ichiro (at) ichiro.org).
10 1.1.2.2 nathanw *
11 1.1.2.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.1.2.2 nathanw * modification, are permitted provided that the following conditions
13 1.1.2.2 nathanw * are met:
14 1.1.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.1.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.1.2.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.1.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
20 1.1.2.2 nathanw * must display the following acknowledgement:
21 1.1.2.2 nathanw * This product includes software developed by the NetBSD
22 1.1.2.2 nathanw * Foundation, Inc. and its contributors.
23 1.1.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1.2.2 nathanw * contributors may be used to endorse or promote products derived
25 1.1.2.2 nathanw * from this software without specific prior written permission.
26 1.1.2.2 nathanw *
27 1.1.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
38 1.1.2.2 nathanw */
39 1.1.2.2 nathanw
40 1.1.2.2 nathanw /*
41 1.1.2.2 nathanw * PCI bus front-end for the Intersil PCI WaveLan.
42 1.1.2.2 nathanw * Works with Prism2.5 Mini-PCI wavelan.
43 1.1.2.2 nathanw */
44 1.1.2.2 nathanw
45 1.1.2.2 nathanw #include <sys/param.h>
46 1.1.2.2 nathanw #include <sys/systm.h>
47 1.1.2.2 nathanw #include <sys/mbuf.h>
48 1.1.2.2 nathanw #include <sys/syslog.h>
49 1.1.2.2 nathanw #include <sys/socket.h>
50 1.1.2.2 nathanw #include <sys/device.h>
51 1.1.2.2 nathanw #include <sys/callout.h>
52 1.1.2.2 nathanw
53 1.1.2.2 nathanw #include <net/if.h>
54 1.1.2.2 nathanw #include <net/if_ether.h>
55 1.1.2.2 nathanw #include <net/if_media.h>
56 1.1.2.2 nathanw #include <net/if_ieee80211.h>
57 1.1.2.2 nathanw
58 1.1.2.2 nathanw #include <machine/bus.h>
59 1.1.2.2 nathanw #include <machine/intr.h>
60 1.1.2.2 nathanw
61 1.1.2.2 nathanw #include <dev/pci/pcireg.h>
62 1.1.2.2 nathanw #include <dev/pci/pcivar.h>
63 1.1.2.2 nathanw #include <dev/pci/pcidevs.h>
64 1.1.2.2 nathanw
65 1.1.2.2 nathanw #include <dev/ic/wi_ieee.h>
66 1.1.2.2 nathanw #include <dev/ic/wireg.h>
67 1.1.2.2 nathanw #include <dev/ic/wivar.h>
68 1.1.2.2 nathanw
69 1.1.2.2 nathanw struct wi_pci_softc {
70 1.1.2.2 nathanw struct wi_softc psc_wi; /* real "wi" softc */
71 1.1.2.2 nathanw
72 1.1.2.2 nathanw /* PCI-specific goo */
73 1.1.2.2 nathanw pci_intr_handle_t psc_ih;
74 1.1.2.2 nathanw struct pci_attach_args *psc_pa;
75 1.1.2.2 nathanw
76 1.1.2.2 nathanw void *sc_powerhook; /* power hook descriptor */
77 1.1.2.2 nathanw };
78 1.1.2.2 nathanw
79 1.1.2.2 nathanw static int wi_pci_match __P((struct device *, struct cfdata *, void *));
80 1.1.2.2 nathanw static void wi_pci_attach __P((struct device *, struct device *, void *));
81 1.1.2.2 nathanw static int wi_pci_enable __P((struct wi_softc *));
82 1.1.2.2 nathanw static void wi_pci_disable __P((struct wi_softc *));
83 1.1.2.2 nathanw static void wi_pci_powerhook __P((int, void *));
84 1.1.2.2 nathanw
85 1.1.2.2 nathanw static const struct wi_pci_product
86 1.1.2.2 nathanw *wi_pci_lookup __P((struct pci_attach_args *));
87 1.1.2.2 nathanw
88 1.1.2.2 nathanw struct cfattach wi_pci_ca = {
89 1.1.2.2 nathanw sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
90 1.1.2.2 nathanw };
91 1.1.2.2 nathanw
92 1.1.2.2 nathanw const struct wi_pci_product {
93 1.1.2.2 nathanw pci_vendor_id_t wpp_vendor; /* vendor ID */
94 1.1.2.2 nathanw pci_product_id_t wpp_product; /* product ID */
95 1.1.2.2 nathanw const char *wpp_name; /* product name */
96 1.1.2.2 nathanw } wi_pci_products[] = {
97 1.1.2.2 nathanw { PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
98 1.1.2.2 nathanw "Intersil Prism2.5" },
99 1.1.2.2 nathanw { 0, 0,
100 1.1.2.2 nathanw NULL},
101 1.1.2.2 nathanw };
102 1.1.2.2 nathanw
103 1.1.2.2 nathanw static int
104 1.1.2.2 nathanw wi_pci_enable(sc)
105 1.1.2.2 nathanw struct wi_softc *sc;
106 1.1.2.2 nathanw {
107 1.1.2.2 nathanw struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
108 1.1.2.2 nathanw
109 1.1.2.2 nathanw /* establish the interrupt. */
110 1.1.2.2 nathanw sc->sc_ih = pci_intr_establish(psc->psc_pa->pa_pc,
111 1.1.2.2 nathanw psc->psc_ih, IPL_NET, wi_intr, sc);
112 1.1.2.2 nathanw if (sc->sc_ih == NULL) {
113 1.1.2.2 nathanw printf("%s: couldn't establish interrupt\n",
114 1.1.2.2 nathanw sc->sc_dev.dv_xname);
115 1.1.2.2 nathanw return (EIO);
116 1.1.2.2 nathanw }
117 1.1.2.2 nathanw sc->sc_pci = 1;
118 1.1.2.2 nathanw
119 1.1.2.2 nathanw /* reset HFA3842 MAC core */
120 1.1.2.2 nathanw wi_pci_reset(sc);
121 1.1.2.2 nathanw
122 1.1.2.2 nathanw return (0);
123 1.1.2.2 nathanw }
124 1.1.2.2 nathanw
125 1.1.2.2 nathanw static void
126 1.1.2.2 nathanw wi_pci_disable(sc)
127 1.1.2.2 nathanw struct wi_softc *sc;
128 1.1.2.2 nathanw {
129 1.1.2.2 nathanw struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
130 1.1.2.2 nathanw
131 1.1.2.2 nathanw pci_intr_disestablish(psc->psc_pa->pa_pc, sc->sc_ih);
132 1.1.2.2 nathanw }
133 1.1.2.2 nathanw
134 1.1.2.2 nathanw static const struct wi_pci_product *
135 1.1.2.2 nathanw wi_pci_lookup(pa)
136 1.1.2.2 nathanw struct pci_attach_args *pa;
137 1.1.2.2 nathanw {
138 1.1.2.2 nathanw const struct wi_pci_product *wpp;
139 1.1.2.2 nathanw
140 1.1.2.2 nathanw for (wpp = wi_pci_products; wpp->wpp_name != NULL; wpp++) {
141 1.1.2.2 nathanw if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
142 1.1.2.2 nathanw PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
143 1.1.2.2 nathanw return (wpp);
144 1.1.2.2 nathanw }
145 1.1.2.2 nathanw return (NULL);
146 1.1.2.2 nathanw }
147 1.1.2.2 nathanw
148 1.1.2.2 nathanw static int
149 1.1.2.2 nathanw wi_pci_match(parent, match, aux)
150 1.1.2.2 nathanw struct device *parent;
151 1.1.2.2 nathanw struct cfdata *match;
152 1.1.2.2 nathanw void *aux;
153 1.1.2.2 nathanw {
154 1.1.2.2 nathanw struct pci_attach_args *pa = aux;
155 1.1.2.2 nathanw
156 1.1.2.2 nathanw if (wi_pci_lookup(pa) != NULL)
157 1.1.2.2 nathanw return (1);
158 1.1.2.2 nathanw return (0);
159 1.1.2.2 nathanw }
160 1.1.2.2 nathanw
161 1.1.2.2 nathanw static void
162 1.1.2.2 nathanw wi_pci_attach(parent, self, aux)
163 1.1.2.2 nathanw struct device *parent, *self;
164 1.1.2.2 nathanw void *aux;
165 1.1.2.2 nathanw {
166 1.1.2.2 nathanw struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
167 1.1.2.2 nathanw struct wi_softc *sc = &psc->psc_wi;
168 1.1.2.2 nathanw struct pci_attach_args *pa = aux;
169 1.1.2.2 nathanw pci_chipset_tag_t pc = pa->pa_pc;
170 1.1.2.2 nathanw const char *intrstr;
171 1.1.2.2 nathanw const struct wi_pci_product *wpp;
172 1.1.2.2 nathanw pci_intr_handle_t ih;
173 1.1.2.2 nathanw bus_space_tag_t memt;
174 1.1.2.2 nathanw bus_space_handle_t memh;
175 1.1.2.2 nathanw int memh_valid;
176 1.1.2.2 nathanw bus_addr_t busbase;
177 1.1.2.2 nathanw bus_size_t bussize;
178 1.1.2.2 nathanw
179 1.1.2.2 nathanw psc->psc_pa = pa;
180 1.1.2.2 nathanw
181 1.1.2.2 nathanw memh_valid = !pci_mapreg_map(pa, WI_PCI_CBMA,
182 1.1.2.2 nathanw PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
183 1.1.2.2 nathanw &memt, &memh, &busbase, &bussize);
184 1.1.2.2 nathanw
185 1.1.2.2 nathanw if(memh_valid){
186 1.1.2.2 nathanw sc->sc_iot = memt;
187 1.1.2.2 nathanw sc->sc_ioh = memh;
188 1.1.2.2 nathanw }
189 1.1.2.2 nathanw else {
190 1.1.2.2 nathanw printf(": unable to map device registers\n");
191 1.1.2.2 nathanw return;
192 1.1.2.2 nathanw }
193 1.1.2.2 nathanw
194 1.1.2.2 nathanw wpp = wi_pci_lookup(pa);
195 1.1.2.2 nathanw if (wpp == NULL) {
196 1.1.2.2 nathanw printf("\n");
197 1.1.2.2 nathanw panic("wi_pci_attach: impossible");
198 1.1.2.2 nathanw }
199 1.1.2.2 nathanw
200 1.1.2.2 nathanw printf(": %s Wireless Lan\n", wpp->wpp_name);
201 1.1.2.2 nathanw
202 1.1.2.2 nathanw sc->sc_pci = 1;
203 1.1.2.2 nathanw sc->sc_enabled = 1;
204 1.1.2.2 nathanw sc->sc_enable = wi_pci_enable;
205 1.1.2.2 nathanw sc->sc_disable = wi_pci_disable;
206 1.1.2.2 nathanw
207 1.1.2.2 nathanw /* Enable the card. */
208 1.1.2.2 nathanw pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
209 1.1.2.2 nathanw pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
210 1.1.2.2 nathanw PCI_COMMAND_MASTER_ENABLE);
211 1.1.2.2 nathanw
212 1.1.2.2 nathanw
213 1.1.2.2 nathanw /* Map and establish the interrupt. */
214 1.1.2.2 nathanw if (pci_intr_map(pa, &ih)) {
215 1.1.2.2 nathanw printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
216 1.1.2.2 nathanw return;
217 1.1.2.2 nathanw }
218 1.1.2.2 nathanw intrstr = pci_intr_string(pc, ih);
219 1.1.2.2 nathanw
220 1.1.2.2 nathanw psc->psc_ih = ih;
221 1.1.2.2 nathanw sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
222 1.1.2.2 nathanw if (sc->sc_ih == NULL) {
223 1.1.2.2 nathanw printf("%s: couldn't establish interrupt",
224 1.1.2.2 nathanw sc->sc_dev.dv_xname);
225 1.1.2.2 nathanw if (intrstr != NULL)
226 1.1.2.2 nathanw printf(" at %s", intrstr);
227 1.1.2.2 nathanw printf("\n");
228 1.1.2.2 nathanw return;
229 1.1.2.2 nathanw }
230 1.1.2.2 nathanw
231 1.1.2.2 nathanw printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
232 1.1.2.2 nathanw
233 1.1.2.2 nathanw /* reset HFA3842 MAC core */
234 1.1.2.2 nathanw wi_pci_reset(sc);
235 1.1.2.2 nathanw
236 1.1.2.2 nathanw sc->sc_ifp = &sc->sc_ethercom.ec_if;
237 1.1.2.2 nathanw if (wi_attach(sc) != 0) {
238 1.1.2.2 nathanw printf("%s: failed to attach controller\n",
239 1.1.2.2 nathanw sc->sc_dev.dv_xname);
240 1.1.2.2 nathanw pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
241 1.1.2.2 nathanw return;
242 1.1.2.2 nathanw }
243 1.1.2.2 nathanw
244 1.1.2.2 nathanw /* Add a suspend hook to restore PCI config state */
245 1.1.2.2 nathanw psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
246 1.1.2.2 nathanw if (psc->sc_powerhook == NULL)
247 1.1.2.2 nathanw printf ("%s: WARNING: unable to establish pci power hook\n",
248 1.1.2.2 nathanw sc->sc_dev.dv_xname);
249 1.1.2.2 nathanw }
250 1.1.2.2 nathanw
251 1.1.2.2 nathanw static void
252 1.1.2.2 nathanw wi_pci_powerhook(why, arg)
253 1.1.2.2 nathanw int why;
254 1.1.2.2 nathanw void *arg;
255 1.1.2.2 nathanw {
256 1.1.2.2 nathanw struct wi_pci_softc *psc = arg;
257 1.1.2.2 nathanw struct wi_softc *sc = &psc->psc_wi;
258 1.1.2.2 nathanw
259 1.1.2.2 nathanw wi_power(sc, why);
260 1.1.2.2 nathanw }
261