blob: 3e256101a947a772fc273a4567d6e7e89f5d3092 [file] [log] [blame]
Teik Heng Chong8ab95782022-06-29 13:51:50 +08001// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
Siew Chin Lim1557df82021-08-10 11:26:30 +08002/*
Teik Heng Chong8ab95782022-06-29 13:51:50 +08003 * Copyright (C) 2020-2022 Intel Corporation <www.intel.com>
Siew Chin Lim1557df82021-08-10 11:26:30 +08004 */
5
Siew Chin Lim1557df82021-08-10 11:26:30 +08006#include <asm/arch/clock_manager.h>
7#include <asm/global_data.h>
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/n5x-clock.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17struct socfpga_clk_plat {
18 void __iomem *regs;
19};
20
21/*
22 * function to write the bypass register which requires a poll of the
23 * busy bit
24 */
25static void clk_write_bypass_mainpll(struct socfpga_clk_plat *plat, u32 val)
26{
27 CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_BYPASS);
28 cm_wait_for_fsm();
29}
30
31static void clk_write_bypass_perpll(struct socfpga_clk_plat *plat, u32 val)
32{
33 CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_BYPASS);
34 cm_wait_for_fsm();
35}
36
37/* function to write the ctrl register which requires a poll of the busy bit */
38static void clk_write_ctrl(struct socfpga_clk_plat *plat, u32 val)
39{
40 CM_REG_WRITEL(plat, val, CLKMGR_CTRL);
41 cm_wait_for_fsm();
42}
43
44/*
45 * Setup clocks while making no assumptions about previous state of the clocks.
46 */
47static void clk_basic_init(struct udevice *dev,
48 const struct cm_config * const cfg)
49{
50 struct socfpga_clk_plat *plat = dev_get_plat(dev);
51
52 if (!cfg)
53 return;
54
55#if IS_ENABLED(CONFIG_SPL_BUILD)
56 /* Always force clock manager into boot mode before any configuration */
57 clk_write_ctrl(plat,
58 CM_REG_READL(plat, CLKMGR_CTRL) | CLKMGR_CTRL_BOOTMODE);
59#else
60 /* Skip clock configuration in SSBL if it's not in boot mode */
61 if (!(CM_REG_READL(plat, CLKMGR_CTRL) & CLKMGR_CTRL_BOOTMODE))
62 return;
63#endif
64
65 /* Put both PLLs in bypass */
66 clk_write_bypass_mainpll(plat, CLKMGR_BYPASS_MAINPLL_ALL);
67 clk_write_bypass_perpll(plat, CLKMGR_BYPASS_PERPLL_ALL);
68
69 /* Put both PLLs in Reset */
70 CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLCTRL,
71 CLKMGR_PLLCTRL_BYPASS_MASK);
72 CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLCTRL,
73 CLKMGR_PLLCTRL_BYPASS_MASK);
74
75 /* setup main PLL */
76 CM_REG_WRITEL(plat, cfg->main_pll_pllglob, CLKMGR_MAINPLL_PLLGLOB);
77 CM_REG_WRITEL(plat, cfg->main_pll_plldiv, CLKMGR_MAINPLL_PLLDIV);
78 CM_REG_WRITEL(plat, cfg->main_pll_plloutdiv, CLKMGR_MAINPLL_PLLOUTDIV);
79 CM_REG_WRITEL(plat, cfg->main_pll_mpuclk, CLKMGR_MAINPLL_MPUCLK);
80 CM_REG_WRITEL(plat, cfg->main_pll_nocclk, CLKMGR_MAINPLL_NOCCLK);
81 CM_REG_WRITEL(plat, cfg->main_pll_nocdiv, CLKMGR_MAINPLL_NOCDIV);
82
83 /* setup peripheral */
84 CM_REG_WRITEL(plat, cfg->per_pll_pllglob, CLKMGR_PERPLL_PLLGLOB);
85 CM_REG_WRITEL(plat, cfg->per_pll_plldiv, CLKMGR_PERPLL_PLLDIV);
86 CM_REG_WRITEL(plat, cfg->per_pll_plloutdiv, CLKMGR_PERPLL_PLLOUTDIV);
87 CM_REG_WRITEL(plat, cfg->per_pll_emacctl, CLKMGR_PERPLL_EMACCTL);
88 CM_REG_WRITEL(plat, cfg->per_pll_gpiodiv, CLKMGR_PERPLL_GPIODIV);
89
90 /* Take both PLL out of reset and power up */
91 CM_REG_CLRBITS(plat, CLKMGR_MAINPLL_PLLCTRL,
92 CLKMGR_PLLCTRL_BYPASS_MASK);
93 CM_REG_CLRBITS(plat, CLKMGR_PERPLL_PLLCTRL,
94 CLKMGR_PLLCTRL_BYPASS_MASK);
95
96 cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK);
97
98 CM_REG_WRITEL(plat, cfg->alt_emacactr, CLKMGR_ALTR_EMACACTR);
99 CM_REG_WRITEL(plat, cfg->alt_emacbctr, CLKMGR_ALTR_EMACBCTR);
100 CM_REG_WRITEL(plat, cfg->alt_emacptpctr, CLKMGR_ALTR_EMACPTPCTR);
101 CM_REG_WRITEL(plat, cfg->alt_gpiodbctr, CLKMGR_ALTR_GPIODBCTR);
102 CM_REG_WRITEL(plat, cfg->alt_sdmmcctr, CLKMGR_ALTR_SDMMCCTR);
103 CM_REG_WRITEL(plat, cfg->alt_s2fuser0ctr, CLKMGR_ALTR_S2FUSER0CTR);
104 CM_REG_WRITEL(plat, cfg->alt_s2fuser1ctr, CLKMGR_ALTR_S2FUSER1CTR);
105 CM_REG_WRITEL(plat, cfg->alt_psirefctr, CLKMGR_ALTR_PSIREFCTR);
106
107 /* Configure ping pong counters in altera group */
108 CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_MAINPLL_LOSTLOCK);
109 CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_PERPLL_LOSTLOCK);
110
111 CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLGLOB) |
112 CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
113 CLKMGR_MAINPLL_PLLGLOB);
114 CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLGLOB) |
115 CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
116 CLKMGR_PERPLL_PLLGLOB);
117
118 /* Take all PLLs out of bypass */
119 clk_write_bypass_mainpll(plat, 0);
120 clk_write_bypass_perpll(plat, 0);
121
122 /* Clear the loss of lock bits */
123 CM_REG_CLRBITS(plat, CLKMGR_INTRCLR,
124 CLKMGR_INTER_PERPLLLOST_MASK |
125 CLKMGR_INTER_MAINPLLLOST_MASK);
126
127 /* Take all ping pong counters out of reset */
128 CM_REG_CLRBITS(plat, CLKMGR_ALTR_EXTCNTRST,
129 CLKMGR_ALT_EXTCNTRST_ALLCNTRST_MASK);
130
131 /* Out of boot mode */
132 clk_write_ctrl(plat,
133 CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE);
134}
135
136static u32 clk_get_5_1_clk_src(struct socfpga_clk_plat *plat, u32 reg)
137{
138 u32 clksrc = CM_REG_READL(plat, reg);
139
140 return (clksrc & CLKMGR_CLKSRC_MASK) >> CLKMGR_CLKSRC_OFFSET;
141}
142
143static u64 clk_get_pll_output_hz(struct socfpga_clk_plat *plat,
144 u32 pllglob_reg, u32 plldiv_reg)
145{
146 u64 clock = 0;
147 u32 clklsrc, divf, divr, divq, power = 1;
148
149 /* Get input clock frequency */
150 clklsrc = (CM_REG_READL(plat, pllglob_reg) &
151 CLKMGR_PLLGLOB_VCO_PSRC_MASK) >>
152 CLKMGR_PLLGLOB_VCO_PSRC_OFFSET;
153
154 switch (clklsrc) {
155 case CLKMGR_VCO_PSRC_EOSC1:
156 clock = cm_get_osc_clk_hz();
157 break;
158 case CLKMGR_VCO_PSRC_INTOSC:
159 clock = cm_get_intosc_clk_hz();
160 break;
161 case CLKMGR_VCO_PSRC_F2S:
162 clock = cm_get_fpga_clk_hz();
163 break;
164 }
165
166 /* Calculate pll out clock frequency */
167 divf = (CM_REG_READL(plat, plldiv_reg) &
168 CLKMGR_PLLDIV_FDIV_MASK) >>
169 CLKMGR_PLLDIV_FDIV_OFFSET;
170
171 divr = (CM_REG_READL(plat, plldiv_reg) &
172 CLKMGR_PLLDIV_REFCLKDIV_MASK) >>
173 CLKMGR_PLLDIV_REFCLKDIV_OFFSET;
174
175 divq = (CM_REG_READL(plat, plldiv_reg) &
176 CLKMGR_PLLDIV_OUTDIV_QDIV_MASK) >>
177 CLKMGR_PLLDIV_OUTDIV_QDIV_OFFSET;
178
179 while (divq) {
180 power *= 2;
181 divq--;
182 }
183
184 return (clock * 2 * (divf + 1)) / ((divr + 1) * power);
185}
186
187static u64 clk_get_clksrc_hz(struct socfpga_clk_plat *plat, u32 clksrc_reg,
188 u32 main_div, u32 per_div)
189{
190 u64 clock = 0;
191 u32 clklsrc = clk_get_5_1_clk_src(plat, clksrc_reg);
192
193 switch (clklsrc) {
194 case CLKMGR_CLKSRC_MAIN:
195 clock = clk_get_pll_output_hz(plat,
196 CLKMGR_MAINPLL_PLLGLOB,
197 CLKMGR_MAINPLL_PLLDIV);
198 clock /= 1 + main_div;
199 break;
200
201 case CLKMGR_CLKSRC_PER:
202 clock = clk_get_pll_output_hz(plat,
203 CLKMGR_PERPLL_PLLGLOB,
204 CLKMGR_PERPLL_PLLDIV);
205 clock /= 1 + per_div;
206 break;
207
208 case CLKMGR_CLKSRC_OSC1:
209 clock = cm_get_osc_clk_hz();
210 break;
211
212 case CLKMGR_CLKSRC_INTOSC:
213 clock = cm_get_intosc_clk_hz();
214 break;
215
216 case CLKMGR_CLKSRC_FPGA:
217 clock = cm_get_fpga_clk_hz();
218 break;
219 default:
220 return 0;
221 }
222
223 return clock;
224}
225
226static u64 clk_get_mpu_clk_hz(struct socfpga_clk_plat *plat)
227{
228 u32 mainpll_c0cnt = (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLOUTDIV) &
229 CLKMGR_PLLOUTDIV_C0CNT_MASK) >>
230 CLKMGR_PLLOUTDIV_C0CNT_OFFSET;
231
232 u32 perpll_c0cnt = (CM_REG_READL(plat, CLKMGR_PERPLL_PLLOUTDIV) &
233 CLKMGR_PLLOUTDIV_C0CNT_MASK) >>
234 CLKMGR_PLLOUTDIV_C0CNT_OFFSET;
235
236 u64 clock = clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_MPUCLK,
237 mainpll_c0cnt, perpll_c0cnt);
238
239 clock /= 1 + (CM_REG_READL(plat, CLKMGR_MAINPLL_MPUCLK) &
240 CLKMGR_CLKCNT_MSK);
241
242 return clock;
243}
244
245static u32 clk_get_l3_main_clk_hz(struct socfpga_clk_plat *plat)
246{
247 u32 mainpll_c1cnt = (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLOUTDIV) &
248 CLKMGR_PLLOUTDIV_C1CNT_MASK) >>
249 CLKMGR_PLLOUTDIV_C1CNT_OFFSET;
250
251 u32 perpll_c1cnt = (CM_REG_READL(plat, CLKMGR_PERPLL_PLLOUTDIV) &
252 CLKMGR_PLLOUTDIV_C1CNT_MASK) >>
253 CLKMGR_PLLOUTDIV_C1CNT_OFFSET;
254
255 return clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_NOCCLK,
256 mainpll_c1cnt, perpll_c1cnt);
257}
258
259static u32 clk_get_l4_main_clk_hz(struct socfpga_clk_plat *plat)
260{
261 u64 clock = clk_get_l3_main_clk_hz(plat);
262
263 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
264 CLKMGR_NOCDIV_L4MAIN_OFFSET) &
265 CLKMGR_NOCDIV_DIVIDER_MASK);
266
267 return clock;
268}
269
270static u32 clk_get_sdmmc_clk_hz(struct socfpga_clk_plat *plat)
271{
272 u32 mainpll_c3cnt = (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLOUTDIV) &
273 CLKMGR_PLLOUTDIV_C3CNT_MASK) >>
274 CLKMGR_PLLOUTDIV_C3CNT_OFFSET;
275
276 u32 perpll_c3cnt = (CM_REG_READL(plat, CLKMGR_PERPLL_PLLOUTDIV) &
277 CLKMGR_PLLOUTDIV_C3CNT_MASK) >>
278 CLKMGR_PLLOUTDIV_C3CNT_OFFSET;
279
280 u64 clock = clk_get_clksrc_hz(plat, CLKMGR_ALTR_SDMMCCTR,
281 mainpll_c3cnt, perpll_c3cnt);
282
283 clock /= 1 + (CM_REG_READL(plat, CLKMGR_ALTR_SDMMCCTR) &
284 CLKMGR_CLKCNT_MSK);
285
286 return clock / 4;
287}
288
289static u32 clk_get_l4_sp_clk_hz(struct socfpga_clk_plat *plat)
290{
291 u64 clock = clk_get_l3_main_clk_hz(plat);
292
293 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
294 CLKMGR_NOCDIV_L4SPCLK_OFFSET) &
295 CLKMGR_NOCDIV_DIVIDER_MASK);
296
297 return clock;
298}
299
300static u32 clk_get_l4_mp_clk_hz(struct socfpga_clk_plat *plat)
301{
302 u64 clock = clk_get_l3_main_clk_hz(plat);
303
304 clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
305 CLKMGR_NOCDIV_L4MPCLK_OFFSET) &
306 CLKMGR_NOCDIV_DIVIDER_MASK);
307
308 return clock;
309}
310
311static u32 clk_get_l4_sys_free_clk_hz(struct socfpga_clk_plat *plat)
312{
313 if (CM_REG_READL(plat, CLKMGR_STAT) & CLKMGR_STAT_BOOTMODE)
314 return clk_get_l3_main_clk_hz(plat) / 2;
315
316 return clk_get_l3_main_clk_hz(plat) / 4;
317}
318
319static u32 clk_get_emac_clk_hz(struct socfpga_clk_plat *plat, u32 emac_id)
320{
321 bool emacsel_a;
322 u32 ctl;
323 u32 ctr_reg;
324 u32 clock;
325 u32 div;
326 u32 reg;
327
328 /* Get EMAC clock source */
329 ctl = CM_REG_READL(plat, CLKMGR_PERPLL_EMACCTL);
330 if (emac_id == N5X_EMAC0_CLK)
331 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_OFFSET) &
332 CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_MASK;
333 else if (emac_id == N5X_EMAC1_CLK)
334 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_OFFSET) &
335 CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_MASK;
336 else if (emac_id == N5X_EMAC2_CLK)
337 ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_OFFSET) &
338 CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_MASK;
339 else
340 return 0;
341
342 if (ctl) {
343 /* EMAC B source */
344 emacsel_a = false;
345 ctr_reg = CLKMGR_ALTR_EMACBCTR;
346 } else {
347 /* EMAC A source */
348 emacsel_a = true;
349 ctr_reg = CLKMGR_ALTR_EMACACTR;
350 }
351
352 reg = CM_REG_READL(plat, ctr_reg);
353 clock = (reg & CLKMGR_ALT_EMACCTR_SRC_MASK)
354 >> CLKMGR_ALT_EMACCTR_SRC_OFFSET;
355 div = (reg & CLKMGR_ALT_EMACCTR_CNT_MASK)
356 >> CLKMGR_ALT_EMACCTR_CNT_OFFSET;
357
358 switch (clock) {
359 case CLKMGR_CLKSRC_MAIN:
360 clock = clk_get_pll_output_hz(plat,
361 CLKMGR_MAINPLL_PLLGLOB,
362 CLKMGR_MAINPLL_PLLDIV);
363
364 if (emacsel_a) {
365 clock /= 1 + ((CM_REG_READL(plat,
366 CLKMGR_MAINPLL_PLLOUTDIV) &
367 CLKMGR_PLLOUTDIV_C2CNT_MASK) >>
368 CLKMGR_PLLOUTDIV_C2CNT_OFFSET);
369 } else {
370 clock /= 1 + ((CM_REG_READL(plat,
371 CLKMGR_MAINPLL_PLLOUTDIV) &
372 CLKMGR_PLLOUTDIV_C3CNT_MASK) >>
373 CLKMGR_PLLOUTDIV_C3CNT_OFFSET);
374 }
375 break;
376
377 case CLKMGR_CLKSRC_PER:
378 clock = clk_get_pll_output_hz(plat,
379 CLKMGR_PERPLL_PLLGLOB,
380 CLKMGR_PERPLL_PLLDIV);
381 if (emacsel_a) {
382 clock /= 1 + ((CM_REG_READL(plat,
383 CLKMGR_PERPLL_PLLOUTDIV) &
384 CLKMGR_PLLOUTDIV_C2CNT_MASK) >>
385 CLKMGR_PLLOUTDIV_C2CNT_OFFSET);
386 } else {
387 clock /= 1 + ((CM_REG_READL(plat,
388 CLKMGR_PERPLL_PLLOUTDIV) &
389 CLKMGR_PLLOUTDIV_C3CNT_MASK >>
390 CLKMGR_PLLOUTDIV_C3CNT_OFFSET));
391 }
392 break;
393
394 case CLKMGR_CLKSRC_OSC1:
395 clock = cm_get_osc_clk_hz();
396 break;
397
398 case CLKMGR_CLKSRC_INTOSC:
399 clock = cm_get_intosc_clk_hz();
400 break;
401
402 case CLKMGR_CLKSRC_FPGA:
403 clock = cm_get_fpga_clk_hz();
404 break;
405 }
406
407 clock /= 1 + div;
408
409 return clock;
410}
411
412static ulong socfpga_clk_get_rate(struct clk *clk)
413{
414 struct socfpga_clk_plat *plat = dev_get_plat(clk->dev);
415
416 switch (clk->id) {
417 case N5X_MPU_CLK:
418 return clk_get_mpu_clk_hz(plat);
419 case N5X_L4_MAIN_CLK:
420 return clk_get_l4_main_clk_hz(plat);
421 case N5X_L4_SYS_FREE_CLK:
422 return clk_get_l4_sys_free_clk_hz(plat);
423 case N5X_L4_MP_CLK:
424 return clk_get_l4_mp_clk_hz(plat);
425 case N5X_L4_SP_CLK:
426 return clk_get_l4_sp_clk_hz(plat);
427 case N5X_SDMMC_CLK:
428 return clk_get_sdmmc_clk_hz(plat);
429 case N5X_EMAC0_CLK:
430 case N5X_EMAC1_CLK:
431 case N5X_EMAC2_CLK:
432 return clk_get_emac_clk_hz(plat, clk->id);
433 case N5X_USB_CLK:
434 case N5X_NAND_X_CLK:
435 return clk_get_l4_mp_clk_hz(plat);
436 case N5X_NAND_CLK:
437 return clk_get_l4_mp_clk_hz(plat) / 4;
438 default:
439 return -ENXIO;
440 }
441}
442
443static int socfpga_clk_enable(struct clk *clk)
444{
445 return 0;
446}
447
448static int socfpga_clk_probe(struct udevice *dev)
449{
450 const struct cm_config *cm_default_cfg = cm_get_default_config();
451
452 clk_basic_init(dev, cm_default_cfg);
453
454 return 0;
455}
456
457static int socfpga_clk_of_to_plat(struct udevice *dev)
458{
459 struct socfpga_clk_plat *plat = dev_get_plat(dev);
460 fdt_addr_t addr;
461
462 addr = devfdt_get_addr(dev);
463 if (addr == FDT_ADDR_T_NONE)
464 return -EINVAL;
465 plat->regs = (void __iomem *)addr;
466
467 return 0;
468}
469
470static struct clk_ops socfpga_clk_ops = {
471 .enable = socfpga_clk_enable,
472 .get_rate = socfpga_clk_get_rate,
473};
474
475static const struct udevice_id socfpga_clk_match[] = {
476 { .compatible = "intel,n5x-clkmgr" },
477 {}
478};
479
480U_BOOT_DRIVER(socfpga_n5x_clk) = {
481 .name = "clk-n5x",
482 .id = UCLASS_CLK,
483 .of_match = socfpga_clk_match,
484 .ops = &socfpga_clk_ops,
485 .probe = socfpga_clk_probe,
486 .of_to_plat = socfpga_clk_of_to_plat,
487 .plat_auto = sizeof(struct socfpga_clk_plat),
488};