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