Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * sun4i, sun5i and sun7i specific clock code |
| 4 | * |
| 5 | * (C) Copyright 2007-2012 |
| 6 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 7 | * Tom Cubie <tangliang@allwinnertech.com> |
| 8 | * |
| 9 | * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net> |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/arch/clock.h> |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 14 | #include <asm/arch/sys_proto.h> |
| 15 | |
Simon Glass | 85ed77d | 2024-09-29 19:49:46 -0600 | [diff] [blame] | 16 | #ifdef CONFIG_XPL_BUILD |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 17 | void clock_init_safe(void) |
| 18 | { |
| 19 | struct sunxi_ccm_reg * const ccm = |
| 20 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 21 | |
| 22 | /* Set safe defaults until PMU is configured */ |
| 23 | writel(AXI_DIV_1 << AXI_DIV_SHIFT | |
| 24 | AHB_DIV_2 << AHB_DIV_SHIFT | |
| 25 | APB0_DIV_1 << APB0_DIV_SHIFT | |
| 26 | CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT, |
| 27 | &ccm->cpu_ahb_apb0_cfg); |
Ludwig Kormann | b2d4e4b | 2024-02-01 09:45:50 +0100 | [diff] [blame] | 28 | sdelay(20); |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 29 | writel(PLL1_CFG_DEFAULT, &ccm->pll1_cfg); |
| 30 | sdelay(200); |
| 31 | writel(AXI_DIV_1 << AXI_DIV_SHIFT | |
| 32 | AHB_DIV_2 << AHB_DIV_SHIFT | |
| 33 | APB0_DIV_1 << APB0_DIV_SHIFT | |
| 34 | CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT, |
| 35 | &ccm->cpu_ahb_apb0_cfg); |
Ludwig Kormann | b2d4e4b | 2024-02-01 09:45:50 +0100 | [diff] [blame] | 36 | sdelay(20); |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 37 | #ifdef CONFIG_MACH_SUN7I |
Ian Campbell | 504166e | 2014-06-05 19:00:16 +0100 | [diff] [blame] | 38 | setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_DMA); |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 39 | #endif |
| 40 | writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg); |
Ian Campbell | a2ebf92 | 2014-07-18 20:38:41 +0100 | [diff] [blame] | 41 | #ifdef CONFIG_SUNXI_AHCI |
| 42 | setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_SATA); |
| 43 | setbits_le32(&ccm->pll6_cfg, 0x1 << CCM_PLL6_CTRL_SATA_EN_SHIFT); |
| 44 | #endif |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 45 | } |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 46 | |
| 47 | void clock_init_uart(void) |
| 48 | { |
| 49 | struct sunxi_ccm_reg *const ccm = |
| 50 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 51 | |
| 52 | /* uart clock source is apb1 */ |
| 53 | writel(APB1_CLK_SRC_OSC24M| |
| 54 | APB1_CLK_RATE_N_1| |
| 55 | APB1_CLK_RATE_M(1), |
| 56 | &ccm->apb1_clk_div_cfg); |
| 57 | |
| 58 | /* open the clock for uart */ |
| 59 | setbits_le32(&ccm->apb1_gate, |
Olliver Schinagl | fc67dbc | 2015-12-03 17:49:29 +0100 | [diff] [blame] | 60 | CLK_GATE_OPEN << (APB1_GATE_UART_SHIFT+CONFIG_CONS_INDEX - 1)); |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int clock_twi_onoff(int port, int state) |
| 64 | { |
| 65 | struct sunxi_ccm_reg *const ccm = |
| 66 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 67 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 68 | /* set the apb clock gate for twi */ |
| 69 | if (state) |
| 70 | setbits_le32(&ccm->apb1_gate, |
Olliver Schinagl | fc67dbc | 2015-12-03 17:49:29 +0100 | [diff] [blame] | 71 | CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port)); |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 72 | else |
| 73 | clrbits_le32(&ccm->apb1_gate, |
Olliver Schinagl | fc67dbc | 2015-12-03 17:49:29 +0100 | [diff] [blame] | 74 | CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port)); |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 79 | #define PLL1_CFG(N, K, M, P) ( 1 << CCM_PLL1_CFG_ENABLE_SHIFT | \ |
| 80 | 0 << CCM_PLL1_CFG_VCO_RST_SHIFT | \ |
| 81 | 8 << CCM_PLL1_CFG_VCO_BIAS_SHIFT | \ |
| 82 | 0 << CCM_PLL1_CFG_PLL4_EXCH_SHIFT | \ |
| 83 | 16 << CCM_PLL1_CFG_BIAS_CUR_SHIFT | \ |
| 84 | (P)<< CCM_PLL1_CFG_DIVP_SHIFT | \ |
| 85 | 2 << CCM_PLL1_CFG_LCK_TMR_SHIFT | \ |
| 86 | (N)<< CCM_PLL1_CFG_FACTOR_N_SHIFT | \ |
| 87 | (K)<< CCM_PLL1_CFG_FACTOR_K_SHIFT | \ |
| 88 | 0 << CCM_PLL1_CFG_SIG_DELT_PAT_IN_SHIFT | \ |
| 89 | 0 << CCM_PLL1_CFG_SIG_DELT_PAT_EN_SHIFT | \ |
| 90 | (M)<< CCM_PLL1_CFG_FACTOR_M_SHIFT) |
| 91 | |
| 92 | static struct { |
| 93 | u32 pll1_cfg; |
| 94 | unsigned int freq; |
| 95 | } pll1_para[] = { |
| 96 | /* This array must be ordered by frequency. */ |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 97 | { PLL1_CFG(31, 1, 0, 0), 1488000000}, |
Iain Paton | c4b9dee | 2015-03-28 10:25:28 +0000 | [diff] [blame] | 98 | { PLL1_CFG(30, 1, 0, 0), 1440000000}, |
| 99 | { PLL1_CFG(29, 1, 0, 0), 1392000000}, |
| 100 | { PLL1_CFG(28, 1, 0, 0), 1344000000}, |
| 101 | { PLL1_CFG(27, 1, 0, 0), 1296000000}, |
| 102 | { PLL1_CFG(26, 1, 0, 0), 1248000000}, |
| 103 | { PLL1_CFG(25, 1, 0, 0), 1200000000}, |
| 104 | { PLL1_CFG(24, 1, 0, 0), 1152000000}, |
| 105 | { PLL1_CFG(23, 1, 0, 0), 1104000000}, |
| 106 | { PLL1_CFG(22, 1, 0, 0), 1056000000}, |
| 107 | { PLL1_CFG(21, 1, 0, 0), 1008000000}, |
| 108 | { PLL1_CFG(20, 1, 0, 0), 960000000 }, |
| 109 | { PLL1_CFG(19, 1, 0, 0), 912000000 }, |
| 110 | { PLL1_CFG(16, 1, 0, 0), 768000000 }, |
| 111 | /* Final catchall entry 384MHz*/ |
| 112 | { PLL1_CFG(16, 0, 0, 0), 0 }, |
| 113 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | void clock_set_pll1(unsigned int hz) |
| 117 | { |
| 118 | int i = 0; |
| 119 | int axi, ahb, apb0; |
| 120 | struct sunxi_ccm_reg * const ccm = |
| 121 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 122 | |
| 123 | /* Find target frequency */ |
Iain Paton | c4b9dee | 2015-03-28 10:25:28 +0000 | [diff] [blame] | 124 | while (pll1_para[i].freq > hz) |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 125 | i++; |
| 126 | |
| 127 | hz = pll1_para[i].freq; |
Iain Paton | c4b9dee | 2015-03-28 10:25:28 +0000 | [diff] [blame] | 128 | if (! hz) |
| 129 | hz = 384000000; |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 130 | |
| 131 | /* Calculate system clock divisors */ |
| 132 | axi = DIV_ROUND_UP(hz, 432000000); /* Max 450MHz */ |
| 133 | ahb = DIV_ROUND_UP(hz/axi, 204000000); /* Max 250MHz */ |
| 134 | apb0 = 2; /* Max 150MHz */ |
| 135 | |
| 136 | printf("CPU: %uHz, AXI/AHB/APB: %d/%d/%d\n", hz, axi, ahb, apb0); |
| 137 | |
| 138 | /* Map divisors to register values */ |
| 139 | axi = axi - 1; |
| 140 | if (ahb > 4) |
| 141 | ahb = 3; |
| 142 | else if (ahb > 2) |
| 143 | ahb = 2; |
| 144 | else if (ahb > 1) |
| 145 | ahb = 1; |
| 146 | else |
| 147 | ahb = 0; |
| 148 | |
| 149 | apb0 = apb0 - 1; |
| 150 | |
| 151 | /* Switch to 24MHz clock while changing PLL1 */ |
| 152 | writel(AXI_DIV_1 << AXI_DIV_SHIFT | |
| 153 | AHB_DIV_2 << AHB_DIV_SHIFT | |
| 154 | APB0_DIV_1 << APB0_DIV_SHIFT | |
| 155 | CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT, |
| 156 | &ccm->cpu_ahb_apb0_cfg); |
| 157 | sdelay(20); |
| 158 | |
| 159 | /* Configure sys clock divisors */ |
| 160 | writel(axi << AXI_DIV_SHIFT | |
| 161 | ahb << AHB_DIV_SHIFT | |
| 162 | apb0 << APB0_DIV_SHIFT | |
| 163 | CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT, |
| 164 | &ccm->cpu_ahb_apb0_cfg); |
| 165 | |
| 166 | /* Configure PLL1 at the desired frequency */ |
| 167 | writel(pll1_para[i].pll1_cfg, &ccm->pll1_cfg); |
| 168 | sdelay(200); |
| 169 | |
| 170 | /* Switch CPU to PLL1 */ |
| 171 | writel(axi << AXI_DIV_SHIFT | |
| 172 | ahb << AHB_DIV_SHIFT | |
| 173 | apb0 << APB0_DIV_SHIFT | |
| 174 | CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT, |
| 175 | &ccm->cpu_ahb_apb0_cfg); |
| 176 | sdelay(20); |
| 177 | } |
Simon Glass | 85ed77d | 2024-09-29 19:49:46 -0600 | [diff] [blame] | 178 | #endif /* CONFIG_XPL_BUILD */ |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 179 | |
Andre Przywara | d6f06fb | 2023-12-07 16:06:51 +0000 | [diff] [blame] | 180 | /* video, DRAM, PLL_PERIPH clocks */ |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 181 | void clock_set_pll3(unsigned int clk) |
| 182 | { |
| 183 | struct sunxi_ccm_reg * const ccm = |
| 184 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 185 | |
| 186 | if (clk == 0) { |
| 187 | clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | /* PLL3 rate = 3000000 * m */ |
| 192 | writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE | |
| 193 | CCM_PLL3_CTRL_M(clk / 3000000), &ccm->pll3_cfg); |
| 194 | } |
| 195 | |
Hans de Goede | 957a72729 | 2015-08-08 12:36:44 +0200 | [diff] [blame] | 196 | unsigned int clock_get_pll3(void) |
| 197 | { |
| 198 | struct sunxi_ccm_reg *const ccm = |
| 199 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 200 | uint32_t rval = readl(&ccm->pll3_cfg); |
| 201 | int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT); |
| 202 | return 3000000 * m; |
| 203 | } |
| 204 | |
Hans de Goede | 9f07273 | 2014-10-22 14:42:48 +0200 | [diff] [blame] | 205 | unsigned int clock_get_pll5p(void) |
| 206 | { |
| 207 | struct sunxi_ccm_reg *const ccm = |
| 208 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 209 | uint32_t rval = readl(&ccm->pll5_cfg); |
| 210 | int n = ((rval & CCM_PLL5_CTRL_N_MASK) >> CCM_PLL5_CTRL_N_SHIFT); |
| 211 | int k = ((rval & CCM_PLL5_CTRL_K_MASK) >> CCM_PLL5_CTRL_K_SHIFT) + 1; |
| 212 | int p = ((rval & CCM_PLL5_CTRL_P_MASK) >> CCM_PLL5_CTRL_P_SHIFT); |
| 213 | return (24000000 * n * k) >> p; |
| 214 | } |
| 215 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 216 | unsigned int clock_get_pll6(void) |
| 217 | { |
| 218 | struct sunxi_ccm_reg *const ccm = |
| 219 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 220 | uint32_t rval = readl(&ccm->pll6_cfg); |
| 221 | int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT); |
| 222 | int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1; |
| 223 | return 24000000 * n * k / 2; |
| 224 | } |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 225 | |
| 226 | void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz) |
| 227 | { |
| 228 | int pll = clock_get_pll5p(); |
| 229 | int div = 1; |
| 230 | |
| 231 | while ((pll / div) > hz) |
| 232 | div++; |
| 233 | |
| 234 | writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_RST | CCM_DE_CTRL_PLL5P | |
| 235 | CCM_DE_CTRL_M(div), clk_cfg); |
| 236 | } |