clk-cpu.c 11 KB

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