stm32mp1_clk.c 45.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2018-2019, STMicroelectronics - All Rights Reserved
3
4
5
6
7
8
9
 *
 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
#include <stdint.h>
10
#include <stdio.h>
11
12
13

#include <libfdt.h>

14
15
#include <platform_def.h>

16
17
18
19
20
#include <arch.h>
#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/generic_delay_timer.h>
Yann Gautier's avatar
Yann Gautier committed
21
#include <drivers/st/stm32mp_clkfunc.h>
22
23
24
25
#include <drivers/st/stm32mp1_clk.h>
#include <drivers/st/stm32mp1_rcc.h>
#include <dt-bindings/clock/stm32mp1-clksrc.h>
#include <lib/mmio.h>
Yann Gautier's avatar
Yann Gautier committed
26
#include <lib/spinlock.h>
27
28
29
#include <lib/utils_def.h>
#include <plat/common/platform.h>

30
#define MAX_HSI_HZ		64000000
Yann Gautier's avatar
Yann Gautier committed
31
#define USB_PHY_48_MHZ		48000000
32

33
34
#define TIMEOUT_US_200MS	U(200000)
#define TIMEOUT_US_1S		U(1000000)
35

36
37
38
39
40
#define PLLRDY_TIMEOUT		TIMEOUT_US_200MS
#define CLKSRC_TIMEOUT		TIMEOUT_US_200MS
#define CLKDIV_TIMEOUT		TIMEOUT_US_200MS
#define HSIDIV_TIMEOUT		TIMEOUT_US_200MS
#define OSCRDY_TIMEOUT		TIMEOUT_US_1S
41

42
43
44
45
46
47
48
49
50
const char *stm32mp_osc_node_label[NB_OSC] = {
	[_LSI] = "clk-lsi",
	[_LSE] = "clk-lse",
	[_HSI] = "clk-hsi",
	[_HSE] = "clk-hse",
	[_CSI] = "clk-csi",
	[_I2S_CKIN] = "i2s_ckin",
};

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
enum stm32mp1_parent_id {
/* Oscillators are defined in enum stm32mp_osc_id */

/* Other parent source */
	_HSI_KER = NB_OSC,
	_HSE_KER,
	_HSE_KER_DIV2,
	_CSI_KER,
	_PLL1_P,
	_PLL1_Q,
	_PLL1_R,
	_PLL2_P,
	_PLL2_Q,
	_PLL2_R,
	_PLL3_P,
	_PLL3_Q,
	_PLL3_R,
	_PLL4_P,
	_PLL4_Q,
	_PLL4_R,
	_ACLK,
	_PCLK1,
	_PCLK2,
	_PCLK3,
	_PCLK4,
	_PCLK5,
	_HCLK6,
	_HCLK2,
	_CK_PER,
	_CK_MPU,
81
	_CK_MCU,
Yann Gautier's avatar
Yann Gautier committed
82
	_USB_PHY_48,
83
84
85
86
	_PARENT_NB,
	_UNKNOWN_ID = 0xff,
};

Yann Gautier's avatar
Yann Gautier committed
87
/* Lists only the parent clock we are interested in */
88
enum stm32mp1_parent_sel {
Yann Gautier's avatar
Yann Gautier committed
89
90
91
	_I2C12_SEL,
	_I2C35_SEL,
	_STGEN_SEL,
92
	_I2C46_SEL,
Yann Gautier's avatar
Yann Gautier committed
93
	_SPI6_SEL,
94
	_UART1_SEL,
Yann Gautier's avatar
Yann Gautier committed
95
	_RNG1_SEL,
96
97
98
99
100
101
102
103
	_UART6_SEL,
	_UART24_SEL,
	_UART35_SEL,
	_UART78_SEL,
	_SDMMC12_SEL,
	_SDMMC3_SEL,
	_QSPI_SEL,
	_FMC_SEL,
104
105
	_AXIS_SEL,
	_MCUS_SEL,
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	_USBPHY_SEL,
	_USBO_SEL,
	_PARENT_SEL_NB,
	_UNKNOWN_SEL = 0xff,
};

enum stm32mp1_pll_id {
	_PLL1,
	_PLL2,
	_PLL3,
	_PLL4,
	_PLL_NB
};

enum stm32mp1_div_id {
	_DIV_P,
	_DIV_Q,
	_DIV_R,
	_DIV_NB,
};

enum stm32mp1_clksrc_id {
	CLKSRC_MPU,
	CLKSRC_AXI,
130
	CLKSRC_MCU,
131
132
133
134
135
136
137
138
139
140
141
142
	CLKSRC_PLL12,
	CLKSRC_PLL3,
	CLKSRC_PLL4,
	CLKSRC_RTC,
	CLKSRC_MCO1,
	CLKSRC_MCO2,
	CLKSRC_NB
};

enum stm32mp1_clkdiv_id {
	CLKDIV_MPU,
	CLKDIV_AXI,
143
	CLKDIV_MCU,
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
	CLKDIV_APB1,
	CLKDIV_APB2,
	CLKDIV_APB3,
	CLKDIV_APB4,
	CLKDIV_APB5,
	CLKDIV_RTC,
	CLKDIV_MCO1,
	CLKDIV_MCO2,
	CLKDIV_NB
};

enum stm32mp1_pllcfg {
	PLLCFG_M,
	PLLCFG_N,
	PLLCFG_P,
	PLLCFG_Q,
	PLLCFG_R,
	PLLCFG_O,
	PLLCFG_NB
};

enum stm32mp1_pllcsg {
	PLLCSG_MOD_PER,
	PLLCSG_INC_STEP,
	PLLCSG_SSCG_MODE,
	PLLCSG_NB
};

enum stm32mp1_plltype {
	PLL_800,
	PLL_1600,
	PLL_TYPE_NB
};

struct stm32mp1_pll {
	uint8_t refclk_min;
	uint8_t refclk_max;
	uint8_t divn_max;
};

struct stm32mp1_clk_gate {
	uint16_t offset;
	uint8_t bit;
	uint8_t index;
	uint8_t set_clr;
Yann Gautier's avatar
Yann Gautier committed
189
190
	uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
	uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
};

struct stm32mp1_clk_sel {
	uint16_t offset;
	uint8_t src;
	uint8_t msk;
	uint8_t nb_parent;
	const uint8_t *parent;
};

#define REFCLK_SIZE 4
struct stm32mp1_clk_pll {
	enum stm32mp1_plltype plltype;
	uint16_t rckxselr;
	uint16_t pllxcfgr1;
	uint16_t pllxcfgr2;
	uint16_t pllxfracr;
	uint16_t pllxcr;
	uint16_t pllxcsgr;
	enum stm32mp_osc_id refclk[REFCLK_SIZE];
};

Yann Gautier's avatar
Yann Gautier committed
213
214
/* Clocks with selectable source and non set/clr register access */
#define _CLK_SELEC(off, b, idx, s)			\
215
216
217
218
219
220
221
222
223
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 0,				\
		.sel = (s),				\
		.fixed = _UNKNOWN_ID,			\
	}

Yann Gautier's avatar
Yann Gautier committed
224
225
/* Clocks with fixed source and non set/clr register access */
#define _CLK_FIXED(off, b, idx, f)			\
226
227
228
229
230
231
232
233
234
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 0,				\
		.sel = _UNKNOWN_SEL,			\
		.fixed = (f),				\
	}

Yann Gautier's avatar
Yann Gautier committed
235
236
/* Clocks with selectable source and set/clr register access */
#define _CLK_SC_SELEC(off, b, idx, s)			\
237
238
239
240
241
242
243
244
245
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 1,				\
		.sel = (s),				\
		.fixed = _UNKNOWN_ID,			\
	}

