hfs: get rid of hfs_sync_super
[linux/fpc-iii.git] / drivers / sh / clk / cpg.c
blobf0d015dd0fef20aa2795b7c4866e00e1e2e478a2
1 /*
2 * Helper routines for SuperH Clock Pulse Generator blocks (CPG).
4 * Copyright (C) 2010 Magnus Damm
5 * Copyright (C) 2010 - 2012 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
11 #include <linux/clk.h>
12 #include <linux/compiler.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 #include <linux/sh_clk.h>
17 static unsigned int sh_clk_read(struct clk *clk)
19 if (clk->flags & CLK_ENABLE_REG_8BIT)
20 return ioread8(clk->mapped_reg);
21 else if (clk->flags & CLK_ENABLE_REG_16BIT)
22 return ioread16(clk->mapped_reg);
24 return ioread32(clk->mapped_reg);
27 static void sh_clk_write(int value, struct clk *clk)
29 if (clk->flags & CLK_ENABLE_REG_8BIT)
30 iowrite8(value, clk->mapped_reg);
31 else if (clk->flags & CLK_ENABLE_REG_16BIT)
32 iowrite16(value, clk->mapped_reg);
33 else
34 iowrite32(value, clk->mapped_reg);
37 static int sh_clk_mstp_enable(struct clk *clk)
39 sh_clk_write(sh_clk_read(clk) & ~(1 << clk->enable_bit), clk);
40 return 0;
43 static void sh_clk_mstp_disable(struct clk *clk)
45 sh_clk_write(sh_clk_read(clk) | (1 << clk->enable_bit), clk);
48 static struct sh_clk_ops sh_clk_mstp_clk_ops = {
49 .enable = sh_clk_mstp_enable,
50 .disable = sh_clk_mstp_disable,
51 .recalc = followparent_recalc,
54 int __init sh_clk_mstp_register(struct clk *clks, int nr)
56 struct clk *clkp;
57 int ret = 0;
58 int k;
60 for (k = 0; !ret && (k < nr); k++) {
61 clkp = clks + k;
62 clkp->ops = &sh_clk_mstp_clk_ops;
63 ret |= clk_register(clkp);
66 return ret;
69 static long sh_clk_div_round_rate(struct clk *clk, unsigned long rate)
71 return clk_rate_table_round(clk, clk->freq_table, rate);
74 static int sh_clk_div6_divisors[64] = {
75 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
76 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
77 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
78 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
81 static struct clk_div_mult_table sh_clk_div6_table = {
82 .divisors = sh_clk_div6_divisors,
83 .nr_divisors = ARRAY_SIZE(sh_clk_div6_divisors),
86 static unsigned long sh_clk_div6_recalc(struct clk *clk)
88 struct clk_div_mult_table *table = &sh_clk_div6_table;
89 unsigned int idx;
91 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
92 table, NULL);
94 idx = sh_clk_read(clk) & 0x003f;
96 return clk->freq_table[idx].frequency;
99 static int sh_clk_div6_set_parent(struct clk *clk, struct clk *parent)
101 struct clk_div_mult_table *table = &sh_clk_div6_table;
102 u32 value;
103 int ret, i;
105 if (!clk->parent_table || !clk->parent_num)
106 return -EINVAL;
108 /* Search the parent */
109 for (i = 0; i < clk->parent_num; i++)
110 if (clk->parent_table[i] == parent)
111 break;
113 if (i == clk->parent_num)
114 return -ENODEV;
116 ret = clk_reparent(clk, parent);
117 if (ret < 0)
118 return ret;
120 value = sh_clk_read(clk) &
121 ~(((1 << clk->src_width) - 1) << clk->src_shift);
123 sh_clk_write(value | (i << clk->src_shift), clk);
125 /* Rebuild the frequency table */
126 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
127 table, NULL);
129 return 0;
132 static int sh_clk_div6_set_rate(struct clk *clk, unsigned long rate)
134 unsigned long value;
135 int idx;
137 idx = clk_rate_table_find(clk, clk->freq_table, rate);
138 if (idx < 0)
139 return idx;
141 value = sh_clk_read(clk);
142 value &= ~0x3f;
143 value |= idx;
144 sh_clk_write(value, clk);
145 return 0;
148 static int sh_clk_div6_enable(struct clk *clk)
150 unsigned long value;
151 int ret;
153 ret = sh_clk_div6_set_rate(clk, clk->rate);
154 if (ret == 0) {
155 value = sh_clk_read(clk);
156 value &= ~0x100; /* clear stop bit to enable clock */
157 sh_clk_write(value, clk);
159 return ret;
162 static void sh_clk_div6_disable(struct clk *clk)
164 unsigned long value;
166 value = sh_clk_read(clk);
167 value |= 0x100; /* stop clock */
168 value |= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
169 sh_clk_write(value, clk);
172 static struct sh_clk_ops sh_clk_div6_clk_ops = {
173 .recalc = sh_clk_div6_recalc,
174 .round_rate = sh_clk_div_round_rate,
175 .set_rate = sh_clk_div6_set_rate,
176 .enable = sh_clk_div6_enable,
177 .disable = sh_clk_div6_disable,
180 static struct sh_clk_ops sh_clk_div6_reparent_clk_ops = {
181 .recalc = sh_clk_div6_recalc,
182 .round_rate = sh_clk_div_round_rate,
183 .set_rate = sh_clk_div6_set_rate,
184 .enable = sh_clk_div6_enable,
185 .disable = sh_clk_div6_disable,
186 .set_parent = sh_clk_div6_set_parent,
189 static int __init sh_clk_init_parent(struct clk *clk)
191 u32 val;
193 if (clk->parent)
194 return 0;
196 if (!clk->parent_table || !clk->parent_num)
197 return 0;
199 if (!clk->src_width) {
200 pr_err("sh_clk_init_parent: cannot select parent clock\n");
201 return -EINVAL;
204 val = (sh_clk_read(clk) >> clk->src_shift);
205 val &= (1 << clk->src_width) - 1;
207 if (val >= clk->parent_num) {
208 pr_err("sh_clk_init_parent: parent table size failed\n");
209 return -EINVAL;
212 clk_reparent(clk, clk->parent_table[val]);
213 if (!clk->parent) {
214 pr_err("sh_clk_init_parent: unable to set parent");
215 return -EINVAL;
218 return 0;
221 static int __init sh_clk_div6_register_ops(struct clk *clks, int nr,
222 struct sh_clk_ops *ops)
224 struct clk *clkp;
225 void *freq_table;
226 int nr_divs = sh_clk_div6_table.nr_divisors;
227 int freq_table_size = sizeof(struct cpufreq_frequency_table);
228 int ret = 0;
229 int k;
231 freq_table_size *= (nr_divs + 1);
232 freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
233 if (!freq_table) {
234 pr_err("sh_clk_div6_register: unable to alloc memory\n");
235 return -ENOMEM;
238 for (k = 0; !ret && (k < nr); k++) {
239 clkp = clks + k;
241 clkp->ops = ops;
242 clkp->freq_table = freq_table + (k * freq_table_size);
243 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
244 ret = clk_register(clkp);
245 if (ret < 0)
246 break;
248 ret = sh_clk_init_parent(clkp);
251 return ret;
254 int __init sh_clk_div6_register(struct clk *clks, int nr)
256 return sh_clk_div6_register_ops(clks, nr, &sh_clk_div6_clk_ops);
259 int __init sh_clk_div6_reparent_register(struct clk *clks, int nr)
261 return sh_clk_div6_register_ops(clks, nr,
262 &sh_clk_div6_reparent_clk_ops);
265 static unsigned long sh_clk_div4_recalc(struct clk *clk)
267 struct clk_div4_table *d4t = clk->priv;
268 struct clk_div_mult_table *table = d4t->div_mult_table;
269 unsigned int idx;
271 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
272 table, &clk->arch_flags);
274 idx = (sh_clk_read(clk) >> clk->enable_bit) & 0x000f;
276 return clk->freq_table[idx].frequency;
279 static int sh_clk_div4_set_parent(struct clk *clk, struct clk *parent)
281 struct clk_div4_table *d4t = clk->priv;
282 struct clk_div_mult_table *table = d4t->div_mult_table;
283 u32 value;
284 int ret;
286 /* we really need a better way to determine parent index, but for
287 * now assume internal parent comes with CLK_ENABLE_ON_INIT set,
288 * no CLK_ENABLE_ON_INIT means external clock...
291 if (parent->flags & CLK_ENABLE_ON_INIT)
292 value = sh_clk_read(clk) & ~(1 << 7);
293 else
294 value = sh_clk_read(clk) | (1 << 7);
296 ret = clk_reparent(clk, parent);
297 if (ret < 0)
298 return ret;
300 sh_clk_write(value, clk);
302 /* Rebiuld the frequency table */
303 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
304 table, &clk->arch_flags);
306 return 0;
309 static int sh_clk_div4_set_rate(struct clk *clk, unsigned long rate)
311 struct clk_div4_table *d4t = clk->priv;
312 unsigned long value;
313 int idx = clk_rate_table_find(clk, clk->freq_table, rate);
314 if (idx < 0)
315 return idx;
317 value = sh_clk_read(clk);
318 value &= ~(0xf << clk->enable_bit);
319 value |= (idx << clk->enable_bit);
320 sh_clk_write(value, clk);
322 if (d4t->kick)
323 d4t->kick(clk);
325 return 0;
328 static int sh_clk_div4_enable(struct clk *clk)
330 sh_clk_write(sh_clk_read(clk) & ~(1 << 8), clk);
331 return 0;
334 static void sh_clk_div4_disable(struct clk *clk)
336 sh_clk_write(sh_clk_read(clk) | (1 << 8), clk);
339 static struct sh_clk_ops sh_clk_div4_clk_ops = {
340 .recalc = sh_clk_div4_recalc,
341 .set_rate = sh_clk_div4_set_rate,
342 .round_rate = sh_clk_div_round_rate,
345 static struct sh_clk_ops sh_clk_div4_enable_clk_ops = {
346 .recalc = sh_clk_div4_recalc,
347 .set_rate = sh_clk_div4_set_rate,
348 .round_rate = sh_clk_div_round_rate,
349 .enable = sh_clk_div4_enable,
350 .disable = sh_clk_div4_disable,
353 static struct sh_clk_ops sh_clk_div4_reparent_clk_ops = {
354 .recalc = sh_clk_div4_recalc,
355 .set_rate = sh_clk_div4_set_rate,
356 .round_rate = sh_clk_div_round_rate,
357 .enable = sh_clk_div4_enable,
358 .disable = sh_clk_div4_disable,
359 .set_parent = sh_clk_div4_set_parent,
362 static int __init sh_clk_div4_register_ops(struct clk *clks, int nr,
363 struct clk_div4_table *table, struct sh_clk_ops *ops)
365 struct clk *clkp;
366 void *freq_table;
367 int nr_divs = table->div_mult_table->nr_divisors;
368 int freq_table_size = sizeof(struct cpufreq_frequency_table);
369 int ret = 0;
370 int k;
372 freq_table_size *= (nr_divs + 1);
373 freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
374 if (!freq_table) {
375 pr_err("sh_clk_div4_register: unable to alloc memory\n");
376 return -ENOMEM;
379 for (k = 0; !ret && (k < nr); k++) {
380 clkp = clks + k;
382 clkp->ops = ops;
383 clkp->priv = table;
385 clkp->freq_table = freq_table + (k * freq_table_size);
386 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
388 ret = clk_register(clkp);
391 return ret;
394 int __init sh_clk_div4_register(struct clk *clks, int nr,
395 struct clk_div4_table *table)
397 return sh_clk_div4_register_ops(clks, nr, table, &sh_clk_div4_clk_ops);
400 int __init sh_clk_div4_enable_register(struct clk *clks, int nr,
401 struct clk_div4_table *table)
403 return sh_clk_div4_register_ops(clks, nr, table,
404 &sh_clk_div4_enable_clk_ops);
407 int __init sh_clk_div4_reparent_register(struct clk *clks, int nr,
408 struct clk_div4_table *table)
410 return sh_clk_div4_register_ops(clks, nr, table,
411 &sh_clk_div4_reparent_clk_ops);