rf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. static bool _rtl92ce_phy_rf6052_config_parafile(struct ieee80211_hw *hw);
  36. void rtl92ce_phy_rf6052_set_bandwidth(struct ieee80211_hw *hw, u8 bandwidth)
  37. {
  38. struct rtl_priv *rtlpriv = rtl_priv(hw);
  39. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  40. switch (bandwidth) {
  41. case HT_CHANNEL_WIDTH_20:
  42. rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
  43. 0xfffff3ff) | 0x0400);
  44. rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
  45. rtlphy->rfreg_chnlval[0]);
  46. break;
  47. case HT_CHANNEL_WIDTH_20_40:
  48. rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
  49. 0xfffff3ff));
  50. rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
  51. rtlphy->rfreg_chnlval[0]);
  52. break;
  53. default:
  54. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  55. "unknown bandwidth: %#X\n", bandwidth);
  56. break;
  57. }
  58. }
  59. void rtl92ce_phy_rf6052_set_cck_txpower(struct ieee80211_hw *hw,
  60. u8 *ppowerlevel)
  61. {
  62. struct rtl_priv *rtlpriv = rtl_priv(hw);
  63. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  64. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  65. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  66. u32 tx_agc[2] = {0, 0}, tmpval;
  67. bool turbo_scanoff = false;
  68. u8 idx1, idx2;
  69. u8 *ptr;
  70. if (rtlefuse->eeprom_regulatory != 0)
  71. turbo_scanoff = true;
  72. if (mac->act_scanning) {
  73. tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
  74. tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
  75. if (turbo_scanoff) {
  76. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  77. tx_agc[idx1] = ppowerlevel[idx1] |
  78. (ppowerlevel[idx1] << 8) |
  79. (ppowerlevel[idx1] << 16) |
  80. (ppowerlevel[idx1] << 24);
  81. }
  82. }
  83. } else {
  84. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  85. tx_agc[idx1] = ppowerlevel[idx1] |
  86. (ppowerlevel[idx1] << 8) |
  87. (ppowerlevel[idx1] << 16) |
  88. (ppowerlevel[idx1] << 24);
  89. }
  90. if (rtlefuse->eeprom_regulatory == 0) {
  91. tmpval = (rtlphy->mcs_offset[0][6]) +
  92. (rtlphy->mcs_offset[0][7] << 8);
  93. tx_agc[RF90_PATH_A] += tmpval;
  94. tmpval = (rtlphy->mcs_offset[0][14]) +
  95. (rtlphy->mcs_offset[0][15] << 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",
  111. tmpval, 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",
  117. tmpval, 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",
  122. tmpval, 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",
  127. tmpval, RTXAGC_B_CCK1_55_MCS32);
  128. }
  129. static void rtl92c_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 = rtlefuse->txpwr_ht20diff[i][channel - 1];
  153. powerlevel[i] += ht20_pwrdiff;
  154. }
  155. powerBase1 = powerlevel[i];
  156. powerBase1 = (powerBase1 << 24) |
  157. (powerBase1 << 16) | (powerBase1 << 8) | powerBase1;
  158. *(mcsbase + i) = powerBase1;
  159. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  160. " [MCS power base index rf(%c) = 0x%x]\n",
  161. i == 0 ? 'A' : 'B', *(mcsbase + i));
  162. }
  163. }
  164. static void _rtl92c_get_txpower_writeval_by_regulatory(struct ieee80211_hw *hw,
  165. u8 channel, u8 index,
  166. u32 *powerBase0,
  167. u32 *powerBase1,
  168. u32 *p_outwriteval)
  169. {
  170. struct rtl_priv *rtlpriv = rtl_priv(hw);
  171. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  172. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  173. u8 i, chnlgroup = 0, pwr_diff_limit[4];
  174. u32 writeVal, customer_limit, rf;
  175. for (rf = 0; rf < 2; rf++) {
  176. switch (rtlefuse->eeprom_regulatory) {
  177. case 0:
  178. chnlgroup = 0;
  179. writeVal = rtlphy->mcs_offset[chnlgroup][index +
  180. (rf ? 8 : 0)]
  181. + ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
  182. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  183. "RTK better performance, writeVal(%c) = 0x%x\n",
  184. rf == 0 ? 'A' : 'B', writeVal);
  185. break;
  186. case 1:
  187. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  188. writeVal = ((index < 2) ? powerBase0[rf] :
  189. powerBase1[rf]);
  190. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  191. "Realtek regulatory, 40MHz, writeVal(%c) = 0x%x\n",
  192. rf == 0 ? 'A' : 'B', writeVal);
  193. } else {
  194. if (rtlphy->pwrgroup_cnt == 1)
  195. chnlgroup = 0;
  196. if (rtlphy->pwrgroup_cnt >= 3) {
  197. if (channel <= 3)
  198. chnlgroup = 0;
  199. else if (channel >= 4 && channel <= 9)
  200. chnlgroup = 1;
  201. else if (channel > 9)
  202. chnlgroup = 2;
  203. if (rtlphy->pwrgroup_cnt == 4)
  204. chnlgroup++;
  205. }
  206. writeVal = rtlphy->mcs_offset[chnlgroup]
  207. [index + (rf ? 8 : 0)] + ((index < 2) ?
  208. powerBase0[rf] :
  209. powerBase1[rf]);
  210. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  211. "Realtek regulatory, 20MHz, writeVal(%c) = 0x%x\n",
  212. rf == 0 ? 'A' : 'B', writeVal);
  213. }
  214. break;
  215. case 2:
  216. writeVal =
  217. ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
  218. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  219. "Better regulatory, writeVal(%c) = 0x%x\n",
  220. rf == 0 ? 'A' : 'B', writeVal);
  221. break;
  222. case 3:
  223. chnlgroup = 0;
  224. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  225. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  226. "customer's limit, 40MHz rf(%c) = 0x%x\n",
  227. rf == 0 ? 'A' : 'B',
  228. rtlefuse->pwrgroup_ht40[rf][channel -
  229. 1]);
  230. } else {
  231. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  232. "customer's limit, 20MHz rf(%c) = 0x%x\n",
  233. rf == 0 ? 'A' : 'B',
  234. rtlefuse->pwrgroup_ht20[rf][channel -
  235. 1]);
  236. }
  237. for (i = 0; i < 4; i++) {
  238. pwr_diff_limit[i] = (u8) ((rtlphy->mcs_offset
  239. [chnlgroup][index +
  240. (rf ? 8 : 0)] & (0x7f << (i * 8))) >>
  241. (i * 8));
  242. if (rtlphy->current_chan_bw ==
  243. HT_CHANNEL_WIDTH_20_40) {
  244. if (pwr_diff_limit[i] >
  245. rtlefuse->
  246. pwrgroup_ht40[rf][channel - 1])
  247. pwr_diff_limit[i] =
  248. rtlefuse->pwrgroup_ht40[rf]
  249. [channel - 1];
  250. } else {
  251. if (pwr_diff_limit[i] >
  252. rtlefuse->
  253. pwrgroup_ht20[rf][channel - 1])
  254. pwr_diff_limit[i] =
  255. rtlefuse->pwrgroup_ht20[rf]
  256. [channel - 1];
  257. }
  258. }
  259. customer_limit = (pwr_diff_limit[3] << 24) |
  260. (pwr_diff_limit[2] << 16) |
  261. (pwr_diff_limit[1] << 8) | (pwr_diff_limit[0]);
  262. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  263. "Customer's limit rf(%c) = 0x%x\n",
  264. rf == 0 ? 'A' : 'B', customer_limit);
  265. writeVal = customer_limit +
  266. ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
  267. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  268. "Customer, writeVal rf(%c)= 0x%x\n",
  269. rf == 0 ? 'A' : 'B', writeVal);
  270. break;
  271. default:
  272. chnlgroup = 0;
  273. writeVal = rtlphy->mcs_offset[chnlgroup]
  274. [index + (rf ? 8 : 0)]
  275. + ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
  276. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  277. "RTK better performance, writeVal rf(%c) = 0x%x\n",
  278. rf == 0 ? 'A' : 'B', writeVal);
  279. break;
  280. }
  281. if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
  282. writeVal = writeVal - 0x06060606;
  283. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  284. TXHIGHPWRLEVEL_BT2)
  285. writeVal = writeVal - 0x0c0c0c0c;
  286. *(p_outwriteval + rf) = writeVal;
  287. }
  288. }
  289. static void _rtl92c_write_ofdm_power_reg(struct ieee80211_hw *hw,
  290. u8 index, u32 *pValue)
  291. {
  292. struct rtl_priv *rtlpriv = rtl_priv(hw);
  293. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  294. u16 regoffset_a[6] = {
  295. RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
  296. RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
  297. RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
  298. };
  299. u16 regoffset_b[6] = {
  300. RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
  301. RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
  302. RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
  303. };
  304. u8 i, rf, pwr_val[4];
  305. u32 writeVal;
  306. u16 regoffset;
  307. for (rf = 0; rf < 2; rf++) {
  308. writeVal = pValue[rf];
  309. for (i = 0; i < 4; i++) {
  310. pwr_val[i] = (u8) ((writeVal & (0x7f <<
  311. (i * 8))) >> (i * 8));
  312. if (pwr_val[i] > RF6052_MAX_TX_PWR)
  313. pwr_val[i] = RF6052_MAX_TX_PWR;
  314. }
  315. writeVal = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
  316. (pwr_val[1] << 8) | pwr_val[0];
  317. if (rf == 0)
  318. regoffset = regoffset_a[index];
  319. else
  320. regoffset = regoffset_b[index];
  321. rtl_set_bbreg(hw, regoffset, MASKDWORD, writeVal);
  322. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  323. "Set 0x%x = %08x\n", regoffset, writeVal);
  324. if (((get_rf_type(rtlphy) == RF_2T2R) &&
  325. (regoffset == RTXAGC_A_MCS15_MCS12 ||
  326. regoffset == RTXAGC_B_MCS15_MCS12)) ||
  327. ((get_rf_type(rtlphy) != RF_2T2R) &&
  328. (regoffset == RTXAGC_A_MCS07_MCS04 ||
  329. regoffset == RTXAGC_B_MCS07_MCS04))) {
  330. writeVal = pwr_val[3];
  331. if (regoffset == RTXAGC_A_MCS15_MCS12 ||
  332. regoffset == RTXAGC_A_MCS07_MCS04)
  333. regoffset = 0xc90;
  334. if (regoffset == RTXAGC_B_MCS15_MCS12 ||
  335. regoffset == RTXAGC_B_MCS07_MCS04)
  336. regoffset = 0xc98;
  337. for (i = 0; i < 3; i++) {
  338. writeVal = (writeVal > 6) ? (writeVal - 6) : 0;
  339. rtl_write_byte(rtlpriv, (u32) (regoffset + i),
  340. (u8) writeVal);
  341. }
  342. }
  343. }
  344. }
  345. void rtl92ce_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
  346. u8 *ppowerlevel, u8 channel)
  347. {
  348. u32 writeVal[2], powerBase0[2], powerBase1[2];
  349. u8 index;
  350. rtl92c_phy_get_power_base(hw, ppowerlevel,
  351. channel, &powerBase0[0], &powerBase1[0]);
  352. for (index = 0; index < 6; index++) {
  353. _rtl92c_get_txpower_writeval_by_regulatory(hw,
  354. channel, index,
  355. &powerBase0[0],
  356. &powerBase1[0],
  357. &writeVal[0]);
  358. _rtl92c_write_ofdm_power_reg(hw, index, &writeVal[0]);
  359. }
  360. }
  361. bool rtl92ce_phy_rf6052_config(struct ieee80211_hw *hw)
  362. {
  363. struct rtl_priv *rtlpriv = rtl_priv(hw);
  364. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  365. if (rtlphy->rf_type == RF_1T1R)
  366. rtlphy->num_total_rfpath = 1;
  367. else
  368. rtlphy->num_total_rfpath = 2;
  369. return _rtl92ce_phy_rf6052_config_parafile(hw);
  370. }
  371. static bool _rtl92ce_phy_rf6052_config_parafile(struct ieee80211_hw *hw)
  372. {
  373. struct rtl_priv *rtlpriv = rtl_priv(hw);
  374. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  375. u32 u4_regvalue = 0;
  376. u8 rfpath;
  377. bool rtstatus = true;
  378. struct bb_reg_def *pphyreg;
  379. for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
  380. pphyreg = &rtlphy->phyreg_def[rfpath];
  381. switch (rfpath) {
  382. case RF90_PATH_A:
  383. case RF90_PATH_C:
  384. u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
  385. BRFSI_RFENV);
  386. break;
  387. case RF90_PATH_B:
  388. case RF90_PATH_D:
  389. u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
  390. BRFSI_RFENV << 16);
  391. break;
  392. }
  393. rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
  394. udelay(1);
  395. rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
  396. udelay(1);
  397. rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
  398. B3WIREADDREAALENGTH, 0x0);
  399. udelay(1);
  400. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
  401. udelay(1);
  402. switch (rfpath) {
  403. case RF90_PATH_A:
  404. rtstatus = rtl92c_phy_config_rf_with_headerfile(hw,
  405. (enum radio_path)rfpath);
  406. break;
  407. case RF90_PATH_B:
  408. rtstatus = rtl92c_phy_config_rf_with_headerfile(hw,
  409. (enum radio_path)rfpath);
  410. break;
  411. case RF90_PATH_C:
  412. break;
  413. case RF90_PATH_D:
  414. break;
  415. }
  416. switch (rfpath) {
  417. case RF90_PATH_A:
  418. case RF90_PATH_C:
  419. rtl_set_bbreg(hw, pphyreg->rfintfs,
  420. BRFSI_RFENV, u4_regvalue);
  421. break;
  422. case RF90_PATH_B:
  423. case RF90_PATH_D:
  424. rtl_set_bbreg(hw, pphyreg->rfintfs,
  425. BRFSI_RFENV << 16, u4_regvalue);
  426. break;
  427. }
  428. if (!rtstatus) {
  429. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  430. "Radio[%d] Fail!!\n", rfpath);
  431. return false;
  432. }
  433. }
  434. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "<---\n");
  435. return rtstatus;
  436. }