diff options
Diffstat (limited to 'arch/sh/kernel/cpu/clock.c')
-rw-r--r-- | arch/sh/kernel/cpu/clock.c | 606 |
1 files changed, 413 insertions, 193 deletions
diff --git a/arch/sh/kernel/cpu/clock.c b/arch/sh/kernel/cpu/clock.c index 1dc896483b59..f3a46be2ae81 100644 --- a/arch/sh/kernel/cpu/clock.c +++ b/arch/sh/kernel/cpu/clock.c | |||
@@ -1,15 +1,19 @@ | |||
1 | /* | 1 | /* |
2 | * arch/sh/kernel/cpu/clock.c - SuperH clock framework | 2 | * arch/sh/kernel/cpu/clock.c - SuperH clock framework |
3 | * | 3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Paul Mundt | 4 | * Copyright (C) 2005 - 2009 Paul Mundt |
5 | * | 5 | * |
6 | * This clock framework is derived from the OMAP version by: | 6 | * This clock framework is derived from the OMAP version by: |
7 | * | 7 | * |
8 | * Copyright (C) 2004 - 2005 Nokia Corporation | 8 | * Copyright (C) 2004 - 2008 Nokia Corporation |
9 | * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com> | 9 | * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com> |
10 | * | 10 | * |
11 | * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com> | 11 | * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com> |
12 | * | 12 | * |
13 | * With clkdev bits: | ||
14 | * | ||
15 | * Copyright (C) 2008 Russell King. | ||
16 | * | ||
13 | * This file is subject to the terms and conditions of the GNU General Public | 17 | * This file is subject to the terms and conditions of the GNU General Public |
14 | * License. See the file "COPYING" in the main directory of this archive | 18 | * License. See the file "COPYING" in the main directory of this archive |
15 | * for more details. | 19 | * for more details. |
@@ -19,134 +23,159 @@ | |||
19 | #include <linux/module.h> | 23 | #include <linux/module.h> |
20 | #include <linux/mutex.h> | 24 | #include <linux/mutex.h> |
21 | #include <linux/list.h> | 25 | #include <linux/list.h> |
22 | #include <linux/kref.h> | ||
23 | #include <linux/kobject.h> | 26 | #include <linux/kobject.h> |
24 | #include <linux/sysdev.h> | 27 | #include <linux/sysdev.h> |
25 | #include <linux/seq_file.h> | 28 | #include <linux/seq_file.h> |
26 | #include <linux/err.h> | 29 | #include <linux/err.h> |
27 | #include <linux/platform_device.h> | 30 | #include <linux/platform_device.h> |
28 | #include <linux/proc_fs.h> | 31 | #include <linux/debugfs.h> |
32 | #include <linux/cpufreq.h> | ||
29 | #include <asm/clock.h> | 33 | #include <asm/clock.h> |
30 | #include <asm/timer.h> | 34 | #include <asm/machvec.h> |
31 | 35 | ||
32 | static LIST_HEAD(clock_list); | 36 | static LIST_HEAD(clock_list); |
33 | static DEFINE_SPINLOCK(clock_lock); | 37 | static DEFINE_SPINLOCK(clock_lock); |
34 | static DEFINE_MUTEX(clock_list_sem); | 38 | static DEFINE_MUTEX(clock_list_sem); |
35 | 39 | ||
36 | /* | 40 | void clk_rate_table_build(struct clk *clk, |
37 | * Each subtype is expected to define the init routines for these clocks, | 41 | struct cpufreq_frequency_table *freq_table, |
38 | * as each subtype (or processor family) will have these clocks at the | 42 | int nr_freqs, |
39 | * very least. These are all provided through the CPG, which even some of | 43 | struct clk_div_mult_table *src_table, |
40 | * the more quirky parts (such as ST40, SH4-202, etc.) still have. | 44 | unsigned long *bitmap) |
41 | * | 45 | { |
42 | * The processor-specific code is expected to register any additional | 46 | unsigned long mult, div; |
43 | * clock sources that are of interest. | 47 | unsigned long freq; |
44 | */ | 48 | int i; |
45 | static struct clk master_clk = { | ||
46 | .name = "master_clk", | ||
47 | .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES, | ||
48 | .rate = CONFIG_SH_PCLK_FREQ, | ||
49 | }; | ||
50 | 49 | ||
51 | static struct clk module_clk = { | 50 | for (i = 0; i < nr_freqs; i++) { |
52 | .name = "module_clk", | 51 | div = 1; |
53 | .parent = &master_clk, | 52 | mult = 1; |
54 | .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES, | ||
55 | }; | ||
56 | 53 | ||
57 | static struct clk bus_clk = { | 54 | if (src_table->divisors && i < src_table->nr_divisors) |
58 | .name = "bus_clk", | 55 | div = src_table->divisors[i]; |
59 | .parent = &master_clk, | ||
60 | .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES, | ||
61 | }; | ||
62 | 56 | ||
63 | static struct clk cpu_clk = { | 57 | if (src_table->multipliers && i < src_table->nr_multipliers) |
64 | .name = "cpu_clk", | 58 | mult = src_table->multipliers[i]; |
65 | .parent = &master_clk, | ||
66 | .flags = CLK_ALWAYS_ENABLED, | ||
67 | }; | ||
68 | 59 | ||
69 | /* | 60 | if (!div || !mult || (bitmap && !test_bit(i, bitmap))) |
70 | * The ordering of these clocks matters, do not change it. | 61 | freq = CPUFREQ_ENTRY_INVALID; |
71 | */ | 62 | else |
72 | static struct clk *onchip_clocks[] = { | 63 | freq = clk->parent->rate * mult / div; |
73 | &master_clk, | ||
74 | &module_clk, | ||
75 | &bus_clk, | ||
76 | &cpu_clk, | ||
77 | }; | ||
78 | 64 | ||
79 | static void propagate_rate(struct clk *clk) | 65 | freq_table[i].index = i; |
66 | freq_table[i].frequency = freq; | ||
67 | } | ||
68 | |||
69 | /* Termination entry */ | ||
70 | freq_table[i].index = i; | ||
71 | freq_table[i].frequency = CPUFREQ_TABLE_END; | ||
72 | } | ||
73 | |||
74 | long clk_rate_table_round(struct clk *clk, | ||
75 | struct cpufreq_frequency_table *freq_table, | ||
76 | unsigned long rate) | ||
80 | { | 77 | { |
81 | struct clk *clkp; | 78 | unsigned long rate_error, rate_error_prev = ~0UL; |
79 | unsigned long rate_best_fit = rate; | ||
80 | unsigned long highest, lowest; | ||
81 | int i; | ||
82 | |||
83 | highest = lowest = 0; | ||
84 | |||
85 | for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) { | ||
86 | unsigned long freq = freq_table[i].frequency; | ||
82 | 87 | ||
83 | list_for_each_entry(clkp, &clock_list, node) { | 88 | if (freq == CPUFREQ_ENTRY_INVALID) |
84 | if (likely(clkp->parent != clk)) | ||
85 | continue; | 89 | continue; |
86 | if (likely(clkp->ops && clkp->ops->recalc)) | 90 | |
87 | clkp->ops->recalc(clkp); | 91 | if (freq > highest) |
88 | if (unlikely(clkp->flags & CLK_RATE_PROPAGATES)) | 92 | highest = freq; |
89 | propagate_rate(clkp); | 93 | if (freq < lowest) |
94 | lowest = freq; | ||
95 | |||
96 | rate_error = abs(freq - rate); | ||
97 | if (rate_error < rate_error_prev) { | ||
98 | rate_best_fit = freq; | ||
99 | rate_error_prev = rate_error; | ||
100 | } | ||
101 | |||
102 | if (rate_error == 0) | ||
103 | break; | ||
90 | } | 104 | } |
105 | |||
106 | if (rate >= highest) | ||
107 | rate_best_fit = highest; | ||
108 | if (rate <= lowest) | ||
109 | rate_best_fit = lowest; | ||
110 | |||
111 | return rate_best_fit; | ||
91 | } | 112 | } |
92 | 113 | ||
93 | static int __clk_enable(struct clk *clk) | 114 | int clk_rate_table_find(struct clk *clk, |
115 | struct cpufreq_frequency_table *freq_table, | ||
116 | unsigned long rate) | ||
94 | { | 117 | { |
95 | /* | 118 | int i; |
96 | * See if this is the first time we're enabling the clock, some | ||
97 | * clocks that are always enabled still require "special" | ||
98 | * initialization. This is especially true if the clock mode | ||
99 | * changes and the clock needs to hunt for the proper set of | ||
100 | * divisors to use before it can effectively recalc. | ||
101 | */ | ||
102 | if (unlikely(atomic_read(&clk->kref.refcount) == 1)) | ||
103 | if (clk->ops && clk->ops->init) | ||
104 | clk->ops->init(clk); | ||
105 | 119 | ||
106 | kref_get(&clk->kref); | 120 | for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) { |
121 | unsigned long freq = freq_table[i].frequency; | ||
107 | 122 | ||
108 | if (clk->flags & CLK_ALWAYS_ENABLED) | 123 | if (freq == CPUFREQ_ENTRY_INVALID) |
109 | return 0; | 124 | continue; |
110 | 125 | ||
111 | if (likely(clk->ops && clk->ops->enable)) | 126 | if (freq == rate) |
112 | clk->ops->enable(clk); | 127 | return i; |
128 | } | ||
113 | 129 | ||
114 | return 0; | 130 | return -ENOENT; |
115 | } | 131 | } |
116 | 132 | ||
117 | int clk_enable(struct clk *clk) | 133 | /* Used for clocks that always have same value as the parent clock */ |
134 | unsigned long followparent_recalc(struct clk *clk) | ||
118 | { | 135 | { |
119 | unsigned long flags; | 136 | return clk->parent ? clk->parent->rate : 0; |
120 | int ret; | 137 | } |
121 | |||
122 | if (!clk) | ||
123 | return -EINVAL; | ||
124 | 138 | ||
125 | clk_enable(clk->parent); | 139 | int clk_reparent(struct clk *child, struct clk *parent) |
140 | { | ||
141 | list_del_init(&child->sibling); | ||
142 | if (parent) | ||
143 | list_add(&child->sibling, &parent->children); | ||
144 | child->parent = parent; | ||
126 | 145 | ||
127 | spin_lock_irqsave(&clock_lock, flags); | 146 | /* now do the debugfs renaming to reattach the child |
128 | ret = __clk_enable(clk); | 147 | to the proper parent */ |
129 | spin_unlock_irqrestore(&clock_lock, flags); | ||
130 | 148 | ||
131 | return ret; | 149 | return 0; |
132 | } | 150 | } |
133 | EXPORT_SYMBOL_GPL(clk_enable); | ||
134 | 151 | ||
135 | static void clk_kref_release(struct kref *kref) | 152 | /* Propagate rate to children */ |
153 | void propagate_rate(struct clk *tclk) | ||
136 | { | 154 | { |
137 | /* Nothing to do */ | 155 | struct clk *clkp; |
156 | |||
157 | list_for_each_entry(clkp, &tclk->children, sibling) { | ||
158 | if (clkp->ops && clkp->ops->recalc) | ||
159 | clkp->rate = clkp->ops->recalc(clkp); | ||
160 | |||
161 | propagate_rate(clkp); | ||
162 | } | ||
138 | } | 163 | } |
139 | 164 | ||
140 | static void __clk_disable(struct clk *clk) | 165 | static void __clk_disable(struct clk *clk) |
141 | { | 166 | { |
142 | int count = kref_put(&clk->kref, clk_kref_release); | 167 | if (clk->usecount == 0) { |
143 | 168 | printk(KERN_ERR "Trying disable clock %s with 0 usecount\n", | |
144 | if (clk->flags & CLK_ALWAYS_ENABLED) | 169 | clk->name); |
170 | WARN_ON(1); | ||
145 | return; | 171 | return; |
172 | } | ||
146 | 173 | ||
147 | if (!count) { /* count reaches zero, disable the clock */ | 174 | if (!(--clk->usecount)) { |
148 | if (likely(clk->ops && clk->ops->disable)) | 175 | if (likely(clk->ops && clk->ops->disable)) |
149 | clk->ops->disable(clk); | 176 | clk->ops->disable(clk); |
177 | if (likely(clk->parent)) | ||
178 | __clk_disable(clk->parent); | ||
150 | } | 179 | } |
151 | } | 180 | } |
152 | 181 | ||
@@ -160,28 +189,97 @@ void clk_disable(struct clk *clk) | |||
160 | spin_lock_irqsave(&clock_lock, flags); | 189 | spin_lock_irqsave(&clock_lock, flags); |
161 | __clk_disable(clk); | 190 | __clk_disable(clk); |
162 | spin_unlock_irqrestore(&clock_lock, flags); | 191 | spin_unlock_irqrestore(&clock_lock, flags); |
163 | |||
164 | clk_disable(clk->parent); | ||
165 | } | 192 | } |
166 | EXPORT_SYMBOL_GPL(clk_disable); | 193 | EXPORT_SYMBOL_GPL(clk_disable); |
167 | 194 | ||
195 | static int __clk_enable(struct clk *clk) | ||
196 | { | ||
197 | int ret = 0; | ||
198 | |||
199 | if (clk->usecount++ == 0) { | ||
200 | if (clk->parent) { | ||
201 | ret = __clk_enable(clk->parent); | ||
202 | if (unlikely(ret)) | ||
203 | goto err; | ||
204 | } | ||
205 | |||
206 | if (clk->ops && clk->ops->enable) { | ||
207 | ret = clk->ops->enable(clk); | ||
208 | if (ret) { | ||
209 | if (clk->parent) | ||
210 | __clk_disable(clk->parent); | ||
211 | goto err; | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | |||
216 | return ret; | ||
217 | err: | ||
218 | clk->usecount--; | ||
219 | return ret; | ||
220 | } | ||
221 | |||
222 | int clk_enable(struct clk *clk) | ||
223 | { | ||
224 | unsigned long flags; | ||
225 | int ret; | ||
226 | |||
227 | if (!clk) | ||
228 | return -EINVAL; | ||
229 | |||
230 | spin_lock_irqsave(&clock_lock, flags); | ||
231 | ret = __clk_enable(clk); | ||
232 | spin_unlock_irqrestore(&clock_lock, flags); | ||
233 | |||
234 | return ret; | ||
235 | } | ||
236 | EXPORT_SYMBOL_GPL(clk_enable); | ||
237 | |||
238 | static LIST_HEAD(root_clks); | ||
239 | |||
240 | /** | ||
241 | * recalculate_root_clocks - recalculate and propagate all root clocks | ||
242 | * | ||
243 | * Recalculates all root clocks (clocks with no parent), which if the | ||
244 | * clock's .recalc is set correctly, should also propagate their rates. | ||
245 | * Called at init. | ||
246 | */ | ||
247 | void recalculate_root_clocks(void) | ||
248 | { | ||
249 | struct clk *clkp; | ||
250 | |||
251 | list_for_each_entry(clkp, &root_clks, sibling) { | ||
252 | if (clkp->ops && clkp->ops->recalc) | ||
253 | clkp->rate = clkp->ops->recalc(clkp); | ||
254 | propagate_rate(clkp); | ||
255 | } | ||
256 | } | ||
257 | |||
168 | int clk_register(struct clk *clk) | 258 | int clk_register(struct clk *clk) |
169 | { | 259 | { |
260 | if (clk == NULL || IS_ERR(clk)) | ||
261 | return -EINVAL; | ||
262 | |||
263 | /* | ||
264 | * trap out already registered clocks | ||
265 | */ | ||
266 | if (clk->node.next || clk->node.prev) | ||
267 | return 0; | ||
268 | |||
170 | mutex_lock(&clock_list_sem); | 269 | mutex_lock(&clock_list_sem); |
171 | 270 | ||
172 | list_add(&clk->node, &clock_list); | 271 | INIT_LIST_HEAD(&clk->children); |
173 | kref_init(&clk->kref); | 272 | clk->usecount = 0; |
174 | 273 | ||
175 | mutex_unlock(&clock_list_sem); | 274 | if (clk->parent) |
275 | list_add(&clk->sibling, &clk->parent->children); | ||
276 | else | ||
277 | list_add(&clk->sibling, &root_clks); | ||
176 | 278 | ||
177 | if (clk->flags & CLK_ALWAYS_ENABLED) { | 279 | list_add(&clk->node, &clock_list); |
178 | pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name); | 280 | if (clk->ops && clk->ops->init) |
179 | if (clk->ops && clk->ops->init) | 281 | clk->ops->init(clk); |
180 | clk->ops->init(clk); | 282 | mutex_unlock(&clock_list_sem); |
181 | if (clk->ops && clk->ops->enable) | ||
182 | clk->ops->enable(clk); | ||
183 | pr_debug( "Enabled."); | ||
184 | } | ||
185 | 283 | ||
186 | return 0; | 284 | return 0; |
187 | } | 285 | } |
@@ -190,11 +288,21 @@ EXPORT_SYMBOL_GPL(clk_register); | |||
190 | void clk_unregister(struct clk *clk) | 288 | void clk_unregister(struct clk *clk) |
191 | { | 289 | { |
192 | mutex_lock(&clock_list_sem); | 290 | mutex_lock(&clock_list_sem); |
291 | list_del(&clk->sibling); | ||
193 | list_del(&clk->node); | 292 | list_del(&clk->node); |
194 | mutex_unlock(&clock_list_sem); | 293 | mutex_unlock(&clock_list_sem); |
195 | } | 294 | } |
196 | EXPORT_SYMBOL_GPL(clk_unregister); | 295 | EXPORT_SYMBOL_GPL(clk_unregister); |
197 | 296 | ||
297 | static void clk_enable_init_clocks(void) | ||
298 | { | ||
299 | struct clk *clkp; | ||
300 | |||
301 | list_for_each_entry(clkp, &clock_list, node) | ||
302 | if (clkp->flags & CLK_ENABLE_ON_INIT) | ||
303 | clk_enable(clkp); | ||
304 | } | ||
305 | |||
198 | unsigned long clk_get_rate(struct clk *clk) | 306 | unsigned long clk_get_rate(struct clk *clk) |
199 | { | 307 | { |
200 | return clk->rate; | 308 | return clk->rate; |
@@ -210,56 +318,59 @@ EXPORT_SYMBOL_GPL(clk_set_rate); | |||
210 | int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id) | 318 | int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id) |
211 | { | 319 | { |
212 | int ret = -EOPNOTSUPP; | 320 | int ret = -EOPNOTSUPP; |
321 | unsigned long flags; | ||
213 | 322 | ||
214 | if (likely(clk->ops && clk->ops->set_rate)) { | 323 | spin_lock_irqsave(&clock_lock, flags); |
215 | unsigned long flags; | ||
216 | 324 | ||
217 | spin_lock_irqsave(&clock_lock, flags); | 325 | if (likely(clk->ops && clk->ops->set_rate)) { |
218 | ret = clk->ops->set_rate(clk, rate, algo_id); | 326 | ret = clk->ops->set_rate(clk, rate, algo_id); |
219 | spin_unlock_irqrestore(&clock_lock, flags); | 327 | if (ret != 0) |
328 | goto out_unlock; | ||
329 | } else { | ||
330 | clk->rate = rate; | ||
331 | ret = 0; | ||
220 | } | 332 | } |
221 | 333 | ||
222 | if (unlikely(clk->flags & CLK_RATE_PROPAGATES)) | 334 | if (clk->ops && clk->ops->recalc) |
223 | propagate_rate(clk); | 335 | clk->rate = clk->ops->recalc(clk); |
224 | 336 | ||
225 | return ret; | 337 | propagate_rate(clk); |
226 | } | ||
227 | EXPORT_SYMBOL_GPL(clk_set_rate_ex); | ||
228 | 338 | ||
229 | void clk_recalc_rate(struct clk *clk) | 339 | out_unlock: |
230 | { | 340 | spin_unlock_irqrestore(&clock_lock, flags); |
231 | if (likely(clk->ops && clk->ops->recalc)) { | ||
232 | unsigned long flags; | ||
233 | |||
234 | spin_lock_irqsave(&clock_lock, flags); | ||
235 | clk->ops->recalc(clk); | ||
236 | spin_unlock_irqrestore(&clock_lock, flags); | ||
237 | } | ||
238 | 341 | ||
239 | if (unlikely(clk->flags & CLK_RATE_PROPAGATES)) | 342 | return ret; |
240 | propagate_rate(clk); | ||
241 | } | 343 | } |
242 | EXPORT_SYMBOL_GPL(clk_recalc_rate); | 344 | EXPORT_SYMBOL_GPL(clk_set_rate_ex); |
243 | 345 | ||
244 | int clk_set_parent(struct clk *clk, struct clk *parent) | 346 | int clk_set_parent(struct clk *clk, struct clk *parent) |
245 | { | 347 | { |
348 | unsigned long flags; | ||
246 | int ret = -EINVAL; | 349 | int ret = -EINVAL; |
247 | struct clk *old; | ||
248 | 350 | ||
249 | if (!parent || !clk) | 351 | if (!parent || !clk) |
250 | return ret; | 352 | return ret; |
353 | if (clk->parent == parent) | ||
354 | return 0; | ||
251 | 355 | ||
252 | old = clk->parent; | 356 | spin_lock_irqsave(&clock_lock, flags); |
253 | if (likely(clk->ops && clk->ops->set_parent)) { | 357 | if (clk->usecount == 0) { |
254 | unsigned long flags; | 358 | if (clk->ops->set_parent) |
255 | spin_lock_irqsave(&clock_lock, flags); | 359 | ret = clk->ops->set_parent(clk, parent); |
256 | ret = clk->ops->set_parent(clk, parent); | 360 | else |
257 | spin_unlock_irqrestore(&clock_lock, flags); | 361 | ret = clk_reparent(clk, parent); |
258 | clk->parent = (ret ? old : parent); | 362 | |
259 | } | 363 | if (ret == 0) { |
364 | pr_debug("clock: set parent of %s to %s (new rate %ld)\n", | ||
365 | clk->name, clk->parent->name, clk->rate); | ||
366 | if (clk->ops->recalc) | ||
367 | clk->rate = clk->ops->recalc(clk); | ||
368 | propagate_rate(clk); | ||
369 | } | ||
370 | } else | ||
371 | ret = -EBUSY; | ||
372 | spin_unlock_irqrestore(&clock_lock, flags); | ||
260 | 373 | ||
261 | if (unlikely(clk->flags & CLK_RATE_PROPAGATES)) | ||
262 | propagate_rate(clk); | ||
263 | return ret; | 374 | return ret; |
264 | } | 375 | } |
265 | EXPORT_SYMBOL_GPL(clk_set_parent); | 376 | EXPORT_SYMBOL_GPL(clk_set_parent); |
@@ -287,14 +398,69 @@ long clk_round_rate(struct clk *clk, unsigned long rate) | |||
287 | EXPORT_SYMBOL_GPL(clk_round_rate); | 398 | EXPORT_SYMBOL_GPL(clk_round_rate); |
288 | 399 | ||
289 | /* | 400 | /* |
401 | * Find the correct struct clk for the device and connection ID. | ||
402 | * We do slightly fuzzy matching here: | ||
403 | * An entry with a NULL ID is assumed to be a wildcard. | ||
404 | * If an entry has a device ID, it must match | ||
405 | * If an entry has a connection ID, it must match | ||
406 | * Then we take the most specific entry - with the following | ||
407 | * order of precidence: dev+con > dev only > con only. | ||
408 | */ | ||
409 | static struct clk *clk_find(const char *dev_id, const char *con_id) | ||
410 | { | ||
411 | struct clk_lookup *p; | ||
412 | struct clk *clk = NULL; | ||
413 | int match, best = 0; | ||
414 | |||
415 | list_for_each_entry(p, &clock_list, node) { | ||
416 | match = 0; | ||
417 | if (p->dev_id) { | ||
418 | if (!dev_id || strcmp(p->dev_id, dev_id)) | ||
419 | continue; | ||
420 | match += 2; | ||
421 | } | ||
422 | if (p->con_id) { | ||
423 | if (!con_id || strcmp(p->con_id, con_id)) | ||
424 | continue; | ||
425 | match += 1; | ||
426 | } | ||
427 | if (match == 0) | ||
428 | continue; | ||
429 | |||
430 | if (match > best) { | ||
431 | clk = p->clk; | ||
432 | best = match; | ||
433 | } | ||
434 | } | ||
435 | return clk; | ||
436 | } | ||
437 | |||
438 | struct clk *clk_get_sys(const char *dev_id, const char *con_id) | ||
439 | { | ||
440 | struct clk *clk; | ||
441 | |||
442 | mutex_lock(&clock_list_sem); | ||
443 | clk = clk_find(dev_id, con_id); | ||
444 | mutex_unlock(&clock_list_sem); | ||
445 | |||
446 | return clk ? clk : ERR_PTR(-ENOENT); | ||
447 | } | ||
448 | EXPORT_SYMBOL_GPL(clk_get_sys); | ||
449 | |||
450 | /* | ||
290 | * Returns a clock. Note that we first try to use device id on the bus | 451 | * Returns a clock. Note that we first try to use device id on the bus |
291 | * and clock name. If this fails, we try to use clock name only. | 452 | * and clock name. If this fails, we try to use clock name only. |
292 | */ | 453 | */ |
293 | struct clk *clk_get(struct device *dev, const char *id) | 454 | struct clk *clk_get(struct device *dev, const char *id) |
294 | { | 455 | { |
456 | const char *dev_id = dev ? dev_name(dev) : NULL; | ||
295 | struct clk *p, *clk = ERR_PTR(-ENOENT); | 457 | struct clk *p, *clk = ERR_PTR(-ENOENT); |
296 | int idno; | 458 | int idno; |
297 | 459 | ||
460 | clk = clk_get_sys(dev_id, id); | ||
461 | if (clk && !IS_ERR(clk)) | ||
462 | return clk; | ||
463 | |||
298 | if (dev == NULL || dev->bus != &platform_bus_type) | 464 | if (dev == NULL || dev->bus != &platform_bus_type) |
299 | idno = -1; | 465 | idno = -1; |
300 | else | 466 | else |
@@ -330,36 +496,6 @@ void clk_put(struct clk *clk) | |||
330 | } | 496 | } |
331 | EXPORT_SYMBOL_GPL(clk_put); | 497 | EXPORT_SYMBOL_GPL(clk_put); |
332 | 498 | ||
333 | void __init __attribute__ ((weak)) | ||
334 | arch_init_clk_ops(struct clk_ops **ops, int type) | ||
335 | { | ||
336 | } | ||
337 | |||
338 | int __init __attribute__ ((weak)) | ||
339 | arch_clk_init(void) | ||
340 | { | ||
341 | return 0; | ||
342 | } | ||
343 | |||
344 | static int show_clocks(char *buf, char **start, off_t off, | ||
345 | int len, int *eof, void *data) | ||
346 | { | ||
347 | struct clk *clk; | ||
348 | char *p = buf; | ||
349 | |||
350 | list_for_each_entry_reverse(clk, &clock_list, node) { | ||
351 | unsigned long rate = clk_get_rate(clk); | ||
352 | |||
353 | p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name, | ||
354 | rate / 1000000, (rate % 1000000) / 10000, | ||
355 | ((clk->flags & CLK_ALWAYS_ENABLED) || | ||
356 | (atomic_read(&clk->kref.refcount) != 1)) ? | ||
357 | "enabled" : "disabled"); | ||
358 | } | ||
359 | |||
360 | return p - buf; | ||
361 | } | ||
362 | |||
363 | #ifdef CONFIG_PM | 499 | #ifdef CONFIG_PM |
364 | static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state) | 500 | static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state) |
365 | { | 501 | { |
@@ -369,20 +505,22 @@ static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state) | |||
369 | switch (state.event) { | 505 | switch (state.event) { |
370 | case PM_EVENT_ON: | 506 | case PM_EVENT_ON: |
371 | /* Resumeing from hibernation */ | 507 | /* Resumeing from hibernation */ |
372 | if (prev_state.event == PM_EVENT_FREEZE) { | 508 | if (prev_state.event != PM_EVENT_FREEZE) |
373 | list_for_each_entry(clkp, &clock_list, node) | 509 | break; |
374 | if (likely(clkp->ops)) { | 510 | |
375 | unsigned long rate = clkp->rate; | 511 | list_for_each_entry(clkp, &clock_list, node) { |
376 | 512 | if (likely(clkp->ops)) { | |
377 | if (likely(clkp->ops->set_parent)) | 513 | unsigned long rate = clkp->rate; |
378 | clkp->ops->set_parent(clkp, | 514 | |
379 | clkp->parent); | 515 | if (likely(clkp->ops->set_parent)) |
380 | if (likely(clkp->ops->set_rate)) | 516 | clkp->ops->set_parent(clkp, |
381 | clkp->ops->set_rate(clkp, | 517 | clkp->parent); |
382 | rate, NO_CHANGE); | 518 | if (likely(clkp->ops->set_rate)) |
383 | else if (likely(clkp->ops->recalc)) | 519 | clkp->ops->set_rate(clkp, |
384 | clkp->ops->recalc(clkp); | 520 | rate, NO_CHANGE); |
385 | } | 521 | else if (likely(clkp->ops->recalc)) |
522 | clkp->rate = clkp->ops->recalc(clkp); | ||
523 | } | ||
386 | } | 524 | } |
387 | break; | 525 | break; |
388 | case PM_EVENT_FREEZE: | 526 | case PM_EVENT_FREEZE: |
@@ -426,34 +564,116 @@ subsys_initcall(clk_sysdev_init); | |||
426 | 564 | ||
427 | int __init clk_init(void) | 565 | int __init clk_init(void) |
428 | { | 566 | { |
429 | int i, ret = 0; | 567 | int ret; |
430 | |||
431 | BUG_ON(!master_clk.rate); | ||
432 | |||
433 | for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) { | ||
434 | struct clk *clk = onchip_clocks[i]; | ||
435 | 568 | ||
436 | arch_init_clk_ops(&clk->ops, i); | 569 | ret = arch_clk_init(); |
437 | ret |= clk_register(clk); | 570 | if (unlikely(ret)) { |
571 | pr_err("%s: CPU clock registration failed.\n", __func__); | ||
572 | return ret; | ||
438 | } | 573 | } |
439 | 574 | ||
440 | ret |= arch_clk_init(); | 575 | if (sh_mv.mv_clk_init) { |
576 | ret = sh_mv.mv_clk_init(); | ||
577 | if (unlikely(ret)) { | ||
578 | pr_err("%s: machvec clock initialization failed.\n", | ||
579 | __func__); | ||
580 | return ret; | ||
581 | } | ||
582 | } | ||
441 | 583 | ||
442 | /* Kick the child clocks.. */ | 584 | /* Kick the child clocks.. */ |
443 | propagate_rate(&master_clk); | 585 | recalculate_root_clocks(); |
444 | propagate_rate(&bus_clk); | 586 | |
587 | /* Enable the necessary init clocks */ | ||
588 | clk_enable_init_clocks(); | ||
445 | 589 | ||
446 | return ret; | 590 | return ret; |
447 | } | 591 | } |
448 | 592 | ||
449 | static int __init clk_proc_init(void) | 593 | /* |
594 | * debugfs support to trace clock tree hierarchy and attributes | ||
595 | */ | ||
596 | static struct dentry *clk_debugfs_root; | ||
597 | |||
598 | static int clk_debugfs_register_one(struct clk *c) | ||
450 | { | 599 | { |
451 | struct proc_dir_entry *p; | 600 | int err; |
452 | p = create_proc_read_entry("clocks", S_IRUSR, NULL, | 601 | struct dentry *d, *child; |
453 | show_clocks, NULL); | 602 | struct clk *pa = c->parent; |
454 | if (unlikely(!p)) | 603 | char s[255]; |
455 | return -EINVAL; | 604 | char *p = s; |
605 | |||
606 | p += sprintf(p, "%s", c->name); | ||
607 | if (c->id >= 0) | ||
608 | sprintf(p, ":%d", c->id); | ||
609 | d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root); | ||
610 | if (!d) | ||
611 | return -ENOMEM; | ||
612 | c->dentry = d; | ||
613 | |||
614 | d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount); | ||
615 | if (!d) { | ||
616 | err = -ENOMEM; | ||
617 | goto err_out; | ||
618 | } | ||
619 | d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate); | ||
620 | if (!d) { | ||
621 | err = -ENOMEM; | ||
622 | goto err_out; | ||
623 | } | ||
624 | d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags); | ||
625 | if (!d) { | ||
626 | err = -ENOMEM; | ||
627 | goto err_out; | ||
628 | } | ||
629 | return 0; | ||
456 | 630 | ||
631 | err_out: | ||
632 | d = c->dentry; | ||
633 | list_for_each_entry(child, &d->d_subdirs, d_u.d_child) | ||
634 | debugfs_remove(child); | ||
635 | debugfs_remove(c->dentry); | ||
636 | return err; | ||
637 | } | ||
638 | |||
639 | static int clk_debugfs_register(struct clk *c) | ||
640 | { | ||
641 | int err; | ||
642 | struct clk *pa = c->parent; | ||
643 | |||
644 | if (pa && !pa->dentry) { | ||
645 | err = clk_debugfs_register(pa); | ||
646 | if (err) | ||
647 | return err; | ||
648 | } | ||
649 | |||
650 | if (!c->dentry) { | ||
651 | err = clk_debugfs_register_one(c); | ||
652 | if (err) | ||
653 | return err; | ||
654 | } | ||
655 | return 0; | ||
656 | } | ||
657 | |||
658 | static int __init clk_debugfs_init(void) | ||
659 | { | ||
660 | struct clk *c; | ||
661 | struct dentry *d; | ||
662 | int err; | ||
663 | |||
664 | d = debugfs_create_dir("clock", NULL); | ||
665 | if (!d) | ||
666 | return -ENOMEM; | ||
667 | clk_debugfs_root = d; | ||
668 | |||
669 | list_for_each_entry(c, &clock_list, node) { | ||
670 | err = clk_debugfs_register(c); | ||
671 | if (err) | ||
672 | goto err_out; | ||
673 | } | ||
457 | return 0; | 674 | return 0; |
675 | err_out: | ||
676 | debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */ | ||
677 | return err; | ||
458 | } | 678 | } |
459 | subsys_initcall(clk_proc_init); | 679 | late_initcall(clk_debugfs_init); |