clk-cpu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. unsigned long flags;
  112. alt_prate = clk_get_rate(cpuclk->alt_parent);
  113. spin_lock_irqsave(cpuclk->lock, flags);
  114. /*
  115. * If the old parent clock speed is less than the clock speed
  116. * of the alternate parent, then it should be ensured that at no point
  117. * the armclk speed is more than the old_rate until the dividers are
  118. * set.
  119. */
  120. if (alt_prate > ndata->old_rate) {
  121. /* calculate dividers */
  122. alt_div = DIV_ROUND_UP(alt_prate, ndata->old_rate) - 1;
  123. if (alt_div > reg_data->div_core_mask) {
  124. pr_warn("%s: limiting alt-divider %lu to %d\n",
  125. __func__, alt_div, reg_data->div_core_mask);
  126. alt_div = reg_data->div_core_mask;
  127. }
  128. /*
  129. * Change parents and add dividers in a single transaction.
  130. *
  131. * NOTE: we do this in a single transaction so we're never
  132. * dividing the primary parent by the extra dividers that were
  133. * needed for the alt.
  134. */
  135. pr_debug("%s: setting div %lu as alt-rate %lu > old-rate %lu\n",
  136. __func__, alt_div, alt_prate, ndata->old_rate);
  137. writel(HIWORD_UPDATE(alt_div, reg_data->div_core_mask,
  138. reg_data->div_core_shift) |
  139. HIWORD_UPDATE(1, 1, reg_data->mux_core_shift),
  140. cpuclk->reg_base + reg_data->core_reg);
  141. } else {
  142. /* select alternate parent */
  143. writel(HIWORD_UPDATE(1, 1, reg_data->mux_core_shift),
  144. cpuclk->reg_base + reg_data->core_reg);
  145. }
  146. spin_unlock_irqrestore(cpuclk->lock, flags);
  147. return 0;
  148. }
  149. static int rockchip_cpuclk_post_rate_change(struct rockchip_cpuclk *cpuclk,
  150. struct clk_notifier_data *ndata)
  151. {
  152. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  153. const struct rockchip_cpuclk_rate_table *rate;
  154. unsigned long flags;
  155. rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
  156. if (!rate) {
  157. pr_err("%s: Invalid rate : %lu for cpuclk\n",
  158. __func__, ndata->new_rate);
  159. return -EINVAL;
  160. }
  161. spin_lock_irqsave(cpuclk->lock, flags);
  162. if (ndata->old_rate < ndata->new_rate)
  163. rockchip_cpuclk_set_dividers(cpuclk, rate);
  164. /*
  165. * post-rate change event, re-mux to primary parent and remove dividers.
  166. *
  167. * NOTE: we do this in a single transaction so we're never dividing the
  168. * primary parent by the extra dividers that were needed for the alt.
  169. */
  170. writel(HIWORD_UPDATE(0, reg_data->div_core_mask,
  171. reg_data->div_core_shift) |
  172. HIWORD_UPDATE(0, 1, reg_data->mux_core_shift),
  173. cpuclk->reg_base + reg_data->core_reg);
  174. if (ndata->old_rate > ndata->new_rate)
  175. rockchip_cpuclk_set_dividers(cpuclk, rate);
  176. spin_unlock_irqrestore(cpuclk->lock, flags);
  177. return 0;
  178. }
  179. /*
  180. * This clock notifier is called when the frequency of the parent clock
  181. * of cpuclk is to be changed. This notifier handles the setting up all
  182. * the divider clocks, remux to temporary parent and handling the safe
  183. * frequency levels when using temporary parent.
  184. */
  185. static int rockchip_cpuclk_notifier_cb(struct notifier_block *nb,
  186. unsigned long event, void *data)
  187. {
  188. struct clk_notifier_data *ndata = data;
  189. struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_nb(nb);
  190. int ret = 0;
  191. pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
  192. __func__, event, ndata->old_rate, ndata->new_rate);
  193. if (event == PRE_RATE_CHANGE)
  194. ret = rockchip_cpuclk_pre_rate_change(cpuclk, ndata);
  195. else if (event == POST_RATE_CHANGE)
  196. ret = rockchip_cpuclk_post_rate_change(cpuclk, ndata);
  197. return notifier_from_errno(ret);
  198. }
  199. struct clk *rockchip_clk_register_cpuclk(const char *name,
  200. const char **parent_names, u8 num_parents,
  201. const struct rockchip_cpuclk_reg_data *reg_data,
  202. const struct rockchip_cpuclk_rate_table *rates,
  203. int nrates, void __iomem *reg_base, spinlock_t *lock)
  204. {
  205. struct rockchip_cpuclk *cpuclk;
  206. struct clk_init_data init;
  207. struct clk *clk, *cclk;
  208. int ret;
  209. if (num_parents != 2) {
  210. pr_err("%s: needs two parent clocks\n", __func__);
  211. return ERR_PTR(-EINVAL);
  212. }
  213. cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
  214. if (!cpuclk)
  215. return ERR_PTR(-ENOMEM);
  216. init.name = name;
  217. init.parent_names = &parent_names[0];
  218. init.num_parents = 1;
  219. init.ops = &rockchip_cpuclk_ops;
  220. /* only allow rate changes when we have a rate table */
  221. init.flags = (nrates > 0) ? CLK_SET_RATE_PARENT : 0;
  222. /* disallow automatic parent changes by ccf */
  223. init.flags |= CLK_SET_RATE_NO_REPARENT;
  224. init.flags |= CLK_GET_RATE_NOCACHE;
  225. cpuclk->reg_base = reg_base;
  226. cpuclk->lock = lock;
  227. cpuclk->reg_data = reg_data;
  228. cpuclk->clk_nb.notifier_call = rockchip_cpuclk_notifier_cb;
  229. cpuclk->hw.init = &init;
  230. cpuclk->alt_parent = __clk_lookup(parent_names[1]);
  231. if (!cpuclk->alt_parent) {
  232. pr_err("%s: could not lookup alternate parent\n",
  233. __func__);
  234. ret = -EINVAL;
  235. goto free_cpuclk;
  236. }
  237. ret = clk_prepare_enable(cpuclk->alt_parent);
  238. if (ret) {
  239. pr_err("%s: could not enable alternate parent\n",
  240. __func__);
  241. goto free_cpuclk;
  242. }
  243. clk = __clk_lookup(parent_names[0]);
  244. if (!clk) {
  245. pr_err("%s: could not lookup parent clock %s\n",
  246. __func__, parent_names[0]);
  247. ret = -EINVAL;
  248. goto free_cpuclk;
  249. }
  250. ret = clk_notifier_register(clk, &cpuclk->clk_nb);
  251. if (ret) {
  252. pr_err("%s: failed to register clock notifier for %s\n",
  253. __func__, name);
  254. goto free_cpuclk;
  255. }
  256. if (nrates > 0) {
  257. cpuclk->rate_count = nrates;
  258. cpuclk->rate_table = kmemdup(rates,
  259. sizeof(*rates) * nrates,
  260. GFP_KERNEL);
  261. if (!cpuclk->rate_table) {
  262. pr_err("%s: could not allocate memory for cpuclk rates\n",
  263. __func__);
  264. ret = -ENOMEM;
  265. goto unregister_notifier;
  266. }
  267. }
  268. cclk = clk_register(NULL, &cpuclk->hw);
  269. if (IS_ERR(clk)) {
  270. pr_err("%s: could not register cpuclk %s\n", __func__, name);
  271. ret = PTR_ERR(clk);
  272. goto free_rate_table;
  273. }
  274. return cclk;
  275. free_rate_table:
  276. kfree(cpuclk->rate_table);
  277. unregister_notifier:
  278. clk_notifier_unregister(clk, &cpuclk->clk_nb);
  279. free_cpuclk:
  280. kfree(cpuclk);
  281. return ERR_PTR(ret);
  282. }