clk-ppc-corenet.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * clock driver for Freescale PowerPC corenet SoCs.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/of.h>
  17. #include <linux/slab.h>
  18. struct cmux_clk {
  19. struct clk_hw hw;
  20. void __iomem *reg;
  21. u32 flags;
  22. };
  23. #define PLL_KILL BIT(31)
  24. #define CLKSEL_SHIFT 27
  25. #define CLKSEL_ADJUST BIT(0)
  26. #define to_cmux_clk(p) container_of(p, struct cmux_clk, hw)
  27. static unsigned int clocks_per_pll;
  28. static int cmux_set_parent(struct clk_hw *hw, u8 idx)
  29. {
  30. struct cmux_clk *clk = to_cmux_clk(hw);
  31. u32 clksel;
  32. clksel = ((idx / clocks_per_pll) << 2) + idx % clocks_per_pll;
  33. if (clk->flags & CLKSEL_ADJUST)
  34. clksel += 8;
  35. clksel = (clksel & 0xf) << CLKSEL_SHIFT;
  36. iowrite32be(clksel, clk->reg);
  37. return 0;
  38. }
  39. static u8 cmux_get_parent(struct clk_hw *hw)
  40. {
  41. struct cmux_clk *clk = to_cmux_clk(hw);
  42. u32 clksel;
  43. clksel = ioread32be(clk->reg);
  44. clksel = (clksel >> CLKSEL_SHIFT) & 0xf;
  45. if (clk->flags & CLKSEL_ADJUST)
  46. clksel -= 8;
  47. clksel = (clksel >> 2) * clocks_per_pll + clksel % 4;
  48. return clksel;
  49. }
  50. const struct clk_ops cmux_ops = {
  51. .get_parent = cmux_get_parent,
  52. .set_parent = cmux_set_parent,
  53. };
  54. static void __init core_mux_init(struct device_node *np)
  55. {
  56. struct clk *clk;
  57. struct clk_init_data init;
  58. struct cmux_clk *cmux_clk;
  59. struct device_node *node;
  60. int rc, count, i;
  61. u32 offset;
  62. const char *clk_name;
  63. const char **parent_names;
  64. rc = of_property_read_u32(np, "reg", &offset);
  65. if (rc) {
  66. pr_err("%s: could not get reg property\n", np->name);
  67. return;
  68. }
  69. /* get the input clock source count */
  70. count = of_property_count_strings(np, "clock-names");
  71. if (count < 0) {
  72. pr_err("%s: get clock count error\n", np->name);
  73. return;
  74. }
  75. parent_names = kzalloc((sizeof(char *) * count), GFP_KERNEL);
  76. if (!parent_names) {
  77. pr_err("%s: could not allocate parent_names\n", __func__);
  78. return;
  79. }
  80. for (i = 0; i < count; i++)
  81. parent_names[i] = of_clk_get_parent_name(np, i);
  82. cmux_clk = kzalloc(sizeof(struct cmux_clk), GFP_KERNEL);
  83. if (!cmux_clk) {
  84. pr_err("%s: could not allocate cmux_clk\n", __func__);
  85. goto err_name;
  86. }
  87. cmux_clk->reg = of_iomap(np, 0);
  88. if (!cmux_clk->reg) {
  89. pr_err("%s: could not map register\n", __func__);
  90. goto err_clk;
  91. }
  92. node = of_find_compatible_node(NULL, NULL, "fsl,p4080-clockgen");
  93. if (node && (offset >= 0x80))
  94. cmux_clk->flags = CLKSEL_ADJUST;
  95. rc = of_property_read_string_index(np, "clock-output-names",
  96. 0, &clk_name);
  97. if (rc) {
  98. pr_err("%s: read clock names error\n", np->name);
  99. goto err_clk;
  100. }
  101. init.name = clk_name;
  102. init.ops = &cmux_ops;
  103. init.parent_names = parent_names;
  104. init.num_parents = count;
  105. init.flags = 0;
  106. cmux_clk->hw.init = &init;
  107. clk = clk_register(NULL, &cmux_clk->hw);
  108. if (IS_ERR(clk)) {
  109. pr_err("%s: could not register clock\n", clk_name);
  110. goto err_clk;
  111. }
  112. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  113. if (rc) {
  114. pr_err("Could not register clock provider for node:%s\n",
  115. np->name);
  116. goto err_clk;
  117. }
  118. goto err_name;
  119. err_clk:
  120. kfree(cmux_clk);
  121. err_name:
  122. /* free *_names because they are reallocated when registered */
  123. kfree(parent_names);
  124. }
  125. static void __init core_pll_init(struct device_node *np)
  126. {
  127. u32 mult;
  128. int i, rc, count;
  129. const char *clk_name, *parent_name;
  130. struct clk_onecell_data *onecell_data;
  131. struct clk **subclks;
  132. void __iomem *base;
  133. base = of_iomap(np, 0);
  134. if (!base) {
  135. pr_err("clk-ppc: iomap error\n");
  136. return;
  137. }
  138. /* get the multiple of PLL */
  139. mult = ioread32be(base);
  140. /* check if this PLL is disabled */
  141. if (mult & PLL_KILL) {
  142. pr_debug("PLL:%s is disabled\n", np->name);
  143. goto err_map;
  144. }
  145. mult = (mult >> 1) & 0x3f;
  146. parent_name = of_clk_get_parent_name(np, 0);
  147. if (!parent_name) {
  148. pr_err("PLL: %s must have a parent\n", np->name);
  149. goto err_map;
  150. }
  151. count = of_property_count_strings(np, "clock-output-names");
  152. if (count < 0 || count > 4) {
  153. pr_err("%s: clock is not supported\n", np->name);
  154. goto err_map;
  155. }
  156. /* output clock number per PLL */
  157. clocks_per_pll = count;
  158. subclks = kzalloc(sizeof(struct clk *) * count, GFP_KERNEL);
  159. if (!subclks) {
  160. pr_err("%s: could not allocate subclks\n", __func__);
  161. goto err_map;
  162. }
  163. onecell_data = kzalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  164. if (!onecell_data) {
  165. pr_err("%s: could not allocate onecell_data\n", __func__);
  166. goto err_clks;
  167. }
  168. for (i = 0; i < count; i++) {
  169. rc = of_property_read_string_index(np, "clock-output-names",
  170. i, &clk_name);
  171. if (rc) {
  172. pr_err("%s: could not get clock names\n", np->name);
  173. goto err_cell;
  174. }
  175. /*
  176. * when count == 4, there are 4 output clocks:
  177. * /1, /2, /3, /4 respectively
  178. * when count < 4, there are at least 2 output clocks:
  179. * /1, /2, (/4, if count == 3) respectively.
  180. */
  181. if (count == 4)
  182. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  183. parent_name, 0, mult, 1 + i);
  184. else
  185. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  186. parent_name, 0, mult, 1 << i);
  187. if (IS_ERR(subclks[i])) {
  188. pr_err("%s: could not register clock\n", clk_name);
  189. goto err_cell;
  190. }
  191. }
  192. onecell_data->clks = subclks;
  193. onecell_data->clk_num = count;
  194. rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
  195. if (rc) {
  196. pr_err("Could not register clk provider for node:%s\n",
  197. np->name);
  198. goto err_cell;
  199. }
  200. iounmap(base);
  201. return;
  202. err_cell:
  203. kfree(onecell_data);
  204. err_clks:
  205. kfree(subclks);
  206. err_map:
  207. iounmap(base);
  208. }
  209. static void __init sysclk_init(struct device_node *node)
  210. {
  211. struct clk *clk;
  212. const char *clk_name = node->name;
  213. struct device_node *np = of_get_parent(node);
  214. u32 rate;
  215. if (!np) {
  216. pr_err("ppc-clk: could not get parent node\n");
  217. return;
  218. }
  219. if (of_property_read_u32(np, "clock-frequency", &rate)) {
  220. of_node_put(node);
  221. return;
  222. }
  223. of_property_read_string(np, "clock-output-names", &clk_name);
  224. clk = clk_register_fixed_rate(NULL, clk_name, NULL, CLK_IS_ROOT, rate);
  225. if (!IS_ERR(clk))
  226. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  227. }
  228. static const struct of_device_id clk_match[] __initconst = {
  229. { .compatible = "fsl,qoriq-sysclk-1.0", .data = sysclk_init, },
  230. { .compatible = "fsl,qoriq-sysclk-2.0", .data = sysclk_init, },
  231. { .compatible = "fsl,qoriq-core-pll-1.0", .data = core_pll_init, },
  232. { .compatible = "fsl,qoriq-core-pll-2.0", .data = core_pll_init, },
  233. { .compatible = "fsl,qoriq-core-mux-1.0", .data = core_mux_init, },
  234. { .compatible = "fsl,qoriq-core-mux-2.0", .data = core_mux_init, },
  235. {}
  236. };
  237. static int __init ppc_corenet_clk_probe(struct platform_device *pdev)
  238. {
  239. of_clk_init(clk_match);
  240. return 0;
  241. }
  242. static const struct of_device_id ppc_clk_ids[] __initconst = {
  243. { .compatible = "fsl,qoriq-clockgen-1.0", },
  244. { .compatible = "fsl,qoriq-clockgen-2.0", },
  245. {}
  246. };
  247. static struct platform_driver ppc_corenet_clk_driver = {
  248. .driver = {
  249. .name = "ppc_corenet_clock",
  250. .owner = THIS_MODULE,
  251. .of_match_table = ppc_clk_ids,
  252. },
  253. .probe = ppc_corenet_clk_probe,
  254. };
  255. static int __init ppc_corenet_clk_init(void)
  256. {
  257. return platform_driver_register(&ppc_corenet_clk_driver);
  258. }
  259. subsys_initcall(ppc_corenet_clk_init);