2 * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
3 * Copyright (c) 2012 Bernhard Schmidt <bschmidt@FreeBSD.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $OpenBSD: rt2860.c,v 1.65 2010/10/23 14:24:54 damien Exp $
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Ralink Technology RT2860/RT3090/RT3390/RT3562/RT5390/RT5392 chipset driver
25 * http://www.ralinktech.com/
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/endian.h>
40 #include <sys/firmware.h>
42 #if defined(__DragonFly__)
45 #include <machine/bus.h>
46 #include <machine/resource.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
59 #include <netproto/802_11/ieee80211_var.h>
60 #include <netproto/802_11/ieee80211_radiotap.h>
61 #include <netproto/802_11/ieee80211_regdomain.h>
62 #include <netproto/802_11/ieee80211_ratectl.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68 #include <netinet/if_ether.h>
70 #include <dev/netif/ral/rt2860reg.h>
71 #include <dev/netif/ral/rt2860var.h>
75 #define DPRINTF(x) do { if (sc->sc_debug > 0) kprintf x; } while (0)
76 #define DPRINTFN(n, x) do { if (sc->sc_debug >= (n)) kprintf x; } while (0)
79 #define DPRINTFN(n, x)
82 static struct ieee80211vap
*rt2860_vap_create(struct ieee80211com
*,
83 const char [IFNAMSIZ
], int, enum ieee80211_opmode
,
84 int, const uint8_t [IEEE80211_ADDR_LEN
],
85 const uint8_t [IEEE80211_ADDR_LEN
]);
86 static void rt2860_vap_delete(struct ieee80211vap
*);
87 static void rt2860_dma_map_addr(void *, bus_dma_segment_t
*, int, int);
88 static int rt2860_alloc_tx_ring(struct rt2860_softc
*,
89 struct rt2860_tx_ring
*);
90 static void rt2860_reset_tx_ring(struct rt2860_softc
*,
91 struct rt2860_tx_ring
*);
92 static void rt2860_free_tx_ring(struct rt2860_softc
*,
93 struct rt2860_tx_ring
*);
94 static int rt2860_alloc_tx_pool(struct rt2860_softc
*);
95 static void rt2860_free_tx_pool(struct rt2860_softc
*);
96 static int rt2860_alloc_rx_ring(struct rt2860_softc
*,
97 struct rt2860_rx_ring
*);
98 static void rt2860_reset_rx_ring(struct rt2860_softc
*,
99 struct rt2860_rx_ring
*);
100 static void rt2860_free_rx_ring(struct rt2860_softc
*,
101 struct rt2860_rx_ring
*);
102 static void rt2860_updatestats(struct rt2860_softc
*);
103 static void rt2860_newassoc(struct ieee80211_node
*, int);
104 static void rt2860_node_free(struct ieee80211_node
*);
106 static int rt2860_ampdu_rx_start(struct ieee80211com
*,
107 struct ieee80211_node
*, uint8_t);
108 static void rt2860_ampdu_rx_stop(struct ieee80211com
*,
109 struct ieee80211_node
*, uint8_t);
111 static int rt2860_newstate(struct ieee80211vap
*, enum ieee80211_state
,
113 static uint16_t rt3090_efuse_read_2(struct rt2860_softc
*, uint16_t);
114 static uint16_t rt2860_eeprom_read_2(struct rt2860_softc
*, uint16_t);
115 static void rt2860_intr_coherent(struct rt2860_softc
*);
116 static void rt2860_drain_stats_fifo(struct rt2860_softc
*);
117 static void rt2860_tx_intr(struct rt2860_softc
*, int);
118 static void rt2860_rx_intr(struct rt2860_softc
*);
119 static void rt2860_tbtt_intr(struct rt2860_softc
*);
120 static void rt2860_gp_intr(struct rt2860_softc
*);
121 static int rt2860_tx(struct rt2860_softc
*, struct mbuf
*,
122 struct ieee80211_node
*);
123 static int rt2860_raw_xmit(struct ieee80211_node
*, struct mbuf
*,
124 const struct ieee80211_bpf_params
*);
125 static int rt2860_tx_raw(struct rt2860_softc
*, struct mbuf
*,
126 struct ieee80211_node
*,
127 const struct ieee80211_bpf_params
*params
);
128 static int rt2860_transmit(struct ieee80211com
*, struct mbuf
*);
129 static void rt2860_start(struct rt2860_softc
*);
130 static void rt2860_watchdog(void *);
131 static void rt2860_parent(struct ieee80211com
*);
132 static void rt2860_mcu_bbp_write(struct rt2860_softc
*, uint8_t, uint8_t);
133 static uint8_t rt2860_mcu_bbp_read(struct rt2860_softc
*, uint8_t);
134 static void rt2860_rf_write(struct rt2860_softc
*, uint8_t, uint32_t);
135 static uint8_t rt3090_rf_read(struct rt2860_softc
*, uint8_t);
136 static void rt3090_rf_write(struct rt2860_softc
*, uint8_t, uint8_t);
137 static int rt2860_mcu_cmd(struct rt2860_softc
*, uint8_t, uint16_t, int);
138 static void rt2860_enable_mrr(struct rt2860_softc
*);
139 static void rt2860_set_txpreamble(struct rt2860_softc
*);
140 static void rt2860_set_basicrates(struct rt2860_softc
*,
141 const struct ieee80211_rateset
*);
142 static void rt2860_scan_start(struct ieee80211com
*);
143 static void rt2860_scan_end(struct ieee80211com
*);
144 static void rt2860_set_channel(struct ieee80211com
*);
145 static void rt2860_select_chan_group(struct rt2860_softc
*, int);
146 static void rt2860_set_chan(struct rt2860_softc
*, u_int
);
147 static void rt3090_set_chan(struct rt2860_softc
*, u_int
);
148 static void rt5390_set_chan(struct rt2860_softc
*, u_int
);
149 static int rt3090_rf_init(struct rt2860_softc
*);
150 static void rt5390_rf_init(struct rt2860_softc
*);
151 static void rt3090_rf_wakeup(struct rt2860_softc
*);
152 static void rt5390_rf_wakeup(struct rt2860_softc
*);
153 static int rt3090_filter_calib(struct rt2860_softc
*, uint8_t, uint8_t,
155 static void rt3090_rf_setup(struct rt2860_softc
*);
156 static void rt2860_set_leds(struct rt2860_softc
*, uint16_t);
157 static void rt2860_set_gp_timer(struct rt2860_softc
*, int);
158 static void rt2860_set_bssid(struct rt2860_softc
*, const uint8_t *);
159 static void rt2860_set_macaddr(struct rt2860_softc
*, const uint8_t *);
160 static void rt2860_update_promisc(struct ieee80211com
*);
161 static void rt2860_updateslot(struct ieee80211com
*);
162 static void rt2860_updateprot(struct rt2860_softc
*);
163 static int rt2860_updateedca(struct ieee80211com
*);
165 static int rt2860_set_key(struct ieee80211com
*, struct ieee80211_node
*,
166 struct ieee80211_key
*);
167 static void rt2860_delete_key(struct ieee80211com
*,
168 struct ieee80211_node
*, struct ieee80211_key
*);
170 static int8_t rt2860_rssi2dbm(struct rt2860_softc
*, uint8_t, uint8_t);
171 static const char *rt2860_get_rf(uint16_t);
172 static int rt2860_read_eeprom(struct rt2860_softc
*,
173 uint8_t macaddr
[IEEE80211_ADDR_LEN
]);
174 static int rt2860_bbp_init(struct rt2860_softc
*);
175 static void rt5390_bbp_init(struct rt2860_softc
*);
176 static int rt2860_txrx_enable(struct rt2860_softc
*);
177 static void rt2860_init(void *);
178 static void rt2860_init_locked(struct rt2860_softc
*);
179 static void rt2860_stop(void *);
180 static void rt2860_stop_locked(struct rt2860_softc
*);
181 static int rt2860_load_microcode(struct rt2860_softc
*);
183 static void rt2860_calib(struct rt2860_softc
*);
185 static void rt3090_set_rx_antenna(struct rt2860_softc
*, int);
186 static void rt2860_switch_chan(struct rt2860_softc
*,
187 struct ieee80211_channel
*);
188 static int rt2860_setup_beacon(struct rt2860_softc
*,
189 struct ieee80211vap
*);
190 static void rt2860_enable_tsf_sync(struct rt2860_softc
*);
192 static const struct {
195 } rt2860_def_mac
[] = {
199 static const struct {
202 } rt2860_def_bbp
[] = {
204 }, rt5390_def_bbp
[] = {
208 static const struct rfprog
{
210 uint32_t r1
, r2
, r3
, r4
;
211 } rt2860_rf2850
[] = {
221 static const struct {
224 } rt3090_def_rf
[] = {
226 }, rt5390_def_rf
[] = {
228 }, rt5392_def_rf
[] = {
233 rt2860_attach(device_t dev
, int id
)
235 struct rt2860_softc
*sc
= device_get_softc(dev
);
236 struct ieee80211com
*ic
= &sc
->sc_ic
;
238 uint8_t bands
[IEEE80211_MODE_BYTES
];
239 int error
, ntries
, qid
;
244 #if defined(__DragonFly__)
245 lockinit(&sc
->sc_mtx
, device_get_nameunit(dev
), 0, LK_CANRECURSE
);
247 mtx_init(&sc
->sc_mtx
, device_get_nameunit(dev
), MTX_NETWORK_LOCK
,
248 MTX_DEF
| MTX_RECURSE
);
251 callout_init_mtx(&sc
->watchdog_ch
, &sc
->sc_mtx
, 0);
252 mbufq_init(&sc
->sc_snd
, ifqmaxlen
);
254 /* wait for NIC to initialize */
255 for (ntries
= 0; ntries
< 100; ntries
++) {
256 tmp
= RAL_READ(sc
, RT2860_ASIC_VER_ID
);
257 if (tmp
!= 0 && tmp
!= 0xffffffff)
262 device_printf(sc
->sc_dev
,
263 "timeout waiting for NIC to initialize\n");
267 sc
->mac_ver
= tmp
>> 16;
268 sc
->mac_rev
= tmp
& 0xffff;
270 if (sc
->mac_ver
!= 0x2860 &&
271 (id
== 0x0681 || id
== 0x0781 || id
== 0x1059))
272 sc
->sc_flags
|= RT2860_ADVANCED_PS
;
274 /* retrieve RF rev. no and various other things from EEPROM */
275 rt2860_read_eeprom(sc
, ic
->ic_macaddr
);
276 #if defined(__DragonFly__)
277 device_printf(sc
->sc_dev
, "MAC/BBP RT%X (rev 0x%04X), "
278 "RF %s (MIMO %dT%dR), address %s\n",
279 sc
->mac_ver
, sc
->mac_rev
, rt2860_get_rf(sc
->rf_rev
),
280 sc
->ntxchains
, sc
->nrxchains
, ether_sprintf(ic
->ic_macaddr
));
282 device_printf(sc
->sc_dev
, "MAC/BBP RT%X (rev 0x%04X), "
283 "RF %s (MIMO %dT%dR), address %6D\n",
284 sc
->mac_ver
, sc
->mac_rev
, rt2860_get_rf(sc
->rf_rev
),
285 sc
->ntxchains
, sc
->nrxchains
, ic
->ic_macaddr
, ":");
289 * Allocate Tx (4 EDCAs + HCCA + Mgt) and Rx rings.
291 for (qid
= 0; qid
< 6; qid
++) {
292 if ((error
= rt2860_alloc_tx_ring(sc
, &sc
->txq
[qid
])) != 0) {
293 device_printf(sc
->sc_dev
,
294 "could not allocate Tx ring %d\n", qid
);
299 if ((error
= rt2860_alloc_rx_ring(sc
, &sc
->rxq
)) != 0) {
300 device_printf(sc
->sc_dev
, "could not allocate Rx ring\n");
304 if ((error
= rt2860_alloc_tx_pool(sc
)) != 0) {
305 device_printf(sc
->sc_dev
, "could not allocate Tx pool\n");
309 /* mgmt ring is broken on RT2860C, use EDCA AC VO ring instead */
310 sc
->mgtqid
= (sc
->mac_ver
== 0x2860 && sc
->mac_rev
== 0x0100) ?
314 ic
->ic_name
= device_get_nameunit(dev
);
315 ic
->ic_opmode
= IEEE80211_M_STA
;
316 ic
->ic_phytype
= IEEE80211_T_OFDM
; /* not only, but not used */
318 /* set device capabilities */
320 IEEE80211_C_STA
/* station mode */
321 | IEEE80211_C_IBSS
/* ibss, nee adhoc, mode */
322 | IEEE80211_C_HOSTAP
/* hostap mode */
323 | IEEE80211_C_MONITOR
/* monitor mode */
324 | IEEE80211_C_AHDEMO
/* adhoc demo mode */
325 | IEEE80211_C_WDS
/* 4-address traffic works */
326 | IEEE80211_C_MBSS
/* mesh point link mode */
327 | IEEE80211_C_SHPREAMBLE
/* short preamble supported */
328 | IEEE80211_C_SHSLOT
/* short slot time supported */
329 | IEEE80211_C_WPA
/* capable of WPA1+WPA2 */
331 | IEEE80211_C_BGSCAN
/* capable of bg scanning */
333 | IEEE80211_C_WME
/* 802.11e */
336 memset(bands
, 0, sizeof(bands
));
337 setbit(bands
, IEEE80211_MODE_11B
);
338 setbit(bands
, IEEE80211_MODE_11G
);
339 if (sc
->rf_rev
== RT2860_RF_2750
|| sc
->rf_rev
== RT2860_RF_2850
)
340 setbit(bands
, IEEE80211_MODE_11A
);
341 ieee80211_init_channels(ic
, NULL
, bands
);
343 ieee80211_ifattach(ic
);
345 ic
->ic_wme
.wme_update
= rt2860_updateedca
;
346 ic
->ic_scan_start
= rt2860_scan_start
;
347 ic
->ic_scan_end
= rt2860_scan_end
;
348 ic
->ic_set_channel
= rt2860_set_channel
;
349 ic
->ic_updateslot
= rt2860_updateslot
;
350 ic
->ic_update_promisc
= rt2860_update_promisc
;
351 ic
->ic_raw_xmit
= rt2860_raw_xmit
;
352 sc
->sc_node_free
= ic
->ic_node_free
;
353 ic
->ic_node_free
= rt2860_node_free
;
354 ic
->ic_newassoc
= rt2860_newassoc
;
355 ic
->ic_transmit
= rt2860_transmit
;
356 ic
->ic_parent
= rt2860_parent
;
357 ic
->ic_vap_create
= rt2860_vap_create
;
358 ic
->ic_vap_delete
= rt2860_vap_delete
;
360 ieee80211_radiotap_attach(ic
,
361 &sc
->sc_txtap
.wt_ihdr
, sizeof(sc
->sc_txtap
),
362 RT2860_TX_RADIOTAP_PRESENT
,
363 &sc
->sc_rxtap
.wr_ihdr
, sizeof(sc
->sc_rxtap
),
364 RT2860_RX_RADIOTAP_PRESENT
);
367 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev
),
368 SYSCTL_CHILDREN(device_get_sysctl_tree(dev
)), OID_AUTO
,
369 "debug", CTLFLAG_RW
, &sc
->sc_debug
, 0, "debug msgs");
372 ieee80211_announce(ic
);
376 fail3
: rt2860_free_rx_ring(sc
, &sc
->rxq
);
377 fail2
: while (--qid
>= 0)
378 rt2860_free_tx_ring(sc
, &sc
->txq
[qid
]);
379 #if defined(__DragonFly__)
380 fail1
: lockuninit(&sc
->sc_mtx
);
382 fail1
: mtx_destroy(&sc
->sc_mtx
);
388 rt2860_detach(void *xsc
)
390 struct rt2860_softc
*sc
= xsc
;
391 struct ieee80211com
*ic
= &sc
->sc_ic
;
395 rt2860_stop_locked(sc
);
398 ieee80211_ifdetach(ic
);
399 mbufq_drain(&sc
->sc_snd
);
400 for (qid
= 0; qid
< 6; qid
++)
401 rt2860_free_tx_ring(sc
, &sc
->txq
[qid
]);
402 rt2860_free_rx_ring(sc
, &sc
->rxq
);
403 rt2860_free_tx_pool(sc
);
405 #if defined(__DragonFly__)
406 lockuninit(&sc
->sc_mtx
);
408 mtx_destroy(&sc
->sc_mtx
);
415 rt2860_shutdown(void *xsc
)
417 struct rt2860_softc
*sc
= xsc
;
423 rt2860_suspend(void *xsc
)
425 struct rt2860_softc
*sc
= xsc
;
431 rt2860_resume(void *xsc
)
433 struct rt2860_softc
*sc
= xsc
;
435 if (sc
->sc_ic
.ic_nrunning
> 0)
439 static struct ieee80211vap
*
440 rt2860_vap_create(struct ieee80211com
*ic
, const char name
[IFNAMSIZ
], int unit
,
441 enum ieee80211_opmode opmode
, int flags
,
442 const uint8_t bssid
[IEEE80211_ADDR_LEN
],
443 const uint8_t mac
[IEEE80211_ADDR_LEN
])
445 struct rt2860_softc
*sc
= ic
->ic_softc
;
446 struct rt2860_vap
*rvp
;
447 struct ieee80211vap
*vap
;
450 case IEEE80211_M_STA
:
451 case IEEE80211_M_IBSS
:
452 case IEEE80211_M_AHDEMO
:
453 case IEEE80211_M_MONITOR
:
454 case IEEE80211_M_HOSTAP
:
455 case IEEE80211_M_MBSS
:
457 if (!TAILQ_EMPTY(&ic
->ic_vaps
)) {
458 device_printf(sc
->sc_dev
, "only 1 vap supported\n");
461 if (opmode
== IEEE80211_M_STA
)
462 flags
|= IEEE80211_CLONE_NOBEACONS
;
464 case IEEE80211_M_WDS
:
465 if (TAILQ_EMPTY(&ic
->ic_vaps
) ||
466 ic
->ic_opmode
!= IEEE80211_M_HOSTAP
) {
467 device_printf(sc
->sc_dev
,
468 "wds only supported in ap mode\n");
472 * Silently remove any request for a unique
473 * bssid; WDS vap's always share the local
476 flags
&= ~IEEE80211_CLONE_BSSID
;
479 device_printf(sc
->sc_dev
, "unknown opmode %d\n", opmode
);
482 rvp
= kmalloc(sizeof(struct rt2860_vap
), M_80211_VAP
, M_WAITOK
| M_ZERO
);
484 ieee80211_vap_setup(ic
, vap
, name
, unit
, opmode
, flags
, bssid
);
486 /* override state transition machine */
487 rvp
->ral_newstate
= vap
->iv_newstate
;
488 vap
->iv_newstate
= rt2860_newstate
;
490 vap
->iv_update_beacon
= rt2860_beacon_update
;
493 /* HW supports up to 255 STAs (0-254) in HostAP and IBSS modes */
494 vap
->iv_max_aid
= min(IEEE80211_AID_MAX
, RT2860_WCID_MAX
);
496 ieee80211_ratectl_init(vap
);
498 ieee80211_vap_attach(vap
, ieee80211_media_change
,
499 ieee80211_media_status
, mac
);
500 if (TAILQ_FIRST(&ic
->ic_vaps
) == vap
)
501 ic
->ic_opmode
= opmode
;
506 rt2860_vap_delete(struct ieee80211vap
*vap
)
508 struct rt2860_vap
*rvp
= RT2860_VAP(vap
);
510 ieee80211_ratectl_deinit(vap
);
511 ieee80211_vap_detach(vap
);
512 kfree(rvp
, M_80211_VAP
);
516 rt2860_dma_map_addr(void *arg
, bus_dma_segment_t
*segs
, int nseg
, int error
)
521 KASSERT(nseg
== 1, ("too many DMA segments, %d should be 1", nseg
));
523 *(bus_addr_t
*)arg
= segs
[0].ds_addr
;
528 rt2860_alloc_tx_ring(struct rt2860_softc
*sc
, struct rt2860_tx_ring
*ring
)
532 size
= RT2860_TX_RING_COUNT
* sizeof (struct rt2860_txd
);
534 #if defined(__DragonFly__)
535 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 16, 0,
536 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
537 size
, 1, size
, 0, &ring
->desc_dmat
);
539 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 16, 0,
540 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
541 size
, 1, size
, 0, NULL
, NULL
, &ring
->desc_dmat
);
544 device_printf(sc
->sc_dev
, "could not create desc DMA tag\n");
548 error
= bus_dmamem_alloc(ring
->desc_dmat
, (void **)&ring
->txd
,
549 BUS_DMA_NOWAIT
| BUS_DMA_ZERO
, &ring
->desc_map
);
551 device_printf(sc
->sc_dev
, "could not allocate DMA memory\n");
555 error
= bus_dmamap_load(ring
->desc_dmat
, ring
->desc_map
, ring
->txd
,
556 size
, rt2860_dma_map_addr
, &ring
->paddr
, 0);
558 device_printf(sc
->sc_dev
, "could not load desc DMA map\n");
562 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
, BUS_DMASYNC_PREWRITE
);
566 fail
: rt2860_free_tx_ring(sc
, ring
);
571 rt2860_reset_tx_ring(struct rt2860_softc
*sc
, struct rt2860_tx_ring
*ring
)
573 struct rt2860_tx_data
*data
;
576 for (i
= 0; i
< RT2860_TX_RING_COUNT
; i
++) {
577 if ((data
= ring
->data
[i
]) == NULL
)
578 continue; /* nothing mapped in this slot */
580 if (data
->m
!= NULL
) {
581 bus_dmamap_sync(sc
->txwi_dmat
, data
->map
,
582 BUS_DMASYNC_POSTWRITE
);
583 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
587 if (data
->ni
!= NULL
) {
588 ieee80211_free_node(data
->ni
);
592 SLIST_INSERT_HEAD(&sc
->data_pool
, data
, next
);
593 ring
->data
[i
] = NULL
;
597 ring
->cur
= ring
->next
= 0;
601 rt2860_free_tx_ring(struct rt2860_softc
*sc
, struct rt2860_tx_ring
*ring
)
603 struct rt2860_tx_data
*data
;
606 if (ring
->txd
!= NULL
) {
607 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
,
608 BUS_DMASYNC_POSTWRITE
);
609 bus_dmamap_unload(ring
->desc_dmat
, ring
->desc_map
);
610 bus_dmamem_free(ring
->desc_dmat
, ring
->txd
, ring
->desc_map
);
612 if (ring
->desc_dmat
!= NULL
)
613 bus_dma_tag_destroy(ring
->desc_dmat
);
615 for (i
= 0; i
< RT2860_TX_RING_COUNT
; i
++) {
616 if ((data
= ring
->data
[i
]) == NULL
)
617 continue; /* nothing mapped in this slot */
619 if (data
->m
!= NULL
) {
620 bus_dmamap_sync(sc
->txwi_dmat
, data
->map
,
621 BUS_DMASYNC_POSTWRITE
);
622 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
625 if (data
->ni
!= NULL
)
626 ieee80211_free_node(data
->ni
);
628 SLIST_INSERT_HEAD(&sc
->data_pool
, data
, next
);
633 * Allocate a pool of TX Wireless Information blocks.
636 rt2860_alloc_tx_pool(struct rt2860_softc
*sc
)
642 size
= RT2860_TX_POOL_COUNT
* RT2860_TXWI_DMASZ
;
644 /* init data_pool early in case of failure.. */
645 SLIST_INIT(&sc
->data_pool
);
647 #if defined(__DragonFly__)
648 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 4, 0,
649 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
650 size
, 1, size
, 0, &sc
->txwi_dmat
);
652 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 1, 0,
653 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
654 size
, 1, size
, 0, NULL
, NULL
, &sc
->txwi_dmat
);
657 device_printf(sc
->sc_dev
, "could not create txwi DMA tag\n");
661 error
= bus_dmamem_alloc(sc
->txwi_dmat
, (void **)&sc
->txwi_vaddr
,
662 BUS_DMA_NOWAIT
| BUS_DMA_ZERO
, &sc
->txwi_map
);
664 device_printf(sc
->sc_dev
, "could not allocate DMA memory\n");
668 error
= bus_dmamap_load(sc
->txwi_dmat
, sc
->txwi_map
,
669 sc
->txwi_vaddr
, size
, rt2860_dma_map_addr
, &paddr
, 0);
671 device_printf(sc
->sc_dev
, "could not load txwi DMA map\n");
675 bus_dmamap_sync(sc
->txwi_dmat
, sc
->txwi_map
, BUS_DMASYNC_PREWRITE
);
677 vaddr
= sc
->txwi_vaddr
;
678 for (i
= 0; i
< RT2860_TX_POOL_COUNT
; i
++) {
679 struct rt2860_tx_data
*data
= &sc
->data
[i
];
681 error
= bus_dmamap_create(sc
->txwi_dmat
, 0, &data
->map
);
683 device_printf(sc
->sc_dev
, "could not create DMA map\n");
686 data
->txwi
= (struct rt2860_txwi
*)vaddr
;
688 vaddr
+= RT2860_TXWI_DMASZ
;
689 paddr
+= RT2860_TXWI_DMASZ
;
691 SLIST_INSERT_HEAD(&sc
->data_pool
, data
, next
);
696 fail
: rt2860_free_tx_pool(sc
);
701 rt2860_free_tx_pool(struct rt2860_softc
*sc
)
703 if (sc
->txwi_vaddr
!= NULL
) {
704 bus_dmamap_sync(sc
->txwi_dmat
, sc
->txwi_map
,
705 BUS_DMASYNC_POSTWRITE
);
706 bus_dmamap_unload(sc
->txwi_dmat
, sc
->txwi_map
);
707 bus_dmamem_free(sc
->txwi_dmat
, sc
->txwi_vaddr
, sc
->txwi_map
);
709 if (sc
->txwi_dmat
!= NULL
)
710 bus_dma_tag_destroy(sc
->txwi_dmat
);
712 while (!SLIST_EMPTY(&sc
->data_pool
)) {
713 struct rt2860_tx_data
*data
;
714 data
= SLIST_FIRST(&sc
->data_pool
);
715 bus_dmamap_destroy(sc
->txwi_dmat
, data
->map
);
716 SLIST_REMOVE_HEAD(&sc
->data_pool
, next
);
721 rt2860_alloc_rx_ring(struct rt2860_softc
*sc
, struct rt2860_rx_ring
*ring
)
726 size
= RT2860_RX_RING_COUNT
* sizeof (struct rt2860_rxd
);
728 #if defined(__DragonFly__)
729 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 16, 0,
730 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
731 size
, 1, size
, 0, &ring
->desc_dmat
);
733 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 16, 0,
734 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
735 size
, 1, size
, 0, NULL
, NULL
, &ring
->desc_dmat
);
738 device_printf(sc
->sc_dev
, "could not create desc DMA tag\n");
742 error
= bus_dmamem_alloc(ring
->desc_dmat
, (void **)&ring
->rxd
,
743 BUS_DMA_NOWAIT
| BUS_DMA_ZERO
, &ring
->desc_map
);
745 device_printf(sc
->sc_dev
, "could not allocate DMA memory\n");
749 error
= bus_dmamap_load(ring
->desc_dmat
, ring
->desc_map
, ring
->rxd
,
750 size
, rt2860_dma_map_addr
, &ring
->paddr
, 0);
752 device_printf(sc
->sc_dev
, "could not load desc DMA map\n");
756 #if defined(__DragonFly__)
757 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 4, 0,
758 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
, MCLBYTES
,
759 1, MCLBYTES
, 0, &ring
->data_dmat
);
761 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 1, 0,
762 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
, MCLBYTES
,
763 1, MCLBYTES
, 0, NULL
, NULL
, &ring
->data_dmat
);
766 device_printf(sc
->sc_dev
, "could not create data DMA tag\n");
770 for (i
= 0; i
< RT2860_RX_RING_COUNT
; i
++) {
771 struct rt2860_rx_data
*data
= &ring
->data
[i
];
772 struct rt2860_rxd
*rxd
= &ring
->rxd
[i
];
774 error
= bus_dmamap_create(ring
->data_dmat
, 0, &data
->map
);
776 device_printf(sc
->sc_dev
, "could not create DMA map\n");
780 data
->m
= m_getcl(M_NOWAIT
, MT_DATA
, M_PKTHDR
);
781 if (data
->m
== NULL
) {
782 device_printf(sc
->sc_dev
,
783 "could not allocate rx mbuf\n");
788 error
= bus_dmamap_load(ring
->data_dmat
, data
->map
,
789 mtod(data
->m
, void *), MCLBYTES
, rt2860_dma_map_addr
,
792 device_printf(sc
->sc_dev
,
793 "could not load rx buf DMA map");
797 rxd
->sdp0
= htole32(physaddr
);
798 rxd
->sdl0
= htole16(MCLBYTES
);
801 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
, BUS_DMASYNC_PREWRITE
);
805 fail
: rt2860_free_rx_ring(sc
, ring
);
810 rt2860_reset_rx_ring(struct rt2860_softc
*sc
, struct rt2860_rx_ring
*ring
)
814 for (i
= 0; i
< RT2860_RX_RING_COUNT
; i
++)
815 ring
->rxd
[i
].sdl0
&= ~htole16(RT2860_RX_DDONE
);
817 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
, BUS_DMASYNC_PREWRITE
);
823 rt2860_free_rx_ring(struct rt2860_softc
*sc
, struct rt2860_rx_ring
*ring
)
827 if (ring
->rxd
!= NULL
) {
828 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
,
829 BUS_DMASYNC_POSTWRITE
);
830 bus_dmamap_unload(ring
->desc_dmat
, ring
->desc_map
);
831 bus_dmamem_free(ring
->desc_dmat
, ring
->rxd
, ring
->desc_map
);
833 if (ring
->desc_dmat
!= NULL
)
834 bus_dma_tag_destroy(ring
->desc_dmat
);
836 for (i
= 0; i
< RT2860_RX_RING_COUNT
; i
++) {
837 struct rt2860_rx_data
*data
= &ring
->data
[i
];
839 if (data
->m
!= NULL
) {
840 bus_dmamap_sync(ring
->data_dmat
, data
->map
,
841 BUS_DMASYNC_POSTREAD
);
842 bus_dmamap_unload(ring
->data_dmat
, data
->map
);
845 if (data
->map
!= NULL
)
846 bus_dmamap_destroy(ring
->data_dmat
, data
->map
);
848 if (ring
->data_dmat
!= NULL
)
849 bus_dma_tag_destroy(ring
->data_dmat
);
853 rt2860_updatestats(struct rt2860_softc
*sc
)
855 struct ieee80211com
*ic
= &sc
->sc_ic
;
858 * In IBSS or HostAP modes (when the hardware sends beacons), the
859 * MAC can run into a livelock and start sending CTS-to-self frames
860 * like crazy if protection is enabled. Fortunately, we can detect
861 * when such a situation occurs and reset the MAC.
863 if (ic
->ic_curmode
!= IEEE80211_M_STA
) {
864 /* check if we're in a livelock situation.. */
865 uint32_t tmp
= RAL_READ(sc
, RT2860_DEBUG
);
866 if ((tmp
& (1 << 29)) && (tmp
& (1 << 7 | 1 << 5))) {
867 /* ..and reset MAC/BBP for a while.. */
868 DPRINTF(("CTS-to-self livelock detected\n"));
869 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, RT2860_MAC_SRST
);
870 RAL_BARRIER_WRITE(sc
);
872 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
,
873 RT2860_MAC_RX_EN
| RT2860_MAC_TX_EN
);
879 rt2860_newassoc(struct ieee80211_node
*ni
, int isnew
)
881 struct ieee80211com
*ic
= ni
->ni_ic
;
882 struct rt2860_softc
*sc
= ic
->ic_softc
;
885 wcid
= IEEE80211_AID(ni
->ni_associd
);
886 if (isnew
&& ni
->ni_associd
!= 0) {
887 sc
->wcid2ni
[wcid
] = ni
;
889 /* init WCID table entry */
890 RAL_WRITE_REGION_1(sc
, RT2860_WCID_ENTRY(wcid
),
891 ni
->ni_macaddr
, IEEE80211_ADDR_LEN
);
893 DPRINTF(("new assoc isnew=%d addr=%s WCID=%d\n",
894 isnew
, ether_sprintf(ni
->ni_macaddr
), wcid
));
898 rt2860_node_free(struct ieee80211_node
*ni
)
900 struct ieee80211com
*ic
= ni
->ni_ic
;
901 struct rt2860_softc
*sc
= ic
->ic_softc
;
904 if (ni
->ni_associd
!= 0) {
905 wcid
= IEEE80211_AID(ni
->ni_associd
);
907 /* clear Rx WCID search table entry */
908 RAL_SET_REGION_4(sc
, RT2860_WCID_ENTRY(wcid
), 0, 2);
910 sc
->sc_node_free(ni
);
915 rt2860_ampdu_rx_start(struct ieee80211com
*ic
, struct ieee80211_node
*ni
,
918 struct rt2860_softc
*sc
= ic
->ic_softc
;
919 uint8_t wcid
= ((struct rt2860_node
*)ni
)->wcid
;
922 /* update BA session mask */
923 tmp
= RAL_READ(sc
, RT2860_WCID_ENTRY(wcid
) + 4);
924 tmp
|= (1 << tid
) << 16;
925 RAL_WRITE(sc
, RT2860_WCID_ENTRY(wcid
) + 4, tmp
);
930 rt2860_ampdu_rx_stop(struct ieee80211com
*ic
, struct ieee80211_node
*ni
,
933 struct rt2860_softc
*sc
= ic
->ic_softc
;
934 uint8_t wcid
= ((struct rt2860_node
*)ni
)->wcid
;
937 /* update BA session mask */
938 tmp
= RAL_READ(sc
, RT2860_WCID_ENTRY(wcid
) + 4);
939 tmp
&= ~((1 << tid
) << 16);
940 RAL_WRITE(sc
, RT2860_WCID_ENTRY(wcid
) + 4, tmp
);
945 rt2860_newstate(struct ieee80211vap
*vap
, enum ieee80211_state nstate
, int arg
)
947 struct rt2860_vap
*rvp
= RT2860_VAP(vap
);
948 struct ieee80211com
*ic
= vap
->iv_ic
;
949 struct rt2860_softc
*sc
= ic
->ic_softc
;
953 if (vap
->iv_state
== IEEE80211_S_RUN
) {
954 /* turn link LED off */
955 rt2860_set_leds(sc
, RT2860_LED_RADIO
);
958 if (nstate
== IEEE80211_S_INIT
&& vap
->iv_state
== IEEE80211_S_RUN
) {
959 /* abort TSF synchronization */
960 tmp
= RAL_READ(sc
, RT2860_BCN_TIME_CFG
);
961 RAL_WRITE(sc
, RT2860_BCN_TIME_CFG
,
962 tmp
& ~(RT2860_BCN_TX_EN
| RT2860_TSF_TIMER_EN
|
963 RT2860_TBTT_TIMER_EN
));
966 rt2860_set_gp_timer(sc
, 0);
968 error
= rvp
->ral_newstate(vap
, nstate
, arg
);
972 if (nstate
== IEEE80211_S_RUN
) {
973 struct ieee80211_node
*ni
= vap
->iv_bss
;
975 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
976 rt2860_enable_mrr(sc
);
977 rt2860_set_txpreamble(sc
);
978 rt2860_set_basicrates(sc
, &ni
->ni_rates
);
979 rt2860_set_bssid(sc
, ni
->ni_bssid
);
982 if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
||
983 vap
->iv_opmode
== IEEE80211_M_IBSS
||
984 vap
->iv_opmode
== IEEE80211_M_MBSS
) {
985 error
= rt2860_setup_beacon(sc
, vap
);
990 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
991 rt2860_enable_tsf_sync(sc
);
992 rt2860_set_gp_timer(sc
, 500);
995 /* turn link LED on */
996 rt2860_set_leds(sc
, RT2860_LED_RADIO
|
997 (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
) ?
998 RT2860_LED_LINK_2GHZ
: RT2860_LED_LINK_5GHZ
));
1003 /* Read 16-bit from eFUSE ROM (>=RT3071 only.) */
1005 rt3090_efuse_read_2(struct rt2860_softc
*sc
, uint16_t addr
)
1013 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1019 tmp
= RAL_READ(sc
, RT3070_EFUSE_CTRL
);
1020 tmp
&= ~(RT3070_EFSROM_MODE_MASK
| RT3070_EFSROM_AIN_MASK
);
1021 tmp
|= (addr
& ~0xf) << RT3070_EFSROM_AIN_SHIFT
| RT3070_EFSROM_KICK
;
1022 RAL_WRITE(sc
, RT3070_EFUSE_CTRL
, tmp
);
1023 for (ntries
= 0; ntries
< 500; ntries
++) {
1024 tmp
= RAL_READ(sc
, RT3070_EFUSE_CTRL
);
1025 if (!(tmp
& RT3070_EFSROM_KICK
))
1032 if ((tmp
& RT3070_EFUSE_AOUT_MASK
) == RT3070_EFUSE_AOUT_MASK
)
1033 return 0xffff; /* address not found */
1035 /* determine to which 32-bit register our 16-bit word belongs */
1036 reg
= RT3070_EFUSE_DATA3
- (addr
& 0xc);
1037 tmp
= RAL_READ(sc
, reg
);
1039 return (addr
& 2) ? tmp
>> 16 : tmp
& 0xffff;
1043 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46,
1047 rt2860_eeprom_read_2(struct rt2860_softc
*sc
, uint16_t addr
)
1053 /* clock C once before the first command */
1054 RT2860_EEPROM_CTL(sc
, 0);
1056 RT2860_EEPROM_CTL(sc
, RT2860_S
);
1057 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_C
);
1058 RT2860_EEPROM_CTL(sc
, RT2860_S
);
1060 /* write start bit (1) */
1061 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_D
);
1062 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_D
| RT2860_C
);
1064 /* write READ opcode (10) */
1065 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_D
);
1066 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_D
| RT2860_C
);
1067 RT2860_EEPROM_CTL(sc
, RT2860_S
);
1068 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_C
);
1070 /* write address (A5-A0 or A7-A0) */
1071 n
= ((RAL_READ(sc
, RT2860_PCI_EECTRL
) & 0x30) == 0) ? 5 : 7;
1072 for (; n
>= 0; n
--) {
1073 RT2860_EEPROM_CTL(sc
, RT2860_S
|
1074 (((addr
>> n
) & 1) << RT2860_SHIFT_D
));
1075 RT2860_EEPROM_CTL(sc
, RT2860_S
|
1076 (((addr
>> n
) & 1) << RT2860_SHIFT_D
) | RT2860_C
);
1079 RT2860_EEPROM_CTL(sc
, RT2860_S
);
1081 /* read data Q15-Q0 */
1083 for (n
= 15; n
>= 0; n
--) {
1084 RT2860_EEPROM_CTL(sc
, RT2860_S
| RT2860_C
);
1085 tmp
= RAL_READ(sc
, RT2860_PCI_EECTRL
);
1086 val
|= ((tmp
& RT2860_Q
) >> RT2860_SHIFT_Q
) << n
;
1087 RT2860_EEPROM_CTL(sc
, RT2860_S
);
1090 RT2860_EEPROM_CTL(sc
, 0);
1092 /* clear Chip Select and clock C */
1093 RT2860_EEPROM_CTL(sc
, RT2860_S
);
1094 RT2860_EEPROM_CTL(sc
, 0);
1095 RT2860_EEPROM_CTL(sc
, RT2860_C
);
1100 static __inline
uint16_t
1101 rt2860_srom_read(struct rt2860_softc
*sc
, uint8_t addr
)
1103 /* either eFUSE ROM or EEPROM */
1104 return sc
->sc_srom_read(sc
, addr
);
1108 rt2860_intr_coherent(struct rt2860_softc
*sc
)
1112 /* DMA finds data coherent event when checking the DDONE bit */
1114 DPRINTF(("Tx/Rx Coherent interrupt\n"));
1116 /* restart DMA engine */
1117 tmp
= RAL_READ(sc
, RT2860_WPDMA_GLO_CFG
);
1118 tmp
&= ~(RT2860_TX_WB_DDONE
| RT2860_RX_DMA_EN
| RT2860_TX_DMA_EN
);
1119 RAL_WRITE(sc
, RT2860_WPDMA_GLO_CFG
, tmp
);
1121 (void)rt2860_txrx_enable(sc
);
1125 rt2860_drain_stats_fifo(struct rt2860_softc
*sc
)
1127 struct ieee80211_node
*ni
;
1130 uint8_t wcid
, mcs
, pid
;
1132 /* drain Tx status FIFO (maxsize = 16) */
1133 while ((stat
= RAL_READ(sc
, RT2860_TX_STAT_FIFO
)) & RT2860_TXQ_VLD
) {
1134 DPRINTFN(4, ("tx stat 0x%08x\n", stat
));
1136 wcid
= (stat
>> RT2860_TXQ_WCID_SHIFT
) & 0xff;
1137 ni
= sc
->wcid2ni
[wcid
];
1139 /* if no ACK was requested, no feedback is available */
1140 if (!(stat
& RT2860_TXQ_ACKREQ
) || wcid
== 0xff || ni
== NULL
)
1143 /* update per-STA AMRR stats */
1144 if (stat
& RT2860_TXQ_OK
) {
1146 * Check if there were retries, ie if the Tx success
1147 * rate is different from the requested rate. Note
1148 * that it works only because we do not allow rate
1149 * fallback from OFDM to CCK.
1151 mcs
= (stat
>> RT2860_TXQ_MCS_SHIFT
) & 0x7f;
1152 pid
= (stat
>> RT2860_TXQ_PID_SHIFT
) & 0xf;
1157 ieee80211_ratectl_tx_complete(ni
->ni_vap
, ni
,
1158 IEEE80211_RATECTL_TX_SUCCESS
, &retrycnt
, NULL
);
1160 ieee80211_ratectl_tx_complete(ni
->ni_vap
, ni
,
1161 IEEE80211_RATECTL_TX_FAILURE
, &retrycnt
, NULL
);
1162 if_inc_counter(ni
->ni_vap
->iv_ifp
,
1163 IFCOUNTER_OERRORS
, 1);
1169 rt2860_tx_intr(struct rt2860_softc
*sc
, int qid
)
1171 struct rt2860_tx_ring
*ring
= &sc
->txq
[qid
];
1174 rt2860_drain_stats_fifo(sc
);
1176 hw
= RAL_READ(sc
, RT2860_TX_DTX_IDX(qid
));
1177 while (ring
->next
!= hw
) {
1178 struct rt2860_tx_data
*data
= ring
->data
[ring
->next
];
1181 bus_dmamap_sync(sc
->txwi_dmat
, data
->map
,
1182 BUS_DMASYNC_POSTWRITE
);
1183 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
1184 if (data
->m
->m_flags
& M_TXCB
) {
1185 ieee80211_process_callback(data
->ni
, data
->m
,
1188 ieee80211_tx_complete(data
->ni
, data
->m
, 0);
1191 SLIST_INSERT_HEAD(&sc
->data_pool
, data
, next
);
1192 ring
->data
[ring
->next
] = NULL
;
1195 ring
->next
= (ring
->next
+ 1) % RT2860_TX_RING_COUNT
;
1198 sc
->sc_tx_timer
= 0;
1199 if (ring
->queued
< RT2860_TX_RING_COUNT
)
1200 sc
->qfullmsk
&= ~(1 << qid
);
1205 * Return the Rx chain with the highest RSSI for a given frame.
1207 static __inline
uint8_t
1208 rt2860_maxrssi_chain(struct rt2860_softc
*sc
, const struct rt2860_rxwi
*rxwi
)
1210 uint8_t rxchain
= 0;
1212 if (sc
->nrxchains
> 1) {
1213 if (rxwi
->rssi
[1] > rxwi
->rssi
[rxchain
])
1215 if (sc
->nrxchains
> 2)
1216 if (rxwi
->rssi
[2] > rxwi
->rssi
[rxchain
])
1223 rt2860_rx_intr(struct rt2860_softc
*sc
)
1225 struct rt2860_rx_radiotap_header
*tap
;
1226 struct ieee80211com
*ic
= &sc
->sc_ic
;
1227 struct ieee80211_frame
*wh
;
1228 struct ieee80211_node
*ni
;
1229 struct mbuf
*m
, *m1
;
1230 bus_addr_t physaddr
;
1237 hw
= RAL_READ(sc
, RT2860_FS_DRX_IDX
) & 0xfff;
1238 while (sc
->rxq
.cur
!= hw
) {
1239 struct rt2860_rx_data
*data
= &sc
->rxq
.data
[sc
->rxq
.cur
];
1240 struct rt2860_rxd
*rxd
= &sc
->rxq
.rxd
[sc
->rxq
.cur
];
1241 struct rt2860_rxwi
*rxwi
;
1243 bus_dmamap_sync(sc
->rxq
.desc_dmat
, sc
->rxq
.desc_map
,
1244 BUS_DMASYNC_POSTREAD
);
1246 if (__predict_false(!(rxd
->sdl0
& htole16(RT2860_RX_DDONE
)))) {
1247 DPRINTF(("RXD DDONE bit not set!\n"));
1248 break; /* should not happen */
1251 if (__predict_false(rxd
->flags
&
1252 htole32(RT2860_RX_CRCERR
| RT2860_RX_ICVERR
))) {
1253 #if defined(__DragonFly__)
1254 /* not implemented */
1256 counter_u64_add(ic
->ic_ierrors
, 1);
1262 if (__predict_false(rxd
->flags
& htole32(RT2860_RX_MICERR
))) {
1263 /* report MIC failures to net80211 for TKIP */
1264 ic
->ic_stats
.is_rx_locmicfail
++;
1265 ieee80211_michael_mic_failure(ic
, 0/* XXX */);
1266 #if defined(__DragonFly__)
1267 /* not implemented */
1269 counter_u64_add(ic
->ic_ierrors
, 1);
1275 m1
= m_getcl(M_NOWAIT
, MT_DATA
, M_PKTHDR
);
1276 if (__predict_false(m1
== NULL
)) {
1277 #if defined(__DragonFly__)
1278 /* not implemented */
1280 counter_u64_add(ic
->ic_ierrors
, 1);
1285 bus_dmamap_sync(sc
->rxq
.data_dmat
, data
->map
,
1286 BUS_DMASYNC_POSTREAD
);
1287 bus_dmamap_unload(sc
->rxq
.data_dmat
, data
->map
);
1289 error
= bus_dmamap_load(sc
->rxq
.data_dmat
, data
->map
,
1290 mtod(m1
, void *), MCLBYTES
, rt2860_dma_map_addr
,
1292 if (__predict_false(error
!= 0)) {
1295 /* try to reload the old mbuf */
1296 error
= bus_dmamap_load(sc
->rxq
.data_dmat
, data
->map
,
1297 mtod(data
->m
, void *), MCLBYTES
,
1298 rt2860_dma_map_addr
, &physaddr
, 0);
1299 if (__predict_false(error
!= 0)) {
1300 panic("%s: could not load old rx mbuf",
1301 device_get_name(sc
->sc_dev
));
1303 /* physical address may have changed */
1304 rxd
->sdp0
= htole32(physaddr
);
1305 #if defined(__DragonFly__)
1306 /* not implemented */
1308 counter_u64_add(ic
->ic_ierrors
, 1);
1314 * New mbuf successfully loaded, update Rx ring and continue
1319 rxd
->sdp0
= htole32(physaddr
);
1321 rxwi
= mtod(m
, struct rt2860_rxwi
*);
1324 m
->m_data
= (caddr_t
)(rxwi
+ 1);
1325 m
->m_pkthdr
.len
= m
->m_len
= le16toh(rxwi
->len
) & 0xfff;
1327 wh
= mtod(m
, struct ieee80211_frame
*);
1329 if (wh
->i_fc
[1] & IEEE80211_FC1_PROTECTED
) {
1330 /* frame is decrypted by hardware */
1331 wh
->i_fc
[1] &= ~IEEE80211_FC1_PROTECTED
;
1335 /* HW may insert 2 padding bytes after 802.11 header */
1336 if (rxd
->flags
& htole32(RT2860_RX_L2PAD
)) {
1337 u_int hdrlen
= ieee80211_hdrsize(wh
);
1338 bcopy(wh
, (caddr_t
)wh
+ 2, hdrlen
);
1340 wh
= mtod(m
, struct ieee80211_frame
*);
1343 ant
= rt2860_maxrssi_chain(sc
, rxwi
);
1344 rssi
= rt2860_rssi2dbm(sc
, rxwi
->rssi
[ant
], ant
);
1345 nf
= RT2860_NOISE_FLOOR
;
1347 if (ieee80211_radiotap_active(ic
)) {
1348 tap
= &sc
->sc_rxtap
;
1350 tap
->wr_antenna
= ant
;
1351 tap
->wr_antsignal
= nf
+ rssi
;
1352 tap
->wr_antnoise
= nf
;
1353 /* in case it can't be found below */
1355 phy
= le16toh(rxwi
->phy
);
1356 switch (phy
& RT2860_PHY_MODE
) {
1357 case RT2860_PHY_CCK
:
1358 switch ((phy
& RT2860_PHY_MCS
) & ~RT2860_PHY_SHPRE
) {
1359 case 0: tap
->wr_rate
= 2; break;
1360 case 1: tap
->wr_rate
= 4; break;
1361 case 2: tap
->wr_rate
= 11; break;
1362 case 3: tap
->wr_rate
= 22; break;
1364 if (phy
& RT2860_PHY_SHPRE
)
1365 tap
->wr_flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
1367 case RT2860_PHY_OFDM
:
1368 switch (phy
& RT2860_PHY_MCS
) {
1369 case 0: tap
->wr_rate
= 12; break;
1370 case 1: tap
->wr_rate
= 18; break;
1371 case 2: tap
->wr_rate
= 24; break;
1372 case 3: tap
->wr_rate
= 36; break;
1373 case 4: tap
->wr_rate
= 48; break;
1374 case 5: tap
->wr_rate
= 72; break;
1375 case 6: tap
->wr_rate
= 96; break;
1376 case 7: tap
->wr_rate
= 108; break;
1383 wh
= mtod(m
, struct ieee80211_frame
*);
1385 /* send the frame to the 802.11 layer */
1386 ni
= ieee80211_find_rxnode(ic
,
1387 (struct ieee80211_frame_min
*)wh
);
1389 (void)ieee80211_input(ni
, m
, rssi
- nf
, nf
);
1390 ieee80211_free_node(ni
);
1392 (void)ieee80211_input_all(ic
, m
, rssi
- nf
, nf
);
1396 skip
: rxd
->sdl0
&= ~htole16(RT2860_RX_DDONE
);
1398 bus_dmamap_sync(sc
->rxq
.desc_dmat
, sc
->rxq
.desc_map
,
1399 BUS_DMASYNC_PREWRITE
);
1401 sc
->rxq
.cur
= (sc
->rxq
.cur
+ 1) % RT2860_RX_RING_COUNT
;
1404 /* tell HW what we have processed */
1405 RAL_WRITE(sc
, RT2860_RX_CALC_IDX
,
1406 (sc
->rxq
.cur
- 1) % RT2860_RX_RING_COUNT
);
1410 rt2860_tbtt_intr(struct rt2860_softc
*sc
)
1413 struct ieee80211com
*ic
= &sc
->sc_ic
;
1415 #ifndef IEEE80211_STA_ONLY
1416 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
) {
1417 /* one less beacon until next DTIM */
1418 if (ic
->ic_dtim_count
== 0)
1419 ic
->ic_dtim_count
= ic
->ic_dtim_period
- 1;
1421 ic
->ic_dtim_count
--;
1423 /* update dynamic parts of beacon */
1424 rt2860_setup_beacon(sc
);
1426 /* flush buffered multicast frames */
1427 if (ic
->ic_dtim_count
== 0)
1428 ieee80211_notify_dtim(ic
);
1431 /* check if protection mode has changed */
1432 if ((sc
->sc_ic_flags
^ ic
->ic_flags
) & IEEE80211_F_USEPROT
) {
1433 rt2860_updateprot(sc
);
1434 sc
->sc_ic_flags
= ic
->ic_flags
;
1440 rt2860_gp_intr(struct rt2860_softc
*sc
)
1442 struct ieee80211com
*ic
= &sc
->sc_ic
;
1443 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
1445 DPRINTFN(2, ("GP timeout state=%d\n", vap
->iv_state
));
1447 if (vap
->iv_state
== IEEE80211_S_RUN
)
1448 rt2860_updatestats(sc
);
1452 rt2860_intr(void *arg
)
1454 struct rt2860_softc
*sc
= arg
;
1459 r
= RAL_READ(sc
, RT2860_INT_STATUS
);
1460 if (__predict_false(r
== 0xffffffff)) {
1462 return; /* device likely went away */
1466 return; /* not for us */
1469 /* acknowledge interrupts */
1470 RAL_WRITE(sc
, RT2860_INT_STATUS
, r
);
1472 if (r
& RT2860_TX_RX_COHERENT
)
1473 rt2860_intr_coherent(sc
);
1475 if (r
& RT2860_MAC_INT_2
) /* TX status */
1476 rt2860_drain_stats_fifo(sc
);
1478 if (r
& RT2860_TX_DONE_INT5
)
1479 rt2860_tx_intr(sc
, 5);
1481 if (r
& RT2860_RX_DONE_INT
)
1484 if (r
& RT2860_TX_DONE_INT4
)
1485 rt2860_tx_intr(sc
, 4);
1487 if (r
& RT2860_TX_DONE_INT3
)
1488 rt2860_tx_intr(sc
, 3);
1490 if (r
& RT2860_TX_DONE_INT2
)
1491 rt2860_tx_intr(sc
, 2);
1493 if (r
& RT2860_TX_DONE_INT1
)
1494 rt2860_tx_intr(sc
, 1);
1496 if (r
& RT2860_TX_DONE_INT0
)
1497 rt2860_tx_intr(sc
, 0);
1499 if (r
& RT2860_MAC_INT_0
) /* TBTT */
1500 rt2860_tbtt_intr(sc
);
1502 if (r
& RT2860_MAC_INT_3
) { /* Auto wakeup */
1506 if (r
& RT2860_MAC_INT_4
) /* GP timer */
1513 rt2860_tx(struct rt2860_softc
*sc
, struct mbuf
*m
, struct ieee80211_node
*ni
)
1515 struct ieee80211com
*ic
= &sc
->sc_ic
;
1516 struct ieee80211vap
*vap
= ni
->ni_vap
;
1517 struct rt2860_tx_ring
*ring
;
1518 struct rt2860_tx_data
*data
;
1519 struct rt2860_txd
*txd
;
1520 struct rt2860_txwi
*txwi
;
1521 struct ieee80211_frame
*wh
;
1522 const struct ieee80211_txparam
*tp
;
1523 struct ieee80211_key
*k
;
1525 bus_dma_segment_t segs
[RT2860_MAX_SCATTER
];
1526 bus_dma_segment_t
*seg
;
1529 uint8_t type
, qsel
, mcs
, pid
, tid
, qid
;
1530 int i
, nsegs
, ntxds
, pad
, rate
, ridx
, error
;
1532 #if defined(__DragonFly__)
1533 ntxds
= 0; /* quiet gcc5 */
1535 /* the data pool contains at least one element, pick the first */
1536 data
= SLIST_FIRST(&sc
->data_pool
);
1538 wh
= mtod(m
, struct ieee80211_frame
*);
1540 if (wh
->i_fc
[1] & IEEE80211_FC1_PROTECTED
) {
1541 k
= ieee80211_crypto_encap(ni
, m
);
1547 /* packet header may have moved, reset our local pointer */
1548 wh
= mtod(m
, struct ieee80211_frame
*);
1551 hdrlen
= ieee80211_anyhdrsize(wh
);
1552 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
1554 tp
= &vap
->iv_txparms
[ieee80211_chan2mode(ni
->ni_chan
)];
1555 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
)) {
1556 rate
= tp
->mcastrate
;
1557 } else if (m
->m_flags
& M_EAPOL
) {
1558 rate
= tp
->mgmtrate
;
1559 } else if (tp
->ucastrate
!= IEEE80211_FIXED_RATE_NONE
) {
1560 rate
= tp
->ucastrate
;
1562 (void) ieee80211_ratectl_rate(ni
, NULL
, 0);
1563 rate
= ni
->ni_txrate
;
1565 rate
&= IEEE80211_RATE_VAL
;
1567 qid
= M_WME_GETAC(m
);
1568 if (IEEE80211_QOS_HAS_SEQ(wh
)) {
1569 qos
= ((const struct ieee80211_qosframe
*)wh
)->i_qos
[0];
1570 tid
= qos
& IEEE80211_QOS_TID
;
1575 ring
= &sc
->txq
[qid
];
1576 ridx
= ieee80211_legacy_rate_lookup(ic
->ic_rt
, rate
);
1578 /* get MCS code from rate index */
1579 mcs
= rt2860_rates
[ridx
].mcs
;
1581 /* setup TX Wireless Information */
1584 /* let HW generate seq numbers for non-QoS frames */
1585 txwi
->xflags
= qos
? 0 : RT2860_TX_NSEQ
;
1586 if (type
== IEEE80211_FC0_TYPE_DATA
)
1587 txwi
->wcid
= IEEE80211_AID(ni
->ni_associd
);
1590 txwi
->len
= htole16(m
->m_pkthdr
.len
);
1591 if (rt2860_rates
[ridx
].phy
== IEEE80211_T_DS
) {
1592 txwi
->phy
= htole16(RT2860_PHY_CCK
);
1593 if (ridx
!= RT2860_RIDX_CCK1
&&
1594 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
1595 mcs
|= RT2860_PHY_SHPRE
;
1597 txwi
->phy
= htole16(RT2860_PHY_OFDM
);
1598 txwi
->phy
|= htole16(mcs
);
1601 * We store the MCS code into the driver-private PacketID field.
1602 * The PacketID is latched into TX_STAT_FIFO when Tx completes so
1603 * that we know at which initial rate the frame was transmitted.
1604 * We add 1 to the MCS code because setting the PacketID field to
1605 * 0 means that we don't want feedback in TX_STAT_FIFO.
1607 pid
= (mcs
+ 1) & 0xf;
1608 txwi
->len
|= htole16(pid
<< RT2860_TX_PID_SHIFT
);
1610 /* check if RTS/CTS or CTS-to-self protection is required */
1611 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
1612 (m
->m_pkthdr
.len
+ IEEE80211_CRC_LEN
> vap
->iv_rtsthreshold
||
1613 ((ic
->ic_flags
& IEEE80211_F_USEPROT
) &&
1614 rt2860_rates
[ridx
].phy
== IEEE80211_T_OFDM
)))
1615 txwi
->txop
= RT2860_TX_TXOP_HT
;
1617 txwi
->txop
= RT2860_TX_TXOP_BACKOFF
;
1619 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
1620 (!qos
|| (qos
& IEEE80211_QOS_ACKPOLICY
) !=
1621 IEEE80211_QOS_ACKPOLICY_NOACK
)) {
1622 txwi
->xflags
|= RT2860_TX_ACK
;
1624 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
1625 dur
= rt2860_rates
[ridx
].sp_ack_dur
;
1627 dur
= rt2860_rates
[ridx
].lp_ack_dur
;
1628 *(uint16_t *)wh
->i_dur
= htole16(dur
);
1630 /* ask MAC to insert timestamp into probe responses */
1632 (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
1633 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
1634 /* NOTE: beacons do not pass through tx_data() */
1635 txwi
->flags
|= RT2860_TX_TS
;
1637 if (ieee80211_radiotap_active_vap(vap
)) {
1638 struct rt2860_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1641 tap
->wt_rate
= rate
;
1642 if (mcs
& RT2860_PHY_SHPRE
)
1643 tap
->wt_flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
1645 ieee80211_radiotap_tx(vap
, m
);
1648 pad
= (hdrlen
+ 3) & ~3;
1650 /* copy and trim 802.11 header */
1651 memcpy(txwi
+ 1, wh
, hdrlen
);
1654 #if defined(__DragonFly__)
1655 error
= bus_dmamap_load_mbuf_segment(sc
->txwi_dmat
, data
->map
, m
,
1656 segs
, 1, &nsegs
, BUS_DMA_NOWAIT
);
1658 error
= bus_dmamap_load_mbuf_sg(sc
->txwi_dmat
, data
->map
, m
, segs
,
1661 if (__predict_false(error
!= 0 && error
!= EFBIG
)) {
1662 device_printf(sc
->sc_dev
, "can't map mbuf (error %d)\n",
1667 if (__predict_true(error
== 0)) {
1668 /* determine how many TXDs are required */
1669 ntxds
= 1 + (nsegs
/ 2);
1671 if (ring
->queued
+ ntxds
>= RT2860_TX_RING_COUNT
) {
1672 /* not enough free TXDs, force mbuf defrag */
1673 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
1677 if (__predict_false(error
!= 0)) {
1678 m1
= m_defrag(m
, M_NOWAIT
);
1680 device_printf(sc
->sc_dev
,
1681 "could not defragment mbuf\n");
1687 #if defined(__DragonFly__)
1688 error
= bus_dmamap_load_mbuf_segment(sc
->txwi_dmat
, data
->map
,
1689 m
, segs
, 1, &nsegs
, BUS_DMA_NOWAIT
);
1691 error
= bus_dmamap_load_mbuf_sg(sc
->txwi_dmat
, data
->map
, m
,
1694 if (__predict_false(error
!= 0)) {
1695 device_printf(sc
->sc_dev
, "can't map mbuf (error %d)\n",
1701 /* determine how many TXDs are now required */
1702 ntxds
= 1 + (nsegs
/ 2);
1704 if (ring
->queued
+ ntxds
>= RT2860_TX_RING_COUNT
) {
1705 /* this is a hopeless case, drop the mbuf! */
1706 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
1712 qsel
= (qid
< WME_NUM_AC
) ? RT2860_TX_QSEL_EDCA
: RT2860_TX_QSEL_MGMT
;
1714 /* first segment is TXWI + 802.11 header */
1715 txd
= &ring
->txd
[ring
->cur
];
1716 txd
->sdp0
= htole32(data
->paddr
);
1717 txd
->sdl0
= htole16(sizeof (struct rt2860_txwi
) + pad
);
1720 /* setup payload segments */
1722 for (i
= nsegs
; i
>= 2; i
-= 2) {
1723 txd
->sdp1
= htole32(seg
->ds_addr
);
1724 txd
->sdl1
= htole16(seg
->ds_len
);
1726 ring
->cur
= (ring
->cur
+ 1) % RT2860_TX_RING_COUNT
;
1727 /* grab a new Tx descriptor */
1728 txd
= &ring
->txd
[ring
->cur
];
1729 txd
->sdp0
= htole32(seg
->ds_addr
);
1730 txd
->sdl0
= htole16(seg
->ds_len
);
1734 /* finalize last segment */
1736 txd
->sdp1
= htole32(seg
->ds_addr
);
1737 txd
->sdl1
= htole16(seg
->ds_len
| RT2860_TX_LS1
);
1739 txd
->sdl0
|= htole16(RT2860_TX_LS0
);
1743 /* remove from the free pool and link it into the SW Tx slot */
1744 SLIST_REMOVE_HEAD(&sc
->data_pool
, next
);
1747 ring
->data
[ring
->cur
] = data
;
1749 bus_dmamap_sync(sc
->txwi_dmat
, sc
->txwi_map
, BUS_DMASYNC_PREWRITE
);
1750 bus_dmamap_sync(sc
->txwi_dmat
, data
->map
, BUS_DMASYNC_PREWRITE
);
1751 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
, BUS_DMASYNC_PREWRITE
);
1753 DPRINTFN(4, ("sending frame qid=%d wcid=%d nsegs=%d ridx=%d\n",
1754 qid
, txwi
->wcid
, nsegs
, ridx
));
1756 ring
->cur
= (ring
->cur
+ 1) % RT2860_TX_RING_COUNT
;
1757 ring
->queued
+= ntxds
;
1758 if (ring
->queued
>= RT2860_TX_RING_COUNT
)
1759 sc
->qfullmsk
|= 1 << qid
;
1762 RAL_WRITE(sc
, RT2860_TX_CTX_IDX(qid
), ring
->cur
);
1768 rt2860_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
1769 const struct ieee80211_bpf_params
*params
)
1771 struct ieee80211com
*ic
= ni
->ni_ic
;
1772 struct rt2860_softc
*sc
= ic
->ic_softc
;
1777 /* prevent management frames from being sent if we're not ready */
1778 if (!(sc
->sc_flags
& RT2860_RUNNING
)) {
1783 if (params
== NULL
) {
1785 * Legacy path; interpret frame contents to decide
1786 * precisely how to send the frame.
1788 error
= rt2860_tx(sc
, m
, ni
);
1791 * Caller supplied explicit parameters to use in
1792 * sending the frame.
1794 error
= rt2860_tx_raw(sc
, m
, ni
, params
);
1796 sc
->sc_tx_timer
= 5;
1802 rt2860_tx_raw(struct rt2860_softc
*sc
, struct mbuf
*m
,
1803 struct ieee80211_node
*ni
, const struct ieee80211_bpf_params
*params
)
1805 struct ieee80211com
*ic
= &sc
->sc_ic
;
1806 struct ieee80211vap
*vap
= ni
->ni_vap
;
1807 struct rt2860_tx_ring
*ring
;
1808 struct rt2860_tx_data
*data
;
1809 struct rt2860_txd
*txd
;
1810 struct rt2860_txwi
*txwi
;
1811 struct ieee80211_frame
*wh
;
1813 bus_dma_segment_t segs
[RT2860_MAX_SCATTER
];
1814 bus_dma_segment_t
*seg
;
1817 uint8_t type
, qsel
, mcs
, pid
, tid
, qid
;
1818 int i
, nsegs
, ntxds
, pad
, rate
, ridx
, error
;
1820 #if defined(__DragonFly__)
1821 ntxds
= 0; /* quiet gcc5 */
1823 /* the data pool contains at least one element, pick the first */
1824 data
= SLIST_FIRST(&sc
->data_pool
);
1826 wh
= mtod(m
, struct ieee80211_frame
*);
1827 hdrlen
= ieee80211_hdrsize(wh
);
1828 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
1830 /* Choose a TX rate index. */
1831 rate
= params
->ibp_rate0
;
1832 ridx
= ieee80211_legacy_rate_lookup(ic
->ic_rt
,
1833 rate
& IEEE80211_RATE_VAL
);
1834 if (ridx
== (uint8_t)-1) {
1835 /* XXX fall back to mcast/mgmt rate? */
1840 qid
= params
->ibp_pri
& 3;
1842 ring
= &sc
->txq
[qid
];
1844 /* get MCS code from rate index */
1845 mcs
= rt2860_rates
[ridx
].mcs
;
1847 /* setup TX Wireless Information */
1850 /* let HW generate seq numbers for non-QoS frames */
1851 txwi
->xflags
= params
->ibp_pri
& 3 ? 0 : RT2860_TX_NSEQ
;
1853 txwi
->len
= htole16(m
->m_pkthdr
.len
);
1854 if (rt2860_rates
[ridx
].phy
== IEEE80211_T_DS
) {
1855 txwi
->phy
= htole16(RT2860_PHY_CCK
);
1856 if (ridx
!= RT2860_RIDX_CCK1
&&
1857 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
1858 mcs
|= RT2860_PHY_SHPRE
;
1860 txwi
->phy
= htole16(RT2860_PHY_OFDM
);
1861 txwi
->phy
|= htole16(mcs
);
1864 * We store the MCS code into the driver-private PacketID field.
1865 * The PacketID is latched into TX_STAT_FIFO when Tx completes so
1866 * that we know at which initial rate the frame was transmitted.
1867 * We add 1 to the MCS code because setting the PacketID field to
1868 * 0 means that we don't want feedback in TX_STAT_FIFO.
1870 pid
= (mcs
+ 1) & 0xf;
1871 txwi
->len
|= htole16(pid
<< RT2860_TX_PID_SHIFT
);
1873 /* check if RTS/CTS or CTS-to-self protection is required */
1874 if (params
->ibp_flags
& IEEE80211_BPF_RTS
||
1875 params
->ibp_flags
& IEEE80211_BPF_CTS
)
1876 txwi
->txop
= RT2860_TX_TXOP_HT
;
1878 txwi
->txop
= RT2860_TX_TXOP_BACKOFF
;
1879 if ((params
->ibp_flags
& IEEE80211_BPF_NOACK
) == 0) {
1880 txwi
->xflags
|= RT2860_TX_ACK
;
1882 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
1883 dur
= rt2860_rates
[ridx
].sp_ack_dur
;
1885 dur
= rt2860_rates
[ridx
].lp_ack_dur
;
1886 *(uint16_t *)wh
->i_dur
= htole16(dur
);
1888 /* ask MAC to insert timestamp into probe responses */
1890 (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
1891 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
1892 /* NOTE: beacons do not pass through tx_data() */
1893 txwi
->flags
|= RT2860_TX_TS
;
1895 if (ieee80211_radiotap_active_vap(vap
)) {
1896 struct rt2860_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1899 tap
->wt_rate
= rate
;
1900 if (mcs
& RT2860_PHY_SHPRE
)
1901 tap
->wt_flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
1903 ieee80211_radiotap_tx(vap
, m
);
1906 pad
= (hdrlen
+ 3) & ~3;
1908 /* copy and trim 802.11 header */
1909 memcpy(txwi
+ 1, wh
, hdrlen
);
1912 #if defined(__DragonFly__)
1913 error
= bus_dmamap_load_mbuf_segment(sc
->txwi_dmat
, data
->map
, m
,
1914 segs
, 1, &nsegs
, BUS_DMA_NOWAIT
);
1916 error
= bus_dmamap_load_mbuf_sg(sc
->txwi_dmat
, data
->map
, m
, segs
,
1919 if (__predict_false(error
!= 0 && error
!= EFBIG
)) {
1920 device_printf(sc
->sc_dev
, "can't map mbuf (error %d)\n",
1925 if (__predict_true(error
== 0)) {
1926 /* determine how many TXDs are required */
1927 ntxds
= 1 + (nsegs
/ 2);
1929 if (ring
->queued
+ ntxds
>= RT2860_TX_RING_COUNT
) {
1930 /* not enough free TXDs, force mbuf defrag */
1931 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
1935 if (__predict_false(error
!= 0)) {
1936 m1
= m_defrag(m
, M_NOWAIT
);
1938 device_printf(sc
->sc_dev
,
1939 "could not defragment mbuf\n");
1945 #if defined(__DragonFly__)
1946 error
= bus_dmamap_load_mbuf_segment(sc
->txwi_dmat
, data
->map
,
1947 m
, segs
, 1, &nsegs
, BUS_DMA_NOWAIT
);
1949 error
= bus_dmamap_load_mbuf_sg(sc
->txwi_dmat
, data
->map
, m
,
1952 if (__predict_false(error
!= 0)) {
1953 device_printf(sc
->sc_dev
, "can't map mbuf (error %d)\n",
1959 /* determine how many TXDs are now required */
1960 ntxds
= 1 + (nsegs
/ 2);
1962 if (ring
->queued
+ ntxds
>= RT2860_TX_RING_COUNT
) {
1963 /* this is a hopeless case, drop the mbuf! */
1964 bus_dmamap_unload(sc
->txwi_dmat
, data
->map
);
1970 qsel
= (qid
< WME_NUM_AC
) ? RT2860_TX_QSEL_EDCA
: RT2860_TX_QSEL_MGMT
;
1972 /* first segment is TXWI + 802.11 header */
1973 txd
= &ring
->txd
[ring
->cur
];
1974 txd
->sdp0
= htole32(data
->paddr
);
1975 txd
->sdl0
= htole16(sizeof (struct rt2860_txwi
) + pad
);
1978 /* setup payload segments */
1980 for (i
= nsegs
; i
>= 2; i
-= 2) {
1981 txd
->sdp1
= htole32(seg
->ds_addr
);
1982 txd
->sdl1
= htole16(seg
->ds_len
);
1984 ring
->cur
= (ring
->cur
+ 1) % RT2860_TX_RING_COUNT
;
1985 /* grab a new Tx descriptor */
1986 txd
= &ring
->txd
[ring
->cur
];
1987 txd
->sdp0
= htole32(seg
->ds_addr
);
1988 txd
->sdl0
= htole16(seg
->ds_len
);
1992 /* finalize last segment */
1994 txd
->sdp1
= htole32(seg
->ds_addr
);
1995 txd
->sdl1
= htole16(seg
->ds_len
| RT2860_TX_LS1
);
1997 txd
->sdl0
|= htole16(RT2860_TX_LS0
);
2001 /* remove from the free pool and link it into the SW Tx slot */
2002 SLIST_REMOVE_HEAD(&sc
->data_pool
, next
);
2005 ring
->data
[ring
->cur
] = data
;
2007 bus_dmamap_sync(sc
->txwi_dmat
, sc
->txwi_map
, BUS_DMASYNC_PREWRITE
);
2008 bus_dmamap_sync(sc
->txwi_dmat
, data
->map
, BUS_DMASYNC_PREWRITE
);
2009 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
, BUS_DMASYNC_PREWRITE
);
2011 DPRINTFN(4, ("sending frame qid=%d wcid=%d nsegs=%d ridx=%d\n",
2012 qid
, txwi
->wcid
, nsegs
, ridx
));
2014 ring
->cur
= (ring
->cur
+ 1) % RT2860_TX_RING_COUNT
;
2015 ring
->queued
+= ntxds
;
2016 if (ring
->queued
>= RT2860_TX_RING_COUNT
)
2017 sc
->qfullmsk
|= 1 << qid
;
2020 RAL_WRITE(sc
, RT2860_TX_CTX_IDX(qid
), ring
->cur
);
2026 rt2860_transmit(struct ieee80211com
*ic
, struct mbuf
*m
)
2028 struct rt2860_softc
*sc
= ic
->ic_softc
;
2032 if ((sc
->sc_flags
& RT2860_RUNNING
) == 0) {
2036 error
= mbufq_enqueue(&sc
->sc_snd
, m
);
2048 rt2860_start(struct rt2860_softc
*sc
)
2050 struct ieee80211_node
*ni
;
2053 RAL_LOCK_ASSERT(sc
);
2055 if ((sc
->sc_flags
& RT2860_RUNNING
) == 0)
2058 while (!SLIST_EMPTY(&sc
->data_pool
) && sc
->qfullmsk
== 0 &&
2059 (m
= mbufq_dequeue(&sc
->sc_snd
)) != NULL
) {
2060 ni
= (struct ieee80211_node
*)m
->m_pkthdr
.rcvif
;
2061 if (rt2860_tx(sc
, m
, ni
) != 0) {
2062 if_inc_counter(ni
->ni_vap
->iv_ifp
,
2063 IFCOUNTER_OERRORS
, 1);
2064 ieee80211_free_node(ni
);
2067 sc
->sc_tx_timer
= 5;
2072 rt2860_watchdog(void *arg
)
2074 struct rt2860_softc
*sc
= arg
;
2076 RAL_LOCK_ASSERT(sc
);
2078 KASSERT(sc
->sc_flags
& RT2860_RUNNING
, ("not running"));
2080 if (sc
->sc_invalid
) /* card ejected */
2083 if (sc
->sc_tx_timer
> 0 && --sc
->sc_tx_timer
== 0) {
2084 device_printf(sc
->sc_dev
, "device timeout\n");
2085 rt2860_stop_locked(sc
);
2086 rt2860_init_locked(sc
);
2087 #if defined(__DragonFly__)
2088 /* not implemented */
2090 counter_u64_add(sc
->sc_ic
.ic_oerrors
, 1);
2094 callout_reset(&sc
->watchdog_ch
, hz
, rt2860_watchdog
, sc
);
2098 rt2860_parent(struct ieee80211com
*ic
)
2100 struct rt2860_softc
*sc
= ic
->ic_softc
;
2104 if (ic
->ic_nrunning
> 0) {
2105 if (!(sc
->sc_flags
& RT2860_RUNNING
)) {
2106 rt2860_init_locked(sc
);
2109 rt2860_update_promisc(ic
);
2110 } else if (sc
->sc_flags
& RT2860_RUNNING
)
2111 rt2860_stop_locked(sc
);
2114 ieee80211_start_all(ic
);
2118 * Reading and writing from/to the BBP is different from RT2560 and RT2661.
2119 * We access the BBP through the 8051 microcontroller unit which means that
2120 * the microcode must be loaded first.
2123 rt2860_mcu_bbp_write(struct rt2860_softc
*sc
, uint8_t reg
, uint8_t val
)
2127 for (ntries
= 0; ntries
< 100; ntries
++) {
2128 if (!(RAL_READ(sc
, RT2860_H2M_BBPAGENT
) & RT2860_BBP_CSR_KICK
))
2132 if (ntries
== 100) {
2133 device_printf(sc
->sc_dev
,
2134 "could not write to BBP through MCU\n");
2138 RAL_WRITE(sc
, RT2860_H2M_BBPAGENT
, RT2860_BBP_RW_PARALLEL
|
2139 RT2860_BBP_CSR_KICK
| reg
<< 8 | val
);
2140 RAL_BARRIER_WRITE(sc
);
2142 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_BBP
, 0, 0);
2147 rt2860_mcu_bbp_read(struct rt2860_softc
*sc
, uint8_t reg
)
2152 for (ntries
= 0; ntries
< 100; ntries
++) {
2153 if (!(RAL_READ(sc
, RT2860_H2M_BBPAGENT
) & RT2860_BBP_CSR_KICK
))
2157 if (ntries
== 100) {
2158 device_printf(sc
->sc_dev
,
2159 "could not read from BBP through MCU\n");
2163 RAL_WRITE(sc
, RT2860_H2M_BBPAGENT
, RT2860_BBP_RW_PARALLEL
|
2164 RT2860_BBP_CSR_KICK
| RT2860_BBP_CSR_READ
| reg
<< 8);
2165 RAL_BARRIER_WRITE(sc
);
2167 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_BBP
, 0, 0);
2170 for (ntries
= 0; ntries
< 100; ntries
++) {
2171 val
= RAL_READ(sc
, RT2860_H2M_BBPAGENT
);
2172 if (!(val
& RT2860_BBP_CSR_KICK
))
2176 device_printf(sc
->sc_dev
, "could not read from BBP through MCU\n");
2182 * Write to one of the 4 programmable 24-bit RF registers.
2185 rt2860_rf_write(struct rt2860_softc
*sc
, uint8_t reg
, uint32_t val
)
2190 for (ntries
= 0; ntries
< 100; ntries
++) {
2191 if (!(RAL_READ(sc
, RT2860_RF_CSR_CFG0
) & RT2860_RF_REG_CTRL
))
2195 if (ntries
== 100) {
2196 device_printf(sc
->sc_dev
, "could not write to RF\n");
2200 /* RF registers are 24-bit on the RT2860 */
2201 tmp
= RT2860_RF_REG_CTRL
| 24 << RT2860_RF_REG_WIDTH_SHIFT
|
2202 (val
& 0x3fffff) << 2 | (reg
& 3);
2203 RAL_WRITE(sc
, RT2860_RF_CSR_CFG0
, tmp
);
2207 rt3090_rf_read(struct rt2860_softc
*sc
, uint8_t reg
)
2212 for (ntries
= 0; ntries
< 100; ntries
++) {
2213 if (!(RAL_READ(sc
, RT3070_RF_CSR_CFG
) & RT3070_RF_KICK
))
2217 if (ntries
== 100) {
2218 device_printf(sc
->sc_dev
, "could not read RF register\n");
2221 tmp
= RT3070_RF_KICK
| reg
<< 8;
2222 RAL_WRITE(sc
, RT3070_RF_CSR_CFG
, tmp
);
2224 for (ntries
= 0; ntries
< 100; ntries
++) {
2225 tmp
= RAL_READ(sc
, RT3070_RF_CSR_CFG
);
2226 if (!(tmp
& RT3070_RF_KICK
))
2230 if (ntries
== 100) {
2231 device_printf(sc
->sc_dev
, "could not read RF register\n");
2238 rt3090_rf_write(struct rt2860_softc
*sc
, uint8_t reg
, uint8_t val
)
2243 for (ntries
= 0; ntries
< 10; ntries
++) {
2244 if (!(RAL_READ(sc
, RT3070_RF_CSR_CFG
) & RT3070_RF_KICK
))
2249 device_printf(sc
->sc_dev
, "could not write to RF\n");
2253 tmp
= RT3070_RF_WRITE
| RT3070_RF_KICK
| reg
<< 8 | val
;
2254 RAL_WRITE(sc
, RT3070_RF_CSR_CFG
, tmp
);
2258 * Send a command to the 8051 microcontroller unit.
2261 rt2860_mcu_cmd(struct rt2860_softc
*sc
, uint8_t cmd
, uint16_t arg
, int wait
)
2267 for (ntries
= 0; ntries
< 100; ntries
++) {
2268 if (!(RAL_READ(sc
, RT2860_H2M_MAILBOX
) & RT2860_H2M_BUSY
))
2275 cid
= wait
? cmd
: RT2860_TOKEN_NO_INTR
;
2276 RAL_WRITE(sc
, RT2860_H2M_MAILBOX
, RT2860_H2M_BUSY
| cid
<< 16 | arg
);
2277 RAL_BARRIER_WRITE(sc
);
2278 RAL_WRITE(sc
, RT2860_HOST_CMD
, cmd
);
2282 /* wait for the command to complete */
2283 for (ntries
= 0; ntries
< 200; ntries
++) {
2284 tmp
= RAL_READ(sc
, RT2860_H2M_MAILBOX_CID
);
2285 /* find the command slot */
2286 for (slot
= 0; slot
< 4; slot
++, tmp
>>= 8)
2287 if ((tmp
& 0xff) == cid
)
2293 if (ntries
== 200) {
2294 /* clear command and status */
2295 RAL_WRITE(sc
, RT2860_H2M_MAILBOX_STATUS
, 0xffffffff);
2296 RAL_WRITE(sc
, RT2860_H2M_MAILBOX_CID
, 0xffffffff);
2299 /* get command status (1 means success) */
2300 tmp
= RAL_READ(sc
, RT2860_H2M_MAILBOX_STATUS
);
2301 tmp
= (tmp
>> (slot
* 8)) & 0xff;
2302 DPRINTF(("MCU command=0x%02x slot=%d status=0x%02x\n",
2304 /* clear command and status */
2305 RAL_WRITE(sc
, RT2860_H2M_MAILBOX_STATUS
, 0xffffffff);
2306 RAL_WRITE(sc
, RT2860_H2M_MAILBOX_CID
, 0xffffffff);
2307 return (tmp
== 1) ? 0 : EIO
;
2311 rt2860_enable_mrr(struct rt2860_softc
*sc
)
2313 #define CCK(mcs) (mcs)
2314 #define OFDM(mcs) (1 << 3 | (mcs))
2315 RAL_WRITE(sc
, RT2860_LG_FBK_CFG0
,
2316 OFDM(6) << 28 | /* 54->48 */
2317 OFDM(5) << 24 | /* 48->36 */
2318 OFDM(4) << 20 | /* 36->24 */
2319 OFDM(3) << 16 | /* 24->18 */
2320 OFDM(2) << 12 | /* 18->12 */
2321 OFDM(1) << 8 | /* 12-> 9 */
2322 OFDM(0) << 4 | /* 9-> 6 */
2323 OFDM(0)); /* 6-> 6 */
2325 RAL_WRITE(sc
, RT2860_LG_FBK_CFG1
,
2326 CCK(2) << 12 | /* 11->5.5 */
2327 CCK(1) << 8 | /* 5.5-> 2 */
2328 CCK(0) << 4 | /* 2-> 1 */
2329 CCK(0)); /* 1-> 1 */
2335 rt2860_set_txpreamble(struct rt2860_softc
*sc
)
2337 struct ieee80211com
*ic
= &sc
->sc_ic
;
2340 tmp
= RAL_READ(sc
, RT2860_AUTO_RSP_CFG
);
2341 tmp
&= ~RT2860_CCK_SHORT_EN
;
2342 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
2343 tmp
|= RT2860_CCK_SHORT_EN
;
2344 RAL_WRITE(sc
, RT2860_AUTO_RSP_CFG
, tmp
);
2348 rt2860_set_basicrates(struct rt2860_softc
*sc
,
2349 const struct ieee80211_rateset
*rs
)
2351 struct ieee80211com
*ic
= &sc
->sc_ic
;
2356 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
2357 rate
= rs
->rs_rates
[i
];
2359 if (!(rate
& IEEE80211_RATE_BASIC
))
2362 mask
|= 1 << ieee80211_legacy_rate_lookup(ic
->ic_rt
,
2363 IEEE80211_RV(rate
));
2366 RAL_WRITE(sc
, RT2860_LEGACY_BASIC_RATE
, mask
);
2370 rt2860_scan_start(struct ieee80211com
*ic
)
2372 struct rt2860_softc
*sc
= ic
->ic_softc
;
2375 tmp
= RAL_READ(sc
, RT2860_BCN_TIME_CFG
);
2376 RAL_WRITE(sc
, RT2860_BCN_TIME_CFG
,
2377 tmp
& ~(RT2860_BCN_TX_EN
| RT2860_TSF_TIMER_EN
|
2378 RT2860_TBTT_TIMER_EN
));
2379 rt2860_set_gp_timer(sc
, 0);
2383 rt2860_scan_end(struct ieee80211com
*ic
)
2385 struct rt2860_softc
*sc
= ic
->ic_softc
;
2386 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
2388 if (vap
->iv_state
== IEEE80211_S_RUN
) {
2389 rt2860_enable_tsf_sync(sc
);
2390 rt2860_set_gp_timer(sc
, 500);
2395 rt2860_set_channel(struct ieee80211com
*ic
)
2397 struct rt2860_softc
*sc
= ic
->ic_softc
;
2400 rt2860_switch_chan(sc
, ic
->ic_curchan
);
2405 rt2860_select_chan_group(struct rt2860_softc
*sc
, int group
)
2410 rt2860_mcu_bbp_write(sc
, 62, 0x37 - sc
->lna
[group
]);
2411 rt2860_mcu_bbp_write(sc
, 63, 0x37 - sc
->lna
[group
]);
2412 rt2860_mcu_bbp_write(sc
, 64, 0x37 - sc
->lna
[group
]);
2413 rt2860_mcu_bbp_write(sc
, 86, 0x00);
2416 if (sc
->ext_2ghz_lna
) {
2417 rt2860_mcu_bbp_write(sc
, 82, 0x62);
2418 rt2860_mcu_bbp_write(sc
, 75, 0x46);
2420 rt2860_mcu_bbp_write(sc
, 82, 0x84);
2421 rt2860_mcu_bbp_write(sc
, 75, 0x50);
2424 if (sc
->ext_5ghz_lna
) {
2425 rt2860_mcu_bbp_write(sc
, 82, 0xf2);
2426 rt2860_mcu_bbp_write(sc
, 75, 0x46);
2428 rt2860_mcu_bbp_write(sc
, 82, 0xf2);
2429 rt2860_mcu_bbp_write(sc
, 75, 0x50);
2433 tmp
= RAL_READ(sc
, RT2860_TX_BAND_CFG
);
2434 tmp
&= ~(RT2860_5G_BAND_SEL_N
| RT2860_5G_BAND_SEL_P
);
2435 tmp
|= (group
== 0) ? RT2860_5G_BAND_SEL_N
: RT2860_5G_BAND_SEL_P
;
2436 RAL_WRITE(sc
, RT2860_TX_BAND_CFG
, tmp
);
2438 /* enable appropriate Power Amplifiers and Low Noise Amplifiers */
2439 tmp
= RT2860_RFTR_EN
| RT2860_TRSW_EN
| RT2860_LNA_PE0_EN
;
2440 if (sc
->nrxchains
> 1)
2441 tmp
|= RT2860_LNA_PE1_EN
;
2442 if (sc
->mac_ver
== 0x3593 && sc
->nrxchains
> 2)
2443 tmp
|= RT3593_LNA_PE2_EN
;
2444 if (group
== 0) { /* 2GHz */
2445 tmp
|= RT2860_PA_PE_G0_EN
;
2446 if (sc
->ntxchains
> 1)
2447 tmp
|= RT2860_PA_PE_G1_EN
;
2448 if (sc
->mac_ver
== 0x3593 && sc
->ntxchains
> 2)
2449 tmp
|= RT3593_PA_PE_G2_EN
;
2451 tmp
|= RT2860_PA_PE_A0_EN
;
2452 if (sc
->ntxchains
> 1)
2453 tmp
|= RT2860_PA_PE_A1_EN
;
2454 if (sc
->mac_ver
== 0x3593 && sc
->ntxchains
> 2)
2455 tmp
|= RT3593_PA_PE_A2_EN
;
2457 RAL_WRITE(sc
, RT2860_TX_PIN_CFG
, tmp
);
2459 if (sc
->mac_ver
== 0x3593) {
2460 tmp
= RAL_READ(sc
, RT2860_GPIO_CTRL
);
2461 if (sc
->sc_flags
& RT2860_PCIE
) {
2470 tmp
= (tmp
& ~0x00001000) | 0x00000010;
2471 RAL_WRITE(sc
, RT2860_GPIO_CTRL
, tmp
);
2474 /* set initial AGC value */
2475 if (group
== 0) { /* 2GHz band */
2476 if (sc
->mac_ver
>= 0x3071)
2477 agc
= 0x1c + sc
->lna
[0] * 2;
2479 agc
= 0x2e + sc
->lna
[0];
2480 } else { /* 5GHz band */
2481 agc
= 0x32 + (sc
->lna
[group
] * 5) / 3;
2483 rt2860_mcu_bbp_write(sc
, 66, agc
);
2489 rt2860_set_chan(struct rt2860_softc
*sc
, u_int chan
)
2491 const struct rfprog
*rfprog
= rt2860_rf2850
;
2492 uint32_t r2
, r3
, r4
;
2493 int8_t txpow1
, txpow2
;
2496 /* find the settings for this channel (we know it exists) */
2497 for (i
= 0; rfprog
[i
].chan
!= chan
; i
++);
2500 if (sc
->ntxchains
== 1)
2501 r2
|= 1 << 12; /* 1T: disable Tx chain 2 */
2502 if (sc
->nrxchains
== 1)
2503 r2
|= 1 << 15 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
2504 else if (sc
->nrxchains
== 2)
2505 r2
|= 1 << 4; /* 2R: disable Rx chain 3 */
2507 /* use Tx power values from EEPROM */
2508 txpow1
= sc
->txpow1
[i
];
2509 txpow2
= sc
->txpow2
[i
];
2512 txpow1
= txpow1
<< 1 | 1;
2514 txpow1
= (7 + txpow1
) << 1;
2516 txpow2
= txpow2
<< 1 | 1;
2518 txpow2
= (7 + txpow2
) << 1;
2520 r3
= rfprog
[i
].r3
| txpow1
<< 7;
2521 r4
= rfprog
[i
].r4
| sc
->freq
<< 13 | txpow2
<< 4;
2523 rt2860_rf_write(sc
, RT2860_RF1
, rfprog
[i
].r1
);
2524 rt2860_rf_write(sc
, RT2860_RF2
, r2
);
2525 rt2860_rf_write(sc
, RT2860_RF3
, r3
);
2526 rt2860_rf_write(sc
, RT2860_RF4
, r4
);
2530 rt2860_rf_write(sc
, RT2860_RF1
, rfprog
[i
].r1
);
2531 rt2860_rf_write(sc
, RT2860_RF2
, r2
);
2532 rt2860_rf_write(sc
, RT2860_RF3
, r3
| 1);
2533 rt2860_rf_write(sc
, RT2860_RF4
, r4
);
2537 rt2860_rf_write(sc
, RT2860_RF1
, rfprog
[i
].r1
);
2538 rt2860_rf_write(sc
, RT2860_RF2
, r2
);
2539 rt2860_rf_write(sc
, RT2860_RF3
, r3
);
2540 rt2860_rf_write(sc
, RT2860_RF4
, r4
);
2544 rt3090_set_chan(struct rt2860_softc
*sc
, u_int chan
)
2546 int8_t txpow1
, txpow2
;
2550 /* RT3090 is 2GHz only */
2551 KASSERT(chan
>= 1 && chan
<= 14, ("chan %d not support", chan
));
2553 /* find the settings for this channel (we know it exists) */
2554 for (i
= 0; rt2860_rf2850
[i
].chan
!= chan
; i
++);
2556 /* use Tx power values from EEPROM */
2557 txpow1
= sc
->txpow1
[i
];
2558 txpow2
= sc
->txpow2
[i
];
2560 rt3090_rf_write(sc
, 2, rt3090_freqs
[i
].n
);
2561 rf
= rt3090_rf_read(sc
, 3);
2562 rf
= (rf
& ~0x0f) | rt3090_freqs
[i
].k
;
2563 rt3090_rf_write(sc
, 3, rf
);
2564 rf
= rt3090_rf_read(sc
, 6);
2565 rf
= (rf
& ~0x03) | rt3090_freqs
[i
].r
;
2566 rt3090_rf_write(sc
, 6, rf
);
2569 rf
= rt3090_rf_read(sc
, 12);
2570 rf
= (rf
& ~0x1f) | txpow1
;
2571 rt3090_rf_write(sc
, 12, rf
);
2574 rf
= rt3090_rf_read(sc
, 13);
2575 rf
= (rf
& ~0x1f) | txpow2
;
2576 rt3090_rf_write(sc
, 13, rf
);
2578 rf
= rt3090_rf_read(sc
, 1);
2580 if (sc
->ntxchains
== 1)
2581 rf
|= RT3070_TX1_PD
| RT3070_TX2_PD
;
2582 else if (sc
->ntxchains
== 2)
2583 rf
|= RT3070_TX2_PD
;
2584 if (sc
->nrxchains
== 1)
2585 rf
|= RT3070_RX1_PD
| RT3070_RX2_PD
;
2586 else if (sc
->nrxchains
== 2)
2587 rf
|= RT3070_RX2_PD
;
2588 rt3090_rf_write(sc
, 1, rf
);
2591 rf
= rt3090_rf_read(sc
, 23);
2592 rf
= (rf
& ~0x7f) | sc
->freq
;
2593 rt3090_rf_write(sc
, 23, rf
);
2595 /* program RF filter */
2596 rf
= rt3090_rf_read(sc
, 24); /* Tx */
2597 rf
= (rf
& ~0x3f) | sc
->rf24_20mhz
;
2598 rt3090_rf_write(sc
, 24, rf
);
2599 rf
= rt3090_rf_read(sc
, 31); /* Rx */
2600 rf
= (rf
& ~0x3f) | sc
->rf24_20mhz
;
2601 rt3090_rf_write(sc
, 31, rf
);
2603 /* enable RF tuning */
2604 rf
= rt3090_rf_read(sc
, 7);
2605 rt3090_rf_write(sc
, 7, rf
| RT3070_TUNE
);
2609 rt5390_set_chan(struct rt2860_softc
*sc
, u_int chan
)
2611 uint8_t h20mhz
, rf
, tmp
;
2612 int8_t txpow1
, txpow2
;
2615 /* RT5390 is 2GHz only */
2616 KASSERT(chan
>= 1 && chan
<= 14, ("chan %d not support", chan
));
2618 /* find the settings for this channel (we know it exists) */
2619 for (i
= 0; rt2860_rf2850
[i
].chan
!= chan
; i
++);
2621 /* use Tx power values from EEPROM */
2622 txpow1
= sc
->txpow1
[i
];
2623 txpow2
= sc
->txpow2
[i
];
2625 rt3090_rf_write(sc
, 8, rt3090_freqs
[i
].n
);
2626 rt3090_rf_write(sc
, 9, rt3090_freqs
[i
].k
& 0x0f);
2627 rf
= rt3090_rf_read(sc
, 11);
2628 rf
= (rf
& ~0x03) | (rt3090_freqs
[i
].r
& 0x03);
2629 rt3090_rf_write(sc
, 11, rf
);
2631 rf
= rt3090_rf_read(sc
, 49);
2632 rf
= (rf
& ~0x3f) | (txpow1
& 0x3f);
2633 /* the valid range of the RF R49 is 0x00~0x27 */
2634 if ((rf
& 0x3f) > 0x27)
2635 rf
= (rf
& ~0x3f) | 0x27;
2636 rt3090_rf_write(sc
, 49, rf
);
2637 if (sc
->mac_ver
== 0x5392) {
2638 rf
= rt3090_rf_read(sc
, 50);
2639 rf
= (rf
& ~0x3f) | (txpow2
& 0x3f);
2640 /* the valid range of the RF R50 is 0x00~0x27 */
2641 if ((rf
& 0x3f) > 0x27)
2642 rf
= (rf
& ~0x3f) | 0x27;
2643 rt3090_rf_write(sc
, 50, rf
);
2646 rf
= rt3090_rf_read(sc
, 1);
2647 rf
|= RT3070_RF_BLOCK
| RT3070_PLL_PD
| RT3070_RX0_PD
| RT3070_TX0_PD
;
2648 if (sc
->mac_ver
== 0x5392)
2649 rf
|= RT3070_RX1_PD
| RT3070_TX1_PD
;
2650 rt3090_rf_write(sc
, 1, rf
);
2652 rf
= rt3090_rf_read(sc
, 2);
2653 rt3090_rf_write(sc
, 2, rf
| RT3593_RESCAL
);
2655 rt3090_rf_write(sc
, 2, rf
& ~RT3593_RESCAL
);
2657 rf
= rt3090_rf_read(sc
, 17);
2659 rf
= (rf
& ~0x7f) | (sc
->freq
& 0x7f);
2662 rt2860_mcu_cmd(sc
, 0x74, (tmp
<< 8 ) | rf
, 0);
2664 if (sc
->mac_ver
== 0x5390) {
2667 else if (chan
>= 5 && chan
<= 6)
2669 else if (chan
>= 7 && chan
<= 10)
2673 rt3090_rf_write(sc
, 55, rf
);
2681 else if (chan
>= 4 && chan
<= 6)
2683 else if (chan
>= 7 && chan
<= 12)
2685 else if (chan
== 13)
2689 rt3090_rf_write(sc
, 59, rf
);
2693 h20mhz
= (sc
->rf24_20mhz
& 0x20) >> 5;
2694 rf
= rt3090_rf_read(sc
, 30);
2695 rf
= (rf
& ~0x06) | (h20mhz
<< 1) | (h20mhz
<< 2);
2696 rt3090_rf_write(sc
, 30, rf
);
2698 /* Rx BB filter VCM */
2699 rf
= rt3090_rf_read(sc
, 30);
2700 rf
= (rf
& ~0x18) | 0x10;
2701 rt3090_rf_write(sc
, 30, rf
);
2703 /* Initiate VCO calibration. */
2704 rf
= rt3090_rf_read(sc
, 3);
2705 rf
|= RT3593_VCOCAL
;
2706 rt3090_rf_write(sc
, 3, rf
);
2710 rt3090_rf_init(struct rt2860_softc
*sc
)
2716 rf
= rt3090_rf_read(sc
, 30);
2717 /* toggle RF R30 bit 7 */
2718 rt3090_rf_write(sc
, 30, rf
| 0x80);
2720 rt3090_rf_write(sc
, 30, rf
& ~0x80);
2722 tmp
= RAL_READ(sc
, RT3070_LDO_CFG0
);
2724 if (sc
->patch_dac
&& sc
->mac_rev
< 0x0211)
2725 tmp
|= 0x0d000000; /* 1.35V */
2727 tmp
|= 0x01000000; /* 1.2V */
2728 RAL_WRITE(sc
, RT3070_LDO_CFG0
, tmp
);
2730 /* patch LNA_PE_G1 */
2731 tmp
= RAL_READ(sc
, RT3070_GPIO_SWITCH
);
2732 RAL_WRITE(sc
, RT3070_GPIO_SWITCH
, tmp
& ~0x20);
2734 /* initialize RF registers to default value */
2735 for (i
= 0; i
< nitems(rt3090_def_rf
); i
++) {
2736 rt3090_rf_write(sc
, rt3090_def_rf
[i
].reg
,
2737 rt3090_def_rf
[i
].val
);
2740 /* select 20MHz bandwidth */
2741 rt3090_rf_write(sc
, 31, 0x14);
2743 rf
= rt3090_rf_read(sc
, 6);
2744 rt3090_rf_write(sc
, 6, rf
| 0x40);
2746 if (sc
->mac_ver
!= 0x3593) {
2747 /* calibrate filter for 20MHz bandwidth */
2748 sc
->rf24_20mhz
= 0x1f; /* default value */
2749 rt3090_filter_calib(sc
, 0x07, 0x16, &sc
->rf24_20mhz
);
2751 /* select 40MHz bandwidth */
2752 bbp
= rt2860_mcu_bbp_read(sc
, 4);
2753 rt2860_mcu_bbp_write(sc
, 4, (bbp
& ~0x08) | 0x10);
2754 rf
= rt3090_rf_read(sc
, 31);
2755 rt3090_rf_write(sc
, 31, rf
| 0x20);
2757 /* calibrate filter for 40MHz bandwidth */
2758 sc
->rf24_40mhz
= 0x2f; /* default value */
2759 rt3090_filter_calib(sc
, 0x27, 0x19, &sc
->rf24_40mhz
);
2761 /* go back to 20MHz bandwidth */
2762 bbp
= rt2860_mcu_bbp_read(sc
, 4);
2763 rt2860_mcu_bbp_write(sc
, 4, bbp
& ~0x18);
2765 if (sc
->mac_rev
< 0x0211)
2766 rt3090_rf_write(sc
, 27, 0x03);
2768 tmp
= RAL_READ(sc
, RT3070_OPT_14
);
2769 RAL_WRITE(sc
, RT3070_OPT_14
, tmp
| 1);
2771 if (sc
->rf_rev
== RT3070_RF_3020
)
2772 rt3090_set_rx_antenna(sc
, 0);
2774 bbp
= rt2860_mcu_bbp_read(sc
, 138);
2775 if (sc
->mac_ver
== 0x3593) {
2776 if (sc
->ntxchains
== 1)
2777 bbp
|= 0x60; /* turn off DAC1 and DAC2 */
2778 else if (sc
->ntxchains
== 2)
2779 bbp
|= 0x40; /* turn off DAC2 */
2780 if (sc
->nrxchains
== 1)
2781 bbp
&= ~0x06; /* turn off ADC1 and ADC2 */
2782 else if (sc
->nrxchains
== 2)
2783 bbp
&= ~0x04; /* turn off ADC2 */
2785 if (sc
->ntxchains
== 1)
2786 bbp
|= 0x20; /* turn off DAC1 */
2787 if (sc
->nrxchains
== 1)
2788 bbp
&= ~0x02; /* turn off ADC1 */
2790 rt2860_mcu_bbp_write(sc
, 138, bbp
);
2792 rf
= rt3090_rf_read(sc
, 1);
2793 rf
&= ~(RT3070_RX0_PD
| RT3070_TX0_PD
);
2794 rf
|= RT3070_RF_BLOCK
| RT3070_RX1_PD
| RT3070_TX1_PD
;
2795 rt3090_rf_write(sc
, 1, rf
);
2797 rf
= rt3090_rf_read(sc
, 15);
2798 rt3090_rf_write(sc
, 15, rf
& ~RT3070_TX_LO2
);
2800 rf
= rt3090_rf_read(sc
, 17);
2801 rf
&= ~RT3070_TX_LO1
;
2802 if (sc
->mac_rev
>= 0x0211 && !sc
->ext_2ghz_lna
)
2803 rf
|= 0x20; /* fix for long range Rx issue */
2804 if (sc
->txmixgain_2ghz
>= 2)
2805 rf
= (rf
& ~0x7) | sc
->txmixgain_2ghz
;
2806 rt3090_rf_write(sc
, 17, rf
);
2808 rf
= rt3090_rf_read(sc
, 20);
2809 rt3090_rf_write(sc
, 20, rf
& ~RT3070_RX_LO1
);
2811 rf
= rt3090_rf_read(sc
, 21);
2812 rt3090_rf_write(sc
, 21, rf
& ~RT3070_RX_LO2
);
2818 rt5390_rf_init(struct rt2860_softc
*sc
)
2823 rf
= rt3090_rf_read(sc
, 2);
2824 /* Toggle RF R2 bit 7. */
2825 rt3090_rf_write(sc
, 2, rf
| RT3593_RESCAL
);
2827 rt3090_rf_write(sc
, 2, rf
& ~RT3593_RESCAL
);
2829 /* Initialize RF registers to default value. */
2830 if (sc
->mac_ver
== 0x5392) {
2831 for (i
= 0; i
< nitems(rt5392_def_rf
); i
++) {
2832 rt3090_rf_write(sc
, rt5392_def_rf
[i
].reg
,
2833 rt5392_def_rf
[i
].val
);
2836 for (i
= 0; i
< nitems(rt5390_def_rf
); i
++) {
2837 rt3090_rf_write(sc
, rt5390_def_rf
[i
].reg
,
2838 rt5390_def_rf
[i
].val
);
2842 sc
->rf24_20mhz
= 0x1f;
2843 sc
->rf24_40mhz
= 0x2f;
2845 if (sc
->mac_rev
< 0x0211)
2846 rt3090_rf_write(sc
, 27, 0x03);
2848 /* Set led open drain enable. */
2849 RAL_WRITE(sc
, RT3070_OPT_14
, RAL_READ(sc
, RT3070_OPT_14
) | 1);
2851 RAL_WRITE(sc
, RT2860_TX_SW_CFG1
, 0);
2852 RAL_WRITE(sc
, RT2860_TX_SW_CFG2
, 0);
2854 if (sc
->mac_ver
== 0x5390)
2855 rt3090_set_rx_antenna(sc
, 0);
2857 /* Patch RSSI inaccurate issue. */
2858 rt2860_mcu_bbp_write(sc
, 79, 0x13);
2859 rt2860_mcu_bbp_write(sc
, 80, 0x05);
2860 rt2860_mcu_bbp_write(sc
, 81, 0x33);
2862 /* Enable DC filter. */
2863 if (sc
->mac_rev
>= 0x0211)
2864 rt2860_mcu_bbp_write(sc
, 103, 0xc0);
2866 bbp
= rt2860_mcu_bbp_read(sc
, 138);
2867 if (sc
->ntxchains
== 1)
2868 bbp
|= 0x20; /* Turn off DAC1. */
2869 if (sc
->nrxchains
== 1)
2870 bbp
&= ~0x02; /* Turn off ADC1. */
2871 rt2860_mcu_bbp_write(sc
, 138, bbp
);
2873 /* Enable RX LO1 and LO2. */
2874 rt3090_rf_write(sc
, 38, rt3090_rf_read(sc
, 38) & ~RT5390_RX_LO1
);
2875 rt3090_rf_write(sc
, 39, rt3090_rf_read(sc
, 39) & ~RT5390_RX_LO2
);
2877 /* Avoid data lost and CRC error. */
2878 rt2860_mcu_bbp_write(sc
, 4,
2879 rt2860_mcu_bbp_read(sc
, 4) | RT5390_MAC_IF_CTRL
);
2881 rf
= rt3090_rf_read(sc
, 30);
2882 rf
= (rf
& ~0x18) | 0x10;
2883 rt3090_rf_write(sc
, 30, rf
);
2887 rt3090_rf_wakeup(struct rt2860_softc
*sc
)
2892 if (sc
->mac_ver
== 0x3593) {
2894 rf
= rt3090_rf_read(sc
, 1);
2895 rt3090_rf_write(sc
, 1, rf
| RT3593_VCO
);
2897 /* initiate VCO calibration */
2898 rf
= rt3090_rf_read(sc
, 3);
2899 rt3090_rf_write(sc
, 3, rf
| RT3593_VCOCAL
);
2901 /* enable VCO bias current control */
2902 rf
= rt3090_rf_read(sc
, 6);
2903 rt3090_rf_write(sc
, 6, rf
| RT3593_VCO_IC
);
2905 /* initiate res calibration */
2906 rf
= rt3090_rf_read(sc
, 2);
2907 rt3090_rf_write(sc
, 2, rf
| RT3593_RESCAL
);
2909 /* set reference current control to 0.33 mA */
2910 rf
= rt3090_rf_read(sc
, 22);
2911 rf
&= ~RT3593_CP_IC_MASK
;
2912 rf
|= 1 << RT3593_CP_IC_SHIFT
;
2913 rt3090_rf_write(sc
, 22, rf
);
2916 rf
= rt3090_rf_read(sc
, 46);
2917 rt3090_rf_write(sc
, 46, rf
| RT3593_RX_CTB
);
2919 rf
= rt3090_rf_read(sc
, 20);
2920 rf
&= ~(RT3593_LDO_RF_VC_MASK
| RT3593_LDO_PLL_VC_MASK
);
2921 rt3090_rf_write(sc
, 20, rf
);
2923 /* enable RF block */
2924 rf
= rt3090_rf_read(sc
, 1);
2925 rt3090_rf_write(sc
, 1, rf
| RT3070_RF_BLOCK
);
2927 /* enable VCO bias current control */
2928 rf
= rt3090_rf_read(sc
, 7);
2929 rt3090_rf_write(sc
, 7, rf
| 0x30);
2931 rf
= rt3090_rf_read(sc
, 9);
2932 rt3090_rf_write(sc
, 9, rf
| 0x0e);
2935 rf
= rt3090_rf_read(sc
, 21);
2936 rt3090_rf_write(sc
, 21, rf
| RT3070_RX_CTB
);
2938 /* fix Tx to Rx IQ glitch by raising RF voltage */
2939 rf
= rt3090_rf_read(sc
, 27);
2941 if (sc
->mac_rev
< 0x0211)
2943 rt3090_rf_write(sc
, 27, rf
);
2945 if (sc
->patch_dac
&& sc
->mac_rev
< 0x0211) {
2946 tmp
= RAL_READ(sc
, RT3070_LDO_CFG0
);
2947 tmp
= (tmp
& ~0x1f000000) | 0x0d000000;
2948 RAL_WRITE(sc
, RT3070_LDO_CFG0
, tmp
);
2953 rt5390_rf_wakeup(struct rt2860_softc
*sc
)
2958 rf
= rt3090_rf_read(sc
, 1);
2959 rf
|= RT3070_RF_BLOCK
| RT3070_PLL_PD
| RT3070_RX0_PD
|
2961 if (sc
->mac_ver
== 0x5392)
2962 rf
|= RT3070_RX1_PD
| RT3070_TX1_PD
;
2963 rt3090_rf_write(sc
, 1, rf
);
2965 rf
= rt3090_rf_read(sc
, 6);
2966 rf
|= RT3593_VCO_IC
| RT3593_VCOCAL
;
2967 if (sc
->mac_ver
== 0x5390)
2968 rf
&= ~RT3593_VCO_IC
;
2969 rt3090_rf_write(sc
, 6, rf
);
2971 rt3090_rf_write(sc
, 2, rt3090_rf_read(sc
, 2) | RT3593_RESCAL
);
2973 rf
= rt3090_rf_read(sc
, 22);
2974 rf
= (rf
& ~0xe0) | 0x20;
2975 rt3090_rf_write(sc
, 22, rf
);
2977 rt3090_rf_write(sc
, 42, rt3090_rf_read(sc
, 42) | RT5390_RX_CTB
);
2978 rt3090_rf_write(sc
, 20, rt3090_rf_read(sc
, 20) & ~0x77);
2979 rt3090_rf_write(sc
, 3, rt3090_rf_read(sc
, 3) | RT3593_VCOCAL
);
2981 if (sc
->patch_dac
&& sc
->mac_rev
< 0x0211) {
2982 tmp
= RAL_READ(sc
, RT3070_LDO_CFG0
);
2983 tmp
= (tmp
& ~0x1f000000) | 0x0d000000;
2984 RAL_WRITE(sc
, RT3070_LDO_CFG0
, tmp
);
2989 rt3090_filter_calib(struct rt2860_softc
*sc
, uint8_t init
, uint8_t target
,
2993 uint8_t bbp55_pb
, bbp55_sb
, delta
;
2996 /* program filter */
2997 rf24
= rt3090_rf_read(sc
, 24);
2998 rf24
= (rf24
& 0xc0) | init
; /* initial filter value */
2999 rt3090_rf_write(sc
, 24, rf24
);
3001 /* enable baseband loopback mode */
3002 rf22
= rt3090_rf_read(sc
, 22);
3003 rt3090_rf_write(sc
, 22, rf22
| RT3070_BB_LOOPBACK
);
3005 /* set power and frequency of passband test tone */
3006 rt2860_mcu_bbp_write(sc
, 24, 0x00);
3007 for (ntries
= 0; ntries
< 100; ntries
++) {
3008 /* transmit test tone */
3009 rt2860_mcu_bbp_write(sc
, 25, 0x90);
3011 /* read received power */
3012 bbp55_pb
= rt2860_mcu_bbp_read(sc
, 55);
3019 /* set power and frequency of stopband test tone */
3020 rt2860_mcu_bbp_write(sc
, 24, 0x06);
3021 for (ntries
= 0; ntries
< 100; ntries
++) {
3022 /* transmit test tone */
3023 rt2860_mcu_bbp_write(sc
, 25, 0x90);
3025 /* read received power */
3026 bbp55_sb
= rt2860_mcu_bbp_read(sc
, 55);
3028 delta
= bbp55_pb
- bbp55_sb
;
3032 /* reprogram filter */
3034 rt3090_rf_write(sc
, 24, rf24
);
3038 rf24
--; /* backtrack */
3040 rt3090_rf_write(sc
, 24, rf24
);
3043 /* restore initial state */
3044 rt2860_mcu_bbp_write(sc
, 24, 0x00);
3046 /* disable baseband loopback mode */
3047 rf22
= rt3090_rf_read(sc
, 22);
3048 rt3090_rf_write(sc
, 22, rf22
& ~RT3070_BB_LOOPBACK
);
3054 rt3090_rf_setup(struct rt2860_softc
*sc
)
3059 if (sc
->mac_rev
>= 0x0211) {
3060 /* enable DC filter */
3061 rt2860_mcu_bbp_write(sc
, 103, 0xc0);
3063 /* improve power consumption */
3064 bbp
= rt2860_mcu_bbp_read(sc
, 31);
3065 rt2860_mcu_bbp_write(sc
, 31, bbp
& ~0x03);
3068 RAL_WRITE(sc
, RT2860_TX_SW_CFG1
, 0);
3069 if (sc
->mac_rev
< 0x0211) {
3070 RAL_WRITE(sc
, RT2860_TX_SW_CFG2
,
3071 sc
->patch_dac
? 0x2c : 0x0f);
3073 RAL_WRITE(sc
, RT2860_TX_SW_CFG2
, 0);
3075 /* initialize RF registers from ROM */
3076 if (sc
->mac_ver
< 0x5390) {
3077 for (i
= 0; i
< 10; i
++) {
3078 if (sc
->rf
[i
].reg
== 0 || sc
->rf
[i
].reg
== 0xff)
3080 rt3090_rf_write(sc
, sc
->rf
[i
].reg
, sc
->rf
[i
].val
);
3086 rt2860_set_leds(struct rt2860_softc
*sc
, uint16_t which
)
3088 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_LEDS
,
3089 which
| (sc
->leds
& 0x7f), 0);
3093 * Hardware has a general-purpose programmable timer interrupt that can
3094 * periodically raise MAC_INT_4.
3097 rt2860_set_gp_timer(struct rt2860_softc
*sc
, int ms
)
3101 /* disable GP timer before reprogramming it */
3102 tmp
= RAL_READ(sc
, RT2860_INT_TIMER_EN
);
3103 RAL_WRITE(sc
, RT2860_INT_TIMER_EN
, tmp
& ~RT2860_GP_TIMER_EN
);
3108 tmp
= RAL_READ(sc
, RT2860_INT_TIMER_CFG
);
3109 ms
*= 16; /* Unit: 64us */
3110 tmp
= (tmp
& 0xffff) | ms
<< RT2860_GP_TIMER_SHIFT
;
3111 RAL_WRITE(sc
, RT2860_INT_TIMER_CFG
, tmp
);
3113 /* enable GP timer */
3114 tmp
= RAL_READ(sc
, RT2860_INT_TIMER_EN
);
3115 RAL_WRITE(sc
, RT2860_INT_TIMER_EN
, tmp
| RT2860_GP_TIMER_EN
);
3119 rt2860_set_bssid(struct rt2860_softc
*sc
, const uint8_t *bssid
)
3121 RAL_WRITE(sc
, RT2860_MAC_BSSID_DW0
,
3122 bssid
[0] | bssid
[1] << 8 | bssid
[2] << 16 | bssid
[3] << 24);
3123 RAL_WRITE(sc
, RT2860_MAC_BSSID_DW1
,
3124 bssid
[4] | bssid
[5] << 8);
3128 rt2860_set_macaddr(struct rt2860_softc
*sc
, const uint8_t *addr
)
3130 RAL_WRITE(sc
, RT2860_MAC_ADDR_DW0
,
3131 addr
[0] | addr
[1] << 8 | addr
[2] << 16 | addr
[3] << 24);
3132 RAL_WRITE(sc
, RT2860_MAC_ADDR_DW1
,
3133 addr
[4] | addr
[5] << 8 | 0xff << 16);
3137 rt2860_updateslot(struct ieee80211com
*ic
)
3139 struct rt2860_softc
*sc
= ic
->ic_softc
;
3142 tmp
= RAL_READ(sc
, RT2860_BKOFF_SLOT_CFG
);
3144 tmp
|= IEEE80211_GET_SLOTTIME(ic
);
3145 RAL_WRITE(sc
, RT2860_BKOFF_SLOT_CFG
, tmp
);
3149 rt2860_updateprot(struct rt2860_softc
*sc
)
3151 struct ieee80211com
*ic
= &sc
->sc_ic
;
3154 tmp
= RT2860_RTSTH_EN
| RT2860_PROT_NAV_SHORT
| RT2860_TXOP_ALLOW_ALL
;
3155 /* setup protection frame rate (MCS code) */
3156 tmp
|= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ?
3157 rt2860_rates
[RT2860_RIDX_OFDM6
].mcs
:
3158 rt2860_rates
[RT2860_RIDX_CCK11
].mcs
;
3160 /* CCK frames don't require protection */
3161 RAL_WRITE(sc
, RT2860_CCK_PROT_CFG
, tmp
);
3163 if (ic
->ic_flags
& IEEE80211_F_USEPROT
) {
3164 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3165 tmp
|= RT2860_PROT_CTRL_RTS_CTS
;
3166 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3167 tmp
|= RT2860_PROT_CTRL_CTS
;
3169 RAL_WRITE(sc
, RT2860_OFDM_PROT_CFG
, tmp
);
3173 rt2860_update_promisc(struct ieee80211com
*ic
)
3175 struct rt2860_softc
*sc
= ic
->ic_softc
;
3178 tmp
= RAL_READ(sc
, RT2860_RX_FILTR_CFG
);
3179 tmp
&= ~RT2860_DROP_NOT_MYBSS
;
3180 if (ic
->ic_promisc
== 0)
3181 tmp
|= RT2860_DROP_NOT_MYBSS
;
3182 RAL_WRITE(sc
, RT2860_RX_FILTR_CFG
, tmp
);
3186 rt2860_updateedca(struct ieee80211com
*ic
)
3188 struct rt2860_softc
*sc
= ic
->ic_softc
;
3189 const struct wmeParams
*wmep
;
3192 wmep
= ic
->ic_wme
.wme_chanParams
.cap_wmeParams
;
3194 /* update MAC TX configuration registers */
3195 for (aci
= 0; aci
< WME_NUM_AC
; aci
++) {
3196 RAL_WRITE(sc
, RT2860_EDCA_AC_CFG(aci
),
3197 wmep
[aci
].wmep_logcwmax
<< 16 |
3198 wmep
[aci
].wmep_logcwmin
<< 12 |
3199 wmep
[aci
].wmep_aifsn
<< 8 |
3200 wmep
[aci
].wmep_txopLimit
);
3203 /* update SCH/DMA registers too */
3204 RAL_WRITE(sc
, RT2860_WMM_AIFSN_CFG
,
3205 wmep
[WME_AC_VO
].wmep_aifsn
<< 12 |
3206 wmep
[WME_AC_VI
].wmep_aifsn
<< 8 |
3207 wmep
[WME_AC_BK
].wmep_aifsn
<< 4 |
3208 wmep
[WME_AC_BE
].wmep_aifsn
);
3209 RAL_WRITE(sc
, RT2860_WMM_CWMIN_CFG
,
3210 wmep
[WME_AC_VO
].wmep_logcwmin
<< 12 |
3211 wmep
[WME_AC_VI
].wmep_logcwmin
<< 8 |
3212 wmep
[WME_AC_BK
].wmep_logcwmin
<< 4 |
3213 wmep
[WME_AC_BE
].wmep_logcwmin
);
3214 RAL_WRITE(sc
, RT2860_WMM_CWMAX_CFG
,
3215 wmep
[WME_AC_VO
].wmep_logcwmax
<< 12 |
3216 wmep
[WME_AC_VI
].wmep_logcwmax
<< 8 |
3217 wmep
[WME_AC_BK
].wmep_logcwmax
<< 4 |
3218 wmep
[WME_AC_BE
].wmep_logcwmax
);
3219 RAL_WRITE(sc
, RT2860_WMM_TXOP0_CFG
,
3220 wmep
[WME_AC_BK
].wmep_txopLimit
<< 16 |
3221 wmep
[WME_AC_BE
].wmep_txopLimit
);
3222 RAL_WRITE(sc
, RT2860_WMM_TXOP1_CFG
,
3223 wmep
[WME_AC_VO
].wmep_txopLimit
<< 16 |
3224 wmep
[WME_AC_VI
].wmep_txopLimit
);
3231 rt2860_set_key(struct ieee80211com
*ic
, struct ieee80211_node
*ni
,
3232 struct ieee80211_key
*k
)
3234 struct rt2860_softc
*sc
= ic
->ic_softc
;
3237 uint8_t mode
, wcid
, iv
[8];
3239 /* defer setting of WEP keys until interface is brought up */
3240 if ((ic
->ic_if
.if_flags
& (IFF_UP
| IFF_RUNNING
)) !=
3241 (IFF_UP
| IFF_RUNNING
))
3244 /* map net80211 cipher to RT2860 security mode */
3245 switch (k
->k_cipher
) {
3246 case IEEE80211_CIPHER_WEP40
:
3247 mode
= RT2860_MODE_WEP40
;
3249 case IEEE80211_CIPHER_WEP104
:
3250 mode
= RT2860_MODE_WEP104
;
3252 case IEEE80211_CIPHER_TKIP
:
3253 mode
= RT2860_MODE_TKIP
;
3255 case IEEE80211_CIPHER_CCMP
:
3256 mode
= RT2860_MODE_AES_CCMP
;
3262 if (k
->k_flags
& IEEE80211_KEY_GROUP
) {
3263 wcid
= 0; /* NB: update WCID0 for group keys */
3264 base
= RT2860_SKEY(0, k
->k_id
);
3266 wcid
= ((struct rt2860_node
*)ni
)->wcid
;
3267 base
= RT2860_PKEY(wcid
);
3270 if (k
->k_cipher
== IEEE80211_CIPHER_TKIP
) {
3271 RAL_WRITE_REGION_1(sc
, base
, k
->k_key
, 16);
3272 #ifndef IEEE80211_STA_ONLY
3273 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
) {
3274 RAL_WRITE_REGION_1(sc
, base
+ 16, &k
->k_key
[16], 8);
3275 RAL_WRITE_REGION_1(sc
, base
+ 24, &k
->k_key
[24], 8);
3279 RAL_WRITE_REGION_1(sc
, base
+ 16, &k
->k_key
[24], 8);
3280 RAL_WRITE_REGION_1(sc
, base
+ 24, &k
->k_key
[16], 8);
3283 RAL_WRITE_REGION_1(sc
, base
, k
->k_key
, k
->k_len
);
3285 if (!(k
->k_flags
& IEEE80211_KEY_GROUP
) ||
3286 (k
->k_flags
& IEEE80211_KEY_TX
)) {
3287 /* set initial packet number in IV+EIV */
3288 if (k
->k_cipher
== IEEE80211_CIPHER_WEP40
||
3289 k
->k_cipher
== IEEE80211_CIPHER_WEP104
) {
3290 uint32_t val
= arc4random();
3291 /* skip weak IVs from Fluhrer/Mantin/Shamir */
3292 if (val
>= 0x03ff00 && (val
& 0xf8ff00) == 0x00ff00)
3297 iv
[3] = k
->k_id
<< 6;
3298 iv
[4] = iv
[5] = iv
[6] = iv
[7] = 0;
3300 if (k
->k_cipher
== IEEE80211_CIPHER_TKIP
) {
3301 iv
[0] = k
->k_tsc
>> 8;
3302 iv
[1] = (iv
[0] | 0x20) & 0x7f;
3306 iv
[1] = k
->k_tsc
>> 8;
3309 iv
[3] = k
->k_id
<< 6 | IEEE80211_WEP_EXTIV
;
3310 iv
[4] = k
->k_tsc
>> 16;
3311 iv
[5] = k
->k_tsc
>> 24;
3312 iv
[6] = k
->k_tsc
>> 32;
3313 iv
[7] = k
->k_tsc
>> 40;
3315 RAL_WRITE_REGION_1(sc
, RT2860_IVEIV(wcid
), iv
, 8);
3318 if (k
->k_flags
& IEEE80211_KEY_GROUP
) {
3319 /* install group key */
3320 attr
= RAL_READ(sc
, RT2860_SKEY_MODE_0_7
);
3321 attr
&= ~(0xf << (k
->k_id
* 4));
3322 attr
|= mode
<< (k
->k_id
* 4);
3323 RAL_WRITE(sc
, RT2860_SKEY_MODE_0_7
, attr
);
3325 /* install pairwise key */
3326 attr
= RAL_READ(sc
, RT2860_WCID_ATTR(wcid
));
3327 attr
= (attr
& ~0xf) | (mode
<< 1) | RT2860_RX_PKEY_EN
;
3328 RAL_WRITE(sc
, RT2860_WCID_ATTR(wcid
), attr
);
3334 rt2860_delete_key(struct ieee80211com
*ic
, struct ieee80211_node
*ni
,
3335 struct ieee80211_key
*k
)
3337 struct rt2860_softc
*sc
= ic
->ic_softc
;
3341 if (k
->k_flags
& IEEE80211_KEY_GROUP
) {
3342 /* remove group key */
3343 attr
= RAL_READ(sc
, RT2860_SKEY_MODE_0_7
);
3344 attr
&= ~(0xf << (k
->k_id
* 4));
3345 RAL_WRITE(sc
, RT2860_SKEY_MODE_0_7
, attr
);
3348 /* remove pairwise key */
3349 wcid
= ((struct rt2860_node
*)ni
)->wcid
;
3350 attr
= RAL_READ(sc
, RT2860_WCID_ATTR(wcid
));
3352 RAL_WRITE(sc
, RT2860_WCID_ATTR(wcid
), attr
);
3358 rt2860_rssi2dbm(struct rt2860_softc
*sc
, uint8_t rssi
, uint8_t rxchain
)
3360 struct ieee80211com
*ic
= &sc
->sc_ic
;
3361 struct ieee80211_channel
*c
= ic
->ic_curchan
;
3364 if (IEEE80211_IS_CHAN_5GHZ(c
)) {
3365 u_int chan
= ieee80211_chan2ieee(ic
, c
);
3366 delta
= sc
->rssi_5ghz
[rxchain
];
3368 /* determine channel group */
3370 delta
-= sc
->lna
[1];
3371 else if (chan
<= 128)
3372 delta
-= sc
->lna
[2];
3374 delta
-= sc
->lna
[3];
3376 delta
= sc
->rssi_2ghz
[rxchain
] - sc
->lna
[0];
3378 return -12 - delta
- rssi
;
3382 * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
3383 * Used to adjust per-rate Tx power registers.
3385 static __inline
uint32_t
3386 b4inc(uint32_t b32
, int8_t delta
)
3390 for (i
= 0; i
< 8; i
++) {
3397 b32
= b32
>> 4 | b4
<< 28;
3403 rt2860_get_rf(uint16_t rev
)
3406 case RT2860_RF_2820
: return "RT2820";
3407 case RT2860_RF_2850
: return "RT2850";
3408 case RT2860_RF_2720
: return "RT2720";
3409 case RT2860_RF_2750
: return "RT2750";
3410 case RT3070_RF_3020
: return "RT3020";
3411 case RT3070_RF_2020
: return "RT2020";
3412 case RT3070_RF_3021
: return "RT3021";
3413 case RT3070_RF_3022
: return "RT3022";
3414 case RT3070_RF_3052
: return "RT3052";
3415 case RT3070_RF_3320
: return "RT3320";
3416 case RT3070_RF_3053
: return "RT3053";
3417 case RT5390_RF_5360
: return "RT5360";
3418 case RT5390_RF_5390
: return "RT5390";
3419 default: return "unknown";
3424 rt2860_read_eeprom(struct rt2860_softc
*sc
, uint8_t macaddr
[IEEE80211_ADDR_LEN
])
3426 int8_t delta_2ghz
, delta_5ghz
;
3431 /* check whether the ROM is eFUSE ROM or EEPROM */
3432 sc
->sc_srom_read
= rt2860_eeprom_read_2
;
3433 if (sc
->mac_ver
>= 0x3071) {
3434 tmp
= RAL_READ(sc
, RT3070_EFUSE_CTRL
);
3435 DPRINTF(("EFUSE_CTRL=0x%08x\n", tmp
));
3436 if (tmp
& RT3070_SEL_EFUSE
)
3437 sc
->sc_srom_read
= rt3090_efuse_read_2
;
3441 /* read EEPROM version */
3442 val
= rt2860_srom_read(sc
, RT2860_EEPROM_VERSION
);
3443 DPRINTF(("EEPROM rev=%d, FAE=%d\n", val
>> 8, val
& 0xff));
3446 /* read MAC address */
3447 val
= rt2860_srom_read(sc
, RT2860_EEPROM_MAC01
);
3448 macaddr
[0] = val
& 0xff;
3449 macaddr
[1] = val
>> 8;
3450 val
= rt2860_srom_read(sc
, RT2860_EEPROM_MAC23
);
3451 macaddr
[2] = val
& 0xff;
3452 macaddr
[3] = val
>> 8;
3453 val
= rt2860_srom_read(sc
, RT2860_EEPROM_MAC45
);
3454 macaddr
[4] = val
& 0xff;
3455 macaddr
[5] = val
>> 8;
3458 /* read country code */
3459 val
= rt2860_srom_read(sc
, RT2860_EEPROM_COUNTRY
);
3460 DPRINTF(("EEPROM region code=0x%04x\n", val
));
3463 /* read vendor BBP settings */
3464 for (i
= 0; i
< 8; i
++) {
3465 val
= rt2860_srom_read(sc
, RT2860_EEPROM_BBP_BASE
+ i
);
3466 sc
->bbp
[i
].val
= val
& 0xff;
3467 sc
->bbp
[i
].reg
= val
>> 8;
3468 DPRINTF(("BBP%d=0x%02x\n", sc
->bbp
[i
].reg
, sc
->bbp
[i
].val
));
3470 if (sc
->mac_ver
>= 0x3071) {
3471 /* read vendor RF settings */
3472 for (i
= 0; i
< 10; i
++) {
3473 val
= rt2860_srom_read(sc
, RT3071_EEPROM_RF_BASE
+ i
);
3474 sc
->rf
[i
].val
= val
& 0xff;
3475 sc
->rf
[i
].reg
= val
>> 8;
3476 DPRINTF(("RF%d=0x%02x\n", sc
->rf
[i
].reg
,
3481 /* read RF frequency offset from EEPROM */
3482 val
= rt2860_srom_read(sc
, RT2860_EEPROM_FREQ_LEDS
);
3483 sc
->freq
= ((val
& 0xff) != 0xff) ? val
& 0xff : 0;
3484 DPRINTF(("EEPROM freq offset %d\n", sc
->freq
& 0xff));
3485 if ((val
>> 8) != 0xff) {
3486 /* read LEDs operating mode */
3487 sc
->leds
= val
>> 8;
3488 sc
->led
[0] = rt2860_srom_read(sc
, RT2860_EEPROM_LED1
);
3489 sc
->led
[1] = rt2860_srom_read(sc
, RT2860_EEPROM_LED2
);
3490 sc
->led
[2] = rt2860_srom_read(sc
, RT2860_EEPROM_LED3
);
3492 /* broken EEPROM, use default settings */
3494 sc
->led
[0] = 0x5555;
3495 sc
->led
[1] = 0x2221;
3496 sc
->led
[2] = 0xa9f8;
3498 DPRINTF(("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
3499 sc
->leds
, sc
->led
[0], sc
->led
[1], sc
->led
[2]));
3501 /* read RF information */
3502 val
= rt2860_srom_read(sc
, RT2860_EEPROM_ANTENNA
);
3503 if (sc
->mac_ver
>= 0x5390)
3504 sc
->rf_rev
= rt2860_srom_read(sc
, RT2860_EEPROM_CHIPID
);
3506 sc
->rf_rev
= (val
>> 8) & 0xf;
3507 sc
->ntxchains
= (val
>> 4) & 0xf;
3508 sc
->nrxchains
= val
& 0xf;
3509 DPRINTF(("EEPROM RF rev=0x%02x chains=%dT%dR\n",
3510 sc
->rf_rev
, sc
->ntxchains
, sc
->nrxchains
));
3512 /* check if RF supports automatic Tx access gain control */
3513 val
= rt2860_srom_read(sc
, RT2860_EEPROM_CONFIG
);
3514 DPRINTF(("EEPROM CFG 0x%04x\n", val
));
3515 /* check if driver should patch the DAC issue */
3516 if ((val
>> 8) != 0xff)
3517 sc
->patch_dac
= (val
>> 15) & 1;
3518 if ((val
& 0xff) != 0xff) {
3519 sc
->ext_5ghz_lna
= (val
>> 3) & 1;
3520 sc
->ext_2ghz_lna
= (val
>> 2) & 1;
3521 /* check if RF supports automatic Tx access gain control */
3522 sc
->calib_2ghz
= sc
->calib_5ghz
= 0; /* XXX (val >> 1) & 1 */
3523 /* check if we have a hardware radio switch */
3524 sc
->rfswitch
= val
& 1;
3526 if (sc
->sc_flags
& RT2860_ADVANCED_PS
) {
3527 /* read PCIe power save level */
3528 val
= rt2860_srom_read(sc
, RT2860_EEPROM_PCIE_PSLEVEL
);
3529 if ((val
& 0xff) != 0xff) {
3530 sc
->pslevel
= val
& 0x3;
3531 val
= rt2860_srom_read(sc
, RT2860_EEPROM_REV
);
3532 if ((val
& 0xff80) != 0x9280)
3533 sc
->pslevel
= MIN(sc
->pslevel
, 1);
3534 DPRINTF(("EEPROM PCIe PS Level=%d\n", sc
->pslevel
));
3538 /* read power settings for 2GHz channels */
3539 for (i
= 0; i
< 14; i
+= 2) {
3540 val
= rt2860_srom_read(sc
,
3541 RT2860_EEPROM_PWR2GHZ_BASE1
+ i
/ 2);
3542 sc
->txpow1
[i
+ 0] = (int8_t)(val
& 0xff);
3543 sc
->txpow1
[i
+ 1] = (int8_t)(val
>> 8);
3545 if (sc
->mac_ver
!= 0x5390) {
3546 val
= rt2860_srom_read(sc
,
3547 RT2860_EEPROM_PWR2GHZ_BASE2
+ i
/ 2);
3548 sc
->txpow2
[i
+ 0] = (int8_t)(val
& 0xff);
3549 sc
->txpow2
[i
+ 1] = (int8_t)(val
>> 8);
3552 /* fix broken Tx power entries */
3553 for (i
= 0; i
< 14; i
++) {
3554 if (sc
->txpow1
[i
] < 0 ||
3555 sc
->txpow1
[i
] > ((sc
->mac_ver
>= 0x5390) ? 39 : 31))
3557 if (sc
->mac_ver
!= 0x5390) {
3558 if (sc
->txpow2
[i
] < 0 ||
3559 sc
->txpow2
[i
] > ((sc
->mac_ver
== 0x5392) ? 39 : 31))
3562 DPRINTF(("chan %d: power1=%d, power2=%d\n",
3563 rt2860_rf2850
[i
].chan
, sc
->txpow1
[i
], sc
->txpow2
[i
]));
3565 /* read power settings for 5GHz channels */
3566 for (i
= 0; i
< 40; i
+= 2) {
3567 val
= rt2860_srom_read(sc
,
3568 RT2860_EEPROM_PWR5GHZ_BASE1
+ i
/ 2);
3569 sc
->txpow1
[i
+ 14] = (int8_t)(val
& 0xff);
3570 sc
->txpow1
[i
+ 15] = (int8_t)(val
>> 8);
3572 val
= rt2860_srom_read(sc
,
3573 RT2860_EEPROM_PWR5GHZ_BASE2
+ i
/ 2);
3574 sc
->txpow2
[i
+ 14] = (int8_t)(val
& 0xff);
3575 sc
->txpow2
[i
+ 15] = (int8_t)(val
>> 8);
3577 /* fix broken Tx power entries */
3578 for (i
= 0; i
< 40; i
++) {
3579 if (sc
->txpow1
[14 + i
] < -7 || sc
->txpow1
[14 + i
] > 15)
3580 sc
->txpow1
[14 + i
] = 5;
3581 if (sc
->txpow2
[14 + i
] < -7 || sc
->txpow2
[14 + i
] > 15)
3582 sc
->txpow2
[14 + i
] = 5;
3583 DPRINTF(("chan %d: power1=%d, power2=%d\n",
3584 rt2860_rf2850
[14 + i
].chan
, sc
->txpow1
[14 + i
],
3585 sc
->txpow2
[14 + i
]));
3588 /* read Tx power compensation for each Tx rate */
3589 val
= rt2860_srom_read(sc
, RT2860_EEPROM_DELTAPWR
);
3590 delta_2ghz
= delta_5ghz
= 0;
3591 if ((val
& 0xff) != 0xff && (val
& 0x80)) {
3592 delta_2ghz
= val
& 0xf;
3593 if (!(val
& 0x40)) /* negative number */
3594 delta_2ghz
= -delta_2ghz
;
3597 if ((val
& 0xff) != 0xff && (val
& 0x80)) {
3598 delta_5ghz
= val
& 0xf;
3599 if (!(val
& 0x40)) /* negative number */
3600 delta_5ghz
= -delta_5ghz
;
3602 DPRINTF(("power compensation=%d (2GHz), %d (5GHz)\n",
3603 delta_2ghz
, delta_5ghz
));
3605 for (ridx
= 0; ridx
< 5; ridx
++) {
3608 val
= rt2860_srom_read(sc
, RT2860_EEPROM_RPWR
+ ridx
* 2);
3610 val
= rt2860_srom_read(sc
, RT2860_EEPROM_RPWR
+ ridx
* 2 + 1);
3611 reg
|= (uint32_t)val
<< 16;
3613 sc
->txpow20mhz
[ridx
] = reg
;
3614 sc
->txpow40mhz_2ghz
[ridx
] = b4inc(reg
, delta_2ghz
);
3615 sc
->txpow40mhz_5ghz
[ridx
] = b4inc(reg
, delta_5ghz
);
3617 DPRINTF(("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
3618 "40MHz/5GHz=0x%08x\n", ridx
, sc
->txpow20mhz
[ridx
],
3619 sc
->txpow40mhz_2ghz
[ridx
], sc
->txpow40mhz_5ghz
[ridx
]));
3622 /* read factory-calibrated samples for temperature compensation */
3623 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI1_2GHZ
);
3624 sc
->tssi_2ghz
[0] = val
& 0xff; /* [-4] */
3625 sc
->tssi_2ghz
[1] = val
>> 8; /* [-3] */
3626 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI2_2GHZ
);
3627 sc
->tssi_2ghz
[2] = val
& 0xff; /* [-2] */
3628 sc
->tssi_2ghz
[3] = val
>> 8; /* [-1] */
3629 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI3_2GHZ
);
3630 sc
->tssi_2ghz
[4] = val
& 0xff; /* [+0] */
3631 sc
->tssi_2ghz
[5] = val
>> 8; /* [+1] */
3632 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI4_2GHZ
);
3633 sc
->tssi_2ghz
[6] = val
& 0xff; /* [+2] */
3634 sc
->tssi_2ghz
[7] = val
>> 8; /* [+3] */
3635 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI5_2GHZ
);
3636 sc
->tssi_2ghz
[8] = val
& 0xff; /* [+4] */
3637 sc
->step_2ghz
= val
>> 8;
3638 DPRINTF(("TSSI 2GHz: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x "
3639 "0x%02x 0x%02x step=%d\n", sc
->tssi_2ghz
[0], sc
->tssi_2ghz
[1],
3640 sc
->tssi_2ghz
[2], sc
->tssi_2ghz
[3], sc
->tssi_2ghz
[4],
3641 sc
->tssi_2ghz
[5], sc
->tssi_2ghz
[6], sc
->tssi_2ghz
[7],
3642 sc
->tssi_2ghz
[8], sc
->step_2ghz
));
3643 /* check that ref value is correct, otherwise disable calibration */
3644 if (sc
->tssi_2ghz
[4] == 0xff)
3647 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI1_5GHZ
);
3648 sc
->tssi_5ghz
[0] = val
& 0xff; /* [-4] */
3649 sc
->tssi_5ghz
[1] = val
>> 8; /* [-3] */
3650 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI2_5GHZ
);
3651 sc
->tssi_5ghz
[2] = val
& 0xff; /* [-2] */
3652 sc
->tssi_5ghz
[3] = val
>> 8; /* [-1] */
3653 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI3_5GHZ
);
3654 sc
->tssi_5ghz
[4] = val
& 0xff; /* [+0] */
3655 sc
->tssi_5ghz
[5] = val
>> 8; /* [+1] */
3656 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI4_5GHZ
);
3657 sc
->tssi_5ghz
[6] = val
& 0xff; /* [+2] */
3658 sc
->tssi_5ghz
[7] = val
>> 8; /* [+3] */
3659 val
= rt2860_srom_read(sc
, RT2860_EEPROM_TSSI5_5GHZ
);
3660 sc
->tssi_5ghz
[8] = val
& 0xff; /* [+4] */
3661 sc
->step_5ghz
= val
>> 8;
3662 DPRINTF(("TSSI 5GHz: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x "
3663 "0x%02x 0x%02x step=%d\n", sc
->tssi_5ghz
[0], sc
->tssi_5ghz
[1],
3664 sc
->tssi_5ghz
[2], sc
->tssi_5ghz
[3], sc
->tssi_5ghz
[4],
3665 sc
->tssi_5ghz
[5], sc
->tssi_5ghz
[6], sc
->tssi_5ghz
[7],
3666 sc
->tssi_5ghz
[8], sc
->step_5ghz
));
3667 /* check that ref value is correct, otherwise disable calibration */
3668 if (sc
->tssi_5ghz
[4] == 0xff)
3671 /* read RSSI offsets and LNA gains from EEPROM */
3672 val
= rt2860_srom_read(sc
, RT2860_EEPROM_RSSI1_2GHZ
);
3673 sc
->rssi_2ghz
[0] = val
& 0xff; /* Ant A */
3674 sc
->rssi_2ghz
[1] = val
>> 8; /* Ant B */
3675 val
= rt2860_srom_read(sc
, RT2860_EEPROM_RSSI2_2GHZ
);
3676 if (sc
->mac_ver
>= 0x3071) {
3678 * On RT3090 chips (limited to 2 Rx chains), this ROM
3679 * field contains the Tx mixer gain for the 2GHz band.
3681 if ((val
& 0xff) != 0xff)
3682 sc
->txmixgain_2ghz
= val
& 0x7;
3683 DPRINTF(("tx mixer gain=%u (2GHz)\n", sc
->txmixgain_2ghz
));
3685 sc
->rssi_2ghz
[2] = val
& 0xff; /* Ant C */
3686 sc
->lna
[2] = val
>> 8; /* channel group 2 */
3688 val
= rt2860_srom_read(sc
, RT2860_EEPROM_RSSI1_5GHZ
);
3689 sc
->rssi_5ghz
[0] = val
& 0xff; /* Ant A */
3690 sc
->rssi_5ghz
[1] = val
>> 8; /* Ant B */
3691 val
= rt2860_srom_read(sc
, RT2860_EEPROM_RSSI2_5GHZ
);
3692 sc
->rssi_5ghz
[2] = val
& 0xff; /* Ant C */
3693 sc
->lna
[3] = val
>> 8; /* channel group 3 */
3695 val
= rt2860_srom_read(sc
, RT2860_EEPROM_LNA
);
3696 if (sc
->mac_ver
>= 0x3071)
3697 sc
->lna
[0] = RT3090_DEF_LNA
;
3698 else /* channel group 0 */
3699 sc
->lna
[0] = val
& 0xff;
3700 sc
->lna
[1] = val
>> 8; /* channel group 1 */
3702 /* fix broken 5GHz LNA entries */
3703 if (sc
->lna
[2] == 0 || sc
->lna
[2] == 0xff) {
3704 DPRINTF(("invalid LNA for channel group %d\n", 2));
3705 sc
->lna
[2] = sc
->lna
[1];
3707 if (sc
->lna
[3] == 0 || sc
->lna
[3] == 0xff) {
3708 DPRINTF(("invalid LNA for channel group %d\n", 3));
3709 sc
->lna
[3] = sc
->lna
[1];
3712 /* fix broken RSSI offset entries */
3713 for (ant
= 0; ant
< 3; ant
++) {
3714 if (sc
->rssi_2ghz
[ant
] < -10 || sc
->rssi_2ghz
[ant
] > 10) {
3715 DPRINTF(("invalid RSSI%d offset: %d (2GHz)\n",
3716 ant
+ 1, sc
->rssi_2ghz
[ant
]));
3717 sc
->rssi_2ghz
[ant
] = 0;
3719 if (sc
->rssi_5ghz
[ant
] < -10 || sc
->rssi_5ghz
[ant
] > 10) {
3720 DPRINTF(("invalid RSSI%d offset: %d (5GHz)\n",
3721 ant
+ 1, sc
->rssi_5ghz
[ant
]));
3722 sc
->rssi_5ghz
[ant
] = 0;
3730 rt2860_bbp_init(struct rt2860_softc
*sc
)
3734 /* wait for BBP to wake up */
3735 for (ntries
= 0; ntries
< 20; ntries
++) {
3736 uint8_t bbp0
= rt2860_mcu_bbp_read(sc
, 0);
3737 if (bbp0
!= 0 && bbp0
!= 0xff)
3741 device_printf(sc
->sc_dev
,
3742 "timeout waiting for BBP to wake up\n");
3746 /* initialize BBP registers to default values */
3747 if (sc
->mac_ver
>= 0x5390)
3748 rt5390_bbp_init(sc
);
3750 for (i
= 0; i
< nitems(rt2860_def_bbp
); i
++) {
3751 rt2860_mcu_bbp_write(sc
, rt2860_def_bbp
[i
].reg
,
3752 rt2860_def_bbp
[i
].val
);
3756 /* fix BBP84 for RT2860E */
3757 if (sc
->mac_ver
== 0x2860 && sc
->mac_rev
!= 0x0101)
3758 rt2860_mcu_bbp_write(sc
, 84, 0x19);
3760 if (sc
->mac_ver
>= 0x3071) {
3761 rt2860_mcu_bbp_write(sc
, 79, 0x13);
3762 rt2860_mcu_bbp_write(sc
, 80, 0x05);
3763 rt2860_mcu_bbp_write(sc
, 81, 0x33);
3764 } else if (sc
->mac_ver
== 0x2860 && sc
->mac_rev
== 0x0100) {
3765 rt2860_mcu_bbp_write(sc
, 69, 0x16);
3766 rt2860_mcu_bbp_write(sc
, 73, 0x12);
3773 rt5390_bbp_init(struct rt2860_softc
*sc
)
3778 /* Apply maximum likelihood detection for 2 stream case. */
3779 if (sc
->nrxchains
> 1) {
3780 bbp
= rt2860_mcu_bbp_read(sc
, 105);
3781 rt2860_mcu_bbp_write(sc
, 105, bbp
| RT5390_MLD
);
3784 /* Avoid data lost and CRC error. */
3785 bbp
= rt2860_mcu_bbp_read(sc
, 4);
3786 rt2860_mcu_bbp_write(sc
, 4, bbp
| RT5390_MAC_IF_CTRL
);
3788 for (i
= 0; i
< nitems(rt5390_def_bbp
); i
++) {
3789 rt2860_mcu_bbp_write(sc
, rt5390_def_bbp
[i
].reg
,
3790 rt5390_def_bbp
[i
].val
);
3793 if (sc
->mac_ver
== 0x5392) {
3794 rt2860_mcu_bbp_write(sc
, 84, 0x9a);
3795 rt2860_mcu_bbp_write(sc
, 95, 0x9a);
3796 rt2860_mcu_bbp_write(sc
, 98, 0x12);
3797 rt2860_mcu_bbp_write(sc
, 106, 0x05);
3798 rt2860_mcu_bbp_write(sc
, 134, 0xd0);
3799 rt2860_mcu_bbp_write(sc
, 135, 0xf6);
3802 bbp
= rt2860_mcu_bbp_read(sc
, 152);
3803 rt2860_mcu_bbp_write(sc
, 152, bbp
| 0x80);
3805 /* Disable hardware antenna diversity. */
3806 if (sc
->mac_ver
== 0x5390)
3807 rt2860_mcu_bbp_write(sc
, 154, 0);
3811 rt2860_txrx_enable(struct rt2860_softc
*sc
)
3813 struct ieee80211com
*ic
= &sc
->sc_ic
;
3817 /* enable Tx/Rx DMA engine */
3818 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, RT2860_MAC_TX_EN
);
3819 RAL_BARRIER_READ_WRITE(sc
);
3820 for (ntries
= 0; ntries
< 200; ntries
++) {
3821 tmp
= RAL_READ(sc
, RT2860_WPDMA_GLO_CFG
);
3822 if ((tmp
& (RT2860_TX_DMA_BUSY
| RT2860_RX_DMA_BUSY
)) == 0)
3826 if (ntries
== 200) {
3827 device_printf(sc
->sc_dev
, "timeout waiting for DMA engine\n");
3833 tmp
|= RT2860_RX_DMA_EN
| RT2860_TX_DMA_EN
|
3834 RT2860_WPDMA_BT_SIZE64
<< RT2860_WPDMA_BT_SIZE_SHIFT
;
3835 RAL_WRITE(sc
, RT2860_WPDMA_GLO_CFG
, tmp
);
3838 tmp
= RT2860_DROP_CRC_ERR
| RT2860_DROP_PHY_ERR
;
3839 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
3840 tmp
|= RT2860_DROP_UC_NOME
| RT2860_DROP_DUPL
|
3841 RT2860_DROP_CTS
| RT2860_DROP_BA
| RT2860_DROP_ACK
|
3842 RT2860_DROP_VER_ERR
| RT2860_DROP_CTRL_RSV
|
3843 RT2860_DROP_CFACK
| RT2860_DROP_CFEND
;
3844 if (ic
->ic_opmode
== IEEE80211_M_STA
)
3845 tmp
|= RT2860_DROP_RTS
| RT2860_DROP_PSPOLL
;
3847 RAL_WRITE(sc
, RT2860_RX_FILTR_CFG
, tmp
);
3849 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
,
3850 RT2860_MAC_RX_EN
| RT2860_MAC_TX_EN
);
3856 rt2860_init(void *arg
)
3858 struct rt2860_softc
*sc
= arg
;
3859 struct ieee80211com
*ic
= &sc
->sc_ic
;
3862 rt2860_init_locked(sc
);
3865 if (sc
->sc_flags
& RT2860_RUNNING
)
3866 ieee80211_start_all(ic
);
3870 rt2860_init_locked(struct rt2860_softc
*sc
)
3872 struct ieee80211com
*ic
= &sc
->sc_ic
;
3873 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
3876 int i
, qid
, ridx
, ntries
, error
;
3878 RAL_LOCK_ASSERT(sc
);
3881 /* hardware has a radio switch on GPIO pin 2 */
3882 if (!(RAL_READ(sc
, RT2860_GPIO_CTRL
) & (1 << 2))) {
3883 device_printf(sc
->sc_dev
,
3884 "radio is disabled by hardware switch\n");
3886 rt2860_stop_locked(sc
);
3891 RAL_WRITE(sc
, RT2860_PWR_PIN_CFG
, RT2860_IO_RA_PE
);
3894 tmp
= RAL_READ(sc
, RT2860_WPDMA_GLO_CFG
);
3896 RAL_WRITE(sc
, RT2860_WPDMA_GLO_CFG
, tmp
);
3898 /* PBF hardware reset */
3899 RAL_WRITE(sc
, RT2860_SYS_CTRL
, 0xe1f);
3900 RAL_BARRIER_WRITE(sc
);
3901 RAL_WRITE(sc
, RT2860_SYS_CTRL
, 0xe00);
3903 if ((error
= rt2860_load_microcode(sc
)) != 0) {
3904 device_printf(sc
->sc_dev
, "could not load 8051 microcode\n");
3905 rt2860_stop_locked(sc
);
3909 rt2860_set_macaddr(sc
, vap
? vap
->iv_myaddr
: ic
->ic_macaddr
);
3911 /* init Tx power for all Tx rates (from EEPROM) */
3912 for (ridx
= 0; ridx
< 5; ridx
++) {
3913 if (sc
->txpow20mhz
[ridx
] == 0xffffffff)
3915 RAL_WRITE(sc
, RT2860_TX_PWR_CFG(ridx
), sc
->txpow20mhz
[ridx
]);
3918 for (ntries
= 0; ntries
< 100; ntries
++) {
3919 tmp
= RAL_READ(sc
, RT2860_WPDMA_GLO_CFG
);
3920 if ((tmp
& (RT2860_TX_DMA_BUSY
| RT2860_RX_DMA_BUSY
)) == 0)
3924 if (ntries
== 100) {
3925 device_printf(sc
->sc_dev
, "timeout waiting for DMA engine\n");
3926 rt2860_stop_locked(sc
);
3930 RAL_WRITE(sc
, RT2860_WPDMA_GLO_CFG
, tmp
);
3932 /* reset Rx ring and all 6 Tx rings */
3933 RAL_WRITE(sc
, RT2860_WPDMA_RST_IDX
, 0x1003f);
3935 /* PBF hardware reset */
3936 RAL_WRITE(sc
, RT2860_SYS_CTRL
, 0xe1f);
3937 RAL_BARRIER_WRITE(sc
);
3938 RAL_WRITE(sc
, RT2860_SYS_CTRL
, 0xe00);
3940 RAL_WRITE(sc
, RT2860_PWR_PIN_CFG
, RT2860_IO_RA_PE
| RT2860_IO_RF_PE
);
3942 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, RT2860_BBP_HRST
| RT2860_MAC_SRST
);
3943 RAL_BARRIER_WRITE(sc
);
3944 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, 0);
3946 for (i
= 0; i
< nitems(rt2860_def_mac
); i
++)
3947 RAL_WRITE(sc
, rt2860_def_mac
[i
].reg
, rt2860_def_mac
[i
].val
);
3948 if (sc
->mac_ver
>= 0x5390)
3949 RAL_WRITE(sc
, RT2860_TX_SW_CFG0
, 0x00000404);
3950 else if (sc
->mac_ver
>= 0x3071) {
3951 /* set delay of PA_PE assertion to 1us (unit of 0.25us) */
3952 RAL_WRITE(sc
, RT2860_TX_SW_CFG0
,
3953 4 << RT2860_DLY_PAPE_EN_SHIFT
);
3956 if (!(RAL_READ(sc
, RT2860_PCI_CFG
) & RT2860_PCI_CFG_PCI
)) {
3957 sc
->sc_flags
|= RT2860_PCIE
;
3958 /* PCIe has different clock cycle count than PCI */
3959 tmp
= RAL_READ(sc
, RT2860_US_CYC_CNT
);
3960 tmp
= (tmp
& ~0xff) | 0x7d;
3961 RAL_WRITE(sc
, RT2860_US_CYC_CNT
, tmp
);
3964 /* wait while MAC is busy */
3965 for (ntries
= 0; ntries
< 100; ntries
++) {
3966 if (!(RAL_READ(sc
, RT2860_MAC_STATUS_REG
) &
3967 (RT2860_RX_STATUS_BUSY
| RT2860_TX_STATUS_BUSY
)))
3971 if (ntries
== 100) {
3972 device_printf(sc
->sc_dev
, "timeout waiting for MAC\n");
3973 rt2860_stop_locked(sc
);
3977 /* clear Host to MCU mailbox */
3978 RAL_WRITE(sc
, RT2860_H2M_BBPAGENT
, 0);
3979 RAL_WRITE(sc
, RT2860_H2M_MAILBOX
, 0);
3981 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_RFRESET
, 0, 0);
3984 if ((error
= rt2860_bbp_init(sc
)) != 0) {
3985 rt2860_stop_locked(sc
);
3989 /* clear RX WCID search table */
3990 RAL_SET_REGION_4(sc
, RT2860_WCID_ENTRY(0), 0, 512);
3991 /* clear pairwise key table */
3992 RAL_SET_REGION_4(sc
, RT2860_PKEY(0), 0, 2048);
3993 /* clear IV/EIV table */
3994 RAL_SET_REGION_4(sc
, RT2860_IVEIV(0), 0, 512);
3995 /* clear WCID attribute table */
3996 RAL_SET_REGION_4(sc
, RT2860_WCID_ATTR(0), 0, 256);
3997 /* clear shared key table */
3998 RAL_SET_REGION_4(sc
, RT2860_SKEY(0, 0), 0, 8 * 32);
3999 /* clear shared key mode */
4000 RAL_SET_REGION_4(sc
, RT2860_SKEY_MODE_0_7
, 0, 4);
4002 /* init Tx rings (4 EDCAs + HCCA + Mgt) */
4003 for (qid
= 0; qid
< 6; qid
++) {
4004 RAL_WRITE(sc
, RT2860_TX_BASE_PTR(qid
), sc
->txq
[qid
].paddr
);
4005 RAL_WRITE(sc
, RT2860_TX_MAX_CNT(qid
), RT2860_TX_RING_COUNT
);
4006 RAL_WRITE(sc
, RT2860_TX_CTX_IDX(qid
), 0);
4010 RAL_WRITE(sc
, RT2860_RX_BASE_PTR
, sc
->rxq
.paddr
);
4011 RAL_WRITE(sc
, RT2860_RX_MAX_CNT
, RT2860_RX_RING_COUNT
);
4012 RAL_WRITE(sc
, RT2860_RX_CALC_IDX
, RT2860_RX_RING_COUNT
- 1);
4014 /* setup maximum buffer sizes */
4015 RAL_WRITE(sc
, RT2860_MAX_LEN_CFG
, 1 << 12 |
4016 (MCLBYTES
- sizeof (struct rt2860_rxwi
) - 2));
4018 for (ntries
= 0; ntries
< 100; ntries
++) {
4019 tmp
= RAL_READ(sc
, RT2860_WPDMA_GLO_CFG
);
4020 if ((tmp
& (RT2860_TX_DMA_BUSY
| RT2860_RX_DMA_BUSY
)) == 0)
4024 if (ntries
== 100) {
4025 device_printf(sc
->sc_dev
, "timeout waiting for DMA engine\n");
4026 rt2860_stop_locked(sc
);
4030 RAL_WRITE(sc
, RT2860_WPDMA_GLO_CFG
, tmp
);
4032 /* disable interrupts mitigation */
4033 RAL_WRITE(sc
, RT2860_DELAY_INT_CFG
, 0);
4035 /* write vendor-specific BBP values (from EEPROM) */
4036 for (i
= 0; i
< 8; i
++) {
4037 if (sc
->bbp
[i
].reg
== 0 || sc
->bbp
[i
].reg
== 0xff)
4039 rt2860_mcu_bbp_write(sc
, sc
->bbp
[i
].reg
, sc
->bbp
[i
].val
);
4042 /* select Main antenna for 1T1R devices */
4043 if (sc
->rf_rev
== RT3070_RF_2020
||
4044 sc
->rf_rev
== RT3070_RF_3020
||
4045 sc
->rf_rev
== RT3070_RF_3320
||
4046 sc
->mac_ver
== 0x5390)
4047 rt3090_set_rx_antenna(sc
, 0);
4049 /* send LEDs operating mode to microcontroller */
4050 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_LED1
, sc
->led
[0], 0);
4051 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_LED2
, sc
->led
[1], 0);
4052 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_LED3
, sc
->led
[2], 0);
4054 if (sc
->mac_ver
>= 0x5390)
4056 else if (sc
->mac_ver
>= 0x3071) {
4057 if ((error
= rt3090_rf_init(sc
)) != 0) {
4058 rt2860_stop_locked(sc
);
4063 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_SLEEP
, 0x02ff, 1);
4064 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_WAKEUP
, 0, 1);
4066 if (sc
->mac_ver
>= 0x5390)
4067 rt5390_rf_wakeup(sc
);
4068 else if (sc
->mac_ver
>= 0x3071)
4069 rt3090_rf_wakeup(sc
);
4071 /* disable non-existing Rx chains */
4072 bbp3
= rt2860_mcu_bbp_read(sc
, 3);
4073 bbp3
&= ~(1 << 3 | 1 << 4);
4074 if (sc
->nrxchains
== 2)
4076 else if (sc
->nrxchains
== 3)
4078 rt2860_mcu_bbp_write(sc
, 3, bbp3
);
4080 /* disable non-existing Tx chains */
4081 bbp1
= rt2860_mcu_bbp_read(sc
, 1);
4082 if (sc
->ntxchains
== 1)
4083 bbp1
= (bbp1
& ~(1 << 3 | 1 << 4));
4084 else if (sc
->mac_ver
== 0x3593 && sc
->ntxchains
== 2)
4085 bbp1
= (bbp1
& ~(1 << 4)) | 1 << 3;
4086 else if (sc
->mac_ver
== 0x3593 && sc
->ntxchains
== 3)
4087 bbp1
= (bbp1
& ~(1 << 3)) | 1 << 4;
4088 rt2860_mcu_bbp_write(sc
, 1, bbp1
);
4090 if (sc
->mac_ver
>= 0x3071)
4091 rt3090_rf_setup(sc
);
4093 /* select default channel */
4094 rt2860_switch_chan(sc
, ic
->ic_curchan
);
4096 /* reset RF from MCU */
4097 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_RFRESET
, 0, 0);
4099 /* set RTS threshold */
4100 tmp
= RAL_READ(sc
, RT2860_TX_RTS_CFG
);
4102 tmp
|= IEEE80211_RTS_DEFAULT
<< 8;
4103 RAL_WRITE(sc
, RT2860_TX_RTS_CFG
, tmp
);
4105 /* setup initial protection mode */
4106 rt2860_updateprot(sc
);
4108 /* turn radio LED on */
4109 rt2860_set_leds(sc
, RT2860_LED_RADIO
);
4111 /* enable Tx/Rx DMA engine */
4112 if ((error
= rt2860_txrx_enable(sc
)) != 0) {
4113 rt2860_stop_locked(sc
);
4117 /* clear pending interrupts */
4118 RAL_WRITE(sc
, RT2860_INT_STATUS
, 0xffffffff);
4119 /* enable interrupts */
4120 RAL_WRITE(sc
, RT2860_INT_MASK
, 0x3fffc);
4122 if (sc
->sc_flags
& RT2860_ADVANCED_PS
)
4123 rt2860_mcu_cmd(sc
, RT2860_MCU_CMD_PSLEVEL
, sc
->pslevel
, 0);
4125 sc
->sc_flags
|= RT2860_RUNNING
;
4127 callout_reset(&sc
->watchdog_ch
, hz
, rt2860_watchdog
, sc
);
4131 rt2860_stop(void *arg
)
4133 struct rt2860_softc
*sc
= arg
;
4136 rt2860_stop_locked(sc
);
4141 rt2860_stop_locked(struct rt2860_softc
*sc
)
4146 if (sc
->sc_flags
& RT2860_RUNNING
)
4147 rt2860_set_leds(sc
, 0); /* turn all LEDs off */
4149 callout_stop(&sc
->watchdog_ch
);
4150 sc
->sc_tx_timer
= 0;
4151 sc
->sc_flags
&= ~RT2860_RUNNING
;
4153 /* disable interrupts */
4154 RAL_WRITE(sc
, RT2860_INT_MASK
, 0);
4156 /* disable GP timer */
4157 rt2860_set_gp_timer(sc
, 0);
4160 tmp
= RAL_READ(sc
, RT2860_MAC_SYS_CTRL
);
4161 tmp
&= ~(RT2860_MAC_RX_EN
| RT2860_MAC_TX_EN
);
4162 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, tmp
);
4165 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, RT2860_BBP_HRST
| RT2860_MAC_SRST
);
4166 RAL_BARRIER_WRITE(sc
);
4167 RAL_WRITE(sc
, RT2860_MAC_SYS_CTRL
, 0);
4169 /* reset Tx and Rx rings (and reclaim TXWIs) */
4171 for (qid
= 0; qid
< 6; qid
++)
4172 rt2860_reset_tx_ring(sc
, &sc
->txq
[qid
]);
4173 rt2860_reset_rx_ring(sc
, &sc
->rxq
);
4177 rt2860_load_microcode(struct rt2860_softc
*sc
)
4179 const struct firmware
*fp
;
4182 RAL_LOCK_ASSERT(sc
);
4185 fp
= firmware_get("rt2860fw");
4188 device_printf(sc
->sc_dev
,
4189 "unable to receive rt2860fw firmware image\n");
4193 /* set "host program ram write selection" bit */
4194 RAL_WRITE(sc
, RT2860_SYS_CTRL
, RT2860_HST_PM_SEL
);
4195 /* write microcode image */
4196 RAL_WRITE_REGION_1(sc
, RT2860_FW_BASE
, fp
->data
, fp
->datasize
);
4197 /* kick microcontroller unit */
4198 RAL_WRITE(sc
, RT2860_SYS_CTRL
, 0);
4199 RAL_BARRIER_WRITE(sc
);
4200 RAL_WRITE(sc
, RT2860_SYS_CTRL
, RT2860_MCU_RESET
);
4202 RAL_WRITE(sc
, RT2860_H2M_BBPAGENT
, 0);
4203 RAL_WRITE(sc
, RT2860_H2M_MAILBOX
, 0);
4205 /* wait until microcontroller is ready */
4206 RAL_BARRIER_READ_WRITE(sc
);
4207 for (ntries
= 0; ntries
< 1000; ntries
++) {
4208 if (RAL_READ(sc
, RT2860_SYS_CTRL
) & RT2860_MCU_READY
)
4212 if (ntries
== 1000) {
4213 device_printf(sc
->sc_dev
,
4214 "timeout waiting for MCU to initialize\n");
4219 firmware_put(fp
, FIRMWARE_UNLOAD
);
4224 * This function is called periodically to adjust Tx power based on
4225 * temperature variation.
4229 rt2860_calib(struct rt2860_softc
*sc
)
4231 struct ieee80211com
*ic
= &sc
->sc_ic
;
4232 const uint8_t *tssi
;
4233 uint8_t step
, bbp49
;
4236 /* read current temperature */
4237 bbp49
= rt2860_mcu_bbp_read(sc
, 49);
4239 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_bss
->ni_chan
)) {
4240 tssi
= &sc
->tssi_2ghz
[4];
4241 step
= sc
->step_2ghz
;
4243 tssi
= &sc
->tssi_5ghz
[4];
4244 step
= sc
->step_5ghz
;
4247 if (bbp49
< tssi
[0]) { /* lower than reference */
4248 /* use higher Tx power than default */
4249 for (d
= 0; d
> -4 && bbp49
<= tssi
[d
- 1]; d
--);
4250 } else if (bbp49
> tssi
[0]) { /* greater than reference */
4251 /* use lower Tx power than default */
4252 for (d
= 0; d
< +4 && bbp49
>= tssi
[d
+ 1]; d
++);
4254 /* use default Tx power */
4259 DPRINTF(("BBP49=0x%02x, adjusting Tx power by %d\n", bbp49
, d
));
4261 /* write adjusted Tx power values for each Tx rate */
4262 for (ridx
= 0; ridx
< 5; ridx
++) {
4263 if (sc
->txpow20mhz
[ridx
] == 0xffffffff)
4265 RAL_WRITE(sc
, RT2860_TX_PWR_CFG(ridx
),
4266 b4inc(sc
->txpow20mhz
[ridx
], d
));
4272 rt3090_set_rx_antenna(struct rt2860_softc
*sc
, int aux
)
4277 if (sc
->mac_ver
== 0x5390) {
4278 rt2860_mcu_bbp_write(sc
, 152,
4279 rt2860_mcu_bbp_read(sc
, 152) & ~0x80);
4281 tmp
= RAL_READ(sc
, RT2860_PCI_EECTRL
);
4282 RAL_WRITE(sc
, RT2860_PCI_EECTRL
, tmp
& ~RT2860_C
);
4283 tmp
= RAL_READ(sc
, RT2860_GPIO_CTRL
);
4284 RAL_WRITE(sc
, RT2860_GPIO_CTRL
, (tmp
& ~0x0808) | 0x08);
4287 if (sc
->mac_ver
== 0x5390) {
4288 rt2860_mcu_bbp_write(sc
, 152,
4289 rt2860_mcu_bbp_read(sc
, 152) | 0x80);
4291 tmp
= RAL_READ(sc
, RT2860_PCI_EECTRL
);
4292 RAL_WRITE(sc
, RT2860_PCI_EECTRL
, tmp
| RT2860_C
);
4293 tmp
= RAL_READ(sc
, RT2860_GPIO_CTRL
);
4294 RAL_WRITE(sc
, RT2860_GPIO_CTRL
, tmp
& ~0x0808);
4300 rt2860_switch_chan(struct rt2860_softc
*sc
, struct ieee80211_channel
*c
)
4302 struct ieee80211com
*ic
= &sc
->sc_ic
;
4305 chan
= ieee80211_chan2ieee(ic
, c
);
4306 if (chan
== 0 || chan
== IEEE80211_CHAN_ANY
)
4309 if (sc
->mac_ver
>= 0x5390)
4310 rt5390_set_chan(sc
, chan
);
4311 else if (sc
->mac_ver
>= 0x3071)
4312 rt3090_set_chan(sc
, chan
);
4314 rt2860_set_chan(sc
, chan
);
4316 /* determine channel group */
4319 else if (chan
<= 64)
4321 else if (chan
<= 128)
4326 /* XXX necessary only when group has changed! */
4327 if (sc
->mac_ver
< 0x5390)
4328 rt2860_select_chan_group(sc
, group
);
4334 rt2860_setup_beacon(struct rt2860_softc
*sc
, struct ieee80211vap
*vap
)
4336 struct ieee80211com
*ic
= vap
->iv_ic
;
4337 struct rt2860_txwi txwi
;
4341 if ((m
= ieee80211_beacon_alloc(vap
->iv_bss
)) == NULL
)
4344 memset(&txwi
, 0, sizeof txwi
);
4346 txwi
.len
= htole16(m
->m_pkthdr
.len
);
4347 /* send beacons at the lowest available rate */
4348 ridx
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_bsschan
) ?
4349 RT2860_RIDX_OFDM6
: RT2860_RIDX_CCK1
;
4350 txwi
.phy
= htole16(rt2860_rates
[ridx
].mcs
);
4351 if (rt2860_rates
[ridx
].phy
== IEEE80211_T_OFDM
)
4352 txwi
.phy
|= htole16(RT2860_PHY_OFDM
);
4353 txwi
.txop
= RT2860_TX_TXOP_HT
;
4354 txwi
.flags
= RT2860_TX_TS
;
4355 txwi
.xflags
= RT2860_TX_NSEQ
;
4357 RAL_WRITE_REGION_1(sc
, RT2860_BCN_BASE(0),
4358 (uint8_t *)&txwi
, sizeof txwi
);
4359 RAL_WRITE_REGION_1(sc
, RT2860_BCN_BASE(0) + sizeof txwi
,
4360 mtod(m
, uint8_t *), m
->m_pkthdr
.len
);
4368 rt2860_enable_tsf_sync(struct rt2860_softc
*sc
)
4370 struct ieee80211com
*ic
= &sc
->sc_ic
;
4371 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
4374 tmp
= RAL_READ(sc
, RT2860_BCN_TIME_CFG
);
4377 tmp
|= vap
->iv_bss
->ni_intval
* 16;
4378 tmp
|= RT2860_TSF_TIMER_EN
| RT2860_TBTT_TIMER_EN
;
4379 if (vap
->iv_opmode
== IEEE80211_M_STA
) {
4381 * Local TSF is always updated with remote TSF on beacon
4384 tmp
|= 1 << RT2860_TSF_SYNC_MODE_SHIFT
;
4386 else if (vap
->iv_opmode
== IEEE80211_M_IBSS
||
4387 vap
->iv_opmode
== IEEE80211_M_MBSS
) {
4388 tmp
|= RT2860_BCN_TX_EN
;
4390 * Local TSF is updated with remote TSF on beacon reception
4391 * only if the remote TSF is greater than local TSF.
4393 tmp
|= 2 << RT2860_TSF_SYNC_MODE_SHIFT
;
4394 } else if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
) {
4395 tmp
|= RT2860_BCN_TX_EN
;
4396 /* SYNC with nobody */
4397 tmp
|= 3 << RT2860_TSF_SYNC_MODE_SHIFT
;
4400 RAL_WRITE(sc
, RT2860_BCN_TIME_CFG
, tmp
);