stm32mp1_clk.c 54 KB

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