sh: get rid of hwblk clock names
[pandora-kernel.git] / arch / sh / kernel / cpu / sh4a / clock-sh7723.c
1 /*
2  * arch/sh/kernel/cpu/sh4a/clock-sh7723.c
3  *
4  * SH7723 clock framework support
5  *
6  * Copyright (C) 2009 Magnus Damm
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/io.h>
24 #include <linux/clk.h>
25 #include <asm/clkdev.h>
26 #include <asm/clock.h>
27 #include <asm/hwblk.h>
28 #include <cpu/sh7723.h>
29
30 /* SH7723 registers */
31 #define FRQCR           0xa4150000
32 #define VCLKCR          0xa4150004
33 #define SCLKACR         0xa4150008
34 #define SCLKBCR         0xa415000c
35 #define IRDACLKCR       0xa4150018
36 #define PLLCR           0xa4150024
37 #define DLLFRQ          0xa4150050
38
39 /* Fixed 32 KHz root clock for RTC and Power Management purposes */
40 static struct clk r_clk = {
41         .name           = "rclk",
42         .id             = -1,
43         .rate           = 32768,
44 };
45
46 /*
47  * Default rate for the root input clock, reset this with clk_set_rate()
48  * from the platform code.
49  */
50 struct clk extal_clk = {
51         .name           = "extal",
52         .id             = -1,
53         .rate           = 33333333,
54 };
55
56 /* The dll multiplies the 32khz r_clk, may be used instead of extal */
57 static unsigned long dll_recalc(struct clk *clk)
58 {
59         unsigned long mult;
60
61         if (__raw_readl(PLLCR) & 0x1000)
62                 mult = __raw_readl(DLLFRQ);
63         else
64                 mult = 0;
65
66         return clk->parent->rate * mult;
67 }
68
69 static struct clk_ops dll_clk_ops = {
70         .recalc         = dll_recalc,
71 };
72
73 static struct clk dll_clk = {
74         .name           = "dll_clk",
75         .id             = -1,
76         .ops            = &dll_clk_ops,
77         .parent         = &r_clk,
78         .flags          = CLK_ENABLE_ON_INIT,
79 };
80
81 static unsigned long pll_recalc(struct clk *clk)
82 {
83         unsigned long mult = 1;
84         unsigned long div = 1;
85
86         if (__raw_readl(PLLCR) & 0x4000)
87                 mult = (((__raw_readl(FRQCR) >> 24) & 0x1f) + 1);
88         else
89                 div = 2;
90
91         return (clk->parent->rate * mult) / div;
92 }
93
94 static struct clk_ops pll_clk_ops = {
95         .recalc         = pll_recalc,
96 };
97
98 static struct clk pll_clk = {
99         .name           = "pll_clk",
100         .id             = -1,
101         .ops            = &pll_clk_ops,
102         .flags          = CLK_ENABLE_ON_INIT,
103 };
104
105 struct clk *main_clks[] = {
106         &r_clk,
107         &extal_clk,
108         &dll_clk,
109         &pll_clk,
110 };
111
112 static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
113 static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
114
115 static struct clk_div_mult_table div4_div_mult_table = {
116         .divisors = divisors,
117         .nr_divisors = ARRAY_SIZE(divisors),
118         .multipliers = multipliers,
119         .nr_multipliers = ARRAY_SIZE(multipliers),
120 };
121
122 static struct clk_div4_table div4_table = {
123         .div_mult_table = &div4_div_mult_table,
124 };
125
126 enum { DIV4_I, DIV4_U, DIV4_SH, DIV4_B, DIV4_B3, DIV4_P, DIV4_NR };
127
128 #define DIV4(_str, _reg, _bit, _mask, _flags) \
129   SH_CLK_DIV4(_str, &pll_clk, _reg, _bit, _mask, _flags)
130
131 struct clk div4_clks[DIV4_NR] = {
132         [DIV4_I] = DIV4("cpu_clk", FRQCR, 20, 0x0dbf, CLK_ENABLE_ON_INIT),
133         [DIV4_U] = DIV4("umem_clk", FRQCR, 16, 0x0dbf, CLK_ENABLE_ON_INIT),
134         [DIV4_SH] = DIV4("shyway_clk", FRQCR, 12, 0x0dbf, CLK_ENABLE_ON_INIT),
135         [DIV4_B] = DIV4("bus_clk", FRQCR, 8, 0x0dbf, CLK_ENABLE_ON_INIT),
136         [DIV4_B3] = DIV4("b3_clk", FRQCR, 4, 0x0db4, CLK_ENABLE_ON_INIT),
137         [DIV4_P] = DIV4("peripheral_clk", FRQCR, 0, 0x0dbf, 0),
138 };
139
140 enum { DIV4_IRDA, DIV4_ENABLE_NR };
141
142 struct clk div4_enable_clks[DIV4_ENABLE_NR] = {
143         [DIV4_IRDA] = DIV4("irda_clk", IRDACLKCR, 0, 0x0dbf, 0),
144 };
145
146 enum { DIV4_SIUA, DIV4_SIUB, DIV4_REPARENT_NR };
147
148 struct clk div4_reparent_clks[DIV4_REPARENT_NR] = {
149         [DIV4_SIUA] = DIV4("siua_clk", SCLKACR, 0, 0x0dbf, 0),
150         [DIV4_SIUB] = DIV4("siub_clk", SCLKBCR, 0, 0x0dbf, 0),
151 };
152 struct clk div6_clks[] = {
153         SH_CLK_DIV6("video_clk", &pll_clk, VCLKCR, 0),
154 };
155
156 static struct clk mstp_clks[] = {
157         /* See page 60 of Datasheet V1.0: Overview -> Block Diagram */
158         SH_HWBLK_CLK(HWBLK_TLB, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
159         SH_HWBLK_CLK(HWBLK_IC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
160         SH_HWBLK_CLK(HWBLK_OC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
161         SH_HWBLK_CLK(HWBLK_L2C, &div4_clks[DIV4_SH], CLK_ENABLE_ON_INIT),
162         SH_HWBLK_CLK(HWBLK_ILMEM, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
163         SH_HWBLK_CLK(HWBLK_FPU, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
164         SH_HWBLK_CLK(HWBLK_INTC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
165         SH_HWBLK_CLK(HWBLK_DMAC0, &div4_clks[DIV4_B], 0),
166         SH_HWBLK_CLK(HWBLK_SHYWAY, &div4_clks[DIV4_SH], CLK_ENABLE_ON_INIT),
167         SH_HWBLK_CLK(HWBLK_HUDI, &div4_clks[DIV4_P], 0),
168         SH_HWBLK_CLK(HWBLK_UBC, &div4_clks[DIV4_I], 0),
169         SH_HWBLK_CLK(HWBLK_TMU0, &div4_clks[DIV4_P], 0),
170         SH_HWBLK_CLK(HWBLK_CMT, &r_clk, 0),
171         SH_HWBLK_CLK(HWBLK_RWDT, &r_clk, 0),
172         SH_HWBLK_CLK(HWBLK_DMAC1, &div4_clks[DIV4_B], 0),
173         SH_HWBLK_CLK(HWBLK_TMU1, &div4_clks[DIV4_P], 0),
174         SH_HWBLK_CLK(HWBLK_FLCTL, &div4_clks[DIV4_P], 0),
175         SH_HWBLK_CLK(HWBLK_SCIF0, &div4_clks[DIV4_P], 0),
176         SH_HWBLK_CLK(HWBLK_SCIF1, &div4_clks[DIV4_P], 0),
177         SH_HWBLK_CLK(HWBLK_SCIF2, &div4_clks[DIV4_P], 0),
178         SH_HWBLK_CLK(HWBLK_SCIF3, &div4_clks[DIV4_B], 0),
179         SH_HWBLK_CLK(HWBLK_SCIF4, &div4_clks[DIV4_B], 0),
180         SH_HWBLK_CLK(HWBLK_SCIF5, &div4_clks[DIV4_B], 0),
181         SH_HWBLK_CLK(HWBLK_MSIOF0, &div4_clks[DIV4_B], 0),
182         SH_HWBLK_CLK(HWBLK_MSIOF1, &div4_clks[DIV4_B], 0),
183         SH_HWBLK_CLK(HWBLK_MERAM, &div4_clks[DIV4_SH], 0),
184
185         SH_HWBLK_CLK(HWBLK_IIC, &div4_clks[DIV4_P], 0),
186         SH_HWBLK_CLK(HWBLK_RTC, &r_clk, 0),
187
188         SH_HWBLK_CLK(HWBLK_ATAPI, &div4_clks[DIV4_SH], 0),
189         SH_HWBLK_CLK(HWBLK_ADC, &div4_clks[DIV4_P], 0),
190         SH_HWBLK_CLK(HWBLK_TPU, &div4_clks[DIV4_B], 0),
191         SH_HWBLK_CLK(HWBLK_IRDA, &div4_clks[DIV4_P], 0),
192         SH_HWBLK_CLK(HWBLK_TSIF, &div4_clks[DIV4_B], 0),
193         SH_HWBLK_CLK(HWBLK_ICB, &div4_clks[DIV4_B], CLK_ENABLE_ON_INIT),
194         SH_HWBLK_CLK(HWBLK_SDHI0, &div4_clks[DIV4_B], 0),
195         SH_HWBLK_CLK(HWBLK_SDHI1, &div4_clks[DIV4_B], 0),
196         SH_HWBLK_CLK(HWBLK_KEYSC, &r_clk, 0),
197         SH_HWBLK_CLK(HWBLK_USB, &div4_clks[DIV4_B], 0),
198         SH_HWBLK_CLK(HWBLK_2DG, &div4_clks[DIV4_B], 0),
199         SH_HWBLK_CLK(HWBLK_SIU, &div4_clks[DIV4_B], 0),
200         SH_HWBLK_CLK(HWBLK_VEU2H1, &div4_clks[DIV4_B], 0),
201         SH_HWBLK_CLK(HWBLK_VOU, &div4_clks[DIV4_B], 0),
202         SH_HWBLK_CLK(HWBLK_BEU, &div4_clks[DIV4_B], 0),
203         SH_HWBLK_CLK(HWBLK_CEU, &div4_clks[DIV4_B], 0),
204         SH_HWBLK_CLK(HWBLK_VEU2H0, &div4_clks[DIV4_B], 0),
205         SH_HWBLK_CLK(HWBLK_VPU, &div4_clks[DIV4_B], 0),
206         SH_HWBLK_CLK(HWBLK_LCDC, &div4_clks[DIV4_B], 0),
207 };
208
209 #define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }
210
211 static struct clk_lookup lookups[] = {
212         /* MSTP clocks */
213         CLKDEV_CON_ID("tlb0", &mstp_clks[HWBLK_TLB]),
214         CLKDEV_CON_ID("ic0", &mstp_clks[HWBLK_IC]),
215         CLKDEV_CON_ID("oc0", &mstp_clks[HWBLK_OC]),
216         CLKDEV_CON_ID("l2c0", &mstp_clks[HWBLK_L2C]),
217         CLKDEV_CON_ID("ilmem0", &mstp_clks[HWBLK_ILMEM]),
218         CLKDEV_CON_ID("fpu0", &mstp_clks[HWBLK_FPU]),
219         CLKDEV_CON_ID("intc0", &mstp_clks[HWBLK_INTC]),
220         CLKDEV_CON_ID("dmac0", &mstp_clks[HWBLK_DMAC0]),
221         CLKDEV_CON_ID("sh0", &mstp_clks[HWBLK_SHYWAY]),
222         CLKDEV_CON_ID("hudi0", &mstp_clks[HWBLK_HUDI]),
223         CLKDEV_CON_ID("ubc0", &mstp_clks[HWBLK_UBC]),
224         {
225                 /* TMU0 */
226                 .dev_id         = "sh_tmu.0",
227                 .con_id         = "tmu_fck",
228                 .clk            = &mstp_clks[HWBLK_TMU0],
229         }, {
230                 /* TMU1 */
231                 .dev_id         = "sh_tmu.1",
232                 .con_id         = "tmu_fck",
233                 .clk            = &mstp_clks[HWBLK_TMU0],
234         }, {
235                 /* TMU2 */
236                 .dev_id         = "sh_tmu.2",
237                 .con_id         = "tmu_fck",
238                 .clk            = &mstp_clks[HWBLK_TMU0],
239         },
240         CLKDEV_CON_ID("cmt_fck", &mstp_clks[HWBLK_CMT]),
241         CLKDEV_CON_ID("rwdt0", &mstp_clks[HWBLK_RWDT]),
242         CLKDEV_CON_ID("dmac1", &mstp_clks[HWBLK_DMAC1]),
243         {
244                 /* TMU3 */
245                 .dev_id         = "sh_tmu.3",
246                 .con_id         = "tmu_fck",
247                 .clk            = &mstp_clks[HWBLK_TMU1],
248         }, {
249                 /* TMU4 */
250                 .dev_id         = "sh_tmu.4",
251                 .con_id         = "tmu_fck",
252                 .clk            = &mstp_clks[HWBLK_TMU1],
253         }, {
254                 /* TMU5 */
255                 .dev_id         = "sh_tmu.5",
256                 .con_id         = "tmu_fck",
257                 .clk            = &mstp_clks[HWBLK_TMU1],
258         },
259         CLKDEV_CON_ID("flctl0", &mstp_clks[HWBLK_FLCTL]),
260         {
261                 /* SCIF0 */
262                 .dev_id         = "sh-sci.0",
263                 .con_id         = "sci_fck",
264                 .clk            = &mstp_clks[HWBLK_SCIF0],
265         }, {
266                 /* SCIF1 */
267                 .dev_id         = "sh-sci.1",
268                 .con_id         = "sci_fck",
269                 .clk            = &mstp_clks[HWBLK_SCIF1],
270         }, {
271                 /* SCIF2 */
272                 .dev_id         = "sh-sci.2",
273                 .con_id         = "sci_fck",
274                 .clk            = &mstp_clks[HWBLK_SCIF2],
275         }, {
276                 /* SCIF3 */
277                 .dev_id         = "sh-sci.3",
278                 .con_id         = "sci_fck",
279                 .clk            = &mstp_clks[HWBLK_SCIF3],
280         }, {
281                 /* SCIF4 */
282                 .dev_id         = "sh-sci.4",
283                 .con_id         = "sci_fck",
284                 .clk            = &mstp_clks[HWBLK_SCIF4],
285         }, {
286                 /* SCIF5 */
287                 .dev_id         = "sh-sci.5",
288                 .con_id         = "sci_fck",
289                 .clk            = &mstp_clks[HWBLK_SCIF5],
290         },
291         CLKDEV_CON_ID("msiof0", &mstp_clks[HWBLK_MSIOF0]),
292         CLKDEV_CON_ID("msiof1", &mstp_clks[HWBLK_MSIOF1]),
293         CLKDEV_CON_ID("meram0", &mstp_clks[HWBLK_MERAM]),
294         CLKDEV_CON_ID("i2c0", &mstp_clks[HWBLK_IIC]),
295         CLKDEV_CON_ID("rtc0", &mstp_clks[HWBLK_RTC]),
296         CLKDEV_CON_ID("atapi0", &mstp_clks[HWBLK_ATAPI]),
297         CLKDEV_CON_ID("adc0", &mstp_clks[HWBLK_ADC]),
298         CLKDEV_CON_ID("tpu0", &mstp_clks[HWBLK_TPU]),
299         CLKDEV_CON_ID("irda0", &mstp_clks[HWBLK_IRDA]),
300         CLKDEV_CON_ID("tsif0", &mstp_clks[HWBLK_TSIF]),
301         CLKDEV_CON_ID("icb0", &mstp_clks[HWBLK_ICB]),
302         CLKDEV_CON_ID("sdhi0", &mstp_clks[HWBLK_SDHI0]),
303         CLKDEV_CON_ID("sdhi1", &mstp_clks[HWBLK_SDHI1]),
304         CLKDEV_CON_ID("keysc0", &mstp_clks[HWBLK_KEYSC]),
305         CLKDEV_CON_ID("usb0", &mstp_clks[HWBLK_USB]),
306         CLKDEV_CON_ID("2dg0", &mstp_clks[HWBLK_2DG]),
307         CLKDEV_CON_ID("siu0", &mstp_clks[HWBLK_SIU]),
308         CLKDEV_CON_ID("veu1", &mstp_clks[HWBLK_VEU2H1]),
309         CLKDEV_CON_ID("vou0", &mstp_clks[HWBLK_VOU]),
310         CLKDEV_CON_ID("beu0", &mstp_clks[HWBLK_BEU]),
311         CLKDEV_CON_ID("ceu0", &mstp_clks[HWBLK_CEU]),
312         CLKDEV_CON_ID("veu0", &mstp_clks[HWBLK_VEU2H0]),
313         CLKDEV_CON_ID("vpu0", &mstp_clks[HWBLK_VPU]),
314         CLKDEV_CON_ID("lcdc0", &mstp_clks[HWBLK_LCDC]),
315 };
316
317 int __init arch_clk_init(void)
318 {
319         int k, ret = 0;
320
321         /* autodetect extal or dll configuration */
322         if (__raw_readl(PLLCR) & 0x1000)
323                 pll_clk.parent = &dll_clk;
324         else
325                 pll_clk.parent = &extal_clk;
326
327         for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
328                 ret |= clk_register(main_clks[k]);
329
330         clkdev_add_table(lookups, ARRAY_SIZE(lookups));
331
332         if (!ret)
333                 ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table);
334
335         if (!ret)
336                 ret = sh_clk_div4_enable_register(div4_enable_clks,
337                                         DIV4_ENABLE_NR, &div4_table);
338
339         if (!ret)
340                 ret = sh_clk_div4_reparent_register(div4_reparent_clks,
341                                         DIV4_REPARENT_NR, &div4_table);
342
343         if (!ret)
344                 ret = sh_clk_div6_register(div6_clks, ARRAY_SIZE(div6_clks));
345
346         if (!ret)
347                 ret = sh_hwblk_clk_register(mstp_clks, HWBLK_NR);
348
349         return ret;
350 }