2 * OMAP2/3/4 DPLL clock functions
4 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2010 Nokia Corporation
8 * Richard Woodruff <r-woodruff2@ti.com>
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.
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/clk-provider.h>
22 #include <asm/div64.h>
26 #include "cm-regbits-24xx.h"
27 #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
33 /* Possible error results from _dpll_test_mult */
34 #define DPLL_MULT_UNDERFLOW -1
37 * Scale factor to mitigate roundoff errors in DPLL rate rounding.
38 * The higher the scale factor, the greater the risk of arithmetic overflow,
39 * but the closer the rounded rate to the target rate. DPLL_SCALE_FACTOR
40 * must be a power of DPLL_SCALE_BASE.
42 #define DPLL_SCALE_FACTOR 64
43 #define DPLL_SCALE_BASE 2
44 #define DPLL_ROUNDING_VAL ((DPLL_SCALE_BASE / 2) * \
45 (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
48 * DPLL valid Fint frequency range for OMAP36xx and OMAP4xxx.
49 * From device data manual section 4.3 "DPLL and DLL Specifications".
51 #define OMAP3PLUS_DPLL_FINT_JTYPE_MIN 500000
52 #define OMAP3PLUS_DPLL_FINT_JTYPE_MAX 2500000
54 /* _dpll_test_fint() return codes */
55 #define DPLL_FINT_UNDERFLOW -1
56 #define DPLL_FINT_INVALID -2
58 /* Private functions */
61 * _dpll_test_fint - test whether an Fint value is valid for the DPLL
62 * @clk: DPLL struct clk to test
63 * @n: divider value (N) to test
65 * Tests whether a particular divider @n will result in a valid DPLL
66 * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
67 * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
68 * (assuming that it is counting N upwards), or -2 if the enclosing loop
69 * should skip to the next iteration (again assuming N is increasing).
71 static int _dpll_test_fint(struct clk_hw_omap *clk, u8 n)
74 long fint, fint_min, fint_max;
79 /* DPLL divider must result in a valid jitter correction val */
80 fint = __clk_get_rate(__clk_get_parent(clk->hw.clk)) / n;
82 if (dd->flags & DPLL_J_TYPE) {
83 fint_min = OMAP3PLUS_DPLL_FINT_JTYPE_MIN;
84 fint_max = OMAP3PLUS_DPLL_FINT_JTYPE_MAX;
86 fint_min = ti_clk_features.fint_min;
87 fint_max = ti_clk_features.fint_max;
90 if (!fint_min || !fint_max) {
91 WARN(1, "No fint limits available!\n");
92 return DPLL_FINT_INVALID;
95 if (fint < ti_clk_features.fint_min) {
96 pr_debug("rejecting n=%d due to Fint failure, lowering max_divider\n",
99 ret = DPLL_FINT_UNDERFLOW;
100 } else if (fint > ti_clk_features.fint_max) {
101 pr_debug("rejecting n=%d due to Fint failure, boosting min_divider\n",
104 ret = DPLL_FINT_INVALID;
105 } else if (fint > ti_clk_features.fint_band1_max &&
106 fint < ti_clk_features.fint_band2_min) {
107 pr_debug("rejecting n=%d due to Fint failure\n", n);
108 ret = DPLL_FINT_INVALID;
114 static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
115 unsigned int m, unsigned int n)
117 unsigned long long num;
119 num = (unsigned long long)parent_rate * m;
125 * _dpll_test_mult - test a DPLL multiplier value
126 * @m: pointer to the DPLL m (multiplier) value under test
127 * @n: current DPLL n (divider) value under test
128 * @new_rate: pointer to storage for the resulting rounded rate
129 * @target_rate: the desired DPLL rate
130 * @parent_rate: the DPLL's parent clock rate
132 * This code tests a DPLL multiplier value, ensuring that the
133 * resulting rate will not be higher than the target_rate, and that
134 * the multiplier value itself is valid for the DPLL. Initially, the
135 * integer pointed to by the m argument should be prescaled by
136 * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
137 * a non-scaled m upon return. This non-scaled m will result in a
138 * new_rate as close as possible to target_rate (but not greater than
139 * target_rate) given the current (parent_rate, n, prescaled m)
140 * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
141 * non-scaled m attempted to underflow, which can allow the calling
142 * function to bail out early; or 0 upon success.
144 static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
145 unsigned long target_rate,
146 unsigned long parent_rate)
148 int r = 0, carry = 0;
150 /* Unscale m and round if necessary */
151 if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
153 *m = (*m / DPLL_SCALE_FACTOR) + carry;
156 * The new rate must be <= the target rate to avoid programming
157 * a rate that is impossible for the hardware to handle
159 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
160 if (*new_rate > target_rate) {
165 /* Guard against m underflow */
166 if (*m < DPLL_MIN_MULTIPLIER) {
167 *m = DPLL_MIN_MULTIPLIER;
169 r = DPLL_MULT_UNDERFLOW;
173 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
178 /* Public functions */
179 u8 omap2_init_dpll_parent(struct clk_hw *hw)
181 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
183 struct dpll_data *dd;
189 v = omap2_clk_readl(clk, dd->control_reg);
190 v &= dd->enable_mask;
191 v >>= __ffs(dd->enable_mask);
193 /* Reparent the struct clk in case the dpll is in bypass */
194 if (cpu_is_omap24xx()) {
195 if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
196 v == OMAP2XXX_EN_DPLL_FRBYPASS)
198 } else if (cpu_is_omap34xx()) {
199 if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
200 v == OMAP3XXX_EN_DPLL_FRBYPASS)
202 } else if (soc_is_am33xx() || cpu_is_omap44xx() || soc_is_am43xx()) {
203 if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
204 v == OMAP4XXX_EN_DPLL_FRBYPASS ||
205 v == OMAP4XXX_EN_DPLL_MNBYPASS)
212 * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
213 * @clk: struct clk * of a DPLL
215 * DPLLs can be locked or bypassed - basically, enabled or disabled.
216 * When locked, the DPLL output depends on the M and N values. When
217 * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
218 * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
219 * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
220 * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
221 * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
222 * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
223 * if the clock @clk is not a DPLL.
225 unsigned long omap2_get_dpll_rate(struct clk_hw_omap *clk)
228 u32 dpll_mult, dpll_div, v;
229 struct dpll_data *dd;
235 /* Return bypass rate if DPLL is bypassed */
236 v = omap2_clk_readl(clk, dd->control_reg);
237 v &= dd->enable_mask;
238 v >>= __ffs(dd->enable_mask);
240 if (cpu_is_omap24xx()) {
241 if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
242 v == OMAP2XXX_EN_DPLL_FRBYPASS)
243 return __clk_get_rate(dd->clk_bypass);
244 } else if (cpu_is_omap34xx()) {
245 if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
246 v == OMAP3XXX_EN_DPLL_FRBYPASS)
247 return __clk_get_rate(dd->clk_bypass);
248 } else if (soc_is_am33xx() || cpu_is_omap44xx() || soc_is_am43xx()) {
249 if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
250 v == OMAP4XXX_EN_DPLL_FRBYPASS ||
251 v == OMAP4XXX_EN_DPLL_MNBYPASS)
252 return __clk_get_rate(dd->clk_bypass);
255 v = omap2_clk_readl(clk, dd->mult_div1_reg);
256 dpll_mult = v & dd->mult_mask;
257 dpll_mult >>= __ffs(dd->mult_mask);
258 dpll_div = v & dd->div1_mask;
259 dpll_div >>= __ffs(dd->div1_mask);
261 dpll_clk = (long long) __clk_get_rate(dd->clk_ref) * dpll_mult;
262 do_div(dpll_clk, dpll_div + 1);
267 /* DPLL rate rounding code */
270 * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
271 * @clk: struct clk * for a DPLL
272 * @target_rate: desired DPLL clock rate
274 * Given a DPLL and a desired target rate, round the target rate to a
275 * possible, programmable rate for this DPLL. Attempts to select the
276 * minimum possible n. Stores the computed (m, n) in the DPLL's
277 * dpll_data structure so set_rate() will not need to call this
278 * (expensive) function again. Returns ~0 if the target rate cannot
279 * be rounded, or the rounded rate upon success.
281 long omap2_dpll_round_rate(struct clk_hw *hw, unsigned long target_rate,
282 unsigned long *parent_rate)
284 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
285 int m, n, r, scaled_max_m;
286 unsigned long scaled_rt_rp;
287 unsigned long new_rate = 0;
288 struct dpll_data *dd;
289 unsigned long ref_rate;
290 const char *clk_name;
292 if (!clk || !clk->dpll_data)
297 ref_rate = __clk_get_rate(dd->clk_ref);
298 clk_name = __clk_get_name(hw->clk);
299 pr_debug("clock: %s: starting DPLL round_rate, target rate %lu\n",
300 clk_name, target_rate);
302 scaled_rt_rp = target_rate / (ref_rate / DPLL_SCALE_FACTOR);
303 scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
305 dd->last_rounded_rate = 0;
307 for (n = dd->min_divider; n <= dd->max_divider; n++) {
309 /* Is the (input clk, divider) pair valid for the DPLL? */
310 r = _dpll_test_fint(clk, n);
311 if (r == DPLL_FINT_UNDERFLOW)
313 else if (r == DPLL_FINT_INVALID)
316 /* Compute the scaled DPLL multiplier, based on the divider */
317 m = scaled_rt_rp * n;
320 * Since we're counting n up, a m overflow means we
321 * can bail out completely (since as n increases in
322 * the next iteration, there's no way that m can
323 * increase beyond the current m)
325 if (m > scaled_max_m)
328 r = _dpll_test_mult(&m, n, &new_rate, target_rate,
331 /* m can't be set low enough for this n - try with a larger n */
332 if (r == DPLL_MULT_UNDERFLOW)
335 pr_debug("clock: %s: m = %d: n = %d: new_rate = %lu\n",
336 clk_name, m, n, new_rate);
338 if (target_rate == new_rate) {
339 dd->last_rounded_m = m;
340 dd->last_rounded_n = n;
341 dd->last_rounded_rate = target_rate;
346 if (target_rate != new_rate) {
347 pr_debug("clock: %s: cannot round to rate %lu\n",
348 clk_name, target_rate);