blob: 0b165f6ae08c12da8c56968fa6d5462537785d44 [file] [log] [blame]
Paul Mundt253b0882009-05-13 17:38:11 +09001#include <linux/clk.h>
2#include <linux/compiler.h>
Magnus Damm4c7eb4e2009-06-17 04:55:42 +00003#include <linux/slab.h>
Magnus Damm6881e8b2009-05-28 12:52:29 +00004#include <linux/io.h>
Paul Mundt253b0882009-05-13 17:38:11 +09005#include <asm/clock.h>
6
Magnus Damm6881e8b2009-05-28 12:52:29 +00007static int sh_clk_mstp32_enable(struct clk *clk)
8{
9 __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << clk->enable_bit),
10 clk->enable_reg);
11 return 0;
12}
13
14static void sh_clk_mstp32_disable(struct clk *clk)
15{
16 __raw_writel(__raw_readl(clk->enable_reg) | (1 << clk->enable_bit),
17 clk->enable_reg);
18}
19
20static struct clk_ops sh_clk_mstp32_clk_ops = {
21 .enable = sh_clk_mstp32_enable,
22 .disable = sh_clk_mstp32_disable,
23 .recalc = followparent_recalc,
24};
25
26int __init sh_clk_mstp32_register(struct clk *clks, int nr)
27{
28 struct clk *clkp;
29 int ret = 0;
30 int k;
31
32 for (k = 0; !ret && (k < nr); k++) {
33 clkp = clks + k;
34 clkp->ops = &sh_clk_mstp32_clk_ops;
35 ret |= clk_register(clkp);
36 }
37
38 return ret;
39}
40
Magnus Damm2693e272009-06-02 08:53:54 +000041static long sh_clk_div_round_rate(struct clk *clk, unsigned long rate)
42{
43 return clk_rate_table_round(clk, clk->freq_table, rate);
44}
45
46static int sh_clk_div6_divisors[64] = {
47 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
48 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
49 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
50 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
51};
52
53static struct clk_div_mult_table sh_clk_div6_table = {
54 .divisors = sh_clk_div6_divisors,
55 .nr_divisors = ARRAY_SIZE(sh_clk_div6_divisors),
56};
57
58static unsigned long sh_clk_div6_recalc(struct clk *clk)
59{
60 struct clk_div_mult_table *table = &sh_clk_div6_table;
61 unsigned int idx;
62
63 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
64 table, NULL);
65
66 idx = __raw_readl(clk->enable_reg) & 0x003f;
67
68 return clk->freq_table[idx].frequency;
69}
70
Magnus Damm098dee92009-06-04 05:31:41 +000071static int sh_clk_div6_set_rate(struct clk *clk,
72 unsigned long rate, int algo_id)
73{
74 unsigned long value;
75 int idx;
76
77 idx = clk_rate_table_find(clk, clk->freq_table, rate);
78 if (idx < 0)
79 return idx;
80
81 value = __raw_readl(clk->enable_reg);
82 value &= ~0x3f;
83 value |= idx;
84 __raw_writel(value, clk->enable_reg);
85 return 0;
86}
87
88static int sh_clk_div6_enable(struct clk *clk)
89{
90 unsigned long value;
91 int ret;
92
93 ret = sh_clk_div6_set_rate(clk, clk->rate, 0);
94 if (ret == 0) {
95 value = __raw_readl(clk->enable_reg);
96 value &= ~0x100; /* clear stop bit to enable clock */
97 __raw_writel(value, clk->enable_reg);
98 }
99 return ret;
100}
101
102static void sh_clk_div6_disable(struct clk *clk)
103{
104 unsigned long value;
105
106 value = __raw_readl(clk->enable_reg);
107 value |= 0x100; /* stop clock */
108 value |= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
109 __raw_writel(value, clk->enable_reg);
110}
111
Magnus Damm2693e272009-06-02 08:53:54 +0000112static struct clk_ops sh_clk_div6_clk_ops = {
113 .recalc = sh_clk_div6_recalc,
114 .round_rate = sh_clk_div_round_rate,
Magnus Damm098dee92009-06-04 05:31:41 +0000115 .set_rate = sh_clk_div6_set_rate,
116 .enable = sh_clk_div6_enable,
117 .disable = sh_clk_div6_disable,
Magnus Damm2693e272009-06-02 08:53:54 +0000118};
119
120int __init sh_clk_div6_register(struct clk *clks, int nr)
121{
122 struct clk *clkp;
123 void *freq_table;
124 int nr_divs = sh_clk_div6_table.nr_divisors;
125 int freq_table_size = sizeof(struct cpufreq_frequency_table);
126 int ret = 0;
127 int k;
128
129 freq_table_size *= (nr_divs + 1);
Magnus Damm4c7eb4e2009-06-17 04:55:42 +0000130 freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
131 if (!freq_table) {
132 pr_err("sh_clk_div6_register: unable to alloc memory\n");
Magnus Damm2693e272009-06-02 08:53:54 +0000133 return -ENOMEM;
Magnus Damm4c7eb4e2009-06-17 04:55:42 +0000134 }
Magnus Damm2693e272009-06-02 08:53:54 +0000135
136 for (k = 0; !ret && (k < nr); k++) {
137 clkp = clks + k;
138
139 clkp->ops = &sh_clk_div6_clk_ops;
140 clkp->id = -1;
141 clkp->freq_table = freq_table + (k * freq_table_size);
142 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
143
144 ret = clk_register(clkp);
145 }
146
147 return ret;
148}
149
Magnus Damma1153e22009-05-28 13:11:31 +0000150static unsigned long sh_clk_div4_recalc(struct clk *clk)
151{
Magnus Damm0a5f3372010-02-19 09:22:25 +0000152 struct clk_div4_table *d4t = clk->priv;
153 struct clk_div_mult_table *table = d4t->div_mult_table;
Magnus Damma1153e22009-05-28 13:11:31 +0000154 unsigned int idx;
155
156 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
157 table, &clk->arch_flags);
158
159 idx = (__raw_readl(clk->enable_reg) >> clk->enable_bit) & 0x000f;
160
161 return clk->freq_table[idx].frequency;
162}
163
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000164static int sh_clk_div4_set_parent(struct clk *clk, struct clk *parent)
165{
Magnus Damm0a5f3372010-02-19 09:22:25 +0000166 struct clk_div4_table *d4t = clk->priv;
167 struct clk_div_mult_table *table = d4t->div_mult_table;
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000168 u32 value;
169 int ret;
170
Magnus Dammd40db0c2010-05-11 07:05:09 +0000171 /* we really need a better way to determine parent index, but for
172 * now assume internal parent comes with CLK_ENABLE_ON_INIT set,
173 * no CLK_ENABLE_ON_INIT means external clock...
174 */
175
176 if (parent->flags & CLK_ENABLE_ON_INIT)
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000177 value = __raw_readl(clk->enable_reg) & ~(1 << 7);
178 else
179 value = __raw_readl(clk->enable_reg) | (1 << 7);
180
181 ret = clk_reparent(clk, parent);
182 if (ret < 0)
183 return ret;
184
185 __raw_writel(value, clk->enable_reg);
186
187 /* Rebiuld the frequency table */
188 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
189 table, &clk->arch_flags);
190
191 return 0;
192}
193
194static int sh_clk_div4_set_rate(struct clk *clk, unsigned long rate, int algo_id)
195{
Magnus Damm7be85c62010-02-19 09:26:56 +0000196 struct clk_div4_table *d4t = clk->priv;
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000197 unsigned long value;
198 int idx = clk_rate_table_find(clk, clk->freq_table, rate);
199 if (idx < 0)
200 return idx;
201
202 value = __raw_readl(clk->enable_reg);
Magnus Dammde7ca212010-02-19 09:12:00 +0000203 value &= ~(0xf << clk->enable_bit);
204 value |= (idx << clk->enable_bit);
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000205 __raw_writel(value, clk->enable_reg);
206
Magnus Damm7be85c62010-02-19 09:26:56 +0000207 if (d4t->kick)
208 d4t->kick(clk);
209
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000210 return 0;
211}
212
213static int sh_clk_div4_enable(struct clk *clk)
214{
215 __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << 8), clk->enable_reg);
216 return 0;
217}
218
219static void sh_clk_div4_disable(struct clk *clk)
220{
221 __raw_writel(__raw_readl(clk->enable_reg) | (1 << 8), clk->enable_reg);
222}
223
Magnus Damma1153e22009-05-28 13:11:31 +0000224static struct clk_ops sh_clk_div4_clk_ops = {
225 .recalc = sh_clk_div4_recalc,
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000226 .set_rate = sh_clk_div4_set_rate,
Magnus Damm2693e272009-06-02 08:53:54 +0000227 .round_rate = sh_clk_div_round_rate,
Magnus Damma1153e22009-05-28 13:11:31 +0000228};
229
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000230static struct clk_ops sh_clk_div4_enable_clk_ops = {
231 .recalc = sh_clk_div4_recalc,
232 .set_rate = sh_clk_div4_set_rate,
233 .round_rate = sh_clk_div_round_rate,
234 .enable = sh_clk_div4_enable,
235 .disable = sh_clk_div4_disable,
236};
237
238static struct clk_ops sh_clk_div4_reparent_clk_ops = {
239 .recalc = sh_clk_div4_recalc,
240 .set_rate = sh_clk_div4_set_rate,
241 .round_rate = sh_clk_div_round_rate,
242 .enable = sh_clk_div4_enable,
243 .disable = sh_clk_div4_disable,
244 .set_parent = sh_clk_div4_set_parent,
245};
246
247static int __init sh_clk_div4_register_ops(struct clk *clks, int nr,
Magnus Damm0a5f3372010-02-19 09:22:25 +0000248 struct clk_div4_table *table, struct clk_ops *ops)
Magnus Damma1153e22009-05-28 13:11:31 +0000249{
250 struct clk *clkp;
251 void *freq_table;
Magnus Damm0a5f3372010-02-19 09:22:25 +0000252 int nr_divs = table->div_mult_table->nr_divisors;
Magnus Damma1153e22009-05-28 13:11:31 +0000253 int freq_table_size = sizeof(struct cpufreq_frequency_table);
254 int ret = 0;
255 int k;
256
Magnus Damma50de782009-06-02 08:43:59 +0000257 freq_table_size *= (nr_divs + 1);
Magnus Damm4c7eb4e2009-06-17 04:55:42 +0000258 freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
259 if (!freq_table) {
260 pr_err("sh_clk_div4_register: unable to alloc memory\n");
Magnus Damma1153e22009-05-28 13:11:31 +0000261 return -ENOMEM;
Magnus Damm4c7eb4e2009-06-17 04:55:42 +0000262 }
Magnus Damma1153e22009-05-28 13:11:31 +0000263
264 for (k = 0; !ret && (k < nr); k++) {
265 clkp = clks + k;
266
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000267 clkp->ops = ops;
Magnus Damma1153e22009-05-28 13:11:31 +0000268 clkp->id = -1;
269 clkp->priv = table;
270
271 clkp->freq_table = freq_table + (k * freq_table_size);
272 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
273
274 ret = clk_register(clkp);
275 }
276
277 return ret;
278}
279
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000280int __init sh_clk_div4_register(struct clk *clks, int nr,
Magnus Damm0a5f3372010-02-19 09:22:25 +0000281 struct clk_div4_table *table)
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000282{
283 return sh_clk_div4_register_ops(clks, nr, table, &sh_clk_div4_clk_ops);
284}
285
286int __init sh_clk_div4_enable_register(struct clk *clks, int nr,
Magnus Damm0a5f3372010-02-19 09:22:25 +0000287 struct clk_div4_table *table)
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000288{
289 return sh_clk_div4_register_ops(clks, nr, table,
290 &sh_clk_div4_enable_clk_ops);
291}
292
293int __init sh_clk_div4_reparent_register(struct clk *clks, int nr,
Magnus Damm0a5f3372010-02-19 09:22:25 +0000294 struct clk_div4_table *table)
Guennadi Liakhovetski31c3af52010-01-19 11:14:31 +0000295{
296 return sh_clk_div4_register_ops(clks, nr, table,
297 &sh_clk_div4_reparent_clk_ops);
298}
299
Paul Mundt36aa1e32009-05-22 14:00:34 +0900300#ifdef CONFIG_SH_CLK_CPG_LEGACY
Paul Mundt253b0882009-05-13 17:38:11 +0900301static struct clk master_clk = {
302 .name = "master_clk",
303 .flags = CLK_ENABLE_ON_INIT,
304 .rate = CONFIG_SH_PCLK_FREQ,
305};
306
307static struct clk peripheral_clk = {
308 .name = "peripheral_clk",
309 .parent = &master_clk,
310 .flags = CLK_ENABLE_ON_INIT,
311};
312
313static struct clk bus_clk = {
314 .name = "bus_clk",
315 .parent = &master_clk,
316 .flags = CLK_ENABLE_ON_INIT,
317};
318
319static struct clk cpu_clk = {
320 .name = "cpu_clk",
321 .parent = &master_clk,
322 .flags = CLK_ENABLE_ON_INIT,
323};
324
325/*
326 * The ordering of these clocks matters, do not change it.
327 */
328static struct clk *onchip_clocks[] = {
329 &master_clk,
330 &peripheral_clk,
331 &bus_clk,
332 &cpu_clk,
333};
334
335int __init __deprecated cpg_clk_init(void)
336{
337 int i, ret = 0;
338
339 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
340 struct clk *clk = onchip_clocks[i];
341 arch_init_clk_ops(&clk->ops, i);
342 if (clk->ops)
343 ret |= clk_register(clk);
344 }
345
Paul Mundt0776d602010-03-29 17:22:50 +0900346 clk_add_alias("tmu_fck", NULL, "peripheral_clk", NULL);
347 clk_add_alias("mtu2_fck", NULL, "peripheral_clk", NULL);
348 clk_add_alias("cmt_fck", NULL, "peripheral_clk", NULL);
349 clk_add_alias("sci_ick", NULL, "peripheral_clk", NULL);
350
Paul Mundt253b0882009-05-13 17:38:11 +0900351 return ret;
352}
353
354/*
355 * Placeholder for compatability, until the lazy CPUs do this
356 * on their own.
357 */
358int __init __weak arch_clk_init(void)
359{
360 return cpg_clk_init();
361}
Paul Mundt36aa1e32009-05-22 14:00:34 +0900362#endif /* CONFIG_SH_CPG_CLK_LEGACY */