clk-cpu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  4. * Author: Thomas Abraham <thomas.ab@samsung.com>
  5. *
  6. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  7. * Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
  8. *
  9. * This file contains the utility function to register CPU clock for Samsung
  10. * Exynos platforms. A CPU clock is defined as a clock supplied to a CPU or a
  11. * group of CPUs. 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 auxiliary 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 achieve a fast coordinated 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. * up to 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/delay.h>
  32. #include <linux/errno.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/clk.h>
  36. #include <linux/clk-provider.h>
  37. #include "clk.h"
  38. #include "clk-cpu.h"
  39. struct exynos_cpuclk;
  40. typedef int (*exynos_rate_change_fn_t)(struct clk_notifier_data *ndata,
  41. struct exynos_cpuclk *cpuclk);
  42. /**
  43. * struct exynos_cpuclk_regs - Register offsets for CPU related clocks
  44. * @mux_sel: offset of CPU MUX_SEL register (for selecting MUX clock parent)
  45. * @mux_stat: offset of CPU MUX_STAT register (for checking MUX clock status)
  46. * @div_cpu0: offset of CPU DIV0 register (for modifying divider values)
  47. * @div_cpu1: offset of CPU DIV1 register (for modifying divider values)
  48. * @div_stat_cpu0: offset of CPU DIV0_STAT register (for checking DIV status)
  49. * @div_stat_cpu1: offset of CPU DIV1_STAT register (for checking DIV status)
  50. * @mux: offset of MUX register for choosing CPU clock source
  51. * @divs: offsets of DIV registers (ACLK, ATCLK, PCLKDBG and PERIPHCLK)
  52. */
  53. struct exynos_cpuclk_regs {
  54. u32 mux_sel;
  55. u32 mux_stat;
  56. u32 div_cpu0;
  57. u32 div_cpu1;
  58. u32 div_stat_cpu0;
  59. u32 div_stat_cpu1;
  60. u32 mux;
  61. u32 divs[4];
  62. };
  63. /**
  64. * struct exynos_cpuclk_chip - Chip specific data for CPU clock
  65. * @regs: register offsets for CPU related clocks
  66. * @pre_rate_cb: callback to run before CPU clock rate change
  67. * @post_rate_cb: callback to run after CPU clock rate change
  68. */
  69. struct exynos_cpuclk_chip {
  70. const struct exynos_cpuclk_regs *regs;
  71. exynos_rate_change_fn_t pre_rate_cb;
  72. exynos_rate_change_fn_t post_rate_cb;
  73. };
  74. /**
  75. * struct exynos_cpuclk - information about clock supplied to a CPU core
  76. * @hw: handle between CCF and CPU clock
  77. * @alt_parent: alternate parent clock to use when switching the speed
  78. * of the primary parent clock
  79. * @base: start address of the CPU clock registers block
  80. * @lock: cpu clock domain register access lock
  81. * @cfg: cpu clock rate configuration data
  82. * @num_cfgs: number of array elements in @cfg array
  83. * @clk_nb: clock notifier registered for changes in clock speed of the
  84. * primary parent clock
  85. * @flags: configuration flags for the CPU clock
  86. * @chip: chip-specific data for the CPU clock
  87. *
  88. * This structure holds information required for programming the CPU clock for
  89. * various clock speeds.
  90. */
  91. struct exynos_cpuclk {
  92. struct clk_hw hw;
  93. const struct clk_hw *alt_parent;
  94. void __iomem *base;
  95. spinlock_t *lock;
  96. const struct exynos_cpuclk_cfg_data *cfg;
  97. const unsigned long num_cfgs;
  98. struct notifier_block clk_nb;
  99. unsigned long flags;
  100. const struct exynos_cpuclk_chip *chip;
  101. };
  102. /* ---- Common code --------------------------------------------------------- */
  103. /* Divider stabilization time, msec */
  104. #define MAX_STAB_TIME 10
  105. #define MAX_DIV 8
  106. #define DIV_MASK GENMASK(2, 0)
  107. #define DIV_MASK_ALL GENMASK(31, 0)
  108. #define MUX_MASK GENMASK(2, 0)
  109. /*
  110. * Helper function to wait until divider(s) have stabilized after the divider
  111. * value has changed.
  112. */
  113. static void wait_until_divider_stable(void __iomem *div_reg, unsigned long mask)
  114. {
  115. unsigned long timeout = jiffies + msecs_to_jiffies(MAX_STAB_TIME);
  116. do {
  117. if (!(readl(div_reg) & mask))
  118. return;
  119. } while (time_before(jiffies, timeout));
  120. if (!(readl(div_reg) & mask))
  121. return;
  122. pr_err("%s: timeout in divider stablization\n", __func__);
  123. }
  124. /*
  125. * Helper function to wait until mux has stabilized after the mux selection
  126. * value was changed.
  127. */
  128. static void wait_until_mux_stable(void __iomem *mux_reg, u32 mux_pos,
  129. unsigned long mask, unsigned long mux_value)
  130. {
  131. unsigned long timeout = jiffies + msecs_to_jiffies(MAX_STAB_TIME);
  132. do {
  133. if (((readl(mux_reg) >> mux_pos) & mask) == mux_value)
  134. return;
  135. } while (time_before(jiffies, timeout));
  136. if (((readl(mux_reg) >> mux_pos) & mask) == mux_value)
  137. return;
  138. pr_err("%s: re-parenting mux timed-out\n", __func__);
  139. }
  140. /*
  141. * Helper function to set the 'safe' dividers for the CPU clock. The parameters
  142. * div and mask contain the divider value and the register bit mask of the
  143. * dividers to be programmed.
  144. */
  145. static void exynos_set_safe_div(struct exynos_cpuclk *cpuclk, unsigned long div,
  146. unsigned long mask)
  147. {
  148. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  149. void __iomem *base = cpuclk->base;
  150. unsigned long div0;
  151. div0 = readl(base + regs->div_cpu0);
  152. div0 = (div0 & ~mask) | (div & mask);
  153. writel(div0, base + regs->div_cpu0);
  154. wait_until_divider_stable(base + regs->div_stat_cpu0, mask);
  155. }
  156. /* ---- Exynos 3/4/5 -------------------------------------------------------- */
  157. #define E4210_DIV0_RATIO0_MASK GENMASK(2, 0)
  158. #define E4210_DIV1_HPM_MASK GENMASK(6, 4)
  159. #define E4210_DIV1_COPY_MASK GENMASK(2, 0)
  160. #define E4210_MUX_HPM_MASK BIT(20)
  161. #define E4210_DIV0_ATB_SHIFT 16
  162. #define E4210_DIV0_ATB_MASK (DIV_MASK << E4210_DIV0_ATB_SHIFT)
  163. static const struct exynos_cpuclk_regs e4210_cpuclk_regs = {
  164. .mux_sel = 0x200,
  165. .mux_stat = 0x400,
  166. .div_cpu0 = 0x500,
  167. .div_cpu1 = 0x504,
  168. .div_stat_cpu0 = 0x600,
  169. .div_stat_cpu1 = 0x604,
  170. };
  171. /* handler for pre-rate change notification from parent clock */
  172. static int exynos_cpuclk_pre_rate_change(struct clk_notifier_data *ndata,
  173. struct exynos_cpuclk *cpuclk)
  174. {
  175. const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg;
  176. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  177. void __iomem *base = cpuclk->base;
  178. unsigned long alt_prate = clk_hw_get_rate(cpuclk->alt_parent);
  179. unsigned long div0, div1 = 0, mux_reg;
  180. unsigned long flags;
  181. /* find out the divider values to use for clock data */
  182. while ((cfg_data->prate * 1000) != ndata->new_rate) {
  183. if (cfg_data->prate == 0)
  184. return -EINVAL;
  185. cfg_data++;
  186. }
  187. spin_lock_irqsave(cpuclk->lock, flags);
  188. /*
  189. * For the selected PLL clock frequency, get the pre-defined divider
  190. * values. If the clock for sclk_hpm is not sourced from apll, then
  191. * the values for DIV_COPY and DIV_HPM dividers need not be set.
  192. */
  193. div0 = cfg_data->div0;
  194. if (cpuclk->flags & CLK_CPU_HAS_DIV1) {
  195. div1 = cfg_data->div1;
  196. if (readl(base + regs->mux_sel) & E4210_MUX_HPM_MASK)
  197. div1 = readl(base + regs->div_cpu1) &
  198. (E4210_DIV1_HPM_MASK | E4210_DIV1_COPY_MASK);
  199. }
  200. /*
  201. * If the old parent clock speed is less than the clock speed of
  202. * the alternate parent, then it should be ensured that at no point
  203. * the armclk speed is more than the old_prate until the dividers are
  204. * set. Also workaround the issue of the dividers being set to lower
  205. * values before the parent clock speed is set to new lower speed
  206. * (this can result in too high speed of armclk output clocks).
  207. */
  208. if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) {
  209. unsigned long tmp_rate = min(ndata->old_rate, ndata->new_rate);
  210. unsigned long alt_div, alt_div_mask = DIV_MASK;
  211. alt_div = DIV_ROUND_UP(alt_prate, tmp_rate) - 1;
  212. WARN_ON(alt_div >= MAX_DIV);
  213. if (cpuclk->flags & CLK_CPU_NEEDS_DEBUG_ALT_DIV) {
  214. /*
  215. * In Exynos4210, ATB clock parent is also mout_core. So
  216. * ATB clock also needs to be mantained at safe speed.
  217. */
  218. alt_div |= E4210_DIV0_ATB_MASK;
  219. alt_div_mask |= E4210_DIV0_ATB_MASK;
  220. }
  221. exynos_set_safe_div(cpuclk, alt_div, alt_div_mask);
  222. div0 |= alt_div;
  223. }
  224. /* select sclk_mpll as the alternate parent */
  225. mux_reg = readl(base + regs->mux_sel);
  226. writel(mux_reg | (1 << 16), base + regs->mux_sel);
  227. wait_until_mux_stable(base + regs->mux_stat, 16, MUX_MASK, 2);
  228. /* alternate parent is active now. set the dividers */
  229. writel(div0, base + regs->div_cpu0);
  230. wait_until_divider_stable(base + regs->div_stat_cpu0, DIV_MASK_ALL);
  231. if (cpuclk->flags & CLK_CPU_HAS_DIV1) {
  232. writel(div1, base + regs->div_cpu1);
  233. wait_until_divider_stable(base + regs->div_stat_cpu1,
  234. DIV_MASK_ALL);
  235. }
  236. spin_unlock_irqrestore(cpuclk->lock, flags);
  237. return 0;
  238. }
  239. /* handler for post-rate change notification from parent clock */
  240. static int exynos_cpuclk_post_rate_change(struct clk_notifier_data *ndata,
  241. struct exynos_cpuclk *cpuclk)
  242. {
  243. const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg;
  244. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  245. void __iomem *base = cpuclk->base;
  246. unsigned long div = 0, div_mask = DIV_MASK;
  247. unsigned long mux_reg;
  248. unsigned long flags;
  249. /* find out the divider values to use for clock data */
  250. if (cpuclk->flags & CLK_CPU_NEEDS_DEBUG_ALT_DIV) {
  251. while ((cfg_data->prate * 1000) != ndata->new_rate) {
  252. if (cfg_data->prate == 0)
  253. return -EINVAL;
  254. cfg_data++;
  255. }
  256. }
  257. spin_lock_irqsave(cpuclk->lock, flags);
  258. /* select mout_apll as the alternate parent */
  259. mux_reg = readl(base + regs->mux_sel);
  260. writel(mux_reg & ~(1 << 16), base + regs->mux_sel);
  261. wait_until_mux_stable(base + regs->mux_stat, 16, MUX_MASK, 1);
  262. if (cpuclk->flags & CLK_CPU_NEEDS_DEBUG_ALT_DIV) {
  263. div |= (cfg_data->div0 & E4210_DIV0_ATB_MASK);
  264. div_mask |= E4210_DIV0_ATB_MASK;
  265. }
  266. exynos_set_safe_div(cpuclk, div, div_mask);
  267. spin_unlock_irqrestore(cpuclk->lock, flags);
  268. return 0;
  269. }
  270. /* ---- Exynos5433 ---------------------------------------------------------- */
  271. static const struct exynos_cpuclk_regs e5433_cpuclk_regs = {
  272. .mux_sel = 0x208,
  273. .mux_stat = 0x408,
  274. .div_cpu0 = 0x600,
  275. .div_cpu1 = 0x604,
  276. .div_stat_cpu0 = 0x700,
  277. .div_stat_cpu1 = 0x704,
  278. };
  279. /* handler for pre-rate change notification from parent clock */
  280. static int exynos5433_cpuclk_pre_rate_change(struct clk_notifier_data *ndata,
  281. struct exynos_cpuclk *cpuclk)
  282. {
  283. const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg;
  284. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  285. void __iomem *base = cpuclk->base;
  286. unsigned long alt_prate = clk_hw_get_rate(cpuclk->alt_parent);
  287. unsigned long div0, div1 = 0, mux_reg;
  288. unsigned long flags;
  289. /* find out the divider values to use for clock data */
  290. while ((cfg_data->prate * 1000) != ndata->new_rate) {
  291. if (cfg_data->prate == 0)
  292. return -EINVAL;
  293. cfg_data++;
  294. }
  295. spin_lock_irqsave(cpuclk->lock, flags);
  296. /*
  297. * For the selected PLL clock frequency, get the pre-defined divider
  298. * values.
  299. */
  300. div0 = cfg_data->div0;
  301. div1 = cfg_data->div1;
  302. /*
  303. * If the old parent clock speed is less than the clock speed of
  304. * the alternate parent, then it should be ensured that at no point
  305. * the armclk speed is more than the old_prate until the dividers are
  306. * set. Also workaround the issue of the dividers being set to lower
  307. * values before the parent clock speed is set to new lower speed
  308. * (this can result in too high speed of armclk output clocks).
  309. */
  310. if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) {
  311. unsigned long tmp_rate = min(ndata->old_rate, ndata->new_rate);
  312. unsigned long alt_div, alt_div_mask = DIV_MASK;
  313. alt_div = DIV_ROUND_UP(alt_prate, tmp_rate) - 1;
  314. WARN_ON(alt_div >= MAX_DIV);
  315. exynos_set_safe_div(cpuclk, alt_div, alt_div_mask);
  316. div0 |= alt_div;
  317. }
  318. /* select the alternate parent */
  319. mux_reg = readl(base + regs->mux_sel);
  320. writel(mux_reg | 1, base + regs->mux_sel);
  321. wait_until_mux_stable(base + regs->mux_stat, 0, MUX_MASK, 2);
  322. /* alternate parent is active now. set the dividers */
  323. writel(div0, base + regs->div_cpu0);
  324. wait_until_divider_stable(base + regs->div_stat_cpu0, DIV_MASK_ALL);
  325. writel(div1, base + regs->div_cpu1);
  326. wait_until_divider_stable(base + regs->div_stat_cpu1, DIV_MASK_ALL);
  327. spin_unlock_irqrestore(cpuclk->lock, flags);
  328. return 0;
  329. }
  330. /* handler for post-rate change notification from parent clock */
  331. static int exynos5433_cpuclk_post_rate_change(struct clk_notifier_data *ndata,
  332. struct exynos_cpuclk *cpuclk)
  333. {
  334. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  335. void __iomem *base = cpuclk->base;
  336. unsigned long div = 0, div_mask = DIV_MASK;
  337. unsigned long mux_reg;
  338. unsigned long flags;
  339. spin_lock_irqsave(cpuclk->lock, flags);
  340. /* select apll as the alternate parent */
  341. mux_reg = readl(base + regs->mux_sel);
  342. writel(mux_reg & ~1, base + regs->mux_sel);
  343. wait_until_mux_stable(base + regs->mux_stat, 0, MUX_MASK, 1);
  344. exynos_set_safe_div(cpuclk, div, div_mask);
  345. spin_unlock_irqrestore(cpuclk->lock, flags);
  346. return 0;
  347. }
  348. /* ---- Exynos850 ----------------------------------------------------------- */
  349. #define E850_DIV_RATIO_MASK GENMASK(3, 0)
  350. #define E850_BUSY_MASK BIT(16)
  351. /* Max time for divider or mux to stabilize, usec */
  352. #define E850_DIV_MUX_STAB_TIME 100
  353. /* OSCCLK clock rate, Hz */
  354. #define E850_OSCCLK (26 * MHZ)
  355. static const struct exynos_cpuclk_regs e850cl0_cpuclk_regs = {
  356. .mux = 0x100c,
  357. .divs = { 0x1800, 0x1808, 0x180c, 0x1810 },
  358. };
  359. static const struct exynos_cpuclk_regs e850cl1_cpuclk_regs = {
  360. .mux = 0x1000,
  361. .divs = { 0x1800, 0x1808, 0x180c, 0x1810 },
  362. };
  363. /*
  364. * Set alternate parent rate to "rate" value or less.
  365. *
  366. * rate: Desired alt_parent rate, or 0 for max alt_parent rate
  367. *
  368. * Exynos850 doesn't have CPU clock divider in CMU_CPUCLx block (CMUREF divider
  369. * doesn't affect CPU speed). So CPUCLx_SWITCH divider from CMU_TOP is used
  370. * instead to adjust alternate parent speed.
  371. *
  372. * It's possible to use clk_set_max_rate() instead of this function, but it
  373. * would set overly pessimistic rate values to alternate parent.
  374. */
  375. static int exynos850_alt_parent_set_max_rate(const struct clk_hw *alt_parent,
  376. unsigned long rate)
  377. {
  378. struct clk_hw *clk_div, *clk_divp;
  379. unsigned long divp_rate, div_rate, div;
  380. int ret;
  381. /* Divider from CMU_TOP */
  382. clk_div = clk_hw_get_parent(alt_parent);
  383. if (!clk_div)
  384. return -ENOENT;
  385. /* Divider's parent from CMU_TOP */
  386. clk_divp = clk_hw_get_parent(clk_div);
  387. if (!clk_divp)
  388. return -ENOENT;
  389. /* Divider input rate */
  390. divp_rate = clk_hw_get_rate(clk_divp);
  391. if (!divp_rate)
  392. return -EINVAL;
  393. /* Calculate new alt_parent rate for integer divider value */
  394. if (rate == 0)
  395. div = 1;
  396. else
  397. div = DIV_ROUND_UP(divp_rate, rate);
  398. div_rate = DIV_ROUND_UP(divp_rate, div);
  399. WARN_ON(div >= MAX_DIV);
  400. /* alt_parent will propagate this change up to the divider */
  401. ret = clk_set_rate(alt_parent->clk, div_rate);
  402. if (ret)
  403. return ret;
  404. udelay(E850_DIV_MUX_STAB_TIME);
  405. return 0;
  406. }
  407. /* Handler for pre-rate change notification from parent clock */
  408. static int exynos850_cpuclk_pre_rate_change(struct clk_notifier_data *ndata,
  409. struct exynos_cpuclk *cpuclk)
  410. {
  411. const unsigned int shifts[4] = { 16, 12, 8, 4 }; /* E850_CPU_DIV0() */
  412. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  413. const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg;
  414. const struct clk_hw *alt_parent = cpuclk->alt_parent;
  415. void __iomem *base = cpuclk->base;
  416. unsigned long alt_prate = clk_hw_get_rate(alt_parent);
  417. unsigned long flags;
  418. u32 mux_reg;
  419. size_t i;
  420. int ret;
  421. /* No actions are needed when switching to or from OSCCLK parent */
  422. if (ndata->new_rate == E850_OSCCLK || ndata->old_rate == E850_OSCCLK)
  423. return 0;
  424. /* Find out the divider values to use for clock data */
  425. while ((cfg_data->prate * 1000) != ndata->new_rate) {
  426. if (cfg_data->prate == 0)
  427. return -EINVAL;
  428. cfg_data++;
  429. }
  430. /*
  431. * If the old parent clock speed is less than the clock speed of
  432. * the alternate parent, then it should be ensured that at no point
  433. * the armclk speed is more than the old_prate until the dividers are
  434. * set. Also workaround the issue of the dividers being set to lower
  435. * values before the parent clock speed is set to new lower speed
  436. * (this can result in too high speed of armclk output clocks).
  437. */
  438. if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) {
  439. unsigned long tmp_rate = min(ndata->old_rate, ndata->new_rate);
  440. ret = exynos850_alt_parent_set_max_rate(alt_parent, tmp_rate);
  441. if (ret)
  442. return ret;
  443. }
  444. spin_lock_irqsave(cpuclk->lock, flags);
  445. /* Select the alternate parent */
  446. mux_reg = readl(base + regs->mux);
  447. writel(mux_reg | 1, base + regs->mux);
  448. wait_until_mux_stable(base + regs->mux, 16, 1, 0);
  449. /* Alternate parent is active now. Set the dividers */
  450. for (i = 0; i < ARRAY_SIZE(shifts); ++i) {
  451. unsigned long div = (cfg_data->div0 >> shifts[i]) & 0xf;
  452. u32 val;
  453. val = readl(base + regs->divs[i]);
  454. val = (val & ~E850_DIV_RATIO_MASK) | div;
  455. writel(val, base + regs->divs[i]);
  456. wait_until_divider_stable(base + regs->divs[i], E850_BUSY_MASK);
  457. }
  458. spin_unlock_irqrestore(cpuclk->lock, flags);
  459. return 0;
  460. }
  461. /* Handler for post-rate change notification from parent clock */
  462. static int exynos850_cpuclk_post_rate_change(struct clk_notifier_data *ndata,
  463. struct exynos_cpuclk *cpuclk)
  464. {
  465. const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs;
  466. const struct clk_hw *alt_parent = cpuclk->alt_parent;
  467. void __iomem *base = cpuclk->base;
  468. unsigned long flags;
  469. u32 mux_reg;
  470. /* No actions are needed when switching to or from OSCCLK parent */
  471. if (ndata->new_rate == E850_OSCCLK || ndata->old_rate == E850_OSCCLK)
  472. return 0;
  473. spin_lock_irqsave(cpuclk->lock, flags);
  474. /* Select main parent (PLL) for mux */
  475. mux_reg = readl(base + regs->mux);
  476. writel(mux_reg & ~1, base + regs->mux);
  477. wait_until_mux_stable(base + regs->mux, 16, 1, 0);
  478. spin_unlock_irqrestore(cpuclk->lock, flags);
  479. /* Set alt_parent rate back to max */
  480. return exynos850_alt_parent_set_max_rate(alt_parent, 0);
  481. }
  482. /* -------------------------------------------------------------------------- */
  483. /* Common round rate callback usable for all types of CPU clocks */
  484. static long exynos_cpuclk_round_rate(struct clk_hw *hw, unsigned long drate,
  485. unsigned long *prate)
  486. {
  487. struct clk_hw *parent = clk_hw_get_parent(hw);
  488. *prate = clk_hw_round_rate(parent, drate);
  489. return *prate;
  490. }
  491. /* Common recalc rate callback usable for all types of CPU clocks */
  492. static unsigned long exynos_cpuclk_recalc_rate(struct clk_hw *hw,
  493. unsigned long parent_rate)
  494. {
  495. /*
  496. * The CPU clock output (armclk) rate is the same as its parent
  497. * rate. Although there exist certain dividers inside the CPU
  498. * clock block that could be used to divide the parent clock,
  499. * the driver does not make use of them currently, except during
  500. * frequency transitions.
  501. */
  502. return parent_rate;
  503. }
  504. static const struct clk_ops exynos_cpuclk_clk_ops = {
  505. .recalc_rate = exynos_cpuclk_recalc_rate,
  506. .round_rate = exynos_cpuclk_round_rate,
  507. };
  508. /*
  509. * This notifier function is called for the pre-rate and post-rate change
  510. * notifications of the parent clock of cpuclk.
  511. */
  512. static int exynos_cpuclk_notifier_cb(struct notifier_block *nb,
  513. unsigned long event, void *data)
  514. {
  515. struct clk_notifier_data *ndata = data;
  516. struct exynos_cpuclk *cpuclk;
  517. int err = 0;
  518. cpuclk = container_of(nb, struct exynos_cpuclk, clk_nb);
  519. if (event == PRE_RATE_CHANGE)
  520. err = cpuclk->chip->pre_rate_cb(ndata, cpuclk);
  521. else if (event == POST_RATE_CHANGE)
  522. err = cpuclk->chip->post_rate_cb(ndata, cpuclk);
  523. return notifier_from_errno(err);
  524. }
  525. static const struct exynos_cpuclk_chip exynos_clkcpu_chips[] = {
  526. [CPUCLK_LAYOUT_E4210] = {
  527. .regs = &e4210_cpuclk_regs,
  528. .pre_rate_cb = exynos_cpuclk_pre_rate_change,
  529. .post_rate_cb = exynos_cpuclk_post_rate_change,
  530. },
  531. [CPUCLK_LAYOUT_E5433] = {
  532. .regs = &e5433_cpuclk_regs,
  533. .pre_rate_cb = exynos5433_cpuclk_pre_rate_change,
  534. .post_rate_cb = exynos5433_cpuclk_post_rate_change,
  535. },
  536. [CPUCLK_LAYOUT_E850_CL0] = {
  537. .regs = &e850cl0_cpuclk_regs,
  538. .pre_rate_cb = exynos850_cpuclk_pre_rate_change,
  539. .post_rate_cb = exynos850_cpuclk_post_rate_change,
  540. },
  541. [CPUCLK_LAYOUT_E850_CL1] = {
  542. .regs = &e850cl1_cpuclk_regs,
  543. .pre_rate_cb = exynos850_cpuclk_pre_rate_change,
  544. .post_rate_cb = exynos850_cpuclk_post_rate_change,
  545. },
  546. };
  547. /* helper function to register a CPU clock */
  548. static int __init exynos_register_cpu_clock(struct samsung_clk_provider *ctx,
  549. const struct samsung_cpu_clock *clk_data)
  550. {
  551. const struct clk_hw *parent, *alt_parent;
  552. struct clk_hw **hws;
  553. struct exynos_cpuclk *cpuclk;
  554. struct clk_init_data init;
  555. const char *parent_name;
  556. unsigned int num_cfgs;
  557. int ret = 0;
  558. hws = ctx->clk_data.hws;
  559. parent = hws[clk_data->parent_id];
  560. alt_parent = hws[clk_data->alt_parent_id];
  561. if (IS_ERR(parent) || IS_ERR(alt_parent)) {
  562. pr_err("%s: invalid parent clock(s)\n", __func__);
  563. return -EINVAL;
  564. }
  565. cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
  566. if (!cpuclk)
  567. return -ENOMEM;
  568. parent_name = clk_hw_get_name(parent);
  569. init.name = clk_data->name;
  570. init.flags = CLK_SET_RATE_PARENT;
  571. init.parent_names = &parent_name;
  572. init.num_parents = 1;
  573. init.ops = &exynos_cpuclk_clk_ops;
  574. cpuclk->alt_parent = alt_parent;
  575. cpuclk->hw.init = &init;
  576. cpuclk->base = ctx->reg_base + clk_data->offset;
  577. cpuclk->lock = &ctx->lock;
  578. cpuclk->flags = clk_data->flags;
  579. cpuclk->clk_nb.notifier_call = exynos_cpuclk_notifier_cb;
  580. cpuclk->chip = &exynos_clkcpu_chips[clk_data->reg_layout];
  581. ret = clk_notifier_register(parent->clk, &cpuclk->clk_nb);
  582. if (ret) {
  583. pr_err("%s: failed to register clock notifier for %s\n",
  584. __func__, clk_data->name);
  585. goto free_cpuclk;
  586. }
  587. /* Find count of configuration rates in cfg */
  588. for (num_cfgs = 0; clk_data->cfg[num_cfgs].prate != 0; )
  589. num_cfgs++;
  590. cpuclk->cfg = kmemdup_array(clk_data->cfg, num_cfgs, sizeof(*cpuclk->cfg),
  591. GFP_KERNEL);
  592. if (!cpuclk->cfg) {
  593. ret = -ENOMEM;
  594. goto unregister_clk_nb;
  595. }
  596. ret = clk_hw_register(NULL, &cpuclk->hw);
  597. if (ret) {
  598. pr_err("%s: could not register cpuclk %s\n", __func__,
  599. clk_data->name);
  600. goto free_cpuclk_data;
  601. }
  602. samsung_clk_add_lookup(ctx, &cpuclk->hw, clk_data->id);
  603. return 0;
  604. free_cpuclk_data:
  605. kfree(cpuclk->cfg);
  606. unregister_clk_nb:
  607. clk_notifier_unregister(parent->clk, &cpuclk->clk_nb);
  608. free_cpuclk:
  609. kfree(cpuclk);
  610. return ret;
  611. }
  612. void __init samsung_clk_register_cpu(struct samsung_clk_provider *ctx,
  613. const struct samsung_cpu_clock *list, unsigned int nr_clk)
  614. {
  615. unsigned int idx;
  616. for (idx = 0; idx < nr_clk; idx++)
  617. exynos_register_cpu_clock(ctx, &list[idx]);
  618. }