Yann Gautier's avatar
Yann Gautier committed
246
247
/* Clocks with fixed source and set/clr register access */
#define _CLK_SC_FIXED(off, b, idx, f)			\
248
249
250
251
252
253
254
255
256
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 1,				\
		.sel = _UNKNOWN_SEL,			\
		.fixed = (f),				\
	}

257
258
259
260
261
262
263
#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents)		\
	[_ ## _label ## _SEL] = {				\
		.offset = _rcc_selr,				\
		.src = _rcc_selr ## _ ## _label ## SRC_SHIFT,	\
		.msk = _rcc_selr ## _ ## _label ## SRC_MASK,	\
		.parent = (_parents),				\
		.nb_parent = ARRAY_SIZE(_parents)		\
264
265
	}

Yann Gautier's avatar
Yann Gautier committed
266
267
268
#define _CLK_PLL(idx, type, off1, off2, off3,		\
		 off4, off5, off6,			\
		 p1, p2, p3, p4)			\
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
	[(idx)] = {					\
		.plltype = (type),			\
		.rckxselr = (off1),			\
		.pllxcfgr1 = (off2),			\
		.pllxcfgr2 = (off3),			\
		.pllxfracr = (off4),			\
		.pllxcr = (off5),			\
		.pllxcsgr = (off6),			\
		.refclk[0] = (p1),			\
		.refclk[1] = (p2),			\
		.refclk[2] = (p3),			\
		.refclk[3] = (p4),			\
	}

static const uint8_t stm32mp1_clks[][2] = {
Yann Gautier's avatar
Yann Gautier committed
284
285
286
	{ CK_PER, _CK_PER },
	{ CK_MPU, _CK_MPU },
	{ CK_AXI, _ACLK },
287
	{ CK_MCU, _CK_MCU },
Yann Gautier's avatar
Yann Gautier committed
288
289
290
291
292
293
	{ CK_HSE, _HSE },
	{ CK_CSI, _CSI },
	{ CK_LSI, _LSI },
	{ CK_LSE, _LSE },
	{ CK_HSI, _HSI },
	{ CK_HSE_DIV2, _HSE_KER_DIV2 },
294
295
};

Yann Gautier's avatar
Yann Gautier committed
296
297
#define NB_GATES	ARRAY_SIZE(stm32mp1_clk_gate)

