blob: 160f6e73ca9e3f97cc2b242cb3cc0619fcd4a208 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chin Liang Seecb350602014-03-04 22:13:53 -06002/*
Ley Foon Tanec6f8822017-04-26 02:44:33 +08003 * Copyright (C) 2013-2017 Altera Corporation <www.altera.com>
Chin Liang Seecb350602014-03-04 22:13:53 -06004 */
5
Siew Chin Limfa2cc492021-03-24 17:16:49 +08006#include <asm/arch/clock_manager.h>
7#include <asm/arch/system_manager.h>
8#include <asm/global_data.h>
9#include <asm/io.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Ley Foon Tanec6f8822017-04-26 02:44:33 +080012#include <wait_bit.h>
Chin Liang Seecb350602014-03-04 22:13:53 -060013
Pavel Machek7c8d5a62014-09-08 14:08:45 +020014DECLARE_GLOBAL_DATA_PTR;
15
Ley Foon Tanec6f8822017-04-26 02:44:33 +080016void cm_wait_for_lock(u32 mask)
Chin Liang Seecb350602014-03-04 22:13:53 -060017{
Ley Foon Tanec6f8822017-04-26 02:44:33 +080018 u32 inter_val;
19 u32 retry = 0;
Chin Liang Seecb350602014-03-04 22:13:53 -060020 do {
Ley Foon Tanca40f292017-04-26 02:44:39 +080021#if defined(CONFIG_TARGET_SOCFPGA_GEN5)
Ley Foon Tan26695912019-11-08 10:38:21 +080022 inter_val = readl(socfpga_get_clkmgr_addr() +
23 CLKMGR_INTER) & mask;
Ley Foon Tan6751e7d2018-05-18 22:05:22 +080024#else
Ley Foon Tan26695912019-11-08 10:38:21 +080025 inter_val = readl(socfpga_get_clkmgr_addr() +
26 CLKMGR_STAT) & mask;
Ley Foon Tanca40f292017-04-26 02:44:39 +080027#endif
28 /* Wait for stable lock */
Marek Vasut43e9c402014-09-16 19:54:32 +020029 if (inter_val == mask)
30 retry++;
31 else
32 retry = 0;
33 if (retry >= 10)
34 break;
35 } while (1);
Chin Liang Seecb350602014-03-04 22:13:53 -060036}
37
38/* function to poll in the fsm busy bit */
Ley Foon Tanec6f8822017-04-26 02:44:33 +080039int cm_wait_for_fsm(void)
Pavel Machek7c8d5a62014-09-08 14:08:45 +020040{
Ley Foon Tan26695912019-11-08 10:38:21 +080041 return wait_for_bit_le32((const void *)(socfpga_get_clkmgr_addr() +
42 CLKMGR_STAT), CLKMGR_STAT_BUSY, false, 20000,
43 false);
Pavel Machek7c8d5a62014-09-08 14:08:45 +020044}
45
46int set_cpu_clk_info(void)
47{
Marek Vasutd430d9a2018-08-06 21:47:50 +020048#if defined(CONFIG_TARGET_SOCFPGA_GEN5)
Pavel Machek7c8d5a62014-09-08 14:08:45 +020049 /* Calculate the clock frequencies required for drivers */
50 cm_get_l4_sp_clk_hz();
51 cm_get_mmc_controller_clk_hz();
Marek Vasutd430d9a2018-08-06 21:47:50 +020052#endif
Pavel Machek7c8d5a62014-09-08 14:08:45 +020053
54 gd->bd->bi_arm_freq = cm_get_mpu_clk_hz() / 1000000;
55 gd->bd->bi_dsp_freq = 0;
Ley Foon Tanca40f292017-04-26 02:44:39 +080056
57#if defined(CONFIG_TARGET_SOCFPGA_GEN5)
Pavel Machek7c8d5a62014-09-08 14:08:45 +020058 gd->bd->bi_ddr_freq = cm_get_sdram_clk_hz() / 1000000;
Ley Foon Tan6751e7d2018-05-18 22:05:22 +080059#else
Ley Foon Tanca40f292017-04-26 02:44:39 +080060 gd->bd->bi_ddr_freq = 0;
61#endif
Pavel Machek7c8d5a62014-09-08 14:08:45 +020062
63 return 0;
64}
65
Siew Chin Limfa2cc492021-03-24 17:16:49 +080066#if IS_ENABLED(CONFIG_TARGET_SOCFPGA_SOC64)
Siew Chin Limc1888b02021-03-24 17:16:50 +080067int cm_set_qspi_controller_clk_hz(u32 clk_hz)
68{
69 u32 reg;
70 u32 clk_khz;
71
72 /*
73 * Store QSPI ref clock and set into sysmgr boot register.
74 * Only clock freq in kHz degree is accepted due to limited bits[27:0]
75 * is reserved for storing the QSPI clock freq into boot scratch cold0
76 * register.
77 */
78 if (clk_hz < 1000)
79 return -EINVAL;
80
81 clk_khz = clk_hz / 1000;
82 printf("QSPI: Reference clock at %d kHz\n", clk_khz);
83
84 reg = (readl(socfpga_get_sysmgr_addr() +
85 SYSMGR_SOC64_BOOT_SCRATCH_COLD0)) &
86 ~(SYSMGR_SCRATCH_REG_0_QSPI_REFCLK_MASK);
87
88 writel((clk_khz & SYSMGR_SCRATCH_REG_0_QSPI_REFCLK_MASK) | reg,
89 socfpga_get_sysmgr_addr() + SYSMGR_SOC64_BOOT_SCRATCH_COLD0);
90
91 return 0;
92}
93
Siew Chin Limfa2cc492021-03-24 17:16:49 +080094unsigned int cm_get_qspi_controller_clk_hz(void)
95{
Siew Chin Limc1888b02021-03-24 17:16:50 +080096 return (readl(socfpga_get_sysmgr_addr() +
97 SYSMGR_SOC64_BOOT_SCRATCH_COLD0) &
98 SYSMGR_SCRATCH_REG_0_QSPI_REFCLK_MASK) * 1000;
Siew Chin Limfa2cc492021-03-24 17:16:49 +080099}
100#endif
101
Tom Rinidf09a192017-12-22 12:19:22 -0500102#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600103static int do_showclocks(struct cmd_tbl *cmdtp, int flag, int argc,
104 char *const argv[])
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200105{
106 cm_print_clock_quick_summary();
107 return 0;
108}
109
110U_BOOT_CMD(
111 clocks, CONFIG_SYS_MAXARGS, 1, do_showclocks,
112 "display clocks",
113 ""
114);
Tom Rinidf09a192017-12-22 12:19:22 -0500115#endif