1 /* $OpenBSD: if_ath_cardbus.c,v 1.22 2024/05/24 06:26:47 jsg Exp $ */
2 /* $NetBSD: if_ath_cardbus.c,v 1.4 2004/08/02 19:14:28 mycroft Exp $ */
3
4 /*
5 * Copyright (c) 2003
6 * Ichiro FUKUHARA <ichiro@ichiro.org>.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * CardBus bus front-end for the AR5001 Wireless LAN 802.11a/b/g CardBus.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/gpio.h>
39
40 #include <net/if.h>
41 #include <net/if_media.h>
42
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
45
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_rssadapt.h>
48
49 #include <machine/bus.h>
50
51 #include <dev/gpio/gpiovar.h>
52
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
55
56 #include <dev/cardbus/cardbusvar.h>
57
58 #include <dev/ic/athvar.h>
59
60 /*
61 * PCI configuration space registers
62 */
63 #define ATH_PCI_MMBA 0x10 /* memory mapped base */
64
65 struct ath_cardbus_softc {
66 struct ath_softc sc_ath;
67
68 /* CardBus-specific goo. */
69 void *sc_ih; /* interrupt handle */
70 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
71 pcitag_t sc_tag; /* our CardBus tag */
72
73 pcireg_t sc_bar_val; /* value of the BAR */
74
75 int sc_intrline; /* interrupt line */
76 pci_chipset_tag_t sc_pc;
77 };
78
79 int ath_cardbus_match(struct device *, void *, void *);
80 void ath_cardbus_attach(struct device *, struct device *, void *);
81 int ath_cardbus_detach(struct device *, int);
82
83 const struct cfattach ath_cardbus_ca = {
84 sizeof(struct ath_cardbus_softc),
85 ath_cardbus_match,
86 ath_cardbus_attach,
87 ath_cardbus_detach
88 };
89
90
91 void ath_cardbus_setup(struct ath_cardbus_softc *);
92
93 int ath_cardbus_enable(struct ath_softc *);
94 void ath_cardbus_disable(struct ath_softc *);
95 void ath_cardbus_power(struct ath_softc *, int);
96
97 int
ath_cardbus_match(struct device * parent,void * match,void * aux)98 ath_cardbus_match(struct device *parent, void *match, void *aux)
99 {
100 struct cardbus_attach_args *ca = aux;
101 const char* devname;
102
103 devname = ath_hal_probe(PCI_VENDOR(ca->ca_id),
104 PCI_PRODUCT(ca->ca_id));
105
106 if (devname)
107 return (1);
108
109 return (0);
110 }
111
112 void
ath_cardbus_attach(struct device * parent,struct device * self,void * aux)113 ath_cardbus_attach(struct device *parent, struct device *self, void *aux)
114 {
115 struct ath_cardbus_softc *csc = (void *)self;
116 struct ath_softc *sc = &csc->sc_ath;
117 struct cardbus_attach_args *ca = aux;
118 cardbus_devfunc_t ct = ca->ca_ct;
119 bus_addr_t adr;
120
121 sc->sc_dmat = ca->ca_dmat;
122 csc->sc_ct = ct;
123 csc->sc_tag = ca->ca_tag;
124 csc->sc_pc = ca->ca_pc;
125
126 /*
127 * Power management hooks.
128 */
129 sc->sc_enable = ath_cardbus_enable;
130 sc->sc_disable = ath_cardbus_disable;
131 sc->sc_power = ath_cardbus_power;
132
133 /*
134 * Map the device.
135 */
136 if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0,
137 &sc->sc_st, &sc->sc_sh, &adr, &sc->sc_ss) == 0) {
138 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
139 }
140
141 else {
142 printf(": unable to map device registers\n");
143 return;
144 }
145
146 /*
147 * Set up the PCI configuration registers.
148 */
149 ath_cardbus_setup(csc);
150
151 /* Remember which interrupt line. */
152 csc->sc_intrline = ca->ca_intrline;
153
154 printf(": irq %d\n", csc->sc_intrline);
155
156 /*
157 * Finish off the attach.
158 */
159 ath_attach(PCI_PRODUCT(ca->ca_id), sc);
160
161 /*
162 * Power down the socket.
163 */
164 Cardbus_function_disable(csc->sc_ct);
165 }
166
167 int
ath_cardbus_detach(struct device * self,int flags)168 ath_cardbus_detach(struct device *self, int flags)
169 {
170 struct ath_cardbus_softc *csc = (void *)self;
171 struct ath_softc *sc = &csc->sc_ath;
172 struct cardbus_devfunc *ct = csc->sc_ct;
173 int rv;
174
175 #if defined(DIAGNOSTIC)
176 if (ct == NULL)
177 panic("%s: data structure lacks", sc->sc_dev.dv_xname);
178 #endif
179
180 rv = ath_detach(sc, flags);
181 if (rv)
182 return (rv);
183
184 /*
185 * Unhook the interrupt handler.
186 */
187 if (csc->sc_ih != NULL) {
188 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
189 csc->sc_ih = NULL;
190 }
191
192 /*
193 * Release bus space and close window.
194 */
195 Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA,
196 sc->sc_st, sc->sc_sh, sc->sc_ss);
197
198 return (0);
199 }
200
201 int
ath_cardbus_enable(struct ath_softc * sc)202 ath_cardbus_enable(struct ath_softc *sc)
203 {
204 struct ath_cardbus_softc *csc = (void *) sc;
205 cardbus_devfunc_t ct = csc->sc_ct;
206 cardbus_chipset_tag_t cc = ct->ct_cc;
207 cardbus_function_tag_t cf = ct->ct_cf;
208
209 /*
210 * Power on the socket.
211 */
212 Cardbus_function_enable(ct);
213
214 /*
215 * Set up the PCI configuration registers.
216 */
217 ath_cardbus_setup(csc);
218
219 /*
220 * Map and establish the interrupt.
221 */
222 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
223 ath_intr, sc, sc->sc_dev.dv_xname);
224 if (csc->sc_ih == NULL) {
225 printf(": unable to establish irq %d\n",
226 csc->sc_intrline);
227 Cardbus_function_disable(csc->sc_ct);
228 return (1);
229 }
230 return (0);
231 }
232
233 void
ath_cardbus_disable(struct ath_softc * sc)234 ath_cardbus_disable(struct ath_softc *sc)
235 {
236 struct ath_cardbus_softc *csc = (void *) sc;
237 cardbus_devfunc_t ct = csc->sc_ct;
238 cardbus_chipset_tag_t cc = ct->ct_cc;
239 cardbus_function_tag_t cf = ct->ct_cf;
240
241 /* Unhook the interrupt handler. */
242 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
243 csc->sc_ih = NULL;
244
245 /* Power down the socket. */
246 Cardbus_function_disable(ct);
247 }
248
249 void
ath_cardbus_power(struct ath_softc * sc,int why)250 ath_cardbus_power(struct ath_softc *sc, int why)
251 {
252 if (why == DVACT_RESUME)
253 ath_enable(sc);
254 }
255
256 void
ath_cardbus_setup(struct ath_cardbus_softc * csc)257 ath_cardbus_setup(struct ath_cardbus_softc *csc)
258 {
259 cardbus_devfunc_t ct = csc->sc_ct;
260 cardbus_chipset_tag_t cc = ct->ct_cc;
261 pci_chipset_tag_t pc = csc->sc_pc;
262 pcireg_t reg;
263
264 #ifdef notyet
265 (void)cardbus_setpowerstate(sc->sc_dev.dv_xname, ct, csc->sc_tag,
266 PCI_PWR_D0);
267 #endif
268
269 /* Program the BAR. */
270 pci_conf_write(pc, csc->sc_tag, ATH_PCI_MMBA,
271 csc->sc_bar_val);
272
273 /* Make sure the right access type is on the CardBus bridge. */
274 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
275 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
276
277 /* Enable the appropriate bits in the PCI CSR. */
278 reg = pci_conf_read(pc, csc->sc_tag,
279 PCI_COMMAND_STATUS_REG);
280 reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
281 pci_conf_write(pc, csc->sc_tag, PCI_COMMAND_STATUS_REG,
282 reg);
283
284 /*
285 * Make sure the latency timer is set to some reasonable
286 * value.
287 */
288 reg = pci_conf_read(pc, csc->sc_tag, PCI_BHLC_REG);
289 if (PCI_LATTIMER(reg) < 0x20) {
290 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
291 reg |= (0x20 << PCI_LATTIMER_SHIFT);
292 pci_conf_write(pc, csc->sc_tag, PCI_BHLC_REG, reg);
293 }
294 }
295