rk3588_clk.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  1. /*
  2. * Copyright (c) 2024, Rockchip, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <drivers/delay_timer.h>
  9. #include <drivers/scmi.h>
  10. #include <lib/mmio.h>
  11. #include <platform_def.h>
  12. #include <plat_private.h>
  13. #include "rk3588_clk.h"
  14. #include <rockchip_sip_svc.h>
  15. #include <scmi_clock.h>
  16. #include <soc.h>
  17. enum pll_type_sel {
  18. PLL_SEL_AUTO, /* all plls (normal pll or pvtpll) */
  19. PLL_SEL_PVT,
  20. PLL_SEL_NOR,
  21. PLL_SEL_AUTO_NOR /* all normal plls (apll/gpll/npll) */
  22. };
  23. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  24. #define RK3588_CPUL_PVTPLL_CON0_L 0x40
  25. #define RK3588_CPUL_PVTPLL_CON0_H 0x44
  26. #define RK3588_CPUL_PVTPLL_CON1 0x48
  27. #define RK3588_CPUL_PVTPLL_CON2 0x4c
  28. #define RK3588_CPUB_PVTPLL_CON0_L 0x00
  29. #define RK3588_CPUB_PVTPLL_CON0_H 0x04
  30. #define RK3588_CPUB_PVTPLL_CON1 0x08
  31. #define RK3588_CPUB_PVTPLL_CON2 0x0c
  32. #define RK3588_DSU_PVTPLL_CON0_L 0x60
  33. #define RK3588_DSU_PVTPLL_CON0_H 0x64
  34. #define RK3588_DSU_PVTPLL_CON1 0x70
  35. #define RK3588_DSU_PVTPLL_CON2 0x74
  36. #define RK3588_GPU_PVTPLL_CON0_L 0x00
  37. #define RK3588_GPU_PVTPLL_CON0_H 0x04
  38. #define RK3588_GPU_PVTPLL_CON1 0x08
  39. #define RK3588_GPU_PVTPLL_CON2 0x0c
  40. #define RK3588_NPU_PVTPLL_CON0_L 0x0c
  41. #define RK3588_NPU_PVTPLL_CON0_H 0x10
  42. #define RK3588_NPU_PVTPLL_CON1 0x14
  43. #define RK3588_NPU_PVTPLL_CON2 0x18
  44. #define RK3588_PVTPLL_MAX_LENGTH 0x3f
  45. #define GPLL_RATE 1188000000
  46. #define CPLL_RATE 1500000000
  47. #define SPLL_RATE 702000000
  48. #define AUPLL_RATE 786431952
  49. #define NPLL_RATE 850000000
  50. #define MAX_RATE_TABLE 16
  51. #define CLKDIV_6BITS_SHF(div, shift) BITS_WITH_WMASK(div, 0x3fU, shift)
  52. #define CLKDIV_5BITS_SHF(div, shift) BITS_WITH_WMASK(div, 0x1fU, shift)
  53. #define CLKDIV_4BITS_SHF(div, shift) BITS_WITH_WMASK(div, 0xfU, shift)
  54. #define CLKDIV_3BITS_SHF(div, shift) BITS_WITH_WMASK(div, 0x7U, shift)
  55. #define CLKDIV_2BITS_SHF(div, shift) BITS_WITH_WMASK(div, 0x3U, shift)
  56. #define CLKDIV_1BITS_SHF(div, shift) BITS_WITH_WMASK(div, 0x1U, shift)
  57. #define CPU_PLL_PATH_SLOWMODE BITS_WITH_WMASK(0U, 0x3U, 0)
  58. #define CPU_PLL_PATH_NORMAL BITS_WITH_WMASK(1U, 0x3U, 0)
  59. #define CPU_PLL_PATH_DEEP_SLOW BITS_WITH_WMASK(2U, 0x3U, 0)
  60. #define CRU_PLL_POWER_DOWN BIT_WITH_WMSK(13)
  61. #define CRU_PLL_POWER_UP WMSK_BIT(13)
  62. /* core_i: from gpll or apll */
  63. #define CLK_CORE_I_SEL_APLL WMSK_BIT(6)
  64. #define CLK_CORE_I_SEL_GPLL BIT_WITH_WMSK(6)
  65. /* clk_core:
  66. * from normal pll(core_i: gpll or apll) path or direct pass from apll
  67. */
  68. /* cpul clk path */
  69. #define CPUL_CLK_PATH_NOR_XIN BITS_WITH_WMASK(0U, 0x3U, 14)
  70. #define CPUL_CLK_PATH_NOR_GPLL BITS_WITH_WMASK(1U, 0x3U, 14)
  71. #define CPUL_CLK_PATH_NOR_LPLL BITS_WITH_WMASK(2U, 0x3U, 14)
  72. #define CPUL_CLK_PATH_LPLL (BITS_WITH_WMASK(0U, 0x3U, 5) | \
  73. BITS_WITH_WMASK(0U, 0x3U, 12))
  74. #define CPUL_CLK_PATH_DIR_LPLL (BITS_WITH_WMASK(0x1, 0x3U, 5) | \
  75. BITS_WITH_WMASK(1U, 0x3U, 12))
  76. #define CPUL_CLK_PATH_PVTPLL (BITS_WITH_WMASK(0x2, 0x3U, 5) | \
  77. BITS_WITH_WMASK(2U, 0x3U, 12))
  78. #define CPUL_PVTPLL_PATH_DEEP_SLOW BITS_WITH_WMASK(0U, 0x1U, 14)
  79. #define CPUL_PVTPLL_PATH_PVTPLL BITS_WITH_WMASK(1U, 0x1U, 14)
  80. /* cpub01 clk path */
  81. #define CPUB01_CLK_PATH_NOR_XIN BITS_WITH_WMASK(0U, 0x3U, 6)
  82. #define CPUB01_CLK_PATH_NOR_GPLL BITS_WITH_WMASK(1U, 0x3U, 6)
  83. #define CPUB01_CLK_PATH_NOR_B0PLL BITS_WITH_WMASK(2U, 0x3U, 6)
  84. #define CPUB01_CLK_PATH_B0PLL BITS_WITH_WMASK(0U, 0x3U, 13)
  85. #define CPUB01_CLK_PATH_DIR_B0PLL BITS_WITH_WMASK(1U, 0x3U, 13)
  86. #define CPUB01_CLK_PATH_B0_PVTPLL BITS_WITH_WMASK(2U, 0x3U, 13)
  87. #define CPUB01_CLK_PATH_B1PLL BITS_WITH_WMASK(0U, 0x3U, 5)
  88. #define CPUB01_CLK_PATH_DIR_B1PLL BITS_WITH_WMASK(1U, 0x3U, 5)
  89. #define CPUB01_CLK_PATH_B1_PVTPLL BITS_WITH_WMASK(2U, 0x3U, 5)
  90. #define CPUB01_PVTPLL_PATH_DEEP_SLOW BITS_WITH_WMASK(0U, 0x1U, 2)
  91. #define CPUB01_PVTPLL_PATH_PVTPLL BITS_WITH_WMASK(1U, 0x1U, 2)
  92. #define CPUB_PCLK_PATH_100M BITS_WITH_WMASK(0U, 0x3U, 0)
  93. #define CPUB_PCLK_PATH_50M BITS_WITH_WMASK(1U, 0x3U, 0)
  94. #define CPUB_PCLK_PATH_24M BITS_WITH_WMASK(2U, 0x3U, 0)
  95. /* dsu clk path */
  96. #define SCLK_DSU_PATH_NOR_B0PLL BITS_WITH_WMASK(0U, 0x3U, 12)
  97. #define SCLK_DSU_PATH_NOR_B1PLL BITS_WITH_WMASK(1U, 0x3U, 12)
  98. #define SCLK_DSU_PATH_NOR_LPLL BITS_WITH_WMASK(2U, 0x3U, 12)
  99. #define SCLK_DSU_PATH_NOR_GPLL BITS_WITH_WMASK(3U, 0x3U, 12)
  100. #define DSU_PVTPLL_PATH_DEEP_SLOW BITS_WITH_WMASK(0U, 0x1U, 15)
  101. #define DSU_PVTPLL_PATH_PVTPLL BITS_WITH_WMASK(1U, 0x1U, 15)
  102. #define SCLK_DSU_PATH_NOR_PLL WMSK_BIT(0)
  103. #define SCLK_DSU_PATH_PVTPLL BIT_WITH_WMSK(0)
  104. /* npu clk path */
  105. #define NPU_CLK_PATH_NOR_GPLL BITS_WITH_WMASK(0U, 0x7U, 7)
  106. #define NPU_CLK_PATH_NOR_CPLL BITS_WITH_WMASK(1U, 0x7U, 7)
  107. #define NPU_CLK_PATH_NOR_AUPLL BITS_WITH_WMASK(2U, 0x7U, 7)
  108. #define NPU_CLK_PATH_NOR_NPLL BITS_WITH_WMASK(3U, 0x7U, 7)
  109. #define NPU_CLK_PATH_NOR_SPLL BITS_WITH_WMASK(4U, 0x7U, 7)
  110. #define NPU_CLK_PATH_NOR_PLL WMSK_BIT(0)
  111. #define NPU_CLK_PATH_PVTPLL BIT_WITH_WMSK(0)
  112. /* gpu clk path */
  113. #define GPU_CLK_PATH_NOR_GPLL BITS_WITH_WMASK(0U, 0x7U, 5)
  114. #define GPU_CLK_PATH_NOR_CPLL BITS_WITH_WMASK(1U, 0x7U, 5)
  115. #define GPU_CLK_PATH_NOR_AUPLL BITS_WITH_WMASK(2U, 0x7U, 5)
  116. #define GPU_CLK_PATH_NOR_NPLL BITS_WITH_WMASK(3U, 0x7U, 5)
  117. #define GPU_CLK_PATH_NOR_SPLL BITS_WITH_WMASK(4U, 0x7U, 5)
  118. #define GPU_CLK_PATH_NOR_PLL WMSK_BIT(14)
  119. #define GPU_CLK_PATH_PVTPLL BIT_WITH_WMSK(14)
  120. #define PVTPLL_NEED(type, length) (((type) == PLL_SEL_PVT || \
  121. (type) == PLL_SEL_AUTO) && \
  122. (length))
  123. struct pvtpll_table {
  124. unsigned int rate;
  125. uint32_t length;
  126. uint32_t ring_sel;
  127. };
  128. struct sys_clk_info_t {
  129. struct pvtpll_table *cpul_table;
  130. struct pvtpll_table *cpub01_table;
  131. struct pvtpll_table *cpub23_table;
  132. struct pvtpll_table *gpu_table;
  133. struct pvtpll_table *npu_table;
  134. unsigned int cpul_rate_count;
  135. unsigned int cpub01_rate_count;
  136. unsigned int cpub23_rate_count;
  137. unsigned int gpu_rate_count;
  138. unsigned int npu_rate_count;
  139. unsigned long cpul_rate;
  140. unsigned long dsu_rate;
  141. unsigned long cpub01_rate;
  142. unsigned long cpub23_rate;
  143. unsigned long gpu_rate;
  144. unsigned long npu_rate;
  145. };
  146. #define RK3588_SCMI_CLOCK(_id, _name, _data, _table, _cnt, _is_s) \
  147. { \
  148. .id = _id, \
  149. .name = _name, \
  150. .clk_ops = _data, \
  151. .rate_table = _table, \
  152. .rate_cnt = _cnt, \
  153. .is_security = _is_s, \
  154. }
  155. #define ROCKCHIP_PVTPLL(_rate, _sel, _len) \
  156. { \
  157. .rate = _rate##U, \
  158. .ring_sel = _sel, \
  159. .length = _len, \
  160. }
  161. static struct pvtpll_table rk3588_cpul_pvtpll_table[] = {
  162. /* rate_hz, ring_sel, length */
  163. ROCKCHIP_PVTPLL(1800000000, 1, 15),
  164. ROCKCHIP_PVTPLL(1704000000, 1, 15),
  165. ROCKCHIP_PVTPLL(1608000000, 1, 15),
  166. ROCKCHIP_PVTPLL(1416000000, 1, 15),
  167. ROCKCHIP_PVTPLL(1200000000, 1, 17),
  168. ROCKCHIP_PVTPLL(1008000000, 1, 22),
  169. ROCKCHIP_PVTPLL(816000000, 1, 32),
  170. ROCKCHIP_PVTPLL(600000000, 0, 0),
  171. ROCKCHIP_PVTPLL(408000000, 0, 0),
  172. { /* sentinel */ },
  173. };
  174. static struct pvtpll_table rk3588_cpub0_pvtpll_table[] = {
  175. /* rate_hz, ring_sel, length */
  176. ROCKCHIP_PVTPLL(2400000000, 1, 11),
  177. ROCKCHIP_PVTPLL(2352000000, 1, 11),
  178. ROCKCHIP_PVTPLL(2304000000, 1, 11),
  179. ROCKCHIP_PVTPLL(2256000000, 1, 11),
  180. ROCKCHIP_PVTPLL(2208000000, 1, 11),
  181. ROCKCHIP_PVTPLL(2112000000, 1, 11),
  182. ROCKCHIP_PVTPLL(2016000000, 1, 11),
  183. ROCKCHIP_PVTPLL(1800000000, 1, 11),
  184. ROCKCHIP_PVTPLL(1608000000, 1, 11),
  185. ROCKCHIP_PVTPLL(1416000000, 1, 13),
  186. ROCKCHIP_PVTPLL(1200000000, 1, 17),
  187. ROCKCHIP_PVTPLL(1008000000, 1, 23),
  188. ROCKCHIP_PVTPLL(816000000, 1, 33),
  189. ROCKCHIP_PVTPLL(600000000, 0, 0),
  190. ROCKCHIP_PVTPLL(408000000, 0, 0),
  191. { /* sentinel */ },
  192. };
  193. static struct
  194. pvtpll_table rk3588_cpub1_pvtpll_table[ARRAY_SIZE(rk3588_cpub0_pvtpll_table)] = { 0 };
  195. static struct pvtpll_table rk3588_gpu_pvtpll_table[] = {
  196. /* rate_hz, ring_sel, length */
  197. ROCKCHIP_PVTPLL(1000000000, 1, 12),
  198. ROCKCHIP_PVTPLL(900000000, 1, 12),
  199. ROCKCHIP_PVTPLL(800000000, 1, 12),
  200. ROCKCHIP_PVTPLL(700000000, 1, 13),
  201. ROCKCHIP_PVTPLL(600000000, 1, 17),
  202. ROCKCHIP_PVTPLL(500000000, 1, 25),
  203. ROCKCHIP_PVTPLL(400000000, 1, 38),
  204. ROCKCHIP_PVTPLL(300000000, 1, 55),
  205. ROCKCHIP_PVTPLL(200000000, 0, 0),
  206. { /* sentinel */ },
  207. };
  208. static struct pvtpll_table rk3588_npu_pvtpll_table[] = {
  209. /* rate_hz, ring_sel, length */
  210. ROCKCHIP_PVTPLL(1000000000, 1, 12),
  211. ROCKCHIP_PVTPLL(900000000, 1, 12),
  212. ROCKCHIP_PVTPLL(800000000, 1, 12),
  213. ROCKCHIP_PVTPLL(700000000, 1, 13),
  214. ROCKCHIP_PVTPLL(600000000, 1, 17),
  215. ROCKCHIP_PVTPLL(500000000, 1, 25),
  216. ROCKCHIP_PVTPLL(400000000, 1, 38),
  217. ROCKCHIP_PVTPLL(300000000, 1, 55),
  218. ROCKCHIP_PVTPLL(200000000, 0, 0),
  219. { /* sentinel */ },
  220. };
  221. static unsigned long rk3588_cpul_rates[] = {
  222. 408000000, 600000000, 816000000, 1008000000,
  223. 1200000000, 1416000000, 1608000000, 1800000063,
  224. };
  225. static unsigned long rk3588_cpub_rates[] = {
  226. 408000000, 816000000, 1008000000, 1200000000,
  227. 1416000000, 1608000000, 1800000000, 2016000000,
  228. 2208000000, 2304000000, 2400000063
  229. };
  230. static unsigned long rk3588_gpu_rates[] = {
  231. 200000000, 300000000, 400000000, 500000000,
  232. 600000000, 700000000, 800000000, 900000000,
  233. 1000000063
  234. };
  235. static unsigned long rk3588_sbus_rates[] = {
  236. 24000000, 50000000, 100000000, 150000000, 200000000,
  237. 250000000, 350000000, 700000000
  238. };
  239. static unsigned long rk3588_sdmmc_rates[] = {
  240. 400000, 24000000, 50000000, 100000000, 150000000, 200000000,
  241. 300000000, 400000000, 600000000, 700000000
  242. };
  243. static struct sys_clk_info_t sys_clk_info;
  244. static int clk_scmi_dsu_set_rate(rk_scmi_clock_t *clock, unsigned long rate);
  245. static struct pvtpll_table *rkclk_get_pvtpll_config(struct pvtpll_table *table,
  246. unsigned int count,
  247. unsigned int freq_hz)
  248. {
  249. int i;
  250. for (i = 0; i < count; i++) {
  251. if (freq_hz == table[i].rate)
  252. return &table[i];
  253. }
  254. return NULL;
  255. }
  256. static int clk_cpul_set_rate(unsigned long rate, enum pll_type_sel type)
  257. {
  258. struct pvtpll_table *pvtpll;
  259. int div;
  260. if (rate == 0)
  261. return SCMI_INVALID_PARAMETERS;
  262. pvtpll = rkclk_get_pvtpll_config(sys_clk_info.cpul_table,
  263. sys_clk_info.cpul_rate_count, rate);
  264. if (pvtpll == NULL)
  265. return SCMI_INVALID_PARAMETERS;
  266. /* set lpll */
  267. if (PVTPLL_NEED(type, pvtpll->length) != 0) {
  268. /* set clock gating interval */
  269. mmio_write_32(LITCOREGRF_BASE + RK3588_CPUL_PVTPLL_CON2,
  270. 0x00040000);
  271. /* set ring sel */
  272. mmio_write_32(LITCOREGRF_BASE + RK3588_CPUL_PVTPLL_CON0_L,
  273. 0x07000000 | (pvtpll->ring_sel << 8));
  274. /* set length */
  275. mmio_write_32(LITCOREGRF_BASE + RK3588_CPUL_PVTPLL_CON0_H,
  276. 0x003f0000 | pvtpll->length);
  277. /* set cal cnt = 24, T = 1us */
  278. mmio_write_32(LITCOREGRF_BASE + RK3588_CPUL_PVTPLL_CON1,
  279. 0x18);
  280. /* enable pvtpll */
  281. mmio_write_32(LITCOREGRF_BASE + RK3588_CPUL_PVTPLL_CON0_L,
  282. 0x00020002);
  283. /* start monitor */
  284. mmio_write_32(LITCOREGRF_BASE + RK3588_CPUL_PVTPLL_CON0_L,
  285. 0x00010001);
  286. /* set corel mux pvtpll */
  287. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(7),
  288. CPUL_PVTPLL_PATH_PVTPLL);
  289. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(6),
  290. CPUL_CLK_PATH_PVTPLL);
  291. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(7),
  292. CPUL_CLK_PATH_PVTPLL);
  293. return 0;
  294. }
  295. /* set clk corel div */
  296. div = DIV_ROUND_UP(GPLL_RATE, rate) - 1;
  297. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(6),
  298. CLKDIV_5BITS_SHF(div, 0) | CLKDIV_5BITS_SHF(div, 7));
  299. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(7),
  300. CLKDIV_5BITS_SHF(div, 0) | CLKDIV_5BITS_SHF(div, 7));
  301. /* set corel mux gpll */
  302. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(5),
  303. CPUL_CLK_PATH_NOR_GPLL);
  304. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(6),
  305. CPUL_CLK_PATH_LPLL);
  306. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(7),
  307. CPUL_CLK_PATH_LPLL);
  308. return 0;
  309. }
  310. static int clk_scmi_cpul_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  311. {
  312. int ret;
  313. if (rate == 0)
  314. return SCMI_INVALID_PARAMETERS;
  315. ret = clk_cpul_set_rate(rate, PLL_SEL_AUTO);
  316. if (ret == 0) {
  317. sys_clk_info.cpul_rate = rate;
  318. ret = clk_scmi_dsu_set_rate(clock, rate);
  319. }
  320. return ret;
  321. }
  322. static unsigned long rk3588_lpll_get_rate(void)
  323. {
  324. unsigned int m, p, s, k;
  325. uint64_t rate64 = 24000000, postdiv;
  326. int mode;
  327. mode = (mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(5)) >> 14) &
  328. 0x3;
  329. if (mode == 0)
  330. return rate64;
  331. m = (mmio_read_32(DSUCRU_BASE + CRU_PLL_CON(16)) >>
  332. CRU_PLLCON0_M_SHIFT) &
  333. CRU_PLLCON0_M_MASK;
  334. p = (mmio_read_32(DSUCRU_BASE + CRU_PLL_CON(17)) >>
  335. CRU_PLLCON1_P_SHIFT) &
  336. CRU_PLLCON1_P_MASK;
  337. s = (mmio_read_32(DSUCRU_BASE + CRU_PLL_CON(17)) >>
  338. CRU_PLLCON1_S_SHIFT) &
  339. CRU_PLLCON1_S_MASK;
  340. k = (mmio_read_32(DSUCRU_BASE + CRU_PLL_CON(18)) >>
  341. CRU_PLLCON2_K_SHIFT) &
  342. CRU_PLLCON2_K_MASK;
  343. rate64 *= m;
  344. rate64 = rate64 / p;
  345. if (k != 0) {
  346. /* fractional mode */
  347. uint64_t frac_rate64 = 24000000 * k;
  348. postdiv = p * 65535;
  349. frac_rate64 = frac_rate64 / postdiv;
  350. rate64 += frac_rate64;
  351. }
  352. rate64 = rate64 >> s;
  353. return (unsigned long)rate64;
  354. }
  355. static unsigned long clk_scmi_cpul_get_rate(rk_scmi_clock_t *clock)
  356. {
  357. int src, div;
  358. src = mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(6)) & 0x0060;
  359. src = src >> 5;
  360. if (src == 2) {
  361. return sys_clk_info.cpul_rate;
  362. } else {
  363. src = mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(5)) & 0xc000;
  364. src = src >> 14;
  365. div = mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(6)) & 0x1f;
  366. switch (src) {
  367. case 0:
  368. return 24000000;
  369. case 1:
  370. /* Make the return rate is equal to the set rate */
  371. if (sys_clk_info.cpul_rate)
  372. return sys_clk_info.cpul_rate;
  373. else
  374. return GPLL_RATE / (div + 1);
  375. case 2:
  376. return rk3588_lpll_get_rate();
  377. default:
  378. return 0;
  379. }
  380. }
  381. }
  382. static int clk_scmi_cpul_set_status(rk_scmi_clock_t *clock, bool status)
  383. {
  384. return 0;
  385. }
  386. static void clk_scmi_b0pll_disable(void)
  387. {
  388. static bool is_b0pll_disabled;
  389. if (is_b0pll_disabled != 0)
  390. return;
  391. /* set coreb01 mux gpll */
  392. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0),
  393. CPUB01_CLK_PATH_NOR_GPLL);
  394. /* pll enter slow mode */
  395. mmio_write_32(BIGCORE0CRU_BASE + CRU_MODE_CON0, CPU_PLL_PATH_SLOWMODE);
  396. /* set pll power down */
  397. mmio_write_32(BIGCORE0CRU_BASE + CRU_PLL_CON(1), CRU_PLL_POWER_DOWN);
  398. is_b0pll_disabled = true;
  399. }
  400. static int clk_cpub01_set_rate(unsigned long rate, enum pll_type_sel type)
  401. {
  402. struct pvtpll_table *pvtpll;
  403. int div;
  404. if (rate == 0)
  405. return SCMI_INVALID_PARAMETERS;
  406. pvtpll = rkclk_get_pvtpll_config(sys_clk_info.cpub01_table,
  407. sys_clk_info.cpub01_rate_count, rate);
  408. if (pvtpll == NULL)
  409. return SCMI_INVALID_PARAMETERS;
  410. /* set b0pll */
  411. if (PVTPLL_NEED(type, pvtpll->length)) {
  412. /* set clock gating interval */
  413. mmio_write_32(BIGCORE0GRF_BASE + RK3588_CPUB_PVTPLL_CON2,
  414. 0x00040000);
  415. /* set ring sel */
  416. mmio_write_32(BIGCORE0GRF_BASE + RK3588_CPUB_PVTPLL_CON0_L,
  417. 0x07000000 | (pvtpll->ring_sel << 8));
  418. /* set length */
  419. mmio_write_32(BIGCORE0GRF_BASE + RK3588_CPUB_PVTPLL_CON0_H,
  420. 0x003f0000 | pvtpll->length);
  421. /* set cal cnt = 24, T = 1us */
  422. mmio_write_32(BIGCORE0GRF_BASE + RK3588_CPUB_PVTPLL_CON1,
  423. 0x18);
  424. /* enable pvtpll */
  425. mmio_write_32(BIGCORE0GRF_BASE + RK3588_CPUB_PVTPLL_CON0_L,
  426. 0x00020002);
  427. /* start monitor */
  428. mmio_write_32(BIGCORE0GRF_BASE + RK3588_CPUB_PVTPLL_CON0_L,
  429. 0x00010001);
  430. /* set core mux pvtpll */
  431. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(2),
  432. CPUB01_PVTPLL_PATH_PVTPLL);
  433. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0),
  434. CPUB01_CLK_PATH_B0_PVTPLL);
  435. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(1),
  436. CPUB01_CLK_PATH_B1_PVTPLL);
  437. goto out;
  438. }
  439. /* set clk coreb01 div */
  440. div = DIV_ROUND_UP(GPLL_RATE, rate) - 1;
  441. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0),
  442. CLKDIV_5BITS_SHF(div, 8));
  443. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(1),
  444. CLKDIV_5BITS_SHF(div, 0));
  445. /* set coreb01 mux gpll */
  446. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0),
  447. CPUB01_CLK_PATH_NOR_GPLL);
  448. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0),
  449. CPUB01_CLK_PATH_B0PLL);
  450. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(1),
  451. CPUB01_CLK_PATH_B1PLL);
  452. out:
  453. clk_scmi_b0pll_disable();
  454. return 0;
  455. }
  456. static int clk_scmi_cpub01_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  457. {
  458. int ret;
  459. if (rate == 0)
  460. return SCMI_INVALID_PARAMETERS;
  461. ret = clk_cpub01_set_rate(rate, PLL_SEL_AUTO);
  462. if (ret == 0)
  463. sys_clk_info.cpub01_rate = rate;
  464. return ret;
  465. }
  466. static unsigned long rk3588_b0pll_get_rate(void)
  467. {
  468. unsigned int m, p, s, k;
  469. uint64_t rate64 = 24000000, postdiv;
  470. int mode;
  471. mode = (mmio_read_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0)) >> 6) &
  472. 0x3;
  473. if (mode == 0)
  474. return rate64;
  475. m = (mmio_read_32(BIGCORE0CRU_BASE + CRU_PLL_CON(0)) >>
  476. CRU_PLLCON0_M_SHIFT) &
  477. CRU_PLLCON0_M_MASK;
  478. p = (mmio_read_32(BIGCORE0CRU_BASE + CRU_PLL_CON(1)) >>
  479. CRU_PLLCON1_P_SHIFT) &
  480. CRU_PLLCON1_P_MASK;
  481. s = (mmio_read_32(BIGCORE0CRU_BASE + CRU_PLL_CON(1)) >>
  482. CRU_PLLCON1_S_SHIFT) &
  483. CRU_PLLCON1_S_MASK;
  484. k = (mmio_read_32(BIGCORE0CRU_BASE + CRU_PLL_CON(2)) >>
  485. CRU_PLLCON2_K_SHIFT) &
  486. CRU_PLLCON2_K_MASK;
  487. rate64 *= m;
  488. rate64 = rate64 / p;
  489. if (k != 0) {
  490. /* fractional mode */
  491. uint64_t frac_rate64 = 24000000 * k;
  492. postdiv = p * 65535;
  493. frac_rate64 = frac_rate64 / postdiv;
  494. rate64 += frac_rate64;
  495. }
  496. rate64 = rate64 >> s;
  497. return (unsigned long)rate64;
  498. }
  499. static unsigned long clk_scmi_cpub01_get_rate(rk_scmi_clock_t *clock)
  500. {
  501. int value, src, div;
  502. value = mmio_read_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0));
  503. src = (value & 0x6000) >> 13;
  504. if (src == 2) {
  505. return sys_clk_info.cpub01_rate;
  506. } else {
  507. src = (value & 0x00c0) >> 6;
  508. div = (value & 0x1f00) >> 8;
  509. switch (src) {
  510. case 0:
  511. return 24000000;
  512. case 1:
  513. /* Make the return rate is equal to the set rate */
  514. if (sys_clk_info.cpub01_rate)
  515. return sys_clk_info.cpub01_rate;
  516. else
  517. return GPLL_RATE / (div + 1);
  518. case 2:
  519. return rk3588_b0pll_get_rate();
  520. default:
  521. return 0;
  522. }
  523. }
  524. }
  525. static int clk_scmi_cpub01_set_status(rk_scmi_clock_t *clock, bool status)
  526. {
  527. return 0;
  528. }
  529. static void clk_scmi_b1pll_disable(void)
  530. {
  531. static bool is_b1pll_disabled;
  532. if (is_b1pll_disabled != 0)
  533. return;
  534. /* set coreb23 mux gpll */
  535. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0),
  536. CPUB01_CLK_PATH_NOR_GPLL);
  537. /* pll enter slow mode */
  538. mmio_write_32(BIGCORE1CRU_BASE + CRU_MODE_CON0, CPU_PLL_PATH_SLOWMODE);
  539. /* set pll power down */
  540. mmio_write_32(BIGCORE1CRU_BASE + CRU_PLL_CON(9), CRU_PLL_POWER_DOWN);
  541. is_b1pll_disabled = true;
  542. }
  543. static int clk_cpub23_set_rate(unsigned long rate, enum pll_type_sel type)
  544. {
  545. struct pvtpll_table *pvtpll;
  546. int div;
  547. if (rate == 0)
  548. return SCMI_INVALID_PARAMETERS;
  549. pvtpll = rkclk_get_pvtpll_config(sys_clk_info.cpub23_table,
  550. sys_clk_info.cpub23_rate_count, rate);
  551. if (pvtpll == NULL)
  552. return SCMI_INVALID_PARAMETERS;
  553. /* set b1pll */
  554. if (PVTPLL_NEED(type, pvtpll->length)) {
  555. /* set clock gating interval */
  556. mmio_write_32(BIGCORE1GRF_BASE + RK3588_CPUB_PVTPLL_CON2,
  557. 0x00040000);
  558. /* set ring sel */
  559. mmio_write_32(BIGCORE1GRF_BASE + RK3588_CPUB_PVTPLL_CON0_L,
  560. 0x07000000 | (pvtpll->ring_sel << 8));
  561. /* set length */
  562. mmio_write_32(BIGCORE1GRF_BASE + RK3588_CPUB_PVTPLL_CON0_H,
  563. 0x003f0000 | pvtpll->length);
  564. /* set cal cnt = 24, T = 1us */
  565. mmio_write_32(BIGCORE1GRF_BASE + RK3588_CPUB_PVTPLL_CON1,
  566. 0x18);
  567. /* enable pvtpll */
  568. mmio_write_32(BIGCORE1GRF_BASE + RK3588_CPUB_PVTPLL_CON0_L,
  569. 0x00020002);
  570. /* start monitor */
  571. mmio_write_32(BIGCORE1GRF_BASE + RK3588_CPUB_PVTPLL_CON0_L,
  572. 0x00010001);
  573. /* set core mux pvtpll */
  574. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(2),
  575. CPUB01_PVTPLL_PATH_PVTPLL);
  576. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0),
  577. CPUB01_CLK_PATH_B0_PVTPLL);
  578. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(1),
  579. CPUB01_CLK_PATH_B1_PVTPLL);
  580. goto out;
  581. }
  582. /* set clk coreb23 div */
  583. div = DIV_ROUND_UP(GPLL_RATE, rate) - 1;
  584. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0),
  585. CLKDIV_5BITS_SHF(div, 8));
  586. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(1),
  587. CLKDIV_5BITS_SHF(div, 0));
  588. /* set coreb23 mux gpll */
  589. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0),
  590. CPUB01_CLK_PATH_NOR_GPLL);
  591. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0),
  592. CPUB01_CLK_PATH_B0PLL);
  593. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(1),
  594. CPUB01_CLK_PATH_B1PLL);
  595. out:
  596. clk_scmi_b1pll_disable();
  597. return 0;
  598. }
  599. static int clk_scmi_cpub23_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  600. {
  601. int ret;
  602. if (rate == 0)
  603. return SCMI_INVALID_PARAMETERS;
  604. ret = clk_cpub23_set_rate(rate, PLL_SEL_AUTO);
  605. if (ret == 0)
  606. sys_clk_info.cpub23_rate = rate;
  607. return ret;
  608. }
  609. static unsigned long rk3588_b1pll_get_rate(void)
  610. {
  611. unsigned int m, p, s, k;
  612. uint64_t rate64 = 24000000, postdiv;
  613. int mode;
  614. mode = (mmio_read_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0)) >> 6) &
  615. 0x3;
  616. if (mode == 0)
  617. return rate64;
  618. m = (mmio_read_32(BIGCORE1CRU_BASE + CRU_PLL_CON(8)) >>
  619. CRU_PLLCON0_M_SHIFT) &
  620. CRU_PLLCON0_M_MASK;
  621. p = (mmio_read_32(BIGCORE1CRU_BASE + CRU_PLL_CON(9)) >>
  622. CRU_PLLCON1_P_SHIFT) &
  623. CRU_PLLCON1_P_MASK;
  624. s = (mmio_read_32(BIGCORE1CRU_BASE + CRU_PLL_CON(9)) >>
  625. CRU_PLLCON1_S_SHIFT) &
  626. CRU_PLLCON1_S_MASK;
  627. k = (mmio_read_32(BIGCORE1CRU_BASE + CRU_PLL_CON(10)) >>
  628. CRU_PLLCON2_K_SHIFT) &
  629. CRU_PLLCON2_K_MASK;
  630. rate64 *= m;
  631. rate64 = rate64 / p;
  632. if (k != 0) {
  633. /* fractional mode */
  634. uint64_t frac_rate64 = 24000000 * k;
  635. postdiv = p * 65535;
  636. frac_rate64 = frac_rate64 / postdiv;
  637. rate64 += frac_rate64;
  638. }
  639. rate64 = rate64 >> s;
  640. return (unsigned long)rate64;
  641. }
  642. static unsigned long clk_scmi_cpub23_get_rate(rk_scmi_clock_t *clock)
  643. {
  644. int value, src, div;
  645. value = mmio_read_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0));
  646. src = (value & 0x6000) >> 13;
  647. if (src == 2) {
  648. return sys_clk_info.cpub23_rate;
  649. } else {
  650. src = (value & 0x00c0) >> 6;
  651. div = (value & 0x1f00) >> 8;
  652. switch (src) {
  653. case 0:
  654. return 24000000;
  655. case 1:
  656. /* Make the return rate is equal to the set rate */
  657. if (sys_clk_info.cpub23_rate)
  658. return sys_clk_info.cpub23_rate;
  659. else
  660. return GPLL_RATE / (div + 1);
  661. case 2:
  662. return rk3588_b1pll_get_rate();
  663. default:
  664. return 0;
  665. }
  666. }
  667. }
  668. static int clk_scmi_cpub23_set_status(rk_scmi_clock_t *clock, bool status)
  669. {
  670. return 0;
  671. }
  672. static unsigned long clk_scmi_dsu_get_rate(rk_scmi_clock_t *clock)
  673. {
  674. int src, div;
  675. src = mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(1)) & 0x1;
  676. if (src != 0) {
  677. return sys_clk_info.dsu_rate;
  678. } else {
  679. src = mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(0)) & 0x3000;
  680. src = src >> 12;
  681. div = mmio_read_32(DSUCRU_BASE + CRU_CLKSEL_CON(0)) & 0xf80;
  682. div = div >> 7;
  683. switch (src) {
  684. case 0:
  685. return rk3588_b0pll_get_rate() / (div + 1);
  686. case 1:
  687. return rk3588_b1pll_get_rate() / (div + 1);
  688. case 2:
  689. return rk3588_lpll_get_rate() / (div + 1);
  690. case 3:
  691. return GPLL_RATE / (div + 1);
  692. default:
  693. return 0;
  694. }
  695. }
  696. }
  697. static void clk_scmi_lpll_disable(void)
  698. {
  699. static bool is_lpll_disabled;
  700. if (is_lpll_disabled)
  701. return;
  702. /* set corel mux gpll */
  703. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(5),
  704. CPUL_CLK_PATH_NOR_GPLL);
  705. /* set dsu mux gpll */
  706. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(0),
  707. SCLK_DSU_PATH_NOR_GPLL);
  708. /* pll enter slow mode */
  709. mmio_write_32(DSUCRU_BASE + CRU_MODE_CON0, CPU_PLL_PATH_SLOWMODE);
  710. /* set pll power down */
  711. mmio_write_32(DSUCRU_BASE + CRU_PLL_CON(17), CRU_PLL_POWER_DOWN);
  712. is_lpll_disabled = true;
  713. }
  714. static int clk_dsu_set_rate(unsigned long rate, enum pll_type_sel type)
  715. {
  716. struct pvtpll_table *pvtpll;
  717. int div;
  718. if (rate == 0)
  719. return SCMI_INVALID_PARAMETERS;
  720. pvtpll = rkclk_get_pvtpll_config(sys_clk_info.cpul_table,
  721. sys_clk_info.cpul_rate_count, rate);
  722. if (pvtpll == NULL)
  723. return SCMI_INVALID_PARAMETERS;
  724. /* set pvtpll */
  725. if (PVTPLL_NEED(type, pvtpll->length)) {
  726. /* set clock gating interval */
  727. mmio_write_32(DSUGRF_BASE + RK3588_DSU_PVTPLL_CON2,
  728. 0x00040000);
  729. /* set ring sel */
  730. mmio_write_32(DSUGRF_BASE + RK3588_DSU_PVTPLL_CON0_L,
  731. 0x07000000 | (pvtpll->ring_sel << 8));
  732. /* set length */
  733. mmio_write_32(DSUGRF_BASE + RK3588_DSU_PVTPLL_CON0_H,
  734. 0x003f0000 | pvtpll->length);
  735. /* set cal cnt = 24, T = 1us */
  736. mmio_write_32(DSUGRF_BASE + RK3588_DSU_PVTPLL_CON1,
  737. 0x18);
  738. /* enable pvtpll */
  739. mmio_write_32(DSUGRF_BASE + RK3588_DSU_PVTPLL_CON0_L,
  740. 0x00020002);
  741. /* start monitor */
  742. mmio_write_32(DSUGRF_BASE + RK3588_DSU_PVTPLL_CON0_L,
  743. 0x00010001);
  744. /* set dsu mux pvtpll */
  745. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(7),
  746. DSU_PVTPLL_PATH_PVTPLL);
  747. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(1),
  748. SCLK_DSU_PATH_PVTPLL);
  749. goto out;
  750. }
  751. /* set dsu div */
  752. div = DIV_ROUND_UP(GPLL_RATE, rate) - 1;
  753. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(0),
  754. CLKDIV_5BITS_SHF(div, 7));
  755. /* set dsu mux gpll */
  756. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(0),
  757. SCLK_DSU_PATH_NOR_GPLL);
  758. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(1),
  759. SCLK_DSU_PATH_NOR_PLL);
  760. out:
  761. clk_scmi_lpll_disable();
  762. return 0;
  763. }
  764. static int clk_scmi_dsu_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  765. {
  766. int ret;
  767. if (rate == 0)
  768. return SCMI_INVALID_PARAMETERS;
  769. ret = clk_dsu_set_rate(rate, PLL_SEL_AUTO);
  770. if (ret == 0)
  771. sys_clk_info.dsu_rate = rate;
  772. return ret;
  773. }
  774. static int clk_scmi_dsu_set_status(rk_scmi_clock_t *clock, bool status)
  775. {
  776. return 0;
  777. }
  778. static unsigned long clk_scmi_gpu_get_rate(rk_scmi_clock_t *clock)
  779. {
  780. int div, src;
  781. if ((mmio_read_32(CRU_BASE + CRU_CLKSEL_CON(158)) & 0x4000) != 0) {
  782. return sys_clk_info.gpu_rate;
  783. } else {
  784. div = mmio_read_32(CRU_BASE + CRU_CLKSEL_CON(158)) & 0x1f;
  785. src = mmio_read_32(CRU_BASE + CRU_CLKSEL_CON(158)) & 0x00e0;
  786. src = src >> 5;
  787. switch (src) {
  788. case 0:
  789. /* Make the return rate is equal to the set rate */
  790. if (sys_clk_info.gpu_rate)
  791. return sys_clk_info.gpu_rate;
  792. else
  793. return GPLL_RATE / (div + 1);
  794. case 1:
  795. return CPLL_RATE / (div + 1);
  796. case 2:
  797. return AUPLL_RATE / (div + 1);
  798. case 3:
  799. return NPLL_RATE / (div + 1);
  800. case 4:
  801. return SPLL_RATE / (div + 1);
  802. default:
  803. return 0;
  804. }
  805. }
  806. }
  807. static int clk_gpu_set_rate(unsigned long rate, enum pll_type_sel type)
  808. {
  809. struct pvtpll_table *pvtpll;
  810. int div;
  811. pvtpll = rkclk_get_pvtpll_config(sys_clk_info.gpu_table,
  812. sys_clk_info.gpu_rate_count, rate);
  813. if (pvtpll == NULL)
  814. return SCMI_INVALID_PARAMETERS;
  815. if (PVTPLL_NEED(type, pvtpll->length)) {
  816. /* set clock gating interval */
  817. mmio_write_32(GPUGRF_BASE + RK3588_GPU_PVTPLL_CON2,
  818. 0x00040000);
  819. /* set ring sel */
  820. mmio_write_32(GPUGRF_BASE + RK3588_GPU_PVTPLL_CON0_L,
  821. 0x07000000 | (pvtpll->ring_sel << 8));
  822. /* set length */
  823. mmio_write_32(GPUGRF_BASE + RK3588_GPU_PVTPLL_CON0_H,
  824. 0x003f0000 | pvtpll->length);
  825. /* set cal cnt = 24, T = 1us */
  826. mmio_write_32(GPUGRF_BASE + RK3588_GPU_PVTPLL_CON1,
  827. 0x18);
  828. /* enable pvtpll */
  829. mmio_write_32(GPUGRF_BASE + RK3588_GPU_PVTPLL_CON0_L,
  830. 0x00020002);
  831. /* start monitor */
  832. mmio_write_32(GPUGRF_BASE + RK3588_GPU_PVTPLL_CON0_L,
  833. 0x00010001);
  834. /* set gpu mux pvtpll */
  835. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(158),
  836. GPU_CLK_PATH_PVTPLL);
  837. return 0;
  838. }
  839. /* set gpu div */
  840. div = DIV_ROUND_UP(GPLL_RATE, rate);
  841. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(158),
  842. CLKDIV_5BITS_SHF(div - 1, 0));
  843. /* set gpu mux gpll */
  844. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(158),
  845. GPU_CLK_PATH_NOR_GPLL);
  846. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(158),
  847. GPU_CLK_PATH_NOR_PLL);
  848. return 0;
  849. }
  850. static int clk_scmi_gpu_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  851. {
  852. int ret;
  853. if (rate == 0)
  854. return SCMI_INVALID_PARAMETERS;
  855. ret = clk_gpu_set_rate(rate, PLL_SEL_AUTO);
  856. if (ret == 0)
  857. sys_clk_info.gpu_rate = rate;
  858. return ret;
  859. }
  860. static int clk_scmi_gpu_set_status(rk_scmi_clock_t *clock, bool status)
  861. {
  862. return 0;
  863. }
  864. static unsigned long clk_scmi_npu_get_rate(rk_scmi_clock_t *clock)
  865. {
  866. int div, src;
  867. if ((mmio_read_32(CRU_BASE + CRU_CLKSEL_CON(74)) & 0x1) != 0) {
  868. return sys_clk_info.npu_rate;
  869. } else {
  870. div = mmio_read_32(CRU_BASE + CRU_CLKSEL_CON(73)) & 0x007c;
  871. div = div >> 2;
  872. src = mmio_read_32(CRU_BASE + CRU_CLKSEL_CON(73)) & 0x0380;
  873. src = src >> 7;
  874. switch (src) {
  875. case 0:
  876. /* Make the return rate is equal to the set rate */
  877. if (sys_clk_info.npu_rate != 0)
  878. return sys_clk_info.npu_rate;
  879. else
  880. return GPLL_RATE / (div + 1);
  881. case 1:
  882. return CPLL_RATE / (div + 1);
  883. case 2:
  884. return AUPLL_RATE / (div + 1);
  885. case 3:
  886. return NPLL_RATE / (div + 1);
  887. case 4:
  888. return SPLL_RATE / (div + 1);
  889. default:
  890. return 0;
  891. }
  892. }
  893. }
  894. static int clk_npu_set_rate(unsigned long rate, enum pll_type_sel type)
  895. {
  896. struct pvtpll_table *pvtpll;
  897. int div;
  898. pvtpll = rkclk_get_pvtpll_config(sys_clk_info.npu_table,
  899. sys_clk_info.npu_rate_count, rate);
  900. if (pvtpll == NULL)
  901. return SCMI_INVALID_PARAMETERS;
  902. if (PVTPLL_NEED(type, pvtpll->length)) {
  903. /* set clock gating interval */
  904. mmio_write_32(NPUGRF_BASE + RK3588_NPU_PVTPLL_CON2,
  905. 0x00040000);
  906. /* set ring sel */
  907. mmio_write_32(NPUGRF_BASE + RK3588_NPU_PVTPLL_CON0_L,
  908. 0x07000000 | (pvtpll->ring_sel << 8));
  909. /* set length */
  910. mmio_write_32(NPUGRF_BASE + RK3588_NPU_PVTPLL_CON0_H,
  911. 0x003f0000 | pvtpll->length);
  912. /* set cal cnt = 24, T = 1us */
  913. mmio_write_32(NPUGRF_BASE + RK3588_NPU_PVTPLL_CON1,
  914. 0x18);
  915. /* enable pvtpll */
  916. mmio_write_32(NPUGRF_BASE + RK3588_NPU_PVTPLL_CON0_L,
  917. 0x00020002);
  918. /* start monitor */
  919. mmio_write_32(NPUGRF_BASE + RK3588_NPU_PVTPLL_CON0_L,
  920. 0x00010001);
  921. /* set npu mux pvtpll */
  922. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(74),
  923. NPU_CLK_PATH_PVTPLL);
  924. return 0;
  925. }
  926. /* set npu div */
  927. div = DIV_ROUND_UP(GPLL_RATE, rate);
  928. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(73),
  929. CLKDIV_5BITS_SHF(div - 1, 2));
  930. /* set npu mux gpll */
  931. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(73),
  932. NPU_CLK_PATH_NOR_GPLL);
  933. mmio_write_32(CRU_BASE + CRU_CLKSEL_CON(74),
  934. NPU_CLK_PATH_NOR_PLL);
  935. return 0;
  936. }
  937. static int clk_scmi_npu_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  938. {
  939. int ret;
  940. if (rate == 0)
  941. return SCMI_INVALID_PARAMETERS;
  942. ret = clk_npu_set_rate(rate, PLL_SEL_AUTO);
  943. if (ret == 0)
  944. sys_clk_info.npu_rate = rate;
  945. return ret;
  946. }
  947. static int clk_scmi_npu_set_status(rk_scmi_clock_t *clock, bool status)
  948. {
  949. return 0;
  950. }
  951. static unsigned long clk_scmi_sbus_get_rate(rk_scmi_clock_t *clock)
  952. {
  953. int div;
  954. if ((mmio_read_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0)) & 0x0800) != 0) {
  955. div = mmio_read_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0));
  956. div = (div & 0x03e0) >> 5;
  957. return SPLL_RATE / (div + 1);
  958. } else {
  959. return OSC_HZ;
  960. }
  961. }
  962. static int clk_scmi_sbus_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  963. {
  964. int div;
  965. if (rate == OSC_HZ) {
  966. mmio_write_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0),
  967. WMSK_BIT(11));
  968. return 0;
  969. }
  970. div = DIV_ROUND_UP(SPLL_RATE, rate);
  971. mmio_write_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0),
  972. CLKDIV_5BITS_SHF(div - 1, 5));
  973. mmio_write_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0),
  974. BIT_WITH_WMSK(11) | WMSK_BIT(10));
  975. return 0;
  976. }
  977. static int clk_scmi_sbus_set_status(rk_scmi_clock_t *clock, bool status)
  978. {
  979. return 0;
  980. }
  981. static unsigned long clk_scmi_pclk_sbus_get_rate(rk_scmi_clock_t *clock)
  982. {
  983. int div;
  984. div = mmio_read_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0));
  985. div = div & 0x001f;
  986. return SPLL_RATE / (div + 1);
  987. }
  988. static int clk_scmi_pclk_sbus_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  989. {
  990. int div;
  991. div = DIV_ROUND_UP(SPLL_RATE, rate);
  992. mmio_write_32(BUSSCRU_BASE + CRU_CLKSEL_CON(0),
  993. CLKDIV_5BITS_SHF(div - 1, 0));
  994. return 0;
  995. }
  996. static int clk_scmi_pclk_sbus_set_status(rk_scmi_clock_t *clock, bool status)
  997. {
  998. return 0;
  999. }
  1000. static unsigned long clk_scmi_cclk_sdmmc_get_rate(rk_scmi_clock_t *clock)
  1001. {
  1002. int div;
  1003. uint32_t src;
  1004. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(3)) & 0x3000;
  1005. src = src >> 12;
  1006. div = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(3)) & 0x0fc0;
  1007. div = div >> 6;
  1008. if (src == 1) {
  1009. return SPLL_RATE / (div + 1);
  1010. } else if (src == 2) {
  1011. return OSC_HZ / (div + 1);
  1012. } else {
  1013. return GPLL_RATE / (div + 1);
  1014. }
  1015. }
  1016. static int clk_scmi_cclk_sdmmc_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1017. {
  1018. int div;
  1019. if ((OSC_HZ % rate) == 0) {
  1020. div = DIV_ROUND_UP(OSC_HZ, rate);
  1021. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(3),
  1022. CLKDIV_6BITS_SHF(div - 1, 6) |
  1023. BITS_WITH_WMASK(2U, 0x3U, 12));
  1024. } else if ((SPLL_RATE % rate) == 0) {
  1025. div = DIV_ROUND_UP(SPLL_RATE, rate);
  1026. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(3),
  1027. CLKDIV_6BITS_SHF(div - 1, 6) |
  1028. BITS_WITH_WMASK(1U, 0x3U, 12));
  1029. } else {
  1030. div = DIV_ROUND_UP(GPLL_RATE, rate);
  1031. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(3),
  1032. CLKDIV_6BITS_SHF(div - 1, 6) |
  1033. BITS_WITH_WMASK(0U, 0x3U, 12));
  1034. }
  1035. return 0;
  1036. }
  1037. static int clk_scmi_cclk_sdmmc_set_status(rk_scmi_clock_t *clock, bool status)
  1038. {
  1039. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(3),
  1040. BITS_WITH_WMASK(!status, 0x1U, 4));
  1041. return 0;
  1042. }
  1043. static unsigned long clk_scmi_dclk_sdmmc_get_rate(rk_scmi_clock_t *clock)
  1044. {
  1045. int div;
  1046. uint32_t src;
  1047. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(3)) & 0x0020;
  1048. div = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(3)) & 0x001f;
  1049. if (src != 0) {
  1050. return SPLL_RATE / (div + 1);
  1051. } else {
  1052. return GPLL_RATE / (div + 1);
  1053. }
  1054. }
  1055. static int clk_scmi_dclk_sdmmc_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1056. {
  1057. int div;
  1058. if ((SPLL_RATE % rate) == 0) {
  1059. div = DIV_ROUND_UP(SPLL_RATE, rate);
  1060. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(3),
  1061. CLKDIV_5BITS_SHF(div - 1, 0) |
  1062. BITS_WITH_WMASK(1U, 0x1U, 5));
  1063. } else {
  1064. div = DIV_ROUND_UP(GPLL_RATE, rate);
  1065. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(3),
  1066. CLKDIV_5BITS_SHF(div - 1, 0) |
  1067. BITS_WITH_WMASK(0U, 0x1U, 5));
  1068. }
  1069. return 0;
  1070. }
  1071. static int clk_scmi_dclk_sdmmc_set_status(rk_scmi_clock_t *clock, bool status)
  1072. {
  1073. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(3),
  1074. BITS_WITH_WMASK(!status, 0x1U, 1));
  1075. return 0;
  1076. }
  1077. static unsigned long clk_scmi_aclk_secure_ns_get_rate(rk_scmi_clock_t *clock)
  1078. {
  1079. uint32_t src;
  1080. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x0003;
  1081. switch (src) {
  1082. case 0:
  1083. return 350 * MHz;
  1084. case 1:
  1085. return 200 * MHz;
  1086. case 2:
  1087. return 100 * MHz;
  1088. case 3:
  1089. return OSC_HZ;
  1090. default:
  1091. return 0;
  1092. }
  1093. }
  1094. static int clk_scmi_aclk_secure_ns_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1095. {
  1096. uint32_t src;
  1097. if (rate >= 350 * MHz)
  1098. src = 0;
  1099. else if (rate >= 200 * MHz)
  1100. src = 1;
  1101. else if (rate >= 100 * MHz)
  1102. src = 2;
  1103. else
  1104. src = 3;
  1105. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1106. BITS_WITH_WMASK(src, 0x3U, 0));
  1107. return 0;
  1108. }
  1109. static int clk_scmi_aclk_secure_ns_set_status(rk_scmi_clock_t *clock, bool status)
  1110. {
  1111. return 0;
  1112. }
  1113. static unsigned long clk_scmi_hclk_secure_ns_get_rate(rk_scmi_clock_t *clock)
  1114. {
  1115. uint32_t src;
  1116. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x000c;
  1117. src = src >> 2;
  1118. switch (src) {
  1119. case 0:
  1120. return 150 * MHz;
  1121. case 1:
  1122. return 100 * MHz;
  1123. case 2:
  1124. return 50 * MHz;
  1125. case 3:
  1126. return OSC_HZ;
  1127. default:
  1128. return 0;
  1129. }
  1130. }
  1131. static int clk_scmi_hclk_secure_ns_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1132. {
  1133. uint32_t src;
  1134. if (rate >= 150 * MHz)
  1135. src = 0;
  1136. else if (rate >= 100 * MHz)
  1137. src = 1;
  1138. else if (rate >= 50 * MHz)
  1139. src = 2;
  1140. else
  1141. src = 3;
  1142. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1143. BITS_WITH_WMASK(src, 0x3U, 2));
  1144. return 0;
  1145. }
  1146. static int clk_scmi_hclk_secure_ns_set_status(rk_scmi_clock_t *clock, bool status)
  1147. {
  1148. return 0;
  1149. }
  1150. static unsigned long clk_scmi_tclk_wdt_get_rate(rk_scmi_clock_t *clock)
  1151. {
  1152. return OSC_HZ;
  1153. }
  1154. static int clk_scmi_tclk_wdt_set_status(rk_scmi_clock_t *clock, bool status)
  1155. {
  1156. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(2),
  1157. BITS_WITH_WMASK(!status, 0x1U, 0));
  1158. return 0;
  1159. }
  1160. static unsigned long clk_scmi_keyladder_core_get_rate(rk_scmi_clock_t *clock)
  1161. {
  1162. uint32_t src;
  1163. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(2)) & 0x00c0;
  1164. src = src >> 6;
  1165. switch (src) {
  1166. case 0:
  1167. return 350 * MHz;
  1168. case 1:
  1169. return 233 * MHz;
  1170. case 2:
  1171. return 116 * MHz;
  1172. case 3:
  1173. return OSC_HZ;
  1174. default:
  1175. return 0;
  1176. }
  1177. }
  1178. static int clk_scmi_keyladder_core_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1179. {
  1180. uint32_t src;
  1181. if (rate >= 350 * MHz)
  1182. src = 0;
  1183. else if (rate >= 233 * MHz)
  1184. src = 1;
  1185. else if (rate >= 116 * MHz)
  1186. src = 2;
  1187. else
  1188. src = 3;
  1189. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(2),
  1190. BITS_WITH_WMASK(src, 0x3U, 6));
  1191. return 0;
  1192. }
  1193. static int clk_scmi_keyladder_core_set_status(rk_scmi_clock_t *clock, bool status)
  1194. {
  1195. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1196. BITS_WITH_WMASK(!status, 0x1U, 9));
  1197. return 0;
  1198. }
  1199. static unsigned long clk_scmi_keyladder_rng_get_rate(rk_scmi_clock_t *clock)
  1200. {
  1201. uint32_t src;
  1202. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(2)) & 0x0300;
  1203. src = src >> 8;
  1204. switch (src) {
  1205. case 0:
  1206. return 175 * MHz;
  1207. case 1:
  1208. return 116 * MHz;
  1209. case 2:
  1210. return 58 * MHz;
  1211. case 3:
  1212. return OSC_HZ;
  1213. default:
  1214. return 0;
  1215. }
  1216. }
  1217. static int clk_scmi_keyladder_rng_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1218. {
  1219. uint32_t src;
  1220. if (rate >= 175 * MHz)
  1221. src = 0;
  1222. else if (rate >= 116 * MHz)
  1223. src = 1;
  1224. else if (rate >= 58 * MHz)
  1225. src = 2;
  1226. else
  1227. src = 3;
  1228. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(2),
  1229. BITS_WITH_WMASK(src, 0x3U, 8));
  1230. return 0;
  1231. }
  1232. static int clk_scmi_keyladder_rng_set_status(rk_scmi_clock_t *clock, bool status)
  1233. {
  1234. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1235. BITS_WITH_WMASK(!status, 0x1U, 10));
  1236. return 0;
  1237. }
  1238. static unsigned long clk_scmi_aclk_secure_s_get_rate(rk_scmi_clock_t *clock)
  1239. {
  1240. uint32_t src;
  1241. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x0030;
  1242. src = src >> 4;
  1243. switch (src) {
  1244. case 0:
  1245. return 350 * MHz;
  1246. case 1:
  1247. return 233 * MHz;
  1248. case 2:
  1249. return 116 * MHz;
  1250. case 3:
  1251. return OSC_HZ;
  1252. default:
  1253. return 0;
  1254. }
  1255. }
  1256. static int clk_scmi_aclk_secure_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1257. {
  1258. uint32_t src;
  1259. if (rate >= 350 * MHz)
  1260. src = 0;
  1261. else if (rate >= 233 * MHz)
  1262. src = 1;
  1263. else if (rate >= 116 * MHz)
  1264. src = 2;
  1265. else
  1266. src = 3;
  1267. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1268. BITS_WITH_WMASK(src, 0x3U, 4));
  1269. return 0;
  1270. }
  1271. static int clk_scmi_aclk_secure_s_set_status(rk_scmi_clock_t *clock, bool status)
  1272. {
  1273. return 0;
  1274. }
  1275. static unsigned long clk_scmi_hclk_secure_s_get_rate(rk_scmi_clock_t *clock)
  1276. {
  1277. uint32_t src;
  1278. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x00c0;
  1279. src = src >> 6;
  1280. switch (src) {
  1281. case 0:
  1282. return 175 * MHz;
  1283. case 1:
  1284. return 116 * MHz;
  1285. case 2:
  1286. return 58 * MHz;
  1287. case 3:
  1288. return OSC_HZ;
  1289. default:
  1290. return 0;
  1291. }
  1292. }
  1293. static int clk_scmi_hclk_secure_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1294. {
  1295. uint32_t src;
  1296. if (rate >= 175 * MHz)
  1297. src = 0;
  1298. else if (rate >= 116 * MHz)
  1299. src = 1;
  1300. else if (rate >= 58 * MHz)
  1301. src = 2;
  1302. else
  1303. src = 3;
  1304. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1305. BITS_WITH_WMASK(src, 0x3U, 6));
  1306. return 0;
  1307. }
  1308. static int clk_scmi_hclk_secure_s_set_status(rk_scmi_clock_t *clock, bool status)
  1309. {
  1310. return 0;
  1311. }
  1312. static unsigned long clk_scmi_pclk_secure_s_get_rate(rk_scmi_clock_t *clock)
  1313. {
  1314. uint32_t src;
  1315. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x0300;
  1316. src = src >> 8;
  1317. switch (src) {
  1318. case 0:
  1319. return 116 * MHz;
  1320. case 1:
  1321. return 58 * MHz;
  1322. case 2:
  1323. return OSC_HZ;
  1324. default:
  1325. return 0;
  1326. }
  1327. }
  1328. static int clk_scmi_pclk_secure_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1329. {
  1330. uint32_t src;
  1331. if (rate >= 116 * MHz)
  1332. src = 0;
  1333. else if (rate >= 58 * MHz)
  1334. src = 1;
  1335. else
  1336. src = 2;
  1337. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1338. BITS_WITH_WMASK(src, 0x3U, 8));
  1339. return 0;
  1340. }
  1341. static int clk_scmi_pclk_secure_s_set_status(rk_scmi_clock_t *clock, bool status)
  1342. {
  1343. return 0;
  1344. }
  1345. static unsigned long clk_scmi_crypto_rng_get_rate(rk_scmi_clock_t *clock)
  1346. {
  1347. uint32_t src;
  1348. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0xc000;
  1349. src = src >> 14;
  1350. switch (src) {
  1351. case 0:
  1352. return 175 * MHz;
  1353. case 1:
  1354. return 116 * MHz;
  1355. case 2:
  1356. return 58 * MHz;
  1357. case 3:
  1358. return OSC_HZ;
  1359. default:
  1360. return 0;
  1361. }
  1362. }
  1363. static int clk_scmi_crypto_rng_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1364. {
  1365. uint32_t src;
  1366. if (rate >= 175 * MHz)
  1367. src = 0;
  1368. else if (rate >= 116 * MHz)
  1369. src = 1;
  1370. else if (rate >= 58 * MHz)
  1371. src = 2;
  1372. else
  1373. src = 3;
  1374. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1375. BITS_WITH_WMASK(src, 0x3U, 14));
  1376. return 0;
  1377. }
  1378. static int clk_scmi_crypto_rng_set_status(rk_scmi_clock_t *clock, bool status)
  1379. {
  1380. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1381. BITS_WITH_WMASK(!status, 0x1U, 1));
  1382. return 0;
  1383. }
  1384. static unsigned long clk_scmi_crypto_core_get_rate(rk_scmi_clock_t *clock)
  1385. {
  1386. uint32_t src;
  1387. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x0c00;
  1388. src = src >> 10;
  1389. switch (src) {
  1390. case 0:
  1391. return 350 * MHz;
  1392. case 1:
  1393. return 233 * MHz;
  1394. case 2:
  1395. return 116 * MHz;
  1396. case 3:
  1397. return OSC_HZ;
  1398. default:
  1399. return 0;
  1400. }
  1401. }
  1402. static int clk_scmi_crypto_core_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1403. {
  1404. uint32_t src;
  1405. if (rate >= 350 * MHz)
  1406. src = 0;
  1407. else if (rate >= 233 * MHz)
  1408. src = 1;
  1409. else if (rate >= 116 * MHz)
  1410. src = 2;
  1411. else
  1412. src = 3;
  1413. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1414. BITS_WITH_WMASK(src, 0x3U, 10));
  1415. return 0;
  1416. }
  1417. static int clk_scmi_crypto_core_set_status(rk_scmi_clock_t *clock, bool status)
  1418. {
  1419. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(0),
  1420. BITS_WITH_WMASK(!status, 0x1U, 15));
  1421. return 0;
  1422. }
  1423. static unsigned long clk_scmi_crypto_pka_get_rate(rk_scmi_clock_t *clock)
  1424. {
  1425. uint32_t src;
  1426. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(1)) & 0x3000;
  1427. src = src >> 12;
  1428. switch (src) {
  1429. case 0:
  1430. return 350 * MHz;
  1431. case 1:
  1432. return 233 * MHz;
  1433. case 2:
  1434. return 116 * MHz;
  1435. case 3:
  1436. return OSC_HZ;
  1437. default:
  1438. return 0;
  1439. }
  1440. }
  1441. static int clk_scmi_crypto_pka_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1442. {
  1443. uint32_t src;
  1444. if (rate >= 350 * MHz)
  1445. src = 0;
  1446. else if (rate >= 233 * MHz)
  1447. src = 1;
  1448. else if (rate >= 116 * MHz)
  1449. src = 2;
  1450. else
  1451. src = 3;
  1452. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(1),
  1453. BITS_WITH_WMASK(src, 0x3U, 12));
  1454. return 0;
  1455. }
  1456. static int clk_scmi_crypto_pka_set_status(rk_scmi_clock_t *clock, bool status)
  1457. {
  1458. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1459. BITS_WITH_WMASK(!status, 0x1U, 0));
  1460. return 0;
  1461. }
  1462. static unsigned long clk_scmi_spll_get_rate(rk_scmi_clock_t *clock)
  1463. {
  1464. uint32_t src;
  1465. src = mmio_read_32(BUSSCRU_BASE + CRU_MODE_CON0) & 0x3;
  1466. switch (src) {
  1467. case 0:
  1468. return OSC_HZ;
  1469. case 1:
  1470. return 702 * MHz;
  1471. case 2:
  1472. return 32768;
  1473. default:
  1474. return 0;
  1475. }
  1476. }
  1477. static int clk_scmi_spll_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1478. {
  1479. uint32_t src;
  1480. if (rate >= 700 * MHz)
  1481. src = 1;
  1482. else
  1483. src = 0;
  1484. mmio_write_32(BUSSCRU_BASE + CRU_MODE_CON0,
  1485. BITS_WITH_WMASK(0, 0x3U, 0));
  1486. mmio_write_32(BUSSCRU_BASE + CRU_PLL_CON(137),
  1487. BITS_WITH_WMASK(2, 0x7U, 6));
  1488. mmio_write_32(BUSSCRU_BASE + CRU_MODE_CON0,
  1489. BITS_WITH_WMASK(src, 0x3U, 0));
  1490. return 0;
  1491. }
  1492. static int clk_scmi_spll_set_status(rk_scmi_clock_t *clock, bool status)
  1493. {
  1494. return 0;
  1495. }
  1496. static unsigned long clk_scmi_hclk_sd_get_rate(rk_scmi_clock_t *clock)
  1497. {
  1498. return clk_scmi_hclk_secure_ns_get_rate(clock);
  1499. }
  1500. static int clk_scmi_hclk_sd_set_status(rk_scmi_clock_t *clock, bool status)
  1501. {
  1502. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(3),
  1503. BITS_WITH_WMASK(!status, 0x1U, 2));
  1504. return 0;
  1505. }
  1506. static unsigned long clk_scmi_crypto_rng_s_get_rate(rk_scmi_clock_t *clock)
  1507. {
  1508. uint32_t src;
  1509. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(2)) & 0x0030;
  1510. src = src >> 4;
  1511. switch (src) {
  1512. case 0:
  1513. return 175 * MHz;
  1514. case 1:
  1515. return 116 * MHz;
  1516. case 2:
  1517. return 58 * MHz;
  1518. case 3:
  1519. return OSC_HZ;
  1520. default:
  1521. return 0;
  1522. }
  1523. }
  1524. static int clk_scmi_crypto_rng_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1525. {
  1526. uint32_t src;
  1527. if (rate >= 175 * MHz)
  1528. src = 0;
  1529. else if (rate >= 116 * MHz)
  1530. src = 1;
  1531. else if (rate >= 58 * MHz)
  1532. src = 2;
  1533. else
  1534. src = 3;
  1535. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(2),
  1536. BITS_WITH_WMASK(src, 0x3U, 4));
  1537. return 0;
  1538. }
  1539. static int clk_scmi_crypto_rng_s_set_status(rk_scmi_clock_t *clock, bool status)
  1540. {
  1541. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1542. BITS_WITH_WMASK(!status, 0x1U, 6));
  1543. return 0;
  1544. }
  1545. static unsigned long clk_scmi_crypto_core_s_get_rate(rk_scmi_clock_t *clock)
  1546. {
  1547. uint32_t src;
  1548. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(2)) & 0x3;
  1549. src = src >> 0;
  1550. switch (src) {
  1551. case 0:
  1552. return 350 * MHz;
  1553. case 1:
  1554. return 233 * MHz;
  1555. case 2:
  1556. return 116 * MHz;
  1557. case 3:
  1558. return OSC_HZ;
  1559. default:
  1560. return 0;
  1561. }
  1562. }
  1563. static int clk_scmi_crypto_core_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1564. {
  1565. uint32_t src;
  1566. if (rate >= 350 * MHz)
  1567. src = 0;
  1568. else if (rate >= 233 * MHz)
  1569. src = 1;
  1570. else if (rate >= 116 * MHz)
  1571. src = 2;
  1572. else
  1573. src = 3;
  1574. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(2),
  1575. BITS_WITH_WMASK(src, 0x3U, 0));
  1576. return 0;
  1577. }
  1578. static int clk_scmi_crypto_core_s_set_status(rk_scmi_clock_t *clock, bool status)
  1579. {
  1580. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1581. BITS_WITH_WMASK(!status, 0x1U, 4));
  1582. return 0;
  1583. }
  1584. static unsigned long clk_scmi_crypto_pka_s_get_rate(rk_scmi_clock_t *clock)
  1585. {
  1586. uint32_t src;
  1587. src = mmio_read_32(SCRU_BASE + CRU_CLKSEL_CON(2)) & 0x000c;
  1588. src = src >> 2;
  1589. switch (src) {
  1590. case 0:
  1591. return 350 * MHz;
  1592. case 1:
  1593. return 233 * MHz;
  1594. case 2:
  1595. return 116 * MHz;
  1596. case 3:
  1597. return OSC_HZ;
  1598. default:
  1599. return 0;
  1600. }
  1601. }
  1602. static int clk_scmi_crypto_pka_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1603. {
  1604. uint32_t src;
  1605. if (rate >= 350 * MHz)
  1606. src = 0;
  1607. else if (rate >= 233 * MHz)
  1608. src = 1;
  1609. else if (rate >= 116 * MHz)
  1610. src = 2;
  1611. else
  1612. src = 3;
  1613. mmio_write_32(SCRU_BASE + CRU_CLKSEL_CON(2),
  1614. BITS_WITH_WMASK(src, 0x3U, 2));
  1615. return 0;
  1616. }
  1617. static int clk_scmi_crypto_pka_s_set_status(rk_scmi_clock_t *clock, bool status)
  1618. {
  1619. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1620. BITS_WITH_WMASK(!status, 0x1U, 5));
  1621. return 0;
  1622. }
  1623. static unsigned long clk_scmi_a_crypto_s_get_rate(rk_scmi_clock_t *clock)
  1624. {
  1625. return clk_scmi_aclk_secure_s_get_rate(clock);
  1626. }
  1627. static int clk_scmi_a_crypto_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1628. {
  1629. return clk_scmi_aclk_secure_s_set_rate(clock, rate);
  1630. }
  1631. static int clk_scmi_a_crypto_s_set_status(rk_scmi_clock_t *clock, bool status)
  1632. {
  1633. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1634. BITS_WITH_WMASK(!status, 0x1U, 7));
  1635. return 0;
  1636. }
  1637. static unsigned long clk_scmi_h_crypto_s_get_rate(rk_scmi_clock_t *clock)
  1638. {
  1639. return clk_scmi_hclk_secure_s_get_rate(clock);
  1640. }
  1641. static int clk_scmi_h_crypto_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1642. {
  1643. return clk_scmi_hclk_secure_s_set_rate(clock, rate);
  1644. }
  1645. static int clk_scmi_h_crypto_s_set_status(rk_scmi_clock_t *clock, bool status)
  1646. {
  1647. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1648. BITS_WITH_WMASK(!status, 0x1U, 8));
  1649. return 0;
  1650. }
  1651. static unsigned long clk_scmi_p_crypto_s_get_rate(rk_scmi_clock_t *clock)
  1652. {
  1653. return clk_scmi_pclk_secure_s_get_rate(clock);
  1654. }
  1655. static int clk_scmi_p_crypto_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1656. {
  1657. return clk_scmi_pclk_secure_s_set_rate(clock, rate);
  1658. }
  1659. static int clk_scmi_p_crypto_s_set_status(rk_scmi_clock_t *clock, bool status)
  1660. {
  1661. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(2),
  1662. BITS_WITH_WMASK(!status, 0x1U, 13));
  1663. return 0;
  1664. }
  1665. static unsigned long clk_scmi_a_keylad_s_get_rate(rk_scmi_clock_t *clock)
  1666. {
  1667. return clk_scmi_aclk_secure_s_get_rate(clock);
  1668. }
  1669. static int clk_scmi_a_keylad_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1670. {
  1671. return clk_scmi_aclk_secure_s_set_rate(clock, rate);
  1672. }
  1673. static int clk_scmi_a_keylad_s_set_status(rk_scmi_clock_t *clock, bool status)
  1674. {
  1675. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1676. BITS_WITH_WMASK(!status, 0x1U, 11));
  1677. return 0;
  1678. }
  1679. static unsigned long clk_scmi_h_keylad_s_get_rate(rk_scmi_clock_t *clock)
  1680. {
  1681. return clk_scmi_hclk_secure_s_get_rate(clock);
  1682. }
  1683. static int clk_scmi_h_keylad_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1684. {
  1685. return clk_scmi_hclk_secure_s_set_rate(clock, rate);
  1686. }
  1687. static int clk_scmi_h_keylad_s_set_status(rk_scmi_clock_t *clock, bool status)
  1688. {
  1689. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1690. BITS_WITH_WMASK(!status, 0x1U, 12));
  1691. return 0;
  1692. }
  1693. static unsigned long clk_scmi_p_keylad_s_get_rate(rk_scmi_clock_t *clock)
  1694. {
  1695. return clk_scmi_pclk_secure_s_get_rate(clock);
  1696. }
  1697. static int clk_scmi_p_keylad_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1698. {
  1699. return clk_scmi_pclk_secure_s_set_rate(clock, rate);
  1700. }
  1701. static int clk_scmi_p_keylad_s_set_status(rk_scmi_clock_t *clock, bool status)
  1702. {
  1703. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(2),
  1704. BITS_WITH_WMASK(!status, 0x1U, 14));
  1705. return 0;
  1706. }
  1707. static unsigned long clk_scmi_trng_s_get_rate(rk_scmi_clock_t *clock)
  1708. {
  1709. return clk_scmi_hclk_secure_s_get_rate(clock);
  1710. }
  1711. static int clk_scmi_trng_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1712. {
  1713. return clk_scmi_hclk_secure_s_set_rate(clock, rate);
  1714. }
  1715. static int clk_scmi_trng_s_set_status(rk_scmi_clock_t *clock, bool status)
  1716. {
  1717. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(3),
  1718. BITS_WITH_WMASK(!status, 0x1U, 6));
  1719. return 0;
  1720. }
  1721. static unsigned long clk_scmi_h_trng_s_get_rate(rk_scmi_clock_t *clock)
  1722. {
  1723. return clk_scmi_hclk_secure_s_get_rate(clock);
  1724. }
  1725. static int clk_scmi_h_trng_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1726. {
  1727. return clk_scmi_hclk_secure_s_set_rate(clock, rate);
  1728. }
  1729. static int clk_scmi_h_trng_s_set_status(rk_scmi_clock_t *clock, bool status)
  1730. {
  1731. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(2),
  1732. BITS_WITH_WMASK(!status, 0x1U, 15));
  1733. return 0;
  1734. }
  1735. static unsigned long clk_scmi_p_otpc_s_get_rate(rk_scmi_clock_t *clock)
  1736. {
  1737. return clk_scmi_pclk_secure_s_get_rate(clock);
  1738. }
  1739. static int clk_scmi_p_otpc_s_set_rate(rk_scmi_clock_t *clock, unsigned long rate)
  1740. {
  1741. return clk_scmi_pclk_secure_s_set_rate(clock, rate);
  1742. }
  1743. static int clk_scmi_p_otpc_s_set_status(rk_scmi_clock_t *clock, bool status)
  1744. {
  1745. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1746. BITS_WITH_WMASK(!status, 0x1U, 13));
  1747. return 0;
  1748. }
  1749. static unsigned long clk_scmi_otpc_s_get_rate(rk_scmi_clock_t *clock)
  1750. {
  1751. return OSC_HZ;
  1752. }
  1753. static int clk_scmi_otpc_s_set_status(rk_scmi_clock_t *clock, bool status)
  1754. {
  1755. mmio_write_32(SCRU_BASE + CRU_CLKGATE_CON(1),
  1756. BITS_WITH_WMASK(!status, 0x1U, 14));
  1757. return 0;
  1758. }
  1759. static unsigned long clk_scmi_otp_phy_get_rate(rk_scmi_clock_t *clock)
  1760. {
  1761. return OSC_HZ;
  1762. }
  1763. static int clk_scmi_otp_phy_set_status(rk_scmi_clock_t *clock, bool status)
  1764. {
  1765. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(18),
  1766. BITS_WITH_WMASK(!status, 0x1U, 13));
  1767. return 0;
  1768. }
  1769. static unsigned long clk_scmi_otpc_rd_get_rate(rk_scmi_clock_t *clock)
  1770. {
  1771. return OSC_HZ;
  1772. }
  1773. static int clk_scmi_otpc_rd_set_status(rk_scmi_clock_t *clock, bool status)
  1774. {
  1775. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(18),
  1776. BITS_WITH_WMASK(!status, 0x1U, 12));
  1777. return 0;
  1778. }
  1779. static unsigned long clk_scmi_otpc_arb_get_rate(rk_scmi_clock_t *clock)
  1780. {
  1781. return OSC_HZ;
  1782. }
  1783. static int clk_scmi_otpc_arb_set_status(rk_scmi_clock_t *clock, bool status)
  1784. {
  1785. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(18),
  1786. BITS_WITH_WMASK(!status, 0x1U, 11));
  1787. return 0;
  1788. }
  1789. static const struct rk_clk_ops clk_scmi_cpul_ops = {
  1790. .get_rate = clk_scmi_cpul_get_rate,
  1791. .set_rate = clk_scmi_cpul_set_rate,
  1792. .set_status = clk_scmi_cpul_set_status,
  1793. };
  1794. static const struct rk_clk_ops clk_scmi_dsu_ops = {
  1795. .get_rate = clk_scmi_dsu_get_rate,
  1796. .set_rate = clk_scmi_dsu_set_rate,
  1797. .set_status = clk_scmi_dsu_set_status,
  1798. };
  1799. static const struct rk_clk_ops clk_scmi_cpub01_ops = {
  1800. .get_rate = clk_scmi_cpub01_get_rate,
  1801. .set_rate = clk_scmi_cpub01_set_rate,
  1802. .set_status = clk_scmi_cpub01_set_status,
  1803. };
  1804. static const struct rk_clk_ops clk_scmi_cpub23_ops = {
  1805. .get_rate = clk_scmi_cpub23_get_rate,
  1806. .set_rate = clk_scmi_cpub23_set_rate,
  1807. .set_status = clk_scmi_cpub23_set_status,
  1808. };
  1809. static const struct rk_clk_ops clk_scmi_gpu_ops = {
  1810. .get_rate = clk_scmi_gpu_get_rate,
  1811. .set_rate = clk_scmi_gpu_set_rate,
  1812. .set_status = clk_scmi_gpu_set_status,
  1813. };
  1814. static const struct rk_clk_ops clk_scmi_npu_ops = {
  1815. .get_rate = clk_scmi_npu_get_rate,
  1816. .set_rate = clk_scmi_npu_set_rate,
  1817. .set_status = clk_scmi_npu_set_status,
  1818. };
  1819. static const struct rk_clk_ops clk_scmi_sbus_ops = {
  1820. .get_rate = clk_scmi_sbus_get_rate,
  1821. .set_rate = clk_scmi_sbus_set_rate,
  1822. .set_status = clk_scmi_sbus_set_status,
  1823. };
  1824. static const struct rk_clk_ops clk_scmi_pclk_sbus_ops = {
  1825. .get_rate = clk_scmi_pclk_sbus_get_rate,
  1826. .set_rate = clk_scmi_pclk_sbus_set_rate,
  1827. .set_status = clk_scmi_pclk_sbus_set_status,
  1828. };
  1829. static const struct rk_clk_ops clk_scmi_cclk_sdmmc_ops = {
  1830. .get_rate = clk_scmi_cclk_sdmmc_get_rate,
  1831. .set_rate = clk_scmi_cclk_sdmmc_set_rate,
  1832. .set_status = clk_scmi_cclk_sdmmc_set_status,
  1833. };
  1834. static const struct rk_clk_ops clk_scmi_dclk_sdmmc_ops = {
  1835. .get_rate = clk_scmi_dclk_sdmmc_get_rate,
  1836. .set_rate = clk_scmi_dclk_sdmmc_set_rate,
  1837. .set_status = clk_scmi_dclk_sdmmc_set_status,
  1838. };
  1839. static const struct rk_clk_ops clk_scmi_aclk_secure_ns_ops = {
  1840. .get_rate = clk_scmi_aclk_secure_ns_get_rate,
  1841. .set_rate = clk_scmi_aclk_secure_ns_set_rate,
  1842. .set_status = clk_scmi_aclk_secure_ns_set_status,
  1843. };
  1844. static const struct rk_clk_ops clk_scmi_hclk_secure_ns_ops = {
  1845. .get_rate = clk_scmi_hclk_secure_ns_get_rate,
  1846. .set_rate = clk_scmi_hclk_secure_ns_set_rate,
  1847. .set_status = clk_scmi_hclk_secure_ns_set_status,
  1848. };
  1849. static const struct rk_clk_ops clk_scmi_tclk_wdt_ops = {
  1850. .get_rate = clk_scmi_tclk_wdt_get_rate,
  1851. .set_status = clk_scmi_tclk_wdt_set_status,
  1852. };
  1853. static const struct rk_clk_ops clk_scmi_keyladder_core_ops = {
  1854. .get_rate = clk_scmi_keyladder_core_get_rate,
  1855. .set_rate = clk_scmi_keyladder_core_set_rate,
  1856. .set_status = clk_scmi_keyladder_core_set_status,
  1857. };
  1858. static const struct rk_clk_ops clk_scmi_keyladder_rng_ops = {
  1859. .get_rate = clk_scmi_keyladder_rng_get_rate,
  1860. .set_rate = clk_scmi_keyladder_rng_set_rate,
  1861. .set_status = clk_scmi_keyladder_rng_set_status,
  1862. };
  1863. static const struct rk_clk_ops clk_scmi_aclk_secure_s_ops = {
  1864. .get_rate = clk_scmi_aclk_secure_s_get_rate,
  1865. .set_rate = clk_scmi_aclk_secure_s_set_rate,
  1866. .set_status = clk_scmi_aclk_secure_s_set_status,
  1867. };
  1868. static const struct rk_clk_ops clk_scmi_hclk_secure_s_ops = {
  1869. .get_rate = clk_scmi_hclk_secure_s_get_rate,
  1870. .set_rate = clk_scmi_hclk_secure_s_set_rate,
  1871. .set_status = clk_scmi_hclk_secure_s_set_status,
  1872. };
  1873. static const struct rk_clk_ops clk_scmi_pclk_secure_s_ops = {
  1874. .get_rate = clk_scmi_pclk_secure_s_get_rate,
  1875. .set_rate = clk_scmi_pclk_secure_s_set_rate,
  1876. .set_status = clk_scmi_pclk_secure_s_set_status,
  1877. };
  1878. static const struct rk_clk_ops clk_scmi_crypto_rng_ops = {
  1879. .get_rate = clk_scmi_crypto_rng_get_rate,
  1880. .set_rate = clk_scmi_crypto_rng_set_rate,
  1881. .set_status = clk_scmi_crypto_rng_set_status,
  1882. };
  1883. static const struct rk_clk_ops clk_scmi_crypto_core_ops = {
  1884. .get_rate = clk_scmi_crypto_core_get_rate,
  1885. .set_rate = clk_scmi_crypto_core_set_rate,
  1886. .set_status = clk_scmi_crypto_core_set_status,
  1887. };
  1888. static const struct rk_clk_ops clk_scmi_crypto_pka_ops = {
  1889. .get_rate = clk_scmi_crypto_pka_get_rate,
  1890. .set_rate = clk_scmi_crypto_pka_set_rate,
  1891. .set_status = clk_scmi_crypto_pka_set_status,
  1892. };
  1893. static const struct rk_clk_ops clk_scmi_spll_ops = {
  1894. .get_rate = clk_scmi_spll_get_rate,
  1895. .set_rate = clk_scmi_spll_set_rate,
  1896. .set_status = clk_scmi_spll_set_status,
  1897. };
  1898. static const struct rk_clk_ops clk_scmi_hclk_sd_ops = {
  1899. .get_rate = clk_scmi_hclk_sd_get_rate,
  1900. .set_status = clk_scmi_hclk_sd_set_status,
  1901. };
  1902. static const struct rk_clk_ops clk_scmi_crypto_rng_s_ops = {
  1903. .get_rate = clk_scmi_crypto_rng_s_get_rate,
  1904. .set_rate = clk_scmi_crypto_rng_s_set_rate,
  1905. .set_status = clk_scmi_crypto_rng_s_set_status,
  1906. };
  1907. static const struct rk_clk_ops clk_scmi_crypto_core_s_ops = {
  1908. .get_rate = clk_scmi_crypto_core_s_get_rate,
  1909. .set_rate = clk_scmi_crypto_core_s_set_rate,
  1910. .set_status = clk_scmi_crypto_core_s_set_status,
  1911. };
  1912. static const struct rk_clk_ops clk_scmi_crypto_pka_s_ops = {
  1913. .get_rate = clk_scmi_crypto_pka_s_get_rate,
  1914. .set_rate = clk_scmi_crypto_pka_s_set_rate,
  1915. .set_status = clk_scmi_crypto_pka_s_set_status,
  1916. };
  1917. static const struct rk_clk_ops clk_scmi_a_crypto_s_ops = {
  1918. .get_rate = clk_scmi_a_crypto_s_get_rate,
  1919. .set_rate = clk_scmi_a_crypto_s_set_rate,
  1920. .set_status = clk_scmi_a_crypto_s_set_status,
  1921. };
  1922. static const struct rk_clk_ops clk_scmi_h_crypto_s_ops = {
  1923. .get_rate = clk_scmi_h_crypto_s_get_rate,
  1924. .set_rate = clk_scmi_h_crypto_s_set_rate,
  1925. .set_status = clk_scmi_h_crypto_s_set_status,
  1926. };
  1927. static const struct rk_clk_ops clk_scmi_p_crypto_s_ops = {
  1928. .get_rate = clk_scmi_p_crypto_s_get_rate,
  1929. .set_rate = clk_scmi_p_crypto_s_set_rate,
  1930. .set_status = clk_scmi_p_crypto_s_set_status,
  1931. };
  1932. static const struct rk_clk_ops clk_scmi_a_keylad_s_ops = {
  1933. .get_rate = clk_scmi_a_keylad_s_get_rate,
  1934. .set_rate = clk_scmi_a_keylad_s_set_rate,
  1935. .set_status = clk_scmi_a_keylad_s_set_status,
  1936. };
  1937. static const struct rk_clk_ops clk_scmi_h_keylad_s_ops = {
  1938. .get_rate = clk_scmi_h_keylad_s_get_rate,
  1939. .set_rate = clk_scmi_h_keylad_s_set_rate,
  1940. .set_status = clk_scmi_h_keylad_s_set_status,
  1941. };
  1942. static const struct rk_clk_ops clk_scmi_p_keylad_s_ops = {
  1943. .get_rate = clk_scmi_p_keylad_s_get_rate,
  1944. .set_rate = clk_scmi_p_keylad_s_set_rate,
  1945. .set_status = clk_scmi_p_keylad_s_set_status,
  1946. };
  1947. static const struct rk_clk_ops clk_scmi_trng_s_ops = {
  1948. .get_rate = clk_scmi_trng_s_get_rate,
  1949. .set_rate = clk_scmi_trng_s_set_rate,
  1950. .set_status = clk_scmi_trng_s_set_status,
  1951. };
  1952. static const struct rk_clk_ops clk_scmi_h_trng_s_ops = {
  1953. .get_rate = clk_scmi_h_trng_s_get_rate,
  1954. .set_rate = clk_scmi_h_trng_s_set_rate,
  1955. .set_status = clk_scmi_h_trng_s_set_status,
  1956. };
  1957. static const struct rk_clk_ops clk_scmi_p_otpc_s_ops = {
  1958. .get_rate = clk_scmi_p_otpc_s_get_rate,
  1959. .set_rate = clk_scmi_p_otpc_s_set_rate,
  1960. .set_status = clk_scmi_p_otpc_s_set_status,
  1961. };
  1962. static const struct rk_clk_ops clk_scmi_otpc_s_ops = {
  1963. .get_rate = clk_scmi_otpc_s_get_rate,
  1964. .set_status = clk_scmi_otpc_s_set_status,
  1965. };
  1966. static const struct rk_clk_ops clk_scmi_otp_phy_ops = {
  1967. .get_rate = clk_scmi_otp_phy_get_rate,
  1968. .set_status = clk_scmi_otp_phy_set_status,
  1969. };
  1970. static const struct rk_clk_ops clk_scmi_otpc_rd_ops = {
  1971. .get_rate = clk_scmi_otpc_rd_get_rate,
  1972. .set_status = clk_scmi_otpc_rd_set_status,
  1973. };
  1974. static const struct rk_clk_ops clk_scmi_otpc_arb_ops = {
  1975. .get_rate = clk_scmi_otpc_arb_get_rate,
  1976. .set_status = clk_scmi_otpc_arb_set_status,
  1977. };
  1978. rk_scmi_clock_t clock_table[] = {
  1979. RK3588_SCMI_CLOCK(SCMI_CLK_CPUL, "scmi_clk_cpul", &clk_scmi_cpul_ops, rk3588_cpul_rates, ARRAY_SIZE(rk3588_cpul_rates), false),
  1980. RK3588_SCMI_CLOCK(SCMI_CLK_DSU, "scmi_clk_dsu", &clk_scmi_dsu_ops, rk3588_cpul_rates, ARRAY_SIZE(rk3588_cpul_rates), false),
  1981. RK3588_SCMI_CLOCK(SCMI_CLK_CPUB01, "scmi_clk_cpub01", &clk_scmi_cpub01_ops, rk3588_cpub_rates, ARRAY_SIZE(rk3588_cpub_rates), false),
  1982. RK3588_SCMI_CLOCK(SCMI_CLK_CPUB23, "scmi_clk_cpub23", &clk_scmi_cpub23_ops, rk3588_cpub_rates, ARRAY_SIZE(rk3588_cpub_rates), false),
  1983. RK3588_SCMI_CLOCK(SCMI_CLK_DDR, "scmi_clk_ddr", NULL, NULL, 0, false),
  1984. RK3588_SCMI_CLOCK(SCMI_CLK_GPU, "scmi_clk_gpu", &clk_scmi_gpu_ops, rk3588_gpu_rates, ARRAY_SIZE(rk3588_gpu_rates), false),
  1985. RK3588_SCMI_CLOCK(SCMI_CLK_NPU, "scmi_clk_npu", &clk_scmi_npu_ops, rk3588_gpu_rates, ARRAY_SIZE(rk3588_gpu_rates), false),
  1986. RK3588_SCMI_CLOCK(SCMI_CLK_SBUS, "scmi_clk_sbus", &clk_scmi_sbus_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1987. RK3588_SCMI_CLOCK(SCMI_PCLK_SBUS, "scmi_pclk_sbus", &clk_scmi_pclk_sbus_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1988. RK3588_SCMI_CLOCK(SCMI_CCLK_SD, "scmi_cclk_sd", &clk_scmi_cclk_sdmmc_ops, rk3588_sdmmc_rates, ARRAY_SIZE(rk3588_sdmmc_rates), false),
  1989. RK3588_SCMI_CLOCK(SCMI_DCLK_SD, "scmi_dclk_sd", &clk_scmi_dclk_sdmmc_ops, rk3588_sdmmc_rates, ARRAY_SIZE(rk3588_sdmmc_rates), false),
  1990. RK3588_SCMI_CLOCK(SCMI_ACLK_SECURE_NS, "scmi_aclk_se_ns", &clk_scmi_aclk_secure_ns_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), false),
  1991. RK3588_SCMI_CLOCK(SCMI_HCLK_SECURE_NS, "scmi_hclk_se_ns", &clk_scmi_hclk_secure_ns_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), false),
  1992. RK3588_SCMI_CLOCK(SCMI_TCLK_WDT, "scmi_tclk_wdt", &clk_scmi_tclk_wdt_ops, NULL, 0, false),
  1993. RK3588_SCMI_CLOCK(SCMI_KEYLADDER_CORE, "scmi_keylad_c", &clk_scmi_keyladder_core_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1994. RK3588_SCMI_CLOCK(SCMI_KEYLADDER_RNG, "scmi_keylad_r", &clk_scmi_keyladder_rng_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1995. RK3588_SCMI_CLOCK(SCMI_ACLK_SECURE_S, "scmi_aclk_se_s", &clk_scmi_aclk_secure_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1996. RK3588_SCMI_CLOCK(SCMI_HCLK_SECURE_S, "scmi_hclk_se_s", &clk_scmi_hclk_secure_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1997. RK3588_SCMI_CLOCK(SCMI_PCLK_SECURE_S, "scmi_pclk_se_s", &clk_scmi_pclk_secure_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  1998. RK3588_SCMI_CLOCK(SCMI_CRYPTO_RNG, "scmi_crypto_r", &clk_scmi_crypto_rng_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), false),
  1999. RK3588_SCMI_CLOCK(SCMI_CRYPTO_CORE, "scmi_crypto_c", &clk_scmi_crypto_core_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), false),
  2000. RK3588_SCMI_CLOCK(SCMI_CRYPTO_PKA, "scmi_crypto_p", &clk_scmi_crypto_pka_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), false),
  2001. RK3588_SCMI_CLOCK(SCMI_SPLL, "scmi_spll", &clk_scmi_spll_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), false),
  2002. RK3588_SCMI_CLOCK(SCMI_HCLK_SD, "scmi_hclk_sd", &clk_scmi_hclk_sd_ops, NULL, 0, false),
  2003. RK3588_SCMI_CLOCK(SCMI_CRYPTO_RNG_S, "scmi_crypto_r_s", &clk_scmi_crypto_rng_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2004. RK3588_SCMI_CLOCK(SCMI_CRYPTO_CORE_S, "scmi_crypto_c_s", &clk_scmi_crypto_core_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2005. RK3588_SCMI_CLOCK(SCMI_CRYPTO_PKA_S, "scmi_crypto_p_s", &clk_scmi_crypto_pka_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2006. RK3588_SCMI_CLOCK(SCMI_A_CRYPTO_S, "scmi_a_crypto_s", &clk_scmi_a_crypto_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2007. RK3588_SCMI_CLOCK(SCMI_H_CRYPTO_S, "scmi_h_crypto_s", &clk_scmi_h_crypto_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2008. RK3588_SCMI_CLOCK(SCMI_P_CRYPTO_S, "scmi_p_crypto_s", &clk_scmi_p_crypto_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2009. RK3588_SCMI_CLOCK(SCMI_A_KEYLADDER_S, "scmi_a_keylad_s", &clk_scmi_a_keylad_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2010. RK3588_SCMI_CLOCK(SCMI_H_KEYLADDER_S, "scmi_h_keylad_s", &clk_scmi_h_keylad_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2011. RK3588_SCMI_CLOCK(SCMI_P_KEYLADDER_S, "scmi_p_keylad_s", &clk_scmi_p_keylad_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2012. RK3588_SCMI_CLOCK(SCMI_TRNG_S, "scmi_trng_s", &clk_scmi_trng_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2013. RK3588_SCMI_CLOCK(SCMI_H_TRNG_S, "scmi_h_trng_s", &clk_scmi_h_trng_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2014. RK3588_SCMI_CLOCK(SCMI_P_OTPC_S, "scmi_p_otpc_s", &clk_scmi_p_otpc_s_ops, rk3588_sbus_rates, ARRAY_SIZE(rk3588_sbus_rates), true),
  2015. RK3588_SCMI_CLOCK(SCMI_OTPC_S, "scmi_otpc_s", &clk_scmi_otpc_s_ops, NULL, 0, true),
  2016. RK3588_SCMI_CLOCK(SCMI_OTP_PHY, "scmi_otp_phy", &clk_scmi_otp_phy_ops, NULL, 0, false),
  2017. RK3588_SCMI_CLOCK(SCMI_OTPC_AUTO_RD, "scmi_otpc_rd", &clk_scmi_otpc_rd_ops, NULL, 0, false),
  2018. RK3588_SCMI_CLOCK(SCMI_OTPC_ARB, "scmi_otpc_arb", &clk_scmi_otpc_arb_ops, NULL, 0, false),
  2019. };
  2020. size_t rockchip_scmi_clock_count(unsigned int agent_id __unused)
  2021. {
  2022. return ARRAY_SIZE(clock_table);
  2023. }
  2024. rk_scmi_clock_t *rockchip_scmi_get_clock(uint32_t agent_id __unused,
  2025. uint32_t clock_id)
  2026. {
  2027. rk_scmi_clock_t *table = NULL;
  2028. if (clock_id < ARRAY_SIZE(clock_table))
  2029. table = &clock_table[clock_id];
  2030. if (table && !table->is_security)
  2031. return table;
  2032. else
  2033. return NULL;
  2034. }
  2035. void pvtplls_suspend(void)
  2036. {
  2037. clk_cpul_set_rate(408000000, PLL_SEL_NOR);
  2038. clk_dsu_set_rate(408000000, PLL_SEL_NOR);
  2039. clk_cpub01_set_rate(408000000, PLL_SEL_NOR);
  2040. clk_cpub23_set_rate(408000000, PLL_SEL_NOR);
  2041. }
  2042. void pvtplls_resume(void)
  2043. {
  2044. clk_cpul_set_rate(sys_clk_info.cpul_rate, PLL_SEL_AUTO);
  2045. clk_dsu_set_rate(sys_clk_info.dsu_rate, PLL_SEL_AUTO);
  2046. clk_cpub01_set_rate(sys_clk_info.cpub01_rate, PLL_SEL_AUTO);
  2047. clk_cpub23_set_rate(sys_clk_info.cpub23_rate, PLL_SEL_AUTO);
  2048. }
  2049. void sys_reset_pvtplls_prepare(void)
  2050. {
  2051. clk_gpu_set_rate(100000000, PLL_SEL_NOR);
  2052. clk_npu_set_rate(100000000, PLL_SEL_NOR);
  2053. clk_cpul_set_rate(408000000, PLL_SEL_NOR);
  2054. clk_cpub01_set_rate(408000000, PLL_SEL_NOR);
  2055. clk_cpub23_set_rate(408000000, PLL_SEL_NOR);
  2056. clk_dsu_set_rate(408000000, PLL_SEL_NOR);
  2057. }
  2058. void rockchip_clock_init(void)
  2059. {
  2060. /* set gpll src div to 0 for cpul */
  2061. mmio_write_32(DSUCRU_BASE + CRU_CLKSEL_CON(5), CLKDIV_5BITS_SHF(0U, 9));
  2062. /* set gpll src div to 0 for cpub01 */
  2063. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(0),
  2064. CLKDIV_5BITS_SHF(0U, 1));
  2065. /* set gpll src div to 0 for cpu23 */
  2066. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(0),
  2067. CLKDIV_5BITS_SHF(0U, 1));
  2068. mmio_write_32(BIGCORE0CRU_BASE + CRU_CLKSEL_CON(2),
  2069. CPUB_PCLK_PATH_50M);
  2070. mmio_write_32(BIGCORE1CRU_BASE + CRU_CLKSEL_CON(2),
  2071. CPUB_PCLK_PATH_50M);
  2072. mmio_write_32(DSUCRU_BASE + DSUCRU_CLKSEL_CON(4),
  2073. CLKDIV_5BITS_SHF(5U, 0));
  2074. mmio_write_32(DSUCRU_BASE + DSUCRU_CLKSEL_CON(4),
  2075. BITS_WITH_WMASK(PCLK_DSU_ROOT_SEL_GPLL,
  2076. PCLK_DSU_ROOT_SEL_MASK,
  2077. PCLK_DSU_ROOT_SEL_SHIFT));
  2078. sys_clk_info.cpul_table = rk3588_cpul_pvtpll_table;
  2079. sys_clk_info.cpul_rate_count = ARRAY_SIZE(rk3588_cpul_pvtpll_table);
  2080. sys_clk_info.cpub01_table = rk3588_cpub0_pvtpll_table;
  2081. sys_clk_info.cpub01_rate_count = ARRAY_SIZE(rk3588_cpub0_pvtpll_table);
  2082. sys_clk_info.cpub23_table = rk3588_cpub1_pvtpll_table;
  2083. sys_clk_info.cpub23_rate_count = ARRAY_SIZE(rk3588_cpub1_pvtpll_table);
  2084. memcpy(sys_clk_info.cpub23_table, sys_clk_info.cpub01_table,
  2085. sys_clk_info.cpub01_rate_count * sizeof(*sys_clk_info.cpub01_table));
  2086. sys_clk_info.gpu_table = rk3588_gpu_pvtpll_table;
  2087. sys_clk_info.gpu_rate_count = ARRAY_SIZE(rk3588_gpu_pvtpll_table);
  2088. sys_clk_info.npu_table = rk3588_npu_pvtpll_table;
  2089. sys_clk_info.npu_rate_count = ARRAY_SIZE(rk3588_npu_pvtpll_table);
  2090. }