clk-cpu.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2015 Endless Mobile, Inc.
  3. * Author: Carlo Caione <carlo@endlessm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * CPU clock path:
  19. *
  20. * +-[/N]-----|3|
  21. * MUX2 +--[/3]-+----------|2| MUX1
  22. * [sys_pll]---|1| |--[/2]------------|1|-|1|
  23. * | |---+------------------|0| | |----- [a5_clk]
  24. * +--|0| | |
  25. * [xtal]---+-------------------------------|0|
  26. *
  27. *
  28. *
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/err.h>
  32. #include <linux/io.h>
  33. #include <linux/module.h>
  34. #include <linux/of_address.h>
  35. #include <linux/slab.h>
  36. #include <linux/clk.h>
  37. #include <linux/clk-provider.h>
  38. #define MESON_CPU_CLK_CNTL1 0x00
  39. #define MESON_CPU_CLK_CNTL 0x40
  40. #define MESON_CPU_CLK_MUX1 BIT(7)
  41. #define MESON_CPU_CLK_MUX2 BIT(0)
  42. #define MESON_N_WIDTH 9
  43. #define MESON_N_SHIFT 20
  44. #define MESON_SEL_WIDTH 2
  45. #define MESON_SEL_SHIFT 2
  46. #include "clkc.h"
  47. struct meson_clk_cpu {
  48. struct notifier_block clk_nb;
  49. const struct clk_div_table *div_table;
  50. struct clk_hw hw;
  51. void __iomem *base;
  52. u16 reg_off;
  53. };
  54. #define to_meson_clk_cpu_hw(_hw) container_of(_hw, struct meson_clk_cpu, hw)
  55. #define to_meson_clk_cpu_nb(_nb) container_of(_nb, struct meson_clk_cpu, clk_nb)
  56. static long meson_clk_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long *prate)
  58. {
  59. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_hw(hw);
  60. return divider_round_rate(hw, rate, prate, clk_cpu->div_table,
  61. MESON_N_WIDTH, CLK_DIVIDER_ROUND_CLOSEST);
  62. }
  63. static int meson_clk_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
  64. unsigned long parent_rate)
  65. {
  66. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_hw(hw);
  67. unsigned int div, sel, N = 0;
  68. u32 reg;
  69. div = DIV_ROUND_UP(parent_rate, rate);
  70. if (div <= 3) {
  71. sel = div - 1;
  72. } else {
  73. sel = 3;
  74. N = div / 2;
  75. }
  76. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL1);
  77. reg = PARM_SET(MESON_N_WIDTH, MESON_N_SHIFT, reg, N);
  78. writel(reg, clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL1);
  79. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL);
  80. reg = PARM_SET(MESON_SEL_WIDTH, MESON_SEL_SHIFT, reg, sel);
  81. writel(reg, clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL);
  82. return 0;
  83. }
  84. static unsigned long meson_clk_cpu_recalc_rate(struct clk_hw *hw,
  85. unsigned long parent_rate)
  86. {
  87. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_hw(hw);
  88. unsigned int N, sel;
  89. unsigned int div = 1;
  90. u32 reg;
  91. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL1);
  92. N = PARM_GET(MESON_N_WIDTH, MESON_N_SHIFT, reg);
  93. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL);
  94. sel = PARM_GET(MESON_SEL_WIDTH, MESON_SEL_SHIFT, reg);
  95. if (sel < 3)
  96. div = sel + 1;
  97. else
  98. div = 2 * N;
  99. return parent_rate / div;
  100. }
  101. static int meson_clk_cpu_pre_rate_change(struct meson_clk_cpu *clk_cpu,
  102. struct clk_notifier_data *ndata)
  103. {
  104. u32 cpu_clk_cntl;
  105. /* switch MUX1 to xtal */
  106. cpu_clk_cntl = readl(clk_cpu->base + clk_cpu->reg_off
  107. + MESON_CPU_CLK_CNTL);
  108. cpu_clk_cntl &= ~MESON_CPU_CLK_MUX1;
  109. writel(cpu_clk_cntl, clk_cpu->base + clk_cpu->reg_off
  110. + MESON_CPU_CLK_CNTL);
  111. udelay(100);
  112. /* switch MUX2 to sys-pll */
  113. cpu_clk_cntl |= MESON_CPU_CLK_MUX2;
  114. writel(cpu_clk_cntl, clk_cpu->base + clk_cpu->reg_off
  115. + MESON_CPU_CLK_CNTL);
  116. return 0;
  117. }
  118. static int meson_clk_cpu_post_rate_change(struct meson_clk_cpu *clk_cpu,
  119. struct clk_notifier_data *ndata)
  120. {
  121. u32 cpu_clk_cntl;
  122. /* switch MUX1 to divisors' output */
  123. cpu_clk_cntl = readl(clk_cpu->base + clk_cpu->reg_off
  124. + MESON_CPU_CLK_CNTL);
  125. cpu_clk_cntl |= MESON_CPU_CLK_MUX1;
  126. writel(cpu_clk_cntl, clk_cpu->base + clk_cpu->reg_off
  127. + MESON_CPU_CLK_CNTL);
  128. udelay(100);
  129. return 0;
  130. }
  131. /*
  132. * This clock notifier is called when the frequency of the of the parent
  133. * PLL clock is to be changed. We use the xtal input as temporary parent
  134. * while the PLL frequency is stabilized.
  135. */
  136. static int meson_clk_cpu_notifier_cb(struct notifier_block *nb,
  137. unsigned long event, void *data)
  138. {
  139. struct clk_notifier_data *ndata = data;
  140. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_nb(nb);
  141. int ret = 0;
  142. if (event == PRE_RATE_CHANGE)
  143. ret = meson_clk_cpu_pre_rate_change(clk_cpu, ndata);
  144. else if (event == POST_RATE_CHANGE)
  145. ret = meson_clk_cpu_post_rate_change(clk_cpu, ndata);
  146. return notifier_from_errno(ret);
  147. }
  148. static const struct clk_ops meson_clk_cpu_ops = {
  149. .recalc_rate = meson_clk_cpu_recalc_rate,
  150. .round_rate = meson_clk_cpu_round_rate,
  151. .set_rate = meson_clk_cpu_set_rate,
  152. };
  153. struct clk *meson_clk_register_cpu(const struct clk_conf *clk_conf,
  154. void __iomem *reg_base,
  155. spinlock_t *lock)
  156. {
  157. struct clk *clk;
  158. struct clk *pclk;
  159. struct meson_clk_cpu *clk_cpu;
  160. struct clk_init_data init;
  161. int ret;
  162. clk_cpu = kzalloc(sizeof(*clk_cpu), GFP_KERNEL);
  163. if (!clk_cpu)
  164. return ERR_PTR(-ENOMEM);
  165. clk_cpu->base = reg_base;
  166. clk_cpu->reg_off = clk_conf->reg_off;
  167. clk_cpu->div_table = clk_conf->conf.div_table;
  168. clk_cpu->clk_nb.notifier_call = meson_clk_cpu_notifier_cb;
  169. init.name = clk_conf->clk_name;
  170. init.ops = &meson_clk_cpu_ops;
  171. init.flags = clk_conf->flags | CLK_GET_RATE_NOCACHE;
  172. init.flags |= CLK_SET_RATE_PARENT;
  173. init.parent_names = clk_conf->clks_parent;
  174. init.num_parents = 1;
  175. clk_cpu->hw.init = &init;
  176. pclk = __clk_lookup(clk_conf->clks_parent[0]);
  177. if (!pclk) {
  178. pr_err("%s: could not lookup parent clock %s\n",
  179. __func__, clk_conf->clks_parent[0]);
  180. ret = -EINVAL;
  181. goto free_clk;
  182. }
  183. ret = clk_notifier_register(pclk, &clk_cpu->clk_nb);
  184. if (ret) {
  185. pr_err("%s: failed to register clock notifier for %s\n",
  186. __func__, clk_conf->clk_name);
  187. goto free_clk;
  188. }
  189. clk = clk_register(NULL, &clk_cpu->hw);
  190. if (IS_ERR(clk)) {
  191. ret = PTR_ERR(clk);
  192. goto unregister_clk_nb;
  193. }
  194. return clk;
  195. unregister_clk_nb:
  196. clk_notifier_unregister(pclk, &clk_cpu->clk_nb);
  197. free_clk:
  198. kfree(clk_cpu);
  199. return ERR_PTR(ret);
  200. }