stm32mp1_clk.c 51.3 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
#include <arch.h>
#include <arch_helpers.h>
#include <common/debug.h>
19
#include <common/fdt_wrappers.h>
20
21
#include <drivers/delay_timer.h>
#include <drivers/generic_delay_timer.h>
Yann Gautier's avatar
Yann Gautier committed
22
#include <drivers/st/stm32mp_clkfunc.h>
23
24
25
26
#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
27
#include <lib/spinlock.h>
28
29
30
#include <lib/utils_def.h>
#include <plat/common/platform.h>

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

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

37
38
39
40
41
#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
42

43
44
45
46
47
48
49
50
51
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",
};

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
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,
82
	_CK_MCU,
Yann Gautier's avatar
Yann Gautier committed
83
	_USB_PHY_48,
84
85
86
87
	_PARENT_NB,
	_UNKNOWN_ID = 0xff,
};

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

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
/* State the parent clock ID straight related to a clock */
static const uint8_t parent_id_clock_id[_PARENT_NB] = {
	[_HSE] = CK_HSE,
	[_HSI] = CK_HSI,
	[_CSI] = CK_CSI,
	[_LSE] = CK_LSE,
	[_LSI] = CK_LSI,
	[_I2S_CKIN] = _UNKNOWN_ID,
	[_USB_PHY_48] = _UNKNOWN_ID,
	[_HSI_KER] = CK_HSI,
	[_HSE_KER] = CK_HSE,
	[_HSE_KER_DIV2] = CK_HSE_DIV2,
	[_CSI_KER] = CK_CSI,
	[_PLL1_P] = PLL1_P,
	[_PLL1_Q] = PLL1_Q,
	[_PLL1_R] = PLL1_R,
	[_PLL2_P] = PLL2_P,
	[_PLL2_Q] = PLL2_Q,
	[_PLL2_R] = PLL2_R,
	[_PLL3_P] = PLL3_P,
	[_PLL3_Q] = PLL3_Q,
	[_PLL3_R] = PLL3_R,
	[_PLL4_P] = PLL4_P,
	[_PLL4_Q] = PLL4_Q,
	[_PLL4_R] = PLL4_R,
	[_ACLK] = CK_AXI,
	[_PCLK1] = CK_AXI,
	[_PCLK2] = CK_AXI,
	[_PCLK3] = CK_AXI,
	[_PCLK4] = CK_AXI,
	[_PCLK5] = CK_AXI,
	[_CK_PER] = CK_PER,
	[_CK_MPU] = CK_MPU,
	[_CK_MCU] = CK_MCU,
};

static unsigned int clock_id2parent_id(unsigned long id)
{
	unsigned int n;

	for (n = 0U; n < ARRAY_SIZE(parent_id_clock_id); n++) {
		if (parent_id_clock_id[n] == id) {
			return n;
		}
	}

	return _UNKNOWN_ID;
}

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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,
183
	CLKSRC_MCU,
184
185
186
187
188
189
190
191
192
193
194
195
	CLKSRC_PLL12,
	CLKSRC_PLL3,
	CLKSRC_PLL4,
	CLKSRC_RTC,
	CLKSRC_MCO1,
	CLKSRC_MCO2,
	CLKSRC_NB
};

enum stm32mp1_clkdiv_id {
	CLKDIV_MPU,
	CLKDIV_AXI,
196
	CLKDIV_MCU,
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
	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
242
243
	uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
	uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
};

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
266
267
/* Clocks with selectable source and non set/clr register access */
#define _CLK_SELEC(off, b, idx, s)			\
268
269
270
271
272
273
274
275
276
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 0,				\
		.sel = (s),				\
		.fixed = _UNKNOWN_ID,			\
	}

Yann Gautier's avatar
Yann Gautier committed
277
278
/* Clocks with fixed source and non set/clr register access */
#define _CLK_FIXED(off, b, idx, f)			\
279
280
281
282
283
284
285
286
287
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 0,				\
		.sel = _UNKNOWN_SEL,			\
		.fixed = (f),				\
	}

Yann Gautier's avatar
Yann Gautier committed
288
289
/* Clocks with selectable source and set/clr register access */
#define _CLK_SC_SELEC(off, b, idx, s)			\
290
291
292
293
294
295
296
297
298
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 1,				\
		.sel = (s),				\
		.fixed = _UNKNOWN_ID,			\
	}

Yann Gautier's avatar
Yann Gautier committed
299
300
/* Clocks with fixed source and set/clr register access */
#define _CLK_SC_FIXED(off, b, idx, f)			\
301
302
303
304
305
306
307
308
309
	{						\
		.offset = (off),			\
		.bit = (b),				\
		.index = (idx),				\
		.set_clr = 1,				\
		.sel = _UNKNOWN_SEL,			\
		.fixed = (f),				\
	}

310
311
312
313
#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents)		\
	[_ ## _label ## _SEL] = {				\
		.offset = _rcc_selr,				\
		.src = _rcc_selr ## _ ## _label ## SRC_SHIFT,	\
314
315
		.msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
		       (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
316
317
		.parent = (_parents),				\
		.nb_parent = ARRAY_SIZE(_parents)		\
318
319
	}

Yann Gautier's avatar
Yann Gautier committed
320
321
322
#define _CLK_PLL(idx, type, off1, off2, off3,		\
		 off4, off5, off6,			\
		 p1, p2, p3, p4)			\
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	[(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),			\
	}

Yann Gautier's avatar
Yann Gautier committed
337
338
#define NB_GATES	ARRAY_SIZE(stm32mp1_clk_gate)

339
static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautier's avatar
Yann Gautier committed
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
	_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),

367
368
	_CLK_SC_FIXED(RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),

Yann Gautier's avatar
Yann Gautier committed
369
370
371
372
373
374
375
	_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),
376
	_CLK_SC_SELEC(RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
Yann Gautier's avatar
Yann Gautier committed
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
	_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),

412
	_CLK_SELEC(RCC_BDCR, 20, RTC, _RTC_SEL),
Yann Gautier's avatar
Yann Gautier committed
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
	_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
470
471
};

472
473
474
475
static const uint8_t mss_parents[] = {
	_HSI, _HSE, _CSI, _PLL3
};

