clk-cpu.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #define to_meson_clk_cpu_hw(_hw) container_of(_hw, struct meson_clk_cpu, hw)
  48. #define to_meson_clk_cpu_nb(_nb) container_of(_nb, struct meson_clk_cpu, clk_nb)
  49. static long meson_clk_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
  50. unsigned long *prate)
  51. {
  52. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_hw(hw);
  53. return divider_round_rate(hw, rate, prate, clk_cpu->div_table,
  54. MESON_N_WIDTH, CLK_DIVIDER_ROUND_CLOSEST);
  55. }
  56. static int meson_clk_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long parent_rate)
  58. {
  59. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_hw(hw);
  60. unsigned int div, sel, N = 0;
  61. u32 reg;
  62. div = DIV_ROUND_UP(parent_rate, rate);
  63. if (div <= 3) {
  64. sel = div - 1;
  65. } else {
  66. sel = 3;
  67. N = div / 2;
  68. }
  69. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL1);
  70. reg = PARM_SET(MESON_N_WIDTH, MESON_N_SHIFT, reg, N);
  71. writel(reg, clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL1);
  72. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL);
  73. reg = PARM_SET(MESON_SEL_WIDTH, MESON_SEL_SHIFT, reg, sel);
  74. writel(reg, clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL);
  75. return 0;
  76. }
  77. static unsigned long meson_clk_cpu_recalc_rate(struct clk_hw *hw,
  78. unsigned long parent_rate)
  79. {
  80. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_hw(hw);
  81. unsigned int N, sel;
  82. unsigned int div = 1;
  83. u32 reg;
  84. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL1);
  85. N = PARM_GET(MESON_N_WIDTH, MESON_N_SHIFT, reg);
  86. reg = readl(clk_cpu->base + clk_cpu->reg_off + MESON_CPU_CLK_CNTL);
  87. sel = PARM_GET(MESON_SEL_WIDTH, MESON_SEL_SHIFT, reg);
  88. if (sel < 3)
  89. div = sel + 1;
  90. else
  91. div = 2 * N;
  92. return parent_rate / div;
  93. }
  94. /* FIXME MUX1 & MUX2 should be struct clk_hw objects */
  95. static int meson_clk_cpu_pre_rate_change(struct meson_clk_cpu *clk_cpu,
  96. struct clk_notifier_data *ndata)
  97. {
  98. u32 cpu_clk_cntl;
  99. /* switch MUX1 to xtal */
  100. cpu_clk_cntl = readl(clk_cpu->base + clk_cpu->reg_off
  101. + MESON_CPU_CLK_CNTL);
  102. cpu_clk_cntl &= ~MESON_CPU_CLK_MUX1;
  103. writel(cpu_clk_cntl, clk_cpu->base + clk_cpu->reg_off
  104. + MESON_CPU_CLK_CNTL);
  105. udelay(100);
  106. /* switch MUX2 to sys-pll */
  107. cpu_clk_cntl |= MESON_CPU_CLK_MUX2;
  108. writel(cpu_clk_cntl, clk_cpu->base + clk_cpu->reg_off
  109. + MESON_CPU_CLK_CNTL);
  110. return 0;
  111. }
  112. /* FIXME MUX1 & MUX2 should be struct clk_hw objects */
  113. static int meson_clk_cpu_post_rate_change(struct meson_clk_cpu *clk_cpu,
  114. struct clk_notifier_data *ndata)
  115. {
  116. u32 cpu_clk_cntl;
  117. /* switch MUX1 to divisors' output */
  118. cpu_clk_cntl = readl(clk_cpu->base + clk_cpu->reg_off
  119. + MESON_CPU_CLK_CNTL);
  120. cpu_clk_cntl |= MESON_CPU_CLK_MUX1;
  121. writel(cpu_clk_cntl, clk_cpu->base + clk_cpu->reg_off
  122. + MESON_CPU_CLK_CNTL);
  123. udelay(100);
  124. return 0;
  125. }
  126. /*
  127. * This clock notifier is called when the frequency of the of the parent
  128. * PLL clock is to be changed. We use the xtal input as temporary parent
  129. * while the PLL frequency is stabilized.
  130. */
  131. int meson_clk_cpu_notifier_cb(struct notifier_block *nb,
  132. unsigned long event, void *data)
  133. {
  134. struct clk_notifier_data *ndata = data;
  135. struct meson_clk_cpu *clk_cpu = to_meson_clk_cpu_nb(nb);
  136. int ret = 0;
  137. if (event == PRE_RATE_CHANGE)
  138. ret = meson_clk_cpu_pre_rate_change(clk_cpu, ndata);
  139. else if (event == POST_RATE_CHANGE)
  140. ret = meson_clk_cpu_post_rate_change(clk_cpu, ndata);
  141. return notifier_from_errno(ret);
  142. }
  143. const struct clk_ops meson_clk_cpu_ops = {
  144. .recalc_rate = meson_clk_cpu_recalc_rate,
  145. .round_rate = meson_clk_cpu_round_rate,
  146. .set_rate = meson_clk_cpu_set_rate,
  147. };