clk-pll.c 7.0 KB

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