Yann Gautier's avatar
Yann Gautier committed
476
477
478
479
480
481
482
static const uint8_t usbphy_parents[] = {
	_HSE_KER, _PLL4_R, _HSE_KER_DIV2
};

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

484
485
486
487
488
489
490
491
static const uint8_t mpu_parents[] = {
	_HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */
};

static const uint8_t per_parents[] = {
	_HSI, _HSE, _CSI,
};

492
493
494
495
static const uint8_t rtc_parents[] = {
	_UNKNOWN_ID, _LSE, _LSI, _HSE
};

496
static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
497
498
499
500
501
502
503
	_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),
504
505
	_CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
	_CLK_PARENT_SEL(PER, RCC_CPERCKSELR, per_parents),
506
	_CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
507
508
509
510
511
512
513
514
515
516
517
518
	_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),
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
};

/* 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
540
	[_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
541
542
543
};

static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautier's avatar
Yann Gautier committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
	_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),
560
561
562
};

/* Prescaler table lookups for clock computation */
563
564
565
566
/* 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
};
567
568
569
570
571
572
573
574
575
576
577
578
579

/* 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
};

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
	[_HSI] = "HSI",
	[_HSE] = "HSE",
	[_CSI] = "CSI",
	[_LSI] = "LSI",
	[_LSE] = "LSE",
	[_I2S_CKIN] = "I2S_CKIN",
	[_HSI_KER] = "HSI_KER",
	[_HSE_KER] = "HSE_KER",
	[_HSE_KER_DIV2] = "HSE_KER_DIV2",
	[_CSI_KER] = "CSI_KER",
	[_PLL1_P] = "PLL1_P",
	[_PLL1_Q] = "PLL1_Q",
	[_PLL1_R] = "PLL1_R",
	[_PLL2_P] = "PLL2_P",
	[_PLL2_Q] = "PLL2_Q",
	[_PLL2_R] = "PLL2_R",
	[_PLL3_P] = "PLL3_P",
	[_PLL3_Q] = "PLL3_Q",
	[_PLL3_R] = "PLL3_R",
	[_PLL4_P] = "PLL4_P",
	[_PLL4_Q] = "PLL4_Q",
	[_PLL4_R] = "PLL4_R",
	[_ACLK] = "ACLK",
	[_PCLK1] = "PCLK1",
	[_PCLK2] = "PCLK2",
	[_PCLK3] = "PCLK3",
	[_PCLK4] = "PCLK4",
	[_PCLK5] = "PCLK5",
	[_HCLK6] = "KCLK6",
	[_HCLK2] = "HCLK2",
	[_CK_PER] = "CK_PER",
	[_CK_MPU] = "CK_MPU",
	[_CK_MCU] = "CK_MCU",
	[_USB_PHY_48] = "USB_PHY_48",
};

Yann Gautier's avatar
Yann Gautier committed
617
618
619
620
621
622
623
624
625
626
/* 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];
}
627

Yann Gautier's avatar
Yann Gautier committed
628
629
630
631
static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
{
	return &stm32mp1_clk_sel[idx];
}
632

Yann Gautier's avatar
Yann Gautier committed
633
634
635
636
637
638
639
static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
{
	return &stm32mp1_clk_pll[idx];
}

static void stm32mp1_clk_lock(struct spinlock *lock)
{
640
641
642
	if (stm32mp_lock_available()) {
		/* Assume interrupts are masked */
		spin_lock(lock);
Yann Gautier's avatar
Yann Gautier committed
643
644
645
646
647
	}
}

static void stm32mp1_clk_unlock(struct spinlock *lock)
{
648
649
	if (stm32mp_lock_available()) {
		spin_unlock(lock);
Yann Gautier's avatar
Yann Gautier committed
650
651
652
653
654
655
656
657
658
659
	}
}

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;
}

660
661
662
663
664
665
666
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
667
668
669
670
671
672
673
674
675
676
677
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)
678
679
680
681
682
{
	if (idx >= NB_OSC) {
		return 0;
	}

Yann Gautier's avatar
Yann Gautier committed
683
	return stm32mp1_osc[idx];
684
685
}

Yann Gautier's avatar
Yann Gautier committed
686
static int stm32mp1_clk_get_gated_id(unsigned long id)
687
{
Yann Gautier's avatar
Yann Gautier committed
688
	unsigned int i;
689

Yann Gautier's avatar
Yann Gautier committed
690
691
	for (i = 0U; i < NB_GATES; i++) {
		if (gate_ref(i)->index == id) {
692
693
694
695
696
697
698
699
700
			return i;
		}
	}

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

	return -EINVAL;
}

Yann Gautier's avatar
Yann Gautier committed
701
static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
702
{
Yann Gautier's avatar
Yann Gautier committed
703
	return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
704
705
}

Yann Gautier's avatar
Yann Gautier committed
706
static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
707
{
Yann Gautier's avatar
Yann Gautier committed
708
	return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
709
710
}

Yann Gautier's avatar
Yann Gautier committed
711
static int stm32mp1_clk_get_parent(unsigned long id)
712
{
Yann Gautier's avatar
Yann Gautier committed
713
	const struct stm32mp1_clk_sel *sel;
714
	uint32_t p_sel;
715
716
717
	int i;
	enum stm32mp1_parent_id p;
	enum stm32mp1_parent_sel s;
Yann Gautier's avatar
Yann Gautier committed
718
	uintptr_t rcc_base = stm32mp_rcc_base();
719

720
721
722
723
	/* Few non gateable clock have a static parent ID, find them */
	i = (int)clock_id2parent_id(id);
	if (i != _UNKNOWN_ID) {
		return i;
724
725
	}

Yann Gautier's avatar
Yann Gautier committed
726
	i = stm32mp1_clk_get_gated_id(id);
727
	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
728
		panic();
729
730
	}

Yann Gautier's avatar
Yann Gautier committed
731
	p = stm32mp1_clk_get_fixed_parent(i);
732
733
734
735
	if (p < _PARENT_NB) {
		return (int)p;
	}

Yann Gautier's avatar
Yann Gautier committed
736
737
	s = stm32mp1_clk_get_sel(i);
	if (s == _UNKNOWN_SEL) {
738
739
		return -EINVAL;
	}
Yann Gautier's avatar
Yann Gautier committed
740
741
	if (s >= _PARENT_SEL_NB) {
		panic();
742
743
	}

