clkt_dpll.c 10 KB

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