blob: d458558ee84a436a0506835c3ca9bdfa722798de [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 void __clk_enable(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010035{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070036 if (clk->parent)
37 __clk_enable(clk->parent);
38 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
Murali Karicheri12221d42011-11-15 01:42:09 +053039 davinci_psc_config(clk->domain, clk->gpsc, clk->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +000040 true, clk->flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010041}
42
43static void __clk_disable(struct clk *clk)
44{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070045 if (WARN_ON(clk->usecount == 0))
Vladimir Barinov3e062b02007-06-05 16:36:55 +010046 return;
Chaithrika U S679f9212009-12-15 18:02:58 +053047 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) &&
48 (clk->flags & CLK_PSC))
Murali Karicheri12221d42011-11-15 01:42:09 +053049 davinci_psc_config(clk->domain, clk->gpsc, clk->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +000050 false, clk->flags);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070051 if (clk->parent)
52 __clk_disable(clk->parent);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010053}
54
Robert Tivyaf47e6b2013-01-10 16:23:23 -080055int davinci_clk_reset(struct clk *clk, bool reset)
56{
57 unsigned long flags;
58
59 if (clk == NULL || IS_ERR(clk))
60 return -EINVAL;
61
62 spin_lock_irqsave(&clockfw_lock, flags);
63 if (clk->flags & CLK_PSC)
64 davinci_psc_reset(clk->gpsc, clk->lpsc, reset);
65 spin_unlock_irqrestore(&clockfw_lock, flags);
66
67 return 0;
68}
69EXPORT_SYMBOL(davinci_clk_reset);
70
71int davinci_clk_reset_assert(struct clk *clk)
72{
73 if (clk == NULL || IS_ERR(clk) || !clk->reset)
74 return -EINVAL;
75
76 return clk->reset(clk, true);
77}
78EXPORT_SYMBOL(davinci_clk_reset_assert);
79
80int davinci_clk_reset_deassert(struct clk *clk)
81{
82 if (clk == NULL || IS_ERR(clk) || !clk->reset)
83 return -EINVAL;
84
85 return clk->reset(clk, false);
86}
87EXPORT_SYMBOL(davinci_clk_reset_deassert);
88
Vladimir Barinov3e062b02007-06-05 16:36:55 +010089int clk_enable(struct clk *clk)
90{
91 unsigned long flags;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010092
93 if (clk == NULL || IS_ERR(clk))
94 return -EINVAL;
95
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070096 spin_lock_irqsave(&clockfw_lock, flags);
97 __clk_enable(clk);
98 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010099
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700100 return 0;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100101}
102EXPORT_SYMBOL(clk_enable);
103
104void clk_disable(struct clk *clk)
105{
106 unsigned long flags;
107
108 if (clk == NULL || IS_ERR(clk))
109 return;
110
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700111 spin_lock_irqsave(&clockfw_lock, flags);
112 __clk_disable(clk);
113 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100114}
115EXPORT_SYMBOL(clk_disable);
116
117unsigned long clk_get_rate(struct clk *clk)
118{
119 if (clk == NULL || IS_ERR(clk))
120 return -EINVAL;
121
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700122 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100123}
124EXPORT_SYMBOL(clk_get_rate);
125
126long clk_round_rate(struct clk *clk, unsigned long rate)
127{
128 if (clk == NULL || IS_ERR(clk))
129 return -EINVAL;
130
Sekhar Norid6a61562009-08-31 15:48:03 +0530131 if (clk->round_rate)
132 return clk->round_rate(clk, rate);
133
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700134 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100135}
136EXPORT_SYMBOL(clk_round_rate);
137
Sekhar Norid6a61562009-08-31 15:48:03 +0530138/* Propagate rate to children */
139static void propagate_rate(struct clk *root)
140{
141 struct clk *clk;
142
143 list_for_each_entry(clk, &root->children, childnode) {
144 if (clk->recalc)
145 clk->rate = clk->recalc(clk);
146 propagate_rate(clk);
147 }
148}
149
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100150int clk_set_rate(struct clk *clk, unsigned long rate)
151{
Sekhar Norid6a61562009-08-31 15:48:03 +0530152 unsigned long flags;
153 int ret = -EINVAL;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100154
Sekhar Norid6a61562009-08-31 15:48:03 +0530155 if (clk == NULL || IS_ERR(clk))
156 return ret;
157
Sekhar Norid6a61562009-08-31 15:48:03 +0530158 if (clk->set_rate)
159 ret = clk->set_rate(clk, rate);
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530160
161 spin_lock_irqsave(&clockfw_lock, flags);
Sekhar Norid6a61562009-08-31 15:48:03 +0530162 if (ret == 0) {
163 if (clk->recalc)
164 clk->rate = clk->recalc(clk);
165 propagate_rate(clk);
166 }
167 spin_unlock_irqrestore(&clockfw_lock, flags);
168
169 return ret;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100170}
171EXPORT_SYMBOL(clk_set_rate);
172
Sekhar Norib82a51e2009-08-31 15:48:04 +0530173int clk_set_parent(struct clk *clk, struct clk *parent)
174{
175 unsigned long flags;
176
177 if (clk == NULL || IS_ERR(clk))
178 return -EINVAL;
179
180 /* Cannot change parent on enabled clock */
181 if (WARN_ON(clk->usecount))
182 return -EINVAL;
183
184 mutex_lock(&clocks_mutex);
185 clk->parent = parent;
186 list_del_init(&clk->childnode);
187 list_add(&clk->childnode, &clk->parent->children);
188 mutex_unlock(&clocks_mutex);
189
190 spin_lock_irqsave(&clockfw_lock, flags);
191 if (clk->recalc)
192 clk->rate = clk->recalc(clk);
193 propagate_rate(clk);
194 spin_unlock_irqrestore(&clockfw_lock, flags);
195
196 return 0;
197}
198EXPORT_SYMBOL(clk_set_parent);
199
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100200int clk_register(struct clk *clk)
201{
202 if (clk == NULL || IS_ERR(clk))
203 return -EINVAL;
204
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700205 if (WARN(clk->parent && !clk->parent->rate,
206 "CLK: %s parent %s has no rate!\n",
207 clk->name, clk->parent->name))
208 return -EINVAL;
209
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530210 INIT_LIST_HEAD(&clk->children);
211
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100212 mutex_lock(&clocks_mutex);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700213 list_add_tail(&clk->node, &clocks);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530214 if (clk->parent)
215 list_add_tail(&clk->childnode, &clk->parent->children);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100216 mutex_unlock(&clocks_mutex);
217
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700218 /* If rate is already set, use it */
219 if (clk->rate)
220 return 0;
221
Sekhar Noride381a92009-08-31 15:48:02 +0530222 /* Else, see if there is a way to calculate it */
223 if (clk->recalc)
224 clk->rate = clk->recalc(clk);
225
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700226 /* Otherwise, default to parent rate */
Sekhar Noride381a92009-08-31 15:48:02 +0530227 else if (clk->parent)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700228 clk->rate = clk->parent->rate;
229
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100230 return 0;
231}
232EXPORT_SYMBOL(clk_register);
233
234void clk_unregister(struct clk *clk)
235{
236 if (clk == NULL || IS_ERR(clk))
237 return;
238
239 mutex_lock(&clocks_mutex);
240 list_del(&clk->node);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530241 list_del(&clk->childnode);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100242 mutex_unlock(&clocks_mutex);
243}
244EXPORT_SYMBOL(clk_unregister);
245
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700246#ifdef CONFIG_DAVINCI_RESET_CLOCKS
247/*
248 * Disable any unused clocks left on by the bootloader
249 */
Shawn Guo3aa3e842012-04-26 09:45:39 +0800250int __init davinci_clk_disable_unused(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100251{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700252 struct clk *ck;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100253
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700254 spin_lock_irq(&clockfw_lock);
255 list_for_each_entry(ck, &clocks, node) {
256 if (ck->usecount > 0)
257 continue;
258 if (!(ck->flags & CLK_PSC))
259 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100260
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700261 /* ignore if in Disabled or SwRstDisable states */
Sergei Shtylyov789a7852009-09-30 19:48:03 +0400262 if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700263 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100264
Kevin Hilmanc89f1682010-08-05 10:55:16 -0700265 pr_debug("Clocks: disable unused %s\n", ck->name);
Cyril Chemparathy52958be2010-03-25 17:43:47 -0400266
Murali Karicheri12221d42011-11-15 01:42:09 +0530267 davinci_psc_config(ck->domain, ck->gpsc, ck->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +0000268 false, ck->flags);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700269 }
270 spin_unlock_irq(&clockfw_lock);
271
272 return 0;
273}
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700274#endif
275
Sekhar Noride381a92009-08-31 15:48:02 +0530276static unsigned long clk_sysclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700277{
278 u32 v, plldiv;
279 struct pll_data *pll;
Sekhar Noride381a92009-08-31 15:48:02 +0530280 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700281
282 /* If this is the PLL base clock, no more calculations needed */
283 if (clk->pll_data)
Sekhar Noride381a92009-08-31 15:48:02 +0530284 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700285
286 if (WARN_ON(!clk->parent))
Sekhar Noride381a92009-08-31 15:48:02 +0530287 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700288
Sekhar Noride381a92009-08-31 15:48:02 +0530289 rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700290
291 /* Otherwise, the parent must be a PLL */
292 if (WARN_ON(!clk->parent->pll_data))
Sekhar Noride381a92009-08-31 15:48:02 +0530293 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700294
295 pll = clk->parent->pll_data;
296
297 /* If pre-PLL, source clock is before the multiplier and divider(s) */
298 if (clk->flags & PRE_PLL)
Sekhar Noride381a92009-08-31 15:48:02 +0530299 rate = pll->input_rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700300
301 if (!clk->div_reg)
Sekhar Noride381a92009-08-31 15:48:02 +0530302 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700303
304 v = __raw_readl(pll->base + clk->div_reg);
305 if (v & PLLDIV_EN) {
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400306 plldiv = (v & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700307 if (plldiv)
Sekhar Noride381a92009-08-31 15:48:02 +0530308 rate /= plldiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700309 }
Sekhar Noride381a92009-08-31 15:48:02 +0530310
311 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700312}
313
Sekhar Norib39639b2010-07-20 16:46:49 +0530314int davinci_set_sysclk_rate(struct clk *clk, unsigned long rate)
315{
316 unsigned v;
317 struct pll_data *pll;
318 unsigned long input;
319 unsigned ratio = 0;
320
321 /* If this is the PLL base clock, wrong function to call */
322 if (clk->pll_data)
323 return -EINVAL;
324
325 /* There must be a parent... */
326 if (WARN_ON(!clk->parent))
327 return -EINVAL;
328
329 /* ... the parent must be a PLL... */
330 if (WARN_ON(!clk->parent->pll_data))
331 return -EINVAL;
332
333 /* ... and this clock must have a divider. */
334 if (WARN_ON(!clk->div_reg))
335 return -EINVAL;
336
337 pll = clk->parent->pll_data;
338
339 input = clk->parent->rate;
340
341 /* If pre-PLL, source clock is before the multiplier and divider(s) */
342 if (clk->flags & PRE_PLL)
343 input = pll->input_rate;
344
345 if (input > rate) {
346 /*
347 * Can afford to provide an output little higher than requested
348 * only if maximum rate supported by hardware on this sysclk
349 * is known.
350 */
351 if (clk->maxrate) {
352 ratio = DIV_ROUND_CLOSEST(input, rate);
353 if (input / ratio > clk->maxrate)
354 ratio = 0;
355 }
356
357 if (ratio == 0)
358 ratio = DIV_ROUND_UP(input, rate);
359
360 ratio--;
361 }
362
Cyril Chemparathyb1d05be2010-10-20 17:49:56 -0400363 if (ratio > pll->div_ratio_mask)
Sekhar Norib39639b2010-07-20 16:46:49 +0530364 return -EINVAL;
365
366 do {
367 v = __raw_readl(pll->base + PLLSTAT);
368 } while (v & PLLSTAT_GOSTAT);
369
370 v = __raw_readl(pll->base + clk->div_reg);
Cyril Chemparathyb1d05be2010-10-20 17:49:56 -0400371 v &= ~pll->div_ratio_mask;
Sekhar Norib39639b2010-07-20 16:46:49 +0530372 v |= ratio | PLLDIV_EN;
373 __raw_writel(v, pll->base + clk->div_reg);
374
375 v = __raw_readl(pll->base + PLLCMD);
376 v |= PLLCMD_GOSET;
377 __raw_writel(v, pll->base + PLLCMD);
378
379 do {
380 v = __raw_readl(pll->base + PLLSTAT);
381 } while (v & PLLSTAT_GOSTAT);
382
383 return 0;
384}
385EXPORT_SYMBOL(davinci_set_sysclk_rate);
386
Sekhar Noride381a92009-08-31 15:48:02 +0530387static unsigned long clk_leafclk_recalc(struct clk *clk)
388{
389 if (WARN_ON(!clk->parent))
390 return clk->rate;
391
392 return clk->parent->rate;
393}
394
Sekhar Nori56e580d2011-06-14 15:33:20 +0000395int davinci_simple_set_rate(struct clk *clk, unsigned long rate)
396{
397 clk->rate = rate;
398 return 0;
399}
400
Sekhar Noride381a92009-08-31 15:48:02 +0530401static unsigned long clk_pllclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700402{
403 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
404 u8 bypass;
405 struct pll_data *pll = clk->pll_data;
Sekhar Noride381a92009-08-31 15:48:02 +0530406 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700407
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700408 ctrl = __raw_readl(pll->base + PLLCTL);
Sekhar Noride381a92009-08-31 15:48:02 +0530409 rate = pll->input_rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700410
411 if (ctrl & PLLCTL_PLLEN) {
412 bypass = 0;
413 mult = __raw_readl(pll->base + PLLM);
Sandeep Paulrajfb8fcb82009-06-11 09:41:05 -0400414 if (cpu_is_davinci_dm365())
415 mult = 2 * (mult & PLLM_PLLM_MASK);
416 else
417 mult = (mult & PLLM_PLLM_MASK) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700418 } else
419 bypass = 1;
420
421 if (pll->flags & PLL_HAS_PREDIV) {
422 prediv = __raw_readl(pll->base + PREDIV);
423 if (prediv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400424 prediv = (prediv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700425 else
426 prediv = 1;
427 }
428
429 /* pre-divider is fixed, but (some?) chips won't report that */
430 if (cpu_is_davinci_dm355() && pll->num == 1)
431 prediv = 8;
432
433 if (pll->flags & PLL_HAS_POSTDIV) {
434 postdiv = __raw_readl(pll->base + POSTDIV);
435 if (postdiv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400436 postdiv = (postdiv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700437 else
438 postdiv = 1;
439 }
440
441 if (!bypass) {
Sekhar Noride381a92009-08-31 15:48:02 +0530442 rate /= prediv;
443 rate *= mult;
444 rate /= postdiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700445 }
446
447 pr_debug("PLL%d: input = %lu MHz [ ",
448 pll->num, clk->parent->rate / 1000000);
449 if (bypass)
450 pr_debug("bypass ");
451 if (prediv > 1)
452 pr_debug("/ %d ", prediv);
453 if (mult > 1)
454 pr_debug("* %d ", mult);
455 if (postdiv > 1)
456 pr_debug("/ %d ", postdiv);
Sekhar Noride381a92009-08-31 15:48:02 +0530457 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
458
459 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700460}
461
Sekhar Norid6a61562009-08-31 15:48:03 +0530462/**
463 * davinci_set_pllrate - set the output rate of a given PLL.
464 *
465 * Note: Currently tested to work with OMAP-L138 only.
466 *
467 * @pll: pll whose rate needs to be changed.
468 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
469 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
470 * @postdiv: The post divider value. Passing 0 disables the post-divider.
471 */
472int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
473 unsigned int mult, unsigned int postdiv)
474{
475 u32 ctrl;
476 unsigned int locktime;
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530477 unsigned long flags;
Sekhar Norid6a61562009-08-31 15:48:03 +0530478
479 if (pll->base == NULL)
480 return -EINVAL;
481
482 /*
483 * PLL lock time required per OMAP-L138 datasheet is
484 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
485 * as 4 and OSCIN cycle as 25 MHz.
486 */
487 if (prediv) {
488 locktime = ((2000 * prediv) / 100);
489 prediv = (prediv - 1) | PLLDIV_EN;
490 } else {
Sekhar Nori9a219a92009-11-16 17:21:33 +0530491 locktime = PLL_LOCK_TIME;
Sekhar Norid6a61562009-08-31 15:48:03 +0530492 }
493 if (postdiv)
494 postdiv = (postdiv - 1) | PLLDIV_EN;
495 if (mult)
496 mult = mult - 1;
497
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530498 /* Protect against simultaneous calls to PLL setting seqeunce */
499 spin_lock_irqsave(&clockfw_lock, flags);
500
Sekhar Norid6a61562009-08-31 15:48:03 +0530501 ctrl = __raw_readl(pll->base + PLLCTL);
502
503 /* Switch the PLL to bypass mode */
504 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
505 __raw_writel(ctrl, pll->base + PLLCTL);
506
Sekhar Nori9a219a92009-11-16 17:21:33 +0530507 udelay(PLL_BYPASS_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530508
509 /* Reset and enable PLL */
510 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
511 __raw_writel(ctrl, pll->base + PLLCTL);
512
513 if (pll->flags & PLL_HAS_PREDIV)
514 __raw_writel(prediv, pll->base + PREDIV);
515
516 __raw_writel(mult, pll->base + PLLM);
517
518 if (pll->flags & PLL_HAS_POSTDIV)
519 __raw_writel(postdiv, pll->base + POSTDIV);
520
Sekhar Nori9a219a92009-11-16 17:21:33 +0530521 udelay(PLL_RESET_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530522
523 /* Bring PLL out of reset */
524 ctrl |= PLLCTL_PLLRST;
525 __raw_writel(ctrl, pll->base + PLLCTL);
526
527 udelay(locktime);
528
529 /* Remove PLL from bypass mode */
530 ctrl |= PLLCTL_PLLEN;
531 __raw_writel(ctrl, pll->base + PLLCTL);
532
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530533 spin_unlock_irqrestore(&clockfw_lock, flags);
534
Sekhar Norid6a61562009-08-31 15:48:03 +0530535 return 0;
536}
537EXPORT_SYMBOL(davinci_set_pllrate);
538
Sekhar Nori56e580d2011-06-14 15:33:20 +0000539/**
540 * davinci_set_refclk_rate() - Set the reference clock rate
541 * @rate: The new rate.
542 *
543 * Sets the reference clock rate to a given value. This will most likely
544 * result in the entire clock tree getting updated.
545 *
546 * This is used to support boards which use a reference clock different
547 * than that used by default in <soc>.c file. The reference clock rate
548 * should be updated early in the boot process; ideally soon after the
549 * clock tree has been initialized once with the default reference clock
550 * rate (davinci_common_init()).
551 *
552 * Returns 0 on success, error otherwise.
553 */
554int davinci_set_refclk_rate(unsigned long rate)
555{
556 struct clk *refclk;
557
558 refclk = clk_get(NULL, "ref");
559 if (IS_ERR(refclk)) {
560 pr_err("%s: failed to get reference clock.\n", __func__);
561 return PTR_ERR(refclk);
562 }
563
564 clk_set_rate(refclk, rate);
565
566 clk_put(refclk);
567
568 return 0;
569}
570
Kevin Hilman08aca082010-01-11 08:22:23 -0800571int __init davinci_clk_init(struct clk_lookup *clocks)
Robert Tivyaf47e6b2013-01-10 16:23:23 -0800572{
Kevin Hilman08aca082010-01-11 08:22:23 -0800573 struct clk_lookup *c;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700574 struct clk *clk;
Kevin Hilman08aca082010-01-11 08:22:23 -0800575 size_t num_clocks = 0;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700576
Kevin Hilman08aca082010-01-11 08:22:23 -0800577 for (c = clocks; c->clk; c++) {
578 clk = c->clk;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700579
Sekhar Noride381a92009-08-31 15:48:02 +0530580 if (!clk->recalc) {
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700581
Sekhar Noride381a92009-08-31 15:48:02 +0530582 /* Check if clock is a PLL */
583 if (clk->pll_data)
584 clk->recalc = clk_pllclk_recalc;
585
586 /* Else, if it is a PLL-derived clock */
587 else if (clk->flags & CLK_PLL)
588 clk->recalc = clk_sysclk_recalc;
589
590 /* Otherwise, it is a leaf clock (PSC clock) */
591 else if (clk->parent)
592 clk->recalc = clk_leafclk_recalc;
593 }
594
Cyril Chemparathye4c822c2010-05-07 17:06:36 -0400595 if (clk->pll_data) {
596 struct pll_data *pll = clk->pll_data;
597
598 if (!pll->div_ratio_mask)
599 pll->div_ratio_mask = PLLDIV_RATIO_MASK;
600
601 if (pll->phys_base && !pll->base) {
602 pll->base = ioremap(pll->phys_base, SZ_4K);
603 WARN_ON(!pll->base);
604 }
605 }
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400606
Sekhar Noride381a92009-08-31 15:48:02 +0530607 if (clk->recalc)
608 clk->rate = clk->recalc(clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700609
610 if (clk->lpsc)
611 clk->flags |= CLK_PSC;
612
Robert Tivyaf47e6b2013-01-10 16:23:23 -0800613 if (clk->flags & PSC_LRST)
614 clk->reset = davinci_clk_reset;
615
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700616 clk_register(clk);
Kevin Hilman08aca082010-01-11 08:22:23 -0800617 num_clocks++;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700618
619 /* Turn on clocks that Linux doesn't otherwise manage */
620 if (clk->flags & ALWAYS_ENABLED)
621 clk_enable(clk);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100622 }
623
Kevin Hilman08aca082010-01-11 08:22:23 -0800624 clkdev_add_table(clocks, num_clocks);
625
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100626 return 0;
627}
628
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530629#ifdef CONFIG_DEBUG_FS
630
631#include <linux/debugfs.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100632#include <linux/seq_file.h>
633
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700634#define CLKNAME_MAX 10 /* longest clock name */
635#define NEST_DELTA 2
636#define NEST_MAX 4
637
638static void
639dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
640{
641 char *state;
642 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
643 struct clk *clk;
644 unsigned i;
645
646 if (parent->flags & CLK_PLL)
647 state = "pll";
648 else if (parent->flags & CLK_PSC)
649 state = "psc";
650 else
651 state = "";
652
653 /* <nest spaces> name <pad to end> */
654 memset(buf, ' ', sizeof(buf) - 1);
655 buf[sizeof(buf) - 1] = 0;
656 i = strlen(parent->name);
657 memcpy(buf + nest, parent->name,
658 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
659
660 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
661 buf, parent->usecount, state, clk_get_rate(parent));
662 /* REVISIT show device associations too */
663
664 /* cost is now small, but not linear... */
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530665 list_for_each_entry(clk, &parent->children, childnode) {
666 dump_clock(s, nest + NEST_DELTA, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700667 }
668}
669
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100670static int davinci_ck_show(struct seq_file *m, void *v)
671{
Sekhar Norif979aa62009-12-03 15:36:51 +0530672 struct clk *clk;
673
674 /*
675 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700676 */
677 mutex_lock(&clocks_mutex);
Sekhar Norif979aa62009-12-03 15:36:51 +0530678 list_for_each_entry(clk, &clocks, node)
679 if (!clk->parent)
680 dump_clock(m, 0, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700681 mutex_unlock(&clocks_mutex);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100682
683 return 0;
684}
685
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100686static int davinci_ck_open(struct inode *inode, struct file *file)
687{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530688 return single_open(file, davinci_ck_show, NULL);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100689}
690
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530691static const struct file_operations davinci_ck_operations = {
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100692 .open = davinci_ck_open,
693 .read = seq_read,
694 .llseek = seq_lseek,
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530695 .release = single_release,
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100696};
697
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530698static int __init davinci_clk_debugfs_init(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100699{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530700 debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
701 &davinci_ck_operations);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100702 return 0;
703
704}
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530705device_initcall(davinci_clk_debugfs_init);
706#endif /* CONFIG_DEBUG_FS */