phy_lcn.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243
  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_array(131, sizeof(s16), 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_array(20, sizeof(u16), 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. mod_phy_reg(pi, 0x4b0, (0x1 << 5), (mode) << 5);
  2801. mod_phy_reg(pi, 0x4b1, (0x1 << 9), 0 << 9);
  2802. if (phybw40 == 0) {
  2803. mod_phy_reg((pi), 0x410,
  2804. (0x1 << 6) |
  2805. (0x1 << 5),
  2806. ((CHSPEC_IS2G(
  2807. pi->radio_chanspec)) ? (!mode) : 0) <<
  2808. 6 | (!mode) << 5);
  2809. mod_phy_reg(pi, 0x410, (0x1 << 7), (mode) << 7);
  2810. }
  2811. }
  2812. void
  2813. wlc_lcnphy_start_tx_tone(struct brcms_phy *pi, s32 f_kHz, u16 max_val,
  2814. bool iqcalmode)
  2815. {
  2816. u8 phy_bw;
  2817. u16 num_samps, t, k;
  2818. u32 bw;
  2819. s32 theta = 0, rot = 0;
  2820. struct cordic_iq tone_samp;
  2821. u32 data_buf[64];
  2822. u16 i_samp, q_samp;
  2823. struct phytbl_info tab;
  2824. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2825. pi->phy_tx_tone_freq = f_kHz;
  2826. wlc_lcnphy_deaf_mode(pi, true);
  2827. phy_bw = 40;
  2828. if (pi_lcn->lcnphy_spurmod) {
  2829. write_phy_reg(pi, 0x942, 0x2);
  2830. write_phy_reg(pi, 0x93b, 0x0);
  2831. write_phy_reg(pi, 0x93c, 0x0);
  2832. wlc_lcnphy_txrx_spur_avoidance_mode(pi, false);
  2833. }
  2834. if (f_kHz) {
  2835. k = 1;
  2836. do {
  2837. bw = phy_bw * 1000 * k;
  2838. num_samps = bw / abs(f_kHz);
  2839. k++;
  2840. } while ((num_samps * (u32) (abs(f_kHz))) != bw);
  2841. } else
  2842. num_samps = 2;
  2843. rot = ((f_kHz * 36) / phy_bw) / 100;
  2844. theta = 0;
  2845. for (t = 0; t < num_samps; t++) {
  2846. tone_samp = cordic_calc_iq(theta);
  2847. theta += rot;
  2848. i_samp = (u16) (FLOAT(tone_samp.i * max_val) & 0x3ff);
  2849. q_samp = (u16) (FLOAT(tone_samp.q * max_val) & 0x3ff);
  2850. data_buf[t] = (i_samp << 10) | q_samp;
  2851. }
  2852. mod_phy_reg(pi, 0x6d6, (0x3 << 0), 0 << 0);
  2853. mod_phy_reg(pi, 0x6da, (0x1 << 3), 1 << 3);
  2854. tab.tbl_ptr = data_buf;
  2855. tab.tbl_len = num_samps;
  2856. tab.tbl_id = LCNPHY_TBL_ID_SAMPLEPLAY;
  2857. tab.tbl_offset = 0;
  2858. tab.tbl_width = 32;
  2859. wlc_lcnphy_write_table(pi, &tab);
  2860. wlc_lcnphy_run_samples(pi, num_samps, 0xffff, 0, iqcalmode);
  2861. }
  2862. void wlc_lcnphy_stop_tx_tone(struct brcms_phy *pi)
  2863. {
  2864. s16 playback_status;
  2865. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2866. pi->phy_tx_tone_freq = 0;
  2867. if (pi_lcn->lcnphy_spurmod) {
  2868. write_phy_reg(pi, 0x942, 0x7);
  2869. write_phy_reg(pi, 0x93b, 0x2017);
  2870. write_phy_reg(pi, 0x93c, 0x27c5);
  2871. wlc_lcnphy_txrx_spur_avoidance_mode(pi, true);
  2872. }
  2873. playback_status = read_phy_reg(pi, 0x644);
  2874. if (playback_status & (0x1 << 0)) {
  2875. wlc_lcnphy_tx_pu(pi, 0);
  2876. mod_phy_reg(pi, 0x63f, (0x1 << 1), 1 << 1);
  2877. } else if (playback_status & (0x1 << 1))
  2878. mod_phy_reg(pi, 0x453, (0x1 << 15), 0 << 15);
  2879. mod_phy_reg(pi, 0x6d6, (0x3 << 0), 1 << 0);
  2880. mod_phy_reg(pi, 0x6da, (0x1 << 3), 0 << 3);
  2881. mod_phy_reg(pi, 0x6da, (0x1 << 7), 0 << 7);
  2882. and_radio_reg(pi, RADIO_2064_REG112, 0xFFF9);
  2883. wlc_lcnphy_deaf_mode(pi, false);
  2884. }
  2885. static void
  2886. wlc_lcnphy_set_cc(struct brcms_phy *pi, int cal_type, s16 coeff_x, s16 coeff_y)
  2887. {
  2888. u16 di0dq0;
  2889. u16 x, y, data_rf;
  2890. int k;
  2891. switch (cal_type) {
  2892. case 0:
  2893. wlc_lcnphy_set_tx_iqcc(pi, coeff_x, coeff_y);
  2894. break;
  2895. case 2:
  2896. di0dq0 = (coeff_x & 0xff) << 8 | (coeff_y & 0xff);
  2897. wlc_lcnphy_set_tx_locc(pi, di0dq0);
  2898. break;
  2899. case 3:
  2900. k = wlc_lcnphy_calc_floor(coeff_x, 0);
  2901. y = 8 + k;
  2902. k = wlc_lcnphy_calc_floor(coeff_x, 1);
  2903. x = 8 - k;
  2904. data_rf = (x * 16 + y);
  2905. write_radio_reg(pi, RADIO_2064_REG089, data_rf);
  2906. k = wlc_lcnphy_calc_floor(coeff_y, 0);
  2907. y = 8 + k;
  2908. k = wlc_lcnphy_calc_floor(coeff_y, 1);
  2909. x = 8 - k;
  2910. data_rf = (x * 16 + y);
  2911. write_radio_reg(pi, RADIO_2064_REG08A, data_rf);
  2912. break;
  2913. case 4:
  2914. k = wlc_lcnphy_calc_floor(coeff_x, 0);
  2915. y = 8 + k;
  2916. k = wlc_lcnphy_calc_floor(coeff_x, 1);
  2917. x = 8 - k;
  2918. data_rf = (x * 16 + y);
  2919. write_radio_reg(pi, RADIO_2064_REG08B, data_rf);
  2920. k = wlc_lcnphy_calc_floor(coeff_y, 0);
  2921. y = 8 + k;
  2922. k = wlc_lcnphy_calc_floor(coeff_y, 1);
  2923. x = 8 - k;
  2924. data_rf = (x * 16 + y);
  2925. write_radio_reg(pi, RADIO_2064_REG08C, data_rf);
  2926. break;
  2927. }
  2928. }
  2929. static struct lcnphy_unsign16_struct
  2930. wlc_lcnphy_get_cc(struct brcms_phy *pi, int cal_type)
  2931. {
  2932. u16 a, b, didq;
  2933. u8 di0, dq0, ei, eq, fi, fq;
  2934. struct lcnphy_unsign16_struct cc;
  2935. cc.re = 0;
  2936. cc.im = 0;
  2937. switch (cal_type) {
  2938. case 0:
  2939. wlc_lcnphy_get_tx_iqcc(pi, &a, &b);
  2940. cc.re = a;
  2941. cc.im = b;
  2942. break;
  2943. case 2:
  2944. didq = wlc_lcnphy_get_tx_locc(pi);
  2945. di0 = (((didq & 0xff00) << 16) >> 24);
  2946. dq0 = (((didq & 0x00ff) << 24) >> 24);
  2947. cc.re = (u16) di0;
  2948. cc.im = (u16) dq0;
  2949. break;
  2950. case 3:
  2951. wlc_lcnphy_get_radio_loft(pi, &ei, &eq, &fi, &fq);
  2952. cc.re = (u16) ei;
  2953. cc.im = (u16) eq;
  2954. break;
  2955. case 4:
  2956. wlc_lcnphy_get_radio_loft(pi, &ei, &eq, &fi, &fq);
  2957. cc.re = (u16) fi;
  2958. cc.im = (u16) fq;
  2959. break;
  2960. }
  2961. return cc;
  2962. }
  2963. static void
  2964. wlc_lcnphy_samp_cap(struct brcms_phy *pi, int clip_detect_algo, u16 thresh,
  2965. s16 *ptr, int mode)
  2966. {
  2967. u32 curval1, curval2, stpptr, curptr, strptr, val;
  2968. u16 sslpnCalibClkEnCtrl, timer;
  2969. u16 old_sslpnCalibClkEnCtrl;
  2970. s16 imag, real;
  2971. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2972. timer = 0;
  2973. old_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2974. curval1 = bcma_read16(pi->d11core, D11REGOFFS(psm_corectlsts));
  2975. ptr[130] = 0;
  2976. bcma_write16(pi->d11core, D11REGOFFS(psm_corectlsts),
  2977. ((1 << 6) | curval1));
  2978. bcma_write16(pi->d11core, D11REGOFFS(smpl_clct_strptr), 0x7E00);
  2979. bcma_write16(pi->d11core, D11REGOFFS(smpl_clct_stpptr), 0x8000);
  2980. udelay(20);
  2981. curval2 = bcma_read16(pi->d11core, D11REGOFFS(psm_phy_hdr_param));
  2982. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param),
  2983. curval2 | 0x30);
  2984. write_phy_reg(pi, 0x555, 0x0);
  2985. write_phy_reg(pi, 0x5a6, 0x5);
  2986. write_phy_reg(pi, 0x5a2, (u16) (mode | mode << 6));
  2987. write_phy_reg(pi, 0x5cf, 3);
  2988. write_phy_reg(pi, 0x5a5, 0x3);
  2989. write_phy_reg(pi, 0x583, 0x0);
  2990. write_phy_reg(pi, 0x584, 0x0);
  2991. write_phy_reg(pi, 0x585, 0x0fff);
  2992. write_phy_reg(pi, 0x586, 0x0000);
  2993. write_phy_reg(pi, 0x580, 0x4501);
  2994. sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2995. write_phy_reg(pi, 0x6da, (u32) (sslpnCalibClkEnCtrl | 0x2008));
  2996. stpptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_stpptr));
  2997. curptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_curptr));
  2998. do {
  2999. udelay(10);
  3000. curptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_curptr));
  3001. timer++;
  3002. } while ((curptr != stpptr) && (timer < 500));
  3003. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param), 0x2);
  3004. strptr = 0x7E00;
  3005. bcma_write32(pi->d11core, D11REGOFFS(tplatewrptr), strptr);
  3006. while (strptr < 0x8000) {
  3007. val = bcma_read32(pi->d11core, D11REGOFFS(tplatewrdata));
  3008. imag = ((val >> 16) & 0x3ff);
  3009. real = ((val) & 0x3ff);
  3010. if (imag > 511)
  3011. imag -= 1024;
  3012. if (real > 511)
  3013. real -= 1024;
  3014. if (pi_lcn->lcnphy_iqcal_swp_dis)
  3015. ptr[(strptr - 0x7E00) / 4] = real;
  3016. else
  3017. ptr[(strptr - 0x7E00) / 4] = imag;
  3018. if (clip_detect_algo) {
  3019. if (imag > thresh || imag < -thresh) {
  3020. strptr = 0x8000;
  3021. ptr[130] = 1;
  3022. }
  3023. }
  3024. strptr += 4;
  3025. }
  3026. write_phy_reg(pi, 0x6da, old_sslpnCalibClkEnCtrl);
  3027. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param), curval2);
  3028. bcma_write16(pi->d11core, D11REGOFFS(psm_corectlsts), curval1);
  3029. }
  3030. static void
  3031. wlc_lcnphy_a1(struct brcms_phy *pi, int cal_type, int num_levels,
  3032. int step_size_lg2)
  3033. {
  3034. const struct lcnphy_spb_tone *phy_c1;
  3035. struct lcnphy_spb_tone phy_c2;
  3036. struct lcnphy_unsign16_struct phy_c3;
  3037. int phy_c4, phy_c5, k, l, j, phy_c6;
  3038. u16 phy_c7, phy_c8, phy_c9;
  3039. s16 phy_c10, phy_c11, phy_c12, phy_c13, phy_c14, phy_c15, phy_c16;
  3040. s16 *ptr, phy_c17;
  3041. s32 phy_c18, phy_c19;
  3042. u32 phy_c20, phy_c21;
  3043. bool phy_c22, phy_c23, phy_c24, phy_c25;
  3044. u16 phy_c26, phy_c27;
  3045. u16 phy_c28, phy_c29, phy_c30;
  3046. u16 phy_c31;
  3047. u16 *phy_c32;
  3048. phy_c21 = 0;
  3049. phy_c10 = phy_c13 = phy_c14 = phy_c8 = 0;
  3050. ptr = kmalloc_array(131, sizeof(s16), GFP_ATOMIC);
  3051. if (NULL == ptr)
  3052. return;
  3053. phy_c32 = kmalloc_array(20, sizeof(u16), GFP_ATOMIC);
  3054. if (NULL == phy_c32) {
  3055. kfree(ptr);
  3056. return;
  3057. }
  3058. phy_c26 = read_phy_reg(pi, 0x6da);
  3059. phy_c27 = read_phy_reg(pi, 0x6db);
  3060. phy_c31 = read_radio_reg(pi, RADIO_2064_REG026);
  3061. write_phy_reg(pi, 0x93d, 0xC0);
  3062. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 0);
  3063. write_phy_reg(pi, 0x6da, 0xffff);
  3064. or_phy_reg(pi, 0x6db, 0x3);
  3065. wlc_lcnphy_tx_iqlo_loopback(pi, phy_c32);
  3066. udelay(500);
  3067. phy_c28 = read_phy_reg(pi, 0x938);
  3068. phy_c29 = read_phy_reg(pi, 0x4d7);
  3069. phy_c30 = read_phy_reg(pi, 0x4d8);
  3070. or_phy_reg(pi, 0x938, 0x1 << 2);
  3071. or_phy_reg(pi, 0x4d7, 0x1 << 2);
  3072. or_phy_reg(pi, 0x4d7, 0x1 << 3);
  3073. mod_phy_reg(pi, 0x4d7, (0x7 << 12), 0x2 << 12);
  3074. or_phy_reg(pi, 0x4d8, 1 << 0);
  3075. or_phy_reg(pi, 0x4d8, 1 << 1);
  3076. mod_phy_reg(pi, 0x4d8, (0x3ff << 2), 0x23A << 2);
  3077. mod_phy_reg(pi, 0x4d8, (0x7 << 12), 0x7 << 12);
  3078. phy_c1 = &lcnphy_spb_tone_3750[0];
  3079. phy_c4 = 32;
  3080. if (num_levels == 0) {
  3081. if (cal_type != 0)
  3082. num_levels = 4;
  3083. else
  3084. num_levels = 9;
  3085. }
  3086. if (step_size_lg2 == 0) {
  3087. if (cal_type != 0)
  3088. step_size_lg2 = 3;
  3089. else
  3090. step_size_lg2 = 8;
  3091. }
  3092. phy_c7 = (1 << step_size_lg2);
  3093. phy_c3 = wlc_lcnphy_get_cc(pi, cal_type);
  3094. phy_c15 = (s16) phy_c3.re;
  3095. phy_c16 = (s16) phy_c3.im;
  3096. if (cal_type == 2) {
  3097. if (phy_c3.re > 127)
  3098. phy_c15 = phy_c3.re - 256;
  3099. if (phy_c3.im > 127)
  3100. phy_c16 = phy_c3.im - 256;
  3101. }
  3102. wlc_lcnphy_set_cc(pi, cal_type, phy_c15, phy_c16);
  3103. udelay(20);
  3104. for (phy_c8 = 0; phy_c7 != 0 && phy_c8 < num_levels; phy_c8++) {
  3105. phy_c23 = true;
  3106. phy_c22 = false;
  3107. switch (cal_type) {
  3108. case 0:
  3109. phy_c10 = 511;
  3110. break;
  3111. case 2:
  3112. phy_c10 = 127;
  3113. break;
  3114. case 3:
  3115. phy_c10 = 15;
  3116. break;
  3117. case 4:
  3118. phy_c10 = 15;
  3119. break;
  3120. }
  3121. phy_c9 = read_phy_reg(pi, 0x93d);
  3122. phy_c9 = 2 * phy_c9;
  3123. phy_c24 = false;
  3124. phy_c5 = 7;
  3125. phy_c25 = true;
  3126. while (1) {
  3127. write_radio_reg(pi, RADIO_2064_REG026,
  3128. (phy_c5 & 0x7) | ((phy_c5 & 0x7) << 4));
  3129. udelay(50);
  3130. phy_c22 = false;
  3131. ptr[130] = 0;
  3132. wlc_lcnphy_samp_cap(pi, 1, phy_c9, &ptr[0], 2);
  3133. if (ptr[130] == 1)
  3134. phy_c22 = true;
  3135. if (phy_c22)
  3136. phy_c5 -= 1;
  3137. if ((phy_c22 != phy_c24) && (!phy_c25))
  3138. break;
  3139. if (!phy_c22)
  3140. phy_c5 += 1;
  3141. if (phy_c5 <= 0 || phy_c5 >= 7)
  3142. break;
  3143. phy_c24 = phy_c22;
  3144. phy_c25 = false;
  3145. }
  3146. if (phy_c5 < 0)
  3147. phy_c5 = 0;
  3148. else if (phy_c5 > 7)
  3149. phy_c5 = 7;
  3150. for (k = -phy_c7; k <= phy_c7; k += phy_c7) {
  3151. for (l = -phy_c7; l <= phy_c7; l += phy_c7) {
  3152. phy_c11 = phy_c15 + k;
  3153. phy_c12 = phy_c16 + l;
  3154. if (phy_c11 < -phy_c10)
  3155. phy_c11 = -phy_c10;
  3156. else if (phy_c11 > phy_c10)
  3157. phy_c11 = phy_c10;
  3158. if (phy_c12 < -phy_c10)
  3159. phy_c12 = -phy_c10;
  3160. else if (phy_c12 > phy_c10)
  3161. phy_c12 = phy_c10;
  3162. wlc_lcnphy_set_cc(pi, cal_type, phy_c11,
  3163. phy_c12);
  3164. udelay(20);
  3165. wlc_lcnphy_samp_cap(pi, 0, 0, ptr, 2);
  3166. phy_c18 = 0;
  3167. phy_c19 = 0;
  3168. for (j = 0; j < 128; j++) {
  3169. if (cal_type != 0)
  3170. phy_c6 = j % phy_c4;
  3171. else
  3172. phy_c6 = (2 * j) % phy_c4;
  3173. phy_c2.re = phy_c1[phy_c6].re;
  3174. phy_c2.im = phy_c1[phy_c6].im;
  3175. phy_c17 = ptr[j];
  3176. phy_c18 = phy_c18 + phy_c17 * phy_c2.re;
  3177. phy_c19 = phy_c19 + phy_c17 * phy_c2.im;
  3178. }
  3179. phy_c18 = phy_c18 >> 10;
  3180. phy_c19 = phy_c19 >> 10;
  3181. phy_c20 = ((phy_c18 * phy_c18) +
  3182. (phy_c19 * phy_c19));
  3183. if (phy_c23 || phy_c20 < phy_c21) {
  3184. phy_c21 = phy_c20;
  3185. phy_c13 = phy_c11;
  3186. phy_c14 = phy_c12;
  3187. }
  3188. phy_c23 = false;
  3189. }
  3190. }
  3191. phy_c23 = true;
  3192. phy_c15 = phy_c13;
  3193. phy_c16 = phy_c14;
  3194. phy_c7 = phy_c7 >> 1;
  3195. wlc_lcnphy_set_cc(pi, cal_type, phy_c15, phy_c16);
  3196. udelay(20);
  3197. }
  3198. goto cleanup;
  3199. cleanup:
  3200. wlc_lcnphy_tx_iqlo_loopback_cleanup(pi, phy_c32);
  3201. wlc_lcnphy_stop_tx_tone(pi);
  3202. write_phy_reg(pi, 0x6da, phy_c26);
  3203. write_phy_reg(pi, 0x6db, phy_c27);
  3204. write_phy_reg(pi, 0x938, phy_c28);
  3205. write_phy_reg(pi, 0x4d7, phy_c29);
  3206. write_phy_reg(pi, 0x4d8, phy_c30);
  3207. write_radio_reg(pi, RADIO_2064_REG026, phy_c31);
  3208. kfree(phy_c32);
  3209. kfree(ptr);
  3210. }
  3211. void wlc_lcnphy_get_tx_iqcc(struct brcms_phy *pi, u16 *a, u16 *b)
  3212. {
  3213. u16 iqcc[2];
  3214. struct phytbl_info tab;
  3215. tab.tbl_ptr = iqcc;
  3216. tab.tbl_len = 2;
  3217. tab.tbl_id = 0;
  3218. tab.tbl_offset = 80;
  3219. tab.tbl_width = 16;
  3220. wlc_lcnphy_read_table(pi, &tab);
  3221. *a = iqcc[0];
  3222. *b = iqcc[1];
  3223. }
  3224. static void wlc_lcnphy_tx_iqlo_soft_cal_full(struct brcms_phy *pi)
  3225. {
  3226. struct lcnphy_unsign16_struct iqcc0, locc2, locc3, locc4;
  3227. wlc_lcnphy_set_cc(pi, 0, 0, 0);
  3228. wlc_lcnphy_set_cc(pi, 2, 0, 0);
  3229. wlc_lcnphy_set_cc(pi, 3, 0, 0);
  3230. wlc_lcnphy_set_cc(pi, 4, 0, 0);
  3231. wlc_lcnphy_a1(pi, 4, 0, 0);
  3232. wlc_lcnphy_a1(pi, 3, 0, 0);
  3233. wlc_lcnphy_a1(pi, 2, 3, 2);
  3234. wlc_lcnphy_a1(pi, 0, 5, 8);
  3235. wlc_lcnphy_a1(pi, 2, 2, 1);
  3236. wlc_lcnphy_a1(pi, 0, 4, 3);
  3237. iqcc0 = wlc_lcnphy_get_cc(pi, 0);
  3238. locc2 = wlc_lcnphy_get_cc(pi, 2);
  3239. locc3 = wlc_lcnphy_get_cc(pi, 3);
  3240. locc4 = wlc_lcnphy_get_cc(pi, 4);
  3241. }
  3242. u16 wlc_lcnphy_get_tx_locc(struct brcms_phy *pi)
  3243. {
  3244. struct phytbl_info tab;
  3245. u16 didq;
  3246. tab.tbl_id = 0;
  3247. tab.tbl_width = 16;
  3248. tab.tbl_ptr = &didq;
  3249. tab.tbl_len = 1;
  3250. tab.tbl_offset = 85;
  3251. wlc_lcnphy_read_table(pi, &tab);
  3252. return didq;
  3253. }
  3254. static void wlc_lcnphy_txpwrtbl_iqlo_cal(struct brcms_phy *pi)
  3255. {
  3256. struct lcnphy_txgains target_gains, old_gains;
  3257. u8 save_bb_mult;
  3258. u16 a, b, didq, save_pa_gain = 0;
  3259. uint idx, SAVE_txpwrindex = 0xFF;
  3260. u32 val;
  3261. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3262. struct phytbl_info tab;
  3263. u8 ei0, eq0, fi0, fq0;
  3264. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3265. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  3266. save_pa_gain = wlc_lcnphy_get_pa_gain(pi);
  3267. save_bb_mult = wlc_lcnphy_get_bbmult(pi);
  3268. if (SAVE_txpwrctrl == LCNPHY_TX_PWR_CTRL_OFF)
  3269. SAVE_txpwrindex = wlc_lcnphy_get_current_tx_pwr_idx(pi);
  3270. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  3271. target_gains.gm_gain = 7;
  3272. target_gains.pga_gain = 0;
  3273. target_gains.pad_gain = 21;
  3274. target_gains.dac_gain = 0;
  3275. wlc_lcnphy_set_tx_gain(pi, &target_gains);
  3276. if (LCNREV_IS(pi->pubpi.phy_rev, 1) || pi_lcn->lcnphy_hw_iqcal_en) {
  3277. wlc_lcnphy_set_tx_pwr_by_index(pi, 30);
  3278. wlc_lcnphy_tx_iqlo_cal(pi, &target_gains,
  3279. (pi_lcn->
  3280. lcnphy_recal ? LCNPHY_CAL_RECAL :
  3281. LCNPHY_CAL_FULL), false);
  3282. } else {
  3283. wlc_lcnphy_set_tx_pwr_by_index(pi, 16);
  3284. wlc_lcnphy_tx_iqlo_soft_cal_full(pi);
  3285. }
  3286. wlc_lcnphy_get_radio_loft(pi, &ei0, &eq0, &fi0, &fq0);
  3287. if ((abs((s8) fi0) == 15) && (abs((s8) fq0) == 15)) {
  3288. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  3289. target_gains.gm_gain = 255;
  3290. target_gains.pga_gain = 255;
  3291. target_gains.pad_gain = 0xf0;
  3292. target_gains.dac_gain = 0;
  3293. } else {
  3294. target_gains.gm_gain = 7;
  3295. target_gains.pga_gain = 45;
  3296. target_gains.pad_gain = 186;
  3297. target_gains.dac_gain = 0;
  3298. }
  3299. if (LCNREV_IS(pi->pubpi.phy_rev, 1)
  3300. || pi_lcn->lcnphy_hw_iqcal_en) {
  3301. target_gains.pga_gain = 0;
  3302. target_gains.pad_gain = 30;
  3303. wlc_lcnphy_set_tx_pwr_by_index(pi, 16);
  3304. wlc_lcnphy_tx_iqlo_cal(pi, &target_gains,
  3305. LCNPHY_CAL_FULL, false);
  3306. } else {
  3307. wlc_lcnphy_tx_iqlo_soft_cal_full(pi);
  3308. }
  3309. }
  3310. wlc_lcnphy_get_tx_iqcc(pi, &a, &b);
  3311. didq = wlc_lcnphy_get_tx_locc(pi);
  3312. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3313. tab.tbl_width = 32;
  3314. tab.tbl_ptr = &val;
  3315. tab.tbl_len = 1;
  3316. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  3317. for (idx = 0; idx < 128; idx++) {
  3318. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + idx;
  3319. wlc_lcnphy_read_table(pi, &tab);
  3320. val = (val & 0xfff00000) |
  3321. ((u32) (a & 0x3FF) << 10) | (b & 0x3ff);
  3322. wlc_lcnphy_write_table(pi, &tab);
  3323. val = didq;
  3324. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_LO_OFFSET + idx;
  3325. wlc_lcnphy_write_table(pi, &tab);
  3326. }
  3327. pi_lcn->lcnphy_cal_results.txiqlocal_a = a;
  3328. pi_lcn->lcnphy_cal_results.txiqlocal_b = b;
  3329. pi_lcn->lcnphy_cal_results.txiqlocal_didq = didq;
  3330. pi_lcn->lcnphy_cal_results.txiqlocal_ei0 = ei0;
  3331. pi_lcn->lcnphy_cal_results.txiqlocal_eq0 = eq0;
  3332. pi_lcn->lcnphy_cal_results.txiqlocal_fi0 = fi0;
  3333. pi_lcn->lcnphy_cal_results.txiqlocal_fq0 = fq0;
  3334. wlc_lcnphy_set_bbmult(pi, save_bb_mult);
  3335. wlc_lcnphy_set_pa_gain(pi, save_pa_gain);
  3336. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  3337. if (SAVE_txpwrctrl != LCNPHY_TX_PWR_CTRL_OFF)
  3338. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  3339. else
  3340. wlc_lcnphy_set_tx_pwr_by_index(pi, SAVE_txpwrindex);
  3341. }
  3342. s16 wlc_lcnphy_tempsense_new(struct brcms_phy *pi, bool mode)
  3343. {
  3344. u16 tempsenseval1, tempsenseval2;
  3345. s16 avg = 0;
  3346. bool suspend = false;
  3347. if (mode == 1) {
  3348. suspend = (0 == (bcma_read32(pi->d11core,
  3349. D11REGOFFS(maccontrol)) &
  3350. MCTL_EN_MAC));
  3351. if (!suspend)
  3352. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3353. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  3354. }
  3355. tempsenseval1 = read_phy_reg(pi, 0x476) & 0x1FF;
  3356. tempsenseval2 = read_phy_reg(pi, 0x477) & 0x1FF;
  3357. if (tempsenseval1 > 255)
  3358. avg = (s16) (tempsenseval1 - 512);
  3359. else
  3360. avg = (s16) tempsenseval1;
  3361. if (tempsenseval2 > 255)
  3362. avg += (s16) (tempsenseval2 - 512);
  3363. else
  3364. avg += (s16) tempsenseval2;
  3365. avg /= 2;
  3366. if (mode == 1) {
  3367. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  3368. udelay(100);
  3369. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  3370. if (!suspend)
  3371. wlapi_enable_mac(pi->sh->physhim);
  3372. }
  3373. return avg;
  3374. }
  3375. u16 wlc_lcnphy_tempsense(struct brcms_phy *pi, bool mode)
  3376. {
  3377. u16 tempsenseval1, tempsenseval2;
  3378. s32 avg = 0;
  3379. bool suspend = false;
  3380. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3381. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3382. if (mode == 1) {
  3383. suspend = (0 == (bcma_read32(pi->d11core,
  3384. D11REGOFFS(maccontrol)) &
  3385. MCTL_EN_MAC));
  3386. if (!suspend)
  3387. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3388. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  3389. }
  3390. tempsenseval1 = read_phy_reg(pi, 0x476) & 0x1FF;
  3391. tempsenseval2 = read_phy_reg(pi, 0x477) & 0x1FF;
  3392. if (tempsenseval1 > 255)
  3393. avg = (int)(tempsenseval1 - 512);
  3394. else
  3395. avg = (int)tempsenseval1;
  3396. if (pi_lcn->lcnphy_tempsense_option == 1 || pi->hwpwrctrl_capable) {
  3397. if (tempsenseval2 > 255)
  3398. avg = (int)(avg - tempsenseval2 + 512);
  3399. else
  3400. avg = (int)(avg - tempsenseval2);
  3401. } else {
  3402. if (tempsenseval2 > 255)
  3403. avg = (int)(avg + tempsenseval2 - 512);
  3404. else
  3405. avg = (int)(avg + tempsenseval2);
  3406. avg = avg / 2;
  3407. }
  3408. if (avg < 0)
  3409. avg = avg + 512;
  3410. if (pi_lcn->lcnphy_tempsense_option == 2)
  3411. avg = tempsenseval1;
  3412. if (mode)
  3413. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  3414. if (mode == 1) {
  3415. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  3416. udelay(100);
  3417. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  3418. if (!suspend)
  3419. wlapi_enable_mac(pi->sh->physhim);
  3420. }
  3421. return (u16) avg;
  3422. }
  3423. s8 wlc_lcnphy_tempsense_degree(struct brcms_phy *pi, bool mode)
  3424. {
  3425. s32 degree = wlc_lcnphy_tempsense_new(pi, mode);
  3426. degree =
  3427. ((degree <<
  3428. 10) + LCN_TEMPSENSE_OFFSET + (LCN_TEMPSENSE_DEN >> 1))
  3429. / LCN_TEMPSENSE_DEN;
  3430. return (s8) degree;
  3431. }
  3432. s8 wlc_lcnphy_vbatsense(struct brcms_phy *pi, bool mode)
  3433. {
  3434. u16 vbatsenseval;
  3435. s32 avg = 0;
  3436. bool suspend = false;
  3437. if (mode == 1) {
  3438. suspend = (0 == (bcma_read32(pi->d11core,
  3439. D11REGOFFS(maccontrol)) &
  3440. MCTL_EN_MAC));
  3441. if (!suspend)
  3442. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3443. wlc_lcnphy_vbat_temp_sense_setup(pi, VBATSENSE);
  3444. }
  3445. vbatsenseval = read_phy_reg(pi, 0x475) & 0x1FF;
  3446. if (vbatsenseval > 255)
  3447. avg = (s32) (vbatsenseval - 512);
  3448. else
  3449. avg = (s32) vbatsenseval;
  3450. avg = (avg * LCN_VBAT_SCALE_NOM +
  3451. (LCN_VBAT_SCALE_DEN >> 1)) / LCN_VBAT_SCALE_DEN;
  3452. if (mode == 1) {
  3453. if (!suspend)
  3454. wlapi_enable_mac(pi->sh->physhim);
  3455. }
  3456. return (s8) avg;
  3457. }
  3458. static void wlc_lcnphy_afe_clk_init(struct brcms_phy *pi, u8 mode)
  3459. {
  3460. u8 phybw40;
  3461. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3462. mod_phy_reg(pi, 0x6d1, (0x1 << 7), (1) << 7);
  3463. if (((mode == AFE_CLK_INIT_MODE_PAPD) && (phybw40 == 0)) ||
  3464. (mode == AFE_CLK_INIT_MODE_TXRX2X))
  3465. write_phy_reg(pi, 0x6d0, 0x7);
  3466. wlc_lcnphy_toggle_afe_pwdn(pi);
  3467. }
  3468. static void wlc_lcnphy_temp_adj(struct brcms_phy *pi)
  3469. {
  3470. }
  3471. static void wlc_lcnphy_glacial_timer_based_cal(struct brcms_phy *pi)
  3472. {
  3473. bool suspend;
  3474. s8 index;
  3475. u16 SAVE_pwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3476. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3477. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  3478. MCTL_EN_MAC));
  3479. if (!suspend)
  3480. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3481. wlc_lcnphy_deaf_mode(pi, true);
  3482. pi->phy_lastcal = pi->sh->now;
  3483. pi->phy_forcecal = false;
  3484. index = pi_lcn->lcnphy_current_index;
  3485. wlc_lcnphy_txpwrtbl_iqlo_cal(pi);
  3486. wlc_lcnphy_set_tx_pwr_by_index(pi, index);
  3487. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_pwrctrl);
  3488. wlc_lcnphy_deaf_mode(pi, false);
  3489. if (!suspend)
  3490. wlapi_enable_mac(pi->sh->physhim);
  3491. }
  3492. static void wlc_lcnphy_periodic_cal(struct brcms_phy *pi)
  3493. {
  3494. bool suspend, full_cal;
  3495. const struct lcnphy_rx_iqcomp *rx_iqcomp;
  3496. int rx_iqcomp_sz;
  3497. u16 SAVE_pwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3498. s8 index;
  3499. struct phytbl_info tab;
  3500. s32 a1, b0, b1;
  3501. s32 tssi, pwr, maxtargetpwr, mintargetpwr;
  3502. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3503. pi->phy_lastcal = pi->sh->now;
  3504. pi->phy_forcecal = false;
  3505. full_cal =
  3506. (pi_lcn->lcnphy_full_cal_channel !=
  3507. CHSPEC_CHANNEL(pi->radio_chanspec));
  3508. pi_lcn->lcnphy_full_cal_channel = CHSPEC_CHANNEL(pi->radio_chanspec);
  3509. index = pi_lcn->lcnphy_current_index;
  3510. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  3511. MCTL_EN_MAC));
  3512. if (!suspend) {
  3513. wlapi_bmac_write_shm(pi->sh->physhim, M_CTS_DURATION, 10000);
  3514. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3515. }
  3516. wlc_lcnphy_deaf_mode(pi, true);
  3517. wlc_lcnphy_txpwrtbl_iqlo_cal(pi);
  3518. rx_iqcomp = lcnphy_rx_iqcomp_table_rev0;
  3519. rx_iqcomp_sz = ARRAY_SIZE(lcnphy_rx_iqcomp_table_rev0);
  3520. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  3521. wlc_lcnphy_rx_iq_cal(pi, NULL, 0, true, false, 1, 40);
  3522. else
  3523. wlc_lcnphy_rx_iq_cal(pi, NULL, 0, true, false, 1, 127);
  3524. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi)) {
  3525. wlc_lcnphy_idle_tssi_est((struct brcms_phy_pub *) pi);
  3526. b0 = pi->txpa_2g[0];
  3527. b1 = pi->txpa_2g[1];
  3528. a1 = pi->txpa_2g[2];
  3529. maxtargetpwr = wlc_lcnphy_tssi2dbm(10, a1, b0, b1);
  3530. mintargetpwr = wlc_lcnphy_tssi2dbm(125, a1, b0, b1);
  3531. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3532. tab.tbl_width = 32;
  3533. tab.tbl_ptr = &pwr;
  3534. tab.tbl_len = 1;
  3535. tab.tbl_offset = 0;
  3536. for (tssi = 0; tssi < 128; tssi++) {
  3537. pwr = wlc_lcnphy_tssi2dbm(tssi, a1, b0, b1);
  3538. pwr = (pwr < mintargetpwr) ? mintargetpwr : pwr;
  3539. wlc_lcnphy_write_table(pi, &tab);
  3540. tab.tbl_offset++;
  3541. }
  3542. }
  3543. wlc_lcnphy_set_tx_pwr_by_index(pi, index);
  3544. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_pwrctrl);
  3545. wlc_lcnphy_deaf_mode(pi, false);
  3546. if (!suspend)
  3547. wlapi_enable_mac(pi->sh->physhim);
  3548. }
  3549. void wlc_lcnphy_calib_modes(struct brcms_phy *pi, uint mode)
  3550. {
  3551. u16 temp_new;
  3552. int temp1, temp2, temp_diff;
  3553. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3554. switch (mode) {
  3555. case PHY_PERICAL_CHAN:
  3556. break;
  3557. case PHY_FULLCAL:
  3558. wlc_lcnphy_periodic_cal(pi);
  3559. break;
  3560. case PHY_PERICAL_PHYINIT:
  3561. wlc_lcnphy_periodic_cal(pi);
  3562. break;
  3563. case PHY_PERICAL_WATCHDOG:
  3564. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  3565. temp_new = wlc_lcnphy_tempsense(pi, 0);
  3566. temp1 = LCNPHY_TEMPSENSE(temp_new);
  3567. temp2 = LCNPHY_TEMPSENSE(pi_lcn->lcnphy_cal_temper);
  3568. temp_diff = temp1 - temp2;
  3569. if ((pi_lcn->lcnphy_cal_counter > 90) ||
  3570. (temp_diff > 60) || (temp_diff < -60)) {
  3571. wlc_lcnphy_glacial_timer_based_cal(pi);
  3572. wlc_2064_vco_cal(pi);
  3573. pi_lcn->lcnphy_cal_temper = temp_new;
  3574. pi_lcn->lcnphy_cal_counter = 0;
  3575. } else
  3576. pi_lcn->lcnphy_cal_counter++;
  3577. }
  3578. break;
  3579. case LCNPHY_PERICAL_TEMPBASED_TXPWRCTRL:
  3580. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  3581. wlc_lcnphy_tx_power_adjustment(
  3582. (struct brcms_phy_pub *) pi);
  3583. break;
  3584. }
  3585. }
  3586. void wlc_lcnphy_get_tssi(struct brcms_phy *pi, s8 *ofdm_pwr, s8 *cck_pwr)
  3587. {
  3588. s8 cck_offset;
  3589. u16 status;
  3590. status = (read_phy_reg(pi, 0x4ab));
  3591. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi) &&
  3592. (status & (0x1 << 15))) {
  3593. *ofdm_pwr = (s8) (((read_phy_reg(pi, 0x4ab) & (0x1ff << 0))
  3594. >> 0) >> 1);
  3595. if (wlc_phy_tpc_isenabled_lcnphy(pi))
  3596. cck_offset = pi->tx_power_offset[TXP_FIRST_CCK];
  3597. else
  3598. cck_offset = 0;
  3599. *cck_pwr = *ofdm_pwr + cck_offset;
  3600. } else {
  3601. *cck_pwr = 0;
  3602. *ofdm_pwr = 0;
  3603. }
  3604. }
  3605. void wlc_phy_cal_init_lcnphy(struct brcms_phy *pi)
  3606. {
  3607. return;
  3608. }
  3609. void wlc_lcnphy_tx_power_adjustment(struct brcms_phy_pub *ppi)
  3610. {
  3611. s8 index;
  3612. u16 index2;
  3613. struct brcms_phy *pi = container_of(ppi, struct brcms_phy, pubpi_ro);
  3614. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3615. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3616. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi) &&
  3617. SAVE_txpwrctrl) {
  3618. index = wlc_lcnphy_tempcompensated_txpwrctrl(pi);
  3619. index2 = (u16) (index * 2);
  3620. mod_phy_reg(pi, 0x4a9, (0x1ff << 0), (index2) << 0);
  3621. pi_lcn->lcnphy_current_index =
  3622. (s8)((read_phy_reg(pi, 0x4a9) & 0xFF) / 2);
  3623. }
  3624. }
  3625. static void
  3626. wlc_lcnphy_load_tx_gain_table(struct brcms_phy *pi,
  3627. const struct lcnphy_tx_gain_tbl_entry *gain_table)
  3628. {
  3629. u32 j;
  3630. struct phytbl_info tab;
  3631. u32 val;
  3632. u16 pa_gain;
  3633. u16 gm_gain;
  3634. if (pi->sh->boardflags & BFL_FEM)
  3635. pa_gain = 0x10;
  3636. else
  3637. pa_gain = 0x60;
  3638. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3639. tab.tbl_width = 32;
  3640. tab.tbl_len = 1;
  3641. tab.tbl_ptr = &val;
  3642. /* fixed gm_gain value for iPA */
  3643. gm_gain = 15;
  3644. for (j = 0; j < 128; j++) {
  3645. if (pi->sh->boardflags & BFL_FEM)
  3646. gm_gain = gain_table[j].gm;
  3647. val = (((u32) pa_gain << 24) |
  3648. (gain_table[j].pad << 16) |
  3649. (gain_table[j].pga << 8) | gm_gain);
  3650. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + j;
  3651. wlc_lcnphy_write_table(pi, &tab);
  3652. val = (gain_table[j].dac << 28) | (gain_table[j].bb_mult << 20);
  3653. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + j;
  3654. wlc_lcnphy_write_table(pi, &tab);
  3655. }
  3656. }
  3657. static void wlc_lcnphy_load_rfpower(struct brcms_phy *pi)
  3658. {
  3659. struct phytbl_info tab;
  3660. u32 val, bbmult, rfgain;
  3661. u8 index;
  3662. u8 scale_factor = 1;
  3663. s16 temp, temp1, temp2, qQ, qQ1, qQ2, shift;
  3664. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3665. tab.tbl_width = 32;
  3666. tab.tbl_len = 1;
  3667. for (index = 0; index < 128; index++) {
  3668. tab.tbl_ptr = &bbmult;
  3669. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + index;
  3670. wlc_lcnphy_read_table(pi, &tab);
  3671. bbmult = bbmult >> 20;
  3672. tab.tbl_ptr = &rfgain;
  3673. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + index;
  3674. wlc_lcnphy_read_table(pi, &tab);
  3675. qm_log10((s32) (bbmult), 0, &temp1, &qQ1);
  3676. qm_log10((s32) (1 << 6), 0, &temp2, &qQ2);
  3677. if (qQ1 < qQ2) {
  3678. temp2 = qm_shr16(temp2, qQ2 - qQ1);
  3679. qQ = qQ1;
  3680. } else {
  3681. temp1 = qm_shr16(temp1, qQ1 - qQ2);
  3682. qQ = qQ2;
  3683. }
  3684. temp = qm_sub16(temp1, temp2);
  3685. if (qQ >= 4)
  3686. shift = qQ - 4;
  3687. else
  3688. shift = 4 - qQ;
  3689. val = (((index << shift) + (5 * temp) +
  3690. (1 << (scale_factor + shift - 3))) >> (scale_factor +
  3691. shift - 2));
  3692. tab.tbl_ptr = &val;
  3693. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_PWR_OFFSET + index;
  3694. wlc_lcnphy_write_table(pi, &tab);
  3695. }
  3696. }
  3697. static void wlc_lcnphy_bu_tweaks(struct brcms_phy *pi)
  3698. {
  3699. or_phy_reg(pi, 0x805, 0x1);
  3700. mod_phy_reg(pi, 0x42f, (0x7 << 0), (0x3) << 0);
  3701. mod_phy_reg(pi, 0x030, (0x7 << 0), (0x3) << 0);
  3702. write_phy_reg(pi, 0x414, 0x1e10);
  3703. write_phy_reg(pi, 0x415, 0x0640);
  3704. mod_phy_reg(pi, 0x4df, (0xff << 8), -9 << 8);
  3705. or_phy_reg(pi, 0x44a, 0x44);
  3706. write_phy_reg(pi, 0x44a, 0x80);
  3707. mod_phy_reg(pi, 0x434, (0xff << 0), (0xFD) << 0);
  3708. mod_phy_reg(pi, 0x420, (0xff << 0), (16) << 0);
  3709. if (!(pi->sh->boardrev < 0x1204))
  3710. mod_radio_reg(pi, RADIO_2064_REG09B, 0xF0, 0xF0);
  3711. write_phy_reg(pi, 0x7d6, 0x0902);
  3712. mod_phy_reg(pi, 0x429, (0xf << 0), (0x9) << 0);
  3713. mod_phy_reg(pi, 0x429, (0x3f << 4), (0xe) << 4);
  3714. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  3715. mod_phy_reg(pi, 0x423, (0xff << 0), (0x46) << 0);
  3716. mod_phy_reg(pi, 0x411, (0xff << 0), (1) << 0);
  3717. mod_phy_reg(pi, 0x434, (0xff << 0), (0xFF) << 0);
  3718. mod_phy_reg(pi, 0x656, (0xf << 0), (2) << 0);
  3719. mod_phy_reg(pi, 0x44d, (0x1 << 2), (1) << 2);
  3720. mod_radio_reg(pi, RADIO_2064_REG0F7, 0x4, 0x4);
  3721. mod_radio_reg(pi, RADIO_2064_REG0F1, 0x3, 0);
  3722. mod_radio_reg(pi, RADIO_2064_REG0F2, 0xF8, 0x90);
  3723. mod_radio_reg(pi, RADIO_2064_REG0F3, 0x3, 0x2);
  3724. mod_radio_reg(pi, RADIO_2064_REG0F3, 0xf0, 0xa0);
  3725. mod_radio_reg(pi, RADIO_2064_REG11F, 0x2, 0x2);
  3726. wlc_lcnphy_clear_tx_power_offsets(pi);
  3727. mod_phy_reg(pi, 0x4d0, (0x1ff << 6), (10) << 6);
  3728. }
  3729. }
  3730. static void wlc_lcnphy_rcal(struct brcms_phy *pi)
  3731. {
  3732. u8 rcal_value;
  3733. and_radio_reg(pi, RADIO_2064_REG05B, 0xfD);
  3734. or_radio_reg(pi, RADIO_2064_REG004, 0x40);
  3735. or_radio_reg(pi, RADIO_2064_REG120, 0x10);
  3736. or_radio_reg(pi, RADIO_2064_REG078, 0x80);
  3737. or_radio_reg(pi, RADIO_2064_REG129, 0x02);
  3738. or_radio_reg(pi, RADIO_2064_REG057, 0x01);
  3739. or_radio_reg(pi, RADIO_2064_REG05B, 0x02);
  3740. mdelay(5);
  3741. SPINWAIT(!wlc_radio_2064_rcal_done(pi), 10 * 1000 * 1000);
  3742. if (wlc_radio_2064_rcal_done(pi)) {
  3743. rcal_value = (u8) read_radio_reg(pi, RADIO_2064_REG05C);
  3744. rcal_value = rcal_value & 0x1f;
  3745. }
  3746. and_radio_reg(pi, RADIO_2064_REG05B, 0xfD);
  3747. and_radio_reg(pi, RADIO_2064_REG057, 0xFE);
  3748. }
  3749. static void wlc_lcnphy_rc_cal(struct brcms_phy *pi)
  3750. {
  3751. u8 dflt_rc_cal_val;
  3752. u16 flt_val;
  3753. dflt_rc_cal_val = 7;
  3754. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  3755. dflt_rc_cal_val = 11;
  3756. flt_val =
  3757. (dflt_rc_cal_val << 10) | (dflt_rc_cal_val << 5) |
  3758. (dflt_rc_cal_val);
  3759. write_phy_reg(pi, 0x933, flt_val);
  3760. write_phy_reg(pi, 0x934, flt_val);
  3761. write_phy_reg(pi, 0x935, flt_val);
  3762. write_phy_reg(pi, 0x936, flt_val);
  3763. write_phy_reg(pi, 0x937, (flt_val & 0x1FF));
  3764. return;
  3765. }
  3766. static void wlc_radio_2064_init(struct brcms_phy *pi)
  3767. {
  3768. u32 i;
  3769. const struct lcnphy_radio_regs *lcnphyregs = NULL;
  3770. lcnphyregs = lcnphy_radio_regs_2064;
  3771. for (i = 0; lcnphyregs[i].address != 0xffff; i++)
  3772. if (CHSPEC_IS5G(pi->radio_chanspec) && lcnphyregs[i].do_init_a)
  3773. write_radio_reg(pi,
  3774. ((lcnphyregs[i].address & 0x3fff) |
  3775. RADIO_DEFAULT_CORE),
  3776. (u16) lcnphyregs[i].init_a);
  3777. else if (lcnphyregs[i].do_init_g)
  3778. write_radio_reg(pi,
  3779. ((lcnphyregs[i].address & 0x3fff) |
  3780. RADIO_DEFAULT_CORE),
  3781. (u16) lcnphyregs[i].init_g);
  3782. write_radio_reg(pi, RADIO_2064_REG032, 0x62);
  3783. write_radio_reg(pi, RADIO_2064_REG033, 0x19);
  3784. write_radio_reg(pi, RADIO_2064_REG090, 0x10);
  3785. write_radio_reg(pi, RADIO_2064_REG010, 0x00);
  3786. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  3787. write_radio_reg(pi, RADIO_2064_REG060, 0x7f);
  3788. write_radio_reg(pi, RADIO_2064_REG061, 0x72);
  3789. write_radio_reg(pi, RADIO_2064_REG062, 0x7f);
  3790. }
  3791. write_radio_reg(pi, RADIO_2064_REG01D, 0x02);
  3792. write_radio_reg(pi, RADIO_2064_REG01E, 0x06);
  3793. mod_phy_reg(pi, 0x4ea, (0x7 << 0), 0 << 0);
  3794. mod_phy_reg(pi, 0x4ea, (0x7 << 3), 1 << 3);
  3795. mod_phy_reg(pi, 0x4ea, (0x7 << 6), 2 << 6);
  3796. mod_phy_reg(pi, 0x4ea, (0x7 << 9), 3 << 9);
  3797. mod_phy_reg(pi, 0x4ea, (0x7 << 12), 4 << 12);
  3798. write_phy_reg(pi, 0x4ea, 0x4688);
  3799. if (pi->sh->boardflags & BFL_FEM)
  3800. mod_phy_reg(pi, 0x4eb, (0x7 << 0), 2 << 0);
  3801. else
  3802. mod_phy_reg(pi, 0x4eb, (0x7 << 0), 3 << 0);
  3803. mod_phy_reg(pi, 0x4eb, (0x7 << 6), 0 << 6);
  3804. mod_phy_reg(pi, 0x46a, (0xffff << 0), 25 << 0);
  3805. wlc_lcnphy_set_tx_locc(pi, 0);
  3806. wlc_lcnphy_rcal(pi);
  3807. wlc_lcnphy_rc_cal(pi);
  3808. if (!(pi->sh->boardflags & BFL_FEM)) {
  3809. write_radio_reg(pi, RADIO_2064_REG032, 0x6f);
  3810. write_radio_reg(pi, RADIO_2064_REG033, 0x19);
  3811. write_radio_reg(pi, RADIO_2064_REG039, 0xe);
  3812. }
  3813. }
  3814. static void wlc_lcnphy_radio_init(struct brcms_phy *pi)
  3815. {
  3816. wlc_radio_2064_init(pi);
  3817. }
  3818. static void wlc_lcnphy_tbl_init(struct brcms_phy *pi)
  3819. {
  3820. uint idx;
  3821. u8 phybw40;
  3822. struct phytbl_info tab;
  3823. const struct phytbl_info *tb;
  3824. u32 val;
  3825. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3826. for (idx = 0; idx < dot11lcnphytbl_info_sz_rev0; idx++)
  3827. wlc_lcnphy_write_table(pi, &dot11lcnphytbl_info_rev0[idx]);
  3828. if (pi->sh->boardflags & BFL_FEM_BT) {
  3829. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  3830. tab.tbl_width = 16;
  3831. tab.tbl_ptr = &val;
  3832. tab.tbl_len = 1;
  3833. val = 100;
  3834. tab.tbl_offset = 4;
  3835. wlc_lcnphy_write_table(pi, &tab);
  3836. }
  3837. if (!(pi->sh->boardflags & BFL_FEM)) {
  3838. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  3839. tab.tbl_width = 16;
  3840. tab.tbl_ptr = &val;
  3841. tab.tbl_len = 1;
  3842. val = 150;
  3843. tab.tbl_offset = 0;
  3844. wlc_lcnphy_write_table(pi, &tab);
  3845. val = 220;
  3846. tab.tbl_offset = 1;
  3847. wlc_lcnphy_write_table(pi, &tab);
  3848. }
  3849. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  3850. if (pi->sh->boardflags & BFL_FEM)
  3851. wlc_lcnphy_load_tx_gain_table(
  3852. pi,
  3853. dot11lcnphy_2GHz_extPA_gaintable_rev0);
  3854. else
  3855. wlc_lcnphy_load_tx_gain_table(
  3856. pi,
  3857. dot11lcnphy_2GHz_gaintable_rev0);
  3858. }
  3859. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  3860. int l;
  3861. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  3862. l = dot11lcnphytbl_rx_gain_info_2G_rev2_sz;
  3863. if (pi->sh->boardflags & BFL_EXTLNA)
  3864. tb = dot11lcnphytbl_rx_gain_info_extlna_2G_rev2;
  3865. else
  3866. tb = dot11lcnphytbl_rx_gain_info_2G_rev2;
  3867. } else {
  3868. l = dot11lcnphytbl_rx_gain_info_5G_rev2_sz;
  3869. if (pi->sh->boardflags & BFL_EXTLNA_5GHz)
  3870. tb = dot11lcnphytbl_rx_gain_info_extlna_5G_rev2;
  3871. else
  3872. tb = dot11lcnphytbl_rx_gain_info_5G_rev2;
  3873. }
  3874. for (idx = 0; idx < l; idx++)
  3875. wlc_lcnphy_write_table(pi, &tb[idx]);
  3876. }
  3877. if (pi->sh->boardflags & BFL_FEM) {
  3878. if (pi->sh->boardflags & BFL_FEM_BT) {
  3879. if (pi->sh->boardrev < 0x1250)
  3880. tb = &dot11lcn_sw_ctrl_tbl_info_4313_bt_epa;
  3881. else
  3882. tb = &dot11lcn_sw_ctrl_tbl_info_4313_bt_epa_p250;
  3883. } else {
  3884. tb = &dot11lcn_sw_ctrl_tbl_info_4313_epa;
  3885. }
  3886. } else {
  3887. if (pi->sh->boardflags & BFL_FEM_BT)
  3888. tb = &dot11lcn_sw_ctrl_tbl_info_4313_bt_ipa;
  3889. else
  3890. tb = &dot11lcn_sw_ctrl_tbl_info_4313;
  3891. }
  3892. wlc_lcnphy_write_table(pi, tb);
  3893. wlc_lcnphy_load_rfpower(pi);
  3894. wlc_lcnphy_clear_papd_comptable(pi);
  3895. }
  3896. static void wlc_lcnphy_rev0_baseband_init(struct brcms_phy *pi)
  3897. {
  3898. u16 afectrl1;
  3899. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3900. write_radio_reg(pi, RADIO_2064_REG11C, 0x0);
  3901. write_phy_reg(pi, 0x43b, 0x0);
  3902. write_phy_reg(pi, 0x43c, 0x0);
  3903. write_phy_reg(pi, 0x44c, 0x0);
  3904. write_phy_reg(pi, 0x4e6, 0x0);
  3905. write_phy_reg(pi, 0x4f9, 0x0);
  3906. write_phy_reg(pi, 0x4b0, 0x0);
  3907. write_phy_reg(pi, 0x938, 0x0);
  3908. write_phy_reg(pi, 0x4b0, 0x0);
  3909. write_phy_reg(pi, 0x44e, 0);
  3910. or_phy_reg(pi, 0x567, 0x03);
  3911. or_phy_reg(pi, 0x44a, 0x44);
  3912. write_phy_reg(pi, 0x44a, 0x80);
  3913. if (!(pi->sh->boardflags & BFL_FEM))
  3914. wlc_lcnphy_set_tx_pwr_by_index(pi, 52);
  3915. if (0) {
  3916. afectrl1 = 0;
  3917. afectrl1 = (u16) ((pi_lcn->lcnphy_rssi_vf) |
  3918. (pi_lcn->lcnphy_rssi_vc << 4) |
  3919. (pi_lcn->lcnphy_rssi_gs << 10));
  3920. write_phy_reg(pi, 0x43e, afectrl1);
  3921. }
  3922. mod_phy_reg(pi, 0x634, (0xff << 0), 0xC << 0);
  3923. if (pi->sh->boardflags & BFL_FEM) {
  3924. mod_phy_reg(pi, 0x634, (0xff << 0), 0xA << 0);
  3925. write_phy_reg(pi, 0x910, 0x1);
  3926. }
  3927. mod_phy_reg(pi, 0x448, (0x3 << 8), 1 << 8);
  3928. mod_phy_reg(pi, 0x608, (0xff << 0), 0x17 << 0);
  3929. mod_phy_reg(pi, 0x604, (0x7ff << 0), 0x3EA << 0);
  3930. }
  3931. static void wlc_lcnphy_rev2_baseband_init(struct brcms_phy *pi)
  3932. {
  3933. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  3934. mod_phy_reg(pi, 0x416, (0xff << 0), 80 << 0);
  3935. mod_phy_reg(pi, 0x416, (0xff << 8), 80 << 8);
  3936. }
  3937. }
  3938. static void wlc_lcnphy_agc_temp_init(struct brcms_phy *pi)
  3939. {
  3940. s16 temp;
  3941. struct phytbl_info tab;
  3942. u32 tableBuffer[2];
  3943. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3944. temp = (s16) read_phy_reg(pi, 0x4df);
  3945. pi_lcn->lcnphy_ofdmgainidxtableoffset = (temp & (0xff << 0)) >> 0;
  3946. if (pi_lcn->lcnphy_ofdmgainidxtableoffset > 127)
  3947. pi_lcn->lcnphy_ofdmgainidxtableoffset -= 256;
  3948. pi_lcn->lcnphy_dsssgainidxtableoffset = (temp & (0xff << 8)) >> 8;
  3949. if (pi_lcn->lcnphy_dsssgainidxtableoffset > 127)
  3950. pi_lcn->lcnphy_dsssgainidxtableoffset -= 256;
  3951. tab.tbl_ptr = tableBuffer;
  3952. tab.tbl_len = 2;
  3953. tab.tbl_id = 17;
  3954. tab.tbl_offset = 59;
  3955. tab.tbl_width = 32;
  3956. wlc_lcnphy_read_table(pi, &tab);
  3957. if (tableBuffer[0] > 63)
  3958. tableBuffer[0] -= 128;
  3959. pi_lcn->lcnphy_tr_R_gain_val = tableBuffer[0];
  3960. if (tableBuffer[1] > 63)
  3961. tableBuffer[1] -= 128;
  3962. pi_lcn->lcnphy_tr_T_gain_val = tableBuffer[1];
  3963. temp = (s16) (read_phy_reg(pi, 0x434) & (0xff << 0));
  3964. if (temp > 127)
  3965. temp -= 256;
  3966. pi_lcn->lcnphy_input_pwr_offset_db = (s8) temp;
  3967. pi_lcn->lcnphy_Med_Low_Gain_db =
  3968. (read_phy_reg(pi, 0x424) & (0xff << 8)) >> 8;
  3969. pi_lcn->lcnphy_Very_Low_Gain_db =
  3970. (read_phy_reg(pi, 0x425) & (0xff << 0)) >> 0;
  3971. tab.tbl_ptr = tableBuffer;
  3972. tab.tbl_len = 2;
  3973. tab.tbl_id = LCNPHY_TBL_ID_GAIN_IDX;
  3974. tab.tbl_offset = 28;
  3975. tab.tbl_width = 32;
  3976. wlc_lcnphy_read_table(pi, &tab);
  3977. pi_lcn->lcnphy_gain_idx_14_lowword = tableBuffer[0];
  3978. pi_lcn->lcnphy_gain_idx_14_hiword = tableBuffer[1];
  3979. }
  3980. static void wlc_lcnphy_baseband_init(struct brcms_phy *pi)
  3981. {
  3982. wlc_lcnphy_tbl_init(pi);
  3983. wlc_lcnphy_rev0_baseband_init(pi);
  3984. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  3985. wlc_lcnphy_rev2_baseband_init(pi);
  3986. wlc_lcnphy_bu_tweaks(pi);
  3987. }
  3988. void wlc_phy_init_lcnphy(struct brcms_phy *pi)
  3989. {
  3990. u8 phybw40;
  3991. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3992. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3993. pi_lcn->lcnphy_cal_counter = 0;
  3994. pi_lcn->lcnphy_cal_temper = pi_lcn->lcnphy_rawtempsense;
  3995. or_phy_reg(pi, 0x44a, 0x80);
  3996. and_phy_reg(pi, 0x44a, 0x7f);
  3997. wlc_lcnphy_afe_clk_init(pi, AFE_CLK_INIT_MODE_TXRX2X);
  3998. write_phy_reg(pi, 0x60a, 160);
  3999. write_phy_reg(pi, 0x46a, 25);
  4000. wlc_lcnphy_baseband_init(pi);
  4001. wlc_lcnphy_radio_init(pi);
  4002. if (CHSPEC_IS2G(pi->radio_chanspec))
  4003. wlc_lcnphy_tx_pwr_ctrl_init((struct brcms_phy_pub *) pi);
  4004. wlc_phy_chanspec_set((struct brcms_phy_pub *) pi, pi->radio_chanspec);
  4005. bcma_chipco_regctl_maskset(&pi->d11core->bus->drv_cc, 0, ~0xf, 0x9);
  4006. bcma_chipco_chipctl_maskset(&pi->d11core->bus->drv_cc, 0, 0x0,
  4007. 0x03CDDDDD);
  4008. if ((pi->sh->boardflags & BFL_FEM)
  4009. && wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  4010. wlc_lcnphy_set_tx_pwr_by_index(pi, FIXED_TXPWR);
  4011. wlc_lcnphy_agc_temp_init(pi);
  4012. wlc_lcnphy_temp_adj(pi);
  4013. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  4014. udelay(100);
  4015. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  4016. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_HW);
  4017. pi_lcn->lcnphy_noise_samples = LCNPHY_NOISE_SAMPLES_DEFAULT;
  4018. wlc_lcnphy_calib_modes(pi, PHY_PERICAL_PHYINIT);
  4019. }
  4020. static bool wlc_phy_txpwr_srom_read_lcnphy(struct brcms_phy *pi)
  4021. {
  4022. s8 txpwr = 0;
  4023. int i;
  4024. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4025. struct ssb_sprom *sprom = &pi->d11core->bus->sprom;
  4026. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  4027. u16 cckpo = 0;
  4028. u32 offset_ofdm, offset_mcs;
  4029. pi_lcn->lcnphy_tr_isolation_mid = sprom->fem.ghz2.tr_iso;
  4030. pi_lcn->lcnphy_rx_power_offset = sprom->rxpo2g;
  4031. pi->txpa_2g[0] = sprom->pa0b0;
  4032. pi->txpa_2g[1] = sprom->pa0b1;
  4033. pi->txpa_2g[2] = sprom->pa0b2;
  4034. pi_lcn->lcnphy_rssi_vf = sprom->rssismf2g;
  4035. pi_lcn->lcnphy_rssi_vc = sprom->rssismc2g;
  4036. pi_lcn->lcnphy_rssi_gs = sprom->rssisav2g;
  4037. pi_lcn->lcnphy_rssi_vf_lowtemp = pi_lcn->lcnphy_rssi_vf;
  4038. pi_lcn->lcnphy_rssi_vc_lowtemp = pi_lcn->lcnphy_rssi_vc;
  4039. pi_lcn->lcnphy_rssi_gs_lowtemp = pi_lcn->lcnphy_rssi_gs;
  4040. pi_lcn->lcnphy_rssi_vf_hightemp = pi_lcn->lcnphy_rssi_vf;
  4041. pi_lcn->lcnphy_rssi_vc_hightemp = pi_lcn->lcnphy_rssi_vc;
  4042. pi_lcn->lcnphy_rssi_gs_hightemp = pi_lcn->lcnphy_rssi_gs;
  4043. txpwr = sprom->core_pwr_info[0].maxpwr_2g;
  4044. pi->tx_srom_max_2g = txpwr;
  4045. for (i = 0; i < PWRTBL_NUM_COEFF; i++) {
  4046. pi->txpa_2g_low_temp[i] = pi->txpa_2g[i];
  4047. pi->txpa_2g_high_temp[i] = pi->txpa_2g[i];
  4048. }
  4049. cckpo = sprom->cck2gpo;
  4050. offset_ofdm = sprom->ofdm2gpo;
  4051. if (cckpo) {
  4052. uint max_pwr_chan = txpwr;
  4053. for (i = TXP_FIRST_CCK; i <= TXP_LAST_CCK; i++) {
  4054. pi->tx_srom_max_rate_2g[i] =
  4055. max_pwr_chan - ((cckpo & 0xf) * 2);
  4056. cckpo >>= 4;
  4057. }
  4058. for (i = TXP_FIRST_OFDM; i <= TXP_LAST_OFDM; i++) {
  4059. pi->tx_srom_max_rate_2g[i] =
  4060. max_pwr_chan -
  4061. ((offset_ofdm & 0xf) * 2);
  4062. offset_ofdm >>= 4;
  4063. }
  4064. } else {
  4065. u8 opo = 0;
  4066. opo = sprom->opo;
  4067. for (i = TXP_FIRST_CCK; i <= TXP_LAST_CCK; i++)
  4068. pi->tx_srom_max_rate_2g[i] = txpwr;
  4069. for (i = TXP_FIRST_OFDM; i <= TXP_LAST_OFDM; i++) {
  4070. pi->tx_srom_max_rate_2g[i] = txpwr -
  4071. ((offset_ofdm & 0xf) * 2);
  4072. offset_ofdm >>= 4;
  4073. }
  4074. offset_mcs = sprom->mcs2gpo[1] << 16;
  4075. offset_mcs |= sprom->mcs2gpo[0];
  4076. pi_lcn->lcnphy_mcs20_po = offset_mcs;
  4077. for (i = TXP_FIRST_SISO_MCS_20;
  4078. i <= TXP_LAST_SISO_MCS_20; i++) {
  4079. pi->tx_srom_max_rate_2g[i] =
  4080. txpwr - ((offset_mcs & 0xf) * 2);
  4081. offset_mcs >>= 4;
  4082. }
  4083. }
  4084. pi_lcn->lcnphy_rawtempsense = sprom->rawtempsense;
  4085. pi_lcn->lcnphy_measPower = sprom->measpower;
  4086. pi_lcn->lcnphy_tempsense_slope = sprom->tempsense_slope;
  4087. pi_lcn->lcnphy_hw_iqcal_en = sprom->hw_iqcal_en;
  4088. pi_lcn->lcnphy_iqcal_swp_dis = sprom->iqcal_swp_dis;
  4089. pi_lcn->lcnphy_tempcorrx = sprom->tempcorrx;
  4090. pi_lcn->lcnphy_tempsense_option = sprom->tempsense_option;
  4091. pi_lcn->lcnphy_freqoffset_corr = sprom->freqoffset_corr;
  4092. if (sprom->ant_available_bg > 1)
  4093. wlc_phy_ant_rxdiv_set((struct brcms_phy_pub *) pi,
  4094. sprom->ant_available_bg);
  4095. }
  4096. pi_lcn->lcnphy_cck_dig_filt_type = -1;
  4097. return true;
  4098. }
  4099. void wlc_2064_vco_cal(struct brcms_phy *pi)
  4100. {
  4101. u8 calnrst;
  4102. mod_radio_reg(pi, RADIO_2064_REG057, 1 << 3, 1 << 3);
  4103. calnrst = (u8) read_radio_reg(pi, RADIO_2064_REG056) & 0xf8;
  4104. write_radio_reg(pi, RADIO_2064_REG056, calnrst);
  4105. udelay(1);
  4106. write_radio_reg(pi, RADIO_2064_REG056, calnrst | 0x03);
  4107. udelay(1);
  4108. write_radio_reg(pi, RADIO_2064_REG056, calnrst | 0x07);
  4109. udelay(300);
  4110. mod_radio_reg(pi, RADIO_2064_REG057, 1 << 3, 0);
  4111. }
  4112. bool wlc_phy_tpc_isenabled_lcnphy(struct brcms_phy *pi)
  4113. {
  4114. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  4115. return false;
  4116. else
  4117. return (LCNPHY_TX_PWR_CTRL_HW ==
  4118. wlc_lcnphy_get_tx_pwr_ctrl((pi)));
  4119. }
  4120. void wlc_phy_txpower_recalc_target_lcnphy(struct brcms_phy *pi)
  4121. {
  4122. u16 pwr_ctrl;
  4123. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  4124. wlc_lcnphy_calib_modes(pi, LCNPHY_PERICAL_TEMPBASED_TXPWRCTRL);
  4125. } else if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi)) {
  4126. pwr_ctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  4127. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  4128. wlc_lcnphy_txpower_recalc_target(pi);
  4129. wlc_lcnphy_set_tx_pwr_ctrl(pi, pwr_ctrl);
  4130. }
  4131. }
  4132. void wlc_phy_chanspec_set_lcnphy(struct brcms_phy *pi, u16 chanspec)
  4133. {
  4134. u8 channel = CHSPEC_CHANNEL(chanspec);
  4135. wlc_phy_chanspec_radio_set((struct brcms_phy_pub *)pi, chanspec);
  4136. wlc_lcnphy_set_chanspec_tweaks(pi, pi->radio_chanspec);
  4137. or_phy_reg(pi, 0x44a, 0x44);
  4138. write_phy_reg(pi, 0x44a, 0x80);
  4139. wlc_lcnphy_radio_2064_channel_tune_4313(pi, channel);
  4140. udelay(1000);
  4141. wlc_lcnphy_toggle_afe_pwdn(pi);
  4142. write_phy_reg(pi, 0x657, lcnphy_sfo_cfg[channel - 1].ptcentreTs20);
  4143. write_phy_reg(pi, 0x658, lcnphy_sfo_cfg[channel - 1].ptcentreFactor);
  4144. if (CHSPEC_CHANNEL(pi->radio_chanspec) == 14) {
  4145. mod_phy_reg(pi, 0x448, (0x3 << 8), (2) << 8);
  4146. wlc_lcnphy_load_tx_iir_filter(pi, false, 3);
  4147. } else {
  4148. mod_phy_reg(pi, 0x448, (0x3 << 8), (1) << 8);
  4149. wlc_lcnphy_load_tx_iir_filter(pi, false, 2);
  4150. }
  4151. if (pi->sh->boardflags & BFL_FEM)
  4152. wlc_lcnphy_load_tx_iir_filter(pi, true, 0);
  4153. else
  4154. wlc_lcnphy_load_tx_iir_filter(pi, true, 3);
  4155. mod_phy_reg(pi, 0x4eb, (0x7 << 3), (1) << 3);
  4156. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  4157. wlc_lcnphy_tssi_setup(pi);
  4158. }
  4159. void wlc_phy_detach_lcnphy(struct brcms_phy *pi)
  4160. {
  4161. kfree(pi->u.pi_lcnphy);
  4162. }
  4163. bool wlc_phy_attach_lcnphy(struct brcms_phy *pi)
  4164. {
  4165. struct brcms_phy_lcnphy *pi_lcn;
  4166. pi->u.pi_lcnphy = kzalloc(sizeof(struct brcms_phy_lcnphy), GFP_ATOMIC);
  4167. if (pi->u.pi_lcnphy == NULL)
  4168. return false;
  4169. pi_lcn = pi->u.pi_lcnphy;
  4170. if (0 == (pi->sh->boardflags & BFL_NOPA)) {
  4171. pi->hwpwrctrl = true;
  4172. pi->hwpwrctrl_capable = true;
  4173. }
  4174. pi->xtalfreq = bcma_chipco_get_alp_clock(&pi->d11core->bus->drv_cc);
  4175. pi_lcn->lcnphy_papd_rxGnCtrl_init = 0;
  4176. pi->pi_fptr.init = wlc_phy_init_lcnphy;
  4177. pi->pi_fptr.calinit = wlc_phy_cal_init_lcnphy;
  4178. pi->pi_fptr.chanset = wlc_phy_chanspec_set_lcnphy;
  4179. pi->pi_fptr.txpwrrecalc = wlc_phy_txpower_recalc_target_lcnphy;
  4180. pi->pi_fptr.txiqccget = wlc_lcnphy_get_tx_iqcc;
  4181. pi->pi_fptr.txiqccset = wlc_lcnphy_set_tx_iqcc;
  4182. pi->pi_fptr.txloccget = wlc_lcnphy_get_tx_locc;
  4183. pi->pi_fptr.radioloftget = wlc_lcnphy_get_radio_loft;
  4184. pi->pi_fptr.detach = wlc_phy_detach_lcnphy;
  4185. if (!wlc_phy_txpwr_srom_read_lcnphy(pi))
  4186. return false;
  4187. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  4188. if (pi_lcn->lcnphy_tempsense_option == 3) {
  4189. pi->hwpwrctrl = true;
  4190. pi->hwpwrctrl_capable = true;
  4191. pi->temppwrctrl_capable = false;
  4192. } else {
  4193. pi->hwpwrctrl = false;
  4194. pi->hwpwrctrl_capable = false;
  4195. pi->temppwrctrl_capable = true;
  4196. }
  4197. }
  4198. return true;
  4199. }
  4200. static void wlc_lcnphy_set_rx_gain(struct brcms_phy *pi, u32 gain)
  4201. {
  4202. u16 trsw, ext_lna, lna1, lna2, tia, biq0, biq1, gain0_15, gain16_19;
  4203. trsw = (gain & ((u32) 1 << 28)) ? 0 : 1;
  4204. ext_lna = (u16) (gain >> 29) & 0x01;
  4205. lna1 = (u16) (gain >> 0) & 0x0f;
  4206. lna2 = (u16) (gain >> 4) & 0x0f;
  4207. tia = (u16) (gain >> 8) & 0xf;
  4208. biq0 = (u16) (gain >> 12) & 0xf;
  4209. biq1 = (u16) (gain >> 16) & 0xf;
  4210. gain0_15 = (u16) ((lna1 & 0x3) | ((lna1 & 0x3) << 2) |
  4211. ((lna2 & 0x3) << 4) | ((lna2 & 0x3) << 6) |
  4212. ((tia & 0xf) << 8) | ((biq0 & 0xf) << 12));
  4213. gain16_19 = biq1;
  4214. mod_phy_reg(pi, 0x44d, (0x1 << 0), trsw << 0);
  4215. mod_phy_reg(pi, 0x4b1, (0x1 << 9), ext_lna << 9);
  4216. mod_phy_reg(pi, 0x4b1, (0x1 << 10), ext_lna << 10);
  4217. mod_phy_reg(pi, 0x4b6, (0xffff << 0), gain0_15 << 0);
  4218. mod_phy_reg(pi, 0x4b7, (0xf << 0), gain16_19 << 0);
  4219. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  4220. mod_phy_reg(pi, 0x4b1, (0x3 << 11), lna1 << 11);
  4221. mod_phy_reg(pi, 0x4e6, (0x3 << 3), lna1 << 3);
  4222. }
  4223. wlc_lcnphy_rx_gain_override_enable(pi, true);
  4224. }
  4225. static u32 wlc_lcnphy_get_receive_power(struct brcms_phy *pi, s32 *gain_index)
  4226. {
  4227. u32 received_power = 0;
  4228. s32 max_index = 0;
  4229. u32 gain_code = 0;
  4230. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4231. max_index = 36;
  4232. if (*gain_index >= 0)
  4233. gain_code = lcnphy_23bitgaincode_table[*gain_index];
  4234. if (-1 == *gain_index) {
  4235. *gain_index = 0;
  4236. while ((*gain_index <= (s32) max_index)
  4237. && (received_power < 700)) {
  4238. wlc_lcnphy_set_rx_gain(pi,
  4239. lcnphy_23bitgaincode_table
  4240. [*gain_index]);
  4241. received_power =
  4242. wlc_lcnphy_measure_digital_power(
  4243. pi,
  4244. pi_lcn->
  4245. lcnphy_noise_samples);
  4246. (*gain_index)++;
  4247. }
  4248. (*gain_index)--;
  4249. } else {
  4250. wlc_lcnphy_set_rx_gain(pi, gain_code);
  4251. received_power =
  4252. wlc_lcnphy_measure_digital_power(pi,
  4253. pi_lcn->
  4254. lcnphy_noise_samples);
  4255. }
  4256. return received_power;
  4257. }
  4258. s32 wlc_lcnphy_rx_signal_power(struct brcms_phy *pi, s32 gain_index)
  4259. {
  4260. s32 gain = 0;
  4261. s32 nominal_power_db;
  4262. s32 log_val, gain_mismatch, desired_gain, input_power_offset_db,
  4263. input_power_db;
  4264. s32 received_power, temperature;
  4265. u32 power;
  4266. u32 msb1, msb2, val1, val2, diff1, diff2;
  4267. uint freq;
  4268. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4269. received_power = wlc_lcnphy_get_receive_power(pi, &gain_index);
  4270. gain = lcnphy_gain_table[gain_index];
  4271. nominal_power_db = read_phy_reg(pi, 0x425) >> 8;
  4272. power = (received_power * 16);
  4273. msb1 = ffs(power) - 1;
  4274. msb2 = msb1 + 1;
  4275. val1 = 1 << msb1;
  4276. val2 = 1 << msb2;
  4277. diff1 = (power - val1);
  4278. diff2 = (val2 - power);
  4279. if (diff1 < diff2)
  4280. log_val = msb1;
  4281. else
  4282. log_val = msb2;
  4283. log_val = log_val * 3;
  4284. gain_mismatch = (nominal_power_db / 2) - (log_val);
  4285. desired_gain = gain + gain_mismatch;
  4286. input_power_offset_db = read_phy_reg(pi, 0x434) & 0xFF;
  4287. if (input_power_offset_db > 127)
  4288. input_power_offset_db -= 256;
  4289. input_power_db = input_power_offset_db - desired_gain;
  4290. input_power_db =
  4291. input_power_db + lcnphy_gain_index_offset_for_rssi[gain_index];
  4292. freq = wlc_phy_channel2freq(CHSPEC_CHANNEL(pi->radio_chanspec));
  4293. if ((freq > 2427) && (freq <= 2467))
  4294. input_power_db = input_power_db - 1;
  4295. temperature = pi_lcn->lcnphy_lastsensed_temperature;
  4296. if ((temperature - 15) < -30)
  4297. input_power_db =
  4298. input_power_db +
  4299. (((temperature - 10 - 25) * 286) >> 12) -
  4300. 7;
  4301. else if ((temperature - 15) < 4)
  4302. input_power_db =
  4303. input_power_db +
  4304. (((temperature - 10 - 25) * 286) >> 12) -
  4305. 3;
  4306. else
  4307. input_power_db = input_power_db +
  4308. (((temperature - 10 - 25) * 286) >> 12);
  4309. wlc_lcnphy_rx_gain_override_enable(pi, 0);
  4310. return input_power_db;
  4311. }