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