blob: d869f47c88caab3aa3c06c48732f7f6cff5e646b [file] [log] [blame]
Chin Liang Seecb350602014-03-04 22:13:53 -06001/*
2 * Copyright (C) 2013 Altera Corporation <www.altera.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/clock_manager.h>
10
Pavel Machek7c8d5a62014-09-08 14:08:45 +020011DECLARE_GLOBAL_DATA_PTR;
12
Chin Liang Seecb350602014-03-04 22:13:53 -060013static const struct socfpga_clock_manager *clock_manager_base =
Pavel Machek7c8d5a62014-09-08 14:08:45 +020014 (struct socfpga_clock_manager *)SOCFPGA_CLKMGR_ADDRESS;
Chin Liang Seecb350602014-03-04 22:13:53 -060015
Marek Vasute5353fd2014-09-08 14:08:45 +020016static void cm_wait_for_lock(uint32_t mask)
Chin Liang Seecb350602014-03-04 22:13:53 -060017{
18 register uint32_t inter_val;
Marek Vasut43e9c402014-09-16 19:54:32 +020019 uint32_t retry = 0;
Chin Liang Seecb350602014-03-04 22:13:53 -060020 do {
21 inter_val = readl(&clock_manager_base->inter) & mask;
Marek Vasut43e9c402014-09-16 19:54:32 +020022 if (inter_val == mask)
23 retry++;
24 else
25 retry = 0;
26 if (retry >= 10)
27 break;
28 } while (1);
Chin Liang Seecb350602014-03-04 22:13:53 -060029}
30
31/* function to poll in the fsm busy bit */
Marek Vasute5353fd2014-09-08 14:08:45 +020032static void cm_wait_for_fsm(void)
Chin Liang Seecb350602014-03-04 22:13:53 -060033{
34 while (readl(&clock_manager_base->stat) & CLKMGR_STAT_BUSY)
35 ;
36}
37
38/*
39 * function to write the bypass register which requires a poll of the
40 * busy bit
41 */
Marek Vasute5353fd2014-09-08 14:08:45 +020042static void cm_write_bypass(uint32_t val)
Chin Liang Seecb350602014-03-04 22:13:53 -060043{
44 writel(val, &clock_manager_base->bypass);
45 cm_wait_for_fsm();
46}
47
48/* function to write the ctrl register which requires a poll of the busy bit */
Marek Vasute5353fd2014-09-08 14:08:45 +020049static void cm_write_ctrl(uint32_t val)
Chin Liang Seecb350602014-03-04 22:13:53 -060050{
51 writel(val, &clock_manager_base->ctrl);
52 cm_wait_for_fsm();
53}
54
55/* function to write a clock register that has phase information */
Marek Vasute5353fd2014-09-08 14:08:45 +020056static void cm_write_with_phase(uint32_t value,
57 uint32_t reg_address, uint32_t mask)
Chin Liang Seecb350602014-03-04 22:13:53 -060058{
59 /* poll until phase is zero */
60 while (readl(reg_address) & mask)
61 ;
62
63 writel(value, reg_address);
64
65 while (readl(reg_address) & mask)
66 ;
67}
68
69/*
70 * Setup clocks while making no assumptions about previous state of the clocks.
71 *
72 * Start by being paranoid and gate all sw managed clocks
73 * Put all plls in bypass
74 * Put all plls VCO registers back to reset value (bandgap power down).
75 * Put peripheral and main pll src to reset value to avoid glitch.
76 * Delay 5 us.
77 * Deassert bandgap power down and set numerator and denominator
78 * Start 7 us timer.
79 * set internal dividers
80 * Wait for 7 us timer.
81 * Enable plls
82 * Set external dividers while plls are locking
83 * Wait for pll lock
84 * Assert/deassert outreset all.
85 * Take all pll's out of bypass
86 * Clear safe mode
87 * set source main and peripheral clocks
88 * Ungate clocks
89 */
90
91void cm_basic_init(const cm_config_t *cfg)
92{
93 uint32_t start, timeout;
94
95 /* Start by being paranoid and gate all sw managed clocks */
96
97 /*
98 * We need to disable nandclk
99 * and then do another apb access before disabling
100 * gatting off the rest of the periperal clocks.
101 */
102 writel(~CLKMGR_PERPLLGRP_EN_NANDCLK_MASK &
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200103 readl(&clock_manager_base->per_pll.en),
104 &clock_manager_base->per_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600105
106 /* DO NOT GATE OFF DEBUG CLOCKS & BRIDGE CLOCKS */
107 writel(CLKMGR_MAINPLLGRP_EN_DBGTIMERCLK_MASK |
108 CLKMGR_MAINPLLGRP_EN_DBGTRACECLK_MASK |
109 CLKMGR_MAINPLLGRP_EN_DBGCLK_MASK |
110 CLKMGR_MAINPLLGRP_EN_DBGATCLK_MASK |
111 CLKMGR_MAINPLLGRP_EN_S2FUSER0CLK_MASK |
112 CLKMGR_MAINPLLGRP_EN_L4MPCLK_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200113 &clock_manager_base->main_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600114
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200115 writel(0, &clock_manager_base->sdr_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600116
117 /* now we can gate off the rest of the peripheral clocks */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200118 writel(0, &clock_manager_base->per_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600119
120 /* Put all plls in bypass */
Marek Vasute0098b372014-09-16 17:21:00 +0200121 cm_write_bypass(CLKMGR_BYPASS_PERPLL | CLKMGR_BYPASS_SDRPLL |
122 CLKMGR_BYPASS_MAINPLL);
Chin Liang Seecb350602014-03-04 22:13:53 -0600123
Marek Vasut43e9c402014-09-16 19:54:32 +0200124 /* Put all plls VCO registers back to reset value. */
125 writel(CLKMGR_MAINPLLGRP_VCO_RESET_VALUE &
126 ~CLKMGR_MAINPLLGRP_VCO_REGEXTSEL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200127 &clock_manager_base->main_pll.vco);
Marek Vasut43e9c402014-09-16 19:54:32 +0200128 writel(CLKMGR_PERPLLGRP_VCO_RESET_VALUE &
129 ~CLKMGR_PERPLLGRP_VCO_REGEXTSEL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200130 &clock_manager_base->per_pll.vco);
Marek Vasut43e9c402014-09-16 19:54:32 +0200131 writel(CLKMGR_SDRPLLGRP_VCO_RESET_VALUE &
132 ~CLKMGR_SDRPLLGRP_VCO_REGEXTSEL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200133 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600134
135 /*
136 * The clocks to the flash devices and the L4_MAIN clocks can
137 * glitch when coming out of safe mode if their source values
138 * are different from their reset value. So the trick it to
139 * put them back to their reset state, and change input
140 * after exiting safe mode but before ungating the clocks.
141 */
142 writel(CLKMGR_PERPLLGRP_SRC_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200143 &clock_manager_base->per_pll.src);
Chin Liang Seecb350602014-03-04 22:13:53 -0600144 writel(CLKMGR_MAINPLLGRP_L4SRC_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200145 &clock_manager_base->main_pll.l4src);
Chin Liang Seecb350602014-03-04 22:13:53 -0600146
147 /* read back for the required 5 us delay. */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200148 readl(&clock_manager_base->main_pll.vco);
149 readl(&clock_manager_base->per_pll.vco);
150 readl(&clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600151
152
153 /*
154 * We made sure bgpwr down was assert for 5 us. Now deassert BG PWR DN
155 * with numerator and denominator.
156 */
Marek Vasut43e9c402014-09-16 19:54:32 +0200157 writel(cfg->main_vco_base, &clock_manager_base->main_pll.vco);
158 writel(cfg->peri_vco_base, &clock_manager_base->per_pll.vco);
159 writel(cfg->sdram_vco_base, &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600160
161 /*
162 * Time starts here
163 * must wait 7 us from BGPWRDN_SET(0) to VCO_ENABLE_SET(1)
164 */
Chin Liang Seecb350602014-03-04 22:13:53 -0600165 start = get_timer(0);
166 /* timeout in unit of us as CONFIG_SYS_HZ = 1000*1000 */
167 timeout = 7;
168
169 /* main mpu */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200170 writel(cfg->mpuclk, &clock_manager_base->main_pll.mpuclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600171
172 /* main main clock */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200173 writel(cfg->mainclk, &clock_manager_base->main_pll.mainclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600174
175 /* main for dbg */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200176 writel(cfg->dbgatclk, &clock_manager_base->main_pll.dbgatclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600177
178 /* main for cfgs2fuser0clk */
179 writel(cfg->cfg2fuser0clk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200180 &clock_manager_base->main_pll.cfgs2fuser0clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600181
182 /* Peri emac0 50 MHz default to RMII */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200183 writel(cfg->emac0clk, &clock_manager_base->per_pll.emac0clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600184
185 /* Peri emac1 50 MHz default to RMII */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200186 writel(cfg->emac1clk, &clock_manager_base->per_pll.emac1clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600187
188 /* Peri QSPI */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200189 writel(cfg->mainqspiclk, &clock_manager_base->main_pll.mainqspiclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600190
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200191 writel(cfg->perqspiclk, &clock_manager_base->per_pll.perqspiclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600192
193 /* Peri pernandsdmmcclk */
Marek Vasut43e9c402014-09-16 19:54:32 +0200194 writel(cfg->mainnandsdmmcclk,
195 &clock_manager_base->main_pll.mainnandsdmmcclk);
196
Chin Liang Seecb350602014-03-04 22:13:53 -0600197 writel(cfg->pernandsdmmcclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200198 &clock_manager_base->per_pll.pernandsdmmcclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600199
200 /* Peri perbaseclk */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200201 writel(cfg->perbaseclk, &clock_manager_base->per_pll.perbaseclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600202
203 /* Peri s2fuser1clk */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200204 writel(cfg->s2fuser1clk, &clock_manager_base->per_pll.s2fuser1clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600205
206 /* 7 us must have elapsed before we can enable the VCO */
207 while (get_timer(start) < timeout)
208 ;
209
210 /* Enable vco */
211 /* main pll vco */
Marek Vasute0098b372014-09-16 17:21:00 +0200212 writel(cfg->main_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200213 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600214
215 /* periferal pll */
Marek Vasute0098b372014-09-16 17:21:00 +0200216 writel(cfg->peri_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200217 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600218
219 /* sdram pll vco */
Marek Vasute0098b372014-09-16 17:21:00 +0200220 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
221 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600222
223 /* L3 MP and L3 SP */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200224 writel(cfg->maindiv, &clock_manager_base->main_pll.maindiv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600225
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200226 writel(cfg->dbgdiv, &clock_manager_base->main_pll.dbgdiv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600227
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200228 writel(cfg->tracediv, &clock_manager_base->main_pll.tracediv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600229
230 /* L4 MP, L4 SP, can0, and can1 */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200231 writel(cfg->perdiv, &clock_manager_base->per_pll.div);
Chin Liang Seecb350602014-03-04 22:13:53 -0600232
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200233 writel(cfg->gpiodiv, &clock_manager_base->per_pll.gpiodiv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600234
235#define LOCKED_MASK \
236 (CLKMGR_INTER_SDRPLLLOCKED_MASK | \
237 CLKMGR_INTER_PERPLLLOCKED_MASK | \
238 CLKMGR_INTER_MAINPLLLOCKED_MASK)
239
240 cm_wait_for_lock(LOCKED_MASK);
241
242 /* write the sdram clock counters before toggling outreset all */
243 writel(cfg->ddrdqsclk & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200244 &clock_manager_base->sdr_pll.ddrdqsclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600245
246 writel(cfg->ddr2xdqsclk & CLKMGR_SDRPLLGRP_DDR2XDQSCLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200247 &clock_manager_base->sdr_pll.ddr2xdqsclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600248
249 writel(cfg->ddrdqclk & CLKMGR_SDRPLLGRP_DDRDQCLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200250 &clock_manager_base->sdr_pll.ddrdqclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600251
252 writel(cfg->s2fuser2clk & CLKMGR_SDRPLLGRP_S2FUSER2CLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200253 &clock_manager_base->sdr_pll.s2fuser2clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600254
255 /*
256 * after locking, but before taking out of bypass
257 * assert/deassert outresetall
258 */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200259 uint32_t mainvco = readl(&clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600260
261 /* assert main outresetall */
262 writel(mainvco | CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200263 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600264
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200265 uint32_t periphvco = readl(&clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600266
267 /* assert pheriph outresetall */
268 writel(periphvco | CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200269 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600270
271 /* assert sdram outresetall */
Marek Vasute0098b372014-09-16 17:21:00 +0200272 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN|
273 CLKMGR_SDRPLLGRP_VCO_OUTRESETALL,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200274 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600275
276 /* deassert main outresetall */
277 writel(mainvco & ~CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200278 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600279
280 /* deassert pheriph outresetall */
281 writel(periphvco & ~CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200282 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600283
284 /* deassert sdram outresetall */
Marek Vasute0098b372014-09-16 17:21:00 +0200285 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
286 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600287
288 /*
289 * now that we've toggled outreset all, all the clocks
290 * are aligned nicely; so we can change any phase.
291 */
292 cm_write_with_phase(cfg->ddrdqsclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200293 (uint32_t)&clock_manager_base->sdr_pll.ddrdqsclk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600294 CLKMGR_SDRPLLGRP_DDRDQSCLK_PHASE_MASK);
295
296 /* SDRAM DDR2XDQSCLK */
297 cm_write_with_phase(cfg->ddr2xdqsclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200298 (uint32_t)&clock_manager_base->sdr_pll.ddr2xdqsclk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600299 CLKMGR_SDRPLLGRP_DDR2XDQSCLK_PHASE_MASK);
300
301 cm_write_with_phase(cfg->ddrdqclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200302 (uint32_t)&clock_manager_base->sdr_pll.ddrdqclk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600303 CLKMGR_SDRPLLGRP_DDRDQCLK_PHASE_MASK);
304
305 cm_write_with_phase(cfg->s2fuser2clk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200306 (uint32_t)&clock_manager_base->sdr_pll.s2fuser2clk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600307 CLKMGR_SDRPLLGRP_S2FUSER2CLK_PHASE_MASK);
308
309 /* Take all three PLLs out of bypass when safe mode is cleared. */
Marek Vasute0098b372014-09-16 17:21:00 +0200310 cm_write_bypass(0);
Chin Liang Seecb350602014-03-04 22:13:53 -0600311
312 /* clear safe mode */
Marek Vasute0098b372014-09-16 17:21:00 +0200313 cm_write_ctrl(readl(&clock_manager_base->ctrl) | CLKMGR_CTRL_SAFEMODE);
Chin Liang Seecb350602014-03-04 22:13:53 -0600314
315 /*
316 * now that safe mode is clear with clocks gated
317 * it safe to change the source mux for the flashes the the L4_MAIN
318 */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200319 writel(cfg->persrc, &clock_manager_base->per_pll.src);
320 writel(cfg->l4src, &clock_manager_base->main_pll.l4src);
Chin Liang Seecb350602014-03-04 22:13:53 -0600321
322 /* Now ungate non-hw-managed clocks */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200323 writel(~0, &clock_manager_base->main_pll.en);
324 writel(~0, &clock_manager_base->per_pll.en);
325 writel(~0, &clock_manager_base->sdr_pll.en);
Marek Vasut43e9c402014-09-16 19:54:32 +0200326
327 /* Clear the loss of lock bits (write 1 to clear) */
328 writel(CLKMGR_INTER_SDRPLLLOST_MASK | CLKMGR_INTER_PERPLLLOST_MASK |
329 CLKMGR_INTER_MAINPLLLOST_MASK,
330 &clock_manager_base->inter);
Chin Liang Seecb350602014-03-04 22:13:53 -0600331}
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200332
Marek Vasut42780af2014-09-13 08:27:16 +0200333static unsigned int cm_get_main_vco_clk_hz(void)
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200334{
335 uint32_t reg, clock;
336
337 /* get the main VCO clock */
338 reg = readl(&clock_manager_base->main_pll.vco);
Marek Vasute0098b372014-09-16 17:21:00 +0200339 clock = CONFIG_HPS_CLK_OSC1_HZ;
340 clock /= ((reg & CLKMGR_MAINPLLGRP_VCO_DENOM_MASK) >>
341 CLKMGR_MAINPLLGRP_VCO_DENOM_OFFSET) + 1;
342 clock *= ((reg & CLKMGR_MAINPLLGRP_VCO_NUMER_MASK) >>
343 CLKMGR_MAINPLLGRP_VCO_NUMER_OFFSET) + 1;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200344
Marek Vasut42780af2014-09-13 08:27:16 +0200345 return clock;
346}
347
348static unsigned int cm_get_per_vco_clk_hz(void)
349{
350 uint32_t reg, clock = 0;
351
352 /* identify PER PLL clock source */
353 reg = readl(&clock_manager_base->per_pll.vco);
Marek Vasute0098b372014-09-16 17:21:00 +0200354 reg = (reg & CLKMGR_PERPLLGRP_VCO_SSRC_MASK) >>
355 CLKMGR_PERPLLGRP_VCO_SSRC_OFFSET;
Marek Vasut42780af2014-09-13 08:27:16 +0200356 if (reg == CLKMGR_VCO_SSRC_EOSC1)
357 clock = CONFIG_HPS_CLK_OSC1_HZ;
358 else if (reg == CLKMGR_VCO_SSRC_EOSC2)
359 clock = CONFIG_HPS_CLK_OSC2_HZ;
360 else if (reg == CLKMGR_VCO_SSRC_F2S)
361 clock = CONFIG_HPS_CLK_F2S_PER_REF_HZ;
362
363 /* get the PER VCO clock */
364 reg = readl(&clock_manager_base->per_pll.vco);
Marek Vasute0098b372014-09-16 17:21:00 +0200365 clock /= ((reg & CLKMGR_PERPLLGRP_VCO_DENOM_MASK) >>
366 CLKMGR_PERPLLGRP_VCO_DENOM_OFFSET) + 1;
367 clock *= ((reg & CLKMGR_PERPLLGRP_VCO_NUMER_MASK) >>
368 CLKMGR_PERPLLGRP_VCO_NUMER_OFFSET) + 1;
Marek Vasut42780af2014-09-13 08:27:16 +0200369
370 return clock;
371}
372
373unsigned long cm_get_mpu_clk_hz(void)
374{
375 uint32_t reg, clock;
376
377 clock = cm_get_main_vco_clk_hz();
378
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200379 /* get the MPU clock */
380 reg = readl(&clock_manager_base->altera.mpuclk);
381 clock /= (reg + 1);
382 reg = readl(&clock_manager_base->main_pll.mpuclk);
383 clock /= (reg + 1);
384 return clock;
385}
386
387unsigned long cm_get_sdram_clk_hz(void)
388{
389 uint32_t reg, clock = 0;
390
391 /* identify SDRAM PLL clock source */
392 reg = readl(&clock_manager_base->sdr_pll.vco);
Marek Vasute0098b372014-09-16 17:21:00 +0200393 reg = (reg & CLKMGR_SDRPLLGRP_VCO_SSRC_MASK) >>
394 CLKMGR_SDRPLLGRP_VCO_SSRC_OFFSET;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200395 if (reg == CLKMGR_VCO_SSRC_EOSC1)
396 clock = CONFIG_HPS_CLK_OSC1_HZ;
397 else if (reg == CLKMGR_VCO_SSRC_EOSC2)
398 clock = CONFIG_HPS_CLK_OSC2_HZ;
399 else if (reg == CLKMGR_VCO_SSRC_F2S)
400 clock = CONFIG_HPS_CLK_F2S_SDR_REF_HZ;
401
402 /* get the SDRAM VCO clock */
403 reg = readl(&clock_manager_base->sdr_pll.vco);
Marek Vasute0098b372014-09-16 17:21:00 +0200404 clock /= ((reg & CLKMGR_SDRPLLGRP_VCO_DENOM_MASK) >>
405 CLKMGR_SDRPLLGRP_VCO_DENOM_OFFSET) + 1;
406 clock *= ((reg & CLKMGR_SDRPLLGRP_VCO_NUMER_MASK) >>
407 CLKMGR_SDRPLLGRP_VCO_NUMER_OFFSET) + 1;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200408
409 /* get the SDRAM (DDR_DQS) clock */
410 reg = readl(&clock_manager_base->sdr_pll.ddrdqsclk);
Marek Vasute0098b372014-09-16 17:21:00 +0200411 reg = (reg & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK) >>
412 CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_OFFSET;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200413 clock /= (reg + 1);
414
415 return clock;
416}
417
418unsigned int cm_get_l4_sp_clk_hz(void)
419{
420 uint32_t reg, clock = 0;
421
422 /* identify the source of L4 SP clock */
423 reg = readl(&clock_manager_base->main_pll.l4src);
Marek Vasute0098b372014-09-16 17:21:00 +0200424 reg = (reg & CLKMGR_MAINPLLGRP_L4SRC_L4SP) >>
425 CLKMGR_MAINPLLGRP_L4SRC_L4SP_OFFSET;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200426
427 if (reg == CLKMGR_L4_SP_CLK_SRC_MAINPLL) {
Marek Vasut42780af2014-09-13 08:27:16 +0200428 clock = cm_get_main_vco_clk_hz();
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200429
430 /* get the clock prior L4 SP divider (main clk) */
431 reg = readl(&clock_manager_base->altera.mainclk);
432 clock /= (reg + 1);
433 reg = readl(&clock_manager_base->main_pll.mainclk);
434 clock /= (reg + 1);
435 } else if (reg == CLKMGR_L4_SP_CLK_SRC_PERPLL) {
Marek Vasut42780af2014-09-13 08:27:16 +0200436 clock = cm_get_per_vco_clk_hz();
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200437
438 /* get the clock prior L4 SP divider (periph_base_clk) */
439 reg = readl(&clock_manager_base->per_pll.perbaseclk);
440 clock /= (reg + 1);
441 }
442
443 /* get the L4 SP clock which supplied to UART */
444 reg = readl(&clock_manager_base->main_pll.maindiv);
Marek Vasute0098b372014-09-16 17:21:00 +0200445 reg = (reg & CLKMGR_MAINPLLGRP_MAINDIV_L4SPCLK_MASK) >>
446 CLKMGR_MAINPLLGRP_MAINDIV_L4SPCLK_OFFSET;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200447 clock = clock / (1 << reg);
448
449 return clock;
450}
451
452unsigned int cm_get_mmc_controller_clk_hz(void)
453{
454 uint32_t reg, clock = 0;
455
456 /* identify the source of MMC clock */
457 reg = readl(&clock_manager_base->per_pll.src);
Marek Vasute0098b372014-09-16 17:21:00 +0200458 reg = (reg & CLKMGR_PERPLLGRP_SRC_SDMMC_MASK) >>
459 CLKMGR_PERPLLGRP_SRC_SDMMC_OFFSET;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200460
461 if (reg == CLKMGR_SDMMC_CLK_SRC_F2S) {
462 clock = CONFIG_HPS_CLK_F2S_PER_REF_HZ;
463 } else if (reg == CLKMGR_SDMMC_CLK_SRC_MAIN) {
Marek Vasut42780af2014-09-13 08:27:16 +0200464 clock = cm_get_main_vco_clk_hz();
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200465
466 /* get the SDMMC clock */
467 reg = readl(&clock_manager_base->main_pll.mainnandsdmmcclk);
468 clock /= (reg + 1);
469 } else if (reg == CLKMGR_SDMMC_CLK_SRC_PER) {
Marek Vasut42780af2014-09-13 08:27:16 +0200470 clock = cm_get_per_vco_clk_hz();
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200471
472 /* get the SDMMC clock */
473 reg = readl(&clock_manager_base->per_pll.pernandsdmmcclk);
474 clock /= (reg + 1);
475 }
476
477 /* further divide by 4 as we have fixed divider at wrapper */
478 clock /= 4;
479 return clock;
480}
481
482unsigned int cm_get_qspi_controller_clk_hz(void)
483{
484 uint32_t reg, clock = 0;
485
486 /* identify the source of QSPI clock */
487 reg = readl(&clock_manager_base->per_pll.src);
Marek Vasute0098b372014-09-16 17:21:00 +0200488 reg = (reg & CLKMGR_PERPLLGRP_SRC_QSPI_MASK) >>
489 CLKMGR_PERPLLGRP_SRC_QSPI_OFFSET;
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200490
491 if (reg == CLKMGR_QSPI_CLK_SRC_F2S) {
492 clock = CONFIG_HPS_CLK_F2S_PER_REF_HZ;
493 } else if (reg == CLKMGR_QSPI_CLK_SRC_MAIN) {
Marek Vasut42780af2014-09-13 08:27:16 +0200494 clock = cm_get_main_vco_clk_hz();
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200495
496 /* get the qspi clock */
497 reg = readl(&clock_manager_base->main_pll.mainqspiclk);
498 clock /= (reg + 1);
499 } else if (reg == CLKMGR_QSPI_CLK_SRC_PER) {
Marek Vasut42780af2014-09-13 08:27:16 +0200500 clock = cm_get_per_vco_clk_hz();
Pavel Machek7c8d5a62014-09-08 14:08:45 +0200501
502 /* get the qspi clock */
503 reg = readl(&clock_manager_base->per_pll.perqspiclk);
504 clock /= (reg + 1);
505 }
506
507 return clock;
508}
509
510static void cm_print_clock_quick_summary(void)
511{
512 printf("MPU %10ld kHz\n", cm_get_mpu_clk_hz() / 1000);
513 printf("DDR %10ld kHz\n", cm_get_sdram_clk_hz() / 1000);
514 printf("EOSC1 %8d kHz\n", CONFIG_HPS_CLK_OSC1_HZ / 1000);
515 printf("EOSC2 %8d kHz\n", CONFIG_HPS_CLK_OSC2_HZ / 1000);
516 printf("F2S_SDR_REF %8d kHz\n", CONFIG_HPS_CLK_F2S_SDR_REF_HZ / 1000);
517 printf("F2S_PER_REF %8d kHz\n", CONFIG_HPS_CLK_F2S_PER_REF_HZ / 1000);
518 printf("MMC %8d kHz\n", cm_get_mmc_controller_clk_hz() / 1000);
519 printf("QSPI %8d kHz\n", cm_get_qspi_controller_clk_hz() / 1000);
520 printf("UART %8d kHz\n", cm_get_l4_sp_clk_hz() / 1000);
521}
522
523int set_cpu_clk_info(void)
524{
525 /* Calculate the clock frequencies required for drivers */
526 cm_get_l4_sp_clk_hz();
527 cm_get_mmc_controller_clk_hz();
528
529 gd->bd->bi_arm_freq = cm_get_mpu_clk_hz() / 1000000;
530 gd->bd->bi_dsp_freq = 0;
531 gd->bd->bi_ddr_freq = cm_get_sdram_clk_hz() / 1000000;
532
533 return 0;
534}
535
536int do_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
537{
538 cm_print_clock_quick_summary();
539 return 0;
540}
541
542U_BOOT_CMD(
543 clocks, CONFIG_SYS_MAXARGS, 1, do_showclocks,
544 "display clocks",
545 ""
546);