clk-pll.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 Endless Mobile, Inc.
  4. * Author: Carlo Caione <carlo@endlessm.com>
  5. *
  6. * Copyright (c) 2018 Baylibre, SAS.
  7. * Author: Jerome Brunet <jbrunet@baylibre.com>
  8. */
  9. /*
  10. * In the most basic form, a Meson PLL is composed as follows:
  11. *
  12. * PLL
  13. * +------------------------------+
  14. * | |
  15. * in -----[ /N ]---[ *M ]---[ >>OD ]----->> out
  16. * | ^ ^ |
  17. * +------------------------------+
  18. * | |
  19. * FREF VCO
  20. *
  21. * out = in * (m + frac / frac_max) / (n << sum(ods))
  22. */
  23. #include <linux/clk-provider.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/math64.h>
  28. #include <linux/module.h>
  29. #include <linux/of_address.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include "clkc.h"
  33. static inline struct meson_clk_pll_data *
  34. meson_clk_pll_data(struct clk_regmap *clk)
  35. {
  36. return (struct meson_clk_pll_data *)clk->data;
  37. }
  38. static unsigned long __pll_params_to_rate(unsigned long parent_rate,
  39. const struct pll_rate_table *pllt,
  40. u16 frac,
  41. struct meson_clk_pll_data *pll)
  42. {
  43. u64 rate = (u64)parent_rate * pllt->m;
  44. unsigned int od = pllt->od + pllt->od2 + pllt->od3;
  45. if (frac && MESON_PARM_APPLICABLE(&pll->frac)) {
  46. u64 frac_rate = (u64)parent_rate * frac;
  47. rate += DIV_ROUND_UP_ULL(frac_rate,
  48. (1 << pll->frac.width));
  49. }
  50. return DIV_ROUND_UP_ULL(rate, pllt->n << od);
  51. }
  52. static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
  53. unsigned long parent_rate)
  54. {
  55. struct clk_regmap *clk = to_clk_regmap(hw);
  56. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  57. struct pll_rate_table pllt;
  58. u16 frac;
  59. pllt.n = meson_parm_read(clk->map, &pll->n);
  60. pllt.m = meson_parm_read(clk->map, &pll->m);
  61. pllt.od = meson_parm_read(clk->map, &pll->od);
  62. pllt.od2 = MESON_PARM_APPLICABLE(&pll->od2) ?
  63. meson_parm_read(clk->map, &pll->od2) :
  64. 0;
  65. pllt.od3 = MESON_PARM_APPLICABLE(&pll->od3) ?
  66. meson_parm_read(clk->map, &pll->od3) :
  67. 0;
  68. frac = MESON_PARM_APPLICABLE(&pll->frac) ?
  69. meson_parm_read(clk->map, &pll->frac) :
  70. 0;
  71. return __pll_params_to_rate(parent_rate, &pllt, frac, pll);
  72. }
  73. static u16 __pll_params_with_frac(unsigned long rate,
  74. unsigned long parent_rate,
  75. const struct pll_rate_table *pllt,
  76. struct meson_clk_pll_data *pll)
  77. {
  78. u16 frac_max = (1 << pll->frac.width);
  79. u64 val = (u64)rate * pllt->n;
  80. val <<= pllt->od + pllt->od2 + pllt->od3;
  81. if (pll->flags & CLK_MESON_PLL_ROUND_CLOSEST)
  82. val = DIV_ROUND_CLOSEST_ULL(val * frac_max, parent_rate);
  83. else
  84. val = div_u64(val * frac_max, parent_rate);
  85. val -= pllt->m * frac_max;
  86. return min((u16)val, (u16)(frac_max - 1));
  87. }
  88. static const struct pll_rate_table *
  89. meson_clk_get_pll_settings(unsigned long rate,
  90. struct meson_clk_pll_data *pll)
  91. {
  92. const struct pll_rate_table *table = pll->table;
  93. unsigned int i = 0;
  94. if (!table)
  95. return NULL;
  96. /* Find the first table element exceeding rate */
  97. while (table[i].rate && table[i].rate <= rate)
  98. i++;
  99. if (i != 0) {
  100. if (MESON_PARM_APPLICABLE(&pll->frac) ||
  101. !(pll->flags & CLK_MESON_PLL_ROUND_CLOSEST) ||
  102. (abs(rate - table[i - 1].rate) <
  103. abs(rate - table[i].rate)))
  104. i--;
  105. }
  106. return (struct pll_rate_table *)&table[i];
  107. }
  108. static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  109. unsigned long *parent_rate)
  110. {
  111. struct clk_regmap *clk = to_clk_regmap(hw);
  112. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  113. const struct pll_rate_table *pllt =
  114. meson_clk_get_pll_settings(rate, pll);
  115. u16 frac;
  116. if (!pllt)
  117. return meson_clk_pll_recalc_rate(hw, *parent_rate);
  118. if (!MESON_PARM_APPLICABLE(&pll->frac)
  119. || rate == pllt->rate)
  120. return pllt->rate;
  121. /*
  122. * The rate provided by the setting is not an exact match, let's
  123. * try to improve the result using the fractional parameter
  124. */
  125. frac = __pll_params_with_frac(rate, *parent_rate, pllt, pll);
  126. return __pll_params_to_rate(*parent_rate, pllt, frac, pll);
  127. }
  128. static int meson_clk_pll_wait_lock(struct clk_hw *hw)
  129. {
  130. struct clk_regmap *clk = to_clk_regmap(hw);
  131. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  132. int delay = 24000000;
  133. do {
  134. /* Is the clock locked now ? */
  135. if (meson_parm_read(clk->map, &pll->l))
  136. return 0;
  137. delay--;
  138. } while (delay > 0);
  139. return -ETIMEDOUT;
  140. }
  141. static void meson_clk_pll_init(struct clk_hw *hw)
  142. {
  143. struct clk_regmap *clk = to_clk_regmap(hw);
  144. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  145. if (pll->init_count) {
  146. meson_parm_write(clk->map, &pll->rst, 1);
  147. regmap_multi_reg_write(clk->map, pll->init_regs,
  148. pll->init_count);
  149. meson_parm_write(clk->map, &pll->rst, 0);
  150. }
  151. }
  152. static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  153. unsigned long parent_rate)
  154. {
  155. struct clk_regmap *clk = to_clk_regmap(hw);
  156. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  157. const struct pll_rate_table *pllt;
  158. unsigned long old_rate;
  159. u16 frac = 0;
  160. if (parent_rate == 0 || rate == 0)
  161. return -EINVAL;
  162. old_rate = rate;
  163. pllt = meson_clk_get_pll_settings(rate, pll);
  164. if (!pllt)
  165. return -EINVAL;
  166. /* Put the pll in reset to write the params */
  167. meson_parm_write(clk->map, &pll->rst, 1);
  168. meson_parm_write(clk->map, &pll->n, pllt->n);
  169. meson_parm_write(clk->map, &pll->m, pllt->m);
  170. meson_parm_write(clk->map, &pll->od, pllt->od);
  171. if (MESON_PARM_APPLICABLE(&pll->od2))
  172. meson_parm_write(clk->map, &pll->od2, pllt->od2);
  173. if (MESON_PARM_APPLICABLE(&pll->od3))
  174. meson_parm_write(clk->map, &pll->od3, pllt->od3);
  175. if (MESON_PARM_APPLICABLE(&pll->frac)) {
  176. frac = __pll_params_with_frac(rate, parent_rate, pllt, pll);
  177. meson_parm_write(clk->map, &pll->frac, frac);
  178. }
  179. /* make sure the reset is cleared at this point */
  180. meson_parm_write(clk->map, &pll->rst, 0);
  181. if (meson_clk_pll_wait_lock(hw)) {
  182. pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
  183. __func__, old_rate);
  184. /*
  185. * FIXME: Do we really need/want this HACK ?
  186. * It looks unsafe. what happens if the clock gets into a
  187. * broken state and we can't lock back on the old_rate ? Looks
  188. * like an infinite recursion is possible
  189. */
  190. meson_clk_pll_set_rate(hw, old_rate, parent_rate);
  191. }
  192. return 0;
  193. }
  194. const struct clk_ops meson_clk_pll_ops = {
  195. .init = meson_clk_pll_init,
  196. .recalc_rate = meson_clk_pll_recalc_rate,
  197. .round_rate = meson_clk_pll_round_rate,
  198. .set_rate = meson_clk_pll_set_rate,
  199. };
  200. const struct clk_ops meson_clk_pll_ro_ops = {
  201. .recalc_rate = meson_clk_pll_recalc_rate,
  202. };