rf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2013 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 rtl88e_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) | BIT(10) | BIT(11));
  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) | BIT(10));
  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 rtl88e_phy_rf6052_set_cck_txpower(struct ieee80211_hw *hw,
  59. u8 *plevel)
  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. u8 direction;
  70. u32 pwrtrac_value;
  71. if (rtlefuse->eeprom_regulatory != 0)
  72. turbo_scanoff = true;
  73. if (mac->act_scanning == true) {
  74. tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
  75. tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
  76. if (turbo_scanoff) {
  77. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  78. tx_agc[idx1] = plevel[idx1] |
  79. (plevel[idx1] << 8) |
  80. (plevel[idx1] << 16) |
  81. (plevel[idx1] << 24);
  82. }
  83. }
  84. } else {
  85. for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
  86. tx_agc[idx1] = plevel[idx1] | (plevel[idx1] << 8) |
  87. (plevel[idx1] << 16) |
  88. (plevel[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. rtl88e_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. rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, 0xffffff00, tmpval);
  122. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  123. "CCK PWR 2~11M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
  124. RTXAGC_B_CCK11_A_CCK2_11);
  125. tmpval = tx_agc[RF90_PATH_B] >> 24;
  126. rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, MASKBYTE0, tmpval);
  127. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  128. "CCK PWR 11M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
  129. RTXAGC_B_CCK11_A_CCK2_11);
  130. tmpval = tx_agc[RF90_PATH_B] & 0x00ffffff;
  131. rtl_set_bbreg(hw, RTXAGC_B_CCK1_55_MCS32, 0xffffff00, tmpval);
  132. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  133. "CCK PWR 1~5.5M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
  134. RTXAGC_B_CCK1_55_MCS32);
  135. }
  136. static void rtl88e_phy_get_power_base(struct ieee80211_hw *hw,
  137. u8 *pwrlvlofdm, u8 *pwrlvlbw20,
  138. u8 *pwrlvlbw40, u8 channel,
  139. u32 *ofdmbase, u32 *mcsbase)
  140. {
  141. struct rtl_priv *rtlpriv = rtl_priv(hw);
  142. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  143. u32 base0, base1;
  144. u8 i, powerlevel[2];
  145. for (i = 0; i < 2; i++) {
  146. base0 = pwrlvlofdm[i];
  147. base0 = (base0 << 24) | (base0 << 16) |
  148. (base0 << 8) | base0;
  149. *(ofdmbase + i) = base0;
  150. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  151. "[OFDM power base index rf(%c) = 0x%x]\n",
  152. ((i == 0) ? 'A' : 'B'), *(ofdmbase + i));
  153. }
  154. for (i = 0; i < 2; i++) {
  155. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20)
  156. powerlevel[i] = pwrlvlbw20[i];
  157. else
  158. powerlevel[i] = pwrlvlbw40[i];
  159. base1 = powerlevel[i];
  160. base1 = (base1 << 24) |
  161. (base1 << 16) | (base1 << 8) | base1;
  162. *(mcsbase + i) = base1;
  163. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  164. "[MCS power base index rf(%c) = 0x%x]\n",
  165. ((i == 0) ? 'A' : 'B'), *(mcsbase + i));
  166. }
  167. }
  168. static void get_txpwr_by_reg(struct ieee80211_hw *hw, u8 chan, u8 index,
  169. u32 *base0, u32 *base1, u32 *outval)
  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, chg = 0, pwr_lim[4], pwr_diff = 0, cust_pwr_dif;
  175. u32 writeval, cust_lim, rf, tmp;
  176. u8 ch = chan - 1;
  177. u8 j;
  178. for (rf = 0; rf < 2; rf++) {
  179. j = index + (rf ? 8 : 0);
  180. tmp = ((index < 2) ? base0[rf] : base1[rf]);
  181. switch (rtlefuse->eeprom_regulatory) {
  182. case 0:
  183. chg = 0;
  184. writeval = rtlphy->mcs_offset[chg][j] + tmp;
  185. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  186. "RTK better performance, "
  187. "writeval(%c) = 0x%x\n",
  188. ((rf == 0) ? 'A' : 'B'), writeval);
  189. break;
  190. case 1:
  191. if (rtlphy->pwrgroup_cnt == 1) {
  192. chg = 0;
  193. } else {
  194. chg = chan / 3;
  195. if (chan == 14)
  196. chg = 5;
  197. }
  198. writeval = rtlphy->mcs_offset[chg][j] + tmp;
  199. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  200. "Realtek regulatory, 20MHz, writeval(%c) = 0x%x\n",
  201. ((rf == 0) ? 'A' : 'B'), writeval);
  202. break;
  203. case 2:
  204. writeval = ((index < 2) ? base0[rf] : base1[rf]);
  205. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  206. "Better regulatory, writeval(%c) = 0x%x\n",
  207. ((rf == 0) ? 'A' : 'B'), writeval);
  208. break;
  209. case 3:
  210. chg = 0;
  211. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
  212. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  213. "customer's limit, 40MHz rf(%c) = 0x%x\n",
  214. ((rf == 0) ? 'A' : 'B'),
  215. rtlefuse->pwrgroup_ht40[rf][ch]);
  216. } else {
  217. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  218. "customer's limit, 20MHz rf(%c) = 0x%x\n",
  219. ((rf == 0) ? 'A' : 'B'),
  220. rtlefuse->pwrgroup_ht20[rf][ch]);
  221. }
  222. if (index < 2)
  223. pwr_diff = rtlefuse->txpwr_legacyhtdiff[rf][ch];
  224. else if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20)
  225. pwr_diff = rtlefuse->txpwr_ht20diff[rf][ch];
  226. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40)
  227. cust_pwr_dif = rtlefuse->pwrgroup_ht40[rf][ch];
  228. else
  229. cust_pwr_dif = rtlefuse->pwrgroup_ht20[rf][ch];
  230. if (pwr_diff > cust_pwr_dif)
  231. pwr_diff = 0;
  232. else
  233. pwr_diff = cust_pwr_dif - pwr_diff;
  234. for (i = 0; i < 4; i++) {
  235. pwr_lim[i] = (u8)((rtlphy->mcs_offset[chg][j] &
  236. (0x7f << (i * 8))) >> (i * 8));
  237. if (pwr_lim[i] > pwr_diff)
  238. pwr_lim[i] = pwr_diff;
  239. }
  240. cust_lim = (pwr_lim[3] << 24) | (pwr_lim[2] << 16) |
  241. (pwr_lim[1] << 8) | (pwr_lim[0]);
  242. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  243. "Customer's limit rf(%c) = 0x%x\n",
  244. ((rf == 0) ? 'A' : 'B'), cust_lim);
  245. writeval = cust_lim + tmp;
  246. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  247. "Customer, writeval rf(%c) = 0x%x\n",
  248. ((rf == 0) ? 'A' : 'B'), writeval);
  249. break;
  250. default:
  251. chg = 0;
  252. writeval = rtlphy->mcs_offset[chg][j] + tmp;
  253. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  254. "RTK better performance, writeval "
  255. "rf(%c) = 0x%x\n",
  256. ((rf == 0) ? 'A' : 'B'), writeval);
  257. break;
  258. }
  259. if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
  260. writeval = writeval - 0x06060606;
  261. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  262. TXHIGHPWRLEVEL_BT2)
  263. writeval -= 0x0c0c0c0c;
  264. *(outval + rf) = writeval;
  265. }
  266. }
  267. static void write_ofdm_pwr(struct ieee80211_hw *hw, u8 index, u32 *pvalue)
  268. {
  269. struct rtl_priv *rtlpriv = rtl_priv(hw);
  270. u16 regoffset_a[6] = {
  271. RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
  272. RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
  273. RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
  274. };
  275. u16 regoffset_b[6] = {
  276. RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
  277. RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
  278. RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
  279. };
  280. u8 i, rf, pwr_val[4];
  281. u32 writeval;
  282. u16 regoffset;
  283. for (rf = 0; rf < 2; rf++) {
  284. writeval = pvalue[rf];
  285. for (i = 0; i < 4; i++) {
  286. pwr_val[i] = (u8) ((writeval & (0x7f <<
  287. (i * 8))) >> (i * 8));
  288. if (pwr_val[i] > RF6052_MAX_TX_PWR)
  289. pwr_val[i] = RF6052_MAX_TX_PWR;
  290. }
  291. writeval = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
  292. (pwr_val[1] << 8) | pwr_val[0];
  293. if (rf == 0)
  294. regoffset = regoffset_a[index];
  295. else
  296. regoffset = regoffset_b[index];
  297. rtl_set_bbreg(hw, regoffset, MASKDWORD, writeval);
  298. RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
  299. "Set 0x%x = %08x\n", regoffset, writeval);
  300. }
  301. }
  302. void rtl88e_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
  303. u8 *pwrlvlofdm,
  304. u8 *pwrlvlbw20,
  305. u8 *pwrlvlbw40, u8 chan)
  306. {
  307. u32 writeval[2], base0[2], base1[2];
  308. u8 index;
  309. u8 direction;
  310. u32 pwrtrac_value;
  311. rtl88e_phy_get_power_base(hw, pwrlvlofdm, pwrlvlbw20,
  312. pwrlvlbw40, chan, &base0[0],
  313. &base1[0]);
  314. rtl88e_dm_txpower_track_adjust(hw, 1, &direction, &pwrtrac_value);
  315. for (index = 0; index < 6; index++) {
  316. get_txpwr_by_reg(hw, chan, index, &base0[0], &base1[0],
  317. &writeval[0]);
  318. if (direction == 1) {
  319. writeval[0] += pwrtrac_value;
  320. writeval[1] += pwrtrac_value;
  321. } else if (direction == 2) {
  322. writeval[0] -= pwrtrac_value;
  323. writeval[1] -= pwrtrac_value;
  324. }
  325. write_ofdm_pwr(hw, index, &writeval[0]);
  326. }
  327. }
  328. static bool rf6052_conf_para(struct ieee80211_hw *hw)
  329. {
  330. struct rtl_priv *rtlpriv = rtl_priv(hw);
  331. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  332. u32 u4val = 0;
  333. u8 rfpath;
  334. bool rtstatus = true;
  335. struct bb_reg_def *pphyreg;
  336. for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
  337. pphyreg = &rtlphy->phyreg_def[rfpath];
  338. switch (rfpath) {
  339. case RF90_PATH_A:
  340. case RF90_PATH_C:
  341. u4val = rtl_get_bbreg(hw, pphyreg->rfintfs,
  342. BRFSI_RFENV);
  343. break;
  344. case RF90_PATH_B:
  345. case RF90_PATH_D:
  346. u4val = rtl_get_bbreg(hw, pphyreg->rfintfs,
  347. BRFSI_RFENV << 16);
  348. break;
  349. }
  350. rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
  351. udelay(1);
  352. rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
  353. udelay(1);
  354. rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
  355. B3WIREADDREAALENGTH, 0x0);
  356. udelay(1);
  357. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
  358. udelay(1);
  359. switch (rfpath) {
  360. case RF90_PATH_A:
  361. rtstatus = rtl88e_phy_config_rf_with_headerfile(hw,
  362. (enum radio_path)rfpath);
  363. break;
  364. case RF90_PATH_B:
  365. rtstatus = rtl88e_phy_config_rf_with_headerfile(hw,
  366. (enum radio_path)rfpath);
  367. break;
  368. case RF90_PATH_C:
  369. break;
  370. case RF90_PATH_D:
  371. break;
  372. }
  373. switch (rfpath) {
  374. case RF90_PATH_A:
  375. case RF90_PATH_C:
  376. rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV, u4val);
  377. break;
  378. case RF90_PATH_B:
  379. case RF90_PATH_D:
  380. rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16,
  381. u4val);
  382. break;
  383. }
  384. if (rtstatus != true) {
  385. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  386. "Radio[%d] Fail!!", rfpath);
  387. return false;
  388. }
  389. }
  390. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "\n");
  391. return rtstatus;
  392. }
  393. bool rtl88e_phy_rf6052_config(struct ieee80211_hw *hw)
  394. {
  395. struct rtl_priv *rtlpriv = rtl_priv(hw);
  396. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  397. if (rtlphy->rf_type == RF_1T1R)
  398. rtlphy->num_total_rfpath = 1;
  399. else
  400. rtlphy->num_total_rfpath = 2;
  401. return rf6052_conf_para(hw);
  402. }