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