clk-stm32f4.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Author: Daniel Thompson <daniel.thompson@linaro.org>
  4. *
  5. * Inspired by clk-asm9260.c .
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/ioport.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/regmap.h>
  17. #include <linux/mfd/syscon.h>
  18. /*
  19. * Include list of clocks wich are not derived from system clock (SYSCLOCK)
  20. * The index of these clocks is the secondary index of DT bindings
  21. *
  22. */
  23. #include <dt-bindings/clock/stm32fx-clock.h>
  24. #define STM32F4_RCC_CR 0x00
  25. #define STM32F4_RCC_PLLCFGR 0x04
  26. #define STM32F4_RCC_CFGR 0x08
  27. #define STM32F4_RCC_AHB1ENR 0x30
  28. #define STM32F4_RCC_AHB2ENR 0x34
  29. #define STM32F4_RCC_AHB3ENR 0x38
  30. #define STM32F4_RCC_APB1ENR 0x40
  31. #define STM32F4_RCC_APB2ENR 0x44
  32. #define STM32F4_RCC_BDCR 0x70
  33. #define STM32F4_RCC_CSR 0x74
  34. #define STM32F4_RCC_PLLI2SCFGR 0x84
  35. #define STM32F4_RCC_PLLSAICFGR 0x88
  36. #define STM32F4_RCC_DCKCFGR 0x8c
  37. #define STM32F7_RCC_DCKCFGR2 0x90
  38. #define NONE -1
  39. #define NO_IDX NONE
  40. #define NO_MUX NONE
  41. #define NO_GATE NONE
  42. struct stm32f4_gate_data {
  43. u8 offset;
  44. u8 bit_idx;
  45. const char *name;
  46. const char *parent_name;
  47. unsigned long flags;
  48. };
  49. static const struct stm32f4_gate_data stm32f429_gates[] __initconst = {
  50. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  51. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  52. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  53. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  54. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  55. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  56. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  57. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  58. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  59. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  60. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  61. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  62. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  63. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  64. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  65. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  66. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  67. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  68. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  69. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  70. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  71. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  72. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  73. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  74. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  75. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  76. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  77. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  78. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  79. CLK_IGNORE_UNUSED },
  80. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  81. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  82. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  83. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  84. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  85. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  86. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  87. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  88. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  89. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  90. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  91. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  92. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  93. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  94. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  95. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  96. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  97. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  98. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  99. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  100. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  101. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  102. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  103. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  104. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  105. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  106. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  107. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  108. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  109. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  110. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  111. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  112. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  113. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  114. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  115. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  116. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  117. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  118. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  119. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  120. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  121. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  122. };
  123. static const struct stm32f4_gate_data stm32f469_gates[] __initconst = {
  124. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  125. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  126. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  127. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  128. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  129. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  130. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  131. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  132. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  133. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  134. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  135. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  136. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  137. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  138. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  139. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  140. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  141. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  142. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  143. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  144. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  145. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  146. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  147. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  148. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  149. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  150. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  151. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  152. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  153. CLK_IGNORE_UNUSED },
  154. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  155. CLK_IGNORE_UNUSED },
  156. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  157. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  158. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  159. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  160. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  161. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  162. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  163. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  164. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  165. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  166. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  167. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  168. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  169. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  170. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  171. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  172. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  173. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  174. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  175. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  176. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  177. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  178. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  179. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  180. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  181. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  182. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  183. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  184. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  185. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  186. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  187. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  188. { STM32F4_RCC_APB2ENR, 11, "sdio", "sdmux" },
  189. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  190. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  191. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  192. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  193. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  194. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  195. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  196. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  197. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  198. };
  199. static const struct stm32f4_gate_data stm32f746_gates[] __initconst = {
  200. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  201. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  202. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  203. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  204. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  205. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  206. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  207. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  208. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  209. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  210. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  211. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  212. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  213. { STM32F4_RCC_AHB1ENR, 20, "dtcmram", "ahb_div" },
  214. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  215. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  216. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  217. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  218. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  219. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  220. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  221. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  222. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  223. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  224. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  225. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  226. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  227. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  228. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  229. CLK_IGNORE_UNUSED },
  230. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  231. CLK_IGNORE_UNUSED },
  232. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  233. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  234. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  235. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  236. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  237. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  238. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  239. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  240. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  241. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  242. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  243. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  244. { STM32F4_RCC_APB1ENR, 16, "spdifrx", "apb1_div" },
  245. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  246. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  247. { STM32F4_RCC_APB1ENR, 27, "cec", "apb1_div" },
  248. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  249. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  250. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  251. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  252. { STM32F4_RCC_APB2ENR, 7, "sdmmc2", "sdmux" },
  253. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  254. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  255. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  256. { STM32F4_RCC_APB2ENR, 11, "sdmmc", "sdmux" },
  257. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  258. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  259. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  260. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  261. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  262. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  263. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  264. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  265. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  266. { STM32F4_RCC_APB2ENR, 23, "sai2", "apb2_div" },
  267. };
  268. static const struct stm32f4_gate_data stm32f769_gates[] __initconst = {
  269. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  270. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  271. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  272. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  273. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  274. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  275. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  276. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  277. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  278. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  279. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  280. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  281. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  282. { STM32F4_RCC_AHB1ENR, 20, "dtcmram", "ahb_div" },
  283. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  284. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  285. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  286. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  287. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  288. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  289. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  290. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  291. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  292. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  293. { STM32F4_RCC_AHB2ENR, 1, "jpeg", "ahb_div" },
  294. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  295. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  296. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  297. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  298. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  299. CLK_IGNORE_UNUSED },
  300. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  301. CLK_IGNORE_UNUSED },
  302. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  303. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  304. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  305. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  306. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  307. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  308. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  309. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  310. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  311. { STM32F4_RCC_APB1ENR, 10, "rtcapb", "apb1_mul" },
  312. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  313. { STM32F4_RCC_APB1ENR, 13, "can3", "apb1_div" },
  314. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  315. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  316. { STM32F4_RCC_APB1ENR, 16, "spdifrx", "apb1_div" },
  317. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  318. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  319. { STM32F4_RCC_APB1ENR, 27, "cec", "apb1_div" },
  320. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  321. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  322. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  323. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  324. { STM32F4_RCC_APB2ENR, 7, "sdmmc2", "sdmux2" },
  325. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  326. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  327. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  328. { STM32F4_RCC_APB2ENR, 11, "sdmmc1", "sdmux1" },
  329. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  330. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  331. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  332. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  333. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  334. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  335. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  336. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  337. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  338. { STM32F4_RCC_APB2ENR, 23, "sai2", "apb2_div" },
  339. { STM32F4_RCC_APB2ENR, 30, "mdio", "apb2_div" },
  340. };
  341. /*
  342. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  343. * have gate bits associated with them. Its combined hweight is 71.
  344. */
  345. #define MAX_GATE_MAP 3
  346. static const u64 stm32f42xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  347. 0x0000000000000001ull,
  348. 0x04777f33f6fec9ffull };
  349. static const u64 stm32f46xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  350. 0x0000000000000003ull,
  351. 0x0c777f33f6fec9ffull };
  352. static const u64 stm32f746_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  353. 0x0000000000000003ull,
  354. 0x04f77f833e01c9ffull };
  355. static const u64 stm32f769_gate_map[MAX_GATE_MAP] = { 0x000000f37ef417ffull,
  356. 0x0000000000000003ull,
  357. 0x44F77F833E01EDFFull };
  358. static const u64 *stm32f4_gate_map;
  359. static struct clk_hw **clks;
  360. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  361. static void __iomem *base;
  362. static struct regmap *pdrm;
  363. static int stm32fx_end_primary_clk;
  364. /*
  365. * "Multiplier" device for APBx clocks.
  366. *
  367. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  368. * mode, they also tap out the one of the low order state bits to run the
  369. * timers. ST datasheets represent this feature as a (conditional) clock
  370. * multiplier.
  371. */
  372. struct clk_apb_mul {
  373. struct clk_hw hw;
  374. u8 bit_idx;
  375. };
  376. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  377. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  378. unsigned long parent_rate)
  379. {
  380. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  381. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  382. return parent_rate * 2;
  383. return parent_rate;
  384. }
  385. static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  386. unsigned long *prate)
  387. {
  388. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  389. unsigned long mult = 1;
  390. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  391. mult = 2;
  392. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  393. unsigned long best_parent = rate / mult;
  394. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  395. }
  396. return *prate * mult;
  397. }
  398. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  399. unsigned long parent_rate)
  400. {
  401. /*
  402. * We must report success but we can do so unconditionally because
  403. * clk_apb_mul_round_rate returns values that ensure this call is a
  404. * nop.
  405. */
  406. return 0;
  407. }
  408. static const struct clk_ops clk_apb_mul_factor_ops = {
  409. .round_rate = clk_apb_mul_round_rate,
  410. .set_rate = clk_apb_mul_set_rate,
  411. .recalc_rate = clk_apb_mul_recalc_rate,
  412. };
  413. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  414. const char *parent_name,
  415. unsigned long flags, u8 bit_idx)
  416. {
  417. struct clk_apb_mul *am;
  418. struct clk_init_data init;
  419. struct clk *clk;
  420. am = kzalloc(sizeof(*am), GFP_KERNEL);
  421. if (!am)
  422. return ERR_PTR(-ENOMEM);
  423. am->bit_idx = bit_idx;
  424. am->hw.init = &init;
  425. init.name = name;
  426. init.ops = &clk_apb_mul_factor_ops;
  427. init.flags = flags;
  428. init.parent_names = &parent_name;
  429. init.num_parents = 1;
  430. clk = clk_register(dev, &am->hw);
  431. if (IS_ERR(clk))
  432. kfree(am);
  433. return clk;
  434. }
  435. enum {
  436. PLL,
  437. PLL_I2S,
  438. PLL_SAI,
  439. };
  440. static const struct clk_div_table pll_divp_table[] = {
  441. { 0, 2 }, { 1, 4 }, { 2, 6 }, { 3, 8 }, { 0 }
  442. };
  443. static const struct clk_div_table pll_divq_table[] = {
  444. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
  445. { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 }, { 13, 13 },
  446. { 14, 14 }, { 15, 15 },
  447. { 0 }
  448. };
  449. static const struct clk_div_table pll_divr_table[] = {
  450. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 }, { 0 }
  451. };
  452. struct stm32f4_pll {
  453. spinlock_t *lock;
  454. struct clk_gate gate;
  455. u8 offset;
  456. u8 bit_rdy_idx;
  457. u8 status;
  458. u8 n_start;
  459. };
  460. #define to_stm32f4_pll(_gate) container_of(_gate, struct stm32f4_pll, gate)
  461. struct stm32f4_pll_post_div_data {
  462. int idx;
  463. int pll_idx;
  464. const char *name;
  465. const char *parent;
  466. u8 flag;
  467. u8 offset;
  468. u8 shift;
  469. u8 width;
  470. u8 flag_div;
  471. const struct clk_div_table *div_table;
  472. };
  473. struct stm32f4_vco_data {
  474. const char *vco_name;
  475. u8 offset;
  476. u8 bit_idx;
  477. u8 bit_rdy_idx;
  478. };
  479. static const struct stm32f4_vco_data vco_data[] = {
  480. { "vco", STM32F4_RCC_PLLCFGR, 24, 25 },
  481. { "vco-i2s", STM32F4_RCC_PLLI2SCFGR, 26, 27 },
  482. { "vco-sai", STM32F4_RCC_PLLSAICFGR, 28, 29 },
  483. };
  484. static const struct clk_div_table post_divr_table[] = {
  485. { 0, 2 }, { 1, 4 }, { 2, 8 }, { 3, 16 }, { 0 }
  486. };
  487. #define MAX_POST_DIV 3
  488. static const struct stm32f4_pll_post_div_data post_div_data[MAX_POST_DIV] = {
  489. { CLK_I2SQ_PDIV, PLL_VCO_I2S, "plli2s-q-div", "plli2s-q",
  490. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 0, 5, 0, NULL},
  491. { CLK_SAIQ_PDIV, PLL_VCO_SAI, "pllsai-q-div", "pllsai-q",
  492. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 8, 5, 0, NULL },
  493. { NO_IDX, PLL_VCO_SAI, "pllsai-r-div", "pllsai-r", CLK_SET_RATE_PARENT,
  494. STM32F4_RCC_DCKCFGR, 16, 2, 0, post_divr_table },
  495. };
  496. struct stm32f4_div_data {
  497. u8 shift;
  498. u8 width;
  499. u8 flag_div;
  500. const struct clk_div_table *div_table;
  501. };
  502. #define MAX_PLL_DIV 3
  503. static const struct stm32f4_div_data div_data[MAX_PLL_DIV] = {
  504. { 16, 2, 0, pll_divp_table },
  505. { 24, 4, 0, pll_divq_table },
  506. { 28, 3, 0, pll_divr_table },
  507. };
  508. struct stm32f4_pll_data {
  509. u8 pll_num;
  510. u8 n_start;
  511. const char *div_name[MAX_PLL_DIV];
  512. };
  513. static const struct stm32f4_pll_data stm32f429_pll[MAX_PLL_DIV] = {
  514. { PLL, 192, { "pll", "pll48", NULL } },
  515. { PLL_I2S, 192, { NULL, "plli2s-q", "plli2s-r" } },
  516. { PLL_SAI, 49, { NULL, "pllsai-q", "pllsai-r" } },
  517. };
  518. static const struct stm32f4_pll_data stm32f469_pll[MAX_PLL_DIV] = {
  519. { PLL, 50, { "pll", "pll-q", "pll-r" } },
  520. { PLL_I2S, 50, { "plli2s-p", "plli2s-q", "plli2s-r" } },
  521. { PLL_SAI, 50, { "pllsai-p", "pllsai-q", "pllsai-r" } },
  522. };
  523. static int stm32f4_pll_is_enabled(struct clk_hw *hw)
  524. {
  525. return clk_gate_ops.is_enabled(hw);
  526. }
  527. #define PLL_TIMEOUT 10000
  528. static int stm32f4_pll_enable(struct clk_hw *hw)
  529. {
  530. struct clk_gate *gate = to_clk_gate(hw);
  531. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  532. int bit_status;
  533. unsigned int timeout = PLL_TIMEOUT;
  534. if (clk_gate_ops.is_enabled(hw))
  535. return 0;
  536. clk_gate_ops.enable(hw);
  537. do {
  538. bit_status = !(readl(gate->reg) & BIT(pll->bit_rdy_idx));
  539. } while (bit_status && --timeout);
  540. return bit_status;
  541. }
  542. static void stm32f4_pll_disable(struct clk_hw *hw)
  543. {
  544. clk_gate_ops.disable(hw);
  545. }
  546. static unsigned long stm32f4_pll_recalc(struct clk_hw *hw,
  547. unsigned long parent_rate)
  548. {
  549. struct clk_gate *gate = to_clk_gate(hw);
  550. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  551. unsigned long n;
  552. n = (readl(base + pll->offset) >> 6) & 0x1ff;
  553. return parent_rate * n;
  554. }
  555. static long stm32f4_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  556. unsigned long *prate)
  557. {
  558. struct clk_gate *gate = to_clk_gate(hw);
  559. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  560. unsigned long n;
  561. n = rate / *prate;
  562. if (n < pll->n_start)
  563. n = pll->n_start;
  564. else if (n > 432)
  565. n = 432;
  566. return *prate * n;
  567. }
  568. static int stm32f4_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  569. unsigned long parent_rate)
  570. {
  571. struct clk_gate *gate = to_clk_gate(hw);
  572. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  573. unsigned long n;
  574. unsigned long val;
  575. int pll_state;
  576. pll_state = stm32f4_pll_is_enabled(hw);
  577. if (pll_state)
  578. stm32f4_pll_disable(hw);
  579. n = rate / parent_rate;
  580. val = readl(base + pll->offset) & ~(0x1ff << 6);
  581. writel(val | ((n & 0x1ff) << 6), base + pll->offset);
  582. if (pll_state)
  583. stm32f4_pll_enable(hw);
  584. return 0;
  585. }
  586. static const struct clk_ops stm32f4_pll_gate_ops = {
  587. .enable = stm32f4_pll_enable,
  588. .disable = stm32f4_pll_disable,
  589. .is_enabled = stm32f4_pll_is_enabled,
  590. .recalc_rate = stm32f4_pll_recalc,
  591. .round_rate = stm32f4_pll_round_rate,
  592. .set_rate = stm32f4_pll_set_rate,
  593. };
  594. struct stm32f4_pll_div {
  595. struct clk_divider div;
  596. struct clk_hw *hw_pll;
  597. };
  598. #define to_pll_div_clk(_div) container_of(_div, struct stm32f4_pll_div, div)
  599. static unsigned long stm32f4_pll_div_recalc_rate(struct clk_hw *hw,
  600. unsigned long parent_rate)
  601. {
  602. return clk_divider_ops.recalc_rate(hw, parent_rate);
  603. }
  604. static int stm32f4_pll_div_determine_rate(struct clk_hw *hw,
  605. struct clk_rate_request *req)
  606. {
  607. return clk_divider_ops.determine_rate(hw, req);
  608. }
  609. static int stm32f4_pll_div_set_rate(struct clk_hw *hw, unsigned long rate,
  610. unsigned long parent_rate)
  611. {
  612. int pll_state, ret;
  613. struct clk_divider *div = to_clk_divider(hw);
  614. struct stm32f4_pll_div *pll_div = to_pll_div_clk(div);
  615. pll_state = stm32f4_pll_is_enabled(pll_div->hw_pll);
  616. if (pll_state)
  617. stm32f4_pll_disable(pll_div->hw_pll);
  618. ret = clk_divider_ops.set_rate(hw, rate, parent_rate);
  619. if (pll_state)
  620. stm32f4_pll_enable(pll_div->hw_pll);
  621. return ret;
  622. }
  623. static const struct clk_ops stm32f4_pll_div_ops = {
  624. .recalc_rate = stm32f4_pll_div_recalc_rate,
  625. .determine_rate = stm32f4_pll_div_determine_rate,
  626. .set_rate = stm32f4_pll_div_set_rate,
  627. };
  628. static struct clk_hw *clk_register_pll_div(const char *name,
  629. const char *parent_name, unsigned long flags,
  630. void __iomem *reg, u8 shift, u8 width,
  631. u8 clk_divider_flags, const struct clk_div_table *table,
  632. struct clk_hw *pll_hw, spinlock_t *lock)
  633. {
  634. struct stm32f4_pll_div *pll_div;
  635. struct clk_hw *hw;
  636. struct clk_init_data init;
  637. int ret;
  638. /* allocate the divider */
  639. pll_div = kzalloc(sizeof(*pll_div), GFP_KERNEL);
  640. if (!pll_div)
  641. return ERR_PTR(-ENOMEM);
  642. init.name = name;
  643. init.ops = &stm32f4_pll_div_ops;
  644. init.flags = flags;
  645. init.parent_names = (parent_name ? &parent_name : NULL);
  646. init.num_parents = (parent_name ? 1 : 0);
  647. /* struct clk_divider assignments */
  648. pll_div->div.reg = reg;
  649. pll_div->div.shift = shift;
  650. pll_div->div.width = width;
  651. pll_div->div.flags = clk_divider_flags;
  652. pll_div->div.lock = lock;
  653. pll_div->div.table = table;
  654. pll_div->div.hw.init = &init;
  655. pll_div->hw_pll = pll_hw;
  656. /* register the clock */
  657. hw = &pll_div->div.hw;
  658. ret = clk_hw_register(NULL, hw);
  659. if (ret) {
  660. kfree(pll_div);
  661. hw = ERR_PTR(ret);
  662. }
  663. return hw;
  664. }
  665. static struct clk_hw *stm32f4_rcc_register_pll(const char *pllsrc,
  666. const struct stm32f4_pll_data *data, spinlock_t *lock)
  667. {
  668. struct stm32f4_pll *pll;
  669. struct clk_init_data init = { NULL };
  670. void __iomem *reg;
  671. struct clk_hw *pll_hw;
  672. int ret;
  673. int i;
  674. const struct stm32f4_vco_data *vco;
  675. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  676. if (!pll)
  677. return ERR_PTR(-ENOMEM);
  678. vco = &vco_data[data->pll_num];
  679. init.name = vco->vco_name;
  680. init.ops = &stm32f4_pll_gate_ops;
  681. init.flags = CLK_SET_RATE_GATE;
  682. init.parent_names = &pllsrc;
  683. init.num_parents = 1;
  684. pll->gate.lock = lock;
  685. pll->gate.reg = base + STM32F4_RCC_CR;
  686. pll->gate.bit_idx = vco->bit_idx;
  687. pll->gate.hw.init = &init;
  688. pll->offset = vco->offset;
  689. pll->n_start = data->n_start;
  690. pll->bit_rdy_idx = vco->bit_rdy_idx;
  691. pll->status = (readl(base + STM32F4_RCC_CR) >> vco->bit_idx) & 0x1;
  692. reg = base + pll->offset;
  693. pll_hw = &pll->gate.hw;
  694. ret = clk_hw_register(NULL, pll_hw);
  695. if (ret) {
  696. kfree(pll);
  697. return ERR_PTR(ret);
  698. }
  699. for (i = 0; i < MAX_PLL_DIV; i++)
  700. if (data->div_name[i])
  701. clk_register_pll_div(data->div_name[i],
  702. vco->vco_name,
  703. 0,
  704. reg,
  705. div_data[i].shift,
  706. div_data[i].width,
  707. div_data[i].flag_div,
  708. div_data[i].div_table,
  709. pll_hw,
  710. lock);
  711. return pll_hw;
  712. }
  713. /*
  714. * Converts the primary and secondary indices (as they appear in DT) to an
  715. * offset into our struct clock array.
  716. */
  717. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  718. {
  719. u64 table[MAX_GATE_MAP];
  720. if (primary == 1) {
  721. if (WARN_ON(secondary >= stm32fx_end_primary_clk))
  722. return -EINVAL;
  723. return secondary;
  724. }
  725. memcpy(table, stm32f4_gate_map, sizeof(table));
  726. /* only bits set in table can be used as indices */
  727. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  728. 0 == (table[BIT_ULL_WORD(secondary)] &
  729. BIT_ULL_MASK(secondary))))
  730. return -EINVAL;
  731. /* mask out bits above our current index */
  732. table[BIT_ULL_WORD(secondary)] &=
  733. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  734. return stm32fx_end_primary_clk - 1 + hweight64(table[0]) +
  735. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  736. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  737. }
  738. static struct clk_hw *
  739. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  740. {
  741. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  742. if (i < 0)
  743. return ERR_PTR(-EINVAL);
  744. return clks[i];
  745. }
  746. #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  747. static inline void disable_power_domain_write_protection(void)
  748. {
  749. if (pdrm)
  750. regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  751. }
  752. static inline void enable_power_domain_write_protection(void)
  753. {
  754. if (pdrm)
  755. regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  756. }
  757. static inline void sofware_reset_backup_domain(void)
  758. {
  759. unsigned long val;
  760. val = readl(base + STM32F4_RCC_BDCR);
  761. writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  762. writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  763. }
  764. struct stm32_rgate {
  765. struct clk_gate gate;
  766. u8 bit_rdy_idx;
  767. };
  768. #define RGATE_TIMEOUT 50000
  769. static int rgclk_enable(struct clk_hw *hw)
  770. {
  771. struct clk_gate *gate = to_clk_gate(hw);
  772. struct stm32_rgate *rgate = to_rgclk(gate);
  773. int bit_status;
  774. unsigned int timeout = RGATE_TIMEOUT;
  775. if (clk_gate_ops.is_enabled(hw))
  776. return 0;
  777. disable_power_domain_write_protection();
  778. clk_gate_ops.enable(hw);
  779. do {
  780. bit_status = !(readl(gate->reg) & BIT(rgate->bit_rdy_idx));
  781. if (bit_status)
  782. udelay(100);
  783. } while (bit_status && --timeout);
  784. enable_power_domain_write_protection();
  785. return bit_status;
  786. }
  787. static void rgclk_disable(struct clk_hw *hw)
  788. {
  789. clk_gate_ops.disable(hw);
  790. }
  791. static int rgclk_is_enabled(struct clk_hw *hw)
  792. {
  793. return clk_gate_ops.is_enabled(hw);
  794. }
  795. static const struct clk_ops rgclk_ops = {
  796. .enable = rgclk_enable,
  797. .disable = rgclk_disable,
  798. .is_enabled = rgclk_is_enabled,
  799. };
  800. static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  801. const char *parent_name, unsigned long flags,
  802. void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  803. u8 clk_gate_flags, spinlock_t *lock)
  804. {
  805. struct stm32_rgate *rgate;
  806. struct clk_init_data init = { NULL };
  807. struct clk_hw *hw;
  808. int ret;
  809. rgate = kzalloc(sizeof(*rgate), GFP_KERNEL);
  810. if (!rgate)
  811. return ERR_PTR(-ENOMEM);
  812. init.name = name;
  813. init.ops = &rgclk_ops;
  814. init.flags = flags;
  815. init.parent_names = &parent_name;
  816. init.num_parents = 1;
  817. rgate->bit_rdy_idx = bit_rdy_idx;
  818. rgate->gate.lock = lock;
  819. rgate->gate.reg = reg;
  820. rgate->gate.bit_idx = bit_idx;
  821. rgate->gate.hw.init = &init;
  822. hw = &rgate->gate.hw;
  823. ret = clk_hw_register(dev, hw);
  824. if (ret) {
  825. kfree(rgate);
  826. hw = ERR_PTR(ret);
  827. }
  828. return hw;
  829. }
  830. static int cclk_gate_enable(struct clk_hw *hw)
  831. {
  832. int ret;
  833. disable_power_domain_write_protection();
  834. ret = clk_gate_ops.enable(hw);
  835. enable_power_domain_write_protection();
  836. return ret;
  837. }
  838. static void cclk_gate_disable(struct clk_hw *hw)
  839. {
  840. disable_power_domain_write_protection();
  841. clk_gate_ops.disable(hw);
  842. enable_power_domain_write_protection();
  843. }
  844. static int cclk_gate_is_enabled(struct clk_hw *hw)
  845. {
  846. return clk_gate_ops.is_enabled(hw);
  847. }
  848. static const struct clk_ops cclk_gate_ops = {
  849. .enable = cclk_gate_enable,
  850. .disable = cclk_gate_disable,
  851. .is_enabled = cclk_gate_is_enabled,
  852. };
  853. static u8 cclk_mux_get_parent(struct clk_hw *hw)
  854. {
  855. return clk_mux_ops.get_parent(hw);
  856. }
  857. static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  858. {
  859. int ret;
  860. disable_power_domain_write_protection();
  861. sofware_reset_backup_domain();
  862. ret = clk_mux_ops.set_parent(hw, index);
  863. enable_power_domain_write_protection();
  864. return ret;
  865. }
  866. static const struct clk_ops cclk_mux_ops = {
  867. .determine_rate = clk_hw_determine_rate_no_reparent,
  868. .get_parent = cclk_mux_get_parent,
  869. .set_parent = cclk_mux_set_parent,
  870. };
  871. static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  872. const char * const *parent_names, int num_parents,
  873. void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  874. spinlock_t *lock)
  875. {
  876. struct clk_hw *hw;
  877. struct clk_gate *gate;
  878. struct clk_mux *mux;
  879. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  880. if (!gate) {
  881. hw = ERR_PTR(-EINVAL);
  882. goto fail;
  883. }
  884. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  885. if (!mux) {
  886. kfree(gate);
  887. hw = ERR_PTR(-EINVAL);
  888. goto fail;
  889. }
  890. gate->reg = reg;
  891. gate->bit_idx = bit_idx;
  892. gate->flags = 0;
  893. gate->lock = lock;
  894. mux->reg = reg;
  895. mux->shift = shift;
  896. mux->mask = 3;
  897. mux->flags = 0;
  898. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  899. &mux->hw, &cclk_mux_ops,
  900. NULL, NULL,
  901. &gate->hw, &cclk_gate_ops,
  902. flags);
  903. if (IS_ERR(hw)) {
  904. kfree(gate);
  905. kfree(mux);
  906. }
  907. fail:
  908. return hw;
  909. }
  910. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  911. static const struct clk_div_table ahb_div_table[] = {
  912. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  913. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  914. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  915. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  916. { 0 },
  917. };
  918. static const struct clk_div_table apb_div_table[] = {
  919. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  920. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  921. { 0 },
  922. };
  923. static const char *rtc_parents[4] = {
  924. "no-clock", "lse", "lsi", "hse-rtc"
  925. };
  926. static const char *pll_src = "pll-src";
  927. static const char *pllsrc_parent[2] = { "hsi", NULL };
  928. static const char *dsi_parent[2] = { NULL, "pll-r" };
  929. static const char *lcd_parent[1] = { "pllsai-r-div" };
  930. static const char *i2s_parents[2] = { "plli2s-r", NULL };
  931. static const char *sai_parents[4] = { "pllsai-q-div", "plli2s-q-div", NULL,
  932. "no-clock" };
  933. static const char *pll48_parents[2] = { "pll-q", "pllsai-p" };
  934. static const char *sdmux_parents[2] = { "pll48", "sys" };
  935. static const char *hdmi_parents[2] = { "lse", "hsi_div488" };
  936. static const char *spdif_parent[1] = { "plli2s-p" };
  937. static const char *lptim_parent[4] = { "apb1_mul", "lsi", "hsi", "lse" };
  938. static const char *uart_parents1[4] = { "apb2_div", "sys", "hsi", "lse" };
  939. static const char *uart_parents2[4] = { "apb1_div", "sys", "hsi", "lse" };
  940. static const char *i2c_parents[4] = { "apb1_div", "sys", "hsi", "no-clock" };
  941. static const char * const dfsdm1_src[] = { "apb2_div", "sys" };
  942. static const char * const adsfdm1_parent[] = { "sai1_clk", "sai2_clk" };
  943. struct stm32_aux_clk {
  944. int idx;
  945. const char *name;
  946. const char * const *parent_names;
  947. int num_parents;
  948. int offset_mux;
  949. u8 shift;
  950. u8 mask;
  951. int offset_gate;
  952. u8 bit_idx;
  953. unsigned long flags;
  954. };
  955. struct stm32f4_clk_data {
  956. const struct stm32f4_gate_data *gates_data;
  957. const u64 *gates_map;
  958. int gates_num;
  959. const struct stm32f4_pll_data *pll_data;
  960. const struct stm32_aux_clk *aux_clk;
  961. int aux_clk_num;
  962. int end_primary;
  963. };
  964. static const struct stm32_aux_clk stm32f429_aux_clk[] = {
  965. {
  966. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  967. NO_MUX, 0, 0,
  968. STM32F4_RCC_APB2ENR, 26,
  969. CLK_SET_RATE_PARENT
  970. },
  971. {
  972. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  973. STM32F4_RCC_CFGR, 23, 1,
  974. NO_GATE, 0,
  975. CLK_SET_RATE_PARENT
  976. },
  977. {
  978. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  979. STM32F4_RCC_DCKCFGR, 20, 3,
  980. STM32F4_RCC_APB2ENR, 22,
  981. CLK_SET_RATE_PARENT
  982. },
  983. {
  984. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  985. STM32F4_RCC_DCKCFGR, 22, 3,
  986. STM32F4_RCC_APB2ENR, 22,
  987. CLK_SET_RATE_PARENT
  988. },
  989. };
  990. static const struct stm32_aux_clk stm32f469_aux_clk[] = {
  991. {
  992. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  993. NO_MUX, 0, 0,
  994. STM32F4_RCC_APB2ENR, 26,
  995. CLK_SET_RATE_PARENT
  996. },
  997. {
  998. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  999. STM32F4_RCC_CFGR, 23, 1,
  1000. NO_GATE, 0,
  1001. CLK_SET_RATE_PARENT
  1002. },
  1003. {
  1004. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  1005. STM32F4_RCC_DCKCFGR, 20, 3,
  1006. STM32F4_RCC_APB2ENR, 22,
  1007. CLK_SET_RATE_PARENT
  1008. },
  1009. {
  1010. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  1011. STM32F4_RCC_DCKCFGR, 22, 3,
  1012. STM32F4_RCC_APB2ENR, 22,
  1013. CLK_SET_RATE_PARENT
  1014. },
  1015. {
  1016. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  1017. STM32F4_RCC_DCKCFGR, 27, 1,
  1018. NO_GATE, 0,
  1019. 0
  1020. },
  1021. {
  1022. NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1023. STM32F4_RCC_DCKCFGR, 28, 1,
  1024. NO_GATE, 0,
  1025. 0
  1026. },
  1027. {
  1028. CLK_F469_DSI, "dsi", dsi_parent, ARRAY_SIZE(dsi_parent),
  1029. STM32F4_RCC_DCKCFGR, 29, 1,
  1030. STM32F4_RCC_APB2ENR, 27,
  1031. CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
  1032. },
  1033. };
  1034. static const struct stm32_aux_clk stm32f746_aux_clk[] = {
  1035. {
  1036. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  1037. NO_MUX, 0, 0,
  1038. STM32F4_RCC_APB2ENR, 26,
  1039. CLK_SET_RATE_PARENT
  1040. },
  1041. {
  1042. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  1043. STM32F4_RCC_CFGR, 23, 1,
  1044. NO_GATE, 0,
  1045. CLK_SET_RATE_PARENT
  1046. },
  1047. {
  1048. CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1049. STM32F4_RCC_DCKCFGR, 20, 3,
  1050. STM32F4_RCC_APB2ENR, 22,
  1051. CLK_SET_RATE_PARENT
  1052. },
  1053. {
  1054. CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1055. STM32F4_RCC_DCKCFGR, 22, 3,
  1056. STM32F4_RCC_APB2ENR, 23,
  1057. CLK_SET_RATE_PARENT
  1058. },
  1059. {
  1060. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  1061. STM32F7_RCC_DCKCFGR2, 27, 1,
  1062. NO_GATE, 0,
  1063. 0
  1064. },
  1065. {
  1066. NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1067. STM32F7_RCC_DCKCFGR2, 28, 1,
  1068. NO_GATE, 0,
  1069. 0
  1070. },
  1071. {
  1072. CLK_HDMI_CEC, "hdmi-cec",
  1073. hdmi_parents, ARRAY_SIZE(hdmi_parents),
  1074. STM32F7_RCC_DCKCFGR2, 26, 1,
  1075. NO_GATE, 0,
  1076. 0
  1077. },
  1078. {
  1079. CLK_SPDIF, "spdif-rx",
  1080. spdif_parent, ARRAY_SIZE(spdif_parent),
  1081. STM32F7_RCC_DCKCFGR2, 22, 3,
  1082. STM32F4_RCC_APB2ENR, 23,
  1083. CLK_SET_RATE_PARENT
  1084. },
  1085. {
  1086. CLK_USART1, "usart1",
  1087. uart_parents1, ARRAY_SIZE(uart_parents1),
  1088. STM32F7_RCC_DCKCFGR2, 0, 3,
  1089. STM32F4_RCC_APB2ENR, 4,
  1090. CLK_SET_RATE_PARENT,
  1091. },
  1092. {
  1093. CLK_USART2, "usart2",
  1094. uart_parents2, ARRAY_SIZE(uart_parents1),
  1095. STM32F7_RCC_DCKCFGR2, 2, 3,
  1096. STM32F4_RCC_APB1ENR, 17,
  1097. CLK_SET_RATE_PARENT,
  1098. },
  1099. {
  1100. CLK_USART3, "usart3",
  1101. uart_parents2, ARRAY_SIZE(uart_parents1),
  1102. STM32F7_RCC_DCKCFGR2, 4, 3,
  1103. STM32F4_RCC_APB1ENR, 18,
  1104. CLK_SET_RATE_PARENT,
  1105. },
  1106. {
  1107. CLK_UART4, "uart4",
  1108. uart_parents2, ARRAY_SIZE(uart_parents1),
  1109. STM32F7_RCC_DCKCFGR2, 6, 3,
  1110. STM32F4_RCC_APB1ENR, 19,
  1111. CLK_SET_RATE_PARENT,
  1112. },
  1113. {
  1114. CLK_UART5, "uart5",
  1115. uart_parents2, ARRAY_SIZE(uart_parents1),
  1116. STM32F7_RCC_DCKCFGR2, 8, 3,
  1117. STM32F4_RCC_APB1ENR, 20,
  1118. CLK_SET_RATE_PARENT,
  1119. },
  1120. {
  1121. CLK_USART6, "usart6",
  1122. uart_parents1, ARRAY_SIZE(uart_parents1),
  1123. STM32F7_RCC_DCKCFGR2, 10, 3,
  1124. STM32F4_RCC_APB2ENR, 5,
  1125. CLK_SET_RATE_PARENT,
  1126. },
  1127. {
  1128. CLK_UART7, "uart7",
  1129. uart_parents2, ARRAY_SIZE(uart_parents1),
  1130. STM32F7_RCC_DCKCFGR2, 12, 3,
  1131. STM32F4_RCC_APB1ENR, 30,
  1132. CLK_SET_RATE_PARENT,
  1133. },
  1134. {
  1135. CLK_UART8, "uart8",
  1136. uart_parents2, ARRAY_SIZE(uart_parents1),
  1137. STM32F7_RCC_DCKCFGR2, 14, 3,
  1138. STM32F4_RCC_APB1ENR, 31,
  1139. CLK_SET_RATE_PARENT,
  1140. },
  1141. {
  1142. CLK_I2C1, "i2c1",
  1143. i2c_parents, ARRAY_SIZE(i2c_parents),
  1144. STM32F7_RCC_DCKCFGR2, 16, 3,
  1145. STM32F4_RCC_APB1ENR, 21,
  1146. CLK_SET_RATE_PARENT,
  1147. },
  1148. {
  1149. CLK_I2C2, "i2c2",
  1150. i2c_parents, ARRAY_SIZE(i2c_parents),
  1151. STM32F7_RCC_DCKCFGR2, 18, 3,
  1152. STM32F4_RCC_APB1ENR, 22,
  1153. CLK_SET_RATE_PARENT,
  1154. },
  1155. {
  1156. CLK_I2C3, "i2c3",
  1157. i2c_parents, ARRAY_SIZE(i2c_parents),
  1158. STM32F7_RCC_DCKCFGR2, 20, 3,
  1159. STM32F4_RCC_APB1ENR, 23,
  1160. CLK_SET_RATE_PARENT,
  1161. },
  1162. {
  1163. CLK_I2C4, "i2c4",
  1164. i2c_parents, ARRAY_SIZE(i2c_parents),
  1165. STM32F7_RCC_DCKCFGR2, 22, 3,
  1166. STM32F4_RCC_APB1ENR, 24,
  1167. CLK_SET_RATE_PARENT,
  1168. },
  1169. {
  1170. CLK_LPTIMER, "lptim1",
  1171. lptim_parent, ARRAY_SIZE(lptim_parent),
  1172. STM32F7_RCC_DCKCFGR2, 24, 3,
  1173. STM32F4_RCC_APB1ENR, 9,
  1174. CLK_SET_RATE_PARENT
  1175. },
  1176. };
  1177. static const struct stm32_aux_clk stm32f769_aux_clk[] = {
  1178. {
  1179. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  1180. NO_MUX, 0, 0,
  1181. STM32F4_RCC_APB2ENR, 26,
  1182. CLK_SET_RATE_PARENT
  1183. },
  1184. {
  1185. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  1186. STM32F4_RCC_CFGR, 23, 1,
  1187. NO_GATE, 0,
  1188. CLK_SET_RATE_PARENT
  1189. },
  1190. {
  1191. CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1192. STM32F4_RCC_DCKCFGR, 20, 3,
  1193. STM32F4_RCC_APB2ENR, 22,
  1194. CLK_SET_RATE_PARENT
  1195. },
  1196. {
  1197. CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1198. STM32F4_RCC_DCKCFGR, 22, 3,
  1199. STM32F4_RCC_APB2ENR, 23,
  1200. CLK_SET_RATE_PARENT
  1201. },
  1202. {
  1203. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  1204. STM32F7_RCC_DCKCFGR2, 27, 1,
  1205. NO_GATE, 0,
  1206. 0
  1207. },
  1208. {
  1209. NO_IDX, "sdmux1", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1210. STM32F7_RCC_DCKCFGR2, 28, 1,
  1211. NO_GATE, 0,
  1212. 0
  1213. },
  1214. {
  1215. NO_IDX, "sdmux2", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1216. STM32F7_RCC_DCKCFGR2, 29, 1,
  1217. NO_GATE, 0,
  1218. 0
  1219. },
  1220. {
  1221. CLK_HDMI_CEC, "hdmi-cec",
  1222. hdmi_parents, ARRAY_SIZE(hdmi_parents),
  1223. STM32F7_RCC_DCKCFGR2, 26, 1,
  1224. NO_GATE, 0,
  1225. 0
  1226. },
  1227. {
  1228. CLK_SPDIF, "spdif-rx",
  1229. spdif_parent, ARRAY_SIZE(spdif_parent),
  1230. STM32F7_RCC_DCKCFGR2, 22, 3,
  1231. STM32F4_RCC_APB2ENR, 23,
  1232. CLK_SET_RATE_PARENT
  1233. },
  1234. {
  1235. CLK_USART1, "usart1",
  1236. uart_parents1, ARRAY_SIZE(uart_parents1),
  1237. STM32F7_RCC_DCKCFGR2, 0, 3,
  1238. STM32F4_RCC_APB2ENR, 4,
  1239. CLK_SET_RATE_PARENT,
  1240. },
  1241. {
  1242. CLK_USART2, "usart2",
  1243. uart_parents2, ARRAY_SIZE(uart_parents1),
  1244. STM32F7_RCC_DCKCFGR2, 2, 3,
  1245. STM32F4_RCC_APB1ENR, 17,
  1246. CLK_SET_RATE_PARENT,
  1247. },
  1248. {
  1249. CLK_USART3, "usart3",
  1250. uart_parents2, ARRAY_SIZE(uart_parents1),
  1251. STM32F7_RCC_DCKCFGR2, 4, 3,
  1252. STM32F4_RCC_APB1ENR, 18,
  1253. CLK_SET_RATE_PARENT,
  1254. },
  1255. {
  1256. CLK_UART4, "uart4",
  1257. uart_parents2, ARRAY_SIZE(uart_parents1),
  1258. STM32F7_RCC_DCKCFGR2, 6, 3,
  1259. STM32F4_RCC_APB1ENR, 19,
  1260. CLK_SET_RATE_PARENT,
  1261. },
  1262. {
  1263. CLK_UART5, "uart5",
  1264. uart_parents2, ARRAY_SIZE(uart_parents1),
  1265. STM32F7_RCC_DCKCFGR2, 8, 3,
  1266. STM32F4_RCC_APB1ENR, 20,
  1267. CLK_SET_RATE_PARENT,
  1268. },
  1269. {
  1270. CLK_USART6, "usart6",
  1271. uart_parents1, ARRAY_SIZE(uart_parents1),
  1272. STM32F7_RCC_DCKCFGR2, 10, 3,
  1273. STM32F4_RCC_APB2ENR, 5,
  1274. CLK_SET_RATE_PARENT,
  1275. },
  1276. {
  1277. CLK_UART7, "uart7",
  1278. uart_parents2, ARRAY_SIZE(uart_parents1),
  1279. STM32F7_RCC_DCKCFGR2, 12, 3,
  1280. STM32F4_RCC_APB1ENR, 30,
  1281. CLK_SET_RATE_PARENT,
  1282. },
  1283. {
  1284. CLK_UART8, "uart8",
  1285. uart_parents2, ARRAY_SIZE(uart_parents1),
  1286. STM32F7_RCC_DCKCFGR2, 14, 3,
  1287. STM32F4_RCC_APB1ENR, 31,
  1288. CLK_SET_RATE_PARENT,
  1289. },
  1290. {
  1291. CLK_I2C1, "i2c1",
  1292. i2c_parents, ARRAY_SIZE(i2c_parents),
  1293. STM32F7_RCC_DCKCFGR2, 16, 3,
  1294. STM32F4_RCC_APB1ENR, 21,
  1295. CLK_SET_RATE_PARENT,
  1296. },
  1297. {
  1298. CLK_I2C2, "i2c2",
  1299. i2c_parents, ARRAY_SIZE(i2c_parents),
  1300. STM32F7_RCC_DCKCFGR2, 18, 3,
  1301. STM32F4_RCC_APB1ENR, 22,
  1302. CLK_SET_RATE_PARENT,
  1303. },
  1304. {
  1305. CLK_I2C3, "i2c3",
  1306. i2c_parents, ARRAY_SIZE(i2c_parents),
  1307. STM32F7_RCC_DCKCFGR2, 20, 3,
  1308. STM32F4_RCC_APB1ENR, 23,
  1309. CLK_SET_RATE_PARENT,
  1310. },
  1311. {
  1312. CLK_I2C4, "i2c4",
  1313. i2c_parents, ARRAY_SIZE(i2c_parents),
  1314. STM32F7_RCC_DCKCFGR2, 22, 3,
  1315. STM32F4_RCC_APB1ENR, 24,
  1316. CLK_SET_RATE_PARENT,
  1317. },
  1318. {
  1319. CLK_LPTIMER, "lptim1",
  1320. lptim_parent, ARRAY_SIZE(lptim_parent),
  1321. STM32F7_RCC_DCKCFGR2, 24, 3,
  1322. STM32F4_RCC_APB1ENR, 9,
  1323. CLK_SET_RATE_PARENT
  1324. },
  1325. {
  1326. CLK_F769_DSI, "dsi",
  1327. dsi_parent, ARRAY_SIZE(dsi_parent),
  1328. STM32F7_RCC_DCKCFGR2, 0, 1,
  1329. STM32F4_RCC_APB2ENR, 27,
  1330. CLK_SET_RATE_PARENT
  1331. },
  1332. {
  1333. CLK_DFSDM1, "dfsdm1",
  1334. dfsdm1_src, ARRAY_SIZE(dfsdm1_src),
  1335. STM32F4_RCC_DCKCFGR, 25, 1,
  1336. STM32F4_RCC_APB2ENR, 29,
  1337. CLK_SET_RATE_PARENT
  1338. },
  1339. {
  1340. CLK_ADFSDM1, "adfsdm1",
  1341. adsfdm1_parent, ARRAY_SIZE(adsfdm1_parent),
  1342. STM32F4_RCC_DCKCFGR, 26, 1,
  1343. STM32F4_RCC_APB2ENR, 29,
  1344. CLK_SET_RATE_PARENT
  1345. },
  1346. };
  1347. static const struct stm32f4_clk_data stm32f429_clk_data = {
  1348. .end_primary = END_PRIMARY_CLK,
  1349. .gates_data = stm32f429_gates,
  1350. .gates_map = stm32f42xx_gate_map,
  1351. .gates_num = ARRAY_SIZE(stm32f429_gates),
  1352. .pll_data = stm32f429_pll,
  1353. .aux_clk = stm32f429_aux_clk,
  1354. .aux_clk_num = ARRAY_SIZE(stm32f429_aux_clk),
  1355. };
  1356. static const struct stm32f4_clk_data stm32f469_clk_data = {
  1357. .end_primary = END_PRIMARY_CLK,
  1358. .gates_data = stm32f469_gates,
  1359. .gates_map = stm32f46xx_gate_map,
  1360. .gates_num = ARRAY_SIZE(stm32f469_gates),
  1361. .pll_data = stm32f469_pll,
  1362. .aux_clk = stm32f469_aux_clk,
  1363. .aux_clk_num = ARRAY_SIZE(stm32f469_aux_clk),
  1364. };
  1365. static const struct stm32f4_clk_data stm32f746_clk_data = {
  1366. .end_primary = END_PRIMARY_CLK_F7,
  1367. .gates_data = stm32f746_gates,
  1368. .gates_map = stm32f746_gate_map,
  1369. .gates_num = ARRAY_SIZE(stm32f746_gates),
  1370. .pll_data = stm32f469_pll,
  1371. .aux_clk = stm32f746_aux_clk,
  1372. .aux_clk_num = ARRAY_SIZE(stm32f746_aux_clk),
  1373. };
  1374. static const struct stm32f4_clk_data stm32f769_clk_data = {
  1375. .end_primary = END_PRIMARY_CLK_F7,
  1376. .gates_data = stm32f769_gates,
  1377. .gates_map = stm32f769_gate_map,
  1378. .gates_num = ARRAY_SIZE(stm32f769_gates),
  1379. .pll_data = stm32f469_pll,
  1380. .aux_clk = stm32f769_aux_clk,
  1381. .aux_clk_num = ARRAY_SIZE(stm32f769_aux_clk),
  1382. };
  1383. static const struct of_device_id stm32f4_of_match[] = {
  1384. {
  1385. .compatible = "st,stm32f42xx-rcc",
  1386. .data = &stm32f429_clk_data
  1387. },
  1388. {
  1389. .compatible = "st,stm32f469-rcc",
  1390. .data = &stm32f469_clk_data
  1391. },
  1392. {
  1393. .compatible = "st,stm32f746-rcc",
  1394. .data = &stm32f746_clk_data
  1395. },
  1396. {
  1397. .compatible = "st,stm32f769-rcc",
  1398. .data = &stm32f769_clk_data
  1399. },
  1400. {}
  1401. };
  1402. static struct clk_hw *stm32_register_aux_clk(const char *name,
  1403. const char * const *parent_names, int num_parents,
  1404. int offset_mux, u8 shift, u8 mask,
  1405. int offset_gate, u8 bit_idx,
  1406. unsigned long flags, spinlock_t *lock)
  1407. {
  1408. struct clk_hw *hw;
  1409. struct clk_gate *gate = NULL;
  1410. struct clk_mux *mux = NULL;
  1411. struct clk_hw *mux_hw = NULL, *gate_hw = NULL;
  1412. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL;
  1413. if (offset_gate != NO_GATE) {
  1414. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  1415. if (!gate) {
  1416. hw = ERR_PTR(-EINVAL);
  1417. goto fail;
  1418. }
  1419. gate->reg = base + offset_gate;
  1420. gate->bit_idx = bit_idx;
  1421. gate->flags = 0;
  1422. gate->lock = lock;
  1423. gate_hw = &gate->hw;
  1424. gate_ops = &clk_gate_ops;
  1425. }
  1426. if (offset_mux != NO_MUX) {
  1427. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  1428. if (!mux) {
  1429. hw = ERR_PTR(-EINVAL);
  1430. goto fail;
  1431. }
  1432. mux->reg = base + offset_mux;
  1433. mux->shift = shift;
  1434. mux->mask = mask;
  1435. mux->flags = 0;
  1436. mux_hw = &mux->hw;
  1437. mux_ops = &clk_mux_ops;
  1438. }
  1439. if (mux_hw == NULL && gate_hw == NULL) {
  1440. hw = ERR_PTR(-EINVAL);
  1441. goto fail;
  1442. }
  1443. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  1444. mux_hw, mux_ops,
  1445. NULL, NULL,
  1446. gate_hw, gate_ops,
  1447. flags);
  1448. fail:
  1449. if (IS_ERR(hw)) {
  1450. kfree(gate);
  1451. kfree(mux);
  1452. }
  1453. return hw;
  1454. }
  1455. static void __init stm32f4_rcc_init(struct device_node *np)
  1456. {
  1457. const char *hse_clk, *i2s_in_clk;
  1458. int n;
  1459. const struct of_device_id *match;
  1460. const struct stm32f4_clk_data *data;
  1461. unsigned long pllm;
  1462. struct clk_hw *pll_src_hw;
  1463. base = of_iomap(np, 0);
  1464. if (!base) {
  1465. pr_err("%pOFn: unable to map resource\n", np);
  1466. return;
  1467. }
  1468. pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1469. if (IS_ERR(pdrm)) {
  1470. pdrm = NULL;
  1471. pr_warn("%s: Unable to get syscfg\n", __func__);
  1472. }
  1473. match = of_match_node(stm32f4_of_match, np);
  1474. if (WARN_ON(!match))
  1475. return;
  1476. data = match->data;
  1477. stm32fx_end_primary_clk = data->end_primary;
  1478. clks = kmalloc_array(data->gates_num + stm32fx_end_primary_clk,
  1479. sizeof(*clks), GFP_KERNEL);
  1480. if (!clks)
  1481. goto fail;
  1482. stm32f4_gate_map = data->gates_map;
  1483. hse_clk = of_clk_get_parent_name(np, 0);
  1484. dsi_parent[0] = hse_clk;
  1485. pllsrc_parent[1] = hse_clk;
  1486. i2s_in_clk = of_clk_get_parent_name(np, 1);
  1487. i2s_parents[1] = i2s_in_clk;
  1488. sai_parents[2] = i2s_in_clk;
  1489. if (of_device_is_compatible(np, "st,stm32f769-rcc")) {
  1490. clk_hw_register_gate(NULL, "dfsdm1_apb", "apb2_div", 0,
  1491. base + STM32F4_RCC_APB2ENR, 29,
  1492. CLK_IGNORE_UNUSED, &stm32f4_clk_lock);
  1493. dsi_parent[0] = pll_src;
  1494. sai_parents[3] = pll_src;
  1495. }
  1496. clks[CLK_HSI] = clk_hw_register_fixed_rate_with_accuracy(NULL, "hsi",
  1497. NULL, 0, 16000000, 160000);
  1498. pll_src_hw = clk_hw_register_mux(NULL, pll_src, pllsrc_parent,
  1499. ARRAY_SIZE(pllsrc_parent), 0,
  1500. base + STM32F4_RCC_PLLCFGR, 22, 1, 0,
  1501. &stm32f4_clk_lock);
  1502. pllm = readl(base + STM32F4_RCC_PLLCFGR) & 0x3f;
  1503. clk_hw_register_fixed_factor(NULL, "vco_in", pll_src,
  1504. 0, 1, pllm);
  1505. stm32f4_rcc_register_pll("vco_in", &data->pll_data[0],
  1506. &stm32f4_clk_lock);
  1507. clks[PLL_VCO_I2S] = stm32f4_rcc_register_pll("vco_in",
  1508. &data->pll_data[1], &stm32f4_clk_lock);
  1509. clks[PLL_VCO_SAI] = stm32f4_rcc_register_pll("vco_in",
  1510. &data->pll_data[2], &stm32f4_clk_lock);
  1511. for (n = 0; n < MAX_POST_DIV; n++) {
  1512. const struct stm32f4_pll_post_div_data *post_div;
  1513. struct clk_hw *hw;
  1514. post_div = &post_div_data[n];
  1515. hw = clk_register_pll_div(post_div->name,
  1516. post_div->parent,
  1517. post_div->flag,
  1518. base + post_div->offset,
  1519. post_div->shift,
  1520. post_div->width,
  1521. post_div->flag_div,
  1522. post_div->div_table,
  1523. clks[post_div->pll_idx],
  1524. &stm32f4_clk_lock);
  1525. if (post_div->idx != NO_IDX)
  1526. clks[post_div->idx] = hw;
  1527. }
  1528. sys_parents[1] = hse_clk;
  1529. clks[CLK_SYSCLK] = clk_hw_register_mux_table(
  1530. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  1531. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  1532. clk_register_divider_table(NULL, "ahb_div", "sys",
  1533. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1534. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  1535. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  1536. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1537. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  1538. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  1539. CLK_SET_RATE_PARENT, 12);
  1540. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  1541. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1542. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  1543. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  1544. CLK_SET_RATE_PARENT, 15);
  1545. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  1546. 0, 1, 8);
  1547. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  1548. 0, 1, 1);
  1549. for (n = 0; n < data->gates_num; n++) {
  1550. const struct stm32f4_gate_data *gd;
  1551. unsigned int secondary;
  1552. int idx;
  1553. gd = &data->gates_data[n];
  1554. secondary = 8 * (gd->offset - STM32F4_RCC_AHB1ENR) +
  1555. gd->bit_idx;
  1556. idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  1557. if (idx < 0)
  1558. goto fail;
  1559. clks[idx] = clk_hw_register_gate(
  1560. NULL, gd->name, gd->parent_name, gd->flags,
  1561. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  1562. if (IS_ERR(clks[idx])) {
  1563. pr_err("%pOF: Unable to register leaf clock %s\n",
  1564. np, gd->name);
  1565. goto fail;
  1566. }
  1567. }
  1568. clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
  1569. base + STM32F4_RCC_CSR, 0, 1, 0, &stm32f4_clk_lock);
  1570. if (IS_ERR(clks[CLK_LSI])) {
  1571. pr_err("Unable to register lsi clock\n");
  1572. goto fail;
  1573. }
  1574. clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
  1575. base + STM32F4_RCC_BDCR, 0, 1, 0, &stm32f4_clk_lock);
  1576. if (IS_ERR(clks[CLK_LSE])) {
  1577. pr_err("Unable to register lse clock\n");
  1578. goto fail;
  1579. }
  1580. clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  1581. 0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  1582. &stm32f4_clk_lock);
  1583. if (IS_ERR(clks[CLK_HSE_RTC])) {
  1584. pr_err("Unable to register hse-rtc clock\n");
  1585. goto fail;
  1586. }
  1587. clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  1588. base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  1589. if (IS_ERR(clks[CLK_RTC])) {
  1590. pr_err("Unable to register rtc clock\n");
  1591. goto fail;
  1592. }
  1593. for (n = 0; n < data->aux_clk_num; n++) {
  1594. const struct stm32_aux_clk *aux_clk;
  1595. struct clk_hw *hw;
  1596. aux_clk = &data->aux_clk[n];
  1597. hw = stm32_register_aux_clk(aux_clk->name,
  1598. aux_clk->parent_names, aux_clk->num_parents,
  1599. aux_clk->offset_mux, aux_clk->shift,
  1600. aux_clk->mask, aux_clk->offset_gate,
  1601. aux_clk->bit_idx, aux_clk->flags,
  1602. &stm32f4_clk_lock);
  1603. if (IS_ERR(hw)) {
  1604. pr_warn("Unable to register %s clk\n", aux_clk->name);
  1605. continue;
  1606. }
  1607. if (aux_clk->idx != NO_IDX)
  1608. clks[aux_clk->idx] = hw;
  1609. }
  1610. if (of_device_is_compatible(np, "st,stm32f746-rcc")) {
  1611. clk_hw_register_fixed_factor(NULL, "hsi_div488", "hsi", 0,
  1612. 1, 488);
  1613. clks[CLK_PLL_SRC] = pll_src_hw;
  1614. }
  1615. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  1616. return;
  1617. fail:
  1618. kfree(clks);
  1619. iounmap(base);
  1620. }
  1621. CLK_OF_DECLARE_DRIVER(stm32f42xx_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);
  1622. CLK_OF_DECLARE_DRIVER(stm32f46xx_rcc, "st,stm32f469-rcc", stm32f4_rcc_init);
  1623. CLK_OF_DECLARE_DRIVER(stm32f746_rcc, "st,stm32f746-rcc", stm32f4_rcc_init);
  1624. CLK_OF_DECLARE_DRIVER(stm32f769_rcc, "st,stm32f769-rcc", stm32f4_rcc_init);