sh: clock div6 helper code
[linux-2.6/mini2440.git] / arch / sh / kernel / cpu / clock-cpg.c
blobfedc8b84db4c2fe52346cdd2a9b5be95afa60bfc
1 #include <linux/clk.h>
2 #include <linux/compiler.h>
3 #include <linux/bootmem.h>
4 #include <linux/io.h>
5 #include <asm/clock.h>
7 static int sh_clk_mstp32_enable(struct clk *clk)
9 __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << clk->enable_bit),
10 clk->enable_reg);
11 return 0;
14 static void sh_clk_mstp32_disable(struct clk *clk)
16 __raw_writel(__raw_readl(clk->enable_reg) | (1 << clk->enable_bit),
17 clk->enable_reg);
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)
28 struct clk *clkp;
29 int ret = 0;
30 int k;
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);
38 return ret;
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;
61 unsigned int idx;
63 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
64 table, NULL);
66 idx = __raw_readl(clk->enable_reg) & 0x003f;
68 return clk->freq_table[idx].frequency;
71 static struct clk_ops sh_clk_div6_clk_ops = {
72 .recalc = sh_clk_div6_recalc,
73 .round_rate = sh_clk_div_round_rate,
76 int __init sh_clk_div6_register(struct clk *clks, int nr)
78 struct clk *clkp;
79 void *freq_table;
80 int nr_divs = sh_clk_div6_table.nr_divisors;
81 int freq_table_size = sizeof(struct cpufreq_frequency_table);
82 int ret = 0;
83 int k;
85 freq_table_size *= (nr_divs + 1);
87 freq_table = alloc_bootmem(freq_table_size * nr);
88 if (!freq_table)
89 return -ENOMEM;
91 for (k = 0; !ret && (k < nr); k++) {
92 clkp = clks + k;
94 clkp->ops = &sh_clk_div6_clk_ops;
95 clkp->id = -1;
96 clkp->freq_table = freq_table + (k * freq_table_size);
97 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
99 ret = clk_register(clkp);
102 return ret;
105 static unsigned long sh_clk_div4_recalc(struct clk *clk)
107 struct clk_div_mult_table *table = clk->priv;
108 unsigned int idx;
110 clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
111 table, &clk->arch_flags);
113 idx = (__raw_readl(clk->enable_reg) >> clk->enable_bit) & 0x000f;
115 return clk->freq_table[idx].frequency;
118 static struct clk_ops sh_clk_div4_clk_ops = {
119 .recalc = sh_clk_div4_recalc,
120 .round_rate = sh_clk_div_round_rate,
123 int __init sh_clk_div4_register(struct clk *clks, int nr,
124 struct clk_div_mult_table *table)
126 struct clk *clkp;
127 void *freq_table;
128 int nr_divs = table->nr_divisors;
129 int freq_table_size = sizeof(struct cpufreq_frequency_table);
130 int ret = 0;
131 int k;
133 freq_table_size *= (nr_divs + 1);
135 freq_table = alloc_bootmem(freq_table_size * nr);
136 if (!freq_table)
137 return -ENOMEM;
139 for (k = 0; !ret && (k < nr); k++) {
140 clkp = clks + k;
142 clkp->ops = &sh_clk_div4_clk_ops;
143 clkp->id = -1;
144 clkp->priv = table;
146 clkp->freq_table = freq_table + (k * freq_table_size);
147 clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
149 ret = clk_register(clkp);
152 return ret;
155 #ifdef CONFIG_SH_CLK_CPG_LEGACY
156 static struct clk master_clk = {
157 .name = "master_clk",
158 .flags = CLK_ENABLE_ON_INIT,
159 .rate = CONFIG_SH_PCLK_FREQ,
162 static struct clk peripheral_clk = {
163 .name = "peripheral_clk",
164 .parent = &master_clk,
165 .flags = CLK_ENABLE_ON_INIT,
168 static struct clk bus_clk = {
169 .name = "bus_clk",
170 .parent = &master_clk,
171 .flags = CLK_ENABLE_ON_INIT,
174 static struct clk cpu_clk = {
175 .name = "cpu_clk",
176 .parent = &master_clk,
177 .flags = CLK_ENABLE_ON_INIT,
181 * The ordering of these clocks matters, do not change it.
183 static struct clk *onchip_clocks[] = {
184 &master_clk,
185 &peripheral_clk,
186 &bus_clk,
187 &cpu_clk,
190 int __init __deprecated cpg_clk_init(void)
192 int i, ret = 0;
194 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
195 struct clk *clk = onchip_clocks[i];
196 arch_init_clk_ops(&clk->ops, i);
197 if (clk->ops)
198 ret |= clk_register(clk);
201 return ret;
205 * Placeholder for compatability, until the lazy CPUs do this
206 * on their own.
208 int __init __weak arch_clk_init(void)
210 return cpg_clk_init();
212 #endif /* CONFIG_SH_CPG_CLK_LEGACY */