dm.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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. char 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. char 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. rcu_read_unlock();
  1099. p_ra->pre_ratr_state = p_ra->ratr_state;
  1100. }
  1101. }
  1102. }
  1103. static void rtl92c_dm_init_dynamic_bb_powersaving(struct ieee80211_hw *hw)
  1104. {
  1105. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1106. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1107. dm_pstable->pre_ccastate = CCA_MAX;
  1108. dm_pstable->cur_ccasate = CCA_MAX;
  1109. dm_pstable->pre_rfstate = RF_MAX;
  1110. dm_pstable->cur_rfstate = RF_MAX;
  1111. dm_pstable->rssi_val_min = 0;
  1112. }
  1113. static void rtl88e_dm_update_rx_idle_ant(struct ieee80211_hw *hw,
  1114. u8 ant)
  1115. {
  1116. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1117. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1118. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1119. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1120. u32 default_ant, optional_ant;
  1121. if (pfat_table->rx_idle_ant != ant) {
  1122. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1123. "need to update rx idle ant\n");
  1124. if (ant == MAIN_ANT) {
  1125. default_ant =
  1126. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1127. MAIN_ANT_CG_TRX : MAIN_ANT_CGCS_RX;
  1128. optional_ant =
  1129. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1130. AUX_ANT_CG_TRX : AUX_ANT_CGCS_RX;
  1131. } else {
  1132. default_ant =
  1133. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1134. AUX_ANT_CG_TRX : AUX_ANT_CGCS_RX;
  1135. optional_ant =
  1136. (pfat_table->rx_idle_ant == CG_TRX_HW_ANTDIV) ?
  1137. MAIN_ANT_CG_TRX : MAIN_ANT_CGCS_RX;
  1138. }
  1139. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
  1140. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1141. BIT(5) | BIT(4) | BIT(3), default_ant);
  1142. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1143. BIT(8) | BIT(7) | BIT(6), optional_ant);
  1144. rtl_set_bbreg(hw, DM_REG_ANTSEL_CTRL_11N,
  1145. BIT(14) | BIT(13) | BIT(12),
  1146. default_ant);
  1147. rtl_set_bbreg(hw, DM_REG_RESP_TX_11N,
  1148. BIT(6) | BIT(7), default_ant);
  1149. } else if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV) {
  1150. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1151. BIT(5) | BIT(4) | BIT(3), default_ant);
  1152. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1153. BIT(8) | BIT(7) | BIT(6), optional_ant);
  1154. }
  1155. }
  1156. pfat_table->rx_idle_ant = ant;
  1157. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "RxIdleAnt %s\n",
  1158. (ant == MAIN_ANT) ? ("MAIN_ANT") : ("AUX_ANT"));
  1159. }
  1160. static void rtl88e_dm_update_tx_ant(struct ieee80211_hw *hw,
  1161. u8 ant, u32 mac_id)
  1162. {
  1163. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1164. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1165. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1166. u8 target_ant;
  1167. if (ant == MAIN_ANT)
  1168. target_ant = MAIN_ANT_CG_TRX;
  1169. else
  1170. target_ant = AUX_ANT_CG_TRX;
  1171. pfat_table->antsel_a[mac_id] = target_ant & BIT(0);
  1172. pfat_table->antsel_b[mac_id] = (target_ant & BIT(1)) >> 1;
  1173. pfat_table->antsel_c[mac_id] = (target_ant & BIT(2)) >> 2;
  1174. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "txfrominfo target ant %s\n",
  1175. (ant == MAIN_ANT) ? ("MAIN_ANT") : ("AUX_ANT"));
  1176. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "antsel_tr_mux = 3'b%d%d%d\n",
  1177. pfat_table->antsel_c[mac_id],
  1178. pfat_table->antsel_b[mac_id],
  1179. pfat_table->antsel_a[mac_id]);
  1180. }
  1181. static void rtl88e_dm_rx_hw_antena_div_init(struct ieee80211_hw *hw)
  1182. {
  1183. u32 value32;
  1184. /*MAC Setting*/
  1185. value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
  1186. rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N,
  1187. MASKDWORD, value32 | (BIT(23) | BIT(25)));
  1188. /*Pin Setting*/
  1189. rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
  1190. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
  1191. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 1);
  1192. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
  1193. /*OFDM Setting*/
  1194. rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
  1195. /*CCK Setting*/
  1196. rtl_set_bbreg(hw, DM_REG_BB_PWR_SAV4_11N, BIT(7), 1);
  1197. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA2_11N, BIT(4), 1);
  1198. rtl88e_dm_update_rx_idle_ant(hw, MAIN_ANT);
  1199. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKLWORD, 0x0201);
  1200. }
  1201. static void rtl88e_dm_trx_hw_antenna_div_init(struct ieee80211_hw *hw)
  1202. {
  1203. u32 value32;
  1204. /*MAC Setting*/
  1205. value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
  1206. rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD,
  1207. value32 | (BIT(23) | BIT(25)));
  1208. /*Pin Setting*/
  1209. rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
  1210. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
  1211. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 0);
  1212. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
  1213. /*OFDM Setting*/
  1214. rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
  1215. /*CCK Setting*/
  1216. rtl_set_bbreg(hw, DM_REG_BB_PWR_SAV4_11N, BIT(7), 1);
  1217. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA2_11N, BIT(4), 1);
  1218. /*TX Setting*/
  1219. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N, BIT(21), 0);
  1220. rtl88e_dm_update_rx_idle_ant(hw, MAIN_ANT);
  1221. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKLWORD, 0x0201);
  1222. }
  1223. static void rtl88e_dm_fast_training_init(struct ieee80211_hw *hw)
  1224. {
  1225. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1226. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1227. u32 ant_combination = 2;
  1228. u32 value32, i;
  1229. for (i = 0; i < 6; i++) {
  1230. pfat_table->bssid[i] = 0;
  1231. pfat_table->ant_sum[i] = 0;
  1232. pfat_table->ant_cnt[i] = 0;
  1233. pfat_table->ant_ave[i] = 0;
  1234. }
  1235. pfat_table->train_idx = 0;
  1236. pfat_table->fat_state = FAT_NORMAL_STATE;
  1237. /*MAC Setting*/
  1238. value32 = rtl_get_bbreg(hw, DM_REG_ANTSEL_PIN_11N, MASKDWORD);
  1239. rtl_set_bbreg(hw, DM_REG_ANTSEL_PIN_11N,
  1240. MASKDWORD, value32 | (BIT(23) | BIT(25)));
  1241. value32 = rtl_get_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N, MASKDWORD);
  1242. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
  1243. MASKDWORD, value32 | (BIT(16) | BIT(17)));
  1244. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
  1245. MASKLWORD, 0);
  1246. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA1_11N,
  1247. MASKDWORD, 0);
  1248. /*Pin Setting*/
  1249. rtl_set_bbreg(hw, DM_REG_PIN_CTRL_11N, BIT(9) | BIT(8), 0);
  1250. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(10), 0);
  1251. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(22), 0);
  1252. rtl_set_bbreg(hw, DM_REG_LNA_SWITCH_11N, BIT(31), 1);
  1253. /*OFDM Setting*/
  1254. rtl_set_bbreg(hw, DM_REG_ANTDIV_PARA1_11N, MASKDWORD, 0x000000a0);
  1255. /*antenna mapping table*/
  1256. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKBYTE0, 1);
  1257. rtl_set_bbreg(hw, DM_REG_ANT_MAPPING1_11N, MASKBYTE1, 2);
  1258. /*TX Setting*/
  1259. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N, BIT(21), 1);
  1260. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1261. BIT(5) | BIT(4) | BIT(3), 0);
  1262. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1263. BIT(8) | BIT(7) | BIT(6), 1);
  1264. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N,
  1265. BIT(2) | BIT(1) | BIT(0), (ant_combination - 1));
  1266. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
  1267. }
  1268. static void rtl88e_dm_antenna_div_init(struct ieee80211_hw *hw)
  1269. {
  1270. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1271. if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV)
  1272. rtl88e_dm_rx_hw_antena_div_init(hw);
  1273. else if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1274. rtl88e_dm_trx_hw_antenna_div_init(hw);
  1275. else if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)
  1276. rtl88e_dm_fast_training_init(hw);
  1277. }
  1278. void rtl88e_dm_set_tx_ant_by_tx_info(struct ieee80211_hw *hw,
  1279. u8 *pdesc, u32 mac_id)
  1280. {
  1281. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1282. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1283. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1284. if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
  1285. (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)) {
  1286. SET_TX_DESC_ANTSEL_A(pdesc, pfat_table->antsel_a[mac_id]);
  1287. SET_TX_DESC_ANTSEL_B(pdesc, pfat_table->antsel_b[mac_id]);
  1288. SET_TX_DESC_ANTSEL_C(pdesc, pfat_table->antsel_c[mac_id]);
  1289. }
  1290. }
  1291. void rtl88e_dm_ant_sel_statistics(struct ieee80211_hw *hw,
  1292. u8 antsel_tr_mux, u32 mac_id,
  1293. u32 rx_pwdb_all)
  1294. {
  1295. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1296. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1297. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1298. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) {
  1299. if (antsel_tr_mux == MAIN_ANT_CG_TRX) {
  1300. pfat_table->main_ant_sum[mac_id] += rx_pwdb_all;
  1301. pfat_table->main_ant_cnt[mac_id]++;
  1302. } else {
  1303. pfat_table->aux_ant_sum[mac_id] += rx_pwdb_all;
  1304. pfat_table->aux_ant_cnt[mac_id]++;
  1305. }
  1306. } else if (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV) {
  1307. if (antsel_tr_mux == MAIN_ANT_CGCS_RX) {
  1308. pfat_table->main_ant_sum[mac_id] += rx_pwdb_all;
  1309. pfat_table->main_ant_cnt[mac_id]++;
  1310. } else {
  1311. pfat_table->aux_ant_sum[mac_id] += rx_pwdb_all;
  1312. pfat_table->aux_ant_cnt[mac_id]++;
  1313. }
  1314. }
  1315. }
  1316. static void rtl88e_dm_hw_ant_div(struct ieee80211_hw *hw)
  1317. {
  1318. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1319. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1320. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1321. struct rtl_sta_info *drv_priv;
  1322. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1323. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  1324. u32 i, min_rssi = 0xff, ant_div_max_rssi = 0;
  1325. u32 max_rssi = 0, local_min_rssi, local_max_rssi;
  1326. u32 main_rssi, aux_rssi;
  1327. u8 rx_idle_ant = 0, target_ant = 7;
  1328. /*for sta its self*/
  1329. i = 0;
  1330. main_rssi = (pfat_table->main_ant_cnt[i] != 0) ?
  1331. (pfat_table->main_ant_sum[i] / pfat_table->main_ant_cnt[i]) : 0;
  1332. aux_rssi = (pfat_table->aux_ant_cnt[i] != 0) ?
  1333. (pfat_table->aux_ant_sum[i] / pfat_table->aux_ant_cnt[i]) : 0;
  1334. target_ant = (main_rssi == aux_rssi) ?
  1335. pfat_table->rx_idle_ant : ((main_rssi >= aux_rssi) ?
  1336. MAIN_ANT : AUX_ANT);
  1337. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1338. "main_ant_sum %d main_ant_cnt %d\n",
  1339. pfat_table->main_ant_sum[i],
  1340. pfat_table->main_ant_cnt[i]);
  1341. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1342. "aux_ant_sum %d aux_ant_cnt %d\n",
  1343. pfat_table->aux_ant_sum[i], pfat_table->aux_ant_cnt[i]);
  1344. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "main_rssi %d aux_rssi%d\n",
  1345. main_rssi, aux_rssi);
  1346. local_max_rssi = (main_rssi > aux_rssi) ? main_rssi : aux_rssi;
  1347. if ((local_max_rssi > ant_div_max_rssi) && (local_max_rssi < 40))
  1348. ant_div_max_rssi = local_max_rssi;
  1349. if (local_max_rssi > max_rssi)
  1350. max_rssi = local_max_rssi;
  1351. if ((pfat_table->rx_idle_ant == MAIN_ANT) && (main_rssi == 0))
  1352. main_rssi = aux_rssi;
  1353. else if ((pfat_table->rx_idle_ant == AUX_ANT) && (aux_rssi == 0))
  1354. aux_rssi = main_rssi;
  1355. local_min_rssi = (main_rssi > aux_rssi) ? aux_rssi : main_rssi;
  1356. if (local_min_rssi < min_rssi) {
  1357. min_rssi = local_min_rssi;
  1358. rx_idle_ant = target_ant;
  1359. }
  1360. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1361. rtl88e_dm_update_tx_ant(hw, target_ant, i);
  1362. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP ||
  1363. rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC) {
  1364. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  1365. list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
  1366. i++;
  1367. main_rssi = (pfat_table->main_ant_cnt[i] != 0) ?
  1368. (pfat_table->main_ant_sum[i] /
  1369. pfat_table->main_ant_cnt[i]) : 0;
  1370. aux_rssi = (pfat_table->aux_ant_cnt[i] != 0) ?
  1371. (pfat_table->aux_ant_sum[i] /
  1372. pfat_table->aux_ant_cnt[i]) : 0;
  1373. target_ant = (main_rssi == aux_rssi) ?
  1374. pfat_table->rx_idle_ant : ((main_rssi >=
  1375. aux_rssi) ? MAIN_ANT : AUX_ANT);
  1376. local_max_rssi = (main_rssi > aux_rssi) ?
  1377. main_rssi : aux_rssi;
  1378. if ((local_max_rssi > ant_div_max_rssi) &&
  1379. (local_max_rssi < 40))
  1380. ant_div_max_rssi = local_max_rssi;
  1381. if (local_max_rssi > max_rssi)
  1382. max_rssi = local_max_rssi;
  1383. if ((pfat_table->rx_idle_ant == MAIN_ANT) &&
  1384. (main_rssi == 0))
  1385. main_rssi = aux_rssi;
  1386. else if ((pfat_table->rx_idle_ant == AUX_ANT) &&
  1387. (aux_rssi == 0))
  1388. aux_rssi = main_rssi;
  1389. local_min_rssi = (main_rssi > aux_rssi) ?
  1390. aux_rssi : main_rssi;
  1391. if (local_min_rssi < min_rssi) {
  1392. min_rssi = local_min_rssi;
  1393. rx_idle_ant = target_ant;
  1394. }
  1395. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1396. rtl88e_dm_update_tx_ant(hw, target_ant, i);
  1397. }
  1398. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  1399. }
  1400. for (i = 0; i < ASSOCIATE_ENTRY_NUM; i++) {
  1401. pfat_table->main_ant_sum[i] = 0;
  1402. pfat_table->aux_ant_sum[i] = 0;
  1403. pfat_table->main_ant_cnt[i] = 0;
  1404. pfat_table->aux_ant_cnt[i] = 0;
  1405. }
  1406. rtl88e_dm_update_rx_idle_ant(hw, rx_idle_ant);
  1407. dm_dig->antdiv_rssi_max = ant_div_max_rssi;
  1408. dm_dig->rssi_max = max_rssi;
  1409. }
  1410. static void rtl88e_set_next_mac_address_target(struct ieee80211_hw *hw)
  1411. {
  1412. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1413. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1414. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1415. struct rtl_sta_info *drv_priv;
  1416. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1417. u32 value32, i, j = 0;
  1418. if (mac->link_state >= MAC80211_LINKED) {
  1419. for (i = 0; i < ASSOCIATE_ENTRY_NUM; i++) {
  1420. if ((pfat_table->train_idx + 1) == ASSOCIATE_ENTRY_NUM)
  1421. pfat_table->train_idx = 0;
  1422. else
  1423. pfat_table->train_idx++;
  1424. if (pfat_table->train_idx == 0) {
  1425. value32 = (mac->mac_addr[5] << 8) |
  1426. mac->mac_addr[4];
  1427. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA2_11N,
  1428. MASKLWORD, value32);
  1429. value32 = (mac->mac_addr[3] << 24) |
  1430. (mac->mac_addr[2] << 16) |
  1431. (mac->mac_addr[1] << 8) |
  1432. mac->mac_addr[0];
  1433. rtl_set_bbreg(hw, DM_REG_ANT_TRAIN_PARA1_11N,
  1434. MASKDWORD, value32);
  1435. break;
  1436. }
  1437. if (rtlpriv->mac80211.opmode !=
  1438. NL80211_IFTYPE_STATION) {
  1439. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  1440. list_for_each_entry(drv_priv,
  1441. &rtlpriv->entry_list, list) {
  1442. j++;
  1443. if (j != pfat_table->train_idx)
  1444. continue;
  1445. value32 = (drv_priv->mac_addr[5] << 8) |
  1446. drv_priv->mac_addr[4];
  1447. rtl_set_bbreg(hw,
  1448. DM_REG_ANT_TRAIN_PARA2_11N,
  1449. MASKLWORD, value32);
  1450. value32 = (drv_priv->mac_addr[3] << 24) |
  1451. (drv_priv->mac_addr[2] << 16) |
  1452. (drv_priv->mac_addr[1] << 8) |
  1453. drv_priv->mac_addr[0];
  1454. rtl_set_bbreg(hw,
  1455. DM_REG_ANT_TRAIN_PARA1_11N,
  1456. MASKDWORD, value32);
  1457. break;
  1458. }
  1459. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  1460. /*find entry, break*/
  1461. if (j == pfat_table->train_idx)
  1462. break;
  1463. }
  1464. }
  1465. }
  1466. }
  1467. static void rtl88e_dm_fast_ant_training(struct ieee80211_hw *hw)
  1468. {
  1469. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1470. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1471. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1472. u32 i, max_rssi = 0;
  1473. u8 target_ant = 2;
  1474. bool bpkt_filter_match = false;
  1475. if (pfat_table->fat_state == FAT_TRAINING_STATE) {
  1476. for (i = 0; i < 7; i++) {
  1477. if (pfat_table->ant_cnt[i] == 0) {
  1478. pfat_table->ant_ave[i] = 0;
  1479. } else {
  1480. pfat_table->ant_ave[i] =
  1481. pfat_table->ant_sum[i] /
  1482. pfat_table->ant_cnt[i];
  1483. bpkt_filter_match = true;
  1484. }
  1485. if (pfat_table->ant_ave[i] > max_rssi) {
  1486. max_rssi = pfat_table->ant_ave[i];
  1487. target_ant = (u8) i;
  1488. }
  1489. }
  1490. if (bpkt_filter_match == false) {
  1491. rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N,
  1492. BIT(16), 0);
  1493. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
  1494. } else {
  1495. rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N,
  1496. BIT(16), 0);
  1497. rtl_set_bbreg(hw, DM_REG_RX_ANT_CTRL_11N, BIT(8) |
  1498. BIT(7) | BIT(6), target_ant);
  1499. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
  1500. BIT(21), 1);
  1501. pfat_table->antsel_a[pfat_table->train_idx] =
  1502. target_ant & BIT(0);
  1503. pfat_table->antsel_b[pfat_table->train_idx] =
  1504. (target_ant & BIT(1)) >> 1;
  1505. pfat_table->antsel_c[pfat_table->train_idx] =
  1506. (target_ant & BIT(2)) >> 2;
  1507. if (target_ant == 0)
  1508. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
  1509. }
  1510. for (i = 0; i < 7; i++) {
  1511. pfat_table->ant_sum[i] = 0;
  1512. pfat_table->ant_cnt[i] = 0;
  1513. }
  1514. pfat_table->fat_state = FAT_NORMAL_STATE;
  1515. return;
  1516. }
  1517. if (pfat_table->fat_state == FAT_NORMAL_STATE) {
  1518. rtl88e_set_next_mac_address_target(hw);
  1519. pfat_table->fat_state = FAT_TRAINING_STATE;
  1520. rtl_set_bbreg(hw, DM_REG_TXAGC_A_1_MCS32_11N, BIT(16), 1);
  1521. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
  1522. mod_timer(&rtlpriv->works.fast_antenna_training_timer,
  1523. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  1524. }
  1525. }
  1526. void rtl88e_dm_fast_antenna_training_callback(unsigned long data)
  1527. {
  1528. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  1529. rtl88e_dm_fast_ant_training(hw);
  1530. }
  1531. static void rtl88e_dm_antenna_diversity(struct ieee80211_hw *hw)
  1532. {
  1533. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1534. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1535. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1536. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  1537. struct fast_ant_training *pfat_table = &rtldm->fat_table;
  1538. if (mac->link_state < MAC80211_LINKED) {
  1539. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD, "No Link\n");
  1540. if (pfat_table->becomelinked) {
  1541. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  1542. "need to turn off HW AntDiv\n");
  1543. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 0);
  1544. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA1_11N,
  1545. BIT(15), 0);
  1546. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1547. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
  1548. BIT(21), 0);
  1549. pfat_table->becomelinked =
  1550. (mac->link_state == MAC80211_LINKED) ?
  1551. true : false;
  1552. }
  1553. return;
  1554. } else {
  1555. if (!pfat_table->becomelinked) {
  1556. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  1557. "Need to turn on HW AntDiv\n");
  1558. rtl_set_bbreg(hw, DM_REG_IGI_A_11N, BIT(7), 1);
  1559. rtl_set_bbreg(hw, DM_REG_CCK_ANTDIV_PARA1_11N,
  1560. BIT(15), 1);
  1561. if (rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV)
  1562. rtl_set_bbreg(hw, DM_REG_TX_ANT_CTRL_11N,
  1563. BIT(21), 1);
  1564. pfat_table->becomelinked =
  1565. (mac->link_state >= MAC80211_LINKED) ?
  1566. true : false;
  1567. }
  1568. }
  1569. if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
  1570. (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV))
  1571. rtl88e_dm_hw_ant_div(hw);
  1572. else if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV)
  1573. rtl88e_dm_fast_ant_training(hw);
  1574. }
  1575. void rtl88e_dm_init(struct ieee80211_hw *hw)
  1576. {
  1577. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1578. u32 cur_igvalue = rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f);
  1579. rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
  1580. rtl_dm_diginit(hw, cur_igvalue);
  1581. rtl88e_dm_init_dynamic_txpower(hw);
  1582. rtl88e_dm_init_edca_turbo(hw);
  1583. rtl88e_dm_init_rate_adaptive_mask(hw);
  1584. rtl88e_dm_init_txpower_tracking(hw);
  1585. rtl92c_dm_init_dynamic_bb_powersaving(hw);
  1586. rtl88e_dm_antenna_div_init(hw);
  1587. }
  1588. void rtl88e_dm_watchdog(struct ieee80211_hw *hw)
  1589. {
  1590. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1591. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1592. bool fw_current_inpsmode = false;
  1593. bool fw_ps_awake = true;
  1594. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
  1595. (u8 *)(&fw_current_inpsmode));
  1596. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FWLPS_RF_ON,
  1597. (u8 *)(&fw_ps_awake));
  1598. if (ppsc->p2p_ps_info.p2p_ps_mode)
  1599. fw_ps_awake = false;
  1600. if ((ppsc->rfpwr_state == ERFON) &&
  1601. ((!fw_current_inpsmode) && fw_ps_awake) &&
  1602. (!ppsc->rfchange_inprogress)) {
  1603. rtl88e_dm_pwdb_monitor(hw);
  1604. rtl88e_dm_dig(hw);
  1605. rtl88e_dm_false_alarm_counter_statistics(hw);
  1606. rtl92c_dm_dynamic_txpower(hw);
  1607. rtl88e_dm_check_txpower_tracking(hw);
  1608. rtl88e_dm_refresh_rate_adaptive_mask(hw);
  1609. rtl88e_dm_check_edca_turbo(hw);
  1610. rtl88e_dm_antenna_diversity(hw);
  1611. }
  1612. }