blob: 0228ac8c3b5e36db6c7e4a88a816252455b149c7 [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
11static const struct socfpga_clock_manager *clock_manager_base =
12 (void *)SOCFPGA_CLKMGR_ADDRESS;
13
14#define CLKMGR_BYPASS_ENABLE 1
15#define CLKMGR_BYPASS_DISABLE 0
16#define CLKMGR_STAT_IDLE 0
17#define CLKMGR_STAT_BUSY 1
18#define CLKMGR_BYPASS_PERPLLSRC_SELECT_EOSC1 0
19#define CLKMGR_BYPASS_PERPLLSRC_SELECT_INPUT_MUX 1
20#define CLKMGR_BYPASS_SDRPLLSRC_SELECT_EOSC1 0
21#define CLKMGR_BYPASS_SDRPLLSRC_SELECT_INPUT_MUX 1
22
23#define CLEAR_BGP_EN_PWRDN \
24 (CLKMGR_MAINPLLGRP_VCO_PWRDN_SET(0)| \
25 CLKMGR_MAINPLLGRP_VCO_EN_SET(0)| \
26 CLKMGR_MAINPLLGRP_VCO_BGPWRDN_SET(0))
27
28#define VCO_EN_BASE \
29 (CLKMGR_MAINPLLGRP_VCO_PWRDN_SET(0)| \
30 CLKMGR_MAINPLLGRP_VCO_EN_SET(1)| \
31 CLKMGR_MAINPLLGRP_VCO_BGPWRDN_SET(0))
32
33static inline void cm_wait_for_lock(uint32_t mask)
34{
35 register uint32_t inter_val;
36 do {
37 inter_val = readl(&clock_manager_base->inter) & mask;
38 } while (inter_val != mask);
39}
40
41/* function to poll in the fsm busy bit */
42static inline void cm_wait_for_fsm(void)
43{
44 while (readl(&clock_manager_base->stat) & CLKMGR_STAT_BUSY)
45 ;
46}
47
48/*
49 * function to write the bypass register which requires a poll of the
50 * busy bit
51 */
52static inline void cm_write_bypass(uint32_t val)
53{
54 writel(val, &clock_manager_base->bypass);
55 cm_wait_for_fsm();
56}
57
58/* function to write the ctrl register which requires a poll of the busy bit */
59static inline void cm_write_ctrl(uint32_t val)
60{
61 writel(val, &clock_manager_base->ctrl);
62 cm_wait_for_fsm();
63}
64
65/* function to write a clock register that has phase information */
66static inline void cm_write_with_phase(uint32_t value,
67 uint32_t reg_address, uint32_t mask)
68{
69 /* poll until phase is zero */
70 while (readl(reg_address) & mask)
71 ;
72
73 writel(value, reg_address);
74
75 while (readl(reg_address) & mask)
76 ;
77}
78
79/*
80 * Setup clocks while making no assumptions about previous state of the clocks.
81 *
82 * Start by being paranoid and gate all sw managed clocks
83 * Put all plls in bypass
84 * Put all plls VCO registers back to reset value (bandgap power down).
85 * Put peripheral and main pll src to reset value to avoid glitch.
86 * Delay 5 us.
87 * Deassert bandgap power down and set numerator and denominator
88 * Start 7 us timer.
89 * set internal dividers
90 * Wait for 7 us timer.
91 * Enable plls
92 * Set external dividers while plls are locking
93 * Wait for pll lock
94 * Assert/deassert outreset all.
95 * Take all pll's out of bypass
96 * Clear safe mode
97 * set source main and peripheral clocks
98 * Ungate clocks
99 */
100
101void cm_basic_init(const cm_config_t *cfg)
102{
103 uint32_t start, timeout;
104
105 /* Start by being paranoid and gate all sw managed clocks */
106
107 /*
108 * We need to disable nandclk
109 * and then do another apb access before disabling
110 * gatting off the rest of the periperal clocks.
111 */
112 writel(~CLKMGR_PERPLLGRP_EN_NANDCLK_MASK &
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200113 readl(&clock_manager_base->per_pll.en),
114 &clock_manager_base->per_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600115
116 /* DO NOT GATE OFF DEBUG CLOCKS & BRIDGE CLOCKS */
117 writel(CLKMGR_MAINPLLGRP_EN_DBGTIMERCLK_MASK |
118 CLKMGR_MAINPLLGRP_EN_DBGTRACECLK_MASK |
119 CLKMGR_MAINPLLGRP_EN_DBGCLK_MASK |
120 CLKMGR_MAINPLLGRP_EN_DBGATCLK_MASK |
121 CLKMGR_MAINPLLGRP_EN_S2FUSER0CLK_MASK |
122 CLKMGR_MAINPLLGRP_EN_L4MPCLK_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200123 &clock_manager_base->main_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600124
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200125 writel(0, &clock_manager_base->sdr_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600126
127 /* now we can gate off the rest of the peripheral clocks */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200128 writel(0, &clock_manager_base->per_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600129
130 /* Put all plls in bypass */
131 cm_write_bypass(
132 CLKMGR_BYPASS_PERPLLSRC_SET(
133 CLKMGR_BYPASS_PERPLLSRC_SELECT_EOSC1) |
134 CLKMGR_BYPASS_SDRPLLSRC_SET(
135 CLKMGR_BYPASS_SDRPLLSRC_SELECT_EOSC1) |
136 CLKMGR_BYPASS_PERPLL_SET(CLKMGR_BYPASS_ENABLE) |
137 CLKMGR_BYPASS_SDRPLL_SET(CLKMGR_BYPASS_ENABLE) |
138 CLKMGR_BYPASS_MAINPLL_SET(CLKMGR_BYPASS_ENABLE));
139
140 /*
141 * Put all plls VCO registers back to reset value.
142 * Some code might have messed with them.
143 */
144 writel(CLKMGR_MAINPLLGRP_VCO_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200145 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600146 writel(CLKMGR_PERPLLGRP_VCO_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200147 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600148 writel(CLKMGR_SDRPLLGRP_VCO_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200149 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600150
151 /*
152 * The clocks to the flash devices and the L4_MAIN clocks can
153 * glitch when coming out of safe mode if their source values
154 * are different from their reset value. So the trick it to
155 * put them back to their reset state, and change input
156 * after exiting safe mode but before ungating the clocks.
157 */
158 writel(CLKMGR_PERPLLGRP_SRC_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200159 &clock_manager_base->per_pll.src);
Chin Liang Seecb350602014-03-04 22:13:53 -0600160 writel(CLKMGR_MAINPLLGRP_L4SRC_RESET_VALUE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200161 &clock_manager_base->main_pll.l4src);
Chin Liang Seecb350602014-03-04 22:13:53 -0600162
163 /* read back for the required 5 us delay. */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200164 readl(&clock_manager_base->main_pll.vco);
165 readl(&clock_manager_base->per_pll.vco);
166 readl(&clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600167
168
169 /*
170 * We made sure bgpwr down was assert for 5 us. Now deassert BG PWR DN
171 * with numerator and denominator.
172 */
173 writel(cfg->main_vco_base | CLEAR_BGP_EN_PWRDN |
174 CLKMGR_MAINPLLGRP_VCO_REGEXTSEL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200175 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600176
177 writel(cfg->peri_vco_base | CLEAR_BGP_EN_PWRDN |
178 CLKMGR_PERPLLGRP_VCO_REGEXTSEL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200179 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600180
181 writel(CLKMGR_SDRPLLGRP_VCO_OUTRESET_SET(0) |
182 CLKMGR_SDRPLLGRP_VCO_OUTRESETALL_SET(0) |
183 cfg->sdram_vco_base | CLEAR_BGP_EN_PWRDN |
184 CLKMGR_SDRPLLGRP_VCO_REGEXTSEL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200185 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600186
187 /*
188 * Time starts here
189 * must wait 7 us from BGPWRDN_SET(0) to VCO_ENABLE_SET(1)
190 */
191 reset_timer();
192 start = get_timer(0);
193 /* timeout in unit of us as CONFIG_SYS_HZ = 1000*1000 */
194 timeout = 7;
195
196 /* main mpu */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200197 writel(cfg->mpuclk, &clock_manager_base->main_pll.mpuclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600198
199 /* main main clock */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200200 writel(cfg->mainclk, &clock_manager_base->main_pll.mainclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600201
202 /* main for dbg */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200203 writel(cfg->dbgatclk, &clock_manager_base->main_pll.dbgatclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600204
205 /* main for cfgs2fuser0clk */
206 writel(cfg->cfg2fuser0clk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200207 &clock_manager_base->main_pll.cfgs2fuser0clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600208
209 /* Peri emac0 50 MHz default to RMII */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200210 writel(cfg->emac0clk, &clock_manager_base->per_pll.emac0clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600211
212 /* Peri emac1 50 MHz default to RMII */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200213 writel(cfg->emac1clk, &clock_manager_base->per_pll.emac1clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600214
215 /* Peri QSPI */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200216 writel(cfg->mainqspiclk, &clock_manager_base->main_pll.mainqspiclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600217
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200218 writel(cfg->perqspiclk, &clock_manager_base->per_pll.perqspiclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600219
220 /* Peri pernandsdmmcclk */
221 writel(cfg->pernandsdmmcclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200222 &clock_manager_base->per_pll.pernandsdmmcclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600223
224 /* Peri perbaseclk */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200225 writel(cfg->perbaseclk, &clock_manager_base->per_pll.perbaseclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600226
227 /* Peri s2fuser1clk */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200228 writel(cfg->s2fuser1clk, &clock_manager_base->per_pll.s2fuser1clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600229
230 /* 7 us must have elapsed before we can enable the VCO */
231 while (get_timer(start) < timeout)
232 ;
233
234 /* Enable vco */
235 /* main pll vco */
236 writel(cfg->main_vco_base | VCO_EN_BASE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200237 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600238
239 /* periferal pll */
240 writel(cfg->peri_vco_base | VCO_EN_BASE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200241 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600242
243 /* sdram pll vco */
244 writel(CLKMGR_SDRPLLGRP_VCO_OUTRESET_SET(0) |
245 CLKMGR_SDRPLLGRP_VCO_OUTRESETALL_SET(0) |
246 cfg->sdram_vco_base | VCO_EN_BASE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200247 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600248
249 /* L3 MP and L3 SP */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200250 writel(cfg->maindiv, &clock_manager_base->main_pll.maindiv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600251
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200252 writel(cfg->dbgdiv, &clock_manager_base->main_pll.dbgdiv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600253
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200254 writel(cfg->tracediv, &clock_manager_base->main_pll.tracediv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600255
256 /* L4 MP, L4 SP, can0, and can1 */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200257 writel(cfg->perdiv, &clock_manager_base->per_pll.div);
Chin Liang Seecb350602014-03-04 22:13:53 -0600258
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200259 writel(cfg->gpiodiv, &clock_manager_base->per_pll.gpiodiv);
Chin Liang Seecb350602014-03-04 22:13:53 -0600260
261#define LOCKED_MASK \
262 (CLKMGR_INTER_SDRPLLLOCKED_MASK | \
263 CLKMGR_INTER_PERPLLLOCKED_MASK | \
264 CLKMGR_INTER_MAINPLLLOCKED_MASK)
265
266 cm_wait_for_lock(LOCKED_MASK);
267
268 /* write the sdram clock counters before toggling outreset all */
269 writel(cfg->ddrdqsclk & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200270 &clock_manager_base->sdr_pll.ddrdqsclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600271
272 writel(cfg->ddr2xdqsclk & CLKMGR_SDRPLLGRP_DDR2XDQSCLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200273 &clock_manager_base->sdr_pll.ddr2xdqsclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600274
275 writel(cfg->ddrdqclk & CLKMGR_SDRPLLGRP_DDRDQCLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200276 &clock_manager_base->sdr_pll.ddrdqclk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600277
278 writel(cfg->s2fuser2clk & CLKMGR_SDRPLLGRP_S2FUSER2CLK_CNT_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200279 &clock_manager_base->sdr_pll.s2fuser2clk);
Chin Liang Seecb350602014-03-04 22:13:53 -0600280
281 /*
282 * after locking, but before taking out of bypass
283 * assert/deassert outresetall
284 */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200285 uint32_t mainvco = readl(&clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600286
287 /* assert main outresetall */
288 writel(mainvco | CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200289 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600290
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200291 uint32_t periphvco = readl(&clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600292
293 /* assert pheriph outresetall */
294 writel(periphvco | CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200295 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600296
297 /* assert sdram outresetall */
298 writel(cfg->sdram_vco_base | VCO_EN_BASE|
299 CLKMGR_SDRPLLGRP_VCO_OUTRESETALL_SET(1),
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200300 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600301
302 /* deassert main outresetall */
303 writel(mainvco & ~CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200304 &clock_manager_base->main_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600305
306 /* deassert pheriph outresetall */
307 writel(periphvco & ~CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200308 &clock_manager_base->per_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600309
310 /* deassert sdram outresetall */
311 writel(CLKMGR_SDRPLLGRP_VCO_OUTRESETALL_SET(0) |
312 cfg->sdram_vco_base | VCO_EN_BASE,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200313 &clock_manager_base->sdr_pll.vco);
Chin Liang Seecb350602014-03-04 22:13:53 -0600314
315 /*
316 * now that we've toggled outreset all, all the clocks
317 * are aligned nicely; so we can change any phase.
318 */
319 cm_write_with_phase(cfg->ddrdqsclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200320 (uint32_t)&clock_manager_base->sdr_pll.ddrdqsclk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600321 CLKMGR_SDRPLLGRP_DDRDQSCLK_PHASE_MASK);
322
323 /* SDRAM DDR2XDQSCLK */
324 cm_write_with_phase(cfg->ddr2xdqsclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200325 (uint32_t)&clock_manager_base->sdr_pll.ddr2xdqsclk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600326 CLKMGR_SDRPLLGRP_DDR2XDQSCLK_PHASE_MASK);
327
328 cm_write_with_phase(cfg->ddrdqclk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200329 (uint32_t)&clock_manager_base->sdr_pll.ddrdqclk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600330 CLKMGR_SDRPLLGRP_DDRDQCLK_PHASE_MASK);
331
332 cm_write_with_phase(cfg->s2fuser2clk,
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200333 (uint32_t)&clock_manager_base->sdr_pll.s2fuser2clk,
Chin Liang Seecb350602014-03-04 22:13:53 -0600334 CLKMGR_SDRPLLGRP_S2FUSER2CLK_PHASE_MASK);
335
336 /* Take all three PLLs out of bypass when safe mode is cleared. */
337 cm_write_bypass(
338 CLKMGR_BYPASS_PERPLLSRC_SET(
339 CLKMGR_BYPASS_PERPLLSRC_SELECT_EOSC1) |
340 CLKMGR_BYPASS_SDRPLLSRC_SET(
341 CLKMGR_BYPASS_SDRPLLSRC_SELECT_EOSC1) |
342 CLKMGR_BYPASS_PERPLL_SET(CLKMGR_BYPASS_DISABLE) |
343 CLKMGR_BYPASS_SDRPLL_SET(CLKMGR_BYPASS_DISABLE) |
344 CLKMGR_BYPASS_MAINPLL_SET(CLKMGR_BYPASS_DISABLE));
345
346 /* clear safe mode */
347 cm_write_ctrl(readl(&clock_manager_base->ctrl) |
348 CLKMGR_CTRL_SAFEMODE_SET(CLKMGR_CTRL_SAFEMODE_MASK));
349
350 /*
351 * now that safe mode is clear with clocks gated
352 * it safe to change the source mux for the flashes the the L4_MAIN
353 */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200354 writel(cfg->persrc, &clock_manager_base->per_pll.src);
355 writel(cfg->l4src, &clock_manager_base->main_pll.l4src);
Chin Liang Seecb350602014-03-04 22:13:53 -0600356
357 /* Now ungate non-hw-managed clocks */
Pavel Machek91c2f8f2014-07-19 23:57:59 +0200358 writel(~0, &clock_manager_base->main_pll.en);
359 writel(~0, &clock_manager_base->per_pll.en);
360 writel(~0, &clock_manager_base->sdr_pll.en);
Chin Liang Seecb350602014-03-04 22:13:53 -0600361}