blob: bea91c78bc5b5b0e8c8886efa272a7fc91168f85 [file] [log] [blame]
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +08001#include <common.h>
2#include <asm/io.h>
3#include <asm/arch/cpu.h>
4#include <asm/arch/clock.h>
Jernej Skrabec55a30a22021-01-11 21:11:38 +01005#include <asm/arch/prcm.h>
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +08006
7#ifdef CONFIG_SPL_BUILD
8void clock_init_safe(void)
9{
10 struct sunxi_ccm_reg *const ccm =
11 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Jernej Skrabece04cd492022-01-30 15:27:13 +010012 struct sunxi_prcm_reg *const prcm =
13 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
Jernej Skrabec70bdefa2021-02-01 18:25:57 +010014
Jernej Skrabec59221142022-01-30 15:27:14 +010015 if (IS_ENABLED(CONFIG_MACH_SUN50I_H616)) {
16 /* this seems to enable PLLs on H616 */
Jernej Skrabece04cd492022-01-30 15:27:13 +010017 setbits_le32(&prcm->sys_pwroff_gating, 0x10);
Jernej Skrabec59221142022-01-30 15:27:14 +010018 setbits_le32(&prcm->res_cal_ctrl, 2);
19 }
20
Andre Przywara068962b2022-10-05 17:54:19 +010021 if (IS_ENABLED(CONFIG_MACH_SUN50I_H616) ||
22 IS_ENABLED(CONFIG_MACH_SUN50I_H6)) {
23 clrbits_le32(&prcm->res_cal_ctrl, 1);
24 setbits_le32(&prcm->res_cal_ctrl, 1);
25 }
Jernej Skrabec70bdefa2021-02-01 18:25:57 +010026
Jernej Skrabec964a86f2022-01-30 15:27:15 +010027 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6)) {
28 /* set key field for ldo enable */
29 setbits_le32(&prcm->pll_ldo_cfg, 0xA7000000);
30 /* set PLL VDD LDO output to 1.14 V */
31 setbits_le32(&prcm->pll_ldo_cfg, 0x60000);
32 }
33
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +080034 clock_set_pll1(408000000);
35
36 writel(CCM_PLL6_DEFAULT, &ccm->pll6_cfg);
37 while (!(readl(&ccm->pll6_cfg) & CCM_PLL6_LOCK))
38 ;
39
40 clrsetbits_le32(&ccm->cpu_axi_cfg, CCM_CPU_AXI_APB_MASK | CCM_CPU_AXI_AXI_MASK,
41 CCM_CPU_AXI_DEFAULT_FACTORS);
42
43 writel(CCM_PSI_AHB1_AHB2_DEFAULT, &ccm->psi_ahb1_ahb2_cfg);
Andre Przywara1987b0c2022-09-06 15:59:57 +010044#ifdef CCM_AHB3_DEFAULT
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +080045 writel(CCM_AHB3_DEFAULT, &ccm->ahb3_cfg);
Andre Przywara1987b0c2022-09-06 15:59:57 +010046#endif
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +080047 writel(CCM_APB1_DEFAULT, &ccm->apb1_cfg);
48
49 /*
50 * The mux and factor are set, but the clock will be enabled in
51 * DRAM initialization code.
52 */
53 writel(MBUS_CLK_SRC_PLL6X2 | MBUS_CLK_M(3), &ccm->mbus_cfg);
54}
55#endif
56
57void clock_init_uart(void)
58{
59 struct sunxi_ccm_reg *const ccm =
60 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
61
62 /* uart clock source is apb2 */
63 writel(APB2_CLK_SRC_OSC24M|
64 APB2_CLK_RATE_N_1|
65 APB2_CLK_RATE_M(1),
66 &ccm->apb2_cfg);
67
68 /* open the clock for uart */
69 setbits_le32(&ccm->uart_gate_reset,
70 1 << (CONFIG_CONS_INDEX - 1));
71
72 /* deassert uart reset */
73 setbits_le32(&ccm->uart_gate_reset,
74 1 << (RESET_SHIFT + CONFIG_CONS_INDEX - 1));
75}
76
77#ifdef CONFIG_SPL_BUILD
78void clock_set_pll1(unsigned int clk)
79{
80 struct sunxi_ccm_reg * const ccm =
81 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
82 u32 val;
83
84 /* Do not support clocks < 288MHz as they need factor P */
85 if (clk < 288000000) clk = 288000000;
86
87 /* Switch to 24MHz clock while changing PLL1 */
88 val = readl(&ccm->cpu_axi_cfg);
89 val &= ~CCM_CPU_AXI_MUX_MASK;
90 val |= CCM_CPU_AXI_MUX_OSC24M;
91 writel(val, &ccm->cpu_axi_cfg);
92
93 /* clk = 24*n/p, p is ignored if clock is >288MHz */
Andre Przywara1b946cd2022-12-02 20:30:40 +000094 val = CCM_PLL1_CTRL_EN | CCM_PLL1_LOCK_EN | CCM_PLL1_CLOCK_TIME_2;
95 val |= CCM_PLL1_CTRL_N(clk / 24000000);
96 if (IS_ENABLED(CONFIG_MACH_SUN50I_H616))
97 val |= CCM_PLL1_OUT_EN;
98 if (IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
99 val |= CCM_PLL1_OUT_EN | CCM_PLL1_LDO_EN;
100 writel(val, &ccm->pll1_cfg);
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800101 while (!(readl(&ccm->pll1_cfg) & CCM_PLL1_LOCK)) {}
102
103 /* Switch CPU to PLL1 */
104 val = readl(&ccm->cpu_axi_cfg);
105 val &= ~CCM_CPU_AXI_MUX_MASK;
106 val |= CCM_CPU_AXI_MUX_PLL_CPUX;
107 writel(val, &ccm->cpu_axi_cfg);
108}
109#endif
110
111unsigned int clock_get_pll6(void)
112{
113 struct sunxi_ccm_reg *const ccm =
114 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800115 uint32_t rval = readl(&ccm->pll6_cfg);
Andre Przywara0f7c8bc2021-05-05 13:53:05 +0100116 int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1;
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800117 int div2 = ((rval & CCM_PLL6_CTRL_DIV2_MASK) >>
Andre Przywara0f2a5b12022-12-02 21:48:19 +0000118 CCM_PLL6_CTRL_DIV2_SHIFT) + 1;
119 int div1, m;
120
121 if (IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2)) {
122 div1 = ((rval & CCM_PLL6_CTRL_P0_MASK) >>
123 CCM_PLL6_CTRL_P0_SHIFT) + 1;
124 m = 1;
125 } else {
126 div1 = ((rval & CCM_PLL6_CTRL_DIV1_MASK) >>
127 CCM_PLL6_CTRL_DIV1_SHIFT) + 1;
128 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
129 m = 4;
130 else
131 m = 2;
132 }
133
134 return 24000000U * n / m / div1 / div2;
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800135}
Jernej Skrabec55a30a22021-01-11 21:11:38 +0100136
137int clock_twi_onoff(int port, int state)
138{
139 struct sunxi_ccm_reg *const ccm =
140 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
141 struct sunxi_prcm_reg *const prcm =
142 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
143 u32 value, *ptr;
144 int shift;
145
146 value = BIT(GATE_SHIFT) | BIT (RESET_SHIFT);
147
148 if (port == 5) {
149 shift = 0;
150 ptr = &prcm->twi_gate_reset;
151 } else {
152 shift = port;
153 ptr = &ccm->twi_gate_reset;
154 }
155
156 /* set the apb clock gate and reset for twi */
157 if (state)
158 setbits_le32(ptr, value << shift);
159 else
160 clrbits_le32(ptr, value << shift);
161
162 return 0;
163}