1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * OMAP3/4 - specific DPLL control functions
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2009-2010 Texas Instruments, Inc.
6*4882a593Smuzhiyun * Copyright (C) 2009-2010 Nokia Corporation
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Written by Paul Walmsley
9*4882a593Smuzhiyun * Testing and integration fixes by Jouni Högander
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * 36xx support added by Vishwanath BS, Richard Woodruff, and Nishanth
12*4882a593Smuzhiyun * Menon
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Parts of this code are based on code written by
15*4882a593Smuzhiyun * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/device.h>
20*4882a593Smuzhiyun #include <linux/list.h>
21*4882a593Smuzhiyun #include <linux/errno.h>
22*4882a593Smuzhiyun #include <linux/delay.h>
23*4882a593Smuzhiyun #include <linux/clk.h>
24*4882a593Smuzhiyun #include <linux/io.h>
25*4882a593Smuzhiyun #include <linux/bitops.h>
26*4882a593Smuzhiyun #include <linux/clkdev.h>
27*4882a593Smuzhiyun #include <linux/clk/ti.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "clock.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* CM_AUTOIDLE_PLL*.AUTO_* bit values */
32*4882a593Smuzhiyun #define DPLL_AUTOIDLE_DISABLE 0x0
33*4882a593Smuzhiyun #define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #define MAX_DPLL_WAIT_TRIES 1000000
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #define OMAP3XXX_EN_DPLL_LOCKED 0x7
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /* Forward declarations */
40*4882a593Smuzhiyun static u32 omap3_dpll_autoidle_read(struct clk_hw_omap *clk);
41*4882a593Smuzhiyun static void omap3_dpll_deny_idle(struct clk_hw_omap *clk);
42*4882a593Smuzhiyun static void omap3_dpll_allow_idle(struct clk_hw_omap *clk);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* Private functions */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
_omap3_dpll_write_clken(struct clk_hw_omap * clk,u8 clken_bits)47*4882a593Smuzhiyun static void _omap3_dpll_write_clken(struct clk_hw_omap *clk, u8 clken_bits)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun const struct dpll_data *dd;
50*4882a593Smuzhiyun u32 v;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun dd = clk->dpll_data;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
55*4882a593Smuzhiyun v &= ~dd->enable_mask;
56*4882a593Smuzhiyun v |= clken_bits << __ffs(dd->enable_mask);
57*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->control_reg);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
_omap3_wait_dpll_status(struct clk_hw_omap * clk,u8 state)61*4882a593Smuzhiyun static int _omap3_wait_dpll_status(struct clk_hw_omap *clk, u8 state)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun const struct dpll_data *dd;
64*4882a593Smuzhiyun int i = 0;
65*4882a593Smuzhiyun int ret = -EINVAL;
66*4882a593Smuzhiyun const char *clk_name;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun dd = clk->dpll_data;
69*4882a593Smuzhiyun clk_name = clk_hw_get_name(&clk->hw);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun state <<= __ffs(dd->idlest_mask);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun while (((ti_clk_ll_ops->clk_readl(&dd->idlest_reg) & dd->idlest_mask)
74*4882a593Smuzhiyun != state) && i < MAX_DPLL_WAIT_TRIES) {
75*4882a593Smuzhiyun i++;
76*4882a593Smuzhiyun udelay(1);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (i == MAX_DPLL_WAIT_TRIES) {
80*4882a593Smuzhiyun pr_err("clock: %s failed transition to '%s'\n",
81*4882a593Smuzhiyun clk_name, (state) ? "locked" : "bypassed");
82*4882a593Smuzhiyun } else {
83*4882a593Smuzhiyun pr_debug("clock: %s transition to '%s' in %d loops\n",
84*4882a593Smuzhiyun clk_name, (state) ? "locked" : "bypassed", i);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun ret = 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return ret;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* From 3430 TRM ES2 4.7.6.2 */
_omap3_dpll_compute_freqsel(struct clk_hw_omap * clk,u8 n)93*4882a593Smuzhiyun static u16 _omap3_dpll_compute_freqsel(struct clk_hw_omap *clk, u8 n)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun unsigned long fint;
96*4882a593Smuzhiyun u16 f = 0;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun fint = clk_hw_get_rate(clk->dpll_data->clk_ref) / n;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun pr_debug("clock: fint is %lu\n", fint);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (fint >= 750000 && fint <= 1000000)
103*4882a593Smuzhiyun f = 0x3;
104*4882a593Smuzhiyun else if (fint > 1000000 && fint <= 1250000)
105*4882a593Smuzhiyun f = 0x4;
106*4882a593Smuzhiyun else if (fint > 1250000 && fint <= 1500000)
107*4882a593Smuzhiyun f = 0x5;
108*4882a593Smuzhiyun else if (fint > 1500000 && fint <= 1750000)
109*4882a593Smuzhiyun f = 0x6;
110*4882a593Smuzhiyun else if (fint > 1750000 && fint <= 2100000)
111*4882a593Smuzhiyun f = 0x7;
112*4882a593Smuzhiyun else if (fint > 7500000 && fint <= 10000000)
113*4882a593Smuzhiyun f = 0xB;
114*4882a593Smuzhiyun else if (fint > 10000000 && fint <= 12500000)
115*4882a593Smuzhiyun f = 0xC;
116*4882a593Smuzhiyun else if (fint > 12500000 && fint <= 15000000)
117*4882a593Smuzhiyun f = 0xD;
118*4882a593Smuzhiyun else if (fint > 15000000 && fint <= 17500000)
119*4882a593Smuzhiyun f = 0xE;
120*4882a593Smuzhiyun else if (fint > 17500000 && fint <= 21000000)
121*4882a593Smuzhiyun f = 0xF;
122*4882a593Smuzhiyun else
123*4882a593Smuzhiyun pr_debug("clock: unknown freqsel setting for %d\n", n);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun return f;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
130*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
133*4882a593Smuzhiyun * readiness before returning. Will save and restore the DPLL's
134*4882a593Smuzhiyun * autoidle state across the enable, per the CDP code. If the DPLL
135*4882a593Smuzhiyun * locked successfully, return 0; if the DPLL did not lock in the time
136*4882a593Smuzhiyun * allotted, or DPLL3 was passed in, return -EINVAL.
137*4882a593Smuzhiyun */
_omap3_noncore_dpll_lock(struct clk_hw_omap * clk)138*4882a593Smuzhiyun static int _omap3_noncore_dpll_lock(struct clk_hw_omap *clk)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun const struct dpll_data *dd;
141*4882a593Smuzhiyun u8 ai;
142*4882a593Smuzhiyun u8 state = 1;
143*4882a593Smuzhiyun int r = 0;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun pr_debug("clock: locking DPLL %s\n", clk_hw_get_name(&clk->hw));
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun dd = clk->dpll_data;
148*4882a593Smuzhiyun state <<= __ffs(dd->idlest_mask);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* Check if already locked */
151*4882a593Smuzhiyun if ((ti_clk_ll_ops->clk_readl(&dd->idlest_reg) & dd->idlest_mask) ==
152*4882a593Smuzhiyun state)
153*4882a593Smuzhiyun goto done;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun ai = omap3_dpll_autoidle_read(clk);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun if (ai)
158*4882a593Smuzhiyun omap3_dpll_deny_idle(clk);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, DPLL_LOCKED);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun r = _omap3_wait_dpll_status(clk, 1);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (ai)
165*4882a593Smuzhiyun omap3_dpll_allow_idle(clk);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun done:
168*4882a593Smuzhiyun return r;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
173*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
174*4882a593Smuzhiyun *
175*4882a593Smuzhiyun * Instructs a non-CORE DPLL to enter low-power bypass mode. In
176*4882a593Smuzhiyun * bypass mode, the DPLL's rate is set equal to its parent clock's
177*4882a593Smuzhiyun * rate. Waits for the DPLL to report readiness before returning.
178*4882a593Smuzhiyun * Will save and restore the DPLL's autoidle state across the enable,
179*4882a593Smuzhiyun * per the CDP code. If the DPLL entered bypass mode successfully,
180*4882a593Smuzhiyun * return 0; if the DPLL did not enter bypass in the time allotted, or
181*4882a593Smuzhiyun * DPLL3 was passed in, or the DPLL does not support low-power bypass,
182*4882a593Smuzhiyun * return -EINVAL.
183*4882a593Smuzhiyun */
_omap3_noncore_dpll_bypass(struct clk_hw_omap * clk)184*4882a593Smuzhiyun static int _omap3_noncore_dpll_bypass(struct clk_hw_omap *clk)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun int r;
187*4882a593Smuzhiyun u8 ai;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
190*4882a593Smuzhiyun return -EINVAL;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun pr_debug("clock: configuring DPLL %s for low-power bypass\n",
193*4882a593Smuzhiyun clk_hw_get_name(&clk->hw));
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun ai = omap3_dpll_autoidle_read(clk);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun r = _omap3_wait_dpll_status(clk, 0);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun if (ai)
202*4882a593Smuzhiyun omap3_dpll_allow_idle(clk);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return r;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun * _omap3_noncore_dpll_stop - instruct a DPLL to stop
209*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * Instructs a non-CORE DPLL to enter low-power stop. Will save and
212*4882a593Smuzhiyun * restore the DPLL's autoidle state across the stop, per the CDP
213*4882a593Smuzhiyun * code. If DPLL3 was passed in, or the DPLL does not support
214*4882a593Smuzhiyun * low-power stop, return -EINVAL; otherwise, return 0.
215*4882a593Smuzhiyun */
_omap3_noncore_dpll_stop(struct clk_hw_omap * clk)216*4882a593Smuzhiyun static int _omap3_noncore_dpll_stop(struct clk_hw_omap *clk)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun u8 ai;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
221*4882a593Smuzhiyun return -EINVAL;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun pr_debug("clock: stopping DPLL %s\n", clk_hw_get_name(&clk->hw));
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun ai = omap3_dpll_autoidle_read(clk);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun if (ai)
230*4882a593Smuzhiyun omap3_dpll_allow_idle(clk);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun return 0;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /**
236*4882a593Smuzhiyun * _lookup_dco - Lookup DCO used by j-type DPLL
237*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
238*4882a593Smuzhiyun * @dco: digital control oscillator selector
239*4882a593Smuzhiyun * @m: DPLL multiplier to set
240*4882a593Smuzhiyun * @n: DPLL divider to set
241*4882a593Smuzhiyun *
242*4882a593Smuzhiyun * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
243*4882a593Smuzhiyun *
244*4882a593Smuzhiyun * XXX This code is not needed for 3430/AM35xx; can it be optimized
245*4882a593Smuzhiyun * out in non-multi-OMAP builds for those chips?
246*4882a593Smuzhiyun */
_lookup_dco(struct clk_hw_omap * clk,u8 * dco,u16 m,u8 n)247*4882a593Smuzhiyun static void _lookup_dco(struct clk_hw_omap *clk, u8 *dco, u16 m, u8 n)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun unsigned long fint, clkinp; /* watch out for overflow */
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun clkinp = clk_hw_get_rate(clk_hw_get_parent(&clk->hw));
252*4882a593Smuzhiyun fint = (clkinp / n) * m;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun if (fint < 1000000000)
255*4882a593Smuzhiyun *dco = 2;
256*4882a593Smuzhiyun else
257*4882a593Smuzhiyun *dco = 4;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /**
261*4882a593Smuzhiyun * _lookup_sddiv - Calculate sigma delta divider for j-type DPLL
262*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
263*4882a593Smuzhiyun * @sd_div: target sigma-delta divider
264*4882a593Smuzhiyun * @m: DPLL multiplier to set
265*4882a593Smuzhiyun * @n: DPLL divider to set
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * XXX This code is not needed for 3430/AM35xx; can it be optimized
270*4882a593Smuzhiyun * out in non-multi-OMAP builds for those chips?
271*4882a593Smuzhiyun */
_lookup_sddiv(struct clk_hw_omap * clk,u8 * sd_div,u16 m,u8 n)272*4882a593Smuzhiyun static void _lookup_sddiv(struct clk_hw_omap *clk, u8 *sd_div, u16 m, u8 n)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun unsigned long clkinp, sd; /* watch out for overflow */
275*4882a593Smuzhiyun int mod1, mod2;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun clkinp = clk_hw_get_rate(clk_hw_get_parent(&clk->hw));
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * target sigma-delta to near 250MHz
281*4882a593Smuzhiyun * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)]
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */
284*4882a593Smuzhiyun mod1 = (clkinp * m) % (250 * n);
285*4882a593Smuzhiyun sd = (clkinp * m) / (250 * n);
286*4882a593Smuzhiyun mod2 = sd % 10;
287*4882a593Smuzhiyun sd /= 10;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun if (mod1 || mod2)
290*4882a593Smuzhiyun sd++;
291*4882a593Smuzhiyun *sd_div = sd;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly
296*4882a593Smuzhiyun * @clk: struct clk * of DPLL to set
297*4882a593Smuzhiyun * @freqsel: FREQSEL value to set
298*4882a593Smuzhiyun *
299*4882a593Smuzhiyun * Program the DPLL with the last M, N values calculated, and wait for
300*4882a593Smuzhiyun * the DPLL to lock. Returns -EINVAL upon error, or 0 upon success.
301*4882a593Smuzhiyun */
omap3_noncore_dpll_program(struct clk_hw_omap * clk,u16 freqsel)302*4882a593Smuzhiyun static int omap3_noncore_dpll_program(struct clk_hw_omap *clk, u16 freqsel)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun struct dpll_data *dd = clk->dpll_data;
305*4882a593Smuzhiyun u8 dco, sd_div, ai = 0;
306*4882a593Smuzhiyun u32 v;
307*4882a593Smuzhiyun bool errata_i810;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
310*4882a593Smuzhiyun _omap3_noncore_dpll_bypass(clk);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun * Set jitter correction. Jitter correction applicable for OMAP343X
314*4882a593Smuzhiyun * only since freqsel field is no longer present on other devices.
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun if (ti_clk_get_features()->flags & TI_CLK_DPLL_HAS_FREQSEL) {
317*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
318*4882a593Smuzhiyun v &= ~dd->freqsel_mask;
319*4882a593Smuzhiyun v |= freqsel << __ffs(dd->freqsel_mask);
320*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->control_reg);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /* Set DPLL multiplier, divider */
324*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun /* Handle Duty Cycle Correction */
327*4882a593Smuzhiyun if (dd->dcc_mask) {
328*4882a593Smuzhiyun if (dd->last_rounded_rate >= dd->dcc_rate)
329*4882a593Smuzhiyun v |= dd->dcc_mask; /* Enable DCC */
330*4882a593Smuzhiyun else
331*4882a593Smuzhiyun v &= ~dd->dcc_mask; /* Disable DCC */
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun v &= ~(dd->mult_mask | dd->div1_mask);
335*4882a593Smuzhiyun v |= dd->last_rounded_m << __ffs(dd->mult_mask);
336*4882a593Smuzhiyun v |= (dd->last_rounded_n - 1) << __ffs(dd->div1_mask);
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /* Configure dco and sd_div for dplls that have these fields */
339*4882a593Smuzhiyun if (dd->dco_mask) {
340*4882a593Smuzhiyun _lookup_dco(clk, &dco, dd->last_rounded_m, dd->last_rounded_n);
341*4882a593Smuzhiyun v &= ~(dd->dco_mask);
342*4882a593Smuzhiyun v |= dco << __ffs(dd->dco_mask);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun if (dd->sddiv_mask) {
345*4882a593Smuzhiyun _lookup_sddiv(clk, &sd_div, dd->last_rounded_m,
346*4882a593Smuzhiyun dd->last_rounded_n);
347*4882a593Smuzhiyun v &= ~(dd->sddiv_mask);
348*4882a593Smuzhiyun v |= sd_div << __ffs(dd->sddiv_mask);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /*
352*4882a593Smuzhiyun * Errata i810 - DPLL controller can get stuck while transitioning
353*4882a593Smuzhiyun * to a power saving state. Software must ensure the DPLL can not
354*4882a593Smuzhiyun * transition to a low power state while changing M/N values.
355*4882a593Smuzhiyun * Easiest way to accomplish this is to prevent DPLL autoidle
356*4882a593Smuzhiyun * before doing the M/N re-program.
357*4882a593Smuzhiyun */
358*4882a593Smuzhiyun errata_i810 = ti_clk_get_features()->flags & TI_CLK_ERRATA_I810;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun if (errata_i810) {
361*4882a593Smuzhiyun ai = omap3_dpll_autoidle_read(clk);
362*4882a593Smuzhiyun if (ai) {
363*4882a593Smuzhiyun omap3_dpll_deny_idle(clk);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* OCP barrier */
366*4882a593Smuzhiyun omap3_dpll_autoidle_read(clk);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->mult_div1_reg);
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /* Set 4X multiplier and low-power mode */
373*4882a593Smuzhiyun if (dd->m4xen_mask || dd->lpmode_mask) {
374*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun if (dd->m4xen_mask) {
377*4882a593Smuzhiyun if (dd->last_rounded_m4xen)
378*4882a593Smuzhiyun v |= dd->m4xen_mask;
379*4882a593Smuzhiyun else
380*4882a593Smuzhiyun v &= ~dd->m4xen_mask;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun if (dd->lpmode_mask) {
384*4882a593Smuzhiyun if (dd->last_rounded_lpmode)
385*4882a593Smuzhiyun v |= dd->lpmode_mask;
386*4882a593Smuzhiyun else
387*4882a593Smuzhiyun v &= ~dd->lpmode_mask;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->control_reg);
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun /* We let the clock framework set the other output dividers later */
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /* REVISIT: Set ramp-up delay? */
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun _omap3_noncore_dpll_lock(clk);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if (errata_i810 && ai)
400*4882a593Smuzhiyun omap3_dpll_allow_idle(clk);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun return 0;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /* Public functions */
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun /**
408*4882a593Smuzhiyun * omap3_dpll_recalc - recalculate DPLL rate
409*4882a593Smuzhiyun * @clk: DPLL struct clk
410*4882a593Smuzhiyun *
411*4882a593Smuzhiyun * Recalculate and propagate the DPLL rate.
412*4882a593Smuzhiyun */
omap3_dpll_recalc(struct clk_hw * hw,unsigned long parent_rate)413*4882a593Smuzhiyun unsigned long omap3_dpll_recalc(struct clk_hw *hw, unsigned long parent_rate)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun return omap2_get_dpll_rate(clk);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /**
423*4882a593Smuzhiyun * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
424*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
427*4882a593Smuzhiyun * The choice of modes depends on the DPLL's programmed rate: if it is
428*4882a593Smuzhiyun * the same as the DPLL's parent clock, it will enter bypass;
429*4882a593Smuzhiyun * otherwise, it will enter lock. This code will wait for the DPLL to
430*4882a593Smuzhiyun * indicate readiness before returning, unless the DPLL takes too long
431*4882a593Smuzhiyun * to enter the target state. Intended to be used as the struct clk's
432*4882a593Smuzhiyun * enable function. If DPLL3 was passed in, or the DPLL does not
433*4882a593Smuzhiyun * support low-power stop, or if the DPLL took too long to enter
434*4882a593Smuzhiyun * bypass or lock, return -EINVAL; otherwise, return 0.
435*4882a593Smuzhiyun */
omap3_noncore_dpll_enable(struct clk_hw * hw)436*4882a593Smuzhiyun int omap3_noncore_dpll_enable(struct clk_hw *hw)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
439*4882a593Smuzhiyun int r;
440*4882a593Smuzhiyun struct dpll_data *dd;
441*4882a593Smuzhiyun struct clk_hw *parent;
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun dd = clk->dpll_data;
444*4882a593Smuzhiyun if (!dd)
445*4882a593Smuzhiyun return -EINVAL;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun if (clk->clkdm) {
448*4882a593Smuzhiyun r = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
449*4882a593Smuzhiyun if (r) {
450*4882a593Smuzhiyun WARN(1,
451*4882a593Smuzhiyun "%s: could not enable %s's clockdomain %s: %d\n",
452*4882a593Smuzhiyun __func__, clk_hw_get_name(hw),
453*4882a593Smuzhiyun clk->clkdm_name, r);
454*4882a593Smuzhiyun return r;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun parent = clk_hw_get_parent(hw);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if (clk_hw_get_rate(hw) == clk_hw_get_rate(dd->clk_bypass)) {
461*4882a593Smuzhiyun WARN_ON(parent != dd->clk_bypass);
462*4882a593Smuzhiyun r = _omap3_noncore_dpll_bypass(clk);
463*4882a593Smuzhiyun } else {
464*4882a593Smuzhiyun WARN_ON(parent != dd->clk_ref);
465*4882a593Smuzhiyun r = _omap3_noncore_dpll_lock(clk);
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun return r;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /**
472*4882a593Smuzhiyun * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
473*4882a593Smuzhiyun * @clk: pointer to a DPLL struct clk
474*4882a593Smuzhiyun *
475*4882a593Smuzhiyun * Instructs a non-CORE DPLL to enter low-power stop. This function is
476*4882a593Smuzhiyun * intended for use in struct clkops. No return value.
477*4882a593Smuzhiyun */
omap3_noncore_dpll_disable(struct clk_hw * hw)478*4882a593Smuzhiyun void omap3_noncore_dpll_disable(struct clk_hw *hw)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun _omap3_noncore_dpll_stop(clk);
483*4882a593Smuzhiyun if (clk->clkdm)
484*4882a593Smuzhiyun ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /* Non-CORE DPLL rate set code */
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun /**
490*4882a593Smuzhiyun * omap3_noncore_dpll_determine_rate - determine rate for a DPLL
491*4882a593Smuzhiyun * @hw: pointer to the clock to determine rate for
492*4882a593Smuzhiyun * @req: target rate request
493*4882a593Smuzhiyun *
494*4882a593Smuzhiyun * Determines which DPLL mode to use for reaching a desired target rate.
495*4882a593Smuzhiyun * Checks whether the DPLL shall be in bypass or locked mode, and if
496*4882a593Smuzhiyun * locked, calculates the M,N values for the DPLL via round-rate.
497*4882a593Smuzhiyun * Returns a 0 on success, negative error value in failure.
498*4882a593Smuzhiyun */
omap3_noncore_dpll_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)499*4882a593Smuzhiyun int omap3_noncore_dpll_determine_rate(struct clk_hw *hw,
500*4882a593Smuzhiyun struct clk_rate_request *req)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
503*4882a593Smuzhiyun struct dpll_data *dd;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun if (!req->rate)
506*4882a593Smuzhiyun return -EINVAL;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun dd = clk->dpll_data;
509*4882a593Smuzhiyun if (!dd)
510*4882a593Smuzhiyun return -EINVAL;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (clk_hw_get_rate(dd->clk_bypass) == req->rate &&
513*4882a593Smuzhiyun (dd->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
514*4882a593Smuzhiyun req->best_parent_hw = dd->clk_bypass;
515*4882a593Smuzhiyun } else {
516*4882a593Smuzhiyun req->rate = omap2_dpll_round_rate(hw, req->rate,
517*4882a593Smuzhiyun &req->best_parent_rate);
518*4882a593Smuzhiyun req->best_parent_hw = dd->clk_ref;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun req->best_parent_rate = req->rate;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun return 0;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /**
527*4882a593Smuzhiyun * omap3_noncore_dpll_set_parent - set parent for a DPLL clock
528*4882a593Smuzhiyun * @hw: pointer to the clock to set parent for
529*4882a593Smuzhiyun * @index: parent index to select
530*4882a593Smuzhiyun *
531*4882a593Smuzhiyun * Sets parent for a DPLL clock. This sets the DPLL into bypass or
532*4882a593Smuzhiyun * locked mode. Returns 0 with success, negative error value otherwise.
533*4882a593Smuzhiyun */
omap3_noncore_dpll_set_parent(struct clk_hw * hw,u8 index)534*4882a593Smuzhiyun int omap3_noncore_dpll_set_parent(struct clk_hw *hw, u8 index)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
537*4882a593Smuzhiyun int ret;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun if (!hw)
540*4882a593Smuzhiyun return -EINVAL;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun if (index)
543*4882a593Smuzhiyun ret = _omap3_noncore_dpll_bypass(clk);
544*4882a593Smuzhiyun else
545*4882a593Smuzhiyun ret = _omap3_noncore_dpll_lock(clk);
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun return ret;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /**
551*4882a593Smuzhiyun * omap3_noncore_dpll_set_rate - set rate for a DPLL clock
552*4882a593Smuzhiyun * @hw: pointer to the clock to set parent for
553*4882a593Smuzhiyun * @rate: target rate for the clock
554*4882a593Smuzhiyun * @parent_rate: rate of the parent clock
555*4882a593Smuzhiyun *
556*4882a593Smuzhiyun * Sets rate for a DPLL clock. First checks if the clock parent is
557*4882a593Smuzhiyun * reference clock (in bypass mode, the rate of the clock can't be
558*4882a593Smuzhiyun * changed) and proceeds with the rate change operation. Returns 0
559*4882a593Smuzhiyun * with success, negative error value otherwise.
560*4882a593Smuzhiyun */
omap3_noncore_dpll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)561*4882a593Smuzhiyun int omap3_noncore_dpll_set_rate(struct clk_hw *hw, unsigned long rate,
562*4882a593Smuzhiyun unsigned long parent_rate)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
565*4882a593Smuzhiyun struct dpll_data *dd;
566*4882a593Smuzhiyun u16 freqsel = 0;
567*4882a593Smuzhiyun int ret;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (!hw || !rate)
570*4882a593Smuzhiyun return -EINVAL;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun dd = clk->dpll_data;
573*4882a593Smuzhiyun if (!dd)
574*4882a593Smuzhiyun return -EINVAL;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun if (clk_hw_get_parent(hw) != dd->clk_ref)
577*4882a593Smuzhiyun return -EINVAL;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun if (dd->last_rounded_rate == 0)
580*4882a593Smuzhiyun return -EINVAL;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun /* Freqsel is available only on OMAP343X devices */
583*4882a593Smuzhiyun if (ti_clk_get_features()->flags & TI_CLK_DPLL_HAS_FREQSEL) {
584*4882a593Smuzhiyun freqsel = _omap3_dpll_compute_freqsel(clk, dd->last_rounded_n);
585*4882a593Smuzhiyun WARN_ON(!freqsel);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun pr_debug("%s: %s: set rate: locking rate to %lu.\n", __func__,
589*4882a593Smuzhiyun clk_hw_get_name(hw), rate);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun ret = omap3_noncore_dpll_program(clk, freqsel);
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun return ret;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun /**
597*4882a593Smuzhiyun * omap3_noncore_dpll_set_rate_and_parent - set rate and parent for a DPLL clock
598*4882a593Smuzhiyun * @hw: pointer to the clock to set rate and parent for
599*4882a593Smuzhiyun * @rate: target rate for the DPLL
600*4882a593Smuzhiyun * @parent_rate: clock rate of the DPLL parent
601*4882a593Smuzhiyun * @index: new parent index for the DPLL, 0 - reference, 1 - bypass
602*4882a593Smuzhiyun *
603*4882a593Smuzhiyun * Sets rate and parent for a DPLL clock. If new parent is the bypass
604*4882a593Smuzhiyun * clock, only selects the parent. Otherwise proceeds with a rate
605*4882a593Smuzhiyun * change, as this will effectively also change the parent as the
606*4882a593Smuzhiyun * DPLL is put into locked mode. Returns 0 with success, negative error
607*4882a593Smuzhiyun * value otherwise.
608*4882a593Smuzhiyun */
omap3_noncore_dpll_set_rate_and_parent(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate,u8 index)609*4882a593Smuzhiyun int omap3_noncore_dpll_set_rate_and_parent(struct clk_hw *hw,
610*4882a593Smuzhiyun unsigned long rate,
611*4882a593Smuzhiyun unsigned long parent_rate,
612*4882a593Smuzhiyun u8 index)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun int ret;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun if (!hw || !rate)
617*4882a593Smuzhiyun return -EINVAL;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun /*
620*4882a593Smuzhiyun * clk-ref at index[0], in which case we only need to set rate,
621*4882a593Smuzhiyun * the parent will be changed automatically with the lock sequence.
622*4882a593Smuzhiyun * With clk-bypass case we only need to change parent.
623*4882a593Smuzhiyun */
624*4882a593Smuzhiyun if (index)
625*4882a593Smuzhiyun ret = omap3_noncore_dpll_set_parent(hw, index);
626*4882a593Smuzhiyun else
627*4882a593Smuzhiyun ret = omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun return ret;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /* DPLL autoidle read/set code */
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /**
635*4882a593Smuzhiyun * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
636*4882a593Smuzhiyun * @clk: struct clk * of the DPLL to read
637*4882a593Smuzhiyun *
638*4882a593Smuzhiyun * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
639*4882a593Smuzhiyun * -EINVAL if passed a null pointer or if the struct clk does not
640*4882a593Smuzhiyun * appear to refer to a DPLL.
641*4882a593Smuzhiyun */
omap3_dpll_autoidle_read(struct clk_hw_omap * clk)642*4882a593Smuzhiyun static u32 omap3_dpll_autoidle_read(struct clk_hw_omap *clk)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun const struct dpll_data *dd;
645*4882a593Smuzhiyun u32 v;
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun if (!clk || !clk->dpll_data)
648*4882a593Smuzhiyun return -EINVAL;
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun dd = clk->dpll_data;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun if (!dd->autoidle_mask)
653*4882a593Smuzhiyun return -EINVAL;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->autoidle_reg);
656*4882a593Smuzhiyun v &= dd->autoidle_mask;
657*4882a593Smuzhiyun v >>= __ffs(dd->autoidle_mask);
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun return v;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun /**
663*4882a593Smuzhiyun * omap3_dpll_allow_idle - enable DPLL autoidle bits
664*4882a593Smuzhiyun * @clk: struct clk * of the DPLL to operate on
665*4882a593Smuzhiyun *
666*4882a593Smuzhiyun * Enable DPLL automatic idle control. This automatic idle mode
667*4882a593Smuzhiyun * switching takes effect only when the DPLL is locked, at least on
668*4882a593Smuzhiyun * OMAP3430. The DPLL will enter low-power stop when its downstream
669*4882a593Smuzhiyun * clocks are gated. No return value.
670*4882a593Smuzhiyun */
omap3_dpll_allow_idle(struct clk_hw_omap * clk)671*4882a593Smuzhiyun static void omap3_dpll_allow_idle(struct clk_hw_omap *clk)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun const struct dpll_data *dd;
674*4882a593Smuzhiyun u32 v;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun if (!clk || !clk->dpll_data)
677*4882a593Smuzhiyun return;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun dd = clk->dpll_data;
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun if (!dd->autoidle_mask)
682*4882a593Smuzhiyun return;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun /*
685*4882a593Smuzhiyun * REVISIT: CORE DPLL can optionally enter low-power bypass
686*4882a593Smuzhiyun * by writing 0x5 instead of 0x1. Add some mechanism to
687*4882a593Smuzhiyun * optionally enter this mode.
688*4882a593Smuzhiyun */
689*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->autoidle_reg);
690*4882a593Smuzhiyun v &= ~dd->autoidle_mask;
691*4882a593Smuzhiyun v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
692*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->autoidle_reg);
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun /**
696*4882a593Smuzhiyun * omap3_dpll_deny_idle - prevent DPLL from automatically idling
697*4882a593Smuzhiyun * @clk: struct clk * of the DPLL to operate on
698*4882a593Smuzhiyun *
699*4882a593Smuzhiyun * Disable DPLL automatic idle control. No return value.
700*4882a593Smuzhiyun */
omap3_dpll_deny_idle(struct clk_hw_omap * clk)701*4882a593Smuzhiyun static void omap3_dpll_deny_idle(struct clk_hw_omap *clk)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun const struct dpll_data *dd;
704*4882a593Smuzhiyun u32 v;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun if (!clk || !clk->dpll_data)
707*4882a593Smuzhiyun return;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun dd = clk->dpll_data;
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun if (!dd->autoidle_mask)
712*4882a593Smuzhiyun return;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->autoidle_reg);
715*4882a593Smuzhiyun v &= ~dd->autoidle_mask;
716*4882a593Smuzhiyun v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
717*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->autoidle_reg);
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /* Clock control for DPLL outputs */
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /* Find the parent DPLL for the given clkoutx2 clock */
omap3_find_clkoutx2_dpll(struct clk_hw * hw)723*4882a593Smuzhiyun static struct clk_hw_omap *omap3_find_clkoutx2_dpll(struct clk_hw *hw)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun struct clk_hw_omap *pclk = NULL;
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun /* Walk up the parents of clk, looking for a DPLL */
728*4882a593Smuzhiyun do {
729*4882a593Smuzhiyun do {
730*4882a593Smuzhiyun hw = clk_hw_get_parent(hw);
731*4882a593Smuzhiyun } while (hw && (!omap2_clk_is_hw_omap(hw)));
732*4882a593Smuzhiyun if (!hw)
733*4882a593Smuzhiyun break;
734*4882a593Smuzhiyun pclk = to_clk_hw_omap(hw);
735*4882a593Smuzhiyun } while (pclk && !pclk->dpll_data);
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /* clk does not have a DPLL as a parent? error in the clock data */
738*4882a593Smuzhiyun if (!pclk) {
739*4882a593Smuzhiyun WARN_ON(1);
740*4882a593Smuzhiyun return NULL;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun return pclk;
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun /**
747*4882a593Smuzhiyun * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
748*4882a593Smuzhiyun * @clk: DPLL output struct clk
749*4882a593Smuzhiyun *
750*4882a593Smuzhiyun * Using parent clock DPLL data, look up DPLL state. If locked, set our
751*4882a593Smuzhiyun * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
752*4882a593Smuzhiyun */
omap3_clkoutx2_recalc(struct clk_hw * hw,unsigned long parent_rate)753*4882a593Smuzhiyun unsigned long omap3_clkoutx2_recalc(struct clk_hw *hw,
754*4882a593Smuzhiyun unsigned long parent_rate)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun const struct dpll_data *dd;
757*4882a593Smuzhiyun unsigned long rate;
758*4882a593Smuzhiyun u32 v;
759*4882a593Smuzhiyun struct clk_hw_omap *pclk = NULL;
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun if (!parent_rate)
762*4882a593Smuzhiyun return 0;
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun pclk = omap3_find_clkoutx2_dpll(hw);
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun if (!pclk)
767*4882a593Smuzhiyun return 0;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun dd = pclk->dpll_data;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun WARN_ON(!dd->enable_mask);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->control_reg) & dd->enable_mask;
774*4882a593Smuzhiyun v >>= __ffs(dd->enable_mask);
775*4882a593Smuzhiyun if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
776*4882a593Smuzhiyun rate = parent_rate;
777*4882a593Smuzhiyun else
778*4882a593Smuzhiyun rate = parent_rate * 2;
779*4882a593Smuzhiyun return rate;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun /**
783*4882a593Smuzhiyun * omap3_core_dpll_save_context - Save the m and n values of the divider
784*4882a593Smuzhiyun * @hw: pointer struct clk_hw
785*4882a593Smuzhiyun *
786*4882a593Smuzhiyun * Before the dpll registers are lost save the last rounded rate m and n
787*4882a593Smuzhiyun * and the enable mask.
788*4882a593Smuzhiyun */
omap3_core_dpll_save_context(struct clk_hw * hw)789*4882a593Smuzhiyun int omap3_core_dpll_save_context(struct clk_hw *hw)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
792*4882a593Smuzhiyun struct dpll_data *dd;
793*4882a593Smuzhiyun u32 v;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun dd = clk->dpll_data;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
798*4882a593Smuzhiyun clk->context = (v & dd->enable_mask) >> __ffs(dd->enable_mask);
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun if (clk->context == DPLL_LOCKED) {
801*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
802*4882a593Smuzhiyun dd->last_rounded_m = (v & dd->mult_mask) >>
803*4882a593Smuzhiyun __ffs(dd->mult_mask);
804*4882a593Smuzhiyun dd->last_rounded_n = ((v & dd->div1_mask) >>
805*4882a593Smuzhiyun __ffs(dd->div1_mask)) + 1;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun return 0;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun /**
812*4882a593Smuzhiyun * omap3_core_dpll_restore_context - restore the m and n values of the divider
813*4882a593Smuzhiyun * @hw: pointer struct clk_hw
814*4882a593Smuzhiyun *
815*4882a593Smuzhiyun * Restore the last rounded rate m and n
816*4882a593Smuzhiyun * and the enable mask.
817*4882a593Smuzhiyun */
omap3_core_dpll_restore_context(struct clk_hw * hw)818*4882a593Smuzhiyun void omap3_core_dpll_restore_context(struct clk_hw *hw)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
821*4882a593Smuzhiyun const struct dpll_data *dd;
822*4882a593Smuzhiyun u32 v;
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun dd = clk->dpll_data;
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun if (clk->context == DPLL_LOCKED) {
827*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, 0x4);
828*4882a593Smuzhiyun _omap3_wait_dpll_status(clk, 0);
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
831*4882a593Smuzhiyun v &= ~(dd->mult_mask | dd->div1_mask);
832*4882a593Smuzhiyun v |= dd->last_rounded_m << __ffs(dd->mult_mask);
833*4882a593Smuzhiyun v |= (dd->last_rounded_n - 1) << __ffs(dd->div1_mask);
834*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &dd->mult_div1_reg);
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, DPLL_LOCKED);
837*4882a593Smuzhiyun _omap3_wait_dpll_status(clk, 1);
838*4882a593Smuzhiyun } else {
839*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, clk->context);
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun /**
844*4882a593Smuzhiyun * omap3_non_core_dpll_save_context - Save the m and n values of the divider
845*4882a593Smuzhiyun * @hw: pointer struct clk_hw
846*4882a593Smuzhiyun *
847*4882a593Smuzhiyun * Before the dpll registers are lost save the last rounded rate m and n
848*4882a593Smuzhiyun * and the enable mask.
849*4882a593Smuzhiyun */
omap3_noncore_dpll_save_context(struct clk_hw * hw)850*4882a593Smuzhiyun int omap3_noncore_dpll_save_context(struct clk_hw *hw)
851*4882a593Smuzhiyun {
852*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
853*4882a593Smuzhiyun struct dpll_data *dd;
854*4882a593Smuzhiyun u32 v;
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun dd = clk->dpll_data;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
859*4882a593Smuzhiyun clk->context = (v & dd->enable_mask) >> __ffs(dd->enable_mask);
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun if (clk->context == DPLL_LOCKED) {
862*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
863*4882a593Smuzhiyun dd->last_rounded_m = (v & dd->mult_mask) >>
864*4882a593Smuzhiyun __ffs(dd->mult_mask);
865*4882a593Smuzhiyun dd->last_rounded_n = ((v & dd->div1_mask) >>
866*4882a593Smuzhiyun __ffs(dd->div1_mask)) + 1;
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun return 0;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun /**
873*4882a593Smuzhiyun * omap3_core_dpll_restore_context - restore the m and n values of the divider
874*4882a593Smuzhiyun * @hw: pointer struct clk_hw
875*4882a593Smuzhiyun *
876*4882a593Smuzhiyun * Restore the last rounded rate m and n
877*4882a593Smuzhiyun * and the enable mask.
878*4882a593Smuzhiyun */
omap3_noncore_dpll_restore_context(struct clk_hw * hw)879*4882a593Smuzhiyun void omap3_noncore_dpll_restore_context(struct clk_hw *hw)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
882*4882a593Smuzhiyun const struct dpll_data *dd;
883*4882a593Smuzhiyun u32 ctrl, mult_div1;
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun dd = clk->dpll_data;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun ctrl = ti_clk_ll_ops->clk_readl(&dd->control_reg);
888*4882a593Smuzhiyun mult_div1 = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun if (clk->context == ((ctrl & dd->enable_mask) >>
891*4882a593Smuzhiyun __ffs(dd->enable_mask)) &&
892*4882a593Smuzhiyun dd->last_rounded_m == ((mult_div1 & dd->mult_mask) >>
893*4882a593Smuzhiyun __ffs(dd->mult_mask)) &&
894*4882a593Smuzhiyun dd->last_rounded_n == ((mult_div1 & dd->div1_mask) >>
895*4882a593Smuzhiyun __ffs(dd->div1_mask)) + 1) {
896*4882a593Smuzhiyun /* nothing to be done */
897*4882a593Smuzhiyun return;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun if (clk->context == DPLL_LOCKED)
901*4882a593Smuzhiyun omap3_noncore_dpll_program(clk, 0);
902*4882a593Smuzhiyun else
903*4882a593Smuzhiyun _omap3_dpll_write_clken(clk, clk->context);
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun /* OMAP3/4 non-CORE DPLL clkops */
907*4882a593Smuzhiyun const struct clk_hw_omap_ops clkhwops_omap3_dpll = {
908*4882a593Smuzhiyun .allow_idle = omap3_dpll_allow_idle,
909*4882a593Smuzhiyun .deny_idle = omap3_dpll_deny_idle,
910*4882a593Smuzhiyun };
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun /**
913*4882a593Smuzhiyun * omap3_dpll4_set_rate - set rate for omap3 per-dpll
914*4882a593Smuzhiyun * @hw: clock to change
915*4882a593Smuzhiyun * @rate: target rate for clock
916*4882a593Smuzhiyun * @parent_rate: rate of the parent clock
917*4882a593Smuzhiyun *
918*4882a593Smuzhiyun * Check if the current SoC supports the per-dpll reprogram operation
919*4882a593Smuzhiyun * or not, and then do the rate change if supported. Returns -EINVAL
920*4882a593Smuzhiyun * if not supported, 0 for success, and potential error codes from the
921*4882a593Smuzhiyun * clock rate change.
922*4882a593Smuzhiyun */
omap3_dpll4_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)923*4882a593Smuzhiyun int omap3_dpll4_set_rate(struct clk_hw *hw, unsigned long rate,
924*4882a593Smuzhiyun unsigned long parent_rate)
925*4882a593Smuzhiyun {
926*4882a593Smuzhiyun /*
927*4882a593Smuzhiyun * According to the 12-5 CDP code from TI, "Limitation 2.5"
928*4882a593Smuzhiyun * on 3430ES1 prevents us from changing DPLL multipliers or dividers
929*4882a593Smuzhiyun * on DPLL4.
930*4882a593Smuzhiyun */
931*4882a593Smuzhiyun if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
932*4882a593Smuzhiyun pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
933*4882a593Smuzhiyun return -EINVAL;
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun /**
940*4882a593Smuzhiyun * omap3_dpll4_set_rate_and_parent - set rate and parent for omap3 per-dpll
941*4882a593Smuzhiyun * @hw: clock to change
942*4882a593Smuzhiyun * @rate: target rate for clock
943*4882a593Smuzhiyun * @parent_rate: rate of the parent clock
944*4882a593Smuzhiyun * @index: parent index, 0 - reference clock, 1 - bypass clock
945*4882a593Smuzhiyun *
946*4882a593Smuzhiyun * Check if the current SoC support the per-dpll reprogram operation
947*4882a593Smuzhiyun * or not, and then do the rate + parent change if supported. Returns
948*4882a593Smuzhiyun * -EINVAL if not supported, 0 for success, and potential error codes
949*4882a593Smuzhiyun * from the clock rate change.
950*4882a593Smuzhiyun */
omap3_dpll4_set_rate_and_parent(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate,u8 index)951*4882a593Smuzhiyun int omap3_dpll4_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
952*4882a593Smuzhiyun unsigned long parent_rate, u8 index)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
955*4882a593Smuzhiyun pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
956*4882a593Smuzhiyun return -EINVAL;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun return omap3_noncore_dpll_set_rate_and_parent(hw, rate, parent_rate,
960*4882a593Smuzhiyun index);
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun /* Apply DM3730 errata sprz319 advisory 2.1. */
omap3_dpll5_apply_errata(struct clk_hw * hw,unsigned long parent_rate)964*4882a593Smuzhiyun static bool omap3_dpll5_apply_errata(struct clk_hw *hw,
965*4882a593Smuzhiyun unsigned long parent_rate)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun struct omap3_dpll5_settings {
968*4882a593Smuzhiyun unsigned int rate, m, n;
969*4882a593Smuzhiyun };
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun static const struct omap3_dpll5_settings precomputed[] = {
972*4882a593Smuzhiyun /*
973*4882a593Smuzhiyun * From DM3730 errata advisory 2.1, table 35 and 36.
974*4882a593Smuzhiyun * The N value is increased by 1 compared to the tables as the
975*4882a593Smuzhiyun * errata lists register values while last_rounded_field is the
976*4882a593Smuzhiyun * real divider value.
977*4882a593Smuzhiyun */
978*4882a593Smuzhiyun { 12000000, 80, 0 + 1 },
979*4882a593Smuzhiyun { 13000000, 443, 5 + 1 },
980*4882a593Smuzhiyun { 19200000, 50, 0 + 1 },
981*4882a593Smuzhiyun { 26000000, 443, 11 + 1 },
982*4882a593Smuzhiyun { 38400000, 25, 0 + 1 }
983*4882a593Smuzhiyun };
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun const struct omap3_dpll5_settings *d;
986*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
987*4882a593Smuzhiyun struct dpll_data *dd;
988*4882a593Smuzhiyun unsigned int i;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(precomputed); ++i) {
991*4882a593Smuzhiyun if (parent_rate == precomputed[i].rate)
992*4882a593Smuzhiyun break;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun if (i == ARRAY_SIZE(precomputed))
996*4882a593Smuzhiyun return false;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun d = &precomputed[i];
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun /* Update the M, N and rounded rate values and program the DPLL. */
1001*4882a593Smuzhiyun dd = clk->dpll_data;
1002*4882a593Smuzhiyun dd->last_rounded_m = d->m;
1003*4882a593Smuzhiyun dd->last_rounded_n = d->n;
1004*4882a593Smuzhiyun dd->last_rounded_rate = div_u64((u64)parent_rate * d->m, d->n);
1005*4882a593Smuzhiyun omap3_noncore_dpll_program(clk, 0);
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun return true;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun /**
1011*4882a593Smuzhiyun * omap3_dpll5_set_rate - set rate for omap3 dpll5
1012*4882a593Smuzhiyun * @hw: clock to change
1013*4882a593Smuzhiyun * @rate: target rate for clock
1014*4882a593Smuzhiyun * @parent_rate: rate of the parent clock
1015*4882a593Smuzhiyun *
1016*4882a593Smuzhiyun * Set rate for the DPLL5 clock. Apply the sprz319 advisory 2.1 on OMAP36xx if
1017*4882a593Smuzhiyun * the DPLL is used for USB host (detected through the requested rate).
1018*4882a593Smuzhiyun */
omap3_dpll5_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1019*4882a593Smuzhiyun int omap3_dpll5_set_rate(struct clk_hw *hw, unsigned long rate,
1020*4882a593Smuzhiyun unsigned long parent_rate)
1021*4882a593Smuzhiyun {
1022*4882a593Smuzhiyun if (rate == OMAP3_DPLL5_FREQ_FOR_USBHOST * 8) {
1023*4882a593Smuzhiyun if (omap3_dpll5_apply_errata(hw, parent_rate))
1024*4882a593Smuzhiyun return 0;
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
1028*4882a593Smuzhiyun }
1029