clkt_dpll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 + 1);
  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. /* Return bypass rate if DPLL is bypassed */
  150. v = __raw_readl(dd->control_reg);
  151. v &= dd->enable_mask;
  152. v >>= __ffs(dd->enable_mask);
  153. /* Reparent in case the dpll is in bypass */
  154. if (cpu_is_omap24xx()) {
  155. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  156. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  157. clk_reparent(clk, dd->clk_bypass);
  158. } else if (cpu_is_omap34xx()) {
  159. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  160. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  161. clk_reparent(clk, dd->clk_bypass);
  162. } else if (cpu_is_omap44xx()) {
  163. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  164. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  165. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  166. clk_reparent(clk, dd->clk_bypass);
  167. }
  168. return;
  169. }
  170. /**
  171. * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
  172. * @clk: struct clk * of a DPLL
  173. *
  174. * DPLLs can be locked or bypassed - basically, enabled or disabled.
  175. * When locked, the DPLL output depends on the M and N values. When
  176. * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
  177. * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
  178. * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
  179. * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
  180. * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
  181. * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
  182. * if the clock @clk is not a DPLL.
  183. */
  184. u32 omap2_get_dpll_rate(struct clk *clk)
  185. {
  186. long long dpll_clk;
  187. u32 dpll_mult, dpll_div, v;
  188. struct dpll_data *dd;
  189. dd = clk->dpll_data;
  190. if (!dd)
  191. return 0;
  192. /* Return bypass rate if DPLL is bypassed */
  193. v = __raw_readl(dd->control_reg);
  194. v &= dd->enable_mask;
  195. v >>= __ffs(dd->enable_mask);
  196. if (cpu_is_omap24xx()) {
  197. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  198. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  199. return dd->clk_bypass->rate;
  200. } else if (cpu_is_omap34xx()) {
  201. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  202. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  203. return dd->clk_bypass->rate;
  204. } else if (cpu_is_omap44xx()) {
  205. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  206. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  207. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  208. return dd->clk_bypass->rate;
  209. }
  210. v = __raw_readl(dd->mult_div1_reg);
  211. dpll_mult = v & dd->mult_mask;
  212. dpll_mult >>= __ffs(dd->mult_mask);
  213. dpll_div = v & dd->div1_mask;
  214. dpll_div >>= __ffs(dd->div1_mask);
  215. dpll_clk = (long long)dd->clk_ref->rate * dpll_mult;
  216. do_div(dpll_clk, dpll_div + 1);
  217. return dpll_clk;
  218. }
  219. /* DPLL rate rounding code */
  220. /**
  221. * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
  222. * @clk: struct clk * of the DPLL
  223. * @tolerance: maximum rate error tolerance
  224. *
  225. * Set the maximum DPLL rate error tolerance for the rate rounding
  226. * algorithm. The rate tolerance is an attempt to balance DPLL power
  227. * saving (the least divider value "n") vs. rate fidelity (the least
  228. * difference between the desired DPLL target rate and the rounded
  229. * rate out of the algorithm). So, increasing the tolerance is likely
  230. * to decrease DPLL power consumption and increase DPLL rate error.
  231. * Returns -EINVAL if provided a null clock ptr or a clk that is not a
  232. * DPLL; or 0 upon success.
  233. */
  234. int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
  235. {
  236. if (!clk || !clk->dpll_data)
  237. return -EINVAL;
  238. clk->dpll_data->rate_tolerance = tolerance;
  239. return 0;
  240. }
  241. /**
  242. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  243. * @clk: struct clk * for a DPLL
  244. * @target_rate: desired DPLL clock rate
  245. *
  246. * Given a DPLL, a desired target rate, and a rate tolerance, round
  247. * the target rate to a possible, programmable rate for this DPLL.
  248. * Rate tolerance is assumed to be set by the caller before this
  249. * function is called. Attempts to select the minimum possible n
  250. * within the tolerance to reduce power consumption. Stores the
  251. * computed (m, n) in the DPLL's dpll_data structure so set_rate()
  252. * will not need to call this (expensive) function again. Returns ~0
  253. * if the target rate cannot be rounded, either because the rate is
  254. * too low or because the rate tolerance is set too tightly; or the
  255. * rounded rate upon success.
  256. */
  257. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  258. {
  259. int m, n, r, e, scaled_max_m;
  260. unsigned long scaled_rt_rp, new_rate;
  261. int min_e = -1, min_e_m = -1, min_e_n = -1;
  262. struct dpll_data *dd;
  263. if (!clk || !clk->dpll_data)
  264. return ~0;
  265. dd = clk->dpll_data;
  266. pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
  267. "%ld\n", clk->name, target_rate);
  268. scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR);
  269. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  270. dd->last_rounded_rate = 0;
  271. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  272. /* Is the (input clk, divider) pair valid for the DPLL? */
  273. r = _dpll_test_fint(clk, n);
  274. if (r == DPLL_FINT_UNDERFLOW)
  275. break;
  276. else if (r == DPLL_FINT_INVALID)
  277. continue;
  278. /* Compute the scaled DPLL multiplier, based on the divider */
  279. m = scaled_rt_rp * n;
  280. /*
  281. * Since we're counting n up, a m overflow means we
  282. * can bail out completely (since as n increases in
  283. * the next iteration, there's no way that m can
  284. * increase beyond the current m)
  285. */
  286. if (m > scaled_max_m)
  287. break;
  288. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  289. dd->clk_ref->rate);
  290. /* m can't be set low enough for this n - try with a larger n */
  291. if (r == DPLL_MULT_UNDERFLOW)
  292. continue;
  293. e = target_rate - new_rate;
  294. pr_debug("clock: n = %d: m = %d: rate error is %d "
  295. "(new_rate = %ld)\n", n, m, e, new_rate);
  296. if (min_e == -1 ||
  297. min_e >= (int)(abs(e) - dd->rate_tolerance)) {
  298. min_e = e;
  299. min_e_m = m;
  300. min_e_n = n;
  301. pr_debug("clock: found new least error %d\n", min_e);
  302. /* We found good settings -- bail out now */
  303. if (min_e <= dd->rate_tolerance)
  304. break;
  305. }
  306. }
  307. if (min_e < 0) {
  308. pr_debug("clock: error: target rate or tolerance too low\n");
  309. return ~0;
  310. }
  311. dd->last_rounded_m = min_e_m;
  312. dd->last_rounded_n = min_e_n;
  313. dd->last_rounded_rate = _dpll_compute_new_rate(dd->clk_ref->rate,
  314. min_e_m, min_e_n);
  315. pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
  316. min_e, min_e_m, min_e_n);
  317. pr_debug("clock: final rate: %ld (target rate: %ld)\n",
  318. dd->last_rounded_rate, target_rate);
  319. return dd->last_rounded_rate;
  320. }