1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2014 Realtek Corporation.*/
10 #include "../rtl8723com/phy_common.h"
13 #include "../rtl8723com/dm_common.h"
16 #include <linux/kernel.h>
18 static bool _rtl8723be_phy_bb8723b_config_parafile(struct ieee80211_hw *hw);
19 static bool _rtl8723be_phy_config_mac_with_headerfile(struct ieee80211_hw *hw);
20 static bool _rtl8723be_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
22 static bool _rtl8723be_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
24 static bool _rtl8723be_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
25 u8 channel, u8 *stage,
26 u8 *step, u32 *delay);
28 static void rtl8723be_phy_set_rf_on(struct ieee80211_hw *hw);
29 static void rtl8723be_phy_set_io(struct ieee80211_hw *hw);
31 u32 rtl8723be_phy_query_rf_reg(struct ieee80211_hw *hw, enum radio_path rfpath,
32 u32 regaddr, u32 bitmask)
34 struct rtl_priv *rtlpriv = rtl_priv(hw);
35 u32 original_value, readback_value, bitshift;
37 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
38 "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
39 regaddr, rfpath, bitmask);
41 spin_lock(&rtlpriv->locks.rf_lock);
43 original_value = rtl8723_phy_rf_serial_read(hw, rfpath, regaddr);
44 bitshift = calculate_bit_shift(bitmask);
45 readback_value = (original_value & bitmask) >> bitshift;
47 spin_unlock(&rtlpriv->locks.rf_lock);
49 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
50 "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
51 regaddr, rfpath, bitmask, original_value);
53 return readback_value;
56 void rtl8723be_phy_set_rf_reg(struct ieee80211_hw *hw, enum radio_path path,
57 u32 regaddr, u32 bitmask, u32 data)
59 struct rtl_priv *rtlpriv = rtl_priv(hw);
60 u32 original_value, bitshift;
62 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
63 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
64 regaddr, bitmask, data, path);
66 spin_lock(&rtlpriv->locks.rf_lock);
68 if (bitmask != RFREG_OFFSET_MASK) {
69 original_value = rtl8723_phy_rf_serial_read(hw, path,
71 bitshift = calculate_bit_shift(bitmask);
72 data = ((original_value & (~bitmask)) |
76 rtl8723_phy_rf_serial_write(hw, path, regaddr, data);
78 spin_unlock(&rtlpriv->locks.rf_lock);
80 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
81 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
82 regaddr, bitmask, data, path);
86 bool rtl8723be_phy_mac_config(struct ieee80211_hw *hw)
88 struct rtl_priv *rtlpriv = rtl_priv(hw);
89 bool rtstatus = _rtl8723be_phy_config_mac_with_headerfile(hw);
91 rtl_write_byte(rtlpriv, 0x04CA, 0x0B);
95 bool rtl8723be_phy_bb_config(struct ieee80211_hw *hw)
98 struct rtl_priv *rtlpriv = rtl_priv(hw);
100 u8 b_reg_hwparafile = 1;
102 u8 crystalcap = rtlpriv->efuse.crystalcap;
103 rtl8723_phy_init_bb_rf_reg_def(hw);
104 regval = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
105 rtl_write_word(rtlpriv, REG_SYS_FUNC_EN,
106 regval | BIT(13) | BIT(0) | BIT(1));
108 rtl_write_byte(rtlpriv, REG_RF_CTRL, RF_EN | RF_RSTB | RF_SDMRSTB);
109 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN,
110 FEN_PPLL | FEN_PCIEA | FEN_DIO_PCIE |
111 FEN_BB_GLB_RSTN | FEN_BBRSTB);
112 tmp = rtl_read_dword(rtlpriv, 0x4c);
113 rtl_write_dword(rtlpriv, 0x4c, tmp | BIT(23));
115 rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL + 1, 0x80);
117 if (b_reg_hwparafile == 1)
118 rtstatus = _rtl8723be_phy_bb8723b_config_parafile(hw);
120 crystalcap = crystalcap & 0x3F;
121 rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0xFFF000,
122 (crystalcap | crystalcap << 6));
127 bool rtl8723be_phy_rf_config(struct ieee80211_hw *hw)
129 return rtl8723be_phy_rf6052_config(hw);
132 static bool _rtl8723be_check_positive(struct ieee80211_hw *hw,
133 const u32 condition1,
134 const u32 condition2)
136 struct rtl_priv *rtlpriv = rtl_priv(hw);
137 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
138 u32 cut_ver = ((rtlhal->version & CHIP_VER_RTL_MASK)
139 >> CHIP_VER_RTL_SHIFT);
140 u32 intf = (rtlhal->interface == INTF_USB ? BIT(1) : BIT(0));
142 u8 board_type = ((rtlhal->board_type & BIT(4)) >> 4) << 0 | /* _GLNA */
143 ((rtlhal->board_type & BIT(3)) >> 3) << 1 | /* _GPA */
144 ((rtlhal->board_type & BIT(7)) >> 7) << 2 | /* _ALNA */
145 ((rtlhal->board_type & BIT(6)) >> 6) << 3 | /* _APA */
146 ((rtlhal->board_type & BIT(2)) >> 2) << 4; /* _BT */
148 u32 cond1 = condition1, cond2 = condition2;
149 u32 driver1 = cut_ver << 24 | /* CUT ver */
150 0 << 20 | /* interface 2/2 */
151 0x04 << 16 | /* platform */
152 rtlhal->package_type << 12 |
153 intf << 8 | /* interface 1/2 */
156 u32 driver2 = rtlhal->type_glna << 0 |
157 rtlhal->type_gpa << 8 |
158 rtlhal->type_alna << 16 |
159 rtlhal->type_apa << 24;
161 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
162 "===> [8812A] CheckPositive (cond1, cond2) = (0x%X 0x%X)\n",
164 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
165 "===> [8812A] CheckPositive (driver1, driver2) = (0x%X 0x%X)\n",
168 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
169 "(Platform, Interface) = (0x%X, 0x%X)\n", 0x04, intf);
170 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
171 "(Board, Package) = (0x%X, 0x%X)\n",
172 rtlhal->board_type, rtlhal->package_type);
174 /*============== Value Defined Check ===============*/
175 /*QFN Type [15:12] and Cut Version [27:24] need to do value check*/
177 if (((cond1 & 0x0000F000) != 0) && ((cond1 & 0x0000F000) !=
178 (driver1 & 0x0000F000)))
180 if (((cond1 & 0x0F000000) != 0) && ((cond1 & 0x0F000000) !=
181 (driver1 & 0x0F000000)))
184 /*=============== Bit Defined Check ================*/
185 /* We don't care [31:28] */
188 driver1 &= 0x00FF0FFF;
190 if ((cond1 & driver1) == cond1) {
193 if ((cond1 & 0x0F) == 0) /* BoardType is DONTCARE*/
196 if ((cond1 & BIT(0)) != 0) /*GLNA*/
198 if ((cond1 & BIT(1)) != 0) /*GPA*/
200 if ((cond1 & BIT(2)) != 0) /*ALNA*/
202 if ((cond1 & BIT(3)) != 0) /*APA*/
205 /* BoardType of each RF path is matched*/
206 if ((cond2 & mask) == (driver2 & mask))
214 static void _rtl8723be_config_rf_reg(struct ieee80211_hw *hw, u32 addr,
215 u32 data, enum radio_path rfpath,
218 if (addr == 0xfe || addr == 0xffe) {
219 /* In order not to disturb BT music
220 * when wifi init.(1ant NIC only)
224 rtl_set_rfreg(hw, rfpath, regaddr, RFREG_OFFSET_MASK, data);
228 static void _rtl8723be_config_rf_radio_a(struct ieee80211_hw *hw,
231 u32 content = 0x1000; /*RF Content: radio_a_txt*/
232 u32 maskforphyset = (u32)(content & 0xE000);
234 _rtl8723be_config_rf_reg(hw, addr, data, RF90_PATH_A,
235 addr | maskforphyset);
239 static void _rtl8723be_phy_init_tx_power_by_rate(struct ieee80211_hw *hw)
241 struct rtl_priv *rtlpriv = rtl_priv(hw);
242 struct rtl_phy *rtlphy = &rtlpriv->phy;
244 u8 band, path, txnum, section;
246 for (band = BAND_ON_2_4G; band <= BAND_ON_5G; ++band)
247 for (path = 0; path < TX_PWR_BY_RATE_NUM_RF; ++path)
248 for (txnum = 0; txnum < TX_PWR_BY_RATE_NUM_RF; ++txnum)
250 section < TX_PWR_BY_RATE_NUM_SECTION;
252 rtlphy->tx_power_by_rate_offset
253 [band][path][txnum][section] = 0;
256 static void _rtl8723be_config_bb_reg(struct ieee80211_hw *hw,
261 } else if (addr == 0xfd) {
263 } else if (addr == 0xfc) {
265 } else if (addr == 0xfb) {
267 } else if (addr == 0xfa) {
269 } else if (addr == 0xf9) {
272 rtl_set_bbreg(hw, addr, MASKDWORD, data);
277 static void _rtl8723be_phy_set_txpower_by_rate_base(struct ieee80211_hw *hw,
279 u8 path, u8 rate_section,
282 struct rtl_priv *rtlpriv = rtl_priv(hw);
283 struct rtl_phy *rtlphy = &rtlpriv->phy;
285 if (path > RF90_PATH_D) {
286 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
287 "Invalid Rf Path %d in phy_SetTxPowerByRatBase()\n",
292 if (band == BAND_ON_2_4G) {
293 switch (rate_section) {
295 rtlphy->txpwr_by_rate_base_24g[path][txnum][0] = value;
298 rtlphy->txpwr_by_rate_base_24g[path][txnum][1] = value;
301 rtlphy->txpwr_by_rate_base_24g[path][txnum][2] = value;
304 rtlphy->txpwr_by_rate_base_24g[path][txnum][3] = value;
307 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
308 "Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_SetTxPowerByRateBase()\n",
309 rate_section, path, txnum);
313 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
314 "Invalid Band %d in PHY_SetTxPowerByRateBase()\n",
320 static u8 _rtl8723be_phy_get_txpower_by_rate_base(struct ieee80211_hw *hw,
321 u8 band, u8 path, u8 txnum,
324 struct rtl_priv *rtlpriv = rtl_priv(hw);
325 struct rtl_phy *rtlphy = &rtlpriv->phy;
327 if (path > RF90_PATH_D) {
328 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
329 "Invalid Rf Path %d in PHY_GetTxPowerByRateBase()\n",
334 if (band == BAND_ON_2_4G) {
335 switch (rate_section) {
337 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][0];
340 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][1];
343 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][2];
346 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][3];
349 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
350 "Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_GetTxPowerByRateBase()\n",
351 rate_section, path, txnum);
355 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
356 "Invalid Band %d in PHY_GetTxPowerByRateBase()\n",
363 static void _rtl8723be_phy_store_txpower_by_rate_base(struct ieee80211_hw *hw)
365 struct rtl_priv *rtlpriv = rtl_priv(hw);
366 struct rtl_phy *rtlphy = &rtlpriv->phy;
368 u8 base = 0, path = 0;
370 for (path = RF90_PATH_A; path <= RF90_PATH_B; ++path) {
371 if (path == RF90_PATH_A) {
372 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
373 [BAND_ON_2_4G][path][RF_1TX][3] >> 24) & 0xFF;
374 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
375 _rtl8723be_phy_set_txpower_by_rate_base(hw,
376 BAND_ON_2_4G, path, CCK, RF_1TX, base);
377 } else if (path == RF90_PATH_B) {
378 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
379 [BAND_ON_2_4G][path][RF_1TX][3] >> 0) & 0xFF;
380 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
381 _rtl8723be_phy_set_txpower_by_rate_base(hw,
386 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
387 [BAND_ON_2_4G][path][RF_1TX][1] >> 24) & 0xFF;
388 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
389 _rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
393 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
394 [BAND_ON_2_4G][path][RF_1TX][5] >> 24) & 0xFF;
395 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
396 _rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
400 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
401 [BAND_ON_2_4G][path][RF_2TX][7] >> 24) & 0xFF;
402 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
403 _rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
409 static void _phy_convert_txpower_dbm_to_relative_value(u32 *data, u8 start,
416 for (i = 3; i >= 0; --i) {
417 if (i >= start && i <= end) {
418 /* Get the exact value */
419 temp_value = (u8)(*data >> (i * 8)) & 0xF;
420 temp_value += ((u8)((*data >> (i*8 + 4)) & 0xF)) * 10;
422 /* Change the value to a relative value */
423 temp_value = (temp_value > base_val) ?
424 temp_value - base_val :
425 base_val - temp_value;
427 temp_value = (u8)(*data >> (i * 8)) & 0xFF;
430 temp_data |= temp_value;
435 static void _rtl8723be_phy_convert_txpower_dbm_to_relative_value(
436 struct ieee80211_hw *hw)
438 struct rtl_priv *rtlpriv = rtl_priv(hw);
439 struct rtl_phy *rtlphy = &rtlpriv->phy;
440 u8 base = 0, rfpath = RF90_PATH_A;
442 base = _rtl8723be_phy_get_txpower_by_rate_base(hw,
443 BAND_ON_2_4G, rfpath, RF_1TX, CCK);
444 _phy_convert_txpower_dbm_to_relative_value(
445 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][2],
447 _phy_convert_txpower_dbm_to_relative_value(
448 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][3],
451 base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath,
453 _phy_convert_txpower_dbm_to_relative_value(
454 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][0],
456 _phy_convert_txpower_dbm_to_relative_value(
457 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][1],
460 base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G,
461 rfpath, RF_1TX, HT_MCS0_MCS7);
462 _phy_convert_txpower_dbm_to_relative_value(
463 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][4],
465 _phy_convert_txpower_dbm_to_relative_value(
466 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][5],
469 base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G,
472 _phy_convert_txpower_dbm_to_relative_value(
473 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][6],
476 _phy_convert_txpower_dbm_to_relative_value(
477 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][7],
480 rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
481 "<===%s\n", __func__);
484 static void phy_txpower_by_rate_config(struct ieee80211_hw *hw)
486 _rtl8723be_phy_store_txpower_by_rate_base(hw);
487 _rtl8723be_phy_convert_txpower_dbm_to_relative_value(hw);
490 static bool _rtl8723be_phy_bb8723b_config_parafile(struct ieee80211_hw *hw)
492 struct rtl_priv *rtlpriv = rtl_priv(hw);
493 struct rtl_phy *rtlphy = &rtlpriv->phy;
494 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
497 /* switch ant to BT */
498 if (rtlpriv->rtlhal.interface == INTF_USB) {
499 rtl_write_dword(rtlpriv, 0x948, 0x0);
501 if (rtlpriv->btcoexist.btc_info.single_ant_path == 0)
502 rtl_write_dword(rtlpriv, 0x948, 0x280);
504 rtl_write_dword(rtlpriv, 0x948, 0x0);
507 rtstatus = _rtl8723be_phy_config_bb_with_headerfile(hw,
508 BASEBAND_CONFIG_PHY_REG);
510 pr_err("Write BB Reg Fail!!\n");
513 _rtl8723be_phy_init_tx_power_by_rate(hw);
514 if (!rtlefuse->autoload_failflag) {
515 rtlphy->pwrgroup_cnt = 0;
516 rtstatus = _rtl8723be_phy_config_bb_with_pgheaderfile(hw,
517 BASEBAND_CONFIG_PHY_REG);
519 phy_txpower_by_rate_config(hw);
521 pr_err("BB_PG Reg Fail!!\n");
524 rtstatus = _rtl8723be_phy_config_bb_with_headerfile(hw,
525 BASEBAND_CONFIG_AGC_TAB);
527 pr_err("AGC Table Fail\n");
530 rtlphy->cck_high_power = (bool)(rtl_get_bbreg(hw,
531 RFPGA0_XA_HSSIPARAMETER2,
536 static bool rtl8723be_phy_config_with_headerfile(struct ieee80211_hw *hw,
539 void (*set_reg)(struct ieee80211_hw *hw, u32 regaddr, u32 data))
546 bool matched = true, skipped = false;
548 while ((i + 1) < arraylen) {
549 u32 v1 = array_table[i];
550 u32 v2 = array_table[i + 1];
552 if (v1 & (BIT(31) | BIT(30))) {/*positive & negative condition*/
553 if (v1 & BIT(31)) {/* positive condition*/
554 cond = (u8)((v1 & (BIT(29) | BIT(28))) >> 28);
555 if (cond == COND_ENDIF) { /*end*/
558 } else if (cond == COND_ELSE) { /*else*/
559 matched = skipped ? false : true;
560 } else {/*if , else if*/
564 if (_rtl8723be_check_positive(
574 } else if (v1 & BIT(30)) { /*negative condition*/
587 static bool _rtl8723be_phy_config_mac_with_headerfile(struct ieee80211_hw *hw)
589 struct rtl_priv *rtlpriv = rtl_priv(hw);
591 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Read rtl8723beMACPHY_Array\n");
593 return rtl8723be_phy_config_with_headerfile(hw,
594 RTL8723BEMAC_1T_ARRAY, RTL8723BEMAC_1T_ARRAYLEN,
595 rtl_write_byte_with_val32);
598 static bool _rtl8723be_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
602 if (configtype == BASEBAND_CONFIG_PHY_REG)
603 return rtl8723be_phy_config_with_headerfile(hw,
604 RTL8723BEPHY_REG_1TARRAY,
605 RTL8723BEPHY_REG_1TARRAYLEN,
606 _rtl8723be_config_bb_reg);
607 else if (configtype == BASEBAND_CONFIG_AGC_TAB)
608 return rtl8723be_phy_config_with_headerfile(hw,
609 RTL8723BEAGCTAB_1TARRAY,
610 RTL8723BEAGCTAB_1TARRAYLEN,
611 rtl_set_bbreg_with_dwmask);
616 static u8 _rtl8723be_get_rate_section_index(u32 regaddr)
621 case RTXAGC_A_RATE18_06:
624 case RTXAGC_A_RATE54_24:
627 case RTXAGC_A_CCK1_MCS32:
630 case RTXAGC_B_CCK11_A_CCK2_11:
633 case RTXAGC_A_MCS03_MCS00:
636 case RTXAGC_A_MCS07_MCS04:
639 case RTXAGC_A_MCS11_MCS08:
642 case RTXAGC_A_MCS15_MCS12:
645 case RTXAGC_B_RATE18_06:
648 case RTXAGC_B_RATE54_24:
651 case RTXAGC_B_CCK1_55_MCS32:
654 case RTXAGC_B_MCS03_MCS00:
657 case RTXAGC_B_MCS07_MCS04:
660 case RTXAGC_B_MCS11_MCS08:
663 case RTXAGC_B_MCS15_MCS12:
668 if (regaddr >= 0xC20 && regaddr <= 0xC4C)
669 index = (u8)((regaddr - 0xC20) / 4);
670 else if (regaddr >= 0xE20 && regaddr <= 0xE4C)
671 index = (u8)((regaddr - 0xE20) / 4);
677 static void _rtl8723be_store_tx_power_by_rate(struct ieee80211_hw *hw,
678 u32 band, u32 rfpath,
679 u32 txnum, u32 regaddr,
680 u32 bitmask, u32 data)
682 struct rtl_priv *rtlpriv = rtl_priv(hw);
683 struct rtl_phy *rtlphy = &rtlpriv->phy;
684 u8 rate_section = _rtl8723be_get_rate_section_index(regaddr);
686 if (band != BAND_ON_2_4G && band != BAND_ON_5G) {
687 rtl_dbg(rtlpriv, FPHY, PHY_TXPWR, "Invalid Band %d\n", band);
690 if (rfpath > MAX_RF_PATH - 1) {
691 rtl_dbg(rtlpriv, FPHY, PHY_TXPWR,
692 "Invalid RfPath %d\n", rfpath);
695 if (txnum > MAX_RF_PATH - 1) {
696 rtl_dbg(rtlpriv, FPHY, PHY_TXPWR, "Invalid TxNum %d\n", txnum);
700 rtlphy->tx_power_by_rate_offset[band][rfpath][txnum][rate_section] =
705 static bool _rtl8723be_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
708 struct rtl_priv *rtlpriv = rtl_priv(hw);
710 u32 *phy_regarray_table_pg;
711 u16 phy_regarray_pg_len;
712 u32 v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0;
714 phy_regarray_pg_len = RTL8723BEPHY_REG_ARRAY_PGLEN;
715 phy_regarray_table_pg = RTL8723BEPHY_REG_ARRAY_PG;
717 if (configtype == BASEBAND_CONFIG_PHY_REG) {
718 for (i = 0; i < phy_regarray_pg_len; i = i + 6) {
719 v1 = phy_regarray_table_pg[i];
720 v2 = phy_regarray_table_pg[i+1];
721 v3 = phy_regarray_table_pg[i+2];
722 v4 = phy_regarray_table_pg[i+3];
723 v5 = phy_regarray_table_pg[i+4];
724 v6 = phy_regarray_table_pg[i+5];
726 if (v1 < 0xcdcdcdcd) {
727 if (phy_regarray_table_pg[i] == 0xfe ||
728 phy_regarray_table_pg[i] == 0xffe)
731 _rtl8723be_store_tx_power_by_rate(hw,
732 v1, v2, v3, v4, v5, v6);
737 rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
738 "configtype != BaseBand_Config_PHY_REG\n");
743 bool rtl8723be_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
744 enum radio_path rfpath)
746 struct rtl_priv *rtlpriv = rtl_priv(hw);
747 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
750 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "Radio No %x\n", rfpath);
753 ret = rtl8723be_phy_config_with_headerfile(hw,
754 RTL8723BE_RADIOA_1TARRAY,
755 RTL8723BE_RADIOA_1TARRAYLEN,
756 _rtl8723be_config_rf_radio_a);
758 if (rtlhal->oem_id == RT_CID_819X_HP)
759 _rtl8723be_config_rf_radio_a(hw, 0x52, 0x7E4BD);
765 rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
766 "switch case %#x not processed\n", rfpath);
772 void rtl8723be_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
774 struct rtl_priv *rtlpriv = rtl_priv(hw);
775 struct rtl_phy *rtlphy = &rtlpriv->phy;
777 rtlphy->default_initialgain[0] =
778 (u8)rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
779 rtlphy->default_initialgain[1] =
780 (u8)rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
781 rtlphy->default_initialgain[2] =
782 (u8)rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
783 rtlphy->default_initialgain[3] =
784 (u8)rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
786 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
787 "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
788 rtlphy->default_initialgain[0],
789 rtlphy->default_initialgain[1],
790 rtlphy->default_initialgain[2],
791 rtlphy->default_initialgain[3]);
793 rtlphy->framesync = (u8)rtl_get_bbreg(hw, ROFDM0_RXDETECTOR3,
795 rtlphy->framesync_c34 = rtl_get_bbreg(hw, ROFDM0_RXDETECTOR2,
798 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
799 "Default framesync (0x%x) = 0x%x\n",
800 ROFDM0_RXDETECTOR3, rtlphy->framesync);
803 static u8 _rtl8723be_phy_get_ratesection_intxpower_byrate(enum radio_path path,
814 case DESC92C_RATE5_5M:
815 if (path == RF90_PATH_A)
817 else if (path == RF90_PATH_B)
821 case DESC92C_RATE11M:
827 case DESC92C_RATE12M:
828 case DESC92C_RATE18M:
832 case DESC92C_RATE24M:
833 case DESC92C_RATE36M:
834 case DESC92C_RATE48M:
835 case DESC92C_RATE54M:
839 case DESC92C_RATEMCS0:
840 case DESC92C_RATEMCS1:
841 case DESC92C_RATEMCS2:
842 case DESC92C_RATEMCS3:
846 case DESC92C_RATEMCS4:
847 case DESC92C_RATEMCS5:
848 case DESC92C_RATEMCS6:
849 case DESC92C_RATEMCS7:
853 case DESC92C_RATEMCS8:
854 case DESC92C_RATEMCS9:
855 case DESC92C_RATEMCS10:
856 case DESC92C_RATEMCS11:
860 case DESC92C_RATEMCS12:
861 case DESC92C_RATEMCS13:
862 case DESC92C_RATEMCS14:
863 case DESC92C_RATEMCS15:
868 WARN_ONCE(true, "rtl8723be: Rate_Section is Illegal\n");
875 static u8 _rtl8723be_get_txpower_by_rate(struct ieee80211_hw *hw,
877 enum radio_path rfpath, u8 rate)
879 struct rtl_priv *rtlpriv = rtl_priv(hw);
880 struct rtl_phy *rtlphy = &rtlpriv->phy;
881 u8 shift = 0, rate_section, tx_num;
884 rate_section = _rtl8723be_phy_get_ratesection_intxpower_byrate(rfpath,
886 tx_num = RF_TX_NUM_NONIMPLEMENT;
888 if (tx_num == RF_TX_NUM_NONIMPLEMENT) {
889 if (rate >= DESC92C_RATEMCS8 && rate <= DESC92C_RATEMCS15)
897 case DESC92C_RATE24M:
898 case DESC92C_RATEMCS0:
899 case DESC92C_RATEMCS4:
900 case DESC92C_RATEMCS8:
901 case DESC92C_RATEMCS12:
907 case DESC92C_RATE36M:
908 case DESC92C_RATEMCS1:
909 case DESC92C_RATEMCS5:
910 case DESC92C_RATEMCS9:
911 case DESC92C_RATEMCS13:
914 case DESC92C_RATE5_5M:
915 case DESC92C_RATE12M:
916 case DESC92C_RATE48M:
917 case DESC92C_RATEMCS2:
918 case DESC92C_RATEMCS6:
919 case DESC92C_RATEMCS10:
920 case DESC92C_RATEMCS14:
923 case DESC92C_RATE11M:
924 case DESC92C_RATE18M:
925 case DESC92C_RATE54M:
926 case DESC92C_RATEMCS3:
927 case DESC92C_RATEMCS7:
928 case DESC92C_RATEMCS11:
929 case DESC92C_RATEMCS15:
933 WARN_ONCE(true, "rtl8723be: Rate_Section is Illegal\n");
936 tx_pwr_diff = (u8)(rtlphy->tx_power_by_rate_offset[band][rfpath][tx_num]
937 [rate_section] >> shift) & 0xff;
942 static u8 _rtl8723be_get_txpower_index(struct ieee80211_hw *hw, u8 path,
943 u8 rate, u8 bandwidth, u8 channel)
945 struct rtl_priv *rtlpriv = rtl_priv(hw);
946 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
947 u8 index = (channel - 1);
949 u8 power_diff_byrate = 0;
951 if (channel > 14 || channel < 1) {
953 rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
954 "Illegal channel!\n");
956 if (RX_HAL_IS_CCK_RATE(rate))
957 txpower = rtlefuse->txpwrlevel_cck[path][index];
958 else if (DESC92C_RATE6M <= rate)
959 txpower = rtlefuse->txpwrlevel_ht40_1s[path][index];
961 rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
964 if (DESC92C_RATE6M <= rate && rate <= DESC92C_RATE54M &&
965 !RX_HAL_IS_CCK_RATE(rate))
966 txpower += rtlefuse->txpwr_legacyhtdiff[0][TX_1S];
968 if (bandwidth == HT_CHANNEL_WIDTH_20) {
969 if (DESC92C_RATEMCS0 <= rate && rate <= DESC92C_RATEMCS15)
970 txpower += rtlefuse->txpwr_ht20diff[0][TX_1S];
971 if (DESC92C_RATEMCS8 <= rate && rate <= DESC92C_RATEMCS15)
972 txpower += rtlefuse->txpwr_ht20diff[0][TX_2S];
973 } else if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
974 if (DESC92C_RATEMCS0 <= rate && rate <= DESC92C_RATEMCS15)
975 txpower += rtlefuse->txpwr_ht40diff[0][TX_1S];
976 if (DESC92C_RATEMCS8 <= rate && rate <= DESC92C_RATEMCS15)
977 txpower += rtlefuse->txpwr_ht40diff[0][TX_2S];
980 if (rtlefuse->eeprom_regulatory != 2)
981 power_diff_byrate = _rtl8723be_get_txpower_by_rate(hw,
985 txpower += power_diff_byrate;
987 if (txpower > MAX_POWER_INDEX)
988 txpower = MAX_POWER_INDEX;
993 static void _rtl8723be_phy_set_txpower_index(struct ieee80211_hw *hw,
994 u8 power_index, u8 path, u8 rate)
996 struct rtl_priv *rtlpriv = rtl_priv(hw);
997 if (path == RF90_PATH_A) {
1000 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_CCK1_MCS32,
1001 MASKBYTE1, power_index);
1003 case DESC92C_RATE2M:
1004 rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
1005 MASKBYTE1, power_index);
1007 case DESC92C_RATE5_5M:
1008 rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
1009 MASKBYTE2, power_index);
1011 case DESC92C_RATE11M:
1012 rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
1013 MASKBYTE3, power_index);
1016 case DESC92C_RATE6M:
1017 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
1018 MASKBYTE0, power_index);
1020 case DESC92C_RATE9M:
1021 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
1022 MASKBYTE1, power_index);
1024 case DESC92C_RATE12M:
1025 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
1026 MASKBYTE2, power_index);
1028 case DESC92C_RATE18M:
1029 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
1030 MASKBYTE3, power_index);
1033 case DESC92C_RATE24M:
1034 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
1035 MASKBYTE0, power_index);
1037 case DESC92C_RATE36M:
1038 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
1039 MASKBYTE1, power_index);
1041 case DESC92C_RATE48M:
1042 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
1043 MASKBYTE2, power_index);
1045 case DESC92C_RATE54M:
1046 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
1047 MASKBYTE3, power_index);
1050 case DESC92C_RATEMCS0:
1051 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
1052 MASKBYTE0, power_index);
1054 case DESC92C_RATEMCS1:
1055 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
1056 MASKBYTE1, power_index);
1058 case DESC92C_RATEMCS2:
1059 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
1060 MASKBYTE2, power_index);
1062 case DESC92C_RATEMCS3:
1063 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
1064 MASKBYTE3, power_index);
1067 case DESC92C_RATEMCS4:
1068 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
1069 MASKBYTE0, power_index);
1071 case DESC92C_RATEMCS5:
1072 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
1073 MASKBYTE1, power_index);
1075 case DESC92C_RATEMCS6:
1076 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
1077 MASKBYTE2, power_index);
1079 case DESC92C_RATEMCS7:
1080 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
1081 MASKBYTE3, power_index);
1084 case DESC92C_RATEMCS8:
1085 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
1086 MASKBYTE0, power_index);
1088 case DESC92C_RATEMCS9:
1089 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
1090 MASKBYTE1, power_index);
1092 case DESC92C_RATEMCS10:
1093 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
1094 MASKBYTE2, power_index);
1096 case DESC92C_RATEMCS11:
1097 rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
1098 MASKBYTE3, power_index);
1102 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid Rate!!\n");
1106 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid RFPath!!\n");
1110 void rtl8723be_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
1112 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1113 static const u8 cck_rates[] = {
1114 DESC92C_RATE1M, DESC92C_RATE2M,
1115 DESC92C_RATE5_5M, DESC92C_RATE11M
1117 static const u8 ofdm_rates[] = {
1118 DESC92C_RATE6M, DESC92C_RATE9M,
1119 DESC92C_RATE12M, DESC92C_RATE18M,
1120 DESC92C_RATE24M, DESC92C_RATE36M,
1121 DESC92C_RATE48M, DESC92C_RATE54M
1123 static const u8 ht_rates_1t[] = {
1124 DESC92C_RATEMCS0, DESC92C_RATEMCS1,
1125 DESC92C_RATEMCS2, DESC92C_RATEMCS3,
1126 DESC92C_RATEMCS4, DESC92C_RATEMCS5,
1127 DESC92C_RATEMCS6, DESC92C_RATEMCS7
1132 if (!rtlefuse->txpwr_fromeprom)
1135 for (i = 0; i < ARRAY_SIZE(cck_rates); i++) {
1136 power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
1138 rtl_priv(hw)->phy.current_chan_bw,
1140 _rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
1143 for (i = 0; i < ARRAY_SIZE(ofdm_rates); i++) {
1144 power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
1146 rtl_priv(hw)->phy.current_chan_bw,
1148 _rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
1151 for (i = 0; i < ARRAY_SIZE(ht_rates_1t); i++) {
1152 power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
1154 rtl_priv(hw)->phy.current_chan_bw,
1156 _rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
1161 void rtl8723be_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
1163 struct rtl_priv *rtlpriv = rtl_priv(hw);
1164 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1165 enum io_type iotype;
1167 if (!is_hal_stop(rtlhal)) {
1168 switch (operation) {
1169 case SCAN_OPT_BACKUP_BAND0:
1170 iotype = IO_CMD_PAUSE_BAND0_DM_BY_SCAN;
1171 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
1175 case SCAN_OPT_RESTORE:
1176 iotype = IO_CMD_RESUME_DM_BY_SCAN;
1177 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
1181 pr_err("Unknown Scan Backup operation.\n");
1187 void rtl8723be_phy_set_bw_mode_callback(struct ieee80211_hw *hw)
1189 struct rtl_priv *rtlpriv = rtl_priv(hw);
1190 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1191 struct rtl_phy *rtlphy = &rtlpriv->phy;
1192 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1196 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
1197 "Switch to %s bandwidth\n",
1198 rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
1201 if (is_hal_stop(rtlhal)) {
1202 rtlphy->set_bwmode_inprogress = false;
1206 reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
1207 reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
1209 switch (rtlphy->current_chan_bw) {
1210 case HT_CHANNEL_WIDTH_20:
1211 reg_bw_opmode |= BW_OPMODE_20MHZ;
1212 rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
1214 case HT_CHANNEL_WIDTH_20_40:
1215 reg_bw_opmode &= ~BW_OPMODE_20MHZ;
1216 rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
1217 reg_prsr_rsc = (reg_prsr_rsc & 0x90) |
1218 (mac->cur_40_prime_sc << 5);
1219 rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
1222 pr_err("unknown bandwidth: %#X\n",
1223 rtlphy->current_chan_bw);
1227 switch (rtlphy->current_chan_bw) {
1228 case HT_CHANNEL_WIDTH_20:
1229 rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
1230 rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
1231 /* rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1);*/
1233 case HT_CHANNEL_WIDTH_20_40:
1234 rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
1235 rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
1237 rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCK_SIDEBAND,
1238 (mac->cur_40_prime_sc >> 1));
1239 rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
1240 /*rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 0);*/
1242 rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
1243 (mac->cur_40_prime_sc ==
1244 HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
1247 pr_err("unknown bandwidth: %#X\n",
1248 rtlphy->current_chan_bw);
1251 rtl8723be_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
1252 rtlphy->set_bwmode_inprogress = false;
1253 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD, "\n");
1256 void rtl8723be_phy_set_bw_mode(struct ieee80211_hw *hw,
1257 enum nl80211_channel_type ch_type)
1259 struct rtl_priv *rtlpriv = rtl_priv(hw);
1260 struct rtl_phy *rtlphy = &rtlpriv->phy;
1261 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1262 u8 tmp_bw = rtlphy->current_chan_bw;
1264 if (rtlphy->set_bwmode_inprogress)
1266 rtlphy->set_bwmode_inprogress = true;
1267 if ((!is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
1268 rtl8723be_phy_set_bw_mode_callback(hw);
1270 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1271 "false driver sleep or unload\n");
1272 rtlphy->set_bwmode_inprogress = false;
1273 rtlphy->current_chan_bw = tmp_bw;
1277 void rtl8723be_phy_sw_chnl_callback(struct ieee80211_hw *hw)
1279 struct rtl_priv *rtlpriv = rtl_priv(hw);
1280 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1281 struct rtl_phy *rtlphy = &rtlpriv->phy;
1284 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
1285 "switch to channel%d\n", rtlphy->current_channel);
1286 if (is_hal_stop(rtlhal))
1289 if (!rtlphy->sw_chnl_inprogress)
1291 if (!_rtl8723be_phy_sw_chnl_step_by_step(hw,
1292 rtlphy->current_channel,
1293 &rtlphy->sw_chnl_stage,
1294 &rtlphy->sw_chnl_step,
1301 rtlphy->sw_chnl_inprogress = false;
1305 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
1308 u8 rtl8723be_phy_sw_chnl(struct ieee80211_hw *hw)
1310 struct rtl_priv *rtlpriv = rtl_priv(hw);
1311 struct rtl_phy *rtlphy = &rtlpriv->phy;
1312 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1314 if (rtlphy->sw_chnl_inprogress)
1316 if (rtlphy->set_bwmode_inprogress)
1318 WARN_ONCE((rtlphy->current_channel > 14),
1319 "rtl8723be: WIRELESS_MODE_G but channel>14");
1320 rtlphy->sw_chnl_inprogress = true;
1321 rtlphy->sw_chnl_stage = 0;
1322 rtlphy->sw_chnl_step = 0;
1323 if (!(is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
1324 rtl8723be_phy_sw_chnl_callback(hw);
1325 rtl_dbg(rtlpriv, COMP_CHAN, DBG_LOUD,
1326 "sw_chnl_inprogress false schedule workitem current channel %d\n",
1327 rtlphy->current_channel);
1328 rtlphy->sw_chnl_inprogress = false;
1330 rtl_dbg(rtlpriv, COMP_CHAN, DBG_LOUD,
1331 "sw_chnl_inprogress false driver sleep or unload\n");
1332 rtlphy->sw_chnl_inprogress = false;
1337 static bool _rtl8723be_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
1338 u8 channel, u8 *stage,
1339 u8 *step, u32 *delay)
1341 struct rtl_priv *rtlpriv = rtl_priv(hw);
1342 struct rtl_phy *rtlphy = &rtlpriv->phy;
1343 struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
1344 u32 precommoncmdcnt;
1345 struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
1346 u32 postcommoncmdcnt;
1347 struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
1349 struct swchnlcmd *currentcmd = NULL;
1351 u8 num_total_rfpath = rtlphy->num_total_rfpath;
1353 precommoncmdcnt = 0;
1354 rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
1356 CMDID_SET_TXPOWEROWER_LEVEL,
1358 rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
1359 MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
1361 postcommoncmdcnt = 0;
1363 rtl8723_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
1364 MAX_POSTCMD_CNT, CMDID_END,
1369 WARN_ONCE((channel < 1 || channel > 14),
1370 "rtl8723be: illegal channel for Zebra: %d\n", channel);
1372 rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
1373 MAX_RFDEPENDCMD_CNT,
1375 RF_CHNLBW, channel, 10);
1377 rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
1378 MAX_RFDEPENDCMD_CNT,
1379 CMDID_END, 0, 0, 0);
1384 currentcmd = &precommoncmd[*step];
1387 currentcmd = &rfdependcmd[*step];
1390 currentcmd = &postcommoncmd[*step];
1393 pr_err("Invalid 'stage' = %d, Check it!\n",
1398 if (currentcmd->cmdid == CMDID_END) {
1399 if ((*stage) == 2) {
1408 switch (currentcmd->cmdid) {
1409 case CMDID_SET_TXPOWEROWER_LEVEL:
1410 rtl8723be_phy_set_txpower_level(hw, channel);
1412 case CMDID_WRITEPORT_ULONG:
1413 rtl_write_dword(rtlpriv, currentcmd->para1,
1416 case CMDID_WRITEPORT_USHORT:
1417 rtl_write_word(rtlpriv, currentcmd->para1,
1418 (u16)currentcmd->para2);
1420 case CMDID_WRITEPORT_UCHAR:
1421 rtl_write_byte(rtlpriv, currentcmd->para1,
1422 (u8)currentcmd->para2);
1424 case CMDID_RF_WRITEREG:
1425 for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
1426 rtlphy->rfreg_chnlval[rfpath] =
1427 ((rtlphy->rfreg_chnlval[rfpath] &
1428 0xfffffc00) | currentcmd->para2);
1430 rtl_set_rfreg(hw, (enum radio_path)rfpath,
1433 rtlphy->rfreg_chnlval[rfpath]);
1437 rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
1438 "switch case %#x not processed\n",
1446 (*delay) = currentcmd->msdelay;
1451 static u8 _rtl8723be_phy_path_a_iqk(struct ieee80211_hw *hw)
1453 u32 reg_eac, reg_e94, reg_e9c, tmp;
1456 /* leave IQK mode */
1457 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1458 /* switch to path A */
1459 rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000000);
1460 /* enable path A PA in TXIQK mode */
1461 rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, RFREG_OFFSET_MASK, 0x800a0);
1462 rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x20000);
1463 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0003f);
1464 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xc7f87);
1467 /* path-A IQK setting */
1469 rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
1470 rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
1471 /* path-A IQK setting */
1472 rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
1473 rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
1474 rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1475 rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1477 rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x821403ea);
1478 rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28160000);
1479 rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
1480 rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
1481 /* LO calibration setting */
1482 rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x00462911);
1483 /* enter IQK mode */
1484 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1486 /* One shot, path A LOK & IQK */
1487 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
1488 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
1490 mdelay(IQK_DELAY_TIME);
1492 /* leave IQK mode */
1493 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1496 reg_eac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
1497 reg_e94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
1498 reg_e9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
1500 if (!(reg_eac & BIT(28)) &&
1501 (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
1502 (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
1504 else /* if Tx not OK, ignore Rx */
1507 /* Allen 20131125 */
1508 tmp = (reg_e9c & 0x03FF0000) >> 16;
1509 if ((tmp & 0x200) > 0)
1512 if (!(reg_eac & BIT(28)) &&
1513 (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
1514 (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
1517 else /* if Tx not OK, ignore Rx */
1523 /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
1524 static u8 _rtl8723be_phy_path_a_rx_iqk(struct ieee80211_hw *hw)
1526 u32 reg_eac, reg_e94, reg_e9c, reg_ea4, u32tmp, tmp;
1529 /* leave IQK mode */
1530 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1532 /* switch to path A */
1533 rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000000);
1535 /* 1 Get TXIMR setting */
1536 /* modify RXIQK mode table */
1537 rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
1538 rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
1539 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
1540 /* LNA2 off, PA on for Dcut */
1541 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7fb7);
1542 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1545 rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
1546 rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
1548 /* path-A IQK setting */
1549 rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
1550 rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
1551 rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1552 rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1554 rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82160ff0);
1555 rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
1556 rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
1557 rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
1559 /* LO calibration setting */
1560 rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a911);
1562 /* enter IQK mode */
1563 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1565 /* One shot, path A LOK & IQK */
1566 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
1567 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
1569 mdelay(IQK_DELAY_TIME);
1571 /* leave IQK mode */
1572 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1575 reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
1576 reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
1577 reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
1579 if (!(reg_eac & BIT(28)) &&
1580 (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
1581 (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
1583 else /* if Tx not OK, ignore Rx */
1586 /* Allen 20131125 */
1587 tmp = (reg_e9c & 0x03FF0000) >> 16;
1588 if ((tmp & 0x200) > 0)
1591 if (!(reg_eac & BIT(28)) &&
1592 (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
1593 (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
1596 else /* if Tx not OK, ignore Rx */
1599 u32tmp = 0x80007C00 | (reg_e94 & 0x3FF0000) |
1600 ((reg_e9c & 0x3FF0000) >> 16);
1601 rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, u32tmp);
1604 /* modify RXIQK mode table */
1605 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1606 rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
1607 rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
1608 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
1609 /* LAN2 on, PA off for Dcut */
1610 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7d77);
1612 /* PA, PAD setting */
1613 rtl_set_rfreg(hw, RF90_PATH_A, 0xdf, RFREG_OFFSET_MASK, 0xf80);
1614 rtl_set_rfreg(hw, RF90_PATH_A, 0x55, RFREG_OFFSET_MASK, 0x4021f);
1617 rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
1619 /* path-A IQK setting */
1620 rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
1621 rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
1622 rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1623 rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1625 rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82110000);
1626 rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x2816001f);
1627 rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
1628 rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
1630 /* LO calibration setting */
1631 rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a8d1);
1633 /* enter IQK mode */
1634 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1636 /* One shot, path A LOK & IQK */
1637 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
1638 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
1640 mdelay(IQK_DELAY_TIME);
1642 /* leave IQK mode */
1643 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1646 reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
1647 reg_ea4 = rtl_get_bbreg(hw, RRX_POWER_BEFORE_IQK_A_2, MASKDWORD);
1649 /* leave IQK mode */
1650 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1651 rtl_set_rfreg(hw, RF90_PATH_A, 0xdf, RFREG_OFFSET_MASK, 0x780);
1653 /* Allen 20131125 */
1654 tmp = (reg_eac & 0x03FF0000) >> 16;
1655 if ((tmp & 0x200) > 0)
1657 /* if Tx is OK, check whether Rx is OK */
1658 if (!(reg_eac & BIT(27)) &&
1659 (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
1660 (((reg_eac & 0x03FF0000) >> 16) != 0x36))
1662 else if (!(reg_eac & BIT(27)) &&
1663 (((reg_ea4 & 0x03FF0000) >> 16) < 0x110) &&
1664 (((reg_ea4 & 0x03FF0000) >> 16) > 0xf0) &&
1671 static u8 _rtl8723be_phy_path_b_iqk(struct ieee80211_hw *hw)
1673 u32 reg_eac, reg_e94, reg_e9c, tmp;
1676 /* leave IQK mode */
1677 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1678 /* switch to path B */
1679 rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000280);
1681 /* enable path B PA in TXIQK mode */
1682 rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
1683 rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x40fc1);
1687 rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
1688 rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
1689 /* path-A IQK setting */
1690 rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
1691 rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
1692 rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1693 rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1695 rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x821403ea);
1696 rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
1697 rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
1698 rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
1700 /* LO calibration setting */
1701 rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x00462911);
1703 /* enter IQK mode */
1704 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1706 /* One shot, path B LOK & IQK */
1707 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
1708 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
1710 mdelay(IQK_DELAY_TIME);
1712 /* leave IQK mode */
1713 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1716 reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
1717 reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
1718 reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
1720 if (!(reg_eac & BIT(28)) &&
1721 (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
1722 (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
1727 /* Allen 20131125 */
1728 tmp = (reg_e9c & 0x03FF0000) >> 16;
1729 if ((tmp & 0x200) > 0)
1732 if (!(reg_eac & BIT(28)) &&
1733 (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
1734 (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
1743 /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
1744 static u8 _rtl8723be_phy_path_b_rx_iqk(struct ieee80211_hw *hw)
1746 u32 reg_e94, reg_e9c, reg_ea4, reg_eac, u32tmp, tmp;
1749 /* leave IQK mode */
1750 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1751 /* switch to path B */
1752 rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000280);
1754 /* 1 Get TXIMR setting */
1755 /* modify RXIQK mode table */
1756 rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, RFREG_OFFSET_MASK, 0x800a0);
1757 rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
1758 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
1759 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7ff7);
1761 /* open PA S1 & SMIXER */
1762 rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
1763 rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x60fed);
1766 rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
1767 rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
1769 /* path-B IQK setting */
1770 rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
1771 rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
1772 rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1773 rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1775 rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82160ff0);
1776 rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
1777 rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
1778 rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
1780 /* LO calibration setting */
1781 rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a911);
1782 /* enter IQK mode */
1783 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1785 /* One shot, path B TXIQK @ RXIQK */
1786 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
1787 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
1789 mdelay(IQK_DELAY_TIME);
1791 /* leave IQK mode */
1792 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1794 reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
1795 reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
1796 reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
1798 if (!(reg_eac & BIT(28)) &&
1799 (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
1800 (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
1802 else /* if Tx not OK, ignore Rx */
1805 /* Allen 20131125 */
1806 tmp = (reg_e9c & 0x03FF0000) >> 16;
1807 if ((tmp & 0x200) > 0)
1810 if (!(reg_eac & BIT(28)) &&
1811 (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
1812 (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
1818 u32tmp = 0x80007C00 | (reg_e94 & 0x3FF0000) |
1819 ((reg_e9c & 0x3FF0000) >> 16);
1820 rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, u32tmp);
1824 /* <20121009, Kordan> RF Mode = 3 */
1825 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1826 rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
1827 rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
1828 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
1829 rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7d77);
1830 rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x0);
1832 /* open PA S1 & close SMIXER */
1833 rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
1834 rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x60fbd);
1837 rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
1839 /* path-B IQK setting */
1840 rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
1841 rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
1842 rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1843 rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
1845 rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82110000);
1846 rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x2816001f);
1847 rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
1848 rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
1850 /* LO calibration setting */
1851 rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a8d1);
1852 /* enter IQK mode */
1853 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
1855 /* One shot, path B LOK & IQK */
1856 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
1857 rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
1859 mdelay(IQK_DELAY_TIME);
1861 /* leave IQK mode */
1862 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
1864 reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
1865 reg_ea4 = rtl_get_bbreg(hw, RRX_POWER_BEFORE_IQK_A_2, MASKDWORD);
1867 /* Allen 20131125 */
1868 tmp = (reg_eac & 0x03FF0000) >> 16;
1869 if ((tmp & 0x200) > 0)
1872 /* if Tx is OK, check whether Rx is OK */
1873 if (!(reg_eac & BIT(27)) &&
1874 (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
1875 (((reg_eac & 0x03FF0000) >> 16) != 0x36))
1877 else if (!(reg_eac & BIT(27)) &&
1878 (((reg_ea4 & 0x03FF0000) >> 16) < 0x110) &&
1879 (((reg_ea4 & 0x03FF0000) >> 16) > 0xf0) &&
1888 static void _rtl8723be_phy_path_b_fill_iqk_matrix(struct ieee80211_hw *hw,
1894 u32 oldval_1, x, tx1_a, reg;
1897 if (final_candidate == 0xFF) {
1899 } else if (b_iqk_ok) {
1900 oldval_1 = (rtl_get_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
1901 MASKDWORD) >> 22) & 0x3FF;
1902 x = result[final_candidate][4];
1903 if ((x & 0x00000200) != 0)
1905 tx1_a = (x * oldval_1) >> 8;
1906 rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x3FF, tx1_a);
1907 rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(27),
1908 ((x * oldval_1 >> 7) & 0x1));
1909 y = result[final_candidate][5];
1910 if ((y & 0x00000200) != 0)
1912 tx1_c = (y * oldval_1) >> 8;
1913 rtl_set_bbreg(hw, ROFDM0_XDTXAFE, 0xF0000000,
1914 ((tx1_c & 0x3C0) >> 6));
1915 rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x003F0000,
1917 rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(25),
1918 ((y * oldval_1 >> 7) & 0x1));
1921 reg = result[final_candidate][6];
1922 rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0x3FF, reg);
1923 reg = result[final_candidate][7] & 0x3F;
1924 rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0xFC00, reg);
1925 reg = (result[final_candidate][7] >> 6) & 0xF;
1926 /* rtl_set_bbreg(hw, 0xca0, 0xF0000000, reg); */
1930 static bool _rtl8723be_phy_simularity_compare(struct ieee80211_hw *hw,
1931 long result[][8], u8 c1, u8 c2)
1933 u32 i, j, diff, simularity_bitmap, bound = 0;
1935 u8 final_candidate[2] = {0xFF, 0xFF}; /* for path A and path B */
1936 bool bresult = true; /* is2t = true*/
1937 s32 tmp1 = 0, tmp2 = 0;
1941 simularity_bitmap = 0;
1943 for (i = 0; i < bound; i++) {
1944 if ((i == 1) || (i == 3) || (i == 5) || (i == 7)) {
1945 if ((result[c1][i] & 0x00000200) != 0)
1946 tmp1 = result[c1][i] | 0xFFFFFC00;
1948 tmp1 = result[c1][i];
1950 if ((result[c2][i] & 0x00000200) != 0)
1951 tmp2 = result[c2][i] | 0xFFFFFC00;
1953 tmp2 = result[c2][i];
1955 tmp1 = result[c1][i];
1956 tmp2 = result[c2][i];
1959 diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
1961 if (diff > MAX_TOLERANCE) {
1962 if ((i == 2 || i == 6) && !simularity_bitmap) {
1963 if (result[c1][i] + result[c1][i + 1] == 0)
1964 final_candidate[(i / 4)] = c2;
1965 else if (result[c2][i] + result[c2][i + 1] == 0)
1966 final_candidate[(i / 4)] = c1;
1968 simularity_bitmap |= (1 << i);
1970 simularity_bitmap |= (1 << i);
1974 if (simularity_bitmap == 0) {
1975 for (i = 0; i < (bound / 4); i++) {
1976 if (final_candidate[i] != 0xFF) {
1977 for (j = i * 4; j < (i + 1) * 4 - 2; j++)
1979 result[final_candidate[i]][j];
1985 if (!(simularity_bitmap & 0x03)) { /* path A TX OK */
1986 for (i = 0; i < 2; i++)
1987 result[3][i] = result[c1][i];
1989 if (!(simularity_bitmap & 0x0c)) { /* path A RX OK */
1990 for (i = 2; i < 4; i++)
1991 result[3][i] = result[c1][i];
1993 if (!(simularity_bitmap & 0x30)) { /* path B TX OK */
1994 for (i = 4; i < 6; i++)
1995 result[3][i] = result[c1][i];
1997 if (!(simularity_bitmap & 0xc0)) { /* path B RX OK */
1998 for (i = 6; i < 8; i++)
1999 result[3][i] = result[c1][i];
2005 static void _rtl8723be_phy_iq_calibrate(struct ieee80211_hw *hw,
2006 long result[][8], u8 t, bool is2t)
2008 struct rtl_priv *rtlpriv = rtl_priv(hw);
2009 struct rtl_phy *rtlphy = &rtlpriv->phy;
2011 u8 patha_ok, pathb_ok;
2012 u32 adda_reg[IQK_ADDA_REG_NUM] = {
2013 0x85c, 0xe6c, 0xe70, 0xe74,
2014 0xe78, 0xe7c, 0xe80, 0xe84,
2015 0xe88, 0xe8c, 0xed0, 0xed4,
2016 0xed8, 0xedc, 0xee0, 0xeec
2019 u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
2020 0x522, 0x550, 0x551, 0x040
2022 u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
2023 ROFDM0_TRXPATHENABLE, ROFDM0_TRMUXPAR,
2024 RFPGA0_XCD_RFINTERFACESW, 0xb68, 0xb6c,
2028 const u32 retrycount = 2;
2030 u32 path_sel_bb;/* path_sel_rf */
2032 u8 tmp_reg_c50, tmp_reg_c58;
2034 tmp_reg_c50 = rtl_get_bbreg(hw, 0xc50, MASKBYTE0);
2035 tmp_reg_c58 = rtl_get_bbreg(hw, 0xc58, MASKBYTE0);
2038 rtl8723_save_adda_registers(hw, adda_reg,
2039 rtlphy->adda_backup, 16);
2040 rtl8723_phy_save_mac_registers(hw, iqk_mac_reg,
2041 rtlphy->iqk_mac_backup);
2042 rtl8723_save_adda_registers(hw, iqk_bb_reg,
2043 rtlphy->iqk_bb_backup,
2046 rtl8723_phy_path_adda_on(hw, adda_reg, true, is2t);
2048 rtlphy->rfpi_enable = (u8)rtl_get_bbreg(hw,
2049 RFPGA0_XA_HSSIPARAMETER1,
2053 path_sel_bb = rtl_get_bbreg(hw, 0x948, MASKDWORD);
2055 rtl8723_phy_mac_setting_calibration(hw, iqk_mac_reg,
2056 rtlphy->iqk_mac_backup);
2058 rtl_set_bbreg(hw, 0xa04, 0x0f000000, 0xf);
2059 rtl_set_bbreg(hw, 0xc04, MASKDWORD, 0x03a05600);
2060 rtl_set_bbreg(hw, 0xc08, MASKDWORD, 0x000800e4);
2061 rtl_set_bbreg(hw, 0x874, MASKDWORD, 0x22204000);
2064 for (i = 0; i < retrycount; i++) {
2065 patha_ok = _rtl8723be_phy_path_a_iqk(hw);
2066 if (patha_ok == 0x01) {
2067 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2068 "Path A Tx IQK Success!!\n");
2069 result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
2071 result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
2075 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2076 "Path A Tx IQK Fail!!\n");
2080 for (i = 0; i < retrycount; i++) {
2081 patha_ok = _rtl8723be_phy_path_a_rx_iqk(hw);
2082 if (patha_ok == 0x03) {
2083 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2084 "Path A Rx IQK Success!!\n");
2085 result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
2087 result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
2091 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2092 "Path A Rx IQK Fail!!\n");
2095 if (0x00 == patha_ok)
2096 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "Path A IQK Fail!!\n");
2100 for (i = 0; i < retrycount; i++) {
2101 pathb_ok = _rtl8723be_phy_path_b_iqk(hw);
2102 if (pathb_ok == 0x01) {
2103 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2104 "Path B Tx IQK Success!!\n");
2105 result[t][4] = (rtl_get_bbreg(hw, 0xe94,
2108 result[t][5] = (rtl_get_bbreg(hw, 0xe9c,
2113 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2114 "Path B Tx IQK Fail!!\n");
2117 for (i = 0; i < retrycount; i++) {
2118 pathb_ok = _rtl8723be_phy_path_b_rx_iqk(hw);
2119 if (pathb_ok == 0x03) {
2120 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2121 "Path B Rx IQK Success!!\n");
2122 result[t][6] = (rtl_get_bbreg(hw, 0xea4,
2125 result[t][7] = (rtl_get_bbreg(hw, 0xeac,
2130 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2131 "Path B Rx IQK Fail!!\n");
2135 /* Back to BB mode, load original value */
2136 rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0);
2139 rtl8723_phy_reload_adda_registers(hw, adda_reg,
2140 rtlphy->adda_backup, 16);
2141 rtl8723_phy_reload_mac_registers(hw, iqk_mac_reg,
2142 rtlphy->iqk_mac_backup);
2143 rtl8723_phy_reload_adda_registers(hw, iqk_bb_reg,
2144 rtlphy->iqk_bb_backup,
2147 rtl_set_bbreg(hw, 0x948, MASKDWORD, path_sel_bb);
2148 /*rtl_set_rfreg(hw, RF90_PATH_B, 0xb0, 0xfffff, path_sel_rf);*/
2150 rtl_set_bbreg(hw, 0xc50, MASKBYTE0, 0x50);
2151 rtl_set_bbreg(hw, 0xc50, MASKBYTE0, tmp_reg_c50);
2153 rtl_set_bbreg(hw, 0xc58, MASKBYTE0, 0x50);
2154 rtl_set_bbreg(hw, 0xc58, MASKBYTE0, tmp_reg_c58);
2156 rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x01008c00);
2157 rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x01008c00);
2159 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "8723be IQK Finish!!\n");
2162 static u8 _get_right_chnl_place_for_iqk(u8 chnl)
2164 static const u8 channel_all[TARGET_CHNL_NUM_2G_5G] = {
2165 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2166 13, 14, 36, 38, 40, 42, 44, 46,
2167 48, 50, 52, 54, 56, 58, 60, 62, 64,
2168 100, 102, 104, 106, 108, 110,
2169 112, 114, 116, 118, 120, 122,
2170 124, 126, 128, 130, 132, 134, 136,
2171 138, 140, 149, 151, 153, 155, 157,
2177 for (place = 14; place < sizeof(channel_all); place++) {
2178 if (channel_all[place] == chnl)
2185 static void _rtl8723be_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
2188 u32 rf_a_mode = 0, rf_b_mode = 0;
2189 struct rtl_priv *rtlpriv = rtl_priv(hw);
2191 tmpreg = rtl_read_byte(rtlpriv, 0xd03);
2193 if ((tmpreg & 0x70) != 0)
2194 rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
2196 rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
2198 if ((tmpreg & 0x70) != 0) {
2199 rf_a_mode = rtl_get_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS);
2202 rf_b_mode = rtl_get_rfreg(hw, RF90_PATH_B, 0x00,
2205 rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS,
2206 (rf_a_mode & 0x8FFFF) | 0x10000);
2209 rtl_set_rfreg(hw, RF90_PATH_B, 0x00, MASK12BITS,
2210 (rf_b_mode & 0x8FFFF) | 0x10000);
2212 rtl_get_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS);
2214 rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, RFREG_OFFSET_MASK, 0xdfbe0);
2215 rtl_set_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS, 0x8c0a);
2217 /* In order not to disturb BT music when wifi init.(1ant NIC only) */
2219 /* In order not to disturb BT music when wifi init.(1ant NIC only) */
2222 rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, RFREG_OFFSET_MASK, 0xdffe0);
2224 if ((tmpreg & 0x70) != 0) {
2225 rtl_write_byte(rtlpriv, 0xd03, tmpreg);
2226 rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS, rf_a_mode);
2229 rtl_set_rfreg(hw, RF90_PATH_B, 0x00,
2230 MASK12BITS, rf_b_mode);
2232 rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
2234 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
2237 static void _rtl8723be_phy_set_rfpath_switch(struct ieee80211_hw *hw,
2238 bool bmain, bool is2t)
2240 struct rtl_priv *rtlpriv = rtl_priv(hw);
2241 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
2243 if (bmain) /* left antenna */
2244 rtl_set_bbreg(hw, 0x92C, MASKDWORD, 0x1);
2246 rtl_set_bbreg(hw, 0x92C, MASKDWORD, 0x2);
2249 #undef IQK_ADDA_REG_NUM
2250 #undef IQK_DELAY_TIME
2251 /* IQK is merge from Merge Temp */
2252 void rtl8723be_phy_iq_calibrate(struct ieee80211_hw *hw, bool b_recovery)
2254 struct rtl_priv *rtlpriv = rtl_priv(hw);
2255 struct rtl_phy *rtlphy = &rtlpriv->phy;
2257 u8 i, final_candidate, idx;
2258 bool b_patha_ok, b_pathb_ok;
2259 long reg_e94, reg_e9c, reg_ea4, reg_eb4, reg_ebc, reg_ec4;
2261 bool is12simular, is13simular, is23simular;
2262 u32 iqk_bb_reg[9] = {
2263 ROFDM0_XARXIQIMBALANCE,
2264 ROFDM0_XBRXIQIMBALANCE,
2265 ROFDM0_ECCATHRESHOLD,
2266 ROFDM0_AGCRSSITABLE,
2267 ROFDM0_XATXIQIMBALANCE,
2268 ROFDM0_XBTXIQIMBALANCE,
2273 u32 path_sel_bb = 0; /* path_sel_rf = 0 */
2275 if (rtlphy->lck_inprogress)
2278 spin_lock(&rtlpriv->locks.iqk_lock);
2279 rtlphy->lck_inprogress = true;
2280 spin_unlock(&rtlpriv->locks.iqk_lock);
2283 rtl8723_phy_reload_adda_registers(hw, iqk_bb_reg,
2284 rtlphy->iqk_bb_backup, 9);
2288 path_sel_bb = rtl_get_bbreg(hw, 0x948, MASKDWORD);
2289 /* path_sel_rf = rtl_get_rfreg(hw, RF90_PATH_A, 0xb0, 0xfffff); */
2291 for (i = 0; i < 8; i++) {
2297 final_candidate = 0xff;
2300 is12simular = false;
2301 is23simular = false;
2302 is13simular = false;
2303 for (i = 0; i < 3; i++) {
2304 _rtl8723be_phy_iq_calibrate(hw, result, i, true);
2306 is12simular = _rtl8723be_phy_simularity_compare(hw,
2310 final_candidate = 0;
2315 is13simular = _rtl8723be_phy_simularity_compare(hw,
2319 final_candidate = 0;
2322 is23simular = _rtl8723be_phy_simularity_compare(hw,
2326 final_candidate = 1;
2328 for (i = 0; i < 8; i++)
2329 reg_tmp += result[3][i];
2332 final_candidate = 3;
2334 final_candidate = 0xFF;
2338 for (i = 0; i < 4; i++) {
2339 reg_e94 = result[i][0];
2340 reg_e9c = result[i][1];
2341 reg_ea4 = result[i][2];
2342 reg_eb4 = result[i][4];
2343 reg_ebc = result[i][5];
2344 reg_ec4 = result[i][6];
2346 if (final_candidate != 0xff) {
2347 reg_e94 = result[final_candidate][0];
2348 rtlphy->reg_e94 = reg_e94;
2349 reg_e9c = result[final_candidate][1];
2350 rtlphy->reg_e9c = reg_e9c;
2351 reg_ea4 = result[final_candidate][2];
2352 reg_eb4 = result[final_candidate][4];
2353 rtlphy->reg_eb4 = reg_eb4;
2354 reg_ebc = result[final_candidate][5];
2355 rtlphy->reg_ebc = reg_ebc;
2356 reg_ec4 = result[final_candidate][6];
2360 rtlphy->reg_e94 = 0x100;
2361 rtlphy->reg_eb4 = 0x100;
2362 rtlphy->reg_e9c = 0x0;
2363 rtlphy->reg_ebc = 0x0;
2366 rtl8723_phy_path_a_fill_iqk_matrix(hw, b_patha_ok, result,
2370 _rtl8723be_phy_path_b_fill_iqk_matrix(hw, b_pathb_ok, result,
2374 idx = _get_right_chnl_place_for_iqk(rtlphy->current_channel);
2376 if (final_candidate < 4) {
2377 for (i = 0; i < IQK_MATRIX_REG_NUM; i++)
2378 rtlphy->iqk_matrix[idx].value[0][i] =
2379 result[final_candidate][i];
2380 rtlphy->iqk_matrix[idx].iqk_done = true;
2383 rtl8723_save_adda_registers(hw, iqk_bb_reg,
2384 rtlphy->iqk_bb_backup, 9);
2386 rtl_set_bbreg(hw, 0x948, MASKDWORD, path_sel_bb);
2387 /* rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, 0xfffff, path_sel_rf); */
2390 spin_lock(&rtlpriv->locks.iqk_lock);
2391 rtlphy->lck_inprogress = false;
2392 spin_unlock(&rtlpriv->locks.iqk_lock);
2395 void rtl8723be_phy_lc_calibrate(struct ieee80211_hw *hw)
2397 struct rtl_priv *rtlpriv = rtl_priv(hw);
2398 struct rtl_phy *rtlphy = &rtlpriv->phy;
2399 struct rtl_hal *rtlhal = &rtlpriv->rtlhal;
2400 u32 timeout = 2000, timecount = 0;
2402 while (rtlpriv->mac80211.act_scanning && timecount < timeout) {
2407 rtlphy->lck_inprogress = true;
2408 RTPRINT(rtlpriv, FINIT, INIT_IQK,
2409 "LCK:Start!!! currentband %x delay %d ms\n",
2410 rtlhal->current_bandtype, timecount);
2412 _rtl8723be_phy_lc_calibrate(hw, false);
2414 rtlphy->lck_inprogress = false;
2417 void rtl8723be_phy_set_rfpath_switch(struct ieee80211_hw *hw, bool bmain)
2419 _rtl8723be_phy_set_rfpath_switch(hw, bmain, true);
2422 bool rtl8723be_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
2424 struct rtl_priv *rtlpriv = rtl_priv(hw);
2425 struct rtl_phy *rtlphy = &rtlpriv->phy;
2426 bool b_postprocessing = false;
2428 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
2429 "-->IO Cmd(%#x), set_io_inprogress(%d)\n",
2430 iotype, rtlphy->set_io_inprogress);
2433 case IO_CMD_RESUME_DM_BY_SCAN:
2434 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
2435 "[IO CMD] Resume DM after scan.\n");
2436 b_postprocessing = true;
2438 case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
2439 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
2440 "[IO CMD] Pause DM before scan.\n");
2441 b_postprocessing = true;
2444 rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
2445 "switch case %#x not processed\n", iotype);
2449 if (b_postprocessing && !rtlphy->set_io_inprogress) {
2450 rtlphy->set_io_inprogress = true;
2451 rtlphy->current_io_type = iotype;
2455 rtl8723be_phy_set_io(hw);
2456 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE, "IO Type(%#x)\n", iotype);
2460 static void rtl8723be_phy_set_io(struct ieee80211_hw *hw)
2462 struct rtl_priv *rtlpriv = rtl_priv(hw);
2463 struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
2464 struct rtl_phy *rtlphy = &rtlpriv->phy;
2466 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
2467 "--->Cmd(%#x), set_io_inprogress(%d)\n",
2468 rtlphy->current_io_type, rtlphy->set_io_inprogress);
2469 switch (rtlphy->current_io_type) {
2470 case IO_CMD_RESUME_DM_BY_SCAN:
2471 dm_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
2472 /*rtl92c_dm_write_dig(hw);*/
2473 rtl8723be_phy_set_txpower_level(hw, rtlphy->current_channel);
2474 rtl_set_bbreg(hw, RCCK0_CCA, 0xff0000, 0x83);
2476 case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
2477 rtlphy->initgain_backup.xaagccore1 = dm_digtable->cur_igvalue;
2478 dm_digtable->cur_igvalue = 0x17;
2479 rtl_set_bbreg(hw, RCCK0_CCA, 0xff0000, 0x40);
2482 rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
2483 "switch case %#x not processed\n",
2484 rtlphy->current_io_type);
2487 rtlphy->set_io_inprogress = false;
2488 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
2489 "(%#x)\n", rtlphy->current_io_type);
2492 static void rtl8723be_phy_set_rf_on(struct ieee80211_hw *hw)
2494 struct rtl_priv *rtlpriv = rtl_priv(hw);
2496 rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
2497 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
2498 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
2499 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
2500 rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
2503 static void _rtl8723be_phy_set_rf_sleep(struct ieee80211_hw *hw)
2505 struct rtl_priv *rtlpriv = rtl_priv(hw);
2507 rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
2508 rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
2509 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
2510 rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
2513 static bool _rtl8723be_phy_set_rf_power_state(struct ieee80211_hw *hw,
2514 enum rf_pwrstate rfpwr_state)
2516 struct rtl_priv *rtlpriv = rtl_priv(hw);
2517 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2518 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2519 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2520 bool bresult = true;
2522 struct rtl8192_tx_ring *ring = NULL;
2524 switch (rfpwr_state) {
2526 if ((ppsc->rfpwr_state == ERFOFF) &&
2527 RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
2529 u32 initializecount = 0;
2532 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
2533 "IPS Set eRf nic enable\n");
2534 rtstatus = rtl_ps_enable_nic(hw);
2535 } while (!rtstatus && (initializecount < 10));
2536 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
2538 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
2539 "Set ERFON slept:%d ms\n",
2540 jiffies_to_msecs(jiffies -
2541 ppsc->last_sleep_jiffies));
2542 ppsc->last_awake_jiffies = jiffies;
2543 rtl8723be_phy_set_rf_on(hw);
2545 if (mac->link_state == MAC80211_LINKED)
2546 rtlpriv->cfg->ops->led_control(hw, LED_CTL_LINK);
2548 rtlpriv->cfg->ops->led_control(hw, LED_CTL_NO_LINK);
2553 for (queue_id = 0, i = 0;
2554 queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
2555 ring = &pcipriv->dev.tx_ring[queue_id];
2556 /* Don't check BEACON Q.
2557 * BEACON Q is always not empty,
2558 * because '_rtl8723be_cmd_send_packet'
2560 if (queue_id == BEACON_QUEUE ||
2561 skb_queue_len(&ring->queue) == 0) {
2565 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
2566 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
2568 skb_queue_len(&ring->queue));
2573 if (i >= MAX_DOZE_WAITING_TIMES_9x) {
2574 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
2575 "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
2576 MAX_DOZE_WAITING_TIMES_9x,
2578 skb_queue_len(&ring->queue));
2583 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
2584 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
2585 "IPS Set eRf nic disable\n");
2586 rtl_ps_disable_nic(hw);
2587 RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
2589 if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS) {
2590 rtlpriv->cfg->ops->led_control(hw,
2593 rtlpriv->cfg->ops->led_control(hw,
2600 if (ppsc->rfpwr_state == ERFOFF)
2602 for (queue_id = 0, i = 0;
2603 queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
2604 ring = &pcipriv->dev.tx_ring[queue_id];
2605 if (skb_queue_len(&ring->queue) == 0) {
2609 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
2610 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
2612 skb_queue_len(&ring->queue));
2617 if (i >= MAX_DOZE_WAITING_TIMES_9x) {
2618 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
2619 "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
2620 MAX_DOZE_WAITING_TIMES_9x,
2622 skb_queue_len(&ring->queue));
2626 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
2627 "Set ERFSLEEP awaked:%d ms\n",
2628 jiffies_to_msecs(jiffies -
2629 ppsc->last_awake_jiffies));
2630 ppsc->last_sleep_jiffies = jiffies;
2631 _rtl8723be_phy_set_rf_sleep(hw);
2635 rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
2636 "switch case %#x not processed\n", rfpwr_state);
2641 ppsc->rfpwr_state = rfpwr_state;
2645 bool rtl8723be_phy_set_rf_power_state(struct ieee80211_hw *hw,
2646 enum rf_pwrstate rfpwr_state)
2648 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2650 bool bresult = false;
2652 if (rfpwr_state == ppsc->rfpwr_state)
2654 bresult = _rtl8723be_phy_set_rf_power_state(hw, rfpwr_state);