blob: cb535ee4e8fe7d322fcf4f02316b17b0e2c8e1bd [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>
29
Rajendra Nayak16975a72009-12-08 18:47:16 -070030#include <plat/cpu.h>
31#include <plat/clock.h>
Rajendra Nayaka1391d22009-12-08 18:47:16 -070032#include <asm/clkdev.h>
33
34#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;
66
67 dd = clk->dpll_data;
68
69 state <<= __ffs(dd->idlest_mask);
70
71 while (((__raw_readl(dd->idlest_reg) & dd->idlest_mask) != state) &&
72 i < MAX_DPLL_WAIT_TRIES) {
73 i++;
74 udelay(1);
75 }
76
77 if (i == MAX_DPLL_WAIT_TRIES) {
78 printk(KERN_ERR "clock: %s failed transition to '%s'\n",
79 clk->name, (state) ? "locked" : "bypassed");
80 } else {
81 pr_debug("clock: %s transition to '%s' in %d loops\n",
82 clk->name, (state) ? "locked" : "bypassed", i);
83
84 ret = 0;
85 }
86
87 return ret;
88}
89
90/* From 3430 TRM ES2 4.7.6.2 */
91static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n)
92{
93 unsigned long fint;
94 u16 f = 0;
95
96 fint = clk->dpll_data->clk_ref->rate / n;
97
98 pr_debug("clock: fint is %lu\n", fint);
99
100 if (fint >= 750000 && fint <= 1000000)
101 f = 0x3;
102 else if (fint > 1000000 && fint <= 1250000)
103 f = 0x4;
104 else if (fint > 1250000 && fint <= 1500000)
105 f = 0x5;
106 else if (fint > 1500000 && fint <= 1750000)
107 f = 0x6;
108 else if (fint > 1750000 && fint <= 2100000)
109 f = 0x7;
110 else if (fint > 7500000 && fint <= 10000000)
111 f = 0xB;
112 else if (fint > 10000000 && fint <= 12500000)
113 f = 0xC;
114 else if (fint > 12500000 && fint <= 15000000)
115 f = 0xD;
116 else if (fint > 15000000 && fint <= 17500000)
117 f = 0xE;
118 else if (fint > 17500000 && fint <= 21000000)
119 f = 0xF;
120 else
121 pr_debug("clock: unknown freqsel setting for %d\n", n);
122
123 return f;
124}
125
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700126/*
127 * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
128 * @clk: pointer to a DPLL struct clk
129 *
130 * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
131 * readiness before returning. Will save and restore the DPLL's
132 * autoidle state across the enable, per the CDP code. If the DPLL
133 * locked successfully, return 0; if the DPLL did not lock in the time
134 * allotted, or DPLL3 was passed in, return -EINVAL.
135 */
136static int _omap3_noncore_dpll_lock(struct clk *clk)
137{
138 u8 ai;
139 int r;
140
141 pr_debug("clock: locking DPLL %s\n", clk->name);
142
143 ai = omap3_dpll_autoidle_read(clk);
144
145 omap3_dpll_deny_idle(clk);
146
147 _omap3_dpll_write_clken(clk, DPLL_LOCKED);
148
149 r = _omap3_wait_dpll_status(clk, 1);
150
151 if (ai)
152 omap3_dpll_allow_idle(clk);
153
154 return r;
155}
156
157/*
158 * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
159 * @clk: pointer to a DPLL struct clk
160 *
161 * Instructs a non-CORE DPLL to enter low-power bypass mode. In
162 * bypass mode, the DPLL's rate is set equal to its parent clock's
163 * rate. Waits for the DPLL to report readiness before returning.
164 * Will save and restore the DPLL's autoidle state across the enable,
165 * per the CDP code. If the DPLL entered bypass mode successfully,
166 * return 0; if the DPLL did not enter bypass in the time allotted, or
167 * DPLL3 was passed in, or the DPLL does not support low-power bypass,
168 * return -EINVAL.
169 */
170static int _omap3_noncore_dpll_bypass(struct clk *clk)
171{
172 int r;
173 u8 ai;
174
175 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
176 return -EINVAL;
177
178 pr_debug("clock: configuring DPLL %s for low-power bypass\n",
179 clk->name);
180
181 ai = omap3_dpll_autoidle_read(clk);
182
183 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
184
185 r = _omap3_wait_dpll_status(clk, 0);
186
187 if (ai)
188 omap3_dpll_allow_idle(clk);
189 else
190 omap3_dpll_deny_idle(clk);
191
192 return r;
193}
194
195/*
196 * _omap3_noncore_dpll_stop - instruct a DPLL to stop
197 * @clk: pointer to a DPLL struct clk
198 *
199 * Instructs a non-CORE DPLL to enter low-power stop. Will save and
200 * restore the DPLL's autoidle state across the stop, per the CDP
201 * code. If DPLL3 was passed in, or the DPLL does not support
202 * low-power stop, return -EINVAL; otherwise, return 0.
203 */
204static int _omap3_noncore_dpll_stop(struct clk *clk)
205{
206 u8 ai;
207
208 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
209 return -EINVAL;
210
211 pr_debug("clock: stopping DPLL %s\n", clk->name);
212
213 ai = omap3_dpll_autoidle_read(clk);
214
215 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
216
217 if (ai)
218 omap3_dpll_allow_idle(clk);
219 else
220 omap3_dpll_deny_idle(clk);
221
222 return 0;
223}
224
Richard Woodruff358965d2010-02-22 22:09:08 -0700225/**
226 * lookup_dco_sddiv - Set j-type DPLL4 compensation variables
227 * @clk: pointer to a DPLL struct clk
228 * @dco: digital control oscillator selector
229 * @sd_div: target sigma-delta divider
230 * @m: DPLL multiplier to set
231 * @n: DPLL divider to set
232 *
233 * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
234 *
235 * XXX This code is not needed for 3430/AM35xx; can it be optimized
236 * out in non-multi-OMAP builds for those chips?
237 */
238static void lookup_dco_sddiv(struct clk *clk, u8 *dco, u8 *sd_div, u16 m,
239 u8 n)
240{
241 unsigned long fint, clkinp, sd; /* watch out for overflow */
242 int mod1, mod2;
243
244 clkinp = clk->parent->rate;
245 fint = (clkinp / n) * m;
246
247 if (fint < 1000000000)
248 *dco = 2;
249 else
250 *dco = 4;
251 /*
252 * target sigma-delta to near 250MHz
253 * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)]
254 */
255 clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */
256 mod1 = (clkinp * m) % (250 * n);
257 sd = (clkinp * m) / (250 * n);
258 mod2 = sd % 10;
259 sd /= 10;
260
261 if (mod1 || mod2)
262 sd++;
263 *sd_div = sd;
264}
265
Paul Walmsley60c3f652010-01-26 20:13:11 -0700266/*
267 * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly
268 * @clk: struct clk * of DPLL to set
269 * @m: DPLL multiplier to set
270 * @n: DPLL divider to set
271 * @freqsel: FREQSEL value to set
272 *
273 * Program the DPLL with the supplied M, N values, and wait for the DPLL to
274 * lock.. Returns -EINVAL upon error, or 0 upon success.
275 */
276static int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel)
277{
278 struct dpll_data *dd = clk->dpll_data;
279 u32 v;
280
281 /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
282 _omap3_noncore_dpll_bypass(clk);
283
Vishwanath BS5eb75f52010-02-24 12:05:57 -0700284 /*
285 * Set jitter correction. No jitter correction for OMAP4 and 3630
286 * since freqsel field is no longer present
287 */
288 if (!cpu_is_omap44xx() && !cpu_is_omap3630()) {
Paul Walmsley60c3f652010-01-26 20:13:11 -0700289 v = __raw_readl(dd->control_reg);
290 v &= ~dd->freqsel_mask;
291 v |= freqsel << __ffs(dd->freqsel_mask);
292 __raw_writel(v, dd->control_reg);
293 }
294
295 /* Set DPLL multiplier, divider */
296 v = __raw_readl(dd->mult_div1_reg);
297 v &= ~(dd->mult_mask | dd->div1_mask);
298 v |= m << __ffs(dd->mult_mask);
299 v |= (n - 1) << __ffs(dd->div1_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700300
301 /*
302 * XXX This code is not needed for 3430/AM35XX; can it be optimized
303 * out in non-multi-OMAP builds for those chips?
304 */
305 if ((dd->flags & DPLL_J_TYPE) && !(dd->flags & DPLL_NO_DCO_SEL)) {
306 u8 dco, sd_div;
307 lookup_dco_sddiv(clk, &dco, &sd_div, m, n);
308 /* XXX This probably will need revision for OMAP4 */
309 v &= ~(OMAP3630_PERIPH_DPLL_DCO_SEL_MASK
310 | OMAP3630_PERIPH_DPLL_SD_DIV_MASK);
311 v |= dco << __ffs(OMAP3630_PERIPH_DPLL_DCO_SEL_MASK);
312 v |= sd_div << __ffs(OMAP3630_PERIPH_DPLL_SD_DIV_MASK);
313 }
314
Paul Walmsley60c3f652010-01-26 20:13:11 -0700315 __raw_writel(v, dd->mult_div1_reg);
316
317 /* We let the clock framework set the other output dividers later */
318
319 /* REVISIT: Set ramp-up delay? */
320
321 _omap3_noncore_dpll_lock(clk);
322
323 return 0;
324}
325
326/* Public functions */
327
328/**
329 * omap3_dpll_recalc - recalculate DPLL rate
330 * @clk: DPLL struct clk
331 *
332 * Recalculate and propagate the DPLL rate.
333 */
334unsigned long omap3_dpll_recalc(struct clk *clk)
335{
336 return omap2_get_dpll_rate(clk);
337}
338
339/* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
340
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700341/**
342 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
343 * @clk: pointer to a DPLL struct clk
344 *
345 * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
346 * The choice of modes depends on the DPLL's programmed rate: if it is
347 * the same as the DPLL's parent clock, it will enter bypass;
348 * otherwise, it will enter lock. This code will wait for the DPLL to
349 * indicate readiness before returning, unless the DPLL takes too long
350 * to enter the target state. Intended to be used as the struct clk's
351 * enable function. If DPLL3 was passed in, or the DPLL does not
352 * support low-power stop, or if the DPLL took too long to enter
353 * bypass or lock, return -EINVAL; otherwise, return 0.
354 */
355int omap3_noncore_dpll_enable(struct clk *clk)
356{
357 int r;
358 struct dpll_data *dd;
359
360 dd = clk->dpll_data;
361 if (!dd)
362 return -EINVAL;
363
364 if (clk->rate == dd->clk_bypass->rate) {
365 WARN_ON(clk->parent != dd->clk_bypass);
366 r = _omap3_noncore_dpll_bypass(clk);
367 } else {
368 WARN_ON(clk->parent != dd->clk_ref);
369 r = _omap3_noncore_dpll_lock(clk);
370 }
371 /*
372 *FIXME: this is dubious - if clk->rate has changed, what about
373 * propagating?
374 */
375 if (!r)
376 clk->rate = omap2_get_dpll_rate(clk);
377
378 return r;
379}
380
381/**
382 * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
383 * @clk: pointer to a DPLL struct clk
384 *
385 * Instructs a non-CORE DPLL to enter low-power stop. This function is
386 * intended for use in struct clkops. No return value.
387 */
388void omap3_noncore_dpll_disable(struct clk *clk)
389{
390 _omap3_noncore_dpll_stop(clk);
391}
392
393
394/* Non-CORE DPLL rate set code */
395
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700396/**
397 * omap3_noncore_dpll_set_rate - set non-core DPLL rate
398 * @clk: struct clk * of DPLL to set
399 * @rate: rounded target rate
400 *
401 * Set the DPLL CLKOUT to the target rate. If the DPLL can enter
402 * low-power bypass, and the target rate is the bypass source clock
403 * rate, then configure the DPLL for bypass. Otherwise, round the
404 * target rate if it hasn't been done already, then program and lock
405 * the DPLL. Returns -EINVAL upon error, or 0 upon success.
406 */
407int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate)
408{
409 struct clk *new_parent = NULL;
Rajendra Nayak16975a72009-12-08 18:47:16 -0700410 u16 freqsel = 0;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700411 struct dpll_data *dd;
412 int ret;
413
414 if (!clk || !rate)
415 return -EINVAL;
416
417 dd = clk->dpll_data;
418 if (!dd)
419 return -EINVAL;
420
421 if (rate == omap2_get_dpll_rate(clk))
422 return 0;
423
424 /*
425 * Ensure both the bypass and ref clocks are enabled prior to
426 * doing anything; we need the bypass clock running to reprogram
427 * the DPLL.
428 */
429 omap2_clk_enable(dd->clk_bypass);
430 omap2_clk_enable(dd->clk_ref);
431
432 if (dd->clk_bypass->rate == rate &&
433 (clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
434 pr_debug("clock: %s: set rate: entering bypass.\n", clk->name);
435
436 ret = _omap3_noncore_dpll_bypass(clk);
437 if (!ret)
438 new_parent = dd->clk_bypass;
439 } else {
440 if (dd->last_rounded_rate != rate)
441 omap2_dpll_round_rate(clk, rate);
442
443 if (dd->last_rounded_rate == 0)
444 return -EINVAL;
445
Vishwanath BS5eb75f52010-02-24 12:05:57 -0700446 /* No freqsel on OMAP4 and OMAP3630 */
447 if (!cpu_is_omap44xx() && !cpu_is_omap3630()) {
Rajendra Nayak16975a72009-12-08 18:47:16 -0700448 freqsel = _omap3_dpll_compute_freqsel(clk,
449 dd->last_rounded_n);
450 if (!freqsel)
451 WARN_ON(1);
452 }
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700453
454 pr_debug("clock: %s: set rate: locking rate to %lu.\n",
455 clk->name, rate);
456
457 ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m,
458 dd->last_rounded_n, freqsel);
459 if (!ret)
460 new_parent = dd->clk_ref;
461 }
462 if (!ret) {
463 /*
Uwe Kleine-König732bee72010-06-11 12:16:59 +0200464 * Switch the parent clock in the hierarchy, and make sure
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700465 * that the new parent's usecount is correct. Note: we
466 * enable the new parent before disabling the old to avoid
467 * any unnecessary hardware disable->enable transitions.
468 */
469 if (clk->usecount) {
470 omap2_clk_enable(new_parent);
471 omap2_clk_disable(clk->parent);
472 }
473 clk_reparent(clk, new_parent);
474 clk->rate = rate;
475 }
476 omap2_clk_disable(dd->clk_ref);
477 omap2_clk_disable(dd->clk_bypass);
478
479 return 0;
480}
481
482/* DPLL autoidle read/set code */
483
484/**
485 * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
486 * @clk: struct clk * of the DPLL to read
487 *
488 * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
489 * -EINVAL if passed a null pointer or if the struct clk does not
490 * appear to refer to a DPLL.
491 */
492u32 omap3_dpll_autoidle_read(struct clk *clk)
493{
494 const struct dpll_data *dd;
495 u32 v;
496
497 if (!clk || !clk->dpll_data)
498 return -EINVAL;
499
500 dd = clk->dpll_data;
501
502 v = __raw_readl(dd->autoidle_reg);
503 v &= dd->autoidle_mask;
504 v >>= __ffs(dd->autoidle_mask);
505
506 return v;
507}
508
509/**
510 * omap3_dpll_allow_idle - enable DPLL autoidle bits
511 * @clk: struct clk * of the DPLL to operate on
512 *
513 * Enable DPLL automatic idle control. This automatic idle mode
514 * switching takes effect only when the DPLL is locked, at least on
515 * OMAP3430. The DPLL will enter low-power stop when its downstream
516 * clocks are gated. No return value.
517 */
518void omap3_dpll_allow_idle(struct clk *clk)
519{
520 const struct dpll_data *dd;
521 u32 v;
522
523 if (!clk || !clk->dpll_data)
524 return;
525
526 dd = clk->dpll_data;
527
528 /*
529 * REVISIT: CORE DPLL can optionally enter low-power bypass
530 * by writing 0x5 instead of 0x1. Add some mechanism to
531 * optionally enter this mode.
532 */
533 v = __raw_readl(dd->autoidle_reg);
534 v &= ~dd->autoidle_mask;
535 v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
536 __raw_writel(v, dd->autoidle_reg);
537}
538
539/**
540 * omap3_dpll_deny_idle - prevent DPLL from automatically idling
541 * @clk: struct clk * of the DPLL to operate on
542 *
543 * Disable DPLL automatic idle control. No return value.
544 */
545void omap3_dpll_deny_idle(struct clk *clk)
546{
547 const struct dpll_data *dd;
548 u32 v;
549
550 if (!clk || !clk->dpll_data)
551 return;
552
553 dd = clk->dpll_data;
554
555 v = __raw_readl(dd->autoidle_reg);
556 v &= ~dd->autoidle_mask;
557 v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
558 __raw_writel(v, dd->autoidle_reg);
559
560}
561
562/* Clock control for DPLL outputs */
563
564/**
565 * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
566 * @clk: DPLL output struct clk
567 *
568 * Using parent clock DPLL data, look up DPLL state. If locked, set our
569 * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
570 */
571unsigned long omap3_clkoutx2_recalc(struct clk *clk)
572{
573 const struct dpll_data *dd;
574 unsigned long rate;
575 u32 v;
576 struct clk *pclk;
577
578 /* Walk up the parents of clk, looking for a DPLL */
579 pclk = clk->parent;
580 while (pclk && !pclk->dpll_data)
581 pclk = pclk->parent;
582
583 /* clk does not have a DPLL as a parent? */
584 WARN_ON(!pclk);
585
586 dd = pclk->dpll_data;
587
588 WARN_ON(!dd->enable_mask);
589
590 v = __raw_readl(dd->control_reg) & dd->enable_mask;
591 v >>= __ffs(dd->enable_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700592 if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700593 rate = clk->parent->rate;
594 else
595 rate = clk->parent->rate * 2;
596 return rate;
597}