dm_common.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. #include <linux/export.h>
  26. #include "dm_common.h"
  27. #include "phy_common.h"
  28. #include "../pci.h"
  29. #include "../base.h"
  30. #include "../core.h"
  31. #define BT_RSSI_STATE_NORMAL_POWER BIT_OFFSET_LEN_MASK_32(0, 1)
  32. #define BT_RSSI_STATE_AMDPU_OFF BIT_OFFSET_LEN_MASK_32(1, 1)
  33. #define BT_RSSI_STATE_SPECIAL_LOW BIT_OFFSET_LEN_MASK_32(2, 1)
  34. #define BT_RSSI_STATE_BG_EDCA_LOW BIT_OFFSET_LEN_MASK_32(3, 1)
  35. #define BT_RSSI_STATE_TXPOWER_LOW BIT_OFFSET_LEN_MASK_32(4, 1)
  36. #define BT_MASK 0x00ffffff
  37. #define RTLPRIV (struct rtl_priv *)
  38. #define GET_UNDECORATED_AVERAGE_RSSI(_priv) \
  39. ((RTLPRIV(_priv))->mac80211.opmode == \
  40. NL80211_IFTYPE_ADHOC) ? \
  41. ((RTLPRIV(_priv))->dm.entry_min_undec_sm_pwdb) : \
  42. ((RTLPRIV(_priv))->dm.undec_sm_pwdb)
  43. static const u32 ofdmswing_table[OFDM_TABLE_SIZE] = {
  44. 0x7f8001fe,
  45. 0x788001e2,
  46. 0x71c001c7,
  47. 0x6b8001ae,
  48. 0x65400195,
  49. 0x5fc0017f,
  50. 0x5a400169,
  51. 0x55400155,
  52. 0x50800142,
  53. 0x4c000130,
  54. 0x47c0011f,
  55. 0x43c0010f,
  56. 0x40000100,
  57. 0x3c8000f2,
  58. 0x390000e4,
  59. 0x35c000d7,
  60. 0x32c000cb,
  61. 0x300000c0,
  62. 0x2d4000b5,
  63. 0x2ac000ab,
  64. 0x288000a2,
  65. 0x26000098,
  66. 0x24000090,
  67. 0x22000088,
  68. 0x20000080,
  69. 0x1e400079,
  70. 0x1c800072,
  71. 0x1b00006c,
  72. 0x19800066,
  73. 0x18000060,
  74. 0x16c0005b,
  75. 0x15800056,
  76. 0x14400051,
  77. 0x1300004c,
  78. 0x12000048,
  79. 0x11000044,
  80. 0x10000040,
  81. };
  82. static const u8 cckswing_table_ch1ch13[CCK_TABLE_SIZE][8] = {
  83. {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04},
  84. {0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04},
  85. {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03},
  86. {0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03},
  87. {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03},
  88. {0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03},
  89. {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03},
  90. {0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03},
  91. {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02},
  92. {0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02},
  93. {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02},
  94. {0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02},
  95. {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02},
  96. {0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02},
  97. {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02},
  98. {0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02},
  99. {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01},
  100. {0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02},
  101. {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01},
  102. {0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
  103. {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
  104. {0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01},
  105. {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01},
  106. {0x0e, 0x0e, 0x0c, 0x0a, 0x08, 0x05, 0x02, 0x01},
  107. {0x0d, 0x0d, 0x0c, 0x0a, 0x07, 0x05, 0x02, 0x01},
  108. {0x0d, 0x0c, 0x0b, 0x09, 0x07, 0x04, 0x02, 0x01},
  109. {0x0c, 0x0c, 0x0a, 0x09, 0x06, 0x04, 0x02, 0x01},
  110. {0x0b, 0x0b, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x01},
  111. {0x0b, 0x0a, 0x09, 0x08, 0x06, 0x04, 0x02, 0x01},
  112. {0x0a, 0x0a, 0x09, 0x07, 0x05, 0x03, 0x02, 0x01},
  113. {0x0a, 0x09, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01},
  114. {0x09, 0x09, 0x08, 0x06, 0x05, 0x03, 0x01, 0x01},
  115. {0x09, 0x08, 0x07, 0x06, 0x04, 0x03, 0x01, 0x01}
  116. };
  117. static const u8 cckswing_table_ch14[CCK_TABLE_SIZE][8] = {
  118. {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00},
  119. {0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00},
  120. {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00},
  121. {0x2d, 0x2d, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00},
  122. {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00},
  123. {0x28, 0x28, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00},
  124. {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00},
  125. {0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00},
  126. {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00},
  127. {0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00},
  128. {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00},
  129. {0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00},
  130. {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00},
  131. {0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00},
  132. {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00},
  133. {0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00},
  134. {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00},
  135. {0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00},
  136. {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00},
  137. {0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
  138. {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
  139. {0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00},
  140. {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00},
  141. {0x0e, 0x0e, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00},
  142. {0x0d, 0x0d, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00},
  143. {0x0d, 0x0c, 0x0b, 0x06, 0x00, 0x00, 0x00, 0x00},
  144. {0x0c, 0x0c, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00},
  145. {0x0b, 0x0b, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00},
  146. {0x0b, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00},
  147. {0x0a, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00},
  148. {0x0a, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00},
  149. {0x09, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00},
  150. {0x09, 0x08, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00}
  151. };
  152. static u32 power_index_reg[6] = {0xc90, 0xc91, 0xc92, 0xc98, 0xc99, 0xc9a};
  153. void dm_restorepowerindex(struct ieee80211_hw *hw)
  154. {
  155. struct rtl_priv *rtlpriv = rtl_priv(hw);
  156. u8 index;
  157. for (index = 0; index < 6; index++)
  158. rtl_write_byte(rtlpriv, power_index_reg[index],
  159. rtlpriv->dm.powerindex_backup[index]);
  160. }
  161. EXPORT_SYMBOL_GPL(dm_restorepowerindex);
  162. void dm_writepowerindex(struct ieee80211_hw *hw, u8 value)
  163. {
  164. struct rtl_priv *rtlpriv = rtl_priv(hw);
  165. u8 index;
  166. for (index = 0; index < 6; index++)
  167. rtl_write_byte(rtlpriv, power_index_reg[index], value);
  168. }
  169. EXPORT_SYMBOL_GPL(dm_writepowerindex);
  170. void dm_savepowerindex(struct ieee80211_hw *hw)
  171. {
  172. struct rtl_priv *rtlpriv = rtl_priv(hw);
  173. u8 index;
  174. u8 tmp;
  175. for (index = 0; index < 6; index++) {
  176. tmp = rtl_read_byte(rtlpriv, power_index_reg[index]);
  177. rtlpriv->dm.powerindex_backup[index] = tmp;
  178. }
  179. }
  180. EXPORT_SYMBOL_GPL(dm_savepowerindex);
  181. static u8 rtl92c_dm_initial_gain_min_pwdb(struct ieee80211_hw *hw)
  182. {
  183. struct rtl_priv *rtlpriv = rtl_priv(hw);
  184. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  185. long rssi_val_min = 0;
  186. if ((dm_digtable->curmultista_cstate == DIG_MULTISTA_CONNECT) &&
  187. (dm_digtable->cursta_cstate == DIG_STA_CONNECT)) {
  188. if (rtlpriv->dm.entry_min_undec_sm_pwdb != 0)
  189. rssi_val_min =
  190. (rtlpriv->dm.entry_min_undec_sm_pwdb >
  191. rtlpriv->dm.undec_sm_pwdb) ?
  192. rtlpriv->dm.undec_sm_pwdb :
  193. rtlpriv->dm.entry_min_undec_sm_pwdb;
  194. else
  195. rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  196. } else if (dm_digtable->cursta_cstate == DIG_STA_CONNECT ||
  197. dm_digtable->cursta_cstate == DIG_STA_BEFORE_CONNECT) {
  198. rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  199. } else if (dm_digtable->curmultista_cstate == DIG_MULTISTA_CONNECT) {
  200. rssi_val_min = rtlpriv->dm.entry_min_undec_sm_pwdb;
  201. }
  202. if (rssi_val_min > 100)
  203. rssi_val_min = 100;
  204. return (u8)rssi_val_min;
  205. }
  206. static void rtl92c_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
  207. {
  208. u32 ret_value;
  209. struct rtl_priv *rtlpriv = rtl_priv(hw);
  210. struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
  211. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
  212. falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
  213. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
  214. falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
  215. falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
  216. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
  217. falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
  218. ret_value = rtl_get_bbreg(hw, ROFDM0_FRAMESYNC, MASKDWORD);
  219. falsealm_cnt->cnt_fast_fsync_fail = (ret_value & 0xffff);
  220. falsealm_cnt->cnt_sb_search_fail = ((ret_value & 0xffff0000) >> 16);
  221. falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
  222. falsealm_cnt->cnt_rate_illegal +
  223. falsealm_cnt->cnt_crc8_fail +
  224. falsealm_cnt->cnt_mcs_fail +
  225. falsealm_cnt->cnt_fast_fsync_fail +
  226. falsealm_cnt->cnt_sb_search_fail;
  227. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(14), 1);
  228. ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERLOWER, MASKBYTE0);
  229. falsealm_cnt->cnt_cck_fail = ret_value;
  230. ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERUPPER, MASKBYTE3);
  231. falsealm_cnt->cnt_cck_fail += (ret_value & 0xff) << 8;
  232. falsealm_cnt->cnt_all = (falsealm_cnt->cnt_parity_fail +
  233. falsealm_cnt->cnt_rate_illegal +
  234. falsealm_cnt->cnt_crc8_fail +
  235. falsealm_cnt->cnt_mcs_fail +
  236. falsealm_cnt->cnt_cck_fail);
  237. rtl_set_bbreg(hw, ROFDM1_LSTF, 0x08000000, 1);
  238. rtl_set_bbreg(hw, ROFDM1_LSTF, 0x08000000, 0);
  239. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, 0x0000c000, 0);
  240. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, 0x0000c000, 2);
  241. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  242. "cnt_parity_fail = %d, cnt_rate_illegal = %d, cnt_crc8_fail = %d, cnt_mcs_fail = %d\n",
  243. falsealm_cnt->cnt_parity_fail,
  244. falsealm_cnt->cnt_rate_illegal,
  245. falsealm_cnt->cnt_crc8_fail, falsealm_cnt->cnt_mcs_fail);
  246. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  247. "cnt_ofdm_fail = %x, cnt_cck_fail = %x, cnt_all = %x\n",
  248. falsealm_cnt->cnt_ofdm_fail,
  249. falsealm_cnt->cnt_cck_fail, falsealm_cnt->cnt_all);
  250. }
  251. static void rtl92c_dm_ctrl_initgain_by_fa(struct ieee80211_hw *hw)
  252. {
  253. struct rtl_priv *rtlpriv = rtl_priv(hw);
  254. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  255. u8 value_igi = dm_digtable->cur_igvalue;
  256. if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH0)
  257. value_igi--;
  258. else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH1)
  259. value_igi += 0;
  260. else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH2)
  261. value_igi++;
  262. else if (rtlpriv->falsealm_cnt.cnt_all >= DM_DIG_FA_TH2)
  263. value_igi += 2;
  264. if (value_igi > DM_DIG_FA_UPPER)
  265. value_igi = DM_DIG_FA_UPPER;
  266. else if (value_igi < DM_DIG_FA_LOWER)
  267. value_igi = DM_DIG_FA_LOWER;
  268. if (rtlpriv->falsealm_cnt.cnt_all > 10000)
  269. value_igi = DM_DIG_FA_UPPER;
  270. dm_digtable->cur_igvalue = value_igi;
  271. rtl92c_dm_write_dig(hw);
  272. }
  273. static void rtl92c_dm_ctrl_initgain_by_rssi(struct ieee80211_hw *hw)
  274. {
  275. struct rtl_priv *rtlpriv = rtl_priv(hw);
  276. struct dig_t *digtable = &rtlpriv->dm_digtable;
  277. u32 isbt;
  278. /* modify DIG lower bound, deal with abnormally large false alarm */
  279. if (rtlpriv->falsealm_cnt.cnt_all > 10000) {
  280. digtable->large_fa_hit++;
  281. if (digtable->forbidden_igi < digtable->cur_igvalue) {
  282. digtable->forbidden_igi = digtable->cur_igvalue;
  283. digtable->large_fa_hit = 1;
  284. }
  285. if (digtable->large_fa_hit >= 3) {
  286. if ((digtable->forbidden_igi + 1) >
  287. digtable->rx_gain_max)
  288. digtable->rx_gain_min = digtable->rx_gain_max;
  289. else
  290. digtable->rx_gain_min = (digtable->forbidden_igi + 1);
  291. digtable->recover_cnt = 3600; /* 3600=2hr */
  292. }
  293. } else {
  294. /* Recovery mechanism for IGI lower bound */
  295. if (digtable->recover_cnt != 0) {
  296. digtable->recover_cnt--;
  297. } else {
  298. if (digtable->large_fa_hit == 0) {
  299. if ((digtable->forbidden_igi-1) < DM_DIG_MIN) {
  300. digtable->forbidden_igi = DM_DIG_MIN;
  301. digtable->rx_gain_min = DM_DIG_MIN;
  302. } else {
  303. digtable->forbidden_igi--;
  304. digtable->rx_gain_min = digtable->forbidden_igi + 1;
  305. }
  306. } else if (digtable->large_fa_hit == 3) {
  307. digtable->large_fa_hit = 0;
  308. }
  309. }
  310. }
  311. if (rtlpriv->falsealm_cnt.cnt_all < 250) {
  312. isbt = rtl_read_byte(rtlpriv, 0x4fd) & 0x01;
  313. if (!isbt) {
  314. if (rtlpriv->falsealm_cnt.cnt_all >
  315. digtable->fa_lowthresh) {
  316. if ((digtable->back_val - 2) <
  317. digtable->back_range_min)
  318. digtable->back_val = digtable->back_range_min;
  319. else
  320. digtable->back_val -= 2;
  321. } else if (rtlpriv->falsealm_cnt.cnt_all <
  322. digtable->fa_lowthresh) {
  323. if ((digtable->back_val + 2) >
  324. digtable->back_range_max)
  325. digtable->back_val = digtable->back_range_max;
  326. else
  327. digtable->back_val += 2;
  328. }
  329. } else {
  330. digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  331. }
  332. } else {
  333. /* Adjust initial gain by false alarm */
  334. if (rtlpriv->falsealm_cnt.cnt_all > 1000)
  335. digtable->cur_igvalue = digtable->pre_igvalue + 2;
  336. else if (rtlpriv->falsealm_cnt.cnt_all > 750)
  337. digtable->cur_igvalue = digtable->pre_igvalue + 1;
  338. else if (rtlpriv->falsealm_cnt.cnt_all < 500)
  339. digtable->cur_igvalue = digtable->pre_igvalue - 1;
  340. }
  341. /* Check initial gain by upper/lower bound */
  342. if (digtable->cur_igvalue > digtable->rx_gain_max)
  343. digtable->cur_igvalue = digtable->rx_gain_max;
  344. if (digtable->cur_igvalue < digtable->rx_gain_min)
  345. digtable->cur_igvalue = digtable->rx_gain_min;
  346. rtl92c_dm_write_dig(hw);
  347. }
  348. static void rtl92c_dm_initial_gain_multi_sta(struct ieee80211_hw *hw)
  349. {
  350. static u8 initialized; /* initialized to false */
  351. struct rtl_priv *rtlpriv = rtl_priv(hw);
  352. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  353. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  354. long rssi_strength = rtlpriv->dm.entry_min_undec_sm_pwdb;
  355. bool multi_sta = false;
  356. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  357. multi_sta = true;
  358. if (!multi_sta ||
  359. dm_digtable->cursta_cstate == DIG_STA_DISCONNECT) {
  360. initialized = false;
  361. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  362. return;
  363. } else if (initialized == false) {
  364. initialized = true;
  365. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_0;
  366. dm_digtable->cur_igvalue = 0x20;
  367. rtl92c_dm_write_dig(hw);
  368. }
  369. if (dm_digtable->curmultista_cstate == DIG_MULTISTA_CONNECT) {
  370. if ((rssi_strength < dm_digtable->rssi_lowthresh) &&
  371. (dm_digtable->dig_ext_port_stage != DIG_EXT_PORT_STAGE_1)) {
  372. if (dm_digtable->dig_ext_port_stage ==
  373. DIG_EXT_PORT_STAGE_2) {
  374. dm_digtable->cur_igvalue = 0x20;
  375. rtl92c_dm_write_dig(hw);
  376. }
  377. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_1;
  378. } else if (rssi_strength > dm_digtable->rssi_highthresh) {
  379. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_2;
  380. rtl92c_dm_ctrl_initgain_by_fa(hw);
  381. }
  382. } else if (dm_digtable->dig_ext_port_stage != DIG_EXT_PORT_STAGE_0) {
  383. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_0;
  384. dm_digtable->cur_igvalue = 0x20;
  385. rtl92c_dm_write_dig(hw);
  386. }
  387. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  388. "curmultista_cstate = %x dig_ext_port_stage %x\n",
  389. dm_digtable->curmultista_cstate,
  390. dm_digtable->dig_ext_port_stage);
  391. }
  392. static void rtl92c_dm_initial_gain_sta(struct ieee80211_hw *hw)
  393. {
  394. struct rtl_priv *rtlpriv = rtl_priv(hw);
  395. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  396. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  397. "presta_cstate = %x, cursta_cstate = %x\n",
  398. dm_digtable->presta_cstate, dm_digtable->cursta_cstate);
  399. if (dm_digtable->presta_cstate == dm_digtable->cursta_cstate ||
  400. dm_digtable->cursta_cstate == DIG_STA_BEFORE_CONNECT ||
  401. dm_digtable->cursta_cstate == DIG_STA_CONNECT) {
  402. if (dm_digtable->cursta_cstate != DIG_STA_DISCONNECT) {
  403. dm_digtable->rssi_val_min =
  404. rtl92c_dm_initial_gain_min_pwdb(hw);
  405. if (dm_digtable->rssi_val_min > 100)
  406. dm_digtable->rssi_val_min = 100;
  407. rtl92c_dm_ctrl_initgain_by_rssi(hw);
  408. }
  409. } else {
  410. dm_digtable->rssi_val_min = 0;
  411. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  412. dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  413. dm_digtable->cur_igvalue = 0x20;
  414. dm_digtable->pre_igvalue = 0;
  415. rtl92c_dm_write_dig(hw);
  416. }
  417. }
  418. static void rtl92c_dm_cck_packet_detection_thresh(struct ieee80211_hw *hw)
  419. {
  420. struct rtl_priv *rtlpriv = rtl_priv(hw);
  421. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  422. if (dm_digtable->cursta_cstate == DIG_STA_CONNECT) {
  423. dm_digtable->rssi_val_min = rtl92c_dm_initial_gain_min_pwdb(hw);
  424. if (dm_digtable->rssi_val_min > 100)
  425. dm_digtable->rssi_val_min = 100;
  426. if (dm_digtable->pre_cck_pd_state == CCK_PD_STAGE_LOWRSSI) {
  427. if (dm_digtable->rssi_val_min <= 25)
  428. dm_digtable->cur_cck_pd_state =
  429. CCK_PD_STAGE_LOWRSSI;
  430. else
  431. dm_digtable->cur_cck_pd_state =
  432. CCK_PD_STAGE_HIGHRSSI;
  433. } else {
  434. if (dm_digtable->rssi_val_min <= 20)
  435. dm_digtable->cur_cck_pd_state =
  436. CCK_PD_STAGE_LOWRSSI;
  437. else
  438. dm_digtable->cur_cck_pd_state =
  439. CCK_PD_STAGE_HIGHRSSI;
  440. }
  441. } else {
  442. dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_MAX;
  443. }
  444. if (dm_digtable->pre_cck_pd_state != dm_digtable->cur_cck_pd_state) {
  445. if ((dm_digtable->cur_cck_pd_state == CCK_PD_STAGE_LOWRSSI) ||
  446. (dm_digtable->cur_cck_pd_state == CCK_PD_STAGE_MAX))
  447. rtl_set_bbreg(hw, RCCK0_CCA, MASKBYTE2, 0x83);
  448. else
  449. rtl_set_bbreg(hw, RCCK0_CCA, MASKBYTE2, 0xcd);
  450. dm_digtable->pre_cck_pd_state = dm_digtable->cur_cck_pd_state;
  451. }
  452. }
  453. static void rtl92c_dm_ctrl_initgain_by_twoport(struct ieee80211_hw *hw)
  454. {
  455. struct rtl_priv *rtlpriv = rtl_priv(hw);
  456. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  457. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  458. if (mac->act_scanning)
  459. return;
  460. if (mac->link_state >= MAC80211_LINKED)
  461. dm_digtable->cursta_cstate = DIG_STA_CONNECT;
  462. else
  463. dm_digtable->cursta_cstate = DIG_STA_DISCONNECT;
  464. dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
  465. rtl92c_dm_initial_gain_sta(hw);
  466. rtl92c_dm_initial_gain_multi_sta(hw);
  467. rtl92c_dm_cck_packet_detection_thresh(hw);
  468. dm_digtable->presta_cstate = dm_digtable->cursta_cstate;
  469. }
  470. static void rtl92c_dm_dig(struct ieee80211_hw *hw)
  471. {
  472. struct rtl_priv *rtlpriv = rtl_priv(hw);
  473. if (rtlpriv->dm.dm_initialgain_enable == false)
  474. return;
  475. if (!(rtlpriv->dm.dm_flag & DYNAMIC_FUNC_DIG))
  476. return;
  477. rtl92c_dm_ctrl_initgain_by_twoport(hw);
  478. }
  479. static void rtl92c_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
  480. {
  481. struct rtl_priv *rtlpriv = rtl_priv(hw);
  482. if (rtlpriv->rtlhal.interface == INTF_USB &&
  483. rtlpriv->rtlhal.board_type & 0x1) {
  484. dm_savepowerindex(hw);
  485. rtlpriv->dm.dynamic_txpower_enable = true;
  486. } else {
  487. rtlpriv->dm.dynamic_txpower_enable = false;
  488. }
  489. rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
  490. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  491. }
  492. void rtl92c_dm_write_dig(struct ieee80211_hw *hw)
  493. {
  494. struct rtl_priv *rtlpriv = rtl_priv(hw);
  495. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  496. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  497. "cur_igvalue = 0x%x, pre_igvalue = 0x%x, back_val = %d\n",
  498. dm_digtable->cur_igvalue, dm_digtable->pre_igvalue,
  499. dm_digtable->back_val);
  500. if (rtlpriv->rtlhal.interface == INTF_USB &&
  501. !dm_digtable->dig_enable_flag) {
  502. dm_digtable->pre_igvalue = 0x17;
  503. return;
  504. }
  505. dm_digtable->cur_igvalue -= 1;
  506. if (dm_digtable->cur_igvalue < DM_DIG_MIN)
  507. dm_digtable->cur_igvalue = DM_DIG_MIN;
  508. if (dm_digtable->pre_igvalue != dm_digtable->cur_igvalue) {
  509. rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f,
  510. dm_digtable->cur_igvalue);
  511. rtl_set_bbreg(hw, ROFDM0_XBAGCCORE1, 0x7f,
  512. dm_digtable->cur_igvalue);
  513. dm_digtable->pre_igvalue = dm_digtable->cur_igvalue;
  514. }
  515. RT_TRACE(rtlpriv, COMP_DIG, DBG_WARNING,
  516. "dig values 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  517. dm_digtable->cur_igvalue, dm_digtable->pre_igvalue,
  518. dm_digtable->rssi_val_min, dm_digtable->back_val,
  519. dm_digtable->rx_gain_max, dm_digtable->rx_gain_min,
  520. dm_digtable->large_fa_hit, dm_digtable->forbidden_igi);
  521. }
  522. EXPORT_SYMBOL(rtl92c_dm_write_dig);
  523. static void rtl92c_dm_pwdb_monitor(struct ieee80211_hw *hw)
  524. {
  525. struct rtl_priv *rtlpriv = rtl_priv(hw);
  526. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  527. long tmpentry_max_pwdb = 0, tmpentry_min_pwdb = 0xff;
  528. if (mac->link_state != MAC80211_LINKED)
  529. return;
  530. if (mac->opmode == NL80211_IFTYPE_ADHOC ||
  531. mac->opmode == NL80211_IFTYPE_AP) {
  532. /* TODO: Handle ADHOC and AP Mode */
  533. }
  534. if (tmpentry_max_pwdb != 0)
  535. rtlpriv->dm.entry_max_undec_sm_pwdb = tmpentry_max_pwdb;
  536. else
  537. rtlpriv->dm.entry_max_undec_sm_pwdb = 0;
  538. if (tmpentry_min_pwdb != 0xff)
  539. rtlpriv->dm.entry_min_undec_sm_pwdb = tmpentry_min_pwdb;
  540. else
  541. rtlpriv->dm.entry_min_undec_sm_pwdb = 0;
  542. /* TODO:
  543. * if (mac->opmode == NL80211_IFTYPE_STATION) {
  544. * if (rtlpriv->rtlhal.fw_ready) {
  545. * u32 param = (u32)(rtlpriv->dm.undec_sm_pwdb << 16);
  546. * rtl8192c_set_rssi_cmd(hw, param);
  547. * }
  548. * }
  549. */
  550. }
  551. void rtl92c_dm_init_edca_turbo(struct ieee80211_hw *hw)
  552. {
  553. struct rtl_priv *rtlpriv = rtl_priv(hw);
  554. rtlpriv->dm.current_turbo_edca = false;
  555. rtlpriv->dm.is_any_nonbepkts = false;
  556. rtlpriv->dm.is_cur_rdlstate = false;
  557. }
  558. EXPORT_SYMBOL(rtl92c_dm_init_edca_turbo);
  559. static void rtl92c_dm_check_edca_turbo(struct ieee80211_hw *hw)
  560. {
  561. struct rtl_priv *rtlpriv = rtl_priv(hw);
  562. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  563. static u64 last_txok_cnt;
  564. static u64 last_rxok_cnt;
  565. static u32 last_bt_edca_ul;
  566. static u32 last_bt_edca_dl;
  567. u64 cur_txok_cnt = 0;
  568. u64 cur_rxok_cnt = 0;
  569. u32 edca_be_ul = 0x5ea42b;
  570. u32 edca_be_dl = 0x5ea42b;
  571. bool bt_change_edca = false;
  572. if ((last_bt_edca_ul != rtlpriv->btcoexist.bt_edca_ul) ||
  573. (last_bt_edca_dl != rtlpriv->btcoexist.bt_edca_dl)) {
  574. rtlpriv->dm.current_turbo_edca = false;
  575. last_bt_edca_ul = rtlpriv->btcoexist.bt_edca_ul;
  576. last_bt_edca_dl = rtlpriv->btcoexist.bt_edca_dl;
  577. }
  578. if (rtlpriv->btcoexist.bt_edca_ul != 0) {
  579. edca_be_ul = rtlpriv->btcoexist.bt_edca_ul;
  580. bt_change_edca = true;
  581. }
  582. if (rtlpriv->btcoexist.bt_edca_dl != 0) {
  583. edca_be_ul = rtlpriv->btcoexist.bt_edca_dl;
  584. bt_change_edca = true;
  585. }
  586. if (mac->link_state != MAC80211_LINKED) {
  587. rtlpriv->dm.current_turbo_edca = false;
  588. return;
  589. }
  590. if ((!mac->ht_enable) && (!rtlpriv->btcoexist.bt_coexistence)) {
  591. if (!(edca_be_ul & 0xffff0000))
  592. edca_be_ul |= 0x005e0000;
  593. if (!(edca_be_dl & 0xffff0000))
  594. edca_be_dl |= 0x005e0000;
  595. }
  596. if ((bt_change_edca) || ((!rtlpriv->dm.is_any_nonbepkts) &&
  597. (!rtlpriv->dm.disable_framebursting))) {
  598. cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
  599. cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
  600. if (cur_rxok_cnt > 4 * cur_txok_cnt) {
  601. if (!rtlpriv->dm.is_cur_rdlstate ||
  602. !rtlpriv->dm.current_turbo_edca) {
  603. rtl_write_dword(rtlpriv,
  604. REG_EDCA_BE_PARAM,
  605. edca_be_dl);
  606. rtlpriv->dm.is_cur_rdlstate = true;
  607. }
  608. } else {
  609. if (rtlpriv->dm.is_cur_rdlstate ||
  610. !rtlpriv->dm.current_turbo_edca) {
  611. rtl_write_dword(rtlpriv,
  612. REG_EDCA_BE_PARAM,
  613. edca_be_ul);
  614. rtlpriv->dm.is_cur_rdlstate = false;
  615. }
  616. }
  617. rtlpriv->dm.current_turbo_edca = true;
  618. } else {
  619. if (rtlpriv->dm.current_turbo_edca) {
  620. u8 tmp = AC0_BE;
  621. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM,
  622. &tmp);
  623. rtlpriv->dm.current_turbo_edca = false;
  624. }
  625. }
  626. rtlpriv->dm.is_any_nonbepkts = false;
  627. last_txok_cnt = rtlpriv->stats.txbytesunicast;
  628. last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
  629. }
  630. static void rtl92c_dm_txpower_tracking_callback_thermalmeter(struct ieee80211_hw
  631. *hw)
  632. {
  633. struct rtl_priv *rtlpriv = rtl_priv(hw);
  634. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  635. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  636. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  637. u8 thermalvalue, delta, delta_lck, delta_iqk;
  638. long ele_a, ele_d, temp_cck, val_x, value32;
  639. long val_y, ele_c = 0;
  640. u8 ofdm_index[2], ofdm_index_old[2] = {0, 0}, cck_index_old = 0;
  641. s8 cck_index = 0;
  642. int i;
  643. bool is2t = IS_92C_SERIAL(rtlhal->version);
  644. s8 txpwr_level[3] = {0, 0, 0};
  645. u8 ofdm_min_index = 6, rf;
  646. rtlpriv->dm.txpower_trackinginit = true;
  647. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  648. "rtl92c_dm_txpower_tracking_callback_thermalmeter\n");
  649. thermalvalue = (u8) rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER, 0x1f);
  650. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  651. "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x\n",
  652. thermalvalue, rtlpriv->dm.thermalvalue,
  653. rtlefuse->eeprom_thermalmeter);
  654. rtl92c_phy_ap_calibrate(hw, (thermalvalue -
  655. rtlefuse->eeprom_thermalmeter));
  656. if (is2t)
  657. rf = 2;
  658. else
  659. rf = 1;
  660. if (thermalvalue) {
  661. ele_d = rtl_get_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  662. MASKDWORD) & MASKOFDM_D;
  663. for (i = 0; i < OFDM_TABLE_LENGTH; i++) {
  664. if (ele_d == (ofdmswing_table[i] & MASKOFDM_D)) {
  665. ofdm_index_old[0] = (u8) i;
  666. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  667. "Initial pathA ele_d reg0x%x = 0x%lx, ofdm_index=0x%x\n",
  668. ROFDM0_XATXIQIMBALANCE,
  669. ele_d, ofdm_index_old[0]);
  670. break;
  671. }
  672. }
  673. if (is2t) {
  674. ele_d = rtl_get_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
  675. MASKDWORD) & MASKOFDM_D;
  676. for (i = 0; i < OFDM_TABLE_LENGTH; i++) {
  677. if (ele_d == (ofdmswing_table[i] &
  678. MASKOFDM_D)) {
  679. ofdm_index_old[1] = (u8) i;
  680. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  681. DBG_LOUD,
  682. "Initial pathB ele_d reg0x%x = 0x%lx, ofdm_index=0x%x\n",
  683. ROFDM0_XBTXIQIMBALANCE, ele_d,
  684. ofdm_index_old[1]);
  685. break;
  686. }
  687. }
  688. }
  689. temp_cck =
  690. rtl_get_bbreg(hw, RCCK0_TXFILTER2, MASKDWORD) & MASKCCK;
  691. for (i = 0; i < CCK_TABLE_LENGTH; i++) {
  692. if (rtlpriv->dm.cck_inch14) {
  693. if (memcmp((void *)&temp_cck,
  694. (void *)&cckswing_table_ch14[i][2],
  695. 4) == 0) {
  696. cck_index_old = (u8) i;
  697. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  698. DBG_LOUD,
  699. "Initial reg0x%x = 0x%lx, cck_index=0x%x, ch 14 %d\n",
  700. RCCK0_TXFILTER2, temp_cck,
  701. cck_index_old,
  702. rtlpriv->dm.cck_inch14);
  703. break;
  704. }
  705. } else {
  706. if (memcmp((void *)&temp_cck,
  707. (void *)
  708. &cckswing_table_ch1ch13[i][2],
  709. 4) == 0) {
  710. cck_index_old = (u8) i;
  711. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  712. DBG_LOUD,
  713. "Initial reg0x%x = 0x%lx, cck_index=0x%x, ch14 %d\n",
  714. RCCK0_TXFILTER2, temp_cck,
  715. cck_index_old,
  716. rtlpriv->dm.cck_inch14);
  717. break;
  718. }
  719. }
  720. }
  721. if (!rtlpriv->dm.thermalvalue) {
  722. rtlpriv->dm.thermalvalue =
  723. rtlefuse->eeprom_thermalmeter;
  724. rtlpriv->dm.thermalvalue_lck = thermalvalue;
  725. rtlpriv->dm.thermalvalue_iqk = thermalvalue;
  726. for (i = 0; i < rf; i++)
  727. rtlpriv->dm.ofdm_index[i] = ofdm_index_old[i];
  728. rtlpriv->dm.cck_index = cck_index_old;
  729. }
  730. /* Handle USB High PA boards */
  731. delta = (thermalvalue > rtlpriv->dm.thermalvalue) ?
  732. (thermalvalue - rtlpriv->dm.thermalvalue) :
  733. (rtlpriv->dm.thermalvalue - thermalvalue);
  734. delta_lck = (thermalvalue > rtlpriv->dm.thermalvalue_lck) ?
  735. (thermalvalue - rtlpriv->dm.thermalvalue_lck) :
  736. (rtlpriv->dm.thermalvalue_lck - thermalvalue);
  737. delta_iqk = (thermalvalue > rtlpriv->dm.thermalvalue_iqk) ?
  738. (thermalvalue - rtlpriv->dm.thermalvalue_iqk) :
  739. (rtlpriv->dm.thermalvalue_iqk - thermalvalue);
  740. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  741. "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x delta 0x%x delta_lck 0x%x delta_iqk 0x%x\n",
  742. thermalvalue, rtlpriv->dm.thermalvalue,
  743. rtlefuse->eeprom_thermalmeter, delta, delta_lck,
  744. delta_iqk);
  745. if (delta_lck > 1) {
  746. rtlpriv->dm.thermalvalue_lck = thermalvalue;
  747. rtl92c_phy_lc_calibrate(hw);
  748. }
  749. if (delta > 0 && rtlpriv->dm.txpower_track_control) {
  750. if (thermalvalue > rtlpriv->dm.thermalvalue) {
  751. for (i = 0; i < rf; i++)
  752. rtlpriv->dm.ofdm_index[i] -= delta;
  753. rtlpriv->dm.cck_index -= delta;
  754. } else {
  755. for (i = 0; i < rf; i++)
  756. rtlpriv->dm.ofdm_index[i] += delta;
  757. rtlpriv->dm.cck_index += delta;
  758. }
  759. if (is2t) {
  760. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  761. "temp OFDM_A_index=0x%x, OFDM_B_index=0x%x, cck_index=0x%x\n",
  762. rtlpriv->dm.ofdm_index[0],
  763. rtlpriv->dm.ofdm_index[1],
  764. rtlpriv->dm.cck_index);
  765. } else {
  766. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  767. "temp OFDM_A_index=0x%x, cck_index=0x%x\n",
  768. rtlpriv->dm.ofdm_index[0],
  769. rtlpriv->dm.cck_index);
  770. }
  771. if (thermalvalue > rtlefuse->eeprom_thermalmeter) {
  772. for (i = 0; i < rf; i++)
  773. ofdm_index[i] =
  774. rtlpriv->dm.ofdm_index[i]
  775. + 1;
  776. cck_index = rtlpriv->dm.cck_index + 1;
  777. } else {
  778. for (i = 0; i < rf; i++)
  779. ofdm_index[i] =
  780. rtlpriv->dm.ofdm_index[i];
  781. cck_index = rtlpriv->dm.cck_index;
  782. }
  783. for (i = 0; i < rf; i++) {
  784. if (txpwr_level[i] >= 0 &&
  785. txpwr_level[i] <= 26) {
  786. if (thermalvalue >
  787. rtlefuse->eeprom_thermalmeter) {
  788. if (delta < 5)
  789. ofdm_index[i] -= 1;
  790. else
  791. ofdm_index[i] -= 2;
  792. } else if (delta > 5 && thermalvalue <
  793. rtlefuse->
  794. eeprom_thermalmeter) {
  795. ofdm_index[i] += 1;
  796. }
  797. } else if (txpwr_level[i] >= 27 &&
  798. txpwr_level[i] <= 32
  799. && thermalvalue >
  800. rtlefuse->eeprom_thermalmeter) {
  801. if (delta < 5)
  802. ofdm_index[i] -= 1;
  803. else
  804. ofdm_index[i] -= 2;
  805. } else if (txpwr_level[i] >= 32 &&
  806. txpwr_level[i] <= 38 &&
  807. thermalvalue >
  808. rtlefuse->eeprom_thermalmeter
  809. && delta > 5) {
  810. ofdm_index[i] -= 1;
  811. }
  812. }
  813. if (txpwr_level[i] >= 0 && txpwr_level[i] <= 26) {
  814. if (thermalvalue >
  815. rtlefuse->eeprom_thermalmeter) {
  816. if (delta < 5)
  817. cck_index -= 1;
  818. else
  819. cck_index -= 2;
  820. } else if (delta > 5 && thermalvalue <
  821. rtlefuse->eeprom_thermalmeter) {
  822. cck_index += 1;
  823. }
  824. } else if (txpwr_level[i] >= 27 &&
  825. txpwr_level[i] <= 32 &&
  826. thermalvalue >
  827. rtlefuse->eeprom_thermalmeter) {
  828. if (delta < 5)
  829. cck_index -= 1;
  830. else
  831. cck_index -= 2;
  832. } else if (txpwr_level[i] >= 32 &&
  833. txpwr_level[i] <= 38 &&
  834. thermalvalue > rtlefuse->eeprom_thermalmeter
  835. && delta > 5) {
  836. cck_index -= 1;
  837. }
  838. for (i = 0; i < rf; i++) {
  839. if (ofdm_index[i] > OFDM_TABLE_SIZE - 1)
  840. ofdm_index[i] = OFDM_TABLE_SIZE - 1;
  841. else if (ofdm_index[i] < ofdm_min_index)
  842. ofdm_index[i] = ofdm_min_index;
  843. }
  844. if (cck_index > CCK_TABLE_SIZE - 1)
  845. cck_index = CCK_TABLE_SIZE - 1;
  846. else if (cck_index < 0)
  847. cck_index = 0;
  848. if (is2t) {
  849. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  850. "new OFDM_A_index=0x%x, OFDM_B_index=0x%x, cck_index=0x%x\n",
  851. ofdm_index[0], ofdm_index[1],
  852. cck_index);
  853. } else {
  854. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  855. "new OFDM_A_index=0x%x, cck_index=0x%x\n",
  856. ofdm_index[0], cck_index);
  857. }
  858. }
  859. if (rtlpriv->dm.txpower_track_control && delta != 0) {
  860. ele_d =
  861. (ofdmswing_table[ofdm_index[0]] & 0xFFC00000) >> 22;
  862. val_x = rtlphy->reg_e94;
  863. val_y = rtlphy->reg_e9c;
  864. if (val_x != 0) {
  865. if ((val_x & 0x00000200) != 0)
  866. val_x = val_x | 0xFFFFFC00;
  867. ele_a = ((val_x * ele_d) >> 8) & 0x000003FF;
  868. if ((val_y & 0x00000200) != 0)
  869. val_y = val_y | 0xFFFFFC00;
  870. ele_c = ((val_y * ele_d) >> 8) & 0x000003FF;
  871. value32 = (ele_d << 22) |
  872. ((ele_c & 0x3F) << 16) | ele_a;
  873. rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  874. MASKDWORD, value32);
  875. value32 = (ele_c & 0x000003C0) >> 6;
  876. rtl_set_bbreg(hw, ROFDM0_XCTXAFE, MASKH4BITS,
  877. value32);
  878. value32 = ((val_x * ele_d) >> 7) & 0x01;
  879. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  880. BIT(31), value32);
  881. value32 = ((val_y * ele_d) >> 7) & 0x01;
  882. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  883. BIT(29), value32);
  884. } else {
  885. rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  886. MASKDWORD,
  887. ofdmswing_table[ofdm_index[0]]);
  888. rtl_set_bbreg(hw, ROFDM0_XCTXAFE, MASKH4BITS,
  889. 0x00);
  890. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  891. BIT(31) | BIT(29), 0x00);
  892. }
  893. if (!rtlpriv->dm.cck_inch14) {
  894. rtl_write_byte(rtlpriv, 0xa22,
  895. cckswing_table_ch1ch13[cck_index]
  896. [0]);
  897. rtl_write_byte(rtlpriv, 0xa23,
  898. cckswing_table_ch1ch13[cck_index]
  899. [1]);
  900. rtl_write_byte(rtlpriv, 0xa24,
  901. cckswing_table_ch1ch13[cck_index]
  902. [2]);
  903. rtl_write_byte(rtlpriv, 0xa25,
  904. cckswing_table_ch1ch13[cck_index]
  905. [3]);
  906. rtl_write_byte(rtlpriv, 0xa26,
  907. cckswing_table_ch1ch13[cck_index]
  908. [4]);
  909. rtl_write_byte(rtlpriv, 0xa27,
  910. cckswing_table_ch1ch13[cck_index]
  911. [5]);
  912. rtl_write_byte(rtlpriv, 0xa28,
  913. cckswing_table_ch1ch13[cck_index]
  914. [6]);
  915. rtl_write_byte(rtlpriv, 0xa29,
  916. cckswing_table_ch1ch13[cck_index]
  917. [7]);
  918. } else {
  919. rtl_write_byte(rtlpriv, 0xa22,
  920. cckswing_table_ch14[cck_index]
  921. [0]);
  922. rtl_write_byte(rtlpriv, 0xa23,
  923. cckswing_table_ch14[cck_index]
  924. [1]);
  925. rtl_write_byte(rtlpriv, 0xa24,
  926. cckswing_table_ch14[cck_index]
  927. [2]);
  928. rtl_write_byte(rtlpriv, 0xa25,
  929. cckswing_table_ch14[cck_index]
  930. [3]);
  931. rtl_write_byte(rtlpriv, 0xa26,
  932. cckswing_table_ch14[cck_index]
  933. [4]);
  934. rtl_write_byte(rtlpriv, 0xa27,
  935. cckswing_table_ch14[cck_index]
  936. [5]);
  937. rtl_write_byte(rtlpriv, 0xa28,
  938. cckswing_table_ch14[cck_index]
  939. [6]);
  940. rtl_write_byte(rtlpriv, 0xa29,
  941. cckswing_table_ch14[cck_index]
  942. [7]);
  943. }
  944. if (is2t) {
  945. ele_d = (ofdmswing_table[ofdm_index[1]] &
  946. 0xFFC00000) >> 22;
  947. val_x = rtlphy->reg_eb4;
  948. val_y = rtlphy->reg_ebc;
  949. if (val_x != 0) {
  950. if ((val_x & 0x00000200) != 0)
  951. val_x = val_x | 0xFFFFFC00;
  952. ele_a = ((val_x * ele_d) >> 8) &
  953. 0x000003FF;
  954. if ((val_y & 0x00000200) != 0)
  955. val_y = val_y | 0xFFFFFC00;
  956. ele_c = ((val_y * ele_d) >> 8) &
  957. 0x00003FF;
  958. value32 = (ele_d << 22) |
  959. ((ele_c & 0x3F) << 16) | ele_a;
  960. rtl_set_bbreg(hw,
  961. ROFDM0_XBTXIQIMBALANCE,
  962. MASKDWORD, value32);
  963. value32 = (ele_c & 0x000003C0) >> 6;
  964. rtl_set_bbreg(hw, ROFDM0_XDTXAFE,
  965. MASKH4BITS, value32);
  966. value32 = ((val_x * ele_d) >> 7) & 0x01;
  967. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  968. BIT(27), value32);
  969. value32 = ((val_y * ele_d) >> 7) & 0x01;
  970. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  971. BIT(25), value32);
  972. } else {
  973. rtl_set_bbreg(hw,
  974. ROFDM0_XBTXIQIMBALANCE,
  975. MASKDWORD,
  976. ofdmswing_table[ofdm_index
  977. [1]]);
  978. rtl_set_bbreg(hw, ROFDM0_XDTXAFE,
  979. MASKH4BITS, 0x00);
  980. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  981. BIT(27) | BIT(25), 0x00);
  982. }
  983. }
  984. }
  985. if (delta_iqk > 3) {
  986. rtlpriv->dm.thermalvalue_iqk = thermalvalue;
  987. rtl92c_phy_iq_calibrate(hw, false);
  988. }
  989. if (rtlpriv->dm.txpower_track_control)
  990. rtlpriv->dm.thermalvalue = thermalvalue;
  991. }
  992. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD, "<===\n");
  993. }
  994. static void rtl92c_dm_initialize_txpower_tracking_thermalmeter(
  995. struct ieee80211_hw *hw)
  996. {
  997. struct rtl_priv *rtlpriv = rtl_priv(hw);
  998. rtlpriv->dm.txpower_tracking = true;
  999. rtlpriv->dm.txpower_trackinginit = false;
  1000. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1001. "pMgntInfo->txpower_tracking = %d\n",
  1002. rtlpriv->dm.txpower_tracking);
  1003. }
  1004. static void rtl92c_dm_initialize_txpower_tracking(struct ieee80211_hw *hw)
  1005. {
  1006. rtl92c_dm_initialize_txpower_tracking_thermalmeter(hw);
  1007. }
  1008. static void rtl92c_dm_txpower_tracking_directcall(struct ieee80211_hw *hw)
  1009. {
  1010. rtl92c_dm_txpower_tracking_callback_thermalmeter(hw);
  1011. }
  1012. static void rtl92c_dm_check_txpower_tracking_thermal_meter(
  1013. struct ieee80211_hw *hw)
  1014. {
  1015. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1016. if (!rtlpriv->dm.txpower_tracking)
  1017. return;
  1018. if (!rtlpriv->dm.tm_trigger) {
  1019. rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER, RFREG_OFFSET_MASK,
  1020. 0x60);
  1021. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1022. "Trigger 92S Thermal Meter!!\n");
  1023. rtlpriv->dm.tm_trigger = 1;
  1024. return;
  1025. } else {
  1026. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1027. "Schedule TxPowerTracking direct call!!\n");
  1028. rtl92c_dm_txpower_tracking_directcall(hw);
  1029. rtlpriv->dm.tm_trigger = 0;
  1030. }
  1031. }
  1032. void rtl92c_dm_check_txpower_tracking(struct ieee80211_hw *hw)
  1033. {
  1034. rtl92c_dm_check_txpower_tracking_thermal_meter(hw);
  1035. }
  1036. EXPORT_SYMBOL(rtl92c_dm_check_txpower_tracking);
  1037. void rtl92c_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
  1038. {
  1039. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1040. struct rate_adaptive *p_ra = &(rtlpriv->ra);
  1041. p_ra->ratr_state = DM_RATR_STA_INIT;
  1042. p_ra->pre_ratr_state = DM_RATR_STA_INIT;
  1043. if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER)
  1044. rtlpriv->dm.useramask = true;
  1045. else
  1046. rtlpriv->dm.useramask = false;
  1047. }
  1048. EXPORT_SYMBOL(rtl92c_dm_init_rate_adaptive_mask);
  1049. static void rtl92c_dm_init_dynamic_bb_powersaving(struct ieee80211_hw *hw)
  1050. {
  1051. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1052. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1053. dm_pstable->pre_ccastate = CCA_MAX;
  1054. dm_pstable->cur_ccasate = CCA_MAX;
  1055. dm_pstable->pre_rfstate = RF_MAX;
  1056. dm_pstable->cur_rfstate = RF_MAX;
  1057. dm_pstable->rssi_val_min = 0;
  1058. }
  1059. void rtl92c_dm_rf_saving(struct ieee80211_hw *hw, u8 bforce_in_normal)
  1060. {
  1061. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1062. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1063. if (!rtlpriv->reg_init) {
  1064. rtlpriv->reg_874 = (rtl_get_bbreg(hw,
  1065. RFPGA0_XCD_RFINTERFACESW,
  1066. MASKDWORD) & 0x1CC000) >> 14;
  1067. rtlpriv->reg_c70 = (rtl_get_bbreg(hw, ROFDM0_AGCPARAMETER1,
  1068. MASKDWORD) & BIT(3)) >> 3;
  1069. rtlpriv->reg_85c = (rtl_get_bbreg(hw, RFPGA0_XCD_SWITCHCONTROL,
  1070. MASKDWORD) & 0xFF000000) >> 24;
  1071. rtlpriv->reg_a74 = (rtl_get_bbreg(hw, 0xa74, MASKDWORD) &
  1072. 0xF000) >> 12;
  1073. rtlpriv->reg_init = true;
  1074. }
  1075. if (!bforce_in_normal) {
  1076. if (dm_pstable->rssi_val_min != 0) {
  1077. if (dm_pstable->pre_rfstate == RF_NORMAL) {
  1078. if (dm_pstable->rssi_val_min >= 30)
  1079. dm_pstable->cur_rfstate = RF_SAVE;
  1080. else
  1081. dm_pstable->cur_rfstate = RF_NORMAL;
  1082. } else {
  1083. if (dm_pstable->rssi_val_min <= 25)
  1084. dm_pstable->cur_rfstate = RF_NORMAL;
  1085. else
  1086. dm_pstable->cur_rfstate = RF_SAVE;
  1087. }
  1088. } else {
  1089. dm_pstable->cur_rfstate = RF_MAX;
  1090. }
  1091. } else {
  1092. dm_pstable->cur_rfstate = RF_NORMAL;
  1093. }
  1094. if (dm_pstable->pre_rfstate != dm_pstable->cur_rfstate) {
  1095. if (dm_pstable->cur_rfstate == RF_SAVE) {
  1096. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW,
  1097. 0x1C0000, 0x2);
  1098. rtl_set_bbreg(hw, ROFDM0_AGCPARAMETER1, BIT(3), 0);
  1099. rtl_set_bbreg(hw, RFPGA0_XCD_SWITCHCONTROL,
  1100. 0xFF000000, 0x63);
  1101. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW,
  1102. 0xC000, 0x2);
  1103. rtl_set_bbreg(hw, 0xa74, 0xF000, 0x3);
  1104. rtl_set_bbreg(hw, 0x818, BIT(28), 0x0);
  1105. rtl_set_bbreg(hw, 0x818, BIT(28), 0x1);
  1106. } else {
  1107. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW,
  1108. 0x1CC000, rtlpriv->reg_874);
  1109. rtl_set_bbreg(hw, ROFDM0_AGCPARAMETER1, BIT(3),
  1110. rtlpriv->reg_c70);
  1111. rtl_set_bbreg(hw, RFPGA0_XCD_SWITCHCONTROL, 0xFF000000,
  1112. rtlpriv->reg_85c);
  1113. rtl_set_bbreg(hw, 0xa74, 0xF000, rtlpriv->reg_a74);
  1114. rtl_set_bbreg(hw, 0x818, BIT(28), 0x0);
  1115. }
  1116. dm_pstable->pre_rfstate = dm_pstable->cur_rfstate;
  1117. }
  1118. }
  1119. EXPORT_SYMBOL(rtl92c_dm_rf_saving);
  1120. static void rtl92c_dm_dynamic_bb_powersaving(struct ieee80211_hw *hw)
  1121. {
  1122. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1123. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1124. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1125. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1126. /* Determine the minimum RSSI */
  1127. if (((mac->link_state == MAC80211_NOLINK)) &&
  1128. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  1129. dm_pstable->rssi_val_min = 0;
  1130. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD, "Not connected to any\n");
  1131. }
  1132. if (mac->link_state == MAC80211_LINKED) {
  1133. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  1134. dm_pstable->rssi_val_min =
  1135. rtlpriv->dm.entry_min_undec_sm_pwdb;
  1136. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD,
  1137. "AP Client PWDB = 0x%lx\n",
  1138. dm_pstable->rssi_val_min);
  1139. } else {
  1140. dm_pstable->rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  1141. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD,
  1142. "STA Default Port PWDB = 0x%lx\n",
  1143. dm_pstable->rssi_val_min);
  1144. }
  1145. } else {
  1146. dm_pstable->rssi_val_min =
  1147. rtlpriv->dm.entry_min_undec_sm_pwdb;
  1148. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD,
  1149. "AP Ext Port PWDB = 0x%lx\n",
  1150. dm_pstable->rssi_val_min);
  1151. }
  1152. /* Power Saving for 92C */
  1153. if (IS_92C_SERIAL(rtlhal->version))
  1154. ;/* rtl92c_dm_1r_cca(hw); */
  1155. else
  1156. rtl92c_dm_rf_saving(hw, false);
  1157. }
  1158. void rtl92c_dm_init(struct ieee80211_hw *hw)
  1159. {
  1160. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1161. rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
  1162. rtlpriv->dm.dm_flag = DYNAMIC_FUNC_DISABLE | DYNAMIC_FUNC_DIG;
  1163. rtlpriv->dm.undec_sm_pwdb = -1;
  1164. rtlpriv->dm.undec_sm_cck = -1;
  1165. rtlpriv->dm.dm_initialgain_enable = true;
  1166. rtl_dm_diginit(hw, 0x20);
  1167. rtlpriv->dm.dm_flag |= HAL_DM_HIPWR_DISABLE;
  1168. rtl92c_dm_init_dynamic_txpower(hw);
  1169. rtl92c_dm_init_edca_turbo(hw);
  1170. rtl92c_dm_init_rate_adaptive_mask(hw);
  1171. rtlpriv->dm.dm_flag |= DYNAMIC_FUNC_SS;
  1172. rtl92c_dm_initialize_txpower_tracking(hw);
  1173. rtl92c_dm_init_dynamic_bb_powersaving(hw);
  1174. rtlpriv->dm.ofdm_pkt_cnt = 0;
  1175. rtlpriv->dm.dm_rssi_sel = RSSI_DEFAULT;
  1176. }
  1177. EXPORT_SYMBOL(rtl92c_dm_init);
  1178. void rtl92c_dm_dynamic_txpower(struct ieee80211_hw *hw)
  1179. {
  1180. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1181. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1182. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1183. long undec_sm_pwdb;
  1184. if (!rtlpriv->dm.dynamic_txpower_enable)
  1185. return;
  1186. if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
  1187. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  1188. return;
  1189. }
  1190. if ((mac->link_state < MAC80211_LINKED) &&
  1191. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  1192. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  1193. "Not connected to any\n");
  1194. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  1195. rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
  1196. return;
  1197. }
  1198. if (mac->link_state >= MAC80211_LINKED) {
  1199. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  1200. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  1201. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1202. "AP Client PWDB = 0x%lx\n",
  1203. undec_sm_pwdb);
  1204. } else {
  1205. undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
  1206. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1207. "STA Default Port PWDB = 0x%lx\n",
  1208. undec_sm_pwdb);
  1209. }
  1210. } else {
  1211. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  1212. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1213. "AP Ext Port PWDB = 0x%lx\n",
  1214. undec_sm_pwdb);
  1215. }
  1216. if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
  1217. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL2;
  1218. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1219. "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x0)\n");
  1220. } else if ((undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
  1221. (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
  1222. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
  1223. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1224. "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x10)\n");
  1225. } else if (undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
  1226. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  1227. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1228. "TXHIGHPWRLEVEL_NORMAL\n");
  1229. }
  1230. if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl)) {
  1231. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1232. "PHY_SetTxPowerLevel8192S() Channel = %d\n",
  1233. rtlphy->current_channel);
  1234. rtl92c_phy_set_txpower_level(hw, rtlphy->current_channel);
  1235. if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  1236. TXHIGHPWRLEVEL_NORMAL)
  1237. dm_restorepowerindex(hw);
  1238. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  1239. TXHIGHPWRLEVEL_LEVEL1)
  1240. dm_writepowerindex(hw, 0x14);
  1241. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  1242. TXHIGHPWRLEVEL_LEVEL2)
  1243. dm_writepowerindex(hw, 0x10);
  1244. }
  1245. rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
  1246. }
  1247. void rtl92c_dm_watchdog(struct ieee80211_hw *hw)
  1248. {
  1249. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1250. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1251. bool fw_current_inpsmode = false;
  1252. bool fw_ps_awake = true;
  1253. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
  1254. (u8 *) (&fw_current_inpsmode));
  1255. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FWLPS_RF_ON,
  1256. (u8 *) (&fw_ps_awake));
  1257. if (ppsc->p2p_ps_info.p2p_ps_mode)
  1258. fw_ps_awake = false;
  1259. if ((ppsc->rfpwr_state == ERFON) && ((!fw_current_inpsmode) &&
  1260. fw_ps_awake)
  1261. && (!ppsc->rfchange_inprogress)) {
  1262. rtl92c_dm_pwdb_monitor(hw);
  1263. rtl92c_dm_dig(hw);
  1264. rtl92c_dm_false_alarm_counter_statistics(hw);
  1265. rtl92c_dm_dynamic_bb_powersaving(hw);
  1266. rtl92c_dm_dynamic_txpower(hw);
  1267. rtl92c_dm_check_txpower_tracking(hw);
  1268. /* rtl92c_dm_refresh_rate_adaptive_mask(hw); */
  1269. rtl92c_dm_bt_coexist(hw);
  1270. rtl92c_dm_check_edca_turbo(hw);
  1271. }
  1272. }
  1273. EXPORT_SYMBOL(rtl92c_dm_watchdog);
  1274. u8 rtl92c_bt_rssi_state_change(struct ieee80211_hw *hw)
  1275. {
  1276. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1277. long undec_sm_pwdb;
  1278. u8 curr_bt_rssi_state = 0x00;
  1279. if (rtlpriv->mac80211.link_state == MAC80211_LINKED) {
  1280. undec_sm_pwdb = GET_UNDECORATED_AVERAGE_RSSI(rtlpriv);
  1281. } else {
  1282. if (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)
  1283. undec_sm_pwdb = 100;
  1284. else
  1285. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  1286. }
  1287. /* Check RSSI to determine HighPower/NormalPower state for
  1288. * BT coexistence. */
  1289. if (undec_sm_pwdb >= 67)
  1290. curr_bt_rssi_state &= (~BT_RSSI_STATE_NORMAL_POWER);
  1291. else if (undec_sm_pwdb < 62)
  1292. curr_bt_rssi_state |= BT_RSSI_STATE_NORMAL_POWER;
  1293. /* Check RSSI to determine AMPDU setting for BT coexistence. */
  1294. if (undec_sm_pwdb >= 40)
  1295. curr_bt_rssi_state &= (~BT_RSSI_STATE_AMDPU_OFF);
  1296. else if (undec_sm_pwdb <= 32)
  1297. curr_bt_rssi_state |= BT_RSSI_STATE_AMDPU_OFF;
  1298. /* Marked RSSI state. It will be used to determine BT coexistence
  1299. * setting later. */
  1300. if (undec_sm_pwdb < 35)
  1301. curr_bt_rssi_state |= BT_RSSI_STATE_SPECIAL_LOW;
  1302. else
  1303. curr_bt_rssi_state &= (~BT_RSSI_STATE_SPECIAL_LOW);
  1304. /* Check BT state related to BT_Idle in B/G mode. */
  1305. if (undec_sm_pwdb < 15)
  1306. curr_bt_rssi_state |= BT_RSSI_STATE_BG_EDCA_LOW;
  1307. else
  1308. curr_bt_rssi_state &= (~BT_RSSI_STATE_BG_EDCA_LOW);
  1309. if (curr_bt_rssi_state != rtlpriv->btcoexist.bt_rssi_state) {
  1310. rtlpriv->btcoexist.bt_rssi_state = curr_bt_rssi_state;
  1311. return true;
  1312. } else {
  1313. return false;
  1314. }
  1315. }
  1316. EXPORT_SYMBOL(rtl92c_bt_rssi_state_change);
  1317. static bool rtl92c_bt_state_change(struct ieee80211_hw *hw)
  1318. {
  1319. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1320. u32 polling, ratio_tx, ratio_pri;
  1321. u32 bt_tx, bt_pri;
  1322. u8 bt_state;
  1323. u8 cur_service_type;
  1324. if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
  1325. return false;
  1326. bt_state = rtl_read_byte(rtlpriv, 0x4fd);
  1327. bt_tx = rtl_read_dword(rtlpriv, 0x488) & BT_MASK;
  1328. bt_pri = rtl_read_dword(rtlpriv, 0x48c) & BT_MASK;
  1329. polling = rtl_read_dword(rtlpriv, 0x490);
  1330. if (bt_tx == BT_MASK && bt_pri == BT_MASK &&
  1331. polling == 0xffffffff && bt_state == 0xff)
  1332. return false;
  1333. bt_state &= BIT_OFFSET_LEN_MASK_32(0, 1);
  1334. if (bt_state != rtlpriv->btcoexist.bt_cur_state) {
  1335. rtlpriv->btcoexist.bt_cur_state = bt_state;
  1336. if (rtlpriv->btcoexist.reg_bt_sco == 3) {
  1337. rtlpriv->btcoexist.bt_service = BT_IDLE;
  1338. bt_state = bt_state |
  1339. ((rtlpriv->btcoexist.bt_ant_isolation == 1) ?
  1340. 0 : BIT_OFFSET_LEN_MASK_32(1, 1)) |
  1341. BIT_OFFSET_LEN_MASK_32(2, 1);
  1342. rtl_write_byte(rtlpriv, 0x4fd, bt_state);
  1343. }
  1344. return true;
  1345. }
  1346. ratio_tx = bt_tx * 1000 / polling;
  1347. ratio_pri = bt_pri * 1000 / polling;
  1348. rtlpriv->btcoexist.ratio_tx = ratio_tx;
  1349. rtlpriv->btcoexist.ratio_pri = ratio_pri;
  1350. if (bt_state && rtlpriv->btcoexist.reg_bt_sco == 3) {
  1351. if ((ratio_tx < 30) && (ratio_pri < 30))
  1352. cur_service_type = BT_IDLE;
  1353. else if ((ratio_pri > 110) && (ratio_pri < 250))
  1354. cur_service_type = BT_SCO;
  1355. else if ((ratio_tx >= 200) && (ratio_pri >= 200))
  1356. cur_service_type = BT_BUSY;
  1357. else if ((ratio_tx >= 350) && (ratio_tx < 500))
  1358. cur_service_type = BT_OTHERBUSY;
  1359. else if (ratio_tx >= 500)
  1360. cur_service_type = BT_PAN;
  1361. else
  1362. cur_service_type = BT_OTHER_ACTION;
  1363. if (cur_service_type != rtlpriv->btcoexist.bt_service) {
  1364. rtlpriv->btcoexist.bt_service = cur_service_type;
  1365. bt_state = bt_state |
  1366. ((rtlpriv->btcoexist.bt_ant_isolation == 1) ?
  1367. 0 : BIT_OFFSET_LEN_MASK_32(1, 1)) |
  1368. ((rtlpriv->btcoexist.bt_service != BT_IDLE) ?
  1369. 0 : BIT_OFFSET_LEN_MASK_32(2, 1));
  1370. /* Add interrupt migration when bt is not ini
  1371. * idle state (no traffic). */
  1372. if (rtlpriv->btcoexist.bt_service != BT_IDLE) {
  1373. rtl_write_word(rtlpriv, 0x504, 0x0ccc);
  1374. rtl_write_byte(rtlpriv, 0x506, 0x54);
  1375. rtl_write_byte(rtlpriv, 0x507, 0x54);
  1376. } else {
  1377. rtl_write_byte(rtlpriv, 0x506, 0x00);
  1378. rtl_write_byte(rtlpriv, 0x507, 0x00);
  1379. }
  1380. rtl_write_byte(rtlpriv, 0x4fd, bt_state);
  1381. return true;
  1382. }
  1383. }
  1384. return false;
  1385. }
  1386. static bool rtl92c_bt_wifi_connect_change(struct ieee80211_hw *hw)
  1387. {
  1388. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1389. static bool media_connect;
  1390. if (rtlpriv->mac80211.link_state < MAC80211_LINKED) {
  1391. media_connect = false;
  1392. } else {
  1393. if (!media_connect) {
  1394. media_connect = true;
  1395. return true;
  1396. }
  1397. media_connect = true;
  1398. }
  1399. return false;
  1400. }
  1401. static void rtl92c_bt_set_normal(struct ieee80211_hw *hw)
  1402. {
  1403. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1404. if (rtlpriv->btcoexist.bt_service == BT_OTHERBUSY) {
  1405. rtlpriv->btcoexist.bt_edca_ul = 0x5ea72b;
  1406. rtlpriv->btcoexist.bt_edca_dl = 0x5ea72b;
  1407. } else if (rtlpriv->btcoexist.bt_service == BT_BUSY) {
  1408. rtlpriv->btcoexist.bt_edca_ul = 0x5eb82f;
  1409. rtlpriv->btcoexist.bt_edca_dl = 0x5eb82f;
  1410. } else if (rtlpriv->btcoexist.bt_service == BT_SCO) {
  1411. if (rtlpriv->btcoexist.ratio_tx > 160) {
  1412. rtlpriv->btcoexist.bt_edca_ul = 0x5ea72f;
  1413. rtlpriv->btcoexist.bt_edca_dl = 0x5ea72f;
  1414. } else {
  1415. rtlpriv->btcoexist.bt_edca_ul = 0x5ea32b;
  1416. rtlpriv->btcoexist.bt_edca_dl = 0x5ea42b;
  1417. }
  1418. } else {
  1419. rtlpriv->btcoexist.bt_edca_ul = 0;
  1420. rtlpriv->btcoexist.bt_edca_dl = 0;
  1421. }
  1422. if ((rtlpriv->btcoexist.bt_service != BT_IDLE) &&
  1423. (rtlpriv->mac80211.mode == WIRELESS_MODE_G ||
  1424. (rtlpriv->mac80211.mode == (WIRELESS_MODE_G | WIRELESS_MODE_B))) &&
  1425. (rtlpriv->btcoexist.bt_rssi_state &
  1426. BT_RSSI_STATE_BG_EDCA_LOW)) {
  1427. rtlpriv->btcoexist.bt_edca_ul = 0x5eb82b;
  1428. rtlpriv->btcoexist.bt_edca_dl = 0x5eb82b;
  1429. }
  1430. }
  1431. static void rtl92c_bt_ant_isolation(struct ieee80211_hw *hw, u8 tmp1byte)
  1432. {
  1433. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1434. /* Only enable HW BT coexist when BT in "Busy" state. */
  1435. if (rtlpriv->mac80211.vendor == PEER_CISCO &&
  1436. rtlpriv->btcoexist.bt_service == BT_OTHER_ACTION) {
  1437. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, 0xa0);
  1438. } else {
  1439. if ((rtlpriv->btcoexist.bt_service == BT_BUSY) &&
  1440. (rtlpriv->btcoexist.bt_rssi_state &
  1441. BT_RSSI_STATE_NORMAL_POWER)) {
  1442. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, 0xa0);
  1443. } else if ((rtlpriv->btcoexist.bt_service ==
  1444. BT_OTHER_ACTION) && (rtlpriv->mac80211.mode <
  1445. WIRELESS_MODE_N_24G) &&
  1446. (rtlpriv->btcoexist.bt_rssi_state &
  1447. BT_RSSI_STATE_SPECIAL_LOW)) {
  1448. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, 0xa0);
  1449. } else if (rtlpriv->btcoexist.bt_service == BT_PAN) {
  1450. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, tmp1byte);
  1451. } else {
  1452. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, tmp1byte);
  1453. }
  1454. }
  1455. if (rtlpriv->btcoexist.bt_service == BT_PAN)
  1456. rtl_write_dword(rtlpriv, REG_GPIO_PIN_CTRL, 0x10100);
  1457. else
  1458. rtl_write_dword(rtlpriv, REG_GPIO_PIN_CTRL, 0x0);
  1459. if (rtlpriv->btcoexist.bt_rssi_state &
  1460. BT_RSSI_STATE_NORMAL_POWER) {
  1461. rtl92c_bt_set_normal(hw);
  1462. } else {
  1463. rtlpriv->btcoexist.bt_edca_ul = 0;
  1464. rtlpriv->btcoexist.bt_edca_dl = 0;
  1465. }
  1466. if (rtlpriv->btcoexist.bt_service != BT_IDLE) {
  1467. rtlpriv->cfg->ops->set_rfreg(hw,
  1468. RF90_PATH_A,
  1469. 0x1e,
  1470. 0xf0, 0xf);
  1471. } else {
  1472. rtlpriv->cfg->ops->set_rfreg(hw,
  1473. RF90_PATH_A, 0x1e, 0xf0,
  1474. rtlpriv->btcoexist.bt_rfreg_origin_1e);
  1475. }
  1476. if (!rtlpriv->dm.dynamic_txpower_enable) {
  1477. if (rtlpriv->btcoexist.bt_service != BT_IDLE) {
  1478. if (rtlpriv->btcoexist.bt_rssi_state &
  1479. BT_RSSI_STATE_TXPOWER_LOW) {
  1480. rtlpriv->dm.dynamic_txhighpower_lvl =
  1481. TXHIGHPWRLEVEL_BT2;
  1482. } else {
  1483. rtlpriv->dm.dynamic_txhighpower_lvl =
  1484. TXHIGHPWRLEVEL_BT1;
  1485. }
  1486. } else {
  1487. rtlpriv->dm.dynamic_txhighpower_lvl =
  1488. TXHIGHPWRLEVEL_NORMAL;
  1489. }
  1490. rtl92c_phy_set_txpower_level(hw,
  1491. rtlpriv->phy.current_channel);
  1492. }
  1493. }
  1494. static void rtl92c_check_bt_change(struct ieee80211_hw *hw)
  1495. {
  1496. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1497. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1498. u8 tmp1byte = 0;
  1499. if (IS_81XXC_VENDOR_UMC_B_CUT(rtlhal->version) &&
  1500. rtlpriv->btcoexist.bt_coexistence)
  1501. tmp1byte |= BIT(5);
  1502. if (rtlpriv->btcoexist.bt_cur_state) {
  1503. if (rtlpriv->btcoexist.bt_ant_isolation)
  1504. rtl92c_bt_ant_isolation(hw, tmp1byte);
  1505. } else {
  1506. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, tmp1byte);
  1507. rtlpriv->cfg->ops->set_rfreg(hw, RF90_PATH_A, 0x1e, 0xf0,
  1508. rtlpriv->btcoexist.bt_rfreg_origin_1e);
  1509. rtlpriv->btcoexist.bt_edca_ul = 0;
  1510. rtlpriv->btcoexist.bt_edca_dl = 0;
  1511. }
  1512. }
  1513. void rtl92c_dm_bt_coexist(struct ieee80211_hw *hw)
  1514. {
  1515. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1516. bool wifi_connect_change;
  1517. bool bt_state_change;
  1518. bool rssi_state_change;
  1519. if ((rtlpriv->btcoexist.bt_coexistence) &&
  1520. (rtlpriv->btcoexist.bt_coexist_type == BT_CSR_BC4)) {
  1521. wifi_connect_change = rtl92c_bt_wifi_connect_change(hw);
  1522. bt_state_change = rtl92c_bt_state_change(hw);
  1523. rssi_state_change = rtl92c_bt_rssi_state_change(hw);
  1524. if (wifi_connect_change || bt_state_change || rssi_state_change)
  1525. rtl92c_check_bt_change(hw);
  1526. }
  1527. }
  1528. EXPORT_SYMBOL(rtl92c_dm_bt_coexist);