Yann Gautier's avatar
Yann Gautier committed
744
	sel = clk_sel_ref(s);
745
746
	p_sel = (mmio_read_32(rcc_base + sel->offset) &
		 (sel->msk << sel->src)) >> sel->src;
Yann Gautier's avatar
Yann Gautier committed
747
748
749
	if (p_sel < sel->nb_parent) {
		return (int)sel->parent[p_sel];
	}
750
751
752
753

	return -EINVAL;
}

Yann Gautier's avatar
Yann Gautier committed
754
static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
755
{
Yann Gautier's avatar
Yann Gautier committed
756
757
	uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
	uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
758

Yann Gautier's avatar
Yann Gautier committed
759
	return stm32mp1_clk_get_fixed(pll->refclk[src]);
760
761
762
763
764
765
766
767
}

/*
 * 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
768
static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
769
770
771
{
	unsigned long refclk, fvco;
	uint32_t cfgr1, fracr, divm, divn;
Yann Gautier's avatar
Yann Gautier committed
772
	uintptr_t rcc_base = stm32mp_rcc_base();
773

Yann Gautier's avatar
Yann Gautier committed
774
775
	cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
	fracr = mmio_read_32(rcc_base + pll->pllxfracr);
776
777
778
779

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

Yann Gautier's avatar
Yann Gautier committed
780
	refclk = stm32mp1_pll_get_fref(pll);
781
782
783
784
785
786
787
788

	/*
	 * 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
789
790
		uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
				 RCC_PLLNFRACR_FRACV_SHIFT;
791
792
		unsigned long long numerator, denominator;

Yann Gautier's avatar
Yann Gautier committed
793
794
795
		numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
		numerator = refclk * numerator;
		denominator = ((unsigned long long)divm + 1U) << 13;
796
797
798
799
800
801
802
803
		fvco = (unsigned long)(numerator / denominator);
	} else {
		fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
	}

	return fvco;
}

Yann Gautier's avatar
Yann Gautier committed
804
static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
805
806
					    enum stm32mp1_div_id div_id)
{
Yann Gautier's avatar
Yann Gautier committed
807
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
808
809
810
811
812
813
814
	unsigned long dfout;
	uint32_t cfgr2, divy;

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

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

Yann Gautier's avatar
Yann Gautier committed
818
	dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
819
820
821
822

	return dfout;
}

Yann Gautier's avatar
Yann Gautier committed
823
static unsigned long get_clock_rate(int p)
824
825
826
{
	uint32_t reg, clkdiv;
	unsigned long clock = 0;
Yann Gautier's avatar
Yann Gautier committed
827
	uintptr_t rcc_base = stm32mp_rcc_base();
828
829
830
831

	switch (p) {
	case _CK_MPU:
	/* MPU sub system */
Yann Gautier's avatar
Yann Gautier committed
832
		reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
833
834
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_MPCKSELR_HSI:
Yann Gautier's avatar
Yann Gautier committed
835
			clock = stm32mp1_clk_get_fixed(_HSI);
836
837
			break;
		case RCC_MPCKSELR_HSE:
Yann Gautier's avatar
Yann Gautier committed
838
			clock = stm32mp1_clk_get_fixed(_HSE);
839
840
			break;
		case RCC_MPCKSELR_PLL:
Yann Gautier's avatar
Yann Gautier committed
841
			clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
842
843
			break;
		case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautier's avatar
Yann Gautier committed
844
			clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
845

Yann Gautier's avatar
Yann Gautier committed
846
			reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
			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
862
		reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
863
864
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_ASSCKSELR_HSI:
Yann Gautier's avatar
Yann Gautier committed
865
			clock = stm32mp1_clk_get_fixed(_HSI);
866
867
			break;
		case RCC_ASSCKSELR_HSE:
Yann Gautier's avatar
Yann Gautier committed
868
			clock = stm32mp1_clk_get_fixed(_HSE);
869
870
			break;
		case RCC_ASSCKSELR_PLL:
Yann Gautier's avatar
Yann Gautier committed
871
			clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
872
873
874
875
876
877
			break;
		default:
			break;
		}

		/* System clock divider */
Yann Gautier's avatar
Yann Gautier committed
878
		reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
879
880
881
882
		clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];

		switch (p) {
		case _PCLK4:
Yann Gautier's avatar
Yann Gautier committed
883
			reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
884
885
886
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		case _PCLK5:
Yann Gautier's avatar
Yann Gautier committed
887
			reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
888
889
890
891
892
893
			clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
			break;
		default:
			break;
		}
		break;
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
	/* 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;
939
	case _CK_PER:
Yann Gautier's avatar
Yann Gautier committed
940
		reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
941
942
		switch (reg & RCC_SELR_SRC_MASK) {
		case RCC_CPERCKSELR_HSI:
Yann Gautier's avatar
Yann Gautier committed
943
			clock = stm32mp1_clk_get_fixed(_HSI);
944
945
			break;
		case RCC_CPERCKSELR_HSE:
Yann Gautier's avatar
Yann Gautier committed
946
			clock = stm32mp1_clk_get_fixed(_HSE);
947
948
			break;
		case RCC_CPERCKSELR_CSI:
Yann Gautier's avatar
Yann Gautier committed
949
			clock = stm32mp1_clk_get_fixed(_CSI);
950
951
952
953
954
955
956
			break;
		default:
			break;
		}
		break;
	case _HSI:
	case _HSI_KER:
Yann Gautier's avatar
Yann Gautier committed
957
		clock = stm32mp1_clk_get_fixed(_HSI);
958
959
960
		break;
	case _CSI:
	case _CSI_KER:
Yann Gautier's avatar
Yann Gautier committed
961
		clock = stm32mp1_clk_get_fixed(_CSI);
962
963
964
		break;
	case _HSE:
	case _HSE_KER:
Yann Gautier's avatar
Yann Gautier committed
965
		clock = stm32mp1_clk_get_fixed(_HSE);
966
967
		break;
	case _HSE_KER_DIV2:
Yann Gautier's avatar
Yann Gautier committed
968
		clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
969
970
		break;
	case _LSI:
Yann Gautier's avatar
Yann Gautier committed
971
		clock = stm32mp1_clk_get_fixed(_LSI);
972
973
		break;
	case _LSE:
Yann Gautier's avatar
Yann Gautier committed
974
		clock = stm32mp1_clk_get_fixed(_LSE);
975
976
977
		break;
	/* PLL */
	case _PLL1_P:
