phy_cmn.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/bitops.h>
  19. #include <brcm_hw_ids.h>
  20. #include <chipcommon.h>
  21. #include <aiutils.h>
  22. #include <d11.h>
  23. #include <phy_shim.h>
  24. #include "phy_hal.h"
  25. #include "phy_int.h"
  26. #include "phy_radio.h"
  27. #include "phy_lcn.h"
  28. #include "phyreg_n.h"
  29. #define VALID_N_RADIO(radioid) ((radioid == BCM2055_ID) || \
  30. (radioid == BCM2056_ID) || \
  31. (radioid == BCM2057_ID))
  32. #define VALID_LCN_RADIO(radioid) (radioid == BCM2064_ID)
  33. #define VALID_RADIO(pi, radioid) ( \
  34. (ISNPHY(pi) ? VALID_N_RADIO(radioid) : false) || \
  35. (ISLCNPHY(pi) ? VALID_LCN_RADIO(radioid) : false))
  36. /* basic mux operation - can be optimized on several architectures */
  37. #define MUX(pred, true, false) ((pred) ? (true) : (false))
  38. /* modulo inc/dec - assumes x E [0, bound - 1] */
  39. #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
  40. /* modulo inc/dec, bound = 2^k */
  41. #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
  42. #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
  43. struct chan_info_basic {
  44. u16 chan;
  45. u16 freq;
  46. };
  47. static const struct chan_info_basic chan_info_all[] = {
  48. {1, 2412},
  49. {2, 2417},
  50. {3, 2422},
  51. {4, 2427},
  52. {5, 2432},
  53. {6, 2437},
  54. {7, 2442},
  55. {8, 2447},
  56. {9, 2452},
  57. {10, 2457},
  58. {11, 2462},
  59. {12, 2467},
  60. {13, 2472},
  61. {14, 2484},
  62. {34, 5170},
  63. {38, 5190},
  64. {42, 5210},
  65. {46, 5230},
  66. {36, 5180},
  67. {40, 5200},
  68. {44, 5220},
  69. {48, 5240},
  70. {52, 5260},
  71. {56, 5280},
  72. {60, 5300},
  73. {64, 5320},
  74. {100, 5500},
  75. {104, 5520},
  76. {108, 5540},
  77. {112, 5560},
  78. {116, 5580},
  79. {120, 5600},
  80. {124, 5620},
  81. {128, 5640},
  82. {132, 5660},
  83. {136, 5680},
  84. {140, 5700},
  85. {149, 5745},
  86. {153, 5765},
  87. {157, 5785},
  88. {161, 5805},
  89. {165, 5825},
  90. {184, 4920},
  91. {188, 4940},
  92. {192, 4960},
  93. {196, 4980},
  94. {200, 5000},
  95. {204, 5020},
  96. {208, 5040},
  97. {212, 5060},
  98. {216, 50800}
  99. };
  100. const u8 ofdm_rate_lookup[] = {
  101. BRCM_RATE_48M,
  102. BRCM_RATE_24M,
  103. BRCM_RATE_12M,
  104. BRCM_RATE_6M,
  105. BRCM_RATE_54M,
  106. BRCM_RATE_36M,
  107. BRCM_RATE_18M,
  108. BRCM_RATE_9M
  109. };
  110. #define PHY_WREG_LIMIT 24
  111. void wlc_phyreg_enter(struct brcms_phy_pub *pih)
  112. {
  113. struct brcms_phy *pi = (struct brcms_phy *) pih;
  114. wlapi_bmac_ucode_wake_override_phyreg_set(pi->sh->physhim);
  115. }
  116. void wlc_phyreg_exit(struct brcms_phy_pub *pih)
  117. {
  118. struct brcms_phy *pi = (struct brcms_phy *) pih;
  119. wlapi_bmac_ucode_wake_override_phyreg_clear(pi->sh->physhim);
  120. }
  121. void wlc_radioreg_enter(struct brcms_phy_pub *pih)
  122. {
  123. struct brcms_phy *pi = (struct brcms_phy *) pih;
  124. wlapi_bmac_mctrl(pi->sh->physhim, MCTL_LOCK_RADIO, MCTL_LOCK_RADIO);
  125. udelay(10);
  126. }
  127. void wlc_radioreg_exit(struct brcms_phy_pub *pih)
  128. {
  129. struct brcms_phy *pi = (struct brcms_phy *) pih;
  130. u16 dummy;
  131. dummy = R_REG(&pi->regs->phyversion);
  132. pi->phy_wreg = 0;
  133. wlapi_bmac_mctrl(pi->sh->physhim, MCTL_LOCK_RADIO, 0);
  134. }
  135. u16 read_radio_reg(struct brcms_phy *pi, u16 addr)
  136. {
  137. u16 data;
  138. if ((addr == RADIO_IDCODE))
  139. return 0xffff;
  140. switch (pi->pubpi.phy_type) {
  141. case PHY_TYPE_N:
  142. if (!CONF_HAS(PHYTYPE, PHY_TYPE_N))
  143. break;
  144. if (NREV_GE(pi->pubpi.phy_rev, 7))
  145. addr |= RADIO_2057_READ_OFF;
  146. else
  147. addr |= RADIO_2055_READ_OFF;
  148. break;
  149. case PHY_TYPE_LCN:
  150. if (!CONF_HAS(PHYTYPE, PHY_TYPE_LCN))
  151. break;
  152. addr |= RADIO_2064_READ_OFF;
  153. break;
  154. default:
  155. break;
  156. }
  157. if ((D11REV_GE(pi->sh->corerev, 24)) ||
  158. (D11REV_IS(pi->sh->corerev, 22)
  159. && (pi->pubpi.phy_type != PHY_TYPE_SSN))) {
  160. W_REG_FLUSH(&pi->regs->radioregaddr, addr);
  161. data = R_REG(&pi->regs->radioregdata);
  162. } else {
  163. W_REG_FLUSH(&pi->regs->phy4waddr, addr);
  164. #ifdef __ARM_ARCH_4T__
  165. __asm__(" .align 4 ");
  166. __asm__(" nop ");
  167. data = R_REG(&pi->regs->phy4wdatalo);
  168. #else
  169. data = R_REG(&pi->regs->phy4wdatalo);
  170. #endif
  171. }
  172. pi->phy_wreg = 0;
  173. return data;
  174. }
  175. void write_radio_reg(struct brcms_phy *pi, u16 addr, u16 val)
  176. {
  177. if ((D11REV_GE(pi->sh->corerev, 24)) ||
  178. (D11REV_IS(pi->sh->corerev, 22)
  179. && (pi->pubpi.phy_type != PHY_TYPE_SSN))) {
  180. W_REG_FLUSH(&pi->regs->radioregaddr, addr);
  181. W_REG(&pi->regs->radioregdata, val);
  182. } else {
  183. W_REG_FLUSH(&pi->regs->phy4waddr, addr);
  184. W_REG(&pi->regs->phy4wdatalo, val);
  185. }
  186. if (++pi->phy_wreg >= pi->phy_wreg_limit) {
  187. (void)R_REG(&pi->regs->maccontrol);
  188. pi->phy_wreg = 0;
  189. }
  190. }
  191. static u32 read_radio_id(struct brcms_phy *pi)
  192. {
  193. u32 id;
  194. if (D11REV_GE(pi->sh->corerev, 24)) {
  195. u32 b0, b1, b2;
  196. W_REG_FLUSH(&pi->regs->radioregaddr, 0);
  197. b0 = (u32) R_REG(&pi->regs->radioregdata);
  198. W_REG_FLUSH(&pi->regs->radioregaddr, 1);
  199. b1 = (u32) R_REG(&pi->regs->radioregdata);
  200. W_REG_FLUSH(&pi->regs->radioregaddr, 2);
  201. b2 = (u32) R_REG(&pi->regs->radioregdata);
  202. id = ((b0 & 0xf) << 28) | (((b2 << 8) | b1) << 12) | ((b0 >> 4)
  203. & 0xf);
  204. } else {
  205. W_REG_FLUSH(&pi->regs->phy4waddr, RADIO_IDCODE);
  206. id = (u32) R_REG(&pi->regs->phy4wdatalo);
  207. id |= (u32) R_REG(&pi->regs->phy4wdatahi) << 16;
  208. }
  209. pi->phy_wreg = 0;
  210. return id;
  211. }
  212. void and_radio_reg(struct brcms_phy *pi, u16 addr, u16 val)
  213. {
  214. u16 rval;
  215. rval = read_radio_reg(pi, addr);
  216. write_radio_reg(pi, addr, (rval & val));
  217. }
  218. void or_radio_reg(struct brcms_phy *pi, u16 addr, u16 val)
  219. {
  220. u16 rval;
  221. rval = read_radio_reg(pi, addr);
  222. write_radio_reg(pi, addr, (rval | val));
  223. }
  224. void xor_radio_reg(struct brcms_phy *pi, u16 addr, u16 mask)
  225. {
  226. u16 rval;
  227. rval = read_radio_reg(pi, addr);
  228. write_radio_reg(pi, addr, (rval ^ mask));
  229. }
  230. void mod_radio_reg(struct brcms_phy *pi, u16 addr, u16 mask, u16 val)
  231. {
  232. u16 rval;
  233. rval = read_radio_reg(pi, addr);
  234. write_radio_reg(pi, addr, (rval & ~mask) | (val & mask));
  235. }
  236. void write_phy_channel_reg(struct brcms_phy *pi, uint val)
  237. {
  238. W_REG(&pi->regs->phychannel, val);
  239. }
  240. u16 read_phy_reg(struct brcms_phy *pi, u16 addr)
  241. {
  242. struct d11regs __iomem *regs;
  243. regs = pi->regs;
  244. W_REG_FLUSH(&regs->phyregaddr, addr);
  245. pi->phy_wreg = 0;
  246. return R_REG(&regs->phyregdata);
  247. }
  248. void write_phy_reg(struct brcms_phy *pi, u16 addr, u16 val)
  249. {
  250. struct d11regs __iomem *regs;
  251. regs = pi->regs;
  252. #ifdef CONFIG_BCM47XX
  253. W_REG_FLUSH(&regs->phyregaddr, addr);
  254. W_REG(&regs->phyregdata, val);
  255. if (addr == 0x72)
  256. (void)R_REG(&regs->phyregdata);
  257. #else
  258. W_REG((u32 __iomem *)(&regs->phyregaddr), addr | (val << 16));
  259. if (++pi->phy_wreg >= pi->phy_wreg_limit) {
  260. pi->phy_wreg = 0;
  261. (void)R_REG(&regs->phyversion);
  262. }
  263. #endif
  264. }
  265. void and_phy_reg(struct brcms_phy *pi, u16 addr, u16 val)
  266. {
  267. struct d11regs __iomem *regs;
  268. regs = pi->regs;
  269. W_REG_FLUSH(&regs->phyregaddr, addr);
  270. W_REG(&regs->phyregdata, (R_REG(&regs->phyregdata) & val));
  271. pi->phy_wreg = 0;
  272. }
  273. void or_phy_reg(struct brcms_phy *pi, u16 addr, u16 val)
  274. {
  275. struct d11regs __iomem *regs;
  276. regs = pi->regs;
  277. W_REG_FLUSH(&regs->phyregaddr, addr);
  278. W_REG(&regs->phyregdata, (R_REG(&regs->phyregdata) | val));
  279. pi->phy_wreg = 0;
  280. }
  281. void mod_phy_reg(struct brcms_phy *pi, u16 addr, u16 mask, u16 val)
  282. {
  283. struct d11regs __iomem *regs;
  284. regs = pi->regs;
  285. W_REG_FLUSH(&regs->phyregaddr, addr);
  286. W_REG(&regs->phyregdata,
  287. ((R_REG(&regs->phyregdata) & ~mask) | (val & mask)));
  288. pi->phy_wreg = 0;
  289. }
  290. static void wlc_set_phy_uninitted(struct brcms_phy *pi)
  291. {
  292. int i, j;
  293. pi->initialized = false;
  294. pi->tx_vos = 0xffff;
  295. pi->nrssi_table_delta = 0x7fffffff;
  296. pi->rc_cal = 0xffff;
  297. pi->mintxbias = 0xffff;
  298. pi->txpwridx = -1;
  299. if (ISNPHY(pi)) {
  300. pi->phy_spuravoid = SPURAVOID_DISABLE;
  301. if (NREV_GE(pi->pubpi.phy_rev, 3)
  302. && NREV_LT(pi->pubpi.phy_rev, 7))
  303. pi->phy_spuravoid = SPURAVOID_AUTO;
  304. pi->nphy_papd_skip = 0;
  305. pi->nphy_papd_epsilon_offset[0] = 0xf588;
  306. pi->nphy_papd_epsilon_offset[1] = 0xf588;
  307. pi->nphy_txpwr_idx[0] = 128;
  308. pi->nphy_txpwr_idx[1] = 128;
  309. pi->nphy_txpwrindex[0].index_internal = 40;
  310. pi->nphy_txpwrindex[1].index_internal = 40;
  311. pi->phy_pabias = 0;
  312. } else {
  313. pi->phy_spuravoid = SPURAVOID_AUTO;
  314. }
  315. pi->radiopwr = 0xffff;
  316. for (i = 0; i < STATIC_NUM_RF; i++) {
  317. for (j = 0; j < STATIC_NUM_BB; j++)
  318. pi->stats_11b_txpower[i][j] = -1;
  319. }
  320. }
  321. struct shared_phy *wlc_phy_shared_attach(struct shared_phy_params *shp)
  322. {
  323. struct shared_phy *sh;
  324. sh = kzalloc(sizeof(struct shared_phy), GFP_ATOMIC);
  325. if (sh == NULL)
  326. return NULL;
  327. sh->sih = shp->sih;
  328. sh->physhim = shp->physhim;
  329. sh->unit = shp->unit;
  330. sh->corerev = shp->corerev;
  331. sh->vid = shp->vid;
  332. sh->did = shp->did;
  333. sh->chip = shp->chip;
  334. sh->chiprev = shp->chiprev;
  335. sh->chippkg = shp->chippkg;
  336. sh->sromrev = shp->sromrev;
  337. sh->boardtype = shp->boardtype;
  338. sh->boardrev = shp->boardrev;
  339. sh->boardvendor = shp->boardvendor;
  340. sh->boardflags = shp->boardflags;
  341. sh->boardflags2 = shp->boardflags2;
  342. sh->buscorerev = shp->buscorerev;
  343. sh->fast_timer = PHY_SW_TIMER_FAST;
  344. sh->slow_timer = PHY_SW_TIMER_SLOW;
  345. sh->glacial_timer = PHY_SW_TIMER_GLACIAL;
  346. sh->rssi_mode = RSSI_ANT_MERGE_MAX;
  347. return sh;
  348. }
  349. static void wlc_phy_timercb_phycal(struct brcms_phy *pi)
  350. {
  351. uint delay = 5;
  352. if (PHY_PERICAL_MPHASE_PENDING(pi)) {
  353. if (!pi->sh->up) {
  354. wlc_phy_cal_perical_mphase_reset(pi);
  355. return;
  356. }
  357. if (SCAN_RM_IN_PROGRESS(pi) || PLT_INPROG_PHY(pi)) {
  358. delay = 1000;
  359. wlc_phy_cal_perical_mphase_restart(pi);
  360. } else
  361. wlc_phy_cal_perical_nphy_run(pi, PHY_PERICAL_AUTO);
  362. wlapi_add_timer(pi->phycal_timer, delay, 0);
  363. return;
  364. }
  365. }
  366. static u32 wlc_phy_get_radio_ver(struct brcms_phy *pi)
  367. {
  368. u32 ver;
  369. ver = read_radio_id(pi);
  370. return ver;
  371. }
  372. struct brcms_phy_pub *
  373. wlc_phy_attach(struct shared_phy *sh, struct d11regs __iomem *regs,
  374. int bandtype, struct wiphy *wiphy)
  375. {
  376. struct brcms_phy *pi;
  377. u32 sflags = 0;
  378. uint phyversion;
  379. u32 idcode;
  380. int i;
  381. if (D11REV_IS(sh->corerev, 4))
  382. sflags = SISF_2G_PHY | SISF_5G_PHY;
  383. else
  384. sflags = ai_core_sflags(sh->sih, 0, 0);
  385. if (bandtype == BRCM_BAND_5G) {
  386. if ((sflags & (SISF_5G_PHY | SISF_DB_PHY)) == 0)
  387. return NULL;
  388. }
  389. pi = sh->phy_head;
  390. if ((sflags & SISF_DB_PHY) && pi) {
  391. wlapi_bmac_corereset(pi->sh->physhim, pi->pubpi.coreflags);
  392. pi->refcnt++;
  393. return &pi->pubpi_ro;
  394. }
  395. pi = kzalloc(sizeof(struct brcms_phy), GFP_ATOMIC);
  396. if (pi == NULL)
  397. return NULL;
  398. pi->wiphy = wiphy;
  399. pi->regs = regs;
  400. pi->sh = sh;
  401. pi->phy_init_por = true;
  402. pi->phy_wreg_limit = PHY_WREG_LIMIT;
  403. pi->txpwr_percent = 100;
  404. pi->do_initcal = true;
  405. pi->phycal_tempdelta = 0;
  406. if (bandtype == BRCM_BAND_2G && (sflags & SISF_2G_PHY))
  407. pi->pubpi.coreflags = SICF_GMODE;
  408. wlapi_bmac_corereset(pi->sh->physhim, pi->pubpi.coreflags);
  409. phyversion = R_REG(&pi->regs->phyversion);
  410. pi->pubpi.phy_type = PHY_TYPE(phyversion);
  411. pi->pubpi.phy_rev = phyversion & PV_PV_MASK;
  412. if (pi->pubpi.phy_type == PHY_TYPE_LCNXN) {
  413. pi->pubpi.phy_type = PHY_TYPE_N;
  414. pi->pubpi.phy_rev += LCNXN_BASEREV;
  415. }
  416. pi->pubpi.phy_corenum = PHY_CORE_NUM_2;
  417. pi->pubpi.ana_rev = (phyversion & PV_AV_MASK) >> PV_AV_SHIFT;
  418. if (!pi->pubpi.phy_type == PHY_TYPE_N &&
  419. !pi->pubpi.phy_type == PHY_TYPE_LCN)
  420. goto err;
  421. if (bandtype == BRCM_BAND_5G) {
  422. if (!ISNPHY(pi))
  423. goto err;
  424. } else if (!ISNPHY(pi) && !ISLCNPHY(pi)) {
  425. goto err;
  426. }
  427. wlc_phy_anacore((struct brcms_phy_pub *) pi, ON);
  428. idcode = wlc_phy_get_radio_ver(pi);
  429. pi->pubpi.radioid =
  430. (idcode & IDCODE_ID_MASK) >> IDCODE_ID_SHIFT;
  431. pi->pubpi.radiorev =
  432. (idcode & IDCODE_REV_MASK) >> IDCODE_REV_SHIFT;
  433. pi->pubpi.radiover =
  434. (idcode & IDCODE_VER_MASK) >> IDCODE_VER_SHIFT;
  435. if (!VALID_RADIO(pi, pi->pubpi.radioid))
  436. goto err;
  437. wlc_phy_switch_radio((struct brcms_phy_pub *) pi, OFF);
  438. wlc_set_phy_uninitted(pi);
  439. pi->bw = WL_CHANSPEC_BW_20;
  440. pi->radio_chanspec = (bandtype == BRCM_BAND_2G) ?
  441. ch20mhz_chspec(1) : ch20mhz_chspec(36);
  442. pi->rxiq_samps = PHY_NOISE_SAMPLE_LOG_NUM_NPHY;
  443. pi->rxiq_antsel = ANT_RX_DIV_DEF;
  444. pi->watchdog_override = true;
  445. pi->cal_type_override = PHY_PERICAL_AUTO;
  446. pi->nphy_saved_noisevars.bufcount = 0;
  447. if (ISNPHY(pi))
  448. pi->min_txpower = PHY_TXPWR_MIN_NPHY;
  449. else
  450. pi->min_txpower = PHY_TXPWR_MIN;
  451. pi->sh->phyrxchain = 0x3;
  452. pi->rx2tx_biasentry = -1;
  453. pi->phy_txcore_disable_temp = PHY_CHAIN_TX_DISABLE_TEMP;
  454. pi->phy_txcore_enable_temp =
  455. PHY_CHAIN_TX_DISABLE_TEMP - PHY_HYSTERESIS_DELTATEMP;
  456. pi->phy_tempsense_offset = 0;
  457. pi->phy_txcore_heatedup = false;
  458. pi->nphy_lastcal_temp = -50;
  459. pi->phynoise_polling = true;
  460. if (ISNPHY(pi) || ISLCNPHY(pi))
  461. pi->phynoise_polling = false;
  462. for (i = 0; i < TXP_NUM_RATES; i++) {
  463. pi->txpwr_limit[i] = BRCMS_TXPWR_MAX;
  464. pi->txpwr_env_limit[i] = BRCMS_TXPWR_MAX;
  465. pi->tx_user_target[i] = BRCMS_TXPWR_MAX;
  466. }
  467. pi->radiopwr_override = RADIOPWR_OVERRIDE_DEF;
  468. pi->user_txpwr_at_rfport = false;
  469. if (ISNPHY(pi)) {
  470. pi->phycal_timer = wlapi_init_timer(pi->sh->physhim,
  471. wlc_phy_timercb_phycal,
  472. pi, "phycal");
  473. if (!pi->phycal_timer)
  474. goto err;
  475. if (!wlc_phy_attach_nphy(pi))
  476. goto err;
  477. } else if (ISLCNPHY(pi)) {
  478. if (!wlc_phy_attach_lcnphy(pi))
  479. goto err;
  480. }
  481. pi->refcnt++;
  482. pi->next = pi->sh->phy_head;
  483. sh->phy_head = pi;
  484. memcpy(&pi->pubpi_ro, &pi->pubpi, sizeof(struct brcms_phy_pub));
  485. return &pi->pubpi_ro;
  486. err:
  487. kfree(pi);
  488. return NULL;
  489. }
  490. void wlc_phy_detach(struct brcms_phy_pub *pih)
  491. {
  492. struct brcms_phy *pi = (struct brcms_phy *) pih;
  493. if (pih) {
  494. if (--pi->refcnt)
  495. return;
  496. if (pi->phycal_timer) {
  497. wlapi_free_timer(pi->phycal_timer);
  498. pi->phycal_timer = NULL;
  499. }
  500. if (pi->sh->phy_head == pi)
  501. pi->sh->phy_head = pi->next;
  502. else if (pi->sh->phy_head->next == pi)
  503. pi->sh->phy_head->next = NULL;
  504. if (pi->pi_fptr.detach)
  505. (pi->pi_fptr.detach)(pi);
  506. kfree(pi);
  507. }
  508. }
  509. bool
  510. wlc_phy_get_phyversion(struct brcms_phy_pub *pih, u16 *phytype, u16 *phyrev,
  511. u16 *radioid, u16 *radiover)
  512. {
  513. struct brcms_phy *pi = (struct brcms_phy *) pih;
  514. *phytype = (u16) pi->pubpi.phy_type;
  515. *phyrev = (u16) pi->pubpi.phy_rev;
  516. *radioid = pi->pubpi.radioid;
  517. *radiover = pi->pubpi.radiorev;
  518. return true;
  519. }
  520. bool wlc_phy_get_encore(struct brcms_phy_pub *pih)
  521. {
  522. struct brcms_phy *pi = (struct brcms_phy *) pih;
  523. return pi->pubpi.abgphy_encore;
  524. }
  525. u32 wlc_phy_get_coreflags(struct brcms_phy_pub *pih)
  526. {
  527. struct brcms_phy *pi = (struct brcms_phy *) pih;
  528. return pi->pubpi.coreflags;
  529. }
  530. void wlc_phy_anacore(struct brcms_phy_pub *pih, bool on)
  531. {
  532. struct brcms_phy *pi = (struct brcms_phy *) pih;
  533. if (ISNPHY(pi)) {
  534. if (on) {
  535. if (NREV_GE(pi->pubpi.phy_rev, 3)) {
  536. write_phy_reg(pi, 0xa6, 0x0d);
  537. write_phy_reg(pi, 0x8f, 0x0);
  538. write_phy_reg(pi, 0xa7, 0x0d);
  539. write_phy_reg(pi, 0xa5, 0x0);
  540. } else {
  541. write_phy_reg(pi, 0xa5, 0x0);
  542. }
  543. } else {
  544. if (NREV_GE(pi->pubpi.phy_rev, 3)) {
  545. write_phy_reg(pi, 0x8f, 0x07ff);
  546. write_phy_reg(pi, 0xa6, 0x0fd);
  547. write_phy_reg(pi, 0xa5, 0x07ff);
  548. write_phy_reg(pi, 0xa7, 0x0fd);
  549. } else {
  550. write_phy_reg(pi, 0xa5, 0x7fff);
  551. }
  552. }
  553. } else if (ISLCNPHY(pi)) {
  554. if (on) {
  555. and_phy_reg(pi, 0x43b,
  556. ~((0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  557. } else {
  558. or_phy_reg(pi, 0x43c,
  559. (0x1 << 0) | (0x1 << 1) | (0x1 << 2));
  560. or_phy_reg(pi, 0x43b,
  561. (0x1 << 0) | (0x1 << 1) | (0x1 << 2));
  562. }
  563. }
  564. }
  565. u32 wlc_phy_clk_bwbits(struct brcms_phy_pub *pih)
  566. {
  567. struct brcms_phy *pi = (struct brcms_phy *) pih;
  568. u32 phy_bw_clkbits = 0;
  569. if (pi && (ISNPHY(pi) || ISLCNPHY(pi))) {
  570. switch (pi->bw) {
  571. case WL_CHANSPEC_BW_10:
  572. phy_bw_clkbits = SICF_BW10;
  573. break;
  574. case WL_CHANSPEC_BW_20:
  575. phy_bw_clkbits = SICF_BW20;
  576. break;
  577. case WL_CHANSPEC_BW_40:
  578. phy_bw_clkbits = SICF_BW40;
  579. break;
  580. default:
  581. break;
  582. }
  583. }
  584. return phy_bw_clkbits;
  585. }
  586. void wlc_phy_por_inform(struct brcms_phy_pub *ppi)
  587. {
  588. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  589. pi->phy_init_por = true;
  590. }
  591. void wlc_phy_edcrs_lock(struct brcms_phy_pub *pih, bool lock)
  592. {
  593. struct brcms_phy *pi = (struct brcms_phy *) pih;
  594. pi->edcrs_threshold_lock = lock;
  595. write_phy_reg(pi, 0x22c, 0x46b);
  596. write_phy_reg(pi, 0x22d, 0x46b);
  597. write_phy_reg(pi, 0x22e, 0x3c0);
  598. write_phy_reg(pi, 0x22f, 0x3c0);
  599. }
  600. void wlc_phy_initcal_enable(struct brcms_phy_pub *pih, bool initcal)
  601. {
  602. struct brcms_phy *pi = (struct brcms_phy *) pih;
  603. pi->do_initcal = initcal;
  604. }
  605. void wlc_phy_hw_clk_state_upd(struct brcms_phy_pub *pih, bool newstate)
  606. {
  607. struct brcms_phy *pi = (struct brcms_phy *) pih;
  608. if (!pi || !pi->sh)
  609. return;
  610. pi->sh->clk = newstate;
  611. }
  612. void wlc_phy_hw_state_upd(struct brcms_phy_pub *pih, bool newstate)
  613. {
  614. struct brcms_phy *pi = (struct brcms_phy *) pih;
  615. if (!pi || !pi->sh)
  616. return;
  617. pi->sh->up = newstate;
  618. }
  619. void wlc_phy_init(struct brcms_phy_pub *pih, u16 chanspec)
  620. {
  621. u32 mc;
  622. void (*phy_init)(struct brcms_phy *) = NULL;
  623. struct brcms_phy *pi = (struct brcms_phy *) pih;
  624. if (pi->init_in_progress)
  625. return;
  626. pi->init_in_progress = true;
  627. pi->radio_chanspec = chanspec;
  628. mc = R_REG(&pi->regs->maccontrol);
  629. if (WARN(mc & MCTL_EN_MAC, "HW error MAC running on init"))
  630. return;
  631. if (!(pi->measure_hold & PHY_HOLD_FOR_SCAN))
  632. pi->measure_hold |= PHY_HOLD_FOR_NOT_ASSOC;
  633. if (WARN(!(ai_core_sflags(pi->sh->sih, 0, 0) & SISF_FCLKA),
  634. "HW error SISF_FCLKA\n"))
  635. return;
  636. phy_init = pi->pi_fptr.init;
  637. if (phy_init == NULL)
  638. return;
  639. wlc_phy_anacore(pih, ON);
  640. if (CHSPEC_BW(pi->radio_chanspec) != pi->bw)
  641. wlapi_bmac_bw_set(pi->sh->physhim,
  642. CHSPEC_BW(pi->radio_chanspec));
  643. pi->nphy_gain_boost = true;
  644. wlc_phy_switch_radio((struct brcms_phy_pub *) pi, ON);
  645. (*phy_init)(pi);
  646. pi->phy_init_por = false;
  647. if (D11REV_IS(pi->sh->corerev, 11) || D11REV_IS(pi->sh->corerev, 12))
  648. wlc_phy_do_dummy_tx(pi, true, OFF);
  649. if (!(ISNPHY(pi)))
  650. wlc_phy_txpower_update_shm(pi);
  651. wlc_phy_ant_rxdiv_set((struct brcms_phy_pub *) pi, pi->sh->rx_antdiv);
  652. pi->init_in_progress = false;
  653. }
  654. void wlc_phy_cal_init(struct brcms_phy_pub *pih)
  655. {
  656. struct brcms_phy *pi = (struct brcms_phy *) pih;
  657. void (*cal_init)(struct brcms_phy *) = NULL;
  658. if (WARN((R_REG(&pi->regs->maccontrol) & MCTL_EN_MAC) != 0,
  659. "HW error: MAC enabled during phy cal\n"))
  660. return;
  661. if (!pi->initialized) {
  662. cal_init = pi->pi_fptr.calinit;
  663. if (cal_init)
  664. (*cal_init)(pi);
  665. pi->initialized = true;
  666. }
  667. }
  668. int wlc_phy_down(struct brcms_phy_pub *pih)
  669. {
  670. struct brcms_phy *pi = (struct brcms_phy *) pih;
  671. int callbacks = 0;
  672. if (pi->phycal_timer
  673. && !wlapi_del_timer(pi->phycal_timer))
  674. callbacks++;
  675. pi->nphy_iqcal_chanspec_2G = 0;
  676. pi->nphy_iqcal_chanspec_5G = 0;
  677. return callbacks;
  678. }
  679. void
  680. wlc_phy_table_addr(struct brcms_phy *pi, uint tbl_id, uint tbl_offset,
  681. u16 tblAddr, u16 tblDataHi, u16 tblDataLo)
  682. {
  683. write_phy_reg(pi, tblAddr, (tbl_id << 10) | tbl_offset);
  684. pi->tbl_data_hi = tblDataHi;
  685. pi->tbl_data_lo = tblDataLo;
  686. if (pi->sh->chip == BCM43224_CHIP_ID &&
  687. pi->sh->chiprev == 1) {
  688. pi->tbl_addr = tblAddr;
  689. pi->tbl_save_id = tbl_id;
  690. pi->tbl_save_offset = tbl_offset;
  691. }
  692. }
  693. void wlc_phy_table_data_write(struct brcms_phy *pi, uint width, u32 val)
  694. {
  695. if ((pi->sh->chip == BCM43224_CHIP_ID) &&
  696. (pi->sh->chiprev == 1) &&
  697. (pi->tbl_save_id == NPHY_TBL_ID_ANTSWCTRLLUT)) {
  698. read_phy_reg(pi, pi->tbl_data_lo);
  699. write_phy_reg(pi, pi->tbl_addr,
  700. (pi->tbl_save_id << 10) | pi->tbl_save_offset);
  701. pi->tbl_save_offset++;
  702. }
  703. if (width == 32) {
  704. write_phy_reg(pi, pi->tbl_data_hi, (u16) (val >> 16));
  705. write_phy_reg(pi, pi->tbl_data_lo, (u16) val);
  706. } else {
  707. write_phy_reg(pi, pi->tbl_data_lo, (u16) val);
  708. }
  709. }
  710. void
  711. wlc_phy_write_table(struct brcms_phy *pi, const struct phytbl_info *ptbl_info,
  712. u16 tblAddr, u16 tblDataHi, u16 tblDataLo)
  713. {
  714. uint idx;
  715. uint tbl_id = ptbl_info->tbl_id;
  716. uint tbl_offset = ptbl_info->tbl_offset;
  717. uint tbl_width = ptbl_info->tbl_width;
  718. const u8 *ptbl_8b = (const u8 *)ptbl_info->tbl_ptr;
  719. const u16 *ptbl_16b = (const u16 *)ptbl_info->tbl_ptr;
  720. const u32 *ptbl_32b = (const u32 *)ptbl_info->tbl_ptr;
  721. write_phy_reg(pi, tblAddr, (tbl_id << 10) | tbl_offset);
  722. for (idx = 0; idx < ptbl_info->tbl_len; idx++) {
  723. if ((pi->sh->chip == BCM43224_CHIP_ID) &&
  724. (pi->sh->chiprev == 1) &&
  725. (tbl_id == NPHY_TBL_ID_ANTSWCTRLLUT)) {
  726. read_phy_reg(pi, tblDataLo);
  727. write_phy_reg(pi, tblAddr,
  728. (tbl_id << 10) | (tbl_offset + idx));
  729. }
  730. if (tbl_width == 32) {
  731. write_phy_reg(pi, tblDataHi,
  732. (u16) (ptbl_32b[idx] >> 16));
  733. write_phy_reg(pi, tblDataLo, (u16) ptbl_32b[idx]);
  734. } else if (tbl_width == 16) {
  735. write_phy_reg(pi, tblDataLo, ptbl_16b[idx]);
  736. } else {
  737. write_phy_reg(pi, tblDataLo, ptbl_8b[idx]);
  738. }
  739. }
  740. }
  741. void
  742. wlc_phy_read_table(struct brcms_phy *pi, const struct phytbl_info *ptbl_info,
  743. u16 tblAddr, u16 tblDataHi, u16 tblDataLo)
  744. {
  745. uint idx;
  746. uint tbl_id = ptbl_info->tbl_id;
  747. uint tbl_offset = ptbl_info->tbl_offset;
  748. uint tbl_width = ptbl_info->tbl_width;
  749. u8 *ptbl_8b = (u8 *)ptbl_info->tbl_ptr;
  750. u16 *ptbl_16b = (u16 *)ptbl_info->tbl_ptr;
  751. u32 *ptbl_32b = (u32 *)ptbl_info->tbl_ptr;
  752. write_phy_reg(pi, tblAddr, (tbl_id << 10) | tbl_offset);
  753. for (idx = 0; idx < ptbl_info->tbl_len; idx++) {
  754. if ((pi->sh->chip == BCM43224_CHIP_ID) &&
  755. (pi->sh->chiprev == 1)) {
  756. (void)read_phy_reg(pi, tblDataLo);
  757. write_phy_reg(pi, tblAddr,
  758. (tbl_id << 10) | (tbl_offset + idx));
  759. }
  760. if (tbl_width == 32) {
  761. ptbl_32b[idx] = read_phy_reg(pi, tblDataLo);
  762. ptbl_32b[idx] |= (read_phy_reg(pi, tblDataHi) << 16);
  763. } else if (tbl_width == 16) {
  764. ptbl_16b[idx] = read_phy_reg(pi, tblDataLo);
  765. } else {
  766. ptbl_8b[idx] = (u8) read_phy_reg(pi, tblDataLo);
  767. }
  768. }
  769. }
  770. uint
  771. wlc_phy_init_radio_regs_allbands(struct brcms_phy *pi,
  772. struct radio_20xx_regs *radioregs)
  773. {
  774. uint i = 0;
  775. do {
  776. if (radioregs[i].do_init)
  777. write_radio_reg(pi, radioregs[i].address,
  778. (u16) radioregs[i].init);
  779. i++;
  780. } while (radioregs[i].address != 0xffff);
  781. return i;
  782. }
  783. uint
  784. wlc_phy_init_radio_regs(struct brcms_phy *pi,
  785. const struct radio_regs *radioregs,
  786. u16 core_offset)
  787. {
  788. uint i = 0;
  789. uint count = 0;
  790. do {
  791. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  792. if (radioregs[i].do_init_a) {
  793. write_radio_reg(pi,
  794. radioregs[i].
  795. address | core_offset,
  796. (u16) radioregs[i].init_a);
  797. if (ISNPHY(pi) && (++count % 4 == 0))
  798. BRCMS_PHY_WAR_PR51571(pi);
  799. }
  800. } else {
  801. if (radioregs[i].do_init_g) {
  802. write_radio_reg(pi,
  803. radioregs[i].
  804. address | core_offset,
  805. (u16) radioregs[i].init_g);
  806. if (ISNPHY(pi) && (++count % 4 == 0))
  807. BRCMS_PHY_WAR_PR51571(pi);
  808. }
  809. }
  810. i++;
  811. } while (radioregs[i].address != 0xffff);
  812. return i;
  813. }
  814. void wlc_phy_do_dummy_tx(struct brcms_phy *pi, bool ofdm, bool pa_on)
  815. {
  816. #define DUMMY_PKT_LEN 20
  817. struct d11regs __iomem *regs = pi->regs;
  818. int i, count;
  819. u8 ofdmpkt[DUMMY_PKT_LEN] = {
  820. 0xcc, 0x01, 0x02, 0x00, 0x00, 0x00, 0xd4, 0x00, 0x00, 0x00,
  821. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
  822. };
  823. u8 cckpkt[DUMMY_PKT_LEN] = {
  824. 0x6e, 0x84, 0x0b, 0x00, 0x00, 0x00, 0xd4, 0x00, 0x00, 0x00,
  825. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
  826. };
  827. u32 *dummypkt;
  828. dummypkt = (u32 *) (ofdm ? ofdmpkt : cckpkt);
  829. wlapi_bmac_write_template_ram(pi->sh->physhim, 0, DUMMY_PKT_LEN,
  830. dummypkt);
  831. W_REG(&regs->xmtsel, 0);
  832. if (D11REV_GE(pi->sh->corerev, 11))
  833. W_REG(&regs->wepctl, 0x100);
  834. else
  835. W_REG(&regs->wepctl, 0);
  836. W_REG(&regs->txe_phyctl, (ofdm ? 1 : 0) | PHY_TXC_ANT_0);
  837. if (ISNPHY(pi) || ISLCNPHY(pi))
  838. W_REG(&regs->txe_phyctl1, 0x1A02);
  839. W_REG(&regs->txe_wm_0, 0);
  840. W_REG(&regs->txe_wm_1, 0);
  841. W_REG(&regs->xmttplatetxptr, 0);
  842. W_REG(&regs->xmttxcnt, DUMMY_PKT_LEN);
  843. W_REG(&regs->xmtsel, ((8 << 8) | (1 << 5) | (1 << 2) | 2));
  844. W_REG(&regs->txe_ctl, 0);
  845. if (!pa_on) {
  846. if (ISNPHY(pi))
  847. wlc_phy_pa_override_nphy(pi, OFF);
  848. }
  849. if (ISNPHY(pi) || ISLCNPHY(pi))
  850. W_REG(&regs->txe_aux, 0xD0);
  851. else
  852. W_REG(&regs->txe_aux, ((1 << 5) | (1 << 4)));
  853. (void)R_REG(&regs->txe_aux);
  854. i = 0;
  855. count = ofdm ? 30 : 250;
  856. while ((i++ < count)
  857. && (R_REG(&regs->txe_status) & (1 << 7)))
  858. udelay(10);
  859. i = 0;
  860. while ((i++ < 10)
  861. && ((R_REG(&regs->txe_status) & (1 << 10)) == 0))
  862. udelay(10);
  863. i = 0;
  864. while ((i++ < 10) && ((R_REG(&regs->ifsstat) & (1 << 8))))
  865. udelay(10);
  866. if (!pa_on) {
  867. if (ISNPHY(pi))
  868. wlc_phy_pa_override_nphy(pi, ON);
  869. }
  870. }
  871. void wlc_phy_hold_upd(struct brcms_phy_pub *pih, u32 id, bool set)
  872. {
  873. struct brcms_phy *pi = (struct brcms_phy *) pih;
  874. if (set)
  875. mboolset(pi->measure_hold, id);
  876. else
  877. mboolclr(pi->measure_hold, id);
  878. return;
  879. }
  880. void wlc_phy_mute_upd(struct brcms_phy_pub *pih, bool mute, u32 flags)
  881. {
  882. struct brcms_phy *pi = (struct brcms_phy *) pih;
  883. if (mute)
  884. mboolset(pi->measure_hold, PHY_HOLD_FOR_MUTE);
  885. else
  886. mboolclr(pi->measure_hold, PHY_HOLD_FOR_MUTE);
  887. if (!mute && (flags & PHY_MUTE_FOR_PREISM))
  888. pi->nphy_perical_last = pi->sh->now - pi->sh->glacial_timer;
  889. return;
  890. }
  891. void wlc_phy_clear_tssi(struct brcms_phy_pub *pih)
  892. {
  893. struct brcms_phy *pi = (struct brcms_phy *) pih;
  894. if (ISNPHY(pi)) {
  895. return;
  896. } else {
  897. wlapi_bmac_write_shm(pi->sh->physhim, M_B_TSSI_0, NULL_TSSI_W);
  898. wlapi_bmac_write_shm(pi->sh->physhim, M_B_TSSI_1, NULL_TSSI_W);
  899. wlapi_bmac_write_shm(pi->sh->physhim, M_G_TSSI_0, NULL_TSSI_W);
  900. wlapi_bmac_write_shm(pi->sh->physhim, M_G_TSSI_1, NULL_TSSI_W);
  901. }
  902. }
  903. static bool wlc_phy_cal_txpower_recalc_sw(struct brcms_phy *pi)
  904. {
  905. return false;
  906. }
  907. void wlc_phy_switch_radio(struct brcms_phy_pub *pih, bool on)
  908. {
  909. struct brcms_phy *pi = (struct brcms_phy *) pih;
  910. (void)R_REG(&pi->regs->maccontrol);
  911. if (ISNPHY(pi)) {
  912. wlc_phy_switch_radio_nphy(pi, on);
  913. } else if (ISLCNPHY(pi)) {
  914. if (on) {
  915. and_phy_reg(pi, 0x44c,
  916. ~((0x1 << 8) |
  917. (0x1 << 9) |
  918. (0x1 << 10) | (0x1 << 11) | (0x1 << 12)));
  919. and_phy_reg(pi, 0x4b0, ~((0x1 << 3) | (0x1 << 11)));
  920. and_phy_reg(pi, 0x4f9, ~(0x1 << 3));
  921. } else {
  922. and_phy_reg(pi, 0x44d,
  923. ~((0x1 << 10) |
  924. (0x1 << 11) |
  925. (0x1 << 12) | (0x1 << 13) | (0x1 << 14)));
  926. or_phy_reg(pi, 0x44c,
  927. (0x1 << 8) |
  928. (0x1 << 9) |
  929. (0x1 << 10) | (0x1 << 11) | (0x1 << 12));
  930. and_phy_reg(pi, 0x4b7, ~((0x7f << 8)));
  931. and_phy_reg(pi, 0x4b1, ~((0x1 << 13)));
  932. or_phy_reg(pi, 0x4b0, (0x1 << 3) | (0x1 << 11));
  933. and_phy_reg(pi, 0x4fa, ~((0x1 << 3)));
  934. or_phy_reg(pi, 0x4f9, (0x1 << 3));
  935. }
  936. }
  937. }
  938. u16 wlc_phy_bw_state_get(struct brcms_phy_pub *ppi)
  939. {
  940. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  941. return pi->bw;
  942. }
  943. void wlc_phy_bw_state_set(struct brcms_phy_pub *ppi, u16 bw)
  944. {
  945. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  946. pi->bw = bw;
  947. }
  948. void wlc_phy_chanspec_radio_set(struct brcms_phy_pub *ppi, u16 newch)
  949. {
  950. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  951. pi->radio_chanspec = newch;
  952. }
  953. u16 wlc_phy_chanspec_get(struct brcms_phy_pub *ppi)
  954. {
  955. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  956. return pi->radio_chanspec;
  957. }
  958. void wlc_phy_chanspec_set(struct brcms_phy_pub *ppi, u16 chanspec)
  959. {
  960. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  961. u16 m_cur_channel;
  962. void (*chanspec_set)(struct brcms_phy *, u16) = NULL;
  963. m_cur_channel = CHSPEC_CHANNEL(chanspec);
  964. if (CHSPEC_IS5G(chanspec))
  965. m_cur_channel |= D11_CURCHANNEL_5G;
  966. if (CHSPEC_IS40(chanspec))
  967. m_cur_channel |= D11_CURCHANNEL_40;
  968. wlapi_bmac_write_shm(pi->sh->physhim, M_CURCHANNEL, m_cur_channel);
  969. chanspec_set = pi->pi_fptr.chanset;
  970. if (chanspec_set)
  971. (*chanspec_set)(pi, chanspec);
  972. }
  973. int wlc_phy_chanspec_freq2bandrange_lpssn(uint freq)
  974. {
  975. int range = -1;
  976. if (freq < 2500)
  977. range = WL_CHAN_FREQ_RANGE_2G;
  978. else if (freq <= 5320)
  979. range = WL_CHAN_FREQ_RANGE_5GL;
  980. else if (freq <= 5700)
  981. range = WL_CHAN_FREQ_RANGE_5GM;
  982. else
  983. range = WL_CHAN_FREQ_RANGE_5GH;
  984. return range;
  985. }
  986. int wlc_phy_chanspec_bandrange_get(struct brcms_phy *pi, u16 chanspec)
  987. {
  988. int range = -1;
  989. uint channel = CHSPEC_CHANNEL(chanspec);
  990. uint freq = wlc_phy_channel2freq(channel);
  991. if (ISNPHY(pi))
  992. range = wlc_phy_get_chan_freq_range_nphy(pi, channel);
  993. else if (ISLCNPHY(pi))
  994. range = wlc_phy_chanspec_freq2bandrange_lpssn(freq);
  995. return range;
  996. }
  997. void wlc_phy_chanspec_ch14_widefilter_set(struct brcms_phy_pub *ppi,
  998. bool wide_filter)
  999. {
  1000. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1001. pi->channel_14_wide_filter = wide_filter;
  1002. }
  1003. int wlc_phy_channel2freq(uint channel)
  1004. {
  1005. uint i;
  1006. for (i = 0; i < ARRAY_SIZE(chan_info_all); i++)
  1007. if (chan_info_all[i].chan == channel)
  1008. return chan_info_all[i].freq;
  1009. return 0;
  1010. }
  1011. void
  1012. wlc_phy_chanspec_band_validch(struct brcms_phy_pub *ppi, uint band,
  1013. struct brcms_chanvec *channels)
  1014. {
  1015. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1016. uint i;
  1017. uint channel;
  1018. memset(channels, 0, sizeof(struct brcms_chanvec));
  1019. for (i = 0; i < ARRAY_SIZE(chan_info_all); i++) {
  1020. channel = chan_info_all[i].chan;
  1021. if ((pi->a_band_high_disable) && (channel >= FIRST_REF5_CHANNUM)
  1022. && (channel <= LAST_REF5_CHANNUM))
  1023. continue;
  1024. if ((band == BRCM_BAND_2G && channel <= CH_MAX_2G_CHANNEL) ||
  1025. (band == BRCM_BAND_5G && channel > CH_MAX_2G_CHANNEL))
  1026. setbit(channels->vec, channel);
  1027. }
  1028. }
  1029. u16 wlc_phy_chanspec_band_firstch(struct brcms_phy_pub *ppi, uint band)
  1030. {
  1031. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1032. uint i;
  1033. uint channel;
  1034. u16 chspec;
  1035. for (i = 0; i < ARRAY_SIZE(chan_info_all); i++) {
  1036. channel = chan_info_all[i].chan;
  1037. if (ISNPHY(pi) && pi->bw == WL_CHANSPEC_BW_40) {
  1038. uint j;
  1039. for (j = 0; j < ARRAY_SIZE(chan_info_all); j++) {
  1040. if (chan_info_all[j].chan ==
  1041. channel + CH_10MHZ_APART)
  1042. break;
  1043. }
  1044. if (j == ARRAY_SIZE(chan_info_all))
  1045. continue;
  1046. channel = upper_20_sb(channel);
  1047. chspec = channel | WL_CHANSPEC_BW_40 |
  1048. WL_CHANSPEC_CTL_SB_LOWER;
  1049. if (band == BRCM_BAND_2G)
  1050. chspec |= WL_CHANSPEC_BAND_2G;
  1051. else
  1052. chspec |= WL_CHANSPEC_BAND_5G;
  1053. } else
  1054. chspec = ch20mhz_chspec(channel);
  1055. if ((pi->a_band_high_disable) && (channel >= FIRST_REF5_CHANNUM)
  1056. && (channel <= LAST_REF5_CHANNUM))
  1057. continue;
  1058. if ((band == BRCM_BAND_2G && channel <= CH_MAX_2G_CHANNEL) ||
  1059. (band == BRCM_BAND_5G && channel > CH_MAX_2G_CHANNEL))
  1060. return chspec;
  1061. }
  1062. return (u16) INVCHANSPEC;
  1063. }
  1064. int wlc_phy_txpower_get(struct brcms_phy_pub *ppi, uint *qdbm, bool *override)
  1065. {
  1066. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1067. *qdbm = pi->tx_user_target[0];
  1068. if (override != NULL)
  1069. *override = pi->txpwroverride;
  1070. return 0;
  1071. }
  1072. void wlc_phy_txpower_target_set(struct brcms_phy_pub *ppi,
  1073. struct txpwr_limits *txpwr)
  1074. {
  1075. bool mac_enabled = false;
  1076. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1077. memcpy(&pi->tx_user_target[TXP_FIRST_CCK],
  1078. &txpwr->cck[0], BRCMS_NUM_RATES_CCK);
  1079. memcpy(&pi->tx_user_target[TXP_FIRST_OFDM],
  1080. &txpwr->ofdm[0], BRCMS_NUM_RATES_OFDM);
  1081. memcpy(&pi->tx_user_target[TXP_FIRST_OFDM_20_CDD],
  1082. &txpwr->ofdm_cdd[0], BRCMS_NUM_RATES_OFDM);
  1083. memcpy(&pi->tx_user_target[TXP_FIRST_OFDM_40_SISO],
  1084. &txpwr->ofdm_40_siso[0], BRCMS_NUM_RATES_OFDM);
  1085. memcpy(&pi->tx_user_target[TXP_FIRST_OFDM_40_CDD],
  1086. &txpwr->ofdm_40_cdd[0], BRCMS_NUM_RATES_OFDM);
  1087. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_20_SISO],
  1088. &txpwr->mcs_20_siso[0], BRCMS_NUM_RATES_MCS_1_STREAM);
  1089. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_20_CDD],
  1090. &txpwr->mcs_20_cdd[0], BRCMS_NUM_RATES_MCS_1_STREAM);
  1091. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_20_STBC],
  1092. &txpwr->mcs_20_stbc[0], BRCMS_NUM_RATES_MCS_1_STREAM);
  1093. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_20_SDM],
  1094. &txpwr->mcs_20_mimo[0], BRCMS_NUM_RATES_MCS_2_STREAM);
  1095. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_40_SISO],
  1096. &txpwr->mcs_40_siso[0], BRCMS_NUM_RATES_MCS_1_STREAM);
  1097. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_40_CDD],
  1098. &txpwr->mcs_40_cdd[0], BRCMS_NUM_RATES_MCS_1_STREAM);
  1099. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_40_STBC],
  1100. &txpwr->mcs_40_stbc[0], BRCMS_NUM_RATES_MCS_1_STREAM);
  1101. memcpy(&pi->tx_user_target[TXP_FIRST_MCS_40_SDM],
  1102. &txpwr->mcs_40_mimo[0], BRCMS_NUM_RATES_MCS_2_STREAM);
  1103. if (R_REG(&pi->regs->maccontrol) & MCTL_EN_MAC)
  1104. mac_enabled = true;
  1105. if (mac_enabled)
  1106. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1107. wlc_phy_txpower_recalc_target(pi);
  1108. wlc_phy_cal_txpower_recalc_sw(pi);
  1109. if (mac_enabled)
  1110. wlapi_enable_mac(pi->sh->physhim);
  1111. }
  1112. int wlc_phy_txpower_set(struct brcms_phy_pub *ppi, uint qdbm, bool override)
  1113. {
  1114. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1115. int i;
  1116. if (qdbm > 127)
  1117. return -EINVAL;
  1118. for (i = 0; i < TXP_NUM_RATES; i++)
  1119. pi->tx_user_target[i] = (u8) qdbm;
  1120. pi->txpwroverride = false;
  1121. if (pi->sh->up) {
  1122. if (!SCAN_INPROG_PHY(pi)) {
  1123. bool suspend;
  1124. suspend = (0 == (R_REG(&pi->regs->maccontrol) &
  1125. MCTL_EN_MAC));
  1126. if (!suspend)
  1127. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1128. wlc_phy_txpower_recalc_target(pi);
  1129. wlc_phy_cal_txpower_recalc_sw(pi);
  1130. if (!suspend)
  1131. wlapi_enable_mac(pi->sh->physhim);
  1132. }
  1133. }
  1134. return 0;
  1135. }
  1136. void
  1137. wlc_phy_txpower_sromlimit(struct brcms_phy_pub *ppi, uint channel, u8 *min_pwr,
  1138. u8 *max_pwr, int txp_rate_idx)
  1139. {
  1140. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1141. uint i;
  1142. *min_pwr = pi->min_txpower * BRCMS_TXPWR_DB_FACTOR;
  1143. if (ISNPHY(pi)) {
  1144. if (txp_rate_idx < 0)
  1145. txp_rate_idx = TXP_FIRST_CCK;
  1146. wlc_phy_txpower_sromlimit_get_nphy(pi, channel, max_pwr,
  1147. (u8) txp_rate_idx);
  1148. } else if ((channel <= CH_MAX_2G_CHANNEL)) {
  1149. if (txp_rate_idx < 0)
  1150. txp_rate_idx = TXP_FIRST_CCK;
  1151. *max_pwr = pi->tx_srom_max_rate_2g[txp_rate_idx];
  1152. } else {
  1153. *max_pwr = BRCMS_TXPWR_MAX;
  1154. if (txp_rate_idx < 0)
  1155. txp_rate_idx = TXP_FIRST_OFDM;
  1156. for (i = 0; i < ARRAY_SIZE(chan_info_all); i++) {
  1157. if (channel == chan_info_all[i].chan)
  1158. break;
  1159. }
  1160. if (pi->hwtxpwr) {
  1161. *max_pwr = pi->hwtxpwr[i];
  1162. } else {
  1163. if ((i >= FIRST_MID_5G_CHAN) && (i <= LAST_MID_5G_CHAN))
  1164. *max_pwr =
  1165. pi->tx_srom_max_rate_5g_mid[txp_rate_idx];
  1166. if ((i >= FIRST_HIGH_5G_CHAN)
  1167. && (i <= LAST_HIGH_5G_CHAN))
  1168. *max_pwr =
  1169. pi->tx_srom_max_rate_5g_hi[txp_rate_idx];
  1170. if ((i >= FIRST_LOW_5G_CHAN) && (i <= LAST_LOW_5G_CHAN))
  1171. *max_pwr =
  1172. pi->tx_srom_max_rate_5g_low[txp_rate_idx];
  1173. }
  1174. }
  1175. }
  1176. void
  1177. wlc_phy_txpower_sromlimit_max_get(struct brcms_phy_pub *ppi, uint chan,
  1178. u8 *max_txpwr, u8 *min_txpwr)
  1179. {
  1180. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1181. u8 tx_pwr_max = 0;
  1182. u8 tx_pwr_min = 255;
  1183. u8 max_num_rate;
  1184. u8 maxtxpwr, mintxpwr, rate, pactrl;
  1185. pactrl = 0;
  1186. max_num_rate = ISNPHY(pi) ? TXP_NUM_RATES :
  1187. ISLCNPHY(pi) ? (TXP_LAST_SISO_MCS_20 +
  1188. 1) : (TXP_LAST_OFDM + 1);
  1189. for (rate = 0; rate < max_num_rate; rate++) {
  1190. wlc_phy_txpower_sromlimit(ppi, chan, &mintxpwr, &maxtxpwr,
  1191. rate);
  1192. maxtxpwr = (maxtxpwr > pactrl) ? (maxtxpwr - pactrl) : 0;
  1193. maxtxpwr = (maxtxpwr > 6) ? (maxtxpwr - 6) : 0;
  1194. tx_pwr_max = max(tx_pwr_max, maxtxpwr);
  1195. tx_pwr_min = min(tx_pwr_min, maxtxpwr);
  1196. }
  1197. *max_txpwr = tx_pwr_max;
  1198. *min_txpwr = tx_pwr_min;
  1199. }
  1200. void
  1201. wlc_phy_txpower_boardlimit_band(struct brcms_phy_pub *ppi, uint bandunit,
  1202. s32 *max_pwr, s32 *min_pwr, u32 *step_pwr)
  1203. {
  1204. return;
  1205. }
  1206. u8 wlc_phy_txpower_get_target_min(struct brcms_phy_pub *ppi)
  1207. {
  1208. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1209. return pi->tx_power_min;
  1210. }
  1211. u8 wlc_phy_txpower_get_target_max(struct brcms_phy_pub *ppi)
  1212. {
  1213. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1214. return pi->tx_power_max;
  1215. }
  1216. static s8 wlc_phy_env_measure_vbat(struct brcms_phy *pi)
  1217. {
  1218. if (ISLCNPHY(pi))
  1219. return wlc_lcnphy_vbatsense(pi, 0);
  1220. else
  1221. return 0;
  1222. }
  1223. static s8 wlc_phy_env_measure_temperature(struct brcms_phy *pi)
  1224. {
  1225. if (ISLCNPHY(pi))
  1226. return wlc_lcnphy_tempsense_degree(pi, 0);
  1227. else
  1228. return 0;
  1229. }
  1230. static void wlc_phy_upd_env_txpwr_rate_limits(struct brcms_phy *pi, u32 band)
  1231. {
  1232. u8 i;
  1233. s8 temp, vbat;
  1234. for (i = 0; i < TXP_NUM_RATES; i++)
  1235. pi->txpwr_env_limit[i] = BRCMS_TXPWR_MAX;
  1236. vbat = wlc_phy_env_measure_vbat(pi);
  1237. temp = wlc_phy_env_measure_temperature(pi);
  1238. }
  1239. static s8
  1240. wlc_user_txpwr_antport_to_rfport(struct brcms_phy *pi, uint chan, u32 band,
  1241. u8 rate)
  1242. {
  1243. s8 offset = 0;
  1244. if (!pi->user_txpwr_at_rfport)
  1245. return offset;
  1246. return offset;
  1247. }
  1248. void wlc_phy_txpower_recalc_target(struct brcms_phy *pi)
  1249. {
  1250. u8 maxtxpwr, mintxpwr, rate, pactrl;
  1251. uint target_chan;
  1252. u8 tx_pwr_target[TXP_NUM_RATES];
  1253. u8 tx_pwr_max = 0;
  1254. u8 tx_pwr_min = 255;
  1255. u8 tx_pwr_max_rate_ind = 0;
  1256. u8 max_num_rate;
  1257. u8 start_rate = 0;
  1258. u16 chspec;
  1259. u32 band = CHSPEC2BAND(pi->radio_chanspec);
  1260. void (*txpwr_recalc_fn)(struct brcms_phy *) = NULL;
  1261. chspec = pi->radio_chanspec;
  1262. if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE)
  1263. target_chan = CHSPEC_CHANNEL(chspec);
  1264. else if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER)
  1265. target_chan = upper_20_sb(CHSPEC_CHANNEL(chspec));
  1266. else
  1267. target_chan = lower_20_sb(CHSPEC_CHANNEL(chspec));
  1268. pactrl = 0;
  1269. if (ISLCNPHY(pi)) {
  1270. u32 offset_mcs, i;
  1271. if (CHSPEC_IS40(pi->radio_chanspec)) {
  1272. offset_mcs = pi->mcs40_po;
  1273. for (i = TXP_FIRST_SISO_MCS_20;
  1274. i <= TXP_LAST_SISO_MCS_20; i++) {
  1275. pi->tx_srom_max_rate_2g[i - 8] =
  1276. pi->tx_srom_max_2g -
  1277. ((offset_mcs & 0xf) * 2);
  1278. offset_mcs >>= 4;
  1279. }
  1280. } else {
  1281. offset_mcs = pi->mcs20_po;
  1282. for (i = TXP_FIRST_SISO_MCS_20;
  1283. i <= TXP_LAST_SISO_MCS_20; i++) {
  1284. pi->tx_srom_max_rate_2g[i - 8] =
  1285. pi->tx_srom_max_2g -
  1286. ((offset_mcs & 0xf) * 2);
  1287. offset_mcs >>= 4;
  1288. }
  1289. }
  1290. }
  1291. max_num_rate = ((ISNPHY(pi)) ? (TXP_NUM_RATES) :
  1292. ((ISLCNPHY(pi)) ?
  1293. (TXP_LAST_SISO_MCS_20 + 1) : (TXP_LAST_OFDM + 1)));
  1294. wlc_phy_upd_env_txpwr_rate_limits(pi, band);
  1295. for (rate = start_rate; rate < max_num_rate; rate++) {
  1296. tx_pwr_target[rate] = pi->tx_user_target[rate];
  1297. if (pi->user_txpwr_at_rfport)
  1298. tx_pwr_target[rate] +=
  1299. wlc_user_txpwr_antport_to_rfport(pi,
  1300. target_chan,
  1301. band,
  1302. rate);
  1303. wlc_phy_txpower_sromlimit((struct brcms_phy_pub *) pi,
  1304. target_chan,
  1305. &mintxpwr, &maxtxpwr, rate);
  1306. maxtxpwr = min(maxtxpwr, pi->txpwr_limit[rate]);
  1307. maxtxpwr = (maxtxpwr > pactrl) ? (maxtxpwr - pactrl) : 0;
  1308. maxtxpwr = (maxtxpwr > 6) ? (maxtxpwr - 6) : 0;
  1309. maxtxpwr = min(maxtxpwr, tx_pwr_target[rate]);
  1310. if (pi->txpwr_percent <= 100)
  1311. maxtxpwr = (maxtxpwr * pi->txpwr_percent) / 100;
  1312. tx_pwr_target[rate] = max(maxtxpwr, mintxpwr);
  1313. tx_pwr_target[rate] =
  1314. min(tx_pwr_target[rate], pi->txpwr_env_limit[rate]);
  1315. if (tx_pwr_target[rate] > tx_pwr_max)
  1316. tx_pwr_max_rate_ind = rate;
  1317. tx_pwr_max = max(tx_pwr_max, tx_pwr_target[rate]);
  1318. tx_pwr_min = min(tx_pwr_min, tx_pwr_target[rate]);
  1319. }
  1320. memset(pi->tx_power_offset, 0, sizeof(pi->tx_power_offset));
  1321. pi->tx_power_max = tx_pwr_max;
  1322. pi->tx_power_min = tx_pwr_min;
  1323. pi->tx_power_max_rate_ind = tx_pwr_max_rate_ind;
  1324. for (rate = 0; rate < max_num_rate; rate++) {
  1325. pi->tx_power_target[rate] = tx_pwr_target[rate];
  1326. if (!pi->hwpwrctrl || ISNPHY(pi))
  1327. pi->tx_power_offset[rate] =
  1328. pi->tx_power_max - pi->tx_power_target[rate];
  1329. else
  1330. pi->tx_power_offset[rate] =
  1331. pi->tx_power_target[rate] - pi->tx_power_min;
  1332. }
  1333. txpwr_recalc_fn = pi->pi_fptr.txpwrrecalc;
  1334. if (txpwr_recalc_fn)
  1335. (*txpwr_recalc_fn)(pi);
  1336. }
  1337. static void
  1338. wlc_phy_txpower_reg_limit_calc(struct brcms_phy *pi, struct txpwr_limits *txpwr,
  1339. u16 chanspec)
  1340. {
  1341. u8 tmp_txpwr_limit[2 * BRCMS_NUM_RATES_OFDM];
  1342. u8 *txpwr_ptr1 = NULL, *txpwr_ptr2 = NULL;
  1343. int rate_start_index = 0, rate1, rate2, k;
  1344. for (rate1 = WL_TX_POWER_CCK_FIRST, rate2 = 0;
  1345. rate2 < WL_TX_POWER_CCK_NUM; rate1++, rate2++)
  1346. pi->txpwr_limit[rate1] = txpwr->cck[rate2];
  1347. for (rate1 = WL_TX_POWER_OFDM_FIRST, rate2 = 0;
  1348. rate2 < WL_TX_POWER_OFDM_NUM; rate1++, rate2++)
  1349. pi->txpwr_limit[rate1] = txpwr->ofdm[rate2];
  1350. if (ISNPHY(pi)) {
  1351. for (k = 0; k < 4; k++) {
  1352. switch (k) {
  1353. case 0:
  1354. txpwr_ptr1 = txpwr->mcs_20_siso;
  1355. txpwr_ptr2 = txpwr->ofdm;
  1356. rate_start_index = WL_TX_POWER_OFDM_FIRST;
  1357. break;
  1358. case 1:
  1359. txpwr_ptr1 = txpwr->mcs_20_cdd;
  1360. txpwr_ptr2 = txpwr->ofdm_cdd;
  1361. rate_start_index = WL_TX_POWER_OFDM20_CDD_FIRST;
  1362. break;
  1363. case 2:
  1364. txpwr_ptr1 = txpwr->mcs_40_siso;
  1365. txpwr_ptr2 = txpwr->ofdm_40_siso;
  1366. rate_start_index =
  1367. WL_TX_POWER_OFDM40_SISO_FIRST;
  1368. break;
  1369. case 3:
  1370. txpwr_ptr1 = txpwr->mcs_40_cdd;
  1371. txpwr_ptr2 = txpwr->ofdm_40_cdd;
  1372. rate_start_index = WL_TX_POWER_OFDM40_CDD_FIRST;
  1373. break;
  1374. }
  1375. for (rate2 = 0; rate2 < BRCMS_NUM_RATES_OFDM;
  1376. rate2++) {
  1377. tmp_txpwr_limit[rate2] = 0;
  1378. tmp_txpwr_limit[BRCMS_NUM_RATES_OFDM + rate2] =
  1379. txpwr_ptr1[rate2];
  1380. }
  1381. wlc_phy_mcs_to_ofdm_powers_nphy(
  1382. tmp_txpwr_limit, 0,
  1383. BRCMS_NUM_RATES_OFDM -
  1384. 1, BRCMS_NUM_RATES_OFDM);
  1385. for (rate1 = rate_start_index, rate2 = 0;
  1386. rate2 < BRCMS_NUM_RATES_OFDM; rate1++, rate2++)
  1387. pi->txpwr_limit[rate1] =
  1388. min(txpwr_ptr2[rate2],
  1389. tmp_txpwr_limit[rate2]);
  1390. }
  1391. for (k = 0; k < 4; k++) {
  1392. switch (k) {
  1393. case 0:
  1394. txpwr_ptr1 = txpwr->ofdm;
  1395. txpwr_ptr2 = txpwr->mcs_20_siso;
  1396. rate_start_index = WL_TX_POWER_MCS20_SISO_FIRST;
  1397. break;
  1398. case 1:
  1399. txpwr_ptr1 = txpwr->ofdm_cdd;
  1400. txpwr_ptr2 = txpwr->mcs_20_cdd;
  1401. rate_start_index = WL_TX_POWER_MCS20_CDD_FIRST;
  1402. break;
  1403. case 2:
  1404. txpwr_ptr1 = txpwr->ofdm_40_siso;
  1405. txpwr_ptr2 = txpwr->mcs_40_siso;
  1406. rate_start_index = WL_TX_POWER_MCS40_SISO_FIRST;
  1407. break;
  1408. case 3:
  1409. txpwr_ptr1 = txpwr->ofdm_40_cdd;
  1410. txpwr_ptr2 = txpwr->mcs_40_cdd;
  1411. rate_start_index = WL_TX_POWER_MCS40_CDD_FIRST;
  1412. break;
  1413. }
  1414. for (rate2 = 0; rate2 < BRCMS_NUM_RATES_OFDM;
  1415. rate2++) {
  1416. tmp_txpwr_limit[rate2] = 0;
  1417. tmp_txpwr_limit[BRCMS_NUM_RATES_OFDM + rate2] =
  1418. txpwr_ptr1[rate2];
  1419. }
  1420. wlc_phy_ofdm_to_mcs_powers_nphy(
  1421. tmp_txpwr_limit, 0,
  1422. BRCMS_NUM_RATES_OFDM -
  1423. 1, BRCMS_NUM_RATES_OFDM);
  1424. for (rate1 = rate_start_index, rate2 = 0;
  1425. rate2 < BRCMS_NUM_RATES_MCS_1_STREAM;
  1426. rate1++, rate2++)
  1427. pi->txpwr_limit[rate1] =
  1428. min(txpwr_ptr2[rate2],
  1429. tmp_txpwr_limit[rate2]);
  1430. }
  1431. for (k = 0; k < 2; k++) {
  1432. switch (k) {
  1433. case 0:
  1434. rate_start_index = WL_TX_POWER_MCS20_STBC_FIRST;
  1435. txpwr_ptr1 = txpwr->mcs_20_stbc;
  1436. break;
  1437. case 1:
  1438. rate_start_index = WL_TX_POWER_MCS40_STBC_FIRST;
  1439. txpwr_ptr1 = txpwr->mcs_40_stbc;
  1440. break;
  1441. }
  1442. for (rate1 = rate_start_index, rate2 = 0;
  1443. rate2 < BRCMS_NUM_RATES_MCS_1_STREAM;
  1444. rate1++, rate2++)
  1445. pi->txpwr_limit[rate1] = txpwr_ptr1[rate2];
  1446. }
  1447. for (k = 0; k < 2; k++) {
  1448. switch (k) {
  1449. case 0:
  1450. rate_start_index = WL_TX_POWER_MCS20_SDM_FIRST;
  1451. txpwr_ptr1 = txpwr->mcs_20_mimo;
  1452. break;
  1453. case 1:
  1454. rate_start_index = WL_TX_POWER_MCS40_SDM_FIRST;
  1455. txpwr_ptr1 = txpwr->mcs_40_mimo;
  1456. break;
  1457. }
  1458. for (rate1 = rate_start_index, rate2 = 0;
  1459. rate2 < BRCMS_NUM_RATES_MCS_2_STREAM;
  1460. rate1++, rate2++)
  1461. pi->txpwr_limit[rate1] = txpwr_ptr1[rate2];
  1462. }
  1463. pi->txpwr_limit[WL_TX_POWER_MCS_32] = txpwr->mcs32;
  1464. pi->txpwr_limit[WL_TX_POWER_MCS40_CDD_FIRST] =
  1465. min(pi->txpwr_limit[WL_TX_POWER_MCS40_CDD_FIRST],
  1466. pi->txpwr_limit[WL_TX_POWER_MCS_32]);
  1467. pi->txpwr_limit[WL_TX_POWER_MCS_32] =
  1468. pi->txpwr_limit[WL_TX_POWER_MCS40_CDD_FIRST];
  1469. }
  1470. }
  1471. void wlc_phy_txpwr_percent_set(struct brcms_phy_pub *ppi, u8 txpwr_percent)
  1472. {
  1473. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1474. pi->txpwr_percent = txpwr_percent;
  1475. }
  1476. void wlc_phy_machwcap_set(struct brcms_phy_pub *ppi, u32 machwcap)
  1477. {
  1478. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1479. pi->sh->machwcap = machwcap;
  1480. }
  1481. void wlc_phy_runbist_config(struct brcms_phy_pub *ppi, bool start_end)
  1482. {
  1483. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1484. u16 rxc;
  1485. rxc = 0;
  1486. if (start_end == ON) {
  1487. if (!ISNPHY(pi))
  1488. return;
  1489. if (NREV_IS(pi->pubpi.phy_rev, 3)
  1490. || NREV_IS(pi->pubpi.phy_rev, 4)) {
  1491. W_REG(&pi->regs->phyregaddr, 0xa0);
  1492. (void)R_REG(&pi->regs->phyregaddr);
  1493. rxc = R_REG(&pi->regs->phyregdata);
  1494. W_REG(&pi->regs->phyregdata,
  1495. (0x1 << 15) | rxc);
  1496. }
  1497. } else {
  1498. if (NREV_IS(pi->pubpi.phy_rev, 3)
  1499. || NREV_IS(pi->pubpi.phy_rev, 4)) {
  1500. W_REG(&pi->regs->phyregaddr, 0xa0);
  1501. (void)R_REG(&pi->regs->phyregaddr);
  1502. W_REG(&pi->regs->phyregdata, rxc);
  1503. }
  1504. wlc_phy_por_inform(ppi);
  1505. }
  1506. }
  1507. void
  1508. wlc_phy_txpower_limit_set(struct brcms_phy_pub *ppi, struct txpwr_limits *txpwr,
  1509. u16 chanspec)
  1510. {
  1511. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1512. wlc_phy_txpower_reg_limit_calc(pi, txpwr, chanspec);
  1513. if (ISLCNPHY(pi)) {
  1514. int i, j;
  1515. for (i = TXP_FIRST_OFDM_20_CDD, j = 0;
  1516. j < BRCMS_NUM_RATES_MCS_1_STREAM; i++, j++) {
  1517. if (txpwr->mcs_20_siso[j])
  1518. pi->txpwr_limit[i] = txpwr->mcs_20_siso[j];
  1519. else
  1520. pi->txpwr_limit[i] = txpwr->ofdm[j];
  1521. }
  1522. }
  1523. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1524. wlc_phy_txpower_recalc_target(pi);
  1525. wlc_phy_cal_txpower_recalc_sw(pi);
  1526. wlapi_enable_mac(pi->sh->physhim);
  1527. }
  1528. void wlc_phy_ofdm_rateset_war(struct brcms_phy_pub *pih, bool war)
  1529. {
  1530. struct brcms_phy *pi = (struct brcms_phy *) pih;
  1531. pi->ofdm_rateset_war = war;
  1532. }
  1533. void wlc_phy_bf_preempt_enable(struct brcms_phy_pub *pih, bool bf_preempt)
  1534. {
  1535. struct brcms_phy *pi = (struct brcms_phy *) pih;
  1536. pi->bf_preempt_4306 = bf_preempt;
  1537. }
  1538. void wlc_phy_txpower_update_shm(struct brcms_phy *pi)
  1539. {
  1540. int j;
  1541. if (ISNPHY(pi))
  1542. return;
  1543. if (!pi->sh->clk)
  1544. return;
  1545. if (pi->hwpwrctrl) {
  1546. u16 offset;
  1547. wlapi_bmac_write_shm(pi->sh->physhim, M_TXPWR_MAX, 63);
  1548. wlapi_bmac_write_shm(pi->sh->physhim, M_TXPWR_N,
  1549. 1 << NUM_TSSI_FRAMES);
  1550. wlapi_bmac_write_shm(pi->sh->physhim, M_TXPWR_TARGET,
  1551. pi->tx_power_min << NUM_TSSI_FRAMES);
  1552. wlapi_bmac_write_shm(pi->sh->physhim, M_TXPWR_CUR,
  1553. pi->hwpwr_txcur);
  1554. for (j = TXP_FIRST_OFDM; j <= TXP_LAST_OFDM; j++) {
  1555. const u8 ucode_ofdm_rates[] = {
  1556. 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c
  1557. };
  1558. offset = wlapi_bmac_rate_shm_offset(
  1559. pi->sh->physhim,
  1560. ucode_ofdm_rates[j - TXP_FIRST_OFDM]);
  1561. wlapi_bmac_write_shm(pi->sh->physhim, offset + 6,
  1562. pi->tx_power_offset[j]);
  1563. wlapi_bmac_write_shm(pi->sh->physhim, offset + 14,
  1564. -(pi->tx_power_offset[j] / 2));
  1565. }
  1566. wlapi_bmac_mhf(pi->sh->physhim, MHF2, MHF2_HWPWRCTL,
  1567. MHF2_HWPWRCTL, BRCM_BAND_ALL);
  1568. } else {
  1569. int i;
  1570. for (i = TXP_FIRST_OFDM; i <= TXP_LAST_OFDM; i++)
  1571. pi->tx_power_offset[i] =
  1572. (u8) roundup(pi->tx_power_offset[i], 8);
  1573. wlapi_bmac_write_shm(pi->sh->physhim, M_OFDM_OFFSET,
  1574. (u16)
  1575. ((pi->tx_power_offset[TXP_FIRST_OFDM]
  1576. + 7) >> 3));
  1577. }
  1578. }
  1579. bool wlc_phy_txpower_hw_ctrl_get(struct brcms_phy_pub *ppi)
  1580. {
  1581. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1582. if (ISNPHY(pi))
  1583. return pi->nphy_txpwrctrl;
  1584. else
  1585. return pi->hwpwrctrl;
  1586. }
  1587. void wlc_phy_txpower_hw_ctrl_set(struct brcms_phy_pub *ppi, bool hwpwrctrl)
  1588. {
  1589. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1590. bool suspend;
  1591. if (!pi->hwpwrctrl_capable)
  1592. return;
  1593. pi->hwpwrctrl = hwpwrctrl;
  1594. pi->nphy_txpwrctrl = hwpwrctrl;
  1595. pi->txpwrctrl = hwpwrctrl;
  1596. if (ISNPHY(pi)) {
  1597. suspend = (0 == (R_REG(&pi->regs->maccontrol) & MCTL_EN_MAC));
  1598. if (!suspend)
  1599. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1600. wlc_phy_txpwrctrl_enable_nphy(pi, pi->nphy_txpwrctrl);
  1601. if (pi->nphy_txpwrctrl == PHY_TPC_HW_OFF)
  1602. wlc_phy_txpwr_fixpower_nphy(pi);
  1603. else
  1604. mod_phy_reg(pi, 0x1e7, (0x7f << 0),
  1605. pi->saved_txpwr_idx);
  1606. if (!suspend)
  1607. wlapi_enable_mac(pi->sh->physhim);
  1608. }
  1609. }
  1610. void wlc_phy_txpower_ipa_upd(struct brcms_phy *pi)
  1611. {
  1612. if (NREV_GE(pi->pubpi.phy_rev, 3)) {
  1613. pi->ipa2g_on = (pi->srom_fem2g.extpagain == 2);
  1614. pi->ipa5g_on = (pi->srom_fem5g.extpagain == 2);
  1615. } else {
  1616. pi->ipa2g_on = false;
  1617. pi->ipa5g_on = false;
  1618. }
  1619. }
  1620. static u32 wlc_phy_txpower_est_power_nphy(struct brcms_phy *pi)
  1621. {
  1622. s16 tx0_status, tx1_status;
  1623. u16 estPower1, estPower2;
  1624. u8 pwr0, pwr1, adj_pwr0, adj_pwr1;
  1625. u32 est_pwr;
  1626. estPower1 = read_phy_reg(pi, 0x118);
  1627. estPower2 = read_phy_reg(pi, 0x119);
  1628. if ((estPower1 & (0x1 << 8)) == (0x1 << 8))
  1629. pwr0 = (u8) (estPower1 & (0xff << 0)) >> 0;
  1630. else
  1631. pwr0 = 0x80;
  1632. if ((estPower2 & (0x1 << 8)) == (0x1 << 8))
  1633. pwr1 = (u8) (estPower2 & (0xff << 0)) >> 0;
  1634. else
  1635. pwr1 = 0x80;
  1636. tx0_status = read_phy_reg(pi, 0x1ed);
  1637. tx1_status = read_phy_reg(pi, 0x1ee);
  1638. if ((tx0_status & (0x1 << 15)) == (0x1 << 15))
  1639. adj_pwr0 = (u8) (tx0_status & (0xff << 0)) >> 0;
  1640. else
  1641. adj_pwr0 = 0x80;
  1642. if ((tx1_status & (0x1 << 15)) == (0x1 << 15))
  1643. adj_pwr1 = (u8) (tx1_status & (0xff << 0)) >> 0;
  1644. else
  1645. adj_pwr1 = 0x80;
  1646. est_pwr = (u32) ((pwr0 << 24) | (pwr1 << 16) | (adj_pwr0 << 8) |
  1647. adj_pwr1);
  1648. return est_pwr;
  1649. }
  1650. void
  1651. wlc_phy_txpower_get_current(struct brcms_phy_pub *ppi, struct tx_power *power,
  1652. uint channel)
  1653. {
  1654. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1655. uint rate, num_rates;
  1656. u8 min_pwr, max_pwr;
  1657. #if WL_TX_POWER_RATES != TXP_NUM_RATES
  1658. #error "struct tx_power out of sync with this fn"
  1659. #endif
  1660. if (ISNPHY(pi)) {
  1661. power->rf_cores = 2;
  1662. power->flags |= (WL_TX_POWER_F_MIMO);
  1663. if (pi->nphy_txpwrctrl == PHY_TPC_HW_ON)
  1664. power->flags |=
  1665. (WL_TX_POWER_F_ENABLED | WL_TX_POWER_F_HW);
  1666. } else if (ISLCNPHY(pi)) {
  1667. power->rf_cores = 1;
  1668. power->flags |= (WL_TX_POWER_F_SISO);
  1669. if (pi->radiopwr_override == RADIOPWR_OVERRIDE_DEF)
  1670. power->flags |= WL_TX_POWER_F_ENABLED;
  1671. if (pi->hwpwrctrl)
  1672. power->flags |= WL_TX_POWER_F_HW;
  1673. }
  1674. num_rates = ((ISNPHY(pi)) ? (TXP_NUM_RATES) :
  1675. ((ISLCNPHY(pi)) ?
  1676. (TXP_LAST_OFDM_20_CDD + 1) : (TXP_LAST_OFDM + 1)));
  1677. for (rate = 0; rate < num_rates; rate++) {
  1678. power->user_limit[rate] = pi->tx_user_target[rate];
  1679. wlc_phy_txpower_sromlimit(ppi, channel, &min_pwr, &max_pwr,
  1680. rate);
  1681. power->board_limit[rate] = (u8) max_pwr;
  1682. power->target[rate] = pi->tx_power_target[rate];
  1683. }
  1684. if (ISNPHY(pi)) {
  1685. u32 est_pout;
  1686. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1687. wlc_phyreg_enter((struct brcms_phy_pub *) pi);
  1688. est_pout = wlc_phy_txpower_est_power_nphy(pi);
  1689. wlc_phyreg_exit((struct brcms_phy_pub *) pi);
  1690. wlapi_enable_mac(pi->sh->physhim);
  1691. power->est_Pout[0] = (est_pout >> 8) & 0xff;
  1692. power->est_Pout[1] = est_pout & 0xff;
  1693. power->est_Pout_act[0] = est_pout >> 24;
  1694. power->est_Pout_act[1] = (est_pout >> 16) & 0xff;
  1695. if (power->est_Pout[0] == 0x80)
  1696. power->est_Pout[0] = 0;
  1697. if (power->est_Pout[1] == 0x80)
  1698. power->est_Pout[1] = 0;
  1699. if (power->est_Pout_act[0] == 0x80)
  1700. power->est_Pout_act[0] = 0;
  1701. if (power->est_Pout_act[1] == 0x80)
  1702. power->est_Pout_act[1] = 0;
  1703. power->est_Pout_cck = 0;
  1704. power->tx_power_max[0] = pi->tx_power_max;
  1705. power->tx_power_max[1] = pi->tx_power_max;
  1706. power->tx_power_max_rate_ind[0] = pi->tx_power_max_rate_ind;
  1707. power->tx_power_max_rate_ind[1] = pi->tx_power_max_rate_ind;
  1708. } else if (pi->hwpwrctrl && pi->sh->up) {
  1709. wlc_phyreg_enter(ppi);
  1710. if (ISLCNPHY(pi)) {
  1711. power->tx_power_max[0] = pi->tx_power_max;
  1712. power->tx_power_max[1] = pi->tx_power_max;
  1713. power->tx_power_max_rate_ind[0] =
  1714. pi->tx_power_max_rate_ind;
  1715. power->tx_power_max_rate_ind[1] =
  1716. pi->tx_power_max_rate_ind;
  1717. if (wlc_phy_tpc_isenabled_lcnphy(pi))
  1718. power->flags |=
  1719. (WL_TX_POWER_F_HW |
  1720. WL_TX_POWER_F_ENABLED);
  1721. else
  1722. power->flags &=
  1723. ~(WL_TX_POWER_F_HW |
  1724. WL_TX_POWER_F_ENABLED);
  1725. wlc_lcnphy_get_tssi(pi, (s8 *) &power->est_Pout[0],
  1726. (s8 *) &power->est_Pout_cck);
  1727. }
  1728. wlc_phyreg_exit(ppi);
  1729. }
  1730. }
  1731. void wlc_phy_antsel_type_set(struct brcms_phy_pub *ppi, u8 antsel_type)
  1732. {
  1733. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1734. pi->antsel_type = antsel_type;
  1735. }
  1736. bool wlc_phy_test_ison(struct brcms_phy_pub *ppi)
  1737. {
  1738. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1739. return pi->phytest_on;
  1740. }
  1741. void wlc_phy_ant_rxdiv_set(struct brcms_phy_pub *ppi, u8 val)
  1742. {
  1743. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  1744. bool suspend;
  1745. pi->sh->rx_antdiv = val;
  1746. if (!(ISNPHY(pi) && D11REV_IS(pi->sh->corerev, 16))) {
  1747. if (val > ANT_RX_DIV_FORCE_1)
  1748. wlapi_bmac_mhf(pi->sh->physhim, MHF1, MHF1_ANTDIV,
  1749. MHF1_ANTDIV, BRCM_BAND_ALL);
  1750. else
  1751. wlapi_bmac_mhf(pi->sh->physhim, MHF1, MHF1_ANTDIV, 0,
  1752. BRCM_BAND_ALL);
  1753. }
  1754. if (ISNPHY(pi))
  1755. return;
  1756. if (!pi->sh->clk)
  1757. return;
  1758. suspend = (0 == (R_REG(&pi->regs->maccontrol) & MCTL_EN_MAC));
  1759. if (!suspend)
  1760. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1761. if (ISLCNPHY(pi)) {
  1762. if (val > ANT_RX_DIV_FORCE_1) {
  1763. mod_phy_reg(pi, 0x410, (0x1 << 1), 0x01 << 1);
  1764. mod_phy_reg(pi, 0x410,
  1765. (0x1 << 0),
  1766. ((ANT_RX_DIV_START_1 == val) ? 1 : 0) << 0);
  1767. } else {
  1768. mod_phy_reg(pi, 0x410, (0x1 << 1), 0x00 << 1);
  1769. mod_phy_reg(pi, 0x410, (0x1 << 0), (u16) val << 0);
  1770. }
  1771. }
  1772. if (!suspend)
  1773. wlapi_enable_mac(pi->sh->physhim);
  1774. return;
  1775. }
  1776. static bool
  1777. wlc_phy_noise_calc_phy(struct brcms_phy *pi, u32 *cmplx_pwr, s8 *pwr_ant)
  1778. {
  1779. s8 cmplx_pwr_dbm[PHY_CORE_MAX];
  1780. u8 i;
  1781. memset((u8 *) cmplx_pwr_dbm, 0, sizeof(cmplx_pwr_dbm));
  1782. wlc_phy_compute_dB(cmplx_pwr, cmplx_pwr_dbm, pi->pubpi.phy_corenum);
  1783. for (i = 0; i < pi->pubpi.phy_corenum; i++) {
  1784. if (NREV_GE(pi->pubpi.phy_rev, 3))
  1785. cmplx_pwr_dbm[i] += (s8) PHY_NOISE_OFFSETFACT_4322;
  1786. else
  1787. cmplx_pwr_dbm[i] += (s8) (16 - (15) * 3 - 70);
  1788. }
  1789. for (i = 0; i < pi->pubpi.phy_corenum; i++) {
  1790. pi->nphy_noise_win[i][pi->nphy_noise_index] = cmplx_pwr_dbm[i];
  1791. pwr_ant[i] = cmplx_pwr_dbm[i];
  1792. }
  1793. pi->nphy_noise_index =
  1794. MODINC_POW2(pi->nphy_noise_index, PHY_NOISE_WINDOW_SZ);
  1795. return true;
  1796. }
  1797. static void wlc_phy_noise_cb(struct brcms_phy *pi, u8 channel, s8 noise_dbm)
  1798. {
  1799. if (!pi->phynoise_state)
  1800. return;
  1801. if (pi->phynoise_state & PHY_NOISE_STATE_MON) {
  1802. if (pi->phynoise_chan_watchdog == channel) {
  1803. pi->sh->phy_noise_window[pi->sh->phy_noise_index] =
  1804. noise_dbm;
  1805. pi->sh->phy_noise_index =
  1806. MODINC(pi->sh->phy_noise_index, MA_WINDOW_SZ);
  1807. }
  1808. pi->phynoise_state &= ~PHY_NOISE_STATE_MON;
  1809. }
  1810. if (pi->phynoise_state & PHY_NOISE_STATE_EXTERNAL)
  1811. pi->phynoise_state &= ~PHY_NOISE_STATE_EXTERNAL;
  1812. }
  1813. static s8 wlc_phy_noise_read_shmem(struct brcms_phy *pi)
  1814. {
  1815. u32 cmplx_pwr[PHY_CORE_MAX];
  1816. s8 noise_dbm_ant[PHY_CORE_MAX];
  1817. u16 lo, hi;
  1818. u32 cmplx_pwr_tot = 0;
  1819. s8 noise_dbm = PHY_NOISE_FIXED_VAL_NPHY;
  1820. u8 idx, core;
  1821. memset((u8 *) cmplx_pwr, 0, sizeof(cmplx_pwr));
  1822. memset((u8 *) noise_dbm_ant, 0, sizeof(noise_dbm_ant));
  1823. for (idx = 0, core = 0; core < pi->pubpi.phy_corenum; idx += 2,
  1824. core++) {
  1825. lo = wlapi_bmac_read_shm(pi->sh->physhim, M_PWRIND_MAP(idx));
  1826. hi = wlapi_bmac_read_shm(pi->sh->physhim,
  1827. M_PWRIND_MAP(idx + 1));
  1828. cmplx_pwr[core] = (hi << 16) + lo;
  1829. cmplx_pwr_tot += cmplx_pwr[core];
  1830. if (cmplx_pwr[core] == 0)
  1831. noise_dbm_ant[core] = PHY_NOISE_FIXED_VAL_NPHY;
  1832. else
  1833. cmplx_pwr[core] >>= PHY_NOISE_SAMPLE_LOG_NUM_UCODE;
  1834. }
  1835. if (cmplx_pwr_tot != 0)
  1836. wlc_phy_noise_calc_phy(pi, cmplx_pwr, noise_dbm_ant);
  1837. for (core = 0; core < pi->pubpi.phy_corenum; core++) {
  1838. pi->nphy_noise_win[core][pi->nphy_noise_index] =
  1839. noise_dbm_ant[core];
  1840. if (noise_dbm_ant[core] > noise_dbm)
  1841. noise_dbm = noise_dbm_ant[core];
  1842. }
  1843. pi->nphy_noise_index =
  1844. MODINC_POW2(pi->nphy_noise_index, PHY_NOISE_WINDOW_SZ);
  1845. return noise_dbm;
  1846. }
  1847. void wlc_phy_noise_sample_intr(struct brcms_phy_pub *pih)
  1848. {
  1849. struct brcms_phy *pi = (struct brcms_phy *) pih;
  1850. u16 jssi_aux;
  1851. u8 channel = 0;
  1852. s8 noise_dbm = PHY_NOISE_FIXED_VAL_NPHY;
  1853. if (ISLCNPHY(pi)) {
  1854. u32 cmplx_pwr, cmplx_pwr0, cmplx_pwr1;
  1855. u16 lo, hi;
  1856. s32 pwr_offset_dB, gain_dB;
  1857. u16 status_0, status_1;
  1858. jssi_aux = wlapi_bmac_read_shm(pi->sh->physhim, M_JSSI_AUX);
  1859. channel = jssi_aux & D11_CURCHANNEL_MAX;
  1860. lo = wlapi_bmac_read_shm(pi->sh->physhim, M_PWRIND_MAP0);
  1861. hi = wlapi_bmac_read_shm(pi->sh->physhim, M_PWRIND_MAP1);
  1862. cmplx_pwr0 = (hi << 16) + lo;
  1863. lo = wlapi_bmac_read_shm(pi->sh->physhim, M_PWRIND_MAP2);
  1864. hi = wlapi_bmac_read_shm(pi->sh->physhim, M_PWRIND_MAP3);
  1865. cmplx_pwr1 = (hi << 16) + lo;
  1866. cmplx_pwr = (cmplx_pwr0 + cmplx_pwr1) >> 6;
  1867. status_0 = 0x44;
  1868. status_1 = wlapi_bmac_read_shm(pi->sh->physhim, M_JSSI_0);
  1869. if ((cmplx_pwr > 0 && cmplx_pwr < 500)
  1870. && ((status_1 & 0xc000) == 0x4000)) {
  1871. wlc_phy_compute_dB(&cmplx_pwr, &noise_dbm,
  1872. pi->pubpi.phy_corenum);
  1873. pwr_offset_dB = (read_phy_reg(pi, 0x434) & 0xFF);
  1874. if (pwr_offset_dB > 127)
  1875. pwr_offset_dB -= 256;
  1876. noise_dbm += (s8) (pwr_offset_dB - 30);
  1877. gain_dB = (status_0 & 0x1ff);
  1878. noise_dbm -= (s8) (gain_dB);
  1879. } else {
  1880. noise_dbm = PHY_NOISE_FIXED_VAL_LCNPHY;
  1881. }
  1882. } else if (ISNPHY(pi)) {
  1883. jssi_aux = wlapi_bmac_read_shm(pi->sh->physhim, M_JSSI_AUX);
  1884. channel = jssi_aux & D11_CURCHANNEL_MAX;
  1885. noise_dbm = wlc_phy_noise_read_shmem(pi);
  1886. }
  1887. wlc_phy_noise_cb(pi, channel, noise_dbm);
  1888. }
  1889. static void
  1890. wlc_phy_noise_sample_request(struct brcms_phy_pub *pih, u8 reason, u8 ch)
  1891. {
  1892. struct brcms_phy *pi = (struct brcms_phy *) pih;
  1893. s8 noise_dbm = PHY_NOISE_FIXED_VAL_NPHY;
  1894. bool sampling_in_progress = (pi->phynoise_state != 0);
  1895. bool wait_for_intr = true;
  1896. switch (reason) {
  1897. case PHY_NOISE_SAMPLE_MON:
  1898. pi->phynoise_chan_watchdog = ch;
  1899. pi->phynoise_state |= PHY_NOISE_STATE_MON;
  1900. break;
  1901. case PHY_NOISE_SAMPLE_EXTERNAL:
  1902. pi->phynoise_state |= PHY_NOISE_STATE_EXTERNAL;
  1903. break;
  1904. default:
  1905. break;
  1906. }
  1907. if (sampling_in_progress)
  1908. return;
  1909. pi->phynoise_now = pi->sh->now;
  1910. if (pi->phy_fixed_noise) {
  1911. if (ISNPHY(pi)) {
  1912. pi->nphy_noise_win[WL_ANT_IDX_1][pi->nphy_noise_index] =
  1913. PHY_NOISE_FIXED_VAL_NPHY;
  1914. pi->nphy_noise_win[WL_ANT_IDX_2][pi->nphy_noise_index] =
  1915. PHY_NOISE_FIXED_VAL_NPHY;
  1916. pi->nphy_noise_index = MODINC_POW2(pi->nphy_noise_index,
  1917. PHY_NOISE_WINDOW_SZ);
  1918. noise_dbm = PHY_NOISE_FIXED_VAL_NPHY;
  1919. } else {
  1920. noise_dbm = PHY_NOISE_FIXED_VAL;
  1921. }
  1922. wait_for_intr = false;
  1923. goto done;
  1924. }
  1925. if (ISLCNPHY(pi)) {
  1926. if (!pi->phynoise_polling
  1927. || (reason == PHY_NOISE_SAMPLE_EXTERNAL)) {
  1928. wlapi_bmac_write_shm(pi->sh->physhim, M_JSSI_0, 0);
  1929. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP0, 0);
  1930. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP1, 0);
  1931. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP2, 0);
  1932. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP3, 0);
  1933. OR_REG(&pi->regs->maccommand,
  1934. MCMD_BG_NOISE);
  1935. } else {
  1936. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1937. wlc_lcnphy_deaf_mode(pi, (bool) 0);
  1938. noise_dbm = (s8) wlc_lcnphy_rx_signal_power(pi, 20);
  1939. wlc_lcnphy_deaf_mode(pi, (bool) 1);
  1940. wlapi_enable_mac(pi->sh->physhim);
  1941. wait_for_intr = false;
  1942. }
  1943. } else if (ISNPHY(pi)) {
  1944. if (!pi->phynoise_polling
  1945. || (reason == PHY_NOISE_SAMPLE_EXTERNAL)) {
  1946. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP0, 0);
  1947. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP1, 0);
  1948. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP2, 0);
  1949. wlapi_bmac_write_shm(pi->sh->physhim, M_PWRIND_MAP3, 0);
  1950. OR_REG(&pi->regs->maccommand,
  1951. MCMD_BG_NOISE);
  1952. } else {
  1953. struct phy_iq_est est[PHY_CORE_MAX];
  1954. u32 cmplx_pwr[PHY_CORE_MAX];
  1955. s8 noise_dbm_ant[PHY_CORE_MAX];
  1956. u16 log_num_samps, num_samps, classif_state = 0;
  1957. u8 wait_time = 32;
  1958. u8 wait_crs = 0;
  1959. u8 i;
  1960. memset((u8 *) est, 0, sizeof(est));
  1961. memset((u8 *) cmplx_pwr, 0, sizeof(cmplx_pwr));
  1962. memset((u8 *) noise_dbm_ant, 0, sizeof(noise_dbm_ant));
  1963. log_num_samps = PHY_NOISE_SAMPLE_LOG_NUM_NPHY;
  1964. num_samps = 1 << log_num_samps;
  1965. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  1966. classif_state = wlc_phy_classifier_nphy(pi, 0, 0);
  1967. wlc_phy_classifier_nphy(pi, 3, 0);
  1968. wlc_phy_rx_iq_est_nphy(pi, est, num_samps, wait_time,
  1969. wait_crs);
  1970. wlc_phy_classifier_nphy(pi, (0x7 << 0), classif_state);
  1971. wlapi_enable_mac(pi->sh->physhim);
  1972. for (i = 0; i < pi->pubpi.phy_corenum; i++)
  1973. cmplx_pwr[i] = (est[i].i_pwr + est[i].q_pwr) >>
  1974. log_num_samps;
  1975. wlc_phy_noise_calc_phy(pi, cmplx_pwr, noise_dbm_ant);
  1976. for (i = 0; i < pi->pubpi.phy_corenum; i++) {
  1977. pi->nphy_noise_win[i][pi->nphy_noise_index] =
  1978. noise_dbm_ant[i];
  1979. if (noise_dbm_ant[i] > noise_dbm)
  1980. noise_dbm = noise_dbm_ant[i];
  1981. }
  1982. pi->nphy_noise_index = MODINC_POW2(pi->nphy_noise_index,
  1983. PHY_NOISE_WINDOW_SZ);
  1984. wait_for_intr = false;
  1985. }
  1986. }
  1987. done:
  1988. if (!wait_for_intr)
  1989. wlc_phy_noise_cb(pi, ch, noise_dbm);
  1990. }
  1991. void wlc_phy_noise_sample_request_external(struct brcms_phy_pub *pih)
  1992. {
  1993. u8 channel;
  1994. channel = CHSPEC_CHANNEL(wlc_phy_chanspec_get(pih));
  1995. wlc_phy_noise_sample_request(pih, PHY_NOISE_SAMPLE_EXTERNAL, channel);
  1996. }
  1997. static const s8 lcnphy_gain_index_offset_for_pkt_rssi[] = {
  1998. 8,
  1999. 8,
  2000. 8,
  2001. 8,
  2002. 8,
  2003. 8,
  2004. 8,
  2005. 9,
  2006. 10,
  2007. 8,
  2008. 8,
  2009. 7,
  2010. 7,
  2011. 1,
  2012. 2,
  2013. 2,
  2014. 2,
  2015. 2,
  2016. 2,
  2017. 2,
  2018. 2,
  2019. 2,
  2020. 2,
  2021. 2,
  2022. 2,
  2023. 2,
  2024. 2,
  2025. 2,
  2026. 2,
  2027. 2,
  2028. 2,
  2029. 2,
  2030. 1,
  2031. 1,
  2032. 0,
  2033. 0,
  2034. 0,
  2035. 0
  2036. };
  2037. void wlc_phy_compute_dB(u32 *cmplx_pwr, s8 *p_cmplx_pwr_dB, u8 core)
  2038. {
  2039. u8 msb, secondmsb, i;
  2040. u32 tmp;
  2041. for (i = 0; i < core; i++) {
  2042. secondmsb = 0;
  2043. tmp = cmplx_pwr[i];
  2044. msb = fls(tmp);
  2045. if (msb)
  2046. secondmsb = (u8) ((tmp >> (--msb - 1)) & 1);
  2047. p_cmplx_pwr_dB[i] = (s8) (3 * msb + 2 * secondmsb);
  2048. }
  2049. }
  2050. int wlc_phy_rssi_compute(struct brcms_phy_pub *pih,
  2051. struct d11rxhdr *rxh)
  2052. {
  2053. int rssi = rxh->PhyRxStatus_1 & PRXS1_JSSI_MASK;
  2054. uint radioid = pih->radioid;
  2055. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2056. if ((pi->sh->corerev >= 11)
  2057. && !(rxh->RxStatus2 & RXS_PHYRXST_VALID)) {
  2058. rssi = BRCMS_RSSI_INVALID;
  2059. goto end;
  2060. }
  2061. if (ISLCNPHY(pi)) {
  2062. u8 gidx = (rxh->PhyRxStatus_2 & 0xFC00) >> 10;
  2063. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2064. if (rssi > 127)
  2065. rssi -= 256;
  2066. rssi = rssi + lcnphy_gain_index_offset_for_pkt_rssi[gidx];
  2067. if ((rssi > -46) && (gidx > 18))
  2068. rssi = rssi + 7;
  2069. rssi = rssi + pi_lcn->lcnphy_pkteng_rssi_slope;
  2070. rssi = rssi + 2;
  2071. }
  2072. if (ISLCNPHY(pi)) {
  2073. if (rssi > 127)
  2074. rssi -= 256;
  2075. } else if (radioid == BCM2055_ID || radioid == BCM2056_ID
  2076. || radioid == BCM2057_ID) {
  2077. rssi = wlc_phy_rssi_compute_nphy(pi, rxh);
  2078. }
  2079. end:
  2080. return rssi;
  2081. }
  2082. void wlc_phy_freqtrack_start(struct brcms_phy_pub *pih)
  2083. {
  2084. return;
  2085. }
  2086. void wlc_phy_freqtrack_end(struct brcms_phy_pub *pih)
  2087. {
  2088. return;
  2089. }
  2090. void wlc_phy_set_deaf(struct brcms_phy_pub *ppi, bool user_flag)
  2091. {
  2092. struct brcms_phy *pi;
  2093. pi = (struct brcms_phy *) ppi;
  2094. if (ISLCNPHY(pi))
  2095. wlc_lcnphy_deaf_mode(pi, true);
  2096. else if (ISNPHY(pi))
  2097. wlc_nphy_deaf_mode(pi, true);
  2098. }
  2099. void wlc_phy_watchdog(struct brcms_phy_pub *pih)
  2100. {
  2101. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2102. bool delay_phy_cal = false;
  2103. pi->sh->now++;
  2104. if (!pi->watchdog_override)
  2105. return;
  2106. if (!(SCAN_RM_IN_PROGRESS(pi) || PLT_INPROG_PHY(pi)))
  2107. wlc_phy_noise_sample_request((struct brcms_phy_pub *) pi,
  2108. PHY_NOISE_SAMPLE_MON,
  2109. CHSPEC_CHANNEL(pi->
  2110. radio_chanspec));
  2111. if (pi->phynoise_state && (pi->sh->now - pi->phynoise_now) > 5)
  2112. pi->phynoise_state = 0;
  2113. if ((!pi->phycal_txpower) ||
  2114. ((pi->sh->now - pi->phycal_txpower) >= pi->sh->fast_timer)) {
  2115. if (!SCAN_INPROG_PHY(pi) && wlc_phy_cal_txpower_recalc_sw(pi))
  2116. pi->phycal_txpower = pi->sh->now;
  2117. }
  2118. if ((SCAN_RM_IN_PROGRESS(pi) || PLT_INPROG_PHY(pi)
  2119. || ASSOC_INPROG_PHY(pi)))
  2120. return;
  2121. if (ISNPHY(pi) && !pi->disable_percal && !delay_phy_cal) {
  2122. if ((pi->nphy_perical != PHY_PERICAL_DISABLE) &&
  2123. (pi->nphy_perical != PHY_PERICAL_MANUAL) &&
  2124. ((pi->sh->now - pi->nphy_perical_last) >=
  2125. pi->sh->glacial_timer))
  2126. wlc_phy_cal_perical((struct brcms_phy_pub *) pi,
  2127. PHY_PERICAL_WATCHDOG);
  2128. wlc_phy_txpwr_papd_cal_nphy(pi);
  2129. }
  2130. if (ISLCNPHY(pi)) {
  2131. if (pi->phy_forcecal ||
  2132. ((pi->sh->now - pi->phy_lastcal) >=
  2133. pi->sh->glacial_timer)) {
  2134. if (!(SCAN_RM_IN_PROGRESS(pi) || ASSOC_INPROG_PHY(pi)))
  2135. wlc_lcnphy_calib_modes(
  2136. pi,
  2137. LCNPHY_PERICAL_TEMPBASED_TXPWRCTRL);
  2138. if (!
  2139. (SCAN_RM_IN_PROGRESS(pi) || PLT_INPROG_PHY(pi)
  2140. || ASSOC_INPROG_PHY(pi)
  2141. || pi->carrier_suppr_disable
  2142. || pi->disable_percal))
  2143. wlc_lcnphy_calib_modes(pi,
  2144. PHY_PERICAL_WATCHDOG);
  2145. }
  2146. }
  2147. }
  2148. void wlc_phy_BSSinit(struct brcms_phy_pub *pih, bool bonlyap, int rssi)
  2149. {
  2150. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2151. uint i;
  2152. uint k;
  2153. for (i = 0; i < MA_WINDOW_SZ; i++)
  2154. pi->sh->phy_noise_window[i] = (s8) (rssi & 0xff);
  2155. if (ISLCNPHY(pi)) {
  2156. for (i = 0; i < MA_WINDOW_SZ; i++)
  2157. pi->sh->phy_noise_window[i] =
  2158. PHY_NOISE_FIXED_VAL_LCNPHY;
  2159. }
  2160. pi->sh->phy_noise_index = 0;
  2161. for (i = 0; i < PHY_NOISE_WINDOW_SZ; i++) {
  2162. for (k = WL_ANT_IDX_1; k < WL_ANT_RX_MAX; k++)
  2163. pi->nphy_noise_win[k][i] = PHY_NOISE_FIXED_VAL_NPHY;
  2164. }
  2165. pi->nphy_noise_index = 0;
  2166. }
  2167. void
  2168. wlc_phy_papd_decode_epsilon(u32 epsilon, s32 *eps_real, s32 *eps_imag)
  2169. {
  2170. *eps_imag = (epsilon >> 13);
  2171. if (*eps_imag > 0xfff)
  2172. *eps_imag -= 0x2000;
  2173. *eps_real = (epsilon & 0x1fff);
  2174. if (*eps_real > 0xfff)
  2175. *eps_real -= 0x2000;
  2176. }
  2177. void wlc_phy_cal_perical_mphase_reset(struct brcms_phy *pi)
  2178. {
  2179. wlapi_del_timer(pi->phycal_timer);
  2180. pi->cal_type_override = PHY_PERICAL_AUTO;
  2181. pi->mphase_cal_phase_id = MPHASE_CAL_STATE_IDLE;
  2182. pi->mphase_txcal_cmdidx = 0;
  2183. }
  2184. static void
  2185. wlc_phy_cal_perical_mphase_schedule(struct brcms_phy *pi, uint delay)
  2186. {
  2187. if ((pi->nphy_perical != PHY_PERICAL_MPHASE) &&
  2188. (pi->nphy_perical != PHY_PERICAL_MANUAL))
  2189. return;
  2190. wlapi_del_timer(pi->phycal_timer);
  2191. pi->mphase_cal_phase_id = MPHASE_CAL_STATE_INIT;
  2192. wlapi_add_timer(pi->phycal_timer, delay, 0);
  2193. }
  2194. void wlc_phy_cal_perical(struct brcms_phy_pub *pih, u8 reason)
  2195. {
  2196. s16 nphy_currtemp = 0;
  2197. s16 delta_temp = 0;
  2198. bool do_periodic_cal = true;
  2199. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2200. if (!ISNPHY(pi))
  2201. return;
  2202. if ((pi->nphy_perical == PHY_PERICAL_DISABLE) ||
  2203. (pi->nphy_perical == PHY_PERICAL_MANUAL))
  2204. return;
  2205. switch (reason) {
  2206. case PHY_PERICAL_DRIVERUP:
  2207. break;
  2208. case PHY_PERICAL_PHYINIT:
  2209. if (pi->nphy_perical == PHY_PERICAL_MPHASE) {
  2210. if (PHY_PERICAL_MPHASE_PENDING(pi))
  2211. wlc_phy_cal_perical_mphase_reset(pi);
  2212. wlc_phy_cal_perical_mphase_schedule(
  2213. pi,
  2214. PHY_PERICAL_INIT_DELAY);
  2215. }
  2216. break;
  2217. case PHY_PERICAL_JOIN_BSS:
  2218. case PHY_PERICAL_START_IBSS:
  2219. case PHY_PERICAL_UP_BSS:
  2220. if ((pi->nphy_perical == PHY_PERICAL_MPHASE) &&
  2221. PHY_PERICAL_MPHASE_PENDING(pi))
  2222. wlc_phy_cal_perical_mphase_reset(pi);
  2223. pi->first_cal_after_assoc = true;
  2224. pi->cal_type_override = PHY_PERICAL_FULL;
  2225. if (pi->phycal_tempdelta)
  2226. pi->nphy_lastcal_temp = wlc_phy_tempsense_nphy(pi);
  2227. wlc_phy_cal_perical_nphy_run(pi, PHY_PERICAL_FULL);
  2228. break;
  2229. case PHY_PERICAL_WATCHDOG:
  2230. if (pi->phycal_tempdelta) {
  2231. nphy_currtemp = wlc_phy_tempsense_nphy(pi);
  2232. delta_temp =
  2233. (nphy_currtemp > pi->nphy_lastcal_temp) ?
  2234. nphy_currtemp - pi->nphy_lastcal_temp :
  2235. pi->nphy_lastcal_temp - nphy_currtemp;
  2236. if ((delta_temp < (s16) pi->phycal_tempdelta) &&
  2237. (pi->nphy_txiqlocal_chanspec ==
  2238. pi->radio_chanspec))
  2239. do_periodic_cal = false;
  2240. else
  2241. pi->nphy_lastcal_temp = nphy_currtemp;
  2242. }
  2243. if (do_periodic_cal) {
  2244. if (pi->nphy_perical == PHY_PERICAL_MPHASE) {
  2245. if (!PHY_PERICAL_MPHASE_PENDING(pi))
  2246. wlc_phy_cal_perical_mphase_schedule(
  2247. pi,
  2248. PHY_PERICAL_WDOG_DELAY);
  2249. } else if (pi->nphy_perical == PHY_PERICAL_SPHASE)
  2250. wlc_phy_cal_perical_nphy_run(pi,
  2251. PHY_PERICAL_AUTO);
  2252. }
  2253. break;
  2254. default:
  2255. break;
  2256. }
  2257. }
  2258. void wlc_phy_cal_perical_mphase_restart(struct brcms_phy *pi)
  2259. {
  2260. pi->mphase_cal_phase_id = MPHASE_CAL_STATE_INIT;
  2261. pi->mphase_txcal_cmdidx = 0;
  2262. }
  2263. u8 wlc_phy_nbits(s32 value)
  2264. {
  2265. s32 abs_val;
  2266. u8 nbits = 0;
  2267. abs_val = abs(value);
  2268. while ((abs_val >> nbits) > 0)
  2269. nbits++;
  2270. return nbits;
  2271. }
  2272. void wlc_phy_stf_chain_init(struct brcms_phy_pub *pih, u8 txchain, u8 rxchain)
  2273. {
  2274. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2275. pi->sh->hw_phytxchain = txchain;
  2276. pi->sh->hw_phyrxchain = rxchain;
  2277. pi->sh->phytxchain = txchain;
  2278. pi->sh->phyrxchain = rxchain;
  2279. pi->pubpi.phy_corenum = (u8)hweight8(pi->sh->phyrxchain);
  2280. }
  2281. void wlc_phy_stf_chain_set(struct brcms_phy_pub *pih, u8 txchain, u8 rxchain)
  2282. {
  2283. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2284. pi->sh->phytxchain = txchain;
  2285. if (ISNPHY(pi))
  2286. wlc_phy_rxcore_setstate_nphy(pih, rxchain);
  2287. pi->pubpi.phy_corenum = (u8)hweight8(pi->sh->phyrxchain);
  2288. }
  2289. void wlc_phy_stf_chain_get(struct brcms_phy_pub *pih, u8 *txchain, u8 *rxchain)
  2290. {
  2291. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2292. *txchain = pi->sh->phytxchain;
  2293. *rxchain = pi->sh->phyrxchain;
  2294. }
  2295. u8 wlc_phy_stf_chain_active_get(struct brcms_phy_pub *pih)
  2296. {
  2297. s16 nphy_currtemp;
  2298. u8 active_bitmap;
  2299. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2300. active_bitmap = (pi->phy_txcore_heatedup) ? 0x31 : 0x33;
  2301. if (!pi->watchdog_override)
  2302. return active_bitmap;
  2303. if (NREV_GE(pi->pubpi.phy_rev, 6)) {
  2304. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2305. nphy_currtemp = wlc_phy_tempsense_nphy(pi);
  2306. wlapi_enable_mac(pi->sh->physhim);
  2307. if (!pi->phy_txcore_heatedup) {
  2308. if (nphy_currtemp >= pi->phy_txcore_disable_temp) {
  2309. active_bitmap &= 0xFD;
  2310. pi->phy_txcore_heatedup = true;
  2311. }
  2312. } else {
  2313. if (nphy_currtemp <= pi->phy_txcore_enable_temp) {
  2314. active_bitmap |= 0x2;
  2315. pi->phy_txcore_heatedup = false;
  2316. }
  2317. }
  2318. }
  2319. return active_bitmap;
  2320. }
  2321. s8 wlc_phy_stf_ssmode_get(struct brcms_phy_pub *pih, u16 chanspec)
  2322. {
  2323. struct brcms_phy *pi = (struct brcms_phy *) pih;
  2324. u8 siso_mcs_id, cdd_mcs_id;
  2325. siso_mcs_id =
  2326. (CHSPEC_IS40(chanspec)) ? TXP_FIRST_MCS_40_SISO :
  2327. TXP_FIRST_MCS_20_SISO;
  2328. cdd_mcs_id =
  2329. (CHSPEC_IS40(chanspec)) ? TXP_FIRST_MCS_40_CDD :
  2330. TXP_FIRST_MCS_20_CDD;
  2331. if (pi->tx_power_target[siso_mcs_id] >
  2332. (pi->tx_power_target[cdd_mcs_id] + 12))
  2333. return PHY_TXC1_MODE_SISO;
  2334. else
  2335. return PHY_TXC1_MODE_CDD;
  2336. }
  2337. const u8 *wlc_phy_get_ofdm_rate_lookup(void)
  2338. {
  2339. return ofdm_rate_lookup;
  2340. }
  2341. void wlc_lcnphy_epa_switch(struct brcms_phy *pi, bool mode)
  2342. {
  2343. if ((pi->sh->chip == BCM4313_CHIP_ID) &&
  2344. (pi->sh->boardflags & BFL_FEM)) {
  2345. if (mode) {
  2346. u16 txant = 0;
  2347. txant = wlapi_bmac_get_txant(pi->sh->physhim);
  2348. if (txant == 1) {
  2349. mod_phy_reg(pi, 0x44d, (0x1 << 2), (1) << 2);
  2350. mod_phy_reg(pi, 0x44c, (0x1 << 2), (1) << 2);
  2351. }
  2352. ai_corereg(pi->sh->sih, SI_CC_IDX,
  2353. offsetof(struct chipcregs, gpiocontrol),
  2354. ~0x0, 0x0);
  2355. ai_corereg(pi->sh->sih, SI_CC_IDX,
  2356. offsetof(struct chipcregs, gpioout), 0x40,
  2357. 0x40);
  2358. ai_corereg(pi->sh->sih, SI_CC_IDX,
  2359. offsetof(struct chipcregs, gpioouten), 0x40,
  2360. 0x40);
  2361. } else {
  2362. mod_phy_reg(pi, 0x44c, (0x1 << 2), (0) << 2);
  2363. mod_phy_reg(pi, 0x44d, (0x1 << 2), (0) << 2);
  2364. ai_corereg(pi->sh->sih, SI_CC_IDX,
  2365. offsetof(struct chipcregs, gpioout), 0x40,
  2366. 0x00);
  2367. ai_corereg(pi->sh->sih, SI_CC_IDX,
  2368. offsetof(struct chipcregs, gpioouten), 0x40,
  2369. 0x0);
  2370. ai_corereg(pi->sh->sih, SI_CC_IDX,
  2371. offsetof(struct chipcregs, gpiocontrol),
  2372. ~0x0, 0x40);
  2373. }
  2374. }
  2375. }
  2376. void wlc_phy_ldpc_override_set(struct brcms_phy_pub *ppi, bool ldpc)
  2377. {
  2378. return;
  2379. }
  2380. void
  2381. wlc_phy_get_pwrdet_offsets(struct brcms_phy *pi, s8 *cckoffset, s8 *ofdmoffset)
  2382. {
  2383. *cckoffset = 0;
  2384. *ofdmoffset = 0;
  2385. }
  2386. s8 wlc_phy_upd_rssi_offset(struct brcms_phy *pi, s8 rssi, u16 chanspec)
  2387. {
  2388. return rssi;
  2389. }
  2390. bool wlc_phy_txpower_ipa_ison(struct brcms_phy_pub *ppi)
  2391. {
  2392. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  2393. if (ISNPHY(pi))
  2394. return wlc_phy_n_txpower_ipa_ison(pi);
  2395. else
  2396. return 0;
  2397. }