if_atw_pci.c revision 1.2 1 /* $NetBSD: if_atw_pci.c,v 1.2 2003/10/13 08:22:19 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center; Charles M. Hannum; and David Young.
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 ADMtek ADM8211 802.11 MAC/BBP chip.
42 *
43 * Derived from the ``Tulip'' PCI bus front-end.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: if_atw_pci.c,v 1.2 2003/10/13 08:22:19 dyoung Exp $");
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/device.h>
58
59 #include <machine/endian.h>
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64 #include <net/if_ether.h>
65
66 #include <net80211/ieee80211_compat.h>
67 #include <net80211/ieee80211_var.h>
68
69 #include <machine/bus.h>
70 #include <machine/intr.h>
71
72 #include <dev/ic/atwreg.h>
73 #include <dev/ic/atwvar.h>
74
75 #include <dev/pci/pcivar.h>
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcidevs.h>
78
79 /*
80 * PCI configuration space registers used by the ADM8211.
81 */
82 #define ATW_PCI_IOBA 0x10 /* i/o mapped base */
83 #define ATW_PCI_MMBA 0x14 /* memory mapped base */
84
85 struct atw_pci_softc {
86 struct atw_softc sc_atw; /* real ADM8211 softc */
87
88 /* PCI-specific goo. */
89 void *sc_ih; /* interrupt handle */
90
91 pci_chipset_tag_t sc_pc; /* our PCI chipset */
92 pcitag_t sc_pcitag; /* our PCI tag */
93 };
94
95 int atw_pci_match __P((struct device *, struct cfdata *, void *));
96 void atw_pci_attach __P((struct device *, struct device *, void *));
97
98 CFATTACH_DECL(atw_pci, sizeof(struct atw_pci_softc),
99 atw_pci_match, atw_pci_attach, NULL, NULL);
100
101 const struct atw_pci_product {
102 u_int32_t app_vendor; /* PCI vendor ID */
103 u_int32_t app_product; /* PCI product ID */
104 const char *app_product_name;
105 } atw_pci_products[] = {
106 { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_ADM8211,
107 "ADMtek ADM8211 802.11 MAC/BBP" },
108
109 { 0, 0, NULL },
110 };
111
112 const struct atw_pci_product *atw_pci_lookup
113 __P((const struct pci_attach_args *));
114
115 const struct atw_pci_product *
116 atw_pci_lookup(pa)
117 const struct pci_attach_args *pa;
118 {
119 const struct atw_pci_product *app;
120
121 for (app = atw_pci_products;
122 app->app_product_name != NULL;
123 app++) {
124 if (PCI_VENDOR(pa->pa_id) == app->app_vendor &&
125 PCI_PRODUCT(pa->pa_id) == app->app_product)
126 return (app);
127 }
128 return (NULL);
129 }
130
131 int
132 atw_pci_match(parent, match, aux)
133 struct device *parent;
134 struct cfdata *match;
135 void *aux;
136 {
137 struct pci_attach_args *pa = aux;
138
139 if (atw_pci_lookup(pa) != NULL)
140 return (1);
141
142 return (0);
143 }
144
145 void
146 atw_pci_attach(parent, self, aux)
147 struct device *parent, *self;
148 void *aux;
149 {
150 struct atw_pci_softc *psc = (void *) self;
151 struct atw_softc *sc = &psc->sc_atw;
152 struct pci_attach_args *pa = aux;
153 pci_chipset_tag_t pc = pa->pa_pc;
154 pci_intr_handle_t ih;
155 const char *intrstr = NULL;
156 bus_space_tag_t iot, memt;
157 bus_space_handle_t ioh, memh;
158 int ioh_valid, memh_valid;
159 const struct atw_pci_product *app;
160 pcireg_t reg;
161 int pmreg, rev;
162
163 psc->sc_pc = pa->pa_pc;
164 psc->sc_pcitag = pa->pa_tag;
165
166 app = atw_pci_lookup(pa);
167 if (app == NULL) {
168 printf("\n");
169 panic("atw_pci_attach: impossible");
170 }
171
172 /*
173 * No power management hooks.
174 * XXX Maybe we should add some!
175 */
176 sc->sc_flags |= ATWF_ENABLED;
177
178 /*
179 * Get revision info, and set some chip-specific variables.
180 */
181 rev = PCI_REVISION(pa->pa_class);
182 printf(": %s, pass %d.%d\n", app->app_product_name,
183 (rev >> 4) & 0xf, rev & 0xf);
184
185 /*
186 * Check to see if the device is in power-save mode, and
187 * being it out if necessary.
188 *
189 * XXX This code comes almost verbatim from if_tlp_pci.c. I do
190 * not understand it. Tulip clears the "sleep mode" bit in the
191 * CFDA register, first. There is an equivalent (?) register at the
192 * same place in the ADM8211, but the docs do not assign its bits
193 * any meanings. -dcy
194 */
195 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
196 reg = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
197 switch (reg & PCI_PMCSR_STATE_MASK) {
198 case PCI_PMCSR_STATE_D1:
199 case PCI_PMCSR_STATE_D2:
200 printf(": waking up from power state D%d\n%s",
201 reg & PCI_PMCSR_STATE_MASK, sc->sc_dev.dv_xname);
202 pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
203 (reg & ~PCI_PMCSR_STATE_MASK) |
204 PCI_PMCSR_STATE_D0);
205 break;
206 case PCI_PMCSR_STATE_D3:
207 /*
208 * The card has lost all configuration data in
209 * this state, so punt.
210 */
211 printf(": unable to wake up from power state D3, "
212 "reboot required.\n");
213 pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
214 (reg & ~PCI_PMCSR_STATE_MASK) |
215 PCI_PMCSR_STATE_D0);
216 return;
217 }
218 }
219
220 /*
221 * Map the device.
222 */
223 ioh_valid = (pci_mapreg_map(pa, ATW_PCI_IOBA,
224 PCI_MAPREG_TYPE_IO, 0,
225 &iot, &ioh, NULL, NULL) == 0);
226 memh_valid = (pci_mapreg_map(pa, ATW_PCI_MMBA,
227 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
228 &memt, &memh, NULL, NULL) == 0);
229
230 if (memh_valid) {
231 sc->sc_st = memt;
232 sc->sc_sh = memh;
233 } else if (ioh_valid) {
234 sc->sc_st = iot;
235 sc->sc_sh = ioh;
236 } else {
237 printf(": unable to map device registers\n");
238 return;
239 }
240
241 sc->sc_dmat = pa->pa_dmat;
242
243 /*
244 * Make sure bus mastering is enabled.
245 */
246 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
247 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
248 PCI_COMMAND_MASTER_ENABLE);
249
250 /*
251 * Get the cacheline size.
252 */
253 sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
254 PCI_BHLC_REG));
255
256 /*
257 * Get PCI data moving command info.
258 */
259 if (pa->pa_flags & PCI_FLAGS_MRL_OKAY) /* read line */
260 sc->sc_flags |= ATWF_MRL;
261 if (pa->pa_flags & PCI_FLAGS_MRM_OKAY) /* read multiple */
262 sc->sc_flags |= ATWF_MRM;
263 if (pa->pa_flags & PCI_FLAGS_MWI_OKAY) /* write invalidate */
264 sc->sc_flags |= ATWF_MWI;
265
266 /*
267 * Map and establish our interrupt.
268 */
269 if (pci_intr_map(pa, &ih)) {
270 printf("%s: unable to map interrupt\n",
271 sc->sc_dev.dv_xname);
272 return;
273 }
274 intrstr = pci_intr_string(pc, ih);
275 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, atw_intr, sc);
276 if (psc->sc_ih == NULL) {
277 printf("%s: unable to establish interrupt",
278 sc->sc_dev.dv_xname);
279 if (intrstr != NULL)
280 printf(" at %s", intrstr);
281 printf("\n");
282 return;
283 }
284 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
285
286 /*
287 * Finish off the attach.
288 */
289 atw_attach(sc);
290 }
291
292