clk-cpu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.h>
  38. #include <linux/clk-provider.h>
  39. #include "clk.h"
  40. /**
  41. * struct rockchip_cpuclk: information about clock supplied to a CPU core.
  42. * @hw: handle between ccf and cpu clock.
  43. * @alt_parent: alternate parent clock to use when switching the speed
  44. * of the primary parent clock.
  45. * @reg_base: base register for cpu-clock values.
  46. * @clk_nb: clock notifier registered for changes in clock speed of the
  47. * primary parent clock.
  48. * @rate_count: number of rates in the rate_table
  49. * @rate_table: pll-rates and their associated dividers
  50. * @reg_data: cpu-specific register settings
  51. * @lock: clock lock
  52. */
  53. struct rockchip_cpuclk {
  54. struct clk_hw hw;
  55. struct clk_mux cpu_mux;
  56. const struct clk_ops *cpu_mux_ops;
  57. struct clk *alt_parent;
  58. void __iomem *reg_base;
  59. struct notifier_block clk_nb;
  60. unsigned int rate_count;
  61. struct rockchip_cpuclk_rate_table *rate_table;
  62. const struct rockchip_cpuclk_reg_data *reg_data;
  63. spinlock_t *lock;
  64. };
  65. #define to_rockchip_cpuclk_hw(hw) container_of(hw, struct rockchip_cpuclk, hw)
  66. #define to_rockchip_cpuclk_nb(nb) \
  67. container_of(nb, struct rockchip_cpuclk, clk_nb)
  68. static const struct rockchip_cpuclk_rate_table *rockchip_get_cpuclk_settings(
  69. struct rockchip_cpuclk *cpuclk, unsigned long rate)
  70. {
  71. const struct rockchip_cpuclk_rate_table *rate_table =
  72. cpuclk->rate_table;
  73. int i;
  74. for (i = 0; i < cpuclk->rate_count; i++) {
  75. if (rate == rate_table[i].prate)
  76. return &rate_table[i];
  77. }
  78. return NULL;
  79. }
  80. static unsigned long rockchip_cpuclk_recalc_rate(struct clk_hw *hw,
  81. unsigned long parent_rate)
  82. {
  83. struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_hw(hw);
  84. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  85. u32 clksel0 = readl_relaxed(cpuclk->reg_base + reg_data->core_reg);
  86. clksel0 >>= reg_data->div_core_shift;
  87. clksel0 &= reg_data->div_core_mask;
  88. return parent_rate / (clksel0 + 1);
  89. }
  90. static const struct clk_ops rockchip_cpuclk_ops = {
  91. .recalc_rate = rockchip_cpuclk_recalc_rate,
  92. };
  93. static void rockchip_cpuclk_set_dividers(struct rockchip_cpuclk *cpuclk,
  94. const struct rockchip_cpuclk_rate_table *rate)
  95. {
  96. int i;
  97. /* alternate parent is active now. set the dividers */
  98. for (i = 0; i < ARRAY_SIZE(rate->divs); i++) {
  99. const struct rockchip_cpuclk_clksel *clksel = &rate->divs[i];
  100. if (!clksel->reg)
  101. continue;
  102. pr_debug("%s: setting reg 0x%x to 0x%x\n",
  103. __func__, clksel->reg, clksel->val);
  104. writel(clksel->val , cpuclk->reg_base + clksel->reg);
  105. }
  106. }
  107. static int rockchip_cpuclk_pre_rate_change(struct rockchip_cpuclk *cpuclk,
  108. struct clk_notifier_data *ndata)
  109. {
  110. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  111. unsigned long alt_prate, alt_div;
  112. unsigned long flags;
  113. alt_prate = clk_get_rate(cpuclk->alt_parent);
  114. spin_lock_irqsave(cpuclk->lock, flags);
  115. /*
  116. * If the old parent clock speed is less than the clock speed
  117. * of the alternate parent, then it should be ensured that at no point
  118. * the armclk speed is more than the old_rate until the dividers are
  119. * set.
  120. */
  121. if (alt_prate > ndata->old_rate) {
  122. /* calculate dividers */
  123. alt_div = DIV_ROUND_UP(alt_prate, ndata->old_rate) - 1;
  124. if (alt_div > reg_data->div_core_mask) {
  125. pr_warn("%s: limiting alt-divider %lu to %d\n",
  126. __func__, alt_div, reg_data->div_core_mask);
  127. alt_div = reg_data->div_core_mask;
  128. }
  129. /*
  130. * Change parents and add dividers in a single transaction.
  131. *
  132. * NOTE: we do this in a single transaction so we're never
  133. * dividing the primary parent by the extra dividers that were
  134. * needed for the alt.
  135. */
  136. pr_debug("%s: setting div %lu as alt-rate %lu > old-rate %lu\n",
  137. __func__, alt_div, alt_prate, ndata->old_rate);
  138. writel(HIWORD_UPDATE(alt_div, reg_data->div_core_mask,
  139. reg_data->div_core_shift) |
  140. HIWORD_UPDATE(1, 1, reg_data->mux_core_shift),
  141. cpuclk->reg_base + reg_data->core_reg);
  142. } else {
  143. /* select alternate parent */
  144. writel(HIWORD_UPDATE(1, 1, reg_data->mux_core_shift),
  145. cpuclk->reg_base + reg_data->core_reg);
  146. }
  147. spin_unlock_irqrestore(cpuclk->lock, flags);
  148. return 0;
  149. }
  150. static int rockchip_cpuclk_post_rate_change(struct rockchip_cpuclk *cpuclk,
  151. struct clk_notifier_data *ndata)
  152. {
  153. const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
  154. const struct rockchip_cpuclk_rate_table *rate;
  155. unsigned long flags;
  156. rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
  157. if (!rate) {
  158. pr_err("%s: Invalid rate : %lu for cpuclk\n",
  159. __func__, ndata->new_rate);
  160. return -EINVAL;
  161. }
  162. spin_lock_irqsave(cpuclk->lock, flags);
  163. if (ndata->old_rate < ndata->new_rate)
  164. rockchip_cpuclk_set_dividers(cpuclk, rate);
  165. /*
  166. * post-rate change event, re-mux to primary parent and remove dividers.
  167. *
  168. * NOTE: we do this in a single transaction so we're never dividing the
  169. * primary parent by the extra dividers that were needed for the alt.
  170. */
  171. writel(HIWORD_UPDATE(0, reg_data->div_core_mask,
  172. reg_data->div_core_shift) |
  173. HIWORD_UPDATE(0, 1, reg_data->mux_core_shift),
  174. cpuclk->reg_base + reg_data->core_reg);
  175. if (ndata->old_rate > ndata->new_rate)
  176. rockchip_cpuclk_set_dividers(cpuclk, rate);
  177. spin_unlock_irqrestore(cpuclk->lock, flags);
  178. return 0;
  179. }
  180. /*
  181. * This clock notifier is called when the frequency of the parent clock
  182. * of cpuclk is to be changed. This notifier handles the setting up all
  183. * the divider clocks, remux to temporary parent and handling the safe
  184. * frequency levels when using temporary parent.
  185. */
  186. static int rockchip_cpuclk_notifier_cb(struct notifier_block *nb,
  187. unsigned long event, void *data)
  188. {
  189. struct clk_notifier_data *ndata = data;
  190. struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_nb(nb);
  191. int ret = 0;
  192. pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
  193. __func__, event, ndata->old_rate, ndata->new_rate);
  194. if (event == PRE_RATE_CHANGE)
  195. ret = rockchip_cpuclk_pre_rate_change(cpuclk, ndata);
  196. else if (event == POST_RATE_CHANGE)
  197. ret = rockchip_cpuclk_post_rate_change(cpuclk, ndata);
  198. return notifier_from_errno(ret);
  199. }
  200. struct clk *rockchip_clk_register_cpuclk(const char *name,
  201. const char *const *parent_names, u8 num_parents,
  202. const struct rockchip_cpuclk_reg_data *reg_data,
  203. const struct rockchip_cpuclk_rate_table *rates,
  204. int nrates, void __iomem *reg_base, spinlock_t *lock)
  205. {
  206. struct rockchip_cpuclk *cpuclk;
  207. struct clk_init_data init;
  208. struct clk *clk, *cclk;
  209. int ret;
  210. if (num_parents != 2) {
  211. pr_err("%s: needs two parent clocks\n", __func__);
  212. return ERR_PTR(-EINVAL);
  213. }
  214. cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
  215. if (!cpuclk)
  216. return ERR_PTR(-ENOMEM);
  217. init.name = name;
  218. init.parent_names = &parent_names[0];
  219. init.num_parents = 1;
  220. init.ops = &rockchip_cpuclk_ops;
  221. /* only allow rate changes when we have a rate table */
  222. init.flags = (nrates > 0) ? CLK_SET_RATE_PARENT : 0;
  223. /* disallow automatic parent changes by ccf */
  224. init.flags |= CLK_SET_RATE_NO_REPARENT;
  225. init.flags |= CLK_GET_RATE_NOCACHE;
  226. cpuclk->reg_base = reg_base;
  227. cpuclk->lock = lock;
  228. cpuclk->reg_data = reg_data;
  229. cpuclk->clk_nb.notifier_call = rockchip_cpuclk_notifier_cb;
  230. cpuclk->hw.init = &init;
  231. cpuclk->alt_parent = __clk_lookup(parent_names[1]);
  232. if (!cpuclk->alt_parent) {
  233. pr_err("%s: could not lookup alternate parent\n",
  234. __func__);
  235. ret = -EINVAL;
  236. goto free_cpuclk;
  237. }
  238. ret = clk_prepare_enable(cpuclk->alt_parent);
  239. if (ret) {
  240. pr_err("%s: could not enable alternate parent\n",
  241. __func__);
  242. goto free_cpuclk;
  243. }
  244. clk = __clk_lookup(parent_names[0]);
  245. if (!clk) {
  246. pr_err("%s: could not lookup parent clock %s\n",
  247. __func__, parent_names[0]);
  248. ret = -EINVAL;
  249. goto free_cpuclk;
  250. }
  251. ret = clk_notifier_register(clk, &cpuclk->clk_nb);
  252. if (ret) {
  253. pr_err("%s: failed to register clock notifier for %s\n",
  254. __func__, name);
  255. goto free_cpuclk;
  256. }
  257. if (nrates > 0) {
  258. cpuclk->rate_count = nrates;
  259. cpuclk->rate_table = kmemdup(rates,
  260. sizeof(*rates) * nrates,
  261. GFP_KERNEL);
  262. if (!cpuclk->rate_table) {
  263. pr_err("%s: could not allocate memory for cpuclk rates\n",
  264. __func__);
  265. ret = -ENOMEM;
  266. goto unregister_notifier;
  267. }
  268. }
  269. cclk = clk_register(NULL, &cpuclk->hw);
  270. if (IS_ERR(clk)) {
  271. pr_err("%s: could not register cpuclk %s\n", __func__, name);
  272. ret = PTR_ERR(clk);
  273. goto free_rate_table;
  274. }
  275. return cclk;
  276. free_rate_table:
  277. kfree(cpuclk->rate_table);
  278. unregister_notifier:
  279. clk_notifier_unregister(clk, &cpuclk->clk_nb);
  280. free_cpuclk:
  281. kfree(cpuclk);
  282. return ERR_PTR(ret);
  283. }