05168c98b3d9f06edd0f74f67e9aeba55fada657
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-omap2 / clkt_dpll.c
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
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/clk-provider.h>
20 #include <linux/io.h>
21
22 #include <asm/div64.h>
23
24 #include "soc.h"
25 #include "clock.h"
26 #include "cm-regbits-24xx.h"
27 #include "cm-regbits-34xx.h"
28
29 /* DPLL rate rounding: minimum DPLL multiplier, divider values */
30 #define DPLL_MIN_MULTIPLIER             2
31 #define DPLL_MIN_DIVIDER                1
32
33 /* Possible error results from _dpll_test_mult */
34 #define DPLL_MULT_UNDERFLOW             -1
35
36 /*
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.
41  */
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))
46
47 /*
48  * DPLL valid Fint frequency range for OMAP36xx and OMAP4xxx.
49  * From device data manual section 4.3 "DPLL and DLL Specifications".
50  */
51 #define OMAP3PLUS_DPLL_FINT_JTYPE_MIN   500000
52 #define OMAP3PLUS_DPLL_FINT_JTYPE_MAX   2500000
53
54 /* _dpll_test_fint() return codes */
55 #define DPLL_FINT_UNDERFLOW             -1
56 #define DPLL_FINT_INVALID               -2
57
58 /* Private functions */
59
60 /*
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
64  *
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).
70  */
71 static int _dpll_test_fint(struct clk_hw_omap *clk, u8 n)
72 {
73         struct dpll_data *dd;
74         long fint, fint_min, fint_max;
75         int ret = 0;
76
77         dd = clk->dpll_data;
78
79         /* DPLL divider must result in a valid jitter correction val */
80         fint = __clk_get_rate(__clk_get_parent(clk->hw.clk)) / n;
81
82         if (dd->flags & DPLL_J_TYPE) {
83                 fint_min = OMAP3PLUS_DPLL_FINT_JTYPE_MIN;
84                 fint_max = OMAP3PLUS_DPLL_FINT_JTYPE_MAX;
85         } else {
86                 fint_min = ti_clk_features.fint_min;
87                 fint_max = ti_clk_features.fint_max;
88         }
89
90         if (!fint_min || !fint_max) {
91                 WARN(1, "No fint limits available!\n");
92                 return DPLL_FINT_INVALID;
93         }
94
95         if (fint < ti_clk_features.fint_min) {
96                 pr_debug("rejecting n=%d due to Fint failure, lowering max_divider\n",
97                          n);
98                 dd->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",
102                          n);
103                 dd->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;
109         }
110
111         return ret;
112 }
113
114 static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
115                                             unsigned int m, unsigned int n)
116 {
117         unsigned long long num;
118
119         num = (unsigned long long)parent_rate * m;
120         do_div(num, n);
121         return num;
122 }
123
124 /*
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
131  *
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.
143  */
144 static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
145                            unsigned long target_rate,
146                            unsigned long parent_rate)
147 {
148         int r = 0, carry = 0;
149
150         /* Unscale m and round if necessary */
151         if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
152                 carry = 1;
153         *m = (*m / DPLL_SCALE_FACTOR) + carry;
154
155         /*
156          * The new rate must be <= the target rate to avoid programming
157          * a rate that is impossible for the hardware to handle
158          */
159         *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
160         if (*new_rate > target_rate) {
161                 (*m)--;
162                 *new_rate = 0;
163         }
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
172         if (*new_rate == 0)
173                 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
174
175         return r;
176 }
177
178 /* Public functions */
179 u8 omap2_init_dpll_parent(struct clk_hw *hw)
180 {
181         struct clk_hw_omap *clk = to_clk_hw_omap(hw);
182         u32 v;
183         struct dpll_data *dd;
184
185         dd = clk->dpll_data;
186         if (!dd)
187                 return -EINVAL;
188
189         v = omap2_clk_readl(clk, dd->control_reg);
190         v &= dd->enable_mask;
191         v >>= __ffs(dd->enable_mask);
192
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)
197                         return 1;
198         } else if (cpu_is_omap34xx()) {
199                 if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
200                     v == OMAP3XXX_EN_DPLL_FRBYPASS)
201                         return 1;
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)
206                         return 1;
207         }
208         return 0;
209 }
210
211 /**
212  * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
213  * @clk: struct clk * of a DPLL
214  *
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.
224  */
225 unsigned long omap2_get_dpll_rate(struct clk_hw_omap *clk)
226 {
227         long long dpll_clk;
228         u32 dpll_mult, dpll_div, v;
229         struct dpll_data *dd;
230
231         dd = clk->dpll_data;
232         if (!dd)
233                 return 0;
234
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);
239
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);
253         }
254
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);
260
261         dpll_clk = (long long) __clk_get_rate(dd->clk_ref) * dpll_mult;
262         do_div(dpll_clk, dpll_div + 1);
263
264         return dpll_clk;
265 }
266
267 /* DPLL rate rounding code */
268
269 /**
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
273  *
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.
280  */
281 long omap2_dpll_round_rate(struct clk_hw *hw, unsigned long target_rate,
282                 unsigned long *parent_rate)
283 {
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;
291
292         if (!clk || !clk->dpll_data)
293                 return ~0;
294
295         dd = clk->dpll_data;
296
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);
301
302         scaled_rt_rp = target_rate / (ref_rate / DPLL_SCALE_FACTOR);
303         scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
304
305         dd->last_rounded_rate = 0;
306
307         for (n = dd->min_divider; n <= dd->max_divider; n++) {
308
309                 /* Is the (input clk, divider) pair valid for the DPLL? */
310                 r = _dpll_test_fint(clk, n);
311                 if (r == DPLL_FINT_UNDERFLOW)
312                         break;
313                 else if (r == DPLL_FINT_INVALID)
314                         continue;
315
316                 /* Compute the scaled DPLL multiplier, based on the divider */
317                 m = scaled_rt_rp * n;
318
319                 /*
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)
324                  */
325                 if (m > scaled_max_m)
326                         break;
327
328                 r = _dpll_test_mult(&m, n, &new_rate, target_rate,
329                                     ref_rate);
330
331                 /* m can't be set low enough for this n - try with a larger n */
332                 if (r == DPLL_MULT_UNDERFLOW)
333                         continue;
334
335                 pr_debug("clock: %s: m = %d: n = %d: new_rate = %lu\n",
336                          clk_name, m, n, new_rate);
337
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;
342                         break;
343                 }
344         }
345
346         if (target_rate != new_rate) {
347                 pr_debug("clock: %s: cannot round to rate %lu\n",
348                          clk_name, target_rate);
349                 return ~0;
350         }
351
352         return target_rate;
353 }
354