clk-pll.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * In the most basic form, a Meson PLL is composed as follows:
  19. *
  20. * PLL
  21. * +------------------------------+
  22. * | |
  23. * in -----[ /N ]---[ *M ]---[ >>OD ]----->> out
  24. * | ^ ^ |
  25. * +------------------------------+
  26. * | |
  27. * FREF VCO
  28. *
  29. * out = (in * M / N) >> OD
  30. */
  31. #include <linux/clk-provider.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/io.h>
  35. #include <linux/module.h>
  36. #include <linux/of_address.h>
  37. #include <linux/slab.h>
  38. #include <linux/string.h>
  39. #include "clkc.h"
  40. #define MESON_PLL_RESET BIT(29)
  41. #define MESON_PLL_LOCK BIT(31)
  42. #define to_meson_clk_pll(_hw) container_of(_hw, struct meson_clk_pll, hw)
  43. static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  47. struct parm *p;
  48. unsigned long parent_rate_mhz = parent_rate / 1000000;
  49. unsigned long rate_mhz;
  50. u16 n, m, frac = 0, od, od2 = 0;
  51. u32 reg;
  52. p = &pll->n;
  53. reg = readl(pll->base + p->reg_off);
  54. n = PARM_GET(p->width, p->shift, reg);
  55. p = &pll->m;
  56. reg = readl(pll->base + p->reg_off);
  57. m = PARM_GET(p->width, p->shift, reg);
  58. p = &pll->od;
  59. reg = readl(pll->base + p->reg_off);
  60. od = PARM_GET(p->width, p->shift, reg);
  61. p = &pll->od2;
  62. if (p->width) {
  63. reg = readl(pll->base + p->reg_off);
  64. od2 = PARM_GET(p->width, p->shift, reg);
  65. }
  66. p = &pll->frac;
  67. if (p->width) {
  68. reg = readl(pll->base + p->reg_off);
  69. frac = PARM_GET(p->width, p->shift, reg);
  70. rate_mhz = (parent_rate_mhz * m + \
  71. (parent_rate_mhz * frac >> 12)) * 2 / n;
  72. rate_mhz = rate_mhz >> od >> od2;
  73. } else
  74. rate_mhz = (parent_rate_mhz * m / n) >> od >> od2;
  75. return rate_mhz * 1000000;
  76. }
  77. static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  78. unsigned long *parent_rate)
  79. {
  80. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  81. const struct pll_rate_table *rate_table = pll->rate_table;
  82. int i;
  83. for (i = 0; i < pll->rate_count; i++) {
  84. if (rate <= rate_table[i].rate)
  85. return rate_table[i].rate;
  86. }
  87. /* else return the smallest value */
  88. return rate_table[0].rate;
  89. }
  90. static const struct pll_rate_table *meson_clk_get_pll_settings(struct meson_clk_pll *pll,
  91. unsigned long rate)
  92. {
  93. const struct pll_rate_table *rate_table = pll->rate_table;
  94. int i;
  95. for (i = 0; i < pll->rate_count; i++) {
  96. if (rate == rate_table[i].rate)
  97. return &rate_table[i];
  98. }
  99. return NULL;
  100. }
  101. static int meson_clk_pll_wait_lock(struct meson_clk_pll *pll,
  102. struct parm *p_n)
  103. {
  104. int delay = 24000000;
  105. u32 reg;
  106. while (delay > 0) {
  107. reg = readl(pll->base + p_n->reg_off);
  108. if (reg & MESON_PLL_LOCK)
  109. return 0;
  110. delay--;
  111. }
  112. return -ETIMEDOUT;
  113. }
  114. static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  115. unsigned long parent_rate)
  116. {
  117. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  118. struct parm *p;
  119. const struct pll_rate_table *rate_set;
  120. unsigned long old_rate;
  121. int ret = 0;
  122. u32 reg;
  123. if (parent_rate == 0 || rate == 0)
  124. return -EINVAL;
  125. old_rate = rate;
  126. rate_set = meson_clk_get_pll_settings(pll, rate);
  127. if (!rate_set)
  128. return -EINVAL;
  129. /* PLL reset */
  130. p = &pll->n;
  131. reg = readl(pll->base + p->reg_off);
  132. writel(reg | MESON_PLL_RESET, pll->base + p->reg_off);
  133. reg = PARM_SET(p->width, p->shift, reg, rate_set->n);
  134. writel(reg, pll->base + p->reg_off);
  135. p = &pll->m;
  136. reg = readl(pll->base + p->reg_off);
  137. reg = PARM_SET(p->width, p->shift, reg, rate_set->m);
  138. writel(reg, pll->base + p->reg_off);
  139. p = &pll->od;
  140. reg = readl(pll->base + p->reg_off);
  141. reg = PARM_SET(p->width, p->shift, reg, rate_set->od);
  142. writel(reg, pll->base + p->reg_off);
  143. p = &pll->od2;
  144. if (p->width) {
  145. reg = readl(pll->base + p->reg_off);
  146. reg = PARM_SET(p->width, p->shift, reg, rate_set->od2);
  147. writel(reg, pll->base + p->reg_off);
  148. }
  149. p = &pll->frac;
  150. if (p->width) {
  151. reg = readl(pll->base + p->reg_off);
  152. reg = PARM_SET(p->width, p->shift, reg, rate_set->frac);
  153. writel(reg, pll->base + p->reg_off);
  154. }
  155. p = &pll->n;
  156. ret = meson_clk_pll_wait_lock(pll, p);
  157. if (ret) {
  158. pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
  159. __func__, old_rate);
  160. meson_clk_pll_set_rate(hw, old_rate, parent_rate);
  161. }
  162. return ret;
  163. }
  164. const struct clk_ops meson_clk_pll_ops = {
  165. .recalc_rate = meson_clk_pll_recalc_rate,
  166. .round_rate = meson_clk_pll_round_rate,
  167. .set_rate = meson_clk_pll_set_rate,
  168. };
  169. const struct clk_ops meson_clk_pll_ro_ops = {
  170. .recalc_rate = meson_clk_pll_recalc_rate,
  171. };