1 /* arch/arm/mach-rk2818/clock.c
3 * Copyright (C) 2010 ROCKCHIP, Inc.
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.
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.
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>
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>
29 #include <mach/iomux.h> // CPU_APB_REG0
31 static struct rockchip_scu_reg_hw
33 u32 scu_pll_config[3]; /* 0:arm 1:dsp 2:codec */
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);
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)
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)
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)
67 #define SCU_DSPMODE_MASK 0x03u
68 #define SCU_DSPMODE_SLOW 0x00u
69 #define SCU_DSPMODE_NORMAL 0x01u
70 #define SCU_DSPMODE_DSLOW 0x02u
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)
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)
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)
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)
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)
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)
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)
120 #define CLK_CPLL_MASK 0x03u
121 #define CLK_CPLL_SLOW 0x00u
122 #define CLK_CPLL_NORMAL 0x01u
123 #define CLK_CPLL_DSLOW 0x02u
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 */
132 struct list_head node;
135 struct list_head children;
136 struct list_head sibling; /* node for children */
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 *);
148 u32 __iomem *clksel_reg;
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);
158 /* Used for clocks that always have same value as the parent clock */
159 static unsigned long followparent_recalc(struct clk *clk)
161 return clk->parent->rate;
164 static unsigned long clksel_recalc(struct clk *clk)
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);
172 static unsigned long clksel_recalc_shift(struct clk *clk)
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);
180 static int clksel_set_rate(struct clk *clk, unsigned long rate)
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;
189 v &= ~clk->clksel_mask;
190 v |= (div - 1) << clk->clksel_shift;
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);
201 static int clksel_set_rate_shift(struct clk *clk, unsigned long rate)
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;
210 v &= ~clk->clksel_mask;
211 v |= shift << clk->clksel_shift;
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);
222 static struct clk xin24m = {
229 static struct clk clk12m = {
237 static struct clk extclk = {
244 static unsigned long pll_clk_recalc(struct clk *clk);
245 static int pll_clk_set_rate(struct clk *clk, unsigned long rate);
247 #define PLL_CLK(NAME,IDX) \
248 static struct clk NAME##_pll_clk = { \
249 .name = #NAME"_pll", \
252 .recalc = pll_clk_recalc, \
253 .set_rate = pll_clk_set_rate, \
261 static unsigned long pll_clk_recalc(struct clk *clk)
263 u32 mask, normal, *reg;
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;
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);
290 rate = clk->parent->rate;
291 pr_debug("clock: %s new clock rate is %ld (slow mode)\n", clk->name, rate);
297 static void pll_clk_slow_mode(struct clk *clk, int enter)
299 u32 mask, value, *reg;
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;
318 writel((readl(reg) & ~mask) | value, reg);
321 static int pll_clk_set_rate(struct clk *clk, unsigned long rate)
323 u32 *reg = &scu_register_base->scu_pll_config[clk->pll_idx];
326 if (rate <= 24000000 ) {
327 pll_clk_slow_mode(clk, 1);
328 v |= PLL_PD; /* pll power down */
330 pr_debug("clock: %s power down", clk->name);
332 int clkf, clkod, unit;
334 unit = rate / 1000000;
337 while (clkf < 200) { /* 160 <= Fref/NR * NF <= 800 , 200 set to the midia point */
341 if (clkod > 8) { /* clkod max 8 , 8*24=192 > 160 , safe */
345 pll_clk_slow_mode(clk, 1);
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));
353 /* arm run at 24m */ //FIXME
354 unit = 7200; /* 24m,0.3ms , 24*300*/
356 v = readl(RK2818_REGFILE_BASE + CPU_APB_REG0);
357 if (v & (0x80u << clk->pll_idx) )
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);
367 static int arm_clk_set_rate(struct clk *clk, unsigned long rate);
369 static struct clk arm_clk = {
371 .parent = &arm_pll_clk,
372 .recalc = clksel_recalc,
373 #ifdef CONFIG_CPU_FREQ
374 .set_rate = arm_clk_set_rate,
377 .clksel_reg = CLKSEL2_REG,
382 static struct clk arm_hclk = {
385 .recalc = clksel_recalc,
387 .clksel_reg = CLKSEL0_REG,
392 static struct clk clk48m = {
396 .recalc = clksel_recalc,
398 .clksel_reg = CLKSEL2_REG,
399 .clksel_mask = 0xF << 4,
403 static struct clk arm_pclk = {
406 .flags = ENABLE_ON_INIT,
407 .recalc = clksel_recalc_shift,
408 .set_rate = clksel_set_rate_shift,
410 .clksel_reg = CLKSEL0_REG,
411 .clksel_mask = 0x3 << 2,
416 static struct clk* demod_divider_clk_get_parent(struct clk *clk)
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;
422 static int demod_divider_clk_set_parent(struct clk *clk, struct clk *parent)
424 u32 r = readl(CLK_DEMOD_REG) & ~CLK_DEMOD_DIV_SRC_MASK;
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;
435 writel(r, CLK_DEMOD_REG);
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,
448 .clksel_reg = CLKSEL1_REG,
449 .clksel_mask = 0xFF << 16,
451 .clksel_maxdiv = 128,
454 static struct clk* demod_clk_get_parent(struct clk *clk)
456 u32 r = readl(CLK_DEMOD_REG) & CLK_DEMOD_SRC_MASK;
457 return (r == CLK_DEMOD_DIVOUT) ? &demod_divider_clk : &extclk;
460 static int demod_clk_set_parent(struct clk *clk, struct clk *parent)
462 u32 r = readl(CLK_DEMOD_REG) & ~CLK_DEMOD_SRC_MASK;
464 if (parent == &extclk) {
466 } else if (parent == &demod_divider_clk) {
467 r |= CLK_DEMOD_DIVOUT;
471 writel(r, CLK_DEMOD_REG);
476 static struct clk demod_clk = {
478 .parent = &demod_divider_clk,
479 .recalc = followparent_recalc,
480 .get_parent = demod_clk_get_parent,
481 .set_parent = demod_clk_set_parent,
485 static struct clk codec_clk = {
487 .parent = &codec_pll_clk,
488 .recalc = clksel_recalc,
489 .set_rate = clksel_set_rate,
491 .clksel_reg = CLKSEL1_REG,
492 .clksel_mask = 0x1F << 3,
497 static struct clk* lcdc_divider_clk_get_parent(struct clk *clk)
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;
503 static int lcdc_divider_clk_set_parent(struct clk *clk, struct clk *parent)
505 u32 r = readl(CLK_LCDC_REG) & ~CLK_LCDC_DIV_SRC_MASK;
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;
516 writel(r, CLK_LCDC_REG);
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,
529 .clksel_reg = CLKSEL0_REG,
530 .clksel_mask = 0xFF << 8,
532 .clksel_maxdiv = 128,
535 static struct clk* otgphy_clk_get_parent(struct clk *clk)
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;
541 static int otgphy_clk_set_parent(struct clk *clk, struct clk *parent)
543 u32 r = readl(CLK_USBPHY_REG) & ~CLK_USBPHY_SRC_MASK;
545 if (parent == &xin24m) {
547 } else if (parent == &clk12m) {
549 } else if (parent == &clk48m) {
554 writel(r, CLK_USBPHY_REG);
559 static struct clk* lcdc_clk_get_parent(struct clk *clk)
561 u32 r = readl(CLK_LCDC_REG) & CLK_LCDC_SRC_MASK;
562 return (r == CLK_LCDC_DIVOUT) ? &lcdc_divider_clk : &extclk;
565 static int lcdc_clk_set_parent(struct clk *clk, struct clk *parent)
567 u32 r = readl(CLK_LCDC_REG) & ~CLK_LCDC_SRC_MASK;
569 if (parent == &lcdc_divider_clk) {
570 r |= CLK_LCDC_DIVOUT;
571 } else if (parent == &extclk) {
576 writel(r, CLK_LCDC_REG);
581 static struct clk* vip_clk_get_parent(struct clk *clk)
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;
587 static int vip_clk_set_parent(struct clk *clk, struct clk *parent)
589 u32 r = readl(CLK_SENSOR_REG) & ~CLK_SENSOR_SRC_MASK;
591 if (parent == &xin24m) {
593 } else if (parent == &extclk) {
595 } else if (parent == &clk48m) {
600 writel(r, CLK_SENSOR_REG);
605 static struct clk* ddr_clk_get_parent(struct clk *clk)
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;
611 static int ddr_clk_set_parent(struct clk *clk, struct clk *parent)
613 u32 r = readl(CLK_DDR_REG) & ~CLK_DDR_SRC_MASK;
615 if (parent == &codec_pll_clk) {
617 } else if (parent == &arm_pll_clk) {
619 } else if (parent == &dsp_pll_clk) {
624 writel(r, CLK_DDR_REG);
629 static struct clk* i2s_clk_get_parent(struct clk *clk)
631 u32 r = readl(CLK_CODEC_REG) & CLK_CODEC_SRC_MASK;
632 return (r == CLK_CODEC_CPLLCLK) ? &codec_clk : &clk12m;
635 static int i2s_clk_set_parent(struct clk *clk, struct clk *parent)
637 u32 r = readl(CLK_CODEC_REG) & ~CLK_CODEC_SRC_MASK;
639 if (parent == &codec_clk) {
640 r |= CLK_CODEC_CPLLCLK;
641 } else if (parent == &clk12m) {
646 writel(r, CLK_CODEC_REG);
651 static int gate_mode(struct clk *clk, int on)
654 int idx = clk->gate_idx;
657 if (idx >= CLK_GATE_MAX)
660 reg = &scu_register_base->scu_clkgate0_config;
666 v &= ~(1 << idx); // clear bit
668 v |= (1 << idx); // set bit
675 static struct clk* uart_clk_get_parent(struct clk *clk)
677 u32 r = readl(CLK_UART_REG) & CLK_UART_SRC_MASK;
678 return (r == CLK_UART_24M) ? &xin24m : &clk48m;
681 static int uart_clk_set_parent(struct clk *clk, struct clk *parent)
683 u32 r = readl(CLK_UART_REG) & ~CLK_UART_SRC_MASK;
685 if (parent == &xin24m) {
687 } else if (parent == &clk48m) {
692 writel(r, CLK_UART_REG);
697 #define UART_CLK(n) \
698 static struct clk uart##n##_clk = { \
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, \
708 #define GATE_CLK(NAME,PARENT,ID) \
709 static struct clk NAME##_clk = { \
713 .recalc = followparent_recalc, \
714 .gate_idx = CLK_GATE_##ID, \
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 = {
728 .recalc = followparent_recalc,
729 .get_parent = otgphy_clk_get_parent,
730 .set_parent = otgphy_clk_set_parent,
731 .gate_idx = CLK_GATE_OTGPHY,
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 = {
738 .parent = &lcdc_divider_clk,
740 .recalc = followparent_recalc,
741 .get_parent = lcdc_clk_get_parent,
742 .set_parent = lcdc_clk_set_parent,
743 .gate_idx = CLK_GATE_LCDC,
745 static struct clk vip_clk = {
749 .recalc = followparent_recalc,
750 .get_parent = vip_clk_get_parent,
751 .set_parent = vip_clk_set_parent,
752 .gate_idx = CLK_GATE_VIP,
754 static struct clk i2s_clk = {
758 .recalc = followparent_recalc,
759 .get_parent = i2s_clk_get_parent,
760 .set_parent = i2s_clk_set_parent,
761 .gate_idx = CLK_GATE_I2S,
763 static struct clk sdmmc0_clk = {
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,
776 GATE_CLK(ebrom, arm_hclk, EBROM);
777 GATE_CLK(gpio0, arm_pclk, GPIO0);
778 GATE_CLK(gpio1, arm_pclk, GPIO1);
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 = {
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,
799 .clksel_maxdiv = 128,
803 static struct clk sdmmc1_clk = {
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,
816 static unsigned long hsadc_clk_recalc(struct clk *clk)
818 return clk->parent->rate >> 1;
821 static struct clk hsadc_clk = {
823 .parent = &demod_clk,
825 .recalc = hsadc_clk_recalc,
826 .gate_idx = CLK_GATE_HSADC,
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 = {
838 .parent = &codec_pll_clk,
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,
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);
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);
870 #define CLK(dev, con, ck) \
881 .clk = &name##_clk, \
884 static struct clk_lookup clks[] = {
885 CLK(NULL, "xin24m", &xin24m),
886 CLK(NULL, "extclk", &extclk),
888 CLK(NULL, "clk12m", &clk12m),
893 CLK(NULL, "arm_hclk", &arm_hclk),
894 CLK(NULL, "clk48m", &clk48m),
895 CLK(NULL, "arm_pclk", &arm_pclk),
915 CLK("rk2818_sdmmc.0", "sdmmc", &sdmmc0_clk),
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),
930 CLK("rk2818_serial.2", "uart", &uart2_clk),
931 CLK("rk2818_serial.3", "uart", &uart3_clk),
932 CLK("rk2818_sdmmc.1", "sdmmc", &sdmmc1_clk),
936 CLK1(sdram_controller),
937 CLK1(mobile_sdram_controller),
938 CLK1(lcdc_share_memory),
940 CLK1(deblocking_h264),
944 CLK1(customized_sdram_controller),
965 static LIST_HEAD(clocks);
966 static DEFINE_MUTEX(clocks_mutex);
967 static DEFINE_SPINLOCK(clockfw_lock);
969 static int __clk_enable(struct clk *clk)
973 if (clk->usecount == 0) {
975 ret = __clk_enable(clk->parent);
981 ret = clk->mode(clk, 1);
984 __clk_disable(clk->parent);
988 pr_debug("clock: %s enabled\n", clk->name);
995 int clk_enable(struct clk *clk)
1000 if (clk == NULL || IS_ERR(clk))
1003 spin_lock_irqsave(&clockfw_lock, flags);
1004 ret = __clk_enable(clk);
1005 spin_unlock_irqrestore(&clockfw_lock, flags);
1009 EXPORT_SYMBOL(clk_enable);
1011 static void __clk_disable(struct clk *clk)
1013 if (--clk->usecount == 0) {
1016 pr_debug("clock: %s disabled\n", clk->name);
1019 __clk_disable(clk->parent);
1022 void clk_disable(struct clk *clk)
1024 unsigned long flags;
1026 if (clk == NULL || IS_ERR(clk))
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);
1039 spin_unlock_irqrestore(&clockfw_lock, flags);
1041 EXPORT_SYMBOL(clk_disable);
1043 unsigned long clk_get_rate(struct clk *clk)
1045 unsigned long flags;
1048 if (clk == NULL || IS_ERR(clk))
1051 spin_lock_irqsave(&clockfw_lock, flags);
1053 spin_unlock_irqrestore(&clockfw_lock, flags);
1057 EXPORT_SYMBOL(clk_get_rate);
1059 /*-------------------------------------------------------------------------
1060 * Optional clock functions defined in include/linux/clk.h
1061 *-------------------------------------------------------------------------*/
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)
1066 if (clk->round_rate)
1067 return clk->round_rate(clk, rate);
1069 if (clk->flags & RATE_FIXED)
1070 printk(KERN_ERR "clock: clk_round_rate called on fixed-rate clock %s\n", clk->name);
1075 long clk_round_rate(struct clk *clk, unsigned long rate)
1077 unsigned long flags;
1080 if (clk == NULL || IS_ERR(clk))
1083 spin_lock_irqsave(&clockfw_lock, flags);
1084 ret = __clk_round_rate(clk, rate);
1085 spin_unlock_irqrestore(&clockfw_lock, flags);
1089 EXPORT_SYMBOL(clk_round_rate);
1091 /* Set the clock rate for a clock source */
1092 static int __clk_set_rate(struct clk *clk, unsigned long rate)
1096 pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
1098 if (clk->flags & CONFIG_PARTICIPANT)
1102 ret = clk->set_rate(clk, rate);
1107 int clk_set_rate(struct clk *clk, unsigned long rate)
1109 unsigned long flags;
1112 if (clk == NULL || IS_ERR(clk))
1115 spin_lock_irqsave(&clockfw_lock, flags);
1116 ret = __clk_set_rate(clk, rate);
1119 clk->rate = clk->recalc(clk);
1120 propagate_rate(clk);
1122 spin_unlock_irqrestore(&clockfw_lock, flags);
1126 EXPORT_SYMBOL(clk_set_rate);
1128 int clk_set_parent(struct clk *clk, struct clk *parent)
1130 unsigned long flags;
1133 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
1136 if (clk->set_parent == NULL)
1139 spin_lock_irqsave(&clockfw_lock, flags);
1140 if (clk->usecount == 0) {
1141 ret = clk->set_parent(clk, parent);
1143 clk_reparent(clk, parent);
1145 clk->rate = clk->recalc(clk);
1146 propagate_rate(clk);
1150 spin_unlock_irqrestore(&clockfw_lock, flags);
1154 EXPORT_SYMBOL(clk_set_parent);
1156 struct clk *clk_get_parent(struct clk *clk)
1160 EXPORT_SYMBOL(clk_get_parent);
1162 static void clk_reparent(struct clk *child, struct clk *parent)
1164 if (child->parent == parent)
1166 pr_debug("clock: %s reparent to %s (was %s)\n", child->name, parent->name, ((child->parent) ? child->parent->name : "NULL"));
1168 list_del_init(&child->sibling);
1170 list_add(&child->sibling, &parent->children);
1171 child->parent = parent;
1174 /* Propagate rate to children */
1175 static void propagate_rate(struct clk *tclk)
1179 list_for_each_entry(clkp, &tclk->children, sibling) {
1181 clkp->rate = clkp->recalc(clkp);
1182 propagate_rate(clkp);
1186 static LIST_HEAD(root_clks);
1189 * recalculate_root_clocks - recalculate and propagate all root clocks
1191 * Recalculates all root clocks (clocks with no parent), which if the
1192 * clock's .recalc is set correctly, should also propagate their rates.
1195 static void recalculate_root_clocks(void)
1199 list_for_each_entry(clkp, &root_clks, sibling) {
1201 clkp->rate = clkp->recalc(clkp);
1202 propagate_rate(clkp);
1206 void clk_recalculate_root_clocks(void)
1208 unsigned long flags;
1210 spin_lock_irqsave(&clockfw_lock, flags);
1211 recalculate_root_clocks();
1212 spin_unlock_irqrestore(&clockfw_lock, flags);
1216 * clk_preinit - initialize any fields in the struct clk before clk init
1217 * @clk: struct clk * to initialize
1219 * Initialize any struct clk fields needed before normal clk initialization
1220 * can run. No return value.
1222 static void clk_preinit(struct clk *clk)
1224 INIT_LIST_HEAD(&clk->children);
1227 static int clk_register(struct clk *clk)
1229 if (clk == NULL || IS_ERR(clk))
1233 * trap out already registered clocks
1235 if (clk->node.next || clk->node.prev)
1238 mutex_lock(&clocks_mutex);
1240 if (clk->get_parent)
1241 clk->parent = clk->get_parent(clk);
1244 list_add(&clk->sibling, &clk->parent->children);
1246 list_add(&clk->sibling, &root_clks);
1248 list_add(&clk->node, &clocks);
1250 mutex_unlock(&clocks_mutex);
1255 static void clk_enable_init_clocks(void)
1259 list_for_each_entry(clkp, &clocks, node) {
1260 if (clkp->flags & ENABLE_ON_INIT)
1265 #ifdef CONFIG_CPU_FREQ
1266 #include <linux/cpufreq.h>
1268 struct rk2818_freq_info {
1277 #define CLK_HCLK_PCLK_11 0
1278 #define CLK_HCLK_PCLK_21 1
1279 #define CLK_HCLK_PCLK_41 2
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
1286 #define OP(_arm_mhz, _ddr_mhz, nf, nr, od, _clk48m, _hclk, _pclk) \
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, \
1297 * F = 24MHz * NF / NR / OD
1298 * 97.7KHz < 24MHz / NR < 800MHz
1299 * 160MHz < 24MHz / NR * NF < 800MHz
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),
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)
1325 static int arm_clk_set_rate(struct clk *clk, unsigned long rate)
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];
1335 for (i = 0; i < ARRAY_SIZE(rk2818_freqs); i++) {
1336 if (rk2818_freqs[i].arm_mhz == arm_mhz) {
1341 if (i == ARRAY_SIZE(rk2818_freqs))
1344 freq = &rk2818_freqs[i];
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");
1352 pll_clk_slow_mode(&arm_pll_clk, 1);
1356 /* XXX:delay for pll state , for 0.3ms , clkf will lock clkf*/
1357 writel(freq->apll_con, reg);
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);
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);
1367 v = readl(clk48m.clksel_reg) & ~clk48m.clksel_mask;
1368 v |= freq->clk48m << clk48m.clksel_shift;
1369 writel(v, clk48m.clksel_reg);
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);
1378 pll_clk_slow_mode(&arm_pll_clk, 0);
1380 pr_debug("clock: set %s to %ld MHz, delay count=%d\n", clk->name, rate/1000000, unit);
1382 arm_pll_clk.rate = arm_pll_clk.recalc(&arm_pll_clk);
1386 list_for_each_entry(clkp, &arm_pll_clk.children, sibling) {
1387 if (clkp == &arm_clk)
1390 clkp->rate = clkp->recalc(clkp);
1391 propagate_rate(clkp);
1395 //FIXME: change sdram freq
1400 static struct cpufreq_frequency_table freq_table[ARRAY_SIZE(rk2818_freqs) + 1];
1402 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
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 */
1411 freq_table[i].index = i;
1412 freq_table[i].frequency = CPUFREQ_TABLE_END;
1414 *table = freq_table;
1416 EXPORT_SYMBOL(clk_init_cpufreq_table);
1417 #endif /* CONFIG_CPU_FREQ */
1419 static unsigned int __initdata armclk;
1422 * By default we use the rate set by the bootloader.
1423 * You can override this with armclk= cmdline option.
1425 static int __init clk_setup(char *str)
1427 get_option(&str, &armclk);
1437 __setup("armclk=", clk_setup);
1440 * Switch the arm_clk rate if specified on cmdline.
1441 * We cannot do this early until cmdline is parsed.
1443 static int __init rk2818_clk_arch_init(void)
1448 if (clk_set_rate(&arm_clk, armclk))
1449 printk(KERN_ERR "*** Unable to set arm_clk rate\n");
1451 recalculate_root_clocks();
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);
1462 core_initcall_sync(rk2818_clk_arch_init);
1464 void __init rk2818_clock_init(void)
1466 struct clk_lookup *lk;
1468 for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++)
1469 clk_preinit(lk->clk);
1471 for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++) {
1473 clk_register(lk->clk);
1476 recalculate_root_clocks();
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);
1484 * Only enable those clocks we will need, let the drivers
1485 * enable other clocks as necessary
1487 clk_enable_init_clocks();
1490 #ifdef CONFIG_PROC_FS
1491 #include <linux/proc_fs.h>
1492 #include <linux/seq_file.h>
1494 static void dump_clock(struct seq_file *s, struct clk *clk, int deep)
1498 unsigned long rate = clk->rate;
1500 for (i = 0; i < deep; i++)
1503 seq_printf(s, "%-9s ", clk->name);
1505 if (clk->gate_idx < CLK_GATE_MAX) {
1507 int idx = clk->gate_idx;
1510 reg = &scu_register_base->scu_clkgate0_config;
1514 v = readl(reg) & (1 << idx);
1516 seq_printf(s, "%s ", v ? "off" : "on ");
1518 seq_printf(s, "%s ", clk->usecount ? "on " : "off");
1520 if (rate >= 1000000) {
1522 seq_printf(s, "%ld.%06ld MHz", rate / 1000000, rate % 1000000);
1524 seq_printf(s, "%ld MHz", rate / 1000000);
1525 } else if (rate >= 1000) {
1527 seq_printf(s, "%ld.%03ld KHz", rate / 1000, rate % 1000);
1529 seq_printf(s, "%ld KHz", rate / 1000);
1531 seq_printf(s, "%ld Hz", rate);
1534 seq_printf(s, " usecount = %d", clk->usecount);
1536 seq_printf(s, " parent = %s\n", clk->parent ? clk->parent->name : "NULL");
1538 list_for_each_entry(ck, &clocks, node) {
1539 if (ck->parent == clk)
1540 dump_clock(s, ck, deep + 1);
1544 static int proc_clk_show(struct seq_file *s, void *v)
1548 mutex_lock(&clocks_mutex);
1549 list_for_each_entry(clk, &clocks, node) {
1551 dump_clock(s, clk, 0);
1553 mutex_unlock(&clocks_mutex);
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));
1571 static int proc_clk_open(struct inode *inode, struct file *file)
1573 return single_open(file, proc_clk_show, NULL);
1576 static const struct file_operations proc_clk_fops = {
1577 .open = proc_clk_open,
1579 .llseek = seq_lseek,
1580 .release = single_release,
1583 static int __init clk_proc_init(void)
1585 proc_create("clocks", 0, NULL, &proc_clk_fops);
1589 late_initcall(clk_proc_init);
1590 #endif /* CONFIG_PROC_FS */