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