xref: /openbsd/sys/dev/pci/if_bwi_pci.c (revision 3cab2bb3)
1 /*	$OpenBSD: if_bwi_pci.c,v 1.17 2019/05/10 16:44:36 bcook Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * PCI front-end for the Broadcom AirForce
21  */
22 
23 #include "bpfilter.h"
24 
25 #include <sys/param.h>
26 #include <sys/sockio.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/timeout.h>
33 #include <sys/device.h>
34 
35 #include <machine/bus.h>
36 
37 #include <net/if.h>
38 #include <net/if_media.h>
39 
40 #include <netinet/in.h>
41 #include <netinet/if_ether.h>
42 
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_amrr.h>
45 #include <net80211/ieee80211_radiotap.h>
46 
47 #include <dev/ic/bwivar.h>
48 #include <dev/ic/bwireg.h>
49 
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcidevs.h>
53 
54 /* Base Address Register */
55 #define BWI_PCI_BAR0	0x10
56 
57 int		bwi_pci_match(struct device *, void *, void *);
58 void		bwi_pci_attach(struct device *, struct device *, void *);
59 int		bwi_pci_detach(struct device *, int);
60 void		bwi_pci_conf_write(void *, uint32_t, uint32_t);
61 uint32_t	bwi_pci_conf_read(void *, uint32_t);
62 int		bwi_pci_activate(struct device *, int);
63 void		bwi_pci_wakeup(struct bwi_softc *);
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 	void 			*psc_ih;
71 
72 	bus_size_t		 psc_mapsize;
73 };
74 
75 struct cfattach bwi_pci_ca = {
76 	sizeof(struct bwi_pci_softc), bwi_pci_match, bwi_pci_attach,
77 	bwi_pci_detach, bwi_pci_activate
78 };
79 
80 const struct pci_matchid bwi_pci_devices[] = {
81 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4303 },
82 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4306 },
83 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4306_2 },
84 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4307 },
85 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4309 },
86 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4311 },
87 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4312 },
88 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4318 },
89 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4319 },
90 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM43XG },
91 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4331 },
92 };
93 
94 int
95 bwi_pci_match(struct device *parent, void *match, void *aux)
96 {
97 	struct pci_attach_args *pa = aux;
98 
99 	/*
100 	 * The second revision of the BCM4311/BCM4312
101 	 * chips require v4 firmware.
102 	 */
103 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROADCOM &&
104             (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM4311 ||
105 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM4312) &&
106 	     PCI_REVISION(pa->pa_class) == 0x02)
107 		return (0);
108 
109 	return (pci_matchbyid((struct pci_attach_args *)aux, bwi_pci_devices,
110 	    sizeof(bwi_pci_devices) / sizeof(bwi_pci_devices[0])));
111 }
112 
113 void
114 bwi_reset_bcm4331(struct bwi_softc *sc)
115 {
116 	int i;
117 
118 	/*
119 	 * The BCM4331 is not actually supported by this driver, but buggy EFI
120 	 * revisions in 2011-2012 Macs leave this chip enabled by default,
121 	 * causing it to emit spurious interrupts when the shared interrupt
122 	 * line is enabled.
123 	 */
124 	for (i = 0; CSR_READ_4(sc, BWI_RESET_STATUS) && i < 30; i++)
125 		delay(10);
126 
127 	CSR_WRITE_4(sc, BWI_RESET_CTRL, BWI_RESET_CTRL_RESET);
128 	CSR_READ_4(sc, BWI_RESET_CTRL);
129 	delay(1);
130 	CSR_WRITE_4(sc, BWI_RESET_CTRL, 0);
131 	CSR_READ_4(sc, BWI_RESET_CTRL);
132 	delay(10);
133 }
134 
135 void
136 bwi_pci_attach(struct device *parent, struct device *self, void *aux)
137 {
138 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)self;
139 	struct pci_attach_args *pa = aux;
140 	struct bwi_softc *sc = &psc->psc_bwi;
141 	const char *intrstr = NULL;
142 	pci_intr_handle_t ih;
143 	pcireg_t memtype, reg;
144 
145 	sc->sc_dmat = pa->pa_dmat;
146 	psc->psc_pc = pa->pa_pc;
147 	psc->psc_pcitag = pa->pa_tag;
148 
149 	/* map control / status registers */
150 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BWI_PCI_BAR0);
151 	if (pci_mapreg_map(pa, BWI_PCI_BAR0, memtype, 0, &sc->sc_mem_bt,
152 	    &sc->sc_mem_bh, NULL, &psc->psc_mapsize, 0)) {
153 		printf(": can't map mem space\n");
154 		return;
155 	}
156 
157 	/* we need to access PCI config space from the driver */
158 	sc->sc_conf_write = bwi_pci_conf_write;
159 	sc->sc_conf_read = bwi_pci_conf_read;
160 
161 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
162 
163 	sc->sc_pci_revid = PCI_REVISION(pa->pa_class);
164 	sc->sc_pci_did = PCI_PRODUCT(pa->pa_id);
165 	sc->sc_pci_subvid = PCI_VENDOR(reg);
166 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
167 
168 	if (sc->sc_pci_did == PCI_PRODUCT_BROADCOM_BCM4331) {
169 		printf(": disabling\n");
170 		bwi_reset_bcm4331(sc);
171 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, psc->psc_mapsize);
172 		return;
173 	}
174 
175 	/* map interrupt */
176 	if (pci_intr_map(pa, &ih) != 0) {
177 		printf(": can't map interrupt\n");
178 		return;
179 	}
180 
181 	/* establish interrupt */
182 	intrstr = pci_intr_string(psc->psc_pc, ih);
183 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_NET, bwi_intr, sc,
184 	    sc->sc_dev.dv_xname);
185 	if (psc->psc_ih == NULL) {
186 		printf(": can't establish interrupt");
187 		if (intrstr != NULL)
188 			printf(" at %s", intrstr);
189 		printf("\n");
190 		return;
191 	}
192 	printf(": %s", intrstr);
193 
194 	bwi_attach(sc);
195 }
196 
197 int
198 bwi_pci_detach(struct device *self, int flags)
199 {
200 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)self;
201 	struct bwi_softc *sc = &psc->psc_bwi;
202 
203 	bwi_detach(sc);
204 	pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
205 
206 	return (0);
207 }
208 
209 int
210 bwi_pci_activate(struct device *self, int act)
211 {
212 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)self;
213 	struct bwi_softc *sc = &psc->psc_bwi;
214 	struct ifnet *ifp = &sc->sc_ic.ic_if;
215 
216 	switch (act) {
217 	case DVACT_SUSPEND:
218 		if (ifp->if_flags & IFF_RUNNING)
219 			bwi_stop(sc, 1);
220 		break;
221 	case DVACT_WAKEUP:
222 		bwi_pci_wakeup(sc);
223 		break;
224 	}
225 
226 	return (0);
227 }
228 
229 void
230 bwi_pci_wakeup(struct bwi_softc *sc)
231 {
232 	struct ifnet *ifp = &sc->sc_ic.ic_if;
233 
234 	if (ifp->if_flags & IFF_UP)
235 		bwi_init(ifp);
236 }
237 
238 void
239 bwi_pci_conf_write(void *self, uint32_t reg, uint32_t val)
240 {
241 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)self;
242 
243 	pci_conf_write(psc->psc_pc, psc->psc_pcitag, reg, val);
244 }
245 
246 uint32_t
247 bwi_pci_conf_read(void *self, uint32_t reg)
248 {
249 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)self;
250 
251 	return (pci_conf_read(psc->psc_pc, psc->psc_pcitag, reg));
252 }
253