xref: /openbsd/sys/dev/cardbus/if_bwi_cardbus.c (revision c7101648)
1 /*	$OpenBSD: if_bwi_cardbus.c,v 1.17 2024/05/24 06:26:47 jsg Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
5  * Copyright (c) 2006 Claudio Jeker <claudio@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  * Cardbus front-end for the Broadcom AirForce
22  */
23 
24 #include "bpfilter.h"
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/timeout.h>
29 
30 #include <net/if.h>
31 #include <net/if_media.h>
32 
33 #include <netinet/in.h>
34 #include <netinet/if_ether.h>
35 
36 #include <net80211/ieee80211_var.h>
37 #include <net80211/ieee80211_amrr.h>
38 #include <net80211/ieee80211_radiotap.h>
39 
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcidevs.h>
43 
44 #include <dev/cardbus/cardbusvar.h>
45 
46 #include <dev/ic/bwivar.h>
47 
48 struct bwi_cardbus_softc {
49 	struct bwi_softc	 csc_bwi;
50 
51 	/* cardbus specific goo */
52 	cardbus_devfunc_t	 csc_ct;
53 	pcitag_t		 csc_tag;
54 	void			*csc_ih;
55 
56 	bus_size_t		 csc_mapsize;
57 	pcireg_t		 csc_bar_val;
58 	int			 csc_intrline;
59 	pci_chipset_tag_t	 csc_pc;
60 };
61 
62 int		bwi_cardbus_match(struct device *, void *, void*);
63 void		bwi_cardbus_attach(struct device *, struct device *, void *);
64 int		bwi_cardbus_detach(struct device *, int);
65 void		bwi_cardbus_setup(struct bwi_cardbus_softc *);
66 int		bwi_cardbus_enable(struct bwi_softc *);
67 void		bwi_cardbus_disable(struct bwi_softc *);
68 void		bwi_cardbus_conf_write(void *, uint32_t, uint32_t);
69 uint32_t	bwi_cardbus_conf_read(void *, uint32_t);
70 
71 const struct cfattach bwi_cardbus_ca = {
72 	sizeof (struct bwi_cardbus_softc), bwi_cardbus_match,
73 	bwi_cardbus_attach, bwi_cardbus_detach
74 };
75 
76 static const struct pci_matchid bwi_cardbus_devices[] = {
77 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4303 },
78 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4306 },
79 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4306_2 },
80 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4307 },
81 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4309 },
82 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4318 },
83 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4319 },
84 	{ PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM43XG }
85 };
86 
87 int
bwi_cardbus_match(struct device * parent,void * match,void * aux)88 bwi_cardbus_match(struct device *parent, void *match, void *aux)
89 {
90 	return (cardbus_matchbyid(aux, bwi_cardbus_devices,
91 	    sizeof (bwi_cardbus_devices) / sizeof (bwi_cardbus_devices[0])));
92 }
93 
94 void
bwi_cardbus_attach(struct device * parent,struct device * self,void * aux)95 bwi_cardbus_attach(struct device *parent, struct device *self, void *aux)
96 {
97 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)self;
98 	struct cardbus_attach_args *ca = aux;
99 	struct bwi_softc *sc = &csc->csc_bwi;
100 	cardbus_devfunc_t ct = ca->ca_ct;
101 	pcireg_t reg;
102 	bus_addr_t base;
103 	int error;
104 
105 	sc->sc_dmat = ca->ca_dmat;
106 	csc->csc_ct = ct;
107 	csc->csc_tag = ca->ca_tag;
108 	csc->csc_intrline = ca->ca_intrline;
109 	csc->csc_pc = ca->ca_pc;
110 
111 	/* power management hooks */
112 	sc->sc_enable = bwi_cardbus_enable;
113 	sc->sc_disable = bwi_cardbus_disable;
114 	//sc->sc_power = bwi_cardbus_power;
115 
116 	/* map control/status registers */
117 	error = Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG,
118 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem_bt,
119 	    &sc->sc_mem_bh, &base, &csc->csc_mapsize);
120 	if (error != 0) {
121 		printf(": can't map mem space\n");
122 		return;
123 	}
124 	csc->csc_bar_val = base | PCI_MAPREG_TYPE_MEM;
125 
126 	/* set up the PCI configuration registers */
127 	bwi_cardbus_setup(csc);
128 
129 	printf(": irq %d", csc->csc_intrline);
130 
131 	/* we need to access Cardbus config space from the driver */
132 	sc->sc_conf_read = bwi_cardbus_conf_read;
133 	sc->sc_conf_write = bwi_cardbus_conf_write;
134 
135 	reg = (sc->sc_conf_read)(sc, PCI_SUBSYS_ID_REG);
136 
137 	sc->sc_pci_revid = PCI_REVISION(ca->ca_class);
138 	sc->sc_pci_did = PCI_PRODUCT(ca->ca_id);
139 	sc->sc_pci_subvid = PCI_VENDOR(reg);
140 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
141 
142 	error = bwi_attach(sc);
143 	if (error != 0)
144 		bwi_cardbus_detach(&sc->sc_dev, 0);
145 
146 	Cardbus_function_disable(ct);
147 }
148 
149 int
bwi_cardbus_detach(struct device * self,int flags)150 bwi_cardbus_detach(struct device *self, int flags)
151 {
152 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)self;
153 	struct bwi_softc *sc = &csc->csc_bwi;
154 	cardbus_devfunc_t ct = csc->csc_ct;
155 	cardbus_chipset_tag_t cc = ct->ct_cc;
156 	cardbus_function_tag_t cf = ct->ct_cf;
157 	int error;
158 
159 	error = bwi_detach(sc);
160 	if (error != 0)
161 		return (error);
162 
163 	/* unhook the interrupt handler */
164 	if (csc->csc_ih != NULL) {
165 		cardbus_intr_disestablish(cc, cf, csc->csc_ih);
166 		csc->csc_ih = NULL;
167 	}
168 
169 	/* release bus space and close window */
170 	Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->sc_mem_bt,
171 	    sc->sc_mem_bh, csc->csc_mapsize);
172 
173 	return (0);
174 }
175 
176 void
bwi_cardbus_setup(struct bwi_cardbus_softc * csc)177 bwi_cardbus_setup(struct bwi_cardbus_softc *csc)
178 {
179 	cardbus_devfunc_t ct = csc->csc_ct;
180 	cardbus_chipset_tag_t cc = ct->ct_cc;
181 	pci_chipset_tag_t pc = csc->csc_pc;
182 	cardbus_function_tag_t cf = ct->ct_cf;
183 	pcireg_t reg;
184 
185 	/* program the BAR */
186 	pci_conf_write(pc, csc->csc_tag, CARDBUS_BASE0_REG,
187 	    csc->csc_bar_val);
188 
189 	/* make sure the right access type is on the cardbus bridge */
190 	(*cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
191 	(*cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
192 
193 	/* enable the appropriate bits in the PCI CSR */
194 	reg = pci_conf_read(pc, csc->csc_tag,
195 	    PCI_COMMAND_STATUS_REG);
196 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
197 	pci_conf_write(pc, csc->csc_tag, PCI_COMMAND_STATUS_REG,
198 	    reg);
199 }
200 
201 int
bwi_cardbus_enable(struct bwi_softc * sc)202 bwi_cardbus_enable(struct bwi_softc *sc)
203 {
204 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
205 	cardbus_devfunc_t ct = csc->csc_ct;
206 	cardbus_chipset_tag_t cc = ct->ct_cc;
207 	cardbus_function_tag_t cf = ct->ct_cf;
208 
209 	/* power on the socket */
210 	Cardbus_function_enable(ct);
211 
212 	/* setup the PCI configuration registers */
213 	bwi_cardbus_setup(csc);
214 
215 	/* map and establish the interrupt handler */
216 	csc->csc_ih = cardbus_intr_establish(cc, cf, csc->csc_intrline, IPL_NET,
217 	    bwi_intr, sc, sc->sc_dev.dv_xname);
218 	if (csc->csc_ih == NULL) {
219 		printf("%s: could not establish interrupt at %d\n",
220 		    sc->sc_dev.dv_xname, csc->csc_intrline);
221 		Cardbus_function_disable(ct);
222 		return (1);
223 	}
224 
225 	return (0);
226 }
227 
228 void
bwi_cardbus_disable(struct bwi_softc * sc)229 bwi_cardbus_disable(struct bwi_softc *sc)
230 {
231 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
232 	cardbus_devfunc_t ct = csc->csc_ct;
233 	cardbus_chipset_tag_t cc = ct->ct_cc;
234 	cardbus_function_tag_t cf = ct->ct_cf;
235 
236 	/* unhook the interrupt handler */
237 	cardbus_intr_disestablish(cc, cf, csc->csc_ih);
238 	csc->csc_ih = NULL;
239 
240 	/* power down the socket */
241 	Cardbus_function_disable(ct);
242 }
243 
244 void
bwi_cardbus_conf_write(void * self,uint32_t reg,uint32_t val)245 bwi_cardbus_conf_write(void *self, uint32_t reg, uint32_t val)
246 {
247 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)self;
248 	pci_chipset_tag_t pc = csc->csc_pc;
249 
250 	pci_conf_write(pc, csc->csc_tag, reg, val);
251 }
252 
253 uint32_t
bwi_cardbus_conf_read(void * self,uint32_t reg)254 bwi_cardbus_conf_read(void *self, uint32_t reg)
255 {
256 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)self;
257 	pci_chipset_tag_t pc = csc->csc_pc;
258 
259 	return (pci_conf_read(pc, csc->csc_tag, reg));
260 }
261