rf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 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 _rtl8723e_phy_rf6052_config_parafile(struct ieee80211_hw *hw);
  32. void rtl8723e_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) | 0x0400);
  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));
  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 rtl8723e_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. if (rtlefuse->eeprom_regulatory != 0)
  67. turbo_scanoff = true;
  68. if (mac->act_scanning == true) {
  69. tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
  70. tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
  71. if (turbo_scanoff) {
  72. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B;
  73. idx1++) {
  74. tx_agc[idx1] = ppowerlevel[idx1] |
  75. (ppowerlevel[idx1] << 8) |
  76. (ppowerlevel[idx1] << 16) |
  77. (ppowerlevel[idx1] << 24);
  78. }
  79. }
  80. } else {
  81. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  82. tx_agc[idx1] = ppowerlevel[idx1] |
  83. (ppowerlevel[idx1] << 8) |
  84. (ppowerlevel[idx1] << 16) |
  85. (ppowerlevel[idx1] << 24);
  86. }
  87. if (rtlefuse->eeprom_regulatory == 0) {
  88. tmpval =
  89. (rtlphy->mcs_txpwrlevel_origoffset[0][6]) +
  90. (rtlphy->mcs_txpwrlevel_origoffset[0][7] <<
  91. 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. tmpval = tx_agc[RF90_PATH_A] & 0xff;
  108. rtl_set_bbreg(hw, RTXAGC_A_CCK1_MCS32, MASKBYTE1, tmpval);
  109. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  110. "CCK PWR 1M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
  111. RTXAGC_A_CCK1_MCS32);
  112. tmpval = tx_agc[RF90_PATH_A] >> 8;
  113. tmpval = tmpval & 0xff00ffff;
  114. rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, 0xffffff00, tmpval);
  115. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  116. "CCK PWR 2~11M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
  117. RTXAGC_B_CCK11_A_CCK2_11);
  118. tmpval = tx_agc[RF90_PATH_B] >> 24;
  119. rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, MASKBYTE0, tmpval);
  120. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  121. "CCK PWR 11M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
  122. RTXAGC_B_CCK11_A_CCK2_11);
  123. tmpval = tx_agc[RF90_PATH_B] & 0x00ffffff;
  124. rtl_set_bbreg(hw, RTXAGC_B_CCK1_55_MCS32, 0xffffff00, tmpval);
  125. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  126. "CCK PWR 1~5.5M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
  127. RTXAGC_B_CCK1_55_MCS32);
  128. }
  129. static void rtl8723e_phy_get_power_base(struct ieee80211_hw *hw,
  130. u8 *ppowerlevel, u8 channel,
  131. u32 *ofdmbase, u32 *mcsbase)
  132. {
  133. struct rtl_priv *rtlpriv = rtl_priv(hw);
  134. struct rtl_phy *rtlphy = &rtlpriv->phy;
  135. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  136. u32 powerbase0, powerbase1;
  137. u8 legacy_pwrdiff, ht20_pwrdiff;
  138. u8 i, powerlevel[2];
  139. for (i = 0; i < 2; i++) {
  140. powerlevel[i] = ppowerlevel[i];
  141. legacy_pwrdiff = rtlefuse->txpwr_legacyhtdiff[i][channel - 1];
  142. powerbase0 = powerlevel[i] + legacy_pwrdiff;
  143. powerbase0 = (powerbase0 << 24) | (powerbase0 << 16) |
  144. (powerbase0 << 8) | powerbase0;
  145. *(ofdmbase + i) = powerbase0;
  146. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  147. " [OFDM power base index rf(%c) = 0x%x]\n",
  148. ((i == 0) ? 'A' : 'B'), *(ofdmbase + i));
  149. }
  150. for (i = 0; i < 2; i++) {
  151. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20) {
  152. ht20_pwrdiff =
  153. rtlefuse->txpwr_ht20diff[i][channel - 1];
  154. powerlevel[i] += ht20_pwrdiff;
  155. }
  156. powerbase1 = powerlevel[i];
  157. powerbase1 = (powerbase1 << 24) |
  158. (powerbase1 << 16) | (powerbase1 << 8) | powerbase1;
  159. *(mcsbase + i) = powerbase1;
  160. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  161. " [MCS power base index rf(%c) = 0x%x]\n",
  162. ((i == 0) ? 'A' : 'B'), *(mcsbase + i));
  163. }
  164. }
  165. static void get_txpower_writeval_by_reg(struct ieee80211_hw *hw,
  166. u8 channel, u8 index,
  167. u32 *powerbase0,
  168. u32 *powerbase1,
  169. u32 *p_outwriteval)
  170. {
  171. struct rtl_priv *rtlpriv = rtl_priv(hw);
  172. struct rtl_phy *rtlphy = &rtlpriv->phy;
  173. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  174. u8 i, chnlgroup = 0, pwr_diff_limit[4];
  175. u32 writeval, customer_limit, rf;
  176. for (rf = 0; rf < 2; rf++) {
  177. switch (rtlefuse->eeprom_regulatory) {
  178. case 0:
  179. chnlgroup = 0;
  180. writeval =
  181. rtlphy->mcs_txpwrlevel_origoffset[chnlgroup][index +
  182. (rf ? 8 : 0)]
  183. + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
  184. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  185. "RTK better performance, writeval(%c) = 0x%x\n",
  186. ((rf == 0) ? 'A' : 'B'), writeval);
  187. break;
  188. case 1:
  189. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  190. writeval = ((index < 2) ? powerbase0[rf] :
  191. powerbase1[rf]);
  192. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  193. "Realtek regulatory, 40MHz, writeval(%c) = 0x%x\n",
  194. ((rf == 0) ? 'A' : 'B'), writeval);
  195. } else {
  196. if (rtlphy->pwrgroup_cnt == 1)
  197. chnlgroup = 0;
  198. if (rtlphy->pwrgroup_cnt >= 3) {
  199. if (channel <= 3)
  200. chnlgroup = 0;
  201. else if (channel >= 4 && channel <= 9)
  202. chnlgroup = 1;
  203. else if (channel > 9)
  204. chnlgroup = 2;
  205. if (rtlphy->current_chan_bw ==
  206. HT_CHANNEL_WIDTH_20)
  207. chnlgroup++;
  208. else
  209. chnlgroup += 4;
  210. }
  211. writeval =
  212. rtlphy->mcs_txpwrlevel_origoffset[chnlgroup]
  213. [index + (rf ? 8 : 0)] + ((index < 2) ?
  214. powerbase0[rf] :
  215. powerbase1[rf]);
  216. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  217. "Realtek regulatory, 20MHz, writeval(%c) = 0x%x\n",
  218. ((rf == 0) ? 'A' : 'B'), writeval);
  219. }
  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[rf][channel -
  235. 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[rf][channel -
  241. 1]);
  242. }
  243. for (i = 0; i < 4; i++) {
  244. pwr_diff_limit[i] =
  245. (u8)((rtlphy->mcs_txpwrlevel_origoffset
  246. [chnlgroup][index +
  247. (rf ? 8 : 0)] & (0x7f <<
  248. (i * 8))) >> (i * 8));
  249. if (rtlphy->current_chan_bw ==
  250. HT_CHANNEL_WIDTH_20_40) {
  251. if (pwr_diff_limit[i] >
  252. rtlefuse->
  253. pwrgroup_ht40[rf][channel - 1])
  254. pwr_diff_limit[i] =
  255. rtlefuse->pwrgroup_ht40[rf]
  256. [channel - 1];
  257. } else {
  258. if (pwr_diff_limit[i] >
  259. rtlefuse->
  260. pwrgroup_ht20[rf][channel - 1])
  261. pwr_diff_limit[i] =
  262. rtlefuse->pwrgroup_ht20[rf]
  263. [channel - 1];
  264. }
  265. }
  266. customer_limit = (pwr_diff_limit[3] << 24) |
  267. (pwr_diff_limit[2] << 16) |
  268. (pwr_diff_limit[1] << 8) | (pwr_diff_limit[0]);
  269. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  270. "Customer's limit rf(%c) = 0x%x\n",
  271. ((rf == 0) ? 'A' : 'B'), customer_limit);
  272. writeval = customer_limit +
  273. ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
  274. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  275. "Customer, writeval rf(%c)= 0x%x\n",
  276. ((rf == 0) ? 'A' : 'B'), writeval);
  277. break;
  278. default:
  279. chnlgroup = 0;
  280. writeval =
  281. rtlphy->mcs_txpwrlevel_origoffset[chnlgroup]
  282. [index + (rf ? 8 : 0)]
  283. + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
  284. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  285. "RTK better performance, writeval rf(%c) = 0x%x\n",
  286. ((rf == 0) ? 'A' : 'B'), writeval);
  287. break;
  288. }
  289. if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
  290. writeval = writeval - 0x06060606;
  291. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  292. TXHIGHPWRLEVEL_BT2)
  293. writeval = writeval - 0x0c0c0c0c;
  294. *(p_outwriteval + rf) = writeval;
  295. }
  296. }
  297. static void _rtl8723e_write_ofdm_power_reg(struct ieee80211_hw *hw,
  298. u8 index, u32 *pvalue)
  299. {
  300. struct rtl_priv *rtlpriv = rtl_priv(hw);
  301. struct rtl_phy *rtlphy = &rtlpriv->phy;
  302. u16 regoffset_a[6] = {
  303. RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
  304. RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
  305. RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
  306. };
  307. u16 regoffset_b[6] = {
  308. RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
  309. RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
  310. RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
  311. };
  312. u8 i, rf, pwr_val[4];
  313. u32 writeval;
  314. u16 regoffset;
  315. for (rf = 0; rf < 2; rf++) {
  316. writeval = pvalue[rf];
  317. for (i = 0; i < 4; i++) {
  318. pwr_val[i] = (u8)((writeval & (0x7f <<
  319. (i * 8))) >> (i * 8));
  320. if (pwr_val[i] > RF6052_MAX_TX_PWR)
  321. pwr_val[i] = RF6052_MAX_TX_PWR;
  322. }
  323. writeval = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
  324. (pwr_val[1] << 8) | pwr_val[0];
  325. if (rf == 0)
  326. regoffset = regoffset_a[index];
  327. else
  328. regoffset = regoffset_b[index];
  329. rtl_set_bbreg(hw, regoffset, MASKDWORD, writeval);
  330. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  331. "Set 0x%x = %08x\n", regoffset, writeval);
  332. if (((get_rf_type(rtlphy) == RF_2T2R) &&
  333. (regoffset == RTXAGC_A_MCS15_MCS12 ||
  334. regoffset == RTXAGC_B_MCS15_MCS12)) ||
  335. ((get_rf_type(rtlphy) != RF_2T2R) &&
  336. (regoffset == RTXAGC_A_MCS07_MCS04 ||
  337. regoffset == RTXAGC_B_MCS07_MCS04))) {
  338. writeval = pwr_val[3];
  339. if (regoffset == RTXAGC_A_MCS15_MCS12 ||
  340. regoffset == RTXAGC_A_MCS07_MCS04)
  341. regoffset = 0xc90;
  342. if (regoffset == RTXAGC_B_MCS15_MCS12 ||
  343. regoffset == RTXAGC_B_MCS07_MCS04)
  344. regoffset = 0xc98;
  345. for (i = 0; i < 3; i++) {
  346. writeval = (writeval > 6) ? (writeval - 6) : 0;
  347. rtl_write_byte(rtlpriv, (u32) (regoffset + i),
  348. (u8)writeval);
  349. }
  350. }
  351. }
  352. }
  353. void rtl8723e_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
  354. u8 *ppowerlevel, u8 channel)
  355. {
  356. u32 writeval[2], powerbase0[2], powerbase1[2];
  357. u8 index;
  358. rtl8723e_phy_get_power_base(hw, ppowerlevel,
  359. channel, &powerbase0[0], &powerbase1[0]);
  360. for (index = 0; index < 6; index++) {
  361. get_txpower_writeval_by_reg(hw, channel, index, &powerbase0[0],
  362. &powerbase1[0],
  363. &writeval[0]);
  364. _rtl8723e_write_ofdm_power_reg(hw, index, &writeval[0]);
  365. }
  366. }
  367. bool rtl8723e_phy_rf6052_config(struct ieee80211_hw *hw)
  368. {
  369. struct rtl_priv *rtlpriv = rtl_priv(hw);
  370. struct rtl_phy *rtlphy = &rtlpriv->phy;
  371. if (rtlphy->rf_type == RF_1T1R)
  372. rtlphy->num_total_rfpath = 1;
  373. else
  374. rtlphy->num_total_rfpath = 2;
  375. return _rtl8723e_phy_rf6052_config_parafile(hw);
  376. }
  377. static bool _rtl8723e_phy_rf6052_config_parafile(struct ieee80211_hw *hw)
  378. {
  379. struct rtl_priv *rtlpriv = rtl_priv(hw);
  380. struct rtl_phy *rtlphy = &rtlpriv->phy;
  381. u32 u4_regvalue = 0;
  382. u8 rfpath;
  383. bool rtstatus = true;
  384. struct bb_reg_def *pphyreg;
  385. for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
  386. pphyreg = &rtlphy->phyreg_def[rfpath];
  387. switch (rfpath) {
  388. case RF90_PATH_A:
  389. case RF90_PATH_C:
  390. u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
  391. BRFSI_RFENV);
  392. break;
  393. case RF90_PATH_B:
  394. case RF90_PATH_D:
  395. u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
  396. BRFSI_RFENV << 16);
  397. break;
  398. }
  399. rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
  400. udelay(1);
  401. rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
  402. udelay(1);
  403. rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
  404. B3WIREADDREAALENGTH, 0x0);
  405. udelay(1);
  406. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
  407. udelay(1);
  408. switch (rfpath) {
  409. case RF90_PATH_A:
  410. rtstatus = rtl8723e_phy_config_rf_with_headerfile(hw,
  411. (enum radio_path)rfpath);
  412. break;
  413. case RF90_PATH_B:
  414. rtstatus =
  415. rtl8723e_phy_config_rf_with_headerfile(hw,
  416. (enum radio_path)rfpath);
  417. break;
  418. case RF90_PATH_C:
  419. break;
  420. case RF90_PATH_D:
  421. break;
  422. }
  423. switch (rfpath) {
  424. case RF90_PATH_A:
  425. case RF90_PATH_C:
  426. rtl_set_bbreg(hw, pphyreg->rfintfs,
  427. BRFSI_RFENV, u4_regvalue);
  428. break;
  429. case RF90_PATH_B:
  430. case RF90_PATH_D:
  431. rtl_set_bbreg(hw, pphyreg->rfintfs,
  432. BRFSI_RFENV << 16, u4_regvalue);
  433. break;
  434. }
  435. if (rtstatus != true) {
  436. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  437. "Radio[%d] Fail!!", rfpath);
  438. return false;
  439. }
  440. }
  441. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "\n");
  442. return rtstatus;
  443. }