if_rtw_pci.c revision 1.9.6.2 1 /* $NetBSD: if_rtw_pci.c,v 1.9.6.2 2008/06/02 13:23:40 mjf 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
35 *
36 * Derived from the ADMtek ADM8211 PCI bus front-end.
37 *
38 * Derived from the ``Tulip'' PCI bus front-end.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: if_rtw_pci.c,v 1.9.6.2 2008/06/02 13:23:40 mjf Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53
54 #include <machine/endian.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_ether.h>
60
61 #include <net80211/ieee80211_netbsd.h>
62 #include <net80211/ieee80211_radiotap.h>
63 #include <net80211/ieee80211_var.h>
64
65 #include <sys/bus.h>
66 #include <sys/intr.h>
67
68 #include <dev/ic/rtwreg.h>
69 #include <dev/ic/sa2400reg.h>
70 #include <dev/ic/rtwvar.h>
71
72 #include <dev/pci/pcivar.h>
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcidevs.h>
75
76 /*
77 * PCI configuration space registers used by the ADM8211.
78 */
79 #define RTW_PCI_IOBA 0x10 /* i/o mapped base */
80 #define RTW_PCI_MMBA 0x14 /* memory mapped base */
81
82 struct rtw_pci_softc {
83 struct rtw_softc psc_rtw; /* real ADM8211 softc */
84
85 pci_intr_handle_t psc_ih; /* interrupt handle */
86 void *psc_intrcookie;
87
88 pci_chipset_tag_t psc_pc; /* our PCI chipset */
89 pcitag_t psc_pcitag; /* our PCI tag */
90 };
91
92 static int rtw_pci_match(device_t, struct cfdata *, void *);
93 static void rtw_pci_attach(device_t, device_t, void *);
94 static int rtw_pci_detach(device_t, int);
95
96 CFATTACH_DECL_NEW(rtw_pci, sizeof(struct rtw_pci_softc),
97 rtw_pci_match, rtw_pci_attach, rtw_pci_detach, NULL);
98
99 static const struct rtw_pci_product {
100 u_int32_t app_vendor; /* PCI vendor ID */
101 u_int32_t app_product; /* PCI product ID */
102 const char *app_product_name;
103 } rtw_pci_products[] = {
104 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8180,
105 "Realtek RTL8180 802.11 MAC/BBP" },
106 { PCI_VENDOR_BELKIN, PCI_PRODUCT_BELKIN_F5D6001,
107 "Belkin F5D6001" },
108
109 { 0, 0, NULL },
110 };
111
112 static const struct rtw_pci_product *
113 rtw_pci_lookup(const struct pci_attach_args *pa)
114 {
115 const struct rtw_pci_product *app;
116
117 for (app = rtw_pci_products;
118 app->app_product_name != NULL;
119 app++) {
120 if (PCI_VENDOR(pa->pa_id) == app->app_vendor &&
121 PCI_PRODUCT(pa->pa_id) == app->app_product)
122 return (app);
123 }
124 return (NULL);
125 }
126
127 static int
128 rtw_pci_match(device_t parent, struct cfdata *match, void *aux)
129 {
130 struct pci_attach_args *pa = aux;
131
132 if (rtw_pci_lookup(pa) != NULL)
133 return (1);
134
135 return (0);
136 }
137
138 static bool
139 rtw_pci_resume(device_t self PMF_FN_ARGS)
140 {
141 struct rtw_pci_softc *psc = device_private(self);
142 struct rtw_softc *sc = &psc->psc_rtw;
143
144 /* Establish the interrupt. */
145 psc->psc_intrcookie = pci_intr_establish(psc->psc_pc, psc->psc_ih,
146 IPL_NET, rtw_intr, sc);
147 if (psc->psc_intrcookie == NULL) {
148 aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
149 return false;
150 }
151
152 return rtw_resume(self, flags);
153 }
154
155 static bool
156 rtw_pci_suspend(device_t self PMF_FN_ARGS)
157 {
158 struct rtw_pci_softc *psc = device_private(self);
159
160 if (!rtw_suspend(self, flags))
161 return false;
162
163 /* Unhook the interrupt handler. */
164 pci_intr_disestablish(psc->psc_pc, psc->psc_intrcookie);
165 psc->psc_intrcookie = NULL;
166 return true;
167 }
168
169 static void
170 rtw_pci_attach(device_t parent, device_t self, void *aux)
171 {
172 struct rtw_pci_softc *psc = device_private(self);
173 struct rtw_softc *sc = &psc->psc_rtw;
174 struct rtw_regs *regs = &sc->sc_regs;
175 struct pci_attach_args *pa = aux;
176 pci_chipset_tag_t pc = pa->pa_pc;
177 const char *intrstr = NULL;
178 const struct rtw_pci_product *app;
179 int error;
180
181 sc->sc_dev = self;
182 psc->psc_pc = pa->pa_pc;
183 psc->psc_pcitag = pa->pa_tag;
184
185 app = rtw_pci_lookup(pa);
186 if (app == NULL) {
187 printf("\n");
188 panic("rtw_pci_attach: impossible");
189 }
190
191 /*
192 * Get revision info, and set some chip-specific variables.
193 */
194 sc->sc_rev = PCI_REVISION(pa->pa_class);
195 aprint_normal(": %s, revision %d.%d\n", app->app_product_name,
196 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
197
198 /* power up chip */
199 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, NULL)) != 0 &&
200 error != EOPNOTSUPP) {
201 aprint_error_dev(self, "cannot activate %d\n", error);
202 return;
203 }
204
205 /*
206 * Map the device.
207 */
208 if (pci_mapreg_map(pa, RTW_PCI_MMBA,
209 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
210 ®s->r_bt, ®s->r_bh, NULL, ®s->r_sz) == 0)
211 ;
212 else if (pci_mapreg_map(pa, RTW_PCI_IOBA, PCI_MAPREG_TYPE_IO, 0,
213 ®s->r_bt, ®s->r_bh, NULL, ®s->r_sz) == 0)
214 ;
215 else {
216 aprint_error_dev(self, "unable to map device registers\n");
217 return;
218 }
219
220 sc->sc_dmat = pa->pa_dmat;
221
222 /*
223 * Make sure bus mastering is enabled.
224 */
225 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
226 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
227 PCI_COMMAND_MASTER_ENABLE);
228
229 /*
230 * Map and establish our interrupt.
231 */
232 if (pci_intr_map(pa, &psc->psc_ih)) {
233 aprint_error_dev(self, "unable to map interrupt\n");
234 return;
235 }
236 intrstr = pci_intr_string(pc, psc->psc_ih);
237 psc->psc_intrcookie = pci_intr_establish(pc, psc->psc_ih, IPL_NET,
238 rtw_intr, sc);
239 if (psc->psc_intrcookie == NULL) {
240 aprint_error_dev(self, "unable to establish interrupt");
241 if (intrstr != NULL)
242 aprint_error(" at %s", intrstr);
243 aprint_error("\n");
244 return;
245 }
246
247 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
248
249 /*
250 * Finish off the attach.
251 */
252 rtw_attach(sc);
253
254 if (!pmf_device_register(sc->sc_dev, rtw_pci_suspend,
255 rtw_pci_resume)) {
256 aprint_error_dev(sc->sc_dev,
257 "couldn't establish power handler\n");
258 } else {
259 pmf_class_network_register(self, &sc->sc_if);
260 /*
261 * Power down the socket.
262 */
263 pmf_device_suspend_self(self);
264 }
265 }
266
267 static int
268 rtw_pci_detach(device_t self, int flags)
269 {
270 struct rtw_pci_softc *psc = device_private(self);
271 struct rtw_softc *sc = &psc->psc_rtw;
272 struct rtw_regs *regs = &sc->sc_regs;
273 int rc;
274
275 if ((rc = rtw_detach(sc)) != 0)
276 return rc;
277 if (psc->psc_intrcookie != NULL)
278 pci_intr_disestablish(psc->psc_pc, psc->psc_intrcookie);
279 bus_space_unmap(regs->r_bt, regs->r_bh, regs->r_sz);
280
281 return 0;
282 }
283