phy.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. #include "../wifi.h"
  26. #include "../pci.h"
  27. #include "../ps.h"
  28. #include "../core.h"
  29. #include "reg.h"
  30. #include "def.h"
  31. #include "phy.h"
  32. #include "rf.h"
  33. #include "dm.h"
  34. #include "table.h"
  35. #include "sw.h"
  36. #include "hw.h"
  37. #define MAX_RF_IMR_INDEX 12
  38. #define MAX_RF_IMR_INDEX_NORMAL 13
  39. #define RF_REG_NUM_FOR_C_CUT_5G 6
  40. #define RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA 7
  41. #define RF_REG_NUM_FOR_C_CUT_2G 5
  42. #define RF_CHNL_NUM_5G 19
  43. #define RF_CHNL_NUM_5G_40M 17
  44. #define TARGET_CHNL_NUM_5G 221
  45. #define TARGET_CHNL_NUM_2G 14
  46. #define CV_CURVE_CNT 64
  47. static u32 rf_reg_for_5g_swchnl_normal[MAX_RF_IMR_INDEX_NORMAL] = {
  48. 0, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x0
  49. };
  50. static u8 rf_reg_for_c_cut_5g[RF_REG_NUM_FOR_C_CUT_5G] = {
  51. RF_SYN_G1, RF_SYN_G2, RF_SYN_G3, RF_SYN_G4, RF_SYN_G5, RF_SYN_G6
  52. };
  53. static u8 rf_reg_for_c_cut_2g[RF_REG_NUM_FOR_C_CUT_2G] = {
  54. RF_SYN_G1, RF_SYN_G2, RF_SYN_G3, RF_SYN_G7, RF_SYN_G8
  55. };
  56. static u8 rf_for_c_cut_5g_internal_pa[RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA] = {
  57. 0x0B, 0x48, 0x49, 0x4B, 0x03, 0x04, 0x0E
  58. };
  59. static u32 rf_reg_mask_for_c_cut_2g[RF_REG_NUM_FOR_C_CUT_2G] = {
  60. BIT(19) | BIT(18) | BIT(17) | BIT(14) | BIT(1),
  61. BIT(10) | BIT(9),
  62. BIT(18) | BIT(17) | BIT(16) | BIT(1),
  63. BIT(2) | BIT(1),
  64. BIT(15) | BIT(14) | BIT(13) | BIT(12) | BIT(11)
  65. };
  66. static u8 rf_chnl_5g[RF_CHNL_NUM_5G] = {
  67. 36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108,
  68. 112, 116, 120, 124, 128, 132, 136, 140
  69. };
  70. static u8 rf_chnl_5g_40m[RF_CHNL_NUM_5G_40M] = {
  71. 38, 42, 46, 50, 54, 58, 62, 102, 106, 110, 114,
  72. 118, 122, 126, 130, 134, 138
  73. };
  74. static u32 rf_reg_pram_c_5g[5][RF_REG_NUM_FOR_C_CUT_5G] = {
  75. {0xE43BE, 0xFC638, 0x77C0A, 0xDE471, 0xd7110, 0x8EB04},
  76. {0xE43BE, 0xFC078, 0xF7C1A, 0xE0C71, 0xD7550, 0xAEB04},
  77. {0xE43BF, 0xFF038, 0xF7C0A, 0xDE471, 0xE5550, 0xAEB04},
  78. {0xE43BF, 0xFF079, 0xF7C1A, 0xDE471, 0xE5550, 0xAEB04},
  79. {0xE43BF, 0xFF038, 0xF7C1A, 0xDE471, 0xd7550, 0xAEB04}
  80. };
  81. static u32 rf_reg_param_for_c_cut_2g[3][RF_REG_NUM_FOR_C_CUT_2G] = {
  82. {0x643BC, 0xFC038, 0x77C1A, 0x41289, 0x01840},
  83. {0x643BC, 0xFC038, 0x07C1A, 0x41289, 0x01840},
  84. {0x243BC, 0xFC438, 0x07C1A, 0x4128B, 0x0FC41}
  85. };
  86. static u32 rf_syn_g4_for_c_cut_2g = 0xD1C31 & 0x7FF;
  87. static u32 rf_pram_c_5g_int_pa[3][RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA] = {
  88. {0x01a00, 0x40443, 0x00eb5, 0x89bec, 0x94a12, 0x94a12, 0x94a12},
  89. {0x01800, 0xc0443, 0x00730, 0x896ee, 0x94a52, 0x94a52, 0x94a52},
  90. {0x01800, 0xc0443, 0x00730, 0x896ee, 0x94a12, 0x94a12, 0x94a12}
  91. };
  92. /* [mode][patha+b][reg] */
  93. static u32 rf_imr_param_normal[1][3][MAX_RF_IMR_INDEX_NORMAL] = {
  94. {
  95. /* channel 1-14. */
  96. {
  97. 0x70000, 0x00ff0, 0x4400f, 0x00ff0, 0x0, 0x0, 0x0,
  98. 0x0, 0x0, 0x64888, 0xe266c, 0x00090, 0x22fff
  99. },
  100. /* path 36-64 */
  101. {
  102. 0x70000, 0x22880, 0x4470f, 0x55880, 0x00070, 0x88000,
  103. 0x0, 0x88080, 0x70000, 0x64a82, 0xe466c, 0x00090,
  104. 0x32c9a
  105. },
  106. /* 100 -165 */
  107. {
  108. 0x70000, 0x44880, 0x4477f, 0x77880, 0x00070, 0x88000,
  109. 0x0, 0x880b0, 0x0, 0x64b82, 0xe466c, 0x00090, 0x32c9a
  110. }
  111. }
  112. };
  113. static u32 curveindex_5g[TARGET_CHNL_NUM_5G] = {0};
  114. static u32 curveindex_2g[TARGET_CHNL_NUM_2G] = {0};
  115. static u32 targetchnl_5g[TARGET_CHNL_NUM_5G] = {
  116. 25141, 25116, 25091, 25066, 25041,
  117. 25016, 24991, 24966, 24941, 24917,
  118. 24892, 24867, 24843, 24818, 24794,
  119. 24770, 24765, 24721, 24697, 24672,
  120. 24648, 24624, 24600, 24576, 24552,
  121. 24528, 24504, 24480, 24457, 24433,
  122. 24409, 24385, 24362, 24338, 24315,
  123. 24291, 24268, 24245, 24221, 24198,
  124. 24175, 24151, 24128, 24105, 24082,
  125. 24059, 24036, 24013, 23990, 23967,
  126. 23945, 23922, 23899, 23876, 23854,
  127. 23831, 23809, 23786, 23764, 23741,
  128. 23719, 23697, 23674, 23652, 23630,
  129. 23608, 23586, 23564, 23541, 23519,
  130. 23498, 23476, 23454, 23432, 23410,
  131. 23388, 23367, 23345, 23323, 23302,
  132. 23280, 23259, 23237, 23216, 23194,
  133. 23173, 23152, 23130, 23109, 23088,
  134. 23067, 23046, 23025, 23003, 22982,
  135. 22962, 22941, 22920, 22899, 22878,
  136. 22857, 22837, 22816, 22795, 22775,
  137. 22754, 22733, 22713, 22692, 22672,
  138. 22652, 22631, 22611, 22591, 22570,
  139. 22550, 22530, 22510, 22490, 22469,
  140. 22449, 22429, 22409, 22390, 22370,
  141. 22350, 22336, 22310, 22290, 22271,
  142. 22251, 22231, 22212, 22192, 22173,
  143. 22153, 22134, 22114, 22095, 22075,
  144. 22056, 22037, 22017, 21998, 21979,
  145. 21960, 21941, 21921, 21902, 21883,
  146. 21864, 21845, 21826, 21807, 21789,
  147. 21770, 21751, 21732, 21713, 21695,
  148. 21676, 21657, 21639, 21620, 21602,
  149. 21583, 21565, 21546, 21528, 21509,
  150. 21491, 21473, 21454, 21436, 21418,
  151. 21400, 21381, 21363, 21345, 21327,
  152. 21309, 21291, 21273, 21255, 21237,
  153. 21219, 21201, 21183, 21166, 21148,
  154. 21130, 21112, 21095, 21077, 21059,
  155. 21042, 21024, 21007, 20989, 20972,
  156. 25679, 25653, 25627, 25601, 25575,
  157. 25549, 25523, 25497, 25471, 25446,
  158. 25420, 25394, 25369, 25343, 25318,
  159. 25292, 25267, 25242, 25216, 25191,
  160. 25166
  161. };
  162. /* channel 1~14 */
  163. static u32 targetchnl_2g[TARGET_CHNL_NUM_2G] = {
  164. 26084, 26030, 25976, 25923, 25869, 25816, 25764,
  165. 25711, 25658, 25606, 25554, 25502, 25451, 25328
  166. };
  167. static u32 _rtl92d_phy_calculate_bit_shift(u32 bitmask)
  168. {
  169. u32 i;
  170. for (i = 0; i <= 31; i++) {
  171. if (((bitmask >> i) & 0x1) == 1)
  172. break;
  173. }
  174. return i;
  175. }
  176. u32 rtl92d_phy_query_bb_reg(struct ieee80211_hw *hw, u32 regaddr, u32 bitmask)
  177. {
  178. struct rtl_priv *rtlpriv = rtl_priv(hw);
  179. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  180. u32 returnvalue, originalvalue, bitshift;
  181. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, "regaddr(%#x), bitmask(%#x)\n",
  182. regaddr, bitmask);
  183. if (rtlhal->during_mac1init_radioa || rtlhal->during_mac0init_radiob) {
  184. u8 dbi_direct = 0;
  185. /* mac1 use phy0 read radio_b. */
  186. /* mac0 use phy1 read radio_b. */
  187. if (rtlhal->during_mac1init_radioa)
  188. dbi_direct = BIT(3);
  189. else if (rtlhal->during_mac0init_radiob)
  190. dbi_direct = BIT(3) | BIT(2);
  191. originalvalue = rtl92de_read_dword_dbi(hw, (u16)regaddr,
  192. dbi_direct);
  193. } else {
  194. originalvalue = rtl_read_dword(rtlpriv, regaddr);
  195. }
  196. bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
  197. returnvalue = (originalvalue & bitmask) >> bitshift;
  198. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  199. "BBR MASK=0x%x Addr[0x%x]=0x%x\n",
  200. bitmask, regaddr, originalvalue);
  201. return returnvalue;
  202. }
  203. void rtl92d_phy_set_bb_reg(struct ieee80211_hw *hw,
  204. u32 regaddr, u32 bitmask, u32 data)
  205. {
  206. struct rtl_priv *rtlpriv = rtl_priv(hw);
  207. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  208. u8 dbi_direct = 0;
  209. u32 originalvalue, bitshift;
  210. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  211. "regaddr(%#x), bitmask(%#x), data(%#x)\n",
  212. regaddr, bitmask, data);
  213. if (rtlhal->during_mac1init_radioa)
  214. dbi_direct = BIT(3);
  215. else if (rtlhal->during_mac0init_radiob)
  216. /* mac0 use phy1 write radio_b. */
  217. dbi_direct = BIT(3) | BIT(2);
  218. if (bitmask != MASKDWORD) {
  219. if (rtlhal->during_mac1init_radioa ||
  220. rtlhal->during_mac0init_radiob)
  221. originalvalue = rtl92de_read_dword_dbi(hw,
  222. (u16) regaddr,
  223. dbi_direct);
  224. else
  225. originalvalue = rtl_read_dword(rtlpriv, regaddr);
  226. bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
  227. data = ((originalvalue & (~bitmask)) | (data << bitshift));
  228. }
  229. if (rtlhal->during_mac1init_radioa || rtlhal->during_mac0init_radiob)
  230. rtl92de_write_dword_dbi(hw, (u16) regaddr, data, dbi_direct);
  231. else
  232. rtl_write_dword(rtlpriv, regaddr, data);
  233. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  234. "regaddr(%#x), bitmask(%#x), data(%#x)\n",
  235. regaddr, bitmask, data);
  236. }
  237. static u32 _rtl92d_phy_rf_serial_read(struct ieee80211_hw *hw,
  238. enum radio_path rfpath, u32 offset)
  239. {
  240. struct rtl_priv *rtlpriv = rtl_priv(hw);
  241. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  242. struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
  243. u32 newoffset;
  244. u32 tmplong, tmplong2;
  245. u8 rfpi_enable = 0;
  246. u32 retvalue;
  247. newoffset = offset;
  248. tmplong = rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, MASKDWORD);
  249. if (rfpath == RF90_PATH_A)
  250. tmplong2 = tmplong;
  251. else
  252. tmplong2 = rtl_get_bbreg(hw, pphyreg->rfhssi_para2, MASKDWORD);
  253. tmplong2 = (tmplong2 & (~BLSSIREADADDRESS)) |
  254. (newoffset << 23) | BLSSIREADEDGE;
  255. rtl_set_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, MASKDWORD,
  256. tmplong & (~BLSSIREADEDGE));
  257. udelay(10);
  258. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, MASKDWORD, tmplong2);
  259. udelay(50);
  260. udelay(50);
  261. rtl_set_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, MASKDWORD,
  262. tmplong | BLSSIREADEDGE);
  263. udelay(10);
  264. if (rfpath == RF90_PATH_A)
  265. rfpi_enable = (u8) rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER1,
  266. BIT(8));
  267. else if (rfpath == RF90_PATH_B)
  268. rfpi_enable = (u8) rtl_get_bbreg(hw, RFPGA0_XB_HSSIPARAMETER1,
  269. BIT(8));
  270. if (rfpi_enable)
  271. retvalue = rtl_get_bbreg(hw, pphyreg->rf_rbpi,
  272. BLSSIREADBACKDATA);
  273. else
  274. retvalue = rtl_get_bbreg(hw, pphyreg->rf_rb,
  275. BLSSIREADBACKDATA);
  276. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, "RFR-%d Addr[0x%x] = 0x%x\n",
  277. rfpath, pphyreg->rf_rb, retvalue);
  278. return retvalue;
  279. }
  280. static void _rtl92d_phy_rf_serial_write(struct ieee80211_hw *hw,
  281. enum radio_path rfpath,
  282. u32 offset, u32 data)
  283. {
  284. u32 data_and_addr;
  285. u32 newoffset;
  286. struct rtl_priv *rtlpriv = rtl_priv(hw);
  287. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  288. struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
  289. newoffset = offset;
  290. /* T65 RF */
  291. data_and_addr = ((newoffset << 20) | (data & 0x000fffff)) & 0x0fffffff;
  292. rtl_set_bbreg(hw, pphyreg->rf3wire_offset, MASKDWORD, data_and_addr);
  293. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, "RFW-%d Addr[0x%x]=0x%x\n",
  294. rfpath, pphyreg->rf3wire_offset, data_and_addr);
  295. }
  296. u32 rtl92d_phy_query_rf_reg(struct ieee80211_hw *hw,
  297. enum radio_path rfpath, u32 regaddr, u32 bitmask)
  298. {
  299. struct rtl_priv *rtlpriv = rtl_priv(hw);
  300. u32 original_value, readback_value, bitshift;
  301. unsigned long flags;
  302. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  303. "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
  304. regaddr, rfpath, bitmask);
  305. spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
  306. original_value = _rtl92d_phy_rf_serial_read(hw, rfpath, regaddr);
  307. bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
  308. readback_value = (original_value & bitmask) >> bitshift;
  309. spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
  310. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  311. "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
  312. regaddr, rfpath, bitmask, original_value);
  313. return readback_value;
  314. }
  315. void rtl92d_phy_set_rf_reg(struct ieee80211_hw *hw, enum radio_path rfpath,
  316. u32 regaddr, u32 bitmask, u32 data)
  317. {
  318. struct rtl_priv *rtlpriv = rtl_priv(hw);
  319. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  320. u32 original_value, bitshift;
  321. unsigned long flags;
  322. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  323. "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
  324. regaddr, bitmask, data, rfpath);
  325. if (bitmask == 0)
  326. return;
  327. spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
  328. if (rtlphy->rf_mode != RF_OP_BY_FW) {
  329. if (bitmask != RFREG_OFFSET_MASK) {
  330. original_value = _rtl92d_phy_rf_serial_read(hw,
  331. rfpath, regaddr);
  332. bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
  333. data = ((original_value & (~bitmask)) |
  334. (data << bitshift));
  335. }
  336. _rtl92d_phy_rf_serial_write(hw, rfpath, regaddr, data);
  337. }
  338. spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
  339. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  340. "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
  341. regaddr, bitmask, data, rfpath);
  342. }
  343. bool rtl92d_phy_mac_config(struct ieee80211_hw *hw)
  344. {
  345. struct rtl_priv *rtlpriv = rtl_priv(hw);
  346. u32 i;
  347. u32 arraylength;
  348. u32 *ptrarray;
  349. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Read Rtl819XMACPHY_Array\n");
  350. arraylength = MAC_2T_ARRAYLENGTH;
  351. ptrarray = rtl8192de_mac_2tarray;
  352. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Img:Rtl819XMAC_Array\n");
  353. for (i = 0; i < arraylength; i = i + 2)
  354. rtl_write_byte(rtlpriv, ptrarray[i], (u8) ptrarray[i + 1]);
  355. if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY) {
  356. /* improve 2-stream TX EVM */
  357. /* rtl_write_byte(rtlpriv, 0x14,0x71); */
  358. /* AMPDU aggregation number 9 */
  359. /* rtl_write_word(rtlpriv, REG_MAX_AGGR_NUM, MAX_AGGR_NUM); */
  360. rtl_write_byte(rtlpriv, REG_MAX_AGGR_NUM, 0x0B);
  361. } else {
  362. /* 92D need to test to decide the num. */
  363. rtl_write_byte(rtlpriv, REG_MAX_AGGR_NUM, 0x07);
  364. }
  365. return true;
  366. }
  367. static void _rtl92d_phy_init_bb_rf_register_definition(struct ieee80211_hw *hw)
  368. {
  369. struct rtl_priv *rtlpriv = rtl_priv(hw);
  370. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  371. /* RF Interface Sowrtware Control */
  372. /* 16 LSBs if read 32-bit from 0x870 */
  373. rtlphy->phyreg_def[RF90_PATH_A].rfintfs = RFPGA0_XAB_RFINTERFACESW;
  374. /* 16 MSBs if read 32-bit from 0x870 (16-bit for 0x872) */
  375. rtlphy->phyreg_def[RF90_PATH_B].rfintfs = RFPGA0_XAB_RFINTERFACESW;
  376. /* 16 LSBs if read 32-bit from 0x874 */
  377. rtlphy->phyreg_def[RF90_PATH_C].rfintfs = RFPGA0_XCD_RFINTERFACESW;
  378. /* 16 MSBs if read 32-bit from 0x874 (16-bit for 0x876) */
  379. rtlphy->phyreg_def[RF90_PATH_D].rfintfs = RFPGA0_XCD_RFINTERFACESW;
  380. /* RF Interface Readback Value */
  381. /* 16 LSBs if read 32-bit from 0x8E0 */
  382. rtlphy->phyreg_def[RF90_PATH_A].rfintfi = RFPGA0_XAB_RFINTERFACERB;
  383. /* 16 MSBs if read 32-bit from 0x8E0 (16-bit for 0x8E2) */
  384. rtlphy->phyreg_def[RF90_PATH_B].rfintfi = RFPGA0_XAB_RFINTERFACERB;
  385. /* 16 LSBs if read 32-bit from 0x8E4 */
  386. rtlphy->phyreg_def[RF90_PATH_C].rfintfi = RFPGA0_XCD_RFINTERFACERB;
  387. /* 16 MSBs if read 32-bit from 0x8E4 (16-bit for 0x8E6) */
  388. rtlphy->phyreg_def[RF90_PATH_D].rfintfi = RFPGA0_XCD_RFINTERFACERB;
  389. /* RF Interface Output (and Enable) */
  390. /* 16 LSBs if read 32-bit from 0x860 */
  391. rtlphy->phyreg_def[RF90_PATH_A].rfintfo = RFPGA0_XA_RFINTERFACEOE;
  392. /* 16 LSBs if read 32-bit from 0x864 */
  393. rtlphy->phyreg_def[RF90_PATH_B].rfintfo = RFPGA0_XB_RFINTERFACEOE;
  394. /* RF Interface (Output and) Enable */
  395. /* 16 MSBs if read 32-bit from 0x860 (16-bit for 0x862) */
  396. rtlphy->phyreg_def[RF90_PATH_A].rfintfe = RFPGA0_XA_RFINTERFACEOE;
  397. /* 16 MSBs if read 32-bit from 0x864 (16-bit for 0x866) */
  398. rtlphy->phyreg_def[RF90_PATH_B].rfintfe = RFPGA0_XB_RFINTERFACEOE;
  399. /* Addr of LSSI. Wirte RF register by driver */
  400. /* LSSI Parameter */
  401. rtlphy->phyreg_def[RF90_PATH_A].rf3wire_offset =
  402. RFPGA0_XA_LSSIPARAMETER;
  403. rtlphy->phyreg_def[RF90_PATH_B].rf3wire_offset =
  404. RFPGA0_XB_LSSIPARAMETER;
  405. /* RF parameter */
  406. /* BB Band Select */
  407. rtlphy->phyreg_def[RF90_PATH_A].rflssi_select = RFPGA0_XAB_RFPARAMETER;
  408. rtlphy->phyreg_def[RF90_PATH_B].rflssi_select = RFPGA0_XAB_RFPARAMETER;
  409. rtlphy->phyreg_def[RF90_PATH_C].rflssi_select = RFPGA0_XCD_RFPARAMETER;
  410. rtlphy->phyreg_def[RF90_PATH_D].rflssi_select = RFPGA0_XCD_RFPARAMETER;
  411. /* Tx AGC Gain Stage (same for all path. Should we remove this?) */
  412. /* Tx gain stage */
  413. rtlphy->phyreg_def[RF90_PATH_A].rftxgain_stage = RFPGA0_TXGAINSTAGE;
  414. /* Tx gain stage */
  415. rtlphy->phyreg_def[RF90_PATH_B].rftxgain_stage = RFPGA0_TXGAINSTAGE;
  416. /* Tx gain stage */
  417. rtlphy->phyreg_def[RF90_PATH_C].rftxgain_stage = RFPGA0_TXGAINSTAGE;
  418. /* Tx gain stage */
  419. rtlphy->phyreg_def[RF90_PATH_D].rftxgain_stage = RFPGA0_TXGAINSTAGE;
  420. /* Tranceiver A~D HSSI Parameter-1 */
  421. /* wire control parameter1 */
  422. rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para1 = RFPGA0_XA_HSSIPARAMETER1;
  423. /* wire control parameter1 */
  424. rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para1 = RFPGA0_XB_HSSIPARAMETER1;
  425. /* Tranceiver A~D HSSI Parameter-2 */
  426. /* wire control parameter2 */
  427. rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para2 = RFPGA0_XA_HSSIPARAMETER2;
  428. /* wire control parameter2 */
  429. rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para2 = RFPGA0_XB_HSSIPARAMETER2;
  430. /* RF switch Control */
  431. /* TR/Ant switch control */
  432. rtlphy->phyreg_def[RF90_PATH_A].rfsw_ctrl = RFPGA0_XAB_SWITCHCONTROL;
  433. rtlphy->phyreg_def[RF90_PATH_B].rfsw_ctrl = RFPGA0_XAB_SWITCHCONTROL;
  434. rtlphy->phyreg_def[RF90_PATH_C].rfsw_ctrl = RFPGA0_XCD_SWITCHCONTROL;
  435. rtlphy->phyreg_def[RF90_PATH_D].rfsw_ctrl = RFPGA0_XCD_SWITCHCONTROL;
  436. /* AGC control 1 */
  437. rtlphy->phyreg_def[RF90_PATH_A].rfagc_control1 = ROFDM0_XAAGCCORE1;
  438. rtlphy->phyreg_def[RF90_PATH_B].rfagc_control1 = ROFDM0_XBAGCCORE1;
  439. rtlphy->phyreg_def[RF90_PATH_C].rfagc_control1 = ROFDM0_XCAGCCORE1;
  440. rtlphy->phyreg_def[RF90_PATH_D].rfagc_control1 = ROFDM0_XDAGCCORE1;
  441. /* AGC control 2 */
  442. rtlphy->phyreg_def[RF90_PATH_A].rfagc_control2 = ROFDM0_XAAGCCORE2;
  443. rtlphy->phyreg_def[RF90_PATH_B].rfagc_control2 = ROFDM0_XBAGCCORE2;
  444. rtlphy->phyreg_def[RF90_PATH_C].rfagc_control2 = ROFDM0_XCAGCCORE2;
  445. rtlphy->phyreg_def[RF90_PATH_D].rfagc_control2 = ROFDM0_XDAGCCORE2;
  446. /* RX AFE control 1 */
  447. rtlphy->phyreg_def[RF90_PATH_A].rfrxiq_imbal = ROFDM0_XARXIQIMBALANCE;
  448. rtlphy->phyreg_def[RF90_PATH_B].rfrxiq_imbal = ROFDM0_XBRXIQIMBALANCE;
  449. rtlphy->phyreg_def[RF90_PATH_C].rfrxiq_imbal = ROFDM0_XCRXIQIMBALANCE;
  450. rtlphy->phyreg_def[RF90_PATH_D].rfrxiq_imbal = ROFDM0_XDRXIQIMBALANCE;
  451. /*RX AFE control 1 */
  452. rtlphy->phyreg_def[RF90_PATH_A].rfrx_afe = ROFDM0_XARXAFE;
  453. rtlphy->phyreg_def[RF90_PATH_B].rfrx_afe = ROFDM0_XBRXAFE;
  454. rtlphy->phyreg_def[RF90_PATH_C].rfrx_afe = ROFDM0_XCRXAFE;
  455. rtlphy->phyreg_def[RF90_PATH_D].rfrx_afe = ROFDM0_XDRXAFE;
  456. /* Tx AFE control 1 */
  457. rtlphy->phyreg_def[RF90_PATH_A].rftxiq_imbal = ROFDM0_XATxIQIMBALANCE;
  458. rtlphy->phyreg_def[RF90_PATH_B].rftxiq_imbal = ROFDM0_XBTxIQIMBALANCE;
  459. rtlphy->phyreg_def[RF90_PATH_C].rftxiq_imbal = ROFDM0_XCTxIQIMBALANCE;
  460. rtlphy->phyreg_def[RF90_PATH_D].rftxiq_imbal = ROFDM0_XDTxIQIMBALANCE;
  461. /* Tx AFE control 2 */
  462. rtlphy->phyreg_def[RF90_PATH_A].rftx_afe = ROFDM0_XATxAFE;
  463. rtlphy->phyreg_def[RF90_PATH_B].rftx_afe = ROFDM0_XBTxAFE;
  464. rtlphy->phyreg_def[RF90_PATH_C].rftx_afe = ROFDM0_XCTxAFE;
  465. rtlphy->phyreg_def[RF90_PATH_D].rftx_afe = ROFDM0_XDTxAFE;
  466. /* Tranceiver LSSI Readback SI mode */
  467. rtlphy->phyreg_def[RF90_PATH_A].rf_rb = RFPGA0_XA_LSSIREADBACK;
  468. rtlphy->phyreg_def[RF90_PATH_B].rf_rb = RFPGA0_XB_LSSIREADBACK;
  469. rtlphy->phyreg_def[RF90_PATH_C].rf_rb = RFPGA0_XC_LSSIREADBACK;
  470. rtlphy->phyreg_def[RF90_PATH_D].rf_rb = RFPGA0_XD_LSSIREADBACK;
  471. /* Tranceiver LSSI Readback PI mode */
  472. rtlphy->phyreg_def[RF90_PATH_A].rf_rbpi = TRANSCEIVERA_HSPI_READBACK;
  473. rtlphy->phyreg_def[RF90_PATH_B].rf_rbpi = TRANSCEIVERB_HSPI_READBACK;
  474. }
  475. static bool _rtl92d_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
  476. u8 configtype)
  477. {
  478. int i;
  479. u32 *phy_regarray_table;
  480. u32 *agctab_array_table = NULL;
  481. u32 *agctab_5garray_table;
  482. u16 phy_reg_arraylen, agctab_arraylen = 0, agctab_5garraylen;
  483. struct rtl_priv *rtlpriv = rtl_priv(hw);
  484. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  485. /* Normal chip,Mac0 use AGC_TAB.txt for 2G and 5G band. */
  486. if (rtlhal->interfaceindex == 0) {
  487. agctab_arraylen = AGCTAB_ARRAYLENGTH;
  488. agctab_array_table = rtl8192de_agctab_array;
  489. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  490. " ===> phy:MAC0, Rtl819XAGCTAB_Array\n");
  491. } else {
  492. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  493. agctab_arraylen = AGCTAB_2G_ARRAYLENGTH;
  494. agctab_array_table = rtl8192de_agctab_2garray;
  495. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  496. " ===> phy:MAC1, Rtl819XAGCTAB_2GArray\n");
  497. } else {
  498. agctab_5garraylen = AGCTAB_5G_ARRAYLENGTH;
  499. agctab_5garray_table = rtl8192de_agctab_5garray;
  500. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  501. " ===> phy:MAC1, Rtl819XAGCTAB_5GArray\n");
  502. }
  503. }
  504. phy_reg_arraylen = PHY_REG_2T_ARRAYLENGTH;
  505. phy_regarray_table = rtl8192de_phy_reg_2tarray;
  506. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  507. " ===> phy:Rtl819XPHY_REG_Array_PG\n");
  508. if (configtype == BASEBAND_CONFIG_PHY_REG) {
  509. for (i = 0; i < phy_reg_arraylen; i = i + 2) {
  510. rtl_addr_delay(phy_regarray_table[i]);
  511. rtl_set_bbreg(hw, phy_regarray_table[i], MASKDWORD,
  512. phy_regarray_table[i + 1]);
  513. udelay(1);
  514. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  515. "The phy_regarray_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
  516. phy_regarray_table[i],
  517. phy_regarray_table[i + 1]);
  518. }
  519. } else if (configtype == BASEBAND_CONFIG_AGC_TAB) {
  520. if (rtlhal->interfaceindex == 0) {
  521. for (i = 0; i < agctab_arraylen; i = i + 2) {
  522. rtl_set_bbreg(hw, agctab_array_table[i],
  523. MASKDWORD,
  524. agctab_array_table[i + 1]);
  525. /* Add 1us delay between BB/RF register
  526. * setting. */
  527. udelay(1);
  528. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  529. "The Rtl819XAGCTAB_Array_Table[0] is %u Rtl819XPHY_REGArray[1] is %u\n",
  530. agctab_array_table[i],
  531. agctab_array_table[i + 1]);
  532. }
  533. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  534. "Normal Chip, MAC0, load Rtl819XAGCTAB_Array\n");
  535. } else {
  536. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  537. for (i = 0; i < agctab_arraylen; i = i + 2) {
  538. rtl_set_bbreg(hw, agctab_array_table[i],
  539. MASKDWORD,
  540. agctab_array_table[i + 1]);
  541. /* Add 1us delay between BB/RF register
  542. * setting. */
  543. udelay(1);
  544. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  545. "The Rtl819XAGCTAB_Array_Table[0] is %u Rtl819XPHY_REGArray[1] is %u\n",
  546. agctab_array_table[i],
  547. agctab_array_table[i + 1]);
  548. }
  549. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  550. "Load Rtl819XAGCTAB_2GArray\n");
  551. } else {
  552. for (i = 0; i < agctab_5garraylen; i = i + 2) {
  553. rtl_set_bbreg(hw,
  554. agctab_5garray_table[i],
  555. MASKDWORD,
  556. agctab_5garray_table[i + 1]);
  557. /* Add 1us delay between BB/RF registeri
  558. * setting. */
  559. udelay(1);
  560. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  561. "The Rtl819XAGCTAB_5GArray_Table[0] is %u Rtl819XPHY_REGArray[1] is %u\n",
  562. agctab_5garray_table[i],
  563. agctab_5garray_table[i + 1]);
  564. }
  565. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  566. "Load Rtl819XAGCTAB_5GArray\n");
  567. }
  568. }
  569. }
  570. return true;
  571. }
  572. static void _rtl92d_store_pwrindex_diffrate_offset(struct ieee80211_hw *hw,
  573. u32 regaddr, u32 bitmask,
  574. u32 data)
  575. {
  576. struct rtl_priv *rtlpriv = rtl_priv(hw);
  577. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  578. int index;
  579. if (regaddr == RTXAGC_A_RATE18_06)
  580. index = 0;
  581. else if (regaddr == RTXAGC_A_RATE54_24)
  582. index = 1;
  583. else if (regaddr == RTXAGC_A_CCK1_MCS32)
  584. index = 6;
  585. else if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0xffffff00)
  586. index = 7;
  587. else if (regaddr == RTXAGC_A_MCS03_MCS00)
  588. index = 2;
  589. else if (regaddr == RTXAGC_A_MCS07_MCS04)
  590. index = 3;
  591. else if (regaddr == RTXAGC_A_MCS11_MCS08)
  592. index = 4;
  593. else if (regaddr == RTXAGC_A_MCS15_MCS12)
  594. index = 5;
  595. else if (regaddr == RTXAGC_B_RATE18_06)
  596. index = 8;
  597. else if (regaddr == RTXAGC_B_RATE54_24)
  598. index = 9;
  599. else if (regaddr == RTXAGC_B_CCK1_55_MCS32)
  600. index = 14;
  601. else if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0x000000ff)
  602. index = 15;
  603. else if (regaddr == RTXAGC_B_MCS03_MCS00)
  604. index = 10;
  605. else if (regaddr == RTXAGC_B_MCS07_MCS04)
  606. index = 11;
  607. else if (regaddr == RTXAGC_B_MCS11_MCS08)
  608. index = 12;
  609. else if (regaddr == RTXAGC_B_MCS15_MCS12)
  610. index = 13;
  611. else
  612. return;
  613. rtlphy->mcs_offset[rtlphy->pwrgroup_cnt][index] = data;
  614. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  615. "MCSTxPowerLevelOriginalOffset[%d][%d] = 0x%x\n",
  616. rtlphy->pwrgroup_cnt, index,
  617. rtlphy->mcs_offset[rtlphy->pwrgroup_cnt][index]);
  618. if (index == 13)
  619. rtlphy->pwrgroup_cnt++;
  620. }
  621. static bool _rtl92d_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
  622. u8 configtype)
  623. {
  624. struct rtl_priv *rtlpriv = rtl_priv(hw);
  625. int i;
  626. u32 *phy_regarray_table_pg;
  627. u16 phy_regarray_pg_len;
  628. phy_regarray_pg_len = PHY_REG_ARRAY_PG_LENGTH;
  629. phy_regarray_table_pg = rtl8192de_phy_reg_array_pg;
  630. if (configtype == BASEBAND_CONFIG_PHY_REG) {
  631. for (i = 0; i < phy_regarray_pg_len; i = i + 3) {
  632. rtl_addr_delay(phy_regarray_table_pg[i]);
  633. _rtl92d_store_pwrindex_diffrate_offset(hw,
  634. phy_regarray_table_pg[i],
  635. phy_regarray_table_pg[i + 1],
  636. phy_regarray_table_pg[i + 2]);
  637. }
  638. } else {
  639. RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
  640. "configtype != BaseBand_Config_PHY_REG\n");
  641. }
  642. return true;
  643. }
  644. static bool _rtl92d_phy_bb_config(struct ieee80211_hw *hw)
  645. {
  646. struct rtl_priv *rtlpriv = rtl_priv(hw);
  647. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  648. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  649. bool rtstatus = true;
  650. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "==>\n");
  651. rtstatus = _rtl92d_phy_config_bb_with_headerfile(hw,
  652. BASEBAND_CONFIG_PHY_REG);
  653. if (!rtstatus) {
  654. pr_err("Write BB Reg Fail!!\n");
  655. return false;
  656. }
  657. /* if (rtlphy->rf_type == RF_1T2R) {
  658. * _rtl92c_phy_bb_config_1t(hw);
  659. * RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Config to 1T!!\n");
  660. *} */
  661. if (rtlefuse->autoload_failflag == false) {
  662. rtlphy->pwrgroup_cnt = 0;
  663. rtstatus = _rtl92d_phy_config_bb_with_pgheaderfile(hw,
  664. BASEBAND_CONFIG_PHY_REG);
  665. }
  666. if (!rtstatus) {
  667. pr_err("BB_PG Reg Fail!!\n");
  668. return false;
  669. }
  670. rtstatus = _rtl92d_phy_config_bb_with_headerfile(hw,
  671. BASEBAND_CONFIG_AGC_TAB);
  672. if (!rtstatus) {
  673. pr_err("AGC Table Fail\n");
  674. return false;
  675. }
  676. rtlphy->cck_high_power = (bool) (rtl_get_bbreg(hw,
  677. RFPGA0_XA_HSSIPARAMETER2, 0x200));
  678. return true;
  679. }
  680. bool rtl92d_phy_bb_config(struct ieee80211_hw *hw)
  681. {
  682. struct rtl_priv *rtlpriv = rtl_priv(hw);
  683. u16 regval;
  684. u32 regvaldw;
  685. u8 value;
  686. _rtl92d_phy_init_bb_rf_register_definition(hw);
  687. regval = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
  688. rtl_write_word(rtlpriv, REG_SYS_FUNC_EN,
  689. regval | BIT(13) | BIT(0) | BIT(1));
  690. rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL, 0x83);
  691. rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL + 1, 0xdb);
  692. /* 0x1f bit7 bit6 represent for mac0/mac1 driver ready */
  693. value = rtl_read_byte(rtlpriv, REG_RF_CTRL);
  694. rtl_write_byte(rtlpriv, REG_RF_CTRL, value | RF_EN | RF_RSTB |
  695. RF_SDMRSTB);
  696. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, FEN_PPLL | FEN_PCIEA |
  697. FEN_DIO_PCIE | FEN_BB_GLB_RSTn | FEN_BBRSTB);
  698. rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL + 1, 0x80);
  699. if (!(IS_92D_SINGLEPHY(rtlpriv->rtlhal.version))) {
  700. regvaldw = rtl_read_dword(rtlpriv, REG_LEDCFG0);
  701. rtl_write_dword(rtlpriv, REG_LEDCFG0, regvaldw | BIT(23));
  702. }
  703. return _rtl92d_phy_bb_config(hw);
  704. }
  705. bool rtl92d_phy_rf_config(struct ieee80211_hw *hw)
  706. {
  707. return rtl92d_phy_rf6052_config(hw);
  708. }
  709. bool rtl92d_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
  710. enum rf_content content,
  711. enum radio_path rfpath)
  712. {
  713. int i;
  714. u32 *radioa_array_table;
  715. u32 *radiob_array_table;
  716. u16 radioa_arraylen, radiob_arraylen;
  717. struct rtl_priv *rtlpriv = rtl_priv(hw);
  718. radioa_arraylen = RADIOA_2T_ARRAYLENGTH;
  719. radioa_array_table = rtl8192de_radioa_2tarray;
  720. radiob_arraylen = RADIOB_2T_ARRAYLENGTH;
  721. radiob_array_table = rtl8192de_radiob_2tarray;
  722. if (rtlpriv->efuse.internal_pa_5g[0]) {
  723. radioa_arraylen = RADIOA_2T_INT_PA_ARRAYLENGTH;
  724. radioa_array_table = rtl8192de_radioa_2t_int_paarray;
  725. }
  726. if (rtlpriv->efuse.internal_pa_5g[1]) {
  727. radiob_arraylen = RADIOB_2T_INT_PA_ARRAYLENGTH;
  728. radiob_array_table = rtl8192de_radiob_2t_int_paarray;
  729. }
  730. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  731. "PHY_ConfigRFWithHeaderFile() Radio_A:Rtl819XRadioA_1TArray\n");
  732. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  733. "PHY_ConfigRFWithHeaderFile() Radio_B:Rtl819XRadioB_1TArray\n");
  734. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Radio No %x\n", rfpath);
  735. /* this only happens when DMDP, mac0 start on 2.4G,
  736. * mac1 start on 5G, mac 0 has to set phy0&phy1
  737. * pathA or mac1 has to set phy0&phy1 pathA */
  738. if ((content == radiob_txt) && (rfpath == RF90_PATH_A)) {
  739. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  740. " ===> althougth Path A, we load radiob.txt\n");
  741. radioa_arraylen = radiob_arraylen;
  742. radioa_array_table = radiob_array_table;
  743. }
  744. switch (rfpath) {
  745. case RF90_PATH_A:
  746. for (i = 0; i < radioa_arraylen; i = i + 2) {
  747. rtl_rfreg_delay(hw, rfpath, radioa_array_table[i],
  748. RFREG_OFFSET_MASK,
  749. radioa_array_table[i + 1]);
  750. }
  751. break;
  752. case RF90_PATH_B:
  753. for (i = 0; i < radiob_arraylen; i = i + 2) {
  754. rtl_rfreg_delay(hw, rfpath, radiob_array_table[i],
  755. RFREG_OFFSET_MASK,
  756. radiob_array_table[i + 1]);
  757. }
  758. break;
  759. case RF90_PATH_C:
  760. case RF90_PATH_D:
  761. pr_err("switch case %#x not processed\n", rfpath);
  762. break;
  763. }
  764. return true;
  765. }
  766. void rtl92d_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
  767. {
  768. struct rtl_priv *rtlpriv = rtl_priv(hw);
  769. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  770. rtlphy->default_initialgain[0] =
  771. (u8) rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
  772. rtlphy->default_initialgain[1] =
  773. (u8) rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
  774. rtlphy->default_initialgain[2] =
  775. (u8) rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
  776. rtlphy->default_initialgain[3] =
  777. (u8) rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
  778. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  779. "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
  780. rtlphy->default_initialgain[0],
  781. rtlphy->default_initialgain[1],
  782. rtlphy->default_initialgain[2],
  783. rtlphy->default_initialgain[3]);
  784. rtlphy->framesync = (u8)rtl_get_bbreg(hw, ROFDM0_RXDETECTOR3,
  785. MASKBYTE0);
  786. rtlphy->framesync_c34 = rtl_get_bbreg(hw, ROFDM0_RXDETECTOR2,
  787. MASKDWORD);
  788. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  789. "Default framesync (0x%x) = 0x%x\n",
  790. ROFDM0_RXDETECTOR3, rtlphy->framesync);
  791. }
  792. static void _rtl92d_get_txpower_index(struct ieee80211_hw *hw, u8 channel,
  793. u8 *cckpowerlevel, u8 *ofdmpowerlevel)
  794. {
  795. struct rtl_priv *rtlpriv = rtl_priv(hw);
  796. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  797. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  798. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  799. u8 index = (channel - 1);
  800. /* 1. CCK */
  801. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  802. /* RF-A */
  803. cckpowerlevel[RF90_PATH_A] =
  804. rtlefuse->txpwrlevel_cck[RF90_PATH_A][index];
  805. /* RF-B */
  806. cckpowerlevel[RF90_PATH_B] =
  807. rtlefuse->txpwrlevel_cck[RF90_PATH_B][index];
  808. } else {
  809. cckpowerlevel[RF90_PATH_A] = 0;
  810. cckpowerlevel[RF90_PATH_B] = 0;
  811. }
  812. /* 2. OFDM for 1S or 2S */
  813. if (rtlphy->rf_type == RF_1T2R || rtlphy->rf_type == RF_1T1R) {
  814. /* Read HT 40 OFDM TX power */
  815. ofdmpowerlevel[RF90_PATH_A] =
  816. rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_A][index];
  817. ofdmpowerlevel[RF90_PATH_B] =
  818. rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_B][index];
  819. } else if (rtlphy->rf_type == RF_2T2R) {
  820. /* Read HT 40 OFDM TX power */
  821. ofdmpowerlevel[RF90_PATH_A] =
  822. rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_A][index];
  823. ofdmpowerlevel[RF90_PATH_B] =
  824. rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_B][index];
  825. }
  826. }
  827. static void _rtl92d_ccxpower_index_check(struct ieee80211_hw *hw,
  828. u8 channel, u8 *cckpowerlevel, u8 *ofdmpowerlevel)
  829. {
  830. struct rtl_priv *rtlpriv = rtl_priv(hw);
  831. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  832. rtlphy->cur_cck_txpwridx = cckpowerlevel[0];
  833. rtlphy->cur_ofdm24g_txpwridx = ofdmpowerlevel[0];
  834. }
  835. static u8 _rtl92c_phy_get_rightchnlplace(u8 chnl)
  836. {
  837. u8 place = chnl;
  838. if (chnl > 14) {
  839. for (place = 14; place < sizeof(channel5g); place++) {
  840. if (channel5g[place] == chnl) {
  841. place++;
  842. break;
  843. }
  844. }
  845. }
  846. return place;
  847. }
  848. void rtl92d_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
  849. {
  850. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  851. struct rtl_priv *rtlpriv = rtl_priv(hw);
  852. u8 cckpowerlevel[2], ofdmpowerlevel[2];
  853. if (!rtlefuse->txpwr_fromeprom)
  854. return;
  855. channel = _rtl92c_phy_get_rightchnlplace(channel);
  856. _rtl92d_get_txpower_index(hw, channel, &cckpowerlevel[0],
  857. &ofdmpowerlevel[0]);
  858. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  859. _rtl92d_ccxpower_index_check(hw, channel, &cckpowerlevel[0],
  860. &ofdmpowerlevel[0]);
  861. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  862. rtl92d_phy_rf6052_set_cck_txpower(hw, &cckpowerlevel[0]);
  863. rtl92d_phy_rf6052_set_ofdm_txpower(hw, &ofdmpowerlevel[0], channel);
  864. }
  865. void rtl92d_phy_set_bw_mode(struct ieee80211_hw *hw,
  866. enum nl80211_channel_type ch_type)
  867. {
  868. struct rtl_priv *rtlpriv = rtl_priv(hw);
  869. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  870. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  871. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  872. unsigned long flag = 0;
  873. u8 reg_prsr_rsc;
  874. u8 reg_bw_opmode;
  875. if (rtlphy->set_bwmode_inprogress)
  876. return;
  877. if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
  878. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  879. "FALSE driver sleep or unload\n");
  880. return;
  881. }
  882. rtlphy->set_bwmode_inprogress = true;
  883. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "Switch to %s bandwidth\n",
  884. rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
  885. "20MHz" : "40MHz");
  886. reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
  887. reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
  888. switch (rtlphy->current_chan_bw) {
  889. case HT_CHANNEL_WIDTH_20:
  890. reg_bw_opmode |= BW_OPMODE_20MHZ;
  891. rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
  892. break;
  893. case HT_CHANNEL_WIDTH_20_40:
  894. reg_bw_opmode &= ~BW_OPMODE_20MHZ;
  895. rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
  896. reg_prsr_rsc = (reg_prsr_rsc & 0x90) |
  897. (mac->cur_40_prime_sc << 5);
  898. rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
  899. break;
  900. default:
  901. pr_err("unknown bandwidth: %#X\n",
  902. rtlphy->current_chan_bw);
  903. break;
  904. }
  905. switch (rtlphy->current_chan_bw) {
  906. case HT_CHANNEL_WIDTH_20:
  907. rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
  908. rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
  909. /* SET BIT10 BIT11 for receive cck */
  910. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10) |
  911. BIT(11), 3);
  912. break;
  913. case HT_CHANNEL_WIDTH_20_40:
  914. rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
  915. rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
  916. /* Set Control channel to upper or lower.
  917. * These settings are required only for 40MHz */
  918. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  919. rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
  920. rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCKSIDEBAND,
  921. (mac->cur_40_prime_sc >> 1));
  922. rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
  923. }
  924. rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
  925. /* SET BIT10 BIT11 for receive cck */
  926. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10) |
  927. BIT(11), 0);
  928. rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
  929. (mac->cur_40_prime_sc ==
  930. HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
  931. break;
  932. default:
  933. pr_err("unknown bandwidth: %#X\n",
  934. rtlphy->current_chan_bw);
  935. break;
  936. }
  937. rtl92d_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
  938. rtlphy->set_bwmode_inprogress = false;
  939. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "<==\n");
  940. }
  941. static void _rtl92d_phy_stop_trx_before_changeband(struct ieee80211_hw *hw)
  942. {
  943. rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0);
  944. rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0);
  945. rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x00);
  946. rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x0);
  947. }
  948. static void rtl92d_phy_switch_wirelessband(struct ieee80211_hw *hw, u8 band)
  949. {
  950. struct rtl_priv *rtlpriv = rtl_priv(hw);
  951. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  952. u8 value8;
  953. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "==>\n");
  954. rtlhal->bandset = band;
  955. rtlhal->current_bandtype = band;
  956. if (IS_92D_SINGLEPHY(rtlhal->version))
  957. rtlhal->bandset = BAND_ON_BOTH;
  958. /* stop RX/Tx */
  959. _rtl92d_phy_stop_trx_before_changeband(hw);
  960. /* reconfig BB/RF according to wireless mode */
  961. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  962. /* BB & RF Config */
  963. RT_TRACE(rtlpriv, COMP_CMD, DBG_DMESG, "====>2.4G\n");
  964. if (rtlhal->interfaceindex == 1)
  965. _rtl92d_phy_config_bb_with_headerfile(hw,
  966. BASEBAND_CONFIG_AGC_TAB);
  967. } else {
  968. /* 5G band */
  969. RT_TRACE(rtlpriv, COMP_CMD, DBG_DMESG, "====>5G\n");
  970. if (rtlhal->interfaceindex == 1)
  971. _rtl92d_phy_config_bb_with_headerfile(hw,
  972. BASEBAND_CONFIG_AGC_TAB);
  973. }
  974. rtl92d_update_bbrf_configuration(hw);
  975. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  976. rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0x1);
  977. rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0x1);
  978. /* 20M BW. */
  979. /* rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1); */
  980. rtlhal->reloadtxpowerindex = true;
  981. /* notice fw know band status 0x81[1]/0x53[1] = 0: 5G, 1: 2G */
  982. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  983. value8 = rtl_read_byte(rtlpriv, (rtlhal->interfaceindex ==
  984. 0 ? REG_MAC0 : REG_MAC1));
  985. value8 |= BIT(1);
  986. rtl_write_byte(rtlpriv, (rtlhal->interfaceindex ==
  987. 0 ? REG_MAC0 : REG_MAC1), value8);
  988. } else {
  989. value8 = rtl_read_byte(rtlpriv, (rtlhal->interfaceindex ==
  990. 0 ? REG_MAC0 : REG_MAC1));
  991. value8 &= (~BIT(1));
  992. rtl_write_byte(rtlpriv, (rtlhal->interfaceindex ==
  993. 0 ? REG_MAC0 : REG_MAC1), value8);
  994. }
  995. mdelay(1);
  996. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "<==Switch Band OK\n");
  997. }
  998. static void _rtl92d_phy_reload_imr_setting(struct ieee80211_hw *hw,
  999. u8 channel, u8 rfpath)
  1000. {
  1001. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1002. u32 imr_num = MAX_RF_IMR_INDEX;
  1003. u32 rfmask = RFREG_OFFSET_MASK;
  1004. u8 group, i;
  1005. unsigned long flag = 0;
  1006. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>path %d\n", rfpath);
  1007. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G) {
  1008. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>5G\n");
  1009. rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(25) | BIT(24), 0);
  1010. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0x00f00000, 0xf);
  1011. /* fc area 0xd2c */
  1012. if (channel > 99)
  1013. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(13) |
  1014. BIT(14), 2);
  1015. else
  1016. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(13) |
  1017. BIT(14), 1);
  1018. /* leave 0 for channel1-14. */
  1019. group = channel <= 64 ? 1 : 2;
  1020. imr_num = MAX_RF_IMR_INDEX_NORMAL;
  1021. for (i = 0; i < imr_num; i++)
  1022. rtl_set_rfreg(hw, (enum radio_path)rfpath,
  1023. rf_reg_for_5g_swchnl_normal[i], rfmask,
  1024. rf_imr_param_normal[0][group][i]);
  1025. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0x00f00000, 0);
  1026. rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 1);
  1027. } else {
  1028. /* G band. */
  1029. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
  1030. "Load RF IMR parameters for G band. IMR already setting %d\n",
  1031. rtlpriv->rtlhal.load_imrandiqk_setting_for2g);
  1032. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>2.4G\n");
  1033. if (!rtlpriv->rtlhal.load_imrandiqk_setting_for2g) {
  1034. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
  1035. "Load RF IMR parameters for G band. %d\n",
  1036. rfpath);
  1037. rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
  1038. rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(25) | BIT(24), 0);
  1039. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4,
  1040. 0x00f00000, 0xf);
  1041. imr_num = MAX_RF_IMR_INDEX_NORMAL;
  1042. for (i = 0; i < imr_num; i++) {
  1043. rtl_set_rfreg(hw, (enum radio_path)rfpath,
  1044. rf_reg_for_5g_swchnl_normal[i],
  1045. RFREG_OFFSET_MASK,
  1046. rf_imr_param_normal[0][0][i]);
  1047. }
  1048. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4,
  1049. 0x00f00000, 0);
  1050. rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN | BCCKEN, 3);
  1051. rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
  1052. }
  1053. }
  1054. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
  1055. }
  1056. static void _rtl92d_phy_enable_rf_env(struct ieee80211_hw *hw,
  1057. u8 rfpath, u32 *pu4_regval)
  1058. {
  1059. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1060. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1061. struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
  1062. RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "====>\n");
  1063. /*----Store original RFENV control type----*/
  1064. switch (rfpath) {
  1065. case RF90_PATH_A:
  1066. case RF90_PATH_C:
  1067. *pu4_regval = rtl_get_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV);
  1068. break;
  1069. case RF90_PATH_B:
  1070. case RF90_PATH_D:
  1071. *pu4_regval =
  1072. rtl_get_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16);
  1073. break;
  1074. }
  1075. /*----Set RF_ENV enable----*/
  1076. rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
  1077. udelay(1);
  1078. /*----Set RF_ENV output high----*/
  1079. rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
  1080. udelay(1);
  1081. /* Set bit number of Address and Data for RF register */
  1082. /* Set 1 to 4 bits for 8255 */
  1083. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREADDRESSLENGTH, 0x0);
  1084. udelay(1);
  1085. /*Set 0 to 12 bits for 8255 */
  1086. rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
  1087. udelay(1);
  1088. RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "<====\n");
  1089. }
  1090. static void _rtl92d_phy_restore_rf_env(struct ieee80211_hw *hw, u8 rfpath,
  1091. u32 *pu4_regval)
  1092. {
  1093. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1094. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1095. struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
  1096. RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "=====>\n");
  1097. /*----Restore RFENV control type----*/
  1098. switch (rfpath) {
  1099. case RF90_PATH_A:
  1100. case RF90_PATH_C:
  1101. rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV, *pu4_regval);
  1102. break;
  1103. case RF90_PATH_B:
  1104. case RF90_PATH_D:
  1105. rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16,
  1106. *pu4_regval);
  1107. break;
  1108. }
  1109. RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "<=====\n");
  1110. }
  1111. static void _rtl92d_phy_switch_rf_setting(struct ieee80211_hw *hw, u8 channel)
  1112. {
  1113. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1114. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1115. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  1116. u8 path = rtlhal->current_bandtype ==
  1117. BAND_ON_5G ? RF90_PATH_A : RF90_PATH_B;
  1118. u8 index = 0, i = 0, rfpath = RF90_PATH_A;
  1119. bool need_pwr_down = false, internal_pa = false;
  1120. u32 u4regvalue, mask = 0x1C000, value = 0, u4tmp, u4tmp2;
  1121. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>\n");
  1122. /* config path A for 5G */
  1123. if (rtlhal->current_bandtype == BAND_ON_5G) {
  1124. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>5G\n");
  1125. u4tmp = curveindex_5g[channel - 1];
  1126. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1127. "ver 1 set RF-A, 5G, 0x28 = 0x%x !!\n", u4tmp);
  1128. for (i = 0; i < RF_CHNL_NUM_5G; i++) {
  1129. if (channel == rf_chnl_5g[i] && channel <= 140)
  1130. index = 0;
  1131. }
  1132. for (i = 0; i < RF_CHNL_NUM_5G_40M; i++) {
  1133. if (channel == rf_chnl_5g_40m[i] && channel <= 140)
  1134. index = 1;
  1135. }
  1136. if (channel == 149 || channel == 155 || channel == 161)
  1137. index = 2;
  1138. else if (channel == 151 || channel == 153 || channel == 163
  1139. || channel == 165)
  1140. index = 3;
  1141. else if (channel == 157 || channel == 159)
  1142. index = 4;
  1143. if (rtlhal->macphymode == DUALMAC_DUALPHY
  1144. && rtlhal->interfaceindex == 1) {
  1145. need_pwr_down = rtl92d_phy_enable_anotherphy(hw, false);
  1146. rtlhal->during_mac1init_radioa = true;
  1147. /* asume no this case */
  1148. if (need_pwr_down)
  1149. _rtl92d_phy_enable_rf_env(hw, path,
  1150. &u4regvalue);
  1151. }
  1152. for (i = 0; i < RF_REG_NUM_FOR_C_CUT_5G; i++) {
  1153. if (i == 0 && (rtlhal->macphymode == DUALMAC_DUALPHY)) {
  1154. rtl_set_rfreg(hw, (enum radio_path)path,
  1155. rf_reg_for_c_cut_5g[i],
  1156. RFREG_OFFSET_MASK, 0xE439D);
  1157. } else if (rf_reg_for_c_cut_5g[i] == RF_SYN_G4) {
  1158. u4tmp2 = (rf_reg_pram_c_5g[index][i] &
  1159. 0x7FF) | (u4tmp << 11);
  1160. if (channel == 36)
  1161. u4tmp2 &= ~(BIT(7) | BIT(6));
  1162. rtl_set_rfreg(hw, (enum radio_path)path,
  1163. rf_reg_for_c_cut_5g[i],
  1164. RFREG_OFFSET_MASK, u4tmp2);
  1165. } else {
  1166. rtl_set_rfreg(hw, (enum radio_path)path,
  1167. rf_reg_for_c_cut_5g[i],
  1168. RFREG_OFFSET_MASK,
  1169. rf_reg_pram_c_5g[index][i]);
  1170. }
  1171. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  1172. "offset 0x%x value 0x%x path %d index %d readback 0x%x\n",
  1173. rf_reg_for_c_cut_5g[i],
  1174. rf_reg_pram_c_5g[index][i],
  1175. path, index,
  1176. rtl_get_rfreg(hw, (enum radio_path)path,
  1177. rf_reg_for_c_cut_5g[i],
  1178. RFREG_OFFSET_MASK));
  1179. }
  1180. if (need_pwr_down)
  1181. _rtl92d_phy_restore_rf_env(hw, path, &u4regvalue);
  1182. if (rtlhal->during_mac1init_radioa)
  1183. rtl92d_phy_powerdown_anotherphy(hw, false);
  1184. if (channel < 149)
  1185. value = 0x07;
  1186. else if (channel >= 149)
  1187. value = 0x02;
  1188. if (channel >= 36 && channel <= 64)
  1189. index = 0;
  1190. else if (channel >= 100 && channel <= 140)
  1191. index = 1;
  1192. else
  1193. index = 2;
  1194. for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
  1195. rfpath++) {
  1196. if (rtlhal->macphymode == DUALMAC_DUALPHY &&
  1197. rtlhal->interfaceindex == 1) /* MAC 1 5G */
  1198. internal_pa = rtlpriv->efuse.internal_pa_5g[1];
  1199. else
  1200. internal_pa =
  1201. rtlpriv->efuse.internal_pa_5g[rfpath];
  1202. if (internal_pa) {
  1203. for (i = 0;
  1204. i < RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA;
  1205. i++) {
  1206. rtl_set_rfreg(hw, rfpath,
  1207. rf_for_c_cut_5g_internal_pa[i],
  1208. RFREG_OFFSET_MASK,
  1209. rf_pram_c_5g_int_pa[index][i]);
  1210. RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD,
  1211. "offset 0x%x value 0x%x path %d index %d\n",
  1212. rf_for_c_cut_5g_internal_pa[i],
  1213. rf_pram_c_5g_int_pa[index][i],
  1214. rfpath, index);
  1215. }
  1216. } else {
  1217. rtl_set_rfreg(hw, (enum radio_path)rfpath, 0x0B,
  1218. mask, value);
  1219. }
  1220. }
  1221. } else if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  1222. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>2.4G\n");
  1223. u4tmp = curveindex_2g[channel - 1];
  1224. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1225. "ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n", u4tmp);
  1226. if (channel == 1 || channel == 2 || channel == 4 || channel == 9
  1227. || channel == 10 || channel == 11 || channel == 12)
  1228. index = 0;
  1229. else if (channel == 3 || channel == 13 || channel == 14)
  1230. index = 1;
  1231. else if (channel >= 5 && channel <= 8)
  1232. index = 2;
  1233. if (rtlhal->macphymode == DUALMAC_DUALPHY) {
  1234. path = RF90_PATH_A;
  1235. if (rtlhal->interfaceindex == 0) {
  1236. need_pwr_down =
  1237. rtl92d_phy_enable_anotherphy(hw, true);
  1238. rtlhal->during_mac0init_radiob = true;
  1239. if (need_pwr_down)
  1240. _rtl92d_phy_enable_rf_env(hw, path,
  1241. &u4regvalue);
  1242. }
  1243. }
  1244. for (i = 0; i < RF_REG_NUM_FOR_C_CUT_2G; i++) {
  1245. if (rf_reg_for_c_cut_2g[i] == RF_SYN_G7)
  1246. rtl_set_rfreg(hw, (enum radio_path)path,
  1247. rf_reg_for_c_cut_2g[i],
  1248. RFREG_OFFSET_MASK,
  1249. (rf_reg_param_for_c_cut_2g[index][i] |
  1250. BIT(17)));
  1251. else
  1252. rtl_set_rfreg(hw, (enum radio_path)path,
  1253. rf_reg_for_c_cut_2g[i],
  1254. RFREG_OFFSET_MASK,
  1255. rf_reg_param_for_c_cut_2g
  1256. [index][i]);
  1257. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  1258. "offset 0x%x value 0x%x mak 0x%x path %d index %d readback 0x%x\n",
  1259. rf_reg_for_c_cut_2g[i],
  1260. rf_reg_param_for_c_cut_2g[index][i],
  1261. rf_reg_mask_for_c_cut_2g[i], path, index,
  1262. rtl_get_rfreg(hw, (enum radio_path)path,
  1263. rf_reg_for_c_cut_2g[i],
  1264. RFREG_OFFSET_MASK));
  1265. }
  1266. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1267. "cosa ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n",
  1268. rf_syn_g4_for_c_cut_2g | (u4tmp << 11));
  1269. rtl_set_rfreg(hw, (enum radio_path)path, RF_SYN_G4,
  1270. RFREG_OFFSET_MASK,
  1271. rf_syn_g4_for_c_cut_2g | (u4tmp << 11));
  1272. if (need_pwr_down)
  1273. _rtl92d_phy_restore_rf_env(hw, path, &u4regvalue);
  1274. if (rtlhal->during_mac0init_radiob)
  1275. rtl92d_phy_powerdown_anotherphy(hw, true);
  1276. }
  1277. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
  1278. }
  1279. u8 rtl92d_get_rightchnlplace_for_iqk(u8 chnl)
  1280. {
  1281. u8 channel_all[59] = {
  1282. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  1283. 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58,
  1284. 60, 62, 64, 100, 102, 104, 106, 108, 110, 112,
  1285. 114, 116, 118, 120, 122, 124, 126, 128, 130,
  1286. 132, 134, 136, 138, 140, 149, 151, 153, 155,
  1287. 157, 159, 161, 163, 165
  1288. };
  1289. u8 place = chnl;
  1290. if (chnl > 14) {
  1291. for (place = 14; place < sizeof(channel_all); place++) {
  1292. if (channel_all[place] == chnl)
  1293. return place - 13;
  1294. }
  1295. }
  1296. return 0;
  1297. }
  1298. #define MAX_TOLERANCE 5
  1299. #define IQK_DELAY_TIME 1 /* ms */
  1300. #define MAX_TOLERANCE_92D 3
  1301. /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
  1302. static u8 _rtl92d_phy_patha_iqk(struct ieee80211_hw *hw, bool configpathb)
  1303. {
  1304. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1305. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1306. u32 regeac, rege94, rege9c, regea4;
  1307. u8 result = 0;
  1308. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path A IQK!\n");
  1309. /* path-A IQK setting */
  1310. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path-A IQK setting!\n");
  1311. if (rtlhal->interfaceindex == 0) {
  1312. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x10008c1f);
  1313. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x10008c1f);
  1314. } else {
  1315. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x10008c22);
  1316. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x10008c22);
  1317. }
  1318. rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82140102);
  1319. rtl_set_bbreg(hw, 0xe3c, MASKDWORD, 0x28160206);
  1320. /* path-B IQK setting */
  1321. if (configpathb) {
  1322. rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x10008c22);
  1323. rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x10008c22);
  1324. rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82140102);
  1325. rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x28160206);
  1326. }
  1327. /* LO calibration setting */
  1328. RTPRINT(rtlpriv, FINIT, INIT_IQK, "LO calibration setting!\n");
  1329. rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x00462911);
  1330. /* One shot, path A LOK & IQK */
  1331. RTPRINT(rtlpriv, FINIT, INIT_IQK, "One shot, path A LOK & IQK!\n");
  1332. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf9000000);
  1333. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
  1334. /* delay x ms */
  1335. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1336. "Delay %d ms for One shot, path A LOK & IQK\n",
  1337. IQK_DELAY_TIME);
  1338. mdelay(IQK_DELAY_TIME);
  1339. /* Check failed */
  1340. regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  1341. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xeac = 0x%x\n", regeac);
  1342. rege94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
  1343. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xe94 = 0x%x\n", rege94);
  1344. rege9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
  1345. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xe9c = 0x%x\n", rege9c);
  1346. regea4 = rtl_get_bbreg(hw, 0xea4, MASKDWORD);
  1347. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xea4 = 0x%x\n", regea4);
  1348. if (!(regeac & BIT(28)) && (((rege94 & 0x03FF0000) >> 16) != 0x142) &&
  1349. (((rege9c & 0x03FF0000) >> 16) != 0x42))
  1350. result |= 0x01;
  1351. else /* if Tx not OK, ignore Rx */
  1352. return result;
  1353. /* if Tx is OK, check whether Rx is OK */
  1354. if (!(regeac & BIT(27)) && (((regea4 & 0x03FF0000) >> 16) != 0x132) &&
  1355. (((regeac & 0x03FF0000) >> 16) != 0x36))
  1356. result |= 0x02;
  1357. else
  1358. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path A Rx IQK fail!!\n");
  1359. return result;
  1360. }
  1361. /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
  1362. static u8 _rtl92d_phy_patha_iqk_5g_normal(struct ieee80211_hw *hw,
  1363. bool configpathb)
  1364. {
  1365. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1366. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1367. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1368. u32 regeac, rege94, rege9c, regea4;
  1369. u8 result = 0;
  1370. u8 i;
  1371. u8 retrycount = 2;
  1372. u32 TxOKBit = BIT(28), RxOKBit = BIT(27);
  1373. if (rtlhal->interfaceindex == 1) { /* PHY1 */
  1374. TxOKBit = BIT(31);
  1375. RxOKBit = BIT(30);
  1376. }
  1377. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path A IQK!\n");
  1378. /* path-A IQK setting */
  1379. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path-A IQK setting!\n");
  1380. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x18008c1f);
  1381. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x18008c1f);
  1382. rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82140307);
  1383. rtl_set_bbreg(hw, 0xe3c, MASKDWORD, 0x68160960);
  1384. /* path-B IQK setting */
  1385. if (configpathb) {
  1386. rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x18008c2f);
  1387. rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x18008c2f);
  1388. rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82110000);
  1389. rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x68110000);
  1390. }
  1391. /* LO calibration setting */
  1392. RTPRINT(rtlpriv, FINIT, INIT_IQK, "LO calibration setting!\n");
  1393. rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x00462911);
  1394. /* path-A PA on */
  1395. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD, 0x07000f60);
  1396. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, MASKDWORD, 0x66e60e30);
  1397. for (i = 0; i < retrycount; i++) {
  1398. /* One shot, path A LOK & IQK */
  1399. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1400. "One shot, path A LOK & IQK!\n");
  1401. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf9000000);
  1402. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
  1403. /* delay x ms */
  1404. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1405. "Delay %d ms for One shot, path A LOK & IQK.\n",
  1406. IQK_DELAY_TIME);
  1407. mdelay(IQK_DELAY_TIME * 10);
  1408. /* Check failed */
  1409. regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  1410. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xeac = 0x%x\n", regeac);
  1411. rege94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
  1412. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xe94 = 0x%x\n", rege94);
  1413. rege9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
  1414. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xe9c = 0x%x\n", rege9c);
  1415. regea4 = rtl_get_bbreg(hw, 0xea4, MASKDWORD);
  1416. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xea4 = 0x%x\n", regea4);
  1417. if (!(regeac & TxOKBit) &&
  1418. (((rege94 & 0x03FF0000) >> 16) != 0x142)) {
  1419. result |= 0x01;
  1420. } else { /* if Tx not OK, ignore Rx */
  1421. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1422. "Path A Tx IQK fail!!\n");
  1423. continue;
  1424. }
  1425. /* if Tx is OK, check whether Rx is OK */
  1426. if (!(regeac & RxOKBit) &&
  1427. (((regea4 & 0x03FF0000) >> 16) != 0x132)) {
  1428. result |= 0x02;
  1429. break;
  1430. } else {
  1431. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1432. "Path A Rx IQK fail!!\n");
  1433. }
  1434. }
  1435. /* path A PA off */
  1436. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD,
  1437. rtlphy->iqk_bb_backup[0]);
  1438. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, MASKDWORD,
  1439. rtlphy->iqk_bb_backup[1]);
  1440. return result;
  1441. }
  1442. /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
  1443. static u8 _rtl92d_phy_pathb_iqk(struct ieee80211_hw *hw)
  1444. {
  1445. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1446. u32 regeac, regeb4, regebc, regec4, regecc;
  1447. u8 result = 0;
  1448. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path B IQK!\n");
  1449. /* One shot, path B LOK & IQK */
  1450. RTPRINT(rtlpriv, FINIT, INIT_IQK, "One shot, path A LOK & IQK!\n");
  1451. rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0x00000002);
  1452. rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0x00000000);
  1453. /* delay x ms */
  1454. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1455. "Delay %d ms for One shot, path B LOK & IQK\n", IQK_DELAY_TIME);
  1456. mdelay(IQK_DELAY_TIME);
  1457. /* Check failed */
  1458. regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  1459. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xeac = 0x%x\n", regeac);
  1460. regeb4 = rtl_get_bbreg(hw, 0xeb4, MASKDWORD);
  1461. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xeb4 = 0x%x\n", regeb4);
  1462. regebc = rtl_get_bbreg(hw, 0xebc, MASKDWORD);
  1463. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xebc = 0x%x\n", regebc);
  1464. regec4 = rtl_get_bbreg(hw, 0xec4, MASKDWORD);
  1465. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xec4 = 0x%x\n", regec4);
  1466. regecc = rtl_get_bbreg(hw, 0xecc, MASKDWORD);
  1467. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xecc = 0x%x\n", regecc);
  1468. if (!(regeac & BIT(31)) && (((regeb4 & 0x03FF0000) >> 16) != 0x142) &&
  1469. (((regebc & 0x03FF0000) >> 16) != 0x42))
  1470. result |= 0x01;
  1471. else
  1472. return result;
  1473. if (!(regeac & BIT(30)) && (((regec4 & 0x03FF0000) >> 16) != 0x132) &&
  1474. (((regecc & 0x03FF0000) >> 16) != 0x36))
  1475. result |= 0x02;
  1476. else
  1477. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path B Rx IQK fail!!\n");
  1478. return result;
  1479. }
  1480. /* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
  1481. static u8 _rtl92d_phy_pathb_iqk_5g_normal(struct ieee80211_hw *hw)
  1482. {
  1483. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1484. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1485. u32 regeac, regeb4, regebc, regec4, regecc;
  1486. u8 result = 0;
  1487. u8 i;
  1488. u8 retrycount = 2;
  1489. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path B IQK!\n");
  1490. /* path-A IQK setting */
  1491. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path-A IQK setting!\n");
  1492. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x18008c1f);
  1493. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x18008c1f);
  1494. rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82110000);
  1495. rtl_set_bbreg(hw, 0xe3c, MASKDWORD, 0x68110000);
  1496. /* path-B IQK setting */
  1497. rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x18008c2f);
  1498. rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x18008c2f);
  1499. rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82140307);
  1500. rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x68160960);
  1501. /* LO calibration setting */
  1502. RTPRINT(rtlpriv, FINIT, INIT_IQK, "LO calibration setting!\n");
  1503. rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x00462911);
  1504. /* path-B PA on */
  1505. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD, 0x0f600700);
  1506. rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, MASKDWORD, 0x061f0d30);
  1507. for (i = 0; i < retrycount; i++) {
  1508. /* One shot, path B LOK & IQK */
  1509. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1510. "One shot, path A LOK & IQK!\n");
  1511. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xfa000000);
  1512. rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
  1513. /* delay x ms */
  1514. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1515. "Delay %d ms for One shot, path B LOK & IQK.\n", 10);
  1516. mdelay(IQK_DELAY_TIME * 10);
  1517. /* Check failed */
  1518. regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
  1519. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xeac = 0x%x\n", regeac);
  1520. regeb4 = rtl_get_bbreg(hw, 0xeb4, MASKDWORD);
  1521. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xeb4 = 0x%x\n", regeb4);
  1522. regebc = rtl_get_bbreg(hw, 0xebc, MASKDWORD);
  1523. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xebc = 0x%x\n", regebc);
  1524. regec4 = rtl_get_bbreg(hw, 0xec4, MASKDWORD);
  1525. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xec4 = 0x%x\n", regec4);
  1526. regecc = rtl_get_bbreg(hw, 0xecc, MASKDWORD);
  1527. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xecc = 0x%x\n", regecc);
  1528. if (!(regeac & BIT(31)) &&
  1529. (((regeb4 & 0x03FF0000) >> 16) != 0x142))
  1530. result |= 0x01;
  1531. else
  1532. continue;
  1533. if (!(regeac & BIT(30)) &&
  1534. (((regec4 & 0x03FF0000) >> 16) != 0x132)) {
  1535. result |= 0x02;
  1536. break;
  1537. } else {
  1538. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1539. "Path B Rx IQK fail!!\n");
  1540. }
  1541. }
  1542. /* path B PA off */
  1543. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD,
  1544. rtlphy->iqk_bb_backup[0]);
  1545. rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, MASKDWORD,
  1546. rtlphy->iqk_bb_backup[2]);
  1547. return result;
  1548. }
  1549. static void _rtl92d_phy_save_adda_registers(struct ieee80211_hw *hw,
  1550. u32 *adda_reg, u32 *adda_backup,
  1551. u32 regnum)
  1552. {
  1553. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1554. u32 i;
  1555. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Save ADDA parameters.\n");
  1556. for (i = 0; i < regnum; i++)
  1557. adda_backup[i] = rtl_get_bbreg(hw, adda_reg[i], MASKDWORD);
  1558. }
  1559. static void _rtl92d_phy_save_mac_registers(struct ieee80211_hw *hw,
  1560. u32 *macreg, u32 *macbackup)
  1561. {
  1562. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1563. u32 i;
  1564. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Save MAC parameters.\n");
  1565. for (i = 0; i < (IQK_MAC_REG_NUM - 1); i++)
  1566. macbackup[i] = rtl_read_byte(rtlpriv, macreg[i]);
  1567. macbackup[i] = rtl_read_dword(rtlpriv, macreg[i]);
  1568. }
  1569. static void _rtl92d_phy_reload_adda_registers(struct ieee80211_hw *hw,
  1570. u32 *adda_reg, u32 *adda_backup,
  1571. u32 regnum)
  1572. {
  1573. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1574. u32 i;
  1575. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1576. "Reload ADDA power saving parameters !\n");
  1577. for (i = 0; i < regnum; i++)
  1578. rtl_set_bbreg(hw, adda_reg[i], MASKDWORD, adda_backup[i]);
  1579. }
  1580. static void _rtl92d_phy_reload_mac_registers(struct ieee80211_hw *hw,
  1581. u32 *macreg, u32 *macbackup)
  1582. {
  1583. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1584. u32 i;
  1585. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Reload MAC parameters !\n");
  1586. for (i = 0; i < (IQK_MAC_REG_NUM - 1); i++)
  1587. rtl_write_byte(rtlpriv, macreg[i], (u8) macbackup[i]);
  1588. rtl_write_byte(rtlpriv, macreg[i], macbackup[i]);
  1589. }
  1590. static void _rtl92d_phy_path_adda_on(struct ieee80211_hw *hw,
  1591. u32 *adda_reg, bool patha_on, bool is2t)
  1592. {
  1593. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1594. u32 pathon;
  1595. u32 i;
  1596. RTPRINT(rtlpriv, FINIT, INIT_IQK, "ADDA ON.\n");
  1597. pathon = patha_on ? 0x04db25a4 : 0x0b1b25a4;
  1598. if (patha_on)
  1599. pathon = rtlpriv->rtlhal.interfaceindex == 0 ?
  1600. 0x04db25a4 : 0x0b1b25a4;
  1601. for (i = 0; i < IQK_ADDA_REG_NUM; i++)
  1602. rtl_set_bbreg(hw, adda_reg[i], MASKDWORD, pathon);
  1603. }
  1604. static void _rtl92d_phy_mac_setting_calibration(struct ieee80211_hw *hw,
  1605. u32 *macreg, u32 *macbackup)
  1606. {
  1607. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1608. u32 i;
  1609. RTPRINT(rtlpriv, FINIT, INIT_IQK, "MAC settings for Calibration.\n");
  1610. rtl_write_byte(rtlpriv, macreg[0], 0x3F);
  1611. for (i = 1; i < (IQK_MAC_REG_NUM - 1); i++)
  1612. rtl_write_byte(rtlpriv, macreg[i], (u8)(macbackup[i] &
  1613. (~BIT(3))));
  1614. rtl_write_byte(rtlpriv, macreg[i], (u8) (macbackup[i] & (~BIT(5))));
  1615. }
  1616. static void _rtl92d_phy_patha_standby(struct ieee80211_hw *hw)
  1617. {
  1618. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1619. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path-A standby mode!\n");
  1620. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x0);
  1621. rtl_set_bbreg(hw, RFPGA0_XA_LSSIPARAMETER, MASKDWORD, 0x00010000);
  1622. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
  1623. }
  1624. static void _rtl92d_phy_pimode_switch(struct ieee80211_hw *hw, bool pi_mode)
  1625. {
  1626. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1627. u32 mode;
  1628. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1629. "BB Switch to %s mode!\n", pi_mode ? "PI" : "SI");
  1630. mode = pi_mode ? 0x01000100 : 0x01000000;
  1631. rtl_set_bbreg(hw, 0x820, MASKDWORD, mode);
  1632. rtl_set_bbreg(hw, 0x828, MASKDWORD, mode);
  1633. }
  1634. static void _rtl92d_phy_iq_calibrate(struct ieee80211_hw *hw, long result[][8],
  1635. u8 t, bool is2t)
  1636. {
  1637. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1638. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1639. u32 i;
  1640. u8 patha_ok, pathb_ok;
  1641. static u32 adda_reg[IQK_ADDA_REG_NUM] = {
  1642. RFPGA0_XCD_SWITCHCONTROL, 0xe6c, 0xe70, 0xe74,
  1643. 0xe78, 0xe7c, 0xe80, 0xe84,
  1644. 0xe88, 0xe8c, 0xed0, 0xed4,
  1645. 0xed8, 0xedc, 0xee0, 0xeec
  1646. };
  1647. static u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
  1648. 0x522, 0x550, 0x551, 0x040
  1649. };
  1650. static u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
  1651. RFPGA0_XAB_RFINTERFACESW, RFPGA0_XA_RFINTERFACEOE,
  1652. RFPGA0_XB_RFINTERFACEOE, ROFDM0_TRMUXPAR,
  1653. RFPGA0_XCD_RFINTERFACESW, ROFDM0_TRXPATHENABLE,
  1654. RFPGA0_RFMOD, RFPGA0_ANALOGPARAMETER4,
  1655. ROFDM0_XAAGCCORE1, ROFDM0_XBAGCCORE1
  1656. };
  1657. const u32 retrycount = 2;
  1658. u32 bbvalue;
  1659. RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQK for 2.4G :Start!!!\n");
  1660. if (t == 0) {
  1661. bbvalue = rtl_get_bbreg(hw, RFPGA0_RFMOD, MASKDWORD);
  1662. RTPRINT(rtlpriv, FINIT, INIT_IQK, "==>0x%08x\n", bbvalue);
  1663. RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQ Calibration for %s\n",
  1664. is2t ? "2T2R" : "1T1R");
  1665. /* Save ADDA parameters, turn Path A ADDA on */
  1666. _rtl92d_phy_save_adda_registers(hw, adda_reg,
  1667. rtlphy->adda_backup, IQK_ADDA_REG_NUM);
  1668. _rtl92d_phy_save_mac_registers(hw, iqk_mac_reg,
  1669. rtlphy->iqk_mac_backup);
  1670. _rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
  1671. rtlphy->iqk_bb_backup, IQK_BB_REG_NUM);
  1672. }
  1673. _rtl92d_phy_path_adda_on(hw, adda_reg, true, is2t);
  1674. if (t == 0)
  1675. rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
  1676. RFPGA0_XA_HSSIPARAMETER1, BIT(8));
  1677. /* Switch BB to PI mode to do IQ Calibration. */
  1678. if (!rtlphy->rfpi_enable)
  1679. _rtl92d_phy_pimode_switch(hw, true);
  1680. rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(24), 0x00);
  1681. rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKDWORD, 0x03a05600);
  1682. rtl_set_bbreg(hw, ROFDM0_TRMUXPAR, MASKDWORD, 0x000800e4);
  1683. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW, MASKDWORD, 0x22204000);
  1684. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xf00000, 0x0f);
  1685. if (is2t) {
  1686. rtl_set_bbreg(hw, RFPGA0_XA_LSSIPARAMETER, MASKDWORD,
  1687. 0x00010000);
  1688. rtl_set_bbreg(hw, RFPGA0_XB_LSSIPARAMETER, MASKDWORD,
  1689. 0x00010000);
  1690. }
  1691. /* MAC settings */
  1692. _rtl92d_phy_mac_setting_calibration(hw, iqk_mac_reg,
  1693. rtlphy->iqk_mac_backup);
  1694. /* Page B init */
  1695. rtl_set_bbreg(hw, 0xb68, MASKDWORD, 0x0f600000);
  1696. if (is2t)
  1697. rtl_set_bbreg(hw, 0xb6c, MASKDWORD, 0x0f600000);
  1698. /* IQ calibration setting */
  1699. RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQK setting!\n");
  1700. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
  1701. rtl_set_bbreg(hw, 0xe40, MASKDWORD, 0x01007c00);
  1702. rtl_set_bbreg(hw, 0xe44, MASKDWORD, 0x01004800);
  1703. for (i = 0; i < retrycount; i++) {
  1704. patha_ok = _rtl92d_phy_patha_iqk(hw, is2t);
  1705. if (patha_ok == 0x03) {
  1706. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1707. "Path A IQK Success!!\n");
  1708. result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
  1709. 0x3FF0000) >> 16;
  1710. result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
  1711. 0x3FF0000) >> 16;
  1712. result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
  1713. 0x3FF0000) >> 16;
  1714. result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
  1715. 0x3FF0000) >> 16;
  1716. break;
  1717. } else if (i == (retrycount - 1) && patha_ok == 0x01) {
  1718. /* Tx IQK OK */
  1719. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1720. "Path A IQK Only Tx Success!!\n");
  1721. result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
  1722. 0x3FF0000) >> 16;
  1723. result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
  1724. 0x3FF0000) >> 16;
  1725. }
  1726. }
  1727. if (0x00 == patha_ok)
  1728. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path A IQK failed!!\n");
  1729. if (is2t) {
  1730. _rtl92d_phy_patha_standby(hw);
  1731. /* Turn Path B ADDA on */
  1732. _rtl92d_phy_path_adda_on(hw, adda_reg, false, is2t);
  1733. for (i = 0; i < retrycount; i++) {
  1734. pathb_ok = _rtl92d_phy_pathb_iqk(hw);
  1735. if (pathb_ok == 0x03) {
  1736. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1737. "Path B IQK Success!!\n");
  1738. result[t][4] = (rtl_get_bbreg(hw, 0xeb4,
  1739. MASKDWORD) & 0x3FF0000) >> 16;
  1740. result[t][5] = (rtl_get_bbreg(hw, 0xebc,
  1741. MASKDWORD) & 0x3FF0000) >> 16;
  1742. result[t][6] = (rtl_get_bbreg(hw, 0xec4,
  1743. MASKDWORD) & 0x3FF0000) >> 16;
  1744. result[t][7] = (rtl_get_bbreg(hw, 0xecc,
  1745. MASKDWORD) & 0x3FF0000) >> 16;
  1746. break;
  1747. } else if (i == (retrycount - 1) && pathb_ok == 0x01) {
  1748. /* Tx IQK OK */
  1749. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1750. "Path B Only Tx IQK Success!!\n");
  1751. result[t][4] = (rtl_get_bbreg(hw, 0xeb4,
  1752. MASKDWORD) & 0x3FF0000) >> 16;
  1753. result[t][5] = (rtl_get_bbreg(hw, 0xebc,
  1754. MASKDWORD) & 0x3FF0000) >> 16;
  1755. }
  1756. }
  1757. if (0x00 == pathb_ok)
  1758. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1759. "Path B IQK failed!!\n");
  1760. }
  1761. /* Back to BB mode, load original value */
  1762. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1763. "IQK:Back to BB mode, load original value!\n");
  1764. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0);
  1765. if (t != 0) {
  1766. /* Switch back BB to SI mode after finish IQ Calibration. */
  1767. if (!rtlphy->rfpi_enable)
  1768. _rtl92d_phy_pimode_switch(hw, false);
  1769. /* Reload ADDA power saving parameters */
  1770. _rtl92d_phy_reload_adda_registers(hw, adda_reg,
  1771. rtlphy->adda_backup, IQK_ADDA_REG_NUM);
  1772. /* Reload MAC parameters */
  1773. _rtl92d_phy_reload_mac_registers(hw, iqk_mac_reg,
  1774. rtlphy->iqk_mac_backup);
  1775. if (is2t)
  1776. _rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
  1777. rtlphy->iqk_bb_backup,
  1778. IQK_BB_REG_NUM);
  1779. else
  1780. _rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
  1781. rtlphy->iqk_bb_backup,
  1782. IQK_BB_REG_NUM - 1);
  1783. /* load 0xe30 IQC default value */
  1784. rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x01008c00);
  1785. rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x01008c00);
  1786. }
  1787. RTPRINT(rtlpriv, FINIT, INIT_IQK, "<==\n");
  1788. }
  1789. static void _rtl92d_phy_iq_calibrate_5g_normal(struct ieee80211_hw *hw,
  1790. long result[][8], u8 t)
  1791. {
  1792. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1793. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1794. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  1795. u8 patha_ok, pathb_ok;
  1796. static u32 adda_reg[IQK_ADDA_REG_NUM] = {
  1797. RFPGA0_XCD_SWITCHCONTROL, 0xe6c, 0xe70, 0xe74,
  1798. 0xe78, 0xe7c, 0xe80, 0xe84,
  1799. 0xe88, 0xe8c, 0xed0, 0xed4,
  1800. 0xed8, 0xedc, 0xee0, 0xeec
  1801. };
  1802. static u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
  1803. 0x522, 0x550, 0x551, 0x040
  1804. };
  1805. static u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
  1806. RFPGA0_XAB_RFINTERFACESW, RFPGA0_XA_RFINTERFACEOE,
  1807. RFPGA0_XB_RFINTERFACEOE, ROFDM0_TRMUXPAR,
  1808. RFPGA0_XCD_RFINTERFACESW, ROFDM0_TRXPATHENABLE,
  1809. RFPGA0_RFMOD, RFPGA0_ANALOGPARAMETER4,
  1810. ROFDM0_XAAGCCORE1, ROFDM0_XBAGCCORE1
  1811. };
  1812. u32 bbvalue;
  1813. bool is2t = IS_92D_SINGLEPHY(rtlhal->version);
  1814. /* Note: IQ calibration must be performed after loading
  1815. * PHY_REG.txt , and radio_a, radio_b.txt */
  1816. RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQK for 5G NORMAL:Start!!!\n");
  1817. mdelay(IQK_DELAY_TIME * 20);
  1818. if (t == 0) {
  1819. bbvalue = rtl_get_bbreg(hw, RFPGA0_RFMOD, MASKDWORD);
  1820. RTPRINT(rtlpriv, FINIT, INIT_IQK, "==>0x%08x\n", bbvalue);
  1821. RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQ Calibration for %s\n",
  1822. is2t ? "2T2R" : "1T1R");
  1823. /* Save ADDA parameters, turn Path A ADDA on */
  1824. _rtl92d_phy_save_adda_registers(hw, adda_reg,
  1825. rtlphy->adda_backup,
  1826. IQK_ADDA_REG_NUM);
  1827. _rtl92d_phy_save_mac_registers(hw, iqk_mac_reg,
  1828. rtlphy->iqk_mac_backup);
  1829. if (is2t)
  1830. _rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
  1831. rtlphy->iqk_bb_backup,
  1832. IQK_BB_REG_NUM);
  1833. else
  1834. _rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
  1835. rtlphy->iqk_bb_backup,
  1836. IQK_BB_REG_NUM - 1);
  1837. }
  1838. _rtl92d_phy_path_adda_on(hw, adda_reg, true, is2t);
  1839. /* MAC settings */
  1840. _rtl92d_phy_mac_setting_calibration(hw, iqk_mac_reg,
  1841. rtlphy->iqk_mac_backup);
  1842. if (t == 0)
  1843. rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
  1844. RFPGA0_XA_HSSIPARAMETER1, BIT(8));
  1845. /* Switch BB to PI mode to do IQ Calibration. */
  1846. if (!rtlphy->rfpi_enable)
  1847. _rtl92d_phy_pimode_switch(hw, true);
  1848. rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(24), 0x00);
  1849. rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKDWORD, 0x03a05600);
  1850. rtl_set_bbreg(hw, ROFDM0_TRMUXPAR, MASKDWORD, 0x000800e4);
  1851. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW, MASKDWORD, 0x22208000);
  1852. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xf00000, 0x0f);
  1853. /* Page B init */
  1854. rtl_set_bbreg(hw, 0xb68, MASKDWORD, 0x0f600000);
  1855. if (is2t)
  1856. rtl_set_bbreg(hw, 0xb6c, MASKDWORD, 0x0f600000);
  1857. /* IQ calibration setting */
  1858. RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQK setting!\n");
  1859. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
  1860. rtl_set_bbreg(hw, 0xe40, MASKDWORD, 0x10007c00);
  1861. rtl_set_bbreg(hw, 0xe44, MASKDWORD, 0x01004800);
  1862. patha_ok = _rtl92d_phy_patha_iqk_5g_normal(hw, is2t);
  1863. if (patha_ok == 0x03) {
  1864. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path A IQK Success!!\n");
  1865. result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
  1866. 0x3FF0000) >> 16;
  1867. result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
  1868. 0x3FF0000) >> 16;
  1869. result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
  1870. 0x3FF0000) >> 16;
  1871. result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
  1872. 0x3FF0000) >> 16;
  1873. } else if (patha_ok == 0x01) { /* Tx IQK OK */
  1874. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1875. "Path A IQK Only Tx Success!!\n");
  1876. result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
  1877. 0x3FF0000) >> 16;
  1878. result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
  1879. 0x3FF0000) >> 16;
  1880. } else {
  1881. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path A IQK Fail!!\n");
  1882. }
  1883. if (is2t) {
  1884. /* _rtl92d_phy_patha_standby(hw); */
  1885. /* Turn Path B ADDA on */
  1886. _rtl92d_phy_path_adda_on(hw, adda_reg, false, is2t);
  1887. pathb_ok = _rtl92d_phy_pathb_iqk_5g_normal(hw);
  1888. if (pathb_ok == 0x03) {
  1889. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1890. "Path B IQK Success!!\n");
  1891. result[t][4] = (rtl_get_bbreg(hw, 0xeb4, MASKDWORD) &
  1892. 0x3FF0000) >> 16;
  1893. result[t][5] = (rtl_get_bbreg(hw, 0xebc, MASKDWORD) &
  1894. 0x3FF0000) >> 16;
  1895. result[t][6] = (rtl_get_bbreg(hw, 0xec4, MASKDWORD) &
  1896. 0x3FF0000) >> 16;
  1897. result[t][7] = (rtl_get_bbreg(hw, 0xecc, MASKDWORD) &
  1898. 0x3FF0000) >> 16;
  1899. } else if (pathb_ok == 0x01) { /* Tx IQK OK */
  1900. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1901. "Path B Only Tx IQK Success!!\n");
  1902. result[t][4] = (rtl_get_bbreg(hw, 0xeb4, MASKDWORD) &
  1903. 0x3FF0000) >> 16;
  1904. result[t][5] = (rtl_get_bbreg(hw, 0xebc, MASKDWORD) &
  1905. 0x3FF0000) >> 16;
  1906. } else {
  1907. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1908. "Path B IQK failed!!\n");
  1909. }
  1910. }
  1911. /* Back to BB mode, load original value */
  1912. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  1913. "IQK:Back to BB mode, load original value!\n");
  1914. rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0);
  1915. if (t != 0) {
  1916. if (is2t)
  1917. _rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
  1918. rtlphy->iqk_bb_backup,
  1919. IQK_BB_REG_NUM);
  1920. else
  1921. _rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
  1922. rtlphy->iqk_bb_backup,
  1923. IQK_BB_REG_NUM - 1);
  1924. /* Reload MAC parameters */
  1925. _rtl92d_phy_reload_mac_registers(hw, iqk_mac_reg,
  1926. rtlphy->iqk_mac_backup);
  1927. /* Switch back BB to SI mode after finish IQ Calibration. */
  1928. if (!rtlphy->rfpi_enable)
  1929. _rtl92d_phy_pimode_switch(hw, false);
  1930. /* Reload ADDA power saving parameters */
  1931. _rtl92d_phy_reload_adda_registers(hw, adda_reg,
  1932. rtlphy->adda_backup,
  1933. IQK_ADDA_REG_NUM);
  1934. }
  1935. RTPRINT(rtlpriv, FINIT, INIT_IQK, "<==\n");
  1936. }
  1937. static bool _rtl92d_phy_simularity_compare(struct ieee80211_hw *hw,
  1938. long result[][8], u8 c1, u8 c2)
  1939. {
  1940. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1941. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  1942. u32 i, j, diff, sim_bitmap, bound;
  1943. u8 final_candidate[2] = {0xFF, 0xFF}; /* for path A and path B */
  1944. bool bresult = true;
  1945. bool is2t = IS_92D_SINGLEPHY(rtlhal->version);
  1946. if (is2t)
  1947. bound = 8;
  1948. else
  1949. bound = 4;
  1950. sim_bitmap = 0;
  1951. for (i = 0; i < bound; i++) {
  1952. diff = (result[c1][i] > result[c2][i]) ? (result[c1][i] -
  1953. result[c2][i]) : (result[c2][i] - result[c1][i]);
  1954. if (diff > MAX_TOLERANCE_92D) {
  1955. if ((i == 2 || i == 6) && !sim_bitmap) {
  1956. if (result[c1][i] + result[c1][i + 1] == 0)
  1957. final_candidate[(i / 4)] = c2;
  1958. else if (result[c2][i] + result[c2][i + 1] == 0)
  1959. final_candidate[(i / 4)] = c1;
  1960. else
  1961. sim_bitmap = sim_bitmap | (1 << i);
  1962. } else {
  1963. sim_bitmap = sim_bitmap | (1 << i);
  1964. }
  1965. }
  1966. }
  1967. if (sim_bitmap == 0) {
  1968. for (i = 0; i < (bound / 4); i++) {
  1969. if (final_candidate[i] != 0xFF) {
  1970. for (j = i * 4; j < (i + 1) * 4 - 2; j++)
  1971. result[3][j] =
  1972. result[final_candidate[i]][j];
  1973. bresult = false;
  1974. }
  1975. }
  1976. return bresult;
  1977. }
  1978. if (!(sim_bitmap & 0x0F)) { /* path A OK */
  1979. for (i = 0; i < 4; i++)
  1980. result[3][i] = result[c1][i];
  1981. } else if (!(sim_bitmap & 0x03)) { /* path A, Tx OK */
  1982. for (i = 0; i < 2; i++)
  1983. result[3][i] = result[c1][i];
  1984. }
  1985. if (!(sim_bitmap & 0xF0) && is2t) { /* path B OK */
  1986. for (i = 4; i < 8; i++)
  1987. result[3][i] = result[c1][i];
  1988. } else if (!(sim_bitmap & 0x30)) { /* path B, Tx OK */
  1989. for (i = 4; i < 6; i++)
  1990. result[3][i] = result[c1][i];
  1991. }
  1992. return false;
  1993. }
  1994. static void _rtl92d_phy_patha_fill_iqk_matrix(struct ieee80211_hw *hw,
  1995. bool iqk_ok, long result[][8],
  1996. u8 final_candidate, bool txonly)
  1997. {
  1998. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1999. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  2000. u32 oldval_0, val_x, tx0_a, reg;
  2001. long val_y, tx0_c;
  2002. bool is2t = IS_92D_SINGLEPHY(rtlhal->version) ||
  2003. rtlhal->macphymode == DUALMAC_DUALPHY;
  2004. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2005. "Path A IQ Calibration %s !\n", iqk_ok ? "Success" : "Failed");
  2006. if (final_candidate == 0xFF) {
  2007. return;
  2008. } else if (iqk_ok) {
  2009. oldval_0 = (rtl_get_bbreg(hw, ROFDM0_XATxIQIMBALANCE,
  2010. MASKDWORD) >> 22) & 0x3FF; /* OFDM0_D */
  2011. val_x = result[final_candidate][0];
  2012. if ((val_x & 0x00000200) != 0)
  2013. val_x = val_x | 0xFFFFFC00;
  2014. tx0_a = (val_x * oldval_0) >> 8;
  2015. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2016. "X = 0x%x, tx0_a = 0x%x, oldval_0 0x%x\n",
  2017. val_x, tx0_a, oldval_0);
  2018. rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, 0x3FF, tx0_a);
  2019. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(24),
  2020. ((val_x * oldval_0 >> 7) & 0x1));
  2021. val_y = result[final_candidate][1];
  2022. if ((val_y & 0x00000200) != 0)
  2023. val_y = val_y | 0xFFFFFC00;
  2024. /* path B IQK result + 3 */
  2025. if (rtlhal->interfaceindex == 1 &&
  2026. rtlhal->current_bandtype == BAND_ON_5G)
  2027. val_y += 3;
  2028. tx0_c = (val_y * oldval_0) >> 8;
  2029. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2030. "Y = 0x%lx, tx0_c = 0x%lx\n",
  2031. val_y, tx0_c);
  2032. rtl_set_bbreg(hw, ROFDM0_XCTxAFE, 0xF0000000,
  2033. ((tx0_c & 0x3C0) >> 6));
  2034. rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, 0x003F0000,
  2035. (tx0_c & 0x3F));
  2036. if (is2t)
  2037. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(26),
  2038. ((val_y * oldval_0 >> 7) & 0x1));
  2039. RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xC80 = 0x%x\n",
  2040. rtl_get_bbreg(hw, ROFDM0_XATxIQIMBALANCE,
  2041. MASKDWORD));
  2042. if (txonly) {
  2043. RTPRINT(rtlpriv, FINIT, INIT_IQK, "only Tx OK\n");
  2044. return;
  2045. }
  2046. reg = result[final_candidate][2];
  2047. rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, 0x3FF, reg);
  2048. reg = result[final_candidate][3] & 0x3F;
  2049. rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, 0xFC00, reg);
  2050. reg = (result[final_candidate][3] >> 6) & 0xF;
  2051. rtl_set_bbreg(hw, 0xca0, 0xF0000000, reg);
  2052. }
  2053. }
  2054. static void _rtl92d_phy_pathb_fill_iqk_matrix(struct ieee80211_hw *hw,
  2055. bool iqk_ok, long result[][8], u8 final_candidate, bool txonly)
  2056. {
  2057. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2058. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  2059. u32 oldval_1, val_x, tx1_a, reg;
  2060. long val_y, tx1_c;
  2061. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path B IQ Calibration %s !\n",
  2062. iqk_ok ? "Success" : "Failed");
  2063. if (final_candidate == 0xFF) {
  2064. return;
  2065. } else if (iqk_ok) {
  2066. oldval_1 = (rtl_get_bbreg(hw, ROFDM0_XBTxIQIMBALANCE,
  2067. MASKDWORD) >> 22) & 0x3FF;
  2068. val_x = result[final_candidate][4];
  2069. if ((val_x & 0x00000200) != 0)
  2070. val_x = val_x | 0xFFFFFC00;
  2071. tx1_a = (val_x * oldval_1) >> 8;
  2072. RTPRINT(rtlpriv, FINIT, INIT_IQK, "X = 0x%x, tx1_a = 0x%x\n",
  2073. val_x, tx1_a);
  2074. rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, 0x3FF, tx1_a);
  2075. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(28),
  2076. ((val_x * oldval_1 >> 7) & 0x1));
  2077. val_y = result[final_candidate][5];
  2078. if ((val_y & 0x00000200) != 0)
  2079. val_y = val_y | 0xFFFFFC00;
  2080. if (rtlhal->current_bandtype == BAND_ON_5G)
  2081. val_y += 3;
  2082. tx1_c = (val_y * oldval_1) >> 8;
  2083. RTPRINT(rtlpriv, FINIT, INIT_IQK, "Y = 0x%lx, tx1_c = 0x%lx\n",
  2084. val_y, tx1_c);
  2085. rtl_set_bbreg(hw, ROFDM0_XDTxAFE, 0xF0000000,
  2086. ((tx1_c & 0x3C0) >> 6));
  2087. rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, 0x003F0000,
  2088. (tx1_c & 0x3F));
  2089. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(30),
  2090. ((val_y * oldval_1 >> 7) & 0x1));
  2091. if (txonly)
  2092. return;
  2093. reg = result[final_candidate][6];
  2094. rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0x3FF, reg);
  2095. reg = result[final_candidate][7] & 0x3F;
  2096. rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0xFC00, reg);
  2097. reg = (result[final_candidate][7] >> 6) & 0xF;
  2098. rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, 0x0000F000, reg);
  2099. }
  2100. }
  2101. void rtl92d_phy_iq_calibrate(struct ieee80211_hw *hw)
  2102. {
  2103. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2104. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2105. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  2106. long result[4][8];
  2107. u8 i, final_candidate, indexforchannel;
  2108. bool patha_ok, pathb_ok;
  2109. long rege94, rege9c, regea4, regeac, regeb4;
  2110. long regebc, regec4, regecc, regtmp = 0;
  2111. bool is12simular, is13simular, is23simular;
  2112. unsigned long flag = 0;
  2113. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2114. "IQK:Start!!!channel %d\n", rtlphy->current_channel);
  2115. for (i = 0; i < 8; i++) {
  2116. result[0][i] = 0;
  2117. result[1][i] = 0;
  2118. result[2][i] = 0;
  2119. result[3][i] = 0;
  2120. }
  2121. final_candidate = 0xff;
  2122. patha_ok = false;
  2123. pathb_ok = false;
  2124. is12simular = false;
  2125. is23simular = false;
  2126. is13simular = false;
  2127. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2128. "IQK !!!currentband %d\n", rtlhal->current_bandtype);
  2129. rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
  2130. for (i = 0; i < 3; i++) {
  2131. if (rtlhal->current_bandtype == BAND_ON_5G) {
  2132. _rtl92d_phy_iq_calibrate_5g_normal(hw, result, i);
  2133. } else if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  2134. if (IS_92D_SINGLEPHY(rtlhal->version))
  2135. _rtl92d_phy_iq_calibrate(hw, result, i, true);
  2136. else
  2137. _rtl92d_phy_iq_calibrate(hw, result, i, false);
  2138. }
  2139. if (i == 1) {
  2140. is12simular = _rtl92d_phy_simularity_compare(hw, result,
  2141. 0, 1);
  2142. if (is12simular) {
  2143. final_candidate = 0;
  2144. break;
  2145. }
  2146. }
  2147. if (i == 2) {
  2148. is13simular = _rtl92d_phy_simularity_compare(hw, result,
  2149. 0, 2);
  2150. if (is13simular) {
  2151. final_candidate = 0;
  2152. break;
  2153. }
  2154. is23simular = _rtl92d_phy_simularity_compare(hw, result,
  2155. 1, 2);
  2156. if (is23simular) {
  2157. final_candidate = 1;
  2158. } else {
  2159. for (i = 0; i < 8; i++)
  2160. regtmp += result[3][i];
  2161. if (regtmp != 0)
  2162. final_candidate = 3;
  2163. else
  2164. final_candidate = 0xFF;
  2165. }
  2166. }
  2167. }
  2168. rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
  2169. for (i = 0; i < 4; i++) {
  2170. rege94 = result[i][0];
  2171. rege9c = result[i][1];
  2172. regea4 = result[i][2];
  2173. regeac = result[i][3];
  2174. regeb4 = result[i][4];
  2175. regebc = result[i][5];
  2176. regec4 = result[i][6];
  2177. regecc = result[i][7];
  2178. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2179. "IQK: rege94=%lx rege9c=%lx regea4=%lx regeac=%lx regeb4=%lx regebc=%lx regec4=%lx regecc=%lx\n",
  2180. rege94, rege9c, regea4, regeac, regeb4, regebc, regec4,
  2181. regecc);
  2182. }
  2183. if (final_candidate != 0xff) {
  2184. rtlphy->reg_e94 = rege94 = result[final_candidate][0];
  2185. rtlphy->reg_e9c = rege9c = result[final_candidate][1];
  2186. regea4 = result[final_candidate][2];
  2187. regeac = result[final_candidate][3];
  2188. rtlphy->reg_eb4 = regeb4 = result[final_candidate][4];
  2189. rtlphy->reg_ebc = regebc = result[final_candidate][5];
  2190. regec4 = result[final_candidate][6];
  2191. regecc = result[final_candidate][7];
  2192. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2193. "IQK: final_candidate is %x\n", final_candidate);
  2194. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2195. "IQK: rege94=%lx rege9c=%lx regea4=%lx regeac=%lx regeb4=%lx regebc=%lx regec4=%lx regecc=%lx\n",
  2196. rege94, rege9c, regea4, regeac, regeb4, regebc, regec4,
  2197. regecc);
  2198. patha_ok = pathb_ok = true;
  2199. } else {
  2200. rtlphy->reg_e94 = rtlphy->reg_eb4 = 0x100; /* X default value */
  2201. rtlphy->reg_e9c = rtlphy->reg_ebc = 0x0; /* Y default value */
  2202. }
  2203. if ((rege94 != 0) /*&&(regea4 != 0) */)
  2204. _rtl92d_phy_patha_fill_iqk_matrix(hw, patha_ok, result,
  2205. final_candidate, (regea4 == 0));
  2206. if (IS_92D_SINGLEPHY(rtlhal->version)) {
  2207. if ((regeb4 != 0) /*&&(regec4 != 0) */)
  2208. _rtl92d_phy_pathb_fill_iqk_matrix(hw, pathb_ok, result,
  2209. final_candidate, (regec4 == 0));
  2210. }
  2211. if (final_candidate != 0xFF) {
  2212. indexforchannel = rtl92d_get_rightchnlplace_for_iqk(
  2213. rtlphy->current_channel);
  2214. for (i = 0; i < IQK_MATRIX_REG_NUM; i++)
  2215. rtlphy->iqk_matrix[indexforchannel].
  2216. value[0][i] = result[final_candidate][i];
  2217. rtlphy->iqk_matrix[indexforchannel].iqk_done =
  2218. true;
  2219. RT_TRACE(rtlpriv, COMP_SCAN | COMP_MLME, DBG_LOUD,
  2220. "IQK OK indexforchannel %d\n", indexforchannel);
  2221. }
  2222. }
  2223. void rtl92d_phy_reload_iqk_setting(struct ieee80211_hw *hw, u8 channel)
  2224. {
  2225. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2226. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2227. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  2228. u8 indexforchannel;
  2229. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "channel %d\n", channel);
  2230. /*------Do IQK for normal chip and test chip 5G band------- */
  2231. indexforchannel = rtl92d_get_rightchnlplace_for_iqk(channel);
  2232. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "indexforchannel %d done %d\n",
  2233. indexforchannel,
  2234. rtlphy->iqk_matrix[indexforchannel].iqk_done);
  2235. if (0 && !rtlphy->iqk_matrix[indexforchannel].iqk_done &&
  2236. rtlphy->need_iqk) {
  2237. /* Re Do IQK. */
  2238. RT_TRACE(rtlpriv, COMP_SCAN | COMP_INIT, DBG_LOUD,
  2239. "Do IQK Matrix reg for channel:%d....\n", channel);
  2240. rtl92d_phy_iq_calibrate(hw);
  2241. } else {
  2242. /* Just load the value. */
  2243. /* 2G band just load once. */
  2244. if (((!rtlhal->load_imrandiqk_setting_for2g) &&
  2245. indexforchannel == 0) || indexforchannel > 0) {
  2246. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
  2247. "Just Read IQK Matrix reg for channel:%d....\n",
  2248. channel);
  2249. if ((rtlphy->iqk_matrix[indexforchannel].
  2250. value[0] != NULL)
  2251. /*&&(regea4 != 0) */)
  2252. _rtl92d_phy_patha_fill_iqk_matrix(hw, true,
  2253. rtlphy->iqk_matrix[
  2254. indexforchannel].value, 0,
  2255. (rtlphy->iqk_matrix[
  2256. indexforchannel].value[0][2] == 0));
  2257. if (IS_92D_SINGLEPHY(rtlhal->version)) {
  2258. if ((rtlphy->iqk_matrix[
  2259. indexforchannel].value[0][4] != 0)
  2260. /*&&(regec4 != 0) */)
  2261. _rtl92d_phy_pathb_fill_iqk_matrix(hw,
  2262. true,
  2263. rtlphy->iqk_matrix[
  2264. indexforchannel].value, 0,
  2265. (rtlphy->iqk_matrix[
  2266. indexforchannel].value[0][6]
  2267. == 0));
  2268. }
  2269. }
  2270. }
  2271. rtlphy->need_iqk = false;
  2272. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
  2273. }
  2274. static u32 _rtl92d_phy_get_abs(u32 val1, u32 val2)
  2275. {
  2276. u32 ret;
  2277. if (val1 >= val2)
  2278. ret = val1 - val2;
  2279. else
  2280. ret = val2 - val1;
  2281. return ret;
  2282. }
  2283. static bool _rtl92d_is_legal_5g_channel(struct ieee80211_hw *hw, u8 channel)
  2284. {
  2285. int i;
  2286. for (i = 0; i < sizeof(channel5g); i++)
  2287. if (channel == channel5g[i])
  2288. return true;
  2289. return false;
  2290. }
  2291. static void _rtl92d_phy_calc_curvindex(struct ieee80211_hw *hw,
  2292. u32 *targetchnl, u32 * curvecount_val,
  2293. bool is5g, u32 *curveindex)
  2294. {
  2295. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2296. u32 smallest_abs_val = 0xffffffff, u4tmp;
  2297. u8 i, j;
  2298. u8 chnl_num = is5g ? TARGET_CHNL_NUM_5G : TARGET_CHNL_NUM_2G;
  2299. for (i = 0; i < chnl_num; i++) {
  2300. if (is5g && !_rtl92d_is_legal_5g_channel(hw, i + 1))
  2301. continue;
  2302. curveindex[i] = 0;
  2303. for (j = 0; j < (CV_CURVE_CNT * 2); j++) {
  2304. u4tmp = _rtl92d_phy_get_abs(targetchnl[i],
  2305. curvecount_val[j]);
  2306. if (u4tmp < smallest_abs_val) {
  2307. curveindex[i] = j;
  2308. smallest_abs_val = u4tmp;
  2309. }
  2310. }
  2311. smallest_abs_val = 0xffffffff;
  2312. RTPRINT(rtlpriv, FINIT, INIT_IQK, "curveindex[%d] = %x\n",
  2313. i, curveindex[i]);
  2314. }
  2315. }
  2316. static void _rtl92d_phy_reload_lck_setting(struct ieee80211_hw *hw,
  2317. u8 channel)
  2318. {
  2319. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2320. u8 erfpath = rtlpriv->rtlhal.current_bandtype ==
  2321. BAND_ON_5G ? RF90_PATH_A :
  2322. IS_92D_SINGLEPHY(rtlpriv->rtlhal.version) ?
  2323. RF90_PATH_B : RF90_PATH_A;
  2324. u32 u4tmp = 0, u4regvalue = 0;
  2325. bool bneed_powerdown_radio = false;
  2326. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "path %d\n", erfpath);
  2327. RTPRINT(rtlpriv, FINIT, INIT_IQK, "band type = %d\n",
  2328. rtlpriv->rtlhal.current_bandtype);
  2329. RTPRINT(rtlpriv, FINIT, INIT_IQK, "channel = %d\n", channel);
  2330. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G) {/* Path-A for 5G */
  2331. u4tmp = curveindex_5g[channel-1];
  2332. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2333. "ver 1 set RF-A, 5G, 0x28 = 0x%x !!\n", u4tmp);
  2334. if (rtlpriv->rtlhal.macphymode == DUALMAC_DUALPHY &&
  2335. rtlpriv->rtlhal.interfaceindex == 1) {
  2336. bneed_powerdown_radio =
  2337. rtl92d_phy_enable_anotherphy(hw, false);
  2338. rtlpriv->rtlhal.during_mac1init_radioa = true;
  2339. /* asume no this case */
  2340. if (bneed_powerdown_radio)
  2341. _rtl92d_phy_enable_rf_env(hw, erfpath,
  2342. &u4regvalue);
  2343. }
  2344. rtl_set_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800, u4tmp);
  2345. if (bneed_powerdown_radio)
  2346. _rtl92d_phy_restore_rf_env(hw, erfpath, &u4regvalue);
  2347. if (rtlpriv->rtlhal.during_mac1init_radioa)
  2348. rtl92d_phy_powerdown_anotherphy(hw, false);
  2349. } else if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G) {
  2350. u4tmp = curveindex_2g[channel-1];
  2351. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2352. "ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n", u4tmp);
  2353. if (rtlpriv->rtlhal.macphymode == DUALMAC_DUALPHY &&
  2354. rtlpriv->rtlhal.interfaceindex == 0) {
  2355. bneed_powerdown_radio =
  2356. rtl92d_phy_enable_anotherphy(hw, true);
  2357. rtlpriv->rtlhal.during_mac0init_radiob = true;
  2358. if (bneed_powerdown_radio)
  2359. _rtl92d_phy_enable_rf_env(hw, erfpath,
  2360. &u4regvalue);
  2361. }
  2362. rtl_set_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800, u4tmp);
  2363. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2364. "ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n",
  2365. rtl_get_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800));
  2366. if (bneed_powerdown_radio)
  2367. _rtl92d_phy_restore_rf_env(hw, erfpath, &u4regvalue);
  2368. if (rtlpriv->rtlhal.during_mac0init_radiob)
  2369. rtl92d_phy_powerdown_anotherphy(hw, true);
  2370. }
  2371. RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
  2372. }
  2373. static void _rtl92d_phy_lc_calibrate_sw(struct ieee80211_hw *hw, bool is2t)
  2374. {
  2375. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2376. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  2377. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  2378. u8 tmpreg, index, rf_mode[2];
  2379. u8 path = is2t ? 2 : 1;
  2380. u8 i;
  2381. u32 u4tmp, offset;
  2382. u32 curvecount_val[CV_CURVE_CNT * 2] = {0};
  2383. u16 timeout = 800, timecount = 0;
  2384. /* Check continuous TX and Packet TX */
  2385. tmpreg = rtl_read_byte(rtlpriv, 0xd03);
  2386. /* if Deal with contisuous TX case, disable all continuous TX */
  2387. /* if Deal with Packet TX case, block all queues */
  2388. if ((tmpreg & 0x70) != 0)
  2389. rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
  2390. else
  2391. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
  2392. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xF00000, 0x0F);
  2393. for (index = 0; index < path; index++) {
  2394. /* 1. Read original RF mode */
  2395. offset = index == 0 ? ROFDM0_XAAGCCORE1 : ROFDM0_XBAGCCORE1;
  2396. rf_mode[index] = rtl_read_byte(rtlpriv, offset);
  2397. /* 2. Set RF mode = standby mode */
  2398. rtl_set_rfreg(hw, (enum radio_path)index, RF_AC,
  2399. RFREG_OFFSET_MASK, 0x010000);
  2400. if (rtlpci->init_ready) {
  2401. /* switch CV-curve control by LC-calibration */
  2402. rtl_set_rfreg(hw, (enum radio_path)index, RF_SYN_G7,
  2403. BIT(17), 0x0);
  2404. /* 4. Set LC calibration begin */
  2405. rtl_set_rfreg(hw, (enum radio_path)index, RF_CHNLBW,
  2406. 0x08000, 0x01);
  2407. }
  2408. u4tmp = rtl_get_rfreg(hw, (enum radio_path)index, RF_SYN_G6,
  2409. RFREG_OFFSET_MASK);
  2410. while ((!(u4tmp & BIT(11))) && timecount <= timeout) {
  2411. mdelay(50);
  2412. timecount += 50;
  2413. u4tmp = rtl_get_rfreg(hw, (enum radio_path)index,
  2414. RF_SYN_G6, RFREG_OFFSET_MASK);
  2415. }
  2416. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2417. "PHY_LCK finish delay for %d ms=2\n", timecount);
  2418. u4tmp = rtl_get_rfreg(hw, index, RF_SYN_G4, RFREG_OFFSET_MASK);
  2419. if (index == 0 && rtlhal->interfaceindex == 0) {
  2420. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2421. "path-A / 5G LCK\n");
  2422. } else {
  2423. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2424. "path-B / 2.4G LCK\n");
  2425. }
  2426. memset(&curvecount_val[0], 0, CV_CURVE_CNT * 2);
  2427. /* Set LC calibration off */
  2428. rtl_set_rfreg(hw, (enum radio_path)index, RF_CHNLBW,
  2429. 0x08000, 0x0);
  2430. RTPRINT(rtlpriv, FINIT, INIT_IQK, "set RF 0x18[15] = 0\n");
  2431. /* save Curve-counting number */
  2432. for (i = 0; i < CV_CURVE_CNT; i++) {
  2433. u32 readval = 0, readval2 = 0;
  2434. rtl_set_rfreg(hw, (enum radio_path)index, 0x3F,
  2435. 0x7f, i);
  2436. rtl_set_rfreg(hw, (enum radio_path)index, 0x4D,
  2437. RFREG_OFFSET_MASK, 0x0);
  2438. readval = rtl_get_rfreg(hw, (enum radio_path)index,
  2439. 0x4F, RFREG_OFFSET_MASK);
  2440. curvecount_val[2 * i + 1] = (readval & 0xfffe0) >> 5;
  2441. /* reg 0x4f [4:0] */
  2442. /* reg 0x50 [19:10] */
  2443. readval2 = rtl_get_rfreg(hw, (enum radio_path)index,
  2444. 0x50, 0xffc00);
  2445. curvecount_val[2 * i] = (((readval & 0x1F) << 10) |
  2446. readval2);
  2447. }
  2448. if (index == 0 && rtlhal->interfaceindex == 0)
  2449. _rtl92d_phy_calc_curvindex(hw, targetchnl_5g,
  2450. curvecount_val,
  2451. true, curveindex_5g);
  2452. else
  2453. _rtl92d_phy_calc_curvindex(hw, targetchnl_2g,
  2454. curvecount_val,
  2455. false, curveindex_2g);
  2456. /* switch CV-curve control mode */
  2457. rtl_set_rfreg(hw, (enum radio_path)index, RF_SYN_G7,
  2458. BIT(17), 0x1);
  2459. }
  2460. /* Restore original situation */
  2461. for (index = 0; index < path; index++) {
  2462. offset = index == 0 ? ROFDM0_XAAGCCORE1 : ROFDM0_XBAGCCORE1;
  2463. rtl_write_byte(rtlpriv, offset, 0x50);
  2464. rtl_write_byte(rtlpriv, offset, rf_mode[index]);
  2465. }
  2466. if ((tmpreg & 0x70) != 0)
  2467. rtl_write_byte(rtlpriv, 0xd03, tmpreg);
  2468. else /*Deal with Packet TX case */
  2469. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  2470. rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xF00000, 0x00);
  2471. _rtl92d_phy_reload_lck_setting(hw, rtlpriv->phy.current_channel);
  2472. }
  2473. static void _rtl92d_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
  2474. {
  2475. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2476. RTPRINT(rtlpriv, FINIT, INIT_IQK, "cosa PHY_LCK ver=2\n");
  2477. _rtl92d_phy_lc_calibrate_sw(hw, is2t);
  2478. }
  2479. void rtl92d_phy_lc_calibrate(struct ieee80211_hw *hw)
  2480. {
  2481. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2482. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2483. struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
  2484. u32 timeout = 2000, timecount = 0;
  2485. while (rtlpriv->mac80211.act_scanning && timecount < timeout) {
  2486. udelay(50);
  2487. timecount += 50;
  2488. }
  2489. rtlphy->lck_inprogress = true;
  2490. RTPRINT(rtlpriv, FINIT, INIT_IQK,
  2491. "LCK:Start!!! currentband %x delay %d ms\n",
  2492. rtlhal->current_bandtype, timecount);
  2493. if (IS_92D_SINGLEPHY(rtlhal->version)) {
  2494. _rtl92d_phy_lc_calibrate(hw, true);
  2495. } else {
  2496. /* For 1T1R */
  2497. _rtl92d_phy_lc_calibrate(hw, false);
  2498. }
  2499. rtlphy->lck_inprogress = false;
  2500. RTPRINT(rtlpriv, FINIT, INIT_IQK, "LCK:Finish!!!\n");
  2501. }
  2502. void rtl92d_phy_ap_calibrate(struct ieee80211_hw *hw, s8 delta)
  2503. {
  2504. return;
  2505. }
  2506. static bool _rtl92d_phy_set_sw_chnl_cmdarray(struct swchnlcmd *cmdtable,
  2507. u32 cmdtableidx, u32 cmdtablesz, enum swchnlcmd_id cmdid,
  2508. u32 para1, u32 para2, u32 msdelay)
  2509. {
  2510. struct swchnlcmd *pcmd;
  2511. if (cmdtable == NULL) {
  2512. WARN_ONCE(true, "rtl8192de: cmdtable cannot be NULL\n");
  2513. return false;
  2514. }
  2515. if (cmdtableidx >= cmdtablesz)
  2516. return false;
  2517. pcmd = cmdtable + cmdtableidx;
  2518. pcmd->cmdid = cmdid;
  2519. pcmd->para1 = para1;
  2520. pcmd->para2 = para2;
  2521. pcmd->msdelay = msdelay;
  2522. return true;
  2523. }
  2524. void rtl92d_phy_reset_iqk_result(struct ieee80211_hw *hw)
  2525. {
  2526. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2527. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2528. u8 i;
  2529. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  2530. "settings regs %d default regs %d\n",
  2531. (int)(sizeof(rtlphy->iqk_matrix) /
  2532. sizeof(struct iqk_matrix_regs)),
  2533. IQK_MATRIX_REG_NUM);
  2534. /* 0xe94, 0xe9c, 0xea4, 0xeac, 0xeb4, 0xebc, 0xec4, 0xecc */
  2535. for (i = 0; i < IQK_MATRIX_SETTINGS_NUM; i++) {
  2536. rtlphy->iqk_matrix[i].value[0][0] = 0x100;
  2537. rtlphy->iqk_matrix[i].value[0][2] = 0x100;
  2538. rtlphy->iqk_matrix[i].value[0][4] = 0x100;
  2539. rtlphy->iqk_matrix[i].value[0][6] = 0x100;
  2540. rtlphy->iqk_matrix[i].value[0][1] = 0x0;
  2541. rtlphy->iqk_matrix[i].value[0][3] = 0x0;
  2542. rtlphy->iqk_matrix[i].value[0][5] = 0x0;
  2543. rtlphy->iqk_matrix[i].value[0][7] = 0x0;
  2544. rtlphy->iqk_matrix[i].iqk_done = false;
  2545. }
  2546. }
  2547. static bool _rtl92d_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
  2548. u8 channel, u8 *stage, u8 *step,
  2549. u32 *delay)
  2550. {
  2551. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2552. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2553. struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
  2554. u32 precommoncmdcnt;
  2555. struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
  2556. u32 postcommoncmdcnt;
  2557. struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
  2558. u32 rfdependcmdcnt;
  2559. struct swchnlcmd *currentcmd = NULL;
  2560. u8 rfpath;
  2561. u8 num_total_rfpath = rtlphy->num_total_rfpath;
  2562. precommoncmdcnt = 0;
  2563. _rtl92d_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
  2564. MAX_PRECMD_CNT,
  2565. CMDID_SET_TXPOWEROWER_LEVEL, 0, 0, 0);
  2566. _rtl92d_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
  2567. MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
  2568. postcommoncmdcnt = 0;
  2569. _rtl92d_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
  2570. MAX_POSTCMD_CNT, CMDID_END, 0, 0, 0);
  2571. rfdependcmdcnt = 0;
  2572. _rtl92d_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
  2573. MAX_RFDEPENDCMD_CNT, CMDID_RF_WRITEREG,
  2574. RF_CHNLBW, channel, 0);
  2575. _rtl92d_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
  2576. MAX_RFDEPENDCMD_CNT, CMDID_END,
  2577. 0, 0, 0);
  2578. do {
  2579. switch (*stage) {
  2580. case 0:
  2581. currentcmd = &precommoncmd[*step];
  2582. break;
  2583. case 1:
  2584. currentcmd = &rfdependcmd[*step];
  2585. break;
  2586. case 2:
  2587. currentcmd = &postcommoncmd[*step];
  2588. break;
  2589. }
  2590. if (currentcmd->cmdid == CMDID_END) {
  2591. if ((*stage) == 2) {
  2592. return true;
  2593. } else {
  2594. (*stage)++;
  2595. (*step) = 0;
  2596. continue;
  2597. }
  2598. }
  2599. switch (currentcmd->cmdid) {
  2600. case CMDID_SET_TXPOWEROWER_LEVEL:
  2601. rtl92d_phy_set_txpower_level(hw, channel);
  2602. break;
  2603. case CMDID_WRITEPORT_ULONG:
  2604. rtl_write_dword(rtlpriv, currentcmd->para1,
  2605. currentcmd->para2);
  2606. break;
  2607. case CMDID_WRITEPORT_USHORT:
  2608. rtl_write_word(rtlpriv, currentcmd->para1,
  2609. (u16)currentcmd->para2);
  2610. break;
  2611. case CMDID_WRITEPORT_UCHAR:
  2612. rtl_write_byte(rtlpriv, currentcmd->para1,
  2613. (u8)currentcmd->para2);
  2614. break;
  2615. case CMDID_RF_WRITEREG:
  2616. for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
  2617. rtlphy->rfreg_chnlval[rfpath] =
  2618. ((rtlphy->rfreg_chnlval[rfpath] &
  2619. 0xffffff00) | currentcmd->para2);
  2620. if (rtlpriv->rtlhal.current_bandtype ==
  2621. BAND_ON_5G) {
  2622. if (currentcmd->para2 > 99)
  2623. rtlphy->rfreg_chnlval[rfpath] =
  2624. rtlphy->rfreg_chnlval
  2625. [rfpath] | (BIT(18));
  2626. else
  2627. rtlphy->rfreg_chnlval[rfpath] =
  2628. rtlphy->rfreg_chnlval
  2629. [rfpath] & (~BIT(18));
  2630. rtlphy->rfreg_chnlval[rfpath] |=
  2631. (BIT(16) | BIT(8));
  2632. } else {
  2633. rtlphy->rfreg_chnlval[rfpath] &=
  2634. ~(BIT(8) | BIT(16) | BIT(18));
  2635. }
  2636. rtl_set_rfreg(hw, (enum radio_path)rfpath,
  2637. currentcmd->para1,
  2638. RFREG_OFFSET_MASK,
  2639. rtlphy->rfreg_chnlval[rfpath]);
  2640. _rtl92d_phy_reload_imr_setting(hw, channel,
  2641. rfpath);
  2642. }
  2643. _rtl92d_phy_switch_rf_setting(hw, channel);
  2644. /* do IQK when all parameters are ready */
  2645. rtl92d_phy_reload_iqk_setting(hw, channel);
  2646. break;
  2647. default:
  2648. pr_err("switch case %#x not processed\n",
  2649. currentcmd->cmdid);
  2650. break;
  2651. }
  2652. break;
  2653. } while (true);
  2654. (*delay) = currentcmd->msdelay;
  2655. (*step)++;
  2656. return false;
  2657. }
  2658. u8 rtl92d_phy_sw_chnl(struct ieee80211_hw *hw)
  2659. {
  2660. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2661. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2662. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  2663. u32 delay;
  2664. u32 timeout = 1000, timecount = 0;
  2665. u8 channel = rtlphy->current_channel;
  2666. u32 ret_value;
  2667. if (rtlphy->sw_chnl_inprogress)
  2668. return 0;
  2669. if (rtlphy->set_bwmode_inprogress)
  2670. return 0;
  2671. if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
  2672. RT_TRACE(rtlpriv, COMP_CHAN, DBG_LOUD,
  2673. "sw_chnl_inprogress false driver sleep or unload\n");
  2674. return 0;
  2675. }
  2676. while (rtlphy->lck_inprogress && timecount < timeout) {
  2677. mdelay(50);
  2678. timecount += 50;
  2679. }
  2680. if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
  2681. rtlhal->bandset == BAND_ON_BOTH) {
  2682. ret_value = rtl_get_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
  2683. MASKDWORD);
  2684. if (rtlphy->current_channel > 14 && !(ret_value & BIT(0)))
  2685. rtl92d_phy_switch_wirelessband(hw, BAND_ON_5G);
  2686. else if (rtlphy->current_channel <= 14 && (ret_value & BIT(0)))
  2687. rtl92d_phy_switch_wirelessband(hw, BAND_ON_2_4G);
  2688. }
  2689. switch (rtlhal->current_bandtype) {
  2690. case BAND_ON_5G:
  2691. /* Get first channel error when change between
  2692. * 5G and 2.4G band. */
  2693. if (channel <= 14)
  2694. return 0;
  2695. WARN_ONCE((channel <= 14), "rtl8192de: 5G but channel<=14\n");
  2696. break;
  2697. case BAND_ON_2_4G:
  2698. /* Get first channel error when change between
  2699. * 5G and 2.4G band. */
  2700. if (channel > 14)
  2701. return 0;
  2702. WARN_ONCE((channel > 14), "rtl8192de: 2G but channel>14\n");
  2703. break;
  2704. default:
  2705. WARN_ONCE(true, "rtl8192de: Invalid WirelessMode(%#x)!!\n",
  2706. rtlpriv->mac80211.mode);
  2707. break;
  2708. }
  2709. rtlphy->sw_chnl_inprogress = true;
  2710. if (channel == 0)
  2711. channel = 1;
  2712. rtlphy->sw_chnl_stage = 0;
  2713. rtlphy->sw_chnl_step = 0;
  2714. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE,
  2715. "switch to channel%d\n", rtlphy->current_channel);
  2716. do {
  2717. if (!rtlphy->sw_chnl_inprogress)
  2718. break;
  2719. if (!_rtl92d_phy_sw_chnl_step_by_step(hw,
  2720. rtlphy->current_channel,
  2721. &rtlphy->sw_chnl_stage, &rtlphy->sw_chnl_step, &delay)) {
  2722. if (delay > 0)
  2723. mdelay(delay);
  2724. else
  2725. continue;
  2726. } else {
  2727. rtlphy->sw_chnl_inprogress = false;
  2728. }
  2729. break;
  2730. } while (true);
  2731. RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "<==\n");
  2732. rtlphy->sw_chnl_inprogress = false;
  2733. return 1;
  2734. }
  2735. static void rtl92d_phy_set_io(struct ieee80211_hw *hw)
  2736. {
  2737. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2738. struct dig_t *de_digtable = &rtlpriv->dm_digtable;
  2739. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2740. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2741. "--->Cmd(%#x), set_io_inprogress(%d)\n",
  2742. rtlphy->current_io_type, rtlphy->set_io_inprogress);
  2743. switch (rtlphy->current_io_type) {
  2744. case IO_CMD_RESUME_DM_BY_SCAN:
  2745. de_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
  2746. rtl92d_dm_write_dig(hw);
  2747. rtl92d_phy_set_txpower_level(hw, rtlphy->current_channel);
  2748. break;
  2749. case IO_CMD_PAUSE_DM_BY_SCAN:
  2750. rtlphy->initgain_backup.xaagccore1 = de_digtable->cur_igvalue;
  2751. de_digtable->cur_igvalue = 0x37;
  2752. rtl92d_dm_write_dig(hw);
  2753. break;
  2754. default:
  2755. pr_err("switch case %#x not processed\n",
  2756. rtlphy->current_io_type);
  2757. break;
  2758. }
  2759. rtlphy->set_io_inprogress = false;
  2760. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE, "<---(%#x)\n",
  2761. rtlphy->current_io_type);
  2762. }
  2763. bool rtl92d_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
  2764. {
  2765. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2766. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2767. bool postprocessing = false;
  2768. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2769. "-->IO Cmd(%#x), set_io_inprogress(%d)\n",
  2770. iotype, rtlphy->set_io_inprogress);
  2771. do {
  2772. switch (iotype) {
  2773. case IO_CMD_RESUME_DM_BY_SCAN:
  2774. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2775. "[IO CMD] Resume DM after scan\n");
  2776. postprocessing = true;
  2777. break;
  2778. case IO_CMD_PAUSE_DM_BY_SCAN:
  2779. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
  2780. "[IO CMD] Pause DM before scan\n");
  2781. postprocessing = true;
  2782. break;
  2783. default:
  2784. pr_err("switch case %#x not processed\n",
  2785. iotype);
  2786. break;
  2787. }
  2788. } while (false);
  2789. if (postprocessing && !rtlphy->set_io_inprogress) {
  2790. rtlphy->set_io_inprogress = true;
  2791. rtlphy->current_io_type = iotype;
  2792. } else {
  2793. return false;
  2794. }
  2795. rtl92d_phy_set_io(hw);
  2796. RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE, "<--IO Type(%#x)\n", iotype);
  2797. return true;
  2798. }
  2799. static void _rtl92d_phy_set_rfon(struct ieee80211_hw *hw)
  2800. {
  2801. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2802. /* a. SYS_CLKR 0x08[11] = 1 restore MAC clock */
  2803. /* b. SPS_CTRL 0x11[7:0] = 0x2b */
  2804. if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY)
  2805. rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
  2806. /* c. For PCIE: SYS_FUNC_EN 0x02[7:0] = 0xE3 enable BB TRX function */
  2807. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  2808. /* RF_ON_EXCEP(d~g): */
  2809. /* d. APSD_CTRL 0x600[7:0] = 0x00 */
  2810. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
  2811. /* e. SYS_FUNC_EN 0x02[7:0] = 0xE2 reset BB TRX function again */
  2812. /* f. SYS_FUNC_EN 0x02[7:0] = 0xE3 enable BB TRX function*/
  2813. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  2814. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  2815. /* g. txpause 0x522[7:0] = 0x00 enable mac tx queue */
  2816. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  2817. }
  2818. static void _rtl92d_phy_set_rfsleep(struct ieee80211_hw *hw)
  2819. {
  2820. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2821. u32 u4btmp;
  2822. u8 delay = 5;
  2823. /* a. TXPAUSE 0x522[7:0] = 0xFF Pause MAC TX queue */
  2824. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
  2825. /* b. RF path 0 offset 0x00 = 0x00 disable RF */
  2826. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
  2827. /* c. APSD_CTRL 0x600[7:0] = 0x40 */
  2828. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
  2829. /* d. APSD_CTRL 0x600[7:0] = 0x00
  2830. * APSD_CTRL 0x600[7:0] = 0x00
  2831. * RF path 0 offset 0x00 = 0x00
  2832. * APSD_CTRL 0x600[7:0] = 0x40
  2833. * */
  2834. u4btmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, RFREG_OFFSET_MASK);
  2835. while (u4btmp != 0 && delay > 0) {
  2836. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x0);
  2837. rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
  2838. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
  2839. u4btmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, RFREG_OFFSET_MASK);
  2840. delay--;
  2841. }
  2842. if (delay == 0) {
  2843. /* Jump out the LPS turn off sequence to RF_ON_EXCEP */
  2844. rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
  2845. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  2846. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
  2847. rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
  2848. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  2849. "Fail !!! Switch RF timeout\n");
  2850. return;
  2851. }
  2852. /* e. For PCIE: SYS_FUNC_EN 0x02[7:0] = 0xE2 reset BB TRX function */
  2853. rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
  2854. /* f. SPS_CTRL 0x11[7:0] = 0x22 */
  2855. if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY)
  2856. rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
  2857. /* g. SYS_CLKR 0x08[11] = 0 gated MAC clock */
  2858. }
  2859. bool rtl92d_phy_set_rf_power_state(struct ieee80211_hw *hw,
  2860. enum rf_pwrstate rfpwr_state)
  2861. {
  2862. bool bresult = true;
  2863. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2864. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  2865. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  2866. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  2867. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  2868. u8 i, queue_id;
  2869. struct rtl8192_tx_ring *ring = NULL;
  2870. if (rfpwr_state == ppsc->rfpwr_state)
  2871. return false;
  2872. switch (rfpwr_state) {
  2873. case ERFON:
  2874. if ((ppsc->rfpwr_state == ERFOFF) &&
  2875. RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
  2876. bool rtstatus;
  2877. u32 InitializeCount = 0;
  2878. do {
  2879. InitializeCount++;
  2880. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  2881. "IPS Set eRf nic enable\n");
  2882. rtstatus = rtl_ps_enable_nic(hw);
  2883. } while (!rtstatus && (InitializeCount < 10));
  2884. RT_CLEAR_PS_LEVEL(ppsc,
  2885. RT_RF_OFF_LEVL_HALT_NIC);
  2886. } else {
  2887. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  2888. "awake, sleeped:%d ms state_inap:%x\n",
  2889. jiffies_to_msecs(jiffies -
  2890. ppsc->last_sleep_jiffies),
  2891. rtlpriv->psc.state_inap);
  2892. ppsc->last_awake_jiffies = jiffies;
  2893. _rtl92d_phy_set_rfon(hw);
  2894. }
  2895. if (mac->link_state == MAC80211_LINKED)
  2896. rtlpriv->cfg->ops->led_control(hw,
  2897. LED_CTL_LINK);
  2898. else
  2899. rtlpriv->cfg->ops->led_control(hw,
  2900. LED_CTL_NO_LINK);
  2901. break;
  2902. case ERFOFF:
  2903. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
  2904. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  2905. "IPS Set eRf nic disable\n");
  2906. rtl_ps_disable_nic(hw);
  2907. RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  2908. } else {
  2909. if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS)
  2910. rtlpriv->cfg->ops->led_control(hw,
  2911. LED_CTL_NO_LINK);
  2912. else
  2913. rtlpriv->cfg->ops->led_control(hw,
  2914. LED_CTL_POWER_OFF);
  2915. }
  2916. break;
  2917. case ERFSLEEP:
  2918. if (ppsc->rfpwr_state == ERFOFF)
  2919. return false;
  2920. for (queue_id = 0, i = 0;
  2921. queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
  2922. ring = &pcipriv->dev.tx_ring[queue_id];
  2923. if (skb_queue_len(&ring->queue) == 0 ||
  2924. queue_id == BEACON_QUEUE) {
  2925. queue_id++;
  2926. continue;
  2927. } else if (rtlpci->pdev->current_state != PCI_D0) {
  2928. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  2929. "eRf Off/Sleep: %d times TcbBusyQueue[%d] !=0 but lower power state!\n",
  2930. i + 1, queue_id);
  2931. break;
  2932. } else {
  2933. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  2934. "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
  2935. i + 1, queue_id,
  2936. skb_queue_len(&ring->queue));
  2937. udelay(10);
  2938. i++;
  2939. }
  2940. if (i >= MAX_DOZE_WAITING_TIMES_9x) {
  2941. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  2942. "ERFOFF: %d times TcbBusyQueue[%d] = %d !\n",
  2943. MAX_DOZE_WAITING_TIMES_9x, queue_id,
  2944. skb_queue_len(&ring->queue));
  2945. break;
  2946. }
  2947. }
  2948. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  2949. "Set rfsleep awaked:%d ms\n",
  2950. jiffies_to_msecs(jiffies - ppsc->last_awake_jiffies));
  2951. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  2952. "sleep awaked:%d ms state_inap:%x\n",
  2953. jiffies_to_msecs(jiffies -
  2954. ppsc->last_awake_jiffies),
  2955. rtlpriv->psc.state_inap);
  2956. ppsc->last_sleep_jiffies = jiffies;
  2957. _rtl92d_phy_set_rfsleep(hw);
  2958. break;
  2959. default:
  2960. pr_err("switch case %#x not processed\n",
  2961. rfpwr_state);
  2962. bresult = false;
  2963. break;
  2964. }
  2965. if (bresult)
  2966. ppsc->rfpwr_state = rfpwr_state;
  2967. return bresult;
  2968. }
  2969. void rtl92d_phy_config_macphymode(struct ieee80211_hw *hw)
  2970. {
  2971. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2972. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  2973. u8 offset = REG_MAC_PHY_CTRL_NORMAL;
  2974. switch (rtlhal->macphymode) {
  2975. case DUALMAC_DUALPHY:
  2976. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  2977. "MacPhyMode: DUALMAC_DUALPHY\n");
  2978. rtl_write_byte(rtlpriv, offset, 0xF3);
  2979. break;
  2980. case SINGLEMAC_SINGLEPHY:
  2981. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  2982. "MacPhyMode: SINGLEMAC_SINGLEPHY\n");
  2983. rtl_write_byte(rtlpriv, offset, 0xF4);
  2984. break;
  2985. case DUALMAC_SINGLEPHY:
  2986. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  2987. "MacPhyMode: DUALMAC_SINGLEPHY\n");
  2988. rtl_write_byte(rtlpriv, offset, 0xF1);
  2989. break;
  2990. }
  2991. }
  2992. void rtl92d_phy_config_macphymode_info(struct ieee80211_hw *hw)
  2993. {
  2994. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2995. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  2996. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  2997. switch (rtlhal->macphymode) {
  2998. case DUALMAC_SINGLEPHY:
  2999. rtlphy->rf_type = RF_2T2R;
  3000. rtlhal->version |= RF_TYPE_2T2R;
  3001. rtlhal->bandset = BAND_ON_BOTH;
  3002. rtlhal->current_bandtype = BAND_ON_2_4G;
  3003. break;
  3004. case SINGLEMAC_SINGLEPHY:
  3005. rtlphy->rf_type = RF_2T2R;
  3006. rtlhal->version |= RF_TYPE_2T2R;
  3007. rtlhal->bandset = BAND_ON_BOTH;
  3008. rtlhal->current_bandtype = BAND_ON_2_4G;
  3009. break;
  3010. case DUALMAC_DUALPHY:
  3011. rtlphy->rf_type = RF_1T1R;
  3012. rtlhal->version &= RF_TYPE_1T1R;
  3013. /* Now we let MAC0 run on 5G band. */
  3014. if (rtlhal->interfaceindex == 0) {
  3015. rtlhal->bandset = BAND_ON_5G;
  3016. rtlhal->current_bandtype = BAND_ON_5G;
  3017. } else {
  3018. rtlhal->bandset = BAND_ON_2_4G;
  3019. rtlhal->current_bandtype = BAND_ON_2_4G;
  3020. }
  3021. break;
  3022. default:
  3023. break;
  3024. }
  3025. }
  3026. u8 rtl92d_get_chnlgroup_fromarray(u8 chnl)
  3027. {
  3028. u8 group;
  3029. u8 channel_info[59] = {
  3030. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  3031. 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56,
  3032. 58, 60, 62, 64, 100, 102, 104, 106, 108,
  3033. 110, 112, 114, 116, 118, 120, 122, 124,
  3034. 126, 128, 130, 132, 134, 136, 138, 140,
  3035. 149, 151, 153, 155, 157, 159, 161, 163,
  3036. 165
  3037. };
  3038. if (channel_info[chnl] <= 3)
  3039. group = 0;
  3040. else if (channel_info[chnl] <= 9)
  3041. group = 1;
  3042. else if (channel_info[chnl] <= 14)
  3043. group = 2;
  3044. else if (channel_info[chnl] <= 44)
  3045. group = 3;
  3046. else if (channel_info[chnl] <= 54)
  3047. group = 4;
  3048. else if (channel_info[chnl] <= 64)
  3049. group = 5;
  3050. else if (channel_info[chnl] <= 112)
  3051. group = 6;
  3052. else if (channel_info[chnl] <= 126)
  3053. group = 7;
  3054. else if (channel_info[chnl] <= 140)
  3055. group = 8;
  3056. else if (channel_info[chnl] <= 153)
  3057. group = 9;
  3058. else if (channel_info[chnl] <= 159)
  3059. group = 10;
  3060. else
  3061. group = 11;
  3062. return group;
  3063. }
  3064. void rtl92d_phy_set_poweron(struct ieee80211_hw *hw)
  3065. {
  3066. struct rtl_priv *rtlpriv = rtl_priv(hw);
  3067. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  3068. unsigned long flags;
  3069. u8 value8;
  3070. u16 i;
  3071. u32 mac_reg = (rtlhal->interfaceindex == 0 ? REG_MAC0 : REG_MAC1);
  3072. /* notice fw know band status 0x81[1]/0x53[1] = 0: 5G, 1: 2G */
  3073. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  3074. value8 = rtl_read_byte(rtlpriv, mac_reg);
  3075. value8 |= BIT(1);
  3076. rtl_write_byte(rtlpriv, mac_reg, value8);
  3077. } else {
  3078. value8 = rtl_read_byte(rtlpriv, mac_reg);
  3079. value8 &= (~BIT(1));
  3080. rtl_write_byte(rtlpriv, mac_reg, value8);
  3081. }
  3082. if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY) {
  3083. value8 = rtl_read_byte(rtlpriv, REG_MAC0);
  3084. rtl_write_byte(rtlpriv, REG_MAC0, value8 | MAC0_ON);
  3085. } else {
  3086. spin_lock_irqsave(&globalmutex_power, flags);
  3087. if (rtlhal->interfaceindex == 0) {
  3088. value8 = rtl_read_byte(rtlpriv, REG_MAC0);
  3089. rtl_write_byte(rtlpriv, REG_MAC0, value8 | MAC0_ON);
  3090. } else {
  3091. value8 = rtl_read_byte(rtlpriv, REG_MAC1);
  3092. rtl_write_byte(rtlpriv, REG_MAC1, value8 | MAC1_ON);
  3093. }
  3094. value8 = rtl_read_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS);
  3095. spin_unlock_irqrestore(&globalmutex_power, flags);
  3096. for (i = 0; i < 200; i++) {
  3097. if ((value8 & BIT(7)) == 0) {
  3098. break;
  3099. } else {
  3100. udelay(500);
  3101. spin_lock_irqsave(&globalmutex_power, flags);
  3102. value8 = rtl_read_byte(rtlpriv,
  3103. REG_POWER_OFF_IN_PROCESS);
  3104. spin_unlock_irqrestore(&globalmutex_power,
  3105. flags);
  3106. }
  3107. }
  3108. if (i == 200)
  3109. WARN_ONCE(true, "rtl8192de: Another mac power off over time\n");
  3110. }
  3111. }
  3112. void rtl92d_phy_config_maccoexist_rfpage(struct ieee80211_hw *hw)
  3113. {
  3114. struct rtl_priv *rtlpriv = rtl_priv(hw);
  3115. switch (rtlpriv->rtlhal.macphymode) {
  3116. case DUALMAC_DUALPHY:
  3117. rtl_write_byte(rtlpriv, REG_DMC, 0x0);
  3118. rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x08);
  3119. rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x13ff);
  3120. break;
  3121. case DUALMAC_SINGLEPHY:
  3122. rtl_write_byte(rtlpriv, REG_DMC, 0xf8);
  3123. rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x08);
  3124. rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x13ff);
  3125. break;
  3126. case SINGLEMAC_SINGLEPHY:
  3127. rtl_write_byte(rtlpriv, REG_DMC, 0x0);
  3128. rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x10);
  3129. rtl_write_word(rtlpriv, (REG_TRXFF_BNDY + 2), 0x27FF);
  3130. break;
  3131. default:
  3132. break;
  3133. }
  3134. }
  3135. void rtl92d_update_bbrf_configuration(struct ieee80211_hw *hw)
  3136. {
  3137. struct rtl_priv *rtlpriv = rtl_priv(hw);
  3138. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  3139. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  3140. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  3141. u8 rfpath, i;
  3142. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "==>\n");
  3143. /* r_select_5G for path_A/B 0 for 2.4G, 1 for 5G */
  3144. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  3145. /* r_select_5G for path_A/B,0x878 */
  3146. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(0), 0x0);
  3147. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0x0);
  3148. if (rtlhal->macphymode != DUALMAC_DUALPHY) {
  3149. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(16), 0x0);
  3150. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(31), 0x0);
  3151. }
  3152. /* rssi_table_select:index 0 for 2.4G.1~3 for 5G,0xc78 */
  3153. rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, BIT(6) | BIT(7), 0x0);
  3154. /* fc_area 0xd2c */
  3155. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(14) | BIT(13), 0x0);
  3156. /* 5G LAN ON */
  3157. rtl_set_bbreg(hw, 0xB30, 0x00F00000, 0xa);
  3158. /* TX BB gain shift*1,Just for testchip,0xc80,0xc88 */
  3159. rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, MASKDWORD,
  3160. 0x40000100);
  3161. rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, MASKDWORD,
  3162. 0x40000100);
  3163. if (rtlhal->macphymode == DUALMAC_DUALPHY) {
  3164. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
  3165. BIT(10) | BIT(6) | BIT(5),
  3166. ((rtlefuse->eeprom_c9 & BIT(3)) >> 3) |
  3167. (rtlefuse->eeprom_c9 & BIT(1)) |
  3168. ((rtlefuse->eeprom_cc & BIT(1)) << 4));
  3169. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE,
  3170. BIT(10) | BIT(6) | BIT(5),
  3171. ((rtlefuse->eeprom_c9 & BIT(2)) >> 2) |
  3172. ((rtlefuse->eeprom_c9 & BIT(0)) << 1) |
  3173. ((rtlefuse->eeprom_cc & BIT(0)) << 5));
  3174. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0);
  3175. } else {
  3176. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
  3177. BIT(26) | BIT(22) | BIT(21) | BIT(10) |
  3178. BIT(6) | BIT(5),
  3179. ((rtlefuse->eeprom_c9 & BIT(3)) >> 3) |
  3180. (rtlefuse->eeprom_c9 & BIT(1)) |
  3181. ((rtlefuse->eeprom_cc & BIT(1)) << 4) |
  3182. ((rtlefuse->eeprom_c9 & BIT(7)) << 9) |
  3183. ((rtlefuse->eeprom_c9 & BIT(5)) << 12) |
  3184. ((rtlefuse->eeprom_cc & BIT(3)) << 18));
  3185. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE,
  3186. BIT(10) | BIT(6) | BIT(5),
  3187. ((rtlefuse->eeprom_c9 & BIT(2)) >> 2) |
  3188. ((rtlefuse->eeprom_c9 & BIT(0)) << 1) |
  3189. ((rtlefuse->eeprom_cc & BIT(0)) << 5));
  3190. rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE,
  3191. BIT(10) | BIT(6) | BIT(5),
  3192. ((rtlefuse->eeprom_c9 & BIT(6)) >> 6) |
  3193. ((rtlefuse->eeprom_c9 & BIT(4)) >> 3) |
  3194. ((rtlefuse->eeprom_cc & BIT(2)) << 3));
  3195. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
  3196. BIT(31) | BIT(15), 0);
  3197. }
  3198. /* 1.5V_LDO */
  3199. } else {
  3200. /* r_select_5G for path_A/B */
  3201. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(0), 0x1);
  3202. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0x1);
  3203. if (rtlhal->macphymode != DUALMAC_DUALPHY) {
  3204. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(16), 0x1);
  3205. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(31), 0x1);
  3206. }
  3207. /* rssi_table_select:index 0 for 2.4G.1~3 for 5G */
  3208. rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, BIT(6) | BIT(7), 0x1);
  3209. /* fc_area */
  3210. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(14) | BIT(13), 0x1);
  3211. /* 5G LAN ON */
  3212. rtl_set_bbreg(hw, 0xB30, 0x00F00000, 0x0);
  3213. /* TX BB gain shift,Just for testchip,0xc80,0xc88 */
  3214. if (rtlefuse->internal_pa_5g[0])
  3215. rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, MASKDWORD,
  3216. 0x2d4000b5);
  3217. else
  3218. rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, MASKDWORD,
  3219. 0x20000080);
  3220. if (rtlefuse->internal_pa_5g[1])
  3221. rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, MASKDWORD,
  3222. 0x2d4000b5);
  3223. else
  3224. rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, MASKDWORD,
  3225. 0x20000080);
  3226. if (rtlhal->macphymode == DUALMAC_DUALPHY) {
  3227. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
  3228. BIT(10) | BIT(6) | BIT(5),
  3229. (rtlefuse->eeprom_cc & BIT(5)));
  3230. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BIT(10),
  3231. ((rtlefuse->eeprom_cc & BIT(4)) >> 4));
  3232. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15),
  3233. (rtlefuse->eeprom_cc & BIT(4)) >> 4);
  3234. } else {
  3235. rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
  3236. BIT(26) | BIT(22) | BIT(21) | BIT(10) |
  3237. BIT(6) | BIT(5),
  3238. (rtlefuse->eeprom_cc & BIT(5)) |
  3239. ((rtlefuse->eeprom_cc & BIT(7)) << 14));
  3240. rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BIT(10),
  3241. ((rtlefuse->eeprom_cc & BIT(4)) >> 4));
  3242. rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, BIT(10),
  3243. ((rtlefuse->eeprom_cc & BIT(6)) >> 6));
  3244. rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
  3245. BIT(31) | BIT(15),
  3246. ((rtlefuse->eeprom_cc & BIT(4)) >> 4) |
  3247. ((rtlefuse->eeprom_cc & BIT(6)) << 10));
  3248. }
  3249. }
  3250. /* update IQK related settings */
  3251. rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, MASKDWORD, 0x40000100);
  3252. rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, MASKDWORD, 0x40000100);
  3253. rtl_set_bbreg(hw, ROFDM0_XCTxAFE, 0xF0000000, 0x00);
  3254. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(30) | BIT(28) |
  3255. BIT(26) | BIT(24), 0x00);
  3256. rtl_set_bbreg(hw, ROFDM0_XDTxAFE, 0xF0000000, 0x00);
  3257. rtl_set_bbreg(hw, 0xca0, 0xF0000000, 0x00);
  3258. rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, 0x0000F000, 0x00);
  3259. /* Update RF */
  3260. for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
  3261. rfpath++) {
  3262. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  3263. /* MOD_AG for RF path_A 0x18 BIT8,BIT16 */
  3264. rtl_set_rfreg(hw, rfpath, RF_CHNLBW, BIT(8) | BIT(16) |
  3265. BIT(18), 0);
  3266. /* RF0x0b[16:14] =3b'111 */
  3267. rtl_set_rfreg(hw, (enum radio_path)rfpath, 0x0B,
  3268. 0x1c000, 0x07);
  3269. } else {
  3270. /* MOD_AG for RF path_A 0x18 BIT8,BIT16 */
  3271. rtl_set_rfreg(hw, rfpath, RF_CHNLBW, BIT(8) |
  3272. BIT(16) | BIT(18),
  3273. (BIT(16) | BIT(8)) >> 8);
  3274. }
  3275. }
  3276. /* Update for all band. */
  3277. /* DMDP */
  3278. if (rtlphy->rf_type == RF_1T1R) {
  3279. /* Use antenna 0,0xc04,0xd04 */
  3280. rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x11);
  3281. rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x1);
  3282. /* enable ad/da clock1 for dual-phy reg0x888 */
  3283. if (rtlhal->interfaceindex == 0) {
  3284. rtl_set_bbreg(hw, RFPGA0_ADDALLOCKEN, BIT(12) |
  3285. BIT(13), 0x3);
  3286. } else {
  3287. rtl92d_phy_enable_anotherphy(hw, false);
  3288. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  3289. "MAC1 use DBI to update 0x888\n");
  3290. /* 0x888 */
  3291. rtl92de_write_dword_dbi(hw, RFPGA0_ADDALLOCKEN,
  3292. rtl92de_read_dword_dbi(hw,
  3293. RFPGA0_ADDALLOCKEN,
  3294. BIT(3)) | BIT(12) | BIT(13),
  3295. BIT(3));
  3296. rtl92d_phy_powerdown_anotherphy(hw, false);
  3297. }
  3298. } else {
  3299. /* Single PHY */
  3300. /* Use antenna 0 & 1,0xc04,0xd04 */
  3301. rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x33);
  3302. rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x3);
  3303. /* disable ad/da clock1,0x888 */
  3304. rtl_set_bbreg(hw, RFPGA0_ADDALLOCKEN, BIT(12) | BIT(13), 0);
  3305. }
  3306. for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
  3307. rfpath++) {
  3308. rtlphy->rfreg_chnlval[rfpath] = rtl_get_rfreg(hw, rfpath,
  3309. RF_CHNLBW, RFREG_OFFSET_MASK);
  3310. rtlphy->reg_rf3c[rfpath] = rtl_get_rfreg(hw, rfpath, 0x3C,
  3311. RFREG_OFFSET_MASK);
  3312. }
  3313. for (i = 0; i < 2; i++)
  3314. RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "RF 0x18 = 0x%x\n",
  3315. rtlphy->rfreg_chnlval[i]);
  3316. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "<==\n");
  3317. }
  3318. bool rtl92d_phy_check_poweroff(struct ieee80211_hw *hw)
  3319. {
  3320. struct rtl_priv *rtlpriv = rtl_priv(hw);
  3321. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  3322. u8 u1btmp;
  3323. unsigned long flags;
  3324. if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY) {
  3325. u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
  3326. rtl_write_byte(rtlpriv, REG_MAC0, u1btmp & (~MAC0_ON));
  3327. return true;
  3328. }
  3329. spin_lock_irqsave(&globalmutex_power, flags);
  3330. if (rtlhal->interfaceindex == 0) {
  3331. u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
  3332. rtl_write_byte(rtlpriv, REG_MAC0, u1btmp & (~MAC0_ON));
  3333. u1btmp = rtl_read_byte(rtlpriv, REG_MAC1);
  3334. u1btmp &= MAC1_ON;
  3335. } else {
  3336. u1btmp = rtl_read_byte(rtlpriv, REG_MAC1);
  3337. rtl_write_byte(rtlpriv, REG_MAC1, u1btmp & (~MAC1_ON));
  3338. u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
  3339. u1btmp &= MAC0_ON;
  3340. }
  3341. if (u1btmp) {
  3342. spin_unlock_irqrestore(&globalmutex_power, flags);
  3343. return false;
  3344. }
  3345. u1btmp = rtl_read_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS);
  3346. u1btmp |= BIT(7);
  3347. rtl_write_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS, u1btmp);
  3348. spin_unlock_irqrestore(&globalmutex_power, flags);
  3349. return true;
  3350. }