blob: f343389fc6e845d46a5ffa86ee08386b626c0cb3 [file] [log] [blame]
Paul Walmsley0b96af62010-01-26 20:13:03 -07001/*
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
17#include <linux/kernel.h>
18#include <linux/errno.h>
Mike Turquette32cc0022012-11-10 16:58:41 -070019#ifdef CONFIG_COMMON_CLK
20#include <linux/clk-provider.h>
21#else
Paul Walmsley0b96af62010-01-26 20:13:03 -070022#include <linux/clk.h>
Mike Turquette32cc0022012-11-10 16:58:41 -070023#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -070024#include <linux/io.h>
25
26#include <asm/div64.h>
27
Tony Lindgrendbc04162012-08-31 10:59:07 -070028#include "soc.h"
Paul Walmsley0b96af62010-01-26 20:13:03 -070029#include "clock.h"
Paul Walmsley0b96af62010-01-26 20:13:03 -070030#include "cm-regbits-24xx.h"
31#include "cm-regbits-34xx.h"
32
33/* DPLL rate rounding: minimum DPLL multiplier, divider values */
Paul Walmsley93340a22010-02-22 22:09:12 -070034#define DPLL_MIN_MULTIPLIER 2
Paul Walmsley0b96af62010-01-26 20:13:03 -070035#define DPLL_MIN_DIVIDER 1
36
37/* Possible error results from _dpll_test_mult */
38#define DPLL_MULT_UNDERFLOW -1
39
40/*
41 * Scale factor to mitigate roundoff errors in DPLL rate rounding.
42 * The higher the scale factor, the greater the risk of arithmetic overflow,
43 * but the closer the rounded rate to the target rate. DPLL_SCALE_FACTOR
44 * must be a power of DPLL_SCALE_BASE.
45 */
46#define DPLL_SCALE_FACTOR 64
47#define DPLL_SCALE_BASE 2
48#define DPLL_ROUNDING_VAL ((DPLL_SCALE_BASE / 2) * \
49 (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
50
51/* DPLL valid Fint frequency band limits - from 34xx TRM Section 4.7.6.2 */
Jon Hunter1194d7b2011-10-07 01:44:20 -060052#define OMAP3430_DPLL_FINT_BAND1_MIN 750000
53#define OMAP3430_DPLL_FINT_BAND1_MAX 2100000
54#define OMAP3430_DPLL_FINT_BAND2_MIN 7500000
55#define OMAP3430_DPLL_FINT_BAND2_MAX 21000000
56
57/*
58 * DPLL valid Fint frequency range for OMAP36xx and OMAP4xxx.
59 * From device data manual section 4.3 "DPLL and DLL Specifications".
60 */
61#define OMAP3PLUS_DPLL_FINT_JTYPE_MIN 500000
62#define OMAP3PLUS_DPLL_FINT_JTYPE_MAX 2500000
63#define OMAP3PLUS_DPLL_FINT_MIN 32000
64#define OMAP3PLUS_DPLL_FINT_MAX 52000000
Paul Walmsley0b96af62010-01-26 20:13:03 -070065
66/* _dpll_test_fint() return codes */
67#define DPLL_FINT_UNDERFLOW -1
68#define DPLL_FINT_INVALID -2
69
70/* Private functions */
71
72/*
73 * _dpll_test_fint - test whether an Fint value is valid for the DPLL
74 * @clk: DPLL struct clk to test
75 * @n: divider value (N) to test
76 *
77 * Tests whether a particular divider @n will result in a valid DPLL
78 * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
79 * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
80 * (assuming that it is counting N upwards), or -2 if the enclosing loop
81 * should skip to the next iteration (again assuming N is increasing).
82 */
Mike Turquette32cc0022012-11-10 16:58:41 -070083#ifdef CONFIG_COMMON_CLK
84static int _dpll_test_fint(struct clk_hw_omap *clk, u8 n)
85#else
Paul Walmsley0b96af62010-01-26 20:13:03 -070086static int _dpll_test_fint(struct clk *clk, u8 n)
Mike Turquette32cc0022012-11-10 16:58:41 -070087#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -070088{
89 struct dpll_data *dd;
Jon Hunter1194d7b2011-10-07 01:44:20 -060090 long fint, fint_min, fint_max;
Paul Walmsley0b96af62010-01-26 20:13:03 -070091 int ret = 0;
92
93 dd = clk->dpll_data;
94
95 /* DPLL divider must result in a valid jitter correction val */
Mike Turquette32cc0022012-11-10 16:58:41 -070096#ifdef CONFIG_COMMON_CLK
97 fint = __clk_get_rate(__clk_get_parent(clk->hw.clk)) / n;
98#else
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060099 fint = __clk_get_rate(__clk_get_parent(clk)) / n;
Mike Turquette32cc0022012-11-10 16:58:41 -0700100#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700101
Jon Hunter1194d7b2011-10-07 01:44:20 -0600102 if (cpu_is_omap24xx()) {
103 /* Should not be called for OMAP2, so warn if it is called */
104 WARN(1, "No fint limits available for OMAP2!\n");
105 return DPLL_FINT_INVALID;
106 } else if (cpu_is_omap3430()) {
107 fint_min = OMAP3430_DPLL_FINT_BAND1_MIN;
108 fint_max = OMAP3430_DPLL_FINT_BAND2_MAX;
109 } else if (dd->flags & DPLL_J_TYPE) {
110 fint_min = OMAP3PLUS_DPLL_FINT_JTYPE_MIN;
111 fint_max = OMAP3PLUS_DPLL_FINT_JTYPE_MAX;
112 } else {
113 fint_min = OMAP3PLUS_DPLL_FINT_MIN;
114 fint_max = OMAP3PLUS_DPLL_FINT_MAX;
115 }
116
117 if (fint < fint_min) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600118 pr_debug("rejecting n=%d due to Fint failure, lowering max_divider\n",
119 n);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700120 dd->max_divider = n;
121 ret = DPLL_FINT_UNDERFLOW;
Jon Hunter1194d7b2011-10-07 01:44:20 -0600122 } else if (fint > fint_max) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600123 pr_debug("rejecting n=%d due to Fint failure, boosting min_divider\n",
124 n);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700125 dd->min_divider = n;
126 ret = DPLL_FINT_INVALID;
Jon Hunter1194d7b2011-10-07 01:44:20 -0600127 } else if (cpu_is_omap3430() && fint > OMAP3430_DPLL_FINT_BAND1_MAX &&
128 fint < OMAP3430_DPLL_FINT_BAND2_MIN) {
129 pr_debug("rejecting n=%d due to Fint failure\n", n);
130 ret = DPLL_FINT_INVALID;
Paul Walmsley0b96af62010-01-26 20:13:03 -0700131 }
132
133 return ret;
134}
135
136static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
137 unsigned int m, unsigned int n)
138{
139 unsigned long long num;
140
141 num = (unsigned long long)parent_rate * m;
142 do_div(num, n);
143 return num;
144}
145
146/*
147 * _dpll_test_mult - test a DPLL multiplier value
148 * @m: pointer to the DPLL m (multiplier) value under test
149 * @n: current DPLL n (divider) value under test
150 * @new_rate: pointer to storage for the resulting rounded rate
151 * @target_rate: the desired DPLL rate
152 * @parent_rate: the DPLL's parent clock rate
153 *
154 * This code tests a DPLL multiplier value, ensuring that the
155 * resulting rate will not be higher than the target_rate, and that
156 * the multiplier value itself is valid for the DPLL. Initially, the
157 * integer pointed to by the m argument should be prescaled by
158 * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
159 * a non-scaled m upon return. This non-scaled m will result in a
160 * new_rate as close as possible to target_rate (but not greater than
161 * target_rate) given the current (parent_rate, n, prescaled m)
162 * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
163 * non-scaled m attempted to underflow, which can allow the calling
164 * function to bail out early; or 0 upon success.
165 */
166static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
167 unsigned long target_rate,
168 unsigned long parent_rate)
169{
170 int r = 0, carry = 0;
171
172 /* Unscale m and round if necessary */
173 if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
174 carry = 1;
175 *m = (*m / DPLL_SCALE_FACTOR) + carry;
176
177 /*
178 * The new rate must be <= the target rate to avoid programming
179 * a rate that is impossible for the hardware to handle
180 */
181 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
182 if (*new_rate > target_rate) {
183 (*m)--;
184 *new_rate = 0;
185 }
186
187 /* Guard against m underflow */
188 if (*m < DPLL_MIN_MULTIPLIER) {
189 *m = DPLL_MIN_MULTIPLIER;
190 *new_rate = 0;
191 r = DPLL_MULT_UNDERFLOW;
192 }
193
194 if (*new_rate == 0)
195 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
196
197 return r;
198}
199
200/* Public functions */
Mike Turquette32cc0022012-11-10 16:58:41 -0700201#ifdef CONFIG_COMMON_CLK
202u8 omap2_init_dpll_parent(struct clk_hw *hw)
203{
204 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
205#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700206void omap2_init_dpll_parent(struct clk *clk)
207{
Mike Turquette32cc0022012-11-10 16:58:41 -0700208#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700209 u32 v;
210 struct dpll_data *dd;
211
212 dd = clk->dpll_data;
213 if (!dd)
Mike Turquette32cc0022012-11-10 16:58:41 -0700214#ifdef CONFIG_COMMON_CLK
215 return -EINVAL;
216#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700217 return;
Mike Turquette32cc0022012-11-10 16:58:41 -0700218#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700219
Paul Walmsley0b96af62010-01-26 20:13:03 -0700220 v = __raw_readl(dd->control_reg);
221 v &= dd->enable_mask;
222 v >>= __ffs(dd->enable_mask);
223
Paul Walmsley241d3a82011-02-16 15:38:39 -0700224 /* Reparent the struct clk in case the dpll is in bypass */
Paul Walmsley0b96af62010-01-26 20:13:03 -0700225 if (cpu_is_omap24xx()) {
226 if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
227 v == OMAP2XXX_EN_DPLL_FRBYPASS)
Mike Turquette32cc0022012-11-10 16:58:41 -0700228#ifdef CONFIG_COMMON_CLK
229 return 1;
230#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700231 clk_reparent(clk, dd->clk_bypass);
Mike Turquette32cc0022012-11-10 16:58:41 -0700232#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700233 } else if (cpu_is_omap34xx()) {
234 if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
235 v == OMAP3XXX_EN_DPLL_FRBYPASS)
Mike Turquette32cc0022012-11-10 16:58:41 -0700236#ifdef CONFIG_COMMON_CLK
237 return 1;
238#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700239 clk_reparent(clk, dd->clk_bypass);
Mike Turquette32cc0022012-11-10 16:58:41 -0700240#endif
Vaibhav Hiremath78da2642012-08-24 20:24:24 +0530241 } else if (soc_is_am33xx() || cpu_is_omap44xx()) {
Paul Walmsley0b96af62010-01-26 20:13:03 -0700242 if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
243 v == OMAP4XXX_EN_DPLL_FRBYPASS ||
244 v == OMAP4XXX_EN_DPLL_MNBYPASS)
Mike Turquette32cc0022012-11-10 16:58:41 -0700245#ifdef CONFIG_COMMON_CLK
246 return 1;
247#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700248 clk_reparent(clk, dd->clk_bypass);
Mike Turquette32cc0022012-11-10 16:58:41 -0700249#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700250 }
Mike Turquette32cc0022012-11-10 16:58:41 -0700251#ifdef CONFIG_COMMON_CLK
252 return 0;
253#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700254 return;
Mike Turquette32cc0022012-11-10 16:58:41 -0700255#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700256}
257
258/**
259 * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
260 * @clk: struct clk * of a DPLL
261 *
262 * DPLLs can be locked or bypassed - basically, enabled or disabled.
263 * When locked, the DPLL output depends on the M and N values. When
264 * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
265 * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
266 * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
267 * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
268 * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
269 * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
270 * if the clock @clk is not a DPLL.
271 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700272#ifdef CONFIG_COMMON_CLK
273unsigned long omap2_get_dpll_rate(struct clk_hw_omap *clk)
274#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700275u32 omap2_get_dpll_rate(struct clk *clk)
Mike Turquette32cc0022012-11-10 16:58:41 -0700276#endif
Paul Walmsley0b96af62010-01-26 20:13:03 -0700277{
278 long long dpll_clk;
279 u32 dpll_mult, dpll_div, v;
280 struct dpll_data *dd;
281
282 dd = clk->dpll_data;
283 if (!dd)
284 return 0;
285
286 /* Return bypass rate if DPLL is bypassed */
287 v = __raw_readl(dd->control_reg);
288 v &= dd->enable_mask;
289 v >>= __ffs(dd->enable_mask);
290
291 if (cpu_is_omap24xx()) {
292 if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
293 v == OMAP2XXX_EN_DPLL_FRBYPASS)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600294 return __clk_get_rate(dd->clk_bypass);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700295 } else if (cpu_is_omap34xx()) {
296 if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
297 v == OMAP3XXX_EN_DPLL_FRBYPASS)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600298 return __clk_get_rate(dd->clk_bypass);
Vaibhav Hiremath78da2642012-08-24 20:24:24 +0530299 } else if (soc_is_am33xx() || cpu_is_omap44xx()) {
Paul Walmsley0b96af62010-01-26 20:13:03 -0700300 if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
301 v == OMAP4XXX_EN_DPLL_FRBYPASS ||
302 v == OMAP4XXX_EN_DPLL_MNBYPASS)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600303 return __clk_get_rate(dd->clk_bypass);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700304 }
305
306 v = __raw_readl(dd->mult_div1_reg);
307 dpll_mult = v & dd->mult_mask;
308 dpll_mult >>= __ffs(dd->mult_mask);
309 dpll_div = v & dd->div1_mask;
310 dpll_div >>= __ffs(dd->div1_mask);
311
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600312 dpll_clk = (long long) __clk_get_rate(dd->clk_ref) * dpll_mult;
Paul Walmsley0b96af62010-01-26 20:13:03 -0700313 do_div(dpll_clk, dpll_div + 1);
314
315 return dpll_clk;
316}
317
318/* DPLL rate rounding code */
319
320/**
Paul Walmsley0b96af62010-01-26 20:13:03 -0700321 * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
322 * @clk: struct clk * for a DPLL
323 * @target_rate: desired DPLL clock rate
324 *
Paul Walmsley241d3a82011-02-16 15:38:39 -0700325 * Given a DPLL and a desired target rate, round the target rate to a
326 * possible, programmable rate for this DPLL. Attempts to select the
327 * minimum possible n. Stores the computed (m, n) in the DPLL's
328 * dpll_data structure so set_rate() will not need to call this
329 * (expensive) function again. Returns ~0 if the target rate cannot
330 * be rounded, or the rounded rate upon success.
Paul Walmsley0b96af62010-01-26 20:13:03 -0700331 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700332#ifdef CONFIG_COMMON_CLK
333long omap2_dpll_round_rate(struct clk_hw *hw, unsigned long target_rate,
334 unsigned long *parent_rate)
335{
336 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
337#else
Paul Walmsley0b96af62010-01-26 20:13:03 -0700338long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
339{
Mike Turquette32cc0022012-11-10 16:58:41 -0700340#endif
Paul Walmsley241d3a82011-02-16 15:38:39 -0700341 int m, n, r, scaled_max_m;
342 unsigned long scaled_rt_rp;
343 unsigned long new_rate = 0;
Paul Walmsley0b96af62010-01-26 20:13:03 -0700344 struct dpll_data *dd;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600345 unsigned long ref_rate;
346 const char *clk_name;
Paul Walmsley0b96af62010-01-26 20:13:03 -0700347
348 if (!clk || !clk->dpll_data)
349 return ~0;
350
351 dd = clk->dpll_data;
352
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600353 ref_rate = __clk_get_rate(dd->clk_ref);
Mike Turquette32cc0022012-11-10 16:58:41 -0700354#ifdef CONFIG_COMMON_CLK
355 clk_name = __clk_get_name(hw->clk);
356#else
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600357 clk_name = __clk_get_name(clk);
Mike Turquette32cc0022012-11-10 16:58:41 -0700358#endif
Paul Walmsley241d3a82011-02-16 15:38:39 -0700359 pr_debug("clock: %s: starting DPLL round_rate, target rate %ld\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600360 clk_name, target_rate);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700361
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600362 scaled_rt_rp = target_rate / (ref_rate / DPLL_SCALE_FACTOR);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700363 scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
364
365 dd->last_rounded_rate = 0;
366
367 for (n = dd->min_divider; n <= dd->max_divider; n++) {
368
369 /* Is the (input clk, divider) pair valid for the DPLL? */
370 r = _dpll_test_fint(clk, n);
371 if (r == DPLL_FINT_UNDERFLOW)
372 break;
373 else if (r == DPLL_FINT_INVALID)
374 continue;
375
376 /* Compute the scaled DPLL multiplier, based on the divider */
377 m = scaled_rt_rp * n;
378
379 /*
380 * Since we're counting n up, a m overflow means we
381 * can bail out completely (since as n increases in
382 * the next iteration, there's no way that m can
383 * increase beyond the current m)
384 */
385 if (m > scaled_max_m)
386 break;
387
388 r = _dpll_test_mult(&m, n, &new_rate, target_rate,
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600389 ref_rate);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700390
391 /* m can't be set low enough for this n - try with a larger n */
392 if (r == DPLL_MULT_UNDERFLOW)
393 continue;
394
Paul Walmsley241d3a82011-02-16 15:38:39 -0700395 pr_debug("clock: %s: m = %d: n = %d: new_rate = %ld\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600396 clk_name, m, n, new_rate);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700397
Paul Walmsley241d3a82011-02-16 15:38:39 -0700398 if (target_rate == new_rate) {
399 dd->last_rounded_m = m;
400 dd->last_rounded_n = n;
401 dd->last_rounded_rate = target_rate;
402 break;
Paul Walmsley0b96af62010-01-26 20:13:03 -0700403 }
404 }
405
Paul Walmsley241d3a82011-02-16 15:38:39 -0700406 if (target_rate != new_rate) {
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600407 pr_debug("clock: %s: cannot round to rate %ld\n",
408 clk_name, target_rate);
Paul Walmsley0b96af62010-01-26 20:13:03 -0700409 return ~0;
410 }
411
Paul Walmsley241d3a82011-02-16 15:38:39 -0700412 return target_rate;
Paul Walmsley0b96af62010-01-26 20:13:03 -0700413}
414