clk-cpu.c 11 KB

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