phy_lcn.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248
  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. mod_phy_reg(pi, 0x4b1, (0x3 << 11), lna1 << 11);
  1057. mod_phy_reg(pi, 0x4e6, (0x3 << 3), lna1 << 3);
  1058. }
  1059. static void wlc_lcnphy_set_trsw_override(struct brcms_phy *pi, bool tx, bool rx)
  1060. {
  1061. mod_phy_reg(pi, 0x44d,
  1062. (0x1 << 1) |
  1063. (0x1 << 0), (tx ? (0x1 << 1) : 0) | (rx ? (0x1 << 0) : 0));
  1064. or_phy_reg(pi, 0x44c, (0x1 << 1) | (0x1 << 0));
  1065. }
  1066. static void wlc_lcnphy_clear_trsw_override(struct brcms_phy *pi)
  1067. {
  1068. and_phy_reg(pi, 0x44c, (u16) ~((0x1 << 1) | (0x1 << 0)));
  1069. }
  1070. static void wlc_lcnphy_set_rx_iq_comp(struct brcms_phy *pi, u16 a, u16 b)
  1071. {
  1072. mod_phy_reg(pi, 0x645, (0x3ff << 0), (a) << 0);
  1073. mod_phy_reg(pi, 0x646, (0x3ff << 0), (b) << 0);
  1074. mod_phy_reg(pi, 0x647, (0x3ff << 0), (a) << 0);
  1075. mod_phy_reg(pi, 0x648, (0x3ff << 0), (b) << 0);
  1076. mod_phy_reg(pi, 0x649, (0x3ff << 0), (a) << 0);
  1077. mod_phy_reg(pi, 0x64a, (0x3ff << 0), (b) << 0);
  1078. }
  1079. static bool
  1080. wlc_lcnphy_rx_iq_est(struct brcms_phy *pi,
  1081. u16 num_samps,
  1082. u8 wait_time, struct lcnphy_iq_est *iq_est)
  1083. {
  1084. int wait_count = 0;
  1085. bool result = true;
  1086. u8 phybw40;
  1087. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  1088. mod_phy_reg(pi, 0x6da, (0x1 << 5), (1) << 5);
  1089. mod_phy_reg(pi, 0x410, (0x1 << 3), (0) << 3);
  1090. mod_phy_reg(pi, 0x482, (0xffff << 0), (num_samps) << 0);
  1091. mod_phy_reg(pi, 0x481, (0xff << 0), ((u16) wait_time) << 0);
  1092. mod_phy_reg(pi, 0x481, (0x1 << 8), (0) << 8);
  1093. mod_phy_reg(pi, 0x481, (0x1 << 9), (1) << 9);
  1094. while (read_phy_reg(pi, 0x481) & (0x1 << 9)) {
  1095. if (wait_count > (10 * 500)) {
  1096. result = false;
  1097. goto cleanup;
  1098. }
  1099. udelay(100);
  1100. wait_count++;
  1101. }
  1102. iq_est->iq_prod = ((u32) read_phy_reg(pi, 0x483) << 16) |
  1103. (u32) read_phy_reg(pi, 0x484);
  1104. iq_est->i_pwr = ((u32) read_phy_reg(pi, 0x485) << 16) |
  1105. (u32) read_phy_reg(pi, 0x486);
  1106. iq_est->q_pwr = ((u32) read_phy_reg(pi, 0x487) << 16) |
  1107. (u32) read_phy_reg(pi, 0x488);
  1108. cleanup:
  1109. mod_phy_reg(pi, 0x410, (0x1 << 3), (1) << 3);
  1110. mod_phy_reg(pi, 0x6da, (0x1 << 5), (0) << 5);
  1111. return result;
  1112. }
  1113. static bool wlc_lcnphy_calc_rx_iq_comp(struct brcms_phy *pi, u16 num_samps)
  1114. {
  1115. #define LCNPHY_MIN_RXIQ_PWR 2
  1116. bool result;
  1117. u16 a0_new, b0_new;
  1118. struct lcnphy_iq_est iq_est = { 0, 0, 0 };
  1119. s32 a, b, temp;
  1120. s16 iq_nbits, qq_nbits, arsh, brsh;
  1121. s32 iq;
  1122. u32 ii, qq;
  1123. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1124. a0_new = ((read_phy_reg(pi, 0x645) & (0x3ff << 0)) >> 0);
  1125. b0_new = ((read_phy_reg(pi, 0x646) & (0x3ff << 0)) >> 0);
  1126. mod_phy_reg(pi, 0x6d1, (0x1 << 2), (0) << 2);
  1127. mod_phy_reg(pi, 0x64b, (0x1 << 6), (1) << 6);
  1128. wlc_lcnphy_set_rx_iq_comp(pi, 0, 0);
  1129. result = wlc_lcnphy_rx_iq_est(pi, num_samps, 32, &iq_est);
  1130. if (!result)
  1131. goto cleanup;
  1132. iq = (s32) iq_est.iq_prod;
  1133. ii = iq_est.i_pwr;
  1134. qq = iq_est.q_pwr;
  1135. if ((ii + qq) < LCNPHY_MIN_RXIQ_PWR) {
  1136. result = false;
  1137. goto cleanup;
  1138. }
  1139. iq_nbits = wlc_phy_nbits(iq);
  1140. qq_nbits = wlc_phy_nbits(qq);
  1141. arsh = 10 - (30 - iq_nbits);
  1142. if (arsh >= 0) {
  1143. a = (-(iq << (30 - iq_nbits)) + (ii >> (1 + arsh)));
  1144. temp = (s32) (ii >> arsh);
  1145. if (temp == 0)
  1146. return false;
  1147. } else {
  1148. a = (-(iq << (30 - iq_nbits)) + (ii << (-1 - arsh)));
  1149. temp = (s32) (ii << -arsh);
  1150. if (temp == 0)
  1151. return false;
  1152. }
  1153. a /= temp;
  1154. brsh = qq_nbits - 31 + 20;
  1155. if (brsh >= 0) {
  1156. b = (qq << (31 - qq_nbits));
  1157. temp = (s32) (ii >> brsh);
  1158. if (temp == 0)
  1159. return false;
  1160. } else {
  1161. b = (qq << (31 - qq_nbits));
  1162. temp = (s32) (ii << -brsh);
  1163. if (temp == 0)
  1164. return false;
  1165. }
  1166. b /= temp;
  1167. b -= a * a;
  1168. b = (s32) int_sqrt((unsigned long) b);
  1169. b -= (1 << 10);
  1170. a0_new = (u16) (a & 0x3ff);
  1171. b0_new = (u16) (b & 0x3ff);
  1172. cleanup:
  1173. wlc_lcnphy_set_rx_iq_comp(pi, a0_new, b0_new);
  1174. mod_phy_reg(pi, 0x64b, (0x1 << 0), (1) << 0);
  1175. mod_phy_reg(pi, 0x64b, (0x1 << 3), (1) << 3);
  1176. pi_lcn->lcnphy_cal_results.rxiqcal_coeff_a0 = a0_new;
  1177. pi_lcn->lcnphy_cal_results.rxiqcal_coeff_b0 = b0_new;
  1178. return result;
  1179. }
  1180. static u32 wlc_lcnphy_measure_digital_power(struct brcms_phy *pi, u16 nsamples)
  1181. {
  1182. struct lcnphy_iq_est iq_est = { 0, 0, 0 };
  1183. if (!wlc_lcnphy_rx_iq_est(pi, nsamples, 32, &iq_est))
  1184. return 0;
  1185. return (iq_est.i_pwr + iq_est.q_pwr) / nsamples;
  1186. }
  1187. static bool wlc_lcnphy_rx_iq_cal_gain(struct brcms_phy *pi, u16 biq1_gain,
  1188. u16 tia_gain, u16 lna2_gain)
  1189. {
  1190. u32 i_thresh_l, q_thresh_l;
  1191. u32 i_thresh_h, q_thresh_h;
  1192. struct lcnphy_iq_est iq_est_h, iq_est_l;
  1193. wlc_lcnphy_set_rx_gain_by_distribution(pi, 0, 0, 0, biq1_gain, tia_gain,
  1194. lna2_gain, 0);
  1195. wlc_lcnphy_rx_gain_override_enable(pi, true);
  1196. wlc_lcnphy_start_tx_tone(pi, 2000, (40 >> 1), 0);
  1197. udelay(500);
  1198. write_radio_reg(pi, RADIO_2064_REG112, 0);
  1199. if (!wlc_lcnphy_rx_iq_est(pi, 1024, 32, &iq_est_l))
  1200. return false;
  1201. wlc_lcnphy_start_tx_tone(pi, 2000, 40, 0);
  1202. udelay(500);
  1203. write_radio_reg(pi, RADIO_2064_REG112, 0);
  1204. if (!wlc_lcnphy_rx_iq_est(pi, 1024, 32, &iq_est_h))
  1205. return false;
  1206. i_thresh_l = (iq_est_l.i_pwr << 1);
  1207. i_thresh_h = (iq_est_l.i_pwr << 2) + iq_est_l.i_pwr;
  1208. q_thresh_l = (iq_est_l.q_pwr << 1);
  1209. q_thresh_h = (iq_est_l.q_pwr << 2) + iq_est_l.q_pwr;
  1210. if ((iq_est_h.i_pwr > i_thresh_l) &&
  1211. (iq_est_h.i_pwr < i_thresh_h) &&
  1212. (iq_est_h.q_pwr > q_thresh_l) &&
  1213. (iq_est_h.q_pwr < q_thresh_h))
  1214. return true;
  1215. return false;
  1216. }
  1217. static bool
  1218. wlc_lcnphy_rx_iq_cal(struct brcms_phy *pi,
  1219. const struct lcnphy_rx_iqcomp *iqcomp,
  1220. int iqcomp_sz, bool tx_switch, bool rx_switch, int module,
  1221. int tx_gain_idx)
  1222. {
  1223. struct lcnphy_txgains old_gains;
  1224. u16 tx_pwr_ctrl;
  1225. u8 tx_gain_index_old = 0;
  1226. bool result = false, tx_gain_override_old = false;
  1227. u16 i, Core1TxControl_old, RFOverride0_old,
  1228. RFOverrideVal0_old, rfoverride2_old, rfoverride2val_old,
  1229. rfoverride3_old, rfoverride3val_old, rfoverride4_old,
  1230. rfoverride4val_old, afectrlovr_old, afectrlovrval_old;
  1231. int tia_gain, lna2_gain, biq1_gain;
  1232. bool set_gain;
  1233. u16 old_sslpnCalibClkEnCtrl, old_sslpnRxFeClkEnCtrl;
  1234. u16 values_to_save[11];
  1235. s16 *ptr;
  1236. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1237. ptr = kmalloc(sizeof(s16) * 131, GFP_ATOMIC);
  1238. if (NULL == ptr)
  1239. return false;
  1240. if (module == 2) {
  1241. while (iqcomp_sz--) {
  1242. if (iqcomp[iqcomp_sz].chan ==
  1243. CHSPEC_CHANNEL(pi->radio_chanspec)) {
  1244. wlc_lcnphy_set_rx_iq_comp(pi,
  1245. (u16)
  1246. iqcomp[iqcomp_sz].a,
  1247. (u16)
  1248. iqcomp[iqcomp_sz].b);
  1249. result = true;
  1250. break;
  1251. }
  1252. }
  1253. goto cal_done;
  1254. }
  1255. WARN_ON(module != 1);
  1256. tx_pwr_ctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  1257. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  1258. for (i = 0; i < 11; i++)
  1259. values_to_save[i] =
  1260. read_radio_reg(pi, rxiq_cal_rf_reg[i]);
  1261. Core1TxControl_old = read_phy_reg(pi, 0x631);
  1262. or_phy_reg(pi, 0x631, 0x0015);
  1263. RFOverride0_old = read_phy_reg(pi, 0x44c);
  1264. RFOverrideVal0_old = read_phy_reg(pi, 0x44d);
  1265. rfoverride2_old = read_phy_reg(pi, 0x4b0);
  1266. rfoverride2val_old = read_phy_reg(pi, 0x4b1);
  1267. rfoverride3_old = read_phy_reg(pi, 0x4f9);
  1268. rfoverride3val_old = read_phy_reg(pi, 0x4fa);
  1269. rfoverride4_old = read_phy_reg(pi, 0x938);
  1270. rfoverride4val_old = read_phy_reg(pi, 0x939);
  1271. afectrlovr_old = read_phy_reg(pi, 0x43b);
  1272. afectrlovrval_old = read_phy_reg(pi, 0x43c);
  1273. old_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  1274. old_sslpnRxFeClkEnCtrl = read_phy_reg(pi, 0x6db);
  1275. tx_gain_override_old = wlc_lcnphy_tx_gain_override_enabled(pi);
  1276. if (tx_gain_override_old) {
  1277. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  1278. tx_gain_index_old = pi_lcn->lcnphy_current_index;
  1279. }
  1280. wlc_lcnphy_set_tx_pwr_by_index(pi, tx_gain_idx);
  1281. mod_phy_reg(pi, 0x4f9, (0x1 << 0), 1 << 0);
  1282. mod_phy_reg(pi, 0x4fa, (0x1 << 0), 0 << 0);
  1283. mod_phy_reg(pi, 0x43b, (0x1 << 1), 1 << 1);
  1284. mod_phy_reg(pi, 0x43c, (0x1 << 1), 0 << 1);
  1285. write_radio_reg(pi, RADIO_2064_REG116, 0x06);
  1286. write_radio_reg(pi, RADIO_2064_REG12C, 0x07);
  1287. write_radio_reg(pi, RADIO_2064_REG06A, 0xd3);
  1288. write_radio_reg(pi, RADIO_2064_REG098, 0x03);
  1289. write_radio_reg(pi, RADIO_2064_REG00B, 0x7);
  1290. mod_radio_reg(pi, RADIO_2064_REG113, 1 << 4, 1 << 4);
  1291. write_radio_reg(pi, RADIO_2064_REG01D, 0x01);
  1292. write_radio_reg(pi, RADIO_2064_REG114, 0x01);
  1293. write_radio_reg(pi, RADIO_2064_REG02E, 0x10);
  1294. write_radio_reg(pi, RADIO_2064_REG12A, 0x08);
  1295. mod_phy_reg(pi, 0x938, (0x1 << 0), 1 << 0);
  1296. mod_phy_reg(pi, 0x939, (0x1 << 0), 0 << 0);
  1297. mod_phy_reg(pi, 0x938, (0x1 << 1), 1 << 1);
  1298. mod_phy_reg(pi, 0x939, (0x1 << 1), 1 << 1);
  1299. mod_phy_reg(pi, 0x938, (0x1 << 2), 1 << 2);
  1300. mod_phy_reg(pi, 0x939, (0x1 << 2), 1 << 2);
  1301. mod_phy_reg(pi, 0x938, (0x1 << 3), 1 << 3);
  1302. mod_phy_reg(pi, 0x939, (0x1 << 3), 1 << 3);
  1303. mod_phy_reg(pi, 0x938, (0x1 << 5), 1 << 5);
  1304. mod_phy_reg(pi, 0x939, (0x1 << 5), 0 << 5);
  1305. mod_phy_reg(pi, 0x43b, (0x1 << 0), 1 << 0);
  1306. mod_phy_reg(pi, 0x43c, (0x1 << 0), 0 << 0);
  1307. write_phy_reg(pi, 0x6da, 0xffff);
  1308. or_phy_reg(pi, 0x6db, 0x3);
  1309. wlc_lcnphy_set_trsw_override(pi, tx_switch, rx_switch);
  1310. set_gain = false;
  1311. lna2_gain = 3;
  1312. while ((lna2_gain >= 0) && !set_gain) {
  1313. tia_gain = 4;
  1314. while ((tia_gain >= 0) && !set_gain) {
  1315. biq1_gain = 6;
  1316. while ((biq1_gain >= 0) && !set_gain) {
  1317. set_gain = wlc_lcnphy_rx_iq_cal_gain(pi,
  1318. (u16)
  1319. biq1_gain,
  1320. (u16)
  1321. tia_gain,
  1322. (u16)
  1323. lna2_gain);
  1324. biq1_gain -= 1;
  1325. }
  1326. tia_gain -= 1;
  1327. }
  1328. lna2_gain -= 1;
  1329. }
  1330. if (set_gain)
  1331. result = wlc_lcnphy_calc_rx_iq_comp(pi, 1024);
  1332. else
  1333. result = false;
  1334. wlc_lcnphy_stop_tx_tone(pi);
  1335. write_phy_reg(pi, 0x631, Core1TxControl_old);
  1336. write_phy_reg(pi, 0x44c, RFOverrideVal0_old);
  1337. write_phy_reg(pi, 0x44d, RFOverrideVal0_old);
  1338. write_phy_reg(pi, 0x4b0, rfoverride2_old);
  1339. write_phy_reg(pi, 0x4b1, rfoverride2val_old);
  1340. write_phy_reg(pi, 0x4f9, rfoverride3_old);
  1341. write_phy_reg(pi, 0x4fa, rfoverride3val_old);
  1342. write_phy_reg(pi, 0x938, rfoverride4_old);
  1343. write_phy_reg(pi, 0x939, rfoverride4val_old);
  1344. write_phy_reg(pi, 0x43b, afectrlovr_old);
  1345. write_phy_reg(pi, 0x43c, afectrlovrval_old);
  1346. write_phy_reg(pi, 0x6da, old_sslpnCalibClkEnCtrl);
  1347. write_phy_reg(pi, 0x6db, old_sslpnRxFeClkEnCtrl);
  1348. wlc_lcnphy_clear_trsw_override(pi);
  1349. mod_phy_reg(pi, 0x44c, (0x1 << 2), 0 << 2);
  1350. for (i = 0; i < 11; i++)
  1351. write_radio_reg(pi, rxiq_cal_rf_reg[i],
  1352. values_to_save[i]);
  1353. if (tx_gain_override_old)
  1354. wlc_lcnphy_set_tx_pwr_by_index(pi, tx_gain_index_old);
  1355. else
  1356. wlc_lcnphy_disable_tx_gain_override(pi);
  1357. wlc_lcnphy_set_tx_pwr_ctrl(pi, tx_pwr_ctrl);
  1358. wlc_lcnphy_rx_gain_override_enable(pi, false);
  1359. cal_done:
  1360. kfree(ptr);
  1361. return result;
  1362. }
  1363. s8 wlc_lcnphy_get_current_tx_pwr_idx(struct brcms_phy *pi)
  1364. {
  1365. s8 index;
  1366. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1367. if (txpwrctrl_off(pi))
  1368. index = pi_lcn->lcnphy_current_index;
  1369. else if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  1370. index = (s8) (wlc_lcnphy_get_current_tx_pwr_idx_if_pwrctrl_on(
  1371. pi) / 2);
  1372. else
  1373. index = pi_lcn->lcnphy_current_index;
  1374. return index;
  1375. }
  1376. void wlc_lcnphy_crsuprs(struct brcms_phy *pi, int channel)
  1377. {
  1378. u16 afectrlovr, afectrlovrval;
  1379. afectrlovr = read_phy_reg(pi, 0x43b);
  1380. afectrlovrval = read_phy_reg(pi, 0x43c);
  1381. if (channel != 0) {
  1382. mod_phy_reg(pi, 0x43b, (0x1 << 1), (1) << 1);
  1383. mod_phy_reg(pi, 0x43c, (0x1 << 1), (0) << 1);
  1384. mod_phy_reg(pi, 0x43b, (0x1 << 4), (1) << 4);
  1385. mod_phy_reg(pi, 0x43c, (0x1 << 6), (0) << 6);
  1386. write_phy_reg(pi, 0x44b, 0xffff);
  1387. wlc_lcnphy_tx_pu(pi, 1);
  1388. mod_phy_reg(pi, 0x634, (0xff << 8), (0) << 8);
  1389. or_phy_reg(pi, 0x6da, 0x0080);
  1390. or_phy_reg(pi, 0x00a, 0x228);
  1391. } else {
  1392. and_phy_reg(pi, 0x00a, ~(0x228));
  1393. and_phy_reg(pi, 0x6da, 0xFF7F);
  1394. write_phy_reg(pi, 0x43b, afectrlovr);
  1395. write_phy_reg(pi, 0x43c, afectrlovrval);
  1396. }
  1397. }
  1398. static void wlc_lcnphy_toggle_afe_pwdn(struct brcms_phy *pi)
  1399. {
  1400. u16 save_AfeCtrlOvrVal, save_AfeCtrlOvr;
  1401. save_AfeCtrlOvrVal = read_phy_reg(pi, 0x43c);
  1402. save_AfeCtrlOvr = read_phy_reg(pi, 0x43b);
  1403. write_phy_reg(pi, 0x43c, save_AfeCtrlOvrVal | 0x1);
  1404. write_phy_reg(pi, 0x43b, save_AfeCtrlOvr | 0x1);
  1405. write_phy_reg(pi, 0x43c, save_AfeCtrlOvrVal & 0xfffe);
  1406. write_phy_reg(pi, 0x43b, save_AfeCtrlOvr & 0xfffe);
  1407. write_phy_reg(pi, 0x43c, save_AfeCtrlOvrVal);
  1408. write_phy_reg(pi, 0x43b, save_AfeCtrlOvr);
  1409. }
  1410. static void
  1411. wlc_lcnphy_txrx_spur_avoidance_mode(struct brcms_phy *pi, bool enable)
  1412. {
  1413. if (enable) {
  1414. write_phy_reg(pi, 0x942, 0x7);
  1415. write_phy_reg(pi, 0x93b, ((1 << 13) + 23));
  1416. write_phy_reg(pi, 0x93c, ((1 << 13) + 1989));
  1417. write_phy_reg(pi, 0x44a, 0x084);
  1418. write_phy_reg(pi, 0x44a, 0x080);
  1419. write_phy_reg(pi, 0x6d3, 0x2222);
  1420. write_phy_reg(pi, 0x6d3, 0x2220);
  1421. } else {
  1422. write_phy_reg(pi, 0x942, 0x0);
  1423. write_phy_reg(pi, 0x93b, ((0 << 13) + 23));
  1424. write_phy_reg(pi, 0x93c, ((0 << 13) + 1989));
  1425. }
  1426. wlapi_switch_macfreq(pi->sh->physhim, enable);
  1427. }
  1428. static void
  1429. wlc_lcnphy_set_chanspec_tweaks(struct brcms_phy *pi, u16 chanspec)
  1430. {
  1431. u8 channel = CHSPEC_CHANNEL(chanspec);
  1432. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1433. if (channel == 14)
  1434. mod_phy_reg(pi, 0x448, (0x3 << 8), (2) << 8);
  1435. else
  1436. mod_phy_reg(pi, 0x448, (0x3 << 8), (1) << 8);
  1437. pi_lcn->lcnphy_bandedge_corr = 2;
  1438. if (channel == 1)
  1439. pi_lcn->lcnphy_bandedge_corr = 4;
  1440. if (channel == 1 || channel == 2 || channel == 3 ||
  1441. channel == 4 || channel == 9 ||
  1442. channel == 10 || channel == 11 || channel == 12) {
  1443. si_pmu_pllcontrol(pi->sh->sih, 0x2, 0xffffffff, 0x03000c04);
  1444. si_pmu_pllcontrol(pi->sh->sih, 0x3, 0xffffff, 0x0);
  1445. si_pmu_pllcontrol(pi->sh->sih, 0x4, 0xffffffff, 0x200005c0);
  1446. si_pmu_pllupd(pi->sh->sih);
  1447. write_phy_reg(pi, 0x942, 0);
  1448. wlc_lcnphy_txrx_spur_avoidance_mode(pi, false);
  1449. pi_lcn->lcnphy_spurmod = false;
  1450. mod_phy_reg(pi, 0x424, (0xff << 8), (0x1b) << 8);
  1451. write_phy_reg(pi, 0x425, 0x5907);
  1452. } else {
  1453. si_pmu_pllcontrol(pi->sh->sih, 0x2, 0xffffffff, 0x03140c04);
  1454. si_pmu_pllcontrol(pi->sh->sih, 0x3, 0xffffff, 0x333333);
  1455. si_pmu_pllcontrol(pi->sh->sih, 0x4, 0xffffffff, 0x202c2820);
  1456. si_pmu_pllupd(pi->sh->sih);
  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. u8 reg038[14] = {0xd, 0xe, 0xd, 0xd, 0xd, 0xc,
  1589. 0xa, 0xb, 0xb, 0x3, 0x3, 0x2, 0x0, 0x0};
  1590. write_radio_reg(pi, RADIO_2064_REG02A, 0xf);
  1591. write_radio_reg(pi, RADIO_2064_REG091, 0x3);
  1592. write_radio_reg(pi, RADIO_2064_REG038, 0x3);
  1593. write_radio_reg(pi, RADIO_2064_REG038, reg038[channel - 1]);
  1594. }
  1595. }
  1596. static int
  1597. wlc_lcnphy_load_tx_iir_filter(struct brcms_phy *pi, bool is_ofdm, s16 filt_type)
  1598. {
  1599. s16 filt_index = -1;
  1600. int j;
  1601. u16 addr[] = {
  1602. 0x910,
  1603. 0x91e,
  1604. 0x91f,
  1605. 0x924,
  1606. 0x925,
  1607. 0x926,
  1608. 0x920,
  1609. 0x921,
  1610. 0x927,
  1611. 0x928,
  1612. 0x929,
  1613. 0x922,
  1614. 0x923,
  1615. 0x930,
  1616. 0x931,
  1617. 0x932
  1618. };
  1619. u16 addr_ofdm[] = {
  1620. 0x90f,
  1621. 0x900,
  1622. 0x901,
  1623. 0x906,
  1624. 0x907,
  1625. 0x908,
  1626. 0x902,
  1627. 0x903,
  1628. 0x909,
  1629. 0x90a,
  1630. 0x90b,
  1631. 0x904,
  1632. 0x905,
  1633. 0x90c,
  1634. 0x90d,
  1635. 0x90e
  1636. };
  1637. if (!is_ofdm) {
  1638. for (j = 0; j < LCNPHY_NUM_TX_DIG_FILTERS_CCK; j++) {
  1639. if (filt_type == LCNPHY_txdigfiltcoeffs_cck[j][0]) {
  1640. filt_index = (s16) j;
  1641. break;
  1642. }
  1643. }
  1644. if (filt_index != -1) {
  1645. for (j = 0; j < LCNPHY_NUM_DIG_FILT_COEFFS; j++)
  1646. write_phy_reg(pi, addr[j],
  1647. LCNPHY_txdigfiltcoeffs_cck
  1648. [filt_index][j + 1]);
  1649. }
  1650. } else {
  1651. for (j = 0; j < LCNPHY_NUM_TX_DIG_FILTERS_OFDM; j++) {
  1652. if (filt_type == LCNPHY_txdigfiltcoeffs_ofdm[j][0]) {
  1653. filt_index = (s16) j;
  1654. break;
  1655. }
  1656. }
  1657. if (filt_index != -1) {
  1658. for (j = 0; j < LCNPHY_NUM_DIG_FILT_COEFFS; j++)
  1659. write_phy_reg(pi, addr_ofdm[j],
  1660. LCNPHY_txdigfiltcoeffs_ofdm
  1661. [filt_index][j + 1]);
  1662. }
  1663. }
  1664. return (filt_index != -1) ? 0 : -1;
  1665. }
  1666. static u16 wlc_lcnphy_get_pa_gain(struct brcms_phy *pi)
  1667. {
  1668. u16 pa_gain;
  1669. pa_gain = (read_phy_reg(pi, 0x4fb) &
  1670. LCNPHY_txgainctrlovrval1_pagain_ovr_val1_MASK) >>
  1671. LCNPHY_txgainctrlovrval1_pagain_ovr_val1_SHIFT;
  1672. return pa_gain;
  1673. }
  1674. static void wlc_lcnphy_set_tx_gain(struct brcms_phy *pi,
  1675. struct lcnphy_txgains *target_gains)
  1676. {
  1677. u16 pa_gain = wlc_lcnphy_get_pa_gain(pi);
  1678. mod_phy_reg(
  1679. pi, 0x4b5,
  1680. (0xffff << 0),
  1681. ((target_gains->gm_gain) |
  1682. (target_gains->pga_gain << 8)) <<
  1683. 0);
  1684. mod_phy_reg(pi, 0x4fb,
  1685. (0x7fff << 0),
  1686. ((target_gains->pad_gain) | (pa_gain << 8)) << 0);
  1687. mod_phy_reg(
  1688. pi, 0x4fc,
  1689. (0xffff << 0),
  1690. ((target_gains->gm_gain) |
  1691. (target_gains->pga_gain << 8)) <<
  1692. 0);
  1693. mod_phy_reg(pi, 0x4fd,
  1694. (0x7fff << 0),
  1695. ((target_gains->pad_gain) | (pa_gain << 8)) << 0);
  1696. wlc_lcnphy_set_dac_gain(pi, target_gains->dac_gain);
  1697. wlc_lcnphy_enable_tx_gain_override(pi);
  1698. }
  1699. static u8 wlc_lcnphy_get_bbmult(struct brcms_phy *pi)
  1700. {
  1701. u16 m0m1;
  1702. struct phytbl_info tab;
  1703. tab.tbl_ptr = &m0m1;
  1704. tab.tbl_len = 1;
  1705. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  1706. tab.tbl_offset = 87;
  1707. tab.tbl_width = 16;
  1708. wlc_lcnphy_read_table(pi, &tab);
  1709. return (u8) ((m0m1 & 0xff00) >> 8);
  1710. }
  1711. static void wlc_lcnphy_set_bbmult(struct brcms_phy *pi, u8 m0)
  1712. {
  1713. u16 m0m1 = (u16) m0 << 8;
  1714. struct phytbl_info tab;
  1715. tab.tbl_ptr = &m0m1;
  1716. tab.tbl_len = 1;
  1717. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  1718. tab.tbl_offset = 87;
  1719. tab.tbl_width = 16;
  1720. wlc_lcnphy_write_table(pi, &tab);
  1721. }
  1722. static void wlc_lcnphy_clear_tx_power_offsets(struct brcms_phy *pi)
  1723. {
  1724. u32 data_buf[64];
  1725. struct phytbl_info tab;
  1726. memset(data_buf, 0, sizeof(data_buf));
  1727. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1728. tab.tbl_width = 32;
  1729. tab.tbl_ptr = data_buf;
  1730. if (!wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  1731. tab.tbl_len = 30;
  1732. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  1733. wlc_lcnphy_write_table(pi, &tab);
  1734. }
  1735. tab.tbl_len = 64;
  1736. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_MAC_OFFSET;
  1737. wlc_lcnphy_write_table(pi, &tab);
  1738. }
  1739. enum lcnphy_tssi_mode {
  1740. LCNPHY_TSSI_PRE_PA,
  1741. LCNPHY_TSSI_POST_PA,
  1742. LCNPHY_TSSI_EXT
  1743. };
  1744. static void
  1745. wlc_lcnphy_set_tssi_mux(struct brcms_phy *pi, enum lcnphy_tssi_mode pos)
  1746. {
  1747. mod_phy_reg(pi, 0x4d7, (0x1 << 0), (0x1) << 0);
  1748. mod_phy_reg(pi, 0x4d7, (0x1 << 6), (1) << 6);
  1749. if (LCNPHY_TSSI_POST_PA == pos) {
  1750. mod_phy_reg(pi, 0x4d9, (0x1 << 2), (0) << 2);
  1751. mod_phy_reg(pi, 0x4d9, (0x1 << 3), (1) << 3);
  1752. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1753. mod_radio_reg(pi, RADIO_2064_REG086, 0x4, 0x4);
  1754. } else {
  1755. mod_radio_reg(pi, RADIO_2064_REG03A, 1, 0x1);
  1756. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 0x8);
  1757. mod_radio_reg(pi, RADIO_2064_REG028, 0x1, 0x0);
  1758. mod_radio_reg(pi, RADIO_2064_REG11A, 0x4, 1<<2);
  1759. mod_radio_reg(pi, RADIO_2064_REG036, 0x10, 0x0);
  1760. mod_radio_reg(pi, RADIO_2064_REG11A, 0x10, 1<<4);
  1761. mod_radio_reg(pi, RADIO_2064_REG036, 0x3, 0x0);
  1762. mod_radio_reg(pi, RADIO_2064_REG035, 0xff, 0x77);
  1763. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, 0xe<<1);
  1764. mod_radio_reg(pi, RADIO_2064_REG112, 0x80, 1<<7);
  1765. mod_radio_reg(pi, RADIO_2064_REG005, 0x7, 1<<1);
  1766. mod_radio_reg(pi, RADIO_2064_REG029, 0xf0, 0<<4);
  1767. }
  1768. } else {
  1769. mod_phy_reg(pi, 0x4d9, (0x1 << 2), (0x1) << 2);
  1770. mod_phy_reg(pi, 0x4d9, (0x1 << 3), (0) << 3);
  1771. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1772. mod_radio_reg(pi, RADIO_2064_REG086, 0x4, 0x4);
  1773. } else {
  1774. mod_radio_reg(pi, RADIO_2064_REG03A, 1, 0);
  1775. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 0x8);
  1776. }
  1777. }
  1778. mod_phy_reg(pi, 0x637, (0x3 << 14), (0) << 14);
  1779. if (LCNPHY_TSSI_EXT == pos) {
  1780. write_radio_reg(pi, RADIO_2064_REG07F, 1);
  1781. mod_radio_reg(pi, RADIO_2064_REG005, 0x7, 0x2);
  1782. mod_radio_reg(pi, RADIO_2064_REG112, 0x80, 0x1 << 7);
  1783. mod_radio_reg(pi, RADIO_2064_REG028, 0x1f, 0x3);
  1784. }
  1785. }
  1786. static u16 wlc_lcnphy_rfseq_tbl_adc_pwrup(struct brcms_phy *pi)
  1787. {
  1788. u16 N1, N2, N3, N4, N5, N6, N;
  1789. N1 = ((read_phy_reg(pi, 0x4a5) & (0xff << 0))
  1790. >> 0);
  1791. N2 = 1 << ((read_phy_reg(pi, 0x4a5) & (0x7 << 12))
  1792. >> 12);
  1793. N3 = ((read_phy_reg(pi, 0x40d) & (0xff << 0))
  1794. >> 0);
  1795. N4 = 1 << ((read_phy_reg(pi, 0x40d) & (0x7 << 8))
  1796. >> 8);
  1797. N5 = ((read_phy_reg(pi, 0x4a2) & (0xff << 0))
  1798. >> 0);
  1799. N6 = 1 << ((read_phy_reg(pi, 0x4a2) & (0x7 << 8))
  1800. >> 8);
  1801. N = 2 * (N1 + N2 + N3 + N4 + 2 * (N5 + N6)) + 80;
  1802. if (N < 1600)
  1803. N = 1600;
  1804. return N;
  1805. }
  1806. static void wlc_lcnphy_pwrctrl_rssiparams(struct brcms_phy *pi)
  1807. {
  1808. u16 auxpga_vmid, auxpga_vmid_temp, auxpga_gain_temp;
  1809. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1810. auxpga_vmid = (2 << 8) |
  1811. (pi_lcn->lcnphy_rssi_vc << 4) | pi_lcn->lcnphy_rssi_vf;
  1812. auxpga_vmid_temp = (2 << 8) | (8 << 4) | 4;
  1813. auxpga_gain_temp = 2;
  1814. mod_phy_reg(pi, 0x4d8, (0x1 << 0), (0) << 0);
  1815. mod_phy_reg(pi, 0x4d8, (0x1 << 1), (0) << 1);
  1816. mod_phy_reg(pi, 0x4d7, (0x1 << 3), (0) << 3);
  1817. mod_phy_reg(pi, 0x4db,
  1818. (0x3ff << 0) |
  1819. (0x7 << 12),
  1820. (auxpga_vmid << 0) | (pi_lcn->lcnphy_rssi_gs << 12));
  1821. mod_phy_reg(pi, 0x4dc,
  1822. (0x3ff << 0) |
  1823. (0x7 << 12),
  1824. (auxpga_vmid << 0) | (pi_lcn->lcnphy_rssi_gs << 12));
  1825. mod_phy_reg(pi, 0x40a,
  1826. (0x3ff << 0) |
  1827. (0x7 << 12),
  1828. (auxpga_vmid << 0) | (pi_lcn->lcnphy_rssi_gs << 12));
  1829. mod_phy_reg(pi, 0x40b,
  1830. (0x3ff << 0) |
  1831. (0x7 << 12),
  1832. (auxpga_vmid_temp << 0) | (auxpga_gain_temp << 12));
  1833. mod_phy_reg(pi, 0x40c,
  1834. (0x3ff << 0) |
  1835. (0x7 << 12),
  1836. (auxpga_vmid_temp << 0) | (auxpga_gain_temp << 12));
  1837. mod_radio_reg(pi, RADIO_2064_REG082, (1 << 5), (1 << 5));
  1838. mod_radio_reg(pi, RADIO_2064_REG07C, (1 << 0), (1 << 0));
  1839. }
  1840. static void wlc_lcnphy_tssi_setup(struct brcms_phy *pi)
  1841. {
  1842. struct phytbl_info tab;
  1843. u32 rfseq, ind;
  1844. u8 tssi_sel;
  1845. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1846. tab.tbl_width = 32;
  1847. tab.tbl_ptr = &ind;
  1848. tab.tbl_len = 1;
  1849. tab.tbl_offset = 0;
  1850. for (ind = 0; ind < 128; ind++) {
  1851. wlc_lcnphy_write_table(pi, &tab);
  1852. tab.tbl_offset++;
  1853. }
  1854. tab.tbl_offset = 704;
  1855. for (ind = 0; ind < 128; ind++) {
  1856. wlc_lcnphy_write_table(pi, &tab);
  1857. tab.tbl_offset++;
  1858. }
  1859. mod_phy_reg(pi, 0x503, (0x1 << 0), (0) << 0);
  1860. mod_phy_reg(pi, 0x503, (0x1 << 2), (0) << 2);
  1861. mod_phy_reg(pi, 0x503, (0x1 << 4), (1) << 4);
  1862. if (pi->sh->boardflags & BFL_FEM) {
  1863. tssi_sel = 0x1;
  1864. wlc_lcnphy_set_tssi_mux(pi, LCNPHY_TSSI_EXT);
  1865. } else {
  1866. tssi_sel = 0xe;
  1867. wlc_lcnphy_set_tssi_mux(pi, LCNPHY_TSSI_POST_PA);
  1868. }
  1869. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0) << 14);
  1870. mod_phy_reg(pi, 0x4a4, (0x1 << 15), (1) << 15);
  1871. mod_phy_reg(pi, 0x4d0, (0x1 << 5), (0) << 5);
  1872. mod_phy_reg(pi, 0x4a4, (0x1ff << 0), (0) << 0);
  1873. mod_phy_reg(pi, 0x4a5, (0xff << 0), (255) << 0);
  1874. mod_phy_reg(pi, 0x4a5, (0x7 << 12), (5) << 12);
  1875. mod_phy_reg(pi, 0x4a5, (0x7 << 8), (0) << 8);
  1876. mod_phy_reg(pi, 0x40d, (0xff << 0), (64) << 0);
  1877. mod_phy_reg(pi, 0x40d, (0x7 << 8), (4) << 8);
  1878. mod_phy_reg(pi, 0x4a2, (0xff << 0), (64) << 0);
  1879. mod_phy_reg(pi, 0x4a2, (0x7 << 8), (4) << 8);
  1880. mod_phy_reg(pi, 0x4d0, (0x1ff << 6), (0) << 6);
  1881. mod_phy_reg(pi, 0x4a8, (0xff << 0), (0x1) << 0);
  1882. wlc_lcnphy_clear_tx_power_offsets(pi);
  1883. mod_phy_reg(pi, 0x4a6, (0x1 << 15), (1) << 15);
  1884. mod_phy_reg(pi, 0x4a6, (0x1ff << 0), (0xff) << 0);
  1885. mod_phy_reg(pi, 0x49a, (0x1ff << 0), (0xff) << 0);
  1886. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1887. mod_radio_reg(pi, RADIO_2064_REG028, 0xf, tssi_sel);
  1888. mod_radio_reg(pi, RADIO_2064_REG086, 0x4, 0x4);
  1889. } else {
  1890. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, tssi_sel << 1);
  1891. mod_radio_reg(pi, RADIO_2064_REG03A, 0x1, 1);
  1892. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 1 << 3);
  1893. }
  1894. write_radio_reg(pi, RADIO_2064_REG025, 0xc);
  1895. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  1896. mod_radio_reg(pi, RADIO_2064_REG03A, 0x1, 1);
  1897. } else {
  1898. if (CHSPEC_IS2G(pi->radio_chanspec))
  1899. mod_radio_reg(pi, RADIO_2064_REG03A, 0x2, 1 << 1);
  1900. else
  1901. mod_radio_reg(pi, RADIO_2064_REG03A, 0x2, 0 << 1);
  1902. }
  1903. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  1904. mod_radio_reg(pi, RADIO_2064_REG03A, 0x2, 1 << 1);
  1905. else
  1906. mod_radio_reg(pi, RADIO_2064_REG03A, 0x4, 1 << 2);
  1907. mod_radio_reg(pi, RADIO_2064_REG11A, 0x1, 1 << 0);
  1908. mod_radio_reg(pi, RADIO_2064_REG005, 0x8, 1 << 3);
  1909. if (!wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  1910. mod_phy_reg(pi, 0x4d7,
  1911. (0x1 << 3) | (0x7 << 12), 0 << 3 | 2 << 12);
  1912. rfseq = wlc_lcnphy_rfseq_tbl_adc_pwrup(pi);
  1913. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  1914. tab.tbl_width = 16;
  1915. tab.tbl_ptr = &rfseq;
  1916. tab.tbl_len = 1;
  1917. tab.tbl_offset = 6;
  1918. wlc_lcnphy_write_table(pi, &tab);
  1919. mod_phy_reg(pi, 0x938, (0x1 << 2), (1) << 2);
  1920. mod_phy_reg(pi, 0x939, (0x1 << 2), (1) << 2);
  1921. mod_phy_reg(pi, 0x4a4, (0x1 << 12), (1) << 12);
  1922. mod_phy_reg(pi, 0x4d7, (0x1 << 2), (1) << 2);
  1923. mod_phy_reg(pi, 0x4d7, (0xf << 8), (0) << 8);
  1924. mod_radio_reg(pi, RADIO_2064_REG035, 0xff, 0x0);
  1925. mod_radio_reg(pi, RADIO_2064_REG036, 0x3, 0x0);
  1926. mod_radio_reg(pi, RADIO_2064_REG11A, 0x8, 0x8);
  1927. wlc_lcnphy_pwrctrl_rssiparams(pi);
  1928. }
  1929. void wlc_lcnphy_tx_pwr_update_npt(struct brcms_phy *pi)
  1930. {
  1931. u16 tx_cnt, tx_total, npt;
  1932. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1933. tx_total = wlc_lcnphy_total_tx_frames(pi);
  1934. tx_cnt = tx_total - pi_lcn->lcnphy_tssi_tx_cnt;
  1935. npt = wlc_lcnphy_get_tx_pwr_npt(pi);
  1936. if (tx_cnt > (1 << npt)) {
  1937. pi_lcn->lcnphy_tssi_tx_cnt = tx_total;
  1938. pi_lcn->lcnphy_tssi_idx = wlc_lcnphy_get_current_tx_pwr_idx(pi);
  1939. pi_lcn->lcnphy_tssi_npt = npt;
  1940. }
  1941. }
  1942. s32 wlc_lcnphy_tssi2dbm(s32 tssi, s32 a1, s32 b0, s32 b1)
  1943. {
  1944. s32 a, b, p;
  1945. a = 32768 + (a1 * tssi);
  1946. b = (1024 * b0) + (64 * b1 * tssi);
  1947. p = ((2 * b) + a) / (2 * a);
  1948. return p;
  1949. }
  1950. static void wlc_lcnphy_txpower_reset_npt(struct brcms_phy *pi)
  1951. {
  1952. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  1953. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  1954. return;
  1955. pi_lcn->lcnphy_tssi_idx = LCNPHY_TX_PWR_CTRL_START_INDEX_2G_4313;
  1956. pi_lcn->lcnphy_tssi_npt = LCNPHY_TX_PWR_CTRL_START_NPT;
  1957. }
  1958. void wlc_lcnphy_txpower_recalc_target(struct brcms_phy *pi)
  1959. {
  1960. struct phytbl_info tab;
  1961. u32 rate_table[BRCMS_NUM_RATES_CCK + BRCMS_NUM_RATES_OFDM +
  1962. BRCMS_NUM_RATES_MCS_1_STREAM];
  1963. uint i, j;
  1964. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  1965. return;
  1966. for (i = 0, j = 0; i < ARRAY_SIZE(rate_table); i++, j++) {
  1967. if (i == BRCMS_NUM_RATES_CCK + BRCMS_NUM_RATES_OFDM)
  1968. j = TXP_FIRST_MCS_20_SISO;
  1969. rate_table[i] = (u32) ((s32) (-pi->tx_power_offset[j]));
  1970. }
  1971. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1972. tab.tbl_width = 32;
  1973. tab.tbl_len = ARRAY_SIZE(rate_table);
  1974. tab.tbl_ptr = rate_table;
  1975. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  1976. wlc_lcnphy_write_table(pi, &tab);
  1977. if (wlc_lcnphy_get_target_tx_pwr(pi) != pi->tx_power_min) {
  1978. wlc_lcnphy_set_target_tx_pwr(pi, pi->tx_power_min);
  1979. wlc_lcnphy_txpower_reset_npt(pi);
  1980. }
  1981. }
  1982. static void wlc_lcnphy_set_tx_pwr_soft_ctrl(struct brcms_phy *pi, s8 index)
  1983. {
  1984. u32 cck_offset[4] = { 22, 22, 22, 22 };
  1985. u32 ofdm_offset, reg_offset_cck;
  1986. int i;
  1987. u16 index2;
  1988. struct phytbl_info tab;
  1989. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  1990. return;
  1991. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0x1) << 14);
  1992. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0x0) << 14);
  1993. or_phy_reg(pi, 0x6da, 0x0040);
  1994. reg_offset_cck = 0;
  1995. for (i = 0; i < 4; i++)
  1996. cck_offset[i] -= reg_offset_cck;
  1997. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  1998. tab.tbl_width = 32;
  1999. tab.tbl_len = 4;
  2000. tab.tbl_ptr = cck_offset;
  2001. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  2002. wlc_lcnphy_write_table(pi, &tab);
  2003. ofdm_offset = 0;
  2004. tab.tbl_len = 1;
  2005. tab.tbl_ptr = &ofdm_offset;
  2006. for (i = 836; i < 862; i++) {
  2007. tab.tbl_offset = i;
  2008. wlc_lcnphy_write_table(pi, &tab);
  2009. }
  2010. mod_phy_reg(pi, 0x4a4, (0x1 << 15), (0x1) << 15);
  2011. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0x1) << 14);
  2012. mod_phy_reg(pi, 0x4a4, (0x1 << 13), (0x1) << 13);
  2013. mod_phy_reg(pi, 0x4b0, (0x1 << 7), (0) << 7);
  2014. mod_phy_reg(pi, 0x43b, (0x1 << 6), (0) << 6);
  2015. mod_phy_reg(pi, 0x4a9, (0x1 << 15), (1) << 15);
  2016. index2 = (u16) (index * 2);
  2017. mod_phy_reg(pi, 0x4a9, (0x1ff << 0), (index2) << 0);
  2018. mod_phy_reg(pi, 0x6a3, (0x1 << 4), (0) << 4);
  2019. }
  2020. static s8 wlc_lcnphy_tempcompensated_txpwrctrl(struct brcms_phy *pi)
  2021. {
  2022. s8 index, delta_brd, delta_temp, new_index, tempcorrx;
  2023. s16 manp, meas_temp, temp_diff;
  2024. bool neg = false;
  2025. u16 temp;
  2026. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2027. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi))
  2028. return pi_lcn->lcnphy_current_index;
  2029. index = FIXED_TXPWR;
  2030. if (pi_lcn->lcnphy_tempsense_slope == 0)
  2031. return index;
  2032. temp = (u16) wlc_lcnphy_tempsense(pi, 0);
  2033. meas_temp = LCNPHY_TEMPSENSE(temp);
  2034. if (pi->tx_power_min != 0)
  2035. delta_brd = (pi_lcn->lcnphy_measPower - pi->tx_power_min);
  2036. else
  2037. delta_brd = 0;
  2038. manp = LCNPHY_TEMPSENSE(pi_lcn->lcnphy_rawtempsense);
  2039. temp_diff = manp - meas_temp;
  2040. if (temp_diff < 0) {
  2041. neg = true;
  2042. temp_diff = -temp_diff;
  2043. }
  2044. delta_temp = (s8) wlc_lcnphy_qdiv_roundup((u32) (temp_diff * 192),
  2045. (u32) (pi_lcn->
  2046. lcnphy_tempsense_slope
  2047. * 10), 0);
  2048. if (neg)
  2049. delta_temp = -delta_temp;
  2050. if (pi_lcn->lcnphy_tempsense_option == 3
  2051. && LCNREV_IS(pi->pubpi.phy_rev, 0))
  2052. delta_temp = 0;
  2053. if (pi_lcn->lcnphy_tempcorrx > 31)
  2054. tempcorrx = (s8) (pi_lcn->lcnphy_tempcorrx - 64);
  2055. else
  2056. tempcorrx = (s8) pi_lcn->lcnphy_tempcorrx;
  2057. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  2058. tempcorrx = 4;
  2059. new_index =
  2060. index + delta_brd + delta_temp - pi_lcn->lcnphy_bandedge_corr;
  2061. new_index += tempcorrx;
  2062. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  2063. index = 127;
  2064. if (new_index < 0 || new_index > 126)
  2065. return index;
  2066. return new_index;
  2067. }
  2068. static u16 wlc_lcnphy_set_tx_pwr_ctrl_mode(struct brcms_phy *pi, u16 mode)
  2069. {
  2070. u16 current_mode = mode;
  2071. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi) &&
  2072. mode == LCNPHY_TX_PWR_CTRL_HW)
  2073. current_mode = LCNPHY_TX_PWR_CTRL_TEMPBASED;
  2074. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi) &&
  2075. mode == LCNPHY_TX_PWR_CTRL_TEMPBASED)
  2076. current_mode = LCNPHY_TX_PWR_CTRL_HW;
  2077. return current_mode;
  2078. }
  2079. void wlc_lcnphy_set_tx_pwr_ctrl(struct brcms_phy *pi, u16 mode)
  2080. {
  2081. u16 old_mode = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  2082. s8 index;
  2083. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2084. mode = wlc_lcnphy_set_tx_pwr_ctrl_mode(pi, mode);
  2085. old_mode = wlc_lcnphy_set_tx_pwr_ctrl_mode(pi, old_mode);
  2086. mod_phy_reg(pi, 0x6da, (0x1 << 6),
  2087. ((LCNPHY_TX_PWR_CTRL_HW == mode) ? 1 : 0) << 6);
  2088. mod_phy_reg(pi, 0x6a3, (0x1 << 4),
  2089. ((LCNPHY_TX_PWR_CTRL_HW == mode) ? 0 : 1) << 4);
  2090. if (old_mode != mode) {
  2091. if (LCNPHY_TX_PWR_CTRL_HW == old_mode) {
  2092. wlc_lcnphy_tx_pwr_update_npt(pi);
  2093. wlc_lcnphy_clear_tx_power_offsets(pi);
  2094. }
  2095. if (LCNPHY_TX_PWR_CTRL_HW == mode) {
  2096. wlc_lcnphy_txpower_recalc_target(pi);
  2097. wlc_lcnphy_set_start_tx_pwr_idx(pi,
  2098. pi_lcn->
  2099. lcnphy_tssi_idx);
  2100. wlc_lcnphy_set_tx_pwr_npt(pi, pi_lcn->lcnphy_tssi_npt);
  2101. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, 0);
  2102. pi_lcn->lcnphy_tssi_tx_cnt =
  2103. wlc_lcnphy_total_tx_frames(pi);
  2104. wlc_lcnphy_disable_tx_gain_override(pi);
  2105. pi_lcn->lcnphy_tx_power_idx_override = -1;
  2106. } else
  2107. wlc_lcnphy_enable_tx_gain_override(pi);
  2108. mod_phy_reg(pi, 0x4a4,
  2109. ((0x1 << 15) | (0x1 << 14) | (0x1 << 13)), mode);
  2110. if (mode == LCNPHY_TX_PWR_CTRL_TEMPBASED) {
  2111. index = wlc_lcnphy_tempcompensated_txpwrctrl(pi);
  2112. wlc_lcnphy_set_tx_pwr_soft_ctrl(pi, index);
  2113. pi_lcn->lcnphy_current_index = (s8)
  2114. ((read_phy_reg(pi,
  2115. 0x4a9) &
  2116. 0xFF) / 2);
  2117. }
  2118. }
  2119. }
  2120. static void
  2121. wlc_lcnphy_tx_iqlo_loopback(struct brcms_phy *pi, u16 *values_to_save)
  2122. {
  2123. u16 vmid;
  2124. int i;
  2125. for (i = 0; i < 20; i++)
  2126. values_to_save[i] =
  2127. read_radio_reg(pi, iqlo_loopback_rf_regs[i]);
  2128. mod_phy_reg(pi, 0x44c, (0x1 << 12), 1 << 12);
  2129. mod_phy_reg(pi, 0x44d, (0x1 << 14), 1 << 14);
  2130. mod_phy_reg(pi, 0x44c, (0x1 << 11), 1 << 11);
  2131. mod_phy_reg(pi, 0x44d, (0x1 << 13), 0 << 13);
  2132. mod_phy_reg(pi, 0x43b, (0x1 << 1), 1 << 1);
  2133. mod_phy_reg(pi, 0x43c, (0x1 << 1), 0 << 1);
  2134. mod_phy_reg(pi, 0x43b, (0x1 << 0), 1 << 0);
  2135. mod_phy_reg(pi, 0x43c, (0x1 << 0), 0 << 0);
  2136. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  2137. and_radio_reg(pi, RADIO_2064_REG03A, 0xFD);
  2138. else
  2139. and_radio_reg(pi, RADIO_2064_REG03A, 0xF9);
  2140. or_radio_reg(pi, RADIO_2064_REG11A, 0x1);
  2141. or_radio_reg(pi, RADIO_2064_REG036, 0x01);
  2142. or_radio_reg(pi, RADIO_2064_REG11A, 0x18);
  2143. udelay(20);
  2144. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  2145. if (CHSPEC_IS5G(pi->radio_chanspec))
  2146. mod_radio_reg(pi, RADIO_2064_REG03A, 1, 0);
  2147. else
  2148. or_radio_reg(pi, RADIO_2064_REG03A, 1);
  2149. } else {
  2150. if (CHSPEC_IS5G(pi->radio_chanspec))
  2151. mod_radio_reg(pi, RADIO_2064_REG03A, 3, 1);
  2152. else
  2153. or_radio_reg(pi, RADIO_2064_REG03A, 0x3);
  2154. }
  2155. udelay(20);
  2156. write_radio_reg(pi, RADIO_2064_REG025, 0xF);
  2157. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  2158. if (CHSPEC_IS5G(pi->radio_chanspec))
  2159. mod_radio_reg(pi, RADIO_2064_REG028, 0xF, 0x4);
  2160. else
  2161. mod_radio_reg(pi, RADIO_2064_REG028, 0xF, 0x6);
  2162. } else {
  2163. if (CHSPEC_IS5G(pi->radio_chanspec))
  2164. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, 0x4 << 1);
  2165. else
  2166. mod_radio_reg(pi, RADIO_2064_REG028, 0x1e, 0x6 << 1);
  2167. }
  2168. udelay(20);
  2169. write_radio_reg(pi, RADIO_2064_REG005, 0x8);
  2170. or_radio_reg(pi, RADIO_2064_REG112, 0x80);
  2171. udelay(20);
  2172. or_radio_reg(pi, RADIO_2064_REG0FF, 0x10);
  2173. or_radio_reg(pi, RADIO_2064_REG11F, 0x44);
  2174. udelay(20);
  2175. or_radio_reg(pi, RADIO_2064_REG00B, 0x7);
  2176. or_radio_reg(pi, RADIO_2064_REG113, 0x10);
  2177. udelay(20);
  2178. write_radio_reg(pi, RADIO_2064_REG007, 0x1);
  2179. udelay(20);
  2180. vmid = 0x2A6;
  2181. mod_radio_reg(pi, RADIO_2064_REG0FC, 0x3 << 0, (vmid >> 8) & 0x3);
  2182. write_radio_reg(pi, RADIO_2064_REG0FD, (vmid & 0xff));
  2183. or_radio_reg(pi, RADIO_2064_REG11F, 0x44);
  2184. udelay(20);
  2185. or_radio_reg(pi, RADIO_2064_REG0FF, 0x10);
  2186. udelay(20);
  2187. write_radio_reg(pi, RADIO_2064_REG012, 0x02);
  2188. or_radio_reg(pi, RADIO_2064_REG112, 0x06);
  2189. write_radio_reg(pi, RADIO_2064_REG036, 0x11);
  2190. write_radio_reg(pi, RADIO_2064_REG059, 0xcc);
  2191. write_radio_reg(pi, RADIO_2064_REG05C, 0x2e);
  2192. write_radio_reg(pi, RADIO_2064_REG078, 0xd7);
  2193. write_radio_reg(pi, RADIO_2064_REG092, 0x15);
  2194. }
  2195. static bool wlc_lcnphy_iqcal_wait(struct brcms_phy *pi)
  2196. {
  2197. uint delay_count = 0;
  2198. while (wlc_lcnphy_iqcal_active(pi)) {
  2199. udelay(100);
  2200. delay_count++;
  2201. if (delay_count > (10 * 500))
  2202. break;
  2203. }
  2204. return (0 == wlc_lcnphy_iqcal_active(pi));
  2205. }
  2206. static void
  2207. wlc_lcnphy_tx_iqlo_loopback_cleanup(struct brcms_phy *pi, u16 *values_to_save)
  2208. {
  2209. int i;
  2210. and_phy_reg(pi, 0x44c, 0x0 >> 11);
  2211. and_phy_reg(pi, 0x43b, 0xC);
  2212. for (i = 0; i < 20; i++)
  2213. write_radio_reg(pi, iqlo_loopback_rf_regs[i],
  2214. values_to_save[i]);
  2215. }
  2216. static void
  2217. wlc_lcnphy_tx_iqlo_cal(struct brcms_phy *pi,
  2218. struct lcnphy_txgains *target_gains,
  2219. enum lcnphy_cal_mode cal_mode, bool keep_tone)
  2220. {
  2221. struct lcnphy_txgains cal_gains, temp_gains;
  2222. u16 hash;
  2223. u8 band_idx;
  2224. int j;
  2225. u16 ncorr_override[5];
  2226. u16 syst_coeffs[] = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  2227. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
  2228. u16 commands_fullcal[] = {
  2229. 0x8434, 0x8334, 0x8084, 0x8267, 0x8056, 0x8234
  2230. };
  2231. u16 commands_recal[] = {
  2232. 0x8434, 0x8334, 0x8084, 0x8267, 0x8056, 0x8234
  2233. };
  2234. u16 command_nums_fullcal[] = {
  2235. 0x7a97, 0x7a97, 0x7a97, 0x7a87, 0x7a87, 0x7b97
  2236. };
  2237. u16 command_nums_recal[] = {
  2238. 0x7a97, 0x7a97, 0x7a97, 0x7a87, 0x7a87, 0x7b97
  2239. };
  2240. u16 *command_nums = command_nums_fullcal;
  2241. u16 *start_coeffs = NULL, *cal_cmds = NULL, cal_type, diq_start;
  2242. u16 tx_pwr_ctrl_old, save_txpwrctrlrfctrl2;
  2243. u16 save_sslpnCalibClkEnCtrl, save_sslpnRxFeClkEnCtrl;
  2244. bool tx_gain_override_old;
  2245. struct lcnphy_txgains old_gains;
  2246. uint i, n_cal_cmds = 0, n_cal_start = 0;
  2247. u16 *values_to_save;
  2248. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2249. values_to_save = kmalloc(sizeof(u16) * 20, GFP_ATOMIC);
  2250. if (NULL == values_to_save)
  2251. return;
  2252. save_sslpnRxFeClkEnCtrl = read_phy_reg(pi, 0x6db);
  2253. save_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2254. or_phy_reg(pi, 0x6da, 0x40);
  2255. or_phy_reg(pi, 0x6db, 0x3);
  2256. switch (cal_mode) {
  2257. case LCNPHY_CAL_FULL:
  2258. start_coeffs = syst_coeffs;
  2259. cal_cmds = commands_fullcal;
  2260. n_cal_cmds = ARRAY_SIZE(commands_fullcal);
  2261. break;
  2262. case LCNPHY_CAL_RECAL:
  2263. start_coeffs = syst_coeffs;
  2264. cal_cmds = commands_recal;
  2265. n_cal_cmds = ARRAY_SIZE(commands_recal);
  2266. command_nums = command_nums_recal;
  2267. break;
  2268. default:
  2269. break;
  2270. }
  2271. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2272. start_coeffs, 11, 16, 64);
  2273. write_phy_reg(pi, 0x6da, 0xffff);
  2274. mod_phy_reg(pi, 0x503, (0x1 << 3), (1) << 3);
  2275. tx_pwr_ctrl_old = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  2276. mod_phy_reg(pi, 0x4a4, (0x1 << 12), (1) << 12);
  2277. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2278. save_txpwrctrlrfctrl2 = read_phy_reg(pi, 0x4db);
  2279. mod_phy_reg(pi, 0x4db, (0x3ff << 0), (0x2a6) << 0);
  2280. mod_phy_reg(pi, 0x4db, (0x7 << 12), (2) << 12);
  2281. wlc_lcnphy_tx_iqlo_loopback(pi, values_to_save);
  2282. tx_gain_override_old = wlc_lcnphy_tx_gain_override_enabled(pi);
  2283. if (tx_gain_override_old)
  2284. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  2285. if (!target_gains) {
  2286. if (!tx_gain_override_old)
  2287. wlc_lcnphy_set_tx_pwr_by_index(pi,
  2288. pi_lcn->lcnphy_tssi_idx);
  2289. wlc_lcnphy_get_tx_gain(pi, &temp_gains);
  2290. target_gains = &temp_gains;
  2291. }
  2292. hash = (target_gains->gm_gain << 8) |
  2293. (target_gains->pga_gain << 4) | (target_gains->pad_gain);
  2294. band_idx = (CHSPEC_IS5G(pi->radio_chanspec) ? 1 : 0);
  2295. cal_gains = *target_gains;
  2296. memset(ncorr_override, 0, sizeof(ncorr_override));
  2297. for (j = 0; j < iqcal_gainparams_numgains_lcnphy[band_idx]; j++) {
  2298. if (hash == tbl_iqcal_gainparams_lcnphy[band_idx][j][0]) {
  2299. cal_gains.gm_gain =
  2300. tbl_iqcal_gainparams_lcnphy[band_idx][j][1];
  2301. cal_gains.pga_gain =
  2302. tbl_iqcal_gainparams_lcnphy[band_idx][j][2];
  2303. cal_gains.pad_gain =
  2304. tbl_iqcal_gainparams_lcnphy[band_idx][j][3];
  2305. memcpy(ncorr_override,
  2306. &tbl_iqcal_gainparams_lcnphy[band_idx][j][3],
  2307. sizeof(ncorr_override));
  2308. break;
  2309. }
  2310. }
  2311. wlc_lcnphy_set_tx_gain(pi, &cal_gains);
  2312. write_phy_reg(pi, 0x453, 0xaa9);
  2313. write_phy_reg(pi, 0x93d, 0xc0);
  2314. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2315. lcnphy_iqcal_loft_gainladder,
  2316. ARRAY_SIZE(lcnphy_iqcal_loft_gainladder),
  2317. 16, 0);
  2318. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2319. lcnphy_iqcal_ir_gainladder,
  2320. ARRAY_SIZE(
  2321. lcnphy_iqcal_ir_gainladder), 16,
  2322. 32);
  2323. if (pi->phy_tx_tone_freq) {
  2324. wlc_lcnphy_stop_tx_tone(pi);
  2325. udelay(5);
  2326. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 1);
  2327. } else {
  2328. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 1);
  2329. }
  2330. write_phy_reg(pi, 0x6da, 0xffff);
  2331. for (i = n_cal_start; i < n_cal_cmds; i++) {
  2332. u16 zero_diq = 0;
  2333. u16 best_coeffs[11];
  2334. u16 command_num;
  2335. cal_type = (cal_cmds[i] & 0x0f00) >> 8;
  2336. command_num = command_nums[i];
  2337. if (ncorr_override[cal_type])
  2338. command_num =
  2339. ncorr_override[cal_type] << 8 | (command_num &
  2340. 0xff);
  2341. write_phy_reg(pi, 0x452, command_num);
  2342. if ((cal_type == 3) || (cal_type == 4)) {
  2343. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2344. &diq_start, 1, 16, 69);
  2345. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2346. &zero_diq, 1, 16, 69);
  2347. }
  2348. write_phy_reg(pi, 0x451, cal_cmds[i]);
  2349. if (!wlc_lcnphy_iqcal_wait(pi))
  2350. goto cleanup;
  2351. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2352. best_coeffs,
  2353. ARRAY_SIZE(best_coeffs), 16, 96);
  2354. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2355. best_coeffs,
  2356. ARRAY_SIZE(best_coeffs), 16, 64);
  2357. if ((cal_type == 3) || (cal_type == 4))
  2358. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2359. &diq_start, 1, 16, 69);
  2360. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2361. pi_lcn->lcnphy_cal_results.
  2362. txiqlocal_bestcoeffs,
  2363. ARRAY_SIZE(pi_lcn->
  2364. lcnphy_cal_results.
  2365. txiqlocal_bestcoeffs),
  2366. 16, 96);
  2367. }
  2368. wlc_lcnphy_common_read_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2369. pi_lcn->lcnphy_cal_results.
  2370. txiqlocal_bestcoeffs,
  2371. ARRAY_SIZE(pi_lcn->lcnphy_cal_results.
  2372. txiqlocal_bestcoeffs), 16, 96);
  2373. pi_lcn->lcnphy_cal_results.txiqlocal_bestcoeffs_valid = true;
  2374. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2375. &pi_lcn->lcnphy_cal_results.
  2376. txiqlocal_bestcoeffs[0], 4, 16, 80);
  2377. wlc_lcnphy_common_write_table(pi, LCNPHY_TBL_ID_IQLOCAL,
  2378. &pi_lcn->lcnphy_cal_results.
  2379. txiqlocal_bestcoeffs[5], 2, 16, 85);
  2380. cleanup:
  2381. wlc_lcnphy_tx_iqlo_loopback_cleanup(pi, values_to_save);
  2382. kfree(values_to_save);
  2383. if (!keep_tone)
  2384. wlc_lcnphy_stop_tx_tone(pi);
  2385. write_phy_reg(pi, 0x4db, save_txpwrctrlrfctrl2);
  2386. write_phy_reg(pi, 0x453, 0);
  2387. if (tx_gain_override_old)
  2388. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  2389. wlc_lcnphy_set_tx_pwr_ctrl(pi, tx_pwr_ctrl_old);
  2390. write_phy_reg(pi, 0x6da, save_sslpnCalibClkEnCtrl);
  2391. write_phy_reg(pi, 0x6db, save_sslpnRxFeClkEnCtrl);
  2392. }
  2393. static void wlc_lcnphy_idle_tssi_est(struct brcms_phy_pub *ppi)
  2394. {
  2395. bool suspend, tx_gain_override_old;
  2396. struct lcnphy_txgains old_gains;
  2397. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  2398. u16 idleTssi, idleTssi0_2C, idleTssi0_OB, idleTssi0_regvalue_OB,
  2399. idleTssi0_regvalue_2C;
  2400. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  2401. u16 SAVE_lpfgain = read_radio_reg(pi, RADIO_2064_REG112);
  2402. u16 SAVE_jtag_bb_afe_switch =
  2403. read_radio_reg(pi, RADIO_2064_REG007) & 1;
  2404. u16 SAVE_jtag_auxpga = read_radio_reg(pi, RADIO_2064_REG0FF) & 0x10;
  2405. u16 SAVE_iqadc_aux_en = read_radio_reg(pi, RADIO_2064_REG11F) & 4;
  2406. u8 SAVE_bbmult = wlc_lcnphy_get_bbmult(pi);
  2407. idleTssi = read_phy_reg(pi, 0x4ab);
  2408. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  2409. MCTL_EN_MAC));
  2410. if (!suspend)
  2411. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2412. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2413. tx_gain_override_old = wlc_lcnphy_tx_gain_override_enabled(pi);
  2414. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  2415. wlc_lcnphy_enable_tx_gain_override(pi);
  2416. wlc_lcnphy_set_tx_pwr_by_index(pi, 127);
  2417. write_radio_reg(pi, RADIO_2064_REG112, 0x6);
  2418. mod_radio_reg(pi, RADIO_2064_REG007, 0x1, 1);
  2419. mod_radio_reg(pi, RADIO_2064_REG0FF, 0x10, 1 << 4);
  2420. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, 1 << 2);
  2421. wlc_lcnphy_tssi_setup(pi);
  2422. mod_phy_reg(pi, 0x4d7, (0x1 << 0), (1 << 0));
  2423. mod_phy_reg(pi, 0x4d7, (0x1 << 6), (1 << 6));
  2424. wlc_lcnphy_set_bbmult(pi, 0x0);
  2425. wlc_phy_do_dummy_tx(pi, true, OFF);
  2426. idleTssi = ((read_phy_reg(pi, 0x4ab) & (0x1ff << 0))
  2427. >> 0);
  2428. idleTssi0_2C = ((read_phy_reg(pi, 0x63e) & (0x1ff << 0))
  2429. >> 0);
  2430. if (idleTssi0_2C >= 256)
  2431. idleTssi0_OB = idleTssi0_2C - 256;
  2432. else
  2433. idleTssi0_OB = idleTssi0_2C + 256;
  2434. idleTssi0_regvalue_OB = idleTssi0_OB;
  2435. if (idleTssi0_regvalue_OB >= 256)
  2436. idleTssi0_regvalue_2C = idleTssi0_regvalue_OB - 256;
  2437. else
  2438. idleTssi0_regvalue_2C = idleTssi0_regvalue_OB + 256;
  2439. mod_phy_reg(pi, 0x4a6, (0x1ff << 0), (idleTssi0_regvalue_2C) << 0);
  2440. mod_phy_reg(pi, 0x44c, (0x1 << 12), (0) << 12);
  2441. wlc_lcnphy_set_bbmult(pi, SAVE_bbmult);
  2442. wlc_lcnphy_set_tx_gain_override(pi, tx_gain_override_old);
  2443. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  2444. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  2445. write_radio_reg(pi, RADIO_2064_REG112, SAVE_lpfgain);
  2446. mod_radio_reg(pi, RADIO_2064_REG007, 0x1, SAVE_jtag_bb_afe_switch);
  2447. mod_radio_reg(pi, RADIO_2064_REG0FF, 0x10, SAVE_jtag_auxpga);
  2448. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, SAVE_iqadc_aux_en);
  2449. mod_radio_reg(pi, RADIO_2064_REG112, 0x80, 1 << 7);
  2450. if (!suspend)
  2451. wlapi_enable_mac(pi->sh->physhim);
  2452. }
  2453. static void wlc_lcnphy_vbat_temp_sense_setup(struct brcms_phy *pi, u8 mode)
  2454. {
  2455. bool suspend;
  2456. u16 save_txpwrCtrlEn;
  2457. u8 auxpga_vmidcourse, auxpga_vmidfine, auxpga_gain;
  2458. u16 auxpga_vmid;
  2459. struct phytbl_info tab;
  2460. u32 val;
  2461. u8 save_reg007, save_reg0FF, save_reg11F, save_reg005, save_reg025,
  2462. save_reg112;
  2463. u16 values_to_save[14];
  2464. s8 index;
  2465. int i;
  2466. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2467. udelay(999);
  2468. save_reg007 = (u8) read_radio_reg(pi, RADIO_2064_REG007);
  2469. save_reg0FF = (u8) read_radio_reg(pi, RADIO_2064_REG0FF);
  2470. save_reg11F = (u8) read_radio_reg(pi, RADIO_2064_REG11F);
  2471. save_reg005 = (u8) read_radio_reg(pi, RADIO_2064_REG005);
  2472. save_reg025 = (u8) read_radio_reg(pi, RADIO_2064_REG025);
  2473. save_reg112 = (u8) read_radio_reg(pi, RADIO_2064_REG112);
  2474. for (i = 0; i < 14; i++)
  2475. values_to_save[i] = read_phy_reg(pi, tempsense_phy_regs[i]);
  2476. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  2477. MCTL_EN_MAC));
  2478. if (!suspend)
  2479. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2480. save_txpwrCtrlEn = read_radio_reg(pi, 0x4a4);
  2481. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2482. index = pi_lcn->lcnphy_current_index;
  2483. wlc_lcnphy_set_tx_pwr_by_index(pi, 127);
  2484. mod_radio_reg(pi, RADIO_2064_REG007, 0x1, 0x1);
  2485. mod_radio_reg(pi, RADIO_2064_REG0FF, 0x10, 0x1 << 4);
  2486. mod_radio_reg(pi, RADIO_2064_REG11F, 0x4, 0x1 << 2);
  2487. mod_phy_reg(pi, 0x503, (0x1 << 0), (0) << 0);
  2488. mod_phy_reg(pi, 0x503, (0x1 << 2), (0) << 2);
  2489. mod_phy_reg(pi, 0x4a4, (0x1 << 14), (0) << 14);
  2490. mod_phy_reg(pi, 0x4a4, (0x1 << 15), (0) << 15);
  2491. mod_phy_reg(pi, 0x4d0, (0x1 << 5), (0) << 5);
  2492. mod_phy_reg(pi, 0x4a5, (0xff << 0), (255) << 0);
  2493. mod_phy_reg(pi, 0x4a5, (0x7 << 12), (5) << 12);
  2494. mod_phy_reg(pi, 0x4a5, (0x7 << 8), (0) << 8);
  2495. mod_phy_reg(pi, 0x40d, (0xff << 0), (64) << 0);
  2496. mod_phy_reg(pi, 0x40d, (0x7 << 8), (6) << 8);
  2497. mod_phy_reg(pi, 0x4a2, (0xff << 0), (64) << 0);
  2498. mod_phy_reg(pi, 0x4a2, (0x7 << 8), (6) << 8);
  2499. mod_phy_reg(pi, 0x4d9, (0x7 << 4), (2) << 4);
  2500. mod_phy_reg(pi, 0x4d9, (0x7 << 8), (3) << 8);
  2501. mod_phy_reg(pi, 0x4d9, (0x7 << 12), (1) << 12);
  2502. mod_phy_reg(pi, 0x4da, (0x1 << 12), (0) << 12);
  2503. mod_phy_reg(pi, 0x4da, (0x1 << 13), (1) << 13);
  2504. mod_phy_reg(pi, 0x4a6, (0x1 << 15), (1) << 15);
  2505. write_radio_reg(pi, RADIO_2064_REG025, 0xC);
  2506. mod_radio_reg(pi, RADIO_2064_REG005, 0x8, 0x1 << 3);
  2507. mod_phy_reg(pi, 0x938, (0x1 << 2), (1) << 2);
  2508. mod_phy_reg(pi, 0x939, (0x1 << 2), (1) << 2);
  2509. mod_phy_reg(pi, 0x4a4, (0x1 << 12), (1) << 12);
  2510. val = wlc_lcnphy_rfseq_tbl_adc_pwrup(pi);
  2511. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  2512. tab.tbl_width = 16;
  2513. tab.tbl_len = 1;
  2514. tab.tbl_ptr = &val;
  2515. tab.tbl_offset = 6;
  2516. wlc_lcnphy_write_table(pi, &tab);
  2517. if (mode == TEMPSENSE) {
  2518. mod_phy_reg(pi, 0x4d7, (0x1 << 3), (1) << 3);
  2519. mod_phy_reg(pi, 0x4d7, (0x7 << 12), (1) << 12);
  2520. auxpga_vmidcourse = 8;
  2521. auxpga_vmidfine = 0x4;
  2522. auxpga_gain = 2;
  2523. mod_radio_reg(pi, RADIO_2064_REG082, 0x20, 1 << 5);
  2524. } else {
  2525. mod_phy_reg(pi, 0x4d7, (0x1 << 3), (1) << 3);
  2526. mod_phy_reg(pi, 0x4d7, (0x7 << 12), (3) << 12);
  2527. auxpga_vmidcourse = 7;
  2528. auxpga_vmidfine = 0xa;
  2529. auxpga_gain = 2;
  2530. }
  2531. auxpga_vmid =
  2532. (u16) ((2 << 8) | (auxpga_vmidcourse << 4) | auxpga_vmidfine);
  2533. mod_phy_reg(pi, 0x4d8, (0x1 << 0), (1) << 0);
  2534. mod_phy_reg(pi, 0x4d8, (0x3ff << 2), (auxpga_vmid) << 2);
  2535. mod_phy_reg(pi, 0x4d8, (0x1 << 1), (1) << 1);
  2536. mod_phy_reg(pi, 0x4d8, (0x7 << 12), (auxpga_gain) << 12);
  2537. mod_phy_reg(pi, 0x4d0, (0x1 << 5), (1) << 5);
  2538. write_radio_reg(pi, RADIO_2064_REG112, 0x6);
  2539. wlc_phy_do_dummy_tx(pi, true, OFF);
  2540. if (!tempsense_done(pi))
  2541. udelay(10);
  2542. write_radio_reg(pi, RADIO_2064_REG007, (u16) save_reg007);
  2543. write_radio_reg(pi, RADIO_2064_REG0FF, (u16) save_reg0FF);
  2544. write_radio_reg(pi, RADIO_2064_REG11F, (u16) save_reg11F);
  2545. write_radio_reg(pi, RADIO_2064_REG005, (u16) save_reg005);
  2546. write_radio_reg(pi, RADIO_2064_REG025, (u16) save_reg025);
  2547. write_radio_reg(pi, RADIO_2064_REG112, (u16) save_reg112);
  2548. for (i = 0; i < 14; i++)
  2549. write_phy_reg(pi, tempsense_phy_regs[i], values_to_save[i]);
  2550. wlc_lcnphy_set_tx_pwr_by_index(pi, (int)index);
  2551. write_radio_reg(pi, 0x4a4, save_txpwrCtrlEn);
  2552. if (!suspend)
  2553. wlapi_enable_mac(pi->sh->physhim);
  2554. udelay(999);
  2555. }
  2556. static void wlc_lcnphy_tx_pwr_ctrl_init(struct brcms_phy_pub *ppi)
  2557. {
  2558. struct lcnphy_txgains tx_gains;
  2559. u8 bbmult;
  2560. struct phytbl_info tab;
  2561. s32 a1, b0, b1;
  2562. s32 tssi, pwr, maxtargetpwr, mintargetpwr;
  2563. bool suspend;
  2564. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  2565. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  2566. MCTL_EN_MAC));
  2567. if (!suspend)
  2568. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  2569. if (!pi->hwpwrctrl_capable) {
  2570. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  2571. tx_gains.gm_gain = 4;
  2572. tx_gains.pga_gain = 12;
  2573. tx_gains.pad_gain = 12;
  2574. tx_gains.dac_gain = 0;
  2575. bbmult = 150;
  2576. } else {
  2577. tx_gains.gm_gain = 7;
  2578. tx_gains.pga_gain = 15;
  2579. tx_gains.pad_gain = 14;
  2580. tx_gains.dac_gain = 0;
  2581. bbmult = 150;
  2582. }
  2583. wlc_lcnphy_set_tx_gain(pi, &tx_gains);
  2584. wlc_lcnphy_set_bbmult(pi, bbmult);
  2585. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  2586. } else {
  2587. wlc_lcnphy_idle_tssi_est(ppi);
  2588. wlc_lcnphy_clear_tx_power_offsets(pi);
  2589. b0 = pi->txpa_2g[0];
  2590. b1 = pi->txpa_2g[1];
  2591. a1 = pi->txpa_2g[2];
  2592. maxtargetpwr = wlc_lcnphy_tssi2dbm(10, a1, b0, b1);
  2593. mintargetpwr = wlc_lcnphy_tssi2dbm(125, a1, b0, b1);
  2594. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  2595. tab.tbl_width = 32;
  2596. tab.tbl_ptr = &pwr;
  2597. tab.tbl_len = 1;
  2598. tab.tbl_offset = 0;
  2599. for (tssi = 0; tssi < 128; tssi++) {
  2600. pwr = wlc_lcnphy_tssi2dbm(tssi, a1, b0, b1);
  2601. pwr = (pwr < mintargetpwr) ? mintargetpwr : pwr;
  2602. wlc_lcnphy_write_table(pi, &tab);
  2603. tab.tbl_offset++;
  2604. }
  2605. mod_phy_reg(pi, 0x4d0, (0x1 << 0), (0) << 0);
  2606. mod_phy_reg(pi, 0x4d3, (0xff << 0), (0) << 0);
  2607. mod_phy_reg(pi, 0x4d3, (0xff << 8), (0) << 8);
  2608. mod_phy_reg(pi, 0x4d0, (0x1 << 4), (0) << 4);
  2609. mod_phy_reg(pi, 0x4d0, (0x1 << 2), (0) << 2);
  2610. mod_phy_reg(pi, 0x410, (0x1 << 7), (0) << 7);
  2611. write_phy_reg(pi, 0x4a8, 10);
  2612. wlc_lcnphy_set_target_tx_pwr(pi, LCN_TARGET_PWR);
  2613. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_HW);
  2614. }
  2615. if (!suspend)
  2616. wlapi_enable_mac(pi->sh->physhim);
  2617. }
  2618. static void wlc_lcnphy_set_pa_gain(struct brcms_phy *pi, u16 gain)
  2619. {
  2620. mod_phy_reg(pi, 0x4fb,
  2621. LCNPHY_txgainctrlovrval1_pagain_ovr_val1_MASK,
  2622. gain << LCNPHY_txgainctrlovrval1_pagain_ovr_val1_SHIFT);
  2623. mod_phy_reg(pi, 0x4fd,
  2624. LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_MASK,
  2625. gain << LCNPHY_stxtxgainctrlovrval1_pagain_ovr_val1_SHIFT);
  2626. }
  2627. void
  2628. wlc_lcnphy_get_radio_loft(struct brcms_phy *pi,
  2629. u8 *ei0, u8 *eq0, u8 *fi0, u8 *fq0)
  2630. {
  2631. *ei0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG089));
  2632. *eq0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG08A));
  2633. *fi0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG08B));
  2634. *fq0 = LCNPHY_IQLOCC_READ(read_radio_reg(pi, RADIO_2064_REG08C));
  2635. }
  2636. void wlc_lcnphy_set_tx_iqcc(struct brcms_phy *pi, u16 a, u16 b)
  2637. {
  2638. struct phytbl_info tab;
  2639. u16 iqcc[2];
  2640. iqcc[0] = a;
  2641. iqcc[1] = b;
  2642. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  2643. tab.tbl_width = 16;
  2644. tab.tbl_ptr = iqcc;
  2645. tab.tbl_len = 2;
  2646. tab.tbl_offset = 80;
  2647. wlc_lcnphy_write_table(pi, &tab);
  2648. }
  2649. void wlc_lcnphy_set_tx_locc(struct brcms_phy *pi, u16 didq)
  2650. {
  2651. struct phytbl_info tab;
  2652. tab.tbl_id = LCNPHY_TBL_ID_IQLOCAL;
  2653. tab.tbl_width = 16;
  2654. tab.tbl_ptr = &didq;
  2655. tab.tbl_len = 1;
  2656. tab.tbl_offset = 85;
  2657. wlc_lcnphy_write_table(pi, &tab);
  2658. }
  2659. void wlc_lcnphy_set_tx_pwr_by_index(struct brcms_phy *pi, int index)
  2660. {
  2661. struct phytbl_info tab;
  2662. u16 a, b;
  2663. u8 bb_mult;
  2664. u32 bbmultiqcomp, txgain, locoeffs, rfpower;
  2665. struct lcnphy_txgains gains;
  2666. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2667. pi_lcn->lcnphy_tx_power_idx_override = (s8) index;
  2668. pi_lcn->lcnphy_current_index = (u8) index;
  2669. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  2670. tab.tbl_width = 32;
  2671. tab.tbl_len = 1;
  2672. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  2673. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + index;
  2674. tab.tbl_ptr = &bbmultiqcomp;
  2675. wlc_lcnphy_read_table(pi, &tab);
  2676. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + index;
  2677. tab.tbl_width = 32;
  2678. tab.tbl_ptr = &txgain;
  2679. wlc_lcnphy_read_table(pi, &tab);
  2680. gains.gm_gain = (u16) (txgain & 0xff);
  2681. gains.pga_gain = (u16) (txgain >> 8) & 0xff;
  2682. gains.pad_gain = (u16) (txgain >> 16) & 0xff;
  2683. gains.dac_gain = (u16) (bbmultiqcomp >> 28) & 0x07;
  2684. wlc_lcnphy_set_tx_gain(pi, &gains);
  2685. wlc_lcnphy_set_pa_gain(pi, (u16) (txgain >> 24) & 0x7f);
  2686. bb_mult = (u8) ((bbmultiqcomp >> 20) & 0xff);
  2687. wlc_lcnphy_set_bbmult(pi, bb_mult);
  2688. wlc_lcnphy_enable_tx_gain_override(pi);
  2689. if (!wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  2690. a = (u16) ((bbmultiqcomp >> 10) & 0x3ff);
  2691. b = (u16) (bbmultiqcomp & 0x3ff);
  2692. wlc_lcnphy_set_tx_iqcc(pi, a, b);
  2693. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_LO_OFFSET + index;
  2694. tab.tbl_ptr = &locoeffs;
  2695. wlc_lcnphy_read_table(pi, &tab);
  2696. wlc_lcnphy_set_tx_locc(pi, (u16) locoeffs);
  2697. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_PWR_OFFSET + index;
  2698. tab.tbl_ptr = &rfpower;
  2699. wlc_lcnphy_read_table(pi, &tab);
  2700. mod_phy_reg(pi, 0x6a6, (0x1fff << 0), (rfpower * 8) << 0);
  2701. }
  2702. }
  2703. static void wlc_lcnphy_clear_papd_comptable(struct brcms_phy *pi)
  2704. {
  2705. u32 j;
  2706. struct phytbl_info tab;
  2707. u32 temp_offset[128];
  2708. tab.tbl_ptr = temp_offset;
  2709. tab.tbl_len = 128;
  2710. tab.tbl_id = LCNPHY_TBL_ID_PAPDCOMPDELTATBL;
  2711. tab.tbl_width = 32;
  2712. tab.tbl_offset = 0;
  2713. memset(temp_offset, 0, sizeof(temp_offset));
  2714. for (j = 1; j < 128; j += 2)
  2715. temp_offset[j] = 0x80000;
  2716. wlc_lcnphy_write_table(pi, &tab);
  2717. return;
  2718. }
  2719. void wlc_lcnphy_tx_pu(struct brcms_phy *pi, bool bEnable)
  2720. {
  2721. if (!bEnable) {
  2722. and_phy_reg(pi, 0x43b, ~(u16) ((0x1 << 1) | (0x1 << 4)));
  2723. mod_phy_reg(pi, 0x43c, (0x1 << 1), 1 << 1);
  2724. and_phy_reg(pi, 0x44c,
  2725. ~(u16) ((0x1 << 3) |
  2726. (0x1 << 5) |
  2727. (0x1 << 12) |
  2728. (0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  2729. and_phy_reg(pi, 0x44d,
  2730. ~(u16) ((0x1 << 3) | (0x1 << 5) | (0x1 << 14)));
  2731. mod_phy_reg(pi, 0x44d, (0x1 << 2), 1 << 2);
  2732. mod_phy_reg(pi, 0x44d, (0x1 << 1) | (0x1 << 0), (0x1 << 0));
  2733. and_phy_reg(pi, 0x4f9,
  2734. ~(u16) ((0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  2735. and_phy_reg(pi, 0x4fa,
  2736. ~(u16) ((0x1 << 0) | (0x1 << 1) | (0x1 << 2)));
  2737. } else {
  2738. mod_phy_reg(pi, 0x43b, (0x1 << 1), 1 << 1);
  2739. mod_phy_reg(pi, 0x43c, (0x1 << 1), 0 << 1);
  2740. mod_phy_reg(pi, 0x43b, (0x1 << 4), 1 << 4);
  2741. mod_phy_reg(pi, 0x43c, (0x1 << 6), 0 << 6);
  2742. mod_phy_reg(pi, 0x44c, (0x1 << 12), 1 << 12);
  2743. mod_phy_reg(pi, 0x44d, (0x1 << 14), 1 << 14);
  2744. wlc_lcnphy_set_trsw_override(pi, true, false);
  2745. mod_phy_reg(pi, 0x44d, (0x1 << 2), 0 << 2);
  2746. mod_phy_reg(pi, 0x44c, (0x1 << 2), 1 << 2);
  2747. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  2748. mod_phy_reg(pi, 0x44c, (0x1 << 3), 1 << 3);
  2749. mod_phy_reg(pi, 0x44d, (0x1 << 3), 1 << 3);
  2750. mod_phy_reg(pi, 0x44c, (0x1 << 5), 1 << 5);
  2751. mod_phy_reg(pi, 0x44d, (0x1 << 5), 0 << 5);
  2752. mod_phy_reg(pi, 0x4f9, (0x1 << 1), 1 << 1);
  2753. mod_phy_reg(pi, 0x4fa, (0x1 << 1), 1 << 1);
  2754. mod_phy_reg(pi, 0x4f9, (0x1 << 2), 1 << 2);
  2755. mod_phy_reg(pi, 0x4fa, (0x1 << 2), 1 << 2);
  2756. mod_phy_reg(pi, 0x4f9, (0x1 << 0), 1 << 0);
  2757. mod_phy_reg(pi, 0x4fa, (0x1 << 0), 1 << 0);
  2758. } else {
  2759. mod_phy_reg(pi, 0x44c, (0x1 << 3), 1 << 3);
  2760. mod_phy_reg(pi, 0x44d, (0x1 << 3), 0 << 3);
  2761. mod_phy_reg(pi, 0x44c, (0x1 << 5), 1 << 5);
  2762. mod_phy_reg(pi, 0x44d, (0x1 << 5), 1 << 5);
  2763. mod_phy_reg(pi, 0x4f9, (0x1 << 1), 1 << 1);
  2764. mod_phy_reg(pi, 0x4fa, (0x1 << 1), 0 << 1);
  2765. mod_phy_reg(pi, 0x4f9, (0x1 << 2), 1 << 2);
  2766. mod_phy_reg(pi, 0x4fa, (0x1 << 2), 0 << 2);
  2767. mod_phy_reg(pi, 0x4f9, (0x1 << 0), 1 << 0);
  2768. mod_phy_reg(pi, 0x4fa, (0x1 << 0), 0 << 0);
  2769. }
  2770. }
  2771. }
  2772. static void
  2773. wlc_lcnphy_run_samples(struct brcms_phy *pi,
  2774. u16 num_samps,
  2775. u16 num_loops, u16 wait, bool iqcalmode)
  2776. {
  2777. or_phy_reg(pi, 0x6da, 0x8080);
  2778. mod_phy_reg(pi, 0x642, (0x7f << 0), (num_samps - 1) << 0);
  2779. if (num_loops != 0xffff)
  2780. num_loops--;
  2781. mod_phy_reg(pi, 0x640, (0xffff << 0), num_loops << 0);
  2782. mod_phy_reg(pi, 0x641, (0xffff << 0), wait << 0);
  2783. if (iqcalmode) {
  2784. and_phy_reg(pi, 0x453, (u16) ~(0x1 << 15));
  2785. or_phy_reg(pi, 0x453, (0x1 << 15));
  2786. } else {
  2787. write_phy_reg(pi, 0x63f, 1);
  2788. wlc_lcnphy_tx_pu(pi, 1);
  2789. }
  2790. or_radio_reg(pi, RADIO_2064_REG112, 0x6);
  2791. }
  2792. void wlc_lcnphy_deaf_mode(struct brcms_phy *pi, bool mode)
  2793. {
  2794. u8 phybw40;
  2795. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  2796. if (LCNREV_LT(pi->pubpi.phy_rev, 2)) {
  2797. mod_phy_reg(pi, 0x4b0, (0x1 << 5), (mode) << 5);
  2798. mod_phy_reg(pi, 0x4b1, (0x1 << 9), 0 << 9);
  2799. } else {
  2800. mod_phy_reg(pi, 0x4b0, (0x1 << 5), (mode) << 5);
  2801. mod_phy_reg(pi, 0x4b1, (0x1 << 9), 0 << 9);
  2802. }
  2803. if (phybw40 == 0) {
  2804. mod_phy_reg((pi), 0x410,
  2805. (0x1 << 6) |
  2806. (0x1 << 5),
  2807. ((CHSPEC_IS2G(
  2808. pi->radio_chanspec)) ? (!mode) : 0) <<
  2809. 6 | (!mode) << 5);
  2810. mod_phy_reg(pi, 0x410, (0x1 << 7), (mode) << 7);
  2811. }
  2812. }
  2813. void
  2814. wlc_lcnphy_start_tx_tone(struct brcms_phy *pi, s32 f_kHz, u16 max_val,
  2815. bool iqcalmode)
  2816. {
  2817. u8 phy_bw;
  2818. u16 num_samps, t, k;
  2819. u32 bw;
  2820. s32 theta = 0, rot = 0;
  2821. struct cordic_iq tone_samp;
  2822. u32 data_buf[64];
  2823. u16 i_samp, q_samp;
  2824. struct phytbl_info tab;
  2825. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2826. pi->phy_tx_tone_freq = f_kHz;
  2827. wlc_lcnphy_deaf_mode(pi, true);
  2828. phy_bw = 40;
  2829. if (pi_lcn->lcnphy_spurmod) {
  2830. write_phy_reg(pi, 0x942, 0x2);
  2831. write_phy_reg(pi, 0x93b, 0x0);
  2832. write_phy_reg(pi, 0x93c, 0x0);
  2833. wlc_lcnphy_txrx_spur_avoidance_mode(pi, false);
  2834. }
  2835. if (f_kHz) {
  2836. k = 1;
  2837. do {
  2838. bw = phy_bw * 1000 * k;
  2839. num_samps = bw / abs(f_kHz);
  2840. k++;
  2841. } while ((num_samps * (u32) (abs(f_kHz))) != bw);
  2842. } else
  2843. num_samps = 2;
  2844. rot = ((f_kHz * 36) / phy_bw) / 100;
  2845. theta = 0;
  2846. for (t = 0; t < num_samps; t++) {
  2847. tone_samp = cordic_calc_iq(theta);
  2848. theta += rot;
  2849. i_samp = (u16) (FLOAT(tone_samp.i * max_val) & 0x3ff);
  2850. q_samp = (u16) (FLOAT(tone_samp.q * max_val) & 0x3ff);
  2851. data_buf[t] = (i_samp << 10) | q_samp;
  2852. }
  2853. mod_phy_reg(pi, 0x6d6, (0x3 << 0), 0 << 0);
  2854. mod_phy_reg(pi, 0x6da, (0x1 << 3), 1 << 3);
  2855. tab.tbl_ptr = data_buf;
  2856. tab.tbl_len = num_samps;
  2857. tab.tbl_id = LCNPHY_TBL_ID_SAMPLEPLAY;
  2858. tab.tbl_offset = 0;
  2859. tab.tbl_width = 32;
  2860. wlc_lcnphy_write_table(pi, &tab);
  2861. wlc_lcnphy_run_samples(pi, num_samps, 0xffff, 0, iqcalmode);
  2862. }
  2863. void wlc_lcnphy_stop_tx_tone(struct brcms_phy *pi)
  2864. {
  2865. s16 playback_status;
  2866. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2867. pi->phy_tx_tone_freq = 0;
  2868. if (pi_lcn->lcnphy_spurmod) {
  2869. write_phy_reg(pi, 0x942, 0x7);
  2870. write_phy_reg(pi, 0x93b, 0x2017);
  2871. write_phy_reg(pi, 0x93c, 0x27c5);
  2872. wlc_lcnphy_txrx_spur_avoidance_mode(pi, true);
  2873. }
  2874. playback_status = read_phy_reg(pi, 0x644);
  2875. if (playback_status & (0x1 << 0)) {
  2876. wlc_lcnphy_tx_pu(pi, 0);
  2877. mod_phy_reg(pi, 0x63f, (0x1 << 1), 1 << 1);
  2878. } else if (playback_status & (0x1 << 1))
  2879. mod_phy_reg(pi, 0x453, (0x1 << 15), 0 << 15);
  2880. mod_phy_reg(pi, 0x6d6, (0x3 << 0), 1 << 0);
  2881. mod_phy_reg(pi, 0x6da, (0x1 << 3), 0 << 3);
  2882. mod_phy_reg(pi, 0x6da, (0x1 << 7), 0 << 7);
  2883. and_radio_reg(pi, RADIO_2064_REG112, 0xFFF9);
  2884. wlc_lcnphy_deaf_mode(pi, false);
  2885. }
  2886. static void
  2887. wlc_lcnphy_set_cc(struct brcms_phy *pi, int cal_type, s16 coeff_x, s16 coeff_y)
  2888. {
  2889. u16 di0dq0;
  2890. u16 x, y, data_rf;
  2891. int k;
  2892. switch (cal_type) {
  2893. case 0:
  2894. wlc_lcnphy_set_tx_iqcc(pi, coeff_x, coeff_y);
  2895. break;
  2896. case 2:
  2897. di0dq0 = (coeff_x & 0xff) << 8 | (coeff_y & 0xff);
  2898. wlc_lcnphy_set_tx_locc(pi, di0dq0);
  2899. break;
  2900. case 3:
  2901. k = wlc_lcnphy_calc_floor(coeff_x, 0);
  2902. y = 8 + k;
  2903. k = wlc_lcnphy_calc_floor(coeff_x, 1);
  2904. x = 8 - k;
  2905. data_rf = (x * 16 + y);
  2906. write_radio_reg(pi, RADIO_2064_REG089, data_rf);
  2907. k = wlc_lcnphy_calc_floor(coeff_y, 0);
  2908. y = 8 + k;
  2909. k = wlc_lcnphy_calc_floor(coeff_y, 1);
  2910. x = 8 - k;
  2911. data_rf = (x * 16 + y);
  2912. write_radio_reg(pi, RADIO_2064_REG08A, data_rf);
  2913. break;
  2914. case 4:
  2915. k = wlc_lcnphy_calc_floor(coeff_x, 0);
  2916. y = 8 + k;
  2917. k = wlc_lcnphy_calc_floor(coeff_x, 1);
  2918. x = 8 - k;
  2919. data_rf = (x * 16 + y);
  2920. write_radio_reg(pi, RADIO_2064_REG08B, data_rf);
  2921. k = wlc_lcnphy_calc_floor(coeff_y, 0);
  2922. y = 8 + k;
  2923. k = wlc_lcnphy_calc_floor(coeff_y, 1);
  2924. x = 8 - k;
  2925. data_rf = (x * 16 + y);
  2926. write_radio_reg(pi, RADIO_2064_REG08C, data_rf);
  2927. break;
  2928. }
  2929. }
  2930. static struct lcnphy_unsign16_struct
  2931. wlc_lcnphy_get_cc(struct brcms_phy *pi, int cal_type)
  2932. {
  2933. u16 a, b, didq;
  2934. u8 di0, dq0, ei, eq, fi, fq;
  2935. struct lcnphy_unsign16_struct cc;
  2936. cc.re = 0;
  2937. cc.im = 0;
  2938. switch (cal_type) {
  2939. case 0:
  2940. wlc_lcnphy_get_tx_iqcc(pi, &a, &b);
  2941. cc.re = a;
  2942. cc.im = b;
  2943. break;
  2944. case 2:
  2945. didq = wlc_lcnphy_get_tx_locc(pi);
  2946. di0 = (((didq & 0xff00) << 16) >> 24);
  2947. dq0 = (((didq & 0x00ff) << 24) >> 24);
  2948. cc.re = (u16) di0;
  2949. cc.im = (u16) dq0;
  2950. break;
  2951. case 3:
  2952. wlc_lcnphy_get_radio_loft(pi, &ei, &eq, &fi, &fq);
  2953. cc.re = (u16) ei;
  2954. cc.im = (u16) eq;
  2955. break;
  2956. case 4:
  2957. wlc_lcnphy_get_radio_loft(pi, &ei, &eq, &fi, &fq);
  2958. cc.re = (u16) fi;
  2959. cc.im = (u16) fq;
  2960. break;
  2961. }
  2962. return cc;
  2963. }
  2964. static void
  2965. wlc_lcnphy_samp_cap(struct brcms_phy *pi, int clip_detect_algo, u16 thresh,
  2966. s16 *ptr, int mode)
  2967. {
  2968. u32 curval1, curval2, stpptr, curptr, strptr, val;
  2969. u16 sslpnCalibClkEnCtrl, timer;
  2970. u16 old_sslpnCalibClkEnCtrl;
  2971. s16 imag, real;
  2972. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  2973. timer = 0;
  2974. old_sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2975. curval1 = bcma_read16(pi->d11core, D11REGOFFS(psm_corectlsts));
  2976. ptr[130] = 0;
  2977. bcma_write16(pi->d11core, D11REGOFFS(psm_corectlsts),
  2978. ((1 << 6) | curval1));
  2979. bcma_write16(pi->d11core, D11REGOFFS(smpl_clct_strptr), 0x7E00);
  2980. bcma_write16(pi->d11core, D11REGOFFS(smpl_clct_stpptr), 0x8000);
  2981. udelay(20);
  2982. curval2 = bcma_read16(pi->d11core, D11REGOFFS(psm_phy_hdr_param));
  2983. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param),
  2984. curval2 | 0x30);
  2985. write_phy_reg(pi, 0x555, 0x0);
  2986. write_phy_reg(pi, 0x5a6, 0x5);
  2987. write_phy_reg(pi, 0x5a2, (u16) (mode | mode << 6));
  2988. write_phy_reg(pi, 0x5cf, 3);
  2989. write_phy_reg(pi, 0x5a5, 0x3);
  2990. write_phy_reg(pi, 0x583, 0x0);
  2991. write_phy_reg(pi, 0x584, 0x0);
  2992. write_phy_reg(pi, 0x585, 0x0fff);
  2993. write_phy_reg(pi, 0x586, 0x0000);
  2994. write_phy_reg(pi, 0x580, 0x4501);
  2995. sslpnCalibClkEnCtrl = read_phy_reg(pi, 0x6da);
  2996. write_phy_reg(pi, 0x6da, (u32) (sslpnCalibClkEnCtrl | 0x2008));
  2997. stpptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_stpptr));
  2998. curptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_curptr));
  2999. do {
  3000. udelay(10);
  3001. curptr = bcma_read16(pi->d11core, D11REGOFFS(smpl_clct_curptr));
  3002. timer++;
  3003. } while ((curptr != stpptr) && (timer < 500));
  3004. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param), 0x2);
  3005. strptr = 0x7E00;
  3006. bcma_write32(pi->d11core, D11REGOFFS(tplatewrptr), strptr);
  3007. while (strptr < 0x8000) {
  3008. val = bcma_read32(pi->d11core, D11REGOFFS(tplatewrdata));
  3009. imag = ((val >> 16) & 0x3ff);
  3010. real = ((val) & 0x3ff);
  3011. if (imag > 511)
  3012. imag -= 1024;
  3013. if (real > 511)
  3014. real -= 1024;
  3015. if (pi_lcn->lcnphy_iqcal_swp_dis)
  3016. ptr[(strptr - 0x7E00) / 4] = real;
  3017. else
  3018. ptr[(strptr - 0x7E00) / 4] = imag;
  3019. if (clip_detect_algo) {
  3020. if (imag > thresh || imag < -thresh) {
  3021. strptr = 0x8000;
  3022. ptr[130] = 1;
  3023. }
  3024. }
  3025. strptr += 4;
  3026. }
  3027. write_phy_reg(pi, 0x6da, old_sslpnCalibClkEnCtrl);
  3028. bcma_write16(pi->d11core, D11REGOFFS(psm_phy_hdr_param), curval2);
  3029. bcma_write16(pi->d11core, D11REGOFFS(psm_corectlsts), curval1);
  3030. }
  3031. static void
  3032. wlc_lcnphy_a1(struct brcms_phy *pi, int cal_type, int num_levels,
  3033. int step_size_lg2)
  3034. {
  3035. const struct lcnphy_spb_tone *phy_c1;
  3036. struct lcnphy_spb_tone phy_c2;
  3037. struct lcnphy_unsign16_struct phy_c3;
  3038. int phy_c4, phy_c5, k, l, j, phy_c6;
  3039. u16 phy_c7, phy_c8, phy_c9;
  3040. s16 phy_c10, phy_c11, phy_c12, phy_c13, phy_c14, phy_c15, phy_c16;
  3041. s16 *ptr, phy_c17;
  3042. s32 phy_c18, phy_c19;
  3043. u32 phy_c20, phy_c21;
  3044. bool phy_c22, phy_c23, phy_c24, phy_c25;
  3045. u16 phy_c26, phy_c27;
  3046. u16 phy_c28, phy_c29, phy_c30;
  3047. u16 phy_c31;
  3048. u16 *phy_c32;
  3049. phy_c21 = 0;
  3050. phy_c10 = phy_c13 = phy_c14 = phy_c8 = 0;
  3051. ptr = kmalloc(sizeof(s16) * 131, GFP_ATOMIC);
  3052. if (NULL == ptr)
  3053. return;
  3054. phy_c32 = kmalloc(sizeof(u16) * 20, GFP_ATOMIC);
  3055. if (NULL == phy_c32) {
  3056. kfree(ptr);
  3057. return;
  3058. }
  3059. phy_c26 = read_phy_reg(pi, 0x6da);
  3060. phy_c27 = read_phy_reg(pi, 0x6db);
  3061. phy_c31 = read_radio_reg(pi, RADIO_2064_REG026);
  3062. write_phy_reg(pi, 0x93d, 0xC0);
  3063. wlc_lcnphy_start_tx_tone(pi, 3750, 88, 0);
  3064. write_phy_reg(pi, 0x6da, 0xffff);
  3065. or_phy_reg(pi, 0x6db, 0x3);
  3066. wlc_lcnphy_tx_iqlo_loopback(pi, phy_c32);
  3067. udelay(500);
  3068. phy_c28 = read_phy_reg(pi, 0x938);
  3069. phy_c29 = read_phy_reg(pi, 0x4d7);
  3070. phy_c30 = read_phy_reg(pi, 0x4d8);
  3071. or_phy_reg(pi, 0x938, 0x1 << 2);
  3072. or_phy_reg(pi, 0x4d7, 0x1 << 2);
  3073. or_phy_reg(pi, 0x4d7, 0x1 << 3);
  3074. mod_phy_reg(pi, 0x4d7, (0x7 << 12), 0x2 << 12);
  3075. or_phy_reg(pi, 0x4d8, 1 << 0);
  3076. or_phy_reg(pi, 0x4d8, 1 << 1);
  3077. mod_phy_reg(pi, 0x4d8, (0x3ff << 2), 0x23A << 2);
  3078. mod_phy_reg(pi, 0x4d8, (0x7 << 12), 0x7 << 12);
  3079. phy_c1 = &lcnphy_spb_tone_3750[0];
  3080. phy_c4 = 32;
  3081. if (num_levels == 0) {
  3082. if (cal_type != 0)
  3083. num_levels = 4;
  3084. else
  3085. num_levels = 9;
  3086. }
  3087. if (step_size_lg2 == 0) {
  3088. if (cal_type != 0)
  3089. step_size_lg2 = 3;
  3090. else
  3091. step_size_lg2 = 8;
  3092. }
  3093. phy_c7 = (1 << step_size_lg2);
  3094. phy_c3 = wlc_lcnphy_get_cc(pi, cal_type);
  3095. phy_c15 = (s16) phy_c3.re;
  3096. phy_c16 = (s16) phy_c3.im;
  3097. if (cal_type == 2) {
  3098. if (phy_c3.re > 127)
  3099. phy_c15 = phy_c3.re - 256;
  3100. if (phy_c3.im > 127)
  3101. phy_c16 = phy_c3.im - 256;
  3102. }
  3103. wlc_lcnphy_set_cc(pi, cal_type, phy_c15, phy_c16);
  3104. udelay(20);
  3105. for (phy_c8 = 0; phy_c7 != 0 && phy_c8 < num_levels; phy_c8++) {
  3106. phy_c23 = true;
  3107. phy_c22 = false;
  3108. switch (cal_type) {
  3109. case 0:
  3110. phy_c10 = 511;
  3111. break;
  3112. case 2:
  3113. phy_c10 = 127;
  3114. break;
  3115. case 3:
  3116. phy_c10 = 15;
  3117. break;
  3118. case 4:
  3119. phy_c10 = 15;
  3120. break;
  3121. }
  3122. phy_c9 = read_phy_reg(pi, 0x93d);
  3123. phy_c9 = 2 * phy_c9;
  3124. phy_c24 = false;
  3125. phy_c5 = 7;
  3126. phy_c25 = true;
  3127. while (1) {
  3128. write_radio_reg(pi, RADIO_2064_REG026,
  3129. (phy_c5 & 0x7) | ((phy_c5 & 0x7) << 4));
  3130. udelay(50);
  3131. phy_c22 = false;
  3132. ptr[130] = 0;
  3133. wlc_lcnphy_samp_cap(pi, 1, phy_c9, &ptr[0], 2);
  3134. if (ptr[130] == 1)
  3135. phy_c22 = true;
  3136. if (phy_c22)
  3137. phy_c5 -= 1;
  3138. if ((phy_c22 != phy_c24) && (!phy_c25))
  3139. break;
  3140. if (!phy_c22)
  3141. phy_c5 += 1;
  3142. if (phy_c5 <= 0 || phy_c5 >= 7)
  3143. break;
  3144. phy_c24 = phy_c22;
  3145. phy_c25 = false;
  3146. }
  3147. if (phy_c5 < 0)
  3148. phy_c5 = 0;
  3149. else if (phy_c5 > 7)
  3150. phy_c5 = 7;
  3151. for (k = -phy_c7; k <= phy_c7; k += phy_c7) {
  3152. for (l = -phy_c7; l <= phy_c7; l += phy_c7) {
  3153. phy_c11 = phy_c15 + k;
  3154. phy_c12 = phy_c16 + l;
  3155. if (phy_c11 < -phy_c10)
  3156. phy_c11 = -phy_c10;
  3157. else if (phy_c11 > phy_c10)
  3158. phy_c11 = phy_c10;
  3159. if (phy_c12 < -phy_c10)
  3160. phy_c12 = -phy_c10;
  3161. else if (phy_c12 > phy_c10)
  3162. phy_c12 = phy_c10;
  3163. wlc_lcnphy_set_cc(pi, cal_type, phy_c11,
  3164. phy_c12);
  3165. udelay(20);
  3166. wlc_lcnphy_samp_cap(pi, 0, 0, ptr, 2);
  3167. phy_c18 = 0;
  3168. phy_c19 = 0;
  3169. for (j = 0; j < 128; j++) {
  3170. if (cal_type != 0)
  3171. phy_c6 = j % phy_c4;
  3172. else
  3173. phy_c6 = (2 * j) % phy_c4;
  3174. phy_c2.re = phy_c1[phy_c6].re;
  3175. phy_c2.im = phy_c1[phy_c6].im;
  3176. phy_c17 = ptr[j];
  3177. phy_c18 = phy_c18 + phy_c17 * phy_c2.re;
  3178. phy_c19 = phy_c19 + phy_c17 * phy_c2.im;
  3179. }
  3180. phy_c18 = phy_c18 >> 10;
  3181. phy_c19 = phy_c19 >> 10;
  3182. phy_c20 = ((phy_c18 * phy_c18) +
  3183. (phy_c19 * phy_c19));
  3184. if (phy_c23 || phy_c20 < phy_c21) {
  3185. phy_c21 = phy_c20;
  3186. phy_c13 = phy_c11;
  3187. phy_c14 = phy_c12;
  3188. }
  3189. phy_c23 = false;
  3190. }
  3191. }
  3192. phy_c23 = true;
  3193. phy_c15 = phy_c13;
  3194. phy_c16 = phy_c14;
  3195. phy_c7 = phy_c7 >> 1;
  3196. wlc_lcnphy_set_cc(pi, cal_type, phy_c15, phy_c16);
  3197. udelay(20);
  3198. }
  3199. goto cleanup;
  3200. cleanup:
  3201. wlc_lcnphy_tx_iqlo_loopback_cleanup(pi, phy_c32);
  3202. wlc_lcnphy_stop_tx_tone(pi);
  3203. write_phy_reg(pi, 0x6da, phy_c26);
  3204. write_phy_reg(pi, 0x6db, phy_c27);
  3205. write_phy_reg(pi, 0x938, phy_c28);
  3206. write_phy_reg(pi, 0x4d7, phy_c29);
  3207. write_phy_reg(pi, 0x4d8, phy_c30);
  3208. write_radio_reg(pi, RADIO_2064_REG026, phy_c31);
  3209. kfree(phy_c32);
  3210. kfree(ptr);
  3211. }
  3212. void wlc_lcnphy_get_tx_iqcc(struct brcms_phy *pi, u16 *a, u16 *b)
  3213. {
  3214. u16 iqcc[2];
  3215. struct phytbl_info tab;
  3216. tab.tbl_ptr = iqcc;
  3217. tab.tbl_len = 2;
  3218. tab.tbl_id = 0;
  3219. tab.tbl_offset = 80;
  3220. tab.tbl_width = 16;
  3221. wlc_lcnphy_read_table(pi, &tab);
  3222. *a = iqcc[0];
  3223. *b = iqcc[1];
  3224. }
  3225. static void wlc_lcnphy_tx_iqlo_soft_cal_full(struct brcms_phy *pi)
  3226. {
  3227. struct lcnphy_unsign16_struct iqcc0, locc2, locc3, locc4;
  3228. wlc_lcnphy_set_cc(pi, 0, 0, 0);
  3229. wlc_lcnphy_set_cc(pi, 2, 0, 0);
  3230. wlc_lcnphy_set_cc(pi, 3, 0, 0);
  3231. wlc_lcnphy_set_cc(pi, 4, 0, 0);
  3232. wlc_lcnphy_a1(pi, 4, 0, 0);
  3233. wlc_lcnphy_a1(pi, 3, 0, 0);
  3234. wlc_lcnphy_a1(pi, 2, 3, 2);
  3235. wlc_lcnphy_a1(pi, 0, 5, 8);
  3236. wlc_lcnphy_a1(pi, 2, 2, 1);
  3237. wlc_lcnphy_a1(pi, 0, 4, 3);
  3238. iqcc0 = wlc_lcnphy_get_cc(pi, 0);
  3239. locc2 = wlc_lcnphy_get_cc(pi, 2);
  3240. locc3 = wlc_lcnphy_get_cc(pi, 3);
  3241. locc4 = wlc_lcnphy_get_cc(pi, 4);
  3242. }
  3243. u16 wlc_lcnphy_get_tx_locc(struct brcms_phy *pi)
  3244. {
  3245. struct phytbl_info tab;
  3246. u16 didq;
  3247. tab.tbl_id = 0;
  3248. tab.tbl_width = 16;
  3249. tab.tbl_ptr = &didq;
  3250. tab.tbl_len = 1;
  3251. tab.tbl_offset = 85;
  3252. wlc_lcnphy_read_table(pi, &tab);
  3253. return didq;
  3254. }
  3255. static void wlc_lcnphy_txpwrtbl_iqlo_cal(struct brcms_phy *pi)
  3256. {
  3257. struct lcnphy_txgains target_gains, old_gains;
  3258. u8 save_bb_mult;
  3259. u16 a, b, didq, save_pa_gain = 0;
  3260. uint idx, SAVE_txpwrindex = 0xFF;
  3261. u32 val;
  3262. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3263. struct phytbl_info tab;
  3264. u8 ei0, eq0, fi0, fq0;
  3265. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3266. wlc_lcnphy_get_tx_gain(pi, &old_gains);
  3267. save_pa_gain = wlc_lcnphy_get_pa_gain(pi);
  3268. save_bb_mult = wlc_lcnphy_get_bbmult(pi);
  3269. if (SAVE_txpwrctrl == LCNPHY_TX_PWR_CTRL_OFF)
  3270. SAVE_txpwrindex = wlc_lcnphy_get_current_tx_pwr_idx(pi);
  3271. wlc_lcnphy_set_tx_pwr_ctrl(pi, LCNPHY_TX_PWR_CTRL_OFF);
  3272. target_gains.gm_gain = 7;
  3273. target_gains.pga_gain = 0;
  3274. target_gains.pad_gain = 21;
  3275. target_gains.dac_gain = 0;
  3276. wlc_lcnphy_set_tx_gain(pi, &target_gains);
  3277. if (LCNREV_IS(pi->pubpi.phy_rev, 1) || pi_lcn->lcnphy_hw_iqcal_en) {
  3278. wlc_lcnphy_set_tx_pwr_by_index(pi, 30);
  3279. wlc_lcnphy_tx_iqlo_cal(pi, &target_gains,
  3280. (pi_lcn->
  3281. lcnphy_recal ? LCNPHY_CAL_RECAL :
  3282. LCNPHY_CAL_FULL), false);
  3283. } else {
  3284. wlc_lcnphy_set_tx_pwr_by_index(pi, 16);
  3285. wlc_lcnphy_tx_iqlo_soft_cal_full(pi);
  3286. }
  3287. wlc_lcnphy_get_radio_loft(pi, &ei0, &eq0, &fi0, &fq0);
  3288. if ((abs((s8) fi0) == 15) && (abs((s8) fq0) == 15)) {
  3289. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  3290. target_gains.gm_gain = 255;
  3291. target_gains.pga_gain = 255;
  3292. target_gains.pad_gain = 0xf0;
  3293. target_gains.dac_gain = 0;
  3294. } else {
  3295. target_gains.gm_gain = 7;
  3296. target_gains.pga_gain = 45;
  3297. target_gains.pad_gain = 186;
  3298. target_gains.dac_gain = 0;
  3299. }
  3300. if (LCNREV_IS(pi->pubpi.phy_rev, 1)
  3301. || pi_lcn->lcnphy_hw_iqcal_en) {
  3302. target_gains.pga_gain = 0;
  3303. target_gains.pad_gain = 30;
  3304. wlc_lcnphy_set_tx_pwr_by_index(pi, 16);
  3305. wlc_lcnphy_tx_iqlo_cal(pi, &target_gains,
  3306. LCNPHY_CAL_FULL, false);
  3307. } else {
  3308. wlc_lcnphy_tx_iqlo_soft_cal_full(pi);
  3309. }
  3310. }
  3311. wlc_lcnphy_get_tx_iqcc(pi, &a, &b);
  3312. didq = wlc_lcnphy_get_tx_locc(pi);
  3313. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3314. tab.tbl_width = 32;
  3315. tab.tbl_ptr = &val;
  3316. tab.tbl_len = 1;
  3317. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_RATE_OFFSET;
  3318. for (idx = 0; idx < 128; idx++) {
  3319. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + idx;
  3320. wlc_lcnphy_read_table(pi, &tab);
  3321. val = (val & 0xfff00000) |
  3322. ((u32) (a & 0x3FF) << 10) | (b & 0x3ff);
  3323. wlc_lcnphy_write_table(pi, &tab);
  3324. val = didq;
  3325. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_LO_OFFSET + idx;
  3326. wlc_lcnphy_write_table(pi, &tab);
  3327. }
  3328. pi_lcn->lcnphy_cal_results.txiqlocal_a = a;
  3329. pi_lcn->lcnphy_cal_results.txiqlocal_b = b;
  3330. pi_lcn->lcnphy_cal_results.txiqlocal_didq = didq;
  3331. pi_lcn->lcnphy_cal_results.txiqlocal_ei0 = ei0;
  3332. pi_lcn->lcnphy_cal_results.txiqlocal_eq0 = eq0;
  3333. pi_lcn->lcnphy_cal_results.txiqlocal_fi0 = fi0;
  3334. pi_lcn->lcnphy_cal_results.txiqlocal_fq0 = fq0;
  3335. wlc_lcnphy_set_bbmult(pi, save_bb_mult);
  3336. wlc_lcnphy_set_pa_gain(pi, save_pa_gain);
  3337. wlc_lcnphy_set_tx_gain(pi, &old_gains);
  3338. if (SAVE_txpwrctrl != LCNPHY_TX_PWR_CTRL_OFF)
  3339. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  3340. else
  3341. wlc_lcnphy_set_tx_pwr_by_index(pi, SAVE_txpwrindex);
  3342. }
  3343. s16 wlc_lcnphy_tempsense_new(struct brcms_phy *pi, bool mode)
  3344. {
  3345. u16 tempsenseval1, tempsenseval2;
  3346. s16 avg = 0;
  3347. bool suspend = false;
  3348. if (mode == 1) {
  3349. suspend = (0 == (bcma_read32(pi->d11core,
  3350. D11REGOFFS(maccontrol)) &
  3351. MCTL_EN_MAC));
  3352. if (!suspend)
  3353. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3354. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  3355. }
  3356. tempsenseval1 = read_phy_reg(pi, 0x476) & 0x1FF;
  3357. tempsenseval2 = read_phy_reg(pi, 0x477) & 0x1FF;
  3358. if (tempsenseval1 > 255)
  3359. avg = (s16) (tempsenseval1 - 512);
  3360. else
  3361. avg = (s16) tempsenseval1;
  3362. if (tempsenseval2 > 255)
  3363. avg += (s16) (tempsenseval2 - 512);
  3364. else
  3365. avg += (s16) tempsenseval2;
  3366. avg /= 2;
  3367. if (mode == 1) {
  3368. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  3369. udelay(100);
  3370. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  3371. if (!suspend)
  3372. wlapi_enable_mac(pi->sh->physhim);
  3373. }
  3374. return avg;
  3375. }
  3376. u16 wlc_lcnphy_tempsense(struct brcms_phy *pi, bool mode)
  3377. {
  3378. u16 tempsenseval1, tempsenseval2;
  3379. s32 avg = 0;
  3380. bool suspend = false;
  3381. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3382. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3383. if (mode == 1) {
  3384. suspend = (0 == (bcma_read32(pi->d11core,
  3385. D11REGOFFS(maccontrol)) &
  3386. MCTL_EN_MAC));
  3387. if (!suspend)
  3388. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3389. wlc_lcnphy_vbat_temp_sense_setup(pi, TEMPSENSE);
  3390. }
  3391. tempsenseval1 = read_phy_reg(pi, 0x476) & 0x1FF;
  3392. tempsenseval2 = read_phy_reg(pi, 0x477) & 0x1FF;
  3393. if (tempsenseval1 > 255)
  3394. avg = (int)(tempsenseval1 - 512);
  3395. else
  3396. avg = (int)tempsenseval1;
  3397. if (pi_lcn->lcnphy_tempsense_option == 1 || pi->hwpwrctrl_capable) {
  3398. if (tempsenseval2 > 255)
  3399. avg = (int)(avg - tempsenseval2 + 512);
  3400. else
  3401. avg = (int)(avg - tempsenseval2);
  3402. } else {
  3403. if (tempsenseval2 > 255)
  3404. avg = (int)(avg + tempsenseval2 - 512);
  3405. else
  3406. avg = (int)(avg + tempsenseval2);
  3407. avg = avg / 2;
  3408. }
  3409. if (avg < 0)
  3410. avg = avg + 512;
  3411. if (pi_lcn->lcnphy_tempsense_option == 2)
  3412. avg = tempsenseval1;
  3413. if (mode)
  3414. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_txpwrctrl);
  3415. if (mode == 1) {
  3416. mod_phy_reg(pi, 0x448, (0x1 << 14), (1) << 14);
  3417. udelay(100);
  3418. mod_phy_reg(pi, 0x448, (0x1 << 14), (0) << 14);
  3419. if (!suspend)
  3420. wlapi_enable_mac(pi->sh->physhim);
  3421. }
  3422. return (u16) avg;
  3423. }
  3424. s8 wlc_lcnphy_tempsense_degree(struct brcms_phy *pi, bool mode)
  3425. {
  3426. s32 degree = wlc_lcnphy_tempsense_new(pi, mode);
  3427. degree =
  3428. ((degree <<
  3429. 10) + LCN_TEMPSENSE_OFFSET + (LCN_TEMPSENSE_DEN >> 1))
  3430. / LCN_TEMPSENSE_DEN;
  3431. return (s8) degree;
  3432. }
  3433. s8 wlc_lcnphy_vbatsense(struct brcms_phy *pi, bool mode)
  3434. {
  3435. u16 vbatsenseval;
  3436. s32 avg = 0;
  3437. bool suspend = false;
  3438. if (mode == 1) {
  3439. suspend = (0 == (bcma_read32(pi->d11core,
  3440. D11REGOFFS(maccontrol)) &
  3441. MCTL_EN_MAC));
  3442. if (!suspend)
  3443. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3444. wlc_lcnphy_vbat_temp_sense_setup(pi, VBATSENSE);
  3445. }
  3446. vbatsenseval = read_phy_reg(pi, 0x475) & 0x1FF;
  3447. if (vbatsenseval > 255)
  3448. avg = (s32) (vbatsenseval - 512);
  3449. else
  3450. avg = (s32) vbatsenseval;
  3451. avg = (avg * LCN_VBAT_SCALE_NOM +
  3452. (LCN_VBAT_SCALE_DEN >> 1)) / LCN_VBAT_SCALE_DEN;
  3453. if (mode == 1) {
  3454. if (!suspend)
  3455. wlapi_enable_mac(pi->sh->physhim);
  3456. }
  3457. return (s8) avg;
  3458. }
  3459. static void wlc_lcnphy_afe_clk_init(struct brcms_phy *pi, u8 mode)
  3460. {
  3461. u8 phybw40;
  3462. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3463. mod_phy_reg(pi, 0x6d1, (0x1 << 7), (1) << 7);
  3464. if (((mode == AFE_CLK_INIT_MODE_PAPD) && (phybw40 == 0)) ||
  3465. (mode == AFE_CLK_INIT_MODE_TXRX2X))
  3466. write_phy_reg(pi, 0x6d0, 0x7);
  3467. wlc_lcnphy_toggle_afe_pwdn(pi);
  3468. }
  3469. static void wlc_lcnphy_temp_adj(struct brcms_phy *pi)
  3470. {
  3471. }
  3472. static void wlc_lcnphy_glacial_timer_based_cal(struct brcms_phy *pi)
  3473. {
  3474. bool suspend;
  3475. s8 index;
  3476. u16 SAVE_pwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3477. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3478. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  3479. MCTL_EN_MAC));
  3480. if (!suspend)
  3481. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3482. wlc_lcnphy_deaf_mode(pi, true);
  3483. pi->phy_lastcal = pi->sh->now;
  3484. pi->phy_forcecal = false;
  3485. index = pi_lcn->lcnphy_current_index;
  3486. wlc_lcnphy_txpwrtbl_iqlo_cal(pi);
  3487. wlc_lcnphy_set_tx_pwr_by_index(pi, index);
  3488. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_pwrctrl);
  3489. wlc_lcnphy_deaf_mode(pi, false);
  3490. if (!suspend)
  3491. wlapi_enable_mac(pi->sh->physhim);
  3492. }
  3493. static void wlc_lcnphy_periodic_cal(struct brcms_phy *pi)
  3494. {
  3495. bool suspend, full_cal;
  3496. const struct lcnphy_rx_iqcomp *rx_iqcomp;
  3497. int rx_iqcomp_sz;
  3498. u16 SAVE_pwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3499. s8 index;
  3500. struct phytbl_info tab;
  3501. s32 a1, b0, b1;
  3502. s32 tssi, pwr, maxtargetpwr, mintargetpwr;
  3503. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3504. pi->phy_lastcal = pi->sh->now;
  3505. pi->phy_forcecal = false;
  3506. full_cal =
  3507. (pi_lcn->lcnphy_full_cal_channel !=
  3508. CHSPEC_CHANNEL(pi->radio_chanspec));
  3509. pi_lcn->lcnphy_full_cal_channel = CHSPEC_CHANNEL(pi->radio_chanspec);
  3510. index = pi_lcn->lcnphy_current_index;
  3511. suspend = (0 == (bcma_read32(pi->d11core, D11REGOFFS(maccontrol)) &
  3512. MCTL_EN_MAC));
  3513. if (!suspend) {
  3514. wlapi_bmac_write_shm(pi->sh->physhim, M_CTS_DURATION, 10000);
  3515. wlapi_suspend_mac_and_wait(pi->sh->physhim);
  3516. }
  3517. wlc_lcnphy_deaf_mode(pi, true);
  3518. wlc_lcnphy_txpwrtbl_iqlo_cal(pi);
  3519. rx_iqcomp = lcnphy_rx_iqcomp_table_rev0;
  3520. rx_iqcomp_sz = ARRAY_SIZE(lcnphy_rx_iqcomp_table_rev0);
  3521. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  3522. wlc_lcnphy_rx_iq_cal(pi, NULL, 0, true, false, 1, 40);
  3523. else
  3524. wlc_lcnphy_rx_iq_cal(pi, NULL, 0, true, false, 1, 127);
  3525. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi)) {
  3526. wlc_lcnphy_idle_tssi_est((struct brcms_phy_pub *) pi);
  3527. b0 = pi->txpa_2g[0];
  3528. b1 = pi->txpa_2g[1];
  3529. a1 = pi->txpa_2g[2];
  3530. maxtargetpwr = wlc_lcnphy_tssi2dbm(10, a1, b0, b1);
  3531. mintargetpwr = wlc_lcnphy_tssi2dbm(125, a1, b0, b1);
  3532. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3533. tab.tbl_width = 32;
  3534. tab.tbl_ptr = &pwr;
  3535. tab.tbl_len = 1;
  3536. tab.tbl_offset = 0;
  3537. for (tssi = 0; tssi < 128; tssi++) {
  3538. pwr = wlc_lcnphy_tssi2dbm(tssi, a1, b0, b1);
  3539. pwr = (pwr < mintargetpwr) ? mintargetpwr : pwr;
  3540. wlc_lcnphy_write_table(pi, &tab);
  3541. tab.tbl_offset++;
  3542. }
  3543. }
  3544. wlc_lcnphy_set_tx_pwr_by_index(pi, index);
  3545. wlc_lcnphy_set_tx_pwr_ctrl(pi, SAVE_pwrctrl);
  3546. wlc_lcnphy_deaf_mode(pi, false);
  3547. if (!suspend)
  3548. wlapi_enable_mac(pi->sh->physhim);
  3549. }
  3550. void wlc_lcnphy_calib_modes(struct brcms_phy *pi, uint mode)
  3551. {
  3552. u16 temp_new;
  3553. int temp1, temp2, temp_diff;
  3554. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3555. switch (mode) {
  3556. case PHY_PERICAL_CHAN:
  3557. break;
  3558. case PHY_FULLCAL:
  3559. wlc_lcnphy_periodic_cal(pi);
  3560. break;
  3561. case PHY_PERICAL_PHYINIT:
  3562. wlc_lcnphy_periodic_cal(pi);
  3563. break;
  3564. case PHY_PERICAL_WATCHDOG:
  3565. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi)) {
  3566. temp_new = wlc_lcnphy_tempsense(pi, 0);
  3567. temp1 = LCNPHY_TEMPSENSE(temp_new);
  3568. temp2 = LCNPHY_TEMPSENSE(pi_lcn->lcnphy_cal_temper);
  3569. temp_diff = temp1 - temp2;
  3570. if ((pi_lcn->lcnphy_cal_counter > 90) ||
  3571. (temp_diff > 60) || (temp_diff < -60)) {
  3572. wlc_lcnphy_glacial_timer_based_cal(pi);
  3573. wlc_2064_vco_cal(pi);
  3574. pi_lcn->lcnphy_cal_temper = temp_new;
  3575. pi_lcn->lcnphy_cal_counter = 0;
  3576. } else
  3577. pi_lcn->lcnphy_cal_counter++;
  3578. }
  3579. break;
  3580. case LCNPHY_PERICAL_TEMPBASED_TXPWRCTRL:
  3581. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi))
  3582. wlc_lcnphy_tx_power_adjustment(
  3583. (struct brcms_phy_pub *) pi);
  3584. break;
  3585. }
  3586. }
  3587. void wlc_lcnphy_get_tssi(struct brcms_phy *pi, s8 *ofdm_pwr, s8 *cck_pwr)
  3588. {
  3589. s8 cck_offset;
  3590. u16 status;
  3591. status = (read_phy_reg(pi, 0x4ab));
  3592. if (wlc_lcnphy_tssi_based_pwr_ctrl_enabled(pi) &&
  3593. (status & (0x1 << 15))) {
  3594. *ofdm_pwr = (s8) (((read_phy_reg(pi, 0x4ab) & (0x1ff << 0))
  3595. >> 0) >> 1);
  3596. if (wlc_phy_tpc_isenabled_lcnphy(pi))
  3597. cck_offset = pi->tx_power_offset[TXP_FIRST_CCK];
  3598. else
  3599. cck_offset = 0;
  3600. *cck_pwr = *ofdm_pwr + cck_offset;
  3601. } else {
  3602. *cck_pwr = 0;
  3603. *ofdm_pwr = 0;
  3604. }
  3605. }
  3606. void wlc_phy_cal_init_lcnphy(struct brcms_phy *pi)
  3607. {
  3608. return;
  3609. }
  3610. void wlc_lcnphy_tx_power_adjustment(struct brcms_phy_pub *ppi)
  3611. {
  3612. s8 index;
  3613. u16 index2;
  3614. struct brcms_phy *pi = (struct brcms_phy *) ppi;
  3615. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3616. u16 SAVE_txpwrctrl = wlc_lcnphy_get_tx_pwr_ctrl(pi);
  3617. if (wlc_lcnphy_tempsense_based_pwr_ctrl_enabled(pi) &&
  3618. SAVE_txpwrctrl) {
  3619. index = wlc_lcnphy_tempcompensated_txpwrctrl(pi);
  3620. index2 = (u16) (index * 2);
  3621. mod_phy_reg(pi, 0x4a9, (0x1ff << 0), (index2) << 0);
  3622. pi_lcn->lcnphy_current_index =
  3623. (s8)((read_phy_reg(pi, 0x4a9) & 0xFF) / 2);
  3624. }
  3625. }
  3626. static void
  3627. wlc_lcnphy_load_tx_gain_table(struct brcms_phy *pi,
  3628. const struct lcnphy_tx_gain_tbl_entry *gain_table)
  3629. {
  3630. u32 j;
  3631. struct phytbl_info tab;
  3632. u32 val;
  3633. u16 pa_gain;
  3634. u16 gm_gain;
  3635. if (CHSPEC_IS5G(pi->radio_chanspec))
  3636. pa_gain = 0x70;
  3637. else
  3638. pa_gain = 0x60;
  3639. if (pi->sh->boardflags & BFL_FEM)
  3640. pa_gain = 0x10;
  3641. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3642. tab.tbl_width = 32;
  3643. tab.tbl_len = 1;
  3644. tab.tbl_ptr = &val;
  3645. for (j = 0; j < 128; j++) {
  3646. if (pi->sh->boardflags & BFL_FEM)
  3647. gm_gain = gain_table[j].gm;
  3648. else
  3649. gm_gain = 15;
  3650. val = (((u32) pa_gain << 24) |
  3651. (gain_table[j].pad << 16) |
  3652. (gain_table[j].pga << 8) | gm_gain);
  3653. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + j;
  3654. wlc_lcnphy_write_table(pi, &tab);
  3655. val = (gain_table[j].dac << 28) | (gain_table[j].bb_mult << 20);
  3656. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + j;
  3657. wlc_lcnphy_write_table(pi, &tab);
  3658. }
  3659. }
  3660. static void wlc_lcnphy_load_rfpower(struct brcms_phy *pi)
  3661. {
  3662. struct phytbl_info tab;
  3663. u32 val, bbmult, rfgain;
  3664. u8 index;
  3665. u8 scale_factor = 1;
  3666. s16 temp, temp1, temp2, qQ, qQ1, qQ2, shift;
  3667. tab.tbl_id = LCNPHY_TBL_ID_TXPWRCTL;
  3668. tab.tbl_width = 32;
  3669. tab.tbl_len = 1;
  3670. for (index = 0; index < 128; index++) {
  3671. tab.tbl_ptr = &bbmult;
  3672. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_IQ_OFFSET + index;
  3673. wlc_lcnphy_read_table(pi, &tab);
  3674. bbmult = bbmult >> 20;
  3675. tab.tbl_ptr = &rfgain;
  3676. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_GAIN_OFFSET + index;
  3677. wlc_lcnphy_read_table(pi, &tab);
  3678. qm_log10((s32) (bbmult), 0, &temp1, &qQ1);
  3679. qm_log10((s32) (1 << 6), 0, &temp2, &qQ2);
  3680. if (qQ1 < qQ2) {
  3681. temp2 = qm_shr16(temp2, qQ2 - qQ1);
  3682. qQ = qQ1;
  3683. } else {
  3684. temp1 = qm_shr16(temp1, qQ1 - qQ2);
  3685. qQ = qQ2;
  3686. }
  3687. temp = qm_sub16(temp1, temp2);
  3688. if (qQ >= 4)
  3689. shift = qQ - 4;
  3690. else
  3691. shift = 4 - qQ;
  3692. val = (((index << shift) + (5 * temp) +
  3693. (1 << (scale_factor + shift - 3))) >> (scale_factor +
  3694. shift - 2));
  3695. tab.tbl_ptr = &val;
  3696. tab.tbl_offset = LCNPHY_TX_PWR_CTRL_PWR_OFFSET + index;
  3697. wlc_lcnphy_write_table(pi, &tab);
  3698. }
  3699. }
  3700. static void wlc_lcnphy_bu_tweaks(struct brcms_phy *pi)
  3701. {
  3702. or_phy_reg(pi, 0x805, 0x1);
  3703. mod_phy_reg(pi, 0x42f, (0x7 << 0), (0x3) << 0);
  3704. mod_phy_reg(pi, 0x030, (0x7 << 0), (0x3) << 0);
  3705. write_phy_reg(pi, 0x414, 0x1e10);
  3706. write_phy_reg(pi, 0x415, 0x0640);
  3707. mod_phy_reg(pi, 0x4df, (0xff << 8), -9 << 8);
  3708. or_phy_reg(pi, 0x44a, 0x44);
  3709. write_phy_reg(pi, 0x44a, 0x80);
  3710. mod_phy_reg(pi, 0x434, (0xff << 0), (0xFD) << 0);
  3711. mod_phy_reg(pi, 0x420, (0xff << 0), (16) << 0);
  3712. if (!(pi->sh->boardrev < 0x1204))
  3713. mod_radio_reg(pi, RADIO_2064_REG09B, 0xF0, 0xF0);
  3714. write_phy_reg(pi, 0x7d6, 0x0902);
  3715. mod_phy_reg(pi, 0x429, (0xf << 0), (0x9) << 0);
  3716. mod_phy_reg(pi, 0x429, (0x3f << 4), (0xe) << 4);
  3717. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  3718. mod_phy_reg(pi, 0x423, (0xff << 0), (0x46) << 0);
  3719. mod_phy_reg(pi, 0x411, (0xff << 0), (1) << 0);
  3720. mod_phy_reg(pi, 0x434, (0xff << 0), (0xFF) << 0);
  3721. mod_phy_reg(pi, 0x656, (0xf << 0), (2) << 0);
  3722. mod_phy_reg(pi, 0x44d, (0x1 << 2), (1) << 2);
  3723. mod_radio_reg(pi, RADIO_2064_REG0F7, 0x4, 0x4);
  3724. mod_radio_reg(pi, RADIO_2064_REG0F1, 0x3, 0);
  3725. mod_radio_reg(pi, RADIO_2064_REG0F2, 0xF8, 0x90);
  3726. mod_radio_reg(pi, RADIO_2064_REG0F3, 0x3, 0x2);
  3727. mod_radio_reg(pi, RADIO_2064_REG0F3, 0xf0, 0xa0);
  3728. mod_radio_reg(pi, RADIO_2064_REG11F, 0x2, 0x2);
  3729. wlc_lcnphy_clear_tx_power_offsets(pi);
  3730. mod_phy_reg(pi, 0x4d0, (0x1ff << 6), (10) << 6);
  3731. }
  3732. }
  3733. static void wlc_lcnphy_rcal(struct brcms_phy *pi)
  3734. {
  3735. u8 rcal_value;
  3736. and_radio_reg(pi, RADIO_2064_REG05B, 0xfD);
  3737. or_radio_reg(pi, RADIO_2064_REG004, 0x40);
  3738. or_radio_reg(pi, RADIO_2064_REG120, 0x10);
  3739. or_radio_reg(pi, RADIO_2064_REG078, 0x80);
  3740. or_radio_reg(pi, RADIO_2064_REG129, 0x02);
  3741. or_radio_reg(pi, RADIO_2064_REG057, 0x01);
  3742. or_radio_reg(pi, RADIO_2064_REG05B, 0x02);
  3743. mdelay(5);
  3744. SPINWAIT(!wlc_radio_2064_rcal_done(pi), 10 * 1000 * 1000);
  3745. if (wlc_radio_2064_rcal_done(pi)) {
  3746. rcal_value = (u8) read_radio_reg(pi, RADIO_2064_REG05C);
  3747. rcal_value = rcal_value & 0x1f;
  3748. }
  3749. and_radio_reg(pi, RADIO_2064_REG05B, 0xfD);
  3750. and_radio_reg(pi, RADIO_2064_REG057, 0xFE);
  3751. }
  3752. static void wlc_lcnphy_rc_cal(struct brcms_phy *pi)
  3753. {
  3754. u8 dflt_rc_cal_val;
  3755. u16 flt_val;
  3756. dflt_rc_cal_val = 7;
  3757. if (LCNREV_IS(pi->pubpi.phy_rev, 1))
  3758. dflt_rc_cal_val = 11;
  3759. flt_val =
  3760. (dflt_rc_cal_val << 10) | (dflt_rc_cal_val << 5) |
  3761. (dflt_rc_cal_val);
  3762. write_phy_reg(pi, 0x933, flt_val);
  3763. write_phy_reg(pi, 0x934, flt_val);
  3764. write_phy_reg(pi, 0x935, flt_val);
  3765. write_phy_reg(pi, 0x936, flt_val);
  3766. write_phy_reg(pi, 0x937, (flt_val & 0x1FF));
  3767. return;
  3768. }
  3769. static void wlc_radio_2064_init(struct brcms_phy *pi)
  3770. {
  3771. u32 i;
  3772. const struct lcnphy_radio_regs *lcnphyregs = NULL;
  3773. lcnphyregs = lcnphy_radio_regs_2064;
  3774. for (i = 0; lcnphyregs[i].address != 0xffff; i++)
  3775. if (CHSPEC_IS5G(pi->radio_chanspec) && lcnphyregs[i].do_init_a)
  3776. write_radio_reg(pi,
  3777. ((lcnphyregs[i].address & 0x3fff) |
  3778. RADIO_DEFAULT_CORE),
  3779. (u16) lcnphyregs[i].init_a);
  3780. else if (lcnphyregs[i].do_init_g)
  3781. write_radio_reg(pi,
  3782. ((lcnphyregs[i].address & 0x3fff) |
  3783. RADIO_DEFAULT_CORE),
  3784. (u16) lcnphyregs[i].init_g);
  3785. write_radio_reg(pi, RADIO_2064_REG032, 0x62);
  3786. write_radio_reg(pi, RADIO_2064_REG033, 0x19);
  3787. write_radio_reg(pi, RADIO_2064_REG090, 0x10);
  3788. write_radio_reg(pi, RADIO_2064_REG010, 0x00);
  3789. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  3790. write_radio_reg(pi, RADIO_2064_REG060, 0x7f);
  3791. write_radio_reg(pi, RADIO_2064_REG061, 0x72);
  3792. write_radio_reg(pi, RADIO_2064_REG062, 0x7f);
  3793. }
  3794. write_radio_reg(pi, RADIO_2064_REG01D, 0x02);
  3795. write_radio_reg(pi, RADIO_2064_REG01E, 0x06);
  3796. mod_phy_reg(pi, 0x4ea, (0x7 << 0), 0 << 0);
  3797. mod_phy_reg(pi, 0x4ea, (0x7 << 3), 1 << 3);
  3798. mod_phy_reg(pi, 0x4ea, (0x7 << 6), 2 << 6);
  3799. mod_phy_reg(pi, 0x4ea, (0x7 << 9), 3 << 9);
  3800. mod_phy_reg(pi, 0x4ea, (0x7 << 12), 4 << 12);
  3801. write_phy_reg(pi, 0x4ea, 0x4688);
  3802. if (pi->sh->boardflags & BFL_FEM)
  3803. mod_phy_reg(pi, 0x4eb, (0x7 << 0), 2 << 0);
  3804. else
  3805. mod_phy_reg(pi, 0x4eb, (0x7 << 0), 3 << 0);
  3806. mod_phy_reg(pi, 0x4eb, (0x7 << 6), 0 << 6);
  3807. mod_phy_reg(pi, 0x46a, (0xffff << 0), 25 << 0);
  3808. wlc_lcnphy_set_tx_locc(pi, 0);
  3809. wlc_lcnphy_rcal(pi);
  3810. wlc_lcnphy_rc_cal(pi);
  3811. if (!(pi->sh->boardflags & BFL_FEM)) {
  3812. write_radio_reg(pi, RADIO_2064_REG032, 0x6f);
  3813. write_radio_reg(pi, RADIO_2064_REG033, 0x19);
  3814. write_radio_reg(pi, RADIO_2064_REG039, 0xe);
  3815. }
  3816. }
  3817. static void wlc_lcnphy_radio_init(struct brcms_phy *pi)
  3818. {
  3819. wlc_radio_2064_init(pi);
  3820. }
  3821. static void wlc_lcnphy_tbl_init(struct brcms_phy *pi)
  3822. {
  3823. uint idx;
  3824. u8 phybw40;
  3825. struct phytbl_info tab;
  3826. u32 val;
  3827. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3828. for (idx = 0; idx < dot11lcnphytbl_info_sz_rev0; idx++)
  3829. wlc_lcnphy_write_table(pi, &dot11lcnphytbl_info_rev0[idx]);
  3830. if (pi->sh->boardflags & BFL_FEM_BT) {
  3831. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  3832. tab.tbl_width = 16;
  3833. tab.tbl_ptr = &val;
  3834. tab.tbl_len = 1;
  3835. val = 100;
  3836. tab.tbl_offset = 4;
  3837. wlc_lcnphy_write_table(pi, &tab);
  3838. }
  3839. if (!(pi->sh->boardflags & BFL_FEM)) {
  3840. tab.tbl_id = LCNPHY_TBL_ID_RFSEQ;
  3841. tab.tbl_width = 16;
  3842. tab.tbl_ptr = &val;
  3843. tab.tbl_len = 1;
  3844. val = 150;
  3845. tab.tbl_offset = 0;
  3846. wlc_lcnphy_write_table(pi, &tab);
  3847. val = 220;
  3848. tab.tbl_offset = 1;
  3849. wlc_lcnphy_write_table(pi, &tab);
  3850. }
  3851. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  3852. if (pi->sh->boardflags & BFL_FEM)
  3853. wlc_lcnphy_load_tx_gain_table(
  3854. pi,
  3855. dot11lcnphy_2GHz_extPA_gaintable_rev0);
  3856. else
  3857. wlc_lcnphy_load_tx_gain_table(
  3858. pi,
  3859. dot11lcnphy_2GHz_gaintable_rev0);
  3860. }
  3861. if (LCNREV_IS(pi->pubpi.phy_rev, 2)) {
  3862. const struct phytbl_info *tb;
  3863. int l;
  3864. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  3865. l = dot11lcnphytbl_rx_gain_info_2G_rev2_sz;
  3866. if (pi->sh->boardflags & BFL_EXTLNA)
  3867. tb = dot11lcnphytbl_rx_gain_info_extlna_2G_rev2;
  3868. else
  3869. tb = dot11lcnphytbl_rx_gain_info_2G_rev2;
  3870. } else {
  3871. l = dot11lcnphytbl_rx_gain_info_5G_rev2_sz;
  3872. if (pi->sh->boardflags & BFL_EXTLNA_5GHz)
  3873. tb = dot11lcnphytbl_rx_gain_info_extlna_5G_rev2;
  3874. else
  3875. tb = dot11lcnphytbl_rx_gain_info_5G_rev2;
  3876. }
  3877. for (idx = 0; idx < l; idx++)
  3878. wlc_lcnphy_write_table(pi, &tb[idx]);
  3879. }
  3880. if ((pi->sh->boardflags & BFL_FEM)
  3881. && !(pi->sh->boardflags & BFL_FEM_BT))
  3882. wlc_lcnphy_write_table(pi, &dot11lcn_sw_ctrl_tbl_info_4313_epa);
  3883. else if (pi->sh->boardflags & BFL_FEM_BT) {
  3884. if (pi->sh->boardrev < 0x1250)
  3885. wlc_lcnphy_write_table(
  3886. pi,
  3887. &dot11lcn_sw_ctrl_tbl_info_4313_bt_epa);
  3888. else
  3889. wlc_lcnphy_write_table(
  3890. pi,
  3891. &dot11lcn_sw_ctrl_tbl_info_4313_bt_epa_p250);
  3892. } else
  3893. wlc_lcnphy_write_table(pi, &dot11lcn_sw_ctrl_tbl_info_4313);
  3894. wlc_lcnphy_load_rfpower(pi);
  3895. wlc_lcnphy_clear_papd_comptable(pi);
  3896. }
  3897. static void wlc_lcnphy_rev0_baseband_init(struct brcms_phy *pi)
  3898. {
  3899. u16 afectrl1;
  3900. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3901. write_radio_reg(pi, RADIO_2064_REG11C, 0x0);
  3902. write_phy_reg(pi, 0x43b, 0x0);
  3903. write_phy_reg(pi, 0x43c, 0x0);
  3904. write_phy_reg(pi, 0x44c, 0x0);
  3905. write_phy_reg(pi, 0x4e6, 0x0);
  3906. write_phy_reg(pi, 0x4f9, 0x0);
  3907. write_phy_reg(pi, 0x4b0, 0x0);
  3908. write_phy_reg(pi, 0x938, 0x0);
  3909. write_phy_reg(pi, 0x4b0, 0x0);
  3910. write_phy_reg(pi, 0x44e, 0);
  3911. or_phy_reg(pi, 0x567, 0x03);
  3912. or_phy_reg(pi, 0x44a, 0x44);
  3913. write_phy_reg(pi, 0x44a, 0x80);
  3914. if (!(pi->sh->boardflags & BFL_FEM))
  3915. wlc_lcnphy_set_tx_pwr_by_index(pi, 52);
  3916. if (0) {
  3917. afectrl1 = 0;
  3918. afectrl1 = (u16) ((pi_lcn->lcnphy_rssi_vf) |
  3919. (pi_lcn->lcnphy_rssi_vc << 4) |
  3920. (pi_lcn->lcnphy_rssi_gs << 10));
  3921. write_phy_reg(pi, 0x43e, afectrl1);
  3922. }
  3923. mod_phy_reg(pi, 0x634, (0xff << 0), 0xC << 0);
  3924. if (pi->sh->boardflags & BFL_FEM) {
  3925. mod_phy_reg(pi, 0x634, (0xff << 0), 0xA << 0);
  3926. write_phy_reg(pi, 0x910, 0x1);
  3927. }
  3928. mod_phy_reg(pi, 0x448, (0x3 << 8), 1 << 8);
  3929. mod_phy_reg(pi, 0x608, (0xff << 0), 0x17 << 0);
  3930. mod_phy_reg(pi, 0x604, (0x7ff << 0), 0x3EA << 0);
  3931. }
  3932. static void wlc_lcnphy_rev2_baseband_init(struct brcms_phy *pi)
  3933. {
  3934. if (CHSPEC_IS5G(pi->radio_chanspec)) {
  3935. mod_phy_reg(pi, 0x416, (0xff << 0), 80 << 0);
  3936. mod_phy_reg(pi, 0x416, (0xff << 8), 80 << 8);
  3937. }
  3938. }
  3939. static void wlc_lcnphy_agc_temp_init(struct brcms_phy *pi)
  3940. {
  3941. s16 temp;
  3942. struct phytbl_info tab;
  3943. u32 tableBuffer[2];
  3944. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3945. temp = (s16) read_phy_reg(pi, 0x4df);
  3946. pi_lcn->lcnphy_ofdmgainidxtableoffset = (temp & (0xff << 0)) >> 0;
  3947. if (pi_lcn->lcnphy_ofdmgainidxtableoffset > 127)
  3948. pi_lcn->lcnphy_ofdmgainidxtableoffset -= 256;
  3949. pi_lcn->lcnphy_dsssgainidxtableoffset = (temp & (0xff << 8)) >> 8;
  3950. if (pi_lcn->lcnphy_dsssgainidxtableoffset > 127)
  3951. pi_lcn->lcnphy_dsssgainidxtableoffset -= 256;
  3952. tab.tbl_ptr = tableBuffer;
  3953. tab.tbl_len = 2;
  3954. tab.tbl_id = 17;
  3955. tab.tbl_offset = 59;
  3956. tab.tbl_width = 32;
  3957. wlc_lcnphy_read_table(pi, &tab);
  3958. if (tableBuffer[0] > 63)
  3959. tableBuffer[0] -= 128;
  3960. pi_lcn->lcnphy_tr_R_gain_val = tableBuffer[0];
  3961. if (tableBuffer[1] > 63)
  3962. tableBuffer[1] -= 128;
  3963. pi_lcn->lcnphy_tr_T_gain_val = tableBuffer[1];
  3964. temp = (s16) (read_phy_reg(pi, 0x434) & (0xff << 0));
  3965. if (temp > 127)
  3966. temp -= 256;
  3967. pi_lcn->lcnphy_input_pwr_offset_db = (s8) temp;
  3968. pi_lcn->lcnphy_Med_Low_Gain_db =
  3969. (read_phy_reg(pi, 0x424) & (0xff << 8)) >> 8;
  3970. pi_lcn->lcnphy_Very_Low_Gain_db =
  3971. (read_phy_reg(pi, 0x425) & (0xff << 0)) >> 0;
  3972. tab.tbl_ptr = tableBuffer;
  3973. tab.tbl_len = 2;
  3974. tab.tbl_id = LCNPHY_TBL_ID_GAIN_IDX;
  3975. tab.tbl_offset = 28;
  3976. tab.tbl_width = 32;
  3977. wlc_lcnphy_read_table(pi, &tab);
  3978. pi_lcn->lcnphy_gain_idx_14_lowword = tableBuffer[0];
  3979. pi_lcn->lcnphy_gain_idx_14_hiword = tableBuffer[1];
  3980. }
  3981. static void wlc_lcnphy_baseband_init(struct brcms_phy *pi)
  3982. {
  3983. wlc_lcnphy_tbl_init(pi);
  3984. wlc_lcnphy_rev0_baseband_init(pi);
  3985. if (LCNREV_IS(pi->pubpi.phy_rev, 2))
  3986. wlc_lcnphy_rev2_baseband_init(pi);
  3987. wlc_lcnphy_bu_tweaks(pi);
  3988. }
  3989. void wlc_phy_init_lcnphy(struct brcms_phy *pi)
  3990. {
  3991. u8 phybw40;
  3992. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  3993. phybw40 = CHSPEC_IS40(pi->radio_chanspec);
  3994. pi_lcn->lcnphy_cal_counter = 0;
  3995. pi_lcn->lcnphy_cal_temper = pi_lcn->lcnphy_rawtempsense;
  3996. or_phy_reg(pi, 0x44a, 0x80);
  3997. and_phy_reg(pi, 0x44a, 0x7f);
  3998. wlc_lcnphy_afe_clk_init(pi, AFE_CLK_INIT_MODE_TXRX2X);
  3999. write_phy_reg(pi, 0x60a, 160);
  4000. write_phy_reg(pi, 0x46a, 25);
  4001. wlc_lcnphy_baseband_init(pi);
  4002. wlc_lcnphy_radio_init(pi);
  4003. if (CHSPEC_IS2G(pi->radio_chanspec))
  4004. wlc_lcnphy_tx_pwr_ctrl_init((struct brcms_phy_pub *) pi);
  4005. wlc_phy_chanspec_set((struct brcms_phy_pub *) pi, pi->radio_chanspec);
  4006. si_pmu_regcontrol(pi->sh->sih, 0, 0xf, 0x9);
  4007. si_pmu_chipcontrol(pi->sh->sih, 0, 0xffffffff, 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 0;
  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. wlc_lcnphy_tssi_setup(pi);
  4157. }
  4158. void wlc_phy_detach_lcnphy(struct brcms_phy *pi)
  4159. {
  4160. kfree(pi->u.pi_lcnphy);
  4161. }
  4162. bool wlc_phy_attach_lcnphy(struct brcms_phy *pi)
  4163. {
  4164. struct brcms_phy_lcnphy *pi_lcn;
  4165. pi->u.pi_lcnphy = kzalloc(sizeof(struct brcms_phy_lcnphy), GFP_ATOMIC);
  4166. if (pi->u.pi_lcnphy == NULL)
  4167. return false;
  4168. pi_lcn = pi->u.pi_lcnphy;
  4169. if (0 == (pi->sh->boardflags & BFL_NOPA)) {
  4170. pi->hwpwrctrl = true;
  4171. pi->hwpwrctrl_capable = true;
  4172. }
  4173. pi->xtalfreq = si_pmu_alp_clock(pi->sh->sih);
  4174. pi_lcn->lcnphy_papd_rxGnCtrl_init = 0;
  4175. pi->pi_fptr.init = wlc_phy_init_lcnphy;
  4176. pi->pi_fptr.calinit = wlc_phy_cal_init_lcnphy;
  4177. pi->pi_fptr.chanset = wlc_phy_chanspec_set_lcnphy;
  4178. pi->pi_fptr.txpwrrecalc = wlc_phy_txpower_recalc_target_lcnphy;
  4179. pi->pi_fptr.txiqccget = wlc_lcnphy_get_tx_iqcc;
  4180. pi->pi_fptr.txiqccset = wlc_lcnphy_set_tx_iqcc;
  4181. pi->pi_fptr.txloccget = wlc_lcnphy_get_tx_locc;
  4182. pi->pi_fptr.radioloftget = wlc_lcnphy_get_radio_loft;
  4183. pi->pi_fptr.detach = wlc_phy_detach_lcnphy;
  4184. if (!wlc_phy_txpwr_srom_read_lcnphy(pi))
  4185. return false;
  4186. if (LCNREV_IS(pi->pubpi.phy_rev, 1)) {
  4187. if (pi_lcn->lcnphy_tempsense_option == 3) {
  4188. pi->hwpwrctrl = true;
  4189. pi->hwpwrctrl_capable = true;
  4190. pi->temppwrctrl_capable = false;
  4191. } else {
  4192. pi->hwpwrctrl = false;
  4193. pi->hwpwrctrl_capable = false;
  4194. pi->temppwrctrl_capable = true;
  4195. }
  4196. }
  4197. return true;
  4198. }
  4199. static void wlc_lcnphy_set_rx_gain(struct brcms_phy *pi, u32 gain)
  4200. {
  4201. u16 trsw, ext_lna, lna1, lna2, tia, biq0, biq1, gain0_15, gain16_19;
  4202. trsw = (gain & ((u32) 1 << 28)) ? 0 : 1;
  4203. ext_lna = (u16) (gain >> 29) & 0x01;
  4204. lna1 = (u16) (gain >> 0) & 0x0f;
  4205. lna2 = (u16) (gain >> 4) & 0x0f;
  4206. tia = (u16) (gain >> 8) & 0xf;
  4207. biq0 = (u16) (gain >> 12) & 0xf;
  4208. biq1 = (u16) (gain >> 16) & 0xf;
  4209. gain0_15 = (u16) ((lna1 & 0x3) | ((lna1 & 0x3) << 2) |
  4210. ((lna2 & 0x3) << 4) | ((lna2 & 0x3) << 6) |
  4211. ((tia & 0xf) << 8) | ((biq0 & 0xf) << 12));
  4212. gain16_19 = biq1;
  4213. mod_phy_reg(pi, 0x44d, (0x1 << 0), trsw << 0);
  4214. mod_phy_reg(pi, 0x4b1, (0x1 << 9), ext_lna << 9);
  4215. mod_phy_reg(pi, 0x4b1, (0x1 << 10), ext_lna << 10);
  4216. mod_phy_reg(pi, 0x4b6, (0xffff << 0), gain0_15 << 0);
  4217. mod_phy_reg(pi, 0x4b7, (0xf << 0), gain16_19 << 0);
  4218. if (CHSPEC_IS2G(pi->radio_chanspec)) {
  4219. mod_phy_reg(pi, 0x4b1, (0x3 << 11), lna1 << 11);
  4220. mod_phy_reg(pi, 0x4e6, (0x3 << 3), lna1 << 3);
  4221. }
  4222. wlc_lcnphy_rx_gain_override_enable(pi, true);
  4223. }
  4224. static u32 wlc_lcnphy_get_receive_power(struct brcms_phy *pi, s32 *gain_index)
  4225. {
  4226. u32 received_power = 0;
  4227. s32 max_index = 0;
  4228. u32 gain_code = 0;
  4229. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4230. max_index = 36;
  4231. if (*gain_index >= 0)
  4232. gain_code = lcnphy_23bitgaincode_table[*gain_index];
  4233. if (-1 == *gain_index) {
  4234. *gain_index = 0;
  4235. while ((*gain_index <= (s32) max_index)
  4236. && (received_power < 700)) {
  4237. wlc_lcnphy_set_rx_gain(pi,
  4238. lcnphy_23bitgaincode_table
  4239. [*gain_index]);
  4240. received_power =
  4241. wlc_lcnphy_measure_digital_power(
  4242. pi,
  4243. pi_lcn->
  4244. lcnphy_noise_samples);
  4245. (*gain_index)++;
  4246. }
  4247. (*gain_index)--;
  4248. } else {
  4249. wlc_lcnphy_set_rx_gain(pi, gain_code);
  4250. received_power =
  4251. wlc_lcnphy_measure_digital_power(pi,
  4252. pi_lcn->
  4253. lcnphy_noise_samples);
  4254. }
  4255. return received_power;
  4256. }
  4257. s32 wlc_lcnphy_rx_signal_power(struct brcms_phy *pi, s32 gain_index)
  4258. {
  4259. s32 gain = 0;
  4260. s32 nominal_power_db;
  4261. s32 log_val, gain_mismatch, desired_gain, input_power_offset_db,
  4262. input_power_db;
  4263. s32 received_power, temperature;
  4264. u32 power;
  4265. u32 msb1, msb2, val1, val2, diff1, diff2;
  4266. uint freq;
  4267. struct brcms_phy_lcnphy *pi_lcn = pi->u.pi_lcnphy;
  4268. received_power = wlc_lcnphy_get_receive_power(pi, &gain_index);
  4269. gain = lcnphy_gain_table[gain_index];
  4270. nominal_power_db = read_phy_reg(pi, 0x425) >> 8;
  4271. power = (received_power * 16);
  4272. msb1 = ffs(power) - 1;
  4273. msb2 = msb1 + 1;
  4274. val1 = 1 << msb1;
  4275. val2 = 1 << msb2;
  4276. diff1 = (power - val1);
  4277. diff2 = (val2 - power);
  4278. if (diff1 < diff2)
  4279. log_val = msb1;
  4280. else
  4281. log_val = msb2;
  4282. log_val = log_val * 3;
  4283. gain_mismatch = (nominal_power_db / 2) - (log_val);
  4284. desired_gain = gain + gain_mismatch;
  4285. input_power_offset_db = read_phy_reg(pi, 0x434) & 0xFF;
  4286. if (input_power_offset_db > 127)
  4287. input_power_offset_db -= 256;
  4288. input_power_db = input_power_offset_db - desired_gain;
  4289. input_power_db =
  4290. input_power_db + lcnphy_gain_index_offset_for_rssi[gain_index];
  4291. freq = wlc_phy_channel2freq(CHSPEC_CHANNEL(pi->radio_chanspec));
  4292. if ((freq > 2427) && (freq <= 2467))
  4293. input_power_db = input_power_db - 1;
  4294. temperature = pi_lcn->lcnphy_lastsensed_temperature;
  4295. if ((temperature - 15) < -30)
  4296. input_power_db =
  4297. input_power_db +
  4298. (((temperature - 10 - 25) * 286) >> 12) -
  4299. 7;
  4300. else if ((temperature - 15) < 4)
  4301. input_power_db =
  4302. input_power_db +
  4303. (((temperature - 10 - 25) * 286) >> 12) -
  4304. 3;
  4305. else
  4306. input_power_db = input_power_db +
  4307. (((temperature - 10 - 25) * 286) >> 12);
  4308. wlc_lcnphy_rx_gain_override_enable(pi, 0);
  4309. return input_power_db;
  4310. }