change sdmmc
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rk2818 / clock.c
1 /* arch/arm/mach-rk2818/clock.c
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/clk.h>
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/version.h>
26 #include <asm/clkdev.h>
27 #include <mach/rk2818_iomap.h>
28 #include <mach/scu.h>
29 #include <mach/iomux.h> // CPU_APB_REG0
30
31 static struct rockchip_scu_reg_hw
32 {
33         u32 scu_pll_config[3];  /* 0:arm 1:dsp 2:codec */
34         u32 scu_mode_config;
35         u32 scu_pmu_config;
36         u32 scu_clksel0_config;
37         u32 scu_clksel1_config;
38         u32 scu_clkgate0_config;
39         u32 scu_clkgate1_config;
40         u32 scu_clkgate2_config;
41         u32 scu_softreset_config;
42         u32 scu_chipcfg_config;
43         u32 scu_cuppd;          /* arm power down */
44         u32 scu_clksel2_config;
45 } *scu_register_base = (struct rockchip_scu_reg_hw *)(RK2818_SCU_BASE);
46
47 #define CLKSEL0_REG     (u32 __iomem *)(RK2818_SCU_BASE + SCU_CLKSEL0_CON)
48 #define CLKSEL1_REG     (u32 __iomem *)(RK2818_SCU_BASE + SCU_CLKSEL1_CON)
49 #define CLKSEL2_REG     (u32 __iomem *)(RK2818_SCU_BASE + SCU_CLKSEL2_CON)
50
51 /* SCU PLL CON */
52 #define PLL_TEST        (0x01u<<25)
53 #define PLL_SAT         (0x01u<<24)
54 #define PLL_FAST        (0x01u<<23)
55 #define PLL_PD          (0x01u<<22)
56 #define PLL_CLKR(i)     (((i)&0x3f)<<16)
57 #define PLL_CLKF(i)     (((i)&0x0fff)<<4)
58 #define PLL_CLKOD(i)    (((i)&0x07)<<1)
59 #define PLL_BYPASS      (0X01)
60
61 /* SCU MODE CON */
62 #define SCU_CPUMODE_MASK        (0x03u << 2)
63 #define SCU_CPUMODE_SLOW        (0x00u << 2)
64 #define SCU_CPUMODE_NORMAL      (0x01u << 2)
65 #define SCU_CPUMODE_DSLOW       (0x02u << 2)
66
67 #define SCU_DSPMODE_MASK        0x03u
68 #define SCU_DSPMODE_SLOW        0x00u
69 #define SCU_DSPMODE_NORMAL      0x01u
70 #define SCU_DSPMODE_DSLOW       0x02u
71
72 /* SCU CLK SEL0 CON */
73 #define CLK_DDR_REG             CLKSEL0_REG
74 #define CLK_DDR_SRC_MASK        (3 << 28)
75 #define CLK_DDR_CODPLL          (0 << 28)
76 #define CLK_DDR_ARMPLL          (1 << 28)
77 #define CLK_DDR_DSPPLL          (2 << 28)
78
79 #define CLK_SENSOR_REG          CLKSEL0_REG
80 #define CLK_SENSOR_SRC_MASK     (3 << 23)
81 #define CLK_SENSOR_24M          (0 << 23)
82 #define CLK_SENSOR_27M          (1 << 23)
83 #define CLK_SENSOR_48M          (2 << 23)
84
85 #define CLK_USBPHY_REG          CLKSEL0_REG
86 #define CLK_USBPHY_SRC_MASK     (3 << 18)
87 #define CLK_USBPHY_24M          (0 << 18)
88 #define CLK_USBPHY_12M          (1 << 18)
89 #define CLK_USBPHY_48M          (2 << 18)
90
91 #define CLK_LCDC_REG            CLKSEL0_REG
92 #define CLK_LCDC_DIV_SRC_MASK   (3 << 16)
93 #define CLK_LCDC_ARMPLL         (0 << 16)
94 #define CLK_LCDC_DSPPLL         (1 << 16)
95 #define CLK_LCDC_CODPLL         (2 << 16)
96 #define CLK_LCDC_SRC_MASK       (1 << 7)
97 #define CLK_LCDC_DIVOUT         (0 << 7)
98 #define CLK_LCDC_27M            (1 << 7)
99
100 /* SCU CLKSEL1 CON */
101 #define CLK_UART_REG            CLKSEL1_REG
102 #define CLK_UART_SRC_MASK       (1 << 31)
103 #define CLK_UART_24M            (0 << 31)
104 #define CLK_UART_48M            (1 << 31)
105
106 #define CLK_DEMOD_REG           CLKSEL1_REG
107 #define CLK_DEMOD_SRC_MASK      (1 << 26)
108 #define CLK_DEMOD_DIVOUT        (0 << 26)
109 #define CLK_DEMOD_27M           (1 << 26)
110 #define CLK_DEMOD_DIV_SRC_MASK  (3 << 24)
111 #define CLK_DEMOD_CODPLL        (0 << 24)
112 #define CLK_DEMOD_ARMPLL        (1 << 24)
113 #define CLK_DEMOD_DSPPLL        (2 << 24)
114
115 #define CLK_CODEC_REG           CLKSEL1_REG
116 #define CLK_CODEC_SRC_MASK      (1 << 2)
117 #define CLK_CODEC_CPLLCLK       (0 << 2)
118 #define CLK_CODEC_12M           (1 << 2)
119
120 #define CLK_CPLL_MASK           0x03u
121 #define CLK_CPLL_SLOW           0x00u
122 #define CLK_CPLL_NORMAL         0x01u
123 #define CLK_CPLL_DSLOW          0x02u
124
125 /* Clock flags */
126 /* bit 0 is free */
127 #define RATE_FIXED              (1 << 1)        /* Fixed clock rate */
128 #define CONFIG_PARTICIPANT      (1 << 10)       /* Fundamental clock */
129 #define ENABLE_ON_INIT          (1 << 11)       /* Enable upon framework init */
130
131 struct clk {
132         struct list_head        node;
133         const char              *name;
134         struct clk              *parent;
135         struct list_head        children;
136         struct list_head        sibling;        /* node for children */
137         unsigned long           rate;
138         u32                     flags;
139         int                     (*mode)(struct clk *clk, int on);
140         unsigned long           (*recalc)(struct clk *);
141         int                     (*set_rate)(struct clk *, unsigned long);
142         long                    (*round_rate)(struct clk *, unsigned long);
143         struct clk*             (*get_parent)(struct clk *);    /* get clk's parent from the hardware */
144         int                     (*set_parent)(struct clk *, struct clk *);
145         s16                     usecount;
146         u8                      gate_idx;
147         u8                      pll_idx;
148         u32 __iomem             *clksel_reg;
149         u32                     clksel_mask;
150         u8                      clksel_shift;
151         u8                      clksel_maxdiv;
152 };
153
154 static void __clk_disable(struct clk *clk);
155 static void clk_reparent(struct clk *child, struct clk *parent);
156 static void propagate_rate(struct clk *tclk);
157
158 /* Used for clocks that always have same value as the parent clock */
159 static unsigned long followparent_recalc(struct clk *clk)
160 {
161         return clk->parent->rate;
162 }
163
164 static unsigned long clksel_recalc(struct clk *clk)
165 {
166         u32 div = ((readl(clk->clksel_reg) & clk->clksel_mask) >> clk->clksel_shift) + 1;
167         unsigned long rate = clk->parent->rate / div;
168         pr_debug("clock: %s new clock rate is %ld (div %d)\n", clk->name, rate, div);
169         return rate;
170 }
171
172 static unsigned long clksel_recalc_shift(struct clk *clk)
173 {
174         u32 shift = (readl(clk->clksel_reg) & clk->clksel_mask) >> clk->clksel_shift;
175         unsigned long rate = clk->parent->rate >> shift;
176         pr_debug("clock: %s new clock rate is %ld (shift %d)\n", clk->name, rate, shift);
177         return rate;
178 }
179
180 static int clksel_set_rate(struct clk *clk, unsigned long rate)
181 {
182         u32 div;
183
184         for (div = 1; div <= clk->clksel_maxdiv; div++) {
185                 u32 new_rate = clk->parent->rate / div;
186                 if (new_rate <= rate) {
187                         u32 *reg = clk->clksel_reg;
188                         u32 v = readl(reg);
189                         v &= ~clk->clksel_mask;
190                         v |= (div - 1) << clk->clksel_shift;
191                         writel(v, reg);
192                         clk->rate = new_rate;
193                         pr_debug("clock: clksel_set_rate for clock %s to rate %ld (div %d)\n", clk->name, rate, div);
194                         return 0;
195                 }
196         }
197
198         return -ENOENT;
199 }
200
201 static int clksel_set_rate_shift(struct clk *clk, unsigned long rate)
202 {
203         u32 shift;
204
205         for (shift = 0; (1 << shift) <= clk->clksel_maxdiv; shift++) {
206                 u32 new_rate = clk->parent->rate >> shift;
207                 if (new_rate <= rate) {
208                         u32 *reg = clk->clksel_reg;
209                         u32 v = readl(reg);
210                         v &= ~clk->clksel_mask;
211                         v |= shift << clk->clksel_shift;
212                         writel(v, reg);
213                         clk->rate = new_rate;
214                         pr_debug("clock: clksel_set_rate for clock %s to rate %ld (shift %d)\n", clk->name, rate, shift);
215                         return 0;
216                 }
217         }
218
219         return -ENOENT;
220 }
221
222 static struct clk xin24m = {
223         .name           = "xin24m",
224         .rate           = 24000000,
225         .flags          = RATE_FIXED,
226         .gate_idx       = -1,
227 };
228
229 static struct clk clk12m = {
230         .name           = "clk12m",
231         .rate           = 12000000,
232         .parent         = &xin24m,
233         .flags          = RATE_FIXED,
234         .gate_idx       = -1,
235 };
236
237 static struct clk extclk = {
238         .name           = "extclk",
239         .rate           = 27000000,
240         .flags          = RATE_FIXED,
241         .gate_idx       = -1,
242 };
243
244 static unsigned long pll_clk_recalc(struct clk *clk);
245 static int pll_clk_set_rate(struct clk *clk, unsigned long rate);
246
247 #define PLL_CLK(NAME,IDX) \
248 static struct clk NAME##_pll_clk = { \
249         .name           = #NAME"_pll", \
250         .parent         = &xin24m, \
251         .pll_idx        = IDX, \
252         .recalc         = pll_clk_recalc, \
253         .set_rate       = pll_clk_set_rate, \
254         .gate_idx       = -1, \
255 }
256
257 PLL_CLK(arm, 0);
258 PLL_CLK(dsp, 1);
259 PLL_CLK(codec, 2);
260
261 static unsigned long pll_clk_recalc(struct clk *clk)
262 {
263         u32 mask, normal, *reg;
264         unsigned long rate;
265
266         if (clk == &arm_pll_clk) {
267                 mask = SCU_CPUMODE_MASK;
268                 normal = SCU_CPUMODE_NORMAL;
269                 reg = &scu_register_base->scu_mode_config;
270         } else if (clk == &dsp_pll_clk) {
271                 mask = SCU_DSPMODE_MASK;
272                 normal = SCU_DSPMODE_NORMAL;
273                 reg = &scu_register_base->scu_mode_config;
274         } else if (clk == &codec_pll_clk) {
275                 mask = CLK_CPLL_MASK;
276                 normal = CLK_CPLL_NORMAL;
277                 reg = &scu_register_base->scu_clksel1_config;
278         } else {
279                 return 0;
280         }
281
282         if ((readl(reg) & mask) == normal) {
283                 u32 v = readl(&scu_register_base->scu_pll_config[clk->pll_idx]);
284                 u32 OD = ((v >> 1) & 0x7) + 1;
285                 u32 NF = ((v >> 4) & 0xfff) + 1;
286                 u32 NR = ((v >> 16) & 0x3f) + 1;
287                 rate = clk->parent->rate / NR * NF / OD;
288                 pr_debug("clock: %s new clock rate is %ld NR %d NF %d OD %d\n", clk->name, rate, NR, NF, OD);
289         } else {
290                 rate = clk->parent->rate;
291                 pr_debug("clock: %s new clock rate is %ld (slow mode)\n", clk->name, rate);
292         }
293
294         return rate;
295 }
296
297 static void pll_clk_slow_mode(struct clk *clk, int enter)
298 {
299         u32 mask, value, *reg;
300
301         pr_debug("clock: %s %s slow mode\n", clk->name, enter ? "enter" : "exit");
302         if (clk == &arm_pll_clk) {
303                 mask = SCU_CPUMODE_MASK;
304                 value = enter ? SCU_CPUMODE_SLOW : SCU_CPUMODE_NORMAL;
305                 reg = &scu_register_base->scu_mode_config;
306         } else if (clk == &dsp_pll_clk) {
307                 mask = SCU_DSPMODE_MASK;
308                 value = enter ? SCU_DSPMODE_SLOW : SCU_DSPMODE_NORMAL;
309                 reg = &scu_register_base->scu_mode_config;
310         } else if (clk == &codec_pll_clk) {
311                 mask = CLK_CPLL_MASK;
312                 value = enter ? CLK_CPLL_SLOW : CLK_CPLL_NORMAL;
313                 reg = &scu_register_base->scu_clksel1_config;
314         } else {
315                 return;
316         }
317
318         writel((readl(reg) & ~mask) | value, reg);
319 }
320
321 static int pll_clk_set_rate(struct clk *clk, unsigned long rate)
322 {
323         u32 *reg = &scu_register_base->scu_pll_config[clk->pll_idx];
324         u32 v = readl(reg);
325
326         if (rate <= 24000000 ) {
327                 pll_clk_slow_mode(clk, 1);
328                 v |= PLL_PD;    /* pll power down */
329                 writel(v, reg);
330                 pr_debug("clock: %s power down", clk->name);
331         } else {
332                 int clkf, clkod, unit;
333
334                 unit = rate / 1000000;
335                 clkf = unit;
336                 clkod = 1;
337                 while (clkf < 200) { /* 160 <= Fref/NR * NF <= 800 , 200 set to the midia point */
338                         clkf += unit;
339                         clkod += 1;
340                 } 
341                 if (clkod > 8) { /* clkod max 8 , 8*24=192 > 160 , safe */
342                         clkod = 9;
343                         clkf = clkod * unit;
344                 }
345                 pll_clk_slow_mode(clk, 1);
346                 v &= ~PLL_PD;
347                 writel(v, reg);
348
349                 /* XXX:delay for pll state , for 0.3ms , clkf will lock clkf*/
350                 v = PLL_SAT|PLL_FAST|(PLL_CLKR(24-1))|(PLL_CLKF(clkf-1))|(PLL_CLKOD(clkod - 1));
351                 writel(v, reg);
352
353                 /* arm run at 24m */ //FIXME
354                 unit = 7200;  /* 24m,0.3ms , 24*300*/
355                 while (unit-- > 0) {
356                         v = readl(RK2818_REGFILE_BASE + CPU_APB_REG0);
357                         if (v & (0x80u << clk->pll_idx) )
358                                 break;
359                 }
360                 pll_clk_slow_mode(clk, 0);
361                 pr_debug("clock: set %s to %ld MHZ, clkf=%d, clkod=%d, delay count=%d\n", clk->name, rate/1000000, clkf, clkod, unit);
362         }
363
364         return 0;
365 }
366
367 static int arm_clk_set_rate(struct clk *clk, unsigned long rate);
368
369 static struct clk arm_clk = {
370         .name           = "arm",
371         .parent         = &arm_pll_clk,
372         .recalc         = clksel_recalc,
373 #ifdef CONFIG_CPU_FREQ
374         .set_rate       = arm_clk_set_rate,
375 #endif
376         .gate_idx       = -1,
377         .clksel_reg     = CLKSEL2_REG,
378         .clksel_mask    = 0xF,
379         .clksel_maxdiv  = 16,
380 };
381
382 static struct clk arm_hclk = {
383         .name           = "arm_hclk",
384         .parent         = &arm_clk,
385         .recalc         = clksel_recalc,
386         .gate_idx       = -1,
387         .clksel_reg     = CLKSEL0_REG,
388         .clksel_mask    = 0x3,
389         .clksel_maxdiv  = 4,
390 };
391
392 static struct clk clk48m = {
393         .name           = "clk48m",
394         .parent         = &arm_clk,
395         .flags          = RATE_FIXED,
396         .recalc         = clksel_recalc,
397         .gate_idx       = -1,
398         .clksel_reg     = CLKSEL2_REG,
399         .clksel_mask    = 0xF << 4,
400         .clksel_shift   = 4,
401 };
402
403 static struct clk arm_pclk = {
404         .name           = "arm_pclk",
405         .parent         = &arm_hclk,
406         .flags          = ENABLE_ON_INIT,
407         .recalc         = clksel_recalc_shift,
408         .set_rate       = clksel_set_rate_shift,
409         .gate_idx       = -1,
410         .clksel_reg     = CLKSEL0_REG,
411         .clksel_mask    = 0x3 << 2,
412         .clksel_shift   = 2,
413         .clksel_maxdiv  = 4,
414 };
415
416 static struct clk* demod_divider_clk_get_parent(struct clk *clk)
417 {
418         u32 r = readl(CLK_DEMOD_REG) & CLK_DEMOD_DIV_SRC_MASK;
419         return (r == CLK_DEMOD_CODPLL) ? &codec_pll_clk : (r == CLK_DEMOD_ARMPLL) ? &arm_pll_clk : (r == CLK_DEMOD_DSPPLL) ? &dsp_pll_clk : clk->parent;
420 }
421
422 static int demod_divider_clk_set_parent(struct clk *clk, struct clk *parent)
423 {
424         u32 r = readl(CLK_DEMOD_REG) & ~CLK_DEMOD_DIV_SRC_MASK;
425
426         if (parent == &codec_pll_clk) {
427                 r |= CLK_DEMOD_CODPLL;
428         } else if (parent == &arm_pll_clk) {
429                 r |= CLK_DEMOD_ARMPLL;
430         } else if (parent == &dsp_pll_clk) {
431                 r |= CLK_DEMOD_DSPPLL;
432         } else {
433                 return -EINVAL;
434         }
435         writel(r, CLK_DEMOD_REG);
436
437         return 0;
438 }
439
440 static struct clk demod_divider_clk = {
441         .name           = "demod_divider",
442         .parent         = &codec_pll_clk,
443         .recalc         = clksel_recalc,
444         .set_rate       = clksel_set_rate,
445         .get_parent     = demod_divider_clk_get_parent,
446         .set_parent     = demod_divider_clk_set_parent,
447         .gate_idx       = -1,
448         .clksel_reg     = CLKSEL1_REG,
449         .clksel_mask    = 0xFF << 16,
450         .clksel_shift   = 16,
451         .clksel_maxdiv  = 128,
452 };
453
454 static struct clk* demod_clk_get_parent(struct clk *clk)
455 {
456         u32 r = readl(CLK_DEMOD_REG) & CLK_DEMOD_SRC_MASK;
457         return (r == CLK_DEMOD_DIVOUT) ? &demod_divider_clk : &extclk;
458 }
459
460 static int demod_clk_set_parent(struct clk *clk, struct clk *parent)
461 {
462         u32 r = readl(CLK_DEMOD_REG) & ~CLK_DEMOD_SRC_MASK;
463
464         if (parent == &extclk) {
465                 r |= CLK_DEMOD_27M;
466         } else if (parent == &demod_divider_clk) {
467                 r |= CLK_DEMOD_DIVOUT;
468         } else {
469                 return -EINVAL;
470         }
471         writel(r, CLK_DEMOD_REG);
472
473         return 0;
474 }
475
476 static struct clk demod_clk = {
477         .name           = "demod",
478         .parent         = &demod_divider_clk,
479         .recalc         = followparent_recalc,
480         .get_parent     = demod_clk_get_parent,
481         .set_parent     = demod_clk_set_parent,
482         .gate_idx       = -1,
483 };
484
485 static struct clk codec_clk = {
486         .name           = "codec",
487         .parent         = &codec_pll_clk,
488         .recalc         = clksel_recalc,
489         .set_rate       = clksel_set_rate,
490         .gate_idx       = -1,
491         .clksel_reg     = CLKSEL1_REG,
492         .clksel_mask    = 0x1F << 3,
493         .clksel_shift   = 3,
494         .clksel_maxdiv  = 32,
495 };
496
497 static struct clk* lcdc_divider_clk_get_parent(struct clk *clk)
498 {
499         u32 r = readl(CLK_LCDC_REG) & CLK_LCDC_DIV_SRC_MASK;
500         return (r == CLK_LCDC_ARMPLL) ? &arm_pll_clk : (r == CLK_LCDC_DSPPLL) ? &dsp_pll_clk : (r == CLK_LCDC_CODPLL) ? &codec_pll_clk : clk->parent;
501 }
502
503 static int lcdc_divider_clk_set_parent(struct clk *clk, struct clk *parent)
504 {
505         u32 r = readl(CLK_LCDC_REG) & ~CLK_LCDC_DIV_SRC_MASK;
506
507         if (parent == &arm_pll_clk) {
508                 r |= CLK_LCDC_ARMPLL;
509         } else if (parent == &dsp_pll_clk) {
510                 r |= CLK_LCDC_DSPPLL;
511         } else if (parent == &codec_pll_clk) {
512                 r |= CLK_LCDC_CODPLL;
513         } else {
514                 return -EINVAL;
515         }
516         writel(r, CLK_LCDC_REG);
517
518         return 0;
519 }
520
521 static struct clk lcdc_divider_clk = {
522         .name           = "lcdc_divider",
523         .parent         = &arm_pll_clk,
524         .recalc         = clksel_recalc,
525         .set_rate       = clksel_set_rate,
526         .get_parent     = lcdc_divider_clk_get_parent,
527         .set_parent     = lcdc_divider_clk_set_parent,
528         .gate_idx       = -1,
529         .clksel_reg     = CLKSEL0_REG,
530         .clksel_mask    = 0xFF << 8,
531         .clksel_shift   = 8,
532         .clksel_maxdiv  = 128,
533 };
534
535 static struct clk* otgphy_clk_get_parent(struct clk *clk)
536 {
537         u32 r = readl(CLK_USBPHY_REG) & CLK_USBPHY_SRC_MASK;
538         return (r == CLK_USBPHY_24M) ? &xin24m : (r == CLK_USBPHY_12M) ? &clk12m : (r == CLK_USBPHY_48M) ? &clk48m : clk->parent;
539 }
540
541 static int otgphy_clk_set_parent(struct clk *clk, struct clk *parent)
542 {
543         u32 r = readl(CLK_USBPHY_REG) & ~CLK_USBPHY_SRC_MASK;
544
545         if (parent == &xin24m) {
546                 r |= CLK_USBPHY_24M;
547         } else if (parent == &clk12m) {
548                 r |= CLK_USBPHY_12M;
549         } else if (parent == &clk48m) {
550                 r |= CLK_USBPHY_48M;
551         } else {
552                 return -EINVAL;
553         }
554         writel(r, CLK_USBPHY_REG);
555
556         return 0;
557 }
558
559 static struct clk* lcdc_clk_get_parent(struct clk *clk)
560 {
561         u32 r = readl(CLK_LCDC_REG) & CLK_LCDC_SRC_MASK;
562         return (r == CLK_LCDC_DIVOUT) ? &lcdc_divider_clk : &extclk;
563 }
564
565 static int lcdc_clk_set_parent(struct clk *clk, struct clk *parent)
566 {
567         u32 r = readl(CLK_LCDC_REG) & ~CLK_LCDC_SRC_MASK;
568
569         if (parent == &lcdc_divider_clk) {
570                 r |= CLK_LCDC_DIVOUT;
571         } else if (parent == &extclk) {
572                 r |= CLK_LCDC_27M;
573         } else {
574                 return -EINVAL;
575         }
576         writel(r, CLK_LCDC_REG);
577
578         return 0;
579 }
580
581 static struct clk* vip_clk_get_parent(struct clk *clk)
582 {
583         u32 r = readl(CLK_SENSOR_REG) & CLK_SENSOR_SRC_MASK;
584         return (r == CLK_SENSOR_24M) ? &xin24m : (r == CLK_SENSOR_27M) ? &extclk : (r == CLK_SENSOR_48M) ? &clk48m : clk->parent;
585 }
586
587 static int vip_clk_set_parent(struct clk *clk, struct clk *parent)
588 {
589         u32 r = readl(CLK_SENSOR_REG) & ~CLK_SENSOR_SRC_MASK;
590
591         if (parent == &xin24m) {
592                 r |= CLK_SENSOR_24M;
593         } else if (parent == &extclk) {
594                 r |= CLK_SENSOR_27M;
595         } else if (parent == &clk48m) {
596                 r |= CLK_SENSOR_48M;
597         } else {
598                 return -EINVAL;
599         }
600         writel(r, CLK_SENSOR_REG);
601
602         return 0;
603 }
604
605 static struct clk* ddr_clk_get_parent(struct clk *clk)
606 {
607         u32 r = readl(CLK_DDR_REG) & CLK_DDR_SRC_MASK;
608         return (r == CLK_DDR_CODPLL) ? &codec_pll_clk : (r == CLK_DDR_ARMPLL) ? &arm_pll_clk : (r == CLK_DDR_DSPPLL) ? &dsp_pll_clk : clk->parent;
609 }
610
611 static int ddr_clk_set_parent(struct clk *clk, struct clk *parent)
612 {
613         u32 r = readl(CLK_DDR_REG) & ~CLK_DDR_SRC_MASK;
614
615         if (parent == &codec_pll_clk) {
616                 r |= CLK_DDR_CODPLL;
617         } else if (parent == &arm_pll_clk) {
618                 r |= CLK_DDR_ARMPLL;
619         } else if (parent == &dsp_pll_clk) {
620                 r |= CLK_DDR_DSPPLL;
621         } else {
622                 return -EINVAL;
623         }
624         writel(r, CLK_DDR_REG);
625
626         return 0;
627 }
628
629 static struct clk* i2s_clk_get_parent(struct clk *clk)
630 {
631         u32 r = readl(CLK_CODEC_REG) & CLK_CODEC_SRC_MASK;
632         return (r == CLK_CODEC_CPLLCLK) ? &codec_clk : &clk12m;
633 }
634
635 static int i2s_clk_set_parent(struct clk *clk, struct clk *parent)
636 {
637         u32 r = readl(CLK_CODEC_REG) & ~CLK_CODEC_SRC_MASK;
638
639         if (parent == &codec_clk) {
640                 r |= CLK_CODEC_CPLLCLK;
641         } else if (parent == &clk12m) {
642                 r |= CLK_CODEC_12M;
643         } else {
644                 return -EINVAL;
645         }
646         writel(r, CLK_CODEC_REG);
647
648         return 0;
649 }
650
651 static int gate_mode(struct clk *clk, int on)
652 {
653         u32 *reg;
654         int idx = clk->gate_idx;
655         u32 v;
656
657         if (idx >= CLK_GATE_MAX)
658                 return -EINVAL;
659
660         reg = &scu_register_base->scu_clkgate0_config;
661         reg += (idx >> 5);
662         idx &= 0x1F;
663
664         v = readl(reg);
665         if (on) {
666                 v &= ~(1 << idx);       // clear bit 
667         } else {
668                 v |= (1 << idx);        // set bit
669         }
670         writel(v, reg);
671
672         return 0;
673 }
674
675 static struct clk* uart_clk_get_parent(struct clk *clk)
676 {
677         u32 r = readl(CLK_UART_REG) & CLK_UART_SRC_MASK;
678         return (r == CLK_UART_24M) ? &xin24m : &clk48m;
679 }
680
681 static int uart_clk_set_parent(struct clk *clk, struct clk *parent)
682 {
683         u32 r = readl(CLK_UART_REG) & ~CLK_UART_SRC_MASK;
684
685         if (parent == &xin24m) {
686                 r |= CLK_UART_24M;
687         } else if (parent == &clk48m) {
688                 r |= CLK_UART_48M;
689         } else {
690                 return -EINVAL;
691         }
692         writel(r, CLK_UART_REG);
693
694         return 0;
695 }
696
697 #define UART_CLK(n) \
698 static struct clk uart##n##_clk = { \
699         .name           = "uart"#n, \
700         .parent         = &xin24m, \
701         .mode           = gate_mode, \
702         .recalc         = followparent_recalc, \
703         .get_parent     = uart_clk_get_parent, \
704         .set_parent     = uart_clk_set_parent, \
705         .gate_idx       = CLK_GATE_UART##n, \
706 }
707
708 #define GATE_CLK(NAME,PARENT,ID) \
709 static struct clk NAME##_clk = { \
710         .name           = #NAME, \
711         .parent         = &PARENT, \
712         .mode           = gate_mode, \
713         .recalc         = followparent_recalc, \
714         .gate_idx       = CLK_GATE_##ID, \
715 }
716
717 GATE_CLK(arm_core, arm_clk, ARM);
718 GATE_CLK(dsp, dsp_pll_clk, DSP);
719 GATE_CLK(dma, arm_hclk, DMA);
720 GATE_CLK(sramarm, arm_hclk, SRAMARM);
721 GATE_CLK(sramdsp, arm_hclk, SRAMDSP);
722 GATE_CLK(hif, arm_hclk, HIF);
723 GATE_CLK(otgbus, arm_hclk, OTGBUS);
724 static struct clk otgphy_clk = {
725         .name           = "otgphy",
726         .parent         = &xin24m,
727         .mode           = gate_mode,
728         .recalc         = followparent_recalc,
729         .get_parent     = otgphy_clk_get_parent,
730         .set_parent     = otgphy_clk_set_parent,
731         .gate_idx       = CLK_GATE_OTGPHY,
732 };
733 GATE_CLK(nandc, arm_hclk, NANDC);
734 GATE_CLK(intc, arm_hclk, INTC);
735 GATE_CLK(deblocking_rv, arm_hclk, DEBLK);
736 static struct clk lcdc_clk = {
737         .name           = "lcdc",
738         .parent         = &lcdc_divider_clk,
739         .mode           = gate_mode,
740         .recalc         = followparent_recalc,
741         .get_parent     = lcdc_clk_get_parent,
742         .set_parent     = lcdc_clk_set_parent,
743         .gate_idx       = CLK_GATE_LCDC,
744 };
745 static struct clk vip_clk = {
746         .name           = "vip",
747         .parent         = &xin24m,
748         .mode           = gate_mode,
749         .recalc         = followparent_recalc,
750         .get_parent     = vip_clk_get_parent,
751         .set_parent     = vip_clk_set_parent,
752         .gate_idx       = CLK_GATE_VIP,
753 };
754 static struct clk i2s_clk = {
755         .name           = "i2s",
756         .parent         = &clk12m,
757         .mode           = gate_mode,
758         .recalc         = followparent_recalc,
759         .get_parent     = i2s_clk_get_parent,
760         .set_parent     = i2s_clk_set_parent,
761         .gate_idx       = CLK_GATE_I2S,
762 };
763 static struct clk sdmmc0_clk = {
764         .name           = "sdmmc0",
765         .parent         = &arm_hclk,
766         .mode           = gate_mode,
767         .recalc         = clksel_recalc,
768         .set_rate       = clksel_set_rate,
769         .gate_idx       = CLK_GATE_SDMMC0,
770         .clksel_reg     = CLKSEL0_REG,
771         .clksel_mask    = 7 << 4,
772         .clksel_shift   = 4,
773         .clksel_maxdiv  = 8,
774 };
775
776 GATE_CLK(ebrom, arm_hclk, EBROM);
777 GATE_CLK(gpio0, arm_pclk, GPIO0);
778 GATE_CLK(gpio1, arm_pclk, GPIO1);
779 UART_CLK(0);
780 UART_CLK(1);
781 GATE_CLK(i2c0, arm_pclk, I2C0);
782 GATE_CLK(i2c1, arm_pclk, I2C1);
783 GATE_CLK(spi0, arm_pclk, SPI0);
784 GATE_CLK(spi1, arm_pclk, SPI1);
785 GATE_CLK(pwm, arm_pclk, PWM);
786 GATE_CLK(timer, arm_pclk, TIMER);
787 GATE_CLK(wdt, arm_pclk, WDT);
788 GATE_CLK(rtc, arm_pclk, RTC);
789 static struct clk lsadc_clk = {
790         .name           = "lsadc",
791         .parent         = &arm_pclk,
792         .mode           = gate_mode,
793         .recalc         = clksel_recalc,
794         .set_rate       = clksel_set_rate,
795         .gate_idx       = CLK_GATE_LSADC,
796         .clksel_reg     = CLKSEL1_REG,
797         .clksel_mask    = 0xFF << 8,
798         .clksel_shift   = 8,
799         .clksel_maxdiv  = 128,
800 };
801 UART_CLK(2);
802 UART_CLK(3);
803 static struct clk sdmmc1_clk = {
804         .name           = "sdmmc1",
805         .parent         = &arm_hclk,
806         .mode           = gate_mode,
807         .recalc         = clksel_recalc,
808         .set_rate       = clksel_set_rate,
809         .gate_idx       = CLK_GATE_SDMMC1,
810         .clksel_reg     = CLKSEL2_REG,
811         .clksel_mask    = 7 << 8,
812         .clksel_shift   = 8,
813         .clksel_maxdiv  = 8,
814 };
815
816 static unsigned long hsadc_clk_recalc(struct clk *clk)
817 {
818         return clk->parent->rate >> 1;
819 }
820
821 static struct clk hsadc_clk = {
822         .name           = "hsadc",
823         .parent         = &demod_clk,
824         .mode           = gate_mode,
825         .recalc         = hsadc_clk_recalc,
826         .gate_idx       = CLK_GATE_HSADC,
827 };
828 GATE_CLK(sdram_common, arm_hclk, SDRAM_COMMON);
829 GATE_CLK(sdram_controller, arm_hclk, SDRAM_CONTROLLER);
830 GATE_CLK(mobile_sdram_controller, arm_hclk, MOBILE_SDRAM_CONTROLLER);
831 GATE_CLK(lcdc_share_memory, arm_hclk, LCDC_SHARE_MEMORY);
832 GATE_CLK(lcdc_hclk, arm_hclk, LCDC_HCLK);
833 GATE_CLK(deblocking_h264, arm_hclk, DEBLK_H264);
834 GATE_CLK(gpu, arm_hclk, GPU);
835 GATE_CLK(ddr_hclk, arm_hclk, DDR_HCLK);
836 static struct clk ddr_clk = {
837         .name           = "ddr",
838         .parent         = &codec_pll_clk,
839         .mode           = gate_mode,
840         .recalc         = clksel_recalc_shift,
841         .set_rate       = clksel_set_rate_shift,
842         .get_parent     = ddr_clk_get_parent,
843         .set_parent     = ddr_clk_set_parent,
844         .gate_idx       = CLK_GATE_DDR,
845         .clksel_reg     = CLKSEL0_REG,
846         .clksel_mask    = 0x3 << 30,
847         .clksel_shift   = 30,
848         .clksel_maxdiv  = 8,
849 };
850 GATE_CLK(customized_sdram_controller, arm_hclk, CUSTOMIZED_SDRAM_CONTROLLER);
851 GATE_CLK(mcdma, arm_hclk, MCDMA);
852 GATE_CLK(sdram, arm_hclk, SDRAM);
853 GATE_CLK(ddr_axi, arm_hclk, DDR_AXI);
854 GATE_CLK(dsp_timer, arm_hclk, DSP_TIMER);
855 GATE_CLK(dsp_slave, arm_hclk, DSP_SLAVE);
856 GATE_CLK(dsp_master, arm_hclk, DSP_MASTER);
857 GATE_CLK(usb_host, clk48m, USB_HOST);
858
859 GATE_CLK(armibus, arm_hclk, ARMIBUS);
860 GATE_CLK(armdbus, arm_hclk, ARMDBUS);
861 GATE_CLK(dspbus, arm_hclk, DSPBUS);
862 GATE_CLK(expbus, arm_hclk, EXPBUS);
863 GATE_CLK(apbbus, arm_hclk, APBBUS);
864 GATE_CLK(efuse, arm_pclk, EFUSE);
865 GATE_CLK(dtcm1, arm_clk, DTCM1);
866 GATE_CLK(dtcm0, arm_clk, DTCM0);
867 GATE_CLK(itcm, arm_clk, ITCM);
868 GATE_CLK(videobus, arm_hclk, VIDEOBUS);
869
870 #define CLK(dev, con, ck) \
871         { \
872                 .dev_id = dev, \
873                 .con_id = con, \
874                 .clk = ck, \
875         }
876
877 #define CLK1(name) \
878         { \
879                 .dev_id = NULL, \
880                 .con_id = #name, \
881                 .clk = &name##_clk, \
882         }
883
884 static struct clk_lookup clks[] = {
885         CLK(NULL, "xin24m", &xin24m),
886         CLK(NULL, "extclk", &extclk),
887
888         CLK(NULL, "clk12m", &clk12m),
889         CLK1(arm_pll),
890         CLK1(dsp_pll),
891         CLK1(codec_pll),
892         CLK1(arm),
893         CLK(NULL, "arm_hclk", &arm_hclk),
894         CLK(NULL, "clk48m", &clk48m),
895         CLK(NULL, "arm_pclk", &arm_pclk),
896         CLK1(demod_divider),
897         CLK1(demod),
898         CLK1(codec),
899         CLK1(lcdc_divider),
900
901         CLK1(arm_core),
902         CLK1(dsp),
903         CLK1(dma),
904         CLK1(sramarm),
905         CLK1(sramdsp),
906         CLK1(hif),
907         CLK1(otgbus),
908         CLK1(otgphy),
909         CLK1(nandc),
910         CLK1(intc),
911         CLK1(deblocking_rv),
912         CLK1(lcdc),
913         CLK1(vip),
914         CLK1(i2s),
915         CLK("rk2818_sdmmc.0", "sdmmc", &sdmmc0_clk),
916         CLK1(ebrom),
917         CLK1(gpio0),
918         CLK1(gpio1),
919         CLK("rk2818_serial.0", "uart", &uart0_clk),
920         CLK("rk2818_serial.1", "uart", &uart1_clk),
921         CLK("rk2818_i2c.0", "i2c", &i2c0_clk),
922         CLK("rk2818_i2c.1", "i2c", &i2c1_clk),
923         CLK("rk2818_spim.0", "spi", &spi0_clk),
924         CLK1(spi1),
925         CLK1(pwm),
926         CLK1(timer),
927         CLK1(wdt),
928         CLK1(rtc),
929         CLK1(lsadc),
930         CLK("rk2818_serial.2", "uart", &uart2_clk),
931         CLK("rk2818_serial.3", "uart", &uart3_clk),
932         CLK("rk2818_sdmmc.1", "sdmmc", &sdmmc1_clk),
933
934         CLK1(hsadc),
935         CLK1(sdram_common),
936         CLK1(sdram_controller),
937         CLK1(mobile_sdram_controller),
938         CLK1(lcdc_share_memory),
939         CLK1(lcdc_hclk),
940         CLK1(deblocking_h264),
941         CLK1(gpu),
942         CLK1(ddr_hclk),
943         CLK1(ddr),
944         CLK1(customized_sdram_controller),
945         CLK1(mcdma),
946         CLK1(sdram),
947         CLK1(ddr_axi),
948         CLK1(dsp_timer),
949         CLK1(dsp_slave),
950         CLK1(dsp_master),
951         CLK1(usb_host),
952
953         CLK1(armibus),
954         CLK1(armdbus),
955         CLK1(dspbus),
956         CLK1(expbus),
957         CLK1(apbbus),
958         CLK1(efuse),
959         CLK1(dtcm1),
960         CLK1(dtcm0),
961         CLK1(itcm),
962         CLK1(videobus),
963 };
964
965 static LIST_HEAD(clocks);
966 static DEFINE_MUTEX(clocks_mutex);
967 static DEFINE_SPINLOCK(clockfw_lock);
968
969 static int __clk_enable(struct clk *clk)
970 {
971         int ret = 0;
972
973         if (clk->usecount == 0) {
974                 if (clk->parent) {
975                         ret = __clk_enable(clk->parent);
976                         if (ret)
977                                 return ret;
978                 }
979
980                 if (clk->mode) {
981                         ret = clk->mode(clk, 1);
982                         if (ret) {
983                                 if (clk->parent)
984                                         __clk_disable(clk->parent);
985                                 return ret;
986                         }
987                 }
988                 pr_debug("clock: %s enabled\n", clk->name);
989         }
990         clk->usecount++;
991
992         return ret;
993 }
994
995 int clk_enable(struct clk *clk)
996 {
997         int ret = 0;
998         unsigned long flags;
999
1000         if (clk == NULL || IS_ERR(clk))
1001                 return -EINVAL;
1002
1003         spin_lock_irqsave(&clockfw_lock, flags);
1004         ret = __clk_enable(clk);
1005         spin_unlock_irqrestore(&clockfw_lock, flags);
1006
1007         return ret;
1008 }
1009 EXPORT_SYMBOL(clk_enable);
1010
1011 static void __clk_disable(struct clk *clk)
1012 {
1013         if (--clk->usecount == 0) {
1014                 if (clk->mode)
1015                         clk->mode(clk, 0);
1016                 pr_debug("clock: %s disabled\n", clk->name);
1017         }
1018         if (clk->parent)
1019                 __clk_disable(clk->parent);
1020 }
1021
1022 void clk_disable(struct clk *clk)
1023 {
1024         unsigned long flags;
1025
1026         if (clk == NULL || IS_ERR(clk))
1027                 return;
1028
1029         spin_lock_irqsave(&clockfw_lock, flags);
1030         if (clk->usecount == 0) {
1031                 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n", clk->name);
1032                 WARN_ON(1);
1033                 goto out;
1034         }
1035
1036         __clk_disable(clk);
1037
1038 out:
1039         spin_unlock_irqrestore(&clockfw_lock, flags);
1040 }
1041 EXPORT_SYMBOL(clk_disable);
1042
1043 unsigned long clk_get_rate(struct clk *clk)
1044 {
1045         unsigned long flags;
1046         unsigned long ret;
1047
1048         if (clk == NULL || IS_ERR(clk))
1049                 return 0;
1050
1051         spin_lock_irqsave(&clockfw_lock, flags);
1052         ret = clk->rate;
1053         spin_unlock_irqrestore(&clockfw_lock, flags);
1054
1055         return ret;
1056 }
1057 EXPORT_SYMBOL(clk_get_rate);
1058
1059 /*-------------------------------------------------------------------------
1060  * Optional clock functions defined in include/linux/clk.h
1061  *-------------------------------------------------------------------------*/
1062
1063 /* Given a clock and a rate apply a clock specific rounding function */
1064 static long __clk_round_rate(struct clk *clk, unsigned long rate)
1065 {
1066         if (clk->round_rate)
1067                 return clk->round_rate(clk, rate);
1068
1069         if (clk->flags & RATE_FIXED)
1070                 printk(KERN_ERR "clock: clk_round_rate called on fixed-rate clock %s\n", clk->name);
1071
1072         return clk->rate;
1073 }
1074
1075 long clk_round_rate(struct clk *clk, unsigned long rate)
1076 {
1077         unsigned long flags;
1078         long ret = 0;
1079
1080         if (clk == NULL || IS_ERR(clk))
1081                 return ret;
1082
1083         spin_lock_irqsave(&clockfw_lock, flags);
1084         ret = __clk_round_rate(clk, rate);
1085         spin_unlock_irqrestore(&clockfw_lock, flags);
1086
1087         return ret;
1088 }
1089 EXPORT_SYMBOL(clk_round_rate);
1090
1091 /* Set the clock rate for a clock source */
1092 static int __clk_set_rate(struct clk *clk, unsigned long rate)
1093 {
1094         int ret = -EINVAL;
1095
1096         pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
1097
1098         if (clk->flags & CONFIG_PARTICIPANT)
1099                 return -EINVAL;
1100
1101         if (clk->set_rate)
1102                 ret = clk->set_rate(clk, rate);
1103
1104         return ret;
1105 }
1106
1107 int clk_set_rate(struct clk *clk, unsigned long rate)
1108 {
1109         unsigned long flags;
1110         int ret = -EINVAL;
1111
1112         if (clk == NULL || IS_ERR(clk))
1113                 return ret;
1114
1115         spin_lock_irqsave(&clockfw_lock, flags);
1116         ret = __clk_set_rate(clk, rate);
1117         if (ret == 0) {
1118                 if (clk->recalc)
1119                         clk->rate = clk->recalc(clk);
1120                 propagate_rate(clk);
1121         }
1122         spin_unlock_irqrestore(&clockfw_lock, flags);
1123
1124         return ret;
1125 }
1126 EXPORT_SYMBOL(clk_set_rate);
1127
1128 int clk_set_parent(struct clk *clk, struct clk *parent)
1129 {
1130         unsigned long flags;
1131         int ret = -EINVAL;
1132
1133         if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
1134                 return ret;
1135
1136         if (clk->set_parent == NULL)
1137                 return ret;
1138
1139         spin_lock_irqsave(&clockfw_lock, flags);
1140         if (clk->usecount == 0) {
1141                 ret = clk->set_parent(clk, parent);
1142                 if (ret == 0) {
1143                         clk_reparent(clk, parent);
1144                         if (clk->recalc)
1145                                 clk->rate = clk->recalc(clk);
1146                         propagate_rate(clk);
1147                 }
1148         } else
1149                 ret = -EBUSY;
1150         spin_unlock_irqrestore(&clockfw_lock, flags);
1151
1152         return ret;
1153 }
1154 EXPORT_SYMBOL(clk_set_parent);
1155
1156 struct clk *clk_get_parent(struct clk *clk)
1157 {
1158         return clk->parent;
1159 }
1160 EXPORT_SYMBOL(clk_get_parent);
1161
1162 static void clk_reparent(struct clk *child, struct clk *parent)
1163 {
1164         if (child->parent == parent)
1165                 return;
1166         pr_debug("clock: %s reparent to %s (was %s)\n", child->name, parent->name, ((child->parent) ? child->parent->name : "NULL"));
1167
1168         list_del_init(&child->sibling);
1169         if (parent)
1170                 list_add(&child->sibling, &parent->children);
1171         child->parent = parent;
1172 }
1173
1174 /* Propagate rate to children */
1175 static void propagate_rate(struct clk *tclk)
1176 {
1177         struct clk *clkp;
1178
1179         list_for_each_entry(clkp, &tclk->children, sibling) {
1180                 if (clkp->recalc)
1181                         clkp->rate = clkp->recalc(clkp);
1182                 propagate_rate(clkp);
1183         }
1184 }
1185
1186 static LIST_HEAD(root_clks);
1187
1188 /**
1189  * recalculate_root_clocks - recalculate and propagate all root clocks
1190  *
1191  * Recalculates all root clocks (clocks with no parent), which if the
1192  * clock's .recalc is set correctly, should also propagate their rates.
1193  * Called at init.
1194  */
1195 static void recalculate_root_clocks(void)
1196 {
1197         struct clk *clkp;
1198
1199         list_for_each_entry(clkp, &root_clks, sibling) {
1200                 if (clkp->recalc)
1201                         clkp->rate = clkp->recalc(clkp);
1202                 propagate_rate(clkp);
1203         }
1204 }
1205
1206 void clk_recalculate_root_clocks(void)
1207 {
1208         unsigned long flags;
1209
1210         spin_lock_irqsave(&clockfw_lock, flags);
1211         recalculate_root_clocks();
1212         spin_unlock_irqrestore(&clockfw_lock, flags);
1213 }
1214
1215 /**
1216  * clk_preinit - initialize any fields in the struct clk before clk init
1217  * @clk: struct clk * to initialize
1218  *
1219  * Initialize any struct clk fields needed before normal clk initialization
1220  * can run.  No return value.
1221  */
1222 static void clk_preinit(struct clk *clk)
1223 {
1224         INIT_LIST_HEAD(&clk->children);
1225 }
1226
1227 static int clk_register(struct clk *clk)
1228 {
1229         if (clk == NULL || IS_ERR(clk))
1230                 return -EINVAL;
1231
1232         /*
1233          * trap out already registered clocks
1234          */
1235         if (clk->node.next || clk->node.prev)
1236                 return 0;
1237
1238         mutex_lock(&clocks_mutex);
1239
1240         if (clk->get_parent)
1241                 clk->parent = clk->get_parent(clk);
1242
1243         if (clk->parent)
1244                 list_add(&clk->sibling, &clk->parent->children);
1245         else
1246                 list_add(&clk->sibling, &root_clks);
1247
1248         list_add(&clk->node, &clocks);
1249
1250         mutex_unlock(&clocks_mutex);
1251
1252         return 0;
1253 }
1254
1255 static void clk_enable_init_clocks(void)
1256 {
1257         struct clk *clkp;
1258
1259         list_for_each_entry(clkp, &clocks, node) {
1260                 if (clkp->flags & ENABLE_ON_INIT)
1261                         clk_enable(clkp);
1262         }
1263 }
1264
1265 #ifdef CONFIG_CPU_FREQ
1266 #include <linux/cpufreq.h>
1267
1268 struct rk2818_freq_info {
1269         u16     arm_mhz;
1270         u16     ddr_mhz;
1271         u32     apll_con;
1272         u8      clk48m : 4;
1273         u8      hclk : 2;
1274         u8      pclk : 2;
1275 };
1276
1277 #define CLK_HCLK_PCLK_11        0
1278 #define CLK_HCLK_PCLK_21        1
1279 #define CLK_HCLK_PCLK_41        2
1280
1281 #define CLK_ARM_HCLK_11         0
1282 #define CLK_ARM_HCLK_21         1
1283 #define CLK_ARM_HCLK_31         2
1284 #define CLK_ARM_HCLK_41         3
1285
1286 #define OP(_arm_mhz, _ddr_mhz, nf, nr, od, _clk48m, _hclk, _pclk) \
1287 {                                                       \
1288         .arm_mhz        = _arm_mhz,                     \
1289         .ddr_mhz        = _ddr_mhz,                     \
1290         .apll_con       = PLL_SAT | PLL_FAST | PLL_CLKR(nr-1) | PLL_CLKF(nf-1) | PLL_CLKOD(od-1), \
1291         .clk48m         = _clk48m - 1,                  \
1292         .hclk           = CLK_ARM_HCLK_##_hclk,         \
1293         .pclk           = CLK_HCLK_PCLK_##_pclk,        \
1294 }
1295
1296 /*
1297  * F = 24MHz * NF / NR / OD
1298  * 97.7KHz < 24MHz / NR < 800MHz
1299  * 160MHz < 24MHz / NR * NF < 800MHz
1300  */
1301 static struct rk2818_freq_info rk2818_freqs[] = {
1302         /* ARM  DDR   NF  NR OD 48M HCLK PCLK */
1303 //X     OP(768, 350, 768, 24, 1, 16,  41,  41),
1304 //      OP(720, 350, 720, 24, 1, 15,  41,  21),
1305 //      OP(672, 350, 672, 24, 1, 14,  41,  21),
1306 //      OP(624, 350, 624, 24, 1, 13,  41,  21),
1307 //      OP(600, 350, 600, 24, 1, 12,  41,  21),
1308 //      OP(576, 350, 576, 24, 1, 12,  41,  21),
1309         OP(528, 350, 528, 24, 1, 11,  41,  21),
1310 //      OP(480, 350, 480, 24, 1, 10,  41,  21),
1311 //      OP(450, 350, 432, 24, 1,  9,  31,  21),
1312 //      OP(432, 350, 432, 24, 1,  9,  31,  21),
1313 //      OP(384, 350, 384, 24, 1,  8,  31,  21),
1314 //      OP(336, 350, 336, 24, 1,  7,  31,  21),
1315         OP(288, 350, 288, 24, 1,  6,  41,  21),
1316 };
1317
1318 /* CPU_APB_REG5 */
1319 #define MEMTYPEMASK   (0x3 << 11) 
1320 #define SDRAM         (0x0 << 11)
1321 #define Mobile_SDRAM  (0x1 << 11)
1322 #define DDRII         (0x2 << 11)
1323 #define Mobile_DDR    (0x3 << 11)
1324
1325 static int arm_clk_set_rate(struct clk *clk, unsigned long rate)
1326 {
1327         int i;
1328         struct rk2818_freq_info *freq;
1329         unsigned int arm_mhz = rate / 1000000;
1330         u32 *reg = &scu_register_base->scu_pll_config[arm_pll_clk.pll_idx];
1331         u32 v = readl(reg);
1332         u32 mem_type;
1333         u32 unit;
1334
1335         for (i = 0; i < ARRAY_SIZE(rk2818_freqs); i++) {
1336                 if (rk2818_freqs[i].arm_mhz == arm_mhz) {
1337                         break;
1338                 }
1339         }
1340
1341         if (i == ARRAY_SIZE(rk2818_freqs))
1342                 return -EINVAL;
1343
1344         freq = &rk2818_freqs[i];
1345
1346         mem_type = readl(RK2818_REGFILE_BASE + CPU_APB_REG0) & MEMTYPEMASK;
1347         if ((mem_type == DDRII || mem_type == Mobile_DDR) && (ddr_clk.parent == &arm_pll_clk)) {
1348                 pr_debug("clock: no set arm clk rate when ddr parent is arm_pll\n");
1349                 return -EINVAL;
1350         }
1351
1352         pll_clk_slow_mode(&arm_pll_clk, 1);
1353         v &= ~PLL_PD;
1354         writel(v, reg);
1355
1356         /* XXX:delay for pll state , for 0.3ms , clkf will lock clkf*/
1357         writel(freq->apll_con, reg);
1358
1359         v = readl(arm_hclk.clksel_reg) & ~arm_hclk.clksel_mask;
1360         v |= freq->hclk << arm_hclk.clksel_shift;
1361         writel(v, arm_hclk.clksel_reg);
1362
1363         v = readl(arm_pclk.clksel_reg) & ~arm_pclk.clksel_mask;
1364         v |= freq->pclk << arm_pclk.clksel_shift;
1365         writel(v, arm_pclk.clksel_reg);
1366
1367         v = readl(clk48m.clksel_reg) & ~clk48m.clksel_mask;
1368         v |= freq->clk48m << clk48m.clksel_shift;
1369         writel(v, clk48m.clksel_reg);
1370
1371         /* arm run at 24m */
1372         unit = 7200;  /* 24m,0.3ms , 24*300*/
1373         while (unit-- > 0) {
1374                 v = readl(RK2818_REGFILE_BASE + CPU_APB_REG0);
1375                 if (v & 0x80u)
1376                         break;
1377         }
1378         pll_clk_slow_mode(&arm_pll_clk, 0);
1379
1380         pr_debug("clock: set %s to %ld MHz, delay count=%d\n", clk->name, rate/1000000, unit);
1381
1382         arm_pll_clk.rate = arm_pll_clk.recalc(&arm_pll_clk);
1383         {
1384                 struct clk *clkp;
1385
1386                 list_for_each_entry(clkp, &arm_pll_clk.children, sibling) {
1387                         if (clkp == &arm_clk)
1388                                 continue;
1389                         if (clkp->recalc)
1390                                 clkp->rate = clkp->recalc(clkp);
1391                         propagate_rate(clkp);
1392                 }
1393         }
1394
1395         //FIXME: change sdram freq
1396
1397         return 0;
1398 }
1399
1400 static struct cpufreq_frequency_table freq_table[ARRAY_SIZE(rk2818_freqs) + 1];
1401
1402 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
1403 {
1404         int i;
1405
1406         for (i = 0; i < ARRAY_SIZE(rk2818_freqs); i++) {
1407                 freq_table[i].index = i;
1408                 freq_table[i].frequency = rk2818_freqs[i].arm_mhz * 1000; /* kHz */
1409         }
1410
1411         freq_table[i].index = i;
1412         freq_table[i].frequency = CPUFREQ_TABLE_END;
1413
1414         *table = freq_table;
1415 }
1416 EXPORT_SYMBOL(clk_init_cpufreq_table);
1417 #endif /* CONFIG_CPU_FREQ */
1418
1419 static unsigned int __initdata armclk;
1420
1421 /*
1422  * By default we use the rate set by the bootloader.
1423  * You can override this with armclk= cmdline option.
1424  */
1425 static int __init clk_setup(char *str)
1426 {
1427         get_option(&str, &armclk);
1428
1429         if (!armclk)
1430                 return 1;
1431
1432         if (armclk < 1000)
1433                 armclk *= 1000000;
1434
1435         return 1;
1436 }
1437 __setup("armclk=", clk_setup);
1438
1439 /*
1440  * Switch the arm_clk rate if specified on cmdline.
1441  * We cannot do this early until cmdline is parsed.
1442  */
1443 static int __init rk2818_clk_arch_init(void)
1444 {
1445         if (!armclk)
1446                 return -EINVAL;
1447
1448         if (clk_set_rate(&arm_clk, armclk))
1449                 printk(KERN_ERR "*** Unable to set arm_clk rate\n");
1450
1451         recalculate_root_clocks();
1452
1453         printk(KERN_INFO "Switched to new clocking rate (pll/arm/hclk/pclk): "
1454                "%ld/%ld/%ld/%ld MHz\n",
1455                arm_pll_clk.rate / 1000000, arm_clk.rate / 1000000,
1456                arm_hclk.rate / 1000000, arm_pclk.rate / 1000000);
1457
1458         calibrate_delay();
1459
1460         return 0;
1461 }
1462 core_initcall_sync(rk2818_clk_arch_init);
1463
1464 void __init rk2818_clock_init(void)
1465 {
1466         struct clk_lookup *lk;
1467
1468         for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++)
1469                 clk_preinit(lk->clk);
1470
1471         for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++) {
1472                 clkdev_add(lk);
1473                 clk_register(lk->clk);
1474         }
1475
1476         recalculate_root_clocks();
1477
1478         printk(KERN_INFO "Clocking rate (pll/arm/hclk/pclk): "
1479                "%ld/%ld/%ld/%ld MHz\n",
1480                arm_pll_clk.rate / 1000000, arm_clk.rate / 1000000,
1481                arm_hclk.rate / 1000000, arm_pclk.rate / 1000000);
1482
1483         /*
1484          * Only enable those clocks we will need, let the drivers
1485          * enable other clocks as necessary
1486          */
1487         clk_enable_init_clocks();
1488 }
1489
1490 #ifdef CONFIG_PROC_FS
1491 #include <linux/proc_fs.h>
1492 #include <linux/seq_file.h>
1493
1494 static void dump_clock(struct seq_file *s, struct clk *clk, int deep)
1495 {
1496         struct clk* ck;
1497         int i;
1498         unsigned long rate = clk->rate;
1499
1500         for (i = 0; i < deep; i++)
1501                 seq_printf(s, "    ");
1502
1503         seq_printf(s, "%-9s ", clk->name);
1504
1505         if (clk->gate_idx < CLK_GATE_MAX) {
1506                 u32 *reg;
1507                 int idx = clk->gate_idx;
1508                 u32 v;
1509
1510                 reg = &scu_register_base->scu_clkgate0_config;
1511                 reg += (idx >> 5);
1512                 idx &= 0x1F;
1513
1514                 v = readl(reg) & (1 << idx);
1515                 
1516                 seq_printf(s, "%s ", v ? "off" : "on ");
1517         } else
1518                 seq_printf(s, "%s ", clk->usecount ? "on " : "off");
1519
1520         if (rate >= 1000000) {
1521                 if (rate % 1000000)
1522                         seq_printf(s, "%ld.%06ld MHz", rate / 1000000, rate % 1000000);
1523                 else
1524                         seq_printf(s, "%ld MHz", rate / 1000000);
1525         } else if (rate >= 1000) {
1526                 if (rate % 1000)
1527                         seq_printf(s, "%ld.%03ld KHz", rate / 1000, rate % 1000);
1528                 else
1529                         seq_printf(s, "%ld KHz", rate / 1000);
1530         } else {
1531                 seq_printf(s, "%ld Hz", rate);
1532         }
1533
1534         seq_printf(s, " usecount = %d", clk->usecount);
1535
1536         seq_printf(s, " parent = %s\n", clk->parent ? clk->parent->name : "NULL");
1537
1538         list_for_each_entry(ck, &clocks, node) {
1539                 if (ck->parent == clk)
1540                         dump_clock(s, ck, deep + 1);
1541         }
1542 }
1543
1544 static int proc_clk_show(struct seq_file *s, void *v)
1545 {
1546         struct clk* clk;
1547
1548         mutex_lock(&clocks_mutex);
1549         list_for_each_entry(clk, &clocks, node) {
1550                 if (!clk->parent)
1551                         dump_clock(s, clk, 0);
1552         }
1553         mutex_unlock(&clocks_mutex);
1554
1555         seq_printf(s, "\nRegisters:\n");
1556         seq_printf(s, "SCU_APLL_CON     : 0x%08x\n", readl(&scu_register_base->scu_pll_config[0]));
1557         seq_printf(s, "SCU_DPLL_CON     : 0x%08x\n", readl(&scu_register_base->scu_pll_config[1]));
1558         seq_printf(s, "SCU_CPLL_CON     : 0x%08x\n", readl(&scu_register_base->scu_pll_config[2]));
1559         seq_printf(s, "SCU_MODE_CON     : 0x%08x\n", readl(&scu_register_base->scu_mode_config));
1560         seq_printf(s, "SCU_PMU_CON      : 0x%08x\n", readl(&scu_register_base->scu_pmu_config));
1561         seq_printf(s, "SCU_CLKSEL0_CON  : 0x%08x\n", readl(&scu_register_base->scu_clksel0_config));
1562         seq_printf(s, "SCU_CLKSEL1_CON  : 0x%08x\n", readl(&scu_register_base->scu_clksel1_config));
1563         seq_printf(s, "SCU_CLKGATE0_CON : 0x%08x\n", readl(&scu_register_base->scu_clkgate0_config));
1564         seq_printf(s, "SCU_CLKGATE1_CON : 0x%08x\n", readl(&scu_register_base->scu_clkgate1_config));
1565         seq_printf(s, "SCU_CLKGATE2_CON : 0x%08x\n", readl(&scu_register_base->scu_clkgate2_config));
1566         seq_printf(s, "SCU_CLKSEL2_CON  : 0x%08x\n", readl(&scu_register_base->scu_clksel2_config));
1567
1568         return 0;
1569 }
1570
1571 static int proc_clk_open(struct inode *inode, struct file *file)
1572 {
1573         return single_open(file, proc_clk_show, NULL);
1574 }
1575
1576 static const struct file_operations proc_clk_fops = {
1577         .open           = proc_clk_open,
1578         .read           = seq_read,
1579         .llseek         = seq_lseek,
1580         .release        = single_release,
1581 };
1582
1583 static int __init clk_proc_init(void)
1584 {
1585         proc_create("clocks", 0, NULL, &proc_clk_fops);
1586         return 0;
1587
1588 }
1589 late_initcall(clk_proc_init);
1590 #endif /* CONFIG_PROC_FS */
1591