clkt_dpll.c 11 KB

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