clk-stm32f4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #define STM32F4_RCC_PLLCFGR 0x04
  26. #define STM32F4_RCC_CFGR 0x08
  27. #define STM32F4_RCC_AHB1ENR 0x30
  28. #define STM32F4_RCC_AHB2ENR 0x34
  29. #define STM32F4_RCC_AHB3ENR 0x38
  30. #define STM32F4_RCC_APB1ENR 0x40
  31. #define STM32F4_RCC_APB2ENR 0x44
  32. struct stm32f4_gate_data {
  33. u8 offset;
  34. u8 bit_idx;
  35. const char *name;
  36. const char *parent_name;
  37. unsigned long flags;
  38. };
  39. static const struct stm32f4_gate_data stm32f4_gates[] __initconst = {
  40. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  41. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  42. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  43. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  44. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  45. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  46. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  47. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  48. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  49. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  50. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  51. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  52. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  53. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  54. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  55. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  56. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  57. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  58. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  59. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  60. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  61. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  62. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  63. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  64. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  65. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  66. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  67. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  68. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  69. CLK_IGNORE_UNUSED },
  70. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  71. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  72. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  73. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  74. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  75. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  76. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  77. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  78. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  79. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  80. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  81. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  82. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  83. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  84. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  85. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  86. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  87. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  88. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  89. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  90. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  91. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  92. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  93. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  94. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  95. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  96. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  97. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  98. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  99. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  100. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  101. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  102. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  103. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  104. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  105. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  106. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  107. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  108. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  109. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  110. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  111. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  112. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  113. };
  114. /*
  115. * MAX_CLKS is the maximum value in the enumeration below plus the combined
  116. * hweight of stm32f42xx_gate_map (plus one).
  117. */
  118. #define MAX_CLKS 74
  119. enum { SYSTICK, FCLK };
  120. /*
  121. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  122. * have gate bits associated with them. Its combined hweight is 71.
  123. */
  124. static const u64 stm32f42xx_gate_map[] = { 0x000000f17ef417ffull,
  125. 0x0000000000000001ull,
  126. 0x04777f33f6fec9ffull };
  127. static struct clk_hw *clks[MAX_CLKS];
  128. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  129. static void __iomem *base;
  130. /*
  131. * "Multiplier" device for APBx clocks.
  132. *
  133. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  134. * mode, they also tap out the one of the low order state bits to run the
  135. * timers. ST datasheets represent this feature as a (conditional) clock
  136. * multiplier.
  137. */
  138. struct clk_apb_mul {
  139. struct clk_hw hw;
  140. u8 bit_idx;
  141. };
  142. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  143. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  144. unsigned long parent_rate)
  145. {
  146. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  147. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  148. return parent_rate * 2;
  149. return parent_rate;
  150. }
  151. static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  152. unsigned long *prate)
  153. {
  154. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  155. unsigned long mult = 1;
  156. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  157. mult = 2;
  158. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  159. unsigned long best_parent = rate / mult;
  160. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  161. }
  162. return *prate * mult;
  163. }
  164. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  165. unsigned long parent_rate)
  166. {
  167. /*
  168. * We must report success but we can do so unconditionally because
  169. * clk_apb_mul_round_rate returns values that ensure this call is a
  170. * nop.
  171. */
  172. return 0;
  173. }
  174. static const struct clk_ops clk_apb_mul_factor_ops = {
  175. .round_rate = clk_apb_mul_round_rate,
  176. .set_rate = clk_apb_mul_set_rate,
  177. .recalc_rate = clk_apb_mul_recalc_rate,
  178. };
  179. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  180. const char *parent_name,
  181. unsigned long flags, u8 bit_idx)
  182. {
  183. struct clk_apb_mul *am;
  184. struct clk_init_data init;
  185. struct clk *clk;
  186. am = kzalloc(sizeof(*am), GFP_KERNEL);
  187. if (!am)
  188. return ERR_PTR(-ENOMEM);
  189. am->bit_idx = bit_idx;
  190. am->hw.init = &init;
  191. init.name = name;
  192. init.ops = &clk_apb_mul_factor_ops;
  193. init.flags = flags;
  194. init.parent_names = &parent_name;
  195. init.num_parents = 1;
  196. clk = clk_register(dev, &am->hw);
  197. if (IS_ERR(clk))
  198. kfree(am);
  199. return clk;
  200. }
  201. /*
  202. * Decode current PLL state and (statically) model the state we inherit from
  203. * the bootloader.
  204. */
  205. static void stm32f4_rcc_register_pll(const char *hse_clk, const char *hsi_clk)
  206. {
  207. unsigned long pllcfgr = readl(base + STM32F4_RCC_PLLCFGR);
  208. unsigned long pllm = pllcfgr & 0x3f;
  209. unsigned long plln = (pllcfgr >> 6) & 0x1ff;
  210. unsigned long pllp = BIT(((pllcfgr >> 16) & 3) + 1);
  211. const char *pllsrc = pllcfgr & BIT(22) ? hse_clk : hsi_clk;
  212. unsigned long pllq = (pllcfgr >> 24) & 0xf;
  213. clk_register_fixed_factor(NULL, "vco", pllsrc, 0, plln, pllm);
  214. clk_register_fixed_factor(NULL, "pll", "vco", 0, 1, pllp);
  215. clk_register_fixed_factor(NULL, "pll48", "vco", 0, 1, pllq);
  216. }
  217. /*
  218. * Converts the primary and secondary indices (as they appear in DT) to an
  219. * offset into our struct clock array.
  220. */
  221. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  222. {
  223. u64 table[ARRAY_SIZE(stm32f42xx_gate_map)];
  224. if (primary == 1) {
  225. if (WARN_ON(secondary > FCLK))
  226. return -EINVAL;
  227. return secondary;
  228. }
  229. memcpy(table, stm32f42xx_gate_map, sizeof(table));
  230. /* only bits set in table can be used as indices */
  231. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  232. 0 == (table[BIT_ULL_WORD(secondary)] &
  233. BIT_ULL_MASK(secondary))))
  234. return -EINVAL;
  235. /* mask out bits above our current index */
  236. table[BIT_ULL_WORD(secondary)] &=
  237. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  238. return FCLK + hweight64(table[0]) +
  239. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  240. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  241. }
  242. static struct clk_hw *
  243. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  244. {
  245. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  246. if (i < 0)
  247. return ERR_PTR(-EINVAL);
  248. return clks[i];
  249. }
  250. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  251. static const struct clk_div_table ahb_div_table[] = {
  252. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  253. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  254. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  255. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  256. { 0 },
  257. };
  258. static const struct clk_div_table apb_div_table[] = {
  259. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  260. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  261. { 0 },
  262. };
  263. static void __init stm32f4_rcc_init(struct device_node *np)
  264. {
  265. const char *hse_clk;
  266. int n;
  267. base = of_iomap(np, 0);
  268. if (!base) {
  269. pr_err("%s: unable to map resource", np->name);
  270. return;
  271. }
  272. hse_clk = of_clk_get_parent_name(np, 0);
  273. clk_register_fixed_rate_with_accuracy(NULL, "hsi", NULL, 0,
  274. 16000000, 160000);
  275. stm32f4_rcc_register_pll(hse_clk, "hsi");
  276. sys_parents[1] = hse_clk;
  277. clk_register_mux_table(
  278. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  279. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  280. clk_register_divider_table(NULL, "ahb_div", "sys",
  281. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  282. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  283. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  284. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  285. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  286. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  287. CLK_SET_RATE_PARENT, 12);
  288. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  289. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  290. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  291. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  292. CLK_SET_RATE_PARENT, 15);
  293. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  294. 0, 1, 8);
  295. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  296. 0, 1, 1);
  297. for (n = 0; n < ARRAY_SIZE(stm32f4_gates); n++) {
  298. const struct stm32f4_gate_data *gd = &stm32f4_gates[n];
  299. unsigned int secondary =
  300. 8 * (gd->offset - STM32F4_RCC_AHB1ENR) + gd->bit_idx;
  301. int idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  302. if (idx < 0)
  303. goto fail;
  304. clks[idx] = clk_hw_register_gate(
  305. NULL, gd->name, gd->parent_name, gd->flags,
  306. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  307. if (IS_ERR(clks[idx])) {
  308. pr_err("%s: Unable to register leaf clock %s\n",
  309. np->full_name, gd->name);
  310. goto fail;
  311. }
  312. }
  313. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  314. return;
  315. fail:
  316. iounmap(base);
  317. }
  318. CLK_OF_DECLARE(stm32f4_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);