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 davinci_psc_config(psc_domain(clk
), clk
->gpsc
, clk
->lpsc
, 0);
55 __clk_disable(clk
->parent
);
58 int clk_enable(struct clk
*clk
)
62 if (clk
== NULL
|| IS_ERR(clk
))
65 spin_lock_irqsave(&clockfw_lock
, flags
);
67 spin_unlock_irqrestore(&clockfw_lock
, flags
);
71 EXPORT_SYMBOL(clk_enable
);
73 void clk_disable(struct clk
*clk
)
77 if (clk
== NULL
|| IS_ERR(clk
))
80 spin_lock_irqsave(&clockfw_lock
, flags
);
82 spin_unlock_irqrestore(&clockfw_lock
, flags
);
84 EXPORT_SYMBOL(clk_disable
);
86 unsigned long clk_get_rate(struct clk
*clk
)
88 if (clk
== NULL
|| IS_ERR(clk
))
93 EXPORT_SYMBOL(clk_get_rate
);
95 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
97 if (clk
== NULL
|| IS_ERR(clk
))
101 return clk
->round_rate(clk
, rate
);
105 EXPORT_SYMBOL(clk_round_rate
);
107 /* Propagate rate to children */
108 static void propagate_rate(struct clk
*root
)
112 list_for_each_entry(clk
, &root
->children
, childnode
) {
114 clk
->rate
= clk
->recalc(clk
);
119 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
124 if (clk
== NULL
|| IS_ERR(clk
))
127 spin_lock_irqsave(&clockfw_lock
, flags
);
129 ret
= clk
->set_rate(clk
, rate
);
132 clk
->rate
= clk
->recalc(clk
);
135 spin_unlock_irqrestore(&clockfw_lock
, flags
);
139 EXPORT_SYMBOL(clk_set_rate
);
141 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
145 if (clk
== NULL
|| IS_ERR(clk
))
148 /* Cannot change parent on enabled clock */
149 if (WARN_ON(clk
->usecount
))
152 mutex_lock(&clocks_mutex
);
153 clk
->parent
= parent
;
154 list_del_init(&clk
->childnode
);
155 list_add(&clk
->childnode
, &clk
->parent
->children
);
156 mutex_unlock(&clocks_mutex
);
158 spin_lock_irqsave(&clockfw_lock
, flags
);
160 clk
->rate
= clk
->recalc(clk
);
162 spin_unlock_irqrestore(&clockfw_lock
, flags
);
166 EXPORT_SYMBOL(clk_set_parent
);
168 int clk_register(struct clk
*clk
)
170 if (clk
== NULL
|| IS_ERR(clk
))
173 if (WARN(clk
->parent
&& !clk
->parent
->rate
,
174 "CLK: %s parent %s has no rate!\n",
175 clk
->name
, clk
->parent
->name
))
178 INIT_LIST_HEAD(&clk
->children
);
180 mutex_lock(&clocks_mutex
);
181 list_add_tail(&clk
->node
, &clocks
);
183 list_add_tail(&clk
->childnode
, &clk
->parent
->children
);
184 mutex_unlock(&clocks_mutex
);
186 /* If rate is already set, use it */
190 /* Else, see if there is a way to calculate it */
192 clk
->rate
= clk
->recalc(clk
);
194 /* Otherwise, default to parent rate */
195 else if (clk
->parent
)
196 clk
->rate
= clk
->parent
->rate
;
200 EXPORT_SYMBOL(clk_register
);
202 void clk_unregister(struct clk
*clk
)
204 if (clk
== NULL
|| IS_ERR(clk
))
207 mutex_lock(&clocks_mutex
);
208 list_del(&clk
->node
);
209 list_del(&clk
->childnode
);
210 mutex_unlock(&clocks_mutex
);
212 EXPORT_SYMBOL(clk_unregister
);
214 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
216 * Disable any unused clocks left on by the bootloader
218 static int __init
clk_disable_unused(void)
222 spin_lock_irq(&clockfw_lock
);
223 list_for_each_entry(ck
, &clocks
, node
) {
224 if (ck
->usecount
> 0)
226 if (!(ck
->flags
& CLK_PSC
))
229 /* ignore if in Disabled or SwRstDisable states */
230 if (!davinci_psc_is_clk_active(ck
->gpsc
, ck
->lpsc
))
233 pr_info("Clocks: disable unused %s\n", ck
->name
);
234 davinci_psc_config(psc_domain(ck
), ck
->gpsc
, ck
->lpsc
, 0);
236 spin_unlock_irq(&clockfw_lock
);
240 late_initcall(clk_disable_unused
);
243 static unsigned long clk_sysclk_recalc(struct clk
*clk
)
246 struct pll_data
*pll
;
247 unsigned long rate
= clk
->rate
;
249 /* If this is the PLL base clock, no more calculations needed */
253 if (WARN_ON(!clk
->parent
))
256 rate
= clk
->parent
->rate
;
258 /* Otherwise, the parent must be a PLL */
259 if (WARN_ON(!clk
->parent
->pll_data
))
262 pll
= clk
->parent
->pll_data
;
264 /* If pre-PLL, source clock is before the multiplier and divider(s) */
265 if (clk
->flags
& PRE_PLL
)
266 rate
= pll
->input_rate
;
271 v
= __raw_readl(pll
->base
+ clk
->div_reg
);
273 plldiv
= (v
& PLLDIV_RATIO_MASK
) + 1;
281 static unsigned long clk_leafclk_recalc(struct clk
*clk
)
283 if (WARN_ON(!clk
->parent
))
286 return clk
->parent
->rate
;
289 static unsigned long clk_pllclk_recalc(struct clk
*clk
)
291 u32 ctrl
, mult
= 1, prediv
= 1, postdiv
= 1;
293 struct pll_data
*pll
= clk
->pll_data
;
294 unsigned long rate
= clk
->rate
;
296 pll
->base
= IO_ADDRESS(pll
->phys_base
);
297 ctrl
= __raw_readl(pll
->base
+ PLLCTL
);
298 rate
= pll
->input_rate
= clk
->parent
->rate
;
300 if (ctrl
& PLLCTL_PLLEN
) {
302 mult
= __raw_readl(pll
->base
+ PLLM
);
303 if (cpu_is_davinci_dm365())
304 mult
= 2 * (mult
& PLLM_PLLM_MASK
);
306 mult
= (mult
& PLLM_PLLM_MASK
) + 1;
310 if (pll
->flags
& PLL_HAS_PREDIV
) {
311 prediv
= __raw_readl(pll
->base
+ PREDIV
);
312 if (prediv
& PLLDIV_EN
)
313 prediv
= (prediv
& PLLDIV_RATIO_MASK
) + 1;
318 /* pre-divider is fixed, but (some?) chips won't report that */
319 if (cpu_is_davinci_dm355() && pll
->num
== 1)
322 if (pll
->flags
& PLL_HAS_POSTDIV
) {
323 postdiv
= __raw_readl(pll
->base
+ POSTDIV
);
324 if (postdiv
& PLLDIV_EN
)
325 postdiv
= (postdiv
& PLLDIV_RATIO_MASK
) + 1;
336 pr_debug("PLL%d: input = %lu MHz [ ",
337 pll
->num
, clk
->parent
->rate
/ 1000000);
341 pr_debug("/ %d ", prediv
);
343 pr_debug("* %d ", mult
);
345 pr_debug("/ %d ", postdiv
);
346 pr_debug("] --> %lu MHz output.\n", rate
/ 1000000);
352 * davinci_set_pllrate - set the output rate of a given PLL.
354 * Note: Currently tested to work with OMAP-L138 only.
356 * @pll: pll whose rate needs to be changed.
357 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
358 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
359 * @postdiv: The post divider value. Passing 0 disables the post-divider.
361 int davinci_set_pllrate(struct pll_data
*pll
, unsigned int prediv
,
362 unsigned int mult
, unsigned int postdiv
)
365 unsigned int locktime
;
367 if (pll
->base
== NULL
)
371 * PLL lock time required per OMAP-L138 datasheet is
372 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
373 * as 4 and OSCIN cycle as 25 MHz.
376 locktime
= ((2000 * prediv
) / 100);
377 prediv
= (prediv
- 1) | PLLDIV_EN
;
379 locktime
= PLL_LOCK_TIME
;
382 postdiv
= (postdiv
- 1) | PLLDIV_EN
;
386 ctrl
= __raw_readl(pll
->base
+ PLLCTL
);
388 /* Switch the PLL to bypass mode */
389 ctrl
&= ~(PLLCTL_PLLENSRC
| PLLCTL_PLLEN
);
390 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
392 udelay(PLL_BYPASS_TIME
);
394 /* Reset and enable PLL */
395 ctrl
&= ~(PLLCTL_PLLRST
| PLLCTL_PLLDIS
);
396 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
398 if (pll
->flags
& PLL_HAS_PREDIV
)
399 __raw_writel(prediv
, pll
->base
+ PREDIV
);
401 __raw_writel(mult
, pll
->base
+ PLLM
);
403 if (pll
->flags
& PLL_HAS_POSTDIV
)
404 __raw_writel(postdiv
, pll
->base
+ POSTDIV
);
406 udelay(PLL_RESET_TIME
);
408 /* Bring PLL out of reset */
409 ctrl
|= PLLCTL_PLLRST
;
410 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
414 /* Remove PLL from bypass mode */
415 ctrl
|= PLLCTL_PLLEN
;
416 __raw_writel(ctrl
, pll
->base
+ PLLCTL
);
420 EXPORT_SYMBOL(davinci_set_pllrate
);
422 int __init
davinci_clk_init(struct davinci_clk
*clocks
)
424 struct davinci_clk
*c
;
427 for (c
= clocks
; c
->lk
.clk
; c
++) {
432 /* Check if clock is a PLL */
434 clk
->recalc
= clk_pllclk_recalc
;
436 /* Else, if it is a PLL-derived clock */
437 else if (clk
->flags
& CLK_PLL
)
438 clk
->recalc
= clk_sysclk_recalc
;
440 /* Otherwise, it is a leaf clock (PSC clock) */
441 else if (clk
->parent
)
442 clk
->recalc
= clk_leafclk_recalc
;
446 clk
->rate
= clk
->recalc(clk
);
449 clk
->flags
|= CLK_PSC
;
454 /* Turn on clocks that Linux doesn't otherwise manage */
455 if (clk
->flags
& ALWAYS_ENABLED
)
462 #ifdef CONFIG_PROC_FS
463 #include <linux/proc_fs.h>
464 #include <linux/seq_file.h>
466 static void *davinci_ck_start(struct seq_file
*m
, loff_t
*pos
)
468 return *pos
< 1 ? (void *)1 : NULL
;
471 static void *davinci_ck_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
477 static void davinci_ck_stop(struct seq_file
*m
, void *v
)
481 #define CLKNAME_MAX 10 /* longest clock name */
486 dump_clock(struct seq_file
*s
, unsigned nest
, struct clk
*parent
)
489 char buf
[CLKNAME_MAX
+ NEST_DELTA
* NEST_MAX
];
493 if (parent
->flags
& CLK_PLL
)
495 else if (parent
->flags
& CLK_PSC
)
500 /* <nest spaces> name <pad to end> */
501 memset(buf
, ' ', sizeof(buf
) - 1);
502 buf
[sizeof(buf
) - 1] = 0;
503 i
= strlen(parent
->name
);
504 memcpy(buf
+ nest
, parent
->name
,
505 min(i
, (unsigned)(sizeof(buf
) - 1 - nest
)));
507 seq_printf(s
, "%s users=%2d %-3s %9ld Hz\n",
508 buf
, parent
->usecount
, state
, clk_get_rate(parent
));
509 /* REVISIT show device associations too */
511 /* cost is now small, but not linear... */
512 list_for_each_entry(clk
, &parent
->children
, childnode
) {
513 dump_clock(s
, nest
+ NEST_DELTA
, clk
);
517 static int davinci_ck_show(struct seq_file
*m
, void *v
)
519 /* Show clock tree; we know the main oscillator is first.
520 * We trust nonzero usecounts equate to PSC enables...
522 mutex_lock(&clocks_mutex
);
523 if (!list_empty(&clocks
))
524 dump_clock(m
, 0, list_first_entry(&clocks
, struct clk
, node
));
525 mutex_unlock(&clocks_mutex
);
530 static const struct seq_operations davinci_ck_op
= {
531 .start
= davinci_ck_start
,
532 .next
= davinci_ck_next
,
533 .stop
= davinci_ck_stop
,
534 .show
= davinci_ck_show
537 static int davinci_ck_open(struct inode
*inode
, struct file
*file
)
539 return seq_open(file
, &davinci_ck_op
);
542 static const struct file_operations proc_davinci_ck_operations
= {
543 .open
= davinci_ck_open
,
546 .release
= seq_release
,
549 static int __init
davinci_ck_proc_init(void)
551 proc_create("davinci_clocks", 0, NULL
, &proc_davinci_ck_operations
);
555 __initcall(davinci_ck_proc_init
);
556 #endif /* CONFIG_DEBUG_PROC_FS */