rf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 void _rtl92s_get_powerbase(struct ieee80211_hw *hw, u8 *p_pwrlevel,
  32. u8 chnl, u32 *ofdmbase, u32 *mcsbase,
  33. u8 *p_final_pwridx)
  34. {
  35. struct rtl_priv *rtlpriv = rtl_priv(hw);
  36. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  37. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  38. u32 pwrbase0, pwrbase1;
  39. u8 legacy_pwrdiff = 0, ht20_pwrdiff = 0;
  40. u8 i, pwrlevel[4];
  41. for (i = 0; i < 2; i++)
  42. pwrlevel[i] = p_pwrlevel[i];
  43. /* We only care about the path A for legacy. */
  44. if (rtlefuse->eeprom_version < 2) {
  45. pwrbase0 = pwrlevel[0] + (rtlefuse->legacy_httxpowerdiff & 0xf);
  46. } else {
  47. legacy_pwrdiff = rtlefuse->txpwr_legacyhtdiff
  48. [RF90_PATH_A][chnl - 1];
  49. /* For legacy OFDM, tx pwr always > HT OFDM pwr.
  50. * We do not care Path B
  51. * legacy OFDM pwr diff. NO BB register
  52. * to notify HW. */
  53. pwrbase0 = pwrlevel[0] + legacy_pwrdiff;
  54. }
  55. pwrbase0 = (pwrbase0 << 24) | (pwrbase0 << 16) | (pwrbase0 << 8) |
  56. pwrbase0;
  57. *ofdmbase = pwrbase0;
  58. /* MCS rates */
  59. if (rtlefuse->eeprom_version >= 2) {
  60. /* Check HT20 to HT40 diff */
  61. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20) {
  62. for (i = 0; i < 2; i++) {
  63. /* rf-A, rf-B */
  64. /* HT 20<->40 pwr diff */
  65. ht20_pwrdiff = rtlefuse->txpwr_ht20diff
  66. [i][chnl - 1];
  67. if (ht20_pwrdiff < 8) /* 0~+7 */
  68. pwrlevel[i] += ht20_pwrdiff;
  69. else /* index8-15=-8~-1 */
  70. pwrlevel[i] -= (16 - ht20_pwrdiff);
  71. }
  72. }
  73. }
  74. /* use index of rf-A */
  75. pwrbase1 = pwrlevel[0];
  76. pwrbase1 = (pwrbase1 << 24) | (pwrbase1 << 16) | (pwrbase1 << 8) |
  77. pwrbase1;
  78. *mcsbase = pwrbase1;
  79. /* The following is for Antenna
  80. * diff from Ant-B to Ant-A */
  81. p_final_pwridx[0] = pwrlevel[0];
  82. p_final_pwridx[1] = pwrlevel[1];
  83. switch (rtlefuse->eeprom_regulatory) {
  84. case 3:
  85. /* The following is for calculation
  86. * of the power diff for Ant-B to Ant-A. */
  87. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  88. p_final_pwridx[0] += rtlefuse->pwrgroup_ht40
  89. [RF90_PATH_A][
  90. chnl - 1];
  91. p_final_pwridx[1] += rtlefuse->pwrgroup_ht40
  92. [RF90_PATH_B][
  93. chnl - 1];
  94. } else {
  95. p_final_pwridx[0] += rtlefuse->pwrgroup_ht20
  96. [RF90_PATH_A][
  97. chnl - 1];
  98. p_final_pwridx[1] += rtlefuse->pwrgroup_ht20
  99. [RF90_PATH_B][
  100. chnl - 1];
  101. }
  102. break;
  103. default:
  104. break;
  105. }
  106. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  107. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  108. "40MHz finalpwr_idx (A / B) = 0x%x / 0x%x\n",
  109. p_final_pwridx[0], p_final_pwridx[1]);
  110. } else {
  111. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  112. "20MHz finalpwr_idx (A / B) = 0x%x / 0x%x\n",
  113. p_final_pwridx[0], p_final_pwridx[1]);
  114. }
  115. }
  116. static void _rtl92s_set_antennadiff(struct ieee80211_hw *hw,
  117. u8 *p_final_pwridx)
  118. {
  119. struct rtl_priv *rtlpriv = rtl_priv(hw);
  120. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  121. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  122. s8 ant_pwr_diff = 0;
  123. u32 u4reg_val = 0;
  124. if (rtlphy->rf_type == RF_2T2R) {
  125. ant_pwr_diff = p_final_pwridx[1] - p_final_pwridx[0];
  126. /* range is from 7~-8,
  127. * index = 0x0~0xf */
  128. if (ant_pwr_diff > 7)
  129. ant_pwr_diff = 7;
  130. if (ant_pwr_diff < -8)
  131. ant_pwr_diff = -8;
  132. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  133. "Antenna Diff from RF-B to RF-A = %d (0x%x)\n",
  134. ant_pwr_diff, ant_pwr_diff & 0xf);
  135. ant_pwr_diff &= 0xf;
  136. }
  137. /* Antenna TX power difference */
  138. rtlefuse->antenna_txpwdiff[2] = 0;/* RF-D, don't care */
  139. rtlefuse->antenna_txpwdiff[1] = 0;/* RF-C, don't care */
  140. rtlefuse->antenna_txpwdiff[0] = (u8)(ant_pwr_diff); /* RF-B */
  141. u4reg_val = rtlefuse->antenna_txpwdiff[2] << 8 |
  142. rtlefuse->antenna_txpwdiff[1] << 4 |
  143. rtlefuse->antenna_txpwdiff[0];
  144. rtl_set_bbreg(hw, RFPGA0_TXGAINSTAGE, (BXBTXAGC | BXCTXAGC | BXDTXAGC),
  145. u4reg_val);
  146. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "Write BCD-Diff(0x%x) = 0x%x\n",
  147. RFPGA0_TXGAINSTAGE, u4reg_val);
  148. }
  149. static void _rtl92s_get_txpower_writeval_byregulatory(struct ieee80211_hw *hw,
  150. u8 chnl, u8 index,
  151. u32 pwrbase0,
  152. u32 pwrbase1,
  153. u32 *p_outwrite_val)
  154. {
  155. struct rtl_priv *rtlpriv = rtl_priv(hw);
  156. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  157. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  158. u8 i, chnlgroup, pwrdiff_limit[4];
  159. u32 writeval, customer_limit;
  160. /* Index 0 & 1= legacy OFDM, 2-5=HT_MCS rate */
  161. switch (rtlefuse->eeprom_regulatory) {
  162. case 0:
  163. /* Realtek better performance increase power diff
  164. * defined by Realtek for large power */
  165. chnlgroup = 0;
  166. writeval = rtlphy->mcs_offset[chnlgroup][index] +
  167. ((index < 2) ? pwrbase0 : pwrbase1);
  168. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  169. "RTK better performance, writeval = 0x%x\n", writeval);
  170. break;
  171. case 1:
  172. /* Realtek regulatory increase power diff defined
  173. * by Realtek for regulatory */
  174. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  175. writeval = ((index < 2) ? pwrbase0 : pwrbase1);
  176. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  177. "Realtek regulatory, 40MHz, writeval = 0x%x\n",
  178. writeval);
  179. } else {
  180. chnlgroup = 0;
  181. if (rtlphy->pwrgroup_cnt >= 3) {
  182. if (chnl <= 3)
  183. chnlgroup = 0;
  184. else if (chnl >= 4 && chnl <= 8)
  185. chnlgroup = 1;
  186. else if (chnl > 8)
  187. chnlgroup = 2;
  188. if (rtlphy->pwrgroup_cnt == 4)
  189. chnlgroup++;
  190. }
  191. writeval = rtlphy->mcs_offset[chnlgroup][index]
  192. + ((index < 2) ?
  193. pwrbase0 : pwrbase1);
  194. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  195. "Realtek regulatory, 20MHz, writeval = 0x%x\n",
  196. writeval);
  197. }
  198. break;
  199. case 2:
  200. /* Better regulatory don't increase any power diff */
  201. writeval = ((index < 2) ? pwrbase0 : pwrbase1);
  202. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  203. "Better regulatory, writeval = 0x%x\n", writeval);
  204. break;
  205. case 3:
  206. /* Customer defined power diff. increase power diff
  207. defined by customer. */
  208. chnlgroup = 0;
  209. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  210. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  211. "customer's limit, 40MHz = 0x%x\n",
  212. rtlefuse->pwrgroup_ht40
  213. [RF90_PATH_A][chnl - 1]);
  214. } else {
  215. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  216. "customer's limit, 20MHz = 0x%x\n",
  217. rtlefuse->pwrgroup_ht20
  218. [RF90_PATH_A][chnl - 1]);
  219. }
  220. for (i = 0; i < 4; i++) {
  221. pwrdiff_limit[i] = (u8)((rtlphy->mcs_offset
  222. [chnlgroup][index] & (0x7f << (i * 8)))
  223. >> (i * 8));
  224. if (rtlphy->current_chan_bw ==
  225. HT_CHANNEL_WIDTH_20_40) {
  226. if (pwrdiff_limit[i] >
  227. rtlefuse->pwrgroup_ht40
  228. [RF90_PATH_A][chnl - 1]) {
  229. pwrdiff_limit[i] =
  230. rtlefuse->pwrgroup_ht40
  231. [RF90_PATH_A][chnl - 1];
  232. }
  233. } else {
  234. if (pwrdiff_limit[i] >
  235. rtlefuse->pwrgroup_ht20
  236. [RF90_PATH_A][chnl - 1]) {
  237. pwrdiff_limit[i] =
  238. rtlefuse->pwrgroup_ht20
  239. [RF90_PATH_A][chnl - 1];
  240. }
  241. }
  242. }
  243. customer_limit = (pwrdiff_limit[3] << 24) |
  244. (pwrdiff_limit[2] << 16) |
  245. (pwrdiff_limit[1] << 8) |
  246. (pwrdiff_limit[0]);
  247. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  248. "Customer's limit = 0x%x\n", customer_limit);
  249. writeval = customer_limit + ((index < 2) ?
  250. pwrbase0 : pwrbase1);
  251. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  252. "Customer, writeval = 0x%x\n", writeval);
  253. break;
  254. default:
  255. chnlgroup = 0;
  256. writeval = rtlphy->mcs_offset[chnlgroup][index] +
  257. ((index < 2) ? pwrbase0 : pwrbase1);
  258. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  259. "RTK better performance, writeval = 0x%x\n", writeval);
  260. break;
  261. }
  262. if (rtlpriv->dm.dynamic_txhighpower_lvl == TX_HIGH_PWR_LEVEL_LEVEL1)
  263. writeval = 0x10101010;
  264. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  265. TX_HIGH_PWR_LEVEL_LEVEL2)
  266. writeval = 0x0;
  267. *p_outwrite_val = writeval;
  268. }
  269. static void _rtl92s_write_ofdm_powerreg(struct ieee80211_hw *hw,
  270. u8 index, u32 val)
  271. {
  272. struct rtl_priv *rtlpriv = rtl_priv(hw);
  273. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  274. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  275. u16 regoffset[6] = {0xe00, 0xe04, 0xe10, 0xe14, 0xe18, 0xe1c};
  276. u8 i, rfa_pwr[4];
  277. u8 rfa_lower_bound = 0, rfa_upper_bound = 0, rf_pwr_diff = 0;
  278. u32 writeval = val;
  279. /* If path A and Path B coexist, we must limit Path A tx power.
  280. * Protect Path B pwr over or under flow. We need to calculate
  281. * upper and lower bound of path A tx power. */
  282. if (rtlphy->rf_type == RF_2T2R) {
  283. rf_pwr_diff = rtlefuse->antenna_txpwdiff[0];
  284. /* Diff=-8~-1 */
  285. if (rf_pwr_diff >= 8) {
  286. /* Prevent underflow!! */
  287. rfa_lower_bound = 0x10 - rf_pwr_diff;
  288. /* if (rf_pwr_diff >= 0) Diff = 0-7 */
  289. } else {
  290. rfa_upper_bound = RF6052_MAX_TX_PWR - rf_pwr_diff;
  291. }
  292. }
  293. for (i = 0; i < 4; i++) {
  294. rfa_pwr[i] = (u8)((writeval & (0x7f << (i * 8))) >> (i * 8));
  295. if (rfa_pwr[i] > RF6052_MAX_TX_PWR)
  296. rfa_pwr[i] = RF6052_MAX_TX_PWR;
  297. /* If path A and Path B coexist, we must limit Path A tx power.
  298. * Protect Path B pwr over or under flow. We need to calculate
  299. * upper and lower bound of path A tx power. */
  300. if (rtlphy->rf_type == RF_2T2R) {
  301. /* Diff=-8~-1 */
  302. if (rf_pwr_diff >= 8) {
  303. /* Prevent underflow!! */
  304. if (rfa_pwr[i] < rfa_lower_bound)
  305. rfa_pwr[i] = rfa_lower_bound;
  306. /* Diff = 0-7 */
  307. } else if (rf_pwr_diff >= 1) {
  308. /* Prevent overflow */
  309. if (rfa_pwr[i] > rfa_upper_bound)
  310. rfa_pwr[i] = rfa_upper_bound;
  311. }
  312. }
  313. }
  314. writeval = (rfa_pwr[3] << 24) | (rfa_pwr[2] << 16) | (rfa_pwr[1] << 8) |
  315. rfa_pwr[0];
  316. rtl_set_bbreg(hw, regoffset[index], 0x7f7f7f7f, writeval);
  317. }
  318. void rtl92s_phy_rf6052_set_ofdmtxpower(struct ieee80211_hw *hw,
  319. u8 *p_pwrlevel, u8 chnl)
  320. {
  321. u32 writeval, pwrbase0, pwrbase1;
  322. u8 index = 0;
  323. u8 finalpwr_idx[4];
  324. _rtl92s_get_powerbase(hw, p_pwrlevel, chnl, &pwrbase0, &pwrbase1,
  325. &finalpwr_idx[0]);
  326. _rtl92s_set_antennadiff(hw, &finalpwr_idx[0]);
  327. for (index = 0; index < 6; index++) {
  328. _rtl92s_get_txpower_writeval_byregulatory(hw, chnl, index,
  329. pwrbase0, pwrbase1, &writeval);
  330. _rtl92s_write_ofdm_powerreg(hw, index, writeval);
  331. }
  332. }
  333. void rtl92s_phy_rf6052_set_ccktxpower(struct ieee80211_hw *hw, u8 pwrlevel)
  334. {
  335. struct rtl_priv *rtlpriv = rtl_priv(hw);
  336. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  337. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  338. u32 txagc = 0;
  339. bool dont_inc_cck_or_turboscanoff = false;
  340. if (((rtlefuse->eeprom_version >= 2) &&
  341. (rtlefuse->txpwr_safetyflag == 1)) ||
  342. ((rtlefuse->eeprom_version >= 2) &&
  343. (rtlefuse->eeprom_regulatory != 0)))
  344. dont_inc_cck_or_turboscanoff = true;
  345. if (mac->act_scanning) {
  346. txagc = 0x3f;
  347. if (dont_inc_cck_or_turboscanoff)
  348. txagc = pwrlevel;
  349. } else {
  350. txagc = pwrlevel;
  351. if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  352. TX_HIGH_PWR_LEVEL_LEVEL1)
  353. txagc = 0x10;
  354. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  355. TX_HIGH_PWR_LEVEL_LEVEL2)
  356. txagc = 0x0;
  357. }
  358. if (txagc > RF6052_MAX_TX_PWR)
  359. txagc = RF6052_MAX_TX_PWR;
  360. rtl_set_bbreg(hw, RTXAGC_CCK_MCS32, BTX_AGCRATECCK, txagc);
  361. }
  362. bool rtl92s_phy_rf6052_config(struct ieee80211_hw *hw)
  363. {
  364. struct rtl_priv *rtlpriv = rtl_priv(hw);
  365. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  366. u32 u4reg_val = 0;
  367. u8 rfpath;
  368. bool rtstatus = true;
  369. struct bb_reg_def *pphyreg;
  370. /* Initialize RF */
  371. for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
  372. pphyreg = &rtlphy->phyreg_def[rfpath];
  373. /* Store original RFENV control type */
  374. switch (rfpath) {
  375. case RF90_PATH_A:
  376. case RF90_PATH_C:
  377. u4reg_val = rtl92s_phy_query_bb_reg(hw,
  378. pphyreg->rfintfs,
  379. BRFSI_RFENV);
  380. break;
  381. case RF90_PATH_B:
  382. case RF90_PATH_D:
  383. u4reg_val = rtl92s_phy_query_bb_reg(hw,
  384. pphyreg->rfintfs,
  385. BRFSI_RFENV << 16);
  386. break;
  387. }
  388. /* Set RF_ENV enable */
  389. rtl92s_phy_set_bb_reg(hw, pphyreg->rfintfe,
  390. BRFSI_RFENV << 16, 0x1);
  391. /* Set RF_ENV output high */
  392. rtl92s_phy_set_bb_reg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
  393. /* Set bit number of Address and Data for RF register */
  394. rtl92s_phy_set_bb_reg(hw, pphyreg->rfhssi_para2,
  395. B3WIRE_ADDRESSLENGTH, 0x0);
  396. rtl92s_phy_set_bb_reg(hw, pphyreg->rfhssi_para2,
  397. B3WIRE_DATALENGTH, 0x0);
  398. /* Initialize RF fom connfiguration file */
  399. switch (rfpath) {
  400. case RF90_PATH_A:
  401. rtstatus = rtl92s_phy_config_rf(hw,
  402. (enum radio_path)rfpath);
  403. break;
  404. case RF90_PATH_B:
  405. rtstatus = rtl92s_phy_config_rf(hw,
  406. (enum radio_path)rfpath);
  407. break;
  408. case RF90_PATH_C:
  409. break;
  410. case RF90_PATH_D:
  411. break;
  412. }
  413. /* Restore RFENV control type */
  414. switch (rfpath) {
  415. case RF90_PATH_A:
  416. case RF90_PATH_C:
  417. rtl92s_phy_set_bb_reg(hw, pphyreg->rfintfs, BRFSI_RFENV,
  418. u4reg_val);
  419. break;
  420. case RF90_PATH_B:
  421. case RF90_PATH_D:
  422. rtl92s_phy_set_bb_reg(hw, pphyreg->rfintfs,
  423. BRFSI_RFENV << 16,
  424. u4reg_val);
  425. break;
  426. }
  427. if (!rtstatus) {
  428. pr_err("Radio[%d] Fail!!\n", rfpath);
  429. goto fail;
  430. }
  431. }
  432. return rtstatus;
  433. fail:
  434. return rtstatus;
  435. }
  436. void rtl92s_phy_rf6052_set_bandwidth(struct ieee80211_hw *hw, u8 bandwidth)
  437. {
  438. struct rtl_priv *rtlpriv = rtl_priv(hw);
  439. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  440. switch (bandwidth) {
  441. case HT_CHANNEL_WIDTH_20:
  442. rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
  443. 0xfffff3ff) | 0x0400);
  444. rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
  445. rtlphy->rfreg_chnlval[0]);
  446. break;
  447. case HT_CHANNEL_WIDTH_20_40:
  448. rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
  449. 0xfffff3ff));
  450. rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
  451. rtlphy->rfreg_chnlval[0]);
  452. break;
  453. default:
  454. pr_err("unknown bandwidth: %#X\n", bandwidth);
  455. break;
  456. }
  457. }