if_bwi_cardbus.c revision 1.2.2.2 1 1.2.2.2 yamt /* $NetBSD: if_bwi_cardbus.c,v 1.2.2.2 2012/04/17 00:07:29 yamt Exp $ */
2 1.2.2.2 yamt /* $OpenBSD: if_bwi_cardbus.c,v 1.13 2010/08/06 05:26:24 mglocker Exp $ */
3 1.2.2.2 yamt
4 1.2.2.2 yamt /*
5 1.2.2.2 yamt * Copyright (c) 2007 Marcus Glocker <mglocker (at) openbsd.org>
6 1.2.2.2 yamt * Copyright (c) 2006 Claudio Jeker <claudio (at) openbsd.org>
7 1.2.2.2 yamt *
8 1.2.2.2 yamt * Permission to use, copy, modify, and distribute this software for any
9 1.2.2.2 yamt * purpose with or without fee is hereby granted, provided that the above
10 1.2.2.2 yamt * copyright notice and this permission notice appear in all copies.
11 1.2.2.2 yamt *
12 1.2.2.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.2.2.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.2.2.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.2.2.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.2.2.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.2.2.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.2.2.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.2.2.2 yamt */
20 1.2.2.2 yamt
21 1.2.2.2 yamt /*
22 1.2.2.2 yamt * Broadcom AirForce BCM43xx IEEE 802.11b/g wireless network driver
23 1.2.2.2 yamt * CardBus front end
24 1.2.2.2 yamt */
25 1.2.2.2 yamt
26 1.2.2.2 yamt #include <sys/cdefs.h>
27 1.2.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: if_bwi_cardbus.c,v 1.2.2.2 2012/04/17 00:07:29 yamt Exp $");
28 1.2.2.2 yamt
29 1.2.2.2 yamt #include "opt_inet.h"
30 1.2.2.2 yamt
31 1.2.2.2 yamt #include <sys/param.h>
32 1.2.2.2 yamt #include <sys/callout.h>
33 1.2.2.2 yamt #include <sys/device.h>
34 1.2.2.2 yamt #include <sys/kernel.h>
35 1.2.2.2 yamt #include <sys/malloc.h>
36 1.2.2.2 yamt #include <sys/mbuf.h>
37 1.2.2.2 yamt #include <sys/socket.h>
38 1.2.2.2 yamt #include <sys/sockio.h>
39 1.2.2.2 yamt #include <sys/systm.h>
40 1.2.2.2 yamt #include <sys/errno.h>
41 1.2.2.2 yamt
42 1.2.2.2 yamt #include <machine/endian.h>
43 1.2.2.2 yamt
44 1.2.2.2 yamt #include <net/if.h>
45 1.2.2.2 yamt #include <net/if_dl.h>
46 1.2.2.2 yamt #include <net/if_ether.h>
47 1.2.2.2 yamt #include <net/if_media.h>
48 1.2.2.2 yamt
49 1.2.2.2 yamt #include <net80211/ieee80211_var.h>
50 1.2.2.2 yamt #include <net80211/ieee80211_amrr.h>
51 1.2.2.2 yamt #include <net80211/ieee80211_radiotap.h>
52 1.2.2.2 yamt
53 1.2.2.2 yamt #include <dev/pci/pcireg.h>
54 1.2.2.2 yamt #include <dev/pci/pcivar.h>
55 1.2.2.2 yamt #include <dev/pci/pcidevs.h>
56 1.2.2.2 yamt
57 1.2.2.2 yamt #include <dev/cardbus/cardbusvar.h>
58 1.2.2.2 yamt
59 1.2.2.2 yamt #include <dev/ic/bwireg.h>
60 1.2.2.2 yamt #include <dev/ic/bwivar.h>
61 1.2.2.2 yamt
62 1.2.2.2 yamt struct bwi_cardbus_softc {
63 1.2.2.2 yamt struct bwi_softc csc_bwi;
64 1.2.2.2 yamt
65 1.2.2.2 yamt /* cardbus specific goo */
66 1.2.2.2 yamt cardbus_devfunc_t csc_ct;
67 1.2.2.2 yamt pcitag_t csc_tag;
68 1.2.2.2 yamt
69 1.2.2.2 yamt bus_size_t csc_mapsize;
70 1.2.2.2 yamt pcireg_t csc_bar_val;
71 1.2.2.2 yamt };
72 1.2.2.2 yamt
73 1.2.2.2 yamt static int bwi_cardbus_match(device_t, cfdata_t, void *);
74 1.2.2.2 yamt static void bwi_cardbus_attach(device_t, device_t, void *);
75 1.2.2.2 yamt static int bwi_cardbus_detach(device_t, int);
76 1.2.2.2 yamt static void bwi_cardbus_setup(struct bwi_cardbus_softc *);
77 1.2.2.2 yamt static int bwi_cardbus_enable(struct bwi_softc *, int);
78 1.2.2.2 yamt static void bwi_cardbus_disable(struct bwi_softc *, int);
79 1.2.2.2 yamt static void bwi_cardbus_conf_write(void *, uint32_t, uint32_t);
80 1.2.2.2 yamt static uint32_t bwi_cardbus_conf_read(void *, uint32_t);
81 1.2.2.2 yamt
82 1.2.2.2 yamt CFATTACH_DECL_NEW(bwi_cardbus, sizeof (struct bwi_cardbus_softc),
83 1.2.2.2 yamt bwi_cardbus_match, bwi_cardbus_attach, bwi_cardbus_detach, NULL);
84 1.2.2.2 yamt
85 1.2.2.2 yamt static int
86 1.2.2.2 yamt bwi_cardbus_match(device_t parent, cfdata_t match, void *aux)
87 1.2.2.2 yamt {
88 1.2.2.2 yamt struct cardbus_attach_args *ca = aux;
89 1.2.2.2 yamt
90 1.2.2.2 yamt if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_BROADCOM) {
91 1.2.2.2 yamt switch (PCI_PRODUCT(ca->ca_id)) {
92 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4303:
93 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4306:
94 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4306_2:
95 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4307:
96 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4309:
97 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4311:
98 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4312:
99 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4318:
100 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4319:
101 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4322:
102 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM43XG:
103 1.2.2.2 yamt case PCI_PRODUCT_BROADCOM_BCM4328:
104 1.2.2.2 yamt return (1);
105 1.2.2.2 yamt default:
106 1.2.2.2 yamt return (0);
107 1.2.2.2 yamt }
108 1.2.2.2 yamt }
109 1.2.2.2 yamt
110 1.2.2.2 yamt return (0);
111 1.2.2.2 yamt }
112 1.2.2.2 yamt
113 1.2.2.2 yamt static void
114 1.2.2.2 yamt bwi_cardbus_attach(device_t parent, device_t self, void *aux)
115 1.2.2.2 yamt {
116 1.2.2.2 yamt struct bwi_cardbus_softc *csc = device_private(self);
117 1.2.2.2 yamt struct cardbus_attach_args *ca = aux;
118 1.2.2.2 yamt struct bwi_softc *sc = &csc->csc_bwi;
119 1.2.2.2 yamt cardbus_devfunc_t ct = ca->ca_ct;
120 1.2.2.2 yamt pcireg_t reg;
121 1.2.2.2 yamt bus_addr_t base;
122 1.2.2.2 yamt int error;
123 1.2.2.2 yamt
124 1.2.2.2 yamt aprint_naive("\n");
125 1.2.2.2 yamt aprint_normal(": Broadcom Wireless\n");
126 1.2.2.2 yamt
127 1.2.2.2 yamt sc->sc_dev = self;
128 1.2.2.2 yamt sc->sc_dmat = ca->ca_dmat;
129 1.2.2.2 yamt csc->csc_ct = ct;
130 1.2.2.2 yamt csc->csc_tag = ca->ca_tag;
131 1.2.2.2 yamt
132 1.2.2.2 yamt /* power management hooks */
133 1.2.2.2 yamt sc->sc_enable = bwi_cardbus_enable;
134 1.2.2.2 yamt sc->sc_disable = bwi_cardbus_disable;
135 1.2.2.2 yamt
136 1.2.2.2 yamt /* map control/status registers */
137 1.2.2.2 yamt error = Cardbus_mapreg_map(ct, BWI_PCIR_BAR,
138 1.2.2.2 yamt PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem_bt,
139 1.2.2.2 yamt &sc->sc_mem_bh, &base, &csc->csc_mapsize);
140 1.2.2.2 yamt if (error != 0) {
141 1.2.2.2 yamt aprint_error_dev(self, "can't map mem space\n");
142 1.2.2.2 yamt return;
143 1.2.2.2 yamt }
144 1.2.2.2 yamt csc->csc_bar_val = base | PCI_MAPREG_TYPE_MEM;
145 1.2.2.2 yamt
146 1.2.2.2 yamt /* set up the PCI configuration registers */
147 1.2.2.2 yamt bwi_cardbus_setup(csc);
148 1.2.2.2 yamt
149 1.2.2.2 yamt /* we need to access Cardbus config space from the driver */
150 1.2.2.2 yamt sc->sc_conf_read = bwi_cardbus_conf_read;
151 1.2.2.2 yamt sc->sc_conf_write = bwi_cardbus_conf_write;
152 1.2.2.2 yamt
153 1.2.2.2 yamt reg = (sc->sc_conf_read)(sc, PCI_SUBSYS_ID_REG);
154 1.2.2.2 yamt
155 1.2.2.2 yamt sc->sc_pci_revid = PCI_REVISION(ca->ca_class);
156 1.2.2.2 yamt sc->sc_pci_did = PCI_PRODUCT(ca->ca_id);
157 1.2.2.2 yamt sc->sc_pci_subvid = PCI_VENDOR(reg);
158 1.2.2.2 yamt sc->sc_pci_subdid = PCI_PRODUCT(reg);
159 1.2.2.2 yamt
160 1.2.2.2 yamt if (pmf_device_register(self, bwi_suspend, bwi_resume))
161 1.2.2.2 yamt pmf_class_network_register(self, &sc->sc_if);
162 1.2.2.2 yamt else
163 1.2.2.2 yamt aprint_error_dev(self, "couldn't establish power handler\n");
164 1.2.2.2 yamt
165 1.2.2.2 yamt error = bwi_attach(sc);
166 1.2.2.2 yamt if (error != 0)
167 1.2.2.2 yamt bwi_cardbus_detach(self, 0);
168 1.2.2.2 yamt
169 1.2.2.2 yamt Cardbus_function_disable(ct);
170 1.2.2.2 yamt }
171 1.2.2.2 yamt
172 1.2.2.2 yamt static int
173 1.2.2.2 yamt bwi_cardbus_detach(device_t self, int flags)
174 1.2.2.2 yamt {
175 1.2.2.2 yamt struct bwi_cardbus_softc *csc = device_private(self);
176 1.2.2.2 yamt struct bwi_softc *sc = &csc->csc_bwi;
177 1.2.2.2 yamt cardbus_devfunc_t ct = csc->csc_ct;
178 1.2.2.2 yamt
179 1.2.2.2 yamt #ifdef DIAGNOSTIC
180 1.2.2.2 yamt if (ct == NULL)
181 1.2.2.2 yamt panic("%s: data structure lacks", device_xname(self));
182 1.2.2.2 yamt #endif
183 1.2.2.2 yamt
184 1.2.2.2 yamt pmf_device_deregister(self);
185 1.2.2.2 yamt
186 1.2.2.2 yamt bwi_detach(sc);
187 1.2.2.2 yamt
188 1.2.2.2 yamt /* unhook the interrupt handler */
189 1.2.2.2 yamt if (sc->sc_ih != NULL) {
190 1.2.2.2 yamt Cardbus_intr_disestablish(ct, sc->sc_ih);
191 1.2.2.2 yamt sc->sc_ih = NULL;
192 1.2.2.2 yamt }
193 1.2.2.2 yamt
194 1.2.2.2 yamt /* release bus space and close window */
195 1.2.2.2 yamt Cardbus_mapreg_unmap(ct, BWI_PCIR_BAR, sc->sc_mem_bt,
196 1.2.2.2 yamt sc->sc_mem_bh, csc->csc_mapsize);
197 1.2.2.2 yamt
198 1.2.2.2 yamt return (0);
199 1.2.2.2 yamt }
200 1.2.2.2 yamt
201 1.2.2.2 yamt static void
202 1.2.2.2 yamt bwi_cardbus_setup(struct bwi_cardbus_softc *csc)
203 1.2.2.2 yamt {
204 1.2.2.2 yamt cardbus_devfunc_t ct = csc->csc_ct;
205 1.2.2.2 yamt pcireg_t reg;
206 1.2.2.2 yamt
207 1.2.2.2 yamt /* program the BAR */
208 1.2.2.2 yamt Cardbus_conf_write(ct, csc->csc_tag, BWI_PCIR_BAR, csc->csc_bar_val);
209 1.2.2.2 yamt
210 1.2.2.2 yamt /* enable the appropriate bits in the PCI CSR */
211 1.2.2.2 yamt reg = Cardbus_conf_read(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG);
212 1.2.2.2 yamt reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
213 1.2.2.2 yamt Cardbus_conf_write(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG, reg);
214 1.2.2.2 yamt }
215 1.2.2.2 yamt
216 1.2.2.2 yamt static int
217 1.2.2.2 yamt bwi_cardbus_enable(struct bwi_softc *sc, int pmf)
218 1.2.2.2 yamt {
219 1.2.2.2 yamt struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
220 1.2.2.2 yamt cardbus_devfunc_t ct = csc->csc_ct;
221 1.2.2.2 yamt
222 1.2.2.2 yamt if (!pmf) {
223 1.2.2.2 yamt /* power on the socket */
224 1.2.2.2 yamt Cardbus_function_enable(ct);
225 1.2.2.2 yamt
226 1.2.2.2 yamt /* setup the PCI configuration registers */
227 1.2.2.2 yamt bwi_cardbus_setup(csc);
228 1.2.2.2 yamt }
229 1.2.2.2 yamt
230 1.2.2.2 yamt /* map and establish the interrupt handler */
231 1.2.2.2 yamt sc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, bwi_intr, sc);
232 1.2.2.2 yamt if (sc->sc_ih == NULL) {
233 1.2.2.2 yamt aprint_error_dev(sc->sc_dev,
234 1.2.2.2 yamt "unable to establish interrupt\n");
235 1.2.2.2 yamt if (!pmf)
236 1.2.2.2 yamt Cardbus_function_disable(ct);
237 1.2.2.2 yamt return (1);
238 1.2.2.2 yamt }
239 1.2.2.2 yamt
240 1.2.2.2 yamt return (0);
241 1.2.2.2 yamt }
242 1.2.2.2 yamt
243 1.2.2.2 yamt static void
244 1.2.2.2 yamt bwi_cardbus_disable(struct bwi_softc *sc, int pmf)
245 1.2.2.2 yamt {
246 1.2.2.2 yamt struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
247 1.2.2.2 yamt cardbus_devfunc_t ct = csc->csc_ct;
248 1.2.2.2 yamt
249 1.2.2.2 yamt /* unhook the interrupt handler */
250 1.2.2.2 yamt if (sc->sc_ih != NULL) {
251 1.2.2.2 yamt Cardbus_intr_disestablish(ct, sc->sc_ih);
252 1.2.2.2 yamt sc->sc_ih = NULL;
253 1.2.2.2 yamt }
254 1.2.2.2 yamt
255 1.2.2.2 yamt if (!pmf) {
256 1.2.2.2 yamt /* power down the socket */
257 1.2.2.2 yamt Cardbus_function_disable(ct);
258 1.2.2.2 yamt }
259 1.2.2.2 yamt }
260 1.2.2.2 yamt
261 1.2.2.2 yamt static void
262 1.2.2.2 yamt bwi_cardbus_conf_write(void *sc, uint32_t reg, uint32_t val)
263 1.2.2.2 yamt {
264 1.2.2.2 yamt struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
265 1.2.2.2 yamt
266 1.2.2.2 yamt Cardbus_conf_write(csc->csc_ct, csc->csc_tag, reg, val);
267 1.2.2.2 yamt }
268 1.2.2.2 yamt
269 1.2.2.2 yamt static uint32_t
270 1.2.2.2 yamt bwi_cardbus_conf_read(void *sc, uint32_t reg)
271 1.2.2.2 yamt {
272 1.2.2.2 yamt struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
273 1.2.2.2 yamt
274 1.2.2.2 yamt return Cardbus_conf_read(csc->csc_ct, csc->csc_tag, reg);
275 1.2.2.2 yamt }
276