clk-cpu.c 15 KB

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