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/platform_device.h>
22 #include <linux/delay.h>
24 #include <mach/hardware.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 unsigned psc_domain(struct clk *clk)
36 return (clk->flags & PSC_DSP)
37 ? DAVINCI_GPSC_DSPDOMAIN
38 : DAVINCI_GPSC_ARMDOMAIN;
41 static void __clk_enable(struct clk *clk)
44 __clk_enable(clk->parent);
45 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
46 davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
50 static void __clk_disable(struct clk *clk)
52 if (WARN_ON(clk->usecount == 0))
54 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL))
55 davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
58 __clk_disable(clk->parent);
61 int clk_enable(struct clk *clk)
65 if (clk == NULL || IS_ERR(clk))
68 spin_lock_irqsave(&clockfw_lock, flags);
70 spin_unlock_irqrestore(&clockfw_lock, flags);
74 EXPORT_SYMBOL(clk_enable);
76 void clk_disable(struct clk *clk)
80 if (clk == NULL || IS_ERR(clk))
83 spin_lock_irqsave(&clockfw_lock, flags);
85 spin_unlock_irqrestore(&clockfw_lock, flags);
87 EXPORT_SYMBOL(clk_disable);
89 unsigned long clk_get_rate(struct clk *clk)
91 if (clk == NULL || IS_ERR(clk))
96 EXPORT_SYMBOL(clk_get_rate);
98 long clk_round_rate(struct clk *clk, unsigned long rate)
100 if (clk == NULL || IS_ERR(clk))
104 return clk->round_rate(clk, rate);
108 EXPORT_SYMBOL(clk_round_rate);
110 /* Propagate rate to children */
111 static void propagate_rate(struct clk *root)
115 list_for_each_entry(clk, &root->children, childnode) {
117 clk->rate = clk->recalc(clk);
122 int clk_set_rate(struct clk *clk, unsigned long rate)
127 if (clk == NULL || IS_ERR(clk))
130 spin_lock_irqsave(&clockfw_lock, flags);
132 ret = clk->set_rate(clk, rate);
135 clk->rate = clk->recalc(clk);
138 spin_unlock_irqrestore(&clockfw_lock, flags);
142 EXPORT_SYMBOL(clk_set_rate);
144 int clk_set_parent(struct clk *clk, struct clk *parent)
148 if (clk == NULL || IS_ERR(clk))
151 /* Cannot change parent on enabled clock */
152 if (WARN_ON(clk->usecount))
155 mutex_lock(&clocks_mutex);
156 clk->parent = parent;
157 list_del_init(&clk->childnode);
158 list_add(&clk->childnode, &clk->parent->children);
159 mutex_unlock(&clocks_mutex);
161 spin_lock_irqsave(&clockfw_lock, flags);
163 clk->rate = clk->recalc(clk);
165 spin_unlock_irqrestore(&clockfw_lock, flags);
169 EXPORT_SYMBOL(clk_set_parent);
171 int clk_register(struct clk *clk)
173 if (clk == NULL || IS_ERR(clk))
176 if (WARN(clk->parent && !clk->parent->rate,
177 "CLK: %s parent %s has no rate!\n",
178 clk->name, clk->parent->name))
181 INIT_LIST_HEAD(&clk->children);
183 mutex_lock(&clocks_mutex);
184 list_add_tail(&clk->node, &clocks);
186 list_add_tail(&clk->childnode, &clk->parent->children);
187 mutex_unlock(&clocks_mutex);
189 /* If rate is already set, use it */
193 /* Else, see if there is a way to calculate it */
195 clk->rate = clk->recalc(clk);
197 /* Otherwise, default to parent rate */
198 else if (clk->parent)
199 clk->rate = clk->parent->rate;
203 EXPORT_SYMBOL(clk_register);
205 void clk_unregister(struct clk *clk)
207 if (clk == NULL || IS_ERR(clk))
210 mutex_lock(&clocks_mutex);
211 list_del(&clk->node);
212 list_del(&clk->childnode);
213 mutex_unlock(&clocks_mutex);
215 EXPORT_SYMBOL(clk_unregister);
217 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
219 * Disable any unused clocks left on by the bootloader
221 static int __init clk_disable_unused(void)
225 spin_lock_irq(&clockfw_lock);
226 list_for_each_entry(ck, &clocks, node) {
227 if (ck->usecount > 0)
229 if (!(ck->flags & CLK_PSC))
232 /* ignore if in Disabled or SwRstDisable states */
233 if (!davinci_psc_is_clk_active(ck->psc_ctlr, ck->lpsc))
236 pr_info("Clocks: disable unused %s\n", ck->name);
237 davinci_psc_config(psc_domain(ck), ck->psc_ctlr, ck->lpsc, 0);
239 spin_unlock_irq(&clockfw_lock);
243 late_initcall(clk_disable_unused);
246 static unsigned long clk_sysclk_recalc(struct clk *clk)
249 struct pll_data *pll;
250 unsigned long rate = clk->rate;
252 /* If this is the PLL base clock, no more calculations needed */
256 if (WARN_ON(!clk->parent))
259 rate = clk->parent->rate;
261 /* Otherwise, the parent must be a PLL */
262 if (WARN_ON(!clk->parent->pll_data))
265 pll = clk->parent->pll_data;
267 /* If pre-PLL, source clock is before the multiplier and divider(s) */
268 if (clk->flags & PRE_PLL)
269 rate = pll->input_rate;
274 v = __raw_readl(pll->base + clk->div_reg);
276 plldiv = (v & PLLDIV_RATIO_MASK) + 1;
284 static unsigned long clk_leafclk_recalc(struct clk *clk)
286 if (WARN_ON(!clk->parent))
289 return clk->parent->rate;
292 static unsigned long clk_pllclk_recalc(struct clk *clk)
294 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
296 struct pll_data *pll = clk->pll_data;
297 unsigned long rate = clk->rate;
299 pll->base = IO_ADDRESS(pll->phys_base);
300 ctrl = __raw_readl(pll->base + PLLCTL);
301 rate = pll->input_rate = clk->parent->rate;
303 if (ctrl & PLLCTL_PLLEN) {
305 mult = __raw_readl(pll->base + PLLM);
306 if (cpu_is_davinci_dm365())
307 mult = 2 * (mult & PLLM_PLLM_MASK);
309 mult = (mult & PLLM_PLLM_MASK) + 1;
313 if (pll->flags & PLL_HAS_PREDIV) {
314 prediv = __raw_readl(pll->base + PREDIV);
315 if (prediv & PLLDIV_EN)
316 prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
321 /* pre-divider is fixed, but (some?) chips won't report that */
322 if (cpu_is_davinci_dm355() && pll->num == 1)
325 if (pll->flags & PLL_HAS_POSTDIV) {
326 postdiv = __raw_readl(pll->base + POSTDIV);
327 if (postdiv & PLLDIV_EN)
328 postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
339 pr_debug("PLL%d: input = %lu MHz [ ",
340 pll->num, clk->parent->rate / 1000000);
344 pr_debug("/ %d ", prediv);
346 pr_debug("* %d ", mult);
348 pr_debug("/ %d ", postdiv);
349 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
355 * davinci_set_pllrate - set the output rate of a given PLL.
357 * Note: Currently tested to work with OMAP-L138 only.
359 * @pll: pll whose rate needs to be changed.
360 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
361 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
362 * @postdiv: The post divider value. Passing 0 disables the post-divider.
364 int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
365 unsigned int mult, unsigned int postdiv)
368 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;
385 postdiv = (postdiv - 1) | PLLDIV_EN;
389 ctrl = __raw_readl(pll->base + PLLCTL);
391 /* Switch the PLL to bypass mode */
392 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
393 __raw_writel(ctrl, pll->base + PLLCTL);
396 * Wait for 4 OSCIN/CLKIN cycles to ensure that the PLLC has switched
397 * to bypass mode. Delay of 1us ensures we are good for all > 4MHz
398 * OSCIN/CLKIN inputs. Typically the input is ~25MHz.
402 /* Reset and enable PLL */
403 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
404 __raw_writel(ctrl, pll->base + PLLCTL);
406 if (pll->flags & PLL_HAS_PREDIV)
407 __raw_writel(prediv, pll->base + PREDIV);
409 __raw_writel(mult, pll->base + PLLM);
411 if (pll->flags & PLL_HAS_POSTDIV)
412 __raw_writel(postdiv, pll->base + POSTDIV);
415 * Wait for PLL to reset properly, OMAP-L138 datasheet says
420 /* Bring PLL out of reset */
421 ctrl |= PLLCTL_PLLRST;
422 __raw_writel(ctrl, pll->base + PLLCTL);
426 /* Remove PLL from bypass mode */
427 ctrl |= PLLCTL_PLLEN;
428 __raw_writel(ctrl, pll->base + PLLCTL);
432 EXPORT_SYMBOL(davinci_set_pllrate);
434 int __init davinci_clk_init(struct davinci_clk *clocks)
436 struct davinci_clk *c;
439 for (c = clocks; c->lk.clk; c++) {
444 /* Check if clock is a PLL */
446 clk->recalc = clk_pllclk_recalc;
448 /* Else, if it is a PLL-derived clock */
449 else if (clk->flags & CLK_PLL)
450 clk->recalc = clk_sysclk_recalc;
452 /* Otherwise, it is a leaf clock (PSC clock) */
453 else if (clk->parent)
454 clk->recalc = clk_leafclk_recalc;
458 clk->rate = clk->recalc(clk);
461 clk->flags |= CLK_PSC;
466 /* Turn on clocks that Linux doesn't otherwise manage */
467 if (clk->flags & ALWAYS_ENABLED)
474 #ifdef CONFIG_PROC_FS
475 #include <linux/proc_fs.h>
476 #include <linux/seq_file.h>
478 static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
480 return *pos < 1 ? (void *)1 : NULL;
483 static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
489 static void davinci_ck_stop(struct seq_file *m, void *v)
493 #define CLKNAME_MAX 10 /* longest clock name */
498 dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
501 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
505 if (parent->flags & CLK_PLL)
507 else if (parent->flags & CLK_PSC)
512 /* <nest spaces> name <pad to end> */
513 memset(buf, ' ', sizeof(buf) - 1);
514 buf[sizeof(buf) - 1] = 0;
515 i = strlen(parent->name);
516 memcpy(buf + nest, parent->name,
517 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
519 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
520 buf, parent->usecount, state, clk_get_rate(parent));
521 /* REVISIT show device associations too */
523 /* cost is now small, but not linear... */
524 list_for_each_entry(clk, &parent->children, childnode) {
525 dump_clock(s, nest + NEST_DELTA, clk);
529 static int davinci_ck_show(struct seq_file *m, void *v)
531 /* Show clock tree; we know the main oscillator is first.
532 * We trust nonzero usecounts equate to PSC enables...
534 mutex_lock(&clocks_mutex);
535 if (!list_empty(&clocks))
536 dump_clock(m, 0, list_first_entry(&clocks, struct clk, node));
537 mutex_unlock(&clocks_mutex);
542 static const struct seq_operations davinci_ck_op = {
543 .start = davinci_ck_start,
544 .next = davinci_ck_next,
545 .stop = davinci_ck_stop,
546 .show = davinci_ck_show
549 static int davinci_ck_open(struct inode *inode, struct file *file)
551 return seq_open(file, &davinci_ck_op);
554 static const struct file_operations proc_davinci_ck_operations = {
555 .open = davinci_ck_open,
558 .release = seq_release,
561 static int __init davinci_ck_proc_init(void)
563 proc_create("davinci_clocks", 0, NULL, &proc_davinci_ck_operations);
567 __initcall(davinci_ck_proc_init);
568 #endif /* CONFIG_DEBUG_PROC_FS */