298
static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautier's avatar
Yann Gautier committed
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
	_CLK_FIXED(RCC_DDRITFCR, 0, DDRC1, _ACLK),
	_CLK_FIXED(RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
	_CLK_FIXED(RCC_DDRITFCR, 2, DDRC2, _ACLK),
	_CLK_FIXED(RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
	_CLK_FIXED(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
	_CLK_FIXED(RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
	_CLK_FIXED(RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
	_CLK_FIXED(RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
	_CLK_FIXED(RCC_DDRITFCR, 8, AXIDCG, _ACLK),
	_CLK_FIXED(RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
	_CLK_FIXED(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),

	_CLK_SC_FIXED(RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
	_CLK_SC_SELEC(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),

	_CLK_SC_FIXED(RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
	_CLK_SC_SELEC(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),

326
327
	_CLK_SC_FIXED(RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),

Yann Gautier's avatar
Yann Gautier committed
328
329
330
331
332
333
334
	_CLK_SC_SELEC(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),

	_CLK_SC_SELEC(RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
	_CLK_SC_SELEC(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
	_CLK_SC_SELEC(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
335
	_CLK_SC_SELEC(RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
Yann Gautier's avatar
Yann Gautier committed
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
	_CLK_SC_FIXED(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
	_CLK_SC_SELEC(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),

	_CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),

	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),

	_CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
	_CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
	_CLK_SC_SELEC(RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
	_CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),

	_CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
	_CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),

	_CLK_SELEC(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
};

static const uint8_t i2c12_parents[] = {
	_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
};

static const uint8_t i2c35_parents[] = {
	_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
};

static const uint8_t stgen_parents[] = {
	_HSI_KER, _HSE_KER
};

static const uint8_t i2c46_parents[] = {
	_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
};

static const uint8_t spi6_parents[] = {
	_PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
};

static const uint8_t usart1_parents[] = {
	_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
};

static const uint8_t rng1_parents[] = {
	_CSI, _PLL4_R, _LSE, _LSI
};

static const uint8_t uart6_parents[] = {
	_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
};

static const uint8_t uart234578_parents[] = {
	_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
};

static const uint8_t sdmmc12_parents[] = {
	_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
};

static const uint8_t sdmmc3_parents[] = {
	_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
};

static const uint8_t qspi_parents[] = {
	_ACLK, _PLL3_R, _PLL4_P, _CK_PER
};

static const uint8_t fmc_parents[] = {
	_ACLK, _PLL3_R, _PLL4_P, _CK_PER
};

static const uint8_t ass_parents[] = {
	_HSI, _HSE, _PLL2
428
429
};

430
431
432
433
static const uint8_t mss_parents[] = {
	_HSI, _HSE, _CSI, _PLL3
};

Yann Gautier's avatar
Yann Gautier committed
434
435
436
437
438
439
440
static const uint8_t usbphy_parents[] = {
	_HSE_KER, _PLL4_R, _HSE_KER_DIV2
};

static const uint8_t usbo_parents[] = {
	_PLL4_R, _USB_PHY_48
};
441
442

static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
	_CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
	_CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
	_CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
	_CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
	_CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
	_CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
	_CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
	_CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
	_CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
	_CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
	_CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
	_CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
	_CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
	_CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
	_CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
	_CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, ass_parents),
	_CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mss_parents),
	_CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
	_CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
};

/* Define characteristic of PLL according type */
#define DIVN_MIN	24
static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
	[PLL_800] = {
		.refclk_min = 4,
		.refclk_max = 16,
		.divn_max = 99,
	},
	[PLL_1600] = {
		.refclk_min = 8,
		.refclk_max = 16,
		.divn_max = 199,
	},
};

/* PLLNCFGR2 register divider by output */
static const uint8_t pllncfgr2[_DIV_NB] = {
	[_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
	[_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautier's avatar
Yann Gautier committed
483
	[_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
484
485
486
};

static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautier's avatar
Yann Gautier committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
	_CLK_PLL(_PLL1, PLL_1600,
		 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
		 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
		 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
	_CLK_PLL(_PLL2, PLL_1600,
		 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
		 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
		 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
	_CLK_PLL(_PLL3, PLL_800,
		 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
		 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
		 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
	_CLK_PLL(_PLL4, PLL_800,
		 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
		 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
		 _HSI, _HSE, _CSI, _I2S_CKIN),
503
504
505
};

/* Prescaler table lookups for clock computation */
506
507
508
509
/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
static const uint8_t stm32mp1_mcu_div[16] = {
	0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
};
510
511
512
513
514
515
516
517
518
519
520
521
522

/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
static const uint8_t stm32mp1_mpu_apbx_div[8] = {
	0, 1, 2, 3, 4, 4, 4, 4
};

/* div = /1 /2 /3 /4 */
static const uint8_t stm32mp1_axi_div[8] = {
	1, 2, 3, 4, 4, 4, 4, 4
};

Yann Gautier's avatar
Yann Gautier committed
523
524
525
526
527
528
529
530
531
532
/* RCC clock device driver private */
static unsigned long stm32mp1_osc[NB_OSC];
static struct spinlock reg_lock;
static unsigned int gate_refcounts[NB_GATES];
static struct spinlock refcount_lock;

static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
{
	return &stm32mp1_clk_gate[idx];
}
533

Yann Gautier's avatar
Yann Gautier committed
534
535
536
537
static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
{
	return &stm32mp1_clk_sel[idx];
}
538

Yann Gautier's avatar
Yann Gautier committed
539
540
541
542
543
544
545
static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
{
	return &stm32mp1_clk_pll[idx];
}

static void stm32mp1_clk_lock(struct spinlock *lock)
{
546
547
548
	if (stm32mp_lock_available()) {
		/* Assume interrupts are masked */
		spin_lock(lock);
Yann Gautier's avatar
Yann Gautier committed
549
550
551
552
553
	}
}

static void stm32mp1_clk_unlock(struct spinlock *lock)
{
554
555
	if (stm32mp_lock_available()) {
		spin_unlock(lock);
Yann Gautier's avatar
Yann Gautier committed
556
557
558
559
560
561
562
563
564
565
	}
}

bool stm32mp1_rcc_is_secure(void)
{
	uintptr_t rcc_base = stm32mp_rcc_base();

	return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_TZEN) != 0;
}

566
567
568
569
570
571
572
bool stm32mp1_rcc_is_mckprot(void)
{
	uintptr_t rcc_base = stm32mp_rcc_base();

	return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_MCKPROT) != 0;
}

Yann Gautier's avatar
Yann Gautier committed
573
574
575
576
577
578
579
580
581
582
583
void stm32mp1_clk_rcc_regs_lock(void)
{
	stm32mp1_clk_lock(&reg_lock);
}

void stm32mp1_clk_rcc_regs_unlock(void)
{
	stm32mp1_clk_unlock(&reg_lock);
}

static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
584
585
586
587
588
{
	if (idx >= NB_OSC) {
		return 0;
	}

Yann Gautier's avatar
Yann Gautier committed
589
	return stm32mp1_osc[idx];
590
591
}

Yann Gautier's avatar
Yann Gautier committed
592
static int stm32mp1_clk_get_gated_id(unsigned long id)
593
{
Yann Gautier's avatar
Yann Gautier committed
594
	unsigned int i;
595

Yann Gautier's avatar
Yann Gautier committed
596
597
	for (i = 0U; i < NB_GATES; i++) {
		if (gate_ref(i)->index == id) {
598
599
600
601
602
603
604
605
606
			return i;
		}
	}

	ERROR("%s: clk id %d not found\n", __func__, (uint32_t)id);

	return -EINVAL;
}

Yann Gautier's avatar
Yann Gautier committed
607
static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
608
{
Yann Gautier's avatar
Yann Gautier committed
609
	return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
610
611
}

Yann Gautier's avatar
Yann Gautier committed
612
static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
613
{
Yann Gautier's avatar
Yann Gautier committed
614
	return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
615
616
}

Yann Gautier's avatar
Yann Gautier committed
617
static int stm32mp1_clk_get_parent(unsigned long id)
618
{
Yann Gautier's avatar
Yann Gautier committed
619
	const struct stm32mp1_clk_sel *sel;
620
621
622
623
	uint32_t j, p_sel;
	int i;
	enum stm32mp1_parent_id p;
	enum stm32mp1_parent_sel s;
Yann Gautier's avatar
Yann Gautier committed
624
	uintptr_t rcc_base = stm32mp_rcc_base();
625

Yann Gautier's avatar
Yann Gautier committed
626
	for (j = 0U; j < ARRAY_SIZE(stm32mp1_clks); j++) {
627
628
629
630
631
		if (stm32mp1_clks[j][0] == id) {
			return (int)stm32mp1_clks[j][1];
		}
	}

Yann Gautier's avatar
Yann Gautier committed
632
	i = stm32mp1_clk_get_gated_id(id);
633
	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
634
		panic();
635
636
	}

Yann Gautier's avatar
Yann Gautier committed
637
	p = stm32mp1_clk_get_fixed_parent(i);
638
639
640
641
	if (p < _PARENT_NB) {
		return (int)p;
	}

Yann Gautier's avatar
Yann Gautier committed
642
643
	s = stm32mp1_clk_get_sel(i);
	if (s == _UNKNOWN_SEL) {
644
645
		return -EINVAL;
	}
Yann Gautier's avatar
Yann Gautier committed
646
647
	if (s >= _PARENT_SEL_NB) {
		panic();
648
649
	}

Yann Gautier's avatar
Yann Gautier committed
650
	sel = clk_sel_ref(s);
651
	p_sel = (mmio_read_32(rcc_base + sel->offset) & sel->msk) >> sel->src;
Yann Gautier's avatar
Yann Gautier committed
652
653
654
	if (p_sel < sel->nb_parent) {
		return (int)sel->parent[p_sel];
	}
655
656
657
658

	return -EINVAL;
}

Yann Gautier's avatar
Yann Gautier committed
659
static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
660
{
Yann Gautier's avatar
Yann Gautier committed
661
662
	uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
	uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
663

Yann Gautier's avatar
Yann Gautier committed
664
	return stm32mp1_clk_get_fixed(pll->refclk[src]);
665
666
667
668
669
670
671
672
}

/*
 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
 * - PLL3 & PLL4 => return VCO     with Fpll_y_ck = FVCO / (DIVy + 1)
 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
 */
Yann Gautier's avatar
Yann Gautier committed
673
static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
674
675
676
{
	unsigned long refclk, fvco;
	uint32_t cfgr1, fracr, divm, divn;
Yann Gautier's avatar
Yann Gautier committed
677
	uintptr_t rcc_base = stm32mp_rcc_base();
678

Yann Gautier's avatar
Yann Gautier committed
679
680
	cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
	fracr = mmio_read_32(rcc_base + pll->pllxfracr);
681
682
683
684

	divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
	divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;

Yann Gautier's avatar
Yann Gautier committed
685
	refclk = stm32mp1_pll_get_fref(pll);
686
687
688
689
690
691
692
693

	/*
	 * With FRACV :
	 *   Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
	 * Without FRACV
	 *   Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
	 */
	if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
694
695
		uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
				 RCC_PLLNFRACR_FRACV_SHIFT;
696
697
		unsigned long long numerator, denominator;

Yann Gautier's avatar
Yann Gautier committed
698
699
700
		numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
		numerator = refclk * numerator;
		denominator = ((unsigned long long)divm + 1U) << 13;
701
702
703
704
705
706
707
708
		fvco = (unsigned long)(numerator / denominator);
	} else {
		fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
	}

	return fvco;
}

Yann Gautier's avatar
Yann Gautier committed
709
static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
710
711
					    enum stm32mp1_div_id div_id)
{
Yann Gautier's avatar
Yann Gautier committed
712
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
713
714
715
716
717
718
719
	unsigned long dfout;
	uint32_t cfgr2, divy;

	if (div_id >= _DIV_NB) {
		return 0;
	}

Yann Gautier's avatar
Yann Gautier committed
720
	cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
721
722
	divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;

Yann Gautier's avatar
Yann Gautier committed
723
	dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
724
725
726
727

	return dfout;
}

Yann Gautier's avatar
Yann Gautier committed
728
static unsigned long get_clock_rate(int p)
729
730
731
{
	uint32_t reg, clkdiv;
	unsigned long clock = 0;
Yann Gautier's avatar
Yann Gautier committed
732
	uintptr_t rcc_base = stm32mp_rcc_base();
733
734
735
736

	switch (p) {
	case _CK_MPU:
	/* MPU sub system */
Yann Gautier's avatar
Yann Gautier committed
737
		reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
738
739
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_MPCKSELR_HSI:
Yann Gautier's avatar
Yann Gautier committed
740
			clock = stm32mp1_clk_get_fixed(_HSI);
741
742
			break;
		case RCC_MPCKSELR_HSE:
Yann Gautier's avatar
Yann Gautier committed
743
			clock = stm32mp1_clk_get_fixed(_HSE);
744
745
			break;
		case RCC_MPCKSELR_PLL:
Yann Gautier's avatar
Yann Gautier committed
746
			clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
747
748
			break;
		case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautier's avatar
Yann Gautier committed
749
			clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
750

Yann Gautier's avatar
Yann Gautier committed
751
			reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
			clkdiv = reg & RCC_MPUDIV_MASK;
			if (clkdiv != 0U) {
				clock /= stm32mp1_mpu_div[clkdiv];
			}
			break;
		default:
			break;
		}
		break;
	/* AXI sub system */
	case _ACLK:
	case _HCLK2:
	case _HCLK6:
	case _PCLK4:
	case _PCLK5:
Yann Gautier's avatar
Yann Gautier committed
767
		reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
768
769
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_ASSCKSELR_HSI:
Yann Gautier's avatar
Yann Gautier committed
770
			clock = stm32mp1_clk_get_fixed(_HSI);
771
772
			break;
		case RCC_ASSCKSELR_HSE:
Yann Gautier's avatar
Yann Gautier committed
773
			clock = stm32mp1_clk_get_fixed(_HSE);
774
775
			break;
		case RCC_ASSCKSELR_PLL:
Yann Gautier's avatar
Yann Gautier committed
776
			clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
777
778
779
780
781
782
			break;
		default:
			break;
		}

		/* System clock divider */
Yann Gautier's avatar
Yann Gautier committed
783
		reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
784
785
786
787
		clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];

		switch (p) {
		case _PCLK4:
Yann Gautier's avatar
Yann Gautier committed
788
			reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
789
790
791
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		case _PCLK5:
Yann Gautier's avatar
Yann Gautier committed
792
			reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
793
794
795
796
797
798
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		default:
			break;
		}
		break;
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
	/* MCU sub system */
	case _CK_MCU:
	case _PCLK1:
	case _PCLK2:
	case _PCLK3:
		reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_MSSCKSELR_HSI:
			clock = stm32mp1_clk_get_fixed(_HSI);
			break;
		case RCC_MSSCKSELR_HSE:
			clock = stm32mp1_clk_get_fixed(_HSE);
			break;
		case RCC_MSSCKSELR_CSI:
			clock = stm32mp1_clk_get_fixed(_CSI);
			break;
		case RCC_MSSCKSELR_PLL:
			clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
			break;
		default:
			break;
		}

		/* MCU clock divider */
		reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
		clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];

		switch (p) {
		case _PCLK1:
			reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		case _PCLK2:
			reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		case _PCLK3:
			reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		case _CK_MCU:
		default:
			break;
		}
		break;
844
	case _CK_PER:
Yann Gautier's avatar
Yann Gautier committed
845
		reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
846
847
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_CPERCKSELR_HSI:
Yann Gautier's avatar
Yann Gautier committed
848
			clock = stm32mp1_clk_get_fixed(_HSI);
849
850
			break;
		case RCC_CPERCKSELR_HSE:
Yann Gautier's avatar
Yann Gautier committed
851
			clock = stm32mp1_clk_get_fixed(_HSE);
852
853
			break;
		case RCC_CPERCKSELR_CSI:
Yann Gautier's avatar
Yann Gautier committed
854
			clock = stm32mp1_clk_get_fixed(_CSI);
855
856
857
858
859
860
861
			break;
		default:
			break;
		}
		break;
	case _HSI:
	case _HSI_KER:
Yann Gautier's avatar
Yann Gautier committed
862
		clock = stm32mp1_clk_get_fixed(_HSI);
863
864
865
		break;
	case _CSI:
	case _CSI_KER:
Yann Gautier's avatar
Yann Gautier committed
866
		clock = stm32mp1_clk_get_fixed(_CSI);
867
868
869
		break;
	case _HSE:
	case _HSE_KER:
Yann Gautier's avatar
Yann Gautier committed
870
		clock = stm32mp1_clk_get_fixed(_HSE);
871
872
		break;
	case _HSE_KER_DIV2:
Yann Gautier's avatar
Yann Gautier committed
873
		clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
874
875
		break;
	case _LSI:
Yann Gautier's avatar
Yann Gautier committed
876
		clock = stm32mp1_clk_get_fixed(_LSI);
877
878
		break;
	case _LSE:
Yann Gautier's avatar
Yann Gautier committed
879
		clock = stm32mp1_clk_get_fixed(_LSE);
880
881
882
		break;
	/* PLL */
	case _PLL1_P:
Yann Gautier's avatar
Yann Gautier committed
883
		clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
884
885
		break;
	case _PLL1_Q:
Yann Gautier's avatar
Yann Gautier committed
886
		clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
887
888
		break;
	case _PLL1_R:
Yann Gautier's avatar
Yann Gautier committed
889
		clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
890
891
		break;
	case _PLL2_P:
Yann Gautier's avatar
Yann Gautier committed
892
		clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
893
894
		break;
	case _PLL2_Q:
Yann Gautier's avatar
Yann Gautier committed
895
		clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
896
897
		break;
	case _PLL2_R:
Yann Gautier's avatar
Yann Gautier committed
898
		clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
899
900
		break;
	case _PLL3_P:
Yann Gautier's avatar
Yann Gautier committed
901
		clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
902
903
		break;
	case _PLL3_Q:
Yann Gautier's avatar
Yann Gautier committed
904
		clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
905
906
		break;
	case _PLL3_R:
Yann Gautier's avatar
Yann Gautier committed
907
		clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
908
909
		break;
	case _PLL4_P:
Yann Gautier's avatar
Yann Gautier committed
910
		clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
911
912
		break;
	case _PLL4_Q:
Yann Gautier's avatar
Yann Gautier committed
913
		clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
914
915
		break;
	case _PLL4_R:
Yann Gautier's avatar
Yann Gautier committed
916
		clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
917
918
919
		break;
	/* Other */
	case _USB_PHY_48:
Yann Gautier's avatar
Yann Gautier committed
920
		clock = USB_PHY_48_MHZ;
921
922
923
924
925
926
927
928
		break;
	default:
		break;
	}

	return clock;
}

Yann Gautier's avatar
Yann Gautier committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
static void __clk_enable(struct stm32mp1_clk_gate const *gate)
{
	uintptr_t rcc_base = stm32mp_rcc_base();

	if (gate->set_clr != 0U) {
		mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
	} else {
		mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
	}

	VERBOSE("Clock %d has been enabled", gate->index);
}

static void __clk_disable(struct stm32mp1_clk_gate const *gate)
{
	uintptr_t rcc_base = stm32mp_rcc_base();

	if (gate->set_clr != 0U) {
		mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
			      BIT(gate->bit));
	} else {
		mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
	}

	VERBOSE("Clock %d has been disabled", gate->index);
}

