1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Intel Corporation
10 #include <asm/arch/clock_manager.h>
13 static u32 cb_intosc_hz;
14 static u32 f2s_free_hz;
15 static u32 cm_l4_main_clk_hz;
16 static u32 cm_l4_sp_clk_hz;
17 static u32 cm_l4_mp_clk_hz;
18 static u32 cm_l4_sys_free_clk_hz;
45 u32 nocdiv_cstraceclk;
70 u32 gpiodiv_gpiodbclk;
73 struct alteragrp_cfg {
78 static const struct socfpga_clock_manager *clock_manager_base =
79 (struct socfpga_clock_manager *)SOCFPGA_CLKMGR_ADDRESS;
81 static int of_to_struct(const void *blob, int node, int cfg_len, void *cfg)
83 if (fdtdec_get_int_array(blob, node, "altr,of_reg_value",
84 (u32 *)cfg, cfg_len)) {
85 /* could not find required property */
92 static int of_get_input_clks(const void *blob, int node, u32 *val)
94 *val = fdtdec_get_uint(blob, node, "clock-frequency", 0);
101 static int of_get_clk_cfg(const void *blob, struct mainpll_cfg *main_cfg,
102 struct perpll_cfg *per_cfg,
103 struct alteragrp_cfg *altrgrp_cfg)
105 int node, child, len;
106 const char *node_name;
108 node = fdtdec_next_compatible(blob, 0, COMPAT_ALTERA_SOCFPGA_CLK);
112 child = fdt_first_subnode(blob, node);
116 child = fdt_first_subnode(blob, child);
120 node_name = fdt_get_name(blob, child, &len);
123 if (!strcmp(node_name, "osc1")) {
124 if (of_get_input_clks(blob, child, &eosc1_hz))
126 } else if (!strcmp(node_name, "cb_intosc_ls_clk")) {
127 if (of_get_input_clks(blob, child, &cb_intosc_hz))
129 } else if (!strcmp(node_name, "f2s_free_clk")) {
130 if (of_get_input_clks(blob, child, &f2s_free_hz))
132 } else if (!strcmp(node_name, "main_pll")) {
133 if (of_to_struct(blob, child,
134 sizeof(*main_cfg)/sizeof(u32),
137 } else if (!strcmp(node_name, "periph_pll")) {
138 if (of_to_struct(blob, child,
139 sizeof(*per_cfg)/sizeof(u32),
142 } else if (!strcmp(node_name, "altera")) {
143 if (of_to_struct(blob, child,
144 sizeof(*altrgrp_cfg)/sizeof(u32),
148 main_cfg->mpuclk = altrgrp_cfg->mpuclk;
149 main_cfg->nocclk = altrgrp_cfg->nocclk;
151 child = fdt_next_subnode(blob, child);
156 node_name = fdt_get_name(blob, child, &len);
162 /* calculate the intended main VCO frequency based on handoff */
163 static unsigned int cm_calc_handoff_main_vco_clk_hz
164 (struct mainpll_cfg *main_cfg)
168 /* Check main VCO clock source: eosc, intosc or f2s? */
169 switch (main_cfg->vco0_psrc) {
170 case CLKMGR_MAINPLL_VCO0_PSRC_EOSC:
173 case CLKMGR_MAINPLL_VCO0_PSRC_E_INTOSC:
174 clk_hz = cb_intosc_hz;
176 case CLKMGR_MAINPLL_VCO0_PSRC_F2S:
177 clk_hz = f2s_free_hz;
183 /* calculate the VCO frequency */
184 clk_hz /= 1 + main_cfg->vco1_denom;
185 clk_hz *= 1 + main_cfg->vco1_numer;
190 /* calculate the intended periph VCO frequency based on handoff */
191 static unsigned int cm_calc_handoff_periph_vco_clk_hz(
192 struct mainpll_cfg *main_cfg, struct perpll_cfg *per_cfg)
196 /* Check periph VCO clock source: eosc, intosc, f2s or mainpll? */
197 switch (per_cfg->vco0_psrc) {
198 case CLKMGR_PERPLL_VCO0_PSRC_EOSC:
201 case CLKMGR_PERPLL_VCO0_PSRC_E_INTOSC:
202 clk_hz = cb_intosc_hz;
204 case CLKMGR_PERPLL_VCO0_PSRC_F2S:
205 clk_hz = f2s_free_hz;
207 case CLKMGR_PERPLL_VCO0_PSRC_MAIN:
208 clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg);
209 clk_hz /= main_cfg->cntr15clk_cnt;
215 /* calculate the VCO frequency */
216 clk_hz /= 1 + per_cfg->vco1_denom;
217 clk_hz *= 1 + per_cfg->vco1_numer;
222 /* calculate the intended MPU clock frequency based on handoff */
223 static unsigned int cm_calc_handoff_mpu_clk_hz(struct mainpll_cfg *main_cfg,
224 struct perpll_cfg *per_cfg)
228 /* Check MPU clock source: main, periph, osc1, intosc or f2s? */
229 switch (main_cfg->mpuclk_src) {
230 case CLKMGR_MAINPLL_MPUCLK_SRC_MAIN:
231 clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg);
232 clk_hz /= (main_cfg->mpuclk & CLKMGR_MAINPLL_MPUCLK_CNT_MSK)
235 case CLKMGR_MAINPLL_MPUCLK_SRC_PERI:
236 clk_hz = cm_calc_handoff_periph_vco_clk_hz(main_cfg, per_cfg);
237 clk_hz /= ((main_cfg->mpuclk >>
238 CLKMGR_MAINPLL_MPUCLK_PERICNT_LSB) &
239 CLKMGR_MAINPLL_MPUCLK_CNT_MSK) + 1;
241 case CLKMGR_MAINPLL_MPUCLK_SRC_OSC1:
244 case CLKMGR_MAINPLL_MPUCLK_SRC_INTOSC:
245 clk_hz = cb_intosc_hz;
247 case CLKMGR_MAINPLL_MPUCLK_SRC_FPGA:
248 clk_hz = f2s_free_hz;
254 clk_hz /= main_cfg->mpuclk_cnt + 1;
258 /* calculate the intended NOC clock frequency based on handoff */
259 static unsigned int cm_calc_handoff_noc_clk_hz(struct mainpll_cfg *main_cfg,
260 struct perpll_cfg *per_cfg)
264 /* Check MPU clock source: main, periph, osc1, intosc or f2s? */
265 switch (main_cfg->nocclk_src) {
266 case CLKMGR_MAINPLL_NOCCLK_SRC_MAIN:
267 clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg);
268 clk_hz /= (main_cfg->nocclk & CLKMGR_MAINPLL_NOCCLK_CNT_MSK)
271 case CLKMGR_MAINPLL_NOCCLK_SRC_PERI:
272 clk_hz = cm_calc_handoff_periph_vco_clk_hz(main_cfg, per_cfg);
273 clk_hz /= ((main_cfg->nocclk >>
274 CLKMGR_MAINPLL_NOCCLK_PERICNT_LSB) &
275 CLKMGR_MAINPLL_NOCCLK_CNT_MSK) + 1;
277 case CLKMGR_MAINPLL_NOCCLK_SRC_OSC1:
280 case CLKMGR_MAINPLL_NOCCLK_SRC_INTOSC:
281 clk_hz = cb_intosc_hz;
283 case CLKMGR_MAINPLL_NOCCLK_SRC_FPGA:
284 clk_hz = f2s_free_hz;
290 clk_hz /= main_cfg->nocclk_cnt + 1;
294 /* return 1 if PLL ramp is required */
295 static int cm_is_pll_ramp_required(int main0periph1,
296 struct mainpll_cfg *main_cfg,
297 struct perpll_cfg *per_cfg)
299 /* Check for main PLL */
300 if (main0periph1 == 0) {
302 * PLL ramp is not required if both MPU clock and NOC clock are
303 * not sourced from main PLL
305 if (main_cfg->mpuclk_src != CLKMGR_MAINPLL_MPUCLK_SRC_MAIN &&
306 main_cfg->nocclk_src != CLKMGR_MAINPLL_NOCCLK_SRC_MAIN)
310 * PLL ramp is required if MPU clock is sourced from main PLL
311 * and MPU clock is over 900MHz (as advised by HW team)
313 if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_MAIN &&
314 (cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg) >
315 CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ))
319 * PLL ramp is required if NOC clock is sourced from main PLL
320 * and NOC clock is over 300MHz (as advised by HW team)
322 if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_MAIN &&
323 (cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg) >
324 CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ))
327 } else if (main0periph1 == 1) {
329 * PLL ramp is not required if both MPU clock and NOC clock are
330 * not sourced from periph PLL
332 if (main_cfg->mpuclk_src != CLKMGR_MAINPLL_MPUCLK_SRC_PERI &&
333 main_cfg->nocclk_src != CLKMGR_MAINPLL_NOCCLK_SRC_PERI)
337 * PLL ramp is required if MPU clock are source from periph PLL
338 * and MPU clock is over 900MHz (as advised by HW team)
340 if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_PERI &&
341 (cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg) >
342 CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ))
346 * PLL ramp is required if NOC clock are source from periph PLL
347 * and NOC clock is over 300MHz (as advised by HW team)
349 if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_PERI &&
350 (cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg) >
351 CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ))
358 static u32 cm_calculate_numer(struct mainpll_cfg *main_cfg,
359 struct perpll_cfg *per_cfg,
360 u32 safe_hz, u32 clk_hz)
368 if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_MAIN) {
369 cnt = main_cfg->mpuclk_cnt;
370 clk = main_cfg->mpuclk;
372 mask = CLKMGR_MAINPLL_MPUCLK_CNT_MSK;
373 denom = main_cfg->vco1_denom;
374 } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_MAIN) {
375 cnt = main_cfg->nocclk_cnt;
376 clk = main_cfg->nocclk;
378 mask = CLKMGR_MAINPLL_NOCCLK_CNT_MSK;
379 denom = main_cfg->vco1_denom;
380 } else if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_PERI) {
381 cnt = main_cfg->mpuclk_cnt;
382 clk = main_cfg->mpuclk;
383 shift = CLKMGR_MAINPLL_MPUCLK_PERICNT_LSB;
384 mask = CLKMGR_MAINPLL_MPUCLK_CNT_MSK;
385 denom = per_cfg->vco1_denom;
386 } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_PERI) {
387 cnt = main_cfg->nocclk_cnt;
388 clk = main_cfg->nocclk;
389 shift = CLKMGR_MAINPLL_NOCCLK_PERICNT_LSB;
390 mask = CLKMGR_MAINPLL_NOCCLK_CNT_MSK;
391 denom = per_cfg->vco1_denom;
396 return (safe_hz / clk_hz) * (cnt + 1) * (((clk >> shift) & mask) + 1) *
401 * Calculate the new PLL numerator which is based on existing DTS hand off and
402 * intended safe frequency (safe_hz). Note that PLL ramp is only modifying the
403 * numerator while maintaining denominator as denominator will influence the
404 * jitter condition. Please refer A10 HPS TRM for the jitter guide. Note final
405 * value for numerator is minus with 1 to cater our register value
408 static unsigned int cm_calc_safe_pll_numer(int main0periph1,
409 struct mainpll_cfg *main_cfg,
410 struct perpll_cfg *per_cfg,
411 unsigned int safe_hz)
413 unsigned int clk_hz = 0;
415 /* Check for main PLL */
416 if (main0periph1 == 0) {
417 /* Check main VCO clock source: eosc, intosc or f2s? */
418 switch (main_cfg->vco0_psrc) {
419 case CLKMGR_MAINPLL_VCO0_PSRC_EOSC:
422 case CLKMGR_MAINPLL_VCO0_PSRC_E_INTOSC:
423 clk_hz = cb_intosc_hz;
425 case CLKMGR_MAINPLL_VCO0_PSRC_F2S:
426 clk_hz = f2s_free_hz;
431 } else if (main0periph1 == 1) {
432 /* Check periph VCO clock source: eosc, intosc, f2s, mainpll */
433 switch (per_cfg->vco0_psrc) {
434 case CLKMGR_PERPLL_VCO0_PSRC_EOSC:
437 case CLKMGR_PERPLL_VCO0_PSRC_E_INTOSC:
438 clk_hz = cb_intosc_hz;
440 case CLKMGR_PERPLL_VCO0_PSRC_F2S:
441 clk_hz = f2s_free_hz;
443 case CLKMGR_PERPLL_VCO0_PSRC_MAIN:
444 clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg);
445 clk_hz /= main_cfg->cntr15clk_cnt;
454 return cm_calculate_numer(main_cfg, per_cfg, safe_hz, clk_hz);
457 /* ramping the main PLL to final value */
458 static void cm_pll_ramp_main(struct mainpll_cfg *main_cfg,
459 struct perpll_cfg *per_cfg,
460 unsigned int pll_ramp_main_hz)
462 unsigned int clk_hz = 0, clk_incr_hz = 0, clk_final_hz = 0;
464 /* find out the increment value */
465 if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_MAIN) {
466 clk_incr_hz = CLKMGR_PLL_RAMP_MPUCLK_INCREMENT_HZ;
467 clk_final_hz = cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg);
468 } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_MAIN) {
469 clk_incr_hz = CLKMGR_PLL_RAMP_NOCCLK_INCREMENT_HZ;
470 clk_final_hz = cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg);
473 /* execute the ramping here */
474 for (clk_hz = pll_ramp_main_hz + clk_incr_hz;
475 clk_hz < clk_final_hz; clk_hz += clk_incr_hz) {
476 writel((main_cfg->vco1_denom <<
477 CLKMGR_MAINPLL_VCO1_DENOM_LSB) |
478 cm_calc_safe_pll_numer(0, main_cfg, per_cfg, clk_hz),
479 &clock_manager_base->main_pll.vco1);
481 cm_wait_for_lock(LOCKED_MASK);
483 writel((main_cfg->vco1_denom << CLKMGR_MAINPLL_VCO1_DENOM_LSB) |
484 main_cfg->vco1_numer, &clock_manager_base->main_pll.vco1);
486 cm_wait_for_lock(LOCKED_MASK);
489 /* ramping the periph PLL to final value */
490 static void cm_pll_ramp_periph(struct mainpll_cfg *main_cfg,
491 struct perpll_cfg *per_cfg,
492 unsigned int pll_ramp_periph_hz)
494 unsigned int clk_hz = 0, clk_incr_hz = 0, clk_final_hz = 0;
496 /* find out the increment value */
497 if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_PERI) {
498 clk_incr_hz = CLKMGR_PLL_RAMP_MPUCLK_INCREMENT_HZ;
499 clk_final_hz = cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg);
500 } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_PERI) {
501 clk_incr_hz = CLKMGR_PLL_RAMP_NOCCLK_INCREMENT_HZ;
502 clk_final_hz = cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg);
504 /* execute the ramping here */
505 for (clk_hz = pll_ramp_periph_hz + clk_incr_hz;
506 clk_hz < clk_final_hz; clk_hz += clk_incr_hz) {
507 writel((per_cfg->vco1_denom << CLKMGR_PERPLL_VCO1_DENOM_LSB) |
508 cm_calc_safe_pll_numer(1, main_cfg, per_cfg, clk_hz),
509 &clock_manager_base->per_pll.vco1);
511 cm_wait_for_lock(LOCKED_MASK);
513 writel((per_cfg->vco1_denom << CLKMGR_PERPLL_VCO1_DENOM_LSB) |
514 per_cfg->vco1_numer, &clock_manager_base->per_pll.vco1);
516 cm_wait_for_lock(LOCKED_MASK);
520 * Setup clocks while making no assumptions of the
521 * previous state of the clocks.
523 * Start by being paranoid and gate all sw managed clocks
525 * Put all plls in bypass
527 * Put all plls VCO registers back to reset value (bgpwr dwn).
529 * Put peripheral and main pll src to reset value to avoid glitch.
533 * Deassert bg pwr dn and set numerator and denominator
537 * set internal dividers
539 * Wait for 7 us timer.
543 * Set external dividers while plls are locking
547 * Assert/deassert outreset all.
549 * Take all pll's out of bypass
553 * set source main and peripheral clocks
558 static int cm_full_cfg(struct mainpll_cfg *main_cfg, struct perpll_cfg *per_cfg)
560 unsigned int pll_ramp_main_hz = 0, pll_ramp_periph_hz = 0,
563 /* gate off all mainpll clock excpet HW managed clock */
564 writel(CLKMGR_MAINPLL_EN_S2FUSER0CLKEN_SET_MSK |
565 CLKMGR_MAINPLL_EN_HMCPLLREFCLKEN_SET_MSK,
566 &clock_manager_base->main_pll.enr);
568 /* now we can gate off the rest of the peripheral clocks */
569 writel(0, &clock_manager_base->per_pll.en);
571 /* Put all plls in external bypass */
572 writel(CLKMGR_MAINPLL_BYPASS_RESET,
573 &clock_manager_base->main_pll.bypasss);
574 writel(CLKMGR_PERPLL_BYPASS_RESET,
575 &clock_manager_base->per_pll.bypasss);
578 * Put all plls VCO registers back to reset value.
579 * Some code might have messed with them. At same time set the
580 * desired clock source
582 writel(CLKMGR_MAINPLL_VCO0_RESET |
583 CLKMGR_MAINPLL_VCO0_REGEXTSEL_SET_MSK |
584 (main_cfg->vco0_psrc << CLKMGR_MAINPLL_VCO0_PSRC_LSB),
585 &clock_manager_base->main_pll.vco0);
587 writel(CLKMGR_PERPLL_VCO0_RESET |
588 CLKMGR_PERPLL_VCO0_REGEXTSEL_SET_MSK |
589 (per_cfg->vco0_psrc << CLKMGR_PERPLL_VCO0_PSRC_LSB),
590 &clock_manager_base->per_pll.vco0);
592 writel(CLKMGR_MAINPLL_VCO1_RESET, &clock_manager_base->main_pll.vco1);
593 writel(CLKMGR_PERPLL_VCO1_RESET, &clock_manager_base->per_pll.vco1);
595 /* clear the interrupt register status register */
596 writel(CLKMGR_CLKMGR_INTR_MAINPLLLOST_SET_MSK |
597 CLKMGR_CLKMGR_INTR_PERPLLLOST_SET_MSK |
598 CLKMGR_CLKMGR_INTR_MAINPLLRFSLIP_SET_MSK |
599 CLKMGR_CLKMGR_INTR_PERPLLRFSLIP_SET_MSK |
600 CLKMGR_CLKMGR_INTR_MAINPLLFBSLIP_SET_MSK |
601 CLKMGR_CLKMGR_INTR_PERPLLFBSLIP_SET_MSK |
602 CLKMGR_CLKMGR_INTR_MAINPLLACHIEVED_SET_MSK |
603 CLKMGR_CLKMGR_INTR_PERPLLACHIEVED_SET_MSK,
604 &clock_manager_base->intr);
606 /* Program VCO Numerator and Denominator for main PLL */
607 ramp_required = cm_is_pll_ramp_required(0, main_cfg, per_cfg);
609 /* set main PLL to safe starting threshold frequency */
610 if (ramp_required == 1)
611 pll_ramp_main_hz = CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ;
612 else if (ramp_required == 2)
613 pll_ramp_main_hz = CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ;
615 writel((main_cfg->vco1_denom << CLKMGR_MAINPLL_VCO1_DENOM_LSB) |
616 cm_calc_safe_pll_numer(0, main_cfg, per_cfg,
618 &clock_manager_base->main_pll.vco1);
620 writel((main_cfg->vco1_denom << CLKMGR_MAINPLL_VCO1_DENOM_LSB) |
621 main_cfg->vco1_numer,
622 &clock_manager_base->main_pll.vco1);
624 /* Program VCO Numerator and Denominator for periph PLL */
625 ramp_required = cm_is_pll_ramp_required(1, main_cfg, per_cfg);
627 /* set periph PLL to safe starting threshold frequency */
628 if (ramp_required == 1)
630 CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ;
631 else if (ramp_required == 2)
633 CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ;
635 writel((per_cfg->vco1_denom << CLKMGR_PERPLL_VCO1_DENOM_LSB) |
636 cm_calc_safe_pll_numer(1, main_cfg, per_cfg,
638 &clock_manager_base->per_pll.vco1);
640 writel((per_cfg->vco1_denom << CLKMGR_PERPLL_VCO1_DENOM_LSB) |
642 &clock_manager_base->per_pll.vco1);
644 /* Wait for at least 5 us */
647 /* Now deassert BGPWRDN and PWRDN */
648 clrbits_le32(&clock_manager_base->main_pll.vco0,
649 CLKMGR_MAINPLL_VCO0_BGPWRDN_SET_MSK |
650 CLKMGR_MAINPLL_VCO0_PWRDN_SET_MSK);
651 clrbits_le32(&clock_manager_base->per_pll.vco0,
652 CLKMGR_PERPLL_VCO0_BGPWRDN_SET_MSK |
653 CLKMGR_PERPLL_VCO0_PWRDN_SET_MSK);
655 /* Wait for at least 7 us */
658 /* enable the VCO and disable the external regulator to PLL */
659 writel((readl(&clock_manager_base->main_pll.vco0) &
660 ~CLKMGR_MAINPLL_VCO0_REGEXTSEL_SET_MSK) |
661 CLKMGR_MAINPLL_VCO0_EN_SET_MSK,
662 &clock_manager_base->main_pll.vco0);
663 writel((readl(&clock_manager_base->per_pll.vco0) &
664 ~CLKMGR_PERPLL_VCO0_REGEXTSEL_SET_MSK) |
665 CLKMGR_PERPLL_VCO0_EN_SET_MSK,
666 &clock_manager_base->per_pll.vco0);
668 /* setup all the main PLL counter and clock source */
669 writel(main_cfg->nocclk,
670 SOCFPGA_CLKMGR_ADDRESS + CLKMGR_MAINPLL_NOC_CLK_OFFSET);
671 writel(main_cfg->mpuclk,
672 SOCFPGA_CLKMGR_ADDRESS + CLKMGR_ALTERAGRP_MPU_CLK_OFFSET);
674 /* main_emaca_clk divider */
675 writel(main_cfg->cntr2clk_cnt, &clock_manager_base->main_pll.cntr2clk);
676 /* main_emacb_clk divider */
677 writel(main_cfg->cntr3clk_cnt, &clock_manager_base->main_pll.cntr3clk);
678 /* main_emac_ptp_clk divider */
679 writel(main_cfg->cntr4clk_cnt, &clock_manager_base->main_pll.cntr4clk);
680 /* main_gpio_db_clk divider */
681 writel(main_cfg->cntr5clk_cnt, &clock_manager_base->main_pll.cntr5clk);
682 /* main_sdmmc_clk divider */
683 writel(main_cfg->cntr6clk_cnt, &clock_manager_base->main_pll.cntr6clk);
684 /* main_s2f_user0_clk divider */
685 writel(main_cfg->cntr7clk_cnt |
686 (main_cfg->cntr7clk_src << CLKMGR_MAINPLL_CNTR7CLK_SRC_LSB),
687 &clock_manager_base->main_pll.cntr7clk);
688 /* main_s2f_user1_clk divider */
689 writel(main_cfg->cntr8clk_cnt, &clock_manager_base->main_pll.cntr8clk);
690 /* main_hmc_pll_clk divider */
691 writel(main_cfg->cntr9clk_cnt |
692 (main_cfg->cntr9clk_src << CLKMGR_MAINPLL_CNTR9CLK_SRC_LSB),
693 &clock_manager_base->main_pll.cntr9clk);
694 /* main_periph_ref_clk divider */
695 writel(main_cfg->cntr15clk_cnt,
696 &clock_manager_base->main_pll.cntr15clk);
698 /* setup all the peripheral PLL counter and clock source */
699 /* peri_emaca_clk divider */
700 writel(per_cfg->cntr2clk_cnt |
701 (per_cfg->cntr2clk_src << CLKMGR_PERPLL_CNTR2CLK_SRC_LSB),
702 &clock_manager_base->per_pll.cntr2clk);
703 /* peri_emacb_clk divider */
704 writel(per_cfg->cntr3clk_cnt |
705 (per_cfg->cntr3clk_src << CLKMGR_PERPLL_CNTR3CLK_SRC_LSB),
706 &clock_manager_base->per_pll.cntr3clk);
707 /* peri_emac_ptp_clk divider */
708 writel(per_cfg->cntr4clk_cnt |
709 (per_cfg->cntr4clk_src << CLKMGR_PERPLL_CNTR4CLK_SRC_LSB),
710 &clock_manager_base->per_pll.cntr4clk);
711 /* peri_gpio_db_clk divider */
712 writel(per_cfg->cntr5clk_cnt |
713 (per_cfg->cntr5clk_src << CLKMGR_PERPLL_CNTR5CLK_SRC_LSB),
714 &clock_manager_base->per_pll.cntr5clk);
715 /* peri_sdmmc_clk divider */
716 writel(per_cfg->cntr6clk_cnt |
717 (per_cfg->cntr6clk_src << CLKMGR_PERPLL_CNTR6CLK_SRC_LSB),
718 &clock_manager_base->per_pll.cntr6clk);
719 /* peri_s2f_user0_clk divider */
720 writel(per_cfg->cntr7clk_cnt, &clock_manager_base->per_pll.cntr7clk);
721 /* peri_s2f_user1_clk divider */
722 writel(per_cfg->cntr8clk_cnt |
723 (per_cfg->cntr8clk_src << CLKMGR_PERPLL_CNTR8CLK_SRC_LSB),
724 &clock_manager_base->per_pll.cntr8clk);
725 /* peri_hmc_pll_clk divider */
726 writel(per_cfg->cntr9clk_cnt, &clock_manager_base->per_pll.cntr9clk);
728 /* setup all the external PLL counter */
729 /* mpu wrapper / external divider */
730 writel(main_cfg->mpuclk_cnt |
731 (main_cfg->mpuclk_src << CLKMGR_MAINPLL_MPUCLK_SRC_LSB),
732 &clock_manager_base->main_pll.mpuclk);
733 /* NOC wrapper / external divider */
734 writel(main_cfg->nocclk_cnt |
735 (main_cfg->nocclk_src << CLKMGR_MAINPLL_NOCCLK_SRC_LSB),
736 &clock_manager_base->main_pll.nocclk);
737 /* NOC subclock divider such as l4 */
738 writel(main_cfg->nocdiv_l4mainclk |
739 (main_cfg->nocdiv_l4mpclk <<
740 CLKMGR_MAINPLL_NOCDIV_L4MPCLK_LSB) |
741 (main_cfg->nocdiv_l4spclk <<
742 CLKMGR_MAINPLL_NOCDIV_L4SPCLK_LSB) |
743 (main_cfg->nocdiv_csatclk <<
744 CLKMGR_MAINPLL_NOCDIV_CSATCLK_LSB) |
745 (main_cfg->nocdiv_cstraceclk <<
746 CLKMGR_MAINPLL_NOCDIV_CSTRACECLK_LSB) |
747 (main_cfg->nocdiv_cspdbclk <<
748 CLKMGR_MAINPLL_NOCDIV_CSPDBGCLK_LSB),
749 &clock_manager_base->main_pll.nocdiv);
750 /* gpio_db external divider */
751 writel(per_cfg->gpiodiv_gpiodbclk,
752 &clock_manager_base->per_pll.gpiodiv);
754 /* setup the EMAC clock mux select */
755 writel((per_cfg->emacctl_emac0sel <<
756 CLKMGR_PERPLL_EMACCTL_EMAC0SEL_LSB) |
757 (per_cfg->emacctl_emac1sel <<
758 CLKMGR_PERPLL_EMACCTL_EMAC1SEL_LSB) |
759 (per_cfg->emacctl_emac2sel <<
760 CLKMGR_PERPLL_EMACCTL_EMAC2SEL_LSB),
761 &clock_manager_base->per_pll.emacctl);
763 /* at this stage, check for PLL lock status */
764 cm_wait_for_lock(LOCKED_MASK);
767 * after locking, but before taking out of bypass,
768 * assert/deassert outresetall
770 /* assert mainpll outresetall */
771 setbits_le32(&clock_manager_base->main_pll.vco0,
772 CLKMGR_MAINPLL_VCO0_OUTRSTALL_SET_MSK);
773 /* assert perpll outresetall */
774 setbits_le32(&clock_manager_base->per_pll.vco0,
775 CLKMGR_PERPLL_VCO0_OUTRSTALL_SET_MSK);
776 /* de-assert mainpll outresetall */
777 clrbits_le32(&clock_manager_base->main_pll.vco0,
778 CLKMGR_MAINPLL_VCO0_OUTRSTALL_SET_MSK);
779 /* de-assert perpll outresetall */
780 clrbits_le32(&clock_manager_base->per_pll.vco0,
781 CLKMGR_PERPLL_VCO0_OUTRSTALL_SET_MSK);
783 /* Take all PLLs out of bypass when boot mode is cleared. */
784 /* release mainpll from bypass */
785 writel(CLKMGR_MAINPLL_BYPASS_RESET,
786 &clock_manager_base->main_pll.bypassr);
787 /* wait till Clock Manager is not busy */
790 /* release perpll from bypass */
791 writel(CLKMGR_PERPLL_BYPASS_RESET,
792 &clock_manager_base->per_pll.bypassr);
793 /* wait till Clock Manager is not busy */
796 /* clear boot mode */
797 clrbits_le32(&clock_manager_base->ctrl,
798 CLKMGR_CLKMGR_CTL_BOOTMOD_SET_MSK);
799 /* wait till Clock Manager is not busy */
802 /* At here, we need to ramp to final value if needed */
803 if (pll_ramp_main_hz != 0)
804 cm_pll_ramp_main(main_cfg, per_cfg, pll_ramp_main_hz);
805 if (pll_ramp_periph_hz != 0)
806 cm_pll_ramp_periph(main_cfg, per_cfg, pll_ramp_periph_hz);
808 /* Now ungate non-hw-managed clocks */
809 writel(CLKMGR_MAINPLL_EN_S2FUSER0CLKEN_SET_MSK |
810 CLKMGR_MAINPLL_EN_HMCPLLREFCLKEN_SET_MSK,
811 &clock_manager_base->main_pll.ens);
812 writel(CLKMGR_PERPLL_EN_RESET, &clock_manager_base->per_pll.ens);
814 /* Clear the loss lock and slip bits as they might set during
815 clock reconfiguration */
816 writel(CLKMGR_CLKMGR_INTR_MAINPLLLOST_SET_MSK |
817 CLKMGR_CLKMGR_INTR_PERPLLLOST_SET_MSK |
818 CLKMGR_CLKMGR_INTR_MAINPLLRFSLIP_SET_MSK |
819 CLKMGR_CLKMGR_INTR_PERPLLRFSLIP_SET_MSK |
820 CLKMGR_CLKMGR_INTR_MAINPLLFBSLIP_SET_MSK |
821 CLKMGR_CLKMGR_INTR_PERPLLFBSLIP_SET_MSK,
822 &clock_manager_base->intr);
827 void cm_use_intosc(void)
829 setbits_le32(&clock_manager_base->ctrl,
830 CLKMGR_CLKMGR_CTL_BOOTCLK_INTOSC_SET_MSK);
833 unsigned int cm_get_noc_clk_hz(void)
835 unsigned int clk_src, divisor, nocclk, src_hz;
837 nocclk = readl(&clock_manager_base->main_pll.nocclk);
838 clk_src = (nocclk >> CLKMGR_MAINPLL_NOCCLK_SRC_LSB) &
839 CLKMGR_MAINPLL_NOCCLK_SRC_MSK;
841 divisor = 1 + (nocclk & CLKMGR_MAINPLL_NOCDIV_MSK);
843 if (clk_src == CLKMGR_PERPLLGRP_SRC_MAIN) {
844 src_hz = cm_get_main_vco_clk_hz();
846 (readl(SOCFPGA_CLKMGR_ADDRESS + CLKMGR_MAINPLL_NOC_CLK_OFFSET) &
847 CLKMGR_MAINPLL_NOCCLK_CNT_MSK);
848 } else if (clk_src == CLKMGR_PERPLLGRP_SRC_PERI) {
849 src_hz = cm_get_per_vco_clk_hz();
851 ((readl(SOCFPGA_CLKMGR_ADDRESS +
852 CLKMGR_MAINPLL_NOC_CLK_OFFSET) >>
853 CLKMGR_MAINPLL_NOCCLK_PERICNT_LSB) &
854 CLKMGR_MAINPLL_NOCCLK_CNT_MSK);
855 } else if (clk_src == CLKMGR_PERPLLGRP_SRC_OSC1) {
857 } else if (clk_src == CLKMGR_PERPLLGRP_SRC_INTOSC) {
858 src_hz = cb_intosc_hz;
859 } else if (clk_src == CLKMGR_PERPLLGRP_SRC_FPGA) {
860 src_hz = f2s_free_hz;
865 return src_hz / divisor;
868 unsigned int cm_get_l4_noc_hz(unsigned int nocdivshift)
870 unsigned int divisor2 = 1 <<
871 ((readl(&clock_manager_base->main_pll.nocdiv) >>
872 nocdivshift) & CLKMGR_MAINPLL_NOCDIV_MSK);
874 return cm_get_noc_clk_hz() / divisor2;
877 int cm_basic_init(const void *blob)
879 struct mainpll_cfg main_cfg;
880 struct perpll_cfg per_cfg;
881 struct alteragrp_cfg altrgrp_cfg;
884 /* initialize to zero for use case of optional node */
885 memset(&main_cfg, 0, sizeof(main_cfg));
886 memset(&per_cfg, 0, sizeof(per_cfg));
887 memset(&altrgrp_cfg, 0, sizeof(altrgrp_cfg));
889 rval = of_get_clk_cfg(blob, &main_cfg, &per_cfg, &altrgrp_cfg);
893 rval = cm_full_cfg(&main_cfg, &per_cfg);
896 cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4MAINCLK_LSB);
898 cm_l4_mp_clk_hz = cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4MPCLK_LSB);
900 cm_l4_sp_clk_hz = cm_get_l4_sp_clk_hz();
902 cm_l4_sys_free_clk_hz = cm_get_noc_clk_hz() / 4;
907 unsigned long cm_get_mpu_clk_hz(void)
910 u32 clk_src, mainmpuclk_reg;
912 mainmpuclk_reg = readl(&clock_manager_base->main_pll.mpuclk);
914 clk_src = (mainmpuclk_reg >> CLKMGR_MAINPLL_MPUCLK_SRC_LSB) &
915 CLKMGR_MAINPLL_MPUCLK_SRC_MSK;
917 reg = readl(&clock_manager_base->altera.mpuclk);
918 /* Check MPU clock source: main, periph, osc1, intosc or f2s? */
920 case CLKMGR_MAINPLL_MPUCLK_SRC_MAIN:
921 clk_hz = cm_get_main_vco_clk_hz();
922 clk_hz /= (reg & CLKMGR_MAINPLL_MPUCLK_CNT_MSK) + 1;
924 case CLKMGR_MAINPLL_MPUCLK_SRC_PERI:
925 clk_hz = cm_get_per_vco_clk_hz();
926 clk_hz /= (((reg >> CLKMGR_MAINPLL_MPUCLK_PERICNT_LSB) &
927 CLKMGR_MAINPLL_MPUCLK_CNT_MSK) + 1);
929 case CLKMGR_MAINPLL_MPUCLK_SRC_OSC1:
932 case CLKMGR_MAINPLL_MPUCLK_SRC_INTOSC:
933 clk_hz = cb_intosc_hz;
935 case CLKMGR_MAINPLL_MPUCLK_SRC_FPGA:
936 clk_hz = f2s_free_hz;
939 printf("cm_get_mpu_clk_hz invalid clk_src %d\n", clk_src);
943 clk_hz /= (mainmpuclk_reg & CLKMGR_MAINPLL_MPUCLK_CNT_MSK) + 1;
948 unsigned int cm_get_per_vco_clk_hz(void)
956 clk_src = readl(&clock_manager_base->per_pll.vco0);
958 clk_src = (clk_src >> CLKMGR_PERPLL_VCO0_PSRC_LSB) &
959 CLKMGR_PERPLL_VCO0_PSRC_MSK;
961 if (clk_src == CLKMGR_PERPLL_VCO0_PSRC_EOSC) {
963 } else if (clk_src == CLKMGR_PERPLL_VCO0_PSRC_E_INTOSC) {
964 src_hz = cb_intosc_hz;
965 } else if (clk_src == CLKMGR_PERPLL_VCO0_PSRC_F2S) {
966 src_hz = f2s_free_hz;
967 } else if (clk_src == CLKMGR_PERPLL_VCO0_PSRC_MAIN) {
968 src_hz = cm_get_main_vco_clk_hz();
969 src_hz /= (readl(&clock_manager_base->main_pll.cntr15clk) &
970 CLKMGR_MAINPLL_CNTRCLK_MSK) + 1;
972 printf("cm_get_per_vco_clk_hz invalid clk_src %d\n", clk_src);
976 vco = readl(&clock_manager_base->per_pll.vco1);
978 numer = vco & CLKMGR_PERPLL_VCO1_NUMER_MSK;
980 denom = (vco >> CLKMGR_PERPLL_VCO1_DENOM_LSB) &
981 CLKMGR_PERPLL_VCO1_DENOM_MSK;
990 unsigned int cm_get_main_vco_clk_hz(void)
992 u32 src_hz, numer, denom, vco;
994 u32 clk_src = readl(&clock_manager_base->main_pll.vco0);
996 clk_src = (clk_src >> CLKMGR_MAINPLL_VCO0_PSRC_LSB) &
997 CLKMGR_MAINPLL_VCO0_PSRC_MSK;
999 if (clk_src == CLKMGR_MAINPLL_VCO0_PSRC_EOSC) {
1001 } else if (clk_src == CLKMGR_MAINPLL_VCO0_PSRC_E_INTOSC) {
1002 src_hz = cb_intosc_hz;
1003 } else if (clk_src == CLKMGR_MAINPLL_VCO0_PSRC_F2S) {
1004 src_hz = f2s_free_hz;
1006 printf("cm_get_main_vco_clk_hz invalid clk_src %d\n", clk_src);
1010 vco = readl(&clock_manager_base->main_pll.vco1);
1012 numer = vco & CLKMGR_MAINPLL_VCO1_NUMER_MSK;
1014 denom = (vco >> CLKMGR_MAINPLL_VCO1_DENOM_LSB) &
1015 CLKMGR_MAINPLL_VCO1_DENOM_MSK;
1024 unsigned int cm_get_l4_sp_clk_hz(void)
1026 return cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4SPCLK_LSB);
1029 unsigned int cm_get_mmc_controller_clk_hz(void)
1034 clk_input = readl(&clock_manager_base->per_pll.cntr6clk);
1035 clk_input = (clk_input >> CLKMGR_PERPLL_CNTR6CLK_SRC_LSB) &
1036 CLKMGR_PERPLLGRP_SRC_MSK;
1038 switch (clk_input) {
1039 case CLKMGR_PERPLLGRP_SRC_MAIN:
1040 clk_hz = cm_get_main_vco_clk_hz();
1041 clk_hz /= 1 + (readl(&clock_manager_base->main_pll.cntr6clk) &
1042 CLKMGR_MAINPLL_CNTRCLK_MSK);
1045 case CLKMGR_PERPLLGRP_SRC_PERI:
1046 clk_hz = cm_get_per_vco_clk_hz();
1047 clk_hz /= 1 + (readl(&clock_manager_base->per_pll.cntr6clk) &
1048 CLKMGR_PERPLL_CNTRCLK_MSK);
1051 case CLKMGR_PERPLLGRP_SRC_OSC1:
1055 case CLKMGR_PERPLLGRP_SRC_INTOSC:
1056 clk_hz = cb_intosc_hz;
1059 case CLKMGR_PERPLLGRP_SRC_FPGA:
1060 clk_hz = f2s_free_hz;
1067 unsigned int cm_get_spi_controller_clk_hz(void)
1069 return cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4MPCLK_LSB);
1072 unsigned int cm_get_qspi_controller_clk_hz(void)
1074 return cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4MAINCLK_LSB);
1077 /* Override weak dw_spi_get_clk implementation in designware_spi.c driver */
1078 int dw_spi_get_clk(struct udevice *bus, ulong *rate)
1080 *rate = cm_get_spi_controller_clk_hz();
1085 void cm_print_clock_quick_summary(void)
1087 printf("MPU %10ld kHz\n", cm_get_mpu_clk_hz() / 1000);
1088 printf("MMC %8d kHz\n", cm_get_mmc_controller_clk_hz() / 1000);
1089 printf("QSPI %8d kHz\n", cm_get_qspi_controller_clk_hz() / 1000);
1090 printf("SPI %8d kHz\n", cm_get_spi_controller_clk_hz() / 1000);
1091 printf("EOSC1 %8d kHz\n", eosc1_hz / 1000);
1092 printf("cb_intosc %8d kHz\n", cb_intosc_hz / 1000);
1093 printf("f2s_free %8d kHz\n", f2s_free_hz / 1000);
1094 printf("Main VCO %8d kHz\n", cm_get_main_vco_clk_hz() / 1000);
1095 printf("NOC %8d kHz\n", cm_get_noc_clk_hz() / 1000);
1096 printf("L4 Main %8d kHz\n",
1097 cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4MAINCLK_LSB) / 1000);
1098 printf("L4 MP %8d kHz\n",
1099 cm_get_l4_noc_hz(CLKMGR_MAINPLL_NOCDIV_L4MPCLK_LSB) / 1000);
1100 printf("L4 SP %8d kHz\n", cm_get_l4_sp_clk_hz() / 1000);
1101 printf("L4 sys free %8d kHz\n", cm_l4_sys_free_clk_hz / 1000);