Yann Gautier's avatar
Yann Gautier committed
978
		clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
979
980
		break;
	case _PLL1_Q:
Yann Gautier's avatar
Yann Gautier committed
981
		clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
982
983
		break;
	case _PLL1_R:
Yann Gautier's avatar
Yann Gautier committed
984
		clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
985
986
		break;
	case _PLL2_P:
Yann Gautier's avatar
Yann Gautier committed
987
		clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
988
989
		break;
	case _PLL2_Q:
Yann Gautier's avatar
Yann Gautier committed
990
		clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
991
992
		break;
	case _PLL2_R:
Yann Gautier's avatar
Yann Gautier committed
993
		clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
994
995
		break;
	case _PLL3_P:
Yann Gautier's avatar
Yann Gautier committed
996
		clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
997
998
		break;
	case _PLL3_Q:
Yann Gautier's avatar
Yann Gautier committed
999
		clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
1000
1001
		break;
	case _PLL3_R:
Yann Gautier's avatar
Yann Gautier committed
1002
		clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
1003
1004
		break;
	case _PLL4_P:
Yann Gautier's avatar
Yann Gautier committed
1005
		clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
1006
1007
		break;
	case _PLL4_Q:
Yann Gautier's avatar
Yann Gautier committed
1008
		clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
1009
1010
		break;
	case _PLL4_R:
Yann Gautier's avatar
Yann Gautier committed
1011
		clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
1012
1013
1014
		break;
	/* Other */
	case _USB_PHY_48:
Yann Gautier's avatar
Yann Gautier committed
1015
		clock = USB_PHY_48_MHZ;
1016
1017
1018
1019
1020
1021
1022
1023
		break;
	default:
		break;
	}

	return clock;
}

Yann Gautier's avatar
Yann Gautier committed
1024
1025
1026
1027
static void __clk_enable(struct stm32mp1_clk_gate const *gate)
{
	uintptr_t rcc_base = stm32mp_rcc_base();

1028
1029
	VERBOSE("Enable clock %u\n", gate->index);

Yann Gautier's avatar
Yann Gautier committed
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
	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));
	}
}

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

1041
1042
	VERBOSE("Disable clock %u\n", gate->index);

Yann Gautier's avatar
Yann Gautier committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
	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));
	}
}

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)
1059
{
Yann Gautier's avatar
Yann Gautier committed
1060
	int i = stm32mp1_clk_get_gated_id(id);
1061
1062

	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
1063
		panic();
1064
1065
	}

Yann Gautier's avatar
Yann Gautier committed
1066
	return gate_refcounts[i];
1067
1068
}

1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
/* Oscillators and PLLs are not gated at runtime */
static bool clock_is_always_on(unsigned long id)
{
	switch (id) {
	case CK_HSE:
	case CK_CSI:
	case CK_LSI:
	case CK_LSE:
	case CK_HSI:
	case CK_HSE_DIV2:
	case PLL1_Q:
	case PLL1_R:
	case PLL2_P:
	case PLL2_Q:
	case PLL2_R:
	case PLL3_P:
	case PLL3_Q:
	case PLL3_R:
		return true;
	default:
		return false;
	}
}

Yann Gautier's avatar
Yann Gautier committed
1093
void __stm32mp1_clk_enable(unsigned long id, bool secure)
1094
{
Yann Gautier's avatar
Yann Gautier committed
1095
	const struct stm32mp1_clk_gate *gate;
1096
	int i;
Yann Gautier's avatar
Yann Gautier committed
1097
	unsigned int *refcnt;
1098

1099
1100
1101
1102
1103
	if (clock_is_always_on(id)) {
		return;
	}

	i = stm32mp1_clk_get_gated_id(id);
1104
	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
1105
1106
		ERROR("Clock %d can't be enabled\n", (uint32_t)id);
		panic();
1107
1108
	}

Yann Gautier's avatar
Yann Gautier committed
1109
1110
1111
1112
1113
1114
1115
	gate = gate_ref(i);
	refcnt = &gate_refcounts[i];

	stm32mp1_clk_lock(&refcount_lock);

	if (stm32mp_incr_shrefcnt(refcnt, secure) != 0) {
		__clk_enable(gate);
1116
1117
	}

Yann Gautier's avatar
Yann Gautier committed
1118
	stm32mp1_clk_unlock(&refcount_lock);
1119
1120
}

Yann Gautier's avatar
Yann Gautier committed
1121
void __stm32mp1_clk_disable(unsigned long id, bool secure)
1122
{
Yann Gautier's avatar
Yann Gautier committed
1123
	const struct stm32mp1_clk_gate *gate;
1124
	int i;
Yann Gautier's avatar
Yann Gautier committed
1125
	unsigned int *refcnt;
1126

1127
1128
1129
1130
1131
	if (clock_is_always_on(id)) {
		return;
	}

	i = stm32mp1_clk_get_gated_id(id);
1132
	if (i < 0) {
Yann Gautier's avatar
Yann Gautier committed
1133
1134
		ERROR("Clock %d can't be disabled\n", (uint32_t)id);
		panic();
1135
1136
	}

Yann Gautier's avatar
Yann Gautier committed
1137
1138
1139
1140
1141
1142
1143
	gate = gate_ref(i);
	refcnt = &gate_refcounts[i];

	stm32mp1_clk_lock(&refcount_lock);

	if (stm32mp_decr_shrefcnt(refcnt, secure) != 0) {
		__clk_disable(gate);
1144
1145
	}

Yann Gautier's avatar
Yann Gautier committed
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
	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)
{
1161
	int i;
Yann Gautier's avatar
Yann Gautier committed
1162

1163
1164
1165
1166
1167
	if (clock_is_always_on(id)) {
		return true;
	}

	i = stm32mp1_clk_get_gated_id(id);
Yann Gautier's avatar
Yann Gautier committed
1168
1169
1170
1171
1172
	if (i < 0) {
		panic();
	}

	return __clk_is_enabled(gate_ref(i));
1173
1174
}