static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
{
	uintptr_t rcc_base = stm32mp_rcc_base();

	return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
}

unsigned int stm32mp1_clk_get_refcount(unsigned long id)
964
{
Yann Gautier's avatar
Yann Gautier committed
965
	int i = stm32mp1_clk_get_gated_id(id);
966
967

	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
968
		panic();
969
970
	}

Yann Gautier's avatar
Yann Gautier committed
971
	return gate_refcounts[i];
972
973
}

Yann Gautier's avatar
Yann Gautier committed
974
void __stm32mp1_clk_enable(unsigned long id, bool secure)
975
{
Yann Gautier's avatar
Yann Gautier committed
976
977
978
	const struct stm32mp1_clk_gate *gate;
	int i = stm32mp1_clk_get_gated_id(id);
	unsigned int *refcnt;
979
980

	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
981
982
		ERROR("Clock %d can't be enabled\n", (uint32_t)id);
		panic();
983
984
	}

Yann Gautier's avatar
Yann Gautier committed
985
986
987
988
989
990
991
	gate = gate_ref(i);
	refcnt = &gate_refcounts[i];

	stm32mp1_clk_lock(&refcount_lock);

	if (stm32mp_incr_shrefcnt(refcnt, secure) != 0) {
		__clk_enable(gate);
992
993
	}

