clk-qoriq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 QorIQ SoCs.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk-provider.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. struct cmux_clk {
  20. struct clk_hw hw;
  21. void __iomem *reg;
  22. unsigned int clk_per_pll;
  23. u32 flags;
  24. };
  25. #define PLL_KILL BIT(31)
  26. #define CLKSEL_SHIFT 27
  27. #define CLKSEL_ADJUST BIT(0)
  28. #define to_cmux_clk(p) container_of(p, struct cmux_clk, hw)
  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 / clk->clk_per_pll) << 2) + idx % clk->clk_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) * clk->clk_per_pll + clksel % 4;
  49. return clksel;
  50. }
  51. static 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. struct of_phandle_args clkspec;
  66. rc = of_property_read_u32(np, "reg", &offset);
  67. if (rc) {
  68. pr_err("%s: could not get reg property\n", np->name);
  69. return;
  70. }
  71. /* get the input clock source count */
  72. count = of_property_count_strings(np, "clock-names");
  73. if (count < 0) {
  74. pr_err("%s: get clock count error\n", np->name);
  75. return;
  76. }
  77. parent_names = kcalloc(count, sizeof(char *), GFP_KERNEL);
  78. if (!parent_names)
  79. return;
  80. for (i = 0; i < count; i++)
  81. parent_names[i] = of_clk_get_parent_name(np, i);
  82. cmux_clk = kzalloc(sizeof(*cmux_clk), GFP_KERNEL);
  83. if (!cmux_clk)
  84. goto err_name;
  85. cmux_clk->reg = of_iomap(np, 0);
  86. if (!cmux_clk->reg) {
  87. pr_err("%s: could not map register\n", __func__);
  88. goto err_clk;
  89. }
  90. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", 0,
  91. &clkspec);
  92. if (rc) {
  93. pr_err("%s: parse clock node error\n", __func__);
  94. goto err_clk;
  95. }
  96. cmux_clk->clk_per_pll = of_property_count_strings(clkspec.np,
  97. "clock-output-names");
  98. of_node_put(clkspec.np);
  99. node = of_find_compatible_node(NULL, NULL, "fsl,p4080-clockgen");
  100. if (node && (offset >= 0x80))
  101. cmux_clk->flags = CLKSEL_ADJUST;
  102. rc = of_property_read_string_index(np, "clock-output-names",
  103. 0, &clk_name);
  104. if (rc) {
  105. pr_err("%s: read clock names error\n", np->name);
  106. goto err_clk;
  107. }
  108. init.name = clk_name;
  109. init.ops = &cmux_ops;
  110. init.parent_names = parent_names;
  111. init.num_parents = count;
  112. init.flags = 0;
  113. cmux_clk->hw.init = &init;
  114. clk = clk_register(NULL, &cmux_clk->hw);
  115. if (IS_ERR(clk)) {
  116. pr_err("%s: could not register clock\n", clk_name);
  117. goto err_clk;
  118. }
  119. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  120. if (rc) {
  121. pr_err("Could not register clock provider for node:%s\n",
  122. np->name);
  123. goto err_clk;
  124. }
  125. goto err_name;
  126. err_clk:
  127. kfree(cmux_clk);
  128. err_name:
  129. /* free *_names because they are reallocated when registered */
  130. kfree(parent_names);
  131. }
  132. static void __init core_pll_init(struct device_node *np)
  133. {
  134. u32 mult;
  135. int i, rc, count;
  136. const char *clk_name, *parent_name;
  137. struct clk_onecell_data *onecell_data;
  138. struct clk **subclks;
  139. void __iomem *base;
  140. base = of_iomap(np, 0);
  141. if (!base) {
  142. pr_err("iomap error\n");
  143. return;
  144. }
  145. /* get the multiple of PLL */
  146. mult = ioread32be(base);
  147. /* check if this PLL is disabled */
  148. if (mult & PLL_KILL) {
  149. pr_debug("PLL:%s is disabled\n", np->name);
  150. goto err_map;
  151. }
  152. mult = (mult >> 1) & 0x3f;
  153. parent_name = of_clk_get_parent_name(np, 0);
  154. if (!parent_name) {
  155. pr_err("PLL: %s must have a parent\n", np->name);
  156. goto err_map;
  157. }
  158. count = of_property_count_strings(np, "clock-output-names");
  159. if (count < 0 || count > 4) {
  160. pr_err("%s: clock is not supported\n", np->name);
  161. goto err_map;
  162. }
  163. subclks = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
  164. if (!subclks)
  165. goto err_map;
  166. onecell_data = kmalloc(sizeof(*onecell_data), GFP_KERNEL);
  167. if (!onecell_data)
  168. goto err_clks;
  169. for (i = 0; i < count; i++) {
  170. rc = of_property_read_string_index(np, "clock-output-names",
  171. i, &clk_name);
  172. if (rc) {
  173. pr_err("%s: could not get clock names\n", np->name);
  174. goto err_cell;
  175. }
  176. /*
  177. * when count == 4, there are 4 output clocks:
  178. * /1, /2, /3, /4 respectively
  179. * when count < 4, there are at least 2 output clocks:
  180. * /1, /2, (/4, if count == 3) respectively.
  181. */
  182. if (count == 4)
  183. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  184. parent_name, 0, mult, 1 + i);
  185. else
  186. subclks[i] = clk_register_fixed_factor(NULL, clk_name,
  187. parent_name, 0, mult, 1 << i);
  188. if (IS_ERR(subclks[i])) {
  189. pr_err("%s: could not register clock\n", clk_name);
  190. goto err_cell;
  191. }
  192. }
  193. onecell_data->clks = subclks;
  194. onecell_data->clk_num = count;
  195. rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
  196. if (rc) {
  197. pr_err("Could not register clk provider for node:%s\n",
  198. np->name);
  199. goto err_cell;
  200. }
  201. iounmap(base);
  202. return;
  203. err_cell:
  204. kfree(onecell_data);
  205. err_clks:
  206. kfree(subclks);
  207. err_map:
  208. iounmap(base);
  209. }
  210. static void __init sysclk_init(struct device_node *node)
  211. {
  212. struct clk *clk;
  213. const char *clk_name = node->name;
  214. struct device_node *np = of_get_parent(node);
  215. u32 rate;
  216. if (!np) {
  217. pr_err("could not get parent node\n");
  218. return;
  219. }
  220. if (of_property_read_u32(np, "clock-frequency", &rate)) {
  221. of_node_put(node);
  222. return;
  223. }
  224. of_property_read_string(np, "clock-output-names", &clk_name);
  225. clk = clk_register_fixed_rate(NULL, clk_name, NULL, CLK_IS_ROOT, rate);
  226. if (!IS_ERR(clk))
  227. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  228. }
  229. static void __init pltfrm_pll_init(struct device_node *np)
  230. {
  231. void __iomem *base;
  232. uint32_t mult;
  233. const char *parent_name, *clk_name;
  234. int i, _errno;
  235. struct clk_onecell_data *cod;
  236. base = of_iomap(np, 0);
  237. if (!base) {
  238. pr_err("%s(): %s: of_iomap() failed\n", __func__, np->name);
  239. return;
  240. }
  241. /* Get the multiple of PLL */
  242. mult = ioread32be(base);
  243. iounmap(base);
  244. /* Check if this PLL is disabled */
  245. if (mult & PLL_KILL) {
  246. pr_debug("%s(): %s: Disabled\n", __func__, np->name);
  247. return;
  248. }
  249. mult = (mult & GENMASK(6, 1)) >> 1;
  250. parent_name = of_clk_get_parent_name(np, 0);
  251. if (!parent_name) {
  252. pr_err("%s(): %s: of_clk_get_parent_name() failed\n",
  253. __func__, np->name);
  254. return;
  255. }
  256. i = of_property_count_strings(np, "clock-output-names");
  257. if (i < 0) {
  258. pr_err("%s(): %s: of_property_count_strings(clock-output-names) = %d\n",
  259. __func__, np->name, i);
  260. return;
  261. }
  262. cod = kmalloc(sizeof(*cod) + i * sizeof(struct clk *), GFP_KERNEL);
  263. if (!cod)
  264. return;
  265. cod->clks = (struct clk **)(cod + 1);
  266. cod->clk_num = i;
  267. for (i = 0; i < cod->clk_num; i++) {
  268. _errno = of_property_read_string_index(np, "clock-output-names",
  269. i, &clk_name);
  270. if (_errno < 0) {
  271. pr_err("%s(): %s: of_property_read_string_index(clock-output-names) = %d\n",
  272. __func__, np->name, _errno);
  273. goto return_clk_unregister;
  274. }
  275. cod->clks[i] = clk_register_fixed_factor(NULL, clk_name,
  276. parent_name, 0, mult, 1 + i);
  277. if (IS_ERR(cod->clks[i])) {
  278. pr_err("%s(): %s: clk_register_fixed_factor(%s) = %ld\n",
  279. __func__, np->name,
  280. clk_name, PTR_ERR(cod->clks[i]));
  281. goto return_clk_unregister;
  282. }
  283. }
  284. _errno = of_clk_add_provider(np, of_clk_src_onecell_get, cod);
  285. if (_errno < 0) {
  286. pr_err("%s(): %s: of_clk_add_provider() = %d\n",
  287. __func__, np->name, _errno);
  288. goto return_clk_unregister;
  289. }
  290. return;
  291. return_clk_unregister:
  292. while (--i >= 0)
  293. clk_unregister(cod->clks[i]);
  294. kfree(cod);
  295. }
  296. CLK_OF_DECLARE(qoriq_sysclk_1, "fsl,qoriq-sysclk-1.0", sysclk_init);
  297. CLK_OF_DECLARE(qoriq_sysclk_2, "fsl,qoriq-sysclk-2.0", sysclk_init);
  298. CLK_OF_DECLARE(qoriq_core_pll_1, "fsl,qoriq-core-pll-1.0", core_pll_init);
  299. CLK_OF_DECLARE(qoriq_core_pll_2, "fsl,qoriq-core-pll-2.0", core_pll_init);
  300. CLK_OF_DECLARE(qoriq_core_mux_1, "fsl,qoriq-core-mux-1.0", core_mux_init);
  301. CLK_OF_DECLARE(qoriq_core_mux_2, "fsl,qoriq-core-mux-2.0", core_mux_init);
  302. CLK_OF_DECLARE(qoriq_pltfrm_pll_1, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init);
  303. CLK_OF_DECLARE(qoriq_pltfrm_pll_2, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init);