1175
unsigned long stm32mp_clk_get_rate(unsigned long id)
1176
{
Yann Gautier's avatar
Yann Gautier committed
1177
	int p = stm32mp1_clk_get_parent(id);
1178
1179
1180
1181
1182

	if (p < 0) {
		return 0;
	}

Yann Gautier's avatar
Yann Gautier committed
1183
	return get_clock_rate(p);
1184
1185
}

Yann Gautier's avatar
Yann Gautier committed
1186
static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
1187
{
Yann Gautier's avatar
Yann Gautier committed
1188
	uintptr_t address = stm32mp_rcc_base() + offset;
1189

Yann Gautier's avatar
Yann Gautier committed
1190
	if (enable) {
1191
1192
1193
1194
1195
1196
		mmio_setbits_32(address, mask_on);
	} else {
		mmio_clrbits_32(address, mask_on);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1197
static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
1198
{
Yann Gautier's avatar
Yann Gautier committed
1199
1200
1201
1202
	uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
	uintptr_t address = stm32mp_rcc_base() + offset;

	mmio_write_32(address, mask_on);
1203
1204
}

Yann Gautier's avatar
Yann Gautier committed
1205
static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
1206
{
1207
	uint64_t timeout;
1208
	uint32_t mask_test;
Yann Gautier's avatar
Yann Gautier committed
1209
	uintptr_t address = stm32mp_rcc_base() + offset;
1210

Yann Gautier's avatar
Yann Gautier committed
1211
	if (enable) {
1212
1213
1214
1215
1216
		mask_test = mask_rdy;
	} else {
		mask_test = 0;
	}

1217
	timeout = timeout_init_us(OSCRDY_TIMEOUT);
1218
	while ((mmio_read_32(address) & mask_rdy) != mask_test) {
1219
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1220
			ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
1221
1222
1223
1224
1225
1226
1227
1228
			      mask_rdy, address, enable, mmio_read_32(address));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1229
static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
1230
1231
{
	uint32_t value;
Yann Gautier's avatar
Yann Gautier committed
1232
1233
1234
1235
1236
	uintptr_t rcc_base = stm32mp_rcc_base();

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

Yann Gautier's avatar
Yann Gautier committed
1238
1239
	if (bypass || digbyp) {
		mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1240
1241
1242
1243
1244
1245
	}

	/*
	 * Warning: not recommended to switch directly from "high drive"
	 * to "medium low drive", and vice-versa.
	 */
Yann Gautier's avatar
Yann Gautier committed
1246
	value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
1247
1248
1249
1250
1251
1252
1253
1254
1255
		RCC_BDCR_LSEDRV_SHIFT;

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

Yann Gautier's avatar
Yann Gautier committed
1256
		mmio_clrsetbits_32(rcc_base + RCC_BDCR,
1257
1258
1259
1260
				   RCC_BDCR_LSEDRV_MASK,
				   value << RCC_BDCR_LSEDRV_SHIFT);
	}

Yann Gautier's avatar
Yann Gautier committed
1261
	stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
1262
1263
}

Yann Gautier's avatar
Yann Gautier committed
1264
static void stm32mp1_lse_wait(void)
1265
{
Yann Gautier's avatar
Yann Gautier committed
1266
	if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
1267
1268
1269
1270
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1271
static void stm32mp1_lsi_set(bool enable)
1272
{
Yann Gautier's avatar
Yann Gautier committed
1273
1274
1275
	stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);

	if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
1276
1277
1278
1279
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1280
static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
1281
{
Yann Gautier's avatar
Yann Gautier committed
1282
1283
1284
1285
	uintptr_t rcc_base = stm32mp_rcc_base();

	if (digbyp) {
		mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
1286
1287
	}

Yann Gautier's avatar
Yann Gautier committed
1288
1289
1290
1291
1292
1293
	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) {
1294
1295
1296
1297
		VERBOSE("%s: failed\n", __func__);
	}

	if (css) {
Yann Gautier's avatar
Yann Gautier committed
1298
		mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
1299
1300
1301
	}
}

Yann Gautier's avatar
Yann Gautier committed
1302
static void stm32mp1_csi_set(bool enable)
1303
{
Yann Gautier's avatar
Yann Gautier committed
1304
1305
	stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
	if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
1306
1307
1308
1309
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1310
static void stm32mp1_hsi_set(bool enable)
1311
{
Yann Gautier's avatar
Yann Gautier committed
1312
1313
	stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
	if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
1314
1315
1316
1317
		VERBOSE("%s: failed\n", __func__);
	}
}

Yann Gautier's avatar
Yann Gautier committed
1318
static int stm32mp1_set_hsidiv(uint8_t hsidiv)
1319
{
1320
	uint64_t timeout;
Yann Gautier's avatar
Yann Gautier committed
1321
1322
	uintptr_t rcc_base = stm32mp_rcc_base();
	uintptr_t address = rcc_base + RCC_OCRDYR;
1323

Yann Gautier's avatar
Yann Gautier committed
1324
	mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
1325
1326
1327
			   RCC_HSICFGR_HSIDIV_MASK,
			   RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);

1328
	timeout = timeout_init_us(HSIDIV_TIMEOUT);
1329
	while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
1330
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1331
			ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
1332
1333
1334
1335
1336
1337
1338
1339
			      address, mmio_read_32(address));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1340
static int stm32mp1_hsidiv(unsigned long hsifreq)
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
{
	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
1359
		return stm32mp1_set_hsidiv(hsidiv);
1360
1361
1362
1363
1364
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
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;
1376
1377
	uint32_t src, value, fracv = 0;
	void *fdt;
Yann Gautier's avatar
Yann Gautier committed
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415

	/* 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 */
1416
1417
1418
	if (fdt_get_address(&fdt) == 1) {
		fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
	}
Yann Gautier's avatar
Yann Gautier committed
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440

	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)
1441
{
Yann Gautier's avatar
Yann Gautier committed
1442
1443
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1444

1445
1446
1447
1448
1449
	/* Preserve RCC_PLLNCR_SSCG_CTRL value */
	mmio_clrsetbits_32(pllxcr,
			   RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
			   RCC_PLLNCR_DIVREN,
			   RCC_PLLNCR_PLLON);
1450
1451
}

Yann Gautier's avatar
Yann Gautier committed
1452
static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
1453
{
Yann Gautier's avatar
Yann Gautier committed
1454
1455
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1456
	uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
1457
1458
1459

	/* Wait PLL lock */
	while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
1460
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1461
			ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
			      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
1473
static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
1474
{
Yann Gautier's avatar
Yann Gautier committed
1475
1476
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1477
	uint64_t timeout;
1478
1479
1480
1481
1482
1483
1484
1485

	/* 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);

1486
	timeout = timeout_init_us(PLLRDY_TIMEOUT);
1487
1488
	/* Wait PLL stopped */
	while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
1489
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1490
			ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
1491
1492
1493
1494
1495
1496
1497
1498
			      pll_id, pllxcr, mmio_read_32(pllxcr));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1499
static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
1500
1501
				       uint32_t *pllcfg)
{
Yann Gautier's avatar
Yann Gautier committed
1502
1503
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t rcc_base = stm32mp_rcc_base();
1504
1505
1506
1507
1508
1509
1510
1511
	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
1512
	mmio_write_32(rcc_base + pll->pllxcfgr2, value);
1513
1514
}

Yann Gautier's avatar
Yann Gautier committed
1515
static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
1516
1517
			       uint32_t *pllcfg, uint32_t fracv)
{
Yann Gautier's avatar
Yann Gautier committed
1518
1519
1520
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
	uintptr_t rcc_base = stm32mp_rcc_base();
	enum stm32mp1_plltype type = pll->plltype;
1521
1522
1523
1524
	unsigned long refclk;
	uint32_t ifrge = 0;
	uint32_t src, value;

Yann Gautier's avatar
Yann Gautier committed
1525
	src = mmio_read_32(rcc_base + pll->rckxselr) &
1526
1527
		RCC_SELR_REFCLK_SRC_MASK;

Yann Gautier's avatar
Yann Gautier committed
1528
	refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
		 (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
1546
	mmio_write_32(rcc_base + pll->pllxcfgr1, value);
1547
1548
1549

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

	value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier's avatar
Yann Gautier committed
1553
	mmio_write_32(rcc_base + pll->pllxfracr, value);
1554
1555

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

Yann Gautier's avatar
Yann Gautier committed
1558
	stm32mp1_pll_config_output(pll_id, pllcfg);
1559
1560
1561
1562

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1563
static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
1564
{
Yann Gautier's avatar
Yann Gautier committed
1565
	const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
	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
1577
	mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
1578
1579
1580

	mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
			RCC_PLLNCR_SSCG_CTRL);
1581
1582
}

Yann Gautier's avatar
Yann Gautier committed
1583
static int stm32mp1_set_clksrc(unsigned int clksrc)
1584
{
Yann Gautier's avatar
Yann Gautier committed
1585
	uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
1586
	uint64_t timeout;
1587

Yann Gautier's avatar
Yann Gautier committed
1588
	mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
1589
1590
			   clksrc & RCC_SELR_SRC_MASK);

1591
	timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautier's avatar
Yann Gautier committed
1592
	while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
1593
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1594
1595
			ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
			      clksrc_address, mmio_read_32(clksrc_address));
