blob: 00861139101dfad13806f6a9af3e8c3acffd39ed [file] [log] [blame]
Vladimir Barinov3e062b02007-06-05 16:36:55 +01001/*
Kevin Hilmanc5b736d2009-03-20 17:29:01 -07002 * Clock and PLL control for DaVinci devices
Vladimir Barinov3e062b02007-06-05 16:36:55 +01003 *
Kevin Hilmanc5b736d2009-03-20 17:29:01 -07004 * Copyright (C) 2006-2007 Texas Instruments.
5 * Copyright (C) 2008-2009 Deep Root Systems, LLC
Vladimir Barinov3e062b02007-06-05 16:36:55 +01006 *
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.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/errno.h>
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070017#include <linux/clk.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010018#include <linux/err.h>
19#include <linux/mutex.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Sekhar Norid6a61562009-08-31 15:48:03 +053021#include <linux/delay.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010022
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010024
Kevin Hilman28552c22010-02-25 15:36:38 -080025#include <mach/clock.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/psc.h>
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070027#include <mach/cputype.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010028#include "clock.h"
29
Vladimir Barinov3e062b02007-06-05 16:36:55 +010030static LIST_HEAD(clocks);
31static DEFINE_MUTEX(clocks_mutex);
32static DEFINE_SPINLOCK(clockfw_lock);
33
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070034static unsigned psc_domain(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010035{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070036 return (clk->flags & PSC_DSP)
37 ? DAVINCI_GPSC_DSPDOMAIN
38 : DAVINCI_GPSC_ARMDOMAIN;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010039}
Vladimir Barinov3e062b02007-06-05 16:36:55 +010040
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070041static void __clk_enable(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010042{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070043 if (clk->parent)
44 __clk_enable(clk->parent);
45 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
Cyril Chemparathy52958be2010-03-25 17:43:47 -040046 davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +000047 true, clk->flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010048}
49
50static void __clk_disable(struct clk *clk)
51{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070052 if (WARN_ON(clk->usecount == 0))
Vladimir Barinov3e062b02007-06-05 16:36:55 +010053 return;
Chaithrika U S679f9212009-12-15 18:02:58 +053054 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) &&
55 (clk->flags & CLK_PSC))
Cyril Chemparathy52958be2010-03-25 17:43:47 -040056 davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +000057 false, clk->flags);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070058 if (clk->parent)
59 __clk_disable(clk->parent);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010060}
61
62int clk_enable(struct clk *clk)
63{
64 unsigned long flags;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010065
66 if (clk == NULL || IS_ERR(clk))
67 return -EINVAL;
68
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070069 spin_lock_irqsave(&clockfw_lock, flags);
70 __clk_enable(clk);
71 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010072
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070073 return 0;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010074}
75EXPORT_SYMBOL(clk_enable);
76
77void clk_disable(struct clk *clk)
78{
79 unsigned long flags;
80
81 if (clk == NULL || IS_ERR(clk))
82 return;
83
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070084 spin_lock_irqsave(&clockfw_lock, flags);
85 __clk_disable(clk);
86 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010087}
88EXPORT_SYMBOL(clk_disable);
89
90unsigned long clk_get_rate(struct clk *clk)
91{
92 if (clk == NULL || IS_ERR(clk))
93 return -EINVAL;
94
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070095 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010096}
97EXPORT_SYMBOL(clk_get_rate);
98
99long clk_round_rate(struct clk *clk, unsigned long rate)
100{
101 if (clk == NULL || IS_ERR(clk))
102 return -EINVAL;
103
Sekhar Norid6a61562009-08-31 15:48:03 +0530104 if (clk->round_rate)
105 return clk->round_rate(clk, rate);
106
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700107 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100108}
109EXPORT_SYMBOL(clk_round_rate);
110
Sekhar Norid6a61562009-08-31 15:48:03 +0530111/* Propagate rate to children */
112static void propagate_rate(struct clk *root)
113{
114 struct clk *clk;
115
116 list_for_each_entry(clk, &root->children, childnode) {
117 if (clk->recalc)
118 clk->rate = clk->recalc(clk);
119 propagate_rate(clk);
120 }
121}
122
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100123int clk_set_rate(struct clk *clk, unsigned long rate)
124{
Sekhar Norid6a61562009-08-31 15:48:03 +0530125 unsigned long flags;
126 int ret = -EINVAL;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100127
Sekhar Norid6a61562009-08-31 15:48:03 +0530128 if (clk == NULL || IS_ERR(clk))
129 return ret;
130
Sekhar Norid6a61562009-08-31 15:48:03 +0530131 if (clk->set_rate)
132 ret = clk->set_rate(clk, rate);
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530133
134 spin_lock_irqsave(&clockfw_lock, flags);
Sekhar Norid6a61562009-08-31 15:48:03 +0530135 if (ret == 0) {
136 if (clk->recalc)
137 clk->rate = clk->recalc(clk);
138 propagate_rate(clk);
139 }
140 spin_unlock_irqrestore(&clockfw_lock, flags);
141
142 return ret;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100143}
144EXPORT_SYMBOL(clk_set_rate);
145
Sekhar Norib82a51e2009-08-31 15:48:04 +0530146int clk_set_parent(struct clk *clk, struct clk *parent)
147{
148 unsigned long flags;
149
150 if (clk == NULL || IS_ERR(clk))
151 return -EINVAL;
152
153 /* Cannot change parent on enabled clock */
154 if (WARN_ON(clk->usecount))
155 return -EINVAL;
156
157 mutex_lock(&clocks_mutex);
158 clk->parent = parent;
159 list_del_init(&clk->childnode);
160 list_add(&clk->childnode, &clk->parent->children);
161 mutex_unlock(&clocks_mutex);
162
163 spin_lock_irqsave(&clockfw_lock, flags);
164 if (clk->recalc)
165 clk->rate = clk->recalc(clk);
166 propagate_rate(clk);
167 spin_unlock_irqrestore(&clockfw_lock, flags);
168
169 return 0;
170}
171EXPORT_SYMBOL(clk_set_parent);
172
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100173int clk_register(struct clk *clk)
174{
175 if (clk == NULL || IS_ERR(clk))
176 return -EINVAL;
177
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700178 if (WARN(clk->parent && !clk->parent->rate,
179 "CLK: %s parent %s has no rate!\n",
180 clk->name, clk->parent->name))
181 return -EINVAL;
182
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530183 INIT_LIST_HEAD(&clk->children);
184
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100185 mutex_lock(&clocks_mutex);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700186 list_add_tail(&clk->node, &clocks);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530187 if (clk->parent)
188 list_add_tail(&clk->childnode, &clk->parent->children);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100189 mutex_unlock(&clocks_mutex);
190
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700191 /* If rate is already set, use it */
192 if (clk->rate)
193 return 0;
194
Sekhar Noride381a92009-08-31 15:48:02 +0530195 /* Else, see if there is a way to calculate it */
196 if (clk->recalc)
197 clk->rate = clk->recalc(clk);
198
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700199 /* Otherwise, default to parent rate */
Sekhar Noride381a92009-08-31 15:48:02 +0530200 else if (clk->parent)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700201 clk->rate = clk->parent->rate;
202
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100203 return 0;
204}
205EXPORT_SYMBOL(clk_register);
206
207void clk_unregister(struct clk *clk)
208{
209 if (clk == NULL || IS_ERR(clk))
210 return;
211
212 mutex_lock(&clocks_mutex);
213 list_del(&clk->node);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530214 list_del(&clk->childnode);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100215 mutex_unlock(&clocks_mutex);
216}
217EXPORT_SYMBOL(clk_unregister);
218
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700219#ifdef CONFIG_DAVINCI_RESET_CLOCKS
220/*
221 * Disable any unused clocks left on by the bootloader
222 */
223static int __init clk_disable_unused(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100224{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700225 struct clk *ck;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100226
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700227 spin_lock_irq(&clockfw_lock);
228 list_for_each_entry(ck, &clocks, node) {
229 if (ck->usecount > 0)
230 continue;
231 if (!(ck->flags & CLK_PSC))
232 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100233
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700234 /* ignore if in Disabled or SwRstDisable states */
Sergei Shtylyov789a7852009-09-30 19:48:03 +0400235 if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700236 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100237
Kevin Hilmanc89f1682010-08-05 10:55:16 -0700238 pr_debug("Clocks: disable unused %s\n", ck->name);
Cyril Chemparathy52958be2010-03-25 17:43:47 -0400239
240 davinci_psc_config(psc_domain(ck), ck->gpsc, ck->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +0000241 false, ck->flags);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700242 }
243 spin_unlock_irq(&clockfw_lock);
244
245 return 0;
246}
247late_initcall(clk_disable_unused);
248#endif
249
Sekhar Noride381a92009-08-31 15:48:02 +0530250static unsigned long clk_sysclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700251{
252 u32 v, plldiv;
253 struct pll_data *pll;
Sekhar Noride381a92009-08-31 15:48:02 +0530254 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700255
256 /* If this is the PLL base clock, no more calculations needed */
257 if (clk->pll_data)
Sekhar Noride381a92009-08-31 15:48:02 +0530258 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700259
260 if (WARN_ON(!clk->parent))
Sekhar Noride381a92009-08-31 15:48:02 +0530261 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700262
Sekhar Noride381a92009-08-31 15:48:02 +0530263 rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700264
265 /* Otherwise, the parent must be a PLL */
266 if (WARN_ON(!clk->parent->pll_data))
Sekhar Noride381a92009-08-31 15:48:02 +0530267 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700268
269 pll = clk->parent->pll_data;
270
271 /* If pre-PLL, source clock is before the multiplier and divider(s) */
272 if (clk->flags & PRE_PLL)
Sekhar Noride381a92009-08-31 15:48:02 +0530273 rate = pll->input_rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700274
275 if (!clk->div_reg)
Sekhar Noride381a92009-08-31 15:48:02 +0530276 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700277
278 v = __raw_readl(pll->base + clk->div_reg);
279 if (v & PLLDIV_EN) {
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400280 plldiv = (v & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700281 if (plldiv)
Sekhar Noride381a92009-08-31 15:48:02 +0530282 rate /= plldiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700283 }
Sekhar Noride381a92009-08-31 15:48:02 +0530284
285 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700286}
287
Sekhar Norib39639b2010-07-20 16:46:49 +0530288int davinci_set_sysclk_rate(struct clk *clk, unsigned long rate)
289{
290 unsigned v;
291 struct pll_data *pll;
292 unsigned long input;
293 unsigned ratio = 0;
294
295 /* If this is the PLL base clock, wrong function to call */
296 if (clk->pll_data)
297 return -EINVAL;
298
299 /* There must be a parent... */
300 if (WARN_ON(!clk->parent))
301 return -EINVAL;
302
303 /* ... the parent must be a PLL... */
304 if (WARN_ON(!clk->parent->pll_data))
305 return -EINVAL;
306
307 /* ... and this clock must have a divider. */
308 if (WARN_ON(!clk->div_reg))
309 return -EINVAL;
310
311 pll = clk->parent->pll_data;
312
313 input = clk->parent->rate;
314
315 /* If pre-PLL, source clock is before the multiplier and divider(s) */
316 if (clk->flags & PRE_PLL)
317 input = pll->input_rate;
318
319 if (input > rate) {
320 /*
321 * Can afford to provide an output little higher than requested
322 * only if maximum rate supported by hardware on this sysclk
323 * is known.
324 */
325 if (clk->maxrate) {
326 ratio = DIV_ROUND_CLOSEST(input, rate);
327 if (input / ratio > clk->maxrate)
328 ratio = 0;
329 }
330
331 if (ratio == 0)
332 ratio = DIV_ROUND_UP(input, rate);
333
334 ratio--;
335 }
336
Cyril Chemparathyb1d05be2010-10-20 17:49:56 -0400337 if (ratio > pll->div_ratio_mask)
Sekhar Norib39639b2010-07-20 16:46:49 +0530338 return -EINVAL;
339
340 do {
341 v = __raw_readl(pll->base + PLLSTAT);
342 } while (v & PLLSTAT_GOSTAT);
343
344 v = __raw_readl(pll->base + clk->div_reg);
Cyril Chemparathyb1d05be2010-10-20 17:49:56 -0400345 v &= ~pll->div_ratio_mask;
Sekhar Norib39639b2010-07-20 16:46:49 +0530346 v |= ratio | PLLDIV_EN;
347 __raw_writel(v, pll->base + clk->div_reg);
348
349 v = __raw_readl(pll->base + PLLCMD);
350 v |= PLLCMD_GOSET;
351 __raw_writel(v, pll->base + PLLCMD);
352
353 do {
354 v = __raw_readl(pll->base + PLLSTAT);
355 } while (v & PLLSTAT_GOSTAT);
356
357 return 0;
358}
359EXPORT_SYMBOL(davinci_set_sysclk_rate);
360
Sekhar Noride381a92009-08-31 15:48:02 +0530361static unsigned long clk_leafclk_recalc(struct clk *clk)
362{
363 if (WARN_ON(!clk->parent))
364 return clk->rate;
365
366 return clk->parent->rate;
367}
368
Sekhar Nori56e580d2011-06-14 15:33:20 +0000369int davinci_simple_set_rate(struct clk *clk, unsigned long rate)
370{
371 clk->rate = rate;
372 return 0;
373}
374
Sekhar Noride381a92009-08-31 15:48:02 +0530375static unsigned long clk_pllclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700376{
377 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
378 u8 bypass;
379 struct pll_data *pll = clk->pll_data;
Sekhar Noride381a92009-08-31 15:48:02 +0530380 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700381
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700382 ctrl = __raw_readl(pll->base + PLLCTL);
Sekhar Noride381a92009-08-31 15:48:02 +0530383 rate = pll->input_rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700384
385 if (ctrl & PLLCTL_PLLEN) {
386 bypass = 0;
387 mult = __raw_readl(pll->base + PLLM);
Sandeep Paulrajfb8fcb82009-06-11 09:41:05 -0400388 if (cpu_is_davinci_dm365())
389 mult = 2 * (mult & PLLM_PLLM_MASK);
390 else
391 mult = (mult & PLLM_PLLM_MASK) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700392 } else
393 bypass = 1;
394
395 if (pll->flags & PLL_HAS_PREDIV) {
396 prediv = __raw_readl(pll->base + PREDIV);
397 if (prediv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400398 prediv = (prediv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700399 else
400 prediv = 1;
401 }
402
403 /* pre-divider is fixed, but (some?) chips won't report that */
404 if (cpu_is_davinci_dm355() && pll->num == 1)
405 prediv = 8;
406
407 if (pll->flags & PLL_HAS_POSTDIV) {
408 postdiv = __raw_readl(pll->base + POSTDIV);
409 if (postdiv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400410 postdiv = (postdiv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700411 else
412 postdiv = 1;
413 }
414
415 if (!bypass) {
Sekhar Noride381a92009-08-31 15:48:02 +0530416 rate /= prediv;
417 rate *= mult;
418 rate /= postdiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700419 }
420
421 pr_debug("PLL%d: input = %lu MHz [ ",
422 pll->num, clk->parent->rate / 1000000);
423 if (bypass)
424 pr_debug("bypass ");
425 if (prediv > 1)
426 pr_debug("/ %d ", prediv);
427 if (mult > 1)
428 pr_debug("* %d ", mult);
429 if (postdiv > 1)
430 pr_debug("/ %d ", postdiv);
Sekhar Noride381a92009-08-31 15:48:02 +0530431 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
432
433 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700434}
435
Sekhar Norid6a61562009-08-31 15:48:03 +0530436/**
437 * davinci_set_pllrate - set the output rate of a given PLL.
438 *
439 * Note: Currently tested to work with OMAP-L138 only.
440 *
441 * @pll: pll whose rate needs to be changed.
442 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
443 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
444 * @postdiv: The post divider value. Passing 0 disables the post-divider.
445 */
446int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
447 unsigned int mult, unsigned int postdiv)
448{
449 u32 ctrl;
450 unsigned int locktime;
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530451 unsigned long flags;
Sekhar Norid6a61562009-08-31 15:48:03 +0530452
453 if (pll->base == NULL)
454 return -EINVAL;
455
456 /*
457 * PLL lock time required per OMAP-L138 datasheet is
458 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
459 * as 4 and OSCIN cycle as 25 MHz.
460 */
461 if (prediv) {
462 locktime = ((2000 * prediv) / 100);
463 prediv = (prediv - 1) | PLLDIV_EN;
464 } else {
Sekhar Nori9a219a92009-11-16 17:21:33 +0530465 locktime = PLL_LOCK_TIME;
Sekhar Norid6a61562009-08-31 15:48:03 +0530466 }
467 if (postdiv)
468 postdiv = (postdiv - 1) | PLLDIV_EN;
469 if (mult)
470 mult = mult - 1;
471
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530472 /* Protect against simultaneous calls to PLL setting seqeunce */
473 spin_lock_irqsave(&clockfw_lock, flags);
474
Sekhar Norid6a61562009-08-31 15:48:03 +0530475 ctrl = __raw_readl(pll->base + PLLCTL);
476
477 /* Switch the PLL to bypass mode */
478 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
479 __raw_writel(ctrl, pll->base + PLLCTL);
480
Sekhar Nori9a219a92009-11-16 17:21:33 +0530481 udelay(PLL_BYPASS_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530482
483 /* Reset and enable PLL */
484 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
485 __raw_writel(ctrl, pll->base + PLLCTL);
486
487 if (pll->flags & PLL_HAS_PREDIV)
488 __raw_writel(prediv, pll->base + PREDIV);
489
490 __raw_writel(mult, pll->base + PLLM);
491
492 if (pll->flags & PLL_HAS_POSTDIV)
493 __raw_writel(postdiv, pll->base + POSTDIV);
494
Sekhar Nori9a219a92009-11-16 17:21:33 +0530495 udelay(PLL_RESET_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530496
497 /* Bring PLL out of reset */
498 ctrl |= PLLCTL_PLLRST;
499 __raw_writel(ctrl, pll->base + PLLCTL);
500
501 udelay(locktime);
502
503 /* Remove PLL from bypass mode */
504 ctrl |= PLLCTL_PLLEN;
505 __raw_writel(ctrl, pll->base + PLLCTL);
506
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530507 spin_unlock_irqrestore(&clockfw_lock, flags);
508
Sekhar Norid6a61562009-08-31 15:48:03 +0530509 return 0;
510}
511EXPORT_SYMBOL(davinci_set_pllrate);
512
Sekhar Nori56e580d2011-06-14 15:33:20 +0000513/**
514 * davinci_set_refclk_rate() - Set the reference clock rate
515 * @rate: The new rate.
516 *
517 * Sets the reference clock rate to a given value. This will most likely
518 * result in the entire clock tree getting updated.
519 *
520 * This is used to support boards which use a reference clock different
521 * than that used by default in <soc>.c file. The reference clock rate
522 * should be updated early in the boot process; ideally soon after the
523 * clock tree has been initialized once with the default reference clock
524 * rate (davinci_common_init()).
525 *
526 * Returns 0 on success, error otherwise.
527 */
528int davinci_set_refclk_rate(unsigned long rate)
529{
530 struct clk *refclk;
531
532 refclk = clk_get(NULL, "ref");
533 if (IS_ERR(refclk)) {
534 pr_err("%s: failed to get reference clock.\n", __func__);
535 return PTR_ERR(refclk);
536 }
537
538 clk_set_rate(refclk, rate);
539
540 clk_put(refclk);
541
542 return 0;
543}
544
Kevin Hilman08aca082010-01-11 08:22:23 -0800545int __init davinci_clk_init(struct clk_lookup *clocks)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700546 {
Kevin Hilman08aca082010-01-11 08:22:23 -0800547 struct clk_lookup *c;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700548 struct clk *clk;
Kevin Hilman08aca082010-01-11 08:22:23 -0800549 size_t num_clocks = 0;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700550
Kevin Hilman08aca082010-01-11 08:22:23 -0800551 for (c = clocks; c->clk; c++) {
552 clk = c->clk;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700553
Sekhar Noride381a92009-08-31 15:48:02 +0530554 if (!clk->recalc) {
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700555
Sekhar Noride381a92009-08-31 15:48:02 +0530556 /* Check if clock is a PLL */
557 if (clk->pll_data)
558 clk->recalc = clk_pllclk_recalc;
559
560 /* Else, if it is a PLL-derived clock */
561 else if (clk->flags & CLK_PLL)
562 clk->recalc = clk_sysclk_recalc;
563
564 /* Otherwise, it is a leaf clock (PSC clock) */
565 else if (clk->parent)
566 clk->recalc = clk_leafclk_recalc;
567 }
568
Cyril Chemparathye4c822c2010-05-07 17:06:36 -0400569 if (clk->pll_data) {
570 struct pll_data *pll = clk->pll_data;
571
572 if (!pll->div_ratio_mask)
573 pll->div_ratio_mask = PLLDIV_RATIO_MASK;
574
575 if (pll->phys_base && !pll->base) {
576 pll->base = ioremap(pll->phys_base, SZ_4K);
577 WARN_ON(!pll->base);
578 }
579 }
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400580
Sekhar Noride381a92009-08-31 15:48:02 +0530581 if (clk->recalc)
582 clk->rate = clk->recalc(clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700583
584 if (clk->lpsc)
585 clk->flags |= CLK_PSC;
586
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700587 clk_register(clk);
Kevin Hilman08aca082010-01-11 08:22:23 -0800588 num_clocks++;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700589
590 /* Turn on clocks that Linux doesn't otherwise manage */
591 if (clk->flags & ALWAYS_ENABLED)
592 clk_enable(clk);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100593 }
594
Kevin Hilman08aca082010-01-11 08:22:23 -0800595 clkdev_add_table(clocks, num_clocks);
596
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100597 return 0;
598}
599
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530600#ifdef CONFIG_DEBUG_FS
601
602#include <linux/debugfs.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100603#include <linux/seq_file.h>
604
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700605#define CLKNAME_MAX 10 /* longest clock name */
606#define NEST_DELTA 2
607#define NEST_MAX 4
608
609static void
610dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
611{
612 char *state;
613 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
614 struct clk *clk;
615 unsigned i;
616
617 if (parent->flags & CLK_PLL)
618 state = "pll";
619 else if (parent->flags & CLK_PSC)
620 state = "psc";
621 else
622 state = "";
623
624 /* <nest spaces> name <pad to end> */
625 memset(buf, ' ', sizeof(buf) - 1);
626 buf[sizeof(buf) - 1] = 0;
627 i = strlen(parent->name);
628 memcpy(buf + nest, parent->name,
629 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
630
631 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
632 buf, parent->usecount, state, clk_get_rate(parent));
633 /* REVISIT show device associations too */
634
635 /* cost is now small, but not linear... */
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530636 list_for_each_entry(clk, &parent->children, childnode) {
637 dump_clock(s, nest + NEST_DELTA, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700638 }
639}
640
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100641static int davinci_ck_show(struct seq_file *m, void *v)
642{
Sekhar Norif979aa62009-12-03 15:36:51 +0530643 struct clk *clk;
644
645 /*
646 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700647 */
648 mutex_lock(&clocks_mutex);
Sekhar Norif979aa62009-12-03 15:36:51 +0530649 list_for_each_entry(clk, &clocks, node)
650 if (!clk->parent)
651 dump_clock(m, 0, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700652 mutex_unlock(&clocks_mutex);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100653
654 return 0;
655}
656
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100657static int davinci_ck_open(struct inode *inode, struct file *file)
658{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530659 return single_open(file, davinci_ck_show, NULL);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100660}
661
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530662static const struct file_operations davinci_ck_operations = {
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100663 .open = davinci_ck_open,
664 .read = seq_read,
665 .llseek = seq_lseek,
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530666 .release = single_release,
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100667};
668
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530669static int __init davinci_clk_debugfs_init(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100670{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530671 debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
672 &davinci_ck_operations);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100673 return 0;
674
675}
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530676device_initcall(davinci_clk_debugfs_init);
677#endif /* CONFIG_DEBUG_FS */