rf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2014 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. #include "../wifi.h"
  26. #include "reg.h"
  27. #include "def.h"
  28. #include "phy.h"
  29. #include "rf.h"
  30. #include "dm.h"
  31. static bool _rtl8723be_phy_rf6052_config_parafile(struct ieee80211_hw *hw);
  32. void rtl8723be_phy_rf6052_set_bandwidth(struct ieee80211_hw *hw, u8 bandwidth)
  33. {
  34. struct rtl_priv *rtlpriv = rtl_priv(hw);
  35. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  36. switch (bandwidth) {
  37. case HT_CHANNEL_WIDTH_20:
  38. rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
  39. 0xfffff3ff) | BIT(10) | BIT(11));
  40. rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
  41. rtlphy->rfreg_chnlval[0]);
  42. break;
  43. case HT_CHANNEL_WIDTH_20_40:
  44. rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
  45. 0xfffff3ff) | BIT(10));
  46. rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
  47. rtlphy->rfreg_chnlval[0]);
  48. break;
  49. default:
  50. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  51. "unknown bandwidth: %#X\n", bandwidth);
  52. break;
  53. }
  54. }
  55. void rtl8723be_phy_rf6052_set_cck_txpower(struct ieee80211_hw *hw,
  56. u8 *ppowerlevel)
  57. {
  58. struct rtl_priv *rtlpriv = rtl_priv(hw);
  59. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  60. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  61. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  62. u32 tx_agc[2] = {0, 0}, tmpval;
  63. bool turbo_scanoff = false;
  64. u8 idx1, idx2;
  65. u8 *ptr;
  66. u8 direction;
  67. u32 pwrtrac_value;
  68. if (rtlefuse->eeprom_regulatory != 0)
  69. turbo_scanoff = true;
  70. if (mac->act_scanning) {
  71. tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
  72. tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
  73. if (turbo_scanoff) {
  74. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  75. tx_agc[idx1] = ppowerlevel[idx1] |
  76. (ppowerlevel[idx1] << 8) |
  77. (ppowerlevel[idx1] << 16) |
  78. (ppowerlevel[idx1] << 24);
  79. }
  80. }
  81. } else {
  82. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  83. tx_agc[idx1] = ppowerlevel[idx1] |
  84. (ppowerlevel[idx1] << 8) |
  85. (ppowerlevel[idx1] << 16) |
  86. (ppowerlevel[idx1] << 24);
  87. }
  88. if (rtlefuse->eeprom_regulatory == 0) {
  89. tmpval =
  90. (rtlphy->mcs_txpwrlevel_origoffset[0][6]) +
  91. (rtlphy->mcs_txpwrlevel_origoffset[0][7] << 8);
  92. tx_agc[RF90_PATH_A] += tmpval;
  93. tmpval = (rtlphy->mcs_txpwrlevel_origoffset[0][14]) +
  94. (rtlphy->mcs_txpwrlevel_origoffset[0][15] <<
  95. 24);
  96. tx_agc[RF90_PATH_B] += tmpval;
  97. }
  98. }
  99. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  100. ptr = (u8 *)(&(tx_agc[idx1]));
  101. for (idx2 = 0; idx2 < 4; idx2++) {
  102. if (*ptr > RF6052_MAX_TX_PWR)
  103. *ptr = RF6052_MAX_TX_PWR;
  104. ptr++;
  105. }
  106. }
  107. rtl8723be_dm_txpower_track_adjust(hw, 1, &direction, &pwrtrac_value);
  108. if (direction == 1) {
  109. tx_agc[0] += pwrtrac_value;
  110. tx_agc[1] += pwrtrac_value;
  111. } else if (direction == 2) {
  112. tx_agc[0] -= pwrtrac_value;
  113. tx_agc[1] -= pwrtrac_value;
  114. }
  115. tmpval = tx_agc[RF90_PATH_A] & 0xff;
  116. rtl_set_bbreg(hw, RTXAGC_A_CCK1_MCS32, MASKBYTE1, tmpval);
  117. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  118. "CCK PWR 1M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
  119. RTXAGC_A_CCK1_MCS32);
  120. tmpval = tx_agc[RF90_PATH_A] >> 8;
  121. /*tmpval = tmpval & 0xff00ffff;*/
  122. rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, 0xffffff00, tmpval);
  123. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  124. "CCK PWR 2~11M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
  125. RTXAGC_B_CCK11_A_CCK2_11);
  126. tmpval = tx_agc[RF90_PATH_B] >> 24;
  127. rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, MASKBYTE0, tmpval);
  128. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  129. "CCK PWR 11M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
  130. RTXAGC_B_CCK11_A_CCK2_11);
  131. tmpval = tx_agc[RF90_PATH_B] & 0x00ffffff;
  132. rtl_set_bbreg(hw, RTXAGC_B_CCK1_55_MCS32, 0xffffff00, tmpval);
  133. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  134. "CCK PWR 1~5.5M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
  135. RTXAGC_B_CCK1_55_MCS32);
  136. }
  137. static void rtl8723be_phy_get_power_base(struct ieee80211_hw *hw,
  138. u8 *ppowerlevel_ofdm,
  139. u8 *ppowerlevel_bw20,
  140. u8 *ppowerlevel_bw40,
  141. u8 channel, u32 *ofdmbase,
  142. u32 *mcsbase)
  143. {
  144. struct rtl_priv *rtlpriv = rtl_priv(hw);
  145. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  146. u32 powerbase0, powerbase1;
  147. u8 i, powerlevel[2];
  148. for (i = 0; i < 2; i++) {
  149. powerbase0 = ppowerlevel_ofdm[i];
  150. powerbase0 = (powerbase0 << 24) | (powerbase0 << 16) |
  151. (powerbase0 << 8) | powerbase0;
  152. *(ofdmbase + i) = powerbase0;
  153. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  154. " [OFDM power base index rf(%c) = 0x%x]\n",
  155. ((i == 0) ? 'A' : 'B'), *(ofdmbase + i));
  156. }
  157. for (i = 0; i < 2; i++) {
  158. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20)
  159. powerlevel[i] = ppowerlevel_bw20[i];
  160. else
  161. powerlevel[i] = ppowerlevel_bw40[i];
  162. powerbase1 = powerlevel[i];
  163. powerbase1 = (powerbase1 << 24) | (powerbase1 << 16) |
  164. (powerbase1 << 8) | powerbase1;
  165. *(mcsbase + i) = powerbase1;
  166. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  167. " [MCS power base index rf(%c) = 0x%x]\n",
  168. ((i == 0) ? 'A' : 'B'), *(mcsbase + i));
  169. }
  170. }
  171. static void _rtl8723be_get_txpower_writeval_by_regulatory(
  172. struct ieee80211_hw *hw,
  173. u8 channel, u8 index,
  174. u32 *powerbase0,
  175. u32 *powerbase1,
  176. u32 *p_outwriteval)
  177. {
  178. struct rtl_priv *rtlpriv = rtl_priv(hw);
  179. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  180. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  181. u8 i, chnlgroup = 0, pwr_diff_limit[4], pwr_diff = 0, customer_pwr_diff;
  182. u32 writeval, customer_limit, rf;
  183. for (rf = 0; rf < 2; rf++) {
  184. switch (rtlefuse->eeprom_regulatory) {
  185. case 0:
  186. chnlgroup = 0;
  187. writeval =
  188. rtlphy->mcs_txpwrlevel_origoffset[chnlgroup][index +
  189. (rf ? 8 : 0)]
  190. + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
  191. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  192. "RTK better performance, writeval(%c) = 0x%x\n",
  193. ((rf == 0) ? 'A' : 'B'), writeval);
  194. break;
  195. case 1:
  196. if (rtlphy->pwrgroup_cnt == 1) {
  197. chnlgroup = 0;
  198. } else {
  199. if (channel < 3)
  200. chnlgroup = 0;
  201. else if (channel < 6)
  202. chnlgroup = 1;
  203. else if (channel < 9)
  204. chnlgroup = 2;
  205. else if (channel < 12)
  206. chnlgroup = 3;
  207. else if (channel < 14)
  208. chnlgroup = 4;
  209. else if (channel == 14)
  210. chnlgroup = 5;
  211. }
  212. writeval =
  213. rtlphy->mcs_txpwrlevel_origoffset[chnlgroup]
  214. [index + (rf ? 8 : 0)] + ((index < 2) ?
  215. powerbase0[rf] :
  216. powerbase1[rf]);
  217. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  218. "Realtek regulatory, 20MHz, writeval(%c) = 0x%x\n",
  219. ((rf == 0) ? 'A' : 'B'), writeval);
  220. break;
  221. case 2:
  222. writeval =
  223. ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
  224. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  225. "Better regulatory, writeval(%c) = 0x%x\n",
  226. ((rf == 0) ? 'A' : 'B'), writeval);
  227. break;
  228. case 3:
  229. chnlgroup = 0;
  230. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  231. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  232. "customer's limit, 40MHz rf(%c) = 0x%x\n",
  233. ((rf == 0) ? 'A' : 'B'),
  234. rtlefuse->pwrgroup_ht40
  235. [rf][channel - 1]);
  236. } else {
  237. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  238. "customer's limit, 20MHz rf(%c) = 0x%x\n",
  239. ((rf == 0) ? 'A' : 'B'),
  240. rtlefuse->pwrgroup_ht20
  241. [rf][channel - 1]);
  242. }
  243. if (index < 2)
  244. pwr_diff =
  245. rtlefuse->txpwr_legacyhtdiff[rf][channel-1];
  246. else if (rtlphy->current_chan_bw ==
  247. HT_CHANNEL_WIDTH_20)
  248. pwr_diff =
  249. rtlefuse->txpwr_ht20diff[rf][channel-1];
  250. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40)
  251. customer_pwr_diff =
  252. rtlefuse->pwrgroup_ht40[rf][channel-1];
  253. else
  254. customer_pwr_diff =
  255. rtlefuse->pwrgroup_ht20[rf][channel-1];
  256. if (pwr_diff > customer_pwr_diff)
  257. pwr_diff = 0;
  258. else
  259. pwr_diff = customer_pwr_diff - pwr_diff;
  260. for (i = 0; i < 4; i++) {
  261. pwr_diff_limit[i] =
  262. (u8)((rtlphy->mcs_txpwrlevel_origoffset
  263. [chnlgroup][index + (rf ? 8 : 0)] &
  264. (0x7f << (i * 8))) >> (i * 8));
  265. if (pwr_diff_limit[i] > pwr_diff)
  266. pwr_diff_limit[i] = pwr_diff;
  267. }
  268. customer_limit = (pwr_diff_limit[3] << 24) |
  269. (pwr_diff_limit[2] << 16) |
  270. (pwr_diff_limit[1] << 8) |
  271. (pwr_diff_limit[0]);
  272. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  273. "Customer's limit rf(%c) = 0x%x\n",
  274. ((rf == 0) ? 'A' : 'B'), customer_limit);
  275. writeval = customer_limit + ((index < 2) ?
  276. powerbase0[rf] :
  277. powerbase1[rf]);
  278. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  279. "Customer, writeval rf(%c)= 0x%x\n",
  280. ((rf == 0) ? 'A' : 'B'), writeval);
  281. break;
  282. default:
  283. chnlgroup = 0;
  284. writeval =
  285. rtlphy->mcs_txpwrlevel_origoffset[chnlgroup]
  286. [index + (rf ? 8 : 0)]
  287. + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
  288. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  289. "RTK better performance, writeval rf(%c) = 0x%x\n",
  290. ((rf == 0) ? 'A' : 'B'), writeval);
  291. break;
  292. }
  293. if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
  294. writeval = writeval - 0x06060606;
  295. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  296. TXHIGHPWRLEVEL_BT2)
  297. writeval = writeval - 0x0c0c0c0c;
  298. *(p_outwriteval + rf) = writeval;
  299. }
  300. }
  301. static void _rtl8723be_write_ofdm_power_reg(struct ieee80211_hw *hw,
  302. u8 index, u32 *pvalue)
  303. {
  304. struct rtl_priv *rtlpriv = rtl_priv(hw);
  305. u16 regoffset_a[6] = {
  306. RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
  307. RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
  308. RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
  309. };
  310. u16 regoffset_b[6] = {
  311. RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
  312. RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
  313. RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
  314. };
  315. u8 i, rf, pwr_val[4];
  316. u32 writeval;
  317. u16 regoffset;
  318. for (rf = 0; rf < 2; rf++) {
  319. writeval = pvalue[rf];
  320. for (i = 0; i < 4; i++) {
  321. pwr_val[i] = (u8)((writeval & (0x7f <<
  322. (i * 8))) >> (i * 8));
  323. if (pwr_val[i] > RF6052_MAX_TX_PWR)
  324. pwr_val[i] = RF6052_MAX_TX_PWR;
  325. }
  326. writeval = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
  327. (pwr_val[1] << 8) | pwr_val[0];
  328. if (rf == 0)
  329. regoffset = regoffset_a[index];
  330. else
  331. regoffset = regoffset_b[index];
  332. rtl_set_bbreg(hw, regoffset, MASKDWORD, writeval);
  333. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  334. "Set 0x%x = %08x\n", regoffset, writeval);
  335. }
  336. }
  337. void rtl8723be_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
  338. u8 *ppowerlevel_ofdm,
  339. u8 *ppowerlevel_bw20,
  340. u8 *ppowerlevel_bw40, u8 channel)
  341. {
  342. u32 writeval[2], powerbase0[2], powerbase1[2];
  343. u8 index;
  344. u8 direction;
  345. u32 pwrtrac_value;
  346. rtl8723be_phy_get_power_base(hw, ppowerlevel_ofdm, ppowerlevel_bw20,
  347. ppowerlevel_bw40, channel,
  348. &powerbase0[0], &powerbase1[0]);
  349. rtl8723be_dm_txpower_track_adjust(hw, 1, &direction, &pwrtrac_value);
  350. for (index = 0; index < 6; index++) {
  351. _rtl8723be_get_txpower_writeval_by_regulatory(hw,
  352. channel, index,
  353. &powerbase0[0],
  354. &powerbase1[0],
  355. &writeval[0]);
  356. if (direction == 1) {
  357. writeval[0] += pwrtrac_value;
  358. writeval[1] += pwrtrac_value;
  359. } else if (direction == 2) {
  360. writeval[0] -= pwrtrac_value;
  361. writeval[1] -= pwrtrac_value;
  362. }
  363. _rtl8723be_write_ofdm_power_reg(hw, index, &writeval[0]);
  364. }
  365. }
  366. bool rtl8723be_phy_rf6052_config(struct ieee80211_hw *hw)
  367. {
  368. struct rtl_priv *rtlpriv = rtl_priv(hw);
  369. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  370. if (rtlphy->rf_type == RF_1T1R)
  371. rtlphy->num_total_rfpath = 1;
  372. else
  373. rtlphy->num_total_rfpath = 2;
  374. return _rtl8723be_phy_rf6052_config_parafile(hw);
  375. }
  376. static bool _rtl8723be_phy_rf6052_config_parafile(struct ieee80211_hw *hw)
  377. {
  378. struct rtl_priv *rtlpriv = rtl_priv(hw);
  379. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  380. u32 u4_regvalue = 0;
  381. u8 rfpath;
  382. bool rtstatus = true;
  383. struct bb_reg_def *pphyreg;
  384. for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
  385. pphyreg = &rtlphy->phyreg_def[rfpath];
  386. switch (rfpath) {
  387. case RF90_PATH_A:
  388. case RF90_PATH_C:
  389. u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
  390. BRFSI_RFENV);
  391. break;
  392. case RF90_PATH_B:
  393. case RF90_PATH_D:
  394. u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
  395. BRFSI_RFENV << 16);
  396. break;
  397. }
  398. rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
  399. udelay(1);
  400. rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
  401. udelay(1);
  402. rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
  403. B3WIREADDREAALENGTH, 0x0);
  404. udelay(1);
  405. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
  406. udelay(1);
  407. switch (rfpath) {
  408. case RF90_PATH_A:
  409. rtstatus = rtl8723be_phy_config_rf_with_headerfile(hw,
  410. (enum radio_path)rfpath);
  411. break;
  412. case RF90_PATH_B:
  413. rtstatus = rtl8723be_phy_config_rf_with_headerfile(hw,
  414. (enum radio_path)rfpath);
  415. break;
  416. case RF90_PATH_C:
  417. break;
  418. case RF90_PATH_D:
  419. break;
  420. }
  421. switch (rfpath) {
  422. case RF90_PATH_A:
  423. case RF90_PATH_C:
  424. rtl_set_bbreg(hw, pphyreg->rfintfs,
  425. BRFSI_RFENV, u4_regvalue);
  426. break;
  427. case RF90_PATH_B:
  428. case RF90_PATH_D:
  429. rtl_set_bbreg(hw, pphyreg->rfintfs,
  430. BRFSI_RFENV << 16, u4_regvalue);
  431. break;
  432. }
  433. if (!rtstatus) {
  434. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  435. "Radio[%d] Fail!!\n", rfpath);
  436. return false;
  437. }
  438. }
  439. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "\n");
  440. return rtstatus;
  441. }