dm.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2013 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * 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 "../base.h"
  27. #include "../pci.h"
  28. #include "../core.h"
  29. #include "reg.h"
  30. #include "def.h"
  31. #include "phy.h"
  32. #include "dm.h"
  33. #include "fw.h"
  34. #include "trx.h"
  35. static const u32 ofdmswing_table[OFDM_TABLE_SIZE] = {
  36. 0x7f8001fe, /* 0, +6.0dB */
  37. 0x788001e2, /* 1, +5.5dB */
  38. 0x71c001c7, /* 2, +5.0dB */
  39. 0x6b8001ae, /* 3, +4.5dB */
  40. 0x65400195, /* 4, +4.0dB */
  41. 0x5fc0017f, /* 5, +3.5dB */
  42. 0x5a400169, /* 6, +3.0dB */
  43. 0x55400155, /* 7, +2.5dB */
  44. 0x50800142, /* 8, +2.0dB */
  45. 0x4c000130, /* 9, +1.5dB */
  46. 0x47c0011f, /* 10, +1.0dB */
  47. 0x43c0010f, /* 11, +0.5dB */
  48. 0x40000100, /* 12, +0dB */
  49. 0x3c8000f2, /* 13, -0.5dB */
  50. 0x390000e4, /* 14, -1.0dB */
  51. 0x35c000d7, /* 15, -1.5dB */
  52. 0x32c000cb, /* 16, -2.0dB */
  53. 0x300000c0, /* 17, -2.5dB */
  54. 0x2d4000b5, /* 18, -3.0dB */
  55. 0x2ac000ab, /* 19, -3.5dB */
  56. 0x288000a2, /* 20, -4.0dB */
  57. 0x26000098, /* 21, -4.5dB */
  58. 0x24000090, /* 22, -5.0dB */
  59. 0x22000088, /* 23, -5.5dB */
  60. 0x20000080, /* 24, -6.0dB */
  61. 0x1e400079, /* 25, -6.5dB */
  62. 0x1c800072, /* 26, -7.0dB */
  63. 0x1b00006c, /* 27. -7.5dB */
  64. 0x19800066, /* 28, -8.0dB */
  65. 0x18000060, /* 29, -8.5dB */
  66. 0x16c0005b, /* 30, -9.0dB */
  67. 0x15800056, /* 31, -9.5dB */
  68. 0x14400051, /* 32, -10.0dB */
  69. 0x1300004c, /* 33, -10.5dB */
  70. 0x12000048, /* 34, -11.0dB */
  71. 0x11000044, /* 35, -11.5dB */
  72. 0x10000040, /* 36, -12.0dB */
  73. 0x0f00003c, /* 37, -12.5dB */
  74. 0x0e400039, /* 38, -13.0dB */
  75. 0x0d800036, /* 39, -13.5dB */
  76. 0x0cc00033, /* 40, -14.0dB */
  77. 0x0c000030, /* 41, -14.5dB */
  78. 0x0b40002d, /* 42, -15.0dB */
  79. };
  80. static const u8 cck_tbl_ch1_13[CCK_TABLE_SIZE][8] = {
  81. {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04}, /* 0, +0dB */
  82. {0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04}, /* 1, -0.5dB */
  83. {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03}, /* 2, -1.0dB */
  84. {0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03}, /* 3, -1.5dB */
  85. {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03}, /* 4, -2.0dB */
  86. {0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03}, /* 5, -2.5dB */
  87. {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03}, /* 6, -3.0dB */
  88. {0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03}, /* 7, -3.5dB */
  89. {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02}, /* 8, -4.0dB */
  90. {0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02}, /* 9, -4.5dB */
  91. {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02}, /* 10, -5.0dB */
  92. {0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02}, /* 11, -5.5dB */
  93. {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02}, /* 12, -6.0dB */
  94. {0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02}, /* 13, -6.5dB */
  95. {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02}, /* 14, -7.0dB */
  96. {0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02}, /* 15, -7.5dB */
  97. {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01}, /* 16, -8.0dB */
  98. {0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02}, /* 17, -8.5dB */
  99. {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01}, /* 18, -9.0dB */
  100. {0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01}, /* 19, -9.5dB */
  101. {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01}, /* 20, -10.0dB*/
  102. {0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01}, /* 21, -10.5dB*/
  103. {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01}, /* 22, -11.0dB*/
  104. {0x0e, 0x0e, 0x0c, 0x0a, 0x08, 0x05, 0x02, 0x01}, /* 23, -11.5dB*/
  105. {0x0d, 0x0d, 0x0c, 0x0a, 0x07, 0x05, 0x02, 0x01}, /* 24, -12.0dB*/
  106. {0x0d, 0x0c, 0x0b, 0x09, 0x07, 0x04, 0x02, 0x01}, /* 25, -12.5dB*/
  107. {0x0c, 0x0c, 0x0a, 0x09, 0x06, 0x04, 0x02, 0x01}, /* 26, -13.0dB*/
  108. {0x0b, 0x0b, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x01}, /* 27, -13.5dB*/
  109. {0x0b, 0x0a, 0x09, 0x08, 0x06, 0x04, 0x02, 0x01}, /* 28, -14.0dB*/
  110. {0x0a, 0x0a, 0x09, 0x07, 0x05, 0x03, 0x02, 0x01}, /* 29, -14.5dB*/
  111. {0x0a, 0x09, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01}, /* 30, -15.0dB*/
  112. {0x09, 0x09, 0x08, 0x06, 0x05, 0x03, 0x01, 0x01}, /* 31, -15.5dB*/
  113. {0x09, 0x08, 0x07, 0x06, 0x04, 0x03, 0x01, 0x01} /* 32, -16.0dB*/
  114. };
  115. static const u8 cck_tbl_ch14[CCK_TABLE_SIZE][8] = {
  116. {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00}, /* 0, +0dB */
  117. {0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00}, /* 1, -0.5dB */
  118. {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00}, /* 2, -1.0dB */
  119. {0x2d, 0x2d, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00}, /* 3, -1.5dB */
  120. {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00}, /* 4, -2.0dB */
  121. {0x28, 0x28, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00}, /* 5, -2.5dB */
  122. {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00}, /* 6, -3.0dB */
  123. {0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00}, /* 7, -3.5dB */
  124. {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00}, /* 8, -4.0dB */
  125. {0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00}, /* 9, -4.5dB */
  126. {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00}, /* 10, -5.0dB */
  127. {0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00}, /* 11, -5.5dB */
  128. {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00}, /* 12, -6.0dB */
  129. {0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00}, /* 13, -6.5dB */
  130. {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00}, /* 14, -7.0dB */
  131. {0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00}, /* 15, -7.5dB */
  132. {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00}, /* 16, -8.0dB */
  133. {0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00}, /* 17, -8.5dB */
  134. {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00}, /* 18, -9.0dB */
  135. {0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00}, /* 19, -9.5dB */
  136. {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00}, /* 20, -10.0dB*/
  137. {0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00}, /* 21, -10.5dB*/
  138. {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00}, /* 22, -11.0dB*/
  139. {0x0e, 0x0e, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00}, /* 23, -11.5dB*/
  140. {0x0d, 0x0d, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00}, /* 24, -12.0dB*/
  141. {0x0d, 0x0c, 0x0b, 0x06, 0x00, 0x00, 0x00, 0x00}, /* 25, -12.5dB*/
  142. {0x0c, 0x0c, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00}, /* 26, -13.0dB*/
  143. {0x0b, 0x0b, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00}, /* 27, -13.5dB*/
  144. {0x0b, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 28, -14.0dB*/
  145. {0x0a, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 29, -14.5dB*/
  146. {0x0a, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 30, -15.0dB*/
  147. {0x09, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 31, -15.5dB*/
  148. {0x09, 0x08, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00} /* 32, -16.0dB*/
  149. };
  150. #define CAL_SWING_OFF(_off, _dir, _size, _del) \
  151. do { \
  152. for (_off = 0; _off < _size; _off++) { \
  153. if (_del < thermal_threshold[_dir][_off]) { \
  154. if (_off != 0) \
  155. _off--; \
  156. break; \
  157. } \
  158. } \
  159. if (_off >= _size) \
  160. _off = _size - 1; \
  161. } while (0)
  162. static void rtl88e_set_iqk_matrix(struct ieee80211_hw *hw,
  163. u8 ofdm_index, u8 rfpath,
  164. long iqk_result_x, long iqk_result_y)
  165. {
  166. long ele_a = 0, ele_d, ele_c = 0, value32;
  167. ele_d = (ofdmswing_table[ofdm_index] & 0xFFC00000)>>22;
  168. if (iqk_result_x != 0) {
  169. if ((iqk_result_x & 0x00000200) != 0)
  170. iqk_result_x = iqk_result_x | 0xFFFFFC00;
  171. ele_a = ((iqk_result_x * ele_d)>>8)&0x000003FF;
  172. if ((iqk_result_y & 0x00000200) != 0)
  173. iqk_result_y = iqk_result_y | 0xFFFFFC00;
  174. ele_c = ((iqk_result_y * ele_d)>>8)&0x000003FF;
  175. switch (rfpath) {
  176. case RF90_PATH_A:
  177. value32 = (ele_d << 22)|((ele_c & 0x3F)<<16) | ele_a;
  178. rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  179. MASKDWORD, value32);
  180. value32 = (ele_c & 0x000003C0) >> 6;
  181. rtl_set_bbreg(hw, ROFDM0_XCTXAFE, MASKH4BITS,
  182. value32);
  183. value32 = ((iqk_result_x * ele_d) >> 7) & 0x01;
  184. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(24),
  185. value32);
  186. break;
  187. case RF90_PATH_B:
  188. value32 = (ele_d << 22)|((ele_c & 0x3F)<<16) | ele_a;
  189. rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, MASKDWORD,
  190. value32);
  191. value32 = (ele_c & 0x000003C0) >> 6;
  192. rtl_set_bbreg(hw, ROFDM0_XDTXAFE, MASKH4BITS, value32);
  193. value32 = ((iqk_result_x * ele_d) >> 7) & 0x01;
  194. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(28),
  195. value32);
  196. break;
  197. default:
  198. break;
  199. }
  200. } else {
  201. switch (rfpath) {
  202. case RF90_PATH_A:
  203. rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  204. MASKDWORD, ofdmswing_table[ofdm_index]);
  205. rtl_set_bbreg(hw, ROFDM0_XCTXAFE,
  206. MASKH4BITS, 0x00);
  207. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  208. BIT(24), 0x00);
  209. break;
  210. case RF90_PATH_B:
  211. rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
  212. MASKDWORD, ofdmswing_table[ofdm_index]);
  213. rtl_set_bbreg(hw, ROFDM0_XDTXAFE,
  214. MASKH4BITS, 0x00);
  215. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  216. BIT(28), 0x00);
  217. break;
  218. default:
  219. break;
  220. }
  221. }
  222. }
  223. void rtl88e_dm_txpower_track_adjust(struct ieee80211_hw *hw,
  224. u8 type, u8 *pdirection, u32 *poutwrite_val)
  225. {
  226. struct rtl_priv *rtlpriv = rtl_priv(hw);
  227. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  228. u8 pwr_val = 0;
  229. u8 cck_base = rtldm->swing_idx_cck_base;
  230. u8 cck_val = rtldm->swing_idx_cck;
  231. u8 ofdm_base = rtldm->swing_idx_ofdm_base[0];
  232. u8 ofdm_val = rtlpriv->dm.swing_idx_ofdm[RF90_PATH_A];
  233. if (type == 0) {
  234. if (ofdm_val <= ofdm_base) {
  235. *pdirection = 1;
  236. pwr_val = ofdm_base - ofdm_val;
  237. } else {
  238. *pdirection = 2;
  239. pwr_val = ofdm_base - ofdm_val;
  240. }
  241. } else if (type == 1) {
  242. if (cck_val <= cck_base) {
  243. *pdirection = 1;
  244. pwr_val = cck_base - cck_val;
  245. } else {
  246. *pdirection = 2;
  247. pwr_val = cck_val - cck_base;
  248. }
  249. }
  250. if (pwr_val >= TXPWRTRACK_MAX_IDX && (*pdirection == 1))
  251. pwr_val = TXPWRTRACK_MAX_IDX;
  252. *poutwrite_val = pwr_val | (pwr_val << 8) | (pwr_val << 16) |
  253. (pwr_val << 24);
  254. }
  255. static void dm_tx_pwr_track_set_pwr(struct ieee80211_hw *hw,
  256. enum pwr_track_control_method method,
  257. u8 rfpath, u8 channel_mapped_index)
  258. {
  259. struct rtl_priv *rtlpriv = rtl_priv(hw);
  260. struct rtl_phy *rtlphy = &rtlpriv->phy;
  261. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  262. if (method == TXAGC) {
  263. if (rtldm->swing_flag_ofdm ||
  264. rtldm->swing_flag_cck) {
  265. rtl88e_phy_set_txpower_level(hw,
  266. rtlphy->current_channel);
  267. rtldm->swing_flag_ofdm = false;
  268. rtldm->swing_flag_cck = false;
  269. }
  270. } else if (method == BBSWING) {
  271. if (!rtldm->cck_inch14) {
  272. rtl_write_byte(rtlpriv, 0xa22,
  273. cck_tbl_ch1_13[rtldm->swing_idx_cck][0]);
  274. rtl_write_byte(rtlpriv, 0xa23,
  275. cck_tbl_ch1_13[rtldm->swing_idx_cck][1]);
  276. rtl_write_byte(rtlpriv, 0xa24,
  277. cck_tbl_ch1_13[rtldm->swing_idx_cck][2]);
  278. rtl_write_byte(rtlpriv, 0xa25,
  279. cck_tbl_ch1_13[rtldm->swing_idx_cck][3]);
  280. rtl_write_byte(rtlpriv, 0xa26,
  281. cck_tbl_ch1_13[rtldm->swing_idx_cck][4]);
  282. rtl_write_byte(rtlpriv, 0xa27,
  283. cck_tbl_ch1_13[rtldm->swing_idx_cck][5]);
  284. rtl_write_byte(rtlpriv, 0xa28,
  285. cck_tbl_ch1_13[rtldm->swing_idx_cck][6]);
  286. rtl_write_byte(rtlpriv, 0xa29,
  287. cck_tbl_ch1_13[rtldm->swing_idx_cck][7]);
  288. } else {
  289. rtl_write_byte(rtlpriv, 0xa22,
  290. cck_tbl_ch14[rtldm->swing_idx_cck][0]);
  291. rtl_write_byte(rtlpriv, 0xa23,
  292. cck_tbl_ch14[rtldm->swing_idx_cck][1]);
  293. rtl_write_byte(rtlpriv, 0xa24,
  294. cck_tbl_ch14[rtldm->swing_idx_cck][2]);
  295. rtl_write_byte(rtlpriv, 0xa25,
  296. cck_tbl_ch14[rtldm->swing_idx_cck][3]);
  297. rtl_write_byte(rtlpriv, 0xa26,
  298. cck_tbl_ch14[rtldm->swing_idx_cck][4]);
  299. rtl_write_byte(rtlpriv, 0xa27,
  300. cck_tbl_ch14[rtldm->swing_idx_cck][5]);
  301. rtl_write_byte(rtlpriv, 0xa28,
  302. cck_tbl_ch14[rtldm->swing_idx_cck][6]);
  303. rtl_write_byte(rtlpriv, 0xa29,
  304. cck_tbl_ch14[rtldm->swing_idx_cck][7]);
  305. }
  306. if (rfpath == RF90_PATH_A) {
  307. rtl88e_set_iqk_matrix(hw, rtldm->swing_idx_ofdm[rfpath],
  308. rfpath, rtlphy->iqk_matrix
  309. [channel_mapped_index].
  310. value[0][0],
  311. rtlphy->iqk_matrix
  312. [channel_mapped_index].
  313. value[0][1]);
  314. } else if (rfpath == RF90_PATH_B) {
  315. rtl88e_set_iqk_matrix(hw, rtldm->swing_idx_ofdm[rfpath],
  316. rfpath, rtlphy->iqk_matrix
  317. [channel_mapped_index].
  318. value[0][4],
  319. rtlphy->iqk_matrix
  320. [channel_mapped_index].
  321. value[0][5]);
  322. }
  323. } else {
  324. return;
  325. }
  326. }
  327. static u8 rtl88e_dm_initial_gain_min_pwdb(struct ieee80211_hw *hw)
  328. {
  329. struct rtl_priv *rtlpriv = rtl_priv(hw);
  330. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  331. long rssi_val_min = 0;
  332. if ((dm_dig->curmultista_cstate == DIG_MULTISTA_CONNECT) &&
  333. (dm_dig->cur_sta_cstate == DIG_STA_CONNECT)) {
  334. if (rtlpriv->dm.entry_min_undec_sm_pwdb != 0)
  335. rssi_val_min =
  336. (rtlpriv->dm.entry_min_undec_sm_pwdb >
  337. rtlpriv->dm.undec_sm_pwdb) ?
  338. rtlpriv->dm.undec_sm_pwdb :
  339. rtlpriv->dm.entry_min_undec_sm_pwdb;
  340. else
  341. rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  342. } else if (dm_dig->cur_sta_cstate == DIG_STA_CONNECT ||
  343. dm_dig->cur_sta_cstate == DIG_STA_BEFORE_CONNECT) {
  344. rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  345. } else if (dm_dig->curmultista_cstate ==
  346. DIG_MULTISTA_CONNECT) {
  347. rssi_val_min = rtlpriv->dm.entry_min_undec_sm_pwdb;
  348. }
  349. return (u8)rssi_val_min;
  350. }
  351. static void rtl88e_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
  352. {
  353. u32 ret_value;
  354. struct rtl_priv *rtlpriv = rtl_priv(hw);
  355. struct false_alarm_statistics *falsealm_cnt = &rtlpriv->falsealm_cnt;
  356. rtl_set_bbreg(hw, ROFDM0_LSTF, BIT(31), 1);
  357. rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(31), 1);
  358. ret_value = rtl_get_bbreg(hw, ROFDM0_FRAMESYNC, MASKDWORD);
  359. falsealm_cnt->cnt_fast_fsync_fail = (ret_value&0xffff);
  360. falsealm_cnt->cnt_sb_search_fail = ((ret_value&0xffff0000)>>16);
  361. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
  362. falsealm_cnt->cnt_ofdm_cca = (ret_value&0xffff);
  363. falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
  364. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
  365. falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
  366. falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
  367. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
  368. falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
  369. falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
  370. falsealm_cnt->cnt_rate_illegal +
  371. falsealm_cnt->cnt_crc8_fail +
  372. falsealm_cnt->cnt_mcs_fail +
  373. falsealm_cnt->cnt_fast_fsync_fail +
  374. falsealm_cnt->cnt_sb_search_fail;
  375. ret_value = rtl_get_bbreg(hw, REG_SC_CNT, MASKDWORD);
  376. falsealm_cnt->cnt_bw_lsc = (ret_value & 0xffff);
  377. falsealm_cnt->cnt_bw_usc = ((ret_value & 0xffff0000) >> 16);
  378. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(12), 1);
  379. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(14), 1);
  380. ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERLOWER, MASKBYTE0);
  381. falsealm_cnt->cnt_cck_fail = ret_value;
  382. ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERUPPER, MASKBYTE3);
  383. falsealm_cnt->cnt_cck_fail += (ret_value & 0xff) << 8;
  384. ret_value = rtl_get_bbreg(hw, RCCK0_CCA_CNT, MASKDWORD);
  385. falsealm_cnt->cnt_cck_cca = ((ret_value & 0xff) << 8) |
  386. ((ret_value&0xFF00)>>8);
  387. falsealm_cnt->cnt_all = (falsealm_cnt->cnt_fast_fsync_fail +
  388. falsealm_cnt->cnt_sb_search_fail +
  389. falsealm_cnt->cnt_parity_fail +
  390. falsealm_cnt->cnt_rate_illegal +
  391. falsealm_cnt->cnt_crc8_fail +
  392. falsealm_cnt->cnt_mcs_fail +
  393. falsealm_cnt->cnt_cck_fail);
  394. falsealm_cnt->cnt_cca_all = falsealm_cnt->cnt_ofdm_cca +
  395. falsealm_cnt->cnt_cck_cca;
  396. rtl_set_bbreg(hw, ROFDM0_TRSWISOLATION, BIT(31), 1);
  397. rtl_set_bbreg(hw, ROFDM0_TRSWISOLATION, BIT(31), 0);
  398. rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(27), 1);
  399. rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(27), 0);
  400. rtl_set_bbreg(hw, ROFDM0_LSTF, BIT(31), 0);
  401. rtl_set_bbreg(hw, ROFDM1_LSTF, BIT(31), 0);
  402. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(13)|BIT(12), 0);
  403. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(13)|BIT(12), 2);
  404. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(15)|BIT(14), 0);
  405. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(15)|BIT(14), 2);
  406. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  407. "cnt_parity_fail = %d, cnt_rate_illegal = %d, cnt_crc8_fail = %d, cnt_mcs_fail = %d\n",
  408. falsealm_cnt->cnt_parity_fail,
  409. falsealm_cnt->cnt_rate_illegal,
  410. falsealm_cnt->cnt_crc8_fail, falsealm_cnt->cnt_mcs_fail);
  411. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  412. "cnt_ofdm_fail = %x, cnt_cck_fail = %x, cnt_all = %x\n",
  413. falsealm_cnt->cnt_ofdm_fail,
  414. falsealm_cnt->cnt_cck_fail, falsealm_cnt->cnt_all);
  415. }
  416. static void rtl88e_dm_cck_packet_detection_thresh(struct ieee80211_hw *hw)
  417. {
  418. struct rtl_priv *rtlpriv = rtl_priv(hw);
  419. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  420. u8 cur_cck_cca_thresh;
  421. if (dm_dig->cur_sta_cstate == DIG_STA_CONNECT) {
  422. dm_dig->rssi_val_min = rtl88e_dm_initial_gain_min_pwdb(hw);
  423. if (dm_dig->rssi_val_min > 25) {
  424. cur_cck_cca_thresh = 0xcd;
  425. } else if ((dm_dig->rssi_val_min <= 25) &&
  426. (dm_dig->rssi_val_min > 10)) {
  427. cur_cck_cca_thresh = 0x83;
  428. } else {
  429. if (rtlpriv->falsealm_cnt.cnt_cck_fail > 1000)
  430. cur_cck_cca_thresh = 0x83;
  431. else
  432. cur_cck_cca_thresh = 0x40;
  433. }
  434. } else {
  435. if (rtlpriv->falsealm_cnt.cnt_cck_fail > 1000)
  436. cur_cck_cca_thresh = 0x83;
  437. else
  438. cur_cck_cca_thresh = 0x40;
  439. }
  440. if (dm_dig->cur_cck_cca_thres != cur_cck_cca_thresh)
  441. rtl_set_bbreg(hw, RCCK0_CCA, MASKBYTE2, cur_cck_cca_thresh);
  442. dm_dig->cur_cck_cca_thres = cur_cck_cca_thresh;
  443. dm_dig->pre_cck_cca_thres = dm_dig->cur_cck_cca_thres;
  444. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  445. "CCK cca thresh hold =%x\n", dm_dig->cur_cck_cca_thres);
  446. }
  447. static void rtl88e_dm_dig(struct ieee80211_hw *hw)
  448. {
  449. struct rtl_priv *rtlpriv = rtl_priv(hw);
  450. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  451. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  452. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  453. u8 dig_dynamic_min, dig_maxofmin;
  454. bool bfirstconnect;
  455. u8 dm_dig_max, dm_dig_min;
  456. u8 current_igi = dm_dig->cur_igvalue;
  457. if (rtlpriv->dm.dm_initialgain_enable == false)
  458. return;
  459. if (dm_dig->dig_enable_flag == false)
  460. return;
  461. if (mac->act_scanning == true)
  462. return;
  463. if (mac->link_state >= MAC80211_LINKED)
  464. dm_dig->cur_sta_cstate = DIG_STA_CONNECT;
  465. else
  466. dm_dig->cur_sta_cstate = DIG_STA_DISCONNECT;
  467. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP ||
  468. rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
  469. dm_dig->cur_sta_cstate = DIG_STA_DISCONNECT;
  470. dm_dig_max = DM_DIG_MAX;
  471. dm_dig_min = DM_DIG_MIN;
  472. dig_maxofmin = DM_DIG_MAX_AP;
  473. dig_dynamic_min = dm_dig->dig_min_0;
  474. bfirstconnect = ((mac->link_state >= MAC80211_LINKED) ? true : false) &&
  475. !dm_dig->media_connect_0;
  476. dm_dig->rssi_val_min =
  477. rtl88e_dm_initial_gain_min_pwdb(hw);
  478. if (mac->link_state >= MAC80211_LINKED) {
  479. if ((dm_dig->rssi_val_min + 20) > dm_dig_max)
  480. dm_dig->rx_gain_max = dm_dig_max;
  481. else if ((dm_dig->rssi_val_min + 20) < dm_dig_min)
  482. dm_dig->rx_gain_max = dm_dig_min;
  483. else
  484. dm_dig->rx_gain_max = dm_dig->rssi_val_min + 20;
  485. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
  486. dig_dynamic_min = dm_dig->antdiv_rssi_max;
  487. } else {
  488. if (dm_dig->rssi_val_min < dm_dig_min)
  489. dig_dynamic_min = dm_dig_min;
  490. else if (dm_dig->rssi_val_min < dig_maxofmin)
  491. dig_dynamic_min = dig_maxofmin;
  492. else
  493. dig_dynamic_min = dm_dig->rssi_val_min;
  494. }
  495. } else {
  496. dm_dig->rx_gain_max = dm_dig_max;
  497. dig_dynamic_min = dm_dig_min;
  498. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD, "no link\n");
  499. }
  500. if (rtlpriv->falsealm_cnt.cnt_all > 10000) {
  501. dm_dig->large_fa_hit++;
  502. if (dm_dig->forbidden_igi < current_igi) {
  503. dm_dig->forbidden_igi = current_igi;
  504. dm_dig->large_fa_hit = 1;
  505. }
  506. if (dm_dig->large_fa_hit >= 3) {
  507. if ((dm_dig->forbidden_igi + 1) >
  508. dm_dig->rx_gain_max)
  509. dm_dig->rx_gain_min =
  510. dm_dig->rx_gain_max;
  511. else
  512. dm_dig->rx_gain_min =
  513. dm_dig->forbidden_igi + 1;
  514. dm_dig->recover_cnt = 3600;
  515. }
  516. } else {
  517. if (dm_dig->recover_cnt != 0) {
  518. dm_dig->recover_cnt--;
  519. } else {
  520. if (dm_dig->large_fa_hit == 0) {
  521. if ((dm_dig->forbidden_igi - 1) <
  522. dig_dynamic_min) {
  523. dm_dig->forbidden_igi = dig_dynamic_min;
  524. dm_dig->rx_gain_min = dig_dynamic_min;
  525. } else {
  526. dm_dig->forbidden_igi--;
  527. dm_dig->rx_gain_min =
  528. dm_dig->forbidden_igi + 1;
  529. }
  530. } else if (dm_dig->large_fa_hit == 3) {
  531. dm_dig->large_fa_hit = 0;
  532. }
  533. }
  534. }
  535. if (dm_dig->cur_sta_cstate == DIG_STA_CONNECT) {
  536. if (bfirstconnect) {
  537. current_igi = dm_dig->rssi_val_min;
  538. } else {
  539. if (rtlpriv->falsealm_cnt.cnt_all > DM_DIG_FA_TH2)
  540. current_igi += 2;
  541. else if (rtlpriv->falsealm_cnt.cnt_all > DM_DIG_FA_TH1)
  542. current_igi++;
  543. else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH0)
  544. current_igi--;
  545. }
  546. } else {
  547. if (rtlpriv->falsealm_cnt.cnt_all > 10000)
  548. current_igi += 2;
  549. else if (rtlpriv->falsealm_cnt.cnt_all > 8000)
  550. current_igi++;
  551. else if (rtlpriv->falsealm_cnt.cnt_all < 500)
  552. current_igi--;
  553. }
  554. if (current_igi > DM_DIG_FA_UPPER)
  555. current_igi = DM_DIG_FA_UPPER;
  556. else if (current_igi < DM_DIG_FA_LOWER)
  557. current_igi = DM_DIG_FA_LOWER;
  558. if (rtlpriv->falsealm_cnt.cnt_all > 10000)
  559. current_igi = DM_DIG_FA_UPPER;
  560. dm_dig->cur_igvalue = current_igi;
  561. rtl88e_dm_write_dig(hw);
  562. dm_dig->media_connect_0 =
  563. ((mac->link_state >= MAC80211_LINKED) ? true : false);
  564. dm_dig->dig_min_0 = dig_dynamic_min;
  565. rtl88e_dm_cck_packet_detection_thresh(hw);
  566. }
  567. static void rtl88e_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
  568. {
  569. struct rtl_priv *rtlpriv = rtl_priv(hw);
  570. rtlpriv->dm.dynamic_txpower_enable = false;
  571. rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
  572. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  573. }
  574. static void rtl92c_dm_dynamic_txpower(struct ieee80211_hw *hw)
  575. {
  576. struct rtl_priv *rtlpriv = rtl_priv(hw);
  577. struct rtl_phy *rtlphy = &rtlpriv->phy;
  578. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  579. long undec_sm_pwdb;
  580. if (!rtlpriv->dm.dynamic_txpower_enable)
  581. return;
  582. if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
  583. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  584. return;
  585. }
  586. if ((mac->link_state < MAC80211_LINKED) &&
  587. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  588. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  589. "Not connected to any\n");
  590. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  591. rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
  592. return;
  593. }
  594. if (mac->link_state >= MAC80211_LINKED) {
  595. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  596. undec_sm_pwdb =
  597. rtlpriv->dm.entry_min_undec_sm_pwdb;
  598. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  599. "AP Client PWDB = 0x%lx\n",
  600. undec_sm_pwdb);
  601. } else {
  602. undec_sm_pwdb =
  603. rtlpriv->dm.undec_sm_pwdb;
  604. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  605. "STA Default Port PWDB = 0x%lx\n",
  606. undec_sm_pwdb);
  607. }
  608. } else {
  609. undec_sm_pwdb =
  610. rtlpriv->dm.entry_min_undec_sm_pwdb;
  611. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  612. "AP Ext Port PWDB = 0x%lx\n",
  613. undec_sm_pwdb);
  614. }
  615. if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
  616. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
  617. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  618. "TXHIGHPWRLEVEL_LEVEL1 (TxPwr = 0x0)\n");
  619. } else if ((undec_sm_pwdb <
  620. (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
  621. (undec_sm_pwdb >=
  622. TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
  623. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
  624. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  625. "TXHIGHPWRLEVEL_LEVEL1 (TxPwr = 0x10)\n");
  626. } else if (undec_sm_pwdb <
  627. (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
  628. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  629. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  630. "TXHIGHPWRLEVEL_NORMAL\n");
  631. }
  632. if ((rtlpriv->dm.dynamic_txhighpower_lvl !=
  633. rtlpriv->dm.last_dtp_lvl)) {
  634. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  635. "PHY_SetTxPowerLevel8192S() Channel = %d\n",
  636. rtlphy->current_channel);
  637. rtl88e_phy_set_txpower_level(hw, rtlphy->current_channel);
  638. }
  639. rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
  640. }
  641. void rtl88e_dm_write_dig(struct ieee80211_hw *hw)
  642. {
  643. struct rtl_priv *rtlpriv = rtl_priv(hw);
  644. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  645. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  646. "cur_igvalue = 0x%x, pre_igvalue = 0x%x, backoff_val = %d\n",
  647. dm_dig->cur_igvalue, dm_dig->pre_igvalue,
  648. dm_dig->back_val);
  649. if (dm_dig->cur_igvalue > 0x3f)
  650. dm_dig->cur_igvalue = 0x3f;
  651. if (dm_dig->pre_igvalue != dm_dig->cur_igvalue) {
  652. rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f,
  653. dm_dig->cur_igvalue);
  654. dm_dig->pre_igvalue = dm_dig->cur_igvalue;
  655. }
  656. }
  657. static void rtl88e_dm_pwdb_monitor(struct ieee80211_hw *hw)
  658. {
  659. struct rtl_priv *rtlpriv = rtl_priv(hw);
  660. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  661. struct rtl_sta_info *drv_priv;
  662. static u64 last_record_txok_cnt;
  663. static u64 last_record_rxok_cnt;
  664. long tmp_entry_max_pwdb = 0, tmp_entry_min_pwdb = 0xff;
  665. if (rtlhal->oem_id == RT_CID_819X_HP) {
  666. u64 cur_txok_cnt = 0;
  667. u64 cur_rxok_cnt = 0;
  668. cur_txok_cnt = rtlpriv->stats.txbytesunicast -
  669. last_record_txok_cnt;
  670. cur_rxok_cnt = rtlpriv->stats.rxbytesunicast -
  671. last_record_rxok_cnt;
  672. last_record_txok_cnt = cur_txok_cnt;
  673. last_record_rxok_cnt = cur_rxok_cnt;
  674. if (cur_rxok_cnt > (cur_txok_cnt * 6))
  675. rtl_write_dword(rtlpriv, REG_ARFR0, 0x8f015);
  676. else
  677. rtl_write_dword(rtlpriv, REG_ARFR0, 0xff015);
  678. }
  679. /* AP & ADHOC & MESH */
  680. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  681. list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
  682. if (drv_priv->rssi_stat.undec_sm_pwdb <
  683. tmp_entry_min_pwdb)
  684. tmp_entry_min_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
  685. if (drv_priv->rssi_stat.undec_sm_pwdb >
  686. tmp_entry_max_pwdb)
  687. tmp_entry_max_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
  688. }
  689. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  690. /* If associated entry is found */
  691. if (tmp_entry_max_pwdb != 0) {
  692. rtlpriv->dm.entry_max_undec_sm_pwdb = tmp_entry_max_pwdb;
  693. RTPRINT(rtlpriv, FDM, DM_PWDB, "EntryMaxPWDB = 0x%lx(%ld)\n",
  694. tmp_entry_max_pwdb, tmp_entry_max_pwdb);
  695. } else {
  696. rtlpriv->dm.entry_max_undec_sm_pwdb = 0;
  697. }
  698. /* If associated entry is found */
  699. if (tmp_entry_min_pwdb != 0xff) {
  700. rtlpriv->dm.entry_min_undec_sm_pwdb = tmp_entry_min_pwdb;
  701. RTPRINT(rtlpriv, FDM, DM_PWDB, "EntryMinPWDB = 0x%lx(%ld)\n",
  702. tmp_entry_min_pwdb, tmp_entry_min_pwdb);
  703. } else {
  704. rtlpriv->dm.entry_min_undec_sm_pwdb = 0;
  705. }
  706. /* Indicate Rx signal strength to FW. */
  707. if (rtlpriv->dm.useramask) {
  708. u8 h2c_parameter[3] = { 0 };
  709. h2c_parameter[2] = (u8)(rtlpriv->dm.undec_sm_pwdb & 0xFF);
  710. h2c_parameter[0] = 0x20;
  711. } else {
  712. rtl_write_byte(rtlpriv, 0x4fe, rtlpriv->dm.undec_sm_pwdb);
  713. }
  714. }
  715. void rtl88e_dm_init_edca_turbo(struct ieee80211_hw *hw)
  716. {
  717. struct rtl_priv *rtlpriv = rtl_priv(hw);
  718. rtlpriv->dm.current_turbo_edca = false;
  719. rtlpriv->dm.is_any_nonbepkts = false;
  720. rtlpriv->dm.is_cur_rdlstate = false;
  721. }
  722. static void rtl88e_dm_check_edca_turbo(struct ieee80211_hw *hw)
  723. {
  724. struct rtl_priv *rtlpriv = rtl_priv(hw);
  725. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  726. static u64 last_txok_cnt;
  727. static u64 last_rxok_cnt;
  728. static u32 last_bt_edca_ul;
  729. static u32 last_bt_edca_dl;
  730. u64 cur_txok_cnt = 0;
  731. u64 cur_rxok_cnt = 0;
  732. u32 edca_be_ul = 0x5ea42b;
  733. u32 edca_be_dl = 0x5ea42b;
  734. bool bt_change_edca = false;
  735. if ((last_bt_edca_ul != rtlpriv->btcoexist.bt_edca_ul) ||
  736. (last_bt_edca_dl != rtlpriv->btcoexist.bt_edca_dl)) {
  737. rtlpriv->dm.current_turbo_edca = false;
  738. last_bt_edca_ul = rtlpriv->btcoexist.bt_edca_ul;
  739. last_bt_edca_dl = rtlpriv->btcoexist.bt_edca_dl;
  740. }
  741. if (rtlpriv->btcoexist.bt_edca_ul != 0) {
  742. edca_be_ul = rtlpriv->btcoexist.bt_edca_ul;
  743. bt_change_edca = true;
  744. }
  745. if (rtlpriv->btcoexist.bt_edca_dl != 0) {
  746. edca_be_ul = rtlpriv->btcoexist.bt_edca_dl;
  747. bt_change_edca = true;
  748. }
  749. if (mac->link_state != MAC80211_LINKED) {
  750. rtlpriv->dm.current_turbo_edca = false;
  751. return;
  752. }
  753. if ((bt_change_edca) ||
  754. ((!rtlpriv->dm.is_any_nonbepkts) &&
  755. (!rtlpriv->dm.disable_framebursting))) {
  756. cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
  757. cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
  758. if (cur_rxok_cnt > 4 * cur_txok_cnt) {
  759. if (!rtlpriv->dm.is_cur_rdlstate ||
  760. !rtlpriv->dm.current_turbo_edca) {
  761. rtl_write_dword(rtlpriv,
  762. REG_EDCA_BE_PARAM,
  763. edca_be_dl);
  764. rtlpriv->dm.is_cur_rdlstate = true;
  765. }
  766. } else {
  767. if (rtlpriv->dm.is_cur_rdlstate ||
  768. !rtlpriv->dm.current_turbo_edca) {
  769. rtl_write_dword(rtlpriv,
  770. REG_EDCA_BE_PARAM,
  771. edca_be_ul);
  772. rtlpriv->dm.is_cur_rdlstate = false;
  773. }
  774. }
  775. rtlpriv->dm.current_turbo_edca = true;
  776. } else {
  777. if (rtlpriv->dm.current_turbo_edca) {
  778. u8 tmp = AC0_BE;
  779. rtlpriv->cfg->ops->set_hw_reg(hw,
  780. HW_VAR_AC_PARAM,
  781. &tmp);
  782. rtlpriv->dm.current_turbo_edca = false;
  783. }
  784. }
  785. rtlpriv->dm.is_any_nonbepkts = false;
  786. last_txok_cnt = rtlpriv->stats.txbytesunicast;
  787. last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
  788. }
  789. static void dm_txpower_track_cb_therm(struct ieee80211_hw *hw)
  790. {
  791. struct rtl_priv *rtlpriv = rtl_priv(hw);
  792. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  793. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  794. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  795. u8 thermalvalue = 0, delta, delta_lck, delta_iqk, offset;
  796. u8 thermalvalue_avg_count = 0;
  797. u32 thermalvalue_avg = 0;
  798. long ele_d, temp_cck;
  799. s8 ofdm_index[2], cck_index = 0,
  800. ofdm_index_old[2] = {0, 0}, cck_index_old = 0;
  801. int i = 0;
  802. /*bool is2t = false;*/
  803. u8 ofdm_min_index = 6, rf = 1;
  804. /*u8 index_for_channel;*/
  805. enum _power_dec_inc {power_dec, power_inc};
  806. /*0.1 the following TWO tables decide the
  807. *final index of OFDM/CCK swing table
  808. */
  809. s8 delta_swing_table_idx[2][15] = {
  810. {0, 0, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11},
  811. {0, 0, -1, -2, -3, -4, -4, -4, -4, -5, -7, -8, -9, -9, -10}
  812. };
  813. u8 thermal_threshold[2][15] = {
  814. {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27},
  815. {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 25, 25, 25}
  816. };
  817. /*Initilization (7 steps in total) */
  818. rtlpriv->dm.txpower_trackinginit = true;
  819. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  820. "dm_txpower_track_cb_therm\n");
  821. thermalvalue = (u8)rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER,
  822. 0xfc00);
  823. if (!thermalvalue)
  824. return;
  825. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  826. "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x\n",
  827. thermalvalue, rtlpriv->dm.thermalvalue,
  828. rtlefuse->eeprom_thermalmeter);
  829. /*1. Query OFDM Default Setting: Path A*/
  830. ele_d = rtl_get_bbreg(hw, ROFDM0_XATXIQIMBALANCE, MASKDWORD) &
  831. MASKOFDM_D;
  832. for (i = 0; i < OFDM_TABLE_LENGTH; i++) {
  833. if (ele_d == (ofdmswing_table[i] & MASKOFDM_D)) {
  834. ofdm_index_old[0] = (u8)i;
  835. rtldm->swing_idx_ofdm_base[RF90_PATH_A] = (u8)i;
  836. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  837. "Initial pathA ele_d reg0x%x = 0x%lx, ofdm_index = 0x%x\n",
  838. ROFDM0_XATXIQIMBALANCE,
  839. ele_d, ofdm_index_old[0]);
  840. break;
  841. }
  842. }
  843. /*2.Query CCK default setting From 0xa24*/
  844. temp_cck = rtl_get_bbreg(hw, RCCK0_TXFILTER2, MASKDWORD) & MASKCCK;
  845. for (i = 0; i < CCK_TABLE_LENGTH; i++) {
  846. if (rtlpriv->dm.cck_inch14) {
  847. if (memcmp(&temp_cck, &cck_tbl_ch14[i][2], 4) == 0) {
  848. cck_index_old = (u8)i;
  849. rtldm->swing_idx_cck_base = (u8)i;
  850. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  851. DBG_LOUD,
  852. "Initial reg0x%x = 0x%lx, cck_index = 0x%x, ch 14 %d\n",
  853. RCCK0_TXFILTER2, temp_cck,
  854. cck_index_old,
  855. rtlpriv->dm.cck_inch14);
  856. break;
  857. }
  858. } else {
  859. if (memcmp(&temp_cck, &cck_tbl_ch1_13[i][2], 4) == 0) {
  860. cck_index_old = (u8)i;
  861. rtldm->swing_idx_cck_base = (u8)i;
  862. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  863. DBG_LOUD,
  864. "Initial reg0x%x = 0x%lx, cck_index = 0x%x, ch14 %d\n",
  865. RCCK0_TXFILTER2, temp_cck,
  866. cck_index_old,
  867. rtlpriv->dm.cck_inch14);
  868. break;
  869. }
  870. }
  871. }
  872. /*3 Initialize ThermalValues of RFCalibrateInfo*/
  873. if (!rtldm->thermalvalue) {
  874. rtlpriv->dm.thermalvalue = rtlefuse->eeprom_thermalmeter;
  875. rtlpriv->dm.thermalvalue_lck = thermalvalue;
  876. rtlpriv->dm.thermalvalue_iqk = thermalvalue;
  877. for (i = 0; i < rf; i++)
  878. rtlpriv->dm.ofdm_index[i] = ofdm_index_old[i];
  879. rtlpriv->dm.cck_index = cck_index_old;
  880. }
  881. /*4 Calculate average thermal meter*/
  882. rtldm->thermalvalue_avg[rtldm->thermalvalue_avg_index] = thermalvalue;
  883. rtldm->thermalvalue_avg_index++;
  884. if (rtldm->thermalvalue_avg_index == AVG_THERMAL_NUM_88E)
  885. rtldm->thermalvalue_avg_index = 0;
  886. for (i = 0; i < AVG_THERMAL_NUM_88E; i++) {
  887. if (rtldm->thermalvalue_avg[i]) {
  888. thermalvalue_avg += rtldm->thermalvalue_avg[i];
  889. thermalvalue_avg_count++;
  890. }
  891. }
  892. if (thermalvalue_avg_count)
  893. thermalvalue = (u8)(thermalvalue_avg / thermalvalue_avg_count);
  894. /* 5 Calculate delta, delta_LCK, delta_IQK.*/
  895. if (rtlhal->reloadtxpowerindex) {
  896. delta = (thermalvalue > rtlefuse->eeprom_thermalmeter) ?
  897. (thermalvalue - rtlefuse->eeprom_thermalmeter) :
  898. (rtlefuse->eeprom_thermalmeter - thermalvalue);
  899. rtlhal->reloadtxpowerindex = false;
  900. rtlpriv->dm.done_txpower = false;
  901. } else if (rtlpriv->dm.done_txpower) {
  902. delta = (thermalvalue > rtlpriv->dm.thermalvalue) ?
  903. (thermalvalue - rtlpriv->dm.thermalvalue) :
  904. (rtlpriv->dm.thermalvalue - thermalvalue);
  905. } else {
  906. delta = (thermalvalue > rtlefuse->eeprom_thermalmeter) ?
  907. (thermalvalue - rtlefuse->eeprom_thermalmeter) :
  908. (rtlefuse->eeprom_thermalmeter - thermalvalue);
  909. }
  910. delta_lck = (thermalvalue > rtlpriv->dm.thermalvalue_lck) ?
  911. (thermalvalue - rtlpriv->dm.thermalvalue_lck) :
  912. (rtlpriv->dm.thermalvalue_lck - thermalvalue);
  913. delta_iqk = (thermalvalue > rtlpriv->dm.thermalvalue_iqk) ?
  914. (thermalvalue - rtlpriv->dm.thermalvalue_iqk) :
  915. (rtlpriv->dm.thermalvalue_iqk - thermalvalue);
  916. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  917. "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",
  918. thermalvalue, rtlpriv->dm.thermalvalue,
  919. rtlefuse->eeprom_thermalmeter, delta, delta_lck,
  920. delta_iqk);
  921. /* 6 If necessary, do LCK.*/
  922. if (delta_lck >= 8) {
  923. rtlpriv->dm.thermalvalue_lck = thermalvalue;
  924. rtl88e_phy_lc_calibrate(hw);
  925. }
  926. /* 7 If necessary, move the index of
  927. * swing table to adjust Tx power.
  928. */
  929. if (delta > 0 && rtlpriv->dm.txpower_track_control) {
  930. delta = (thermalvalue > rtlefuse->eeprom_thermalmeter) ?
  931. (thermalvalue - rtlefuse->eeprom_thermalmeter) :
  932. (rtlefuse->eeprom_thermalmeter - thermalvalue);
  933. /* 7.1 Get the final CCK_index and OFDM_index for each
  934. * swing table.
  935. */
  936. if (thermalvalue > rtlefuse->eeprom_thermalmeter) {
  937. CAL_SWING_OFF(offset, power_inc, INDEX_MAPPING_NUM,
  938. delta);
  939. for (i = 0; i < rf; i++)
  940. ofdm_index[i] =
  941. rtldm->ofdm_index[i] +
  942. delta_swing_table_idx[power_inc][offset];
  943. cck_index = rtldm->cck_index +
  944. delta_swing_table_idx[power_inc][offset];
  945. } else {
  946. CAL_SWING_OFF(offset, power_dec, INDEX_MAPPING_NUM,
  947. delta);
  948. for (i = 0; i < rf; i++)
  949. ofdm_index[i] =
  950. rtldm->ofdm_index[i] +
  951. delta_swing_table_idx[power_dec][offset];
  952. cck_index = rtldm->cck_index +
  953. delta_swing_table_idx[power_dec][offset];
  954. }
  955. /* 7.2 Handle boundary conditions of index.*/
  956. for (i = 0; i < rf; i++) {
  957. if (ofdm_index[i] > OFDM_TABLE_SIZE-1)
  958. ofdm_index[i] = OFDM_TABLE_SIZE-1;
  959. else if (rtldm->ofdm_index[i] < ofdm_min_index)
  960. ofdm_index[i] = ofdm_min_index;
  961. }
  962. if (cck_index > CCK_TABLE_SIZE-1)
  963. cck_index = CCK_TABLE_SIZE-1;
  964. else if (cck_index < 0)
  965. cck_index = 0;
  966. /*7.3Configure the Swing Table to adjust Tx Power.*/
  967. if (rtlpriv->dm.txpower_track_control) {
  968. rtldm->done_txpower = true;
  969. rtldm->swing_idx_ofdm[RF90_PATH_A] =
  970. (u8)ofdm_index[RF90_PATH_A];
  971. rtldm->swing_idx_cck = cck_index;
  972. if (rtldm->swing_idx_ofdm_cur !=
  973. rtldm->swing_idx_ofdm[0]) {
  974. rtldm->swing_idx_ofdm_cur =
  975. rtldm->swing_idx_ofdm[0];
  976. rtldm->swing_flag_ofdm = true;
  977. }
  978. if (rtldm->swing_idx_cck_cur != rtldm->swing_idx_cck) {
  979. rtldm->swing_idx_cck_cur = rtldm->swing_idx_cck;
  980. rtldm->swing_flag_cck = true;
  981. }
  982. dm_tx_pwr_track_set_pwr(hw, TXAGC, 0, 0);
  983. }
  984. }
  985. if (delta_iqk >= 8) {
  986. rtlpriv->dm.thermalvalue_iqk = thermalvalue;
  987. rtl88e_phy_iq_calibrate(hw, false);
  988. }
  989. if (rtldm->txpower_track_control)
  990. rtldm->thermalvalue = thermalvalue;
  991. rtldm->txpowercount = 0;
  992. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD, "end\n");
  993. }
  994. static void rtl88e_dm_init_txpower_tracking(struct ieee80211_hw *hw)
  995. {
  996. struct rtl_priv *rtlpriv = rtl_priv(hw);
  997. rtlpriv->dm.txpower_tracking = true;
  998. rtlpriv->dm.txpower_trackinginit = false;
  999. rtlpriv->dm.txpowercount = 0;
  1000. rtlpriv->dm.txpower_track_control = true;
  1001. rtlpriv->dm.swing_idx_ofdm[RF90_PATH_A] = 12;
  1002. rtlpriv->dm.swing_idx_ofdm_cur = 12;
  1003. rtlpriv->dm.swing_flag_ofdm = false;
  1004. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1005. "rtlpriv->dm.txpower_tracking = %d\n",
  1006. rtlpriv->dm.txpower_tracking);
  1007. }
  1008. void rtl88e_dm_check_txpower_tracking(struct ieee80211_hw *hw)
  1009. {
  1010. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1011. if (!rtlpriv->dm.txpower_tracking)
  1012. return;
  1013. if (!rtlpriv->dm.tm_trigger) {
  1014. rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER, BIT(17)|BIT(16),
  1015. 0x03);
  1016. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1017. "Trigger 88E Thermal Meter!!\n");
  1018. rtlpriv->dm.tm_trigger = 1;
  1019. return;
  1020. } else {
  1021. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1022. "Schedule TxPowerTracking !!\n");
  1023. dm_txpower_track_cb_therm(hw);
  1024. rtlpriv->dm.tm_trigger = 0;
  1025. }
  1026. }
  1027. void rtl88e_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
  1028. {
  1029. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1030. struct rate_adaptive *p_ra = &rtlpriv->ra;
  1031. p_ra->ratr_state = DM_RATR_STA_INIT;
  1032. p_ra->pre_ratr_state = DM_RATR_STA_INIT;
  1033. if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER)
  1034. rtlpriv->dm.useramask = true;
  1035. else
  1036. rtlpriv->dm.useramask = false;
  1037. }
  1038. static void rtl88e_dm_refresh_rate_adaptive_mask(struct ieee80211_hw *hw)
  1039. {
  1040. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1041. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1042. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1043. struct rate_adaptive *p_ra = &rtlpriv->ra;
  1044. u32 low_rssithresh_for_ra, high_rssithresh_for_ra;
  1045. struct ieee80211_sta *sta = NULL;
  1046. if (is_hal_stop(rtlhal)) {
  1047. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  1048. "driver is going to unload\n");
  1049. return;
  1050. }
  1051. if (!rtlpriv->dm.useramask) {
  1052. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  1053. "driver does not control rate adaptive mask\n");
  1054. return;
  1055. }
  1056. if (mac->link_state == MAC80211_LINKED &&
  1057. mac->opmode == NL80211_IFTYPE_STATION) {
  1058. switch (p_ra->pre_ratr_state) {
  1059. case DM_RATR_STA_HIGH:
  1060. high_rssithresh_for_ra = 50;
  1061. low_rssithresh_for_ra = 20;
  1062. break;
  1063. case DM_RATR_STA_MIDDLE:
  1064. high_rssithresh_for_ra = 55;
  1065. low_rssithresh_for_ra = 20;
  1066. break;
  1067. case DM_RATR_STA_LOW:
  1068. high_rssithresh_for_ra = 50;
  1069. low_rssithresh_for_ra = 25;
  1070. break;
  1071. default:
  1072. high_rssithresh_for_ra = 50;
  1073. low_rssithresh_for_ra = 20;
  1074. break;
  1075. }
  1076. if (rtlpriv->dm.undec_sm_pwdb >
  1077. (long)high_rssithresh_for_ra)
  1078. p_ra->ratr_state = DM_RATR_STA_HIGH;
  1079. else if (rtlpriv->dm.undec_sm_pwdb >
  1080. (long)low_rssithresh_for_ra)
  1081. p_ra->ratr_state = DM_RATR_STA_MIDDLE;
  1082. else
  1083. p_ra->ratr_state = DM_RATR_STA_LOW;
  1084. if (p_ra->pre_ratr_state != p_ra->ratr_state) {
  1085. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  1086. "RSSI = %ld\n",
  1087. rtlpriv->dm.undec_sm_pwdb);
  1088. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  1089. "RSSI_LEVEL = %d\n", p_ra->ratr_state);
  1090. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  1091. "PreState = %d, CurState = %d\n",
  1092. p_ra->pre_ratr_state, p_ra->ratr_state);
  1093. rcu_read_lock();
  1094. sta = rtl_find_sta(hw, mac->bssid);
  1095. if (sta)
  1096. rtlpriv->cfg->ops->update_rate_tbl(hw, sta,
  1097. p_ra->ratr_state,
  1098. true);
  1099. rcu_read_unlock();
  1100. p_ra->pre_ratr_state = p_ra->ratr_state;
  1101. }
  1102. }
  1103. }
  1104. static void rtl92c_dm_init_dynamic_bb_powersaving(struct ieee80211_hw *hw)
  1105. {
  1106. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1107. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1108. dm_pstable->pre_ccastate = CCA_MAX;
  1109. dm_pstable->cur_ccasate = CCA_MAX;
  1110. dm_pstable->pre_rfstate = RF_MAX;
  1111. dm_pstable->cur_rfstate = RF_MAX;
  1112. dm_pstable->rssi_val_min = 0;
  1113. }
  1114. static void rtl88e_dm_update_rx_idle_ant(struct ieee80211_hw *hw,
  1115. u8 ant)
  1116. {
  1117. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1118. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1119. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1120. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1121. u32 default_ant, optional_ant;
  1122. if (pfat_table->rx_idle_ant != ant) {
  1123. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1124. "need to update rx idle ant\n");
  1125. if (ant == MAIN_ANT) {
  1126. default_ant =
  1127. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1128. MAIN_ANT_CG_TRX : MAIN_ANT_CGCS_RX;
  1129. optional_ant =
  1130. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1131. AUX_ANT_CG_TRX : AUX_ANT_CGCS_RX;
  1132. } else {
  1133. default_ant =
  1134. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1135. AUX_ANT_CG_TRX : AUX_ANT_CGCS_RX;
  1136. optional_ant =
  1137. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1138. MAIN_ANT_CG_TRX : MAIN_ANT_CGCS_RX;
  1139. }
  1140. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
  1141. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1142. BIT(5) | BIT(4) | BIT(3), default_ant);
  1143. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1144. BIT(8) | BIT(7) | BIT(6), optional_ant);
  1145. rtl_set_bbreg(hw, DM_REG_ANTSEL_CTRL_11N,
  1146. BIT(14) | BIT(13) | BIT(12),
  1147. default_ant);
  1148. rtl_set_bbreg(hw, DM_REG_RESP_TX_11N,
  1149. BIT(6) | BIT(7), default_ant);
  1150. } else if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV) {
  1151. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1152. BIT(5) | BIT(4) | BIT(3), default_ant);
  1153. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1154. BIT(8) | BIT(7) | BIT(6), optional_ant);
  1155. }
  1156. }
  1157. pfat_table->rx_idle_ant = ant;
  1158. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "RxIdleAnt %s\n",
  1159. (ant == MAIN_ANT) ? ("MAIN_ANT") : ("AUX_ANT"));
  1160. }
  1161. static void rtl88e_dm_update_tx_ant(struct ieee80211_hw *hw,
  1162. u8 ant, u32 mac_id)
  1163. {
  1164. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1165. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1166. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1167. u8 target_ant;
  1168. if (ant == MAIN_ANT)
  1169. target_ant = MAIN_ANT_CG_TRX;
  1170. else
  1171. target_ant = AUX_ANT_CG_TRX;
  1172. pfat_table->antsel_a[mac_id] = target_ant & BIT(0);
  1173. pfat_table->antsel_b[mac_id] = (target_ant & BIT(1)) >> 1;
  1174. pfat_table->antsel_c[mac_id] = (target_ant & BIT(2)) >> 2;
  1175. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "txfrominfo target ant %s\n",
  1176. (ant == MAIN_ANT) ? ("MAIN_ANT") : ("AUX_ANT"));
  1177. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "antsel_tr_mux = 3'b%d%d%d\n",
  1178. pfat_table->antsel_c[mac_id],
  1179. pfat_table->antsel_b[mac_id],
  1180. pfat_table->antsel_a[mac_id]);
  1181. }
  1182. static void rtl88e_dm_rx_hw_antena_div_init(struct ieee80211_hw *hw)
  1183. {
  1184. u32 value32;
  1185. /*MAC Setting*/
  1186. value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
  1187. rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N,
  1188. MASKDWORD, value32 | (BIT(23) | BIT(25)));
  1189. /*Pin Setting*/
  1190. rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
  1191. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
  1192. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 1);
  1193. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
  1194. /*OFDM Setting*/
  1195. rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
  1196. /*CCK Setting*/
  1197. rtl_set_bbreg(hw, DM_REG_BB_PWR_SAV4_11N, BIT(7), 1);
  1198. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA2_11N, BIT(4), 1);
  1199. rtl88e_dm_update_rx_idle_ant(hw, MAIN_ANT);
  1200. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKLWORD, 0x0201);
  1201. }
  1202. static void rtl88e_dm_trx_hw_antenna_div_init(struct ieee80211_hw *hw)
  1203. {
  1204. u32 value32;
  1205. /*MAC Setting*/
  1206. value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
  1207. rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD,
  1208. value32 | (BIT(23) | BIT(25)));
  1209. /*Pin Setting*/
  1210. rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
  1211. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
  1212. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 0);
  1213. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
  1214. /*OFDM Setting*/
  1215. rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
  1216. /*CCK Setting*/
  1217. rtl_set_bbreg(hw, DM_REG_BB_PWR_SAV4_11N, BIT(7), 1);
  1218. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA2_11N, BIT(4), 1);
  1219. /*TX Setting*/
  1220. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N, BIT(21), 0);
  1221. rtl88e_dm_update_rx_idle_ant(hw, MAIN_ANT);
  1222. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKLWORD, 0x0201);
  1223. }
  1224. static void rtl88e_dm_fast_training_init(struct ieee80211_hw *hw)
  1225. {
  1226. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1227. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1228. u32 ant_combination = 2;
  1229. u32 value32, i;
  1230. for (i = 0; i < 6; i++) {
  1231. pfat_table->bssid[i] = 0;
  1232. pfat_table->ant_sum[i] = 0;
  1233. pfat_table->ant_cnt[i] = 0;
  1234. pfat_table->ant_ave[i] = 0;
  1235. }
  1236. pfat_table->train_idx = 0;
  1237. pfat_table->fat_state = FAT_NORMAL_STATE;
  1238. /*MAC Setting*/
  1239. value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
  1240. rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N,
  1241. MASKDWORD, value32 | (BIT(23) | BIT(25)));
  1242. value32 = rtl_get_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N, MASKDWORD);
  1243. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
  1244. MASKDWORD, value32 | (BIT(16) | BIT(17)));
  1245. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
  1246. MASKLWORD, 0);
  1247. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA1_11N,
  1248. MASKDWORD, 0);
  1249. /*Pin Setting*/
  1250. rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
  1251. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
  1252. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 0);
  1253. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
  1254. /*OFDM Setting*/
  1255. rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
  1256. /*antenna mapping table*/
  1257. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKBYTE0, 1);
  1258. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKBYTE1, 2);
  1259. /*TX Setting*/
  1260. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N, BIT(21), 1);
  1261. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1262. BIT(5) | BIT(4) | BIT(3), 0);
  1263. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1264. BIT(8) | BIT(7) | BIT(6), 1);
  1265. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1266. BIT(2) | BIT(1) | BIT(0), (ant_combination - 1));
  1267. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
  1268. }
  1269. static void rtl88e_dm_antenna_div_init(struct ieee80211_hw *hw)
  1270. {
  1271. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1272. if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV)
  1273. rtl88e_dm_rx_hw_antena_div_init(hw);
  1274. else if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1275. rtl88e_dm_trx_hw_antenna_div_init(hw);
  1276. else if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)
  1277. rtl88e_dm_fast_training_init(hw);
  1278. }
  1279. void rtl88e_dm_set_tx_ant_by_tx_info(struct ieee80211_hw *hw,
  1280. u8 *pdesc, u32 mac_id)
  1281. {
  1282. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1283. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1284. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1285. if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
  1286. (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)) {
  1287. SET_TX_DESC_ANTSEL_A(pdesc, pfat_table->antsel_a[mac_id]);
  1288. SET_TX_DESC_ANTSEL_B(pdesc, pfat_table->antsel_b[mac_id]);
  1289. SET_TX_DESC_ANTSEL_C(pdesc, pfat_table->antsel_c[mac_id]);
  1290. }
  1291. }
  1292. void rtl88e_dm_ant_sel_statistics(struct ieee80211_hw *hw,
  1293. u8 antsel_tr_mux, u32 mac_id,
  1294. u32 rx_pwdb_all)
  1295. {
  1296. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1297. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1298. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1299. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
  1300. if (antsel_tr_mux == MAIN_ANT_CG_TRX) {
  1301. pfat_table->main_ant_sum[mac_id] += rx_pwdb_all;
  1302. pfat_table->main_ant_cnt[mac_id]++;
  1303. } else {
  1304. pfat_table->aux_ant_sum[mac_id] += rx_pwdb_all;
  1305. pfat_table->aux_ant_cnt[mac_id]++;
  1306. }
  1307. } else if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV) {
  1308. if (antsel_tr_mux == MAIN_ANT_CGCS_RX) {
  1309. pfat_table->main_ant_sum[mac_id] += rx_pwdb_all;
  1310. pfat_table->main_ant_cnt[mac_id]++;
  1311. } else {
  1312. pfat_table->aux_ant_sum[mac_id] += rx_pwdb_all;
  1313. pfat_table->aux_ant_cnt[mac_id]++;
  1314. }
  1315. }
  1316. }
  1317. static void rtl88e_dm_hw_ant_div(struct ieee80211_hw *hw)
  1318. {
  1319. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1320. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1321. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1322. struct rtl_sta_info *drv_priv;
  1323. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1324. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  1325. u32 i, min_rssi = 0xff, ant_div_max_rssi = 0;
  1326. u32 max_rssi = 0, local_min_rssi, local_max_rssi;
  1327. u32 main_rssi, aux_rssi;
  1328. u8 rx_idle_ant = 0, target_ant = 7;
  1329. /*for sta its self*/
  1330. i = 0;
  1331. main_rssi = (pfat_table->main_ant_cnt[i] != 0) ?
  1332. (pfat_table->main_ant_sum[i] / pfat_table->main_ant_cnt[i]) : 0;
  1333. aux_rssi = (pfat_table->aux_ant_cnt[i] != 0) ?
  1334. (pfat_table->aux_ant_sum[i] / pfat_table->aux_ant_cnt[i]) : 0;
  1335. target_ant = (main_rssi == aux_rssi) ?
  1336. pfat_table->rx_idle_ant : ((main_rssi >= aux_rssi) ?
  1337. MAIN_ANT : AUX_ANT);
  1338. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1339. "main_ant_sum %d main_ant_cnt %d\n",
  1340. pfat_table->main_ant_sum[i],
  1341. pfat_table->main_ant_cnt[i]);
  1342. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1343. "aux_ant_sum %d aux_ant_cnt %d\n",
  1344. pfat_table->aux_ant_sum[i], pfat_table->aux_ant_cnt[i]);
  1345. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "main_rssi %d aux_rssi%d\n",
  1346. main_rssi, aux_rssi);
  1347. local_max_rssi = (main_rssi > aux_rssi) ? main_rssi : aux_rssi;
  1348. if ((local_max_rssi > ant_div_max_rssi) && (local_max_rssi < 40))
  1349. ant_div_max_rssi = local_max_rssi;
  1350. if (local_max_rssi > max_rssi)
  1351. max_rssi = local_max_rssi;
  1352. if ((pfat_table->rx_idle_ant == MAIN_ANT) && (main_rssi == 0))
  1353. main_rssi = aux_rssi;
  1354. else if ((pfat_table->rx_idle_ant == AUX_ANT) && (aux_rssi == 0))
  1355. aux_rssi = main_rssi;
  1356. local_min_rssi = (main_rssi > aux_rssi) ? aux_rssi : main_rssi;
  1357. if (local_min_rssi < min_rssi) {
  1358. min_rssi = local_min_rssi;
  1359. rx_idle_ant = target_ant;
  1360. }
  1361. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1362. rtl88e_dm_update_tx_ant(hw, target_ant, i);
  1363. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP ||
  1364. rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC) {
  1365. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  1366. list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
  1367. i++;
  1368. main_rssi = (pfat_table->main_ant_cnt[i] != 0) ?
  1369. (pfat_table->main_ant_sum[i] /
  1370. pfat_table->main_ant_cnt[i]) : 0;
  1371. aux_rssi = (pfat_table->aux_ant_cnt[i] != 0) ?
  1372. (pfat_table->aux_ant_sum[i] /
  1373. pfat_table->aux_ant_cnt[i]) : 0;
  1374. target_ant = (main_rssi == aux_rssi) ?
  1375. pfat_table->rx_idle_ant : ((main_rssi >=
  1376. aux_rssi) ? MAIN_ANT : AUX_ANT);
  1377. local_max_rssi = (main_rssi > aux_rssi) ?
  1378. main_rssi : aux_rssi;
  1379. if ((local_max_rssi > ant_div_max_rssi) &&
  1380. (local_max_rssi < 40))
  1381. ant_div_max_rssi = local_max_rssi;
  1382. if (local_max_rssi > max_rssi)
  1383. max_rssi = local_max_rssi;
  1384. if ((pfat_table->rx_idle_ant == MAIN_ANT) &&
  1385. (main_rssi == 0))
  1386. main_rssi = aux_rssi;
  1387. else if ((pfat_table->rx_idle_ant == AUX_ANT) &&
  1388. (aux_rssi == 0))
  1389. aux_rssi = main_rssi;
  1390. local_min_rssi = (main_rssi > aux_rssi) ?
  1391. aux_rssi : main_rssi;
  1392. if (local_min_rssi < min_rssi) {
  1393. min_rssi = local_min_rssi;
  1394. rx_idle_ant = target_ant;
  1395. }
  1396. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1397. rtl88e_dm_update_tx_ant(hw, target_ant, i);
  1398. }
  1399. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  1400. }
  1401. for (i = 0; i < ASSOCIATE_ENTRY_NUM; i++) {
  1402. pfat_table->main_ant_sum[i] = 0;
  1403. pfat_table->aux_ant_sum[i] = 0;
  1404. pfat_table->main_ant_cnt[i] = 0;
  1405. pfat_table->aux_ant_cnt[i] = 0;
  1406. }
  1407. rtl88e_dm_update_rx_idle_ant(hw, rx_idle_ant);
  1408. dm_dig->antdiv_rssi_max = ant_div_max_rssi;
  1409. dm_dig->rssi_max = max_rssi;
  1410. }
  1411. static void rtl88e_set_next_mac_address_target(struct ieee80211_hw *hw)
  1412. {
  1413. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1414. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1415. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1416. struct rtl_sta_info *drv_priv;
  1417. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1418. u32 value32, i, j = 0;
  1419. if (mac->link_state >= MAC80211_LINKED) {
  1420. for (i = 0; i < ASSOCIATE_ENTRY_NUM; i++) {
  1421. if ((pfat_table->train_idx + 1) == ASSOCIATE_ENTRY_NUM)
  1422. pfat_table->train_idx = 0;
  1423. else
  1424. pfat_table->train_idx++;
  1425. if (pfat_table->train_idx == 0) {
  1426. value32 = (mac->mac_addr[5] << 8) |
  1427. mac->mac_addr[4];
  1428. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
  1429. MASKLWORD, value32);
  1430. value32 = (mac->mac_addr[3] << 24) |
  1431. (mac->mac_addr[2] << 16) |
  1432. (mac->mac_addr[1] << 8) |
  1433. mac->mac_addr[0];
  1434. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA1_11N,
  1435. MASKDWORD, value32);
  1436. break;
  1437. }
  1438. if (rtlpriv->mac80211.opmode !=
  1439. NL80211_IFTYPE_STATION) {
  1440. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  1441. list_for_each_entry(drv_priv,
  1442. &rtlpriv->entry_list, list) {
  1443. j++;
  1444. if (j != pfat_table->train_idx)
  1445. continue;
  1446. value32 = (drv_priv->mac_addr[5] << 8) |
  1447. drv_priv->mac_addr[4];
  1448. rtl_set_bbreg(hw,
  1449. DM_REG_ANT_TRAIN_PARA2_11N,
  1450. MASKLWORD, value32);
  1451. value32 = (drv_priv->mac_addr[3] << 24) |
  1452. (drv_priv->mac_addr[2] << 16) |
  1453. (drv_priv->mac_addr[1] << 8) |
  1454. drv_priv->mac_addr[0];
  1455. rtl_set_bbreg(hw,
  1456. DM_REG_ANT_TRAIN_PARA1_11N,
  1457. MASKDWORD, value32);
  1458. break;
  1459. }
  1460. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  1461. /*find entry, break*/
  1462. if (j == pfat_table->train_idx)
  1463. break;
  1464. }
  1465. }
  1466. }
  1467. }
  1468. static void rtl88e_dm_fast_ant_training(struct ieee80211_hw *hw)
  1469. {
  1470. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1471. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1472. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1473. u32 i, max_rssi = 0;
  1474. u8 target_ant = 2;
  1475. bool bpkt_filter_match = false;
  1476. if (pfat_table->fat_state == FAT_TRAINING_STATE) {
  1477. for (i = 0; i < 7; i++) {
  1478. if (pfat_table->ant_cnt[i] == 0) {
  1479. pfat_table->ant_ave[i] = 0;
  1480. } else {
  1481. pfat_table->ant_ave[i] =
  1482. pfat_table->ant_sum[i] /
  1483. pfat_table->ant_cnt[i];
  1484. bpkt_filter_match = true;
  1485. }
  1486. if (pfat_table->ant_ave[i] > max_rssi) {
  1487. max_rssi = pfat_table->ant_ave[i];
  1488. target_ant = (u8) i;
  1489. }
  1490. }
  1491. if (bpkt_filter_match == false) {
  1492. rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N,
  1493. BIT(16), 0);
  1494. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
  1495. } else {
  1496. rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N,
  1497. BIT(16), 0);
  1498. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(8) |
  1499. BIT(7) | BIT(6), target_ant);
  1500. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
  1501. BIT(21), 1);
  1502. pfat_table->antsel_a[pfat_table->train_idx] =
  1503. target_ant & BIT(0);
  1504. pfat_table->antsel_b[pfat_table->train_idx] =
  1505. (target_ant & BIT(1)) >> 1;
  1506. pfat_table->antsel_c[pfat_table->train_idx] =
  1507. (target_ant & BIT(2)) >> 2;
  1508. if (target_ant == 0)
  1509. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
  1510. }
  1511. for (i = 0; i < 7; i++) {
  1512. pfat_table->ant_sum[i] = 0;
  1513. pfat_table->ant_cnt[i] = 0;
  1514. }
  1515. pfat_table->fat_state = FAT_NORMAL_STATE;
  1516. return;
  1517. }
  1518. if (pfat_table->fat_state == FAT_NORMAL_STATE) {
  1519. rtl88e_set_next_mac_address_target(hw);
  1520. pfat_table->fat_state = FAT_TRAINING_STATE;
  1521. rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N, BIT(16), 1);
  1522. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
  1523. mod_timer(&rtlpriv->works.fast_antenna_training_timer,
  1524. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  1525. }
  1526. }
  1527. void rtl88e_dm_fast_antenna_training_callback(struct timer_list *t)
  1528. {
  1529. struct rtl_priv *rtlpriv =
  1530. from_timer(rtlpriv, t, works.fast_antenna_training_timer);
  1531. struct ieee80211_hw *hw = rtlpriv->hw;
  1532. rtl88e_dm_fast_ant_training(hw);
  1533. }
  1534. static void rtl88e_dm_antenna_diversity(struct ieee80211_hw *hw)
  1535. {
  1536. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1537. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1538. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1539. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1540. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1541. if (mac->link_state < MAC80211_LINKED) {
  1542. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD, "No Link\n");
  1543. if (pfat_table->becomelinked) {
  1544. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  1545. "need to turn off HW AntDiv\n");
  1546. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
  1547. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA1_11N,
  1548. BIT(15), 0);
  1549. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1550. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
  1551. BIT(21), 0);
  1552. pfat_table->becomelinked =
  1553. (mac->link_state == MAC80211_LINKED) ?
  1554. true : false;
  1555. }
  1556. return;
  1557. } else {
  1558. if (!pfat_table->becomelinked) {
  1559. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  1560. "Need to turn on HW AntDiv\n");
  1561. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
  1562. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA1_11N,
  1563. BIT(15), 1);
  1564. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1565. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
  1566. BIT(21), 1);
  1567. pfat_table->becomelinked =
  1568. (mac->link_state >= MAC80211_LINKED) ?
  1569. true : false;
  1570. }
  1571. }
  1572. if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
  1573. (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV))
  1574. rtl88e_dm_hw_ant_div(hw);
  1575. else if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)
  1576. rtl88e_dm_fast_ant_training(hw);
  1577. }
  1578. void rtl88e_dm_init(struct ieee80211_hw *hw)
  1579. {
  1580. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1581. u32 cur_igvalue = rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f);
  1582. rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
  1583. rtl_dm_diginit(hw, cur_igvalue);
  1584. rtl88e_dm_init_dynamic_txpower(hw);
  1585. rtl88e_dm_init_edca_turbo(hw);
  1586. rtl88e_dm_init_rate_adaptive_mask(hw);
  1587. rtl88e_dm_init_txpower_tracking(hw);
  1588. rtl92c_dm_init_dynamic_bb_powersaving(hw);
  1589. rtl88e_dm_antenna_div_init(hw);
  1590. }
  1591. void rtl88e_dm_watchdog(struct ieee80211_hw *hw)
  1592. {
  1593. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1594. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1595. bool fw_current_inpsmode = false;
  1596. bool fw_ps_awake = true;
  1597. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
  1598. (u8 *)(&fw_current_inpsmode));
  1599. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FWLPS_RF_ON,
  1600. (u8 *)(&fw_ps_awake));
  1601. if (ppsc->p2p_ps_info.p2p_ps_mode)
  1602. fw_ps_awake = false;
  1603. spin_lock(&rtlpriv->locks.rf_ps_lock);
  1604. if ((ppsc->rfpwr_state == ERFON) &&
  1605. ((!fw_current_inpsmode) && fw_ps_awake) &&
  1606. (!ppsc->rfchange_inprogress)) {
  1607. rtl88e_dm_pwdb_monitor(hw);
  1608. rtl88e_dm_dig(hw);
  1609. rtl88e_dm_false_alarm_counter_statistics(hw);
  1610. rtl92c_dm_dynamic_txpower(hw);
  1611. rtl88e_dm_check_txpower_tracking(hw);
  1612. rtl88e_dm_refresh_rate_adaptive_mask(hw);
  1613. rtl88e_dm_check_edca_turbo(hw);
  1614. rtl88e_dm_antenna_diversity(hw);
  1615. }
  1616. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  1617. }