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
11 #include <linux/clk.h>
12 #include <linux/compiler.h>
13 #include <linux/slab.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
);
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
);
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
)
60 for (k
= 0; !ret
&& (k
< nr
); k
++) {
62 clkp
->ops
= &sh_clk_mstp_clk_ops
;
63 ret
|= clk_register(clkp
);
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
;
91 clk_rate_table_build(clk
, clk
->freq_table
, table
->nr_divisors
,
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
;
105 if (!clk
->parent_table
|| !clk
->parent_num
)
108 /* Search the parent */
109 for (i
= 0; i
< clk
->parent_num
; i
++)
110 if (clk
->parent_table
[i
] == parent
)
113 if (i
== clk
->parent_num
)
116 ret
= clk_reparent(clk
, parent
);
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
,
132 static int sh_clk_div6_set_rate(struct clk
*clk
, unsigned long rate
)
137 idx
= clk_rate_table_find(clk
, clk
->freq_table
, rate
);
141 value
= sh_clk_read(clk
);
144 sh_clk_write(value
, clk
);
148 static int sh_clk_div6_enable(struct clk
*clk
)
153 ret
= sh_clk_div6_set_rate(clk
, clk
->rate
);
155 value
= sh_clk_read(clk
);
156 value
&= ~0x100; /* clear stop bit to enable clock */
157 sh_clk_write(value
, clk
);
162 static void sh_clk_div6_disable(struct clk
*clk
)
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
)
196 if (!clk
->parent_table
|| !clk
->parent_num
)
199 if (!clk
->src_width
) {
200 pr_err("sh_clk_init_parent: cannot select parent clock\n");
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");
212 clk_reparent(clk
, clk
->parent_table
[val
]);
214 pr_err("sh_clk_init_parent: unable to set parent");
221 static int __init
sh_clk_div6_register_ops(struct clk
*clks
, int nr
,
222 struct sh_clk_ops
*ops
)
226 int nr_divs
= sh_clk_div6_table
.nr_divisors
;
227 int freq_table_size
= sizeof(struct cpufreq_frequency_table
);
231 freq_table_size
*= (nr_divs
+ 1);
232 freq_table
= kzalloc(freq_table_size
* nr
, GFP_KERNEL
);
234 pr_err("sh_clk_div6_register: unable to alloc memory\n");
238 for (k
= 0; !ret
&& (k
< nr
); k
++) {
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
);
248 ret
= sh_clk_init_parent(clkp
);
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
;
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
;
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);
294 value
= sh_clk_read(clk
) | (1 << 7);
296 ret
= clk_reparent(clk
, parent
);
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
);
309 static int sh_clk_div4_set_rate(struct clk
*clk
, unsigned long rate
)
311 struct clk_div4_table
*d4t
= clk
->priv
;
313 int idx
= clk_rate_table_find(clk
, clk
->freq_table
, rate
);
317 value
= sh_clk_read(clk
);
318 value
&= ~(0xf << clk
->enable_bit
);
319 value
|= (idx
<< clk
->enable_bit
);
320 sh_clk_write(value
, clk
);
328 static int sh_clk_div4_enable(struct clk
*clk
)
330 sh_clk_write(sh_clk_read(clk
) & ~(1 << 8), clk
);
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
)
367 int nr_divs
= table
->div_mult_table
->nr_divisors
;
368 int freq_table_size
= sizeof(struct cpufreq_frequency_table
);
372 freq_table_size
*= (nr_divs
+ 1);
373 freq_table
= kzalloc(freq_table_size
* nr
, GFP_KERNEL
);
375 pr_err("sh_clk_div4_register: unable to alloc memory\n");
379 for (k
= 0; !ret
&& (k
< nr
); k
++) {
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
);
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
);