blob: 0a2c84c9e1316a3cd1660780150a282ea7ab7ae0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ley Foon Tanec6f8822017-04-26 02:44:33 +08002/*
3 * Copyright (C) 2013-2017 Altera Corporation <www.altera.com>
Ley Foon Tanec6f8822017-04-26 02:44:33 +08004 */
5
Simon Glass495a5dc2019-11-14 12:57:30 -07006#include <time.h>
Ley Foon Tanec6f8822017-04-26 02:44:33 +08007#include <asm/io.h>
Eugeniy Paltsev74739322017-12-28 15:09:02 +03008#include <dm.h>
Ley Foon Tanec6f8822017-04-26 02:44:33 +08009#include <asm/arch/clock_manager.h>
10#include <wait_bit.h>
11
Ley Foon Tanec6f8822017-04-26 02:44:33 +080012/*
13 * function to write the bypass register which requires a poll of the
14 * busy bit
15 */
16static void cm_write_bypass(u32 val)
17{
Ley Foon Tan26695912019-11-08 10:38:21 +080018 writel(val, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_BYPASS);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080019 cm_wait_for_fsm();
20}
21
22/* function to write the ctrl register which requires a poll of the busy bit */
23static void cm_write_ctrl(u32 val)
24{
Ley Foon Tan26695912019-11-08 10:38:21 +080025 writel(val, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_CTRL);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080026 cm_wait_for_fsm();
27}
28
29/* function to write a clock register that has phase information */
Tom Rini1c478c22018-01-26 11:24:04 -050030static int cm_write_with_phase(u32 value, const void *reg_address, u32 mask)
Ley Foon Tanec6f8822017-04-26 02:44:33 +080031{
32 int ret;
33
34 /* poll until phase is zero */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010035 ret = wait_for_bit_le32(reg_address, mask, false, 20000, false);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080036 if (ret)
37 return ret;
38
39 writel(value, reg_address);
40
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010041 return wait_for_bit_le32(reg_address, mask, false, 20000, false);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080042}
43
44/*
45 * Setup clocks while making no assumptions about previous state of the clocks.
46 *
47 * Start by being paranoid and gate all sw managed clocks
48 * Put all plls in bypass
49 * Put all plls VCO registers back to reset value (bandgap power down).
50 * Put peripheral and main pll src to reset value to avoid glitch.
51 * Delay 5 us.
52 * Deassert bandgap power down and set numerator and denominator
53 * Start 7 us timer.
54 * set internal dividers
55 * Wait for 7 us timer.
56 * Enable plls
57 * Set external dividers while plls are locking
58 * Wait for pll lock
59 * Assert/deassert outreset all.
60 * Take all pll's out of bypass
61 * Clear safe mode
62 * set source main and peripheral clocks
63 * Ungate clocks
64 */
65
66int cm_basic_init(const struct cm_config * const cfg)
67{
68 unsigned long end;
69 int ret;
70
71 /* Start by being paranoid and gate all sw managed clocks */
72
73 /*
74 * We need to disable nandclk
75 * and then do another apb access before disabling
76 * gatting off the rest of the periperal clocks.
77 */
78 writel(~CLKMGR_PERPLLGRP_EN_NANDCLK_MASK &
Ley Foon Tan26695912019-11-08 10:38:21 +080079 readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_EN),
80 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_EN);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080081
82 /* DO NOT GATE OFF DEBUG CLOCKS & BRIDGE CLOCKS */
83 writel(CLKMGR_MAINPLLGRP_EN_DBGTIMERCLK_MASK |
84 CLKMGR_MAINPLLGRP_EN_DBGTRACECLK_MASK |
85 CLKMGR_MAINPLLGRP_EN_DBGCLK_MASK |
86 CLKMGR_MAINPLLGRP_EN_DBGATCLK_MASK |
87 CLKMGR_MAINPLLGRP_EN_S2FUSER0CLK_MASK |
88 CLKMGR_MAINPLLGRP_EN_L4MPCLK_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +080089 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_EN);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080090
Ley Foon Tan26695912019-11-08 10:38:21 +080091 writel(0, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_EN);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080092
93 /* now we can gate off the rest of the peripheral clocks */
Ley Foon Tan26695912019-11-08 10:38:21 +080094 writel(0, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_EN);
Ley Foon Tanec6f8822017-04-26 02:44:33 +080095
96 /* Put all plls in bypass */
97 cm_write_bypass(CLKMGR_BYPASS_PERPLL | CLKMGR_BYPASS_SDRPLL |
98 CLKMGR_BYPASS_MAINPLL);
99
100 /* Put all plls VCO registers back to reset value. */
101 writel(CLKMGR_MAINPLLGRP_VCO_RESET_VALUE &
102 ~CLKMGR_MAINPLLGRP_VCO_REGEXTSEL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800103 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800104 writel(CLKMGR_PERPLLGRP_VCO_RESET_VALUE &
105 ~CLKMGR_PERPLLGRP_VCO_REGEXTSEL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800106 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800107 writel(CLKMGR_SDRPLLGRP_VCO_RESET_VALUE &
108 ~CLKMGR_SDRPLLGRP_VCO_REGEXTSEL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800109 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800110
111 /*
112 * The clocks to the flash devices and the L4_MAIN clocks can
113 * glitch when coming out of safe mode if their source values
114 * are different from their reset value. So the trick it to
115 * put them back to their reset state, and change input
116 * after exiting safe mode but before ungating the clocks.
117 */
118 writel(CLKMGR_PERPLLGRP_SRC_RESET_VALUE,
Ley Foon Tan26695912019-11-08 10:38:21 +0800119 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_SRC);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800120 writel(CLKMGR_MAINPLLGRP_L4SRC_RESET_VALUE,
Ley Foon Tan26695912019-11-08 10:38:21 +0800121 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_L4SRC);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800122
123 /* read back for the required 5 us delay. */
Ley Foon Tan26695912019-11-08 10:38:21 +0800124 readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
125 readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
126 readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800127
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800128 /*
129 * We made sure bgpwr down was assert for 5 us. Now deassert BG PWR DN
130 * with numerator and denominator.
131 */
Ley Foon Tan26695912019-11-08 10:38:21 +0800132 writel(cfg->main_vco_base,
133 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
134 writel(cfg->peri_vco_base,
135 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
136 writel(cfg->sdram_vco_base,
137 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800138
139 /*
140 * Time starts here. Must wait 7 us from
141 * BGPWRDN_SET(0) to VCO_ENABLE_SET(1).
142 */
143 end = timer_get_us() + 7;
144
145 /* main mpu */
Ley Foon Tan26695912019-11-08 10:38:21 +0800146 writel(cfg->mpuclk,
147 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_MPUCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800148
149 /* altera group mpuclk */
Ley Foon Tan26695912019-11-08 10:38:21 +0800150 writel(cfg->altera_grp_mpuclk,
151 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_ALTR_MPUCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800152
153 /* main main clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800154 writel(cfg->mainclk,
155 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_MAINCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800156
157 /* main for dbg */
Ley Foon Tan26695912019-11-08 10:38:21 +0800158 writel(cfg->dbgatclk,
159 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_DBGATCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800160
161 /* main for cfgs2fuser0clk */
162 writel(cfg->cfg2fuser0clk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800163 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_CFGS2FUSER0CLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800164
165 /* Peri emac0 50 MHz default to RMII */
Ley Foon Tan26695912019-11-08 10:38:21 +0800166 writel(cfg->emac0clk,
167 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_EMAC0CLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800168
169 /* Peri emac1 50 MHz default to RMII */
Ley Foon Tan26695912019-11-08 10:38:21 +0800170 writel(cfg->emac1clk,
171 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_EMAC1CLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800172
173 /* Peri QSPI */
Ley Foon Tan26695912019-11-08 10:38:21 +0800174 writel(cfg->mainqspiclk,
175 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_MAINQSPICLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800176
Ley Foon Tan26695912019-11-08 10:38:21 +0800177 writel(cfg->perqspiclk,
178 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_PERQSPICLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800179
180 /* Peri pernandsdmmcclk */
181 writel(cfg->mainnandsdmmcclk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800182 socfpga_get_clkmgr_addr() +
183 CLKMGR_GEN5_MAINPLL_MAINNANDSDMMCCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800184
185 writel(cfg->pernandsdmmcclk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800186 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_PERNANDSDMMCCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800187
188 /* Peri perbaseclk */
Ley Foon Tan26695912019-11-08 10:38:21 +0800189 writel(cfg->perbaseclk,
190 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_PERBASECLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800191
192 /* Peri s2fuser1clk */
Ley Foon Tan26695912019-11-08 10:38:21 +0800193 writel(cfg->s2fuser1clk,
194 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_S2FUSER1CLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800195
196 /* 7 us must have elapsed before we can enable the VCO */
197 while (timer_get_us() < end)
198 ;
199
200 /* Enable vco */
201 /* main pll vco */
202 writel(cfg->main_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
Ley Foon Tan26695912019-11-08 10:38:21 +0800203 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800204
205 /* periferal pll */
206 writel(cfg->peri_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
Ley Foon Tan26695912019-11-08 10:38:21 +0800207 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800208
209 /* sdram pll vco */
210 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
Ley Foon Tan26695912019-11-08 10:38:21 +0800211 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800212
213 /* L3 MP and L3 SP */
Ley Foon Tan26695912019-11-08 10:38:21 +0800214 writel(cfg->maindiv,
215 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_MAINDIV);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800216
Ley Foon Tan26695912019-11-08 10:38:21 +0800217 writel(cfg->dbgdiv,
218 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_DBGDIV);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800219
Ley Foon Tan26695912019-11-08 10:38:21 +0800220 writel(cfg->tracediv,
221 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_TRACEDIV);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800222
223 /* L4 MP, L4 SP, can0, and can1 */
Ley Foon Tan26695912019-11-08 10:38:21 +0800224 writel(cfg->perdiv,
225 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_DIV);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800226
Ley Foon Tan26695912019-11-08 10:38:21 +0800227 writel(cfg->gpiodiv,
228 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_GPIODIV);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800229
230 cm_wait_for_lock(LOCKED_MASK);
231
232 /* write the sdram clock counters before toggling outreset all */
233 writel(cfg->ddrdqsclk & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800234 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_DDRDQSCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800235
236 writel(cfg->ddr2xdqsclk & CLKMGR_SDRPLLGRP_DDR2XDQSCLK_CNT_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800237 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_DDR2XDQSCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800238
239 writel(cfg->ddrdqclk & CLKMGR_SDRPLLGRP_DDRDQCLK_CNT_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800240 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_DDRDQCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800241
242 writel(cfg->s2fuser2clk & CLKMGR_SDRPLLGRP_S2FUSER2CLK_CNT_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800243 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_S2FUSER2CLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800244
245 /*
246 * after locking, but before taking out of bypass
247 * assert/deassert outresetall
248 */
Ley Foon Tan26695912019-11-08 10:38:21 +0800249 u32 mainvco = readl(socfpga_get_clkmgr_addr() +
250 CLKMGR_GEN5_MAINPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800251
252 /* assert main outresetall */
253 writel(mainvco | CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800254 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800255
Ley Foon Tan26695912019-11-08 10:38:21 +0800256 u32 periphvco = readl(socfpga_get_clkmgr_addr() +
257 CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800258
259 /* assert pheriph outresetall */
260 writel(periphvco | CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800261 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800262
263 /* assert sdram outresetall */
Ley Foon Tan26695912019-11-08 10:38:21 +0800264 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN |
265 CLKMGR_SDRPLLGRP_VCO_OUTRESETALL,
266 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800267
268 /* deassert main outresetall */
269 writel(mainvco & ~CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800270 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800271
272 /* deassert pheriph outresetall */
273 writel(periphvco & ~CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
Ley Foon Tan26695912019-11-08 10:38:21 +0800274 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800275
276 /* deassert sdram outresetall */
277 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
Ley Foon Tan26695912019-11-08 10:38:21 +0800278 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800279
280 /*
281 * now that we've toggled outreset all, all the clocks
282 * are aligned nicely; so we can change any phase.
283 */
284 ret = cm_write_with_phase(cfg->ddrdqsclk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800285 (const void *)(socfpga_get_clkmgr_addr() +
286 CLKMGR_GEN5_SDRPLL_DDRDQSCLK),
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800287 CLKMGR_SDRPLLGRP_DDRDQSCLK_PHASE_MASK);
288 if (ret)
289 return ret;
290
291 /* SDRAM DDR2XDQSCLK */
292 ret = cm_write_with_phase(cfg->ddr2xdqsclk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800293 (const void *)(socfpga_get_clkmgr_addr() +
294 CLKMGR_GEN5_SDRPLL_DDR2XDQSCLK),
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800295 CLKMGR_SDRPLLGRP_DDR2XDQSCLK_PHASE_MASK);
296 if (ret)
297 return ret;
298
299 ret = cm_write_with_phase(cfg->ddrdqclk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800300 (const void *)(socfpga_get_clkmgr_addr() +
301 CLKMGR_GEN5_SDRPLL_DDRDQCLK),
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800302 CLKMGR_SDRPLLGRP_DDRDQCLK_PHASE_MASK);
303 if (ret)
304 return ret;
305
306 ret = cm_write_with_phase(cfg->s2fuser2clk,
Ley Foon Tan26695912019-11-08 10:38:21 +0800307 (const void *)(socfpga_get_clkmgr_addr() +
308 CLKMGR_GEN5_SDRPLL_S2FUSER2CLK),
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800309 CLKMGR_SDRPLLGRP_S2FUSER2CLK_PHASE_MASK);
310 if (ret)
311 return ret;
312
313 /* Take all three PLLs out of bypass when safe mode is cleared. */
314 cm_write_bypass(0);
315
316 /* clear safe mode */
Ley Foon Tan26695912019-11-08 10:38:21 +0800317 cm_write_ctrl(readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_CTRL) |
318 CLKMGR_CTRL_SAFEMODE);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800319
320 /*
321 * now that safe mode is clear with clocks gated
322 * it safe to change the source mux for the flashes the the L4_MAIN
323 */
Ley Foon Tan26695912019-11-08 10:38:21 +0800324 writel(cfg->persrc,
325 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_SRC);
326 writel(cfg->l4src,
327 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_L4SRC);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800328
329 /* Now ungate non-hw-managed clocks */
Ley Foon Tan26695912019-11-08 10:38:21 +0800330 writel(~0, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_EN);
331 writel(~0, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_EN);
332 writel(~0, socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_EN);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800333
334 /* Clear the loss of lock bits (write 1 to clear) */
Ley Foon Tan26695912019-11-08 10:38:21 +0800335 writel(CLKMGR_INTER_SDRPLLLOST_MASK |
336 CLKMGR_INTER_PERPLLLOST_MASK |
337 CLKMGR_INTER_MAINPLLLOST_MASK,
338 socfpga_get_clkmgr_addr() + CLKMGR_GEN5_INTER);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800339
340 return 0;
341}
342
343static unsigned int cm_get_main_vco_clk_hz(void)
344{
345 u32 reg, clock;
346
347 /* get the main VCO clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800348 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800349 clock = cm_get_osc_clk_hz(1);
350 clock /= ((reg & CLKMGR_MAINPLLGRP_VCO_DENOM_MASK) >>
351 CLKMGR_MAINPLLGRP_VCO_DENOM_OFFSET) + 1;
352 clock *= ((reg & CLKMGR_MAINPLLGRP_VCO_NUMER_MASK) >>
353 CLKMGR_MAINPLLGRP_VCO_NUMER_OFFSET) + 1;
354
355 return clock;
356}
357
358static unsigned int cm_get_per_vco_clk_hz(void)
359{
360 u32 reg, clock = 0;
361
362 /* identify PER PLL clock source */
Ley Foon Tan26695912019-11-08 10:38:21 +0800363 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800364 reg = (reg & CLKMGR_PERPLLGRP_VCO_SSRC_MASK) >>
365 CLKMGR_PERPLLGRP_VCO_SSRC_OFFSET;
366 if (reg == CLKMGR_VCO_SSRC_EOSC1)
367 clock = cm_get_osc_clk_hz(1);
368 else if (reg == CLKMGR_VCO_SSRC_EOSC2)
369 clock = cm_get_osc_clk_hz(2);
370 else if (reg == CLKMGR_VCO_SSRC_F2S)
371 clock = cm_get_f2s_per_ref_clk_hz();
372
373 /* get the PER VCO clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800374 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800375 clock /= ((reg & CLKMGR_PERPLLGRP_VCO_DENOM_MASK) >>
376 CLKMGR_PERPLLGRP_VCO_DENOM_OFFSET) + 1;
377 clock *= ((reg & CLKMGR_PERPLLGRP_VCO_NUMER_MASK) >>
378 CLKMGR_PERPLLGRP_VCO_NUMER_OFFSET) + 1;
379
380 return clock;
381}
382
383unsigned long cm_get_mpu_clk_hz(void)
384{
385 u32 reg, clock;
386
387 clock = cm_get_main_vco_clk_hz();
388
389 /* get the MPU clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800390 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_ALTR_MPUCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800391 clock /= (reg + 1);
Ley Foon Tan26695912019-11-08 10:38:21 +0800392 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_MPUCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800393 clock /= (reg + 1);
394 return clock;
395}
396
397unsigned long cm_get_sdram_clk_hz(void)
398{
399 u32 reg, clock = 0;
400
401 /* identify SDRAM PLL clock source */
Ley Foon Tan26695912019-11-08 10:38:21 +0800402 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800403 reg = (reg & CLKMGR_SDRPLLGRP_VCO_SSRC_MASK) >>
404 CLKMGR_SDRPLLGRP_VCO_SSRC_OFFSET;
405 if (reg == CLKMGR_VCO_SSRC_EOSC1)
406 clock = cm_get_osc_clk_hz(1);
407 else if (reg == CLKMGR_VCO_SSRC_EOSC2)
408 clock = cm_get_osc_clk_hz(2);
409 else if (reg == CLKMGR_VCO_SSRC_F2S)
410 clock = cm_get_f2s_sdr_ref_clk_hz();
411
412 /* get the SDRAM VCO clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800413 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_VCO);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800414 clock /= ((reg & CLKMGR_SDRPLLGRP_VCO_DENOM_MASK) >>
415 CLKMGR_SDRPLLGRP_VCO_DENOM_OFFSET) + 1;
416 clock *= ((reg & CLKMGR_SDRPLLGRP_VCO_NUMER_MASK) >>
417 CLKMGR_SDRPLLGRP_VCO_NUMER_OFFSET) + 1;
418
419 /* get the SDRAM (DDR_DQS) clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800420 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_SDRPLL_DDRDQSCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800421 reg = (reg & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK) >>
422 CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_OFFSET;
423 clock /= (reg + 1);
424
425 return clock;
426}
427
428unsigned int cm_get_l4_sp_clk_hz(void)
429{
430 u32 reg, clock = 0;
431
432 /* identify the source of L4 SP clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800433 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_L4SRC);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800434 reg = (reg & CLKMGR_MAINPLLGRP_L4SRC_L4SP) >>
435 CLKMGR_MAINPLLGRP_L4SRC_L4SP_OFFSET;
436
437 if (reg == CLKMGR_L4_SP_CLK_SRC_MAINPLL) {
438 clock = cm_get_main_vco_clk_hz();
439
440 /* get the clock prior L4 SP divider (main clk) */
Ley Foon Tan26695912019-11-08 10:38:21 +0800441 reg = readl(socfpga_get_clkmgr_addr() +
442 CLKMGR_GEN5_ALTR_MAINCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800443 clock /= (reg + 1);
Ley Foon Tan26695912019-11-08 10:38:21 +0800444 reg = readl(socfpga_get_clkmgr_addr() +
445 CLKMGR_GEN5_MAINPLL_MAINCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800446 clock /= (reg + 1);
447 } else if (reg == CLKMGR_L4_SP_CLK_SRC_PERPLL) {
448 clock = cm_get_per_vco_clk_hz();
449
450 /* get the clock prior L4 SP divider (periph_base_clk) */
Ley Foon Tan26695912019-11-08 10:38:21 +0800451 reg = readl(socfpga_get_clkmgr_addr() +
452 CLKMGR_GEN5_PERPLL_PERBASECLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800453 clock /= (reg + 1);
454 }
455
456 /* get the L4 SP clock which supplied to UART */
Ley Foon Tan26695912019-11-08 10:38:21 +0800457 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_MAINPLL_MAINDIV);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800458 reg = (reg & CLKMGR_MAINPLLGRP_MAINDIV_L4SPCLK_MASK) >>
459 CLKMGR_MAINPLLGRP_MAINDIV_L4SPCLK_OFFSET;
460 clock = clock / (1 << reg);
461
462 return clock;
463}
464
465unsigned int cm_get_mmc_controller_clk_hz(void)
466{
467 u32 reg, clock = 0;
468
469 /* identify the source of MMC clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800470 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_SRC);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800471 reg = (reg & CLKMGR_PERPLLGRP_SRC_SDMMC_MASK) >>
472 CLKMGR_PERPLLGRP_SRC_SDMMC_OFFSET;
473
474 if (reg == CLKMGR_SDMMC_CLK_SRC_F2S) {
475 clock = cm_get_f2s_per_ref_clk_hz();
476 } else if (reg == CLKMGR_SDMMC_CLK_SRC_MAIN) {
477 clock = cm_get_main_vco_clk_hz();
478
479 /* get the SDMMC clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800480 reg = readl(socfpga_get_clkmgr_addr() +
481 CLKMGR_GEN5_MAINPLL_MAINNANDSDMMCCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800482 clock /= (reg + 1);
483 } else if (reg == CLKMGR_SDMMC_CLK_SRC_PER) {
484 clock = cm_get_per_vco_clk_hz();
485
486 /* get the SDMMC clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800487 reg = readl(socfpga_get_clkmgr_addr() +
488 CLKMGR_GEN5_PERPLL_PERNANDSDMMCCLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800489 clock /= (reg + 1);
490 }
491
492 /* further divide by 4 as we have fixed divider at wrapper */
493 clock /= 4;
494 return clock;
495}
496
497unsigned int cm_get_qspi_controller_clk_hz(void)
498{
499 u32 reg, clock = 0;
500
501 /* identify the source of QSPI clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800502 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_SRC);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800503 reg = (reg & CLKMGR_PERPLLGRP_SRC_QSPI_MASK) >>
504 CLKMGR_PERPLLGRP_SRC_QSPI_OFFSET;
505
506 if (reg == CLKMGR_QSPI_CLK_SRC_F2S) {
507 clock = cm_get_f2s_per_ref_clk_hz();
508 } else if (reg == CLKMGR_QSPI_CLK_SRC_MAIN) {
509 clock = cm_get_main_vco_clk_hz();
510
511 /* get the qspi clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800512 reg = readl(socfpga_get_clkmgr_addr() +
513 CLKMGR_GEN5_MAINPLL_MAINQSPICLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800514 clock /= (reg + 1);
515 } else if (reg == CLKMGR_QSPI_CLK_SRC_PER) {
516 clock = cm_get_per_vco_clk_hz();
517
518 /* get the qspi clock */
Ley Foon Tan26695912019-11-08 10:38:21 +0800519 reg = readl(socfpga_get_clkmgr_addr() +
520 CLKMGR_GEN5_PERPLL_PERQSPICLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800521 clock /= (reg + 1);
522 }
523
524 return clock;
525}
526
527unsigned int cm_get_spi_controller_clk_hz(void)
528{
529 u32 reg, clock = 0;
530
531 clock = cm_get_per_vco_clk_hz();
532
533 /* get the clock prior L4 SP divider (periph_base_clk) */
Ley Foon Tan26695912019-11-08 10:38:21 +0800534 reg = readl(socfpga_get_clkmgr_addr() + CLKMGR_GEN5_PERPLL_PERBASECLK);
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800535 clock /= (reg + 1);
536
537 return clock;
538}
539
Eugeniy Paltsev74739322017-12-28 15:09:02 +0300540/* Override weak dw_spi_get_clk implementation in designware_spi.c driver */
541int dw_spi_get_clk(struct udevice *bus, ulong *rate)
542{
543 *rate = cm_get_spi_controller_clk_hz();
544
545 return 0;
546}
547
Ley Foon Tanec6f8822017-04-26 02:44:33 +0800548void cm_print_clock_quick_summary(void)
549{
550 printf("MPU %10ld kHz\n", cm_get_mpu_clk_hz() / 1000);
551 printf("DDR %10ld kHz\n", cm_get_sdram_clk_hz() / 1000);
552 printf("EOSC1 %8d kHz\n", cm_get_osc_clk_hz(1) / 1000);
553 printf("EOSC2 %8d kHz\n", cm_get_osc_clk_hz(2) / 1000);
554 printf("F2S_SDR_REF %8d kHz\n", cm_get_f2s_sdr_ref_clk_hz() / 1000);
555 printf("F2S_PER_REF %8d kHz\n", cm_get_f2s_per_ref_clk_hz() / 1000);
556 printf("MMC %8d kHz\n", cm_get_mmc_controller_clk_hz() / 1000);
557 printf("QSPI %8d kHz\n", cm_get_qspi_controller_clk_hz() / 1000);
558 printf("UART %8d kHz\n", cm_get_l4_sp_clk_hz() / 1000);
559 printf("SPI %8d kHz\n", cm_get_spi_controller_clk_hz() / 1000);
560}