blob: 7926394cf7624284255f6fe9e2ab71907141e0fa [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
21 clrbits_le32(&prcm->res_cal_ctrl, 1);
22 setbits_le32(&prcm->res_cal_ctrl, 1);
Jernej Skrabec70bdefa2021-02-01 18:25:57 +010023
Jernej Skrabec964a86f2022-01-30 15:27:15 +010024 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6)) {
25 /* set key field for ldo enable */
26 setbits_le32(&prcm->pll_ldo_cfg, 0xA7000000);
27 /* set PLL VDD LDO output to 1.14 V */
28 setbits_le32(&prcm->pll_ldo_cfg, 0x60000);
29 }
30
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +080031 clock_set_pll1(408000000);
32
33 writel(CCM_PLL6_DEFAULT, &ccm->pll6_cfg);
34 while (!(readl(&ccm->pll6_cfg) & CCM_PLL6_LOCK))
35 ;
36
37 clrsetbits_le32(&ccm->cpu_axi_cfg, CCM_CPU_AXI_APB_MASK | CCM_CPU_AXI_AXI_MASK,
38 CCM_CPU_AXI_DEFAULT_FACTORS);
39
40 writel(CCM_PSI_AHB1_AHB2_DEFAULT, &ccm->psi_ahb1_ahb2_cfg);
41 writel(CCM_AHB3_DEFAULT, &ccm->ahb3_cfg);
42 writel(CCM_APB1_DEFAULT, &ccm->apb1_cfg);
43
44 /*
45 * The mux and factor are set, but the clock will be enabled in
46 * DRAM initialization code.
47 */
48 writel(MBUS_CLK_SRC_PLL6X2 | MBUS_CLK_M(3), &ccm->mbus_cfg);
49}
50#endif
51
52void clock_init_uart(void)
53{
54 struct sunxi_ccm_reg *const ccm =
55 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
56
57 /* uart clock source is apb2 */
58 writel(APB2_CLK_SRC_OSC24M|
59 APB2_CLK_RATE_N_1|
60 APB2_CLK_RATE_M(1),
61 &ccm->apb2_cfg);
62
63 /* open the clock for uart */
64 setbits_le32(&ccm->uart_gate_reset,
65 1 << (CONFIG_CONS_INDEX - 1));
66
67 /* deassert uart reset */
68 setbits_le32(&ccm->uart_gate_reset,
69 1 << (RESET_SHIFT + CONFIG_CONS_INDEX - 1));
70}
71
72#ifdef CONFIG_SPL_BUILD
73void clock_set_pll1(unsigned int clk)
74{
75 struct sunxi_ccm_reg * const ccm =
76 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
77 u32 val;
78
79 /* Do not support clocks < 288MHz as they need factor P */
80 if (clk < 288000000) clk = 288000000;
81
82 /* Switch to 24MHz clock while changing PLL1 */
83 val = readl(&ccm->cpu_axi_cfg);
84 val &= ~CCM_CPU_AXI_MUX_MASK;
85 val |= CCM_CPU_AXI_MUX_OSC24M;
86 writel(val, &ccm->cpu_axi_cfg);
87
88 /* clk = 24*n/p, p is ignored if clock is >288MHz */
89 writel(CCM_PLL1_CTRL_EN | CCM_PLL1_LOCK_EN | CCM_PLL1_CLOCK_TIME_2 |
Jernej Skrabec8b2239c2021-01-11 21:11:40 +010090#ifdef CONFIG_MACH_SUN50I_H616
91 CCM_PLL1_OUT_EN |
92#endif
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +080093 CCM_PLL1_CTRL_N(clk / 24000000), &ccm->pll1_cfg);
94 while (!(readl(&ccm->pll1_cfg) & CCM_PLL1_LOCK)) {}
95
96 /* Switch CPU to PLL1 */
97 val = readl(&ccm->cpu_axi_cfg);
98 val &= ~CCM_CPU_AXI_MUX_MASK;
99 val |= CCM_CPU_AXI_MUX_PLL_CPUX;
100 writel(val, &ccm->cpu_axi_cfg);
101}
102#endif
103
104unsigned int clock_get_pll6(void)
105{
106 struct sunxi_ccm_reg *const ccm =
107 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Jernej Skrabec8b2239c2021-01-11 21:11:40 +0100108 int m = IS_ENABLED(CONFIG_MACH_SUN50I_H6) ? 4 : 2;
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800109
110 uint32_t rval = readl(&ccm->pll6_cfg);
Andre Przywara0f7c8bc2021-05-05 13:53:05 +0100111 int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1;
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800112 int div1 = ((rval & CCM_PLL6_CTRL_DIV1_MASK) >>
113 CCM_PLL6_CTRL_DIV1_SHIFT) + 1;
114 int div2 = ((rval & CCM_PLL6_CTRL_DIV2_MASK) >>
115 CCM_PLL6_CTRL_DIV2_SHIFT) + 1;
Jernej Skrabec8b2239c2021-01-11 21:11:40 +0100116 /* The register defines PLL6-2X or PLL6-4X, not plain PLL6 */
117 return 24000000 / m * n / div1 / div2;
Icenowy Zhengd1fa87d2018-07-21 16:20:26 +0800118}
Jernej Skrabec55a30a22021-01-11 21:11:38 +0100119
120int clock_twi_onoff(int port, int state)
121{
122 struct sunxi_ccm_reg *const ccm =
123 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
124 struct sunxi_prcm_reg *const prcm =
125 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
126 u32 value, *ptr;
127 int shift;
128
129 value = BIT(GATE_SHIFT) | BIT (RESET_SHIFT);
130
131 if (port == 5) {
132 shift = 0;
133 ptr = &prcm->twi_gate_reset;
134 } else {
135 shift = port;
136 ptr = &ccm->twi_gate_reset;
137 }
138
139 /* set the apb clock gate and reset for twi */
140 if (state)
141 setbits_le32(ptr, value << shift);
142 else
143 clrbits_le32(ptr, value << shift);
144
145 return 0;
146}