Yann Gautier's avatar
Yann Gautier committed
994
	stm32mp1_clk_unlock(&refcount_lock);
995
996
}

Yann Gautier's avatar
Yann Gautier committed
997
void __stm32mp1_clk_disable(unsigned long id, bool secure)
998
{
Yann Gautier's avatar
Yann Gautier committed
999
1000
1001
	const struct stm32mp1_clk_gate *gate;
	int i = stm32mp1_clk_get_gated_id(id);
	unsigned int *refcnt;
1002
1003

	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
1004
1005
		ERROR("Clock %d can't be disabled\n", (uint32_t)id);
		panic();
1006
1007
	}

Yann Gautier's avatar
Yann Gautier committed
1008
1009
1010
1011
1012
1013
1014
	gate = gate_ref(i);
	refcnt = &gate_refcounts[i];

	stm32mp1_clk_lock(&refcount_lock);

	if (stm32mp_decr_shrefcnt(refcnt, secure) != 0) {
		__clk_disable(gate);
1015
1016
	}

Yann Gautier's avatar
Yann Gautier committed
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
	stm32mp1_clk_unlock(&refcount_lock);
}

void stm32mp_clk_enable(unsigned long id)
{
	__stm32mp1_clk_enable(id, true);
}

void stm32mp_clk_disable(unsigned long id)
{
	__stm32mp1_clk_disable(id, true);
}

bool stm32mp_clk_is_enabled(unsigned long id)
{
	int i = stm32mp1_clk_get_gated_id(id);

	if (i < 0) {
		panic();
	}

	return __clk_is_enabled(gate_ref(i));
1039
1040
}

1041
unsigned long stm32mp_clk_get_rate(unsigned long id)
1042
{
Yann Gautier's avatar
Yann Gautier committed
1043
	int p = stm32mp1_clk_get_parent(id);
1044
1045
1046
1047
1048

	if (p < 0) {
		return 0;
	}

Yann Gautier's avatar
Yann Gautier committed
1049
	return get_clock_rate(p);
1050
1051
}

Yann Gautier's avatar
Yann Gautier committed
1052
static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
1053
{
Yann Gautier's avatar
Yann Gautier committed
1054
	uintptr_t address = stm32mp_rcc_base() + offset;
1055

Yann Gautier's avatar
Yann Gautier committed
1056
	if (enable) {
1057
1058
1059
1060
1061
1062
		mmio_setbits_32(address, mask_on);
	} else {
		mmio_clrbits_32(address, mask_on);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1063
static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
1064
{
Yann Gautier's avatar
Yann Gautier committed
1065
1066
1067
1068
	uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
	uintptr_t address = stm32mp_rcc_base() + offset;

	mmio_write_32(address, mask_on);
1069
1070
}

Yann Gautier's avatar
Yann Gautier committed
1071
static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
1072
{
1073
	uint64_t timeout;
1074
	uint32_t mask_test;
Yann Gautier's avatar
Yann Gautier committed
1075
	uintptr_t address = stm32mp_rcc_base() + offset;
1076

Yann Gautier's avatar
Yann Gautier committed
1077
	if (enable) {
1078
1079
1080
1081
1082
		mask_test = mask_rdy;
	} else {
		mask_test = 0;
	}

1083
	timeout = timeout_init_us(OSCRDY_TIMEOUT);
1084
	while ((mmio_read_32(address) & mask_rdy) != mask_test) {
1085
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1086
			ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
1087
1088
1089
1090
1091
1092
1093
1094
			      mask_rdy, address, enable, mmio_read_32(address));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1095
static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
1096
1097
{
	uint32_t value;
Yann Gautier's avatar
Yann Gautier committed
1098
1099
1100
1101
1102
	uintptr_t rcc_base = stm32mp_rcc_base();

	if (digbyp) {
		mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
	}
1103

Yann Gautier's avatar
Yann Gautier committed
1104
1105
	if (bypass || digbyp) {
		mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1106
1107
1108
1109
1110
1111
	}

	/*
	 * Warning: not recommended to switch directly from "high drive"
	 * to "medium low drive", and vice-versa.
	 */
Yann Gautier's avatar
Yann Gautier committed
1112
	value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
1113
1114
1115
1116
1117
1118
1119
1120
1121
		RCC_BDCR_LSEDRV_SHIFT;

	while (value != lsedrv) {
		if (value > lsedrv) {
			value--;
		} else {
			value++;
		}

Yann Gautier's avatar
Yann Gautier committed
1122
		mmio_clrsetbits_32(rcc_base + RCC_BDCR,
1123
1124
1125
1126
				   RCC_BDCR_LSEDRV_MASK,
				   value << RCC_BDCR_LSEDRV_SHIFT);
	}

Yann Gautier's avatar
Yann Gautier committed
1127
	stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
1128
1129
}

Yann Gautier's avatar
Yann Gautier committed
1130
static void stm32mp1_lse_wait(void)
1131
{
Yann Gautier's avatar
Yann Gautier committed
1132
	if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
1133
1134
1135
1136
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1137
static void stm32mp1_lsi_set(bool enable)
1138
{
Yann Gautier's avatar
Yann Gautier committed
1139
1140
1141
	stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);

	if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
1142
1143
1144
1145
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1146
static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
1147
{
Yann Gautier's avatar
Yann Gautier committed
1148
1149
1150
1151
	uintptr_t rcc_base = stm32mp_rcc_base();

	if (digbyp) {
		mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
1152
1153
	}

Yann Gautier's avatar
Yann Gautier committed
1154
1155
1156
1157
1158
1159
	if (bypass || digbyp) {
		mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
	}

	stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
	if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
1160
1161
1162
1163
		VERBOSE("%s: failed\n", __func__);
	}

	if (css) {
Yann Gautier's avatar
Yann Gautier committed
1164
		mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
1165
1166
1167
	}
}

Yann Gautier's avatar
Yann Gautier committed
1168
static void stm32mp1_csi_set(bool enable)
1169
{
Yann Gautier's avatar
Yann Gautier committed
1170
1171
	stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
	if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
1172
1173
1174
1175
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1176
static void stm32mp1_hsi_set(bool enable)
1177
{
Yann Gautier's avatar
Yann Gautier committed
1178
1179
	stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
	if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
1180
1181
1182
1183
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1184
static int stm32mp1_set_hsidiv(uint8_t hsidiv)
1185
{
1186
	uint64_t timeout;
Yann Gautier's avatar
Yann Gautier committed
1187
1188
	uintptr_t rcc_base = stm32mp_rcc_base();
	uintptr_t address = rcc_base + RCC_OCRDYR;
1189

Yann Gautier's avatar
Yann Gautier committed
1190
	mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
1191
1192
1193
			   RCC_HSICFGR_HSIDIV_MASK,
			   RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);

1194
	timeout = timeout_init_us(HSIDIV_TIMEOUT);
1195
	while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
1196
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1197
			ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
1198
1199
1200
1201
1202
1203
1204
1205
			      address, mmio_read_32(address));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1206
static int stm32mp1_hsidiv(unsigned long hsifreq)
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
{
	uint8_t hsidiv;
	uint32_t hsidivfreq = MAX_HSI_HZ;

	for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
		if (hsidivfreq == hsifreq) {
			break;
		}

		hsidivfreq /= 2U;
	}

	if (hsidiv == 4U) {
		ERROR("Invalid clk-hsi frequency\n");
		return -1;
	}

	if (hsidiv != 0U) {
Yann Gautier's avatar
Yann Gautier committed
1225
		return stm32mp1_set_hsidiv(hsidiv);
1226
1227
1228
1229
1230
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
				    unsigned int clksrc,
				    uint32_t *pllcfg, int plloff)
{
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t rcc_base = stm32mp_rcc_base();
	uintptr_t pllxcr = rcc_base + pll->pllxcr;
	enum stm32mp1_plltype type = pll->plltype;
	uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
	unsigned long refclk;
	uint32_t ifrge = 0U;
	uint32_t src, value, fracv;

	/* Check PLL output */
	if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
		return false;
	}

	/* Check current clksrc */
	src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
	if (src != (clksrc & RCC_SELR_SRC_MASK)) {
		return false;
	}

	/* Check Div */
	src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;

	refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
		 (pllcfg[PLLCFG_M] + 1U);

	if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
	    (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
		return false;
	}

	if ((type == PLL_800) && (refclk >= 8000000U)) {
		ifrge = 1U;
	}

	value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
		RCC_PLLNCFGR1_DIVN_MASK;
	value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
		 RCC_PLLNCFGR1_DIVM_MASK;
	value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
		 RCC_PLLNCFGR1_IFRGE_MASK;
	if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
		return false;
	}

	/* Fractional configuration */
	fracv = fdt_read_uint32_default(plloff, "frac", 0);

	value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
	value |= RCC_PLLNFRACR_FRACLE;
	if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
		return false;
	}

	/* Output config */
	value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
		RCC_PLLNCFGR2_DIVP_MASK;
	value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
		 RCC_PLLNCFGR2_DIVQ_MASK;
	value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
		 RCC_PLLNCFGR2_DIVR_MASK;
	if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
		return false;
	}

	return true;
}

