clk-ppc-corenet.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 void __iomem *base;
  28. static unsigned int clocks_per_pll;
  29. static int cmux_set_parent(struct clk_hw *hw, u8 idx)
  30. {
  31. struct cmux_clk *clk = to_cmux_clk(hw);
  32. u32 clksel;
  33. clksel = ((idx / clocks_per_pll) << 2) + idx % clocks_per_pll;
  34. if (clk->flags & CLKSEL_ADJUST)
  35. clksel += 8;
  36. clksel = (clksel & 0xf) << CLKSEL_SHIFT;
  37. iowrite32be(clksel, clk->reg);
  38. return 0;
  39. }
  40. static u8 cmux_get_parent(struct clk_hw *hw)
  41. {
  42. struct cmux_clk *clk = to_cmux_clk(hw);
  43. u32 clksel;
  44. clksel = ioread32be(clk->reg);
  45. clksel = (clksel >> CLKSEL_SHIFT) & 0xf;
  46. if (clk->flags & CLKSEL_ADJUST)
  47. clksel -= 8;
  48. clksel = (clksel >> 2) * clocks_per_pll + clksel % 4;
  49. return clksel;
  50. }
  51. const struct clk_ops cmux_ops = {
  52. .get_parent = cmux_get_parent,
  53. .set_parent = cmux_set_parent,
  54. };
  55. static void __init core_mux_init(struct device_node *np)
  56. {
  57. struct clk *clk;
  58. struct clk_init_data init;
  59. struct cmux_clk *cmux_clk;
  60. struct device_node *node;
  61. int rc, count, i;
  62. u32 offset;
  63. const char *clk_name;
  64. const char **parent_names;
  65. rc = of_property_read_u32(np, "reg", &offset);
  66. if (rc) {
  67. pr_err("%s: could not get reg property\n", np->name);
  68. return;
  69. }
  70. /* get the input clock source count */
  71. count = of_property_count_strings(np, "clock-names");
  72. if (count < 0) {
  73. pr_err("%s: get clock count error\n", np->name);
  74. return;
  75. }
  76. parent_names = kzalloc((sizeof(char *) * count), GFP_KERNEL);
  77. if (!parent_names) {
  78. pr_err("%s: could not allocate parent_names\n", __func__);
  79. return;
  80. }
  81. for (i = 0; i < count; i++)
  82. parent_names[i] = of_clk_get_parent_name(np, i);
  83. cmux_clk = kzalloc(sizeof(struct cmux_clk), GFP_KERNEL);
  84. if (!cmux_clk) {
  85. pr_err("%s: could not allocate cmux_clk\n", __func__);
  86. goto err_name;
  87. }
  88. cmux_clk->reg = base + offset;
  89. node = of_find_compatible_node(NULL, NULL, "fsl,p4080-clockgen");
  90. if (node && (offset >= 0x80))
  91. cmux_clk->flags = CLKSEL_ADJUST;
  92. rc = of_property_read_string_index(np, "clock-output-names",
  93. 0, &clk_name);
  94. if (rc) {
  95. pr_err("%s: read clock names error\n", np->name);
  96. goto err_clk;
  97. }
  98. init.name = clk_name;
  99. init.ops = &cmux_ops;
  100. init.parent_names = parent_names;
  101. init.num_parents = count;
  102. init.flags = 0;
  103. cmux_clk->hw.init = &init;
  104. clk = clk_register(NULL, &cmux_clk->hw);
  105. if (IS_ERR(clk)) {
  106. pr_err("%s: could not register clock\n", clk_name);
  107. goto err_clk;
  108. }
  109. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  110. if (rc) {
  111. pr_err("Could not register clock provider for node:%s\n",
  112. np->name);
  113. goto err_clk;
  114. }
  115. goto err_name;
  116. err_clk:
  117. kfree(cmux_clk);
  118. err_name:
  119. /* free *_names because they are reallocated when registered */
  120. kfree(parent_names);
  121. }
  122. static void __init core_pll_init(struct device_node *np)
  123. {
  124. u32 offset, mult;
  125. int i, rc, count;
  126. const char *clk_name, *parent_name;
  127. struct clk_onecell_data *onecell_data;
  128. struct clk **subclks;
  129. rc = of_property_read_u32(np, "reg", &offset);
  130. if (rc) {
  131. pr_err("%s: could not get reg property\n", np->name);
  132. return;
  133. }
  134. /* get the multiple of PLL */
  135. mult = ioread32be(base + offset);
  136. /* check if this PLL is disabled */
  137. if (mult & PLL_KILL) {
  138. pr_debug("PLL:%s is disabled\n", np->name);
  139. return;
  140. }
  141. mult = (mult >> 1) & 0x3f;
  142. parent_name = of_clk_get_parent_name(np, 0);
  143. if (!parent_name) {
  144. pr_err("PLL: %s must have a parent\n", np->name);
  145. return;
  146. }
  147. count = of_property_count_strings(np, "clock-output-names");
  148. if (count < 0 || count > 4) {
  149. pr_err("%s: clock is not supported\n", np->name);
  150. return;
  151. }
  152. /* output clock number per PLL */
  153. clocks_per_pll = count;
  154. subclks = kzalloc(sizeof(struct clk *) * count, GFP_KERNEL);
  155. if (!subclks) {
  156. pr_err("%s: could not allocate subclks\n", __func__);
  157. return;
  158. }
  159. onecell_data = kzalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  160. if (!onecell_data) {
  161. pr_err("%s: could not allocate onecell_data\n", __func__);
  162. goto err_clks;
  163. }
  164. for (i = 0; i < count; i++) {
  165. rc = of_property_read_string_index(np, "clock-output-names",
  166. i, &clk_name);
  167. if (rc) {
  168. pr_err("%s: could not get clock names\n", np->name);
  169. goto err_cell;
  170. }
  171. /*
  172. * when count == 4, there are 4 output clocks:
  173. * /1, /2, /3, /4 respectively
  174. * when count < 4, there are at least 2 output clocks:
  175. * /1, /2, (/4, if count == 3) respectively.
  176. */
  177. if (count == 4)
  178. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  179. parent_name, 0, mult, 1 + i);
  180. else
  181. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  182. parent_name, 0, mult, 1 << i);
  183. if (IS_ERR(subclks[i])) {
  184. pr_err("%s: could not register clock\n", clk_name);
  185. goto err_cell;
  186. }
  187. }
  188. onecell_data->clks = subclks;
  189. onecell_data->clk_num = count;
  190. rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
  191. if (rc) {
  192. pr_err("Could not register clk provider for node:%s\n",
  193. np->name);
  194. goto err_cell;
  195. }
  196. return;
  197. err_cell:
  198. kfree(onecell_data);
  199. err_clks:
  200. kfree(subclks);
  201. }
  202. static const struct of_device_id clk_match[] __initconst = {
  203. { .compatible = "fixed-clock", .data = of_fixed_clk_setup, },
  204. { .compatible = "fsl,core-pll-clock", .data = core_pll_init, },
  205. { .compatible = "fsl,core-mux-clock", .data = core_mux_init, },
  206. {}
  207. };
  208. static int __init ppc_corenet_clk_probe(struct platform_device *pdev)
  209. {
  210. struct device_node *np;
  211. np = pdev->dev.of_node;
  212. base = of_iomap(np, 0);
  213. if (!base) {
  214. dev_err(&pdev->dev, "iomap error\n");
  215. return -ENOMEM;
  216. }
  217. of_clk_init(clk_match);
  218. return 0;
  219. }
  220. static const struct of_device_id ppc_clk_ids[] __initconst = {
  221. { .compatible = "fsl,qoriq-clockgen-1.0", },
  222. { .compatible = "fsl,qoriq-clockgen-2.0", },
  223. {}
  224. };
  225. static struct platform_driver ppc_corenet_clk_driver = {
  226. .driver = {
  227. .name = "ppc_corenet_clock",
  228. .owner = THIS_MODULE,
  229. .of_match_table = ppc_clk_ids,
  230. },
  231. .probe = ppc_corenet_clk_probe,
  232. };
  233. static int __init ppc_corenet_clk_init(void)
  234. {
  235. return platform_driver_register(&ppc_corenet_clk_driver);
  236. }
  237. subsys_initcall(ppc_corenet_clk_init);