clk-pll.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * | +--+ |
  16. * in >>-----[ /N ]--->| | +-----+ |
  17. * | | |------| DCO |---->> out
  18. * | +--------->| | +--v--+ |
  19. * | | +--+ | |
  20. * | | | |
  21. * | +--[ *(M + (F/Fmax) ]<--+ |
  22. * | |
  23. * +--------------------------------+
  24. *
  25. * out = in * (m + frac / frac_max) / n
  26. */
  27. #include <linux/clk-provider.h>
  28. #include <linux/delay.h>
  29. #include <linux/err.h>
  30. #include <linux/io.h>
  31. #include <linux/math64.h>
  32. #include <linux/module.h>
  33. #include <linux/of_address.h>
  34. #include <linux/slab.h>
  35. #include <linux/string.h>
  36. #include "clkc.h"
  37. static inline struct meson_clk_pll_data *
  38. meson_clk_pll_data(struct clk_regmap *clk)
  39. {
  40. return (struct meson_clk_pll_data *)clk->data;
  41. }
  42. static unsigned long __pll_params_to_rate(unsigned long parent_rate,
  43. const struct pll_params_table *pllt,
  44. u16 frac,
  45. struct meson_clk_pll_data *pll)
  46. {
  47. u64 rate = (u64)parent_rate * pllt->m;
  48. if (frac && MESON_PARM_APPLICABLE(&pll->frac)) {
  49. u64 frac_rate = (u64)parent_rate * frac;
  50. rate += DIV_ROUND_UP_ULL(frac_rate,
  51. (1 << pll->frac.width));
  52. }
  53. return DIV_ROUND_UP_ULL(rate, pllt->n);
  54. }
  55. static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
  56. unsigned long parent_rate)
  57. {
  58. struct clk_regmap *clk = to_clk_regmap(hw);
  59. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  60. struct pll_params_table pllt;
  61. u16 frac;
  62. pllt.n = meson_parm_read(clk->map, &pll->n);
  63. pllt.m = meson_parm_read(clk->map, &pll->m);
  64. frac = MESON_PARM_APPLICABLE(&pll->frac) ?
  65. meson_parm_read(clk->map, &pll->frac) :
  66. 0;
  67. return __pll_params_to_rate(parent_rate, &pllt, frac, pll);
  68. }
  69. static u16 __pll_params_with_frac(unsigned long rate,
  70. unsigned long parent_rate,
  71. const struct pll_params_table *pllt,
  72. struct meson_clk_pll_data *pll)
  73. {
  74. u16 frac_max = (1 << pll->frac.width);
  75. u64 val = (u64)rate * pllt->n;
  76. if (pll->flags & CLK_MESON_PLL_ROUND_CLOSEST)
  77. val = DIV_ROUND_CLOSEST_ULL(val * frac_max, parent_rate);
  78. else
  79. val = div_u64(val * frac_max, parent_rate);
  80. val -= pllt->m * frac_max;
  81. return min((u16)val, (u16)(frac_max - 1));
  82. }
  83. static bool meson_clk_pll_is_better(unsigned long rate,
  84. unsigned long best,
  85. unsigned long now,
  86. struct meson_clk_pll_data *pll)
  87. {
  88. if (!(pll->flags & CLK_MESON_PLL_ROUND_CLOSEST) ||
  89. MESON_PARM_APPLICABLE(&pll->frac)) {
  90. /* Round down */
  91. if (now < rate && best < now)
  92. return true;
  93. } else {
  94. /* Round Closest */
  95. if (abs(now - rate) < abs(best - rate))
  96. return true;
  97. }
  98. return false;
  99. }
  100. static const struct pll_params_table *
  101. meson_clk_get_pll_settings(unsigned long rate,
  102. unsigned long parent_rate,
  103. struct meson_clk_pll_data *pll)
  104. {
  105. const struct pll_params_table *table = pll->table;
  106. unsigned long best = 0, now = 0;
  107. unsigned int i, best_i = 0;
  108. if (!table)
  109. return NULL;
  110. for (i = 0; table[i].n; i++) {
  111. now = __pll_params_to_rate(parent_rate, &table[i], 0, pll);
  112. /* If we get an exact match, don't bother any further */
  113. if (now == rate) {
  114. return &table[i];
  115. } else if (meson_clk_pll_is_better(rate, best, now, pll)) {
  116. best = now;
  117. best_i = i;
  118. }
  119. }
  120. return (struct pll_params_table *)&table[best_i];
  121. }
  122. static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  123. unsigned long *parent_rate)
  124. {
  125. struct clk_regmap *clk = to_clk_regmap(hw);
  126. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  127. const struct pll_params_table *pllt =
  128. meson_clk_get_pll_settings(rate, *parent_rate, pll);
  129. unsigned long round;
  130. u16 frac;
  131. if (!pllt)
  132. return meson_clk_pll_recalc_rate(hw, *parent_rate);
  133. round = __pll_params_to_rate(*parent_rate, pllt, 0, pll);
  134. if (!MESON_PARM_APPLICABLE(&pll->frac) || rate == round)
  135. return round;
  136. /*
  137. * The rate provided by the setting is not an exact match, let's
  138. * try to improve the result using the fractional parameter
  139. */
  140. frac = __pll_params_with_frac(rate, *parent_rate, pllt, pll);
  141. return __pll_params_to_rate(*parent_rate, pllt, frac, pll);
  142. }
  143. static int meson_clk_pll_wait_lock(struct clk_hw *hw)
  144. {
  145. struct clk_regmap *clk = to_clk_regmap(hw);
  146. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  147. int delay = 24000000;
  148. do {
  149. /* Is the clock locked now ? */
  150. if (meson_parm_read(clk->map, &pll->l))
  151. return 0;
  152. delay--;
  153. } while (delay > 0);
  154. return -ETIMEDOUT;
  155. }
  156. static void meson_clk_pll_init(struct clk_hw *hw)
  157. {
  158. struct clk_regmap *clk = to_clk_regmap(hw);
  159. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  160. if (pll->init_count) {
  161. meson_parm_write(clk->map, &pll->rst, 1);
  162. regmap_multi_reg_write(clk->map, pll->init_regs,
  163. pll->init_count);
  164. meson_parm_write(clk->map, &pll->rst, 0);
  165. }
  166. }
  167. static int meson_clk_pll_enable(struct clk_hw *hw)
  168. {
  169. struct clk_regmap *clk = to_clk_regmap(hw);
  170. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  171. /* Make sure the pll is in reset */
  172. meson_parm_write(clk->map, &pll->rst, 1);
  173. /* Enable the pll */
  174. meson_parm_write(clk->map, &pll->en, 1);
  175. /* Take the pll out reset */
  176. meson_parm_write(clk->map, &pll->rst, 0);
  177. if (meson_clk_pll_wait_lock(hw))
  178. return -EIO;
  179. return 0;
  180. }
  181. static void meson_clk_pll_disable(struct clk_hw *hw)
  182. {
  183. struct clk_regmap *clk = to_clk_regmap(hw);
  184. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  185. /* Put the pll is in reset */
  186. meson_parm_write(clk->map, &pll->rst, 1);
  187. /* Disable the pll */
  188. meson_parm_write(clk->map, &pll->en, 0);
  189. }
  190. static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  191. unsigned long parent_rate)
  192. {
  193. struct clk_regmap *clk = to_clk_regmap(hw);
  194. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  195. const struct pll_params_table *pllt;
  196. unsigned int enabled;
  197. unsigned long old_rate;
  198. u16 frac = 0;
  199. if (parent_rate == 0 || rate == 0)
  200. return -EINVAL;
  201. old_rate = rate;
  202. pllt = meson_clk_get_pll_settings(rate, parent_rate, pll);
  203. if (!pllt)
  204. return -EINVAL;
  205. enabled = meson_parm_read(clk->map, &pll->en);
  206. if (enabled)
  207. meson_clk_pll_disable(hw);
  208. meson_parm_write(clk->map, &pll->n, pllt->n);
  209. meson_parm_write(clk->map, &pll->m, pllt->m);
  210. if (MESON_PARM_APPLICABLE(&pll->frac)) {
  211. frac = __pll_params_with_frac(rate, parent_rate, pllt, pll);
  212. meson_parm_write(clk->map, &pll->frac, frac);
  213. }
  214. /* If the pll is stopped, bail out now */
  215. if (!enabled)
  216. return 0;
  217. if (meson_clk_pll_enable(hw)) {
  218. pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
  219. __func__, old_rate);
  220. /*
  221. * FIXME: Do we really need/want this HACK ?
  222. * It looks unsafe. what happens if the clock gets into a
  223. * broken state and we can't lock back on the old_rate ? Looks
  224. * like an infinite recursion is possible
  225. */
  226. meson_clk_pll_set_rate(hw, old_rate, parent_rate);
  227. }
  228. return 0;
  229. }
  230. const struct clk_ops meson_clk_pll_ops = {
  231. .init = meson_clk_pll_init,
  232. .recalc_rate = meson_clk_pll_recalc_rate,
  233. .round_rate = meson_clk_pll_round_rate,
  234. .set_rate = meson_clk_pll_set_rate,
  235. .enable = meson_clk_pll_enable,
  236. .disable = meson_clk_pll_disable
  237. };
  238. const struct clk_ops meson_clk_pll_ro_ops = {
  239. .recalc_rate = meson_clk_pll_recalc_rate,
  240. };