clk-stm32f4.c 42 KB

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