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>
35 #define PLL_HIGH_BAND (0x01 << 16)
36 #define PLL_LOW_BAND (0x00 << 16)
37 #define PLL_PD (0x01 << 15)
39 #define PLL_CLKR(i) ((((i) - 1) & 0x1f) << 10)
40 #define PLL_NR(v) ((((v) >> 10) & 0x1f) + 1)
42 #define PLL_CLKF(i) ((((i) - 1) & 0x7f) << 3)
43 #define PLL_NF(v) ((((v) >> 3) & 0x7f) + 1)
44 #define PLL_NF2(v) (((((v) >> 3) & 0x7f) + 1) << 1)
46 #define PLL_CLKOD(i) (((i) & 0x03) << 1)
47 #define PLL_NO_1 PLL_CLKOD(0)
48 #define PLL_NO_2 PLL_CLKOD(1)
49 #define PLL_NO_4 PLL_CLKOD(2)
50 #define PLL_NO_8 PLL_CLKOD(3)
51 #define PLL_NO_SHIFT(v) (((v) >> 1) & 0x03)
53 #define PLL_BYPASS (0x01)
56 #define CRU_CPU_MODE_MASK (0x03u << 0)
57 #define CRU_CPU_MODE_SLOW (0x00u << 0)
58 #define CRU_CPU_MODE_NORMAL (0x01u << 0)
59 #define CRU_CPU_MODE_DSLOW (0x02u << 0)
61 #define CRU_PERIPH_MODE_MASK (0x03u << 2)
62 #define CRU_PERIPH_MODE_SLOW (0x00u << 2)
63 #define CRU_PERIPH_MODE_NORMAL (0x01u << 2)
64 #define CRU_PERIPH_MODE_DSLOW (0x02u << 2)
66 #define CRU_CODEC_MODE_MASK (0x03u << 4)
67 #define CRU_CODEC_MODE_SLOW (0x00u << 4)
68 #define CRU_CODEC_MODE_NORMAL (0x01u << 4)
69 #define CRU_CODEC_MODE_DSLOW (0x02u << 4)
71 #define CRU_DDR_MODE_MASK (0x03u << 6)
72 #define CRU_DDR_MODE_SLOW (0x00u << 6)
73 #define CRU_DDR_MODE_NORMAL (0x01u << 6)
74 #define CRU_DDR_MODE_DSLOW (0x02u << 6)
78 #define RATE_FIXED (1 << 1) /* Fixed clock rate */
79 #define CONFIG_PARTICIPANT (1 << 10) /* Fundamental clock */
80 #define ENABLE_ON_INIT (1 << 11) /* Enable upon framework init */
82 #define cru_readl(offset) readl(RK29_CRU_BASE + offset)
83 #define cru_writel(v, offset) writel(v, RK29_CRU_BASE + offset)
84 #define cru_writel_force(v, offset) do { u32 _v = v; u32 _count = 5; do { cru_writel(_v, offset); } while (cru_readl(offset) != _v && _count--); } while (0) /* huangtao: when write CRU_xPLL_CON, first time may failed, so try again. unknown why. */
86 #define regfile_readl(offset) readl(RK29_GRF_BASE + offset)
88 #define MHZ (1000*1000)
92 struct list_head node;
95 struct list_head children;
96 struct list_head sibling; /* node for children */
99 int (*mode)(struct clk *clk, int on);
100 unsigned long (*recalc)(struct clk *); /* if null, follow parent */
101 int (*set_rate)(struct clk *, unsigned long);
102 long (*round_rate)(struct clk *, unsigned long);
103 struct clk* (*get_parent)(struct clk *); /* get clk's parent from the hardware. default is clksel_get_parent if parents present */
104 int (*set_parent)(struct clk *, struct clk *); /* default is clksel_set_parent if parents present */
112 u8 clksel_parent_mask;
113 u8 clksel_parent_shift;
114 struct clk **parents;
117 static int clk_enable_nolock(struct clk *clk);
118 static void clk_disable_nolock(struct clk *clk);
119 static int clk_set_rate_nolock(struct clk *clk, unsigned long rate);
120 static int clk_set_parent_nolock(struct clk *clk, struct clk *parent);
121 static void __clk_reparent(struct clk *child, struct clk *parent);
122 static void __propagate_rate(struct clk *tclk);
124 static unsigned long clksel_recalc_div(struct clk *clk)
126 u32 div = ((cru_readl(clk->clksel_con) >> clk->clksel_shift) & clk->clksel_mask) + 1;
127 unsigned long rate = clk->parent->rate / div;
128 pr_debug("%s new clock rate is %lu (div %u)\n", clk->name, rate, div);
132 static unsigned long clksel_recalc_shift(struct clk *clk)
134 u32 shift = (cru_readl(clk->clksel_con) >> clk->clksel_shift) & clk->clksel_mask;
135 unsigned long rate = clk->parent->rate >> shift;
136 pr_debug("%s new clock rate is %lu (shift %u)\n", clk->name, rate, shift);
140 static unsigned long clksel_recalc_frac(struct clk *clk)
144 u32 r = cru_readl(clk->clksel_con), numerator, denominator;
145 if (r == 0) // FPGA ?
146 return clk->parent->rate;
148 denominator = r & 0xFFFF;
149 rate64 = (u64)clk->parent->rate * numerator;
150 do_div(rate64, denominator);
152 pr_debug("%s new clock rate is %lu (frac %u/%u)\n", clk->name, rate, numerator, denominator);
156 static int clksel_set_rate_div(struct clk *clk, unsigned long rate)
160 for (div = 0; div <= clk->clksel_mask; div++) {
161 u32 new_rate = clk->parent->rate / (div + 1);
162 if (new_rate <= rate) {
163 u32 v = cru_readl(clk->clksel_con);
164 v &= ~((u32) clk->clksel_mask << clk->clksel_shift);
165 v |= div << clk->clksel_shift;
166 cru_writel(v, clk->clksel_con);
167 clk->rate = new_rate;
168 pr_debug("clksel_set_rate_div for clock %s to rate %ld (div %d)\n", clk->name, rate, div + 1);
176 static int clksel_set_rate_shift(struct clk *clk, unsigned long rate)
180 for (shift = 0; (1 << shift) <= clk->clksel_maxdiv; shift++) {
181 u32 new_rate = clk->parent->rate >> shift;
182 if (new_rate <= rate) {
183 u32 v = cru_readl(clk->clksel_con);
184 v &= ~((u32) clk->clksel_mask << clk->clksel_shift);
185 v |= shift << clk->clksel_shift;
186 cru_writel(v, clk->clksel_con);
187 clk->rate = new_rate;
188 pr_debug("clksel_set_rate_shift for clock %s to rate %ld (shift %d)\n", clk->name, rate, shift);
196 static struct clk* clksel_get_parent(struct clk *clk)
198 return clk->parents[(cru_readl(clk->clksel_con) >> clk->clksel_parent_shift) & clk->clksel_parent_mask];
201 static int clksel_set_parent(struct clk *clk, struct clk *parent)
203 struct clk **p = clk->parents;
208 for (i = 0; (i <= clk->clksel_parent_mask) && *p; i++, p++) {
212 v = cru_readl(clk->clksel_con);
213 v &= ~((u32) clk->clksel_parent_mask << clk->clksel_parent_shift);
214 v |= (i << clk->clksel_parent_shift);
215 cru_writel(v, clk->clksel_con);
221 static int gate_mode(struct clk *clk, int on)
224 int idx = clk->gate_idx;
227 if (idx >= CLK_GATE_MAX)
230 reg = CRU_CLKGATE0_CON;
231 reg += (idx >> 5) << 2;
236 v &= ~(1 << idx); // clear bit
238 v |= (1 << idx); // set bit
245 static struct clk xin24m = {
251 static struct clk clk_12m = {
258 static struct clk extclk = {
264 static struct clk otgphy0_clkin = {
265 .name = "otgphy0_clkin",
270 static struct clk otgphy1_clkin = {
271 .name = "otgphy1_clkin",
277 static void delay_500ns(void)
285 #define PERIPH_PLL_IDX 0
286 #define CODEC_PLL_IDX 1
287 #define ARM_PLL_IDX 2
288 #define DDR_PLL_IDX 3
290 #define GRF_SOC_CON0 0xbc
291 static void pll_wait_lock(int pll_idx, int delay)
293 u32 bit = 0x2000000u << pll_idx;
295 if (regfile_readl(GRF_SOC_CON0) & bit)
300 pr_warning("wait pll bit 0x%x time out!\n", bit);
304 static unsigned long arm_pll_clk_recalc(struct clk *clk)
308 if ((cru_readl(CRU_MODE_CON) & CRU_CPU_MODE_MASK) == CRU_CPU_MODE_NORMAL) {
309 u32 v = cru_readl(CRU_APLL_CON);
310 u64 rate64 = (u64) clk->parent->rate * PLL_NF2(v);
311 do_div(rate64, PLL_NR(v));
312 rate = rate64 >> PLL_NO_SHIFT(v);
313 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));
315 rate = clk->parent->rate;
316 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
328 #define CORE_ACLK_11 (0 << 5)
329 #define CORE_ACLK_21 (1 << 5)
330 #define CORE_ACLK_31 (2 << 5)
331 #define CORE_ACLK_41 (3 << 5)
332 #define CORE_ACLK_81 (4 << 5)
333 #define CORE_ACLK_MASK (7 << 5)
335 #define ACLK_HCLK_11 (0 << 8)
336 #define ACLK_HCLK_21 (1 << 8)
337 #define ACLK_HCLK_41 (2 << 8)
338 #define ACLK_HCLK_MASK (3 << 8)
340 #define ACLK_PCLK_11 (0 << 10)
341 #define ACLK_PCLK_21 (1 << 10)
342 #define ACLK_PCLK_41 (2 << 10)
343 #define ACLK_PCLK_81 (3 << 10)
344 #define ACLK_PCLK_MASK (3 << 10)
346 #define ARM_PLL(_clk_mhz, nr, nf, no, _axi_div, _ahb_div, _apb_div) \
348 .clk_hz = _clk_mhz * MHZ, \
349 .pll_con = PLL_CLKR(nr) | PLL_CLKF(nf >> 1) | PLL_NO_##no, \
350 .clksel0_con = CORE_ACLK_##_axi_div | ACLK_HCLK_##_ahb_div | ACLK_PCLK_##_apb_div, \
353 static const struct arm_pll_set arm_pll[] = {
354 // clk_mhz = 24 * NF / (NR * NO)
355 // mhz NR NF NO adiv hdiv pdiv
356 // ARM_PLL(600, 1, 50, 2, 21, 21, 41),
357 // ARM_PLL(624, 1, 52, 2, 21, 21, 41),
358 ARM_PLL(720, 1, 60, 2, 21, 21, 41),
359 // last item, pll power down.
360 ARM_PLL( 24, 1, 64, 8, 21, 21, 41),
363 #define CORE_PARENT_MASK (3 << 23)
364 #define CORE_PARENT_ARM_PLL (0 << 23)
365 #define CORE_PARENT_PERIPH_PLL (1 << 23)
367 static int arm_pll_clk_set_rate(struct clk *clk, unsigned long rate)
369 u32 v = arm_pll[0].pll_con;
371 /* make aclk safe & reparent to periph pll */
372 cru_writel((cru_readl(CRU_CLKSEL0_CON) & ~(CORE_PARENT_MASK | CORE_ACLK_MASK)) | CORE_PARENT_PERIPH_PLL | CORE_ACLK_21, CRU_CLKSEL0_CON);
374 /* enter slow mode */
375 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CPU_MODE_MASK) | CRU_CPU_MODE_SLOW, CRU_MODE_CON);
377 pll_wait_lock(ARM_PLL_IDX, 2400000);
380 cru_writel(cru_readl(CRU_APLL_CON) | PLL_PD, CRU_APLL_CON);
384 cru_writel(v | PLL_PD, CRU_APLL_CON);
389 cru_writel(v, CRU_APLL_CON);
391 pll_wait_lock(ARM_PLL_IDX, 2400000);
393 /* enter normal mode */
394 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CPU_MODE_MASK) | CRU_CPU_MODE_NORMAL, CRU_MODE_CON);
396 /* reparent to arm pll & set aclk/hclk/pclk */
397 cru_writel((cru_readl(CRU_CLKSEL0_CON) & ~(CORE_PARENT_MASK | CORE_ACLK_MASK | ACLK_HCLK_MASK | ACLK_PCLK_MASK)) | CORE_PARENT_ARM_PLL | arm_pll[0].clksel0_con, CRU_CLKSEL0_CON);
402 static struct clk arm_pll_clk = {
405 .recalc = arm_pll_clk_recalc,
406 .set_rate = arm_pll_clk_set_rate,
409 static unsigned long ddr_pll_clk_recalc(struct clk *clk)
413 if ((cru_readl(CRU_MODE_CON) & CRU_DDR_MODE_MASK) == CRU_DDR_MODE_NORMAL) {
414 u32 v = cru_readl(CRU_DPLL_CON);
415 u64 rate64 = (u64) clk->parent->rate * PLL_NF(v);
416 do_div(rate64, PLL_NR(v));
417 rate = rate64 >> PLL_NO_SHIFT(v);
418 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));
420 rate = clk->parent->rate;
421 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
427 static struct clk ddr_pll_clk = {
430 .recalc = ddr_pll_clk_recalc,
434 static unsigned long codec_pll_clk_recalc(struct clk *clk)
438 if ((cru_readl(CRU_MODE_CON) & CRU_CODEC_MODE_MASK) == CRU_CODEC_MODE_NORMAL) {
439 u32 v = cru_readl(CRU_CPLL_CON);
440 u64 rate64 = (u64) clk->parent->rate * PLL_NF(v);
441 do_div(rate64, PLL_NR(v));
442 rate = rate64 >> PLL_NO_SHIFT(v);
443 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));
445 rate = clk->parent->rate;
446 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
452 static struct clk codec_pll_clk = {
455 .recalc = codec_pll_clk_recalc,
459 static unsigned long periph_pll_clk_recalc(struct clk *clk)
463 if ((cru_readl(CRU_MODE_CON) & CRU_PERIPH_MODE_MASK) == CRU_PERIPH_MODE_NORMAL) {
464 u32 v = cru_readl(CRU_PPLL_CON);
465 u64 rate64 = (u64) clk->parent->rate * PLL_NF(v);
466 do_div(rate64, PLL_NR(v));
467 rate = rate64 >> PLL_NO_SHIFT(v);
468 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));
470 rate = clk->parent->rate;
471 pr_debug("%s new clock rate is %ld (slow mode)\n", clk->name, rate);
477 static int periph_pll_clk_set_rate(struct clk *clk, unsigned long rate)
479 /* 624M: high-band, NR=1, NF=26, NO=1 */
480 u32 v = PLL_HIGH_BAND | PLL_CLKR(1) | PLL_CLKF(26) | PLL_NO_1;
482 /* enter slow mode */
483 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_PERIPH_MODE_MASK) | CRU_PERIPH_MODE_SLOW, CRU_MODE_CON);
485 pll_wait_lock(PERIPH_PLL_IDX, 2400000);
488 cru_writel(cru_readl(CRU_PPLL_CON) | PLL_PD, CRU_PPLL_CON);
492 cru_writel(v | PLL_PD, CRU_PPLL_CON);
497 cru_writel(v, CRU_PPLL_CON);
499 pll_wait_lock(PERIPH_PLL_IDX, 2400000);
501 /* enter normal mode */
502 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_PERIPH_MODE_MASK) | CRU_PERIPH_MODE_NORMAL, CRU_MODE_CON);
507 static struct clk periph_pll_clk = {
508 .name = "periph_pll",
510 .recalc = periph_pll_clk_recalc,
511 .set_rate = periph_pll_clk_set_rate,
515 static struct clk clk_core = {
517 .parent = &arm_pll_clk,
518 .recalc = clksel_recalc_div,
519 .clksel_con = CRU_CLKSEL0_CON,
524 static unsigned long aclk_cpu_recalc(struct clk *clk)
527 u32 div = ((cru_readl(CRU_CLKSEL0_CON) >> 5) & 0x7) + 1;
532 rate = clk->parent->rate / div;
533 pr_debug("%s new clock rate is %ld (div %d)\n", clk->name, rate, div);
538 static struct clk aclk_cpu = {
541 .recalc = aclk_cpu_recalc,
544 static struct clk hclk_cpu = {
547 .recalc = clksel_recalc_shift,
548 .clksel_con = CRU_CLKSEL0_CON,
554 static struct clk pclk_cpu = {
557 .recalc = clksel_recalc_shift,
558 .clksel_con = CRU_CLKSEL0_CON,
564 static struct clk aclk_periph = {
565 .name = "aclk_periph",
567 .gate_idx = CLK_GATE_PEIRPH_AXI,
568 .parent = &periph_pll_clk,
569 .recalc = clksel_recalc_div,
570 .clksel_con = CRU_CLKSEL0_CON,
575 static struct clk pclk_periph = {
576 .name = "pclk_periph",
578 .gate_idx = CLK_GATE_PEIRPH_APB,
579 .parent = &aclk_periph,
580 .recalc = clksel_recalc_shift,
581 .clksel_con = CRU_CLKSEL0_CON,
587 static struct clk hclk_periph = {
588 .name = "hclk_periph",
590 .gate_idx = CLK_GATE_PEIRPH_AHB,
591 .parent = &aclk_periph,
592 .recalc = clksel_recalc_shift,
593 .clksel_con = CRU_CLKSEL0_CON,
600 static struct clk *clk_uhost_parents[8] = { &periph_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
602 static struct clk clk_uhost = {
605 .recalc = clksel_recalc_div,
606 .set_rate = clksel_set_rate_div,
607 .gate_idx = CLK_GATE_UHOST,
608 .clksel_con = CRU_CLKSEL1_CON,
611 .clksel_parent_mask = 7,
612 .clksel_parent_shift = 13,
613 .parents = clk_uhost_parents,
616 static struct clk *clk_otgphy_parents[4] = { &xin24m, &clk_12m, &clk_uhost };
618 static struct clk clk_otgphy0 = {
621 .gate_idx = CLK_GATE_USBPHY0,
622 .clksel_con = CRU_CLKSEL1_CON,
623 .clksel_parent_mask = 3,
624 .clksel_parent_shift = 9,
625 .parents = clk_otgphy_parents,
628 static struct clk clk_otgphy1 = {
631 .gate_idx = CLK_GATE_USBPHY1,
632 .clksel_con = CRU_CLKSEL1_CON,
633 .clksel_parent_mask = 3,
634 .clksel_parent_shift = 11,
635 .parents = clk_otgphy_parents,
639 static struct clk rmii_clkin = {
640 .name = "rmii_clkin",
643 static struct clk *clk_mac_ref_div_parents[4] = { &arm_pll_clk, &periph_pll_clk, &codec_pll_clk, &ddr_pll_clk };
645 static struct clk clk_mac_ref_div = {
646 .name = "mac_ref_div",
647 .recalc = clksel_recalc_div,
648 .set_rate = clksel_set_rate_div,
649 .clksel_con = CRU_CLKSEL1_CON,
652 .clksel_parent_mask = 3,
653 .clksel_parent_shift = 21,
654 .parents = clk_mac_ref_div_parents,
657 static struct clk *clk_mac_ref_parents[2] = { &clk_mac_ref_div, &rmii_clkin };
659 static struct clk clk_mac_ref = {
662 .gate_idx = CLK_GATE_MAC_PHY,
663 .clksel_con = CRU_CLKSEL1_CON,
664 .clksel_parent_mask = 1,
665 .clksel_parent_shift = 28,
666 .parents = clk_mac_ref_parents,
670 static struct clk *clk_i2s_div_parents[8] = { &codec_pll_clk, &periph_pll_clk, &arm_pll_clk, &ddr_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
672 static struct clk clk_i2s0_div = {
674 .recalc = clksel_recalc_div,
675 .set_rate = clksel_set_rate_div,
676 .clksel_con = CRU_CLKSEL2_CON,
679 .clksel_parent_mask = 7,
680 .clksel_parent_shift = 0,
681 .parents = clk_i2s_div_parents,
684 static struct clk clk_i2s1_div = {
686 .recalc = clksel_recalc_div,
687 .set_rate = clksel_set_rate_div,
688 .clksel_con = CRU_CLKSEL2_CON,
691 .clksel_parent_mask = 7,
692 .clksel_parent_shift = 10,
693 .parents = clk_i2s_div_parents,
696 static struct clk clk_spdif_div = {
698 .recalc = clksel_recalc_div,
699 .set_rate = clksel_set_rate_div,
700 .clksel_con = CRU_CLKSEL2_CON,
703 .clksel_parent_mask = 7,
704 .clksel_parent_shift = 20,
705 .parents = clk_i2s_div_parents,
708 static int clk_i2s_frac_div_set_rate(struct clk *clk, unsigned long rate)
710 u16 numerator, denominator;
713 case 8192000: /* 624*128/9750 */
717 case 11289600: /* 624*294/16250 */
721 case 12288000: /* 624*64/3250 */
725 case 22579200: /* 624*294/8125 */
729 case 24576000: /* 624*64/1625 */
733 case 45158400: /* 624*588/8125 */
737 case 49152000: /* 624*128/1625 */
745 pr_debug("set clock %s to rate %ld (%d/%d)\n", clk->name, rate, numerator, denominator);
746 cru_writel((u32)numerator << 16 | denominator, clk->clksel_con);
751 static struct clk clk_i2s0_frac_div = {
752 .name = "i2s0_frac_div",
753 .parent = &clk_i2s0_div,
754 .recalc = clksel_recalc_frac,
755 .set_rate = clk_i2s_frac_div_set_rate,
756 .clksel_con = CRU_CLKSEL3_CON,
759 static struct clk clk_i2s1_frac_div = {
760 .name = "i2s1_frac_div",
761 .parent = &clk_i2s1_div,
762 .recalc = clksel_recalc_frac,
763 .set_rate = clk_i2s_frac_div_set_rate,
764 .clksel_con = CRU_CLKSEL4_CON,
767 static struct clk clk_spdif_frac_div = {
768 .name = "spdif_frac_div",
769 .parent = &clk_spdif_div,
770 .recalc = clksel_recalc_frac,
771 .set_rate = clk_i2s_frac_div_set_rate,
772 .clksel_con = CRU_CLKSEL5_CON,
775 static int i2s_set_rate(struct clk *clk, unsigned long rate)
780 if (rate == 12 * MHZ) {
783 parent = clk->parents[1]; /* frac div */
784 ret = clk_set_rate_nolock(parent, rate);
788 if (clk->parent != parent)
789 clk_set_parent_nolock(clk, parent);
794 static struct clk *clk_i2s0_parents[4] = { &clk_i2s0_div, &clk_i2s0_frac_div, &clk_12m, &xin24m };
796 static struct clk clk_i2s0 = {
799 .gate_idx = CLK_GATE_I2S0,
800 .set_rate = i2s_set_rate,
801 .clksel_con = CRU_CLKSEL2_CON,
802 .clksel_parent_mask = 3,
803 .clksel_parent_shift = 8,
804 .parents = clk_i2s0_parents,
807 static struct clk *clk_i2s1_parents[4] = { &clk_i2s1_div, &clk_i2s1_frac_div, &clk_12m, &xin24m };
809 static struct clk clk_i2s1 = {
812 .gate_idx = CLK_GATE_I2S1,
813 .set_rate = i2s_set_rate,
814 .clksel_con = CRU_CLKSEL2_CON,
815 .clksel_parent_mask = 3,
816 .clksel_parent_shift = 18,
817 .parents = clk_i2s1_parents,
820 static struct clk *clk_spdif_parents[4] = { &clk_spdif_div, &clk_spdif_frac_div, &clk_12m, &xin24m };
822 static struct clk clk_spdif = {
825 .gate_idx = CLK_GATE_SPDIF,
826 .set_rate = i2s_set_rate,
827 .clksel_con = CRU_CLKSEL2_CON,
828 .clksel_parent_mask = 3,
829 .clksel_parent_shift = 28,
830 .parents = clk_spdif_parents,
834 static struct clk *clk_spi_src_parents[4] = { &periph_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk };
836 static struct clk clk_spi_src = {
838 .clksel_con = CRU_CLKSEL6_CON,
839 .clksel_parent_mask = 3,
840 .clksel_parent_shift = 0,
841 .parents = clk_spi_src_parents,
844 static struct clk clk_spi0 = {
846 .parent = &clk_spi_src,
848 .recalc = clksel_recalc_div,
849 .set_rate = clksel_set_rate_div,
850 .gate_idx = CLK_GATE_SPI0,
851 .clksel_con = CRU_CLKSEL6_CON,
856 static struct clk clk_spi1 = {
858 .parent = &clk_spi_src,
860 .recalc = clksel_recalc_div,
861 .set_rate = clksel_set_rate_div,
862 .gate_idx = CLK_GATE_SPI1,
863 .clksel_con = CRU_CLKSEL6_CON,
869 static struct clk clk_saradc = {
871 .parent = &pclk_periph,
873 .recalc = clksel_recalc_div,
874 .set_rate = clksel_set_rate_div,
875 .gate_idx = CLK_GATE_SARADC,
876 .clksel_con = CRU_CLKSEL6_CON,
882 static struct clk *clk_cpu_timer_parents[2] = { &pclk_cpu, &xin24m };
884 static struct clk clk_timer0 = {
887 .gate_idx = CLK_GATE_TIMER0,
888 .clksel_con = CRU_CLKSEL6_CON,
889 .clksel_parent_mask = 1,
890 .clksel_parent_shift = 26,
891 .parents = clk_cpu_timer_parents,
894 static struct clk clk_timer1 = {
897 .gate_idx = CLK_GATE_TIMER1,
898 .clksel_con = CRU_CLKSEL6_CON,
899 .clksel_parent_mask = 1,
900 .clksel_parent_shift = 27,
901 .parents = clk_cpu_timer_parents,
904 static struct clk *clk_periph_timer_parents[2] = { &pclk_periph, &xin24m };
906 static struct clk clk_timer2 = {
909 .gate_idx = CLK_GATE_TIMER2,
910 .clksel_con = CRU_CLKSEL6_CON,
911 .clksel_parent_mask = 1,
912 .clksel_parent_shift = 28,
913 .parents = clk_periph_timer_parents,
916 static struct clk clk_timer3 = {
919 .gate_idx = CLK_GATE_TIMER3,
920 .clksel_con = CRU_CLKSEL6_CON,
921 .clksel_parent_mask = 1,
922 .clksel_parent_shift = 29,
923 .parents = clk_periph_timer_parents,
927 static struct clk *clk_sdmmc_src_parents[4] = { &arm_pll_clk, &periph_pll_clk, &codec_pll_clk, &ddr_pll_clk };
929 static struct clk clk_sdmmc_src = {
931 .clksel_con = CRU_CLKSEL7_CON,
932 .clksel_parent_mask = 3,
933 .clksel_parent_shift = 0,
934 .parents = clk_sdmmc_src_parents,
937 static struct clk clk_sdmmc0 = {
939 .parent = &clk_sdmmc_src,
941 .recalc = clksel_recalc_div,
942 .set_rate = clksel_set_rate_div,
943 .gate_idx = CLK_GATE_SDMMC0,
944 .clksel_con = CRU_CLKSEL7_CON,
949 static struct clk clk_sdmmc1 = {
951 .parent = &clk_sdmmc_src,
953 .recalc = clksel_recalc_div,
954 .set_rate = clksel_set_rate_div,
955 .gate_idx = CLK_GATE_SDMMC1,
956 .clksel_con = CRU_CLKSEL7_CON,
961 static struct clk clk_emmc = {
963 .parent = &clk_sdmmc_src,
965 .recalc = clksel_recalc_div,
966 .set_rate = clksel_set_rate_div,
967 .gate_idx = CLK_GATE_EMMC,
968 .clksel_con = CRU_CLKSEL7_CON,
974 static struct clk *clk_ddr_parents[8] = { &ddr_pll_clk, &periph_pll_clk, &codec_pll_clk, &arm_pll_clk };
976 static struct clk clk_ddr = {
978 .recalc = clksel_recalc_shift,
979 .clksel_con = CRU_CLKSEL7_CON,
983 .clksel_parent_mask = 3,
984 .clksel_parent_shift = 24,
985 .parents = clk_ddr_parents,
989 static int clk_uart_set_rate(struct clk *clk, unsigned long rate)
993 struct clk *clk_div = clk->parents[0];
996 case 24*MHZ: /* 1.5M/0.5M/50/75/150/200/300/600/1200/2400 */
997 parent = clk->parents[2]; /* xin24m */
1009 parent = clk->parents[1]; /* frac div */
1010 /* reset div to 1 */
1011 ret = clk_set_rate_nolock(clk_div, clk_div->parent->rate);
1020 if (parent->set_rate) {
1021 ret = clk_set_rate_nolock(parent, rate);
1026 if (clk->parent != parent)
1027 clk_set_parent_nolock(clk, parent);
1032 static int clk_uart_frac_div_set_rate(struct clk *clk, unsigned long rate)
1034 u16 numerator, denominator;
1081 pr_debug("set clock %s to rate %ld (%d/%d)\n", clk->name, rate, numerator, denominator);
1082 cru_writel((u32)numerator << 16 | denominator, clk->clksel_con);
1086 static struct clk *clk_uart_src_parents[8] = { &periph_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
1088 static struct clk clk_uart01_src = {
1089 .name = "uart01_src",
1090 .clksel_con = CRU_CLKSEL8_CON,
1091 .clksel_parent_mask = 7,
1092 .clksel_parent_shift = 0,
1093 .parents = clk_uart_src_parents,
1096 static struct clk clk_uart0_div = {
1097 .name = "uart0_div",
1098 .parent = &clk_uart01_src,
1099 .recalc = clksel_recalc_div,
1100 .set_rate = clksel_set_rate_div,
1101 .clksel_con = CRU_CLKSEL8_CON,
1102 .clksel_mask = 0x3F,
1106 static struct clk clk_uart0_frac_div = {
1107 .name = "uart0_frac_div",
1108 .parent = &clk_uart0_div,
1109 .recalc = clksel_recalc_frac,
1110 .set_rate = clk_uart_frac_div_set_rate,
1111 .clksel_con = CRU_CLKSEL10_CON,
1114 static struct clk *clk_uart0_parents[4] = { &clk_uart0_div, &clk_uart0_frac_div, &xin24m };
1116 static struct clk clk_uart0 = {
1119 .set_rate = clk_uart_set_rate,
1120 .gate_idx = CLK_GATE_UART0,
1121 .clksel_con = CRU_CLKSEL8_CON,
1122 .clksel_parent_mask = 3,
1123 .clksel_parent_shift = 9,
1124 .parents = clk_uart0_parents,
1127 static struct clk clk_uart1_div = {
1128 .name = "uart1_div",
1129 .parent = &clk_uart01_src,
1130 .recalc = clksel_recalc_div,
1131 .set_rate = clksel_set_rate_div,
1132 .clksel_con = CRU_CLKSEL8_CON,
1133 .clksel_mask = 0x3F,
1137 static struct clk clk_uart1_frac_div = {
1138 .name = "uart1_frac_div",
1139 .parent = &clk_uart1_div,
1140 .recalc = clksel_recalc_frac,
1141 .set_rate = clk_uart_frac_div_set_rate,
1142 .clksel_con = CRU_CLKSEL11_CON,
1145 static struct clk *clk_uart1_parents[4] = { &clk_uart1_div, &clk_uart1_frac_div, &xin24m };
1147 static struct clk clk_uart1 = {
1150 .set_rate = clk_uart_set_rate,
1151 .gate_idx = CLK_GATE_UART1,
1152 .clksel_con = CRU_CLKSEL8_CON,
1153 .clksel_parent_mask = 3,
1154 .clksel_parent_shift = 20,
1155 .parents = clk_uart1_parents,
1158 static struct clk clk_uart23_src = {
1159 .name = "uart23_src",
1160 .clksel_con = CRU_CLKSEL9_CON,
1161 .clksel_parent_mask = 7,
1162 .clksel_parent_shift = 0,
1163 .parents = clk_uart_src_parents,
1166 static struct clk clk_uart2_div = {
1167 .name = "uart2_div",
1168 .parent = &clk_uart23_src,
1169 .recalc = clksel_recalc_div,
1170 .set_rate = clksel_set_rate_div,
1171 .clksel_con = CRU_CLKSEL9_CON,
1172 .clksel_mask = 0x3F,
1176 static struct clk clk_uart2_frac_div = {
1177 .name = "uart2_frac_div",
1178 .parent = &clk_uart2_div,
1179 .recalc = clksel_recalc_frac,
1180 .set_rate = clk_uart_frac_div_set_rate,
1181 .clksel_con = CRU_CLKSEL12_CON,
1184 static struct clk *clk_uart2_parents[4] = { &clk_uart2_div, &clk_uart2_frac_div, &xin24m };
1186 static struct clk clk_uart2 = {
1189 .set_rate = clk_uart_set_rate,
1190 .gate_idx = CLK_GATE_UART2,
1191 .clksel_con = CRU_CLKSEL9_CON,
1192 .clksel_parent_mask = 3,
1193 .clksel_parent_shift = 9,
1194 .parents = clk_uart2_parents,
1197 static struct clk clk_uart3_div = {
1198 .name = "uart3_div",
1199 .parent = &clk_uart23_src,
1200 .recalc = clksel_recalc_div,
1201 .set_rate = clksel_set_rate_div,
1202 .clksel_con = CRU_CLKSEL9_CON,
1203 .clksel_mask = 0x3F,
1207 static struct clk clk_uart3_frac_div = {
1208 .name = "uart3_frac_div",
1209 .parent = &clk_uart3_div,
1210 .recalc = clksel_recalc_frac,
1211 .set_rate = clk_uart_frac_div_set_rate,
1212 .clksel_con = CRU_CLKSEL13_CON,
1215 static struct clk *clk_uart3_parents[4] = { &clk_uart3_div, &clk_uart3_frac_div, &xin24m };
1217 static struct clk clk_uart3 = {
1220 .set_rate = clk_uart_set_rate,
1221 .gate_idx = CLK_GATE_UART3,
1222 .clksel_con = CRU_CLKSEL9_CON,
1223 .clksel_parent_mask = 3,
1224 .clksel_parent_shift = 20,
1225 .parents = clk_uart3_parents,
1229 static struct clk *clk_hsadc_div_parents[8] = { &codec_pll_clk, &ddr_pll_clk, &periph_pll_clk, &arm_pll_clk, &otgphy0_clkin, &otgphy1_clkin };
1231 static struct clk clk_hsadc_div = {
1232 .name = "hsadc_div",
1233 .recalc = clksel_recalc_div,
1234 .set_rate = clksel_set_rate_div,
1235 .clksel_con = CRU_CLKSEL14_CON,
1236 .clksel_mask = 0xFF,
1238 .clksel_parent_mask = 7,
1239 .clksel_parent_shift = 7,
1240 .parents = clk_hsadc_div_parents,
1243 static struct clk clk_hsadc_frac_div = {
1244 .name = "hsadc_frac_div",
1245 .parent = &clk_hsadc_div,
1246 .recalc = clksel_recalc_frac,
1247 .clksel_con = CRU_CLKSEL15_CON,
1250 static struct clk *clk_demod_parents[4] = { &clk_hsadc_div, &clk_hsadc_frac_div, &extclk };
1252 static struct clk clk_demod = {
1254 .clksel_con = CRU_CLKSEL14_CON,
1255 .clksel_parent_mask = 3,
1256 .clksel_parent_shift = 18,
1257 .parents = clk_demod_parents,
1260 static struct clk gpsclk = {
1264 static struct clk *clk_hsadc_parents[2] = { &clk_demod, &gpsclk };
1266 static struct clk clk_hsadc = {
1269 .gate_idx = CLK_GATE_HSADC,
1270 .clksel_con = CRU_CLKSEL14_CON,
1271 .clksel_parent_mask = 1,
1272 .clksel_parent_shift = 21,
1273 .parents = clk_hsadc_parents,
1276 static unsigned long div2_recalc(struct clk *clk)
1278 return clk->parent->rate >> 1;
1281 static struct clk clk_hsadc_div2 = {
1282 .name = "hsadc_div2",
1283 .parent = &clk_demod,
1284 .recalc = div2_recalc,
1287 static struct clk clk_hsadc_div2_inv = {
1288 .name = "hsadc_div2_inv",
1289 .parent = &clk_demod,
1290 .recalc = div2_recalc,
1293 static struct clk *clk_hsadc_out_parents[2] = { &clk_hsadc_div2, &clk_hsadc_div2_inv };
1295 static struct clk clk_hsadc_out = {
1296 .name = "hsadc_out",
1297 .clksel_con = CRU_CLKSEL14_CON,
1298 .clksel_parent_mask = 1,
1299 .clksel_parent_shift = 20,
1300 .parents = clk_hsadc_out_parents,
1304 static struct clk *dclk_lcdc_div_parents[4] = { &codec_pll_clk, &ddr_pll_clk, &periph_pll_clk, &arm_pll_clk };
1306 static struct clk dclk_lcdc_div = {
1307 .name = "dclk_lcdc_div",
1308 .recalc = clksel_recalc_div,
1309 .set_rate = clksel_set_rate_div,
1310 .clksel_con = CRU_CLKSEL16_CON,
1311 .clksel_mask = 0xFF,
1313 .clksel_parent_mask = 3,
1314 .clksel_parent_shift = 0,
1315 .parents = dclk_lcdc_div_parents,
1318 static struct clk *dclk_lcdc_parents[2] = { &dclk_lcdc_div, &extclk };
1320 static struct clk dclk_lcdc = {
1321 .name = "dclk_lcdc",
1323 .gate_idx = CLK_GATE_LCDC,
1324 .clksel_con = CRU_CLKSEL16_CON,
1325 .clksel_parent_mask = 1,
1326 .clksel_parent_shift = 10,
1327 .parents = dclk_lcdc_parents,
1330 static struct clk dclk_ebook = {
1331 .name = "dclk_ebook",
1333 .gate_idx = CLK_GATE_EBOOK,
1334 .recalc = clksel_recalc_div,
1335 .set_rate = clksel_set_rate_div,
1336 .clksel_con = CRU_CLKSEL16_CON,
1337 .clksel_mask = 0x1F,
1339 .clksel_parent_mask = 3,
1340 .clksel_parent_shift = 11,
1341 .parents = dclk_lcdc_div_parents,
1344 static struct clk *aclk_lcdc_parents[4] = { &ddr_pll_clk, &codec_pll_clk, &periph_pll_clk, &arm_pll_clk };
1346 static struct clk aclk_lcdc = {
1347 .name = "aclk_lcdc",
1349 .gate_idx = CLK_GATE_LCDC_AXI,
1350 .recalc = clksel_recalc_div,
1351 .set_rate = clksel_set_rate_div,
1352 .clksel_con = CRU_CLKSEL16_CON,
1353 .clksel_mask = 0x1F,
1355 .clksel_parent_mask = 3,
1356 .clksel_parent_shift = 18,
1357 .parents = aclk_lcdc_parents,
1360 static struct clk hclk_lcdc = {
1361 .name = "hclk_lcdc",
1363 .gate_idx = CLK_GATE_LCDC_AHB,
1364 .parent = &aclk_lcdc,
1365 .clksel_con = CRU_CLKSEL16_CON,
1366 .recalc = clksel_recalc_shift,
1367 .set_rate = clksel_set_rate_shift,
1373 static struct clk *xpu_parents[4] = { &periph_pll_clk, &ddr_pll_clk, &codec_pll_clk, &arm_pll_clk };
1375 static struct clk aclk_vepu = {
1376 .name = "aclk_vepu",
1378 .recalc = clksel_recalc_div,
1379 .set_rate = clksel_set_rate_div,
1380 .gate_idx = CLK_GAET_VEPU_AXI,
1381 .clksel_con = CRU_CLKSEL17_CON,
1382 .clksel_mask = 0x1F,
1384 .clksel_parent_mask = 3,
1385 .clksel_parent_shift = 0,
1386 .parents = xpu_parents,
1389 static struct clk hclk_vepu = {
1390 .name = "hclk_vepu",
1391 .parent = &aclk_vepu,
1393 .recalc = clksel_recalc_shift,
1394 .set_rate = clksel_set_rate_shift,
1395 .gate_idx = CLK_GATE_VEPU_AHB,
1396 .clksel_con = CRU_CLKSEL17_CON,
1402 static struct clk aclk_vdpu = {
1403 .name = "aclk_vdpu",
1404 .parent = &periph_pll_clk,
1406 .recalc = clksel_recalc_div,
1407 .set_rate = clksel_set_rate_div,
1408 .gate_idx = CLK_GATE_VDPU_AXI,
1409 .clksel_con = CRU_CLKSEL17_CON,
1410 .clksel_mask = 0x1F,
1412 .clksel_parent_mask = 3,
1413 .clksel_parent_shift = 7,
1414 .parents = xpu_parents,
1417 static struct clk hclk_vdpu = {
1418 .name = "hclk_vdpu",
1419 .parent = &aclk_vdpu,
1421 .recalc = clksel_recalc_shift,
1422 .set_rate = clksel_set_rate_shift,
1423 .gate_idx = CLK_GATE_VDPU_AHB,
1424 .clksel_con = CRU_CLKSEL17_CON,
1430 static struct clk clk_gpu = {
1433 .gate_idx = CLK_GATE_GPU,
1434 .recalc = clksel_recalc_div,
1435 .set_rate = clksel_set_rate_div,
1436 .clksel_con = CRU_CLKSEL17_CON,
1437 .clksel_mask = 0x1F,
1439 .clksel_parent_mask = 3,
1440 .clksel_parent_shift = 14,
1441 .parents = xpu_parents,
1444 static struct clk aclk_gpu = {
1447 .gate_idx = CLK_GATE_GPU_AXI,
1448 .recalc = clksel_recalc_div,
1449 .set_rate = clksel_set_rate_div,
1450 .clksel_con = CRU_CLKSEL17_CON,
1451 .clksel_mask = 0x1F,
1453 .clksel_parent_mask = 3,
1454 .clksel_parent_shift = 21,
1455 .parents = xpu_parents,
1459 static struct clk *clk_vip_parents[4] = { &xin24m, &extclk, &dclk_ebook };
1461 static struct clk clk_vip = {
1464 .gate_idx = CLK_GATE_VIP,
1465 .clksel_con = CRU_CLKSEL1_CON,
1466 .clksel_parent_mask = 3,
1467 .clksel_parent_shift = 7,
1468 .parents = clk_vip_parents,
1472 #define GATE_CLK(NAME,PARENT,ID) \
1473 static struct clk clk_##NAME = { \
1475 .parent = &PARENT, \
1476 .mode = gate_mode, \
1477 .gate_idx = CLK_GATE_##ID, \
1480 GATE_CLK(i2c0, pclk_cpu, I2C0);
1481 GATE_CLK(i2c1, pclk_periph, I2C1);
1482 GATE_CLK(i2c2, pclk_periph, I2C2);
1483 GATE_CLK(i2c3, pclk_periph, I2C3);
1485 GATE_CLK(gpio0, pclk_cpu, GPIO0);
1486 GATE_CLK(gpio1, pclk_periph, GPIO1);
1487 GATE_CLK(gpio2, pclk_periph, GPIO2);
1488 GATE_CLK(gpio3, pclk_periph, GPIO3);
1489 GATE_CLK(gpio4, pclk_cpu, GPIO4);
1490 GATE_CLK(gpio5, pclk_periph, GPIO5);
1491 GATE_CLK(gpio6, pclk_cpu, GPIO6);
1493 GATE_CLK(dma0, aclk_cpu, DMA0);
1494 GATE_CLK(dma1, aclk_periph, DMA1);
1495 GATE_CLK(dma2, aclk_periph, DMA2);
1497 GATE_CLK(gic, aclk_cpu, GIC);
1498 GATE_CLK(imem, aclk_cpu, IMEM);
1499 GATE_CLK(ebrom, hclk_cpu, EBROM);
1500 GATE_CLK(ddr_phy, aclk_cpu, DDR_PHY);
1501 GATE_CLK(ddr_reg, aclk_cpu, DDR_REG);
1502 GATE_CLK(ddr_cpu, aclk_cpu, DDR_CPU);
1503 GATE_CLK(efuse, pclk_cpu, EFUSE);
1504 GATE_CLK(tzpc, pclk_cpu, TZPC);
1505 GATE_CLK(debug, pclk_cpu, DEBUG);
1506 GATE_CLK(tpiu, pclk_cpu, TPIU);
1507 GATE_CLK(rtc, pclk_cpu, RTC);
1508 GATE_CLK(pmu, pclk_cpu, PMU);
1509 GATE_CLK(grf, pclk_cpu, GRF);
1511 GATE_CLK(emem, hclk_periph, EMEM);
1512 GATE_CLK(usb, hclk_periph, USB);
1513 GATE_CLK(ddr_periph, aclk_periph, DDR_PERIPH);
1514 GATE_CLK(periph_cpu, aclk_cpu, PERIPH_CPU);
1515 GATE_CLK(smc_axi, aclk_periph, SMC_AXI);
1516 GATE_CLK(smc, pclk_periph, SMC);
1517 GATE_CLK(mac_ahb, hclk_periph, MAC_AHB);
1518 GATE_CLK(mac_tx, hclk_periph, MAC_TX);
1519 GATE_CLK(mac_rx, hclk_periph, MAC_RX);
1520 GATE_CLK(hif, hclk_periph, HIF);
1521 GATE_CLK(nandc, hclk_periph, NANDC);
1522 GATE_CLK(hsadc_ahb, hclk_periph, HSADC_AHB);
1523 GATE_CLK(usbotg0, hclk_periph, USBOTG0);
1524 GATE_CLK(usbotg1, hclk_periph, USBOTG1);
1525 GATE_CLK(uhost_ahb, hclk_periph, UHOST_AHB);
1526 GATE_CLK(pid_filter, hclk_periph, PID_FILTER);
1528 GATE_CLK(vip_slave, hclk_cpu, VIP_SLAVE);
1529 GATE_CLK(wdt, pclk_periph, WDT);
1530 GATE_CLK(pwm, pclk_periph, PWM);
1531 GATE_CLK(vip_bus, aclk_cpu, VIP_BUS);
1532 GATE_CLK(vip_matrix, hclk_cpu, VIP_MATRIX);
1533 GATE_CLK(vip_input, hclk_cpu, VIP_INPUT);
1534 GATE_CLK(jtag, aclk_cpu, JTAG);
1536 GATE_CLK(ddr_lcdc_axi, aclk_cpu, DDR_LCDC_AXI);
1537 GATE_CLK(ipp_axi, aclk_cpu, IPP_AXI);
1538 GATE_CLK(ipp_ahb, hclk_cpu, IPP_AHB);
1539 GATE_CLK(ebook_ahb, hclk_cpu, EBOOK_AHB);
1540 GATE_CLK(display_matrix_axi, aclk_cpu, DISPLAY_MATRIX_AXI);
1541 GATE_CLK(display_matrix_ahb, hclk_cpu, DISPLAY_MATRIX_AHB);
1542 GATE_CLK(ddr_vepu_axi, aclk_cpu, DDR_VEPU_AXI);
1543 GATE_CLK(ddr_vdpu_axi, aclk_cpu, DDR_VDPU_AXI);
1544 GATE_CLK(ddr_gpu_axi, aclk_cpu, DDR_GPU_AXI);
1545 GATE_CLK(gpu_ahb, hclk_cpu, GPU_AHB);
1546 GATE_CLK(cpu_vcodec_ahb, hclk_cpu, CPU_VCODEC_AHB);
1547 GATE_CLK(cpu_display_ahb, hclk_cpu, CPU_DISPLAY_AHB);
1549 GATE_CLK(sdmmc0_ahb, hclk_periph, SDMMC0_AHB);
1550 GATE_CLK(sdmmc1_ahb, hclk_periph, SDMMC1_AHB);
1551 GATE_CLK(emmc_ahb, hclk_periph, EMMC_AHB);
1553 #define CLK(dev, con, ck) \
1560 #define CLK1(name) \
1564 .clk = &clk_##name, \
1567 static struct clk_lookup clks[] = {
1568 CLK(NULL, "xin24m", &xin24m),
1569 CLK(NULL, "extclk", &extclk),
1570 CLK(NULL, "otgphy0_clkin", &otgphy0_clkin),
1571 CLK(NULL, "otgphy1_clkin", &otgphy1_clkin),
1572 CLK(NULL, "gpsclk", &gpsclk),
1575 CLK(NULL, "arm_pll", &arm_pll_clk),
1576 CLK(NULL, "ddr_pll", &ddr_pll_clk),
1577 CLK(NULL, "codec_pll", &codec_pll_clk),
1578 CLK(NULL, "periph_pll", &periph_pll_clk),
1581 CLK(NULL, "aclk_cpu", &aclk_cpu),
1582 CLK(NULL, "hclk_cpu", &hclk_cpu),
1583 CLK(NULL, "pclk_cpu", &pclk_cpu),
1585 CLK(NULL, "aclk_periph", &aclk_periph),
1586 CLK(NULL, "hclk_periph", &hclk_periph),
1587 CLK(NULL, "pclk_periph", &pclk_periph),
1590 CLK("rk29_otgphy.0", "otgphy", &clk_otgphy0),
1591 CLK("rk29_otgphy.1", "otgphy", &clk_otgphy1),
1592 CLK(NULL, "uhost", &clk_uhost),
1593 CLK(NULL, "mac_ref_div", &clk_mac_ref_div),
1594 CLK(NULL, "mac_ref", &clk_mac_ref),
1596 CLK("rk29_i2s.0", "i2s_div", &clk_i2s0_div),
1597 CLK("rk29_i2s.0", "i2s_frac_div", &clk_i2s0_frac_div),
1598 CLK("rk29_i2s.0", "i2s", &clk_i2s0),
1599 CLK("rk29_i2s.1", "i2s_div", &clk_i2s1_div),
1600 CLK("rk29_i2s.1", "i2s_frac_div", &clk_i2s1_frac_div),
1601 CLK("rk29_i2s.1", "i2s", &clk_i2s1),
1602 CLK(NULL, "spdif_div", &clk_spdif_div),
1603 CLK(NULL, "spdif_frac_div", &clk_spdif_frac_div),
1604 CLK(NULL, "spdif", &clk_spdif),
1607 CLK("rk29xx_spim.0", "spi0", &clk_spi0),
1608 CLK("rk29xx_spim.1", "spi1", &clk_spi1),
1617 CLK("rk29_sdmmc.0", "sdmmc", &clk_sdmmc0),
1618 CLK("rk29_sdmmc.0", "sdmmc_ahb", &clk_sdmmc0_ahb),
1619 CLK("rk29_sdmmc.1", "sdmmc", &clk_sdmmc1),
1620 CLK("rk29_sdmmc.1", "sdmmc_ahb", &clk_sdmmc1_ahb),
1626 CLK("rk29_serial.0", "uart", &clk_uart0),
1627 CLK("rk29_serial.0", "uart_div", &clk_uart0_div),
1628 CLK("rk29_serial.0", "uart_frac_div", &clk_uart0_frac_div),
1629 CLK("rk29_serial.1", "uart", &clk_uart1),
1630 CLK("rk29_serial.1", "uart_div", &clk_uart1_div),
1631 CLK("rk29_serial.1", "uart_frac_div", &clk_uart1_frac_div),
1634 CLK("rk29_serial.2", "uart", &clk_uart2),
1635 CLK("rk29_serial.2", "uart_div", &clk_uart2_div),
1636 CLK("rk29_serial.2", "uart_frac_div", &clk_uart2_frac_div),
1637 CLK("rk29_serial.3", "uart", &clk_uart3),
1638 CLK("rk29_serial.3", "uart_div", &clk_uart3_div),
1639 CLK("rk29_serial.3", "uart_frac_div", &clk_uart3_frac_div),
1642 CLK1(hsadc_frac_div),
1646 CLK1(hsadc_div2_inv),
1649 CLK(NULL, "dclk_lcdc_div", &dclk_lcdc_div),
1650 CLK(NULL, "dclk_lcdc", &dclk_lcdc),
1651 CLK(NULL, "dclk_ebook", &dclk_ebook),
1652 CLK(NULL, "aclk_lcdc", &aclk_lcdc),
1653 CLK(NULL, "hclk_lcdc", &hclk_lcdc),
1655 CLK(NULL, "aclk_vepu", &aclk_vepu),
1656 CLK(NULL, "hclk_vepu", &hclk_vepu),
1657 CLK(NULL, "aclk_vdpu", &aclk_vdpu),
1658 CLK(NULL, "hclk_vdpu", &hclk_vdpu),
1660 CLK(NULL, "aclk_gpu", &aclk_gpu),
1662 CLK("rk29_i2c.0", "i2c", &clk_i2c0),
1663 CLK("rk29_i2c.1", "i2c", &clk_i2c1),
1664 CLK("rk29_i2c.2", "i2c", &clk_i2c2),
1665 CLK("rk29_i2c.3", "i2c", &clk_i2c3),
1722 CLK1(display_matrix_axi),
1723 CLK1(display_matrix_ahb),
1728 CLK1(cpu_vcodec_ahb),
1729 CLK1(cpu_display_ahb),
1732 static LIST_HEAD(clocks);
1733 static DEFINE_MUTEX(clocks_mutex);
1734 static DEFINE_SPINLOCK(clockfw_lock);
1735 #define LOCK() do { WARN_ON(in_irq()); if (!irqs_disabled()) spin_lock_bh(&clockfw_lock); } while (0)
1736 #define UNLOCK() do { if (!irqs_disabled()) spin_unlock_bh(&clockfw_lock); } while (0)
1738 static int clk_enable_nolock(struct clk *clk)
1742 if (clk->usecount == 0) {
1744 ret = clk_enable_nolock(clk->parent);
1750 ret = clk->mode(clk, 1);
1753 clk_disable_nolock(clk->parent);
1757 pr_debug("%s enabled\n", clk->name);
1764 int clk_enable(struct clk *clk)
1768 if (clk == NULL || IS_ERR(clk))
1772 ret = clk_enable_nolock(clk);
1777 EXPORT_SYMBOL(clk_enable);
1779 static void clk_disable_nolock(struct clk *clk)
1781 if (clk->usecount == 0) {
1782 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n", clk->name);
1787 if (--clk->usecount == 0) {
1790 pr_debug("%s disabled\n", clk->name);
1792 clk_disable_nolock(clk->parent);
1796 void clk_disable(struct clk *clk)
1798 if (clk == NULL || IS_ERR(clk))
1802 clk_disable_nolock(clk);
1805 EXPORT_SYMBOL(clk_disable);
1807 unsigned long clk_get_rate(struct clk *clk)
1809 if (clk == NULL || IS_ERR(clk))
1814 EXPORT_SYMBOL(clk_get_rate);
1816 /*-------------------------------------------------------------------------
1817 * Optional clock functions defined in include/linux/clk.h
1818 *-------------------------------------------------------------------------*/
1820 /* Given a clock and a rate apply a clock specific rounding function */
1821 static long clk_round_rate_nolock(struct clk *clk, unsigned long rate)
1823 if (clk->round_rate)
1824 return clk->round_rate(clk, rate);
1826 if (clk->flags & RATE_FIXED)
1827 printk(KERN_ERR "clock: clk_round_rate called on fixed-rate clock %s\n", clk->name);
1832 long clk_round_rate(struct clk *clk, unsigned long rate)
1836 if (clk == NULL || IS_ERR(clk))
1840 ret = clk_round_rate_nolock(clk, rate);
1845 EXPORT_SYMBOL(clk_round_rate);
1847 static void __clk_recalc(struct clk *clk)
1849 if (unlikely(clk->flags & RATE_FIXED))
1852 clk->rate = clk->recalc(clk);
1853 else if (clk->parent)
1854 clk->rate = clk->parent->rate;
1855 pr_debug("%s new clock rate is %lu\n", clk->name, clk->rate);
1858 static int clk_set_rate_nolock(struct clk *clk, unsigned long rate)
1862 if (rate == clk->rate)
1865 pr_debug("set_rate for clock %s to rate %ld\n", clk->name, rate);
1867 if (clk->flags & CONFIG_PARTICIPANT)
1873 ret = clk->set_rate(clk, rate);
1877 __propagate_rate(clk);
1883 /* Set the clock rate for a clock source */
1884 int clk_set_rate(struct clk *clk, unsigned long rate)
1888 if (clk == NULL || IS_ERR(clk))
1892 ret = clk_set_rate_nolock(clk, rate);
1897 EXPORT_SYMBOL(clk_set_rate);
1899 static int clk_set_parent_nolock(struct clk *clk, struct clk *parent)
1902 int enabled = clk->usecount > 0;
1903 struct clk *old_parent = clk->parent;
1905 if (clk->parent == parent)
1908 /* if clk is already enabled, enable new parent first and disable old parent later. */
1910 clk_enable_nolock(parent);
1912 if (clk->set_parent)
1913 ret = clk->set_parent(clk, parent);
1915 ret = clksel_set_parent(clk, parent);
1919 __clk_reparent(clk, parent);
1921 __propagate_rate(clk);
1923 clk_disable_nolock(old_parent);
1926 clk_disable_nolock(parent);
1932 int clk_set_parent(struct clk *clk, struct clk *parent)
1936 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
1939 if (clk->set_parent == NULL && clk->parents == NULL)
1943 if (clk->usecount == 0)
1944 ret = clk_set_parent_nolock(clk, parent);
1951 EXPORT_SYMBOL(clk_set_parent);
1953 struct clk *clk_get_parent(struct clk *clk)
1957 EXPORT_SYMBOL(clk_get_parent);
1959 static void __clk_reparent(struct clk *child, struct clk *parent)
1961 if (child->parent == parent)
1963 pr_debug("%s reparent to %s (was %s)\n", child->name, parent->name, ((child->parent) ? child->parent->name : "NULL"));
1965 list_del_init(&child->sibling);
1967 list_add(&child->sibling, &parent->children);
1968 child->parent = parent;
1971 /* Propagate rate to children */
1972 static void __propagate_rate(struct clk *tclk)
1976 list_for_each_entry(clkp, &tclk->children, sibling) {
1978 __propagate_rate(clkp);
1982 static LIST_HEAD(root_clks);
1985 * recalculate_root_clocks - recalculate and propagate all root clocks
1987 * Recalculates all root clocks (clocks with no parent), which if the
1988 * clock's .recalc is set correctly, should also propagate their rates.
1991 static void clk_recalculate_root_clocks_nolock(void)
1995 list_for_each_entry(clkp, &root_clks, sibling) {
1997 __propagate_rate(clkp);
2001 void clk_recalculate_root_clocks(void)
2004 clk_recalculate_root_clocks_nolock();
2010 * clk_preinit - initialize any fields in the struct clk before clk init
2011 * @clk: struct clk * to initialize
2013 * Initialize any struct clk fields needed before normal clk initialization
2014 * can run. No return value.
2016 static void clk_preinit(struct clk *clk)
2018 INIT_LIST_HEAD(&clk->children);
2021 static int clk_register(struct clk *clk)
2023 if (clk == NULL || IS_ERR(clk))
2027 * trap out already registered clocks
2029 if (clk->node.next || clk->node.prev)
2032 mutex_lock(&clocks_mutex);
2034 if (clk->get_parent)
2035 clk->parent = clk->get_parent(clk);
2036 else if (clk->parents)
2037 clk->parent = clksel_get_parent(clk);
2040 list_add(&clk->sibling, &clk->parent->children);
2042 list_add(&clk->sibling, &root_clks);
2044 list_add(&clk->node, &clocks);
2046 mutex_unlock(&clocks_mutex);
2051 static void clk_enable_init_clocks(void)
2055 list_for_each_entry(clkp, &clocks, node) {
2056 if (clkp->flags & ENABLE_ON_INIT)
2061 static void rk29_clock_common_init(void)
2064 clk_set_rate_nolock(&periph_pll_clk, 624 * MHZ);
2065 clk_set_parent_nolock(&aclk_periph, &periph_pll_clk); // default
2066 clk_set_rate_nolock(&aclk_periph, 312 * MHZ); // default
2067 clk_set_rate_nolock(&hclk_periph, 156 * MHZ); // default
2068 clk_set_rate_nolock(&pclk_periph, 78 * MHZ); // default
2069 clk_set_parent_nolock(&clk_uhost, &periph_pll_clk); // default
2070 clk_set_rate_nolock(&clk_uhost, 48 * MHZ);
2071 clk_set_parent_nolock(&clk_i2s0_div, &periph_pll_clk); // default
2072 clk_set_parent_nolock(&clk_i2s1_div, &periph_pll_clk); // default
2073 clk_set_parent_nolock(&clk_spdif_div, &periph_pll_clk); // default
2074 clk_set_parent_nolock(&clk_spi_src, &periph_pll_clk); // default
2075 clk_set_parent_nolock(&clk_sdmmc_src, &periph_pll_clk);
2076 clk_set_parent_nolock(&clk_uart01_src, &periph_pll_clk); // default
2077 clk_set_parent_nolock(&clk_uart23_src, &periph_pll_clk); // default
2078 clk_set_parent_nolock(&dclk_lcdc_div, &periph_pll_clk);
2079 clk_set_parent_nolock(&aclk_lcdc, &periph_pll_clk);
2080 clk_set_parent_nolock(&aclk_vepu, &periph_pll_clk); // default
2081 clk_set_parent_nolock(&aclk_vdpu, &periph_pll_clk); // default
2082 clk_set_parent_nolock(&clk_gpu, &periph_pll_clk); // default
2083 clk_set_parent_nolock(&aclk_gpu, &periph_pll_clk); // default
2084 clk_set_parent_nolock(&clk_mac_ref_div, &periph_pll_clk);
2085 clk_set_parent_nolock(&clk_hsadc_div, &periph_pll_clk);
2088 clk_set_rate_nolock(&arm_pll_clk, 600 * MHZ);
2091 void __init rk29_clock_init(void)
2093 struct clk_lookup *lk;
2095 for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++)
2096 clk_preinit(lk->clk);
2098 for (lk = clks; lk < clks + ARRAY_SIZE(clks); lk++) {
2100 clk_register(lk->clk);
2103 clk_recalculate_root_clocks_nolock();
2105 rk29_clock_common_init();
2107 printk(KERN_INFO "Clocking rate (apll/dpll/cpll/ppll/core/aclk/hclk/pclk): %ld/%ld/%ld/%ld/%ld/%ld/%ld/%ld MHz\n",
2108 arm_pll_clk.rate / MHZ, ddr_pll_clk.rate / MHZ, codec_pll_clk.rate / MHZ, periph_pll_clk.rate / MHZ,
2109 clk_core.rate / MHZ, aclk_cpu.rate / MHZ, hclk_cpu.rate / MHZ, pclk_cpu.rate / MHZ);
2112 * Only enable those clocks we will need, let the drivers
2113 * enable other clocks as necessary
2115 clk_enable_init_clocks();
2118 #ifdef CONFIG_PROC_FS
2119 #include <linux/proc_fs.h>
2120 #include <linux/seq_file.h>
2122 static void dump_clock(struct seq_file *s, struct clk *clk, int deep)
2126 unsigned long rate = clk->rate;
2128 for (i = 0; i < deep; i++)
2131 seq_printf(s, "%-9s ", clk->name);
2133 if (clk->mode && (clk->gate_idx < CLK_GATE_MAX)) {
2135 int idx = clk->gate_idx;
2138 reg = CRU_CLKGATE0_CON;
2139 reg += (idx >> 5) << 2;
2142 v = cru_readl(reg) & (1 << idx);
2144 seq_printf(s, "%s ", v ? "off" : "on ");
2149 seq_printf(s, "%ld.%06ld MHz", rate / MHZ, rate % MHZ);
2151 seq_printf(s, "%ld MHz", rate / MHZ);
2152 } else if (rate >= KHZ) {
2154 seq_printf(s, "%ld.%03ld KHz", rate / KHZ, rate % KHZ);
2156 seq_printf(s, "%ld KHz", rate / KHZ);
2158 seq_printf(s, "%ld Hz", rate);
2161 seq_printf(s, " usecount = %d", clk->usecount);
2163 seq_printf(s, " parent = %s\n", clk->parent ? clk->parent->name : "NULL");
2165 list_for_each_entry(ck, &clocks, node) {
2166 if (ck->parent == clk)
2167 dump_clock(s, ck, deep + 1);
2171 static int proc_clk_show(struct seq_file *s, void *v)
2175 mutex_lock(&clocks_mutex);
2176 list_for_each_entry(clk, &clocks, node) {
2178 dump_clock(s, clk, 0);
2180 mutex_unlock(&clocks_mutex);
2182 seq_printf(s, "\nRegisters:\n");
2183 seq_printf(s, "APLL : 0x%08x\n", cru_readl(CRU_APLL_CON));
2184 seq_printf(s, "DPLL : 0x%08x\n", cru_readl(CRU_DPLL_CON));
2185 seq_printf(s, "CPLL : 0x%08x\n", cru_readl(CRU_CPLL_CON));
2186 seq_printf(s, "PPLL : 0x%08x\n", cru_readl(CRU_PPLL_CON));
2187 seq_printf(s, "MODE : 0x%08x\n", cru_readl(CRU_MODE_CON));
2188 seq_printf(s, "CLKSEL0 : 0x%08x\n", cru_readl(CRU_CLKSEL0_CON));
2189 seq_printf(s, "CLKSEL1 : 0x%08x\n", cru_readl(CRU_CLKSEL1_CON));
2190 seq_printf(s, "CLKSEL2 : 0x%08x\n", cru_readl(CRU_CLKSEL2_CON));
2191 seq_printf(s, "CLKSEL3 : 0x%08x\n", cru_readl(CRU_CLKSEL3_CON));
2192 seq_printf(s, "CLKSEL4 : 0x%08x\n", cru_readl(CRU_CLKSEL4_CON));
2193 seq_printf(s, "CLKSEL5 : 0x%08x\n", cru_readl(CRU_CLKSEL5_CON));
2194 seq_printf(s, "CLKSEL6 : 0x%08x\n", cru_readl(CRU_CLKSEL6_CON));
2195 seq_printf(s, "CLKSEL7 : 0x%08x\n", cru_readl(CRU_CLKSEL7_CON));
2196 seq_printf(s, "CLKSEL8 : 0x%08x\n", cru_readl(CRU_CLKSEL8_CON));
2197 seq_printf(s, "CLKSEL9 : 0x%08x\n", cru_readl(CRU_CLKSEL9_CON));
2198 seq_printf(s, "CLKSEL10 : 0x%08x\n", cru_readl(CRU_CLKSEL10_CON));
2199 seq_printf(s, "CLKSEL11 : 0x%08x\n", cru_readl(CRU_CLKSEL11_CON));
2200 seq_printf(s, "CLKSEL12 : 0x%08x\n", cru_readl(CRU_CLKSEL12_CON));
2201 seq_printf(s, "CLKSEL13 : 0x%08x\n", cru_readl(CRU_CLKSEL13_CON));
2202 seq_printf(s, "CLKSEL14 : 0x%08x\n", cru_readl(CRU_CLKSEL14_CON));
2203 seq_printf(s, "CLKSEL15 : 0x%08x\n", cru_readl(CRU_CLKSEL15_CON));
2204 seq_printf(s, "CLKSEL16 : 0x%08x\n", cru_readl(CRU_CLKSEL16_CON));
2205 seq_printf(s, "CLKSEL17 : 0x%08x\n", cru_readl(CRU_CLKSEL17_CON));
2206 seq_printf(s, "CLKGATE0 : 0x%08x\n", cru_readl(CRU_CLKGATE0_CON));
2207 seq_printf(s, "CLKGATE1 : 0x%08x\n", cru_readl(CRU_CLKGATE1_CON));
2208 seq_printf(s, "CLKGATE2 : 0x%08x\n", cru_readl(CRU_CLKGATE2_CON));
2209 seq_printf(s, "CLKGATE3 : 0x%08x\n", cru_readl(CRU_CLKGATE3_CON));
2214 static int proc_clk_open(struct inode *inode, struct file *file)
2216 return single_open(file, proc_clk_show, NULL);
2219 static const struct file_operations proc_clk_fops = {
2220 .open = proc_clk_open,
2222 .llseek = seq_lseek,
2223 .release = single_release,
2226 static int __init clk_proc_init(void)
2228 proc_create("clocks", 0, NULL, &proc_clk_fops);
2232 late_initcall(clk_proc_init);
2233 #endif /* CONFIG_PROC_FS */