Chen-Yu Tsai | 14177e4 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 1 | /* |
| 2 | * sun6i 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> |
Chen-Yu Tsai | c757a50 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 16 | #include <asm/arch/prcm.h> |
Chen-Yu Tsai | 14177e4 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 17 | #include <asm/arch/sys_proto.h> |
| 18 | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 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; |
Andre Przywara | 2865433 | 2017-01-02 11:48:25 +0000 | [diff] [blame] | 24 | |
Andre Przywara | 7b82a22 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 25 | #if !defined(CONFIG_MACH_SUNXI_H3_H5) && !defined(CONFIG_MACH_SUN50I) |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 26 | struct sunxi_prcm_reg * const prcm = |
| 27 | (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE; |
| 28 | |
| 29 | /* Set PLL ldo voltage without this PLL6 does not work properly */ |
| 30 | clrsetbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK, |
| 31 | PRCM_PLL_CTRL_LDO_KEY); |
| 32 | clrsetbits_le32(&prcm->pll_ctrl1, ~PRCM_PLL_CTRL_LDO_KEY_MASK, |
| 33 | PRCM_PLL_CTRL_LDO_DIGITAL_EN | PRCM_PLL_CTRL_LDO_ANALOG_EN | |
| 34 | PRCM_PLL_CTRL_EXT_OSC_EN | PRCM_PLL_CTRL_LDO_OUT_L(1140)); |
| 35 | clrbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK); |
Andre Przywara | 2865433 | 2017-01-02 11:48:25 +0000 | [diff] [blame] | 36 | #endif |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 37 | |
Chen-Yu Tsai | 328ce7f | 2016-11-30 16:54:34 +0800 | [diff] [blame^] | 38 | #ifdef CONFIG_MACH_SUN8I_R40 |
| 39 | /* Set PLL lock enable bits and switch to old lock mode */ |
| 40 | writel(GENMASK(12, 0), &ccm->pll_lock_ctrl); |
| 41 | #endif |
| 42 | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 43 | clock_set_pll1(408000000); |
| 44 | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 45 | writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg); |
Siarhei Siamashka | 52d0931 | 2015-11-20 07:07:48 +0200 | [diff] [blame] | 46 | while (!(readl(&ccm->pll6_cfg) & CCM_PLL6_CTRL_LOCK)) |
| 47 | ; |
| 48 | |
| 49 | writel(AHB1_ABP1_DIV_DEFAULT, &ccm->ahb1_apb1_div); |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 50 | |
| 51 | writel(MBUS_CLK_DEFAULT, &ccm->mbus0_clk_cfg); |
Andre Przywara | 2865433 | 2017-01-02 11:48:25 +0000 | [diff] [blame] | 52 | if (IS_ENABLED(CONFIG_MACH_SUN6I)) |
| 53 | writel(MBUS_CLK_DEFAULT, &ccm->mbus1_clk_cfg); |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 54 | } |
| 55 | #endif |
| 56 | |
Chen-Yu Tsai | ed80584 | 2016-01-06 15:13:07 +0800 | [diff] [blame] | 57 | void clock_init_sec(void) |
| 58 | { |
Andre Przywara | 7b82a22 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 59 | #ifdef CONFIG_MACH_SUNXI_H3_H5 |
Chen-Yu Tsai | ed80584 | 2016-01-06 15:13:07 +0800 | [diff] [blame] | 60 | struct sunxi_ccm_reg * const ccm = |
| 61 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 62 | |
| 63 | setbits_le32(&ccm->ccu_sec_switch, |
| 64 | CCM_SEC_SWITCH_MBUS_NONSEC | |
| 65 | CCM_SEC_SWITCH_BUS_NONSEC | |
| 66 | CCM_SEC_SWITCH_PLL_NONSEC); |
| 67 | #endif |
| 68 | } |
| 69 | |
Chen-Yu Tsai | 14177e4 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 70 | void clock_init_uart(void) |
| 71 | { |
Hans de Goede | 22b6183 | 2015-01-14 19:28:38 +0100 | [diff] [blame] | 72 | #if CONFIG_CONS_INDEX < 5 |
Chen-Yu Tsai | 14177e4 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 73 | struct sunxi_ccm_reg *const ccm = |
| 74 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 75 | |
| 76 | /* uart clock source is apb2 */ |
| 77 | writel(APB2_CLK_SRC_OSC24M| |
| 78 | APB2_CLK_RATE_N_1| |
| 79 | APB2_CLK_RATE_M(1), |
| 80 | &ccm->apb2_div); |
| 81 | |
| 82 | /* open the clock for uart */ |
| 83 | setbits_le32(&ccm->apb2_gate, |
| 84 | CLK_GATE_OPEN << (APB2_GATE_UART_SHIFT + |
| 85 | CONFIG_CONS_INDEX - 1)); |
| 86 | |
| 87 | /* deassert uart reset */ |
| 88 | setbits_le32(&ccm->apb2_reset_cfg, |
| 89 | 1 << (APB2_RESET_UART_SHIFT + |
| 90 | CONFIG_CONS_INDEX - 1)); |
Chen-Yu Tsai | c757a50 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 91 | #else |
| 92 | /* enable R_PIO and R_UART clocks, and de-assert resets */ |
| 93 | prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_UART); |
| 94 | #endif |
Chen-Yu Tsai | 14177e4 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 95 | } |
| 96 | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_SPL_BUILD |
| 98 | void clock_set_pll1(unsigned int clk) |
| 99 | { |
| 100 | struct sunxi_ccm_reg * const ccm = |
| 101 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Hans de Goede | 25508ab | 2014-12-27 17:56:59 +0100 | [diff] [blame] | 102 | const int p = 0; |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 103 | int k = 1; |
| 104 | int m = 1; |
| 105 | |
| 106 | if (clk > 1152000000) { |
| 107 | k = 2; |
| 108 | } else if (clk > 768000000) { |
| 109 | k = 3; |
| 110 | m = 2; |
| 111 | } |
| 112 | |
| 113 | /* Switch to 24MHz clock while changing PLL1 */ |
| 114 | writel(AXI_DIV_3 << AXI_DIV_SHIFT | |
| 115 | ATB_DIV_2 << ATB_DIV_SHIFT | |
| 116 | CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT, |
| 117 | &ccm->cpu_axi_cfg); |
| 118 | |
Hans de Goede | 25508ab | 2014-12-27 17:56:59 +0100 | [diff] [blame] | 119 | /* |
| 120 | * sun6i: PLL1 rate = ((24000000 * n * k) >> 0) / m (p is ignored) |
| 121 | * sun8i: PLL1 rate = ((24000000 * n * k) >> p) / m |
| 122 | */ |
| 123 | writel(CCM_PLL1_CTRL_EN | CCM_PLL1_CTRL_P(p) | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 124 | CCM_PLL1_CTRL_N(clk / (24000000 * k / m)) | |
| 125 | CCM_PLL1_CTRL_K(k) | CCM_PLL1_CTRL_M(m), &ccm->pll1_cfg); |
| 126 | sdelay(200); |
| 127 | |
| 128 | /* Switch CPU to PLL1 */ |
| 129 | writel(AXI_DIV_3 << AXI_DIV_SHIFT | |
| 130 | ATB_DIV_2 << ATB_DIV_SHIFT | |
| 131 | CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT, |
| 132 | &ccm->cpu_axi_cfg); |
| 133 | } |
| 134 | #endif |
| 135 | |
Hans de Goede | 0bd5125 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 136 | void clock_set_pll3(unsigned int clk) |
| 137 | { |
| 138 | struct sunxi_ccm_reg * const ccm = |
| 139 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 140 | const int m = 8; /* 3 MHz steps just like sun4i, sun5i and sun7i */ |
| 141 | |
| 142 | if (clk == 0) { |
| 143 | clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | /* PLL3 rate = 24000000 * n / m */ |
| 148 | writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE | |
| 149 | CCM_PLL3_CTRL_N(clk / (24000000 / m)) | CCM_PLL3_CTRL_M(m), |
| 150 | &ccm->pll3_cfg); |
| 151 | } |
| 152 | |
Hans de Goede | 5af741f | 2014-11-30 11:58:17 +0100 | [diff] [blame] | 153 | void clock_set_pll5(unsigned int clk, bool sigma_delta_enable) |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 154 | { |
| 155 | struct sunxi_ccm_reg * const ccm = |
| 156 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Hans de Goede | 1aac47b | 2014-12-07 21:09:31 +0100 | [diff] [blame] | 157 | const int max_n = 32; |
| 158 | int k = 1, m = 2; |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 159 | |
Andre Przywara | 7b82a22 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 160 | #ifdef CONFIG_MACH_SUNXI_H3_H5 |
Jens Kuske | d5ac6ee | 2016-08-19 13:40:46 +0200 | [diff] [blame] | 161 | clrsetbits_le32(&ccm->pll5_tuning_cfg, CCM_PLL5_TUN_LOCK_TIME_MASK | |
| 162 | CCM_PLL5_TUN_INIT_FREQ_MASK, |
| 163 | CCM_PLL5_TUN_LOCK_TIME(2) | CCM_PLL5_TUN_INIT_FREQ(16)); |
| 164 | #endif |
| 165 | |
Hans de Goede | 5af741f | 2014-11-30 11:58:17 +0100 | [diff] [blame] | 166 | if (sigma_delta_enable) |
| 167 | writel(CCM_PLL5_PATTERN, &ccm->pll5_pattern_cfg); |
| 168 | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 169 | /* PLL5 rate = 24000000 * n * k / m */ |
Hans de Goede | 1aac47b | 2014-12-07 21:09:31 +0100 | [diff] [blame] | 170 | if (clk > 24000000 * k * max_n / m) { |
| 171 | m = 1; |
| 172 | if (clk > 24000000 * k * max_n / m) |
| 173 | k = 2; |
| 174 | } |
Hans de Goede | 5af741f | 2014-11-30 11:58:17 +0100 | [diff] [blame] | 175 | writel(CCM_PLL5_CTRL_EN | |
| 176 | (sigma_delta_enable ? CCM_PLL5_CTRL_SIGMA_DELTA_EN : 0) | |
| 177 | CCM_PLL5_CTRL_UPD | |
Hans de Goede | 62c87ef | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 178 | CCM_PLL5_CTRL_N(clk / (24000000 * k / m)) | |
| 179 | CCM_PLL5_CTRL_K(k) | CCM_PLL5_CTRL_M(m), &ccm->pll5_cfg); |
| 180 | |
| 181 | udelay(5500); |
| 182 | } |
| 183 | |
Hans de Goede | 55ea98d | 2015-08-08 14:05:35 +0200 | [diff] [blame] | 184 | #ifdef CONFIG_MACH_SUN6I |
| 185 | void clock_set_mipi_pll(unsigned int clk) |
| 186 | { |
| 187 | struct sunxi_ccm_reg * const ccm = |
| 188 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 189 | unsigned int k, m, n, value, diff; |
| 190 | unsigned best_k = 0, best_m = 0, best_n = 0, best_diff = 0xffffffff; |
| 191 | unsigned int src = clock_get_pll3(); |
| 192 | |
| 193 | /* All calculations are in KHz to avoid overflows */ |
| 194 | clk /= 1000; |
| 195 | src /= 1000; |
| 196 | |
| 197 | /* Pick the closest lower clock */ |
| 198 | for (k = 1; k <= 4; k++) { |
| 199 | for (m = 1; m <= 16; m++) { |
| 200 | for (n = 1; n <= 16; n++) { |
| 201 | value = src * n * k / m; |
| 202 | if (value > clk) |
| 203 | continue; |
| 204 | |
| 205 | diff = clk - value; |
| 206 | if (diff < best_diff) { |
| 207 | best_diff = diff; |
| 208 | best_k = k; |
| 209 | best_m = m; |
| 210 | best_n = n; |
| 211 | } |
| 212 | if (diff == 0) |
| 213 | goto done; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | done: |
| 219 | writel(CCM_MIPI_PLL_CTRL_EN | CCM_MIPI_PLL_CTRL_LDO_EN | |
| 220 | CCM_MIPI_PLL_CTRL_N(best_n) | CCM_MIPI_PLL_CTRL_K(best_k) | |
| 221 | CCM_MIPI_PLL_CTRL_M(best_m), &ccm->mipi_pll_cfg); |
| 222 | } |
| 223 | #endif |
| 224 | |
Jens Kuske | 1bc464b | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 225 | #if defined(CONFIG_MACH_SUN8I_A33) || defined(CONFIG_MACH_SUN50I) |
Hans de Goede | 886a7b4 | 2015-04-12 11:46:41 +0200 | [diff] [blame] | 226 | void clock_set_pll11(unsigned int clk, bool sigma_delta_enable) |
| 227 | { |
| 228 | struct sunxi_ccm_reg * const ccm = |
| 229 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 230 | |
| 231 | if (sigma_delta_enable) |
Philipp Tomsich | b556159 | 2017-01-02 11:48:41 +0000 | [diff] [blame] | 232 | writel(CCM_PLL11_PATTERN, &ccm->pll11_pattern_cfg0); |
Hans de Goede | 886a7b4 | 2015-04-12 11:46:41 +0200 | [diff] [blame] | 233 | |
| 234 | writel(CCM_PLL11_CTRL_EN | CCM_PLL11_CTRL_UPD | |
| 235 | (sigma_delta_enable ? CCM_PLL11_CTRL_SIGMA_DELTA_EN : 0) | |
| 236 | CCM_PLL11_CTRL_N(clk / 24000000), &ccm->pll11_cfg); |
| 237 | |
| 238 | while (readl(&ccm->pll11_cfg) & CCM_PLL11_CTRL_UPD) |
| 239 | ; |
| 240 | } |
| 241 | #endif |
| 242 | |
Hans de Goede | 49043cb | 2015-08-08 12:36:44 +0200 | [diff] [blame] | 243 | unsigned int clock_get_pll3(void) |
| 244 | { |
| 245 | struct sunxi_ccm_reg *const ccm = |
| 246 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 247 | uint32_t rval = readl(&ccm->pll3_cfg); |
| 248 | int n = ((rval & CCM_PLL3_CTRL_N_MASK) >> CCM_PLL3_CTRL_N_SHIFT) + 1; |
| 249 | int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT) + 1; |
| 250 | |
| 251 | /* Multiply by 1000 after dividing by m to avoid integer overflows */ |
| 252 | return (24000 * n / m) * 1000; |
| 253 | } |
| 254 | |
Chen-Yu Tsai | 14177e4 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 255 | unsigned int clock_get_pll6(void) |
| 256 | { |
| 257 | struct sunxi_ccm_reg *const ccm = |
| 258 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 259 | uint32_t rval = readl(&ccm->pll6_cfg); |
| 260 | int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1; |
| 261 | int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1; |
| 262 | return 24000000 * n * k / 2; |
| 263 | } |
Hans de Goede | 0bd5125 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 264 | |
Hans de Goede | 55ea98d | 2015-08-08 14:05:35 +0200 | [diff] [blame] | 265 | unsigned int clock_get_mipi_pll(void) |
| 266 | { |
| 267 | struct sunxi_ccm_reg *const ccm = |
| 268 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 269 | uint32_t rval = readl(&ccm->mipi_pll_cfg); |
| 270 | unsigned int n = ((rval & CCM_MIPI_PLL_CTRL_N_MASK) >> CCM_MIPI_PLL_CTRL_N_SHIFT) + 1; |
| 271 | unsigned int k = ((rval & CCM_MIPI_PLL_CTRL_K_MASK) >> CCM_MIPI_PLL_CTRL_K_SHIFT) + 1; |
| 272 | unsigned int m = ((rval & CCM_MIPI_PLL_CTRL_M_MASK) >> CCM_MIPI_PLL_CTRL_M_SHIFT) + 1; |
| 273 | unsigned int src = clock_get_pll3(); |
| 274 | |
| 275 | /* Multiply by 1000 after dividing by m to avoid integer overflows */ |
| 276 | return ((src / 1000) * n * k / m) * 1000; |
| 277 | } |
| 278 | |
Hans de Goede | 0bd5125 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 279 | void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz) |
| 280 | { |
| 281 | int pll = clock_get_pll6() * 2; |
| 282 | int div = 1; |
| 283 | |
| 284 | while ((pll / div) > hz) |
| 285 | div++; |
| 286 | |
| 287 | writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_PLL6_2X | CCM_DE_CTRL_M(div), |
| 288 | clk_cfg); |
| 289 | } |