1596
1597
1598
1599
1600
1601
1602
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1603
static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
1604
{
1605
	uint64_t timeout;
1606
1607
1608
1609

	mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
			   clkdiv & RCC_DIVR_DIV_MASK);

1610
	timeout = timeout_init_us(CLKDIV_TIMEOUT);
1611
	while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
1612
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
1613
			ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
1614
1615
1616
1617
1618
1619
1620
1621
			      clkdiv, address, mmio_read_32(address));
			return -ETIMEDOUT;
		}
	}

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
1622
static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
1623
{
Yann Gautier's avatar
Yann Gautier committed
1624
	uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
1625
1626
1627
1628
1629
1630
1631
1632

	/*
	 * Binding clksrc :
	 *      bit15-4 offset
	 *      bit3:   disable
	 *      bit2-0: MCOSEL[2:0]
	 */
	if ((clksrc & 0x8U) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
1633
		mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
1634
	} else {
Yann Gautier's avatar
Yann Gautier committed
1635
		mmio_clrsetbits_32(clksrc_address,
1636
1637
				   RCC_MCOCFG_MCOSRC_MASK,
				   clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautier's avatar
Yann Gautier committed
1638
		mmio_clrsetbits_32(clksrc_address,
1639
1640
				   RCC_MCOCFG_MCODIV_MASK,
				   clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautier's avatar
Yann Gautier committed
1641
		mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
1642
1643
1644
	}
}

Yann Gautier's avatar
Yann Gautier committed
1645
static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
1646
{
Yann Gautier's avatar
Yann Gautier committed
1647
	uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662

	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
1663
static void stm32mp1_stgen_config(void)
1664
1665
1666
1667
{
	uintptr_t stgen;
	uint32_t cntfid0;
	unsigned long rate;
Yann Gautier's avatar
Yann Gautier committed
1668
	unsigned long long counter;
1669
1670
1671

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

Yann Gautier's avatar
Yann Gautier committed
1674
1675
1676
	if (cntfid0 == rate) {
		return;
	}
1677

Yann Gautier's avatar
Yann Gautier committed
1678
1679
1680
1681
	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);
1682

Yann Gautier's avatar
Yann Gautier committed
1683
1684
1685
1686
	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);
1687

Yann Gautier's avatar
Yann Gautier committed
1688
1689
1690
1691
	write_cntfrq((u_register_t)rate);

	/* Need to update timer with new frequency */
	generic_delay_timer_init();
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
}

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
1712
static void stm32mp1_pkcs_config(uint32_t pkcs)
1713
{
Yann Gautier's avatar
Yann Gautier committed
1714
	uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
	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
1728
	uintptr_t rcc_base = stm32mp_rcc_base();
1729
1730
1731
1732
1733
1734
1735
	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
1736
1737
1738
	bool pll3_preserve = false;
	bool pll4_preserve = false;
	bool pll4_bootrom = false;
1739
	const fdt32_t *pkcs_cell;
1740
1741
1742
1743
1744
	void *fdt;

	if (fdt_get_address(&fdt) == 0) {
		return false;
	}
1745
1746
1747

	/* Check status field to disable security */
	if (!fdt_get_rcc_secure_status()) {
Yann Gautier's avatar
Yann Gautier committed
1748
		mmio_write_32(rcc_base + RCC_TZCR, 0);
1749
1750
	}

1751
1752
	ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
					clksrc);
1753
1754
1755
1756
	if (ret < 0) {
		return -FDT_ERR_NOTFOUND;
	}

1757
1758
	ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
					clkdiv);
