clkt_dpll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-provider.h>
  19. #include <linux/io.h>
  20. #include <asm/div64.h>
  21. #include "clock.h"
  22. /* DPLL rate rounding: minimum DPLL multiplier, divider values */
  23. #define DPLL_MIN_MULTIPLIER 2
  24. #define DPLL_MIN_DIVIDER 1
  25. /* Possible error results from _dpll_test_mult */
  26. #define DPLL_MULT_UNDERFLOW -1
  27. /*
  28. * Scale factor to mitigate roundoff errors in DPLL rate rounding.
  29. * The higher the scale factor, the greater the risk of arithmetic overflow,
  30. * but the closer the rounded rate to the target rate. DPLL_SCALE_FACTOR
  31. * must be a power of DPLL_SCALE_BASE.
  32. */
  33. #define DPLL_SCALE_FACTOR 64
  34. #define DPLL_SCALE_BASE 2
  35. #define DPLL_ROUNDING_VAL ((DPLL_SCALE_BASE / 2) * \
  36. (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
  37. /*
  38. * DPLL valid Fint frequency range for OMAP36xx and OMAP4xxx.
  39. * From device data manual section 4.3 "DPLL and DLL Specifications".
  40. */
  41. #define OMAP3PLUS_DPLL_FINT_JTYPE_MIN 500000
  42. #define OMAP3PLUS_DPLL_FINT_JTYPE_MAX 2500000
  43. /* _dpll_test_fint() return codes */
  44. #define DPLL_FINT_UNDERFLOW -1
  45. #define DPLL_FINT_INVALID -2
  46. /* Private functions */
  47. /*
  48. * _dpll_test_fint - test whether an Fint value is valid for the DPLL
  49. * @clk: DPLL struct clk to test
  50. * @n: divider value (N) to test
  51. *
  52. * Tests whether a particular divider @n will result in a valid DPLL
  53. * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
  54. * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
  55. * (assuming that it is counting N upwards), or -2 if the enclosing loop
  56. * should skip to the next iteration (again assuming N is increasing).
  57. */
  58. static int _dpll_test_fint(struct clk_hw_omap *clk, unsigned int n)
  59. {
  60. struct dpll_data *dd;
  61. long fint, fint_min, fint_max;
  62. int ret = 0;
  63. dd = clk->dpll_data;
  64. /* DPLL divider must result in a valid jitter correction val */
  65. fint = __clk_get_rate(__clk_get_parent(clk->hw.clk)) / n;
  66. if (dd->flags & DPLL_J_TYPE) {
  67. fint_min = OMAP3PLUS_DPLL_FINT_JTYPE_MIN;
  68. fint_max = OMAP3PLUS_DPLL_FINT_JTYPE_MAX;
  69. } else {
  70. fint_min = ti_clk_features.fint_min;
  71. fint_max = ti_clk_features.fint_max;
  72. }
  73. if (!fint_min || !fint_max) {
  74. WARN(1, "No fint limits available!\n");
  75. return DPLL_FINT_INVALID;
  76. }
  77. if (fint < ti_clk_features.fint_min) {
  78. pr_debug("rejecting n=%d due to Fint failure, lowering max_divider\n",
  79. n);
  80. dd->max_divider = n;
  81. ret = DPLL_FINT_UNDERFLOW;
  82. } else if (fint > ti_clk_features.fint_max) {
  83. pr_debug("rejecting n=%d due to Fint failure, boosting min_divider\n",
  84. n);
  85. dd->min_divider = n;
  86. ret = DPLL_FINT_INVALID;
  87. } else if (fint > ti_clk_features.fint_band1_max &&
  88. fint < ti_clk_features.fint_band2_min) {
  89. pr_debug("rejecting n=%d due to Fint failure\n", n);
  90. ret = DPLL_FINT_INVALID;
  91. }
  92. return ret;
  93. }
  94. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  95. unsigned int m, unsigned int n)
  96. {
  97. unsigned long long num;
  98. num = (unsigned long long)parent_rate * m;
  99. do_div(num, n);
  100. return num;
  101. }
  102. /*
  103. * _dpll_test_mult - test a DPLL multiplier value
  104. * @m: pointer to the DPLL m (multiplier) value under test
  105. * @n: current DPLL n (divider) value under test
  106. * @new_rate: pointer to storage for the resulting rounded rate
  107. * @target_rate: the desired DPLL rate
  108. * @parent_rate: the DPLL's parent clock rate
  109. *
  110. * This code tests a DPLL multiplier value, ensuring that the
  111. * resulting rate will not be higher than the target_rate, and that
  112. * the multiplier value itself is valid for the DPLL. Initially, the
  113. * integer pointed to by the m argument should be prescaled by
  114. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  115. * a non-scaled m upon return. This non-scaled m will result in a
  116. * new_rate as close as possible to target_rate (but not greater than
  117. * target_rate) given the current (parent_rate, n, prescaled m)
  118. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  119. * non-scaled m attempted to underflow, which can allow the calling
  120. * function to bail out early; or 0 upon success.
  121. */
  122. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  123. unsigned long target_rate,
  124. unsigned long parent_rate)
  125. {
  126. int r = 0, carry = 0;
  127. /* Unscale m and round if necessary */
  128. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  129. carry = 1;
  130. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  131. /*
  132. * The new rate must be <= the target rate to avoid programming
  133. * a rate that is impossible for the hardware to handle
  134. */
  135. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  136. if (*new_rate > target_rate) {
  137. (*m)--;
  138. *new_rate = 0;
  139. }
  140. /* Guard against m underflow */
  141. if (*m < DPLL_MIN_MULTIPLIER) {
  142. *m = DPLL_MIN_MULTIPLIER;
  143. *new_rate = 0;
  144. r = DPLL_MULT_UNDERFLOW;
  145. }
  146. if (*new_rate == 0)
  147. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  148. return r;
  149. }
  150. /**
  151. * _omap2_dpll_is_in_bypass - check if DPLL is in bypass mode or not
  152. * @v: bitfield value of the DPLL enable
  153. *
  154. * Checks given DPLL enable bitfield to see whether the DPLL is in bypass
  155. * mode or not. Returns 1 if the DPLL is in bypass, 0 otherwise.
  156. */
  157. static int _omap2_dpll_is_in_bypass(u32 v)
  158. {
  159. u8 mask, val;
  160. mask = ti_clk_features.dpll_bypass_vals;
  161. /*
  162. * Each set bit in the mask corresponds to a bypass value equal
  163. * to the bitshift. Go through each set-bit in the mask and
  164. * compare against the given register value.
  165. */
  166. while (mask) {
  167. val = __ffs(mask);
  168. mask ^= (1 << val);
  169. if (v == val)
  170. return 1;
  171. }
  172. return 0;
  173. }
  174. /* Public functions */
  175. u8 omap2_init_dpll_parent(struct clk_hw *hw)
  176. {
  177. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  178. u32 v;
  179. struct dpll_data *dd;
  180. dd = clk->dpll_data;
  181. if (!dd)
  182. return -EINVAL;
  183. v = omap2_clk_readl(clk, dd->control_reg);
  184. v &= dd->enable_mask;
  185. v >>= __ffs(dd->enable_mask);
  186. /* Reparent the struct clk in case the dpll is in bypass */
  187. if (_omap2_dpll_is_in_bypass(v))
  188. return 1;
  189. return 0;
  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. unsigned long omap2_get_dpll_rate(struct clk_hw_omap *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 = omap2_clk_readl(clk, dd->control_reg);
  215. v &= dd->enable_mask;
  216. v >>= __ffs(dd->enable_mask);
  217. if (_omap2_dpll_is_in_bypass(v))
  218. return __clk_get_rate(dd->clk_bypass);
  219. v = omap2_clk_readl(clk, dd->mult_div1_reg);
  220. dpll_mult = v & dd->mult_mask;
  221. dpll_mult >>= __ffs(dd->mult_mask);
  222. dpll_div = v & dd->div1_mask;
  223. dpll_div >>= __ffs(dd->div1_mask);
  224. dpll_clk = (long long) __clk_get_rate(dd->clk_ref) * dpll_mult;
  225. do_div(dpll_clk, dpll_div + 1);
  226. return dpll_clk;
  227. }
  228. /* DPLL rate rounding code */
  229. /**
  230. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  231. * @clk: struct clk * for a DPLL
  232. * @target_rate: desired DPLL clock rate
  233. *
  234. * Given a DPLL and a desired target rate, round the target rate to a
  235. * possible, programmable rate for this DPLL. Attempts to select the
  236. * minimum possible n. Stores the computed (m, n) in the DPLL's
  237. * dpll_data structure so set_rate() will not need to call this
  238. * (expensive) function again. Returns ~0 if the target rate cannot
  239. * be rounded, or the rounded rate upon success.
  240. */
  241. long omap2_dpll_round_rate(struct clk_hw *hw, unsigned long target_rate,
  242. unsigned long *parent_rate)
  243. {
  244. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  245. int m, n, r, scaled_max_m;
  246. int min_delta_m = INT_MAX, min_delta_n = INT_MAX;
  247. unsigned long scaled_rt_rp;
  248. unsigned long new_rate = 0;
  249. struct dpll_data *dd;
  250. unsigned long ref_rate;
  251. long delta;
  252. long prev_min_delta = LONG_MAX;
  253. const char *clk_name;
  254. if (!clk || !clk->dpll_data)
  255. return ~0;
  256. dd = clk->dpll_data;
  257. ref_rate = __clk_get_rate(dd->clk_ref);
  258. clk_name = __clk_get_name(hw->clk);
  259. pr_debug("clock: %s: starting DPLL round_rate, target rate %lu\n",
  260. clk_name, target_rate);
  261. scaled_rt_rp = target_rate / (ref_rate / DPLL_SCALE_FACTOR);
  262. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  263. dd->last_rounded_rate = 0;
  264. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  265. /* Is the (input clk, divider) pair valid for the DPLL? */
  266. r = _dpll_test_fint(clk, n);
  267. if (r == DPLL_FINT_UNDERFLOW)
  268. break;
  269. else if (r == DPLL_FINT_INVALID)
  270. continue;
  271. /* Compute the scaled DPLL multiplier, based on the divider */
  272. m = scaled_rt_rp * n;
  273. /*
  274. * Since we're counting n up, a m overflow means we
  275. * can bail out completely (since as n increases in
  276. * the next iteration, there's no way that m can
  277. * increase beyond the current m)
  278. */
  279. if (m > scaled_max_m)
  280. break;
  281. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  282. ref_rate);
  283. /* m can't be set low enough for this n - try with a larger n */
  284. if (r == DPLL_MULT_UNDERFLOW)
  285. continue;
  286. /* skip rates above our target rate */
  287. delta = target_rate - new_rate;
  288. if (delta < 0)
  289. continue;
  290. if (delta < prev_min_delta) {
  291. prev_min_delta = delta;
  292. min_delta_m = m;
  293. min_delta_n = n;
  294. }
  295. pr_debug("clock: %s: m = %d: n = %d: new_rate = %lu\n",
  296. clk_name, m, n, new_rate);
  297. if (delta == 0)
  298. break;
  299. }
  300. if (prev_min_delta == LONG_MAX) {
  301. pr_debug("clock: %s: cannot round to rate %lu\n",
  302. clk_name, target_rate);
  303. return ~0;
  304. }
  305. dd->last_rounded_m = min_delta_m;
  306. dd->last_rounded_n = min_delta_n;
  307. dd->last_rounded_rate = target_rate - prev_min_delta;
  308. return dd->last_rounded_rate;
  309. }