2 #include <linux/compiler.h>
3 #include <linux/slab.h>
5 #include <linux/sh_clk.h>
7 static int sh_clk_mstp32_enable(struct clk
*clk
)
9 __raw_writel(__raw_readl(clk
->enable_reg
) & ~(1 << clk
->enable_bit
),
14 static void sh_clk_mstp32_disable(struct clk
*clk
)
16 __raw_writel(__raw_readl(clk
->enable_reg
) | (1 << clk
->enable_bit
),
20 static struct clk_ops sh_clk_mstp32_clk_ops
= {
21 .enable
= sh_clk_mstp32_enable
,
22 .disable
= sh_clk_mstp32_disable
,
23 .recalc
= followparent_recalc
,
26 int __init
sh_clk_mstp32_register(struct clk
*clks
, int nr
)
32 for (k
= 0; !ret
&& (k
< nr
); k
++) {
34 clkp
->ops
= &sh_clk_mstp32_clk_ops
;
35 ret
|= clk_register(clkp
);
41 static long sh_clk_div_round_rate(struct clk
*clk
, unsigned long rate
)
43 return clk_rate_table_round(clk
, clk
->freq_table
, rate
);
46 static 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
53 static struct clk_div_mult_table sh_clk_div6_table
= {
54 .divisors
= sh_clk_div6_divisors
,
55 .nr_divisors
= ARRAY_SIZE(sh_clk_div6_divisors
),
58 static unsigned long sh_clk_div6_recalc(struct clk
*clk
)
60 struct clk_div_mult_table
*table
= &sh_clk_div6_table
;
63 clk_rate_table_build(clk
, clk
->freq_table
, table
->nr_divisors
,
66 idx
= __raw_readl(clk
->enable_reg
) & 0x003f;
68 return clk
->freq_table
[idx
].frequency
;
71 static int sh_clk_div6_set_parent(struct clk
*clk
, struct clk
*parent
)
73 struct clk_div_mult_table
*table
= &sh_clk_div6_table
;
77 if (!clk
->parent_table
|| !clk
->parent_num
)
80 /* Search the parent */
81 for (i
= 0; i
< clk
->parent_num
; i
++)
82 if (clk
->parent_table
[i
] == parent
)
85 if (i
== clk
->parent_num
)
88 ret
= clk_reparent(clk
, parent
);
92 value
= __raw_readl(clk
->enable_reg
) &
93 ~(((1 << clk
->src_width
) - 1) << clk
->src_shift
);
95 __raw_writel(value
| (i
<< clk
->src_shift
), clk
->enable_reg
);
97 /* Rebuild the frequency table */
98 clk_rate_table_build(clk
, clk
->freq_table
, table
->nr_divisors
,
99 table
, &clk
->arch_flags
);
104 static int sh_clk_div6_set_rate(struct clk
*clk
,
105 unsigned long rate
, int algo_id
)
110 idx
= clk_rate_table_find(clk
, clk
->freq_table
, rate
);
114 value
= __raw_readl(clk
->enable_reg
);
117 __raw_writel(value
, clk
->enable_reg
);
121 static int sh_clk_div6_enable(struct clk
*clk
)
126 ret
= sh_clk_div6_set_rate(clk
, clk
->rate
, 0);
128 value
= __raw_readl(clk
->enable_reg
);
129 value
&= ~0x100; /* clear stop bit to enable clock */
130 __raw_writel(value
, clk
->enable_reg
);
135 static void sh_clk_div6_disable(struct clk
*clk
)
139 value
= __raw_readl(clk
->enable_reg
);
140 value
|= 0x100; /* stop clock */
141 value
|= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
142 __raw_writel(value
, clk
->enable_reg
);
145 static struct clk_ops sh_clk_div6_clk_ops
= {
146 .recalc
= sh_clk_div6_recalc
,
147 .round_rate
= sh_clk_div_round_rate
,
148 .set_rate
= sh_clk_div6_set_rate
,
149 .enable
= sh_clk_div6_enable
,
150 .disable
= sh_clk_div6_disable
,
153 static struct clk_ops sh_clk_div6_reparent_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
,
159 .set_parent
= sh_clk_div6_set_parent
,
162 static int __init
sh_clk_div6_register_ops(struct clk
*clks
, int nr
,
167 int nr_divs
= sh_clk_div6_table
.nr_divisors
;
168 int freq_table_size
= sizeof(struct cpufreq_frequency_table
);
172 freq_table_size
*= (nr_divs
+ 1);
173 freq_table
= kzalloc(freq_table_size
* nr
, GFP_KERNEL
);
175 pr_err("sh_clk_div6_register: unable to alloc memory\n");
179 for (k
= 0; !ret
&& (k
< nr
); k
++) {
184 clkp
->freq_table
= freq_table
+ (k
* freq_table_size
);
185 clkp
->freq_table
[nr_divs
].frequency
= CPUFREQ_TABLE_END
;
187 ret
= clk_register(clkp
);
193 int __init
sh_clk_div6_register(struct clk
*clks
, int nr
)
195 return sh_clk_div6_register_ops(clks
, nr
, &sh_clk_div6_clk_ops
);
198 int __init
sh_clk_div6_reparent_register(struct clk
*clks
, int nr
)
200 return sh_clk_div6_register_ops(clks
, nr
,
201 &sh_clk_div6_reparent_clk_ops
);
204 static unsigned long sh_clk_div4_recalc(struct clk
*clk
)
206 struct clk_div4_table
*d4t
= clk
->priv
;
207 struct clk_div_mult_table
*table
= d4t
->div_mult_table
;
210 clk_rate_table_build(clk
, clk
->freq_table
, table
->nr_divisors
,
211 table
, &clk
->arch_flags
);
213 idx
= (__raw_readl(clk
->enable_reg
) >> clk
->enable_bit
) & 0x000f;
215 return clk
->freq_table
[idx
].frequency
;
218 static int sh_clk_div4_set_parent(struct clk
*clk
, struct clk
*parent
)
220 struct clk_div4_table
*d4t
= clk
->priv
;
221 struct clk_div_mult_table
*table
= d4t
->div_mult_table
;
225 /* we really need a better way to determine parent index, but for
226 * now assume internal parent comes with CLK_ENABLE_ON_INIT set,
227 * no CLK_ENABLE_ON_INIT means external clock...
230 if (parent
->flags
& CLK_ENABLE_ON_INIT
)
231 value
= __raw_readl(clk
->enable_reg
) & ~(1 << 7);
233 value
= __raw_readl(clk
->enable_reg
) | (1 << 7);
235 ret
= clk_reparent(clk
, parent
);
239 __raw_writel(value
, clk
->enable_reg
);
241 /* Rebiuld the frequency table */
242 clk_rate_table_build(clk
, clk
->freq_table
, table
->nr_divisors
,
243 table
, &clk
->arch_flags
);
248 static int sh_clk_div4_set_rate(struct clk
*clk
, unsigned long rate
, int algo_id
)
250 struct clk_div4_table
*d4t
= clk
->priv
;
252 int idx
= clk_rate_table_find(clk
, clk
->freq_table
, rate
);
256 value
= __raw_readl(clk
->enable_reg
);
257 value
&= ~(0xf << clk
->enable_bit
);
258 value
|= (idx
<< clk
->enable_bit
);
259 __raw_writel(value
, clk
->enable_reg
);
267 static int sh_clk_div4_enable(struct clk
*clk
)
269 __raw_writel(__raw_readl(clk
->enable_reg
) & ~(1 << 8), clk
->enable_reg
);
273 static void sh_clk_div4_disable(struct clk
*clk
)
275 __raw_writel(__raw_readl(clk
->enable_reg
) | (1 << 8), clk
->enable_reg
);
278 static struct clk_ops sh_clk_div4_clk_ops
= {
279 .recalc
= sh_clk_div4_recalc
,
280 .set_rate
= sh_clk_div4_set_rate
,
281 .round_rate
= sh_clk_div_round_rate
,
284 static struct clk_ops sh_clk_div4_enable_clk_ops
= {
285 .recalc
= sh_clk_div4_recalc
,
286 .set_rate
= sh_clk_div4_set_rate
,
287 .round_rate
= sh_clk_div_round_rate
,
288 .enable
= sh_clk_div4_enable
,
289 .disable
= sh_clk_div4_disable
,
292 static struct clk_ops sh_clk_div4_reparent_clk_ops
= {
293 .recalc
= sh_clk_div4_recalc
,
294 .set_rate
= sh_clk_div4_set_rate
,
295 .round_rate
= sh_clk_div_round_rate
,
296 .enable
= sh_clk_div4_enable
,
297 .disable
= sh_clk_div4_disable
,
298 .set_parent
= sh_clk_div4_set_parent
,
301 static int __init
sh_clk_div4_register_ops(struct clk
*clks
, int nr
,
302 struct clk_div4_table
*table
, struct clk_ops
*ops
)
306 int nr_divs
= table
->div_mult_table
->nr_divisors
;
307 int freq_table_size
= sizeof(struct cpufreq_frequency_table
);
311 freq_table_size
*= (nr_divs
+ 1);
312 freq_table
= kzalloc(freq_table_size
* nr
, GFP_KERNEL
);
314 pr_err("sh_clk_div4_register: unable to alloc memory\n");
318 for (k
= 0; !ret
&& (k
< nr
); k
++) {
325 clkp
->freq_table
= freq_table
+ (k
* freq_table_size
);
326 clkp
->freq_table
[nr_divs
].frequency
= CPUFREQ_TABLE_END
;
328 ret
= clk_register(clkp
);
334 int __init
sh_clk_div4_register(struct clk
*clks
, int nr
,
335 struct clk_div4_table
*table
)
337 return sh_clk_div4_register_ops(clks
, nr
, table
, &sh_clk_div4_clk_ops
);
340 int __init
sh_clk_div4_enable_register(struct clk
*clks
, int nr
,
341 struct clk_div4_table
*table
)
343 return sh_clk_div4_register_ops(clks
, nr
, table
,
344 &sh_clk_div4_enable_clk_ops
);
347 int __init
sh_clk_div4_reparent_register(struct clk
*clks
, int nr
,
348 struct clk_div4_table
*table
)
350 return sh_clk_div4_register_ops(clks
, nr
, table
,
351 &sh_clk_div4_reparent_clk_ops
);