2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <proto/802.11.h>
30 #include "wlc_types.h"
38 #include "phy/wlc_phy_hal.h"
39 #include "wlc_channel.h"
40 #include "wlc_bsscfg.h"
41 #include "wlc_mac80211.h"
42 #include "wl_export.h"
46 #define MIN_SPATIAL_EXPANSION 0
47 #define MAX_SPATIAL_EXPANSION 1
49 #define WLC_STF_SS_STBC_RX(wlc) (WLCISNPHY(wlc->band) && \
50 NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
52 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val);
53 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 val);
54 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val);
55 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val);
57 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc);
58 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec);
64 const u8 txcore_default[5] = {
65 (0), /* bitmap of the core enabled */
66 (0x01), /* For Nsts = 1, enable core 1 */
67 (0x03), /* For Nsts = 2, enable core 1 & 2 */
68 (0x07), /* For Nsts = 3, enable core 1, 2 & 3 */
69 (0x0f) /* For Nsts = 4, enable all cores */
72 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val)
74 ASSERT((val == HT_CAP_RX_STBC_NO)
75 || (val == HT_CAP_RX_STBC_ONE_STREAM));
77 /* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
78 if (WLC_STF_SS_STBC_RX(wlc)) {
79 if ((wlc->stf->rxstreams == 1) && (val != HT_CAP_RX_STBC_NO))
83 wlc->ht_cap.cap_info &= ~HT_CAP_RX_STBC_MASK;
84 wlc->ht_cap.cap_info |= (val << HT_CAP_RX_STBC_SHIFT);
87 wlc_update_beacon(wlc);
88 wlc_update_probe_resp(wlc, true);
92 /* every WLC_TEMPSENSE_PERIOD seconds temperature check to decide whether to turn on/off txchain */
93 void wlc_tempsense_upd(struct wlc_info *wlc)
95 wlc_phy_t *pi = wlc->band->pi;
96 uint active_chains, txchain;
98 /* Check if the chip is too hot. Disable one Tx chain, if it is */
99 /* high 4 bits are for Rx chain, low 4 bits are for Tx chain */
100 active_chains = wlc_phy_stf_chain_active_get(pi);
101 txchain = active_chains & 0xf;
103 if (wlc->stf->txchain == wlc->stf->hw_txchain) {
104 if (txchain && (txchain < wlc->stf->hw_txchain)) {
105 /* turn off 1 tx chain */
106 wlc_stf_txchain_set(wlc, txchain, true);
108 } else if (wlc->stf->txchain < wlc->stf->hw_txchain) {
109 if (txchain == wlc->stf->hw_txchain) {
110 /* turn back on txchain */
111 wlc_stf_txchain_set(wlc, txchain, true);
117 wlc_stf_ss_algo_channel_get(struct wlc_info *wlc, u16 *ss_algo_channel,
121 u8 siso_mcs_id, cdd_mcs_id, stbc_mcs_id;
123 /* Clear previous settings */
124 *ss_algo_channel = 0;
127 *ss_algo_channel = (u16) -1;
131 wlc_phy_txpower_get_current(wlc->band->pi, &power,
132 CHSPEC_CHANNEL(chanspec));
134 siso_mcs_id = (CHSPEC_IS40(chanspec)) ?
135 WL_TX_POWER_MCS40_SISO_FIRST : WL_TX_POWER_MCS20_SISO_FIRST;
136 cdd_mcs_id = (CHSPEC_IS40(chanspec)) ?
137 WL_TX_POWER_MCS40_CDD_FIRST : WL_TX_POWER_MCS20_CDD_FIRST;
138 stbc_mcs_id = (CHSPEC_IS40(chanspec)) ?
139 WL_TX_POWER_MCS40_STBC_FIRST : WL_TX_POWER_MCS20_STBC_FIRST;
141 /* criteria to choose stf mode */
143 /* the "+3dbm (12 0.25db units)" is to account for the fact that with CDD, tx occurs
146 if (power.target[siso_mcs_id] > (power.target[cdd_mcs_id] + 12))
147 setbit(ss_algo_channel, PHY_TXC1_MODE_SISO);
149 setbit(ss_algo_channel, PHY_TXC1_MODE_CDD);
151 /* STBC is ORed into to algo channel as STBC requires per-packet SCB capability check
152 * so cannot be default mode of operation. One of SISO, CDD have to be set
154 if (power.target[siso_mcs_id] <= (power.target[stbc_mcs_id] + 12))
155 setbit(ss_algo_channel, PHY_TXC1_MODE_STBC);
158 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val)
160 if ((int_val != AUTO) && (int_val != OFF) && (int_val != ON)) {
164 if ((int_val == ON) && (wlc->stf->txstreams == 1))
167 if ((int_val == OFF) || (wlc->stf->txstreams == 1)
168 || !WLC_STBC_CAP_PHY(wlc))
169 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_TX_STBC;
171 wlc->ht_cap.cap_info |= IEEE80211_HT_CAP_TX_STBC;
173 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = (s8) int_val;
174 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = (s8) int_val;
179 bool wlc_stf_stbc_rx_set(struct wlc_info *wlc, s32 int_val)
181 if ((int_val != HT_CAP_RX_STBC_NO)
182 && (int_val != HT_CAP_RX_STBC_ONE_STREAM)) {
186 if (WLC_STF_SS_STBC_RX(wlc)) {
187 if ((int_val != HT_CAP_RX_STBC_NO)
188 && (wlc->stf->rxstreams == 1))
192 wlc_stf_stbc_rx_ht_update(wlc, int_val);
196 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 core_mask)
198 WL_TRACE("wl%d: %s: Nsts %d core_mask %x\n",
199 wlc->pub->unit, __func__, Nsts, core_mask);
201 ASSERT((Nsts > 0) && (Nsts <= MAX_STREAMS_SUPPORTED));
203 if (WLC_BITSCNT(core_mask) > wlc->stf->txstreams) {
207 if ((WLC_BITSCNT(core_mask) == wlc->stf->txstreams) &&
208 ((core_mask & ~wlc->stf->txchain)
209 || !(core_mask & wlc->stf->txchain))) {
210 core_mask = wlc->stf->txchain;
213 ASSERT(!core_mask || Nsts <= WLC_BITSCNT(core_mask));
215 wlc->stf->txcore[Nsts] = core_mask;
216 /* Nsts = 1..4, txcore index = 1..4 */
218 /* Needs to update beacon and ucode generated response
219 * frames when 1 stream core map changed
221 wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
222 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
224 wlc_suspend_mac_and_wait(wlc);
225 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
233 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val)
238 WL_TRACE("wl%d: %s: val %x\n", wlc->pub->unit, __func__, val);
240 wlc->stf->spatial_policy = (s8) val;
241 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
242 core_mask = (val == MAX_SPATIAL_EXPANSION) ?
243 wlc->stf->txchain : txcore_default[i];
244 wlc_stf_txcore_set(wlc, (u8) i, core_mask);
249 int wlc_stf_txchain_set(struct wlc_info *wlc, s32 int_val, bool force)
251 u8 txchain = (u8) int_val;
255 if (wlc->stf->txchain == txchain)
258 if ((txchain & ~wlc->stf->hw_txchain)
259 || !(txchain & wlc->stf->hw_txchain))
262 /* if nrate override is configured to be non-SISO STF mode, reject reducing txchain to 1 */
263 txstreams = (u8) WLC_BITSCNT(txchain);
264 if (txstreams > MAX_STREAMS_SUPPORTED)
267 if (txstreams == 1) {
268 for (i = 0; i < NBANDS(wlc); i++)
269 if ((RSPEC_STF(wlc->bandstate[i]->rspec_override) !=
271 || (RSPEC_STF(wlc->bandstate[i]->mrspec_override) !=
272 PHY_TXC1_MODE_SISO)) {
276 /* over-write the override rspec */
277 if (RSPEC_STF(wlc->bandstate[i]->rspec_override)
278 != PHY_TXC1_MODE_SISO) {
279 wlc->bandstate[i]->rspec_override = 0;
280 WL_ERROR("%s(): temp sense override non-SISO rspec_override\n",
284 (wlc->bandstate[i]->mrspec_override) !=
285 PHY_TXC1_MODE_SISO) {
286 wlc->bandstate[i]->mrspec_override = 0;
287 WL_ERROR("%s(): temp sense override non-SISO mrspec_override\n",
293 wlc->stf->txchain = txchain;
294 wlc->stf->txstreams = txstreams;
295 wlc_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
296 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
297 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
299 (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
300 _wlc_stf_phy_txant_upd(wlc);
302 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
305 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
306 wlc_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
311 /* update wlc->stf->ss_opmode which represents the operational stf_ss mode we're using */
312 int wlc_stf_ss_update(struct wlc_info *wlc, struct wlcband *band)
318 prev_stf_ss = wlc->stf->ss_opmode;
320 /* NOTE: opmode can only be SISO or CDD as STBC is decided on a per-packet basis */
321 if (WLC_STBC_CAP_PHY(wlc) &&
322 wlc->stf->ss_algosel_auto
323 && (wlc->stf->ss_algo_channel != (u16) -1)) {
324 ASSERT(isset(&wlc->stf->ss_algo_channel, PHY_TXC1_MODE_CDD)
325 || isset(&wlc->stf->ss_algo_channel,
326 PHY_TXC1_MODE_SISO));
327 upd_stf_ss = (wlc->stf->no_cddstbc || (wlc->stf->txstreams == 1)
328 || isset(&wlc->stf->ss_algo_channel,
329 PHY_TXC1_MODE_SISO)) ? PHY_TXC1_MODE_SISO
332 if (wlc->band != band)
334 upd_stf_ss = (wlc->stf->no_cddstbc
335 || (wlc->stf->txstreams ==
336 1)) ? PHY_TXC1_MODE_SISO : band->
339 if (prev_stf_ss != upd_stf_ss) {
340 wlc->stf->ss_opmode = upd_stf_ss;
341 wlc_bmac_band_stf_ss_set(wlc->hw, upd_stf_ss);
347 int wlc_stf_attach(struct wlc_info *wlc)
349 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
350 wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
352 if (WLCISNPHY(wlc->band) &&
353 (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
354 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
356 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
357 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
359 wlc_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
360 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
361 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
363 if (WLC_STBC_CAP_PHY(wlc)) {
364 wlc->stf->ss_algosel_auto = true;
365 wlc->stf->ss_algo_channel = (u16) -1; /* Init the default value */
370 void wlc_stf_detach(struct wlc_info *wlc)
374 int wlc_stf_ant_txant_validate(struct wlc_info *wlc, s8 val)
376 int bcmerror = BCME_OK;
378 /* when there is only 1 tx_streams, don't allow to change the txant */
379 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
380 return ((val == wlc->stf->txant) ? bcmerror : BCME_RANGE);
387 val = ANT_TX_FORCE_0;
390 val = ANT_TX_FORCE_1;
393 val = ANT_TX_LAST_RX;
396 bcmerror = BCME_RANGE;
400 if (bcmerror == BCME_OK)
401 wlc->stf->txant = (s8) val;
408 * Centralized txant update function. call it whenever wlc->stf->txant and/or wlc->stf->txchain
411 * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
412 * achieve various tx/rx antenna selection schemes
414 * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means auto(last rx)
415 * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means last rx and
416 * do tx-antenna selection for SISO transmissions
417 * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7 means last rx and
418 * do tx-antenna selection for SISO transmissions
419 * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7 means both cores active
421 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc)
425 txant = (s8) wlc->stf->txant;
426 ASSERT(txant == ANT_TX_FORCE_0 || txant == ANT_TX_FORCE_1
427 || txant == ANT_TX_LAST_RX);
429 if (WLC_PHY_11N_CAP(wlc->band)) {
430 if (txant == ANT_TX_FORCE_0) {
431 wlc->stf->phytxant = PHY_TXC_ANT_0;
432 } else if (txant == ANT_TX_FORCE_1) {
433 wlc->stf->phytxant = PHY_TXC_ANT_1;
435 if (WLCISNPHY(wlc->band) &&
436 NREV_GE(wlc->band->phyrev, 3)
437 && NREV_LT(wlc->band->phyrev, 7)) {
438 wlc->stf->phytxant = PHY_TXC_ANT_2;
441 if (WLCISLCNPHY(wlc->band) || WLCISSSLPNPHY(wlc->band))
442 wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
444 /* keep this assert to catch out of sync wlc->stf->txcore */
445 ASSERT(wlc->stf->txchain > 0);
447 wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
451 if (txant == ANT_TX_FORCE_0)
452 wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
453 else if (txant == ANT_TX_FORCE_1)
454 wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
456 wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
459 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
462 void wlc_stf_phy_txant_upd(struct wlc_info *wlc)
464 _wlc_stf_phy_txant_upd(wlc);
467 void wlc_stf_phy_chain_calc(struct wlc_info *wlc)
469 /* get available rx/tx chains */
470 wlc->stf->hw_txchain = (u8) getintvar(wlc->pub->vars, "txchain");
471 wlc->stf->hw_rxchain = (u8) getintvar(wlc->pub->vars, "rxchain");
473 /* these parameter are intended to be used for all PHY types */
474 if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
475 if (WLCISNPHY(wlc->band)) {
476 wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
478 wlc->stf->hw_txchain = TXCHAIN_DEF;
482 wlc->stf->txchain = wlc->stf->hw_txchain;
483 wlc->stf->txstreams = (u8) WLC_BITSCNT(wlc->stf->hw_txchain);
485 if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
486 if (WLCISNPHY(wlc->band)) {
487 wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
489 wlc->stf->hw_rxchain = RXCHAIN_DEF;
493 wlc->stf->rxchain = wlc->stf->hw_rxchain;
494 wlc->stf->rxstreams = (u8) WLC_BITSCNT(wlc->stf->hw_rxchain);
496 /* initialize the txcore table */
497 memcpy(wlc->stf->txcore, txcore_default, sizeof(wlc->stf->txcore));
499 /* default spatial_policy */
500 wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
501 wlc_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
504 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
506 u16 phytxant = wlc->stf->phytxant;
508 if (RSPEC_STF(rspec) != PHY_TXC1_MODE_SISO) {
509 ASSERT(wlc->stf->txstreams > 1);
510 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
511 } else if (wlc->stf->txant == ANT_TX_DEF)
512 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
513 phytxant &= PHY_TXC_ANT_MASK;
517 u16 wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
519 return _wlc_stf_phytxchain_sel(wlc, rspec);
522 u16 wlc_stf_d11hdrs_phyctl_txant(struct wlc_info *wlc, ratespec_t rspec)
524 u16 phytxant = wlc->stf->phytxant;
525 u16 mask = PHY_TXC_ANT_MASK;
527 /* for non-siso rates or default setting, use the available chains */
528 if (WLCISNPHY(wlc->band)) {
529 ASSERT(wlc->stf->txchain != 0);
530 phytxant = _wlc_stf_phytxchain_sel(wlc, rspec);
531 mask = PHY_TXC_HTANT_MASK;
533 phytxant |= phytxant & mask;