clk-cpu.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2014 MundoReader S.L.
  3. * Author: Heiko Stuebner <heiko@sntech.de>
  4. *
  5. * based on clk/samsung/clk-cpu.c
  6. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  7. * Author: Thomas Abraham <thomas.ab@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * A CPU clock is defined as a clock supplied to a CPU or a group of CPUs.
  14. * The CPU clock is typically derived from a hierarchy of clock
  15. * blocks which includes mux and divider blocks. There are a number of other
  16. * auxiliary clocks supplied to the CPU domain such as the debug blocks and AXI
  17. * clock for CPU domain. The rates of these auxiliary clocks are related to the
  18. * CPU clock rate and this relation is usually specified in the hardware manual
  19. * of the SoC or supplied after the SoC characterization.
  20. *
  21. * The below implementation of the CPU clock allows the rate changes of the CPU
  22. * clock and the corresponding rate changes of the auxillary clocks of the CPU
  23. * domain. The platform clock driver provides a clock register configuration
  24. * for each configurable rate which is then used to program the clock hardware
  25. * registers to acheive a fast co-oridinated rate change for all the CPU domain
  26. * clocks.
  27. *
  28. * On a rate change request for the CPU clock, the rate change is propagated
  29. * upto the PLL supplying the clock to the CPU domain clock blocks. While the
  30. * CPU domain PLL is reconfigured, the CPU domain clocks are driven using an
  31. * alternate clock source. If required, the alternate clock source is divided
  32. * down in order to keep the output clock rate within the previous OPP limits.
  33. */
  34. #include <linux/of.h>
  35. #include <linux/slab.h>
  36. #include <linux/io.h>
  37. #include <linux/clk-provider.h>
  38. #include "clk.h"
  39. /**
  40. * struct rockchip_cpuclk: information about clock supplied to a CPU core.
  41. * @hw: handle between ccf and cpu clock.
  42. * @alt_parent: alternate parent clock to use when switching the speed
  43. * of the primary parent clock.
  44. * @reg_base: base register for cpu-clock values.
  45. * @clk_nb: clock notifier registered for changes in clock speed of the
  46. * primary parent clock.
  47. * @rate_count: number of rates in the rate_table
  48. * @rate_table: pll-rates and their associated dividers
  49. * @reg_data: cpu-specific register settings
  50. * @lock: clock lock
  51. */
  52. struct rockchip_cpuclk {
  53. struct clk_hw hw;
  54. struct clk_mux cpu_mux;
  55. const struct clk_ops *cpu_mux_ops;
  56. struct clk *alt_parent;
  57. void __iomem *reg_base;
  58. struct notifier_block clk_nb;
  59. unsigned int rate_count;
  60. struct rockchip_cpuclk_rate_table *rate_table;
  61. const struct rockchip_cpuclk_reg_data *reg_data;
  62. spinlock_t *lock;
  63. };
  64. #define to_rockchip_cpuclk_hw(hw) container_of(hw, struct rockchip_cpuclk, hw)
  65. #define to_rockchip_cpuclk_nb(nb) \
  66. container_of(nb, struct rockchip_cpuclk, clk_nb)
  67. static const struct rockchip_cpuclk_rate_table *rockchip_get_cpuclk_settings(
  68. struct rockchip_cpuclk *cpuclk, unsigned long rate)
  69. {
  70. const struct rockchip_cpuclk_rate_table *rate_table =
  71. cpuclk->rate_table;
  72. int i;
  73. for (i = 0; i < cpuclk->rate_count; i++) {
  74. if (rate == rate_table[i].prate)
  75. return &rate_table[i];
  76. }
  77. return NULL;
  78. }
  79. static unsigned long rockchip_cpuclk_recalc_rate(struct clk_hw *hw,
  80. unsigned long parent_rate)
  81. {
  82. struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_hw(hw);
  83. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  84. u32 clksel0 = readl_relaxed(cpuclk->reg_base + reg_data->core_reg);
  85. clksel0 >>= reg_data->div_core_shift;
  86. clksel0 &= reg_data->div_core_mask;
  87. return parent_rate / (clksel0 + 1);
  88. }
  89. static const struct clk_ops rockchip_cpuclk_ops = {
  90. .recalc_rate = rockchip_cpuclk_recalc_rate,
  91. };
  92. static void rockchip_cpuclk_set_dividers(struct rockchip_cpuclk *cpuclk,
  93. const struct rockchip_cpuclk_rate_table *rate)
  94. {
  95. int i;
  96. /* alternate parent is active now. set the dividers */
  97. for (i = 0; i < ARRAY_SIZE(rate->divs); i++) {
  98. const struct rockchip_cpuclk_clksel *clksel = &rate->divs[i];
  99. if (!clksel->reg)
  100. continue;
  101. pr_debug("%s: setting reg 0x%x to 0x%x\n",
  102. __func__, clksel->reg, clksel->val);
  103. writel(clksel->val , cpuclk->reg_base + clksel->reg);
  104. }
  105. }
  106. static int rockchip_cpuclk_pre_rate_change(struct rockchip_cpuclk *cpuclk,
  107. struct clk_notifier_data *ndata)
  108. {
  109. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  110. unsigned long alt_prate, alt_div;
  111. alt_prate = clk_get_rate(cpuclk->alt_parent);
  112. spin_lock(cpuclk->lock);
  113. /*
  114. * If the old parent clock speed is less than the clock speed
  115. * of the alternate parent, then it should be ensured that at no point
  116. * the armclk speed is more than the old_rate until the dividers are
  117. * set.
  118. */
  119. if (alt_prate > ndata->old_rate) {
  120. /* calculate dividers */
  121. alt_div = DIV_ROUND_UP(alt_prate, ndata->old_rate) - 1;
  122. if (alt_div > reg_data->div_core_mask) {
  123. pr_warn("%s: limiting alt-divider %lu to %d\n",
  124. __func__, alt_div, reg_data->div_core_mask);
  125. alt_div = reg_data->div_core_mask;
  126. }
  127. /*
  128. * Change parents and add dividers in a single transaction.
  129. *
  130. * NOTE: we do this in a single transaction so we're never
  131. * dividing the primary parent by the extra dividers that were
  132. * needed for the alt.
  133. */
  134. pr_debug("%s: setting div %lu as alt-rate %lu > old-rate %lu\n",
  135. __func__, alt_div, alt_prate, ndata->old_rate);
  136. writel(HIWORD_UPDATE(alt_div, reg_data->div_core_mask,
  137. reg_data->div_core_shift) |
  138. HIWORD_UPDATE(1, 1, reg_data->mux_core_shift),
  139. cpuclk->reg_base + reg_data->core_reg);
  140. } else {
  141. /* select alternate parent */
  142. writel(HIWORD_UPDATE(1, 1, reg_data->mux_core_shift),
  143. cpuclk->reg_base + reg_data->core_reg);
  144. }
  145. spin_unlock(cpuclk->lock);
  146. return 0;
  147. }
  148. static int rockchip_cpuclk_post_rate_change(struct rockchip_cpuclk *cpuclk,
  149. struct clk_notifier_data *ndata)
  150. {
  151. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  152. const struct rockchip_cpuclk_rate_table *rate;
  153. rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
  154. if (!rate) {
  155. pr_err("%s: Invalid rate : %lu for cpuclk\n",
  156. __func__, ndata->new_rate);
  157. return -EINVAL;
  158. }
  159. spin_lock(cpuclk->lock);
  160. if (ndata->old_rate < ndata->new_rate)
  161. rockchip_cpuclk_set_dividers(cpuclk, rate);
  162. /*
  163. * post-rate change event, re-mux to primary parent and remove dividers.
  164. *
  165. * NOTE: we do this in a single transaction so we're never dividing the
  166. * primary parent by the extra dividers that were needed for the alt.
  167. */
  168. writel(HIWORD_UPDATE(0, reg_data->div_core_mask,
  169. reg_data->div_core_shift) |
  170. HIWORD_UPDATE(0, 1, reg_data->mux_core_shift),
  171. cpuclk->reg_base + reg_data->core_reg);
  172. if (ndata->old_rate > ndata->new_rate)
  173. rockchip_cpuclk_set_dividers(cpuclk, rate);
  174. spin_unlock(cpuclk->lock);
  175. return 0;
  176. }
  177. /*
  178. * This clock notifier is called when the frequency of the parent clock
  179. * of cpuclk is to be changed. This notifier handles the setting up all
  180. * the divider clocks, remux to temporary parent and handling the safe
  181. * frequency levels when using temporary parent.
  182. */
  183. static int rockchip_cpuclk_notifier_cb(struct notifier_block *nb,
  184. unsigned long event, void *data)
  185. {
  186. struct clk_notifier_data *ndata = data;
  187. struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_nb(nb);
  188. int ret = 0;
  189. pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
  190. __func__, event, ndata->old_rate, ndata->new_rate);
  191. if (event == PRE_RATE_CHANGE)
  192. ret = rockchip_cpuclk_pre_rate_change(cpuclk, ndata);
  193. else if (event == POST_RATE_CHANGE)
  194. ret = rockchip_cpuclk_post_rate_change(cpuclk, ndata);
  195. return notifier_from_errno(ret);
  196. }
  197. struct clk *rockchip_clk_register_cpuclk(const char *name,
  198. const char **parent_names, u8 num_parents,
  199. const struct rockchip_cpuclk_reg_data *reg_data,
  200. const struct rockchip_cpuclk_rate_table *rates,
  201. int nrates, void __iomem *reg_base, spinlock_t *lock)
  202. {
  203. struct rockchip_cpuclk *cpuclk;
  204. struct clk_init_data init;
  205. struct clk *clk, *cclk;
  206. int ret;
  207. if (num_parents != 2) {
  208. pr_err("%s: needs two parent clocks\n", __func__);
  209. return ERR_PTR(-EINVAL);
  210. }
  211. cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
  212. if (!cpuclk)
  213. return ERR_PTR(-ENOMEM);
  214. init.name = name;
  215. init.parent_names = &parent_names[0];
  216. init.num_parents = 1;
  217. init.ops = &rockchip_cpuclk_ops;
  218. /* only allow rate changes when we have a rate table */
  219. init.flags = (nrates > 0) ? CLK_SET_RATE_PARENT : 0;
  220. /* disallow automatic parent changes by ccf */
  221. init.flags |= CLK_SET_RATE_NO_REPARENT;
  222. init.flags |= CLK_GET_RATE_NOCACHE;
  223. cpuclk->reg_base = reg_base;
  224. cpuclk->lock = lock;
  225. cpuclk->reg_data = reg_data;
  226. cpuclk->clk_nb.notifier_call = rockchip_cpuclk_notifier_cb;
  227. cpuclk->hw.init = &init;
  228. cpuclk->alt_parent = __clk_lookup(parent_names[1]);
  229. if (!cpuclk->alt_parent) {
  230. pr_err("%s: could not lookup alternate parent\n",
  231. __func__);
  232. ret = -EINVAL;
  233. goto free_cpuclk;
  234. }
  235. ret = clk_prepare_enable(cpuclk->alt_parent);
  236. if (ret) {
  237. pr_err("%s: could not enable alternate parent\n",
  238. __func__);
  239. goto free_cpuclk;
  240. }
  241. clk = __clk_lookup(parent_names[0]);
  242. if (!clk) {
  243. pr_err("%s: could not lookup parent clock %s\n",
  244. __func__, parent_names[0]);
  245. ret = -EINVAL;
  246. goto free_cpuclk;
  247. }
  248. ret = clk_notifier_register(clk, &cpuclk->clk_nb);
  249. if (ret) {
  250. pr_err("%s: failed to register clock notifier for %s\n",
  251. __func__, name);
  252. goto free_cpuclk;
  253. }
  254. if (nrates > 0) {
  255. cpuclk->rate_count = nrates;
  256. cpuclk->rate_table = kmemdup(rates,
  257. sizeof(*rates) * nrates,
  258. GFP_KERNEL);
  259. if (!cpuclk->rate_table) {
  260. pr_err("%s: could not allocate memory for cpuclk rates\n",
  261. __func__);
  262. ret = -ENOMEM;
  263. goto unregister_notifier;
  264. }
  265. }
  266. cclk = clk_register(NULL, &cpuclk->hw);
  267. if (IS_ERR(clk)) {
  268. pr_err("%s: could not register cpuclk %s\n", __func__, name);
  269. ret = PTR_ERR(clk);
  270. goto free_rate_table;
  271. }
  272. return cclk;
  273. free_rate_table:
  274. kfree(cpuclk->rate_table);
  275. unregister_notifier:
  276. clk_notifier_unregister(clk, &cpuclk->clk_nb);
  277. free_cpuclk:
  278. kfree(cpuclk);
  279. return ERR_PTR(ret);
  280. }