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>
26 #include <mach/cputype.h>
29 static LIST_HEAD(clocks
);
30 static DEFINE_MUTEX(clocks_mutex
);
31 static DEFINE_SPINLOCK(clockfw_lock
);
33 static unsigned psc_domain(struct clk
*clk
)
35 return (clk
->flags
& PSC_DSP
)
36 ? DAVINCI_GPSC_DSPDOMAIN
37 : DAVINCI_GPSC_ARMDOMAIN
;
40 static void __clk_enable(struct clk
*clk
)
43 __clk_enable(clk
->parent
);
44 if (clk
->usecount
++ == 0 && (clk
->flags
& CLK_PSC
))
45 davinci_psc_config(psc_domain(clk
), clk
->gpsc
, clk
->lpsc
, 1);
48 static void __clk_disable(struct clk
*clk
)
50 if (WARN_ON(clk
->usecount
== 0))
52 if (--clk
->usecount
== 0 && !(clk
->flags
& CLK_PLL
) &&
53 (clk
->flags
& CLK_PSC
))
54 davinci_psc_config(psc_domain(clk
), clk
->gpsc
, clk
->lpsc
, 0);
56 __clk_disable(clk
->parent
);
59 int clk_enable(struct clk
*clk
)
63 if (clk
== NULL
|| IS_ERR(clk
))
66 spin_lock_irqsave(&clockfw_lock
, flags
);
68 spin_unlock_irqrestore(&clockfw_lock
, flags
);
72 EXPORT_SYMBOL(clk_enable
);
74 void clk_disable(struct clk
*clk
)
78 if (clk
== NULL
|| IS_ERR(clk
))
81 spin_lock_irqsave(&clockfw_lock
, flags
);
83 spin_unlock_irqrestore(&clockfw_lock
, flags
);
85 EXPORT_SYMBOL(clk_disable
);
87 unsigned long clk_get_rate(struct clk
*clk
)
89 if (clk
== NULL
|| IS_ERR(clk
))
94 EXPORT_SYMBOL(clk_get_rate
);
96 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
98 if (clk
== NULL
|| IS_ERR(clk
))
102 return clk
->round_rate(clk
, rate
);
106 EXPORT_SYMBOL(clk_round_rate
);
108 /* Propagate rate to children */
109 static void propagate_rate(struct clk
*root
)
113 list_for_each_entry(clk
, &root
->children
, childnode
) {
115 clk
->rate
= clk
->recalc(clk
);
120 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
125 if (clk
== NULL
|| IS_ERR(clk
))
129 ret
= clk
->set_rate(clk
, rate
);
131 spin_lock_irqsave(&clockfw_lock
, flags
);
134 clk
->rate
= clk
->recalc(clk
);
137 spin_unlock_irqrestore(&clockfw_lock
, flags
);
141 EXPORT_SYMBOL(clk_set_rate
);
143 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
147 if (clk
== NULL
|| IS_ERR(clk
))
150 /* Cannot change parent on enabled clock */
151 if (WARN_ON(clk
->usecount
))
154 mutex_lock(&clocks_mutex
);
155 clk
->parent
= parent
;
156 list_del_init(&clk
->childnode
);
157 list_add(&clk
->childnode
, &clk
->parent
->children
);
158 mutex_unlock(&clocks_mutex
);
160 spin_lock_irqsave(&clockfw_lock
, flags
);
162 clk
->rate
= clk
->recalc(clk
);
164 spin_unlock_irqrestore(&clockfw_lock
, flags
);
168 EXPORT_SYMBOL(clk_set_parent
);
170 int clk_register(struct clk
*clk
)
172 if (clk
== NULL
|| IS_ERR(clk
))
175 if (WARN(clk
->parent
&& !clk
->parent
->rate
,
176 "CLK: %s parent %s has no rate!\n",
177 clk
->name
, clk
->parent
->name
))
180 INIT_LIST_HEAD(&clk
->children
);
182 mutex_lock(&clocks_mutex
);
183 list_add_tail(&clk
->node
, &clocks
);
185 list_add_tail(&clk
->childnode
, &clk
->parent
->children
);
186 mutex_unlock(&clocks_mutex
);
188 /* If rate is already set, use it */
192 /* Else, see if there is a way to calculate it */
194 clk
->rate
= clk
->recalc(clk
);
196 /* Otherwise, default to parent rate */
197 else if (clk
->parent
)
198 clk
->rate
= clk
->parent
->rate
;
202 EXPORT_SYMBOL(clk_register
);
204 void clk_unregister(struct clk
*clk
)
206 if (clk
== NULL
|| IS_ERR(clk
))
209 mutex_lock(&clocks_mutex
);
210 list_del(&clk
->node
);
211 list_del(&clk
->childnode
);
212 mutex_unlock(&clocks_mutex
);
214 EXPORT_SYMBOL(clk_unregister
);
216 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
218 * Disable any unused clocks left on by the bootloader
220 static int __init
clk_disable_unused(void)
224 spin_lock_irq(&clockfw_lock
);
225 list_for_each_entry(ck
, &clocks
, node
) {
226 if (ck
->usecount
> 0)
228 if (!(ck
->flags
& CLK_PSC
))
231 /* ignore if in Disabled or SwRstDisable states */
232 if (!davinci_psc_is_clk_active(ck
->gpsc
, ck
->lpsc
))
235 pr_info("Clocks: disable unused %s\n", ck
->name
);
236 davinci_psc_config(psc_domain(ck
), ck
->gpsc
, ck
->lpsc
, 0);
238 spin_unlock_irq(&clockfw_lock
);
242 late_initcall(clk_disable_unused
);
245 static unsigned long clk_sysclk_recalc(struct clk
*clk
)
248 struct pll_data
*pll
;
249 unsigned long rate
= clk
->rate
;
251 /* If this is the PLL base clock, no more calculations needed */
255 if (WARN_ON(!clk
->parent
))
258 rate
= clk
->parent
->rate
;
260 /* Otherwise, the parent must be a PLL */
261 if (WARN_ON(!clk
->parent
->pll_data
))
264 pll
= clk
->parent
->pll_data
;
266 /* If pre-PLL, source clock is before the multiplier and divider(s) */
267 if (clk
->flags
& PRE_PLL
)
268 rate
= pll
->input_rate
;
273 v
= __raw_readl(pll
->base
+ clk
->div_reg
);
275 plldiv
= (v
& PLLDIV_RATIO_MASK
) + 1;
283 static unsigned long clk_leafclk_recalc(struct clk
*clk
)
285 if (WARN_ON(!clk
->parent
))
288 return clk
->parent
->rate
;
291 static unsigned long clk_pllclk_recalc(struct clk
*clk
)
293 u32 ctrl
, mult
= 1, prediv
= 1, postdiv
= 1;
295 struct pll_data
*pll
= clk
->pll_data
;
296 unsigned long rate
= clk
->rate
;
298 pll
->base
= IO_ADDRESS(pll
->phys_base
);
299 ctrl
= __raw_readl(pll
->base
+ PLLCTL
);
300 rate
= pll
->input_rate
= clk
->parent
->rate
;
302 if (ctrl
& PLLCTL_PLLEN
) {
304 mult
= __raw_readl(pll
->base
+ PLLM
);
305 if (cpu_is_davinci_dm365())
306 mult
= 2 * (mult
& PLLM_PLLM_MASK
);
308 mult
= (mult
& PLLM_PLLM_MASK
) + 1;
312 if (pll
->flags
& PLL_HAS_PREDIV
) {
313 prediv
= __raw_readl(pll
->base
+ PREDIV
);
314 if (prediv
& PLLDIV_EN
)
315 prediv
= (prediv
& PLLDIV_RATIO_MASK
) + 1;
320 /* pre-divider is fixed, but (some?) chips won't report that */
321 if (cpu_is_davinci_dm355() && pll
->num
== 1)
324 if (pll
->flags
& PLL_HAS_POSTDIV
) {
325 postdiv
= __raw_readl(pll
->base
+ POSTDIV
);
326 if (postdiv
& PLLDIV_EN
)
327 postdiv
= (postdiv
& PLLDIV_RATIO_MASK
) + 1;
338 pr_debug("PLL%d: input = %lu MHz [ ",
339 pll
->num
, clk
->parent
->rate
/ 1000000);
343 pr_debug("/ %d ", prediv
);
345 pr_debug("* %d ", mult
);
347 pr_debug("/ %d ", postdiv
);
348 pr_debug("] --> %lu MHz output.\n", rate
/ 1000000);
354 * davinci_set_pllrate - set the output rate of a given PLL.
356 * Note: Currently tested to work with OMAP-L138 only.
358 * @pll: pll whose rate needs to be changed.
359 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
360 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
361 * @postdiv: The post divider value. Passing 0 disables the post-divider.
363 int davinci_set_pllrate(struct pll_data
*pll
, unsigned int prediv
,
364 unsigned int mult
, unsigned int postdiv
)
367 unsigned int locktime
;
370 if (pll
->base
== NULL
)
374 * PLL lock time required per OMAP-L138 datasheet is
375 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
376 * as 4 and OSCIN cycle as 25 MHz.
379 locktime
= ((2000 * prediv
) / 100);
380 prediv
= (prediv
- 1) | PLLDIV_EN
;
382 locktime
= PLL_LOCK_TIME
;
385 postdiv
= (postdiv
- 1) | PLLDIV_EN
;
389 /* Protect against simultaneous calls to PLL setting seqeunce */
390 spin_lock_irqsave(&clockfw_lock
, flags
);
392 ctrl
= __raw_readl(pll
->base
+ PLLCTL
);
394 /* Switch the PLL to bypass mode */
395 ctrl
&= ~(PLLCTL_PLLENSRC
| PLLCTL_PLLEN
);
396 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
398 udelay(PLL_BYPASS_TIME
);
400 /* Reset and enable PLL */
401 ctrl
&= ~(PLLCTL_PLLRST
| PLLCTL_PLLDIS
);
402 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
404 if (pll
->flags
& PLL_HAS_PREDIV
)
405 __raw_writel(prediv
, pll
->base
+ PREDIV
);
407 __raw_writel(mult
, pll
->base
+ PLLM
);
409 if (pll
->flags
& PLL_HAS_POSTDIV
)
410 __raw_writel(postdiv
, pll
->base
+ POSTDIV
);
412 udelay(PLL_RESET_TIME
);
414 /* Bring PLL out of reset */
415 ctrl
|= PLLCTL_PLLRST
;
416 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
420 /* Remove PLL from bypass mode */
421 ctrl
|= PLLCTL_PLLEN
;
422 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
424 spin_unlock_irqrestore(&clockfw_lock
, flags
);
428 EXPORT_SYMBOL(davinci_set_pllrate
);
430 int __init
davinci_clk_init(struct clk_lookup
*clocks
)
432 struct clk_lookup
*c
;
434 size_t num_clocks
= 0;
436 for (c
= clocks
; c
->clk
; c
++) {
441 /* Check if clock is a PLL */
443 clk
->recalc
= clk_pllclk_recalc
;
445 /* Else, if it is a PLL-derived clock */
446 else if (clk
->flags
& CLK_PLL
)
447 clk
->recalc
= clk_sysclk_recalc
;
449 /* Otherwise, it is a leaf clock (PSC clock) */
450 else if (clk
->parent
)
451 clk
->recalc
= clk_leafclk_recalc
;
455 clk
->rate
= clk
->recalc(clk
);
458 clk
->flags
|= CLK_PSC
;
463 /* Turn on clocks that Linux doesn't otherwise manage */
464 if (clk
->flags
& ALWAYS_ENABLED
)
468 clkdev_add_table(clocks
, num_clocks
);
473 #ifdef CONFIG_DEBUG_FS
475 #include <linux/debugfs.h>
476 #include <linux/seq_file.h>
478 #define CLKNAME_MAX 10 /* longest clock name */
483 dump_clock(struct seq_file
*s
, unsigned nest
, struct clk
*parent
)
486 char buf
[CLKNAME_MAX
+ NEST_DELTA
* NEST_MAX
];
490 if (parent
->flags
& CLK_PLL
)
492 else if (parent
->flags
& CLK_PSC
)
497 /* <nest spaces> name <pad to end> */
498 memset(buf
, ' ', sizeof(buf
) - 1);
499 buf
[sizeof(buf
) - 1] = 0;
500 i
= strlen(parent
->name
);
501 memcpy(buf
+ nest
, parent
->name
,
502 min(i
, (unsigned)(sizeof(buf
) - 1 - nest
)));
504 seq_printf(s
, "%s users=%2d %-3s %9ld Hz\n",
505 buf
, parent
->usecount
, state
, clk_get_rate(parent
));
506 /* REVISIT show device associations too */
508 /* cost is now small, but not linear... */
509 list_for_each_entry(clk
, &parent
->children
, childnode
) {
510 dump_clock(s
, nest
+ NEST_DELTA
, clk
);
514 static int davinci_ck_show(struct seq_file
*m
, void *v
)
519 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
521 mutex_lock(&clocks_mutex
);
522 list_for_each_entry(clk
, &clocks
, node
)
524 dump_clock(m
, 0, clk
);
525 mutex_unlock(&clocks_mutex
);
530 static int davinci_ck_open(struct inode
*inode
, struct file
*file
)
532 return single_open(file
, davinci_ck_show
, NULL
);
535 static const struct file_operations davinci_ck_operations
= {
536 .open
= davinci_ck_open
,
539 .release
= single_release
,
542 static int __init
davinci_clk_debugfs_init(void)
544 debugfs_create_file("davinci_clocks", S_IFREG
| S_IRUGO
, NULL
, NULL
,
545 &davinci_ck_operations
);
549 device_initcall(davinci_clk_debugfs_init
);
550 #endif /* CONFIG_DEBUG_FS */