1759
1760
1761
1762
1763
1764
1765
	if (ret < 0) {
		return -FDT_ERR_NOTFOUND;
	}

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

1766
		snprintf(name, sizeof(name), "st,pll@%d", i);
1767
1768
1769
1770
1771
1772
		plloff[i] = fdt_rcc_subnode_offset(name);

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

1773
1774
		ret = fdt_read_uint32_array(fdt, plloff[i], "cfg",
					    (int)PLLCFG_NB, pllcfg[i]);
1775
1776
1777
1778
1779
		if (ret < 0) {
			return -FDT_ERR_NOTFOUND;
		}
	}

Yann Gautier's avatar
Yann Gautier committed
1780
1781
	stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
	stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1782
1783
1784
1785
1786

	/*
	 * Switch ON oscillator found in device-tree.
	 * Note: HSI already ON after BootROM stage.
	 */
Yann Gautier's avatar
Yann Gautier committed
1787
1788
	if (stm32mp1_osc[_LSI] != 0U) {
		stm32mp1_lsi_set(true);
1789
	}
Yann Gautier's avatar
Yann Gautier committed
1790
1791
	if (stm32mp1_osc[_LSE] != 0U) {
		bool bypass, digbyp;
1792
1793
1794
		uint32_t lsedrv;

		bypass = fdt_osc_read_bool(_LSE, "st,bypass");
Yann Gautier's avatar
Yann Gautier committed
1795
		digbyp = fdt_osc_read_bool(_LSE, "st,digbypass");
1796
1797
1798
		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
1799
		stm32mp1_lse_enable(bypass, digbyp, lsedrv);
1800
	}
Yann Gautier's avatar
Yann Gautier committed
1801
1802
	if (stm32mp1_osc[_HSE] != 0U) {
		bool bypass, digbyp, css;
1803

Yann Gautier's avatar
Yann Gautier committed
1804
1805
1806
1807
		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);
1808
1809
1810
1811
1812
	}
	/*
	 * 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
1813
	stm32mp1_csi_set(true);
1814
1815

	/* Come back to HSI */
Yann Gautier's avatar
Yann Gautier committed
1816
	ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
1817
1818
1819
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1820
	ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
1821
1822
1823
	if (ret != 0) {
		return ret;
	}
1824
1825
1826
1827
	ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
	if (ret != 0) {
		return ret;
	}
1828

Yann Gautier's avatar
Yann Gautier committed
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
	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]);
	}

1841
	for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautier's avatar
Yann Gautier committed
1842
1843
		if (((i == _PLL3) && pll3_preserve) ||
		    ((i == _PLL4) && pll4_preserve)) {
1844
			continue;
Yann Gautier's avatar
Yann Gautier committed
1845
1846
1847
		}

		ret = stm32mp1_pll_stop(i);
1848
1849
1850
1851
1852
1853
		if (ret != 0) {
			return ret;
		}
	}

	/* Configure HSIDIV */
Yann Gautier's avatar
Yann Gautier committed
1854
1855
	if (stm32mp1_osc[_HSI] != 0U) {
		ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
1856
1857
1858
		if (ret != 0) {
			return ret;
		}
Yann Gautier's avatar
Yann Gautier committed
1859
		stm32mp1_stgen_config();
1860
1861
1862
1863
	}

	/* Select DIV */
	/* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautier's avatar
Yann Gautier committed
1864
	mmio_write_32(rcc_base + RCC_MPCKDIVR,
1865
		      clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautier's avatar
Yann Gautier committed
1866
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
1867
1868
1869
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1870
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
1871
1872
1873
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1874
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
1875
1876
1877
	if (ret != 0) {
		return ret;
	}
1878
1879
1880
1881
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1882
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
1883
1884
1885
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1886
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
1887
1888
1889
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1890
	ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
1891
1892
1893
1894
1895
	if (ret != 0) {
		return ret;
	}

	/* No ready bit for RTC */
Yann Gautier's avatar
Yann Gautier committed
1896
	mmio_write_32(rcc_base + RCC_RTCDIVR,
1897
1898
1899
		      clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);

	/* Configure PLLs source */
Yann Gautier's avatar
Yann Gautier committed
1900
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
1901
1902
1903
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1904
1905
1906
1907
1908
1909

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

Yann Gautier's avatar
Yann Gautier committed
1912
1913
1914
1915
1916
	if (!pll4_preserve) {
		ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
		if (ret != 0) {
			return ret;
		}
1917
1918
1919
1920
1921
1922
1923
	}

	/* 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
1924
1925
1926
1927
1928
		if (((i == _PLL3) && pll3_preserve) ||
		    ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
			continue;
		}

1929
1930
1931
1932
		if (!fdt_check_node(plloff[i])) {
			continue;
		}

Yann Gautier's avatar
Yann Gautier committed
1933
1934
1935
1936
1937
1938
		if ((i == _PLL4) && pll4_bootrom) {
			/* Set output divider if not done by the Bootrom */
			stm32mp1_pll_config_output(i, pllcfg[i]);
			continue;
		}

1939
		fracv = fdt_read_uint32_default(fdt, plloff[i], "frac", 0);
1940

Yann Gautier's avatar
Yann Gautier committed
1941
		ret = stm32mp1_pll_config(i, pllcfg[i], fracv);
1942
1943
1944
		if (ret != 0) {
			return ret;
		}
1945
1946
		ret = fdt_read_uint32_array(fdt, plloff[i], "csg",
					    (uint32_t)PLLCSG_NB, csg);
1947
		if (ret == 0) {
Yann Gautier's avatar
Yann Gautier committed
1948
			stm32mp1_pll_csg(i, csg);
1949
1950
1951
1952
		} else if (ret != -FDT_ERR_NOTFOUND) {
			return ret;
		}

Yann Gautier's avatar
Yann Gautier committed
1953
		stm32mp1_pll_start(i);
1954
1955
1956
1957
1958
1959
1960
	}
	/* 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
1961
		ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
1962
1963
1964
1965
1966
		if (ret != 0) {
			return ret;
		}
	}
	/* Wait LSE ready before to use it */
