phy_lcn.c 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247
  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/cordic.h>
  19. #include <pmu.h>
  20. #include <d11.h>
  21. #include <phy_shim.h>
  22. #include "phy_qmath.h"
  23. #include "phy_hal.h"
  24. #include "phy_radio.h"
  25. #include "phytbl_lcn.h"
  26. #include "phy_lcn.h"
  27. #define PLL_2064_NDIV 90
  28. #define PLL_2064_LOW_END_VCO 3000
  29. #define PLL_2064_LOW_END_KVCO 27
  30. #define PLL_2064_HIGH_END_VCO 4200
  31. #define PLL_2064_HIGH_END_KVCO 68
  32. #define PLL_2064_LOOP_BW_DOUBLER 200
  33. #define PLL_2064_D30_DOUBLER 10500
  34. #define PLL_2064_LOOP_BW 260
  35. #define PLL_2064_D30 8000
  36. #define PLL_2064_CAL_REF_TO 8
  37. #define PLL_2064_MHZ 1000000
  38. #define PLL_2064_OPEN_LOOP_DELAY 5
  39. #define TEMPSENSE 1
  40. #define VBATSENSE 2
  41. #define NOISE_IF_UPD_CHK_INTERVAL 1
  42. #define NOISE_IF_UPD_RST_INTERVAL 60
  43. #define NOISE_IF_UPD_THRESHOLD_CNT 1
  44. #define NOISE_IF_UPD_TRHRESHOLD 50
  45. #define NOISE_IF_UPD_TIMEOUT 1000
  46. #define NOISE_IF_OFF 0
  47. #define NOISE_IF_CHK 1
  48. #define NOISE_IF_ON 2
  49. #define PAPD_BLANKING_PROFILE 3
  50. #define PAPD2LUT 0
  51. #define PAPD_CORR_NORM 0
  52. #define PAPD_BLANKING_THRESHOLD 0
  53. #define PAPD_STOP_AFTER_LAST_UPDATE 0
  54. #define LCN_TARGET_PWR 60
  55. #define LCN_VBAT_OFFSET_433X 34649679
  56. #define LCN_VBAT_SLOPE_433X 8258032
  57. #define LCN_VBAT_SCALE_NOM 53
  58. #define LCN_VBAT_SCALE_DEN 432
  59. #define LCN_TEMPSENSE_OFFSET 80812
  60. #define LCN_TEMPSENSE_DEN 2647
  61. #define LCN_BW_LMT 200
  62. #define LCN_CUR_LMT 1250
  63. #define LCN_MULT 1
  64. #define LCN_VCO_DIV 30
  65. #define LCN_OFFSET 680
  66. #define LCN_FACT 490
  67. #define LCN_CUR_DIV 2640
  68. #define LCNPHY_txgainctrlovrval1_pagain_ovr_val1_SHIFT \
  69. (0 + 8)
  70. #define LCNPHY_txgainctrlovrval1_pagain_ovr_val1_MASK \
  71. (0x7f << LCNPHY_txgainctrlovrval1_pagain_ovr_val1_SHIFT)
  72. #define LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_SHIFT \
  73. (0 + 8)
  74. #define LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_MASK \
  75. (0x7f << LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_SHIFT)
  76. #define wlc_lcnphy_enable_tx_gain_override(pi) \
  77. wlc_lcnphy_set_tx_gain_override(pi, true)
  78. #define wlc_lcnphy_disable_tx_gain_override(pi) \
  79. wlc_lcnphy_set_tx_gain_override(pi, false)
  80. #define wlc_lcnphy_iqcal_active(pi) \
  81. (read_phy_reg((pi), 0x451) & \
  82. ((0x1 << 15) | (0x1 << 14)))
  83. #define txpwrctrl_off(pi) (0x7 != ((read_phy_reg(pi, 0x4a4) & 0xE000) >> 13))
  84. #define wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi) \
  85. (pi->temppwrctrl_capable)
  86. #define wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi) \
  87. (pi->hwpwrctrl_capable)
  88. #define SWCTRL_BT_TX 0x18
  89. #define SWCTRL_OVR_DISABLE 0x40
  90. #define AFE_CLK_INIT_MODE_TXRX2X 1
  91. #define AFE_CLK_INIT_MODE_PAPD 0
  92. #define LCNPHY_TBL_ID_IQLOCAL 0x00
  93. #define LCNPHY_TBL_ID_RFSEQ 0x08
  94. #define LCNPHY_TBL_ID_GAIN_IDX 0x0d
  95. #define LCNPHY_TBL_ID_SW_CTRL 0x0f
  96. #define LCNPHY_TBL_ID_GAIN_TBL 0x12
  97. #define LCNPHY_TBL_ID_SPUR 0x14
  98. #define LCNPHY_TBL_ID_SAMPLEPLAY 0x15
  99. #define LCNPHY_TBL_ID_SAMPLEPLAY1 0x16
  100. #define LCNPHY_TX_PWR_CTRL_RATE_OFFSET 832
  101. #define LCNPHY_TX_PWR_CTRL_MAC_OFFSET 128
  102. #define LCNPHY_TX_PWR_CTRL_GAIN_OFFSET 192
  103. #define LCNPHY_TX_PWR_CTRL_IQ_OFFSET 320
  104. #define LCNPHY_TX_PWR_CTRL_LO_OFFSET 448
  105. #define LCNPHY_TX_PWR_CTRL_PWR_OFFSET 576
  106. #define LCNPHY_TX_PWR_CTRL_START_INDEX_2G_4313 140
  107. #define LCNPHY_TX_PWR_CTRL_START_NPT 1
  108. #define LCNPHY_TX_PWR_CTRL_MAX_NPT 7
  109. #define LCNPHY_NOISE_SAMPLES_DEFAULT 5000
  110. #define LCNPHY_ACI_DETECT_START 1
  111. #define LCNPHY_ACI_DETECT_PROGRESS 2
  112. #define LCNPHY_ACI_DETECT_STOP 3
  113. #define LCNPHY_ACI_CRSHIFRMLO_TRSH 100
  114. #define LCNPHY_ACI_GLITCH_TRSH 2000
  115. #define LCNPHY_ACI_TMOUT 250
  116. #define LCNPHY_ACI_DETECT_TIMEOUT 2
  117. #define LCNPHY_ACI_START_DELAY 0
  118. #define wlc_lcnphy_tx_gain_override_enabled(pi) \
  119. (0 != (read_phy_reg((pi), 0x43b) & (0x1 << 6)))
  120. #define wlc_lcnphy_total_tx_frames(pi) \
  121. wlapi_bmac_read_shm((pi)->sh->physhim, M_UCODE_MACSTAT + \
  122. offsetof(struct macstat, txallfrm))
  123. struct lcnphy_txgains {
  124. u16 gm_gain;
  125. u16 pga_gain;
  126. u16 pad_gain;
  127. u16 dac_gain;
  128. };
  129. enum lcnphy_cal_mode {
  130. LCNPHY_CAL_FULL,
  131. LCNPHY_CAL_RECAL,
  132. LCNPHY_CAL_CURRECAL,
  133. LCNPHY_CAL_DIGCAL,
  134. LCNPHY_CAL_GCTRL
  135. };
  136. struct lcnphy_rx_iqcomp {
  137. u8 chan;
  138. s16 a;
  139. s16 b;
  140. };
  141. struct lcnphy_spb_tone {
  142. s16 re;
  143. s16 im;
  144. };
  145. struct lcnphy_unsign16_struct {
  146. u16 re;
  147. u16 im;
  148. };
  149. struct lcnphy_iq_est {
  150. u32 iq_prod;
  151. u32 i_pwr;
  152. u32 q_pwr;
  153. };
  154. struct lcnphy_sfo_cfg {
  155. u16 ptcentreTs20;
  156. u16 ptcentreFactor;
  157. };
  158. enum lcnphy_papd_cal_type {
  159. LCNPHY_PAPD_CAL_CW,
  160. LCNPHY_PAPD_CAL_OFDM
  161. };
  162. typedef u16 iqcal_gain_params_lcnphy[9];
  163. static const iqcal_gain_params_lcnphy tbl_iqcal_gainparams_lcnphy_2G[] = {
  164. {0, 0, 0, 0, 0, 0, 0, 0, 0},
  165. };
  166. static const iqcal_gain_params_lcnphy *tbl_iqcal_gainparams_lcnphy[1] = {
  167. tbl_iqcal_gainparams_lcnphy_2G,
  168. };
  169. static const u16 iqcal_gainparams_numgains_lcnphy[1] = {
  170. ARRAY_SIZE(tbl_iqcal_gainparams_lcnphy_2G),
  171. };
  172. static const struct lcnphy_sfo_cfg lcnphy_sfo_cfg[] = {
  173. {965, 1087},
  174. {967, 1085},
  175. {969, 1082},
  176. {971, 1080},
  177. {973, 1078},
  178. {975, 1076},
  179. {977, 1073},
  180. {979, 1071},
  181. {981, 1069},
  182. {983, 1067},
  183. {985, 1065},
  184. {987, 1063},
  185. {989, 1060},
  186. {994, 1055}
  187. };
  188. static const
  189. u16 lcnphy_iqcal_loft_gainladder[] = {
  190. ((2 << 8) | 0),
  191. ((3 << 8) | 0),
  192. ((4 << 8) | 0),
  193. ((6 << 8) | 0),
  194. ((8 << 8) | 0),
  195. ((11 << 8) | 0),
  196. ((16 << 8) | 0),
  197. ((16 << 8) | 1),
  198. ((16 << 8) | 2),
  199. ((16 << 8) | 3),
  200. ((16 << 8) | 4),
  201. ((16 << 8) | 5),
  202. ((16 << 8) | 6),
  203. ((16 << 8) | 7),
  204. ((23 << 8) | 7),
  205. ((32 << 8) | 7),
  206. ((45 << 8) | 7),
  207. ((64 << 8) | 7),
  208. ((91 << 8) | 7),
  209. ((128 << 8) | 7)
  210. };
  211. static const
  212. u16 lcnphy_iqcal_ir_gainladder[] = {
  213. ((1 << 8) | 0),
  214. ((2 << 8) | 0),
  215. ((4 << 8) | 0),
  216. ((6 << 8) | 0),
  217. ((8 << 8) | 0),
  218. ((11 << 8) | 0),
  219. ((16 << 8) | 0),
  220. ((23 << 8) | 0),
  221. ((32 << 8) | 0),
  222. ((45 << 8) | 0),
  223. ((64 << 8) | 0),
  224. ((64 << 8) | 1),
  225. ((64 << 8) | 2),
  226. ((64 << 8) | 3),
  227. ((64 << 8) | 4),
  228. ((64 << 8) | 5),
  229. ((64 << 8) | 6),
  230. ((64 << 8) | 7),
  231. ((91 << 8) | 7),
  232. ((128 << 8) | 7)
  233. };
  234. static const
  235. struct lcnphy_spb_tone lcnphy_spb_tone_3750[] = {
  236. {88, 0},
  237. {73, 49},
  238. {34, 81},
  239. {-17, 86},
  240. {-62, 62},
  241. {-86, 17},
  242. {-81, -34},
  243. {-49, -73},
  244. {0, -88},
  245. {49, -73},
  246. {81, -34},
  247. {86, 17},
  248. {62, 62},
  249. {17, 86},
  250. {-34, 81},
  251. {-73, 49},
  252. {-88, 0},
  253. {-73, -49},
  254. {-34, -81},
  255. {17, -86},
  256. {62, -62},
  257. {86, -17},
  258. {81, 34},
  259. {49, 73},
  260. {0, 88},
  261. {-49, 73},
  262. {-81, 34},
  263. {-86, -17},
  264. {-62, -62},
  265. {-17, -86},
  266. {34, -81},
  267. {73, -49},
  268. };
  269. static const
  270. u16 iqlo_loopback_rf_regs[20] = {
  271. RADIO_2064_REG036,
  272. RADIO_2064_REG11A,
  273. RADIO_2064_REG03A,
  274. RADIO_2064_REG025,
  275. RADIO_2064_REG028,
  276. RADIO_2064_REG005,
  277. RADIO_2064_REG112,
  278. RADIO_2064_REG0FF,
  279. RADIO_2064_REG11F,
  280. RADIO_2064_REG00B,
  281. RADIO_2064_REG113,
  282. RADIO_2064_REG007,
  283. RADIO_2064_REG0FC,
  284. RADIO_2064_REG0FD,
  285. RADIO_2064_REG012,
  286. RADIO_2064_REG057,
  287. RADIO_2064_REG059,
  288. RADIO_2064_REG05C,
  289. RADIO_2064_REG078,
  290. RADIO_2064_REG092,
  291. };
  292. static const
  293. u16 tempsense_phy_regs[14] = {
  294. 0x503,
  295. 0x4a4,
  296. 0x4d0,
  297. 0x4d9,
  298. 0x4da,
  299. 0x4a6,
  300. 0x938,
  301. 0x939,
  302. 0x4d8,
  303. 0x4d0,
  304. 0x4d7,
  305. 0x4a5,
  306. 0x40d,
  307. 0x4a2,
  308. };
  309. static const
  310. u16 rxiq_cal_rf_reg[11] = {
  311. RADIO_2064_REG098,
  312. RADIO_2064_REG116,
  313. RADIO_2064_REG12C,
  314. RADIO_2064_REG06A,
  315. RADIO_2064_REG00B,
  316. RADIO_2064_REG01B,
  317. RADIO_2064_REG113,
  318. RADIO_2064_REG01D,
  319. RADIO_2064_REG114,
  320. RADIO_2064_REG02E,
  321. RADIO_2064_REG12A,
  322. };
  323. static const
  324. struct lcnphy_rx_iqcomp lcnphy_rx_iqcomp_table_rev0[] = {
  325. {1, 0, 0},
  326. {2, 0, 0},
  327. {3, 0, 0},
  328. {4, 0, 0},
  329. {5, 0, 0},
  330. {6, 0, 0},
  331. {7, 0, 0},
  332. {8, 0, 0},
  333. {9, 0, 0},
  334. {10, 0, 0},
  335. {11, 0, 0},
  336. {12, 0, 0},
  337. {13, 0, 0},
  338. {14, 0, 0},
  339. {34, 0, 0},
  340. {38, 0, 0},
  341. {42, 0, 0},
  342. {46, 0, 0},
  343. {36, 0, 0},
  344. {40, 0, 0},
  345. {44, 0, 0},
  346. {48, 0, 0},
  347. {52, 0, 0},
  348. {56, 0, 0},
  349. {60, 0, 0},
  350. {64, 0, 0},
  351. {100, 0, 0},
  352. {104, 0, 0},
  353. {108, 0, 0},
  354. {112, 0, 0},
  355. {116, 0, 0},
  356. {120, 0, 0},
  357. {124, 0, 0},
  358. {128, 0, 0},
  359. {132, 0, 0},
  360. {136, 0, 0},
  361. {140, 0, 0},
  362. {149, 0, 0},
  363. {153, 0, 0},
  364. {157, 0, 0},
  365. {161, 0, 0},
  366. {165, 0, 0},
  367. {184, 0, 0},
  368. {188, 0, 0},
  369. {192, 0, 0},
  370. {196, 0, 0},
  371. {200, 0, 0},
  372. {204, 0, 0},
  373. {208, 0, 0},
  374. {212, 0, 0},
  375. {216, 0, 0},
  376. };
  377. static const u32 lcnphy_23bitgaincode_table[] = {
  378. 0x200100,
  379. 0x200200,
  380. 0x200004,
  381. 0x200014,
  382. 0x200024,
  383. 0x200034,
  384. 0x200134,
  385. 0x200234,
  386. 0x200334,
  387. 0x200434,
  388. 0x200037,
  389. 0x200137,
  390. 0x200237,
  391. 0x200337,
  392. 0x200437,
  393. 0x000035,
  394. 0x000135,
  395. 0x000235,
  396. 0x000037,
  397. 0x000137,
  398. 0x000237,
  399. 0x000337,
  400. 0x00013f,
  401. 0x00023f,
  402. 0x00033f,
  403. 0x00034f,
  404. 0x00044f,
  405. 0x00144f,
  406. 0x00244f,
  407. 0x00254f,
  408. 0x00354f,
  409. 0x00454f,
  410. 0x00464f,
  411. 0x01464f,
  412. 0x02464f,
  413. 0x03464f,
  414. 0x04464f,
  415. };
  416. static const s8 lcnphy_gain_table[] = {
  417. -16,
  418. -13,
  419. 10,
  420. 7,
  421. 4,
  422. 0,
  423. 3,
  424. 6,
  425. 9,
  426. 12,
  427. 15,
  428. 18,
  429. 21,
  430. 24,
  431. 27,
  432. 30,
  433. 33,
  434. 36,
  435. 39,
  436. 42,
  437. 45,
  438. 48,
  439. 50,
  440. 53,
  441. 56,
  442. 59,
  443. 62,
  444. 65,
  445. 68,
  446. 71,
  447. 74,
  448. 77,
  449. 80,
  450. 83,
  451. 86,
  452. 89,
  453. 92,
  454. };
  455. static const s8 lcnphy_gain_index_offset_for_rssi[] = {
  456. 7,
  457. 7,
  458. 7,
  459. 7,
  460. 7,
  461. 7,
  462. 7,
  463. 8,
  464. 7,
  465. 7,
  466. 6,
  467. 7,
  468. 7,
  469. 4,
  470. 4,
  471. 4,
  472. 4,
  473. 4,
  474. 4,
  475. 4,
  476. 4,
  477. 3,
  478. 3,
  479. 3,
  480. 3,
  481. 3,
  482. 3,
  483. 4,
  484. 2,
  485. 2,
  486. 2,
  487. 2,
  488. 2,
  489. 2,
  490. -1,
  491. -2,
  492. -2,
  493. -2
  494. };
  495. struct chan_info_2064_lcnphy {
  496. uint chan;
  497. uint freq;
  498. u8 logen_buftune;
  499. u8 logen_rccr_tx;
  500. u8 txrf_mix_tune_ctrl;
  501. u8 pa_input_tune_g;
  502. u8 logen_rccr_rx;
  503. u8 pa_rxrf_lna1_freq_tune;
  504. u8 pa_rxrf_lna2_freq_tune;
  505. u8 rxrf_rxrf_spare1;
  506. };
  507. static const struct chan_info_2064_lcnphy chan_info_2064_lcnphy[] = {
  508. {1, 2412, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  509. {2, 2417, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  510. {3, 2422, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  511. {4, 2427, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  512. {5, 2432, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  513. {6, 2437, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  514. {7, 2442, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  515. {8, 2447, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  516. {9, 2452, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  517. {10, 2457, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  518. {11, 2462, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  519. {12, 2467, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  520. {13, 2472, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  521. {14, 2484, 0x0B, 0x0A, 0x00, 0x07, 0x0A, 0x88, 0x88, 0x80},
  522. };
  523. static const struct lcnphy_radio_regs lcnphy_radio_regs_2064[] = {
  524. {0x00, 0, 0, 0, 0},
  525. {0x01, 0x64, 0x64, 0, 0},
  526. {0x02, 0x20, 0x20, 0, 0},
  527. {0x03, 0x66, 0x66, 0, 0},
  528. {0x04, 0xf8, 0xf8, 0, 0},
  529. {0x05, 0, 0, 0, 0},
  530. {0x06, 0x10, 0x10, 0, 0},
  531. {0x07, 0, 0, 0, 0},
  532. {0x08, 0, 0, 0, 0},
  533. {0x09, 0, 0, 0, 0},
  534. {0x0A, 0x37, 0x37, 0, 0},
  535. {0x0B, 0x6, 0x6, 0, 0},
  536. {0x0C, 0x55, 0x55, 0, 0},
  537. {0x0D, 0x8b, 0x8b, 0, 0},
  538. {0x0E, 0, 0, 0, 0},
  539. {0x0F, 0x5, 0x5, 0, 0},
  540. {0x10, 0, 0, 0, 0},
  541. {0x11, 0xe, 0xe, 0, 0},
  542. {0x12, 0, 0, 0, 0},
  543. {0x13, 0xb, 0xb, 0, 0},
  544. {0x14, 0x2, 0x2, 0, 0},
  545. {0x15, 0x12, 0x12, 0, 0},
  546. {0x16, 0x12, 0x12, 0, 0},
  547. {0x17, 0xc, 0xc, 0, 0},
  548. {0x18, 0xc, 0xc, 0, 0},
  549. {0x19, 0xc, 0xc, 0, 0},
  550. {0x1A, 0x8, 0x8, 0, 0},
  551. {0x1B, 0x2, 0x2, 0, 0},
  552. {0x1C, 0, 0, 0, 0},
  553. {0x1D, 0x1, 0x1, 0, 0},
  554. {0x1E, 0x12, 0x12, 0, 0},
  555. {0x1F, 0x6e, 0x6e, 0, 0},
  556. {0x20, 0x2, 0x2, 0, 0},
  557. {0x21, 0x23, 0x23, 0, 0},
  558. {0x22, 0x8, 0x8, 0, 0},
  559. {0x23, 0, 0, 0, 0},
  560. {0x24, 0, 0, 0, 0},
  561. {0x25, 0xc, 0xc, 0, 0},
  562. {0x26, 0x33, 0x33, 0, 0},
  563. {0x27, 0x55, 0x55, 0, 0},
  564. {0x28, 0, 0, 0, 0},
  565. {0x29, 0x30, 0x30, 0, 0},
  566. {0x2A, 0xb, 0xb, 0, 0},
  567. {0x2B, 0x1b, 0x1b, 0, 0},
  568. {0x2C, 0x3, 0x3, 0, 0},
  569. {0x2D, 0x1b, 0x1b, 0, 0},
  570. {0x2E, 0, 0, 0, 0},
  571. {0x2F, 0x20, 0x20, 0, 0},
  572. {0x30, 0xa, 0xa, 0, 0},
  573. {0x31, 0, 0, 0, 0},
  574. {0x32, 0x62, 0x62, 0, 0},
  575. {0x33, 0x19, 0x19, 0, 0},
  576. {0x34, 0x33, 0x33, 0, 0},
  577. {0x35, 0x77, 0x77, 0, 0},
  578. {0x36, 0, 0, 0, 0},
  579. {0x37, 0x70, 0x70, 0, 0},
  580. {0x38, 0x3, 0x3, 0, 0},
  581. {0x39, 0xf, 0xf, 0, 0},
  582. {0x3A, 0x6, 0x6, 0, 0},
  583. {0x3B, 0xcf, 0xcf, 0, 0},
  584. {0x3C, 0x1a, 0x1a, 0, 0},
  585. {0x3D, 0x6, 0x6, 0, 0},
  586. {0x3E, 0x42, 0x42, 0, 0},
  587. {0x3F, 0, 0, 0, 0},
  588. {0x40, 0xfb, 0xfb, 0, 0},
  589. {0x41, 0x9a, 0x9a, 0, 0},
  590. {0x42, 0x7a, 0x7a, 0, 0},
  591. {0x43, 0x29, 0x29, 0, 0},
  592. {0x44, 0, 0, 0, 0},
  593. {0x45, 0x8, 0x8, 0, 0},
  594. {0x46, 0xce, 0xce, 0, 0},
  595. {0x47, 0x27, 0x27, 0, 0},
  596. {0x48, 0x62, 0x62, 0, 0},
  597. {0x49, 0x6, 0x6, 0, 0},
  598. {0x4A, 0x58, 0x58, 0, 0},
  599. {0x4B, 0xf7, 0xf7, 0, 0},
  600. {0x4C, 0, 0, 0, 0},
  601. {0x4D, 0xb3, 0xb3, 0, 0},
  602. {0x4E, 0, 0, 0, 0},
  603. {0x4F, 0x2, 0x2, 0, 0},
  604. {0x50, 0, 0, 0, 0},
  605. {0x51, 0x9, 0x9, 0, 0},
  606. {0x52, 0x5, 0x5, 0, 0},
  607. {0x53, 0x17, 0x17, 0, 0},
  608. {0x54, 0x38, 0x38, 0, 0},
  609. {0x55, 0, 0, 0, 0},
  610. {0x56, 0, 0, 0, 0},
  611. {0x57, 0xb, 0xb, 0, 0},
  612. {0x58, 0, 0, 0, 0},
  613. {0x59, 0, 0, 0, 0},
  614. {0x5A, 0, 0, 0, 0},
  615. {0x5B, 0, 0, 0, 0},
  616. {0x5C, 0, 0, 0, 0},
  617. {0x5D, 0, 0, 0, 0},
  618. {0x5E, 0x88, 0x88, 0, 0},
  619. {0x5F, 0xcc, 0xcc, 0, 0},
  620. {0x60, 0x74, 0x74, 0, 0},
  621. {0x61, 0x74, 0x74, 0, 0},
  622. {0x62, 0x74, 0x74, 0, 0},
  623. {0x63, 0x44, 0x44, 0, 0},
  624. {0x64, 0x77, 0x77, 0, 0},
  625. {0x65, 0x44, 0x44, 0, 0},
  626. {0x66, 0x77, 0x77, 0, 0},
  627. {0x67, 0x55, 0x55, 0, 0},
  628. {0x68, 0x77, 0x77, 0, 0},
  629. {0x69, 0x77, 0x77, 0, 0},
  630. {0x6A, 0, 0, 0, 0},
  631. {0x6B, 0x7f, 0x7f, 0, 0},
  632. {0x6C, 0x8, 0x8, 0, 0},
  633. {0x6D, 0, 0, 0, 0},
  634. {0x6E, 0x88, 0x88, 0, 0},
  635. {0x6F, 0x66, 0x66, 0, 0},
  636. {0x70, 0x66, 0x66, 0, 0},
  637. {0x71, 0x28, 0x28, 0, 0},
  638. {0x72, 0x55, 0x55, 0, 0},
  639. {0x73, 0x4, 0x4, 0, 0},
  640. {0x74, 0, 0, 0, 0},
  641. {0x75, 0, 0, 0, 0},
  642. {0x76, 0, 0, 0, 0},
  643. {0x77, 0x1, 0x1, 0, 0},
  644. {0x78, 0xd6, 0xd6, 0, 0},
  645. {0x79, 0, 0, 0, 0},
  646. {0x7A, 0, 0, 0, 0},
  647. {0x7B, 0, 0, 0, 0},
  648. {0x7C, 0, 0, 0, 0},
  649. {0x7D, 0, 0, 0, 0},
  650. {0x7E, 0, 0, 0, 0},
  651. {0x7F, 0, 0, 0, 0},
  652. {0x80, 0, 0, 0, 0},
  653. {0x81, 0, 0, 0, 0},
  654. {0x82, 0, 0, 0, 0},
  655. {0x83, 0xb4, 0xb4, 0, 0},
  656. {0x84, 0x1, 0x1, 0, 0},
  657. {0x85, 0x20, 0x20, 0, 0},
  658. {0x86, 0x5, 0x5, 0, 0},
  659. {0x87, 0xff, 0xff, 0, 0},
  660. {0x88, 0x7, 0x7, 0, 0},
  661. {0x89, 0x77, 0x77, 0, 0},
  662. {0x8A, 0x77, 0x77, 0, 0},
  663. {0x8B, 0x77, 0x77, 0, 0},
  664. {0x8C, 0x77, 0x77, 0, 0},
  665. {0x8D, 0x8, 0x8, 0, 0},
  666. {0x8E, 0xa, 0xa, 0, 0},
  667. {0x8F, 0x8, 0x8, 0, 0},
  668. {0x90, 0x18, 0x18, 0, 0},
  669. {0x91, 0x5, 0x5, 0, 0},
  670. {0x92, 0x1f, 0x1f, 0, 0},
  671. {0x93, 0x10, 0x10, 0, 0},
  672. {0x94, 0x3, 0x3, 0, 0},
  673. {0x95, 0, 0, 0, 0},
  674. {0x96, 0, 0, 0, 0},
  675. {0x97, 0xaa, 0xaa, 0, 0},
  676. {0x98, 0, 0, 0, 0},
  677. {0x99, 0x23, 0x23, 0, 0},
  678. {0x9A, 0x7, 0x7, 0, 0},
  679. {0x9B, 0xf, 0xf, 0, 0},
  680. {0x9C, 0x10, 0x10, 0, 0},
  681. {0x9D, 0x3, 0x3, 0, 0},
  682. {0x9E, 0x4, 0x4, 0, 0},
  683. {0x9F, 0x20, 0x20, 0, 0},
  684. {0xA0, 0, 0, 0, 0},
  685. {0xA1, 0, 0, 0, 0},
  686. {0xA2, 0, 0, 0, 0},
  687. {0xA3, 0, 0, 0, 0},
  688. {0xA4, 0x1, 0x1, 0, 0},
  689. {0xA5, 0x77, 0x77, 0, 0},
  690. {0xA6, 0x77, 0x77, 0, 0},
  691. {0xA7, 0x77, 0x77, 0, 0},
  692. {0xA8, 0x77, 0x77, 0, 0},
  693. {0xA9, 0x8c, 0x8c, 0, 0},
  694. {0xAA, 0x88, 0x88, 0, 0},
  695. {0xAB, 0x78, 0x78, 0, 0},
  696. {0xAC, 0x57, 0x57, 0, 0},
  697. {0xAD, 0x88, 0x88, 0, 0},
  698. {0xAE, 0, 0, 0, 0},
  699. {0xAF, 0x8, 0x8, 0, 0},
  700. {0xB0, 0x88, 0x88, 0, 0},
  701. {0xB1, 0, 0, 0, 0},
  702. {0xB2, 0x1b, 0x1b, 0, 0},
  703. {0xB3, 0x3, 0x3, 0, 0},
  704. {0xB4, 0x24, 0x24, 0, 0},
  705. {0xB5, 0x3, 0x3, 0, 0},
  706. {0xB6, 0x1b, 0x1b, 0, 0},
  707. {0xB7, 0x24, 0x24, 0, 0},
  708. {0xB8, 0x3, 0x3, 0, 0},
  709. {0xB9, 0, 0, 0, 0},
  710. {0xBA, 0xaa, 0xaa, 0, 0},
  711. {0xBB, 0, 0, 0, 0},
  712. {0xBC, 0x4, 0x4, 0, 0},
  713. {0xBD, 0, 0, 0, 0},
  714. {0xBE, 0x8, 0x8, 0, 0},
  715. {0xBF, 0x11, 0x11, 0, 0},
  716. {0xC0, 0, 0, 0, 0},
  717. {0xC1, 0, 0, 0, 0},
  718. {0xC2, 0x62, 0x62, 0, 0},
  719. {0xC3, 0x1e, 0x1e, 0, 0},
  720. {0xC4, 0x33, 0x33, 0, 0},
  721. {0xC5, 0x37, 0x37, 0, 0},
  722. {0xC6, 0, 0, 0, 0},
  723. {0xC7, 0x70, 0x70, 0, 0},
  724. {0xC8, 0x1e, 0x1e, 0, 0},
  725. {0xC9, 0x6, 0x6, 0, 0},
  726. {0xCA, 0x4, 0x4, 0, 0},
  727. {0xCB, 0x2f, 0x2f, 0, 0},
  728. {0xCC, 0xf, 0xf, 0, 0},
  729. {0xCD, 0, 0, 0, 0},
  730. {0xCE, 0xff, 0xff, 0, 0},
  731. {0xCF, 0x8, 0x8, 0, 0},
  732. {0xD0, 0x3f, 0x3f, 0, 0},
  733. {0xD1, 0x3f, 0x3f, 0, 0},
  734. {0xD2, 0x3f, 0x3f, 0, 0},
  735. {0xD3, 0, 0, 0, 0},
  736. {0xD4, 0, 0, 0, 0},
  737. {0xD5, 0, 0, 0, 0},
  738. {0xD6, 0xcc, 0xcc, 0, 0},
  739. {0xD7, 0, 0, 0, 0},
  740. {0xD8, 0x8, 0x8, 0, 0},
  741. {0xD9, 0x8, 0x8, 0, 0},
  742. {0xDA, 0x8, 0x8, 0, 0},
  743. {0xDB, 0x11, 0x11, 0, 0},
  744. {0xDC, 0, 0, 0, 0},
  745. {0xDD, 0x87, 0x87, 0, 0},
  746. {0xDE, 0x88, 0x88, 0, 0},
  747. {0xDF, 0x8, 0x8, 0, 0},
  748. {0xE0, 0x8, 0x8, 0, 0},
  749. {0xE1, 0x8, 0x8, 0, 0},
  750. {0xE2, 0, 0, 0, 0},
  751. {0xE3, 0, 0, 0, 0},
  752. {0xE4, 0, 0, 0, 0},
  753. {0xE5, 0xf5, 0xf5, 0, 0},
  754. {0xE6, 0x30, 0x30, 0, 0},
  755. {0xE7, 0x1, 0x1, 0, 0},
  756. {0xE8, 0, 0, 0, 0},
  757. {0xE9, 0xff, 0xff, 0, 0},
  758. {0xEA, 0, 0, 0, 0},
  759. {0xEB, 0, 0, 0, 0},
  760. {0xEC, 0x22, 0x22, 0, 0},
  761. {0xED, 0, 0, 0, 0},
  762. {0xEE, 0, 0, 0, 0},
  763. {0xEF, 0, 0, 0, 0},
  764. {0xF0, 0x3, 0x3, 0, 0},
  765. {0xF1, 0x1, 0x1, 0, 0},
  766. {0xF2, 0, 0, 0, 0},
  767. {0xF3, 0, 0, 0, 0},
  768. {0xF4, 0, 0, 0, 0},
  769. {0xF5, 0, 0, 0, 0},
  770. {0xF6, 0, 0, 0, 0},
  771. {0xF7, 0x6, 0x6, 0, 0},
  772. {0xF8, 0, 0, 0, 0},
  773. {0xF9, 0, 0, 0, 0},
  774. {0xFA, 0x40, 0x40, 0, 0},
  775. {0xFB, 0, 0, 0, 0},
  776. {0xFC, 0x1, 0x1, 0, 0},
  777. {0xFD, 0x80, 0x80, 0, 0},
  778. {0xFE, 0x2, 0x2, 0, 0},
  779. {0xFF, 0x10, 0x10, 0, 0},
  780. {0x100, 0x2, 0x2, 0, 0},
  781. {0x101, 0x1e, 0x1e, 0, 0},
  782. {0x102, 0x1e, 0x1e, 0, 0},
  783. {0x103, 0, 0, 0, 0},
  784. {0x104, 0x1f, 0x1f, 0, 0},
  785. {0x105, 0, 0x8, 0, 1},
  786. {0x106, 0x2a, 0x2a, 0, 0},
  787. {0x107, 0xf, 0xf, 0, 0},
  788. {0x108, 0, 0, 0, 0},
  789. {0x109, 0, 0, 0, 0},
  790. {0x10A, 0, 0, 0, 0},
  791. {0x10B, 0, 0, 0, 0},
  792. {0x10C, 0, 0, 0, 0},
  793. {0x10D, 0, 0, 0, 0},
  794. {0x10E, 0, 0, 0, 0},
  795. {0x10F, 0, 0, 0, 0},
  796. {0x110, 0, 0, 0, 0},
  797. {0x111, 0, 0, 0, 0},
  798. {0x112, 0, 0, 0, 0},
  799. {0x113, 0, 0, 0, 0},
  800. {0x114, 0, 0, 0, 0},
  801. {0x115, 0, 0, 0, 0},
  802. {0x116, 0, 0, 0, 0},
  803. {0x117, 0, 0, 0, 0},
  804. {0x118, 0, 0, 0, 0},
  805. {0x119, 0, 0, 0, 0},
  806. {0x11A, 0, 0, 0, 0},
  807. {0x11B, 0, 0, 0, 0},
  808. {0x11C, 0x1, 0x1, 0, 0},
  809. {0x11D, 0, 0, 0, 0},
  810. {0x11E, 0, 0, 0, 0},
  811. {0x11F, 0, 0, 0, 0},
  812. {0x120, 0, 0, 0, 0},
  813. {0x121, 0, 0, 0, 0},
  814. {0x122, 0x80, 0x80, 0, 0},
  815. {0x123, 0, 0, 0, 0},
  816. {0x124, 0xf8, 0xf8, 0, 0},
  817. {0x125, 0, 0, 0, 0},
  818. {0x126, 0, 0, 0, 0},
  819. {0x127, 0, 0, 0, 0},
  820. {0x128, 0, 0, 0, 0},
  821. {0x129, 0, 0, 0, 0},
  822. {0x12A, 0, 0, 0, 0},
  823. {0x12B, 0, 0, 0, 0},
  824. {0x12C, 0, 0, 0, 0},
  825. {0x12D, 0, 0, 0, 0},
  826. {0x12E, 0, 0, 0, 0},
  827. {0x12F, 0, 0, 0, 0},
  828. {0x130, 0, 0, 0, 0},
  829. {0xFFFF, 0, 0, 0, 0}
  830. };
  831. #define LCNPHY_NUM_DIG_FILT_COEFFS 16
  832. #define LCNPHY_NUM_TX_DIG_FILTERS_CCK 13
  833. static const u16 LCNPHY_txdigfiltcoeffs_cck[LCNPHY_NUM_TX_DIG_FILTERS_CCK]
  834. [LCNPHY_NUM_DIG_FILT_COEFFS + 1] = {
  835. {0, 1, 415, 1874, 64, 128, 64, 792, 1656, 64, 128, 64, 778, 1582, 64,
  836. 128, 64,},
  837. {1, 1, 402, 1847, 259, 59, 259, 671, 1794, 68, 54, 68, 608, 1863, 93,
  838. 167, 93,},
  839. {2, 1, 415, 1874, 64, 128, 64, 792, 1656, 192, 384, 192, 778, 1582, 64,
  840. 128, 64,},
  841. {3, 1, 302, 1841, 129, 258, 129, 658, 1720, 205, 410, 205, 754, 1760,
  842. 170, 340, 170,},
  843. {20, 1, 360, 1884, 242, 1734, 242, 752, 1720, 205, 1845, 205, 767, 1760,
  844. 256, 185, 256,},
  845. {21, 1, 360, 1884, 149, 1874, 149, 752, 1720, 205, 1883, 205, 767, 1760,
  846. 256, 273, 256,},
  847. {22, 1, 360, 1884, 98, 1948, 98, 752, 1720, 205, 1924, 205, 767, 1760,
  848. 256, 352, 256,},
  849. {23, 1, 350, 1884, 116, 1966, 116, 752, 1720, 205, 2008, 205, 767, 1760,
  850. 128, 233, 128,},
  851. {24, 1, 325, 1884, 32, 40, 32, 756, 1720, 256, 471, 256, 766, 1760, 256,
  852. 1881, 256,},
  853. {25, 1, 299, 1884, 51, 64, 51, 736, 1720, 256, 471, 256, 765, 1760, 256,
  854. 1881, 256,},
  855. {26, 1, 277, 1943, 39, 117, 88, 637, 1838, 64, 192, 144, 614, 1864, 128,
  856. 384, 288,},
  857. {27, 1, 245, 1943, 49, 147, 110, 626, 1838, 256, 768, 576, 613, 1864,
  858. 128, 384, 288,},
  859. {30, 1, 302, 1841, 61, 122, 61, 658, 1720, 205, 410, 205, 754, 1760,
  860. 170, 340, 170,},
  861. };
  862. #define LCNPHY_NUM_TX_DIG_FILTERS_OFDM 3
  863. static const u16 LCNPHY_txdigfiltcoeffs_ofdm[LCNPHY_NUM_TX_DIG_FILTERS_OFDM]
  864. [LCNPHY_NUM_DIG_FILT_COEFFS + 1] = {
  865. {0, 0, 0xa2, 0x0, 0x100, 0x100, 0x0, 0x0, 0x0, 0x100, 0x0, 0x0,
  866. 0x278, 0xfea0, 0x80, 0x100, 0x80,},
  867. {1, 0, 374, 0xFF79, 16, 32, 16, 799, 0xFE74, 50, 32, 50,
  868. 750, 0xFE2B, 212, 0xFFCE, 212,},
  869. {2, 0, 375, 0xFF16, 37, 76, 37, 799, 0xFE74, 32, 20, 32, 748,
  870. 0xFEF2, 128, 0xFFE2, 128}
  871. };
  872. #define wlc_lcnphy_set_start_tx_pwr_idx(pi, idx) \
  873. mod_phy_reg(pi, 0x4a4, \
  874. (0x1ff << 0), \
  875. (u16)(idx) << 0)
  876. #define wlc_lcnphy_set_tx_pwr_npt(pi, npt) \
  877. mod_phy_reg(pi, 0x4a5, \
  878. (0x7 << 8), \
  879. (u16)(npt) << 8)
  880. #define wlc_lcnphy_get_tx_pwr_ctrl(pi) \
  881. (read_phy_reg((pi), 0x4a4) & \
  882. ((0x1 << 15) | \
  883. (0x1 << 14) | \
  884. (0x1 << 13)))
  885. #define wlc_lcnphy_get_tx_pwr_npt(pi) \
  886. ((read_phy_reg(pi, 0x4a5) & \
  887. (0x7 << 8)) >> \
  888. 8)
  889. #define wlc_lcnphy_get_current_tx_pwr_idx_if_pwrctrl_on(pi) \
  890. (read_phy_reg(pi, 0x473) & 0x1ff)
  891. #define wlc_lcnphy_get_target_tx_pwr(pi) \
  892. ((read_phy_reg(pi, 0x4a7) & \
  893. (0xff << 0)) >> \
  894. 0)
  895. #define wlc_lcnphy_set_target_tx_pwr(pi, target) \
  896. mod_phy_reg(pi, 0x4a7, \
  897. (0xff << 0), \
  898. (u16)(target) << 0)
  899. #define wlc_radio_2064_rcal_done(pi) \
  900. (0 != (read_radio_reg(pi, RADIO_2064_REG05C) & 0x20))
  901. #define tempsense_done(pi) \
  902. (0x8000 == (read_phy_reg(pi, 0x476) & 0x8000))
  903. #define LCNPHY_IQLOCC_READ(val) \
  904. ((u8)(-(s8)(((val) & 0xf0) >> 4) + (s8)((val) & 0x0f)))
  905. #define FIXED_TXPWR 78
  906. #define LCNPHY_TEMPSENSE(val) ((s16)((val > 255) ? (val - 512) : val))
  907. void wlc_lcnphy_write_table(struct brcms_phy *pi, const struct phytbl_info *pti)
  908. {
  909. wlc_phy_write_table(pi, pti, 0x455, 0x457, 0x456);
  910. }
  911. void wlc_lcnphy_read_table(struct brcms_phy *pi, struct phytbl_info *pti)
  912. {
  913. wlc_phy_read_table(pi, pti, 0x455, 0x457, 0x456);
  914. }
  915. static void
  916. wlc_lcnphy_common_read_table(struct brcms_phy *pi, u32 tbl_id,
  917. const u16 *tbl_ptr, u32 tbl_len,
  918. u32 tbl_width, u32 tbl_offset)
  919. {
  920. struct phytbl_info tab;
  921. tab.tbl_id = tbl_id;
  922. tab.tbl_ptr = tbl_ptr;
  923. tab.tbl_len = tbl_len;
  924. tab.tbl_width = tbl_width;
  925. tab.tbl_offset = tbl_offset;
  926. wlc_lcnphy_read_table(pi, &tab);
  927. }
  928. static void
  929. wlc_lcnphy_common_write_table(struct brcms_phy *pi, u32 tbl_id,
  930. const u16 *tbl_ptr, u32 tbl_len,
  931. u32 tbl_width, u32 tbl_offset)
  932. {
  933. struct phytbl_info tab;
  934. tab.tbl_id = tbl_id;
  935. tab.tbl_ptr = tbl_ptr;
  936. tab.tbl_len = tbl_len;
  937. tab.tbl_width = tbl_width;
  938. tab.tbl_offset = tbl_offset;
  939. wlc_lcnphy_write_table(pi, &tab);
  940. }
  941. static u32
  942. wlc_lcnphy_qdiv_roundup(u32 dividend, u32 divisor, u8 precision)
  943. {
  944. u32 quotient, remainder, roundup, rbit;
  945. quotient = dividend / divisor;
  946. remainder = dividend % divisor;
  947. rbit = divisor & 1;
  948. roundup = (divisor >> 1) + rbit;
  949. while (precision--) {
  950. quotient <<= 1;
  951. if (remainder >= roundup) {
  952. quotient++;
  953. remainder = ((remainder - roundup) << 1) + rbit;
  954. } else {
  955. remainder <<= 1;
  956. }
  957. }
  958. if (remainder >= roundup)
  959. quotient++;
  960. return quotient;
  961. }
  962. static int wlc_lcnphy_calc_floor(s16 coeff_x, int type)
  963. {
  964. int k;
  965. k = 0;
  966. if (type == 0) {
  967. if (coeff_x < 0)
  968. k = (coeff_x - 1) / 2;
  969. else
  970. k = coeff_x / 2;
  971. }
  972. if (type == 1) {
  973. if ((coeff_x + 1) < 0)
  974. k = (coeff_x) / 2;
  975. else
  976. k = (coeff_x + 1) / 2;
  977. }
  978. return k;
  979. }
  980. static void
  981. wlc_lcnphy_get_tx_gain(struct brcms_phy *pi, struct lcnphy_txgains *gains)
  982. {
  983. u16 dac_gain, rfgain0, rfgain1;
  984. dac_gain = read_phy_reg(pi, 0x439) >> 0;
  985. gains->dac_gain = (dac_gain & 0x380) >> 7;
  986. rfgain0 = (read_phy_reg(pi, 0x4b5) & (0xffff << 0)) >> 0;
  987. rfgain1 = (read_phy_reg(pi, 0x4fb) & (0x7fff << 0)) >> 0;
  988. gains->gm_gain = rfgain0 & 0xff;
  989. gains->pga_gain = (rfgain0 >> 8) & 0xff;
  990. gains->pad_gain = rfgain1 & 0xff;
  991. }
  992. static void wlc_lcnphy_set_dac_gain(struct brcms_phy *pi, u16 dac_gain)
  993. {
  994. u16 dac_ctrl;
  995. dac_ctrl = (read_phy_reg(pi, 0x439) >> 0);
  996. dac_ctrl = dac_ctrl & 0xc7f;
  997. dac_ctrl = dac_ctrl | (dac_gain << 7);
  998. mod_phy_reg(pi, 0x439, (0xfff << 0), (dac_ctrl) << 0);
  999. }
  1000. static void wlc_lcnphy_set_tx_gain_override(struct brcms_phy *pi, bool bEnable)
  1001. {
  1002. u16 bit = bEnable ? 1 : 0;
  1003. mod_phy_reg(pi, 0x4b0, (0x1 << 7), bit << 7);
  1004. mod_phy_reg(pi, 0x4b0, (0x1 << 14), bit << 14);
  1005. mod_phy_reg(pi, 0x43b, (0x1 << 6), bit << 6);
  1006. }
  1007. static void
  1008. wlc_lcnphy_rx_gain_override_enable(struct brcms_phy *pi, bool enable)
  1009. {
  1010. u16 ebit = enable ? 1 : 0;
  1011. mod_phy_reg(pi, 0x4b0, (0x1 << 8), ebit << 8);
  1012. mod_phy_reg(pi, 0x44c, (0x1 << 0), ebit << 0);
  1013. if (LCNREV_LT(pi->pubpi.phy_rev, 2)) {
  1014. mod_phy_reg(pi, 0x44c, (0x1 << 4), ebit << 4);
  1015. mod_phy_reg(pi, 0x44c, (0x1 << 6), ebit << 6);
  1016. mod_phy_reg(pi, 0x4b0, (0x1 << 5), ebit << 5);
  1017. mod_phy_reg(pi, 0x4b0, (0x1 << 6), ebit << 6);
  1018. } else {
  1019. mod_phy_reg(pi, 0x4b0, (0x1 << 12), ebit << 12);
  1020. mod_phy_reg(pi, 0x4b0, (0x1 << 13), ebit << 13);
  1021. mod_phy_reg(pi, 0x4b0, (0x1 << 5), ebit << 5);
  1022. }
  1023. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  1024. mod_phy_reg(pi, 0x4b0, (0x1 << 10), ebit << 10);
  1025. mod_phy_reg(pi, 0x4e5, (0x1 << 3), ebit << 3);
  1026. }
  1027. }
  1028. static void
  1029. wlc_lcnphy_set_rx_gain_by_distribution(struct brcms_phy *pi,
  1030. u16 trsw,
  1031. u16 ext_lna,
  1032. u16 biq2,
  1033. u16 biq1,
  1034. u16 tia, u16 lna2, u16 lna1)
  1035. {
  1036. u16 gain0_15, gain16_19;
  1037. gain16_19 = biq2 & 0xf;
  1038. gain0_15 = ((biq1 & 0xf) << 12) |
  1039. ((tia & 0xf) << 8) |
  1040. ((lna2 & 0x3) << 6) |
  1041. ((lna2 & 0x3) << 4) |
  1042. ((lna1 & 0x3) << 2) |
  1043. ((lna1 & 0x3) << 0);
  1044. mod_phy_reg(pi, 0x4b6, (0xffff << 0), gain0_15 << 0);
  1045. mod_phy_reg(pi, 0x4b7, (0xf << 0), gain16_19 << 0);
  1046. mod_phy_reg(pi, 0x4b1, (0x3 << 11), lna1 << 11);
  1047. if (LCNREV_LT(pi->pubpi.phy_rev, 2)) {
  1048. mod_phy_reg(pi, 0x4b1, (0x1 << 9), ext_lna << 9);
  1049. mod_phy_reg(pi, 0x4b1, (0x1 << 10), ext_lna << 10);
  1050. } else {
  1051. mod_phy_reg(pi, 0x4b1, (0x1 << 10), 0 << 10);
  1052. mod_phy_reg(pi, 0x4b1, (0x1 << 15), 0 << 15);
  1053. mod_phy_reg(pi, 0x4b1, (0x1 << 9), ext_lna << 9);
  1054. }
  1055. mod_phy_reg(pi, 0x44d, (0x1 << 0), (!trsw) << 0);
  1056. }
  1057. static void wlc_lcnphy_set_trsw_override(struct brcms_phy *pi, bool tx, bool rx)
  1058. {
  1059. mod_phy_reg(pi, 0x44d,
  1060. (0x1 << 1) |
  1061. (0x1 << 0), (tx ? (0x1 << 1) : 0) | (rx ? (0x1 << 0) : 0));
  1062. or_phy_reg(pi, 0x44c, (0x1 << 1) | (0x1 << 0));
  1063. }
  1064. static void wlc_lcnphy_clear_trsw_override(struct brcms_phy *pi)
  1065. {
  1066. and_phy_reg(pi, 0x44c, (u16) ~((0x1 << 1) | (0x1 << 0)));
  1067. }
  1068. static void wlc_lcnphy_set_rx_iq_comp(struct brcms_phy *pi, u16 a, u16 b)
  1069. {
  1070. mod_phy_reg(pi, 0x645, (0x3ff << 0), (a) << 0);
  1071. mod_phy_reg(pi, 0x646, (0x3ff << 0), (b) << 0);
  1072. mod_phy_reg(pi, 0x647, (0x3ff << 0), (a) << 0);
  1073. mod_phy_reg(pi, 0x648, (0x3ff << 0), (b) << 0);
  1074. mod_phy_reg(pi, 0x649, (0x3ff << 0), (a) << 0);
  1075. mod_phy_reg(pi, 0x64a, (0x3ff << 0), (b) << 0);
  1076. }
  1077. static bool
  1078. wlc_lcnphy_rx_iq_est(struct brcms_phy *pi,
  1079. u16 num_samps,
  1080. u8 wait_time, struct lcnphy_iq_est *iq_est)
  1081. {
  1082. int wait_count = 0;
  1083. bool result = true;
  1084. u8 phybw40;
  1085. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  1086. mod_phy_reg(pi, 0x6da, (0x1 << 5), (1) << 5);
  1087. mod_phy_reg(pi, 0x410, (0x1 << 3), (0) << 3);
  1088. mod_phy_reg(pi, 0x482, (0xffff << 0), (num_samps) << 0);
  1089. mod_phy_reg(pi, 0x481, (0xff << 0), ((u16) wait_time) << 0);
  1090. mod_phy_reg(pi, 0x481, (0x1 << 8), (0) << 8);
  1091. mod_phy_reg(pi, 0x481, (0x1 << 9), (1) << 9);
  1092. while (read_phy_reg(pi, 0x481) & (0x1 << 9)) {
  1093. if (wait_count > (10 * 500)) {
  1094. result = false;
  1095. goto cleanup;
  1096. }
  1097. udelay(100);
  1098. wait_count++;
  1099. }
  1100. iq_est->iq_prod = ((u32) read_phy_reg(pi, 0x483) << 16) |
  1101. (u32) read_phy_reg(pi, 0x484);
  1102. iq_est->i_pwr = ((u32) read_phy_reg(pi, 0x485) << 16) |
  1103. (u32) read_phy_reg(pi, 0x486);
  1104. iq_est->q_pwr = ((u32) read_phy_reg(pi, 0x487) << 16) |
  1105. (u32) read_phy_reg(pi, 0x488);
  1106. cleanup:
  1107. mod_phy_reg(pi, 0x410, (0x1 << 3), (1) << 3);
  1108. mod_phy_reg(pi, 0x6da, (0x1 << 5), (0) << 5);
  1109. return result;
  1110. }
  1111. static bool wlc_lcnphy_calc_rx_iq_comp(struct brcms_phy *pi, u16 num_samps)
  1112. {
  1113. #define LCNPHY_MIN_RXIQ_PWR 2
  1114. bool result;
  1115. u16 a0_new, b0_new;
  1116. struct lcnphy_iq_est iq_est = { 0, 0, 0 };
  1117. s32 a, b, temp;
  1118. s16 iq_nbits, qq_nbits, arsh, brsh;
  1119. s32 iq;
  1120. u32 ii, qq;
  1121. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1122. a0_new = ((read_phy_reg(pi, 0x645) & (0x3ff << 0)) >> 0);
  1123. b0_new = ((read_phy_reg(pi, 0x646) & (0x3ff << 0)) >> 0);
  1124. mod_phy_reg(pi, 0x6d1, (0x1 << 2), (0) << 2);
  1125. mod_phy_reg(pi, 0x64b, (0x1 << 6), (1) << 6);
  1126. wlc_lcnphy_set_rx_iq_comp(pi, 0, 0);
  1127. result = wlc_lcnphy_rx_iq_est(pi, num_samps, 32, &iq_est);
  1128. if (!result)
  1129. goto cleanup;
  1130. iq = (s32) iq_est.iq_prod;
  1131. ii = iq_est.i_pwr;
  1132. qq = iq_est.q_pwr;
  1133. if ((ii + qq) < LCNPHY_MIN_RXIQ_PWR) {
  1134. result = false;
  1135. goto cleanup;
  1136. }
  1137. iq_nbits = wlc_phy_nbits(iq);
  1138. qq_nbits = wlc_phy_nbits(qq);
  1139. arsh = 10 - (30 - iq_nbits);
  1140. if (arsh >= 0) {
  1141. a = (-(iq << (30 - iq_nbits)) + (ii >> (1 + arsh)));
  1142. temp = (s32) (ii >> arsh);
  1143. if (temp == 0)
  1144. return false;
  1145. } else {
  1146. a = (-(iq << (30 - iq_nbits)) + (ii << (-1 - arsh)));
  1147. temp = (s32) (ii << -arsh);
  1148. if (temp == 0)
  1149. return false;
  1150. }
  1151. a /= temp;
  1152. brsh = qq_nbits - 31 + 20;
  1153. if (brsh >= 0) {
  1154. b = (qq << (31 - qq_nbits));
  1155. temp = (s32) (ii >> brsh);
  1156. if (temp == 0)
  1157. return false;
  1158. } else {
  1159. b = (qq << (31 - qq_nbits));
  1160. temp = (s32) (ii << -brsh);
  1161. if (temp == 0)
  1162. return false;
  1163. }
  1164. b /= temp;
  1165. b -= a * a;
  1166. b = (s32) int_sqrt((unsigned long) b);
  1167. b -= (1 << 10);
  1168. a0_new = (u16) (a & 0x3ff);
  1169. b0_new = (u16) (b & 0x3ff);
  1170. cleanup:
  1171. wlc_lcnphy_set_rx_iq_comp(pi, a0_new, b0_new);
  1172. mod_phy_reg(pi, 0x64b, (0x1 << 0), (1) << 0);
  1173. mod_phy_reg(pi, 0x64b, (0x1 << 3), (1) << 3);
  1174. pi_lcn->lcnphy_cal_results.rxiqcal_coeff_a0 = a0_new;
  1175. pi_lcn->lcnphy_cal_results.rxiqcal_coeff_b0 = b0_new;
  1176. return result;
  1177. }
  1178. static u32 wlc_lcnphy_measure_digital_power(struct brcms_phy *pi, u16 nsamples)
  1179. {
  1180. struct lcnphy_iq_est iq_est = { 0, 0, 0 };
  1181. if (!wlc_lcnphy_rx_iq_est(pi, nsamples, 32, &iq_est))
  1182. return 0;
  1183. return (iq_est.i_pwr + iq_est.q_pwr) / nsamples;
  1184. }
  1185. static bool wlc_lcnphy_rx_iq_cal_gain(struct brcms_phy *pi, u16 biq1_gain,
  1186. u16 tia_gain, u16 lna2_gain)
  1187. {
  1188. u32 i_thresh_l, q_thresh_l;
  1189. u32 i_thresh_h, q_thresh_h;
  1190. struct lcnphy_iq_est iq_est_h, iq_est_l;
  1191. wlc_lcnphy_set_rx_gain_by_distribution(pi, 0, 0, 0, biq1_gain, tia_gain,
  1192. lna2_gain, 0);
  1193. wlc_lcnphy_rx_gain_override_enable(pi, true);
  1194. wlc_lcnphy_start_tx_tone(pi, 2000, (40 >> 1), 0);
  1195. udelay(500);
  1196. write_radio_reg(pi, RADIO_2064_REG112, 0);
  1197. if (!wlc_lcnphy_rx_iq_est(pi, 1024, 32, &iq_est_l))
  1198. return false;
  1199. wlc_lcnphy_start_tx_tone(pi, 2000, 40, 0);
  1200. udelay(500);
  1201. write_radio_reg(pi, RADIO_2064_REG112, 0);
  1202. if (!wlc_lcnphy_rx_iq_est(pi, 1024, 32, &iq_est_h))
  1203. return false;
  1204. i_thresh_l = (iq_est_l.i_pwr << 1);
  1205. i_thresh_h = (iq_est_l.i_pwr << 2) + iq_est_l.i_pwr;
  1206. q_thresh_l = (iq_est_l.q_pwr << 1);
  1207. q_thresh_h = (iq_est_l.q_pwr << 2) + iq_est_l.q_pwr;
  1208. if ((iq_est_h.i_pwr > i_thresh_l) &&
  1209. (iq_est_h.i_pwr < i_thresh_h) &&
  1210. (iq_est_h.q_pwr > q_thresh_l) &&
  1211. (iq_est_h.q_pwr < q_thresh_h))
  1212. return true;
  1213. return false;
  1214. }
  1215. static bool
  1216. wlc_lcnphy_rx_iq_cal(struct brcms_phy *pi,
  1217. const struct lcnphy_rx_iqcomp *iqcomp,
  1218. int iqcomp_sz, bool tx_switch, bool rx_switch, int module,
  1219. int tx_gain_idx)
  1220. {
  1221. struct lcnphy_txgains old_gains;
  1222. u16 tx_pwr_ctrl;
  1223. u8 tx_gain_index_old = 0;
  1224. bool result = false, tx_gain_override_old = false;
  1225. u16 i, Core1TxControl_old, RFOverride0_old,
  1226. RFOverrideVal0_old, rfoverride2_old, rfoverride2val_old,
  1227. rfoverride3_old, rfoverride3val_old, rfoverride4_old,
  1228. rfoverride4val_old, afectrlovr_old, afectrlovrval_old;
  1229. int tia_gain, lna2_gain, biq1_gain;
  1230. bool set_gain;
  1231. u16 old_sslpnCalibClkEnCtrl, old_sslpnRxFeClkEnCtrl;
  1232. u16 values_to_save[11];
  1233. s16 *ptr;
  1234. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1235. ptr = kmalloc(sizeof(s16) * 131, GFP_ATOMIC);
  1236. if (NULL == ptr)
  1237. return false;
  1238. if (module == 2) {
  1239. while (iqcomp_sz--) {
  1240. if (iqcomp[iqcomp_sz].chan ==
  1241. CHSPEC_CHANNEL(pi->radio_chanspec)) {
  1242. wlc_lcnphy_set_rx_iq_comp(pi,
  1243. (u16)
  1244. iqcomp[iqcomp_sz].a,
  1245. (u16)
  1246. iqcomp[iqcomp_sz].b);
  1247. result = true;
  1248. break;
  1249. }
  1250. }
  1251. goto cal_done;
  1252. }
  1253. WARN_ON(module != 1);
  1254. tx_pwr_ctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  1255. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  1256. for (i = 0; i < 11; i++)
  1257. values_to_save[i] =
  1258. read_radio_reg(pi, rxiq_cal_rf_reg[i]);
  1259. Core1TxControl_old = read_phy_reg(pi, 0x631);
  1260. or_phy_reg(pi, 0x631, 0x0015);
  1261. RFOverride0_old = read_phy_reg(pi, 0x44c);
  1262. RFOverrideVal0_old = read_phy_reg(pi, 0x44d);
  1263. rfoverride2_old = read_phy_reg(pi, 0x4b0);
  1264. rfoverride2val_old = read_phy_reg(pi, 0x4b1);
  1265. rfoverride3_old = read_phy_reg(pi, 0x4f9);
  1266. rfoverride3val_old = read_phy_reg(pi, 0x4fa);
  1267. rfoverride4_old = read_phy_reg(pi, 0x938);
  1268. rfoverride4val_old = read_phy_reg(pi, 0x939);
  1269. afectrlovr_old = read_phy_reg(pi, 0x43b);
  1270. afectrlovrval_old = read_phy_reg(pi, 0x43c);
  1271. old_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  1272. old_sslpnRxFeClkEnCtrl = read_phy_reg(pi, 0x6db);
  1273. tx_gain_override_old = wlc_lcnphy_tx_gain_override_enabled(pi);
  1274. if (tx_gain_override_old) {
  1275. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  1276. tx_gain_index_old = pi_lcn->lcnphy_current_index;
  1277. }
  1278. wlc_lcnphy_set_tx_pwr_by_index(pi, tx_gain_idx);
  1279. mod_phy_reg(pi, 0x4f9, (0x1 << 0), 1 << 0);
  1280. mod_phy_reg(pi, 0x4fa, (0x1 << 0), 0 << 0);
  1281. mod_phy_reg(pi, 0x43b, (0x1 << 1), 1 << 1);
  1282. mod_phy_reg(pi, 0x43c, (0x1 << 1), 0 << 1);
  1283. write_radio_reg(pi, RADIO_2064_REG116, 0x06);
  1284. write_radio_reg(pi, RADIO_2064_REG12C, 0x07);
  1285. write_radio_reg(pi, RADIO_2064_REG06A, 0xd3);
  1286. write_radio_reg(pi, RADIO_2064_REG098, 0x03);
  1287. write_radio_reg(pi, RADIO_2064_REG00B, 0x7);
  1288. mod_radio_reg(pi, RADIO_2064_REG113, 1 << 4, 1 << 4);
  1289. write_radio_reg(pi, RADIO_2064_REG01D, 0x01);
  1290. write_radio_reg(pi, RADIO_2064_REG114, 0x01);
  1291. write_radio_reg(pi, RADIO_2064_REG02E, 0x10);
  1292. write_radio_reg(pi, RADIO_2064_REG12A, 0x08);
  1293. mod_phy_reg(pi, 0x938, (0x1 << 0), 1 << 0);
  1294. mod_phy_reg(pi, 0x939, (0x1 << 0), 0 << 0);
  1295. mod_phy_reg(pi, 0x938, (0x1 << 1), 1 << 1);
  1296. mod_phy_reg(pi, 0x939, (0x1 << 1), 1 << 1);
  1297. mod_phy_reg(pi, 0x938, (0x1 << 2), 1 << 2);
  1298. mod_phy_reg(pi, 0x939, (0x1 << 2), 1 << 2);
  1299. mod_phy_reg(pi, 0x938, (0x1 << 3), 1 << 3);
  1300. mod_phy_reg(pi, 0x939, (0x1 << 3), 1 << 3);
  1301. mod_phy_reg(pi, 0x938, (0x1 << 5), 1 << 5);
  1302. mod_phy_reg(pi, 0x939, (0x1 << 5), 0 << 5);
  1303. mod_phy_reg(pi, 0x43b, (0x1 << 0), 1 << 0);
  1304. mod_phy_reg(pi, 0x43c, (0x1 << 0), 0 << 0);
  1305. write_phy_reg(pi, 0x6da, 0xffff);
  1306. or_phy_reg(pi, 0x6db, 0x3);
  1307. wlc_lcnphy_set_trsw_override(pi, tx_switch, rx_switch);
  1308. for (lna2_gain = 3; lna2_gain >= 0; lna2_gain--) {
  1309. for (tia_gain = 4; tia_gain >= 0; tia_gain--) {
  1310. for (biq1_gain = 6; biq1_gain >= 0; biq1_gain--) {
  1311. set_gain = wlc_lcnphy_rx_iq_cal_gain(pi,
  1312. (u16)
  1313. biq1_gain,
  1314. (u16)
  1315. tia_gain,
  1316. (u16)
  1317. lna2_gain);
  1318. if (!set_gain)
  1319. continue;
  1320. result = wlc_lcnphy_calc_rx_iq_comp(pi, 1024);
  1321. goto stop_tone;
  1322. }
  1323. }
  1324. }
  1325. stop_tone:
  1326. wlc_lcnphy_stop_tx_tone(pi);
  1327. write_phy_reg(pi, 0x631, Core1TxControl_old);
  1328. write_phy_reg(pi, 0x44c, RFOverrideVal0_old);
  1329. write_phy_reg(pi, 0x44d, RFOverrideVal0_old);
  1330. write_phy_reg(pi, 0x4b0, rfoverride2_old);
  1331. write_phy_reg(pi, 0x4b1, rfoverride2val_old);
  1332. write_phy_reg(pi, 0x4f9, rfoverride3_old);
  1333. write_phy_reg(pi, 0x4fa, rfoverride3val_old);
  1334. write_phy_reg(pi, 0x938, rfoverride4_old);
  1335. write_phy_reg(pi, 0x939, rfoverride4val_old);
  1336. write_phy_reg(pi, 0x43b, afectrlovr_old);
  1337. write_phy_reg(pi, 0x43c, afectrlovrval_old);
  1338. write_phy_reg(pi, 0x6da, old_sslpnCalibClkEnCtrl);
  1339. write_phy_reg(pi, 0x6db, old_sslpnRxFeClkEnCtrl);
  1340. wlc_lcnphy_clear_trsw_override(pi);
  1341. mod_phy_reg(pi, 0x44c, (0x1 << 2), 0 << 2);
  1342. for (i = 0; i < 11; i++)
  1343. write_radio_reg(pi, rxiq_cal_rf_reg[i],
  1344. values_to_save[i]);
  1345. if (tx_gain_override_old)
  1346. wlc_lcnphy_set_tx_pwr_by_index(pi, tx_gain_index_old);
  1347. else
  1348. wlc_lcnphy_disable_tx_gain_override(pi);
  1349. wlc_lcnphy_set_tx_pwr_ctrl(pi, tx_pwr_ctrl);
  1350. wlc_lcnphy_rx_gain_override_enable(pi, false);
  1351. cal_done:
  1352. kfree(ptr);
  1353. return result;
  1354. }
  1355. s8 wlc_lcnphy_get_current_tx_pwr_idx(struct brcms_phy *pi)
  1356. {
  1357. s8 index;
  1358. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1359. if (txpwrctrl_off(pi))
  1360. index = pi_lcn->lcnphy_current_index;
  1361. else if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  1362. index = (s8) (wlc_lcnphy_get_current_tx_pwr_idx_if_pwrctrl_on(
  1363. pi) / 2);
  1364. else
  1365. index = pi_lcn->lcnphy_current_index;
  1366. return index;
  1367. }
  1368. void wlc_lcnphy_crsuprs(struct brcms_phy *pi, int channel)
  1369. {
  1370. u16 afectrlovr, afectrlovrval;
  1371. afectrlovr = read_phy_reg(pi, 0x43b);
  1372. afectrlovrval = read_phy_reg(pi, 0x43c);
  1373. if (channel != 0) {
  1374. mod_phy_reg(pi, 0x43b, (0x1 << 1), (1) << 1);
  1375. mod_phy_reg(pi, 0x43c, (0x1 << 1), (0) << 1);
  1376. mod_phy_reg(pi, 0x43b, (0x1 << 4), (1) << 4);
  1377. mod_phy_reg(pi, 0x43c, (0x1 << 6), (0) << 6);
  1378. write_phy_reg(pi, 0x44b, 0xffff);
  1379. wlc_lcnphy_tx_pu(pi, 1);
  1380. mod_phy_reg(pi, 0x634, (0xff << 8), (0) << 8);
  1381. or_phy_reg(pi, 0x6da, 0x0080);
  1382. or_phy_reg(pi, 0x00a, 0x228);
  1383. } else {
  1384. and_phy_reg(pi, 0x00a, ~(0x228));
  1385. and_phy_reg(pi, 0x6da, 0xFF7F);
  1386. write_phy_reg(pi, 0x43b, afectrlovr);
  1387. write_phy_reg(pi, 0x43c, afectrlovrval);
  1388. }
  1389. }
  1390. static void wlc_lcnphy_toggle_afe_pwdn(struct brcms_phy *pi)
  1391. {
  1392. u16 save_AfeCtrlOvrVal, save_AfeCtrlOvr;
  1393. save_AfeCtrlOvrVal = read_phy_reg(pi, 0x43c);
  1394. save_AfeCtrlOvr = read_phy_reg(pi, 0x43b);
  1395. write_phy_reg(pi, 0x43c, save_AfeCtrlOvrVal | 0x1);
  1396. write_phy_reg(pi, 0x43b, save_AfeCtrlOvr | 0x1);
  1397. write_phy_reg(pi, 0x43c, save_AfeCtrlOvrVal & 0xfffe);
  1398. write_phy_reg(pi, 0x43b, save_AfeCtrlOvr & 0xfffe);
  1399. write_phy_reg(pi, 0x43c, save_AfeCtrlOvrVal);
  1400. write_phy_reg(pi, 0x43b, save_AfeCtrlOvr);
  1401. }
  1402. static void
  1403. wlc_lcnphy_txrx_spur_avoidance_mode(struct brcms_phy *pi, bool enable)
  1404. {
  1405. if (enable) {
  1406. write_phy_reg(pi, 0x942, 0x7);
  1407. write_phy_reg(pi, 0x93b, ((1 << 13) + 23));
  1408. write_phy_reg(pi, 0x93c, ((1 << 13) + 1989));
  1409. write_phy_reg(pi, 0x44a, 0x084);
  1410. write_phy_reg(pi, 0x44a, 0x080);
  1411. write_phy_reg(pi, 0x6d3, 0x2222);
  1412. write_phy_reg(pi, 0x6d3, 0x2220);
  1413. } else {
  1414. write_phy_reg(pi, 0x942, 0x0);
  1415. write_phy_reg(pi, 0x93b, ((0 << 13) + 23));
  1416. write_phy_reg(pi, 0x93c, ((0 << 13) + 1989));
  1417. }
  1418. wlapi_switch_macfreq(pi->sh->physhim, enable);
  1419. }
  1420. static void
  1421. wlc_lcnphy_set_chanspec_tweaks(struct brcms_phy *pi, u16 chanspec)
  1422. {
  1423. u8 channel = CHSPEC_CHANNEL(chanspec);
  1424. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1425. if (channel == 14)
  1426. mod_phy_reg(pi, 0x448, (0x3 << 8), (2) << 8);
  1427. else
  1428. mod_phy_reg(pi, 0x448, (0x3 << 8), (1) << 8);
  1429. pi_lcn->lcnphy_bandedge_corr = 2;
  1430. if (channel == 1)
  1431. pi_lcn->lcnphy_bandedge_corr = 4;
  1432. if (channel == 1 || channel == 2 || channel == 3 ||
  1433. channel == 4 || channel == 9 ||
  1434. channel == 10 || channel == 11 || channel == 12) {
  1435. bcma_chipco_pll_write(&pi->d11core->bus->drv_cc, 0x2,
  1436. 0x03000c04);
  1437. bcma_chipco_pll_maskset(&pi->d11core->bus->drv_cc, 0x3,
  1438. ~0x00ffffff, 0x0);
  1439. bcma_chipco_pll_write(&pi->d11core->bus->drv_cc, 0x4,
  1440. 0x200005c0);
  1441. bcma_cc_set32(&pi->d11core->bus->drv_cc, BCMA_CC_PMU_CTL,
  1442. BCMA_CC_PMU_CTL_PLL_UPD);
  1443. write_phy_reg(pi, 0x942, 0);
  1444. wlc_lcnphy_txrx_spur_avoidance_mode(pi, false);
  1445. pi_lcn->lcnphy_spurmod = false;
  1446. mod_phy_reg(pi, 0x424, (0xff << 8), (0x1b) << 8);
  1447. write_phy_reg(pi, 0x425, 0x5907);
  1448. } else {
  1449. bcma_chipco_pll_write(&pi->d11core->bus->drv_cc, 0x2,
  1450. 0x03140c04);
  1451. bcma_chipco_pll_maskset(&pi->d11core->bus->drv_cc, 0x3,
  1452. ~0x00ffffff, 0x333333);
  1453. bcma_chipco_pll_write(&pi->d11core->bus->drv_cc, 0x4,
  1454. 0x202c2820);
  1455. bcma_cc_set32(&pi->d11core->bus->drv_cc, BCMA_CC_PMU_CTL,
  1456. BCMA_CC_PMU_CTL_PLL_UPD);
  1457. write_phy_reg(pi, 0x942, 0);
  1458. wlc_lcnphy_txrx_spur_avoidance_mode(pi, true);
  1459. pi_lcn->lcnphy_spurmod = false;
  1460. mod_phy_reg(pi, 0x424, (0xff << 8), (0x1f) << 8);
  1461. write_phy_reg(pi, 0x425, 0x590a);
  1462. }
  1463. or_phy_reg(pi, 0x44a, 0x44);
  1464. write_phy_reg(pi, 0x44a, 0x80);
  1465. }
  1466. static void
  1467. wlc_lcnphy_radio_2064_channel_tune_4313(struct brcms_phy *pi, u8 channel)
  1468. {
  1469. uint i;
  1470. const struct chan_info_2064_lcnphy *ci;
  1471. u8 rfpll_doubler = 0;
  1472. u8 pll_pwrup, pll_pwrup_ovr;
  1473. s32 qFxtal, qFref, qFvco, qFcal;
  1474. u8 d15, d16, f16, e44, e45;
  1475. u32 div_int, div_frac, fvco3, fpfd, fref3, fcal_div;
  1476. u16 loop_bw, d30, setCount;
  1477. u8 h29, h28_ten, e30, h30_ten, cp_current;
  1478. u16 g30, d28;
  1479. ci = &chan_info_2064_lcnphy[0];
  1480. rfpll_doubler = 1;
  1481. mod_radio_reg(pi, RADIO_2064_REG09D, 0x4, 0x1 << 2);
  1482. write_radio_reg(pi, RADIO_2064_REG09E, 0xf);
  1483. if (!rfpll_doubler) {
  1484. loop_bw = PLL_2064_LOOP_BW;
  1485. d30 = PLL_2064_D30;
  1486. } else {
  1487. loop_bw = PLL_2064_LOOP_BW_DOUBLER;
  1488. d30 = PLL_2064_D30_DOUBLER;
  1489. }
  1490. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  1491. for (i = 0; i < ARRAY_SIZE(chan_info_2064_lcnphy); i++)
  1492. if (chan_info_2064_lcnphy[i].chan == channel)
  1493. break;
  1494. if (i >= ARRAY_SIZE(chan_info_2064_lcnphy))
  1495. return;
  1496. ci = &chan_info_2064_lcnphy[i];
  1497. }
  1498. write_radio_reg(pi, RADIO_2064_REG02A, ci->logen_buftune);
  1499. mod_radio_reg(pi, RADIO_2064_REG030, 0x3, ci->logen_rccr_tx);
  1500. mod_radio_reg(pi, RADIO_2064_REG091, 0x3, ci->txrf_mix_tune_ctrl);
  1501. mod_radio_reg(pi, RADIO_2064_REG038, 0xf, ci->pa_input_tune_g);
  1502. mod_radio_reg(pi, RADIO_2064_REG030, 0x3 << 2,
  1503. (ci->logen_rccr_rx) << 2);
  1504. mod_radio_reg(pi, RADIO_2064_REG05E, 0xf, ci->pa_rxrf_lna1_freq_tune);
  1505. mod_radio_reg(pi, RADIO_2064_REG05E, (0xf) << 4,
  1506. (ci->pa_rxrf_lna2_freq_tune) << 4);
  1507. write_radio_reg(pi, RADIO_2064_REG06C, ci->rxrf_rxrf_spare1);
  1508. pll_pwrup = (u8) read_radio_reg(pi, RADIO_2064_REG044);
  1509. pll_pwrup_ovr = (u8) read_radio_reg(pi, RADIO_2064_REG12B);
  1510. or_radio_reg(pi, RADIO_2064_REG044, 0x07);
  1511. or_radio_reg(pi, RADIO_2064_REG12B, (0x07) << 1);
  1512. e44 = 0;
  1513. e45 = 0;
  1514. fpfd = rfpll_doubler ? (pi->xtalfreq << 1) : (pi->xtalfreq);
  1515. if (pi->xtalfreq > 26000000)
  1516. e44 = 1;
  1517. if (pi->xtalfreq > 52000000)
  1518. e45 = 1;
  1519. if (e44 == 0)
  1520. fcal_div = 1;
  1521. else if (e45 == 0)
  1522. fcal_div = 2;
  1523. else
  1524. fcal_div = 4;
  1525. fvco3 = (ci->freq * 3);
  1526. fref3 = 2 * fpfd;
  1527. qFxtal = wlc_lcnphy_qdiv_roundup(pi->xtalfreq, PLL_2064_MHZ, 16);
  1528. qFref = wlc_lcnphy_qdiv_roundup(fpfd, PLL_2064_MHZ, 16);
  1529. qFcal = pi->xtalfreq * fcal_div / PLL_2064_MHZ;
  1530. qFvco = wlc_lcnphy_qdiv_roundup(fvco3, 2, 16);
  1531. write_radio_reg(pi, RADIO_2064_REG04F, 0x02);
  1532. d15 = (pi->xtalfreq * fcal_div * 4 / 5) / PLL_2064_MHZ - 1;
  1533. write_radio_reg(pi, RADIO_2064_REG052, (0x07 & (d15 >> 2)));
  1534. write_radio_reg(pi, RADIO_2064_REG053, (d15 & 0x3) << 5);
  1535. d16 = (qFcal * 8 / (d15 + 1)) - 1;
  1536. write_radio_reg(pi, RADIO_2064_REG051, d16);
  1537. f16 = ((d16 + 1) * (d15 + 1)) / qFcal;
  1538. setCount = f16 * 3 * (ci->freq) / 32 - 1;
  1539. mod_radio_reg(pi, RADIO_2064_REG053, (0x0f << 0),
  1540. (u8) (setCount >> 8));
  1541. or_radio_reg(pi, RADIO_2064_REG053, 0x10);
  1542. write_radio_reg(pi, RADIO_2064_REG054, (u8) (setCount & 0xff));
  1543. div_int = ((fvco3 * (PLL_2064_MHZ >> 4)) / fref3) << 4;
  1544. div_frac = ((fvco3 * (PLL_2064_MHZ >> 4)) % fref3) << 4;
  1545. while (div_frac >= fref3) {
  1546. div_int++;
  1547. div_frac -= fref3;
  1548. }
  1549. div_frac = wlc_lcnphy_qdiv_roundup(div_frac, fref3, 20);
  1550. mod_radio_reg(pi, RADIO_2064_REG045, (0x1f << 0),
  1551. (u8) (div_int >> 4));
  1552. mod_radio_reg(pi, RADIO_2064_REG046, (0x1f << 4),
  1553. (u8) (div_int << 4));
  1554. mod_radio_reg(pi, RADIO_2064_REG046, (0x0f << 0),
  1555. (u8) (div_frac >> 16));
  1556. write_radio_reg(pi, RADIO_2064_REG047, (u8) (div_frac >> 8) & 0xff);
  1557. write_radio_reg(pi, RADIO_2064_REG048, (u8) div_frac & 0xff);
  1558. write_radio_reg(pi, RADIO_2064_REG040, 0xfb);
  1559. write_radio_reg(pi, RADIO_2064_REG041, 0x9A);
  1560. write_radio_reg(pi, RADIO_2064_REG042, 0xA3);
  1561. write_radio_reg(pi, RADIO_2064_REG043, 0x0C);
  1562. h29 = LCN_BW_LMT / loop_bw;
  1563. d28 = (((PLL_2064_HIGH_END_KVCO - PLL_2064_LOW_END_KVCO) *
  1564. (fvco3 / 2 - PLL_2064_LOW_END_VCO)) /
  1565. (PLL_2064_HIGH_END_VCO - PLL_2064_LOW_END_VCO))
  1566. + PLL_2064_LOW_END_KVCO;
  1567. h28_ten = (d28 * 10) / LCN_VCO_DIV;
  1568. e30 = (d30 - LCN_OFFSET) / LCN_FACT;
  1569. g30 = LCN_OFFSET + (e30 * LCN_FACT);
  1570. h30_ten = (g30 * 10) / LCN_CUR_DIV;
  1571. cp_current = ((LCN_CUR_LMT * h29 * LCN_MULT * 100) / h28_ten) / h30_ten;
  1572. mod_radio_reg(pi, RADIO_2064_REG03C, 0x3f, cp_current);
  1573. if (channel >= 1 && channel <= 5)
  1574. write_radio_reg(pi, RADIO_2064_REG03C, 0x8);
  1575. else
  1576. write_radio_reg(pi, RADIO_2064_REG03C, 0x7);
  1577. write_radio_reg(pi, RADIO_2064_REG03D, 0x3);
  1578. mod_radio_reg(pi, RADIO_2064_REG044, 0x0c, 0x0c);
  1579. udelay(1);
  1580. wlc_2064_vco_cal(pi);
  1581. write_radio_reg(pi, RADIO_2064_REG044, pll_pwrup);
  1582. write_radio_reg(pi, RADIO_2064_REG12B, pll_pwrup_ovr);
  1583. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  1584. write_radio_reg(pi, RADIO_2064_REG038, 3);
  1585. write_radio_reg(pi, RADIO_2064_REG091, 7);
  1586. }
  1587. if (!(pi->sh->boardflags & BFL_FEM)) {
  1588. static const u8 reg038[14] = {
  1589. 0xd, 0xe, 0xd, 0xd, 0xd, 0xc, 0xa,
  1590. 0xb, 0xb, 0x3, 0x3, 0x2, 0x0, 0x0
  1591. };
  1592. write_radio_reg(pi, RADIO_2064_REG02A, 0xf);
  1593. write_radio_reg(pi, RADIO_2064_REG091, 0x3);
  1594. write_radio_reg(pi, RADIO_2064_REG038, 0x3);
  1595. write_radio_reg(pi, RADIO_2064_REG038, reg038[channel - 1]);
  1596. }
  1597. }
  1598. static int
  1599. wlc_lcnphy_load_tx_iir_filter(struct brcms_phy *pi, bool is_ofdm, s16 filt_type)
  1600. {
  1601. s16 filt_index = -1;
  1602. int j;
  1603. u16 addr[] = {
  1604. 0x910,
  1605. 0x91e,
  1606. 0x91f,
  1607. 0x924,
  1608. 0x925,
  1609. 0x926,
  1610. 0x920,
  1611. 0x921,
  1612. 0x927,
  1613. 0x928,
  1614. 0x929,
  1615. 0x922,
  1616. 0x923,
  1617. 0x930,
  1618. 0x931,
  1619. 0x932
  1620. };
  1621. u16 addr_ofdm[] = {
  1622. 0x90f,
  1623. 0x900,
  1624. 0x901,
  1625. 0x906,
  1626. 0x907,
  1627. 0x908,
  1628. 0x902,
  1629. 0x903,
  1630. 0x909,
  1631. 0x90a,
  1632. 0x90b,
  1633. 0x904,
  1634. 0x905,
  1635. 0x90c,
  1636. 0x90d,
  1637. 0x90e
  1638. };
  1639. if (!is_ofdm) {
  1640. for (j = 0; j < LCNPHY_NUM_TX_DIG_FILTERS_CCK; j++) {
  1641. if (filt_type == LCNPHY_txdigfiltcoeffs_cck[j][0]) {
  1642. filt_index = (s16) j;
  1643. break;
  1644. }
  1645. }
  1646. if (filt_index != -1) {
  1647. for (j = 0; j < LCNPHY_NUM_DIG_FILT_COEFFS; j++)
  1648. write_phy_reg(pi, addr[j],
  1649. LCNPHY_txdigfiltcoeffs_cck
  1650. [filt_index][j + 1]);
  1651. }
  1652. } else {
  1653. for (j = 0; j < LCNPHY_NUM_TX_DIG_FILTERS_OFDM; j++) {
  1654. if (filt_type == LCNPHY_txdigfiltcoeffs_ofdm[j][0]) {
  1655. filt_index = (s16) j;
  1656. break;
  1657. }
  1658. }
  1659. if (filt_index != -1) {
  1660. for (j = 0; j < LCNPHY_NUM_DIG_FILT_COEFFS; j++)
  1661. write_phy_reg(pi, addr_ofdm[j],
  1662. LCNPHY_txdigfiltcoeffs_ofdm
  1663. [filt_index][j + 1]);
  1664. }
  1665. }
  1666. return (filt_index != -1) ? 0 : -1;
  1667. }
  1668. static u16 wlc_lcnphy_get_pa_gain(struct brcms_phy *pi)
  1669. {
  1670. u16 pa_gain;
  1671. pa_gain = (read_phy_reg(pi, 0x4fb) &
  1672. LCNPHY_txgainctrlovrval1_pagain_ovr_val1_MASK) >>
  1673. LCNPHY_txgainctrlovrval1_pagain_ovr_val1_SHIFT;
  1674. return pa_gain;
  1675. }
  1676. static void wlc_lcnphy_set_tx_gain(struct brcms_phy *pi,
  1677. struct lcnphy_txgains *target_gains)
  1678. {
  1679. u16 pa_gain = wlc_lcnphy_get_pa_gain(pi);
  1680. mod_phy_reg(
  1681. pi, 0x4b5,
  1682. (0xffff << 0),
  1683. ((target_gains->gm_gain) |
  1684. (target_gains->pga_gain << 8)) <<
  1685. 0);
  1686. mod_phy_reg(pi, 0x4fb,
  1687. (0x7fff << 0),
  1688. ((target_gains->pad_gain) | (pa_gain << 8)) << 0);
  1689. mod_phy_reg(
  1690. pi, 0x4fc,
  1691. (0xffff << 0),
  1692. ((target_gains->gm_gain) |
  1693. (target_gains->pga_gain << 8)) <<
  1694. 0);
  1695. mod_phy_reg(pi, 0x4fd,
  1696. (0x7fff << 0),
  1697. ((target_gains->pad_gain) | (pa_gain << 8)) << 0);
  1698. wlc_lcnphy_set_dac_gain(pi, target_gains->dac_gain);
  1699. wlc_lcnphy_enable_tx_gain_override(pi);
  1700. }
  1701. static u8 wlc_lcnphy_get_bbmult(struct brcms_phy *pi)
  1702. {
  1703. u16 m0m1;
  1704. struct phytbl_info tab;
  1705. tab.tbl_ptr = &m0m1;
  1706. tab.tbl_len = 1;
  1707. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  1708. tab.tbl_offset = 87;
  1709. tab.tbl_width = 16;
  1710. wlc_lcnphy_read_table(pi, &tab);
  1711. return (u8) ((m0m1 & 0xff00) >> 8);
  1712. }
  1713. static void wlc_lcnphy_set_bbmult(struct brcms_phy *pi, u8 m0)
  1714. {
  1715. u16 m0m1 = (u16) m0 << 8;
  1716. struct phytbl_info tab;
  1717. tab.tbl_ptr = &m0m1;
  1718. tab.tbl_len = 1;
  1719. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  1720. tab.tbl_offset = 87;
  1721. tab.tbl_width = 16;
  1722. wlc_lcnphy_write_table(pi, &tab);
  1723. }
  1724. static void wlc_lcnphy_clear_tx_power_offsets(struct brcms_phy *pi)
  1725. {
  1726. u32 data_buf[64];
  1727. struct phytbl_info tab;
  1728. memset(data_buf, 0, sizeof(data_buf));
  1729. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1730. tab.tbl_width = 32;
  1731. tab.tbl_ptr = data_buf;
  1732. if (!wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  1733. tab.tbl_len = 30;
  1734. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  1735. wlc_lcnphy_write_table(pi, &tab);
  1736. }
  1737. tab.tbl_len = 64;
  1738. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_MAC_OFFSET;
  1739. wlc_lcnphy_write_table(pi, &tab);
  1740. }
  1741. enum lcnphy_tssi_mode {
  1742. LCNPHY_TSSI_PRE_PA,
  1743. LCNPHY_TSSI_POST_PA,
  1744. LCNPHY_TSSI_EXT
  1745. };
  1746. static void
  1747. wlc_lcnphy_set_tssi_mux(struct brcms_phy *pi, enum lcnphy_tssi_mode pos)
  1748. {
  1749. mod_phy_reg(pi, 0x4d7, (0x1 << 0), (0x1) << 0);
  1750. mod_phy_reg(pi, 0x4d7, (0x1 << 6), (1) << 6);
  1751. if (LCNPHY_TSSI_POST_PA == pos) {
  1752. mod_phy_reg(pi, 0x4d9, (0x1 << 2), (0) << 2);
  1753. mod_phy_reg(pi, 0x4d9, (0x1 << 3), (1) << 3);
  1754. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1755. mod_radio_reg(pi, RADIO_2064_REG086, 0x4, 0x4);
  1756. } else {
  1757. mod_radio_reg(pi, RADIO_2064_REG03A, 1, 0x1);
  1758. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 0x8);
  1759. mod_radio_reg(pi, RADIO_2064_REG028, 0x1, 0x0);
  1760. mod_radio_reg(pi, RADIO_2064_REG11A, 0x4, 1<<2);
  1761. mod_radio_reg(pi, RADIO_2064_REG036, 0x10, 0x0);
  1762. mod_radio_reg(pi, RADIO_2064_REG11A, 0x10, 1<<4);
  1763. mod_radio_reg(pi, RADIO_2064_REG036, 0x3, 0x0);
  1764. mod_radio_reg(pi, RADIO_2064_REG035, 0xff, 0x77);
  1765. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, 0xe<<1);
  1766. mod_radio_reg(pi, RADIO_2064_REG112, 0x80, 1<<7);
  1767. mod_radio_reg(pi, RADIO_2064_REG005, 0x7, 1<<1);
  1768. mod_radio_reg(pi, RADIO_2064_REG029, 0xf0, 0<<4);
  1769. }
  1770. } else {
  1771. mod_phy_reg(pi, 0x4d9, (0x1 << 2), (0x1) << 2);
  1772. mod_phy_reg(pi, 0x4d9, (0x1 << 3), (0) << 3);
  1773. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1774. mod_radio_reg(pi, RADIO_2064_REG086, 0x4, 0x4);
  1775. } else {
  1776. mod_radio_reg(pi, RADIO_2064_REG03A, 1, 0);
  1777. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 0x8);
  1778. }
  1779. }
  1780. mod_phy_reg(pi, 0x637, (0x3 << 14), (0) << 14);
  1781. if (LCNPHY_TSSI_EXT == pos) {
  1782. write_radio_reg(pi, RADIO_2064_REG07F, 1);
  1783. mod_radio_reg(pi, RADIO_2064_REG005, 0x7, 0x2);
  1784. mod_radio_reg(pi, RADIO_2064_REG112, 0x80, 0x1 << 7);
  1785. mod_radio_reg(pi, RADIO_2064_REG028, 0x1f, 0x3);
  1786. }
  1787. }
  1788. static u16 wlc_lcnphy_rfseq_tbl_adc_pwrup(struct brcms_phy *pi)
  1789. {
  1790. u16 N1, N2, N3, N4, N5, N6, N;
  1791. N1 = ((read_phy_reg(pi, 0x4a5) & (0xff << 0))
  1792. >> 0);
  1793. N2 = 1 << ((read_phy_reg(pi, 0x4a5) & (0x7 << 12))
  1794. >> 12);
  1795. N3 = ((read_phy_reg(pi, 0x40d) & (0xff << 0))
  1796. >> 0);
  1797. N4 = 1 << ((read_phy_reg(pi, 0x40d) & (0x7 << 8))
  1798. >> 8);
  1799. N5 = ((read_phy_reg(pi, 0x4a2) & (0xff << 0))
  1800. >> 0);
  1801. N6 = 1 << ((read_phy_reg(pi, 0x4a2) & (0x7 << 8))
  1802. >> 8);
  1803. N = 2 * (N1 + N2 + N3 + N4 + 2 * (N5 + N6)) + 80;
  1804. if (N < 1600)
  1805. N = 1600;
  1806. return N;
  1807. }
  1808. static void wlc_lcnphy_pwrctrl_rssiparams(struct brcms_phy *pi)
  1809. {
  1810. u16 auxpga_vmid, auxpga_vmid_temp, auxpga_gain_temp;
  1811. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1812. auxpga_vmid = (2 << 8) |
  1813. (pi_lcn->lcnphy_rssi_vc << 4) | pi_lcn->lcnphy_rssi_vf;
  1814. auxpga_vmid_temp = (2 << 8) | (8 << 4) | 4;
  1815. auxpga_gain_temp = 2;
  1816. mod_phy_reg(pi, 0x4d8, (0x1 << 0), (0) << 0);
  1817. mod_phy_reg(pi, 0x4d8, (0x1 << 1), (0) << 1);
  1818. mod_phy_reg(pi, 0x4d7, (0x1 << 3), (0) << 3);
  1819. mod_phy_reg(pi, 0x4db,
  1820. (0x3ff << 0) |
  1821. (0x7 << 12),
  1822. (auxpga_vmid << 0) | (pi_lcn->lcnphy_rssi_gs << 12));
  1823. mod_phy_reg(pi, 0x4dc,
  1824. (0x3ff << 0) |
  1825. (0x7 << 12),
  1826. (auxpga_vmid << 0) | (pi_lcn->lcnphy_rssi_gs << 12));
  1827. mod_phy_reg(pi, 0x40a,
  1828. (0x3ff << 0) |
  1829. (0x7 << 12),
  1830. (auxpga_vmid << 0) | (pi_lcn->lcnphy_rssi_gs << 12));
  1831. mod_phy_reg(pi, 0x40b,
  1832. (0x3ff << 0) |
  1833. (0x7 << 12),
  1834. (auxpga_vmid_temp << 0) | (auxpga_gain_temp << 12));
  1835. mod_phy_reg(pi, 0x40c,
  1836. (0x3ff << 0) |
  1837. (0x7 << 12),
  1838. (auxpga_vmid_temp << 0) | (auxpga_gain_temp << 12));
  1839. mod_radio_reg(pi, RADIO_2064_REG082, (1 << 5), (1 << 5));
  1840. mod_radio_reg(pi, RADIO_2064_REG07C, (1 << 0), (1 << 0));
  1841. }
  1842. static void wlc_lcnphy_tssi_setup(struct brcms_phy *pi)
  1843. {
  1844. struct phytbl_info tab;
  1845. u32 rfseq, ind;
  1846. enum lcnphy_tssi_mode mode;
  1847. u8 tssi_sel;
  1848. if (pi->sh->boardflags & BFL_FEM) {
  1849. tssi_sel = 0x1;
  1850. mode = LCNPHY_TSSI_EXT;
  1851. } else {
  1852. tssi_sel = 0xe;
  1853. mode = LCNPHY_TSSI_POST_PA;
  1854. }
  1855. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1856. tab.tbl_width = 32;
  1857. tab.tbl_ptr = &ind;
  1858. tab.tbl_len = 1;
  1859. tab.tbl_offset = 0;
  1860. for (ind = 0; ind < 128; ind++) {
  1861. wlc_lcnphy_write_table(pi, &tab);
  1862. tab.tbl_offset++;
  1863. }
  1864. tab.tbl_offset = 704;
  1865. for (ind = 0; ind < 128; ind++) {
  1866. wlc_lcnphy_write_table(pi, &tab);
  1867. tab.tbl_offset++;
  1868. }
  1869. mod_phy_reg(pi, 0x503, (0x1 << 0), (0) << 0);
  1870. mod_phy_reg(pi, 0x503, (0x1 << 2), (0) << 2);
  1871. mod_phy_reg(pi, 0x503, (0x1 << 4), (1) << 4);
  1872. wlc_lcnphy_set_tssi_mux(pi, mode);
  1873. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0) << 14);
  1874. mod_phy_reg(pi, 0x4a4, (0x1 << 15), (1) << 15);
  1875. mod_phy_reg(pi, 0x4d0, (0x1 << 5), (0) << 5);
  1876. mod_phy_reg(pi, 0x4a4, (0x1ff << 0), (0) << 0);
  1877. mod_phy_reg(pi, 0x4a5, (0xff << 0), (255) << 0);
  1878. mod_phy_reg(pi, 0x4a5, (0x7 << 12), (5) << 12);
  1879. mod_phy_reg(pi, 0x4a5, (0x7 << 8), (0) << 8);
  1880. mod_phy_reg(pi, 0x40d, (0xff << 0), (64) << 0);
  1881. mod_phy_reg(pi, 0x40d, (0x7 << 8), (4) << 8);
  1882. mod_phy_reg(pi, 0x4a2, (0xff << 0), (64) << 0);
  1883. mod_phy_reg(pi, 0x4a2, (0x7 << 8), (4) << 8);
  1884. mod_phy_reg(pi, 0x4d0, (0x1ff << 6), (0) << 6);
  1885. mod_phy_reg(pi, 0x4a8, (0xff << 0), (0x1) << 0);
  1886. wlc_lcnphy_clear_tx_power_offsets(pi);
  1887. mod_phy_reg(pi, 0x4a6, (0x1 << 15), (1) << 15);
  1888. mod_phy_reg(pi, 0x4a6, (0x1ff << 0), (0xff) << 0);
  1889. mod_phy_reg(pi, 0x49a, (0x1ff << 0), (0xff) << 0);
  1890. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1891. mod_radio_reg(pi, RADIO_2064_REG028, 0xf, tssi_sel);
  1892. mod_radio_reg(pi, RADIO_2064_REG086, 0x4, 0x4);
  1893. } else {
  1894. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, tssi_sel << 1);
  1895. mod_radio_reg(pi, RADIO_2064_REG03A, 0x1, 1);
  1896. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 1 << 3);
  1897. }
  1898. write_radio_reg(pi, RADIO_2064_REG025, 0xc);
  1899. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1900. mod_radio_reg(pi, RADIO_2064_REG03A, 0x1, 1);
  1901. } else {
  1902. if (CHSPEC_IS2G(pi->radio_chanspec))
  1903. mod_radio_reg(pi, RADIO_2064_REG03A, 0x2, 1 << 1);
  1904. else
  1905. mod_radio_reg(pi, RADIO_2064_REG03A, 0x2, 0 << 1);
  1906. }
  1907. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  1908. mod_radio_reg(pi, RADIO_2064_REG03A, 0x2, 1 << 1);
  1909. else
  1910. mod_radio_reg(pi, RADIO_2064_REG03A, 0x4, 1 << 2);
  1911. mod_radio_reg(pi, RADIO_2064_REG11A, 0x1, 1 << 0);
  1912. mod_radio_reg(pi, RADIO_2064_REG005, 0x8, 1 << 3);
  1913. if (!wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  1914. mod_phy_reg(pi, 0x4d7,
  1915. (0x1 << 3) | (0x7 << 12), 0 << 3 | 2 << 12);
  1916. rfseq = wlc_lcnphy_rfseq_tbl_adc_pwrup(pi);
  1917. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  1918. tab.tbl_width = 16;
  1919. tab.tbl_ptr = &rfseq;
  1920. tab.tbl_len = 1;
  1921. tab.tbl_offset = 6;
  1922. wlc_lcnphy_write_table(pi, &tab);
  1923. mod_phy_reg(pi, 0x938, (0x1 << 2), (1) << 2);
  1924. mod_phy_reg(pi, 0x939, (0x1 << 2), (1) << 2);
  1925. mod_phy_reg(pi, 0x4a4, (0x1 << 12), (1) << 12);
  1926. mod_phy_reg(pi, 0x4d7, (0x1 << 2), (1) << 2);
  1927. mod_phy_reg(pi, 0x4d7, (0xf << 8), (0) << 8);
  1928. mod_radio_reg(pi, RADIO_2064_REG035, 0xff, 0x0);
  1929. mod_radio_reg(pi, RADIO_2064_REG036, 0x3, 0x0);
  1930. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 0x8);
  1931. wlc_lcnphy_pwrctrl_rssiparams(pi);
  1932. }
  1933. void wlc_lcnphy_tx_pwr_update_npt(struct brcms_phy *pi)
  1934. {
  1935. u16 tx_cnt, tx_total, npt;
  1936. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1937. tx_total = wlc_lcnphy_total_tx_frames(pi);
  1938. tx_cnt = tx_total - pi_lcn->lcnphy_tssi_tx_cnt;
  1939. npt = wlc_lcnphy_get_tx_pwr_npt(pi);
  1940. if (tx_cnt > (1 << npt)) {
  1941. pi_lcn->lcnphy_tssi_tx_cnt = tx_total;
  1942. pi_lcn->lcnphy_tssi_idx = wlc_lcnphy_get_current_tx_pwr_idx(pi);
  1943. pi_lcn->lcnphy_tssi_npt = npt;
  1944. }
  1945. }
  1946. s32 wlc_lcnphy_tssi2dbm(s32 tssi, s32 a1, s32 b0, s32 b1)
  1947. {
  1948. s32 a, b, p;
  1949. a = 32768 + (a1 * tssi);
  1950. b = (1024 * b0) + (64 * b1 * tssi);
  1951. p = ((2 * b) + a) / (2 * a);
  1952. return p;
  1953. }
  1954. static void wlc_lcnphy_txpower_reset_npt(struct brcms_phy *pi)
  1955. {
  1956. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1957. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  1958. return;
  1959. pi_lcn->lcnphy_tssi_idx = LCNPHY_TX_PWR_CTRL_START_INDEX_2G_4313;
  1960. pi_lcn->lcnphy_tssi_npt = LCNPHY_TX_PWR_CTRL_START_NPT;
  1961. }
  1962. void wlc_lcnphy_txpower_recalc_target(struct brcms_phy *pi)
  1963. {
  1964. struct phytbl_info tab;
  1965. u32 rate_table[BRCMS_NUM_RATES_CCK + BRCMS_NUM_RATES_OFDM +
  1966. BRCMS_NUM_RATES_MCS_1_STREAM];
  1967. uint i, j;
  1968. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  1969. return;
  1970. for (i = 0, j = 0; i < ARRAY_SIZE(rate_table); i++, j++) {
  1971. if (i == BRCMS_NUM_RATES_CCK + BRCMS_NUM_RATES_OFDM)
  1972. j = TXP_FIRST_MCS_20_SISO;
  1973. rate_table[i] = (u32) ((s32) (-pi->tx_power_offset[j]));
  1974. }
  1975. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1976. tab.tbl_width = 32;
  1977. tab.tbl_len = ARRAY_SIZE(rate_table);
  1978. tab.tbl_ptr = rate_table;
  1979. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  1980. wlc_lcnphy_write_table(pi, &tab);
  1981. if (wlc_lcnphy_get_target_tx_pwr(pi) != pi->tx_power_min) {
  1982. wlc_lcnphy_set_target_tx_pwr(pi, pi->tx_power_min);
  1983. wlc_lcnphy_txpower_reset_npt(pi);
  1984. }
  1985. }
  1986. static void wlc_lcnphy_set_tx_pwr_soft_ctrl(struct brcms_phy *pi, s8 index)
  1987. {
  1988. u32 cck_offset[4] = { 22, 22, 22, 22 };
  1989. u32 ofdm_offset, reg_offset_cck;
  1990. int i;
  1991. u16 index2;
  1992. struct phytbl_info tab;
  1993. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  1994. return;
  1995. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0x1) << 14);
  1996. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0x0) << 14);
  1997. or_phy_reg(pi, 0x6da, 0x0040);
  1998. reg_offset_cck = 0;
  1999. for (i = 0; i < 4; i++)
  2000. cck_offset[i] -= reg_offset_cck;
  2001. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  2002. tab.tbl_width = 32;
  2003. tab.tbl_len = 4;
  2004. tab.tbl_ptr = cck_offset;
  2005. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  2006. wlc_lcnphy_write_table(pi, &tab);
  2007. ofdm_offset = 0;
  2008. tab.tbl_len = 1;
  2009. tab.tbl_ptr = &ofdm_offset;
  2010. for (i = 836; i < 862; i++) {
  2011. tab.tbl_offset = i;
  2012. wlc_lcnphy_write_table(pi, &tab);
  2013. }
  2014. mod_phy_reg(pi, 0x4a4, (0x1 << 15), (0x1) << 15);
  2015. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0x1) << 14);
  2016. mod_phy_reg(pi, 0x4a4, (0x1 << 13), (0x1) << 13);
  2017. mod_phy_reg(pi, 0x4b0, (0x1 << 7), (0) << 7);
  2018. mod_phy_reg(pi, 0x43b, (0x1 << 6), (0) << 6);
  2019. mod_phy_reg(pi, 0x4a9, (0x1 << 15), (1) << 15);
  2020. index2 = (u16) (index * 2);
  2021. mod_phy_reg(pi, 0x4a9, (0x1ff << 0), (index2) << 0);
  2022. mod_phy_reg(pi, 0x6a3, (0x1 << 4), (0) << 4);
  2023. }
  2024. static s8 wlc_lcnphy_tempcompensated_txpwrctrl(struct brcms_phy *pi)
  2025. {
  2026. s8 index, delta_brd, delta_temp, new_index, tempcorrx;
  2027. s16 manp, meas_temp, temp_diff;
  2028. bool neg = false;
  2029. u16 temp;
  2030. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2031. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  2032. return pi_lcn->lcnphy_current_index;
  2033. index = FIXED_TXPWR;
  2034. if (pi_lcn->lcnphy_tempsense_slope == 0)
  2035. return index;
  2036. temp = (u16) wlc_lcnphy_tempsense(pi, 0);
  2037. meas_temp = LCNPHY_TEMPSENSE(temp);
  2038. if (pi->tx_power_min != 0)
  2039. delta_brd = (pi_lcn->lcnphy_measPower - pi->tx_power_min);
  2040. else
  2041. delta_brd = 0;
  2042. manp = LCNPHY_TEMPSENSE(pi_lcn->lcnphy_rawtempsense);
  2043. temp_diff = manp - meas_temp;
  2044. if (temp_diff < 0) {
  2045. neg = true;
  2046. temp_diff = -temp_diff;
  2047. }
  2048. delta_temp = (s8) wlc_lcnphy_qdiv_roundup((u32) (temp_diff * 192),
  2049. (u32) (pi_lcn->
  2050. lcnphy_tempsense_slope
  2051. * 10), 0);
  2052. if (neg)
  2053. delta_temp = -delta_temp;
  2054. if (pi_lcn->lcnphy_tempsense_option == 3
  2055. && LCNREV_IS(pi->pubpi.phy_rev, 0))
  2056. delta_temp = 0;
  2057. if (pi_lcn->lcnphy_tempcorrx > 31)
  2058. tempcorrx = (s8) (pi_lcn->lcnphy_tempcorrx - 64);
  2059. else
  2060. tempcorrx = (s8) pi_lcn->lcnphy_tempcorrx;
  2061. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  2062. tempcorrx = 4;
  2063. new_index =
  2064. index + delta_brd + delta_temp - pi_lcn->lcnphy_bandedge_corr;
  2065. new_index += tempcorrx;
  2066. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  2067. index = 127;
  2068. if (new_index < 0 || new_index > 126)
  2069. return index;
  2070. return new_index;
  2071. }
  2072. static u16 wlc_lcnphy_set_tx_pwr_ctrl_mode(struct brcms_phy *pi, u16 mode)
  2073. {
  2074. u16 current_mode = mode;
  2075. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi) &&
  2076. mode == LCNPHY_TX_PWR_CTRL_HW)
  2077. current_mode = LCNPHY_TX_PWR_CTRL_TEMPBASED;
  2078. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi) &&
  2079. mode == LCNPHY_TX_PWR_CTRL_TEMPBASED)
  2080. current_mode = LCNPHY_TX_PWR_CTRL_HW;
  2081. return current_mode;
  2082. }
  2083. void wlc_lcnphy_set_tx_pwr_ctrl(struct brcms_phy *pi, u16 mode)
  2084. {
  2085. u16 old_mode = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  2086. s8 index;
  2087. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2088. mode = wlc_lcnphy_set_tx_pwr_ctrl_mode(pi, mode);
  2089. old_mode = wlc_lcnphy_set_tx_pwr_ctrl_mode(pi, old_mode);
  2090. mod_phy_reg(pi, 0x6da, (0x1 << 6),
  2091. ((LCNPHY_TX_PWR_CTRL_HW == mode) ? 1 : 0) << 6);
  2092. mod_phy_reg(pi, 0x6a3, (0x1 << 4),
  2093. ((LCNPHY_TX_PWR_CTRL_HW == mode) ? 0 : 1) << 4);
  2094. if (old_mode != mode) {
  2095. if (LCNPHY_TX_PWR_CTRL_HW == old_mode) {
  2096. wlc_lcnphy_tx_pwr_update_npt(pi);
  2097. wlc_lcnphy_clear_tx_power_offsets(pi);
  2098. }
  2099. if (LCNPHY_TX_PWR_CTRL_HW == mode) {
  2100. wlc_lcnphy_txpower_recalc_target(pi);
  2101. wlc_lcnphy_set_start_tx_pwr_idx(pi,
  2102. pi_lcn->
  2103. lcnphy_tssi_idx);
  2104. wlc_lcnphy_set_tx_pwr_npt(pi, pi_lcn->lcnphy_tssi_npt);
  2105. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, 0);
  2106. pi_lcn->lcnphy_tssi_tx_cnt =
  2107. wlc_lcnphy_total_tx_frames(pi);
  2108. wlc_lcnphy_disable_tx_gain_override(pi);
  2109. pi_lcn->lcnphy_tx_power_idx_override = -1;
  2110. } else
  2111. wlc_lcnphy_enable_tx_gain_override(pi);
  2112. mod_phy_reg(pi, 0x4a4,
  2113. ((0x1 << 15) | (0x1 << 14) | (0x1 << 13)), mode);
  2114. if (mode == LCNPHY_TX_PWR_CTRL_TEMPBASED) {
  2115. index = wlc_lcnphy_tempcompensated_txpwrctrl(pi);
  2116. wlc_lcnphy_set_tx_pwr_soft_ctrl(pi, index);
  2117. pi_lcn->lcnphy_current_index = (s8)
  2118. ((read_phy_reg(pi,
  2119. 0x4a9) &
  2120. 0xFF) / 2);
  2121. }
  2122. }
  2123. }
  2124. static void
  2125. wlc_lcnphy_tx_iqlo_loopback(struct brcms_phy *pi, u16 *values_to_save)
  2126. {
  2127. u16 vmid;
  2128. int i;
  2129. for (i = 0; i < 20; i++)
  2130. values_to_save[i] =
  2131. read_radio_reg(pi, iqlo_loopback_rf_regs[i]);
  2132. mod_phy_reg(pi, 0x44c, (0x1 << 12), 1 << 12);
  2133. mod_phy_reg(pi, 0x44d, (0x1 << 14), 1 << 14);
  2134. mod_phy_reg(pi, 0x44c, (0x1 << 11), 1 << 11);
  2135. mod_phy_reg(pi, 0x44d, (0x1 << 13), 0 << 13);
  2136. mod_phy_reg(pi, 0x43b, (0x1 << 1), 1 << 1);
  2137. mod_phy_reg(pi, 0x43c, (0x1 << 1), 0 << 1);
  2138. mod_phy_reg(pi, 0x43b, (0x1 << 0), 1 << 0);
  2139. mod_phy_reg(pi, 0x43c, (0x1 << 0), 0 << 0);
  2140. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  2141. and_radio_reg(pi, RADIO_2064_REG03A, 0xFD);
  2142. else
  2143. and_radio_reg(pi, RADIO_2064_REG03A, 0xF9);
  2144. or_radio_reg(pi, RADIO_2064_REG11A, 0x1);
  2145. or_radio_reg(pi, RADIO_2064_REG036, 0x01);
  2146. or_radio_reg(pi, RADIO_2064_REG11A, 0x18);
  2147. udelay(20);
  2148. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  2149. if (CHSPEC_IS5G(pi->radio_chanspec))
  2150. mod_radio_reg(pi, RADIO_2064_REG03A, 1, 0);
  2151. else
  2152. or_radio_reg(pi, RADIO_2064_REG03A, 1);
  2153. } else {
  2154. if (CHSPEC_IS5G(pi->radio_chanspec))
  2155. mod_radio_reg(pi, RADIO_2064_REG03A, 3, 1);
  2156. else
  2157. or_radio_reg(pi, RADIO_2064_REG03A, 0x3);
  2158. }
  2159. udelay(20);
  2160. write_radio_reg(pi, RADIO_2064_REG025, 0xF);
  2161. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  2162. if (CHSPEC_IS5G(pi->radio_chanspec))
  2163. mod_radio_reg(pi, RADIO_2064_REG028, 0xF, 0x4);
  2164. else
  2165. mod_radio_reg(pi, RADIO_2064_REG028, 0xF, 0x6);
  2166. } else {
  2167. if (CHSPEC_IS5G(pi->radio_chanspec))
  2168. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, 0x4 << 1);
  2169. else
  2170. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, 0x6 << 1);
  2171. }
  2172. udelay(20);
  2173. write_radio_reg(pi, RADIO_2064_REG005, 0x8);
  2174. or_radio_reg(pi, RADIO_2064_REG112, 0x80);
  2175. udelay(20);
  2176. or_radio_reg(pi, RADIO_2064_REG0FF, 0x10);
  2177. or_radio_reg(pi, RADIO_2064_REG11F, 0x44);
  2178. udelay(20);
  2179. or_radio_reg(pi, RADIO_2064_REG00B, 0x7);
  2180. or_radio_reg(pi, RADIO_2064_REG113, 0x10);
  2181. udelay(20);
  2182. write_radio_reg(pi, RADIO_2064_REG007, 0x1);
  2183. udelay(20);
  2184. vmid = 0x2A6;
  2185. mod_radio_reg(pi, RADIO_2064_REG0FC, 0x3 << 0, (vmid >> 8) & 0x3);
  2186. write_radio_reg(pi, RADIO_2064_REG0FD, (vmid & 0xff));
  2187. or_radio_reg(pi, RADIO_2064_REG11F, 0x44);
  2188. udelay(20);
  2189. or_radio_reg(pi, RADIO_2064_REG0FF, 0x10);
  2190. udelay(20);
  2191. write_radio_reg(pi, RADIO_2064_REG012, 0x02);
  2192. or_radio_reg(pi, RADIO_2064_REG112, 0x06);
  2193. write_radio_reg(pi, RADIO_2064_REG036, 0x11);
  2194. write_radio_reg(pi, RADIO_2064_REG059, 0xcc);
  2195. write_radio_reg(pi, RADIO_2064_REG05C, 0x2e);
  2196. write_radio_reg(pi, RADIO_2064_REG078, 0xd7);
  2197. write_radio_reg(pi, RADIO_2064_REG092, 0x15);
  2198. }
  2199. static bool wlc_lcnphy_iqcal_wait(struct brcms_phy *pi)
  2200. {
  2201. uint delay_count = 0;
  2202. while (wlc_lcnphy_iqcal_active(pi)) {
  2203. udelay(100);
  2204. delay_count++;
  2205. if (delay_count > (10 * 500))
  2206. break;
  2207. }
  2208. return (0 == wlc_lcnphy_iqcal_active(pi));
  2209. }
  2210. static void
  2211. wlc_lcnphy_tx_iqlo_loopback_cleanup(struct brcms_phy *pi, u16 *values_to_save)
  2212. {
  2213. int i;
  2214. and_phy_reg(pi, 0x44c, 0x0 >> 11);
  2215. and_phy_reg(pi, 0x43b, 0xC);
  2216. for (i = 0; i < 20; i++)
  2217. write_radio_reg(pi, iqlo_loopback_rf_regs[i],
  2218. values_to_save[i]);
  2219. }
  2220. static void
  2221. wlc_lcnphy_tx_iqlo_cal(struct brcms_phy *pi,
  2222. struct lcnphy_txgains *target_gains,
  2223. enum lcnphy_cal_mode cal_mode, bool keep_tone)
  2224. {
  2225. struct lcnphy_txgains cal_gains, temp_gains;
  2226. u16 hash;
  2227. u8 band_idx;
  2228. int j;
  2229. u16 ncorr_override[5];
  2230. u16 syst_coeffs[] = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  2231. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
  2232. u16 commands_fullcal[] = {
  2233. 0x8434, 0x8334, 0x8084, 0x8267, 0x8056, 0x8234
  2234. };
  2235. u16 commands_recal[] = {
  2236. 0x8434, 0x8334, 0x8084, 0x8267, 0x8056, 0x8234
  2237. };
  2238. u16 command_nums_fullcal[] = {
  2239. 0x7a97, 0x7a97, 0x7a97, 0x7a87, 0x7a87, 0x7b97
  2240. };
  2241. u16 command_nums_recal[] = {
  2242. 0x7a97, 0x7a97, 0x7a97, 0x7a87, 0x7a87, 0x7b97
  2243. };
  2244. u16 *command_nums = command_nums_fullcal;
  2245. u16 *start_coeffs = NULL, *cal_cmds = NULL, cal_type, diq_start;
  2246. u16 tx_pwr_ctrl_old, save_txpwrctrlrfctrl2;
  2247. u16 save_sslpnCalibClkEnCtrl, save_sslpnRxFeClkEnCtrl;
  2248. bool tx_gain_override_old;
  2249. struct lcnphy_txgains old_gains;
  2250. uint i, n_cal_cmds = 0, n_cal_start = 0;
  2251. u16 *values_to_save;
  2252. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2253. values_to_save = kmalloc(sizeof(u16) * 20, GFP_ATOMIC);
  2254. if (NULL == values_to_save)
  2255. return;
  2256. save_sslpnRxFeClkEnCtrl = read_phy_reg(pi, 0x6db);
  2257. save_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2258. or_phy_reg(pi, 0x6da, 0x40);
  2259. or_phy_reg(pi, 0x6db, 0x3);
  2260. switch (cal_mode) {
  2261. case LCNPHY_CAL_FULL:
  2262. start_coeffs = syst_coeffs;
  2263. cal_cmds = commands_fullcal;
  2264. n_cal_cmds = ARRAY_SIZE(commands_fullcal);
  2265. break;
  2266. case LCNPHY_CAL_RECAL:
  2267. start_coeffs = syst_coeffs;
  2268. cal_cmds = commands_recal;
  2269. n_cal_cmds = ARRAY_SIZE(commands_recal);
  2270. command_nums = command_nums_recal;
  2271. break;
  2272. default:
  2273. break;
  2274. }
  2275. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2276. start_coeffs, 11, 16, 64);
  2277. write_phy_reg(pi, 0x6da, 0xffff);
  2278. mod_phy_reg(pi, 0x503, (0x1 << 3), (1) << 3);
  2279. tx_pwr_ctrl_old = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  2280. mod_phy_reg(pi, 0x4a4, (0x1 << 12), (1) << 12);
  2281. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2282. save_txpwrctrlrfctrl2 = read_phy_reg(pi, 0x4db);
  2283. mod_phy_reg(pi, 0x4db, (0x3ff << 0), (0x2a6) << 0);
  2284. mod_phy_reg(pi, 0x4db, (0x7 << 12), (2) << 12);
  2285. wlc_lcnphy_tx_iqlo_loopback(pi, values_to_save);
  2286. tx_gain_override_old = wlc_lcnphy_tx_gain_override_enabled(pi);
  2287. if (tx_gain_override_old)
  2288. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  2289. if (!target_gains) {
  2290. if (!tx_gain_override_old)
  2291. wlc_lcnphy_set_tx_pwr_by_index(pi,
  2292. pi_lcn->lcnphy_tssi_idx);
  2293. wlc_lcnphy_get_tx_gain(pi, &temp_gains);
  2294. target_gains = &temp_gains;
  2295. }
  2296. hash = (target_gains->gm_gain << 8) |
  2297. (target_gains->pga_gain << 4) | (target_gains->pad_gain);
  2298. band_idx = (CHSPEC_IS5G(pi->radio_chanspec) ? 1 : 0);
  2299. cal_gains = *target_gains;
  2300. memset(ncorr_override, 0, sizeof(ncorr_override));
  2301. for (j = 0; j < iqcal_gainparams_numgains_lcnphy[band_idx]; j++) {
  2302. if (hash == tbl_iqcal_gainparams_lcnphy[band_idx][j][0]) {
  2303. cal_gains.gm_gain =
  2304. tbl_iqcal_gainparams_lcnphy[band_idx][j][1];
  2305. cal_gains.pga_gain =
  2306. tbl_iqcal_gainparams_lcnphy[band_idx][j][2];
  2307. cal_gains.pad_gain =
  2308. tbl_iqcal_gainparams_lcnphy[band_idx][j][3];
  2309. memcpy(ncorr_override,
  2310. &tbl_iqcal_gainparams_lcnphy[band_idx][j][3],
  2311. sizeof(ncorr_override));
  2312. break;
  2313. }
  2314. }
  2315. wlc_lcnphy_set_tx_gain(pi, &cal_gains);
  2316. write_phy_reg(pi, 0x453, 0xaa9);
  2317. write_phy_reg(pi, 0x93d, 0xc0);
  2318. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2319. lcnphy_iqcal_loft_gainladder,
  2320. ARRAY_SIZE(lcnphy_iqcal_loft_gainladder),
  2321. 16, 0);
  2322. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2323. lcnphy_iqcal_ir_gainladder,
  2324. ARRAY_SIZE(
  2325. lcnphy_iqcal_ir_gainladder), 16,
  2326. 32);
  2327. if (pi->phy_tx_tone_freq) {
  2328. wlc_lcnphy_stop_tx_tone(pi);
  2329. udelay(5);
  2330. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 1);
  2331. } else {
  2332. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 1);
  2333. }
  2334. write_phy_reg(pi, 0x6da, 0xffff);
  2335. for (i = n_cal_start; i < n_cal_cmds; i++) {
  2336. u16 zero_diq = 0;
  2337. u16 best_coeffs[11];
  2338. u16 command_num;
  2339. cal_type = (cal_cmds[i] & 0x0f00) >> 8;
  2340. command_num = command_nums[i];
  2341. if (ncorr_override[cal_type])
  2342. command_num =
  2343. ncorr_override[cal_type] << 8 | (command_num &
  2344. 0xff);
  2345. write_phy_reg(pi, 0x452, command_num);
  2346. if ((cal_type == 3) || (cal_type == 4)) {
  2347. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2348. &diq_start, 1, 16, 69);
  2349. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2350. &zero_diq, 1, 16, 69);
  2351. }
  2352. write_phy_reg(pi, 0x451, cal_cmds[i]);
  2353. if (!wlc_lcnphy_iqcal_wait(pi))
  2354. goto cleanup;
  2355. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2356. best_coeffs,
  2357. ARRAY_SIZE(best_coeffs), 16, 96);
  2358. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2359. best_coeffs,
  2360. ARRAY_SIZE(best_coeffs), 16, 64);
  2361. if ((cal_type == 3) || (cal_type == 4))
  2362. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2363. &diq_start, 1, 16, 69);
  2364. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2365. pi_lcn->lcnphy_cal_results.
  2366. txiqlocal_bestcoeffs,
  2367. ARRAY_SIZE(pi_lcn->
  2368. lcnphy_cal_results.
  2369. txiqlocal_bestcoeffs),
  2370. 16, 96);
  2371. }
  2372. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2373. pi_lcn->lcnphy_cal_results.
  2374. txiqlocal_bestcoeffs,
  2375. ARRAY_SIZE(pi_lcn->lcnphy_cal_results.
  2376. txiqlocal_bestcoeffs), 16, 96);
  2377. pi_lcn->lcnphy_cal_results.txiqlocal_bestcoeffs_valid = true;
  2378. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2379. &pi_lcn->lcnphy_cal_results.
  2380. txiqlocal_bestcoeffs[0], 4, 16, 80);
  2381. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2382. &pi_lcn->lcnphy_cal_results.
  2383. txiqlocal_bestcoeffs[5], 2, 16, 85);
  2384. cleanup:
  2385. wlc_lcnphy_tx_iqlo_loopback_cleanup(pi, values_to_save);
  2386. kfree(values_to_save);
  2387. if (!keep_tone)
  2388. wlc_lcnphy_stop_tx_tone(pi);
  2389. write_phy_reg(pi, 0x4db, save_txpwrctrlrfctrl2);
  2390. write_phy_reg(pi, 0x453, 0);
  2391. if (tx_gain_override_old)
  2392. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  2393. wlc_lcnphy_set_tx_pwr_ctrl(pi, tx_pwr_ctrl_old);
  2394. write_phy_reg(pi, 0x6da, save_sslpnCalibClkEnCtrl);
  2395. write_phy_reg(pi, 0x6db, save_sslpnRxFeClkEnCtrl);
  2396. }
  2397. static void wlc_lcnphy_idle_tssi_est(struct brcms_phy_pub *ppi)
  2398. {
  2399. bool suspend, tx_gain_override_old;
  2400. struct lcnphy_txgains old_gains;
  2401. struct brcms_phy *pi = container_of(ppi, struct brcms_phy, pubpi_ro);
  2402. u16 idleTssi, idleTssi0_2C, idleTssi0_OB, idleTssi0_regvalue_OB,
  2403. idleTssi0_regvalue_2C;
  2404. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  2405. u16 SAVE_lpfgain = read_radio_reg(pi, RADIO_2064_REG112);
  2406. u16 SAVE_jtag_bb_afe_switch =
  2407. read_radio_reg(pi, RADIO_2064_REG007) & 1;
  2408. u16 SAVE_jtag_auxpga = read_radio_reg(pi, RADIO_2064_REG0FF) & 0x10;
  2409. u16 SAVE_iqadc_aux_en = read_radio_reg(pi, RADIO_2064_REG11F) & 4;
  2410. u8 SAVE_bbmult = wlc_lcnphy_get_bbmult(pi);
  2411. idleTssi = read_phy_reg(pi, 0x4ab);
  2412. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  2413. MCTL_EN_MAC));
  2414. if (!suspend)
  2415. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2416. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2417. tx_gain_override_old = wlc_lcnphy_tx_gain_override_enabled(pi);
  2418. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  2419. wlc_lcnphy_enable_tx_gain_override(pi);
  2420. wlc_lcnphy_set_tx_pwr_by_index(pi, 127);
  2421. write_radio_reg(pi, RADIO_2064_REG112, 0x6);
  2422. mod_radio_reg(pi, RADIO_2064_REG007, 0x1, 1);
  2423. mod_radio_reg(pi, RADIO_2064_REG0FF, 0x10, 1 << 4);
  2424. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, 1 << 2);
  2425. wlc_lcnphy_tssi_setup(pi);
  2426. mod_phy_reg(pi, 0x4d7, (0x1 << 0), (1 << 0));
  2427. mod_phy_reg(pi, 0x4d7, (0x1 << 6), (1 << 6));
  2428. wlc_lcnphy_set_bbmult(pi, 0x0);
  2429. wlc_phy_do_dummy_tx(pi, true, OFF);
  2430. idleTssi = ((read_phy_reg(pi, 0x4ab) & (0x1ff << 0))
  2431. >> 0);
  2432. idleTssi0_2C = ((read_phy_reg(pi, 0x63e) & (0x1ff << 0))
  2433. >> 0);
  2434. if (idleTssi0_2C >= 256)
  2435. idleTssi0_OB = idleTssi0_2C - 256;
  2436. else
  2437. idleTssi0_OB = idleTssi0_2C + 256;
  2438. idleTssi0_regvalue_OB = idleTssi0_OB;
  2439. if (idleTssi0_regvalue_OB >= 256)
  2440. idleTssi0_regvalue_2C = idleTssi0_regvalue_OB - 256;
  2441. else
  2442. idleTssi0_regvalue_2C = idleTssi0_regvalue_OB + 256;
  2443. mod_phy_reg(pi, 0x4a6, (0x1ff << 0), (idleTssi0_regvalue_2C) << 0);
  2444. mod_phy_reg(pi, 0x44c, (0x1 << 12), (0) << 12);
  2445. wlc_lcnphy_set_bbmult(pi, SAVE_bbmult);
  2446. wlc_lcnphy_set_tx_gain_override(pi, tx_gain_override_old);
  2447. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  2448. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  2449. write_radio_reg(pi, RADIO_2064_REG112, SAVE_lpfgain);
  2450. mod_radio_reg(pi, RADIO_2064_REG007, 0x1, SAVE_jtag_bb_afe_switch);
  2451. mod_radio_reg(pi, RADIO_2064_REG0FF, 0x10, SAVE_jtag_auxpga);
  2452. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, SAVE_iqadc_aux_en);
  2453. mod_radio_reg(pi, RADIO_2064_REG112, 0x80, 1 << 7);
  2454. if (!suspend)
  2455. wlapi_enable_mac(pi->sh->physhim);
  2456. }
  2457. static void wlc_lcnphy_vbat_temp_sense_setup(struct brcms_phy *pi, u8 mode)
  2458. {
  2459. bool suspend;
  2460. u16 save_txpwrCtrlEn;
  2461. u8 auxpga_vmidcourse, auxpga_vmidfine, auxpga_gain;
  2462. u16 auxpga_vmid;
  2463. struct phytbl_info tab;
  2464. u32 val;
  2465. u8 save_reg007, save_reg0FF, save_reg11F, save_reg005, save_reg025,
  2466. save_reg112;
  2467. u16 values_to_save[14];
  2468. s8 index;
  2469. int i;
  2470. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2471. udelay(999);
  2472. save_reg007 = (u8) read_radio_reg(pi, RADIO_2064_REG007);
  2473. save_reg0FF = (u8) read_radio_reg(pi, RADIO_2064_REG0FF);
  2474. save_reg11F = (u8) read_radio_reg(pi, RADIO_2064_REG11F);
  2475. save_reg005 = (u8) read_radio_reg(pi, RADIO_2064_REG005);
  2476. save_reg025 = (u8) read_radio_reg(pi, RADIO_2064_REG025);
  2477. save_reg112 = (u8) read_radio_reg(pi, RADIO_2064_REG112);
  2478. for (i = 0; i < 14; i++)
  2479. values_to_save[i] = read_phy_reg(pi, tempsense_phy_regs[i]);
  2480. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  2481. MCTL_EN_MAC));
  2482. if (!suspend)
  2483. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2484. save_txpwrCtrlEn = read_radio_reg(pi, 0x4a4);
  2485. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2486. index = pi_lcn->lcnphy_current_index;
  2487. wlc_lcnphy_set_tx_pwr_by_index(pi, 127);
  2488. mod_radio_reg(pi, RADIO_2064_REG007, 0x1, 0x1);
  2489. mod_radio_reg(pi, RADIO_2064_REG0FF, 0x10, 0x1 << 4);
  2490. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, 0x1 << 2);
  2491. mod_phy_reg(pi, 0x503, (0x1 << 0), (0) << 0);
  2492. mod_phy_reg(pi, 0x503, (0x1 << 2), (0) << 2);
  2493. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0) << 14);
  2494. mod_phy_reg(pi, 0x4a4, (0x1 << 15), (0) << 15);
  2495. mod_phy_reg(pi, 0x4d0, (0x1 << 5), (0) << 5);
  2496. mod_phy_reg(pi, 0x4a5, (0xff << 0), (255) << 0);
  2497. mod_phy_reg(pi, 0x4a5, (0x7 << 12), (5) << 12);
  2498. mod_phy_reg(pi, 0x4a5, (0x7 << 8), (0) << 8);
  2499. mod_phy_reg(pi, 0x40d, (0xff << 0), (64) << 0);
  2500. mod_phy_reg(pi, 0x40d, (0x7 << 8), (6) << 8);
  2501. mod_phy_reg(pi, 0x4a2, (0xff << 0), (64) << 0);
  2502. mod_phy_reg(pi, 0x4a2, (0x7 << 8), (6) << 8);
  2503. mod_phy_reg(pi, 0x4d9, (0x7 << 4), (2) << 4);
  2504. mod_phy_reg(pi, 0x4d9, (0x7 << 8), (3) << 8);
  2505. mod_phy_reg(pi, 0x4d9, (0x7 << 12), (1) << 12);
  2506. mod_phy_reg(pi, 0x4da, (0x1 << 12), (0) << 12);
  2507. mod_phy_reg(pi, 0x4da, (0x1 << 13), (1) << 13);
  2508. mod_phy_reg(pi, 0x4a6, (0x1 << 15), (1) << 15);
  2509. write_radio_reg(pi, RADIO_2064_REG025, 0xC);
  2510. mod_radio_reg(pi, RADIO_2064_REG005, 0x8, 0x1 << 3);
  2511. mod_phy_reg(pi, 0x938, (0x1 << 2), (1) << 2);
  2512. mod_phy_reg(pi, 0x939, (0x1 << 2), (1) << 2);
  2513. mod_phy_reg(pi, 0x4a4, (0x1 << 12), (1) << 12);
  2514. val = wlc_lcnphy_rfseq_tbl_adc_pwrup(pi);
  2515. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  2516. tab.tbl_width = 16;
  2517. tab.tbl_len = 1;
  2518. tab.tbl_ptr = &val;
  2519. tab.tbl_offset = 6;
  2520. wlc_lcnphy_write_table(pi, &tab);
  2521. if (mode == TEMPSENSE) {
  2522. mod_phy_reg(pi, 0x4d7, (0x1 << 3), (1) << 3);
  2523. mod_phy_reg(pi, 0x4d7, (0x7 << 12), (1) << 12);
  2524. auxpga_vmidcourse = 8;
  2525. auxpga_vmidfine = 0x4;
  2526. auxpga_gain = 2;
  2527. mod_radio_reg(pi, RADIO_2064_REG082, 0x20, 1 << 5);
  2528. } else {
  2529. mod_phy_reg(pi, 0x4d7, (0x1 << 3), (1) << 3);
  2530. mod_phy_reg(pi, 0x4d7, (0x7 << 12), (3) << 12);
  2531. auxpga_vmidcourse = 7;
  2532. auxpga_vmidfine = 0xa;
  2533. auxpga_gain = 2;
  2534. }
  2535. auxpga_vmid =
  2536. (u16) ((2 << 8) | (auxpga_vmidcourse << 4) | auxpga_vmidfine);
  2537. mod_phy_reg(pi, 0x4d8, (0x1 << 0), (1) << 0);
  2538. mod_phy_reg(pi, 0x4d8, (0x3ff << 2), (auxpga_vmid) << 2);
  2539. mod_phy_reg(pi, 0x4d8, (0x1 << 1), (1) << 1);
  2540. mod_phy_reg(pi, 0x4d8, (0x7 << 12), (auxpga_gain) << 12);
  2541. mod_phy_reg(pi, 0x4d0, (0x1 << 5), (1) << 5);
  2542. write_radio_reg(pi, RADIO_2064_REG112, 0x6);
  2543. wlc_phy_do_dummy_tx(pi, true, OFF);
  2544. if (!tempsense_done(pi))
  2545. udelay(10);
  2546. write_radio_reg(pi, RADIO_2064_REG007, (u16) save_reg007);
  2547. write_radio_reg(pi, RADIO_2064_REG0FF, (u16) save_reg0FF);
  2548. write_radio_reg(pi, RADIO_2064_REG11F, (u16) save_reg11F);
  2549. write_radio_reg(pi, RADIO_2064_REG005, (u16) save_reg005);
  2550. write_radio_reg(pi, RADIO_2064_REG025, (u16) save_reg025);
  2551. write_radio_reg(pi, RADIO_2064_REG112, (u16) save_reg112);
  2552. for (i = 0; i < 14; i++)
  2553. write_phy_reg(pi, tempsense_phy_regs[i], values_to_save[i]);
  2554. wlc_lcnphy_set_tx_pwr_by_index(pi, (int)index);
  2555. write_radio_reg(pi, 0x4a4, save_txpwrCtrlEn);
  2556. if (!suspend)
  2557. wlapi_enable_mac(pi->sh->physhim);
  2558. udelay(999);
  2559. }
  2560. static void wlc_lcnphy_tx_pwr_ctrl_init(struct brcms_phy_pub *ppi)
  2561. {
  2562. struct lcnphy_txgains tx_gains;
  2563. u8 bbmult;
  2564. struct phytbl_info tab;
  2565. s32 a1, b0, b1;
  2566. s32 tssi, pwr, maxtargetpwr, mintargetpwr;
  2567. bool suspend;
  2568. struct brcms_phy *pi = container_of(ppi, struct brcms_phy, pubpi_ro);
  2569. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  2570. MCTL_EN_MAC));
  2571. if (!suspend)
  2572. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2573. if (!pi->hwpwrctrl_capable) {
  2574. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  2575. tx_gains.gm_gain = 4;
  2576. tx_gains.pga_gain = 12;
  2577. tx_gains.pad_gain = 12;
  2578. tx_gains.dac_gain = 0;
  2579. bbmult = 150;
  2580. } else {
  2581. tx_gains.gm_gain = 7;
  2582. tx_gains.pga_gain = 15;
  2583. tx_gains.pad_gain = 14;
  2584. tx_gains.dac_gain = 0;
  2585. bbmult = 150;
  2586. }
  2587. wlc_lcnphy_set_tx_gain(pi, &tx_gains);
  2588. wlc_lcnphy_set_bbmult(pi, bbmult);
  2589. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  2590. } else {
  2591. wlc_lcnphy_idle_tssi_est(ppi);
  2592. wlc_lcnphy_clear_tx_power_offsets(pi);
  2593. b0 = pi->txpa_2g[0];
  2594. b1 = pi->txpa_2g[1];
  2595. a1 = pi->txpa_2g[2];
  2596. maxtargetpwr = wlc_lcnphy_tssi2dbm(10, a1, b0, b1);
  2597. mintargetpwr = wlc_lcnphy_tssi2dbm(125, a1, b0, b1);
  2598. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  2599. tab.tbl_width = 32;
  2600. tab.tbl_ptr = &pwr;
  2601. tab.tbl_len = 1;
  2602. tab.tbl_offset = 0;
  2603. for (tssi = 0; tssi < 128; tssi++) {
  2604. pwr = wlc_lcnphy_tssi2dbm(tssi, a1, b0, b1);
  2605. pwr = (pwr < mintargetpwr) ? mintargetpwr : pwr;
  2606. wlc_lcnphy_write_table(pi, &tab);
  2607. tab.tbl_offset++;
  2608. }
  2609. mod_phy_reg(pi, 0x4d0, (0x1 << 0), (0) << 0);
  2610. mod_phy_reg(pi, 0x4d3, (0xff << 0), (0) << 0);
  2611. mod_phy_reg(pi, 0x4d3, (0xff << 8), (0) << 8);
  2612. mod_phy_reg(pi, 0x4d0, (0x1 << 4), (0) << 4);
  2613. mod_phy_reg(pi, 0x4d0, (0x1 << 2), (0) << 2);
  2614. mod_phy_reg(pi, 0x410, (0x1 << 7), (0) << 7);
  2615. write_phy_reg(pi, 0x4a8, 10);
  2616. wlc_lcnphy_set_target_tx_pwr(pi, LCN_TARGET_PWR);
  2617. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_HW);
  2618. }
  2619. if (!suspend)
  2620. wlapi_enable_mac(pi->sh->physhim);
  2621. }
  2622. static void wlc_lcnphy_set_pa_gain(struct brcms_phy *pi, u16 gain)
  2623. {
  2624. mod_phy_reg(pi, 0x4fb,
  2625. LCNPHY_txgainctrlovrval1_pagain_ovr_val1_MASK,
  2626. gain << LCNPHY_txgainctrlovrval1_pagain_ovr_val1_SHIFT);
  2627. mod_phy_reg(pi, 0x4fd,
  2628. LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_MASK,
  2629. gain << LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_SHIFT);
  2630. }
  2631. void
  2632. wlc_lcnphy_get_radio_loft(struct brcms_phy *pi,
  2633. u8 *ei0, u8 *eq0, u8 *fi0, u8 *fq0)
  2634. {
  2635. *ei0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG089));
  2636. *eq0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG08A));
  2637. *fi0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG08B));
  2638. *fq0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG08C));
  2639. }
  2640. void wlc_lcnphy_set_tx_iqcc(struct brcms_phy *pi, u16 a, u16 b)
  2641. {
  2642. struct phytbl_info tab;
  2643. u16 iqcc[2];
  2644. iqcc[0] = a;
  2645. iqcc[1] = b;
  2646. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  2647. tab.tbl_width = 16;
  2648. tab.tbl_ptr = iqcc;
  2649. tab.tbl_len = 2;
  2650. tab.tbl_offset = 80;
  2651. wlc_lcnphy_write_table(pi, &tab);
  2652. }
  2653. void wlc_lcnphy_set_tx_locc(struct brcms_phy *pi, u16 didq)
  2654. {
  2655. struct phytbl_info tab;
  2656. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  2657. tab.tbl_width = 16;
  2658. tab.tbl_ptr = &didq;
  2659. tab.tbl_len = 1;
  2660. tab.tbl_offset = 85;
  2661. wlc_lcnphy_write_table(pi, &tab);
  2662. }
  2663. void wlc_lcnphy_set_tx_pwr_by_index(struct brcms_phy *pi, int index)
  2664. {
  2665. struct phytbl_info tab;
  2666. u16 a, b;
  2667. u8 bb_mult;
  2668. u32 bbmultiqcomp, txgain, locoeffs, rfpower;
  2669. struct lcnphy_txgains gains;
  2670. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2671. pi_lcn->lcnphy_tx_power_idx_override = (s8) index;
  2672. pi_lcn->lcnphy_current_index = (u8) index;
  2673. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  2674. tab.tbl_width = 32;
  2675. tab.tbl_len = 1;
  2676. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2677. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + index;
  2678. tab.tbl_ptr = &bbmultiqcomp;
  2679. wlc_lcnphy_read_table(pi, &tab);
  2680. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + index;
  2681. tab.tbl_width = 32;
  2682. tab.tbl_ptr = &txgain;
  2683. wlc_lcnphy_read_table(pi, &tab);
  2684. gains.gm_gain = (u16) (txgain & 0xff);
  2685. gains.pga_gain = (u16) (txgain >> 8) & 0xff;
  2686. gains.pad_gain = (u16) (txgain >> 16) & 0xff;
  2687. gains.dac_gain = (u16) (bbmultiqcomp >> 28) & 0x07;
  2688. wlc_lcnphy_set_tx_gain(pi, &gains);
  2689. wlc_lcnphy_set_pa_gain(pi, (u16) (txgain >> 24) & 0x7f);
  2690. bb_mult = (u8) ((bbmultiqcomp >> 20) & 0xff);
  2691. wlc_lcnphy_set_bbmult(pi, bb_mult);
  2692. wlc_lcnphy_enable_tx_gain_override(pi);
  2693. if (!wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  2694. a = (u16) ((bbmultiqcomp >> 10) & 0x3ff);
  2695. b = (u16) (bbmultiqcomp & 0x3ff);
  2696. wlc_lcnphy_set_tx_iqcc(pi, a, b);
  2697. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_LO_OFFSET + index;
  2698. tab.tbl_ptr = &locoeffs;
  2699. wlc_lcnphy_read_table(pi, &tab);
  2700. wlc_lcnphy_set_tx_locc(pi, (u16) locoeffs);
  2701. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_PWR_OFFSET + index;
  2702. tab.tbl_ptr = &rfpower;
  2703. wlc_lcnphy_read_table(pi, &tab);
  2704. mod_phy_reg(pi, 0x6a6, (0x1fff << 0), (rfpower * 8) << 0);
  2705. }
  2706. }
  2707. static void wlc_lcnphy_clear_papd_comptable(struct brcms_phy *pi)
  2708. {
  2709. u32 j;
  2710. struct phytbl_info tab;
  2711. u32 temp_offset[128];
  2712. tab.tbl_ptr = temp_offset;
  2713. tab.tbl_len = 128;
  2714. tab.tbl_id = LCNPHY_TBL_ID_PAPDCOMPDELTATBL;
  2715. tab.tbl_width = 32;
  2716. tab.tbl_offset = 0;
  2717. memset(temp_offset, 0, sizeof(temp_offset));
  2718. for (j = 1; j < 128; j += 2)
  2719. temp_offset[j] = 0x80000;
  2720. wlc_lcnphy_write_table(pi, &tab);
  2721. return;
  2722. }
  2723. void wlc_lcnphy_tx_pu(struct brcms_phy *pi, bool bEnable)
  2724. {
  2725. if (!bEnable) {
  2726. and_phy_reg(pi, 0x43b, ~(u16) ((0x1 << 1) | (0x1 << 4)));
  2727. mod_phy_reg(pi, 0x43c, (0x1 << 1), 1 << 1);
  2728. and_phy_reg(pi, 0x44c,
  2729. ~(u16) ((0x1 << 3) |
  2730. (0x1 << 5) |
  2731. (0x1 << 12) |
  2732. (0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  2733. and_phy_reg(pi, 0x44d,
  2734. ~(u16) ((0x1 << 3) | (0x1 << 5) | (0x1 << 14)));
  2735. mod_phy_reg(pi, 0x44d, (0x1 << 2), 1 << 2);
  2736. mod_phy_reg(pi, 0x44d, (0x1 << 1) | (0x1 << 0), (0x1 << 0));
  2737. and_phy_reg(pi, 0x4f9,
  2738. ~(u16) ((0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  2739. and_phy_reg(pi, 0x4fa,
  2740. ~(u16) ((0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  2741. } else {
  2742. mod_phy_reg(pi, 0x43b, (0x1 << 1), 1 << 1);
  2743. mod_phy_reg(pi, 0x43c, (0x1 << 1), 0 << 1);
  2744. mod_phy_reg(pi, 0x43b, (0x1 << 4), 1 << 4);
  2745. mod_phy_reg(pi, 0x43c, (0x1 << 6), 0 << 6);
  2746. mod_phy_reg(pi, 0x44c, (0x1 << 12), 1 << 12);
  2747. mod_phy_reg(pi, 0x44d, (0x1 << 14), 1 << 14);
  2748. wlc_lcnphy_set_trsw_override(pi, true, false);
  2749. mod_phy_reg(pi, 0x44d, (0x1 << 2), 0 << 2);
  2750. mod_phy_reg(pi, 0x44c, (0x1 << 2), 1 << 2);
  2751. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  2752. mod_phy_reg(pi, 0x44c, (0x1 << 3), 1 << 3);
  2753. mod_phy_reg(pi, 0x44d, (0x1 << 3), 1 << 3);
  2754. mod_phy_reg(pi, 0x44c, (0x1 << 5), 1 << 5);
  2755. mod_phy_reg(pi, 0x44d, (0x1 << 5), 0 << 5);
  2756. mod_phy_reg(pi, 0x4f9, (0x1 << 1), 1 << 1);
  2757. mod_phy_reg(pi, 0x4fa, (0x1 << 1), 1 << 1);
  2758. mod_phy_reg(pi, 0x4f9, (0x1 << 2), 1 << 2);
  2759. mod_phy_reg(pi, 0x4fa, (0x1 << 2), 1 << 2);
  2760. mod_phy_reg(pi, 0x4f9, (0x1 << 0), 1 << 0);
  2761. mod_phy_reg(pi, 0x4fa, (0x1 << 0), 1 << 0);
  2762. } else {
  2763. mod_phy_reg(pi, 0x44c, (0x1 << 3), 1 << 3);
  2764. mod_phy_reg(pi, 0x44d, (0x1 << 3), 0 << 3);
  2765. mod_phy_reg(pi, 0x44c, (0x1 << 5), 1 << 5);
  2766. mod_phy_reg(pi, 0x44d, (0x1 << 5), 1 << 5);
  2767. mod_phy_reg(pi, 0x4f9, (0x1 << 1), 1 << 1);
  2768. mod_phy_reg(pi, 0x4fa, (0x1 << 1), 0 << 1);
  2769. mod_phy_reg(pi, 0x4f9, (0x1 << 2), 1 << 2);
  2770. mod_phy_reg(pi, 0x4fa, (0x1 << 2), 0 << 2);
  2771. mod_phy_reg(pi, 0x4f9, (0x1 << 0), 1 << 0);
  2772. mod_phy_reg(pi, 0x4fa, (0x1 << 0), 0 << 0);
  2773. }
  2774. }
  2775. }
  2776. static void
  2777. wlc_lcnphy_run_samples(struct brcms_phy *pi,
  2778. u16 num_samps,
  2779. u16 num_loops, u16 wait, bool iqcalmode)
  2780. {
  2781. or_phy_reg(pi, 0x6da, 0x8080);
  2782. mod_phy_reg(pi, 0x642, (0x7f << 0), (num_samps - 1) << 0);
  2783. if (num_loops != 0xffff)
  2784. num_loops--;
  2785. mod_phy_reg(pi, 0x640, (0xffff << 0), num_loops << 0);
  2786. mod_phy_reg(pi, 0x641, (0xffff << 0), wait << 0);
  2787. if (iqcalmode) {
  2788. and_phy_reg(pi, 0x453, (u16) ~(0x1 << 15));
  2789. or_phy_reg(pi, 0x453, (0x1 << 15));
  2790. } else {
  2791. write_phy_reg(pi, 0x63f, 1);
  2792. wlc_lcnphy_tx_pu(pi, 1);
  2793. }
  2794. or_radio_reg(pi, RADIO_2064_REG112, 0x6);
  2795. }
  2796. void wlc_lcnphy_deaf_mode(struct brcms_phy *pi, bool mode)
  2797. {
  2798. u8 phybw40;
  2799. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  2800. if (LCNREV_LT(pi->pubpi.phy_rev, 2)) {
  2801. mod_phy_reg(pi, 0x4b0, (0x1 << 5), (mode) << 5);
  2802. mod_phy_reg(pi, 0x4b1, (0x1 << 9), 0 << 9);
  2803. } else {
  2804. mod_phy_reg(pi, 0x4b0, (0x1 << 5), (mode) << 5);
  2805. mod_phy_reg(pi, 0x4b1, (0x1 << 9), 0 << 9);
  2806. }
  2807. if (phybw40 == 0) {
  2808. mod_phy_reg((pi), 0x410,
  2809. (0x1 << 6) |
  2810. (0x1 << 5),
  2811. ((CHSPEC_IS2G(
  2812. pi->radio_chanspec)) ? (!mode) : 0) <<
  2813. 6 | (!mode) << 5);
  2814. mod_phy_reg(pi, 0x410, (0x1 << 7), (mode) << 7);
  2815. }
  2816. }
  2817. void
  2818. wlc_lcnphy_start_tx_tone(struct brcms_phy *pi, s32 f_kHz, u16 max_val,
  2819. bool iqcalmode)
  2820. {
  2821. u8 phy_bw;
  2822. u16 num_samps, t, k;
  2823. u32 bw;
  2824. s32 theta = 0, rot = 0;
  2825. struct cordic_iq tone_samp;
  2826. u32 data_buf[64];
  2827. u16 i_samp, q_samp;
  2828. struct phytbl_info tab;
  2829. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2830. pi->phy_tx_tone_freq = f_kHz;
  2831. wlc_lcnphy_deaf_mode(pi, true);
  2832. phy_bw = 40;
  2833. if (pi_lcn->lcnphy_spurmod) {
  2834. write_phy_reg(pi, 0x942, 0x2);
  2835. write_phy_reg(pi, 0x93b, 0x0);
  2836. write_phy_reg(pi, 0x93c, 0x0);
  2837. wlc_lcnphy_txrx_spur_avoidance_mode(pi, false);
  2838. }
  2839. if (f_kHz) {
  2840. k = 1;
  2841. do {
  2842. bw = phy_bw * 1000 * k;
  2843. num_samps = bw / abs(f_kHz);
  2844. k++;
  2845. } while ((num_samps * (u32) (abs(f_kHz))) != bw);
  2846. } else
  2847. num_samps = 2;
  2848. rot = ((f_kHz * 36) / phy_bw) / 100;
  2849. theta = 0;
  2850. for (t = 0; t < num_samps; t++) {
  2851. tone_samp = cordic_calc_iq(theta);
  2852. theta += rot;
  2853. i_samp = (u16) (FLOAT(tone_samp.i * max_val) & 0x3ff);
  2854. q_samp = (u16) (FLOAT(tone_samp.q * max_val) & 0x3ff);
  2855. data_buf[t] = (i_samp << 10) | q_samp;
  2856. }
  2857. mod_phy_reg(pi, 0x6d6, (0x3 << 0), 0 << 0);
  2858. mod_phy_reg(pi, 0x6da, (0x1 << 3), 1 << 3);
  2859. tab.tbl_ptr = data_buf;
  2860. tab.tbl_len = num_samps;
  2861. tab.tbl_id = LCNPHY_TBL_ID_SAMPLEPLAY;
  2862. tab.tbl_offset = 0;
  2863. tab.tbl_width = 32;
  2864. wlc_lcnphy_write_table(pi, &tab);
  2865. wlc_lcnphy_run_samples(pi, num_samps, 0xffff, 0, iqcalmode);
  2866. }
  2867. void wlc_lcnphy_stop_tx_tone(struct brcms_phy *pi)
  2868. {
  2869. s16 playback_status;
  2870. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2871. pi->phy_tx_tone_freq = 0;
  2872. if (pi_lcn->lcnphy_spurmod) {
  2873. write_phy_reg(pi, 0x942, 0x7);
  2874. write_phy_reg(pi, 0x93b, 0x2017);
  2875. write_phy_reg(pi, 0x93c, 0x27c5);
  2876. wlc_lcnphy_txrx_spur_avoidance_mode(pi, true);
  2877. }
  2878. playback_status = read_phy_reg(pi, 0x644);
  2879. if (playback_status & (0x1 << 0)) {
  2880. wlc_lcnphy_tx_pu(pi, 0);
  2881. mod_phy_reg(pi, 0x63f, (0x1 << 1), 1 << 1);
  2882. } else if (playback_status & (0x1 << 1))
  2883. mod_phy_reg(pi, 0x453, (0x1 << 15), 0 << 15);
  2884. mod_phy_reg(pi, 0x6d6, (0x3 << 0), 1 << 0);
  2885. mod_phy_reg(pi, 0x6da, (0x1 << 3), 0 << 3);
  2886. mod_phy_reg(pi, 0x6da, (0x1 << 7), 0 << 7);
  2887. and_radio_reg(pi, RADIO_2064_REG112, 0xFFF9);
  2888. wlc_lcnphy_deaf_mode(pi, false);
  2889. }
  2890. static void
  2891. wlc_lcnphy_set_cc(struct brcms_phy *pi, int cal_type, s16 coeff_x, s16 coeff_y)
  2892. {
  2893. u16 di0dq0;
  2894. u16 x, y, data_rf;
  2895. int k;
  2896. switch (cal_type) {
  2897. case 0:
  2898. wlc_lcnphy_set_tx_iqcc(pi, coeff_x, coeff_y);
  2899. break;
  2900. case 2:
  2901. di0dq0 = (coeff_x & 0xff) << 8 | (coeff_y & 0xff);
  2902. wlc_lcnphy_set_tx_locc(pi, di0dq0);
  2903. break;
  2904. case 3:
  2905. k = wlc_lcnphy_calc_floor(coeff_x, 0);
  2906. y = 8 + k;
  2907. k = wlc_lcnphy_calc_floor(coeff_x, 1);
  2908. x = 8 - k;
  2909. data_rf = (x * 16 + y);
  2910. write_radio_reg(pi, RADIO_2064_REG089, data_rf);
  2911. k = wlc_lcnphy_calc_floor(coeff_y, 0);
  2912. y = 8 + k;
  2913. k = wlc_lcnphy_calc_floor(coeff_y, 1);
  2914. x = 8 - k;
  2915. data_rf = (x * 16 + y);
  2916. write_radio_reg(pi, RADIO_2064_REG08A, data_rf);
  2917. break;
  2918. case 4:
  2919. k = wlc_lcnphy_calc_floor(coeff_x, 0);
  2920. y = 8 + k;
  2921. k = wlc_lcnphy_calc_floor(coeff_x, 1);
  2922. x = 8 - k;
  2923. data_rf = (x * 16 + y);
  2924. write_radio_reg(pi, RADIO_2064_REG08B, data_rf);
  2925. k = wlc_lcnphy_calc_floor(coeff_y, 0);
  2926. y = 8 + k;
  2927. k = wlc_lcnphy_calc_floor(coeff_y, 1);
  2928. x = 8 - k;
  2929. data_rf = (x * 16 + y);
  2930. write_radio_reg(pi, RADIO_2064_REG08C, data_rf);
  2931. break;
  2932. }
  2933. }
  2934. static struct lcnphy_unsign16_struct
  2935. wlc_lcnphy_get_cc(struct brcms_phy *pi, int cal_type)
  2936. {
  2937. u16 a, b, didq;
  2938. u8 di0, dq0, ei, eq, fi, fq;
  2939. struct lcnphy_unsign16_struct cc;
  2940. cc.re = 0;
  2941. cc.im = 0;
  2942. switch (cal_type) {
  2943. case 0:
  2944. wlc_lcnphy_get_tx_iqcc(pi, &a, &b);
  2945. cc.re = a;
  2946. cc.im = b;
  2947. break;
  2948. case 2:
  2949. didq = wlc_lcnphy_get_tx_locc(pi);
  2950. di0 = (((didq & 0xff00) << 16) >> 24);
  2951. dq0 = (((didq & 0x00ff) << 24) >> 24);
  2952. cc.re = (u16) di0;
  2953. cc.im = (u16) dq0;
  2954. break;
  2955. case 3:
  2956. wlc_lcnphy_get_radio_loft(pi, &ei, &eq, &fi, &fq);
  2957. cc.re = (u16) ei;
  2958. cc.im = (u16) eq;
  2959. break;
  2960. case 4:
  2961. wlc_lcnphy_get_radio_loft(pi, &ei, &eq, &fi, &fq);
  2962. cc.re = (u16) fi;
  2963. cc.im = (u16) fq;
  2964. break;
  2965. }
  2966. return cc;
  2967. }
  2968. static void
  2969. wlc_lcnphy_samp_cap(struct brcms_phy *pi, int clip_detect_algo, u16 thresh,
  2970. s16 *ptr, int mode)
  2971. {
  2972. u32 curval1, curval2, stpptr, curptr, strptr, val;
  2973. u16 sslpnCalibClkEnCtrl, timer;
  2974. u16 old_sslpnCalibClkEnCtrl;
  2975. s16 imag, real;
  2976. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2977. timer = 0;
  2978. old_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2979. curval1 = bcma_read16(pi->d11core, D11REGOFFS(psm_corectlsts));
  2980. ptr[130] = 0;
  2981. bcma_write16(pi->d11core, D11REGOFFS(psm_corectlsts),
  2982. ((1 << 6) | curval1));
  2983. bcma_write16(pi->d11core, D11REGOFFS(smpl_clct_strptr), 0x7E00);
  2984. bcma_write16(pi->d11core, D11REGOFFS(smpl_clct_stpptr), 0x8000);
  2985. udelay(20);
  2986. curval2 = bcma_read16(pi->d11core, D11REGOFFS(psm_phy_hdr_param));
  2987. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param),
  2988. curval2 | 0x30);
  2989. write_phy_reg(pi, 0x555, 0x0);
  2990. write_phy_reg(pi, 0x5a6, 0x5);
  2991. write_phy_reg(pi, 0x5a2, (u16) (mode | mode << 6));
  2992. write_phy_reg(pi, 0x5cf, 3);
  2993. write_phy_reg(pi, 0x5a5, 0x3);
  2994. write_phy_reg(pi, 0x583, 0x0);
  2995. write_phy_reg(pi, 0x584, 0x0);
  2996. write_phy_reg(pi, 0x585, 0x0fff);
  2997. write_phy_reg(pi, 0x586, 0x0000);
  2998. write_phy_reg(pi, 0x580, 0x4501);
  2999. sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  3000. write_phy_reg(pi, 0x6da, (u32) (sslpnCalibClkEnCtrl | 0x2008));
  3001. stpptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_stpptr));
  3002. curptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_curptr));
  3003. do {
  3004. udelay(10);
  3005. curptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_curptr));
  3006. timer++;
  3007. } while ((curptr != stpptr) && (timer < 500));
  3008. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param), 0x2);
  3009. strptr = 0x7E00;
  3010. bcma_write32(pi->d11core, D11REGOFFS(tplatewrptr), strptr);
  3011. while (strptr < 0x8000) {
  3012. val = bcma_read32(pi->d11core, D11REGOFFS(tplatewrdata));
  3013. imag = ((val >> 16) & 0x3ff);
  3014. real = ((val) & 0x3ff);
  3015. if (imag > 511)
  3016. imag -= 1024;
  3017. if (real > 511)
  3018. real -= 1024;
  3019. if (pi_lcn->lcnphy_iqcal_swp_dis)
  3020. ptr[(strptr - 0x7E00) / 4] = real;
  3021. else
  3022. ptr[(strptr - 0x7E00) / 4] = imag;
  3023. if (clip_detect_algo) {
  3024. if (imag > thresh || imag < -thresh) {
  3025. strptr = 0x8000;
  3026. ptr[130] = 1;
  3027. }
  3028. }
  3029. strptr += 4;
  3030. }
  3031. write_phy_reg(pi, 0x6da, old_sslpnCalibClkEnCtrl);
  3032. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param), curval2);
  3033. bcma_write16(pi->d11core, D11REGOFFS(psm_corectlsts), curval1);
  3034. }
  3035. static void
  3036. wlc_lcnphy_a1(struct brcms_phy *pi, int cal_type, int num_levels,
  3037. int step_size_lg2)
  3038. {
  3039. const struct lcnphy_spb_tone *phy_c1;
  3040. struct lcnphy_spb_tone phy_c2;
  3041. struct lcnphy_unsign16_struct phy_c3;
  3042. int phy_c4, phy_c5, k, l, j, phy_c6;
  3043. u16 phy_c7, phy_c8, phy_c9;
  3044. s16 phy_c10, phy_c11, phy_c12, phy_c13, phy_c14, phy_c15, phy_c16;
  3045. s16 *ptr, phy_c17;
  3046. s32 phy_c18, phy_c19;
  3047. u32 phy_c20, phy_c21;
  3048. bool phy_c22, phy_c23, phy_c24, phy_c25;
  3049. u16 phy_c26, phy_c27;
  3050. u16 phy_c28, phy_c29, phy_c30;
  3051. u16 phy_c31;
  3052. u16 *phy_c32;
  3053. phy_c21 = 0;
  3054. phy_c10 = phy_c13 = phy_c14 = phy_c8 = 0;
  3055. ptr = kmalloc(sizeof(s16) * 131, GFP_ATOMIC);
  3056. if (NULL == ptr)
  3057. return;
  3058. phy_c32 = kmalloc(sizeof(u16) * 20, GFP_ATOMIC);
  3059. if (NULL == phy_c32) {
  3060. kfree(ptr);
  3061. return;
  3062. }
  3063. phy_c26 = read_phy_reg(pi, 0x6da);
  3064. phy_c27 = read_phy_reg(pi, 0x6db);
  3065. phy_c31 = read_radio_reg(pi, RADIO_2064_REG026);
  3066. write_phy_reg(pi, 0x93d, 0xC0);
  3067. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 0);
  3068. write_phy_reg(pi, 0x6da, 0xffff);
  3069. or_phy_reg(pi, 0x6db, 0x3);
  3070. wlc_lcnphy_tx_iqlo_loopback(pi, phy_c32);
  3071. udelay(500);
  3072. phy_c28 = read_phy_reg(pi, 0x938);
  3073. phy_c29 = read_phy_reg(pi, 0x4d7);
  3074. phy_c30 = read_phy_reg(pi, 0x4d8);
  3075. or_phy_reg(pi, 0x938, 0x1 << 2);
  3076. or_phy_reg(pi, 0x4d7, 0x1 << 2);
  3077. or_phy_reg(pi, 0x4d7, 0x1 << 3);
  3078. mod_phy_reg(pi, 0x4d7, (0x7 << 12), 0x2 << 12);
  3079. or_phy_reg(pi, 0x4d8, 1 << 0);
  3080. or_phy_reg(pi, 0x4d8, 1 << 1);
  3081. mod_phy_reg(pi, 0x4d8, (0x3ff << 2), 0x23A << 2);
  3082. mod_phy_reg(pi, 0x4d8, (0x7 << 12), 0x7 << 12);
  3083. phy_c1 = &lcnphy_spb_tone_3750[0];
  3084. phy_c4 = 32;
  3085. if (num_levels == 0) {
  3086. if (cal_type != 0)
  3087. num_levels = 4;
  3088. else
  3089. num_levels = 9;
  3090. }
  3091. if (step_size_lg2 == 0) {
  3092. if (cal_type != 0)
  3093. step_size_lg2 = 3;
  3094. else
  3095. step_size_lg2 = 8;
  3096. }
  3097. phy_c7 = (1 << step_size_lg2);
  3098. phy_c3 = wlc_lcnphy_get_cc(pi, cal_type);
  3099. phy_c15 = (s16) phy_c3.re;
  3100. phy_c16 = (s16) phy_c3.im;
  3101. if (cal_type == 2) {
  3102. if (phy_c3.re > 127)
  3103. phy_c15 = phy_c3.re - 256;
  3104. if (phy_c3.im > 127)
  3105. phy_c16 = phy_c3.im - 256;
  3106. }
  3107. wlc_lcnphy_set_cc(pi, cal_type, phy_c15, phy_c16);
  3108. udelay(20);
  3109. for (phy_c8 = 0; phy_c7 != 0 && phy_c8 < num_levels; phy_c8++) {
  3110. phy_c23 = true;
  3111. phy_c22 = false;
  3112. switch (cal_type) {
  3113. case 0:
  3114. phy_c10 = 511;
  3115. break;
  3116. case 2:
  3117. phy_c10 = 127;
  3118. break;
  3119. case 3:
  3120. phy_c10 = 15;
  3121. break;
  3122. case 4:
  3123. phy_c10 = 15;
  3124. break;
  3125. }
  3126. phy_c9 = read_phy_reg(pi, 0x93d);
  3127. phy_c9 = 2 * phy_c9;
  3128. phy_c24 = false;
  3129. phy_c5 = 7;
  3130. phy_c25 = true;
  3131. while (1) {
  3132. write_radio_reg(pi, RADIO_2064_REG026,
  3133. (phy_c5 & 0x7) | ((phy_c5 & 0x7) << 4));
  3134. udelay(50);
  3135. phy_c22 = false;
  3136. ptr[130] = 0;
  3137. wlc_lcnphy_samp_cap(pi, 1, phy_c9, &ptr[0], 2);
  3138. if (ptr[130] == 1)
  3139. phy_c22 = true;
  3140. if (phy_c22)
  3141. phy_c5 -= 1;
  3142. if ((phy_c22 != phy_c24) && (!phy_c25))
  3143. break;
  3144. if (!phy_c22)
  3145. phy_c5 += 1;
  3146. if (phy_c5 <= 0 || phy_c5 >= 7)
  3147. break;
  3148. phy_c24 = phy_c22;
  3149. phy_c25 = false;
  3150. }
  3151. if (phy_c5 < 0)
  3152. phy_c5 = 0;
  3153. else if (phy_c5 > 7)
  3154. phy_c5 = 7;
  3155. for (k = -phy_c7; k <= phy_c7; k += phy_c7) {
  3156. for (l = -phy_c7; l <= phy_c7; l += phy_c7) {
  3157. phy_c11 = phy_c15 + k;
  3158. phy_c12 = phy_c16 + l;
  3159. if (phy_c11 < -phy_c10)
  3160. phy_c11 = -phy_c10;
  3161. else if (phy_c11 > phy_c10)
  3162. phy_c11 = phy_c10;
  3163. if (phy_c12 < -phy_c10)
  3164. phy_c12 = -phy_c10;
  3165. else if (phy_c12 > phy_c10)
  3166. phy_c12 = phy_c10;
  3167. wlc_lcnphy_set_cc(pi, cal_type, phy_c11,
  3168. phy_c12);
  3169. udelay(20);
  3170. wlc_lcnphy_samp_cap(pi, 0, 0, ptr, 2);
  3171. phy_c18 = 0;
  3172. phy_c19 = 0;
  3173. for (j = 0; j < 128; j++) {
  3174. if (cal_type != 0)
  3175. phy_c6 = j % phy_c4;
  3176. else
  3177. phy_c6 = (2 * j) % phy_c4;
  3178. phy_c2.re = phy_c1[phy_c6].re;
  3179. phy_c2.im = phy_c1[phy_c6].im;
  3180. phy_c17 = ptr[j];
  3181. phy_c18 = phy_c18 + phy_c17 * phy_c2.re;
  3182. phy_c19 = phy_c19 + phy_c17 * phy_c2.im;
  3183. }
  3184. phy_c18 = phy_c18 >> 10;
  3185. phy_c19 = phy_c19 >> 10;
  3186. phy_c20 = ((phy_c18 * phy_c18) +
  3187. (phy_c19 * phy_c19));
  3188. if (phy_c23 || phy_c20 < phy_c21) {
  3189. phy_c21 = phy_c20;
  3190. phy_c13 = phy_c11;
  3191. phy_c14 = phy_c12;
  3192. }
  3193. phy_c23 = false;
  3194. }
  3195. }
  3196. phy_c23 = true;
  3197. phy_c15 = phy_c13;
  3198. phy_c16 = phy_c14;
  3199. phy_c7 = phy_c7 >> 1;
  3200. wlc_lcnphy_set_cc(pi, cal_type, phy_c15, phy_c16);
  3201. udelay(20);
  3202. }
  3203. goto cleanup;
  3204. cleanup:
  3205. wlc_lcnphy_tx_iqlo_loopback_cleanup(pi, phy_c32);
  3206. wlc_lcnphy_stop_tx_tone(pi);
  3207. write_phy_reg(pi, 0x6da, phy_c26);
  3208. write_phy_reg(pi, 0x6db, phy_c27);
  3209. write_phy_reg(pi, 0x938, phy_c28);
  3210. write_phy_reg(pi, 0x4d7, phy_c29);
  3211. write_phy_reg(pi, 0x4d8, phy_c30);
  3212. write_radio_reg(pi, RADIO_2064_REG026, phy_c31);
  3213. kfree(phy_c32);
  3214. kfree(ptr);
  3215. }
  3216. void wlc_lcnphy_get_tx_iqcc(struct brcms_phy *pi, u16 *a, u16 *b)
  3217. {
  3218. u16 iqcc[2];
  3219. struct phytbl_info tab;
  3220. tab.tbl_ptr = iqcc;
  3221. tab.tbl_len = 2;
  3222. tab.tbl_id = 0;
  3223. tab.tbl_offset = 80;
  3224. tab.tbl_width = 16;
  3225. wlc_lcnphy_read_table(pi, &tab);
  3226. *a = iqcc[0];
  3227. *b = iqcc[1];
  3228. }
  3229. static void wlc_lcnphy_tx_iqlo_soft_cal_full(struct brcms_phy *pi)
  3230. {
  3231. struct lcnphy_unsign16_struct iqcc0, locc2, locc3, locc4;
  3232. wlc_lcnphy_set_cc(pi, 0, 0, 0);
  3233. wlc_lcnphy_set_cc(pi, 2, 0, 0);
  3234. wlc_lcnphy_set_cc(pi, 3, 0, 0);
  3235. wlc_lcnphy_set_cc(pi, 4, 0, 0);
  3236. wlc_lcnphy_a1(pi, 4, 0, 0);
  3237. wlc_lcnphy_a1(pi, 3, 0, 0);
  3238. wlc_lcnphy_a1(pi, 2, 3, 2);
  3239. wlc_lcnphy_a1(pi, 0, 5, 8);
  3240. wlc_lcnphy_a1(pi, 2, 2, 1);
  3241. wlc_lcnphy_a1(pi, 0, 4, 3);
  3242. iqcc0 = wlc_lcnphy_get_cc(pi, 0);
  3243. locc2 = wlc_lcnphy_get_cc(pi, 2);
  3244. locc3 = wlc_lcnphy_get_cc(pi, 3);
  3245. locc4 = wlc_lcnphy_get_cc(pi, 4);
  3246. }
  3247. u16 wlc_lcnphy_get_tx_locc(struct brcms_phy *pi)
  3248. {
  3249. struct phytbl_info tab;
  3250. u16 didq;
  3251. tab.tbl_id = 0;
  3252. tab.tbl_width = 16;
  3253. tab.tbl_ptr = &didq;
  3254. tab.tbl_len = 1;
  3255. tab.tbl_offset = 85;
  3256. wlc_lcnphy_read_table(pi, &tab);
  3257. return didq;
  3258. }
  3259. static void wlc_lcnphy_txpwrtbl_iqlo_cal(struct brcms_phy *pi)
  3260. {
  3261. struct lcnphy_txgains target_gains, old_gains;
  3262. u8 save_bb_mult;
  3263. u16 a, b, didq, save_pa_gain = 0;
  3264. uint idx, SAVE_txpwrindex = 0xFF;
  3265. u32 val;
  3266. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3267. struct phytbl_info tab;
  3268. u8 ei0, eq0, fi0, fq0;
  3269. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3270. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  3271. save_pa_gain = wlc_lcnphy_get_pa_gain(pi);
  3272. save_bb_mult = wlc_lcnphy_get_bbmult(pi);
  3273. if (SAVE_txpwrctrl == LCNPHY_TX_PWR_CTRL_OFF)
  3274. SAVE_txpwrindex = wlc_lcnphy_get_current_tx_pwr_idx(pi);
  3275. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  3276. target_gains.gm_gain = 7;
  3277. target_gains.pga_gain = 0;
  3278. target_gains.pad_gain = 21;
  3279. target_gains.dac_gain = 0;
  3280. wlc_lcnphy_set_tx_gain(pi, &target_gains);
  3281. if (LCNREV_IS(pi->pubpi.phy_rev, 1) || pi_lcn->lcnphy_hw_iqcal_en) {
  3282. wlc_lcnphy_set_tx_pwr_by_index(pi, 30);
  3283. wlc_lcnphy_tx_iqlo_cal(pi, &target_gains,
  3284. (pi_lcn->
  3285. lcnphy_recal ? LCNPHY_CAL_RECAL :
  3286. LCNPHY_CAL_FULL), false);
  3287. } else {
  3288. wlc_lcnphy_set_tx_pwr_by_index(pi, 16);
  3289. wlc_lcnphy_tx_iqlo_soft_cal_full(pi);
  3290. }
  3291. wlc_lcnphy_get_radio_loft(pi, &ei0, &eq0, &fi0, &fq0);
  3292. if ((abs((s8) fi0) == 15) && (abs((s8) fq0) == 15)) {
  3293. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  3294. target_gains.gm_gain = 255;
  3295. target_gains.pga_gain = 255;
  3296. target_gains.pad_gain = 0xf0;
  3297. target_gains.dac_gain = 0;
  3298. } else {
  3299. target_gains.gm_gain = 7;
  3300. target_gains.pga_gain = 45;
  3301. target_gains.pad_gain = 186;
  3302. target_gains.dac_gain = 0;
  3303. }
  3304. if (LCNREV_IS(pi->pubpi.phy_rev, 1)
  3305. || pi_lcn->lcnphy_hw_iqcal_en) {
  3306. target_gains.pga_gain = 0;
  3307. target_gains.pad_gain = 30;
  3308. wlc_lcnphy_set_tx_pwr_by_index(pi, 16);
  3309. wlc_lcnphy_tx_iqlo_cal(pi, &target_gains,
  3310. LCNPHY_CAL_FULL, false);
  3311. } else {
  3312. wlc_lcnphy_tx_iqlo_soft_cal_full(pi);
  3313. }
  3314. }
  3315. wlc_lcnphy_get_tx_iqcc(pi, &a, &b);
  3316. didq = wlc_lcnphy_get_tx_locc(pi);
  3317. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3318. tab.tbl_width = 32;
  3319. tab.tbl_ptr = &val;
  3320. tab.tbl_len = 1;
  3321. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  3322. for (idx = 0; idx < 128; idx++) {
  3323. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + idx;
  3324. wlc_lcnphy_read_table(pi, &tab);
  3325. val = (val & 0xfff00000) |
  3326. ((u32) (a & 0x3FF) << 10) | (b & 0x3ff);
  3327. wlc_lcnphy_write_table(pi, &tab);
  3328. val = didq;
  3329. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_LO_OFFSET + idx;
  3330. wlc_lcnphy_write_table(pi, &tab);
  3331. }
  3332. pi_lcn->lcnphy_cal_results.txiqlocal_a = a;
  3333. pi_lcn->lcnphy_cal_results.txiqlocal_b = b;
  3334. pi_lcn->lcnphy_cal_results.txiqlocal_didq = didq;
  3335. pi_lcn->lcnphy_cal_results.txiqlocal_ei0 = ei0;
  3336. pi_lcn->lcnphy_cal_results.txiqlocal_eq0 = eq0;
  3337. pi_lcn->lcnphy_cal_results.txiqlocal_fi0 = fi0;
  3338. pi_lcn->lcnphy_cal_results.txiqlocal_fq0 = fq0;
  3339. wlc_lcnphy_set_bbmult(pi, save_bb_mult);
  3340. wlc_lcnphy_set_pa_gain(pi, save_pa_gain);
  3341. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  3342. if (SAVE_txpwrctrl != LCNPHY_TX_PWR_CTRL_OFF)
  3343. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  3344. else
  3345. wlc_lcnphy_set_tx_pwr_by_index(pi, SAVE_txpwrindex);
  3346. }
  3347. s16 wlc_lcnphy_tempsense_new(struct brcms_phy *pi, bool mode)
  3348. {
  3349. u16 tempsenseval1, tempsenseval2;
  3350. s16 avg = 0;
  3351. bool suspend = false;
  3352. if (mode == 1) {
  3353. suspend = (0 == (bcma_read32(pi->d11core,
  3354. D11REGOFFS(maccontrol)) &
  3355. MCTL_EN_MAC));
  3356. if (!suspend)
  3357. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3358. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  3359. }
  3360. tempsenseval1 = read_phy_reg(pi, 0x476) & 0x1FF;
  3361. tempsenseval2 = read_phy_reg(pi, 0x477) & 0x1FF;
  3362. if (tempsenseval1 > 255)
  3363. avg = (s16) (tempsenseval1 - 512);
  3364. else
  3365. avg = (s16) tempsenseval1;
  3366. if (tempsenseval2 > 255)
  3367. avg += (s16) (tempsenseval2 - 512);
  3368. else
  3369. avg += (s16) tempsenseval2;
  3370. avg /= 2;
  3371. if (mode == 1) {
  3372. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  3373. udelay(100);
  3374. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  3375. if (!suspend)
  3376. wlapi_enable_mac(pi->sh->physhim);
  3377. }
  3378. return avg;
  3379. }
  3380. u16 wlc_lcnphy_tempsense(struct brcms_phy *pi, bool mode)
  3381. {
  3382. u16 tempsenseval1, tempsenseval2;
  3383. s32 avg = 0;
  3384. bool suspend = false;
  3385. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3386. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3387. if (mode == 1) {
  3388. suspend = (0 == (bcma_read32(pi->d11core,
  3389. D11REGOFFS(maccontrol)) &
  3390. MCTL_EN_MAC));
  3391. if (!suspend)
  3392. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3393. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  3394. }
  3395. tempsenseval1 = read_phy_reg(pi, 0x476) & 0x1FF;
  3396. tempsenseval2 = read_phy_reg(pi, 0x477) & 0x1FF;
  3397. if (tempsenseval1 > 255)
  3398. avg = (int)(tempsenseval1 - 512);
  3399. else
  3400. avg = (int)tempsenseval1;
  3401. if (pi_lcn->lcnphy_tempsense_option == 1 || pi->hwpwrctrl_capable) {
  3402. if (tempsenseval2 > 255)
  3403. avg = (int)(avg - tempsenseval2 + 512);
  3404. else
  3405. avg = (int)(avg - tempsenseval2);
  3406. } else {
  3407. if (tempsenseval2 > 255)
  3408. avg = (int)(avg + tempsenseval2 - 512);
  3409. else
  3410. avg = (int)(avg + tempsenseval2);
  3411. avg = avg / 2;
  3412. }
  3413. if (avg < 0)
  3414. avg = avg + 512;
  3415. if (pi_lcn->lcnphy_tempsense_option == 2)
  3416. avg = tempsenseval1;
  3417. if (mode)
  3418. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  3419. if (mode == 1) {
  3420. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  3421. udelay(100);
  3422. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  3423. if (!suspend)
  3424. wlapi_enable_mac(pi->sh->physhim);
  3425. }
  3426. return (u16) avg;
  3427. }
  3428. s8 wlc_lcnphy_tempsense_degree(struct brcms_phy *pi, bool mode)
  3429. {
  3430. s32 degree = wlc_lcnphy_tempsense_new(pi, mode);
  3431. degree =
  3432. ((degree <<
  3433. 10) + LCN_TEMPSENSE_OFFSET + (LCN_TEMPSENSE_DEN >> 1))
  3434. / LCN_TEMPSENSE_DEN;
  3435. return (s8) degree;
  3436. }
  3437. s8 wlc_lcnphy_vbatsense(struct brcms_phy *pi, bool mode)
  3438. {
  3439. u16 vbatsenseval;
  3440. s32 avg = 0;
  3441. bool suspend = false;
  3442. if (mode == 1) {
  3443. suspend = (0 == (bcma_read32(pi->d11core,
  3444. D11REGOFFS(maccontrol)) &
  3445. MCTL_EN_MAC));
  3446. if (!suspend)
  3447. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3448. wlc_lcnphy_vbat_temp_sense_setup(pi, VBATSENSE);
  3449. }
  3450. vbatsenseval = read_phy_reg(pi, 0x475) & 0x1FF;
  3451. if (vbatsenseval > 255)
  3452. avg = (s32) (vbatsenseval - 512);
  3453. else
  3454. avg = (s32) vbatsenseval;
  3455. avg = (avg * LCN_VBAT_SCALE_NOM +
  3456. (LCN_VBAT_SCALE_DEN >> 1)) / LCN_VBAT_SCALE_DEN;
  3457. if (mode == 1) {
  3458. if (!suspend)
  3459. wlapi_enable_mac(pi->sh->physhim);
  3460. }
  3461. return (s8) avg;
  3462. }
  3463. static void wlc_lcnphy_afe_clk_init(struct brcms_phy *pi, u8 mode)
  3464. {
  3465. u8 phybw40;
  3466. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3467. mod_phy_reg(pi, 0x6d1, (0x1 << 7), (1) << 7);
  3468. if (((mode == AFE_CLK_INIT_MODE_PAPD) && (phybw40 == 0)) ||
  3469. (mode == AFE_CLK_INIT_MODE_TXRX2X))
  3470. write_phy_reg(pi, 0x6d0, 0x7);
  3471. wlc_lcnphy_toggle_afe_pwdn(pi);
  3472. }
  3473. static void wlc_lcnphy_temp_adj(struct brcms_phy *pi)
  3474. {
  3475. }
  3476. static void wlc_lcnphy_glacial_timer_based_cal(struct brcms_phy *pi)
  3477. {
  3478. bool suspend;
  3479. s8 index;
  3480. u16 SAVE_pwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3481. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3482. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  3483. MCTL_EN_MAC));
  3484. if (!suspend)
  3485. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3486. wlc_lcnphy_deaf_mode(pi, true);
  3487. pi->phy_lastcal = pi->sh->now;
  3488. pi->phy_forcecal = false;
  3489. index = pi_lcn->lcnphy_current_index;
  3490. wlc_lcnphy_txpwrtbl_iqlo_cal(pi);
  3491. wlc_lcnphy_set_tx_pwr_by_index(pi, index);
  3492. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_pwrctrl);
  3493. wlc_lcnphy_deaf_mode(pi, false);
  3494. if (!suspend)
  3495. wlapi_enable_mac(pi->sh->physhim);
  3496. }
  3497. static void wlc_lcnphy_periodic_cal(struct brcms_phy *pi)
  3498. {
  3499. bool suspend, full_cal;
  3500. const struct lcnphy_rx_iqcomp *rx_iqcomp;
  3501. int rx_iqcomp_sz;
  3502. u16 SAVE_pwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3503. s8 index;
  3504. struct phytbl_info tab;
  3505. s32 a1, b0, b1;
  3506. s32 tssi, pwr, maxtargetpwr, mintargetpwr;
  3507. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3508. pi->phy_lastcal = pi->sh->now;
  3509. pi->phy_forcecal = false;
  3510. full_cal =
  3511. (pi_lcn->lcnphy_full_cal_channel !=
  3512. CHSPEC_CHANNEL(pi->radio_chanspec));
  3513. pi_lcn->lcnphy_full_cal_channel = CHSPEC_CHANNEL(pi->radio_chanspec);
  3514. index = pi_lcn->lcnphy_current_index;
  3515. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  3516. MCTL_EN_MAC));
  3517. if (!suspend) {
  3518. wlapi_bmac_write_shm(pi->sh->physhim, M_CTS_DURATION, 10000);
  3519. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3520. }
  3521. wlc_lcnphy_deaf_mode(pi, true);
  3522. wlc_lcnphy_txpwrtbl_iqlo_cal(pi);
  3523. rx_iqcomp = lcnphy_rx_iqcomp_table_rev0;
  3524. rx_iqcomp_sz = ARRAY_SIZE(lcnphy_rx_iqcomp_table_rev0);
  3525. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  3526. wlc_lcnphy_rx_iq_cal(pi, NULL, 0, true, false, 1, 40);
  3527. else
  3528. wlc_lcnphy_rx_iq_cal(pi, NULL, 0, true, false, 1, 127);
  3529. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi)) {
  3530. wlc_lcnphy_idle_tssi_est((struct brcms_phy_pub *) pi);
  3531. b0 = pi->txpa_2g[0];
  3532. b1 = pi->txpa_2g[1];
  3533. a1 = pi->txpa_2g[2];
  3534. maxtargetpwr = wlc_lcnphy_tssi2dbm(10, a1, b0, b1);
  3535. mintargetpwr = wlc_lcnphy_tssi2dbm(125, a1, b0, b1);
  3536. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3537. tab.tbl_width = 32;
  3538. tab.tbl_ptr = &pwr;
  3539. tab.tbl_len = 1;
  3540. tab.tbl_offset = 0;
  3541. for (tssi = 0; tssi < 128; tssi++) {
  3542. pwr = wlc_lcnphy_tssi2dbm(tssi, a1, b0, b1);
  3543. pwr = (pwr < mintargetpwr) ? mintargetpwr : pwr;
  3544. wlc_lcnphy_write_table(pi, &tab);
  3545. tab.tbl_offset++;
  3546. }
  3547. }
  3548. wlc_lcnphy_set_tx_pwr_by_index(pi, index);
  3549. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_pwrctrl);
  3550. wlc_lcnphy_deaf_mode(pi, false);
  3551. if (!suspend)
  3552. wlapi_enable_mac(pi->sh->physhim);
  3553. }
  3554. void wlc_lcnphy_calib_modes(struct brcms_phy *pi, uint mode)
  3555. {
  3556. u16 temp_new;
  3557. int temp1, temp2, temp_diff;
  3558. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3559. switch (mode) {
  3560. case PHY_PERICAL_CHAN:
  3561. break;
  3562. case PHY_FULLCAL:
  3563. wlc_lcnphy_periodic_cal(pi);
  3564. break;
  3565. case PHY_PERICAL_PHYINIT:
  3566. wlc_lcnphy_periodic_cal(pi);
  3567. break;
  3568. case PHY_PERICAL_WATCHDOG:
  3569. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  3570. temp_new = wlc_lcnphy_tempsense(pi, 0);
  3571. temp1 = LCNPHY_TEMPSENSE(temp_new);
  3572. temp2 = LCNPHY_TEMPSENSE(pi_lcn->lcnphy_cal_temper);
  3573. temp_diff = temp1 - temp2;
  3574. if ((pi_lcn->lcnphy_cal_counter > 90) ||
  3575. (temp_diff > 60) || (temp_diff < -60)) {
  3576. wlc_lcnphy_glacial_timer_based_cal(pi);
  3577. wlc_2064_vco_cal(pi);
  3578. pi_lcn->lcnphy_cal_temper = temp_new;
  3579. pi_lcn->lcnphy_cal_counter = 0;
  3580. } else
  3581. pi_lcn->lcnphy_cal_counter++;
  3582. }
  3583. break;
  3584. case LCNPHY_PERICAL_TEMPBASED_TXPWRCTRL:
  3585. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  3586. wlc_lcnphy_tx_power_adjustment(
  3587. (struct brcms_phy_pub *) pi);
  3588. break;
  3589. }
  3590. }
  3591. void wlc_lcnphy_get_tssi(struct brcms_phy *pi, s8 *ofdm_pwr, s8 *cck_pwr)
  3592. {
  3593. s8 cck_offset;
  3594. u16 status;
  3595. status = (read_phy_reg(pi, 0x4ab));
  3596. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi) &&
  3597. (status & (0x1 << 15))) {
  3598. *ofdm_pwr = (s8) (((read_phy_reg(pi, 0x4ab) & (0x1ff << 0))
  3599. >> 0) >> 1);
  3600. if (wlc_phy_tpc_isenabled_lcnphy(pi))
  3601. cck_offset = pi->tx_power_offset[TXP_FIRST_CCK];
  3602. else
  3603. cck_offset = 0;
  3604. *cck_pwr = *ofdm_pwr + cck_offset;
  3605. } else {
  3606. *cck_pwr = 0;
  3607. *ofdm_pwr = 0;
  3608. }
  3609. }
  3610. void wlc_phy_cal_init_lcnphy(struct brcms_phy *pi)
  3611. {
  3612. return;
  3613. }
  3614. void wlc_lcnphy_tx_power_adjustment(struct brcms_phy_pub *ppi)
  3615. {
  3616. s8 index;
  3617. u16 index2;
  3618. struct brcms_phy *pi = container_of(ppi, struct brcms_phy, pubpi_ro);
  3619. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3620. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3621. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi) &&
  3622. SAVE_txpwrctrl) {
  3623. index = wlc_lcnphy_tempcompensated_txpwrctrl(pi);
  3624. index2 = (u16) (index * 2);
  3625. mod_phy_reg(pi, 0x4a9, (0x1ff << 0), (index2) << 0);
  3626. pi_lcn->lcnphy_current_index =
  3627. (s8)((read_phy_reg(pi, 0x4a9) & 0xFF) / 2);
  3628. }
  3629. }
  3630. static void
  3631. wlc_lcnphy_load_tx_gain_table(struct brcms_phy *pi,
  3632. const struct lcnphy_tx_gain_tbl_entry *gain_table)
  3633. {
  3634. u32 j;
  3635. struct phytbl_info tab;
  3636. u32 val;
  3637. u16 pa_gain;
  3638. u16 gm_gain;
  3639. if (pi->sh->boardflags & BFL_FEM)
  3640. pa_gain = 0x10;
  3641. else
  3642. pa_gain = 0x60;
  3643. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3644. tab.tbl_width = 32;
  3645. tab.tbl_len = 1;
  3646. tab.tbl_ptr = &val;
  3647. /* fixed gm_gain value for iPA */
  3648. gm_gain = 15;
  3649. for (j = 0; j < 128; j++) {
  3650. if (pi->sh->boardflags & BFL_FEM)
  3651. gm_gain = gain_table[j].gm;
  3652. val = (((u32) pa_gain << 24) |
  3653. (gain_table[j].pad << 16) |
  3654. (gain_table[j].pga << 8) | gm_gain);
  3655. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + j;
  3656. wlc_lcnphy_write_table(pi, &tab);
  3657. val = (gain_table[j].dac << 28) | (gain_table[j].bb_mult << 20);
  3658. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + j;
  3659. wlc_lcnphy_write_table(pi, &tab);
  3660. }
  3661. }
  3662. static void wlc_lcnphy_load_rfpower(struct brcms_phy *pi)
  3663. {
  3664. struct phytbl_info tab;
  3665. u32 val, bbmult, rfgain;
  3666. u8 index;
  3667. u8 scale_factor = 1;
  3668. s16 temp, temp1, temp2, qQ, qQ1, qQ2, shift;
  3669. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3670. tab.tbl_width = 32;
  3671. tab.tbl_len = 1;
  3672. for (index = 0; index < 128; index++) {
  3673. tab.tbl_ptr = &bbmult;
  3674. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + index;
  3675. wlc_lcnphy_read_table(pi, &tab);
  3676. bbmult = bbmult >> 20;
  3677. tab.tbl_ptr = &rfgain;
  3678. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + index;
  3679. wlc_lcnphy_read_table(pi, &tab);
  3680. qm_log10((s32) (bbmult), 0, &temp1, &qQ1);
  3681. qm_log10((s32) (1 << 6), 0, &temp2, &qQ2);
  3682. if (qQ1 < qQ2) {
  3683. temp2 = qm_shr16(temp2, qQ2 - qQ1);
  3684. qQ = qQ1;
  3685. } else {
  3686. temp1 = qm_shr16(temp1, qQ1 - qQ2);
  3687. qQ = qQ2;
  3688. }
  3689. temp = qm_sub16(temp1, temp2);
  3690. if (qQ >= 4)
  3691. shift = qQ - 4;
  3692. else
  3693. shift = 4 - qQ;
  3694. val = (((index << shift) + (5 * temp) +
  3695. (1 << (scale_factor + shift - 3))) >> (scale_factor +
  3696. shift - 2));
  3697. tab.tbl_ptr = &val;
  3698. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_PWR_OFFSET + index;
  3699. wlc_lcnphy_write_table(pi, &tab);
  3700. }
  3701. }
  3702. static void wlc_lcnphy_bu_tweaks(struct brcms_phy *pi)
  3703. {
  3704. or_phy_reg(pi, 0x805, 0x1);
  3705. mod_phy_reg(pi, 0x42f, (0x7 << 0), (0x3) << 0);
  3706. mod_phy_reg(pi, 0x030, (0x7 << 0), (0x3) << 0);
  3707. write_phy_reg(pi, 0x414, 0x1e10);
  3708. write_phy_reg(pi, 0x415, 0x0640);
  3709. mod_phy_reg(pi, 0x4df, (0xff << 8), -9 << 8);
  3710. or_phy_reg(pi, 0x44a, 0x44);
  3711. write_phy_reg(pi, 0x44a, 0x80);
  3712. mod_phy_reg(pi, 0x434, (0xff << 0), (0xFD) << 0);
  3713. mod_phy_reg(pi, 0x420, (0xff << 0), (16) << 0);
  3714. if (!(pi->sh->boardrev < 0x1204))
  3715. mod_radio_reg(pi, RADIO_2064_REG09B, 0xF0, 0xF0);
  3716. write_phy_reg(pi, 0x7d6, 0x0902);
  3717. mod_phy_reg(pi, 0x429, (0xf << 0), (0x9) << 0);
  3718. mod_phy_reg(pi, 0x429, (0x3f << 4), (0xe) << 4);
  3719. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  3720. mod_phy_reg(pi, 0x423, (0xff << 0), (0x46) << 0);
  3721. mod_phy_reg(pi, 0x411, (0xff << 0), (1) << 0);
  3722. mod_phy_reg(pi, 0x434, (0xff << 0), (0xFF) << 0);
  3723. mod_phy_reg(pi, 0x656, (0xf << 0), (2) << 0);
  3724. mod_phy_reg(pi, 0x44d, (0x1 << 2), (1) << 2);
  3725. mod_radio_reg(pi, RADIO_2064_REG0F7, 0x4, 0x4);
  3726. mod_radio_reg(pi, RADIO_2064_REG0F1, 0x3, 0);
  3727. mod_radio_reg(pi, RADIO_2064_REG0F2, 0xF8, 0x90);
  3728. mod_radio_reg(pi, RADIO_2064_REG0F3, 0x3, 0x2);
  3729. mod_radio_reg(pi, RADIO_2064_REG0F3, 0xf0, 0xa0);
  3730. mod_radio_reg(pi, RADIO_2064_REG11F, 0x2, 0x2);
  3731. wlc_lcnphy_clear_tx_power_offsets(pi);
  3732. mod_phy_reg(pi, 0x4d0, (0x1ff << 6), (10) << 6);
  3733. }
  3734. }
  3735. static void wlc_lcnphy_rcal(struct brcms_phy *pi)
  3736. {
  3737. u8 rcal_value;
  3738. and_radio_reg(pi, RADIO_2064_REG05B, 0xfD);
  3739. or_radio_reg(pi, RADIO_2064_REG004, 0x40);
  3740. or_radio_reg(pi, RADIO_2064_REG120, 0x10);
  3741. or_radio_reg(pi, RADIO_2064_REG078, 0x80);
  3742. or_radio_reg(pi, RADIO_2064_REG129, 0x02);
  3743. or_radio_reg(pi, RADIO_2064_REG057, 0x01);
  3744. or_radio_reg(pi, RADIO_2064_REG05B, 0x02);
  3745. mdelay(5);
  3746. SPINWAIT(!wlc_radio_2064_rcal_done(pi), 10 * 1000 * 1000);
  3747. if (wlc_radio_2064_rcal_done(pi)) {
  3748. rcal_value = (u8) read_radio_reg(pi, RADIO_2064_REG05C);
  3749. rcal_value = rcal_value & 0x1f;
  3750. }
  3751. and_radio_reg(pi, RADIO_2064_REG05B, 0xfD);
  3752. and_radio_reg(pi, RADIO_2064_REG057, 0xFE);
  3753. }
  3754. static void wlc_lcnphy_rc_cal(struct brcms_phy *pi)
  3755. {
  3756. u8 dflt_rc_cal_val;
  3757. u16 flt_val;
  3758. dflt_rc_cal_val = 7;
  3759. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  3760. dflt_rc_cal_val = 11;
  3761. flt_val =
  3762. (dflt_rc_cal_val << 10) | (dflt_rc_cal_val << 5) |
  3763. (dflt_rc_cal_val);
  3764. write_phy_reg(pi, 0x933, flt_val);
  3765. write_phy_reg(pi, 0x934, flt_val);
  3766. write_phy_reg(pi, 0x935, flt_val);
  3767. write_phy_reg(pi, 0x936, flt_val);
  3768. write_phy_reg(pi, 0x937, (flt_val & 0x1FF));
  3769. return;
  3770. }
  3771. static void wlc_radio_2064_init(struct brcms_phy *pi)
  3772. {
  3773. u32 i;
  3774. const struct lcnphy_radio_regs *lcnphyregs = NULL;
  3775. lcnphyregs = lcnphy_radio_regs_2064;
  3776. for (i = 0; lcnphyregs[i].address != 0xffff; i++)
  3777. if (CHSPEC_IS5G(pi->radio_chanspec) && lcnphyregs[i].do_init_a)
  3778. write_radio_reg(pi,
  3779. ((lcnphyregs[i].address & 0x3fff) |
  3780. RADIO_DEFAULT_CORE),
  3781. (u16) lcnphyregs[i].init_a);
  3782. else if (lcnphyregs[i].do_init_g)
  3783. write_radio_reg(pi,
  3784. ((lcnphyregs[i].address & 0x3fff) |
  3785. RADIO_DEFAULT_CORE),
  3786. (u16) lcnphyregs[i].init_g);
  3787. write_radio_reg(pi, RADIO_2064_REG032, 0x62);
  3788. write_radio_reg(pi, RADIO_2064_REG033, 0x19);
  3789. write_radio_reg(pi, RADIO_2064_REG090, 0x10);
  3790. write_radio_reg(pi, RADIO_2064_REG010, 0x00);
  3791. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  3792. write_radio_reg(pi, RADIO_2064_REG060, 0x7f);
  3793. write_radio_reg(pi, RADIO_2064_REG061, 0x72);
  3794. write_radio_reg(pi, RADIO_2064_REG062, 0x7f);
  3795. }
  3796. write_radio_reg(pi, RADIO_2064_REG01D, 0x02);
  3797. write_radio_reg(pi, RADIO_2064_REG01E, 0x06);
  3798. mod_phy_reg(pi, 0x4ea, (0x7 << 0), 0 << 0);
  3799. mod_phy_reg(pi, 0x4ea, (0x7 << 3), 1 << 3);
  3800. mod_phy_reg(pi, 0x4ea, (0x7 << 6), 2 << 6);
  3801. mod_phy_reg(pi, 0x4ea, (0x7 << 9), 3 << 9);
  3802. mod_phy_reg(pi, 0x4ea, (0x7 << 12), 4 << 12);
  3803. write_phy_reg(pi, 0x4ea, 0x4688);
  3804. if (pi->sh->boardflags & BFL_FEM)
  3805. mod_phy_reg(pi, 0x4eb, (0x7 << 0), 2 << 0);
  3806. else
  3807. mod_phy_reg(pi, 0x4eb, (0x7 << 0), 3 << 0);
  3808. mod_phy_reg(pi, 0x4eb, (0x7 << 6), 0 << 6);
  3809. mod_phy_reg(pi, 0x46a, (0xffff << 0), 25 << 0);
  3810. wlc_lcnphy_set_tx_locc(pi, 0);
  3811. wlc_lcnphy_rcal(pi);
  3812. wlc_lcnphy_rc_cal(pi);
  3813. if (!(pi->sh->boardflags & BFL_FEM)) {
  3814. write_radio_reg(pi, RADIO_2064_REG032, 0x6f);
  3815. write_radio_reg(pi, RADIO_2064_REG033, 0x19);
  3816. write_radio_reg(pi, RADIO_2064_REG039, 0xe);
  3817. }
  3818. }
  3819. static void wlc_lcnphy_radio_init(struct brcms_phy *pi)
  3820. {
  3821. wlc_radio_2064_init(pi);
  3822. }
  3823. static void wlc_lcnphy_tbl_init(struct brcms_phy *pi)
  3824. {
  3825. uint idx;
  3826. u8 phybw40;
  3827. struct phytbl_info tab;
  3828. const struct phytbl_info *tb;
  3829. u32 val;
  3830. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3831. for (idx = 0; idx < dot11lcnphytbl_info_sz_rev0; idx++)
  3832. wlc_lcnphy_write_table(pi, &dot11lcnphytbl_info_rev0[idx]);
  3833. if (pi->sh->boardflags & BFL_FEM_BT) {
  3834. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  3835. tab.tbl_width = 16;
  3836. tab.tbl_ptr = &val;
  3837. tab.tbl_len = 1;
  3838. val = 100;
  3839. tab.tbl_offset = 4;
  3840. wlc_lcnphy_write_table(pi, &tab);
  3841. }
  3842. if (!(pi->sh->boardflags & BFL_FEM)) {
  3843. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  3844. tab.tbl_width = 16;
  3845. tab.tbl_ptr = &val;
  3846. tab.tbl_len = 1;
  3847. val = 150;
  3848. tab.tbl_offset = 0;
  3849. wlc_lcnphy_write_table(pi, &tab);
  3850. val = 220;
  3851. tab.tbl_offset = 1;
  3852. wlc_lcnphy_write_table(pi, &tab);
  3853. }
  3854. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  3855. if (pi->sh->boardflags & BFL_FEM)
  3856. wlc_lcnphy_load_tx_gain_table(
  3857. pi,
  3858. dot11lcnphy_2GHz_extPA_gaintable_rev0);
  3859. else
  3860. wlc_lcnphy_load_tx_gain_table(
  3861. pi,
  3862. dot11lcnphy_2GHz_gaintable_rev0);
  3863. }
  3864. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  3865. int l;
  3866. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  3867. l = dot11lcnphytbl_rx_gain_info_2G_rev2_sz;
  3868. if (pi->sh->boardflags & BFL_EXTLNA)
  3869. tb = dot11lcnphytbl_rx_gain_info_extlna_2G_rev2;
  3870. else
  3871. tb = dot11lcnphytbl_rx_gain_info_2G_rev2;
  3872. } else {
  3873. l = dot11lcnphytbl_rx_gain_info_5G_rev2_sz;
  3874. if (pi->sh->boardflags & BFL_EXTLNA_5GHz)
  3875. tb = dot11lcnphytbl_rx_gain_info_extlna_5G_rev2;
  3876. else
  3877. tb = dot11lcnphytbl_rx_gain_info_5G_rev2;
  3878. }
  3879. for (idx = 0; idx < l; idx++)
  3880. wlc_lcnphy_write_table(pi, &tb[idx]);
  3881. }
  3882. if (pi->sh->boardflags & BFL_FEM) {
  3883. if (pi->sh->boardflags & BFL_FEM_BT) {
  3884. if (pi->sh->boardrev < 0x1250)
  3885. tb = &dot11lcn_sw_ctrl_tbl_info_4313_bt_epa;
  3886. else
  3887. tb = &dot11lcn_sw_ctrl_tbl_info_4313_bt_epa_p250;
  3888. } else {
  3889. tb = &dot11lcn_sw_ctrl_tbl_info_4313_epa;
  3890. }
  3891. } else {
  3892. if (pi->sh->boardflags & BFL_FEM_BT)
  3893. tb = &dot11lcn_sw_ctrl_tbl_info_4313_bt_ipa;
  3894. else
  3895. tb = &dot11lcn_sw_ctrl_tbl_info_4313;
  3896. }
  3897. wlc_lcnphy_write_table(pi, tb);
  3898. wlc_lcnphy_load_rfpower(pi);
  3899. wlc_lcnphy_clear_papd_comptable(pi);
  3900. }
  3901. static void wlc_lcnphy_rev0_baseband_init(struct brcms_phy *pi)
  3902. {
  3903. u16 afectrl1;
  3904. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3905. write_radio_reg(pi, RADIO_2064_REG11C, 0x0);
  3906. write_phy_reg(pi, 0x43b, 0x0);
  3907. write_phy_reg(pi, 0x43c, 0x0);
  3908. write_phy_reg(pi, 0x44c, 0x0);
  3909. write_phy_reg(pi, 0x4e6, 0x0);
  3910. write_phy_reg(pi, 0x4f9, 0x0);
  3911. write_phy_reg(pi, 0x4b0, 0x0);
  3912. write_phy_reg(pi, 0x938, 0x0);
  3913. write_phy_reg(pi, 0x4b0, 0x0);
  3914. write_phy_reg(pi, 0x44e, 0);
  3915. or_phy_reg(pi, 0x567, 0x03);
  3916. or_phy_reg(pi, 0x44a, 0x44);
  3917. write_phy_reg(pi, 0x44a, 0x80);
  3918. if (!(pi->sh->boardflags & BFL_FEM))
  3919. wlc_lcnphy_set_tx_pwr_by_index(pi, 52);
  3920. if (0) {
  3921. afectrl1 = 0;
  3922. afectrl1 = (u16) ((pi_lcn->lcnphy_rssi_vf) |
  3923. (pi_lcn->lcnphy_rssi_vc << 4) |
  3924. (pi_lcn->lcnphy_rssi_gs << 10));
  3925. write_phy_reg(pi, 0x43e, afectrl1);
  3926. }
  3927. mod_phy_reg(pi, 0x634, (0xff << 0), 0xC << 0);
  3928. if (pi->sh->boardflags & BFL_FEM) {
  3929. mod_phy_reg(pi, 0x634, (0xff << 0), 0xA << 0);
  3930. write_phy_reg(pi, 0x910, 0x1);
  3931. }
  3932. mod_phy_reg(pi, 0x448, (0x3 << 8), 1 << 8);
  3933. mod_phy_reg(pi, 0x608, (0xff << 0), 0x17 << 0);
  3934. mod_phy_reg(pi, 0x604, (0x7ff << 0), 0x3EA << 0);
  3935. }
  3936. static void wlc_lcnphy_rev2_baseband_init(struct brcms_phy *pi)
  3937. {
  3938. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  3939. mod_phy_reg(pi, 0x416, (0xff << 0), 80 << 0);
  3940. mod_phy_reg(pi, 0x416, (0xff << 8), 80 << 8);
  3941. }
  3942. }
  3943. static void wlc_lcnphy_agc_temp_init(struct brcms_phy *pi)
  3944. {
  3945. s16 temp;
  3946. struct phytbl_info tab;
  3947. u32 tableBuffer[2];
  3948. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3949. temp = (s16) read_phy_reg(pi, 0x4df);
  3950. pi_lcn->lcnphy_ofdmgainidxtableoffset = (temp & (0xff << 0)) >> 0;
  3951. if (pi_lcn->lcnphy_ofdmgainidxtableoffset > 127)
  3952. pi_lcn->lcnphy_ofdmgainidxtableoffset -= 256;
  3953. pi_lcn->lcnphy_dsssgainidxtableoffset = (temp & (0xff << 8)) >> 8;
  3954. if (pi_lcn->lcnphy_dsssgainidxtableoffset > 127)
  3955. pi_lcn->lcnphy_dsssgainidxtableoffset -= 256;
  3956. tab.tbl_ptr = tableBuffer;
  3957. tab.tbl_len = 2;
  3958. tab.tbl_id = 17;
  3959. tab.tbl_offset = 59;
  3960. tab.tbl_width = 32;
  3961. wlc_lcnphy_read_table(pi, &tab);
  3962. if (tableBuffer[0] > 63)
  3963. tableBuffer[0] -= 128;
  3964. pi_lcn->lcnphy_tr_R_gain_val = tableBuffer[0];
  3965. if (tableBuffer[1] > 63)
  3966. tableBuffer[1] -= 128;
  3967. pi_lcn->lcnphy_tr_T_gain_val = tableBuffer[1];
  3968. temp = (s16) (read_phy_reg(pi, 0x434) & (0xff << 0));
  3969. if (temp > 127)
  3970. temp -= 256;
  3971. pi_lcn->lcnphy_input_pwr_offset_db = (s8) temp;
  3972. pi_lcn->lcnphy_Med_Low_Gain_db =
  3973. (read_phy_reg(pi, 0x424) & (0xff << 8)) >> 8;
  3974. pi_lcn->lcnphy_Very_Low_Gain_db =
  3975. (read_phy_reg(pi, 0x425) & (0xff << 0)) >> 0;
  3976. tab.tbl_ptr = tableBuffer;
  3977. tab.tbl_len = 2;
  3978. tab.tbl_id = LCNPHY_TBL_ID_GAIN_IDX;
  3979. tab.tbl_offset = 28;
  3980. tab.tbl_width = 32;
  3981. wlc_lcnphy_read_table(pi, &tab);
  3982. pi_lcn->lcnphy_gain_idx_14_lowword = tableBuffer[0];
  3983. pi_lcn->lcnphy_gain_idx_14_hiword = tableBuffer[1];
  3984. }
  3985. static void wlc_lcnphy_baseband_init(struct brcms_phy *pi)
  3986. {
  3987. wlc_lcnphy_tbl_init(pi);
  3988. wlc_lcnphy_rev0_baseband_init(pi);
  3989. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  3990. wlc_lcnphy_rev2_baseband_init(pi);
  3991. wlc_lcnphy_bu_tweaks(pi);
  3992. }
  3993. void wlc_phy_init_lcnphy(struct brcms_phy *pi)
  3994. {
  3995. u8 phybw40;
  3996. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3997. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3998. pi_lcn->lcnphy_cal_counter = 0;
  3999. pi_lcn->lcnphy_cal_temper = pi_lcn->lcnphy_rawtempsense;
  4000. or_phy_reg(pi, 0x44a, 0x80);
  4001. and_phy_reg(pi, 0x44a, 0x7f);
  4002. wlc_lcnphy_afe_clk_init(pi, AFE_CLK_INIT_MODE_TXRX2X);
  4003. write_phy_reg(pi, 0x60a, 160);
  4004. write_phy_reg(pi, 0x46a, 25);
  4005. wlc_lcnphy_baseband_init(pi);
  4006. wlc_lcnphy_radio_init(pi);
  4007. if (CHSPEC_IS2G(pi->radio_chanspec))
  4008. wlc_lcnphy_tx_pwr_ctrl_init((struct brcms_phy_pub *) pi);
  4009. wlc_phy_chanspec_set((struct brcms_phy_pub *) pi, pi->radio_chanspec);
  4010. bcma_chipco_regctl_maskset(&pi->d11core->bus->drv_cc, 0, ~0xf, 0x9);
  4011. bcma_chipco_chipctl_maskset(&pi->d11core->bus->drv_cc, 0, 0x0,
  4012. 0x03CDDDDD);
  4013. if ((pi->sh->boardflags & BFL_FEM)
  4014. && wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  4015. wlc_lcnphy_set_tx_pwr_by_index(pi, FIXED_TXPWR);
  4016. wlc_lcnphy_agc_temp_init(pi);
  4017. wlc_lcnphy_temp_adj(pi);
  4018. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  4019. udelay(100);
  4020. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  4021. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_HW);
  4022. pi_lcn->lcnphy_noise_samples = LCNPHY_NOISE_SAMPLES_DEFAULT;
  4023. wlc_lcnphy_calib_modes(pi, PHY_PERICAL_PHYINIT);
  4024. }
  4025. static bool wlc_phy_txpwr_srom_read_lcnphy(struct brcms_phy *pi)
  4026. {
  4027. s8 txpwr = 0;
  4028. int i;
  4029. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4030. struct ssb_sprom *sprom = &pi->d11core->bus->sprom;
  4031. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  4032. u16 cckpo = 0;
  4033. u32 offset_ofdm, offset_mcs;
  4034. pi_lcn->lcnphy_tr_isolation_mid = sprom->fem.ghz2.tr_iso;
  4035. pi_lcn->lcnphy_rx_power_offset = sprom->rxpo2g;
  4036. pi->txpa_2g[0] = sprom->pa0b0;
  4037. pi->txpa_2g[1] = sprom->pa0b1;
  4038. pi->txpa_2g[2] = sprom->pa0b2;
  4039. pi_lcn->lcnphy_rssi_vf = sprom->rssismf2g;
  4040. pi_lcn->lcnphy_rssi_vc = sprom->rssismc2g;
  4041. pi_lcn->lcnphy_rssi_gs = sprom->rssisav2g;
  4042. pi_lcn->lcnphy_rssi_vf_lowtemp = pi_lcn->lcnphy_rssi_vf;
  4043. pi_lcn->lcnphy_rssi_vc_lowtemp = pi_lcn->lcnphy_rssi_vc;
  4044. pi_lcn->lcnphy_rssi_gs_lowtemp = pi_lcn->lcnphy_rssi_gs;
  4045. pi_lcn->lcnphy_rssi_vf_hightemp = pi_lcn->lcnphy_rssi_vf;
  4046. pi_lcn->lcnphy_rssi_vc_hightemp = pi_lcn->lcnphy_rssi_vc;
  4047. pi_lcn->lcnphy_rssi_gs_hightemp = pi_lcn->lcnphy_rssi_gs;
  4048. txpwr = sprom->core_pwr_info[0].maxpwr_2g;
  4049. pi->tx_srom_max_2g = txpwr;
  4050. for (i = 0; i < PWRTBL_NUM_COEFF; i++) {
  4051. pi->txpa_2g_low_temp[i] = pi->txpa_2g[i];
  4052. pi->txpa_2g_high_temp[i] = pi->txpa_2g[i];
  4053. }
  4054. cckpo = sprom->cck2gpo;
  4055. offset_ofdm = sprom->ofdm2gpo;
  4056. if (cckpo) {
  4057. uint max_pwr_chan = txpwr;
  4058. for (i = TXP_FIRST_CCK; i <= TXP_LAST_CCK; i++) {
  4059. pi->tx_srom_max_rate_2g[i] =
  4060. max_pwr_chan - ((cckpo & 0xf) * 2);
  4061. cckpo >>= 4;
  4062. }
  4063. for (i = TXP_FIRST_OFDM; i <= TXP_LAST_OFDM; i++) {
  4064. pi->tx_srom_max_rate_2g[i] =
  4065. max_pwr_chan -
  4066. ((offset_ofdm & 0xf) * 2);
  4067. offset_ofdm >>= 4;
  4068. }
  4069. } else {
  4070. u8 opo = 0;
  4071. opo = sprom->opo;
  4072. for (i = TXP_FIRST_CCK; i <= TXP_LAST_CCK; i++)
  4073. pi->tx_srom_max_rate_2g[i] = txpwr;
  4074. for (i = TXP_FIRST_OFDM; i <= TXP_LAST_OFDM; i++) {
  4075. pi->tx_srom_max_rate_2g[i] = txpwr -
  4076. ((offset_ofdm & 0xf) * 2);
  4077. offset_ofdm >>= 4;
  4078. }
  4079. offset_mcs = sprom->mcs2gpo[1] << 16;
  4080. offset_mcs |= sprom->mcs2gpo[0];
  4081. pi_lcn->lcnphy_mcs20_po = offset_mcs;
  4082. for (i = TXP_FIRST_SISO_MCS_20;
  4083. i <= TXP_LAST_SISO_MCS_20; i++) {
  4084. pi->tx_srom_max_rate_2g[i] =
  4085. txpwr - ((offset_mcs & 0xf) * 2);
  4086. offset_mcs >>= 4;
  4087. }
  4088. }
  4089. pi_lcn->lcnphy_rawtempsense = sprom->rawtempsense;
  4090. pi_lcn->lcnphy_measPower = sprom->measpower;
  4091. pi_lcn->lcnphy_tempsense_slope = sprom->tempsense_slope;
  4092. pi_lcn->lcnphy_hw_iqcal_en = sprom->hw_iqcal_en;
  4093. pi_lcn->lcnphy_iqcal_swp_dis = sprom->iqcal_swp_dis;
  4094. pi_lcn->lcnphy_tempcorrx = sprom->tempcorrx;
  4095. pi_lcn->lcnphy_tempsense_option = sprom->tempsense_option;
  4096. pi_lcn->lcnphy_freqoffset_corr = sprom->freqoffset_corr;
  4097. if (sprom->ant_available_bg > 1)
  4098. wlc_phy_ant_rxdiv_set((struct brcms_phy_pub *) pi,
  4099. sprom->ant_available_bg);
  4100. }
  4101. pi_lcn->lcnphy_cck_dig_filt_type = -1;
  4102. return true;
  4103. }
  4104. void wlc_2064_vco_cal(struct brcms_phy *pi)
  4105. {
  4106. u8 calnrst;
  4107. mod_radio_reg(pi, RADIO_2064_REG057, 1 << 3, 1 << 3);
  4108. calnrst = (u8) read_radio_reg(pi, RADIO_2064_REG056) & 0xf8;
  4109. write_radio_reg(pi, RADIO_2064_REG056, calnrst);
  4110. udelay(1);
  4111. write_radio_reg(pi, RADIO_2064_REG056, calnrst | 0x03);
  4112. udelay(1);
  4113. write_radio_reg(pi, RADIO_2064_REG056, calnrst | 0x07);
  4114. udelay(300);
  4115. mod_radio_reg(pi, RADIO_2064_REG057, 1 << 3, 0);
  4116. }
  4117. bool wlc_phy_tpc_isenabled_lcnphy(struct brcms_phy *pi)
  4118. {
  4119. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  4120. return false;
  4121. else
  4122. return (LCNPHY_TX_PWR_CTRL_HW ==
  4123. wlc_lcnphy_get_tx_pwr_ctrl((pi)));
  4124. }
  4125. void wlc_phy_txpower_recalc_target_lcnphy(struct brcms_phy *pi)
  4126. {
  4127. u16 pwr_ctrl;
  4128. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  4129. wlc_lcnphy_calib_modes(pi, LCNPHY_PERICAL_TEMPBASED_TXPWRCTRL);
  4130. } else if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi)) {
  4131. pwr_ctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  4132. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  4133. wlc_lcnphy_txpower_recalc_target(pi);
  4134. wlc_lcnphy_set_tx_pwr_ctrl(pi, pwr_ctrl);
  4135. }
  4136. }
  4137. void wlc_phy_chanspec_set_lcnphy(struct brcms_phy *pi, u16 chanspec)
  4138. {
  4139. u8 channel = CHSPEC_CHANNEL(chanspec);
  4140. wlc_phy_chanspec_radio_set((struct brcms_phy_pub *)pi, chanspec);
  4141. wlc_lcnphy_set_chanspec_tweaks(pi, pi->radio_chanspec);
  4142. or_phy_reg(pi, 0x44a, 0x44);
  4143. write_phy_reg(pi, 0x44a, 0x80);
  4144. wlc_lcnphy_radio_2064_channel_tune_4313(pi, channel);
  4145. udelay(1000);
  4146. wlc_lcnphy_toggle_afe_pwdn(pi);
  4147. write_phy_reg(pi, 0x657, lcnphy_sfo_cfg[channel - 1].ptcentreTs20);
  4148. write_phy_reg(pi, 0x658, lcnphy_sfo_cfg[channel - 1].ptcentreFactor);
  4149. if (CHSPEC_CHANNEL(pi->radio_chanspec) == 14) {
  4150. mod_phy_reg(pi, 0x448, (0x3 << 8), (2) << 8);
  4151. wlc_lcnphy_load_tx_iir_filter(pi, false, 3);
  4152. } else {
  4153. mod_phy_reg(pi, 0x448, (0x3 << 8), (1) << 8);
  4154. wlc_lcnphy_load_tx_iir_filter(pi, false, 2);
  4155. }
  4156. if (pi->sh->boardflags & BFL_FEM)
  4157. wlc_lcnphy_load_tx_iir_filter(pi, true, 0);
  4158. else
  4159. wlc_lcnphy_load_tx_iir_filter(pi, true, 3);
  4160. mod_phy_reg(pi, 0x4eb, (0x7 << 3), (1) << 3);
  4161. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  4162. wlc_lcnphy_tssi_setup(pi);
  4163. }
  4164. void wlc_phy_detach_lcnphy(struct brcms_phy *pi)
  4165. {
  4166. kfree(pi->u.pi_lcnphy);
  4167. }
  4168. bool wlc_phy_attach_lcnphy(struct brcms_phy *pi)
  4169. {
  4170. struct brcms_phy_lcnphy *pi_lcn;
  4171. pi->u.pi_lcnphy = kzalloc(sizeof(struct brcms_phy_lcnphy), GFP_ATOMIC);
  4172. if (pi->u.pi_lcnphy == NULL)
  4173. return false;
  4174. pi_lcn = pi->u.pi_lcnphy;
  4175. if (0 == (pi->sh->boardflags & BFL_NOPA)) {
  4176. pi->hwpwrctrl = true;
  4177. pi->hwpwrctrl_capable = true;
  4178. }
  4179. pi->xtalfreq = bcma_chipco_get_alp_clock(&pi->d11core->bus->drv_cc);
  4180. pi_lcn->lcnphy_papd_rxGnCtrl_init = 0;
  4181. pi->pi_fptr.init = wlc_phy_init_lcnphy;
  4182. pi->pi_fptr.calinit = wlc_phy_cal_init_lcnphy;
  4183. pi->pi_fptr.chanset = wlc_phy_chanspec_set_lcnphy;
  4184. pi->pi_fptr.txpwrrecalc = wlc_phy_txpower_recalc_target_lcnphy;
  4185. pi->pi_fptr.txiqccget = wlc_lcnphy_get_tx_iqcc;
  4186. pi->pi_fptr.txiqccset = wlc_lcnphy_set_tx_iqcc;
  4187. pi->pi_fptr.txloccget = wlc_lcnphy_get_tx_locc;
  4188. pi->pi_fptr.radioloftget = wlc_lcnphy_get_radio_loft;
  4189. pi->pi_fptr.detach = wlc_phy_detach_lcnphy;
  4190. if (!wlc_phy_txpwr_srom_read_lcnphy(pi))
  4191. return false;
  4192. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  4193. if (pi_lcn->lcnphy_tempsense_option == 3) {
  4194. pi->hwpwrctrl = true;
  4195. pi->hwpwrctrl_capable = true;
  4196. pi->temppwrctrl_capable = false;
  4197. } else {
  4198. pi->hwpwrctrl = false;
  4199. pi->hwpwrctrl_capable = false;
  4200. pi->temppwrctrl_capable = true;
  4201. }
  4202. }
  4203. return true;
  4204. }
  4205. static void wlc_lcnphy_set_rx_gain(struct brcms_phy *pi, u32 gain)
  4206. {
  4207. u16 trsw, ext_lna, lna1, lna2, tia, biq0, biq1, gain0_15, gain16_19;
  4208. trsw = (gain & ((u32) 1 << 28)) ? 0 : 1;
  4209. ext_lna = (u16) (gain >> 29) & 0x01;
  4210. lna1 = (u16) (gain >> 0) & 0x0f;
  4211. lna2 = (u16) (gain >> 4) & 0x0f;
  4212. tia = (u16) (gain >> 8) & 0xf;
  4213. biq0 = (u16) (gain >> 12) & 0xf;
  4214. biq1 = (u16) (gain >> 16) & 0xf;
  4215. gain0_15 = (u16) ((lna1 & 0x3) | ((lna1 & 0x3) << 2) |
  4216. ((lna2 & 0x3) << 4) | ((lna2 & 0x3) << 6) |
  4217. ((tia & 0xf) << 8) | ((biq0 & 0xf) << 12));
  4218. gain16_19 = biq1;
  4219. mod_phy_reg(pi, 0x44d, (0x1 << 0), trsw << 0);
  4220. mod_phy_reg(pi, 0x4b1, (0x1 << 9), ext_lna << 9);
  4221. mod_phy_reg(pi, 0x4b1, (0x1 << 10), ext_lna << 10);
  4222. mod_phy_reg(pi, 0x4b6, (0xffff << 0), gain0_15 << 0);
  4223. mod_phy_reg(pi, 0x4b7, (0xf << 0), gain16_19 << 0);
  4224. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  4225. mod_phy_reg(pi, 0x4b1, (0x3 << 11), lna1 << 11);
  4226. mod_phy_reg(pi, 0x4e6, (0x3 << 3), lna1 << 3);
  4227. }
  4228. wlc_lcnphy_rx_gain_override_enable(pi, true);
  4229. }
  4230. static u32 wlc_lcnphy_get_receive_power(struct brcms_phy *pi, s32 *gain_index)
  4231. {
  4232. u32 received_power = 0;
  4233. s32 max_index = 0;
  4234. u32 gain_code = 0;
  4235. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4236. max_index = 36;
  4237. if (*gain_index >= 0)
  4238. gain_code = lcnphy_23bitgaincode_table[*gain_index];
  4239. if (-1 == *gain_index) {
  4240. *gain_index = 0;
  4241. while ((*gain_index <= (s32) max_index)
  4242. && (received_power < 700)) {
  4243. wlc_lcnphy_set_rx_gain(pi,
  4244. lcnphy_23bitgaincode_table
  4245. [*gain_index]);
  4246. received_power =
  4247. wlc_lcnphy_measure_digital_power(
  4248. pi,
  4249. pi_lcn->
  4250. lcnphy_noise_samples);
  4251. (*gain_index)++;
  4252. }
  4253. (*gain_index)--;
  4254. } else {
  4255. wlc_lcnphy_set_rx_gain(pi, gain_code);
  4256. received_power =
  4257. wlc_lcnphy_measure_digital_power(pi,
  4258. pi_lcn->
  4259. lcnphy_noise_samples);
  4260. }
  4261. return received_power;
  4262. }
  4263. s32 wlc_lcnphy_rx_signal_power(struct brcms_phy *pi, s32 gain_index)
  4264. {
  4265. s32 gain = 0;
  4266. s32 nominal_power_db;
  4267. s32 log_val, gain_mismatch, desired_gain, input_power_offset_db,
  4268. input_power_db;
  4269. s32 received_power, temperature;
  4270. u32 power;
  4271. u32 msb1, msb2, val1, val2, diff1, diff2;
  4272. uint freq;
  4273. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4274. received_power = wlc_lcnphy_get_receive_power(pi, &gain_index);
  4275. gain = lcnphy_gain_table[gain_index];
  4276. nominal_power_db = read_phy_reg(pi, 0x425) >> 8;
  4277. power = (received_power * 16);
  4278. msb1 = ffs(power) - 1;
  4279. msb2 = msb1 + 1;
  4280. val1 = 1 << msb1;
  4281. val2 = 1 << msb2;
  4282. diff1 = (power - val1);
  4283. diff2 = (val2 - power);
  4284. if (diff1 < diff2)
  4285. log_val = msb1;
  4286. else
  4287. log_val = msb2;
  4288. log_val = log_val * 3;
  4289. gain_mismatch = (nominal_power_db / 2) - (log_val);
  4290. desired_gain = gain + gain_mismatch;
  4291. input_power_offset_db = read_phy_reg(pi, 0x434) & 0xFF;
  4292. if (input_power_offset_db > 127)
  4293. input_power_offset_db -= 256;
  4294. input_power_db = input_power_offset_db - desired_gain;
  4295. input_power_db =
  4296. input_power_db + lcnphy_gain_index_offset_for_rssi[gain_index];
  4297. freq = wlc_phy_channel2freq(CHSPEC_CHANNEL(pi->radio_chanspec));
  4298. if ((freq > 2427) && (freq <= 2467))
  4299. input_power_db = input_power_db - 1;
  4300. temperature = pi_lcn->lcnphy_lastsensed_temperature;
  4301. if ((temperature - 15) < -30)
  4302. input_power_db =
  4303. input_power_db +
  4304. (((temperature - 10 - 25) * 286) >> 12) -
  4305. 7;
  4306. else if ((temperature - 15) < 4)
  4307. input_power_db =
  4308. input_power_db +
  4309. (((temperature - 10 - 25) * 286) >> 12) -
  4310. 3;
  4311. else
  4312. input_power_db = input_power_db +
  4313. (((temperature - 10 - 25) * 286) >> 12);
  4314. wlc_lcnphy_rx_gain_override_enable(pi, 0);
  4315. return input_power_db;
  4316. }