phy.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2014 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 "../rtl8723com/phy_common.h"
  32. #include "rf.h"
  33. #include "dm.h"
  34. #include "../rtl8723com/dm_common.h"
  35. #include "table.h"
  36. #include "trx.h"
  37. static bool _rtl8723be_phy_bb8723b_config_parafile(struct ieee80211_hw *hw);
  38. static bool _rtl8723be_phy_config_mac_with_headerfile(struct ieee80211_hw *hw);
  39. static bool _rtl8723be_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
  40. u8 configtype);
  41. static bool _rtl8723be_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
  42. u8 configtype);
  43. static bool _rtl8723be_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
  44. u8 channel, u8 *stage,
  45. u8 *step, u32 *delay);
  46. static void rtl8723be_phy_set_rf_on(struct ieee80211_hw *hw);
  47. static void rtl8723be_phy_set_io(struct ieee80211_hw *hw);
  48. u32 rtl8723be_phy_query_rf_reg(struct ieee80211_hw *hw, enum radio_path rfpath,
  49. u32 regaddr, u32 bitmask)
  50. {
  51. struct rtl_priv *rtlpriv = rtl_priv(hw);
  52. u32 original_value, readback_value, bitshift;
  53. unsigned long flags;
  54. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  55. "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
  56. regaddr, rfpath, bitmask);
  57. spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
  58. original_value = rtl8723_phy_rf_serial_read(hw, rfpath, regaddr);
  59. bitshift = rtl8723_phy_calculate_bit_shift(bitmask);
  60. readback_value = (original_value & bitmask) >> bitshift;
  61. spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
  62. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  63. "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
  64. regaddr, rfpath, bitmask, original_value);
  65. return readback_value;
  66. }
  67. void rtl8723be_phy_set_rf_reg(struct ieee80211_hw *hw, enum radio_path path,
  68. u32 regaddr, u32 bitmask, u32 data)
  69. {
  70. struct rtl_priv *rtlpriv = rtl_priv(hw);
  71. u32 original_value, bitshift;
  72. unsigned long flags;
  73. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  74. "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
  75. regaddr, bitmask, data, path);
  76. spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
  77. if (bitmask != RFREG_OFFSET_MASK) {
  78. original_value = rtl8723_phy_rf_serial_read(hw, path,
  79. regaddr);
  80. bitshift = rtl8723_phy_calculate_bit_shift(bitmask);
  81. data = ((original_value & (~bitmask)) |
  82. (data << bitshift));
  83. }
  84. rtl8723_phy_rf_serial_write(hw, path, regaddr, data);
  85. spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
  86. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  87. "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
  88. regaddr, bitmask, data, path);
  89. }
  90. bool rtl8723be_phy_mac_config(struct ieee80211_hw *hw)
  91. {
  92. struct rtl_priv *rtlpriv = rtl_priv(hw);
  93. bool rtstatus = _rtl8723be_phy_config_mac_with_headerfile(hw);
  94. rtl_write_byte(rtlpriv, 0x04CA, 0x0B);
  95. return rtstatus;
  96. }
  97. bool rtl8723be_phy_bb_config(struct ieee80211_hw *hw)
  98. {
  99. bool rtstatus = true;
  100. struct rtl_priv *rtlpriv = rtl_priv(hw);
  101. u16 regval;
  102. u8 b_reg_hwparafile = 1;
  103. u32 tmp;
  104. u8 crystalcap = rtlpriv->efuse.crystalcap;
  105. rtl8723_phy_init_bb_rf_reg_def(hw);
  106. regval = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
  107. rtl_write_word(rtlpriv, REG_SYS_FUNC_EN,
  108. regval | BIT(13) | BIT(0) | BIT(1));
  109. rtl_write_byte(rtlpriv, REG_RF_CTRL, RF_EN | RF_RSTB | RF_SDMRSTB);
  110. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN,
  111. FEN_PPLL | FEN_PCIEA | FEN_DIO_PCIE |
  112. FEN_BB_GLB_RSTN | FEN_BBRSTB);
  113. tmp = rtl_read_dword(rtlpriv, 0x4c);
  114. rtl_write_dword(rtlpriv, 0x4c, tmp | BIT(23));
  115. rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL + 1, 0x80);
  116. if (b_reg_hwparafile == 1)
  117. rtstatus = _rtl8723be_phy_bb8723b_config_parafile(hw);
  118. crystalcap = crystalcap & 0x3F;
  119. rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0xFFF000,
  120. (crystalcap | crystalcap << 6));
  121. return rtstatus;
  122. }
  123. bool rtl8723be_phy_rf_config(struct ieee80211_hw *hw)
  124. {
  125. return rtl8723be_phy_rf6052_config(hw);
  126. }
  127. static bool _rtl8723be_check_condition(struct ieee80211_hw *hw,
  128. const u32 condition)
  129. {
  130. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  131. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  132. u32 _board = rtlefuse->board_type; /*need efuse define*/
  133. u32 _interface = rtlhal->interface;
  134. u32 _platform = 0x08;/*SupportPlatform */
  135. u32 cond = condition;
  136. if (condition == 0xCDCDCDCD)
  137. return true;
  138. cond = condition & 0xFF;
  139. if ((_board & cond) == 0 && cond != 0x1F)
  140. return false;
  141. cond = condition & 0xFF00;
  142. cond = cond >> 8;
  143. if ((_interface & cond) == 0 && cond != 0x07)
  144. return false;
  145. cond = condition & 0xFF0000;
  146. cond = cond >> 16;
  147. if ((_platform & cond) == 0 && cond != 0x0F)
  148. return false;
  149. return true;
  150. }
  151. static void _rtl8723be_config_rf_reg(struct ieee80211_hw *hw, u32 addr,
  152. u32 data, enum radio_path rfpath,
  153. u32 regaddr)
  154. {
  155. if (addr == 0xfe || addr == 0xffe) {
  156. /* In order not to disturb BT music
  157. * when wifi init.(1ant NIC only)
  158. */
  159. mdelay(50);
  160. } else {
  161. rtl_set_rfreg(hw, rfpath, regaddr, RFREG_OFFSET_MASK, data);
  162. udelay(1);
  163. }
  164. }
  165. static void _rtl8723be_config_rf_radio_a(struct ieee80211_hw *hw,
  166. u32 addr, u32 data)
  167. {
  168. u32 content = 0x1000; /*RF Content: radio_a_txt*/
  169. u32 maskforphyset = (u32)(content & 0xE000);
  170. _rtl8723be_config_rf_reg(hw, addr, data, RF90_PATH_A,
  171. addr | maskforphyset);
  172. }
  173. static void _rtl8723be_phy_init_tx_power_by_rate(struct ieee80211_hw *hw)
  174. {
  175. struct rtl_priv *rtlpriv = rtl_priv(hw);
  176. struct rtl_phy *rtlphy = &rtlpriv->phy;
  177. u8 band, path, txnum, section;
  178. for (band = BAND_ON_2_4G; band <= BAND_ON_5G; ++band)
  179. for (path = 0; path < TX_PWR_BY_RATE_NUM_RF; ++path)
  180. for (txnum = 0; txnum < TX_PWR_BY_RATE_NUM_RF; ++txnum)
  181. for (section = 0;
  182. section < TX_PWR_BY_RATE_NUM_SECTION;
  183. ++section)
  184. rtlphy->tx_power_by_rate_offset
  185. [band][path][txnum][section] = 0;
  186. }
  187. static void _rtl8723be_config_bb_reg(struct ieee80211_hw *hw,
  188. u32 addr, u32 data)
  189. {
  190. if (addr == 0xfe) {
  191. mdelay(50);
  192. } else if (addr == 0xfd) {
  193. mdelay(5);
  194. } else if (addr == 0xfc) {
  195. mdelay(1);
  196. } else if (addr == 0xfb) {
  197. udelay(50);
  198. } else if (addr == 0xfa) {
  199. udelay(5);
  200. } else if (addr == 0xf9) {
  201. udelay(1);
  202. } else {
  203. rtl_set_bbreg(hw, addr, MASKDWORD, data);
  204. udelay(1);
  205. }
  206. }
  207. static void _rtl8723be_phy_set_txpower_by_rate_base(struct ieee80211_hw *hw,
  208. u8 band,
  209. u8 path, u8 rate_section,
  210. u8 txnum, u8 value)
  211. {
  212. struct rtl_priv *rtlpriv = rtl_priv(hw);
  213. struct rtl_phy *rtlphy = &rtlpriv->phy;
  214. if (path > RF90_PATH_D) {
  215. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  216. "Invalid Rf Path %d in phy_SetTxPowerByRatBase()\n",
  217. path);
  218. return;
  219. }
  220. if (band == BAND_ON_2_4G) {
  221. switch (rate_section) {
  222. case CCK:
  223. rtlphy->txpwr_by_rate_base_24g[path][txnum][0] = value;
  224. break;
  225. case OFDM:
  226. rtlphy->txpwr_by_rate_base_24g[path][txnum][1] = value;
  227. break;
  228. case HT_MCS0_MCS7:
  229. rtlphy->txpwr_by_rate_base_24g[path][txnum][2] = value;
  230. break;
  231. case HT_MCS8_MCS15:
  232. rtlphy->txpwr_by_rate_base_24g[path][txnum][3] = value;
  233. break;
  234. default:
  235. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  236. "Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_SetTxPowerByRateBase()\n",
  237. rate_section, path, txnum);
  238. break;
  239. };
  240. } else {
  241. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  242. "Invalid Band %d in PHY_SetTxPowerByRateBase()\n",
  243. band);
  244. }
  245. }
  246. static u8 _rtl8723be_phy_get_txpower_by_rate_base(struct ieee80211_hw *hw,
  247. u8 band, u8 path, u8 txnum,
  248. u8 rate_section)
  249. {
  250. struct rtl_priv *rtlpriv = rtl_priv(hw);
  251. struct rtl_phy *rtlphy = &rtlpriv->phy;
  252. u8 value = 0;
  253. if (path > RF90_PATH_D) {
  254. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  255. "Invalid Rf Path %d in PHY_GetTxPowerByRateBase()\n",
  256. path);
  257. return 0;
  258. }
  259. if (band == BAND_ON_2_4G) {
  260. switch (rate_section) {
  261. case CCK:
  262. value = rtlphy->txpwr_by_rate_base_24g[path][txnum][0];
  263. break;
  264. case OFDM:
  265. value = rtlphy->txpwr_by_rate_base_24g[path][txnum][1];
  266. break;
  267. case HT_MCS0_MCS7:
  268. value = rtlphy->txpwr_by_rate_base_24g[path][txnum][2];
  269. break;
  270. case HT_MCS8_MCS15:
  271. value = rtlphy->txpwr_by_rate_base_24g[path][txnum][3];
  272. break;
  273. default:
  274. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  275. "Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_GetTxPowerByRateBase()\n",
  276. rate_section, path, txnum);
  277. break;
  278. };
  279. } else {
  280. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  281. "Invalid Band %d in PHY_GetTxPowerByRateBase()\n",
  282. band);
  283. }
  284. return value;
  285. }
  286. static void _rtl8723be_phy_store_txpower_by_rate_base(struct ieee80211_hw *hw)
  287. {
  288. struct rtl_priv *rtlpriv = rtl_priv(hw);
  289. struct rtl_phy *rtlphy = &rtlpriv->phy;
  290. u16 rawvalue = 0;
  291. u8 base = 0, path = 0;
  292. for (path = RF90_PATH_A; path <= RF90_PATH_B; ++path) {
  293. if (path == RF90_PATH_A) {
  294. rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
  295. [BAND_ON_2_4G][path][RF_1TX][3] >> 24) & 0xFF;
  296. base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
  297. _rtl8723be_phy_set_txpower_by_rate_base(hw,
  298. BAND_ON_2_4G, path, CCK, RF_1TX, base);
  299. } else if (path == RF90_PATH_B) {
  300. rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
  301. [BAND_ON_2_4G][path][RF_1TX][3] >> 0) & 0xFF;
  302. base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
  303. _rtl8723be_phy_set_txpower_by_rate_base(hw,
  304. BAND_ON_2_4G,
  305. path, CCK,
  306. RF_1TX, base);
  307. }
  308. rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
  309. [BAND_ON_2_4G][path][RF_1TX][1] >> 24) & 0xFF;
  310. base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
  311. _rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
  312. path, OFDM, RF_1TX,
  313. base);
  314. rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
  315. [BAND_ON_2_4G][path][RF_1TX][5] >> 24) & 0xFF;
  316. base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
  317. _rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
  318. path, HT_MCS0_MCS7,
  319. RF_1TX, base);
  320. rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
  321. [BAND_ON_2_4G][path][RF_2TX][7] >> 24) & 0xFF;
  322. base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
  323. _rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
  324. path, HT_MCS8_MCS15,
  325. RF_2TX, base);
  326. }
  327. }
  328. static void _phy_convert_txpower_dbm_to_relative_value(u32 *data, u8 start,
  329. u8 end, u8 base_val)
  330. {
  331. char i = 0;
  332. u8 temp_value = 0;
  333. u32 temp_data = 0;
  334. for (i = 3; i >= 0; --i) {
  335. if (i >= start && i <= end) {
  336. /* Get the exact value */
  337. temp_value = (u8)(*data >> (i * 8)) & 0xF;
  338. temp_value += ((u8)((*data >> (i*8 + 4)) & 0xF)) * 10;
  339. /* Change the value to a relative value */
  340. temp_value = (temp_value > base_val) ?
  341. temp_value - base_val :
  342. base_val - temp_value;
  343. } else {
  344. temp_value = (u8)(*data >> (i * 8)) & 0xFF;
  345. }
  346. temp_data <<= 8;
  347. temp_data |= temp_value;
  348. }
  349. *data = temp_data;
  350. }
  351. static void _rtl8723be_phy_convert_txpower_dbm_to_relative_value(
  352. struct ieee80211_hw *hw)
  353. {
  354. struct rtl_priv *rtlpriv = rtl_priv(hw);
  355. struct rtl_phy *rtlphy = &rtlpriv->phy;
  356. u8 base = 0, rfpath = RF90_PATH_A;
  357. base = _rtl8723be_phy_get_txpower_by_rate_base(hw,
  358. BAND_ON_2_4G, rfpath, RF_1TX, CCK);
  359. _phy_convert_txpower_dbm_to_relative_value(
  360. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][2],
  361. 1, 1, base);
  362. _phy_convert_txpower_dbm_to_relative_value(
  363. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][3],
  364. 1, 3, base);
  365. base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath,
  366. RF_1TX, OFDM);
  367. _phy_convert_txpower_dbm_to_relative_value(
  368. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][0],
  369. 0, 3, base);
  370. _phy_convert_txpower_dbm_to_relative_value(
  371. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][1],
  372. 0, 3, base);
  373. base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G,
  374. rfpath, RF_1TX, HT_MCS0_MCS7);
  375. _phy_convert_txpower_dbm_to_relative_value(
  376. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][4],
  377. 0, 3, base);
  378. _phy_convert_txpower_dbm_to_relative_value(
  379. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][5],
  380. 0, 3, base);
  381. base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G,
  382. rfpath, RF_2TX,
  383. HT_MCS8_MCS15);
  384. _phy_convert_txpower_dbm_to_relative_value(
  385. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][6],
  386. 0, 3, base);
  387. _phy_convert_txpower_dbm_to_relative_value(
  388. &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][7],
  389. 0, 3, base);
  390. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  391. "<===_rtl8723be_phy_convert_txpower_dbm_to_relative_value()\n");
  392. }
  393. static void phy_txpower_by_rate_config(struct ieee80211_hw *hw)
  394. {
  395. _rtl8723be_phy_store_txpower_by_rate_base(hw);
  396. _rtl8723be_phy_convert_txpower_dbm_to_relative_value(hw);
  397. }
  398. static bool _rtl8723be_phy_bb8723b_config_parafile(struct ieee80211_hw *hw)
  399. {
  400. struct rtl_priv *rtlpriv = rtl_priv(hw);
  401. struct rtl_phy *rtlphy = &rtlpriv->phy;
  402. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  403. bool rtstatus;
  404. rtstatus = _rtl8723be_phy_config_bb_with_headerfile(hw,
  405. BASEBAND_CONFIG_PHY_REG);
  406. if (!rtstatus) {
  407. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Write BB Reg Fail!!");
  408. return false;
  409. }
  410. _rtl8723be_phy_init_tx_power_by_rate(hw);
  411. if (!rtlefuse->autoload_failflag) {
  412. rtlphy->pwrgroup_cnt = 0;
  413. rtstatus = _rtl8723be_phy_config_bb_with_pgheaderfile(hw,
  414. BASEBAND_CONFIG_PHY_REG);
  415. }
  416. phy_txpower_by_rate_config(hw);
  417. if (!rtstatus) {
  418. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "BB_PG Reg Fail!!");
  419. return false;
  420. }
  421. rtstatus = _rtl8723be_phy_config_bb_with_headerfile(hw,
  422. BASEBAND_CONFIG_AGC_TAB);
  423. if (!rtstatus) {
  424. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "AGC Table Fail\n");
  425. return false;
  426. }
  427. rtlphy->cck_high_power = (bool)(rtl_get_bbreg(hw,
  428. RFPGA0_XA_HSSIPARAMETER2,
  429. 0x200));
  430. return true;
  431. }
  432. static bool _rtl8723be_phy_config_mac_with_headerfile(struct ieee80211_hw *hw)
  433. {
  434. struct rtl_priv *rtlpriv = rtl_priv(hw);
  435. u32 i;
  436. u32 arraylength;
  437. u32 *ptrarray;
  438. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Read rtl8723beMACPHY_Array\n");
  439. arraylength = RTL8723BEMAC_1T_ARRAYLEN;
  440. ptrarray = RTL8723BEMAC_1T_ARRAY;
  441. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  442. "Img:RTL8723bEMAC_1T_ARRAY LEN %d\n", arraylength);
  443. for (i = 0; i < arraylength; i = i + 2)
  444. rtl_write_byte(rtlpriv, ptrarray[i], (u8)ptrarray[i + 1]);
  445. return true;
  446. }
  447. static bool _rtl8723be_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
  448. u8 configtype)
  449. {
  450. #define READ_NEXT_PAIR(v1, v2, i) \
  451. do { \
  452. i += 2; \
  453. v1 = array_table[i];\
  454. v2 = array_table[i+1]; \
  455. } while (0)
  456. int i;
  457. u32 *array_table;
  458. u16 arraylen;
  459. struct rtl_priv *rtlpriv = rtl_priv(hw);
  460. u32 v1 = 0, v2 = 0;
  461. if (configtype == BASEBAND_CONFIG_PHY_REG) {
  462. arraylen = RTL8723BEPHY_REG_1TARRAYLEN;
  463. array_table = RTL8723BEPHY_REG_1TARRAY;
  464. for (i = 0; i < arraylen; i = i + 2) {
  465. v1 = array_table[i];
  466. v2 = array_table[i+1];
  467. if (v1 < 0xcdcdcdcd) {
  468. _rtl8723be_config_bb_reg(hw, v1, v2);
  469. } else {/*This line is the start line of branch.*/
  470. /* to protect READ_NEXT_PAIR not overrun */
  471. if (i >= arraylen - 2)
  472. break;
  473. if (!_rtl8723be_check_condition(hw,
  474. array_table[i])) {
  475. /*Discard the following
  476. *(offset, data) pairs
  477. */
  478. READ_NEXT_PAIR(v1, v2, i);
  479. while (v2 != 0xDEAD &&
  480. v2 != 0xCDEF &&
  481. v2 != 0xCDCD &&
  482. i < arraylen - 2) {
  483. READ_NEXT_PAIR(v1, v2, i);
  484. }
  485. i -= 2; /* prevent from for-loop += 2*/
  486. /*Configure matched pairs and
  487. *skip to end of if-else.
  488. */
  489. } else {
  490. READ_NEXT_PAIR(v1, v2, i);
  491. while (v2 != 0xDEAD &&
  492. v2 != 0xCDEF &&
  493. v2 != 0xCDCD &&
  494. i < arraylen - 2) {
  495. _rtl8723be_config_bb_reg(hw,
  496. v1, v2);
  497. READ_NEXT_PAIR(v1, v2, i);
  498. }
  499. while (v2 != 0xDEAD && i < arraylen - 2)
  500. READ_NEXT_PAIR(v1, v2, i);
  501. }
  502. }
  503. }
  504. } else if (configtype == BASEBAND_CONFIG_AGC_TAB) {
  505. arraylen = RTL8723BEAGCTAB_1TARRAYLEN;
  506. array_table = RTL8723BEAGCTAB_1TARRAY;
  507. for (i = 0; i < arraylen; i = i + 2) {
  508. v1 = array_table[i];
  509. v2 = array_table[i+1];
  510. if (v1 < 0xCDCDCDCD) {
  511. rtl_set_bbreg(hw, array_table[i],
  512. MASKDWORD,
  513. array_table[i + 1]);
  514. udelay(1);
  515. continue;
  516. } else {/*This line is the start line of branch.*/
  517. /* to protect READ_NEXT_PAIR not overrun */
  518. if (i >= arraylen - 2)
  519. break;
  520. if (!_rtl8723be_check_condition(hw,
  521. array_table[i])) {
  522. /*Discard the following
  523. *(offset, data) pairs
  524. */
  525. READ_NEXT_PAIR(v1, v2, i);
  526. while (v2 != 0xDEAD &&
  527. v2 != 0xCDEF &&
  528. v2 != 0xCDCD &&
  529. i < arraylen - 2) {
  530. READ_NEXT_PAIR(v1, v2, i);
  531. }
  532. i -= 2; /* prevent from for-loop += 2*/
  533. /*Configure matched pairs and
  534. *skip to end of if-else.
  535. */
  536. } else {
  537. READ_NEXT_PAIR(v1, v2, i);
  538. while (v2 != 0xDEAD &&
  539. v2 != 0xCDEF &&
  540. v2 != 0xCDCD &&
  541. i < arraylen - 2) {
  542. rtl_set_bbreg(hw, array_table[i],
  543. MASKDWORD,
  544. array_table[i + 1]);
  545. udelay(1);
  546. READ_NEXT_PAIR(v1, v2, i);
  547. }
  548. while (v2 != 0xDEAD && i < arraylen - 2)
  549. READ_NEXT_PAIR(v1, v2, i);
  550. }
  551. }
  552. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  553. "The agctab_array_table[0] is %x Rtl818EEPHY_REGArray[1] is %x\n",
  554. array_table[i], array_table[i + 1]);
  555. }
  556. }
  557. return true;
  558. }
  559. static u8 _rtl8723be_get_rate_section_index(u32 regaddr)
  560. {
  561. u8 index = 0;
  562. switch (regaddr) {
  563. case RTXAGC_A_RATE18_06:
  564. index = 0;
  565. break;
  566. case RTXAGC_A_RATE54_24:
  567. index = 1;
  568. break;
  569. case RTXAGC_A_CCK1_MCS32:
  570. index = 2;
  571. break;
  572. case RTXAGC_B_CCK11_A_CCK2_11:
  573. index = 3;
  574. break;
  575. case RTXAGC_A_MCS03_MCS00:
  576. index = 4;
  577. break;
  578. case RTXAGC_A_MCS07_MCS04:
  579. index = 5;
  580. break;
  581. case RTXAGC_A_MCS11_MCS08:
  582. index = 6;
  583. break;
  584. case RTXAGC_A_MCS15_MCS12:
  585. index = 7;
  586. break;
  587. case RTXAGC_B_RATE18_06:
  588. index = 0;
  589. break;
  590. case RTXAGC_B_RATE54_24:
  591. index = 1;
  592. break;
  593. case RTXAGC_B_CCK1_55_MCS32:
  594. index = 2;
  595. break;
  596. case RTXAGC_B_MCS03_MCS00:
  597. index = 4;
  598. break;
  599. case RTXAGC_B_MCS07_MCS04:
  600. index = 5;
  601. break;
  602. case RTXAGC_B_MCS11_MCS08:
  603. index = 6;
  604. break;
  605. case RTXAGC_B_MCS15_MCS12:
  606. index = 7;
  607. break;
  608. default:
  609. regaddr &= 0xFFF;
  610. if (regaddr >= 0xC20 && regaddr <= 0xC4C)
  611. index = (u8)((regaddr - 0xC20) / 4);
  612. else if (regaddr >= 0xE20 && regaddr <= 0xE4C)
  613. index = (u8)((regaddr - 0xE20) / 4);
  614. break;
  615. };
  616. return index;
  617. }
  618. static void _rtl8723be_store_tx_power_by_rate(struct ieee80211_hw *hw,
  619. u32 band, u32 rfpath,
  620. u32 txnum, u32 regaddr,
  621. u32 bitmask, u32 data)
  622. {
  623. struct rtl_priv *rtlpriv = rtl_priv(hw);
  624. struct rtl_phy *rtlphy = &rtlpriv->phy;
  625. u8 rate_section = _rtl8723be_get_rate_section_index(regaddr);
  626. if (band != BAND_ON_2_4G && band != BAND_ON_5G) {
  627. RT_TRACE(rtlpriv, FPHY, PHY_TXPWR, "Invalid Band %d\n", band);
  628. return;
  629. }
  630. if (rfpath > MAX_RF_PATH - 1) {
  631. RT_TRACE(rtlpriv, FPHY, PHY_TXPWR,
  632. "Invalid RfPath %d\n", rfpath);
  633. return;
  634. }
  635. if (txnum > MAX_RF_PATH - 1) {
  636. RT_TRACE(rtlpriv, FPHY, PHY_TXPWR, "Invalid TxNum %d\n", txnum);
  637. return;
  638. }
  639. rtlphy->tx_power_by_rate_offset[band][rfpath][txnum][rate_section] =
  640. data;
  641. }
  642. static bool _rtl8723be_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
  643. u8 configtype)
  644. {
  645. struct rtl_priv *rtlpriv = rtl_priv(hw);
  646. int i;
  647. u32 *phy_regarray_table_pg;
  648. u16 phy_regarray_pg_len;
  649. u32 v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0;
  650. phy_regarray_pg_len = RTL8723BEPHY_REG_ARRAY_PGLEN;
  651. phy_regarray_table_pg = RTL8723BEPHY_REG_ARRAY_PG;
  652. if (configtype == BASEBAND_CONFIG_PHY_REG) {
  653. for (i = 0; i < phy_regarray_pg_len; i = i + 6) {
  654. v1 = phy_regarray_table_pg[i];
  655. v2 = phy_regarray_table_pg[i+1];
  656. v3 = phy_regarray_table_pg[i+2];
  657. v4 = phy_regarray_table_pg[i+3];
  658. v5 = phy_regarray_table_pg[i+4];
  659. v6 = phy_regarray_table_pg[i+5];
  660. if (v1 < 0xcdcdcdcd) {
  661. if (phy_regarray_table_pg[i] == 0xfe ||
  662. phy_regarray_table_pg[i] == 0xffe)
  663. mdelay(50);
  664. else
  665. _rtl8723be_store_tx_power_by_rate(hw,
  666. v1, v2, v3, v4, v5, v6);
  667. continue;
  668. }
  669. }
  670. } else {
  671. RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
  672. "configtype != BaseBand_Config_PHY_REG\n");
  673. }
  674. return true;
  675. }
  676. bool rtl8723be_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
  677. enum radio_path rfpath)
  678. {
  679. #define READ_NEXT_RF_PAIR(v1, v2, i) \
  680. do { \
  681. i += 2; \
  682. v1 = radioa_array_table[i]; \
  683. v2 = radioa_array_table[i+1]; \
  684. } while (0)
  685. int i;
  686. bool rtstatus = true;
  687. u32 *radioa_array_table;
  688. u16 radioa_arraylen;
  689. struct rtl_priv *rtlpriv = rtl_priv(hw);
  690. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  691. u32 v1 = 0, v2 = 0;
  692. radioa_arraylen = RTL8723BE_RADIOA_1TARRAYLEN;
  693. radioa_array_table = RTL8723BE_RADIOA_1TARRAY;
  694. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  695. "Radio_A:RTL8723BE_RADIOA_1TARRAY %d\n", radioa_arraylen);
  696. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Radio No %x\n", rfpath);
  697. rtstatus = true;
  698. switch (rfpath) {
  699. case RF90_PATH_A:
  700. for (i = 0; i < radioa_arraylen; i = i + 2) {
  701. v1 = radioa_array_table[i];
  702. v2 = radioa_array_table[i+1];
  703. if (v1 < 0xcdcdcdcd) {
  704. _rtl8723be_config_rf_radio_a(hw, v1, v2);
  705. } else {/*This line is the start line of branch.*/
  706. /* to protect READ_NEXT_PAIR not overrun */
  707. if (i >= radioa_arraylen - 2)
  708. break;
  709. if (!_rtl8723be_check_condition(hw,
  710. radioa_array_table[i])) {
  711. /*Discard the following
  712. *(offset, data) pairs
  713. */
  714. READ_NEXT_RF_PAIR(v1, v2, i);
  715. while (v2 != 0xDEAD &&
  716. v2 != 0xCDEF &&
  717. v2 != 0xCDCD &&
  718. i < radioa_arraylen - 2) {
  719. READ_NEXT_RF_PAIR(v1, v2, i);
  720. }
  721. i -= 2; /* prevent from for-loop += 2*/
  722. } else {
  723. /*Configure matched pairs
  724. *and skip to end of if-else.
  725. */
  726. READ_NEXT_RF_PAIR(v1, v2, i);
  727. while (v2 != 0xDEAD &&
  728. v2 != 0xCDEF &&
  729. v2 != 0xCDCD &&
  730. i < radioa_arraylen - 2) {
  731. _rtl8723be_config_rf_radio_a(hw,
  732. v1, v2);
  733. READ_NEXT_RF_PAIR(v1, v2, i);
  734. }
  735. while (v2 != 0xDEAD &&
  736. i < radioa_arraylen - 2) {
  737. READ_NEXT_RF_PAIR(v1, v2, i);
  738. }
  739. }
  740. }
  741. }
  742. if (rtlhal->oem_id == RT_CID_819X_HP)
  743. _rtl8723be_config_rf_radio_a(hw, 0x52, 0x7E4BD);
  744. break;
  745. case RF90_PATH_B:
  746. case RF90_PATH_C:
  747. break;
  748. case RF90_PATH_D:
  749. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  750. "switch case not process\n");
  751. break;
  752. }
  753. return true;
  754. }
  755. void rtl8723be_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
  756. {
  757. struct rtl_priv *rtlpriv = rtl_priv(hw);
  758. struct rtl_phy *rtlphy = &rtlpriv->phy;
  759. rtlphy->default_initialgain[0] =
  760. (u8)rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
  761. rtlphy->default_initialgain[1] =
  762. (u8)rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
  763. rtlphy->default_initialgain[2] =
  764. (u8)rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
  765. rtlphy->default_initialgain[3] =
  766. (u8)rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
  767. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  768. "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
  769. rtlphy->default_initialgain[0],
  770. rtlphy->default_initialgain[1],
  771. rtlphy->default_initialgain[2],
  772. rtlphy->default_initialgain[3]);
  773. rtlphy->framesync = (u8)rtl_get_bbreg(hw, ROFDM0_RXDETECTOR3,
  774. MASKBYTE0);
  775. rtlphy->framesync_c34 = rtl_get_bbreg(hw, ROFDM0_RXDETECTOR2,
  776. MASKDWORD);
  777. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  778. "Default framesync (0x%x) = 0x%x\n",
  779. ROFDM0_RXDETECTOR3, rtlphy->framesync);
  780. }
  781. static u8 _rtl8723be_phy_get_ratesection_intxpower_byrate(enum radio_path path,
  782. u8 rate)
  783. {
  784. u8 rate_section = 0;
  785. switch (rate) {
  786. case DESC92C_RATE1M:
  787. rate_section = 2;
  788. break;
  789. case DESC92C_RATE2M:
  790. case DESC92C_RATE5_5M:
  791. if (path == RF90_PATH_A)
  792. rate_section = 3;
  793. else if (path == RF90_PATH_B)
  794. rate_section = 2;
  795. break;
  796. case DESC92C_RATE11M:
  797. rate_section = 3;
  798. break;
  799. case DESC92C_RATE6M:
  800. case DESC92C_RATE9M:
  801. case DESC92C_RATE12M:
  802. case DESC92C_RATE18M:
  803. rate_section = 0;
  804. break;
  805. case DESC92C_RATE24M:
  806. case DESC92C_RATE36M:
  807. case DESC92C_RATE48M:
  808. case DESC92C_RATE54M:
  809. rate_section = 1;
  810. break;
  811. case DESC92C_RATEMCS0:
  812. case DESC92C_RATEMCS1:
  813. case DESC92C_RATEMCS2:
  814. case DESC92C_RATEMCS3:
  815. rate_section = 4;
  816. break;
  817. case DESC92C_RATEMCS4:
  818. case DESC92C_RATEMCS5:
  819. case DESC92C_RATEMCS6:
  820. case DESC92C_RATEMCS7:
  821. rate_section = 5;
  822. break;
  823. case DESC92C_RATEMCS8:
  824. case DESC92C_RATEMCS9:
  825. case DESC92C_RATEMCS10:
  826. case DESC92C_RATEMCS11:
  827. rate_section = 6;
  828. break;
  829. case DESC92C_RATEMCS12:
  830. case DESC92C_RATEMCS13:
  831. case DESC92C_RATEMCS14:
  832. case DESC92C_RATEMCS15:
  833. rate_section = 7;
  834. break;
  835. default:
  836. RT_ASSERT(true, "Rate_Section is Illegal\n");
  837. break;
  838. }
  839. return rate_section;
  840. }
  841. static u8 _rtl8723be_get_txpower_by_rate(struct ieee80211_hw *hw,
  842. enum band_type band,
  843. enum radio_path rfpath, u8 rate)
  844. {
  845. struct rtl_priv *rtlpriv = rtl_priv(hw);
  846. struct rtl_phy *rtlphy = &rtlpriv->phy;
  847. u8 shift = 0, rate_section, tx_num;
  848. char tx_pwr_diff = 0;
  849. rate_section = _rtl8723be_phy_get_ratesection_intxpower_byrate(rfpath,
  850. rate);
  851. tx_num = RF_TX_NUM_NONIMPLEMENT;
  852. if (tx_num == RF_TX_NUM_NONIMPLEMENT) {
  853. if (rate >= DESC92C_RATEMCS8 && rate <= DESC92C_RATEMCS15)
  854. tx_num = RF_2TX;
  855. else
  856. tx_num = RF_1TX;
  857. }
  858. switch (rate) {
  859. case DESC92C_RATE6M:
  860. case DESC92C_RATE24M:
  861. case DESC92C_RATEMCS0:
  862. case DESC92C_RATEMCS4:
  863. case DESC92C_RATEMCS8:
  864. case DESC92C_RATEMCS12:
  865. shift = 0;
  866. break;
  867. case DESC92C_RATE1M:
  868. case DESC92C_RATE2M:
  869. case DESC92C_RATE9M:
  870. case DESC92C_RATE36M:
  871. case DESC92C_RATEMCS1:
  872. case DESC92C_RATEMCS5:
  873. case DESC92C_RATEMCS9:
  874. case DESC92C_RATEMCS13:
  875. shift = 8;
  876. break;
  877. case DESC92C_RATE5_5M:
  878. case DESC92C_RATE12M:
  879. case DESC92C_RATE48M:
  880. case DESC92C_RATEMCS2:
  881. case DESC92C_RATEMCS6:
  882. case DESC92C_RATEMCS10:
  883. case DESC92C_RATEMCS14:
  884. shift = 16;
  885. break;
  886. case DESC92C_RATE11M:
  887. case DESC92C_RATE18M:
  888. case DESC92C_RATE54M:
  889. case DESC92C_RATEMCS3:
  890. case DESC92C_RATEMCS7:
  891. case DESC92C_RATEMCS11:
  892. case DESC92C_RATEMCS15:
  893. shift = 24;
  894. break;
  895. default:
  896. RT_ASSERT(true, "Rate_Section is Illegal\n");
  897. break;
  898. }
  899. tx_pwr_diff = (u8)(rtlphy->tx_power_by_rate_offset[band][rfpath][tx_num]
  900. [rate_section] >> shift) & 0xff;
  901. return tx_pwr_diff;
  902. }
  903. static u8 _rtl8723be_get_txpower_index(struct ieee80211_hw *hw, u8 path,
  904. u8 rate, u8 bandwidth, u8 channel)
  905. {
  906. struct rtl_priv *rtlpriv = rtl_priv(hw);
  907. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  908. u8 index = (channel - 1);
  909. u8 txpower;
  910. u8 power_diff_byrate = 0;
  911. if (channel > 14 || channel < 1) {
  912. index = 0;
  913. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  914. "Illegal channel!\n");
  915. }
  916. if (RX_HAL_IS_CCK_RATE(rate))
  917. txpower = rtlefuse->txpwrlevel_cck[path][index];
  918. else if (DESC92C_RATE6M <= rate)
  919. txpower = rtlefuse->txpwrlevel_ht40_1s[path][index];
  920. else
  921. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  922. "invalid rate\n");
  923. if (DESC92C_RATE6M <= rate && rate <= DESC92C_RATE54M &&
  924. !RX_HAL_IS_CCK_RATE(rate))
  925. txpower += rtlefuse->txpwr_legacyhtdiff[0][TX_1S];
  926. if (bandwidth == HT_CHANNEL_WIDTH_20) {
  927. if (DESC92C_RATEMCS0 <= rate && rate <= DESC92C_RATEMCS15)
  928. txpower += rtlefuse->txpwr_ht20diff[0][TX_1S];
  929. if (DESC92C_RATEMCS8 <= rate && rate <= DESC92C_RATEMCS15)
  930. txpower += rtlefuse->txpwr_ht20diff[0][TX_2S];
  931. } else if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
  932. if (DESC92C_RATEMCS0 <= rate && rate <= DESC92C_RATEMCS15)
  933. txpower += rtlefuse->txpwr_ht40diff[0][TX_1S];
  934. if (DESC92C_RATEMCS8 <= rate && rate <= DESC92C_RATEMCS15)
  935. txpower += rtlefuse->txpwr_ht40diff[0][TX_2S];
  936. }
  937. if (rtlefuse->eeprom_regulatory != 2)
  938. power_diff_byrate = _rtl8723be_get_txpower_by_rate(hw,
  939. BAND_ON_2_4G,
  940. path, rate);
  941. txpower += power_diff_byrate;
  942. if (txpower > MAX_POWER_INDEX)
  943. txpower = MAX_POWER_INDEX;
  944. return txpower;
  945. }
  946. static void _rtl8723be_phy_set_txpower_index(struct ieee80211_hw *hw,
  947. u8 power_index, u8 path, u8 rate)
  948. {
  949. struct rtl_priv *rtlpriv = rtl_priv(hw);
  950. if (path == RF90_PATH_A) {
  951. switch (rate) {
  952. case DESC92C_RATE1M:
  953. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_CCK1_MCS32,
  954. MASKBYTE1, power_index);
  955. break;
  956. case DESC92C_RATE2M:
  957. rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
  958. MASKBYTE1, power_index);
  959. break;
  960. case DESC92C_RATE5_5M:
  961. rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
  962. MASKBYTE2, power_index);
  963. break;
  964. case DESC92C_RATE11M:
  965. rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
  966. MASKBYTE3, power_index);
  967. break;
  968. case DESC92C_RATE6M:
  969. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
  970. MASKBYTE0, power_index);
  971. break;
  972. case DESC92C_RATE9M:
  973. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
  974. MASKBYTE1, power_index);
  975. break;
  976. case DESC92C_RATE12M:
  977. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
  978. MASKBYTE2, power_index);
  979. break;
  980. case DESC92C_RATE18M:
  981. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
  982. MASKBYTE3, power_index);
  983. break;
  984. case DESC92C_RATE24M:
  985. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
  986. MASKBYTE0, power_index);
  987. break;
  988. case DESC92C_RATE36M:
  989. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
  990. MASKBYTE1, power_index);
  991. break;
  992. case DESC92C_RATE48M:
  993. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
  994. MASKBYTE2, power_index);
  995. break;
  996. case DESC92C_RATE54M:
  997. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
  998. MASKBYTE3, power_index);
  999. break;
  1000. case DESC92C_RATEMCS0:
  1001. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
  1002. MASKBYTE0, power_index);
  1003. break;
  1004. case DESC92C_RATEMCS1:
  1005. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
  1006. MASKBYTE1, power_index);
  1007. break;
  1008. case DESC92C_RATEMCS2:
  1009. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
  1010. MASKBYTE2, power_index);
  1011. break;
  1012. case DESC92C_RATEMCS3:
  1013. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
  1014. MASKBYTE3, power_index);
  1015. break;
  1016. case DESC92C_RATEMCS4:
  1017. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
  1018. MASKBYTE0, power_index);
  1019. break;
  1020. case DESC92C_RATEMCS5:
  1021. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
  1022. MASKBYTE1, power_index);
  1023. break;
  1024. case DESC92C_RATEMCS6:
  1025. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
  1026. MASKBYTE2, power_index);
  1027. break;
  1028. case DESC92C_RATEMCS7:
  1029. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
  1030. MASKBYTE3, power_index);
  1031. break;
  1032. case DESC92C_RATEMCS8:
  1033. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
  1034. MASKBYTE0, power_index);
  1035. break;
  1036. case DESC92C_RATEMCS9:
  1037. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
  1038. MASKBYTE1, power_index);
  1039. break;
  1040. case DESC92C_RATEMCS10:
  1041. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
  1042. MASKBYTE2, power_index);
  1043. break;
  1044. case DESC92C_RATEMCS11:
  1045. rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
  1046. MASKBYTE3, power_index);
  1047. break;
  1048. default:
  1049. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid Rate!!\n");
  1050. break;
  1051. }
  1052. } else {
  1053. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid RFPath!!\n");
  1054. }
  1055. }
  1056. void rtl8723be_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
  1057. {
  1058. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1059. u8 cck_rates[] = {DESC92C_RATE1M, DESC92C_RATE2M,
  1060. DESC92C_RATE5_5M, DESC92C_RATE11M};
  1061. u8 ofdm_rates[] = {DESC92C_RATE6M, DESC92C_RATE9M,
  1062. DESC92C_RATE12M, DESC92C_RATE18M,
  1063. DESC92C_RATE24M, DESC92C_RATE36M,
  1064. DESC92C_RATE48M, DESC92C_RATE54M};
  1065. u8 ht_rates_1t[] = {DESC92C_RATEMCS0, DESC92C_RATEMCS1,
  1066. DESC92C_RATEMCS2, DESC92C_RATEMCS3,
  1067. DESC92C_RATEMCS4, DESC92C_RATEMCS5,
  1068. DESC92C_RATEMCS6, DESC92C_RATEMCS7};
  1069. u8 i, size;
  1070. u8 power_index;
  1071. if (!rtlefuse->txpwr_fromeprom)
  1072. return;
  1073. size = sizeof(cck_rates) / sizeof(u8);
  1074. for (i = 0; i < size; i++) {
  1075. power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
  1076. cck_rates[i],
  1077. rtl_priv(hw)->phy.current_chan_bw,
  1078. channel);
  1079. _rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
  1080. cck_rates[i]);
  1081. }
  1082. size = sizeof(ofdm_rates) / sizeof(u8);
  1083. for (i = 0; i < size; i++) {
  1084. power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
  1085. ofdm_rates[i],
  1086. rtl_priv(hw)->phy.current_chan_bw,
  1087. channel);
  1088. _rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
  1089. ofdm_rates[i]);
  1090. }
  1091. size = sizeof(ht_rates_1t) / sizeof(u8);
  1092. for (i = 0; i < size; i++) {
  1093. power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
  1094. ht_rates_1t[i],
  1095. rtl_priv(hw)->phy.current_chan_bw,
  1096. channel);
  1097. _rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
  1098. ht_rates_1t[i]);
  1099. }
  1100. }
  1101. void rtl8723be_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
  1102. {
  1103. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1104. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1105. enum io_type iotype;
  1106. if (!is_hal_stop(rtlhal)) {
  1107. switch (operation) {
  1108. case SCAN_OPT_BACKUP_BAND0:
  1109. iotype = IO_CMD_PAUSE_BAND0_DM_BY_SCAN;
  1110. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
  1111. (u8 *)&iotype);
  1112. break;
  1113. case SCAN_OPT_RESTORE:
  1114. iotype = IO_CMD_RESUME_DM_BY_SCAN;
  1115. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
  1116. (u8 *)&iotype);
  1117. break;
  1118. default:
  1119. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1120. "Unknown Scan Backup operation.\n");
  1121. break;
  1122. }
  1123. }
  1124. }
  1125. void rtl8723be_phy_set_bw_mode_callback(struct ieee80211_hw *hw)
  1126. {
  1127. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1128. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1129. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1130. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1131. u8 reg_bw_opmode;
  1132. u8 reg_prsr_rsc;
  1133. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE,
  1134. "Switch to %s bandwidth\n",
  1135. rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
  1136. "20MHz" : "40MHz");
  1137. if (is_hal_stop(rtlhal)) {
  1138. rtlphy->set_bwmode_inprogress = false;
  1139. return;
  1140. }
  1141. reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
  1142. reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
  1143. switch (rtlphy->current_chan_bw) {
  1144. case HT_CHANNEL_WIDTH_20:
  1145. reg_bw_opmode |= BW_OPMODE_20MHZ;
  1146. rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
  1147. break;
  1148. case HT_CHANNEL_WIDTH_20_40:
  1149. reg_bw_opmode &= ~BW_OPMODE_20MHZ;
  1150. rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
  1151. reg_prsr_rsc = (reg_prsr_rsc & 0x90) |
  1152. (mac->cur_40_prime_sc << 5);
  1153. rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
  1154. break;
  1155. default:
  1156. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1157. "unknown bandwidth: %#X\n", rtlphy->current_chan_bw);
  1158. break;
  1159. }
  1160. switch (rtlphy->current_chan_bw) {
  1161. case HT_CHANNEL_WIDTH_20:
  1162. rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
  1163. rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
  1164. /* rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1);*/
  1165. break;
  1166. case HT_CHANNEL_WIDTH_20_40:
  1167. rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
  1168. rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
  1169. rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCK_SIDEBAND,
  1170. (mac->cur_40_prime_sc >> 1));
  1171. rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
  1172. /*rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 0);*/
  1173. rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
  1174. (mac->cur_40_prime_sc ==
  1175. HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
  1176. break;
  1177. default:
  1178. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1179. "unknown bandwidth: %#X\n", rtlphy->current_chan_bw);
  1180. break;
  1181. }
  1182. rtl8723be_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
  1183. rtlphy->set_bwmode_inprogress = false;
  1184. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD, "\n");
  1185. }
  1186. void rtl8723be_phy_set_bw_mode(struct ieee80211_hw *hw,
  1187. enum nl80211_channel_type ch_type)
  1188. {
  1189. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1190. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1191. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1192. u8 tmp_bw = rtlphy->current_chan_bw;
  1193. if (rtlphy->set_bwmode_inprogress)
  1194. return;
  1195. rtlphy->set_bwmode_inprogress = true;
  1196. if ((!is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
  1197. rtl8723be_phy_set_bw_mode_callback(hw);
  1198. } else {
  1199. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1200. "false driver sleep or unload\n");
  1201. rtlphy->set_bwmode_inprogress = false;
  1202. rtlphy->current_chan_bw = tmp_bw;
  1203. }
  1204. }
  1205. void rtl8723be_phy_sw_chnl_callback(struct ieee80211_hw *hw)
  1206. {
  1207. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1208. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1209. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1210. u32 delay;
  1211. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE,
  1212. "switch to channel%d\n", rtlphy->current_channel);
  1213. if (is_hal_stop(rtlhal))
  1214. return;
  1215. do {
  1216. if (!rtlphy->sw_chnl_inprogress)
  1217. break;
  1218. if (!_rtl8723be_phy_sw_chnl_step_by_step(hw,
  1219. rtlphy->current_channel,
  1220. &rtlphy->sw_chnl_stage,
  1221. &rtlphy->sw_chnl_step,
  1222. &delay)) {
  1223. if (delay > 0)
  1224. mdelay(delay);
  1225. else
  1226. continue;
  1227. } else {
  1228. rtlphy->sw_chnl_inprogress = false;
  1229. }
  1230. break;
  1231. } while (true);
  1232. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
  1233. }
  1234. u8 rtl8723be_phy_sw_chnl(struct ieee80211_hw *hw)
  1235. {
  1236. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1237. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1238. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1239. if (rtlphy->sw_chnl_inprogress)
  1240. return 0;
  1241. if (rtlphy->set_bwmode_inprogress)
  1242. return 0;
  1243. RT_ASSERT((rtlphy->current_channel <= 14),
  1244. "WIRELESS_MODE_G but channel>14");
  1245. rtlphy->sw_chnl_inprogress = true;
  1246. rtlphy->sw_chnl_stage = 0;
  1247. rtlphy->sw_chnl_step = 0;
  1248. if (!(is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
  1249. rtl8723be_phy_sw_chnl_callback(hw);
  1250. RT_TRACE(rtlpriv, COMP_CHAN, DBG_LOUD,
  1251. "sw_chnl_inprogress false schdule workitem current channel %d\n",
  1252. rtlphy->current_channel);
  1253. rtlphy->sw_chnl_inprogress = false;
  1254. } else {
  1255. RT_TRACE(rtlpriv, COMP_CHAN, DBG_LOUD,
  1256. "sw_chnl_inprogress false driver sleep or unload\n");
  1257. rtlphy->sw_chnl_inprogress = false;
  1258. }
  1259. return 1;
  1260. }
  1261. static bool _rtl8723be_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
  1262. u8 channel, u8 *stage,
  1263. u8 *step, u32 *delay)
  1264. {
  1265. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1266. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1267. struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
  1268. u32 precommoncmdcnt;
  1269. struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
  1270. u32 postcommoncmdcnt;
  1271. struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
  1272. u32 rfdependcmdcnt;
  1273. struct swchnlcmd *currentcmd = NULL;
  1274. u8 rfpath;
  1275. u8 num_total_rfpath = rtlphy->num_total_rfpath;
  1276. precommoncmdcnt = 0;
  1277. rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
  1278. MAX_PRECMD_CNT,
  1279. CMDID_SET_TXPOWEROWER_LEVEL,
  1280. 0, 0, 0);
  1281. rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
  1282. MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
  1283. postcommoncmdcnt = 0;
  1284. rtl8723_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
  1285. MAX_POSTCMD_CNT, CMDID_END,
  1286. 0, 0, 0);
  1287. rfdependcmdcnt = 0;
  1288. RT_ASSERT((channel >= 1 && channel <= 14),
  1289. "illegal channel for Zebra: %d\n", channel);
  1290. rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
  1291. MAX_RFDEPENDCMD_CNT,
  1292. CMDID_RF_WRITEREG,
  1293. RF_CHNLBW, channel, 10);
  1294. rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
  1295. MAX_RFDEPENDCMD_CNT,
  1296. CMDID_END, 0, 0, 0);
  1297. do {
  1298. switch (*stage) {
  1299. case 0:
  1300. currentcmd = &precommoncmd[*step];
  1301. break;
  1302. case 1:
  1303. currentcmd = &rfdependcmd[*step];
  1304. break;
  1305. case 2:
  1306. currentcmd = &postcommoncmd[*step];
  1307. break;
  1308. default:
  1309. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1310. "Invalid 'stage' = %d, Check it!\n", *stage);
  1311. return true;
  1312. }
  1313. if (currentcmd->cmdid == CMDID_END) {
  1314. if ((*stage) == 2) {
  1315. return true;
  1316. } else {
  1317. (*stage)++;
  1318. (*step) = 0;
  1319. continue;
  1320. }
  1321. }
  1322. switch (currentcmd->cmdid) {
  1323. case CMDID_SET_TXPOWEROWER_LEVEL:
  1324. rtl8723be_phy_set_txpower_level(hw, channel);
  1325. break;
  1326. case CMDID_WRITEPORT_ULONG:
  1327. rtl_write_dword(rtlpriv, currentcmd->para1,
  1328. currentcmd->para2);
  1329. break;
  1330. case CMDID_WRITEPORT_USHORT:
  1331. rtl_write_word(rtlpriv, currentcmd->para1,
  1332. (u16)currentcmd->para2);
  1333. break;
  1334. case CMDID_WRITEPORT_UCHAR:
  1335. rtl_write_byte(rtlpriv, currentcmd->para1,
  1336. (u8)currentcmd->para2);
  1337. break;
  1338. case CMDID_RF_WRITEREG:
  1339. for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
  1340. rtlphy->rfreg_chnlval[rfpath] =
  1341. ((rtlphy->rfreg_chnlval[rfpath] &
  1342. 0xfffffc00) | currentcmd->para2);
  1343. rtl_set_rfreg(hw, (enum radio_path)rfpath,
  1344. currentcmd->para1,
  1345. RFREG_OFFSET_MASK,
  1346. rtlphy->rfreg_chnlval[rfpath]);
  1347. }
  1348. break;
  1349. default:
  1350. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  1351. "switch case not process\n");
  1352. break;
  1353. }
  1354. break;
  1355. } while (true);
  1356. (*delay) = currentcmd->msdelay;
  1357. (*step)++;
  1358. return false;
  1359. }
  1360. static u8 _rtl8723be_phy_path_a_iqk(struct ieee80211_hw *hw)
  1361. {
  1362. u32 reg_eac, reg_e94, reg_e9c, tmp;
  1363. u8 result = 0x00;
  1364. /* leave IQK mode */
  1365. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1366. /* switch to path A */
  1367. rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000000);
  1368. /* enable path A PA in TXIQK mode */
  1369. rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, RFREG_OFFSET_MASK, 0x800a0);
  1370. rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x20000);
  1371. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0003f);
  1372. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xc7f87);
  1373. /* 1. TX IQK */
  1374. /* path-A IQK setting */
  1375. /* IQK setting */
  1376. rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
  1377. rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
  1378. /* path-A IQK setting */
  1379. rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
  1380. rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
  1381. rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1382. rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1383. rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x821403ea);
  1384. rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28160000);
  1385. rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
  1386. rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
  1387. /* LO calibration setting */
  1388. rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x00462911);
  1389. /* enter IQK mode */
  1390. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1391. /* One shot, path A LOK & IQK */
  1392. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
  1393. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
  1394. mdelay(IQK_DELAY_TIME);
  1395. /* leave IQK mode */
  1396. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1397. /* Check failed */
  1398. reg_eac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  1399. reg_e94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
  1400. reg_e9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
  1401. if (!(reg_eac & BIT(28)) &&
  1402. (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
  1403. (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
  1404. result |= 0x01;
  1405. else /* if Tx not OK, ignore Rx */
  1406. return result;
  1407. /* Allen 20131125 */
  1408. tmp = (reg_e9c & 0x03FF0000) >> 16;
  1409. if ((tmp & 0x200) > 0)
  1410. tmp = 0x400 - tmp;
  1411. if (!(reg_eac & BIT(28)) &&
  1412. (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
  1413. (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
  1414. (tmp < 0xf))
  1415. result |= 0x01;
  1416. else /* if Tx not OK, ignore Rx */
  1417. return result;
  1418. return result;
  1419. }
  1420. /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
  1421. static u8 _rtl8723be_phy_path_a_rx_iqk(struct ieee80211_hw *hw)
  1422. {
  1423. u32 reg_eac, reg_e94, reg_e9c, reg_ea4, u32tmp, tmp;
  1424. u8 result = 0x00;
  1425. /* leave IQK mode */
  1426. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1427. /* switch to path A */
  1428. rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000000);
  1429. /* 1 Get TXIMR setting */
  1430. /* modify RXIQK mode table */
  1431. rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
  1432. rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
  1433. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
  1434. /* LNA2 off, PA on for Dcut */
  1435. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7fb7);
  1436. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1437. /* IQK setting */
  1438. rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
  1439. rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
  1440. /* path-A IQK setting */
  1441. rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
  1442. rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
  1443. rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1444. rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1445. rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82160ff0);
  1446. rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
  1447. rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
  1448. rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
  1449. /* LO calibration setting */
  1450. rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a911);
  1451. /* enter IQK mode */
  1452. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1453. /* One shot, path A LOK & IQK */
  1454. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
  1455. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
  1456. mdelay(IQK_DELAY_TIME);
  1457. /* leave IQK mode */
  1458. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1459. /* Check failed */
  1460. reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
  1461. reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
  1462. reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
  1463. if (!(reg_eac & BIT(28)) &&
  1464. (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
  1465. (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
  1466. result |= 0x01;
  1467. else /* if Tx not OK, ignore Rx */
  1468. return result;
  1469. /* Allen 20131125 */
  1470. tmp = (reg_e9c & 0x03FF0000) >> 16;
  1471. if ((tmp & 0x200) > 0)
  1472. tmp = 0x400 - tmp;
  1473. if (!(reg_eac & BIT(28)) &&
  1474. (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
  1475. (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
  1476. (tmp < 0xf))
  1477. result |= 0x01;
  1478. else /* if Tx not OK, ignore Rx */
  1479. return result;
  1480. u32tmp = 0x80007C00 | (reg_e94 & 0x3FF0000) |
  1481. ((reg_e9c & 0x3FF0000) >> 16);
  1482. rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, u32tmp);
  1483. /* 1 RX IQK */
  1484. /* modify RXIQK mode table */
  1485. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1486. rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
  1487. rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
  1488. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
  1489. /* LAN2 on, PA off for Dcut */
  1490. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7d77);
  1491. /* PA, PAD setting */
  1492. rtl_set_rfreg(hw, RF90_PATH_A, 0xdf, RFREG_OFFSET_MASK, 0xf80);
  1493. rtl_set_rfreg(hw, RF90_PATH_A, 0x55, RFREG_OFFSET_MASK, 0x4021f);
  1494. /* IQK setting */
  1495. rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
  1496. /* path-A IQK setting */
  1497. rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
  1498. rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
  1499. rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1500. rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1501. rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82110000);
  1502. rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x2816001f);
  1503. rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
  1504. rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
  1505. /* LO calibration setting */
  1506. rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a8d1);
  1507. /* enter IQK mode */
  1508. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1509. /* One shot, path A LOK & IQK */
  1510. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
  1511. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
  1512. mdelay(IQK_DELAY_TIME);
  1513. /* leave IQK mode */
  1514. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1515. /* Check failed */
  1516. reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
  1517. reg_ea4 = rtl_get_bbreg(hw, RRX_POWER_BEFORE_IQK_A_2, MASKDWORD);
  1518. /* leave IQK mode */
  1519. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1520. rtl_set_rfreg(hw, RF90_PATH_A, 0xdf, RFREG_OFFSET_MASK, 0x780);
  1521. /* Allen 20131125 */
  1522. tmp = (reg_eac & 0x03FF0000) >> 16;
  1523. if ((tmp & 0x200) > 0)
  1524. tmp = 0x400 - tmp;
  1525. /* if Tx is OK, check whether Rx is OK */
  1526. if (!(reg_eac & BIT(27)) &&
  1527. (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
  1528. (((reg_eac & 0x03FF0000) >> 16) != 0x36))
  1529. result |= 0x02;
  1530. else if (!(reg_eac & BIT(27)) &&
  1531. (((reg_ea4 & 0x03FF0000) >> 16) < 0x110) &&
  1532. (((reg_ea4 & 0x03FF0000) >> 16) > 0xf0) &&
  1533. (tmp < 0xf))
  1534. result |= 0x02;
  1535. return result;
  1536. }
  1537. static u8 _rtl8723be_phy_path_b_iqk(struct ieee80211_hw *hw)
  1538. {
  1539. u32 reg_eac, reg_e94, reg_e9c, tmp;
  1540. u8 result = 0x00;
  1541. /* leave IQK mode */
  1542. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1543. /* switch to path B */
  1544. rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000280);
  1545. /* enable path B PA in TXIQK mode */
  1546. rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
  1547. rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x40fc1);
  1548. /* 1 Tx IQK */
  1549. /* IQK setting */
  1550. rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
  1551. rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
  1552. /* path-A IQK setting */
  1553. rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
  1554. rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
  1555. rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1556. rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1557. rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x821403ea);
  1558. rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
  1559. rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
  1560. rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
  1561. /* LO calibration setting */
  1562. rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x00462911);
  1563. /* enter IQK mode */
  1564. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1565. /* One shot, path B LOK & IQK */
  1566. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
  1567. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
  1568. mdelay(IQK_DELAY_TIME);
  1569. /* leave IQK mode */
  1570. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1571. /* Check failed */
  1572. reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
  1573. reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
  1574. reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
  1575. if (!(reg_eac & BIT(28)) &&
  1576. (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
  1577. (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
  1578. result |= 0x01;
  1579. else
  1580. return result;
  1581. /* Allen 20131125 */
  1582. tmp = (reg_e9c & 0x03FF0000) >> 16;
  1583. if ((tmp & 0x200) > 0)
  1584. tmp = 0x400 - tmp;
  1585. if (!(reg_eac & BIT(28)) &&
  1586. (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
  1587. (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
  1588. (tmp < 0xf))
  1589. result |= 0x01;
  1590. else
  1591. return result;
  1592. return result;
  1593. }
  1594. /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
  1595. static u8 _rtl8723be_phy_path_b_rx_iqk(struct ieee80211_hw *hw)
  1596. {
  1597. u32 reg_e94, reg_e9c, reg_ea4, reg_eac, u32tmp, tmp;
  1598. u8 result = 0x00;
  1599. /* leave IQK mode */
  1600. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1601. /* switch to path B */
  1602. rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000280);
  1603. /* 1 Get TXIMR setting */
  1604. /* modify RXIQK mode table */
  1605. rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, RFREG_OFFSET_MASK, 0x800a0);
  1606. rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
  1607. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
  1608. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7ff7);
  1609. /* open PA S1 & SMIXER */
  1610. rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
  1611. rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x60fed);
  1612. /* IQK setting */
  1613. rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
  1614. rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
  1615. /* path-B IQK setting */
  1616. rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
  1617. rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
  1618. rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1619. rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1620. rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82160ff0);
  1621. rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
  1622. rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
  1623. rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
  1624. /* LO calibration setting */
  1625. rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a911);
  1626. /* enter IQK mode */
  1627. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1628. /* One shot, path B TXIQK @ RXIQK */
  1629. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
  1630. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
  1631. mdelay(IQK_DELAY_TIME);
  1632. /* leave IQK mode */
  1633. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1634. /* Check failed */
  1635. reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
  1636. reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
  1637. reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
  1638. if (!(reg_eac & BIT(28)) &&
  1639. (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
  1640. (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
  1641. result |= 0x01;
  1642. else /* if Tx not OK, ignore Rx */
  1643. return result;
  1644. /* Allen 20131125 */
  1645. tmp = (reg_e9c & 0x03FF0000) >> 16;
  1646. if ((tmp & 0x200) > 0)
  1647. tmp = 0x400 - tmp;
  1648. if (!(reg_eac & BIT(28)) &&
  1649. (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
  1650. (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
  1651. (tmp < 0xf))
  1652. result |= 0x01;
  1653. else
  1654. return result;
  1655. u32tmp = 0x80007C00 | (reg_e94 & 0x3FF0000) |
  1656. ((reg_e9c & 0x3FF0000) >> 16);
  1657. rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, u32tmp);
  1658. /* 1 RX IQK */
  1659. /* <20121009, Kordan> RF Mode = 3 */
  1660. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1661. rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
  1662. rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
  1663. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
  1664. rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7d77);
  1665. rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x0);
  1666. /* open PA S1 & close SMIXER */
  1667. rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
  1668. rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x60fbd);
  1669. /* IQK setting */
  1670. rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
  1671. /* path-B IQK setting */
  1672. rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
  1673. rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
  1674. rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1675. rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
  1676. rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82110000);
  1677. rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x2816001f);
  1678. rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
  1679. rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
  1680. /* LO calibration setting */
  1681. rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a8d1);
  1682. /* enter IQK mode */
  1683. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
  1684. /* One shot, path B LOK & IQK */
  1685. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
  1686. rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
  1687. mdelay(IQK_DELAY_TIME);
  1688. /* leave IQK mode */
  1689. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
  1690. /* Check failed */
  1691. reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
  1692. reg_ea4 = rtl_get_bbreg(hw, RRX_POWER_BEFORE_IQK_A_2, MASKDWORD);
  1693. /* Allen 20131125 */
  1694. tmp = (reg_eac & 0x03FF0000) >> 16;
  1695. if ((tmp & 0x200) > 0)
  1696. tmp = 0x400 - tmp;
  1697. /* if Tx is OK, check whether Rx is OK */
  1698. if (!(reg_eac & BIT(27)) &&
  1699. (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
  1700. (((reg_eac & 0x03FF0000) >> 16) != 0x36))
  1701. result |= 0x02;
  1702. else if (!(reg_eac & BIT(27)) &&
  1703. (((reg_ea4 & 0x03FF0000) >> 16) < 0x110) &&
  1704. (((reg_ea4 & 0x03FF0000) >> 16) > 0xf0) &&
  1705. (tmp < 0xf))
  1706. result |= 0x02;
  1707. else
  1708. return result;
  1709. return result;
  1710. }
  1711. static void _rtl8723be_phy_path_b_fill_iqk_matrix(struct ieee80211_hw *hw,
  1712. bool b_iqk_ok,
  1713. long result[][8],
  1714. u8 final_candidate,
  1715. bool btxonly)
  1716. {
  1717. u32 oldval_1, x, tx1_a, reg;
  1718. long y, tx1_c;
  1719. if (final_candidate == 0xFF) {
  1720. return;
  1721. } else if (b_iqk_ok) {
  1722. oldval_1 = (rtl_get_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
  1723. MASKDWORD) >> 22) & 0x3FF;
  1724. x = result[final_candidate][4];
  1725. if ((x & 0x00000200) != 0)
  1726. x = x | 0xFFFFFC00;
  1727. tx1_a = (x * oldval_1) >> 8;
  1728. rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x3FF, tx1_a);
  1729. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(27),
  1730. ((x * oldval_1 >> 7) & 0x1));
  1731. y = result[final_candidate][5];
  1732. if ((y & 0x00000200) != 0)
  1733. y = y | 0xFFFFFC00;
  1734. tx1_c = (y * oldval_1) >> 8;
  1735. rtl_set_bbreg(hw, ROFDM0_XDTXAFE, 0xF0000000,
  1736. ((tx1_c & 0x3C0) >> 6));
  1737. rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x003F0000,
  1738. (tx1_c & 0x3F));
  1739. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(25),
  1740. ((y * oldval_1 >> 7) & 0x1));
  1741. if (btxonly)
  1742. return;
  1743. reg = result[final_candidate][6];
  1744. rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0x3FF, reg);
  1745. reg = result[final_candidate][7] & 0x3F;
  1746. rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0xFC00, reg);
  1747. reg = (result[final_candidate][7] >> 6) & 0xF;
  1748. /* rtl_set_bbreg(hw, 0xca0, 0xF0000000, reg); */
  1749. }
  1750. }
  1751. static bool _rtl8723be_phy_simularity_compare(struct ieee80211_hw *hw,
  1752. long result[][8], u8 c1, u8 c2)
  1753. {
  1754. u32 i, j, diff, simularity_bitmap, bound = 0;
  1755. u8 final_candidate[2] = {0xFF, 0xFF}; /* for path A and path B */
  1756. bool bresult = true; /* is2t = true*/
  1757. s32 tmp1 = 0, tmp2 = 0;
  1758. bound = 8;
  1759. simularity_bitmap = 0;
  1760. for (i = 0; i < bound; i++) {
  1761. if ((i == 1) || (i == 3) || (i == 5) || (i == 7)) {
  1762. if ((result[c1][i] & 0x00000200) != 0)
  1763. tmp1 = result[c1][i] | 0xFFFFFC00;
  1764. else
  1765. tmp1 = result[c1][i];
  1766. if ((result[c2][i] & 0x00000200) != 0)
  1767. tmp2 = result[c2][i] | 0xFFFFFC00;
  1768. else
  1769. tmp2 = result[c2][i];
  1770. } else {
  1771. tmp1 = result[c1][i];
  1772. tmp2 = result[c2][i];
  1773. }
  1774. diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
  1775. if (diff > MAX_TOLERANCE) {
  1776. if ((i == 2 || i == 6) && !simularity_bitmap) {
  1777. if (result[c1][i] + result[c1][i + 1] == 0)
  1778. final_candidate[(i / 4)] = c2;
  1779. else if (result[c2][i] + result[c2][i + 1] == 0)
  1780. final_candidate[(i / 4)] = c1;
  1781. else
  1782. simularity_bitmap |= (1 << i);
  1783. } else
  1784. simularity_bitmap |= (1 << i);
  1785. }
  1786. }
  1787. if (simularity_bitmap == 0) {
  1788. for (i = 0; i < (bound / 4); i++) {
  1789. if (final_candidate[i] != 0xFF) {
  1790. for (j = i * 4; j < (i + 1) * 4 - 2; j++)
  1791. result[3][j] =
  1792. result[final_candidate[i]][j];
  1793. bresult = false;
  1794. }
  1795. }
  1796. return bresult;
  1797. } else {
  1798. if (!(simularity_bitmap & 0x03)) { /* path A TX OK */
  1799. for (i = 0; i < 2; i++)
  1800. result[3][i] = result[c1][i];
  1801. }
  1802. if (!(simularity_bitmap & 0x0c)) { /* path A RX OK */
  1803. for (i = 2; i < 4; i++)
  1804. result[3][i] = result[c1][i];
  1805. }
  1806. if (!(simularity_bitmap & 0x30)) { /* path B TX OK */
  1807. for (i = 4; i < 6; i++)
  1808. result[3][i] = result[c1][i];
  1809. }
  1810. if (!(simularity_bitmap & 0xc0)) { /* path B RX OK */
  1811. for (i = 6; i < 8; i++)
  1812. result[3][i] = result[c1][i];
  1813. }
  1814. return false;
  1815. }
  1816. }
  1817. static void _rtl8723be_phy_iq_calibrate(struct ieee80211_hw *hw,
  1818. long result[][8], u8 t, bool is2t)
  1819. {
  1820. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1821. struct rtl_phy *rtlphy = &rtlpriv->phy;
  1822. u32 i;
  1823. u8 patha_ok, pathb_ok;
  1824. u32 adda_reg[IQK_ADDA_REG_NUM] = {
  1825. 0x85c, 0xe6c, 0xe70, 0xe74,
  1826. 0xe78, 0xe7c, 0xe80, 0xe84,
  1827. 0xe88, 0xe8c, 0xed0, 0xed4,
  1828. 0xed8, 0xedc, 0xee0, 0xeec
  1829. };
  1830. u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
  1831. 0x522, 0x550, 0x551, 0x040
  1832. };
  1833. u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
  1834. ROFDM0_TRXPATHENABLE, ROFDM0_TRMUXPAR,
  1835. RFPGA0_XCD_RFINTERFACESW, 0xb68, 0xb6c,
  1836. 0x870, 0x860,
  1837. 0x864, 0xa04
  1838. };
  1839. const u32 retrycount = 2;
  1840. u32 path_sel_bb;/* path_sel_rf */
  1841. u8 tmp_reg_c50, tmp_reg_c58;
  1842. tmp_reg_c50 = rtl_get_bbreg(hw, 0xc50, MASKBYTE0);
  1843. tmp_reg_c58 = rtl_get_bbreg(hw, 0xc58, MASKBYTE0);
  1844. if (t == 0) {
  1845. rtl8723_save_adda_registers(hw, adda_reg,
  1846. rtlphy->adda_backup, 16);
  1847. rtl8723_phy_save_mac_registers(hw, iqk_mac_reg,
  1848. rtlphy->iqk_mac_backup);
  1849. rtl8723_save_adda_registers(hw, iqk_bb_reg,
  1850. rtlphy->iqk_bb_backup,
  1851. IQK_BB_REG_NUM);
  1852. }
  1853. rtl8723_phy_path_adda_on(hw, adda_reg, true, is2t);
  1854. if (t == 0) {
  1855. rtlphy->rfpi_enable = (u8)rtl_get_bbreg(hw,
  1856. RFPGA0_XA_HSSIPARAMETER1,
  1857. BIT(8));
  1858. }
  1859. path_sel_bb = rtl_get_bbreg(hw, 0x948, MASKDWORD);
  1860. rtl8723_phy_mac_setting_calibration(hw, iqk_mac_reg,
  1861. rtlphy->iqk_mac_backup);
  1862. /*BB Setting*/
  1863. rtl_set_bbreg(hw, 0xa04, 0x0f000000, 0xf);
  1864. rtl_set_bbreg(hw, 0xc04, MASKDWORD, 0x03a05600);
  1865. rtl_set_bbreg(hw, 0xc08, MASKDWORD, 0x000800e4);
  1866. rtl_set_bbreg(hw, 0x874, MASKDWORD, 0x22204000);
  1867. /* path A TX IQK */
  1868. for (i = 0; i < retrycount; i++) {
  1869. patha_ok = _rtl8723be_phy_path_a_iqk(hw);
  1870. if (patha_ok == 0x01) {
  1871. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1872. "Path A Tx IQK Success!!\n");
  1873. result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
  1874. 0x3FF0000) >> 16;
  1875. result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
  1876. 0x3FF0000) >> 16;
  1877. break;
  1878. } else {
  1879. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1880. "Path A Tx IQK Fail!!\n");
  1881. }
  1882. }
  1883. /* path A RX IQK */
  1884. for (i = 0; i < retrycount; i++) {
  1885. patha_ok = _rtl8723be_phy_path_a_rx_iqk(hw);
  1886. if (patha_ok == 0x03) {
  1887. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1888. "Path A Rx IQK Success!!\n");
  1889. result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
  1890. 0x3FF0000) >> 16;
  1891. result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
  1892. 0x3FF0000) >> 16;
  1893. break;
  1894. }
  1895. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1896. "Path A Rx IQK Fail!!\n");
  1897. }
  1898. if (0x00 == patha_ok)
  1899. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Path A IQK Fail!!\n");
  1900. if (is2t) {
  1901. /* path B TX IQK */
  1902. for (i = 0; i < retrycount; i++) {
  1903. pathb_ok = _rtl8723be_phy_path_b_iqk(hw);
  1904. if (pathb_ok == 0x01) {
  1905. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1906. "Path B Tx IQK Success!!\n");
  1907. result[t][4] = (rtl_get_bbreg(hw, 0xe94,
  1908. MASKDWORD) &
  1909. 0x3FF0000) >> 16;
  1910. result[t][5] = (rtl_get_bbreg(hw, 0xe9c,
  1911. MASKDWORD) &
  1912. 0x3FF0000) >> 16;
  1913. break;
  1914. }
  1915. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1916. "Path B Tx IQK Fail!!\n");
  1917. }
  1918. /* path B RX IQK */
  1919. for (i = 0; i < retrycount; i++) {
  1920. pathb_ok = _rtl8723be_phy_path_b_rx_iqk(hw);
  1921. if (pathb_ok == 0x03) {
  1922. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1923. "Path B Rx IQK Success!!\n");
  1924. result[t][6] = (rtl_get_bbreg(hw, 0xea4,
  1925. MASKDWORD) &
  1926. 0x3FF0000) >> 16;
  1927. result[t][7] = (rtl_get_bbreg(hw, 0xeac,
  1928. MASKDWORD) &
  1929. 0x3FF0000) >> 16;
  1930. break;
  1931. }
  1932. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1933. "Path B Rx IQK Fail!!\n");
  1934. }
  1935. }
  1936. /* Back to BB mode, load original value */
  1937. rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0);
  1938. if (t != 0) {
  1939. rtl8723_phy_reload_adda_registers(hw, adda_reg,
  1940. rtlphy->adda_backup, 16);
  1941. rtl8723_phy_reload_mac_registers(hw, iqk_mac_reg,
  1942. rtlphy->iqk_mac_backup);
  1943. rtl8723_phy_reload_adda_registers(hw, iqk_bb_reg,
  1944. rtlphy->iqk_bb_backup,
  1945. IQK_BB_REG_NUM);
  1946. rtl_set_bbreg(hw, 0x948, MASKDWORD, path_sel_bb);
  1947. /*rtl_set_rfreg(hw, RF90_PATH_B, 0xb0, 0xfffff, path_sel_rf);*/
  1948. rtl_set_bbreg(hw, 0xc50, MASKBYTE0, 0x50);
  1949. rtl_set_bbreg(hw, 0xc50, MASKBYTE0, tmp_reg_c50);
  1950. if (is2t) {
  1951. rtl_set_bbreg(hw, 0xc58, MASKBYTE0, 0x50);
  1952. rtl_set_bbreg(hw, 0xc58, MASKBYTE0, tmp_reg_c58);
  1953. }
  1954. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x01008c00);
  1955. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x01008c00);
  1956. }
  1957. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "8723be IQK Finish!!\n");
  1958. }
  1959. static u8 _get_right_chnl_place_for_iqk(u8 chnl)
  1960. {
  1961. u8 channel_all[TARGET_CHNL_NUM_2G_5G] = {
  1962. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  1963. 13, 14, 36, 38, 40, 42, 44, 46,
  1964. 48, 50, 52, 54, 56, 58, 60, 62, 64,
  1965. 100, 102, 104, 106, 108, 110,
  1966. 112, 114, 116, 118, 120, 122,
  1967. 124, 126, 128, 130, 132, 134, 136,
  1968. 138, 140, 149, 151, 153, 155, 157,
  1969. 159, 161, 163, 165};
  1970. u8 place = chnl;
  1971. if (chnl > 14) {
  1972. for (place = 14; place < sizeof(channel_all); place++) {
  1973. if (channel_all[place] == chnl)
  1974. return place - 13;
  1975. }
  1976. }
  1977. return 0;
  1978. }
  1979. static void _rtl8723be_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
  1980. {
  1981. u8 tmpreg;
  1982. u32 rf_a_mode = 0, rf_b_mode = 0, lc_cal;
  1983. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1984. tmpreg = rtl_read_byte(rtlpriv, 0xd03);
  1985. if ((tmpreg & 0x70) != 0)
  1986. rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
  1987. else
  1988. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
  1989. if ((tmpreg & 0x70) != 0) {
  1990. rf_a_mode = rtl_get_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS);
  1991. if (is2t)
  1992. rf_b_mode = rtl_get_rfreg(hw, RF90_PATH_B, 0x00,
  1993. MASK12BITS);
  1994. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS,
  1995. (rf_a_mode & 0x8FFFF) | 0x10000);
  1996. if (is2t)
  1997. rtl_set_rfreg(hw, RF90_PATH_B, 0x00, MASK12BITS,
  1998. (rf_b_mode & 0x8FFFF) | 0x10000);
  1999. }
  2000. lc_cal = rtl_get_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS);
  2001. rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, RFREG_OFFSET_MASK, 0xdfbe0);
  2002. rtl_set_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS, 0x8c0a);
  2003. /* In order not to disturb BT music when wifi init.(1ant NIC only) */
  2004. /*mdelay(100);*/
  2005. /* In order not to disturb BT music when wifi init.(1ant NIC only) */
  2006. mdelay(50);
  2007. rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, RFREG_OFFSET_MASK, 0xdffe0);
  2008. if ((tmpreg & 0x70) != 0) {
  2009. rtl_write_byte(rtlpriv, 0xd03, tmpreg);
  2010. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS, rf_a_mode);
  2011. if (is2t)
  2012. rtl_set_rfreg(hw, RF90_PATH_B, 0x00,
  2013. MASK12BITS, rf_b_mode);
  2014. } else {
  2015. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  2016. }
  2017. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
  2018. }
  2019. static void _rtl8723be_phy_set_rfpath_switch(struct ieee80211_hw *hw,
  2020. bool bmain, bool is2t)
  2021. {
  2022. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2023. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
  2024. if (bmain) /* left antenna */
  2025. rtl_set_bbreg(hw, 0x92C, MASKDWORD, 0x1);
  2026. else
  2027. rtl_set_bbreg(hw, 0x92C, MASKDWORD, 0x2);
  2028. }
  2029. #undef IQK_ADDA_REG_NUM
  2030. #undef IQK_DELAY_TIME
  2031. /* IQK is merge from Merge Temp */
  2032. void rtl8723be_phy_iq_calibrate(struct ieee80211_hw *hw, bool b_recovery)
  2033. {
  2034. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2035. struct rtl_phy *rtlphy = &rtlpriv->phy;
  2036. long result[4][8];
  2037. u8 i, final_candidate, idx;
  2038. bool b_patha_ok, b_pathb_ok;
  2039. long reg_e94, reg_e9c, reg_ea4, reg_eac, reg_eb4, reg_ebc, reg_ec4;
  2040. long reg_ecc, reg_tmp = 0;
  2041. bool is12simular, is13simular, is23simular;
  2042. u32 iqk_bb_reg[9] = {
  2043. ROFDM0_XARXIQIMBALANCE,
  2044. ROFDM0_XBRXIQIMBALANCE,
  2045. ROFDM0_ECCATHRESHOLD,
  2046. ROFDM0_AGCRSSITABLE,
  2047. ROFDM0_XATXIQIMBALANCE,
  2048. ROFDM0_XBTXIQIMBALANCE,
  2049. ROFDM0_XCTXAFE,
  2050. ROFDM0_XDTXAFE,
  2051. ROFDM0_RXIQEXTANTA
  2052. };
  2053. u32 path_sel_bb = 0; /* path_sel_rf = 0 */
  2054. if (rtlphy->lck_inprogress)
  2055. return;
  2056. spin_lock(&rtlpriv->locks.iqk_lock);
  2057. rtlphy->lck_inprogress = true;
  2058. spin_unlock(&rtlpriv->locks.iqk_lock);
  2059. if (b_recovery) {
  2060. rtl8723_phy_reload_adda_registers(hw, iqk_bb_reg,
  2061. rtlphy->iqk_bb_backup, 9);
  2062. return;
  2063. }
  2064. /* Save RF Path */
  2065. path_sel_bb = rtl_get_bbreg(hw, 0x948, MASKDWORD);
  2066. /* path_sel_rf = rtl_get_rfreg(hw, RF90_PATH_A, 0xb0, 0xfffff); */
  2067. for (i = 0; i < 8; i++) {
  2068. result[0][i] = 0;
  2069. result[1][i] = 0;
  2070. result[2][i] = 0;
  2071. result[3][i] = 0;
  2072. }
  2073. final_candidate = 0xff;
  2074. b_patha_ok = false;
  2075. b_pathb_ok = false;
  2076. is12simular = false;
  2077. is23simular = false;
  2078. is13simular = false;
  2079. for (i = 0; i < 3; i++) {
  2080. _rtl8723be_phy_iq_calibrate(hw, result, i, true);
  2081. if (i == 1) {
  2082. is12simular = _rtl8723be_phy_simularity_compare(hw,
  2083. result,
  2084. 0, 1);
  2085. if (is12simular) {
  2086. final_candidate = 0;
  2087. break;
  2088. }
  2089. }
  2090. if (i == 2) {
  2091. is13simular = _rtl8723be_phy_simularity_compare(hw,
  2092. result,
  2093. 0, 2);
  2094. if (is13simular) {
  2095. final_candidate = 0;
  2096. break;
  2097. }
  2098. is23simular = _rtl8723be_phy_simularity_compare(hw,
  2099. result,
  2100. 1, 2);
  2101. if (is23simular) {
  2102. final_candidate = 1;
  2103. } else {
  2104. for (i = 0; i < 8; i++)
  2105. reg_tmp += result[3][i];
  2106. if (reg_tmp != 0)
  2107. final_candidate = 3;
  2108. else
  2109. final_candidate = 0xFF;
  2110. }
  2111. }
  2112. }
  2113. for (i = 0; i < 4; i++) {
  2114. reg_e94 = result[i][0];
  2115. reg_e9c = result[i][1];
  2116. reg_ea4 = result[i][2];
  2117. reg_eac = result[i][3];
  2118. reg_eb4 = result[i][4];
  2119. reg_ebc = result[i][5];
  2120. reg_ec4 = result[i][6];
  2121. reg_ecc = result[i][7];
  2122. }
  2123. if (final_candidate != 0xff) {
  2124. reg_e94 = result[final_candidate][0];
  2125. rtlphy->reg_e94 = reg_e94;
  2126. reg_e9c = result[final_candidate][1];
  2127. rtlphy->reg_e9c = reg_e9c;
  2128. reg_ea4 = result[final_candidate][2];
  2129. reg_eac = result[final_candidate][3];
  2130. reg_eb4 = result[final_candidate][4];
  2131. rtlphy->reg_eb4 = reg_eb4;
  2132. reg_ebc = result[final_candidate][5];
  2133. rtlphy->reg_ebc = reg_ebc;
  2134. reg_ec4 = result[final_candidate][6];
  2135. reg_ecc = result[final_candidate][7];
  2136. b_patha_ok = true;
  2137. b_pathb_ok = true;
  2138. } else {
  2139. rtlphy->reg_e94 = 0x100;
  2140. rtlphy->reg_eb4 = 0x100;
  2141. rtlphy->reg_e9c = 0x0;
  2142. rtlphy->reg_ebc = 0x0;
  2143. }
  2144. if (reg_e94 != 0)
  2145. rtl8723_phy_path_a_fill_iqk_matrix(hw, b_patha_ok, result,
  2146. final_candidate,
  2147. (reg_ea4 == 0));
  2148. if (reg_eb4 != 0)
  2149. _rtl8723be_phy_path_b_fill_iqk_matrix(hw, b_pathb_ok, result,
  2150. final_candidate,
  2151. (reg_ec4 == 0));
  2152. idx = _get_right_chnl_place_for_iqk(rtlphy->current_channel);
  2153. if (final_candidate < 4) {
  2154. for (i = 0; i < IQK_MATRIX_REG_NUM; i++)
  2155. rtlphy->iqk_matrix[idx].value[0][i] =
  2156. result[final_candidate][i];
  2157. rtlphy->iqk_matrix[idx].iqk_done = true;
  2158. }
  2159. rtl8723_save_adda_registers(hw, iqk_bb_reg,
  2160. rtlphy->iqk_bb_backup, 9);
  2161. rtl_set_bbreg(hw, 0x948, MASKDWORD, path_sel_bb);
  2162. /* rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, 0xfffff, path_sel_rf); */
  2163. spin_lock(&rtlpriv->locks.iqk_lock);
  2164. rtlphy->lck_inprogress = false;
  2165. spin_unlock(&rtlpriv->locks.iqk_lock);
  2166. }
  2167. void rtl8723be_phy_lc_calibrate(struct ieee80211_hw *hw)
  2168. {
  2169. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2170. struct rtl_phy *rtlphy = &rtlpriv->phy;
  2171. struct rtl_hal *rtlhal = &rtlpriv->rtlhal;
  2172. u32 timeout = 2000, timecount = 0;
  2173. while (rtlpriv->mac80211.act_scanning && timecount < timeout) {
  2174. udelay(50);
  2175. timecount += 50;
  2176. }
  2177. rtlphy->lck_inprogress = true;
  2178. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2179. "LCK:Start!!! currentband %x delay %d ms\n",
  2180. rtlhal->current_bandtype, timecount);
  2181. _rtl8723be_phy_lc_calibrate(hw, false);
  2182. rtlphy->lck_inprogress = false;
  2183. }
  2184. void rtl8723be_phy_set_rfpath_switch(struct ieee80211_hw *hw, bool bmain)
  2185. {
  2186. _rtl8723be_phy_set_rfpath_switch(hw, bmain, true);
  2187. }
  2188. bool rtl8723be_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
  2189. {
  2190. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2191. struct rtl_phy *rtlphy = &rtlpriv->phy;
  2192. bool b_postprocessing = false;
  2193. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2194. "-->IO Cmd(%#x), set_io_inprogress(%d)\n",
  2195. iotype, rtlphy->set_io_inprogress);
  2196. do {
  2197. switch (iotype) {
  2198. case IO_CMD_RESUME_DM_BY_SCAN:
  2199. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2200. "[IO CMD] Resume DM after scan.\n");
  2201. b_postprocessing = true;
  2202. break;
  2203. case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
  2204. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2205. "[IO CMD] Pause DM before scan.\n");
  2206. b_postprocessing = true;
  2207. break;
  2208. default:
  2209. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  2210. "switch case not process\n");
  2211. break;
  2212. }
  2213. } while (false);
  2214. if (b_postprocessing && !rtlphy->set_io_inprogress) {
  2215. rtlphy->set_io_inprogress = true;
  2216. rtlphy->current_io_type = iotype;
  2217. } else {
  2218. return false;
  2219. }
  2220. rtl8723be_phy_set_io(hw);
  2221. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE, "IO Type(%#x)\n", iotype);
  2222. return true;
  2223. }
  2224. static void rtl8723be_phy_set_io(struct ieee80211_hw *hw)
  2225. {
  2226. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2227. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  2228. struct rtl_phy *rtlphy = &rtlpriv->phy;
  2229. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2230. "--->Cmd(%#x), set_io_inprogress(%d)\n",
  2231. rtlphy->current_io_type, rtlphy->set_io_inprogress);
  2232. switch (rtlphy->current_io_type) {
  2233. case IO_CMD_RESUME_DM_BY_SCAN:
  2234. dm_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
  2235. /*rtl92c_dm_write_dig(hw);*/
  2236. rtl8723be_phy_set_txpower_level(hw, rtlphy->current_channel);
  2237. rtl_set_bbreg(hw, RCCK0_CCA, 0xff0000, 0x83);
  2238. break;
  2239. case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
  2240. rtlphy->initgain_backup.xaagccore1 = dm_digtable->cur_igvalue;
  2241. dm_digtable->cur_igvalue = 0x17;
  2242. rtl_set_bbreg(hw, RCCK0_CCA, 0xff0000, 0x40);
  2243. break;
  2244. default:
  2245. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  2246. "switch case not process\n");
  2247. break;
  2248. }
  2249. rtlphy->set_io_inprogress = false;
  2250. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2251. "(%#x)\n", rtlphy->current_io_type);
  2252. }
  2253. static void rtl8723be_phy_set_rf_on(struct ieee80211_hw *hw)
  2254. {
  2255. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2256. rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
  2257. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  2258. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  2259. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  2260. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  2261. }
  2262. static void _rtl8723be_phy_set_rf_sleep(struct ieee80211_hw *hw)
  2263. {
  2264. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2265. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
  2266. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
  2267. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  2268. rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
  2269. }
  2270. static bool _rtl8723be_phy_set_rf_power_state(struct ieee80211_hw *hw,
  2271. enum rf_pwrstate rfpwr_state)
  2272. {
  2273. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2274. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  2275. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  2276. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  2277. bool bresult = true;
  2278. u8 i, queue_id;
  2279. struct rtl8192_tx_ring *ring = NULL;
  2280. switch (rfpwr_state) {
  2281. case ERFON:
  2282. if ((ppsc->rfpwr_state == ERFOFF) &&
  2283. RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
  2284. bool rtstatus;
  2285. u32 initializecount = 0;
  2286. do {
  2287. initializecount++;
  2288. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  2289. "IPS Set eRf nic enable\n");
  2290. rtstatus = rtl_ps_enable_nic(hw);
  2291. } while (!rtstatus && (initializecount < 10));
  2292. RT_CLEAR_PS_LEVEL(ppsc,
  2293. RT_RF_OFF_LEVL_HALT_NIC);
  2294. } else {
  2295. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  2296. "Set ERFON sleeped:%d ms\n",
  2297. jiffies_to_msecs(jiffies -
  2298. ppsc->last_sleep_jiffies));
  2299. ppsc->last_awake_jiffies = jiffies;
  2300. rtl8723be_phy_set_rf_on(hw);
  2301. }
  2302. if (mac->link_state == MAC80211_LINKED)
  2303. rtlpriv->cfg->ops->led_control(hw, LED_CTL_LINK);
  2304. else
  2305. rtlpriv->cfg->ops->led_control(hw, LED_CTL_NO_LINK);
  2306. break;
  2307. case ERFOFF:
  2308. for (queue_id = 0, i = 0;
  2309. queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
  2310. ring = &pcipriv->dev.tx_ring[queue_id];
  2311. /* Don't check BEACON Q.
  2312. * BEACON Q is always not empty,
  2313. * because '_rtl8723be_cmd_send_packet'
  2314. */
  2315. if (queue_id == BEACON_QUEUE ||
  2316. skb_queue_len(&ring->queue) == 0) {
  2317. queue_id++;
  2318. continue;
  2319. } else {
  2320. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  2321. "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
  2322. (i + 1), queue_id,
  2323. skb_queue_len(&ring->queue));
  2324. udelay(10);
  2325. i++;
  2326. }
  2327. if (i >= MAX_DOZE_WAITING_TIMES_9x) {
  2328. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  2329. "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
  2330. MAX_DOZE_WAITING_TIMES_9x,
  2331. queue_id,
  2332. skb_queue_len(&ring->queue));
  2333. break;
  2334. }
  2335. }
  2336. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
  2337. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  2338. "IPS Set eRf nic disable\n");
  2339. rtl_ps_disable_nic(hw);
  2340. RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  2341. } else {
  2342. if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS) {
  2343. rtlpriv->cfg->ops->led_control(hw,
  2344. LED_CTL_NO_LINK);
  2345. } else {
  2346. rtlpriv->cfg->ops->led_control(hw,
  2347. LED_CTL_POWER_OFF);
  2348. }
  2349. }
  2350. break;
  2351. case ERFSLEEP:
  2352. if (ppsc->rfpwr_state == ERFOFF)
  2353. break;
  2354. for (queue_id = 0, i = 0;
  2355. queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
  2356. ring = &pcipriv->dev.tx_ring[queue_id];
  2357. if (skb_queue_len(&ring->queue) == 0) {
  2358. queue_id++;
  2359. continue;
  2360. } else {
  2361. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  2362. "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
  2363. (i + 1), queue_id,
  2364. skb_queue_len(&ring->queue));
  2365. udelay(10);
  2366. i++;
  2367. }
  2368. if (i >= MAX_DOZE_WAITING_TIMES_9x) {
  2369. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  2370. "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
  2371. MAX_DOZE_WAITING_TIMES_9x,
  2372. queue_id,
  2373. skb_queue_len(&ring->queue));
  2374. break;
  2375. }
  2376. }
  2377. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  2378. "Set ERFSLEEP awaked:%d ms\n",
  2379. jiffies_to_msecs(jiffies -
  2380. ppsc->last_awake_jiffies));
  2381. ppsc->last_sleep_jiffies = jiffies;
  2382. _rtl8723be_phy_set_rf_sleep(hw);
  2383. break;
  2384. default:
  2385. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  2386. "switch case not process\n");
  2387. bresult = false;
  2388. break;
  2389. }
  2390. if (bresult)
  2391. ppsc->rfpwr_state = rfpwr_state;
  2392. return bresult;
  2393. }
  2394. bool rtl8723be_phy_set_rf_power_state(struct ieee80211_hw *hw,
  2395. enum rf_pwrstate rfpwr_state)
  2396. {
  2397. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  2398. bool bresult = false;
  2399. if (rfpwr_state == ppsc->rfpwr_state)
  2400. return bresult;
  2401. bresult = _rtl8723be_phy_set_rf_power_state(hw, rfpwr_state);
  2402. return bresult;
  2403. }