blob: a539889d5be4d8272f361b2f60c9439d3aa6ca6d [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
Chee Hong Ang95793012020-07-10 20:55:23 +080050
51#define MEMBUS_CLKSLICE_REG 0x27
52#define MEMBUS_SYNTHCALFOSC_INIT_CENTERFREQ_REG 0xb3
53#define MEMBUS_SYNTHPPM_WATCHDOGTMR_VF01_REG 0xe6
54#define MEMBUS_CALCLKSLICE0_DUTY_LOCOVR_REG 0x03
55#define MEMBUS_CALCLKSLICE1_DUTY_LOCOVR_REG 0x07
56
57static 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};
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800110
111static int membus_wait_for_req(struct socfpga_clk_platdata *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
136static int membus_write_pll(struct socfpga_clk_platdata *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
157static int membus_read_pll(struct socfpga_clk_platdata *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
Chee Hong Ang95793012020-07-10 20:55:23 +0800187static void membus_pll_configs(struct socfpga_clk_platdata *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) | membus_pll[i].val),
197 MEMBUS_TIMEOUT);
198 }
199}
200
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800201static u32 calc_vocalib_pll(u32 pllm, u32 pllglob)
202{
203 u32 mdiv, refclkdiv, arefclkdiv, drefclkdiv, mscnt, hscnt, vcocalib;
204
205 mdiv = pllm & CLKMGR_PLLM_MDIV_MASK;
206 arefclkdiv = (pllglob & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >>
207 CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET;
208 drefclkdiv = (pllglob & CLKMGR_PLLGLOB_DREFCLKDIV_MASK) >>
209 CLKMGR_PLLGLOB_DREFCLKDIV_OFFSET;
210 refclkdiv = (pllglob & CLKMGR_PLLGLOB_REFCLKDIV_MASK) >>
211 CLKMGR_PLLGLOB_REFCLKDIV_OFFSET;
212 mscnt = CLKMGR_VCOCALIB_MSCNT_CONST / (mdiv * BIT(drefclkdiv));
213 if (!mscnt)
214 mscnt = 1;
215 hscnt = (mdiv * mscnt * BIT(drefclkdiv) / refclkdiv) -
216 CLKMGR_VCOCALIB_HSCNT_CONST;
217 vcocalib = (hscnt & CLKMGR_VCOCALIB_HSCNT_MASK) |
218 ((mscnt << CLKMGR_VCOCALIB_MSCNT_OFFSET) &
219 CLKMGR_VCOCALIB_MSCNT_MASK);
220
221 /* Dump all the pll calibration settings for debug purposes */
222 debug("mdiv : %d\n", mdiv);
223 debug("arefclkdiv : %d\n", arefclkdiv);
224 debug("drefclkdiv : %d\n", drefclkdiv);
225 debug("refclkdiv : %d\n", refclkdiv);
226 debug("mscnt : %d\n", mscnt);
227 debug("hscnt : %d\n", hscnt);
228 debug("vcocalib : 0x%08x\n", vcocalib);
229
230 return vcocalib;
231}
232
233/*
234 * Setup clocks while making no assumptions about previous state of the clocks.
235 */
236static void clk_basic_init(struct udevice *dev,
237 const struct cm_config * const cfg)
238{
239 struct socfpga_clk_platdata *plat = dev_get_platdata(dev);
240 u32 vcocalib;
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800241
242 if (!cfg)
243 return;
244
Chee Hong Ang2d94ee62020-07-10 20:55:22 +0800245#ifdef CONFIG_SPL_BUILD
246 /* Always force clock manager into boot mode before any configuration */
247 clk_write_ctrl(plat,
248 CM_REG_READL(plat, CLKMGR_CTRL) | CLKMGR_CTRL_BOOTMODE);
249#else
250 /* Skip clock configuration in SSBL if it's not in boot mode */
251 if (!(CM_REG_READL(plat, CLKMGR_CTRL) & CLKMGR_CTRL_BOOTMODE))
252 return;
253#endif
254
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800255 /* 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 /* Put both PLLs in Reset and Power Down */
260 CM_REG_CLRBITS(plat, CLKMGR_MAINPLL_PLLGLOB,
261 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
262 CM_REG_CLRBITS(plat, CLKMGR_PERPLL_PLLGLOB,
263 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
264
265 /* setup main PLL dividers where calculate the vcocalib value */
266 vcocalib = calc_vocalib_pll(cfg->main_pll_pllm, cfg->main_pll_pllglob);
267 CM_REG_WRITEL(plat, cfg->main_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK,
268 CLKMGR_MAINPLL_PLLGLOB);
269 CM_REG_WRITEL(plat, cfg->main_pll_fdbck, CLKMGR_MAINPLL_FDBCK);
270 CM_REG_WRITEL(plat, vcocalib, CLKMGR_MAINPLL_VCOCALIB);
271 CM_REG_WRITEL(plat, cfg->main_pll_pllc0, CLKMGR_MAINPLL_PLLC0);
272 CM_REG_WRITEL(plat, cfg->main_pll_pllc1, CLKMGR_MAINPLL_PLLC1);
273 CM_REG_WRITEL(plat, cfg->main_pll_pllc2, CLKMGR_MAINPLL_PLLC2);
274 CM_REG_WRITEL(plat, cfg->main_pll_pllc3, CLKMGR_MAINPLL_PLLC3);
275 CM_REG_WRITEL(plat, cfg->main_pll_pllm, CLKMGR_MAINPLL_PLLM);
276 CM_REG_WRITEL(plat, cfg->main_pll_mpuclk, CLKMGR_MAINPLL_MPUCLK);
277 CM_REG_WRITEL(plat, cfg->main_pll_nocclk, CLKMGR_MAINPLL_NOCCLK);
278 CM_REG_WRITEL(plat, cfg->main_pll_nocdiv, CLKMGR_MAINPLL_NOCDIV);
279
280 /* setup peripheral PLL dividers where calculate the vcocalib value */
281 vcocalib = calc_vocalib_pll(cfg->per_pll_pllm, cfg->per_pll_pllglob);
282 CM_REG_WRITEL(plat, cfg->per_pll_pllglob & ~CLKMGR_PLLGLOB_RST_MASK,
283 CLKMGR_PERPLL_PLLGLOB);
284 CM_REG_WRITEL(plat, cfg->per_pll_fdbck, CLKMGR_PERPLL_FDBCK);
285 CM_REG_WRITEL(plat, vcocalib, CLKMGR_PERPLL_VCOCALIB);
286 CM_REG_WRITEL(plat, cfg->per_pll_pllc0, CLKMGR_PERPLL_PLLC0);
287 CM_REG_WRITEL(plat, cfg->per_pll_pllc1, CLKMGR_PERPLL_PLLC1);
288 CM_REG_WRITEL(plat, cfg->per_pll_pllc2, CLKMGR_PERPLL_PLLC2);
289 CM_REG_WRITEL(plat, cfg->per_pll_pllc3, CLKMGR_PERPLL_PLLC3);
290 CM_REG_WRITEL(plat, cfg->per_pll_pllm, CLKMGR_PERPLL_PLLM);
291 CM_REG_WRITEL(plat, cfg->per_pll_emacctl, CLKMGR_PERPLL_EMACCTL);
292 CM_REG_WRITEL(plat, cfg->per_pll_gpiodiv, CLKMGR_PERPLL_GPIODIV);
293
294 /* Take both PLL out of reset and power up */
295 CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLGLOB,
296 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
297 CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLGLOB,
298 CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
299
Chee Hong Ang95793012020-07-10 20:55:23 +0800300 /* Membus programming for mainpll */
301 membus_pll_configs(plat, MEMBUS_MAINPLL);
302 /* Membus programming for peripll */
303 membus_pll_configs(plat, MEMBUS_PERPLL);
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800304
305 cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK);
306
307 /* Configure ping pong counters in altera group */
308 CM_REG_WRITEL(plat, cfg->alt_emacactr, CLKMGR_ALTR_EMACACTR);
309 CM_REG_WRITEL(plat, cfg->alt_emacbctr, CLKMGR_ALTR_EMACBCTR);
310 CM_REG_WRITEL(plat, cfg->alt_emacptpctr, CLKMGR_ALTR_EMACPTPCTR);
311 CM_REG_WRITEL(plat, cfg->alt_gpiodbctr, CLKMGR_ALTR_GPIODBCTR);
312 CM_REG_WRITEL(plat, cfg->alt_sdmmcctr, CLKMGR_ALTR_SDMMCCTR);
313 CM_REG_WRITEL(plat, cfg->alt_s2fuser0ctr, CLKMGR_ALTR_S2FUSER0CTR);
314 CM_REG_WRITEL(plat, cfg->alt_s2fuser1ctr, CLKMGR_ALTR_S2FUSER1CTR);
315 CM_REG_WRITEL(plat, cfg->alt_psirefctr, CLKMGR_ALTR_PSIREFCTR);
316
317 CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_MAINPLL_LOSTLOCK);
318 CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_PERPLL_LOSTLOCK);
319
320 CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLGLOB) |
321 CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
322 CLKMGR_MAINPLL_PLLGLOB);
323 CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLGLOB) |
324 CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
325 CLKMGR_PERPLL_PLLGLOB);
326
327 /* Take all PLLs out of bypass */
328 clk_write_bypass_mainpll(plat, 0);
329 clk_write_bypass_perpll(plat, 0);
330
331 /* Clear the loss of lock bits (write 1 to clear) */
332 CM_REG_CLRBITS(plat, CLKMGR_INTRCLR,
333 CLKMGR_INTER_PERPLLLOST_MASK |
334 CLKMGR_INTER_MAINPLLLOST_MASK);
335
336 /* Take all ping pong counters out of reset */
337 CM_REG_CLRBITS(plat, CLKMGR_ALTR_EXTCNTRST,
338 CLKMGR_ALT_EXTCNTRST_ALLCNTRST);
339
340 /* Out of boot mode */
341 clk_write_ctrl(plat,
342 CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE);
343}
344
345static u64 clk_get_vco_clk_hz(struct socfpga_clk_platdata *plat,
346 u32 pllglob_reg, u32 pllm_reg)
347{
348 u64 fref, arefdiv, mdiv, reg, vco;
349
350 reg = CM_REG_READL(plat, pllglob_reg);
351
352 fref = (reg & CLKMGR_PLLGLOB_VCO_PSRC_MASK) >>
353 CLKMGR_PLLGLOB_VCO_PSRC_OFFSET;
354
355 switch (fref) {
356 case CLKMGR_VCO_PSRC_EOSC1:
357 fref = cm_get_osc_clk_hz();
358 break;
359 case CLKMGR_VCO_PSRC_INTOSC:
360 fref = cm_get_intosc_clk_hz();
361 break;
362 case CLKMGR_VCO_PSRC_F2S:
363 fref = cm_get_fpga_clk_hz();
364 break;
365 }
366
367 arefdiv = (reg & CLKMGR_PLLGLOB_AREFCLKDIV_MASK) >>
368 CLKMGR_PLLGLOB_AREFCLKDIV_OFFSET;
369
370 mdiv = CM_REG_READL(plat, pllm_reg) & CLKMGR_PLLM_MDIV_MASK;
371
372 vco = fref / arefdiv;
373 vco = vco * mdiv;
374
375 return vco;
376}
377
378static u64 clk_get_main_vco_clk_hz(struct socfpga_clk_platdata *plat)
379{
380 return clk_get_vco_clk_hz(plat, CLKMGR_MAINPLL_PLLGLOB,
381 CLKMGR_MAINPLL_PLLM);
382}
383
384static u64 clk_get_per_vco_clk_hz(struct socfpga_clk_platdata *plat)
385{
386 return clk_get_vco_clk_hz(plat, CLKMGR_PERPLL_PLLGLOB,
387 CLKMGR_PERPLL_PLLM);
388}
389
390static u32 clk_get_5_1_clk_src(struct socfpga_clk_platdata *plat, u64 reg)
391{
392 u32 clksrc = CM_REG_READL(plat, reg);
393
394 return (clksrc & CLKMGR_CLKSRC_MASK) >> CLKMGR_CLKSRC_OFFSET;
395}
396
397static u64 clk_get_clksrc_hz(struct socfpga_clk_platdata *plat, u32 clksrc_reg,
398 u32 main_reg, u32 per_reg)
399{
400 u64 clock;
401 u32 clklsrc = clk_get_5_1_clk_src(plat, clksrc_reg);
402
403 switch (clklsrc) {
404 case CLKMGR_CLKSRC_MAIN:
405 clock = clk_get_main_vco_clk_hz(plat);
406 clock /= (CM_REG_READL(plat, main_reg) &
407 CLKMGR_CLKCNT_MSK);
408 break;
409
410 case CLKMGR_CLKSRC_PER:
411 clock = clk_get_per_vco_clk_hz(plat);
412 clock /= (CM_REG_READL(plat, per_reg) &
413 CLKMGR_CLKCNT_MSK);
414 break;
415
416 case CLKMGR_CLKSRC_OSC1:
417 clock = cm_get_osc_clk_hz();
418 break;
419
420 case CLKMGR_CLKSRC_INTOSC:
421 clock = cm_get_intosc_clk_hz();
422 break;
423
424 case CLKMGR_CLKSRC_FPGA:
425 clock = cm_get_fpga_clk_hz();
426 break;
427 default:
428 return 0;
429 }
430
431 return clock;
432}
433
434static u64 clk_get_mpu_clk_hz(struct socfpga_clk_platdata *plat)
435{
436 u64 clock = clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_MPUCLK,
437 CLKMGR_MAINPLL_PLLC0,
438 CLKMGR_PERPLL_PLLC0);
439
440 clock /= 1 + (CM_REG_READL(plat, CLKMGR_MAINPLL_MPUCLK) &
441 CLKMGR_CLKCNT_MSK);
442
443 return clock;
444}
445
446static u32 clk_get_l3_main_clk_hz(struct socfpga_clk_platdata *plat)
447{
448 return clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_NOCCLK,
449 CLKMGR_MAINPLL_PLLC1,
450 CLKMGR_PERPLL_PLLC1);
451}
452
453static u32 clk_get_l4_main_clk_hz(struct socfpga_clk_platdata *plat)
454{
455 u64 clock = clk_get_l3_main_clk_hz(plat);
456
457 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
458 CLKMGR_NOCDIV_L4MAIN_OFFSET) &
459 CLKMGR_NOCDIV_DIVIDER_MASK);
460
461 return clock;
462}
463
464static u32 clk_get_sdmmc_clk_hz(struct socfpga_clk_platdata *plat)
465{
466 u64 clock = clk_get_clksrc_hz(plat, CLKMGR_ALTR_SDMMCCTR,
467 CLKMGR_MAINPLL_PLLC3,
468 CLKMGR_PERPLL_PLLC3);
469
470 clock /= 1 + (CM_REG_READL(plat, CLKMGR_ALTR_SDMMCCTR) &
471 CLKMGR_CLKCNT_MSK);
472
473 return clock / 4;
474}
475
476static u32 clk_get_l4_sp_clk_hz(struct socfpga_clk_platdata *plat)
477{
478 u64 clock = clk_get_l3_main_clk_hz(plat);
479
480 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
481 CLKMGR_NOCDIV_L4SPCLK_OFFSET) &
482 CLKMGR_NOCDIV_DIVIDER_MASK);
483
484 return clock;
485}
486
487static u32 clk_get_l4_mp_clk_hz(struct socfpga_clk_platdata *plat)
488{
489 u64 clock = clk_get_l3_main_clk_hz(plat);
490
491 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
492 CLKMGR_NOCDIV_L4MPCLK_OFFSET) &
493 CLKMGR_NOCDIV_DIVIDER_MASK);
494
495 return clock;
496}
497
498static u32 clk_get_l4_sys_free_clk_hz(struct socfpga_clk_platdata *plat)
499{
500 if (CM_REG_READL(plat, CLKMGR_STAT) & CLKMGR_STAT_BOOTMODE)
501 return clk_get_l3_main_clk_hz(plat) / 2;
502
503 return clk_get_l3_main_clk_hz(plat) / 4;
504}
505
506static u32 clk_get_emac_clk_hz(struct socfpga_clk_platdata *plat, u32 emac_id)
507{
508 bool emacsel_a;
509 u32 ctl;
510 u32 ctr_reg;
511 u32 clock;
512 u32 div;
513 u32 reg;
514
515 /* Get EMAC clock source */
516 ctl = CM_REG_READL(plat, CLKMGR_PERPLL_EMACCTL);
517 if (emac_id == AGILEX_EMAC0_CLK)
518 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_OFFSET) &
519 CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_MASK;
520 else if (emac_id == AGILEX_EMAC1_CLK)
521 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_OFFSET) &
522 CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_MASK;
523 else if (emac_id == AGILEX_EMAC2_CLK)
524 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_OFFSET) &
525 CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_MASK;
526 else
527 return 0;
528
529 if (ctl) {
530 /* EMAC B source */
531 emacsel_a = false;
532 ctr_reg = CLKMGR_ALTR_EMACBCTR;
533 } else {
534 /* EMAC A source */
535 emacsel_a = true;
536 ctr_reg = CLKMGR_ALTR_EMACACTR;
537 }
538
539 reg = CM_REG_READL(plat, ctr_reg);
540 clock = (reg & CLKMGR_ALT_EMACCTR_SRC_MASK)
541 >> CLKMGR_ALT_EMACCTR_SRC_OFFSET;
542 div = (reg & CLKMGR_ALT_EMACCTR_CNT_MASK)
543 >> CLKMGR_ALT_EMACCTR_CNT_OFFSET;
544
545 switch (clock) {
546 case CLKMGR_CLKSRC_MAIN:
547 clock = clk_get_main_vco_clk_hz(plat);
548 if (emacsel_a) {
549 clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC2) &
550 CLKMGR_CLKCNT_MSK);
551 } else {
552 clock /= (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLC3) &
553 CLKMGR_CLKCNT_MSK);
554 }
555 break;
556
557 case CLKMGR_CLKSRC_PER:
558 clock = clk_get_per_vco_clk_hz(plat);
559 if (emacsel_a) {
560 clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC2) &
561 CLKMGR_CLKCNT_MSK);
562 } else {
563 clock /= (CM_REG_READL(plat, CLKMGR_PERPLL_PLLC3) &
564 CLKMGR_CLKCNT_MSK);
565 }
566 break;
567
568 case CLKMGR_CLKSRC_OSC1:
569 clock = cm_get_osc_clk_hz();
570 break;
571
572 case CLKMGR_CLKSRC_INTOSC:
573 clock = cm_get_intosc_clk_hz();
574 break;
575
576 case CLKMGR_CLKSRC_FPGA:
577 clock = cm_get_fpga_clk_hz();
578 break;
579 }
580
581 clock /= 1 + div;
582
583 return clock;
584}
585
586static ulong socfpga_clk_get_rate(struct clk *clk)
587{
588 struct socfpga_clk_platdata *plat = dev_get_platdata(clk->dev);
589
590 switch (clk->id) {
591 case AGILEX_MPU_CLK:
592 return clk_get_mpu_clk_hz(plat);
593 case AGILEX_L4_MAIN_CLK:
594 return clk_get_l4_main_clk_hz(plat);
595 case AGILEX_L4_SYS_FREE_CLK:
596 return clk_get_l4_sys_free_clk_hz(plat);
597 case AGILEX_L4_MP_CLK:
598 return clk_get_l4_mp_clk_hz(plat);
599 case AGILEX_L4_SP_CLK:
600 return clk_get_l4_sp_clk_hz(plat);
601 case AGILEX_SDMMC_CLK:
602 return clk_get_sdmmc_clk_hz(plat);
603 case AGILEX_EMAC0_CLK:
604 case AGILEX_EMAC1_CLK:
605 case AGILEX_EMAC2_CLK:
606 return clk_get_emac_clk_hz(plat, clk->id);
607 case AGILEX_USB_CLK:
Ley Foon Tanf94b0fd2020-07-10 20:55:20 +0800608 case AGILEX_NAND_X_CLK:
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800609 return clk_get_l4_mp_clk_hz(plat);
Ley Foon Tanf94b0fd2020-07-10 20:55:20 +0800610 case AGILEX_NAND_CLK:
611 return clk_get_l4_mp_clk_hz(plat) / 4;
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800612 default:
613 return -ENXIO;
614 }
615}
616
Ley Foon Tan76092202020-07-10 20:55:21 +0800617static int socfpga_clk_enable(struct clk *clk)
618{
619 return 0;
620}
621
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800622static int socfpga_clk_probe(struct udevice *dev)
623{
624 const struct cm_config *cm_default_cfg = cm_get_default_config();
625
626 clk_basic_init(dev, cm_default_cfg);
627
628 return 0;
629}
630
631static int socfpga_clk_ofdata_to_platdata(struct udevice *dev)
632{
633 struct socfpga_clk_platdata *plat = dev_get_platdata(dev);
634 fdt_addr_t addr;
635
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900636 addr = dev_read_addr(dev);
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800637 if (addr == FDT_ADDR_T_NONE)
638 return -EINVAL;
639 plat->regs = (void __iomem *)addr;
640
641 return 0;
642}
643
644static struct clk_ops socfpga_clk_ops = {
Ley Foon Tan76092202020-07-10 20:55:21 +0800645 .enable = socfpga_clk_enable,
Ley Foon Tanef5458f2019-11-27 15:55:22 +0800646 .get_rate = socfpga_clk_get_rate,
647};
648
649static const struct udevice_id socfpga_clk_match[] = {
650 { .compatible = "intel,agilex-clkmgr" },
651 {}
652};
653
654U_BOOT_DRIVER(socfpga_agilex_clk) = {
655 .name = "clk-agilex",
656 .id = UCLASS_CLK,
657 .of_match = socfpga_clk_match,
658 .ops = &socfpga_clk_ops,
659 .probe = socfpga_clk_probe,
660 .ofdata_to_platdata = socfpga_clk_ofdata_to_platdata,
661 .platdata_auto_alloc_size = sizeof(struct socfpga_clk_platdata),
662};