stm32mp1_clk.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  1. /*
  2. * Copyright (C) 2018-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <arch.h>
  11. #include <arch_helpers.h>
  12. #include <common/debug.h>
  13. #include <common/fdt_wrappers.h>
  14. #include <drivers/clk.h>
  15. #include <drivers/delay_timer.h>
  16. #include <drivers/st/stm32mp_clkfunc.h>
  17. #include <drivers/st/stm32mp1_clk.h>
  18. #include <drivers/st/stm32mp1_rcc.h>
  19. #include <dt-bindings/clock/stm32mp1-clksrc.h>
  20. #include <lib/mmio.h>
  21. #include <lib/spinlock.h>
  22. #include <lib/utils_def.h>
  23. #include <libfdt.h>
  24. #include <plat/common/platform.h>
  25. #include <platform_def.h>
  26. enum stm32mp1_pllcfg {
  27. PLLCFG_M,
  28. PLLCFG_N,
  29. PLL_DIV_MN_NB,
  30. PLLCFG_P = PLL_DIV_MN_NB,
  31. PLLCFG_Q,
  32. PLLCFG_R,
  33. PLLCFG_O,
  34. PLLCFG_NB
  35. };
  36. #define PLL_DIV_MN_NB 2
  37. #define PLL_DIV_PQR_NB 3
  38. enum stm32mp1_pllcsg {
  39. PLLCSG_MOD_PER,
  40. PLLCSG_INC_STEP,
  41. PLLCSG_SSCG_MODE,
  42. PLLCSG_NB
  43. };
  44. struct stm32_pll_dt_cfg {
  45. bool status;
  46. uint32_t src;
  47. uint32_t cfg[PLLCFG_NB];
  48. uint32_t frac;
  49. bool csg_enabled;
  50. uint32_t csg[PLLCSG_NB];
  51. };
  52. struct stm32_clk_platdata {
  53. uint32_t npll;
  54. struct stm32_pll_dt_cfg *pll;
  55. uint32_t nclksrc;
  56. uint32_t *clksrc;
  57. uint32_t nclkdiv;
  58. uint32_t *clkdiv;
  59. bool lse_css;
  60. };
  61. struct stm32_clk_priv {
  62. uintptr_t base;
  63. const struct mux_cfg *parents;
  64. const uint32_t nb_parents;
  65. const struct div_cfg *div;
  66. const uint32_t nb_div;
  67. void *pdata;
  68. };
  69. static struct stm32_clk_priv *stm32_clock_data;
  70. static struct stm32_clk_priv *clk_stm32_get_priv(void)
  71. {
  72. return stm32_clock_data;
  73. }
  74. static int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base)
  75. {
  76. stm32_clock_data = priv;
  77. priv->base = base;
  78. return 0;
  79. }
  80. #define MAX_HSI_HZ 64000000
  81. #define USB_PHY_48_MHZ 48000000
  82. #define TIMEOUT_US_200MS U(200000)
  83. #define TIMEOUT_US_1S U(1000000)
  84. #define PLLRDY_TIMEOUT TIMEOUT_US_200MS
  85. #define CLKSRC_TIMEOUT TIMEOUT_US_200MS
  86. #define CLKDIV_TIMEOUT TIMEOUT_US_200MS
  87. #define HSIDIV_TIMEOUT TIMEOUT_US_200MS
  88. #define OSCRDY_TIMEOUT TIMEOUT_US_1S
  89. struct mux_cfg {
  90. uint16_t offset;
  91. uint8_t shift;
  92. uint8_t width;
  93. uint8_t bitrdy;
  94. };
  95. struct div_cfg {
  96. uint16_t offset;
  97. uint8_t shift;
  98. uint8_t width;
  99. uint8_t bitrdy;
  100. };
  101. #define DIV_NO_BIT_RDY UINT8_MAX
  102. #define DIV_CFG(_id, _offset, _shift, _width, _bitrdy)\
  103. [(_id)] = {\
  104. .offset = (_offset),\
  105. .shift = (_shift),\
  106. .width = (_width),\
  107. .bitrdy = (_bitrdy),\
  108. }
  109. static const struct div_cfg dividers_mp15[] = {
  110. DIV_CFG(DIV_MPU, RCC_MPCKDIVR, 0, 4, 31),
  111. DIV_CFG(DIV_AXI, RCC_AXIDIVR, 0, 3, 31),
  112. DIV_CFG(DIV_MCU, RCC_MCUDIVR, 0, 4, 31),
  113. DIV_CFG(DIV_APB1, RCC_APB1DIVR, 0, 3, 31),
  114. DIV_CFG(DIV_APB2, RCC_APB2DIVR, 0, 3, 31),
  115. DIV_CFG(DIV_APB3, RCC_APB3DIVR, 0, 3, 31),
  116. DIV_CFG(DIV_APB4, RCC_APB4DIVR, 0, 3, 31),
  117. DIV_CFG(DIV_APB5, RCC_APB5DIVR, 0, 3, 31),
  118. DIV_CFG(DIV_RTC, RCC_RTCDIVR, 0, 6, DIV_NO_BIT_RDY),
  119. DIV_CFG(DIV_MCO1, RCC_MCO1CFGR, 4, 4, DIV_NO_BIT_RDY),
  120. DIV_CFG(DIV_MCO2, RCC_MCO2CFGR, 4, 4, DIV_NO_BIT_RDY),
  121. DIV_CFG(DIV_TRACE, RCC_DBGCFGR, 0, 3, DIV_NO_BIT_RDY),
  122. DIV_CFG(DIV_ETHPTP, RCC_ETHCKSELR, 4, 4, DIV_NO_BIT_RDY),
  123. };
  124. /*
  125. * MUX CONFIG
  126. */
  127. #define MUX_NO_BIT_RDY UINT8_MAX
  128. #define MUXRDY_CFG(_id, _offset, _shift, _width, _bitrdy)\
  129. [(_id)] = {\
  130. .offset = (_offset),\
  131. .shift = (_shift),\
  132. .width = (_width),\
  133. .bitrdy = (_bitrdy),\
  134. }
  135. #define MUX_CFG(_id, _offset, _shift, _width)\
  136. MUXRDY_CFG(_id, _offset, _shift, _width, MUX_NO_BIT_RDY)
  137. static const struct mux_cfg parent_mp15[MUX_NB] = {
  138. MUX_CFG(MUX_PLL12, RCC_RCK12SELR, 0, 2),
  139. MUX_CFG(MUX_PLL3, RCC_RCK3SELR, 0, 2),
  140. MUX_CFG(MUX_PLL4, RCC_RCK4SELR, 0, 2),
  141. MUX_CFG(MUX_CKPER, RCC_CPERCKSELR, 0, 2),
  142. MUXRDY_CFG(MUX_MPU, RCC_MPCKSELR, 0, 2, 31),
  143. MUXRDY_CFG(MUX_AXI, RCC_ASSCKSELR, 0, 3, 31),
  144. MUXRDY_CFG(MUX_MCU, RCC_MSSCKSELR, 0, 2, 31),
  145. MUX_CFG(MUX_RTC, RCC_BDCR, 16, 2),
  146. MUX_CFG(MUX_SDMMC12, RCC_SDMMC12CKSELR, 0, 3),
  147. MUX_CFG(MUX_SPI2S23, RCC_SPI2S23CKSELR, 0, 3),
  148. MUX_CFG(MUX_SPI45, RCC_SPI45CKSELR, 0, 3),
  149. MUX_CFG(MUX_I2C12, RCC_I2C12CKSELR, 0, 3),
  150. MUX_CFG(MUX_I2C35, RCC_I2C35CKSELR, 0, 3),
  151. MUX_CFG(MUX_LPTIM23, RCC_LPTIM23CKSELR, 0, 3),
  152. MUX_CFG(MUX_LPTIM45, RCC_LPTIM45CKSELR, 0, 3),
  153. MUX_CFG(MUX_UART24, RCC_UART24CKSELR, 0, 3),
  154. MUX_CFG(MUX_UART35, RCC_UART35CKSELR, 0, 3),
  155. MUX_CFG(MUX_UART78, RCC_UART78CKSELR, 0, 3),
  156. MUX_CFG(MUX_SAI1, RCC_SAI1CKSELR, 0, 3),
  157. MUX_CFG(MUX_ETH, RCC_ETHCKSELR, 0, 2),
  158. MUX_CFG(MUX_I2C46, RCC_I2C46CKSELR, 0, 3),
  159. MUX_CFG(MUX_RNG2, RCC_RNG2CKSELR, 0, 2),
  160. MUX_CFG(MUX_SDMMC3, RCC_SDMMC3CKSELR, 0, 3),
  161. MUX_CFG(MUX_FMC, RCC_FMCCKSELR, 0, 2),
  162. MUX_CFG(MUX_QSPI, RCC_QSPICKSELR, 0, 2),
  163. MUX_CFG(MUX_USBPHY, RCC_USBCKSELR, 0, 2),
  164. MUX_CFG(MUX_USBO, RCC_USBCKSELR, 4, 1),
  165. MUX_CFG(MUX_SPDIF, RCC_SPDIFCKSELR, 0, 2),
  166. MUX_CFG(MUX_SPI2S1, RCC_SPI2S1CKSELR, 0, 3),
  167. MUX_CFG(MUX_CEC, RCC_CECCKSELR, 0, 2),
  168. MUX_CFG(MUX_LPTIM1, RCC_LPTIM1CKSELR, 0, 3),
  169. MUX_CFG(MUX_UART6, RCC_UART6CKSELR, 0, 3),
  170. MUX_CFG(MUX_FDCAN, RCC_FDCANCKSELR, 0, 2),
  171. MUX_CFG(MUX_SAI2, RCC_SAI2CKSELR, 0, 3),
  172. MUX_CFG(MUX_SAI3, RCC_SAI3CKSELR, 0, 3),
  173. MUX_CFG(MUX_SAI4, RCC_SAI4CKSELR, 0, 3),
  174. MUX_CFG(MUX_ADC, RCC_ADCCKSELR, 0, 2),
  175. MUX_CFG(MUX_DSI, RCC_DSICKSELR, 0, 1),
  176. MUX_CFG(MUX_RNG1, RCC_RNG1CKSELR, 0, 2),
  177. MUX_CFG(MUX_STGEN, RCC_STGENCKSELR, 0, 2),
  178. MUX_CFG(MUX_UART1, RCC_UART1CKSELR, 0, 3),
  179. MUX_CFG(MUX_SPI6, RCC_SPI6CKSELR, 0, 3),
  180. MUX_CFG(MUX_MCO1, RCC_MCO1CFGR, 0, 3),
  181. MUX_CFG(MUX_MCO2, RCC_MCO2CFGR, 0, 3),
  182. };
  183. #define MASK_WIDTH_SHIFT(_width, _shift) \
  184. GENMASK(((_width) + (_shift) - 1U), (_shift))
  185. int clk_mux_get_parent(struct stm32_clk_priv *priv, uint32_t mux_id)
  186. {
  187. const struct mux_cfg *mux;
  188. uint32_t mask;
  189. if (mux_id >= priv->nb_parents) {
  190. panic();
  191. }
  192. mux = &priv->parents[mux_id];
  193. mask = MASK_WIDTH_SHIFT(mux->width, mux->shift);
  194. return (mmio_read_32(priv->base + mux->offset) & mask) >> mux->shift;
  195. }
  196. static int clk_mux_set_parent(struct stm32_clk_priv *priv, uint16_t pid, uint8_t sel)
  197. {
  198. const struct mux_cfg *mux = &priv->parents[pid];
  199. uintptr_t address = priv->base + mux->offset;
  200. uint32_t mask;
  201. uint64_t timeout;
  202. mask = MASK_WIDTH_SHIFT(mux->width, mux->shift);
  203. mmio_clrsetbits_32(address, mask, (sel << mux->shift) & mask);
  204. if (mux->bitrdy == MUX_NO_BIT_RDY) {
  205. return 0;
  206. }
  207. timeout = timeout_init_us(CLKSRC_TIMEOUT);
  208. mask = BIT(mux->bitrdy);
  209. while ((mmio_read_32(address) & mask) == 0U) {
  210. if (timeout_elapsed(timeout)) {
  211. return -ETIMEDOUT;
  212. }
  213. }
  214. return 0;
  215. }
  216. static int stm32_clk_configure_mux(struct stm32_clk_priv *priv, uint32_t val)
  217. {
  218. uint32_t data = val & CMD_DATA_MASK;
  219. int mux = (data & MUX_ID_MASK) >> MUX_ID_SHIFT;
  220. int sel = (data & MUX_SEL_MASK) >> MUX_SEL_SHIFT;
  221. return clk_mux_set_parent(priv, mux, sel);
  222. }
  223. int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value)
  224. {
  225. const struct div_cfg *divider;
  226. uintptr_t address;
  227. uint64_t timeout;
  228. uint32_t mask;
  229. if (div_id >= priv->nb_div) {
  230. panic();
  231. }
  232. divider = &priv->div[div_id];
  233. address = priv->base + divider->offset;
  234. mask = MASK_WIDTH_SHIFT(divider->width, divider->shift);
  235. mmio_clrsetbits_32(address, mask, (value << divider->shift) & mask);
  236. if (divider->bitrdy == DIV_NO_BIT_RDY) {
  237. return 0;
  238. }
  239. timeout = timeout_init_us(CLKSRC_TIMEOUT);
  240. mask = BIT(divider->bitrdy);
  241. while ((mmio_read_32(address) & mask) == 0U) {
  242. if (timeout_elapsed(timeout)) {
  243. return -ETIMEDOUT;
  244. }
  245. }
  246. return 0;
  247. }
  248. const char *stm32mp_osc_node_label[NB_OSC] = {
  249. [_LSI] = "clk-lsi",
  250. [_LSE] = "clk-lse",
  251. [_HSI] = "clk-hsi",
  252. [_HSE] = "clk-hse",
  253. [_CSI] = "clk-csi",
  254. [_I2S_CKIN] = "i2s_ckin",
  255. };
  256. enum stm32mp1_parent_id {
  257. /* Oscillators are defined in enum stm32mp_osc_id */
  258. /* Other parent source */
  259. _HSI_KER = NB_OSC,
  260. _HSE_KER,
  261. _HSE_KER_DIV2,
  262. _HSE_RTC,
  263. _CSI_KER,
  264. _PLL1_P,
  265. _PLL1_Q,
  266. _PLL1_R,
  267. _PLL2_P,
  268. _PLL2_Q,
  269. _PLL2_R,
  270. _PLL3_P,
  271. _PLL3_Q,
  272. _PLL3_R,
  273. _PLL4_P,
  274. _PLL4_Q,
  275. _PLL4_R,
  276. _ACLK,
  277. _PCLK1,
  278. _PCLK2,
  279. _PCLK3,
  280. _PCLK4,
  281. _PCLK5,
  282. _HCLK6,
  283. _HCLK2,
  284. _CK_PER,
  285. _CK_MPU,
  286. _CK_MCU,
  287. _USB_PHY_48,
  288. _PARENT_NB,
  289. _UNKNOWN_ID = 0xff,
  290. };
  291. /* Lists only the parent clock we are interested in */
  292. enum stm32mp1_parent_sel {
  293. _I2C12_SEL,
  294. _I2C35_SEL,
  295. _STGEN_SEL,
  296. _I2C46_SEL,
  297. _SPI6_SEL,
  298. _UART1_SEL,
  299. _RNG1_SEL,
  300. _UART6_SEL,
  301. _UART24_SEL,
  302. _UART35_SEL,
  303. _UART78_SEL,
  304. _SDMMC12_SEL,
  305. _SDMMC3_SEL,
  306. _QSPI_SEL,
  307. _FMC_SEL,
  308. _AXIS_SEL,
  309. _MCUS_SEL,
  310. _USBPHY_SEL,
  311. _USBO_SEL,
  312. _MPU_SEL,
  313. _CKPER_SEL,
  314. _RTC_SEL,
  315. _PARENT_SEL_NB,
  316. _UNKNOWN_SEL = 0xff,
  317. };
  318. /* State the parent clock ID straight related to a clock */
  319. static const uint8_t parent_id_clock_id[_PARENT_NB] = {
  320. [_HSE] = CK_HSE,
  321. [_HSI] = CK_HSI,
  322. [_CSI] = CK_CSI,
  323. [_LSE] = CK_LSE,
  324. [_LSI] = CK_LSI,
  325. [_I2S_CKIN] = _UNKNOWN_ID,
  326. [_USB_PHY_48] = _UNKNOWN_ID,
  327. [_HSI_KER] = CK_HSI,
  328. [_HSE_KER] = CK_HSE,
  329. [_HSE_KER_DIV2] = CK_HSE_DIV2,
  330. [_HSE_RTC] = _UNKNOWN_ID,
  331. [_CSI_KER] = CK_CSI,
  332. [_PLL1_P] = PLL1_P,
  333. [_PLL1_Q] = PLL1_Q,
  334. [_PLL1_R] = PLL1_R,
  335. [_PLL2_P] = PLL2_P,
  336. [_PLL2_Q] = PLL2_Q,
  337. [_PLL2_R] = PLL2_R,
  338. [_PLL3_P] = PLL3_P,
  339. [_PLL3_Q] = PLL3_Q,
  340. [_PLL3_R] = PLL3_R,
  341. [_PLL4_P] = PLL4_P,
  342. [_PLL4_Q] = PLL4_Q,
  343. [_PLL4_R] = PLL4_R,
  344. [_ACLK] = CK_AXI,
  345. [_PCLK1] = CK_AXI,
  346. [_PCLK2] = CK_AXI,
  347. [_PCLK3] = CK_AXI,
  348. [_PCLK4] = CK_AXI,
  349. [_PCLK5] = CK_AXI,
  350. [_CK_PER] = CK_PER,
  351. [_CK_MPU] = CK_MPU,
  352. [_CK_MCU] = CK_MCU,
  353. };
  354. static unsigned int clock_id2parent_id(unsigned long id)
  355. {
  356. unsigned int n;
  357. for (n = 0U; n < ARRAY_SIZE(parent_id_clock_id); n++) {
  358. if (parent_id_clock_id[n] == id) {
  359. return n;
  360. }
  361. }
  362. return _UNKNOWN_ID;
  363. }
  364. enum stm32mp1_pll_id {
  365. _PLL1,
  366. _PLL2,
  367. _PLL3,
  368. _PLL4,
  369. _PLL_NB
  370. };
  371. enum stm32mp1_div_id {
  372. _DIV_P,
  373. _DIV_Q,
  374. _DIV_R,
  375. _DIV_NB,
  376. };
  377. enum stm32mp1_clksrc_id {
  378. CLKSRC_MPU,
  379. CLKSRC_AXI,
  380. CLKSRC_MCU,
  381. CLKSRC_PLL12,
  382. CLKSRC_PLL3,
  383. CLKSRC_PLL4,
  384. CLKSRC_RTC,
  385. CLKSRC_MCO1,
  386. CLKSRC_MCO2,
  387. CLKSRC_NB
  388. };
  389. enum stm32mp1_clkdiv_id {
  390. CLKDIV_MPU,
  391. CLKDIV_AXI,
  392. CLKDIV_MCU,
  393. CLKDIV_APB1,
  394. CLKDIV_APB2,
  395. CLKDIV_APB3,
  396. CLKDIV_APB4,
  397. CLKDIV_APB5,
  398. CLKDIV_RTC,
  399. CLKDIV_MCO1,
  400. CLKDIV_MCO2,
  401. CLKDIV_NB
  402. };
  403. enum stm32mp1_plltype {
  404. PLL_800,
  405. PLL_1600,
  406. PLL_TYPE_NB
  407. };
  408. struct stm32mp1_pll {
  409. uint8_t refclk_min;
  410. uint8_t refclk_max;
  411. };
  412. struct stm32mp1_clk_gate {
  413. uint16_t offset;
  414. uint8_t bit;
  415. uint8_t index;
  416. uint8_t set_clr;
  417. uint8_t secure;
  418. uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
  419. uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
  420. };
  421. struct stm32mp1_clk_sel {
  422. uint16_t offset;
  423. uint8_t src;
  424. uint8_t msk;
  425. uint8_t nb_parent;
  426. const uint8_t *parent;
  427. };
  428. #define REFCLK_SIZE 4
  429. struct stm32mp1_clk_pll {
  430. enum stm32mp1_plltype plltype;
  431. uint16_t rckxselr;
  432. uint16_t pllxcfgr1;
  433. uint16_t pllxcfgr2;
  434. uint16_t pllxfracr;
  435. uint16_t pllxcr;
  436. uint16_t pllxcsgr;
  437. enum stm32mp_osc_id refclk[REFCLK_SIZE];
  438. };
  439. /* Clocks with selectable source and non set/clr register access */
  440. #define _CLK_SELEC(sec, off, b, idx, s) \
  441. { \
  442. .offset = (off), \
  443. .bit = (b), \
  444. .index = (idx), \
  445. .set_clr = 0, \
  446. .secure = (sec), \
  447. .sel = (s), \
  448. .fixed = _UNKNOWN_ID, \
  449. }
  450. /* Clocks with fixed source and non set/clr register access */
  451. #define _CLK_FIXED(sec, off, b, idx, f) \
  452. { \
  453. .offset = (off), \
  454. .bit = (b), \
  455. .index = (idx), \
  456. .set_clr = 0, \
  457. .secure = (sec), \
  458. .sel = _UNKNOWN_SEL, \
  459. .fixed = (f), \
  460. }
  461. /* Clocks with selectable source and set/clr register access */
  462. #define _CLK_SC_SELEC(sec, off, b, idx, s) \
  463. { \
  464. .offset = (off), \
  465. .bit = (b), \
  466. .index = (idx), \
  467. .set_clr = 1, \
  468. .secure = (sec), \
  469. .sel = (s), \
  470. .fixed = _UNKNOWN_ID, \
  471. }
  472. /* Clocks with fixed source and set/clr register access */
  473. #define _CLK_SC_FIXED(sec, off, b, idx, f) \
  474. { \
  475. .offset = (off), \
  476. .bit = (b), \
  477. .index = (idx), \
  478. .set_clr = 1, \
  479. .secure = (sec), \
  480. .sel = _UNKNOWN_SEL, \
  481. .fixed = (f), \
  482. }
  483. #define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \
  484. [_ ## _label ## _SEL] = { \
  485. .offset = _rcc_selr, \
  486. .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \
  487. .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
  488. (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
  489. .parent = (_parents), \
  490. .nb_parent = ARRAY_SIZE(_parents) \
  491. }
  492. #define _CLK_PLL(idx, type, off1, off2, off3, \
  493. off4, off5, off6, \
  494. p1, p2, p3, p4) \
  495. [(idx)] = { \
  496. .plltype = (type), \
  497. .rckxselr = (off1), \
  498. .pllxcfgr1 = (off2), \
  499. .pllxcfgr2 = (off3), \
  500. .pllxfracr = (off4), \
  501. .pllxcr = (off5), \
  502. .pllxcsgr = (off6), \
  503. .refclk[0] = (p1), \
  504. .refclk[1] = (p2), \
  505. .refclk[2] = (p3), \
  506. .refclk[3] = (p4), \
  507. }
  508. #define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
  509. #define SEC 1
  510. #define N_S 0
  511. static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
  512. _CLK_FIXED(SEC, RCC_DDRITFCR, 0, DDRC1, _ACLK),
  513. _CLK_FIXED(SEC, RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
  514. _CLK_FIXED(SEC, RCC_DDRITFCR, 2, DDRC2, _ACLK),
  515. _CLK_FIXED(SEC, RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
  516. _CLK_FIXED(SEC, RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
  517. _CLK_FIXED(SEC, RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
  518. _CLK_FIXED(SEC, RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
  519. _CLK_FIXED(SEC, RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
  520. _CLK_FIXED(SEC, RCC_DDRITFCR, 8, AXIDCG, _ACLK),
  521. _CLK_FIXED(SEC, RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
  522. _CLK_FIXED(SEC, RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),
  523. #if defined(IMAGE_BL32)
  524. _CLK_SC_FIXED(N_S, RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
  525. #endif
  526. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
  527. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
  528. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
  529. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
  530. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
  531. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
  532. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
  533. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
  534. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
  535. _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
  536. #if defined(IMAGE_BL32)
  537. _CLK_SC_FIXED(N_S, RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
  538. #endif
  539. _CLK_SC_SELEC(N_S, RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
  540. _CLK_SC_FIXED(N_S, RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),
  541. _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
  542. _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
  543. _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
  544. _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
  545. _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
  546. _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
  547. _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
  548. _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
  549. _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
  550. _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
  551. _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
  552. _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
  553. _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
  554. _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
  555. #if defined(IMAGE_BL32)
  556. _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
  557. _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
  558. #endif
  559. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
  560. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
  561. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
  562. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
  563. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
  564. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
  565. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
  566. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
  567. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
  568. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
  569. _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
  570. _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
  571. _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
  572. _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
  573. _CLK_SC_SELEC(SEC, RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
  574. _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),
  575. #if defined(IMAGE_BL2)
  576. _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
  577. _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
  578. #endif
  579. _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
  580. _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
  581. #if defined(IMAGE_BL32)
  582. _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
  583. #endif
  584. _CLK_SELEC(SEC, RCC_BDCR, 20, RTC, _RTC_SEL),
  585. _CLK_SELEC(N_S, RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
  586. };
  587. static const uint8_t i2c12_parents[] = {
  588. _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
  589. };
  590. static const uint8_t i2c35_parents[] = {
  591. _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
  592. };
  593. static const uint8_t stgen_parents[] = {
  594. _HSI_KER, _HSE_KER
  595. };
  596. static const uint8_t i2c46_parents[] = {
  597. _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
  598. };
  599. static const uint8_t spi6_parents[] = {
  600. _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
  601. };
  602. static const uint8_t usart1_parents[] = {
  603. _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
  604. };
  605. static const uint8_t rng1_parents[] = {
  606. _CSI, _PLL4_R, _LSE, _LSI
  607. };
  608. static const uint8_t uart6_parents[] = {
  609. _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
  610. };
  611. static const uint8_t uart234578_parents[] = {
  612. _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
  613. };
  614. static const uint8_t sdmmc12_parents[] = {
  615. _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
  616. };
  617. static const uint8_t sdmmc3_parents[] = {
  618. _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
  619. };
  620. static const uint8_t qspi_parents[] = {
  621. _ACLK, _PLL3_R, _PLL4_P, _CK_PER
  622. };
  623. static const uint8_t fmc_parents[] = {
  624. _ACLK, _PLL3_R, _PLL4_P, _CK_PER
  625. };
  626. static const uint8_t axiss_parents[] = {
  627. _HSI, _HSE, _PLL2_P
  628. };
  629. static const uint8_t mcuss_parents[] = {
  630. _HSI, _HSE, _CSI, _PLL3_P
  631. };
  632. static const uint8_t usbphy_parents[] = {
  633. _HSE_KER, _PLL4_R, _HSE_KER_DIV2
  634. };
  635. static const uint8_t usbo_parents[] = {
  636. _PLL4_R, _USB_PHY_48
  637. };
  638. static const uint8_t mpu_parents[] = {
  639. _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */
  640. };
  641. static const uint8_t per_parents[] = {
  642. _HSI, _HSE, _CSI,
  643. };
  644. static const uint8_t rtc_parents[] = {
  645. _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC
  646. };
  647. static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
  648. _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
  649. _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
  650. _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
  651. _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
  652. _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
  653. _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
  654. _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
  655. _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
  656. _CLK_PARENT_SEL(CKPER, RCC_CPERCKSELR, per_parents),
  657. _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
  658. _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
  659. _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
  660. _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
  661. _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
  662. _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
  663. _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
  664. _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
  665. _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
  666. _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, axiss_parents),
  667. _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mcuss_parents),
  668. _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
  669. _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
  670. };
  671. /* Define characteristic of PLL according type */
  672. #define POST_DIVM_MIN 8000000U
  673. #define POST_DIVM_MAX 16000000U
  674. #define DIVM_MIN 0U
  675. #define DIVM_MAX 63U
  676. #define DIVN_MIN 24U
  677. #define DIVN_MAX 99U
  678. #define DIVP_MIN 0U
  679. #define DIVP_MAX 127U
  680. #define FRAC_MAX 8192U
  681. #define VCO_MIN 800000000U
  682. #define VCO_MAX 1600000000U
  683. static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
  684. [PLL_800] = {
  685. .refclk_min = 4,
  686. .refclk_max = 16,
  687. },
  688. [PLL_1600] = {
  689. .refclk_min = 8,
  690. .refclk_max = 16,
  691. },
  692. };
  693. /* PLLNCFGR2 register divider by output */
  694. static const uint8_t pllncfgr2[_DIV_NB] = {
  695. [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
  696. [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
  697. [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
  698. };
  699. static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
  700. _CLK_PLL(_PLL1, PLL_1600,
  701. RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
  702. RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
  703. _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
  704. _CLK_PLL(_PLL2, PLL_1600,
  705. RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
  706. RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
  707. _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
  708. _CLK_PLL(_PLL3, PLL_800,
  709. RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
  710. RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
  711. _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
  712. _CLK_PLL(_PLL4, PLL_800,
  713. RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
  714. RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
  715. _HSI, _HSE, _CSI, _I2S_CKIN),
  716. };
  717. /* Prescaler table lookups for clock computation */
  718. /* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
  719. static const uint8_t stm32mp1_mcu_div[16] = {
  720. 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
  721. };
  722. /* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
  723. #define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
  724. #define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
  725. static const uint8_t stm32mp1_mpu_apbx_div[8] = {
  726. 0, 1, 2, 3, 4, 4, 4, 4
  727. };
  728. /* div = /1 /2 /3 /4 */
  729. static const uint8_t stm32mp1_axi_div[8] = {
  730. 1, 2, 3, 4, 4, 4, 4, 4
  731. };
  732. static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
  733. [_HSI] = "HSI",
  734. [_HSE] = "HSE",
  735. [_CSI] = "CSI",
  736. [_LSI] = "LSI",
  737. [_LSE] = "LSE",
  738. [_I2S_CKIN] = "I2S_CKIN",
  739. [_HSI_KER] = "HSI_KER",
  740. [_HSE_KER] = "HSE_KER",
  741. [_HSE_KER_DIV2] = "HSE_KER_DIV2",
  742. [_HSE_RTC] = "HSE_RTC",
  743. [_CSI_KER] = "CSI_KER",
  744. [_PLL1_P] = "PLL1_P",
  745. [_PLL1_Q] = "PLL1_Q",
  746. [_PLL1_R] = "PLL1_R",
  747. [_PLL2_P] = "PLL2_P",
  748. [_PLL2_Q] = "PLL2_Q",
  749. [_PLL2_R] = "PLL2_R",
  750. [_PLL3_P] = "PLL3_P",
  751. [_PLL3_Q] = "PLL3_Q",
  752. [_PLL3_R] = "PLL3_R",
  753. [_PLL4_P] = "PLL4_P",
  754. [_PLL4_Q] = "PLL4_Q",
  755. [_PLL4_R] = "PLL4_R",
  756. [_ACLK] = "ACLK",
  757. [_PCLK1] = "PCLK1",
  758. [_PCLK2] = "PCLK2",
  759. [_PCLK3] = "PCLK3",
  760. [_PCLK4] = "PCLK4",
  761. [_PCLK5] = "PCLK5",
  762. [_HCLK6] = "KCLK6",
  763. [_HCLK2] = "HCLK2",
  764. [_CK_PER] = "CK_PER",
  765. [_CK_MPU] = "CK_MPU",
  766. [_CK_MCU] = "CK_MCU",
  767. [_USB_PHY_48] = "USB_PHY_48",
  768. };
  769. /* RCC clock device driver private */
  770. static unsigned long stm32mp1_osc[NB_OSC];
  771. static struct spinlock reg_lock;
  772. static unsigned int gate_refcounts[NB_GATES];
  773. static struct spinlock refcount_lock;
  774. static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
  775. {
  776. return &stm32mp1_clk_gate[idx];
  777. }
  778. #if defined(IMAGE_BL32)
  779. static bool gate_is_non_secure(const struct stm32mp1_clk_gate *gate)
  780. {
  781. return gate->secure == N_S;
  782. }
  783. #endif
  784. static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
  785. {
  786. return &stm32mp1_clk_sel[idx];
  787. }
  788. static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
  789. {
  790. return &stm32mp1_clk_pll[idx];
  791. }
  792. static void stm32mp1_clk_lock(struct spinlock *lock)
  793. {
  794. if (stm32mp_lock_available()) {
  795. /* Assume interrupts are masked */
  796. spin_lock(lock);
  797. }
  798. }
  799. static void stm32mp1_clk_unlock(struct spinlock *lock)
  800. {
  801. if (stm32mp_lock_available()) {
  802. spin_unlock(lock);
  803. }
  804. }
  805. bool stm32mp1_rcc_is_secure(void)
  806. {
  807. uintptr_t rcc_base = stm32mp_rcc_base();
  808. uint32_t mask = RCC_TZCR_TZEN;
  809. return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
  810. }
  811. bool stm32mp1_rcc_is_mckprot(void)
  812. {
  813. uintptr_t rcc_base = stm32mp_rcc_base();
  814. uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT;
  815. return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
  816. }
  817. void stm32mp1_clk_rcc_regs_lock(void)
  818. {
  819. stm32mp1_clk_lock(&reg_lock);
  820. }
  821. void stm32mp1_clk_rcc_regs_unlock(void)
  822. {
  823. stm32mp1_clk_unlock(&reg_lock);
  824. }
  825. static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
  826. {
  827. if (idx >= NB_OSC) {
  828. return 0;
  829. }
  830. return stm32mp1_osc[idx];
  831. }
  832. static int stm32mp1_clk_get_gated_id(unsigned long id)
  833. {
  834. unsigned int i;
  835. for (i = 0U; i < NB_GATES; i++) {
  836. if (gate_ref(i)->index == id) {
  837. return i;
  838. }
  839. }
  840. ERROR("%s: clk id %lu not found\n", __func__, id);
  841. return -EINVAL;
  842. }
  843. static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
  844. {
  845. return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
  846. }
  847. static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
  848. {
  849. return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
  850. }
  851. static int stm32mp1_clk_get_parent(unsigned long id)
  852. {
  853. const struct stm32mp1_clk_sel *sel;
  854. uint32_t p_sel;
  855. int i;
  856. enum stm32mp1_parent_id p;
  857. enum stm32mp1_parent_sel s;
  858. uintptr_t rcc_base = stm32mp_rcc_base();
  859. /* Few non gateable clock have a static parent ID, find them */
  860. i = (int)clock_id2parent_id(id);
  861. if (i != _UNKNOWN_ID) {
  862. return i;
  863. }
  864. i = stm32mp1_clk_get_gated_id(id);
  865. if (i < 0) {
  866. panic();
  867. }
  868. p = stm32mp1_clk_get_fixed_parent(i);
  869. if (p < _PARENT_NB) {
  870. return (int)p;
  871. }
  872. s = stm32mp1_clk_get_sel(i);
  873. if (s == _UNKNOWN_SEL) {
  874. return -EINVAL;
  875. }
  876. if (s >= _PARENT_SEL_NB) {
  877. panic();
  878. }
  879. sel = clk_sel_ref(s);
  880. p_sel = (mmio_read_32(rcc_base + sel->offset) &
  881. (sel->msk << sel->src)) >> sel->src;
  882. if (p_sel < sel->nb_parent) {
  883. return (int)sel->parent[p_sel];
  884. }
  885. return -EINVAL;
  886. }
  887. static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
  888. {
  889. uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
  890. uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
  891. return stm32mp1_clk_get_fixed(pll->refclk[src]);
  892. }
  893. /*
  894. * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
  895. * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
  896. * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
  897. * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
  898. */
  899. static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
  900. {
  901. unsigned long refclk, fvco;
  902. uint32_t cfgr1, fracr, divm, divn;
  903. uintptr_t rcc_base = stm32mp_rcc_base();
  904. cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
  905. fracr = mmio_read_32(rcc_base + pll->pllxfracr);
  906. divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
  907. divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
  908. refclk = stm32mp1_pll_get_fref(pll);
  909. /*
  910. * With FRACV :
  911. * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
  912. * Without FRACV
  913. * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
  914. */
  915. if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
  916. uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
  917. RCC_PLLNFRACR_FRACV_SHIFT;
  918. unsigned long long numerator, denominator;
  919. numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
  920. numerator = refclk * numerator;
  921. denominator = ((unsigned long long)divm + 1U) << 13;
  922. fvco = (unsigned long)(numerator / denominator);
  923. } else {
  924. fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
  925. }
  926. return fvco;
  927. }
  928. static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
  929. enum stm32mp1_div_id div_id)
  930. {
  931. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  932. unsigned long dfout;
  933. uint32_t cfgr2, divy;
  934. if (div_id >= _DIV_NB) {
  935. return 0;
  936. }
  937. cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
  938. divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
  939. dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
  940. return dfout;
  941. }
  942. static unsigned long get_clock_rate(int p)
  943. {
  944. uint32_t reg, clkdiv;
  945. unsigned long clock = 0;
  946. uintptr_t rcc_base = stm32mp_rcc_base();
  947. switch (p) {
  948. case _CK_MPU:
  949. /* MPU sub system */
  950. reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
  951. switch (reg & RCC_SELR_SRC_MASK) {
  952. case RCC_MPCKSELR_HSI:
  953. clock = stm32mp1_clk_get_fixed(_HSI);
  954. break;
  955. case RCC_MPCKSELR_HSE:
  956. clock = stm32mp1_clk_get_fixed(_HSE);
  957. break;
  958. case RCC_MPCKSELR_PLL:
  959. clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
  960. break;
  961. case RCC_MPCKSELR_PLL_MPUDIV:
  962. clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
  963. reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
  964. clkdiv = reg & RCC_MPUDIV_MASK;
  965. clock >>= stm32mp1_mpu_div[clkdiv];
  966. break;
  967. default:
  968. break;
  969. }
  970. break;
  971. /* AXI sub system */
  972. case _ACLK:
  973. case _HCLK2:
  974. case _HCLK6:
  975. case _PCLK4:
  976. case _PCLK5:
  977. reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
  978. switch (reg & RCC_SELR_SRC_MASK) {
  979. case RCC_ASSCKSELR_HSI:
  980. clock = stm32mp1_clk_get_fixed(_HSI);
  981. break;
  982. case RCC_ASSCKSELR_HSE:
  983. clock = stm32mp1_clk_get_fixed(_HSE);
  984. break;
  985. case RCC_ASSCKSELR_PLL:
  986. clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
  987. break;
  988. default:
  989. break;
  990. }
  991. /* System clock divider */
  992. reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
  993. clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
  994. switch (p) {
  995. case _PCLK4:
  996. reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
  997. clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
  998. break;
  999. case _PCLK5:
  1000. reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
  1001. clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
  1002. break;
  1003. default:
  1004. break;
  1005. }
  1006. break;
  1007. /* MCU sub system */
  1008. case _CK_MCU:
  1009. case _PCLK1:
  1010. case _PCLK2:
  1011. case _PCLK3:
  1012. reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
  1013. switch (reg & RCC_SELR_SRC_MASK) {
  1014. case RCC_MSSCKSELR_HSI:
  1015. clock = stm32mp1_clk_get_fixed(_HSI);
  1016. break;
  1017. case RCC_MSSCKSELR_HSE:
  1018. clock = stm32mp1_clk_get_fixed(_HSE);
  1019. break;
  1020. case RCC_MSSCKSELR_CSI:
  1021. clock = stm32mp1_clk_get_fixed(_CSI);
  1022. break;
  1023. case RCC_MSSCKSELR_PLL:
  1024. clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
  1025. break;
  1026. default:
  1027. break;
  1028. }
  1029. /* MCU clock divider */
  1030. reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
  1031. clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
  1032. switch (p) {
  1033. case _PCLK1:
  1034. reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
  1035. clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
  1036. break;
  1037. case _PCLK2:
  1038. reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
  1039. clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
  1040. break;
  1041. case _PCLK3:
  1042. reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
  1043. clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
  1044. break;
  1045. case _CK_MCU:
  1046. default:
  1047. break;
  1048. }
  1049. break;
  1050. case _CK_PER:
  1051. reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
  1052. switch (reg & RCC_SELR_SRC_MASK) {
  1053. case RCC_CPERCKSELR_HSI:
  1054. clock = stm32mp1_clk_get_fixed(_HSI);
  1055. break;
  1056. case RCC_CPERCKSELR_HSE:
  1057. clock = stm32mp1_clk_get_fixed(_HSE);
  1058. break;
  1059. case RCC_CPERCKSELR_CSI:
  1060. clock = stm32mp1_clk_get_fixed(_CSI);
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. break;
  1066. case _HSI:
  1067. case _HSI_KER:
  1068. clock = stm32mp1_clk_get_fixed(_HSI);
  1069. break;
  1070. case _CSI:
  1071. case _CSI_KER:
  1072. clock = stm32mp1_clk_get_fixed(_CSI);
  1073. break;
  1074. case _HSE:
  1075. case _HSE_KER:
  1076. clock = stm32mp1_clk_get_fixed(_HSE);
  1077. break;
  1078. case _HSE_KER_DIV2:
  1079. clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
  1080. break;
  1081. case _HSE_RTC:
  1082. clock = stm32mp1_clk_get_fixed(_HSE);
  1083. clock /= (mmio_read_32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U;
  1084. break;
  1085. case _LSI:
  1086. clock = stm32mp1_clk_get_fixed(_LSI);
  1087. break;
  1088. case _LSE:
  1089. clock = stm32mp1_clk_get_fixed(_LSE);
  1090. break;
  1091. /* PLL */
  1092. case _PLL1_P:
  1093. clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
  1094. break;
  1095. case _PLL1_Q:
  1096. clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
  1097. break;
  1098. case _PLL1_R:
  1099. clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
  1100. break;
  1101. case _PLL2_P:
  1102. clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
  1103. break;
  1104. case _PLL2_Q:
  1105. clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
  1106. break;
  1107. case _PLL2_R:
  1108. clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
  1109. break;
  1110. case _PLL3_P:
  1111. clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
  1112. break;
  1113. case _PLL3_Q:
  1114. clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
  1115. break;
  1116. case _PLL3_R:
  1117. clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
  1118. break;
  1119. case _PLL4_P:
  1120. clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
  1121. break;
  1122. case _PLL4_Q:
  1123. clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
  1124. break;
  1125. case _PLL4_R:
  1126. clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
  1127. break;
  1128. /* Other */
  1129. case _USB_PHY_48:
  1130. clock = USB_PHY_48_MHZ;
  1131. break;
  1132. default:
  1133. break;
  1134. }
  1135. return clock;
  1136. }
  1137. static void __clk_enable(struct stm32mp1_clk_gate const *gate)
  1138. {
  1139. uintptr_t rcc_base = stm32mp_rcc_base();
  1140. VERBOSE("Enable clock %u\n", gate->index);
  1141. if (gate->set_clr != 0U) {
  1142. mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
  1143. } else {
  1144. mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
  1145. }
  1146. }
  1147. static void __clk_disable(struct stm32mp1_clk_gate const *gate)
  1148. {
  1149. uintptr_t rcc_base = stm32mp_rcc_base();
  1150. VERBOSE("Disable clock %u\n", gate->index);
  1151. if (gate->set_clr != 0U) {
  1152. mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
  1153. BIT(gate->bit));
  1154. } else {
  1155. mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
  1156. }
  1157. }
  1158. static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
  1159. {
  1160. uintptr_t rcc_base = stm32mp_rcc_base();
  1161. return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
  1162. }
  1163. /* Oscillators and PLLs are not gated at runtime */
  1164. static bool clock_is_always_on(unsigned long id)
  1165. {
  1166. switch (id) {
  1167. case CK_HSE:
  1168. case CK_CSI:
  1169. case CK_LSI:
  1170. case CK_LSE:
  1171. case CK_HSI:
  1172. case CK_HSE_DIV2:
  1173. case PLL1_Q:
  1174. case PLL1_R:
  1175. case PLL2_P:
  1176. case PLL2_Q:
  1177. case PLL2_R:
  1178. case PLL3_P:
  1179. case PLL3_Q:
  1180. case PLL3_R:
  1181. case CK_AXI:
  1182. case CK_MPU:
  1183. case CK_MCU:
  1184. case RTC:
  1185. return true;
  1186. default:
  1187. return false;
  1188. }
  1189. }
  1190. static void __stm32mp1_clk_enable(unsigned long id, bool with_refcnt)
  1191. {
  1192. const struct stm32mp1_clk_gate *gate;
  1193. int i;
  1194. if (clock_is_always_on(id)) {
  1195. return;
  1196. }
  1197. i = stm32mp1_clk_get_gated_id(id);
  1198. if (i < 0) {
  1199. ERROR("Clock %lu can't be enabled\n", id);
  1200. panic();
  1201. }
  1202. gate = gate_ref(i);
  1203. if (!with_refcnt) {
  1204. __clk_enable(gate);
  1205. return;
  1206. }
  1207. #if defined(IMAGE_BL32)
  1208. if (gate_is_non_secure(gate)) {
  1209. /* Enable non-secure clock w/o any refcounting */
  1210. __clk_enable(gate);
  1211. return;
  1212. }
  1213. #endif
  1214. stm32mp1_clk_lock(&refcount_lock);
  1215. if (gate_refcounts[i] == 0U) {
  1216. __clk_enable(gate);
  1217. }
  1218. gate_refcounts[i]++;
  1219. if (gate_refcounts[i] == UINT_MAX) {
  1220. ERROR("Clock %lu refcount reached max value\n", id);
  1221. panic();
  1222. }
  1223. stm32mp1_clk_unlock(&refcount_lock);
  1224. }
  1225. static void __stm32mp1_clk_disable(unsigned long id, bool with_refcnt)
  1226. {
  1227. const struct stm32mp1_clk_gate *gate;
  1228. int i;
  1229. if (clock_is_always_on(id)) {
  1230. return;
  1231. }
  1232. i = stm32mp1_clk_get_gated_id(id);
  1233. if (i < 0) {
  1234. ERROR("Clock %lu can't be disabled\n", id);
  1235. panic();
  1236. }
  1237. gate = gate_ref(i);
  1238. if (!with_refcnt) {
  1239. __clk_disable(gate);
  1240. return;
  1241. }
  1242. #if defined(IMAGE_BL32)
  1243. if (gate_is_non_secure(gate)) {
  1244. /* Don't disable non-secure clocks */
  1245. return;
  1246. }
  1247. #endif
  1248. stm32mp1_clk_lock(&refcount_lock);
  1249. if (gate_refcounts[i] == 0U) {
  1250. ERROR("Clock %lu refcount reached 0\n", id);
  1251. panic();
  1252. }
  1253. gate_refcounts[i]--;
  1254. if (gate_refcounts[i] == 0U) {
  1255. __clk_disable(gate);
  1256. }
  1257. stm32mp1_clk_unlock(&refcount_lock);
  1258. }
  1259. static int stm32mp_clk_enable(unsigned long id)
  1260. {
  1261. __stm32mp1_clk_enable(id, true);
  1262. return 0;
  1263. }
  1264. static void stm32mp_clk_disable(unsigned long id)
  1265. {
  1266. __stm32mp1_clk_disable(id, true);
  1267. }
  1268. static bool stm32mp_clk_is_enabled(unsigned long id)
  1269. {
  1270. int i;
  1271. if (clock_is_always_on(id)) {
  1272. return true;
  1273. }
  1274. i = stm32mp1_clk_get_gated_id(id);
  1275. if (i < 0) {
  1276. panic();
  1277. }
  1278. return __clk_is_enabled(gate_ref(i));
  1279. }
  1280. static unsigned long stm32mp_clk_get_rate(unsigned long id)
  1281. {
  1282. uintptr_t rcc_base = stm32mp_rcc_base();
  1283. int p = stm32mp1_clk_get_parent(id);
  1284. uint32_t prescaler, timpre;
  1285. unsigned long parent_rate;
  1286. if (p < 0) {
  1287. return 0;
  1288. }
  1289. parent_rate = get_clock_rate(p);
  1290. switch (id) {
  1291. case TIM2_K:
  1292. case TIM3_K:
  1293. case TIM4_K:
  1294. case TIM5_K:
  1295. case TIM6_K:
  1296. case TIM7_K:
  1297. case TIM12_K:
  1298. case TIM13_K:
  1299. case TIM14_K:
  1300. prescaler = mmio_read_32(rcc_base + RCC_APB1DIVR) &
  1301. RCC_APBXDIV_MASK;
  1302. timpre = mmio_read_32(rcc_base + RCC_TIMG1PRER) &
  1303. RCC_TIMGXPRER_TIMGXPRE;
  1304. break;
  1305. case TIM1_K:
  1306. case TIM8_K:
  1307. case TIM15_K:
  1308. case TIM16_K:
  1309. case TIM17_K:
  1310. prescaler = mmio_read_32(rcc_base + RCC_APB2DIVR) &
  1311. RCC_APBXDIV_MASK;
  1312. timpre = mmio_read_32(rcc_base + RCC_TIMG2PRER) &
  1313. RCC_TIMGXPRER_TIMGXPRE;
  1314. break;
  1315. default:
  1316. return parent_rate;
  1317. }
  1318. if (prescaler == 0U) {
  1319. return parent_rate;
  1320. }
  1321. return parent_rate * (timpre + 1U) * 2U;
  1322. }
  1323. static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
  1324. {
  1325. uintptr_t address = stm32mp_rcc_base() + offset;
  1326. if (enable) {
  1327. mmio_setbits_32(address, mask_on);
  1328. } else {
  1329. mmio_clrbits_32(address, mask_on);
  1330. }
  1331. }
  1332. static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
  1333. {
  1334. uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
  1335. uintptr_t address = stm32mp_rcc_base() + offset;
  1336. mmio_write_32(address, mask_on);
  1337. }
  1338. static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
  1339. {
  1340. uint64_t timeout;
  1341. uint32_t mask_test;
  1342. uintptr_t address = stm32mp_rcc_base() + offset;
  1343. if (enable) {
  1344. mask_test = mask_rdy;
  1345. } else {
  1346. mask_test = 0;
  1347. }
  1348. timeout = timeout_init_us(OSCRDY_TIMEOUT);
  1349. while ((mmio_read_32(address) & mask_rdy) != mask_test) {
  1350. if (timeout_elapsed(timeout)) {
  1351. ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
  1352. mask_rdy, address, enable, mmio_read_32(address));
  1353. return -ETIMEDOUT;
  1354. }
  1355. }
  1356. return 0;
  1357. }
  1358. static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
  1359. {
  1360. uint32_t value;
  1361. uintptr_t rcc_base = stm32mp_rcc_base();
  1362. /* Do not reconfigure LSE if it is already ON */
  1363. if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEON) == RCC_BDCR_LSEON) {
  1364. return;
  1365. }
  1366. if (digbyp) {
  1367. mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
  1368. }
  1369. if (bypass || digbyp) {
  1370. mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
  1371. }
  1372. /*
  1373. * Warning: not recommended to switch directly from "high drive"
  1374. * to "medium low drive", and vice-versa.
  1375. */
  1376. value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
  1377. RCC_BDCR_LSEDRV_SHIFT;
  1378. while (value != lsedrv) {
  1379. if (value > lsedrv) {
  1380. value--;
  1381. } else {
  1382. value++;
  1383. }
  1384. mmio_clrsetbits_32(rcc_base + RCC_BDCR,
  1385. RCC_BDCR_LSEDRV_MASK,
  1386. value << RCC_BDCR_LSEDRV_SHIFT);
  1387. }
  1388. stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
  1389. }
  1390. static void stm32mp1_lse_wait(void)
  1391. {
  1392. if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
  1393. EARLY_ERROR("%s: failed\n", __func__);
  1394. }
  1395. }
  1396. static void stm32mp1_lsi_set(bool enable)
  1397. {
  1398. stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
  1399. if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
  1400. EARLY_ERROR("%s: failed\n", __func__);
  1401. }
  1402. }
  1403. static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
  1404. {
  1405. uintptr_t rcc_base = stm32mp_rcc_base();
  1406. if (digbyp) {
  1407. mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
  1408. }
  1409. if (bypass || digbyp) {
  1410. mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
  1411. }
  1412. stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
  1413. if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
  1414. EARLY_ERROR("%s: failed\n", __func__);
  1415. }
  1416. if (css) {
  1417. mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
  1418. }
  1419. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  1420. if ((mmio_read_32(rcc_base + RCC_OCENSETR) & RCC_OCENR_HSEBYP) &&
  1421. (!(digbyp || bypass))) {
  1422. panic();
  1423. }
  1424. #endif
  1425. }
  1426. static void stm32mp1_csi_set(bool enable)
  1427. {
  1428. stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
  1429. if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
  1430. EARLY_ERROR("%s: failed\n", __func__);
  1431. }
  1432. }
  1433. static void stm32mp1_hsi_set(bool enable)
  1434. {
  1435. stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
  1436. if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
  1437. EARLY_ERROR("%s: failed\n", __func__);
  1438. }
  1439. }
  1440. static int stm32mp1_set_hsidiv(uint8_t hsidiv)
  1441. {
  1442. uint64_t timeout;
  1443. uintptr_t rcc_base = stm32mp_rcc_base();
  1444. uintptr_t address = rcc_base + RCC_OCRDYR;
  1445. mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
  1446. RCC_HSICFGR_HSIDIV_MASK,
  1447. RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
  1448. timeout = timeout_init_us(HSIDIV_TIMEOUT);
  1449. while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
  1450. if (timeout_elapsed(timeout)) {
  1451. ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
  1452. address, mmio_read_32(address));
  1453. return -ETIMEDOUT;
  1454. }
  1455. }
  1456. return 0;
  1457. }
  1458. static int stm32mp1_hsidiv(unsigned long hsifreq)
  1459. {
  1460. uint8_t hsidiv;
  1461. uint32_t hsidivfreq = MAX_HSI_HZ;
  1462. for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
  1463. if (hsidivfreq == hsifreq) {
  1464. break;
  1465. }
  1466. hsidivfreq /= 2U;
  1467. }
  1468. if (hsidiv == 4U) {
  1469. EARLY_ERROR("Invalid clk-hsi frequency\n");
  1470. return -1;
  1471. }
  1472. if (hsidiv != 0U) {
  1473. return stm32mp1_set_hsidiv(hsidiv);
  1474. }
  1475. return 0;
  1476. }
  1477. static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
  1478. unsigned int clksrc,
  1479. uint32_t *pllcfg, uint32_t fracv)
  1480. {
  1481. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1482. uintptr_t rcc_base = stm32mp_rcc_base();
  1483. uintptr_t pllxcr = rcc_base + pll->pllxcr;
  1484. enum stm32mp1_plltype type = pll->plltype;
  1485. uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
  1486. unsigned long refclk;
  1487. uint32_t ifrge = 0U;
  1488. uint32_t src, value;
  1489. /* Check PLL output */
  1490. if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
  1491. return false;
  1492. }
  1493. /* Check current clksrc */
  1494. src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
  1495. if (src != (clksrc & RCC_SELR_SRC_MASK)) {
  1496. return false;
  1497. }
  1498. /* Check Div */
  1499. src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
  1500. refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
  1501. (pllcfg[PLLCFG_M] + 1U);
  1502. if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
  1503. (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
  1504. return false;
  1505. }
  1506. if ((type == PLL_800) && (refclk >= 8000000U)) {
  1507. ifrge = 1U;
  1508. }
  1509. value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
  1510. RCC_PLLNCFGR1_DIVN_MASK;
  1511. value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
  1512. RCC_PLLNCFGR1_DIVM_MASK;
  1513. value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
  1514. RCC_PLLNCFGR1_IFRGE_MASK;
  1515. if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
  1516. return false;
  1517. }
  1518. /* Fractional configuration */
  1519. value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
  1520. value |= RCC_PLLNFRACR_FRACLE;
  1521. if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
  1522. return false;
  1523. }
  1524. /* Output config */
  1525. value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
  1526. RCC_PLLNCFGR2_DIVP_MASK;
  1527. value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
  1528. RCC_PLLNCFGR2_DIVQ_MASK;
  1529. value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
  1530. RCC_PLLNCFGR2_DIVR_MASK;
  1531. if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
  1532. return false;
  1533. }
  1534. return true;
  1535. }
  1536. static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
  1537. {
  1538. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1539. uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
  1540. /* Preserve RCC_PLLNCR_SSCG_CTRL value */
  1541. mmio_clrsetbits_32(pllxcr,
  1542. RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
  1543. RCC_PLLNCR_DIVREN,
  1544. RCC_PLLNCR_PLLON);
  1545. }
  1546. static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
  1547. {
  1548. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1549. uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
  1550. uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
  1551. /* Wait PLL lock */
  1552. while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
  1553. if (timeout_elapsed(timeout)) {
  1554. EARLY_ERROR("PLL%u start failed @ 0x%lx: 0x%x\n",
  1555. pll_id, pllxcr, mmio_read_32(pllxcr));
  1556. return -ETIMEDOUT;
  1557. }
  1558. }
  1559. /* Start the requested output */
  1560. mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
  1561. return 0;
  1562. }
  1563. static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
  1564. {
  1565. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1566. uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
  1567. uint64_t timeout;
  1568. /* Stop all output */
  1569. mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
  1570. RCC_PLLNCR_DIVREN);
  1571. /* Stop PLL */
  1572. mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
  1573. timeout = timeout_init_us(PLLRDY_TIMEOUT);
  1574. /* Wait PLL stopped */
  1575. while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
  1576. if (timeout_elapsed(timeout)) {
  1577. EARLY_ERROR("PLL%u stop failed @ 0x%lx: 0x%x\n",
  1578. pll_id, pllxcr, mmio_read_32(pllxcr));
  1579. return -ETIMEDOUT;
  1580. }
  1581. }
  1582. return 0;
  1583. }
  1584. static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
  1585. uint32_t *pllcfg)
  1586. {
  1587. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1588. uintptr_t rcc_base = stm32mp_rcc_base();
  1589. uint32_t value;
  1590. value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
  1591. RCC_PLLNCFGR2_DIVP_MASK;
  1592. value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
  1593. RCC_PLLNCFGR2_DIVQ_MASK;
  1594. value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
  1595. RCC_PLLNCFGR2_DIVR_MASK;
  1596. mmio_write_32(rcc_base + pll->pllxcfgr2, value);
  1597. }
  1598. static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
  1599. uint32_t *pllcfg, uint32_t fracv)
  1600. {
  1601. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1602. uintptr_t rcc_base = stm32mp_rcc_base();
  1603. enum stm32mp1_plltype type = pll->plltype;
  1604. unsigned long refclk;
  1605. uint32_t ifrge = 0;
  1606. uint32_t src, value;
  1607. src = mmio_read_32(rcc_base + pll->rckxselr) &
  1608. RCC_SELR_REFCLK_SRC_MASK;
  1609. refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
  1610. (pllcfg[PLLCFG_M] + 1U);
  1611. if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
  1612. (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
  1613. return -EINVAL;
  1614. }
  1615. if ((type == PLL_800) && (refclk >= 8000000U)) {
  1616. ifrge = 1U;
  1617. }
  1618. value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
  1619. RCC_PLLNCFGR1_DIVN_MASK;
  1620. value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
  1621. RCC_PLLNCFGR1_DIVM_MASK;
  1622. value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
  1623. RCC_PLLNCFGR1_IFRGE_MASK;
  1624. mmio_write_32(rcc_base + pll->pllxcfgr1, value);
  1625. /* Fractional configuration */
  1626. value = 0;
  1627. mmio_write_32(rcc_base + pll->pllxfracr, value);
  1628. value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
  1629. mmio_write_32(rcc_base + pll->pllxfracr, value);
  1630. value |= RCC_PLLNFRACR_FRACLE;
  1631. mmio_write_32(rcc_base + pll->pllxfracr, value);
  1632. stm32mp1_pll_config_output(pll_id, pllcfg);
  1633. return 0;
  1634. }
  1635. static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
  1636. {
  1637. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  1638. uint32_t pllxcsg = 0;
  1639. pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
  1640. RCC_PLLNCSGR_MOD_PER_MASK;
  1641. pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
  1642. RCC_PLLNCSGR_INC_STEP_MASK;
  1643. pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
  1644. RCC_PLLNCSGR_SSCG_MODE_MASK;
  1645. mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
  1646. mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
  1647. RCC_PLLNCR_SSCG_CTRL);
  1648. }
  1649. static int clk_compute_pll1_settings(unsigned long input_freq,
  1650. uint32_t freq_khz,
  1651. uint32_t *pllcfg, uint32_t *fracv)
  1652. {
  1653. unsigned long long best_diff = ULLONG_MAX;
  1654. unsigned int divm;
  1655. /* Following parameters have always the same value */
  1656. pllcfg[PLLCFG_Q] = 0U;
  1657. pllcfg[PLLCFG_R] = 0U;
  1658. pllcfg[PLLCFG_O] = PQR(1, 0, 0);
  1659. for (divm = (DIVM_MAX + 1U); divm != DIVM_MIN; divm--) {
  1660. unsigned long post_divm = input_freq / divm;
  1661. unsigned int divp;
  1662. if ((post_divm < POST_DIVM_MIN) || (post_divm > POST_DIVM_MAX)) {
  1663. continue;
  1664. }
  1665. for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
  1666. unsigned long long output_freq = freq_khz * 1000ULL;
  1667. unsigned long long freq;
  1668. unsigned long long divn;
  1669. unsigned long long frac;
  1670. unsigned int i;
  1671. freq = output_freq * divm * (divp + 1U);
  1672. divn = (freq / input_freq) - 1U;
  1673. if ((divn < DIVN_MIN) || (divn > DIVN_MAX)) {
  1674. continue;
  1675. }
  1676. frac = ((freq * FRAC_MAX) / input_freq) - ((divn + 1U) * FRAC_MAX);
  1677. /* 2 loops to refine the fractional part */
  1678. for (i = 2U; i != 0U; i--) {
  1679. unsigned long long diff;
  1680. unsigned long long vco;
  1681. if (frac > FRAC_MAX) {
  1682. break;
  1683. }
  1684. vco = (post_divm * (divn + 1U)) + ((post_divm * frac) / FRAC_MAX);
  1685. if ((vco < (VCO_MIN / 2U)) || (vco > (VCO_MAX / 2U))) {
  1686. frac++;
  1687. continue;
  1688. }
  1689. freq = vco / (divp + 1U);
  1690. if (output_freq < freq) {
  1691. diff = freq - output_freq;
  1692. } else {
  1693. diff = output_freq - freq;
  1694. }
  1695. if (diff < best_diff) {
  1696. pllcfg[PLLCFG_M] = divm - 1U;
  1697. pllcfg[PLLCFG_N] = (uint32_t)divn;
  1698. pllcfg[PLLCFG_P] = divp;
  1699. *fracv = (uint32_t)frac;
  1700. if (diff == 0U) {
  1701. return 0;
  1702. }
  1703. best_diff = diff;
  1704. }
  1705. frac++;
  1706. }
  1707. }
  1708. }
  1709. if (best_diff == ULLONG_MAX) {
  1710. return -EINVAL;
  1711. }
  1712. return 0;
  1713. }
  1714. static int clk_get_pll1_settings(uint32_t clksrc, uint32_t freq_khz,
  1715. uint32_t *pllcfg, uint32_t *fracv)
  1716. {
  1717. unsigned long input_freq = 0UL;
  1718. assert(pllcfg != NULL);
  1719. assert(fracv != NULL);
  1720. switch (clksrc) {
  1721. case CLK_PLL12_HSI:
  1722. input_freq = stm32mp_clk_get_rate(CK_HSI);
  1723. break;
  1724. case CLK_PLL12_HSE:
  1725. input_freq = stm32mp_clk_get_rate(CK_HSE);
  1726. break;
  1727. default:
  1728. break;
  1729. }
  1730. if (input_freq == 0UL) {
  1731. panic();
  1732. }
  1733. return clk_compute_pll1_settings(input_freq, freq_khz, pllcfg, fracv);
  1734. }
  1735. static int stm32_clk_dividers_configure(struct stm32_clk_priv *priv)
  1736. {
  1737. struct stm32_clk_platdata *pdata = priv->pdata;
  1738. uint32_t i;
  1739. for (i = 0U; i < pdata->nclkdiv; i++) {
  1740. uint32_t div_id, div_n;
  1741. uint32_t val;
  1742. int ret;
  1743. val = pdata->clkdiv[i] & CMD_DATA_MASK;
  1744. div_id = (val & DIV_ID_MASK) >> DIV_ID_SHIFT;
  1745. div_n = (val & DIV_DIVN_MASK) >> DIV_DIVN_SHIFT;
  1746. ret = clk_stm32_set_div(priv, div_id, div_n);
  1747. if (ret != 0) {
  1748. return ret;
  1749. }
  1750. }
  1751. return 0;
  1752. }
  1753. static int stm32_clk_configure_clk(struct stm32_clk_priv *priv, uint32_t data)
  1754. {
  1755. uint32_t sel = (data & CLK_SEL_MASK) >> CLK_SEL_SHIFT;
  1756. uint32_t enable = (data & CLK_ON_MASK) >> CLK_ON_SHIFT;
  1757. unsigned long binding_id = ((unsigned long)data & CLK_ID_MASK) >> CLK_ID_SHIFT;
  1758. struct stm32_clk_platdata *pdata = priv->pdata;
  1759. if (binding_id == RTC) {
  1760. uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
  1761. if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) || (enable != 0U)) {
  1762. mmio_clrsetbits_32(address, RCC_BDCR_RTCSRC_MASK,
  1763. (sel & RCC_SELR_SRC_MASK) << RCC_BDCR_RTCSRC_SHIFT);
  1764. mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
  1765. /* Configure LSE CSS */
  1766. if (pdata->lse_css) {
  1767. mmio_setbits_32(priv->base + RCC_BDCR, RCC_BDCR_LSECSSON);
  1768. }
  1769. }
  1770. }
  1771. return 0;
  1772. }
  1773. static int stm32_clk_configure_by_addr_val(struct stm32_clk_priv *priv,
  1774. uint32_t data)
  1775. {
  1776. uint32_t addr = data >> CLK_ADDR_SHIFT;
  1777. uint32_t val = data & CLK_ADDR_VAL_MASK;
  1778. mmio_setbits_32(priv->base + addr, val);
  1779. return 0;
  1780. }
  1781. static int stm32_clk_source_configure(struct stm32_clk_priv *priv)
  1782. {
  1783. struct stm32_clk_platdata *pdata = priv->pdata;
  1784. bool ckper_disabled = false;
  1785. uint32_t i;
  1786. for (i = 0U; i < pdata->nclksrc; i++) {
  1787. uint32_t val = pdata->clksrc[i];
  1788. uint32_t cmd, cmd_data;
  1789. int ret;
  1790. if (val & CMD_ADDR_BIT) {
  1791. ret = stm32_clk_configure_by_addr_val(priv, val & ~CMD_ADDR_BIT);
  1792. if (ret != 0) {
  1793. return ret;
  1794. }
  1795. continue;
  1796. }
  1797. if (val == (uint32_t)CLK_CKPER_DISABLED) {
  1798. ckper_disabled = true;
  1799. continue;
  1800. }
  1801. cmd = (val & CMD_MASK) >> CMD_SHIFT;
  1802. cmd_data = val & ~CMD_MASK;
  1803. switch (cmd) {
  1804. case CMD_MUX:
  1805. ret = stm32_clk_configure_mux(priv, cmd_data);
  1806. break;
  1807. case CMD_CLK:
  1808. ret = stm32_clk_configure_clk(priv, cmd_data);
  1809. break;
  1810. default:
  1811. ret = -EINVAL;
  1812. break;
  1813. }
  1814. if (ret != 0) {
  1815. return ret;
  1816. }
  1817. }
  1818. /*
  1819. * CKPER is source for some peripheral clocks
  1820. * (FMC-NAND / QPSI-NOR) and switching source is allowed
  1821. * only if previous clock is still ON
  1822. * => deactivate CKPER only after switching clock
  1823. */
  1824. if (!ckper_disabled) {
  1825. return 0;
  1826. }
  1827. return stm32_clk_configure_mux(priv, CLK_CKPER_DISABLED);
  1828. }
  1829. static int stm32mp1_pll_configure_src(struct stm32_clk_priv *priv, int pll_idx)
  1830. {
  1831. struct stm32_clk_platdata *pdata = priv->pdata;
  1832. struct stm32_pll_dt_cfg *pll_conf = &pdata->pll[pll_idx];
  1833. if (!pll_conf->status) {
  1834. return 0;
  1835. }
  1836. return stm32_clk_configure_mux(priv, pll_conf->src);
  1837. }
  1838. int stm32mp1_clk_init(void)
  1839. {
  1840. struct stm32_clk_priv *priv = clk_stm32_get_priv();
  1841. struct stm32_clk_platdata *pdata = priv->pdata;
  1842. struct stm32_pll_dt_cfg *pll_conf = pdata->pll;
  1843. int ret;
  1844. enum stm32mp1_pll_id i;
  1845. bool pll3_preserve = false;
  1846. bool pll4_preserve = false;
  1847. bool pll4_bootrom = false;
  1848. int stgen_p = stm32mp1_clk_get_parent(STGEN_K);
  1849. int usbphy_p = stm32mp1_clk_get_parent(USBPHY_K);
  1850. uint32_t usbreg_bootrom = 0U;
  1851. if (!pll_conf[_PLL1].status) {
  1852. ret = clk_get_pll1_settings(pll_conf[_PLL2].src, PLL1_NOMINAL_FREQ_IN_KHZ,
  1853. pll_conf[_PLL1].cfg, &pll_conf[_PLL1].frac);
  1854. if (ret != 0) {
  1855. return ret;
  1856. }
  1857. pll_conf[_PLL1].status = true;
  1858. pll_conf[_PLL1].src = pll_conf[_PLL2].src;
  1859. }
  1860. /*
  1861. * Switch ON oscillator found in device-tree.
  1862. * Note: HSI already ON after BootROM stage.
  1863. */
  1864. if (stm32mp1_osc[_LSI] != 0U) {
  1865. stm32mp1_lsi_set(true);
  1866. }
  1867. if (stm32mp1_osc[_LSE] != 0U) {
  1868. const char *name = stm32mp_osc_node_label[_LSE];
  1869. bool bypass, digbyp;
  1870. uint32_t lsedrv;
  1871. bypass = fdt_clk_read_bool(name, "st,bypass");
  1872. digbyp = fdt_clk_read_bool(name, "st,digbypass");
  1873. pdata->lse_css = fdt_clk_read_bool(name, "st,css");
  1874. lsedrv = fdt_clk_read_uint32_default(name, "st,drive",
  1875. LSEDRV_MEDIUM_HIGH);
  1876. stm32mp1_lse_enable(bypass, digbyp, lsedrv);
  1877. }
  1878. if (stm32mp1_osc[_HSE] != 0U) {
  1879. const char *name = stm32mp_osc_node_label[_HSE];
  1880. bool bypass, digbyp, css;
  1881. bypass = fdt_clk_read_bool(name, "st,bypass");
  1882. digbyp = fdt_clk_read_bool(name, "st,digbypass");
  1883. css = fdt_clk_read_bool(name, "st,css");
  1884. stm32mp1_hse_enable(bypass, digbyp, css);
  1885. }
  1886. /*
  1887. * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
  1888. * => switch on CSI even if node is not present in device tree
  1889. */
  1890. stm32mp1_csi_set(true);
  1891. /* Come back to HSI */
  1892. ret = stm32_clk_configure_mux(priv, CLK_MPU_HSI);
  1893. if (ret != 0) {
  1894. return ret;
  1895. }
  1896. ret = stm32_clk_configure_mux(priv, CLK_AXI_HSI);
  1897. if (ret != 0) {
  1898. return ret;
  1899. }
  1900. ret = stm32_clk_configure_mux(priv, CLK_MCU_HSI);
  1901. if (ret != 0) {
  1902. return ret;
  1903. }
  1904. if ((mmio_read_32(priv->base + RCC_MP_RSTSCLRR) &
  1905. RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
  1906. pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
  1907. pll_conf[_PLL3].src,
  1908. pll_conf[_PLL3].cfg,
  1909. pll_conf[_PLL3].frac);
  1910. pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
  1911. pll_conf[_PLL4].src,
  1912. pll_conf[_PLL4].cfg,
  1913. pll_conf[_PLL4].frac);
  1914. }
  1915. /* Don't initialize PLL4, when used by BOOTROM */
  1916. if ((stm32mp_get_boot_itf_selected() ==
  1917. BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) &&
  1918. ((stgen_p == (int)_PLL4_R) || (usbphy_p == (int)_PLL4_R))) {
  1919. pll4_bootrom = true;
  1920. pll4_preserve = true;
  1921. }
  1922. for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
  1923. if (((i == _PLL3) && pll3_preserve) ||
  1924. ((i == _PLL4) && pll4_preserve)) {
  1925. continue;
  1926. }
  1927. ret = stm32mp1_pll_stop(i);
  1928. if (ret != 0) {
  1929. return ret;
  1930. }
  1931. }
  1932. /* Configure HSIDIV */
  1933. if (stm32mp1_osc[_HSI] != 0U) {
  1934. ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
  1935. if (ret != 0) {
  1936. return ret;
  1937. }
  1938. stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K));
  1939. }
  1940. /* Configure dividers */
  1941. ret = stm32_clk_dividers_configure(priv);
  1942. if (ret != 0) {
  1943. return ret;
  1944. }
  1945. /* Configure PLLs source */
  1946. ret = stm32mp1_pll_configure_src(priv, _PLL1);
  1947. if (ret != 0) {
  1948. return ret;
  1949. }
  1950. if (!pll3_preserve) {
  1951. ret = stm32mp1_pll_configure_src(priv, _PLL3);
  1952. if (ret != 0) {
  1953. return ret;
  1954. }
  1955. }
  1956. if (!pll4_preserve) {
  1957. ret = stm32mp1_pll_configure_src(priv, _PLL4);
  1958. if (ret != 0) {
  1959. return ret;
  1960. }
  1961. }
  1962. /* Configure and start PLLs */
  1963. for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
  1964. if (((i == _PLL3) && pll3_preserve) ||
  1965. ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
  1966. continue;
  1967. }
  1968. if (!pll_conf[i].status) {
  1969. continue;
  1970. }
  1971. if ((i == _PLL4) && pll4_bootrom) {
  1972. /* Set output divider if not done by the Bootrom */
  1973. stm32mp1_pll_config_output(i, pll_conf[i].cfg);
  1974. continue;
  1975. }
  1976. ret = stm32mp1_pll_config(i, pll_conf[i].cfg, pll_conf[i].frac);
  1977. if (ret != 0) {
  1978. return ret;
  1979. }
  1980. if (pll_conf[i].csg_enabled) {
  1981. stm32mp1_pll_csg(i, pll_conf[i].csg);
  1982. }
  1983. stm32mp1_pll_start(i);
  1984. }
  1985. /* Wait and start PLLs output when ready */
  1986. for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
  1987. if (!pll_conf[i].status) {
  1988. continue;
  1989. }
  1990. ret = stm32mp1_pll_output(i, pll_conf[i].cfg[PLLCFG_O]);
  1991. if (ret != 0) {
  1992. return ret;
  1993. }
  1994. }
  1995. /* Wait LSE ready before to use it */
  1996. if (stm32mp1_osc[_LSE] != 0U) {
  1997. stm32mp1_lse_wait();
  1998. }
  1999. if (pll4_bootrom) {
  2000. usbreg_bootrom = mmio_read_32(priv->base + RCC_USBCKSELR);
  2001. }
  2002. /* Configure with expected clock source */
  2003. ret = stm32_clk_source_configure(priv);
  2004. if (ret != 0) {
  2005. panic();
  2006. }
  2007. if (pll4_bootrom) {
  2008. uint32_t usbreg_value, usbreg_mask;
  2009. const struct stm32mp1_clk_sel *sel;
  2010. sel = clk_sel_ref(_USBPHY_SEL);
  2011. usbreg_mask = (uint32_t)sel->msk << sel->src;
  2012. sel = clk_sel_ref(_USBO_SEL);
  2013. usbreg_mask |= (uint32_t)sel->msk << sel->src;
  2014. usbreg_value = mmio_read_32(priv->base + RCC_USBCKSELR) &
  2015. usbreg_mask;
  2016. usbreg_bootrom &= usbreg_mask;
  2017. if (usbreg_bootrom != usbreg_value) {
  2018. EARLY_ERROR("forbidden new USB clk path\n");
  2019. EARLY_ERROR("vs bootrom on USB boot\n");
  2020. return -FDT_ERR_BADVALUE;
  2021. }
  2022. }
  2023. /* Switch OFF HSI if not found in device-tree */
  2024. if (stm32mp1_osc[_HSI] == 0U) {
  2025. stm32mp1_hsi_set(false);
  2026. }
  2027. stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K));
  2028. /* Software Self-Refresh mode (SSR) during DDR initilialization */
  2029. mmio_clrsetbits_32(priv->base + RCC_DDRITFCR,
  2030. RCC_DDRITFCR_DDRCKMOD_MASK,
  2031. RCC_DDRITFCR_DDRCKMOD_SSR <<
  2032. RCC_DDRITFCR_DDRCKMOD_SHIFT);
  2033. return 0;
  2034. }
  2035. static void stm32mp1_osc_clk_init(const char *name,
  2036. enum stm32mp_osc_id index)
  2037. {
  2038. uint32_t frequency;
  2039. if (fdt_osc_read_freq(name, &frequency) == 0) {
  2040. stm32mp1_osc[index] = frequency;
  2041. }
  2042. }
  2043. static void stm32mp1_osc_init(void)
  2044. {
  2045. enum stm32mp_osc_id i;
  2046. for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
  2047. stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
  2048. }
  2049. }
  2050. #ifdef STM32MP_SHARED_RESOURCES
  2051. /*
  2052. * Get the parent ID of the target parent clock, for tagging as secure
  2053. * shared clock dependencies.
  2054. */
  2055. static int get_parent_id_parent(unsigned int parent_id)
  2056. {
  2057. enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
  2058. enum stm32mp1_pll_id pll_id;
  2059. uint32_t p_sel;
  2060. uintptr_t rcc_base = stm32mp_rcc_base();
  2061. switch (parent_id) {
  2062. case _ACLK:
  2063. case _PCLK4:
  2064. case _PCLK5:
  2065. s = _AXIS_SEL;
  2066. break;
  2067. case _PLL1_P:
  2068. case _PLL1_Q:
  2069. case _PLL1_R:
  2070. pll_id = _PLL1;
  2071. break;
  2072. case _PLL2_P:
  2073. case _PLL2_Q:
  2074. case _PLL2_R:
  2075. pll_id = _PLL2;
  2076. break;
  2077. case _PLL3_P:
  2078. case _PLL3_Q:
  2079. case _PLL3_R:
  2080. pll_id = _PLL3;
  2081. break;
  2082. case _PLL4_P:
  2083. case _PLL4_Q:
  2084. case _PLL4_R:
  2085. pll_id = _PLL4;
  2086. break;
  2087. case _PCLK1:
  2088. case _PCLK2:
  2089. case _HCLK2:
  2090. case _HCLK6:
  2091. case _CK_PER:
  2092. case _CK_MPU:
  2093. case _CK_MCU:
  2094. case _USB_PHY_48:
  2095. /* We do not expect to access these */
  2096. panic();
  2097. break;
  2098. default:
  2099. /* Other parents have no parent */
  2100. return -1;
  2101. }
  2102. if (s != _UNKNOWN_SEL) {
  2103. const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
  2104. p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
  2105. sel->msk;
  2106. if (p_sel < sel->nb_parent) {
  2107. return (int)sel->parent[p_sel];
  2108. }
  2109. } else {
  2110. const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
  2111. p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
  2112. RCC_SELR_REFCLK_SRC_MASK;
  2113. if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
  2114. return (int)pll->refclk[p_sel];
  2115. }
  2116. }
  2117. VERBOSE("No parent selected for %s\n",
  2118. stm32mp1_clk_parent_name[parent_id]);
  2119. return -1;
  2120. }
  2121. static void secure_parent_clocks(unsigned long parent_id)
  2122. {
  2123. int grandparent_id;
  2124. switch (parent_id) {
  2125. case _PLL3_P:
  2126. case _PLL3_Q:
  2127. case _PLL3_R:
  2128. stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
  2129. break;
  2130. /* These clocks are always secure when RCC is secure */
  2131. case _ACLK:
  2132. case _HCLK2:
  2133. case _HCLK6:
  2134. case _PCLK4:
  2135. case _PCLK5:
  2136. case _PLL1_P:
  2137. case _PLL1_Q:
  2138. case _PLL1_R:
  2139. case _PLL2_P:
  2140. case _PLL2_Q:
  2141. case _PLL2_R:
  2142. case _HSI:
  2143. case _HSI_KER:
  2144. case _LSI:
  2145. case _CSI:
  2146. case _CSI_KER:
  2147. case _HSE:
  2148. case _HSE_KER:
  2149. case _HSE_KER_DIV2:
  2150. case _HSE_RTC:
  2151. case _LSE:
  2152. break;
  2153. default:
  2154. VERBOSE("Cannot secure parent clock %s\n",
  2155. stm32mp1_clk_parent_name[parent_id]);
  2156. panic();
  2157. }
  2158. grandparent_id = get_parent_id_parent(parent_id);
  2159. if (grandparent_id >= 0) {
  2160. secure_parent_clocks(grandparent_id);
  2161. }
  2162. }
  2163. void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
  2164. {
  2165. int parent_id;
  2166. if (!stm32mp1_rcc_is_secure()) {
  2167. return;
  2168. }
  2169. switch (clock_id) {
  2170. case PLL1:
  2171. case PLL2:
  2172. /* PLL1/PLL2 are always secure: nothing to do */
  2173. break;
  2174. case PLL3:
  2175. stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
  2176. break;
  2177. case PLL4:
  2178. ERROR("PLL4 cannot be secured\n");
  2179. panic();
  2180. break;
  2181. default:
  2182. /* Others are expected gateable clock */
  2183. parent_id = stm32mp1_clk_get_parent(clock_id);
  2184. if (parent_id < 0) {
  2185. INFO("No parent found for clock %lu\n", clock_id);
  2186. } else {
  2187. secure_parent_clocks(parent_id);
  2188. }
  2189. break;
  2190. }
  2191. }
  2192. #endif /* STM32MP_SHARED_RESOURCES */
  2193. void stm32mp1_clk_mcuss_protect(bool enable)
  2194. {
  2195. uintptr_t rcc_base = stm32mp_rcc_base();
  2196. if (enable) {
  2197. mmio_setbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
  2198. } else {
  2199. mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
  2200. }
  2201. }
  2202. static void sync_earlyboot_clocks_state(void)
  2203. {
  2204. unsigned int idx;
  2205. const unsigned long secure_enable[] = {
  2206. AXIDCG,
  2207. BSEC,
  2208. DDRC1, DDRC1LP,
  2209. DDRC2, DDRC2LP,
  2210. DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
  2211. DDRPHYC, DDRPHYCLP,
  2212. RTCAPB,
  2213. TZC1, TZC2,
  2214. TZPC,
  2215. STGEN_K,
  2216. };
  2217. for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
  2218. stm32mp_clk_enable(secure_enable[idx]);
  2219. }
  2220. }
  2221. static const struct clk_ops stm32mp_clk_ops = {
  2222. .enable = stm32mp_clk_enable,
  2223. .disable = stm32mp_clk_disable,
  2224. .is_enabled = stm32mp_clk_is_enabled,
  2225. .get_rate = stm32mp_clk_get_rate,
  2226. .get_parent = stm32mp1_clk_get_parent,
  2227. };
  2228. struct stm32_pll_dt_cfg mp15_pll[_PLL_NB];
  2229. uint32_t mp15_clksrc[MUX_NB];
  2230. uint32_t mp15_clkdiv[DIV_NB];
  2231. struct stm32_clk_platdata stm32mp15_clock_pdata = {
  2232. .pll = mp15_pll,
  2233. .npll = _PLL_NB,
  2234. .clksrc = mp15_clksrc,
  2235. .nclksrc = MUX_NB,
  2236. .clkdiv = mp15_clkdiv,
  2237. .nclkdiv = DIV_NB,
  2238. };
  2239. static struct stm32_clk_priv stm32mp15_clock_data = {
  2240. .base = RCC_BASE,
  2241. .parents = parent_mp15,
  2242. .nb_parents = ARRAY_SIZE(parent_mp15),
  2243. .div = dividers_mp15,
  2244. .nb_div = ARRAY_SIZE(dividers_mp15),
  2245. .pdata = &stm32mp15_clock_pdata,
  2246. };
  2247. static int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name,
  2248. uint32_t *tab, uint32_t *nb)
  2249. {
  2250. const fdt32_t *cell;
  2251. int len = 0;
  2252. uint32_t i;
  2253. cell = fdt_getprop(fdt, node, name, &len);
  2254. if (cell == NULL) {
  2255. *nb = 0U;
  2256. return 0;
  2257. }
  2258. for (i = 0U; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
  2259. tab[i] = fdt32_to_cpu(cell[i]);
  2260. }
  2261. *nb = (uint32_t)len / sizeof(uint32_t);
  2262. return 0;
  2263. }
  2264. #define RCC_PLL_NAME_SIZE 12
  2265. static int clk_stm32_load_vco_config(void *fdt, int subnode, struct stm32_pll_dt_cfg *pll)
  2266. {
  2267. int err;
  2268. err = fdt_read_uint32_array(fdt, subnode, "divmn", (int)PLL_DIV_MN_NB, &pll->cfg[PLLCFG_M]);
  2269. if (err != 0) {
  2270. return err;
  2271. }
  2272. err = fdt_read_uint32_array(fdt, subnode, "csg", (int)PLLCSG_NB, pll->csg);
  2273. if (err == 0) {
  2274. pll->csg_enabled = true;
  2275. } else if (err == -FDT_ERR_NOTFOUND) {
  2276. pll->csg_enabled = false;
  2277. } else {
  2278. return err;
  2279. }
  2280. pll->status = true;
  2281. pll->frac = fdt_read_uint32_default(fdt, subnode, "frac", 0);
  2282. pll->src = fdt_read_uint32_default(fdt, subnode, "src", UINT32_MAX);
  2283. return 0;
  2284. }
  2285. static int clk_stm32_load_output_config(void *fdt, int subnode, struct stm32_pll_dt_cfg *pll)
  2286. {
  2287. int err;
  2288. err = fdt_read_uint32_array(fdt, subnode, "st,pll_div_pqr", (int)PLL_DIV_PQR_NB,
  2289. &pll->cfg[PLLCFG_P]);
  2290. if (err != 0) {
  2291. return err;
  2292. }
  2293. pll->cfg[PLLCFG_O] = PQR(1, 1, 1);
  2294. return 0;
  2295. }
  2296. static int clk_stm32_parse_pll_fdt(void *fdt, int subnode, struct stm32_pll_dt_cfg *pll)
  2297. {
  2298. const fdt32_t *cuint;
  2299. int subnode_pll;
  2300. int subnode_vco;
  2301. int err;
  2302. cuint = fdt_getprop(fdt, subnode, "st,pll", NULL);
  2303. if (cuint == NULL) {
  2304. /* Case of no pll is defined */
  2305. return 0;
  2306. }
  2307. subnode_pll = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
  2308. if (subnode_pll < 0) {
  2309. return -FDT_ERR_NOTFOUND;
  2310. }
  2311. cuint = fdt_getprop(fdt, subnode_pll, "st,pll_vco", NULL);
  2312. if (cuint == NULL) {
  2313. return -FDT_ERR_NOTFOUND;
  2314. }
  2315. subnode_vco = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
  2316. if (subnode_vco < 0) {
  2317. return -FDT_ERR_NOTFOUND;
  2318. }
  2319. err = clk_stm32_load_vco_config(fdt, subnode_vco, pll);
  2320. if (err != 0) {
  2321. return err;
  2322. }
  2323. err = clk_stm32_load_output_config(fdt, subnode_pll, pll);
  2324. if (err != 0) {
  2325. return err;
  2326. }
  2327. return 0;
  2328. }
  2329. static int stm32_clk_parse_fdt_all_pll(void *fdt, int node, struct stm32_clk_platdata *pdata)
  2330. {
  2331. size_t i = 0U;
  2332. for (i = _PLL1; i < pdata->npll; i++) {
  2333. struct stm32_pll_dt_cfg *pll = pdata->pll + i;
  2334. char name[RCC_PLL_NAME_SIZE];
  2335. int subnode;
  2336. int err;
  2337. snprintf(name, sizeof(name), "st,pll@%u", i);
  2338. subnode = fdt_subnode_offset(fdt, node, name);
  2339. if (!fdt_check_node(subnode)) {
  2340. continue;
  2341. }
  2342. err = clk_stm32_parse_pll_fdt(fdt, subnode, pll);
  2343. if (err != 0) {
  2344. panic();
  2345. }
  2346. }
  2347. return 0;
  2348. }
  2349. static int stm32_clk_parse_fdt(struct stm32_clk_platdata *pdata)
  2350. {
  2351. void *fdt = NULL;
  2352. int node;
  2353. uint32_t err;
  2354. if (fdt_get_address(&fdt) == 0) {
  2355. return -ENOENT;
  2356. }
  2357. node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_CLK_COMPAT);
  2358. if (node < 0) {
  2359. panic();
  2360. }
  2361. err = stm32_clk_parse_fdt_all_pll(fdt, node, pdata);
  2362. if (err != 0) {
  2363. return err;
  2364. }
  2365. err = stm32_clk_parse_fdt_by_name(fdt, node, "st,clkdiv", pdata->clkdiv, &pdata->nclkdiv);
  2366. if (err != 0) {
  2367. return err;
  2368. }
  2369. err = stm32_clk_parse_fdt_by_name(fdt, node, "st,clksrc", pdata->clksrc, &pdata->nclksrc);
  2370. if (err != 0) {
  2371. return err;
  2372. }
  2373. return 0;
  2374. }
  2375. int stm32mp1_clk_probe(void)
  2376. {
  2377. uintptr_t base = RCC_BASE;
  2378. int ret;
  2379. #if defined(IMAGE_BL32)
  2380. if (!fdt_get_rcc_secure_state()) {
  2381. mmio_write_32(stm32mp_rcc_base() + RCC_TZCR, 0U);
  2382. }
  2383. #endif
  2384. stm32mp1_osc_init();
  2385. ret = stm32_clk_parse_fdt(&stm32mp15_clock_pdata);
  2386. if (ret != 0) {
  2387. return ret;
  2388. }
  2389. ret = clk_stm32_init(&stm32mp15_clock_data, base);
  2390. if (ret != 0) {
  2391. return ret;
  2392. }
  2393. sync_earlyboot_clocks_state();
  2394. clk_register(&stm32mp_clk_ops);
  2395. return 0;
  2396. }