1
2
3
4
5
6#include <common.h>
7#include <time.h>
8#include <asm/io.h>
9#include <dm.h>
10#include <asm/arch/clock_manager.h>
11#include <wait_bit.h>
12
13static const struct socfpga_clock_manager *clock_manager_base =
14 (struct socfpga_clock_manager *)SOCFPGA_CLKMGR_ADDRESS;
15
16
17
18
19
20static void cm_write_bypass(u32 val)
21{
22 writel(val, &clock_manager_base->bypass);
23 cm_wait_for_fsm();
24}
25
26
27static void cm_write_ctrl(u32 val)
28{
29 writel(val, &clock_manager_base->ctrl);
30 cm_wait_for_fsm();
31}
32
33
34static int cm_write_with_phase(u32 value, const void *reg_address, u32 mask)
35{
36 int ret;
37
38
39 ret = wait_for_bit_le32(reg_address, mask, false, 20000, false);
40 if (ret)
41 return ret;
42
43 writel(value, reg_address);
44
45 return wait_for_bit_le32(reg_address, mask, false, 20000, false);
46}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70int cm_basic_init(const struct cm_config * const cfg)
71{
72 unsigned long end;
73 int ret;
74
75
76
77
78
79
80
81
82 writel(~CLKMGR_PERPLLGRP_EN_NANDCLK_MASK &
83 readl(&clock_manager_base->per_pll.en),
84 &clock_manager_base->per_pll.en);
85
86
87 writel(CLKMGR_MAINPLLGRP_EN_DBGTIMERCLK_MASK |
88 CLKMGR_MAINPLLGRP_EN_DBGTRACECLK_MASK |
89 CLKMGR_MAINPLLGRP_EN_DBGCLK_MASK |
90 CLKMGR_MAINPLLGRP_EN_DBGATCLK_MASK |
91 CLKMGR_MAINPLLGRP_EN_S2FUSER0CLK_MASK |
92 CLKMGR_MAINPLLGRP_EN_L4MPCLK_MASK,
93 &clock_manager_base->main_pll.en);
94
95 writel(0, &clock_manager_base->sdr_pll.en);
96
97
98 writel(0, &clock_manager_base->per_pll.en);
99
100
101 cm_write_bypass(CLKMGR_BYPASS_PERPLL | CLKMGR_BYPASS_SDRPLL |
102 CLKMGR_BYPASS_MAINPLL);
103
104
105 writel(CLKMGR_MAINPLLGRP_VCO_RESET_VALUE &
106 ~CLKMGR_MAINPLLGRP_VCO_REGEXTSEL_MASK,
107 &clock_manager_base->main_pll.vco);
108 writel(CLKMGR_PERPLLGRP_VCO_RESET_VALUE &
109 ~CLKMGR_PERPLLGRP_VCO_REGEXTSEL_MASK,
110 &clock_manager_base->per_pll.vco);
111 writel(CLKMGR_SDRPLLGRP_VCO_RESET_VALUE &
112 ~CLKMGR_SDRPLLGRP_VCO_REGEXTSEL_MASK,
113 &clock_manager_base->sdr_pll.vco);
114
115
116
117
118
119
120
121
122 writel(CLKMGR_PERPLLGRP_SRC_RESET_VALUE,
123 &clock_manager_base->per_pll.src);
124 writel(CLKMGR_MAINPLLGRP_L4SRC_RESET_VALUE,
125 &clock_manager_base->main_pll.l4src);
126
127
128 readl(&clock_manager_base->main_pll.vco);
129 readl(&clock_manager_base->per_pll.vco);
130 readl(&clock_manager_base->sdr_pll.vco);
131
132
133
134
135
136
137 writel(cfg->main_vco_base, &clock_manager_base->main_pll.vco);
138 writel(cfg->peri_vco_base, &clock_manager_base->per_pll.vco);
139 writel(cfg->sdram_vco_base, &clock_manager_base->sdr_pll.vco);
140
141
142
143
144
145 end = timer_get_us() + 7;
146
147
148 writel(cfg->mpuclk, &clock_manager_base->main_pll.mpuclk);
149
150
151 writel(cfg->altera_grp_mpuclk, &clock_manager_base->altera.mpuclk);
152
153
154 writel(cfg->mainclk, &clock_manager_base->main_pll.mainclk);
155
156
157 writel(cfg->dbgatclk, &clock_manager_base->main_pll.dbgatclk);
158
159
160 writel(cfg->cfg2fuser0clk,
161 &clock_manager_base->main_pll.cfgs2fuser0clk);
162
163
164 writel(cfg->emac0clk, &clock_manager_base->per_pll.emac0clk);
165
166
167 writel(cfg->emac1clk, &clock_manager_base->per_pll.emac1clk);
168
169
170 writel(cfg->mainqspiclk, &clock_manager_base->main_pll.mainqspiclk);
171
172 writel(cfg->perqspiclk, &clock_manager_base->per_pll.perqspiclk);
173
174
175 writel(cfg->mainnandsdmmcclk,
176 &clock_manager_base->main_pll.mainnandsdmmcclk);
177
178 writel(cfg->pernandsdmmcclk,
179 &clock_manager_base->per_pll.pernandsdmmcclk);
180
181
182 writel(cfg->perbaseclk, &clock_manager_base->per_pll.perbaseclk);
183
184
185 writel(cfg->s2fuser1clk, &clock_manager_base->per_pll.s2fuser1clk);
186
187
188 while (timer_get_us() < end)
189 ;
190
191
192
193 writel(cfg->main_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
194 &clock_manager_base->main_pll.vco);
195
196
197 writel(cfg->peri_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
198 &clock_manager_base->per_pll.vco);
199
200
201 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
202 &clock_manager_base->sdr_pll.vco);
203
204
205 writel(cfg->maindiv, &clock_manager_base->main_pll.maindiv);
206
207 writel(cfg->dbgdiv, &clock_manager_base->main_pll.dbgdiv);
208
209 writel(cfg->tracediv, &clock_manager_base->main_pll.tracediv);
210
211
212 writel(cfg->perdiv, &clock_manager_base->per_pll.div);
213
214 writel(cfg->gpiodiv, &clock_manager_base->per_pll.gpiodiv);
215
216 cm_wait_for_lock(LOCKED_MASK);
217
218
219 writel(cfg->ddrdqsclk & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK,
220 &clock_manager_base->sdr_pll.ddrdqsclk);
221
222 writel(cfg->ddr2xdqsclk & CLKMGR_SDRPLLGRP_DDR2XDQSCLK_CNT_MASK,
223 &clock_manager_base->sdr_pll.ddr2xdqsclk);
224
225 writel(cfg->ddrdqclk & CLKMGR_SDRPLLGRP_DDRDQCLK_CNT_MASK,
226 &clock_manager_base->sdr_pll.ddrdqclk);
227
228 writel(cfg->s2fuser2clk & CLKMGR_SDRPLLGRP_S2FUSER2CLK_CNT_MASK,
229 &clock_manager_base->sdr_pll.s2fuser2clk);
230
231
232
233
234
235 u32 mainvco = readl(&clock_manager_base->main_pll.vco);
236
237
238 writel(mainvco | CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
239 &clock_manager_base->main_pll.vco);
240
241 u32 periphvco = readl(&clock_manager_base->per_pll.vco);
242
243
244 writel(periphvco | CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
245 &clock_manager_base->per_pll.vco);
246
247
248 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN|
249 CLKMGR_SDRPLLGRP_VCO_OUTRESETALL,
250 &clock_manager_base->sdr_pll.vco);
251
252
253 writel(mainvco & ~CLKMGR_MAINPLLGRP_VCO_OUTRESETALL_MASK,
254 &clock_manager_base->main_pll.vco);
255
256
257 writel(periphvco & ~CLKMGR_PERPLLGRP_VCO_OUTRESETALL_MASK,
258 &clock_manager_base->per_pll.vco);
259
260
261 writel(cfg->sdram_vco_base | CLKMGR_MAINPLLGRP_VCO_EN,
262 &clock_manager_base->sdr_pll.vco);
263
264
265
266
267
268 ret = cm_write_with_phase(cfg->ddrdqsclk,
269 &clock_manager_base->sdr_pll.ddrdqsclk,
270 CLKMGR_SDRPLLGRP_DDRDQSCLK_PHASE_MASK);
271 if (ret)
272 return ret;
273
274
275 ret = cm_write_with_phase(cfg->ddr2xdqsclk,
276 &clock_manager_base->sdr_pll.ddr2xdqsclk,
277 CLKMGR_SDRPLLGRP_DDR2XDQSCLK_PHASE_MASK);
278 if (ret)
279 return ret;
280
281 ret = cm_write_with_phase(cfg->ddrdqclk,
282 &clock_manager_base->sdr_pll.ddrdqclk,
283 CLKMGR_SDRPLLGRP_DDRDQCLK_PHASE_MASK);
284 if (ret)
285 return ret;
286
287 ret = cm_write_with_phase(cfg->s2fuser2clk,
288 &clock_manager_base->sdr_pll.s2fuser2clk,
289 CLKMGR_SDRPLLGRP_S2FUSER2CLK_PHASE_MASK);
290 if (ret)
291 return ret;
292
293
294 cm_write_bypass(0);
295
296
297 cm_write_ctrl(readl(&clock_manager_base->ctrl) | CLKMGR_CTRL_SAFEMODE);
298
299
300
301
302
303 writel(cfg->persrc, &clock_manager_base->per_pll.src);
304 writel(cfg->l4src, &clock_manager_base->main_pll.l4src);
305
306
307 writel(~0, &clock_manager_base->main_pll.en);
308 writel(~0, &clock_manager_base->per_pll.en);
309 writel(~0, &clock_manager_base->sdr_pll.en);
310
311
312 writel(CLKMGR_INTER_SDRPLLLOST_MASK | CLKMGR_INTER_PERPLLLOST_MASK |
313 CLKMGR_INTER_MAINPLLLOST_MASK,
314 &clock_manager_base->inter);
315
316 return 0;
317}
318
319static unsigned int cm_get_main_vco_clk_hz(void)
320{
321 u32 reg, clock;
322
323
324 reg = readl(&clock_manager_base->main_pll.vco);
325 clock = cm_get_osc_clk_hz(1);
326 clock /= ((reg & CLKMGR_MAINPLLGRP_VCO_DENOM_MASK) >>
327 CLKMGR_MAINPLLGRP_VCO_DENOM_OFFSET) + 1;
328 clock *= ((reg & CLKMGR_MAINPLLGRP_VCO_NUMER_MASK) >>
329 CLKMGR_MAINPLLGRP_VCO_NUMER_OFFSET) + 1;
330
331 return clock;
332}
333
334static unsigned int cm_get_per_vco_clk_hz(void)
335{
336 u32 reg, clock = 0;
337
338
339 reg = readl(&clock_manager_base->per_pll.vco);
340 reg = (reg & CLKMGR_PERPLLGRP_VCO_SSRC_MASK) >>
341 CLKMGR_PERPLLGRP_VCO_SSRC_OFFSET;
342 if (reg == CLKMGR_VCO_SSRC_EOSC1)
343 clock = cm_get_osc_clk_hz(1);
344 else if (reg == CLKMGR_VCO_SSRC_EOSC2)
345 clock = cm_get_osc_clk_hz(2);
346 else if (reg == CLKMGR_VCO_SSRC_F2S)
347 clock = cm_get_f2s_per_ref_clk_hz();
348
349
350 reg = readl(&clock_manager_base->per_pll.vco);
351 clock /= ((reg & CLKMGR_PERPLLGRP_VCO_DENOM_MASK) >>
352 CLKMGR_PERPLLGRP_VCO_DENOM_OFFSET) + 1;
353 clock *= ((reg & CLKMGR_PERPLLGRP_VCO_NUMER_MASK) >>
354 CLKMGR_PERPLLGRP_VCO_NUMER_OFFSET) + 1;
355
356 return clock;
357}
358
359unsigned long cm_get_mpu_clk_hz(void)
360{
361 u32 reg, clock;
362
363 clock = cm_get_main_vco_clk_hz();
364
365
366 reg = readl(&clock_manager_base->altera.mpuclk);
367 clock /= (reg + 1);
368 reg = readl(&clock_manager_base->main_pll.mpuclk);
369 clock /= (reg + 1);
370 return clock;
371}
372
373unsigned long cm_get_sdram_clk_hz(void)
374{
375 u32 reg, clock = 0;
376
377
378 reg = readl(&clock_manager_base->sdr_pll.vco);
379 reg = (reg & CLKMGR_SDRPLLGRP_VCO_SSRC_MASK) >>
380 CLKMGR_SDRPLLGRP_VCO_SSRC_OFFSET;
381 if (reg == CLKMGR_VCO_SSRC_EOSC1)
382 clock = cm_get_osc_clk_hz(1);
383 else if (reg == CLKMGR_VCO_SSRC_EOSC2)
384 clock = cm_get_osc_clk_hz(2);
385 else if (reg == CLKMGR_VCO_SSRC_F2S)
386 clock = cm_get_f2s_sdr_ref_clk_hz();
387
388
389 reg = readl(&clock_manager_base->sdr_pll.vco);
390 clock /= ((reg & CLKMGR_SDRPLLGRP_VCO_DENOM_MASK) >>
391 CLKMGR_SDRPLLGRP_VCO_DENOM_OFFSET) + 1;
392 clock *= ((reg & CLKMGR_SDRPLLGRP_VCO_NUMER_MASK) >>
393 CLKMGR_SDRPLLGRP_VCO_NUMER_OFFSET) + 1;
394
395
396 reg = readl(&clock_manager_base->sdr_pll.ddrdqsclk);
397 reg = (reg & CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_MASK) >>
398 CLKMGR_SDRPLLGRP_DDRDQSCLK_CNT_OFFSET;
399 clock /= (reg + 1);
400
401 return clock;
402}
403
404unsigned int cm_get_l4_sp_clk_hz(void)
405{
406 u32 reg, clock = 0;
407
408
409 reg = readl(&clock_manager_base->main_pll.l4src);
410 reg = (reg & CLKMGR_MAINPLLGRP_L4SRC_L4SP) >>
411 CLKMGR_MAINPLLGRP_L4SRC_L4SP_OFFSET;
412
413 if (reg == CLKMGR_L4_SP_CLK_SRC_MAINPLL) {
414 clock = cm_get_main_vco_clk_hz();
415
416
417 reg = readl(&clock_manager_base->altera.mainclk);
418 clock /= (reg + 1);
419 reg = readl(&clock_manager_base->main_pll.mainclk);
420 clock /= (reg + 1);
421 } else if (reg == CLKMGR_L4_SP_CLK_SRC_PERPLL) {
422 clock = cm_get_per_vco_clk_hz();
423
424
425 reg = readl(&clock_manager_base->per_pll.perbaseclk);
426 clock /= (reg + 1);
427 }
428
429
430 reg = readl(&clock_manager_base->main_pll.maindiv);
431 reg = (reg & CLKMGR_MAINPLLGRP_MAINDIV_L4SPCLK_MASK) >>
432 CLKMGR_MAINPLLGRP_MAINDIV_L4SPCLK_OFFSET;
433 clock = clock / (1 << reg);
434
435 return clock;
436}
437
438unsigned int cm_get_mmc_controller_clk_hz(void)
439{
440 u32 reg, clock = 0;
441
442
443 reg = readl(&clock_manager_base->per_pll.src);
444 reg = (reg & CLKMGR_PERPLLGRP_SRC_SDMMC_MASK) >>
445 CLKMGR_PERPLLGRP_SRC_SDMMC_OFFSET;
446
447 if (reg == CLKMGR_SDMMC_CLK_SRC_F2S) {
448 clock = cm_get_f2s_per_ref_clk_hz();
449 } else if (reg == CLKMGR_SDMMC_CLK_SRC_MAIN) {
450 clock = cm_get_main_vco_clk_hz();
451
452
453 reg = readl(&clock_manager_base->main_pll.mainnandsdmmcclk);
454 clock /= (reg + 1);
455 } else if (reg == CLKMGR_SDMMC_CLK_SRC_PER) {
456 clock = cm_get_per_vco_clk_hz();
457
458
459 reg = readl(&clock_manager_base->per_pll.pernandsdmmcclk);
460 clock /= (reg + 1);
461 }
462
463
464 clock /= 4;
465 return clock;
466}
467
468unsigned int cm_get_qspi_controller_clk_hz(void)
469{
470 u32 reg, clock = 0;
471
472
473 reg = readl(&clock_manager_base->per_pll.src);
474 reg = (reg & CLKMGR_PERPLLGRP_SRC_QSPI_MASK) >>
475 CLKMGR_PERPLLGRP_SRC_QSPI_OFFSET;
476
477 if (reg == CLKMGR_QSPI_CLK_SRC_F2S) {
478 clock = cm_get_f2s_per_ref_clk_hz();
479 } else if (reg == CLKMGR_QSPI_CLK_SRC_MAIN) {
480 clock = cm_get_main_vco_clk_hz();
481
482
483 reg = readl(&clock_manager_base->main_pll.mainqspiclk);
484 clock /= (reg + 1);
485 } else if (reg == CLKMGR_QSPI_CLK_SRC_PER) {
486 clock = cm_get_per_vco_clk_hz();
487
488
489 reg = readl(&clock_manager_base->per_pll.perqspiclk);
490 clock /= (reg + 1);
491 }
492
493 return clock;
494}
495
496unsigned int cm_get_spi_controller_clk_hz(void)
497{
498 u32 reg, clock = 0;
499
500 clock = cm_get_per_vco_clk_hz();
501
502
503 reg = readl(&clock_manager_base->per_pll.perbaseclk);
504 clock /= (reg + 1);
505
506 return clock;
507}
508
509
510int dw_spi_get_clk(struct udevice *bus, ulong *rate)
511{
512 *rate = cm_get_spi_controller_clk_hz();
513
514 return 0;
515}
516
517void cm_print_clock_quick_summary(void)
518{
519 printf("MPU %10ld kHz\n", cm_get_mpu_clk_hz() / 1000);
520 printf("DDR %10ld kHz\n", cm_get_sdram_clk_hz() / 1000);
521 printf("EOSC1 %8d kHz\n", cm_get_osc_clk_hz(1) / 1000);
522 printf("EOSC2 %8d kHz\n", cm_get_osc_clk_hz(2) / 1000);
523 printf("F2S_SDR_REF %8d kHz\n", cm_get_f2s_sdr_ref_clk_hz() / 1000);
524 printf("F2S_PER_REF %8d kHz\n", cm_get_f2s_per_ref_clk_hz() / 1000);
525 printf("MMC %8d kHz\n", cm_get_mmc_controller_clk_hz() / 1000);
526 printf("QSPI %8d kHz\n", cm_get_qspi_controller_clk_hz() / 1000);
527 printf("UART %8d kHz\n", cm_get_l4_sp_clk_hz() / 1000);
528 printf("SPI %8d kHz\n", cm_get_spi_controller_clk_hz() / 1000);
529}
530