blob: 02e74c1e62cfa8551b463b7750ba6f3094751bcd [file] [log] [blame]
Rajendra Nayaka1391d22009-12-08 18:47:16 -07001/*
2 * OMAP3/4 - specific DPLL control functions
3 *
Richard Woodruff358965d2010-02-22 22:09:08 -07004 * Copyright (C) 2009-2010 Texas Instruments, Inc.
5 * Copyright (C) 2009-2010 Nokia Corporation
Rajendra Nayaka1391d22009-12-08 18:47:16 -07006 *
7 * Written by Paul Walmsley
Richard Woodruff358965d2010-02-22 22:09:08 -07008 * Testing and integration fixes by Jouni Högander
9 *
10 * 36xx support added by Vishwanath BS, Richard Woodruff, and Nishanth
11 * Menon
Rajendra Nayaka1391d22009-12-08 18:47:16 -070012 *
13 * Parts of this code are based on code written by
14 * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
20
Rajendra Nayaka1391d22009-12-08 18:47:16 -070021#include <linux/kernel.h>
22#include <linux/device.h>
23#include <linux/list.h>
24#include <linux/errno.h>
25#include <linux/delay.h>
26#include <linux/clk.h>
27#include <linux/io.h>
Rajendra Nayaka1391d22009-12-08 18:47:16 -070028#include <linux/bitops.h>
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +010029#include <linux/clkdev.h>
Rajendra Nayaka1391d22009-12-08 18:47:16 -070030
Rajendra Nayak16975a72009-12-08 18:47:16 -070031#include <plat/clock.h>
Rajendra Nayaka1391d22009-12-08 18:47:16 -070032
Tony Lindgrendbc04162012-08-31 10:59:07 -070033#include "soc.h"
Rajendra Nayaka1391d22009-12-08 18:47:16 -070034#include "clock.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -070035#include "cm2xxx_3xxx.h"
Rajendra Nayaka1391d22009-12-08 18:47:16 -070036#include "cm-regbits-34xx.h"
37
38/* CM_AUTOIDLE_PLL*.AUTO_* bit values */
39#define DPLL_AUTOIDLE_DISABLE 0x0
40#define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
41
42#define MAX_DPLL_WAIT_TRIES 1000000
43
Paul Walmsley60c3f652010-01-26 20:13:11 -070044/* Private functions */
Rajendra Nayaka1391d22009-12-08 18:47:16 -070045
46/* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
47static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
48{
49 const struct dpll_data *dd;
50 u32 v;
51
52 dd = clk->dpll_data;
53
54 v = __raw_readl(dd->control_reg);
55 v &= ~dd->enable_mask;
56 v |= clken_bits << __ffs(dd->enable_mask);
57 __raw_writel(v, dd->control_reg);
58}
59
60/* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
61static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
62{
63 const struct dpll_data *dd;
64 int i = 0;
65 int ret = -EINVAL;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060066 const char *clk_name;
Rajendra Nayaka1391d22009-12-08 18:47:16 -070067
68 dd = clk->dpll_data;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060069 clk_name = __clk_get_name(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -070070
71 state <<= __ffs(dd->idlest_mask);
72
73 while (((__raw_readl(dd->idlest_reg) & dd->idlest_mask) != state) &&
74 i < MAX_DPLL_WAIT_TRIES) {
75 i++;
76 udelay(1);
77 }
78
79 if (i == MAX_DPLL_WAIT_TRIES) {
80 printk(KERN_ERR "clock: %s failed transition to '%s'\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060081 clk_name, (state) ? "locked" : "bypassed");
Rajendra Nayaka1391d22009-12-08 18:47:16 -070082 } else {
83 pr_debug("clock: %s transition to '%s' in %d loops\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060084 clk_name, (state) ? "locked" : "bypassed", i);
Rajendra Nayaka1391d22009-12-08 18:47:16 -070085
86 ret = 0;
87 }
88
89 return ret;
90}
91
92/* From 3430 TRM ES2 4.7.6.2 */
93static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n)
94{
95 unsigned long fint;
96 u16 f = 0;
97
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060098 fint = __clk_get_rate(clk->dpll_data->clk_ref) / n;
Rajendra Nayaka1391d22009-12-08 18:47:16 -070099
100 pr_debug("clock: fint is %lu\n", fint);
101
102 if (fint >= 750000 && fint <= 1000000)
103 f = 0x3;
104 else if (fint > 1000000 && fint <= 1250000)
105 f = 0x4;
106 else if (fint > 1250000 && fint <= 1500000)
107 f = 0x5;
108 else if (fint > 1500000 && fint <= 1750000)
109 f = 0x6;
110 else if (fint > 1750000 && fint <= 2100000)
111 f = 0x7;
112 else if (fint > 7500000 && fint <= 10000000)
113 f = 0xB;
114 else if (fint > 10000000 && fint <= 12500000)
115 f = 0xC;
116 else if (fint > 12500000 && fint <= 15000000)
117 f = 0xD;
118 else if (fint > 15000000 && fint <= 17500000)
119 f = 0xE;
120 else if (fint > 17500000 && fint <= 21000000)
121 f = 0xF;
122 else
123 pr_debug("clock: unknown freqsel setting for %d\n", n);
124
125 return f;
126}
127
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700128/*
129 * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
130 * @clk: pointer to a DPLL struct clk
131 *
132 * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
133 * readiness before returning. Will save and restore the DPLL's
134 * autoidle state across the enable, per the CDP code. If the DPLL
135 * locked successfully, return 0; if the DPLL did not lock in the time
136 * allotted, or DPLL3 was passed in, return -EINVAL.
137 */
138static int _omap3_noncore_dpll_lock(struct clk *clk)
139{
Vikram Pandita55ffe162012-07-04 05:00:44 -0600140 const struct dpll_data *dd;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700141 u8 ai;
Vikram Pandita55ffe162012-07-04 05:00:44 -0600142 u8 state = 1;
143 int r = 0;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700144
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600145 pr_debug("clock: locking DPLL %s\n", __clk_get_name(clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700146
Vikram Pandita55ffe162012-07-04 05:00:44 -0600147 dd = clk->dpll_data;
148 state <<= __ffs(dd->idlest_mask);
149
150 /* Check if already locked */
151 if ((__raw_readl(dd->idlest_reg) & dd->idlest_mask) == state)
152 goto done;
153
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700154 ai = omap3_dpll_autoidle_read(clk);
155
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600156 if (ai)
157 omap3_dpll_deny_idle(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700158
159 _omap3_dpll_write_clken(clk, DPLL_LOCKED);
160
161 r = _omap3_wait_dpll_status(clk, 1);
162
163 if (ai)
164 omap3_dpll_allow_idle(clk);
165
Vikram Pandita55ffe162012-07-04 05:00:44 -0600166done:
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700167 return r;
168}
169
170/*
171 * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
172 * @clk: pointer to a DPLL struct clk
173 *
174 * Instructs a non-CORE DPLL to enter low-power bypass mode. In
175 * bypass mode, the DPLL's rate is set equal to its parent clock's
176 * rate. Waits for the DPLL to report readiness before returning.
177 * Will save and restore the DPLL's autoidle state across the enable,
178 * per the CDP code. If the DPLL entered bypass mode successfully,
179 * return 0; if the DPLL did not enter bypass in the time allotted, or
180 * DPLL3 was passed in, or the DPLL does not support low-power bypass,
181 * return -EINVAL.
182 */
183static int _omap3_noncore_dpll_bypass(struct clk *clk)
184{
185 int r;
186 u8 ai;
187
188 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
189 return -EINVAL;
190
191 pr_debug("clock: configuring DPLL %s for low-power bypass\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600192 __clk_get_name(clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700193
194 ai = omap3_dpll_autoidle_read(clk);
195
196 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
197
198 r = _omap3_wait_dpll_status(clk, 0);
199
200 if (ai)
201 omap3_dpll_allow_idle(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700202
203 return r;
204}
205
206/*
207 * _omap3_noncore_dpll_stop - instruct a DPLL to stop
208 * @clk: pointer to a DPLL struct clk
209 *
210 * Instructs a non-CORE DPLL to enter low-power stop. Will save and
211 * restore the DPLL's autoidle state across the stop, per the CDP
212 * code. If DPLL3 was passed in, or the DPLL does not support
213 * low-power stop, return -EINVAL; otherwise, return 0.
214 */
215static int _omap3_noncore_dpll_stop(struct clk *clk)
216{
217 u8 ai;
218
219 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
220 return -EINVAL;
221
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600222 pr_debug("clock: stopping DPLL %s\n", __clk_get_name(clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700223
224 ai = omap3_dpll_autoidle_read(clk);
225
226 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
227
228 if (ai)
229 omap3_dpll_allow_idle(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700230
231 return 0;
232}
233
Richard Woodruff358965d2010-02-22 22:09:08 -0700234/**
Jon Huntera36795c2010-12-21 21:31:43 -0700235 * _lookup_dco - Lookup DCO used by j-type DPLL
Richard Woodruff358965d2010-02-22 22:09:08 -0700236 * @clk: pointer to a DPLL struct clk
237 * @dco: digital control oscillator selector
Jon Huntera36795c2010-12-21 21:31:43 -0700238 * @m: DPLL multiplier to set
239 * @n: DPLL divider to set
240 *
241 * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
242 *
243 * XXX This code is not needed for 3430/AM35xx; can it be optimized
244 * out in non-multi-OMAP builds for those chips?
245 */
246static void _lookup_dco(struct clk *clk, u8 *dco, u16 m, u8 n)
247{
248 unsigned long fint, clkinp; /* watch out for overflow */
249
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600250 clkinp = __clk_get_rate(__clk_get_parent(clk));
Jon Huntera36795c2010-12-21 21:31:43 -0700251 fint = (clkinp / n) * m;
252
253 if (fint < 1000000000)
254 *dco = 2;
255 else
256 *dco = 4;
257}
258
259/**
260 * _lookup_sddiv - Calculate sigma delta divider for j-type DPLL
261 * @clk: pointer to a DPLL struct clk
Richard Woodruff358965d2010-02-22 22:09:08 -0700262 * @sd_div: target sigma-delta divider
263 * @m: DPLL multiplier to set
264 * @n: DPLL divider to set
265 *
266 * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
267 *
268 * XXX This code is not needed for 3430/AM35xx; can it be optimized
269 * out in non-multi-OMAP builds for those chips?
270 */
Jon Huntera36795c2010-12-21 21:31:43 -0700271static void _lookup_sddiv(struct clk *clk, u8 *sd_div, u16 m, u8 n)
Richard Woodruff358965d2010-02-22 22:09:08 -0700272{
Jon Huntera36795c2010-12-21 21:31:43 -0700273 unsigned long clkinp, sd; /* watch out for overflow */
Richard Woodruff358965d2010-02-22 22:09:08 -0700274 int mod1, mod2;
275
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600276 clkinp = __clk_get_rate(__clk_get_parent(clk));
Richard Woodruff358965d2010-02-22 22:09:08 -0700277
Richard Woodruff358965d2010-02-22 22:09:08 -0700278 /*
279 * target sigma-delta to near 250MHz
280 * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)]
281 */
282 clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */
283 mod1 = (clkinp * m) % (250 * n);
284 sd = (clkinp * m) / (250 * n);
285 mod2 = sd % 10;
286 sd /= 10;
287
288 if (mod1 || mod2)
289 sd++;
290 *sd_div = sd;
291}
292
Paul Walmsley60c3f652010-01-26 20:13:11 -0700293/*
294 * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly
295 * @clk: struct clk * of DPLL to set
296 * @m: DPLL multiplier to set
297 * @n: DPLL divider to set
298 * @freqsel: FREQSEL value to set
299 *
300 * Program the DPLL with the supplied M, N values, and wait for the DPLL to
301 * lock.. Returns -EINVAL upon error, or 0 upon success.
302 */
303static int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel)
304{
305 struct dpll_data *dd = clk->dpll_data;
Jon Huntera36795c2010-12-21 21:31:43 -0700306 u8 dco, sd_div;
Paul Walmsley60c3f652010-01-26 20:13:11 -0700307 u32 v;
308
309 /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
310 _omap3_noncore_dpll_bypass(clk);
311
Vishwanath BS5eb75f52010-02-24 12:05:57 -0700312 /*
313 * Set jitter correction. No jitter correction for OMAP4 and 3630
314 * since freqsel field is no longer present
315 */
Vaibhav Hiremath78da2642012-08-24 20:24:24 +0530316 if (!soc_is_am33xx() && !cpu_is_omap44xx() && !cpu_is_omap3630()) {
Paul Walmsley60c3f652010-01-26 20:13:11 -0700317 v = __raw_readl(dd->control_reg);
318 v &= ~dd->freqsel_mask;
319 v |= freqsel << __ffs(dd->freqsel_mask);
320 __raw_writel(v, dd->control_reg);
321 }
322
323 /* Set DPLL multiplier, divider */
324 v = __raw_readl(dd->mult_div1_reg);
325 v &= ~(dd->mult_mask | dd->div1_mask);
326 v |= m << __ffs(dd->mult_mask);
327 v |= (n - 1) << __ffs(dd->div1_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700328
Jon Huntera36795c2010-12-21 21:31:43 -0700329 /* Configure dco and sd_div for dplls that have these fields */
330 if (dd->dco_mask) {
331 _lookup_dco(clk, &dco, m, n);
332 v &= ~(dd->dco_mask);
333 v |= dco << __ffs(dd->dco_mask);
334 }
335 if (dd->sddiv_mask) {
336 _lookup_sddiv(clk, &sd_div, m, n);
337 v &= ~(dd->sddiv_mask);
338 v |= sd_div << __ffs(dd->sddiv_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700339 }
340
Paul Walmsley60c3f652010-01-26 20:13:11 -0700341 __raw_writel(v, dd->mult_div1_reg);
342
343 /* We let the clock framework set the other output dividers later */
344
345 /* REVISIT: Set ramp-up delay? */
346
347 _omap3_noncore_dpll_lock(clk);
348
349 return 0;
350}
351
352/* Public functions */
353
354/**
355 * omap3_dpll_recalc - recalculate DPLL rate
356 * @clk: DPLL struct clk
357 *
358 * Recalculate and propagate the DPLL rate.
359 */
360unsigned long omap3_dpll_recalc(struct clk *clk)
361{
362 return omap2_get_dpll_rate(clk);
363}
364
365/* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
366
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700367/**
368 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
369 * @clk: pointer to a DPLL struct clk
370 *
371 * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
372 * The choice of modes depends on the DPLL's programmed rate: if it is
373 * the same as the DPLL's parent clock, it will enter bypass;
374 * otherwise, it will enter lock. This code will wait for the DPLL to
375 * indicate readiness before returning, unless the DPLL takes too long
376 * to enter the target state. Intended to be used as the struct clk's
377 * enable function. If DPLL3 was passed in, or the DPLL does not
378 * support low-power stop, or if the DPLL took too long to enter
379 * bypass or lock, return -EINVAL; otherwise, return 0.
380 */
381int omap3_noncore_dpll_enable(struct clk *clk)
382{
383 int r;
384 struct dpll_data *dd;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600385 struct clk *parent;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700386
387 dd = clk->dpll_data;
388 if (!dd)
389 return -EINVAL;
390
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600391 parent = __clk_get_parent(clk);
392
393 if (__clk_get_rate(clk) == __clk_get_rate(dd->clk_bypass)) {
394 WARN_ON(parent != dd->clk_bypass);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700395 r = _omap3_noncore_dpll_bypass(clk);
396 } else {
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600397 WARN_ON(parent != dd->clk_ref);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700398 r = _omap3_noncore_dpll_lock(clk);
399 }
400 /*
401 *FIXME: this is dubious - if clk->rate has changed, what about
402 * propagating?
403 */
404 if (!r)
Jon Hunter49642ac2011-10-07 00:53:01 -0600405 clk->rate = (clk->recalc) ? clk->recalc(clk) :
406 omap2_get_dpll_rate(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700407
408 return r;
409}
410
411/**
412 * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
413 * @clk: pointer to a DPLL struct clk
414 *
415 * Instructs a non-CORE DPLL to enter low-power stop. This function is
416 * intended for use in struct clkops. No return value.
417 */
418void omap3_noncore_dpll_disable(struct clk *clk)
419{
420 _omap3_noncore_dpll_stop(clk);
421}
422
423
424/* Non-CORE DPLL rate set code */
425
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700426/**
427 * omap3_noncore_dpll_set_rate - set non-core DPLL rate
428 * @clk: struct clk * of DPLL to set
429 * @rate: rounded target rate
430 *
431 * Set the DPLL CLKOUT to the target rate. If the DPLL can enter
432 * low-power bypass, and the target rate is the bypass source clock
433 * rate, then configure the DPLL for bypass. Otherwise, round the
434 * target rate if it hasn't been done already, then program and lock
435 * the DPLL. Returns -EINVAL upon error, or 0 upon success.
436 */
437int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate)
438{
439 struct clk *new_parent = NULL;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600440 unsigned long hw_rate, bypass_rate;
Rajendra Nayak16975a72009-12-08 18:47:16 -0700441 u16 freqsel = 0;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700442 struct dpll_data *dd;
443 int ret;
444
445 if (!clk || !rate)
446 return -EINVAL;
447
448 dd = clk->dpll_data;
449 if (!dd)
450 return -EINVAL;
451
Jon Hunter49642ac2011-10-07 00:53:01 -0600452 hw_rate = (clk->recalc) ? clk->recalc(clk) : omap2_get_dpll_rate(clk);
453 if (rate == hw_rate)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700454 return 0;
455
456 /*
457 * Ensure both the bypass and ref clocks are enabled prior to
458 * doing anything; we need the bypass clock running to reprogram
459 * the DPLL.
460 */
461 omap2_clk_enable(dd->clk_bypass);
462 omap2_clk_enable(dd->clk_ref);
463
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600464 bypass_rate = __clk_get_rate(dd->clk_bypass);
465 if (bypass_rate == rate &&
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700466 (clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
467 pr_debug("clock: %s: set rate: entering bypass.\n", clk->name);
468
469 ret = _omap3_noncore_dpll_bypass(clk);
470 if (!ret)
471 new_parent = dd->clk_bypass;
472 } else {
473 if (dd->last_rounded_rate != rate)
Mike Turquette273a1ce2011-10-07 00:53:00 -0600474 rate = clk->round_rate(clk, rate);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700475
476 if (dd->last_rounded_rate == 0)
477 return -EINVAL;
478
Vishwanath BS5eb75f52010-02-24 12:05:57 -0700479 /* No freqsel on OMAP4 and OMAP3630 */
Vaibhav Hiremath78da2642012-08-24 20:24:24 +0530480 if (!soc_is_am33xx() && !cpu_is_omap44xx() && !cpu_is_omap3630()) {
Rajendra Nayak16975a72009-12-08 18:47:16 -0700481 freqsel = _omap3_dpll_compute_freqsel(clk,
482 dd->last_rounded_n);
483 if (!freqsel)
484 WARN_ON(1);
485 }
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700486
487 pr_debug("clock: %s: set rate: locking rate to %lu.\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600488 __clk_get_name(clk), rate);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700489
490 ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m,
491 dd->last_rounded_n, freqsel);
492 if (!ret)
493 new_parent = dd->clk_ref;
494 }
495 if (!ret) {
496 /*
Uwe Kleine-König732bee72010-06-11 12:16:59 +0200497 * Switch the parent clock in the hierarchy, and make sure
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700498 * that the new parent's usecount is correct. Note: we
499 * enable the new parent before disabling the old to avoid
500 * any unnecessary hardware disable->enable transitions.
501 */
502 if (clk->usecount) {
503 omap2_clk_enable(new_parent);
504 omap2_clk_disable(clk->parent);
505 }
506 clk_reparent(clk, new_parent);
507 clk->rate = rate;
508 }
509 omap2_clk_disable(dd->clk_ref);
510 omap2_clk_disable(dd->clk_bypass);
511
512 return 0;
513}
514
515/* DPLL autoidle read/set code */
516
517/**
518 * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
519 * @clk: struct clk * of the DPLL to read
520 *
521 * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
522 * -EINVAL if passed a null pointer or if the struct clk does not
523 * appear to refer to a DPLL.
524 */
525u32 omap3_dpll_autoidle_read(struct clk *clk)
526{
527 const struct dpll_data *dd;
528 u32 v;
529
530 if (!clk || !clk->dpll_data)
531 return -EINVAL;
532
533 dd = clk->dpll_data;
534
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600535 if (!dd->autoidle_reg)
536 return -EINVAL;
537
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700538 v = __raw_readl(dd->autoidle_reg);
539 v &= dd->autoidle_mask;
540 v >>= __ffs(dd->autoidle_mask);
541
542 return v;
543}
544
545/**
546 * omap3_dpll_allow_idle - enable DPLL autoidle bits
547 * @clk: struct clk * of the DPLL to operate on
548 *
549 * Enable DPLL automatic idle control. This automatic idle mode
550 * switching takes effect only when the DPLL is locked, at least on
551 * OMAP3430. The DPLL will enter low-power stop when its downstream
552 * clocks are gated. No return value.
553 */
554void omap3_dpll_allow_idle(struct clk *clk)
555{
556 const struct dpll_data *dd;
557 u32 v;
558
559 if (!clk || !clk->dpll_data)
560 return;
561
562 dd = clk->dpll_data;
563
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600564 if (!dd->autoidle_reg) {
565 pr_debug("clock: DPLL %s: autoidle not supported\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600566 __clk_get_name(clk));
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600567 return;
568 }
569
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700570 /*
571 * REVISIT: CORE DPLL can optionally enter low-power bypass
572 * by writing 0x5 instead of 0x1. Add some mechanism to
573 * optionally enter this mode.
574 */
575 v = __raw_readl(dd->autoidle_reg);
576 v &= ~dd->autoidle_mask;
577 v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
578 __raw_writel(v, dd->autoidle_reg);
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600579
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700580}
581
582/**
583 * omap3_dpll_deny_idle - prevent DPLL from automatically idling
584 * @clk: struct clk * of the DPLL to operate on
585 *
586 * Disable DPLL automatic idle control. No return value.
587 */
588void omap3_dpll_deny_idle(struct clk *clk)
589{
590 const struct dpll_data *dd;
591 u32 v;
592
593 if (!clk || !clk->dpll_data)
594 return;
595
596 dd = clk->dpll_data;
597
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600598 if (!dd->autoidle_reg) {
599 pr_debug("clock: DPLL %s: autoidle not supported\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600600 __clk_get_name(clk));
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600601 return;
602 }
603
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700604 v = __raw_readl(dd->autoidle_reg);
605 v &= ~dd->autoidle_mask;
606 v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
607 __raw_writel(v, dd->autoidle_reg);
608
609}
610
611/* Clock control for DPLL outputs */
612
613/**
614 * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
615 * @clk: DPLL output struct clk
616 *
617 * Using parent clock DPLL data, look up DPLL state. If locked, set our
618 * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
619 */
620unsigned long omap3_clkoutx2_recalc(struct clk *clk)
621{
622 const struct dpll_data *dd;
623 unsigned long rate;
624 u32 v;
625 struct clk *pclk;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600626 unsigned long parent_rate;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700627
628 /* Walk up the parents of clk, looking for a DPLL */
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600629 pclk = __clk_get_parent(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700630 while (pclk && !pclk->dpll_data)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600631 pclk = __clk_get_parent(pclk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700632
633 /* clk does not have a DPLL as a parent? */
634 WARN_ON(!pclk);
635
636 dd = pclk->dpll_data;
637
638 WARN_ON(!dd->enable_mask);
639
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600640 parent_rate = __clk_get_rate(__clk_get_parent(clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700641 v = __raw_readl(dd->control_reg) & dd->enable_mask;
642 v >>= __ffs(dd->enable_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700643 if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600644 rate = parent_rate;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700645 else
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600646 rate = parent_rate * 2;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700647 return rate;
648}
Vaibhav Hiremath353cec42012-07-05 08:05:15 -0700649
650/* OMAP3/4 non-CORE DPLL clkops */
651
652const struct clkops clkops_omap3_noncore_dpll_ops = {
653 .enable = omap3_noncore_dpll_enable,
654 .disable = omap3_noncore_dpll_disable,
655 .allow_idle = omap3_dpll_allow_idle,
656 .deny_idle = omap3_dpll_deny_idle,
657};
658
659const struct clkops clkops_omap3_core_dpll_ops = {
660 .allow_idle = omap3_dpll_allow_idle,
661 .deny_idle = omap3_dpll_deny_idle,
662};