rtw8851b_rfk.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* Copyright(c) 2022-2023 Realtek Corporation
  3. */
  4. #include "coex.h"
  5. #include "debug.h"
  6. #include "mac.h"
  7. #include "phy.h"
  8. #include "reg.h"
  9. #include "rtw8851b.h"
  10. #include "rtw8851b_rfk.h"
  11. #include "rtw8851b_rfk_table.h"
  12. #include "rtw8851b_table.h"
  13. #define DPK_VER_8851B 0x5
  14. #define DPK_KIP_REG_NUM_8851B 7
  15. #define DPK_RF_REG_NUM_8851B 4
  16. #define DPK_KSET_NUM 4
  17. #define RTW8851B_RXK_GROUP_NR 4
  18. #define RTW8851B_RXK_GROUP_IDX_NR 2
  19. #define RTW8851B_TXK_GROUP_NR 1
  20. #define RTW8851B_IQK_VER 0x2a
  21. #define RTW8851B_IQK_SS 1
  22. #define RTW8851B_LOK_GRAM 10
  23. #define RTW8851B_TSSI_PATH_NR 1
  24. #define _TSSI_DE_MASK GENMASK(21, 12)
  25. enum dpk_id {
  26. LBK_RXIQK = 0x06,
  27. SYNC = 0x10,
  28. MDPK_IDL = 0x11,
  29. MDPK_MPA = 0x12,
  30. GAIN_LOSS = 0x13,
  31. GAIN_CAL = 0x14,
  32. DPK_RXAGC = 0x15,
  33. KIP_PRESET = 0x16,
  34. KIP_RESTORE = 0x17,
  35. DPK_TXAGC = 0x19,
  36. D_KIP_PRESET = 0x28,
  37. D_TXAGC = 0x29,
  38. D_RXAGC = 0x2a,
  39. D_SYNC = 0x2b,
  40. D_GAIN_LOSS = 0x2c,
  41. D_MDPK_IDL = 0x2d,
  42. D_MDPK_LDL = 0x2e,
  43. D_GAIN_NORM = 0x2f,
  44. D_KIP_THERMAL = 0x30,
  45. D_KIP_RESTORE = 0x31
  46. };
  47. enum dpk_agc_step {
  48. DPK_AGC_STEP_SYNC_DGAIN,
  49. DPK_AGC_STEP_GAIN_LOSS_IDX,
  50. DPK_AGC_STEP_GL_GT_CRITERION,
  51. DPK_AGC_STEP_GL_LT_CRITERION,
  52. DPK_AGC_STEP_SET_TX_GAIN,
  53. };
  54. enum rtw8851b_iqk_type {
  55. ID_TXAGC = 0x0,
  56. ID_FLOK_COARSE = 0x1,
  57. ID_FLOK_FINE = 0x2,
  58. ID_TXK = 0x3,
  59. ID_RXAGC = 0x4,
  60. ID_RXK = 0x5,
  61. ID_NBTXK = 0x6,
  62. ID_NBRXK = 0x7,
  63. ID_FLOK_VBUFFER = 0x8,
  64. ID_A_FLOK_COARSE = 0x9,
  65. ID_G_FLOK_COARSE = 0xa,
  66. ID_A_FLOK_FINE = 0xb,
  67. ID_G_FLOK_FINE = 0xc,
  68. ID_IQK_RESTORE = 0x10,
  69. };
  70. enum rf_mode {
  71. RF_SHUT_DOWN = 0x0,
  72. RF_STANDBY = 0x1,
  73. RF_TX = 0x2,
  74. RF_RX = 0x3,
  75. RF_TXIQK = 0x4,
  76. RF_DPK = 0x5,
  77. RF_RXK1 = 0x6,
  78. RF_RXK2 = 0x7,
  79. };
  80. static const u32 _tssi_de_cck_long[RF_PATH_NUM_8851B] = {0x5858};
  81. static const u32 _tssi_de_cck_short[RF_PATH_NUM_8851B] = {0x5860};
  82. static const u32 _tssi_de_mcs_20m[RF_PATH_NUM_8851B] = {0x5838};
  83. static const u32 _tssi_de_mcs_40m[RF_PATH_NUM_8851B] = {0x5840};
  84. static const u32 _tssi_de_mcs_80m[RF_PATH_NUM_8851B] = {0x5848};
  85. static const u32 _tssi_de_mcs_80m_80m[RF_PATH_NUM_8851B] = {0x5850};
  86. static const u32 _tssi_de_mcs_5m[RF_PATH_NUM_8851B] = {0x5828};
  87. static const u32 _tssi_de_mcs_10m[RF_PATH_NUM_8851B] = {0x5830};
  88. static const u32 g_idxrxgain[RTW8851B_RXK_GROUP_NR] = {0x10e, 0x116, 0x28e, 0x296};
  89. static const u32 g_idxattc2[RTW8851B_RXK_GROUP_NR] = {0x0, 0xf, 0x0, 0xf};
  90. static const u32 g_idxrxagc[RTW8851B_RXK_GROUP_NR] = {0x0, 0x1, 0x2, 0x3};
  91. static const u32 a_idxrxgain[RTW8851B_RXK_GROUP_IDX_NR] = {0x10C, 0x28c};
  92. static const u32 a_idxattc2[RTW8851B_RXK_GROUP_IDX_NR] = {0xf, 0xf};
  93. static const u32 a_idxrxagc[RTW8851B_RXK_GROUP_IDX_NR] = {0x4, 0x6};
  94. static const u32 a_power_range[RTW8851B_TXK_GROUP_NR] = {0x0};
  95. static const u32 a_track_range[RTW8851B_TXK_GROUP_NR] = {0x6};
  96. static const u32 a_gain_bb[RTW8851B_TXK_GROUP_NR] = {0x0a};
  97. static const u32 a_itqt[RTW8851B_TXK_GROUP_NR] = {0x12};
  98. static const u32 g_power_range[RTW8851B_TXK_GROUP_NR] = {0x0};
  99. static const u32 g_track_range[RTW8851B_TXK_GROUP_NR] = {0x6};
  100. static const u32 g_gain_bb[RTW8851B_TXK_GROUP_NR] = {0x10};
  101. static const u32 g_itqt[RTW8851B_TXK_GROUP_NR] = {0x12};
  102. static const u32 rtw8851b_backup_bb_regs[] = {0xc0d4, 0xc0d8, 0xc0c4, 0xc0ec, 0xc0e8};
  103. static const u32 rtw8851b_backup_rf_regs[] = {
  104. 0xef, 0xde, 0x0, 0x1e, 0x2, 0x85, 0x90, 0x5};
  105. #define BACKUP_BB_REGS_NR ARRAY_SIZE(rtw8851b_backup_bb_regs)
  106. #define BACKUP_RF_REGS_NR ARRAY_SIZE(rtw8851b_backup_rf_regs)
  107. static const u32 dpk_kip_reg[DPK_KIP_REG_NUM_8851B] = {
  108. 0x813c, 0x8124, 0xc0ec, 0xc0e8, 0xc0c4, 0xc0d4, 0xc0d8};
  109. static const u32 dpk_rf_reg[DPK_RF_REG_NUM_8851B] = {0xde, 0x8f, 0x5, 0x10005};
  110. static void _set_ch(struct rtw89_dev *rtwdev, u32 val);
  111. static u8 _rxk_5ghz_group_from_idx(u8 idx)
  112. {
  113. /* There are four RXK groups (RTW8851B_RXK_GROUP_NR), but only group 0
  114. * and 2 are used in 5 GHz band, so reduce elements to 2.
  115. */
  116. if (idx < RTW8851B_RXK_GROUP_IDX_NR)
  117. return idx * 2;
  118. return 0;
  119. }
  120. static u8 _kpath(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
  121. {
  122. return RF_A;
  123. }
  124. static void _adc_fifo_rst(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  125. u8 path)
  126. {
  127. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x0101);
  128. fsleep(10);
  129. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x1111);
  130. }
  131. static void _rfk_rf_direct_cntrl(struct rtw89_dev *rtwdev,
  132. enum rtw89_rf_path path, bool is_bybb)
  133. {
  134. if (is_bybb)
  135. rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x1);
  136. else
  137. rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
  138. }
  139. static void _rfk_drf_direct_cntrl(struct rtw89_dev *rtwdev,
  140. enum rtw89_rf_path path, bool is_bybb)
  141. {
  142. if (is_bybb)
  143. rtw89_write_rf(rtwdev, path, RR_BBDC, RR_BBDC_SEL, 0x1);
  144. else
  145. rtw89_write_rf(rtwdev, path, RR_BBDC, RR_BBDC_SEL, 0x0);
  146. }
  147. static void _wait_rx_mode(struct rtw89_dev *rtwdev, u8 kpath)
  148. {
  149. u32 rf_mode;
  150. u8 path;
  151. int ret;
  152. for (path = 0; path < RF_PATH_MAX; path++) {
  153. if (!(kpath & BIT(path)))
  154. continue;
  155. ret = read_poll_timeout_atomic(rtw89_read_rf, rf_mode,
  156. rf_mode != 2, 2, 5000, false,
  157. rtwdev, path, 0x00, RR_MOD_MASK);
  158. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  159. "[RFK] Wait S%d to Rx mode!! (ret = %d)\n",
  160. path, ret);
  161. }
  162. }
  163. static void _dack_reset(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  164. {
  165. rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_RST, 0x0);
  166. rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_RST, 0x1);
  167. }
  168. static void _drck(struct rtw89_dev *rtwdev)
  169. {
  170. u32 rck_d;
  171. u32 val;
  172. int ret;
  173. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]Ddie RCK start!!!\n");
  174. rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_IDLE, 0x1);
  175. rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_EN, 0x1);
  176. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
  177. 1, 10000, false,
  178. rtwdev, R_DRCK_RES, B_DRCK_POL);
  179. if (ret)
  180. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DRCK timeout\n");
  181. rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_EN, 0x0);
  182. rtw89_phy_write32_mask(rtwdev, R_DRCK_FH, B_DRCK_LAT, 0x1);
  183. udelay(1);
  184. rtw89_phy_write32_mask(rtwdev, R_DRCK_FH, B_DRCK_LAT, 0x0);
  185. rck_d = rtw89_phy_read32_mask(rtwdev, R_DRCK_RES, 0x7c00);
  186. rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_IDLE, 0x0);
  187. rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_VAL, rck_d);
  188. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0xc0c4 = 0x%x\n",
  189. rtw89_phy_read32_mask(rtwdev, R_DRCK, MASKDWORD));
  190. }
  191. static void _addck_backup(struct rtw89_dev *rtwdev)
  192. {
  193. struct rtw89_dack_info *dack = &rtwdev->dack;
  194. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x0);
  195. dack->addck_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_A0);
  196. dack->addck_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_A1);
  197. }
  198. static void _addck_reload(struct rtw89_dev *rtwdev)
  199. {
  200. struct rtw89_dack_info *dack = &rtwdev->dack;
  201. rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL1, dack->addck_d[0][0]);
  202. rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL0, dack->addck_d[0][1]);
  203. rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RLS, 0x3);
  204. }
  205. static void _dack_backup_s0(struct rtw89_dev *rtwdev)
  206. {
  207. struct rtw89_dack_info *dack = &rtwdev->dack;
  208. u8 i;
  209. rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x1);
  210. for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
  211. rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_V, i);
  212. dack->msbk_d[0][0][i] =
  213. rtw89_phy_read32_mask(rtwdev, R_DACK_S0P2, B_DACK_S0M0);
  214. rtw89_phy_write32_mask(rtwdev, R_DCOF8, B_DCOF8_V, i);
  215. dack->msbk_d[0][1][i] =
  216. rtw89_phy_read32_mask(rtwdev, R_DACK_S0P3, B_DACK_S0M1);
  217. }
  218. dack->biask_d[0][0] =
  219. rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS00, B_DACK_BIAS00);
  220. dack->biask_d[0][1] =
  221. rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS01, B_DACK_BIAS01);
  222. dack->dadck_d[0][0] =
  223. rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK00, B_DACK_DADCK00) + 24;
  224. dack->dadck_d[0][1] =
  225. rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK01, B_DACK_DADCK01) + 24;
  226. }
  227. static void _dack_reload_by_path(struct rtw89_dev *rtwdev,
  228. enum rtw89_rf_path path, u8 index)
  229. {
  230. struct rtw89_dack_info *dack = &rtwdev->dack;
  231. u32 idx_offset, path_offset;
  232. u32 offset, reg;
  233. u32 tmp;
  234. u8 i;
  235. if (index == 0)
  236. idx_offset = 0;
  237. else
  238. idx_offset = 0x14;
  239. if (path == RF_PATH_A)
  240. path_offset = 0;
  241. else
  242. path_offset = 0x28;
  243. offset = idx_offset + path_offset;
  244. rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_RST, 0x1);
  245. rtw89_phy_write32_mask(rtwdev, R_DCOF9, B_DCOF9_RST, 0x1);
  246. /* msbk_d: 15/14/13/12 */
  247. tmp = 0x0;
  248. for (i = 0; i < 4; i++)
  249. tmp |= dack->msbk_d[path][index][i + 12] << (i * 8);
  250. reg = 0xc200 + offset;
  251. rtw89_phy_write32(rtwdev, reg, tmp);
  252. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
  253. rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
  254. /* msbk_d: 11/10/9/8 */
  255. tmp = 0x0;
  256. for (i = 0; i < 4; i++)
  257. tmp |= dack->msbk_d[path][index][i + 8] << (i * 8);
  258. reg = 0xc204 + offset;
  259. rtw89_phy_write32(rtwdev, reg, tmp);
  260. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
  261. rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
  262. /* msbk_d: 7/6/5/4 */
  263. tmp = 0x0;
  264. for (i = 0; i < 4; i++)
  265. tmp |= dack->msbk_d[path][index][i + 4] << (i * 8);
  266. reg = 0xc208 + offset;
  267. rtw89_phy_write32(rtwdev, reg, tmp);
  268. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
  269. rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
  270. /* msbk_d: 3/2/1/0 */
  271. tmp = 0x0;
  272. for (i = 0; i < 4; i++)
  273. tmp |= dack->msbk_d[path][index][i] << (i * 8);
  274. reg = 0xc20c + offset;
  275. rtw89_phy_write32(rtwdev, reg, tmp);
  276. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
  277. rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
  278. /* dadak_d/biask_d */
  279. tmp = 0x0;
  280. tmp = (dack->biask_d[path][index] << 22) |
  281. (dack->dadck_d[path][index] << 14);
  282. reg = 0xc210 + offset;
  283. rtw89_phy_write32(rtwdev, reg, tmp);
  284. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
  285. rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
  286. rtw89_phy_write32_mask(rtwdev, R_DACKN0_CTL + offset, B_DACKN0_EN, 0x1);
  287. }
  288. static void _dack_reload(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  289. {
  290. u8 index;
  291. for (index = 0; index < 2; index++)
  292. _dack_reload_by_path(rtwdev, path, index);
  293. }
  294. static void _addck(struct rtw89_dev *rtwdev)
  295. {
  296. struct rtw89_dack_info *dack = &rtwdev->dack;
  297. u32 val;
  298. int ret;
  299. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x1);
  300. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x1);
  301. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x0);
  302. udelay(1);
  303. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x1);
  304. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
  305. 1, 10000, false,
  306. rtwdev, R_ADDCKR0, BIT(0));
  307. if (ret) {
  308. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADDCK timeout\n");
  309. dack->addck_timeout[0] = true;
  310. }
  311. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]ADDCK ret = %d\n", ret);
  312. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x0);
  313. }
  314. static void _new_dadck(struct rtw89_dev *rtwdev)
  315. {
  316. struct rtw89_dack_info *dack = &rtwdev->dack;
  317. u32 i_dc, q_dc, ic, qc;
  318. u32 val;
  319. int ret;
  320. rtw89_rfk_parser(rtwdev, &rtw8851b_dadck_setup_defs_tbl);
  321. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
  322. 1, 10000, false,
  323. rtwdev, R_ADDCKR0, BIT(0));
  324. if (ret) {
  325. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DADCK timeout\n");
  326. dack->addck_timeout[0] = true;
  327. }
  328. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DADCK ret = %d\n", ret);
  329. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_IQ, 0x0);
  330. i_dc = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_DC);
  331. rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_IQ, 0x1);
  332. q_dc = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_DC);
  333. ic = 0x80 - sign_extend32(i_dc, 11) * 6;
  334. qc = 0x80 - sign_extend32(q_dc, 11) * 6;
  335. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  336. "[DACK]before DADCK, i_dc=0x%x, q_dc=0x%x\n", i_dc, q_dc);
  337. dack->dadck_d[0][0] = ic;
  338. dack->dadck_d[0][1] = qc;
  339. rtw89_phy_write32_mask(rtwdev, R_DACKN0_CTL, B_DACKN0_V, dack->dadck_d[0][0]);
  340. rtw89_phy_write32_mask(rtwdev, R_DACKN1_CTL, B_DACKN1_V, dack->dadck_d[0][1]);
  341. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  342. "[DACK]after DADCK, 0xc210=0x%x, 0xc224=0x%x\n",
  343. rtw89_phy_read32_mask(rtwdev, R_DACKN0_CTL, MASKDWORD),
  344. rtw89_phy_read32_mask(rtwdev, R_DACKN1_CTL, MASKDWORD));
  345. rtw89_rfk_parser(rtwdev, &rtw8851b_dadck_post_defs_tbl);
  346. }
  347. static bool _dack_s0_poll(struct rtw89_dev *rtwdev)
  348. {
  349. if (rtw89_phy_read32_mask(rtwdev, R_DACK_S0P0, B_DACK_S0P0_OK) == 0 ||
  350. rtw89_phy_read32_mask(rtwdev, R_DACK_S0P1, B_DACK_S0P1_OK) == 0 ||
  351. rtw89_phy_read32_mask(rtwdev, R_DACK_S0P2, B_DACK_S0P2_OK) == 0 ||
  352. rtw89_phy_read32_mask(rtwdev, R_DACK_S0P3, B_DACK_S0P3_OK) == 0)
  353. return false;
  354. return true;
  355. }
  356. static void _dack_s0(struct rtw89_dev *rtwdev)
  357. {
  358. struct rtw89_dack_info *dack = &rtwdev->dack;
  359. bool done;
  360. int ret;
  361. rtw89_rfk_parser(rtwdev, &rtw8851b_dack_s0_1_defs_tbl);
  362. _dack_reset(rtwdev, RF_PATH_A);
  363. rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_S, 0x1);
  364. ret = read_poll_timeout_atomic(_dack_s0_poll, done, done,
  365. 1, 10000, false, rtwdev);
  366. if (ret) {
  367. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DACK timeout\n");
  368. dack->msbk_timeout[0] = true;
  369. }
  370. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK ret = %d\n", ret);
  371. rtw89_rfk_parser(rtwdev, &rtw8851b_dack_s0_2_defs_tbl);
  372. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]after S0 DADCK\n");
  373. _dack_backup_s0(rtwdev);
  374. _dack_reload(rtwdev, RF_PATH_A);
  375. rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x0);
  376. }
  377. static void _dack(struct rtw89_dev *rtwdev)
  378. {
  379. _dack_s0(rtwdev);
  380. }
  381. static void _dack_dump(struct rtw89_dev *rtwdev)
  382. {
  383. struct rtw89_dack_info *dack = &rtwdev->dack;
  384. u8 i;
  385. u8 t;
  386. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADC_DCK ic = 0x%x, qc = 0x%x\n",
  387. dack->addck_d[0][0], dack->addck_d[0][1]);
  388. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DAC_DCK ic = 0x%x, qc = 0x%x\n",
  389. dack->dadck_d[0][0], dack->dadck_d[0][1]);
  390. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 biask ic = 0x%x, qc = 0x%x\n",
  391. dack->biask_d[0][0], dack->biask_d[0][1]);
  392. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK ic:\n");
  393. for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
  394. t = dack->msbk_d[0][0][i];
  395. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
  396. }
  397. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK qc:\n");
  398. for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
  399. t = dack->msbk_d[0][1][i];
  400. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
  401. }
  402. }
  403. static void _dack_manual_off(struct rtw89_dev *rtwdev)
  404. {
  405. rtw89_rfk_parser(rtwdev, &rtw8851b_dack_manual_off_defs_tbl);
  406. }
  407. static void _dac_cal(struct rtw89_dev *rtwdev, bool force)
  408. {
  409. struct rtw89_dack_info *dack = &rtwdev->dack;
  410. u32 rf0_0;
  411. dack->dack_done = false;
  412. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK 0x2\n");
  413. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK start!!!\n");
  414. rf0_0 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK);
  415. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]RF0=0x%x\n", rf0_0);
  416. _drck(rtwdev);
  417. _dack_manual_off(rtwdev);
  418. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, 0x337e1);
  419. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x0);
  420. _addck(rtwdev);
  421. _addck_backup(rtwdev);
  422. _addck_reload(rtwdev);
  423. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, 0x40001);
  424. _dack(rtwdev);
  425. _new_dadck(rtwdev);
  426. _dack_dump(rtwdev);
  427. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x1);
  428. dack->dack_done = true;
  429. dack->dack_cnt++;
  430. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK finish!!!\n");
  431. }
  432. static void _rx_dck_info(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  433. enum rtw89_rf_path path, bool is_afe)
  434. {
  435. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  436. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  437. "[RX_DCK] ==== S%d RX DCK (%s / CH%d / %s / by %s)====\n", path,
  438. chan->band_type == RTW89_BAND_2G ? "2G" :
  439. chan->band_type == RTW89_BAND_5G ? "5G" : "6G",
  440. chan->channel,
  441. chan->band_width == RTW89_CHANNEL_WIDTH_20 ? "20M" :
  442. chan->band_width == RTW89_CHANNEL_WIDTH_40 ? "40M" : "80M",
  443. is_afe ? "AFE" : "RFC");
  444. }
  445. static void _rxbb_ofst_swap(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 rf_mode)
  446. {
  447. u32 val, val_i, val_q;
  448. val_i = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_S1);
  449. val_q = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_S1);
  450. val = val_q << 4 | val_i;
  451. rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_DIS, 0x1);
  452. rtw89_write_rf(rtwdev, path, RR_LUTWA, RFREG_MASK, rf_mode);
  453. rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, val);
  454. rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_DIS, 0x0);
  455. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  456. "[RX_DCK] val_i = 0x%x, val_q = 0x%x, 0x3F = 0x%x\n",
  457. val_i, val_q, val);
  458. }
  459. static void _set_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 rf_mode)
  460. {
  461. u32 val;
  462. int ret;
  463. rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
  464. rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x1);
  465. ret = read_poll_timeout_atomic(rtw89_read_rf, val, val,
  466. 2, 2000, false,
  467. rtwdev, path, RR_DCK, BIT(8));
  468. rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
  469. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] S%d RXDCK finish (ret = %d)\n",
  470. path, ret);
  471. _rxbb_ofst_swap(rtwdev, path, rf_mode);
  472. }
  473. static void _rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool is_afe)
  474. {
  475. u32 rf_reg5;
  476. u8 path;
  477. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  478. "[RX_DCK] ****** RXDCK Start (Ver: 0x%x, Cv: %d) ******\n",
  479. 0x2, rtwdev->hal.cv);
  480. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  481. _rx_dck_info(rtwdev, phy, path, is_afe);
  482. rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
  483. if (rtwdev->is_tssi_mode[path])
  484. rtw89_phy_write32_mask(rtwdev,
  485. R_P0_TSSI_TRK + (path << 13),
  486. B_P0_TSSI_TRK_EN, 0x1);
  487. rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
  488. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RF_RX);
  489. _set_rx_dck(rtwdev, path, RF_RX);
  490. rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
  491. if (rtwdev->is_tssi_mode[path])
  492. rtw89_phy_write32_mask(rtwdev,
  493. R_P0_TSSI_TRK + (path << 13),
  494. B_P0_TSSI_TRK_EN, 0x0);
  495. }
  496. }
  497. static void _iqk_sram(struct rtw89_dev *rtwdev, u8 path)
  498. {
  499. u32 i;
  500. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  501. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, MASKDWORD, 0x00020000);
  502. rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, MASKDWORD, 0x80000000);
  503. rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX2, MASKDWORD, 0x00000080);
  504. rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD, 0x00010000);
  505. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x009);
  506. for (i = 0; i <= 0x9f; i++) {
  507. rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD,
  508. 0x00010000 + i);
  509. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]0x%x\n",
  510. rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI));
  511. }
  512. for (i = 0; i <= 0x9f; i++) {
  513. rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD,
  514. 0x00010000 + i);
  515. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]0x%x\n",
  516. rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCQ));
  517. }
  518. rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX2, MASKDWORD, 0x00000000);
  519. rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD, 0x00000000);
  520. }
  521. static void _iqk_rxk_setting(struct rtw89_dev *rtwdev, u8 path)
  522. {
  523. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
  524. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_POW, 0x0);
  525. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_POW, 0x1);
  526. }
  527. static bool _iqk_check_cal(struct rtw89_dev *rtwdev, u8 path)
  528. {
  529. bool fail1 = false, fail2 = false;
  530. u32 val;
  531. int ret;
  532. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
  533. 10, 8200, false,
  534. rtwdev, 0xbff8, MASKBYTE0);
  535. if (ret) {
  536. fail1 = true;
  537. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  538. "[IQK]NCTL1 IQK timeout!!!\n");
  539. }
  540. fsleep(10);
  541. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x8000,
  542. 10, 200, false,
  543. rtwdev, R_RPT_COM, B_RPT_COM_RDY);
  544. if (ret) {
  545. fail2 = true;
  546. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  547. "[IQK]NCTL2 IQK timeout!!!\n");
  548. }
  549. fsleep(10);
  550. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, MASKBYTE0, 0x0);
  551. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  552. "[IQK]S%x, ret = %d, notready = %x fail=%d,%d\n",
  553. path, ret, fail1 || fail2, fail1, fail2);
  554. return fail1 || fail2;
  555. }
  556. static bool _iqk_one_shot(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  557. u8 path, u8 ktype)
  558. {
  559. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  560. bool notready;
  561. u32 iqk_cmd;
  562. switch (ktype) {
  563. case ID_A_FLOK_COARSE:
  564. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  565. "[IQK]============ S%d ID_A_FLOK_COARSE ============\n", path);
  566. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  567. iqk_cmd = 0x108 | (1 << (4 + path));
  568. break;
  569. case ID_G_FLOK_COARSE:
  570. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  571. "[IQK]============ S%d ID_G_FLOK_COARSE ============\n", path);
  572. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  573. iqk_cmd = 0x108 | (1 << (4 + path));
  574. break;
  575. case ID_A_FLOK_FINE:
  576. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  577. "[IQK]============ S%d ID_A_FLOK_FINE ============\n", path);
  578. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  579. iqk_cmd = 0x308 | (1 << (4 + path));
  580. break;
  581. case ID_G_FLOK_FINE:
  582. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  583. "[IQK]============ S%d ID_G_FLOK_FINE ============\n", path);
  584. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  585. iqk_cmd = 0x308 | (1 << (4 + path));
  586. break;
  587. case ID_TXK:
  588. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  589. "[IQK]============ S%d ID_TXK ============\n", path);
  590. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
  591. iqk_cmd = 0x008 | (1 << (path + 4)) |
  592. (((0x8 + iqk_info->iqk_bw[path]) & 0xf) << 8);
  593. break;
  594. case ID_RXAGC:
  595. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  596. "[IQK]============ S%d ID_RXAGC ============\n", path);
  597. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  598. iqk_cmd = 0x708 | (1 << (4 + path)) | (path << 1);
  599. break;
  600. case ID_RXK:
  601. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  602. "[IQK]============ S%d ID_RXK ============\n", path);
  603. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  604. iqk_cmd = 0x008 | (1 << (path + 4)) |
  605. (((0xc + iqk_info->iqk_bw[path]) & 0xf) << 8);
  606. break;
  607. case ID_NBTXK:
  608. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  609. "[IQK]============ S%d ID_NBTXK ============\n", path);
  610. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
  611. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT,
  612. 0x00b);
  613. iqk_cmd = 0x408 | (1 << (4 + path));
  614. break;
  615. case ID_NBRXK:
  616. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  617. "[IQK]============ S%d ID_NBRXK ============\n", path);
  618. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  619. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT,
  620. 0x011);
  621. iqk_cmd = 0x608 | (1 << (4 + path));
  622. break;
  623. default:
  624. return false;
  625. }
  626. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, iqk_cmd + 1);
  627. notready = _iqk_check_cal(rtwdev, path);
  628. if (iqk_info->iqk_sram_en &&
  629. (ktype == ID_NBRXK || ktype == ID_RXK))
  630. _iqk_sram(rtwdev, path);
  631. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
  632. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  633. "[IQK]S%x, ktype= %x, id = %x, notready = %x\n",
  634. path, ktype, iqk_cmd + 1, notready);
  635. return notready;
  636. }
  637. static bool _rxk_2g_group_sel(struct rtw89_dev *rtwdev,
  638. enum rtw89_phy_idx phy_idx, u8 path)
  639. {
  640. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  641. bool kfail = false;
  642. bool notready;
  643. u32 rf_0;
  644. u8 gp;
  645. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  646. for (gp = 0; gp < RTW8851B_RXK_GROUP_NR; gp++) {
  647. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
  648. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RGM, g_idxrxgain[gp]);
  649. rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_C2, g_idxattc2[gp]);
  650. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  651. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
  652. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
  653. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
  654. fsleep(10);
  655. rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
  656. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
  657. rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, g_idxrxagc[gp]);
  658. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
  659. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
  660. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  661. "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n", path,
  662. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
  663. rtw89_read_rf(rtwdev, path, RR_MOD, 0x003e0));
  664. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
  665. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
  666. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
  667. iqk_info->nb_rxcfir[path] =
  668. rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
  669. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
  670. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  671. "[IQK]S%x, WBRXK 0x8008 = 0x%x\n", path,
  672. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  673. }
  674. if (!notready)
  675. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  676. if (kfail)
  677. _iqk_sram(rtwdev, path);
  678. if (kfail) {
  679. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
  680. MASKDWORD, iqk_info->nb_rxcfir[path] | 0x2);
  681. iqk_info->is_wb_txiqk[path] = false;
  682. } else {
  683. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
  684. MASKDWORD, 0x40000000);
  685. iqk_info->is_wb_txiqk[path] = true;
  686. }
  687. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  688. "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
  689. 1 << path, iqk_info->nb_rxcfir[path]);
  690. return kfail;
  691. }
  692. static bool _rxk_5g_group_sel(struct rtw89_dev *rtwdev,
  693. enum rtw89_phy_idx phy_idx, u8 path)
  694. {
  695. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  696. bool kfail = false;
  697. bool notready;
  698. u32 rf_0;
  699. u8 idx;
  700. u8 gp;
  701. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  702. for (idx = 0; idx < RTW8851B_RXK_GROUP_IDX_NR; idx++) {
  703. gp = _rxk_5ghz_group_from_idx(idx);
  704. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
  705. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_RGM, a_idxrxgain[idx]);
  706. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RXA2, RR_RXA2_ATT, a_idxattc2[idx]);
  707. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  708. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
  709. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
  710. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
  711. fsleep(100);
  712. rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
  713. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
  714. rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, a_idxrxagc[idx]);
  715. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
  716. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
  717. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  718. "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n", path,
  719. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
  720. rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_RXB));
  721. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
  722. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
  723. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
  724. iqk_info->nb_rxcfir[path] =
  725. rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
  726. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  727. "[IQK]S%x, NBRXK 0x8008 = 0x%x\n", path,
  728. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  729. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
  730. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  731. "[IQK]S%x, WBRXK 0x8008 = 0x%x\n", path,
  732. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  733. }
  734. if (!notready)
  735. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  736. if (kfail)
  737. _iqk_sram(rtwdev, path);
  738. if (kfail) {
  739. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD,
  740. iqk_info->nb_rxcfir[path] | 0x2);
  741. iqk_info->is_wb_txiqk[path] = false;
  742. } else {
  743. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD,
  744. 0x40000000);
  745. iqk_info->is_wb_txiqk[path] = true;
  746. }
  747. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  748. "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
  749. 1 << path, iqk_info->nb_rxcfir[path]);
  750. return kfail;
  751. }
  752. static bool _iqk_5g_nbrxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  753. u8 path)
  754. {
  755. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  756. bool kfail = false;
  757. bool notready;
  758. u8 idx = 0x1;
  759. u32 rf_0;
  760. u8 gp;
  761. gp = _rxk_5ghz_group_from_idx(idx);
  762. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  763. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
  764. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_RGM, a_idxrxgain[idx]);
  765. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RXA2, RR_RXA2_ATT, a_idxattc2[idx]);
  766. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  767. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
  768. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
  769. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
  770. fsleep(100);
  771. rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
  772. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
  773. rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, a_idxrxagc[idx]);
  774. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
  775. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
  776. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  777. "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n", path,
  778. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
  779. rtw89_read_rf(rtwdev, path, RR_MOD, 0x003e0));
  780. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
  781. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
  782. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
  783. iqk_info->nb_rxcfir[path] =
  784. rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
  785. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  786. "[IQK]S%x, NBRXK 0x8008 = 0x%x\n", path,
  787. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  788. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, WBRXK 0x8008 = 0x%x\n",
  789. path, rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  790. if (!notready)
  791. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  792. if (kfail) {
  793. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
  794. MASKDWORD, 0x40000002);
  795. iqk_info->is_wb_rxiqk[path] = false;
  796. } else {
  797. iqk_info->is_wb_rxiqk[path] = false;
  798. }
  799. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  800. "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
  801. 1 << path, iqk_info->nb_rxcfir[path]);
  802. return kfail;
  803. }
  804. static bool _iqk_2g_nbrxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  805. u8 path)
  806. {
  807. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  808. bool kfail = false;
  809. bool notready;
  810. u8 gp = 0x3;
  811. u32 rf_0;
  812. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  813. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
  814. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RGM, g_idxrxgain[gp]);
  815. rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_C2, g_idxattc2[gp]);
  816. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  817. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
  818. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
  819. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
  820. fsleep(10);
  821. rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
  822. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
  823. rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, g_idxrxagc[gp]);
  824. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
  825. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
  826. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  827. "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n",
  828. path, rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
  829. rtw89_read_rf(rtwdev, path, RR_MOD, 0x003e0));
  830. rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
  831. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
  832. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
  833. iqk_info->nb_rxcfir[path] =
  834. rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
  835. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  836. "[IQK]S%x, NBRXK 0x8008 = 0x%x\n", path,
  837. rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  838. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, WBRXK 0x8008 = 0x%x\n",
  839. path, rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
  840. if (!notready)
  841. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  842. if (kfail) {
  843. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
  844. MASKDWORD, 0x40000002);
  845. iqk_info->is_wb_rxiqk[path] = false;
  846. } else {
  847. iqk_info->is_wb_rxiqk[path] = false;
  848. }
  849. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  850. "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
  851. 1 << path, iqk_info->nb_rxcfir[path]);
  852. return kfail;
  853. }
  854. static void _iqk_rxclk_setting(struct rtw89_dev *rtwdev, u8 path)
  855. {
  856. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  857. rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
  858. if (iqk_info->iqk_bw[path] == RTW89_CHANNEL_WIDTH_80)
  859. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_rxclk_80_defs_tbl);
  860. else
  861. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_rxclk_others_defs_tbl);
  862. }
  863. static bool _txk_5g_group_sel(struct rtw89_dev *rtwdev,
  864. enum rtw89_phy_idx phy_idx, u8 path)
  865. {
  866. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  867. bool kfail = false;
  868. bool notready;
  869. u8 gp;
  870. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  871. for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
  872. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, a_power_range[gp]);
  873. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, a_track_range[gp]);
  874. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, a_gain_bb[gp]);
  875. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  876. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
  877. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
  878. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
  879. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  880. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, a_itqt[gp]);
  881. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
  882. iqk_info->nb_txcfir[path] =
  883. rtw89_phy_read32_mask(rtwdev, R_TXIQC, MASKDWORD) | 0x2;
  884. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
  885. MASKDWORD, a_itqt[gp]);
  886. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_TXK);
  887. }
  888. if (!notready)
  889. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  890. if (kfail) {
  891. rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
  892. MASKDWORD, iqk_info->nb_txcfir[path] | 0x2);
  893. iqk_info->is_wb_txiqk[path] = false;
  894. } else {
  895. rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
  896. MASKDWORD, 0x40000000);
  897. iqk_info->is_wb_txiqk[path] = true;
  898. }
  899. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  900. "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
  901. 1 << path, iqk_info->nb_txcfir[path]);
  902. return kfail;
  903. }
  904. static bool _txk_2g_group_sel(struct rtw89_dev *rtwdev,
  905. enum rtw89_phy_idx phy_idx, u8 path)
  906. {
  907. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  908. bool kfail = false;
  909. bool notready;
  910. u8 gp;
  911. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  912. for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
  913. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, g_power_range[gp]);
  914. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, g_track_range[gp]);
  915. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, g_gain_bb[gp]);
  916. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, g_itqt[gp]);
  917. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  918. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
  919. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
  920. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
  921. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  922. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
  923. iqk_info->nb_txcfir[path] =
  924. rtw89_phy_read32_mask(rtwdev, R_TXIQC, MASKDWORD) | 0x2;
  925. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
  926. MASKDWORD, g_itqt[gp]);
  927. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_TXK);
  928. }
  929. if (!notready)
  930. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  931. if (kfail) {
  932. rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
  933. MASKDWORD, iqk_info->nb_txcfir[path] | 0x2);
  934. iqk_info->is_wb_txiqk[path] = false;
  935. } else {
  936. rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
  937. MASKDWORD, 0x40000000);
  938. iqk_info->is_wb_txiqk[path] = true;
  939. }
  940. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  941. "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
  942. 1 << path, iqk_info->nb_txcfir[path]);
  943. return kfail;
  944. }
  945. static bool _iqk_5g_nbtxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  946. u8 path)
  947. {
  948. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  949. bool kfail = false;
  950. bool notready;
  951. u8 gp;
  952. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  953. for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
  954. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, a_power_range[gp]);
  955. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, a_track_range[gp]);
  956. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, a_gain_bb[gp]);
  957. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  958. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
  959. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
  960. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
  961. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  962. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, a_itqt[gp]);
  963. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
  964. iqk_info->nb_txcfir[path] =
  965. rtw89_phy_read32_mask(rtwdev, R_TXIQC, MASKDWORD) | 0x2;
  966. }
  967. if (!notready)
  968. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  969. if (kfail) {
  970. rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
  971. MASKDWORD, 0x40000002);
  972. iqk_info->is_wb_rxiqk[path] = false;
  973. } else {
  974. iqk_info->is_wb_rxiqk[path] = false;
  975. }
  976. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  977. "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
  978. 1 << path, iqk_info->nb_txcfir[path]);
  979. return kfail;
  980. }
  981. static bool _iqk_2g_nbtxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  982. u8 path)
  983. {
  984. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  985. bool kfail = false;
  986. bool notready;
  987. u8 gp;
  988. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  989. for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
  990. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, g_power_range[gp]);
  991. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, g_track_range[gp]);
  992. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, g_gain_bb[gp]);
  993. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, g_itqt[gp]);
  994. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
  995. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
  996. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
  997. rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
  998. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  999. notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
  1000. iqk_info->nb_txcfir[path] =
  1001. rtw89_phy_read32_mask(rtwdev, R_TXIQC + (path << 8),
  1002. MASKDWORD) | 0x2;
  1003. }
  1004. if (!notready)
  1005. kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
  1006. if (kfail) {
  1007. rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
  1008. MASKDWORD, 0x40000002);
  1009. iqk_info->is_wb_rxiqk[path] = false;
  1010. } else {
  1011. iqk_info->is_wb_rxiqk[path] = false;
  1012. }
  1013. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1014. "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
  1015. 1 << path, iqk_info->nb_txcfir[path]);
  1016. return kfail;
  1017. }
  1018. static bool _iqk_2g_lok(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  1019. u8 path)
  1020. {
  1021. static const u32 g_txbb[RTW8851B_LOK_GRAM] = {
  1022. 0x02, 0x06, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x17};
  1023. static const u32 g_itqt[RTW8851B_LOK_GRAM] = {
  1024. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x12, 0x12, 0x12, 0x1b};
  1025. static const u32 g_wa[RTW8851B_LOK_GRAM] = {
  1026. 0x00, 0x04, 0x08, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x17};
  1027. bool fail = false;
  1028. u8 i;
  1029. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1030. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTDBG, RR_LUTDBG_LOK, 0x0);
  1031. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR0, 0x0);
  1032. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR1, 0x6);
  1033. for (i = 0; i < RTW8851B_LOK_GRAM; i++) {
  1034. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_TG, g_txbb[i]);
  1035. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTWA, RR_LUTWA_M1, g_wa[i]);
  1036. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  1037. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, g_itqt[i]);
  1038. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x021);
  1039. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
  1040. 0x00000109 | (1 << (4 + path)));
  1041. fail |= _iqk_check_cal(rtwdev, path);
  1042. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  1043. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, g_itqt[i]);
  1044. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
  1045. 0x00000309 | (1 << (4 + path)));
  1046. fail |= _iqk_check_cal(rtwdev, path);
  1047. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  1048. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
  1049. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1050. "[IQK]S0, i = %x, 0x8[19:15] = 0x%x,0x8[09:05] = 0x%x\n", i,
  1051. rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0xf8000),
  1052. rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0x003e0));
  1053. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1054. "[IQK]S0, i = %x, 0x9[19:16] = 0x%x,0x9[09:06] = 0x%x\n", i,
  1055. rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0xf0000),
  1056. rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0x003c0));
  1057. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1058. "[IQK]S0, i = %x, 0x58 = %x\n", i,
  1059. rtw89_read_rf(rtwdev, RF_PATH_A, RR_TXMO, RFREG_MASK));
  1060. }
  1061. return fail;
  1062. }
  1063. static bool _iqk_5g_lok(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  1064. u8 path)
  1065. {
  1066. static const u32 a_txbb[RTW8851B_LOK_GRAM] = {
  1067. 0x02, 0x06, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x17};
  1068. static const u32 a_itqt[RTW8851B_LOK_GRAM] = {
  1069. 0x09, 0x09, 0x09, 0x12, 0x12, 0x12, 0x1b, 0x1b, 0x1b, 0x1b};
  1070. static const u32 a_wa[RTW8851B_LOK_GRAM] = {
  1071. 0x80, 0x84, 0x88, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x97};
  1072. bool fail = false;
  1073. u8 i;
  1074. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1075. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTDBG, RR_LUTDBG_LOK, 0x0);
  1076. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR0, 0x0);
  1077. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR1, 0x7);
  1078. for (i = 0; i < RTW8851B_LOK_GRAM; i++) {
  1079. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_TG, a_txbb[i]);
  1080. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTWA, RR_LUTWA_M1, a_wa[i]);
  1081. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
  1082. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, a_itqt[i]);
  1083. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x021);
  1084. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
  1085. 0x00000109 | (1 << (4 + path)));
  1086. fail |= _iqk_check_cal(rtwdev, path);
  1087. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  1088. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, a_itqt[i]);
  1089. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x021);
  1090. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
  1091. 0x00000309 | (1 << (4 + path)));
  1092. fail |= _iqk_check_cal(rtwdev, path);
  1093. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  1094. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
  1095. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1096. "[IQK]S0, i = %x, 0x8[19:15] = 0x%x,0x8[09:05] = 0x%x\n", i,
  1097. rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0xf8000),
  1098. rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0x003e0));
  1099. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1100. "[IQK]S0, i = %x, 0x9[19:16] = 0x%x,0x9[09:06] = 0x%x\n", i,
  1101. rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0xf0000),
  1102. rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0x003c0));
  1103. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1104. "[IQK]S0, i = %x, 0x58 = %x\n", i,
  1105. rtw89_read_rf(rtwdev, RF_PATH_A, RR_TXMO, RFREG_MASK));
  1106. }
  1107. return fail;
  1108. }
  1109. static void _iqk_txk_setting(struct rtw89_dev *rtwdev, u8 path)
  1110. {
  1111. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  1112. switch (iqk_info->iqk_band[path]) {
  1113. case RTW89_BAND_2G:
  1114. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]RTW89_BAND_2G\n");
  1115. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_txk_2ghz_defs_tbl);
  1116. break;
  1117. case RTW89_BAND_5G:
  1118. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]RTW89_BAND_5G\n");
  1119. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_txk_5ghz_defs_tbl);
  1120. break;
  1121. default:
  1122. break;
  1123. }
  1124. }
  1125. #define IQK_LOK_RETRY 1
  1126. static void _iqk_by_path(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  1127. u8 path)
  1128. {
  1129. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  1130. bool lok_is_fail;
  1131. u8 i;
  1132. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1133. for (i = 0; i < IQK_LOK_RETRY; i++) {
  1134. _iqk_txk_setting(rtwdev, path);
  1135. if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
  1136. lok_is_fail = _iqk_2g_lok(rtwdev, phy_idx, path);
  1137. else
  1138. lok_is_fail = _iqk_5g_lok(rtwdev, phy_idx, path);
  1139. if (!lok_is_fail)
  1140. break;
  1141. }
  1142. if (iqk_info->is_nbiqk) {
  1143. if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
  1144. iqk_info->iqk_tx_fail[0][path] =
  1145. _iqk_2g_nbtxk(rtwdev, phy_idx, path);
  1146. else
  1147. iqk_info->iqk_tx_fail[0][path] =
  1148. _iqk_5g_nbtxk(rtwdev, phy_idx, path);
  1149. } else {
  1150. if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
  1151. iqk_info->iqk_tx_fail[0][path] =
  1152. _txk_2g_group_sel(rtwdev, phy_idx, path);
  1153. else
  1154. iqk_info->iqk_tx_fail[0][path] =
  1155. _txk_5g_group_sel(rtwdev, phy_idx, path);
  1156. }
  1157. _iqk_rxclk_setting(rtwdev, path);
  1158. _iqk_rxk_setting(rtwdev, path);
  1159. _adc_fifo_rst(rtwdev, phy_idx, path);
  1160. if (iqk_info->is_nbiqk) {
  1161. if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
  1162. iqk_info->iqk_rx_fail[0][path] =
  1163. _iqk_2g_nbrxk(rtwdev, phy_idx, path);
  1164. else
  1165. iqk_info->iqk_rx_fail[0][path] =
  1166. _iqk_5g_nbrxk(rtwdev, phy_idx, path);
  1167. } else {
  1168. if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
  1169. iqk_info->iqk_rx_fail[0][path] =
  1170. _rxk_2g_group_sel(rtwdev, phy_idx, path);
  1171. else
  1172. iqk_info->iqk_rx_fail[0][path] =
  1173. _rxk_5g_group_sel(rtwdev, phy_idx, path);
  1174. }
  1175. }
  1176. static void _rfk_backup_bb_reg(struct rtw89_dev *rtwdev,
  1177. u32 backup_bb_reg_val[])
  1178. {
  1179. u32 i;
  1180. for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
  1181. backup_bb_reg_val[i] =
  1182. rtw89_phy_read32_mask(rtwdev, rtw8851b_backup_bb_regs[i],
  1183. MASKDWORD);
  1184. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1185. "[RFK]backup bb reg : %x, value =%x\n",
  1186. rtw8851b_backup_bb_regs[i], backup_bb_reg_val[i]);
  1187. }
  1188. }
  1189. static void _rfk_backup_rf_reg(struct rtw89_dev *rtwdev,
  1190. u32 backup_rf_reg_val[], u8 rf_path)
  1191. {
  1192. u32 i;
  1193. for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
  1194. backup_rf_reg_val[i] =
  1195. rtw89_read_rf(rtwdev, rf_path,
  1196. rtw8851b_backup_rf_regs[i], RFREG_MASK);
  1197. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1198. "[RFK]backup rf S%d reg : %x, value =%x\n", rf_path,
  1199. rtw8851b_backup_rf_regs[i], backup_rf_reg_val[i]);
  1200. }
  1201. }
  1202. static void _rfk_restore_bb_reg(struct rtw89_dev *rtwdev,
  1203. const u32 backup_bb_reg_val[])
  1204. {
  1205. u32 i;
  1206. for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
  1207. rtw89_phy_write32_mask(rtwdev, rtw8851b_backup_bb_regs[i],
  1208. MASKDWORD, backup_bb_reg_val[i]);
  1209. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1210. "[RFK]restore bb reg : %x, value =%x\n",
  1211. rtw8851b_backup_bb_regs[i], backup_bb_reg_val[i]);
  1212. }
  1213. }
  1214. static void _rfk_restore_rf_reg(struct rtw89_dev *rtwdev,
  1215. const u32 backup_rf_reg_val[], u8 rf_path)
  1216. {
  1217. u32 i;
  1218. for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
  1219. rtw89_write_rf(rtwdev, rf_path, rtw8851b_backup_rf_regs[i],
  1220. RFREG_MASK, backup_rf_reg_val[i]);
  1221. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1222. "[RFK]restore rf S%d reg: %x, value =%x\n", rf_path,
  1223. rtw8851b_backup_rf_regs[i], backup_rf_reg_val[i]);
  1224. }
  1225. }
  1226. static void _iqk_get_ch_info(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1227. u8 path)
  1228. {
  1229. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  1230. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  1231. u8 idx = 0;
  1232. iqk_info->iqk_band[path] = chan->band_type;
  1233. iqk_info->iqk_bw[path] = chan->band_width;
  1234. iqk_info->iqk_ch[path] = chan->channel;
  1235. iqk_info->iqk_table_idx[path] = idx;
  1236. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d (PHY%d): / DBCC %s/ %s/ CH%d/ %s\n",
  1237. path, phy, rtwdev->dbcc_en ? "on" : "off",
  1238. iqk_info->iqk_band[path] == 0 ? "2G" :
  1239. iqk_info->iqk_band[path] == 1 ? "5G" : "6G",
  1240. iqk_info->iqk_ch[path],
  1241. iqk_info->iqk_bw[path] == 0 ? "20M" :
  1242. iqk_info->iqk_bw[path] == 1 ? "40M" : "80M");
  1243. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]times = 0x%x, ch =%x\n",
  1244. iqk_info->iqk_times, idx);
  1245. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, iqk_info->syn1to2= 0x%x\n",
  1246. path, iqk_info->syn1to2);
  1247. }
  1248. static void _iqk_start_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
  1249. u8 path)
  1250. {
  1251. _iqk_by_path(rtwdev, phy_idx, path);
  1252. }
  1253. static void _iqk_restore(struct rtw89_dev *rtwdev, u8 path)
  1254. {
  1255. bool fail;
  1256. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1257. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, 0x00001219);
  1258. fsleep(10);
  1259. fail = _iqk_check_cal(rtwdev, path);
  1260. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK] restore fail=%d\n", fail);
  1261. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTWE, RR_LUTWE_LOK, 0x0);
  1262. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTDBG, RR_LUTDBG_TIA, 0x0);
  1263. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
  1264. rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000000);
  1265. rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x80000000);
  1266. }
  1267. static void _iqk_afebb_restore(struct rtw89_dev *rtwdev,
  1268. enum rtw89_phy_idx phy_idx, u8 path)
  1269. {
  1270. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_afebb_restore_defs_tbl);
  1271. }
  1272. static void _iqk_preset(struct rtw89_dev *rtwdev, u8 path)
  1273. {
  1274. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1275. rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
  1276. rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000080);
  1277. rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x81ff010a);
  1278. }
  1279. static void _iqk_macbb_setting(struct rtw89_dev *rtwdev,
  1280. enum rtw89_phy_idx phy_idx, u8 path)
  1281. {
  1282. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1283. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_macbb_defs_tbl);
  1284. }
  1285. static void _iqk_init(struct rtw89_dev *rtwdev)
  1286. {
  1287. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  1288. u8 idx, path;
  1289. rtw89_phy_write32_mask(rtwdev, R_IQKINF, MASKDWORD, 0x0);
  1290. if (iqk_info->is_iqk_init)
  1291. return;
  1292. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
  1293. iqk_info->is_iqk_init = true;
  1294. iqk_info->is_nbiqk = false;
  1295. iqk_info->iqk_fft_en = false;
  1296. iqk_info->iqk_sram_en = false;
  1297. iqk_info->iqk_cfir_en = false;
  1298. iqk_info->iqk_xym_en = false;
  1299. iqk_info->iqk_times = 0x0;
  1300. for (idx = 0; idx < RTW89_IQK_CHS_NR; idx++) {
  1301. iqk_info->iqk_channel[idx] = 0x0;
  1302. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  1303. iqk_info->lok_cor_fail[idx][path] = false;
  1304. iqk_info->lok_fin_fail[idx][path] = false;
  1305. iqk_info->iqk_tx_fail[idx][path] = false;
  1306. iqk_info->iqk_rx_fail[idx][path] = false;
  1307. iqk_info->iqk_table_idx[path] = 0x0;
  1308. }
  1309. }
  1310. }
  1311. static void _doiqk(struct rtw89_dev *rtwdev, bool force,
  1312. enum rtw89_phy_idx phy_idx, u8 path)
  1313. {
  1314. struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
  1315. u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, RF_AB);
  1316. u32 backup_rf_val[RTW8851B_IQK_SS][BACKUP_RF_REGS_NR];
  1317. u32 backup_bb_val[BACKUP_BB_REGS_NR];
  1318. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK,
  1319. BTC_WRFK_ONESHOT_START);
  1320. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1321. "[IQK]==========IQK start!!!!!==========\n");
  1322. iqk_info->iqk_times++;
  1323. iqk_info->version = RTW8851B_IQK_VER;
  1324. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]Test Ver 0x%x\n", iqk_info->version);
  1325. _iqk_get_ch_info(rtwdev, phy_idx, path);
  1326. _rfk_backup_bb_reg(rtwdev, &backup_bb_val[0]);
  1327. _rfk_backup_rf_reg(rtwdev, &backup_rf_val[path][0], path);
  1328. _iqk_macbb_setting(rtwdev, phy_idx, path);
  1329. _iqk_preset(rtwdev, path);
  1330. _iqk_start_iqk(rtwdev, phy_idx, path);
  1331. _iqk_restore(rtwdev, path);
  1332. _iqk_afebb_restore(rtwdev, phy_idx, path);
  1333. _rfk_restore_bb_reg(rtwdev, &backup_bb_val[0]);
  1334. _rfk_restore_rf_reg(rtwdev, &backup_rf_val[path][0], path);
  1335. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK,
  1336. BTC_WRFK_ONESHOT_STOP);
  1337. }
  1338. static void _iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx, bool force)
  1339. {
  1340. _doiqk(rtwdev, force, phy_idx, RF_PATH_A);
  1341. }
  1342. static void _dpk_bkup_kip(struct rtw89_dev *rtwdev, const u32 *reg,
  1343. u32 reg_bkup[][DPK_KIP_REG_NUM_8851B], u8 path)
  1344. {
  1345. u8 i;
  1346. for (i = 0; i < DPK_KIP_REG_NUM_8851B; i++) {
  1347. reg_bkup[path][i] =
  1348. rtw89_phy_read32_mask(rtwdev, reg[i] + (path << 8), MASKDWORD);
  1349. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Backup 0x%x = %x\n",
  1350. reg[i] + (path << 8), reg_bkup[path][i]);
  1351. }
  1352. }
  1353. static void _dpk_bkup_rf(struct rtw89_dev *rtwdev, const u32 *rf_reg,
  1354. u32 rf_bkup[][DPK_RF_REG_NUM_8851B], u8 path)
  1355. {
  1356. u8 i;
  1357. for (i = 0; i < DPK_RF_REG_NUM_8851B; i++) {
  1358. rf_bkup[path][i] = rtw89_read_rf(rtwdev, path, rf_reg[i], RFREG_MASK);
  1359. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Backup RF S%d 0x%x = %x\n",
  1360. path, rf_reg[i], rf_bkup[path][i]);
  1361. }
  1362. }
  1363. static void _dpk_reload_kip(struct rtw89_dev *rtwdev, const u32 *reg,
  1364. u32 reg_bkup[][DPK_KIP_REG_NUM_8851B], u8 path)
  1365. {
  1366. u8 i;
  1367. for (i = 0; i < DPK_KIP_REG_NUM_8851B; i++) {
  1368. rtw89_phy_write32_mask(rtwdev, reg[i] + (path << 8), MASKDWORD,
  1369. reg_bkup[path][i]);
  1370. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1371. "[DPK] Reload 0x%x = %x\n",
  1372. reg[i] + (path << 8), reg_bkup[path][i]);
  1373. }
  1374. }
  1375. static void _dpk_reload_rf(struct rtw89_dev *rtwdev, const u32 *rf_reg,
  1376. u32 rf_bkup[][DPK_RF_REG_NUM_8851B], u8 path)
  1377. {
  1378. u8 i;
  1379. for (i = 0; i < DPK_RF_REG_NUM_8851B; i++) {
  1380. rtw89_write_rf(rtwdev, path, rf_reg[i], RFREG_MASK, rf_bkup[path][i]);
  1381. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1382. "[DPK] Reload RF S%d 0x%x = %x\n", path,
  1383. rf_reg[i], rf_bkup[path][i]);
  1384. }
  1385. }
  1386. static void _dpk_one_shot(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1387. enum rtw89_rf_path path, enum dpk_id id)
  1388. {
  1389. u16 dpk_cmd;
  1390. u32 val;
  1391. int ret;
  1392. dpk_cmd = ((id << 8) | (0x19 + path * 0x12));
  1393. rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, dpk_cmd);
  1394. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
  1395. 10, 20000, false,
  1396. rtwdev, 0xbff8, MASKBYTE0);
  1397. if (ret)
  1398. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] one-shot 1 timeout\n");
  1399. udelay(1);
  1400. ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x8000,
  1401. 1, 2000, false,
  1402. rtwdev, R_RPT_COM, MASKLWORD);
  1403. if (ret)
  1404. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] one-shot 2 timeout\n");
  1405. rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, MASKBYTE0, 0x0);
  1406. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1407. "[DPK] one-shot for %s = 0x%04x\n",
  1408. id == 0x28 ? "KIP_PRESET" :
  1409. id == 0x29 ? "DPK_TXAGC" :
  1410. id == 0x2a ? "DPK_RXAGC" :
  1411. id == 0x2b ? "SYNC" :
  1412. id == 0x2c ? "GAIN_LOSS" :
  1413. id == 0x2d ? "MDPK_IDL" :
  1414. id == 0x2f ? "DPK_GAIN_NORM" :
  1415. id == 0x31 ? "KIP_RESTORE" :
  1416. id == 0x6 ? "LBK_RXIQK" : "Unknown id",
  1417. dpk_cmd);
  1418. }
  1419. static void _dpk_onoff(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
  1420. bool off)
  1421. {
  1422. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1423. u8 kidx = dpk->cur_idx[path];
  1424. u8 off_reverse = off ? 0 : 1;
  1425. u8 val;
  1426. val = dpk->is_dpk_enable * off_reverse * dpk->bp[path][kidx].path_ok;
  1427. rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
  1428. 0xf0000000, val);
  1429. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] DPK %s !!!\n", path,
  1430. kidx, val == 0 ? "disable" : "enable");
  1431. }
  1432. static void _dpk_init(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  1433. {
  1434. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1435. u8 kidx = dpk->cur_idx[path];
  1436. dpk->bp[path][kidx].path_ok = 0;
  1437. }
  1438. static void _dpk_information(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1439. enum rtw89_rf_path path)
  1440. {
  1441. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  1442. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1443. u8 kidx = dpk->cur_idx[path];
  1444. dpk->bp[path][kidx].band = chan->band_type;
  1445. dpk->bp[path][kidx].ch = chan->band_width;
  1446. dpk->bp[path][kidx].bw = chan->channel;
  1447. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1448. "[DPK] S%d[%d] (PHY%d): TSSI %s/ DBCC %s/ %s/ CH%d/ %s\n",
  1449. path, dpk->cur_idx[path], phy,
  1450. rtwdev->is_tssi_mode[path] ? "on" : "off",
  1451. rtwdev->dbcc_en ? "on" : "off",
  1452. dpk->bp[path][kidx].band == 0 ? "2G" :
  1453. dpk->bp[path][kidx].band == 1 ? "5G" : "6G",
  1454. dpk->bp[path][kidx].ch,
  1455. dpk->bp[path][kidx].bw == 0 ? "20M" :
  1456. dpk->bp[path][kidx].bw == 1 ? "40M" :
  1457. dpk->bp[path][kidx].bw == 2 ? "80M" : "160M");
  1458. }
  1459. static void _dpk_rxagc_onoff(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
  1460. bool turn_on)
  1461. {
  1462. if (path == RF_PATH_A)
  1463. rtw89_phy_write32_mask(rtwdev, R_P0_AGC_CTL, B_P0_AGC_EN, turn_on);
  1464. else
  1465. rtw89_phy_write32_mask(rtwdev, R_P1_AGC_CTL, B_P1_AGC_EN, turn_on);
  1466. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d RXAGC is %s\n", path,
  1467. turn_on ? "turn_on" : "turn_off");
  1468. }
  1469. static void _dpk_bb_afe_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  1470. {
  1471. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(16 + path), 0x1);
  1472. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(20 + path), 0x0);
  1473. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(24 + path), 0x1);
  1474. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(28 + path), 0x0);
  1475. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0xd801dffd);
  1476. rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_bb_afe_defs_tbl);
  1477. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(20 + path), 0x1);
  1478. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(28 + path), 0x1);
  1479. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE setting\n", path);
  1480. }
  1481. static void _dpk_bb_afe_restore(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  1482. {
  1483. rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x0);
  1484. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(16 + path), 0x1);
  1485. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(20 + path), 0x0);
  1486. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(24 + path), 0x1);
  1487. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(28 + path), 0x0);
  1488. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0x00000000);
  1489. rtw89_phy_write32_mask(rtwdev, R_P0_RXCK + (path << 13), B_P0_TXCK_ALL, 0x00);
  1490. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(16 + path), 0x0);
  1491. rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(24 + path), 0x0);
  1492. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE restore\n", path);
  1493. }
  1494. static void _dpk_tssi_pause(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
  1495. bool is_pause)
  1496. {
  1497. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
  1498. B_P0_TSSI_TRK_EN, is_pause);
  1499. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d TSSI %s\n", path,
  1500. is_pause ? "pause" : "resume");
  1501. }
  1502. static void _dpk_tpg_sel(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
  1503. {
  1504. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1505. if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80) {
  1506. rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x0);
  1507. rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xffe0fa00);
  1508. } else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40) {
  1509. rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x2);
  1510. rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xff4009e0);
  1511. } else {
  1512. rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x1);
  1513. rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xf9f007d0);
  1514. }
  1515. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] TPG Select for %s\n",
  1516. dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80 ? "80M" :
  1517. dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40 ? "40M" : "20M");
  1518. }
  1519. static void _dpk_txpwr_bb_force(struct rtw89_dev *rtwdev,
  1520. enum rtw89_rf_path path, bool force)
  1521. {
  1522. rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13), B_TXPWRB_ON, force);
  1523. rtw89_phy_write32_mask(rtwdev, R_TXPWRB_H + (path << 13), B_TXPWRB_RDY, force);
  1524. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d txpwr_bb_force %s\n",
  1525. path, force ? "on" : "off");
  1526. }
  1527. static void _dpk_kip_pwr_clk_onoff(struct rtw89_dev *rtwdev, bool turn_on)
  1528. {
  1529. if (turn_on) {
  1530. rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000080);
  1531. rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x807f030a);
  1532. } else {
  1533. rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000000);
  1534. rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x80000000);
  1535. rtw89_phy_write32_mask(rtwdev, R_DPK_WR, BIT(18), 0x1);
  1536. }
  1537. }
  1538. static void _dpk_kip_control_rfc(struct rtw89_dev *rtwdev,
  1539. enum rtw89_rf_path path, bool ctrl_by_kip)
  1540. {
  1541. rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13),
  1542. B_IQK_RFC_ON, ctrl_by_kip);
  1543. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] RFC is controlled by %s\n",
  1544. ctrl_by_kip ? "KIP" : "BB");
  1545. }
  1546. static void _dpk_kip_preset(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1547. enum rtw89_rf_path path, u8 kidx)
  1548. {
  1549. rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
  1550. rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
  1551. rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
  1552. B_DPD_SEL, 0x01);
  1553. _dpk_kip_control_rfc(rtwdev, path, true);
  1554. _dpk_one_shot(rtwdev, phy, path, D_KIP_PRESET);
  1555. }
  1556. static void _dpk_kip_restore(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1557. enum rtw89_rf_path path)
  1558. {
  1559. _dpk_one_shot(rtwdev, phy, path, D_KIP_RESTORE);
  1560. _dpk_kip_control_rfc(rtwdev, path, false);
  1561. _dpk_txpwr_bb_force(rtwdev, path, false);
  1562. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d restore KIP\n", path);
  1563. }
  1564. static void _dpk_kset_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  1565. {
  1566. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1567. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8), B_KIP_RPT_SEL, 0x10);
  1568. dpk->cur_k_set =
  1569. rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_KSET) - 1;
  1570. }
  1571. static void _dpk_para_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
  1572. {
  1573. static const u32 reg[RTW89_DPK_BKUP_NUM][DPK_KSET_NUM] = {
  1574. {0x8190, 0x8194, 0x8198, 0x81a4},
  1575. {0x81a8, 0x81c4, 0x81c8, 0x81e8}
  1576. };
  1577. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1578. u8 cur_k_set = dpk->cur_k_set;
  1579. u32 para;
  1580. if (cur_k_set >= DPK_KSET_NUM) {
  1581. rtw89_warn(rtwdev, "DPK cur_k_set = %d\n", cur_k_set);
  1582. cur_k_set = 2;
  1583. }
  1584. para = rtw89_phy_read32_mask(rtwdev, reg[kidx][cur_k_set] + (path << 8),
  1585. MASKDWORD);
  1586. dpk->bp[path][kidx].txagc_dpk = (para >> 10) & 0x3f;
  1587. dpk->bp[path][kidx].ther_dpk = (para >> 26) & 0x3f;
  1588. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1589. "[DPK] thermal/ txagc_RF (K%d) = 0x%x/ 0x%x\n",
  1590. dpk->cur_k_set, dpk->bp[path][kidx].ther_dpk,
  1591. dpk->bp[path][kidx].txagc_dpk);
  1592. }
  1593. static bool _dpk_sync_check(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
  1594. {
  1595. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1596. u8 corr_val, corr_idx, rxbb;
  1597. u16 dc_i, dc_q;
  1598. u8 rxbb_ov;
  1599. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
  1600. corr_idx = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORI);
  1601. corr_val = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORV);
  1602. dpk->corr_idx[path][kidx] = corr_idx;
  1603. dpk->corr_val[path][kidx] = corr_val;
  1604. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x9);
  1605. dc_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
  1606. dc_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCQ);
  1607. dc_i = abs(sign_extend32(dc_i, 11));
  1608. dc_q = abs(sign_extend32(dc_q, 11));
  1609. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1610. "[DPK] S%d Corr_idx/ Corr_val /DC I/Q, = %d / %d / %d / %d\n",
  1611. path, corr_idx, corr_val, dc_i, dc_q);
  1612. dpk->dc_i[path][kidx] = dc_i;
  1613. dpk->dc_q[path][kidx] = dc_q;
  1614. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x8);
  1615. rxbb = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXBB);
  1616. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x31);
  1617. rxbb_ov = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXOV);
  1618. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1619. "[DPK] S%d RXBB/ RXAGC_done /RXBB_ovlmt = %d / %d / %d\n",
  1620. path, rxbb,
  1621. rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DONE),
  1622. rxbb_ov);
  1623. if (dc_i > 200 || dc_q > 200 || corr_val < 170)
  1624. return true;
  1625. else
  1626. return false;
  1627. }
  1628. static void _dpk_kip_set_txagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1629. enum rtw89_rf_path path, u8 dbm,
  1630. bool set_from_bb)
  1631. {
  1632. if (set_from_bb) {
  1633. dbm = clamp_t(u8, dbm, 7, 24);
  1634. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1635. "[DPK] set S%d txagc to %ddBm\n", path, dbm);
  1636. rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13),
  1637. B_TXPWRB_VAL, dbm << 2);
  1638. }
  1639. _dpk_one_shot(rtwdev, phy, path, D_TXAGC);
  1640. _dpk_kset_query(rtwdev, path);
  1641. }
  1642. static bool _dpk_kip_set_rxagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1643. enum rtw89_rf_path path, u8 kidx)
  1644. {
  1645. _dpk_kip_control_rfc(rtwdev, path, false);
  1646. rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
  1647. rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
  1648. _dpk_kip_control_rfc(rtwdev, path, true);
  1649. _dpk_one_shot(rtwdev, phy, path, D_RXAGC);
  1650. return _dpk_sync_check(rtwdev, path, kidx);
  1651. }
  1652. static void _dpk_lbk_rxiqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1653. enum rtw89_rf_path path)
  1654. {
  1655. u32 rf_11, reg_81cc;
  1656. u8 cur_rxbb;
  1657. rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
  1658. rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x1);
  1659. _dpk_kip_control_rfc(rtwdev, path, false);
  1660. cur_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_RXB);
  1661. rf_11 = rtw89_read_rf(rtwdev, path, RR_TXIG, RFREG_MASK);
  1662. reg_81cc = rtw89_phy_read32_mask(rtwdev, R_KIP_IQP + (path << 8),
  1663. B_KIP_IQP_SW);
  1664. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
  1665. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x3);
  1666. rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0xd);
  1667. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RXB, 0x1f);
  1668. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_IQSW, 0x12);
  1669. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, 0x3);
  1670. _dpk_kip_control_rfc(rtwdev, path, true);
  1671. rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, MASKDWORD, 0x00250025);
  1672. _dpk_one_shot(rtwdev, phy, path, LBK_RXIQK);
  1673. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d LBK RXIQC = 0x%x\n", path,
  1674. rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD));
  1675. _dpk_kip_control_rfc(rtwdev, path, false);
  1676. rtw89_write_rf(rtwdev, path, RR_TXIG, RFREG_MASK, rf_11);
  1677. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RXB, cur_rxbb);
  1678. rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, reg_81cc);
  1679. rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x0);
  1680. rtw89_phy_write32_mask(rtwdev, R_KPATH_CFG, B_KPATH_CFG_ED, 0x0);
  1681. rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_DI, 0x1);
  1682. _dpk_kip_control_rfc(rtwdev, path, true);
  1683. }
  1684. static void _dpk_rf_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
  1685. {
  1686. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1687. if (dpk->bp[path][kidx].band == RTW89_BAND_2G) {
  1688. rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK, 0x50521);
  1689. rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
  1690. rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTC, 0x0);
  1691. rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTR, 0x7);
  1692. } else {
  1693. rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
  1694. 0x50521 | BIT(rtwdev->dbcc_en));
  1695. rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
  1696. rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RAA2_SATT, 0x3);
  1697. }
  1698. rtw89_write_rf(rtwdev, path, RR_RCKD, RR_RCKD_BW, 0x1);
  1699. rtw89_write_rf(rtwdev, path, RR_BTC, RR_BTC_TXBB, dpk->bp[path][kidx].bw + 1);
  1700. rtw89_write_rf(rtwdev, path, RR_BTC, RR_BTC_RXBB, 0x0);
  1701. rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_EBW, 0x0);
  1702. }
  1703. static void _dpk_bypass_rxiqc(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  1704. {
  1705. rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
  1706. rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD, 0x40000002);
  1707. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Bypass RXIQC\n");
  1708. }
  1709. static u16 _dpk_dgain_read(struct rtw89_dev *rtwdev)
  1710. {
  1711. u16 dgain;
  1712. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
  1713. dgain = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
  1714. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] DGain = 0x%x\n", dgain);
  1715. return dgain;
  1716. }
  1717. static u8 _dpk_gainloss_read(struct rtw89_dev *rtwdev)
  1718. {
  1719. u8 result;
  1720. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x6);
  1721. rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x1);
  1722. result = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_GL);
  1723. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] tmp GL = %d\n", result);
  1724. return result;
  1725. }
  1726. static u8 _dpk_gainloss(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1727. enum rtw89_rf_path path, u8 kidx)
  1728. {
  1729. _dpk_one_shot(rtwdev, phy, path, D_GAIN_LOSS);
  1730. _dpk_kip_set_txagc(rtwdev, phy, path, 0xff, false);
  1731. rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A1, 0xf078);
  1732. rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A0, 0x0);
  1733. return _dpk_gainloss_read(rtwdev);
  1734. }
  1735. static u8 _dpk_pas_read(struct rtw89_dev *rtwdev, u8 is_check)
  1736. {
  1737. u32 val1_i = 0, val1_q = 0, val2_i = 0, val2_q = 0;
  1738. u32 val1_sqrt_sum, val2_sqrt_sum;
  1739. u8 i;
  1740. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, MASKBYTE2, 0x06);
  1741. rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x0);
  1742. rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE2, 0x08);
  1743. if (is_check) {
  1744. rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x00);
  1745. val1_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
  1746. val1_i = abs(sign_extend32(val1_i, 11));
  1747. val1_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
  1748. val1_q = abs(sign_extend32(val1_q, 11));
  1749. rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x1f);
  1750. val2_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
  1751. val2_i = abs(sign_extend32(val2_i, 11));
  1752. val2_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
  1753. val2_q = abs(sign_extend32(val2_q, 11));
  1754. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] PAS_delta = 0x%x\n",
  1755. phy_div(val1_i * val1_i + val1_q * val1_q,
  1756. val2_i * val2_i + val2_q * val2_q));
  1757. } else {
  1758. for (i = 0; i < 32; i++) {
  1759. rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, i);
  1760. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1761. "[DPK] PAS_Read[%02d]= 0x%08x\n", i,
  1762. rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD));
  1763. }
  1764. }
  1765. val1_sqrt_sum = val1_i * val1_i + val1_q * val1_q;
  1766. val2_sqrt_sum = val2_i * val2_i + val2_q * val2_q;
  1767. if (val1_sqrt_sum < val2_sqrt_sum)
  1768. return 2;
  1769. else if (val1_sqrt_sum >= val2_sqrt_sum * 8 / 5)
  1770. return 1;
  1771. else
  1772. return 0;
  1773. }
  1774. static u8 _dpk_agc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1775. enum rtw89_rf_path path, u8 kidx, u8 init_xdbm, u8 loss_only)
  1776. {
  1777. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1778. u8 tmp_dbm = init_xdbm, tmp_gl_idx = 0;
  1779. u8 step = DPK_AGC_STEP_SYNC_DGAIN;
  1780. u8 goout = 0, agc_cnt = 0;
  1781. bool is_fail = false;
  1782. int limit = 200;
  1783. u8 tmp_rxbb;
  1784. u16 dgain;
  1785. do {
  1786. switch (step) {
  1787. case DPK_AGC_STEP_SYNC_DGAIN:
  1788. is_fail = _dpk_kip_set_rxagc(rtwdev, phy, path, kidx);
  1789. if (is_fail) {
  1790. goout = 1;
  1791. break;
  1792. }
  1793. dgain = _dpk_dgain_read(rtwdev);
  1794. if (dgain > 0x5fc || dgain < 0x556) {
  1795. _dpk_one_shot(rtwdev, phy, path, D_SYNC);
  1796. dgain = _dpk_dgain_read(rtwdev);
  1797. }
  1798. if (agc_cnt == 0) {
  1799. if (dpk->bp[path][kidx].band == RTW89_BAND_2G)
  1800. _dpk_bypass_rxiqc(rtwdev, path);
  1801. else
  1802. _dpk_lbk_rxiqk(rtwdev, phy, path);
  1803. }
  1804. step = DPK_AGC_STEP_GAIN_LOSS_IDX;
  1805. break;
  1806. case DPK_AGC_STEP_GAIN_LOSS_IDX:
  1807. tmp_gl_idx = _dpk_gainloss(rtwdev, phy, path, kidx);
  1808. if (_dpk_pas_read(rtwdev, true) == 2 && tmp_gl_idx > 0)
  1809. step = DPK_AGC_STEP_GL_LT_CRITERION;
  1810. else if ((tmp_gl_idx == 0 && _dpk_pas_read(rtwdev, true) == 1) ||
  1811. tmp_gl_idx >= 7)
  1812. step = DPK_AGC_STEP_GL_GT_CRITERION;
  1813. else if (tmp_gl_idx == 0)
  1814. step = DPK_AGC_STEP_GL_LT_CRITERION;
  1815. else
  1816. step = DPK_AGC_STEP_SET_TX_GAIN;
  1817. break;
  1818. case DPK_AGC_STEP_GL_GT_CRITERION:
  1819. if (tmp_dbm <= 7) {
  1820. goout = 1;
  1821. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1822. "[DPK] Txagc@lower bound!!\n");
  1823. } else {
  1824. tmp_dbm = max_t(u8, tmp_dbm - 3, 7);
  1825. _dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
  1826. }
  1827. step = DPK_AGC_STEP_SYNC_DGAIN;
  1828. agc_cnt++;
  1829. break;
  1830. case DPK_AGC_STEP_GL_LT_CRITERION:
  1831. if (tmp_dbm >= 24) {
  1832. goout = 1;
  1833. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1834. "[DPK] Txagc@upper bound!!\n");
  1835. } else {
  1836. tmp_dbm = min_t(u8, tmp_dbm + 2, 24);
  1837. _dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
  1838. }
  1839. step = DPK_AGC_STEP_SYNC_DGAIN;
  1840. agc_cnt++;
  1841. break;
  1842. case DPK_AGC_STEP_SET_TX_GAIN:
  1843. _dpk_kip_control_rfc(rtwdev, path, false);
  1844. tmp_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_RXB);
  1845. tmp_rxbb = min_t(u8, tmp_rxbb + tmp_gl_idx, 0x1f);
  1846. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RXB, tmp_rxbb);
  1847. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1848. "[DPK] Adjust RXBB (%+d) = 0x%x\n",
  1849. tmp_gl_idx, tmp_rxbb);
  1850. _dpk_kip_control_rfc(rtwdev, path, true);
  1851. goout = 1;
  1852. break;
  1853. default:
  1854. goout = 1;
  1855. break;
  1856. }
  1857. } while (!goout && agc_cnt < 6 && limit-- > 0);
  1858. return is_fail;
  1859. }
  1860. static void _dpk_set_mdpd_para(struct rtw89_dev *rtwdev, u8 order)
  1861. {
  1862. switch (order) {
  1863. case 0: /* (5,3,1) */
  1864. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x0);
  1865. rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x2);
  1866. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x4);
  1867. rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x1);
  1868. break;
  1869. case 1: /* (5,3,0) */
  1870. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x1);
  1871. rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x1);
  1872. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x0);
  1873. rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x0);
  1874. break;
  1875. case 2: /* (5,0,0) */
  1876. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x2);
  1877. rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x0);
  1878. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x0);
  1879. rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x0);
  1880. break;
  1881. case 3: /* (7,3,1) */
  1882. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x3);
  1883. rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x3);
  1884. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x4);
  1885. rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x1);
  1886. break;
  1887. default:
  1888. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  1889. "[DPK] Wrong MDPD order!!(0x%x)\n", order);
  1890. break;
  1891. }
  1892. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Set %s for IDL\n",
  1893. order == 0x0 ? "(5,3,1)" :
  1894. order == 0x1 ? "(5,3,0)" :
  1895. order == 0x2 ? "(5,0,0)" : "(7,3,1)");
  1896. }
  1897. static void _dpk_idl_mpa(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1898. enum rtw89_rf_path path, u8 kidx)
  1899. {
  1900. rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_MA, 0x1);
  1901. if (rtw89_phy_read32_mask(rtwdev, R_IDL_MPA, B_IDL_MD500) == 0x1)
  1902. _dpk_set_mdpd_para(rtwdev, 0x2);
  1903. else if (rtw89_phy_read32_mask(rtwdev, R_IDL_MPA, B_IDL_MD530) == 0x1)
  1904. _dpk_set_mdpd_para(rtwdev, 0x1);
  1905. else
  1906. _dpk_set_mdpd_para(rtwdev, 0x0);
  1907. rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL, 0x0);
  1908. fsleep(1000);
  1909. _dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
  1910. }
  1911. static u8 _dpk_order_convert(struct rtw89_dev *rtwdev)
  1912. {
  1913. u32 order;
  1914. u8 val;
  1915. order = rtw89_phy_read32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP);
  1916. switch (order) {
  1917. case 0: /* (5,3,1) */
  1918. val = 0x6;
  1919. break;
  1920. case 1: /* (5,3,0) */
  1921. val = 0x2;
  1922. break;
  1923. case 2: /* (5,0,0) */
  1924. val = 0x0;
  1925. break;
  1926. default:
  1927. val = 0xff;
  1928. break;
  1929. }
  1930. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] convert MDPD order to 0x%x\n", val);
  1931. return val;
  1932. }
  1933. static void _dpk_gain_normalize(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1934. enum rtw89_rf_path path, u8 kidx, bool is_execute)
  1935. {
  1936. static const u32 reg[RTW89_DPK_BKUP_NUM][DPK_KSET_NUM] = {
  1937. {0x8190, 0x8194, 0x8198, 0x81a4},
  1938. {0x81a8, 0x81c4, 0x81c8, 0x81e8}
  1939. };
  1940. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1941. u8 cur_k_set = dpk->cur_k_set;
  1942. if (cur_k_set >= DPK_KSET_NUM) {
  1943. rtw89_warn(rtwdev, "DPK cur_k_set = %d\n", cur_k_set);
  1944. cur_k_set = 2;
  1945. }
  1946. if (is_execute) {
  1947. rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8),
  1948. B_DPK_GN_AG, 0x200);
  1949. rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8),
  1950. B_DPK_GN_EN, 0x3);
  1951. _dpk_one_shot(rtwdev, phy, path, D_GAIN_NORM);
  1952. } else {
  1953. rtw89_phy_write32_mask(rtwdev, reg[kidx][cur_k_set] + (path << 8),
  1954. 0x0000007F, 0x5b);
  1955. }
  1956. dpk->bp[path][kidx].gs =
  1957. rtw89_phy_read32_mask(rtwdev, reg[kidx][cur_k_set] + (path << 8),
  1958. 0x0000007F);
  1959. }
  1960. static void _dpk_on(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1961. enum rtw89_rf_path path, u8 kidx)
  1962. {
  1963. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1964. rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x1);
  1965. rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x0);
  1966. rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
  1967. B_DPD_ORDER, _dpk_order_convert(rtwdev));
  1968. dpk->bp[path][kidx].path_ok =
  1969. dpk->bp[path][kidx].path_ok | BIT(dpk->cur_k_set);
  1970. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] path_ok = 0x%x\n",
  1971. path, kidx, dpk->bp[path][kidx].path_ok);
  1972. rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
  1973. B_DPD_MEN, dpk->bp[path][kidx].path_ok);
  1974. _dpk_gain_normalize(rtwdev, phy, path, kidx, false);
  1975. }
  1976. static bool _dpk_main(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  1977. enum rtw89_rf_path path)
  1978. {
  1979. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  1980. u8 kidx = dpk->cur_idx[path];
  1981. u8 init_xdbm = 17;
  1982. bool is_fail;
  1983. if (dpk->bp[path][kidx].band != RTW89_BAND_2G)
  1984. init_xdbm = 15;
  1985. _dpk_kip_control_rfc(rtwdev, path, false);
  1986. _rfk_rf_direct_cntrl(rtwdev, path, false);
  1987. rtw89_write_rf(rtwdev, path, RR_BBDC, RFREG_MASK, 0x03ffd);
  1988. _dpk_rf_setting(rtwdev, path, kidx);
  1989. _set_rx_dck(rtwdev, path, RF_DPK);
  1990. _dpk_kip_pwr_clk_onoff(rtwdev, true);
  1991. _dpk_kip_preset(rtwdev, phy, path, kidx);
  1992. _dpk_txpwr_bb_force(rtwdev, path, true);
  1993. _dpk_kip_set_txagc(rtwdev, phy, path, init_xdbm, true);
  1994. _dpk_tpg_sel(rtwdev, path, kidx);
  1995. is_fail = _dpk_agc(rtwdev, phy, path, kidx, init_xdbm, false);
  1996. if (is_fail)
  1997. goto _error;
  1998. _dpk_idl_mpa(rtwdev, phy, path, kidx);
  1999. _dpk_para_query(rtwdev, path, kidx);
  2000. _dpk_on(rtwdev, phy, path, kidx);
  2001. _error:
  2002. _dpk_kip_control_rfc(rtwdev, path, false);
  2003. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RF_RX);
  2004. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d]_K%d %s\n", path, kidx,
  2005. dpk->cur_k_set, is_fail ? "need Check" : "is Success");
  2006. return is_fail;
  2007. }
  2008. static void _dpk_cal_select(struct rtw89_dev *rtwdev, bool force,
  2009. enum rtw89_phy_idx phy, u8 kpath)
  2010. {
  2011. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  2012. u32 kip_bkup[RF_PATH_NUM_8851B][DPK_KIP_REG_NUM_8851B] = {};
  2013. u32 rf_bkup[RF_PATH_NUM_8851B][DPK_RF_REG_NUM_8851B] = {};
  2014. bool is_fail;
  2015. u8 path;
  2016. for (path = 0; path < RF_PATH_NUM_8851B; path++)
  2017. dpk->cur_idx[path] = 0;
  2018. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  2019. if (!(kpath & BIT(path)))
  2020. continue;
  2021. _dpk_bkup_kip(rtwdev, dpk_kip_reg, kip_bkup, path);
  2022. _dpk_bkup_rf(rtwdev, dpk_rf_reg, rf_bkup, path);
  2023. _dpk_information(rtwdev, phy, path);
  2024. _dpk_init(rtwdev, path);
  2025. if (rtwdev->is_tssi_mode[path])
  2026. _dpk_tssi_pause(rtwdev, path, true);
  2027. }
  2028. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  2029. if (!(kpath & BIT(path)))
  2030. continue;
  2031. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2032. "[DPK] ========= S%d[%d] DPK Start =========\n",
  2033. path, dpk->cur_idx[path]);
  2034. _dpk_rxagc_onoff(rtwdev, path, false);
  2035. _rfk_drf_direct_cntrl(rtwdev, path, false);
  2036. _dpk_bb_afe_setting(rtwdev, path);
  2037. is_fail = _dpk_main(rtwdev, phy, path);
  2038. _dpk_onoff(rtwdev, path, is_fail);
  2039. }
  2040. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  2041. if (!(kpath & BIT(path)))
  2042. continue;
  2043. _dpk_kip_restore(rtwdev, phy, path);
  2044. _dpk_reload_kip(rtwdev, dpk_kip_reg, kip_bkup, path);
  2045. _dpk_reload_rf(rtwdev, dpk_rf_reg, rf_bkup, path);
  2046. _dpk_bb_afe_restore(rtwdev, path);
  2047. _dpk_rxagc_onoff(rtwdev, path, true);
  2048. if (rtwdev->is_tssi_mode[path])
  2049. _dpk_tssi_pause(rtwdev, path, false);
  2050. }
  2051. _dpk_kip_pwr_clk_onoff(rtwdev, false);
  2052. }
  2053. static void _dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool force)
  2054. {
  2055. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2056. "[DPK] ****** 8851B DPK Start (Ver: 0x%x, Cv: %d) ******\n",
  2057. DPK_VER_8851B, rtwdev->hal.cv);
  2058. _dpk_cal_select(rtwdev, force, phy, _kpath(rtwdev, phy));
  2059. }
  2060. static void _dpk_track(struct rtw89_dev *rtwdev)
  2061. {
  2062. struct rtw89_dpk_info *dpk = &rtwdev->dpk;
  2063. s8 txagc_bb, txagc_bb_tp, txagc_ofst;
  2064. s16 pwsf_tssi_ofst;
  2065. s8 delta_ther = 0;
  2066. u8 path, kidx;
  2067. u8 txagc_rf;
  2068. u8 cur_ther;
  2069. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  2070. kidx = dpk->cur_idx[path];
  2071. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2072. "[DPK_TRK] ================[S%d[%d] (CH %d)]================\n",
  2073. path, kidx, dpk->bp[path][kidx].ch);
  2074. txagc_rf = rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13),
  2075. B_TXAGC_RF);
  2076. txagc_bb = rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13),
  2077. MASKBYTE2);
  2078. txagc_bb_tp = rtw89_phy_read32_mask(rtwdev, R_TXAGC_BTP + (path << 13),
  2079. B_TXAGC_BTP);
  2080. rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8),
  2081. B_KIP_RPT_SEL, 0xf);
  2082. cur_ther = rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8),
  2083. B_RPT_PER_TH);
  2084. txagc_ofst = rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8),
  2085. B_RPT_PER_OF);
  2086. pwsf_tssi_ofst = rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8),
  2087. B_RPT_PER_TSSI);
  2088. pwsf_tssi_ofst = sign_extend32(pwsf_tssi_ofst, 12);
  2089. delta_ther = cur_ther - dpk->bp[path][kidx].ther_dpk;
  2090. delta_ther = delta_ther * 2 / 3;
  2091. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2092. "[DPK_TRK] extra delta_ther = %d (0x%x / 0x%x@k)\n",
  2093. delta_ther, cur_ther, dpk->bp[path][kidx].ther_dpk);
  2094. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2095. "[DPK_TRK] delta_txagc = %d (0x%x / 0x%x@k)\n",
  2096. txagc_rf - dpk->bp[path][kidx].txagc_dpk,
  2097. txagc_rf, dpk->bp[path][kidx].txagc_dpk);
  2098. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2099. "[DPK_TRK] txagc_offset / pwsf_tssi_ofst = 0x%x / %+d\n",
  2100. txagc_ofst, pwsf_tssi_ofst);
  2101. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2102. "[DPK_TRK] txagc_bb_tp / txagc_bb = 0x%x / 0x%x\n",
  2103. txagc_bb_tp, txagc_bb);
  2104. if (rtw89_phy_read32_mask(rtwdev, R_IDL_MPA, B_IDL_DN) == 0x0 &&
  2105. txagc_rf != 0) {
  2106. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2107. "[DPK_TRK] New pwsf = 0x%x\n", 0x78 - delta_ther);
  2108. rtw89_phy_write32_mask(rtwdev,
  2109. R_DPD_BND + (path << 8) + (kidx << 2),
  2110. 0x07FC0000, 0x78 - delta_ther);
  2111. }
  2112. }
  2113. }
  2114. static void _rck(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  2115. {
  2116. u32 rf_reg5;
  2117. u32 rck_val;
  2118. u32 val;
  2119. int ret;
  2120. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] ====== S%d RCK ======\n", path);
  2121. rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
  2122. rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
  2123. rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
  2124. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RF0x00 = 0x%05x\n",
  2125. rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
  2126. /* RCK trigger */
  2127. rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, 0x00240);
  2128. ret = read_poll_timeout_atomic(rtw89_read_rf, val, val, 2, 30,
  2129. false, rtwdev, path, RR_RCKS, BIT(3));
  2130. rck_val = rtw89_read_rf(rtwdev, path, RR_RCKC, RR_RCKC_CA);
  2131. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] rck_val = 0x%x, ret = %d\n",
  2132. rck_val, ret);
  2133. rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, rck_val);
  2134. rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
  2135. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RF 0x1b = 0x%x\n",
  2136. rtw89_read_rf(rtwdev, path, RR_RCKC, RFREG_MASK));
  2137. }
  2138. static void _tssi_set_sys(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2139. enum rtw89_rf_path path)
  2140. {
  2141. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2142. enum rtw89_band band = chan->band_type;
  2143. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_sys_defs_tbl);
  2144. rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
  2145. &rtw8851b_tssi_sys_a_defs_2g_tbl,
  2146. &rtw8851b_tssi_sys_a_defs_5g_tbl);
  2147. }
  2148. static void _tssi_ini_txpwr_ctrl_bb(struct rtw89_dev *rtwdev,
  2149. enum rtw89_phy_idx phy,
  2150. enum rtw89_rf_path path)
  2151. {
  2152. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_init_txpwr_defs_a_tbl);
  2153. }
  2154. static void _tssi_ini_txpwr_ctrl_bb_he_tb(struct rtw89_dev *rtwdev,
  2155. enum rtw89_phy_idx phy,
  2156. enum rtw89_rf_path path)
  2157. {
  2158. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_init_txpwr_he_tb_defs_a_tbl);
  2159. }
  2160. static void _tssi_set_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2161. enum rtw89_rf_path path)
  2162. {
  2163. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_dck_defs_a_tbl);
  2164. }
  2165. static void _tssi_set_tmeter_tbl(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2166. enum rtw89_rf_path path)
  2167. {
  2168. #define RTW8851B_TSSI_GET_VAL(ptr, idx) \
  2169. ({ \
  2170. s8 *__ptr = (ptr); \
  2171. u8 __idx = (idx), __i, __v; \
  2172. u32 __val = 0; \
  2173. for (__i = 0; __i < 4; __i++) { \
  2174. __v = (__ptr[__idx + __i]); \
  2175. __val |= (__v << (8 * __i)); \
  2176. } \
  2177. __val; \
  2178. })
  2179. struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
  2180. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2181. u8 ch = chan->channel;
  2182. u8 subband = chan->subband_type;
  2183. const s8 *thm_up_a = NULL;
  2184. const s8 *thm_down_a = NULL;
  2185. u8 thermal = 0xff;
  2186. s8 thm_ofst[64] = {0};
  2187. u32 tmp = 0;
  2188. u8 i, j;
  2189. switch (subband) {
  2190. default:
  2191. case RTW89_CH_2G:
  2192. thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_2ga_p;
  2193. thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_2ga_n;
  2194. break;
  2195. case RTW89_CH_5G_BAND_1:
  2196. thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_p[0];
  2197. thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_n[0];
  2198. break;
  2199. case RTW89_CH_5G_BAND_3:
  2200. thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_p[1];
  2201. thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_n[1];
  2202. break;
  2203. case RTW89_CH_5G_BAND_4:
  2204. thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_p[2];
  2205. thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_n[2];
  2206. break;
  2207. }
  2208. if (path == RF_PATH_A) {
  2209. thermal = tssi_info->thermal[RF_PATH_A];
  2210. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2211. "[TSSI] ch=%d thermal_pathA=0x%x\n", ch, thermal);
  2212. rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_DIS, 0x0);
  2213. rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_TRK, 0x1);
  2214. if (thermal == 0xff) {
  2215. rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER, 32);
  2216. rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL, 32);
  2217. for (i = 0; i < 64; i += 4) {
  2218. rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, 0x0);
  2219. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2220. "[TSSI] write 0x%x val=0x%08x\n",
  2221. R_P0_TSSI_BASE + i, 0x0);
  2222. }
  2223. } else {
  2224. rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER,
  2225. thermal);
  2226. rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL,
  2227. thermal);
  2228. i = 0;
  2229. for (j = 0; j < 32; j++)
  2230. thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
  2231. -thm_down_a[i++] :
  2232. -thm_down_a[DELTA_SWINGIDX_SIZE - 1];
  2233. i = 1;
  2234. for (j = 63; j >= 32; j--)
  2235. thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
  2236. thm_up_a[i++] :
  2237. thm_up_a[DELTA_SWINGIDX_SIZE - 1];
  2238. for (i = 0; i < 64; i += 4) {
  2239. tmp = RTW8851B_TSSI_GET_VAL(thm_ofst, i);
  2240. rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, tmp);
  2241. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2242. "[TSSI] write 0x%x val=0x%08x\n",
  2243. 0x5c00 + i, tmp);
  2244. }
  2245. }
  2246. rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x1);
  2247. rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x0);
  2248. }
  2249. #undef RTW8851B_TSSI_GET_VAL
  2250. }
  2251. static void _tssi_set_dac_gain_tbl(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2252. enum rtw89_rf_path path)
  2253. {
  2254. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_dac_gain_defs_a_tbl);
  2255. }
  2256. static void _tssi_slope_cal_org(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2257. enum rtw89_rf_path path)
  2258. {
  2259. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2260. enum rtw89_band band = chan->band_type;
  2261. rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
  2262. &rtw8851b_tssi_slope_a_defs_2g_tbl,
  2263. &rtw8851b_tssi_slope_a_defs_5g_tbl);
  2264. }
  2265. static void _tssi_alignment_default(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2266. enum rtw89_rf_path path, bool all)
  2267. {
  2268. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2269. enum rtw89_band band = chan->band_type;
  2270. rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
  2271. &rtw8851b_tssi_align_a_2g_defs_tbl,
  2272. &rtw8851b_tssi_align_a_5g_defs_tbl);
  2273. }
  2274. static void _tssi_set_tssi_slope(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2275. enum rtw89_rf_path path)
  2276. {
  2277. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_slope_defs_a_tbl);
  2278. }
  2279. static void _tssi_set_tssi_track(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2280. enum rtw89_rf_path path)
  2281. {
  2282. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_track_defs_a_tbl);
  2283. }
  2284. static void _tssi_set_txagc_offset_mv_avg(struct rtw89_dev *rtwdev,
  2285. enum rtw89_phy_idx phy,
  2286. enum rtw89_rf_path path)
  2287. {
  2288. rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_mv_avg_defs_a_tbl);
  2289. }
  2290. static void _tssi_enable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
  2291. {
  2292. _tssi_set_tssi_track(rtwdev, phy, RF_PATH_A);
  2293. _tssi_set_txagc_offset_mv_avg(rtwdev, phy, RF_PATH_A);
  2294. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_MV_AVG, B_P0_TSSI_MV_CLR, 0x0);
  2295. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_AVG, B_P0_TSSI_EN, 0x0);
  2296. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_AVG, B_P0_TSSI_EN, 0x1);
  2297. rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXGA_V1, RR_TXGA_V1_TRK_EN, 0x1);
  2298. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
  2299. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_RFC, 0x3);
  2300. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT, 0xc0);
  2301. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
  2302. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
  2303. rtwdev->is_tssi_mode[RF_PATH_A] = true;
  2304. }
  2305. static void _tssi_disable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
  2306. {
  2307. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_AVG, B_P0_TSSI_EN, 0x0);
  2308. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
  2309. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
  2310. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
  2311. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_MV_AVG, B_P0_TSSI_MV_CLR, 0x1);
  2312. rtwdev->is_tssi_mode[RF_PATH_A] = false;
  2313. }
  2314. static u32 _tssi_get_cck_group(struct rtw89_dev *rtwdev, u8 ch)
  2315. {
  2316. switch (ch) {
  2317. case 1 ... 2:
  2318. return 0;
  2319. case 3 ... 5:
  2320. return 1;
  2321. case 6 ... 8:
  2322. return 2;
  2323. case 9 ... 11:
  2324. return 3;
  2325. case 12 ... 13:
  2326. return 4;
  2327. case 14:
  2328. return 5;
  2329. }
  2330. return 0;
  2331. }
  2332. #define TSSI_EXTRA_GROUP_BIT (BIT(31))
  2333. #define TSSI_EXTRA_GROUP(idx) (TSSI_EXTRA_GROUP_BIT | (idx))
  2334. #define IS_TSSI_EXTRA_GROUP(group) ((group) & TSSI_EXTRA_GROUP_BIT)
  2335. #define TSSI_EXTRA_GET_GROUP_IDX1(group) ((group) & ~TSSI_EXTRA_GROUP_BIT)
  2336. #define TSSI_EXTRA_GET_GROUP_IDX2(group) (TSSI_EXTRA_GET_GROUP_IDX1(group) + 1)
  2337. static u32 _tssi_get_ofdm_group(struct rtw89_dev *rtwdev, u8 ch)
  2338. {
  2339. switch (ch) {
  2340. case 1 ... 2:
  2341. return 0;
  2342. case 3 ... 5:
  2343. return 1;
  2344. case 6 ... 8:
  2345. return 2;
  2346. case 9 ... 11:
  2347. return 3;
  2348. case 12 ... 14:
  2349. return 4;
  2350. case 36 ... 40:
  2351. return 5;
  2352. case 41 ... 43:
  2353. return TSSI_EXTRA_GROUP(5);
  2354. case 44 ... 48:
  2355. return 6;
  2356. case 49 ... 51:
  2357. return TSSI_EXTRA_GROUP(6);
  2358. case 52 ... 56:
  2359. return 7;
  2360. case 57 ... 59:
  2361. return TSSI_EXTRA_GROUP(7);
  2362. case 60 ... 64:
  2363. return 8;
  2364. case 100 ... 104:
  2365. return 9;
  2366. case 105 ... 107:
  2367. return TSSI_EXTRA_GROUP(9);
  2368. case 108 ... 112:
  2369. return 10;
  2370. case 113 ... 115:
  2371. return TSSI_EXTRA_GROUP(10);
  2372. case 116 ... 120:
  2373. return 11;
  2374. case 121 ... 123:
  2375. return TSSI_EXTRA_GROUP(11);
  2376. case 124 ... 128:
  2377. return 12;
  2378. case 129 ... 131:
  2379. return TSSI_EXTRA_GROUP(12);
  2380. case 132 ... 136:
  2381. return 13;
  2382. case 137 ... 139:
  2383. return TSSI_EXTRA_GROUP(13);
  2384. case 140 ... 144:
  2385. return 14;
  2386. case 149 ... 153:
  2387. return 15;
  2388. case 154 ... 156:
  2389. return TSSI_EXTRA_GROUP(15);
  2390. case 157 ... 161:
  2391. return 16;
  2392. case 162 ... 164:
  2393. return TSSI_EXTRA_GROUP(16);
  2394. case 165 ... 169:
  2395. return 17;
  2396. case 170 ... 172:
  2397. return TSSI_EXTRA_GROUP(17);
  2398. case 173 ... 177:
  2399. return 18;
  2400. }
  2401. return 0;
  2402. }
  2403. static u32 _tssi_get_trim_group(struct rtw89_dev *rtwdev, u8 ch)
  2404. {
  2405. switch (ch) {
  2406. case 1 ... 8:
  2407. return 0;
  2408. case 9 ... 14:
  2409. return 1;
  2410. case 36 ... 48:
  2411. return 2;
  2412. case 52 ... 64:
  2413. return 3;
  2414. case 100 ... 112:
  2415. return 4;
  2416. case 116 ... 128:
  2417. return 5;
  2418. case 132 ... 144:
  2419. return 6;
  2420. case 149 ... 177:
  2421. return 7;
  2422. }
  2423. return 0;
  2424. }
  2425. static s8 _tssi_get_ofdm_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2426. enum rtw89_rf_path path)
  2427. {
  2428. struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
  2429. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2430. u32 gidx, gidx_1st, gidx_2nd;
  2431. u8 ch = chan->channel;
  2432. s8 de_1st;
  2433. s8 de_2nd;
  2434. s8 val;
  2435. gidx = _tssi_get_ofdm_group(rtwdev, ch);
  2436. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2437. "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n", path, gidx);
  2438. if (IS_TSSI_EXTRA_GROUP(gidx)) {
  2439. gidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(gidx);
  2440. gidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(gidx);
  2441. de_1st = tssi_info->tssi_mcs[path][gidx_1st];
  2442. de_2nd = tssi_info->tssi_mcs[path][gidx_2nd];
  2443. val = (de_1st + de_2nd) / 2;
  2444. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2445. "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n",
  2446. path, val, de_1st, de_2nd);
  2447. } else {
  2448. val = tssi_info->tssi_mcs[path][gidx];
  2449. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2450. "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val);
  2451. }
  2452. return val;
  2453. }
  2454. static s8 _tssi_get_ofdm_trim_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2455. enum rtw89_rf_path path)
  2456. {
  2457. struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
  2458. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2459. u32 tgidx, tgidx_1st, tgidx_2nd;
  2460. u8 ch = chan->channel;
  2461. s8 tde_1st;
  2462. s8 tde_2nd;
  2463. s8 val;
  2464. tgidx = _tssi_get_trim_group(rtwdev, ch);
  2465. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2466. "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n",
  2467. path, tgidx);
  2468. if (IS_TSSI_EXTRA_GROUP(tgidx)) {
  2469. tgidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(tgidx);
  2470. tgidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(tgidx);
  2471. tde_1st = tssi_info->tssi_trim[path][tgidx_1st];
  2472. tde_2nd = tssi_info->tssi_trim[path][tgidx_2nd];
  2473. val = (tde_1st + tde_2nd) / 2;
  2474. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2475. "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n",
  2476. path, val, tde_1st, tde_2nd);
  2477. } else {
  2478. val = tssi_info->tssi_trim[path][tgidx];
  2479. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2480. "[TSSI][TRIM]: path=%d mcs trim_de=%d\n",
  2481. path, val);
  2482. }
  2483. return val;
  2484. }
  2485. static void _tssi_set_efuse_to_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
  2486. {
  2487. struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
  2488. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2489. u8 ch = chan->channel;
  2490. u8 gidx;
  2491. s8 ofdm_de;
  2492. s8 trim_de;
  2493. s32 val;
  2494. u32 i;
  2495. rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI][TRIM]: phy=%d ch=%d\n",
  2496. phy, ch);
  2497. for (i = RF_PATH_A; i < RTW8851B_TSSI_PATH_NR; i++) {
  2498. gidx = _tssi_get_cck_group(rtwdev, ch);
  2499. trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
  2500. val = tssi_info->tssi_cck[i][gidx] + trim_de;
  2501. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2502. "[TSSI][TRIM]: path=%d cck[%d]=0x%x trim=0x%x\n",
  2503. i, gidx, tssi_info->tssi_cck[i][gidx], trim_de);
  2504. rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_long[i], _TSSI_DE_MASK, val);
  2505. rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_short[i], _TSSI_DE_MASK, val);
  2506. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2507. "[TSSI] Set TSSI CCK DE 0x%x[21:12]=0x%x\n",
  2508. _tssi_de_cck_long[i],
  2509. rtw89_phy_read32_mask(rtwdev, _tssi_de_cck_long[i],
  2510. _TSSI_DE_MASK));
  2511. ofdm_de = _tssi_get_ofdm_de(rtwdev, phy, i);
  2512. trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
  2513. val = ofdm_de + trim_de;
  2514. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2515. "[TSSI][TRIM]: path=%d mcs=0x%x trim=0x%x\n",
  2516. i, ofdm_de, trim_de);
  2517. rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_20m[i], _TSSI_DE_MASK, val);
  2518. rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_40m[i], _TSSI_DE_MASK, val);
  2519. rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m[i], _TSSI_DE_MASK, val);
  2520. rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m_80m[i], _TSSI_DE_MASK, val);
  2521. rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_5m[i], _TSSI_DE_MASK, val);
  2522. rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_10m[i], _TSSI_DE_MASK, val);
  2523. rtw89_debug(rtwdev, RTW89_DBG_TSSI,
  2524. "[TSSI] Set TSSI MCS DE 0x%x[21:12]=0x%x\n",
  2525. _tssi_de_mcs_20m[i],
  2526. rtw89_phy_read32_mask(rtwdev, _tssi_de_mcs_20m[i],
  2527. _TSSI_DE_MASK));
  2528. }
  2529. }
  2530. static void _tssi_alimentk_dump_result(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
  2531. {
  2532. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2533. "[TSSI PA K]\n0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n"
  2534. "0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n",
  2535. R_TSSI_PA_K1 + (path << 13),
  2536. rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K1 + (path << 13), MASKDWORD),
  2537. R_TSSI_PA_K2 + (path << 13),
  2538. rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K2 + (path << 13), MASKDWORD),
  2539. R_P0_TSSI_ALIM1 + (path << 13),
  2540. rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM1 + (path << 13), MASKDWORD),
  2541. R_P0_TSSI_ALIM3 + (path << 13),
  2542. rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM3 + (path << 13), MASKDWORD),
  2543. R_TSSI_PA_K5 + (path << 13),
  2544. rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K5 + (path << 13), MASKDWORD),
  2545. R_P0_TSSI_ALIM2 + (path << 13),
  2546. rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM2 + (path << 13), MASKDWORD),
  2547. R_P0_TSSI_ALIM4 + (path << 13),
  2548. rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM4 + (path << 13), MASKDWORD),
  2549. R_TSSI_PA_K8 + (path << 13),
  2550. rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K8 + (path << 13), MASKDWORD));
  2551. }
  2552. static void _tssi_alimentk_done(struct rtw89_dev *rtwdev,
  2553. enum rtw89_phy_idx phy, enum rtw89_rf_path path)
  2554. {
  2555. struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
  2556. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2557. u8 channel = chan->channel;
  2558. u8 band;
  2559. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2560. "======>%s phy=%d path=%d\n", __func__, phy, path);
  2561. if (channel >= 1 && channel <= 14)
  2562. band = TSSI_ALIMK_2G;
  2563. else if (channel >= 36 && channel <= 64)
  2564. band = TSSI_ALIMK_5GL;
  2565. else if (channel >= 100 && channel <= 144)
  2566. band = TSSI_ALIMK_5GM;
  2567. else if (channel >= 149 && channel <= 177)
  2568. band = TSSI_ALIMK_5GH;
  2569. else
  2570. band = TSSI_ALIMK_2G;
  2571. if (tssi_info->alignment_done[path][band]) {
  2572. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM1 + (path << 13), MASKDWORD,
  2573. tssi_info->alignment_value[path][band][0]);
  2574. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM3 + (path << 13), MASKDWORD,
  2575. tssi_info->alignment_value[path][band][1]);
  2576. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM2 + (path << 13), MASKDWORD,
  2577. tssi_info->alignment_value[path][band][2]);
  2578. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM4 + (path << 13), MASKDWORD,
  2579. tssi_info->alignment_value[path][band][3]);
  2580. }
  2581. _tssi_alimentk_dump_result(rtwdev, path);
  2582. }
  2583. static void rtw8851b_by_rate_dpd(struct rtw89_dev *rtwdev)
  2584. {
  2585. rtw89_write32_mask(rtwdev, R_AX_PWR_SWING_OTHER_CTRL0,
  2586. B_AX_CFIR_BY_RATE_OFF_MASK, 0x21861);
  2587. }
  2588. void rtw8851b_dpk_init(struct rtw89_dev *rtwdev)
  2589. {
  2590. rtw8851b_by_rate_dpd(rtwdev);
  2591. }
  2592. void rtw8851b_aack(struct rtw89_dev *rtwdev)
  2593. {
  2594. u32 tmp05, tmpd3, ib[4];
  2595. u32 tmp;
  2596. int ret;
  2597. int rek;
  2598. int i;
  2599. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]DO AACK\n");
  2600. tmp05 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK);
  2601. tmpd3 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK);
  2602. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_MASK, 0x3);
  2603. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, 0x0);
  2604. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_ST, 0x0);
  2605. for (rek = 0; rek < 4; rek++) {
  2606. rtw89_write_rf(rtwdev, RF_PATH_A, RR_AACK, RFREG_MASK, 0x8201e);
  2607. rtw89_write_rf(rtwdev, RF_PATH_A, RR_AACK, RFREG_MASK, 0x8201f);
  2608. fsleep(100);
  2609. ret = read_poll_timeout_atomic(rtw89_read_rf, tmp, tmp,
  2610. 1, 1000, false,
  2611. rtwdev, RF_PATH_A, 0xd0, BIT(16));
  2612. if (ret)
  2613. rtw89_warn(rtwdev, "[LCK]AACK timeout\n");
  2614. rtw89_write_rf(rtwdev, RF_PATH_A, RR_VCI, RR_VCI_ON, 0x1);
  2615. for (i = 0; i < 4; i++) {
  2616. rtw89_write_rf(rtwdev, RF_PATH_A, RR_VCO, RR_VCO_SEL, i);
  2617. ib[i] = rtw89_read_rf(rtwdev, RF_PATH_A, RR_IBD, RR_IBD_VAL);
  2618. }
  2619. rtw89_write_rf(rtwdev, RF_PATH_A, RR_VCI, RR_VCI_ON, 0x0);
  2620. if (ib[0] != 0 && ib[1] != 0 && ib[2] != 0 && ib[3] != 0)
  2621. break;
  2622. }
  2623. if (rek != 0)
  2624. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]AACK rek = %d\n", rek);
  2625. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, tmp05);
  2626. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK, tmpd3);
  2627. }
  2628. static void _lck_keep_thermal(struct rtw89_dev *rtwdev)
  2629. {
  2630. struct rtw89_lck_info *lck = &rtwdev->lck;
  2631. lck->thermal[RF_PATH_A] =
  2632. ewma_thermal_read(&rtwdev->phystat.avg_thermal[RF_PATH_A]);
  2633. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2634. "[LCK] path=%d thermal=0x%x", RF_PATH_A, lck->thermal[RF_PATH_A]);
  2635. }
  2636. static void rtw8851b_lck(struct rtw89_dev *rtwdev)
  2637. {
  2638. u32 tmp05, tmp18, tmpd3;
  2639. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]DO LCK\n");
  2640. tmp05 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK);
  2641. tmp18 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
  2642. tmpd3 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK);
  2643. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_MASK, 0x3);
  2644. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, 0x0);
  2645. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
  2646. _set_ch(rtwdev, tmp18);
  2647. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK, tmpd3);
  2648. rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, tmp05);
  2649. _lck_keep_thermal(rtwdev);
  2650. }
  2651. #define RTW8851B_LCK_TH 8
  2652. void rtw8851b_lck_track(struct rtw89_dev *rtwdev)
  2653. {
  2654. struct rtw89_lck_info *lck = &rtwdev->lck;
  2655. u8 cur_thermal;
  2656. int delta;
  2657. cur_thermal =
  2658. ewma_thermal_read(&rtwdev->phystat.avg_thermal[RF_PATH_A]);
  2659. delta = abs((int)cur_thermal - lck->thermal[RF_PATH_A]);
  2660. rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
  2661. "[LCK] path=%d current thermal=0x%x delta=0x%x\n",
  2662. RF_PATH_A, cur_thermal, delta);
  2663. if (delta >= RTW8851B_LCK_TH) {
  2664. rtw8851b_aack(rtwdev);
  2665. rtw8851b_lck(rtwdev);
  2666. }
  2667. }
  2668. void rtw8851b_lck_init(struct rtw89_dev *rtwdev)
  2669. {
  2670. _lck_keep_thermal(rtwdev);
  2671. }
  2672. void rtw8851b_rck(struct rtw89_dev *rtwdev)
  2673. {
  2674. _rck(rtwdev, RF_PATH_A);
  2675. }
  2676. void rtw8851b_dack(struct rtw89_dev *rtwdev)
  2677. {
  2678. _dac_cal(rtwdev, false);
  2679. }
  2680. void rtw8851b_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
  2681. {
  2682. u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
  2683. u32 tx_en;
  2684. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_START);
  2685. rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
  2686. _wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
  2687. _iqk_init(rtwdev);
  2688. _iqk(rtwdev, phy_idx, false);
  2689. rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
  2690. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_STOP);
  2691. }
  2692. void rtw8851b_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
  2693. {
  2694. u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
  2695. u32 tx_en;
  2696. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_START);
  2697. rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
  2698. _wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
  2699. _rx_dck(rtwdev, phy_idx, false);
  2700. rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
  2701. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_STOP);
  2702. }
  2703. void rtw8851b_dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
  2704. {
  2705. u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
  2706. u32 tx_en;
  2707. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_START);
  2708. rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
  2709. _wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
  2710. rtwdev->dpk.is_dpk_enable = true;
  2711. rtwdev->dpk.is_dpk_reload_en = false;
  2712. _dpk(rtwdev, phy_idx, false);
  2713. rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
  2714. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_STOP);
  2715. }
  2716. void rtw8851b_dpk_track(struct rtw89_dev *rtwdev)
  2717. {
  2718. _dpk_track(rtwdev);
  2719. }
  2720. void rtw8851b_tssi(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool hwtx_en)
  2721. {
  2722. u8 phy_map = rtw89_btc_phymap(rtwdev, phy, RF_A);
  2723. u8 i;
  2724. rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI] %s: phy=%d\n", __func__, phy);
  2725. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_START);
  2726. _tssi_disable(rtwdev, phy);
  2727. for (i = RF_PATH_A; i < RF_PATH_NUM_8851B; i++) {
  2728. _tssi_set_sys(rtwdev, phy, i);
  2729. _tssi_ini_txpwr_ctrl_bb(rtwdev, phy, i);
  2730. _tssi_ini_txpwr_ctrl_bb_he_tb(rtwdev, phy, i);
  2731. _tssi_set_dck(rtwdev, phy, i);
  2732. _tssi_set_tmeter_tbl(rtwdev, phy, i);
  2733. _tssi_set_dac_gain_tbl(rtwdev, phy, i);
  2734. _tssi_slope_cal_org(rtwdev, phy, i);
  2735. _tssi_alignment_default(rtwdev, phy, i, true);
  2736. _tssi_set_tssi_slope(rtwdev, phy, i);
  2737. }
  2738. _tssi_enable(rtwdev, phy);
  2739. _tssi_set_efuse_to_de(rtwdev, phy);
  2740. rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_STOP);
  2741. }
  2742. void rtw8851b_tssi_scan(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
  2743. {
  2744. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2745. u8 channel = chan->channel;
  2746. u32 i;
  2747. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2748. "======>%s phy=%d channel=%d\n", __func__, phy, channel);
  2749. _tssi_disable(rtwdev, phy);
  2750. for (i = RF_PATH_A; i < RF_PATH_NUM_8851B; i++) {
  2751. _tssi_set_sys(rtwdev, phy, i);
  2752. _tssi_set_tmeter_tbl(rtwdev, phy, i);
  2753. _tssi_slope_cal_org(rtwdev, phy, i);
  2754. _tssi_alignment_default(rtwdev, phy, i, true);
  2755. }
  2756. _tssi_enable(rtwdev, phy);
  2757. _tssi_set_efuse_to_de(rtwdev, phy);
  2758. }
  2759. static void rtw8851b_tssi_default_txagc(struct rtw89_dev *rtwdev,
  2760. enum rtw89_phy_idx phy, bool enable)
  2761. {
  2762. const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
  2763. u8 channel = chan->channel;
  2764. rtw89_debug(rtwdev, RTW89_DBG_RFK, "======> %s ch=%d\n",
  2765. __func__, channel);
  2766. if (enable)
  2767. return;
  2768. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2769. "======>%s 1 SCAN_END Set 0x5818[7:0]=0x%x\n",
  2770. __func__,
  2771. rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT));
  2772. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT, 0xc0);
  2773. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
  2774. rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
  2775. _tssi_alimentk_done(rtwdev, phy, RF_PATH_A);
  2776. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2777. "======>%s 2 SCAN_END Set 0x5818[7:0]=0x%x\n",
  2778. __func__,
  2779. rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT));
  2780. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2781. "======> %s SCAN_END\n", __func__);
  2782. }
  2783. void rtw8851b_wifi_scan_notify(struct rtw89_dev *rtwdev, bool scan_start,
  2784. enum rtw89_phy_idx phy_idx)
  2785. {
  2786. if (scan_start)
  2787. rtw8851b_tssi_default_txagc(rtwdev, phy_idx, true);
  2788. else
  2789. rtw8851b_tssi_default_txagc(rtwdev, phy_idx, false);
  2790. }
  2791. static void _bw_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
  2792. enum rtw89_bandwidth bw, bool dav)
  2793. {
  2794. u32 reg18_addr = dav ? RR_CFGCH : RR_CFGCH_V1;
  2795. u32 rf_reg18;
  2796. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===> %s\n", __func__);
  2797. rf_reg18 = rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK);
  2798. if (rf_reg18 == INV_RF_DATA) {
  2799. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2800. "[RFK]Invalid RF_0x18 for Path-%d\n", path);
  2801. return;
  2802. }
  2803. rf_reg18 &= ~RR_CFGCH_BW;
  2804. switch (bw) {
  2805. case RTW89_CHANNEL_WIDTH_5:
  2806. case RTW89_CHANNEL_WIDTH_10:
  2807. case RTW89_CHANNEL_WIDTH_20:
  2808. rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_20M);
  2809. break;
  2810. case RTW89_CHANNEL_WIDTH_40:
  2811. rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_40M);
  2812. break;
  2813. case RTW89_CHANNEL_WIDTH_80:
  2814. rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_80M);
  2815. break;
  2816. default:
  2817. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]Fail to set CH\n");
  2818. }
  2819. rf_reg18 &= ~(RR_CFGCH_POW_LCK | RR_CFGCH_TRX_AH | RR_CFGCH_BCN |
  2820. RR_CFGCH_BW2) & RFREG_MASK;
  2821. rf_reg18 |= RR_CFGCH_BW2;
  2822. rtw89_write_rf(rtwdev, path, reg18_addr, RFREG_MASK, rf_reg18);
  2823. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK] set %x at path%d, %x =0x%x\n",
  2824. bw, path, reg18_addr,
  2825. rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK));
  2826. }
  2827. static void _ctrl_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2828. enum rtw89_bandwidth bw)
  2829. {
  2830. _bw_setting(rtwdev, RF_PATH_A, bw, true);
  2831. _bw_setting(rtwdev, RF_PATH_A, bw, false);
  2832. }
  2833. static bool _set_s0_arfc18(struct rtw89_dev *rtwdev, u32 val)
  2834. {
  2835. u32 bak;
  2836. u32 tmp;
  2837. int ret;
  2838. bak = rtw89_read_rf(rtwdev, RF_PATH_A, RR_LDO, RFREG_MASK);
  2839. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LDO, RR_LDO_SEL, 0x1);
  2840. rtw89_write_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK, val);
  2841. ret = read_poll_timeout_atomic(rtw89_read_rf, tmp, tmp == 0, 1, 1000,
  2842. false, rtwdev, RF_PATH_A, RR_LPF, RR_LPF_BUSY);
  2843. if (ret)
  2844. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]LCK timeout\n");
  2845. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LDO, RFREG_MASK, bak);
  2846. return !!ret;
  2847. }
  2848. static void _lck_check(struct rtw89_dev *rtwdev)
  2849. {
  2850. u32 tmp;
  2851. if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RR_SYNFB_LK) == 0) {
  2852. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]SYN MMD reset\n");
  2853. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_EN, 0x1);
  2854. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_SYN, 0x0);
  2855. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_SYN, 0x1);
  2856. rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_EN, 0x0);
  2857. }
  2858. udelay(10);
  2859. if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RR_SYNFB_LK) == 0) {
  2860. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]re-set RF 0x18\n");
  2861. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
  2862. tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
  2863. _set_s0_arfc18(rtwdev, tmp);
  2864. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x0);
  2865. }
  2866. if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RR_SYNFB_LK) == 0) {
  2867. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]SYN off/on\n");
  2868. tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_POW, RFREG_MASK);
  2869. rtw89_write_rf(rtwdev, RF_PATH_A, RR_POW, RFREG_MASK, tmp);
  2870. tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_SX, RFREG_MASK);
  2871. rtw89_write_rf(rtwdev, RF_PATH_A, RR_SX, RFREG_MASK, tmp);
  2872. rtw89_write_rf(rtwdev, RF_PATH_A, RR_SYNLUT, RR_SYNLUT_MOD, 0x1);
  2873. rtw89_write_rf(rtwdev, RF_PATH_A, RR_POW, RR_POW_SYN, 0x0);
  2874. rtw89_write_rf(rtwdev, RF_PATH_A, RR_POW, RR_POW_SYN, 0x3);
  2875. rtw89_write_rf(rtwdev, RF_PATH_A, RR_SYNLUT, RR_SYNLUT_MOD, 0x0);
  2876. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
  2877. tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
  2878. _set_s0_arfc18(rtwdev, tmp);
  2879. rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x0);
  2880. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]0xb2=%x, 0xc5=%x\n",
  2881. rtw89_read_rf(rtwdev, RF_PATH_A, RR_VCO, RFREG_MASK),
  2882. rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RFREG_MASK));
  2883. }
  2884. }
  2885. static void _set_ch(struct rtw89_dev *rtwdev, u32 val)
  2886. {
  2887. bool timeout;
  2888. timeout = _set_s0_arfc18(rtwdev, val);
  2889. if (!timeout)
  2890. _lck_check(rtwdev);
  2891. }
  2892. static void _ch_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
  2893. u8 central_ch, bool dav)
  2894. {
  2895. u32 reg18_addr = dav ? RR_CFGCH : RR_CFGCH_V1;
  2896. bool is_2g_ch = central_ch <= 14;
  2897. u32 rf_reg18;
  2898. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===> %s\n", __func__);
  2899. rf_reg18 = rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK);
  2900. rf_reg18 &= ~(RR_CFGCH_BAND1 | RR_CFGCH_POW_LCK | RR_CFGCH_TRX_AH |
  2901. RR_CFGCH_BCN | RR_CFGCH_BAND0 | RR_CFGCH_CH);
  2902. rf_reg18 |= FIELD_PREP(RR_CFGCH_CH, central_ch);
  2903. if (!is_2g_ch)
  2904. rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_5G) |
  2905. FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_5G);
  2906. rf_reg18 &= ~(RR_CFGCH_POW_LCK | RR_CFGCH_TRX_AH | RR_CFGCH_BCN |
  2907. RR_CFGCH_BW2) & RFREG_MASK;
  2908. rf_reg18 |= RR_CFGCH_BW2;
  2909. if (path == RF_PATH_A && dav)
  2910. _set_ch(rtwdev, rf_reg18);
  2911. else
  2912. rtw89_write_rf(rtwdev, path, reg18_addr, RFREG_MASK, rf_reg18);
  2913. rtw89_write_rf(rtwdev, path, RR_LCKST, RR_LCKST_BIN, 0);
  2914. rtw89_write_rf(rtwdev, path, RR_LCKST, RR_LCKST_BIN, 1);
  2915. rtw89_debug(rtwdev, RTW89_DBG_RFK,
  2916. "[RFK]CH: %d for Path-%d, reg0x%x = 0x%x\n",
  2917. central_ch, path, reg18_addr,
  2918. rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK));
  2919. }
  2920. static void _ctrl_ch(struct rtw89_dev *rtwdev, u8 central_ch)
  2921. {
  2922. _ch_setting(rtwdev, RF_PATH_A, central_ch, true);
  2923. _ch_setting(rtwdev, RF_PATH_A, central_ch, false);
  2924. }
  2925. static void _set_rxbb_bw(struct rtw89_dev *rtwdev, enum rtw89_bandwidth bw,
  2926. enum rtw89_rf_path path)
  2927. {
  2928. rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x1);
  2929. rtw89_write_rf(rtwdev, path, RR_LUTWA, RR_LUTWA_M2, 0x12);
  2930. if (bw == RTW89_CHANNEL_WIDTH_20)
  2931. rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0x1b);
  2932. else if (bw == RTW89_CHANNEL_WIDTH_40)
  2933. rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0x13);
  2934. else if (bw == RTW89_CHANNEL_WIDTH_80)
  2935. rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0xb);
  2936. else
  2937. rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0x3);
  2938. rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK] set S%d RXBB BW 0x3F = 0x%x\n", path,
  2939. rtw89_read_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB));
  2940. rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x0);
  2941. }
  2942. static void _rxbb_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
  2943. enum rtw89_bandwidth bw)
  2944. {
  2945. u8 kpath, path;
  2946. kpath = _kpath(rtwdev, phy);
  2947. for (path = 0; path < RF_PATH_NUM_8851B; path++) {
  2948. if (!(kpath & BIT(path)))
  2949. continue;
  2950. _set_rxbb_bw(rtwdev, bw, path);
  2951. }
  2952. }
  2953. static void rtw8851b_ctrl_bw_ch(struct rtw89_dev *rtwdev,
  2954. enum rtw89_phy_idx phy, u8 central_ch,
  2955. enum rtw89_band band, enum rtw89_bandwidth bw)
  2956. {
  2957. _ctrl_ch(rtwdev, central_ch);
  2958. _ctrl_bw(rtwdev, phy, bw);
  2959. _rxbb_bw(rtwdev, phy, bw);
  2960. }
  2961. void rtw8851b_set_channel_rf(struct rtw89_dev *rtwdev,
  2962. const struct rtw89_chan *chan,
  2963. enum rtw89_phy_idx phy_idx)
  2964. {
  2965. rtw8851b_ctrl_bw_ch(rtwdev, phy_idx, chan->channel, chan->band_type,
  2966. chan->band_width);
  2967. }