blob: fc4d239551b7ea4e50d56fdf57a42b60acc97b9b [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>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Ley Foon Tanef5458f2019-11-27 15:55:22 +08008#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 Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Ley Foon Tanef5458f2019-11-27 15:55:22 +080015
16#include <asm/arch/clock_manager.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20struct 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 */
28static 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
34static 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 */
41static 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
53static 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
78static 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
99static 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
129static 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 */
164static 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
Chee Hong Ang2d94ee62020-07-10 20:55:22 +0800174#ifdef CONFIG_SPL_BUILD
175 /* Always force clock manager into boot mode before any configuration */
176 clk_write_ctrl(plat,
177 CM_REG_READL(plat, CLKMGR_CTRL) | CLKMGR_CTRL_BOOTMODE);
178#else
179 /* Skip clock configuration in SSBL if it's not in boot mode */
180 if (!(CM_REG_READL(plat, CLKMGR_CTRL) & CLKMGR_CTRL_BOOTMODE))
181 return;
182#endif
183
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800184 /* Put both PLLs in bypass */
185 clk_write_bypass_mainpll(plat, CLKMGR_BYPASS_MAINPLL_ALL);
186 clk_write_bypass_perpll(plat, CLKMGR_BYPASS_PERPLL_ALL);
187
188 /* Put both PLLs in Reset and Power Down */
189 CM_REG_CLRBITS(plat, CLKMGR_MAINPLL_PLLGLOB,
190 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
191 CM_REG_CLRBITS(plat, CLKMGR_PERPLL_PLLGLOB,
192 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
193
194 /* setup main PLL dividers where calculate the vcocalib value */
195 vcocalib = calc_vocalib_pll(cfg->main_pll_pllm, cfg->main_pll_pllglob);
196 CM_REG_WRITEL(plat, cfg->main_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK,
197 CLKMGR_MAINPLL_PLLGLOB);
198 CM_REG_WRITEL(plat, cfg->main_pll_fdbck, CLKMGR_MAINPLL_FDBCK);
199 CM_REG_WRITEL(plat, vcocalib, CLKMGR_MAINPLL_VCOCALIB);
200 CM_REG_WRITEL(plat, cfg->main_pll_pllc0, CLKMGR_MAINPLL_PLLC0);
201 CM_REG_WRITEL(plat, cfg->main_pll_pllc1, CLKMGR_MAINPLL_PLLC1);
202 CM_REG_WRITEL(plat, cfg->main_pll_pllc2, CLKMGR_MAINPLL_PLLC2);
203 CM_REG_WRITEL(plat, cfg->main_pll_pllc3, CLKMGR_MAINPLL_PLLC3);
204 CM_REG_WRITEL(plat, cfg->main_pll_pllm, CLKMGR_MAINPLL_PLLM);
205 CM_REG_WRITEL(plat, cfg->main_pll_mpuclk, CLKMGR_MAINPLL_MPUCLK);
206 CM_REG_WRITEL(plat, cfg->main_pll_nocclk, CLKMGR_MAINPLL_NOCCLK);
207 CM_REG_WRITEL(plat, cfg->main_pll_nocdiv, CLKMGR_MAINPLL_NOCDIV);
208
209 /* setup peripheral PLL dividers where calculate the vcocalib value */
210 vcocalib = calc_vocalib_pll(cfg->per_pll_pllm, cfg->per_pll_pllglob);
211 CM_REG_WRITEL(plat, cfg->per_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK,
212 CLKMGR_PERPLL_PLLGLOB);
213 CM_REG_WRITEL(plat, cfg->per_pll_fdbck, CLKMGR_PERPLL_FDBCK);
214 CM_REG_WRITEL(plat, vcocalib, CLKMGR_PERPLL_VCOCALIB);
215 CM_REG_WRITEL(plat, cfg->per_pll_pllc0, CLKMGR_PERPLL_PLLC0);
216 CM_REG_WRITEL(plat, cfg->per_pll_pllc1, CLKMGR_PERPLL_PLLC1);
217 CM_REG_WRITEL(plat, cfg->per_pll_pllc2, CLKMGR_PERPLL_PLLC2);
218 CM_REG_WRITEL(plat, cfg->per_pll_pllc3, CLKMGR_PERPLL_PLLC3);
219 CM_REG_WRITEL(plat, cfg->per_pll_pllm, CLKMGR_PERPLL_PLLM);
220 CM_REG_WRITEL(plat, cfg->per_pll_emacctl, CLKMGR_PERPLL_EMACCTL);
221 CM_REG_WRITEL(plat, cfg->per_pll_gpiodiv, CLKMGR_PERPLL_GPIODIV);
222
223 /* Take both PLL out of reset and power up */
224 CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLGLOB,
225 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
226 CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLGLOB,
227 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
228
229 /* Membus programming to set mainpll and perripll to
230 * source synchronous mode
231 */
232 membus_read_pll(plat, MEMBUS_MAINPLL, MEMBUS_ADDR_CLKSLICE, &rdata,
233 MEMBUS_TIMEOUT);
234 membus_write_pll(plat, MEMBUS_MAINPLL, MEMBUS_ADDR_CLKSLICE,
235 (rdata | MEMBUS_CLKSLICE_SYNC_MODE_EN),
236 MEMBUS_TIMEOUT);
237 membus_read_pll(plat, MEMBUS_PERPLL, MEMBUS_ADDR_CLKSLICE, &rdata,
238 MEMBUS_TIMEOUT);
239 membus_write_pll(plat, MEMBUS_PERPLL, MEMBUS_ADDR_CLKSLICE,
240 (rdata | MEMBUS_CLKSLICE_SYNC_MODE_EN),
241 MEMBUS_TIMEOUT);
242
243 cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK);
244
245 /* Configure ping pong counters in altera group */
246 CM_REG_WRITEL(plat, cfg->alt_emacactr, CLKMGR_ALTR_EMACACTR);
247 CM_REG_WRITEL(plat, cfg->alt_emacbctr, CLKMGR_ALTR_EMACBCTR);
248 CM_REG_WRITEL(plat, cfg->alt_emacptpctr, CLKMGR_ALTR_EMACPTPCTR);
249 CM_REG_WRITEL(plat, cfg->alt_gpiodbctr, CLKMGR_ALTR_GPIODBCTR);
250 CM_REG_WRITEL(plat, cfg->alt_sdmmcctr, CLKMGR_ALTR_SDMMCCTR);
251 CM_REG_WRITEL(plat, cfg->alt_s2fuser0ctr, CLKMGR_ALTR_S2FUSER0CTR);
252 CM_REG_WRITEL(plat, cfg->alt_s2fuser1ctr, CLKMGR_ALTR_S2FUSER1CTR);
253 CM_REG_WRITEL(plat, cfg->alt_psirefctr, CLKMGR_ALTR_PSIREFCTR);
254
255 CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_MAINPLL_LOSTLOCK);
256 CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_PERPLL_LOSTLOCK);
257
258 CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLGLOB) |
259 CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
260 CLKMGR_MAINPLL_PLLGLOB);
261 CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLGLOB) |
262 CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
263 CLKMGR_PERPLL_PLLGLOB);
264
265 /* Take all PLLs out of bypass */
266 clk_write_bypass_mainpll(plat, 0);
267 clk_write_bypass_perpll(plat, 0);
268
269 /* Clear the loss of lock bits (write 1 to clear) */
270 CM_REG_CLRBITS(plat, CLKMGR_INTRCLR,
271 CLKMGR_INTER_PERPLLLOST_MASK |
272 CLKMGR_INTER_MAINPLLLOST_MASK);
273
274 /* Take all ping pong counters out of reset */
275 CM_REG_CLRBITS(plat, CLKMGR_ALTR_EXTCNTRST,
276 CLKMGR_ALT_EXTCNTRST_ALLCNTRST);
277
278 /* Out of boot mode */
279 clk_write_ctrl(plat,
280 CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE);
281}
282
283static u64 clk_get_vco_clk_hz(struct socfpga_clk_platdata *plat,
284 u32 pllglob_reg, u32 pllm_reg)
285{
286 u64 fref, arefdiv, mdiv, reg, vco;
287
288 reg = CM_REG_READL(plat, pllglob_reg);
289
290 fref = (reg & CLKMGR_PLLGLOB_VCO_PSRC_MASK) >>
291 CLKMGR_PLLGLOB_VCO_PSRC_OFFSET;
292
293 switch (fref) {
294 case CLKMGR_VCO_PSRC_EOSC1:
295 fref = cm_get_osc_clk_hz();
296 break;
297 case CLKMGR_VCO_PSRC_INTOSC:
298 fref = cm_get_intosc_clk_hz();
299 break;
300 case CLKMGR_VCO_PSRC_F2S:
301 fref = cm_get_fpga_clk_hz();
302 break;
303 }
304
305 arefdiv = (reg & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >>
306 CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET;
307
308 mdiv = CM_REG_READL(plat, pllm_reg) & CLKMGR_PLLM_MDIV_MASK;
309
310 vco = fref / arefdiv;
311 vco = vco * mdiv;
312
313 return vco;
314}
315
316static u64 clk_get_main_vco_clk_hz(struct socfpga_clk_platdata *plat)
317{
318 return clk_get_vco_clk_hz(plat, CLKMGR_MAINPLL_PLLGLOB,
319 CLKMGR_MAINPLL_PLLM);
320}
321
322static u64 clk_get_per_vco_clk_hz(struct socfpga_clk_platdata *plat)
323{
324 return clk_get_vco_clk_hz(plat, CLKMGR_PERPLL_PLLGLOB,
325 CLKMGR_PERPLL_PLLM);
326}
327
328static u32 clk_get_5_1_clk_src(struct socfpga_clk_platdata *plat, u64 reg)
329{
330 u32 clksrc = CM_REG_READL(plat, reg);
331
332 return (clksrc & CLKMGR_CLKSRC_MASK) >> CLKMGR_CLKSRC_OFFSET;
333}
334
335static u64 clk_get_clksrc_hz(struct socfpga_clk_platdata *plat, u32 clksrc_reg,
336 u32 main_reg, u32 per_reg)
337{
338 u64 clock;
339 u32 clklsrc = clk_get_5_1_clk_src(plat, clksrc_reg);
340
341 switch (clklsrc) {
342 case CLKMGR_CLKSRC_MAIN:
343 clock = clk_get_main_vco_clk_hz(plat);
344 clock /= (CM_REG_READL(plat, main_reg) &
345 CLKMGR_CLKCNT_MSK);
346 break;
347
348 case CLKMGR_CLKSRC_PER:
349 clock = clk_get_per_vco_clk_hz(plat);
350 clock /= (CM_REG_READL(plat, per_reg) &
351 CLKMGR_CLKCNT_MSK);
352 break;
353
354 case CLKMGR_CLKSRC_OSC1:
355 clock = cm_get_osc_clk_hz();
356 break;
357
358 case CLKMGR_CLKSRC_INTOSC:
359 clock = cm_get_intosc_clk_hz();
360 break;
361
362 case CLKMGR_CLKSRC_FPGA:
363 clock = cm_get_fpga_clk_hz();
364 break;
365 default:
366 return 0;
367 }
368
369 return clock;
370}
371
372static u64 clk_get_mpu_clk_hz(struct socfpga_clk_platdata *plat)
373{
374 u64 clock = clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_MPUCLK,
375 CLKMGR_MAINPLL_PLLC0,
376 CLKMGR_PERPLL_PLLC0);
377
378 clock /= 1 + (CM_REG_READL(plat, CLKMGR_MAINPLL_MPUCLK) &
379 CLKMGR_CLKCNT_MSK);
380
381 return clock;
382}
383
384static u32 clk_get_l3_main_clk_hz(struct socfpga_clk_platdata *plat)
385{
386 return clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_NOCCLK,
387 CLKMGR_MAINPLL_PLLC1,
388 CLKMGR_PERPLL_PLLC1);
389}
390
391static u32 clk_get_l4_main_clk_hz(struct socfpga_clk_platdata *plat)
392{
393 u64 clock = clk_get_l3_main_clk_hz(plat);
394
395 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
396 CLKMGR_NOCDIV_L4MAIN_OFFSET) &
397 CLKMGR_NOCDIV_DIVIDER_MASK);
398
399 return clock;
400}
401
402static u32 clk_get_sdmmc_clk_hz(struct socfpga_clk_platdata *plat)
403{
404 u64 clock = clk_get_clksrc_hz(plat, CLKMGR_ALTR_SDMMCCTR,
405 CLKMGR_MAINPLL_PLLC3,
406 CLKMGR_PERPLL_PLLC3);
407
408 clock /= 1 + (CM_REG_READL(plat, CLKMGR_ALTR_SDMMCCTR) &
409 CLKMGR_CLKCNT_MSK);
410
411 return clock / 4;
412}
413
414static u32 clk_get_l4_sp_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_L4SPCLK_OFFSET) &
420 CLKMGR_NOCDIV_DIVIDER_MASK);
421
422 return clock;
423}
424
425static u32 clk_get_l4_mp_clk_hz(struct socfpga_clk_platdata *plat)
426{
427 u64 clock = clk_get_l3_main_clk_hz(plat);
428
429 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
430 CLKMGR_NOCDIV_L4MPCLK_OFFSET) &
431 CLKMGR_NOCDIV_DIVIDER_MASK);
432
433 return clock;
434}
435
436static u32 clk_get_l4_sys_free_clk_hz(struct socfpga_clk_platdata *plat)
437{
438 if (CM_REG_READL(plat, CLKMGR_STAT) & CLKMGR_STAT_BOOTMODE)
439 return clk_get_l3_main_clk_hz(plat) / 2;
440
441 return clk_get_l3_main_clk_hz(plat) / 4;
442}
443
444static u32 clk_get_emac_clk_hz(struct socfpga_clk_platdata *plat, u32 emac_id)
445{
446 bool emacsel_a;
447 u32 ctl;
448 u32 ctr_reg;
449 u32 clock;
450 u32 div;
451 u32 reg;
452
453 /* Get EMAC clock source */
454 ctl = CM_REG_READL(plat, CLKMGR_PERPLL_EMACCTL);
455 if (emac_id == AGILEX_EMAC0_CLK)
456 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_OFFSET) &
457 CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_MASK;
458 else if (emac_id == AGILEX_EMAC1_CLK)
459 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_OFFSET) &
460 CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_MASK;
461 else if (emac_id == AGILEX_EMAC2_CLK)
462 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_OFFSET) &
463 CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_MASK;
464 else
465 return 0;
466
467 if (ctl) {
468 /* EMAC B source */
469 emacsel_a = false;
470 ctr_reg = CLKMGR_ALTR_EMACBCTR;
471 } else {
472 /* EMAC A source */
473 emacsel_a = true;
474 ctr_reg = CLKMGR_ALTR_EMACACTR;
475 }
476
477 reg = CM_REG_READL(plat, ctr_reg);
478 clock = (reg & CLKMGR_ALT_EMACCTR_SRC_MASK)
479 >> CLKMGR_ALT_EMACCTR_SRC_OFFSET;
480 div = (reg & CLKMGR_ALT_EMACCTR_CNT_MASK)
481 >> CLKMGR_ALT_EMACCTR_CNT_OFFSET;
482
483 switch (clock) {
484 case CLKMGR_CLKSRC_MAIN:
485 clock = clk_get_main_vco_clk_hz(plat);
486 if (emacsel_a) {
487 clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC2) &
488 CLKMGR_CLKCNT_MSK);
489 } else {
490 clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC3) &
491 CLKMGR_CLKCNT_MSK);
492 }
493 break;
494
495 case CLKMGR_CLKSRC_PER:
496 clock = clk_get_per_vco_clk_hz(plat);
497 if (emacsel_a) {
498 clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC2) &
499 CLKMGR_CLKCNT_MSK);
500 } else {
501 clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC3) &
502 CLKMGR_CLKCNT_MSK);
503 }
504 break;
505
506 case CLKMGR_CLKSRC_OSC1:
507 clock = cm_get_osc_clk_hz();
508 break;
509
510 case CLKMGR_CLKSRC_INTOSC:
511 clock = cm_get_intosc_clk_hz();
512 break;
513
514 case CLKMGR_CLKSRC_FPGA:
515 clock = cm_get_fpga_clk_hz();
516 break;
517 }
518
519 clock /= 1 + div;
520
521 return clock;
522}
523
524static ulong socfpga_clk_get_rate(struct clk *clk)
525{
526 struct socfpga_clk_platdata *plat = dev_get_platdata(clk->dev);
527
528 switch (clk->id) {
529 case AGILEX_MPU_CLK:
530 return clk_get_mpu_clk_hz(plat);
531 case AGILEX_L4_MAIN_CLK:
532 return clk_get_l4_main_clk_hz(plat);
533 case AGILEX_L4_SYS_FREE_CLK:
534 return clk_get_l4_sys_free_clk_hz(plat);
535 case AGILEX_L4_MP_CLK:
536 return clk_get_l4_mp_clk_hz(plat);
537 case AGILEX_L4_SP_CLK:
538 return clk_get_l4_sp_clk_hz(plat);
539 case AGILEX_SDMMC_CLK:
540 return clk_get_sdmmc_clk_hz(plat);
541 case AGILEX_EMAC0_CLK:
542 case AGILEX_EMAC1_CLK:
543 case AGILEX_EMAC2_CLK:
544 return clk_get_emac_clk_hz(plat, clk->id);
545 case AGILEX_USB_CLK:
Ley Foon Tanf94b0fd2020-07-10 20:55:20 +0800546 case AGILEX_NAND_X_CLK:
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800547 return clk_get_l4_mp_clk_hz(plat);
Ley Foon Tanf94b0fd2020-07-10 20:55:20 +0800548 case AGILEX_NAND_CLK:
549 return clk_get_l4_mp_clk_hz(plat) / 4;
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800550 default:
551 return -ENXIO;
552 }
553}
554
Ley Foon Tan76092202020-07-10 20:55:21 +0800555static int socfpga_clk_enable(struct clk *clk)
556{
557 return 0;
558}
559
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800560static int socfpga_clk_probe(struct udevice *dev)
561{
562 const struct cm_config *cm_default_cfg = cm_get_default_config();
563
564 clk_basic_init(dev, cm_default_cfg);
565
566 return 0;
567}
568
569static int socfpga_clk_ofdata_to_platdata(struct udevice *dev)
570{
571 struct socfpga_clk_platdata *plat = dev_get_platdata(dev);
572 fdt_addr_t addr;
573
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900574 addr = dev_read_addr(dev);
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800575 if (addr == FDT_ADDR_T_NONE)
576 return -EINVAL;
577 plat->regs = (void __iomem *)addr;
578
579 return 0;
580}
581
582static struct clk_ops socfpga_clk_ops = {
Ley Foon Tan76092202020-07-10 20:55:21 +0800583 .enable = socfpga_clk_enable,
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800584 .get_rate = socfpga_clk_get_rate,
585};
586
587static const struct udevice_id socfpga_clk_match[] = {
588 { .compatible = "intel,agilex-clkmgr" },
589 {}
590};
591
592U_BOOT_DRIVER(socfpga_agilex_clk) = {
593 .name = "clk-agilex",
594 .id = UCLASS_CLK,
595 .of_match = socfpga_clk_match,
596 .ops = &socfpga_clk_ops,
597 .probe = socfpga_clk_probe,
598 .ofdata_to_platdata = socfpga_clk_ofdata_to_platdata,
599 .platdata_auto_alloc_size = sizeof(struct socfpga_clk_platdata),
600};