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 void davinci_clk_enable(struct clk
*clk
)
37 davinci_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
,
42 else if (clk
->clk_enable
)
47 void davinci_clk_disable(struct clk
*clk
)
49 if (WARN_ON(clk
->usecount
== 0))
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
,
55 else if (clk
->clk_disable
)
56 clk
->clk_disable(clk
);
59 davinci_clk_disable(clk
->parent
);
62 int davinci_clk_reset(struct clk
*clk
, bool reset
)
66 if (clk
== NULL
|| IS_ERR(clk
))
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
);
76 EXPORT_SYMBOL(davinci_clk_reset
);
78 int davinci_clk_reset_assert(struct clk
*clk
)
80 if (clk
== NULL
|| IS_ERR(clk
) || !clk
->reset
)
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
)
92 return clk
->reset(clk
, false);
94 EXPORT_SYMBOL(davinci_clk_reset_deassert
);
96 int clk_enable(struct clk
*clk
)
102 else if (IS_ERR(clk
))
105 spin_lock_irqsave(&clockfw_lock
, flags
);
106 davinci_clk_enable(clk
);
107 spin_unlock_irqrestore(&clockfw_lock
, flags
);
111 EXPORT_SYMBOL(clk_enable
);
113 void clk_disable(struct clk
*clk
)
117 if (clk
== NULL
|| IS_ERR(clk
))
120 spin_lock_irqsave(&clockfw_lock
, flags
);
121 davinci_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
))
133 EXPORT_SYMBOL(clk_get_rate
);
135 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
137 if (clk
== NULL
|| IS_ERR(clk
))
141 return clk
->round_rate(clk
, rate
);
145 EXPORT_SYMBOL(clk_round_rate
);
147 /* Propagate rate to children */
148 static void propagate_rate(struct clk
*root
)
152 list_for_each_entry(clk
, &root
->children
, childnode
) {
154 clk
->rate
= clk
->recalc(clk
);
159 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
166 else if (IS_ERR(clk
))
170 ret
= clk
->set_rate(clk
, rate
);
172 spin_lock_irqsave(&clockfw_lock
, flags
);
175 clk
->rate
= clk
->recalc(clk
);
178 spin_unlock_irqrestore(&clockfw_lock
, flags
);
182 EXPORT_SYMBOL(clk_set_rate
);
184 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
190 else if (IS_ERR(clk
))
193 /* Cannot change parent on enabled clock */
194 if (WARN_ON(clk
->usecount
))
197 mutex_lock(&clocks_mutex
);
198 if (clk
->set_parent
) {
199 int ret
= clk
->set_parent(clk
, parent
);
202 mutex_unlock(&clocks_mutex
);
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
);
213 clk
->rate
= clk
->recalc(clk
);
215 spin_unlock_irqrestore(&clockfw_lock
, flags
);
219 EXPORT_SYMBOL(clk_set_parent
);
221 struct clk
*clk_get_parent(struct clk
*clk
)
228 EXPORT_SYMBOL(clk_get_parent
);
230 int clk_register(struct clk
*clk
)
232 if (clk
== NULL
|| IS_ERR(clk
))
235 if (WARN(clk
->parent
&& !clk
->parent
->rate
,
236 "CLK: %s parent %s has no rate!\n",
237 clk
->name
, clk
->parent
->name
))
240 INIT_LIST_HEAD(&clk
->children
);
242 mutex_lock(&clocks_mutex
);
243 list_add_tail(&clk
->node
, &clocks
);
245 if (clk
->set_parent
) {
246 int ret
= clk
->set_parent(clk
, clk
->parent
);
249 mutex_unlock(&clocks_mutex
);
253 list_add_tail(&clk
->childnode
, &clk
->parent
->children
);
255 mutex_unlock(&clocks_mutex
);
257 /* If rate is already set, use it */
261 /* Else, see if there is a way to calculate it */
263 clk
->rate
= clk
->recalc(clk
);
265 /* Otherwise, default to parent rate */
266 else if (clk
->parent
)
267 clk
->rate
= clk
->parent
->rate
;
271 EXPORT_SYMBOL(clk_register
);
273 void clk_unregister(struct clk
*clk
)
275 if (clk
== NULL
|| IS_ERR(clk
))
278 mutex_lock(&clocks_mutex
);
279 list_del(&clk
->node
);
280 list_del(&clk
->childnode
);
281 mutex_unlock(&clocks_mutex
);
283 EXPORT_SYMBOL(clk_unregister
);
285 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
287 * Disable any unused clocks left on by the bootloader
289 int __init
davinci_clk_disable_unused(void)
293 spin_lock_irq(&clockfw_lock
);
294 list_for_each_entry(ck
, &clocks
, node
) {
295 if (ck
->usecount
> 0)
297 if (!(ck
->flags
& CLK_PSC
))
300 /* ignore if in Disabled or SwRstDisable states */
301 if (!davinci_psc_is_clk_active(ck
->gpsc
, ck
->lpsc
))
304 pr_debug("Clocks: disable unused %s\n", ck
->name
);
306 davinci_psc_config(ck
->domain
, ck
->gpsc
, ck
->lpsc
,
309 spin_unlock_irq(&clockfw_lock
);
315 static unsigned long clk_sysclk_recalc(struct clk
*clk
)
318 struct pll_data
*pll
;
319 unsigned long rate
= clk
->rate
;
321 /* If this is the PLL base clock, no more calculations needed */
325 if (WARN_ON(!clk
->parent
))
328 rate
= clk
->parent
->rate
;
330 /* Otherwise, the parent must be a PLL */
331 if (WARN_ON(!clk
->parent
->pll_data
))
334 pll
= clk
->parent
->pll_data
;
336 /* If pre-PLL, source clock is before the multiplier and divider(s) */
337 if (clk
->flags
& PRE_PLL
)
338 rate
= pll
->input_rate
;
343 v
= __raw_readl(pll
->base
+ clk
->div_reg
);
345 plldiv
= (v
& pll
->div_ratio_mask
) + 1;
353 int davinci_set_sysclk_rate(struct clk
*clk
, unsigned long rate
)
356 struct pll_data
*pll
;
360 /* If this is the PLL base clock, wrong function to call */
364 /* There must be a parent... */
365 if (WARN_ON(!clk
->parent
))
368 /* ... the parent must be a PLL... */
369 if (WARN_ON(!clk
->parent
->pll_data
))
372 /* ... and this clock must have a divider. */
373 if (WARN_ON(!clk
->div_reg
))
376 pll
= clk
->parent
->pll_data
;
378 input
= clk
->parent
->rate
;
380 /* If pre-PLL, source clock is before the multiplier and divider(s) */
381 if (clk
->flags
& PRE_PLL
)
382 input
= pll
->input_rate
;
386 * Can afford to provide an output little higher than requested
387 * only if maximum rate supported by hardware on this sysclk
391 ratio
= DIV_ROUND_CLOSEST(input
, rate
);
392 if (input
/ ratio
> clk
->maxrate
)
397 ratio
= DIV_ROUND_UP(input
, rate
);
402 if (ratio
> pll
->div_ratio_mask
)
406 v
= __raw_readl(pll
->base
+ PLLSTAT
);
407 } while (v
& PLLSTAT_GOSTAT
);
409 v
= __raw_readl(pll
->base
+ clk
->div_reg
);
410 v
&= ~pll
->div_ratio_mask
;
411 v
|= ratio
| PLLDIV_EN
;
412 __raw_writel(v
, pll
->base
+ clk
->div_reg
);
414 v
= __raw_readl(pll
->base
+ PLLCMD
);
416 __raw_writel(v
, pll
->base
+ PLLCMD
);
419 v
= __raw_readl(pll
->base
+ PLLSTAT
);
420 } while (v
& PLLSTAT_GOSTAT
);
424 EXPORT_SYMBOL(davinci_set_sysclk_rate
);
426 static unsigned long clk_leafclk_recalc(struct clk
*clk
)
428 if (WARN_ON(!clk
->parent
))
431 return clk
->parent
->rate
;
434 int davinci_simple_set_rate(struct clk
*clk
, unsigned long rate
)
440 static unsigned long clk_pllclk_recalc(struct clk
*clk
)
442 u32 ctrl
, mult
= 1, prediv
= 1, postdiv
= 1;
444 struct pll_data
*pll
= clk
->pll_data
;
445 unsigned long rate
= clk
->rate
;
447 ctrl
= __raw_readl(pll
->base
+ PLLCTL
);
448 rate
= pll
->input_rate
= clk
->parent
->rate
;
450 if (ctrl
& PLLCTL_PLLEN
) {
452 mult
= __raw_readl(pll
->base
+ PLLM
);
453 if (cpu_is_davinci_dm365())
454 mult
= 2 * (mult
& PLLM_PLLM_MASK
);
456 mult
= (mult
& PLLM_PLLM_MASK
) + 1;
460 if (pll
->flags
& PLL_HAS_PREDIV
) {
461 prediv
= __raw_readl(pll
->base
+ PREDIV
);
462 if (prediv
& PLLDIV_EN
)
463 prediv
= (prediv
& pll
->div_ratio_mask
) + 1;
468 /* pre-divider is fixed, but (some?) chips won't report that */
469 if (cpu_is_davinci_dm355() && pll
->num
== 1)
472 if (pll
->flags
& PLL_HAS_POSTDIV
) {
473 postdiv
= __raw_readl(pll
->base
+ POSTDIV
);
474 if (postdiv
& PLLDIV_EN
)
475 postdiv
= (postdiv
& pll
->div_ratio_mask
) + 1;
486 pr_debug("PLL%d: input = %lu MHz [ ",
487 pll
->num
, clk
->parent
->rate
/ 1000000);
491 pr_debug("/ %d ", prediv
);
493 pr_debug("* %d ", mult
);
495 pr_debug("/ %d ", postdiv
);
496 pr_debug("] --> %lu MHz output.\n", rate
/ 1000000);
502 * davinci_set_pllrate - set the output rate of a given PLL.
504 * Note: Currently tested to work with OMAP-L138 only.
506 * @pll: pll whose rate needs to be changed.
507 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
508 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
509 * @postdiv: The post divider value. Passing 0 disables the post-divider.
511 int davinci_set_pllrate(struct pll_data
*pll
, unsigned int prediv
,
512 unsigned int mult
, unsigned int postdiv
)
515 unsigned int locktime
;
518 if (pll
->base
== NULL
)
522 * PLL lock time required per OMAP-L138 datasheet is
523 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
524 * as 4 and OSCIN cycle as 25 MHz.
527 locktime
= ((2000 * prediv
) / 100);
528 prediv
= (prediv
- 1) | PLLDIV_EN
;
530 locktime
= PLL_LOCK_TIME
;
533 postdiv
= (postdiv
- 1) | PLLDIV_EN
;
537 /* Protect against simultaneous calls to PLL setting seqeunce */
538 spin_lock_irqsave(&clockfw_lock
, flags
);
540 ctrl
= __raw_readl(pll
->base
+ PLLCTL
);
542 /* Switch the PLL to bypass mode */
543 ctrl
&= ~(PLLCTL_PLLENSRC
| PLLCTL_PLLEN
);
544 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
546 udelay(PLL_BYPASS_TIME
);
548 /* Reset and enable PLL */
549 ctrl
&= ~(PLLCTL_PLLRST
| PLLCTL_PLLDIS
);
550 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
552 if (pll
->flags
& PLL_HAS_PREDIV
)
553 __raw_writel(prediv
, pll
->base
+ PREDIV
);
555 __raw_writel(mult
, pll
->base
+ PLLM
);
557 if (pll
->flags
& PLL_HAS_POSTDIV
)
558 __raw_writel(postdiv
, pll
->base
+ POSTDIV
);
560 udelay(PLL_RESET_TIME
);
562 /* Bring PLL out of reset */
563 ctrl
|= PLLCTL_PLLRST
;
564 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
568 /* Remove PLL from bypass mode */
569 ctrl
|= PLLCTL_PLLEN
;
570 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
572 spin_unlock_irqrestore(&clockfw_lock
, flags
);
576 EXPORT_SYMBOL(davinci_set_pllrate
);
579 * davinci_set_refclk_rate() - Set the reference clock rate
580 * @rate: The new rate.
582 * Sets the reference clock rate to a given value. This will most likely
583 * result in the entire clock tree getting updated.
585 * This is used to support boards which use a reference clock different
586 * than that used by default in <soc>.c file. The reference clock rate
587 * should be updated early in the boot process; ideally soon after the
588 * clock tree has been initialized once with the default reference clock
589 * rate (davinci_clk_init()).
591 * Returns 0 on success, error otherwise.
593 int davinci_set_refclk_rate(unsigned long rate
)
597 refclk
= clk_get(NULL
, "ref");
598 if (IS_ERR(refclk
)) {
599 pr_err("%s: failed to get reference clock\n", __func__
);
600 return PTR_ERR(refclk
);
603 clk_set_rate(refclk
, rate
);
610 int __init
davinci_clk_init(struct clk_lookup
*clocks
)
612 struct clk_lookup
*c
;
614 size_t num_clocks
= 0;
616 for (c
= clocks
; c
->clk
; c
++) {
621 /* Check if clock is a PLL */
623 clk
->recalc
= clk_pllclk_recalc
;
625 /* Else, if it is a PLL-derived clock */
626 else if (clk
->flags
& CLK_PLL
)
627 clk
->recalc
= clk_sysclk_recalc
;
629 /* Otherwise, it is a leaf clock (PSC clock) */
630 else if (clk
->parent
)
631 clk
->recalc
= clk_leafclk_recalc
;
635 struct pll_data
*pll
= clk
->pll_data
;
637 if (!pll
->div_ratio_mask
)
638 pll
->div_ratio_mask
= PLLDIV_RATIO_MASK
;
640 if (pll
->phys_base
&& !pll
->base
) {
641 pll
->base
= ioremap(pll
->phys_base
, SZ_4K
);
647 clk
->rate
= clk
->recalc(clk
);
650 clk
->flags
|= CLK_PSC
;
652 if (clk
->flags
& PSC_LRST
)
653 clk
->reset
= davinci_clk_reset
;
658 /* Turn on clocks that Linux doesn't otherwise manage */
659 if (clk
->flags
& ALWAYS_ENABLED
)
663 clkdev_add_table(clocks
, num_clocks
);
668 #ifdef CONFIG_DEBUG_FS
670 #include <linux/debugfs.h>
671 #include <linux/seq_file.h>
673 #define CLKNAME_MAX 10 /* longest clock name */
678 dump_clock(struct seq_file
*s
, unsigned nest
, struct clk
*parent
)
681 char buf
[CLKNAME_MAX
+ NEST_DELTA
* NEST_MAX
];
685 if (parent
->flags
& CLK_PLL
)
687 else if (parent
->flags
& CLK_PSC
)
692 /* <nest spaces> name <pad to end> */
693 memset(buf
, ' ', sizeof(buf
) - 1);
694 buf
[sizeof(buf
) - 1] = 0;
695 i
= strlen(parent
->name
);
696 memcpy(buf
+ nest
, parent
->name
,
697 min(i
, (unsigned)(sizeof(buf
) - 1 - nest
)));
699 seq_printf(s
, "%s users=%2d %-3s %9ld Hz\n",
700 buf
, parent
->usecount
, state
, clk_get_rate(parent
));
701 /* REVISIT show device associations too */
703 /* cost is now small, but not linear... */
704 list_for_each_entry(clk
, &parent
->children
, childnode
) {
705 dump_clock(s
, nest
+ NEST_DELTA
, clk
);
709 static int davinci_ck_show(struct seq_file
*m
, void *v
)
714 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
716 mutex_lock(&clocks_mutex
);
717 list_for_each_entry(clk
, &clocks
, node
)
719 dump_clock(m
, 0, clk
);
720 mutex_unlock(&clocks_mutex
);
725 static int davinci_ck_open(struct inode
*inode
, struct file
*file
)
727 return single_open(file
, davinci_ck_show
, NULL
);
730 static const struct file_operations davinci_ck_operations
= {
731 .open
= davinci_ck_open
,
734 .release
= single_release
,
737 static int __init
davinci_clk_debugfs_init(void)
739 debugfs_create_file("davinci_clocks", S_IFREG
| S_IRUGO
, NULL
, NULL
,
740 &davinci_ck_operations
);
744 device_initcall(davinci_clk_debugfs_init
);
745 #endif /* CONFIG_DEBUG_FS */