clkt_dpll.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * OMAP2/3/4 DPLL clock functions
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2010 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <asm/div64.h>
  21. #include <plat/clock.h>
  22. #include "clock.h"
  23. #include "cm-regbits-24xx.h"
  24. #include "cm-regbits-34xx.h"
  25. /* DPLL rate rounding: minimum DPLL multiplier, divider values */
  26. #define DPLL_MIN_MULTIPLIER 2
  27. #define DPLL_MIN_DIVIDER 1
  28. /* Possible error results from _dpll_test_mult */
  29. #define DPLL_MULT_UNDERFLOW -1
  30. /*
  31. * Scale factor to mitigate roundoff errors in DPLL rate rounding.
  32. * The higher the scale factor, the greater the risk of arithmetic overflow,
  33. * but the closer the rounded rate to the target rate. DPLL_SCALE_FACTOR
  34. * must be a power of DPLL_SCALE_BASE.
  35. */
  36. #define DPLL_SCALE_FACTOR 64
  37. #define DPLL_SCALE_BASE 2
  38. #define DPLL_ROUNDING_VAL ((DPLL_SCALE_BASE / 2) * \
  39. (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
  40. /* DPLL valid Fint frequency band limits - from 34xx TRM Section 4.7.6.2 */
  41. #define DPLL_FINT_BAND1_MIN 750000
  42. #define DPLL_FINT_BAND1_MAX 2100000
  43. #define DPLL_FINT_BAND2_MIN 7500000
  44. #define DPLL_FINT_BAND2_MAX 21000000
  45. /* _dpll_test_fint() return codes */
  46. #define DPLL_FINT_UNDERFLOW -1
  47. #define DPLL_FINT_INVALID -2
  48. /* Private functions */
  49. /*
  50. * _dpll_test_fint - test whether an Fint value is valid for the DPLL
  51. * @clk: DPLL struct clk to test
  52. * @n: divider value (N) to test
  53. *
  54. * Tests whether a particular divider @n will result in a valid DPLL
  55. * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
  56. * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
  57. * (assuming that it is counting N upwards), or -2 if the enclosing loop
  58. * should skip to the next iteration (again assuming N is increasing).
  59. */
  60. static int _dpll_test_fint(struct clk *clk, u8 n)
  61. {
  62. struct dpll_data *dd;
  63. long fint;
  64. int ret = 0;
  65. dd = clk->dpll_data;
  66. /* DPLL divider must result in a valid jitter correction val */
  67. fint = clk->parent->rate / n;
  68. if (fint < DPLL_FINT_BAND1_MIN) {
  69. pr_debug("rejecting n=%d due to Fint failure, "
  70. "lowering max_divider\n", n);
  71. dd->max_divider = n;
  72. ret = DPLL_FINT_UNDERFLOW;
  73. } else if (fint > DPLL_FINT_BAND1_MAX &&
  74. fint < DPLL_FINT_BAND2_MIN) {
  75. pr_debug("rejecting n=%d due to Fint failure\n", n);
  76. ret = DPLL_FINT_INVALID;
  77. } else if (fint > DPLL_FINT_BAND2_MAX) {
  78. pr_debug("rejecting n=%d due to Fint failure, "
  79. "boosting min_divider\n", n);
  80. dd->min_divider = n;
  81. ret = DPLL_FINT_INVALID;
  82. }
  83. return ret;
  84. }
  85. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  86. unsigned int m, unsigned int n)
  87. {
  88. unsigned long long num;
  89. num = (unsigned long long)parent_rate * m;
  90. do_div(num, n);
  91. return num;
  92. }
  93. /*
  94. * _dpll_test_mult - test a DPLL multiplier value
  95. * @m: pointer to the DPLL m (multiplier) value under test
  96. * @n: current DPLL n (divider) value under test
  97. * @new_rate: pointer to storage for the resulting rounded rate
  98. * @target_rate: the desired DPLL rate
  99. * @parent_rate: the DPLL's parent clock rate
  100. *
  101. * This code tests a DPLL multiplier value, ensuring that the
  102. * resulting rate will not be higher than the target_rate, and that
  103. * the multiplier value itself is valid for the DPLL. Initially, the
  104. * integer pointed to by the m argument should be prescaled by
  105. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  106. * a non-scaled m upon return. This non-scaled m will result in a
  107. * new_rate as close as possible to target_rate (but not greater than
  108. * target_rate) given the current (parent_rate, n, prescaled m)
  109. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  110. * non-scaled m attempted to underflow, which can allow the calling
  111. * function to bail out early; or 0 upon success.
  112. */
  113. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  114. unsigned long target_rate,
  115. unsigned long parent_rate)
  116. {
  117. int r = 0, carry = 0;
  118. /* Unscale m and round if necessary */
  119. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  120. carry = 1;
  121. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  122. /*
  123. * The new rate must be <= the target rate to avoid programming
  124. * a rate that is impossible for the hardware to handle
  125. */
  126. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  127. if (*new_rate > target_rate) {
  128. (*m)--;
  129. *new_rate = 0;
  130. }
  131. /* Guard against m underflow */
  132. if (*m < DPLL_MIN_MULTIPLIER) {
  133. *m = DPLL_MIN_MULTIPLIER;
  134. *new_rate = 0;
  135. r = DPLL_MULT_UNDERFLOW;
  136. }
  137. if (*new_rate == 0)
  138. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  139. return r;
  140. }
  141. /* Public functions */
  142. void omap2_init_dpll_parent(struct clk *clk)
  143. {
  144. u32 v;
  145. struct dpll_data *dd;
  146. dd = clk->dpll_data;
  147. if (!dd)
  148. return;
  149. v = __raw_readl(dd->control_reg);
  150. v &= dd->enable_mask;
  151. v >>= __ffs(dd->enable_mask);
  152. /* Reparent the struct clk in case the dpll is in bypass */
  153. if (cpu_is_omap24xx()) {
  154. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  155. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  156. clk_reparent(clk, dd->clk_bypass);
  157. } else if (cpu_is_omap34xx()) {
  158. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  159. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  160. clk_reparent(clk, dd->clk_bypass);
  161. } else if (cpu_is_omap44xx()) {
  162. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  163. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  164. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  165. clk_reparent(clk, dd->clk_bypass);
  166. }
  167. return;
  168. }
  169. /**
  170. * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
  171. * @clk: struct clk * of a DPLL
  172. *
  173. * DPLLs can be locked or bypassed - basically, enabled or disabled.
  174. * When locked, the DPLL output depends on the M and N values. When
  175. * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
  176. * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
  177. * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
  178. * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
  179. * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
  180. * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
  181. * if the clock @clk is not a DPLL.
  182. */
  183. u32 omap2_get_dpll_rate(struct clk *clk)
  184. {
  185. long long dpll_clk;
  186. u32 dpll_mult, dpll_div, v;
  187. struct dpll_data *dd;
  188. dd = clk->dpll_data;
  189. if (!dd)
  190. return 0;
  191. /* Return bypass rate if DPLL is bypassed */
  192. v = __raw_readl(dd->control_reg);
  193. v &= dd->enable_mask;
  194. v >>= __ffs(dd->enable_mask);
  195. if (cpu_is_omap24xx()) {
  196. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  197. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  198. return dd->clk_bypass->rate;
  199. } else if (cpu_is_omap34xx()) {
  200. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  201. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  202. return dd->clk_bypass->rate;
  203. } else if (cpu_is_omap44xx()) {
  204. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  205. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  206. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  207. return dd->clk_bypass->rate;
  208. }
  209. v = __raw_readl(dd->mult_div1_reg);
  210. dpll_mult = v & dd->mult_mask;
  211. dpll_mult >>= __ffs(dd->mult_mask);
  212. dpll_div = v & dd->div1_mask;
  213. dpll_div >>= __ffs(dd->div1_mask);
  214. dpll_clk = (long long)dd->clk_ref->rate * dpll_mult;
  215. do_div(dpll_clk, dpll_div + 1);
  216. return dpll_clk;
  217. }
  218. /* DPLL rate rounding code */
  219. /**
  220. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  221. * @clk: struct clk * for a DPLL
  222. * @target_rate: desired DPLL clock rate
  223. *
  224. * Given a DPLL and a desired target rate, round the target rate to a
  225. * possible, programmable rate for this DPLL. Attempts to select the
  226. * minimum possible n. Stores the computed (m, n) in the DPLL's
  227. * dpll_data structure so set_rate() will not need to call this
  228. * (expensive) function again. Returns ~0 if the target rate cannot
  229. * be rounded, or the rounded rate upon success.
  230. */
  231. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  232. {
  233. int m, n, r, scaled_max_m;
  234. unsigned long scaled_rt_rp;
  235. unsigned long new_rate = 0;
  236. struct dpll_data *dd;
  237. if (!clk || !clk->dpll_data)
  238. return ~0;
  239. dd = clk->dpll_data;
  240. pr_debug("clock: %s: starting DPLL round_rate, target rate %ld\n",
  241. clk->name, target_rate);
  242. scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR);
  243. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  244. dd->last_rounded_rate = 0;
  245. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  246. /* Is the (input clk, divider) pair valid for the DPLL? */
  247. r = _dpll_test_fint(clk, n);
  248. if (r == DPLL_FINT_UNDERFLOW)
  249. break;
  250. else if (r == DPLL_FINT_INVALID)
  251. continue;
  252. /* Compute the scaled DPLL multiplier, based on the divider */
  253. m = scaled_rt_rp * n;
  254. /*
  255. * Since we're counting n up, a m overflow means we
  256. * can bail out completely (since as n increases in
  257. * the next iteration, there's no way that m can
  258. * increase beyond the current m)
  259. */
  260. if (m > scaled_max_m)
  261. break;
  262. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  263. dd->clk_ref->rate);
  264. /* m can't be set low enough for this n - try with a larger n */
  265. if (r == DPLL_MULT_UNDERFLOW)
  266. continue;
  267. pr_debug("clock: %s: m = %d: n = %d: new_rate = %ld\n",
  268. clk->name, m, n, new_rate);
  269. if (target_rate == new_rate) {
  270. dd->last_rounded_m = m;
  271. dd->last_rounded_n = n;
  272. dd->last_rounded_rate = target_rate;
  273. break;
  274. }
  275. }
  276. if (target_rate != new_rate) {
  277. pr_debug("clock: %s: cannot round to rate %ld\n", clk->name,
  278. target_rate);
  279. return ~0;
  280. }
  281. return target_rate;
  282. }