static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
1304
{
Yann Gautier's avatar
Yann Gautier committed
1305
1306
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1307

1308
1309
1310
1311
1312
	/* Preserve RCC_PLLNCR_SSCG_CTRL value */
	mmio_clrsetbits_32(pllxcr,
			   RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
			   RCC_PLLNCR_DIVREN,
			   RCC_PLLNCR_PLLON);
1313
1314
}

Yann Gautier's avatar
Yann Gautier committed
1315
static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
1316
{
Yann Gautier's avatar
Yann Gautier committed
1317
1318
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1319
	uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
1320
1321
1322

	/* Wait PLL lock */
	while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
1323
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1324
			ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
			      pll_id, pllxcr, mmio_read_32(pllxcr));
			return -ETIMEDOUT;
		}
	}

	/* Start the requested output */
	mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1336
static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
1337
{
Yann Gautier's avatar
Yann Gautier committed
1338
1339
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1340
	uint64_t timeout;
1341
1342
1343
1344
1345
1346
1347
1348

	/* Stop all output */
	mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
			RCC_PLLNCR_DIVREN);

	/* Stop PLL */
	mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);

1349
	timeout = timeout_init_us(PLLRDY_TIMEOUT);
1350
1351
	/* Wait PLL stopped */
	while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
1352
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1353
			ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
1354
1355
1356
1357
1358
1359
1360
1361
			      pll_id, pllxcr, mmio_read_32(pllxcr));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1362
static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
1363
1364
				       uint32_t *pllcfg)
{
Yann Gautier's avatar
Yann Gautier committed
1365
1366
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t rcc_base = stm32mp_rcc_base();
1367
1368
1369
1370
1371
1372
1373
1374
	uint32_t value;

	value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
		RCC_PLLNCFGR2_DIVP_MASK;
	value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
		 RCC_PLLNCFGR2_DIVQ_MASK;
	value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
		 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautier's avatar
Yann Gautier committed
1375
	mmio_write_32(rcc_base + pll->pllxcfgr2, value);
1376
1377
}

Yann Gautier's avatar
Yann Gautier committed
1378
static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
1379
1380
			       uint32_t *pllcfg, uint32_t fracv)
{
Yann Gautier's avatar
Yann Gautier committed
1381
1382
1383
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t rcc_base = stm32mp_rcc_base();
	enum stm32mp1_plltype type = pll->plltype;
1384
1385
1386
1387
	unsigned long refclk;
	uint32_t ifrge = 0;
	uint32_t src, value;

Yann Gautier's avatar
Yann Gautier committed
1388
	src = mmio_read_32(rcc_base + pll->rckxselr) &
1389
1390
		RCC_SELR_REFCLK_SRC_MASK;

Yann Gautier's avatar
Yann Gautier committed
1391
	refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
		 (pllcfg[PLLCFG_M] + 1U);

	if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
	    (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
		return -EINVAL;
	}

	if ((type == PLL_800) && (refclk >= 8000000U)) {
		ifrge = 1U;
	}

	value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
		RCC_PLLNCFGR1_DIVN_MASK;
	value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
		 RCC_PLLNCFGR1_DIVM_MASK;
	value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
		 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautier's avatar
Yann Gautier committed
1409
	mmio_write_32(rcc_base + pll->pllxcfgr1, value);
1410
1411
1412

	/* Fractional configuration */
	value = 0;
Yann Gautier's avatar
Yann Gautier committed
1413
	mmio_write_32(rcc_base + pll->pllxfracr, value);
1414
1415

	value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier's avatar
Yann Gautier committed
1416
	mmio_write_32(rcc_base + pll->pllxfracr, value);
1417
1418

	value |= RCC_PLLNFRACR_FRACLE;
Yann Gautier's avatar
Yann Gautier committed
1419
	mmio_write_32(rcc_base + pll->pllxfracr, value);
1420

Yann Gautier's avatar
Yann Gautier committed
1421
	stm32mp1_pll_config_output(pll_id, pllcfg);
1422
1423
1424
1425

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1426
static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
1427
{
Yann Gautier's avatar
Yann Gautier committed
1428
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
	uint32_t pllxcsg = 0;

	pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
		    RCC_PLLNCSGR_MOD_PER_MASK;

	pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
		    RCC_PLLNCSGR_INC_STEP_MASK;

	pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
		    RCC_PLLNCSGR_SSCG_MODE_MASK;

Yann Gautier's avatar
Yann Gautier committed
1440
	mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
1441
1442
1443

	mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
			RCC_PLLNCR_SSCG_CTRL);
1444
1445
}

Yann Gautier's avatar
Yann Gautier committed
1446
static int stm32mp1_set_clksrc(unsigned int clksrc)
1447
{
Yann Gautier's avatar
Yann Gautier committed
1448
	uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
1449
	uint64_t timeout;
1450

Yann Gautier's avatar
Yann Gautier committed
1451
	mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
1452
1453
			   clksrc & RCC_SELR_SRC_MASK);

1454
	timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautier's avatar
Yann Gautier committed
1455
	while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
1456
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1457
1458
			ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
			      clksrc_address, mmio_read_32(clksrc_address));
1459
1460
1461
1462
1463
1464
1465
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1466
static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
1467
{
1468
	uint64_t timeout;
1469
1470
1471
1472

	mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
			   clkdiv & RCC_DIVR_DIV_MASK);

1473
	timeout = timeout_init_us(CLKDIV_TIMEOUT);
1474
	while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
1475
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1476
			ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
1477
1478
1479
1480
1481
1482
1483
1484
			      clkdiv, address, mmio_read_32(address));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1485
