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