1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
* Copyright (C) 2020-2022 Intel Corporation <www.intel.com>
*/
#include <asm/arch/clock_manager.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <clk-uclass.h>
#include <dm.h>
#include <dm/lists.h>
#include <dm/util.h>
#include <dt-bindings/clock/n5x-clock.h>
DECLARE_GLOBAL_DATA_PTR;
struct socfpga_clk_plat {
void __iomem *regs;
};
/*
* function to write the bypass register which requires a poll of the
* busy bit
*/
static void clk_write_bypass_mainpll(struct socfpga_clk_plat *plat, u32 val)
{
CM_REG_WRITEL(plat, val, CLKMGR_MAINPLL_BYPASS);
cm_wait_for_fsm();
}
static void clk_write_bypass_perpll(struct socfpga_clk_plat *plat, u32 val)
{
CM_REG_WRITEL(plat, val, CLKMGR_PERPLL_BYPASS);
cm_wait_for_fsm();
}
/* function to write the ctrl register which requires a poll of the busy bit */
static void clk_write_ctrl(struct socfpga_clk_plat *plat, u32 val)
{
CM_REG_WRITEL(plat, val, CLKMGR_CTRL);
cm_wait_for_fsm();
}
/*
* Setup clocks while making no assumptions about previous state of the clocks.
*/
static void clk_basic_init(struct udevice *dev,
const struct cm_config * const cfg)
{
struct socfpga_clk_plat *plat = dev_get_plat(dev);
if (!cfg)
return;
#if IS_ENABLED(CONFIG_SPL_BUILD)
/* Always force clock manager into boot mode before any configuration */
clk_write_ctrl(plat,
CM_REG_READL(plat, CLKMGR_CTRL) | CLKMGR_CTRL_BOOTMODE);
#else
/* Skip clock configuration in SSBL if it's not in boot mode */
if (!(CM_REG_READL(plat, CLKMGR_CTRL) & CLKMGR_CTRL_BOOTMODE))
return;
#endif
/* Put both PLLs in bypass */
clk_write_bypass_mainpll(plat, CLKMGR_BYPASS_MAINPLL_ALL);
clk_write_bypass_perpll(plat, CLKMGR_BYPASS_PERPLL_ALL);
/* Put both PLLs in Reset */
CM_REG_SETBITS(plat, CLKMGR_MAINPLL_PLLCTRL,
CLKMGR_PLLCTRL_BYPASS_MASK);
CM_REG_SETBITS(plat, CLKMGR_PERPLL_PLLCTRL,
CLKMGR_PLLCTRL_BYPASS_MASK);
/* setup main PLL */
CM_REG_WRITEL(plat, cfg->main_pll_pllglob, CLKMGR_MAINPLL_PLLGLOB);
CM_REG_WRITEL(plat, cfg->main_pll_plldiv, CLKMGR_MAINPLL_PLLDIV);
CM_REG_WRITEL(plat, cfg->main_pll_plloutdiv, CLKMGR_MAINPLL_PLLOUTDIV);
CM_REG_WRITEL(plat, cfg->main_pll_mpuclk, CLKMGR_MAINPLL_MPUCLK);
CM_REG_WRITEL(plat, cfg->main_pll_nocclk, CLKMGR_MAINPLL_NOCCLK);
CM_REG_WRITEL(plat, cfg->main_pll_nocdiv, CLKMGR_MAINPLL_NOCDIV);
/* setup peripheral */
CM_REG_WRITEL(plat, cfg->per_pll_pllglob, CLKMGR_PERPLL_PLLGLOB);
CM_REG_WRITEL(plat, cfg->per_pll_plldiv, CLKMGR_PERPLL_PLLDIV);
CM_REG_WRITEL(plat, cfg->per_pll_plloutdiv, CLKMGR_PERPLL_PLLOUTDIV);
CM_REG_WRITEL(plat, cfg->per_pll_emacctl, CLKMGR_PERPLL_EMACCTL);
CM_REG_WRITEL(plat, cfg->per_pll_gpiodiv, CLKMGR_PERPLL_GPIODIV);
/* Take both PLL out of reset and power up */
CM_REG_CLRBITS(plat, CLKMGR_MAINPLL_PLLCTRL,
CLKMGR_PLLCTRL_BYPASS_MASK);
CM_REG_CLRBITS(plat, CLKMGR_PERPLL_PLLCTRL,
CLKMGR_PLLCTRL_BYPASS_MASK);
cm_wait_for_lock(CLKMGR_STAT_ALLPLL_LOCKED_MASK);
CM_REG_WRITEL(plat, cfg->alt_emacactr, CLKMGR_ALTR_EMACACTR);
CM_REG_WRITEL(plat, cfg->alt_emacbctr, CLKMGR_ALTR_EMACBCTR);
CM_REG_WRITEL(plat, cfg->alt_emacptpctr, CLKMGR_ALTR_EMACPTPCTR);
CM_REG_WRITEL(plat, cfg->alt_gpiodbctr, CLKMGR_ALTR_GPIODBCTR);
CM_REG_WRITEL(plat, cfg->alt_sdmmcctr, CLKMGR_ALTR_SDMMCCTR);
CM_REG_WRITEL(plat, cfg->alt_s2fuser0ctr, CLKMGR_ALTR_S2FUSER0CTR);
CM_REG_WRITEL(plat, cfg->alt_s2fuser1ctr, CLKMGR_ALTR_S2FUSER1CTR);
CM_REG_WRITEL(plat, cfg->alt_psirefctr, CLKMGR_ALTR_PSIREFCTR);
/* Configure ping pong counters in altera group */
CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_MAINPLL_LOSTLOCK);
CM_REG_WRITEL(plat, CLKMGR_LOSTLOCK_SET_MASK, CLKMGR_PERPLL_LOSTLOCK);
CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_MAINPLL_PLLGLOB) |
CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
CLKMGR_MAINPLL_PLLGLOB);
CM_REG_WRITEL(plat, CM_REG_READL(plat, CLKMGR_PERPLL_PLLGLOB) |
CLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
CLKMGR_PERPLL_PLLGLOB);
/* Take all PLLs out of bypass */
clk_write_bypass_mainpll(plat, 0);
clk_write_bypass_perpll(plat, 0);
/* Clear the loss of lock bits */
CM_REG_CLRBITS(plat, CLKMGR_INTRCLR,
CLKMGR_INTER_PERPLLLOST_MASK |
CLKMGR_INTER_MAINPLLLOST_MASK);
/* Take all ping pong counters out of reset */
CM_REG_CLRBITS(plat, CLKMGR_ALTR_EXTCNTRST,
CLKMGR_ALT_EXTCNTRST_ALLCNTRST_MASK);
/* Out of boot mode */
clk_write_ctrl(plat,
CM_REG_READL(plat, CLKMGR_CTRL) & ~CLKMGR_CTRL_BOOTMODE);
}
static u32 clk_get_5_1_clk_src(struct socfpga_clk_plat *plat, u32 reg)
{
u32 clksrc = CM_REG_READL(plat, reg);
return (clksrc & CLKMGR_CLKSRC_MASK) >> CLKMGR_CLKSRC_OFFSET;
}
static u64 clk_get_pll_output_hz(struct socfpga_clk_plat *plat,
u32 pllglob_reg, u32 plldiv_reg)
{
u64 clock = 0;
u32 clklsrc, divf, divr, divq, power = 1;
/* Get input clock frequency */
clklsrc = (CM_REG_READL(plat, pllglob_reg) &
CLKMGR_PLLGLOB_VCO_PSRC_MASK) >>
CLKMGR_PLLGLOB_VCO_PSRC_OFFSET;
switch (clklsrc) {
case CLKMGR_VCO_PSRC_EOSC1:
clock = cm_get_osc_clk_hz();
break;
case CLKMGR_VCO_PSRC_INTOSC:
clock = cm_get_intosc_clk_hz();
break;
case CLKMGR_VCO_PSRC_F2S:
clock = cm_get_fpga_clk_hz();
break;
}
/* Calculate pll out clock frequency */
divf = (CM_REG_READL(plat, plldiv_reg) &
CLKMGR_PLLDIV_FDIV_MASK) >>
CLKMGR_PLLDIV_FDIV_OFFSET;
divr = (CM_REG_READL(plat, plldiv_reg) &
CLKMGR_PLLDIV_REFCLKDIV_MASK) >>
CLKMGR_PLLDIV_REFCLKDIV_OFFSET;
divq = (CM_REG_READL(plat, plldiv_reg) &
CLKMGR_PLLDIV_OUTDIV_QDIV_MASK) >>
CLKMGR_PLLDIV_OUTDIV_QDIV_OFFSET;
while (divq) {
power *= 2;
divq--;
}
return (clock * 2 * (divf + 1)) / ((divr + 1) * power);
}
static u64 clk_get_clksrc_hz(struct socfpga_clk_plat *plat, u32 clksrc_reg,
u32 main_div, u32 per_div)
{
u64 clock = 0;
u32 clklsrc = clk_get_5_1_clk_src(plat, clksrc_reg);
switch (clklsrc) {
case CLKMGR_CLKSRC_MAIN:
clock = clk_get_pll_output_hz(plat,
CLKMGR_MAINPLL_PLLGLOB,
CLKMGR_MAINPLL_PLLDIV);
clock /= 1 + main_div;
break;
case CLKMGR_CLKSRC_PER:
clock = clk_get_pll_output_hz(plat,
CLKMGR_PERPLL_PLLGLOB,
CLKMGR_PERPLL_PLLDIV);
clock /= 1 + per_div;
break;
case CLKMGR_CLKSRC_OSC1:
clock = cm_get_osc_clk_hz();
break;
case CLKMGR_CLKSRC_INTOSC:
clock = cm_get_intosc_clk_hz();
break;
case CLKMGR_CLKSRC_FPGA:
clock = cm_get_fpga_clk_hz();
break;
default:
return 0;
}
return clock;
}
static u64 clk_get_mpu_clk_hz(struct socfpga_clk_plat *plat)
{
u32 mainpll_c0cnt = (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C0CNT_MASK) >>
CLKMGR_PLLOUTDIV_C0CNT_OFFSET;
u32 perpll_c0cnt = (CM_REG_READL(plat, CLKMGR_PERPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C0CNT_MASK) >>
CLKMGR_PLLOUTDIV_C0CNT_OFFSET;
u64 clock = clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_MPUCLK,
mainpll_c0cnt, perpll_c0cnt);
clock /= 1 + (CM_REG_READL(plat, CLKMGR_MAINPLL_MPUCLK) &
CLKMGR_CLKCNT_MSK);
return clock;
}
static u32 clk_get_l3_main_clk_hz(struct socfpga_clk_plat *plat)
{
u32 mainpll_c1cnt = (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C1CNT_MASK) >>
CLKMGR_PLLOUTDIV_C1CNT_OFFSET;
u32 perpll_c1cnt = (CM_REG_READL(plat, CLKMGR_PERPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C1CNT_MASK) >>
CLKMGR_PLLOUTDIV_C1CNT_OFFSET;
return clk_get_clksrc_hz(plat, CLKMGR_MAINPLL_NOCCLK,
mainpll_c1cnt, perpll_c1cnt);
}
static u32 clk_get_l4_main_clk_hz(struct socfpga_clk_plat *plat)
{
u64 clock = clk_get_l3_main_clk_hz(plat);
clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
CLKMGR_NOCDIV_L4MAIN_OFFSET) &
CLKMGR_NOCDIV_DIVIDER_MASK);
return clock;
}
static u32 clk_get_sdmmc_clk_hz(struct socfpga_clk_plat *plat)
{
u32 mainpll_c3cnt = (CM_REG_READL(plat, CLKMGR_MAINPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C3CNT_MASK) >>
CLKMGR_PLLOUTDIV_C3CNT_OFFSET;
u32 perpll_c3cnt = (CM_REG_READL(plat, CLKMGR_PERPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C3CNT_MASK) >>
CLKMGR_PLLOUTDIV_C3CNT_OFFSET;
u64 clock = clk_get_clksrc_hz(plat, CLKMGR_ALTR_SDMMCCTR,
mainpll_c3cnt, perpll_c3cnt);
clock /= 1 + (CM_REG_READL(plat, CLKMGR_ALTR_SDMMCCTR) &
CLKMGR_CLKCNT_MSK);
return clock / 4;
}
static u32 clk_get_l4_sp_clk_hz(struct socfpga_clk_plat *plat)
{
u64 clock = clk_get_l3_main_clk_hz(plat);
clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
CLKMGR_NOCDIV_L4SPCLK_OFFSET) &
CLKMGR_NOCDIV_DIVIDER_MASK);
return clock;
}
static u32 clk_get_l4_mp_clk_hz(struct socfpga_clk_plat *plat)
{
u64 clock = clk_get_l3_main_clk_hz(plat);
clock /= BIT((CM_REG_READL(plat, CLKMGR_MAINPLL_NOCDIV) >>
CLKMGR_NOCDIV_L4MPCLK_OFFSET) &
CLKMGR_NOCDIV_DIVIDER_MASK);
return clock;
}
static u32 clk_get_l4_sys_free_clk_hz(struct socfpga_clk_plat *plat)
{
if (CM_REG_READL(plat, CLKMGR_STAT) & CLKMGR_STAT_BOOTMODE)
return clk_get_l3_main_clk_hz(plat) / 2;
return clk_get_l3_main_clk_hz(plat) / 4;
}
static u32 clk_get_emac_clk_hz(struct socfpga_clk_plat *plat, u32 emac_id)
{
bool emacsel_a;
u32 ctl;
u32 ctr_reg;
u32 clock;
u32 div;
u32 reg;
/* Get EMAC clock source */
ctl = CM_REG_READL(plat, CLKMGR_PERPLL_EMACCTL);
if (emac_id == N5X_EMAC0_CLK)
ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_OFFSET) &
CLKMGR_PERPLLGRP_EMACCTL_EMAC0SELB_MASK;
else if (emac_id == N5X_EMAC1_CLK)
ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_OFFSET) &
CLKMGR_PERPLLGRP_EMACCTL_EMAC1SELB_MASK;
else if (emac_id == N5X_EMAC2_CLK)
ctl = (ctl >> CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_OFFSET) &
CLKMGR_PERPLLGRP_EMACCTL_EMAC2SELB_MASK;
else
return 0;
if (ctl) {
/* EMAC B source */
emacsel_a = false;
ctr_reg = CLKMGR_ALTR_EMACBCTR;
} else {
/* EMAC A source */
emacsel_a = true;
ctr_reg = CLKMGR_ALTR_EMACACTR;
}
reg = CM_REG_READL(plat, ctr_reg);
clock = (reg & CLKMGR_ALT_EMACCTR_SRC_MASK)
>> CLKMGR_ALT_EMACCTR_SRC_OFFSET;
div = (reg & CLKMGR_ALT_EMACCTR_CNT_MASK)
>> CLKMGR_ALT_EMACCTR_CNT_OFFSET;
switch (clock) {
case CLKMGR_CLKSRC_MAIN:
clock = clk_get_pll_output_hz(plat,
CLKMGR_MAINPLL_PLLGLOB,
CLKMGR_MAINPLL_PLLDIV);
if (emacsel_a) {
clock /= 1 + ((CM_REG_READL(plat,
CLKMGR_MAINPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C2CNT_MASK) >>
CLKMGR_PLLOUTDIV_C2CNT_OFFSET);
} else {
clock /= 1 + ((CM_REG_READL(plat,
CLKMGR_MAINPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C3CNT_MASK) >>
CLKMGR_PLLOUTDIV_C3CNT_OFFSET);
}
break;
case CLKMGR_CLKSRC_PER:
clock = clk_get_pll_output_hz(plat,
CLKMGR_PERPLL_PLLGLOB,
CLKMGR_PERPLL_PLLDIV);
if (emacsel_a) {
clock /= 1 + ((CM_REG_READL(plat,
CLKMGR_PERPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C2CNT_MASK) >>
CLKMGR_PLLOUTDIV_C2CNT_OFFSET);
} else {
clock /= 1 + ((CM_REG_READL(plat,
CLKMGR_PERPLL_PLLOUTDIV) &
CLKMGR_PLLOUTDIV_C3CNT_MASK >>
CLKMGR_PLLOUTDIV_C3CNT_OFFSET));
}
break;
case CLKMGR_CLKSRC_OSC1:
clock = cm_get_osc_clk_hz();
break;
case CLKMGR_CLKSRC_INTOSC:
clock = cm_get_intosc_clk_hz();
break;
case CLKMGR_CLKSRC_FPGA:
clock = cm_get_fpga_clk_hz();
break;
}
clock /= 1 + div;
return clock;
}
static ulong socfpga_clk_get_rate(struct clk *clk)
{
struct socfpga_clk_plat *plat = dev_get_plat(clk->dev);
switch (clk->id) {
case N5X_MPU_CLK:
return clk_get_mpu_clk_hz(plat);
case N5X_L4_MAIN_CLK:
return clk_get_l4_main_clk_hz(plat);
case N5X_L4_SYS_FREE_CLK:
return clk_get_l4_sys_free_clk_hz(plat);
case N5X_L4_MP_CLK:
return clk_get_l4_mp_clk_hz(plat);
case N5X_L4_SP_CLK:
return clk_get_l4_sp_clk_hz(plat);
case N5X_SDMMC_CLK:
return clk_get_sdmmc_clk_hz(plat);
case N5X_EMAC0_CLK:
case N5X_EMAC1_CLK:
case N5X_EMAC2_CLK:
return clk_get_emac_clk_hz(plat, clk->id);
case N5X_USB_CLK:
case N5X_NAND_X_CLK:
return clk_get_l4_mp_clk_hz(plat);
case N5X_NAND_CLK:
return clk_get_l4_mp_clk_hz(plat) / 4;
default:
return -ENXIO;
}
}
static int socfpga_clk_enable(struct clk *clk)
{
return 0;
}
static int socfpga_clk_probe(struct udevice *dev)
{
const struct cm_config *cm_default_cfg = cm_get_default_config();
clk_basic_init(dev, cm_default_cfg);
return 0;
}
static int socfpga_clk_of_to_plat(struct udevice *dev)
{
struct socfpga_clk_plat *plat = dev_get_plat(dev);
fdt_addr_t addr;
addr = devfdt_get_addr(dev);
if (addr == FDT_ADDR_T_NONE)
return -EINVAL;
plat->regs = (void __iomem *)addr;
return 0;
}
static struct clk_ops socfpga_clk_ops = {
.enable = socfpga_clk_enable,
.get_rate = socfpga_clk_get_rate,
};
static const struct udevice_id socfpga_clk_match[] = {
{ .compatible = "intel,n5x-clkmgr" },
{}
};
U_BOOT_DRIVER(socfpga_n5x_clk) = {
.name = "clk-n5x",
.id = UCLASS_CLK,
.of_match = socfpga_clk_match,
.ops = &socfpga_clk_ops,
.probe = socfpga_clk_probe,
.of_to_plat = socfpga_clk_of_to_plat,
.plat_auto = sizeof(struct socfpga_clk_plat),
};
|