clk-ppc-corenet.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_platform.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. struct cmux_clk {
  18. struct clk_hw hw;
  19. void __iomem *reg;
  20. u32 flags;
  21. };
  22. #define PLL_KILL BIT(31)
  23. #define CLKSEL_SHIFT 27
  24. #define CLKSEL_ADJUST BIT(0)
  25. #define to_cmux_clk(p) container_of(p, struct cmux_clk, hw)
  26. static void __iomem *base;
  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 = base + offset;
  88. node = of_find_compatible_node(NULL, NULL, "fsl,p4080-clockgen");
  89. if (node && (offset >= 0x80))
  90. cmux_clk->flags = CLKSEL_ADJUST;
  91. rc = of_property_read_string_index(np, "clock-output-names",
  92. 0, &clk_name);
  93. if (rc) {
  94. pr_err("%s: read clock names error\n", np->name);
  95. goto err_clk;
  96. }
  97. init.name = clk_name;
  98. init.ops = &cmux_ops;
  99. init.parent_names = parent_names;
  100. init.num_parents = count;
  101. init.flags = 0;
  102. cmux_clk->hw.init = &init;
  103. clk = clk_register(NULL, &cmux_clk->hw);
  104. if (IS_ERR(clk)) {
  105. pr_err("%s: could not register clock\n", clk_name);
  106. goto err_clk;
  107. }
  108. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  109. if (rc) {
  110. pr_err("Could not register clock provider for node:%s\n",
  111. np->name);
  112. goto err_clk;
  113. }
  114. goto err_name;
  115. err_clk:
  116. kfree(cmux_clk);
  117. err_name:
  118. /* free *_names because they are reallocated when registered */
  119. kfree(parent_names);
  120. }
  121. static void __init core_pll_init(struct device_node *np)
  122. {
  123. u32 offset, mult;
  124. int i, rc, count;
  125. const char *clk_name, *parent_name;
  126. struct clk_onecell_data *onecell_data;
  127. struct clk **subclks;
  128. rc = of_property_read_u32(np, "reg", &offset);
  129. if (rc) {
  130. pr_err("%s: could not get reg property\n", np->name);
  131. return;
  132. }
  133. /* get the multiple of PLL */
  134. mult = ioread32be(base + offset);
  135. /* check if this PLL is disabled */
  136. if (mult & PLL_KILL) {
  137. pr_debug("PLL:%s is disabled\n", np->name);
  138. return;
  139. }
  140. mult = (mult >> 1) & 0x3f;
  141. parent_name = of_clk_get_parent_name(np, 0);
  142. if (!parent_name) {
  143. pr_err("PLL: %s must have a parent\n", np->name);
  144. return;
  145. }
  146. count = of_property_count_strings(np, "clock-output-names");
  147. if (count < 0 || count > 4) {
  148. pr_err("%s: clock is not supported\n", np->name);
  149. return;
  150. }
  151. /* output clock number per PLL */
  152. clocks_per_pll = count;
  153. subclks = kzalloc(sizeof(struct clk *) * count, GFP_KERNEL);
  154. if (!subclks) {
  155. pr_err("%s: could not allocate subclks\n", __func__);
  156. return;
  157. }
  158. onecell_data = kzalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  159. if (!onecell_data) {
  160. pr_err("%s: could not allocate onecell_data\n", __func__);
  161. goto err_clks;
  162. }
  163. for (i = 0; i < count; i++) {
  164. rc = of_property_read_string_index(np, "clock-output-names",
  165. i, &clk_name);
  166. if (rc) {
  167. pr_err("%s: could not get clock names\n", np->name);
  168. goto err_cell;
  169. }
  170. /*
  171. * when count == 4, there are 4 output clocks:
  172. * /1, /2, /3, /4 respectively
  173. * when count < 4, there are at least 2 output clocks:
  174. * /1, /2, (/4, if count == 3) respectively.
  175. */
  176. if (count == 4)
  177. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  178. parent_name, 0, mult, 1 + i);
  179. else
  180. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  181. parent_name, 0, mult, 1 << i);
  182. if (IS_ERR(subclks[i])) {
  183. pr_err("%s: could not register clock\n", clk_name);
  184. goto err_cell;
  185. }
  186. }
  187. onecell_data->clks = subclks;
  188. onecell_data->clk_num = count;
  189. rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
  190. if (rc) {
  191. pr_err("Could not register clk provider for node:%s\n",
  192. np->name);
  193. goto err_cell;
  194. }
  195. return;
  196. err_cell:
  197. kfree(onecell_data);
  198. err_clks:
  199. kfree(subclks);
  200. }
  201. static const struct of_device_id clk_match[] __initconst = {
  202. { .compatible = "fixed-clock", .data = of_fixed_clk_setup, },
  203. { .compatible = "fsl,core-pll-clock", .data = core_pll_init, },
  204. { .compatible = "fsl,core-mux-clock", .data = core_mux_init, },
  205. {}
  206. };
  207. static int __init ppc_corenet_clk_probe(struct platform_device *pdev)
  208. {
  209. struct device_node *np;
  210. np = pdev->dev.of_node;
  211. base = of_iomap(np, 0);
  212. if (!base) {
  213. dev_err(&pdev->dev, "iomap error\n");
  214. return -ENOMEM;
  215. }
  216. of_clk_init(clk_match);
  217. return 0;
  218. }
  219. static const struct of_device_id ppc_clk_ids[] __initconst = {
  220. { .compatible = "fsl,qoriq-clockgen-1.0", },
  221. { .compatible = "fsl,qoriq-clockgen-2.0", },
  222. {}
  223. };
  224. static struct platform_driver ppc_corenet_clk_driver = {
  225. .driver = {
  226. .name = "ppc_corenet_clock",
  227. .owner = THIS_MODULE,
  228. .of_match_table = ppc_clk_ids,
  229. },
  230. .probe = ppc_corenet_clk_probe,
  231. };
  232. static int __init ppc_corenet_clk_init(void)
  233. {
  234. return platform_driver_register(&ppc_corenet_clk_driver);
  235. }
  236. subsys_initcall(ppc_corenet_clk_init);