clk-cpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  3. * Author: Thomas Abraham <thomas.ab@samsung.com>
  4. *
  5. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  6. * Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This file contains the utility function to register CPU clock for Samsung
  13. * Exynos platforms. A CPU clock is defined as a clock supplied to a CPU or a
  14. * group of CPUs. 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/errno.h>
  35. #include "clk-cpu.h"
  36. #define E4210_SRC_CPU 0x0
  37. #define E4210_STAT_CPU 0x200
  38. #define E4210_DIV_CPU0 0x300
  39. #define E4210_DIV_CPU1 0x304
  40. #define E4210_DIV_STAT_CPU0 0x400
  41. #define E4210_DIV_STAT_CPU1 0x404
  42. #define E4210_DIV0_RATIO0_MASK 0x7
  43. #define E4210_DIV1_HPM_MASK (0x7 << 4)
  44. #define E4210_DIV1_COPY_MASK (0x7 << 0)
  45. #define E4210_MUX_HPM_MASK (1 << 20)
  46. #define E4210_DIV0_ATB_SHIFT 16
  47. #define E4210_DIV0_ATB_MASK (DIV_MASK << E4210_DIV0_ATB_SHIFT)
  48. #define MAX_DIV 8
  49. #define DIV_MASK 7
  50. #define DIV_MASK_ALL 0xffffffff
  51. #define MUX_MASK 7
  52. /*
  53. * Helper function to wait until divider(s) have stabilized after the divider
  54. * value has changed.
  55. */
  56. static void wait_until_divider_stable(void __iomem *div_reg, unsigned long mask)
  57. {
  58. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  59. do {
  60. if (!(readl(div_reg) & mask))
  61. return;
  62. } while (time_before(jiffies, timeout));
  63. if (!(readl(div_reg) & mask))
  64. return;
  65. pr_err("%s: timeout in divider stablization\n", __func__);
  66. }
  67. /*
  68. * Helper function to wait until mux has stabilized after the mux selection
  69. * value was changed.
  70. */
  71. static void wait_until_mux_stable(void __iomem *mux_reg, u32 mux_pos,
  72. unsigned long mux_value)
  73. {
  74. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  75. do {
  76. if (((readl(mux_reg) >> mux_pos) & MUX_MASK) == mux_value)
  77. return;
  78. } while (time_before(jiffies, timeout));
  79. if (((readl(mux_reg) >> mux_pos) & MUX_MASK) == mux_value)
  80. return;
  81. pr_err("%s: re-parenting mux timed-out\n", __func__);
  82. }
  83. /* common round rate callback useable for all types of CPU clocks */
  84. static long exynos_cpuclk_round_rate(struct clk_hw *hw,
  85. unsigned long drate, unsigned long *prate)
  86. {
  87. struct clk *parent = __clk_get_parent(hw->clk);
  88. *prate = __clk_round_rate(parent, drate);
  89. return *prate;
  90. }
  91. /* common recalc rate callback useable for all types of CPU clocks */
  92. static unsigned long exynos_cpuclk_recalc_rate(struct clk_hw *hw,
  93. unsigned long parent_rate)
  94. {
  95. /*
  96. * The CPU clock output (armclk) rate is the same as its parent
  97. * rate. Although there exist certain dividers inside the CPU
  98. * clock block that could be used to divide the parent clock,
  99. * the driver does not make use of them currently, except during
  100. * frequency transitions.
  101. */
  102. return parent_rate;
  103. }
  104. static const struct clk_ops exynos_cpuclk_clk_ops = {
  105. .recalc_rate = exynos_cpuclk_recalc_rate,
  106. .round_rate = exynos_cpuclk_round_rate,
  107. };
  108. /*
  109. * Helper function to set the 'safe' dividers for the CPU clock. The parameters
  110. * div and mask contain the divider value and the register bit mask of the
  111. * dividers to be programmed.
  112. */
  113. static void exynos_set_safe_div(void __iomem *base, unsigned long div,
  114. unsigned long mask)
  115. {
  116. unsigned long div0;
  117. div0 = readl(base + E4210_DIV_CPU0);
  118. div0 = (div0 & ~mask) | (div & mask);
  119. writel(div0, base + E4210_DIV_CPU0);
  120. wait_until_divider_stable(base + E4210_DIV_STAT_CPU0, mask);
  121. }
  122. /* handler for pre-rate change notification from parent clock */
  123. static int exynos_cpuclk_pre_rate_change(struct clk_notifier_data *ndata,
  124. struct exynos_cpuclk *cpuclk, void __iomem *base)
  125. {
  126. const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg;
  127. unsigned long alt_prate = clk_get_rate(cpuclk->alt_parent);
  128. unsigned long alt_div = 0, alt_div_mask = DIV_MASK;
  129. unsigned long div0, div1 = 0, mux_reg;
  130. /* find out the divider values to use for clock data */
  131. while ((cfg_data->prate * 1000) != ndata->new_rate) {
  132. if (cfg_data->prate == 0)
  133. return -EINVAL;
  134. cfg_data++;
  135. }
  136. spin_lock(cpuclk->lock);
  137. /*
  138. * For the selected PLL clock frequency, get the pre-defined divider
  139. * values. If the clock for sclk_hpm is not sourced from apll, then
  140. * the values for DIV_COPY and DIV_HPM dividers need not be set.
  141. */
  142. div0 = cfg_data->div0;
  143. if (test_bit(CLK_CPU_HAS_DIV1, &cpuclk->flags)) {
  144. div1 = cfg_data->div1;
  145. if (readl(base + E4210_SRC_CPU) & E4210_MUX_HPM_MASK)
  146. div1 = readl(base + E4210_DIV_CPU1) &
  147. (E4210_DIV1_HPM_MASK | E4210_DIV1_COPY_MASK);
  148. }
  149. /*
  150. * If the old parent clock speed is less than the clock speed of
  151. * the alternate parent, then it should be ensured that at no point
  152. * the armclk speed is more than the old_prate until the dividers are
  153. * set. Also workaround the issue of the dividers being set to lower
  154. * values before the parent clock speed is set to new lower speed
  155. * (this can result in too high speed of armclk output clocks).
  156. */
  157. if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) {
  158. unsigned long tmp_rate = min(ndata->old_rate, ndata->new_rate);
  159. alt_div = DIV_ROUND_UP(alt_prate, tmp_rate) - 1;
  160. WARN_ON(alt_div >= MAX_DIV);
  161. if (test_bit(CLK_CPU_NEEDS_DEBUG_ALT_DIV, &cpuclk->flags)) {
  162. /*
  163. * In Exynos4210, ATB clock parent is also mout_core. So
  164. * ATB clock also needs to be mantained at safe speed.
  165. */
  166. alt_div |= E4210_DIV0_ATB_MASK;
  167. alt_div_mask |= E4210_DIV0_ATB_MASK;
  168. }
  169. exynos_set_safe_div(base, alt_div, alt_div_mask);
  170. div0 |= alt_div;
  171. }
  172. /* select sclk_mpll as the alternate parent */
  173. mux_reg = readl(base + E4210_SRC_CPU);
  174. writel(mux_reg | (1 << 16), base + E4210_SRC_CPU);
  175. wait_until_mux_stable(base + E4210_STAT_CPU, 16, 2);
  176. /* alternate parent is active now. set the dividers */
  177. writel(div0, base + E4210_DIV_CPU0);
  178. wait_until_divider_stable(base + E4210_DIV_STAT_CPU0, DIV_MASK_ALL);
  179. if (test_bit(CLK_CPU_HAS_DIV1, &cpuclk->flags)) {
  180. writel(div1, base + E4210_DIV_CPU1);
  181. wait_until_divider_stable(base + E4210_DIV_STAT_CPU1,
  182. DIV_MASK_ALL);
  183. }
  184. spin_unlock(cpuclk->lock);
  185. return 0;
  186. }
  187. /* handler for post-rate change notification from parent clock */
  188. static int exynos_cpuclk_post_rate_change(struct clk_notifier_data *ndata,
  189. struct exynos_cpuclk *cpuclk, void __iomem *base)
  190. {
  191. const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg;
  192. unsigned long div = 0, div_mask = DIV_MASK;
  193. unsigned long mux_reg;
  194. /* find out the divider values to use for clock data */
  195. if (test_bit(CLK_CPU_NEEDS_DEBUG_ALT_DIV, &cpuclk->flags)) {
  196. while ((cfg_data->prate * 1000) != ndata->new_rate) {
  197. if (cfg_data->prate == 0)
  198. return -EINVAL;
  199. cfg_data++;
  200. }
  201. }
  202. spin_lock(cpuclk->lock);
  203. /* select mout_apll as the alternate parent */
  204. mux_reg = readl(base + E4210_SRC_CPU);
  205. writel(mux_reg & ~(1 << 16), base + E4210_SRC_CPU);
  206. wait_until_mux_stable(base + E4210_STAT_CPU, 16, 1);
  207. if (test_bit(CLK_CPU_NEEDS_DEBUG_ALT_DIV, &cpuclk->flags)) {
  208. div |= (cfg_data->div0 & E4210_DIV0_ATB_MASK);
  209. div_mask |= E4210_DIV0_ATB_MASK;
  210. }
  211. exynos_set_safe_div(base, div, div_mask);
  212. spin_unlock(cpuclk->lock);
  213. return 0;
  214. }
  215. /*
  216. * This notifier function is called for the pre-rate and post-rate change
  217. * notifications of the parent clock of cpuclk.
  218. */
  219. static int exynos_cpuclk_notifier_cb(struct notifier_block *nb,
  220. unsigned long event, void *data)
  221. {
  222. struct clk_notifier_data *ndata = data;
  223. struct exynos_cpuclk *cpuclk;
  224. void __iomem *base;
  225. int err = 0;
  226. cpuclk = container_of(nb, struct exynos_cpuclk, clk_nb);
  227. base = cpuclk->ctrl_base;
  228. if (event == PRE_RATE_CHANGE)
  229. err = exynos_cpuclk_pre_rate_change(ndata, cpuclk, base);
  230. else if (event == POST_RATE_CHANGE)
  231. err = exynos_cpuclk_post_rate_change(ndata, cpuclk, base);
  232. return notifier_from_errno(err);
  233. }
  234. /* helper function to register a CPU clock */
  235. int __init exynos_register_cpu_clock(struct samsung_clk_provider *ctx,
  236. unsigned int lookup_id, const char *name, const char *parent,
  237. const char *alt_parent, unsigned long offset,
  238. const struct exynos_cpuclk_cfg_data *cfg,
  239. unsigned long num_cfgs, unsigned long flags)
  240. {
  241. struct exynos_cpuclk *cpuclk;
  242. struct clk_init_data init;
  243. struct clk *clk;
  244. int ret = 0;
  245. cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
  246. if (!cpuclk)
  247. return -ENOMEM;
  248. init.name = name;
  249. init.flags = CLK_SET_RATE_PARENT;
  250. init.parent_names = &parent;
  251. init.num_parents = 1;
  252. init.ops = &exynos_cpuclk_clk_ops;
  253. cpuclk->hw.init = &init;
  254. cpuclk->ctrl_base = ctx->reg_base + offset;
  255. cpuclk->lock = &ctx->lock;
  256. cpuclk->flags = flags;
  257. cpuclk->clk_nb.notifier_call = exynos_cpuclk_notifier_cb;
  258. cpuclk->alt_parent = __clk_lookup(alt_parent);
  259. if (!cpuclk->alt_parent) {
  260. pr_err("%s: could not lookup alternate parent %s\n",
  261. __func__, alt_parent);
  262. ret = -EINVAL;
  263. goto free_cpuclk;
  264. }
  265. clk = __clk_lookup(parent);
  266. if (!clk) {
  267. pr_err("%s: could not lookup parent clock %s\n",
  268. __func__, parent);
  269. ret = -EINVAL;
  270. goto free_cpuclk;
  271. }
  272. ret = clk_notifier_register(clk, &cpuclk->clk_nb);
  273. if (ret) {
  274. pr_err("%s: failed to register clock notifier for %s\n",
  275. __func__, name);
  276. goto free_cpuclk;
  277. }
  278. cpuclk->cfg = kmemdup(cfg, sizeof(*cfg) * num_cfgs, GFP_KERNEL);
  279. if (!cpuclk->cfg) {
  280. pr_err("%s: could not allocate memory for cpuclk data\n",
  281. __func__);
  282. ret = -ENOMEM;
  283. goto unregister_clk_nb;
  284. }
  285. clk = clk_register(NULL, &cpuclk->hw);
  286. if (IS_ERR(clk)) {
  287. pr_err("%s: could not register cpuclk %s\n", __func__, name);
  288. ret = PTR_ERR(clk);
  289. goto free_cpuclk_data;
  290. }
  291. samsung_clk_add_lookup(ctx, clk, lookup_id);
  292. return 0;
  293. free_cpuclk_data:
  294. kfree(cpuclk->cfg);
  295. unregister_clk_nb:
  296. clk_notifier_unregister(__clk_lookup(parent), &cpuclk->clk_nb);
  297. free_cpuclk:
  298. kfree(cpuclk);
  299. return ret;
  300. }