clkt_dpll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 OMAP3430_DPLL_FINT_BAND1_MIN 750000
  42. #define OMAP3430_DPLL_FINT_BAND1_MAX 2100000
  43. #define OMAP3430_DPLL_FINT_BAND2_MIN 7500000
  44. #define OMAP3430_DPLL_FINT_BAND2_MAX 21000000
  45. /*
  46. * DPLL valid Fint frequency range for OMAP36xx and OMAP4xxx.
  47. * From device data manual section 4.3 "DPLL and DLL Specifications".
  48. */
  49. #define OMAP3PLUS_DPLL_FINT_JTYPE_MIN 500000
  50. #define OMAP3PLUS_DPLL_FINT_JTYPE_MAX 2500000
  51. #define OMAP3PLUS_DPLL_FINT_MIN 32000
  52. #define OMAP3PLUS_DPLL_FINT_MAX 52000000
  53. /* _dpll_test_fint() return codes */
  54. #define DPLL_FINT_UNDERFLOW -1
  55. #define DPLL_FINT_INVALID -2
  56. /* Private functions */
  57. /*
  58. * _dpll_test_fint - test whether an Fint value is valid for the DPLL
  59. * @clk: DPLL struct clk to test
  60. * @n: divider value (N) to test
  61. *
  62. * Tests whether a particular divider @n will result in a valid DPLL
  63. * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
  64. * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
  65. * (assuming that it is counting N upwards), or -2 if the enclosing loop
  66. * should skip to the next iteration (again assuming N is increasing).
  67. */
  68. static int _dpll_test_fint(struct clk *clk, u8 n)
  69. {
  70. struct dpll_data *dd;
  71. long fint, fint_min, fint_max;
  72. int ret = 0;
  73. dd = clk->dpll_data;
  74. /* DPLL divider must result in a valid jitter correction val */
  75. fint = clk->parent->rate / n;
  76. if (cpu_is_omap24xx()) {
  77. /* Should not be called for OMAP2, so warn if it is called */
  78. WARN(1, "No fint limits available for OMAP2!\n");
  79. return DPLL_FINT_INVALID;
  80. } else if (cpu_is_omap3430()) {
  81. fint_min = OMAP3430_DPLL_FINT_BAND1_MIN;
  82. fint_max = OMAP3430_DPLL_FINT_BAND2_MAX;
  83. } else if (dd->flags & DPLL_J_TYPE) {
  84. fint_min = OMAP3PLUS_DPLL_FINT_JTYPE_MIN;
  85. fint_max = OMAP3PLUS_DPLL_FINT_JTYPE_MAX;
  86. } else {
  87. fint_min = OMAP3PLUS_DPLL_FINT_MIN;
  88. fint_max = OMAP3PLUS_DPLL_FINT_MAX;
  89. }
  90. if (fint < fint_min) {
  91. pr_debug("rejecting n=%d due to Fint failure, "
  92. "lowering max_divider\n", n);
  93. dd->max_divider = n;
  94. ret = DPLL_FINT_UNDERFLOW;
  95. } else if (fint > fint_max) {
  96. pr_debug("rejecting n=%d due to Fint failure, "
  97. "boosting min_divider\n", n);
  98. dd->min_divider = n;
  99. ret = DPLL_FINT_INVALID;
  100. } else if (cpu_is_omap3430() && fint > OMAP3430_DPLL_FINT_BAND1_MAX &&
  101. fint < OMAP3430_DPLL_FINT_BAND2_MIN) {
  102. pr_debug("rejecting n=%d due to Fint failure\n", n);
  103. ret = DPLL_FINT_INVALID;
  104. }
  105. return ret;
  106. }
  107. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  108. unsigned int m, unsigned int n)
  109. {
  110. unsigned long long num;
  111. num = (unsigned long long)parent_rate * m;
  112. do_div(num, n);
  113. return num;
  114. }
  115. /*
  116. * _dpll_test_mult - test a DPLL multiplier value
  117. * @m: pointer to the DPLL m (multiplier) value under test
  118. * @n: current DPLL n (divider) value under test
  119. * @new_rate: pointer to storage for the resulting rounded rate
  120. * @target_rate: the desired DPLL rate
  121. * @parent_rate: the DPLL's parent clock rate
  122. *
  123. * This code tests a DPLL multiplier value, ensuring that the
  124. * resulting rate will not be higher than the target_rate, and that
  125. * the multiplier value itself is valid for the DPLL. Initially, the
  126. * integer pointed to by the m argument should be prescaled by
  127. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  128. * a non-scaled m upon return. This non-scaled m will result in a
  129. * new_rate as close as possible to target_rate (but not greater than
  130. * target_rate) given the current (parent_rate, n, prescaled m)
  131. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  132. * non-scaled m attempted to underflow, which can allow the calling
  133. * function to bail out early; or 0 upon success.
  134. */
  135. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  136. unsigned long target_rate,
  137. unsigned long parent_rate)
  138. {
  139. int r = 0, carry = 0;
  140. /* Unscale m and round if necessary */
  141. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  142. carry = 1;
  143. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  144. /*
  145. * The new rate must be <= the target rate to avoid programming
  146. * a rate that is impossible for the hardware to handle
  147. */
  148. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  149. if (*new_rate > target_rate) {
  150. (*m)--;
  151. *new_rate = 0;
  152. }
  153. /* Guard against m underflow */
  154. if (*m < DPLL_MIN_MULTIPLIER) {
  155. *m = DPLL_MIN_MULTIPLIER;
  156. *new_rate = 0;
  157. r = DPLL_MULT_UNDERFLOW;
  158. }
  159. if (*new_rate == 0)
  160. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  161. return r;
  162. }
  163. /* Public functions */
  164. void omap2_init_dpll_parent(struct clk *clk)
  165. {
  166. u32 v;
  167. struct dpll_data *dd;
  168. dd = clk->dpll_data;
  169. if (!dd)
  170. return;
  171. v = __raw_readl(dd->control_reg);
  172. v &= dd->enable_mask;
  173. v >>= __ffs(dd->enable_mask);
  174. /* Reparent the struct clk in case the dpll is in bypass */
  175. if (cpu_is_omap24xx()) {
  176. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  177. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  178. clk_reparent(clk, dd->clk_bypass);
  179. } else if (cpu_is_omap34xx()) {
  180. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  181. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  182. clk_reparent(clk, dd->clk_bypass);
  183. } else if (cpu_is_omap44xx()) {
  184. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  185. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  186. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  187. clk_reparent(clk, dd->clk_bypass);
  188. }
  189. return;
  190. }
  191. /**
  192. * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
  193. * @clk: struct clk * of a DPLL
  194. *
  195. * DPLLs can be locked or bypassed - basically, enabled or disabled.
  196. * When locked, the DPLL output depends on the M and N values. When
  197. * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
  198. * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
  199. * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
  200. * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
  201. * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
  202. * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
  203. * if the clock @clk is not a DPLL.
  204. */
  205. u32 omap2_get_dpll_rate(struct clk *clk)
  206. {
  207. long long dpll_clk;
  208. u32 dpll_mult, dpll_div, v;
  209. struct dpll_data *dd;
  210. dd = clk->dpll_data;
  211. if (!dd)
  212. return 0;
  213. /* Return bypass rate if DPLL is bypassed */
  214. v = __raw_readl(dd->control_reg);
  215. v &= dd->enable_mask;
  216. v >>= __ffs(dd->enable_mask);
  217. if (cpu_is_omap24xx()) {
  218. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  219. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  220. return dd->clk_bypass->rate;
  221. } else if (cpu_is_omap34xx()) {
  222. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  223. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  224. return dd->clk_bypass->rate;
  225. } else if (cpu_is_omap44xx()) {
  226. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  227. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  228. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  229. return dd->clk_bypass->rate;
  230. }
  231. v = __raw_readl(dd->mult_div1_reg);
  232. dpll_mult = v & dd->mult_mask;
  233. dpll_mult >>= __ffs(dd->mult_mask);
  234. dpll_div = v & dd->div1_mask;
  235. dpll_div >>= __ffs(dd->div1_mask);
  236. dpll_clk = (long long)dd->clk_ref->rate * dpll_mult;
  237. do_div(dpll_clk, dpll_div + 1);
  238. return dpll_clk;
  239. }
  240. /* DPLL rate rounding code */
  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 and a desired target rate, round the target rate to a
  247. * possible, programmable rate for this DPLL. Attempts to select the
  248. * minimum possible n. Stores the computed (m, n) in the DPLL's
  249. * dpll_data structure so set_rate() will not need to call this
  250. * (expensive) function again. Returns ~0 if the target rate cannot
  251. * be rounded, or the rounded rate upon success.
  252. */
  253. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  254. {
  255. int m, n, r, scaled_max_m;
  256. unsigned long scaled_rt_rp;
  257. unsigned long new_rate = 0;
  258. struct dpll_data *dd;
  259. if (!clk || !clk->dpll_data)
  260. return ~0;
  261. dd = clk->dpll_data;
  262. pr_debug("clock: %s: starting DPLL round_rate, target rate %ld\n",
  263. clk->name, target_rate);
  264. scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR);
  265. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  266. dd->last_rounded_rate = 0;
  267. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  268. /* Is the (input clk, divider) pair valid for the DPLL? */
  269. r = _dpll_test_fint(clk, n);
  270. if (r == DPLL_FINT_UNDERFLOW)
  271. break;
  272. else if (r == DPLL_FINT_INVALID)
  273. continue;
  274. /* Compute the scaled DPLL multiplier, based on the divider */
  275. m = scaled_rt_rp * n;
  276. /*
  277. * Since we're counting n up, a m overflow means we
  278. * can bail out completely (since as n increases in
  279. * the next iteration, there's no way that m can
  280. * increase beyond the current m)
  281. */
  282. if (m > scaled_max_m)
  283. break;
  284. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  285. dd->clk_ref->rate);
  286. /* m can't be set low enough for this n - try with a larger n */
  287. if (r == DPLL_MULT_UNDERFLOW)
  288. continue;
  289. pr_debug("clock: %s: m = %d: n = %d: new_rate = %ld\n",
  290. clk->name, m, n, new_rate);
  291. if (target_rate == new_rate) {
  292. dd->last_rounded_m = m;
  293. dd->last_rounded_n = n;
  294. dd->last_rounded_rate = target_rate;
  295. break;
  296. }
  297. }
  298. if (target_rate != new_rate) {
  299. pr_debug("clock: %s: cannot round to rate %ld\n", clk->name,
  300. target_rate);
  301. return ~0;
  302. }
  303. return target_rate;
  304. }