clk-stm32f4.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #define STM32F4_RCC_PLLCFGR 0x04
  30. #define STM32F4_RCC_CFGR 0x08
  31. #define STM32F4_RCC_AHB1ENR 0x30
  32. #define STM32F4_RCC_AHB2ENR 0x34
  33. #define STM32F4_RCC_AHB3ENR 0x38
  34. #define STM32F4_RCC_APB1ENR 0x40
  35. #define STM32F4_RCC_APB2ENR 0x44
  36. #define STM32F4_RCC_BDCR 0x70
  37. #define STM32F4_RCC_CSR 0x74
  38. struct stm32f4_gate_data {
  39. u8 offset;
  40. u8 bit_idx;
  41. const char *name;
  42. const char *parent_name;
  43. unsigned long flags;
  44. };
  45. static const struct stm32f4_gate_data stm32f429_gates[] __initconst = {
  46. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  47. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  48. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  49. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  50. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  51. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  52. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  53. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  54. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  55. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  56. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  57. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  58. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  59. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  60. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  61. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  62. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  63. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  64. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  65. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  66. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  67. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  68. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  69. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  70. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  71. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  72. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  73. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  74. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  75. CLK_IGNORE_UNUSED },
  76. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  77. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  78. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  79. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  80. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  81. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  82. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  83. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  84. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  85. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  86. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  87. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  88. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  89. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  90. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  91. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  92. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  93. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  94. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  95. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  96. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  97. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  98. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  99. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  100. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  101. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  102. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  103. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  104. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  105. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  106. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  107. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  108. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  109. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  110. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  111. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  112. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  113. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  114. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  115. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  116. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  117. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  118. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  119. };
  120. static const struct stm32f4_gate_data stm32f469_gates[] __initconst = {
  121. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  122. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  123. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  124. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  125. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  126. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  127. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  128. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  129. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  130. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  131. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  132. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  133. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  134. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  135. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  136. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  137. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  138. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  139. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  140. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  141. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  142. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  143. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  144. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  145. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  146. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  147. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  148. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  149. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  150. CLK_IGNORE_UNUSED },
  151. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  152. CLK_IGNORE_UNUSED },
  153. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  154. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  155. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  156. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  157. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  158. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  159. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  160. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  161. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  162. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  163. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  164. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  165. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  166. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  167. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  168. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  169. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  170. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  171. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  172. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  173. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  174. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  175. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  176. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  177. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  178. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  179. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  180. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  181. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  182. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  183. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  184. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  185. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  186. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  187. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  188. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  189. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  190. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  191. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  192. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  193. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  194. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  195. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  196. };
  197. enum { SYSTICK, FCLK, CLK_LSI, CLK_LSE, CLK_HSE_RTC, CLK_RTC, END_PRIMARY_CLK };
  198. /*
  199. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  200. * have gate bits associated with them. Its combined hweight is 71.
  201. */
  202. #define MAX_GATE_MAP 3
  203. static const u64 stm32f42xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  204. 0x0000000000000001ull,
  205. 0x04777f33f6fec9ffull };
  206. static const u64 stm32f46xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  207. 0x0000000000000003ull,
  208. 0x0c777f33f6fec9ffull };
  209. static const u64 *stm32f4_gate_map;
  210. static struct clk_hw **clks;
  211. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  212. static void __iomem *base;
  213. static struct regmap *pdrm;
  214. /*
  215. * "Multiplier" device for APBx clocks.
  216. *
  217. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  218. * mode, they also tap out the one of the low order state bits to run the
  219. * timers. ST datasheets represent this feature as a (conditional) clock
  220. * multiplier.
  221. */
  222. struct clk_apb_mul {
  223. struct clk_hw hw;
  224. u8 bit_idx;
  225. };
  226. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  227. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  228. unsigned long parent_rate)
  229. {
  230. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  231. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  232. return parent_rate * 2;
  233. return parent_rate;
  234. }
  235. static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  236. unsigned long *prate)
  237. {
  238. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  239. unsigned long mult = 1;
  240. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  241. mult = 2;
  242. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  243. unsigned long best_parent = rate / mult;
  244. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  245. }
  246. return *prate * mult;
  247. }
  248. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  249. unsigned long parent_rate)
  250. {
  251. /*
  252. * We must report success but we can do so unconditionally because
  253. * clk_apb_mul_round_rate returns values that ensure this call is a
  254. * nop.
  255. */
  256. return 0;
  257. }
  258. static const struct clk_ops clk_apb_mul_factor_ops = {
  259. .round_rate = clk_apb_mul_round_rate,
  260. .set_rate = clk_apb_mul_set_rate,
  261. .recalc_rate = clk_apb_mul_recalc_rate,
  262. };
  263. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  264. const char *parent_name,
  265. unsigned long flags, u8 bit_idx)
  266. {
  267. struct clk_apb_mul *am;
  268. struct clk_init_data init;
  269. struct clk *clk;
  270. am = kzalloc(sizeof(*am), GFP_KERNEL);
  271. if (!am)
  272. return ERR_PTR(-ENOMEM);
  273. am->bit_idx = bit_idx;
  274. am->hw.init = &init;
  275. init.name = name;
  276. init.ops = &clk_apb_mul_factor_ops;
  277. init.flags = flags;
  278. init.parent_names = &parent_name;
  279. init.num_parents = 1;
  280. clk = clk_register(dev, &am->hw);
  281. if (IS_ERR(clk))
  282. kfree(am);
  283. return clk;
  284. }
  285. /*
  286. * Decode current PLL state and (statically) model the state we inherit from
  287. * the bootloader.
  288. */
  289. static void stm32f4_rcc_register_pll(const char *hse_clk, const char *hsi_clk)
  290. {
  291. unsigned long pllcfgr = readl(base + STM32F4_RCC_PLLCFGR);
  292. unsigned long pllm = pllcfgr & 0x3f;
  293. unsigned long plln = (pllcfgr >> 6) & 0x1ff;
  294. unsigned long pllp = BIT(((pllcfgr >> 16) & 3) + 1);
  295. const char *pllsrc = pllcfgr & BIT(22) ? hse_clk : hsi_clk;
  296. unsigned long pllq = (pllcfgr >> 24) & 0xf;
  297. clk_register_fixed_factor(NULL, "vco", pllsrc, 0, plln, pllm);
  298. clk_register_fixed_factor(NULL, "pll", "vco", 0, 1, pllp);
  299. clk_register_fixed_factor(NULL, "pll48", "vco", 0, 1, pllq);
  300. }
  301. /*
  302. * Converts the primary and secondary indices (as they appear in DT) to an
  303. * offset into our struct clock array.
  304. */
  305. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  306. {
  307. u64 table[MAX_GATE_MAP];
  308. if (primary == 1) {
  309. if (WARN_ON(secondary >= END_PRIMARY_CLK))
  310. return -EINVAL;
  311. return secondary;
  312. }
  313. memcpy(table, stm32f4_gate_map, sizeof(table));
  314. /* only bits set in table can be used as indices */
  315. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  316. 0 == (table[BIT_ULL_WORD(secondary)] &
  317. BIT_ULL_MASK(secondary))))
  318. return -EINVAL;
  319. /* mask out bits above our current index */
  320. table[BIT_ULL_WORD(secondary)] &=
  321. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  322. return END_PRIMARY_CLK - 1 + hweight64(table[0]) +
  323. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  324. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  325. }
  326. static struct clk_hw *
  327. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  328. {
  329. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  330. if (i < 0)
  331. return ERR_PTR(-EINVAL);
  332. return clks[i];
  333. }
  334. #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  335. static inline void disable_power_domain_write_protection(void)
  336. {
  337. if (pdrm)
  338. regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  339. }
  340. static inline void enable_power_domain_write_protection(void)
  341. {
  342. if (pdrm)
  343. regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  344. }
  345. static inline void sofware_reset_backup_domain(void)
  346. {
  347. unsigned long val;
  348. val = readl(base + STM32F4_RCC_BDCR);
  349. writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  350. writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  351. }
  352. struct stm32_rgate {
  353. struct clk_gate gate;
  354. u8 bit_rdy_idx;
  355. };
  356. #define RTC_TIMEOUT 1000000
  357. static int rgclk_enable(struct clk_hw *hw)
  358. {
  359. struct clk_gate *gate = to_clk_gate(hw);
  360. struct stm32_rgate *rgate = to_rgclk(gate);
  361. u32 reg;
  362. int ret;
  363. disable_power_domain_write_protection();
  364. clk_gate_ops.enable(hw);
  365. ret = readl_relaxed_poll_timeout_atomic(gate->reg, reg,
  366. reg & rgate->bit_rdy_idx, 1000, RTC_TIMEOUT);
  367. enable_power_domain_write_protection();
  368. return ret;
  369. }
  370. static void rgclk_disable(struct clk_hw *hw)
  371. {
  372. clk_gate_ops.disable(hw);
  373. }
  374. static int rgclk_is_enabled(struct clk_hw *hw)
  375. {
  376. return clk_gate_ops.is_enabled(hw);
  377. }
  378. static const struct clk_ops rgclk_ops = {
  379. .enable = rgclk_enable,
  380. .disable = rgclk_disable,
  381. .is_enabled = rgclk_is_enabled,
  382. };
  383. static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  384. const char *parent_name, unsigned long flags,
  385. void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  386. u8 clk_gate_flags, spinlock_t *lock)
  387. {
  388. struct stm32_rgate *rgate;
  389. struct clk_init_data init = { NULL };
  390. struct clk_hw *hw;
  391. int ret;
  392. rgate = kzalloc(sizeof(*rgate), GFP_KERNEL);
  393. if (!rgate)
  394. return ERR_PTR(-ENOMEM);
  395. init.name = name;
  396. init.ops = &rgclk_ops;
  397. init.flags = flags;
  398. init.parent_names = &parent_name;
  399. init.num_parents = 1;
  400. rgate->bit_rdy_idx = bit_rdy_idx;
  401. rgate->gate.lock = lock;
  402. rgate->gate.reg = reg;
  403. rgate->gate.bit_idx = bit_idx;
  404. rgate->gate.hw.init = &init;
  405. hw = &rgate->gate.hw;
  406. ret = clk_hw_register(dev, hw);
  407. if (ret) {
  408. kfree(rgate);
  409. hw = ERR_PTR(ret);
  410. }
  411. return hw;
  412. }
  413. static int cclk_gate_enable(struct clk_hw *hw)
  414. {
  415. int ret;
  416. disable_power_domain_write_protection();
  417. ret = clk_gate_ops.enable(hw);
  418. enable_power_domain_write_protection();
  419. return ret;
  420. }
  421. static void cclk_gate_disable(struct clk_hw *hw)
  422. {
  423. disable_power_domain_write_protection();
  424. clk_gate_ops.disable(hw);
  425. enable_power_domain_write_protection();
  426. }
  427. static int cclk_gate_is_enabled(struct clk_hw *hw)
  428. {
  429. return clk_gate_ops.is_enabled(hw);
  430. }
  431. static const struct clk_ops cclk_gate_ops = {
  432. .enable = cclk_gate_enable,
  433. .disable = cclk_gate_disable,
  434. .is_enabled = cclk_gate_is_enabled,
  435. };
  436. static u8 cclk_mux_get_parent(struct clk_hw *hw)
  437. {
  438. return clk_mux_ops.get_parent(hw);
  439. }
  440. static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  441. {
  442. int ret;
  443. disable_power_domain_write_protection();
  444. sofware_reset_backup_domain();
  445. ret = clk_mux_ops.set_parent(hw, index);
  446. enable_power_domain_write_protection();
  447. return ret;
  448. }
  449. static const struct clk_ops cclk_mux_ops = {
  450. .get_parent = cclk_mux_get_parent,
  451. .set_parent = cclk_mux_set_parent,
  452. };
  453. static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  454. const char * const *parent_names, int num_parents,
  455. void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  456. spinlock_t *lock)
  457. {
  458. struct clk_hw *hw;
  459. struct clk_gate *gate;
  460. struct clk_mux *mux;
  461. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  462. if (!gate) {
  463. hw = ERR_PTR(-EINVAL);
  464. goto fail;
  465. }
  466. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  467. if (!mux) {
  468. kfree(gate);
  469. hw = ERR_PTR(-EINVAL);
  470. goto fail;
  471. }
  472. gate->reg = reg;
  473. gate->bit_idx = bit_idx;
  474. gate->flags = 0;
  475. gate->lock = lock;
  476. mux->reg = reg;
  477. mux->shift = shift;
  478. mux->mask = 3;
  479. mux->flags = 0;
  480. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  481. &mux->hw, &cclk_mux_ops,
  482. NULL, NULL,
  483. &gate->hw, &cclk_gate_ops,
  484. flags);
  485. if (IS_ERR(hw)) {
  486. kfree(gate);
  487. kfree(mux);
  488. }
  489. fail:
  490. return hw;
  491. }
  492. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  493. static const struct clk_div_table ahb_div_table[] = {
  494. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  495. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  496. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  497. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  498. { 0 },
  499. };
  500. static const struct clk_div_table apb_div_table[] = {
  501. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  502. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  503. { 0 },
  504. };
  505. static const char *rtc_parents[4] = {
  506. "no-clock", "lse", "lsi", "hse-rtc"
  507. };
  508. struct stm32f4_clk_data {
  509. const struct stm32f4_gate_data *gates_data;
  510. const u64 *gates_map;
  511. int gates_num;
  512. };
  513. static const struct stm32f4_clk_data stm32f429_clk_data = {
  514. .gates_data = stm32f429_gates,
  515. .gates_map = stm32f42xx_gate_map,
  516. .gates_num = ARRAY_SIZE(stm32f429_gates),
  517. };
  518. static const struct stm32f4_clk_data stm32f469_clk_data = {
  519. .gates_data = stm32f469_gates,
  520. .gates_map = stm32f46xx_gate_map,
  521. .gates_num = ARRAY_SIZE(stm32f469_gates),
  522. };
  523. static const struct of_device_id stm32f4_of_match[] = {
  524. {
  525. .compatible = "st,stm32f42xx-rcc",
  526. .data = &stm32f429_clk_data
  527. },
  528. {
  529. .compatible = "st,stm32f469-rcc",
  530. .data = &stm32f469_clk_data
  531. },
  532. {}
  533. };
  534. static void __init stm32f4_rcc_init(struct device_node *np)
  535. {
  536. const char *hse_clk;
  537. int n;
  538. const struct of_device_id *match;
  539. const struct stm32f4_clk_data *data;
  540. base = of_iomap(np, 0);
  541. if (!base) {
  542. pr_err("%s: unable to map resource", np->name);
  543. return;
  544. }
  545. pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  546. if (IS_ERR(pdrm)) {
  547. pdrm = NULL;
  548. pr_warn("%s: Unable to get syscfg\n", __func__);
  549. }
  550. match = of_match_node(stm32f4_of_match, np);
  551. if (WARN_ON(!match))
  552. return;
  553. data = match->data;
  554. clks = kmalloc_array(data->gates_num + END_PRIMARY_CLK,
  555. sizeof(*clks), GFP_KERNEL);
  556. if (!clks)
  557. goto fail;
  558. stm32f4_gate_map = data->gates_map;
  559. hse_clk = of_clk_get_parent_name(np, 0);
  560. clk_register_fixed_rate_with_accuracy(NULL, "hsi", NULL, 0,
  561. 16000000, 160000);
  562. stm32f4_rcc_register_pll(hse_clk, "hsi");
  563. sys_parents[1] = hse_clk;
  564. clk_register_mux_table(
  565. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  566. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  567. clk_register_divider_table(NULL, "ahb_div", "sys",
  568. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  569. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  570. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  571. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  572. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  573. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  574. CLK_SET_RATE_PARENT, 12);
  575. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  576. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  577. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  578. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  579. CLK_SET_RATE_PARENT, 15);
  580. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  581. 0, 1, 8);
  582. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  583. 0, 1, 1);
  584. for (n = 0; n < data->gates_num; n++) {
  585. const struct stm32f4_gate_data *gd;
  586. unsigned int secondary;
  587. int idx;
  588. gd = &data->gates_data[n];
  589. secondary = 8 * (gd->offset - STM32F4_RCC_AHB1ENR) +
  590. gd->bit_idx;
  591. idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  592. if (idx < 0)
  593. goto fail;
  594. clks[idx] = clk_hw_register_gate(
  595. NULL, gd->name, gd->parent_name, gd->flags,
  596. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  597. if (IS_ERR(clks[idx])) {
  598. pr_err("%s: Unable to register leaf clock %s\n",
  599. np->full_name, gd->name);
  600. goto fail;
  601. }
  602. }
  603. clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
  604. base + STM32F4_RCC_CSR, 0, 2, 0, &stm32f4_clk_lock);
  605. if (IS_ERR(clks[CLK_LSI])) {
  606. pr_err("Unable to register lsi clock\n");
  607. goto fail;
  608. }
  609. clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
  610. base + STM32F4_RCC_BDCR, 0, 2, 0, &stm32f4_clk_lock);
  611. if (IS_ERR(clks[CLK_LSE])) {
  612. pr_err("Unable to register lse clock\n");
  613. goto fail;
  614. }
  615. clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  616. 0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  617. &stm32f4_clk_lock);
  618. if (IS_ERR(clks[CLK_HSE_RTC])) {
  619. pr_err("Unable to register hse-rtc clock\n");
  620. goto fail;
  621. }
  622. clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  623. base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  624. if (IS_ERR(clks[CLK_RTC])) {
  625. pr_err("Unable to register rtc clock\n");
  626. goto fail;
  627. }
  628. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  629. return;
  630. fail:
  631. kfree(clks);
  632. iounmap(base);
  633. }
  634. CLK_OF_DECLARE_DRIVER(stm32f42xx_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);
  635. CLK_OF_DECLARE_DRIVER(stm32f46xx_rcc, "st,stm32f469-rcc", stm32f4_rcc_init);