This source file includes following definitions.
- rtw_cardbus_match
- rtw_cardbus_intr_ack
- rtw_cardbus_funcregen
- rtw_cardbus_attach
- rtw_cardbus_detach
- rtw_cardbus_enable
- rtw_cardbus_disable
- rtw_cardbus_power
- rtw_cardbus_setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 #include <sys/cdefs.h>
78
79 #include "bpfilter.h"
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/mbuf.h>
84 #include <sys/malloc.h>
85 #include <sys/kernel.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/errno.h>
89 #include <sys/device.h>
90
91 #include <machine/endian.h>
92
93 #include <net/if.h>
94 #include <net/if_dl.h>
95 #include <net/if_media.h>
96 #ifdef INET
97 #include <netinet/in.h>
98 #include <netinet/if_ether.h>
99 #endif
100
101 #include <net80211/ieee80211_radiotap.h>
102 #include <net80211/ieee80211_var.h>
103
104 #if NBPFILTER > 0
105 #include <net/bpf.h>
106 #endif
107
108 #include <machine/bus.h>
109 #include <machine/intr.h>
110
111 #include <dev/ic/rtwreg.h>
112 #include <dev/ic/rtwvar.h>
113
114 #include <dev/pci/pcivar.h>
115 #include <dev/pci/pcireg.h>
116 #include <dev/pci/pcidevs.h>
117
118 #include <dev/cardbus/cardbusvar.h>
119 #include <dev/pci/pcidevs.h>
120
121
122
123
124 #define RTW_PCI_IOBA 0x10
125 #define RTW_PCI_MMBA 0x14
126
127 struct rtw_cardbus_softc {
128 struct rtw_softc sc_rtw;
129
130
131 void *sc_ih;
132 cardbus_devfunc_t sc_ct;
133 cardbustag_t sc_tag;
134 int sc_csr;
135 bus_size_t sc_mapsize;
136
137
138
139 int sc_cben;
140 int sc_bar_reg;
141 pcireg_t sc_bar_val;
142
143 int sc_intrline;
144 };
145
146 int rtw_cardbus_match(struct device *, void *, void *);
147 void rtw_cardbus_attach(struct device *, struct device *, void *);
148 int rtw_cardbus_detach(struct device *, int);
149 void rtw_cardbus_intr_ack(struct rtw_regs *);
150 void rtw_cardbus_funcregen(struct rtw_regs *, int);
151
152 struct cfattach rtw_cardbus_ca = {
153 sizeof(struct rtw_cardbus_softc), rtw_cardbus_match, rtw_cardbus_attach,
154 rtw_cardbus_detach
155 };
156
157 void rtw_cardbus_setup(struct rtw_cardbus_softc *);
158
159 int rtw_cardbus_enable(struct rtw_softc *);
160 void rtw_cardbus_disable(struct rtw_softc *);
161 void rtw_cardbus_power(struct rtw_softc *, int);
162
163 const struct cardbus_matchid rtw_cardbus_devices[] = {
164 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8185 },
165 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8180 },
166 { PCI_VENDOR_BELKIN2, PCI_PRODUCT_BELKIN2_F5D6020V3 },
167 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DWL610 },
168 };
169
170 int
171 rtw_cardbus_match(struct device *parent, void *match, void *aux)
172 {
173 return (cardbus_matchbyid((struct cardbus_attach_args *)aux,
174 rtw_cardbus_devices,
175 sizeof(rtw_cardbus_devices)/sizeof(rtw_cardbus_devices[0])));
176 }
177
178 void
179 rtw_cardbus_intr_ack(struct rtw_regs *regs)
180 {
181 RTW_WRITE(regs, RTW_FER, RTW_FER_INTR);
182 }
183
184 void
185 rtw_cardbus_funcregen(struct rtw_regs *regs, int enable)
186 {
187 u_int32_t reg;
188 rtw_config0123_enable(regs, 1);
189 reg = RTW_READ(regs, RTW_CONFIG3);
190 if (enable) {
191 RTW_WRITE(regs, RTW_CONFIG3, reg | RTW_CONFIG3_FUNCREGEN);
192 } else {
193 RTW_WRITE(regs, RTW_CONFIG3, reg & ~RTW_CONFIG3_FUNCREGEN);
194 }
195 rtw_config0123_enable(regs, 0);
196 }
197
198 void
199 rtw_cardbus_attach(struct device *parent, struct device *self, void *aux)
200 {
201 struct rtw_cardbus_softc *csc = (void *)self;
202 struct rtw_softc *sc = &csc->sc_rtw;
203 struct rtw_regs *regs = &sc->sc_regs;
204 struct cardbus_attach_args *ca = aux;
205 cardbus_devfunc_t ct = ca->ca_ct;
206 bus_addr_t adr;
207 int rev;
208
209 sc->sc_dmat = ca->ca_dmat;
210 csc->sc_ct = ct;
211 csc->sc_tag = ca->ca_tag;
212
213
214
215
216 sc->sc_enable = rtw_cardbus_enable;
217 sc->sc_disable = rtw_cardbus_disable;
218 sc->sc_power = rtw_cardbus_power;
219
220 sc->sc_intr_ack = rtw_cardbus_intr_ack;
221
222
223 rev = PCI_REVISION(ca->ca_class);
224
225 RTW_DPRINTF(RTW_DEBUG_ATTACH,
226 ("%s: pass %d.%d signature %08x\n", sc->sc_dev.dv_xname,
227 (rev >> 4) & 0xf, rev & 0xf,
228 cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag, 0x80)));
229
230
231
232
233 csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
234 if (Cardbus_mapreg_map(ct, RTW_PCI_MMBA,
235 CARDBUS_MAPREG_TYPE_MEM, 0, ®s->r_bt, ®s->r_bh, &adr,
236 &csc->sc_mapsize) == 0) {
237 RTW_DPRINTF(RTW_DEBUG_ATTACH,
238 ("%s: %s mapped %lu bytes mem space\n",
239 sc->sc_dev.dv_xname, __func__, (long)csc->sc_mapsize));
240 csc->sc_cben = CARDBUS_MEM_ENABLE;
241 csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
242 csc->sc_bar_reg = RTW_PCI_MMBA;
243 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
244 } else if (Cardbus_mapreg_map(ct, RTW_PCI_IOBA,
245 CARDBUS_MAPREG_TYPE_IO, 0, ®s->r_bt, ®s->r_bh, &adr,
246 &csc->sc_mapsize) == 0) {
247 RTW_DPRINTF(RTW_DEBUG_ATTACH,
248 ("%s: %s mapped %lu bytes I/O space\n",
249 sc->sc_dev.dv_xname, __func__, (long)csc->sc_mapsize));
250 csc->sc_cben = CARDBUS_IO_ENABLE;
251 csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
252 csc->sc_bar_reg = RTW_PCI_IOBA;
253 csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
254 } else {
255 printf("%s: unable to map device registers\n",
256 sc->sc_dev.dv_xname);
257 return;
258 }
259
260
261
262
263
264 rtw_cardbus_setup(csc);
265
266
267 csc->sc_intrline = ca->ca_intrline;
268
269 printf(" irq %d\n", csc->sc_intrline);
270
271
272
273
274 rtw_attach(sc);
275
276 rtw_cardbus_funcregen(regs, 1);
277
278 RTW_WRITE(regs, RTW_FEMR, RTW_FEMR_INTR);
279 RTW_WRITE(regs, RTW_FER, RTW_FER_INTR);
280
281
282
283
284 Cardbus_function_disable(csc->sc_ct);
285 }
286
287 int
288 rtw_cardbus_detach(struct device *self, int flags)
289 {
290 struct rtw_cardbus_softc *csc = (void *)self;
291 struct rtw_softc *sc = &csc->sc_rtw;
292 struct rtw_regs *regs = &sc->sc_regs;
293 struct cardbus_devfunc *ct = csc->sc_ct;
294 int rv;
295
296 #if defined(DIAGNOSTIC)
297 if (ct == NULL)
298 panic("%s: data structure lacks", sc->sc_dev.dv_xname);
299 #endif
300
301 rv = rtw_detach(sc);
302 if (rv)
303 return (rv);
304
305 rtw_cardbus_funcregen(regs, 0);
306
307
308
309
310 if (csc->sc_ih != NULL)
311 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
312
313
314
315
316 if (csc->sc_bar_reg != 0)
317 Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
318 regs->r_bt, regs->r_bh, csc->sc_mapsize);
319
320 return (0);
321 }
322
323 int
324 rtw_cardbus_enable(struct rtw_softc *sc)
325 {
326 struct rtw_cardbus_softc *csc = (void *) sc;
327 cardbus_devfunc_t ct = csc->sc_ct;
328 cardbus_chipset_tag_t cc = ct->ct_cc;
329 cardbus_function_tag_t cf = ct->ct_cf;
330
331
332
333
334 Cardbus_function_enable(ct);
335
336
337
338
339 rtw_cardbus_setup(csc);
340
341
342
343
344 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
345 rtw_intr, sc, sc->sc_dev.dv_xname);
346 if (csc->sc_ih == NULL) {
347 printf("%s: unable to establish interrupt at %d\n",
348 sc->sc_dev.dv_xname, csc->sc_intrline);
349 Cardbus_function_disable(csc->sc_ct);
350 return (1);
351 }
352
353 rtw_cardbus_funcregen(&sc->sc_regs, 1);
354
355 RTW_WRITE(&sc->sc_regs, RTW_FEMR, RTW_FEMR_INTR);
356 RTW_WRITE(&sc->sc_regs, RTW_FER, RTW_FER_INTR);
357
358 return (0);
359 }
360
361 void
362 rtw_cardbus_disable(struct rtw_softc *sc)
363 {
364 struct rtw_cardbus_softc *csc = (void *) sc;
365 cardbus_devfunc_t ct = csc->sc_ct;
366 cardbus_chipset_tag_t cc = ct->ct_cc;
367 cardbus_function_tag_t cf = ct->ct_cf;
368
369 RTW_WRITE(&sc->sc_regs, RTW_FEMR,
370 RTW_READ(&sc->sc_regs, RTW_FEMR) & ~RTW_FEMR_INTR);
371
372 rtw_cardbus_funcregen(&sc->sc_regs, 0);
373
374
375 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
376 csc->sc_ih = NULL;
377
378
379 Cardbus_function_disable(ct);
380 }
381
382 void
383 rtw_cardbus_power(struct rtw_softc *sc, int why)
384 {
385 RTW_DPRINTF(RTW_DEBUG_ATTACH,
386 ("%s: rtw_cardbus_power\n", sc->sc_dev.dv_xname));
387
388 if (why == PWR_RESUME)
389 rtw_enable(sc);
390 }
391
392 void
393 rtw_cardbus_setup(struct rtw_cardbus_softc *csc)
394 {
395 struct rtw_softc *sc = &csc->sc_rtw;
396 cardbus_devfunc_t ct = csc->sc_ct;
397 cardbus_chipset_tag_t cc = ct->ct_cc;
398 cardbus_function_tag_t cf = ct->ct_cf;
399 pcireg_t reg;
400 int pmreg;
401
402 if (cardbus_get_capability(cc, cf, csc->sc_tag,
403 PCI_CAP_PWRMGMT, &pmreg, 0)) {
404 reg = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4) & 0x03;
405 #if 1
406 if (reg == 3) {
407
408
409
410
411 printf("%s: unable to wake up from power state D3\n",
412 sc->sc_dev.dv_xname);
413 return;
414 }
415 #endif
416 if (reg != 0) {
417 printf("%s: waking up from power state D%d\n",
418 sc->sc_dev.dv_xname, reg);
419 cardbus_conf_write(cc, cf, csc->sc_tag,
420 pmreg + 4, 0);
421 }
422 }
423
424
425 cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
426 csc->sc_bar_val);
427
428
429 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
430 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
431
432
433 reg = cardbus_conf_read(cc, cf, csc->sc_tag,
434 CARDBUS_COMMAND_STATUS_REG);
435 reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
436 reg |= csc->sc_csr;
437 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
438 reg);
439
440
441
442
443
444 reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
445 if (CARDBUS_LATTIMER(reg) < 0x20) {
446 reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
447 reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
448 cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
449 }
450 }