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