ping: implement proper locking
[linux/fpc-iii.git] / arch / arm / mach-davinci / clock.c
blobdf42c93a93d69e88db6e8e621271a59e36025c5f
1 /*
2 * Clock and PLL control for DaVinci devices
4 * Copyright (C) 2006-2007 Texas Instruments.
5 * Copyright (C) 2008-2009 Deep Root Systems, LLC
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/io.h>
21 #include <linux/delay.h>
23 #include <mach/hardware.h>
25 #include <mach/clock.h>
26 #include "psc.h"
27 #include <mach/cputype.h>
28 #include "clock.h"
30 static LIST_HEAD(clocks);
31 static DEFINE_MUTEX(clocks_mutex);
32 static DEFINE_SPINLOCK(clockfw_lock);
34 static void __clk_enable(struct clk *clk)
36 if (clk->parent)
37 __clk_enable(clk->parent);
38 if (clk->usecount++ == 0) {
39 if (clk->flags & CLK_PSC)
40 davinci_psc_config(clk->domain, clk->gpsc, clk->lpsc,
41 true, clk->flags);
42 else if (clk->clk_enable)
43 clk->clk_enable(clk);
47 static void __clk_disable(struct clk *clk)
49 if (WARN_ON(clk->usecount == 0))
50 return;
51 if (--clk->usecount == 0) {
52 if (!(clk->flags & CLK_PLL) && (clk->flags & CLK_PSC))
53 davinci_psc_config(clk->domain, clk->gpsc, clk->lpsc,
54 false, clk->flags);
55 else if (clk->clk_disable)
56 clk->clk_disable(clk);
58 if (clk->parent)
59 __clk_disable(clk->parent);
62 int davinci_clk_reset(struct clk *clk, bool reset)
64 unsigned long flags;
66 if (clk == NULL || IS_ERR(clk))
67 return -EINVAL;
69 spin_lock_irqsave(&clockfw_lock, flags);
70 if (clk->flags & CLK_PSC)
71 davinci_psc_reset(clk->gpsc, clk->lpsc, reset);
72 spin_unlock_irqrestore(&clockfw_lock, flags);
74 return 0;
76 EXPORT_SYMBOL(davinci_clk_reset);
78 int davinci_clk_reset_assert(struct clk *clk)
80 if (clk == NULL || IS_ERR(clk) || !clk->reset)
81 return -EINVAL;
83 return clk->reset(clk, true);
85 EXPORT_SYMBOL(davinci_clk_reset_assert);
87 int davinci_clk_reset_deassert(struct clk *clk)
89 if (clk == NULL || IS_ERR(clk) || !clk->reset)
90 return -EINVAL;
92 return clk->reset(clk, false);
94 EXPORT_SYMBOL(davinci_clk_reset_deassert);
96 int clk_enable(struct clk *clk)
98 unsigned long flags;
100 if (!clk)
101 return 0;
102 else if (IS_ERR(clk))
103 return -EINVAL;
105 spin_lock_irqsave(&clockfw_lock, flags);
106 __clk_enable(clk);
107 spin_unlock_irqrestore(&clockfw_lock, flags);
109 return 0;
111 EXPORT_SYMBOL(clk_enable);
113 void clk_disable(struct clk *clk)
115 unsigned long flags;
117 if (clk == NULL || IS_ERR(clk))
118 return;
120 spin_lock_irqsave(&clockfw_lock, flags);
121 __clk_disable(clk);
122 spin_unlock_irqrestore(&clockfw_lock, flags);
124 EXPORT_SYMBOL(clk_disable);
126 unsigned long clk_get_rate(struct clk *clk)
128 if (clk == NULL || IS_ERR(clk))
129 return 0;
131 return clk->rate;
133 EXPORT_SYMBOL(clk_get_rate);
135 long clk_round_rate(struct clk *clk, unsigned long rate)
137 if (clk == NULL || IS_ERR(clk))
138 return 0;
140 if (clk->round_rate)
141 return clk->round_rate(clk, rate);
143 return clk->rate;
145 EXPORT_SYMBOL(clk_round_rate);
147 /* Propagate rate to children */
148 static void propagate_rate(struct clk *root)
150 struct clk *clk;
152 list_for_each_entry(clk, &root->children, childnode) {
153 if (clk->recalc)
154 clk->rate = clk->recalc(clk);
155 propagate_rate(clk);
159 int clk_set_rate(struct clk *clk, unsigned long rate)
161 unsigned long flags;
162 int ret = -EINVAL;
164 if (!clk)
165 return 0;
166 else if (IS_ERR(clk))
167 return -EINVAL;
169 if (clk->set_rate)
170 ret = clk->set_rate(clk, rate);
172 spin_lock_irqsave(&clockfw_lock, flags);
173 if (ret == 0) {
174 if (clk->recalc)
175 clk->rate = clk->recalc(clk);
176 propagate_rate(clk);
178 spin_unlock_irqrestore(&clockfw_lock, flags);
180 return ret;
182 EXPORT_SYMBOL(clk_set_rate);
184 int clk_set_parent(struct clk *clk, struct clk *parent)
186 unsigned long flags;
188 if (!clk)
189 return 0;
190 else if (IS_ERR(clk))
191 return -EINVAL;
193 /* Cannot change parent on enabled clock */
194 if (WARN_ON(clk->usecount))
195 return -EINVAL;
197 mutex_lock(&clocks_mutex);
198 if (clk->set_parent) {
199 int ret = clk->set_parent(clk, parent);
201 if (ret) {
202 mutex_unlock(&clocks_mutex);
203 return ret;
206 clk->parent = parent;
207 list_del_init(&clk->childnode);
208 list_add(&clk->childnode, &clk->parent->children);
209 mutex_unlock(&clocks_mutex);
211 spin_lock_irqsave(&clockfw_lock, flags);
212 if (clk->recalc)
213 clk->rate = clk->recalc(clk);
214 propagate_rate(clk);
215 spin_unlock_irqrestore(&clockfw_lock, flags);
217 return 0;
219 EXPORT_SYMBOL(clk_set_parent);
221 int clk_register(struct clk *clk)
223 if (clk == NULL || IS_ERR(clk))
224 return -EINVAL;
226 if (WARN(clk->parent && !clk->parent->rate,
227 "CLK: %s parent %s has no rate!\n",
228 clk->name, clk->parent->name))
229 return -EINVAL;
231 INIT_LIST_HEAD(&clk->children);
233 mutex_lock(&clocks_mutex);
234 list_add_tail(&clk->node, &clocks);
235 if (clk->parent) {
236 if (clk->set_parent) {
237 int ret = clk->set_parent(clk, clk->parent);
239 if (ret) {
240 mutex_unlock(&clocks_mutex);
241 return ret;
244 list_add_tail(&clk->childnode, &clk->parent->children);
246 mutex_unlock(&clocks_mutex);
248 /* If rate is already set, use it */
249 if (clk->rate)
250 return 0;
252 /* Else, see if there is a way to calculate it */
253 if (clk->recalc)
254 clk->rate = clk->recalc(clk);
256 /* Otherwise, default to parent rate */
257 else if (clk->parent)
258 clk->rate = clk->parent->rate;
260 return 0;
262 EXPORT_SYMBOL(clk_register);
264 void clk_unregister(struct clk *clk)
266 if (clk == NULL || IS_ERR(clk))
267 return;
269 mutex_lock(&clocks_mutex);
270 list_del(&clk->node);
271 list_del(&clk->childnode);
272 mutex_unlock(&clocks_mutex);
274 EXPORT_SYMBOL(clk_unregister);
276 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
278 * Disable any unused clocks left on by the bootloader
280 int __init davinci_clk_disable_unused(void)
282 struct clk *ck;
284 spin_lock_irq(&clockfw_lock);
285 list_for_each_entry(ck, &clocks, node) {
286 if (ck->usecount > 0)
287 continue;
288 if (!(ck->flags & CLK_PSC))
289 continue;
291 /* ignore if in Disabled or SwRstDisable states */
292 if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
293 continue;
295 pr_debug("Clocks: disable unused %s\n", ck->name);
297 davinci_psc_config(ck->domain, ck->gpsc, ck->lpsc,
298 false, ck->flags);
300 spin_unlock_irq(&clockfw_lock);
302 return 0;
304 #endif
306 static unsigned long clk_sysclk_recalc(struct clk *clk)
308 u32 v, plldiv;
309 struct pll_data *pll;
310 unsigned long rate = clk->rate;
312 /* If this is the PLL base clock, no more calculations needed */
313 if (clk->pll_data)
314 return rate;
316 if (WARN_ON(!clk->parent))
317 return rate;
319 rate = clk->parent->rate;
321 /* Otherwise, the parent must be a PLL */
322 if (WARN_ON(!clk->parent->pll_data))
323 return rate;
325 pll = clk->parent->pll_data;
327 /* If pre-PLL, source clock is before the multiplier and divider(s) */
328 if (clk->flags & PRE_PLL)
329 rate = pll->input_rate;
331 if (!clk->div_reg)
332 return rate;
334 v = __raw_readl(pll->base + clk->div_reg);
335 if (v & PLLDIV_EN) {
336 plldiv = (v & pll->div_ratio_mask) + 1;
337 if (plldiv)
338 rate /= plldiv;
341 return rate;
344 int davinci_set_sysclk_rate(struct clk *clk, unsigned long rate)
346 unsigned v;
347 struct pll_data *pll;
348 unsigned long input;
349 unsigned ratio = 0;
351 /* If this is the PLL base clock, wrong function to call */
352 if (clk->pll_data)
353 return -EINVAL;
355 /* There must be a parent... */
356 if (WARN_ON(!clk->parent))
357 return -EINVAL;
359 /* ... the parent must be a PLL... */
360 if (WARN_ON(!clk->parent->pll_data))
361 return -EINVAL;
363 /* ... and this clock must have a divider. */
364 if (WARN_ON(!clk->div_reg))
365 return -EINVAL;
367 pll = clk->parent->pll_data;
369 input = clk->parent->rate;
371 /* If pre-PLL, source clock is before the multiplier and divider(s) */
372 if (clk->flags & PRE_PLL)
373 input = pll->input_rate;
375 if (input > rate) {
377 * Can afford to provide an output little higher than requested
378 * only if maximum rate supported by hardware on this sysclk
379 * is known.
381 if (clk->maxrate) {
382 ratio = DIV_ROUND_CLOSEST(input, rate);
383 if (input / ratio > clk->maxrate)
384 ratio = 0;
387 if (ratio == 0)
388 ratio = DIV_ROUND_UP(input, rate);
390 ratio--;
393 if (ratio > pll->div_ratio_mask)
394 return -EINVAL;
396 do {
397 v = __raw_readl(pll->base + PLLSTAT);
398 } while (v & PLLSTAT_GOSTAT);
400 v = __raw_readl(pll->base + clk->div_reg);
401 v &= ~pll->div_ratio_mask;
402 v |= ratio | PLLDIV_EN;
403 __raw_writel(v, pll->base + clk->div_reg);
405 v = __raw_readl(pll->base + PLLCMD);
406 v |= PLLCMD_GOSET;
407 __raw_writel(v, pll->base + PLLCMD);
409 do {
410 v = __raw_readl(pll->base + PLLSTAT);
411 } while (v & PLLSTAT_GOSTAT);
413 return 0;
415 EXPORT_SYMBOL(davinci_set_sysclk_rate);
417 static unsigned long clk_leafclk_recalc(struct clk *clk)
419 if (WARN_ON(!clk->parent))
420 return clk->rate;
422 return clk->parent->rate;
425 int davinci_simple_set_rate(struct clk *clk, unsigned long rate)
427 clk->rate = rate;
428 return 0;
431 static unsigned long clk_pllclk_recalc(struct clk *clk)
433 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
434 u8 bypass;
435 struct pll_data *pll = clk->pll_data;
436 unsigned long rate = clk->rate;
438 ctrl = __raw_readl(pll->base + PLLCTL);
439 rate = pll->input_rate = clk->parent->rate;
441 if (ctrl & PLLCTL_PLLEN) {
442 bypass = 0;
443 mult = __raw_readl(pll->base + PLLM);
444 if (cpu_is_davinci_dm365())
445 mult = 2 * (mult & PLLM_PLLM_MASK);
446 else
447 mult = (mult & PLLM_PLLM_MASK) + 1;
448 } else
449 bypass = 1;
451 if (pll->flags & PLL_HAS_PREDIV) {
452 prediv = __raw_readl(pll->base + PREDIV);
453 if (prediv & PLLDIV_EN)
454 prediv = (prediv & pll->div_ratio_mask) + 1;
455 else
456 prediv = 1;
459 /* pre-divider is fixed, but (some?) chips won't report that */
460 if (cpu_is_davinci_dm355() && pll->num == 1)
461 prediv = 8;
463 if (pll->flags & PLL_HAS_POSTDIV) {
464 postdiv = __raw_readl(pll->base + POSTDIV);
465 if (postdiv & PLLDIV_EN)
466 postdiv = (postdiv & pll->div_ratio_mask) + 1;
467 else
468 postdiv = 1;
471 if (!bypass) {
472 rate /= prediv;
473 rate *= mult;
474 rate /= postdiv;
477 pr_debug("PLL%d: input = %lu MHz [ ",
478 pll->num, clk->parent->rate / 1000000);
479 if (bypass)
480 pr_debug("bypass ");
481 if (prediv > 1)
482 pr_debug("/ %d ", prediv);
483 if (mult > 1)
484 pr_debug("* %d ", mult);
485 if (postdiv > 1)
486 pr_debug("/ %d ", postdiv);
487 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
489 return rate;
493 * davinci_set_pllrate - set the output rate of a given PLL.
495 * Note: Currently tested to work with OMAP-L138 only.
497 * @pll: pll whose rate needs to be changed.
498 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
499 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
500 * @postdiv: The post divider value. Passing 0 disables the post-divider.
502 int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
503 unsigned int mult, unsigned int postdiv)
505 u32 ctrl;
506 unsigned int locktime;
507 unsigned long flags;
509 if (pll->base == NULL)
510 return -EINVAL;
513 * PLL lock time required per OMAP-L138 datasheet is
514 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
515 * as 4 and OSCIN cycle as 25 MHz.
517 if (prediv) {
518 locktime = ((2000 * prediv) / 100);
519 prediv = (prediv - 1) | PLLDIV_EN;
520 } else {
521 locktime = PLL_LOCK_TIME;
523 if (postdiv)
524 postdiv = (postdiv - 1) | PLLDIV_EN;
525 if (mult)
526 mult = mult - 1;
528 /* Protect against simultaneous calls to PLL setting seqeunce */
529 spin_lock_irqsave(&clockfw_lock, flags);
531 ctrl = __raw_readl(pll->base + PLLCTL);
533 /* Switch the PLL to bypass mode */
534 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
535 __raw_writel(ctrl, pll->base + PLLCTL);
537 udelay(PLL_BYPASS_TIME);
539 /* Reset and enable PLL */
540 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
541 __raw_writel(ctrl, pll->base + PLLCTL);
543 if (pll->flags & PLL_HAS_PREDIV)
544 __raw_writel(prediv, pll->base + PREDIV);
546 __raw_writel(mult, pll->base + PLLM);
548 if (pll->flags & PLL_HAS_POSTDIV)
549 __raw_writel(postdiv, pll->base + POSTDIV);
551 udelay(PLL_RESET_TIME);
553 /* Bring PLL out of reset */
554 ctrl |= PLLCTL_PLLRST;
555 __raw_writel(ctrl, pll->base + PLLCTL);
557 udelay(locktime);
559 /* Remove PLL from bypass mode */
560 ctrl |= PLLCTL_PLLEN;
561 __raw_writel(ctrl, pll->base + PLLCTL);
563 spin_unlock_irqrestore(&clockfw_lock, flags);
565 return 0;
567 EXPORT_SYMBOL(davinci_set_pllrate);
570 * davinci_set_refclk_rate() - Set the reference clock rate
571 * @rate: The new rate.
573 * Sets the reference clock rate to a given value. This will most likely
574 * result in the entire clock tree getting updated.
576 * This is used to support boards which use a reference clock different
577 * than that used by default in <soc>.c file. The reference clock rate
578 * should be updated early in the boot process; ideally soon after the
579 * clock tree has been initialized once with the default reference clock
580 * rate (davinci_clk_init()).
582 * Returns 0 on success, error otherwise.
584 int davinci_set_refclk_rate(unsigned long rate)
586 struct clk *refclk;
588 refclk = clk_get(NULL, "ref");
589 if (IS_ERR(refclk)) {
590 pr_err("%s: failed to get reference clock\n", __func__);
591 return PTR_ERR(refclk);
594 clk_set_rate(refclk, rate);
596 clk_put(refclk);
598 return 0;
601 int __init davinci_clk_init(struct clk_lookup *clocks)
603 struct clk_lookup *c;
604 struct clk *clk;
605 size_t num_clocks = 0;
607 for (c = clocks; c->clk; c++) {
608 clk = c->clk;
610 if (!clk->recalc) {
612 /* Check if clock is a PLL */
613 if (clk->pll_data)
614 clk->recalc = clk_pllclk_recalc;
616 /* Else, if it is a PLL-derived clock */
617 else if (clk->flags & CLK_PLL)
618 clk->recalc = clk_sysclk_recalc;
620 /* Otherwise, it is a leaf clock (PSC clock) */
621 else if (clk->parent)
622 clk->recalc = clk_leafclk_recalc;
625 if (clk->pll_data) {
626 struct pll_data *pll = clk->pll_data;
628 if (!pll->div_ratio_mask)
629 pll->div_ratio_mask = PLLDIV_RATIO_MASK;
631 if (pll->phys_base && !pll->base) {
632 pll->base = ioremap(pll->phys_base, SZ_4K);
633 WARN_ON(!pll->base);
637 if (clk->recalc)
638 clk->rate = clk->recalc(clk);
640 if (clk->lpsc)
641 clk->flags |= CLK_PSC;
643 if (clk->flags & PSC_LRST)
644 clk->reset = davinci_clk_reset;
646 clk_register(clk);
647 num_clocks++;
649 /* Turn on clocks that Linux doesn't otherwise manage */
650 if (clk->flags & ALWAYS_ENABLED)
651 clk_enable(clk);
654 clkdev_add_table(clocks, num_clocks);
656 return 0;
659 #ifdef CONFIG_DEBUG_FS
661 #include <linux/debugfs.h>
662 #include <linux/seq_file.h>
664 #define CLKNAME_MAX 10 /* longest clock name */
665 #define NEST_DELTA 2
666 #define NEST_MAX 4
668 static void
669 dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
671 char *state;
672 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
673 struct clk *clk;
674 unsigned i;
676 if (parent->flags & CLK_PLL)
677 state = "pll";
678 else if (parent->flags & CLK_PSC)
679 state = "psc";
680 else
681 state = "";
683 /* <nest spaces> name <pad to end> */
684 memset(buf, ' ', sizeof(buf) - 1);
685 buf[sizeof(buf) - 1] = 0;
686 i = strlen(parent->name);
687 memcpy(buf + nest, parent->name,
688 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
690 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
691 buf, parent->usecount, state, clk_get_rate(parent));
692 /* REVISIT show device associations too */
694 /* cost is now small, but not linear... */
695 list_for_each_entry(clk, &parent->children, childnode) {
696 dump_clock(s, nest + NEST_DELTA, clk);
700 static int davinci_ck_show(struct seq_file *m, void *v)
702 struct clk *clk;
705 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
707 mutex_lock(&clocks_mutex);
708 list_for_each_entry(clk, &clocks, node)
709 if (!clk->parent)
710 dump_clock(m, 0, clk);
711 mutex_unlock(&clocks_mutex);
713 return 0;
716 static int davinci_ck_open(struct inode *inode, struct file *file)
718 return single_open(file, davinci_ck_show, NULL);
721 static const struct file_operations davinci_ck_operations = {
722 .open = davinci_ck_open,
723 .read = seq_read,
724 .llseek = seq_lseek,
725 .release = single_release,
728 static int __init davinci_clk_debugfs_init(void)
730 debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
731 &davinci_ck_operations);
732 return 0;
735 device_initcall(davinci_clk_debugfs_init);
736 #endif /* CONFIG_DEBUG_FS */