xref: /optee_os/core/drivers/clk/clk-stm32mp13.c (revision 6a977fc40fb1e5069ee1a0d3da67d52759c9ef3b)
1e5e793a6SGabriel Fernandez // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2e5e793a6SGabriel Fernandez /*
3e5e793a6SGabriel Fernandez  * Copyright (C) STMicroelectronics 2022 - All Rights Reserved
4e5e793a6SGabriel Fernandez  */
5e5e793a6SGabriel Fernandez 
6e5e793a6SGabriel Fernandez #include <assert.h>
7e5e793a6SGabriel Fernandez #include <drivers/clk.h>
8e5e793a6SGabriel Fernandez #include <drivers/clk_dt.h>
9e5e793a6SGabriel Fernandez #include <drivers/stm32mp_dt_bindings.h>
10e5e793a6SGabriel Fernandez #include <drivers/stm32mp13_rcc.h>
11e5e793a6SGabriel Fernandez #include <io.h>
12e5e793a6SGabriel Fernandez #include <kernel/boot.h>
13e5e793a6SGabriel Fernandez #include <libfdt.h>
14e5e793a6SGabriel Fernandez #include <stdio.h>
15e5e793a6SGabriel Fernandez 
16e5e793a6SGabriel Fernandez #include "clk-stm32-core.h"
17e5e793a6SGabriel Fernandez 
18e5e793a6SGabriel Fernandez #define MAX_HSI_HZ		64000000
19e5e793a6SGabriel Fernandez #define USB_PHY_48_MHZ		48000000
20e5e793a6SGabriel Fernandez 
21e5e793a6SGabriel Fernandez #define TIMEOUT_US_200MS	U(200000)
22e5e793a6SGabriel Fernandez #define HSIDIV_TIMEOUT		TIMEOUT_US_200MS
23e5e793a6SGabriel Fernandez 
24e5e793a6SGabriel Fernandez #define MAX_OPP		CFG_STM32MP_OPP_COUNT
25e5e793a6SGabriel Fernandez 
26e5e793a6SGabriel Fernandez #define RCC_PLL_NAME_SIZE 12
27e5e793a6SGabriel Fernandez 
28e5e793a6SGabriel Fernandez struct stm32_osci_dt_cfg {
29e5e793a6SGabriel Fernandez 	unsigned long freq;
30e5e793a6SGabriel Fernandez 	bool bypass;
31e5e793a6SGabriel Fernandez 	bool digbyp;
32e5e793a6SGabriel Fernandez 	bool css;
33e5e793a6SGabriel Fernandez 	uint32_t drive;
34e5e793a6SGabriel Fernandez };
35e5e793a6SGabriel Fernandez 
36e5e793a6SGabriel Fernandez enum pll_mn {
37e5e793a6SGabriel Fernandez 	PLL_CFG_M,
38e5e793a6SGabriel Fernandez 	PLL_CFG_N,
39e5e793a6SGabriel Fernandez 	PLL_DIV_MN_NB
40e5e793a6SGabriel Fernandez };
41e5e793a6SGabriel Fernandez 
42e5e793a6SGabriel Fernandez enum pll_pqr {
43e5e793a6SGabriel Fernandez 	PLL_CFG_P,
44e5e793a6SGabriel Fernandez 	PLL_CFG_Q,
45e5e793a6SGabriel Fernandez 	PLL_CFG_R,
46e5e793a6SGabriel Fernandez 	PLL_DIV_PQR_NB
47e5e793a6SGabriel Fernandez };
48e5e793a6SGabriel Fernandez 
49e5e793a6SGabriel Fernandez enum pll_csg {
50e5e793a6SGabriel Fernandez 	PLL_CSG_MOD_PER,
51e5e793a6SGabriel Fernandez 	PLL_CSG_INC_STEP,
52e5e793a6SGabriel Fernandez 	PLL_CSG_SSCG_MODE,
53e5e793a6SGabriel Fernandez 	PLL_CSG_NB
54e5e793a6SGabriel Fernandez };
55e5e793a6SGabriel Fernandez 
56e5e793a6SGabriel Fernandez struct stm32_pll_vco {
57e5e793a6SGabriel Fernandez 	uint32_t status;
58e5e793a6SGabriel Fernandez 	uint32_t src;
59e5e793a6SGabriel Fernandez 	uint32_t div_mn[PLL_DIV_MN_NB];
60e5e793a6SGabriel Fernandez 	uint32_t frac;
61e5e793a6SGabriel Fernandez 	bool csg_enabled;
62e5e793a6SGabriel Fernandez 	uint32_t csg[PLL_CSG_NB];
63e5e793a6SGabriel Fernandez };
64e5e793a6SGabriel Fernandez 
65e5e793a6SGabriel Fernandez struct stm32_pll_output {
66e5e793a6SGabriel Fernandez 	uint32_t output[PLL_DIV_PQR_NB];
67e5e793a6SGabriel Fernandez };
68e5e793a6SGabriel Fernandez 
69e5e793a6SGabriel Fernandez struct stm32_pll_dt_cfg {
70e5e793a6SGabriel Fernandez 	struct stm32_pll_vco vco;
71e5e793a6SGabriel Fernandez 	struct stm32_pll_output output;
72e5e793a6SGabriel Fernandez };
73e5e793a6SGabriel Fernandez 
74e5e793a6SGabriel Fernandez struct stm32_clk_opp_cfg {
75e5e793a6SGabriel Fernandez 	uint32_t frq;
76e5e793a6SGabriel Fernandez 	uint32_t src;
77e5e793a6SGabriel Fernandez 	uint32_t div;
78e5e793a6SGabriel Fernandez 	struct stm32_pll_dt_cfg pll_cfg;
79e5e793a6SGabriel Fernandez };
80e5e793a6SGabriel Fernandez 
81e5e793a6SGabriel Fernandez struct stm32_clk_opp_dt_cfg {
82e5e793a6SGabriel Fernandez 	struct stm32_clk_opp_cfg mpu_opp[MAX_OPP];
83e5e793a6SGabriel Fernandez 	struct stm32_clk_opp_cfg axi_opp[MAX_OPP];
84e5e793a6SGabriel Fernandez 	struct stm32_clk_opp_cfg mlahbs_opp[MAX_OPP];
85e5e793a6SGabriel Fernandez };
86e5e793a6SGabriel Fernandez 
87e5e793a6SGabriel Fernandez struct stm32_clk_platdata {
88e5e793a6SGabriel Fernandez 	uintptr_t rcc_base;
89e5e793a6SGabriel Fernandez 	uint32_t nosci;
90e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci;
91e5e793a6SGabriel Fernandez 	uint32_t npll;
92e5e793a6SGabriel Fernandez 	struct stm32_pll_dt_cfg *pll;
93e5e793a6SGabriel Fernandez 	struct stm32_clk_opp_dt_cfg *opp;
94e5e793a6SGabriel Fernandez 	uint32_t nclksrc;
95e5e793a6SGabriel Fernandez 	uint32_t *clksrc;
96e5e793a6SGabriel Fernandez 	uint32_t nclkdiv;
97e5e793a6SGabriel Fernandez 	uint32_t *clkdiv;
98e5e793a6SGabriel Fernandez };
99e5e793a6SGabriel Fernandez 
100e5e793a6SGabriel Fernandez /*
101e5e793a6SGabriel Fernandez  * GATE CONFIG
102e5e793a6SGabriel Fernandez  */
103e5e793a6SGabriel Fernandez 
104e5e793a6SGabriel Fernandez /* Warning GATE_XXX_RDY must follow GATE_XXX */
105e5e793a6SGabriel Fernandez enum enum_gate_cfg {
106e5e793a6SGabriel Fernandez 	GATE_LSE,
107e5e793a6SGabriel Fernandez 	GATE_LSE_RDY,
108e5e793a6SGabriel Fernandez 	GATE_LSI,
109e5e793a6SGabriel Fernandez 	GATE_LSI_RDY,
110e5e793a6SGabriel Fernandez 	GATE_HSI,
111e5e793a6SGabriel Fernandez 	GATE_HSI_RDY,
112e5e793a6SGabriel Fernandez 	GATE_CSI,
113e5e793a6SGabriel Fernandez 	GATE_CSI_RDY,
114e5e793a6SGabriel Fernandez 	GATE_HSE,
115e5e793a6SGabriel Fernandez 	GATE_HSE_RDY,
116e5e793a6SGabriel Fernandez 	GATE_PLL1,
117e5e793a6SGabriel Fernandez 	GATE_PLL1_RDY,
118e5e793a6SGabriel Fernandez 	GATE_PLL2,
119e5e793a6SGabriel Fernandez 	GATE_PLL2_RDY,
120e5e793a6SGabriel Fernandez 	GATE_PLL3,
121e5e793a6SGabriel Fernandez 	GATE_PLL3_RDY,
122e5e793a6SGabriel Fernandez 	GATE_PLL4,
123e5e793a6SGabriel Fernandez 	GATE_PLL4_RDY,
124e5e793a6SGabriel Fernandez 	GATE_HSIDIVRDY,
125e5e793a6SGabriel Fernandez 	GATE_MPUSRCRDY,
126e5e793a6SGabriel Fernandez 	GATE_AXISSRCRDY,
127e5e793a6SGabriel Fernandez 	GATE_MCUSSRCRDY,
128e5e793a6SGabriel Fernandez 	GATE_PLL12SRCRDY,
129e5e793a6SGabriel Fernandez 	GATE_PLL3SRCRDY,
130e5e793a6SGabriel Fernandez 	GATE_PLL4SRCRDY,
131e5e793a6SGabriel Fernandez 	GATE_MPUDIVRDY,
132e5e793a6SGabriel Fernandez 	GATE_AXIDIVRDY,
133e5e793a6SGabriel Fernandez 	GATE_MLAHBDIVRDY,
134e5e793a6SGabriel Fernandez 	GATE_APB1DIVRDY,
135e5e793a6SGabriel Fernandez 	GATE_APB2DIVRDY,
136e5e793a6SGabriel Fernandez 	GATE_APB3DIVRDY,
137e5e793a6SGabriel Fernandez 	GATE_APB4DIVRDY,
138e5e793a6SGabriel Fernandez 	GATE_APB5DIVRDY,
139e5e793a6SGabriel Fernandez 	GATE_APB6DIVRDY,
140e5e793a6SGabriel Fernandez 	GATE_RTCCK,
141e5e793a6SGabriel Fernandez 	GATE_MCO1,
142e5e793a6SGabriel Fernandez 	GATE_MCO2,
143e5e793a6SGabriel Fernandez 	GATE_DBGCK,
144e5e793a6SGabriel Fernandez 	GATE_TRACECK,
145e5e793a6SGabriel Fernandez 	GATE_PLL1_DIVP,
146e5e793a6SGabriel Fernandez 	GATE_PLL1_DIVQ,
147e5e793a6SGabriel Fernandez 	GATE_PLL1_DIVR,
148e5e793a6SGabriel Fernandez 	GATE_PLL2_DIVP,
149e5e793a6SGabriel Fernandez 	GATE_PLL2_DIVQ,
150e5e793a6SGabriel Fernandez 	GATE_PLL2_DIVR,
151e5e793a6SGabriel Fernandez 	GATE_PLL3_DIVP,
152e5e793a6SGabriel Fernandez 	GATE_PLL3_DIVQ,
153e5e793a6SGabriel Fernandez 	GATE_PLL3_DIVR,
154e5e793a6SGabriel Fernandez 	GATE_PLL4_DIVP,
155e5e793a6SGabriel Fernandez 	GATE_PLL4_DIVQ,
156e5e793a6SGabriel Fernandez 	GATE_PLL4_DIVR,
157e5e793a6SGabriel Fernandez 	GATE_DDRC1,
158e5e793a6SGabriel Fernandez 	GATE_DDRC1LP,
159e5e793a6SGabriel Fernandez 	GATE_DDRPHYC,
160e5e793a6SGabriel Fernandez 	GATE_DDRPHYCLP,
161e5e793a6SGabriel Fernandez 	GATE_DDRCAPB,
162e5e793a6SGabriel Fernandez 	GATE_DDRCAPBLP,
163e5e793a6SGabriel Fernandez 	GATE_AXIDCG,
164e5e793a6SGabriel Fernandez 	GATE_DDRPHYCAPB,
165e5e793a6SGabriel Fernandez 	GATE_DDRPHYCAPBLP,
166e5e793a6SGabriel Fernandez 	GATE_TIM2,
167e5e793a6SGabriel Fernandez 	GATE_TIM3,
168e5e793a6SGabriel Fernandez 	GATE_TIM4,
169e5e793a6SGabriel Fernandez 	GATE_TIM5,
170e5e793a6SGabriel Fernandez 	GATE_TIM6,
171e5e793a6SGabriel Fernandez 	GATE_TIM7,
172e5e793a6SGabriel Fernandez 	GATE_LPTIM1,
173e5e793a6SGabriel Fernandez 	GATE_SPI2,
174e5e793a6SGabriel Fernandez 	GATE_SPI3,
175e5e793a6SGabriel Fernandez 	GATE_USART3,
176e5e793a6SGabriel Fernandez 	GATE_UART4,
177e5e793a6SGabriel Fernandez 	GATE_UART5,
178e5e793a6SGabriel Fernandez 	GATE_UART7,
179e5e793a6SGabriel Fernandez 	GATE_UART8,
180e5e793a6SGabriel Fernandez 	GATE_I2C1,
181e5e793a6SGabriel Fernandez 	GATE_I2C2,
182e5e793a6SGabriel Fernandez 	GATE_SPDIF,
183e5e793a6SGabriel Fernandez 	GATE_TIM1,
184e5e793a6SGabriel Fernandez 	GATE_TIM8,
185e5e793a6SGabriel Fernandez 	GATE_SPI1,
186e5e793a6SGabriel Fernandez 	GATE_USART6,
187e5e793a6SGabriel Fernandez 	GATE_SAI1,
188e5e793a6SGabriel Fernandez 	GATE_SAI2,
189e5e793a6SGabriel Fernandez 	GATE_DFSDM,
190e5e793a6SGabriel Fernandez 	GATE_ADFSDM,
191e5e793a6SGabriel Fernandez 	GATE_FDCAN,
192e5e793a6SGabriel Fernandez 	GATE_LPTIM2,
193e5e793a6SGabriel Fernandez 	GATE_LPTIM3,
194e5e793a6SGabriel Fernandez 	GATE_LPTIM4,
195e5e793a6SGabriel Fernandez 	GATE_LPTIM5,
196e5e793a6SGabriel Fernandez 	GATE_VREF,
197e5e793a6SGabriel Fernandez 	GATE_DTS,
198e5e793a6SGabriel Fernandez 	GATE_PMBCTRL,
199e5e793a6SGabriel Fernandez 	GATE_HDP,
200e5e793a6SGabriel Fernandez 	GATE_SYSCFG,
201e5e793a6SGabriel Fernandez 	GATE_DCMIPP,
202e5e793a6SGabriel Fernandez 	GATE_DDRPERFM,
203e5e793a6SGabriel Fernandez 	GATE_IWDG2APB,
204e5e793a6SGabriel Fernandez 	GATE_USBPHY,
205e5e793a6SGabriel Fernandez 	GATE_STGENRO,
206e5e793a6SGabriel Fernandez 	GATE_LTDC,
207e5e793a6SGabriel Fernandez 	GATE_RTCAPB,
208e5e793a6SGabriel Fernandez 	GATE_TZC,
209e5e793a6SGabriel Fernandez 	GATE_ETZPC,
210e5e793a6SGabriel Fernandez 	GATE_IWDG1APB,
211e5e793a6SGabriel Fernandez 	GATE_BSEC,
212e5e793a6SGabriel Fernandez 	GATE_STGENC,
213e5e793a6SGabriel Fernandez 	GATE_USART1,
214e5e793a6SGabriel Fernandez 	GATE_USART2,
215e5e793a6SGabriel Fernandez 	GATE_SPI4,
216e5e793a6SGabriel Fernandez 	GATE_SPI5,
217e5e793a6SGabriel Fernandez 	GATE_I2C3,
218e5e793a6SGabriel Fernandez 	GATE_I2C4,
219e5e793a6SGabriel Fernandez 	GATE_I2C5,
220e5e793a6SGabriel Fernandez 	GATE_TIM12,
221e5e793a6SGabriel Fernandez 	GATE_TIM13,
222e5e793a6SGabriel Fernandez 	GATE_TIM14,
223e5e793a6SGabriel Fernandez 	GATE_TIM15,
224e5e793a6SGabriel Fernandez 	GATE_TIM16,
225e5e793a6SGabriel Fernandez 	GATE_TIM17,
226e5e793a6SGabriel Fernandez 	GATE_DMA1,
227e5e793a6SGabriel Fernandez 	GATE_DMA2,
228e5e793a6SGabriel Fernandez 	GATE_DMAMUX1,
229e5e793a6SGabriel Fernandez 	GATE_DMA3,
230e5e793a6SGabriel Fernandez 	GATE_DMAMUX2,
231e5e793a6SGabriel Fernandez 	GATE_ADC1,
232e5e793a6SGabriel Fernandez 	GATE_ADC2,
233e5e793a6SGabriel Fernandez 	GATE_USBO,
234e5e793a6SGabriel Fernandez 	GATE_TSC,
235e5e793a6SGabriel Fernandez 	GATE_GPIOA,
236e5e793a6SGabriel Fernandez 	GATE_GPIOB,
237e5e793a6SGabriel Fernandez 	GATE_GPIOC,
238e5e793a6SGabriel Fernandez 	GATE_GPIOD,
239e5e793a6SGabriel Fernandez 	GATE_GPIOE,
240e5e793a6SGabriel Fernandez 	GATE_GPIOF,
241e5e793a6SGabriel Fernandez 	GATE_GPIOG,
242e5e793a6SGabriel Fernandez 	GATE_GPIOH,
243e5e793a6SGabriel Fernandez 	GATE_GPIOI,
244e5e793a6SGabriel Fernandez 	GATE_PKA,
245e5e793a6SGabriel Fernandez 	GATE_SAES,
246e5e793a6SGabriel Fernandez 	GATE_CRYP1,
247e5e793a6SGabriel Fernandez 	GATE_HASH1,
248e5e793a6SGabriel Fernandez 	GATE_RNG1,
249e5e793a6SGabriel Fernandez 	GATE_BKPSRAM,
250e5e793a6SGabriel Fernandez 	GATE_AXIMC,
251e5e793a6SGabriel Fernandez 	GATE_MCE,
252e5e793a6SGabriel Fernandez 	GATE_ETH1CK,
253e5e793a6SGabriel Fernandez 	GATE_ETH1TX,
254e5e793a6SGabriel Fernandez 	GATE_ETH1RX,
255e5e793a6SGabriel Fernandez 	GATE_ETH1MAC,
256e5e793a6SGabriel Fernandez 	GATE_FMC,
257e5e793a6SGabriel Fernandez 	GATE_QSPI,
258e5e793a6SGabriel Fernandez 	GATE_SDMMC1,
259e5e793a6SGabriel Fernandez 	GATE_SDMMC2,
260e5e793a6SGabriel Fernandez 	GATE_CRC1,
261e5e793a6SGabriel Fernandez 	GATE_USBH,
262e5e793a6SGabriel Fernandez 	GATE_ETH2CK,
263e5e793a6SGabriel Fernandez 	GATE_ETH2TX,
264e5e793a6SGabriel Fernandez 	GATE_ETH2RX,
265e5e793a6SGabriel Fernandez 	GATE_ETH2MAC,
266e5e793a6SGabriel Fernandez 	GATE_MDMA,
267e5e793a6SGabriel Fernandez 	GATE_NB
268e5e793a6SGabriel Fernandez };
269e5e793a6SGabriel Fernandez 
270e5e793a6SGabriel Fernandez #define GATE_CFG(_id, _offset, _bit_idx, _offset_clr)\
271e5e793a6SGabriel Fernandez 	[(_id)] = {\
272e5e793a6SGabriel Fernandez 		.offset		= (_offset),\
273e5e793a6SGabriel Fernandez 		.bit_idx	= (_bit_idx),\
274e5e793a6SGabriel Fernandez 		.set_clr	= (_offset_clr),\
275e5e793a6SGabriel Fernandez 	}
276e5e793a6SGabriel Fernandez 
277e5e793a6SGabriel Fernandez static const struct gate_cfg gates_mp13[GATE_NB] = {
278e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LSE,		RCC_BDCR,		0,	0),
279e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LSE_RDY,		RCC_BDCR,		2,	0),
280e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_RTCCK,		RCC_BDCR,		20,	0),
281e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LSI,		RCC_RDLSICR,		0,	0),
282e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LSI_RDY,		RCC_RDLSICR,		1,	0),
283e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HSI,		RCC_OCENSETR,		0,	1),
284e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HSI_RDY,		RCC_OCRDYR,		0,	0),
285e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_CSI,		RCC_OCENSETR,		4,	1),
286e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_CSI_RDY,		RCC_OCRDYR,		4,	0),
287e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HSE,		RCC_OCENSETR,		8,	1),
288e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HSE_RDY,		RCC_OCRDYR,		8,	0),
289e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HSIDIVRDY,	RCC_OCRDYR,		2,	0),
290e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MPUSRCRDY,	RCC_MPCKSELR,		31,	0),
291e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_AXISSRCRDY,	RCC_ASSCKSELR,		31,	0),
292e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MCUSSRCRDY,	RCC_MSSCKSELR,		31,	0),
293e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL12SRCRDY,	RCC_RCK12SELR,		31,	0),
294e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL3SRCRDY,	RCC_RCK3SELR,		31,	0),
295e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL4SRCRDY,	RCC_RCK4SELR,		31,	0),
296e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MPUDIVRDY,	RCC_MPCKDIVR,		31,	0),
297e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_AXIDIVRDY,	RCC_AXIDIVR,		31,	0),
298e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MLAHBDIVRDY,	RCC_MLAHBDIVR,		31,	0),
299e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_APB1DIVRDY,	RCC_APB1DIVR,		31,	0),
300e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_APB2DIVRDY,	RCC_APB2DIVR,		31,	0),
301e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_APB3DIVRDY,	RCC_APB3DIVR,		31,	0),
302e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_APB4DIVRDY,	RCC_APB4DIVR,		31,	0),
303e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_APB5DIVRDY,	RCC_APB5DIVR,		31,	0),
304e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_APB6DIVRDY,	RCC_APB6DIVR,		31,	0),
305e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MCO1,		RCC_MCO1CFGR,		12,	0),
306e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MCO2,		RCC_MCO2CFGR,		12,	0),
307e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DBGCK,		RCC_DBGCFGR,		8,	0),
308e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TRACECK,		RCC_DBGCFGR,		9,	0),
309e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL1,		RCC_PLL1CR,		0,	0),
310e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL1_RDY,		RCC_PLL1CR,		1,	0),
311e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL1_DIVP,	RCC_PLL1CR,		4,	0),
312e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL1_DIVQ,	RCC_PLL1CR,		5,	0),
313e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL1_DIVR,	RCC_PLL1CR,		6,	0),
314e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL2,		RCC_PLL2CR,		0,	0),
315e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL2_RDY,		RCC_PLL2CR,		1,	0),
316e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL2_DIVP,	RCC_PLL2CR,		4,	0),
317e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL2_DIVQ,	RCC_PLL2CR,		5,	0),
318e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL2_DIVR,	RCC_PLL2CR,		6,	0),
319e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL3,		RCC_PLL3CR,		0,	0),
320e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL3_RDY,		RCC_PLL3CR,		1,	0),
321e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL3_DIVP,	RCC_PLL3CR,		4,	0),
322e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL3_DIVQ,	RCC_PLL3CR,		5,	0),
323e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL3_DIVR,	RCC_PLL3CR,		6,	0),
324e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL4,		RCC_PLL4CR,		0,	0),
325e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL4_RDY,		RCC_PLL4CR,		1,	0),
326e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL4_DIVP,	RCC_PLL4CR,		4,	0),
327e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL4_DIVQ,	RCC_PLL4CR,		5,	0),
328e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PLL4_DIVR,	RCC_PLL4CR,		6,	0),
329e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRC1,		RCC_DDRITFCR,		0,	0),
330e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRC1LP,		RCC_DDRITFCR,		1,	0),
331e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRPHYC,		RCC_DDRITFCR,		4,	0),
332e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRPHYCLP,	RCC_DDRITFCR,		5,	0),
333e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRCAPB,		RCC_DDRITFCR,		6,	0),
334e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRCAPBLP,	RCC_DDRITFCR,		7,	0),
335e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_AXIDCG,		RCC_DDRITFCR,		8,	0),
336e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRPHYCAPB,	RCC_DDRITFCR,		9,	0),
337e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRPHYCAPBLP,	RCC_DDRITFCR,		10,	0),
338e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM2,		RCC_MP_APB1ENSETR,	0,	1),
339e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM3,		RCC_MP_APB1ENSETR,	1,	1),
340e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM4,		RCC_MP_APB1ENSETR,	2,	1),
341e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM5,		RCC_MP_APB1ENSETR,	3,	1),
342e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM6,		RCC_MP_APB1ENSETR,	4,	1),
343e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM7,		RCC_MP_APB1ENSETR,	5,	1),
344e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LPTIM1,		RCC_MP_APB1ENSETR,	9,	1),
345e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SPI2,		RCC_MP_APB1ENSETR,	11,	1),
346e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SPI3,		RCC_MP_APB1ENSETR,	12,	1),
347e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USART3,		RCC_MP_APB1ENSETR,	15,	1),
348e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_UART4,		RCC_MP_APB1ENSETR,	16,	1),
349e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_UART5,		RCC_MP_APB1ENSETR,	17,	1),
350e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_UART7,		RCC_MP_APB1ENSETR,	18,	1),
351e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_UART8,		RCC_MP_APB1ENSETR,	19,	1),
352e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_I2C1,		RCC_MP_APB1ENSETR,	21,	1),
353e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_I2C2,		RCC_MP_APB1ENSETR,	22,	1),
354e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SPDIF,		RCC_MP_APB1ENSETR,	26,	1),
355e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM1,		RCC_MP_APB2ENSETR,	0,	1),
356e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM8,		RCC_MP_APB2ENSETR,	1,	1),
357e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SPI1,		RCC_MP_APB2ENSETR,	8,	1),
358e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USART6,		RCC_MP_APB2ENSETR,	13,	1),
359e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SAI1,		RCC_MP_APB2ENSETR,	16,	1),
360e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SAI2,		RCC_MP_APB2ENSETR,	17,	1),
361e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DFSDM,		RCC_MP_APB2ENSETR,	20,	1),
362e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ADFSDM,		RCC_MP_APB2ENSETR,	21,	1),
363e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_FDCAN,		RCC_MP_APB2ENSETR,	24,	1),
364e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LPTIM2,		RCC_MP_APB3ENSETR,	0,	1),
365e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LPTIM3,		RCC_MP_APB3ENSETR,	1,	1),
366e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LPTIM4,		RCC_MP_APB3ENSETR,	2,	1),
367e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LPTIM5,		RCC_MP_APB3ENSETR,	3,	1),
368e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_VREF,		RCC_MP_APB3ENSETR,	13,	1),
369e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DTS,		RCC_MP_APB3ENSETR,	16,	1),
370e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PMBCTRL,		RCC_MP_APB3ENSETR,	17,	1),
371e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HDP,		RCC_MP_APB3ENSETR,	20,	1),
372e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SYSCFG,		RCC_MP_S_APB3ENSETR,	0,	1),
373e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DCMIPP,		RCC_MP_APB4ENSETR,	1,	1),
374e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DDRPERFM,		RCC_MP_APB4ENSETR,	8,	1),
375e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_IWDG2APB,		RCC_MP_APB4ENSETR,	15,	1),
376e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USBPHY,		RCC_MP_APB4ENSETR,	16,	1),
377e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_STGENRO,		RCC_MP_APB4ENSETR,	20,	1),
378e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_LTDC,		RCC_MP_S_APB4ENSETR,	0,	1),
379e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_RTCAPB,		RCC_MP_APB5ENSETR,	8,	1),
380e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TZC,		RCC_MP_APB5ENSETR,	11,	1),
381e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETZPC,		RCC_MP_APB5ENSETR,	13,	1),
382e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_IWDG1APB,		RCC_MP_APB5ENSETR,	15,	1),
383e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_BSEC,		RCC_MP_APB5ENSETR,	16,	1),
384e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_STGENC,		RCC_MP_APB5ENSETR,	20,	1),
385e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USART1,		RCC_MP_APB6ENSETR,	0,	1),
386e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USART2,		RCC_MP_APB6ENSETR,	1,	1),
387e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SPI4,		RCC_MP_APB6ENSETR,	2,	1),
388e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SPI5,		RCC_MP_APB6ENSETR,	3,	1),
389e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_I2C3,		RCC_MP_APB6ENSETR,	4,	1),
390e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_I2C4,		RCC_MP_APB6ENSETR,	5,	1),
391e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_I2C5,		RCC_MP_APB6ENSETR,	6,	1),
392e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM12,		RCC_MP_APB6ENSETR,	7,	1),
393e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM13,		RCC_MP_APB6ENSETR,	8,	1),
394e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM14,		RCC_MP_APB6ENSETR,	9,	1),
395e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM15,		RCC_MP_APB6ENSETR,	10,	1),
396e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM16,		RCC_MP_APB6ENSETR,	11,	1),
397e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TIM17,		RCC_MP_APB6ENSETR,	12,	1),
398e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DMA1,		RCC_MP_AHB2ENSETR,	0,	1),
399e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DMA2,		RCC_MP_AHB2ENSETR,	1,	1),
400e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DMAMUX1,		RCC_MP_AHB2ENSETR,	2,	1),
401e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DMA3,		RCC_MP_AHB2ENSETR,	3,	1),
402e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_DMAMUX2,		RCC_MP_AHB2ENSETR,	4,	1),
403e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ADC1,		RCC_MP_AHB2ENSETR,	5,	1),
404e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ADC2,		RCC_MP_AHB2ENSETR,	6,	1),
405e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USBO,		RCC_MP_AHB2ENSETR,	8,	1),
406e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_TSC,		RCC_MP_AHB4ENSETR,	15,	1),
407e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOA,		RCC_MP_S_AHB4ENSETR,	0,	1),
408e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOB,		RCC_MP_S_AHB4ENSETR,	1,	1),
409e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOC,		RCC_MP_S_AHB4ENSETR,	2,	1),
410e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOD,		RCC_MP_S_AHB4ENSETR,	3,	1),
411e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOE,		RCC_MP_S_AHB4ENSETR,	4,	1),
412e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOF,		RCC_MP_S_AHB4ENSETR,	5,	1),
413e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOG,		RCC_MP_S_AHB4ENSETR,	6,	1),
414e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOH,		RCC_MP_S_AHB4ENSETR,	7,	1),
415e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_GPIOI,		RCC_MP_S_AHB4ENSETR,	8,	1),
416e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_PKA,		RCC_MP_AHB5ENSETR,	2,	1),
417e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SAES,		RCC_MP_AHB5ENSETR,	3,	1),
418e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_CRYP1,		RCC_MP_AHB5ENSETR,	4,	1),
419e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_HASH1,		RCC_MP_AHB5ENSETR,	5,	1),
420e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_RNG1,		RCC_MP_AHB5ENSETR,	6,	1),
421e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_BKPSRAM,		RCC_MP_AHB5ENSETR,	8,	1),
422e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_AXIMC,		RCC_MP_AHB5ENSETR,	16,	1),
423e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MCE,		RCC_MP_AHB6ENSETR,	1,	1),
424e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH1CK,		RCC_MP_AHB6ENSETR,	7,	1),
425e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH1TX,		RCC_MP_AHB6ENSETR,	8,	1),
426e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH1RX,		RCC_MP_AHB6ENSETR,	9,	1),
427e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH1MAC,		RCC_MP_AHB6ENSETR,	10,	1),
428e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_FMC,		RCC_MP_AHB6ENSETR,	12,	1),
429e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_QSPI,		RCC_MP_AHB6ENSETR,	14,	1),
430e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SDMMC1,		RCC_MP_AHB6ENSETR,	16,	1),
431e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_SDMMC2,		RCC_MP_AHB6ENSETR,	17,	1),
432e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_CRC1,		RCC_MP_AHB6ENSETR,	20,	1),
433e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_USBH,		RCC_MP_AHB6ENSETR,	24,	1),
434e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH2CK,		RCC_MP_AHB6ENSETR,	27,	1),
435e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH2TX,		RCC_MP_AHB6ENSETR,	28,	1),
436e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH2RX,		RCC_MP_AHB6ENSETR,	29,	1),
437e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_ETH2MAC,		RCC_MP_AHB6ENSETR,	30,	1),
438e5e793a6SGabriel Fernandez 	GATE_CFG(GATE_MDMA,		RCC_MP_S_AHB6ENSETR,	0,	1),
439e5e793a6SGabriel Fernandez };
440e5e793a6SGabriel Fernandez 
441e5e793a6SGabriel Fernandez /*
442e5e793a6SGabriel Fernandez  * MUX CONFIG
443e5e793a6SGabriel Fernandez  */
444e5e793a6SGabriel Fernandez #define MUXRDY_CFG(_id, _offset, _shift, _witdh, _rdy)\
445e5e793a6SGabriel Fernandez 	[(_id)] = {\
446e5e793a6SGabriel Fernandez 			.offset	= (_offset),\
447e5e793a6SGabriel Fernandez 			.shift	= (_shift),\
448e5e793a6SGabriel Fernandez 			.width	= (_witdh),\
449e5e793a6SGabriel Fernandez 			.ready	= (_rdy),\
450e5e793a6SGabriel Fernandez 	}
451e5e793a6SGabriel Fernandez 
452e5e793a6SGabriel Fernandez #define MUX_CFG(_id, _offset, _shift, _witdh)\
453e5e793a6SGabriel Fernandez 	MUXRDY_CFG(_id, _offset, _shift, _witdh, MUX_NO_RDY)
454e5e793a6SGabriel Fernandez 
455e5e793a6SGabriel Fernandez static const struct mux_cfg parent_mp13[MUX_NB] = {
456e5e793a6SGabriel Fernandez 	MUXRDY_CFG(MUX_MPU,	RCC_MPCKSELR,		0, 2, GATE_MPUSRCRDY),
457e5e793a6SGabriel Fernandez 	MUXRDY_CFG(MUX_AXI,	RCC_ASSCKSELR,		0, 3, GATE_AXISSRCRDY),
458e5e793a6SGabriel Fernandez 	MUXRDY_CFG(MUX_MLAHB,	RCC_MSSCKSELR,		0, 2, GATE_MCUSSRCRDY),
459e5e793a6SGabriel Fernandez 	MUXRDY_CFG(MUX_PLL12,	RCC_RCK12SELR,		0, 2, GATE_PLL12SRCRDY),
460e5e793a6SGabriel Fernandez 	MUXRDY_CFG(MUX_PLL3,	RCC_RCK3SELR,		0, 2, GATE_PLL3SRCRDY),
461e5e793a6SGabriel Fernandez 	MUXRDY_CFG(MUX_PLL4,	RCC_RCK4SELR,		0, 2, GATE_PLL4SRCRDY),
462e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_ADC1,	RCC_ADC12CKSELR,	0, 2),
463e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_ADC2,	RCC_ADC12CKSELR,	2, 2),
464e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_CKPER,	RCC_CPERCKSELR,		0, 2),
465e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_DCMIPP,	RCC_DCMIPPCKSELR,	0, 2),
466e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_ETH1,	RCC_ETH12CKSELR,	0, 2),
467e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_ETH2,	RCC_ETH12CKSELR,	8, 2),
468e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_FDCAN,	RCC_FDCANCKSELR,	0, 2),
469e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_FMC,	RCC_FMCCKSELR,		0, 2),
470e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_I2C12,	RCC_I2C12CKSELR,	0, 3),
471e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_I2C3,	RCC_I2C345CKSELR,	0, 3),
472e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_I2C4,	RCC_I2C345CKSELR,	3, 3),
473e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_I2C5,	RCC_I2C345CKSELR,	6, 3),
474e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_LPTIM1,	RCC_LPTIM1CKSELR,	0, 3),
475e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_LPTIM2,	RCC_LPTIM23CKSELR,	0, 3),
476e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_LPTIM3,	RCC_LPTIM23CKSELR,	3, 3),
477e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_LPTIM45,	RCC_LPTIM45CKSELR,	0, 3),
478e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_MCO1,	RCC_MCO1CFGR,		0, 3),
479e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_MCO2,	RCC_MCO2CFGR,		0, 3),
480e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_QSPI,	RCC_QSPICKSELR,		0, 2),
481e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_RNG1,	RCC_RNG1CKSELR,		0, 2),
482e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_RTC,	RCC_BDCR,		16, 2),
483e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SAES,	RCC_SAESCKSELR,		0, 2),
484e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SAI1,	RCC_SAI1CKSELR,		0, 3),
485e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SAI2,	RCC_SAI2CKSELR,		0, 3),
486e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SDMMC1,	RCC_SDMMC12CKSELR,	0, 3),
487e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SDMMC2,	RCC_SDMMC12CKSELR,	3, 3),
488e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SPDIF,	RCC_SPDIFCKSELR,	0, 2),
489e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SPI1,	RCC_SPI2S1CKSELR,	0, 3),
490e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SPI23,	RCC_SPI2S23CKSELR,	0, 3),
491e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SPI4,	RCC_SPI45CKSELR,	0, 3),
492e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_SPI5,	RCC_SPI45CKSELR,	3, 3),
493e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_STGEN,	RCC_STGENCKSELR,	0, 2),
494e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_UART1,	RCC_UART12CKSELR,	0, 3),
495e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_UART2,	RCC_UART12CKSELR,	3, 3),
496e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_UART35,	RCC_UART35CKSELR,	0, 3),
497e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_UART4,	RCC_UART4CKSELR,	0, 3),
498e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_UART6,	RCC_UART6CKSELR,	0, 3),
499e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_UART78,	RCC_UART78CKSELR,	0, 3),
500e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_USBO,	RCC_USBCKSELR,		4, 1),
501e5e793a6SGabriel Fernandez 	MUX_CFG(MUX_USBPHY,	RCC_USBCKSELR,		0, 2),
502e5e793a6SGabriel Fernandez };
503e5e793a6SGabriel Fernandez 
504e5e793a6SGabriel Fernandez /*
505e5e793a6SGabriel Fernandez  * DIV CONFIG
506e5e793a6SGabriel Fernandez  */
507e5e793a6SGabriel Fernandez static const struct div_table_cfg axi_div_table[] = {
508e5e793a6SGabriel Fernandez 	{ 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
509e5e793a6SGabriel Fernandez 	{ 4, 4 }, { 5, 4 }, { 6, 4 }, { 7, 4 },
510e5e793a6SGabriel Fernandez 	{ 0 },
511e5e793a6SGabriel Fernandez };
512e5e793a6SGabriel Fernandez 
513e5e793a6SGabriel Fernandez static const struct div_table_cfg mlahb_div_table[] = {
514e5e793a6SGabriel Fernandez 	{ 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
515e5e793a6SGabriel Fernandez 	{ 4, 16 }, { 5, 32 }, { 6, 64 }, { 7, 128 },
516e5e793a6SGabriel Fernandez 	{ 8, 256 }, { 9, 512 }, { 10, 512}, { 11, 512 },
517e5e793a6SGabriel Fernandez 	{ 12, 512 }, { 13, 512 }, { 14, 512}, { 15, 512 },
518e5e793a6SGabriel Fernandez 	{ 0 },
519e5e793a6SGabriel Fernandez };
520e5e793a6SGabriel Fernandez 
521e5e793a6SGabriel Fernandez static const struct div_table_cfg apb_div_table[] = {
522e5e793a6SGabriel Fernandez 	{ 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
523e5e793a6SGabriel Fernandez 	{ 4, 16 }, { 5, 16 }, { 6, 16 }, { 7, 16 },
524e5e793a6SGabriel Fernandez 	{ 0 },
525e5e793a6SGabriel Fernandez };
526e5e793a6SGabriel Fernandez 
527e5e793a6SGabriel Fernandez #define DIVRDY_CFG(_id, _offset, _shift, _width, _flags, _table, _ready)\
528e5e793a6SGabriel Fernandez 	[(_id)] = {\
529e5e793a6SGabriel Fernandez 		.offset	= (_offset),\
530e5e793a6SGabriel Fernandez 		.shift	= (_shift),\
531e5e793a6SGabriel Fernandez 		.width	= (_width),\
532e5e793a6SGabriel Fernandez 		.flags	= (_flags),\
533e5e793a6SGabriel Fernandez 		.table	= (_table),\
534e5e793a6SGabriel Fernandez 		.ready	= (_ready),\
535e5e793a6SGabriel Fernandez 	}
536e5e793a6SGabriel Fernandez 
537e5e793a6SGabriel Fernandez #define DIV_CFG(_id, _offset, _shift, _width, _flags, _table)\
538e5e793a6SGabriel Fernandez 	DIVRDY_CFG(_id, _offset, _shift, _width, _flags, _table, DIV_NO_RDY)
539e5e793a6SGabriel Fernandez 
540e5e793a6SGabriel Fernandez static const struct div_cfg dividers_mp13[] = {
541e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_MPU, RCC_MPCKDIVR, 0, 4, 0, NULL,
542e5e793a6SGabriel Fernandez 		   GATE_MPUDIVRDY),
543e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_AXI, RCC_AXIDIVR, 0, 3, 0, axi_div_table,
544e5e793a6SGabriel Fernandez 		   GATE_AXIDIVRDY),
545e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_MLAHB, RCC_MLAHBDIVR, 0, 4, 0, mlahb_div_table,
546e5e793a6SGabriel Fernandez 		   GATE_MLAHBDIVRDY),
547e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_APB1, RCC_APB1DIVR, 0, 3, 0, apb_div_table,
548e5e793a6SGabriel Fernandez 		   GATE_APB1DIVRDY),
549e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_APB2, RCC_APB2DIVR, 0, 3, 0, apb_div_table,
550e5e793a6SGabriel Fernandez 		   GATE_APB2DIVRDY),
551e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_APB3, RCC_APB3DIVR, 0, 3, 0, apb_div_table,
552e5e793a6SGabriel Fernandez 		   GATE_APB3DIVRDY),
553e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_APB4, RCC_APB4DIVR, 0, 3, 0, apb_div_table,
554e5e793a6SGabriel Fernandez 		   GATE_APB4DIVRDY),
555e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_APB5, RCC_APB5DIVR, 0, 3, 0, apb_div_table,
556e5e793a6SGabriel Fernandez 		   GATE_APB5DIVRDY),
557e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_APB6, RCC_APB6DIVR, 0, 3, 0, apb_div_table,
558e5e793a6SGabriel Fernandez 		   GATE_APB6DIVRDY),
559e5e793a6SGabriel Fernandez 	DIVRDY_CFG(DIV_HSI, RCC_HSICFGR, 0, 2, CLK_DIVIDER_POWER_OF_TWO, NULL,
560e5e793a6SGabriel Fernandez 		   GATE_HSIDIVRDY),
561e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL1DIVP, RCC_PLL1CFGR2, 0, 7, 0, NULL),
562e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL2DIVP, RCC_PLL2CFGR2, 0, 7, 0, NULL),
563e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL2DIVQ, RCC_PLL2CFGR2, 8, 7, 0, NULL),
564e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL2DIVR, RCC_PLL2CFGR2, 16, 7, 0, NULL),
565e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL3DIVP, RCC_PLL3CFGR2, 0, 7, 0, NULL),
566e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL3DIVQ, RCC_PLL3CFGR2, 8, 7, 0, NULL),
567e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL3DIVR, RCC_PLL3CFGR2, 16, 7, 0, NULL),
568e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL4DIVP, RCC_PLL4CFGR2, 0, 7, 0, NULL),
569e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL4DIVQ, RCC_PLL4CFGR2, 8, 7, 0, NULL),
570e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_PLL4DIVR, RCC_PLL4CFGR2, 16, 7, 0, NULL),
571e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_RTC, RCC_RTCDIVR, 0, 6, 0, NULL),
572e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_MCO1, RCC_MCO1CFGR, 4, 4, 0, NULL),
573e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_MCO2, RCC_MCO2CFGR, 4, 4, 0, NULL),
574e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_TRACE, RCC_DBGCFGR, 0, 3, CLK_DIVIDER_POWER_OF_TWO, NULL),
575e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_ETH1PTP, RCC_ETH12CKSELR, 4, 4, 0, NULL),
576e5e793a6SGabriel Fernandez 	DIV_CFG(DIV_ETH2PTP, RCC_ETH12CKSELR, 12, 4, 0, NULL),
577e5e793a6SGabriel Fernandez };
578e5e793a6SGabriel Fernandez 
579e5e793a6SGabriel Fernandez enum stm32_osc {
580e5e793a6SGabriel Fernandez 	OSC_HSI,
581e5e793a6SGabriel Fernandez 	OSC_HSE,
582e5e793a6SGabriel Fernandez 	OSC_CSI,
583e5e793a6SGabriel Fernandez 	OSC_LSI,
584e5e793a6SGabriel Fernandez 	OSC_LSE,
585e5e793a6SGabriel Fernandez 	NB_OSCILLATOR
586e5e793a6SGabriel Fernandez };
587e5e793a6SGabriel Fernandez 
588e5e793a6SGabriel Fernandez struct stm32_osc_cfg {
589e5e793a6SGabriel Fernandez 	int osc_id;
590e5e793a6SGabriel Fernandez };
591e5e793a6SGabriel Fernandez 
592e5e793a6SGabriel Fernandez struct clk_stm32_bypass {
593e5e793a6SGabriel Fernandez 	uint16_t offset;
594e5e793a6SGabriel Fernandez 	uint8_t bit_byp;
595e5e793a6SGabriel Fernandez 	uint8_t bit_digbyp;
596e5e793a6SGabriel Fernandez };
597e5e793a6SGabriel Fernandez 
598e5e793a6SGabriel Fernandez struct clk_stm32_css {
599e5e793a6SGabriel Fernandez 	uint16_t offset;
600e5e793a6SGabriel Fernandez 	uint8_t bit_css;
601e5e793a6SGabriel Fernandez };
602e5e793a6SGabriel Fernandez 
603e5e793a6SGabriel Fernandez struct clk_stm32_drive {
604e5e793a6SGabriel Fernandez 	uint16_t offset;
605e5e793a6SGabriel Fernandez 	uint8_t drv_shift;
606e5e793a6SGabriel Fernandez 	uint8_t drv_width;
607e5e793a6SGabriel Fernandez 	uint8_t drv_default;
608e5e793a6SGabriel Fernandez };
609e5e793a6SGabriel Fernandez 
610e5e793a6SGabriel Fernandez struct clk_oscillator_data {
611e5e793a6SGabriel Fernandez 	const char *name;
612e5e793a6SGabriel Fernandez 	unsigned long frequency;
613e5e793a6SGabriel Fernandez 	uint16_t gate_id;
614e5e793a6SGabriel Fernandez 	struct clk_stm32_bypass *bypass;
615e5e793a6SGabriel Fernandez 	struct clk_stm32_css *css;
616e5e793a6SGabriel Fernandez 	struct clk_stm32_drive *drive;
617e5e793a6SGabriel Fernandez };
618e5e793a6SGabriel Fernandez 
619e5e793a6SGabriel Fernandez #define BYPASS(_offset, _bit_byp, _bit_digbyp) (&(struct clk_stm32_bypass){\
620e5e793a6SGabriel Fernandez 	.offset		= (_offset),\
621e5e793a6SGabriel Fernandez 	.bit_byp	= (_bit_byp),\
622e5e793a6SGabriel Fernandez 	.bit_digbyp	= (_bit_digbyp),\
623e5e793a6SGabriel Fernandez })
624e5e793a6SGabriel Fernandez 
625e5e793a6SGabriel Fernandez #define CSS(_offset, _bit_css) (&(struct clk_stm32_css){\
626e5e793a6SGabriel Fernandez 	.offset		= (_offset),\
627e5e793a6SGabriel Fernandez 	.bit_css	= (_bit_css),\
628e5e793a6SGabriel Fernandez })
629e5e793a6SGabriel Fernandez 
630e5e793a6SGabriel Fernandez #define DRIVE(_offset, _shift, _width, _default) (&(struct clk_stm32_drive){\
631e5e793a6SGabriel Fernandez 	.offset		= (_offset),\
632e5e793a6SGabriel Fernandez 	.drv_shift	= (_shift),\
633e5e793a6SGabriel Fernandez 	.drv_width	= (_width),\
634e5e793a6SGabriel Fernandez 	.drv_default	= (_default),\
635e5e793a6SGabriel Fernandez })
636e5e793a6SGabriel Fernandez 
637e5e793a6SGabriel Fernandez #define OSCILLATOR(idx_osc, _name, _gate_id, _bypass, _css, _drive) \
638e5e793a6SGabriel Fernandez 	[(idx_osc)] = (struct clk_oscillator_data){\
639e5e793a6SGabriel Fernandez 		.name		= (_name),\
640e5e793a6SGabriel Fernandez 		.gate_id	= (_gate_id),\
641e5e793a6SGabriel Fernandez 		.bypass		= (_bypass),\
642e5e793a6SGabriel Fernandez 		.css		= (_css),\
643e5e793a6SGabriel Fernandez 		.drive		= (_drive),\
644e5e793a6SGabriel Fernandez 	}
645e5e793a6SGabriel Fernandez 
646e5e793a6SGabriel Fernandez static struct clk_oscillator_data stm32mp13_osc_data[NB_OSCILLATOR] = {
647e5e793a6SGabriel Fernandez 	OSCILLATOR(OSC_HSI, "clk-hsi", GATE_HSI,
648e5e793a6SGabriel Fernandez 		   NULL, NULL, NULL),
649e5e793a6SGabriel Fernandez 
650e5e793a6SGabriel Fernandez 	OSCILLATOR(OSC_LSI, "clk-lsi", GATE_LSI,
651e5e793a6SGabriel Fernandez 		   NULL, NULL, NULL),
652e5e793a6SGabriel Fernandez 
653e5e793a6SGabriel Fernandez 	OSCILLATOR(OSC_CSI, "clk-csi", GATE_CSI,
654e5e793a6SGabriel Fernandez 		   NULL, NULL, NULL),
655e5e793a6SGabriel Fernandez 
656e5e793a6SGabriel Fernandez 	OSCILLATOR(OSC_LSE, "clk-lse", GATE_LSE,
657e5e793a6SGabriel Fernandez 		   BYPASS(RCC_BDCR, 1, 3),
658e5e793a6SGabriel Fernandez 		   CSS(RCC_BDCR, 8),
659e5e793a6SGabriel Fernandez 		   DRIVE(RCC_BDCR, 4, 2, 2)),
660e5e793a6SGabriel Fernandez 
661e5e793a6SGabriel Fernandez 	OSCILLATOR(OSC_HSE, "clk-hse", GATE_HSE,
662e5e793a6SGabriel Fernandez 		   BYPASS(RCC_OCENSETR, 10, 7),
663e5e793a6SGabriel Fernandez 		   CSS(RCC_OCENSETR, 11),
664e5e793a6SGabriel Fernandez 		   NULL),
665e5e793a6SGabriel Fernandez };
666e5e793a6SGabriel Fernandez 
clk_oscillator_get_data(int osc_id)667e5e793a6SGabriel Fernandez static struct clk_oscillator_data *clk_oscillator_get_data(int osc_id)
668e5e793a6SGabriel Fernandez {
669e5e793a6SGabriel Fernandez 	assert(osc_id >= 0 && osc_id < (int)ARRAY_SIZE(stm32mp13_osc_data));
670e5e793a6SGabriel Fernandez 
671e5e793a6SGabriel Fernandez 	return &stm32mp13_osc_data[osc_id];
672e5e793a6SGabriel Fernandez }
673e5e793a6SGabriel Fernandez 
clk_stm32_get_rate_oscillateur(int osc_id)674e5e793a6SGabriel Fernandez static unsigned long clk_stm32_get_rate_oscillateur(int osc_id)
675e5e793a6SGabriel Fernandez {
676e5e793a6SGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
677e5e793a6SGabriel Fernandez 	struct stm32_clk_platdata *pdata = priv->pdata;
678e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[osc_id];
679e5e793a6SGabriel Fernandez 
680e5e793a6SGabriel Fernandez 	return osci->freq;
681e5e793a6SGabriel Fernandez }
682e5e793a6SGabriel Fernandez 
clk_oscillator_set_bypass(struct clk_stm32_priv * priv,struct clk_oscillator_data * osc_data,bool digbyp,bool bypass)683e5e793a6SGabriel Fernandez static void clk_oscillator_set_bypass(struct clk_stm32_priv *priv,
684e5e793a6SGabriel Fernandez 				      struct clk_oscillator_data *osc_data,
685e5e793a6SGabriel Fernandez 				      bool digbyp, bool bypass)
686e5e793a6SGabriel Fernandez {
687e5e793a6SGabriel Fernandez 	struct clk_stm32_bypass *bypass_data = osc_data->bypass;
688e5e793a6SGabriel Fernandez 	uintptr_t address = 0;
689e5e793a6SGabriel Fernandez 
690e5e793a6SGabriel Fernandez 	if (!bypass_data)
691e5e793a6SGabriel Fernandez 		return;
692e5e793a6SGabriel Fernandez 
693e5e793a6SGabriel Fernandez 	address = priv->base + bypass_data->offset;
694e5e793a6SGabriel Fernandez 
695e5e793a6SGabriel Fernandez 	if (digbyp)
696e5e793a6SGabriel Fernandez 		io_setbits32(address, BIT(bypass_data->bit_digbyp));
697e5e793a6SGabriel Fernandez 
698e5e793a6SGabriel Fernandez 	if (bypass || digbyp)
699e5e793a6SGabriel Fernandez 		io_setbits32(address, BIT(bypass_data->bit_byp));
700e5e793a6SGabriel Fernandez }
701e5e793a6SGabriel Fernandez 
clk_oscillator_set_css(struct clk_stm32_priv * priv,struct clk_oscillator_data * osc_data,bool css)702e5e793a6SGabriel Fernandez static void clk_oscillator_set_css(struct clk_stm32_priv *priv,
703e5e793a6SGabriel Fernandez 				   struct clk_oscillator_data *osc_data,
704e5e793a6SGabriel Fernandez 				   bool css)
705e5e793a6SGabriel Fernandez {
706e5e793a6SGabriel Fernandez 	struct clk_stm32_css *css_data = osc_data->css;
707e5e793a6SGabriel Fernandez 	uintptr_t address = 0;
708e5e793a6SGabriel Fernandez 
709e5e793a6SGabriel Fernandez 	if (!css_data)
710e5e793a6SGabriel Fernandez 		return;
711e5e793a6SGabriel Fernandez 
712e5e793a6SGabriel Fernandez 	address = priv->base + css_data->offset;
713e5e793a6SGabriel Fernandez 
714e5e793a6SGabriel Fernandez 	if (css)
715e5e793a6SGabriel Fernandez 		io_setbits32(address, BIT(css_data->bit_css));
716e5e793a6SGabriel Fernandez }
717e5e793a6SGabriel Fernandez 
clk_oscillator_set_drive(struct clk_stm32_priv * priv,struct clk_oscillator_data * osc_data,uint8_t lsedrv)718e5e793a6SGabriel Fernandez static void clk_oscillator_set_drive(struct clk_stm32_priv *priv,
719e5e793a6SGabriel Fernandez 				     struct clk_oscillator_data *osc_data,
720e5e793a6SGabriel Fernandez 				     uint8_t lsedrv)
721e5e793a6SGabriel Fernandez {
722e5e793a6SGabriel Fernandez 	struct clk_stm32_drive *drive_data = osc_data->drive;
723e5e793a6SGabriel Fernandez 	uintptr_t address = 0;
724e5e793a6SGabriel Fernandez 	uint32_t mask = 0;
725e5e793a6SGabriel Fernandez 	uint32_t value = 0;
726e5e793a6SGabriel Fernandez 
727e5e793a6SGabriel Fernandez 	if (!drive_data)
728e5e793a6SGabriel Fernandez 		return;
729e5e793a6SGabriel Fernandez 
730e5e793a6SGabriel Fernandez 	address = priv->base + drive_data->offset;
731e5e793a6SGabriel Fernandez 
732e5e793a6SGabriel Fernandez 	mask = (BIT(drive_data->drv_width) - 1U) << drive_data->drv_shift;
733e5e793a6SGabriel Fernandez 
734e5e793a6SGabriel Fernandez 	/*
735e5e793a6SGabriel Fernandez 	 * Warning: not recommended to switch directly from "high drive"
736e5e793a6SGabriel Fernandez 	 * to "medium low drive", and vice-versa.
737e5e793a6SGabriel Fernandez 	 */
738e5e793a6SGabriel Fernandez 	value = (io_read32(address) & mask) >> drive_data->drv_shift;
739e5e793a6SGabriel Fernandez 
740e5e793a6SGabriel Fernandez 	while (value != lsedrv) {
741e5e793a6SGabriel Fernandez 		if (value > lsedrv)
742e5e793a6SGabriel Fernandez 			value--;
743e5e793a6SGabriel Fernandez 		else
744e5e793a6SGabriel Fernandez 			value++;
745e5e793a6SGabriel Fernandez 
746e5e793a6SGabriel Fernandez 		io_clrsetbits32(address, mask, value << drive_data->drv_shift);
747e5e793a6SGabriel Fernandez 	}
748e5e793a6SGabriel Fernandez }
749e5e793a6SGabriel Fernandez 
stm32_enable_oscillator_hse(struct clk_stm32_priv * priv,struct stm32_clk_platdata * pdata)750e5e793a6SGabriel Fernandez static void stm32_enable_oscillator_hse(struct clk_stm32_priv *priv,
751e5e793a6SGabriel Fernandez 					struct stm32_clk_platdata *pdata)
752e5e793a6SGabriel Fernandez {
753e5e793a6SGabriel Fernandez 	struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_HSE);
754e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_HSE];
755e5e793a6SGabriel Fernandez 
756e5e793a6SGabriel Fernandez 	if (osci->freq == 0U)
757e5e793a6SGabriel Fernandez 		return;
758e5e793a6SGabriel Fernandez 
759e5e793a6SGabriel Fernandez 	clk_oscillator_set_bypass(priv, osc_data,  osci->digbyp, osci->bypass);
760e5e793a6SGabriel Fernandez 
761e5e793a6SGabriel Fernandez 	/* Enable clock and wait ready bit */
762e5e793a6SGabriel Fernandez 	if (stm32_gate_rdy_enable(osc_data->gate_id)) {
763e5e793a6SGabriel Fernandez 		EMSG("timeout to enable hse clock");
764e5e793a6SGabriel Fernandez 		panic();
765e5e793a6SGabriel Fernandez 	}
766e5e793a6SGabriel Fernandez 
767e5e793a6SGabriel Fernandez 	clk_oscillator_set_css(priv, osc_data, osci->css);
768e5e793a6SGabriel Fernandez }
769e5e793a6SGabriel Fernandez 
stm32_enable_oscillator_lse(struct clk_stm32_priv * priv,struct stm32_clk_platdata * pdata)770e5e793a6SGabriel Fernandez static void stm32_enable_oscillator_lse(struct clk_stm32_priv *priv,
771e5e793a6SGabriel Fernandez 					struct stm32_clk_platdata *pdata)
772e5e793a6SGabriel Fernandez {
773e5e793a6SGabriel Fernandez 	struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSE);
774e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE];
775e5e793a6SGabriel Fernandez 
776e5e793a6SGabriel Fernandez 	if (osci->freq == 0U)
777e5e793a6SGabriel Fernandez 		return;
778e5e793a6SGabriel Fernandez 
779e5e793a6SGabriel Fernandez 	clk_oscillator_set_bypass(priv, osc_data, osci->digbyp, osci->bypass);
780e5e793a6SGabriel Fernandez 
781e5e793a6SGabriel Fernandez 	clk_oscillator_set_drive(priv, osc_data,  osci->drive);
782e5e793a6SGabriel Fernandez 
783e5e793a6SGabriel Fernandez 	/* Enable lse clock, but don't wait ready bit */
784e5e793a6SGabriel Fernandez 	stm32_gate_enable(osc_data->gate_id);
785e5e793a6SGabriel Fernandez }
786e5e793a6SGabriel Fernandez 
787e5e793a6SGabriel Fernandez static void
stm32_enable_oscillator_lsi(struct clk_stm32_priv * priv __maybe_unused,struct stm32_clk_platdata * pdata)788e5e793a6SGabriel Fernandez stm32_enable_oscillator_lsi(struct clk_stm32_priv *priv __maybe_unused,
789e5e793a6SGabriel Fernandez 			    struct stm32_clk_platdata *pdata)
790e5e793a6SGabriel Fernandez {
791e5e793a6SGabriel Fernandez 	struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSI);
792e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSI];
793e5e793a6SGabriel Fernandez 
794e5e793a6SGabriel Fernandez 	if (osci->freq == 0U)
795e5e793a6SGabriel Fernandez 		return;
796e5e793a6SGabriel Fernandez 
797e5e793a6SGabriel Fernandez 	/* Enable clock and wait ready bit */
798e5e793a6SGabriel Fernandez 	if (stm32_gate_rdy_enable(osc_data->gate_id)) {
799e5e793a6SGabriel Fernandez 		EMSG("timeout to enable lsi clock");
800e5e793a6SGabriel Fernandez 		panic();
801e5e793a6SGabriel Fernandez 	}
802e5e793a6SGabriel Fernandez }
803e5e793a6SGabriel Fernandez 
804e5e793a6SGabriel Fernandez static void
stm32_enable_oscillator_csi(struct clk_stm32_priv * priv __maybe_unused,struct stm32_clk_platdata * pdata)805e5e793a6SGabriel Fernandez stm32_enable_oscillator_csi(struct clk_stm32_priv *priv __maybe_unused,
806e5e793a6SGabriel Fernandez 			    struct stm32_clk_platdata *pdata)
807e5e793a6SGabriel Fernandez {
808e5e793a6SGabriel Fernandez 	struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_CSI);
809e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_CSI];
810e5e793a6SGabriel Fernandez 
811e5e793a6SGabriel Fernandez 	if (osci->freq == 0U)
812e5e793a6SGabriel Fernandez 		return;
813e5e793a6SGabriel Fernandez 
814e5e793a6SGabriel Fernandez 	/* Enable clock and wait ready bit */
815e5e793a6SGabriel Fernandez 	if (stm32_gate_rdy_enable(osc_data->gate_id)) {
816e5e793a6SGabriel Fernandez 		EMSG("timeout to enable csi clock");
817e5e793a6SGabriel Fernandez 		panic();
818e5e793a6SGabriel Fernandez 	}
819e5e793a6SGabriel Fernandez }
820e5e793a6SGabriel Fernandez 
stm32_clk_oscillators_lse_set_css(struct clk_stm32_priv * priv,struct stm32_clk_platdata * pdata)821e5e793a6SGabriel Fernandez static int stm32_clk_oscillators_lse_set_css(struct clk_stm32_priv *priv,
822e5e793a6SGabriel Fernandez 					     struct stm32_clk_platdata *pdata)
823e5e793a6SGabriel Fernandez 
824e5e793a6SGabriel Fernandez {
825e5e793a6SGabriel Fernandez 	struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSE);
826e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE];
827e5e793a6SGabriel Fernandez 
828e5e793a6SGabriel Fernandez 	clk_oscillator_set_css(priv, osc_data, osci->css);
829e5e793a6SGabriel Fernandez 
830e5e793a6SGabriel Fernandez 	return 0;
831e5e793a6SGabriel Fernandez }
832e5e793a6SGabriel Fernandez 
833e5e793a6SGabriel Fernandez static int
stm32_clk_oscillators_wait_lse_ready(struct clk_stm32_priv * priv __maybe_unused,struct stm32_clk_platdata * pdata)834e5e793a6SGabriel Fernandez stm32_clk_oscillators_wait_lse_ready(struct clk_stm32_priv *priv __maybe_unused,
835e5e793a6SGabriel Fernandez 				     struct stm32_clk_platdata *pdata)
836e5e793a6SGabriel Fernandez {
837e5e793a6SGabriel Fernandez 	struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSE);
838e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE];
839e5e793a6SGabriel Fernandez 
840e5e793a6SGabriel Fernandez 	if (osci->freq == 0U)
841e5e793a6SGabriel Fernandez 		return 0;
842e5e793a6SGabriel Fernandez 
843e5e793a6SGabriel Fernandez 	if (stm32_gate_wait_ready(osc_data->gate_id, true))
844e5e793a6SGabriel Fernandez 		return -1;
845e5e793a6SGabriel Fernandez 
846e5e793a6SGabriel Fernandez 	return 0;
847e5e793a6SGabriel Fernandez }
848e5e793a6SGabriel Fernandez 
stm32_clk_oscillators_enable(struct clk_stm32_priv * priv,struct stm32_clk_platdata * pdata)849e5e793a6SGabriel Fernandez static void stm32_clk_oscillators_enable(struct clk_stm32_priv *priv,
850e5e793a6SGabriel Fernandez 					 struct stm32_clk_platdata *pdata)
851e5e793a6SGabriel Fernandez {
852e5e793a6SGabriel Fernandez 	stm32_enable_oscillator_hse(priv, pdata);
853e5e793a6SGabriel Fernandez 	stm32_enable_oscillator_lse(priv, pdata);
854e5e793a6SGabriel Fernandez 	stm32_enable_oscillator_lsi(priv, pdata);
855e5e793a6SGabriel Fernandez 	stm32_enable_oscillator_csi(priv, pdata);
856e5e793a6SGabriel Fernandez }
857e5e793a6SGabriel Fernandez 
858e5e793a6SGabriel Fernandez enum stm32_pll_id {
859e5e793a6SGabriel Fernandez 	PLL1_ID,
860e5e793a6SGabriel Fernandez 	PLL2_ID,
861e5e793a6SGabriel Fernandez 	PLL3_ID,
862e5e793a6SGabriel Fernandez 	PLL4_ID,
863e5e793a6SGabriel Fernandez 	PLL_NB
864e5e793a6SGabriel Fernandez };
865e5e793a6SGabriel Fernandez 
866e5e793a6SGabriel Fernandez enum stm32mp1_plltype {
867e5e793a6SGabriel Fernandez 	PLL_800,
868e5e793a6SGabriel Fernandez 	PLL_1600,
869e5e793a6SGabriel Fernandez 	PLL_2000,
870e5e793a6SGabriel Fernandez 	PLL_TYPE_NB
871e5e793a6SGabriel Fernandez };
872e5e793a6SGabriel Fernandez 
873e5e793a6SGabriel Fernandez #define RCC_OFFSET_PLLXCR		0
874e5e793a6SGabriel Fernandez #define RCC_OFFSET_PLLXCFGR1		4
875e5e793a6SGabriel Fernandez #define RCC_OFFSET_PLLXCFGR2		8
876e5e793a6SGabriel Fernandez #define RCC_OFFSET_PLLXFRACR		12
877e5e793a6SGabriel Fernandez #define RCC_OFFSET_PLLXCSGR		16
878e5e793a6SGabriel Fernandez 
879e5e793a6SGabriel Fernandez struct stm32_clk_pll {
880e5e793a6SGabriel Fernandez 	enum stm32mp1_plltype plltype;
881e5e793a6SGabriel Fernandez 	uint16_t gate_id;
882e5e793a6SGabriel Fernandez 	uint16_t mux_id;
883e5e793a6SGabriel Fernandez 	uint16_t reg_pllxcr;
884e5e793a6SGabriel Fernandez };
885e5e793a6SGabriel Fernandez 
886e5e793a6SGabriel Fernandez struct stm32mp1_pll {
887e5e793a6SGabriel Fernandez 	uint8_t refclk_min;
888e5e793a6SGabriel Fernandez 	uint8_t refclk_max;
889e5e793a6SGabriel Fernandez };
890e5e793a6SGabriel Fernandez 
891e5e793a6SGabriel Fernandez /* Define characteristic of PLL according type */
892e5e793a6SGabriel Fernandez static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
893e5e793a6SGabriel Fernandez 	[PLL_800] = {
894e5e793a6SGabriel Fernandez 		.refclk_min = 4,
895e5e793a6SGabriel Fernandez 		.refclk_max = 16,
896e5e793a6SGabriel Fernandez 	},
897e5e793a6SGabriel Fernandez 	[PLL_1600] = {
898e5e793a6SGabriel Fernandez 		.refclk_min = 8,
899e5e793a6SGabriel Fernandez 		.refclk_max = 16,
900e5e793a6SGabriel Fernandez 	},
901e5e793a6SGabriel Fernandez 	[PLL_2000] = {
902e5e793a6SGabriel Fernandez 		.refclk_min = 8,
903e5e793a6SGabriel Fernandez 		.refclk_max = 16,
904e5e793a6SGabriel Fernandez 	}
905e5e793a6SGabriel Fernandez };
906e5e793a6SGabriel Fernandez 
907e5e793a6SGabriel Fernandez #define CLK_PLL_CFG(_idx, _type, _gate_id, _mux_id, _reg)\
908e5e793a6SGabriel Fernandez 	[(_idx)] = {\
909e5e793a6SGabriel Fernandez 		.gate_id = (_gate_id),\
910e5e793a6SGabriel Fernandez 		.mux_id = (_mux_id),\
911e5e793a6SGabriel Fernandez 		.plltype = (_type),\
912e5e793a6SGabriel Fernandez 		.reg_pllxcr = (_reg),\
913e5e793a6SGabriel Fernandez 	}
914e5e793a6SGabriel Fernandez 
915e5e793a6SGabriel Fernandez static const struct stm32_clk_pll stm32_mp13_clk_pll[PLL_NB] = {
916e5e793a6SGabriel Fernandez 	CLK_PLL_CFG(PLL1_ID, PLL_2000, GATE_PLL1, MUX_PLL12, RCC_PLL1CR),
917e5e793a6SGabriel Fernandez 	CLK_PLL_CFG(PLL2_ID, PLL_1600, GATE_PLL2, MUX_PLL12, RCC_PLL2CR),
918e5e793a6SGabriel Fernandez 	CLK_PLL_CFG(PLL3_ID, PLL_800, GATE_PLL3, MUX_PLL3, RCC_PLL3CR),
919e5e793a6SGabriel Fernandez 	CLK_PLL_CFG(PLL4_ID, PLL_800, GATE_PLL4, MUX_PLL4, RCC_PLL4CR),
920e5e793a6SGabriel Fernandez };
921e5e793a6SGabriel Fernandez 
clk_stm32_pll_data(unsigned int idx)922e5e793a6SGabriel Fernandez static const struct stm32_clk_pll *clk_stm32_pll_data(unsigned int idx)
923e5e793a6SGabriel Fernandez {
924e5e793a6SGabriel Fernandez 	return &stm32_mp13_clk_pll[idx];
925e5e793a6SGabriel Fernandez }
926e5e793a6SGabriel Fernandez 
927e5e793a6SGabriel Fernandez /* Clock TREE configuration */
928e5e793a6SGabriel Fernandez 
stm32_clk_configure_clk_get_binding_id(uint32_t data)929e5e793a6SGabriel Fernandez static unsigned int stm32_clk_configure_clk_get_binding_id(uint32_t data)
930e5e793a6SGabriel Fernandez {
931e5e793a6SGabriel Fernandez 	return (data & CLK_ID_MASK) >> CLK_ID_SHIFT;
932e5e793a6SGabriel Fernandez }
933e5e793a6SGabriel Fernandez 
stm32_clk_configure_clk(struct clk_stm32_priv * priv __maybe_unused,uint32_t data)934e5e793a6SGabriel Fernandez static int stm32_clk_configure_clk(struct clk_stm32_priv *priv __maybe_unused,
935e5e793a6SGabriel Fernandez 				   uint32_t data)
936e5e793a6SGabriel Fernandez {
937e5e793a6SGabriel Fernandez 	int sel = (data & CLK_SEL_MASK) >> CLK_SEL_SHIFT;
938e5e793a6SGabriel Fernandez 	int enable = (data & CLK_ON_MASK) >> CLK_ON_SHIFT;
939e5e793a6SGabriel Fernandez 	int clk_id = 0;
940e5e793a6SGabriel Fernandez 	int ret = 0;
941e5e793a6SGabriel Fernandez 	int mux = -1;
942e5e793a6SGabriel Fernandez 	int gate = -1;
943e5e793a6SGabriel Fernandez 
944e5e793a6SGabriel Fernandez 	clk_id = stm32_clk_configure_clk_get_binding_id(data);
945e5e793a6SGabriel Fernandez 
946e5e793a6SGabriel Fernandez 	switch (clk_id)	{
947e5e793a6SGabriel Fernandez 	case CK_MCO1:
948e5e793a6SGabriel Fernandez 		mux = MUX_MCO1;
949e5e793a6SGabriel Fernandez 		gate = GATE_MCO1;
950e5e793a6SGabriel Fernandez 		break;
951e5e793a6SGabriel Fernandez 
952e5e793a6SGabriel Fernandez 	case CK_MCO2:
953e5e793a6SGabriel Fernandez 		mux = MUX_MCO2;
954e5e793a6SGabriel Fernandez 		gate = GATE_MCO2;
955e5e793a6SGabriel Fernandez 		break;
956e5e793a6SGabriel Fernandez 	default:
957e5e793a6SGabriel Fernandez 		ret = -1;
958e5e793a6SGabriel Fernandez 		break;
959e5e793a6SGabriel Fernandez 	}
960e5e793a6SGabriel Fernandez 
961e5e793a6SGabriel Fernandez 	if (ret != 0)
962e5e793a6SGabriel Fernandez 		return ret;
963e5e793a6SGabriel Fernandez 
964e5e793a6SGabriel Fernandez 	if (stm32_mux_set_parent(mux, sel))
965e5e793a6SGabriel Fernandez 		return -1;
966e5e793a6SGabriel Fernandez 
967*6a977fc4SEtienne Carriere 	stm32_gate_set_init_state(gate, enable);
968e5e793a6SGabriel Fernandez 
969e5e793a6SGabriel Fernandez 	return 0;
970e5e793a6SGabriel Fernandez }
971e5e793a6SGabriel Fernandez 
stm32_clk_configure_mux(__unused struct clk_stm32_priv * priv,uint32_t data)972e5e793a6SGabriel Fernandez static int stm32_clk_configure_mux(__unused struct clk_stm32_priv *priv,
973e5e793a6SGabriel Fernandez 				   uint32_t data)
974e5e793a6SGabriel Fernandez {
975e5e793a6SGabriel Fernandez 	int mux = (data & MUX_ID_MASK) >> MUX_ID_SHIFT;
976e5e793a6SGabriel Fernandez 	int sel = (data & MUX_SEL_MASK) >> MUX_SEL_SHIFT;
977e5e793a6SGabriel Fernandez 
978e5e793a6SGabriel Fernandez 	if (mux == MUX_RTC) {
979e5e793a6SGabriel Fernandez 		/* Mux RTC clock only is selector is valid and RTC not yet
980e5e793a6SGabriel Fernandez 		 * enabled
981e5e793a6SGabriel Fernandez 		 */
982e5e793a6SGabriel Fernandez 		if (sel == 0)
983e5e793a6SGabriel Fernandez 			return 0;
984e5e793a6SGabriel Fernandez 
985e5e793a6SGabriel Fernandez 		if (stm32_gate_is_enabled(GATE_RTCCK))
986e5e793a6SGabriel Fernandez 			return 0;
987e5e793a6SGabriel Fernandez 	}
988e5e793a6SGabriel Fernandez 
989e5e793a6SGabriel Fernandez 	if (stm32_mux_set_parent(mux, sel))
990e5e793a6SGabriel Fernandez 		return -1;
991e5e793a6SGabriel Fernandez 
992e5e793a6SGabriel Fernandez 	return 0;
993e5e793a6SGabriel Fernandez }
994e5e793a6SGabriel Fernandez 
995e5e793a6SGabriel Fernandez static TEE_Result
stm32_clk_configure_div(struct clk_stm32_priv * priv __maybe_unused,uint32_t data)996e5e793a6SGabriel Fernandez stm32_clk_configure_div(struct clk_stm32_priv *priv __maybe_unused,
997e5e793a6SGabriel Fernandez 			uint32_t data)
998e5e793a6SGabriel Fernandez {
999e5e793a6SGabriel Fernandez 	int div_id = (data & DIV_ID_MASK) >> DIV_ID_SHIFT;
1000e5e793a6SGabriel Fernandez 	int div_n = (data & DIV_DIVN_MASK) >> DIV_DIVN_SHIFT;
1001e5e793a6SGabriel Fernandez 
1002e5e793a6SGabriel Fernandez 	return stm32_div_set_value(div_id, div_n);
1003e5e793a6SGabriel Fernandez }
1004e5e793a6SGabriel Fernandez 
stm32_clk_dividers_configure(struct clk_stm32_priv * priv)1005e5e793a6SGabriel Fernandez static int stm32_clk_dividers_configure(struct clk_stm32_priv *priv)
1006e5e793a6SGabriel Fernandez {
1007e5e793a6SGabriel Fernandez 	struct stm32_clk_platdata *pdata = priv->pdata;
1008e5e793a6SGabriel Fernandez 	unsigned int i = 0;
1009e5e793a6SGabriel Fernandez 
1010e5e793a6SGabriel Fernandez 	for (i = 0; i < pdata->nclkdiv; i++) {
1011e5e793a6SGabriel Fernandez 		if (stm32_clk_configure_div(priv, pdata->clkdiv[i]))
1012e5e793a6SGabriel Fernandez 			return -1;
1013e5e793a6SGabriel Fernandez 	}
1014e5e793a6SGabriel Fernandez 
1015e5e793a6SGabriel Fernandez 	return 0;
1016e5e793a6SGabriel Fernandez }
1017e5e793a6SGabriel Fernandez 
stm32_clk_source_configure(struct clk_stm32_priv * priv)1018e5e793a6SGabriel Fernandez static int stm32_clk_source_configure(struct clk_stm32_priv *priv)
1019e5e793a6SGabriel Fernandez {
1020e5e793a6SGabriel Fernandez 	struct stm32_clk_platdata *pdata = priv->pdata;
1021e5e793a6SGabriel Fernandez 	bool ckper_disabled = false;
1022e5e793a6SGabriel Fernandez 	int ret = 0;
1023e5e793a6SGabriel Fernandez 	size_t i = 0;
1024e5e793a6SGabriel Fernandez 
1025e5e793a6SGabriel Fernandez 	for (i = 0; i < pdata->nclksrc; i++) {
1026e5e793a6SGabriel Fernandez 		uint32_t val = pdata->clksrc[i];
1027e5e793a6SGabriel Fernandez 		uint32_t cmd = 0;
1028e5e793a6SGabriel Fernandez 		uint32_t cmd_data = 0;
1029e5e793a6SGabriel Fernandez 
1030e5e793a6SGabriel Fernandez 		if (val == (uint32_t)CLK_CKPER_DISABLED) {
1031e5e793a6SGabriel Fernandez 			ckper_disabled = true;
1032e5e793a6SGabriel Fernandez 			continue;
1033e5e793a6SGabriel Fernandez 		}
1034e5e793a6SGabriel Fernandez 
1035e5e793a6SGabriel Fernandez 		cmd = (val & CMD_MASK) >> CMD_SHIFT;
1036e5e793a6SGabriel Fernandez 		cmd_data = val & ~CMD_MASK;
1037e5e793a6SGabriel Fernandez 
1038e5e793a6SGabriel Fernandez 		switch (cmd) {
1039e5e793a6SGabriel Fernandez 		case CMD_MUX:
1040e5e793a6SGabriel Fernandez 			ret = stm32_clk_configure_mux(priv, cmd_data);
1041e5e793a6SGabriel Fernandez 			break;
1042e5e793a6SGabriel Fernandez 
1043e5e793a6SGabriel Fernandez 		case CMD_CLK:
1044e5e793a6SGabriel Fernandez 			ret = stm32_clk_configure_clk(priv, cmd_data);
1045e5e793a6SGabriel Fernandez 			break;
1046e5e793a6SGabriel Fernandez 		default:
1047e5e793a6SGabriel Fernandez 			ret = -1;
1048e5e793a6SGabriel Fernandez 			break;
1049e5e793a6SGabriel Fernandez 		}
1050e5e793a6SGabriel Fernandez 
1051e5e793a6SGabriel Fernandez 		if (ret != 0)
1052e5e793a6SGabriel Fernandez 			return ret;
1053e5e793a6SGabriel Fernandez 	}
1054e5e793a6SGabriel Fernandez 
1055e5e793a6SGabriel Fernandez 	/*
1056e5e793a6SGabriel Fernandez 	 * CKPER is source for some peripheral clocks
1057e5e793a6SGabriel Fernandez 	 * (FMC-NAND / QPSI-NOR) and switching source is allowed
1058e5e793a6SGabriel Fernandez 	 * only if previous clock is still ON
1059e5e793a6SGabriel Fernandez 	 * => deactivate CKPER only after switching clock
1060e5e793a6SGabriel Fernandez 	 */
1061e5e793a6SGabriel Fernandez 	if (ckper_disabled) {
1062e5e793a6SGabriel Fernandez 		ret = stm32_clk_configure_mux(priv,
1063e5e793a6SGabriel Fernandez 					      CLK_CKPER_DISABLED & CMD_MASK);
1064e5e793a6SGabriel Fernandez 		if (ret != 0)
1065e5e793a6SGabriel Fernandez 			return ret;
1066e5e793a6SGabriel Fernandez 	}
1067e5e793a6SGabriel Fernandez 
1068e5e793a6SGabriel Fernandez 	return 0;
1069e5e793a6SGabriel Fernandez }
1070e5e793a6SGabriel Fernandez 
clk_stm32_pll_get_oscillator_rate(int sel)1071e5e793a6SGabriel Fernandez static unsigned long clk_stm32_pll_get_oscillator_rate(int sel)
1072e5e793a6SGabriel Fernandez {
1073e5e793a6SGabriel Fernandez 	const int osc[] = { OSC_HSI, OSC_HSE, OSC_CSI };
1074e5e793a6SGabriel Fernandez 
1075e5e793a6SGabriel Fernandez 	assert(sel >= 0 && sel < (int)ARRAY_SIZE(osc));
1076e5e793a6SGabriel Fernandez 
1077e5e793a6SGabriel Fernandez 	return clk_stm32_get_rate_oscillateur(osc[sel]);
1078e5e793a6SGabriel Fernandez }
1079e5e793a6SGabriel Fernandez 
clk_stm32_pll_compute_cfgr1(const struct stm32_clk_pll * pll,struct stm32_pll_vco * vco,uint32_t * value)1080e5e793a6SGabriel Fernandez static int clk_stm32_pll_compute_cfgr1(const struct stm32_clk_pll *pll,
1081e5e793a6SGabriel Fernandez 				       struct stm32_pll_vco *vco,
1082e5e793a6SGabriel Fernandez 				       uint32_t *value)
1083e5e793a6SGabriel Fernandez {
1084e5e793a6SGabriel Fernandez 	int sel = (vco->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT;
1085e5e793a6SGabriel Fernandez 	uint32_t divm = vco->div_mn[PLL_CFG_M];
1086e5e793a6SGabriel Fernandez 	uint32_t divn = vco->div_mn[PLL_CFG_N];
1087e5e793a6SGabriel Fernandez 	unsigned long refclk = 0UL;
1088e5e793a6SGabriel Fernandez 
1089e5e793a6SGabriel Fernandez 	refclk = clk_stm32_pll_get_oscillator_rate(sel) / (divm + 1U);
1090e5e793a6SGabriel Fernandez 
1091e5e793a6SGabriel Fernandez 	if ((refclk < (stm32mp1_pll[pll->plltype].refclk_min * 1000000U)) ||
1092e5e793a6SGabriel Fernandez 	    (refclk > (stm32mp1_pll[pll->plltype].refclk_max * 1000000U)))
1093e5e793a6SGabriel Fernandez 		return -1;
1094e5e793a6SGabriel Fernandez 
1095e5e793a6SGabriel Fernandez 	*value = 0;
1096e5e793a6SGabriel Fernandez 
1097e5e793a6SGabriel Fernandez 	if (pll->plltype == PLL_800 && refclk >= 8000000U)
1098e5e793a6SGabriel Fernandez 		*value = 1U << RCC_PLLNCFGR1_IFRGE_SHIFT;
1099e5e793a6SGabriel Fernandez 
1100e5e793a6SGabriel Fernandez 	*value |= (divn << RCC_PLLNCFGR1_DIVN_SHIFT) & RCC_PLLNCFGR1_DIVN_MASK;
1101e5e793a6SGabriel Fernandez 	*value |= (divm << RCC_PLLNCFGR1_DIVM_SHIFT) & RCC_PLLNCFGR1_DIVM_MASK;
1102e5e793a6SGabriel Fernandez 
1103e5e793a6SGabriel Fernandez 	return 0;
1104e5e793a6SGabriel Fernandez }
1105e5e793a6SGabriel Fernandez 
clk_stm32_pll_compute_cfgr2(struct stm32_pll_output * out)1106e5e793a6SGabriel Fernandez static uint32_t  clk_stm32_pll_compute_cfgr2(struct stm32_pll_output *out)
1107e5e793a6SGabriel Fernandez {
1108e5e793a6SGabriel Fernandez 	uint32_t value = 0;
1109e5e793a6SGabriel Fernandez 
1110e5e793a6SGabriel Fernandez 	value |= (out->output[PLL_CFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1111e5e793a6SGabriel Fernandez 		 RCC_PLLNCFGR2_DIVP_MASK;
1112e5e793a6SGabriel Fernandez 	value |= (out->output[PLL_CFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1113e5e793a6SGabriel Fernandez 		 RCC_PLLNCFGR2_DIVQ_MASK;
1114e5e793a6SGabriel Fernandez 	value |= (out->output[PLL_CFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1115e5e793a6SGabriel Fernandez 		 RCC_PLLNCFGR2_DIVR_MASK;
1116e5e793a6SGabriel Fernandez 
1117e5e793a6SGabriel Fernandez 	return value;
1118e5e793a6SGabriel Fernandez }
1119e5e793a6SGabriel Fernandez 
1120e5e793a6SGabriel Fernandez /*
1121e5e793a6SGabriel Fernandez  * Check if PLL1 can be configured on the fly.
1122e5e793a6SGabriel Fernandez  * @result (-1) => config on the fly is not possible.
1123e5e793a6SGabriel Fernandez  *         (0)  => config on the fly is possible.
1124e5e793a6SGabriel Fernandez  *         (+1) => same parameters, no need to reconfigure.
1125e5e793a6SGabriel Fernandez  * Return value is 0 if no error.
1126e5e793a6SGabriel Fernandez  */
clk_stm32_is_pll_config_on_the_fly(struct clk_stm32_priv * priv,const struct stm32_clk_pll * pll,struct stm32_pll_dt_cfg * pll_conf,int * result)1127e5e793a6SGabriel Fernandez static int clk_stm32_is_pll_config_on_the_fly(struct clk_stm32_priv *priv,
1128e5e793a6SGabriel Fernandez 					      const struct stm32_clk_pll *pll,
1129e5e793a6SGabriel Fernandez 					      struct stm32_pll_dt_cfg *pll_conf,
1130e5e793a6SGabriel Fernandez 					      int *result)
1131e5e793a6SGabriel Fernandez {
1132e5e793a6SGabriel Fernandez 	uintptr_t pll_base = priv->base + pll->reg_pllxcr;
1133e5e793a6SGabriel Fernandez 	struct stm32_pll_vco *vco = &pll_conf->vco;
1134e5e793a6SGabriel Fernandez 	struct stm32_pll_output *out = &pll_conf->output;
1135e5e793a6SGabriel Fernandez 	uint32_t fracr = 0;
1136e5e793a6SGabriel Fernandez 	uint32_t value = 0;
1137e5e793a6SGabriel Fernandez 	int ret = 0;
1138e5e793a6SGabriel Fernandez 	size_t sel = 0;
1139e5e793a6SGabriel Fernandez 
1140e5e793a6SGabriel Fernandez 	ret = clk_stm32_pll_compute_cfgr1(pll, vco, &value);
1141e5e793a6SGabriel Fernandez 	if (ret != 0)
1142e5e793a6SGabriel Fernandez 		return ret;
1143e5e793a6SGabriel Fernandez 
1144e5e793a6SGabriel Fernandez 	sel = (vco->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT;
1145e5e793a6SGabriel Fernandez 	if (sel != stm32_mux_get_parent(pll->mux_id)) {
1146e5e793a6SGabriel Fernandez 		/* Clock source of the PLL is different */
1147e5e793a6SGabriel Fernandez 		*result = -1;
1148e5e793a6SGabriel Fernandez 		return 0;
1149e5e793a6SGabriel Fernandez 	}
1150e5e793a6SGabriel Fernandez 
1151e5e793a6SGabriel Fernandez 	if (io_read32(pll_base + RCC_OFFSET_PLLXCFGR1) != value) {
1152e5e793a6SGabriel Fernandez 		/* Different DIVN/DIVM, can't config on the fly */
1153e5e793a6SGabriel Fernandez 		*result = -1;
1154e5e793a6SGabriel Fernandez 		return 0;
1155e5e793a6SGabriel Fernandez 	}
1156e5e793a6SGabriel Fernandez 
1157e5e793a6SGabriel Fernandez 	*result = 1;
1158e5e793a6SGabriel Fernandez 
1159e5e793a6SGabriel Fernandez 	fracr = vco->frac << RCC_PLLNFRACR_FRACV_SHIFT;
1160e5e793a6SGabriel Fernandez 	fracr |= RCC_PLLNCFGR1_DIVM_MASK;
1161e5e793a6SGabriel Fernandez 	value = clk_stm32_pll_compute_cfgr2(out);
1162e5e793a6SGabriel Fernandez 
1163e5e793a6SGabriel Fernandez 	if ((io_read32(pll_base + RCC_OFFSET_PLLXFRACR) == fracr) &&
1164e5e793a6SGabriel Fernandez 	    (io_read32(pll_base + RCC_OFFSET_PLLXCFGR2) == value)) {
1165e5e793a6SGabriel Fernandez 		/* Same parameters, no need to config */
1166e5e793a6SGabriel Fernandez 		*result = 1;
1167e5e793a6SGabriel Fernandez 	} else {
1168e5e793a6SGabriel Fernandez 		*result = 0;
1169e5e793a6SGabriel Fernandez 	}
1170e5e793a6SGabriel Fernandez 
1171e5e793a6SGabriel Fernandez 	return 0;
1172e5e793a6SGabriel Fernandez }
1173e5e793a6SGabriel Fernandez 
stm32_clk_hsidiv_configure(struct clk_stm32_priv * priv)1174e5e793a6SGabriel Fernandez static int stm32_clk_hsidiv_configure(struct clk_stm32_priv *priv)
1175e5e793a6SGabriel Fernandez {
1176e5e793a6SGabriel Fernandez 	struct stm32_clk_platdata *pdata = priv->pdata;
1177e5e793a6SGabriel Fernandez 	struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_HSI];
1178e5e793a6SGabriel Fernandez 
1179e5e793a6SGabriel Fernandez 	return stm32_div_set_rate(DIV_HSI, osci->freq, MAX_HSI_HZ);
1180e5e793a6SGabriel Fernandez }
1181e5e793a6SGabriel Fernandez 
clk_stm32_pll_config_vco(struct clk_stm32_priv * priv,const struct stm32_clk_pll * pll,struct stm32_pll_vco * vco)1182e5e793a6SGabriel Fernandez static void clk_stm32_pll_config_vco(struct clk_stm32_priv *priv,
1183e5e793a6SGabriel Fernandez 				     const struct stm32_clk_pll *pll,
1184e5e793a6SGabriel Fernandez 				     struct stm32_pll_vco *vco)
1185e5e793a6SGabriel Fernandez {
1186e5e793a6SGabriel Fernandez 	uintptr_t pll_base = priv->base + pll->reg_pllxcr;
1187e5e793a6SGabriel Fernandez 	uint32_t value = 0;
1188e5e793a6SGabriel Fernandez 
1189e5e793a6SGabriel Fernandez 	if (clk_stm32_pll_compute_cfgr1(pll, vco, &value) != 0) {
1190e5e793a6SGabriel Fernandez 		EMSG("Invalid Vref clock");
1191e5e793a6SGabriel Fernandez 		panic();
1192e5e793a6SGabriel Fernandez 	}
1193e5e793a6SGabriel Fernandez 
1194e5e793a6SGabriel Fernandez 	/* Write N / M / IFREGE fields */
1195e5e793a6SGabriel Fernandez 	io_write32(pll_base + RCC_OFFSET_PLLXCFGR1, value);
1196e5e793a6SGabriel Fernandez 
1197e5e793a6SGabriel Fernandez 	/* Fractional configuration */
1198e5e793a6SGabriel Fernandez 	io_write32(pll_base + RCC_OFFSET_PLLXFRACR, 0);
1199e5e793a6SGabriel Fernandez 
1200e5e793a6SGabriel Fernandez 	/* Frac must be enabled only once its configuration is loaded */
1201e5e793a6SGabriel Fernandez 	io_write32(pll_base + RCC_OFFSET_PLLXFRACR,
1202e5e793a6SGabriel Fernandez 		   vco->frac << RCC_PLLNFRACR_FRACV_SHIFT);
1203e5e793a6SGabriel Fernandez 
1204e5e793a6SGabriel Fernandez 	io_setbits32(pll_base + RCC_OFFSET_PLLXFRACR, RCC_PLLNFRACR_FRACLE);
1205e5e793a6SGabriel Fernandez }
1206e5e793a6SGabriel Fernandez 
clk_stm32_pll_config_csg(struct clk_stm32_priv * priv,const struct stm32_clk_pll * pll,struct stm32_pll_vco * vco)1207e5e793a6SGabriel Fernandez static void clk_stm32_pll_config_csg(struct clk_stm32_priv *priv,
1208e5e793a6SGabriel Fernandez 				     const struct stm32_clk_pll *pll,
1209e5e793a6SGabriel Fernandez 				     struct stm32_pll_vco *vco)
1210e5e793a6SGabriel Fernandez {
1211e5e793a6SGabriel Fernandez 	uintptr_t pll_base = priv->base + pll->reg_pllxcr;
1212e5e793a6SGabriel Fernandez 	uint32_t mod_per = 0;
1213e5e793a6SGabriel Fernandez 	uint32_t inc_step = 0;
1214e5e793a6SGabriel Fernandez 	uint32_t sscg_mode = 0;
1215e5e793a6SGabriel Fernandez 	uint32_t value = 0;
1216e5e793a6SGabriel Fernandez 
1217e5e793a6SGabriel Fernandez 	if (!vco->csg_enabled)
1218e5e793a6SGabriel Fernandez 		return;
1219e5e793a6SGabriel Fernandez 
1220e5e793a6SGabriel Fernandez 	mod_per = vco->csg[PLL_CSG_MOD_PER];
1221e5e793a6SGabriel Fernandez 	inc_step = vco->csg[PLL_CSG_INC_STEP];
1222e5e793a6SGabriel Fernandez 	sscg_mode = vco->csg[PLL_CSG_SSCG_MODE];
1223e5e793a6SGabriel Fernandez 
1224e5e793a6SGabriel Fernandez 	value |= (mod_per << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1225e5e793a6SGabriel Fernandez 		 RCC_PLLNCSGR_MOD_PER_MASK;
1226e5e793a6SGabriel Fernandez 	value |= (inc_step << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1227e5e793a6SGabriel Fernandez 		 RCC_PLLNCSGR_INC_STEP_MASK;
1228e5e793a6SGabriel Fernandez 	value |= (sscg_mode << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1229e5e793a6SGabriel Fernandez 		 RCC_PLLNCSGR_SSCG_MODE_MASK;
1230e5e793a6SGabriel Fernandez 
1231e5e793a6SGabriel Fernandez 	io_write32(pll_base + RCC_OFFSET_PLLXCSGR, value);
1232e5e793a6SGabriel Fernandez 	io_setbits32(pll_base + RCC_OFFSET_PLLXCR, RCC_PLLNCR_SSCG_CTRL);
1233e5e793a6SGabriel Fernandez }
1234e5e793a6SGabriel Fernandez 
clk_stm32_pll_config_out(struct clk_stm32_priv * priv,const struct stm32_clk_pll * pll,struct stm32_pll_output * out)1235e5e793a6SGabriel Fernandez static void clk_stm32_pll_config_out(struct clk_stm32_priv *priv,
1236e5e793a6SGabriel Fernandez 				     const struct stm32_clk_pll *pll,
1237e5e793a6SGabriel Fernandez 				     struct stm32_pll_output *out)
1238e5e793a6SGabriel Fernandez {
1239e5e793a6SGabriel Fernandez 	uintptr_t pll_base = priv->base + pll->reg_pllxcr;
1240e5e793a6SGabriel Fernandez 	uint32_t value = 0;
1241e5e793a6SGabriel Fernandez 
1242e5e793a6SGabriel Fernandez 	value = clk_stm32_pll_compute_cfgr2(out);
1243e5e793a6SGabriel Fernandez 
1244e5e793a6SGabriel Fernandez 	io_write32(pll_base + RCC_OFFSET_PLLXCFGR2, value);
1245e5e793a6SGabriel Fernandez }
1246e5e793a6SGabriel Fernandez 
clk_stm32_pll_get_pdata(int pll_idx)1247e5e793a6SGabriel Fernandez static struct stm32_pll_dt_cfg *clk_stm32_pll_get_pdata(int pll_idx)
1248e5e793a6SGabriel Fernandez {
1249e5e793a6SGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
1250e5e793a6SGabriel Fernandez 	struct stm32_clk_platdata *pdata = priv->pdata;
1251e5e793a6SGabriel Fernandez 
1252e5e793a6SGabriel Fernandez 	return &pdata->pll[pll_idx];
1253e5e793a6SGabriel Fernandez }
1254e5e793a6SGabriel Fernandez 
clk_stm32_pll_init_switch_to_hsi_clk_system(int mux_sys)1255e5e793a6SGabriel Fernandez static int clk_stm32_pll_init_switch_to_hsi_clk_system(int mux_sys)
1256e5e793a6SGabriel Fernandez {
1257e5e793a6SGabriel Fernandez 	int sel = 0;
1258e5e793a6SGabriel Fernandez 
1259e5e793a6SGabriel Fernandez 	if (mux_sys == -1)
1260e5e793a6SGabriel Fernandez 		return -1;
1261e5e793a6SGabriel Fernandez 
1262e5e793a6SGabriel Fernandez 	/* Make a backup to the current parent */
1263e5e793a6SGabriel Fernandez 	sel = stm32_mux_get_parent(mux_sys);
1264e5e793a6SGabriel Fernandez 
1265e5e793a6SGabriel Fernandez 	/* Switch to HSI */
1266e5e793a6SGabriel Fernandez 	if (stm32_mux_set_parent(mux_sys, 0))
1267e5e793a6SGabriel Fernandez 		return -1;
1268e5e793a6SGabriel Fernandez 
1269e5e793a6SGabriel Fernandez 	return sel;
1270e5e793a6SGabriel Fernandez }
1271e5e793a6SGabriel Fernandez 
1272e5e793a6SGabriel Fernandez static uint32_t
clk_stm32_pll_backup_output_diven(const struct stm32_clk_pll * pll)1273e5e793a6SGabriel Fernandez clk_stm32_pll_backup_output_diven(const struct stm32_clk_pll *pll)
1274e5e793a6SGabriel Fernandez {
1275e5e793a6SGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
1276e5e793a6SGabriel Fernandez 	uintptr_t addr = priv->base + pll->reg_pllxcr;
1277e5e793a6SGabriel Fernandez 
1278e5e793a6SGabriel Fernandez 	return io_read32(addr + RCC_OFFSET_PLLXCR) &
1279e5e793a6SGabriel Fernandez 			 (RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1280e5e793a6SGabriel Fernandez 			  RCC_PLLNCR_DIVREN);
1281e5e793a6SGabriel Fernandez }
1282e5e793a6SGabriel Fernandez 
clk_stm32_pll_restore_output_diven(const struct stm32_clk_pll * pll,uint32_t value)1283e5e793a6SGabriel Fernandez static void clk_stm32_pll_restore_output_diven(const struct stm32_clk_pll *pll,
1284e5e793a6SGabriel Fernandez 					       uint32_t value)
1285e5e793a6SGabriel Fernandez {
1286e5e793a6SGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
1287e5e793a6SGabriel Fernandez 	uintptr_t addr = priv->base + pll->reg_pllxcr;
1288e5e793a6SGabriel Fernandez 	const uint32_t mask = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1289e5e793a6SGabriel Fernandez 			      RCC_PLLNCR_DIVREN;
1290e5e793a6SGabriel Fernandez 
1291e5e793a6SGabriel Fernandez 	io_clrsetbits32(addr, mask, value & mask);
1292e5e793a6SGabriel Fernandez }
1293e5e793a6SGabriel Fernandez 
clk_stm32_pll_init(struct clk_stm32_priv * priv,int pll_idx,struct stm32_pll_dt_cfg * pll_conf)1294e5e793a6SGabriel Fernandez static int clk_stm32_pll_init(struct clk_stm32_priv *priv, int pll_idx,
1295e5e793a6SGabriel Fernandez 			      struct stm32_pll_dt_cfg *pll_conf)
1296e5e793a6SGabriel Fernandez {
1297e5e793a6SGabriel Fernandez 	const struct stm32_clk_pll *pll = clk_stm32_pll_data(pll_idx);
1298e5e793a6SGabriel Fernandez 	int config_on_the_fly = -1;
1299e5e793a6SGabriel Fernandez 	int ret = 0;
1300e5e793a6SGabriel Fernandez 	uint8_t sel = 0;
1301e5e793a6SGabriel Fernandez 	uint32_t save_div_pqr_en = 0;
1302e5e793a6SGabriel Fernandez 	int mux_system[] = { MUX_MPU, MUX_AXI, MUX_MLAHB, -1 };
1303e5e793a6SGabriel Fernandez 	int mux_sys  = mux_system[pll_idx];
1304e5e793a6SGabriel Fernandez 
1305e5e793a6SGabriel Fernandez 	ret = clk_stm32_is_pll_config_on_the_fly(priv, pll, pll_conf,
1306e5e793a6SGabriel Fernandez 						 &config_on_the_fly);
1307e5e793a6SGabriel Fernandez 	if (ret != 0)
1308e5e793a6SGabriel Fernandez 		return ret;
1309e5e793a6SGabriel Fernandez 
1310e5e793a6SGabriel Fernandez 	/* Backup status of DIV DIVPEN / DIVQEN / DIVREN */
1311e5e793a6SGabriel Fernandez 	save_div_pqr_en = clk_stm32_pll_backup_output_diven(pll);
1312e5e793a6SGabriel Fernandez 
1313e5e793a6SGabriel Fernandez 	if (config_on_the_fly == -1) {
1314e5e793a6SGabriel Fernandez 		/* Make a backup to the current parent and switch to HSI */
1315e5e793a6SGabriel Fernandez 		sel = clk_stm32_pll_init_switch_to_hsi_clk_system(mux_sys);
1316e5e793a6SGabriel Fernandez 
1317e5e793a6SGabriel Fernandez 		/* Stop the PLL before */
1318e5e793a6SGabriel Fernandez 		if (stm32_gate_is_enabled(pll->gate_id))  {
1319e5e793a6SGabriel Fernandez 			io_clrbits32(priv->base + pll->reg_pllxcr,
1320e5e793a6SGabriel Fernandez 				     RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1321e5e793a6SGabriel Fernandez 				     RCC_PLLNCR_DIVREN);
1322e5e793a6SGabriel Fernandez 
1323e5e793a6SGabriel Fernandez 			if (stm32_gate_rdy_disable(pll->gate_id))
1324e5e793a6SGabriel Fernandez 				return -1;
1325e5e793a6SGabriel Fernandez 		}
1326e5e793a6SGabriel Fernandez 
1327e5e793a6SGabriel Fernandez 		/* Configure PLLs source */
1328e5e793a6SGabriel Fernandez 		ret = stm32_clk_configure_mux(priv, pll_conf->vco.src);
1329e5e793a6SGabriel Fernandez 		if (ret)
1330e5e793a6SGabriel Fernandez 			return ret;
1331e5e793a6SGabriel Fernandez 
1332e5e793a6SGabriel Fernandez 		clk_stm32_pll_config_vco(priv, pll, &pll_conf->vco);
1333e5e793a6SGabriel Fernandez 	}
1334e5e793a6SGabriel Fernandez 
1335e5e793a6SGabriel Fernandez 	if (config_on_the_fly != 1) {
1336e5e793a6SGabriel Fernandez 		clk_stm32_pll_config_out(priv, pll, &pll_conf->output);
1337e5e793a6SGabriel Fernandez 		clk_stm32_pll_config_csg(priv, pll, &pll_conf->vco);
1338e5e793a6SGabriel Fernandez 	}
1339e5e793a6SGabriel Fernandez 
1340e5e793a6SGabriel Fernandez 	if (!stm32_gate_is_enabled(pll->gate_id)) {
1341e5e793a6SGabriel Fernandez 		if (stm32_gate_rdy_enable(pll->gate_id))
1342e5e793a6SGabriel Fernandez 			return -1;
1343e5e793a6SGabriel Fernandez 
1344e5e793a6SGabriel Fernandez 		clk_stm32_pll_restore_output_diven(pll, save_div_pqr_en);
1345e5e793a6SGabriel Fernandez 	}
1346e5e793a6SGabriel Fernandez 
1347e5e793a6SGabriel Fernandez 	if ((config_on_the_fly == -1) && (mux_sys != -1)) {
1348e5e793a6SGabriel Fernandez 		/* Restore to backup parent */
1349e5e793a6SGabriel Fernandez 		if (stm32_mux_set_parent(mux_sys, sel))
1350e5e793a6SGabriel Fernandez 			return -1;
1351e5e793a6SGabriel Fernandez 	}
1352e5e793a6SGabriel Fernandez 
1353e5e793a6SGabriel Fernandez 	return 0;
1354e5e793a6SGabriel Fernandez }
1355e5e793a6SGabriel Fernandez 
stm32_clk_pll_configure(struct clk_stm32_priv * priv)1356e5e793a6SGabriel Fernandez static int stm32_clk_pll_configure(struct clk_stm32_priv *priv)
1357e5e793a6SGabriel Fernandez {
1358e5e793a6SGabriel Fernandez 	struct stm32_pll_dt_cfg *pll_conf = NULL;
1359e5e793a6SGabriel Fernandez 	size_t i = 0;
1360e5e793a6SGabriel Fernandez 	const int plls[] = { PLL1_ID, PLL3_ID, PLL4_ID };
1361e5e793a6SGabriel Fernandez 
1362e5e793a6SGabriel Fernandez 	for (i = 0; i < ARRAY_SIZE(plls); i++) {
1363e5e793a6SGabriel Fernandez 		pll_conf = clk_stm32_pll_get_pdata(plls[i]);
1364e5e793a6SGabriel Fernandez 
1365e5e793a6SGabriel Fernandez 		if (pll_conf->vco.status) {
1366e5e793a6SGabriel Fernandez 			int err = 0;
1367e5e793a6SGabriel Fernandez 
1368e5e793a6SGabriel Fernandez 			err = clk_stm32_pll_init(priv, plls[i], pll_conf);
1369e5e793a6SGabriel Fernandez 			if (err)
1370e5e793a6SGabriel Fernandez 				return err;
1371e5e793a6SGabriel Fernandez 		}
1372e5e793a6SGabriel Fernandez 	}
1373e5e793a6SGabriel Fernandez 
1374e5e793a6SGabriel Fernandez 	return 0;
1375e5e793a6SGabriel Fernandez }
1376e5e793a6SGabriel Fernandez 
stm32mp1_init_clock_tree(struct clk_stm32_priv * priv,struct stm32_clk_platdata * pdata)1377e5e793a6SGabriel Fernandez static int stm32mp1_init_clock_tree(struct clk_stm32_priv *priv,
1378e5e793a6SGabriel Fernandez 				    struct stm32_clk_platdata *pdata)
1379e5e793a6SGabriel Fernandez {
1380e5e793a6SGabriel Fernandez 	int ret = 0;
1381e5e793a6SGabriel Fernandez 
1382e5e793a6SGabriel Fernandez 	/*
1383e5e793a6SGabriel Fernandez 	 * Switch ON oscillators found in device-tree.
1384e5e793a6SGabriel Fernandez 	 * Note: HSI already ON after BootROM stage.
1385e5e793a6SGabriel Fernandez 	 */
1386e5e793a6SGabriel Fernandez 	stm32_clk_oscillators_enable(priv, pdata);
1387e5e793a6SGabriel Fernandez 
1388e5e793a6SGabriel Fernandez 	ret = stm32_clk_hsidiv_configure(priv);
1389e5e793a6SGabriel Fernandez 	if (ret != 0)
1390e5e793a6SGabriel Fernandez 		return ret;
1391e5e793a6SGabriel Fernandez 
1392e5e793a6SGabriel Fernandez 	ret = stm32_clk_dividers_configure(priv);
1393e5e793a6SGabriel Fernandez 	if (ret != 0)
1394e5e793a6SGabriel Fernandez 		panic();
1395e5e793a6SGabriel Fernandez 
1396e5e793a6SGabriel Fernandez 	ret = stm32_clk_pll_configure(priv);
1397e5e793a6SGabriel Fernandez 	if (ret != 0)
1398e5e793a6SGabriel Fernandez 		panic();
1399e5e793a6SGabriel Fernandez 
1400e5e793a6SGabriel Fernandez 	/* Wait LSE ready before to use it */
1401e5e793a6SGabriel Fernandez 	ret = stm32_clk_oscillators_wait_lse_ready(priv, pdata);
1402e5e793a6SGabriel Fernandez 	if (ret != 0)
1403e5e793a6SGabriel Fernandez 		panic();
1404e5e793a6SGabriel Fernandez 
1405e5e793a6SGabriel Fernandez 	/* Configure with expected clock source */
1406e5e793a6SGabriel Fernandez 	ret = stm32_clk_source_configure(priv);
1407e5e793a6SGabriel Fernandez 	if (ret != 0)
1408e5e793a6SGabriel Fernandez 		panic();
1409e5e793a6SGabriel Fernandez 
1410e5e793a6SGabriel Fernandez 	/* Configure LSE CSS after RTC source configuration */
1411e5e793a6SGabriel Fernandez 	ret = stm32_clk_oscillators_lse_set_css(priv, pdata);
1412e5e793a6SGabriel Fernandez 	if (ret != 0)
1413e5e793a6SGabriel Fernandez 		panic();
1414e5e793a6SGabriel Fernandez 
1415e5e793a6SGabriel Fernandez 	/* Software Self-Refresh mode (SSR) during DDR initilialization */
1416e5e793a6SGabriel Fernandez 	io_clrsetbits32(priv->base + RCC_DDRITFCR, RCC_DDRITFCR_DDRCKMOD_MASK,
1417e5e793a6SGabriel Fernandez 			RCC_DDRITFCR_DDRCKMOD_SSR <<
1418e5e793a6SGabriel Fernandez 			RCC_DDRITFCR_DDRCKMOD_SHIFT);
1419e5e793a6SGabriel Fernandez 
1420e5e793a6SGabriel Fernandez 	return 0;
1421e5e793a6SGabriel Fernandez }
1422e5e793a6SGabriel Fernandez 
clk_stm32_parse_oscillator_fdt(const void * fdt,int node,const char * name,struct stm32_osci_dt_cfg * osci)1423e5e793a6SGabriel Fernandez static int clk_stm32_parse_oscillator_fdt(const void *fdt, int node,
1424e5e793a6SGabriel Fernandez 					  const char *name,
1425e5e793a6SGabriel Fernandez 					  struct stm32_osci_dt_cfg *osci)
1426e5e793a6SGabriel Fernandez {
1427e5e793a6SGabriel Fernandez 	int subnode = 0;
1428e5e793a6SGabriel Fernandez 
1429e5e793a6SGabriel Fernandez 	fdt_for_each_subnode(subnode, fdt, node) {
1430e5e793a6SGabriel Fernandez 		const char *cchar = NULL;
1431e5e793a6SGabriel Fernandez 		const fdt32_t *cuint = NULL;
1432e5e793a6SGabriel Fernandez 		int ret = 0;
1433e5e793a6SGabriel Fernandez 
1434e5e793a6SGabriel Fernandez 		cchar = fdt_get_name(fdt, subnode, &ret);
1435e5e793a6SGabriel Fernandez 		if (!cchar)
1436e5e793a6SGabriel Fernandez 			return ret;
1437e5e793a6SGabriel Fernandez 
1438e5e793a6SGabriel Fernandez 		if (strncmp(cchar, name, (size_t)ret) ||
1439f354a5d8SGatien Chevallier 		    fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED)
1440e5e793a6SGabriel Fernandez 			continue;
1441e5e793a6SGabriel Fernandez 
1442e5e793a6SGabriel Fernandez 		cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret);
1443e5e793a6SGabriel Fernandez 		if (!cuint)
1444e5e793a6SGabriel Fernandez 			panic();
1445e5e793a6SGabriel Fernandez 
1446e5e793a6SGabriel Fernandez 		osci->freq = fdt32_to_cpu(*cuint);
1447e5e793a6SGabriel Fernandez 
1448e5e793a6SGabriel Fernandez 		if (fdt_getprop(fdt, subnode, "st,bypass", NULL))
1449e5e793a6SGabriel Fernandez 			osci->bypass = true;
1450e5e793a6SGabriel Fernandez 
1451e5e793a6SGabriel Fernandez 		if (fdt_getprop(fdt, subnode, "st,digbypass", NULL))
1452e5e793a6SGabriel Fernandez 			osci->digbyp = true;
1453e5e793a6SGabriel Fernandez 
1454e5e793a6SGabriel Fernandez 		if (fdt_getprop(fdt, subnode, "st,css", NULL))
1455e5e793a6SGabriel Fernandez 			osci->css = true;
1456e5e793a6SGabriel Fernandez 
1457f354a5d8SGatien Chevallier 		osci->drive = fdt_read_uint32_default(fdt, subnode, "st,drive",
1458e5e793a6SGabriel Fernandez 						      LSEDRV_MEDIUM_HIGH);
1459e5e793a6SGabriel Fernandez 
1460e5e793a6SGabriel Fernandez 		return 0;
1461e5e793a6SGabriel Fernandez 	}
1462e5e793a6SGabriel Fernandez 
1463e5e793a6SGabriel Fernandez 	return -FDT_ERR_NOTFOUND;
1464e5e793a6SGabriel Fernandez }
1465e5e793a6SGabriel Fernandez 
stm32_clk_parse_fdt_all_oscillator(const void * fdt,int node __maybe_unused,struct stm32_clk_platdata * pdata)1466e5e793a6SGabriel Fernandez static int stm32_clk_parse_fdt_all_oscillator(const void *fdt,
1467e5e793a6SGabriel Fernandez 					      int node __maybe_unused,
1468e5e793a6SGabriel Fernandez 					      struct stm32_clk_platdata *pdata)
1469e5e793a6SGabriel Fernandez {
1470e5e793a6SGabriel Fernandez 	int fdt_err = 0;
1471e5e793a6SGabriel Fernandez 	size_t i = 0;
1472e5e793a6SGabriel Fernandez 	int osc_node = 0;
1473e5e793a6SGabriel Fernandez 
1474e5e793a6SGabriel Fernandez 	osc_node = fdt_path_offset(fdt, "/clocks");
1475e5e793a6SGabriel Fernandez 	if (osc_node < 0)
1476e5e793a6SGabriel Fernandez 		return -FDT_ERR_NOTFOUND;
1477e5e793a6SGabriel Fernandez 
1478e5e793a6SGabriel Fernandez 	for (i = 0; i < NB_OSCILLATOR; i++) {
1479e5e793a6SGabriel Fernandez 		struct stm32_osci_dt_cfg *osci = &pdata->osci[i];
1480e5e793a6SGabriel Fernandez 		struct clk_oscillator_data *osc_data = NULL;
1481e5e793a6SGabriel Fernandez 
1482e5e793a6SGabriel Fernandez 		osc_data = clk_oscillator_get_data(i);
1483e5e793a6SGabriel Fernandez 
1484e5e793a6SGabriel Fernandez 		fdt_err = clk_stm32_parse_oscillator_fdt(fdt, osc_node,
1485e5e793a6SGabriel Fernandez 							 osc_data->name, osci);
148695f2142bSEtienne Carriere 		if (fdt_err) {
148795f2142bSEtienne Carriere 			if (fdt_err == -FDT_ERR_NOTFOUND) {
148895f2142bSEtienne Carriere 				/* Oscillator not found means it is not wired */
148995f2142bSEtienne Carriere 				osci->freq = 0;
149095f2142bSEtienne Carriere 			} else {
1491e5e793a6SGabriel Fernandez 				panic();
1492e5e793a6SGabriel Fernandez 			}
149395f2142bSEtienne Carriere 		}
149495f2142bSEtienne Carriere 	}
1495e5e793a6SGabriel Fernandez 
1496e5e793a6SGabriel Fernandez 	return 0;
1497e5e793a6SGabriel Fernandez }
1498e5e793a6SGabriel Fernandez 
clk_stm32_load_vco_config_fdt(const void * fdt,int subnode,struct stm32_pll_vco * vco)1499e5e793a6SGabriel Fernandez static int clk_stm32_load_vco_config_fdt(const void *fdt, int subnode,
1500e5e793a6SGabriel Fernandez 					 struct stm32_pll_vco *vco)
1501e5e793a6SGabriel Fernandez {
1502e5e793a6SGabriel Fernandez 	int ret = 0;
1503e5e793a6SGabriel Fernandez 
1504f354a5d8SGatien Chevallier 	ret = fdt_read_uint32_array(fdt, subnode, "divmn", vco->div_mn,
1505e5e793a6SGabriel Fernandez 				    PLL_DIV_MN_NB);
1506e5e793a6SGabriel Fernandez 	if (ret != 0)
1507e5e793a6SGabriel Fernandez 		return ret;
1508e5e793a6SGabriel Fernandez 
1509f354a5d8SGatien Chevallier 	ret = fdt_read_uint32_array(fdt, subnode, "csg", vco->csg,
1510e5e793a6SGabriel Fernandez 				    PLL_CSG_NB);
1511e5e793a6SGabriel Fernandez 
1512e5e793a6SGabriel Fernandez 	vco->csg_enabled = (ret == 0);
1513e5e793a6SGabriel Fernandez 
1514e5e793a6SGabriel Fernandez 	if (ret == -FDT_ERR_NOTFOUND)
1515e5e793a6SGabriel Fernandez 		ret = 0;
1516e5e793a6SGabriel Fernandez 
1517e5e793a6SGabriel Fernandez 	if (ret != 0)
1518e5e793a6SGabriel Fernandez 		return ret;
1519e5e793a6SGabriel Fernandez 
1520e5e793a6SGabriel Fernandez 	vco->status = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1521e5e793a6SGabriel Fernandez 		      RCC_PLLNCR_DIVREN | RCC_PLLNCR_PLLON;
1522e5e793a6SGabriel Fernandez 
1523f354a5d8SGatien Chevallier 	vco->frac = fdt_read_uint32_default(fdt, subnode, "frac", 0);
1524e5e793a6SGabriel Fernandez 
1525f354a5d8SGatien Chevallier 	vco->src = fdt_read_uint32_default(fdt, subnode, "src", UINT32_MAX);
1526e5e793a6SGabriel Fernandez 
1527e5e793a6SGabriel Fernandez 	return 0;
1528e5e793a6SGabriel Fernandez }
1529e5e793a6SGabriel Fernandez 
clk_stm32_load_output_config_fdt(const void * fdt,int subnode,struct stm32_pll_output * output)1530e5e793a6SGabriel Fernandez static int clk_stm32_load_output_config_fdt(const void *fdt, int subnode,
1531e5e793a6SGabriel Fernandez 					    struct stm32_pll_output *output)
1532e5e793a6SGabriel Fernandez {
1533f354a5d8SGatien Chevallier 	return fdt_read_uint32_array(fdt, subnode, "st,pll_div_pqr",
1534e5e793a6SGabriel Fernandez 				     output->output, (int)PLL_DIV_PQR_NB);
1535e5e793a6SGabriel Fernandez }
1536e5e793a6SGabriel Fernandez 
clk_stm32_parse_pll_fdt(const void * fdt,int subnode,struct stm32_pll_dt_cfg * pll)1537e5e793a6SGabriel Fernandez static int clk_stm32_parse_pll_fdt(const void *fdt, int subnode,
1538e5e793a6SGabriel Fernandez 				   struct stm32_pll_dt_cfg *pll)
1539e5e793a6SGabriel Fernandez {
1540e5e793a6SGabriel Fernandez 	const fdt32_t *cuint = NULL;
1541e5e793a6SGabriel Fernandez 	int subnode_pll = 0;
1542e5e793a6SGabriel Fernandez 	int subnode_vco = 0;
1543e5e793a6SGabriel Fernandez 	int err = 0;
1544e5e793a6SGabriel Fernandez 
1545e5e793a6SGabriel Fernandez 	cuint = fdt_getprop(fdt, subnode, "st,pll", NULL);
1546e5e793a6SGabriel Fernandez 	if (!cuint)
1547e5e793a6SGabriel Fernandez 		return -FDT_ERR_NOTFOUND;
1548e5e793a6SGabriel Fernandez 
1549e5e793a6SGabriel Fernandez 	subnode_pll = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
1550e5e793a6SGabriel Fernandez 	if (subnode_pll < 0)
1551e5e793a6SGabriel Fernandez 		return -FDT_ERR_NOTFOUND;
1552e5e793a6SGabriel Fernandez 
1553e5e793a6SGabriel Fernandez 	cuint = fdt_getprop(fdt, subnode_pll, "st,pll_vco", NULL);
1554e5e793a6SGabriel Fernandez 	if (!cuint)
1555e5e793a6SGabriel Fernandez 		return -FDT_ERR_NOTFOUND;
1556e5e793a6SGabriel Fernandez 
1557e5e793a6SGabriel Fernandez 	subnode_vco = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
1558e5e793a6SGabriel Fernandez 	if (subnode_vco < 0)
1559e5e793a6SGabriel Fernandez 		return -FDT_ERR_NOTFOUND;
1560e5e793a6SGabriel Fernandez 
1561e5e793a6SGabriel Fernandez 	err = clk_stm32_load_vco_config_fdt(fdt, subnode_vco, &pll->vco);
1562e5e793a6SGabriel Fernandez 	if (err != 0)
1563e5e793a6SGabriel Fernandez 		return err;
1564e5e793a6SGabriel Fernandez 
1565e5e793a6SGabriel Fernandez 	err = clk_stm32_load_output_config_fdt(fdt, subnode_pll, &pll->output);
1566e5e793a6SGabriel Fernandez 	if (err != 0)
1567e5e793a6SGabriel Fernandez 		return err;
1568e5e793a6SGabriel Fernandez 
1569e5e793a6SGabriel Fernandez 	return 0;
1570e5e793a6SGabriel Fernandez }
1571e5e793a6SGabriel Fernandez 
stm32_clk_parse_fdt_all_pll(const void * fdt,int node,struct stm32_clk_platdata * pdata)1572e5e793a6SGabriel Fernandez static int stm32_clk_parse_fdt_all_pll(const void *fdt, int node,
1573e5e793a6SGabriel Fernandez 				       struct stm32_clk_platdata *pdata)
1574e5e793a6SGabriel Fernandez {
1575e5e793a6SGabriel Fernandez 	size_t i = 0;
1576e5e793a6SGabriel Fernandez 
1577e5e793a6SGabriel Fernandez 	for (i = PLL1_ID; i < pdata->npll; i++) {
1578e5e793a6SGabriel Fernandez 		struct stm32_pll_dt_cfg *pll = pdata->pll + i;
1579e5e793a6SGabriel Fernandez 		char name[RCC_PLL_NAME_SIZE] = { 0 };
1580e5e793a6SGabriel Fernandez 		int subnode = 0;
1581e5e793a6SGabriel Fernandez 		int err = 0;
1582e5e793a6SGabriel Fernandez 
1583e5e793a6SGabriel Fernandez 		snprintf(name, sizeof(name), "st,pll@%d", i);
1584e5e793a6SGabriel Fernandez 
1585e5e793a6SGabriel Fernandez 		subnode = fdt_subnode_offset(fdt, node, name);
1586e5e793a6SGabriel Fernandez 		if (subnode < 0)
1587e5e793a6SGabriel Fernandez 			continue;
1588e5e793a6SGabriel Fernandez 
1589e5e793a6SGabriel Fernandez 		err = clk_stm32_parse_pll_fdt(fdt, subnode, pll);
1590e5e793a6SGabriel Fernandez 		if (err != 0)
1591e5e793a6SGabriel Fernandez 			panic();
1592e5e793a6SGabriel Fernandez 	}
1593e5e793a6SGabriel Fernandez 
1594e5e793a6SGabriel Fernandez 	return 0;
1595e5e793a6SGabriel Fernandez }
1596e5e793a6SGabriel Fernandez 
stm32_clk_parse_fdt_opp(const void * fdt,int node,const char * opp_name,struct stm32_clk_opp_cfg * opp_cfg)1597e5e793a6SGabriel Fernandez static int stm32_clk_parse_fdt_opp(const void *fdt, int node,
1598e5e793a6SGabriel Fernandez 				   const char *opp_name,
1599e5e793a6SGabriel Fernandez 				   struct stm32_clk_opp_cfg *opp_cfg)
1600e5e793a6SGabriel Fernandez {
1601e5e793a6SGabriel Fernandez 	int subnode = 0;
1602e5e793a6SGabriel Fernandez 	int nb_opp = 0;
1603e5e793a6SGabriel Fernandez 	int ret = 0;
1604e5e793a6SGabriel Fernandez 
1605e5e793a6SGabriel Fernandez 	node = fdt_subnode_offset(fdt, node, opp_name);
1606e5e793a6SGabriel Fernandez 	if (node == -FDT_ERR_NOTFOUND)
1607e5e793a6SGabriel Fernandez 		return 0;
1608e5e793a6SGabriel Fernandez 	if (node < 0)
1609e5e793a6SGabriel Fernandez 		return node;
1610e5e793a6SGabriel Fernandez 
1611e5e793a6SGabriel Fernandez 	fdt_for_each_subnode(subnode, fdt, node) {
1612e5e793a6SGabriel Fernandez 		if (nb_opp >= MAX_OPP) {
1613e5e793a6SGabriel Fernandez 			EMSG("%d MAX opp in %s", MAX_OPP, opp_name);
1614e5e793a6SGabriel Fernandez 			panic();
1615e5e793a6SGabriel Fernandez 		}
1616e5e793a6SGabriel Fernandez 
1617f354a5d8SGatien Chevallier 		opp_cfg->frq = fdt_read_uint32_default(fdt, subnode,
1618e5e793a6SGabriel Fernandez 						       "hz",
1619e5e793a6SGabriel Fernandez 						       UINT32_MAX);
1620e5e793a6SGabriel Fernandez 
1621f354a5d8SGatien Chevallier 		opp_cfg->src = fdt_read_uint32_default(fdt, subnode,
1622e5e793a6SGabriel Fernandez 						       "st,clksrc",
1623e5e793a6SGabriel Fernandez 						       UINT32_MAX);
1624e5e793a6SGabriel Fernandez 
1625f354a5d8SGatien Chevallier 		opp_cfg->div = fdt_read_uint32_default(fdt, subnode,
1626e5e793a6SGabriel Fernandez 						       "st,clkdiv",
1627e5e793a6SGabriel Fernandez 						       UINT32_MAX);
1628e5e793a6SGabriel Fernandez 
1629e5e793a6SGabriel Fernandez 		ret = clk_stm32_parse_pll_fdt(fdt, subnode, &opp_cfg->pll_cfg);
1630e5e793a6SGabriel Fernandez 		if (ret)
1631e5e793a6SGabriel Fernandez 			return ret;
1632e5e793a6SGabriel Fernandez 
1633e5e793a6SGabriel Fernandez 		opp_cfg++;
1634e5e793a6SGabriel Fernandez 		nb_opp++;
1635e5e793a6SGabriel Fernandez 	}
1636e5e793a6SGabriel Fernandez 
1637e5e793a6SGabriel Fernandez 	return 0;
1638e5e793a6SGabriel Fernandez }
1639e5e793a6SGabriel Fernandez 
stm32_clk_parse_fdt_all_opp(const void * fdt,int node,struct stm32_clk_platdata * pdata)1640e5e793a6SGabriel Fernandez static int stm32_clk_parse_fdt_all_opp(const void *fdt, int node,
1641e5e793a6SGabriel Fernandez 				       struct stm32_clk_platdata *pdata)
1642e5e793a6SGabriel Fernandez {
1643e5e793a6SGabriel Fernandez 	struct stm32_clk_opp_dt_cfg *opp = pdata->opp;
1644e5e793a6SGabriel Fernandez 	int ret = 0;
1645e5e793a6SGabriel Fernandez 
1646e5e793a6SGabriel Fernandez 	node = fdt_subnode_offset(fdt, node, "st,clk_opp");
1647e5e793a6SGabriel Fernandez 	/* No opp are defined */
1648e5e793a6SGabriel Fernandez 	if (node == -FDT_ERR_NOTFOUND)
1649e5e793a6SGabriel Fernandez 		return 0;
1650e5e793a6SGabriel Fernandez 	if (node < 0)
1651e5e793a6SGabriel Fernandez 		return node;
1652e5e793a6SGabriel Fernandez 
1653e5e793a6SGabriel Fernandez 	ret = stm32_clk_parse_fdt_opp(fdt, node, "st,ck_mpu", opp->mpu_opp);
1654e5e793a6SGabriel Fernandez 	if (ret)
1655e5e793a6SGabriel Fernandez 		return ret;
1656e5e793a6SGabriel Fernandez 
1657e5e793a6SGabriel Fernandez 	ret = stm32_clk_parse_fdt_opp(fdt, node, "st,ck_axi", opp->axi_opp);
1658e5e793a6SGabriel Fernandez 	if (ret)
1659e5e793a6SGabriel Fernandez 		return ret;
1660e5e793a6SGabriel Fernandez 
1661e5e793a6SGabriel Fernandez 	ret = stm32_clk_parse_fdt_opp(fdt, node, "st,ck_mlahbs",
1662e5e793a6SGabriel Fernandez 				      opp->mlahbs_opp);
1663e5e793a6SGabriel Fernandez 	if (ret)
1664e5e793a6SGabriel Fernandez 		return ret;
1665e5e793a6SGabriel Fernandez 
1666e5e793a6SGabriel Fernandez 	return 0;
1667e5e793a6SGabriel Fernandez }
1668e5e793a6SGabriel Fernandez 
stm32_clk_parse_fdt(const void * fdt,int node,struct stm32_clk_platdata * pdata)1669e5e793a6SGabriel Fernandez static int stm32_clk_parse_fdt(const void *fdt, int node,
1670e5e793a6SGabriel Fernandez 			       struct stm32_clk_platdata *pdata)
1671e5e793a6SGabriel Fernandez {
1672e5e793a6SGabriel Fernandez 	int err = 0;
1673e5e793a6SGabriel Fernandez 
1674e5e793a6SGabriel Fernandez 	err = stm32_clk_parse_fdt_all_oscillator(fdt, node, pdata);
1675e5e793a6SGabriel Fernandez 	if (err != 0)
1676e5e793a6SGabriel Fernandez 		return err;
1677e5e793a6SGabriel Fernandez 
1678e5e793a6SGabriel Fernandez 	err = stm32_clk_parse_fdt_all_pll(fdt, node, pdata);
1679e5e793a6SGabriel Fernandez 	if (err != 0)
1680e5e793a6SGabriel Fernandez 		return err;
1681e5e793a6SGabriel Fernandez 
1682e5e793a6SGabriel Fernandez 	err = stm32_clk_parse_fdt_all_opp(fdt, node, pdata);
1683e5e793a6SGabriel Fernandez 	if (err != 0)
1684e5e793a6SGabriel Fernandez 		return err;
1685e5e793a6SGabriel Fernandez 
1686e5e793a6SGabriel Fernandez 	err = clk_stm32_parse_fdt_by_name(fdt, node, "st,clkdiv", pdata->clkdiv,
1687e5e793a6SGabriel Fernandez 					  &pdata->nclkdiv);
1688e5e793a6SGabriel Fernandez 	if (err != 0)
1689e5e793a6SGabriel Fernandez 		return err;
1690e5e793a6SGabriel Fernandez 
1691e5e793a6SGabriel Fernandez 	err = clk_stm32_parse_fdt_by_name(fdt, node, "st,clksrc", pdata->clksrc,
1692e5e793a6SGabriel Fernandez 					  &pdata->nclksrc);
1693e5e793a6SGabriel Fernandez 	if (err != 0)
1694e5e793a6SGabriel Fernandez 		return err;
1695e5e793a6SGabriel Fernandez 
1696e5e793a6SGabriel Fernandez 	return 0;
1697e5e793a6SGabriel Fernandez }
1698e5e793a6SGabriel Fernandez 
16995436921fSGabriel Fernandez struct clk_stm32_pll_cfg {
17005436921fSGabriel Fernandez 	uint32_t reg_pllxcr;
17015436921fSGabriel Fernandez 	int gate_id;
17025436921fSGabriel Fernandez 	int mux_id;
17035436921fSGabriel Fernandez };
17045436921fSGabriel Fernandez 
clk_stm32_pll_get_parent(struct clk * clk)17055436921fSGabriel Fernandez static size_t clk_stm32_pll_get_parent(struct clk *clk)
17065436921fSGabriel Fernandez {
17075436921fSGabriel Fernandez 	struct clk_stm32_pll_cfg *cfg = clk->priv;
17085436921fSGabriel Fernandez 
17095436921fSGabriel Fernandez 	return stm32_mux_get_parent(cfg->mux_id);
17105436921fSGabriel Fernandez }
17115436921fSGabriel Fernandez 
clk_stm32_pll_get_rate(struct clk * clk,unsigned long prate)17125436921fSGabriel Fernandez static unsigned long clk_stm32_pll_get_rate(struct clk *clk,
17135436921fSGabriel Fernandez 					    unsigned long prate)
17145436921fSGabriel Fernandez {
17155436921fSGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
17165436921fSGabriel Fernandez 	struct clk_stm32_pll_cfg *cfg = clk->priv;
17175436921fSGabriel Fernandez 	uintptr_t pll_base = priv->base + cfg->reg_pllxcr;
17185436921fSGabriel Fernandez 	uint32_t cfgr1 = 0;
17195436921fSGabriel Fernandez 	uint32_t fracr = 0;
17205436921fSGabriel Fernandez 	uint32_t divm = 0;
17215436921fSGabriel Fernandez 	uint32_t divn = 0;
17225436921fSGabriel Fernandez 	unsigned long fvco = 0UL;
17235436921fSGabriel Fernandez 
17245436921fSGabriel Fernandez 	cfgr1 = io_read32(pll_base + RCC_OFFSET_PLLXCFGR1);
17255436921fSGabriel Fernandez 	fracr = io_read32(pll_base + RCC_OFFSET_PLLXFRACR);
17265436921fSGabriel Fernandez 
17275436921fSGabriel Fernandez 	divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
17285436921fSGabriel Fernandez 	divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
17295436921fSGabriel Fernandez 
17305436921fSGabriel Fernandez 	/*
17315436921fSGabriel Fernandez 	 * With FRACV :
17325436921fSGabriel Fernandez 	 *   Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
17335436921fSGabriel Fernandez 	 * Without FRACV
17345436921fSGabriel Fernandez 	 *   Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
17355436921fSGabriel Fernandez 	 */
17365436921fSGabriel Fernandez 	if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
17375436921fSGabriel Fernandez 		uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
17385436921fSGabriel Fernandez 				 RCC_PLLNFRACR_FRACV_SHIFT;
17395436921fSGabriel Fernandez 		unsigned long long numerator = 0UL;
17405436921fSGabriel Fernandez 		unsigned long long denominator = 0UL;
17415436921fSGabriel Fernandez 
17425436921fSGabriel Fernandez 		numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
17435436921fSGabriel Fernandez 		numerator = prate * numerator;
17445436921fSGabriel Fernandez 		denominator = ((unsigned long long)divm + 1U) << 13;
17455436921fSGabriel Fernandez 		fvco = (unsigned long)(numerator / denominator);
17465436921fSGabriel Fernandez 	} else {
17475436921fSGabriel Fernandez 		fvco = (unsigned long)(prate * (divn + 1U) / (divm + 1U));
17485436921fSGabriel Fernandez 	}
17495436921fSGabriel Fernandez 
1750da1a293eSEtienne Carriere 	return UDIV_ROUND_NEAREST(fvco, 100000) * 100000;
17515436921fSGabriel Fernandez };
17525436921fSGabriel Fernandez 
clk_stm32_pll_is_enabled(struct clk * clk)17535436921fSGabriel Fernandez static bool clk_stm32_pll_is_enabled(struct clk *clk)
17545436921fSGabriel Fernandez {
17555436921fSGabriel Fernandez 	struct clk_stm32_pll_cfg *cfg = clk->priv;
17565436921fSGabriel Fernandez 
17575436921fSGabriel Fernandez 	return stm32_gate_is_enabled(cfg->gate_id);
17585436921fSGabriel Fernandez }
17595436921fSGabriel Fernandez 
clk_stm32_pll_enable(struct clk * clk)17605436921fSGabriel Fernandez static TEE_Result clk_stm32_pll_enable(struct clk *clk)
17615436921fSGabriel Fernandez {
17625436921fSGabriel Fernandez 	struct clk_stm32_pll_cfg *cfg = clk->priv;
17635436921fSGabriel Fernandez 
17645436921fSGabriel Fernandez 	if (clk_stm32_pll_is_enabled(clk))
17655436921fSGabriel Fernandez 		return TEE_SUCCESS;
17665436921fSGabriel Fernandez 
17675436921fSGabriel Fernandez 	return stm32_gate_rdy_enable(cfg->gate_id);
17685436921fSGabriel Fernandez }
17695436921fSGabriel Fernandez 
clk_stm32_pll_disable(struct clk * clk)17705436921fSGabriel Fernandez static void clk_stm32_pll_disable(struct clk *clk)
17715436921fSGabriel Fernandez {
17725436921fSGabriel Fernandez 	struct clk_stm32_pll_cfg *cfg = clk->priv;
17735436921fSGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
17745436921fSGabriel Fernandez 	uintptr_t pll_base = priv->base + cfg->reg_pllxcr;
17755436921fSGabriel Fernandez 
17765436921fSGabriel Fernandez 	if (!clk_stm32_pll_is_enabled(clk))
17775436921fSGabriel Fernandez 		return;
17785436921fSGabriel Fernandez 
17795436921fSGabriel Fernandez 	/* Stop all output */
17805436921fSGabriel Fernandez 	io_clrbits32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
17815436921fSGabriel Fernandez 		     RCC_PLLNCR_DIVREN);
17825436921fSGabriel Fernandez 
17835436921fSGabriel Fernandez 	stm32_gate_rdy_disable(cfg->gate_id);
17845436921fSGabriel Fernandez }
17855436921fSGabriel Fernandez 
17865436921fSGabriel Fernandez static const struct clk_ops clk_stm32_pll_ops = {
17875436921fSGabriel Fernandez 	.get_parent	= clk_stm32_pll_get_parent,
17885436921fSGabriel Fernandez 	.get_rate	= clk_stm32_pll_get_rate,
17895436921fSGabriel Fernandez 	.enable		= clk_stm32_pll_enable,
17905436921fSGabriel Fernandez 	.disable	= clk_stm32_pll_disable,
17915436921fSGabriel Fernandez };
17925436921fSGabriel Fernandez 
1793821cb656SEtienne Carriere static TEE_Result
clk_stm32_composite_get_duty_cycle(struct clk * clk,struct clk_duty_cycle * duty_cycle)1794821cb656SEtienne Carriere clk_stm32_composite_get_duty_cycle(struct clk *clk,
1795821cb656SEtienne Carriere 				   struct clk_duty_cycle *duty_cycle)
1796821cb656SEtienne Carriere {
1797821cb656SEtienne Carriere 	struct clk_stm32_composite_cfg *cfg = clk->priv;
1798821cb656SEtienne Carriere 	uint32_t val = stm32_div_get_value(cfg->div_id);
1799821cb656SEtienne Carriere 
1800821cb656SEtienne Carriere 	duty_cycle->num = (val + 1) / 2;
1801821cb656SEtienne Carriere 	duty_cycle->den = val + 1;
1802821cb656SEtienne Carriere 
1803821cb656SEtienne Carriere 	return TEE_SUCCESS;
1804821cb656SEtienne Carriere }
1805821cb656SEtienne Carriere 
1806821cb656SEtienne Carriere static const struct clk_ops clk_stm32_composite_duty_cycle_ops = {
1807821cb656SEtienne Carriere 	.get_parent	= clk_stm32_composite_get_parent,
1808821cb656SEtienne Carriere 	.set_parent	= clk_stm32_composite_set_parent,
1809821cb656SEtienne Carriere 	.get_rate	= clk_stm32_composite_get_rate,
1810821cb656SEtienne Carriere 	.set_rate	= clk_stm32_composite_set_rate,
1811821cb656SEtienne Carriere 	.enable		= clk_stm32_composite_gate_enable,
1812821cb656SEtienne Carriere 	.disable	= clk_stm32_composite_gate_disable,
1813821cb656SEtienne Carriere 	.get_duty_cycle	= clk_stm32_composite_get_duty_cycle,
1814821cb656SEtienne Carriere };
1815821cb656SEtienne Carriere 
18165436921fSGabriel Fernandez static struct
clk_stm32_get_opp_config(struct stm32_clk_opp_cfg * opp_cfg,unsigned long rate)18175436921fSGabriel Fernandez stm32_clk_opp_cfg *clk_stm32_get_opp_config(struct stm32_clk_opp_cfg *opp_cfg,
18185436921fSGabriel Fernandez 					    unsigned long rate)
18195436921fSGabriel Fernandez {
18205436921fSGabriel Fernandez 	unsigned int i = 0;
18215436921fSGabriel Fernandez 
18225436921fSGabriel Fernandez 	for (i = 0; i < MAX_OPP; i++, opp_cfg++) {
18235436921fSGabriel Fernandez 		if (opp_cfg->frq == 0UL)
18245436921fSGabriel Fernandez 			break;
18255436921fSGabriel Fernandez 
18265436921fSGabriel Fernandez 		if (opp_cfg->frq == rate)
18275436921fSGabriel Fernandez 			return opp_cfg;
18285436921fSGabriel Fernandez 	}
18295436921fSGabriel Fernandez 
18305436921fSGabriel Fernandez 	return NULL;
18315436921fSGabriel Fernandez }
18325436921fSGabriel Fernandez 
clk_stm32_pll1_set_rate(struct clk * clk __maybe_unused,unsigned long rate,unsigned long prate __maybe_unused)18335436921fSGabriel Fernandez static TEE_Result clk_stm32_pll1_set_rate(struct clk *clk __maybe_unused,
18345436921fSGabriel Fernandez 					  unsigned long rate,
18355436921fSGabriel Fernandez 					  unsigned long prate __maybe_unused)
18365436921fSGabriel Fernandez {
18375436921fSGabriel Fernandez 	const struct stm32_clk_pll *pll = clk_stm32_pll_data(PLL1_ID);
18385436921fSGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
18395436921fSGabriel Fernandez 	struct stm32_clk_platdata *pdata = priv->pdata;
18405436921fSGabriel Fernandez 	struct stm32_pll_dt_cfg *pll_conf = NULL;
18415436921fSGabriel Fernandez 	struct stm32_clk_opp_cfg *opp = NULL;
18425436921fSGabriel Fernandez 	int config_on_the_fly = -1;
18435436921fSGabriel Fernandez 	int err = 0;
18445436921fSGabriel Fernandez 	size_t sel = stm32_mux_get_parent(MUX_MPU);
18455436921fSGabriel Fernandez 
18465436921fSGabriel Fernandez 	opp = clk_stm32_get_opp_config(pdata->opp->mpu_opp, rate);
18475436921fSGabriel Fernandez 	if (!opp)
18485436921fSGabriel Fernandez 		return TEE_ERROR_GENERIC;
18495436921fSGabriel Fernandez 
18505436921fSGabriel Fernandez 	pll_conf = &opp->pll_cfg;
18515436921fSGabriel Fernandez 
18525436921fSGabriel Fernandez 	err = clk_stm32_is_pll_config_on_the_fly(priv, pll, pll_conf,
18535436921fSGabriel Fernandez 						 &config_on_the_fly);
18545436921fSGabriel Fernandez 	if (err)
18555436921fSGabriel Fernandez 		return TEE_ERROR_GENERIC;
18565436921fSGabriel Fernandez 
18575436921fSGabriel Fernandez 	if (config_on_the_fly == 1)
18585436921fSGabriel Fernandez 		return TEE_SUCCESS;
18595436921fSGabriel Fernandez 
18605436921fSGabriel Fernandez 	if (config_on_the_fly == -1) {
18615436921fSGabriel Fernandez 		/* Switch to HSI and stop PLL1 before reconfiguration */
18625436921fSGabriel Fernandez 		if (stm32_mux_set_parent(MUX_MPU, 0))
18635436921fSGabriel Fernandez 			return TEE_ERROR_GENERIC;
18645436921fSGabriel Fernandez 
18655436921fSGabriel Fernandez 		stm32_gate_disable(GATE_PLL1_DIVP);
18665436921fSGabriel Fernandez 		stm32_gate_rdy_disable(GATE_PLL1);
18675436921fSGabriel Fernandez 		clk_stm32_pll_config_vco(priv, pll, &pll_conf->vco);
18685436921fSGabriel Fernandez 	}
18695436921fSGabriel Fernandez 
18705436921fSGabriel Fernandez 	clk_stm32_pll_config_out(priv, pll, &pll_conf->output);
18715436921fSGabriel Fernandez 	if (stm32_gate_rdy_enable(GATE_PLL1)) {
18725436921fSGabriel Fernandez 		EMSG("timeout to enable PLL1 clock");
18735436921fSGabriel Fernandez 		panic();
18745436921fSGabriel Fernandez 	}
18755436921fSGabriel Fernandez 	stm32_gate_enable(GATE_PLL1_DIVP);
18765436921fSGabriel Fernandez 
18775436921fSGabriel Fernandez 	/* Restore MPU source */
18785436921fSGabriel Fernandez 	if (stm32_mux_set_parent(MUX_MPU, sel))
18795436921fSGabriel Fernandez 		return TEE_ERROR_GENERIC;
18805436921fSGabriel Fernandez 
18815436921fSGabriel Fernandez 	return TEE_SUCCESS;
18825436921fSGabriel Fernandez }
18835436921fSGabriel Fernandez 
18845436921fSGabriel Fernandez static const struct clk_ops clk_stm32_pll1_ops = {
18855436921fSGabriel Fernandez 	.set_rate	= clk_stm32_pll1_set_rate,
18865436921fSGabriel Fernandez 	.get_parent	= clk_stm32_pll_get_parent,
18875436921fSGabriel Fernandez 	.get_rate	= clk_stm32_pll_get_rate,
18885436921fSGabriel Fernandez 	.enable		= clk_stm32_pll_enable,
18895436921fSGabriel Fernandez 	.disable	= clk_stm32_pll_disable,
18905436921fSGabriel Fernandez };
18915436921fSGabriel Fernandez 
18925436921fSGabriel Fernandez static const struct clk_ops clk_stm32_pll1p_ops = {
18935436921fSGabriel Fernandez 	.get_rate	= clk_stm32_composite_get_rate,
18945436921fSGabriel Fernandez 	.enable		= clk_stm32_composite_gate_enable,
18955436921fSGabriel Fernandez 	.disable	= clk_stm32_composite_gate_disable,
18965436921fSGabriel Fernandez };
18975436921fSGabriel Fernandez 
18985436921fSGabriel Fernandez static const struct clk_ops clk_stm32_mpu_ops = {
18995436921fSGabriel Fernandez 	.get_parent	= clk_stm32_composite_get_parent,
19005436921fSGabriel Fernandez 	.set_parent	= clk_stm32_composite_set_parent,
19015436921fSGabriel Fernandez };
19025436921fSGabriel Fernandez 
19035436921fSGabriel Fernandez static const struct clk_ops clk_stm32_axi_ops = {
19045436921fSGabriel Fernandez 	.get_parent	= clk_stm32_composite_get_parent,
19055436921fSGabriel Fernandez 	.set_parent	= clk_stm32_composite_set_parent,
19065436921fSGabriel Fernandez 	.set_rate	= clk_stm32_composite_set_rate,
19075436921fSGabriel Fernandez 	.get_rate	= clk_stm32_composite_get_rate,
19085436921fSGabriel Fernandez };
19095436921fSGabriel Fernandez 
19105436921fSGabriel Fernandez const struct clk_ops clk_stm32_mlahb_ops = {
19115436921fSGabriel Fernandez 	.get_parent	= clk_stm32_composite_get_parent,
19125436921fSGabriel Fernandez 	.set_parent	= clk_stm32_composite_set_parent,
19135436921fSGabriel Fernandez 	.set_rate	= clk_stm32_composite_set_rate,
19145436921fSGabriel Fernandez 	.get_rate	= clk_stm32_composite_get_rate,
19155436921fSGabriel Fernandez };
19165436921fSGabriel Fernandez 
19175436921fSGabriel Fernandez #define APB_DIV_MASK	GENMASK_32(2, 0)
19185436921fSGabriel Fernandez #define TIM_PRE_MASK	BIT(0)
19195436921fSGabriel Fernandez 
ck_timer_get_rate_ops(struct clk * clk,unsigned long prate)19205436921fSGabriel Fernandez static unsigned long ck_timer_get_rate_ops(struct clk *clk, unsigned long prate)
19215436921fSGabriel Fernandez {
19225436921fSGabriel Fernandez 	struct clk_stm32_priv *priv = clk_stm32_get_priv();
19235436921fSGabriel Fernandez 	struct clk_stm32_timer_cfg *cfg = clk->priv;
19245436921fSGabriel Fernandez 	uint32_t prescaler, timpre;
19255436921fSGabriel Fernandez 	uintptr_t rcc_base = priv->base;
19265436921fSGabriel Fernandez 
19275436921fSGabriel Fernandez 	prescaler = io_read32(rcc_base + cfg->apbdiv) & APB_DIV_MASK;
19285436921fSGabriel Fernandez 
19295436921fSGabriel Fernandez 	timpre = io_read32(rcc_base + cfg->timpre) & TIM_PRE_MASK;
19305436921fSGabriel Fernandez 
19315436921fSGabriel Fernandez 	if (prescaler == 0U)
19325436921fSGabriel Fernandez 		return prate;
19335436921fSGabriel Fernandez 
19345436921fSGabriel Fernandez 	return prate * (timpre + 1U) * 2U;
19355436921fSGabriel Fernandez };
19365436921fSGabriel Fernandez 
19375436921fSGabriel Fernandez const struct clk_ops ck_timer_ops = {
19385436921fSGabriel Fernandez 	.get_rate	= ck_timer_get_rate_ops,
19395436921fSGabriel Fernandez };
19405436921fSGabriel Fernandez 
19415436921fSGabriel Fernandez #define STM32_TIMER(_name, _parent, _flags, _apbdiv, _timpre)\
19425436921fSGabriel Fernandez 	struct clk _name = {\
19435436921fSGabriel Fernandez 		.ops	= &ck_timer_ops,\
19445436921fSGabriel Fernandez 		.priv	= &(struct clk_stm32_timer_cfg) {\
19455436921fSGabriel Fernandez 			.apbdiv		= (_apbdiv),\
19465436921fSGabriel Fernandez 			.timpre		= (_timpre),\
19475436921fSGabriel Fernandez 		},\
19485436921fSGabriel Fernandez 		.name		= #_name,\
19495436921fSGabriel Fernandez 		.flags		= (_flags),\
19505436921fSGabriel Fernandez 		.num_parents	= 1,\
19515436921fSGabriel Fernandez 		.parents	= { _parent },\
19525436921fSGabriel Fernandez 	}
19535436921fSGabriel Fernandez 
19545436921fSGabriel Fernandez #define STM32_KCLK(_name, _nb_parents, _parents, _flags, _gate_id, _mux_id)\
19555436921fSGabriel Fernandez 	struct clk _name = {\
19565436921fSGabriel Fernandez 		.ops	= &clk_stm32_composite_ops,\
19575436921fSGabriel Fernandez 		.priv	= &(struct clk_stm32_composite_cfg) {\
19585436921fSGabriel Fernandez 			.gate_id	= (_gate_id),\
19595436921fSGabriel Fernandez 			.div_id		= (NO_DIV),\
19605436921fSGabriel Fernandez 			.mux_id		= (_mux_id),\
19615436921fSGabriel Fernandez 		},\
19625436921fSGabriel Fernandez 		.name		= #_name,\
19635436921fSGabriel Fernandez 		.flags		= (_flags),\
19645436921fSGabriel Fernandez 		.num_parents	= (_nb_parents),\
19655436921fSGabriel Fernandez 		.parents	=  _parents,\
19665436921fSGabriel Fernandez 	}
19675436921fSGabriel Fernandez 
19685436921fSGabriel Fernandez #define STM32_PLL_VCO(_name, _nb_parents, _parents, _flags, _reg,\
19695436921fSGabriel Fernandez 		      _gate_id, _mux_id)\
19705436921fSGabriel Fernandez 	struct clk _name = {\
19715436921fSGabriel Fernandez 		.ops	= &clk_stm32_pll_ops,\
19725436921fSGabriel Fernandez 		.priv	= &(struct clk_stm32_pll_cfg) {\
19735436921fSGabriel Fernandez 			.reg_pllxcr	= (_reg),\
19745436921fSGabriel Fernandez 			.gate_id	= (_gate_id),\
19755436921fSGabriel Fernandez 			.mux_id		= (_mux_id),\
19765436921fSGabriel Fernandez 		},\
19775436921fSGabriel Fernandez 		.name		= #_name,\
19785436921fSGabriel Fernandez 		.flags		= (_flags),\
19795436921fSGabriel Fernandez 		.num_parents	= (_nb_parents),\
19805436921fSGabriel Fernandez 		.parents	=  _parents,\
19815436921fSGabriel Fernandez 	}
19825436921fSGabriel Fernandez 
19835436921fSGabriel Fernandez #define STM32_PLL_OUPUT(_name, _nb_parents, _parents, _flags,\
19845436921fSGabriel Fernandez 			_gate_id, _div_id, _mux_id)\
19855436921fSGabriel Fernandez 	struct clk _name = {\
1986821cb656SEtienne Carriere 		.ops	= &clk_stm32_composite_duty_cycle_ops,\
19875436921fSGabriel Fernandez 		.priv	= &(struct clk_stm32_composite_cfg) {\
19885436921fSGabriel Fernandez 			.gate_id	= (_gate_id),\
19895436921fSGabriel Fernandez 			.div_id		= (_div_id),\
19905436921fSGabriel Fernandez 			.mux_id		= (_mux_id),\
19915436921fSGabriel Fernandez 		},\
19925436921fSGabriel Fernandez 		.name		= #_name,\
19935436921fSGabriel Fernandez 		.flags		= (_flags),\
19945436921fSGabriel Fernandez 		.num_parents	= (_nb_parents),\
19955436921fSGabriel Fernandez 		.parents	=  _parents,\
19965436921fSGabriel Fernandez 	}
19975436921fSGabriel Fernandez 
19985436921fSGabriel Fernandez /* Oscillator clocks */
199995f2142bSEtienne Carriere 
clk_stm32_oscillator_enable(struct clk * clk)200095f2142bSEtienne Carriere static TEE_Result clk_stm32_oscillator_enable(struct clk *clk)
200195f2142bSEtienne Carriere {
200295f2142bSEtienne Carriere 	struct clk_stm32_gate_cfg *cfg = clk->priv;
200395f2142bSEtienne Carriere 
200495f2142bSEtienne Carriere 	if (clk->rate == 0U)
200595f2142bSEtienne Carriere 		return TEE_SUCCESS;
200695f2142bSEtienne Carriere 
200795f2142bSEtienne Carriere 	return stm32_gate_rdy_enable(cfg->gate_id);
200895f2142bSEtienne Carriere }
200995f2142bSEtienne Carriere 
clk_stm32_oscillator_disable(struct clk * clk)201095f2142bSEtienne Carriere static void clk_stm32_oscillator_disable(struct clk *clk)
201195f2142bSEtienne Carriere {
201295f2142bSEtienne Carriere 	struct clk_stm32_gate_cfg *cfg = clk->priv;
201395f2142bSEtienne Carriere 
201495f2142bSEtienne Carriere 	if (clk->rate == 0U)
201595f2142bSEtienne Carriere 		return;
201695f2142bSEtienne Carriere 
201795f2142bSEtienne Carriere 	if (stm32_gate_rdy_disable(cfg->gate_id))
201895f2142bSEtienne Carriere 		panic();
201995f2142bSEtienne Carriere }
202095f2142bSEtienne Carriere 
202195f2142bSEtienne Carriere static const struct clk_ops clk_stm32_oscillator_ops = {
202295f2142bSEtienne Carriere 	.enable		= clk_stm32_oscillator_enable,
202395f2142bSEtienne Carriere 	.disable	= clk_stm32_oscillator_disable,
202495f2142bSEtienne Carriere };
202595f2142bSEtienne Carriere 
2026a3552708SEtienne Carriere /*
2027a3552708SEtienne Carriere  * Each oscillator has 1 parent which reference is NULL here
2028a3552708SEtienne Carriere  * but set during initialization.
2029a3552708SEtienne Carriere  */
203095f2142bSEtienne Carriere #define STM32_OSCILLATOR(_name, _gate_id)\
203195f2142bSEtienne Carriere 	struct clk _name = {\
203295f2142bSEtienne Carriere 		.ops = &clk_stm32_oscillator_ops,\
203395f2142bSEtienne Carriere 		.priv = &(struct clk_stm32_gate_cfg) {\
203495f2142bSEtienne Carriere 			.gate_id = (_gate_id),\
203595f2142bSEtienne Carriere 		},\
203695f2142bSEtienne Carriere 		.name = #_name,\
2037a3552708SEtienne Carriere 		.num_parents = 1, \
2038a3552708SEtienne Carriere 		.parents = { NULL }, \
203995f2142bSEtienne Carriere 	}
204095f2142bSEtienne Carriere 
204195f2142bSEtienne Carriere static STM32_OSCILLATOR(ck_hsi, GATE_HSI);
204295f2142bSEtienne Carriere static STM32_OSCILLATOR(ck_hse, GATE_HSE);
204395f2142bSEtienne Carriere static STM32_OSCILLATOR(ck_csi, GATE_CSI);
204495f2142bSEtienne Carriere static STM32_OSCILLATOR(ck_lsi, GATE_LSI);
204595f2142bSEtienne Carriere static STM32_OSCILLATOR(ck_lse, GATE_LSE);
20465436921fSGabriel Fernandez 
20475436921fSGabriel Fernandez static STM32_FIXED_FACTOR(ck_i2sckin, NULL, 0, 1, 1);
20485436921fSGabriel Fernandez static STM32_FIXED_FACTOR(ck_hse_div2, &ck_hse, 0, 1, 2);
20495436921fSGabriel Fernandez 
20505436921fSGabriel Fernandez static STM32_FIXED_RATE(ck_off, 0UL);
20515436921fSGabriel Fernandez static STM32_FIXED_RATE(ck_usb_phy_48Mhz, USB_PHY_48_MHZ);
20525436921fSGabriel Fernandez 
20535436921fSGabriel Fernandez /* PLL1 clocks */
20545436921fSGabriel Fernandez static struct clk ck_pll1_vco = {
20555436921fSGabriel Fernandez 	.ops	= &clk_stm32_pll1_ops,
20565436921fSGabriel Fernandez 	.priv		= &(struct clk_stm32_pll_cfg) {
20575436921fSGabriel Fernandez 		.reg_pllxcr	= RCC_PLL1CR,
20585436921fSGabriel Fernandez 		.gate_id	= GATE_PLL1,
20595436921fSGabriel Fernandez 		.mux_id		= MUX_PLL12,
20605436921fSGabriel Fernandez 	},
20615436921fSGabriel Fernandez 	.name		= "ck_pll1_vco",
20625436921fSGabriel Fernandez 	.flags		= 0,
20635436921fSGabriel Fernandez 	.num_parents	= 2,
20645436921fSGabriel Fernandez 	.parents	= { &ck_hsi, &ck_hse },
20655436921fSGabriel Fernandez };
20665436921fSGabriel Fernandez 
20675436921fSGabriel Fernandez static struct clk ck_pll1p = {
20685436921fSGabriel Fernandez 	.ops	= &clk_stm32_pll1p_ops,
20695436921fSGabriel Fernandez 	.priv		= &(struct clk_stm32_composite_cfg) {
20705436921fSGabriel Fernandez 		.gate_id	= GATE_PLL1_DIVP,
20715436921fSGabriel Fernandez 		.div_id		= DIV_PLL1DIVP,
20725436921fSGabriel Fernandez 		.mux_id		= NO_MUX,
20735436921fSGabriel Fernandez 	},
20745436921fSGabriel Fernandez 	.name		= "ck_pll1p",
20751bc6d1bcSEtienne Carriere 	.flags		= CLK_SET_RATE_PARENT,
20765436921fSGabriel Fernandez 	.num_parents	= 1,
20775436921fSGabriel Fernandez 	.parents	= { &ck_pll1_vco },
20785436921fSGabriel Fernandez };
20795436921fSGabriel Fernandez 
20805436921fSGabriel Fernandez const struct clk_ops clk_stm32_pll1p_div_ops = {
20815436921fSGabriel Fernandez 	.get_rate	= clk_stm32_divider_get_rate,
20825436921fSGabriel Fernandez };
20835436921fSGabriel Fernandez 
20845436921fSGabriel Fernandez static struct clk ck_pll1p_div = {
20855436921fSGabriel Fernandez 	.ops	= &clk_stm32_pll1p_div_ops,
20865436921fSGabriel Fernandez 	.priv		= &(struct clk_stm32_div_cfg) {
20875436921fSGabriel Fernandez 		.div_id	= DIV_MPU,
20885436921fSGabriel Fernandez 	},
20895436921fSGabriel Fernandez 	.name	= "ck_pll1p_div",
20901bc6d1bcSEtienne Carriere 	.flags	= CLK_SET_RATE_PARENT,
20915436921fSGabriel Fernandez 	.num_parents	= 1,
20925436921fSGabriel Fernandez 	.parents	= { &ck_pll1p },
20935436921fSGabriel Fernandez };
20945436921fSGabriel Fernandez 
20955436921fSGabriel Fernandez /* Other PLLs */
20965436921fSGabriel Fernandez static STM32_PLL_VCO(ck_pll2_vco, 2, PARENT(&ck_hsi, &ck_hse),
20975436921fSGabriel Fernandez 		     0, RCC_PLL2CR, GATE_PLL2, MUX_PLL12);
20985436921fSGabriel Fernandez 
20995436921fSGabriel Fernandez static STM32_PLL_VCO(ck_pll3_vco, 3,
21005436921fSGabriel Fernandez 		     PARENT(&ck_hsi, &ck_hse, &ck_csi),
21015436921fSGabriel Fernandez 			    0, RCC_PLL3CR, GATE_PLL3, MUX_PLL3);
21025436921fSGabriel Fernandez 
21035436921fSGabriel Fernandez static STM32_PLL_VCO(ck_pll4_vco, 4,
21045436921fSGabriel Fernandez 		     PARENT(&ck_hsi, &ck_hse, &ck_csi, &ck_i2sckin),
21055436921fSGabriel Fernandez 			    0, RCC_PLL4CR, GATE_PLL4, MUX_PLL4);
21065436921fSGabriel Fernandez 
21075436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll2p, 1, PARENT(&ck_pll2_vco), 0,
21085436921fSGabriel Fernandez 		       GATE_PLL2_DIVP, DIV_PLL2DIVP, NO_MUX);
21095436921fSGabriel Fernandez 
21105436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll2q, 1, PARENT(&ck_pll2_vco), 0,
21115436921fSGabriel Fernandez 		       GATE_PLL2_DIVQ, DIV_PLL2DIVQ, NO_MUX);
21125436921fSGabriel Fernandez 
21135436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll2r, 1, PARENT(&ck_pll2_vco), 0,
21145436921fSGabriel Fernandez 		       GATE_PLL2_DIVR, DIV_PLL2DIVR, NO_MUX);
21155436921fSGabriel Fernandez 
21165436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll3p, 1, PARENT(&ck_pll3_vco), 0,
21175436921fSGabriel Fernandez 		       GATE_PLL3_DIVP, DIV_PLL3DIVP, NO_MUX);
21185436921fSGabriel Fernandez 
21195436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll3q, 1, PARENT(&ck_pll3_vco), 0,
21205436921fSGabriel Fernandez 		       GATE_PLL3_DIVQ, DIV_PLL3DIVQ, NO_MUX);
21215436921fSGabriel Fernandez 
21225436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll3r, 1, PARENT(&ck_pll3_vco), 0,
21235436921fSGabriel Fernandez 		       GATE_PLL3_DIVR, DIV_PLL3DIVR, NO_MUX);
21245436921fSGabriel Fernandez 
21255436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll4p, 1, PARENT(&ck_pll4_vco), 0,
21265436921fSGabriel Fernandez 		       GATE_PLL4_DIVP, DIV_PLL4DIVP, NO_MUX);
21275436921fSGabriel Fernandez 
21285436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll4q, 1, PARENT(&ck_pll4_vco), 0,
21295436921fSGabriel Fernandez 		       GATE_PLL4_DIVQ, DIV_PLL4DIVQ, NO_MUX);
21305436921fSGabriel Fernandez 
21315436921fSGabriel Fernandez static STM32_PLL_OUPUT(ck_pll4r, 1, PARENT(&ck_pll4_vco), 0,
21325436921fSGabriel Fernandez 		       GATE_PLL4_DIVR, DIV_PLL4DIVR, NO_MUX);
21335436921fSGabriel Fernandez 
21345436921fSGabriel Fernandez /* System clocks */
21355436921fSGabriel Fernandez static struct clk ck_mpu = {
21365436921fSGabriel Fernandez 	.ops		= &clk_stm32_mpu_ops,
21375436921fSGabriel Fernandez 	.priv		= &(struct clk_stm32_composite_cfg) {
21385436921fSGabriel Fernandez 		.mux_id	= MUX_MPU,
21395436921fSGabriel Fernandez 	},
21405436921fSGabriel Fernandez 	.name		= "ck_mpu",
21411bc6d1bcSEtienne Carriere 	.flags		= CLK_SET_PARENT_PRE_ENABLE | CLK_SET_RATE_PARENT,
21425436921fSGabriel Fernandez 	.num_parents	= 4,
21435436921fSGabriel Fernandez 	.parents	= { &ck_hsi, &ck_hse, &ck_pll1p, &ck_pll1p_div },
21445436921fSGabriel Fernandez };
21455436921fSGabriel Fernandez 
21465436921fSGabriel Fernandez static struct clk ck_axi = {
21475436921fSGabriel Fernandez 	.ops		= &clk_stm32_axi_ops,
21485436921fSGabriel Fernandez 	.priv		= &(struct clk_stm32_composite_cfg) {
21495436921fSGabriel Fernandez 		.mux_id	= MUX_AXI,
21505436921fSGabriel Fernandez 		.div_id	= DIV_AXI,
21515436921fSGabriel Fernandez 	},
21525436921fSGabriel Fernandez 	.name		= "ck_axi",
21531bc6d1bcSEtienne Carriere 	.flags		= CLK_SET_PARENT_PRE_ENABLE | CLK_SET_RATE_PARENT,
21545436921fSGabriel Fernandez 	.num_parents	= 3,
21555436921fSGabriel Fernandez 	.parents	= { &ck_hsi, &ck_hse, &ck_pll2p },
21565436921fSGabriel Fernandez };
21575436921fSGabriel Fernandez 
21585436921fSGabriel Fernandez static struct clk ck_mlahb = {
21595436921fSGabriel Fernandez 	.ops		= &clk_stm32_mlahb_ops,
21605436921fSGabriel Fernandez 	.priv		= &(struct clk_stm32_composite_cfg) {
21615436921fSGabriel Fernandez 		.mux_id	= MUX_MLAHB,
21625436921fSGabriel Fernandez 		.div_id	= DIV_MLAHB,
21635436921fSGabriel Fernandez 	},
21645436921fSGabriel Fernandez 	.name		= "ck_mlahb",
21651bc6d1bcSEtienne Carriere 	.flags		= CLK_SET_PARENT_PRE_ENABLE | CLK_SET_RATE_PARENT,
21665436921fSGabriel Fernandez 	.num_parents	= 4,
21675436921fSGabriel Fernandez 	.parents	= { &ck_hsi, &ck_hse, &ck_csi, &ck_pll3p },
21685436921fSGabriel Fernandez };
21695436921fSGabriel Fernandez 
21705436921fSGabriel Fernandez static STM32_MUX(ck_per, 4, PARENT(&ck_hsi, &ck_csi, &ck_hse, &ck_off),
21715436921fSGabriel Fernandez 		 0, MUX_CKPER);
21725436921fSGabriel Fernandez 
21735436921fSGabriel Fernandez /* Bus clocks */
21745436921fSGabriel Fernandez static STM32_DIVIDER(ck_pclk1, &ck_mlahb, 0, DIV_APB1);
21755436921fSGabriel Fernandez static STM32_DIVIDER(ck_pclk2, &ck_mlahb, 0, DIV_APB2);
21765436921fSGabriel Fernandez static STM32_DIVIDER(ck_pclk3, &ck_mlahb, 0, DIV_APB3);
21775436921fSGabriel Fernandez static STM32_DIVIDER(ck_pclk4, &ck_axi, 0, DIV_APB4);
21785436921fSGabriel Fernandez static STM32_DIVIDER(ck_pclk5, &ck_axi, 0, DIV_APB5);
21795436921fSGabriel Fernandez static STM32_DIVIDER(ck_pclk6, &ck_mlahb, 0, DIV_APB6);
21805436921fSGabriel Fernandez 
21815436921fSGabriel Fernandez /* Timer Clocks */
21825436921fSGabriel Fernandez static STM32_TIMER(ck_timg1, &ck_pclk1, 0, RCC_APB1DIVR, RCC_TIMG1PRER);
21835436921fSGabriel Fernandez static STM32_TIMER(ck_timg2, &ck_pclk2, 0, RCC_APB2DIVR, RCC_TIMG2PRER);
21845436921fSGabriel Fernandez static STM32_TIMER(ck_timg3, &ck_pclk6, 0, RCC_APB6DIVR, RCC_TIMG3PRER);
21855436921fSGabriel Fernandez 
21865436921fSGabriel Fernandez /* Peripheral and Kernel Clocks */
21875436921fSGabriel Fernandez static STM32_GATE(ck_ddrc1, &ck_axi, 0, GATE_DDRC1);
21885436921fSGabriel Fernandez static STM32_GATE(ck_ddrc1lp, &ck_axi, 0, GATE_DDRC1LP);
21895436921fSGabriel Fernandez static STM32_GATE(ck_ddrphyc, &ck_pll2r, 0, GATE_DDRPHYC);
21905436921fSGabriel Fernandez static STM32_GATE(ck_ddrphyclp, &ck_pll2r, 0, GATE_DDRPHYCLP);
21915436921fSGabriel Fernandez static STM32_GATE(ck_ddrcapb, &ck_pclk4, 0, GATE_DDRCAPB);
21925436921fSGabriel Fernandez static STM32_GATE(ck_ddrcapblp, &ck_pclk4, 0, GATE_DDRCAPBLP);
21935436921fSGabriel Fernandez static STM32_GATE(ck_axidcg, &ck_axi, 0, GATE_AXIDCG);
21945436921fSGabriel Fernandez static STM32_GATE(ck_ddrphycapb, &ck_pclk4, 0, 0);
21955436921fSGabriel Fernandez static STM32_GATE(ck_ddrphycapblp, &ck_pclk4, 0, GATE_DDRPHYCAPBLP);
21965436921fSGabriel Fernandez static STM32_GATE(ck_syscfg, &ck_pclk3, 0, GATE_SYSCFG);
21975436921fSGabriel Fernandez static STM32_GATE(ck_ddrperfm, &ck_pclk4, 0, GATE_DDRPERFM);
21985436921fSGabriel Fernandez static STM32_GATE(ck_iwdg2, &ck_pclk4, 0, GATE_IWDG2APB);
21995436921fSGabriel Fernandez static STM32_GATE(ck_rtcapb, &ck_pclk5, 0, GATE_RTCAPB);
22005436921fSGabriel Fernandez static STM32_GATE(ck_tzc, &ck_pclk5, 0, GATE_TZC);
22015436921fSGabriel Fernandez static STM32_GATE(ck_etzpcb, &ck_pclk5, 0, GATE_ETZPC);
22025436921fSGabriel Fernandez static STM32_GATE(ck_iwdg1apb, &ck_pclk5, 0, GATE_IWDG1APB);
22035436921fSGabriel Fernandez static STM32_GATE(ck_bsec, &ck_pclk5, 0, GATE_BSEC);
22045436921fSGabriel Fernandez static STM32_GATE(ck_tim12_k, &ck_timg3, 0, GATE_TIM12);
22055436921fSGabriel Fernandez static STM32_GATE(ck_tim15_k, &ck_timg3, 0, GATE_TIM15);
22065436921fSGabriel Fernandez static STM32_GATE(ck_gpioa, &ck_mlahb, 0, GATE_GPIOA);
22075436921fSGabriel Fernandez static STM32_GATE(ck_gpiob, &ck_mlahb, 0, GATE_GPIOB);
22085436921fSGabriel Fernandez static STM32_GATE(ck_gpioc, &ck_mlahb, 0, GATE_GPIOC);
22095436921fSGabriel Fernandez static STM32_GATE(ck_gpiod, &ck_mlahb, 0, GATE_GPIOD);
22105436921fSGabriel Fernandez static STM32_GATE(ck_gpioe, &ck_mlahb, 0, GATE_GPIOE);
22115436921fSGabriel Fernandez static STM32_GATE(ck_gpiof, &ck_mlahb, 0, GATE_GPIOF);
22125436921fSGabriel Fernandez static STM32_GATE(ck_gpiog, &ck_mlahb, 0, GATE_GPIOG);
22135436921fSGabriel Fernandez static STM32_GATE(ck_gpioh, &ck_mlahb, 0, GATE_GPIOH);
22145436921fSGabriel Fernandez static STM32_GATE(ck_gpioi, &ck_mlahb, 0, GATE_GPIOI);
22155436921fSGabriel Fernandez static STM32_GATE(ck_pka, &ck_axi, 0, GATE_PKA);
22165436921fSGabriel Fernandez static STM32_GATE(ck_cryp1, &ck_pclk5, 0, GATE_CRYP1);
22175436921fSGabriel Fernandez static STM32_GATE(ck_hash1, &ck_pclk5, 0, GATE_HASH1);
22185436921fSGabriel Fernandez static STM32_GATE(ck_bkpsram, &ck_pclk5, 0, GATE_BKPSRAM);
22195436921fSGabriel Fernandez static STM32_GATE(ck_dbg, &ck_axi, 0, GATE_DBGCK);
22205436921fSGabriel Fernandez static STM32_GATE(ck_mce, &ck_axi, 0, GATE_MCE);
22215436921fSGabriel Fernandez static STM32_GATE(ck_tim2_k, &ck_timg1, 0, GATE_TIM2);
22225436921fSGabriel Fernandez static STM32_GATE(ck_tim3_k, &ck_timg1, 0, GATE_TIM3);
22235436921fSGabriel Fernandez static STM32_GATE(ck_tim4_k, &ck_timg1, 0, GATE_TIM4);
22245436921fSGabriel Fernandez static STM32_GATE(ck_tim5_k, &ck_timg1, 0, GATE_TIM5);
22255436921fSGabriel Fernandez static STM32_GATE(ck_tim6_k, &ck_timg1, 0, GATE_TIM6);
22265436921fSGabriel Fernandez static STM32_GATE(ck_tim7_k, &ck_timg1, 0, GATE_TIM7);
22275436921fSGabriel Fernandez static STM32_GATE(ck_tim13_k, &ck_timg3, 0, GATE_TIM13);
22285436921fSGabriel Fernandez static STM32_GATE(ck_tim14_k, &ck_timg3, 0, GATE_TIM14);
22295436921fSGabriel Fernandez static STM32_GATE(ck_tim1_k, &ck_timg2, 0, GATE_TIM1);
22305436921fSGabriel Fernandez static STM32_GATE(ck_tim8_k, &ck_timg2, 0, GATE_TIM8);
22315436921fSGabriel Fernandez static STM32_GATE(ck_tim16_k, &ck_timg3, 0, GATE_TIM16);
22325436921fSGabriel Fernandez static STM32_GATE(ck_tim17_k, &ck_timg3, 0, GATE_TIM17);
22335436921fSGabriel Fernandez static STM32_GATE(ck_ltdc_px, &ck_pll4q, 0, GATE_LTDC);
22345436921fSGabriel Fernandez static STM32_GATE(ck_dma1, &ck_mlahb, 0, GATE_DMA1);
22355436921fSGabriel Fernandez static STM32_GATE(ck_dma2, &ck_mlahb, 0, GATE_DMA2);
2236e84c2998SEtienne Carriere static STM32_GATE(ck_adc1, &ck_mlahb, 0, GATE_ADC1);
2237e84c2998SEtienne Carriere static STM32_GATE(ck_adc2, &ck_mlahb, 0, GATE_ADC2);
22385436921fSGabriel Fernandez static STM32_GATE(ck_mdma, &ck_axi, 0, GATE_MDMA);
22395436921fSGabriel Fernandez static STM32_GATE(ck_eth1mac, &ck_axi, 0, GATE_ETH1MAC);
22405436921fSGabriel Fernandez static STM32_GATE(ck_usbh, &ck_axi, 0, GATE_USBH);
22415436921fSGabriel Fernandez static STM32_GATE(ck_vref, &ck_pclk3, 0, GATE_VREF);
22425436921fSGabriel Fernandez static STM32_GATE(ck_tmpsens, &ck_pclk3, 0, GATE_DTS);
2243f4dba325SEtienne Carriere static STM32_GATE(ck_pmbctrl, &ck_pclk3, 0, GATE_PMBCTRL);
2244f4dba325SEtienne Carriere static STM32_GATE(ck_hdp, &ck_pclk3, 0, GATE_HDP);
2245f4dba325SEtienne Carriere static STM32_GATE(ck_stgenro, &ck_pclk4, 0, GATE_STGENRO);
22465436921fSGabriel Fernandez static STM32_GATE(ck_dmamux1, &ck_axi, 0, GATE_DMAMUX1);
22475436921fSGabriel Fernandez static STM32_GATE(ck_dmamux2, &ck_axi, 0, GATE_DMAMUX2);
2248f4dba325SEtienne Carriere static STM32_GATE(ck_dma3, &ck_axi, 0, GATE_DMA3);
22495436921fSGabriel Fernandez static STM32_GATE(ck_tsc, &ck_axi, 0, GATE_TSC);
22505436921fSGabriel Fernandez static STM32_GATE(ck_aximc, &ck_axi, 0, GATE_AXIMC);
2251f4dba325SEtienne Carriere static STM32_GATE(ck_crc1, &ck_axi, 0, GATE_CRC1);
22525436921fSGabriel Fernandez static STM32_GATE(ck_eth1tx, &ck_axi, 0, GATE_ETH1TX);
22535436921fSGabriel Fernandez static STM32_GATE(ck_eth1rx, &ck_axi, 0, GATE_ETH1RX);
22545436921fSGabriel Fernandez static STM32_GATE(ck_eth2tx, &ck_axi, 0, GATE_ETH2TX);
22555436921fSGabriel Fernandez static STM32_GATE(ck_eth2rx, &ck_axi, 0, GATE_ETH2RX);
22565436921fSGabriel Fernandez static STM32_GATE(ck_eth2mac, &ck_axi, 0, GATE_ETH2MAC);
2257e84c2998SEtienne Carriere static STM32_GATE(ck_spi1, &ck_pclk2, 0, GATE_SPI1);
2258e84c2998SEtienne Carriere static STM32_GATE(ck_spi2, &ck_pclk1, 0, GATE_SPI2);
2259e84c2998SEtienne Carriere static STM32_GATE(ck_spi3, &ck_pclk1, 0, GATE_SPI3);
2260e84c2998SEtienne Carriere static STM32_GATE(ck_spi4, &ck_pclk6, 0, GATE_SPI4);
2261e84c2998SEtienne Carriere static STM32_GATE(ck_spi5, &ck_pclk6, 0, GATE_SPI5);
22625436921fSGabriel Fernandez 
22635436921fSGabriel Fernandez /* Kernel Clocks */
22645436921fSGabriel Fernandez static STM32_KCLK(ck_usbphy_k, 3,
22655436921fSGabriel Fernandez 		  PARENT(&ck_hse, &ck_pll4r, &ck_hse_div2),
22665436921fSGabriel Fernandez 		  0, GATE_USBPHY, MUX_USBPHY);
22675436921fSGabriel Fernandez 
22685436921fSGabriel Fernandez static STM32_KCLK(ck_usbo_k, 2,
22695436921fSGabriel Fernandez 		  PARENT(&ck_pll4r, &ck_usb_phy_48Mhz), 0,
22705436921fSGabriel Fernandez 		  GATE_USBO, MUX_USBO);
22715436921fSGabriel Fernandez 
22725436921fSGabriel Fernandez static STM32_KCLK(ck_stgen_k, 2,
22735436921fSGabriel Fernandez 		  PARENT(&ck_hsi, &ck_hse), 0, GATE_STGENC, MUX_STGEN);
22745436921fSGabriel Fernandez 
22755436921fSGabriel Fernandez static STM32_KCLK(ck_usart1_k, 6,
22765436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll3q, &ck_hsi,
22775436921fSGabriel Fernandez 			 &ck_csi, &ck_pll4q, &ck_hse),
22785436921fSGabriel Fernandez 		  0, GATE_USART1, MUX_UART1);
22795436921fSGabriel Fernandez 
22805436921fSGabriel Fernandez static STM32_KCLK(ck_usart2_k, 6,
22815436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll3q, &ck_hsi, &ck_csi, &ck_pll4q,
22825436921fSGabriel Fernandez 			 &ck_hse),
22835436921fSGabriel Fernandez 		  0, GATE_USART2, MUX_UART2);
22845436921fSGabriel Fernandez 
22855436921fSGabriel Fernandez static STM32_KCLK(ck_i2c4_k, 4,
22865436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll4r, &ck_hsi, &ck_csi),
22875436921fSGabriel Fernandez 		  0, GATE_I2C4, MUX_I2C4);
22885436921fSGabriel Fernandez 
22895436921fSGabriel Fernandez static STM32_KCLK(ck_rtc, 4,
22905436921fSGabriel Fernandez 		  PARENT(&ck_off, &ck_lse, &ck_lsi, &ck_hse),
22915436921fSGabriel Fernandez 		  0, GATE_RTCCK, MUX_RTC);
22925436921fSGabriel Fernandez 
22935436921fSGabriel Fernandez static STM32_KCLK(ck_saes_k, 4,
22945436921fSGabriel Fernandez 		  PARENT(&ck_axi, &ck_per, &ck_pll4r, &ck_lsi),
22955436921fSGabriel Fernandez 		  0, GATE_SAES, MUX_SAES);
22965436921fSGabriel Fernandez 
22975436921fSGabriel Fernandez static STM32_KCLK(ck_rng1_k, 4,
2298a32213b8SEtienne Carriere 		  PARENT(&ck_csi, &ck_pll4r, &ck_off, &ck_lsi),
22995436921fSGabriel Fernandez 		  0, GATE_RNG1, MUX_RNG1);
23005436921fSGabriel Fernandez 
23015436921fSGabriel Fernandez static STM32_KCLK(ck_sdmmc1_k, 4,
23025436921fSGabriel Fernandez 		  PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, &ck_hsi),
23035436921fSGabriel Fernandez 		  0, GATE_SDMMC1, MUX_SDMMC1);
23045436921fSGabriel Fernandez 
23055436921fSGabriel Fernandez static STM32_KCLK(ck_sdmmc2_k, 4,
23065436921fSGabriel Fernandez 		  PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, &ck_hsi),
23075436921fSGabriel Fernandez 		  0, GATE_SDMMC2, MUX_SDMMC2);
23085436921fSGabriel Fernandez 
23095436921fSGabriel Fernandez static STM32_KCLK(ck_usart3_k, 5,
23105436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23115436921fSGabriel Fernandez 		  0, GATE_USART3, MUX_UART35);
23125436921fSGabriel Fernandez 
23135436921fSGabriel Fernandez static STM32_KCLK(ck_uart4_k, 5,
23145436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23155436921fSGabriel Fernandez 		  0, GATE_UART4, MUX_UART4);
23165436921fSGabriel Fernandez 
23175436921fSGabriel Fernandez static STM32_KCLK(ck_uart5_k, 5,
23185436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23195436921fSGabriel Fernandez 		  0, GATE_UART5, MUX_UART35);
23205436921fSGabriel Fernandez 
23215436921fSGabriel Fernandez static STM32_KCLK(ck_uart7_k, 5,
23225436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23235436921fSGabriel Fernandez 		  0, GATE_UART7, MUX_UART78);
23245436921fSGabriel Fernandez 
23255436921fSGabriel Fernandez static STM32_KCLK(ck_uart8_k, 5,
23265436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23275436921fSGabriel Fernandez 		  0, GATE_UART8, MUX_UART78);
23285436921fSGabriel Fernandez 
23295436921fSGabriel Fernandez static STM32_KCLK(ck_usart6_k, 5,
23305436921fSGabriel Fernandez 		  PARENT(&ck_pclk2, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23315436921fSGabriel Fernandez 		  0, GATE_USART6, MUX_UART6);
23325436921fSGabriel Fernandez 
23335436921fSGabriel Fernandez static STM32_KCLK(ck_fmc_k, 4,
23345436921fSGabriel Fernandez 		  PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, &ck_per),
23355436921fSGabriel Fernandez 		  0, GATE_FMC, MUX_FMC);
23365436921fSGabriel Fernandez 
23375436921fSGabriel Fernandez static STM32_KCLK(ck_qspi_k, 4,
23385436921fSGabriel Fernandez 		  PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, &ck_per),
23395436921fSGabriel Fernandez 		  0, GATE_QSPI, MUX_QSPI);
23405436921fSGabriel Fernandez 
23415436921fSGabriel Fernandez static STM32_KCLK(ck_lptim1_k, 6,
23425436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4p, &ck_pll3q, &ck_lse, &ck_lsi,
23435436921fSGabriel Fernandez 			 &ck_per),
23445436921fSGabriel Fernandez 		  0, GATE_LPTIM1, MUX_LPTIM1);
23455436921fSGabriel Fernandez 
23465436921fSGabriel Fernandez static STM32_KCLK(ck_spi2_k, 5,
23475436921fSGabriel Fernandez 		  PARENT(&ck_pll4p, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_pll3r),
23485436921fSGabriel Fernandez 		  0, GATE_SPI2, MUX_SPI23);
23495436921fSGabriel Fernandez 
23505436921fSGabriel Fernandez static STM32_KCLK(ck_spi3_k, 5,
23515436921fSGabriel Fernandez 		  PARENT(&ck_pll4p, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_pll3r),
23525436921fSGabriel Fernandez 		  0, GATE_SPI3, MUX_SPI23);
23535436921fSGabriel Fernandez 
23545436921fSGabriel Fernandez static STM32_KCLK(ck_spdif_k, 3,
23555436921fSGabriel Fernandez 		  PARENT(&ck_pll4p, &ck_pll3q, &ck_hsi),
23565436921fSGabriel Fernandez 		  0, GATE_SPDIF, MUX_SPDIF);
23575436921fSGabriel Fernandez 
23585436921fSGabriel Fernandez static STM32_KCLK(ck_spi1_k, 5,
23595436921fSGabriel Fernandez 		  PARENT(&ck_pll4p, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_pll3r),
23605436921fSGabriel Fernandez 		  0, GATE_SPI1, MUX_SPI1);
23615436921fSGabriel Fernandez 
23625436921fSGabriel Fernandez static STM32_KCLK(ck_spi4_k, 6,
23635436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse,
23645436921fSGabriel Fernandez 			 &ck_i2sckin),
23655436921fSGabriel Fernandez 		  0, GATE_SPI4, MUX_SPI4);
23665436921fSGabriel Fernandez 
23675436921fSGabriel Fernandez static STM32_KCLK(ck_spi5_k, 5,
23685436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll4q, &ck_hsi, &ck_csi, &ck_hse),
23695436921fSGabriel Fernandez 		  0, GATE_SPI5, MUX_SPI5);
23705436921fSGabriel Fernandez 
23715436921fSGabriel Fernandez static STM32_KCLK(ck_sai1_k, 5,
23725436921fSGabriel Fernandez 		  PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_pll3r),
23735436921fSGabriel Fernandez 		  0, GATE_SAI1, MUX_SAI1);
23745436921fSGabriel Fernandez 
23755436921fSGabriel Fernandez static STM32_KCLK(ck_sai2_k, 6,
23765436921fSGabriel Fernandez 		  PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_off,
23775436921fSGabriel Fernandez 			 &ck_pll3r),
23785436921fSGabriel Fernandez 		  0, GATE_SAI2, MUX_SAI2);
23795436921fSGabriel Fernandez 
23805436921fSGabriel Fernandez static STM32_KCLK(ck_dfsdm_k, 5,
23815436921fSGabriel Fernandez 		  PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_pll3r),
23825436921fSGabriel Fernandez 		  0, GATE_DFSDM, MUX_SAI1);
23835436921fSGabriel Fernandez 
23845436921fSGabriel Fernandez static STM32_KCLK(ck_fdcan_k, 4,
23855436921fSGabriel Fernandez 		  PARENT(&ck_hse, &ck_pll3q, &ck_pll4q, &ck_pll4r),
23865436921fSGabriel Fernandez 		  0, GATE_FDCAN, MUX_FDCAN);
23875436921fSGabriel Fernandez 
23885436921fSGabriel Fernandez static STM32_KCLK(ck_i2c1_k, 4,
23895436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4r, &ck_hsi, &ck_csi),
23905436921fSGabriel Fernandez 		  0, GATE_I2C1, MUX_I2C12);
23915436921fSGabriel Fernandez 
23925436921fSGabriel Fernandez static STM32_KCLK(ck_i2c2_k, 4,
23935436921fSGabriel Fernandez 		  PARENT(&ck_pclk1, &ck_pll4r, &ck_hsi, &ck_csi),
23945436921fSGabriel Fernandez 		  0, GATE_I2C2, MUX_I2C12);
23955436921fSGabriel Fernandez 
23965436921fSGabriel Fernandez static STM32_KCLK(ck_adfsdm_k, 5,
23975436921fSGabriel Fernandez 		  PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, &ck_per, &ck_pll3r),
23985436921fSGabriel Fernandez 		  0, GATE_ADFSDM, MUX_SAI1);
23995436921fSGabriel Fernandez 
24005436921fSGabriel Fernandez static STM32_KCLK(ck_lptim2_k, 5,
24015436921fSGabriel Fernandez 		  PARENT(&ck_pclk3, &ck_pll4q, &ck_per, &ck_lse, &ck_lsi),
24025436921fSGabriel Fernandez 		  0, GATE_LPTIM2, MUX_LPTIM2);
24035436921fSGabriel Fernandez 
24045436921fSGabriel Fernandez static STM32_KCLK(ck_lptim3_k, 5,
24055436921fSGabriel Fernandez 		  PARENT(&ck_pclk3, &ck_pll4q, &ck_per, &ck_lse, &ck_lsi),
24065436921fSGabriel Fernandez 		  0, GATE_LPTIM3, MUX_LPTIM3);
24075436921fSGabriel Fernandez 
24085436921fSGabriel Fernandez static STM32_KCLK(ck_lptim4_k, 6,
24095436921fSGabriel Fernandez 		  PARENT(&ck_pclk3, &ck_pll4p, &ck_pll3q, &ck_lse, &ck_lsi,
24105436921fSGabriel Fernandez 			 &ck_per),
24115436921fSGabriel Fernandez 		  0, GATE_LPTIM4, MUX_LPTIM45);
24125436921fSGabriel Fernandez 
24135436921fSGabriel Fernandez static STM32_KCLK(ck_lptim5_k, 6,
24145436921fSGabriel Fernandez 		  PARENT(&ck_pclk3, &ck_pll4p, &ck_pll3q, &ck_lse, &ck_lsi,
24155436921fSGabriel Fernandez 			 &ck_per),
24165436921fSGabriel Fernandez 		  0, GATE_LPTIM5, MUX_LPTIM45);
24175436921fSGabriel Fernandez 
24185436921fSGabriel Fernandez static STM32_KCLK(ck_i2c3_k, 4,
24195436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll4r, &ck_hsi, &ck_csi),
24205436921fSGabriel Fernandez 		  0, GATE_I2C3, MUX_I2C3);
24215436921fSGabriel Fernandez 
24225436921fSGabriel Fernandez static STM32_KCLK(ck_i2c5_k, 4,
24235436921fSGabriel Fernandez 		  PARENT(&ck_pclk6, &ck_pll4r, &ck_hsi, &ck_csi),
24245436921fSGabriel Fernandez 		  0, GATE_I2C5, MUX_I2C5);
24255436921fSGabriel Fernandez 
24265436921fSGabriel Fernandez static STM32_KCLK(ck_dcmipp_k, 4,
24275436921fSGabriel Fernandez 		  PARENT(&ck_axi, &ck_pll2q, &ck_pll4p, &ck_per),
24285436921fSGabriel Fernandez 		  0, GATE_DCMIPP, MUX_DCMIPP);
24295436921fSGabriel Fernandez 
24305436921fSGabriel Fernandez static STM32_KCLK(ck_adc1_k, 3, PARENT(&ck_pll4r, &ck_per, &ck_pll3q),
24315436921fSGabriel Fernandez 		  0, GATE_ADC1, MUX_ADC1);
24325436921fSGabriel Fernandez 
24335436921fSGabriel Fernandez static STM32_KCLK(ck_adc2_k, 3, PARENT(&ck_pll4r, &ck_per, &ck_pll3q),
24345436921fSGabriel Fernandez 		  0, GATE_ADC2, MUX_ADC2);
24355436921fSGabriel Fernandez 
24365436921fSGabriel Fernandez static STM32_KCLK(ck_eth1ck_k, 2, PARENT(&ck_pll4p, &ck_pll3q),
24375436921fSGabriel Fernandez 		  0, GATE_ETH1CK, MUX_ETH1);
24385436921fSGabriel Fernandez 
24395436921fSGabriel Fernandez static STM32_KCLK(ck_eth2ck_k, 2, PARENT(&ck_pll4p, &ck_pll3q),
24405436921fSGabriel Fernandez 		  0, GATE_ETH2CK, MUX_ETH2);
24415436921fSGabriel Fernandez 
24425436921fSGabriel Fernandez static STM32_COMPOSITE(ck_mco1, 5,
24435436921fSGabriel Fernandez 		       PARENT(&ck_hsi, &ck_hse, &ck_csi, &ck_lsi, &ck_lse),
24445436921fSGabriel Fernandez 		       0, GATE_MCO1, DIV_MCO1, MUX_MCO1);
24455436921fSGabriel Fernandez 
24465436921fSGabriel Fernandez static STM32_COMPOSITE(ck_mco2, 6,
24475436921fSGabriel Fernandez 		       PARENT(&ck_mpu, &ck_axi, &ck_mlahb,
24485436921fSGabriel Fernandez 			      &ck_pll4p, &ck_hse, &ck_hsi),
24495436921fSGabriel Fernandez 		       0, GATE_MCO2, DIV_MCO2, MUX_MCO2);
24505436921fSGabriel Fernandez 
24515436921fSGabriel Fernandez static STM32_COMPOSITE(ck_trace, 1, PARENT(&ck_axi),
24525436921fSGabriel Fernandez 		       0, GATE_TRACECK, DIV_TRACE, NO_MUX);
24535436921fSGabriel Fernandez 
24545436921fSGabriel Fernandez enum {
24555436921fSGabriel Fernandez 	USB_PHY_48 = STM32MP1_LAST_CLK,
24565436921fSGabriel Fernandez 	PLL1P_DIV,
24575436921fSGabriel Fernandez 	CK_OFF,
24585436921fSGabriel Fernandez 	I2S_CKIN,
24595436921fSGabriel Fernandez 	STM32MP13_ALL_CLK_NB
24605436921fSGabriel Fernandez };
24615436921fSGabriel Fernandez 
24625436921fSGabriel Fernandez static struct clk *stm32mp13_clk_provided[STM32MP13_ALL_CLK_NB] = {
24635436921fSGabriel Fernandez 	[CK_HSE]	= &ck_hse,
24645436921fSGabriel Fernandez 	[CK_CSI]	= &ck_csi,
24655436921fSGabriel Fernandez 	[CK_LSI]	= &ck_lsi,
24665436921fSGabriel Fernandez 	[CK_LSE]	= &ck_lse,
24675436921fSGabriel Fernandez 	[CK_HSI]	= &ck_hsi,
24685436921fSGabriel Fernandez 	[CK_HSE_DIV2]	= &ck_hse_div2,
24695436921fSGabriel Fernandez 	[PLL1]		= &ck_pll1_vco,
24705436921fSGabriel Fernandez 	[PLL2]		= &ck_pll2_vco,
24715436921fSGabriel Fernandez 	[PLL3]		= &ck_pll3_vco,
24725436921fSGabriel Fernandez 	[PLL4]		= &ck_pll4_vco,
24735436921fSGabriel Fernandez 	[PLL1_P]	= &ck_pll1p,
24745436921fSGabriel Fernandez 	[PLL2_P]	= &ck_pll2p,
24755436921fSGabriel Fernandez 	[PLL2_Q]	= &ck_pll2q,
24765436921fSGabriel Fernandez 	[PLL2_R]	= &ck_pll2r,
24775436921fSGabriel Fernandez 	[PLL3_P]	= &ck_pll3p,
24785436921fSGabriel Fernandez 	[PLL3_Q]	= &ck_pll3q,
24795436921fSGabriel Fernandez 	[PLL3_R]	= &ck_pll3r,
24805436921fSGabriel Fernandez 	[PLL4_P]	= &ck_pll4p,
24815436921fSGabriel Fernandez 	[PLL4_Q]	= &ck_pll4q,
24825436921fSGabriel Fernandez 	[PLL4_R]	= &ck_pll4r,
24835436921fSGabriel Fernandez 	[PLL1P_DIV]	= &ck_pll1p_div,
24845436921fSGabriel Fernandez 	[CK_MPU]	= &ck_mpu,
24855436921fSGabriel Fernandez 	[CK_AXI]	= &ck_axi,
24865436921fSGabriel Fernandez 	[CK_MLAHB]	= &ck_mlahb,
24875436921fSGabriel Fernandez 	[CK_PER]	= &ck_per,
24885436921fSGabriel Fernandez 	[PCLK1]		= &ck_pclk1,
24895436921fSGabriel Fernandez 	[PCLK2]		= &ck_pclk2,
24905436921fSGabriel Fernandez 	[PCLK3]		= &ck_pclk3,
24915436921fSGabriel Fernandez 	[PCLK4]		= &ck_pclk4,
24925436921fSGabriel Fernandez 	[PCLK5]		= &ck_pclk5,
24935436921fSGabriel Fernandez 	[PCLK6]		= &ck_pclk6,
24945436921fSGabriel Fernandez 	[CK_TIMG1]	= &ck_timg1,
24955436921fSGabriel Fernandez 	[CK_TIMG2]	= &ck_timg2,
24965436921fSGabriel Fernandez 	[CK_TIMG3]	= &ck_timg3,
24975436921fSGabriel Fernandez 	[DDRC1]		= &ck_ddrc1,
24985436921fSGabriel Fernandez 	[DDRC1LP]	= &ck_ddrc1lp,
24995436921fSGabriel Fernandez 	[DDRPHYC]	= &ck_ddrphyc,
25005436921fSGabriel Fernandez 	[DDRPHYCLP]	= &ck_ddrphyclp,
25015436921fSGabriel Fernandez 	[DDRCAPB]	= &ck_ddrcapb,
25025436921fSGabriel Fernandez 	[DDRCAPBLP]	= &ck_ddrcapblp,
25035436921fSGabriel Fernandez 	[AXIDCG]	= &ck_axidcg,
25045436921fSGabriel Fernandez 	[DDRPHYCAPB]	= &ck_ddrphycapb,
25055436921fSGabriel Fernandez 	[DDRPHYCAPBLP]	= &ck_ddrphycapblp,
25065436921fSGabriel Fernandez 	[SYSCFG]	= &ck_syscfg,
25075436921fSGabriel Fernandez 	[DDRPERFM]	= &ck_ddrperfm,
25085436921fSGabriel Fernandez 	[IWDG2]		= &ck_iwdg2,
25095436921fSGabriel Fernandez 	[USBPHY_K]	= &ck_usbphy_k,
25105436921fSGabriel Fernandez 	[USBO_K]	= &ck_usbo_k,
25115436921fSGabriel Fernandez 	[RTCAPB]	= &ck_rtcapb,
25125436921fSGabriel Fernandez 	[TZC]		= &ck_tzc,
25135436921fSGabriel Fernandez 	[TZPC]		= &ck_etzpcb,
25145436921fSGabriel Fernandez 	[IWDG1]		= &ck_iwdg1apb,
25155436921fSGabriel Fernandez 	[BSEC]		= &ck_bsec,
25165436921fSGabriel Fernandez 	[STGEN_K]	= &ck_stgen_k,
25175436921fSGabriel Fernandez 	[USART1_K]	= &ck_usart1_k,
25185436921fSGabriel Fernandez 	[USART2_K]	= &ck_usart2_k,
25195436921fSGabriel Fernandez 	[I2C4_K]	= &ck_i2c4_k,
25205436921fSGabriel Fernandez 	[TIM12_K]	= &ck_tim12_k,
25215436921fSGabriel Fernandez 	[TIM15_K]	= &ck_tim15_k,
25225436921fSGabriel Fernandez 	[RTC]		= &ck_rtc,
25235436921fSGabriel Fernandez 	[GPIOA]		= &ck_gpioa,
25245436921fSGabriel Fernandez 	[GPIOB]		= &ck_gpiob,
25255436921fSGabriel Fernandez 	[GPIOC]		= &ck_gpioc,
25265436921fSGabriel Fernandez 	[GPIOD]		= &ck_gpiod,
25275436921fSGabriel Fernandez 	[GPIOE]		= &ck_gpioe,
25285436921fSGabriel Fernandez 	[GPIOF]		= &ck_gpiof,
25295436921fSGabriel Fernandez 	[GPIOG]		= &ck_gpiog,
25305436921fSGabriel Fernandez 	[GPIOH]		= &ck_gpioh,
25315436921fSGabriel Fernandez 	[GPIOI]		= &ck_gpioi,
25325436921fSGabriel Fernandez 	[PKA]		= &ck_pka,
25335436921fSGabriel Fernandez 	[SAES_K]	= &ck_saes_k,
25345436921fSGabriel Fernandez 	[CRYP1]		= &ck_cryp1,
25355436921fSGabriel Fernandez 	[HASH1]		= &ck_hash1,
25365436921fSGabriel Fernandez 	[RNG1_K]	= &ck_rng1_k,
25375436921fSGabriel Fernandez 	[BKPSRAM]	= &ck_bkpsram,
25385436921fSGabriel Fernandez 	[SDMMC1_K]	= &ck_sdmmc1_k,
25395436921fSGabriel Fernandez 	[SDMMC2_K]	= &ck_sdmmc2_k,
25405436921fSGabriel Fernandez 	[CK_DBG]	= &ck_dbg,
25415436921fSGabriel Fernandez 	[MCE]		= &ck_mce,
25425436921fSGabriel Fernandez 	[TIM2_K]	= &ck_tim2_k,
25435436921fSGabriel Fernandez 	[TIM3_K]	= &ck_tim3_k,
25445436921fSGabriel Fernandez 	[TIM4_K]	= &ck_tim4_k,
25455436921fSGabriel Fernandez 	[TIM5_K]	= &ck_tim5_k,
25465436921fSGabriel Fernandez 	[TIM6_K]	= &ck_tim6_k,
25475436921fSGabriel Fernandez 	[TIM7_K]	= &ck_tim7_k,
25485436921fSGabriel Fernandez 	[TIM13_K]	= &ck_tim13_k,
25495436921fSGabriel Fernandez 	[TIM14_K]	= &ck_tim14_k,
25505436921fSGabriel Fernandez 	[TIM1_K]	= &ck_tim1_k,
25515436921fSGabriel Fernandez 	[TIM8_K]	= &ck_tim8_k,
25525436921fSGabriel Fernandez 	[TIM16_K]	= &ck_tim16_k,
25535436921fSGabriel Fernandez 	[TIM17_K]	= &ck_tim17_k,
25545436921fSGabriel Fernandez 	[LTDC_PX]	= &ck_ltdc_px,
25555436921fSGabriel Fernandez 	[DMA1]		= &ck_dma1,
25565436921fSGabriel Fernandez 	[DMA2]		= &ck_dma2,
2557e84c2998SEtienne Carriere 	[ADC1]		= &ck_adc1,
2558e84c2998SEtienne Carriere 	[ADC2]		= &ck_adc2,
25595436921fSGabriel Fernandez 	[MDMA]		= &ck_mdma,
25605436921fSGabriel Fernandez 	[ETH1MAC]	= &ck_eth1mac,
25615436921fSGabriel Fernandez 	[USBH]		= &ck_usbh,
25625436921fSGabriel Fernandez 	[VREF]		= &ck_vref,
25635436921fSGabriel Fernandez 	[TMPSENS]	= &ck_tmpsens,
25645436921fSGabriel Fernandez 	[PMBCTRL]	= &ck_pmbctrl,
25655436921fSGabriel Fernandez 	[HDP]		= &ck_hdp,
25665436921fSGabriel Fernandez 	[STGENRO]	= &ck_stgenro,
25675436921fSGabriel Fernandez 	[DMAMUX1]	= &ck_dmamux1,
25685436921fSGabriel Fernandez 	[DMAMUX2]	= &ck_dmamux2,
25695436921fSGabriel Fernandez 	[DMA3]		= &ck_dma3,
25705436921fSGabriel Fernandez 	[TSC]		= &ck_tsc,
25715436921fSGabriel Fernandez 	[AXIMC]		= &ck_aximc,
25725436921fSGabriel Fernandez 	[CRC1]		= &ck_crc1,
25735436921fSGabriel Fernandez 	[ETH1TX]	= &ck_eth1tx,
25745436921fSGabriel Fernandez 	[ETH1RX]	= &ck_eth1rx,
25755436921fSGabriel Fernandez 	[ETH2TX]	= &ck_eth2tx,
25765436921fSGabriel Fernandez 	[ETH2RX]	= &ck_eth2rx,
25775436921fSGabriel Fernandez 	[ETH2MAC]	= &ck_eth2mac,
25785436921fSGabriel Fernandez 	[USART3_K]	= &ck_usart3_k,
25795436921fSGabriel Fernandez 	[UART4_K]	= &ck_uart4_k,
25805436921fSGabriel Fernandez 	[UART5_K]	= &ck_uart5_k,
25815436921fSGabriel Fernandez 	[UART7_K]	= &ck_uart7_k,
25825436921fSGabriel Fernandez 	[UART8_K]	= &ck_uart8_k,
25835436921fSGabriel Fernandez 	[USART6_K]	= &ck_usart6_k,
25845436921fSGabriel Fernandez 	[FMC_K]		= &ck_fmc_k,
25855436921fSGabriel Fernandez 	[QSPI_K]	= &ck_qspi_k,
25865436921fSGabriel Fernandez 	[LPTIM1_K]	= &ck_lptim1_k,
25875436921fSGabriel Fernandez 	[SPI2_K]	= &ck_spi2_k,
25885436921fSGabriel Fernandez 	[SPI3_K]	= &ck_spi3_k,
25895436921fSGabriel Fernandez 	[SPDIF_K]	= &ck_spdif_k,
25905436921fSGabriel Fernandez 	[SPI1_K]	= &ck_spi1_k,
25915436921fSGabriel Fernandez 	[SPI4_K]	= &ck_spi4_k,
25925436921fSGabriel Fernandez 	[SPI5_K]	= &ck_spi5_k,
25935436921fSGabriel Fernandez 	[SAI1_K]	= &ck_sai1_k,
25945436921fSGabriel Fernandez 	[SAI2_K]	= &ck_sai2_k,
25955436921fSGabriel Fernandez 	[DFSDM_K]	= &ck_dfsdm_k,
25965436921fSGabriel Fernandez 	[FDCAN_K]	= &ck_fdcan_k,
25975436921fSGabriel Fernandez 	[I2C1_K]	= &ck_i2c1_k,
25985436921fSGabriel Fernandez 	[I2C2_K]	= &ck_i2c2_k,
25995436921fSGabriel Fernandez 	[ADFSDM_K]	= &ck_adfsdm_k,
26005436921fSGabriel Fernandez 	[LPTIM2_K]	= &ck_lptim2_k,
26015436921fSGabriel Fernandez 	[LPTIM3_K]	= &ck_lptim3_k,
26025436921fSGabriel Fernandez 	[LPTIM4_K]	= &ck_lptim4_k,
26035436921fSGabriel Fernandez 	[LPTIM5_K]	= &ck_lptim5_k,
26045436921fSGabriel Fernandez 	[I2C3_K]	= &ck_i2c3_k,
26055436921fSGabriel Fernandez 	[I2C5_K]	= &ck_i2c5_k,
26065436921fSGabriel Fernandez 	[DCMIPP_K]	= &ck_dcmipp_k,
26075436921fSGabriel Fernandez 	[ADC1_K]	= &ck_adc1_k,
26085436921fSGabriel Fernandez 	[ADC2_K]	= &ck_adc2_k,
26095436921fSGabriel Fernandez 	[ETH1CK_K]	= &ck_eth1ck_k,
26105436921fSGabriel Fernandez 	[ETH2CK_K]	= &ck_eth2ck_k,
2611e84c2998SEtienne Carriere 	[SPI1]		= &ck_spi1,
2612e84c2998SEtienne Carriere 	[SPI2]		= &ck_spi2,
2613e84c2998SEtienne Carriere 	[SPI3]		= &ck_spi3,
2614e84c2998SEtienne Carriere 	[SPI4]		= &ck_spi4,
2615e84c2998SEtienne Carriere 	[SPI5]		= &ck_spi5,
26165436921fSGabriel Fernandez 	[CK_MCO1]	= &ck_mco1,
26175436921fSGabriel Fernandez 	[CK_MCO2]	= &ck_mco2,
26185436921fSGabriel Fernandez 	[CK_TRACE]	= &ck_trace,
26195436921fSGabriel Fernandez 	[CK_OFF]	= &ck_off,
26205436921fSGabriel Fernandez 	[USB_PHY_48]	= &ck_usb_phy_48Mhz,
26215436921fSGabriel Fernandez 	[I2S_CKIN]	= &ck_i2sckin,
26225436921fSGabriel Fernandez };
26235436921fSGabriel Fernandez 
clk_stm32_clock_is_critical(struct clk * clk __maybe_unused)26245436921fSGabriel Fernandez static bool clk_stm32_clock_is_critical(struct clk *clk __maybe_unused)
26255436921fSGabriel Fernandez {
26265436921fSGabriel Fernandez 	struct clk *clk_criticals[] = {
26275436921fSGabriel Fernandez 		&ck_hsi,
26285436921fSGabriel Fernandez 		&ck_hse,
26295436921fSGabriel Fernandez 		&ck_csi,
26305436921fSGabriel Fernandez 		&ck_lsi,
26315436921fSGabriel Fernandez 		&ck_lse,
26325436921fSGabriel Fernandez 		&ck_pll2r,
26335436921fSGabriel Fernandez 		&ck_mpu,
26345436921fSGabriel Fernandez 		&ck_ddrc1,
26355436921fSGabriel Fernandez 		&ck_ddrc1lp,
26365436921fSGabriel Fernandez 		&ck_ddrphyc,
26375436921fSGabriel Fernandez 		&ck_ddrphyclp,
26385436921fSGabriel Fernandez 		&ck_ddrcapb,
26395436921fSGabriel Fernandez 		&ck_ddrcapblp,
26405436921fSGabriel Fernandez 		&ck_axidcg,
26415436921fSGabriel Fernandez 		&ck_ddrphycapb,
26425436921fSGabriel Fernandez 		&ck_ddrphycapblp,
26435436921fSGabriel Fernandez 		&ck_rtcapb,
26445436921fSGabriel Fernandez 		&ck_tzc,
26455436921fSGabriel Fernandez 		&ck_etzpcb,
26465436921fSGabriel Fernandez 		&ck_iwdg1apb,
26475436921fSGabriel Fernandez 		&ck_bsec,
26485436921fSGabriel Fernandez 		&ck_stgen_k,
26495436921fSGabriel Fernandez 		&ck_bkpsram,
26505436921fSGabriel Fernandez 		&ck_mce,
26515436921fSGabriel Fernandez 		&ck_mco1,
2652f73343d3SGatien Chevallier 		&ck_rng1_k,
2653f73343d3SGatien Chevallier 		&ck_mlahb,
26545436921fSGabriel Fernandez 	};
26555436921fSGabriel Fernandez 	size_t i = 0;
26565436921fSGabriel Fernandez 
26575436921fSGabriel Fernandez 	for (i = 0; i < ARRAY_SIZE(clk_criticals); i++) {
26585436921fSGabriel Fernandez 		struct clk *clk_critical = clk_criticals[i];
26595436921fSGabriel Fernandez 
26605436921fSGabriel Fernandez 		if (clk == clk_critical)
26615436921fSGabriel Fernandez 			return true;
26625436921fSGabriel Fernandez 	}
26635436921fSGabriel Fernandez 
26645436921fSGabriel Fernandez 	return false;
26655436921fSGabriel Fernandez }
26665436921fSGabriel Fernandez 
clk_stm32_init_oscillators(const void * fdt,int node)26675436921fSGabriel Fernandez static void clk_stm32_init_oscillators(const void *fdt, int node)
26685436921fSGabriel Fernandez {
26695436921fSGabriel Fernandez 	size_t i = 0;
26705436921fSGabriel Fernandez 	const char *name[6] = { "clk-hse", "clk-hsi", "clk-lse",
26715436921fSGabriel Fernandez 				"clk-lsi", "clk-csi", "clk-i2sin" };
26725436921fSGabriel Fernandez 	struct clk *clks[6] = { &ck_hse, &ck_hsi, &ck_lse,
26735436921fSGabriel Fernandez 				&ck_lsi, &ck_csi, &ck_i2sckin };
26745436921fSGabriel Fernandez 
26755436921fSGabriel Fernandez 	for (i = 0; i < ARRAY_SIZE(clks); i++) {
26765436921fSGabriel Fernandez 		struct clk *clk = NULL;
26775436921fSGabriel Fernandez 
26785436921fSGabriel Fernandez 		clk_dt_get_by_name(fdt, node, name[i], &clk);
26795436921fSGabriel Fernandez 
26805436921fSGabriel Fernandez 		clks[i]->parents[0] = clk;
26815436921fSGabriel Fernandez 	}
26825436921fSGabriel Fernandez }
26835436921fSGabriel Fernandez 
2684e5e793a6SGabriel Fernandez static struct stm32_pll_dt_cfg mp13_pll[PLL_NB];
2685e5e793a6SGabriel Fernandez static struct stm32_clk_opp_dt_cfg mp13_clk_opp;
2686e5e793a6SGabriel Fernandez static struct stm32_osci_dt_cfg mp13_osci[NB_OSCILLATOR];
2687e5e793a6SGabriel Fernandez static uint32_t mp13_clksrc[MUX_NB];
2688e5e793a6SGabriel Fernandez static uint32_t mp13_clkdiv[DIV_NB];
2689e5e793a6SGabriel Fernandez 
2690e5e793a6SGabriel Fernandez static struct stm32_clk_platdata stm32mp13_clock_pdata = {
2691e5e793a6SGabriel Fernandez 	.osci		= mp13_osci,
2692e5e793a6SGabriel Fernandez 	.nosci		= NB_OSCILLATOR,
2693e5e793a6SGabriel Fernandez 	.pll		= mp13_pll,
2694e5e793a6SGabriel Fernandez 	.opp		= &mp13_clk_opp,
2695e5e793a6SGabriel Fernandez 	.npll		= PLL_NB,
2696e5e793a6SGabriel Fernandez 	.clksrc		= mp13_clksrc,
2697e5e793a6SGabriel Fernandez 	.nclksrc	= MUX_NB,
2698e5e793a6SGabriel Fernandez 	.clkdiv		= mp13_clkdiv,
2699e5e793a6SGabriel Fernandez 	.nclkdiv	= DIV_NB,
2700e5e793a6SGabriel Fernandez };
2701e5e793a6SGabriel Fernandez 
2702e5e793a6SGabriel Fernandez static struct clk_stm32_priv stm32mp13_clock_data = {
2703e5e793a6SGabriel Fernandez 	.muxes			= parent_mp13,
2704e5e793a6SGabriel Fernandez 	.nb_muxes		= ARRAY_SIZE(parent_mp13),
2705e5e793a6SGabriel Fernandez 	.gates			= gates_mp13,
2706e5e793a6SGabriel Fernandez 	.nb_gates		= ARRAY_SIZE(gates_mp13),
2707e5e793a6SGabriel Fernandez 	.div			= dividers_mp13,
2708e5e793a6SGabriel Fernandez 	.nb_div			= ARRAY_SIZE(dividers_mp13),
2709e5e793a6SGabriel Fernandez 	.pdata			= &stm32mp13_clock_pdata,
27105436921fSGabriel Fernandez 	.nb_clk_refs		= STM32MP13_ALL_CLK_NB,
27115436921fSGabriel Fernandez 	.clk_refs		= stm32mp13_clk_provided,
27125436921fSGabriel Fernandez 	.is_critical		= clk_stm32_clock_is_critical,
2713e5e793a6SGabriel Fernandez };
2714e5e793a6SGabriel Fernandez 
stm32mp13_clk_probe(const void * fdt,int node,const void * compat_data __unused)2715e5e793a6SGabriel Fernandez static TEE_Result stm32mp13_clk_probe(const void *fdt, int node,
2716e5e793a6SGabriel Fernandez 				      const void *compat_data __unused)
2717e5e793a6SGabriel Fernandez {
2718e5e793a6SGabriel Fernandez 	TEE_Result res = TEE_ERROR_GENERIC;
2719e5e793a6SGabriel Fernandez 	int fdt_rc = 0;
2720e5e793a6SGabriel Fernandez 	int rc = 0;
2721e5e793a6SGabriel Fernandez 	struct clk_stm32_priv *priv = &stm32mp13_clock_data;
2722e5e793a6SGabriel Fernandez 	struct stm32_clk_platdata *pdata = &stm32mp13_clock_pdata;
2723e5e793a6SGabriel Fernandez 
2724e5e793a6SGabriel Fernandez 	fdt_rc = stm32_clk_parse_fdt(fdt, node, pdata);
2725e5e793a6SGabriel Fernandez 	if (fdt_rc) {
2726e5e793a6SGabriel Fernandez 		EMSG("Failed to parse clock node: %d", fdt_rc);
2727e5e793a6SGabriel Fernandez 		return TEE_ERROR_GENERIC;
2728e5e793a6SGabriel Fernandez 	}
2729e5e793a6SGabriel Fernandez 
2730e5e793a6SGabriel Fernandez 	res = clk_stm32_init(priv, stm32_rcc_base());
2731e5e793a6SGabriel Fernandez 	if (res)
2732e5e793a6SGabriel Fernandez 		return res;
2733e5e793a6SGabriel Fernandez 
2734e5e793a6SGabriel Fernandez 	rc = stm32mp1_init_clock_tree(priv, pdata);
2735e5e793a6SGabriel Fernandez 	if (rc)
2736e5e793a6SGabriel Fernandez 		return TEE_ERROR_GENERIC;
2737e5e793a6SGabriel Fernandez 
27385436921fSGabriel Fernandez 	clk_stm32_init_oscillators(fdt, node);
27395436921fSGabriel Fernandez 
27405436921fSGabriel Fernandez 	stm32mp_clk_provider_probe_final(fdt, node, priv);
27415436921fSGabriel Fernandez 
2742e5e793a6SGabriel Fernandez 	return TEE_SUCCESS;
2743e5e793a6SGabriel Fernandez }
2744e5e793a6SGabriel Fernandez 
2745e5e793a6SGabriel Fernandez CLK_DT_DECLARE(stm32mp13_clk, "st,stm32mp13-rcc", stm32mp13_clk_probe);
2746