if_wi_pci.c revision 1.1 1 /* $NetBSD: if_wi_pci.c,v 1.1 2001/10/13 15:01:07 ichiro 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/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/syslog.h>
49 #include <sys/socket.h>
50 #include <sys/device.h>
51 #include <sys/callout.h>
52
53 #include <net/if.h>
54 #include <net/if_ether.h>
55 #include <net/if_media.h>
56 #include <net/if_ieee80211.h>
57
58 #include <machine/bus.h>
59 #include <machine/intr.h>
60
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcidevs.h>
64
65 #include <dev/ic/wi_ieee.h>
66 #include <dev/ic/wireg.h>
67 #include <dev/ic/wivar.h>
68
69 struct wi_pci_softc {
70 struct wi_softc psc_wi; /* real "wi" softc */
71
72 /* PCI-specific goo */
73 pci_intr_handle_t psc_ih;
74 struct pci_attach_args *psc_pa;
75
76 void *sc_powerhook; /* power hook descriptor */
77 };
78
79 static int wi_pci_match __P((struct device *, struct cfdata *, void *));
80 static void wi_pci_attach __P((struct device *, struct device *, void *));
81 static int wi_pci_enable __P((struct wi_softc *));
82 static void wi_pci_disable __P((struct wi_softc *));
83 static void wi_pci_powerhook __P((int, void *));
84
85 static const struct wi_pci_product
86 *wi_pci_lookup __P((struct pci_attach_args *));
87
88 struct cfattach wi_pci_ca = {
89 sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
90 };
91
92 const struct wi_pci_product {
93 pci_vendor_id_t wpp_vendor; /* vendor ID */
94 pci_product_id_t wpp_product; /* product ID */
95 const char *wpp_name; /* product name */
96 } wi_pci_products[] = {
97 { PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
98 "Intersil Prism2.5" },
99 { 0, 0,
100 NULL},
101 };
102
103 static int
104 wi_pci_enable(sc)
105 struct wi_softc *sc;
106 {
107 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
108
109 /* establish the interrupt. */
110 sc->sc_ih = pci_intr_establish(psc->psc_pa->pa_pc,
111 psc->psc_ih, IPL_NET, wi_intr, sc);
112 if (sc->sc_ih == NULL) {
113 printf("%s: couldn't establish interrupt\n",
114 sc->sc_dev.dv_xname);
115 return (EIO);
116 }
117 sc->sc_pci = 1;
118
119 /* reset HFA3842 MAC core */
120 wi_pci_reset(sc);
121
122 return (0);
123 }
124
125 static void
126 wi_pci_disable(sc)
127 struct wi_softc *sc;
128 {
129 struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
130
131 pci_intr_disestablish(psc->psc_pa->pa_pc, sc->sc_ih);
132 }
133
134 static const struct wi_pci_product *
135 wi_pci_lookup(pa)
136 struct pci_attach_args *pa;
137 {
138 const struct wi_pci_product *wpp;
139
140 for (wpp = wi_pci_products; wpp->wpp_name != NULL; wpp++) {
141 if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
142 PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
143 return (wpp);
144 }
145 return (NULL);
146 }
147
148 static int
149 wi_pci_match(parent, match, aux)
150 struct device *parent;
151 struct cfdata *match;
152 void *aux;
153 {
154 struct pci_attach_args *pa = aux;
155
156 if (wi_pci_lookup(pa) != NULL)
157 return (1);
158 return (0);
159 }
160
161 static void
162 wi_pci_attach(parent, self, aux)
163 struct device *parent, *self;
164 void *aux;
165 {
166 struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
167 struct wi_softc *sc = &psc->psc_wi;
168 struct pci_attach_args *pa = aux;
169 pci_chipset_tag_t pc = pa->pa_pc;
170 const char *intrstr;
171 const struct wi_pci_product *wpp;
172 pci_intr_handle_t ih;
173 bus_space_tag_t memt;
174 bus_space_handle_t memh;
175 int memh_valid;
176 bus_addr_t busbase;
177 bus_size_t bussize;
178
179 psc->psc_pa = pa;
180
181 memh_valid = !pci_mapreg_map(pa, WI_PCI_CBMA,
182 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
183 &memt, &memh, &busbase, &bussize);
184
185 if(memh_valid){
186 sc->sc_iot = memt;
187 sc->sc_ioh = memh;
188 }
189 else {
190 printf(": unable to map device registers\n");
191 return;
192 }
193
194 wpp = wi_pci_lookup(pa);
195 if (wpp == NULL) {
196 printf("\n");
197 panic("wi_pci_attach: impossible");
198 }
199
200 printf(": %s Wireless Lan\n", wpp->wpp_name);
201
202 sc->sc_pci = 1;
203 sc->sc_enabled = 1;
204 sc->sc_enable = wi_pci_enable;
205 sc->sc_disable = wi_pci_disable;
206
207 /* Enable the card. */
208 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
209 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
210 PCI_COMMAND_MASTER_ENABLE);
211
212
213 /* Map and establish the interrupt. */
214 if (pci_intr_map(pa, &ih)) {
215 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
216 return;
217 }
218 intrstr = pci_intr_string(pc, ih);
219
220 psc->psc_ih = ih;
221 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
222 if (sc->sc_ih == NULL) {
223 printf("%s: couldn't establish interrupt",
224 sc->sc_dev.dv_xname);
225 if (intrstr != NULL)
226 printf(" at %s", intrstr);
227 printf("\n");
228 return;
229 }
230
231 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
232
233 /* reset HFA3842 MAC core */
234 wi_pci_reset(sc);
235
236 sc->sc_ifp = &sc->sc_ethercom.ec_if;
237 if (wi_attach(sc) != 0) {
238 printf("%s: failed to attach controller\n",
239 sc->sc_dev.dv_xname);
240 pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
241 return;
242 }
243
244 /* Add a suspend hook to restore PCI config state */
245 psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
246 if (psc->sc_powerhook == NULL)
247 printf ("%s: WARNING: unable to establish pci power hook\n",
248 sc->sc_dev.dv_xname);
249 }
250
251 static void
252 wi_pci_powerhook(why, arg)
253 int why;
254 void *arg;
255 {
256 struct wi_pci_softc *psc = arg;
257 struct wi_softc *sc = &psc->psc_wi;
258
259 wi_power(sc, why);
260 }
261