2 * Helper routines for SuperH Clock Pulse Generator blocks (CPG).
4 * Copyright (C) 2010 Magnus Damm
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/clk.h>
11 #include <linux/compiler.h>
12 #include <linux/slab.h>
14 #include <linux/sh_clk.h>
16 static int sh_clk_mstp32_enable(struct clk *clk)
18 iowrite32(ioread32(clk->mapped_reg) & ~(1 << clk->enable_bit),
23 static void sh_clk_mstp32_disable(struct clk *clk)
25 iowrite32(ioread32(clk->mapped_reg) | (1 << clk->enable_bit),
29 static struct clk_ops sh_clk_mstp32_clk_ops = {
30 .enable = sh_clk_mstp32_enable,
31 .disable = sh_clk_mstp32_disable,
32 .recalc = followparent_recalc,
35 int __init sh_clk_mstp32_register(struct clk *clks, int nr)
41 for (k = 0; !ret && (k < nr); k++) {
43 clkp->ops = &sh_clk_mstp32_clk_ops;
44 ret |= clk_register(clkp);
50 static long sh_clk_div_round_rate(struct clk *clk, unsigned long rate)
52 return clk_rate_table_round(clk, clk->freq_table, rate);
55 static int sh_clk_div6_divisors[64] = {
56 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
57 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
58 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
59 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
62 static struct clk_div_mult_table sh_clk_div6_table = {
63 .divisors = sh_clk_div6_divisors,
64 .nr_divisors = ARRAY_SIZE(sh_clk_div6_divisors),
67 static unsigned long sh_clk_div6_recalc(struct clk *clk)
69 struct clk_div_mult_table *table = &sh_clk_div6_table;
72 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
75 idx = ioread32(clk->mapped_reg) & 0x003f;
77 return clk->freq_table[idx].frequency;
80 static int sh_clk_div6_set_parent(struct clk *clk, struct clk *parent)
82 struct clk_div_mult_table *table = &sh_clk_div6_table;
86 if (!clk->parent_table || !clk->parent_num)
89 /* Search the parent */
90 for (i = 0; i < clk->parent_num; i++)
91 if (clk->parent_table[i] == parent)
94 if (i == clk->parent_num)
97 ret = clk_reparent(clk, parent);
101 value = ioread32(clk->mapped_reg) &
102 ~(((1 << clk->src_width) - 1) << clk->src_shift);
104 iowrite32(value | (i << clk->src_shift), clk->mapped_reg);
106 /* Rebuild the frequency table */
107 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
113 static int sh_clk_div6_set_rate(struct clk *clk, unsigned long rate)
118 idx = clk_rate_table_find(clk, clk->freq_table, rate);
122 value = ioread32(clk->mapped_reg);
125 iowrite32(value, clk->mapped_reg);
129 static int sh_clk_div6_enable(struct clk *clk)
134 ret = sh_clk_div6_set_rate(clk, clk->rate);
136 value = ioread32(clk->mapped_reg);
137 value &= ~0x100; /* clear stop bit to enable clock */
138 iowrite32(value, clk->mapped_reg);
143 static void sh_clk_div6_disable(struct clk *clk)
147 value = ioread32(clk->mapped_reg);
148 value |= 0x100; /* stop clock */
149 value |= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
150 iowrite32(value, clk->mapped_reg);
153 static struct clk_ops sh_clk_div6_clk_ops = {
154 .recalc = sh_clk_div6_recalc,
155 .round_rate = sh_clk_div_round_rate,
156 .set_rate = sh_clk_div6_set_rate,
157 .enable = sh_clk_div6_enable,
158 .disable = sh_clk_div6_disable,
161 static struct clk_ops sh_clk_div6_reparent_clk_ops = {
162 .recalc = sh_clk_div6_recalc,
163 .round_rate = sh_clk_div_round_rate,
164 .set_rate = sh_clk_div6_set_rate,
165 .enable = sh_clk_div6_enable,
166 .disable = sh_clk_div6_disable,
167 .set_parent = sh_clk_div6_set_parent,
170 static int __init sh_clk_init_parent(struct clk *clk)
177 if (!clk->parent_table || !clk->parent_num)
180 if (!clk->src_width) {
181 pr_err("sh_clk_init_parent: cannot select parent clock\n");
185 val = (ioread32(clk->mapped_reg) >> clk->src_shift);
186 val &= (1 << clk->src_width) - 1;
188 if (val >= clk->parent_num) {
189 pr_err("sh_clk_init_parent: parent table size failed\n");
193 clk_reparent(clk, clk->parent_table[val]);
195 pr_err("sh_clk_init_parent: unable to set parent");
202 static int __init sh_clk_div6_register_ops(struct clk *clks, int nr,
207 int nr_divs = sh_clk_div6_table.nr_divisors;
208 int freq_table_size = sizeof(struct cpufreq_frequency_table);
212 freq_table_size *= (nr_divs + 1);
213 freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
215 pr_err("sh_clk_div6_register: unable to alloc memory\n");
219 for (k = 0; !ret && (k < nr); k++) {
223 clkp->freq_table = freq_table + (k * freq_table_size);
224 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
225 ret = clk_register(clkp);
229 ret = sh_clk_init_parent(clkp);
235 int __init sh_clk_div6_register(struct clk *clks, int nr)
237 return sh_clk_div6_register_ops(clks, nr, &sh_clk_div6_clk_ops);
240 int __init sh_clk_div6_reparent_register(struct clk *clks, int nr)
242 return sh_clk_div6_register_ops(clks, nr,
243 &sh_clk_div6_reparent_clk_ops);
246 static unsigned long sh_clk_div4_recalc(struct clk *clk)
248 struct clk_div4_table *d4t = clk->priv;
249 struct clk_div_mult_table *table = d4t->div_mult_table;
252 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
253 table, &clk->arch_flags);
255 idx = (ioread32(clk->mapped_reg) >> clk->enable_bit) & 0x000f;
257 return clk->freq_table[idx].frequency;
260 static int sh_clk_div4_set_parent(struct clk *clk, struct clk *parent)
262 struct clk_div4_table *d4t = clk->priv;
263 struct clk_div_mult_table *table = d4t->div_mult_table;
267 /* we really need a better way to determine parent index, but for
268 * now assume internal parent comes with CLK_ENABLE_ON_INIT set,
269 * no CLK_ENABLE_ON_INIT means external clock...
272 if (parent->flags & CLK_ENABLE_ON_INIT)
273 value = ioread32(clk->mapped_reg) & ~(1 << 7);
275 value = ioread32(clk->mapped_reg) | (1 << 7);
277 ret = clk_reparent(clk, parent);
281 iowrite32(value, clk->mapped_reg);
283 /* Rebiuld the frequency table */
284 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
285 table, &clk->arch_flags);
290 static int sh_clk_div4_set_rate(struct clk *clk, unsigned long rate)
292 struct clk_div4_table *d4t = clk->priv;
294 int idx = clk_rate_table_find(clk, clk->freq_table, rate);
298 value = ioread32(clk->mapped_reg);
299 value &= ~(0xf << clk->enable_bit);
300 value |= (idx << clk->enable_bit);
301 iowrite32(value, clk->mapped_reg);
309 static int sh_clk_div4_enable(struct clk *clk)
311 iowrite32(ioread32(clk->mapped_reg) & ~(1 << 8), clk->mapped_reg);
315 static void sh_clk_div4_disable(struct clk *clk)
317 iowrite32(ioread32(clk->mapped_reg) | (1 << 8), clk->mapped_reg);
320 static struct clk_ops sh_clk_div4_clk_ops = {
321 .recalc = sh_clk_div4_recalc,
322 .set_rate = sh_clk_div4_set_rate,
323 .round_rate = sh_clk_div_round_rate,
326 static struct clk_ops sh_clk_div4_enable_clk_ops = {
327 .recalc = sh_clk_div4_recalc,
328 .set_rate = sh_clk_div4_set_rate,
329 .round_rate = sh_clk_div_round_rate,
330 .enable = sh_clk_div4_enable,
331 .disable = sh_clk_div4_disable,
334 static struct clk_ops sh_clk_div4_reparent_clk_ops = {
335 .recalc = sh_clk_div4_recalc,
336 .set_rate = sh_clk_div4_set_rate,
337 .round_rate = sh_clk_div_round_rate,
338 .enable = sh_clk_div4_enable,
339 .disable = sh_clk_div4_disable,
340 .set_parent = sh_clk_div4_set_parent,
343 static int __init sh_clk_div4_register_ops(struct clk *clks, int nr,
344 struct clk_div4_table *table, struct clk_ops *ops)
348 int nr_divs = table->div_mult_table->nr_divisors;
349 int freq_table_size = sizeof(struct cpufreq_frequency_table);
353 freq_table_size *= (nr_divs + 1);
354 freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
356 pr_err("sh_clk_div4_register: unable to alloc memory\n");
360 for (k = 0; !ret && (k < nr); k++) {
366 clkp->freq_table = freq_table + (k * freq_table_size);
367 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
369 ret = clk_register(clkp);
375 int __init sh_clk_div4_register(struct clk *clks, int nr,
376 struct clk_div4_table *table)
378 return sh_clk_div4_register_ops(clks, nr, table, &sh_clk_div4_clk_ops);
381 int __init sh_clk_div4_enable_register(struct clk *clks, int nr,
382 struct clk_div4_table *table)
384 return sh_clk_div4_register_ops(clks, nr, table,
385 &sh_clk_div4_enable_clk_ops);
388 int __init sh_clk_div4_reparent_register(struct clk *clks, int nr,
389 struct clk_div4_table *table)
391 return sh_clk_div4_register_ops(clks, nr, table,
392 &sh_clk_div4_reparent_clk_ops);