2 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
4 * Copyright (C) 2005, 2006 Paul Mundt
6 * This clock framework is derived from the OMAP version by:
8 * Copyright (C) 2004 Nokia Corporation
9 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License. See the file "COPYING" in the main directory of this archive
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/list.h>
20 #include <linux/kref.h>
21 #include <linux/seq_file.h>
22 #include <linux/err.h>
23 #include <asm/clock.h>
24 #include <asm/timer.h>
26 static LIST_HEAD(clock_list);
27 static DEFINE_SPINLOCK(clock_lock);
28 static DEFINE_MUTEX(clock_list_sem);
31 * Each subtype is expected to define the init routines for these clocks,
32 * as each subtype (or processor family) will have these clocks at the
33 * very least. These are all provided through the CPG, which even some of
34 * the more quirky parts (such as ST40, SH4-202, etc.) still have.
36 * The processor-specific code is expected to register any additional
37 * clock sources that are of interest.
39 static struct clk master_clk = {
41 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
42 .rate = CONFIG_SH_PCLK_FREQ,
45 static struct clk module_clk = {
47 .parent = &master_clk,
48 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
51 static struct clk bus_clk = {
53 .parent = &master_clk,
54 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
57 static struct clk cpu_clk = {
59 .parent = &master_clk,
60 .flags = CLK_ALWAYS_ENABLED,
64 * The ordering of these clocks matters, do not change it.
66 static struct clk *onchip_clocks[] = {
73 static void propagate_rate(struct clk *clk)
77 list_for_each_entry(clkp, &clock_list, node) {
78 if (likely(clkp->parent != clk))
80 if (likely(clkp->ops && clkp->ops->recalc))
81 clkp->ops->recalc(clkp);
85 int __clk_enable(struct clk *clk)
88 * See if this is the first time we're enabling the clock, some
89 * clocks that are always enabled still require "special"
90 * initialization. This is especially true if the clock mode
91 * changes and the clock needs to hunt for the proper set of
92 * divisors to use before it can effectively recalc.
94 if (unlikely(atomic_read(&clk->kref.refcount) == 1))
95 if (clk->ops && clk->ops->init)
98 if (clk->flags & CLK_ALWAYS_ENABLED)
101 if (likely(clk->ops && clk->ops->enable))
102 clk->ops->enable(clk);
104 kref_get(&clk->kref);
108 int clk_enable(struct clk *clk)
113 spin_lock_irqsave(&clock_lock, flags);
114 ret = __clk_enable(clk);
115 spin_unlock_irqrestore(&clock_lock, flags);
120 static void clk_kref_release(struct kref *kref)
125 void __clk_disable(struct clk *clk)
127 if (clk->flags & CLK_ALWAYS_ENABLED)
130 kref_put(&clk->kref, clk_kref_release);
133 void clk_disable(struct clk *clk)
137 spin_lock_irqsave(&clock_lock, flags);
139 spin_unlock_irqrestore(&clock_lock, flags);
142 int clk_register(struct clk *clk)
144 mutex_lock(&clock_list_sem);
146 list_add(&clk->node, &clock_list);
147 kref_init(&clk->kref);
149 mutex_unlock(&clock_list_sem);
154 void clk_unregister(struct clk *clk)
156 mutex_lock(&clock_list_sem);
157 list_del(&clk->node);
158 mutex_unlock(&clock_list_sem);
161 inline unsigned long clk_get_rate(struct clk *clk)
166 int clk_set_rate(struct clk *clk, unsigned long rate)
168 int ret = -EOPNOTSUPP;
170 if (likely(clk->ops && clk->ops->set_rate)) {
173 spin_lock_irqsave(&clock_lock, flags);
174 ret = clk->ops->set_rate(clk, rate);
175 spin_unlock_irqrestore(&clock_lock, flags);
178 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
184 void clk_recalc_rate(struct clk *clk)
186 if (likely(clk->ops && clk->ops->recalc)) {
189 spin_lock_irqsave(&clock_lock, flags);
190 clk->ops->recalc(clk);
191 spin_unlock_irqrestore(&clock_lock, flags);
194 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
198 struct clk *clk_get(const char *id)
200 struct clk *p, *clk = ERR_PTR(-ENOENT);
202 mutex_lock(&clock_list_sem);
203 list_for_each_entry(p, &clock_list, node) {
204 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
209 mutex_unlock(&clock_list_sem);
214 void clk_put(struct clk *clk)
216 if (clk && !IS_ERR(clk))
217 module_put(clk->owner);
220 void __init __attribute__ ((weak))
221 arch_init_clk_ops(struct clk_ops **ops, int type)
225 int __init clk_init(void)
229 BUG_ON(!master_clk.rate);
231 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
232 struct clk *clk = onchip_clocks[i];
234 arch_init_clk_ops(&clk->ops, i);
235 ret |= clk_register(clk);
239 /* Kick the child clocks.. */
240 propagate_rate(&master_clk);
241 propagate_rate(&bus_clk);
246 int show_clocks(struct seq_file *m)
250 list_for_each_entry_reverse(clk, &clock_list, node) {
251 unsigned long rate = clk_get_rate(clk);
254 * Don't bother listing dummy clocks with no ancestry
255 * that only support enable and disable ops.
257 if (unlikely(!rate && !clk->parent))
260 seq_printf(m, "%-12s\t: %ld.%02ldMHz\n", clk->name,
261 rate / 1000000, (rate % 1000000) / 10000);
267 EXPORT_SYMBOL_GPL(clk_register);
268 EXPORT_SYMBOL_GPL(clk_unregister);
269 EXPORT_SYMBOL_GPL(clk_get);
270 EXPORT_SYMBOL_GPL(clk_put);
271 EXPORT_SYMBOL_GPL(clk_enable);
272 EXPORT_SYMBOL_GPL(clk_disable);
273 EXPORT_SYMBOL_GPL(__clk_enable);
274 EXPORT_SYMBOL_GPL(__clk_disable);
275 EXPORT_SYMBOL_GPL(clk_get_rate);
276 EXPORT_SYMBOL_GPL(clk_set_rate);
277 EXPORT_SYMBOL_GPL(clk_recalc_rate);