1 /* arch/arm/mach-rk29/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.
17 #define pr_fmt(fmt) "clock: %s: " fmt, __func__
19 #include <linux/clk.h>
20 #include <linux/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/version.h>
30 #include <asm/clkdev.h>
31 #include <mach/rk29_iomap.h>
34 #include <mach/sram.h>
35 #include <mach/board.h>
40 #define RATE_FIXED (1 << 1) /* Fixed clock rate */
41 #define CONFIG_PARTICIPANT (1 << 10) /* Fundamental clock */
42 #define IS_PD (1 << 2) /* Power Domain */
44 #define regfile_readl(offset) readl(RK29_GRF_BASE + offset)
45 #define pmu_readl(offset) readl(RK29_PMU_BASE + offset)
47 #define MHZ (1000*1000)
51 struct list_head node;
54 struct list_head children;
55 struct list_head sibling; /* node for children */
58 int (*mode)(struct clk *clk, int on);
59 unsigned long (*recalc)(struct clk *); /* if null, follow parent */
60 int (*set_rate)(struct clk *, unsigned long);
61 long (*round_rate)(struct clk *, unsigned long);
62 struct clk* (*get_parent)(struct clk *); /* get clk's parent from the hardware. default is clksel_get_parent if parents present */
63 int (*set_parent)(struct clk *, struct clk *); /* default is clksel_set_parent if parents present */
71 u8 clksel_parent_mask;
72 u8 clksel_parent_shift;
76 static int clk_enable_nolock(struct clk *clk);
77 static void clk_disable_nolock(struct clk *clk);
78 static int clk_set_rate_nolock(struct clk *clk, unsigned long rate);
79 static int clk_set_parent_nolock(struct clk *clk, struct clk *parent);
80 static void __clk_reparent(struct clk *child, struct clk *parent);
81 static void __propagate_rate(struct clk *tclk);
82 static struct clk codec_pll_clk;
83 static struct clk general_pll_clk;
85 static unsigned long clksel_recalc_div(struct clk *clk)
87 u32 div = ((cru_readl(clk->clksel_con) >> clk->clksel_shift) & clk->clksel_mask) + 1;
88 unsigned long rate = clk->parent->rate / div;
89 pr_debug("%s new clock rate is %lu (div %u)\n", clk->name, rate, div);
93 static unsigned long clksel_recalc_shift(struct clk *clk)
95 u32 shift = (cru_readl(clk->clksel_con) >> clk->clksel_shift) & clk->clksel_mask;
96 unsigned long rate = clk->parent->rate >> shift;
97 pr_debug("%s new clock rate is %lu (shift %u)\n", clk->name, rate, shift);
101 static unsigned long clksel_recalc_frac(struct clk *clk)
105 u32 r = cru_readl(clk->clksel_con), numerator, denominator;
106 if (r == 0) // FPGA ?
107 return clk->parent->rate;
109 denominator = r & 0xFFFF;
110 rate64 = (u64)clk->parent->rate * numerator;
111 do_div(rate64, denominator);
113 pr_debug("%s new clock rate is %lu (frac %u/%u)\n", clk->name, rate, numerator, denominator);
117 static int clksel_set_rate_div(struct clk *clk, unsigned long rate)
121 for (div = 0; div <= clk->clksel_mask; div++) {
122 u32 new_rate = clk->parent->rate / (div + 1);
123 if (new_rate <= rate) {
124 u32 v = cru_readl(clk->clksel_con);
125 v &= ~((u32) clk->clksel_mask << clk->clksel_shift);
126 v |= div << clk->clksel_shift;
127 cru_writel(v, clk->clksel_con);
128 clk->rate = new_rate;
129 pr_debug("clksel_set_rate_div for clock %s to rate %ld (div %d)\n", clk->name, rate, div + 1);
137 static int clksel_set_rate_shift(struct clk *clk, unsigned long rate)
141 for (shift = 0; (1 << shift) <= clk->clksel_maxdiv; shift++) {
142 u32 new_rate = clk->parent->rate >> shift;
143 if (new_rate <= rate) {
144 u32 v = cru_readl(clk->clksel_con);
145 v &= ~((u32) clk->clksel_mask << clk->clksel_shift);
146 v |= shift << clk->clksel_shift;
147 cru_writel(v, clk->clksel_con);
148 clk->rate = new_rate;
149 pr_debug("clksel_set_rate_shift for clock %s to rate %ld (shift %d)\n", clk->name, rate, shift);
157 static struct clk* clksel_get_parent(struct clk *clk)
159 return clk->parents[(cru_readl(clk->clksel_con) >> clk->clksel_parent_shift) & clk->clksel_parent_mask];
162 static int clksel_set_parent(struct clk *clk, struct clk *parent)
164 struct clk **p = clk->parents;
169 for (i = 0; (i <= clk->clksel_parent_mask) && *p; i++, p++) {
173 v = cru_readl(clk->clksel_con);
174 v &= ~((u32) clk->clksel_parent_mask << clk->clksel_parent_shift);
175 v |= (i << clk->clksel_parent_shift);
176 cru_writel(v, clk->clksel_con);
182 /* Work around CRU_CLKGATE3_CON bit21~20 bug */
183 volatile u32 cru_clkgate3_con_mirror;
185 static int gate_mode(struct clk *clk, int on)
188 int idx = clk->gate_idx;
191 if (idx >= CLK_GATE_MAX)
194 reg = CRU_CLKGATE0_CON;
195 reg += (idx >> 5) << 2;
198 if (reg == CRU_CLKGATE3_CON)
199 v = cru_clkgate3_con_mirror;
204 v &= ~(1 << idx); // clear bit
206 v |= (1 << idx); // set bit
208 if (reg == CRU_CLKGATE3_CON)
209 cru_clkgate3_con_mirror = v;
215 /* Work around CRU_SOFTRST0_CON bit29~27 bug */
216 static volatile u32 cru_softrst0_con_mirror;
218 void cru_set_soft_reset(enum cru_soft_reset idx, bool on)
221 u32 reg = CRU_SOFTRST0_CON + ((idx >> 5) << 2);
222 u32 mask = 1 << (idx & 31);
225 if (idx >= SOFT_RST_MAX)
228 local_irq_save(flags);
230 if (reg == CRU_SOFTRST0_CON)
231 v = cru_softrst0_con_mirror;
240 if (reg == CRU_SOFTRST0_CON)
241 cru_softrst0_con_mirror = v;
244 local_irq_restore(flags);
247 static struct clk xin24m = {
253 static struct clk clk_12m = {
260 static struct clk xin27m = {
266 static struct clk otgphy0_clkin = {
267 .name = "otgphy0_clkin",
272 static struct clk otgphy1_clkin = {
273 .name = "otgphy1_clkin",
279 static void delay_500ns(void)
286 static void delay_300us(void)
289 for (i = 0; i < 600; i++)
293 #define GENERAL_PLL_IDX 0
294 #define CODEC_PLL_IDX 1
295 #define ARM_PLL_IDX 2
296 #define DDR_PLL_IDX 3
298 #define GRF_SOC_CON0 0xbc
299 static void pll_wait_lock(int pll_idx)
301 u32 bit = 0x2000000u << pll_idx;
304 if (regfile_readl(GRF_SOC_CON0) & bit)
309 pr_warning("wait pll bit 0x%x time out!\n", bit);
313 static unsigned long arm_pll_clk_recalc(struct clk *clk)
317 if ((cru_readl(CRU_MODE_CON) & CRU_CPU_MODE_MASK) == CRU_CPU_MODE_NORMAL) {
318 u32 v = cru_readl(CRU_APLL_CON);
319 u64 rate64 = (u64) clk->parent->rate * PLL_NF2(v);
320 do_div(rate64, PLL_NR(v));
321 rate = rate64 >> PLL_NO_SHIFT(v);
322 pr_debug("%s new clock rate is %ld (NF %d NR %d NO %d)\n", clk->name, rate, PLL_NF2(v), PLL_NR(v), 1 << PLL_NO_SHIFT(v));
324 rate = clk->parent->rate;
325 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
337 #define CORE_ACLK_11 (0 << 5)
338 #define CORE_ACLK_21 (1 << 5)
339 #define CORE_ACLK_31 (2 << 5)
340 #define CORE_ACLK_41 (3 << 5)
341 #define CORE_ACLK_81 (4 << 5)
342 #define CORE_ACLK_MASK (7 << 5)
344 #define ACLK_HCLK_11 (0 << 8)
345 #define ACLK_HCLK_21 (1 << 8)
346 #define ACLK_HCLK_41 (2 << 8)
347 #define ACLK_HCLK_MASK (3 << 8)
349 #define ACLK_PCLK_11 (0 << 10)
350 #define ACLK_PCLK_21 (1 << 10)
351 #define ACLK_PCLK_41 (2 << 10)
352 #define ACLK_PCLK_81 (3 << 10)
353 #define ACLK_PCLK_MASK (3 << 10)
355 #define ARM_PLL(_mhz, nr, nf, no, _axi_div, _ahb_div, _apb_div) \
357 .rate = _mhz * MHZ, \
358 .apll_con = PLL_CLKR(nr) | PLL_CLKF(nf >> 1) | PLL_NO_##no, \
359 .clksel0_con = CORE_ACLK_##_axi_div | ACLK_HCLK_##_ahb_div | ACLK_PCLK_##_apb_div, \
362 static const struct arm_pll_set arm_pll[] = {
363 // rate = 24 * NF / (NR * NO)
364 // rate NR NF NO adiv hdiv pdiv
365 ARM_PLL(1200, 1, 50, 1, 41, 21, 81),
366 ARM_PLL(1104, 1, 46, 1, 41, 21, 81),
367 ARM_PLL(1008, 1, 42, 1, 41, 21, 81),
368 ARM_PLL( 912, 1, 38, 1, 31, 21, 81),
369 ARM_PLL( 888, 2, 74, 1, 31, 21, 81),
370 ARM_PLL( 816, 1, 34, 1, 31, 21, 81),
371 ARM_PLL( 696, 1, 58, 2, 31, 21, 81),
372 ARM_PLL( 624, 1, 52, 2, 31, 21, 81),
373 ARM_PLL( 600, 1, 50, 2, 21, 21, 81),
374 ARM_PLL( 504, 1, 42, 2, 21, 21, 81),
375 ARM_PLL( 408, 1, 34, 2, 21, 21, 81),
376 ARM_PLL( 300, 1, 50, 4, 21, 21, 41),
377 ARM_PLL( 204, 1, 34, 4, 21, 21, 41),
378 ARM_PLL( 102, 1, 34, 8, 21, 21, 41),
379 // last item, pll power down.
380 ARM_PLL( 24, 1, 64, 8, 21, 21, 41),
383 #define CORE_PARENT_MASK (3 << 23)
384 #define CORE_PARENT_ARM_PLL (0 << 23)
385 #define CORE_PARENT_GENERAL_PLL (1 << 23)
387 static int arm_pll_clk_set_rate(struct clk *clk, unsigned long rate)
389 const struct arm_pll_set *ps, *pt;
391 /* find the arm_pll we want. */
392 ps = pt = &arm_pll[0];
394 if (pt->rate == rate) {
398 // we are sorted, and ps->rate > pt->rate.
399 if ((pt->rate > rate || (rate - pt->rate < ps->rate - rate)))
401 if (pt->rate < rate || pt->rate == 24 * MHZ)
406 /* make aclk safe & reparent to general pll */
407 cru_writel((cru_readl(CRU_CLKSEL0_CON) & ~(CORE_PARENT_MASK | CORE_ACLK_MASK)) | CORE_PARENT_GENERAL_PLL | CORE_ACLK_21, CRU_CLKSEL0_CON);
409 /* enter slow mode */
410 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CPU_MODE_MASK) | CRU_CPU_MODE_SLOW, CRU_MODE_CON);
413 cru_writel(cru_readl(CRU_APLL_CON) | PLL_PD, CRU_APLL_CON);
417 cru_writel(ps->apll_con | PLL_PD, CRU_APLL_CON);
422 cru_writel(ps->apll_con, CRU_APLL_CON);
425 pll_wait_lock(ARM_PLL_IDX);
427 /* reparent to arm pll & set aclk/hclk/pclk */
428 cru_writel((cru_readl(CRU_CLKSEL0_CON) & ~(CORE_PARENT_MASK | CORE_ACLK_MASK | ACLK_HCLK_MASK | ACLK_PCLK_MASK)) | CORE_PARENT_ARM_PLL | ps->clksel0_con, CRU_CLKSEL0_CON);
430 /* enter normal mode */
431 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CPU_MODE_MASK) | CRU_CPU_MODE_NORMAL, CRU_MODE_CON);
436 static struct clk *arm_pll_parents[2] = { &xin24m, &xin27m };
438 static struct clk arm_pll_clk = {
441 .recalc = arm_pll_clk_recalc,
442 .set_rate = arm_pll_clk_set_rate,
443 .clksel_con = CRU_MODE_CON,
444 .clksel_parent_mask = 1,
445 .clksel_parent_shift = 8,
446 .parents = arm_pll_parents,
449 static unsigned long ddr_pll_clk_recalc(struct clk *clk)
453 if ((cru_readl(CRU_MODE_CON) & CRU_DDR_MODE_MASK) == CRU_DDR_MODE_NORMAL) {
454 u32 v = cru_readl(CRU_DPLL_CON);
455 u64 rate64 = (u64) clk->parent->rate * PLL_NF(v);
456 do_div(rate64, PLL_NR(v));
457 rate = rate64 >> PLL_NO_SHIFT(v);
458 pr_debug("%s new clock rate is %ld (NF %d NR %d NO %d)\n", clk->name, rate, PLL_NF(v), PLL_NR(v), 1 << PLL_NO_SHIFT(v));
460 rate = clk->parent->rate;
461 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
467 static int ddr_pll_clk_set_rate(struct clk *clk, unsigned long rate)
469 /* do nothing here */
473 static struct clk *ddr_pll_parents[4] = { &xin24m, &xin27m, &codec_pll_clk, &general_pll_clk };
475 static struct clk ddr_pll_clk = {
478 .recalc = ddr_pll_clk_recalc,
479 .set_rate = ddr_pll_clk_set_rate,
480 .clksel_con = CRU_MODE_CON,
481 .clksel_parent_mask = 3,
482 .clksel_parent_shift = 13,
483 .parents = ddr_pll_parents,
487 static int codec_pll_clk_mode(struct clk *clk, int on)
489 u32 cpll = cru_readl(CRU_CPLL_CON);
491 cru_writel(cpll & ~(PLL_PD | PLL_BYPASS), CRU_CPLL_CON);
493 pll_wait_lock(CODEC_PLL_IDX);
494 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CODEC_MODE_MASK) | CRU_CODEC_MODE_NORMAL, CRU_MODE_CON);
496 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CODEC_MODE_MASK) | CRU_CODEC_MODE_SLOW, CRU_MODE_CON);
497 cru_writel(cpll | PLL_BYPASS, CRU_CPLL_CON);
498 cru_writel(cpll | PLL_PD | PLL_BYPASS, CRU_CPLL_CON);
504 static unsigned long codec_pll_clk_recalc(struct clk *clk)
507 u32 v = cru_readl(CRU_CPLL_CON);
508 u64 rate64 = (u64) clk->parent->rate * PLL_NF(v);
509 do_div(rate64, PLL_NR(v));
510 rate = rate64 >> PLL_NO_SHIFT(v);
511 pr_debug("%s new clock rate is %ld (NF %d NR %d NO %d)\n", clk->name, rate, PLL_NF(v), PLL_NR(v), 1 << PLL_NO_SHIFT(v));
512 if ((cru_readl(CRU_MODE_CON) & CRU_CODEC_MODE_MASK) != CRU_CODEC_MODE_NORMAL)
513 pr_debug("%s rate is %ld (slow mode) actually\n", clk->name, clk->parent->rate);
517 #define CODEC_PLL_PARENT_MASK (3 << 11)
518 #define CODEC_PLL_PARENT_XIN24M (0 << 11)
519 #define CODEC_PLL_PARENT_XIN27M (1 << 11)
520 #define CODEC_PLL_PARENT_DDR_PLL (2 << 11)
521 #define CODEC_PLL_PARENT_GENERAL_PLL (3 << 11)
523 struct codec_pll_set {
529 #define CODEC_PLL(_mhz, _parent, band, nr, nf, no) \
531 .rate = _mhz * MHZ, \
532 .pll_con = PLL_##band##_BAND | PLL_CLKR(nr) | PLL_CLKF(nf) | PLL_NO_##no, \
533 .parent_con = CODEC_PLL_PARENT_XIN##_parent##M, \
536 static const struct codec_pll_set codec_pll[] = {
537 // rate parent band NR NF NO
538 CODEC_PLL(108, 24, LOW, 1, 18, 4), // for TV
539 CODEC_PLL(648, 24, HIGH, 1, 27, 1),
540 CODEC_PLL(297, 27, LOW, 1, 22, 2), // for HDMI
541 CODEC_PLL(594, 27, LOW, 1, 22, 1),
542 CODEC_PLL(360, 24, LOW, 1, 15, 1), // for GPU
543 CODEC_PLL(408, 24, LOW, 1, 17, 1),
544 CODEC_PLL(456, 24, LOW, 1, 19, 1),
545 CODEC_PLL(504, 24, LOW, 1, 21, 1),
546 CODEC_PLL(552, 24, LOW, 1, 23, 1),
547 CODEC_PLL(600, 24, HIGH, 1, 25, 1),
550 static int codec_pll_clk_set_rate(struct clk *clk, unsigned long rate)
555 const struct codec_pll_set *ps = NULL;
557 for (i = 0; i < ARRAY_SIZE(codec_pll); i++) {
558 if (codec_pll[i].rate == rate) {
566 work_mode = cru_readl(CRU_MODE_CON) & CRU_CODEC_MODE_MASK;
568 /* enter slow mode */
569 cru_writel((cru_readl(CRU_MODE_CON) & ~(CRU_CODEC_MODE_MASK | CODEC_PLL_PARENT_MASK)) | CRU_CODEC_MODE_SLOW | ps->parent_con, CRU_MODE_CON);
572 cru_writel(cru_readl(CRU_CPLL_CON) | PLL_PD, CRU_CPLL_CON);
576 cru_writel(ps->pll_con | PLL_PD, CRU_CPLL_CON);
581 cru_writel(ps->pll_con, CRU_CPLL_CON);
584 pll_wait_lock(CODEC_PLL_IDX);
586 /* enter normal mode */
587 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CODEC_MODE_MASK) | work_mode, CRU_MODE_CON);
589 clk_set_parent_nolock(clk, ps->parent_con == CODEC_PLL_PARENT_XIN24M ? &xin24m : &xin27m);
594 static struct clk *codec_pll_parents[4] = { &xin24m, &xin27m, &ddr_pll_clk, &general_pll_clk };
596 static struct clk codec_pll_clk = {
599 .mode = codec_pll_clk_mode,
600 .recalc = codec_pll_clk_recalc,
601 .set_rate = codec_pll_clk_set_rate,
602 .clksel_con = CRU_MODE_CON,
603 .clksel_parent_mask = 3,
604 .clksel_parent_shift = 11,
605 .parents = codec_pll_parents,
609 static unsigned long general_pll_clk_recalc(struct clk *clk)
613 if ((cru_readl(CRU_MODE_CON) & CRU_GENERAL_MODE_MASK) == CRU_GENERAL_MODE_NORMAL) {
614 u32 v = cru_readl(CRU_GPLL_CON);
615 u64 rate64 = (u64) clk->parent->rate * PLL_NF(v);
616 do_div(rate64, PLL_NR(v));
617 rate = rate64 >> PLL_NO_SHIFT(v);
618 pr_debug("%s new clock rate is %ld (NF %d NR %d NO %d)\n", clk->name, rate, PLL_NF(v), PLL_NR(v), 1 << PLL_NO_SHIFT(v));
620 rate = clk->parent->rate;
621 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
627 static int general_pll_clk_set_rate(struct clk *clk, unsigned long rate)
633 /* 96M: low-band, NR=1, NF=16, NO=4 */
634 pll_con = PLL_LOW_BAND | PLL_CLKR(1) | PLL_CLKF(16) | PLL_NO_4;
637 /* 96M: low-band, NR=1, NF=16, NO=4 */
638 pll_con = PLL_LOW_BAND | PLL_CLKR(1) | PLL_CLKF(24) | PLL_NO_4;
641 /* 288M: low-band, NR=1, NF=24, NO=2 */
642 pll_con = PLL_LOW_BAND | PLL_CLKR(1) | PLL_CLKF(24) | PLL_NO_2;
645 /* 300M: low-band, NR=1, NF=25, NO=2 */
646 pll_con = PLL_LOW_BAND | PLL_CLKR(1) | PLL_CLKF(25) | PLL_NO_2;
649 /* 624M: high-band, NR=1, NF=26, NO=1 */
650 pll_con = PLL_HIGH_BAND | PLL_CLKR(1) | PLL_CLKF(26) | PLL_NO_1;
657 /* enter slow mode */
658 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_GENERAL_MODE_MASK) | CRU_GENERAL_MODE_SLOW, CRU_MODE_CON);
661 cru_writel(cru_readl(CRU_GPLL_CON) | PLL_PD, CRU_GPLL_CON);
665 cru_writel(pll_con | PLL_PD, CRU_GPLL_CON);
670 cru_writel(pll_con, CRU_GPLL_CON);
673 pll_wait_lock(GENERAL_PLL_IDX);
675 /* enter normal mode */
676 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_GENERAL_MODE_MASK) | CRU_GENERAL_MODE_NORMAL, CRU_MODE_CON);
681 static struct clk *general_pll_parents[4] = { &xin24m, &xin27m, &ddr_pll_clk, &codec_pll_clk };
683 static struct clk general_pll_clk = {
684 .name = "general_pll",
686 .recalc = general_pll_clk_recalc,
687 .set_rate = general_pll_clk_set_rate,
688 .clksel_con = CRU_MODE_CON,
689 .clksel_parent_mask = 3,
690 .clksel_parent_shift = 9,
691 .parents = general_pll_parents,
695 static struct clk *clk_core_parents[4] = { &arm_pll_clk, &general_pll_clk, &codec_pll_clk, &ddr_pll_clk };
697 static struct clk clk_core = {
699 .parent = &arm_pll_clk,
700 .recalc = clksel_recalc_div,
701 .clksel_con = CRU_CLKSEL0_CON,
704 .clksel_parent_mask = 3,
705 .clksel_parent_shift = 23,
706 .parents = clk_core_parents,
709 static unsigned long aclk_cpu_recalc(struct clk *clk)
712 u32 div = ((cru_readl(CRU_CLKSEL0_CON) >> 5) & 0x7) + 1;
717 rate = clk->parent->rate / div;
718 pr_debug("%s new clock rate is %ld (div %d)\n", clk->name, rate, div);
723 static struct clk aclk_cpu = {
726 .recalc = aclk_cpu_recalc,
729 static struct clk hclk_cpu = {
732 .recalc = clksel_recalc_shift,
733 .set_rate = clksel_set_rate_shift,
734 .clksel_con = CRU_CLKSEL0_CON,
740 static struct clk pclk_cpu = {
743 .recalc = clksel_recalc_shift,
744 .set_rate = clksel_set_rate_shift,
745 .clksel_con = CRU_CLKSEL0_CON,
751 static struct clk *aclk_periph_parents[4] = { &general_pll_clk, &arm_pll_clk, &ddr_pll_clk, &codec_pll_clk };
753 static struct clk aclk_periph = {
754 .name = "aclk_periph",
756 .gate_idx = CLK_GATE_ACLK_PEIRPH,
757 .recalc = clksel_recalc_div,
758 .set_rate = clksel_set_rate_div,
759 .clksel_con = CRU_CLKSEL0_CON,
762 .clksel_parent_mask = 3,
763 .clksel_parent_shift = 12,
764 .parents = aclk_periph_parents,
767 static struct clk pclk_periph = {
768 .name = "pclk_periph",
770 .gate_idx = CLK_GATE_PCLK_PEIRPH,
771 .parent = &aclk_periph,
772 .recalc = clksel_recalc_shift,
773 .set_rate = clksel_set_rate_shift,
774 .clksel_con = CRU_CLKSEL0_CON,
780 static struct clk hclk_periph = {
781 .name = "hclk_periph",
783 .gate_idx = CLK_GATE_HCLK_PEIRPH,
784 .parent = &aclk_periph,
785 .recalc = clksel_recalc_shift,
786 .set_rate = clksel_set_rate_shift,
787 .clksel_con = CRU_CLKSEL0_CON,
794 static unsigned long uhost_recalc(struct clk *clk)
796 unsigned long rate = clksel_recalc_div(clk);
797 if (rate != 48 * MHZ) {
798 clksel_set_rate_div(clk, 48 * MHZ);
799 rate = clksel_recalc_div(clk);
804 static struct clk *clk_uhost_parents[8] = { &general_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
806 static struct clk clk_uhost = {
809 .recalc = uhost_recalc,
810 .set_rate = clksel_set_rate_div,
811 .gate_idx = CLK_GATE_UHOST,
812 .clksel_con = CRU_CLKSEL1_CON,
815 .clksel_parent_mask = 7,
816 .clksel_parent_shift = 13,
817 .parents = clk_uhost_parents,
820 static struct clk *clk_otgphy_parents[4] = { &xin24m, &clk_12m, &clk_uhost };
822 static struct clk clk_otgphy0 = {
825 .gate_idx = CLK_GATE_USBPHY0,
826 .clksel_con = CRU_CLKSEL1_CON,
827 .clksel_parent_mask = 3,
828 .clksel_parent_shift = 9,
829 .parents = clk_otgphy_parents,
832 static struct clk clk_otgphy1 = {
835 .gate_idx = CLK_GATE_USBPHY1,
836 .clksel_con = CRU_CLKSEL1_CON,
837 .clksel_parent_mask = 3,
838 .clksel_parent_shift = 11,
839 .parents = clk_otgphy_parents,
843 static struct clk rmii_clkin = {
844 .name = "rmii_clkin",
847 static struct clk *clk_mac_ref_div_parents[4] = { &arm_pll_clk, &general_pll_clk, &codec_pll_clk, &ddr_pll_clk };
849 static struct clk clk_mac_ref_div = {
850 .name = "mac_ref_div",
851 .recalc = clksel_recalc_div,
852 .set_rate = clksel_set_rate_div,
853 .clksel_con = CRU_CLKSEL1_CON,
856 .clksel_parent_mask = 3,
857 .clksel_parent_shift = 21,
858 .parents = clk_mac_ref_div_parents,
861 static struct clk *clk_mac_ref_parents[2] = { &clk_mac_ref_div, &rmii_clkin };
863 static struct clk clk_mac_ref = {
866 .gate_idx = CLK_GATE_MAC_REF,
867 .clksel_con = CRU_CLKSEL1_CON,
868 .clksel_parent_mask = 1,
869 .clksel_parent_shift = 28,
870 .parents = clk_mac_ref_parents,
874 static struct clk *clk_i2s_div_parents[8] = { &codec_pll_clk, &general_pll_clk, &arm_pll_clk, &ddr_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
876 static struct clk clk_i2s0_div = {
878 .recalc = clksel_recalc_div,
879 .set_rate = clksel_set_rate_div,
880 .clksel_con = CRU_CLKSEL2_CON,
883 .clksel_parent_mask = 7,
884 .clksel_parent_shift = 0,
885 .parents = clk_i2s_div_parents,
888 static struct clk clk_i2s1_div = {
890 .recalc = clksel_recalc_div,
891 .set_rate = clksel_set_rate_div,
892 .clksel_con = CRU_CLKSEL2_CON,
895 .clksel_parent_mask = 7,
896 .clksel_parent_shift = 10,
897 .parents = clk_i2s_div_parents,
900 static struct clk clk_spdif_div = {
902 .recalc = clksel_recalc_div,
903 .set_rate = clksel_set_rate_div,
904 .clksel_con = CRU_CLKSEL2_CON,
907 .clksel_parent_mask = 7,
908 .clksel_parent_shift = 20,
909 .parents = clk_i2s_div_parents,
912 static u32 clk_gcd(u32 numerator, u32 denominator)
916 if (!numerator || !denominator)
918 if (numerator > denominator) {
934 static int clk_i2s_frac_div_set_rate(struct clk *clk, unsigned long rate)
936 u32 numerator, denominator;
939 gcd = clk_gcd(rate, clk->parent->rate);
940 pr_debug("i2s rate=%ld,parent=%ld,gcd=%d\n", rate, clk->parent->rate, gcd);
942 pr_err("gcd=0, i2s frac div is not be supported\n");
946 numerator = rate / gcd;
947 denominator = clk->parent->rate / gcd;
948 pr_debug("i2s numerator=%d,denominator=%d,times=%d\n",
949 numerator, denominator, denominator / numerator);
950 if (numerator > 0xffff || denominator > 0xffff) {
951 pr_err("i2s can't get a available nume and deno\n");
955 pr_debug("set clock %s to rate %ld (%d/%d)\n", clk->name, rate, numerator, denominator);
956 cru_writel(numerator << 16 | denominator, clk->clksel_con);
961 static struct clk clk_i2s0_frac_div = {
962 .name = "i2s0_frac_div",
963 .parent = &clk_i2s0_div,
964 .recalc = clksel_recalc_frac,
965 .set_rate = clk_i2s_frac_div_set_rate,
966 .clksel_con = CRU_CLKSEL3_CON,
969 static struct clk clk_i2s1_frac_div = {
970 .name = "i2s1_frac_div",
971 .parent = &clk_i2s1_div,
972 .recalc = clksel_recalc_frac,
973 .set_rate = clk_i2s_frac_div_set_rate,
974 .clksel_con = CRU_CLKSEL4_CON,
977 static struct clk clk_spdif_frac_div = {
978 .name = "spdif_frac_div",
979 .parent = &clk_spdif_div,
980 .recalc = clksel_recalc_frac,
981 .set_rate = clk_i2s_frac_div_set_rate,
982 .clksel_con = CRU_CLKSEL5_CON,
985 static int i2s_set_rate(struct clk *clk, unsigned long rate)
990 if (rate == 12 * MHZ) {
993 parent = clk->parents[1]; /* frac div */
994 ret = clk_set_rate_nolock(parent, rate);
998 if (clk->parent != parent)
999 clk_set_parent_nolock(clk, parent);
1004 static struct clk *clk_i2s0_parents[4] = { &clk_i2s0_div, &clk_i2s0_frac_div, &clk_12m, &xin24m };
1006 static struct clk clk_i2s0 = {
1009 .gate_idx = CLK_GATE_I2S0,
1010 .set_rate = i2s_set_rate,
1011 .clksel_con = CRU_CLKSEL2_CON,
1012 .clksel_parent_mask = 3,
1013 .clksel_parent_shift = 8,
1014 .parents = clk_i2s0_parents,
1017 static struct clk *clk_i2s1_parents[4] = { &clk_i2s1_div, &clk_i2s1_frac_div, &clk_12m, &xin24m };
1019 static struct clk clk_i2s1 = {
1022 .gate_idx = CLK_GATE_I2S1,
1023 .set_rate = i2s_set_rate,
1024 .clksel_con = CRU_CLKSEL2_CON,
1025 .clksel_parent_mask = 3,
1026 .clksel_parent_shift = 18,
1027 .parents = clk_i2s1_parents,
1030 static struct clk *clk_spdif_parents[4] = { &clk_spdif_div, &clk_spdif_frac_div, &clk_12m, &xin24m };
1032 static struct clk clk_spdif = {
1035 .gate_idx = CLK_GATE_SPDIF,
1036 .set_rate = i2s_set_rate,
1037 .clksel_con = CRU_CLKSEL2_CON,
1038 .clksel_parent_mask = 3,
1039 .clksel_parent_shift = 28,
1040 .parents = clk_spdif_parents,
1044 static struct clk *clk_spi_src_parents[4] = { &general_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk };
1046 static struct clk clk_spi_src = {
1048 .clksel_con = CRU_CLKSEL6_CON,
1049 .clksel_parent_mask = 3,
1050 .clksel_parent_shift = 0,
1051 .parents = clk_spi_src_parents,
1054 static struct clk clk_spi0 = {
1056 .parent = &clk_spi_src,
1058 .recalc = clksel_recalc_div,
1059 .set_rate = clksel_set_rate_div,
1060 .gate_idx = CLK_GATE_SPI0,
1061 .clksel_con = CRU_CLKSEL6_CON,
1062 .clksel_mask = 0x7F,
1066 static struct clk clk_spi1 = {
1068 .parent = &clk_spi_src,
1070 .recalc = clksel_recalc_div,
1071 .set_rate = clksel_set_rate_div,
1072 .gate_idx = CLK_GATE_SPI1,
1073 .clksel_con = CRU_CLKSEL6_CON,
1074 .clksel_mask = 0x7F,
1079 static struct clk clk_saradc = {
1081 .parent = &pclk_periph,
1083 .recalc = clksel_recalc_div,
1084 .set_rate = clksel_set_rate_div,
1085 .gate_idx = CLK_GATE_SARADC,
1086 .clksel_con = CRU_CLKSEL6_CON,
1087 .clksel_mask = 0xFF,
1092 static struct clk *clk_cpu_timer_parents[2] = { &pclk_cpu, &xin24m };
1094 static struct clk clk_timer0 = {
1097 .gate_idx = CLK_GATE_TIMER0,
1098 .clksel_con = CRU_CLKSEL6_CON,
1099 .clksel_parent_mask = 1,
1100 .clksel_parent_shift = 26,
1101 .parents = clk_cpu_timer_parents,
1104 static struct clk clk_timer1 = {
1107 .gate_idx = CLK_GATE_TIMER1,
1108 .clksel_con = CRU_CLKSEL6_CON,
1109 .clksel_parent_mask = 1,
1110 .clksel_parent_shift = 27,
1111 .parents = clk_cpu_timer_parents,
1114 static struct clk *clk_periph_timer_parents[2] = { &pclk_periph, &xin24m };
1116 static struct clk clk_timer2 = {
1119 .gate_idx = CLK_GATE_TIMER2,
1120 .clksel_con = CRU_CLKSEL6_CON,
1121 .clksel_parent_mask = 1,
1122 .clksel_parent_shift = 28,
1123 .parents = clk_periph_timer_parents,
1126 static struct clk clk_timer3 = {
1129 .gate_idx = CLK_GATE_TIMER3,
1130 .clksel_con = CRU_CLKSEL6_CON,
1131 .clksel_parent_mask = 1,
1132 .clksel_parent_shift = 29,
1133 .parents = clk_periph_timer_parents,
1137 static struct clk *clk_mmc_src_parents[4] = { &arm_pll_clk, &general_pll_clk, &codec_pll_clk, &ddr_pll_clk };
1139 static struct clk clk_mmc_src = {
1141 .clksel_con = CRU_CLKSEL7_CON,
1142 .clksel_parent_mask = 3,
1143 .clksel_parent_shift = 0,
1144 .parents = clk_mmc_src_parents,
1147 static struct clk clk_mmc0 = {
1149 .parent = &clk_mmc_src,
1151 .recalc = clksel_recalc_div,
1152 .set_rate = clksel_set_rate_div,
1153 .gate_idx = CLK_GATE_MMC0,
1154 .clksel_con = CRU_CLKSEL7_CON,
1155 .clksel_mask = 0x3F,
1159 static struct clk clk_mmc1 = {
1161 .parent = &clk_mmc_src,
1163 .recalc = clksel_recalc_div,
1164 .set_rate = clksel_set_rate_div,
1165 .gate_idx = CLK_GATE_MMC1,
1166 .clksel_con = CRU_CLKSEL7_CON,
1167 .clksel_mask = 0x3F,
1171 static struct clk clk_emmc = {
1173 .parent = &clk_mmc_src,
1175 .recalc = clksel_recalc_div,
1176 .set_rate = clksel_set_rate_div,
1177 .gate_idx = CLK_GATE_EMMC,
1178 .clksel_con = CRU_CLKSEL7_CON,
1179 .clksel_mask = 0x3F,
1184 static struct clk *clk_ddr_parents[8] = { &ddr_pll_clk, &general_pll_clk, &codec_pll_clk, &arm_pll_clk };
1186 static struct clk clk_ddr = {
1188 .recalc = clksel_recalc_shift,
1189 .clksel_con = CRU_CLKSEL7_CON,
1192 .clksel_maxdiv = 32,
1193 .clksel_parent_mask = 3,
1194 .clksel_parent_shift = 24,
1195 .parents = clk_ddr_parents,
1199 static int clk_uart_set_rate(struct clk *clk, unsigned long rate)
1203 struct clk *clk_div = clk->parents[0];
1206 case 24*MHZ: /* 1.5M/0.5M/50/75/150/200/300/600/1200/2400 */
1207 parent = clk->parents[2]; /* xin24m */
1216 parent = clk->parents[1]; /* frac div */
1217 /* reset div to 1 */
1218 ret = clk_set_rate_nolock(clk_div, clk_div->parent->rate);
1227 if (parent->set_rate) {
1228 ret = clk_set_rate_nolock(parent, rate);
1233 if (clk->parent != parent)
1234 clk_set_parent_nolock(clk, parent);
1239 static int clk_uart_frac_div_set_rate(struct clk *clk, unsigned long rate)
1241 u32 numerator, denominator;
1244 gcd = clk_gcd(rate, clk->parent->rate);
1245 pr_debug("uart rate=%ld,parent=%ld,gcd=%d\n", rate, clk->parent->rate, gcd);
1247 pr_err("gcd=0, uart frac div is not be supported\n");
1251 numerator = rate / gcd;
1252 denominator = clk->parent->rate / gcd;
1253 pr_debug("uart numerator=%d,denominator=%d,times=%d\n",
1254 numerator, denominator, denominator / numerator);
1255 if (numerator > 0xffff || denominator > 0xffff) {
1256 pr_err("uart_frac can't get a available nume and deno\n");
1260 pr_debug("set clock %s to rate %ld (%d/%d)\n", clk->name, rate, numerator, denominator);
1261 cru_writel(numerator << 16 | denominator, clk->clksel_con);
1266 static struct clk *clk_uart_src_parents[8] = { &general_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
1268 static struct clk clk_uart01_src = {
1269 .name = "uart01_src",
1270 .clksel_con = CRU_CLKSEL8_CON,
1271 .clksel_parent_mask = 7,
1272 .clksel_parent_shift = 0,
1273 .parents = clk_uart_src_parents,
1276 static struct clk clk_uart0_div = {
1277 .name = "uart0_div",
1278 .parent = &clk_uart01_src,
1279 .recalc = clksel_recalc_div,
1280 .set_rate = clksel_set_rate_div,
1281 .clksel_con = CRU_CLKSEL8_CON,
1282 .clksel_mask = 0x3F,
1286 static struct clk clk_uart0_frac_div = {
1287 .name = "uart0_frac_div",
1288 .parent = &clk_uart0_div,
1289 .recalc = clksel_recalc_frac,
1290 .set_rate = clk_uart_frac_div_set_rate,
1291 .clksel_con = CRU_CLKSEL10_CON,
1294 static struct clk *clk_uart0_parents[4] = { &clk_uart0_div, &clk_uart0_frac_div, &xin24m };
1296 static struct clk clk_uart0 = {
1299 .set_rate = clk_uart_set_rate,
1300 .gate_idx = CLK_GATE_UART0,
1301 .clksel_con = CRU_CLKSEL8_CON,
1302 .clksel_parent_mask = 3,
1303 .clksel_parent_shift = 9,
1304 .parents = clk_uart0_parents,
1307 static struct clk clk_uart1_div = {
1308 .name = "uart1_div",
1309 .parent = &clk_uart01_src,
1310 .recalc = clksel_recalc_div,
1311 .set_rate = clksel_set_rate_div,
1312 .clksel_con = CRU_CLKSEL8_CON,
1313 .clksel_mask = 0x3F,
1317 static struct clk clk_uart1_frac_div = {
1318 .name = "uart1_frac_div",
1319 .parent = &clk_uart1_div,
1320 .recalc = clksel_recalc_frac,
1321 .set_rate = clk_uart_frac_div_set_rate,
1322 .clksel_con = CRU_CLKSEL11_CON,
1325 static struct clk *clk_uart1_parents[4] = { &clk_uart1_div, &clk_uart1_frac_div, &xin24m };
1327 static struct clk clk_uart1 = {
1330 .set_rate = clk_uart_set_rate,
1331 .gate_idx = CLK_GATE_UART1,
1332 .clksel_con = CRU_CLKSEL8_CON,
1333 .clksel_parent_mask = 3,
1334 .clksel_parent_shift = 20,
1335 .parents = clk_uart1_parents,
1338 static struct clk clk_uart23_src = {
1339 .name = "uart23_src",
1340 .clksel_con = CRU_CLKSEL9_CON,
1341 .clksel_parent_mask = 7,
1342 .clksel_parent_shift = 0,
1343 .parents = clk_uart_src_parents,
1346 static struct clk clk_uart2_div = {
1347 .name = "uart2_div",
1348 .parent = &clk_uart23_src,
1349 .recalc = clksel_recalc_div,
1350 .set_rate = clksel_set_rate_div,
1351 .clksel_con = CRU_CLKSEL9_CON,
1352 .clksel_mask = 0x3F,
1356 static struct clk clk_uart2_frac_div = {
1357 .name = "uart2_frac_div",
1358 .parent = &clk_uart2_div,
1359 .recalc = clksel_recalc_frac,
1360 .set_rate = clk_uart_frac_div_set_rate,
1361 .clksel_con = CRU_CLKSEL12_CON,
1364 static struct clk *clk_uart2_parents[4] = { &clk_uart2_div, &clk_uart2_frac_div, &xin24m };
1366 static struct clk clk_uart2 = {
1369 .set_rate = clk_uart_set_rate,
1370 .gate_idx = CLK_GATE_UART2,
1371 .clksel_con = CRU_CLKSEL9_CON,
1372 .clksel_parent_mask = 3,
1373 .clksel_parent_shift = 9,
1374 .parents = clk_uart2_parents,
1377 static struct clk clk_uart3_div = {
1378 .name = "uart3_div",
1379 .parent = &clk_uart23_src,
1380 .recalc = clksel_recalc_div,
1381 .set_rate = clksel_set_rate_div,
1382 .clksel_con = CRU_CLKSEL9_CON,
1383 .clksel_mask = 0x3F,
1387 static struct clk clk_uart3_frac_div = {
1388 .name = "uart3_frac_div",
1389 .parent = &clk_uart3_div,
1390 .recalc = clksel_recalc_frac,
1391 .set_rate = clk_uart_frac_div_set_rate,
1392 .clksel_con = CRU_CLKSEL13_CON,
1395 static struct clk *clk_uart3_parents[4] = { &clk_uart3_div, &clk_uart3_frac_div, &xin24m };
1397 static struct clk clk_uart3 = {
1400 .set_rate = clk_uart_set_rate,
1401 .gate_idx = CLK_GATE_UART3,
1402 .clksel_con = CRU_CLKSEL9_CON,
1403 .clksel_parent_mask = 3,
1404 .clksel_parent_shift = 20,
1405 .parents = clk_uart3_parents,
1409 static struct clk *clk_hsadc_div_parents[8] = { &codec_pll_clk, &ddr_pll_clk, &general_pll_clk, &arm_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
1411 static struct clk clk_hsadc_div = {
1412 .name = "hsadc_div",
1413 .recalc = clksel_recalc_div,
1414 .set_rate = clksel_set_rate_div,
1415 .clksel_con = CRU_CLKSEL14_CON,
1416 .clksel_mask = 0xFF,
1418 .clksel_parent_mask = 7,
1419 .clksel_parent_shift = 7,
1420 .parents = clk_hsadc_div_parents,
1423 static struct clk clk_hsadc_frac_div = {
1424 .name = "hsadc_frac_div",
1425 .parent = &clk_hsadc_div,
1426 .recalc = clksel_recalc_frac,
1427 .clksel_con = CRU_CLKSEL15_CON,
1430 static struct clk *clk_demod_parents[4] = { &clk_hsadc_div, &clk_hsadc_frac_div, &xin27m };
1432 static struct clk clk_demod = {
1434 .clksel_con = CRU_CLKSEL14_CON,
1435 .clksel_parent_mask = 3,
1436 .clksel_parent_shift = 18,
1437 .parents = clk_demod_parents,
1440 static struct clk gpsclk = {
1444 static struct clk *clk_hsadc_parents[2] = { &clk_demod, &gpsclk };
1446 static struct clk clk_hsadc = {
1449 .gate_idx = CLK_GATE_HSADC,
1450 .clksel_con = CRU_CLKSEL14_CON,
1451 .clksel_parent_mask = 1,
1452 .clksel_parent_shift = 21,
1453 .parents = clk_hsadc_parents,
1456 static unsigned long div2_recalc(struct clk *clk)
1458 return clk->parent->rate >> 1;
1461 static struct clk clk_hsadc_div2 = {
1462 .name = "hsadc_div2",
1463 .parent = &clk_demod,
1464 .recalc = div2_recalc,
1467 static struct clk clk_hsadc_div2_inv = {
1468 .name = "hsadc_div2_inv",
1469 .parent = &clk_demod,
1470 .recalc = div2_recalc,
1473 static struct clk *clk_hsadc_out_parents[2] = { &clk_hsadc_div2, &clk_hsadc_div2_inv };
1475 static struct clk clk_hsadc_out = {
1476 .name = "hsadc_out",
1477 .clksel_con = CRU_CLKSEL14_CON,
1478 .clksel_parent_mask = 1,
1479 .clksel_parent_shift = 20,
1480 .parents = clk_hsadc_out_parents,
1484 static struct clk *dclk_lcdc_div_parents[4] = { &codec_pll_clk, &ddr_pll_clk, &general_pll_clk, &arm_pll_clk };
1486 static struct clk dclk_lcdc_div = {
1487 .name = "dclk_lcdc_div",
1488 .recalc = clksel_recalc_div,
1489 .set_rate = clksel_set_rate_div,
1490 .clksel_con = CRU_CLKSEL16_CON,
1491 .clksel_mask = 0xFF,
1493 .clksel_parent_mask = 3,
1494 .clksel_parent_shift = 0,
1495 .parents = dclk_lcdc_div_parents,
1498 static struct clk *dclk_lcdc_parents[2] = { &dclk_lcdc_div, &xin27m };
1500 static struct clk dclk_lcdc = {
1501 .name = "dclk_lcdc",
1503 .gate_idx = CLK_GATE_DCLK_LCDC,
1504 .clksel_con = CRU_CLKSEL16_CON,
1505 .clksel_parent_mask = 1,
1506 .clksel_parent_shift = 10,
1507 .parents = dclk_lcdc_parents,
1510 static struct clk dclk_ebook = {
1511 .name = "dclk_ebook",
1513 .gate_idx = CLK_GATE_DCLK_EBOOK,
1514 .recalc = clksel_recalc_div,
1515 .set_rate = clksel_set_rate_div,
1516 .clksel_con = CRU_CLKSEL16_CON,
1517 .clksel_mask = 0x1F,
1519 .clksel_parent_mask = 3,
1520 .clksel_parent_shift = 11,
1521 .parents = dclk_lcdc_div_parents,
1524 static struct clk *aclk_lcdc_parents[4] = { &ddr_pll_clk, &codec_pll_clk, &general_pll_clk, &arm_pll_clk };
1526 static struct clk aclk_lcdc = {
1527 .name = "aclk_lcdc",
1529 .gate_idx = CLK_GATE_ACLK_LCDC,
1530 .recalc = clksel_recalc_div,
1531 .set_rate = clksel_set_rate_div,
1532 .clksel_con = CRU_CLKSEL16_CON,
1533 .clksel_mask = 0x1F,
1535 .clksel_parent_mask = 3,
1536 .clksel_parent_shift = 18,
1537 .parents = aclk_lcdc_parents,
1540 static struct clk hclk_lcdc = {
1541 .name = "hclk_lcdc",
1543 .gate_idx = CLK_GATE_HCLK_LCDC,
1544 .parent = &aclk_lcdc,
1545 .clksel_con = CRU_CLKSEL16_CON,
1546 .recalc = clksel_recalc_shift,
1547 .set_rate = clksel_set_rate_shift,
1553 static struct clk *xpu_parents[4] = { &general_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk };
1555 static struct clk aclk_vepu = {
1556 .name = "aclk_vepu",
1558 .recalc = clksel_recalc_div,
1559 .set_rate = clksel_set_rate_div,
1560 .gate_idx = CLK_GATE_ACLK_VEPU,
1561 .clksel_con = CRU_CLKSEL17_CON,
1562 .clksel_mask = 0x1F,
1564 .clksel_parent_mask = 3,
1565 .clksel_parent_shift = 0,
1566 .parents = xpu_parents,
1569 static struct clk hclk_vepu = {
1570 .name = "hclk_vepu",
1571 .parent = &aclk_vepu,
1573 .recalc = clksel_recalc_shift,
1574 .set_rate = clksel_set_rate_shift,
1575 .gate_idx = CLK_GATE_HCLK_VEPU,
1576 .clksel_con = CRU_CLKSEL17_CON,
1582 static struct clk aclk_vdpu = {
1583 .name = "aclk_vdpu",
1584 .parent = &general_pll_clk,
1586 .recalc = clksel_recalc_div,
1587 .set_rate = clksel_set_rate_div,
1588 .gate_idx = CLK_GATE_ACLK_VDPU,
1589 .clksel_con = CRU_CLKSEL17_CON,
1590 .clksel_mask = 0x1F,
1592 .clksel_parent_mask = 3,
1593 .clksel_parent_shift = 7,
1594 .parents = xpu_parents,
1597 static struct clk hclk_vdpu = {
1598 .name = "hclk_vdpu",
1599 .parent = &aclk_vdpu,
1601 .recalc = clksel_recalc_shift,
1602 .set_rate = clksel_set_rate_shift,
1603 .gate_idx = CLK_GATE_HCLK_VDPU,
1604 .clksel_con = CRU_CLKSEL17_CON,
1610 static struct clk clk_gpu = {
1613 .gate_idx = CLK_GATE_GPU,
1614 .recalc = clksel_recalc_div,
1615 .set_rate = clksel_set_rate_div,
1616 .clksel_con = CRU_CLKSEL17_CON,
1617 .clksel_mask = 0x1F,
1619 .clksel_parent_mask = 3,
1620 .clksel_parent_shift = 14,
1621 .parents = xpu_parents,
1624 static struct clk aclk_gpu = {
1627 .gate_idx = CLK_GATE_ACLK_GPU,
1628 .recalc = clksel_recalc_div,
1629 .set_rate = clksel_set_rate_div,
1630 .clksel_con = CRU_CLKSEL17_CON,
1631 .clksel_mask = 0x1F,
1633 .clksel_parent_mask = 3,
1634 .clksel_parent_shift = 21,
1635 .parents = xpu_parents,
1639 static struct clk vip_clkin = {
1640 .name = "vip_clkin",
1643 static struct clk *clk_vip_parents[4] = { &xin24m, &xin27m, &dclk_ebook };
1645 static struct clk clk_vip_out = {
1648 .gate_idx = CLK_GATE_VIP_OUT,
1649 .clksel_con = CRU_CLKSEL1_CON,
1650 .clksel_parent_mask = 3,
1651 .clksel_parent_shift = 7,
1652 .parents = clk_vip_parents,
1656 #define GATE_CLK(NAME,PARENT,ID) \
1657 static struct clk clk_##NAME = { \
1659 .parent = &PARENT, \
1660 .mode = gate_mode, \
1661 .gate_idx = CLK_GATE_##ID, \
1664 GATE_CLK(i2c0, pclk_cpu, I2C0);
1665 GATE_CLK(i2c1, pclk_periph, I2C1);
1666 GATE_CLK(i2c2, pclk_periph, I2C2);
1667 GATE_CLK(i2c3, pclk_periph, I2C3);
1669 GATE_CLK(gpio0, pclk_cpu, GPIO0);
1670 GATE_CLK(gpio1, pclk_periph, GPIO1);
1671 GATE_CLK(gpio2, pclk_periph, GPIO2);
1672 GATE_CLK(gpio3, pclk_periph, GPIO3);
1673 GATE_CLK(gpio4, pclk_cpu, GPIO4);
1674 GATE_CLK(gpio5, pclk_periph, GPIO5);
1675 GATE_CLK(gpio6, pclk_cpu, GPIO6);
1677 GATE_CLK(dma1, aclk_cpu, DMA1);
1678 GATE_CLK(dma2, aclk_periph, DMA2);
1680 GATE_CLK(gic, aclk_cpu, GIC);
1681 GATE_CLK(intmem, aclk_cpu, INTMEM);
1682 GATE_CLK(rom, hclk_cpu, ROM);
1683 GATE_CLK(ddr_phy, aclk_cpu, DDR_PHY);
1684 GATE_CLK(ddr_reg, aclk_cpu, DDR_REG);
1685 GATE_CLK(ddr_cpu, aclk_cpu, DDR_CPU);
1686 GATE_CLK(efuse, pclk_cpu, EFUSE);
1687 GATE_CLK(tzpc, pclk_cpu, TZPC);
1688 GATE_CLK(debug, pclk_cpu, DEBUG);
1689 GATE_CLK(tpiu, pclk_cpu, TPIU);
1690 GATE_CLK(rtc, pclk_cpu, RTC);
1691 GATE_CLK(pmu, pclk_cpu, PMU);
1692 GATE_CLK(grf, pclk_cpu, GRF);
1694 GATE_CLK(emem, hclk_periph, EMEM);
1695 GATE_CLK(hclk_usb_peri, hclk_periph, HCLK_USB_PERI);
1696 GATE_CLK(aclk_ddr_peri, aclk_periph, ACLK_DDR_PERI);
1697 GATE_CLK(aclk_cpu_peri, aclk_cpu, ACLK_CPU_PERI);
1698 GATE_CLK(aclk_smc, aclk_periph, ACLK_SMC);
1699 GATE_CLK(smc, pclk_periph, SMC);
1700 GATE_CLK(hclk_mac, hclk_periph, HCLK_MAC);
1701 GATE_CLK(mii_tx, hclk_periph, MII_TX);
1702 GATE_CLK(mii_rx, hclk_periph, MII_RX);
1703 GATE_CLK(hif, hclk_periph, HIF);
1704 GATE_CLK(nandc, hclk_periph, NANDC);
1705 GATE_CLK(hclk_hsadc, hclk_periph, HCLK_HSADC);
1706 GATE_CLK(usbotg0, hclk_periph, USBOTG0);
1707 GATE_CLK(usbotg1, hclk_periph, USBOTG1);
1708 GATE_CLK(hclk_uhost, hclk_periph, HCLK_UHOST);
1709 GATE_CLK(pid_filter, hclk_periph, PID_FILTER);
1711 GATE_CLK(vip_slave, hclk_lcdc, VIP_SLAVE);
1712 GATE_CLK(wdt, pclk_periph, WDT);
1713 GATE_CLK(pwm, pclk_periph, PWM);
1714 GATE_CLK(vip_bus, aclk_cpu, VIP_BUS);
1715 GATE_CLK(vip_matrix, clk_vip_bus, VIP_MATRIX);
1716 GATE_CLK(vip_input, vip_clkin, VIP_INPUT);
1717 GATE_CLK(jtag, aclk_cpu, JTAG);
1719 GATE_CLK(aclk_ddr_lcdc, aclk_lcdc, ACLK_DDR_LCDC);
1720 GATE_CLK(aclk_ipp, aclk_lcdc, ACLK_IPP);
1721 GATE_CLK(hclk_ipp, hclk_lcdc, HCLK_IPP);
1722 GATE_CLK(hclk_ebook, hclk_lcdc, HCLK_EBOOK);
1723 GATE_CLK(aclk_disp_matrix, aclk_lcdc, ACLK_DISP_MATRIX);
1724 GATE_CLK(hclk_disp_matrix, hclk_lcdc, HCLK_DISP_MATRIX);
1725 GATE_CLK(aclk_ddr_vepu, aclk_vepu, ACLK_DDR_VEPU);
1726 GATE_CLK(aclk_ddr_vdpu, aclk_vdpu, ACLK_DDR_VDPU);
1727 GATE_CLK(aclk_ddr_gpu, aclk_gpu, ACLK_DDR_GPU);
1728 GATE_CLK(hclk_gpu, hclk_cpu, HCLK_GPU);
1729 GATE_CLK(hclk_cpu_vcodec, hclk_cpu, HCLK_CPU_VCODEC);
1730 GATE_CLK(hclk_cpu_display, hclk_cpu, HCLK_CPU_DISPLAY);
1732 GATE_CLK(hclk_mmc0, hclk_periph, HCLK_MMC0);
1733 GATE_CLK(hclk_mmc1, hclk_periph, HCLK_MMC1);
1734 GATE_CLK(hclk_emmc, hclk_periph, HCLK_EMMC);
1737 static void __sramfunc pmu_set_power_domain_sram(enum pmu_power_domain pd, bool on)
1740 writel(readl(RK29_PMU_BASE + PMU_PD_CON) & ~(1 << pd), RK29_PMU_BASE + PMU_PD_CON);
1742 writel(readl(RK29_PMU_BASE + PMU_PD_CON) | (1 << pd), RK29_PMU_BASE + PMU_PD_CON);
1745 while (pmu_power_domain_is_on(pd) != on)
1749 static noinline void do_pmu_set_power_domain(enum pmu_power_domain pd, bool on)
1751 static unsigned long save_sp;
1753 DDR_SAVE_SP(save_sp);
1754 pmu_set_power_domain_sram(pd, on);
1755 DDR_RESTORE_SP(save_sp);
1758 void pmu_set_power_domain(enum pmu_power_domain pd, bool on)
1760 unsigned long flags;
1763 local_irq_save(flags);
1764 do_pmu_set_power_domain(pd, on);
1765 local_irq_restore(flags);
1769 static int pd_vcodec_mode(struct clk *clk, int on)
1774 gate = cru_clkgate3_con_mirror;
1775 gate |= (1 << CLK_GATE_ACLK_DDR_VEPU % 32);
1776 gate &= ~((1 << CLK_GATE_ACLK_VEPU % 32)
1777 | (1 << CLK_GATE_HCLK_VEPU % 32)
1778 | (1 << CLK_GATE_HCLK_CPU_VCODEC % 32));
1779 cru_writel(gate, CRU_CLKGATE3_CON);
1781 pmu_set_power_domain(PD_VCODEC, true);
1783 cru_writel(cru_clkgate3_con_mirror, CRU_CLKGATE3_CON);
1785 pmu_set_power_domain(PD_VCODEC, false);
1791 static struct clk pd_vcodec = {
1792 .name = "pd_vcodec",
1794 .mode = pd_vcodec_mode,
1795 .gate_idx = PD_VCODEC,
1798 static int pd_display_mode(struct clk *clk, int on)
1803 gate = cru_clkgate3_con_mirror;
1804 gate |= (1 << CLK_GATE_ACLK_DDR_LCDC % 32);
1805 gate &= ~((1 << CLK_GATE_HCLK_CPU_DISPLAY % 32)
1806 | (1 << CLK_GATE_HCLK_DISP_MATRIX % 32)
1807 | (1 << CLK_GATE_ACLK_DISP_MATRIX % 32)
1808 | (1 << CLK_GATE_DCLK_EBOOK % 32)
1809 | (1 << CLK_GATE_HCLK_EBOOK % 32)
1810 | (1 << CLK_GATE_HCLK_IPP % 32)
1811 | (1 << CLK_GATE_ACLK_IPP % 32)
1812 | (1 << CLK_GATE_DCLK_LCDC % 32)
1813 | (1 << CLK_GATE_HCLK_LCDC % 32)
1814 | (1 << CLK_GATE_ACLK_LCDC % 32));
1815 cru_writel(gate, CRU_CLKGATE3_CON);
1817 gate2 = cru_readl(CRU_CLKGATE2_CON);
1819 gate &= ~((1 << CLK_GATE_VIP_OUT % 32)
1820 | (1 << CLK_GATE_VIP_SLAVE % 32)
1821 | (1 << CLK_GATE_VIP_MATRIX % 32)
1822 | (1 << CLK_GATE_VIP_BUS % 32));
1823 cru_writel(gate, CRU_CLKGATE2_CON);
1825 pmu_set_power_domain(PD_DISPLAY, true);
1827 cru_writel(gate2, CRU_CLKGATE2_CON);
1828 cru_writel(cru_clkgate3_con_mirror, CRU_CLKGATE3_CON);
1830 pmu_set_power_domain(PD_DISPLAY, false);
1836 static struct clk pd_display = {
1837 .name = "pd_display",
1839 .mode = pd_display_mode,
1840 .gate_idx = PD_DISPLAY,
1843 static int pd_gpu_mode(struct clk *clk, int on)
1846 pmu_set_power_domain(PD_GPU, true);
1848 pmu_set_power_domain(PD_GPU, false);
1854 static struct clk pd_gpu = {
1857 .mode = pd_gpu_mode,
1862 #define CLK(dev, con, ck) \
1869 #define CLK1(name) \
1873 .clk = &clk_##name, \
1876 static struct clk_lookup clks[] = {
1877 CLK(NULL, "xin24m", &xin24m),
1878 CLK(NULL, "xin27m", &xin27m),
1879 CLK(NULL, "otgphy0_clkin", &otgphy0_clkin),
1880 CLK(NULL, "otgphy1_clkin", &otgphy1_clkin),
1881 CLK(NULL, "gpsclk", &gpsclk),
1882 CLK(NULL, "vip_clkin", &vip_clkin),
1885 CLK(NULL, "arm_pll", &arm_pll_clk),
1886 CLK(NULL, "ddr_pll", &ddr_pll_clk),
1887 CLK(NULL, "codec_pll", &codec_pll_clk),
1888 CLK(NULL, "general_pll", &general_pll_clk),
1891 CLK(NULL, "aclk_cpu", &aclk_cpu),
1892 CLK(NULL, "hclk_cpu", &hclk_cpu),
1893 CLK(NULL, "pclk_cpu", &pclk_cpu),
1895 CLK(NULL, "aclk_periph", &aclk_periph),
1896 CLK(NULL, "hclk_periph", &hclk_periph),
1897 CLK(NULL, "pclk_periph", &pclk_periph),
1906 CLK("rk29_i2s.0", "i2s_div", &clk_i2s0_div),
1907 CLK("rk29_i2s.0", "i2s_frac_div", &clk_i2s0_frac_div),
1908 CLK("rk29_i2s.0", "i2s", &clk_i2s0),
1909 CLK("rk29_i2s.1", "i2s_div", &clk_i2s1_div),
1910 CLK("rk29_i2s.1", "i2s_frac_div", &clk_i2s1_frac_div),
1911 CLK("rk29_i2s.1", "i2s", &clk_i2s1),
1912 CLK(NULL, "spdif_div", &clk_spdif_div),
1913 CLK(NULL, "spdif_frac_div", &clk_spdif_frac_div),
1914 CLK(NULL, "spdif", &clk_spdif),
1917 CLK("rk29xx_spim.0", "spi", &clk_spi0),
1918 CLK("rk29xx_spim.1", "spi", &clk_spi1),
1927 CLK("rk29_sdmmc.0", "mmc", &clk_mmc0),
1928 CLK("rk29_sdmmc.0", "hclk_mmc", &clk_hclk_mmc0),
1929 CLK("rk29_sdmmc.1", "mmc", &clk_mmc1),
1930 CLK("rk29_sdmmc.1", "hclk_mmc", &clk_hclk_mmc1),
1936 CLK("rk29_serial.0", "uart", &clk_uart0),
1937 CLK("rk29_serial.0", "uart_div", &clk_uart0_div),
1938 CLK("rk29_serial.0", "uart_frac_div", &clk_uart0_frac_div),
1939 CLK("rk29_serial.1", "uart", &clk_uart1),
1940 CLK("rk29_serial.1", "uart_div", &clk_uart1_div),
1941 CLK("rk29_serial.1", "uart_frac_div", &clk_uart1_frac_div),
1944 CLK("rk29_serial.2", "uart", &clk_uart2),
1945 CLK("rk29_serial.2", "uart_div", &clk_uart2_div),
1946 CLK("rk29_serial.2", "uart_frac_div", &clk_uart2_frac_div),
1947 CLK("rk29_serial.3", "uart", &clk_uart3),
1948 CLK("rk29_serial.3", "uart_div", &clk_uart3_div),
1949 CLK("rk29_serial.3", "uart_frac_div", &clk_uart3_frac_div),
1952 CLK1(hsadc_frac_div),
1956 CLK1(hsadc_div2_inv),
1959 CLK(NULL, "dclk_lcdc_div", &dclk_lcdc_div),
1960 CLK(NULL, "dclk_lcdc", &dclk_lcdc),
1961 CLK(NULL, "dclk_ebook", &dclk_ebook),
1962 CLK(NULL, "aclk_lcdc", &aclk_lcdc),
1963 CLK(NULL, "hclk_lcdc", &hclk_lcdc),
1965 CLK(NULL, "aclk_vepu", &aclk_vepu),
1966 CLK(NULL, "hclk_vepu", &hclk_vepu),
1967 CLK(NULL, "aclk_vdpu", &aclk_vdpu),
1968 CLK(NULL, "hclk_vdpu", &hclk_vdpu),
1970 CLK(NULL, "aclk_gpu", &aclk_gpu),
1972 CLK("rk29_i2c.0", "i2c", &clk_i2c0),
1973 CLK("rk29_i2c.1", "i2c", &clk_i2c1),
1974 CLK("rk29_i2c.2", "i2c", &clk_i2c2),
1975 CLK("rk29_i2c.3", "i2c", &clk_i2c3),
2003 CLK1(hclk_usb_peri),
2004 CLK1(aclk_ddr_peri),
2005 CLK1(aclk_cpu_peri),
2027 CLK1(aclk_ddr_lcdc),
2031 CLK1(aclk_disp_matrix),
2032 CLK1(hclk_disp_matrix),
2033 CLK1(aclk_ddr_vepu),
2034 CLK1(aclk_ddr_vdpu),
2037 CLK1(hclk_cpu_vcodec),
2038 CLK1(hclk_cpu_display),
2040 CLK(NULL, "pd_vcodec", &pd_vcodec),
2041 CLK(NULL, "pd_display", &pd_display),
2042 CLK(NULL, "pd_gpu", &pd_gpu),
2045 static LIST_HEAD(clocks);
2046 static DEFINE_MUTEX(clocks_mutex);
2047 static DEFINE_SPINLOCK(clockfw_lock);
2048 #define LOCK() do { WARN_ON(in_irq()); if (!irqs_disabled()) spin_lock_bh(&clockfw_lock); } while (0)
2049 #define UNLOCK() do { if (!irqs_disabled()) spin_unlock_bh(&clockfw_lock); } while (0)
2051 static int clk_enable_nolock(struct clk *clk)
2055 if (clk->usecount == 0) {
2057 ret = clk_enable_nolock(clk->parent);
2063 ret = clk->mode(clk, 1);
2066 clk_disable_nolock(clk->parent);
2070 pr_debug("%s enabled\n", clk->name);
2077 int clk_enable(struct clk *clk)
2081 if (clk == NULL || IS_ERR(clk))
2085 ret = clk_enable_nolock(clk);
2090 EXPORT_SYMBOL(clk_enable);
2092 static void clk_disable_nolock(struct clk *clk)
2094 if (clk->usecount == 0) {
2095 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n", clk->name);
2100 if (--clk->usecount == 0) {
2103 pr_debug("%s disabled\n", clk->name);
2105 clk_disable_nolock(clk->parent);
2109 void clk_disable(struct clk *clk)
2111 if (clk == NULL || IS_ERR(clk))
2115 clk_disable_nolock(clk);
2118 EXPORT_SYMBOL(clk_disable);
2120 unsigned long clk_get_rate(struct clk *clk)
2122 if (clk == NULL || IS_ERR(clk))
2127 EXPORT_SYMBOL(clk_get_rate);
2129 /*-------------------------------------------------------------------------
2130 * Optional clock functions defined in include/linux/clk.h
2131 *-------------------------------------------------------------------------*/
2133 /* Given a clock and a rate apply a clock specific rounding function */
2134 static long clk_round_rate_nolock(struct clk *clk, unsigned long rate)
2136 if (clk->round_rate)
2137 return clk->round_rate(clk, rate);
2139 if (clk->flags & RATE_FIXED)
2140 printk(KERN_ERR "clock: clk_round_rate called on fixed-rate clock %s\n", clk->name);
2145 long clk_round_rate(struct clk *clk, unsigned long rate)
2149 if (clk == NULL || IS_ERR(clk))
2153 ret = clk_round_rate_nolock(clk, rate);
2158 EXPORT_SYMBOL(clk_round_rate);
2160 static void __clk_recalc(struct clk *clk)
2162 if (unlikely(clk->flags & RATE_FIXED))
2165 clk->rate = clk->recalc(clk);
2166 else if (clk->parent)
2167 clk->rate = clk->parent->rate;
2168 pr_debug("%s new clock rate is %lu\n", clk->name, clk->rate);
2171 static int clk_set_rate_nolock(struct clk *clk, unsigned long rate)
2175 if (rate == clk->rate)
2178 pr_debug("set_rate for clock %s to rate %ld\n", clk->name, rate);
2180 if (clk->flags & CONFIG_PARTICIPANT)
2186 ret = clk->set_rate(clk, rate);
2190 __propagate_rate(clk);
2196 /* Set the clock rate for a clock source */
2197 int clk_set_rate(struct clk *clk, unsigned long rate)
2201 if (clk == NULL || IS_ERR(clk))
2205 ret = clk_set_rate_nolock(clk, rate);
2210 EXPORT_SYMBOL(clk_set_rate);
2212 static int clk_set_parent_nolock(struct clk *clk, struct clk *parent)
2215 int enabled = clk->usecount > 0;
2216 struct clk *old_parent = clk->parent;
2218 if (clk->parent == parent)
2221 /* if clk is already enabled, enable new parent first and disable old parent later. */
2223 clk_enable_nolock(parent);
2225 if (clk->set_parent)
2226 ret = clk->set_parent(clk, parent);
2228 ret = clksel_set_parent(clk, parent);
2232 __clk_reparent(clk, parent);
2234 __propagate_rate(clk);
2236 clk_disable_nolock(old_parent);
2239 clk_disable_nolock(parent);
2245 int clk_set_parent(struct clk *clk, struct clk *parent)
2249 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
2252 if (clk->set_parent == NULL && clk->parents == NULL)
2256 if (clk->usecount == 0)
2257 ret = clk_set_parent_nolock(clk, parent);
2264 EXPORT_SYMBOL(clk_set_parent);
2266 struct clk *clk_get_parent(struct clk *clk)
2270 EXPORT_SYMBOL(clk_get_parent);
2272 static void __clk_reparent(struct clk *child, struct clk *parent)
2274 if (child->parent == parent)
2276 pr_debug("%s reparent to %s (was %s)\n", child->name, parent->name, ((child->parent) ? child->parent->name : "NULL"));
2278 list_del_init(&child->sibling);
2280 list_add(&child->sibling, &parent->children);
2281 child->parent = parent;
2284 /* Propagate rate to children */
2285 static void __propagate_rate(struct clk *tclk)
2289 list_for_each_entry(clkp, &tclk->children, sibling) {
2291 __propagate_rate(clkp);
2295 static LIST_HEAD(root_clks);
2298 * recalculate_root_clocks - recalculate and propagate all root clocks
2300 * Recalculates all root clocks (clocks with no parent), which if the
2301 * clock's .recalc is set correctly, should also propagate their rates.
2304 static void clk_recalculate_root_clocks_nolock(void)
2308 list_for_each_entry(clkp, &root_clks, sibling) {
2310 __propagate_rate(clkp);
2314 void clk_recalculate_root_clocks(void)
2317 clk_recalculate_root_clocks_nolock();
2323 * clk_preinit - initialize any fields in the struct clk before clk init
2324 * @clk: struct clk * to initialize
2326 * Initialize any struct clk fields needed before normal clk initialization
2327 * can run. No return value.
2329 static void clk_preinit(struct clk *clk)
2331 INIT_LIST_HEAD(&clk->children);
2334 static int clk_register(struct clk *clk)
2336 if (clk == NULL || IS_ERR(clk))
2340 * trap out already registered clocks
2342 if (clk->node.next || clk->node.prev)
2345 mutex_lock(&clocks_mutex);
2347 if (clk->get_parent)
2348 clk->parent = clk->get_parent(clk);
2349 else if (clk->parents)
2350 clk->parent = clksel_get_parent(clk);
2353 list_add(&clk->sibling, &clk->parent->children);
2355 list_add(&clk->sibling, &root_clks);
2357 list_add(&clk->node, &clocks);
2359 mutex_unlock(&clocks_mutex);
2364 static unsigned int __initdata armclk = 300 * MHZ;
2367 * You can override arm_clk rate with armclk= cmdline option.
2369 static int __init armclk_setup(char *str)
2371 get_option(&str, &armclk);
2379 clk_set_rate_nolock(&arm_pll_clk, armclk);
2382 early_param("armclk", armclk_setup);
2384 static void __init rk29_clock_common_init(unsigned long ppll_rate)
2386 unsigned long aclk_p, hclk_p, pclk_p;
2387 struct clk *aclk_vepu_parent, *aclk_vdpu_parent, *aclk_gpu_parent;
2388 unsigned long aclk_vepu_rate, hclk_vepu_rate, aclk_ddr_vepu_rate, aclk_gpu_rate;
2389 unsigned long codec_pll = 552 * MHZ;
2392 switch (ppll_rate) {
2397 aclk_gpu_parent = aclk_vdpu_parent = aclk_vepu_parent = &codec_pll_clk;
2398 aclk_gpu_rate = aclk_ddr_vepu_rate = aclk_vepu_rate = codec_pll / 2;
2399 hclk_vepu_rate = codec_pll / 4;
2405 aclk_gpu_parent = aclk_vdpu_parent = aclk_vepu_parent = &codec_pll_clk;
2406 aclk_gpu_rate = aclk_ddr_vepu_rate = aclk_vepu_rate = codec_pll / 2;
2407 hclk_vepu_rate = codec_pll / 4;
2410 ppll_rate = 288 * MHZ;
2413 aclk_p = ppll_rate / 2;
2414 hclk_p = ppll_rate / 2;
2415 pclk_p = ppll_rate / 8;
2416 aclk_gpu_parent = aclk_vdpu_parent = aclk_vepu_parent = &general_pll_clk;
2417 aclk_gpu_rate = aclk_ddr_vepu_rate = aclk_vepu_rate = ppll_rate;
2418 hclk_vepu_rate = ppll_rate / 2;
2422 clk_set_rate_nolock(&general_pll_clk, ppll_rate);
2423 clk_set_parent_nolock(&aclk_periph, &general_pll_clk);
2424 clk_set_rate_nolock(&aclk_periph, aclk_p);
2425 clk_set_rate_nolock(&hclk_periph, hclk_p);
2426 clk_set_rate_nolock(&pclk_periph, pclk_p);
2427 clk_set_parent_nolock(&clk_uhost, &general_pll_clk);
2428 clk_set_rate_nolock(&clk_uhost, 48 * MHZ);
2429 clk_set_parent_nolock(&clk_i2s0_div, &general_pll_clk);
2430 clk_set_parent_nolock(&clk_i2s1_div, &general_pll_clk);
2431 clk_set_parent_nolock(&clk_spdif_div, &general_pll_clk);
2432 clk_set_parent_nolock(&clk_spi_src, &general_pll_clk);
2433 clk_set_parent_nolock(&clk_mmc_src, &general_pll_clk);
2434 clk_set_parent_nolock(&clk_uart01_src, &general_pll_clk);
2435 clk_set_parent_nolock(&clk_uart23_src, &general_pll_clk);
2436 clk_set_parent_nolock(&dclk_lcdc_div, &general_pll_clk);
2437 clk_set_parent_nolock(&aclk_lcdc, &general_pll_clk);
2438 clk_set_parent_nolock(&clk_mac_ref_div, &general_pll_clk);
2439 clk_set_parent_nolock(&clk_hsadc_div, &general_pll_clk);
2442 clk_set_rate_nolock(&codec_pll_clk, codec_pll);
2443 clk_set_parent_nolock(&clk_gpu, &codec_pll_clk);
2446 clk_set_rate_nolock(&arm_pll_clk, armclk);
2448 /*you can choose clk parent form codec pll or periph pll for following logic*/
2449 clk_set_parent_nolock(&aclk_vepu, aclk_vepu_parent);
2450 clk_set_rate_nolock(&aclk_vepu, aclk_vepu_rate);
2451 clk_set_rate_nolock(&clk_aclk_ddr_vepu,aclk_ddr_vepu_rate);
2452 clk_set_rate_nolock(&hclk_vepu, hclk_vepu_rate);
2453 clk_set_parent_nolock(&aclk_vdpu, aclk_vdpu_parent);
2454 clk_set_parent_nolock(&aclk_gpu, aclk_gpu_parent);
2455 clk_set_rate_nolock(&aclk_gpu, aclk_gpu_rate);
2458 static void __init clk_enable_init_clocks(void)
2460 clk_enable_nolock(&hclk_cpu);
2461 clk_enable_nolock(&pclk_cpu);
2462 clk_enable_nolock(&hclk_periph);
2463 clk_enable_nolock(&pclk_periph);
2464 clk_enable_nolock(&clk_nandc);
2465 clk_enable_nolock(&clk_aclk_cpu_peri);
2466 clk_enable_nolock(&clk_aclk_ddr_peri);
2467 clk_enable_nolock(&clk_grf);
2468 clk_enable_nolock(&clk_pmu);
2469 clk_enable_nolock(&clk_ddr_cpu);
2470 clk_enable_nolock(&clk_ddr_reg);
2471 clk_enable_nolock(&clk_ddr_phy);
2472 clk_enable_nolock(&clk_gic);
2473 clk_enable_nolock(&clk_dma2);
2474 clk_enable_nolock(&clk_dma1);
2475 clk_enable_nolock(&clk_emem);
2476 clk_enable_nolock(&clk_intmem);
2477 clk_enable_nolock(&clk_ddr);
2478 clk_enable_nolock(&clk_debug);
2479 clk_enable_nolock(&clk_jtag);
2480 clk_enable_nolock(&clk_uart1);
2483 static int __init clk_disable_unused(void)
2487 list_for_each_entry(ck, &clocks, node) {
2488 if (ck->usecount > 0 || ck->mode == NULL || (ck->flags & IS_PD))
2492 clk_enable_nolock(ck);
2493 clk_disable_nolock(ck);
2500 void __init rk29_clock_init(enum periph_pll ppll_rate)
2502 struct clk_lookup *lk;
2504 cru_clkgate3_con_mirror = cru_readl(CRU_CLKGATE3_CON);
2505 cru_softrst0_con_mirror = cru_readl(CRU_SOFTRST0_CON);
2507 for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++)
2508 clk_preinit(lk->clk);
2510 for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++) {
2512 clk_register(lk->clk);
2515 clk_recalculate_root_clocks_nolock();
2518 * Only enable those clocks we will need, let the drivers
2519 * enable other clocks as necessary
2521 clk_enable_init_clocks();
2524 * Disable any unused clocks left on by the bootloader
2526 clk_disable_unused();
2528 rk29_clock_common_init(ppll_rate);
2530 printk(KERN_INFO "Clocking rate (apll/dpll/cpll/gpll/core/aclk_cpu/hclk_cpu/pclk_cpu/aclk_periph/hclk_periph/pclk_periph): %ld/%ld/%ld/%ld/%ld/%ld/%ld/%ld/%ld/%ld/%ld MHz\n",
2531 arm_pll_clk.rate / MHZ, ddr_pll_clk.rate / MHZ, codec_pll_clk.rate / MHZ, general_pll_clk.rate / MHZ, clk_core.rate / MHZ,
2532 aclk_cpu.rate / MHZ, hclk_cpu.rate / MHZ, pclk_cpu.rate / MHZ, aclk_periph.rate / MHZ, hclk_periph.rate / MHZ, pclk_periph.rate / MHZ);
2535 #ifdef CONFIG_PROC_FS
2536 #include <linux/proc_fs.h>
2537 #include <linux/seq_file.h>
2539 static void dump_clock(struct seq_file *s, struct clk *clk, int deep)
2543 unsigned long rate = clk->rate;
2545 for (i = 0; i < deep; i++)
2548 seq_printf(s, "%-11s ", clk->name);
2550 if (clk->flags & IS_PD) {
2551 seq_printf(s, "%s ", pmu_power_domain_is_on(clk->gate_idx) ? "on " : "off");
2554 if ((clk->mode == gate_mode) && (clk->gate_idx < CLK_GATE_MAX)) {
2556 int idx = clk->gate_idx;
2559 reg = CRU_CLKGATE0_CON;
2560 reg += (idx >> 5) << 2;
2563 if (reg == CRU_CLKGATE3_CON)
2564 v = cru_clkgate3_con_mirror & (1 << idx);
2566 v = cru_readl(reg) & (1 << idx);
2568 seq_printf(s, "%s ", v ? "off" : "on ");
2571 if (clk == &arm_pll_clk) {
2572 switch (cru_readl(CRU_MODE_CON) & CRU_CPU_MODE_MASK) {
2573 case CRU_CPU_MODE_SLOW: seq_printf(s, "slow "); break;
2574 case CRU_CPU_MODE_NORMAL: seq_printf(s, "normal "); break;
2575 case CRU_CPU_MODE_SLOW27: seq_printf(s, "slow27 "); break;
2577 if (cru_readl(CRU_APLL_CON) & PLL_BYPASS) seq_printf(s, "bypass ");
2578 } else if (clk == &ddr_pll_clk) {
2579 switch (cru_readl(CRU_MODE_CON) & CRU_DDR_MODE_MASK) {
2580 case CRU_DDR_MODE_SLOW: seq_printf(s, "slow "); break;
2581 case CRU_DDR_MODE_NORMAL: seq_printf(s, "normal "); break;
2582 case CRU_DDR_MODE_SLOW27: seq_printf(s, "slow27 "); break;
2584 if (cru_readl(CRU_DPLL_CON) & PLL_BYPASS) seq_printf(s, "bypass ");
2585 } else if (clk == &codec_pll_clk) {
2586 switch (cru_readl(CRU_MODE_CON) & CRU_CODEC_MODE_MASK) {
2587 case CRU_CODEC_MODE_SLOW: seq_printf(s, "slow "); break;
2588 case CRU_CODEC_MODE_NORMAL: seq_printf(s, "normal "); break;
2589 case CRU_CODEC_MODE_SLOW27: seq_printf(s, "slow27 "); break;
2591 if (cru_readl(CRU_CPLL_CON) & PLL_BYPASS) seq_printf(s, "bypass ");
2592 } else if (clk == &general_pll_clk) {
2593 switch (cru_readl(CRU_MODE_CON) & CRU_GENERAL_MODE_MASK) {
2594 case CRU_GENERAL_MODE_SLOW: seq_printf(s, "slow "); break;
2595 case CRU_GENERAL_MODE_NORMAL: seq_printf(s, "normal "); break;
2596 case CRU_GENERAL_MODE_SLOW27: seq_printf(s, "slow27 "); break;
2598 if (cru_readl(CRU_GPLL_CON) & PLL_BYPASS) seq_printf(s, "bypass ");
2603 seq_printf(s, "%ld.%06ld MHz", rate / MHZ, rate % MHZ);
2605 seq_printf(s, "%ld MHz", rate / MHZ);
2606 } else if (rate >= KHZ) {
2608 seq_printf(s, "%ld.%03ld KHz", rate / KHZ, rate % KHZ);
2610 seq_printf(s, "%ld KHz", rate / KHZ);
2612 seq_printf(s, "%ld Hz", rate);
2615 seq_printf(s, " usecount = %d", clk->usecount);
2618 seq_printf(s, " parent = %s", clk->parent->name);
2620 seq_printf(s, "\n");
2622 list_for_each_entry(ck, &clocks, node) {
2623 if (ck->parent == clk)
2624 dump_clock(s, ck, deep + 1);
2628 static int proc_clk_show(struct seq_file *s, void *v)
2632 mutex_lock(&clocks_mutex);
2633 list_for_each_entry(clk, &clocks, node) {
2635 dump_clock(s, clk, 0);
2637 mutex_unlock(&clocks_mutex);
2639 seq_printf(s, "\nCRU Registers:\n");
2640 seq_printf(s, "APLL : 0x%08x\n", cru_readl(CRU_APLL_CON));
2641 seq_printf(s, "DPLL : 0x%08x\n", cru_readl(CRU_DPLL_CON));
2642 seq_printf(s, "CPLL : 0x%08x\n", cru_readl(CRU_CPLL_CON));
2643 seq_printf(s, "GPLL : 0x%08x\n", cru_readl(CRU_GPLL_CON));
2644 seq_printf(s, "MODE : 0x%08x\n", cru_readl(CRU_MODE_CON));
2645 seq_printf(s, "CLKSEL0 : 0x%08x\n", cru_readl(CRU_CLKSEL0_CON));
2646 seq_printf(s, "CLKSEL1 : 0x%08x\n", cru_readl(CRU_CLKSEL1_CON));
2647 seq_printf(s, "CLKSEL2 : 0x%08x\n", cru_readl(CRU_CLKSEL2_CON));
2648 seq_printf(s, "CLKSEL3 : 0x%08x\n", cru_readl(CRU_CLKSEL3_CON));
2649 seq_printf(s, "CLKSEL4 : 0x%08x\n", cru_readl(CRU_CLKSEL4_CON));
2650 seq_printf(s, "CLKSEL5 : 0x%08x\n", cru_readl(CRU_CLKSEL5_CON));
2651 seq_printf(s, "CLKSEL6 : 0x%08x\n", cru_readl(CRU_CLKSEL6_CON));
2652 seq_printf(s, "CLKSEL7 : 0x%08x\n", cru_readl(CRU_CLKSEL7_CON));
2653 seq_printf(s, "CLKSEL8 : 0x%08x\n", cru_readl(CRU_CLKSEL8_CON));
2654 seq_printf(s, "CLKSEL9 : 0x%08x\n", cru_readl(CRU_CLKSEL9_CON));
2655 seq_printf(s, "CLKSEL10 : 0x%08x\n", cru_readl(CRU_CLKSEL10_CON));
2656 seq_printf(s, "CLKSEL11 : 0x%08x\n", cru_readl(CRU_CLKSEL11_CON));
2657 seq_printf(s, "CLKSEL12 : 0x%08x\n", cru_readl(CRU_CLKSEL12_CON));
2658 seq_printf(s, "CLKSEL13 : 0x%08x\n", cru_readl(CRU_CLKSEL13_CON));
2659 seq_printf(s, "CLKSEL14 : 0x%08x\n", cru_readl(CRU_CLKSEL14_CON));
2660 seq_printf(s, "CLKSEL15 : 0x%08x\n", cru_readl(CRU_CLKSEL15_CON));
2661 seq_printf(s, "CLKSEL16 : 0x%08x\n", cru_readl(CRU_CLKSEL16_CON));
2662 seq_printf(s, "CLKSEL17 : 0x%08x\n", cru_readl(CRU_CLKSEL17_CON));
2663 seq_printf(s, "CLKGATE0 : 0x%08x\n", cru_readl(CRU_CLKGATE0_CON));
2664 seq_printf(s, "CLKGATE1 : 0x%08x\n", cru_readl(CRU_CLKGATE1_CON));
2665 seq_printf(s, "CLKGATE2 : 0x%08x\n", cru_readl(CRU_CLKGATE2_CON));
2666 seq_printf(s, "CLKGATE3 : 0x%08x\n", cru_readl(CRU_CLKGATE3_CON));
2667 seq_printf(s, "CLKGATE3M: 0x%08x\n", cru_clkgate3_con_mirror);
2668 seq_printf(s, "SOFTRST0 : 0x%08x\n", cru_readl(CRU_SOFTRST0_CON));
2669 seq_printf(s, "SOFTRST0M: 0x%08x\n", cru_softrst0_con_mirror);
2670 seq_printf(s, "SOFTRST1 : 0x%08x\n", cru_readl(CRU_SOFTRST1_CON));
2671 seq_printf(s, "SOFTRST2 : 0x%08x\n", cru_readl(CRU_SOFTRST2_CON));
2673 seq_printf(s, "\nPMU Registers:\n");
2674 seq_printf(s, "WAKEUP_EN0 : 0x%08x\n", pmu_readl(PMU_WAKEUP_EN0));
2675 seq_printf(s, "WAKEUP_EN1 : 0x%08x\n", pmu_readl(PMU_WAKEUP_EN1));
2676 seq_printf(s, "WAKEUP_EN2 : 0x%08x\n", pmu_readl(PMU_WAKEUP_EN2));
2677 seq_printf(s, "PD_CON : 0x%08x\n", pmu_readl(PMU_PD_CON));
2678 seq_printf(s, "MISC_CON : 0x%08x\n", pmu_readl(PMU_MISC_CON));
2679 seq_printf(s, "PLL_CNT : 0x%08x\n", pmu_readl(PMU_PLL_CNT));
2680 seq_printf(s, "PD_ST : 0x%08x\n", pmu_readl(PMU_PD_ST));
2681 seq_printf(s, "INT_ST : 0x%08x\n", pmu_readl(PMU_INT_ST));
2686 static int proc_clk_open(struct inode *inode, struct file *file)
2688 return single_open(file, proc_clk_show, NULL);
2691 static const struct file_operations proc_clk_fops = {
2692 .open = proc_clk_open,
2694 .llseek = seq_lseek,
2695 .release = single_release,
2698 static int __init clk_proc_init(void)
2700 proc_create("clocks", 0, NULL, &proc_clk_fops);
2704 late_initcall(clk_proc_init);
2705 #endif /* CONFIG_PROC_FS */