summaryrefslogtreecommitdiff
path: root/arch/arm/mach-socfpga/clock_manager_s10.c
blob: 3ba2a00c02a39f9d94bd64e5f2a21234736e89f9 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2016-2018 Intel Corporation <www.intel.com>
 *
 */

#include <common.h>
#include <asm/io.h>
#include <asm/arch/clock_manager.h>
#include <asm/arch/handoff_s10.h>
#include <asm/arch/system_manager.h>

DECLARE_GLOBAL_DATA_PTR;

static const struct socfpga_clock_manager *clock_manager_base =
	(struct socfpga_clock_manager *)SOCFPGA_CLKMGR_ADDRESS;
static const struct socfpga_system_manager *sysmgr_regs =
		(struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;

/*
 * function to write the bypass register which requires a poll of the
 * busy bit
 */
static void cm_write_bypass_mainpll(u32 val)
{
	writel(val, &clock_manager_base->main_pll.bypass);
	cm_wait_for_fsm();
}

static void cm_write_bypass_perpll(u32 val)
{
	writel(val, &clock_manager_base->per_pll.bypass);
	cm_wait_for_fsm();
}

/* function to write the ctrl register which requires a poll of the busy bit */
static void cm_write_ctrl(u32 val)
{
	writel(val, &clock_manager_base->ctrl);
	cm_wait_for_fsm();
}

/*
 * Setup clocks while making no assumptions about previous state of the clocks.
 */
void cm_basic_init(const struct cm_config * const cfg)
{
	u32 mdiv, refclkdiv, mscnt, hscnt, vcocalib;

	if (cfg == 0)
		return;

	/* Put all plls in bypass */
	cm_write_bypass_mainpll(CLKMGR_BYPASS_MAINPLL_ALL);
	cm_write_bypass_perpll(CLKMGR_BYPASS_PERPLL_ALL);

	/* setup main PLL dividers where calculate the vcocalib value */
	mdiv = (cfg->main_pll_fdbck >> CLKMGR_FDBCK_MDIV_OFFSET) &
		CLKMGR_FDBCK_MDIV_MASK;
	refclkdiv = (cfg->main_pll_pllglob >> CLKMGR_PLLGLOB_REFCLKDIV_OFFSET) &
		     CLKMGR_PLLGLOB_REFCLKDIV_MASK;
	mscnt = CLKMGR_MSCNT_CONST / (CLKMGR_MDIV_CONST + mdiv) / refclkdiv;
	hscnt = (mdiv + CLKMGR_MDIV_CONST) * mscnt / refclkdiv -
		CLKMGR_HSCNT_CONST;
	vcocalib = (hscnt & CLKMGR_VCOCALIB_HSCNT_MASK) |
		   ((mscnt & CLKMGR_VCOCALIB_MSCNT_MASK) <<
		   CLKMGR_VCOCALIB_MSCNT_OFFSET);

	writel((cfg->main_pll_pllglob & ~CLKMGR_PLLGLOB_PD_MASK &
		~CLKMGR_PLLGLOB_RST_MASK),
		&clock_manager_base->main_pll.pllglob);
	writel(cfg->main_pll_fdbck, &clock_manager_base->main_pll.fdbck);
	writel(vcocalib, &clock_manager_base->main_pll.vcocalib);
	writel(cfg->main_pll_pllc0, &clock_manager_base->main_pll.pllc0);
	writel(cfg->main_pll_pllc1, &clock_manager_base->main_pll.pllc1);
	writel(cfg->main_pll_nocdiv, &clock_manager_base->main_pll.nocdiv);

	/* setup peripheral PLL dividers */
	/* calculate the vcocalib value */
	mdiv = (cfg->per_pll_fdbck >> CLKMGR_FDBCK_MDIV_OFFSET) &
		CLKMGR_FDBCK_MDIV_MASK;
	refclkdiv = (cfg->per_pll_pllglob >> CLKMGR_PLLGLOB_REFCLKDIV_OFFSET) &
		     CLKMGR_PLLGLOB_REFCLKDIV_MASK;
	mscnt = CLKMGR_MSCNT_CONST / (CLKMGR_MDIV_CONST + mdiv) / refclkdiv;
	hscnt = (mdiv + CLKMGR_MDIV_CONST) * mscnt / refclkdiv -
		CLKMGR_HSCNT_CONST;
	vcocalib = (hscnt & CLKMGR_VCOCALIB_HSCNT_MASK) |
		   ((mscnt & CLKMGR_VCOCALIB_MSCNT_MASK) <<
		   CLKMGR_VCOCALIB_MSCNT_OFFSET);

	writel((cfg->per_pll_pllglob & ~CLKMGR_PLLGLOB_PD_MASK &
		~CLKMGR_PLLGLOB_RST_MASK),
		&clock_manager_base->per_pll.pllglob);
	writel(cfg->per_pll_fdbck, &clock_manager_base->per_pll.fdbck);
	writel(vcocalib, &clock_manager_base->per_pll.vcocalib);
	writel(cfg->per_pll_pllc0, &clock_manager_base->per_pll.pllc0);
	writel(cfg->per_pll_pllc1, &clock_manager_base->per_pll.pllc1);
	writel(cfg->per_pll_emacctl, &clock_manager_base->per_pll.emacctl);
	writel(cfg->per_pll_gpiodiv, &clock_manager_base->per_pll.gpiodiv);

	/* Take both PLL out of reset and power up */
	setbits_le32(&clock_manager_base->main_pll.pllglob,
		     CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);
	setbits_le32(&clock_manager_base->per_pll.pllglob,
		     CLKMGR_PLLGLOB_PD_MASK | CLKMGR_PLLGLOB_RST_MASK);

#define LOCKED_MASK \
	(CLKMGR_STAT_MAINPLL_LOCKED | \
	CLKMGR_STAT_PERPLL_LOCKED)

	cm_wait_for_lock(LOCKED_MASK);

	/*
	 * Dividers for C2 to C9 only init after PLLs are lock. As dividers
	 * only take effect upon value change, we shall set a maximum value as
	 * default value.
	 */
	writel(0xff, &clock_manager_base->main_pll.mpuclk);
	writel(0xff, &clock_manager_base->main_pll.nocclk);
	writel(0xff, &clock_manager_base->main_pll.cntr2clk);
	writel(0xff, &clock_manager_base->main_pll.cntr3clk);
	writel(0xff, &clock_manager_base->main_pll.cntr4clk);
	writel(0xff, &clock_manager_base->main_pll.cntr5clk);
	writel(0xff, &clock_manager_base->main_pll.cntr6clk);
	writel(0xff, &clock_manager_base->main_pll.cntr7clk);
	writel(0xff, &clock_manager_base->main_pll.cntr8clk);
	writel(0xff, &clock_manager_base->main_pll.cntr9clk);
	writel(0xff, &clock_manager_base->per_pll.cntr2clk);
	writel(0xff, &clock_manager_base->per_pll.cntr3clk);
	writel(0xff, &clock_manager_base->per_pll.cntr4clk);
	writel(0xff, &clock_manager_base->per_pll.cntr5clk);
	writel(0xff, &clock_manager_base->per_pll.cntr6clk);
	writel(0xff, &clock_manager_base->per_pll.cntr7clk);
	writel(0xff, &clock_manager_base->per_pll.cntr8clk);
	writel(0xff, &clock_manager_base->per_pll.cntr9clk);

	writel(cfg->main_pll_mpuclk, &clock_manager_base->main_pll.mpuclk);
	writel(cfg->main_pll_nocclk, &clock_manager_base->main_pll.nocclk);
	writel(cfg->main_pll_cntr2clk, &clock_manager_base->main_pll.cntr2clk);
	writel(cfg->main_pll_cntr3clk, &clock_manager_base->main_pll.cntr3clk);
	writel(cfg->main_pll_cntr4clk, &clock_manager_base->main_pll.cntr4clk);
	writel(cfg->main_pll_cntr5clk, &clock_manager_base->main_pll.cntr5clk);
	writel(cfg->main_pll_cntr6clk, &clock_manager_base->main_pll.cntr6clk);
	writel(cfg->main_pll_cntr7clk, &clock_manager_base->main_pll.cntr7clk);
	writel(cfg->main_pll_cntr8clk, &clock_manager_base->main_pll.cntr8clk);
	writel(cfg->main_pll_cntr9clk, &clock_manager_base->main_pll.cntr9clk);
	writel(cfg->per_pll_cntr2clk, &clock_manager_base->per_pll.cntr2clk);
	writel(cfg->per_pll_cntr3clk, &clock_manager_base->per_pll.cntr3clk);
	writel(cfg->per_pll_cntr4clk, &clock_manager_base->per_pll.cntr4clk);
	writel(cfg->per_pll_cntr5clk, &clock_manager_base->per_pll.cntr5clk);
	writel(cfg->per_pll_cntr6clk, &clock_manager_base->per_pll.cntr6clk);
	writel(cfg->per_pll_cntr7clk, &clock_manager_base->per_pll.cntr7clk);
	writel(cfg->per_pll_cntr8clk, &clock_manager_base->per_pll.cntr8clk);
	writel(cfg->per_pll_cntr9clk, &clock_manager_base->per_pll.cntr9clk);

	/* Take all PLLs out of bypass */
	cm_write_bypass_mainpll(0);
	cm_write_bypass_perpll(0);

	/* clear safe mode / out of boot mode */
	cm_write_ctrl(readl(&clock_manager_base->ctrl)
			& ~(CLKMGR_CTRL_SAFEMODE));

	/* Now ungate non-hw-managed clocks */
	writel(~0, &clock_manager_base->main_pll.en);
	writel(~0, &clock_manager_base->per_pll.en);

	/* Clear the loss of lock bits (write 1 to clear) */
	writel(CLKMGR_INTER_PERPLLLOST_MASK | CLKMGR_INTER_MAINPLLLOST_MASK,
	       &clock_manager_base->intrclr);
}

static unsigned long cm_get_main_vco_clk_hz(void)
{
	 unsigned long fref, refdiv, mdiv, reg, vco;

	reg = readl(&clock_manager_base->main_pll.pllglob);

	fref = (reg >> CLKMGR_PLLGLOB_VCO_PSRC_OFFSET) &
		CLKMGR_PLLGLOB_VCO_PSRC_MASK;
	switch (fref) {
	case CLKMGR_VCO_PSRC_EOSC1:
		fref = cm_get_osc_clk_hz();
		break;
	case CLKMGR_VCO_PSRC_INTOSC:
		fref = cm_get_intosc_clk_hz();
		break;
	case CLKMGR_VCO_PSRC_F2S:
		fref = cm_get_fpga_clk_hz();
		break;
	}

	refdiv = (reg >> CLKMGR_PLLGLOB_REFCLKDIV_OFFSET) &
		  CLKMGR_PLLGLOB_REFCLKDIV_MASK;

	reg = readl(&clock_manager_base->main_pll.fdbck);
	mdiv = (reg >> CLKMGR_FDBCK_MDIV_OFFSET) & CLKMGR_FDBCK_MDIV_MASK;

	vco = fref / refdiv;
	vco = vco * (CLKMGR_MDIV_CONST + mdiv);
	return vco;
}

static unsigned long cm_get_per_vco_clk_hz(void)
{
	unsigned long fref, refdiv, mdiv, reg, vco;

	reg = readl(&clock_manager_base->per_pll.pllglob);

	fref = (reg >> CLKMGR_PLLGLOB_VCO_PSRC_OFFSET) &
		CLKMGR_PLLGLOB_VCO_PSRC_MASK;
	switch (fref) {
	case CLKMGR_VCO_PSRC_EOSC1:
		fref = cm_get_osc_clk_hz();
		break;
	case CLKMGR_VCO_PSRC_INTOSC:
		fref = cm_get_intosc_clk_hz();
		break;
	case CLKMGR_VCO_PSRC_F2S:
		fref = cm_get_fpga_clk_hz();
		break;
	}

	refdiv = (reg >> CLKMGR_PLLGLOB_REFCLKDIV_OFFSET) &
		  CLKMGR_PLLGLOB_REFCLKDIV_MASK;

	reg = readl(&clock_manager_base->per_pll.fdbck);
	mdiv = (reg >> CLKMGR_FDBCK_MDIV_OFFSET) & CLKMGR_FDBCK_MDIV_MASK;

	vco = fref / refdiv;
	vco = vco * (CLKMGR_MDIV_CONST + mdiv);
	return vco;
}

unsigned long cm_get_mpu_clk_hz(void)
{
	unsigned long clock = readl(&clock_manager_base->main_pll.mpuclk);

	clock = (clock >> CLKMGR_CLKSRC_OFFSET) & CLKMGR_CLKSRC_MASK;

	switch (clock) {
	case CLKMGR_CLKSRC_MAIN:
		clock = cm_get_main_vco_clk_hz();
		clock /= (readl(&clock_manager_base->main_pll.pllc0) &
			  CLKMGR_PLLC0_DIV_MASK);
		break;

	case CLKMGR_CLKSRC_PER:
		clock = cm_get_per_vco_clk_hz();
		clock /= (readl(&clock_manager_base->per_pll.pllc0) &
			  CLKMGR_CLKCNT_MSK);
		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 + (readl(&clock_manager_base->main_pll.mpuclk) &
		CLKMGR_CLKCNT_MSK);
	return clock;
}

unsigned int cm_get_l3_main_clk_hz(void)
{
	u32 clock = readl(&clock_manager_base->main_pll.nocclk);

	clock = (clock >> CLKMGR_CLKSRC_OFFSET) & CLKMGR_CLKSRC_MASK;

	switch (clock) {
	case CLKMGR_CLKSRC_MAIN:
		clock = cm_get_main_vco_clk_hz();
		clock /= (readl(&clock_manager_base->main_pll.pllc1) &
			  CLKMGR_PLLC0_DIV_MASK);
		break;

	case CLKMGR_CLKSRC_PER:
		clock = cm_get_per_vco_clk_hz();
		clock /= (readl(&clock_manager_base->per_pll.pllc1) &
			  CLKMGR_CLKCNT_MSK);
		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 + (readl(&clock_manager_base->main_pll.nocclk) &
		CLKMGR_CLKCNT_MSK);
	return clock;
}

unsigned int cm_get_mmc_controller_clk_hz(void)
{
	u32 clock = readl(&clock_manager_base->per_pll.cntr6clk);

	clock = (clock >> CLKMGR_CLKSRC_OFFSET) & CLKMGR_CLKSRC_MASK;

	switch (clock) {
	case CLKMGR_CLKSRC_MAIN:
		clock = cm_get_l3_main_clk_hz();
		clock /= 1 + (readl(&clock_manager_base->main_pll.cntr6clk) &
			CLKMGR_CLKCNT_MSK);
		break;

	case CLKMGR_CLKSRC_PER:
		clock = cm_get_l3_main_clk_hz();
		clock /= 1 + (readl(&clock_manager_base->per_pll.cntr6clk) &
			CLKMGR_CLKCNT_MSK);
		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;
	}
	return clock / 4;
}

unsigned int cm_get_l4_sp_clk_hz(void)
{
	u32 clock = cm_get_l3_main_clk_hz();

	clock /= (1 << ((readl(&clock_manager_base->main_pll.nocdiv) >>
		  CLKMGR_NOCDIV_L4SPCLK_OFFSET) & CLKMGR_CLKCNT_MSK));
	return clock;
}

unsigned int cm_get_qspi_controller_clk_hz(void)
{
	return readl(&sysmgr_regs->boot_scratch_cold0);
}

unsigned int cm_get_spi_controller_clk_hz(void)
{
	u32 clock = cm_get_l3_main_clk_hz();

	clock /= (1 << ((readl(&clock_manager_base->main_pll.nocdiv) >>
		  CLKMGR_NOCDIV_L4MAIN_OFFSET) & CLKMGR_CLKCNT_MSK));
	return clock;
}

unsigned int cm_get_l4_sys_free_clk_hz(void)
{
	return cm_get_l3_main_clk_hz() / 4;
}

void cm_print_clock_quick_summary(void)
{
	printf("MPU         %d kHz\n", (u32)(cm_get_mpu_clk_hz() / 1000));
	printf("L3 main     %d kHz\n", cm_get_l3_main_clk_hz() / 1000);
	printf("Main VCO    %d kHz\n", (u32)(cm_get_main_vco_clk_hz() / 1000));
	printf("Per VCO     %d kHz\n", (u32)(cm_get_per_vco_clk_hz() / 1000));
	printf("EOSC1       %d kHz\n", cm_get_osc_clk_hz() / 1000);
	printf("HPS MMC     %d kHz\n", cm_get_mmc_controller_clk_hz() / 1000);
	printf("UART        %d kHz\n", cm_get_l4_sp_clk_hz() / 1000);
}