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