Icenowy Zheng | d1fa87d | 2018-07-21 16:20:26 +0800 | [diff] [blame] | 1 | #include <common.h> |
| 2 | #include <asm/io.h> |
| 3 | #include <asm/arch/cpu.h> |
| 4 | #include <asm/arch/clock.h> |
Jernej Skrabec | 55a30a2 | 2021-01-11 21:11:38 +0100 | [diff] [blame] | 5 | #include <asm/arch/prcm.h> |
Icenowy Zheng | d1fa87d | 2018-07-21 16:20:26 +0800 | [diff] [blame] | 6 | |
| 7 | #ifdef CONFIG_SPL_BUILD |
| 8 | void 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 | |
| 33 | void 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 |
| 54 | void 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 | |
Jernej Skrabec | 8b2239c | 2021-01-11 21:11:40 +0100 | [diff] [blame] | 71 | #ifdef CONFIG_MACH_SUN50I_H616 |
| 72 | CCM_PLL1_OUT_EN | |
| 73 | #endif |
Icenowy Zheng | d1fa87d | 2018-07-21 16:20:26 +0800 | [diff] [blame] | 74 | CCM_PLL1_CTRL_N(clk / 24000000), &ccm->pll1_cfg); |
| 75 | while (!(readl(&ccm->pll1_cfg) & CCM_PLL1_LOCK)) {} |
| 76 | |
| 77 | /* Switch CPU to PLL1 */ |
| 78 | val = readl(&ccm->cpu_axi_cfg); |
| 79 | val &= ~CCM_CPU_AXI_MUX_MASK; |
| 80 | val |= CCM_CPU_AXI_MUX_PLL_CPUX; |
| 81 | writel(val, &ccm->cpu_axi_cfg); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | unsigned int clock_get_pll6(void) |
| 86 | { |
| 87 | struct sunxi_ccm_reg *const ccm = |
| 88 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Jernej Skrabec | 8b2239c | 2021-01-11 21:11:40 +0100 | [diff] [blame] | 89 | int m = IS_ENABLED(CONFIG_MACH_SUN50I_H6) ? 4 : 2; |
Icenowy Zheng | d1fa87d | 2018-07-21 16:20:26 +0800 | [diff] [blame] | 90 | |
| 91 | uint32_t rval = readl(&ccm->pll6_cfg); |
| 92 | int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT); |
| 93 | int div1 = ((rval & CCM_PLL6_CTRL_DIV1_MASK) >> |
| 94 | CCM_PLL6_CTRL_DIV1_SHIFT) + 1; |
| 95 | int div2 = ((rval & CCM_PLL6_CTRL_DIV2_MASK) >> |
| 96 | CCM_PLL6_CTRL_DIV2_SHIFT) + 1; |
Jernej Skrabec | 8b2239c | 2021-01-11 21:11:40 +0100 | [diff] [blame] | 97 | /* The register defines PLL6-2X or PLL6-4X, not plain PLL6 */ |
| 98 | return 24000000 / m * n / div1 / div2; |
Icenowy Zheng | d1fa87d | 2018-07-21 16:20:26 +0800 | [diff] [blame] | 99 | } |
Jernej Skrabec | 55a30a2 | 2021-01-11 21:11:38 +0100 | [diff] [blame] | 100 | |
| 101 | int clock_twi_onoff(int port, int state) |
| 102 | { |
| 103 | struct sunxi_ccm_reg *const ccm = |
| 104 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 105 | struct sunxi_prcm_reg *const prcm = |
| 106 | (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE; |
| 107 | u32 value, *ptr; |
| 108 | int shift; |
| 109 | |
| 110 | value = BIT(GATE_SHIFT) | BIT (RESET_SHIFT); |
| 111 | |
| 112 | if (port == 5) { |
| 113 | shift = 0; |
| 114 | ptr = &prcm->twi_gate_reset; |
| 115 | } else { |
| 116 | shift = port; |
| 117 | ptr = &ccm->twi_gate_reset; |
| 118 | } |
| 119 | |
| 120 | /* set the apb clock gate and reset for twi */ |
| 121 | if (state) |
| 122 | setbits_le32(ptr, value << shift); |
| 123 | else |
| 124 | clrbits_le32(ptr, value << shift); |
| 125 | |
| 126 | return 0; |
| 127 | } |