common.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Marvell EBU SoC common clock handling
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  7. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  8. * Andrew Lunn <andrew@lunn.ch>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/clk.h>
  16. #include <linux/clkdev.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/syscore_ops.h>
  22. #include "common.h"
  23. /*
  24. * Core Clocks
  25. */
  26. #define SSCG_CONF_MODE(reg) (((reg) >> 16) & 0x3)
  27. #define SSCG_SPREAD_DOWN 0x0
  28. #define SSCG_SPREAD_UP 0x1
  29. #define SSCG_SPREAD_CENTRAL 0x2
  30. #define SSCG_CONF_LOW(reg) (((reg) >> 8) & 0xFF)
  31. #define SSCG_CONF_HIGH(reg) ((reg) & 0xFF)
  32. static struct clk_onecell_data clk_data;
  33. /*
  34. * This function can be used by the Kirkwood, the Armada 370, the
  35. * Armada XP and the Armada 375 SoC. The name of the function was
  36. * chosen following the dt convention: using the first known SoC
  37. * compatible with it.
  38. */
  39. u32 kirkwood_fix_sscg_deviation(u32 system_clk)
  40. {
  41. struct device_node *sscg_np = NULL;
  42. void __iomem *sscg_map;
  43. u32 sscg_reg;
  44. s32 low_bound, high_bound;
  45. u64 freq_swing_half;
  46. sscg_np = of_find_node_by_name(NULL, "sscg");
  47. if (sscg_np == NULL) {
  48. pr_err("cannot get SSCG register node\n");
  49. return system_clk;
  50. }
  51. sscg_map = of_iomap(sscg_np, 0);
  52. if (sscg_map == NULL) {
  53. pr_err("cannot map SSCG register\n");
  54. goto out;
  55. }
  56. sscg_reg = readl(sscg_map);
  57. high_bound = SSCG_CONF_HIGH(sscg_reg);
  58. low_bound = SSCG_CONF_LOW(sscg_reg);
  59. if ((high_bound - low_bound) <= 0)
  60. goto out;
  61. /*
  62. * From Marvell engineer we got the following formula (when
  63. * this code was written, the datasheet was erroneous)
  64. * Spread percentage = 1/96 * (H - L) / H
  65. * H = SSCG_High_Boundary
  66. * L = SSCG_Low_Boundary
  67. *
  68. * As the deviation is half of spread then it lead to the
  69. * following formula in the code.
  70. *
  71. * To avoid an overflow and not lose any significant digit in
  72. * the same time we have to use a 64 bit integer.
  73. */
  74. freq_swing_half = (((u64)high_bound - (u64)low_bound)
  75. * (u64)system_clk);
  76. do_div(freq_swing_half, (2 * 96 * high_bound));
  77. switch (SSCG_CONF_MODE(sscg_reg)) {
  78. case SSCG_SPREAD_DOWN:
  79. system_clk -= freq_swing_half;
  80. break;
  81. case SSCG_SPREAD_UP:
  82. system_clk += freq_swing_half;
  83. break;
  84. case SSCG_SPREAD_CENTRAL:
  85. default:
  86. break;
  87. }
  88. iounmap(sscg_map);
  89. out:
  90. of_node_put(sscg_np);
  91. return system_clk;
  92. }
  93. void __init mvebu_coreclk_setup(struct device_node *np,
  94. const struct coreclk_soc_desc *desc)
  95. {
  96. const char *tclk_name = "tclk";
  97. const char *cpuclk_name = "cpuclk";
  98. void __iomem *base;
  99. unsigned long rate;
  100. int n;
  101. base = of_iomap(np, 0);
  102. if (WARN_ON(!base))
  103. return;
  104. /* Allocate struct for TCLK, cpu clk, and core ratio clocks */
  105. clk_data.clk_num = 2 + desc->num_ratios;
  106. clk_data.clks = kzalloc(clk_data.clk_num * sizeof(struct clk *),
  107. GFP_KERNEL);
  108. if (WARN_ON(!clk_data.clks)) {
  109. iounmap(base);
  110. return;
  111. }
  112. /* Register TCLK */
  113. of_property_read_string_index(np, "clock-output-names", 0,
  114. &tclk_name);
  115. rate = desc->get_tclk_freq(base);
  116. clk_data.clks[0] = clk_register_fixed_rate(NULL, tclk_name, NULL,
  117. CLK_IS_ROOT, rate);
  118. WARN_ON(IS_ERR(clk_data.clks[0]));
  119. /* Register CPU clock */
  120. of_property_read_string_index(np, "clock-output-names", 1,
  121. &cpuclk_name);
  122. rate = desc->get_cpu_freq(base);
  123. if (desc->is_sscg_enabled && desc->fix_sscg_deviation
  124. && desc->is_sscg_enabled(base))
  125. rate = desc->fix_sscg_deviation(rate);
  126. clk_data.clks[1] = clk_register_fixed_rate(NULL, cpuclk_name, NULL,
  127. CLK_IS_ROOT, rate);
  128. WARN_ON(IS_ERR(clk_data.clks[1]));
  129. /* Register fixed-factor clocks derived from CPU clock */
  130. for (n = 0; n < desc->num_ratios; n++) {
  131. const char *rclk_name = desc->ratios[n].name;
  132. int mult, div;
  133. of_property_read_string_index(np, "clock-output-names",
  134. 2+n, &rclk_name);
  135. desc->get_clk_ratio(base, desc->ratios[n].id, &mult, &div);
  136. clk_data.clks[2+n] = clk_register_fixed_factor(NULL, rclk_name,
  137. cpuclk_name, 0, mult, div);
  138. WARN_ON(IS_ERR(clk_data.clks[2+n]));
  139. };
  140. /* SAR register isn't needed anymore */
  141. iounmap(base);
  142. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  143. }
  144. /*
  145. * Clock Gating Control
  146. */
  147. DEFINE_SPINLOCK(ctrl_gating_lock);
  148. struct clk_gating_ctrl {
  149. spinlock_t *lock;
  150. struct clk **gates;
  151. int num_gates;
  152. void __iomem *base;
  153. u32 saved_reg;
  154. };
  155. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  156. static struct clk_gating_ctrl *ctrl;
  157. static struct clk *clk_gating_get_src(
  158. struct of_phandle_args *clkspec, void *data)
  159. {
  160. int n;
  161. if (clkspec->args_count < 1)
  162. return ERR_PTR(-EINVAL);
  163. for (n = 0; n < ctrl->num_gates; n++) {
  164. struct clk_gate *gate =
  165. to_clk_gate(__clk_get_hw(ctrl->gates[n]));
  166. if (clkspec->args[0] == gate->bit_idx)
  167. return ctrl->gates[n];
  168. }
  169. return ERR_PTR(-ENODEV);
  170. }
  171. static int mvebu_clk_gating_suspend(void)
  172. {
  173. ctrl->saved_reg = readl(ctrl->base);
  174. return 0;
  175. }
  176. static void mvebu_clk_gating_resume(void)
  177. {
  178. writel(ctrl->saved_reg, ctrl->base);
  179. }
  180. static struct syscore_ops clk_gate_syscore_ops = {
  181. .suspend = mvebu_clk_gating_suspend,
  182. .resume = mvebu_clk_gating_resume,
  183. };
  184. void __init mvebu_clk_gating_setup(struct device_node *np,
  185. const struct clk_gating_soc_desc *desc)
  186. {
  187. struct clk *clk;
  188. void __iomem *base;
  189. const char *default_parent = NULL;
  190. int n;
  191. if (ctrl) {
  192. pr_err("mvebu-clk-gating: cannot instantiate more than one gatable clock device\n");
  193. return;
  194. }
  195. base = of_iomap(np, 0);
  196. if (WARN_ON(!base))
  197. return;
  198. clk = of_clk_get(np, 0);
  199. if (!IS_ERR(clk)) {
  200. default_parent = __clk_get_name(clk);
  201. clk_put(clk);
  202. }
  203. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  204. if (WARN_ON(!ctrl))
  205. goto ctrl_out;
  206. /* lock must already be initialized */
  207. ctrl->lock = &ctrl_gating_lock;
  208. ctrl->base = base;
  209. /* Count, allocate, and register clock gates */
  210. for (n = 0; desc[n].name;)
  211. n++;
  212. ctrl->num_gates = n;
  213. ctrl->gates = kzalloc(ctrl->num_gates * sizeof(struct clk *),
  214. GFP_KERNEL);
  215. if (WARN_ON(!ctrl->gates))
  216. goto gates_out;
  217. for (n = 0; n < ctrl->num_gates; n++) {
  218. const char *parent =
  219. (desc[n].parent) ? desc[n].parent : default_parent;
  220. ctrl->gates[n] = clk_register_gate(NULL, desc[n].name, parent,
  221. desc[n].flags, base, desc[n].bit_idx,
  222. 0, ctrl->lock);
  223. WARN_ON(IS_ERR(ctrl->gates[n]));
  224. }
  225. of_clk_add_provider(np, clk_gating_get_src, ctrl);
  226. register_syscore_ops(&clk_gate_syscore_ops);
  227. return;
  228. gates_out:
  229. kfree(ctrl);
  230. ctrl_out:
  231. iounmap(base);
  232. }