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