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>
21 #include <linux/delay.h>
23 #include <mach/hardware.h>
25 #include <mach/clock.h>
27 #include <mach/cputype.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)
37 __clk_enable(clk->parent);
38 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
39 davinci_psc_config(clk->domain, clk->gpsc, clk->lpsc,
43 static void __clk_disable(struct clk *clk)
45 if (WARN_ON(clk->usecount == 0))
47 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) &&
48 (clk->flags & CLK_PSC))
49 davinci_psc_config(clk->domain, clk->gpsc, clk->lpsc,
52 __clk_disable(clk->parent);
55 int clk_enable(struct clk *clk)
59 if (clk == NULL || IS_ERR(clk))
62 spin_lock_irqsave(&clockfw_lock, flags);
64 spin_unlock_irqrestore(&clockfw_lock, flags);
68 EXPORT_SYMBOL(clk_enable);
70 void clk_disable(struct clk *clk)
74 if (clk == NULL || IS_ERR(clk))
77 spin_lock_irqsave(&clockfw_lock, flags);
79 spin_unlock_irqrestore(&clockfw_lock, flags);
81 EXPORT_SYMBOL(clk_disable);
83 unsigned long clk_get_rate(struct clk *clk)
85 if (clk == NULL || IS_ERR(clk))
90 EXPORT_SYMBOL(clk_get_rate);
92 long clk_round_rate(struct clk *clk, unsigned long rate)
94 if (clk == NULL || IS_ERR(clk))
98 return clk->round_rate(clk, rate);
102 EXPORT_SYMBOL(clk_round_rate);
104 /* Propagate rate to children */
105 static void propagate_rate(struct clk *root)
109 list_for_each_entry(clk, &root->children, childnode) {
111 clk->rate = clk->recalc(clk);
116 int clk_set_rate(struct clk *clk, unsigned long rate)
121 if (clk == NULL || IS_ERR(clk))
125 ret = clk->set_rate(clk, rate);
127 spin_lock_irqsave(&clockfw_lock, flags);
130 clk->rate = clk->recalc(clk);
133 spin_unlock_irqrestore(&clockfw_lock, flags);
137 EXPORT_SYMBOL(clk_set_rate);
139 int clk_set_parent(struct clk *clk, struct clk *parent)
143 if (clk == NULL || IS_ERR(clk))
146 /* Cannot change parent on enabled clock */
147 if (WARN_ON(clk->usecount))
150 mutex_lock(&clocks_mutex);
151 clk->parent = parent;
152 list_del_init(&clk->childnode);
153 list_add(&clk->childnode, &clk->parent->children);
154 mutex_unlock(&clocks_mutex);
156 spin_lock_irqsave(&clockfw_lock, flags);
158 clk->rate = clk->recalc(clk);
160 spin_unlock_irqrestore(&clockfw_lock, flags);
164 EXPORT_SYMBOL(clk_set_parent);
166 int clk_register(struct clk *clk)
168 if (clk == NULL || IS_ERR(clk))
171 if (WARN(clk->parent && !clk->parent->rate,
172 "CLK: %s parent %s has no rate!\n",
173 clk->name, clk->parent->name))
176 INIT_LIST_HEAD(&clk->children);
178 mutex_lock(&clocks_mutex);
179 list_add_tail(&clk->node, &clocks);
181 list_add_tail(&clk->childnode, &clk->parent->children);
182 mutex_unlock(&clocks_mutex);
184 /* If rate is already set, use it */
188 /* Else, see if there is a way to calculate it */
190 clk->rate = clk->recalc(clk);
192 /* Otherwise, default to parent rate */
193 else if (clk->parent)
194 clk->rate = clk->parent->rate;
198 EXPORT_SYMBOL(clk_register);
200 void clk_unregister(struct clk *clk)
202 if (clk == NULL || IS_ERR(clk))
205 mutex_lock(&clocks_mutex);
206 list_del(&clk->node);
207 list_del(&clk->childnode);
208 mutex_unlock(&clocks_mutex);
210 EXPORT_SYMBOL(clk_unregister);
212 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
214 * Disable any unused clocks left on by the bootloader
216 int __init davinci_clk_disable_unused(void)
220 spin_lock_irq(&clockfw_lock);
221 list_for_each_entry(ck, &clocks, node) {
222 if (ck->usecount > 0)
224 if (!(ck->flags & CLK_PSC))
227 /* ignore if in Disabled or SwRstDisable states */
228 if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
231 pr_debug("Clocks: disable unused %s\n", ck->name);
233 davinci_psc_config(ck->domain, ck->gpsc, ck->lpsc,
236 spin_unlock_irq(&clockfw_lock);
242 static unsigned long clk_sysclk_recalc(struct clk *clk)
245 struct pll_data *pll;
246 unsigned long rate = clk->rate;
248 /* If this is the PLL base clock, no more calculations needed */
252 if (WARN_ON(!clk->parent))
255 rate = clk->parent->rate;
257 /* Otherwise, the parent must be a PLL */
258 if (WARN_ON(!clk->parent->pll_data))
261 pll = clk->parent->pll_data;
263 /* If pre-PLL, source clock is before the multiplier and divider(s) */
264 if (clk->flags & PRE_PLL)
265 rate = pll->input_rate;
270 v = __raw_readl(pll->base + clk->div_reg);
272 plldiv = (v & pll->div_ratio_mask) + 1;
280 int davinci_set_sysclk_rate(struct clk *clk, unsigned long rate)
283 struct pll_data *pll;
287 /* If this is the PLL base clock, wrong function to call */
291 /* There must be a parent... */
292 if (WARN_ON(!clk->parent))
295 /* ... the parent must be a PLL... */
296 if (WARN_ON(!clk->parent->pll_data))
299 /* ... and this clock must have a divider. */
300 if (WARN_ON(!clk->div_reg))
303 pll = clk->parent->pll_data;
305 input = clk->parent->rate;
307 /* If pre-PLL, source clock is before the multiplier and divider(s) */
308 if (clk->flags & PRE_PLL)
309 input = pll->input_rate;
313 * Can afford to provide an output little higher than requested
314 * only if maximum rate supported by hardware on this sysclk
318 ratio = DIV_ROUND_CLOSEST(input, rate);
319 if (input / ratio > clk->maxrate)
324 ratio = DIV_ROUND_UP(input, rate);
329 if (ratio > pll->div_ratio_mask)
333 v = __raw_readl(pll->base + PLLSTAT);
334 } while (v & PLLSTAT_GOSTAT);
336 v = __raw_readl(pll->base + clk->div_reg);
337 v &= ~pll->div_ratio_mask;
338 v |= ratio | PLLDIV_EN;
339 __raw_writel(v, pll->base + clk->div_reg);
341 v = __raw_readl(pll->base + PLLCMD);
343 __raw_writel(v, pll->base + PLLCMD);
346 v = __raw_readl(pll->base + PLLSTAT);
347 } while (v & PLLSTAT_GOSTAT);
351 EXPORT_SYMBOL(davinci_set_sysclk_rate);
353 static unsigned long clk_leafclk_recalc(struct clk *clk)
355 if (WARN_ON(!clk->parent))
358 return clk->parent->rate;
361 int davinci_simple_set_rate(struct clk *clk, unsigned long rate)
367 static unsigned long clk_pllclk_recalc(struct clk *clk)
369 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
371 struct pll_data *pll = clk->pll_data;
372 unsigned long rate = clk->rate;
374 ctrl = __raw_readl(pll->base + PLLCTL);
375 rate = pll->input_rate = clk->parent->rate;
377 if (ctrl & PLLCTL_PLLEN) {
379 mult = __raw_readl(pll->base + PLLM);
380 if (cpu_is_davinci_dm365())
381 mult = 2 * (mult & PLLM_PLLM_MASK);
383 mult = (mult & PLLM_PLLM_MASK) + 1;
387 if (pll->flags & PLL_HAS_PREDIV) {
388 prediv = __raw_readl(pll->base + PREDIV);
389 if (prediv & PLLDIV_EN)
390 prediv = (prediv & pll->div_ratio_mask) + 1;
395 /* pre-divider is fixed, but (some?) chips won't report that */
396 if (cpu_is_davinci_dm355() && pll->num == 1)
399 if (pll->flags & PLL_HAS_POSTDIV) {
400 postdiv = __raw_readl(pll->base + POSTDIV);
401 if (postdiv & PLLDIV_EN)
402 postdiv = (postdiv & pll->div_ratio_mask) + 1;
413 pr_debug("PLL%d: input = %lu MHz [ ",
414 pll->num, clk->parent->rate / 1000000);
418 pr_debug("/ %d ", prediv);
420 pr_debug("* %d ", mult);
422 pr_debug("/ %d ", postdiv);
423 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
429 * davinci_set_pllrate - set the output rate of a given PLL.
431 * Note: Currently tested to work with OMAP-L138 only.
433 * @pll: pll whose rate needs to be changed.
434 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
435 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
436 * @postdiv: The post divider value. Passing 0 disables the post-divider.
438 int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
439 unsigned int mult, unsigned int postdiv)
442 unsigned int locktime;
445 if (pll->base == NULL)
449 * PLL lock time required per OMAP-L138 datasheet is
450 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
451 * as 4 and OSCIN cycle as 25 MHz.
454 locktime = ((2000 * prediv) / 100);
455 prediv = (prediv - 1) | PLLDIV_EN;
457 locktime = PLL_LOCK_TIME;
460 postdiv = (postdiv - 1) | PLLDIV_EN;
464 /* Protect against simultaneous calls to PLL setting seqeunce */
465 spin_lock_irqsave(&clockfw_lock, flags);
467 ctrl = __raw_readl(pll->base + PLLCTL);
469 /* Switch the PLL to bypass mode */
470 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
471 __raw_writel(ctrl, pll->base + PLLCTL);
473 udelay(PLL_BYPASS_TIME);
475 /* Reset and enable PLL */
476 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
477 __raw_writel(ctrl, pll->base + PLLCTL);
479 if (pll->flags & PLL_HAS_PREDIV)
480 __raw_writel(prediv, pll->base + PREDIV);
482 __raw_writel(mult, pll->base + PLLM);
484 if (pll->flags & PLL_HAS_POSTDIV)
485 __raw_writel(postdiv, pll->base + POSTDIV);
487 udelay(PLL_RESET_TIME);
489 /* Bring PLL out of reset */
490 ctrl |= PLLCTL_PLLRST;
491 __raw_writel(ctrl, pll->base + PLLCTL);
495 /* Remove PLL from bypass mode */
496 ctrl |= PLLCTL_PLLEN;
497 __raw_writel(ctrl, pll->base + PLLCTL);
499 spin_unlock_irqrestore(&clockfw_lock, flags);
503 EXPORT_SYMBOL(davinci_set_pllrate);
506 * davinci_set_refclk_rate() - Set the reference clock rate
507 * @rate: The new rate.
509 * Sets the reference clock rate to a given value. This will most likely
510 * result in the entire clock tree getting updated.
512 * This is used to support boards which use a reference clock different
513 * than that used by default in <soc>.c file. The reference clock rate
514 * should be updated early in the boot process; ideally soon after the
515 * clock tree has been initialized once with the default reference clock
516 * rate (davinci_common_init()).
518 * Returns 0 on success, error otherwise.
520 int davinci_set_refclk_rate(unsigned long rate)
524 refclk = clk_get(NULL, "ref");
525 if (IS_ERR(refclk)) {
526 pr_err("%s: failed to get reference clock.\n", __func__);
527 return PTR_ERR(refclk);
530 clk_set_rate(refclk, rate);
537 int __init davinci_clk_init(struct clk_lookup *clocks)
539 struct clk_lookup *c;
541 size_t num_clocks = 0;
543 for (c = clocks; c->clk; c++) {
548 /* Check if clock is a PLL */
550 clk->recalc = clk_pllclk_recalc;
552 /* Else, if it is a PLL-derived clock */
553 else if (clk->flags & CLK_PLL)
554 clk->recalc = clk_sysclk_recalc;
556 /* Otherwise, it is a leaf clock (PSC clock) */
557 else if (clk->parent)
558 clk->recalc = clk_leafclk_recalc;
562 struct pll_data *pll = clk->pll_data;
564 if (!pll->div_ratio_mask)
565 pll->div_ratio_mask = PLLDIV_RATIO_MASK;
567 if (pll->phys_base && !pll->base) {
568 pll->base = ioremap(pll->phys_base, SZ_4K);
574 clk->rate = clk->recalc(clk);
577 clk->flags |= CLK_PSC;
582 /* Turn on clocks that Linux doesn't otherwise manage */
583 if (clk->flags & ALWAYS_ENABLED)
587 clkdev_add_table(clocks, num_clocks);
592 #ifdef CONFIG_DEBUG_FS
594 #include <linux/debugfs.h>
595 #include <linux/seq_file.h>
597 #define CLKNAME_MAX 10 /* longest clock name */
602 dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
605 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
609 if (parent->flags & CLK_PLL)
611 else if (parent->flags & CLK_PSC)
616 /* <nest spaces> name <pad to end> */
617 memset(buf, ' ', sizeof(buf) - 1);
618 buf[sizeof(buf) - 1] = 0;
619 i = strlen(parent->name);
620 memcpy(buf + nest, parent->name,
621 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
623 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
624 buf, parent->usecount, state, clk_get_rate(parent));
625 /* REVISIT show device associations too */
627 /* cost is now small, but not linear... */
628 list_for_each_entry(clk, &parent->children, childnode) {
629 dump_clock(s, nest + NEST_DELTA, clk);
633 static int davinci_ck_show(struct seq_file *m, void *v)
638 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
640 mutex_lock(&clocks_mutex);
641 list_for_each_entry(clk, &clocks, node)
643 dump_clock(m, 0, clk);
644 mutex_unlock(&clocks_mutex);
649 static int davinci_ck_open(struct inode *inode, struct file *file)
651 return single_open(file, davinci_ck_show, NULL);
654 static const struct file_operations davinci_ck_operations = {
655 .open = davinci_ck_open,
658 .release = single_release,
661 static int __init davinci_clk_debugfs_init(void)
663 debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
664 &davinci_ck_operations);
668 device_initcall(davinci_clk_debugfs_init);
669 #endif /* CONFIG_DEBUG_FS */