rf.c 14 KB

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