clk-stm32f4.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 stm32f4_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. enum { SYSTICK, FCLK, CLK_LSI, CLK_LSE, CLK_HSE_RTC, CLK_RTC, END_PRIMARY_CLK };
  121. /*
  122. * MAX_CLKS is the maximum value in the enumeration below plus the combined
  123. * hweight of stm32f42xx_gate_map (plus one).
  124. */
  125. #define MAX_CLKS (71 + END_PRIMARY_CLK + 1)
  126. /*
  127. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  128. * have gate bits associated with them. Its combined hweight is 71.
  129. */
  130. static const u64 stm32f42xx_gate_map[] = { 0x000000f17ef417ffull,
  131. 0x0000000000000001ull,
  132. 0x04777f33f6fec9ffull };
  133. static struct clk_hw *clks[MAX_CLKS];
  134. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  135. static void __iomem *base;
  136. static struct regmap *pdrm;
  137. /*
  138. * "Multiplier" device for APBx clocks.
  139. *
  140. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  141. * mode, they also tap out the one of the low order state bits to run the
  142. * timers. ST datasheets represent this feature as a (conditional) clock
  143. * multiplier.
  144. */
  145. struct clk_apb_mul {
  146. struct clk_hw hw;
  147. u8 bit_idx;
  148. };
  149. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  150. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  151. unsigned long parent_rate)
  152. {
  153. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  154. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  155. return parent_rate * 2;
  156. return parent_rate;
  157. }
  158. static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  159. unsigned long *prate)
  160. {
  161. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  162. unsigned long mult = 1;
  163. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  164. mult = 2;
  165. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  166. unsigned long best_parent = rate / mult;
  167. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  168. }
  169. return *prate * mult;
  170. }
  171. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  172. unsigned long parent_rate)
  173. {
  174. /*
  175. * We must report success but we can do so unconditionally because
  176. * clk_apb_mul_round_rate returns values that ensure this call is a
  177. * nop.
  178. */
  179. return 0;
  180. }
  181. static const struct clk_ops clk_apb_mul_factor_ops = {
  182. .round_rate = clk_apb_mul_round_rate,
  183. .set_rate = clk_apb_mul_set_rate,
  184. .recalc_rate = clk_apb_mul_recalc_rate,
  185. };
  186. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  187. const char *parent_name,
  188. unsigned long flags, u8 bit_idx)
  189. {
  190. struct clk_apb_mul *am;
  191. struct clk_init_data init;
  192. struct clk *clk;
  193. am = kzalloc(sizeof(*am), GFP_KERNEL);
  194. if (!am)
  195. return ERR_PTR(-ENOMEM);
  196. am->bit_idx = bit_idx;
  197. am->hw.init = &init;
  198. init.name = name;
  199. init.ops = &clk_apb_mul_factor_ops;
  200. init.flags = flags;
  201. init.parent_names = &parent_name;
  202. init.num_parents = 1;
  203. clk = clk_register(dev, &am->hw);
  204. if (IS_ERR(clk))
  205. kfree(am);
  206. return clk;
  207. }
  208. /*
  209. * Decode current PLL state and (statically) model the state we inherit from
  210. * the bootloader.
  211. */
  212. static void stm32f4_rcc_register_pll(const char *hse_clk, const char *hsi_clk)
  213. {
  214. unsigned long pllcfgr = readl(base + STM32F4_RCC_PLLCFGR);
  215. unsigned long pllm = pllcfgr & 0x3f;
  216. unsigned long plln = (pllcfgr >> 6) & 0x1ff;
  217. unsigned long pllp = BIT(((pllcfgr >> 16) & 3) + 1);
  218. const char *pllsrc = pllcfgr & BIT(22) ? hse_clk : hsi_clk;
  219. unsigned long pllq = (pllcfgr >> 24) & 0xf;
  220. clk_register_fixed_factor(NULL, "vco", pllsrc, 0, plln, pllm);
  221. clk_register_fixed_factor(NULL, "pll", "vco", 0, 1, pllp);
  222. clk_register_fixed_factor(NULL, "pll48", "vco", 0, 1, pllq);
  223. }
  224. /*
  225. * Converts the primary and secondary indices (as they appear in DT) to an
  226. * offset into our struct clock array.
  227. */
  228. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  229. {
  230. u64 table[ARRAY_SIZE(stm32f42xx_gate_map)];
  231. if (primary == 1) {
  232. if (WARN_ON(secondary >= END_PRIMARY_CLK))
  233. return -EINVAL;
  234. return secondary;
  235. }
  236. memcpy(table, stm32f42xx_gate_map, sizeof(table));
  237. /* only bits set in table can be used as indices */
  238. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  239. 0 == (table[BIT_ULL_WORD(secondary)] &
  240. BIT_ULL_MASK(secondary))))
  241. return -EINVAL;
  242. /* mask out bits above our current index */
  243. table[BIT_ULL_WORD(secondary)] &=
  244. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  245. return END_PRIMARY_CLK - 1 + hweight64(table[0]) +
  246. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  247. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  248. }
  249. static struct clk_hw *
  250. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  251. {
  252. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  253. if (i < 0)
  254. return ERR_PTR(-EINVAL);
  255. return clks[i];
  256. }
  257. #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  258. static inline void disable_power_domain_write_protection(void)
  259. {
  260. if (pdrm)
  261. regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  262. }
  263. static inline void enable_power_domain_write_protection(void)
  264. {
  265. if (pdrm)
  266. regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  267. }
  268. static inline void sofware_reset_backup_domain(void)
  269. {
  270. unsigned long val;
  271. val = readl(base + STM32F4_RCC_BDCR);
  272. writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  273. writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  274. }
  275. struct stm32_rgate {
  276. struct clk_gate gate;
  277. u8 bit_rdy_idx;
  278. };
  279. #define RTC_TIMEOUT 1000000
  280. static int rgclk_enable(struct clk_hw *hw)
  281. {
  282. struct clk_gate *gate = to_clk_gate(hw);
  283. struct stm32_rgate *rgate = to_rgclk(gate);
  284. u32 reg;
  285. int ret;
  286. disable_power_domain_write_protection();
  287. clk_gate_ops.enable(hw);
  288. ret = readl_relaxed_poll_timeout_atomic(gate->reg, reg,
  289. reg & rgate->bit_rdy_idx, 1000, RTC_TIMEOUT);
  290. enable_power_domain_write_protection();
  291. return ret;
  292. }
  293. static void rgclk_disable(struct clk_hw *hw)
  294. {
  295. clk_gate_ops.disable(hw);
  296. }
  297. static int rgclk_is_enabled(struct clk_hw *hw)
  298. {
  299. return clk_gate_ops.is_enabled(hw);
  300. }
  301. static const struct clk_ops rgclk_ops = {
  302. .enable = rgclk_enable,
  303. .disable = rgclk_disable,
  304. .is_enabled = rgclk_is_enabled,
  305. };
  306. static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  307. const char *parent_name, unsigned long flags,
  308. void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  309. u8 clk_gate_flags, spinlock_t *lock)
  310. {
  311. struct stm32_rgate *rgate;
  312. struct clk_init_data init = { NULL };
  313. struct clk_hw *hw;
  314. int ret;
  315. rgate = kzalloc(sizeof(*rgate), GFP_KERNEL);
  316. if (!rgate)
  317. return ERR_PTR(-ENOMEM);
  318. init.name = name;
  319. init.ops = &rgclk_ops;
  320. init.flags = flags;
  321. init.parent_names = &parent_name;
  322. init.num_parents = 1;
  323. rgate->bit_rdy_idx = bit_rdy_idx;
  324. rgate->gate.lock = lock;
  325. rgate->gate.reg = reg;
  326. rgate->gate.bit_idx = bit_idx;
  327. rgate->gate.hw.init = &init;
  328. hw = &rgate->gate.hw;
  329. ret = clk_hw_register(dev, hw);
  330. if (ret) {
  331. kfree(rgate);
  332. hw = ERR_PTR(ret);
  333. }
  334. return hw;
  335. }
  336. static int cclk_gate_enable(struct clk_hw *hw)
  337. {
  338. int ret;
  339. disable_power_domain_write_protection();
  340. ret = clk_gate_ops.enable(hw);
  341. enable_power_domain_write_protection();
  342. return ret;
  343. }
  344. static void cclk_gate_disable(struct clk_hw *hw)
  345. {
  346. disable_power_domain_write_protection();
  347. clk_gate_ops.disable(hw);
  348. enable_power_domain_write_protection();
  349. }
  350. static int cclk_gate_is_enabled(struct clk_hw *hw)
  351. {
  352. return clk_gate_ops.is_enabled(hw);
  353. }
  354. static const struct clk_ops cclk_gate_ops = {
  355. .enable = cclk_gate_enable,
  356. .disable = cclk_gate_disable,
  357. .is_enabled = cclk_gate_is_enabled,
  358. };
  359. static u8 cclk_mux_get_parent(struct clk_hw *hw)
  360. {
  361. return clk_mux_ops.get_parent(hw);
  362. }
  363. static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  364. {
  365. int ret;
  366. disable_power_domain_write_protection();
  367. sofware_reset_backup_domain();
  368. ret = clk_mux_ops.set_parent(hw, index);
  369. enable_power_domain_write_protection();
  370. return ret;
  371. }
  372. static const struct clk_ops cclk_mux_ops = {
  373. .get_parent = cclk_mux_get_parent,
  374. .set_parent = cclk_mux_set_parent,
  375. };
  376. static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  377. const char * const *parent_names, int num_parents,
  378. void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  379. spinlock_t *lock)
  380. {
  381. struct clk_hw *hw;
  382. struct clk_gate *gate;
  383. struct clk_mux *mux;
  384. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  385. if (!gate) {
  386. hw = ERR_PTR(-EINVAL);
  387. goto fail;
  388. }
  389. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  390. if (!mux) {
  391. kfree(gate);
  392. hw = ERR_PTR(-EINVAL);
  393. goto fail;
  394. }
  395. gate->reg = reg;
  396. gate->bit_idx = bit_idx;
  397. gate->flags = 0;
  398. gate->lock = lock;
  399. mux->reg = reg;
  400. mux->shift = shift;
  401. mux->mask = 3;
  402. mux->flags = 0;
  403. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  404. &mux->hw, &cclk_mux_ops,
  405. NULL, NULL,
  406. &gate->hw, &cclk_gate_ops,
  407. flags);
  408. if (IS_ERR(hw)) {
  409. kfree(gate);
  410. kfree(mux);
  411. }
  412. fail:
  413. return hw;
  414. }
  415. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  416. static const struct clk_div_table ahb_div_table[] = {
  417. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  418. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  419. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  420. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  421. { 0 },
  422. };
  423. static const struct clk_div_table apb_div_table[] = {
  424. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  425. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  426. { 0 },
  427. };
  428. static const char *rtc_parents[4] = {
  429. "no-clock", "lse", "lsi", "hse-rtc"
  430. };
  431. static void __init stm32f4_rcc_init(struct device_node *np)
  432. {
  433. const char *hse_clk;
  434. int n;
  435. base = of_iomap(np, 0);
  436. if (!base) {
  437. pr_err("%s: unable to map resource", np->name);
  438. return;
  439. }
  440. pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  441. if (IS_ERR(pdrm)) {
  442. pdrm = NULL;
  443. pr_warn("%s: Unable to get syscfg\n", __func__);
  444. }
  445. hse_clk = of_clk_get_parent_name(np, 0);
  446. clk_register_fixed_rate_with_accuracy(NULL, "hsi", NULL, 0,
  447. 16000000, 160000);
  448. stm32f4_rcc_register_pll(hse_clk, "hsi");
  449. sys_parents[1] = hse_clk;
  450. clk_register_mux_table(
  451. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  452. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  453. clk_register_divider_table(NULL, "ahb_div", "sys",
  454. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  455. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  456. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  457. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  458. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  459. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  460. CLK_SET_RATE_PARENT, 12);
  461. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  462. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  463. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  464. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  465. CLK_SET_RATE_PARENT, 15);
  466. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  467. 0, 1, 8);
  468. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  469. 0, 1, 1);
  470. for (n = 0; n < ARRAY_SIZE(stm32f4_gates); n++) {
  471. const struct stm32f4_gate_data *gd = &stm32f4_gates[n];
  472. unsigned int secondary =
  473. 8 * (gd->offset - STM32F4_RCC_AHB1ENR) + gd->bit_idx;
  474. int idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  475. if (idx < 0)
  476. goto fail;
  477. clks[idx] = clk_hw_register_gate(
  478. NULL, gd->name, gd->parent_name, gd->flags,
  479. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  480. if (IS_ERR(clks[idx])) {
  481. pr_err("%s: Unable to register leaf clock %s\n",
  482. np->full_name, gd->name);
  483. goto fail;
  484. }
  485. }
  486. clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
  487. base + STM32F4_RCC_CSR, 0, 2, 0, &stm32f4_clk_lock);
  488. if (IS_ERR(clks[CLK_LSI])) {
  489. pr_err("Unable to register lsi clock\n");
  490. goto fail;
  491. }
  492. clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
  493. base + STM32F4_RCC_BDCR, 0, 2, 0, &stm32f4_clk_lock);
  494. if (IS_ERR(clks[CLK_LSE])) {
  495. pr_err("Unable to register lse clock\n");
  496. goto fail;
  497. }
  498. clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  499. 0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  500. &stm32f4_clk_lock);
  501. if (IS_ERR(clks[CLK_HSE_RTC])) {
  502. pr_err("Unable to register hse-rtc clock\n");
  503. goto fail;
  504. }
  505. clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  506. base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  507. if (IS_ERR(clks[CLK_RTC])) {
  508. pr_err("Unable to register rtc clock\n");
  509. goto fail;
  510. }
  511. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  512. return;
  513. fail:
  514. iounmap(base);
  515. }
  516. CLK_OF_DECLARE(stm32f4_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);