static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
1486
{
Yann Gautier's avatar
Yann Gautier committed
1487
	uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
1488
1489
1490
1491
1492
1493
1494
1495

	/*
	 * Binding clksrc :
	 *      bit15-4 offset
	 *      bit3:   disable
	 *      bit2-0: MCOSEL[2:0]
	 */
	if ((clksrc & 0x8U) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
1496
		mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
1497
	} else {
Yann Gautier's avatar
Yann Gautier committed
1498
		mmio_clrsetbits_32(clksrc_address,
1499
1500
				   RCC_MCOCFG_MCOSRC_MASK,
				   clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautier's avatar
Yann Gautier committed
1501
		mmio_clrsetbits_32(clksrc_address,
1502
1503
				   RCC_MCOCFG_MCODIV_MASK,
				   clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautier's avatar
Yann Gautier committed
1504
		mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
1505
1506
1507
	}
}

Yann Gautier's avatar
Yann Gautier committed
1508
static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
1509
{
Yann Gautier's avatar
Yann Gautier committed
1510
	uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525

	if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
	    (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
		mmio_clrsetbits_32(address,
				   RCC_BDCR_RTCSRC_MASK,
				   clksrc << RCC_BDCR_RTCSRC_SHIFT);

		mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
	}

	if (lse_css) {
		mmio_setbits_32(address, RCC_BDCR_LSECSSON);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1526
static void stm32mp1_stgen_config(void)
1527
1528
1529
1530
{
	uintptr_t stgen;
	uint32_t cntfid0;
	unsigned long rate;
Yann Gautier's avatar
Yann Gautier committed
1531
	unsigned long long counter;
1532
1533
1534

	stgen = fdt_get_stgen_base();
	cntfid0 = mmio_read_32(stgen + CNTFID_OFF);
Yann Gautier's avatar
Yann Gautier committed
1535
	rate = get_clock_rate(stm32mp1_clk_get_parent(STGEN_K));
1536

Yann Gautier's avatar
Yann Gautier committed
1537
1538
1539
	if (cntfid0 == rate) {
		return;
	}
1540

Yann Gautier's avatar
Yann Gautier committed
1541
1542
1543
1544
	mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
	counter = (unsigned long long)mmio_read_32(stgen + CNTCVL_OFF);
	counter |= ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF)) << 32;
	counter = (counter * rate / cntfid0);
1545

Yann Gautier's avatar
Yann Gautier committed
1546
1547
1548
1549
	mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)counter);
	mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(counter >> 32));
	mmio_write_32(stgen + CNTFID_OFF, rate);
	mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1550

Yann Gautier's avatar
Yann Gautier committed
1551
1552
1553
1554
	write_cntfrq((u_register_t)rate);

	/* Need to update timer with new frequency */
	generic_delay_timer_init();
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
}

void stm32mp1_stgen_increment(unsigned long long offset_in_ms)
{
	uintptr_t stgen;
	unsigned long long cnt;

	stgen = fdt_get_stgen_base();

	cnt = ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF) << 32) |
		mmio_read_32(stgen + CNTCVL_OFF);

	cnt += (offset_in_ms * mmio_read_32(stgen + CNTFID_OFF)) / 1000U;

	mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
	mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)cnt);
	mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(cnt >> 32));
	mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
}

Yann Gautier's avatar
Yann Gautier committed
1575
static void stm32mp1_pkcs_config(uint32_t pkcs)
1576
{
Yann Gautier's avatar
Yann Gautier committed
1577
	uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
	uint32_t value = pkcs & 0xFU;
	uint32_t mask = 0xFU;

	if ((pkcs & BIT(31)) != 0U) {
		mask <<= 4;
		value <<= 4;
	}

	mmio_clrsetbits_32(address, mask, value);
}

int stm32mp1_clk_init(void)
{
Yann Gautier's avatar
Yann Gautier committed
1591
	uintptr_t rcc_base = stm32mp_rcc_base();
1592
1593
1594
1595
1596
1597
1598
	unsigned int clksrc[CLKSRC_NB];
	unsigned int clkdiv[CLKDIV_NB];
	unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
	int plloff[_PLL_NB];
	int ret, len;
	enum stm32mp1_pll_id i;
	bool lse_css = false;
Yann Gautier's avatar
Yann Gautier committed
1599
1600
1601
	bool pll3_preserve = false;
	bool pll4_preserve = false;
	bool pll4_bootrom = false;
1602
	const fdt32_t *pkcs_cell;
1603
1604
1605

	/* Check status field to disable security */
	if (!fdt_get_rcc_secure_status()) {
Yann Gautier's avatar
Yann Gautier committed
1606
		mmio_write_32(rcc_base + RCC_TZCR, 0);
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
	}

	ret = fdt_rcc_read_uint32_array("st,clksrc", clksrc,
					(uint32_t)CLKSRC_NB);
	if (ret < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	ret = fdt_rcc_read_uint32_array("st,clkdiv", clkdiv,
					(uint32_t)CLKDIV_NB);
	if (ret < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
		char name[12];

1624
		snprintf(name, sizeof(name), "st,pll@%d", i);
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
		plloff[i] = fdt_rcc_subnode_offset(name);

		if (!fdt_check_node(plloff[i])) {
			continue;
		}

		ret = fdt_read_uint32_array(plloff[i], "cfg",
					    pllcfg[i], (int)PLLCFG_NB);
		if (ret < 0) {
			return -FDT_ERR_NOTFOUND;
		}
	}

Yann Gautier's avatar
Yann Gautier committed
1638
1639
	stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
	stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1640
1641
1642
1643
1644

	/*
	 * Switch ON oscillator found in device-tree.
	 * Note: HSI already ON after BootROM stage.
	 */
Yann Gautier's avatar
Yann Gautier committed
1645
1646
	if (stm32mp1_osc[_LSI] != 0U) {
		stm32mp1_lsi_set(true);
1647
	}
Yann Gautier's avatar
Yann Gautier committed
1648
1649
	if (stm32mp1_osc[_LSE] != 0U) {
		bool bypass, digbyp;
1650
1651
1652
		uint32_t lsedrv;

		bypass = fdt_osc_read_bool(_LSE, "st,bypass");
Yann Gautier's avatar
Yann Gautier committed
1653
		digbyp = fdt_osc_read_bool(_LSE, "st,digbypass");
1654
1655
1656
		lse_css = fdt_osc_read_bool(_LSE, "st,css");
		lsedrv = fdt_osc_read_uint32_default(_LSE, "st,drive",
						     LSEDRV_MEDIUM_HIGH);
Yann Gautier's avatar
Yann Gautier committed
1657
		stm32mp1_lse_enable(bypass, digbyp, lsedrv);
1658
	}
Yann Gautier's avatar
Yann Gautier committed
1659
1660
	if (stm32mp1_osc[_HSE] != 0U) {
		bool bypass, digbyp, css;
1661

Yann Gautier's avatar
Yann Gautier committed
1662
1663
1664
1665
		bypass = fdt_osc_read_bool(_HSE, "st,bypass");
		digbyp = fdt_osc_read_bool(_HSE, "st,digbypass");
		css = fdt_osc_read_bool(_HSE, "st,css");
		stm32mp1_hse_enable(bypass, digbyp, css);
1666
1667
1668
1669
1670
	}
	/*
	 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
	 * => switch on CSI even if node is not present in device tree
	 */
Yann Gautier's avatar
Yann Gautier committed
1671
	stm32mp1_csi_set(true);
1672
1673

	/* Come back to HSI */
Yann Gautier's avatar
Yann Gautier committed
1674
	ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
1675
1676
1677
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1678
	ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
1679
1680
1681
	if (ret != 0) {
		return ret;
	}
1682
1683
1684
1685
	ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
	if (ret != 0) {
		return ret;
	}
1686

Yann Gautier's avatar
Yann Gautier committed
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
	if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
	     RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
		pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
							clksrc[CLKSRC_PLL3],
							pllcfg[_PLL3],
							plloff[_PLL3]);
		pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
							clksrc[CLKSRC_PLL4],
							pllcfg[_PLL4],
							plloff[_PLL4]);
	}

