Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2018-2019 NXP |
| 4 | * |
| 5 | * Peng Fan <peng.fan@nxp.com> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/arch/clock.h> |
| 10 | #include <asm/arch/imx-regs.h> |
| 11 | #include <asm/arch/sys_proto.h> |
| 12 | #include <asm/io.h> |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 13 | #include <div64.h> |
| 14 | #include <errno.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 20 | static struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR; |
| 21 | |
Peng Fan | 4e36e33 | 2020-07-09 11:06:24 +0800 | [diff] [blame] | 22 | static u32 get_root_clk(enum clk_root_index clock_id); |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 23 | void enable_ocotp_clk(unsigned char enable) |
| 24 | { |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 25 | clock_enable(CCGR_OCOTP, !!enable); |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | int enable_i2c_clk(unsigned char enable, unsigned i2c_num) |
| 29 | { |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 30 | /* 0 - 3 is valid i2c num */ |
| 31 | if (i2c_num > 3) |
| 32 | return -EINVAL; |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 33 | |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 34 | clock_enable(CCGR_I2C1 + i2c_num, !!enable); |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 35 | |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 36 | return 0; |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | #ifdef CONFIG_SPL_BUILD |
| 40 | static struct imx_int_pll_rate_table imx8mm_fracpll_tbl[] = { |
Peng Fan | d29bf22 | 2019-12-27 11:40:55 +0800 | [diff] [blame] | 41 | PLL_1443X_RATE(1000000000U, 250, 3, 1, 0), |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 42 | PLL_1443X_RATE(800000000U, 300, 9, 0, 0), |
| 43 | PLL_1443X_RATE(750000000U, 250, 8, 0, 0), |
| 44 | PLL_1443X_RATE(650000000U, 325, 3, 2, 0), |
| 45 | PLL_1443X_RATE(600000000U, 300, 3, 2, 0), |
| 46 | PLL_1443X_RATE(594000000U, 99, 1, 2, 0), |
| 47 | PLL_1443X_RATE(400000000U, 300, 9, 1, 0), |
| 48 | PLL_1443X_RATE(266666667U, 400, 9, 2, 0), |
| 49 | PLL_1443X_RATE(167000000U, 334, 3, 4, 0), |
| 50 | PLL_1443X_RATE(100000000U, 300, 9, 3, 0), |
| 51 | }; |
| 52 | |
Alifer Moraes | 3993110 | 2020-01-14 15:54:59 -0300 | [diff] [blame] | 53 | static int fracpll_configure(enum pll_clocks pll, u32 freq) |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 54 | { |
| 55 | int i; |
| 56 | u32 tmp, div_val; |
| 57 | void *pll_base; |
| 58 | struct imx_int_pll_rate_table *rate; |
| 59 | |
| 60 | for (i = 0; i < ARRAY_SIZE(imx8mm_fracpll_tbl); i++) { |
| 61 | if (freq == imx8mm_fracpll_tbl[i].rate) |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (i == ARRAY_SIZE(imx8mm_fracpll_tbl)) { |
| 66 | printf("No matched freq table %u\n", freq); |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | rate = &imx8mm_fracpll_tbl[i]; |
| 71 | |
| 72 | switch (pll) { |
| 73 | case ANATOP_DRAM_PLL: |
| 74 | setbits_le32(GPC_BASE_ADDR + 0xEC, 1 << 7); |
| 75 | setbits_le32(GPC_BASE_ADDR + 0xF8, 1 << 5); |
| 76 | writel(SRC_DDR1_ENABLE_MASK, SRC_BASE_ADDR + 0x1004); |
| 77 | |
| 78 | pll_base = &ana_pll->dram_pll_gnrl_ctl; |
| 79 | break; |
| 80 | case ANATOP_VIDEO_PLL: |
| 81 | pll_base = &ana_pll->video_pll1_gnrl_ctl; |
| 82 | break; |
| 83 | default: |
| 84 | return 0; |
| 85 | } |
| 86 | /* Bypass clock and set lock to pll output lock */ |
| 87 | tmp = readl(pll_base); |
| 88 | tmp |= BYPASS_MASK; |
| 89 | writel(tmp, pll_base); |
| 90 | |
| 91 | /* Enable RST */ |
| 92 | tmp &= ~RST_MASK; |
| 93 | writel(tmp, pll_base); |
| 94 | |
| 95 | div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) | |
| 96 | (rate->sdiv << SDIV_SHIFT); |
| 97 | writel(div_val, pll_base + 4); |
| 98 | writel(rate->kdiv << KDIV_SHIFT, pll_base + 8); |
| 99 | |
| 100 | __udelay(100); |
| 101 | |
| 102 | /* Disable RST */ |
| 103 | tmp |= RST_MASK; |
| 104 | writel(tmp, pll_base); |
| 105 | |
| 106 | /* Wait Lock*/ |
| 107 | while (!(readl(pll_base) & LOCK_STATUS)) |
| 108 | ; |
| 109 | |
| 110 | /* Bypass */ |
| 111 | tmp &= ~BYPASS_MASK; |
| 112 | writel(tmp, pll_base); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | void dram_pll_init(ulong pll_val) |
| 118 | { |
| 119 | fracpll_configure(ANATOP_DRAM_PLL, pll_val); |
| 120 | } |
| 121 | |
| 122 | static struct dram_bypass_clk_setting imx8mm_dram_bypass_tbl[] = { |
| 123 | DRAM_BYPASS_ROOT_CONFIG(MHZ(100), 2, CLK_ROOT_PRE_DIV1, 2, |
| 124 | CLK_ROOT_PRE_DIV2), |
| 125 | DRAM_BYPASS_ROOT_CONFIG(MHZ(250), 3, CLK_ROOT_PRE_DIV2, 2, |
| 126 | CLK_ROOT_PRE_DIV2), |
| 127 | DRAM_BYPASS_ROOT_CONFIG(MHZ(400), 1, CLK_ROOT_PRE_DIV2, 3, |
| 128 | CLK_ROOT_PRE_DIV2), |
| 129 | }; |
| 130 | |
| 131 | void dram_enable_bypass(ulong clk_val) |
| 132 | { |
| 133 | int i; |
| 134 | struct dram_bypass_clk_setting *config; |
| 135 | |
| 136 | for (i = 0; i < ARRAY_SIZE(imx8mm_dram_bypass_tbl); i++) { |
| 137 | if (clk_val == imx8mm_dram_bypass_tbl[i].clk) |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | if (i == ARRAY_SIZE(imx8mm_dram_bypass_tbl)) { |
| 142 | printf("No matched freq table %lu\n", clk_val); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | config = &imx8mm_dram_bypass_tbl[i]; |
| 147 | |
| 148 | clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON | |
| 149 | CLK_ROOT_SOURCE_SEL(config->alt_root_sel) | |
| 150 | CLK_ROOT_PRE_DIV(config->alt_pre_div)); |
| 151 | clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | |
| 152 | CLK_ROOT_SOURCE_SEL(config->apb_root_sel) | |
| 153 | CLK_ROOT_PRE_DIV(config->apb_pre_div)); |
| 154 | clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON | |
| 155 | CLK_ROOT_SOURCE_SEL(1)); |
| 156 | } |
| 157 | |
| 158 | void dram_disable_bypass(void) |
| 159 | { |
| 160 | clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON | |
| 161 | CLK_ROOT_SOURCE_SEL(0)); |
| 162 | clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | |
| 163 | CLK_ROOT_SOURCE_SEL(4) | |
| 164 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV5)); |
| 165 | } |
| 166 | #endif |
| 167 | |
Peng Fan | 4e36e33 | 2020-07-09 11:06:24 +0800 | [diff] [blame] | 168 | int intpll_configure(enum pll_clocks pll, ulong freq) |
| 169 | { |
| 170 | void __iomem *pll_gnrl_ctl, __iomem *pll_div_ctl; |
| 171 | u32 pll_div_ctl_val, pll_clke_masks; |
| 172 | |
| 173 | switch (pll) { |
| 174 | case ANATOP_SYSTEM_PLL1: |
| 175 | pll_gnrl_ctl = &ana_pll->sys_pll1_gnrl_ctl; |
| 176 | pll_div_ctl = &ana_pll->sys_pll1_div_ctl; |
| 177 | pll_clke_masks = INTPLL_DIV20_CLKE_MASK | |
| 178 | INTPLL_DIV10_CLKE_MASK | INTPLL_DIV8_CLKE_MASK | |
| 179 | INTPLL_DIV6_CLKE_MASK | INTPLL_DIV5_CLKE_MASK | |
| 180 | INTPLL_DIV4_CLKE_MASK | INTPLL_DIV3_CLKE_MASK | |
| 181 | INTPLL_DIV2_CLKE_MASK | INTPLL_CLKE_MASK; |
| 182 | break; |
| 183 | case ANATOP_SYSTEM_PLL2: |
| 184 | pll_gnrl_ctl = &ana_pll->sys_pll2_gnrl_ctl; |
| 185 | pll_div_ctl = &ana_pll->sys_pll2_div_ctl; |
| 186 | pll_clke_masks = INTPLL_DIV20_CLKE_MASK | |
| 187 | INTPLL_DIV10_CLKE_MASK | INTPLL_DIV8_CLKE_MASK | |
| 188 | INTPLL_DIV6_CLKE_MASK | INTPLL_DIV5_CLKE_MASK | |
| 189 | INTPLL_DIV4_CLKE_MASK | INTPLL_DIV3_CLKE_MASK | |
| 190 | INTPLL_DIV2_CLKE_MASK | INTPLL_CLKE_MASK; |
| 191 | break; |
| 192 | case ANATOP_SYSTEM_PLL3: |
| 193 | pll_gnrl_ctl = &ana_pll->sys_pll3_gnrl_ctl; |
| 194 | pll_div_ctl = &ana_pll->sys_pll3_div_ctl; |
| 195 | pll_clke_masks = INTPLL_CLKE_MASK; |
| 196 | break; |
| 197 | case ANATOP_ARM_PLL: |
| 198 | pll_gnrl_ctl = &ana_pll->arm_pll_gnrl_ctl; |
| 199 | pll_div_ctl = &ana_pll->arm_pll_div_ctl; |
| 200 | pll_clke_masks = INTPLL_CLKE_MASK; |
| 201 | break; |
| 202 | case ANATOP_GPU_PLL: |
| 203 | pll_gnrl_ctl = &ana_pll->gpu_pll_gnrl_ctl; |
| 204 | pll_div_ctl = &ana_pll->gpu_pll_div_ctl; |
| 205 | pll_clke_masks = INTPLL_CLKE_MASK; |
| 206 | break; |
| 207 | case ANATOP_VPU_PLL: |
| 208 | pll_gnrl_ctl = &ana_pll->vpu_pll_gnrl_ctl; |
| 209 | pll_div_ctl = &ana_pll->vpu_pll_div_ctl; |
| 210 | pll_clke_masks = INTPLL_CLKE_MASK; |
| 211 | break; |
| 212 | default: |
| 213 | return -EINVAL; |
| 214 | }; |
| 215 | |
| 216 | switch (freq) { |
| 217 | case MHZ(600): |
| 218 | /* 24 * 0x12c / 3 / 2 ^ 2 */ |
| 219 | pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x12c) | |
| 220 | INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(2); |
| 221 | break; |
| 222 | case MHZ(750): |
| 223 | /* 24 * 0xfa / 2 / 2 ^ 2 */ |
| 224 | pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) | |
| 225 | INTPLL_PRE_DIV_VAL(2) | INTPLL_POST_DIV_VAL(2); |
| 226 | break; |
| 227 | case MHZ(800): |
| 228 | /* 24 * 0x190 / 3 / 2 ^ 2 */ |
| 229 | pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x190) | |
| 230 | INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(2); |
| 231 | break; |
| 232 | case MHZ(1000): |
| 233 | /* 24 * 0xfa / 3 / 2 ^ 1 */ |
| 234 | pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) | |
| 235 | INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(1); |
| 236 | break; |
| 237 | case MHZ(1200): |
| 238 | /* 24 * 0xc8 / 2 / 2 ^ 1 */ |
| 239 | pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xc8) | |
| 240 | INTPLL_PRE_DIV_VAL(2) | INTPLL_POST_DIV_VAL(1); |
| 241 | break; |
| 242 | case MHZ(2000): |
| 243 | /* 24 * 0xfa / 3 / 2 ^ 0 */ |
| 244 | pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) | |
| 245 | INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(0); |
| 246 | break; |
| 247 | default: |
| 248 | return -EINVAL; |
| 249 | }; |
| 250 | /* Bypass clock and set lock to pll output lock */ |
| 251 | setbits_le32(pll_gnrl_ctl, INTPLL_BYPASS_MASK | INTPLL_LOCK_SEL_MASK); |
| 252 | /* Enable reset */ |
| 253 | clrbits_le32(pll_gnrl_ctl, INTPLL_RST_MASK); |
| 254 | /* Configure */ |
| 255 | writel(pll_div_ctl_val, pll_div_ctl); |
| 256 | |
| 257 | __udelay(100); |
| 258 | |
| 259 | /* Disable reset */ |
| 260 | setbits_le32(pll_gnrl_ctl, INTPLL_RST_MASK); |
| 261 | /* Wait Lock */ |
| 262 | while (!(readl(pll_gnrl_ctl) & INTPLL_LOCK_MASK)) |
| 263 | ; |
| 264 | /* Clear bypass */ |
| 265 | clrbits_le32(pll_gnrl_ctl, INTPLL_BYPASS_MASK); |
| 266 | setbits_le32(pll_gnrl_ctl, pll_clke_masks); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 271 | void init_uart_clk(u32 index) |
| 272 | { |
| 273 | /* |
| 274 | * set uart clock root |
| 275 | * 24M OSC |
| 276 | */ |
| 277 | switch (index) { |
| 278 | case 0: |
| 279 | clock_enable(CCGR_UART1, 0); |
| 280 | clock_set_target_val(UART1_CLK_ROOT, CLK_ROOT_ON | |
| 281 | CLK_ROOT_SOURCE_SEL(0)); |
| 282 | clock_enable(CCGR_UART1, 1); |
| 283 | return; |
| 284 | case 1: |
| 285 | clock_enable(CCGR_UART2, 0); |
| 286 | clock_set_target_val(UART2_CLK_ROOT, CLK_ROOT_ON | |
| 287 | CLK_ROOT_SOURCE_SEL(0)); |
| 288 | clock_enable(CCGR_UART2, 1); |
| 289 | return; |
| 290 | case 2: |
| 291 | clock_enable(CCGR_UART3, 0); |
| 292 | clock_set_target_val(UART3_CLK_ROOT, CLK_ROOT_ON | |
| 293 | CLK_ROOT_SOURCE_SEL(0)); |
| 294 | clock_enable(CCGR_UART3, 1); |
| 295 | return; |
| 296 | case 3: |
| 297 | clock_enable(CCGR_UART4, 0); |
| 298 | clock_set_target_val(UART4_CLK_ROOT, CLK_ROOT_ON | |
| 299 | CLK_ROOT_SOURCE_SEL(0)); |
| 300 | clock_enable(CCGR_UART4, 1); |
| 301 | return; |
| 302 | default: |
| 303 | printf("Invalid uart index\n"); |
| 304 | return; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void init_wdog_clk(void) |
| 309 | { |
| 310 | clock_enable(CCGR_WDOG1, 0); |
| 311 | clock_enable(CCGR_WDOG2, 0); |
| 312 | clock_enable(CCGR_WDOG3, 0); |
| 313 | clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON | |
| 314 | CLK_ROOT_SOURCE_SEL(0)); |
| 315 | clock_enable(CCGR_WDOG1, 1); |
| 316 | clock_enable(CCGR_WDOG2, 1); |
| 317 | clock_enable(CCGR_WDOG3, 1); |
| 318 | } |
| 319 | |
| 320 | int clock_init(void) |
| 321 | { |
| 322 | u32 val_cfg0; |
| 323 | |
| 324 | /* |
| 325 | * The gate is not exported to clk tree, so configure them here. |
| 326 | * According to ANAMIX SPEC |
| 327 | * sys pll1 fixed at 800MHz |
| 328 | * sys pll2 fixed at 1GHz |
| 329 | * Here we only enable the outputs. |
| 330 | */ |
| 331 | val_cfg0 = readl(&ana_pll->sys_pll1_gnrl_ctl); |
| 332 | val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK | |
| 333 | INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK | |
| 334 | INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK | |
| 335 | INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK | |
| 336 | INTPLL_DIV20_CLKE_MASK; |
| 337 | writel(val_cfg0, &ana_pll->sys_pll1_gnrl_ctl); |
| 338 | |
| 339 | val_cfg0 = readl(&ana_pll->sys_pll2_gnrl_ctl); |
| 340 | val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK | |
| 341 | INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK | |
| 342 | INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK | |
| 343 | INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK | |
| 344 | INTPLL_DIV20_CLKE_MASK; |
| 345 | writel(val_cfg0, &ana_pll->sys_pll2_gnrl_ctl); |
| 346 | |
Peng Fan | 4e36e33 | 2020-07-09 11:06:24 +0800 | [diff] [blame] | 347 | /* Configure ARM at 1.2GHz */ |
| 348 | clock_set_target_val(ARM_A53_CLK_ROOT, CLK_ROOT_ON | |
| 349 | CLK_ROOT_SOURCE_SEL(2)); |
| 350 | |
| 351 | intpll_configure(ANATOP_ARM_PLL, MHZ(1200)); |
| 352 | |
| 353 | /* Bypass CCM A53 ROOT, Switch to ARM PLL -> MUX-> CPU */ |
| 354 | clock_set_target_val(CORE_SEL_CFG, CLK_ROOT_SOURCE_SEL(1)); |
| 355 | |
Peng Fan | e565393 | 2020-07-09 11:18:50 +0800 | [diff] [blame^] | 356 | if (is_imx8mn() || is_imx8mp()) |
| 357 | intpll_configure(ANATOP_SYSTEM_PLL3, MHZ(600)); |
| 358 | else |
| 359 | intpll_configure(ANATOP_SYSTEM_PLL3, MHZ(750)); |
| 360 | |
| 361 | #ifdef CONFIG_IMX8MP |
| 362 | /* 8MP ROM already set NOC to 800Mhz, only need to configure NOC_IO clk to 600Mhz */ |
| 363 | /* 8MP ROM already set GIC to 400Mhz, system_pll1_800m with div = 2 */ |
| 364 | clock_set_target_val(NOC_IO_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(2)); |
| 365 | #else |
| 366 | clock_set_target_val(NOC_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(2)); |
| 367 | |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 368 | /* config GIC to sys_pll2_100m */ |
| 369 | clock_enable(CCGR_GIC, 0); |
| 370 | clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON | |
| 371 | CLK_ROOT_SOURCE_SEL(3)); |
| 372 | clock_enable(CCGR_GIC, 1); |
Peng Fan | e565393 | 2020-07-09 11:18:50 +0800 | [diff] [blame^] | 373 | #endif |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 374 | |
| 375 | clock_set_target_val(NAND_USDHC_BUS_CLK_ROOT, CLK_ROOT_ON | |
| 376 | CLK_ROOT_SOURCE_SEL(1)); |
| 377 | |
| 378 | clock_enable(CCGR_DDR1, 0); |
| 379 | clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON | |
| 380 | CLK_ROOT_SOURCE_SEL(1)); |
| 381 | clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | |
| 382 | CLK_ROOT_SOURCE_SEL(1)); |
| 383 | clock_enable(CCGR_DDR1, 1); |
| 384 | |
| 385 | init_wdog_clk(); |
| 386 | |
| 387 | clock_enable(CCGR_TEMP_SENSOR, 1); |
| 388 | |
| 389 | clock_enable(CCGR_SEC_DEBUG, 1); |
| 390 | |
| 391 | return 0; |
| 392 | }; |
| 393 | |
| 394 | u32 imx_get_uartclk(void) |
| 395 | { |
| 396 | return 24000000U; |
| 397 | } |
| 398 | |
Alifer Moraes | 3993110 | 2020-01-14 15:54:59 -0300 | [diff] [blame] | 399 | static u32 decode_intpll(enum clk_root_src intpll) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 400 | { |
| 401 | u32 pll_gnrl_ctl, pll_div_ctl, pll_clke_mask; |
| 402 | u32 main_div, pre_div, post_div, div; |
| 403 | u64 freq; |
| 404 | |
| 405 | switch (intpll) { |
| 406 | case ARM_PLL_CLK: |
| 407 | pll_gnrl_ctl = readl(&ana_pll->arm_pll_gnrl_ctl); |
| 408 | pll_div_ctl = readl(&ana_pll->arm_pll_div_ctl); |
| 409 | break; |
| 410 | case GPU_PLL_CLK: |
| 411 | pll_gnrl_ctl = readl(&ana_pll->gpu_pll_gnrl_ctl); |
| 412 | pll_div_ctl = readl(&ana_pll->gpu_pll_div_ctl); |
| 413 | break; |
| 414 | case VPU_PLL_CLK: |
| 415 | pll_gnrl_ctl = readl(&ana_pll->vpu_pll_gnrl_ctl); |
| 416 | pll_div_ctl = readl(&ana_pll->vpu_pll_div_ctl); |
| 417 | break; |
| 418 | case SYSTEM_PLL1_800M_CLK: |
| 419 | case SYSTEM_PLL1_400M_CLK: |
| 420 | case SYSTEM_PLL1_266M_CLK: |
| 421 | case SYSTEM_PLL1_200M_CLK: |
| 422 | case SYSTEM_PLL1_160M_CLK: |
| 423 | case SYSTEM_PLL1_133M_CLK: |
| 424 | case SYSTEM_PLL1_100M_CLK: |
| 425 | case SYSTEM_PLL1_80M_CLK: |
| 426 | case SYSTEM_PLL1_40M_CLK: |
| 427 | pll_gnrl_ctl = readl(&ana_pll->sys_pll1_gnrl_ctl); |
| 428 | pll_div_ctl = readl(&ana_pll->sys_pll1_div_ctl); |
| 429 | break; |
| 430 | case SYSTEM_PLL2_1000M_CLK: |
| 431 | case SYSTEM_PLL2_500M_CLK: |
| 432 | case SYSTEM_PLL2_333M_CLK: |
| 433 | case SYSTEM_PLL2_250M_CLK: |
| 434 | case SYSTEM_PLL2_200M_CLK: |
| 435 | case SYSTEM_PLL2_166M_CLK: |
| 436 | case SYSTEM_PLL2_125M_CLK: |
| 437 | case SYSTEM_PLL2_100M_CLK: |
| 438 | case SYSTEM_PLL2_50M_CLK: |
| 439 | pll_gnrl_ctl = readl(&ana_pll->sys_pll2_gnrl_ctl); |
| 440 | pll_div_ctl = readl(&ana_pll->sys_pll2_div_ctl); |
| 441 | break; |
| 442 | case SYSTEM_PLL3_CLK: |
| 443 | pll_gnrl_ctl = readl(&ana_pll->sys_pll3_gnrl_ctl); |
| 444 | pll_div_ctl = readl(&ana_pll->sys_pll3_div_ctl); |
| 445 | break; |
| 446 | default: |
| 447 | return -EINVAL; |
| 448 | } |
| 449 | |
| 450 | /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */ |
| 451 | if ((pll_gnrl_ctl & INTPLL_REF_CLK_SEL_MASK) != 0) |
| 452 | return 0; |
| 453 | |
| 454 | if ((pll_gnrl_ctl & INTPLL_RST_MASK) == 0) |
| 455 | return 0; |
| 456 | |
| 457 | /* |
| 458 | * When BYPASS is equal to 1, PLL enters the bypass mode |
| 459 | * regardless of the values of RESETB |
| 460 | */ |
| 461 | if (pll_gnrl_ctl & INTPLL_BYPASS_MASK) |
| 462 | return 24000000u; |
| 463 | |
| 464 | if (!(pll_gnrl_ctl & INTPLL_LOCK_MASK)) { |
| 465 | puts("pll not locked\n"); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | switch (intpll) { |
| 470 | case ARM_PLL_CLK: |
| 471 | case GPU_PLL_CLK: |
| 472 | case VPU_PLL_CLK: |
| 473 | case SYSTEM_PLL3_CLK: |
| 474 | case SYSTEM_PLL1_800M_CLK: |
| 475 | case SYSTEM_PLL2_1000M_CLK: |
| 476 | pll_clke_mask = INTPLL_CLKE_MASK; |
| 477 | div = 1; |
| 478 | break; |
| 479 | |
| 480 | case SYSTEM_PLL1_400M_CLK: |
| 481 | case SYSTEM_PLL2_500M_CLK: |
| 482 | pll_clke_mask = INTPLL_DIV2_CLKE_MASK; |
| 483 | div = 2; |
| 484 | break; |
| 485 | |
| 486 | case SYSTEM_PLL1_266M_CLK: |
| 487 | case SYSTEM_PLL2_333M_CLK: |
| 488 | pll_clke_mask = INTPLL_DIV3_CLKE_MASK; |
| 489 | div = 3; |
| 490 | break; |
| 491 | |
| 492 | case SYSTEM_PLL1_200M_CLK: |
| 493 | case SYSTEM_PLL2_250M_CLK: |
| 494 | pll_clke_mask = INTPLL_DIV4_CLKE_MASK; |
| 495 | div = 4; |
| 496 | break; |
| 497 | |
| 498 | case SYSTEM_PLL1_160M_CLK: |
| 499 | case SYSTEM_PLL2_200M_CLK: |
| 500 | pll_clke_mask = INTPLL_DIV5_CLKE_MASK; |
| 501 | div = 5; |
| 502 | break; |
| 503 | |
| 504 | case SYSTEM_PLL1_133M_CLK: |
| 505 | case SYSTEM_PLL2_166M_CLK: |
| 506 | pll_clke_mask = INTPLL_DIV6_CLKE_MASK; |
| 507 | div = 6; |
| 508 | break; |
| 509 | |
| 510 | case SYSTEM_PLL1_100M_CLK: |
| 511 | case SYSTEM_PLL2_125M_CLK: |
| 512 | pll_clke_mask = INTPLL_DIV8_CLKE_MASK; |
| 513 | div = 8; |
| 514 | break; |
| 515 | |
| 516 | case SYSTEM_PLL1_80M_CLK: |
| 517 | case SYSTEM_PLL2_100M_CLK: |
| 518 | pll_clke_mask = INTPLL_DIV10_CLKE_MASK; |
| 519 | div = 10; |
| 520 | break; |
| 521 | |
| 522 | case SYSTEM_PLL1_40M_CLK: |
| 523 | case SYSTEM_PLL2_50M_CLK: |
| 524 | pll_clke_mask = INTPLL_DIV20_CLKE_MASK; |
| 525 | div = 20; |
| 526 | break; |
| 527 | default: |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | if ((pll_gnrl_ctl & pll_clke_mask) == 0) |
| 532 | return 0; |
| 533 | |
| 534 | main_div = (pll_div_ctl & INTPLL_MAIN_DIV_MASK) >> |
| 535 | INTPLL_MAIN_DIV_SHIFT; |
| 536 | pre_div = (pll_div_ctl & INTPLL_PRE_DIV_MASK) >> |
| 537 | INTPLL_PRE_DIV_SHIFT; |
| 538 | post_div = (pll_div_ctl & INTPLL_POST_DIV_MASK) >> |
| 539 | INTPLL_POST_DIV_SHIFT; |
| 540 | |
| 541 | /* FFVCO = (m * FFIN) / p, FFOUT = (m * FFIN) / (p * 2^s) */ |
| 542 | freq = 24000000ULL * main_div; |
| 543 | return lldiv(freq, pre_div * (1 << post_div) * div); |
| 544 | } |
| 545 | |
Alifer Moraes | 3993110 | 2020-01-14 15:54:59 -0300 | [diff] [blame] | 546 | static u32 decode_fracpll(enum clk_root_src frac_pll) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 547 | { |
| 548 | u32 pll_gnrl_ctl, pll_fdiv_ctl0, pll_fdiv_ctl1; |
| 549 | u32 main_div, pre_div, post_div, k; |
| 550 | |
| 551 | switch (frac_pll) { |
| 552 | case DRAM_PLL1_CLK: |
| 553 | pll_gnrl_ctl = readl(&ana_pll->dram_pll_gnrl_ctl); |
| 554 | pll_fdiv_ctl0 = readl(&ana_pll->dram_pll_fdiv_ctl0); |
| 555 | pll_fdiv_ctl1 = readl(&ana_pll->dram_pll_fdiv_ctl1); |
| 556 | break; |
| 557 | case AUDIO_PLL1_CLK: |
| 558 | pll_gnrl_ctl = readl(&ana_pll->audio_pll1_gnrl_ctl); |
| 559 | pll_fdiv_ctl0 = readl(&ana_pll->audio_pll1_fdiv_ctl0); |
| 560 | pll_fdiv_ctl1 = readl(&ana_pll->audio_pll1_fdiv_ctl1); |
| 561 | break; |
| 562 | case AUDIO_PLL2_CLK: |
| 563 | pll_gnrl_ctl = readl(&ana_pll->audio_pll2_gnrl_ctl); |
| 564 | pll_fdiv_ctl0 = readl(&ana_pll->audio_pll2_fdiv_ctl0); |
| 565 | pll_fdiv_ctl1 = readl(&ana_pll->audio_pll2_fdiv_ctl1); |
| 566 | break; |
| 567 | case VIDEO_PLL_CLK: |
| 568 | pll_gnrl_ctl = readl(&ana_pll->video_pll1_gnrl_ctl); |
| 569 | pll_fdiv_ctl0 = readl(&ana_pll->video_pll1_fdiv_ctl0); |
| 570 | pll_fdiv_ctl1 = readl(&ana_pll->video_pll1_fdiv_ctl1); |
| 571 | break; |
| 572 | default: |
| 573 | printf("Not supported\n"); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */ |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 578 | if ((pll_gnrl_ctl & GENMASK(1, 0)) != 0) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 579 | return 0; |
| 580 | |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 581 | if ((pll_gnrl_ctl & RST_MASK) == 0) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 582 | return 0; |
| 583 | /* |
| 584 | * When BYPASS is equal to 1, PLL enters the bypass mode |
| 585 | * regardless of the values of RESETB |
| 586 | */ |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 587 | if (pll_gnrl_ctl & BYPASS_MASK) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 588 | return 24000000u; |
| 589 | |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 590 | if (!(pll_gnrl_ctl & LOCK_STATUS)) { |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 591 | puts("pll not locked\n"); |
| 592 | return 0; |
| 593 | } |
| 594 | |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 595 | if (!(pll_gnrl_ctl & CLKE_MASK)) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 596 | return 0; |
| 597 | |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 598 | main_div = (pll_fdiv_ctl0 & MDIV_MASK) >> |
| 599 | MDIV_SHIFT; |
| 600 | pre_div = (pll_fdiv_ctl0 & PDIV_MASK) >> |
| 601 | PDIV_SHIFT; |
| 602 | post_div = (pll_fdiv_ctl0 & SDIV_MASK) >> |
| 603 | SDIV_SHIFT; |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 604 | |
Ye Li | cc643ea | 2020-03-23 19:54:29 -0700 | [diff] [blame] | 605 | k = pll_fdiv_ctl1 & KDIV_MASK; |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 606 | |
| 607 | return lldiv((main_div * 65536 + k) * 24000000ULL, |
| 608 | 65536 * pre_div * (1 << post_div)); |
| 609 | } |
| 610 | |
Alifer Moraes | 3993110 | 2020-01-14 15:54:59 -0300 | [diff] [blame] | 611 | static u32 get_root_src_clk(enum clk_root_src root_src) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 612 | { |
| 613 | switch (root_src) { |
| 614 | case OSC_24M_CLK: |
| 615 | return 24000000u; |
| 616 | case OSC_HDMI_CLK: |
| 617 | return 26000000u; |
| 618 | case OSC_32K_CLK: |
| 619 | return 32000u; |
| 620 | case ARM_PLL_CLK: |
| 621 | case GPU_PLL_CLK: |
| 622 | case VPU_PLL_CLK: |
| 623 | case SYSTEM_PLL1_800M_CLK: |
| 624 | case SYSTEM_PLL1_400M_CLK: |
| 625 | case SYSTEM_PLL1_266M_CLK: |
| 626 | case SYSTEM_PLL1_200M_CLK: |
| 627 | case SYSTEM_PLL1_160M_CLK: |
| 628 | case SYSTEM_PLL1_133M_CLK: |
| 629 | case SYSTEM_PLL1_100M_CLK: |
| 630 | case SYSTEM_PLL1_80M_CLK: |
| 631 | case SYSTEM_PLL1_40M_CLK: |
| 632 | case SYSTEM_PLL2_1000M_CLK: |
| 633 | case SYSTEM_PLL2_500M_CLK: |
| 634 | case SYSTEM_PLL2_333M_CLK: |
| 635 | case SYSTEM_PLL2_250M_CLK: |
| 636 | case SYSTEM_PLL2_200M_CLK: |
| 637 | case SYSTEM_PLL2_166M_CLK: |
| 638 | case SYSTEM_PLL2_125M_CLK: |
| 639 | case SYSTEM_PLL2_100M_CLK: |
| 640 | case SYSTEM_PLL2_50M_CLK: |
| 641 | case SYSTEM_PLL3_CLK: |
| 642 | return decode_intpll(root_src); |
| 643 | case DRAM_PLL1_CLK: |
| 644 | case AUDIO_PLL1_CLK: |
| 645 | case AUDIO_PLL2_CLK: |
| 646 | case VIDEO_PLL_CLK: |
| 647 | return decode_fracpll(root_src); |
Peng Fan | 4e36e33 | 2020-07-09 11:06:24 +0800 | [diff] [blame] | 648 | case ARM_A53_ALT_CLK: |
| 649 | return get_root_clk(ARM_A53_CLK_ROOT); |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 650 | default: |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Alifer Moraes | 3993110 | 2020-01-14 15:54:59 -0300 | [diff] [blame] | 657 | static u32 get_root_clk(enum clk_root_index clock_id) |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 658 | { |
| 659 | enum clk_root_src root_src; |
| 660 | u32 post_podf, pre_podf, root_src_clk; |
| 661 | |
| 662 | if (clock_root_enabled(clock_id) <= 0) |
| 663 | return 0; |
| 664 | |
| 665 | if (clock_get_prediv(clock_id, &pre_podf) < 0) |
| 666 | return 0; |
| 667 | |
| 668 | if (clock_get_postdiv(clock_id, &post_podf) < 0) |
| 669 | return 0; |
| 670 | |
| 671 | if (clock_get_src(clock_id, &root_src) < 0) |
| 672 | return 0; |
| 673 | |
| 674 | root_src_clk = get_root_src_clk(root_src); |
| 675 | |
| 676 | return root_src_clk / (post_podf + 1) / (pre_podf + 1); |
| 677 | } |
| 678 | |
Peng Fan | 4e36e33 | 2020-07-09 11:06:24 +0800 | [diff] [blame] | 679 | u32 get_arm_core_clk(void) |
| 680 | { |
| 681 | enum clk_root_src root_src; |
| 682 | u32 root_src_clk; |
| 683 | |
| 684 | if (clock_get_src(CORE_SEL_CFG, &root_src) < 0) |
| 685 | return 0; |
| 686 | |
| 687 | root_src_clk = get_root_src_clk(root_src); |
| 688 | |
| 689 | return root_src_clk; |
| 690 | } |
| 691 | |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 692 | u32 mxc_get_clock(enum mxc_clock clk) |
| 693 | { |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 694 | u32 val; |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 695 | |
| 696 | switch (clk) { |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 697 | case MXC_ARM_CLK: |
Peng Fan | 4e36e33 | 2020-07-09 11:06:24 +0800 | [diff] [blame] | 698 | return get_arm_core_clk(); |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 699 | case MXC_IPG_CLK: |
| 700 | clock_get_target_val(IPG_CLK_ROOT, &val); |
| 701 | val = val & 0x3; |
| 702 | return get_root_clk(AHB_CLK_ROOT) / 2 / (val + 1); |
| 703 | case MXC_CSPI_CLK: |
| 704 | return get_root_clk(ECSPI1_CLK_ROOT); |
| 705 | case MXC_ESDHC_CLK: |
| 706 | return get_root_clk(USDHC1_CLK_ROOT); |
| 707 | case MXC_ESDHC2_CLK: |
| 708 | return get_root_clk(USDHC2_CLK_ROOT); |
| 709 | case MXC_ESDHC3_CLK: |
| 710 | return get_root_clk(USDHC3_CLK_ROOT); |
| 711 | case MXC_I2C_CLK: |
| 712 | return get_root_clk(I2C1_CLK_ROOT); |
| 713 | case MXC_UART_CLK: |
| 714 | return get_root_clk(UART1_CLK_ROOT); |
| 715 | case MXC_QSPI_CLK: |
| 716 | return get_root_clk(QSPI_CLK_ROOT); |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 717 | default: |
Peng Fan | 60c29bb | 2019-12-30 16:52:30 +0800 | [diff] [blame] | 718 | printf("Unsupported mxc_clock %d\n", clk); |
| 719 | break; |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
Marek Vasut | 363725d | 2020-04-24 21:37:26 +0200 | [diff] [blame] | 724 | |
| 725 | #ifdef CONFIG_FEC_MXC |
| 726 | int set_clk_enet(enum enet_freq type) |
| 727 | { |
| 728 | u32 target; |
| 729 | u32 enet1_ref; |
| 730 | |
| 731 | switch (type) { |
| 732 | case ENET_125MHZ: |
| 733 | enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK; |
| 734 | break; |
| 735 | case ENET_50MHZ: |
| 736 | enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK; |
| 737 | break; |
| 738 | case ENET_25MHZ: |
| 739 | enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK; |
| 740 | break; |
| 741 | default: |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | |
| 745 | /* disable the clock first */ |
| 746 | clock_enable(CCGR_ENET1, 0); |
| 747 | clock_enable(CCGR_SIM_ENET, 0); |
| 748 | |
| 749 | /* set enet axi clock 266Mhz */ |
| 750 | target = CLK_ROOT_ON | ENET_AXI_CLK_ROOT_FROM_SYS1_PLL_266M | |
| 751 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 752 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 753 | clock_set_target_val(ENET_AXI_CLK_ROOT, target); |
| 754 | |
| 755 | target = CLK_ROOT_ON | enet1_ref | |
| 756 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 757 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 758 | clock_set_target_val(ENET_REF_CLK_ROOT, target); |
| 759 | |
| 760 | target = CLK_ROOT_ON | |
| 761 | ENET1_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK | |
| 762 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 763 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 764 | clock_set_target_val(ENET_TIMER_CLK_ROOT, target); |
| 765 | |
| 766 | /* enable clock */ |
| 767 | clock_enable(CCGR_SIM_ENET, 1); |
| 768 | clock_enable(CCGR_ENET1, 1); |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | #endif |