clkt_dpll.c 10 KB

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