1699
	for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautier's avatar
Yann Gautier committed
1700
1701
		if (((i == _PLL3) && pll3_preserve) ||
		    ((i == _PLL4) && pll4_preserve)) {
1702
			continue;
Yann Gautier's avatar
Yann Gautier committed
1703
1704
1705
		}

		ret = stm32mp1_pll_stop(i);
1706
1707
1708
1709
1710
1711
		if (ret != 0) {
			return ret;
		}
	}

	/* Configure HSIDIV */
Yann Gautier's avatar
Yann Gautier committed
1712
1713
	if (stm32mp1_osc[_HSI] != 0U) {
		ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
1714
1715
1716
		if (ret != 0) {
			return ret;
		}
Yann Gautier's avatar
Yann Gautier committed
1717
		stm32mp1_stgen_config();
1718
1719
1720
1721
	}

	/* Select DIV */
	/* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautier's avatar
Yann Gautier committed
1722
	mmio_write_32(rcc_base + RCC_MPCKDIVR,
1723
		      clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautier's avatar
Yann Gautier committed
1724
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
1725
1726
1727
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1728
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
1729
1730
1731
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1732
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
1733
1734
1735
	if (ret != 0) {
		return ret;
	}
1736
1737
1738
1739
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1740
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
1741
1742
1743
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1744
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
1745
1746
1747
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1748
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
1749
1750
1751
1752
1753
	if (ret != 0) {
		return ret;
	}

	/* No ready bit for RTC */
Yann Gautier's avatar
Yann Gautier committed
1754
	mmio_write_32(rcc_base + RCC_RTCDIVR,
1755
1756
1757
		      clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);

	/* Configure PLLs source */
Yann Gautier's avatar
Yann Gautier committed
1758
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
1759
1760
1761
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1762
1763
1764
1765
1766
1767

	if (!pll3_preserve) {
		ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
		if (ret != 0) {
			return ret;
		}
1768
1769
	}

Yann Gautier's avatar
Yann Gautier committed
1770
1771
1772
1773
1774
	if (!pll4_preserve) {
		ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
		if (ret != 0) {
			return ret;
		}
1775
1776
1777
1778
1779
1780
1781
	}

	/* Configure and start PLLs */
	for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
		uint32_t fracv;
		uint32_t csg[PLLCSG_NB];

Yann Gautier's avatar
Yann Gautier committed
1782
1783
1784
1785
1786
		if (((i == _PLL3) && pll3_preserve) ||
		    ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
			continue;
		}

1787
1788
1789
1790
		if (!fdt_check_node(plloff[i])) {
			continue;
		}

Yann Gautier's avatar
Yann Gautier committed
1791
1792
1793
1794
1795
1796
		if ((i == _PLL4) && pll4_bootrom) {
			/* Set output divider if not done by the Bootrom */
			stm32mp1_pll_config_output(i, pllcfg[i]);
			continue;
		}

1797
1798
		fracv = fdt_read_uint32_default(plloff[i], "frac", 0);

Yann Gautier's avatar
Yann Gautier committed
1799
		ret = stm32mp1_pll_config(i, pllcfg[i], fracv);
1800
1801
1802
1803
1804
1805
		if (ret != 0) {
			return ret;
		}
		ret = fdt_read_uint32_array(plloff[i], "csg", csg,
					    (uint32_t)PLLCSG_NB);
		if (ret == 0) {
Yann Gautier's avatar
Yann Gautier committed
1806
			stm32mp1_pll_csg(i, csg);
1807
1808
1809
1810
		} else if (ret != -FDT_ERR_NOTFOUND) {
			return ret;
		}

Yann Gautier's avatar
Yann Gautier committed
1811
		stm32mp1_pll_start(i);
1812
1813
1814
1815
1816
1817
1818
	}
	/* Wait and start PLLs ouptut when ready */
	for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
		if (!fdt_check_node(plloff[i])) {
			continue;
		}

Yann Gautier's avatar
Yann Gautier committed
1819
		ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
1820
1821
1822
1823
1824
		if (ret != 0) {
			return ret;
		}
	}
	/* Wait LSE ready before to use it */
Yann Gautier's avatar
Yann Gautier committed
1825
1826
	if (stm32mp1_osc[_LSE] != 0U) {
		stm32mp1_lse_wait();
1827
1828
1829
	}

	/* Configure with expected clock source */
Yann Gautier's avatar
Yann Gautier committed
1830
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
1831
1832
1833
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1834
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
1835
1836
1837
	if (ret != 0) {
		return ret;
	}
1838
1839
1840
1841
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1842
	stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
1843
1844
1845
1846
1847
1848
1849
1850

	/* Configure PKCK */
	pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
	if (pkcs_cell != NULL) {
		bool ckper_disabled = false;
		uint32_t j;

		for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
1851
			uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
1852
1853
1854
1855
1856

			if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
				ckper_disabled = true;
				continue;
			}
Yann Gautier's avatar
Yann Gautier committed
1857
			stm32mp1_pkcs_config(pkcs);
1858
1859
1860
1861
1862
1863
1864
1865
1866
		}

		/*
		 * CKPER is source for some peripheral clocks
		 * (FMC-NAND / QPSI-NOR) and switching source is allowed
		 * only if previous clock is still ON
		 * => deactivated CKPER only after switching clock
		 */
		if (ckper_disabled) {
Yann Gautier's avatar
Yann Gautier committed
1867
			stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
1868
1869
1870
1871
		}
	}

	/* Switch OFF HSI if not found in device-tree */
Yann Gautier's avatar
Yann Gautier committed
1872
1873
	if (stm32mp1_osc[_HSI] == 0U) {
		stm32mp1_hsi_set(false);
1874
	}
Yann Gautier's avatar
Yann Gautier committed
1875
	stm32mp1_stgen_config();
1876
1877

	/* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautier's avatar
Yann Gautier committed
1878
	mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
			   RCC_DDRITFCR_DDRCKMOD_MASK,
			   RCC_DDRITFCR_DDRCKMOD_SSR <<
			   RCC_DDRITFCR_DDRCKMOD_SHIFT);

	return 0;
}

static void stm32mp1_osc_clk_init(const char *name,
				  enum stm32mp_osc_id index)
{
	uint32_t frequency;

Yann Gautier's avatar
Yann Gautier committed
1891
1892
	if (fdt_osc_read_freq(name, &frequency) == 0) {
		stm32mp1_osc[index] = frequency;
1893
1894
1895
1896
1897
1898
1899
1900
	}
}

static void stm32mp1_osc_init(void)
{
	enum stm32mp_osc_id i;

	for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautier's avatar
Yann Gautier committed
1901
		stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
1902
1903
1904
	}
}

1905
1906
1907
1908
1909
1910
1911
static void sync_earlyboot_clocks_state(void)
{
	if (!stm32mp_is_single_core()) {
		stm32mp1_clk_enable_secure(RTCAPB);
	}
}

1912
1913
1914
1915
int stm32mp1_clk_probe(void)
{
	stm32mp1_osc_init();

1916
1917
	sync_earlyboot_clocks_state();

1918
1919
	return 0;
}