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> |
| 13 | #include <clk.h> |
| 14 | #include <clk-uclass.h> |
| 15 | #include <dt-bindings/clock/imx8mm-clock.h> |
| 16 | #include <div64.h> |
| 17 | #include <errno.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | static struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR; |
| 22 | |
| 23 | void enable_ocotp_clk(unsigned char enable) |
| 24 | { |
| 25 | struct clk *clkp; |
| 26 | int ret; |
| 27 | |
| 28 | ret = clk_get_by_id(IMX8MM_CLK_OCOTP_ROOT, &clkp); |
| 29 | if (ret) { |
| 30 | printf("%s: err: %d\n", __func__, ret); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | enable ? clk_enable(clkp) : clk_disable(clkp); |
| 35 | } |
| 36 | |
| 37 | int enable_i2c_clk(unsigned char enable, unsigned i2c_num) |
| 38 | { |
| 39 | struct clk *clkp; |
| 40 | int ret; |
| 41 | |
| 42 | ret = clk_get_by_id(IMX8MM_CLK_I2C1_ROOT + i2c_num, &clkp); |
| 43 | if (ret) { |
| 44 | printf("%s: err: %d\n", __func__, ret); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | return enable ? clk_enable(clkp) : clk_disable(clkp); |
| 49 | } |
| 50 | |
| 51 | #ifdef CONFIG_SPL_BUILD |
| 52 | static struct imx_int_pll_rate_table imx8mm_fracpll_tbl[] = { |
Peng Fan | d29bf22 | 2019-12-27 11:40:55 +0800 | [diff] [blame^] | 53 | PLL_1443X_RATE(1000000000U, 250, 3, 1, 0), |
Peng Fan | 9987846 | 2019-08-27 06:25:51 +0000 | [diff] [blame] | 54 | PLL_1443X_RATE(800000000U, 300, 9, 0, 0), |
| 55 | PLL_1443X_RATE(750000000U, 250, 8, 0, 0), |
| 56 | PLL_1443X_RATE(650000000U, 325, 3, 2, 0), |
| 57 | PLL_1443X_RATE(600000000U, 300, 3, 2, 0), |
| 58 | PLL_1443X_RATE(594000000U, 99, 1, 2, 0), |
| 59 | PLL_1443X_RATE(400000000U, 300, 9, 1, 0), |
| 60 | PLL_1443X_RATE(266666667U, 400, 9, 2, 0), |
| 61 | PLL_1443X_RATE(167000000U, 334, 3, 4, 0), |
| 62 | PLL_1443X_RATE(100000000U, 300, 9, 3, 0), |
| 63 | }; |
| 64 | |
| 65 | int fracpll_configure(enum pll_clocks pll, u32 freq) |
| 66 | { |
| 67 | int i; |
| 68 | u32 tmp, div_val; |
| 69 | void *pll_base; |
| 70 | struct imx_int_pll_rate_table *rate; |
| 71 | |
| 72 | for (i = 0; i < ARRAY_SIZE(imx8mm_fracpll_tbl); i++) { |
| 73 | if (freq == imx8mm_fracpll_tbl[i].rate) |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | if (i == ARRAY_SIZE(imx8mm_fracpll_tbl)) { |
| 78 | printf("No matched freq table %u\n", freq); |
| 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | rate = &imx8mm_fracpll_tbl[i]; |
| 83 | |
| 84 | switch (pll) { |
| 85 | case ANATOP_DRAM_PLL: |
| 86 | setbits_le32(GPC_BASE_ADDR + 0xEC, 1 << 7); |
| 87 | setbits_le32(GPC_BASE_ADDR + 0xF8, 1 << 5); |
| 88 | writel(SRC_DDR1_ENABLE_MASK, SRC_BASE_ADDR + 0x1004); |
| 89 | |
| 90 | pll_base = &ana_pll->dram_pll_gnrl_ctl; |
| 91 | break; |
| 92 | case ANATOP_VIDEO_PLL: |
| 93 | pll_base = &ana_pll->video_pll1_gnrl_ctl; |
| 94 | break; |
| 95 | default: |
| 96 | return 0; |
| 97 | } |
| 98 | /* Bypass clock and set lock to pll output lock */ |
| 99 | tmp = readl(pll_base); |
| 100 | tmp |= BYPASS_MASK; |
| 101 | writel(tmp, pll_base); |
| 102 | |
| 103 | /* Enable RST */ |
| 104 | tmp &= ~RST_MASK; |
| 105 | writel(tmp, pll_base); |
| 106 | |
| 107 | div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) | |
| 108 | (rate->sdiv << SDIV_SHIFT); |
| 109 | writel(div_val, pll_base + 4); |
| 110 | writel(rate->kdiv << KDIV_SHIFT, pll_base + 8); |
| 111 | |
| 112 | __udelay(100); |
| 113 | |
| 114 | /* Disable RST */ |
| 115 | tmp |= RST_MASK; |
| 116 | writel(tmp, pll_base); |
| 117 | |
| 118 | /* Wait Lock*/ |
| 119 | while (!(readl(pll_base) & LOCK_STATUS)) |
| 120 | ; |
| 121 | |
| 122 | /* Bypass */ |
| 123 | tmp &= ~BYPASS_MASK; |
| 124 | writel(tmp, pll_base); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | void dram_pll_init(ulong pll_val) |
| 130 | { |
| 131 | fracpll_configure(ANATOP_DRAM_PLL, pll_val); |
| 132 | } |
| 133 | |
| 134 | static struct dram_bypass_clk_setting imx8mm_dram_bypass_tbl[] = { |
| 135 | DRAM_BYPASS_ROOT_CONFIG(MHZ(100), 2, CLK_ROOT_PRE_DIV1, 2, |
| 136 | CLK_ROOT_PRE_DIV2), |
| 137 | DRAM_BYPASS_ROOT_CONFIG(MHZ(250), 3, CLK_ROOT_PRE_DIV2, 2, |
| 138 | CLK_ROOT_PRE_DIV2), |
| 139 | DRAM_BYPASS_ROOT_CONFIG(MHZ(400), 1, CLK_ROOT_PRE_DIV2, 3, |
| 140 | CLK_ROOT_PRE_DIV2), |
| 141 | }; |
| 142 | |
| 143 | void dram_enable_bypass(ulong clk_val) |
| 144 | { |
| 145 | int i; |
| 146 | struct dram_bypass_clk_setting *config; |
| 147 | |
| 148 | for (i = 0; i < ARRAY_SIZE(imx8mm_dram_bypass_tbl); i++) { |
| 149 | if (clk_val == imx8mm_dram_bypass_tbl[i].clk) |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | if (i == ARRAY_SIZE(imx8mm_dram_bypass_tbl)) { |
| 154 | printf("No matched freq table %lu\n", clk_val); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | config = &imx8mm_dram_bypass_tbl[i]; |
| 159 | |
| 160 | clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON | |
| 161 | CLK_ROOT_SOURCE_SEL(config->alt_root_sel) | |
| 162 | CLK_ROOT_PRE_DIV(config->alt_pre_div)); |
| 163 | clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | |
| 164 | CLK_ROOT_SOURCE_SEL(config->apb_root_sel) | |
| 165 | CLK_ROOT_PRE_DIV(config->apb_pre_div)); |
| 166 | clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON | |
| 167 | CLK_ROOT_SOURCE_SEL(1)); |
| 168 | } |
| 169 | |
| 170 | void dram_disable_bypass(void) |
| 171 | { |
| 172 | clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON | |
| 173 | CLK_ROOT_SOURCE_SEL(0)); |
| 174 | clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | |
| 175 | CLK_ROOT_SOURCE_SEL(4) | |
| 176 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV5)); |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | void init_uart_clk(u32 index) |
| 181 | { |
| 182 | /* |
| 183 | * set uart clock root |
| 184 | * 24M OSC |
| 185 | */ |
| 186 | switch (index) { |
| 187 | case 0: |
| 188 | clock_enable(CCGR_UART1, 0); |
| 189 | clock_set_target_val(UART1_CLK_ROOT, CLK_ROOT_ON | |
| 190 | CLK_ROOT_SOURCE_SEL(0)); |
| 191 | clock_enable(CCGR_UART1, 1); |
| 192 | return; |
| 193 | case 1: |
| 194 | clock_enable(CCGR_UART2, 0); |
| 195 | clock_set_target_val(UART2_CLK_ROOT, CLK_ROOT_ON | |
| 196 | CLK_ROOT_SOURCE_SEL(0)); |
| 197 | clock_enable(CCGR_UART2, 1); |
| 198 | return; |
| 199 | case 2: |
| 200 | clock_enable(CCGR_UART3, 0); |
| 201 | clock_set_target_val(UART3_CLK_ROOT, CLK_ROOT_ON | |
| 202 | CLK_ROOT_SOURCE_SEL(0)); |
| 203 | clock_enable(CCGR_UART3, 1); |
| 204 | return; |
| 205 | case 3: |
| 206 | clock_enable(CCGR_UART4, 0); |
| 207 | clock_set_target_val(UART4_CLK_ROOT, CLK_ROOT_ON | |
| 208 | CLK_ROOT_SOURCE_SEL(0)); |
| 209 | clock_enable(CCGR_UART4, 1); |
| 210 | return; |
| 211 | default: |
| 212 | printf("Invalid uart index\n"); |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void init_wdog_clk(void) |
| 218 | { |
| 219 | clock_enable(CCGR_WDOG1, 0); |
| 220 | clock_enable(CCGR_WDOG2, 0); |
| 221 | clock_enable(CCGR_WDOG3, 0); |
| 222 | clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON | |
| 223 | CLK_ROOT_SOURCE_SEL(0)); |
| 224 | clock_enable(CCGR_WDOG1, 1); |
| 225 | clock_enable(CCGR_WDOG2, 1); |
| 226 | clock_enable(CCGR_WDOG3, 1); |
| 227 | } |
| 228 | |
| 229 | int clock_init(void) |
| 230 | { |
| 231 | u32 val_cfg0; |
| 232 | |
| 233 | /* |
| 234 | * The gate is not exported to clk tree, so configure them here. |
| 235 | * According to ANAMIX SPEC |
| 236 | * sys pll1 fixed at 800MHz |
| 237 | * sys pll2 fixed at 1GHz |
| 238 | * Here we only enable the outputs. |
| 239 | */ |
| 240 | val_cfg0 = readl(&ana_pll->sys_pll1_gnrl_ctl); |
| 241 | val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK | |
| 242 | INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK | |
| 243 | INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK | |
| 244 | INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK | |
| 245 | INTPLL_DIV20_CLKE_MASK; |
| 246 | writel(val_cfg0, &ana_pll->sys_pll1_gnrl_ctl); |
| 247 | |
| 248 | val_cfg0 = readl(&ana_pll->sys_pll2_gnrl_ctl); |
| 249 | val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK | |
| 250 | INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK | |
| 251 | INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK | |
| 252 | INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK | |
| 253 | INTPLL_DIV20_CLKE_MASK; |
| 254 | writel(val_cfg0, &ana_pll->sys_pll2_gnrl_ctl); |
| 255 | |
| 256 | /* config GIC to sys_pll2_100m */ |
| 257 | clock_enable(CCGR_GIC, 0); |
| 258 | clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON | |
| 259 | CLK_ROOT_SOURCE_SEL(3)); |
| 260 | clock_enable(CCGR_GIC, 1); |
| 261 | |
| 262 | clock_set_target_val(NAND_USDHC_BUS_CLK_ROOT, CLK_ROOT_ON | |
| 263 | CLK_ROOT_SOURCE_SEL(1)); |
| 264 | |
| 265 | clock_enable(CCGR_DDR1, 0); |
| 266 | clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON | |
| 267 | CLK_ROOT_SOURCE_SEL(1)); |
| 268 | clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | |
| 269 | CLK_ROOT_SOURCE_SEL(1)); |
| 270 | clock_enable(CCGR_DDR1, 1); |
| 271 | |
| 272 | init_wdog_clk(); |
| 273 | |
| 274 | clock_enable(CCGR_TEMP_SENSOR, 1); |
| 275 | |
| 276 | clock_enable(CCGR_SEC_DEBUG, 1); |
| 277 | |
| 278 | return 0; |
| 279 | }; |
| 280 | |
| 281 | u32 imx_get_uartclk(void) |
| 282 | { |
| 283 | return 24000000U; |
| 284 | } |
| 285 | |
| 286 | u32 mxc_get_clock(enum mxc_clock clk) |
| 287 | { |
| 288 | struct clk *clkp; |
| 289 | int ret; |
| 290 | |
| 291 | switch (clk) { |
| 292 | case MXC_IPG_CLK: |
| 293 | ret = clk_get_by_id(IMX8MM_CLK_IPG_ROOT, &clkp); |
| 294 | if (ret) |
| 295 | return 0; |
| 296 | return clk_get_rate(clkp); |
| 297 | case MXC_ARM_CLK: |
| 298 | ret = clk_get_by_id(IMX8MM_CLK_A53_DIV, &clkp); |
| 299 | if (ret) |
| 300 | return 0; |
| 301 | return clk_get_rate(clkp); |
| 302 | default: |
| 303 | printf("%s: %d not supported\n", __func__, clk); |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |