xref: /netbsd-src/sys/dev/pci/if_bwi_pci.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: if_bwi_pci.c,v 1.17 2021/05/08 00:27:02 thorpej Exp $	*/
2 /*	$OpenBSD: if_bwi_pci.c,v 1.6 2008/02/14 22:10:02 brad Exp $ */
3 
4 /*
5  * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * Broadcom AirForce BCM43xx IEEE 802.11b/g wireless network driver
22  * PCI front end
23  */
24 
25 
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: if_bwi_pci.c,v 1.17 2021/05/08 00:27:02 thorpej Exp $");
28 
29 #include <sys/param.h>
30 #include <sys/callout.h>
31 #include <sys/device.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/systm.h>
38 
39 #include <sys/bus.h>
40 
41 #include <net/if.h>
42 #include <net/if_dl.h>
43 #include <net/if_ether.h>
44 #include <net/if_media.h>
45 
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_amrr.h>
48 #include <net80211/ieee80211_radiotap.h>
49 
50 #include <dev/ic/bwivar.h>
51 
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcidevs.h>
55 
56 /* Base Address Register */
57 #define BWI_PCI_BAR0 PCI_BAR(0)
58 
59 static int	bwi_pci_match(device_t, cfdata_t, void *);
60 static void	bwi_pci_attach(device_t, device_t, void *);
61 static int	bwi_pci_detach(device_t, int);
62 static void	bwi_pci_conf_write(void *, uint32_t, uint32_t);
63 static uint32_t	bwi_pci_conf_read(void *, uint32_t);
64 
65 struct bwi_pci_softc {
66 	struct bwi_softc	 psc_bwi;
67 
68 	pci_chipset_tag_t        psc_pc;
69 	pcitag_t		 psc_pcitag;
70 
71 	bus_size_t		 psc_mapsize;
72 };
73 
74 CFATTACH_DECL_NEW(bwi_pci, sizeof(struct bwi_pci_softc),
75     bwi_pci_match, bwi_pci_attach, bwi_pci_detach, NULL);
76 
77 static const struct device_compatible_entry compat_data[] = {
78 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
79 		PCI_PRODUCT_BROADCOM_BCM4303), },
80 
81 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
82 		PCI_PRODUCT_BROADCOM_BCM4306), },
83 
84 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
85 		PCI_PRODUCT_BROADCOM_BCM4306_2), },
86 
87 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
88 		PCI_PRODUCT_BROADCOM_BCM4307), },
89 
90 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
91 		PCI_PRODUCT_BROADCOM_BCM4309), },
92 
93 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
94 		PCI_PRODUCT_BROADCOM_BCM4311), },
95 
96 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
97 		PCI_PRODUCT_BROADCOM_BCM4312), },
98 
99 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
100 		PCI_PRODUCT_BROADCOM_BCM4318), },
101 
102 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
103 		PCI_PRODUCT_BROADCOM_BCM4319), },
104 
105 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
106 		PCI_PRODUCT_BROADCOM_BCM4322), },
107 
108 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
109 		PCI_PRODUCT_BROADCOM_BCM43XG), },
110 
111 	{ .id = PCI_ID_CODE(PCI_VENDOR_BROADCOM,
112 		PCI_PRODUCT_BROADCOM_BCM4328), },
113 
114 	PCI_COMPAT_EOL
115 };
116 
117 static int
118 bwi_pci_match(device_t parent, cfdata_t match, void *aux)
119 {
120 	struct pci_attach_args *pa = aux;
121 
122 	return pci_compatible_match(pa, compat_data);
123 }
124 
125 static void
126 bwi_pci_attach(device_t parent, device_t self, void *aux)
127 {
128 	struct bwi_pci_softc *psc = device_private(self);
129 	struct pci_attach_args *pa = aux;
130 	struct bwi_softc *sc = &psc->psc_bwi;
131 	const char *intrstr = NULL;
132 	pci_intr_handle_t ih;
133 	pcireg_t memtype, reg;
134 	int error = 0;
135 	char intrbuf[PCI_INTRSTR_LEN];
136 
137 	aprint_naive("\n");
138 	aprint_normal(": Broadcom Wireless\n");
139 
140 	sc->sc_dev = self;
141 	sc->sc_dmat = pa->pa_dmat;
142 	psc->psc_pc = pa->pa_pc;
143 	psc->psc_pcitag = pa->pa_tag;
144 
145 	/* map control / status registers */
146 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BWI_PCI_BAR0);
147 	switch (memtype) {
148 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
149 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
150 		break;
151 	default:
152 		aprint_error_dev(self, "invalid base address register\n");
153 		return;
154 	}
155 
156 	if (pci_mapreg_map(pa, BWI_PCI_BAR0, memtype, 0, &sc->sc_mem_bt,
157 	    &sc->sc_mem_bh, NULL, &psc->psc_mapsize) != 0) {
158 		aprint_error_dev(self, "could not map mem space\n");
159 		return;
160 	}
161 
162 	/* map interrupt */
163 	if (pci_intr_map(pa, &ih) != 0) {
164 		aprint_error_dev(self, "could not map interrupt\n");
165 		goto fail;
166 	}
167 
168 	/* establish interrupt */
169 	intrstr = pci_intr_string(psc->psc_pc, ih, intrbuf, sizeof(intrbuf));
170 	sc->sc_ih = pci_intr_establish_xname(psc->psc_pc, ih, IPL_NET, bwi_intr,
171 	    sc, device_xname(self));
172 	if (sc->sc_ih == NULL) {
173 		aprint_error_dev(self, "could not establish interrupt");
174 		if (intrstr != NULL)
175 			aprint_error(" at %s", intrstr);
176 		aprint_error("\n");
177 		goto fail;
178 	}
179 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
180 
181 	/* we need to access PCI config space from the driver */
182 	sc->sc_conf_write = bwi_pci_conf_write;
183 	sc->sc_conf_read = bwi_pci_conf_read;
184 
185 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
186 
187 	sc->sc_pci_revid = PCI_REVISION(pa->pa_class);
188 	sc->sc_pci_did = PCI_PRODUCT(pa->pa_id);
189 	sc->sc_pci_subvid = PCI_VENDOR(reg);
190 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
191 
192 	if (!pmf_device_register(self, bwi_suspend, bwi_resume))
193 		aprint_error_dev(self, "couldn't establish power handler\n");
194 
195 	error = bwi_attach(sc);
196 	if (error)
197 		goto fail;
198 	return;
199 
200 fail:
201 	if (sc->sc_ih) {
202 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
203 		sc->sc_ih = NULL;
204 	}
205 	if (psc->psc_mapsize) {
206 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, psc->psc_mapsize);
207 		psc->psc_mapsize = 0;
208 	}
209 	return;
210 }
211 
212 int
213 bwi_pci_detach(device_t self, int flags)
214 {
215 	struct bwi_pci_softc *psc = device_private(self);
216 	struct bwi_softc *sc = &psc->psc_bwi;
217 
218 	pmf_device_deregister(self);
219 
220 	bwi_detach(sc);
221 
222 	if (sc->sc_ih != NULL) {
223 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
224 		sc->sc_ih = NULL;
225 	}
226 
227 	return (0);
228 }
229 
230 static void
231 bwi_pci_conf_write(void *sc, uint32_t reg, uint32_t val)
232 {
233 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc;
234 
235 	pci_conf_write(psc->psc_pc, psc->psc_pcitag, reg, val);
236 }
237 
238 static uint32_t
239 bwi_pci_conf_read(void *sc, uint32_t reg)
240 {
241 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc;
242 
243 	return (pci_conf_read(psc->psc_pc, psc->psc_pcitag, reg));
244 }
245