blob: 34668ead53c73b1c70ee0b09261ba74ff8a3627b [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
55int clk_enable(struct clk *clk)
56{
57 unsigned long flags;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010058
59 if (clk == NULL || IS_ERR(clk))
60 return -EINVAL;
61
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070062 spin_lock_irqsave(&clockfw_lock, flags);
63 __clk_enable(clk);
64 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010065
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070066 return 0;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010067}
68EXPORT_SYMBOL(clk_enable);
69
70void clk_disable(struct clk *clk)
71{
72 unsigned long flags;
73
74 if (clk == NULL || IS_ERR(clk))
75 return;
76
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070077 spin_lock_irqsave(&clockfw_lock, flags);
78 __clk_disable(clk);
79 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010080}
81EXPORT_SYMBOL(clk_disable);
82
83unsigned long clk_get_rate(struct clk *clk)
84{
85 if (clk == NULL || IS_ERR(clk))
86 return -EINVAL;
87
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070088 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010089}
90EXPORT_SYMBOL(clk_get_rate);
91
92long clk_round_rate(struct clk *clk, unsigned long rate)
93{
94 if (clk == NULL || IS_ERR(clk))
95 return -EINVAL;
96
Sekhar Norid6a61562009-08-31 15:48:03 +053097 if (clk->round_rate)
98 return clk->round_rate(clk, rate);
99
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700100 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100101}
102EXPORT_SYMBOL(clk_round_rate);
103
Sekhar Norid6a61562009-08-31 15:48:03 +0530104/* Propagate rate to children */
105static void propagate_rate(struct clk *root)
106{
107 struct clk *clk;
108
109 list_for_each_entry(clk, &root->children, childnode) {
110 if (clk->recalc)
111 clk->rate = clk->recalc(clk);
112 propagate_rate(clk);
113 }
114}
115
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100116int clk_set_rate(struct clk *clk, unsigned long rate)
117{
Sekhar Norid6a61562009-08-31 15:48:03 +0530118 unsigned long flags;
119 int ret = -EINVAL;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100120
Sekhar Norid6a61562009-08-31 15:48:03 +0530121 if (clk == NULL || IS_ERR(clk))
122 return ret;
123
Sekhar Norid6a61562009-08-31 15:48:03 +0530124 if (clk->set_rate)
125 ret = clk->set_rate(clk, rate);
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530126
127 spin_lock_irqsave(&clockfw_lock, flags);
Sekhar Norid6a61562009-08-31 15:48:03 +0530128 if (ret == 0) {
129 if (clk->recalc)
130 clk->rate = clk->recalc(clk);
131 propagate_rate(clk);
132 }
133 spin_unlock_irqrestore(&clockfw_lock, flags);
134
135 return ret;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100136}
137EXPORT_SYMBOL(clk_set_rate);
138
Sekhar Norib82a51e2009-08-31 15:48:04 +0530139int clk_set_parent(struct clk *clk, struct clk *parent)
140{
141 unsigned long flags;
142
143 if (clk == NULL || IS_ERR(clk))
144 return -EINVAL;
145
146 /* Cannot change parent on enabled clock */
147 if (WARN_ON(clk->usecount))
148 return -EINVAL;
149
150 mutex_lock(&clocks_mutex);
151 clk->parent = parent;
152 list_del_init(&clk->childnode);
153 list_add(&clk->childnode, &clk->parent->children);
154 mutex_unlock(&clocks_mutex);
155
156 spin_lock_irqsave(&clockfw_lock, flags);
157 if (clk->recalc)
158 clk->rate = clk->recalc(clk);
159 propagate_rate(clk);
160 spin_unlock_irqrestore(&clockfw_lock, flags);
161
162 return 0;
163}
164EXPORT_SYMBOL(clk_set_parent);
165
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100166int clk_register(struct clk *clk)
167{
168 if (clk == NULL || IS_ERR(clk))
169 return -EINVAL;
170
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700171 if (WARN(clk->parent && !clk->parent->rate,
172 "CLK: %s parent %s has no rate!\n",
173 clk->name, clk->parent->name))
174 return -EINVAL;
175
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530176 INIT_LIST_HEAD(&clk->children);
177
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100178 mutex_lock(&clocks_mutex);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700179 list_add_tail(&clk->node, &clocks);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530180 if (clk->parent)
181 list_add_tail(&clk->childnode, &clk->parent->children);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100182 mutex_unlock(&clocks_mutex);
183
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700184 /* If rate is already set, use it */
185 if (clk->rate)
186 return 0;
187
Sekhar Noride381a92009-08-31 15:48:02 +0530188 /* Else, see if there is a way to calculate it */
189 if (clk->recalc)
190 clk->rate = clk->recalc(clk);
191
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700192 /* Otherwise, default to parent rate */
Sekhar Noride381a92009-08-31 15:48:02 +0530193 else if (clk->parent)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700194 clk->rate = clk->parent->rate;
195
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100196 return 0;
197}
198EXPORT_SYMBOL(clk_register);
199
200void clk_unregister(struct clk *clk)
201{
202 if (clk == NULL || IS_ERR(clk))
203 return;
204
205 mutex_lock(&clocks_mutex);
206 list_del(&clk->node);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530207 list_del(&clk->childnode);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100208 mutex_unlock(&clocks_mutex);
209}
210EXPORT_SYMBOL(clk_unregister);
211
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700212#ifdef CONFIG_DAVINCI_RESET_CLOCKS
213/*
214 * Disable any unused clocks left on by the bootloader
215 */
Shawn Guo3aa3e842012-04-26 09:45:39 +0800216int __init davinci_clk_disable_unused(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100217{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700218 struct clk *ck;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100219
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700220 spin_lock_irq(&clockfw_lock);
221 list_for_each_entry(ck, &clocks, node) {
222 if (ck->usecount > 0)
223 continue;
224 if (!(ck->flags & CLK_PSC))
225 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100226
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700227 /* ignore if in Disabled or SwRstDisable states */
Sergei Shtylyov789a7852009-09-30 19:48:03 +0400228 if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700229 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100230
Kevin Hilmanc89f1682010-08-05 10:55:16 -0700231 pr_debug("Clocks: disable unused %s\n", ck->name);
Cyril Chemparathy52958be2010-03-25 17:43:47 -0400232
Murali Karicheri12221d42011-11-15 01:42:09 +0530233 davinci_psc_config(ck->domain, ck->gpsc, ck->lpsc,
Sekhar Noria51ca382011-07-06 06:01:21 +0000234 false, ck->flags);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700235 }
236 spin_unlock_irq(&clockfw_lock);
237
238 return 0;
239}
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700240#endif
241
Sekhar Noride381a92009-08-31 15:48:02 +0530242static unsigned long clk_sysclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700243{
244 u32 v, plldiv;
245 struct pll_data *pll;
Sekhar Noride381a92009-08-31 15:48:02 +0530246 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700247
248 /* If this is the PLL base clock, no more calculations needed */
249 if (clk->pll_data)
Sekhar Noride381a92009-08-31 15:48:02 +0530250 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700251
252 if (WARN_ON(!clk->parent))
Sekhar Noride381a92009-08-31 15:48:02 +0530253 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700254
Sekhar Noride381a92009-08-31 15:48:02 +0530255 rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700256
257 /* Otherwise, the parent must be a PLL */
258 if (WARN_ON(!clk->parent->pll_data))
Sekhar Noride381a92009-08-31 15:48:02 +0530259 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700260
261 pll = clk->parent->pll_data;
262
263 /* If pre-PLL, source clock is before the multiplier and divider(s) */
264 if (clk->flags & PRE_PLL)
Sekhar Noride381a92009-08-31 15:48:02 +0530265 rate = pll->input_rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700266
267 if (!clk->div_reg)
Sekhar Noride381a92009-08-31 15:48:02 +0530268 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700269
270 v = __raw_readl(pll->base + clk->div_reg);
271 if (v & PLLDIV_EN) {
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400272 plldiv = (v & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700273 if (plldiv)
Sekhar Noride381a92009-08-31 15:48:02 +0530274 rate /= plldiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700275 }
Sekhar Noride381a92009-08-31 15:48:02 +0530276
277 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700278}
279
Sekhar Norib39639b2010-07-20 16:46:49 +0530280int davinci_set_sysclk_rate(struct clk *clk, unsigned long rate)
281{
282 unsigned v;
283 struct pll_data *pll;
284 unsigned long input;
285 unsigned ratio = 0;
286
287 /* If this is the PLL base clock, wrong function to call */
288 if (clk->pll_data)
289 return -EINVAL;
290
291 /* There must be a parent... */
292 if (WARN_ON(!clk->parent))
293 return -EINVAL;
294
295 /* ... the parent must be a PLL... */
296 if (WARN_ON(!clk->parent->pll_data))
297 return -EINVAL;
298
299 /* ... and this clock must have a divider. */
300 if (WARN_ON(!clk->div_reg))
301 return -EINVAL;
302
303 pll = clk->parent->pll_data;
304
305 input = clk->parent->rate;
306
307 /* If pre-PLL, source clock is before the multiplier and divider(s) */
308 if (clk->flags & PRE_PLL)
309 input = pll->input_rate;
310
311 if (input > rate) {
312 /*
313 * Can afford to provide an output little higher than requested
314 * only if maximum rate supported by hardware on this sysclk
315 * is known.
316 */
317 if (clk->maxrate) {
318 ratio = DIV_ROUND_CLOSEST(input, rate);
319 if (input / ratio > clk->maxrate)
320 ratio = 0;
321 }
322
323 if (ratio == 0)
324 ratio = DIV_ROUND_UP(input, rate);
325
326 ratio--;
327 }
328
Cyril Chemparathyb1d05be2010-10-20 17:49:56 -0400329 if (ratio > pll->div_ratio_mask)
Sekhar Norib39639b2010-07-20 16:46:49 +0530330 return -EINVAL;
331
332 do {
333 v = __raw_readl(pll->base + PLLSTAT);
334 } while (v & PLLSTAT_GOSTAT);
335
336 v = __raw_readl(pll->base + clk->div_reg);
Cyril Chemparathyb1d05be2010-10-20 17:49:56 -0400337 v &= ~pll->div_ratio_mask;
Sekhar Norib39639b2010-07-20 16:46:49 +0530338 v |= ratio | PLLDIV_EN;
339 __raw_writel(v, pll->base + clk->div_reg);
340
341 v = __raw_readl(pll->base + PLLCMD);
342 v |= PLLCMD_GOSET;
343 __raw_writel(v, pll->base + PLLCMD);
344
345 do {
346 v = __raw_readl(pll->base + PLLSTAT);
347 } while (v & PLLSTAT_GOSTAT);
348
349 return 0;
350}
351EXPORT_SYMBOL(davinci_set_sysclk_rate);
352
Sekhar Noride381a92009-08-31 15:48:02 +0530353static unsigned long clk_leafclk_recalc(struct clk *clk)
354{
355 if (WARN_ON(!clk->parent))
356 return clk->rate;
357
358 return clk->parent->rate;
359}
360
Sekhar Nori56e580d2011-06-14 15:33:20 +0000361int davinci_simple_set_rate(struct clk *clk, unsigned long rate)
362{
363 clk->rate = rate;
364 return 0;
365}
366
Sekhar Noride381a92009-08-31 15:48:02 +0530367static unsigned long clk_pllclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700368{
369 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
370 u8 bypass;
371 struct pll_data *pll = clk->pll_data;
Sekhar Noride381a92009-08-31 15:48:02 +0530372 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700373
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700374 ctrl = __raw_readl(pll->base + PLLCTL);
Sekhar Noride381a92009-08-31 15:48:02 +0530375 rate = pll->input_rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700376
377 if (ctrl & PLLCTL_PLLEN) {
378 bypass = 0;
379 mult = __raw_readl(pll->base + PLLM);
Sandeep Paulrajfb8fcb82009-06-11 09:41:05 -0400380 if (cpu_is_davinci_dm365())
381 mult = 2 * (mult & PLLM_PLLM_MASK);
382 else
383 mult = (mult & PLLM_PLLM_MASK) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700384 } else
385 bypass = 1;
386
387 if (pll->flags & PLL_HAS_PREDIV) {
388 prediv = __raw_readl(pll->base + PREDIV);
389 if (prediv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400390 prediv = (prediv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700391 else
392 prediv = 1;
393 }
394
395 /* pre-divider is fixed, but (some?) chips won't report that */
396 if (cpu_is_davinci_dm355() && pll->num == 1)
397 prediv = 8;
398
399 if (pll->flags & PLL_HAS_POSTDIV) {
400 postdiv = __raw_readl(pll->base + POSTDIV);
401 if (postdiv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400402 postdiv = (postdiv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700403 else
404 postdiv = 1;
405 }
406
407 if (!bypass) {
Sekhar Noride381a92009-08-31 15:48:02 +0530408 rate /= prediv;
409 rate *= mult;
410 rate /= postdiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700411 }
412
413 pr_debug("PLL%d: input = %lu MHz [ ",
414 pll->num, clk->parent->rate / 1000000);
415 if (bypass)
416 pr_debug("bypass ");
417 if (prediv > 1)
418 pr_debug("/ %d ", prediv);
419 if (mult > 1)
420 pr_debug("* %d ", mult);
421 if (postdiv > 1)
422 pr_debug("/ %d ", postdiv);
Sekhar Noride381a92009-08-31 15:48:02 +0530423 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
424
425 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700426}
427
Sekhar Norid6a61562009-08-31 15:48:03 +0530428/**
429 * davinci_set_pllrate - set the output rate of a given PLL.
430 *
431 * Note: Currently tested to work with OMAP-L138 only.
432 *
433 * @pll: pll whose rate needs to be changed.
434 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
435 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
436 * @postdiv: The post divider value. Passing 0 disables the post-divider.
437 */
438int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
439 unsigned int mult, unsigned int postdiv)
440{
441 u32 ctrl;
442 unsigned int locktime;
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530443 unsigned long flags;
Sekhar Norid6a61562009-08-31 15:48:03 +0530444
445 if (pll->base == NULL)
446 return -EINVAL;
447
448 /*
449 * PLL lock time required per OMAP-L138 datasheet is
450 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
451 * as 4 and OSCIN cycle as 25 MHz.
452 */
453 if (prediv) {
454 locktime = ((2000 * prediv) / 100);
455 prediv = (prediv - 1) | PLLDIV_EN;
456 } else {
Sekhar Nori9a219a92009-11-16 17:21:33 +0530457 locktime = PLL_LOCK_TIME;
Sekhar Norid6a61562009-08-31 15:48:03 +0530458 }
459 if (postdiv)
460 postdiv = (postdiv - 1) | PLLDIV_EN;
461 if (mult)
462 mult = mult - 1;
463
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530464 /* Protect against simultaneous calls to PLL setting seqeunce */
465 spin_lock_irqsave(&clockfw_lock, flags);
466
Sekhar Norid6a61562009-08-31 15:48:03 +0530467 ctrl = __raw_readl(pll->base + PLLCTL);
468
469 /* Switch the PLL to bypass mode */
470 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
471 __raw_writel(ctrl, pll->base + PLLCTL);
472
Sekhar Nori9a219a92009-11-16 17:21:33 +0530473 udelay(PLL_BYPASS_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530474
475 /* Reset and enable PLL */
476 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
477 __raw_writel(ctrl, pll->base + PLLCTL);
478
479 if (pll->flags & PLL_HAS_PREDIV)
480 __raw_writel(prediv, pll->base + PREDIV);
481
482 __raw_writel(mult, pll->base + PLLM);
483
484 if (pll->flags & PLL_HAS_POSTDIV)
485 __raw_writel(postdiv, pll->base + POSTDIV);
486
Sekhar Nori9a219a92009-11-16 17:21:33 +0530487 udelay(PLL_RESET_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530488
489 /* Bring PLL out of reset */
490 ctrl |= PLLCTL_PLLRST;
491 __raw_writel(ctrl, pll->base + PLLCTL);
492
493 udelay(locktime);
494
495 /* Remove PLL from bypass mode */
496 ctrl |= PLLCTL_PLLEN;
497 __raw_writel(ctrl, pll->base + PLLCTL);
498
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530499 spin_unlock_irqrestore(&clockfw_lock, flags);
500
Sekhar Norid6a61562009-08-31 15:48:03 +0530501 return 0;
502}
503EXPORT_SYMBOL(davinci_set_pllrate);
504
Sekhar Nori56e580d2011-06-14 15:33:20 +0000505/**
506 * davinci_set_refclk_rate() - Set the reference clock rate
507 * @rate: The new rate.
508 *
509 * Sets the reference clock rate to a given value. This will most likely
510 * result in the entire clock tree getting updated.
511 *
512 * This is used to support boards which use a reference clock different
513 * than that used by default in <soc>.c file. The reference clock rate
514 * should be updated early in the boot process; ideally soon after the
515 * clock tree has been initialized once with the default reference clock
516 * rate (davinci_common_init()).
517 *
518 * Returns 0 on success, error otherwise.
519 */
520int davinci_set_refclk_rate(unsigned long rate)
521{
522 struct clk *refclk;
523
524 refclk = clk_get(NULL, "ref");
525 if (IS_ERR(refclk)) {
526 pr_err("%s: failed to get reference clock.\n", __func__);
527 return PTR_ERR(refclk);
528 }
529
530 clk_set_rate(refclk, rate);
531
532 clk_put(refclk);
533
534 return 0;
535}
536
Kevin Hilman08aca082010-01-11 08:22:23 -0800537int __init davinci_clk_init(struct clk_lookup *clocks)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700538 {
Kevin Hilman08aca082010-01-11 08:22:23 -0800539 struct clk_lookup *c;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700540 struct clk *clk;
Kevin Hilman08aca082010-01-11 08:22:23 -0800541 size_t num_clocks = 0;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700542
Kevin Hilman08aca082010-01-11 08:22:23 -0800543 for (c = clocks; c->clk; c++) {
544 clk = c->clk;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700545
Sekhar Noride381a92009-08-31 15:48:02 +0530546 if (!clk->recalc) {
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700547
Sekhar Noride381a92009-08-31 15:48:02 +0530548 /* Check if clock is a PLL */
549 if (clk->pll_data)
550 clk->recalc = clk_pllclk_recalc;
551
552 /* Else, if it is a PLL-derived clock */
553 else if (clk->flags & CLK_PLL)
554 clk->recalc = clk_sysclk_recalc;
555
556 /* Otherwise, it is a leaf clock (PSC clock) */
557 else if (clk->parent)
558 clk->recalc = clk_leafclk_recalc;
559 }
560
Cyril Chemparathye4c822c2010-05-07 17:06:36 -0400561 if (clk->pll_data) {
562 struct pll_data *pll = clk->pll_data;
563
564 if (!pll->div_ratio_mask)
565 pll->div_ratio_mask = PLLDIV_RATIO_MASK;
566
567 if (pll->phys_base && !pll->base) {
568 pll->base = ioremap(pll->phys_base, SZ_4K);
569 WARN_ON(!pll->base);
570 }
571 }
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400572
Sekhar Noride381a92009-08-31 15:48:02 +0530573 if (clk->recalc)
574 clk->rate = clk->recalc(clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700575
576 if (clk->lpsc)
577 clk->flags |= CLK_PSC;
578
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700579 clk_register(clk);
Kevin Hilman08aca082010-01-11 08:22:23 -0800580 num_clocks++;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700581
582 /* Turn on clocks that Linux doesn't otherwise manage */
583 if (clk->flags & ALWAYS_ENABLED)
584 clk_enable(clk);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100585 }
586
Kevin Hilman08aca082010-01-11 08:22:23 -0800587 clkdev_add_table(clocks, num_clocks);
588
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100589 return 0;
590}
591
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530592#ifdef CONFIG_DEBUG_FS
593
594#include <linux/debugfs.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100595#include <linux/seq_file.h>
596
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700597#define CLKNAME_MAX 10 /* longest clock name */
598#define NEST_DELTA 2
599#define NEST_MAX 4
600
601static void
602dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
603{
604 char *state;
605 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
606 struct clk *clk;
607 unsigned i;
608
609 if (parent->flags & CLK_PLL)
610 state = "pll";
611 else if (parent->flags & CLK_PSC)
612 state = "psc";
613 else
614 state = "";
615
616 /* <nest spaces> name <pad to end> */
617 memset(buf, ' ', sizeof(buf) - 1);
618 buf[sizeof(buf) - 1] = 0;
619 i = strlen(parent->name);
620 memcpy(buf + nest, parent->name,
621 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
622
623 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
624 buf, parent->usecount, state, clk_get_rate(parent));
625 /* REVISIT show device associations too */
626
627 /* cost is now small, but not linear... */
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530628 list_for_each_entry(clk, &parent->children, childnode) {
629 dump_clock(s, nest + NEST_DELTA, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700630 }
631}
632
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100633static int davinci_ck_show(struct seq_file *m, void *v)
634{
Sekhar Norif979aa62009-12-03 15:36:51 +0530635 struct clk *clk;
636
637 /*
638 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700639 */
640 mutex_lock(&clocks_mutex);
Sekhar Norif979aa62009-12-03 15:36:51 +0530641 list_for_each_entry(clk, &clocks, node)
642 if (!clk->parent)
643 dump_clock(m, 0, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700644 mutex_unlock(&clocks_mutex);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100645
646 return 0;
647}
648
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100649static int davinci_ck_open(struct inode *inode, struct file *file)
650{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530651 return single_open(file, davinci_ck_show, NULL);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100652}
653
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530654static const struct file_operations davinci_ck_operations = {
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100655 .open = davinci_ck_open,
656 .read = seq_read,
657 .llseek = seq_lseek,
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530658 .release = single_release,
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100659};
660
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530661static int __init davinci_clk_debugfs_init(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100662{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530663 debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
664 &davinci_ck_operations);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100665 return 0;
666
667}
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530668device_initcall(davinci_clk_debugfs_init);
669#endif /* CONFIG_DEBUG_FS */