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