16 #include <linux/kernel.h>
17 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/list.h>
30 #define JZ_REG_CLOCK_CTRL 0x00
31 #define JZ_REG_CLOCK_LOW_POWER 0x04
32 #define JZ_REG_CLOCK_PLL 0x10
33 #define JZ_REG_CLOCK_GATE 0x20
34 #define JZ_REG_CLOCK_SLEEP_CTRL 0x24
35 #define JZ_REG_CLOCK_I2S 0x60
36 #define JZ_REG_CLOCK_LCD 0x64
37 #define JZ_REG_CLOCK_MMC 0x68
38 #define JZ_REG_CLOCK_UHC 0x6C
39 #define JZ_REG_CLOCK_SPI 0x74
41 #define JZ_CLOCK_CTRL_I2S_SRC_PLL BIT(31)
42 #define JZ_CLOCK_CTRL_KO_ENABLE BIT(30)
43 #define JZ_CLOCK_CTRL_UDC_SRC_PLL BIT(29)
44 #define JZ_CLOCK_CTRL_UDIV_MASK 0x1f800000
45 #define JZ_CLOCK_CTRL_CHANGE_ENABLE BIT(22)
46 #define JZ_CLOCK_CTRL_PLL_HALF BIT(21)
47 #define JZ_CLOCK_CTRL_LDIV_MASK 0x001f0000
48 #define JZ_CLOCK_CTRL_UDIV_OFFSET 23
49 #define JZ_CLOCK_CTRL_LDIV_OFFSET 16
50 #define JZ_CLOCK_CTRL_MDIV_OFFSET 12
51 #define JZ_CLOCK_CTRL_PDIV_OFFSET 8
52 #define JZ_CLOCK_CTRL_HDIV_OFFSET 4
53 #define JZ_CLOCK_CTRL_CDIV_OFFSET 0
55 #define JZ_CLOCK_GATE_UART0 BIT(0)
56 #define JZ_CLOCK_GATE_TCU BIT(1)
57 #define JZ_CLOCK_GATE_RTC BIT(2)
58 #define JZ_CLOCK_GATE_I2C BIT(3)
59 #define JZ_CLOCK_GATE_SPI BIT(4)
60 #define JZ_CLOCK_GATE_AIC BIT(5)
61 #define JZ_CLOCK_GATE_I2S BIT(6)
62 #define JZ_CLOCK_GATE_MMC BIT(7)
63 #define JZ_CLOCK_GATE_ADC BIT(8)
64 #define JZ_CLOCK_GATE_CIM BIT(9)
65 #define JZ_CLOCK_GATE_LCD BIT(10)
66 #define JZ_CLOCK_GATE_UDC BIT(11)
67 #define JZ_CLOCK_GATE_DMAC BIT(12)
68 #define JZ_CLOCK_GATE_IPU BIT(13)
69 #define JZ_CLOCK_GATE_UHC BIT(14)
70 #define JZ_CLOCK_GATE_UART1 BIT(15)
72 #define JZ_CLOCK_I2S_DIV_MASK 0x01ff
74 #define JZ_CLOCK_LCD_DIV_MASK 0x01ff
76 #define JZ_CLOCK_MMC_DIV_MASK 0x001f
78 #define JZ_CLOCK_UHC_DIV_MASK 0x000f
80 #define JZ_CLOCK_SPI_SRC_PLL BIT(31)
81 #define JZ_CLOCK_SPI_DIV_MASK 0x000f
83 #define JZ_CLOCK_PLL_M_MASK 0x01ff
84 #define JZ_CLOCK_PLL_N_MASK 0x001f
85 #define JZ_CLOCK_PLL_OD_MASK 0x0003
86 #define JZ_CLOCK_PLL_STABLE BIT(10)
87 #define JZ_CLOCK_PLL_BYPASS BIT(9)
88 #define JZ_CLOCK_PLL_ENABLED BIT(8)
89 #define JZ_CLOCK_PLL_STABLIZE_MASK 0x000f
90 #define JZ_CLOCK_PLL_M_OFFSET 23
91 #define JZ_CLOCK_PLL_N_OFFSET 18
92 #define JZ_CLOCK_PLL_OD_OFFSET 16
94 #define JZ_CLOCK_LOW_POWER_MODE_DOZE BIT(2)
95 #define JZ_CLOCK_LOW_POWER_MODE_SLEEP BIT(0)
97 #define JZ_CLOCK_SLEEP_CTRL_SUSPEND_UHC BIT(7)
98 #define JZ_CLOCK_SLEEP_CTRL_ENABLE_UDC BIT(6)
100 static void __iomem *jz_clock_base;
122 return readl(jz_clock_base + reg);
129 spin_lock(&jz_clock_lock);
130 val2 =
readl(jz_clock_base + reg);
133 writel(val2, jz_clock_base + reg);
134 spin_unlock(&jz_clock_lock);
141 spin_lock(&jz_clock_lock);
142 val =
readl(jz_clock_base + reg);
144 writel(val, jz_clock_base + reg);
145 spin_unlock(&jz_clock_lock);
152 spin_lock(&jz_clock_lock);
153 val =
readl(jz_clock_base + reg);
155 writel(val, jz_clock_base + reg);
156 spin_unlock(&jz_clock_lock);
159 static int jz_clk_enable_gating(
struct clk *
clk)
168 static int jz_clk_disable_gating(
struct clk *clk)
177 static int jz_clk_is_enabled_gating(
struct clk *clk)
185 static unsigned long jz_clk_static_get_rate(
struct clk *clk)
190 static int jz_clk_ko_enable(
struct clk *clk)
196 static int jz_clk_ko_disable(
struct clk *clk)
202 static int jz_clk_ko_is_enabled(
struct clk *clk)
207 static const int pllno[] = {1, 2, 2, 4};
209 static unsigned long jz_clk_pll_get_rate(
struct clk *clk)
221 m = ((val >> 23) & 0x1ff) + 2;
222 n = ((val >> 18) & 0x1f) + 2;
223 od = (val >> 16) & 0x3;
228 static unsigned long jz_clk_pll_half_get_rate(
struct clk *clk)
234 return jz_clk_pll_get_rate(clk->
parent);
235 return jz_clk_pll_get_rate(clk->
parent) >> 1;
238 static const int jz_clk_main_divs[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
240 static unsigned long jz_clk_main_round_rate(
struct clk *clk,
unsigned long rate)
242 unsigned long parent_rate = jz_clk_pll_get_rate(clk->
parent);
245 div = parent_rate /
rate;
247 return parent_rate / 32;
251 div &= (0x3 << (
ffs(div) - 1));
253 return parent_rate /
div;
256 static unsigned long jz_clk_main_get_rate(
struct clk *clk)
269 return jz_clk_pll_get_rate(clk->
parent) / jz_clk_main_divs[
div];
272 static int jz_clk_main_set_rate(
struct clk *clk,
unsigned long rate)
277 unsigned long parent_rate = jz_clk_pll_get_rate(clk->
parent);
279 rate = jz_clk_main_round_rate(clk, rate);
281 div = parent_rate /
rate;
283 i = (
ffs(div) - 1) << 1;
284 if (i > 0 && !(div &
BIT(i-1)))
293 static struct clk_ops jz_clk_static_ops = {
294 .get_rate = jz_clk_static_get_rate,
295 .enable = jz_clk_enable_gating,
296 .disable = jz_clk_disable_gating,
297 .is_enabled = jz_clk_is_enabled_gating,
304 .ops = &jz_clk_static_ops,
308 static struct clk_ops jz_clk_pll_ops = {
309 .get_rate = jz_clk_pll_get_rate,
312 static struct clk jz_clk_pll = {
314 .parent = &jz_clk_ext.
clk,
315 .ops = &jz_clk_pll_ops,
318 static struct clk_ops jz_clk_pll_half_ops = {
319 .get_rate = jz_clk_pll_half_get_rate,
322 static struct clk jz_clk_pll_half = {
324 .parent = &jz_clk_pll,
325 .
ops = &jz_clk_pll_half_ops,
328 static const struct clk_ops jz_clk_main_ops = {
329 .get_rate = jz_clk_main_get_rate,
330 .set_rate = jz_clk_main_set_rate,
331 .round_rate = jz_clk_main_round_rate,
334 static struct main_clk jz_clk_cpu = {
337 .parent = &jz_clk_pll,
338 .
ops = &jz_clk_main_ops,
343 static struct main_clk jz_clk_memory = {
346 .parent = &jz_clk_pll,
347 .
ops = &jz_clk_main_ops,
352 static struct main_clk jz_clk_high_speed_peripheral = {
355 .parent = &jz_clk_pll,
356 .
ops = &jz_clk_main_ops,
362 static struct main_clk jz_clk_low_speed_peripheral = {
365 .parent = &jz_clk_pll,
366 .
ops = &jz_clk_main_ops,
371 static const struct clk_ops jz_clk_ko_ops = {
372 .enable = jz_clk_ko_enable,
373 .disable = jz_clk_ko_disable,
374 .is_enabled = jz_clk_ko_is_enabled,
377 static struct clk jz_clk_ko = {
379 .parent = &jz_clk_memory.
clk,
380 .ops = &jz_clk_ko_ops,
383 static int jz_clk_spi_set_parent(
struct clk *clk,
struct clk *
parent)
385 if (parent == &jz_clk_pll)
387 else if (parent == &jz_clk_ext.
clk)
397 static int jz_clk_i2s_set_parent(
struct clk *clk,
struct clk *parent)
399 if (parent == &jz_clk_pll_half)
401 else if (parent == &jz_clk_ext.
clk)
411 static int jz_clk_udc_enable(
struct clk *clk)
419 static int jz_clk_udc_disable(
struct clk *clk)
427 static int jz_clk_udc_is_enabled(
struct clk *clk)
433 static int jz_clk_udc_set_parent(
struct clk *clk,
struct clk *parent)
435 if (parent == &jz_clk_pll_half)
437 else if (parent == &jz_clk_ext.
clk)
447 static int jz_clk_udc_set_rate(
struct clk *clk,
unsigned long rate)
466 static unsigned long jz_clk_udc_get_rate(
struct clk *clk)
480 static unsigned long jz_clk_divided_get_rate(
struct clk *clk)
488 div = (jz_clk_reg_read(dclk->
reg) & dclk->
mask) + 1;
493 static int jz_clk_divided_set_rate(
struct clk *clk,
unsigned long rate)
505 else if (div > dclk->
mask)
508 jz_clk_reg_write_mask(dclk->
reg, div, dclk->
mask);
513 static unsigned long jz_clk_ldclk_round_rate(
struct clk *clk,
unsigned long rate)
516 unsigned long parent_rate = jz_clk_pll_half_get_rate(clk->
parent);
518 if (rate > 150000000)
521 div = parent_rate /
rate;
527 return parent_rate /
div;
530 static int jz_clk_ldclk_set_rate(
struct clk *clk,
unsigned long rate)
534 if (rate > 150000000)
537 div = jz_clk_pll_half_get_rate(clk->
parent) / rate - 1;
549 static unsigned long jz_clk_ldclk_get_rate(
struct clk *clk)
556 return jz_clk_pll_half_get_rate(clk->
parent) / (div + 1);
559 static const struct clk_ops jz_clk_ops_ld = {
560 .set_rate = jz_clk_ldclk_set_rate,
561 .get_rate = jz_clk_ldclk_get_rate,
562 .round_rate = jz_clk_ldclk_round_rate,
563 .enable = jz_clk_enable_gating,
564 .disable = jz_clk_disable_gating,
565 .is_enabled = jz_clk_is_enabled_gating,
568 static struct clk jz_clk_ld = {
571 .parent = &jz_clk_pll_half,
572 .
ops = &jz_clk_ops_ld,
575 static const struct clk_ops jz_clk_i2s_ops = {
576 .set_rate = jz_clk_divided_set_rate,
577 .get_rate = jz_clk_divided_get_rate,
578 .enable = jz_clk_enable_gating,
579 .disable = jz_clk_disable_gating,
580 .is_enabled = jz_clk_is_enabled_gating,
581 .set_parent = jz_clk_i2s_set_parent,
584 static const struct clk_ops jz_clk_spi_ops = {
585 .set_rate = jz_clk_divided_set_rate,
586 .get_rate = jz_clk_divided_get_rate,
587 .enable = jz_clk_enable_gating,
588 .disable = jz_clk_disable_gating,
589 .is_enabled = jz_clk_is_enabled_gating,
590 .set_parent = jz_clk_spi_set_parent,
593 static const struct clk_ops jz_clk_divided_ops = {
594 .set_rate = jz_clk_divided_set_rate,
595 .get_rate = jz_clk_divided_get_rate,
596 .enable = jz_clk_enable_gating,
597 .disable = jz_clk_disable_gating,
598 .is_enabled = jz_clk_is_enabled_gating,
601 static struct divided_clk jz4740_clock_divided_clks[] = {
605 .parent = &jz_clk_ext.
clk,
607 .ops = &jz_clk_i2s_ops,
615 .parent = &jz_clk_ext.
clk,
617 .ops = &jz_clk_spi_ops,
625 .parent = &jz_clk_pll_half,
627 .ops = &jz_clk_divided_ops,
635 .parent = &jz_clk_pll_half,
637 .ops = &jz_clk_divided_ops,
645 .parent = &jz_clk_pll_half,
647 .ops = &jz_clk_divided_ops,
654 static const struct clk_ops jz_clk_udc_ops = {
655 .set_parent = jz_clk_udc_set_parent,
656 .set_rate = jz_clk_udc_set_rate,
657 .get_rate = jz_clk_udc_get_rate,
658 .enable = jz_clk_udc_enable,
659 .disable = jz_clk_udc_disable,
660 .is_enabled = jz_clk_udc_is_enabled,
663 static const struct clk_ops jz_clk_simple_ops = {
664 .enable = jz_clk_enable_gating,
665 .disable = jz_clk_disable_gating,
666 .is_enabled = jz_clk_is_enabled_gating,
669 static struct clk jz4740_clock_simple_clks[] = {
672 .parent = &jz_clk_ext.
clk,
673 .ops = &jz_clk_udc_ops,
677 .parent = &jz_clk_ext.
clk,
679 .ops = &jz_clk_simple_ops,
683 .parent = &jz_clk_ext.
clk,
685 .ops = &jz_clk_simple_ops,
689 .parent = &jz_clk_high_speed_peripheral.
clk,
691 .ops = &jz_clk_simple_ops,
695 .parent = &jz_clk_high_speed_peripheral.
clk,
697 .ops = &jz_clk_simple_ops,
701 .parent = &jz_clk_ext.
clk,
703 .ops = &jz_clk_simple_ops,
707 .parent = &jz_clk_ext.
clk,
709 .ops = &jz_clk_simple_ops,
713 .parent = &jz_clk_ext.
clk,
715 .ops = &jz_clk_simple_ops,
723 .ops = &jz_clk_static_ops,
730 if (!clk->
ops->enable)
733 return clk->
ops->enable(clk);
739 if (clk->
ops->disable)
740 clk->
ops->disable(clk);
746 if (clk->
ops->is_enabled)
747 return clk->
ops->is_enabled(clk);
754 if (clk->
ops->get_rate)
755 return clk->
ops->get_rate(clk);
765 if (!clk->
ops->set_rate)
767 return clk->
ops->set_rate(clk, rate);
773 if (clk->
ops->round_rate)
774 return clk->
ops->round_rate(clk, rate);
785 if (!clk->
ops->set_parent)
791 ret = clk->
ops->set_parent(clk, parent);
809 return ERR_PTR(-
ENXIO);
818 static inline void clk_add(
struct clk *clk)
825 static void clk_register_clks(
void)
829 clk_add(&jz_clk_ext.
clk);
830 clk_add(&jz_clk_pll);
831 clk_add(&jz_clk_pll_half);
832 clk_add(&jz_clk_cpu.
clk);
833 clk_add(&jz_clk_high_speed_peripheral.
clk);
834 clk_add(&jz_clk_low_speed_peripheral.
clk);
837 clk_add(&jz_clk_rtc.
clk);
839 for (i = 0; i <
ARRAY_SIZE(jz4740_clock_divided_clks); ++
i)
840 clk_add(&jz4740_clock_divided_clks[i].clk);
842 for (i = 0; i <
ARRAY_SIZE(jz4740_clock_simple_clks); ++
i)
843 clk_add(&jz4740_clock_simple_clks[i]);
892 static int jz4740_clock_init(
void)
908 jz4740_clock_divided_clks[1].
clk.parent = &jz_clk_pll_half;
913 jz4740_clock_divided_clks[0].
clk.parent = &jz_clk_pll_half;
916 jz4740_clock_simple_clks[0].
parent = &jz_clk_pll_half;