Ley Foon Tan | ef5458f | 2019-11-27 15:55:22 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2019 Intel Corporation <www.intel.com> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 7 | #include <log.h> |
Ley Foon Tan | ef5458f | 2019-11-27 15:55:22 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <clk-uclass.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/lists.h> |
| 12 | #include <dm/util.h> |
| 13 | #include <dt-bindings/clock/agilex-clock.h> |
| 14 | |
| 15 | #include <asm/arch/clock_manager.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct socfpga_clk_platdata { |
| 20 | void __iomem *regs; |
| 21 | }; |
| 22 | |
| 23 | /* |
| 24 | * function to write the bypass register which requires a poll of the |
| 25 | * busy bit |
| 26 | */ |
| 27 | static void clk_write_bypass_mainpll(struct socfpga_clk_platdata *plat, u32 val) |
| 28 | { |
| 29 | CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_BYPASS); |
| 30 | cm_wait_for_fsm(); |
| 31 | } |
| 32 | |
| 33 | static void clk_write_bypass_perpll(struct socfpga_clk_platdata *plat, u32 val) |
| 34 | { |
| 35 | CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_BYPASS); |
| 36 | cm_wait_for_fsm(); |
| 37 | } |
| 38 | |
| 39 | /* function to write the ctrl register which requires a poll of the busy bit */ |
| 40 | static void clk_write_ctrl(struct socfpga_clk_platdata *plat, u32 val) |
| 41 | { |
| 42 | CM_REG_WRITEL(plat, val, CLKMGR_CTRL); |
| 43 | cm_wait_for_fsm(); |
| 44 | } |
| 45 | |
| 46 | #define MEMBUS_MAINPLL 0 |
| 47 | #define MEMBUS_PERPLL 1 |
| 48 | #define MEMBUS_TIMEOUT 1000 |
| 49 | #define MEMBUS_ADDR_CLKSLICE 0x27 |
| 50 | #define MEMBUS_CLKSLICE_SYNC_MODE_EN 0x80 |
| 51 | |
| 52 | static int membus_wait_for_req(struct socfpga_clk_platdata *plat, u32 pll, |
| 53 | int timeout) |
| 54 | { |
| 55 | int cnt = 0; |
| 56 | u32 req_status; |
| 57 | |
| 58 | if (pll == MEMBUS_MAINPLL) |
| 59 | req_status = CM_REG_READL(plat, CLKMGR_MAINPLL_MEM); |
| 60 | else |
| 61 | req_status = CM_REG_READL(plat, CLKMGR_PERPLL_MEM); |
| 62 | |
| 63 | while ((cnt < timeout) && (req_status & CLKMGR_MEM_REQ_SET_MSK)) { |
| 64 | if (pll == MEMBUS_MAINPLL) |
| 65 | req_status = CM_REG_READL(plat, CLKMGR_MAINPLL_MEM); |
| 66 | else |
| 67 | req_status = CM_REG_READL(plat, CLKMGR_PERPLL_MEM); |
| 68 | cnt++; |
| 69 | } |
| 70 | |
| 71 | if (cnt >= timeout) |
| 72 | return -ETIMEDOUT; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int membus_write_pll(struct socfpga_clk_platdata *plat, u32 pll, |
| 78 | u32 addr_offset, u32 wdat, int timeout) |
| 79 | { |
| 80 | u32 addr; |
| 81 | u32 val; |
| 82 | |
| 83 | addr = ((addr_offset | CLKMGR_MEM_ADDR_START) & CLKMGR_MEM_ADDR_MASK); |
| 84 | |
| 85 | val = (CLKMGR_MEM_REQ_SET_MSK | CLKMGR_MEM_WR_SET_MSK | |
| 86 | (wdat << CLKMGR_MEM_WDAT_LSB_OFFSET) | addr); |
| 87 | |
| 88 | if (pll == MEMBUS_MAINPLL) |
| 89 | CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_MEM); |
| 90 | else |
| 91 | CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_MEM); |
| 92 | |
| 93 | debug("MEMBUS: Write 0x%08x to addr = 0x%08x\n", wdat, addr); |
| 94 | |
| 95 | return membus_wait_for_req(plat, pll, timeout); |
| 96 | } |
| 97 | |
| 98 | static int membus_read_pll(struct socfpga_clk_platdata *plat, u32 pll, |
| 99 | u32 addr_offset, u32 *rdata, int timeout) |
| 100 | { |
| 101 | u32 addr; |
| 102 | u32 val; |
| 103 | |
| 104 | addr = ((addr_offset | CLKMGR_MEM_ADDR_START) & CLKMGR_MEM_ADDR_MASK); |
| 105 | |
| 106 | val = ((CLKMGR_MEM_REQ_SET_MSK & ~CLKMGR_MEM_WR_SET_MSK) | addr); |
| 107 | |
| 108 | if (pll == MEMBUS_MAINPLL) |
| 109 | CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_MEM); |
| 110 | else |
| 111 | CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_MEM); |
| 112 | |
| 113 | *rdata = 0; |
| 114 | |
| 115 | if (membus_wait_for_req(plat, pll, timeout)) |
| 116 | return -ETIMEDOUT; |
| 117 | |
| 118 | if (pll == MEMBUS_MAINPLL) |
| 119 | *rdata = CM_REG_READL(plat, CLKMGR_MAINPLL_MEMSTAT); |
| 120 | else |
| 121 | *rdata = CM_REG_READL(plat, CLKMGR_PERPLL_MEMSTAT); |
| 122 | |
| 123 | debug("MEMBUS: Read 0x%08x from addr = 0x%08x\n", *rdata, addr); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static u32 calc_vocalib_pll(u32 pllm, u32 pllglob) |
| 129 | { |
| 130 | u32 mdiv, refclkdiv, arefclkdiv, drefclkdiv, mscnt, hscnt, vcocalib; |
| 131 | |
| 132 | mdiv = pllm & CLKMGR_PLLM_MDIV_MASK; |
| 133 | arefclkdiv = (pllglob & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >> |
| 134 | CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET; |
| 135 | drefclkdiv = (pllglob & CLKMGR_PLLGLOB_DREFCLKDIV_MASK) >> |
| 136 | CLKMGR_PLLGLOB_DREFCLKDIV_OFFSET; |
| 137 | refclkdiv = (pllglob & CLKMGR_PLLGLOB_REFCLKDIV_MASK) >> |
| 138 | CLKMGR_PLLGLOB_REFCLKDIV_OFFSET; |
| 139 | mscnt = CLKMGR_VCOCALIB_MSCNT_CONST / (mdiv * BIT(drefclkdiv)); |
| 140 | if (!mscnt) |
| 141 | mscnt = 1; |
| 142 | hscnt = (mdiv * mscnt * BIT(drefclkdiv) / refclkdiv) - |
| 143 | CLKMGR_VCOCALIB_HSCNT_CONST; |
| 144 | vcocalib = (hscnt & CLKMGR_VCOCALIB_HSCNT_MASK) | |
| 145 | ((mscnt << CLKMGR_VCOCALIB_MSCNT_OFFSET) & |
| 146 | CLKMGR_VCOCALIB_MSCNT_MASK); |
| 147 | |
| 148 | /* Dump all the pll calibration settings for debug purposes */ |
| 149 | debug("mdiv : %d\n", mdiv); |
| 150 | debug("arefclkdiv : %d\n", arefclkdiv); |
| 151 | debug("drefclkdiv : %d\n", drefclkdiv); |
| 152 | debug("refclkdiv : %d\n", refclkdiv); |
| 153 | debug("mscnt : %d\n", mscnt); |
| 154 | debug("hscnt : %d\n", hscnt); |
| 155 | debug("vcocalib : 0x%08x\n", vcocalib); |
| 156 | |
| 157 | return vcocalib; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Setup clocks while making no assumptions about previous state of the clocks. |
| 162 | */ |
| 163 | static void clk_basic_init(struct udevice *dev, |
| 164 | const struct cm_config * const cfg) |
| 165 | { |
| 166 | struct socfpga_clk_platdata *plat = dev_get_platdata(dev); |
| 167 | u32 vcocalib; |
| 168 | u32 rdata; |
| 169 | |
| 170 | if (!cfg) |
| 171 | return; |
| 172 | |
| 173 | /* Put both PLLs in bypass */ |
| 174 | clk_write_bypass_mainpll(plat, CLKMGR_BYPASS_MAINPLL_ALL); |
| 175 | clk_write_bypass_perpll(plat, CLKMGR_BYPASS_PERPLL_ALL); |
| 176 | |
| 177 | /* Put both PLLs in Reset and Power Down */ |
| 178 | CM_REG_CLRBITS(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 179 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 180 | CM_REG_CLRBITS(plat, CLKMGR_PERPLL_PLLGLOB, |
| 181 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 182 | |
| 183 | /* setup main PLL dividers where calculate the vcocalib value */ |
| 184 | vcocalib = calc_vocalib_pll(cfg->main_pll_pllm, cfg->main_pll_pllglob); |
| 185 | CM_REG_WRITEL(plat, cfg->main_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK, |
| 186 | CLKMGR_MAINPLL_PLLGLOB); |
| 187 | CM_REG_WRITEL(plat, cfg->main_pll_fdbck, CLKMGR_MAINPLL_FDBCK); |
| 188 | CM_REG_WRITEL(plat, vcocalib, CLKMGR_MAINPLL_VCOCALIB); |
| 189 | CM_REG_WRITEL(plat, cfg->main_pll_pllc0, CLKMGR_MAINPLL_PLLC0); |
| 190 | CM_REG_WRITEL(plat, cfg->main_pll_pllc1, CLKMGR_MAINPLL_PLLC1); |
| 191 | CM_REG_WRITEL(plat, cfg->main_pll_pllc2, CLKMGR_MAINPLL_PLLC2); |
| 192 | CM_REG_WRITEL(plat, cfg->main_pll_pllc3, CLKMGR_MAINPLL_PLLC3); |
| 193 | CM_REG_WRITEL(plat, cfg->main_pll_pllm, CLKMGR_MAINPLL_PLLM); |
| 194 | CM_REG_WRITEL(plat, cfg->main_pll_mpuclk, CLKMGR_MAINPLL_MPUCLK); |
| 195 | CM_REG_WRITEL(plat, cfg->main_pll_nocclk, CLKMGR_MAINPLL_NOCCLK); |
| 196 | CM_REG_WRITEL(plat, cfg->main_pll_nocdiv, CLKMGR_MAINPLL_NOCDIV); |
| 197 | |
| 198 | /* setup peripheral PLL dividers where calculate the vcocalib value */ |
| 199 | vcocalib = calc_vocalib_pll(cfg->per_pll_pllm, cfg->per_pll_pllglob); |
| 200 | CM_REG_WRITEL(plat, cfg->per_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK, |
| 201 | CLKMGR_PERPLL_PLLGLOB); |
| 202 | CM_REG_WRITEL(plat, cfg->per_pll_fdbck, CLKMGR_PERPLL_FDBCK); |
| 203 | CM_REG_WRITEL(plat, vcocalib, CLKMGR_PERPLL_VCOCALIB); |
| 204 | CM_REG_WRITEL(plat, cfg->per_pll_pllc0, CLKMGR_PERPLL_PLLC0); |
| 205 | CM_REG_WRITEL(plat, cfg->per_pll_pllc1, CLKMGR_PERPLL_PLLC1); |
| 206 | CM_REG_WRITEL(plat, cfg->per_pll_pllc2, CLKMGR_PERPLL_PLLC2); |
| 207 | CM_REG_WRITEL(plat, cfg->per_pll_pllc3, CLKMGR_PERPLL_PLLC3); |
| 208 | CM_REG_WRITEL(plat, cfg->per_pll_pllm, CLKMGR_PERPLL_PLLM); |
| 209 | CM_REG_WRITEL(plat, cfg->per_pll_emacctl, CLKMGR_PERPLL_EMACCTL); |
| 210 | CM_REG_WRITEL(plat, cfg->per_pll_gpiodiv, CLKMGR_PERPLL_GPIODIV); |
| 211 | |
| 212 | /* Take both PLL out of reset and power up */ |
| 213 | CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 214 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 215 | CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLGLOB, |
| 216 | CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK); |
| 217 | |
| 218 | /* Membus programming to set mainpll and perripll to |
| 219 | * source synchronous mode |
| 220 | */ |
| 221 | membus_read_pll(plat, MEMBUS_MAINPLL, MEMBUS_ADDR_CLKSLICE, &rdata, |
| 222 | MEMBUS_TIMEOUT); |
| 223 | membus_write_pll(plat, MEMBUS_MAINPLL, MEMBUS_ADDR_CLKSLICE, |
| 224 | (rdata | MEMBUS_CLKSLICE_SYNC_MODE_EN), |
| 225 | MEMBUS_TIMEOUT); |
| 226 | membus_read_pll(plat, MEMBUS_PERPLL, MEMBUS_ADDR_CLKSLICE, &rdata, |
| 227 | MEMBUS_TIMEOUT); |
| 228 | membus_write_pll(plat, MEMBUS_PERPLL, MEMBUS_ADDR_CLKSLICE, |
| 229 | (rdata | MEMBUS_CLKSLICE_SYNC_MODE_EN), |
| 230 | MEMBUS_TIMEOUT); |
| 231 | |
| 232 | cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK); |
| 233 | |
| 234 | /* Configure ping pong counters in altera group */ |
| 235 | CM_REG_WRITEL(plat, cfg->alt_emacactr, CLKMGR_ALTR_EMACACTR); |
| 236 | CM_REG_WRITEL(plat, cfg->alt_emacbctr, CLKMGR_ALTR_EMACBCTR); |
| 237 | CM_REG_WRITEL(plat, cfg->alt_emacptpctr, CLKMGR_ALTR_EMACPTPCTR); |
| 238 | CM_REG_WRITEL(plat, cfg->alt_gpiodbctr, CLKMGR_ALTR_GPIODBCTR); |
| 239 | CM_REG_WRITEL(plat, cfg->alt_sdmmcctr, CLKMGR_ALTR_SDMMCCTR); |
| 240 | CM_REG_WRITEL(plat, cfg->alt_s2fuser0ctr, CLKMGR_ALTR_S2FUSER0CTR); |
| 241 | CM_REG_WRITEL(plat, cfg->alt_s2fuser1ctr, CLKMGR_ALTR_S2FUSER1CTR); |
| 242 | CM_REG_WRITEL(plat, cfg->alt_psirefctr, CLKMGR_ALTR_PSIREFCTR); |
| 243 | |
| 244 | CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_MAINPLL_LOSTLOCK); |
| 245 | CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_PERPLL_LOSTLOCK); |
| 246 | |
| 247 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLGLOB) | |
| 248 | CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK, |
| 249 | CLKMGR_MAINPLL_PLLGLOB); |
| 250 | CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLGLOB) | |
| 251 | CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK, |
| 252 | CLKMGR_PERPLL_PLLGLOB); |
| 253 | |
| 254 | /* Take all PLLs out of bypass */ |
| 255 | clk_write_bypass_mainpll(plat, 0); |
| 256 | clk_write_bypass_perpll(plat, 0); |
| 257 | |
| 258 | /* Clear the loss of lock bits (write 1 to clear) */ |
| 259 | CM_REG_CLRBITS(plat, CLKMGR_INTRCLR, |
| 260 | CLKMGR_INTER_PERPLLLOST_MASK | |
| 261 | CLKMGR_INTER_MAINPLLLOST_MASK); |
| 262 | |
| 263 | /* Take all ping pong counters out of reset */ |
| 264 | CM_REG_CLRBITS(plat, CLKMGR_ALTR_EXTCNTRST, |
| 265 | CLKMGR_ALT_EXTCNTRST_ALLCNTRST); |
| 266 | |
| 267 | /* Out of boot mode */ |
| 268 | clk_write_ctrl(plat, |
| 269 | CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE); |
| 270 | } |
| 271 | |
| 272 | static u64 clk_get_vco_clk_hz(struct socfpga_clk_platdata *plat, |
| 273 | u32 pllglob_reg, u32 pllm_reg) |
| 274 | { |
| 275 | u64 fref, arefdiv, mdiv, reg, vco; |
| 276 | |
| 277 | reg = CM_REG_READL(plat, pllglob_reg); |
| 278 | |
| 279 | fref = (reg & CLKMGR_PLLGLOB_VCO_PSRC_MASK) >> |
| 280 | CLKMGR_PLLGLOB_VCO_PSRC_OFFSET; |
| 281 | |
| 282 | switch (fref) { |
| 283 | case CLKMGR_VCO_PSRC_EOSC1: |
| 284 | fref = cm_get_osc_clk_hz(); |
| 285 | break; |
| 286 | case CLKMGR_VCO_PSRC_INTOSC: |
| 287 | fref = cm_get_intosc_clk_hz(); |
| 288 | break; |
| 289 | case CLKMGR_VCO_PSRC_F2S: |
| 290 | fref = cm_get_fpga_clk_hz(); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | arefdiv = (reg & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >> |
| 295 | CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET; |
| 296 | |
| 297 | mdiv = CM_REG_READL(plat, pllm_reg) & CLKMGR_PLLM_MDIV_MASK; |
| 298 | |
| 299 | vco = fref / arefdiv; |
| 300 | vco = vco * mdiv; |
| 301 | |
| 302 | return vco; |
| 303 | } |
| 304 | |
| 305 | static u64 clk_get_main_vco_clk_hz(struct socfpga_clk_platdata *plat) |
| 306 | { |
| 307 | return clk_get_vco_clk_hz(plat, CLKMGR_MAINPLL_PLLGLOB, |
| 308 | CLKMGR_MAINPLL_PLLM); |
| 309 | } |
| 310 | |
| 311 | static u64 clk_get_per_vco_clk_hz(struct socfpga_clk_platdata *plat) |
| 312 | { |
| 313 | return clk_get_vco_clk_hz(plat, CLKMGR_PERPLL_PLLGLOB, |
| 314 | CLKMGR_PERPLL_PLLM); |
| 315 | } |
| 316 | |
| 317 | static u32 clk_get_5_1_clk_src(struct socfpga_clk_platdata *plat, u64 reg) |
| 318 | { |
| 319 | u32 clksrc = CM_REG_READL(plat, reg); |
| 320 | |
| 321 | return (clksrc & CLKMGR_CLKSRC_MASK) >> CLKMGR_CLKSRC_OFFSET; |
| 322 | } |
| 323 | |
| 324 | static u64 clk_get_clksrc_hz(struct socfpga_clk_platdata *plat, u32 clksrc_reg, |
| 325 | u32 main_reg, u32 per_reg) |
| 326 | { |
| 327 | u64 clock; |
| 328 | u32 clklsrc = clk_get_5_1_clk_src(plat, clksrc_reg); |
| 329 | |
| 330 | switch (clklsrc) { |
| 331 | case CLKMGR_CLKSRC_MAIN: |
| 332 | clock = clk_get_main_vco_clk_hz(plat); |
| 333 | clock /= (CM_REG_READL(plat, main_reg) & |
| 334 | CLKMGR_CLKCNT_MSK); |
| 335 | break; |
| 336 | |
| 337 | case CLKMGR_CLKSRC_PER: |
| 338 | clock = clk_get_per_vco_clk_hz(plat); |
| 339 | clock /= (CM_REG_READL(plat, per_reg) & |
| 340 | CLKMGR_CLKCNT_MSK); |
| 341 | break; |
| 342 | |
| 343 | case CLKMGR_CLKSRC_OSC1: |
| 344 | clock = cm_get_osc_clk_hz(); |
| 345 | break; |
| 346 | |
| 347 | case CLKMGR_CLKSRC_INTOSC: |
| 348 | clock = cm_get_intosc_clk_hz(); |
| 349 | break; |
| 350 | |
| 351 | case CLKMGR_CLKSRC_FPGA: |
| 352 | clock = cm_get_fpga_clk_hz(); |
| 353 | break; |
| 354 | default: |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | return clock; |
| 359 | } |
| 360 | |
| 361 | static u64 clk_get_mpu_clk_hz(struct socfpga_clk_platdata *plat) |
| 362 | { |
| 363 | u64 clock = clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_MPUCLK, |
| 364 | CLKMGR_MAINPLL_PLLC0, |
| 365 | CLKMGR_PERPLL_PLLC0); |
| 366 | |
| 367 | clock /= 1 + (CM_REG_READL(plat, CLKMGR_MAINPLL_MPUCLK) & |
| 368 | CLKMGR_CLKCNT_MSK); |
| 369 | |
| 370 | return clock; |
| 371 | } |
| 372 | |
| 373 | static u32 clk_get_l3_main_clk_hz(struct socfpga_clk_platdata *plat) |
| 374 | { |
| 375 | return clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_NOCCLK, |
| 376 | CLKMGR_MAINPLL_PLLC1, |
| 377 | CLKMGR_PERPLL_PLLC1); |
| 378 | } |
| 379 | |
| 380 | static u32 clk_get_l4_main_clk_hz(struct socfpga_clk_platdata *plat) |
| 381 | { |
| 382 | u64 clock = clk_get_l3_main_clk_hz(plat); |
| 383 | |
| 384 | clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >> |
| 385 | CLKMGR_NOCDIV_L4MAIN_OFFSET) & |
| 386 | CLKMGR_NOCDIV_DIVIDER_MASK); |
| 387 | |
| 388 | return clock; |
| 389 | } |
| 390 | |
| 391 | static u32 clk_get_sdmmc_clk_hz(struct socfpga_clk_platdata *plat) |
| 392 | { |
| 393 | u64 clock = clk_get_clksrc_hz(plat, CLKMGR_ALTR_SDMMCCTR, |
| 394 | CLKMGR_MAINPLL_PLLC3, |
| 395 | CLKMGR_PERPLL_PLLC3); |
| 396 | |
| 397 | clock /= 1 + (CM_REG_READL(plat, CLKMGR_ALTR_SDMMCCTR) & |
| 398 | CLKMGR_CLKCNT_MSK); |
| 399 | |
| 400 | return clock / 4; |
| 401 | } |
| 402 | |
| 403 | static u32 clk_get_l4_sp_clk_hz(struct socfpga_clk_platdata *plat) |
| 404 | { |
| 405 | u64 clock = clk_get_l3_main_clk_hz(plat); |
| 406 | |
| 407 | clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >> |
| 408 | CLKMGR_NOCDIV_L4SPCLK_OFFSET) & |
| 409 | CLKMGR_NOCDIV_DIVIDER_MASK); |
| 410 | |
| 411 | return clock; |
| 412 | } |
| 413 | |
| 414 | static u32 clk_get_l4_mp_clk_hz(struct socfpga_clk_platdata *plat) |
| 415 | { |
| 416 | u64 clock = clk_get_l3_main_clk_hz(plat); |
| 417 | |
| 418 | clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >> |
| 419 | CLKMGR_NOCDIV_L4MPCLK_OFFSET) & |
| 420 | CLKMGR_NOCDIV_DIVIDER_MASK); |
| 421 | |
| 422 | return clock; |
| 423 | } |
| 424 | |
| 425 | static u32 clk_get_l4_sys_free_clk_hz(struct socfpga_clk_platdata *plat) |
| 426 | { |
| 427 | if (CM_REG_READL(plat, CLKMGR_STAT) & CLKMGR_STAT_BOOTMODE) |
| 428 | return clk_get_l3_main_clk_hz(plat) / 2; |
| 429 | |
| 430 | return clk_get_l3_main_clk_hz(plat) / 4; |
| 431 | } |
| 432 | |
| 433 | static u32 clk_get_emac_clk_hz(struct socfpga_clk_platdata *plat, u32 emac_id) |
| 434 | { |
| 435 | bool emacsel_a; |
| 436 | u32 ctl; |
| 437 | u32 ctr_reg; |
| 438 | u32 clock; |
| 439 | u32 div; |
| 440 | u32 reg; |
| 441 | |
| 442 | /* Get EMAC clock source */ |
| 443 | ctl = CM_REG_READL(plat, CLKMGR_PERPLL_EMACCTL); |
| 444 | if (emac_id == AGILEX_EMAC0_CLK) |
| 445 | ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_OFFSET) & |
| 446 | CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_MASK; |
| 447 | else if (emac_id == AGILEX_EMAC1_CLK) |
| 448 | ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_OFFSET) & |
| 449 | CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_MASK; |
| 450 | else if (emac_id == AGILEX_EMAC2_CLK) |
| 451 | ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_OFFSET) & |
| 452 | CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_MASK; |
| 453 | else |
| 454 | return 0; |
| 455 | |
| 456 | if (ctl) { |
| 457 | /* EMAC B source */ |
| 458 | emacsel_a = false; |
| 459 | ctr_reg = CLKMGR_ALTR_EMACBCTR; |
| 460 | } else { |
| 461 | /* EMAC A source */ |
| 462 | emacsel_a = true; |
| 463 | ctr_reg = CLKMGR_ALTR_EMACACTR; |
| 464 | } |
| 465 | |
| 466 | reg = CM_REG_READL(plat, ctr_reg); |
| 467 | clock = (reg & CLKMGR_ALT_EMACCTR_SRC_MASK) |
| 468 | >> CLKMGR_ALT_EMACCTR_SRC_OFFSET; |
| 469 | div = (reg & CLKMGR_ALT_EMACCTR_CNT_MASK) |
| 470 | >> CLKMGR_ALT_EMACCTR_CNT_OFFSET; |
| 471 | |
| 472 | switch (clock) { |
| 473 | case CLKMGR_CLKSRC_MAIN: |
| 474 | clock = clk_get_main_vco_clk_hz(plat); |
| 475 | if (emacsel_a) { |
| 476 | clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC2) & |
| 477 | CLKMGR_CLKCNT_MSK); |
| 478 | } else { |
| 479 | clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC3) & |
| 480 | CLKMGR_CLKCNT_MSK); |
| 481 | } |
| 482 | break; |
| 483 | |
| 484 | case CLKMGR_CLKSRC_PER: |
| 485 | clock = clk_get_per_vco_clk_hz(plat); |
| 486 | if (emacsel_a) { |
| 487 | clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC2) & |
| 488 | CLKMGR_CLKCNT_MSK); |
| 489 | } else { |
| 490 | clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC3) & |
| 491 | CLKMGR_CLKCNT_MSK); |
| 492 | } |
| 493 | break; |
| 494 | |
| 495 | case CLKMGR_CLKSRC_OSC1: |
| 496 | clock = cm_get_osc_clk_hz(); |
| 497 | break; |
| 498 | |
| 499 | case CLKMGR_CLKSRC_INTOSC: |
| 500 | clock = cm_get_intosc_clk_hz(); |
| 501 | break; |
| 502 | |
| 503 | case CLKMGR_CLKSRC_FPGA: |
| 504 | clock = cm_get_fpga_clk_hz(); |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | clock /= 1 + div; |
| 509 | |
| 510 | return clock; |
| 511 | } |
| 512 | |
| 513 | static ulong socfpga_clk_get_rate(struct clk *clk) |
| 514 | { |
| 515 | struct socfpga_clk_platdata *plat = dev_get_platdata(clk->dev); |
| 516 | |
| 517 | switch (clk->id) { |
| 518 | case AGILEX_MPU_CLK: |
| 519 | return clk_get_mpu_clk_hz(plat); |
| 520 | case AGILEX_L4_MAIN_CLK: |
| 521 | return clk_get_l4_main_clk_hz(plat); |
| 522 | case AGILEX_L4_SYS_FREE_CLK: |
| 523 | return clk_get_l4_sys_free_clk_hz(plat); |
| 524 | case AGILEX_L4_MP_CLK: |
| 525 | return clk_get_l4_mp_clk_hz(plat); |
| 526 | case AGILEX_L4_SP_CLK: |
| 527 | return clk_get_l4_sp_clk_hz(plat); |
| 528 | case AGILEX_SDMMC_CLK: |
| 529 | return clk_get_sdmmc_clk_hz(plat); |
| 530 | case AGILEX_EMAC0_CLK: |
| 531 | case AGILEX_EMAC1_CLK: |
| 532 | case AGILEX_EMAC2_CLK: |
| 533 | return clk_get_emac_clk_hz(plat, clk->id); |
| 534 | case AGILEX_USB_CLK: |
| 535 | return clk_get_l4_mp_clk_hz(plat); |
| 536 | default: |
| 537 | return -ENXIO; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static int socfpga_clk_probe(struct udevice *dev) |
| 542 | { |
| 543 | const struct cm_config *cm_default_cfg = cm_get_default_config(); |
| 544 | |
| 545 | clk_basic_init(dev, cm_default_cfg); |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int socfpga_clk_ofdata_to_platdata(struct udevice *dev) |
| 551 | { |
| 552 | struct socfpga_clk_platdata *plat = dev_get_platdata(dev); |
| 553 | fdt_addr_t addr; |
| 554 | |
| 555 | addr = devfdt_get_addr(dev); |
| 556 | if (addr == FDT_ADDR_T_NONE) |
| 557 | return -EINVAL; |
| 558 | plat->regs = (void __iomem *)addr; |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static struct clk_ops socfpga_clk_ops = { |
| 564 | .get_rate = socfpga_clk_get_rate, |
| 565 | }; |
| 566 | |
| 567 | static const struct udevice_id socfpga_clk_match[] = { |
| 568 | { .compatible = "intel,agilex-clkmgr" }, |
| 569 | {} |
| 570 | }; |
| 571 | |
| 572 | U_BOOT_DRIVER(socfpga_agilex_clk) = { |
| 573 | .name = "clk-agilex", |
| 574 | .id = UCLASS_CLK, |
| 575 | .of_match = socfpga_clk_match, |
| 576 | .ops = &socfpga_clk_ops, |
| 577 | .probe = socfpga_clk_probe, |
| 578 | .ofdata_to_platdata = socfpga_clk_ofdata_to_platdata, |
| 579 | .platdata_auto_alloc_size = sizeof(struct socfpga_clk_platdata), |
| 580 | }; |