clkt_dpll.c 11 KB

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