Jit Loon Lim | 977071e | 2024-03-12 22:01:03 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2024 Intel Corporation <www.intel.com> |
| 4 | */ |
| 5 | |
| 6 | #include <clk-uclass.h> |
| 7 | #include <config.h> |
| 8 | #include <errno.h> |
| 9 | #include <dm.h> |
| 10 | #include <log.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <stdio.h> |
| 13 | #include <time.h> |
| 14 | #include <vsprintf.h> |
| 15 | #include <asm/global_data.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/system.h> |
| 18 | #include <asm/u-boot.h> |
| 19 | #include <dm/lists.h> |
| 20 | #include <dm/util.h> |
| 21 | #include <linux/bitops.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <asm/arch/clock_manager.h> |
| 26 | #include <dt-bindings/clock/agilex5-clock.h> |
| 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | struct socfpga_clk_plat { |
| 31 | void __iomem *regs; |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * function to write the bypass register which requires a poll of the |
| 36 | * busy bit |
| 37 | */ |
| 38 | static void clk_write_bypass_mainpll(struct socfpga_clk_plat *plat, u32 val) |
| 39 | { |
| 40 | CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_BYPASS); |
| 41 | cm_wait_for_fsm(); |
| 42 | } |
| 43 | |
| 44 | static void clk_write_bypass_perpll(struct socfpga_clk_plat *plat, u32 val) |
| 45 | { |
| 46 | CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_BYPASS); |
| 47 | cm_wait_for_fsm(); |
| 48 | } |
| 49 | |
| 50 | /* function to write the ctrl register which requires a poll of the busy bit */ |
| 51 | static void clk_write_ctrl(struct socfpga_clk_plat *plat, u32 val) |
| 52 | { |
| 53 | CM_REG_WRITEL(plat, val, CLKMGR_CTRL); |
| 54 | cm_wait_for_fsm(); |
| 55 | } |
| 56 | |
| 57 | static const struct { |
| 58 | u32 reg; |
| 59 | u32 val; |
| 60 | u32 mask; |
| 61 | } membus_pll[] = { |
| 62 | { |
| 63 | MEMBUS_CLKSLICE_REG, |
| 64 | /* |
| 65 | * BIT[7:7] |
| 66 | * Enable source synchronous mode |
| 67 | */ |
| 68 | BIT(7), |
| 69 | BIT(7) |
| 70 | }, |
| 71 | { |
| 72 | MEMBUS_SYNTHCALFOSC_INIT_CENTERFREQ_REG, |
| 73 | /* |
| 74 | * BIT[0:0] |
| 75 | * Sets synthcalfosc_init_centerfreq=1 to limit overshoot |
| 76 | * frequency during lock |
| 77 | */ |
| 78 | BIT(0), |
| 79 | BIT(0) |
| 80 | }, |
| 81 | { |
| 82 | MEMBUS_SYNTHPPM_WATCHDOGTMR_VF01_REG, |
| 83 | /* |
| 84 | * BIT[0:0] |
| 85 | * Sets synthppm_watchdogtmr_vf0=1 to give the pll more time |
| 86 | * to settle before lock is asserted. |
| 87 | */ |
| 88 | BIT(0), |
| 89 | BIT(0) |
| 90 | }, |
| 91 | { |
| 92 | MEMBUS_CALCLKSLICE0_DUTY_LOCOVR_REG, |
| 93 | /* |
| 94 | * BIT[6:0] |
| 95 | * Centering duty cycle for clkslice0 output |
| 96 | */ |
| 97 | 0x4a, |
| 98 | GENMASK(6, 0) |
| 99 | }, |
| 100 | { |
| 101 | MEMBUS_CALCLKSLICE1_DUTY_LOCOVR_REG, |
| 102 | /* |
| 103 | * BIT[6:0] |
| 104 | * Centering duty cycle for clkslice1 output |
| 105 | */ |
| 106 | 0x4a, |
| 107 | GENMASK(6, 0) |
| 108 | }, |
| 109 | }; |
| 110 | |
| 111 | static int membus_wait_for_req(struct socfpga_clk_plat *plat, u32 pll, |
| 112 | int timeout) |
| 113 | { |
| 114 | int cnt = 0; |
| 115 | u32 req_status; |
| 116 | |
| 117 | if (pll == MEMBUS_MAINPLL) |
| 118 | req_status = CM_REG_READL(plat, CLKMGR_MAINPLL_MEM); |
| 119 | else |
| 120 | req_status = CM_REG_READL(plat, CLKMGR_PERPLL_MEM); |
| 121 | |
| 122 | while ((cnt < timeout) && (req_status & CLKMGR_MEM_REQ_SET_MSK)) { |
| 123 | if (pll == MEMBUS_MAINPLL) |
| 124 | req_status = CM_REG_READL(plat, CLKMGR_MAINPLL_MEM); |
| 125 | else |
| 126 | req_status = CM_REG_READL(plat, CLKMGR_PERPLL_MEM); |
| 127 | cnt++; |
| 128 | } |
| 129 | |
| 130 | if (cnt >= timeout) |
| 131 | return -ETIMEDOUT; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int membus_write_pll(struct socfpga_clk_plat *plat, u32 pll, |
| 137 | u32 addr_offset, u32 wdat, int timeout) |
| 138 | { |
| 139 | u32 addr; |
| 140 | u32 val; |
| 141 | |
| 142 | addr = ((addr_offset | CLKMGR_MEM_ADDR_START) & CLKMGR_MEM_ADDR_MASK); |
| 143 | |
| 144 | val = (CLKMGR_MEM_REQ_SET_MSK | CLKMGR_MEM_WR_SET_MSK | |
| 145 | (wdat << CLKMGR_MEM_WDAT_LSB_OFFSET) | addr); |
| 146 | |
| 147 | if (pll == MEMBUS_MAINPLL) |
| 148 | CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_MEM); |
| 149 | else |
| 150 | CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_MEM); |
| 151 | |
| 152 | debug("MEMBUS: Write 0x%08x to addr = 0x%08x\n", wdat, addr); |
| 153 | |
| 154 | return membus_wait_for_req(plat, pll, timeout); |
| 155 | } |
| 156 | |
| 157 | static int membus_read_pll(struct socfpga_clk_plat *plat, u32 pll, |
| 158 | u32 addr_offset, u32 *rdata, int timeout) |
| 159 | { |
| 160 | u32 addr; |
| 161 | u32 val; |
| 162 | |
| 163 | addr = ((addr_offset | CLKMGR_MEM_ADDR_START) & CLKMGR_MEM_ADDR_MASK); |
| 164 | |
| 165 | val = ((CLKMGR_MEM_REQ_SET_MSK & ~CLKMGR_MEM_WR_SET_MSK) | addr); |
| 166 | |
| 167 | if (pll == MEMBUS_MAINPLL) |
| 168 | CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_MEM); |
| 169 | else |
| 170 | CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_MEM); |
| 171 | |
| 172 | *rdata = 0; |
| 173 | |
| 174 | if (membus_wait_for_req(plat, pll, timeout)) |
| 175 | return -ETIMEDOUT; |
| 176 | |
| 177 | if (pll == MEMBUS_MAINPLL) |
| 178 | *rdata = CM_REG_READL(plat, CLKMGR_MAINPLL_MEMSTAT); |
| 179 | else |
| 180 | *rdata = CM_REG_READL(plat, CLKMGR_PERPLL_MEMSTAT); |
| 181 | |
| 182 | debug("MEMBUS: Read 0x%08x from addr = 0x%08x\n", *rdata, addr); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static void membus_pll_configs(struct socfpga_clk_plat *plat, u32 pll) |
| 188 | { |
| 189 | int i; |
| 190 | u32 rdata; |
| 191 | |
| 192 | for (i = 0; i < ARRAY_SIZE(membus_pll); i++) { |
| 193 | membus_read_pll(plat, pll, membus_pll[i].reg, |
| 194 | &rdata, MEMBUS_TIMEOUT); |
| 195 | membus_write_pll(plat, pll, membus_pll[i].reg, |
| 196 | ((rdata & ~membus_pll[i].mask) | |
| 197 | membus_pll[i].val), |
| 198 | MEMBUS_TIMEOUT); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static u32 calc_vocalib_pll(u32 pllm, u32 pllglob) |
| 203 | { |
| 204 | u32 mdiv, refclkdiv, arefclkdiv, drefclkdiv, mscnt, hscnt, vcocalib; |
| 205 | |
| 206 | mdiv = pllm & CLKMGR_PLLM_MDIV_MASK; |
| 207 | arefclkdiv = (pllglob & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >> |
| 208 | CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET; |
| 209 | drefclkdiv = (pllglob & CLKMGR_PLLGLOB_DREFCLKDIV_MASK) >> |
| 210 | CLKMGR_PLLGLOB_DREFCLKDIV_OFFSET; |
| 211 | refclkdiv = (pllglob & CLKMGR_PLLGLOB_REFCLKDIV_MASK) >> |
| 212 | CLKMGR_PLLGLOB_REFCLKDIV_OFFSET; |
| 213 | mscnt = CLKMGR_VCOCALIB_MSCNT_CONST / (mdiv * BIT(drefclkdiv)); |
| 214 | if (!mscnt) |
| 215 | mscnt = 1; |
| 216 | hscnt = (mdiv * mscnt * BIT(drefclkdiv) / refclkdiv) - |
| 217 | CLKMGR_VCOCALIB_HSCNT_CONST; |
| 218 | vcocalib = (hscnt & CLKMGR_VCOCALIB_HSCNT_MASK) | |
| 219 | ((mscnt << CLKMGR_VCOCALIB_MSCNT_OFFSET) & |
| 220 | CLKMGR_VCOCALIB_MSCNT_MASK); |
| 221 | |
| 222 | /* Dump all the pll calibration settings for debug purposes */ |
| 223 | debug("mdiv : %d\n", mdiv); |
| 224 | debug("arefclkdiv : %d\n", arefclkdiv); |
| 225 | debug("drefclkdiv : %d\n", drefclkdiv); |
| 226 | debug("refclkdiv : %d\n", refclkdiv); |
| 227 | debug("mscnt : %d\n", mscnt); |
| 228 | debug("hscnt : %d\n", hscnt); |
| 229 | debug("vcocalib : 0x%08x\n", vcocalib); |
| 230 | |
| 231 | return vcocalib; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Setup clocks while making no assumptions about previous state of the clocks. |
| 236 | */ |
| 237 | static void clk_basic_init(struct udevice *dev, |
| 238 | const struct cm_config * const cfg) |
| 239 | { |
| 240 | struct socfpga_clk_plat *plat = dev_get_plat(dev); |
| 241 | u32 vcocalib; |
| 242 | |
| 243 | if (!cfg) |
| 244 | return; |
| 245 | |
| 246 | if (IS_ENABLED(CONFIG_TARGET_SOCFPGA_AGILEX5_EMU)) { |
| 247 | /* Take both PLL out of reset and power up */ |
| 248 | CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 249 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 250 | CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLGLOB, |
| 251 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 252 | |
| 253 | cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK); |
| 254 | |
| 255 | /* Put both PLLs in bypass */ |
| 256 | clk_write_bypass_mainpll(plat, CLKMGR_BYPASS_MAINPLL_ALL); |
| 257 | clk_write_bypass_perpll(plat, CLKMGR_BYPASS_PERPLL_ALL); |
| 258 | |
| 259 | /* Take all PLLs out of bypass */ |
| 260 | clk_write_bypass_mainpll(plat, 0); |
| 261 | clk_write_bypass_perpll(plat, 0); |
| 262 | |
| 263 | /* Out of boot mode */ |
| 264 | clk_write_ctrl(plat, |
| 265 | CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE); |
| 266 | } else { |
| 267 | #ifdef CONFIG_SPL_BUILD |
| 268 | /* Always force clock manager into boot mode before any configuration */ |
| 269 | clk_write_ctrl(plat, |
| 270 | CM_REG_READL(plat, CLKMGR_CTRL) | CLKMGR_CTRL_BOOTMODE); |
| 271 | #else |
| 272 | /* Skip clock configuration in SSBL if it's not in boot mode */ |
| 273 | if (!(CM_REG_READL(plat, CLKMGR_CTRL) & CLKMGR_CTRL_BOOTMODE)) |
| 274 | return; |
| 275 | #endif |
| 276 | |
| 277 | /* Put both PLLs in bypass */ |
| 278 | clk_write_bypass_mainpll(plat, CLKMGR_BYPASS_MAINPLL_ALL); |
| 279 | clk_write_bypass_perpll(plat, CLKMGR_BYPASS_PERPLL_ALL); |
| 280 | |
| 281 | /* Put both PLLs in Reset and Power Down */ |
| 282 | CM_REG_CLRBITS(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 283 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 284 | CM_REG_CLRBITS(plat, CLKMGR_PERPLL_PLLGLOB, |
| 285 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 286 | |
| 287 | /* setup main PLL dividers where calculate the vcocalib value */ |
| 288 | vcocalib = calc_vocalib_pll(cfg->main_pll_pllm, cfg->main_pll_pllglob); |
| 289 | CM_REG_WRITEL(plat, cfg->main_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK, |
| 290 | CLKMGR_MAINPLL_PLLGLOB); |
| 291 | CM_REG_WRITEL(plat, cfg->main_pll_fdbck, CLKMGR_MAINPLL_FDBCK); |
| 292 | CM_REG_WRITEL(plat, vcocalib, CLKMGR_MAINPLL_VCOCALIB); |
| 293 | CM_REG_WRITEL(plat, cfg->main_pll_pllc0, CLKMGR_MAINPLL_PLLC0); |
| 294 | CM_REG_WRITEL(plat, cfg->main_pll_pllc1, CLKMGR_MAINPLL_PLLC1); |
| 295 | CM_REG_WRITEL(plat, cfg->main_pll_pllc2, CLKMGR_MAINPLL_PLLC2); |
| 296 | CM_REG_WRITEL(plat, cfg->main_pll_pllc3, CLKMGR_MAINPLL_PLLC3); |
| 297 | CM_REG_WRITEL(plat, cfg->main_pll_pllm, CLKMGR_MAINPLL_PLLM); |
| 298 | CM_REG_WRITEL(plat, cfg->main_pll_nocclk, CLKMGR_MAINPLL_NOCCLK); |
| 299 | CM_REG_WRITEL(plat, cfg->main_pll_nocdiv, CLKMGR_MAINPLL_NOCDIV); |
| 300 | |
| 301 | /* setup peripheral PLL dividers where calculate the vcocalib value */ |
| 302 | vcocalib = calc_vocalib_pll(cfg->per_pll_pllm, cfg->per_pll_pllglob); |
| 303 | CM_REG_WRITEL(plat, cfg->per_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK, |
| 304 | CLKMGR_PERPLL_PLLGLOB); |
| 305 | CM_REG_WRITEL(plat, cfg->per_pll_fdbck, CLKMGR_PERPLL_FDBCK); |
| 306 | CM_REG_WRITEL(plat, vcocalib, CLKMGR_PERPLL_VCOCALIB); |
| 307 | CM_REG_WRITEL(plat, cfg->per_pll_pllc0, CLKMGR_PERPLL_PLLC0); |
| 308 | CM_REG_WRITEL(plat, cfg->per_pll_pllc1, CLKMGR_PERPLL_PLLC1); |
| 309 | CM_REG_WRITEL(plat, cfg->per_pll_pllc2, CLKMGR_PERPLL_PLLC2); |
| 310 | CM_REG_WRITEL(plat, cfg->per_pll_pllc3, CLKMGR_PERPLL_PLLC3); |
| 311 | CM_REG_WRITEL(plat, cfg->per_pll_pllm, CLKMGR_PERPLL_PLLM); |
| 312 | CM_REG_WRITEL(plat, cfg->per_pll_emacctl, CLKMGR_PERPLL_EMACCTL); |
| 313 | CM_REG_WRITEL(plat, cfg->per_pll_gpiodiv, CLKMGR_PERPLL_GPIODIV); |
| 314 | |
| 315 | /* Configure ping pong counters in control group */ |
| 316 | CM_REG_WRITEL(plat, cfg->ctl_emacactr, CLKMGR_CTL_EMACACTR); |
| 317 | CM_REG_WRITEL(plat, cfg->ctl_emacbctr, CLKMGR_CTL_EMACBCTR); |
| 318 | CM_REG_WRITEL(plat, cfg->ctl_emacptpctr, CLKMGR_CTL_EMACPTPCTR); |
| 319 | CM_REG_WRITEL(plat, cfg->ctl_gpiodbctr, CLKMGR_CTL_GPIODBCTR); |
| 320 | CM_REG_WRITEL(plat, cfg->ctl_s2fuser0ctr, CLKMGR_CTL_S2FUSER0CTR); |
| 321 | CM_REG_WRITEL(plat, cfg->ctl_s2fuser1ctr, CLKMGR_CTL_S2FUSER1CTR); |
| 322 | CM_REG_WRITEL(plat, cfg->ctl_psirefctr, CLKMGR_CTL_PSIREFCTR); |
| 323 | CM_REG_WRITEL(plat, cfg->ctl_usb31ctr, CLKMGR_CTL_USB31CTR); |
| 324 | CM_REG_WRITEL(plat, cfg->ctl_dsuctr, CLKMGR_CTL_DSUCTR); |
| 325 | CM_REG_WRITEL(plat, cfg->ctl_core01ctr, CLKMGR_CTL_CORE01CTR); |
| 326 | CM_REG_WRITEL(plat, cfg->ctl_core23ctr, CLKMGR_CTL_CORE23CTR); |
| 327 | CM_REG_WRITEL(plat, cfg->ctl_core2ctr, CLKMGR_CTL_CORE2CTR); |
| 328 | CM_REG_WRITEL(plat, cfg->ctl_core3ctr, CLKMGR_CTL_CORE3CTR); |
| 329 | |
| 330 | /* Take both PLL out of reset and power up */ |
| 331 | CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 332 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 333 | CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLGLOB, |
| 334 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 335 | |
| 336 | /* Membus programming for mainpll */ |
| 337 | membus_pll_configs(plat, MEMBUS_MAINPLL); |
| 338 | /* Membus programming for peripll */ |
| 339 | membus_pll_configs(plat, MEMBUS_PERPLL); |
| 340 | |
| 341 | /* Enable Main pll clkslices */ |
| 342 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC0) | |
| 343 | CLKMGR_PLLCX_EN_SET_MSK, |
| 344 | CLKMGR_MAINPLL_PLLC0); |
| 345 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC1) | |
| 346 | CLKMGR_PLLCX_EN_SET_MSK, |
| 347 | CLKMGR_MAINPLL_PLLC1); |
| 348 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC2) | |
| 349 | CLKMGR_PLLCX_EN_SET_MSK, |
| 350 | CLKMGR_MAINPLL_PLLC2); |
| 351 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC3) | |
| 352 | CLKMGR_PLLCX_EN_SET_MSK, |
| 353 | CLKMGR_MAINPLL_PLLC3); |
| 354 | |
| 355 | /* Enable Periph pll clkslices */ |
| 356 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLC0) | |
| 357 | CLKMGR_PLLCX_EN_SET_MSK, |
| 358 | CLKMGR_PERPLL_PLLC0); |
| 359 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLC1) | |
| 360 | CLKMGR_PLLCX_EN_SET_MSK, |
| 361 | CLKMGR_PERPLL_PLLC1); |
| 362 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLC2) | |
| 363 | CLKMGR_PLLCX_EN_SET_MSK, |
| 364 | CLKMGR_PERPLL_PLLC2); |
| 365 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLC3) | |
| 366 | CLKMGR_PLLCX_EN_SET_MSK, |
| 367 | CLKMGR_PERPLL_PLLC3); |
| 368 | |
| 369 | cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK); |
| 370 | |
| 371 | CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_MAINPLL_LOSTLOCK); |
| 372 | CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_PERPLL_LOSTLOCK); |
| 373 | |
| 374 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLGLOB) | |
| 375 | CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK, |
| 376 | CLKMGR_MAINPLL_PLLGLOB); |
| 377 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLGLOB) | |
| 378 | CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK, |
| 379 | CLKMGR_PERPLL_PLLGLOB); |
| 380 | |
| 381 | /* Take all PLLs out of bypass */ |
| 382 | clk_write_bypass_mainpll(plat, 0); |
| 383 | clk_write_bypass_perpll(plat, 0); |
| 384 | |
| 385 | /* Clear the loss of lock bits (write 1 to clear) */ |
| 386 | CM_REG_CLRBITS(plat, CLKMGR_INTRCLR, |
| 387 | CLKMGR_INTER_PERPLLLOST_MASK | |
| 388 | CLKMGR_INTER_MAINPLLLOST_MASK); |
| 389 | |
| 390 | /* Take all ping pong counters out of reset */ |
| 391 | CM_REG_CLRBITS(plat, CLKMGR_CTL_EXTCNTRST, |
| 392 | CLKMGR_CTL_EXTCNTRST_ALLCNTRST); |
| 393 | |
| 394 | #ifdef COUNTER_FREQUENCY_REAL |
| 395 | u32 cntfrq = COUNTER_FREQUENCY_REAL; |
| 396 | u32 counter_freq = 0; |
| 397 | |
| 398 | /* Update with accurate clock frequency */ |
| 399 | if (current_el() == 3) { |
| 400 | asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory"); |
| 401 | asm volatile("mrs %0, cntfrq_el0" : "=r" (counter_freq)); |
| 402 | debug("Counter freq = 0x%x\n", counter_freq); |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | /* Out of boot mode */ |
| 407 | clk_write_ctrl(plat, |
| 408 | CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static u64 clk_get_vco_clk_hz(struct socfpga_clk_plat *plat, |
| 413 | u32 pllglob_reg, u32 pllm_reg) |
| 414 | { |
| 415 | u64 fref, arefdiv, mdiv, reg, vco; |
| 416 | |
| 417 | reg = CM_REG_READL(plat, pllglob_reg); |
| 418 | |
| 419 | fref = (reg & CLKMGR_PLLGLOB_VCO_PSRC_MASK) >> |
| 420 | CLKMGR_PLLGLOB_VCO_PSRC_OFFSET; |
| 421 | |
| 422 | switch (fref) { |
| 423 | case CLKMGR_VCO_PSRC_EOSC1: |
| 424 | fref = cm_get_osc_clk_hz(); |
| 425 | break; |
| 426 | case CLKMGR_VCO_PSRC_INTOSC: |
| 427 | fref = cm_get_intosc_clk_hz(); |
| 428 | break; |
| 429 | case CLKMGR_VCO_PSRC_F2S: |
| 430 | fref = cm_get_fpga_clk_hz(); |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | arefdiv = (reg & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >> |
| 435 | CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET; |
| 436 | |
| 437 | mdiv = CM_REG_READL(plat, pllm_reg) & CLKMGR_PLLM_MDIV_MASK; |
| 438 | |
| 439 | vco = fref / arefdiv; |
| 440 | vco = vco * mdiv; |
| 441 | |
| 442 | return vco; |
| 443 | } |
| 444 | |
| 445 | static u64 clk_get_main_vco_clk_hz(struct socfpga_clk_plat *plat) |
| 446 | { |
| 447 | return clk_get_vco_clk_hz(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 448 | CLKMGR_MAINPLL_PLLM); |
| 449 | } |
| 450 | |
| 451 | static u64 clk_get_per_vco_clk_hz(struct socfpga_clk_plat *plat) |
| 452 | { |
| 453 | return clk_get_vco_clk_hz(plat, CLKMGR_PERPLL_PLLGLOB, |
| 454 | CLKMGR_PERPLL_PLLM); |
| 455 | } |
| 456 | |
| 457 | static u32 clk_get_5_1_clk_src(struct socfpga_clk_plat *plat, u64 reg) |
| 458 | { |
| 459 | u32 clksrc = CM_REG_READL(plat, reg); |
| 460 | |
| 461 | return (clksrc & CLKMGR_CLKSRC_MASK) >> CLKMGR_CLKSRC_OFFSET; |
| 462 | } |
| 463 | |
| 464 | static u64 clk_get_clksrc_hz(struct socfpga_clk_plat *plat, u32 clksrc_reg, |
| 465 | u32 main_reg, u32 per_reg) |
| 466 | { |
| 467 | u64 clock; |
| 468 | u32 clklsrc = clk_get_5_1_clk_src(plat, clksrc_reg); |
| 469 | |
| 470 | switch (clklsrc) { |
| 471 | case CLKMGR_CLKSRC_MAIN: |
| 472 | clock = clk_get_main_vco_clk_hz(plat); |
| 473 | clock /= (CM_REG_READL(plat, main_reg) & |
| 474 | CLKMGR_CLKCNT_MSK); |
| 475 | break; |
| 476 | |
| 477 | case CLKMGR_CLKSRC_PER: |
| 478 | clock = clk_get_per_vco_clk_hz(plat); |
| 479 | clock /= (CM_REG_READL(plat, per_reg) & |
| 480 | CLKMGR_CLKCNT_MSK); |
| 481 | break; |
| 482 | |
| 483 | case CLKMGR_CLKSRC_OSC1: |
| 484 | clock = cm_get_osc_clk_hz(); |
| 485 | break; |
| 486 | |
| 487 | case CLKMGR_CLKSRC_INTOSC: |
| 488 | clock = cm_get_intosc_clk_hz(); |
| 489 | break; |
| 490 | |
| 491 | case CLKMGR_CLKSRC_FPGA: |
| 492 | clock = cm_get_fpga_clk_hz(); |
| 493 | break; |
| 494 | default: |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | return clock; |
| 499 | } |
| 500 | |
| 501 | static u64 clk_get_mpu_clk_hz(struct socfpga_clk_plat *plat) |
| 502 | { |
| 503 | u64 clock; |
| 504 | u32 ctr_reg; |
| 505 | u32 cpu = ((read_mpidr() >> MPIDR_AFF1_OFFSET) & MPIDR_AFF1_OFFSET); |
| 506 | |
| 507 | if (cpu > CORE1) { |
| 508 | ctr_reg = CLKMGR_CTL_CORE23CTR; |
| 509 | |
| 510 | clock = clk_get_clksrc_hz(plat, ctr_reg, |
| 511 | CLKMGR_MAINPLL_PLLC0, |
| 512 | CLKMGR_PERPLL_PLLC0); |
| 513 | } else { |
| 514 | ctr_reg = CLKMGR_CTL_CORE01CTR; |
| 515 | |
| 516 | clock = clk_get_clksrc_hz(plat, ctr_reg, |
| 517 | CLKMGR_MAINPLL_PLLC1, |
| 518 | CLKMGR_PERPLL_PLLC0); |
| 519 | } |
| 520 | |
| 521 | if (cpu == CORE3) |
| 522 | ctr_reg = CLKMGR_CTL_CORE3CTR; |
| 523 | else if (cpu == CORE2) |
| 524 | ctr_reg = CLKMGR_CTL_CORE2CTR; |
| 525 | else |
| 526 | ctr_reg = CLKMGR_CTL_CORE01CTR; |
| 527 | |
| 528 | clock /= 1 + (CM_REG_READL(plat, ctr_reg) & |
| 529 | CLKMGR_CLKCNT_MSK); |
| 530 | |
| 531 | return clock; |
| 532 | } |
| 533 | |
| 534 | static u32 clk_get_l3_main_clk_hz(struct socfpga_clk_plat *plat) |
| 535 | { |
| 536 | return clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_NOCCLK, |
| 537 | CLKMGR_MAINPLL_PLLC3, |
| 538 | CLKMGR_PERPLL_PLLC1); |
| 539 | } |
| 540 | |
| 541 | static u32 clk_get_l4_main_clk_hz(struct socfpga_clk_plat *plat) |
| 542 | { |
| 543 | u64 clock = clk_get_l3_main_clk_hz(plat); |
| 544 | |
| 545 | return clock; |
| 546 | } |
| 547 | |
| 548 | static u32 clk_get_l4_sp_clk_hz(struct socfpga_clk_plat *plat) |
| 549 | { |
| 550 | u64 clock = clk_get_l3_main_clk_hz(plat); |
| 551 | |
| 552 | clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >> |
| 553 | CLKMGR_NOCDIV_L4SPCLK_OFFSET) & |
| 554 | CLKMGR_NOCDIV_DIVIDER_MASK); |
| 555 | |
| 556 | return clock; |
| 557 | } |
| 558 | |
| 559 | static u32 clk_get_l4_mp_clk_hz(struct socfpga_clk_plat *plat) |
| 560 | { |
| 561 | u64 clock = clk_get_l3_main_clk_hz(plat); |
| 562 | |
| 563 | clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >> |
| 564 | CLKMGR_NOCDIV_L4MPCLK_OFFSET) & |
| 565 | CLKMGR_NOCDIV_DIVIDER_MASK); |
| 566 | |
| 567 | return clock; |
| 568 | } |
| 569 | |
| 570 | static u32 clk_get_sdmmc_clk_hz(struct socfpga_clk_plat *plat) |
| 571 | { |
| 572 | u64 clock = clk_get_l4_mp_clk_hz(plat); |
| 573 | |
| 574 | clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >> |
| 575 | CLKMGR_NOCDIV_SOFTPHY_OFFSET) & |
| 576 | CLKMGR_NOCDIV_DIVIDER_MASK); |
| 577 | |
| 578 | return clock; |
| 579 | } |
| 580 | |
| 581 | static u32 clk_get_l4_sys_free_clk_hz(struct socfpga_clk_plat *plat) |
| 582 | { |
| 583 | if (CM_REG_READL(plat, CLKMGR_STAT) & CLKMGR_STAT_BOOTMODE) |
| 584 | return clk_get_l3_main_clk_hz(plat) / 2; |
| 585 | |
| 586 | return clk_get_l3_main_clk_hz(plat) / 4; |
| 587 | } |
| 588 | |
| 589 | static u32 clk_get_emac_clk_hz(struct socfpga_clk_plat *plat, u32 emac_id) |
| 590 | { |
| 591 | u32 ctl; |
| 592 | u32 ctr_reg; |
| 593 | u32 clock; |
| 594 | u32 div; |
| 595 | u32 reg; |
| 596 | |
| 597 | if (emac_id == AGILEX5_EMAC_PTP_CLK) { |
| 598 | reg = CM_REG_READL(plat, CLKMGR_CTL_EMACPTPCTR); |
| 599 | ctr_reg = CLKMGR_CTL_EMACPTPCTR; |
| 600 | } else { |
| 601 | reg = CM_REG_READL(plat, CLKMGR_CTL_EMACACTR); |
| 602 | ctl = CM_REG_READL(plat, CLKMGR_PERPLL_EMACCTL); |
| 603 | if (emac_id == AGILEX5_EMAC0_CLK) |
| 604 | ctl = (ctl & CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_MASK) >> |
| 605 | CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_OFFSET; |
| 606 | else if (emac_id == AGILEX5_EMAC1_CLK) |
| 607 | ctl = (ctl & CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_MASK) >> |
| 608 | CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_OFFSET; |
| 609 | else if (emac_id == AGILEX5_EMAC2_CLK) |
| 610 | ctl = (ctl & CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_MASK) >> |
| 611 | CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_OFFSET; |
| 612 | else |
| 613 | return 0; |
| 614 | |
| 615 | if (ctl) { |
| 616 | /* EMAC B source */ |
| 617 | ctr_reg = CLKMGR_CTL_EMACBCTR; |
| 618 | } else { |
| 619 | /* EMAC A source */ |
| 620 | ctr_reg = CLKMGR_CTL_EMACACTR; |
| 621 | } |
| 622 | } |
| 623 | /* Get EMAC clock source */ |
| 624 | clock = (reg & CLKMGR_CTL_EMACCTR_SRC_MASK) |
| 625 | >> CLKMGR_CTL_EMACCTR_SRC_OFFSET; |
| 626 | |
| 627 | reg = CM_REG_READL(plat, ctr_reg); |
| 628 | div = (reg & CLKMGR_CTL_EMACCTR_CNT_MASK) |
| 629 | >> CLKMGR_CTL_EMACCTR_CNT_OFFSET; |
| 630 | |
| 631 | switch (clock) { |
| 632 | case CLKMGR_CLKSRC_MAIN: |
| 633 | clock = clk_get_main_vco_clk_hz(plat); |
| 634 | |
| 635 | if (emac_id == AGILEX5_EMAC_PTP_CLK) { |
| 636 | clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC3) & |
| 637 | CLKMGR_CLKCNT_MSK); |
| 638 | } else { |
| 639 | clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC1) & |
| 640 | CLKMGR_CLKCNT_MSK); |
| 641 | } |
| 642 | break; |
| 643 | |
| 644 | case CLKMGR_CLKSRC_PER: |
| 645 | clock = clk_get_per_vco_clk_hz(plat); |
| 646 | |
| 647 | clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC3) & |
| 648 | CLKMGR_CLKCNT_MSK); |
| 649 | break; |
| 650 | |
| 651 | case CLKMGR_CLKSRC_OSC1: |
| 652 | clock = cm_get_osc_clk_hz(); |
| 653 | break; |
| 654 | |
| 655 | case CLKMGR_CLKSRC_INTOSC: |
| 656 | clock = cm_get_intosc_clk_hz(); |
| 657 | break; |
| 658 | |
| 659 | case CLKMGR_CLKSRC_FPGA: |
| 660 | clock = cm_get_fpga_clk_hz(); |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | clock /= 1 + div; |
| 665 | |
| 666 | return clock; |
| 667 | } |
| 668 | |
| 669 | static ulong socfpga_clk_get_rate(struct clk *clk) |
| 670 | { |
| 671 | struct socfpga_clk_plat *plat = dev_get_plat(clk->dev); |
| 672 | |
| 673 | switch (clk->id) { |
| 674 | case AGILEX5_MPU_CLK: |
| 675 | return clk_get_mpu_clk_hz(plat); |
| 676 | case AGILEX5_L4_MAIN_CLK: |
| 677 | return clk_get_l4_main_clk_hz(plat); |
| 678 | case AGILEX5_L4_SYS_FREE_CLK: |
| 679 | return clk_get_l4_sys_free_clk_hz(plat); |
| 680 | case AGILEX5_L4_MP_CLK: |
| 681 | return clk_get_l4_mp_clk_hz(plat); |
| 682 | case AGILEX5_L4_SP_CLK: |
| 683 | return clk_get_l4_sp_clk_hz(plat); |
| 684 | case AGILEX5_SDMMC_CLK: |
| 685 | case AGILEX5_NAND_CLK: |
| 686 | return clk_get_sdmmc_clk_hz(plat); |
| 687 | case AGILEX5_EMAC0_CLK: |
| 688 | case AGILEX5_EMAC1_CLK: |
| 689 | case AGILEX5_EMAC2_CLK: |
| 690 | case AGILEX5_EMAC_PTP_CLK: |
| 691 | return clk_get_emac_clk_hz(plat, clk->id); |
| 692 | case AGILEX5_USB_CLK: |
| 693 | case AGILEX5_NAND_X_CLK: |
| 694 | return clk_get_l4_mp_clk_hz(plat); |
| 695 | default: |
| 696 | return -ENXIO; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static int socfpga_clk_enable(struct clk *clk) |
| 701 | { |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int socfpga_clk_probe(struct udevice *dev) |
| 706 | { |
| 707 | const struct cm_config *cm_default_cfg = cm_get_default_config(); |
| 708 | |
| 709 | clk_basic_init(dev, cm_default_cfg); |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static int socfpga_clk_of_to_plat(struct udevice *dev) |
| 715 | { |
| 716 | struct socfpga_clk_plat *plat = dev_get_plat(dev); |
| 717 | fdt_addr_t addr; |
| 718 | |
| 719 | addr = dev_read_addr(dev); |
| 720 | if (addr == FDT_ADDR_T_NONE) |
| 721 | return -EINVAL; |
| 722 | plat->regs = (void __iomem *)addr; |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static struct clk_ops socfpga_clk_ops = { |
| 728 | .enable = socfpga_clk_enable, |
| 729 | .get_rate = socfpga_clk_get_rate, |
| 730 | }; |
| 731 | |
| 732 | static const struct udevice_id socfpga_clk_match[] = { |
| 733 | { .compatible = "intel,agilex5-clkmgr" }, |
| 734 | {} |
| 735 | }; |
| 736 | |
| 737 | U_BOOT_DRIVER(socfpga_agilex5_clk) = { |
| 738 | .name = "clk-agilex5", |
| 739 | .id = UCLASS_CLK, |
| 740 | .of_match = socfpga_clk_match, |
| 741 | .ops = &socfpga_clk_ops, |
| 742 | .probe = socfpga_clk_probe, |
| 743 | .of_to_plat = socfpga_clk_of_to_plat, |
| 744 | .plat_auto = sizeof(struct socfpga_clk_plat), |
| 745 | }; |