phy.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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 "../pci.h"
  27. #include "../ps.h"
  28. #include "reg.h"
  29. #include "def.h"
  30. #include "phy.h"
  31. #include "rf.h"
  32. #include "dm.h"
  33. #include "table.h"
  34. #include "../rtl8723com/phy_common.h"
  35. static void _rtl8723e_phy_fw_rf_serial_write(struct ieee80211_hw *hw,
  36. enum radio_path rfpath, u32 offset,
  37. u32 data);
  38. static bool _rtl8723e_phy_bb8192c_config_parafile(struct ieee80211_hw *hw);
  39. static bool _rtl8723e_phy_config_mac_with_headerfile(struct ieee80211_hw *hw);
  40. static bool _rtl8723e_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
  41. u8 configtype);
  42. static bool _rtl8723e_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
  43. u8 configtype);
  44. static bool _rtl8723e_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
  45. u8 channel, u8 *stage, u8 *step,
  46. u32 *delay);
  47. static u8 _rtl8723e_phy_dbm_to_txpwr_idx(struct ieee80211_hw *hw,
  48. enum wireless_mode wirelessmode,
  49. long power_indbm);
  50. static void rtl8723e_phy_set_rf_on(struct ieee80211_hw *hw);
  51. static void rtl8723e_phy_set_io(struct ieee80211_hw *hw);
  52. u32 rtl8723e_phy_query_rf_reg(struct ieee80211_hw *hw,
  53. enum radio_path rfpath,
  54. u32 regaddr, u32 bitmask)
  55. {
  56. struct rtl_priv *rtlpriv = rtl_priv(hw);
  57. u32 original_value = 0, readback_value, bitshift;
  58. struct rtl_phy *rtlphy = &rtlpriv->phy;
  59. unsigned long flags;
  60. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  61. "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
  62. regaddr, rfpath, bitmask);
  63. spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
  64. if (rtlphy->rf_mode != RF_OP_BY_FW) {
  65. original_value = rtl8723_phy_rf_serial_read(hw,
  66. rfpath, regaddr);
  67. }
  68. bitshift = rtl8723_phy_calculate_bit_shift(bitmask);
  69. readback_value = (original_value & bitmask) >> bitshift;
  70. spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
  71. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  72. "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
  73. regaddr, rfpath, bitmask, original_value);
  74. return readback_value;
  75. }
  76. void rtl8723e_phy_set_rf_reg(struct ieee80211_hw *hw,
  77. enum radio_path rfpath,
  78. u32 regaddr, u32 bitmask, u32 data)
  79. {
  80. struct rtl_priv *rtlpriv = rtl_priv(hw);
  81. struct rtl_phy *rtlphy = &rtlpriv->phy;
  82. u32 original_value = 0, bitshift;
  83. unsigned long flags;
  84. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  85. "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
  86. regaddr, bitmask, data, rfpath);
  87. spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
  88. if (rtlphy->rf_mode != RF_OP_BY_FW) {
  89. if (bitmask != RFREG_OFFSET_MASK) {
  90. original_value = rtl8723_phy_rf_serial_read(hw,
  91. rfpath,
  92. regaddr);
  93. bitshift = rtl8723_phy_calculate_bit_shift(bitmask);
  94. data =
  95. ((original_value & (~bitmask)) |
  96. (data << bitshift));
  97. }
  98. rtl8723_phy_rf_serial_write(hw, rfpath, regaddr, data);
  99. } else {
  100. if (bitmask != RFREG_OFFSET_MASK) {
  101. bitshift = rtl8723_phy_calculate_bit_shift(bitmask);
  102. data =
  103. ((original_value & (~bitmask)) |
  104. (data << bitshift));
  105. }
  106. _rtl8723e_phy_fw_rf_serial_write(hw, rfpath, regaddr, data);
  107. }
  108. spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
  109. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  110. "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
  111. regaddr, bitmask, data, rfpath);
  112. }
  113. static void _rtl8723e_phy_fw_rf_serial_write(struct ieee80211_hw *hw,
  114. enum radio_path rfpath, u32 offset,
  115. u32 data)
  116. {
  117. WARN_ONCE(true, "rtl8723ae: _rtl8723e_phy_fw_rf_serial_write deprecated!\n");
  118. }
  119. static void _rtl8723e_phy_bb_config_1t(struct ieee80211_hw *hw)
  120. {
  121. rtl_set_bbreg(hw, RFPGA0_TXINFO, 0x3, 0x2);
  122. rtl_set_bbreg(hw, RFPGA1_TXINFO, 0x300033, 0x200022);
  123. rtl_set_bbreg(hw, RCCK0_AFESETTING, MASKBYTE3, 0x45);
  124. rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x23);
  125. rtl_set_bbreg(hw, ROFDM0_AGCPARAMETER1, 0x30, 0x1);
  126. rtl_set_bbreg(hw, 0xe74, 0x0c000000, 0x2);
  127. rtl_set_bbreg(hw, 0xe78, 0x0c000000, 0x2);
  128. rtl_set_bbreg(hw, 0xe7c, 0x0c000000, 0x2);
  129. rtl_set_bbreg(hw, 0xe80, 0x0c000000, 0x2);
  130. rtl_set_bbreg(hw, 0xe88, 0x0c000000, 0x2);
  131. }
  132. bool rtl8723e_phy_mac_config(struct ieee80211_hw *hw)
  133. {
  134. struct rtl_priv *rtlpriv = rtl_priv(hw);
  135. bool rtstatus = _rtl8723e_phy_config_mac_with_headerfile(hw);
  136. rtl_write_byte(rtlpriv, 0x04CA, 0x0A);
  137. return rtstatus;
  138. }
  139. bool rtl8723e_phy_bb_config(struct ieee80211_hw *hw)
  140. {
  141. bool rtstatus = true;
  142. struct rtl_priv *rtlpriv = rtl_priv(hw);
  143. u8 tmpu1b;
  144. u8 b_reg_hwparafile = 1;
  145. rtl8723_phy_init_bb_rf_reg_def(hw);
  146. /* 1. 0x28[1] = 1 */
  147. tmpu1b = rtl_read_byte(rtlpriv, REG_AFE_PLL_CTRL);
  148. udelay(2);
  149. rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL, (tmpu1b|BIT(1)));
  150. udelay(2);
  151. /* 2. 0x29[7:0] = 0xFF */
  152. rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL+1, 0xff);
  153. udelay(2);
  154. /* 3. 0x02[1:0] = 2b'11 */
  155. tmpu1b = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN);
  156. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN,
  157. (tmpu1b | FEN_BB_GLB_RSTN | FEN_BBRSTB));
  158. /* 4. 0x25[6] = 0 */
  159. tmpu1b = rtl_read_byte(rtlpriv, REG_AFE_XTAL_CTRL+1);
  160. rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL+1, (tmpu1b & (~BIT(6))));
  161. /* 5. 0x24[20] = 0 //Advised by SD3 Alex Wang. 2011.02.09. */
  162. tmpu1b = rtl_read_byte(rtlpriv, REG_AFE_XTAL_CTRL+2);
  163. rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL+2, (tmpu1b & (~BIT(4))));
  164. /* 6. 0x1f[7:0] = 0x07 */
  165. rtl_write_byte(rtlpriv, REG_RF_CTRL, 0x07);
  166. if (b_reg_hwparafile == 1)
  167. rtstatus = _rtl8723e_phy_bb8192c_config_parafile(hw);
  168. return rtstatus;
  169. }
  170. bool rtl8723e_phy_rf_config(struct ieee80211_hw *hw)
  171. {
  172. return rtl8723e_phy_rf6052_config(hw);
  173. }
  174. static bool _rtl8723e_phy_bb8192c_config_parafile(struct ieee80211_hw *hw)
  175. {
  176. struct rtl_priv *rtlpriv = rtl_priv(hw);
  177. struct rtl_phy *rtlphy = &rtlpriv->phy;
  178. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  179. bool rtstatus;
  180. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "\n");
  181. rtstatus = _rtl8723e_phy_config_bb_with_headerfile(hw,
  182. BASEBAND_CONFIG_PHY_REG);
  183. if (rtstatus != true) {
  184. pr_err("Write BB Reg Fail!!\n");
  185. return false;
  186. }
  187. if (rtlphy->rf_type == RF_1T2R) {
  188. _rtl8723e_phy_bb_config_1t(hw);
  189. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Config to 1T!!\n");
  190. }
  191. if (rtlefuse->autoload_failflag == false) {
  192. rtlphy->pwrgroup_cnt = 0;
  193. rtstatus = _rtl8723e_phy_config_bb_with_pgheaderfile(hw,
  194. BASEBAND_CONFIG_PHY_REG);
  195. }
  196. if (rtstatus != true) {
  197. pr_err("BB_PG Reg Fail!!\n");
  198. return false;
  199. }
  200. rtstatus =
  201. _rtl8723e_phy_config_bb_with_headerfile(hw, BASEBAND_CONFIG_AGC_TAB);
  202. if (rtstatus != true) {
  203. pr_err("AGC Table Fail\n");
  204. return false;
  205. }
  206. rtlphy->cck_high_power = (bool) (rtl_get_bbreg(hw,
  207. RFPGA0_XA_HSSIPARAMETER2,
  208. 0x200));
  209. return true;
  210. }
  211. static bool _rtl8723e_phy_config_mac_with_headerfile(struct ieee80211_hw *hw)
  212. {
  213. struct rtl_priv *rtlpriv = rtl_priv(hw);
  214. u32 i;
  215. u32 arraylength;
  216. u32 *ptrarray;
  217. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Read Rtl723MACPHY_Array\n");
  218. arraylength = RTL8723E_MACARRAYLENGTH;
  219. ptrarray = RTL8723EMAC_ARRAY;
  220. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  221. "Img:RTL8192CEMAC_2T_ARRAY\n");
  222. for (i = 0; i < arraylength; i = i + 2)
  223. rtl_write_byte(rtlpriv, ptrarray[i], (u8) ptrarray[i + 1]);
  224. return true;
  225. }
  226. static bool _rtl8723e_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
  227. u8 configtype)
  228. {
  229. int i;
  230. u32 *phy_regarray_table;
  231. u32 *agctab_array_table;
  232. u16 phy_reg_arraylen, agctab_arraylen;
  233. struct rtl_priv *rtlpriv = rtl_priv(hw);
  234. agctab_arraylen = RTL8723E_AGCTAB_1TARRAYLENGTH;
  235. agctab_array_table = RTL8723EAGCTAB_1TARRAY;
  236. phy_reg_arraylen = RTL8723E_PHY_REG_1TARRAY_LENGTH;
  237. phy_regarray_table = RTL8723EPHY_REG_1TARRAY;
  238. if (configtype == BASEBAND_CONFIG_PHY_REG) {
  239. for (i = 0; i < phy_reg_arraylen; i = i + 2) {
  240. if (phy_regarray_table[i] == 0xfe)
  241. mdelay(50);
  242. else if (phy_regarray_table[i] == 0xfd)
  243. mdelay(5);
  244. else if (phy_regarray_table[i] == 0xfc)
  245. mdelay(1);
  246. else if (phy_regarray_table[i] == 0xfb)
  247. udelay(50);
  248. else if (phy_regarray_table[i] == 0xfa)
  249. udelay(5);
  250. else if (phy_regarray_table[i] == 0xf9)
  251. udelay(1);
  252. rtl_set_bbreg(hw, phy_regarray_table[i], MASKDWORD,
  253. phy_regarray_table[i + 1]);
  254. udelay(1);
  255. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  256. "The phy_regarray_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
  257. phy_regarray_table[i],
  258. phy_regarray_table[i + 1]);
  259. }
  260. } else if (configtype == BASEBAND_CONFIG_AGC_TAB) {
  261. for (i = 0; i < agctab_arraylen; i = i + 2) {
  262. rtl_set_bbreg(hw, agctab_array_table[i], MASKDWORD,
  263. agctab_array_table[i + 1]);
  264. udelay(1);
  265. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  266. "The agctab_array_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
  267. agctab_array_table[i],
  268. agctab_array_table[i + 1]);
  269. }
  270. }
  271. return true;
  272. }
  273. static void store_pwrindex_diffrate_offset(struct ieee80211_hw *hw,
  274. u32 regaddr, u32 bitmask,
  275. u32 data)
  276. {
  277. struct rtl_priv *rtlpriv = rtl_priv(hw);
  278. struct rtl_phy *rtlphy = &rtlpriv->phy;
  279. if (regaddr == RTXAGC_A_RATE18_06) {
  280. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][0] =
  281. data;
  282. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  283. "MCSTxPowerLevelOriginalOffset[%d][0] = 0x%x\n",
  284. rtlphy->pwrgroup_cnt,
  285. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  286. pwrgroup_cnt][0]);
  287. }
  288. if (regaddr == RTXAGC_A_RATE54_24) {
  289. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][1] =
  290. data;
  291. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  292. "MCSTxPowerLevelOriginalOffset[%d][1] = 0x%x\n",
  293. rtlphy->pwrgroup_cnt,
  294. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  295. pwrgroup_cnt][1]);
  296. }
  297. if (regaddr == RTXAGC_A_CCK1_MCS32) {
  298. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][6] =
  299. data;
  300. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  301. "MCSTxPowerLevelOriginalOffset[%d][6] = 0x%x\n",
  302. rtlphy->pwrgroup_cnt,
  303. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  304. pwrgroup_cnt][6]);
  305. }
  306. if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0xffffff00) {
  307. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][7] =
  308. data;
  309. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  310. "MCSTxPowerLevelOriginalOffset[%d][7] = 0x%x\n",
  311. rtlphy->pwrgroup_cnt,
  312. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  313. pwrgroup_cnt][7]);
  314. }
  315. if (regaddr == RTXAGC_A_MCS03_MCS00) {
  316. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][2] =
  317. data;
  318. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  319. "MCSTxPowerLevelOriginalOffset[%d][2] = 0x%x\n",
  320. rtlphy->pwrgroup_cnt,
  321. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  322. pwrgroup_cnt][2]);
  323. }
  324. if (regaddr == RTXAGC_A_MCS07_MCS04) {
  325. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][3] =
  326. data;
  327. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  328. "MCSTxPowerLevelOriginalOffset[%d][3] = 0x%x\n",
  329. rtlphy->pwrgroup_cnt,
  330. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  331. pwrgroup_cnt][3]);
  332. }
  333. if (regaddr == RTXAGC_A_MCS11_MCS08) {
  334. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][4] =
  335. data;
  336. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  337. "MCSTxPowerLevelOriginalOffset[%d][4] = 0x%x\n",
  338. rtlphy->pwrgroup_cnt,
  339. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  340. pwrgroup_cnt][4]);
  341. }
  342. if (regaddr == RTXAGC_A_MCS15_MCS12) {
  343. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][5] =
  344. data;
  345. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  346. "MCSTxPowerLevelOriginalOffset[%d][5] = 0x%x\n",
  347. rtlphy->pwrgroup_cnt,
  348. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  349. pwrgroup_cnt][5]);
  350. }
  351. if (regaddr == RTXAGC_B_RATE18_06) {
  352. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][8] =
  353. data;
  354. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  355. "MCSTxPowerLevelOriginalOffset[%d][8] = 0x%x\n",
  356. rtlphy->pwrgroup_cnt,
  357. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  358. pwrgroup_cnt][8]);
  359. }
  360. if (regaddr == RTXAGC_B_RATE54_24) {
  361. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][9] =
  362. data;
  363. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  364. "MCSTxPowerLevelOriginalOffset[%d][9] = 0x%x\n",
  365. rtlphy->pwrgroup_cnt,
  366. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  367. pwrgroup_cnt][9]);
  368. }
  369. if (regaddr == RTXAGC_B_CCK1_55_MCS32) {
  370. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][14] =
  371. data;
  372. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  373. "MCSTxPowerLevelOriginalOffset[%d][14] = 0x%x\n",
  374. rtlphy->pwrgroup_cnt,
  375. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  376. pwrgroup_cnt][14]);
  377. }
  378. if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0x000000ff) {
  379. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][15] =
  380. data;
  381. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  382. "MCSTxPowerLevelOriginalOffset[%d][15] = 0x%x\n",
  383. rtlphy->pwrgroup_cnt,
  384. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  385. pwrgroup_cnt][15]);
  386. }
  387. if (regaddr == RTXAGC_B_MCS03_MCS00) {
  388. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][10] =
  389. data;
  390. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  391. "MCSTxPowerLevelOriginalOffset[%d][10] = 0x%x\n",
  392. rtlphy->pwrgroup_cnt,
  393. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  394. pwrgroup_cnt][10]);
  395. }
  396. if (regaddr == RTXAGC_B_MCS07_MCS04) {
  397. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][11] =
  398. data;
  399. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  400. "MCSTxPowerLevelOriginalOffset[%d][11] = 0x%x\n",
  401. rtlphy->pwrgroup_cnt,
  402. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  403. pwrgroup_cnt][11]);
  404. }
  405. if (regaddr == RTXAGC_B_MCS11_MCS08) {
  406. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][12] =
  407. data;
  408. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  409. "MCSTxPowerLevelOriginalOffset[%d][12] = 0x%x\n",
  410. rtlphy->pwrgroup_cnt,
  411. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  412. pwrgroup_cnt][12]);
  413. }
  414. if (regaddr == RTXAGC_B_MCS15_MCS12) {
  415. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][13] =
  416. data;
  417. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  418. "MCSTxPowerLevelOriginalOffset[%d][13] = 0x%x\n",
  419. rtlphy->pwrgroup_cnt,
  420. rtlphy->mcs_txpwrlevel_origoffset[rtlphy->
  421. pwrgroup_cnt][13]);
  422. rtlphy->pwrgroup_cnt++;
  423. }
  424. }
  425. static bool _rtl8723e_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
  426. u8 configtype)
  427. {
  428. struct rtl_priv *rtlpriv = rtl_priv(hw);
  429. int i;
  430. u32 *phy_regarray_table_pg;
  431. u16 phy_regarray_pg_len;
  432. phy_regarray_pg_len = RTL8723E_PHY_REG_ARRAY_PGLENGTH;
  433. phy_regarray_table_pg = RTL8723EPHY_REG_ARRAY_PG;
  434. if (configtype == BASEBAND_CONFIG_PHY_REG) {
  435. for (i = 0; i < phy_regarray_pg_len; i = i + 3) {
  436. if (phy_regarray_table_pg[i] == 0xfe)
  437. mdelay(50);
  438. else if (phy_regarray_table_pg[i] == 0xfd)
  439. mdelay(5);
  440. else if (phy_regarray_table_pg[i] == 0xfc)
  441. mdelay(1);
  442. else if (phy_regarray_table_pg[i] == 0xfb)
  443. udelay(50);
  444. else if (phy_regarray_table_pg[i] == 0xfa)
  445. udelay(5);
  446. else if (phy_regarray_table_pg[i] == 0xf9)
  447. udelay(1);
  448. store_pwrindex_diffrate_offset(hw,
  449. phy_regarray_table_pg[i],
  450. phy_regarray_table_pg[i + 1],
  451. phy_regarray_table_pg[i + 2]);
  452. }
  453. } else {
  454. RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
  455. "configtype != BaseBand_Config_PHY_REG\n");
  456. }
  457. return true;
  458. }
  459. bool rtl8723e_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
  460. enum radio_path rfpath)
  461. {
  462. int i;
  463. bool rtstatus = true;
  464. u32 *radioa_array_table;
  465. u32 *radiob_array_table;
  466. u16 radioa_arraylen, radiob_arraylen;
  467. radioa_arraylen = RTL8723ERADIOA_1TARRAYLENGTH;
  468. radioa_array_table = RTL8723E_RADIOA_1TARRAY;
  469. radiob_arraylen = RTL8723E_RADIOB_1TARRAYLENGTH;
  470. radiob_array_table = RTL8723E_RADIOB_1TARRAY;
  471. rtstatus = true;
  472. switch (rfpath) {
  473. case RF90_PATH_A:
  474. for (i = 0; i < radioa_arraylen; i = i + 2) {
  475. if (radioa_array_table[i] == 0xfe) {
  476. mdelay(50);
  477. } else if (radioa_array_table[i] == 0xfd) {
  478. mdelay(5);
  479. } else if (radioa_array_table[i] == 0xfc) {
  480. mdelay(1);
  481. } else if (radioa_array_table[i] == 0xfb) {
  482. udelay(50);
  483. } else if (radioa_array_table[i] == 0xfa) {
  484. udelay(5);
  485. } else if (radioa_array_table[i] == 0xf9) {
  486. udelay(1);
  487. } else {
  488. rtl_set_rfreg(hw, rfpath, radioa_array_table[i],
  489. RFREG_OFFSET_MASK,
  490. radioa_array_table[i + 1]);
  491. udelay(1);
  492. }
  493. }
  494. break;
  495. case RF90_PATH_B:
  496. case RF90_PATH_C:
  497. case RF90_PATH_D:
  498. break;
  499. }
  500. return true;
  501. }
  502. void rtl8723e_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
  503. {
  504. struct rtl_priv *rtlpriv = rtl_priv(hw);
  505. struct rtl_phy *rtlphy = &rtlpriv->phy;
  506. rtlphy->default_initialgain[0] =
  507. (u8) rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
  508. rtlphy->default_initialgain[1] =
  509. (u8) rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
  510. rtlphy->default_initialgain[2] =
  511. (u8) rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
  512. rtlphy->default_initialgain[3] =
  513. (u8) rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
  514. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  515. "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
  516. rtlphy->default_initialgain[0],
  517. rtlphy->default_initialgain[1],
  518. rtlphy->default_initialgain[2],
  519. rtlphy->default_initialgain[3]);
  520. rtlphy->framesync = (u8) rtl_get_bbreg(hw,
  521. ROFDM0_RXDETECTOR3, MASKBYTE0);
  522. rtlphy->framesync_c34 = rtl_get_bbreg(hw,
  523. ROFDM0_RXDETECTOR2, MASKDWORD);
  524. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  525. "Default framesync (0x%x) = 0x%x\n",
  526. ROFDM0_RXDETECTOR3, rtlphy->framesync);
  527. }
  528. void rtl8723e_phy_get_txpower_level(struct ieee80211_hw *hw, long *powerlevel)
  529. {
  530. struct rtl_priv *rtlpriv = rtl_priv(hw);
  531. struct rtl_phy *rtlphy = &rtlpriv->phy;
  532. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  533. u8 txpwr_level;
  534. long txpwr_dbm;
  535. txpwr_level = rtlphy->cur_cck_txpwridx;
  536. txpwr_dbm = rtl8723_phy_txpwr_idx_to_dbm(hw,
  537. WIRELESS_MODE_B, txpwr_level);
  538. txpwr_level = rtlphy->cur_ofdm24g_txpwridx +
  539. rtlefuse->legacy_ht_txpowerdiff;
  540. if (rtl8723_phy_txpwr_idx_to_dbm(hw,
  541. WIRELESS_MODE_G,
  542. txpwr_level) > txpwr_dbm)
  543. txpwr_dbm =
  544. rtl8723_phy_txpwr_idx_to_dbm(hw, WIRELESS_MODE_G,
  545. txpwr_level);
  546. txpwr_level = rtlphy->cur_ofdm24g_txpwridx;
  547. if (rtl8723_phy_txpwr_idx_to_dbm(hw,
  548. WIRELESS_MODE_N_24G,
  549. txpwr_level) > txpwr_dbm)
  550. txpwr_dbm =
  551. rtl8723_phy_txpwr_idx_to_dbm(hw, WIRELESS_MODE_N_24G,
  552. txpwr_level);
  553. *powerlevel = txpwr_dbm;
  554. }
  555. static void _rtl8723e_get_txpower_index(struct ieee80211_hw *hw, u8 channel,
  556. u8 *cckpowerlevel, u8 *ofdmpowerlevel)
  557. {
  558. struct rtl_priv *rtlpriv = rtl_priv(hw);
  559. struct rtl_phy *rtlphy = &rtlpriv->phy;
  560. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  561. u8 index = (channel - 1);
  562. cckpowerlevel[RF90_PATH_A] =
  563. rtlefuse->txpwrlevel_cck[RF90_PATH_A][index];
  564. cckpowerlevel[RF90_PATH_B] =
  565. rtlefuse->txpwrlevel_cck[RF90_PATH_B][index];
  566. if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_1T1R) {
  567. ofdmpowerlevel[RF90_PATH_A] =
  568. rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_A][index];
  569. ofdmpowerlevel[RF90_PATH_B] =
  570. rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_B][index];
  571. } else if (get_rf_type(rtlphy) == RF_2T2R) {
  572. ofdmpowerlevel[RF90_PATH_A] =
  573. rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_A][index];
  574. ofdmpowerlevel[RF90_PATH_B] =
  575. rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_B][index];
  576. }
  577. }
  578. static void _rtl8723e_ccxpower_index_check(struct ieee80211_hw *hw,
  579. u8 channel, u8 *cckpowerlevel,
  580. u8 *ofdmpowerlevel)
  581. {
  582. struct rtl_priv *rtlpriv = rtl_priv(hw);
  583. struct rtl_phy *rtlphy = &rtlpriv->phy;
  584. rtlphy->cur_cck_txpwridx = cckpowerlevel[0];
  585. rtlphy->cur_ofdm24g_txpwridx = ofdmpowerlevel[0];
  586. }
  587. void rtl8723e_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
  588. {
  589. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  590. u8 cckpowerlevel[2], ofdmpowerlevel[2];
  591. if (rtlefuse->txpwr_fromeprom == false)
  592. return;
  593. _rtl8723e_get_txpower_index(hw, channel,
  594. &cckpowerlevel[0], &ofdmpowerlevel[0]);
  595. _rtl8723e_ccxpower_index_check(hw,
  596. channel, &cckpowerlevel[0],
  597. &ofdmpowerlevel[0]);
  598. rtl8723e_phy_rf6052_set_cck_txpower(hw, &cckpowerlevel[0]);
  599. rtl8723e_phy_rf6052_set_ofdm_txpower(hw, &ofdmpowerlevel[0], channel);
  600. }
  601. bool rtl8723e_phy_update_txpower_dbm(struct ieee80211_hw *hw, long power_indbm)
  602. {
  603. struct rtl_priv *rtlpriv = rtl_priv(hw);
  604. struct rtl_phy *rtlphy = &rtlpriv->phy;
  605. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  606. u8 idx;
  607. u8 rf_path;
  608. u8 ccktxpwridx = _rtl8723e_phy_dbm_to_txpwr_idx(hw,
  609. WIRELESS_MODE_B,
  610. power_indbm);
  611. u8 ofdmtxpwridx = _rtl8723e_phy_dbm_to_txpwr_idx(hw,
  612. WIRELESS_MODE_N_24G,
  613. power_indbm);
  614. if (ofdmtxpwridx - rtlefuse->legacy_ht_txpowerdiff > 0)
  615. ofdmtxpwridx -= rtlefuse->legacy_ht_txpowerdiff;
  616. else
  617. ofdmtxpwridx = 0;
  618. RT_TRACE(rtlpriv, COMP_TXAGC, DBG_TRACE,
  619. "%lx dBm, ccktxpwridx = %d, ofdmtxpwridx = %d\n",
  620. power_indbm, ccktxpwridx, ofdmtxpwridx);
  621. for (idx = 0; idx < 14; idx++) {
  622. for (rf_path = 0; rf_path < 2; rf_path++) {
  623. rtlefuse->txpwrlevel_cck[rf_path][idx] = ccktxpwridx;
  624. rtlefuse->txpwrlevel_ht40_1s[rf_path][idx] =
  625. ofdmtxpwridx;
  626. rtlefuse->txpwrlevel_ht40_2s[rf_path][idx] =
  627. ofdmtxpwridx;
  628. }
  629. }
  630. rtl8723e_phy_set_txpower_level(hw, rtlphy->current_channel);
  631. return true;
  632. }
  633. static u8 _rtl8723e_phy_dbm_to_txpwr_idx(struct ieee80211_hw *hw,
  634. enum wireless_mode wirelessmode,
  635. long power_indbm)
  636. {
  637. u8 txpwridx;
  638. long offset;
  639. switch (wirelessmode) {
  640. case WIRELESS_MODE_B:
  641. offset = -7;
  642. break;
  643. case WIRELESS_MODE_G:
  644. case WIRELESS_MODE_N_24G:
  645. offset = -8;
  646. break;
  647. default:
  648. offset = -8;
  649. break;
  650. }
  651. if ((power_indbm - offset) > 0)
  652. txpwridx = (u8)((power_indbm - offset) * 2);
  653. else
  654. txpwridx = 0;
  655. if (txpwridx > MAX_TXPWR_IDX_NMODE_92S)
  656. txpwridx = MAX_TXPWR_IDX_NMODE_92S;
  657. return txpwridx;
  658. }
  659. void rtl8723e_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
  660. {
  661. struct rtl_priv *rtlpriv = rtl_priv(hw);
  662. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  663. enum io_type iotype;
  664. if (!is_hal_stop(rtlhal)) {
  665. switch (operation) {
  666. case SCAN_OPT_BACKUP_BAND0:
  667. iotype = IO_CMD_PAUSE_BAND0_DM_BY_SCAN;
  668. rtlpriv->cfg->ops->set_hw_reg(hw,
  669. HW_VAR_IO_CMD,
  670. (u8 *)&iotype);
  671. break;
  672. case SCAN_OPT_RESTORE:
  673. iotype = IO_CMD_RESUME_DM_BY_SCAN;
  674. rtlpriv->cfg->ops->set_hw_reg(hw,
  675. HW_VAR_IO_CMD,
  676. (u8 *)&iotype);
  677. break;
  678. default:
  679. pr_err("Unknown Scan Backup operation.\n");
  680. break;
  681. }
  682. }
  683. }
  684. void rtl8723e_phy_set_bw_mode_callback(struct ieee80211_hw *hw)
  685. {
  686. struct rtl_priv *rtlpriv = rtl_priv(hw);
  687. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  688. struct rtl_phy *rtlphy = &rtlpriv->phy;
  689. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  690. u8 reg_bw_opmode;
  691. u8 reg_prsr_rsc;
  692. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE,
  693. "Switch to %s bandwidth\n",
  694. rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
  695. "20MHz" : "40MHz");
  696. if (is_hal_stop(rtlhal)) {
  697. rtlphy->set_bwmode_inprogress = false;
  698. return;
  699. }
  700. reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
  701. reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
  702. switch (rtlphy->current_chan_bw) {
  703. case HT_CHANNEL_WIDTH_20:
  704. reg_bw_opmode |= BW_OPMODE_20MHZ;
  705. rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
  706. break;
  707. case HT_CHANNEL_WIDTH_20_40:
  708. reg_bw_opmode &= ~BW_OPMODE_20MHZ;
  709. rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
  710. reg_prsr_rsc =
  711. (reg_prsr_rsc & 0x90) | (mac->cur_40_prime_sc << 5);
  712. rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
  713. break;
  714. default:
  715. pr_err("unknown bandwidth: %#X\n",
  716. rtlphy->current_chan_bw);
  717. break;
  718. }
  719. switch (rtlphy->current_chan_bw) {
  720. case HT_CHANNEL_WIDTH_20:
  721. rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
  722. rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
  723. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1);
  724. break;
  725. case HT_CHANNEL_WIDTH_20_40:
  726. rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
  727. rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
  728. rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCK_SIDEBAND,
  729. (mac->cur_40_prime_sc >> 1));
  730. rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
  731. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 0);
  732. rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
  733. (mac->cur_40_prime_sc ==
  734. HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
  735. break;
  736. default:
  737. pr_err("unknown bandwidth: %#X\n",
  738. rtlphy->current_chan_bw);
  739. break;
  740. }
  741. rtl8723e_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
  742. rtlphy->set_bwmode_inprogress = false;
  743. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
  744. }
  745. void rtl8723e_phy_set_bw_mode(struct ieee80211_hw *hw,
  746. enum nl80211_channel_type ch_type)
  747. {
  748. struct rtl_priv *rtlpriv = rtl_priv(hw);
  749. struct rtl_phy *rtlphy = &rtlpriv->phy;
  750. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  751. u8 tmp_bw = rtlphy->current_chan_bw;
  752. if (rtlphy->set_bwmode_inprogress)
  753. return;
  754. rtlphy->set_bwmode_inprogress = true;
  755. if ((!is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
  756. rtl8723e_phy_set_bw_mode_callback(hw);
  757. } else {
  758. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  759. "false driver sleep or unload\n");
  760. rtlphy->set_bwmode_inprogress = false;
  761. rtlphy->current_chan_bw = tmp_bw;
  762. }
  763. }
  764. void rtl8723e_phy_sw_chnl_callback(struct ieee80211_hw *hw)
  765. {
  766. struct rtl_priv *rtlpriv = rtl_priv(hw);
  767. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  768. struct rtl_phy *rtlphy = &rtlpriv->phy;
  769. u32 delay;
  770. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE,
  771. "switch to channel%d\n", rtlphy->current_channel);
  772. if (is_hal_stop(rtlhal))
  773. return;
  774. do {
  775. if (!rtlphy->sw_chnl_inprogress)
  776. break;
  777. if (!_rtl8723e_phy_sw_chnl_step_by_step
  778. (hw, rtlphy->current_channel, &rtlphy->sw_chnl_stage,
  779. &rtlphy->sw_chnl_step, &delay)) {
  780. if (delay > 0)
  781. mdelay(delay);
  782. else
  783. continue;
  784. } else {
  785. rtlphy->sw_chnl_inprogress = false;
  786. }
  787. break;
  788. } while (true);
  789. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
  790. }
  791. u8 rtl8723e_phy_sw_chnl(struct ieee80211_hw *hw)
  792. {
  793. struct rtl_priv *rtlpriv = rtl_priv(hw);
  794. struct rtl_phy *rtlphy = &rtlpriv->phy;
  795. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  796. if (rtlphy->sw_chnl_inprogress)
  797. return 0;
  798. if (rtlphy->set_bwmode_inprogress)
  799. return 0;
  800. WARN_ONCE((rtlphy->current_channel > 14),
  801. "rtl8723ae: WIRELESS_MODE_G but channel>14");
  802. rtlphy->sw_chnl_inprogress = true;
  803. rtlphy->sw_chnl_stage = 0;
  804. rtlphy->sw_chnl_step = 0;
  805. if (!(is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
  806. rtl8723e_phy_sw_chnl_callback(hw);
  807. RT_TRACE(rtlpriv, COMP_CHAN, DBG_LOUD,
  808. "sw_chnl_inprogress false schedule workitem\n");
  809. rtlphy->sw_chnl_inprogress = false;
  810. } else {
  811. RT_TRACE(rtlpriv, COMP_CHAN, DBG_LOUD,
  812. "sw_chnl_inprogress false driver sleep or unload\n");
  813. rtlphy->sw_chnl_inprogress = false;
  814. }
  815. return 1;
  816. }
  817. static void _rtl8723e_phy_sw_rf_seting(struct ieee80211_hw *hw, u8 channel)
  818. {
  819. struct rtl_priv *rtlpriv = rtl_priv(hw);
  820. struct rtl_phy *rtlphy = &rtlpriv->phy;
  821. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  822. if (IS_81xxC_VENDOR_UMC_B_CUT(rtlhal->version)) {
  823. if (channel == 6 && rtlphy->current_chan_bw ==
  824. HT_CHANNEL_WIDTH_20)
  825. rtl_set_rfreg(hw, RF90_PATH_A, RF_RX_G1,
  826. MASKDWORD, 0x00255);
  827. else{
  828. u32 backuprf0x1a = (u32)rtl_get_rfreg(hw,
  829. RF90_PATH_A, RF_RX_G1,
  830. RFREG_OFFSET_MASK);
  831. rtl_set_rfreg(hw, RF90_PATH_A, RF_RX_G1,
  832. MASKDWORD, backuprf0x1a);
  833. }
  834. }
  835. }
  836. static bool _rtl8723e_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
  837. u8 channel, u8 *stage, u8 *step,
  838. u32 *delay)
  839. {
  840. struct rtl_priv *rtlpriv = rtl_priv(hw);
  841. struct rtl_phy *rtlphy = &rtlpriv->phy;
  842. struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
  843. u32 precommoncmdcnt;
  844. struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
  845. u32 postcommoncmdcnt;
  846. struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
  847. u32 rfdependcmdcnt;
  848. struct swchnlcmd *currentcmd = NULL;
  849. u8 rfpath;
  850. u8 num_total_rfpath = rtlphy->num_total_rfpath;
  851. precommoncmdcnt = 0;
  852. rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
  853. MAX_PRECMD_CNT,
  854. CMDID_SET_TXPOWEROWER_LEVEL, 0, 0, 0);
  855. rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
  856. MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
  857. postcommoncmdcnt = 0;
  858. rtl8723_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
  859. MAX_POSTCMD_CNT, CMDID_END, 0, 0, 0);
  860. rfdependcmdcnt = 0;
  861. WARN_ONCE((channel < 1 || channel > 14),
  862. "rtl8723ae: illegal channel for Zebra: %d\n", channel);
  863. rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
  864. MAX_RFDEPENDCMD_CNT, CMDID_RF_WRITEREG,
  865. RF_CHNLBW, channel, 10);
  866. rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
  867. MAX_RFDEPENDCMD_CNT, CMDID_END, 0, 0,
  868. 0);
  869. do {
  870. switch (*stage) {
  871. case 0:
  872. currentcmd = &precommoncmd[*step];
  873. break;
  874. case 1:
  875. currentcmd = &rfdependcmd[*step];
  876. break;
  877. case 2:
  878. currentcmd = &postcommoncmd[*step];
  879. break;
  880. default:
  881. pr_err("Invalid 'stage' = %d, Check it!\n",
  882. *stage);
  883. return true;
  884. }
  885. if (currentcmd->cmdid == CMDID_END) {
  886. if ((*stage) == 2) {
  887. return true;
  888. } else {
  889. (*stage)++;
  890. (*step) = 0;
  891. continue;
  892. }
  893. }
  894. switch (currentcmd->cmdid) {
  895. case CMDID_SET_TXPOWEROWER_LEVEL:
  896. rtl8723e_phy_set_txpower_level(hw, channel);
  897. break;
  898. case CMDID_WRITEPORT_ULONG:
  899. rtl_write_dword(rtlpriv, currentcmd->para1,
  900. currentcmd->para2);
  901. break;
  902. case CMDID_WRITEPORT_USHORT:
  903. rtl_write_word(rtlpriv, currentcmd->para1,
  904. (u16) currentcmd->para2);
  905. break;
  906. case CMDID_WRITEPORT_UCHAR:
  907. rtl_write_byte(rtlpriv, currentcmd->para1,
  908. (u8) currentcmd->para2);
  909. break;
  910. case CMDID_RF_WRITEREG:
  911. for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
  912. rtlphy->rfreg_chnlval[rfpath] =
  913. ((rtlphy->rfreg_chnlval[rfpath] &
  914. 0xfffffc00) | currentcmd->para2);
  915. rtl_set_rfreg(hw, (enum radio_path)rfpath,
  916. currentcmd->para1,
  917. RFREG_OFFSET_MASK,
  918. rtlphy->rfreg_chnlval[rfpath]);
  919. }
  920. _rtl8723e_phy_sw_rf_seting(hw, channel);
  921. break;
  922. default:
  923. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  924. "switch case %#x not processed\n",
  925. currentcmd->cmdid);
  926. break;
  927. }
  928. break;
  929. } while (true);
  930. (*delay) = currentcmd->msdelay;
  931. (*step)++;
  932. return false;
  933. }
  934. static u8 _rtl8723e_phy_path_a_iqk(struct ieee80211_hw *hw, bool config_pathb)
  935. {
  936. u32 reg_eac, reg_e94, reg_e9c, reg_ea4;
  937. u8 result = 0x00;
  938. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x10008c1f);
  939. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x10008c1f);
  940. rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82140102);
  941. rtl_set_bbreg(hw, 0xe3c, MASKDWORD,
  942. config_pathb ? 0x28160202 : 0x28160502);
  943. if (config_pathb) {
  944. rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x10008c22);
  945. rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x10008c22);
  946. rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82140102);
  947. rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x28160202);
  948. }
  949. rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x001028d1);
  950. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf9000000);
  951. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
  952. mdelay(IQK_DELAY_TIME);
  953. reg_eac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  954. reg_e94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
  955. reg_e9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
  956. reg_ea4 = rtl_get_bbreg(hw, 0xea4, MASKDWORD);
  957. if (!(reg_eac & BIT(28)) &&
  958. (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
  959. (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
  960. result |= 0x01;
  961. else
  962. return result;
  963. if (!(reg_eac & BIT(27)) &&
  964. (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
  965. (((reg_eac & 0x03FF0000) >> 16) != 0x36))
  966. result |= 0x02;
  967. return result;
  968. }
  969. static u8 _rtl8723e_phy_path_b_iqk(struct ieee80211_hw *hw)
  970. {
  971. u32 reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc;
  972. u8 result = 0x00;
  973. rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0x00000002);
  974. rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0x00000000);
  975. mdelay(IQK_DELAY_TIME);
  976. reg_eac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  977. reg_eb4 = rtl_get_bbreg(hw, 0xeb4, MASKDWORD);
  978. reg_ebc = rtl_get_bbreg(hw, 0xebc, MASKDWORD);
  979. reg_ec4 = rtl_get_bbreg(hw, 0xec4, MASKDWORD);
  980. reg_ecc = rtl_get_bbreg(hw, 0xecc, MASKDWORD);
  981. if (!(reg_eac & BIT(31)) &&
  982. (((reg_eb4 & 0x03FF0000) >> 16) != 0x142) &&
  983. (((reg_ebc & 0x03FF0000) >> 16) != 0x42))
  984. result |= 0x01;
  985. else
  986. return result;
  987. if (!(reg_eac & BIT(30)) &&
  988. (((reg_ec4 & 0x03FF0000) >> 16) != 0x132) &&
  989. (((reg_ecc & 0x03FF0000) >> 16) != 0x36))
  990. result |= 0x02;
  991. return result;
  992. }
  993. static bool _rtl8723e_phy_simularity_compare(struct ieee80211_hw *hw,
  994. long result[][8], u8 c1, u8 c2)
  995. {
  996. u32 i, j, diff, simularity_bitmap, bound;
  997. u8 final_candidate[2] = { 0xFF, 0xFF };
  998. bool bresult = true;
  999. bound = 4;
  1000. simularity_bitmap = 0;
  1001. for (i = 0; i < bound; i++) {
  1002. diff = (result[c1][i] > result[c2][i]) ?
  1003. (result[c1][i] - result[c2][i]) :
  1004. (result[c2][i] - result[c1][i]);
  1005. if (diff > MAX_TOLERANCE) {
  1006. if ((i == 2 || i == 6) && !simularity_bitmap) {
  1007. if (result[c1][i] + result[c1][i + 1] == 0)
  1008. final_candidate[(i / 4)] = c2;
  1009. else if (result[c2][i] + result[c2][i + 1] == 0)
  1010. final_candidate[(i / 4)] = c1;
  1011. else
  1012. simularity_bitmap = simularity_bitmap |
  1013. (1 << i);
  1014. } else
  1015. simularity_bitmap =
  1016. simularity_bitmap | (1 << i);
  1017. }
  1018. }
  1019. if (simularity_bitmap == 0) {
  1020. for (i = 0; i < (bound / 4); i++) {
  1021. if (final_candidate[i] != 0xFF) {
  1022. for (j = i * 4; j < (i + 1) * 4 - 2; j++)
  1023. result[3][j] =
  1024. result[final_candidate[i]][j];
  1025. bresult = false;
  1026. }
  1027. }
  1028. return bresult;
  1029. } else if (!(simularity_bitmap & 0x0F)) {
  1030. for (i = 0; i < 4; i++)
  1031. result[3][i] = result[c1][i];
  1032. return false;
  1033. } else {
  1034. return false;
  1035. }
  1036. }
  1037. static void _rtl8723e_phy_iq_calibrate(struct ieee80211_hw *hw,
  1038. long result[][8], u8 t, bool is2t)
  1039. {
  1040. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1041. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1042. u32 i;
  1043. u8 patha_ok, pathb_ok;
  1044. u32 adda_reg[IQK_ADDA_REG_NUM] = {
  1045. 0x85c, 0xe6c, 0xe70, 0xe74,
  1046. 0xe78, 0xe7c, 0xe80, 0xe84,
  1047. 0xe88, 0xe8c, 0xed0, 0xed4,
  1048. 0xed8, 0xedc, 0xee0, 0xeec
  1049. };
  1050. u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
  1051. 0x522, 0x550, 0x551, 0x040
  1052. };
  1053. const u32 retrycount = 2;
  1054. u32 bbvalue;
  1055. if (t == 0) {
  1056. bbvalue = rtl_get_bbreg(hw, 0x800, MASKDWORD);
  1057. rtl8723_save_adda_registers(hw, adda_reg,
  1058. rtlphy->adda_backup, 16);
  1059. rtl8723_phy_save_mac_registers(hw, iqk_mac_reg,
  1060. rtlphy->iqk_mac_backup);
  1061. }
  1062. rtl8723_phy_path_adda_on(hw, adda_reg, true, is2t);
  1063. if (t == 0) {
  1064. rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
  1065. RFPGA0_XA_HSSIPARAMETER1,
  1066. BIT(8));
  1067. }
  1068. if (!rtlphy->rfpi_enable)
  1069. rtl8723_phy_pi_mode_switch(hw, true);
  1070. if (t == 0) {
  1071. rtlphy->reg_c04 = rtl_get_bbreg(hw, 0xc04, MASKDWORD);
  1072. rtlphy->reg_c08 = rtl_get_bbreg(hw, 0xc08, MASKDWORD);
  1073. rtlphy->reg_874 = rtl_get_bbreg(hw, 0x874, MASKDWORD);
  1074. }
  1075. rtl_set_bbreg(hw, 0xc04, MASKDWORD, 0x03a05600);
  1076. rtl_set_bbreg(hw, 0xc08, MASKDWORD, 0x000800e4);
  1077. rtl_set_bbreg(hw, 0x874, MASKDWORD, 0x22204000);
  1078. if (is2t) {
  1079. rtl_set_bbreg(hw, 0x840, MASKDWORD, 0x00010000);
  1080. rtl_set_bbreg(hw, 0x844, MASKDWORD, 0x00010000);
  1081. }
  1082. rtl8723_phy_mac_setting_calibration(hw, iqk_mac_reg,
  1083. rtlphy->iqk_mac_backup);
  1084. rtl_set_bbreg(hw, 0xb68, MASKDWORD, 0x00080000);
  1085. if (is2t)
  1086. rtl_set_bbreg(hw, 0xb6c, MASKDWORD, 0x00080000);
  1087. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
  1088. rtl_set_bbreg(hw, 0xe40, MASKDWORD, 0x01007c00);
  1089. rtl_set_bbreg(hw, 0xe44, MASKDWORD, 0x01004800);
  1090. for (i = 0; i < retrycount; i++) {
  1091. patha_ok = _rtl8723e_phy_path_a_iqk(hw, is2t);
  1092. if (patha_ok == 0x03) {
  1093. result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
  1094. 0x3FF0000) >> 16;
  1095. result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
  1096. 0x3FF0000) >> 16;
  1097. result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
  1098. 0x3FF0000) >> 16;
  1099. result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
  1100. 0x3FF0000) >> 16;
  1101. break;
  1102. } else if (i == (retrycount - 1) && patha_ok == 0x01)
  1103. result[t][0] = (rtl_get_bbreg(hw, 0xe94,
  1104. MASKDWORD) & 0x3FF0000) >>
  1105. 16;
  1106. result[t][1] =
  1107. (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) & 0x3FF0000) >> 16;
  1108. }
  1109. if (is2t) {
  1110. rtl8723_phy_path_a_standby(hw);
  1111. rtl8723_phy_path_adda_on(hw, adda_reg, false, is2t);
  1112. for (i = 0; i < retrycount; i++) {
  1113. pathb_ok = _rtl8723e_phy_path_b_iqk(hw);
  1114. if (pathb_ok == 0x03) {
  1115. result[t][4] = (rtl_get_bbreg(hw,
  1116. 0xeb4,
  1117. MASKDWORD) &
  1118. 0x3FF0000) >> 16;
  1119. result[t][5] =
  1120. (rtl_get_bbreg(hw, 0xebc, MASKDWORD) &
  1121. 0x3FF0000) >> 16;
  1122. result[t][6] =
  1123. (rtl_get_bbreg(hw, 0xec4, MASKDWORD) &
  1124. 0x3FF0000) >> 16;
  1125. result[t][7] =
  1126. (rtl_get_bbreg(hw, 0xecc, MASKDWORD) &
  1127. 0x3FF0000) >> 16;
  1128. break;
  1129. } else if (i == (retrycount - 1) && pathb_ok == 0x01) {
  1130. result[t][4] = (rtl_get_bbreg(hw,
  1131. 0xeb4,
  1132. MASKDWORD) &
  1133. 0x3FF0000) >> 16;
  1134. }
  1135. result[t][5] = (rtl_get_bbreg(hw, 0xebc, MASKDWORD) &
  1136. 0x3FF0000) >> 16;
  1137. }
  1138. }
  1139. rtl_set_bbreg(hw, 0xc04, MASKDWORD, rtlphy->reg_c04);
  1140. rtl_set_bbreg(hw, 0x874, MASKDWORD, rtlphy->reg_874);
  1141. rtl_set_bbreg(hw, 0xc08, MASKDWORD, rtlphy->reg_c08);
  1142. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0);
  1143. rtl_set_bbreg(hw, 0x840, MASKDWORD, 0x00032ed3);
  1144. if (is2t)
  1145. rtl_set_bbreg(hw, 0x844, MASKDWORD, 0x00032ed3);
  1146. if (t != 0) {
  1147. if (!rtlphy->rfpi_enable)
  1148. rtl8723_phy_pi_mode_switch(hw, false);
  1149. rtl8723_phy_reload_adda_registers(hw, adda_reg,
  1150. rtlphy->adda_backup, 16);
  1151. rtl8723_phy_reload_mac_registers(hw, iqk_mac_reg,
  1152. rtlphy->iqk_mac_backup);
  1153. }
  1154. }
  1155. static void _rtl8723e_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
  1156. {
  1157. u8 tmpreg;
  1158. u32 rf_a_mode = 0, rf_b_mode = 0, lc_cal;
  1159. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1160. tmpreg = rtl_read_byte(rtlpriv, 0xd03);
  1161. if ((tmpreg & 0x70) != 0)
  1162. rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
  1163. else
  1164. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
  1165. if ((tmpreg & 0x70) != 0) {
  1166. rf_a_mode = rtl_get_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS);
  1167. if (is2t)
  1168. rf_b_mode = rtl_get_rfreg(hw, RF90_PATH_B, 0x00,
  1169. MASK12BITS);
  1170. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS,
  1171. (rf_a_mode & 0x8FFFF) | 0x10000);
  1172. if (is2t)
  1173. rtl_set_rfreg(hw, RF90_PATH_B, 0x00, MASK12BITS,
  1174. (rf_b_mode & 0x8FFFF) | 0x10000);
  1175. }
  1176. lc_cal = rtl_get_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS);
  1177. rtl_set_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS, lc_cal | 0x08000);
  1178. mdelay(100);
  1179. if ((tmpreg & 0x70) != 0) {
  1180. rtl_write_byte(rtlpriv, 0xd03, tmpreg);
  1181. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS, rf_a_mode);
  1182. if (is2t)
  1183. rtl_set_rfreg(hw, RF90_PATH_B, 0x00, MASK12BITS,
  1184. rf_b_mode);
  1185. } else {
  1186. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  1187. }
  1188. }
  1189. static void _rtl8723e_phy_set_rfpath_switch(struct ieee80211_hw *hw,
  1190. bool bmain, bool is2t)
  1191. {
  1192. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1193. if (is_hal_stop(rtlhal)) {
  1194. rtl_set_bbreg(hw, REG_LEDCFG0, BIT(23), 0x01);
  1195. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(13), 0x01);
  1196. }
  1197. if (is2t) {
  1198. if (bmain)
  1199. rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE,
  1200. BIT(5) | BIT(6), 0x1);
  1201. else
  1202. rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE,
  1203. BIT(5) | BIT(6), 0x2);
  1204. } else {
  1205. if (bmain)
  1206. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, 0x300, 0x2);
  1207. else
  1208. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, 0x300, 0x1);
  1209. }
  1210. }
  1211. #undef IQK_ADDA_REG_NUM
  1212. #undef IQK_DELAY_TIME
  1213. void rtl8723e_phy_iq_calibrate(struct ieee80211_hw *hw, bool b_recovery)
  1214. {
  1215. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1216. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1217. long result[4][8];
  1218. u8 i, final_candidate;
  1219. bool b_patha_ok, b_pathb_ok;
  1220. long reg_e94, reg_e9c, reg_ea4, reg_eac, reg_eb4, reg_ebc, reg_ec4,
  1221. reg_ecc, reg_tmp = 0;
  1222. bool is12simular, is13simular, is23simular;
  1223. u32 iqk_bb_reg[10] = {
  1224. ROFDM0_XARXIQIMBALANCE,
  1225. ROFDM0_XBRXIQIMBALANCE,
  1226. ROFDM0_ECCATHRESHOLD,
  1227. ROFDM0_AGCRSSITABLE,
  1228. ROFDM0_XATXIQIMBALANCE,
  1229. ROFDM0_XBTXIQIMBALANCE,
  1230. ROFDM0_XCTXIQIMBALANCE,
  1231. ROFDM0_XCTXAFE,
  1232. ROFDM0_XDTXAFE,
  1233. ROFDM0_RXIQEXTANTA
  1234. };
  1235. if (b_recovery) {
  1236. rtl8723_phy_reload_adda_registers(hw,
  1237. iqk_bb_reg,
  1238. rtlphy->iqk_bb_backup, 10);
  1239. return;
  1240. }
  1241. for (i = 0; i < 8; i++) {
  1242. result[0][i] = 0;
  1243. result[1][i] = 0;
  1244. result[2][i] = 0;
  1245. result[3][i] = 0;
  1246. }
  1247. final_candidate = 0xff;
  1248. b_patha_ok = false;
  1249. b_pathb_ok = false;
  1250. is12simular = false;
  1251. is23simular = false;
  1252. is13simular = false;
  1253. for (i = 0; i < 3; i++) {
  1254. _rtl8723e_phy_iq_calibrate(hw, result, i, false);
  1255. if (i == 1) {
  1256. is12simular =
  1257. _rtl8723e_phy_simularity_compare(hw, result, 0, 1);
  1258. if (is12simular) {
  1259. final_candidate = 0;
  1260. break;
  1261. }
  1262. }
  1263. if (i == 2) {
  1264. is13simular =
  1265. _rtl8723e_phy_simularity_compare(hw, result, 0, 2);
  1266. if (is13simular) {
  1267. final_candidate = 0;
  1268. break;
  1269. }
  1270. is23simular =
  1271. _rtl8723e_phy_simularity_compare(hw, result, 1, 2);
  1272. if (is23simular)
  1273. final_candidate = 1;
  1274. else {
  1275. for (i = 0; i < 8; i++)
  1276. reg_tmp += result[3][i];
  1277. if (reg_tmp != 0)
  1278. final_candidate = 3;
  1279. else
  1280. final_candidate = 0xFF;
  1281. }
  1282. }
  1283. }
  1284. for (i = 0; i < 4; i++) {
  1285. reg_e94 = result[i][0];
  1286. reg_e9c = result[i][1];
  1287. reg_ea4 = result[i][2];
  1288. reg_eac = result[i][3];
  1289. reg_eb4 = result[i][4];
  1290. reg_ebc = result[i][5];
  1291. reg_ec4 = result[i][6];
  1292. reg_ecc = result[i][7];
  1293. }
  1294. if (final_candidate != 0xff) {
  1295. rtlphy->reg_e94 = reg_e94 = result[final_candidate][0];
  1296. rtlphy->reg_e9c = reg_e9c = result[final_candidate][1];
  1297. reg_ea4 = result[final_candidate][2];
  1298. reg_eac = result[final_candidate][3];
  1299. rtlphy->reg_eb4 = reg_eb4 = result[final_candidate][4];
  1300. rtlphy->reg_ebc = reg_ebc = result[final_candidate][5];
  1301. reg_ec4 = result[final_candidate][6];
  1302. reg_ecc = result[final_candidate][7];
  1303. b_patha_ok = true;
  1304. b_pathb_ok = true;
  1305. } else {
  1306. rtlphy->reg_e94 = rtlphy->reg_eb4 = 0x100;
  1307. rtlphy->reg_e9c = rtlphy->reg_ebc = 0x0;
  1308. }
  1309. if (reg_e94 != 0)
  1310. rtl8723_phy_path_a_fill_iqk_matrix(hw, b_patha_ok, result,
  1311. final_candidate,
  1312. (reg_ea4 == 0));
  1313. rtl8723_save_adda_registers(hw, iqk_bb_reg,
  1314. rtlphy->iqk_bb_backup, 10);
  1315. }
  1316. void rtl8723e_phy_lc_calibrate(struct ieee80211_hw *hw)
  1317. {
  1318. _rtl8723e_phy_lc_calibrate(hw, false);
  1319. }
  1320. void rtl8723e_phy_set_rfpath_switch(struct ieee80211_hw *hw, bool bmain)
  1321. {
  1322. _rtl8723e_phy_set_rfpath_switch(hw, bmain, false);
  1323. }
  1324. bool rtl8723e_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
  1325. {
  1326. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1327. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1328. bool postprocessing = false;
  1329. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  1330. "-->IO Cmd(%#x), set_io_inprogress(%d)\n",
  1331. iotype, rtlphy->set_io_inprogress);
  1332. do {
  1333. switch (iotype) {
  1334. case IO_CMD_RESUME_DM_BY_SCAN:
  1335. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  1336. "[IO CMD] Resume DM after scan.\n");
  1337. postprocessing = true;
  1338. break;
  1339. case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
  1340. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  1341. "[IO CMD] Pause DM before scan.\n");
  1342. postprocessing = true;
  1343. break;
  1344. default:
  1345. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  1346. "switch case %#x not processed\n", iotype);
  1347. break;
  1348. }
  1349. } while (false);
  1350. if (postprocessing && !rtlphy->set_io_inprogress) {
  1351. rtlphy->set_io_inprogress = true;
  1352. rtlphy->current_io_type = iotype;
  1353. } else {
  1354. return false;
  1355. }
  1356. rtl8723e_phy_set_io(hw);
  1357. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE, "IO Type(%#x)\n", iotype);
  1358. return true;
  1359. }
  1360. static void rtl8723e_phy_set_io(struct ieee80211_hw *hw)
  1361. {
  1362. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1363. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1364. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  1365. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  1366. "--->Cmd(%#x), set_io_inprogress(%d)\n",
  1367. rtlphy->current_io_type, rtlphy->set_io_inprogress);
  1368. switch (rtlphy->current_io_type) {
  1369. case IO_CMD_RESUME_DM_BY_SCAN:
  1370. dm_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
  1371. rtl8723e_dm_write_dig(hw);
  1372. rtl8723e_phy_set_txpower_level(hw, rtlphy->current_channel);
  1373. break;
  1374. case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
  1375. rtlphy->initgain_backup.xaagccore1 = dm_digtable->cur_igvalue;
  1376. dm_digtable->cur_igvalue = 0x17;
  1377. rtl8723e_dm_write_dig(hw);
  1378. break;
  1379. default:
  1380. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  1381. "switch case %#x not processed\n",
  1382. rtlphy->current_io_type);
  1383. break;
  1384. }
  1385. rtlphy->set_io_inprogress = false;
  1386. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  1387. "(%#x)\n", rtlphy->current_io_type);
  1388. }
  1389. static void rtl8723e_phy_set_rf_on(struct ieee80211_hw *hw)
  1390. {
  1391. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1392. rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
  1393. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  1394. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
  1395. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  1396. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  1397. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  1398. }
  1399. static void _rtl8723e_phy_set_rf_sleep(struct ieee80211_hw *hw)
  1400. {
  1401. u32 u4b_tmp;
  1402. u8 delay = 5;
  1403. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1404. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
  1405. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
  1406. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
  1407. u4b_tmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, RFREG_OFFSET_MASK);
  1408. while (u4b_tmp != 0 && delay > 0) {
  1409. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x0);
  1410. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
  1411. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
  1412. u4b_tmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, RFREG_OFFSET_MASK);
  1413. delay--;
  1414. }
  1415. if (delay == 0) {
  1416. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
  1417. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  1418. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  1419. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  1420. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  1421. "Switch RF timeout !!!.\n");
  1422. return;
  1423. }
  1424. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  1425. rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
  1426. }
  1427. static bool _rtl8723e_phy_set_rf_power_state(struct ieee80211_hw *hw,
  1428. enum rf_pwrstate rfpwr_state)
  1429. {
  1430. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1431. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  1432. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1433. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1434. bool bresult = true;
  1435. u8 i, queue_id;
  1436. struct rtl8192_tx_ring *ring = NULL;
  1437. switch (rfpwr_state) {
  1438. case ERFON:
  1439. if ((ppsc->rfpwr_state == ERFOFF) &&
  1440. RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
  1441. bool rtstatus;
  1442. u32 initializecount = 0;
  1443. do {
  1444. initializecount++;
  1445. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1446. "IPS Set eRf nic enable\n");
  1447. rtstatus = rtl_ps_enable_nic(hw);
  1448. } while (!rtstatus && (initializecount < 10));
  1449. RT_CLEAR_PS_LEVEL(ppsc,
  1450. RT_RF_OFF_LEVL_HALT_NIC);
  1451. } else {
  1452. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1453. "Set ERFON sleeped:%d ms\n",
  1454. jiffies_to_msecs(jiffies -
  1455. ppsc->
  1456. last_sleep_jiffies));
  1457. ppsc->last_awake_jiffies = jiffies;
  1458. rtl8723e_phy_set_rf_on(hw);
  1459. }
  1460. if (mac->link_state == MAC80211_LINKED) {
  1461. rtlpriv->cfg->ops->led_control(hw,
  1462. LED_CTL_LINK);
  1463. } else {
  1464. rtlpriv->cfg->ops->led_control(hw,
  1465. LED_CTL_NO_LINK);
  1466. }
  1467. break;
  1468. case ERFOFF:
  1469. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
  1470. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1471. "IPS Set eRf nic disable\n");
  1472. rtl_ps_disable_nic(hw);
  1473. RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  1474. } else {
  1475. if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS) {
  1476. rtlpriv->cfg->ops->led_control(hw,
  1477. LED_CTL_NO_LINK);
  1478. } else {
  1479. rtlpriv->cfg->ops->led_control(hw,
  1480. LED_CTL_POWER_OFF);
  1481. }
  1482. }
  1483. break;
  1484. case ERFSLEEP:
  1485. if (ppsc->rfpwr_state == ERFOFF)
  1486. break;
  1487. for (queue_id = 0, i = 0;
  1488. queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
  1489. ring = &pcipriv->dev.tx_ring[queue_id];
  1490. if (queue_id == BEACON_QUEUE ||
  1491. skb_queue_len(&ring->queue) == 0) {
  1492. queue_id++;
  1493. continue;
  1494. } else {
  1495. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1496. "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
  1497. (i + 1), queue_id,
  1498. skb_queue_len(&ring->queue));
  1499. udelay(10);
  1500. i++;
  1501. }
  1502. if (i >= MAX_DOZE_WAITING_TIMES_9x) {
  1503. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1504. "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
  1505. MAX_DOZE_WAITING_TIMES_9x,
  1506. queue_id,
  1507. skb_queue_len(&ring->queue));
  1508. break;
  1509. }
  1510. }
  1511. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1512. "Set ERFSLEEP awaked:%d ms\n",
  1513. jiffies_to_msecs(jiffies -
  1514. ppsc->last_awake_jiffies));
  1515. ppsc->last_sleep_jiffies = jiffies;
  1516. _rtl8723e_phy_set_rf_sleep(hw);
  1517. break;
  1518. default:
  1519. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  1520. "switch case %#x not processed\n", rfpwr_state);
  1521. bresult = false;
  1522. break;
  1523. }
  1524. if (bresult)
  1525. ppsc->rfpwr_state = rfpwr_state;
  1526. return bresult;
  1527. }
  1528. bool rtl8723e_phy_set_rf_power_state(struct ieee80211_hw *hw,
  1529. enum rf_pwrstate rfpwr_state)
  1530. {
  1531. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1532. bool bresult = false;
  1533. if (rfpwr_state == ppsc->rfpwr_state)
  1534. return bresult;
  1535. bresult = _rtl8723e_phy_set_rf_power_state(hw, rfpwr_state);
  1536. return bresult;
  1537. }