Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Freescale Semiconductor, Inc. |
| 4 | * |
| 5 | * Author: |
| 6 | * Peng Fan <Peng.Fan@freescale.com> |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Tom Rini | 5ab32a0 | 2024-04-30 07:35:49 -0600 | [diff] [blame] | 9 | #include <config.h> |
Simon Glass | 85d6531 | 2019-12-28 10:44:58 -0700 | [diff] [blame] | 10 | #include <clock_legacy.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | #include <command.h> |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 12 | #include <div64.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Tom Rini | 5ab32a0 | 2024-04-30 07:35:49 -0600 | [diff] [blame] | 14 | #include <time.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 16 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 17 | #include <linux/errno.h> |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 18 | #include <asm/arch/imx-regs.h> |
| 19 | #include <asm/arch/crm_regs.h> |
| 20 | #include <asm/arch/clock.h> |
| 21 | #include <asm/arch/sys_proto.h> |
| 22 | |
| 23 | struct mxc_ccm_anatop_reg *ccm_anatop = (struct mxc_ccm_anatop_reg *) |
| 24 | ANATOP_BASE_ADDR; |
| 25 | struct mxc_ccm_reg *ccm_reg = (struct mxc_ccm_reg *)CCM_BASE_ADDR; |
| 26 | |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 27 | #ifdef CONFIG_FSL_ESDHC_IMX |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | #endif |
| 30 | |
| 31 | int get_clocks(void) |
| 32 | { |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 33 | #ifdef CONFIG_FSL_ESDHC_IMX |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 34 | #if CFG_SYS_FSL_ESDHC_ADDR == USDHC2_BASE_ADDR |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 35 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK); |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 36 | #elif CFG_SYS_FSL_ESDHC_ADDR == USDHC3_BASE_ADDR |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 37 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK); |
| 38 | #else |
| 39 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK); |
| 40 | #endif |
| 41 | #endif |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | u32 get_ahb_clk(void) |
| 46 | { |
| 47 | return get_root_clk(AHB_CLK_ROOT); |
| 48 | } |
| 49 | |
| 50 | static u32 get_ipg_clk(void) |
| 51 | { |
| 52 | /* |
| 53 | * The AHB and IPG are fixed at 2:1 ratio, and synchronized to |
| 54 | * each other. |
| 55 | */ |
| 56 | return get_ahb_clk() / 2; |
| 57 | } |
| 58 | |
| 59 | u32 imx_get_uartclk(void) |
| 60 | { |
Jun Nie | 8ebf3bf | 2019-05-08 14:38:31 +0800 | [diff] [blame] | 61 | return get_root_clk(UART_CLK_ROOT); |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | u32 imx_get_fecclk(void) |
| 65 | { |
| 66 | return get_root_clk(ENET_AXI_CLK_ROOT); |
| 67 | } |
| 68 | |
| 69 | #ifdef CONFIG_MXC_OCOTP |
| 70 | void enable_ocotp_clk(unsigned char enable) |
| 71 | { |
| 72 | clock_enable(CCGR_OCOTP, enable); |
| 73 | } |
| 74 | |
| 75 | void enable_thermal_clk(void) |
| 76 | { |
| 77 | enable_ocotp_clk(1); |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | void enable_usboh3_clk(unsigned char enable) |
| 82 | { |
| 83 | u32 target; |
| 84 | |
| 85 | if (enable) { |
| 86 | /* disable the clock gate first */ |
| 87 | clock_enable(CCGR_USB_HSIC, 0); |
| 88 | |
| 89 | /* 120Mhz */ |
| 90 | target = CLK_ROOT_ON | |
| 91 | USB_HSIC_CLK_ROOT_FROM_PLL_SYS_MAIN_480M_CLK | |
| 92 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 93 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 94 | clock_set_target_val(USB_HSIC_CLK_ROOT, target); |
| 95 | |
| 96 | /* enable the clock gate */ |
| 97 | clock_enable(CCGR_USB_CTRL, 1); |
| 98 | clock_enable(CCGR_USB_HSIC, 1); |
| 99 | clock_enable(CCGR_USB_PHY1, 1); |
| 100 | clock_enable(CCGR_USB_PHY2, 1); |
| 101 | } else { |
| 102 | clock_enable(CCGR_USB_CTRL, 0); |
| 103 | clock_enable(CCGR_USB_HSIC, 0); |
| 104 | clock_enable(CCGR_USB_PHY1, 0); |
| 105 | clock_enable(CCGR_USB_PHY2, 0); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static u32 decode_pll(enum pll_clocks pll, u32 infreq) |
| 110 | { |
| 111 | u32 reg, div_sel; |
| 112 | u32 num, denom; |
| 113 | |
| 114 | /* |
| 115 | * Alought there are four choices for the bypass src, |
| 116 | * we choose OSC_24M which is the default set in ROM. |
| 117 | */ |
| 118 | switch (pll) { |
| 119 | case PLL_CORE: |
| 120 | reg = readl(&ccm_anatop->pll_arm); |
| 121 | |
| 122 | if (reg & CCM_ANALOG_PLL_ARM_POWERDOWN_MASK) |
| 123 | return 0; |
| 124 | |
| 125 | if (reg & CCM_ANALOG_PLL_ARM_BYPASS_MASK) |
| 126 | return MXC_HCLK; |
| 127 | |
| 128 | div_sel = (reg & CCM_ANALOG_PLL_ARM_DIV_SELECT_MASK) >> |
| 129 | CCM_ANALOG_PLL_ARM_DIV_SELECT_SHIFT; |
| 130 | |
| 131 | return (infreq * div_sel) / 2; |
| 132 | |
| 133 | case PLL_SYS: |
| 134 | reg = readl(&ccm_anatop->pll_480); |
| 135 | |
| 136 | if (reg & CCM_ANALOG_PLL_480_POWERDOWN_MASK) |
| 137 | return 0; |
| 138 | |
| 139 | if (reg & CCM_ANALOG_PLL_480_BYPASS_MASK) |
| 140 | return MXC_HCLK; |
| 141 | |
| 142 | if (((reg & CCM_ANALOG_PLL_480_DIV_SELECT_MASK) >> |
| 143 | CCM_ANALOG_PLL_480_DIV_SELECT_SHIFT) == 0) |
| 144 | return 480000000u; |
| 145 | else |
| 146 | return 528000000u; |
| 147 | |
| 148 | case PLL_ENET: |
| 149 | reg = readl(&ccm_anatop->pll_enet); |
| 150 | |
| 151 | if (reg & CCM_ANALOG_PLL_ENET_POWERDOWN_MASK) |
| 152 | return 0; |
| 153 | |
| 154 | if (reg & CCM_ANALOG_PLL_ENET_BYPASS_MASK) |
| 155 | return MXC_HCLK; |
| 156 | |
| 157 | return 1000000000u; |
| 158 | |
| 159 | case PLL_DDR: |
| 160 | reg = readl(&ccm_anatop->pll_ddr); |
| 161 | |
| 162 | if (reg & CCM_ANALOG_PLL_DDR_POWERDOWN_MASK) |
| 163 | return 0; |
| 164 | |
| 165 | num = ccm_anatop->pll_ddr_num; |
| 166 | denom = ccm_anatop->pll_ddr_denom; |
| 167 | |
| 168 | if (reg & CCM_ANALOG_PLL_DDR_BYPASS_MASK) |
| 169 | return MXC_HCLK; |
| 170 | |
| 171 | div_sel = (reg & CCM_ANALOG_PLL_DDR_DIV_SELECT_MASK) >> |
| 172 | CCM_ANALOG_PLL_DDR_DIV_SELECT_SHIFT; |
| 173 | |
| 174 | return infreq * (div_sel + num / denom); |
| 175 | |
| 176 | case PLL_USB: |
| 177 | return 480000000u; |
| 178 | |
| 179 | default: |
| 180 | printf("Unsupported pll clocks %d\n", pll); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static u32 mxc_get_pll_sys_derive(int derive) |
| 188 | { |
| 189 | u32 freq, div, frac; |
| 190 | u32 reg; |
| 191 | |
| 192 | div = 1; |
| 193 | reg = readl(&ccm_anatop->pll_480); |
| 194 | freq = decode_pll(PLL_SYS, MXC_HCLK); |
| 195 | |
| 196 | switch (derive) { |
| 197 | case PLL_SYS_MAIN_480M_CLK: |
| 198 | if (reg & CCM_ANALOG_PLL_480_MAIN_DIV1_CLKGATE_MASK) |
| 199 | return 0; |
| 200 | else |
| 201 | return freq; |
| 202 | case PLL_SYS_MAIN_240M_CLK: |
| 203 | if (reg & CCM_ANALOG_PLL_480_MAIN_DIV2_CLKGATE_MASK) |
| 204 | return 0; |
| 205 | else |
| 206 | return freq / 2; |
| 207 | case PLL_SYS_MAIN_120M_CLK: |
| 208 | if (reg & CCM_ANALOG_PLL_480_MAIN_DIV4_CLKGATE_MASK) |
| 209 | return 0; |
| 210 | else |
| 211 | return freq / 4; |
| 212 | case PLL_SYS_PFD0_392M_CLK: |
| 213 | reg = readl(&ccm_anatop->pfd_480a); |
| 214 | if (reg & CCM_ANALOG_PFD_480A_PFD0_DIV1_CLKGATE_MASK) |
| 215 | return 0; |
| 216 | frac = (reg & CCM_ANALOG_PFD_480A_PFD0_FRAC_MASK) >> |
| 217 | CCM_ANALOG_PFD_480A_PFD0_FRAC_SHIFT; |
| 218 | break; |
| 219 | case PLL_SYS_PFD0_196M_CLK: |
| 220 | if (reg & CCM_ANALOG_PLL_480_PFD0_DIV2_CLKGATE_MASK) |
| 221 | return 0; |
| 222 | reg = readl(&ccm_anatop->pfd_480a); |
| 223 | frac = (reg & CCM_ANALOG_PFD_480A_PFD0_FRAC_MASK) >> |
| 224 | CCM_ANALOG_PFD_480A_PFD0_FRAC_SHIFT; |
| 225 | div = 2; |
| 226 | break; |
| 227 | case PLL_SYS_PFD1_332M_CLK: |
| 228 | reg = readl(&ccm_anatop->pfd_480a); |
| 229 | if (reg & CCM_ANALOG_PFD_480A_PFD1_DIV1_CLKGATE_MASK) |
| 230 | return 0; |
| 231 | frac = (reg & CCM_ANALOG_PFD_480A_PFD1_FRAC_MASK) >> |
| 232 | CCM_ANALOG_PFD_480A_PFD1_FRAC_SHIFT; |
| 233 | break; |
| 234 | case PLL_SYS_PFD1_166M_CLK: |
| 235 | if (reg & CCM_ANALOG_PLL_480_PFD1_DIV2_CLKGATE_MASK) |
| 236 | return 0; |
| 237 | reg = readl(&ccm_anatop->pfd_480a); |
| 238 | frac = (reg & CCM_ANALOG_PFD_480A_PFD1_FRAC_MASK) >> |
| 239 | CCM_ANALOG_PFD_480A_PFD1_FRAC_SHIFT; |
| 240 | div = 2; |
| 241 | break; |
| 242 | case PLL_SYS_PFD2_270M_CLK: |
| 243 | reg = readl(&ccm_anatop->pfd_480a); |
| 244 | if (reg & CCM_ANALOG_PFD_480A_PFD2_DIV1_CLKGATE_MASK) |
| 245 | return 0; |
| 246 | frac = (reg & CCM_ANALOG_PFD_480A_PFD2_FRAC_MASK) >> |
| 247 | CCM_ANALOG_PFD_480A_PFD2_FRAC_SHIFT; |
| 248 | break; |
| 249 | case PLL_SYS_PFD2_135M_CLK: |
| 250 | if (reg & CCM_ANALOG_PLL_480_PFD2_DIV2_CLKGATE_MASK) |
| 251 | return 0; |
| 252 | reg = readl(&ccm_anatop->pfd_480a); |
| 253 | frac = (reg & CCM_ANALOG_PFD_480A_PFD2_FRAC_MASK) >> |
| 254 | CCM_ANALOG_PFD_480A_PFD2_FRAC_SHIFT; |
| 255 | div = 2; |
| 256 | break; |
| 257 | case PLL_SYS_PFD3_CLK: |
| 258 | reg = readl(&ccm_anatop->pfd_480a); |
| 259 | if (reg & CCM_ANALOG_PFD_480A_PFD3_DIV1_CLKGATE_MASK) |
| 260 | return 0; |
| 261 | frac = (reg & CCM_ANALOG_PFD_480A_PFD3_FRAC_MASK) >> |
| 262 | CCM_ANALOG_PFD_480A_PFD3_FRAC_SHIFT; |
| 263 | break; |
| 264 | case PLL_SYS_PFD4_CLK: |
| 265 | reg = readl(&ccm_anatop->pfd_480b); |
| 266 | if (reg & CCM_ANALOG_PFD_480B_PFD4_DIV1_CLKGATE_MASK) |
| 267 | return 0; |
| 268 | frac = (reg & CCM_ANALOG_PFD_480B_PFD4_FRAC_MASK) >> |
| 269 | CCM_ANALOG_PFD_480B_PFD4_FRAC_SHIFT; |
| 270 | break; |
| 271 | case PLL_SYS_PFD5_CLK: |
| 272 | reg = readl(&ccm_anatop->pfd_480b); |
| 273 | if (reg & CCM_ANALOG_PFD_480B_PFD5_DIV1_CLKGATE_MASK) |
| 274 | return 0; |
| 275 | frac = (reg & CCM_ANALOG_PFD_480B_PFD5_FRAC_MASK) >> |
| 276 | CCM_ANALOG_PFD_480B_PFD5_FRAC_SHIFT; |
| 277 | break; |
| 278 | case PLL_SYS_PFD6_CLK: |
| 279 | reg = readl(&ccm_anatop->pfd_480b); |
| 280 | if (reg & CCM_ANALOG_PFD_480B_PFD6_DIV1_CLKGATE_MASK) |
| 281 | return 0; |
| 282 | frac = (reg & CCM_ANALOG_PFD_480B_PFD6_FRAC_MASK) >> |
| 283 | CCM_ANALOG_PFD_480B_PFD6_FRAC_SHIFT; |
| 284 | break; |
| 285 | case PLL_SYS_PFD7_CLK: |
| 286 | reg = readl(&ccm_anatop->pfd_480b); |
| 287 | if (reg & CCM_ANALOG_PFD_480B_PFD7_DIV1_CLKGATE_MASK) |
| 288 | return 0; |
| 289 | frac = (reg & CCM_ANALOG_PFD_480B_PFD7_FRAC_MASK) >> |
| 290 | CCM_ANALOG_PFD_480B_PFD7_FRAC_SHIFT; |
| 291 | break; |
| 292 | default: |
| 293 | printf("Error derived pll_sys clock %d\n", derive); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | return ((freq / frac) * 18) / div; |
| 298 | } |
| 299 | |
| 300 | static u32 mxc_get_pll_enet_derive(int derive) |
| 301 | { |
| 302 | u32 freq, reg; |
| 303 | |
| 304 | freq = decode_pll(PLL_ENET, MXC_HCLK); |
| 305 | reg = readl(&ccm_anatop->pll_enet); |
| 306 | |
| 307 | switch (derive) { |
| 308 | case PLL_ENET_MAIN_500M_CLK: |
| 309 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_500MHZ_MASK) |
| 310 | return freq / 2; |
| 311 | break; |
| 312 | case PLL_ENET_MAIN_250M_CLK: |
| 313 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_250MHZ_MASK) |
| 314 | return freq / 4; |
| 315 | break; |
| 316 | case PLL_ENET_MAIN_125M_CLK: |
| 317 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_125MHZ_MASK) |
| 318 | return freq / 8; |
| 319 | break; |
| 320 | case PLL_ENET_MAIN_100M_CLK: |
| 321 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_100MHZ_MASK) |
| 322 | return freq / 10; |
| 323 | break; |
| 324 | case PLL_ENET_MAIN_50M_CLK: |
| 325 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_50MHZ_MASK) |
| 326 | return freq / 20; |
| 327 | break; |
| 328 | case PLL_ENET_MAIN_40M_CLK: |
| 329 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_40MHZ_MASK) |
| 330 | return freq / 25; |
| 331 | break; |
| 332 | case PLL_ENET_MAIN_25M_CLK: |
| 333 | if (reg & CCM_ANALOG_PLL_ENET_ENABLE_CLK_25MHZ_MASK) |
| 334 | return freq / 40; |
| 335 | break; |
| 336 | default: |
| 337 | printf("Error derived pll_enet clock %d\n", derive); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static u32 mxc_get_pll_ddr_derive(int derive) |
| 345 | { |
| 346 | u32 freq, reg; |
| 347 | |
| 348 | freq = decode_pll(PLL_DDR, MXC_HCLK); |
| 349 | reg = readl(&ccm_anatop->pll_ddr); |
| 350 | |
| 351 | switch (derive) { |
| 352 | case PLL_DRAM_MAIN_1066M_CLK: |
| 353 | return freq; |
| 354 | case PLL_DRAM_MAIN_533M_CLK: |
| 355 | if (reg & CCM_ANALOG_PLL_DDR_DIV2_ENABLE_CLK_MASK) |
| 356 | return freq / 2; |
| 357 | break; |
| 358 | default: |
| 359 | printf("Error derived pll_ddr clock %d\n", derive); |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static u32 mxc_get_pll_derive(enum pll_clocks pll, int derive) |
| 367 | { |
| 368 | switch (pll) { |
| 369 | case PLL_SYS: |
| 370 | return mxc_get_pll_sys_derive(derive); |
| 371 | case PLL_ENET: |
| 372 | return mxc_get_pll_enet_derive(derive); |
| 373 | case PLL_DDR: |
| 374 | return mxc_get_pll_ddr_derive(derive); |
| 375 | default: |
| 376 | printf("Error pll.\n"); |
| 377 | return 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static u32 get_root_src_clk(enum clk_root_src root_src) |
| 382 | { |
| 383 | switch (root_src) { |
| 384 | case OSC_24M_CLK: |
| 385 | return 24000000u; |
| 386 | case PLL_ARM_MAIN_800M_CLK: |
| 387 | return decode_pll(PLL_CORE, MXC_HCLK); |
| 388 | |
| 389 | case PLL_SYS_MAIN_480M_CLK: |
| 390 | case PLL_SYS_MAIN_240M_CLK: |
| 391 | case PLL_SYS_MAIN_120M_CLK: |
| 392 | case PLL_SYS_PFD0_392M_CLK: |
| 393 | case PLL_SYS_PFD0_196M_CLK: |
| 394 | case PLL_SYS_PFD1_332M_CLK: |
| 395 | case PLL_SYS_PFD1_166M_CLK: |
| 396 | case PLL_SYS_PFD2_270M_CLK: |
| 397 | case PLL_SYS_PFD2_135M_CLK: |
| 398 | case PLL_SYS_PFD3_CLK: |
| 399 | case PLL_SYS_PFD4_CLK: |
| 400 | case PLL_SYS_PFD5_CLK: |
| 401 | case PLL_SYS_PFD6_CLK: |
| 402 | case PLL_SYS_PFD7_CLK: |
| 403 | return mxc_get_pll_derive(PLL_SYS, root_src); |
| 404 | |
| 405 | case PLL_ENET_MAIN_500M_CLK: |
| 406 | case PLL_ENET_MAIN_250M_CLK: |
| 407 | case PLL_ENET_MAIN_125M_CLK: |
| 408 | case PLL_ENET_MAIN_100M_CLK: |
| 409 | case PLL_ENET_MAIN_50M_CLK: |
| 410 | case PLL_ENET_MAIN_40M_CLK: |
| 411 | case PLL_ENET_MAIN_25M_CLK: |
| 412 | return mxc_get_pll_derive(PLL_ENET, root_src); |
| 413 | |
| 414 | case PLL_DRAM_MAIN_1066M_CLK: |
| 415 | case PLL_DRAM_MAIN_533M_CLK: |
| 416 | return mxc_get_pll_derive(PLL_DDR, root_src); |
| 417 | |
| 418 | case PLL_AUDIO_MAIN_CLK: |
| 419 | return decode_pll(PLL_AUDIO, MXC_HCLK); |
| 420 | case PLL_VIDEO_MAIN_CLK: |
| 421 | return decode_pll(PLL_VIDEO, MXC_HCLK); |
| 422 | |
| 423 | case PLL_USB_MAIN_480M_CLK: |
| 424 | return decode_pll(PLL_USB, MXC_HCLK); |
| 425 | |
| 426 | case REF_1M_CLK: |
| 427 | return 1000000; |
| 428 | case OSC_32K_CLK: |
| 429 | return MXC_CLK32; |
| 430 | |
| 431 | case EXT_CLK_1: |
| 432 | case EXT_CLK_2: |
| 433 | case EXT_CLK_3: |
| 434 | case EXT_CLK_4: |
| 435 | printf("No EXT CLK supported??\n"); |
| 436 | break; |
| 437 | }; |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | u32 get_root_clk(enum clk_root_index clock_id) |
| 443 | { |
| 444 | enum clk_root_src root_src; |
| 445 | u32 post_podf, pre_podf, auto_podf, root_src_clk; |
| 446 | int auto_en; |
| 447 | |
| 448 | if (clock_root_enabled(clock_id) <= 0) |
| 449 | return 0; |
| 450 | |
| 451 | if (clock_get_prediv(clock_id, &pre_podf) < 0) |
| 452 | return 0; |
| 453 | |
| 454 | if (clock_get_postdiv(clock_id, &post_podf) < 0) |
| 455 | return 0; |
| 456 | |
| 457 | if (clock_get_autopostdiv(clock_id, &auto_podf, &auto_en) < 0) |
| 458 | return 0; |
| 459 | |
| 460 | if (auto_en == 0) |
| 461 | auto_podf = 0; |
| 462 | |
| 463 | if (clock_get_src(clock_id, &root_src) < 0) |
| 464 | return 0; |
| 465 | |
| 466 | root_src_clk = get_root_src_clk(root_src); |
| 467 | |
| 468 | /* |
| 469 | * bypass clk is ignored. |
| 470 | */ |
| 471 | |
| 472 | return root_src_clk / (post_podf + 1) / (pre_podf + 1) / |
| 473 | (auto_podf + 1); |
| 474 | } |
| 475 | |
| 476 | static u32 get_ddrc_clk(void) |
| 477 | { |
| 478 | u32 reg, freq; |
| 479 | enum root_post_div post_div; |
| 480 | |
| 481 | reg = readl(&ccm_reg->root[DRAM_CLK_ROOT].target_root); |
| 482 | if (reg & CLK_ROOT_MUX_MASK) |
| 483 | /* DRAM_ALT_CLK_ROOT */ |
| 484 | freq = get_root_clk(DRAM_ALT_CLK_ROOT); |
| 485 | else |
| 486 | /* PLL_DRAM_MAIN_1066M_CLK */ |
| 487 | freq = mxc_get_pll_derive(PLL_DDR, PLL_DRAM_MAIN_1066M_CLK); |
| 488 | |
| 489 | post_div = reg & DRAM_CLK_ROOT_POST_DIV_MASK; |
| 490 | |
| 491 | return freq / (post_div + 1) / 2; |
| 492 | } |
| 493 | |
| 494 | unsigned int mxc_get_clock(enum mxc_clock clk) |
| 495 | { |
| 496 | switch (clk) { |
| 497 | case MXC_ARM_CLK: |
| 498 | return get_root_clk(ARM_A7_CLK_ROOT); |
| 499 | case MXC_AXI_CLK: |
| 500 | return get_root_clk(MAIN_AXI_CLK_ROOT); |
| 501 | case MXC_AHB_CLK: |
| 502 | return get_root_clk(AHB_CLK_ROOT); |
| 503 | case MXC_IPG_CLK: |
| 504 | return get_ipg_clk(); |
| 505 | case MXC_I2C_CLK: |
Fabio Estevam | ed141b2 | 2023-01-03 10:19:39 -0300 | [diff] [blame] | 506 | return 60000000; |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 507 | case MXC_UART_CLK: |
| 508 | return get_root_clk(UART1_CLK_ROOT); |
| 509 | case MXC_CSPI_CLK: |
| 510 | return get_root_clk(ECSPI1_CLK_ROOT); |
| 511 | case MXC_DDR_CLK: |
| 512 | return get_ddrc_clk(); |
| 513 | case MXC_ESDHC_CLK: |
| 514 | return get_root_clk(USDHC1_CLK_ROOT); |
| 515 | case MXC_ESDHC2_CLK: |
| 516 | return get_root_clk(USDHC2_CLK_ROOT); |
| 517 | case MXC_ESDHC3_CLK: |
| 518 | return get_root_clk(USDHC3_CLK_ROOT); |
| 519 | default: |
| 520 | printf("Unsupported mxc_clock %d\n", clk); |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | #ifdef CONFIG_SYS_I2C_MXC |
| 528 | /* i2c_num can be 0 - 3 */ |
| 529 | int enable_i2c_clk(unsigned char enable, unsigned i2c_num) |
| 530 | { |
| 531 | u32 target; |
| 532 | |
| 533 | if (i2c_num >= 4) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | if (enable) { |
| 537 | clock_enable(CCGR_I2C1 + i2c_num, 0); |
| 538 | |
| 539 | /* Set i2c root clock to PLL_SYS_MAIN_120M_CLK */ |
| 540 | |
| 541 | target = CLK_ROOT_ON | |
| 542 | I2C1_CLK_ROOT_FROM_PLL_SYS_MAIN_120M_CLK | |
| 543 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 544 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV2); |
| 545 | clock_set_target_val(I2C1_CLK_ROOT + i2c_num, target); |
| 546 | |
| 547 | clock_enable(CCGR_I2C1 + i2c_num, 1); |
| 548 | } else { |
| 549 | clock_enable(CCGR_I2C1 + i2c_num, 0); |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | static void init_clk_esdhc(void) |
| 557 | { |
| 558 | u32 target; |
| 559 | |
| 560 | /* disable the clock gate first */ |
| 561 | clock_enable(CCGR_USDHC1, 0); |
| 562 | clock_enable(CCGR_USDHC2, 0); |
| 563 | clock_enable(CCGR_USDHC3, 0); |
| 564 | |
| 565 | /* 196: 392/2 */ |
| 566 | target = CLK_ROOT_ON | USDHC1_CLK_ROOT_FROM_PLL_SYS_PFD0_392M_CLK | |
| 567 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 568 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV2); |
| 569 | clock_set_target_val(USDHC1_CLK_ROOT, target); |
| 570 | |
| 571 | target = CLK_ROOT_ON | USDHC1_CLK_ROOT_FROM_PLL_SYS_PFD0_392M_CLK | |
| 572 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 573 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV2); |
| 574 | clock_set_target_val(USDHC2_CLK_ROOT, target); |
| 575 | |
| 576 | target = CLK_ROOT_ON | USDHC1_CLK_ROOT_FROM_PLL_SYS_PFD0_392M_CLK | |
| 577 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 578 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV2); |
| 579 | clock_set_target_val(USDHC3_CLK_ROOT, target); |
| 580 | |
| 581 | /* enable the clock gate */ |
| 582 | clock_enable(CCGR_USDHC1, 1); |
| 583 | clock_enable(CCGR_USDHC2, 1); |
| 584 | clock_enable(CCGR_USDHC3, 1); |
| 585 | } |
| 586 | |
| 587 | static void init_clk_uart(void) |
| 588 | { |
| 589 | u32 target; |
| 590 | |
| 591 | /* disable the clock gate first */ |
| 592 | clock_enable(CCGR_UART1, 0); |
| 593 | clock_enable(CCGR_UART2, 0); |
| 594 | clock_enable(CCGR_UART3, 0); |
| 595 | clock_enable(CCGR_UART4, 0); |
| 596 | clock_enable(CCGR_UART5, 0); |
| 597 | clock_enable(CCGR_UART6, 0); |
| 598 | clock_enable(CCGR_UART7, 0); |
| 599 | |
| 600 | /* 24Mhz */ |
| 601 | target = CLK_ROOT_ON | UART1_CLK_ROOT_FROM_OSC_24M_CLK | |
| 602 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 603 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 604 | clock_set_target_val(UART1_CLK_ROOT, target); |
| 605 | |
| 606 | target = CLK_ROOT_ON | UART2_CLK_ROOT_FROM_OSC_24M_CLK | |
| 607 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 608 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 609 | clock_set_target_val(UART2_CLK_ROOT, target); |
| 610 | |
| 611 | target = CLK_ROOT_ON | UART3_CLK_ROOT_FROM_OSC_24M_CLK | |
| 612 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 613 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 614 | clock_set_target_val(UART3_CLK_ROOT, target); |
| 615 | |
| 616 | target = CLK_ROOT_ON | UART4_CLK_ROOT_FROM_OSC_24M_CLK | |
| 617 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 618 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 619 | clock_set_target_val(UART4_CLK_ROOT, target); |
| 620 | |
| 621 | target = CLK_ROOT_ON | UART5_CLK_ROOT_FROM_OSC_24M_CLK | |
| 622 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 623 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 624 | clock_set_target_val(UART5_CLK_ROOT, target); |
| 625 | |
| 626 | target = CLK_ROOT_ON | UART6_CLK_ROOT_FROM_OSC_24M_CLK | |
| 627 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 628 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 629 | clock_set_target_val(UART6_CLK_ROOT, target); |
| 630 | |
| 631 | target = CLK_ROOT_ON | UART7_CLK_ROOT_FROM_OSC_24M_CLK | |
| 632 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 633 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 634 | clock_set_target_val(UART7_CLK_ROOT, target); |
| 635 | |
| 636 | /* enable the clock gate */ |
| 637 | clock_enable(CCGR_UART1, 1); |
| 638 | clock_enable(CCGR_UART2, 1); |
| 639 | clock_enable(CCGR_UART3, 1); |
| 640 | clock_enable(CCGR_UART4, 1); |
| 641 | clock_enable(CCGR_UART5, 1); |
| 642 | clock_enable(CCGR_UART6, 1); |
| 643 | clock_enable(CCGR_UART7, 1); |
| 644 | } |
| 645 | |
| 646 | static void init_clk_weim(void) |
| 647 | { |
| 648 | u32 target; |
| 649 | |
| 650 | /* disable the clock gate first */ |
| 651 | clock_enable(CCGR_WEIM, 0); |
| 652 | |
| 653 | /* 120Mhz */ |
| 654 | target = CLK_ROOT_ON | EIM_CLK_ROOT_FROM_PLL_SYS_MAIN_120M_CLK | |
| 655 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 656 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 657 | clock_set_target_val(EIM_CLK_ROOT, target); |
| 658 | |
| 659 | /* enable the clock gate */ |
| 660 | clock_enable(CCGR_WEIM, 1); |
| 661 | } |
| 662 | |
| 663 | static void init_clk_ecspi(void) |
| 664 | { |
| 665 | u32 target; |
| 666 | |
| 667 | /* disable the clock gate first */ |
| 668 | clock_enable(CCGR_ECSPI1, 0); |
| 669 | clock_enable(CCGR_ECSPI2, 0); |
| 670 | clock_enable(CCGR_ECSPI3, 0); |
| 671 | clock_enable(CCGR_ECSPI4, 0); |
| 672 | |
| 673 | /* 60Mhz: 240/4 */ |
| 674 | target = CLK_ROOT_ON | ECSPI1_CLK_ROOT_FROM_PLL_SYS_MAIN_240M_CLK | |
| 675 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 676 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 677 | clock_set_target_val(ECSPI1_CLK_ROOT, target); |
| 678 | |
| 679 | target = CLK_ROOT_ON | ECSPI2_CLK_ROOT_FROM_PLL_SYS_MAIN_240M_CLK | |
| 680 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 681 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 682 | clock_set_target_val(ECSPI2_CLK_ROOT, target); |
| 683 | |
| 684 | target = CLK_ROOT_ON | ECSPI3_CLK_ROOT_FROM_PLL_SYS_MAIN_240M_CLK | |
| 685 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 686 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 687 | clock_set_target_val(ECSPI3_CLK_ROOT, target); |
| 688 | |
| 689 | target = CLK_ROOT_ON | ECSPI4_CLK_ROOT_FROM_PLL_SYS_MAIN_240M_CLK | |
| 690 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 691 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 692 | clock_set_target_val(ECSPI4_CLK_ROOT, target); |
| 693 | |
| 694 | /* enable the clock gate */ |
| 695 | clock_enable(CCGR_ECSPI1, 1); |
| 696 | clock_enable(CCGR_ECSPI2, 1); |
| 697 | clock_enable(CCGR_ECSPI3, 1); |
| 698 | clock_enable(CCGR_ECSPI4, 1); |
| 699 | } |
| 700 | |
| 701 | static void init_clk_wdog(void) |
| 702 | { |
| 703 | u32 target; |
| 704 | |
| 705 | /* disable the clock gate first */ |
| 706 | clock_enable(CCGR_WDOG1, 0); |
| 707 | clock_enable(CCGR_WDOG2, 0); |
| 708 | clock_enable(CCGR_WDOG3, 0); |
| 709 | clock_enable(CCGR_WDOG4, 0); |
| 710 | |
| 711 | /* 24Mhz */ |
| 712 | target = CLK_ROOT_ON | WDOG_CLK_ROOT_FROM_OSC_24M_CLK | |
| 713 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 714 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 715 | clock_set_target_val(WDOG_CLK_ROOT, target); |
| 716 | |
| 717 | /* enable the clock gate */ |
| 718 | clock_enable(CCGR_WDOG1, 1); |
| 719 | clock_enable(CCGR_WDOG2, 1); |
| 720 | clock_enable(CCGR_WDOG3, 1); |
| 721 | clock_enable(CCGR_WDOG4, 1); |
| 722 | } |
| 723 | |
| 724 | #ifdef CONFIG_MXC_EPDC |
| 725 | static void init_clk_epdc(void) |
| 726 | { |
| 727 | u32 target; |
| 728 | |
| 729 | /* disable the clock gate first */ |
| 730 | clock_enable(CCGR_EPDC, 0); |
| 731 | |
| 732 | /* 24Mhz */ |
| 733 | target = CLK_ROOT_ON | EPDC_PIXEL_CLK_ROOT_FROM_PLL_SYS_MAIN_480M_CLK | |
| 734 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 735 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV12); |
| 736 | clock_set_target_val(EPDC_PIXEL_CLK_ROOT, target); |
| 737 | |
| 738 | /* enable the clock gate */ |
| 739 | clock_enable(CCGR_EPDC, 1); |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | static int enable_pll_enet(void) |
| 744 | { |
| 745 | u32 reg; |
| 746 | s32 timeout = 100000; |
| 747 | |
| 748 | reg = readl(&ccm_anatop->pll_enet); |
| 749 | /* If pll_enet powered up, no need to set it again */ |
| 750 | if (reg & ANADIG_PLL_ENET_PWDN_MASK) { |
| 751 | reg &= ~ANADIG_PLL_ENET_PWDN_MASK; |
| 752 | writel(reg, &ccm_anatop->pll_enet); |
| 753 | |
| 754 | while (timeout--) { |
| 755 | if (readl(&ccm_anatop->pll_enet) & ANADIG_PLL_LOCK) |
| 756 | break; |
| 757 | } |
| 758 | |
| 759 | if (timeout <= 0) { |
| 760 | /* If timeout, we set pwdn for pll_enet. */ |
| 761 | reg |= ANADIG_PLL_ENET_PWDN_MASK; |
| 762 | return -ETIME; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* Clear bypass */ |
| 767 | writel(CCM_ANALOG_PLL_ENET_BYPASS_MASK, &ccm_anatop->pll_enet_clr); |
| 768 | |
| 769 | writel((CCM_ANALOG_PLL_ENET_ENABLE_CLK_500MHZ_MASK |
| 770 | | CCM_ANALOG_PLL_ENET_ENABLE_CLK_250MHZ_MASK |
| 771 | | CCM_ANALOG_PLL_ENET_ENABLE_CLK_125MHZ_MASK |
| 772 | | CCM_ANALOG_PLL_ENET_ENABLE_CLK_100MHZ_MASK |
| 773 | | CCM_ANALOG_PLL_ENET_ENABLE_CLK_50MHZ_MASK |
| 774 | | CCM_ANALOG_PLL_ENET_ENABLE_CLK_40MHZ_MASK |
| 775 | | CCM_ANALOG_PLL_ENET_ENABLE_CLK_25MHZ_MASK), |
| 776 | &ccm_anatop->pll_enet_set); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | static int enable_pll_video(u32 pll_div, u32 pll_num, u32 pll_denom, |
| 781 | u32 post_div) |
| 782 | { |
| 783 | u32 reg = 0; |
| 784 | ulong start; |
| 785 | |
| 786 | debug("pll5 div = %d, num = %d, denom = %d\n", |
| 787 | pll_div, pll_num, pll_denom); |
| 788 | |
| 789 | /* Power up PLL5 video and disable its output */ |
| 790 | writel(CCM_ANALOG_PLL_VIDEO_CLR_ENABLE_CLK_MASK | |
| 791 | CCM_ANALOG_PLL_VIDEO_CLR_POWERDOWN_MASK | |
| 792 | CCM_ANALOG_PLL_VIDEO_CLR_BYPASS_MASK | |
| 793 | CCM_ANALOG_PLL_VIDEO_CLR_DIV_SELECT_MASK | |
| 794 | CCM_ANALOG_PLL_VIDEO_CLR_POST_DIV_SEL_MASK | |
| 795 | CCM_ANALOG_PLL_VIDEO_CLR_TEST_DIV_SELECT_MASK, |
| 796 | &ccm_anatop->pll_video_clr); |
| 797 | |
| 798 | /* Set div, num and denom */ |
| 799 | switch (post_div) { |
| 800 | case 1: |
| 801 | writel(CCM_ANALOG_PLL_VIDEO_SET_DIV_SELECT(pll_div) | |
| 802 | CCM_ANALOG_PLL_VIDEO_SET_TEST_DIV_SELECT(0x1) | |
| 803 | CCM_ANALOG_PLL_VIDEO_SET_POST_DIV_SEL(0x0), |
| 804 | &ccm_anatop->pll_video_set); |
| 805 | break; |
| 806 | case 2: |
| 807 | writel(CCM_ANALOG_PLL_VIDEO_SET_DIV_SELECT(pll_div) | |
| 808 | CCM_ANALOG_PLL_VIDEO_SET_TEST_DIV_SELECT(0x0) | |
| 809 | CCM_ANALOG_PLL_VIDEO_SET_POST_DIV_SEL(0x0), |
| 810 | &ccm_anatop->pll_video_set); |
| 811 | break; |
| 812 | case 3: |
| 813 | writel(CCM_ANALOG_PLL_VIDEO_SET_DIV_SELECT(pll_div) | |
| 814 | CCM_ANALOG_PLL_VIDEO_SET_TEST_DIV_SELECT(0x0) | |
| 815 | CCM_ANALOG_PLL_VIDEO_SET_POST_DIV_SEL(0x1), |
| 816 | &ccm_anatop->pll_video_set); |
| 817 | break; |
| 818 | case 4: |
| 819 | writel(CCM_ANALOG_PLL_VIDEO_SET_DIV_SELECT(pll_div) | |
| 820 | CCM_ANALOG_PLL_VIDEO_SET_TEST_DIV_SELECT(0x0) | |
| 821 | CCM_ANALOG_PLL_VIDEO_SET_POST_DIV_SEL(0x3), |
| 822 | &ccm_anatop->pll_video_set); |
| 823 | break; |
| 824 | case 0: |
| 825 | default: |
| 826 | writel(CCM_ANALOG_PLL_VIDEO_SET_DIV_SELECT(pll_div) | |
| 827 | CCM_ANALOG_PLL_VIDEO_SET_TEST_DIV_SELECT(0x2) | |
| 828 | CCM_ANALOG_PLL_VIDEO_SET_POST_DIV_SEL(0x0), |
| 829 | &ccm_anatop->pll_video_set); |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | writel(CCM_ANALOG_PLL_VIDEO_NUM_A(pll_num), |
| 834 | &ccm_anatop->pll_video_num); |
| 835 | |
| 836 | writel(CCM_ANALOG_PLL_VIDEO_DENOM_B(pll_denom), |
| 837 | &ccm_anatop->pll_video_denom); |
| 838 | |
| 839 | /* Wait PLL5 lock */ |
| 840 | start = get_timer(0); /* Get current timestamp */ |
| 841 | |
| 842 | do { |
| 843 | reg = readl(&ccm_anatop->pll_video); |
| 844 | if (reg & CCM_ANALOG_PLL_VIDEO_LOCK_MASK) { |
| 845 | /* Enable PLL out */ |
| 846 | writel(CCM_ANALOG_PLL_VIDEO_CLR_ENABLE_CLK_MASK, |
| 847 | &ccm_anatop->pll_video_set); |
| 848 | return 0; |
| 849 | } |
| 850 | } while (get_timer(0) < (start + 10)); /* Wait 10ms */ |
| 851 | |
| 852 | printf("Lock PLL5 timeout\n"); |
| 853 | |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | int set_clk_qspi(void) |
| 858 | { |
| 859 | u32 target; |
| 860 | |
| 861 | /* disable the clock gate first */ |
| 862 | clock_enable(CCGR_QSPI, 0); |
| 863 | |
| 864 | /* 49M: 392/2/4 */ |
| 865 | target = CLK_ROOT_ON | QSPI_CLK_ROOT_FROM_PLL_SYS_PFD4_CLK | |
| 866 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 867 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV2); |
| 868 | clock_set_target_val(QSPI_CLK_ROOT, target); |
| 869 | |
| 870 | /* enable the clock gate */ |
| 871 | clock_enable(CCGR_QSPI, 1); |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | int set_clk_nand(void) |
| 877 | { |
| 878 | u32 target; |
| 879 | |
| 880 | /* disable the clock gate first */ |
| 881 | clock_enable(CCGR_RAWNAND, 0); |
| 882 | |
| 883 | enable_pll_enet(); |
| 884 | /* 100: 500/5 */ |
| 885 | target = CLK_ROOT_ON | NAND_CLK_ROOT_FROM_PLL_ENET_MAIN_500M_CLK | |
| 886 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 887 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV5); |
| 888 | clock_set_target_val(NAND_CLK_ROOT, target); |
| 889 | |
| 890 | /* enable the clock gate */ |
| 891 | clock_enable(CCGR_RAWNAND, 1); |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | void mxs_set_lcdclk(uint32_t base_addr, uint32_t freq) |
| 897 | { |
| 898 | u32 hck = MXC_HCLK/1000; |
| 899 | u32 min = hck * 27; |
| 900 | u32 max = hck * 54; |
| 901 | u32 temp, best = 0; |
| 902 | u32 i, j, pred = 1, postd = 1; |
| 903 | u32 pll_div, pll_num, pll_denom, post_div = 0; |
| 904 | u32 target; |
| 905 | |
| 906 | debug("mxs_set_lcdclk, freq = %d\n", freq); |
| 907 | |
| 908 | clock_enable(CCGR_LCDIF, 0); |
| 909 | |
| 910 | temp = (freq * 8 * 8); |
| 911 | if (temp < min) { |
| 912 | for (i = 1; i <= 4; i++) { |
| 913 | if ((temp * (1 << i)) > min) { |
| 914 | post_div = i; |
| 915 | freq = (freq * (1 << i)); |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (5 == i) { |
Heinrich Schuchardt | ab38c39 | 2020-12-25 16:22:27 +0100 | [diff] [blame] | 921 | printf("Fail to set rate to %u kHz", freq); |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 922 | return; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | for (i = 1; i <= 8; i++) { |
| 927 | for (j = 1; j <= 8; j++) { |
| 928 | temp = freq * i * j; |
| 929 | if (temp > max || temp < min) |
| 930 | continue; |
| 931 | |
| 932 | if (best == 0 || temp < best) { |
| 933 | best = temp; |
| 934 | pred = i; |
| 935 | postd = j; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | if (best == 0) { |
Heinrich Schuchardt | ab38c39 | 2020-12-25 16:22:27 +0100 | [diff] [blame] | 941 | printf("Fail to set rate to %u kHz", freq); |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
| 945 | debug("best %d, pred = %d, postd = %d\n", best, pred, postd); |
| 946 | |
| 947 | pll_div = best / hck; |
| 948 | pll_denom = 1000000; |
| 949 | pll_num = (best - hck * pll_div) * pll_denom / hck; |
| 950 | |
| 951 | if (enable_pll_video(pll_div, pll_num, pll_denom, post_div)) |
| 952 | return; |
| 953 | |
| 954 | target = CLK_ROOT_ON | LCDIF_PIXEL_CLK_ROOT_FROM_PLL_VIDEO_MAIN_CLK | |
| 955 | CLK_ROOT_PRE_DIV((pred - 1)) | CLK_ROOT_POST_DIV((postd - 1)); |
| 956 | clock_set_target_val(LCDIF_PIXEL_CLK_ROOT, target); |
| 957 | |
| 958 | clock_enable(CCGR_LCDIF, 1); |
| 959 | } |
| 960 | |
| 961 | #ifdef CONFIG_FEC_MXC |
| 962 | int set_clk_enet(enum enet_freq type) |
| 963 | { |
| 964 | u32 target; |
| 965 | int ret; |
| 966 | u32 enet1_ref, enet2_ref; |
| 967 | |
| 968 | /* disable the clock first */ |
| 969 | clock_enable(CCGR_ENET1, 0); |
| 970 | clock_enable(CCGR_ENET2, 0); |
| 971 | |
| 972 | switch (type) { |
Eric Nelson | eadd732 | 2017-08-31 08:34:23 -0700 | [diff] [blame] | 973 | case ENET_125MHZ: |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 974 | enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK; |
| 975 | enet2_ref = ENET2_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK; |
| 976 | break; |
Eric Nelson | eadd732 | 2017-08-31 08:34:23 -0700 | [diff] [blame] | 977 | case ENET_50MHZ: |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 978 | enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK; |
| 979 | enet2_ref = ENET2_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK; |
| 980 | break; |
Eric Nelson | eadd732 | 2017-08-31 08:34:23 -0700 | [diff] [blame] | 981 | case ENET_25MHZ: |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 982 | enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK; |
| 983 | enet2_ref = ENET2_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK; |
| 984 | break; |
| 985 | default: |
| 986 | return -EINVAL; |
| 987 | } |
| 988 | |
| 989 | ret = enable_pll_enet(); |
| 990 | if (ret != 0) |
| 991 | return ret; |
| 992 | |
| 993 | /* set enet axi clock 196M: 392/2 */ |
| 994 | target = CLK_ROOT_ON | ENET_AXI_CLK_ROOT_FROM_PLL_SYS_PFD4_CLK | |
| 995 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 996 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV2); |
| 997 | clock_set_target_val(ENET_AXI_CLK_ROOT, target); |
| 998 | |
| 999 | target = CLK_ROOT_ON | enet1_ref | |
| 1000 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 1001 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 1002 | clock_set_target_val(ENET1_REF_CLK_ROOT, target); |
| 1003 | |
| 1004 | target = CLK_ROOT_ON | ENET1_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK | |
| 1005 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 1006 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 1007 | clock_set_target_val(ENET1_TIME_CLK_ROOT, target); |
| 1008 | |
| 1009 | target = CLK_ROOT_ON | enet2_ref | |
| 1010 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 1011 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 1012 | clock_set_target_val(ENET2_REF_CLK_ROOT, target); |
| 1013 | |
| 1014 | target = CLK_ROOT_ON | ENET2_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK | |
| 1015 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 1016 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4); |
| 1017 | clock_set_target_val(ENET2_TIME_CLK_ROOT, target); |
| 1018 | |
| 1019 | #ifdef CONFIG_FEC_MXC_25M_REF_CLK |
| 1020 | target = CLK_ROOT_ON | |
| 1021 | ENET_PHY_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK | |
| 1022 | CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) | |
| 1023 | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1); |
| 1024 | clock_set_target_val(ENET_PHY_REF_CLK_ROOT, target); |
| 1025 | #endif |
| 1026 | /* enable clock */ |
| 1027 | clock_enable(CCGR_ENET1, 1); |
| 1028 | clock_enable(CCGR_ENET2, 1); |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | #endif |
| 1033 | |
| 1034 | /* Configure PLL/PFD freq */ |
| 1035 | void clock_init(void) |
| 1036 | { |
| 1037 | /* Rom has enabled PLL_ARM, PLL_DDR, PLL_SYS, PLL_ENET |
| 1038 | * In u-boot, we have to: |
| 1039 | * 1. Configure PFD3- PFD7 for freq we needed in u-boot |
| 1040 | * 2. Set clock root for peripherals (ip channel) used in u-boot but without set rate |
| 1041 | * interface. The clocks for these peripherals are enabled after this intialization. |
| 1042 | * 3. Other peripherals with set clock rate interface does not be set in this function. |
| 1043 | */ |
| 1044 | u32 reg; |
| 1045 | |
| 1046 | /* |
| 1047 | * Configure PFD4 to 392M |
| 1048 | * 480M * 18 / 0x16 = 392M |
| 1049 | */ |
| 1050 | reg = readl(&ccm_anatop->pfd_480b); |
| 1051 | |
| 1052 | reg &= ~(ANATOP_PFD480B_PFD4_FRAC_MASK | |
| 1053 | CCM_ANALOG_PFD_480B_PFD4_DIV1_CLKGATE_MASK); |
| 1054 | reg |= ANATOP_PFD480B_PFD4_FRAC_392M_VAL; |
| 1055 | |
| 1056 | writel(reg, &ccm_anatop->pfd_480b); |
| 1057 | |
| 1058 | init_clk_esdhc(); |
| 1059 | init_clk_uart(); |
| 1060 | init_clk_weim(); |
| 1061 | init_clk_ecspi(); |
| 1062 | init_clk_wdog(); |
| 1063 | #ifdef CONFIG_MXC_EPDC |
| 1064 | init_clk_epdc(); |
| 1065 | #endif |
| 1066 | |
| 1067 | enable_usboh3_clk(1); |
| 1068 | |
| 1069 | clock_enable(CCGR_SNVS, 1); |
| 1070 | |
| 1071 | #ifdef CONFIG_NAND_MXS |
| 1072 | clock_enable(CCGR_RAWNAND, 1); |
| 1073 | #endif |
Peng Fan | 2a78474 | 2016-01-28 16:55:03 +0800 | [diff] [blame] | 1074 | |
| 1075 | if (IS_ENABLED(CONFIG_IMX_RDC)) { |
| 1076 | clock_enable(CCGR_RDC, 1); |
| 1077 | clock_enable(CCGR_SEMA1, 1); |
| 1078 | clock_enable(CCGR_SEMA2, 1); |
| 1079 | } |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
Stefano Babic | f8b509b | 2019-09-20 08:47:53 +0200 | [diff] [blame] | 1082 | #ifdef CONFIG_IMX_HAB |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1083 | void hab_caam_clock_enable(unsigned char enable) |
| 1084 | { |
| 1085 | if (enable) |
| 1086 | clock_enable(CCGR_CAAM, 1); |
| 1087 | else |
| 1088 | clock_enable(CCGR_CAAM, 0); |
| 1089 | } |
| 1090 | #endif |
| 1091 | |
| 1092 | #ifdef CONFIG_MXC_EPDC |
| 1093 | void epdc_clock_enable(void) |
| 1094 | { |
| 1095 | clock_enable(CCGR_EPDC, 1); |
| 1096 | } |
| 1097 | void epdc_clock_disable(void) |
| 1098 | { |
| 1099 | clock_enable(CCGR_EPDC, 0); |
| 1100 | } |
| 1101 | #endif |
| 1102 | |
Tom Rini | 2f21887 | 2018-01-03 08:52:39 -0500 | [diff] [blame] | 1103 | #ifndef CONFIG_SPL_BUILD |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1104 | /* |
| 1105 | * Dump some core clockes. |
| 1106 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1107 | int do_mx7_showclocks(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1108 | char *const argv[]) |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1109 | { |
| 1110 | u32 freq; |
| 1111 | freq = decode_pll(PLL_CORE, MXC_HCLK); |
| 1112 | printf("PLL_CORE %8d MHz\n", freq / 1000000); |
| 1113 | freq = decode_pll(PLL_SYS, MXC_HCLK); |
| 1114 | printf("PLL_SYS %8d MHz\n", freq / 1000000); |
| 1115 | freq = decode_pll(PLL_ENET, MXC_HCLK); |
| 1116 | printf("PLL_NET %8d MHz\n", freq / 1000000); |
| 1117 | |
| 1118 | printf("\n"); |
| 1119 | |
Heinrich Schuchardt | ab38c39 | 2020-12-25 16:22:27 +0100 | [diff] [blame] | 1120 | printf("IPG %8u kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000); |
| 1121 | printf("UART %8u kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000); |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1122 | #ifdef CONFIG_MXC_SPI |
Heinrich Schuchardt | ab38c39 | 2020-12-25 16:22:27 +0100 | [diff] [blame] | 1123 | printf("CSPI %8u kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000); |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1124 | #endif |
Heinrich Schuchardt | ab38c39 | 2020-12-25 16:22:27 +0100 | [diff] [blame] | 1125 | printf("AHB %8u kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000); |
| 1126 | printf("AXI %8u kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000); |
| 1127 | printf("DDR %8u kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000); |
| 1128 | printf("USDHC1 %8u kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000); |
| 1129 | printf("USDHC2 %8u kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000); |
| 1130 | printf("USDHC3 %8u kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000); |
Adrian Alonso | eed22a0 | 2015-09-02 13:54:18 -0500 | [diff] [blame] | 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | U_BOOT_CMD( |
| 1136 | clocks, CONFIG_SYS_MAXARGS, 1, do_mx7_showclocks, |
| 1137 | "display clocks", |
| 1138 | "" |
| 1139 | ); |
Tom Rini | 2f21887 | 2018-01-03 08:52:39 -0500 | [diff] [blame] | 1140 | #endif |