blob: 6de1e3428f1f3153ba145b8cf434f00d41e60e34 [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>
20#include <linux/platform_device.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.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
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/psc.h>
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070026#include <mach/cputype.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010027#include "clock.h"
28
Vladimir Barinov3e062b02007-06-05 16:36:55 +010029static LIST_HEAD(clocks);
30static DEFINE_MUTEX(clocks_mutex);
31static DEFINE_SPINLOCK(clockfw_lock);
32
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070033static unsigned psc_domain(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010034{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070035 return (clk->flags & PSC_DSP)
36 ? DAVINCI_GPSC_DSPDOMAIN
37 : DAVINCI_GPSC_ARMDOMAIN;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010038}
Vladimir Barinov3e062b02007-06-05 16:36:55 +010039
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070040static void __clk_enable(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010041{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070042 if (clk->parent)
43 __clk_enable(clk->parent);
44 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
Mark A. Greerd81d1882009-04-15 12:39:33 -070045 davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
46 clk->lpsc, 1);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010047}
48
49static void __clk_disable(struct clk *clk)
50{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070051 if (WARN_ON(clk->usecount == 0))
Vladimir Barinov3e062b02007-06-05 16:36:55 +010052 return;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070053 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL))
Mark A. Greerd81d1882009-04-15 12:39:33 -070054 davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
55 clk->lpsc, 0);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070056 if (clk->parent)
57 __clk_disable(clk->parent);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010058}
59
60int clk_enable(struct clk *clk)
61{
62 unsigned long flags;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010063
64 if (clk == NULL || IS_ERR(clk))
65 return -EINVAL;
66
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070067 spin_lock_irqsave(&clockfw_lock, flags);
68 __clk_enable(clk);
69 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010070
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070071 return 0;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010072}
73EXPORT_SYMBOL(clk_enable);
74
75void clk_disable(struct clk *clk)
76{
77 unsigned long flags;
78
79 if (clk == NULL || IS_ERR(clk))
80 return;
81
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070082 spin_lock_irqsave(&clockfw_lock, flags);
83 __clk_disable(clk);
84 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010085}
86EXPORT_SYMBOL(clk_disable);
87
88unsigned long clk_get_rate(struct clk *clk)
89{
90 if (clk == NULL || IS_ERR(clk))
91 return -EINVAL;
92
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070093 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010094}
95EXPORT_SYMBOL(clk_get_rate);
96
97long clk_round_rate(struct clk *clk, unsigned long rate)
98{
99 if (clk == NULL || IS_ERR(clk))
100 return -EINVAL;
101
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700102 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100103}
104EXPORT_SYMBOL(clk_round_rate);
105
106int clk_set_rate(struct clk *clk, unsigned long rate)
107{
108 if (clk == NULL || IS_ERR(clk))
109 return -EINVAL;
110
111 /* changing the clk rate is not supported */
112 return -EINVAL;
113}
114EXPORT_SYMBOL(clk_set_rate);
115
116int clk_register(struct clk *clk)
117{
118 if (clk == NULL || IS_ERR(clk))
119 return -EINVAL;
120
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700121 if (WARN(clk->parent && !clk->parent->rate,
122 "CLK: %s parent %s has no rate!\n",
123 clk->name, clk->parent->name))
124 return -EINVAL;
125
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530126 INIT_LIST_HEAD(&clk->children);
127
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100128 mutex_lock(&clocks_mutex);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700129 list_add_tail(&clk->node, &clocks);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530130 if (clk->parent)
131 list_add_tail(&clk->childnode, &clk->parent->children);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100132 mutex_unlock(&clocks_mutex);
133
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700134 /* If rate is already set, use it */
135 if (clk->rate)
136 return 0;
137
Sekhar Noride381a92009-08-31 15:48:02 +0530138 /* Else, see if there is a way to calculate it */
139 if (clk->recalc)
140 clk->rate = clk->recalc(clk);
141
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700142 /* Otherwise, default to parent rate */
Sekhar Noride381a92009-08-31 15:48:02 +0530143 else if (clk->parent)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700144 clk->rate = clk->parent->rate;
145
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100146 return 0;
147}
148EXPORT_SYMBOL(clk_register);
149
150void clk_unregister(struct clk *clk)
151{
152 if (clk == NULL || IS_ERR(clk))
153 return;
154
155 mutex_lock(&clocks_mutex);
156 list_del(&clk->node);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530157 list_del(&clk->childnode);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100158 mutex_unlock(&clocks_mutex);
159}
160EXPORT_SYMBOL(clk_unregister);
161
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700162#ifdef CONFIG_DAVINCI_RESET_CLOCKS
163/*
164 * Disable any unused clocks left on by the bootloader
165 */
166static int __init clk_disable_unused(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100167{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700168 struct clk *ck;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100169
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700170 spin_lock_irq(&clockfw_lock);
171 list_for_each_entry(ck, &clocks, node) {
172 if (ck->usecount > 0)
173 continue;
174 if (!(ck->flags & CLK_PSC))
175 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100176
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700177 /* ignore if in Disabled or SwRstDisable states */
Mark A. Greerd81d1882009-04-15 12:39:33 -0700178 if (!davinci_psc_is_clk_active(ck->psc_ctlr, ck->lpsc))
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700179 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100180
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700181 pr_info("Clocks: disable unused %s\n", ck->name);
Mark A. Greerd81d1882009-04-15 12:39:33 -0700182 davinci_psc_config(psc_domain(ck), ck->psc_ctlr, ck->lpsc, 0);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700183 }
184 spin_unlock_irq(&clockfw_lock);
185
186 return 0;
187}
188late_initcall(clk_disable_unused);
189#endif
190
Sekhar Noride381a92009-08-31 15:48:02 +0530191static unsigned long clk_sysclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700192{
193 u32 v, plldiv;
194 struct pll_data *pll;
Sekhar Noride381a92009-08-31 15:48:02 +0530195 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700196
197 /* If this is the PLL base clock, no more calculations needed */
198 if (clk->pll_data)
Sekhar Noride381a92009-08-31 15:48:02 +0530199 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700200
201 if (WARN_ON(!clk->parent))
Sekhar Noride381a92009-08-31 15:48:02 +0530202 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700203
Sekhar Noride381a92009-08-31 15:48:02 +0530204 rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700205
206 /* Otherwise, the parent must be a PLL */
207 if (WARN_ON(!clk->parent->pll_data))
Sekhar Noride381a92009-08-31 15:48:02 +0530208 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700209
210 pll = clk->parent->pll_data;
211
212 /* If pre-PLL, source clock is before the multiplier and divider(s) */
213 if (clk->flags & PRE_PLL)
Sekhar Noride381a92009-08-31 15:48:02 +0530214 rate = pll->input_rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700215
216 if (!clk->div_reg)
Sekhar Noride381a92009-08-31 15:48:02 +0530217 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700218
219 v = __raw_readl(pll->base + clk->div_reg);
220 if (v & PLLDIV_EN) {
221 plldiv = (v & PLLDIV_RATIO_MASK) + 1;
222 if (plldiv)
Sekhar Noride381a92009-08-31 15:48:02 +0530223 rate /= plldiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700224 }
Sekhar Noride381a92009-08-31 15:48:02 +0530225
226 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700227}
228
Sekhar Noride381a92009-08-31 15:48:02 +0530229static unsigned long clk_leafclk_recalc(struct clk *clk)
230{
231 if (WARN_ON(!clk->parent))
232 return clk->rate;
233
234 return clk->parent->rate;
235}
236
237static unsigned long clk_pllclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700238{
239 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
240 u8 bypass;
241 struct pll_data *pll = clk->pll_data;
Sekhar Noride381a92009-08-31 15:48:02 +0530242 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700243
244 pll->base = IO_ADDRESS(pll->phys_base);
245 ctrl = __raw_readl(pll->base + PLLCTL);
Sekhar Noride381a92009-08-31 15:48:02 +0530246 rate = pll->input_rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700247
248 if (ctrl & PLLCTL_PLLEN) {
249 bypass = 0;
250 mult = __raw_readl(pll->base + PLLM);
Sandeep Paulrajfb8fcb82009-06-11 09:41:05 -0400251 if (cpu_is_davinci_dm365())
252 mult = 2 * (mult & PLLM_PLLM_MASK);
253 else
254 mult = (mult & PLLM_PLLM_MASK) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700255 } else
256 bypass = 1;
257
258 if (pll->flags & PLL_HAS_PREDIV) {
259 prediv = __raw_readl(pll->base + PREDIV);
260 if (prediv & PLLDIV_EN)
261 prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
262 else
263 prediv = 1;
264 }
265
266 /* pre-divider is fixed, but (some?) chips won't report that */
267 if (cpu_is_davinci_dm355() && pll->num == 1)
268 prediv = 8;
269
270 if (pll->flags & PLL_HAS_POSTDIV) {
271 postdiv = __raw_readl(pll->base + POSTDIV);
272 if (postdiv & PLLDIV_EN)
273 postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
274 else
275 postdiv = 1;
276 }
277
278 if (!bypass) {
Sekhar Noride381a92009-08-31 15:48:02 +0530279 rate /= prediv;
280 rate *= mult;
281 rate /= postdiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700282 }
283
284 pr_debug("PLL%d: input = %lu MHz [ ",
285 pll->num, clk->parent->rate / 1000000);
286 if (bypass)
287 pr_debug("bypass ");
288 if (prediv > 1)
289 pr_debug("/ %d ", prediv);
290 if (mult > 1)
291 pr_debug("* %d ", mult);
292 if (postdiv > 1)
293 pr_debug("/ %d ", postdiv);
Sekhar Noride381a92009-08-31 15:48:02 +0530294 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
295
296 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700297}
298
299int __init davinci_clk_init(struct davinci_clk *clocks)
300 {
301 struct davinci_clk *c;
302 struct clk *clk;
303
304 for (c = clocks; c->lk.clk; c++) {
305 clk = c->lk.clk;
306
Sekhar Noride381a92009-08-31 15:48:02 +0530307 if (!clk->recalc) {
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700308
Sekhar Noride381a92009-08-31 15:48:02 +0530309 /* Check if clock is a PLL */
310 if (clk->pll_data)
311 clk->recalc = clk_pllclk_recalc;
312
313 /* Else, if it is a PLL-derived clock */
314 else if (clk->flags & CLK_PLL)
315 clk->recalc = clk_sysclk_recalc;
316
317 /* Otherwise, it is a leaf clock (PSC clock) */
318 else if (clk->parent)
319 clk->recalc = clk_leafclk_recalc;
320 }
321
322 if (clk->recalc)
323 clk->rate = clk->recalc(clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700324
325 if (clk->lpsc)
326 clk->flags |= CLK_PSC;
327
328 clkdev_add(&c->lk);
329 clk_register(clk);
330
331 /* Turn on clocks that Linux doesn't otherwise manage */
332 if (clk->flags & ALWAYS_ENABLED)
333 clk_enable(clk);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100334 }
335
336 return 0;
337}
338
339#ifdef CONFIG_PROC_FS
340#include <linux/proc_fs.h>
341#include <linux/seq_file.h>
342
343static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
344{
345 return *pos < 1 ? (void *)1 : NULL;
346}
347
348static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
349{
350 ++*pos;
351 return NULL;
352}
353
354static void davinci_ck_stop(struct seq_file *m, void *v)
355{
356}
357
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700358#define CLKNAME_MAX 10 /* longest clock name */
359#define NEST_DELTA 2
360#define NEST_MAX 4
361
362static void
363dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
364{
365 char *state;
366 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
367 struct clk *clk;
368 unsigned i;
369
370 if (parent->flags & CLK_PLL)
371 state = "pll";
372 else if (parent->flags & CLK_PSC)
373 state = "psc";
374 else
375 state = "";
376
377 /* <nest spaces> name <pad to end> */
378 memset(buf, ' ', sizeof(buf) - 1);
379 buf[sizeof(buf) - 1] = 0;
380 i = strlen(parent->name);
381 memcpy(buf + nest, parent->name,
382 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
383
384 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
385 buf, parent->usecount, state, clk_get_rate(parent));
386 /* REVISIT show device associations too */
387
388 /* cost is now small, but not linear... */
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530389 list_for_each_entry(clk, &parent->children, childnode) {
390 dump_clock(s, nest + NEST_DELTA, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700391 }
392}
393
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100394static int davinci_ck_show(struct seq_file *m, void *v)
395{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700396 /* Show clock tree; we know the main oscillator is first.
397 * We trust nonzero usecounts equate to PSC enables...
398 */
399 mutex_lock(&clocks_mutex);
400 if (!list_empty(&clocks))
401 dump_clock(m, 0, list_first_entry(&clocks, struct clk, node));
402 mutex_unlock(&clocks_mutex);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100403
404 return 0;
405}
406
Jan Engelhardt2ffd6e12008-01-22 20:41:07 +0100407static const struct seq_operations davinci_ck_op = {
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100408 .start = davinci_ck_start,
409 .next = davinci_ck_next,
410 .stop = davinci_ck_stop,
411 .show = davinci_ck_show
412};
413
414static int davinci_ck_open(struct inode *inode, struct file *file)
415{
416 return seq_open(file, &davinci_ck_op);
417}
418
Jan Engelhardt2ffd6e12008-01-22 20:41:07 +0100419static const struct file_operations proc_davinci_ck_operations = {
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100420 .open = davinci_ck_open,
421 .read = seq_read,
422 .llseek = seq_lseek,
423 .release = seq_release,
424};
425
426static int __init davinci_ck_proc_init(void)
427{
Denis V. Lunev40ad35d2008-04-29 01:02:21 -0700428 proc_create("davinci_clocks", 0, NULL, &proc_davinci_ck_operations);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100429 return 0;
430
431}
432__initcall(davinci_ck_proc_init);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700433#endif /* CONFIG_DEBUG_PROC_FS */