Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 2 | /* |
| 3 | * sun6i 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> |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/clock.h> |
Chen-Yu Tsai | 6ee6388 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 15 | #include <asm/arch/prcm.h> |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 16 | #include <asm/arch/sys_proto.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 19 | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 20 | #ifdef CONFIG_SPL_BUILD |
| 21 | void clock_init_safe(void) |
| 22 | { |
| 23 | struct sunxi_ccm_reg * const ccm = |
| 24 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Andre Przywara | 79b59ef | 2017-01-02 11:48:25 +0000 | [diff] [blame] | 25 | |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 26 | #if !defined(CONFIG_MACH_SUNXI_H3_H5) && !defined(CONFIG_MACH_SUN50I) && \ |
| 27 | !defined(CONFIG_MACH_SUNIV) |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 28 | struct sunxi_prcm_reg * const prcm = |
| 29 | (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE; |
| 30 | |
| 31 | /* Set PLL ldo voltage without this PLL6 does not work properly */ |
| 32 | clrsetbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK, |
| 33 | PRCM_PLL_CTRL_LDO_KEY); |
| 34 | clrsetbits_le32(&prcm->pll_ctrl1, ~PRCM_PLL_CTRL_LDO_KEY_MASK, |
| 35 | PRCM_PLL_CTRL_LDO_DIGITAL_EN | PRCM_PLL_CTRL_LDO_ANALOG_EN | |
| 36 | PRCM_PLL_CTRL_EXT_OSC_EN | PRCM_PLL_CTRL_LDO_OUT_L(1140)); |
| 37 | clrbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK); |
Andre Przywara | 79b59ef | 2017-01-02 11:48:25 +0000 | [diff] [blame] | 38 | #endif |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 39 | |
Jernej Skrabec | 9b4ca92 | 2017-03-27 19:22:31 +0200 | [diff] [blame] | 40 | #if defined(CONFIG_MACH_SUN8I_R40) || defined(CONFIG_MACH_SUN50I) |
Chen-Yu Tsai | 5eddcbb | 2016-11-30 16:54:34 +0800 | [diff] [blame] | 41 | /* Set PLL lock enable bits and switch to old lock mode */ |
| 42 | writel(GENMASK(12, 0), &ccm->pll_lock_ctrl); |
| 43 | #endif |
| 44 | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 45 | clock_set_pll1(408000000); |
| 46 | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 47 | writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg); |
Siarhei Siamashka | 2b8bd91 | 2015-11-20 07:07:48 +0200 | [diff] [blame] | 48 | while (!(readl(&ccm->pll6_cfg) & CCM_PLL6_CTRL_LOCK)) |
| 49 | ; |
| 50 | |
| 51 | writel(AHB1_ABP1_DIV_DEFAULT, &ccm->ahb1_apb1_div); |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 52 | |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 53 | if (!IS_ENABLED(CONFIG_MACH_SUNIV)) { |
| 54 | writel(MBUS_CLK_DEFAULT, &ccm->mbus0_clk_cfg); |
| 55 | if (IS_ENABLED(CONFIG_MACH_SUN6I)) |
| 56 | writel(MBUS_CLK_DEFAULT, &ccm->mbus1_clk_cfg); |
| 57 | } |
Icenowy Zheng | 3279661 | 2017-05-01 14:31:56 +0800 | [diff] [blame] | 58 | |
| 59 | #if defined(CONFIG_MACH_SUN8I_R40) && defined(CONFIG_SUNXI_AHCI) |
| 60 | setbits_le32(&ccm->sata_pll_cfg, CCM_SATA_PLL_DEFAULT); |
| 61 | setbits_le32(&ccm->ahb_reset0_cfg, 0x1 << AHB_GATE_OFFSET_SATA); |
| 62 | setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_SATA); |
| 63 | setbits_le32(&ccm->sata_clk_cfg, CCM_SATA_CTRL_ENABLE); |
| 64 | #endif |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 65 | } |
Andre Przywara | a9aab24 | 2022-11-28 00:02:56 +0000 | [diff] [blame] | 66 | #endif /* CONFIG_SPL_BUILD */ |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 67 | |
Chen-Yu Tsai | 6daddfe | 2016-01-06 15:13:07 +0800 | [diff] [blame] | 68 | void clock_init_sec(void) |
| 69 | { |
Andre Przywara | 5fb9743 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 70 | #ifdef CONFIG_MACH_SUNXI_H3_H5 |
Chen-Yu Tsai | 6daddfe | 2016-01-06 15:13:07 +0800 | [diff] [blame] | 71 | struct sunxi_ccm_reg * const ccm = |
| 72 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Icenowy Zheng | 883b3c0 | 2017-07-20 14:00:32 +0800 | [diff] [blame] | 73 | struct sunxi_prcm_reg * const prcm = |
| 74 | (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE; |
Chen-Yu Tsai | 6daddfe | 2016-01-06 15:13:07 +0800 | [diff] [blame] | 75 | |
| 76 | setbits_le32(&ccm->ccu_sec_switch, |
| 77 | CCM_SEC_SWITCH_MBUS_NONSEC | |
| 78 | CCM_SEC_SWITCH_BUS_NONSEC | |
| 79 | CCM_SEC_SWITCH_PLL_NONSEC); |
Icenowy Zheng | 883b3c0 | 2017-07-20 14:00:32 +0800 | [diff] [blame] | 80 | setbits_le32(&prcm->prcm_sec_switch, |
| 81 | PRCM_SEC_SWITCH_APB0_CLK_NONSEC | |
| 82 | PRCM_SEC_SWITCH_PLL_CFG_NONSEC | |
| 83 | PRCM_SEC_SWITCH_PWR_GATE_NONSEC); |
Chen-Yu Tsai | 6daddfe | 2016-01-06 15:13:07 +0800 | [diff] [blame] | 84 | #endif |
| 85 | } |
| 86 | |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 87 | void clock_init_uart(void) |
| 88 | { |
Hans de Goede | 627bc69 | 2015-01-14 19:28:38 +0100 | [diff] [blame] | 89 | #if CONFIG_CONS_INDEX < 5 |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 90 | struct sunxi_ccm_reg *const ccm = |
| 91 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 92 | |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 93 | #ifdef CONFIG_MACH_SUNIV |
| 94 | /* suniv doesn't have apb2, UART clock source is always apb1 */ |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 95 | |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 96 | /* open the clock for uart */ |
| 97 | setbits_le32(&ccm->apb1_gate, |
| 98 | CLK_GATE_OPEN << (APB1_GATE_UART_SHIFT + |
| 99 | CONFIG_CONS_INDEX - 1)); |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 100 | |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 101 | /* deassert uart reset */ |
| 102 | setbits_le32(&ccm->apb1_reset_cfg, |
| 103 | 1 << (APB1_RESET_UART_SHIFT + |
| 104 | CONFIG_CONS_INDEX - 1)); |
Chen-Yu Tsai | 6ee6388 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 105 | #else |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 106 | /* uart clock source is apb2 */ |
| 107 | writel(APB2_CLK_SRC_OSC24M| |
| 108 | APB2_CLK_RATE_N_1| |
| 109 | APB2_CLK_RATE_M(1), |
| 110 | &ccm->apb2_div); |
| 111 | |
| 112 | /* open the clock for uart */ |
| 113 | setbits_le32(&ccm->apb2_gate, |
| 114 | CLK_GATE_OPEN << (APB2_GATE_UART_SHIFT + |
| 115 | CONFIG_CONS_INDEX - 1)); |
| 116 | |
| 117 | /* deassert uart reset */ |
| 118 | setbits_le32(&ccm->apb2_reset_cfg, |
| 119 | 1 << (APB2_RESET_UART_SHIFT + |
| 120 | CONFIG_CONS_INDEX - 1)); |
| 121 | #endif /* !CONFIG_MACH_SUNIV */ |
| 122 | #else /* CONFIG_CONS_INDEX >= 5 */ |
Chen-Yu Tsai | 6ee6388 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 123 | /* enable R_PIO and R_UART clocks, and de-assert resets */ |
| 124 | prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_UART); |
| 125 | #endif |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 128 | #ifdef CONFIG_SPL_BUILD |
| 129 | void clock_set_pll1(unsigned int clk) |
| 130 | { |
| 131 | struct sunxi_ccm_reg * const ccm = |
| 132 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Hans de Goede | 645d4d5 | 2014-12-27 17:56:59 +0100 | [diff] [blame] | 133 | const int p = 0; |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 134 | int k = 1; |
| 135 | int m = 1; |
| 136 | |
| 137 | if (clk > 1152000000) { |
| 138 | k = 2; |
| 139 | } else if (clk > 768000000) { |
Stefan Mavrodiev | b01dc98 | 2019-07-31 16:15:52 +0300 | [diff] [blame] | 140 | k = 4; |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 141 | m = 2; |
| 142 | } |
| 143 | |
| 144 | /* Switch to 24MHz clock while changing PLL1 */ |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 145 | if (IS_ENABLED(CONFIG_MACH_SUNIV)) { |
| 146 | writel(CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT, |
| 147 | &ccm->cpu_axi_cfg); |
| 148 | } else { |
| 149 | writel(AXI_DIV_3 << AXI_DIV_SHIFT | |
| 150 | ATB_DIV_2 << ATB_DIV_SHIFT | |
| 151 | CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT, |
| 152 | &ccm->cpu_axi_cfg); |
| 153 | } |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 154 | |
Hans de Goede | 645d4d5 | 2014-12-27 17:56:59 +0100 | [diff] [blame] | 155 | /* |
| 156 | * sun6i: PLL1 rate = ((24000000 * n * k) >> 0) / m (p is ignored) |
| 157 | * sun8i: PLL1 rate = ((24000000 * n * k) >> p) / m |
| 158 | */ |
| 159 | writel(CCM_PLL1_CTRL_EN | CCM_PLL1_CTRL_P(p) | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 160 | CCM_PLL1_CTRL_N(clk / (24000000 * k / m)) | |
| 161 | CCM_PLL1_CTRL_K(k) | CCM_PLL1_CTRL_M(m), &ccm->pll1_cfg); |
| 162 | sdelay(200); |
| 163 | |
| 164 | /* Switch CPU to PLL1 */ |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 165 | if (IS_ENABLED(CONFIG_MACH_SUNIV)) { |
| 166 | writel(CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT, |
| 167 | &ccm->cpu_axi_cfg); |
| 168 | } else { |
| 169 | writel(AXI_DIV_3 << AXI_DIV_SHIFT | |
| 170 | ATB_DIV_2 << ATB_DIV_SHIFT | |
| 171 | CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT, |
| 172 | &ccm->cpu_axi_cfg); |
| 173 | } |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 174 | } |
Andre Przywara | a9aab24 | 2022-11-28 00:02:56 +0000 | [diff] [blame] | 175 | #endif /* CONFIG_SPL_BUILD */ |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 176 | |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 177 | void clock_set_pll3(unsigned int clk) |
| 178 | { |
| 179 | struct sunxi_ccm_reg * const ccm = |
| 180 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Icenowy Zheng | 05e7220 | 2018-10-28 14:26:12 -0700 | [diff] [blame] | 181 | #ifdef CONFIG_SUNXI_DE2 |
| 182 | const int m = 4; /* 6 MHz steps to allow higher frequency for DE2 */ |
| 183 | #else |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 184 | const int m = 8; /* 3 MHz steps just like sun4i, sun5i and sun7i */ |
Icenowy Zheng | 05e7220 | 2018-10-28 14:26:12 -0700 | [diff] [blame] | 185 | #endif |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 186 | |
| 187 | if (clk == 0) { |
| 188 | clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | /* PLL3 rate = 24000000 * n / m */ |
| 193 | writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE | |
| 194 | CCM_PLL3_CTRL_N(clk / (24000000 / m)) | CCM_PLL3_CTRL_M(m), |
| 195 | &ccm->pll3_cfg); |
| 196 | } |
| 197 | |
Jernej Skrabec | 9b4ca92 | 2017-03-27 19:22:31 +0200 | [diff] [blame] | 198 | #ifdef CONFIG_SUNXI_DE2 |
| 199 | void clock_set_pll3_factors(int m, int n) |
| 200 | { |
| 201 | struct sunxi_ccm_reg * const ccm = |
| 202 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 203 | |
| 204 | /* PLL3 rate = 24000000 * n / m */ |
| 205 | writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE | |
| 206 | CCM_PLL3_CTRL_N(n) | CCM_PLL3_CTRL_M(m), |
| 207 | &ccm->pll3_cfg); |
| 208 | |
| 209 | while (!(readl(&ccm->pll3_cfg) & CCM_PLL3_CTRL_LOCK)) |
| 210 | ; |
| 211 | } |
| 212 | #endif |
| 213 | |
Hans de Goede | 0cbc4cb | 2014-11-30 11:58:17 +0100 | [diff] [blame] | 214 | void clock_set_pll5(unsigned int clk, bool sigma_delta_enable) |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 215 | { |
| 216 | struct sunxi_ccm_reg * const ccm = |
| 217 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Hans de Goede | 0bfa774 | 2014-12-07 21:09:31 +0100 | [diff] [blame] | 218 | const int max_n = 32; |
| 219 | int k = 1, m = 2; |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 220 | |
Andre Przywara | 5fb9743 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 221 | #ifdef CONFIG_MACH_SUNXI_H3_H5 |
Jens Kuske | 213407e | 2016-08-19 13:40:46 +0200 | [diff] [blame] | 222 | clrsetbits_le32(&ccm->pll5_tuning_cfg, CCM_PLL5_TUN_LOCK_TIME_MASK | |
| 223 | CCM_PLL5_TUN_INIT_FREQ_MASK, |
| 224 | CCM_PLL5_TUN_LOCK_TIME(2) | CCM_PLL5_TUN_INIT_FREQ(16)); |
| 225 | #endif |
| 226 | |
Hans de Goede | 0cbc4cb | 2014-11-30 11:58:17 +0100 | [diff] [blame] | 227 | if (sigma_delta_enable) |
| 228 | writel(CCM_PLL5_PATTERN, &ccm->pll5_pattern_cfg); |
| 229 | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 230 | /* PLL5 rate = 24000000 * n * k / m */ |
Hans de Goede | 0bfa774 | 2014-12-07 21:09:31 +0100 | [diff] [blame] | 231 | if (clk > 24000000 * k * max_n / m) { |
| 232 | m = 1; |
| 233 | if (clk > 24000000 * k * max_n / m) |
| 234 | k = 2; |
| 235 | } |
Hans de Goede | 0cbc4cb | 2014-11-30 11:58:17 +0100 | [diff] [blame] | 236 | writel(CCM_PLL5_CTRL_EN | |
| 237 | (sigma_delta_enable ? CCM_PLL5_CTRL_SIGMA_DELTA_EN : 0) | |
| 238 | CCM_PLL5_CTRL_UPD | |
Hans de Goede | c27d68d | 2014-10-25 20:16:33 +0200 | [diff] [blame] | 239 | CCM_PLL5_CTRL_N(clk / (24000000 * k / m)) | |
| 240 | CCM_PLL5_CTRL_K(k) | CCM_PLL5_CTRL_M(m), &ccm->pll5_cfg); |
| 241 | |
| 242 | udelay(5500); |
| 243 | } |
| 244 | |
Hans de Goede | d6eaadc | 2015-08-08 14:05:35 +0200 | [diff] [blame] | 245 | #ifdef CONFIG_MACH_SUN6I |
| 246 | void clock_set_mipi_pll(unsigned int clk) |
| 247 | { |
| 248 | struct sunxi_ccm_reg * const ccm = |
| 249 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 250 | unsigned int k, m, n, value, diff; |
| 251 | unsigned best_k = 0, best_m = 0, best_n = 0, best_diff = 0xffffffff; |
| 252 | unsigned int src = clock_get_pll3(); |
| 253 | |
| 254 | /* All calculations are in KHz to avoid overflows */ |
| 255 | clk /= 1000; |
| 256 | src /= 1000; |
| 257 | |
| 258 | /* Pick the closest lower clock */ |
| 259 | for (k = 1; k <= 4; k++) { |
| 260 | for (m = 1; m <= 16; m++) { |
| 261 | for (n = 1; n <= 16; n++) { |
| 262 | value = src * n * k / m; |
| 263 | if (value > clk) |
| 264 | continue; |
| 265 | |
| 266 | diff = clk - value; |
| 267 | if (diff < best_diff) { |
| 268 | best_diff = diff; |
| 269 | best_k = k; |
| 270 | best_m = m; |
| 271 | best_n = n; |
| 272 | } |
| 273 | if (diff == 0) |
| 274 | goto done; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | done: |
| 280 | writel(CCM_MIPI_PLL_CTRL_EN | CCM_MIPI_PLL_CTRL_LDO_EN | |
| 281 | CCM_MIPI_PLL_CTRL_N(best_n) | CCM_MIPI_PLL_CTRL_K(best_k) | |
| 282 | CCM_MIPI_PLL_CTRL_M(best_m), &ccm->mipi_pll_cfg); |
| 283 | } |
| 284 | #endif |
| 285 | |
Jernej Skrabec | 9b4ca92 | 2017-03-27 19:22:31 +0200 | [diff] [blame] | 286 | #ifdef CONFIG_SUNXI_DE2 |
| 287 | void clock_set_pll10(unsigned int clk) |
| 288 | { |
| 289 | struct sunxi_ccm_reg * const ccm = |
| 290 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 291 | const int m = 2; /* 12 MHz steps */ |
| 292 | |
| 293 | if (clk == 0) { |
| 294 | clrbits_le32(&ccm->pll10_cfg, CCM_PLL10_CTRL_EN); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | /* PLL10 rate = 24000000 * n / m */ |
| 299 | writel(CCM_PLL10_CTRL_EN | CCM_PLL10_CTRL_INTEGER_MODE | |
| 300 | CCM_PLL10_CTRL_N(clk / (24000000 / m)) | CCM_PLL10_CTRL_M(m), |
| 301 | &ccm->pll10_cfg); |
| 302 | |
| 303 | while (!(readl(&ccm->pll10_cfg) & CCM_PLL10_CTRL_LOCK)) |
| 304 | ; |
| 305 | } |
| 306 | #endif |
| 307 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 308 | #if defined(CONFIG_MACH_SUN8I_A33) || \ |
| 309 | defined(CONFIG_MACH_SUN8I_R40) || \ |
| 310 | defined(CONFIG_MACH_SUN50I) |
Hans de Goede | 0fdbe20 | 2015-04-12 11:46:41 +0200 | [diff] [blame] | 311 | void clock_set_pll11(unsigned int clk, bool sigma_delta_enable) |
| 312 | { |
| 313 | struct sunxi_ccm_reg * const ccm = |
| 314 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 315 | |
| 316 | if (sigma_delta_enable) |
Philipp Tomsich | ced4a9a | 2017-01-02 11:48:41 +0000 | [diff] [blame] | 317 | writel(CCM_PLL11_PATTERN, &ccm->pll11_pattern_cfg0); |
Hans de Goede | 0fdbe20 | 2015-04-12 11:46:41 +0200 | [diff] [blame] | 318 | |
| 319 | writel(CCM_PLL11_CTRL_EN | CCM_PLL11_CTRL_UPD | |
| 320 | (sigma_delta_enable ? CCM_PLL11_CTRL_SIGMA_DELTA_EN : 0) | |
| 321 | CCM_PLL11_CTRL_N(clk / 24000000), &ccm->pll11_cfg); |
| 322 | |
| 323 | while (readl(&ccm->pll11_cfg) & CCM_PLL11_CTRL_UPD) |
| 324 | ; |
| 325 | } |
| 326 | #endif |
| 327 | |
Hans de Goede | 957a72729 | 2015-08-08 12:36:44 +0200 | [diff] [blame] | 328 | unsigned int clock_get_pll3(void) |
| 329 | { |
| 330 | struct sunxi_ccm_reg *const ccm = |
| 331 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 332 | uint32_t rval = readl(&ccm->pll3_cfg); |
| 333 | int n = ((rval & CCM_PLL3_CTRL_N_MASK) >> CCM_PLL3_CTRL_N_SHIFT) + 1; |
| 334 | int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT) + 1; |
| 335 | |
| 336 | /* Multiply by 1000 after dividing by m to avoid integer overflows */ |
| 337 | return (24000 * n / m) * 1000; |
| 338 | } |
| 339 | |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 340 | unsigned int clock_get_pll6(void) |
| 341 | { |
| 342 | struct sunxi_ccm_reg *const ccm = |
| 343 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 344 | uint32_t rval = readl(&ccm->pll6_cfg); |
| 345 | int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1; |
| 346 | int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1; |
Icenowy Zheng | 8f2d1c0 | 2022-01-29 10:23:07 -0500 | [diff] [blame] | 347 | if (IS_ENABLED(CONFIG_MACH_SUNIV)) |
| 348 | return 24000000 * n * k; |
| 349 | else |
| 350 | return 24000000 * n * k / 2; |
Chen-Yu Tsai | 3a04542 | 2014-10-03 20:16:25 +0800 | [diff] [blame] | 351 | } |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 352 | |
Hans de Goede | d6eaadc | 2015-08-08 14:05:35 +0200 | [diff] [blame] | 353 | unsigned int clock_get_mipi_pll(void) |
| 354 | { |
| 355 | struct sunxi_ccm_reg *const ccm = |
| 356 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 357 | uint32_t rval = readl(&ccm->mipi_pll_cfg); |
| 358 | unsigned int n = ((rval & CCM_MIPI_PLL_CTRL_N_MASK) >> CCM_MIPI_PLL_CTRL_N_SHIFT) + 1; |
| 359 | unsigned int k = ((rval & CCM_MIPI_PLL_CTRL_K_MASK) >> CCM_MIPI_PLL_CTRL_K_SHIFT) + 1; |
| 360 | unsigned int m = ((rval & CCM_MIPI_PLL_CTRL_M_MASK) >> CCM_MIPI_PLL_CTRL_M_SHIFT) + 1; |
| 361 | unsigned int src = clock_get_pll3(); |
| 362 | |
| 363 | /* Multiply by 1000 after dividing by m to avoid integer overflows */ |
| 364 | return ((src / 1000) * n * k / m) * 1000; |
| 365 | } |
| 366 | |
Hans de Goede | 70d7ab5 | 2014-11-08 14:07:27 +0100 | [diff] [blame] | 367 | void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz) |
| 368 | { |
| 369 | int pll = clock_get_pll6() * 2; |
| 370 | int div = 1; |
| 371 | |
| 372 | while ((pll / div) > hz) |
| 373 | div++; |
| 374 | |
| 375 | writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_PLL6_2X | CCM_DE_CTRL_M(div), |
| 376 | clk_cfg); |
| 377 | } |