Yann Gautier's avatar
Yann Gautier committed
1967
1968
	if (stm32mp1_osc[_LSE] != 0U) {
		stm32mp1_lse_wait();
1969
1970
1971
	}

	/* Configure with expected clock source */
Yann Gautier's avatar
Yann Gautier committed
1972
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
1973
1974
1975
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1976
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
1977
1978
1979
	if (ret != 0) {
		return ret;
	}
1980
1981
1982
1983
	ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
	if (ret != 0) {
		return ret;
	}
Yann Gautier's avatar
Yann Gautier committed
1984
	stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
1985
1986
1987
1988
1989
1990
1991
1992

	/* 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++) {
1993
			uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
1994
1995
1996
1997
1998

			if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
				ckper_disabled = true;
				continue;
			}
Yann Gautier's avatar
Yann Gautier committed
1999
			stm32mp1_pkcs_config(pkcs);
2000
2001
2002
2003
2004
2005
2006
2007
2008
		}

		/*
		 * 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
2009
			stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
2010
2011
2012
2013
		}
	}

	/* Switch OFF HSI if not found in device-tree */
Yann Gautier's avatar
Yann Gautier committed
2014
2015
	if (stm32mp1_osc[_HSI] == 0U) {
		stm32mp1_hsi_set(false);
2016
	}
Yann Gautier's avatar
Yann Gautier committed
2017
	stm32mp1_stgen_config();
2018
2019

	/* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautier's avatar
Yann Gautier committed
2020
	mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
			   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
2033
2034
	if (fdt_osc_read_freq(name, &frequency) == 0) {
		stm32mp1_osc[index] = frequency;
2035
2036
2037
2038
2039
2040
2041
2042
	}
}

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
2043
		stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
2044
2045
2046
	}
}

2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
#ifdef STM32MP_SHARED_RESOURCES
/*
 * Get the parent ID of the target parent clock, for tagging as secure
 * shared clock dependencies.
 */
static int get_parent_id_parent(unsigned int parent_id)
{
	enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
	enum stm32mp1_pll_id pll_id;
	uint32_t p_sel;
	uintptr_t rcc_base = stm32mp_rcc_base();

	switch (parent_id) {
	case _ACLK:
	case _PCLK4:
	case _PCLK5:
		s = _AXIS_SEL;
		break;
	case _PLL1_P:
	case _PLL1_Q:
	case _PLL1_R:
		pll_id = _PLL1;
		break;
	case _PLL2_P:
	case _PLL2_Q:
	case _PLL2_R:
		pll_id = _PLL2;
		break;
	case _PLL3_P:
	case _PLL3_Q:
	case _PLL3_R:
		pll_id = _PLL3;
		break;
	case _PLL4_P:
	case _PLL4_Q:
	case _PLL4_R:
		pll_id = _PLL4;
		break;
	case _PCLK1:
	case _PCLK2:
	case _HCLK2:
	case _HCLK6:
	case _CK_PER:
	case _CK_MPU:
	case _CK_MCU:
	case _USB_PHY_48:
		/* We do not expect to access these */
		panic();
		break;
	default:
		/* Other parents have no parent */
		return -1;
	}

	if (s != _UNKNOWN_SEL) {
		const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);

		p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
			sel->msk;

		if (p_sel < sel->nb_parent) {
			return (int)sel->parent[p_sel];
		}
	} else {
		const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);

		p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
			RCC_SELR_REFCLK_SRC_MASK;

		if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
			return (int)pll->refclk[p_sel];
		}
	}

	VERBOSE("No parent selected for %s\n",
		stm32mp1_clk_parent_name[parent_id]);

	return -1;
}

static void secure_parent_clocks(unsigned long parent_id)
{
	int grandparent_id;

	switch (parent_id) {
	case _PLL3_P:
	case _PLL3_Q:
	case _PLL3_R:
		stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
		break;

	/* These clocks are always secure when RCC is secure */
	case _ACLK:
	case _HCLK2:
	case _HCLK6:
	case _PCLK4:
	case _PCLK5:
	case _PLL1_P:
	case _PLL1_Q:
	case _PLL1_R:
	case _PLL2_P:
	case _PLL2_Q:
	case _PLL2_R:
	case _HSI:
	case _HSI_KER:
	case _LSI:
	case _CSI:
	case _CSI_KER:
	case _HSE:
	case _HSE_KER:
	case _HSE_KER_DIV2:
	case _LSE:
		break;

	default:
		VERBOSE("Cannot secure parent clock %s\n",
			stm32mp1_clk_parent_name[parent_id]);
		panic();
	}

	grandparent_id = get_parent_id_parent(parent_id);
	if (grandparent_id >= 0) {
		secure_parent_clocks(grandparent_id);
	}
}

void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
{
	int parent_id;

	if (!stm32mp1_rcc_is_secure()) {
		return;
	}

	switch (clock_id) {
	case PLL1:
	case PLL2:
		/* PLL1/PLL2 are always secure: nothing to do */
		break;
	case PLL3:
		stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
		break;
	case PLL4:
		ERROR("PLL4 cannot be secured\n");
		panic();
		break;
	default:
		/* Others are expected gateable clock */
		parent_id = stm32mp1_clk_get_parent(clock_id);
		if (parent_id < 0) {
			INFO("No parent found for clock %lu\n", clock_id);
		} else {
			secure_parent_clocks(parent_id);
		}
		break;
	}
}
#endif /* STM32MP_SHARED_RESOURCES */

2206
2207
static void sync_earlyboot_clocks_state(void)
{
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
	unsigned int idx;
	const unsigned long secure_enable[] = {
		AXIDCG,
		BSEC,
		DDRC1, DDRC1LP,
		DDRC2, DDRC2LP,
		DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
		DDRPHYC, DDRPHYCLP,
		TZC1, TZC2,
		TZPC,
		STGEN_K,
	};

	for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
		stm32mp_clk_enable(secure_enable[idx]);
	}

2225
2226
2227
2228
2229
	if (!stm32mp_is_single_core()) {
		stm32mp1_clk_enable_secure(RTCAPB);
	}
}

2230
2231
2232
2233
int stm32mp1_clk_probe(void)
{
	stm32mp1_osc_init();

2234
2235
	sync_earlyboot_clocks_state();

2236
2237
	return 0;
}