clk-stm32mp13.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. /*
  2. * Copyright (C) 2022-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 <limits.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <arch.h>
  12. #include <arch_helpers.h>
  13. #include "clk-stm32-core.h"
  14. #include <common/debug.h>
  15. #include <common/fdt_wrappers.h>
  16. #include <drivers/clk.h>
  17. #include <drivers/delay_timer.h>
  18. #include <drivers/st/stm32mp13_rcc.h>
  19. #include <drivers/st/stm32mp1_clk.h>
  20. #include <drivers/st/stm32mp_clkfunc.h>
  21. #include <dt-bindings/clock/stm32mp13-clksrc.h>
  22. #include <lib/mmio.h>
  23. #include <lib/spinlock.h>
  24. #include <lib/utils_def.h>
  25. #include <libfdt.h>
  26. #include <plat/common/platform.h>
  27. #include <platform_def.h>
  28. struct stm32_osci_dt_cfg {
  29. unsigned long freq;
  30. bool bypass;
  31. bool digbyp;
  32. bool css;
  33. uint32_t drive;
  34. };
  35. enum pll_mn {
  36. PLL_CFG_M,
  37. PLL_CFG_N,
  38. PLL_DIV_MN_NB
  39. };
  40. enum pll_pqr {
  41. PLL_CFG_P,
  42. PLL_CFG_Q,
  43. PLL_CFG_R,
  44. PLL_DIV_PQR_NB
  45. };
  46. enum pll_csg {
  47. PLL_CSG_MOD_PER,
  48. PLL_CSG_INC_STEP,
  49. PLL_CSG_SSCG_MODE,
  50. PLL_CSG_NB
  51. };
  52. struct stm32_pll_vco {
  53. uint32_t status;
  54. uint32_t src;
  55. uint32_t div_mn[PLL_DIV_MN_NB];
  56. uint32_t frac;
  57. bool csg_enabled;
  58. uint32_t csg[PLL_CSG_NB];
  59. };
  60. struct stm32_pll_output {
  61. uint32_t output[PLL_DIV_PQR_NB];
  62. };
  63. struct stm32_pll_dt_cfg {
  64. struct stm32_pll_vco vco;
  65. struct stm32_pll_output output;
  66. };
  67. struct stm32_clk_platdata {
  68. uint32_t nosci;
  69. struct stm32_osci_dt_cfg *osci;
  70. uint32_t npll;
  71. struct stm32_pll_dt_cfg *pll;
  72. uint32_t nclksrc;
  73. uint32_t *clksrc;
  74. uint32_t nclkdiv;
  75. uint32_t *clkdiv;
  76. };
  77. enum stm32_clock {
  78. /* ROOT CLOCKS */
  79. _CK_OFF,
  80. _CK_HSI,
  81. _CK_HSE,
  82. _CK_CSI,
  83. _CK_LSI,
  84. _CK_LSE,
  85. _I2SCKIN,
  86. _CSI_DIV122,
  87. _HSE_DIV,
  88. _HSE_DIV2,
  89. _CK_PLL1,
  90. _CK_PLL2,
  91. _CK_PLL3,
  92. _CK_PLL4,
  93. _PLL1P,
  94. _PLL1P_DIV,
  95. _PLL2P,
  96. _PLL2Q,
  97. _PLL2R,
  98. _PLL3P,
  99. _PLL3Q,
  100. _PLL3R,
  101. _PLL4P,
  102. _PLL4Q,
  103. _PLL4R,
  104. _PCLK1,
  105. _PCLK2,
  106. _PCLK3,
  107. _PCLK4,
  108. _PCLK5,
  109. _PCLK6,
  110. _CKMPU,
  111. _CKAXI,
  112. _CKMLAHB,
  113. _CKPER,
  114. _CKTIMG1,
  115. _CKTIMG2,
  116. _CKTIMG3,
  117. _USB_PHY_48,
  118. _MCO1_K,
  119. _MCO2_K,
  120. _TRACECK,
  121. /* BUS and KERNEL CLOCKS */
  122. _DDRC1,
  123. _DDRC1LP,
  124. _DDRPHYC,
  125. _DDRPHYCLP,
  126. _DDRCAPB,
  127. _DDRCAPBLP,
  128. _AXIDCG,
  129. _DDRPHYCAPB,
  130. _DDRPHYCAPBLP,
  131. _SYSCFG,
  132. _DDRPERFM,
  133. _IWDG2APB,
  134. _USBPHY_K,
  135. _USBO_K,
  136. _RTCAPB,
  137. _TZC,
  138. _ETZPC,
  139. _IWDG1APB,
  140. _BSEC,
  141. _STGENC,
  142. _USART1_K,
  143. _USART2_K,
  144. _I2C3_K,
  145. _I2C4_K,
  146. _I2C5_K,
  147. _TIM12,
  148. _TIM15,
  149. _RTCCK,
  150. _GPIOA,
  151. _GPIOB,
  152. _GPIOC,
  153. _GPIOD,
  154. _GPIOE,
  155. _GPIOF,
  156. _GPIOG,
  157. _GPIOH,
  158. _GPIOI,
  159. _PKA,
  160. _SAES_K,
  161. _CRYP1,
  162. _HASH1,
  163. _RNG1_K,
  164. _BKPSRAM,
  165. _SDMMC1_K,
  166. _SDMMC2_K,
  167. _DBGCK,
  168. _USART3_K,
  169. _UART4_K,
  170. _UART5_K,
  171. _UART7_K,
  172. _UART8_K,
  173. _USART6_K,
  174. _MCE,
  175. _FMC_K,
  176. _QSPI_K,
  177. CK_LAST
  178. };
  179. /* PARENT CONFIG */
  180. static const uint16_t RTC_src[] = {
  181. _CK_OFF, _CK_LSE, _CK_LSI, _CK_HSE
  182. };
  183. static const uint16_t MCO1_src[] = {
  184. _CK_HSI, _CK_HSE, _CK_CSI, _CK_LSI, _CK_LSE
  185. };
  186. static const uint16_t MCO2_src[] = {
  187. _CKMPU, _CKAXI, _CKMLAHB, _PLL4P, _CK_HSE, _CK_HSI
  188. };
  189. static const uint16_t PLL12_src[] = {
  190. _CK_HSI, _CK_HSE
  191. };
  192. static const uint16_t PLL3_src[] = {
  193. _CK_HSI, _CK_HSE, _CK_CSI
  194. };
  195. static const uint16_t PLL4_src[] = {
  196. _CK_HSI, _CK_HSE, _CK_CSI, _I2SCKIN
  197. };
  198. static const uint16_t MPU_src[] = {
  199. _CK_HSI, _CK_HSE, _PLL1P, _PLL1P_DIV
  200. };
  201. static const uint16_t AXI_src[] = {
  202. _CK_HSI, _CK_HSE, _PLL2P
  203. };
  204. static const uint16_t MLAHBS_src[] = {
  205. _CK_HSI, _CK_HSE, _CK_CSI, _PLL3P
  206. };
  207. static const uint16_t CKPER_src[] = {
  208. _CK_HSI, _CK_CSI, _CK_HSE, _CK_OFF
  209. };
  210. static const uint16_t I2C12_src[] = {
  211. _PCLK1, _PLL4R, _CK_HSI, _CK_CSI
  212. };
  213. static const uint16_t I2C3_src[] = {
  214. _PCLK6, _PLL4R, _CK_HSI, _CK_CSI
  215. };
  216. static const uint16_t I2C4_src[] = {
  217. _PCLK6, _PLL4R, _CK_HSI, _CK_CSI
  218. };
  219. static const uint16_t I2C5_src[] = {
  220. _PCLK6, _PLL4R, _CK_HSI, _CK_CSI
  221. };
  222. static const uint16_t SPI1_src[] = {
  223. _PLL4P, _PLL3Q, _I2SCKIN, _CKPER, _PLL3R
  224. };
  225. static const uint16_t SPI23_src[] = {
  226. _PLL4P, _PLL3Q, _I2SCKIN, _CKPER, _PLL3R
  227. };
  228. static const uint16_t SPI4_src[] = {
  229. _PCLK6, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE, _I2SCKIN
  230. };
  231. static const uint16_t SPI5_src[] = {
  232. _PCLK6, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE
  233. };
  234. static const uint16_t UART1_src[] = {
  235. _PCLK6, _PLL3Q, _CK_HSI, _CK_CSI, _PLL4Q, _CK_HSE
  236. };
  237. static const uint16_t UART2_src[] = {
  238. _PCLK6, _PLL3Q, _CK_HSI, _CK_CSI, _PLL4Q, _CK_HSE
  239. };
  240. static const uint16_t UART35_src[] = {
  241. _PCLK1, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE
  242. };
  243. static const uint16_t UART4_src[] = {
  244. _PCLK1, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE
  245. };
  246. static const uint16_t UART6_src[] = {
  247. _PCLK2, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE
  248. };
  249. static const uint16_t UART78_src[] = {
  250. _PCLK1, _PLL4Q, _CK_HSI, _CK_CSI, _CK_HSE
  251. };
  252. static const uint16_t LPTIM1_src[] = {
  253. _PCLK1, _PLL4P, _PLL3Q, _CK_LSE, _CK_LSI, _CKPER
  254. };
  255. static const uint16_t LPTIM2_src[] = {
  256. _PCLK3, _PLL4Q, _CKPER, _CK_LSE, _CK_LSI
  257. };
  258. static const uint16_t LPTIM3_src[] = {
  259. _PCLK3, _PLL4Q, _CKPER, _CK_LSE, _CK_LSI
  260. };
  261. static const uint16_t LPTIM45_src[] = {
  262. _PCLK3, _PLL4P, _PLL3Q, _CK_LSE, _CK_LSI, _CKPER
  263. };
  264. static const uint16_t SAI1_src[] = {
  265. _PLL4Q, _PLL3Q, _I2SCKIN, _CKPER, _PLL3R
  266. };
  267. static const uint16_t SAI2_src[] = {
  268. _PLL4Q, _PLL3Q, _I2SCKIN, _CKPER, _NO_ID, _PLL3R
  269. };
  270. static const uint16_t FDCAN_src[] = {
  271. _CK_HSE, _PLL3Q, _PLL4Q, _PLL4R
  272. };
  273. static const uint16_t SPDIF_src[] = {
  274. _PLL4P, _PLL3Q, _CK_HSI
  275. };
  276. static const uint16_t ADC1_src[] = {
  277. _PLL4R, _CKPER, _PLL3Q
  278. };
  279. static const uint16_t ADC2_src[] = {
  280. _PLL4R, _CKPER, _PLL3Q
  281. };
  282. static const uint16_t SDMMC1_src[] = {
  283. _CKAXI, _PLL3R, _PLL4P, _CK_HSI
  284. };
  285. static const uint16_t SDMMC2_src[] = {
  286. _CKAXI, _PLL3R, _PLL4P, _CK_HSI
  287. };
  288. static const uint16_t ETH1_src[] = {
  289. _PLL4P, _PLL3Q
  290. };
  291. static const uint16_t ETH2_src[] = {
  292. _PLL4P, _PLL3Q
  293. };
  294. static const uint16_t USBPHY_src[] = {
  295. _CK_HSE, _PLL4R, _HSE_DIV2
  296. };
  297. static const uint16_t USBO_src[] = {
  298. _PLL4R, _USB_PHY_48
  299. };
  300. static const uint16_t QSPI_src[] = {
  301. _CKAXI, _PLL3R, _PLL4P, _CKPER
  302. };
  303. static const uint16_t FMC_src[] = {
  304. _CKAXI, _PLL3R, _PLL4P, _CKPER
  305. };
  306. /* Position 2 of RNG1 mux is reserved */
  307. static const uint16_t RNG1_src[] = {
  308. _CK_CSI, _PLL4R, _CK_OFF, _CK_LSI
  309. };
  310. static const uint16_t STGEN_src[] = {
  311. _CK_HSI, _CK_HSE
  312. };
  313. static const uint16_t DCMIPP_src[] = {
  314. _CKAXI, _PLL2Q, _PLL4P, _CKPER
  315. };
  316. static const uint16_t SAES_src[] = {
  317. _CKAXI, _CKPER, _PLL4R, _CK_LSI
  318. };
  319. #define MUX_CFG(id, src, _offset, _shift, _witdh)[id] = {\
  320. .id_parents = src,\
  321. .num_parents = ARRAY_SIZE(src),\
  322. .mux = &(struct mux_cfg) {\
  323. .offset = (_offset),\
  324. .shift = (_shift),\
  325. .width = (_witdh),\
  326. .bitrdy = MUX_NO_BIT_RDY,\
  327. },\
  328. }
  329. #define MUX_RDY_CFG(id, src, _offset, _shift, _witdh)[id] = {\
  330. .id_parents = src,\
  331. .num_parents = ARRAY_SIZE(src),\
  332. .mux = &(struct mux_cfg) {\
  333. .offset = (_offset),\
  334. .shift = (_shift),\
  335. .width = (_witdh),\
  336. .bitrdy = 31,\
  337. },\
  338. }
  339. static const struct parent_cfg parent_mp13[MUX_MAX] = {
  340. MUX_CFG(MUX_ADC1, ADC1_src, RCC_ADC12CKSELR, 0, 2),
  341. MUX_CFG(MUX_ADC2, ADC2_src, RCC_ADC12CKSELR, 2, 2),
  342. MUX_RDY_CFG(MUX_AXI, AXI_src, RCC_ASSCKSELR, 0, 3),
  343. MUX_CFG(MUX_CKPER, CKPER_src, RCC_CPERCKSELR, 0, 2),
  344. MUX_CFG(MUX_DCMIPP, DCMIPP_src, RCC_DCMIPPCKSELR, 0, 2),
  345. MUX_CFG(MUX_ETH1, ETH1_src, RCC_ETH12CKSELR, 0, 2),
  346. MUX_CFG(MUX_ETH2, ETH2_src, RCC_ETH12CKSELR, 8, 2),
  347. MUX_CFG(MUX_FDCAN, FDCAN_src, RCC_FDCANCKSELR, 0, 2),
  348. MUX_CFG(MUX_FMC, FMC_src, RCC_FMCCKSELR, 0, 2),
  349. MUX_CFG(MUX_I2C12, I2C12_src, RCC_I2C12CKSELR, 0, 3),
  350. MUX_CFG(MUX_I2C3, I2C3_src, RCC_I2C345CKSELR, 0, 3),
  351. MUX_CFG(MUX_I2C4, I2C4_src, RCC_I2C345CKSELR, 3, 3),
  352. MUX_CFG(MUX_I2C5, I2C5_src, RCC_I2C345CKSELR, 6, 3),
  353. MUX_CFG(MUX_LPTIM1, LPTIM1_src, RCC_LPTIM1CKSELR, 0, 3),
  354. MUX_CFG(MUX_LPTIM2, LPTIM2_src, RCC_LPTIM23CKSELR, 0, 3),
  355. MUX_CFG(MUX_LPTIM3, LPTIM3_src, RCC_LPTIM23CKSELR, 3, 3),
  356. MUX_CFG(MUX_LPTIM45, LPTIM45_src, RCC_LPTIM45CKSELR, 0, 3),
  357. MUX_CFG(MUX_MCO1, MCO1_src, RCC_MCO1CFGR, 0, 3),
  358. MUX_CFG(MUX_MCO2, MCO2_src, RCC_MCO2CFGR, 0, 3),
  359. MUX_RDY_CFG(MUX_MLAHB, MLAHBS_src, RCC_MSSCKSELR, 0, 2),
  360. MUX_RDY_CFG(MUX_MPU, MPU_src, RCC_MPCKSELR, 0, 2),
  361. MUX_RDY_CFG(MUX_PLL12, PLL12_src, RCC_RCK12SELR, 0, 2),
  362. MUX_RDY_CFG(MUX_PLL3, PLL3_src, RCC_RCK3SELR, 0, 2),
  363. MUX_RDY_CFG(MUX_PLL4, PLL4_src, RCC_RCK4SELR, 0, 2),
  364. MUX_CFG(MUX_QSPI, QSPI_src, RCC_QSPICKSELR, 0, 2),
  365. MUX_CFG(MUX_RNG1, RNG1_src, RCC_RNG1CKSELR, 0, 2),
  366. MUX_CFG(MUX_RTC, RTC_src, RCC_BDCR, 16, 2),
  367. MUX_CFG(MUX_SAES, SAES_src, RCC_SAESCKSELR, 0, 2),
  368. MUX_CFG(MUX_SAI1, SAI1_src, RCC_SAI1CKSELR, 0, 3),
  369. MUX_CFG(MUX_SAI2, SAI2_src, RCC_SAI2CKSELR, 0, 3),
  370. MUX_CFG(MUX_SDMMC1, SDMMC1_src, RCC_SDMMC12CKSELR, 0, 3),
  371. MUX_CFG(MUX_SDMMC2, SDMMC2_src, RCC_SDMMC12CKSELR, 3, 3),
  372. MUX_CFG(MUX_SPDIF, SPDIF_src, RCC_SPDIFCKSELR, 0, 2),
  373. MUX_CFG(MUX_SPI1, SPI1_src, RCC_SPI2S1CKSELR, 0, 3),
  374. MUX_CFG(MUX_SPI23, SPI23_src, RCC_SPI2S23CKSELR, 0, 3),
  375. MUX_CFG(MUX_SPI4, SPI4_src, RCC_SPI45CKSELR, 0, 3),
  376. MUX_CFG(MUX_SPI5, SPI5_src, RCC_SPI45CKSELR, 3, 3),
  377. MUX_CFG(MUX_STGEN, STGEN_src, RCC_STGENCKSELR, 0, 2),
  378. MUX_CFG(MUX_UART1, UART1_src, RCC_UART12CKSELR, 0, 3),
  379. MUX_CFG(MUX_UART2, UART2_src, RCC_UART12CKSELR, 3, 3),
  380. MUX_CFG(MUX_UART35, UART35_src, RCC_UART35CKSELR, 0, 3),
  381. MUX_CFG(MUX_UART4, UART4_src, RCC_UART4CKSELR, 0, 3),
  382. MUX_CFG(MUX_UART6, UART6_src, RCC_UART6CKSELR, 0, 3),
  383. MUX_CFG(MUX_UART78, UART78_src, RCC_UART78CKSELR, 0, 3),
  384. MUX_CFG(MUX_USBO, USBO_src, RCC_USBCKSELR, 4, 1),
  385. MUX_CFG(MUX_USBPHY, USBPHY_src, RCC_USBCKSELR, 0, 2),
  386. };
  387. /*
  388. * GATE CONFIG
  389. */
  390. enum enum_gate_cfg {
  391. GATE_ZERO, /* reserved for no gate */
  392. GATE_LSE,
  393. GATE_RTCCK,
  394. GATE_LSI,
  395. GATE_HSI,
  396. GATE_CSI,
  397. GATE_HSE,
  398. GATE_LSI_RDY,
  399. GATE_CSI_RDY,
  400. GATE_LSE_RDY,
  401. GATE_HSE_RDY,
  402. GATE_HSI_RDY,
  403. GATE_MCO1,
  404. GATE_MCO2,
  405. GATE_DBGCK,
  406. GATE_TRACECK,
  407. GATE_PLL1,
  408. GATE_PLL1_DIVP,
  409. GATE_PLL1_DIVQ,
  410. GATE_PLL1_DIVR,
  411. GATE_PLL2,
  412. GATE_PLL2_DIVP,
  413. GATE_PLL2_DIVQ,
  414. GATE_PLL2_DIVR,
  415. GATE_PLL3,
  416. GATE_PLL3_DIVP,
  417. GATE_PLL3_DIVQ,
  418. GATE_PLL3_DIVR,
  419. GATE_PLL4,
  420. GATE_PLL4_DIVP,
  421. GATE_PLL4_DIVQ,
  422. GATE_PLL4_DIVR,
  423. GATE_DDRC1,
  424. GATE_DDRC1LP,
  425. GATE_DDRPHYC,
  426. GATE_DDRPHYCLP,
  427. GATE_DDRCAPB,
  428. GATE_DDRCAPBLP,
  429. GATE_AXIDCG,
  430. GATE_DDRPHYCAPB,
  431. GATE_DDRPHYCAPBLP,
  432. GATE_TIM2,
  433. GATE_TIM3,
  434. GATE_TIM4,
  435. GATE_TIM5,
  436. GATE_TIM6,
  437. GATE_TIM7,
  438. GATE_LPTIM1,
  439. GATE_SPI2,
  440. GATE_SPI3,
  441. GATE_USART3,
  442. GATE_UART4,
  443. GATE_UART5,
  444. GATE_UART7,
  445. GATE_UART8,
  446. GATE_I2C1,
  447. GATE_I2C2,
  448. GATE_SPDIF,
  449. GATE_TIM1,
  450. GATE_TIM8,
  451. GATE_SPI1,
  452. GATE_USART6,
  453. GATE_SAI1,
  454. GATE_SAI2,
  455. GATE_DFSDM,
  456. GATE_ADFSDM,
  457. GATE_FDCAN,
  458. GATE_LPTIM2,
  459. GATE_LPTIM3,
  460. GATE_LPTIM4,
  461. GATE_LPTIM5,
  462. GATE_VREF,
  463. GATE_DTS,
  464. GATE_PMBCTRL,
  465. GATE_HDP,
  466. GATE_SYSCFG,
  467. GATE_DCMIPP,
  468. GATE_DDRPERFM,
  469. GATE_IWDG2APB,
  470. GATE_USBPHY,
  471. GATE_STGENRO,
  472. GATE_LTDC,
  473. GATE_RTCAPB,
  474. GATE_TZC,
  475. GATE_ETZPC,
  476. GATE_IWDG1APB,
  477. GATE_BSEC,
  478. GATE_STGENC,
  479. GATE_USART1,
  480. GATE_USART2,
  481. GATE_SPI4,
  482. GATE_SPI5,
  483. GATE_I2C3,
  484. GATE_I2C4,
  485. GATE_I2C5,
  486. GATE_TIM12,
  487. GATE_TIM13,
  488. GATE_TIM14,
  489. GATE_TIM15,
  490. GATE_TIM16,
  491. GATE_TIM17,
  492. GATE_DMA1,
  493. GATE_DMA2,
  494. GATE_DMAMUX1,
  495. GATE_DMA3,
  496. GATE_DMAMUX2,
  497. GATE_ADC1,
  498. GATE_ADC2,
  499. GATE_USBO,
  500. GATE_TSC,
  501. GATE_GPIOA,
  502. GATE_GPIOB,
  503. GATE_GPIOC,
  504. GATE_GPIOD,
  505. GATE_GPIOE,
  506. GATE_GPIOF,
  507. GATE_GPIOG,
  508. GATE_GPIOH,
  509. GATE_GPIOI,
  510. GATE_PKA,
  511. GATE_SAES,
  512. GATE_CRYP1,
  513. GATE_HASH1,
  514. GATE_RNG1,
  515. GATE_BKPSRAM,
  516. GATE_AXIMC,
  517. GATE_MCE,
  518. GATE_ETH1CK,
  519. GATE_ETH1TX,
  520. GATE_ETH1RX,
  521. GATE_ETH1MAC,
  522. GATE_FMC,
  523. GATE_QSPI,
  524. GATE_SDMMC1,
  525. GATE_SDMMC2,
  526. GATE_CRC1,
  527. GATE_USBH,
  528. GATE_ETH2CK,
  529. GATE_ETH2TX,
  530. GATE_ETH2RX,
  531. GATE_ETH2MAC,
  532. GATE_MDMA,
  533. LAST_GATE
  534. };
  535. #define GATE_CFG(id, _offset, _bit_idx, _offset_clr)[id] = {\
  536. .offset = (_offset),\
  537. .bit_idx = (_bit_idx),\
  538. .set_clr = (_offset_clr),\
  539. }
  540. static const struct gate_cfg gates_mp13[LAST_GATE] = {
  541. GATE_CFG(GATE_LSE, RCC_BDCR, 0, 0),
  542. GATE_CFG(GATE_RTCCK, RCC_BDCR, 20, 0),
  543. GATE_CFG(GATE_LSI, RCC_RDLSICR, 0, 0),
  544. GATE_CFG(GATE_HSI, RCC_OCENSETR, 0, 1),
  545. GATE_CFG(GATE_CSI, RCC_OCENSETR, 4, 1),
  546. GATE_CFG(GATE_HSE, RCC_OCENSETR, 8, 1),
  547. GATE_CFG(GATE_LSI_RDY, RCC_RDLSICR, 1, 0),
  548. GATE_CFG(GATE_CSI_RDY, RCC_OCRDYR, 4, 0),
  549. GATE_CFG(GATE_LSE_RDY, RCC_BDCR, 2, 0),
  550. GATE_CFG(GATE_HSE_RDY, RCC_OCRDYR, 8, 0),
  551. GATE_CFG(GATE_HSI_RDY, RCC_OCRDYR, 0, 0),
  552. GATE_CFG(GATE_MCO1, RCC_MCO1CFGR, 12, 0),
  553. GATE_CFG(GATE_MCO2, RCC_MCO2CFGR, 12, 0),
  554. GATE_CFG(GATE_DBGCK, RCC_DBGCFGR, 8, 0),
  555. GATE_CFG(GATE_TRACECK, RCC_DBGCFGR, 9, 0),
  556. GATE_CFG(GATE_PLL1, RCC_PLL1CR, 0, 0),
  557. GATE_CFG(GATE_PLL1_DIVP, RCC_PLL1CR, 4, 0),
  558. GATE_CFG(GATE_PLL1_DIVQ, RCC_PLL1CR, 5, 0),
  559. GATE_CFG(GATE_PLL1_DIVR, RCC_PLL1CR, 6, 0),
  560. GATE_CFG(GATE_PLL2, RCC_PLL2CR, 0, 0),
  561. GATE_CFG(GATE_PLL2_DIVP, RCC_PLL2CR, 4, 0),
  562. GATE_CFG(GATE_PLL2_DIVQ, RCC_PLL2CR, 5, 0),
  563. GATE_CFG(GATE_PLL2_DIVR, RCC_PLL2CR, 6, 0),
  564. GATE_CFG(GATE_PLL3, RCC_PLL3CR, 0, 0),
  565. GATE_CFG(GATE_PLL3_DIVP, RCC_PLL3CR, 4, 0),
  566. GATE_CFG(GATE_PLL3_DIVQ, RCC_PLL3CR, 5, 0),
  567. GATE_CFG(GATE_PLL3_DIVR, RCC_PLL3CR, 6, 0),
  568. GATE_CFG(GATE_PLL4, RCC_PLL4CR, 0, 0),
  569. GATE_CFG(GATE_PLL4_DIVP, RCC_PLL4CR, 4, 0),
  570. GATE_CFG(GATE_PLL4_DIVQ, RCC_PLL4CR, 5, 0),
  571. GATE_CFG(GATE_PLL4_DIVR, RCC_PLL4CR, 6, 0),
  572. GATE_CFG(GATE_DDRC1, RCC_DDRITFCR, 0, 0),
  573. GATE_CFG(GATE_DDRC1LP, RCC_DDRITFCR, 1, 0),
  574. GATE_CFG(GATE_DDRPHYC, RCC_DDRITFCR, 4, 0),
  575. GATE_CFG(GATE_DDRPHYCLP, RCC_DDRITFCR, 5, 0),
  576. GATE_CFG(GATE_DDRCAPB, RCC_DDRITFCR, 6, 0),
  577. GATE_CFG(GATE_DDRCAPBLP, RCC_DDRITFCR, 7, 0),
  578. GATE_CFG(GATE_AXIDCG, RCC_DDRITFCR, 8, 0),
  579. GATE_CFG(GATE_DDRPHYCAPB, RCC_DDRITFCR, 9, 0),
  580. GATE_CFG(GATE_DDRPHYCAPBLP, RCC_DDRITFCR, 10, 0),
  581. GATE_CFG(GATE_TIM2, RCC_MP_APB1ENSETR, 0, 1),
  582. GATE_CFG(GATE_TIM3, RCC_MP_APB1ENSETR, 1, 1),
  583. GATE_CFG(GATE_TIM4, RCC_MP_APB1ENSETR, 2, 1),
  584. GATE_CFG(GATE_TIM5, RCC_MP_APB1ENSETR, 3, 1),
  585. GATE_CFG(GATE_TIM6, RCC_MP_APB1ENSETR, 4, 1),
  586. GATE_CFG(GATE_TIM7, RCC_MP_APB1ENSETR, 5, 1),
  587. GATE_CFG(GATE_LPTIM1, RCC_MP_APB1ENSETR, 9, 1),
  588. GATE_CFG(GATE_SPI2, RCC_MP_APB1ENSETR, 11, 1),
  589. GATE_CFG(GATE_SPI3, RCC_MP_APB1ENSETR, 12, 1),
  590. GATE_CFG(GATE_USART3, RCC_MP_APB1ENSETR, 15, 1),
  591. GATE_CFG(GATE_UART4, RCC_MP_APB1ENSETR, 16, 1),
  592. GATE_CFG(GATE_UART5, RCC_MP_APB1ENSETR, 17, 1),
  593. GATE_CFG(GATE_UART7, RCC_MP_APB1ENSETR, 18, 1),
  594. GATE_CFG(GATE_UART8, RCC_MP_APB1ENSETR, 19, 1),
  595. GATE_CFG(GATE_I2C1, RCC_MP_APB1ENSETR, 21, 1),
  596. GATE_CFG(GATE_I2C2, RCC_MP_APB1ENSETR, 22, 1),
  597. GATE_CFG(GATE_SPDIF, RCC_MP_APB1ENSETR, 26, 1),
  598. GATE_CFG(GATE_TIM1, RCC_MP_APB2ENSETR, 0, 1),
  599. GATE_CFG(GATE_TIM8, RCC_MP_APB2ENSETR, 1, 1),
  600. GATE_CFG(GATE_SPI1, RCC_MP_APB2ENSETR, 8, 1),
  601. GATE_CFG(GATE_USART6, RCC_MP_APB2ENSETR, 13, 1),
  602. GATE_CFG(GATE_SAI1, RCC_MP_APB2ENSETR, 16, 1),
  603. GATE_CFG(GATE_SAI2, RCC_MP_APB2ENSETR, 17, 1),
  604. GATE_CFG(GATE_DFSDM, RCC_MP_APB2ENSETR, 20, 1),
  605. GATE_CFG(GATE_ADFSDM, RCC_MP_APB2ENSETR, 21, 1),
  606. GATE_CFG(GATE_FDCAN, RCC_MP_APB2ENSETR, 24, 1),
  607. GATE_CFG(GATE_LPTIM2, RCC_MP_APB3ENSETR, 0, 1),
  608. GATE_CFG(GATE_LPTIM3, RCC_MP_APB3ENSETR, 1, 1),
  609. GATE_CFG(GATE_LPTIM4, RCC_MP_APB3ENSETR, 2, 1),
  610. GATE_CFG(GATE_LPTIM5, RCC_MP_APB3ENSETR, 3, 1),
  611. GATE_CFG(GATE_VREF, RCC_MP_APB3ENSETR, 13, 1),
  612. GATE_CFG(GATE_DTS, RCC_MP_APB3ENSETR, 16, 1),
  613. GATE_CFG(GATE_PMBCTRL, RCC_MP_APB3ENSETR, 17, 1),
  614. GATE_CFG(GATE_HDP, RCC_MP_APB3ENSETR, 20, 1),
  615. GATE_CFG(GATE_SYSCFG, RCC_MP_S_APB3ENSETR, 0, 1),
  616. GATE_CFG(GATE_DCMIPP, RCC_MP_APB4ENSETR, 1, 1),
  617. GATE_CFG(GATE_DDRPERFM, RCC_MP_APB4ENSETR, 8, 1),
  618. GATE_CFG(GATE_IWDG2APB, RCC_MP_APB4ENSETR, 15, 1),
  619. GATE_CFG(GATE_USBPHY, RCC_MP_APB4ENSETR, 16, 1),
  620. GATE_CFG(GATE_STGENRO, RCC_MP_APB4ENSETR, 20, 1),
  621. GATE_CFG(GATE_LTDC, RCC_MP_S_APB4ENSETR, 0, 1),
  622. GATE_CFG(GATE_RTCAPB, RCC_MP_APB5ENSETR, 8, 1),
  623. GATE_CFG(GATE_TZC, RCC_MP_APB5ENSETR, 11, 1),
  624. GATE_CFG(GATE_ETZPC, RCC_MP_APB5ENSETR, 13, 1),
  625. GATE_CFG(GATE_IWDG1APB, RCC_MP_APB5ENSETR, 15, 1),
  626. GATE_CFG(GATE_BSEC, RCC_MP_APB5ENSETR, 16, 1),
  627. GATE_CFG(GATE_STGENC, RCC_MP_APB5ENSETR, 20, 1),
  628. GATE_CFG(GATE_USART1, RCC_MP_APB6ENSETR, 0, 1),
  629. GATE_CFG(GATE_USART2, RCC_MP_APB6ENSETR, 1, 1),
  630. GATE_CFG(GATE_SPI4, RCC_MP_APB6ENSETR, 2, 1),
  631. GATE_CFG(GATE_SPI5, RCC_MP_APB6ENSETR, 3, 1),
  632. GATE_CFG(GATE_I2C3, RCC_MP_APB6ENSETR, 4, 1),
  633. GATE_CFG(GATE_I2C4, RCC_MP_APB6ENSETR, 5, 1),
  634. GATE_CFG(GATE_I2C5, RCC_MP_APB6ENSETR, 6, 1),
  635. GATE_CFG(GATE_TIM12, RCC_MP_APB6ENSETR, 7, 1),
  636. GATE_CFG(GATE_TIM13, RCC_MP_APB6ENSETR, 8, 1),
  637. GATE_CFG(GATE_TIM14, RCC_MP_APB6ENSETR, 9, 1),
  638. GATE_CFG(GATE_TIM15, RCC_MP_APB6ENSETR, 10, 1),
  639. GATE_CFG(GATE_TIM16, RCC_MP_APB6ENSETR, 11, 1),
  640. GATE_CFG(GATE_TIM17, RCC_MP_APB6ENSETR, 12, 1),
  641. GATE_CFG(GATE_DMA1, RCC_MP_AHB2ENSETR, 0, 1),
  642. GATE_CFG(GATE_DMA2, RCC_MP_AHB2ENSETR, 1, 1),
  643. GATE_CFG(GATE_DMAMUX1, RCC_MP_AHB2ENSETR, 2, 1),
  644. GATE_CFG(GATE_DMA3, RCC_MP_AHB2ENSETR, 3, 1),
  645. GATE_CFG(GATE_DMAMUX2, RCC_MP_AHB2ENSETR, 4, 1),
  646. GATE_CFG(GATE_ADC1, RCC_MP_AHB2ENSETR, 5, 1),
  647. GATE_CFG(GATE_ADC2, RCC_MP_AHB2ENSETR, 6, 1),
  648. GATE_CFG(GATE_USBO, RCC_MP_AHB2ENSETR, 8, 1),
  649. GATE_CFG(GATE_TSC, RCC_MP_AHB4ENSETR, 15, 1),
  650. GATE_CFG(GATE_GPIOA, RCC_MP_S_AHB4ENSETR, 0, 1),
  651. GATE_CFG(GATE_GPIOB, RCC_MP_S_AHB4ENSETR, 1, 1),
  652. GATE_CFG(GATE_GPIOC, RCC_MP_S_AHB4ENSETR, 2, 1),
  653. GATE_CFG(GATE_GPIOD, RCC_MP_S_AHB4ENSETR, 3, 1),
  654. GATE_CFG(GATE_GPIOE, RCC_MP_S_AHB4ENSETR, 4, 1),
  655. GATE_CFG(GATE_GPIOF, RCC_MP_S_AHB4ENSETR, 5, 1),
  656. GATE_CFG(GATE_GPIOG, RCC_MP_S_AHB4ENSETR, 6, 1),
  657. GATE_CFG(GATE_GPIOH, RCC_MP_S_AHB4ENSETR, 7, 1),
  658. GATE_CFG(GATE_GPIOI, RCC_MP_S_AHB4ENSETR, 8, 1),
  659. GATE_CFG(GATE_PKA, RCC_MP_AHB5ENSETR, 2, 1),
  660. GATE_CFG(GATE_SAES, RCC_MP_AHB5ENSETR, 3, 1),
  661. GATE_CFG(GATE_CRYP1, RCC_MP_AHB5ENSETR, 4, 1),
  662. GATE_CFG(GATE_HASH1, RCC_MP_AHB5ENSETR, 5, 1),
  663. GATE_CFG(GATE_RNG1, RCC_MP_AHB5ENSETR, 6, 1),
  664. GATE_CFG(GATE_BKPSRAM, RCC_MP_AHB5ENSETR, 8, 1),
  665. GATE_CFG(GATE_AXIMC, RCC_MP_AHB5ENSETR, 16, 1),
  666. GATE_CFG(GATE_MCE, RCC_MP_AHB6ENSETR, 1, 1),
  667. GATE_CFG(GATE_ETH1CK, RCC_MP_AHB6ENSETR, 7, 1),
  668. GATE_CFG(GATE_ETH1TX, RCC_MP_AHB6ENSETR, 8, 1),
  669. GATE_CFG(GATE_ETH1RX, RCC_MP_AHB6ENSETR, 9, 1),
  670. GATE_CFG(GATE_ETH1MAC, RCC_MP_AHB6ENSETR, 10, 1),
  671. GATE_CFG(GATE_FMC, RCC_MP_AHB6ENSETR, 12, 1),
  672. GATE_CFG(GATE_QSPI, RCC_MP_AHB6ENSETR, 14, 1),
  673. GATE_CFG(GATE_SDMMC1, RCC_MP_AHB6ENSETR, 16, 1),
  674. GATE_CFG(GATE_SDMMC2, RCC_MP_AHB6ENSETR, 17, 1),
  675. GATE_CFG(GATE_CRC1, RCC_MP_AHB6ENSETR, 20, 1),
  676. GATE_CFG(GATE_USBH, RCC_MP_AHB6ENSETR, 24, 1),
  677. GATE_CFG(GATE_ETH2CK, RCC_MP_AHB6ENSETR, 27, 1),
  678. GATE_CFG(GATE_ETH2TX, RCC_MP_AHB6ENSETR, 28, 1),
  679. GATE_CFG(GATE_ETH2RX, RCC_MP_AHB6ENSETR, 29, 1),
  680. GATE_CFG(GATE_ETH2MAC, RCC_MP_AHB6ENSETR, 30, 1),
  681. GATE_CFG(GATE_MDMA, RCC_MP_S_AHB6ENSETR, 0, 1),
  682. };
  683. /*
  684. * DIV CONFIG
  685. */
  686. static const struct clk_div_table axi_div_table[] = {
  687. { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
  688. { 4, 4 }, { 5, 4 }, { 6, 4 }, { 7, 4 },
  689. { 0 },
  690. };
  691. static const struct clk_div_table mlahb_div_table[] = {
  692. { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
  693. { 4, 16 }, { 5, 32 }, { 6, 64 }, { 7, 128 },
  694. { 8, 256 }, { 9, 512 }, { 10, 512}, { 11, 512 },
  695. { 12, 512 }, { 13, 512 }, { 14, 512}, { 15, 512 },
  696. { 0 },
  697. };
  698. static const struct clk_div_table apb_div_table[] = {
  699. { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
  700. { 4, 16 }, { 5, 16 }, { 6, 16 }, { 7, 16 },
  701. { 0 },
  702. };
  703. #define DIV_CFG(id, _offset, _shift, _width, _flags, _table, _bitrdy)[id] = {\
  704. .offset = _offset,\
  705. .shift = _shift,\
  706. .width = _width,\
  707. .flags = _flags,\
  708. .table = _table,\
  709. .bitrdy = _bitrdy,\
  710. }
  711. static const struct div_cfg dividers_mp13[DIV_MAX] = {
  712. DIV_CFG(DIV_PLL1DIVP, RCC_PLL1CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY),
  713. DIV_CFG(DIV_PLL2DIVP, RCC_PLL2CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY),
  714. DIV_CFG(DIV_PLL2DIVQ, RCC_PLL2CFGR2, 8, 7, 0, NULL, DIV_NO_BIT_RDY),
  715. DIV_CFG(DIV_PLL2DIVR, RCC_PLL2CFGR2, 16, 7, 0, NULL, DIV_NO_BIT_RDY),
  716. DIV_CFG(DIV_PLL3DIVP, RCC_PLL3CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY),
  717. DIV_CFG(DIV_PLL3DIVQ, RCC_PLL3CFGR2, 8, 7, 0, NULL, DIV_NO_BIT_RDY),
  718. DIV_CFG(DIV_PLL3DIVR, RCC_PLL3CFGR2, 16, 7, 0, NULL, DIV_NO_BIT_RDY),
  719. DIV_CFG(DIV_PLL4DIVP, RCC_PLL4CFGR2, 0, 7, 0, NULL, DIV_NO_BIT_RDY),
  720. DIV_CFG(DIV_PLL4DIVQ, RCC_PLL4CFGR2, 8, 7, 0, NULL, DIV_NO_BIT_RDY),
  721. DIV_CFG(DIV_PLL4DIVR, RCC_PLL4CFGR2, 16, 7, 0, NULL, DIV_NO_BIT_RDY),
  722. DIV_CFG(DIV_MPU, RCC_MPCKDIVR, 0, 4, 0, NULL, DIV_NO_BIT_RDY),
  723. DIV_CFG(DIV_AXI, RCC_AXIDIVR, 0, 3, 0, axi_div_table, 31),
  724. DIV_CFG(DIV_MLAHB, RCC_MLAHBDIVR, 0, 4, 0, mlahb_div_table, 31),
  725. DIV_CFG(DIV_APB1, RCC_APB1DIVR, 0, 3, 0, apb_div_table, 31),
  726. DIV_CFG(DIV_APB2, RCC_APB2DIVR, 0, 3, 0, apb_div_table, 31),
  727. DIV_CFG(DIV_APB3, RCC_APB3DIVR, 0, 3, 0, apb_div_table, 31),
  728. DIV_CFG(DIV_APB4, RCC_APB4DIVR, 0, 3, 0, apb_div_table, 31),
  729. DIV_CFG(DIV_APB5, RCC_APB5DIVR, 0, 3, 0, apb_div_table, 31),
  730. DIV_CFG(DIV_APB6, RCC_APB6DIVR, 0, 3, 0, apb_div_table, 31),
  731. DIV_CFG(DIV_RTC, RCC_RTCDIVR, 0, 6, 0, NULL, DIV_NO_BIT_RDY),
  732. DIV_CFG(DIV_MCO1, RCC_MCO1CFGR, 4, 4, 0, NULL, DIV_NO_BIT_RDY),
  733. DIV_CFG(DIV_MCO2, RCC_MCO2CFGR, 4, 4, 0, NULL, DIV_NO_BIT_RDY),
  734. DIV_CFG(DIV_HSI, RCC_HSICFGR, 0, 2, CLK_DIVIDER_POWER_OF_TWO, NULL, DIV_NO_BIT_RDY),
  735. DIV_CFG(DIV_TRACE, RCC_DBGCFGR, 0, 3, CLK_DIVIDER_POWER_OF_TWO, NULL, DIV_NO_BIT_RDY),
  736. DIV_CFG(DIV_ETH1PTP, RCC_ETH12CKSELR, 4, 4, 0, NULL, DIV_NO_BIT_RDY),
  737. DIV_CFG(DIV_ETH2PTP, RCC_ETH12CKSELR, 12, 4, 0, NULL, DIV_NO_BIT_RDY),
  738. };
  739. #define MAX_HSI_HZ 64000000
  740. #define USB_PHY_48_MHZ 48000000
  741. #define TIMEOUT_US_200MS U(200000)
  742. #define TIMEOUT_US_1S U(1000000)
  743. #define PLLRDY_TIMEOUT TIMEOUT_US_200MS
  744. #define CLKSRC_TIMEOUT TIMEOUT_US_200MS
  745. #define CLKDIV_TIMEOUT TIMEOUT_US_200MS
  746. #define HSIDIV_TIMEOUT TIMEOUT_US_200MS
  747. #define OSCRDY_TIMEOUT TIMEOUT_US_1S
  748. enum stm32_osc {
  749. OSC_HSI,
  750. OSC_HSE,
  751. OSC_CSI,
  752. OSC_LSI,
  753. OSC_LSE,
  754. OSC_I2SCKIN,
  755. NB_OSCILLATOR
  756. };
  757. enum stm32mp1_pll_id {
  758. _PLL1,
  759. _PLL2,
  760. _PLL3,
  761. _PLL4,
  762. _PLL_NB
  763. };
  764. enum stm32mp1_plltype {
  765. PLL_800,
  766. PLL_1600,
  767. PLL_2000,
  768. PLL_TYPE_NB
  769. };
  770. #define RCC_OFFSET_PLLXCR 0
  771. #define RCC_OFFSET_PLLXCFGR1 4
  772. #define RCC_OFFSET_PLLXCFGR2 8
  773. #define RCC_OFFSET_PLLXFRACR 12
  774. #define RCC_OFFSET_PLLXCSGR 16
  775. struct stm32_clk_pll {
  776. enum stm32mp1_plltype plltype;
  777. uint16_t clk_id;
  778. uint16_t reg_pllxcr;
  779. };
  780. struct stm32mp1_pll {
  781. uint8_t refclk_min;
  782. uint8_t refclk_max;
  783. };
  784. /* Define characteristic of PLL according type */
  785. static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
  786. [PLL_800] = {
  787. .refclk_min = 4,
  788. .refclk_max = 16,
  789. },
  790. [PLL_1600] = {
  791. .refclk_min = 8,
  792. .refclk_max = 16,
  793. },
  794. [PLL_2000] = {
  795. .refclk_min = 8,
  796. .refclk_max = 16,
  797. },
  798. };
  799. #if STM32MP_USB_PROGRAMMER
  800. static bool pll4_bootrom;
  801. #endif
  802. /* RCC clock device driver private */
  803. static uint8_t refcounts_mp13[CK_LAST];
  804. static const struct stm32_clk_pll *clk_st32_pll_data(unsigned int idx);
  805. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  806. static void clk_oscillator_check_bypass(struct stm32_clk_priv *priv, int idx,
  807. bool digbyp, bool bypass)
  808. {
  809. struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, idx);
  810. struct stm32_clk_bypass *bypass_data = osc_data->bypass;
  811. uintptr_t address;
  812. if (bypass_data == NULL) {
  813. return;
  814. }
  815. address = priv->base + bypass_data->offset;
  816. if ((mmio_read_32(address) & RCC_OCENR_HSEBYP) &&
  817. (!(digbyp || bypass))) {
  818. panic();
  819. }
  820. }
  821. #endif
  822. static void stm32_enable_oscillator_hse(struct stm32_clk_priv *priv)
  823. {
  824. struct stm32_clk_platdata *pdata = priv->pdata;
  825. struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_HSE];
  826. bool digbyp = osci->digbyp;
  827. bool bypass = osci->bypass;
  828. bool css = osci->css;
  829. if (_clk_stm32_get_rate(priv, _CK_HSE) == 0U) {
  830. return;
  831. }
  832. clk_oscillator_set_bypass(priv, _CK_HSE, digbyp, bypass);
  833. _clk_stm32_enable(priv, _CK_HSE);
  834. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  835. clk_oscillator_check_bypass(priv, _CK_HSE, digbyp, bypass);
  836. #endif
  837. clk_oscillator_set_css(priv, _CK_HSE, css);
  838. }
  839. static void stm32_enable_oscillator_lse(struct stm32_clk_priv *priv)
  840. {
  841. struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, _CK_LSE);
  842. struct stm32_clk_platdata *pdata = priv->pdata;
  843. struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE];
  844. bool digbyp = osci->digbyp;
  845. bool bypass = osci->bypass;
  846. uint8_t drive = osci->drive;
  847. if (_clk_stm32_get_rate(priv, _CK_LSE) == 0U) {
  848. return;
  849. }
  850. /* Do not reconfigure LSE if already enabled */
  851. if (_clk_stm32_gate_is_enabled(priv, osc_data->gate_id)) {
  852. return;
  853. }
  854. clk_oscillator_set_bypass(priv, _CK_LSE, digbyp, bypass);
  855. clk_oscillator_set_drive(priv, _CK_LSE, drive);
  856. _clk_stm32_gate_enable(priv, osc_data->gate_id);
  857. }
  858. static int stm32mp1_set_hsidiv(uint8_t hsidiv)
  859. {
  860. uint64_t timeout;
  861. uintptr_t rcc_base = stm32mp_rcc_base();
  862. uintptr_t address = rcc_base + RCC_OCRDYR;
  863. mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
  864. RCC_HSICFGR_HSIDIV_MASK,
  865. RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
  866. timeout = timeout_init_us(HSIDIV_TIMEOUT);
  867. while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
  868. if (timeout_elapsed(timeout)) {
  869. EARLY_ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
  870. address, mmio_read_32(address));
  871. return -ETIMEDOUT;
  872. }
  873. }
  874. return 0;
  875. }
  876. static int stm32mp1_hsidiv(unsigned long hsifreq)
  877. {
  878. uint8_t hsidiv;
  879. uint32_t hsidivfreq = MAX_HSI_HZ;
  880. for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
  881. if (hsidivfreq == hsifreq) {
  882. break;
  883. }
  884. hsidivfreq /= 2U;
  885. }
  886. if (hsidiv == 4U) {
  887. EARLY_ERROR("Invalid clk-hsi frequency\n");
  888. return -EINVAL;
  889. }
  890. if (hsidiv != 0U) {
  891. return stm32mp1_set_hsidiv(hsidiv);
  892. }
  893. return 0;
  894. }
  895. static int stm32_clk_oscillators_lse_set_css(struct stm32_clk_priv *priv)
  896. {
  897. struct stm32_clk_platdata *pdata = priv->pdata;
  898. struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE];
  899. clk_oscillator_set_css(priv, _CK_LSE, osci->css);
  900. return 0;
  901. }
  902. static int stm32mp1_come_back_to_hsi(void)
  903. {
  904. int ret;
  905. struct stm32_clk_priv *priv = clk_stm32_get_priv();
  906. /* Come back to HSI */
  907. ret = _clk_stm32_set_parent(priv, _CKMPU, _CK_HSI);
  908. if (ret != 0) {
  909. return ret;
  910. }
  911. ret = _clk_stm32_set_parent(priv, _CKAXI, _CK_HSI);
  912. if (ret != 0) {
  913. return ret;
  914. }
  915. ret = _clk_stm32_set_parent(priv, _CKMLAHB, _CK_HSI);
  916. if (ret != 0) {
  917. return ret;
  918. }
  919. return 0;
  920. }
  921. static int stm32_clk_configure_clk_get_binding_id(struct stm32_clk_priv *priv, uint32_t data)
  922. {
  923. unsigned long binding_id = ((unsigned long)data & CLK_ID_MASK) >> CLK_ID_SHIFT;
  924. return clk_get_index(priv, binding_id);
  925. }
  926. static int stm32_clk_configure_clk(struct stm32_clk_priv *priv, uint32_t data)
  927. {
  928. int sel = (data & CLK_SEL_MASK) >> CLK_SEL_SHIFT;
  929. int enable = (data & CLK_ON_MASK) >> CLK_ON_SHIFT;
  930. int clk_id;
  931. int ret;
  932. clk_id = stm32_clk_configure_clk_get_binding_id(priv, data);
  933. if (clk_id < 0) {
  934. return clk_id;
  935. }
  936. ret = _clk_stm32_set_parent_by_index(priv, clk_id, sel);
  937. if (ret != 0) {
  938. return ret;
  939. }
  940. if (enable != 0) {
  941. clk_stm32_enable_call_ops(priv, clk_id);
  942. } else {
  943. clk_stm32_disable_call_ops(priv, clk_id);
  944. }
  945. return 0;
  946. }
  947. static int stm32_clk_configure_mux(struct stm32_clk_priv *priv, uint32_t data)
  948. {
  949. int mux = (data & MUX_ID_MASK) >> MUX_ID_SHIFT;
  950. int sel = (data & MUX_SEL_MASK) >> MUX_SEL_SHIFT;
  951. return clk_mux_set_parent(priv, mux, sel);
  952. }
  953. static int stm32_clk_dividers_configure(struct stm32_clk_priv *priv)
  954. {
  955. struct stm32_clk_platdata *pdata = priv->pdata;
  956. uint32_t i;
  957. for (i = 0; i < pdata->nclkdiv; i++) {
  958. int div_id, div_n;
  959. int val;
  960. int ret;
  961. val = pdata->clkdiv[i] & CMD_DATA_MASK;
  962. div_id = (val & DIV_ID_MASK) >> DIV_ID_SHIFT;
  963. div_n = (val & DIV_DIVN_MASK) >> DIV_DIVN_SHIFT;
  964. ret = clk_stm32_set_div(priv, div_id, div_n);
  965. if (ret != 0) {
  966. return ret;
  967. }
  968. }
  969. return 0;
  970. }
  971. static int stm32_clk_source_configure(struct stm32_clk_priv *priv)
  972. {
  973. struct stm32_clk_platdata *pdata = priv->pdata;
  974. bool ckper_disabled = false;
  975. int clk_id;
  976. int ret;
  977. uint32_t i;
  978. for (i = 0; i < pdata->nclksrc; i++) {
  979. uint32_t val = pdata->clksrc[i];
  980. uint32_t cmd, cmd_data;
  981. if (val == (uint32_t)CLK_CKPER_DISABLED) {
  982. ckper_disabled = true;
  983. continue;
  984. }
  985. if (val == (uint32_t)CLK_RTC_DISABLED) {
  986. continue;
  987. }
  988. cmd = (val & CMD_MASK) >> CMD_SHIFT;
  989. cmd_data = val & ~CMD_MASK;
  990. switch (cmd) {
  991. case CMD_MUX:
  992. ret = stm32_clk_configure_mux(priv, cmd_data);
  993. break;
  994. case CMD_CLK:
  995. clk_id = stm32_clk_configure_clk_get_binding_id(priv, cmd_data);
  996. if (clk_id == _RTCCK) {
  997. if ((_clk_stm32_is_enabled(priv, _RTCCK) == true)) {
  998. continue;
  999. }
  1000. }
  1001. ret = stm32_clk_configure_clk(priv, cmd_data);
  1002. break;
  1003. default:
  1004. ret = -EINVAL;
  1005. break;
  1006. }
  1007. if (ret != 0) {
  1008. return ret;
  1009. }
  1010. }
  1011. /*
  1012. * CKPER is source for some peripheral clocks
  1013. * (FMC-NAND / QPSI-NOR) and switching source is allowed
  1014. * only if previous clock is still ON
  1015. * => deactivate CKPER only after switching clock
  1016. */
  1017. if (ckper_disabled) {
  1018. ret = stm32_clk_configure_mux(priv, CLK_CKPER_DISABLED);
  1019. if (ret != 0) {
  1020. return ret;
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. static int stm32_clk_stgen_configure(struct stm32_clk_priv *priv, int id)
  1026. {
  1027. unsigned long stgen_freq;
  1028. stgen_freq = _clk_stm32_get_rate(priv, id);
  1029. stm32mp_stgen_config(stgen_freq);
  1030. return 0;
  1031. }
  1032. #define CLK_PLL_CFG(_idx, _clk_id, _type, _reg)\
  1033. [(_idx)] = {\
  1034. .clk_id = (_clk_id),\
  1035. .plltype = (_type),\
  1036. .reg_pllxcr = (_reg),\
  1037. }
  1038. static int clk_stm32_pll_compute_cfgr1(struct stm32_clk_priv *priv,
  1039. const struct stm32_clk_pll *pll,
  1040. struct stm32_pll_vco *vco,
  1041. uint32_t *value)
  1042. {
  1043. uint32_t divm = vco->div_mn[PLL_CFG_M];
  1044. uint32_t divn = vco->div_mn[PLL_CFG_N];
  1045. unsigned long prate = 0UL;
  1046. unsigned long refclk = 0UL;
  1047. prate = _clk_stm32_get_parent_rate(priv, pll->clk_id);
  1048. refclk = prate / (divm + 1U);
  1049. if ((refclk < (stm32mp1_pll[pll->plltype].refclk_min * 1000000U)) ||
  1050. (refclk > (stm32mp1_pll[pll->plltype].refclk_max * 1000000U))) {
  1051. return -EINVAL;
  1052. }
  1053. *value = 0;
  1054. if ((pll->plltype == PLL_800) && (refclk >= 8000000U)) {
  1055. *value = 1U << RCC_PLLNCFGR1_IFRGE_SHIFT;
  1056. }
  1057. *value |= (divn << RCC_PLLNCFGR1_DIVN_SHIFT) & RCC_PLLNCFGR1_DIVN_MASK;
  1058. *value |= (divm << RCC_PLLNCFGR1_DIVM_SHIFT) & RCC_PLLNCFGR1_DIVM_MASK;
  1059. return 0;
  1060. }
  1061. static uint32_t clk_stm32_pll_compute_cfgr2(struct stm32_pll_output *out)
  1062. {
  1063. uint32_t value = 0;
  1064. value |= (out->output[PLL_CFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & RCC_PLLNCFGR2_DIVP_MASK;
  1065. value |= (out->output[PLL_CFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & RCC_PLLNCFGR2_DIVQ_MASK;
  1066. value |= (out->output[PLL_CFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & RCC_PLLNCFGR2_DIVR_MASK;
  1067. return value;
  1068. }
  1069. static void clk_stm32_pll_config_vco(struct stm32_clk_priv *priv,
  1070. const struct stm32_clk_pll *pll,
  1071. struct stm32_pll_vco *vco)
  1072. {
  1073. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1074. uint32_t value = 0;
  1075. if (clk_stm32_pll_compute_cfgr1(priv, pll, vco, &value) != 0) {
  1076. EARLY_ERROR("Invalid Vref clock !\n");
  1077. panic();
  1078. }
  1079. /* Write N / M / IFREGE fields */
  1080. mmio_write_32(pll_base + RCC_OFFSET_PLLXCFGR1, value);
  1081. /* Fractional configuration */
  1082. mmio_write_32(pll_base + RCC_OFFSET_PLLXFRACR, 0);
  1083. /* Frac must be enabled only once its configuration is loaded */
  1084. mmio_write_32(pll_base + RCC_OFFSET_PLLXFRACR, vco->frac << RCC_PLLNFRACR_FRACV_SHIFT);
  1085. mmio_setbits_32(pll_base + RCC_OFFSET_PLLXFRACR, RCC_PLLNFRACR_FRACLE);
  1086. }
  1087. static void clk_stm32_pll_config_csg(struct stm32_clk_priv *priv,
  1088. const struct stm32_clk_pll *pll,
  1089. struct stm32_pll_vco *vco)
  1090. {
  1091. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1092. uint32_t mod_per = 0;
  1093. uint32_t inc_step = 0;
  1094. uint32_t sscg_mode = 0;
  1095. uint32_t value = 0;
  1096. if (!vco->csg_enabled) {
  1097. return;
  1098. }
  1099. mod_per = vco->csg[PLL_CSG_MOD_PER];
  1100. inc_step = vco->csg[PLL_CSG_INC_STEP];
  1101. sscg_mode = vco->csg[PLL_CSG_SSCG_MODE];
  1102. value |= (mod_per << RCC_PLLNCSGR_MOD_PER_SHIFT) & RCC_PLLNCSGR_MOD_PER_MASK;
  1103. value |= (inc_step << RCC_PLLNCSGR_INC_STEP_SHIFT) & RCC_PLLNCSGR_INC_STEP_MASK;
  1104. value |= (sscg_mode << RCC_PLLNCSGR_SSCG_MODE_SHIFT) & RCC_PLLNCSGR_SSCG_MODE_MASK;
  1105. mmio_write_32(pll_base + RCC_OFFSET_PLLXCSGR, value);
  1106. mmio_setbits_32(pll_base + RCC_OFFSET_PLLXCR, RCC_PLLNCR_SSCG_CTRL);
  1107. }
  1108. static void clk_stm32_pll_config_out(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll,
  1109. struct stm32_pll_output *out)
  1110. {
  1111. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1112. uint32_t value = 0;
  1113. value = clk_stm32_pll_compute_cfgr2(out);
  1114. mmio_write_32(pll_base + RCC_OFFSET_PLLXCFGR2, value);
  1115. }
  1116. static inline struct stm32_pll_dt_cfg *clk_stm32_pll_get_pdata(int pll_idx)
  1117. {
  1118. struct stm32_clk_priv *priv = clk_stm32_get_priv();
  1119. struct stm32_clk_platdata *pdata = priv->pdata;
  1120. return &pdata->pll[pll_idx];
  1121. }
  1122. /* Define characteristic for PLL1 : PLL_2000 */
  1123. #define POST_DIVM_MIN 8000000U
  1124. #define POST_DIVM_MAX 16000000U
  1125. #define DIVM_MIN 0U
  1126. #define DIVM_MAX 63U
  1127. #define DIVN_MIN 24U
  1128. #define DIVN_MAX 99U
  1129. #define DIVP_MIN 0U
  1130. #define DIVP_MAX 127U
  1131. #define FRAC_MAX 8192U
  1132. #define VCO_MIN 992000000U
  1133. #define VCO_MAX 2000000000U
  1134. static int clk_compute_pll1_settings(uint32_t freq_khz)
  1135. {
  1136. struct stm32_clk_priv *priv = clk_stm32_get_priv();
  1137. struct stm32_pll_dt_cfg *pll1 = clk_stm32_pll_get_pdata(_PLL1);
  1138. struct stm32_pll_dt_cfg *pll2 = clk_stm32_pll_get_pdata(_PLL2);
  1139. unsigned long long best_diff = ULLONG_MAX;
  1140. unsigned int divm;
  1141. unsigned long input_freq = 0UL;
  1142. uint32_t src = pll2->vco.src;
  1143. /* PLL1 share the same clock source than PLL2 */
  1144. switch (src) {
  1145. case CLK_PLL12_HSI:
  1146. input_freq = _clk_stm32_get_rate(priv, _CK_HSI);
  1147. break;
  1148. case CLK_PLL12_HSE:
  1149. input_freq = _clk_stm32_get_rate(priv, _CK_HSE);
  1150. break;
  1151. default:
  1152. break;
  1153. }
  1154. if (input_freq == 0UL) {
  1155. panic();
  1156. }
  1157. /* Following parameters have always the same value */
  1158. pll1->output.output[PLL_CFG_Q] = 0U;
  1159. pll1->output.output[PLL_CFG_R] = 0U;
  1160. for (divm = (DIVM_MAX + 1U); divm != DIVM_MIN; divm--) {
  1161. unsigned long post_divm = input_freq / divm;
  1162. unsigned int divp;
  1163. if ((post_divm < POST_DIVM_MIN) || (post_divm > POST_DIVM_MAX)) {
  1164. continue;
  1165. }
  1166. for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
  1167. unsigned long long output_freq = freq_khz * 1000ULL;
  1168. unsigned long long freq;
  1169. unsigned long long divn;
  1170. unsigned long long frac;
  1171. unsigned int i;
  1172. freq = output_freq * divm * (divp + 1U);
  1173. divn = (freq / input_freq) - 1U;
  1174. if ((divn < DIVN_MIN) || (divn > DIVN_MAX)) {
  1175. continue;
  1176. }
  1177. frac = ((freq * FRAC_MAX) / input_freq) - ((divn + 1U) * FRAC_MAX);
  1178. /* 2 loops to refine the fractional part */
  1179. for (i = 2U; i != 0U; i--) {
  1180. unsigned long long diff;
  1181. unsigned long long vco;
  1182. if (frac > FRAC_MAX) {
  1183. break;
  1184. }
  1185. vco = (post_divm * (divn + 1U)) + ((post_divm * frac) / FRAC_MAX);
  1186. if ((vco < (VCO_MIN / 2U)) || (vco > (VCO_MAX / 2U))) {
  1187. frac++;
  1188. continue;
  1189. }
  1190. freq = vco / (divp + 1U);
  1191. if (output_freq < freq) {
  1192. diff = freq - output_freq;
  1193. } else {
  1194. diff = output_freq - freq;
  1195. }
  1196. if (diff < best_diff) {
  1197. pll1->vco.src = src;
  1198. pll1->vco.status = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_PLLON;
  1199. pll1->vco.div_mn[PLL_CFG_M] = divm - 1U;
  1200. pll1->vco.div_mn[PLL_CFG_N] = (uint32_t)divn;
  1201. pll1->vco.frac = (uint32_t)frac;
  1202. pll1->output.output[PLL_CFG_P] = divp;
  1203. if (diff == 0U) {
  1204. return 0;
  1205. }
  1206. best_diff = diff;
  1207. }
  1208. frac++;
  1209. }
  1210. }
  1211. }
  1212. if (best_diff == ULLONG_MAX) {
  1213. return -EINVAL;
  1214. }
  1215. return 0;
  1216. }
  1217. static bool _clk_stm32_pll_is_enabled(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll)
  1218. {
  1219. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1220. return ((mmio_read_32(pll_base) & RCC_PLLNCR_PLLON) != 0U);
  1221. }
  1222. static void _clk_stm32_pll_set_on(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll)
  1223. {
  1224. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1225. /* Preserve RCC_PLLNCR_SSCG_CTRL value */
  1226. mmio_clrsetbits_32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN,
  1227. RCC_PLLNCR_PLLON);
  1228. }
  1229. static void _clk_stm32_pll_set_off(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll)
  1230. {
  1231. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1232. /* Stop all output */
  1233. mmio_clrbits_32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
  1234. /* Stop PLL */
  1235. mmio_clrbits_32(pll_base, RCC_PLLNCR_PLLON);
  1236. }
  1237. static int _clk_stm32_pll_wait_ready_on(struct stm32_clk_priv *priv,
  1238. const struct stm32_clk_pll *pll)
  1239. {
  1240. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1241. uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
  1242. /* Wait PLL lock */
  1243. while ((mmio_read_32(pll_base) & RCC_PLLNCR_PLLRDY) == 0U) {
  1244. if (timeout_elapsed(timeout)) {
  1245. ERROR("PLL%d start failed @ 0x%x: 0x%x\n",
  1246. pll->clk_id - _CK_PLL1 + 1, pll->reg_pllxcr, mmio_read_32(pll_base));
  1247. return -EINVAL;
  1248. }
  1249. }
  1250. return 0;
  1251. }
  1252. static int _clk_stm32_pll_wait_ready_off(struct stm32_clk_priv *priv,
  1253. const struct stm32_clk_pll *pll)
  1254. {
  1255. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1256. uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
  1257. /* Wait PLL lock */
  1258. while ((mmio_read_32(pll_base) & RCC_PLLNCR_PLLRDY) != 0U) {
  1259. if (timeout_elapsed(timeout)) {
  1260. ERROR("PLL%d stop failed @ 0x%x: 0x%x\n",
  1261. pll->clk_id - _CK_PLL1 + 1, pll->reg_pllxcr, mmio_read_32(pll_base));
  1262. return -EINVAL;
  1263. }
  1264. }
  1265. return 0;
  1266. }
  1267. static int _clk_stm32_pll_enable(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll)
  1268. {
  1269. if (_clk_stm32_pll_is_enabled(priv, pll)) {
  1270. return 0;
  1271. }
  1272. /* Preserve RCC_PLLNCR_SSCG_CTRL value */
  1273. _clk_stm32_pll_set_on(priv, pll);
  1274. /* Wait PLL lock */
  1275. return _clk_stm32_pll_wait_ready_on(priv, pll);
  1276. }
  1277. static void _clk_stm32_pll_disable(struct stm32_clk_priv *priv, const struct stm32_clk_pll *pll)
  1278. {
  1279. if (!_clk_stm32_pll_is_enabled(priv, pll)) {
  1280. return;
  1281. }
  1282. /* Stop all outputs and the PLL */
  1283. _clk_stm32_pll_set_off(priv, pll);
  1284. /* Wait PLL stopped */
  1285. _clk_stm32_pll_wait_ready_off(priv, pll);
  1286. }
  1287. static int _clk_stm32_pll_init(struct stm32_clk_priv *priv, int pll_idx,
  1288. struct stm32_pll_dt_cfg *pll_conf)
  1289. {
  1290. const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_idx);
  1291. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1292. int ret = 0;
  1293. /* Configure PLLs source */
  1294. ret = stm32_clk_configure_mux(priv, pll_conf->vco.src);
  1295. if (ret != 0) {
  1296. return ret;
  1297. }
  1298. #if STM32MP_USB_PROGRAMMER
  1299. if ((pll_idx == _PLL4) && pll4_bootrom) {
  1300. clk_stm32_pll_config_out(priv, pll, &pll_conf->output);
  1301. mmio_setbits_32(pll_base,
  1302. RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
  1303. return 0;
  1304. }
  1305. #endif
  1306. /* Stop the PLL before */
  1307. _clk_stm32_pll_disable(priv, pll);
  1308. clk_stm32_pll_config_vco(priv, pll, &pll_conf->vco);
  1309. clk_stm32_pll_config_out(priv, pll, &pll_conf->output);
  1310. clk_stm32_pll_config_csg(priv, pll, &pll_conf->vco);
  1311. ret = _clk_stm32_pll_enable(priv, pll);
  1312. if (ret != 0) {
  1313. return ret;
  1314. }
  1315. mmio_setbits_32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
  1316. return 0;
  1317. }
  1318. static int clk_stm32_pll_init(struct stm32_clk_priv *priv, int pll_idx)
  1319. {
  1320. struct stm32_pll_dt_cfg *pll_conf = clk_stm32_pll_get_pdata(pll_idx);
  1321. if (pll_conf->vco.status != 0U) {
  1322. return _clk_stm32_pll_init(priv, pll_idx, pll_conf);
  1323. }
  1324. return 0;
  1325. }
  1326. static int stm32_clk_pll_configure(struct stm32_clk_priv *priv)
  1327. {
  1328. int err = 0;
  1329. err = clk_stm32_pll_init(priv, _PLL1);
  1330. if (err != 0) {
  1331. return err;
  1332. }
  1333. err = clk_stm32_pll_init(priv, _PLL2);
  1334. if (err != 0) {
  1335. return err;
  1336. }
  1337. err = clk_stm32_pll_init(priv, _PLL3);
  1338. if (err != 0) {
  1339. return err;
  1340. }
  1341. err = clk_stm32_pll_init(priv, _PLL4);
  1342. if (err != 0) {
  1343. return err;
  1344. }
  1345. return 0;
  1346. }
  1347. static int stm32_clk_oscillators_wait_lse_ready(struct stm32_clk_priv *priv)
  1348. {
  1349. int ret = 0;
  1350. if (_clk_stm32_get_rate(priv, _CK_LSE) != 0U) {
  1351. ret = clk_oscillator_wait_ready_on(priv, _CK_LSE);
  1352. }
  1353. return ret;
  1354. }
  1355. static void stm32_clk_oscillators_enable(struct stm32_clk_priv *priv)
  1356. {
  1357. stm32_enable_oscillator_hse(priv);
  1358. stm32_enable_oscillator_lse(priv);
  1359. _clk_stm32_enable(priv, _CK_LSI);
  1360. _clk_stm32_enable(priv, _CK_CSI);
  1361. }
  1362. static int stm32_clk_hsidiv_configure(struct stm32_clk_priv *priv)
  1363. {
  1364. return stm32mp1_hsidiv(_clk_stm32_get_rate(priv, _CK_HSI));
  1365. }
  1366. #if STM32MP_USB_PROGRAMMER
  1367. static bool stm32mp1_clk_is_pll4_used_by_bootrom(struct stm32_clk_priv *priv, int usbphy_p)
  1368. {
  1369. /* Don't initialize PLL4, when used by BOOTROM */
  1370. if ((stm32mp_get_boot_itf_selected() ==
  1371. BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) &&
  1372. (usbphy_p == _PLL4R)) {
  1373. return true;
  1374. }
  1375. return false;
  1376. }
  1377. static int stm32mp1_clk_check_usb_conflict(struct stm32_clk_priv *priv, int usbphy_p, int usbo_p)
  1378. {
  1379. int _usbo_p;
  1380. int _usbphy_p;
  1381. if (!pll4_bootrom) {
  1382. return 0;
  1383. }
  1384. _usbo_p = _clk_stm32_get_parent(priv, _USBO_K);
  1385. _usbphy_p = _clk_stm32_get_parent(priv, _USBPHY_K);
  1386. if ((_usbo_p != usbo_p) || (_usbphy_p != usbphy_p)) {
  1387. return -FDT_ERR_BADVALUE;
  1388. }
  1389. return 0;
  1390. }
  1391. #endif
  1392. static struct clk_oscillator_data stm32mp13_osc_data[NB_OSCILLATOR] = {
  1393. OSCILLATOR(OSC_HSI, _CK_HSI, "clk-hsi", GATE_HSI, GATE_HSI_RDY,
  1394. NULL, NULL, NULL),
  1395. OSCILLATOR(OSC_LSI, _CK_LSI, "clk-lsi", GATE_LSI, GATE_LSI_RDY,
  1396. NULL, NULL, NULL),
  1397. OSCILLATOR(OSC_CSI, _CK_CSI, "clk-csi", GATE_CSI, GATE_CSI_RDY,
  1398. NULL, NULL, NULL),
  1399. OSCILLATOR(OSC_LSE, _CK_LSE, "clk-lse", GATE_LSE, GATE_LSE_RDY,
  1400. BYPASS(RCC_BDCR, 1, 3),
  1401. CSS(RCC_BDCR, 8),
  1402. DRIVE(RCC_BDCR, 4, 2, 2)),
  1403. OSCILLATOR(OSC_HSE, _CK_HSE, "clk-hse", GATE_HSE, GATE_HSE_RDY,
  1404. BYPASS(RCC_OCENSETR, 10, 7),
  1405. CSS(RCC_OCENSETR, 11),
  1406. NULL),
  1407. OSCILLATOR(OSC_I2SCKIN, _I2SCKIN, "i2s_ckin", NO_GATE, NO_GATE,
  1408. NULL, NULL, NULL),
  1409. };
  1410. static const char *clk_stm32_get_oscillator_name(enum stm32_osc id)
  1411. {
  1412. if (id < NB_OSCILLATOR) {
  1413. return stm32mp13_osc_data[id].name;
  1414. }
  1415. return NULL;
  1416. }
  1417. #define CLK_PLL_CFG(_idx, _clk_id, _type, _reg)\
  1418. [(_idx)] = {\
  1419. .clk_id = (_clk_id),\
  1420. .plltype = (_type),\
  1421. .reg_pllxcr = (_reg),\
  1422. }
  1423. static const struct stm32_clk_pll stm32_mp13_clk_pll[_PLL_NB] = {
  1424. CLK_PLL_CFG(_PLL1, _CK_PLL1, PLL_2000, RCC_PLL1CR),
  1425. CLK_PLL_CFG(_PLL2, _CK_PLL2, PLL_1600, RCC_PLL2CR),
  1426. CLK_PLL_CFG(_PLL3, _CK_PLL3, PLL_800, RCC_PLL3CR),
  1427. CLK_PLL_CFG(_PLL4, _CK_PLL4, PLL_800, RCC_PLL4CR),
  1428. };
  1429. static const struct stm32_clk_pll *clk_st32_pll_data(unsigned int idx)
  1430. {
  1431. return &stm32_mp13_clk_pll[idx];
  1432. }
  1433. struct stm32_pll_cfg {
  1434. uint8_t pll_id;
  1435. };
  1436. static unsigned long clk_stm32_pll_recalc_rate(struct stm32_clk_priv *priv, int id,
  1437. unsigned long prate)
  1438. {
  1439. const struct clk_stm32 *clk = _clk_get(priv, id);
  1440. struct stm32_pll_cfg *pll_cfg = clk->clock_cfg;
  1441. const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id);
  1442. uintptr_t pll_base = priv->base + pll->reg_pllxcr;
  1443. uint32_t cfgr1, fracr, divm, divn;
  1444. unsigned long fvco;
  1445. cfgr1 = mmio_read_32(pll_base + RCC_OFFSET_PLLXCFGR1);
  1446. fracr = mmio_read_32(pll_base + RCC_OFFSET_PLLXFRACR);
  1447. divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
  1448. divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
  1449. /*
  1450. * With FRACV :
  1451. * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
  1452. * Without FRACV
  1453. * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
  1454. */
  1455. if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
  1456. uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
  1457. RCC_PLLNFRACR_FRACV_SHIFT;
  1458. unsigned long long numerator, denominator;
  1459. numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
  1460. numerator = prate * numerator;
  1461. denominator = ((unsigned long long)divm + 1U) << 13;
  1462. fvco = (unsigned long)(numerator / denominator);
  1463. } else {
  1464. fvco = (unsigned long)(prate * (divn + 1U) / (divm + 1U));
  1465. }
  1466. return fvco;
  1467. };
  1468. static bool clk_stm32_pll_is_enabled(struct stm32_clk_priv *priv, int id)
  1469. {
  1470. const struct clk_stm32 *clk = _clk_get(priv, id);
  1471. struct stm32_pll_cfg *pll_cfg = clk->clock_cfg;
  1472. const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id);
  1473. return _clk_stm32_pll_is_enabled(priv, pll);
  1474. }
  1475. static int clk_stm32_pll_enable(struct stm32_clk_priv *priv, int id)
  1476. {
  1477. const struct clk_stm32 *clk = _clk_get(priv, id);
  1478. struct stm32_pll_cfg *pll_cfg = clk->clock_cfg;
  1479. const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id);
  1480. return _clk_stm32_pll_enable(priv, pll);
  1481. }
  1482. static void clk_stm32_pll_disable(struct stm32_clk_priv *priv, int id)
  1483. {
  1484. const struct clk_stm32 *clk = _clk_get(priv, id);
  1485. struct stm32_pll_cfg *pll_cfg = clk->clock_cfg;
  1486. const struct stm32_clk_pll *pll = clk_st32_pll_data(pll_cfg->pll_id);
  1487. _clk_stm32_pll_disable(priv, pll);
  1488. }
  1489. static const struct stm32_clk_ops clk_stm32_pll_ops = {
  1490. .recalc_rate = clk_stm32_pll_recalc_rate,
  1491. .enable = clk_stm32_pll_enable,
  1492. .disable = clk_stm32_pll_disable,
  1493. .is_enabled = clk_stm32_pll_is_enabled,
  1494. };
  1495. #define CLK_PLL(idx, _idx, _parent, _gate, _pll_id, _flags)[idx] = {\
  1496. .binding = _idx,\
  1497. .parent = _parent,\
  1498. .flags = (_flags),\
  1499. .clock_cfg = &(struct stm32_pll_cfg) {\
  1500. .pll_id = _pll_id,\
  1501. },\
  1502. .ops = STM32_PLL_OPS,\
  1503. }
  1504. struct clk_stm32_composite_cfg {
  1505. uint8_t gate_id;
  1506. uint8_t div_id;
  1507. };
  1508. static unsigned long clk_stm32_composite_recalc_rate(struct stm32_clk_priv *priv,
  1509. int idx, unsigned long prate)
  1510. {
  1511. const struct clk_stm32 *clk = _clk_get(priv, idx);
  1512. struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg;
  1513. return _clk_stm32_divider_recalc(priv, composite_cfg->div_id, prate);
  1514. };
  1515. static bool clk_stm32_composite_gate_is_enabled(struct stm32_clk_priv *priv, int idx)
  1516. {
  1517. const struct clk_stm32 *clk = _clk_get(priv, idx);
  1518. struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg;
  1519. return _clk_stm32_gate_is_enabled(priv, composite_cfg->gate_id);
  1520. }
  1521. static int clk_stm32_composite_gate_enable(struct stm32_clk_priv *priv, int idx)
  1522. {
  1523. const struct clk_stm32 *clk = _clk_get(priv, idx);
  1524. struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg;
  1525. return _clk_stm32_gate_enable(priv, composite_cfg->gate_id);
  1526. }
  1527. static void clk_stm32_composite_gate_disable(struct stm32_clk_priv *priv, int idx)
  1528. {
  1529. const struct clk_stm32 *clk = _clk_get(priv, idx);
  1530. struct clk_stm32_composite_cfg *composite_cfg = clk->clock_cfg;
  1531. _clk_stm32_gate_disable(priv, composite_cfg->gate_id);
  1532. }
  1533. static const struct stm32_clk_ops clk_stm32_composite_ops = {
  1534. .recalc_rate = clk_stm32_composite_recalc_rate,
  1535. .is_enabled = clk_stm32_composite_gate_is_enabled,
  1536. .enable = clk_stm32_composite_gate_enable,
  1537. .disable = clk_stm32_composite_gate_disable,
  1538. };
  1539. #define STM32_COMPOSITE(idx, _binding, _parent, _flags, _gate_id,\
  1540. _div_id)[idx] = {\
  1541. .binding = (_binding),\
  1542. .parent = (_parent),\
  1543. .flags = (_flags),\
  1544. .clock_cfg = &(struct clk_stm32_composite_cfg) {\
  1545. .gate_id = (_gate_id),\
  1546. .div_id = (_div_id),\
  1547. },\
  1548. .ops = STM32_COMPOSITE_OPS,\
  1549. }
  1550. enum {
  1551. STM32_PLL_OPS = STM32_LAST_OPS,
  1552. STM32_COMPOSITE_OPS,
  1553. MP13_LAST_OPS
  1554. };
  1555. static const struct stm32_clk_ops *ops_array_mp13[MP13_LAST_OPS] = {
  1556. [NO_OPS] = NULL,
  1557. [FIXED_FACTOR_OPS] = &clk_fixed_factor_ops,
  1558. [GATE_OPS] = &clk_gate_ops,
  1559. [STM32_MUX_OPS] = &clk_mux_ops,
  1560. [STM32_DIVIDER_OPS] = &clk_stm32_divider_ops,
  1561. [STM32_GATE_OPS] = &clk_stm32_gate_ops,
  1562. [STM32_TIMER_OPS] = &clk_timer_ops,
  1563. [STM32_FIXED_RATE_OPS] = &clk_stm32_fixed_rate_ops,
  1564. [STM32_OSC_OPS] = &clk_stm32_osc_ops,
  1565. [STM32_OSC_NOGATE_OPS] = &clk_stm32_osc_nogate_ops,
  1566. [STM32_PLL_OPS] = &clk_stm32_pll_ops,
  1567. [STM32_COMPOSITE_OPS] = &clk_stm32_composite_ops
  1568. };
  1569. static const struct clk_stm32 stm32mp13_clk[CK_LAST] = {
  1570. /* ROOT CLOCKS */
  1571. CLK_FIXED_RATE(_CK_OFF, _NO_ID, 0),
  1572. CLK_OSC(_CK_HSE, CK_HSE, CLK_IS_ROOT, OSC_HSE),
  1573. CLK_OSC(_CK_HSI, CK_HSI, CLK_IS_ROOT, OSC_HSI),
  1574. CLK_OSC(_CK_CSI, CK_CSI, CLK_IS_ROOT, OSC_CSI),
  1575. CLK_OSC(_CK_LSI, CK_LSI, CLK_IS_ROOT, OSC_LSI),
  1576. CLK_OSC(_CK_LSE, CK_LSE, CLK_IS_ROOT, OSC_LSE),
  1577. CLK_OSC_FIXED(_I2SCKIN, _NO_ID, CLK_IS_ROOT, OSC_I2SCKIN),
  1578. CLK_FIXED_RATE(_USB_PHY_48, _NO_ID, USB_PHY_48_MHZ),
  1579. STM32_DIV(_HSE_DIV, _NO_ID, _CK_HSE, 0, DIV_RTC),
  1580. FIXED_FACTOR(_HSE_DIV2, CK_HSE_DIV2, _CK_HSE, 1, 2),
  1581. FIXED_FACTOR(_CSI_DIV122, _NO_ID, _CK_CSI, 1, 122),
  1582. CLK_PLL(_CK_PLL1, PLL1, MUX(MUX_PLL12), GATE_PLL1, _PLL1, 0),
  1583. CLK_PLL(_CK_PLL2, PLL2, MUX(MUX_PLL12), GATE_PLL2, _PLL2, 0),
  1584. CLK_PLL(_CK_PLL3, PLL3, MUX(MUX_PLL3), GATE_PLL3, _PLL3, 0),
  1585. CLK_PLL(_CK_PLL4, PLL4, MUX(MUX_PLL4), GATE_PLL4, _PLL4, 0),
  1586. STM32_COMPOSITE(_PLL1P, PLL1_P, _CK_PLL1, CLK_IS_CRITICAL, GATE_PLL1_DIVP, DIV_PLL1DIVP),
  1587. STM32_DIV(_PLL1P_DIV, _NO_ID, _CK_PLL1, 0, DIV_MPU),
  1588. STM32_COMPOSITE(_PLL2P, PLL2_P, _CK_PLL2, CLK_IS_CRITICAL, GATE_PLL2_DIVP, DIV_PLL2DIVP),
  1589. STM32_COMPOSITE(_PLL2Q, PLL2_Q, _CK_PLL2, 0, GATE_PLL2_DIVQ, DIV_PLL2DIVQ),
  1590. STM32_COMPOSITE(_PLL2R, PLL2_R, _CK_PLL2, CLK_IS_CRITICAL, GATE_PLL2_DIVR, DIV_PLL2DIVR),
  1591. STM32_COMPOSITE(_PLL3P, PLL3_P, _CK_PLL3, 0, GATE_PLL3_DIVP, DIV_PLL3DIVP),
  1592. STM32_COMPOSITE(_PLL3Q, PLL3_Q, _CK_PLL3, 0, GATE_PLL3_DIVQ, DIV_PLL3DIVQ),
  1593. STM32_COMPOSITE(_PLL3R, PLL3_R, _CK_PLL3, 0, GATE_PLL3_DIVR, DIV_PLL3DIVR),
  1594. STM32_COMPOSITE(_PLL4P, PLL4_P, _CK_PLL4, 0, GATE_PLL4_DIVP, DIV_PLL4DIVP),
  1595. STM32_COMPOSITE(_PLL4Q, PLL4_Q, _CK_PLL4, 0, GATE_PLL4_DIVQ, DIV_PLL4DIVQ),
  1596. STM32_COMPOSITE(_PLL4R, PLL4_R, _CK_PLL4, 0, GATE_PLL4_DIVR, DIV_PLL4DIVR),
  1597. STM32_MUX(_CKMPU, CK_MPU, MUX_MPU, 0),
  1598. STM32_DIV(_CKAXI, CK_AXI, MUX(MUX_AXI), 0, DIV_AXI),
  1599. STM32_DIV(_CKMLAHB, CK_MLAHB, MUX(MUX_MLAHB), CLK_IS_CRITICAL, DIV_MLAHB),
  1600. STM32_MUX(_CKPER, CK_PER, MUX(MUX_CKPER), 0),
  1601. STM32_DIV(_PCLK1, PCLK1, _CKMLAHB, 0, DIV_APB1),
  1602. STM32_DIV(_PCLK2, PCLK2, _CKMLAHB, 0, DIV_APB2),
  1603. STM32_DIV(_PCLK3, PCLK3, _CKMLAHB, 0, DIV_APB3),
  1604. STM32_DIV(_PCLK4, PCLK4, _CKAXI, 0, DIV_APB4),
  1605. STM32_DIV(_PCLK5, PCLK5, _CKAXI, 0, DIV_APB5),
  1606. STM32_DIV(_PCLK6, PCLK6, _CKMLAHB, 0, DIV_APB6),
  1607. CK_TIMER(_CKTIMG1, CK_TIMG1, _PCLK1, 0, RCC_APB1DIVR, RCC_TIMG1PRER),
  1608. CK_TIMER(_CKTIMG2, CK_TIMG2, _PCLK2, 0, RCC_APB2DIVR, RCC_TIMG2PRER),
  1609. CK_TIMER(_CKTIMG3, CK_TIMG3, _PCLK6, 0, RCC_APB6DIVR, RCC_TIMG3PRER),
  1610. /* END ROOT CLOCKS */
  1611. STM32_GATE(_DDRC1, DDRC1, _CKAXI, CLK_IS_CRITICAL, GATE_DDRC1),
  1612. STM32_GATE(_DDRC1LP, DDRC1LP, _CKAXI, CLK_IS_CRITICAL, GATE_DDRC1LP),
  1613. STM32_GATE(_DDRPHYC, DDRPHYC, _PLL2R, CLK_IS_CRITICAL, GATE_DDRPHYC),
  1614. STM32_GATE(_DDRPHYCLP, DDRPHYCLP, _PLL2R, CLK_IS_CRITICAL, GATE_DDRPHYCLP),
  1615. STM32_GATE(_DDRCAPB, DDRCAPB, _PCLK4, CLK_IS_CRITICAL, GATE_DDRCAPB),
  1616. STM32_GATE(_DDRCAPBLP, DDRCAPBLP, _PCLK4, CLK_IS_CRITICAL, GATE_DDRCAPBLP),
  1617. STM32_GATE(_AXIDCG, AXIDCG, _CKAXI, CLK_IS_CRITICAL, GATE_AXIDCG),
  1618. STM32_GATE(_DDRPHYCAPB, DDRPHYCAPB, _PCLK4, CLK_IS_CRITICAL, GATE_DDRPHYCAPB),
  1619. STM32_GATE(_DDRPHYCAPBLP, DDRPHYCAPBLP, _PCLK4, CLK_IS_CRITICAL, GATE_DDRPHYCAPBLP),
  1620. STM32_GATE(_SYSCFG, SYSCFG, _PCLK3, 0, GATE_SYSCFG),
  1621. STM32_GATE(_DDRPERFM, DDRPERFM, _PCLK4, 0, GATE_DDRPERFM),
  1622. STM32_GATE(_IWDG2APB, IWDG2, _PCLK4, 0, GATE_IWDG2APB),
  1623. STM32_GATE(_USBPHY_K, USBPHY_K, MUX(MUX_USBPHY), 0, GATE_USBPHY),
  1624. STM32_GATE(_USBO_K, USBO_K, MUX(MUX_USBO), 0, GATE_USBO),
  1625. STM32_GATE(_RTCAPB, RTCAPB, _PCLK5, CLK_IS_CRITICAL, GATE_RTCAPB),
  1626. STM32_GATE(_TZC, TZC, _PCLK5, CLK_IS_CRITICAL, GATE_TZC),
  1627. STM32_GATE(_ETZPC, TZPC, _PCLK5, CLK_IS_CRITICAL, GATE_ETZPC),
  1628. STM32_GATE(_IWDG1APB, IWDG1, _PCLK5, 0, GATE_IWDG1APB),
  1629. STM32_GATE(_BSEC, BSEC, _PCLK5, CLK_IS_CRITICAL, GATE_BSEC),
  1630. STM32_GATE(_STGENC, STGEN_K, MUX(MUX_STGEN), CLK_IS_CRITICAL, GATE_STGENC),
  1631. STM32_GATE(_USART1_K, USART1_K, MUX(MUX_UART1), 0, GATE_USART1),
  1632. STM32_GATE(_USART2_K, USART2_K, MUX(MUX_UART2), 0, GATE_USART2),
  1633. STM32_GATE(_I2C3_K, I2C3_K, MUX(MUX_I2C3), 0, GATE_I2C3),
  1634. STM32_GATE(_I2C4_K, I2C4_K, MUX(MUX_I2C4), 0, GATE_I2C4),
  1635. STM32_GATE(_I2C5_K, I2C5_K, MUX(MUX_I2C5), 0, GATE_I2C5),
  1636. STM32_GATE(_TIM12, TIM12_K, _CKTIMG3, 0, GATE_TIM12),
  1637. STM32_GATE(_TIM15, TIM15_K, _CKTIMG3, 0, GATE_TIM15),
  1638. STM32_GATE(_RTCCK, RTC, MUX(MUX_RTC), 0, GATE_RTCCK),
  1639. STM32_GATE(_GPIOA, GPIOA, _CKMLAHB, 0, GATE_GPIOA),
  1640. STM32_GATE(_GPIOB, GPIOB, _CKMLAHB, 0, GATE_GPIOB),
  1641. STM32_GATE(_GPIOC, GPIOC, _CKMLAHB, 0, GATE_GPIOC),
  1642. STM32_GATE(_GPIOD, GPIOD, _CKMLAHB, 0, GATE_GPIOD),
  1643. STM32_GATE(_GPIOE, GPIOE, _CKMLAHB, 0, GATE_GPIOE),
  1644. STM32_GATE(_GPIOF, GPIOF, _CKMLAHB, 0, GATE_GPIOF),
  1645. STM32_GATE(_GPIOG, GPIOG, _CKMLAHB, 0, GATE_GPIOG),
  1646. STM32_GATE(_GPIOH, GPIOH, _CKMLAHB, 0, GATE_GPIOH),
  1647. STM32_GATE(_GPIOI, GPIOI, _CKMLAHB, 0, GATE_GPIOI),
  1648. STM32_GATE(_PKA, PKA, _CKAXI, 0, GATE_PKA),
  1649. STM32_GATE(_SAES_K, SAES_K, MUX(MUX_SAES), 0, GATE_SAES),
  1650. STM32_GATE(_CRYP1, CRYP1, _PCLK5, 0, GATE_CRYP1),
  1651. STM32_GATE(_HASH1, HASH1, _PCLK5, 0, GATE_HASH1),
  1652. STM32_GATE(_RNG1_K, RNG1_K, MUX(MUX_RNG1), 0, GATE_RNG1),
  1653. STM32_GATE(_BKPSRAM, BKPSRAM, _PCLK5, CLK_IS_CRITICAL, GATE_BKPSRAM),
  1654. STM32_GATE(_SDMMC1_K, SDMMC1_K, MUX(MUX_SDMMC1), 0, GATE_SDMMC1),
  1655. STM32_GATE(_SDMMC2_K, SDMMC2_K, MUX(MUX_SDMMC2), 0, GATE_SDMMC2),
  1656. STM32_GATE(_DBGCK, CK_DBG, _CKAXI, 0, GATE_DBGCK),
  1657. STM32_GATE(_USART3_K, USART3_K, MUX(MUX_UART35), 0, GATE_USART3),
  1658. STM32_GATE(_UART4_K, UART4_K, MUX(MUX_UART4), 0, GATE_UART4),
  1659. STM32_GATE(_UART5_K, UART5_K, MUX(MUX_UART35), 0, GATE_UART5),
  1660. STM32_GATE(_UART7_K, UART7_K, MUX(MUX_UART78), 0, GATE_UART7),
  1661. STM32_GATE(_UART8_K, UART8_K, MUX(MUX_UART78), 0, GATE_UART8),
  1662. STM32_GATE(_USART6_K, USART6_K, MUX(MUX_UART6), 0, GATE_USART6),
  1663. STM32_GATE(_MCE, MCE, _CKAXI, CLK_IS_CRITICAL, GATE_MCE),
  1664. STM32_GATE(_FMC_K, FMC_K, MUX(MUX_FMC), 0, GATE_FMC),
  1665. STM32_GATE(_QSPI_K, QSPI_K, MUX(MUX_QSPI), 0, GATE_QSPI),
  1666. STM32_COMPOSITE(_MCO1_K, CK_MCO1, MUX(MUX_MCO1), 0, GATE_MCO1, DIV_MCO1),
  1667. STM32_COMPOSITE(_MCO2_K, CK_MCO2, MUX(MUX_MCO2), 0, GATE_MCO2, DIV_MCO2),
  1668. STM32_COMPOSITE(_TRACECK, CK_TRACE, _CKAXI, 0, GATE_TRACECK, DIV_TRACE),
  1669. };
  1670. static struct stm32_pll_dt_cfg mp13_pll[_PLL_NB];
  1671. static struct stm32_osci_dt_cfg mp13_osci[NB_OSCILLATOR];
  1672. static uint32_t mp13_clksrc[MUX_MAX];
  1673. static uint32_t mp13_clkdiv[DIV_MAX];
  1674. static struct stm32_clk_platdata stm32mp13_clock_pdata = {
  1675. .osci = mp13_osci,
  1676. .nosci = NB_OSCILLATOR,
  1677. .pll = mp13_pll,
  1678. .npll = _PLL_NB,
  1679. .clksrc = mp13_clksrc,
  1680. .nclksrc = MUX_MAX,
  1681. .clkdiv = mp13_clkdiv,
  1682. .nclkdiv = DIV_MAX,
  1683. };
  1684. static struct stm32_clk_priv stm32mp13_clock_data = {
  1685. .base = RCC_BASE,
  1686. .num = ARRAY_SIZE(stm32mp13_clk),
  1687. .clks = stm32mp13_clk,
  1688. .parents = parent_mp13,
  1689. .nb_parents = ARRAY_SIZE(parent_mp13),
  1690. .gates = gates_mp13,
  1691. .nb_gates = ARRAY_SIZE(gates_mp13),
  1692. .div = dividers_mp13,
  1693. .nb_div = ARRAY_SIZE(dividers_mp13),
  1694. .osci_data = stm32mp13_osc_data,
  1695. .nb_osci_data = ARRAY_SIZE(stm32mp13_osc_data),
  1696. .gate_refcounts = refcounts_mp13,
  1697. .pdata = &stm32mp13_clock_pdata,
  1698. .ops_array = ops_array_mp13,
  1699. };
  1700. static int stm32mp1_init_clock_tree(void)
  1701. {
  1702. struct stm32_clk_priv *priv = clk_stm32_get_priv();
  1703. int ret;
  1704. #if STM32MP_USB_PROGRAMMER
  1705. int usbphy_p = _clk_stm32_get_parent(priv, _USBPHY_K);
  1706. int usbo_p = _clk_stm32_get_parent(priv, _USBO_K);
  1707. /* Don't initialize PLL4, when used by BOOTROM */
  1708. pll4_bootrom = stm32mp1_clk_is_pll4_used_by_bootrom(priv, usbphy_p);
  1709. #endif
  1710. /*
  1711. * Switch ON oscillators found in device-tree.
  1712. * Note: HSI already ON after BootROM stage.
  1713. */
  1714. stm32_clk_oscillators_enable(priv);
  1715. /* Come back to HSI */
  1716. ret = stm32mp1_come_back_to_hsi();
  1717. if (ret != 0) {
  1718. return ret;
  1719. }
  1720. ret = stm32_clk_hsidiv_configure(priv);
  1721. if (ret != 0) {
  1722. return ret;
  1723. }
  1724. ret = stm32_clk_stgen_configure(priv, _STGENC);
  1725. if (ret != 0) {
  1726. panic();
  1727. }
  1728. ret = stm32_clk_dividers_configure(priv);
  1729. if (ret != 0) {
  1730. panic();
  1731. }
  1732. ret = stm32_clk_pll_configure(priv);
  1733. if (ret != 0) {
  1734. panic();
  1735. }
  1736. /* Wait LSE ready before to use it */
  1737. ret = stm32_clk_oscillators_wait_lse_ready(priv);
  1738. if (ret != 0) {
  1739. panic();
  1740. }
  1741. /* Configure with expected clock source */
  1742. ret = stm32_clk_source_configure(priv);
  1743. if (ret != 0) {
  1744. panic();
  1745. }
  1746. /* Configure LSE css after RTC source configuration */
  1747. ret = stm32_clk_oscillators_lse_set_css(priv);
  1748. if (ret != 0) {
  1749. panic();
  1750. }
  1751. #if STM32MP_USB_PROGRAMMER
  1752. ret = stm32mp1_clk_check_usb_conflict(priv, usbphy_p, usbo_p);
  1753. if (ret != 0) {
  1754. return ret;
  1755. }
  1756. #endif
  1757. /* reconfigure STGEN with DT config */
  1758. ret = stm32_clk_stgen_configure(priv, _STGENC);
  1759. if (ret != 0) {
  1760. panic();
  1761. }
  1762. /* Software Self-Refresh mode (SSR) during DDR initilialization */
  1763. mmio_clrsetbits_32(priv->base + RCC_DDRITFCR,
  1764. RCC_DDRITFCR_DDRCKMOD_MASK,
  1765. RCC_DDRITFCR_DDRCKMOD_SSR <<
  1766. RCC_DDRITFCR_DDRCKMOD_SHIFT);
  1767. return 0;
  1768. }
  1769. static int clk_stm32_parse_oscillator_fdt(void *fdt, int node, const char *name,
  1770. struct stm32_osci_dt_cfg *osci)
  1771. {
  1772. int subnode = 0;
  1773. /* default value oscillator not found, freq=0 */
  1774. osci->freq = 0;
  1775. fdt_for_each_subnode(subnode, fdt, node) {
  1776. const char *cchar = NULL;
  1777. const fdt32_t *cuint = NULL;
  1778. int ret = 0;
  1779. cchar = fdt_get_name(fdt, subnode, &ret);
  1780. if (cchar == NULL) {
  1781. return ret;
  1782. }
  1783. if (strncmp(cchar, name, (size_t)ret) ||
  1784. fdt_get_status(subnode) == DT_DISABLED) {
  1785. continue;
  1786. }
  1787. cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret);
  1788. if (cuint == NULL) {
  1789. return ret;
  1790. }
  1791. osci->freq = fdt32_to_cpu(*cuint);
  1792. if (fdt_getprop(fdt, subnode, "st,bypass", NULL) != NULL) {
  1793. osci->bypass = true;
  1794. }
  1795. if (fdt_getprop(fdt, subnode, "st,digbypass", NULL) != NULL) {
  1796. osci->digbyp = true;
  1797. }
  1798. if (fdt_getprop(fdt, subnode, "st,css", NULL) != NULL) {
  1799. osci->css = true;
  1800. }
  1801. osci->drive = fdt_read_uint32_default(fdt, subnode, "st,drive", LSEDRV_MEDIUM_HIGH);
  1802. return 0;
  1803. }
  1804. return 0;
  1805. }
  1806. static int stm32_clk_parse_fdt_all_oscillator(void *fdt, struct stm32_clk_platdata *pdata)
  1807. {
  1808. int fdt_err = 0;
  1809. uint32_t i = 0;
  1810. int node = 0;
  1811. node = fdt_path_offset(fdt, "/clocks");
  1812. if (node < 0) {
  1813. return -FDT_ERR_NOTFOUND;
  1814. }
  1815. for (i = 0; i < pdata->nosci; i++) {
  1816. const char *name = NULL;
  1817. name = clk_stm32_get_oscillator_name((enum stm32_osc)i);
  1818. if (name == NULL) {
  1819. continue;
  1820. }
  1821. fdt_err = clk_stm32_parse_oscillator_fdt(fdt, node, name, &pdata->osci[i]);
  1822. if (fdt_err < 0) {
  1823. panic();
  1824. }
  1825. }
  1826. return 0;
  1827. }
  1828. #define RCC_PLL_NAME_SIZE 12
  1829. static int clk_stm32_load_vco_config(void *fdt, int subnode, struct stm32_pll_vco *vco)
  1830. {
  1831. int err = 0;
  1832. err = fdt_read_uint32_array(fdt, subnode, "divmn", (int)PLL_DIV_MN_NB, vco->div_mn);
  1833. if (err != 0) {
  1834. return err;
  1835. }
  1836. err = fdt_read_uint32_array(fdt, subnode, "csg", (int)PLL_CSG_NB, vco->csg);
  1837. vco->csg_enabled = (err == 0);
  1838. if (err == -FDT_ERR_NOTFOUND) {
  1839. err = 0;
  1840. }
  1841. if (err != 0) {
  1842. return err;
  1843. }
  1844. vco->status = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN | RCC_PLLNCR_PLLON;
  1845. vco->frac = fdt_read_uint32_default(fdt, subnode, "frac", 0);
  1846. vco->src = fdt_read_uint32_default(fdt, subnode, "src", UINT32_MAX);
  1847. return 0;
  1848. }
  1849. static int clk_stm32_load_output_config(void *fdt, int subnode, struct stm32_pll_output *output)
  1850. {
  1851. int err = 0;
  1852. err = fdt_read_uint32_array(fdt, subnode, "st,pll_div_pqr", (int)PLL_DIV_PQR_NB,
  1853. output->output);
  1854. if (err != 0) {
  1855. return err;
  1856. }
  1857. return 0;
  1858. }
  1859. static int clk_stm32_parse_pll_fdt(void *fdt, int subnode, struct stm32_pll_dt_cfg *pll)
  1860. {
  1861. const fdt32_t *cuint = NULL;
  1862. int subnode_pll = 0;
  1863. int subnode_vco = 0;
  1864. int err = 0;
  1865. cuint = fdt_getprop(fdt, subnode, "st,pll", NULL);
  1866. if (!cuint) {
  1867. return -FDT_ERR_NOTFOUND;
  1868. }
  1869. subnode_pll = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
  1870. if (subnode_pll < 0) {
  1871. return -FDT_ERR_NOTFOUND;
  1872. }
  1873. cuint = fdt_getprop(fdt, subnode_pll, "st,pll_vco", NULL);
  1874. if (!cuint) {
  1875. return -FDT_ERR_NOTFOUND;
  1876. }
  1877. subnode_vco = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
  1878. if (subnode_vco < 0) {
  1879. return -FDT_ERR_NOTFOUND;
  1880. }
  1881. err = clk_stm32_load_vco_config(fdt, subnode_vco, &pll->vco);
  1882. if (err != 0) {
  1883. return err;
  1884. }
  1885. err = clk_stm32_load_output_config(fdt, subnode_pll, &pll->output);
  1886. if (err != 0) {
  1887. return err;
  1888. }
  1889. return 0;
  1890. }
  1891. static int stm32_clk_parse_fdt_all_pll(void *fdt, int node, struct stm32_clk_platdata *pdata)
  1892. {
  1893. size_t i = 0U;
  1894. /* PLL1 is not configurable with device tree */
  1895. for (i = _PLL2; i < pdata->npll; i++) {
  1896. struct stm32_pll_dt_cfg *pll = &pdata->pll[i];
  1897. char name[RCC_PLL_NAME_SIZE];
  1898. int subnode = 0;
  1899. int err = 0;
  1900. snprintf(name, sizeof(name), "st,pll@%u", i);
  1901. subnode = fdt_subnode_offset(fdt, node, name);
  1902. if (!fdt_check_node(subnode)) {
  1903. continue;
  1904. }
  1905. err = clk_stm32_parse_pll_fdt(fdt, subnode, pll);
  1906. if (err != 0) {
  1907. panic();
  1908. }
  1909. }
  1910. return 0;
  1911. }
  1912. static int stm32_clk_parse_fdt(struct stm32_clk_platdata *pdata)
  1913. {
  1914. void *fdt = NULL;
  1915. int node;
  1916. uint32_t err;
  1917. if (fdt_get_address(&fdt) == 0) {
  1918. return -ENOENT;
  1919. }
  1920. node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_CLK_COMPAT);
  1921. if (node < 0) {
  1922. panic();
  1923. }
  1924. err = stm32_clk_parse_fdt_all_oscillator(fdt, pdata);
  1925. if (err != 0) {
  1926. return err;
  1927. }
  1928. err = stm32_clk_parse_fdt_all_pll(fdt, node, pdata);
  1929. if (err != 0) {
  1930. return err;
  1931. }
  1932. err = stm32_clk_parse_fdt_by_name(fdt, node, "st,clkdiv", pdata->clkdiv, &pdata->nclkdiv);
  1933. if (err != 0) {
  1934. return err;
  1935. }
  1936. err = stm32_clk_parse_fdt_by_name(fdt, node, "st,clksrc", pdata->clksrc, &pdata->nclksrc);
  1937. if (err != 0) {
  1938. return err;
  1939. }
  1940. return 0;
  1941. }
  1942. void stm32mp1_clk_rcc_regs_lock(void)
  1943. {
  1944. clk_stm32_rcc_regs_lock();
  1945. }
  1946. void stm32mp1_clk_rcc_regs_unlock(void)
  1947. {
  1948. clk_stm32_rcc_regs_unlock();
  1949. }
  1950. int stm32mp1_clk_init(void)
  1951. {
  1952. int ret;
  1953. /* compute the PLL1 settings, not read in device tree */
  1954. ret = clk_compute_pll1_settings(PLL1_NOMINAL_FREQ_IN_KHZ);
  1955. if (ret != 0) {
  1956. return ret;
  1957. }
  1958. ret = stm32mp1_init_clock_tree();
  1959. if (ret != 0) {
  1960. return ret;
  1961. }
  1962. clk_stm32_enable_critical_clocks();
  1963. return 0;
  1964. }
  1965. int stm32mp1_clk_probe(void)
  1966. {
  1967. uintptr_t base = RCC_BASE;
  1968. int ret;
  1969. ret = stm32_clk_parse_fdt(&stm32mp13_clock_pdata);
  1970. if (ret != 0) {
  1971. return ret;
  1972. }
  1973. ret = clk_stm32_init(&stm32mp13_clock_data, base);
  1974. return ret;
  1975. }