blob: 73e6a2baae7f81bf17430edf3c6a5f55c9e78e63 [file] [log] [blame]
Yann Gautier7839a052018-07-24 17:13:36 +02001/*
Lionel Debieve77b4ca02020-12-15 13:22:27 +01002 * Copyright (C) 2018-2024, STMicroelectronics - All Rights Reserved
Yann Gautier7839a052018-07-24 17:13:36 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
Yann Gautier7839a052018-07-24 17:13:36 +02007#include <assert.h>
Yann Gautier7839a052018-07-24 17:13:36 +02008#include <errno.h>
Yann Gautier7839a052018-07-24 17:13:36 +02009#include <stdint.h>
Antonio Nino Diaz39b6cc62018-08-16 16:46:06 +010010#include <stdio.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <arch.h>
13#include <arch_helpers.h>
14#include <common/debug.h>
Andre Przywara52a616b2020-03-26 12:51:21 +000015#include <common/fdt_wrappers.h>
Yann Gautier33667d22021-08-30 15:06:54 +020016#include <drivers/clk.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000017#include <drivers/delay_timer.h>
Yann Gautier447b2b12019-02-14 11:15:20 +010018#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000019#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000020#include <drivers/st/stm32mp1_rcc.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000021#include <dt-bindings/clock/stm32mp1-clksrc.h>
22#include <lib/mmio.h>
Yann Gautier0d216802019-02-14 10:53:33 +010023#include <lib/spinlock.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000024#include <lib/utils_def.h>
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +010025#include <libfdt.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000026#include <plat/common/platform.h>
27
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +010028#include <platform_def.h>
29
Yann Gautierdfdb0572019-02-14 11:14:39 +010030#define MAX_HSI_HZ 64000000
Yann Gautier0d216802019-02-14 10:53:33 +010031#define USB_PHY_48_MHZ 48000000
Yann Gautier7839a052018-07-24 17:13:36 +020032
Yann Gautierdfdb0572019-02-14 11:14:39 +010033#define TIMEOUT_US_200MS U(200000)
34#define TIMEOUT_US_1S U(1000000)
Yann Gautier7839a052018-07-24 17:13:36 +020035
Yann Gautierdfdb0572019-02-14 11:14:39 +010036#define PLLRDY_TIMEOUT TIMEOUT_US_200MS
37#define CLKSRC_TIMEOUT TIMEOUT_US_200MS
38#define CLKDIV_TIMEOUT TIMEOUT_US_200MS
39#define HSIDIV_TIMEOUT TIMEOUT_US_200MS
40#define OSCRDY_TIMEOUT TIMEOUT_US_1S
Yann Gautier7839a052018-07-24 17:13:36 +020041
Yann Gautierf66358a2019-05-17 15:57:56 +020042const char *stm32mp_osc_node_label[NB_OSC] = {
43 [_LSI] = "clk-lsi",
44 [_LSE] = "clk-lse",
45 [_HSI] = "clk-hsi",
46 [_HSE] = "clk-hse",
47 [_CSI] = "clk-csi",
48 [_I2S_CKIN] = "i2s_ckin",
49};
50
Yann Gautier7839a052018-07-24 17:13:36 +020051enum stm32mp1_parent_id {
52/* Oscillators are defined in enum stm32mp_osc_id */
53
54/* Other parent source */
55 _HSI_KER = NB_OSC,
56 _HSE_KER,
57 _HSE_KER_DIV2,
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +020058 _HSE_RTC,
Yann Gautier7839a052018-07-24 17:13:36 +020059 _CSI_KER,
60 _PLL1_P,
61 _PLL1_Q,
62 _PLL1_R,
63 _PLL2_P,
64 _PLL2_Q,
65 _PLL2_R,
66 _PLL3_P,
67 _PLL3_Q,
68 _PLL3_R,
69 _PLL4_P,
70 _PLL4_Q,
71 _PLL4_R,
72 _ACLK,
73 _PCLK1,
74 _PCLK2,
75 _PCLK3,
76 _PCLK4,
77 _PCLK5,
78 _HCLK6,
79 _HCLK2,
80 _CK_PER,
81 _CK_MPU,
Yann Gautierb053a222019-02-15 17:33:27 +010082 _CK_MCU,
Yann Gautier0d216802019-02-14 10:53:33 +010083 _USB_PHY_48,
Yann Gautier7839a052018-07-24 17:13:36 +020084 _PARENT_NB,
85 _UNKNOWN_ID = 0xff,
86};
87
Yann Gautier0d216802019-02-14 10:53:33 +010088/* Lists only the parent clock we are interested in */
Yann Gautier7839a052018-07-24 17:13:36 +020089enum stm32mp1_parent_sel {
Yann Gautier0d216802019-02-14 10:53:33 +010090 _I2C12_SEL,
91 _I2C35_SEL,
92 _STGEN_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +020093 _I2C46_SEL,
Yann Gautier0d216802019-02-14 10:53:33 +010094 _SPI6_SEL,
Yann Gautierd4151d22019-05-07 18:49:33 +020095 _UART1_SEL,
Yann Gautier0d216802019-02-14 10:53:33 +010096 _RNG1_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +020097 _UART6_SEL,
98 _UART24_SEL,
99 _UART35_SEL,
100 _UART78_SEL,
101 _SDMMC12_SEL,
102 _SDMMC3_SEL,
103 _QSPI_SEL,
104 _FMC_SEL,
Yann Gautierd4151d22019-05-07 18:49:33 +0200105 _AXIS_SEL,
106 _MCUS_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +0200107 _USBPHY_SEL,
108 _USBO_SEL,
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100109 _MPU_SEL,
Yann Gautier288f5cf2021-08-31 18:23:13 +0200110 _CKPER_SEL,
Etienne Carriere016af002019-12-08 08:22:31 +0100111 _RTC_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +0200112 _PARENT_SEL_NB,
113 _UNKNOWN_SEL = 0xff,
114};
115
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100116/* State the parent clock ID straight related to a clock */
117static const uint8_t parent_id_clock_id[_PARENT_NB] = {
118 [_HSE] = CK_HSE,
119 [_HSI] = CK_HSI,
120 [_CSI] = CK_CSI,
121 [_LSE] = CK_LSE,
122 [_LSI] = CK_LSI,
123 [_I2S_CKIN] = _UNKNOWN_ID,
124 [_USB_PHY_48] = _UNKNOWN_ID,
125 [_HSI_KER] = CK_HSI,
126 [_HSE_KER] = CK_HSE,
127 [_HSE_KER_DIV2] = CK_HSE_DIV2,
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200128 [_HSE_RTC] = _UNKNOWN_ID,
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100129 [_CSI_KER] = CK_CSI,
130 [_PLL1_P] = PLL1_P,
131 [_PLL1_Q] = PLL1_Q,
132 [_PLL1_R] = PLL1_R,
133 [_PLL2_P] = PLL2_P,
134 [_PLL2_Q] = PLL2_Q,
135 [_PLL2_R] = PLL2_R,
136 [_PLL3_P] = PLL3_P,
137 [_PLL3_Q] = PLL3_Q,
138 [_PLL3_R] = PLL3_R,
139 [_PLL4_P] = PLL4_P,
140 [_PLL4_Q] = PLL4_Q,
141 [_PLL4_R] = PLL4_R,
142 [_ACLK] = CK_AXI,
143 [_PCLK1] = CK_AXI,
144 [_PCLK2] = CK_AXI,
145 [_PCLK3] = CK_AXI,
146 [_PCLK4] = CK_AXI,
147 [_PCLK5] = CK_AXI,
148 [_CK_PER] = CK_PER,
149 [_CK_MPU] = CK_MPU,
150 [_CK_MCU] = CK_MCU,
151};
152
153static unsigned int clock_id2parent_id(unsigned long id)
154{
155 unsigned int n;
156
157 for (n = 0U; n < ARRAY_SIZE(parent_id_clock_id); n++) {
158 if (parent_id_clock_id[n] == id) {
159 return n;
160 }
161 }
162
163 return _UNKNOWN_ID;
164}
165
Yann Gautier7839a052018-07-24 17:13:36 +0200166enum stm32mp1_pll_id {
167 _PLL1,
168 _PLL2,
169 _PLL3,
170 _PLL4,
171 _PLL_NB
172};
173
174enum stm32mp1_div_id {
175 _DIV_P,
176 _DIV_Q,
177 _DIV_R,
178 _DIV_NB,
179};
180
181enum stm32mp1_clksrc_id {
182 CLKSRC_MPU,
183 CLKSRC_AXI,
Yann Gautierb053a222019-02-15 17:33:27 +0100184 CLKSRC_MCU,
Yann Gautier7839a052018-07-24 17:13:36 +0200185 CLKSRC_PLL12,
186 CLKSRC_PLL3,
187 CLKSRC_PLL4,
188 CLKSRC_RTC,
189 CLKSRC_MCO1,
190 CLKSRC_MCO2,
191 CLKSRC_NB
192};
193
194enum stm32mp1_clkdiv_id {
195 CLKDIV_MPU,
196 CLKDIV_AXI,
Yann Gautierb053a222019-02-15 17:33:27 +0100197 CLKDIV_MCU,
Yann Gautier7839a052018-07-24 17:13:36 +0200198 CLKDIV_APB1,
199 CLKDIV_APB2,
200 CLKDIV_APB3,
201 CLKDIV_APB4,
202 CLKDIV_APB5,
203 CLKDIV_RTC,
204 CLKDIV_MCO1,
205 CLKDIV_MCO2,
206 CLKDIV_NB
207};
208
209enum stm32mp1_pllcfg {
210 PLLCFG_M,
211 PLLCFG_N,
212 PLLCFG_P,
213 PLLCFG_Q,
214 PLLCFG_R,
215 PLLCFG_O,
216 PLLCFG_NB
217};
218
219enum stm32mp1_pllcsg {
220 PLLCSG_MOD_PER,
221 PLLCSG_INC_STEP,
222 PLLCSG_SSCG_MODE,
223 PLLCSG_NB
224};
225
226enum stm32mp1_plltype {
227 PLL_800,
228 PLL_1600,
229 PLL_TYPE_NB
230};
231
232struct stm32mp1_pll {
233 uint8_t refclk_min;
234 uint8_t refclk_max;
Yann Gautier7839a052018-07-24 17:13:36 +0200235};
236
237struct stm32mp1_clk_gate {
238 uint16_t offset;
239 uint8_t bit;
240 uint8_t index;
241 uint8_t set_clr;
Yann Gautieraaa09b72021-10-27 18:16:59 +0200242 uint8_t secure;
Yann Gautier0d216802019-02-14 10:53:33 +0100243 uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
244 uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
Yann Gautier7839a052018-07-24 17:13:36 +0200245};
246
247struct stm32mp1_clk_sel {
248 uint16_t offset;
249 uint8_t src;
250 uint8_t msk;
251 uint8_t nb_parent;
252 const uint8_t *parent;
253};
254
255#define REFCLK_SIZE 4
256struct stm32mp1_clk_pll {
257 enum stm32mp1_plltype plltype;
258 uint16_t rckxselr;
259 uint16_t pllxcfgr1;
260 uint16_t pllxcfgr2;
261 uint16_t pllxfracr;
262 uint16_t pllxcr;
263 uint16_t pllxcsgr;
264 enum stm32mp_osc_id refclk[REFCLK_SIZE];
265};
266
Yann Gautier0d216802019-02-14 10:53:33 +0100267/* Clocks with selectable source and non set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200268#define _CLK_SELEC(sec, off, b, idx, s) \
Yann Gautier7839a052018-07-24 17:13:36 +0200269 { \
270 .offset = (off), \
271 .bit = (b), \
272 .index = (idx), \
273 .set_clr = 0, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200274 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200275 .sel = (s), \
276 .fixed = _UNKNOWN_ID, \
Yann Gautier7839a052018-07-24 17:13:36 +0200277 }
278
Yann Gautier0d216802019-02-14 10:53:33 +0100279/* Clocks with fixed source and non set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200280#define _CLK_FIXED(sec, off, b, idx, f) \
Yann Gautier7839a052018-07-24 17:13:36 +0200281 { \
282 .offset = (off), \
283 .bit = (b), \
284 .index = (idx), \
285 .set_clr = 0, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200286 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200287 .sel = _UNKNOWN_SEL, \
288 .fixed = (f), \
Yann Gautier7839a052018-07-24 17:13:36 +0200289 }
290
Yann Gautier0d216802019-02-14 10:53:33 +0100291/* Clocks with selectable source and set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200292#define _CLK_SC_SELEC(sec, off, b, idx, s) \
Yann Gautier7839a052018-07-24 17:13:36 +0200293 { \
294 .offset = (off), \
295 .bit = (b), \
296 .index = (idx), \
297 .set_clr = 1, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200298 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200299 .sel = (s), \
300 .fixed = _UNKNOWN_ID, \
Yann Gautier7839a052018-07-24 17:13:36 +0200301 }
302
Yann Gautier0d216802019-02-14 10:53:33 +0100303/* Clocks with fixed source and set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200304#define _CLK_SC_FIXED(sec, off, b, idx, f) \
Yann Gautier7839a052018-07-24 17:13:36 +0200305 { \
306 .offset = (off), \
307 .bit = (b), \
308 .index = (idx), \
309 .set_clr = 1, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200310 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200311 .sel = _UNKNOWN_SEL, \
312 .fixed = (f), \
Yann Gautier7839a052018-07-24 17:13:36 +0200313 }
314
Yann Gautierd4151d22019-05-07 18:49:33 +0200315#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \
316 [_ ## _label ## _SEL] = { \
317 .offset = _rcc_selr, \
318 .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \
Etienne Carriere8ae08dc2019-12-08 08:20:40 +0100319 .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
320 (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
Yann Gautierd4151d22019-05-07 18:49:33 +0200321 .parent = (_parents), \
322 .nb_parent = ARRAY_SIZE(_parents) \
Yann Gautier7839a052018-07-24 17:13:36 +0200323 }
324
Yann Gautier0d216802019-02-14 10:53:33 +0100325#define _CLK_PLL(idx, type, off1, off2, off3, \
326 off4, off5, off6, \
327 p1, p2, p3, p4) \
Yann Gautier7839a052018-07-24 17:13:36 +0200328 [(idx)] = { \
329 .plltype = (type), \
330 .rckxselr = (off1), \
331 .pllxcfgr1 = (off2), \
332 .pllxcfgr2 = (off3), \
333 .pllxfracr = (off4), \
334 .pllxcr = (off5), \
335 .pllxcsgr = (off6), \
336 .refclk[0] = (p1), \
337 .refclk[1] = (p2), \
338 .refclk[2] = (p3), \
339 .refclk[3] = (p4), \
340 }
341
Yann Gautier0d216802019-02-14 10:53:33 +0100342#define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
343
Yann Gautieraaa09b72021-10-27 18:16:59 +0200344#define SEC 1
345#define N_S 0
346
Yann Gautier7839a052018-07-24 17:13:36 +0200347static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautieraaa09b72021-10-27 18:16:59 +0200348 _CLK_FIXED(SEC, RCC_DDRITFCR, 0, DDRC1, _ACLK),
349 _CLK_FIXED(SEC, RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
350 _CLK_FIXED(SEC, RCC_DDRITFCR, 2, DDRC2, _ACLK),
351 _CLK_FIXED(SEC, RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
352 _CLK_FIXED(SEC, RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
353 _CLK_FIXED(SEC, RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
354 _CLK_FIXED(SEC, RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
355 _CLK_FIXED(SEC, RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
356 _CLK_FIXED(SEC, RCC_DDRITFCR, 8, AXIDCG, _ACLK),
357 _CLK_FIXED(SEC, RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
358 _CLK_FIXED(SEC, RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),
Yann Gautier7839a052018-07-24 17:13:36 +0200359
Yann Gautier7418cf32020-01-17 11:59:28 +0100360#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200361 _CLK_SC_FIXED(N_S, RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
Yann Gautier7418cf32020-01-17 11:59:28 +0100362#endif
Yann Gautieraaa09b72021-10-27 18:16:59 +0200363 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
364 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
365 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
366 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
367 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
368 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
369 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
370 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
371 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
372 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200373
Yann Gautier7418cf32020-01-17 11:59:28 +0100374#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200375 _CLK_SC_FIXED(N_S, RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
Yann Gautier7418cf32020-01-17 11:59:28 +0100376#endif
Yann Gautieraaa09b72021-10-27 18:16:59 +0200377 _CLK_SC_SELEC(N_S, RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200378
Yann Gautieraaa09b72021-10-27 18:16:59 +0200379 _CLK_SC_FIXED(N_S, RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),
Yann Gautierf33b2432019-05-20 19:17:08 +0200380
Yann Gautieraaa09b72021-10-27 18:16:59 +0200381 _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
382 _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
383 _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200384
Yann Gautieraaa09b72021-10-27 18:16:59 +0200385 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
386 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
387 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
388 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
389 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
390 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
391 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
392 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
393 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
394 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
395 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200396
Yann Gautier7418cf32020-01-17 11:59:28 +0100397#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200398 _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
399 _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100400#endif
Yann Gautier7839a052018-07-24 17:13:36 +0200401
Yann Gautieraaa09b72021-10-27 18:16:59 +0200402 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
403 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
404 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
405 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
406 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
407 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
408 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
409 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
410 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
411 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
412 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200413
Yann Gautieraaa09b72021-10-27 18:16:59 +0200414 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
415 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
416 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
417 _CLK_SC_SELEC(SEC, RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
418 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),
Yann Gautier7839a052018-07-24 17:13:36 +0200419
Yann Gautier7418cf32020-01-17 11:59:28 +0100420#if defined(IMAGE_BL2)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200421 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
422 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100423#endif
Yann Gautieraaa09b72021-10-27 18:16:59 +0200424 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
425 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100426#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200427 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100428#endif
Yann Gautier7839a052018-07-24 17:13:36 +0200429
Yann Gautieraaa09b72021-10-27 18:16:59 +0200430 _CLK_SELEC(SEC, RCC_BDCR, 20, RTC, _RTC_SEL),
431 _CLK_SELEC(N_S, RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200432};
433
Yann Gautier0d216802019-02-14 10:53:33 +0100434static const uint8_t i2c12_parents[] = {
435 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
436};
437
438static const uint8_t i2c35_parents[] = {
439 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
440};
441
442static const uint8_t stgen_parents[] = {
443 _HSI_KER, _HSE_KER
444};
445
446static const uint8_t i2c46_parents[] = {
447 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
448};
449
450static const uint8_t spi6_parents[] = {
451 _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
452};
453
454static const uint8_t usart1_parents[] = {
455 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
456};
457
458static const uint8_t rng1_parents[] = {
459 _CSI, _PLL4_R, _LSE, _LSI
460};
461
462static const uint8_t uart6_parents[] = {
463 _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
464};
465
466static const uint8_t uart234578_parents[] = {
467 _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
468};
469
470static const uint8_t sdmmc12_parents[] = {
471 _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
472};
473
474static const uint8_t sdmmc3_parents[] = {
475 _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
476};
477
478static const uint8_t qspi_parents[] = {
479 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
480};
481
482static const uint8_t fmc_parents[] = {
483 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
484};
485
Etienne Carriereb8fe48b2019-12-19 10:03:23 +0100486static const uint8_t axiss_parents[] = {
487 _HSI, _HSE, _PLL2_P
Yann Gautier0d216802019-02-14 10:53:33 +0100488};
489
Etienne Carriereb8fe48b2019-12-19 10:03:23 +0100490static const uint8_t mcuss_parents[] = {
491 _HSI, _HSE, _CSI, _PLL3_P
Yann Gautierb053a222019-02-15 17:33:27 +0100492};
493
Yann Gautier0d216802019-02-14 10:53:33 +0100494static const uint8_t usbphy_parents[] = {
495 _HSE_KER, _PLL4_R, _HSE_KER_DIV2
496};
497
498static const uint8_t usbo_parents[] = {
499 _PLL4_R, _USB_PHY_48
500};
Yann Gautier7839a052018-07-24 17:13:36 +0200501
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100502static const uint8_t mpu_parents[] = {
503 _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */
504};
505
506static const uint8_t per_parents[] = {
507 _HSI, _HSE, _CSI,
508};
509
Etienne Carriere016af002019-12-08 08:22:31 +0100510static const uint8_t rtc_parents[] = {
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200511 _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC
Etienne Carriere016af002019-12-08 08:22:31 +0100512};
513
Yann Gautier7839a052018-07-24 17:13:36 +0200514static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
Yann Gautierd4151d22019-05-07 18:49:33 +0200515 _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
516 _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
517 _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
518 _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
519 _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
520 _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
521 _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100522 _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
Yann Gautier288f5cf2021-08-31 18:23:13 +0200523 _CLK_PARENT_SEL(CKPER, RCC_CPERCKSELR, per_parents),
Etienne Carriere016af002019-12-08 08:22:31 +0100524 _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
Yann Gautierd4151d22019-05-07 18:49:33 +0200525 _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
526 _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
527 _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
528 _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
529 _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
530 _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
531 _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
532 _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
Etienne Carriereb8fe48b2019-12-19 10:03:23 +0100533 _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, axiss_parents),
534 _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mcuss_parents),
Yann Gautierd4151d22019-05-07 18:49:33 +0200535 _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
536 _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
Yann Gautier7839a052018-07-24 17:13:36 +0200537};
538
539/* Define characteristic of PLL according type */
Yann Gautierf6559222022-09-12 11:26:09 +0200540#define POST_DIVM_MIN 8000000U
541#define POST_DIVM_MAX 16000000U
542#define DIVM_MIN 0U
543#define DIVM_MAX 63U
544#define DIVN_MIN 24U
545#define DIVN_MAX 99U
546#define DIVP_MIN 0U
547#define DIVP_MAX 127U
548#define FRAC_MAX 8192U
549#define VCO_MIN 800000000U
550#define VCO_MAX 1600000000U
551
Yann Gautier7839a052018-07-24 17:13:36 +0200552static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
553 [PLL_800] = {
554 .refclk_min = 4,
555 .refclk_max = 16,
Yann Gautier7839a052018-07-24 17:13:36 +0200556 },
557 [PLL_1600] = {
558 .refclk_min = 8,
559 .refclk_max = 16,
Yann Gautier7839a052018-07-24 17:13:36 +0200560 },
561};
562
563/* PLLNCFGR2 register divider by output */
564static const uint8_t pllncfgr2[_DIV_NB] = {
565 [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
566 [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautier0d216802019-02-14 10:53:33 +0100567 [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
Yann Gautier7839a052018-07-24 17:13:36 +0200568};
569
570static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautier0d216802019-02-14 10:53:33 +0100571 _CLK_PLL(_PLL1, PLL_1600,
572 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
573 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
574 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
575 _CLK_PLL(_PLL2, PLL_1600,
576 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
577 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
578 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
579 _CLK_PLL(_PLL3, PLL_800,
580 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
581 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
582 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
583 _CLK_PLL(_PLL4, PLL_800,
584 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
585 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
586 _HSI, _HSE, _CSI, _I2S_CKIN),
Yann Gautier7839a052018-07-24 17:13:36 +0200587};
588
589/* Prescaler table lookups for clock computation */
Yann Gautierb053a222019-02-15 17:33:27 +0100590/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
591static const uint8_t stm32mp1_mcu_div[16] = {
592 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
593};
Yann Gautier7839a052018-07-24 17:13:36 +0200594
595/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
596#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
597#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
598static const uint8_t stm32mp1_mpu_apbx_div[8] = {
599 0, 1, 2, 3, 4, 4, 4, 4
600};
601
602/* div = /1 /2 /3 /4 */
603static const uint8_t stm32mp1_axi_div[8] = {
604 1, 2, 3, 4, 4, 4, 4, 4
605};
606
Etienne Carriere37e82952020-05-13 11:49:49 +0200607static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
608 [_HSI] = "HSI",
609 [_HSE] = "HSE",
610 [_CSI] = "CSI",
611 [_LSI] = "LSI",
612 [_LSE] = "LSE",
613 [_I2S_CKIN] = "I2S_CKIN",
614 [_HSI_KER] = "HSI_KER",
615 [_HSE_KER] = "HSE_KER",
616 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200617 [_HSE_RTC] = "HSE_RTC",
Etienne Carriere37e82952020-05-13 11:49:49 +0200618 [_CSI_KER] = "CSI_KER",
619 [_PLL1_P] = "PLL1_P",
620 [_PLL1_Q] = "PLL1_Q",
621 [_PLL1_R] = "PLL1_R",
622 [_PLL2_P] = "PLL2_P",
623 [_PLL2_Q] = "PLL2_Q",
624 [_PLL2_R] = "PLL2_R",
625 [_PLL3_P] = "PLL3_P",
626 [_PLL3_Q] = "PLL3_Q",
627 [_PLL3_R] = "PLL3_R",
628 [_PLL4_P] = "PLL4_P",
629 [_PLL4_Q] = "PLL4_Q",
630 [_PLL4_R] = "PLL4_R",
631 [_ACLK] = "ACLK",
632 [_PCLK1] = "PCLK1",
633 [_PCLK2] = "PCLK2",
634 [_PCLK3] = "PCLK3",
635 [_PCLK4] = "PCLK4",
636 [_PCLK5] = "PCLK5",
637 [_HCLK6] = "KCLK6",
638 [_HCLK2] = "HCLK2",
639 [_CK_PER] = "CK_PER",
640 [_CK_MPU] = "CK_MPU",
641 [_CK_MCU] = "CK_MCU",
642 [_USB_PHY_48] = "USB_PHY_48",
643};
644
Yann Gautier0d216802019-02-14 10:53:33 +0100645/* RCC clock device driver private */
646static unsigned long stm32mp1_osc[NB_OSC];
647static struct spinlock reg_lock;
648static unsigned int gate_refcounts[NB_GATES];
649static struct spinlock refcount_lock;
Yann Gautier7839a052018-07-24 17:13:36 +0200650
Yann Gautier0d216802019-02-14 10:53:33 +0100651static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
652{
653 return &stm32mp1_clk_gate[idx];
654}
Yann Gautier7839a052018-07-24 17:13:36 +0200655
Yann Gautier3d691492021-10-27 18:21:11 +0200656#if defined(IMAGE_BL32)
657static bool gate_is_non_secure(const struct stm32mp1_clk_gate *gate)
658{
659 return gate->secure == N_S;
660}
661#endif
662
Yann Gautier0d216802019-02-14 10:53:33 +0100663static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
664{
665 return &stm32mp1_clk_sel[idx];
666}
667
668static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
669{
670 return &stm32mp1_clk_pll[idx];
671}
672
Yann Gautier0d216802019-02-14 10:53:33 +0100673static void stm32mp1_clk_lock(struct spinlock *lock)
674{
Yann Gautiere463d3f2019-05-22 19:13:51 +0200675 if (stm32mp_lock_available()) {
676 /* Assume interrupts are masked */
677 spin_lock(lock);
Yann Gautier0d216802019-02-14 10:53:33 +0100678 }
Yann Gautier0d216802019-02-14 10:53:33 +0100679}
680
681static void stm32mp1_clk_unlock(struct spinlock *lock)
682{
Yann Gautiere463d3f2019-05-22 19:13:51 +0200683 if (stm32mp_lock_available()) {
684 spin_unlock(lock);
Yann Gautier0d216802019-02-14 10:53:33 +0100685 }
Yann Gautier0d216802019-02-14 10:53:33 +0100686}
687
688bool stm32mp1_rcc_is_secure(void)
689{
690 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere1bb90722020-02-05 10:03:27 +0100691 uint32_t mask = RCC_TZCR_TZEN;
Yann Gautier0d216802019-02-14 10:53:33 +0100692
Etienne Carriere1bb90722020-02-05 10:03:27 +0100693 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautier0d216802019-02-14 10:53:33 +0100694}
695
Yann Gautierb053a222019-02-15 17:33:27 +0100696bool stm32mp1_rcc_is_mckprot(void)
697{
698 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere1bb90722020-02-05 10:03:27 +0100699 uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT;
Yann Gautierb053a222019-02-15 17:33:27 +0100700
Etienne Carriere1bb90722020-02-05 10:03:27 +0100701 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautierb053a222019-02-15 17:33:27 +0100702}
703
Yann Gautier0d216802019-02-14 10:53:33 +0100704void stm32mp1_clk_rcc_regs_lock(void)
705{
706 stm32mp1_clk_lock(&reg_lock);
707}
708
709void stm32mp1_clk_rcc_regs_unlock(void)
710{
711 stm32mp1_clk_unlock(&reg_lock);
712}
713
714static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier7839a052018-07-24 17:13:36 +0200715{
716 if (idx >= NB_OSC) {
717 return 0;
718 }
719
Yann Gautier0d216802019-02-14 10:53:33 +0100720 return stm32mp1_osc[idx];
Yann Gautier7839a052018-07-24 17:13:36 +0200721}
722
Yann Gautier0d216802019-02-14 10:53:33 +0100723static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +0200724{
Yann Gautier0d216802019-02-14 10:53:33 +0100725 unsigned int i;
Yann Gautier7839a052018-07-24 17:13:36 +0200726
Yann Gautier0d216802019-02-14 10:53:33 +0100727 for (i = 0U; i < NB_GATES; i++) {
728 if (gate_ref(i)->index == id) {
Yann Gautier7839a052018-07-24 17:13:36 +0200729 return i;
730 }
731 }
732
Yann Gautier44fb4702021-09-07 09:05:44 +0200733 ERROR("%s: clk id %lu not found\n", __func__, id);
Yann Gautier7839a052018-07-24 17:13:36 +0200734
735 return -EINVAL;
736}
737
Yann Gautier0d216802019-02-14 10:53:33 +0100738static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier7839a052018-07-24 17:13:36 +0200739{
Yann Gautier0d216802019-02-14 10:53:33 +0100740 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier7839a052018-07-24 17:13:36 +0200741}
742
Yann Gautier0d216802019-02-14 10:53:33 +0100743static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier7839a052018-07-24 17:13:36 +0200744{
Yann Gautier0d216802019-02-14 10:53:33 +0100745 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier7839a052018-07-24 17:13:36 +0200746}
747
Yann Gautier0d216802019-02-14 10:53:33 +0100748static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +0200749{
Yann Gautier0d216802019-02-14 10:53:33 +0100750 const struct stm32mp1_clk_sel *sel;
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100751 uint32_t p_sel;
Yann Gautier7839a052018-07-24 17:13:36 +0200752 int i;
753 enum stm32mp1_parent_id p;
754 enum stm32mp1_parent_sel s;
Yann Gautier0d216802019-02-14 10:53:33 +0100755 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +0200756
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100757 /* Few non gateable clock have a static parent ID, find them */
758 i = (int)clock_id2parent_id(id);
759 if (i != _UNKNOWN_ID) {
760 return i;
Yann Gautier7839a052018-07-24 17:13:36 +0200761 }
762
Yann Gautier0d216802019-02-14 10:53:33 +0100763 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier7839a052018-07-24 17:13:36 +0200764 if (i < 0) {
Yann Gautier0d216802019-02-14 10:53:33 +0100765 panic();
Yann Gautier7839a052018-07-24 17:13:36 +0200766 }
767
Yann Gautier0d216802019-02-14 10:53:33 +0100768 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier7839a052018-07-24 17:13:36 +0200769 if (p < _PARENT_NB) {
770 return (int)p;
771 }
772
Yann Gautier0d216802019-02-14 10:53:33 +0100773 s = stm32mp1_clk_get_sel(i);
774 if (s == _UNKNOWN_SEL) {
Yann Gautier7839a052018-07-24 17:13:36 +0200775 return -EINVAL;
776 }
Yann Gautier0d216802019-02-14 10:53:33 +0100777 if (s >= _PARENT_SEL_NB) {
778 panic();
Yann Gautier7839a052018-07-24 17:13:36 +0200779 }
780
Yann Gautier0d216802019-02-14 10:53:33 +0100781 sel = clk_sel_ref(s);
Etienne Carriere8ae08dc2019-12-08 08:20:40 +0100782 p_sel = (mmio_read_32(rcc_base + sel->offset) &
783 (sel->msk << sel->src)) >> sel->src;
Yann Gautier0d216802019-02-14 10:53:33 +0100784 if (p_sel < sel->nb_parent) {
785 return (int)sel->parent[p_sel];
786 }
Yann Gautier7839a052018-07-24 17:13:36 +0200787
788 return -EINVAL;
789}
790
Yann Gautier0d216802019-02-14 10:53:33 +0100791static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier7839a052018-07-24 17:13:36 +0200792{
Yann Gautier0d216802019-02-14 10:53:33 +0100793 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
794 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier7839a052018-07-24 17:13:36 +0200795
Yann Gautier0d216802019-02-14 10:53:33 +0100796 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier7839a052018-07-24 17:13:36 +0200797}
798
799/*
800 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
801 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
802 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
803 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
804 */
Yann Gautier0d216802019-02-14 10:53:33 +0100805static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier7839a052018-07-24 17:13:36 +0200806{
Yann Gautier7839a052018-07-24 17:13:36 +0200807 unsigned long refclk, fvco;
808 uint32_t cfgr1, fracr, divm, divn;
Yann Gautier0d216802019-02-14 10:53:33 +0100809 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +0200810
Yann Gautier0d216802019-02-14 10:53:33 +0100811 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
812 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier7839a052018-07-24 17:13:36 +0200813
814 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
815 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
816
Yann Gautier0d216802019-02-14 10:53:33 +0100817 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier7839a052018-07-24 17:13:36 +0200818
819 /*
820 * With FRACV :
821 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
822 * Without FRACV
823 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
824 */
825 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +0100826 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
827 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier7839a052018-07-24 17:13:36 +0200828 unsigned long long numerator, denominator;
829
Yann Gautier0d216802019-02-14 10:53:33 +0100830 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
831 numerator = refclk * numerator;
832 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier7839a052018-07-24 17:13:36 +0200833 fvco = (unsigned long)(numerator / denominator);
834 } else {
835 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
836 }
837
838 return fvco;
839}
840
Yann Gautier0d216802019-02-14 10:53:33 +0100841static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier7839a052018-07-24 17:13:36 +0200842 enum stm32mp1_div_id div_id)
843{
Yann Gautier0d216802019-02-14 10:53:33 +0100844 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier7839a052018-07-24 17:13:36 +0200845 unsigned long dfout;
846 uint32_t cfgr2, divy;
847
848 if (div_id >= _DIV_NB) {
849 return 0;
850 }
851
Yann Gautier0d216802019-02-14 10:53:33 +0100852 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier7839a052018-07-24 17:13:36 +0200853 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
854
Yann Gautier0d216802019-02-14 10:53:33 +0100855 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier7839a052018-07-24 17:13:36 +0200856
857 return dfout;
858}
859
Yann Gautier0d216802019-02-14 10:53:33 +0100860static unsigned long get_clock_rate(int p)
Yann Gautier7839a052018-07-24 17:13:36 +0200861{
862 uint32_t reg, clkdiv;
863 unsigned long clock = 0;
Yann Gautier0d216802019-02-14 10:53:33 +0100864 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +0200865
866 switch (p) {
867 case _CK_MPU:
868 /* MPU sub system */
Yann Gautier0d216802019-02-14 10:53:33 +0100869 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier7839a052018-07-24 17:13:36 +0200870 switch (reg & RCC_SELR_SRC_MASK) {
871 case RCC_MPCKSELR_HSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100872 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200873 break;
874 case RCC_MPCKSELR_HSE:
Yann Gautier0d216802019-02-14 10:53:33 +0100875 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200876 break;
877 case RCC_MPCKSELR_PLL:
Yann Gautier0d216802019-02-14 10:53:33 +0100878 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +0200879 break;
880 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautier0d216802019-02-14 10:53:33 +0100881 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +0200882
Yann Gautier0d216802019-02-14 10:53:33 +0100883 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200884 clkdiv = reg & RCC_MPUDIV_MASK;
Gabriel Fernandez602ae2f2020-02-28 09:09:06 +0100885 clock >>= stm32mp1_mpu_div[clkdiv];
Yann Gautier7839a052018-07-24 17:13:36 +0200886 break;
887 default:
888 break;
889 }
890 break;
891 /* AXI sub system */
892 case _ACLK:
893 case _HCLK2:
894 case _HCLK6:
895 case _PCLK4:
896 case _PCLK5:
Yann Gautier0d216802019-02-14 10:53:33 +0100897 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier7839a052018-07-24 17:13:36 +0200898 switch (reg & RCC_SELR_SRC_MASK) {
899 case RCC_ASSCKSELR_HSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100900 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200901 break;
902 case RCC_ASSCKSELR_HSE:
Yann Gautier0d216802019-02-14 10:53:33 +0100903 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200904 break;
905 case RCC_ASSCKSELR_PLL:
Yann Gautier0d216802019-02-14 10:53:33 +0100906 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +0200907 break;
908 default:
909 break;
910 }
911
912 /* System clock divider */
Yann Gautier0d216802019-02-14 10:53:33 +0100913 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200914 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
915
916 switch (p) {
917 case _PCLK4:
Yann Gautier0d216802019-02-14 10:53:33 +0100918 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200919 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
920 break;
921 case _PCLK5:
Yann Gautier0d216802019-02-14 10:53:33 +0100922 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200923 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
924 break;
925 default:
926 break;
927 }
928 break;
Yann Gautierb053a222019-02-15 17:33:27 +0100929 /* MCU sub system */
930 case _CK_MCU:
931 case _PCLK1:
932 case _PCLK2:
933 case _PCLK3:
934 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
935 switch (reg & RCC_SELR_SRC_MASK) {
936 case RCC_MSSCKSELR_HSI:
937 clock = stm32mp1_clk_get_fixed(_HSI);
938 break;
939 case RCC_MSSCKSELR_HSE:
940 clock = stm32mp1_clk_get_fixed(_HSE);
941 break;
942 case RCC_MSSCKSELR_CSI:
943 clock = stm32mp1_clk_get_fixed(_CSI);
944 break;
945 case RCC_MSSCKSELR_PLL:
946 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
947 break;
948 default:
949 break;
950 }
951
952 /* MCU clock divider */
953 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
954 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
955
956 switch (p) {
957 case _PCLK1:
958 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
959 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
960 break;
961 case _PCLK2:
962 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
963 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
964 break;
965 case _PCLK3:
966 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
967 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
968 break;
969 case _CK_MCU:
970 default:
971 break;
972 }
973 break;
Yann Gautier7839a052018-07-24 17:13:36 +0200974 case _CK_PER:
Yann Gautier0d216802019-02-14 10:53:33 +0100975 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier7839a052018-07-24 17:13:36 +0200976 switch (reg & RCC_SELR_SRC_MASK) {
977 case RCC_CPERCKSELR_HSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100978 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200979 break;
980 case RCC_CPERCKSELR_HSE:
Yann Gautier0d216802019-02-14 10:53:33 +0100981 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200982 break;
983 case RCC_CPERCKSELR_CSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100984 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200985 break;
986 default:
987 break;
988 }
989 break;
990 case _HSI:
991 case _HSI_KER:
Yann Gautier0d216802019-02-14 10:53:33 +0100992 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200993 break;
994 case _CSI:
995 case _CSI_KER:
Yann Gautier0d216802019-02-14 10:53:33 +0100996 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200997 break;
998 case _HSE:
999 case _HSE_KER:
Yann Gautier0d216802019-02-14 10:53:33 +01001000 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +02001001 break;
1002 case _HSE_KER_DIV2:
Yann Gautier0d216802019-02-14 10:53:33 +01001003 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier7839a052018-07-24 17:13:36 +02001004 break;
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +02001005 case _HSE_RTC:
1006 clock = stm32mp1_clk_get_fixed(_HSE);
1007 clock /= (mmio_read_32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U;
1008 break;
Yann Gautier7839a052018-07-24 17:13:36 +02001009 case _LSI:
Yann Gautier0d216802019-02-14 10:53:33 +01001010 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier7839a052018-07-24 17:13:36 +02001011 break;
1012 case _LSE:
Yann Gautier0d216802019-02-14 10:53:33 +01001013 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier7839a052018-07-24 17:13:36 +02001014 break;
1015 /* PLL */
1016 case _PLL1_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001017 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001018 break;
1019 case _PLL1_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001020 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001021 break;
1022 case _PLL1_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001023 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001024 break;
1025 case _PLL2_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001026 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001027 break;
1028 case _PLL2_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001029 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001030 break;
1031 case _PLL2_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001032 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001033 break;
1034 case _PLL3_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001035 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001036 break;
1037 case _PLL3_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001038 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001039 break;
1040 case _PLL3_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001041 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001042 break;
1043 case _PLL4_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001044 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001045 break;
1046 case _PLL4_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001047 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001048 break;
1049 case _PLL4_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001050 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001051 break;
1052 /* Other */
1053 case _USB_PHY_48:
Yann Gautier0d216802019-02-14 10:53:33 +01001054 clock = USB_PHY_48_MHZ;
Yann Gautier7839a052018-07-24 17:13:36 +02001055 break;
1056 default:
1057 break;
1058 }
1059
1060 return clock;
1061}
1062
Yann Gautier0d216802019-02-14 10:53:33 +01001063static void __clk_enable(struct stm32mp1_clk_gate const *gate)
1064{
1065 uintptr_t rcc_base = stm32mp_rcc_base();
1066
Etienne Carriere25be8452019-12-08 08:21:08 +01001067 VERBOSE("Enable clock %u\n", gate->index);
1068
Yann Gautier0d216802019-02-14 10:53:33 +01001069 if (gate->set_clr != 0U) {
1070 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
1071 } else {
1072 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
1073 }
Yann Gautier0d216802019-02-14 10:53:33 +01001074}
1075
1076static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1077{
1078 uintptr_t rcc_base = stm32mp_rcc_base();
1079
Etienne Carriere25be8452019-12-08 08:21:08 +01001080 VERBOSE("Disable clock %u\n", gate->index);
1081
Yann Gautier0d216802019-02-14 10:53:33 +01001082 if (gate->set_clr != 0U) {
1083 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1084 BIT(gate->bit));
1085 } else {
1086 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1087 }
Yann Gautier0d216802019-02-14 10:53:33 +01001088}
1089
1090static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1091{
1092 uintptr_t rcc_base = stm32mp_rcc_base();
1093
1094 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1095}
1096
Etienne Carriere35848202019-12-08 08:21:44 +01001097/* Oscillators and PLLs are not gated at runtime */
1098static bool clock_is_always_on(unsigned long id)
1099{
1100 switch (id) {
1101 case CK_HSE:
1102 case CK_CSI:
1103 case CK_LSI:
1104 case CK_LSE:
1105 case CK_HSI:
1106 case CK_HSE_DIV2:
1107 case PLL1_Q:
1108 case PLL1_R:
1109 case PLL2_P:
1110 case PLL2_Q:
1111 case PLL2_R:
1112 case PLL3_P:
1113 case PLL3_Q:
1114 case PLL3_R:
Yann Gautierbf393182020-09-16 16:41:55 +02001115 case CK_AXI:
1116 case CK_MPU:
1117 case CK_MCU:
HE Shushan5b111c72021-07-12 23:04:10 +02001118 case RTC:
Etienne Carriere35848202019-12-08 08:21:44 +01001119 return true;
1120 default:
1121 return false;
1122 }
1123}
1124
Yann Gautier2444d232022-01-19 13:57:49 +01001125static void __stm32mp1_clk_enable(unsigned long id, bool with_refcnt)
Yann Gautier0d216802019-02-14 10:53:33 +01001126{
1127 const struct stm32mp1_clk_gate *gate;
Etienne Carriere35848202019-12-08 08:21:44 +01001128 int i;
Yann Gautier0d216802019-02-14 10:53:33 +01001129
Etienne Carriere35848202019-12-08 08:21:44 +01001130 if (clock_is_always_on(id)) {
1131 return;
1132 }
1133
1134 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier0d216802019-02-14 10:53:33 +01001135 if (i < 0) {
Yann Gautier44fb4702021-09-07 09:05:44 +02001136 ERROR("Clock %lu can't be enabled\n", id);
Yann Gautier0d216802019-02-14 10:53:33 +01001137 panic();
1138 }
1139
1140 gate = gate_ref(i);
Yann Gautier2444d232022-01-19 13:57:49 +01001141
1142 if (!with_refcnt) {
1143 __clk_enable(gate);
1144 return;
1145 }
Yann Gautier0d216802019-02-14 10:53:33 +01001146
Yann Gautier3d691492021-10-27 18:21:11 +02001147#if defined(IMAGE_BL32)
1148 if (gate_is_non_secure(gate)) {
1149 /* Enable non-secure clock w/o any refcounting */
1150 __clk_enable(gate);
1151 return;
1152 }
1153#endif
1154
Yann Gautier0d216802019-02-14 10:53:33 +01001155 stm32mp1_clk_lock(&refcount_lock);
1156
Yann Gautier2444d232022-01-19 13:57:49 +01001157 if (gate_refcounts[i] == 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001158 __clk_enable(gate);
1159 }
1160
Yann Gautier2444d232022-01-19 13:57:49 +01001161 gate_refcounts[i]++;
1162 if (gate_refcounts[i] == UINT_MAX) {
1163 ERROR("Clock %lu refcount reached max value\n", id);
1164 panic();
1165 }
1166
Yann Gautier0d216802019-02-14 10:53:33 +01001167 stm32mp1_clk_unlock(&refcount_lock);
1168}
1169
Yann Gautier2444d232022-01-19 13:57:49 +01001170static void __stm32mp1_clk_disable(unsigned long id, bool with_refcnt)
Yann Gautier0d216802019-02-14 10:53:33 +01001171{
1172 const struct stm32mp1_clk_gate *gate;
Etienne Carriere35848202019-12-08 08:21:44 +01001173 int i;
Yann Gautier0d216802019-02-14 10:53:33 +01001174
Etienne Carriere35848202019-12-08 08:21:44 +01001175 if (clock_is_always_on(id)) {
1176 return;
1177 }
1178
1179 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier0d216802019-02-14 10:53:33 +01001180 if (i < 0) {
Yann Gautier44fb4702021-09-07 09:05:44 +02001181 ERROR("Clock %lu can't be disabled\n", id);
Yann Gautier0d216802019-02-14 10:53:33 +01001182 panic();
1183 }
1184
1185 gate = gate_ref(i);
Yann Gautier2444d232022-01-19 13:57:49 +01001186
1187 if (!with_refcnt) {
1188 __clk_disable(gate);
1189 return;
1190 }
Yann Gautier0d216802019-02-14 10:53:33 +01001191
Yann Gautier3d691492021-10-27 18:21:11 +02001192#if defined(IMAGE_BL32)
1193 if (gate_is_non_secure(gate)) {
1194 /* Don't disable non-secure clocks */
1195 return;
1196 }
1197#endif
1198
Yann Gautier0d216802019-02-14 10:53:33 +01001199 stm32mp1_clk_lock(&refcount_lock);
1200
Yann Gautier2444d232022-01-19 13:57:49 +01001201 if (gate_refcounts[i] == 0U) {
1202 ERROR("Clock %lu refcount reached 0\n", id);
1203 panic();
1204 }
1205 gate_refcounts[i]--;
1206
1207 if (gate_refcounts[i] == 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001208 __clk_disable(gate);
1209 }
1210
1211 stm32mp1_clk_unlock(&refcount_lock);
1212}
1213
Yann Gautier33667d22021-08-30 15:06:54 +02001214static int stm32mp_clk_enable(unsigned long id)
Yann Gautier0d216802019-02-14 10:53:33 +01001215{
1216 __stm32mp1_clk_enable(id, true);
Yann Gautier33667d22021-08-30 15:06:54 +02001217
1218 return 0;
Yann Gautier0d216802019-02-14 10:53:33 +01001219}
1220
Yann Gautier33667d22021-08-30 15:06:54 +02001221static void stm32mp_clk_disable(unsigned long id)
Yann Gautier0d216802019-02-14 10:53:33 +01001222{
1223 __stm32mp1_clk_disable(id, true);
1224}
1225
Yann Gautier33667d22021-08-30 15:06:54 +02001226static bool stm32mp_clk_is_enabled(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +02001227{
Etienne Carriere35848202019-12-08 08:21:44 +01001228 int i;
Yann Gautier7839a052018-07-24 17:13:36 +02001229
Etienne Carriere35848202019-12-08 08:21:44 +01001230 if (clock_is_always_on(id)) {
1231 return true;
1232 }
1233
1234 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier7839a052018-07-24 17:13:36 +02001235 if (i < 0) {
Yann Gautier0d216802019-02-14 10:53:33 +01001236 panic();
Yann Gautier7839a052018-07-24 17:13:36 +02001237 }
1238
Yann Gautier0d216802019-02-14 10:53:33 +01001239 return __clk_is_enabled(gate_ref(i));
Yann Gautier7839a052018-07-24 17:13:36 +02001240}
1241
Yann Gautier33667d22021-08-30 15:06:54 +02001242static unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +02001243{
Yann Gautier33667d22021-08-30 15:06:54 +02001244 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier0d216802019-02-14 10:53:33 +01001245 int p = stm32mp1_clk_get_parent(id);
Yann Gautier33667d22021-08-30 15:06:54 +02001246 uint32_t prescaler, timpre;
1247 unsigned long parent_rate;
Yann Gautier7839a052018-07-24 17:13:36 +02001248
1249 if (p < 0) {
1250 return 0;
1251 }
1252
Yann Gautier33667d22021-08-30 15:06:54 +02001253 parent_rate = get_clock_rate(p);
1254
1255 switch (id) {
1256 case TIM2_K:
1257 case TIM3_K:
1258 case TIM4_K:
1259 case TIM5_K:
1260 case TIM6_K:
1261 case TIM7_K:
1262 case TIM12_K:
1263 case TIM13_K:
1264 case TIM14_K:
1265 prescaler = mmio_read_32(rcc_base + RCC_APB1DIVR) &
1266 RCC_APBXDIV_MASK;
1267 timpre = mmio_read_32(rcc_base + RCC_TIMG1PRER) &
1268 RCC_TIMGXPRER_TIMGXPRE;
1269 break;
1270
1271 case TIM1_K:
1272 case TIM8_K:
1273 case TIM15_K:
1274 case TIM16_K:
1275 case TIM17_K:
1276 prescaler = mmio_read_32(rcc_base + RCC_APB2DIVR) &
1277 RCC_APBXDIV_MASK;
1278 timpre = mmio_read_32(rcc_base + RCC_TIMG2PRER) &
1279 RCC_TIMGXPRER_TIMGXPRE;
1280 break;
1281
1282 default:
1283 return parent_rate;
1284 }
1285
1286 if (prescaler == 0U) {
1287 return parent_rate;
1288 }
1289
1290 return parent_rate * (timpre + 1U) * 2U;
Yann Gautier7839a052018-07-24 17:13:36 +02001291}
1292
Yann Gautier0d216802019-02-14 10:53:33 +01001293static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier7839a052018-07-24 17:13:36 +02001294{
Yann Gautier0d216802019-02-14 10:53:33 +01001295 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier7839a052018-07-24 17:13:36 +02001296
Yann Gautier0d216802019-02-14 10:53:33 +01001297 if (enable) {
Yann Gautier7839a052018-07-24 17:13:36 +02001298 mmio_setbits_32(address, mask_on);
1299 } else {
1300 mmio_clrbits_32(address, mask_on);
1301 }
1302}
1303
Yann Gautier0d216802019-02-14 10:53:33 +01001304static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier7839a052018-07-24 17:13:36 +02001305{
Yann Gautier0d216802019-02-14 10:53:33 +01001306 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1307 uintptr_t address = stm32mp_rcc_base() + offset;
1308
1309 mmio_write_32(address, mask_on);
Yann Gautier7839a052018-07-24 17:13:36 +02001310}
1311
Yann Gautier0d216802019-02-14 10:53:33 +01001312static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier7839a052018-07-24 17:13:36 +02001313{
Yann Gautierdfdb0572019-02-14 11:14:39 +01001314 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001315 uint32_t mask_test;
Yann Gautier0d216802019-02-14 10:53:33 +01001316 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier7839a052018-07-24 17:13:36 +02001317
Yann Gautier0d216802019-02-14 10:53:33 +01001318 if (enable) {
Yann Gautier7839a052018-07-24 17:13:36 +02001319 mask_test = mask_rdy;
1320 } else {
1321 mask_test = 0;
1322 }
1323
Yann Gautierdfdb0572019-02-14 11:14:39 +01001324 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001325 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001326 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001327 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001328 mask_rdy, address, enable, mmio_read_32(address));
1329 return -ETIMEDOUT;
1330 }
1331 }
1332
1333 return 0;
1334}
1335
Yann Gautier0d216802019-02-14 10:53:33 +01001336static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier7839a052018-07-24 17:13:36 +02001337{
1338 uint32_t value;
Yann Gautier0d216802019-02-14 10:53:33 +01001339 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +02001340
Yann Gautierf4a2bb92022-03-21 11:39:33 +01001341 /* Do not reconfigure LSE if it is already ON */
1342 if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEON) == RCC_BDCR_LSEON) {
1343 return;
1344 }
1345
Yann Gautier0d216802019-02-14 10:53:33 +01001346 if (digbyp) {
1347 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
1348 }
1349
1350 if (bypass || digbyp) {
1351 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
Yann Gautier7839a052018-07-24 17:13:36 +02001352 }
1353
1354 /*
1355 * Warning: not recommended to switch directly from "high drive"
1356 * to "medium low drive", and vice-versa.
1357 */
Yann Gautier0d216802019-02-14 10:53:33 +01001358 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier7839a052018-07-24 17:13:36 +02001359 RCC_BDCR_LSEDRV_SHIFT;
1360
1361 while (value != lsedrv) {
1362 if (value > lsedrv) {
1363 value--;
1364 } else {
1365 value++;
1366 }
1367
Yann Gautier0d216802019-02-14 10:53:33 +01001368 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier7839a052018-07-24 17:13:36 +02001369 RCC_BDCR_LSEDRV_MASK,
1370 value << RCC_BDCR_LSEDRV_SHIFT);
1371 }
1372
Yann Gautier0d216802019-02-14 10:53:33 +01001373 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier7839a052018-07-24 17:13:36 +02001374}
1375
Yann Gautier0d216802019-02-14 10:53:33 +01001376static void stm32mp1_lse_wait(void)
Yann Gautier7839a052018-07-24 17:13:36 +02001377{
Yann Gautier0d216802019-02-14 10:53:33 +01001378 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001379 VERBOSE("%s: failed\n", __func__);
1380 }
1381}
1382
Yann Gautier0d216802019-02-14 10:53:33 +01001383static void stm32mp1_lsi_set(bool enable)
Yann Gautier7839a052018-07-24 17:13:36 +02001384{
Yann Gautier0d216802019-02-14 10:53:33 +01001385 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1386
1387 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001388 VERBOSE("%s: failed\n", __func__);
1389 }
1390}
1391
Yann Gautier0d216802019-02-14 10:53:33 +01001392static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier7839a052018-07-24 17:13:36 +02001393{
Yann Gautier0d216802019-02-14 10:53:33 +01001394 uintptr_t rcc_base = stm32mp_rcc_base();
1395
1396 if (digbyp) {
1397 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier7839a052018-07-24 17:13:36 +02001398 }
1399
Yann Gautier0d216802019-02-14 10:53:33 +01001400 if (bypass || digbyp) {
1401 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1402 }
1403
1404 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1405 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001406 VERBOSE("%s: failed\n", __func__);
1407 }
1408
1409 if (css) {
Yann Gautier0d216802019-02-14 10:53:33 +01001410 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier7839a052018-07-24 17:13:36 +02001411 }
Lionel Debieve31e97502019-07-02 18:03:34 +02001412
1413#if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
1414 if ((mmio_read_32(rcc_base + RCC_OCENSETR) & RCC_OCENR_HSEBYP) &&
1415 (!(digbyp || bypass))) {
1416 panic();
1417 }
1418#endif
Yann Gautier7839a052018-07-24 17:13:36 +02001419}
1420
Yann Gautier0d216802019-02-14 10:53:33 +01001421static void stm32mp1_csi_set(bool enable)
Yann Gautier7839a052018-07-24 17:13:36 +02001422{
Yann Gautier0d216802019-02-14 10:53:33 +01001423 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1424 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001425 VERBOSE("%s: failed\n", __func__);
1426 }
1427}
1428
Yann Gautier0d216802019-02-14 10:53:33 +01001429static void stm32mp1_hsi_set(bool enable)
Yann Gautier7839a052018-07-24 17:13:36 +02001430{
Yann Gautier0d216802019-02-14 10:53:33 +01001431 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1432 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001433 VERBOSE("%s: failed\n", __func__);
1434 }
1435}
1436
Yann Gautier0d216802019-02-14 10:53:33 +01001437static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier7839a052018-07-24 17:13:36 +02001438{
Yann Gautierdfdb0572019-02-14 11:14:39 +01001439 uint64_t timeout;
Yann Gautier0d216802019-02-14 10:53:33 +01001440 uintptr_t rcc_base = stm32mp_rcc_base();
1441 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier7839a052018-07-24 17:13:36 +02001442
Yann Gautier0d216802019-02-14 10:53:33 +01001443 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier7839a052018-07-24 17:13:36 +02001444 RCC_HSICFGR_HSIDIV_MASK,
1445 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1446
Yann Gautierdfdb0572019-02-14 11:14:39 +01001447 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001448 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001449 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001450 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001451 address, mmio_read_32(address));
1452 return -ETIMEDOUT;
1453 }
1454 }
1455
1456 return 0;
1457}
1458
Yann Gautier0d216802019-02-14 10:53:33 +01001459static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier7839a052018-07-24 17:13:36 +02001460{
1461 uint8_t hsidiv;
1462 uint32_t hsidivfreq = MAX_HSI_HZ;
1463
1464 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1465 if (hsidivfreq == hsifreq) {
1466 break;
1467 }
1468
1469 hsidivfreq /= 2U;
1470 }
1471
1472 if (hsidiv == 4U) {
1473 ERROR("Invalid clk-hsi frequency\n");
1474 return -1;
1475 }
1476
1477 if (hsidiv != 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001478 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier7839a052018-07-24 17:13:36 +02001479 }
1480
1481 return 0;
1482}
1483
Yann Gautier0d216802019-02-14 10:53:33 +01001484static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1485 unsigned int clksrc,
1486 uint32_t *pllcfg, int plloff)
Yann Gautier7839a052018-07-24 17:13:36 +02001487{
Yann Gautier0d216802019-02-14 10:53:33 +01001488 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1489 uintptr_t rcc_base = stm32mp_rcc_base();
1490 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1491 enum stm32mp1_plltype type = pll->plltype;
1492 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1493 unsigned long refclk;
1494 uint32_t ifrge = 0U;
Andre Przywarabe858cf2020-03-26 11:50:33 +00001495 uint32_t src, value, fracv = 0;
1496 void *fdt;
Yann Gautier7839a052018-07-24 17:13:36 +02001497
Yann Gautier0d216802019-02-14 10:53:33 +01001498 /* Check PLL output */
1499 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1500 return false;
1501 }
1502
1503 /* Check current clksrc */
1504 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1505 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1506 return false;
1507 }
1508
1509 /* Check Div */
1510 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1511
1512 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1513 (pllcfg[PLLCFG_M] + 1U);
1514
1515 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1516 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1517 return false;
1518 }
1519
1520 if ((type == PLL_800) && (refclk >= 8000000U)) {
1521 ifrge = 1U;
1522 }
1523
1524 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1525 RCC_PLLNCFGR1_DIVN_MASK;
1526 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1527 RCC_PLLNCFGR1_DIVM_MASK;
1528 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1529 RCC_PLLNCFGR1_IFRGE_MASK;
1530 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1531 return false;
1532 }
1533
1534 /* Fractional configuration */
Andre Przywarabe858cf2020-03-26 11:50:33 +00001535 if (fdt_get_address(&fdt) == 1) {
1536 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1537 }
Yann Gautier0d216802019-02-14 10:53:33 +01001538
1539 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1540 value |= RCC_PLLNFRACR_FRACLE;
1541 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1542 return false;
1543 }
1544
1545 /* Output config */
1546 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1547 RCC_PLLNCFGR2_DIVP_MASK;
1548 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1549 RCC_PLLNCFGR2_DIVQ_MASK;
1550 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1551 RCC_PLLNCFGR2_DIVR_MASK;
1552 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1553 return false;
1554 }
1555
1556 return true;
Yann Gautier7839a052018-07-24 17:13:36 +02001557}
1558
Yann Gautier0d216802019-02-14 10:53:33 +01001559static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier7839a052018-07-24 17:13:36 +02001560{
Yann Gautier0d216802019-02-14 10:53:33 +01001561 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1562 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1563
Yann Gautierdd98aec2019-06-04 15:55:37 +02001564 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1565 mmio_clrsetbits_32(pllxcr,
1566 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1567 RCC_PLLNCR_DIVREN,
1568 RCC_PLLNCR_PLLON);
Yann Gautier0d216802019-02-14 10:53:33 +01001569}
1570
1571static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
1572{
1573 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1574 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautierdfdb0572019-02-14 11:14:39 +01001575 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001576
Yann Gautier7839a052018-07-24 17:13:36 +02001577 /* Wait PLL lock */
1578 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001579 if (timeout_elapsed(timeout)) {
Yann Gautier9fa9a0c2022-02-28 11:34:05 +01001580 ERROR("PLL%u start failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001581 pll_id, pllxcr, mmio_read_32(pllxcr));
1582 return -ETIMEDOUT;
1583 }
1584 }
1585
1586 /* Start the requested output */
1587 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1588
1589 return 0;
1590}
1591
Yann Gautier0d216802019-02-14 10:53:33 +01001592static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier7839a052018-07-24 17:13:36 +02001593{
Yann Gautier0d216802019-02-14 10:53:33 +01001594 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1595 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautierdfdb0572019-02-14 11:14:39 +01001596 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001597
1598 /* Stop all output */
1599 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1600 RCC_PLLNCR_DIVREN);
1601
1602 /* Stop PLL */
1603 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1604
Yann Gautierdfdb0572019-02-14 11:14:39 +01001605 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001606 /* Wait PLL stopped */
1607 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001608 if (timeout_elapsed(timeout)) {
Yann Gautier9fa9a0c2022-02-28 11:34:05 +01001609 ERROR("PLL%u stop failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001610 pll_id, pllxcr, mmio_read_32(pllxcr));
1611 return -ETIMEDOUT;
1612 }
1613 }
1614
1615 return 0;
1616}
1617
Yann Gautier0d216802019-02-14 10:53:33 +01001618static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier7839a052018-07-24 17:13:36 +02001619 uint32_t *pllcfg)
1620{
Yann Gautier0d216802019-02-14 10:53:33 +01001621 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1622 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +02001623 uint32_t value;
1624
1625 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1626 RCC_PLLNCFGR2_DIVP_MASK;
1627 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1628 RCC_PLLNCFGR2_DIVQ_MASK;
1629 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1630 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautier0d216802019-02-14 10:53:33 +01001631 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001632}
1633
Yann Gautier0d216802019-02-14 10:53:33 +01001634static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier7839a052018-07-24 17:13:36 +02001635 uint32_t *pllcfg, uint32_t fracv)
1636{
Yann Gautier0d216802019-02-14 10:53:33 +01001637 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1638 uintptr_t rcc_base = stm32mp_rcc_base();
1639 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier7839a052018-07-24 17:13:36 +02001640 unsigned long refclk;
1641 uint32_t ifrge = 0;
1642 uint32_t src, value;
1643
Yann Gautier0d216802019-02-14 10:53:33 +01001644 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier7839a052018-07-24 17:13:36 +02001645 RCC_SELR_REFCLK_SRC_MASK;
1646
Yann Gautier0d216802019-02-14 10:53:33 +01001647 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier7839a052018-07-24 17:13:36 +02001648 (pllcfg[PLLCFG_M] + 1U);
1649
1650 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1651 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1652 return -EINVAL;
1653 }
1654
1655 if ((type == PLL_800) && (refclk >= 8000000U)) {
1656 ifrge = 1U;
1657 }
1658
1659 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1660 RCC_PLLNCFGR1_DIVN_MASK;
1661 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1662 RCC_PLLNCFGR1_DIVM_MASK;
1663 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1664 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautier0d216802019-02-14 10:53:33 +01001665 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001666
1667 /* Fractional configuration */
1668 value = 0;
Yann Gautier0d216802019-02-14 10:53:33 +01001669 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001670
1671 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier0d216802019-02-14 10:53:33 +01001672 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001673
1674 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautier0d216802019-02-14 10:53:33 +01001675 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001676
Yann Gautier0d216802019-02-14 10:53:33 +01001677 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier7839a052018-07-24 17:13:36 +02001678
1679 return 0;
1680}
1681
Yann Gautier0d216802019-02-14 10:53:33 +01001682static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier7839a052018-07-24 17:13:36 +02001683{
Yann Gautier0d216802019-02-14 10:53:33 +01001684 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier7839a052018-07-24 17:13:36 +02001685 uint32_t pllxcsg = 0;
1686
1687 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1688 RCC_PLLNCSGR_MOD_PER_MASK;
1689
1690 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1691 RCC_PLLNCSGR_INC_STEP_MASK;
1692
1693 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1694 RCC_PLLNCSGR_SSCG_MODE_MASK;
1695
Yann Gautier0d216802019-02-14 10:53:33 +01001696 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierdd98aec2019-06-04 15:55:37 +02001697
1698 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1699 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier7839a052018-07-24 17:13:36 +02001700}
1701
Yann Gautier0d216802019-02-14 10:53:33 +01001702static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier7839a052018-07-24 17:13:36 +02001703{
Yann Gautier0d216802019-02-14 10:53:33 +01001704 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautierdfdb0572019-02-14 11:14:39 +01001705 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001706
Yann Gautier0d216802019-02-14 10:53:33 +01001707 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier7839a052018-07-24 17:13:36 +02001708 clksrc & RCC_SELR_SRC_MASK);
1709
Yann Gautierdfdb0572019-02-14 11:14:39 +01001710 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautier0d216802019-02-14 10:53:33 +01001711 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001712 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001713 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1714 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier7839a052018-07-24 17:13:36 +02001715 return -ETIMEDOUT;
1716 }
1717 }
1718
1719 return 0;
1720}
1721
Yann Gautier0d216802019-02-14 10:53:33 +01001722static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier7839a052018-07-24 17:13:36 +02001723{
Yann Gautierdfdb0572019-02-14 11:14:39 +01001724 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001725
1726 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1727 clkdiv & RCC_DIVR_DIV_MASK);
1728
Yann Gautierdfdb0572019-02-14 11:14:39 +01001729 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001730 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001731 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001732 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001733 clkdiv, address, mmio_read_32(address));
1734 return -ETIMEDOUT;
1735 }
1736 }
1737
1738 return 0;
1739}
1740
Yann Gautier0d216802019-02-14 10:53:33 +01001741static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier7839a052018-07-24 17:13:36 +02001742{
Yann Gautier0d216802019-02-14 10:53:33 +01001743 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier7839a052018-07-24 17:13:36 +02001744
1745 /*
1746 * Binding clksrc :
1747 * bit15-4 offset
1748 * bit3: disable
1749 * bit2-0: MCOSEL[2:0]
1750 */
1751 if ((clksrc & 0x8U) != 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001752 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier7839a052018-07-24 17:13:36 +02001753 } else {
Yann Gautier0d216802019-02-14 10:53:33 +01001754 mmio_clrsetbits_32(clksrc_address,
Yann Gautier7839a052018-07-24 17:13:36 +02001755 RCC_MCOCFG_MCOSRC_MASK,
1756 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautier0d216802019-02-14 10:53:33 +01001757 mmio_clrsetbits_32(clksrc_address,
Yann Gautier7839a052018-07-24 17:13:36 +02001758 RCC_MCOCFG_MCODIV_MASK,
1759 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautier0d216802019-02-14 10:53:33 +01001760 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier7839a052018-07-24 17:13:36 +02001761 }
1762}
1763
Yann Gautier0d216802019-02-14 10:53:33 +01001764static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier7839a052018-07-24 17:13:36 +02001765{
Yann Gautier0d216802019-02-14 10:53:33 +01001766 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier7839a052018-07-24 17:13:36 +02001767
1768 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1769 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1770 mmio_clrsetbits_32(address,
1771 RCC_BDCR_RTCSRC_MASK,
Yann Gautier15509092021-04-06 13:41:19 +02001772 (clksrc & RCC_SELR_SRC_MASK) << RCC_BDCR_RTCSRC_SHIFT);
Yann Gautier7839a052018-07-24 17:13:36 +02001773
1774 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1775 }
1776
1777 if (lse_css) {
1778 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1779 }
1780}
1781
Yann Gautier0d216802019-02-14 10:53:33 +01001782static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier7839a052018-07-24 17:13:36 +02001783{
Yann Gautier0d216802019-02-14 10:53:33 +01001784 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier7839a052018-07-24 17:13:36 +02001785 uint32_t value = pkcs & 0xFU;
1786 uint32_t mask = 0xFU;
1787
1788 if ((pkcs & BIT(31)) != 0U) {
1789 mask <<= 4;
1790 value <<= 4;
1791 }
1792
1793 mmio_clrsetbits_32(address, mask, value);
1794}
1795
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001796static int clk_get_pll_settings_from_dt(int plloff, unsigned int *pllcfg,
1797 uint32_t *fracv, uint32_t *csg,
1798 bool *csg_set)
1799{
1800 void *fdt;
1801 int ret;
1802
1803 if (fdt_get_address(&fdt) == 0) {
1804 return -FDT_ERR_NOTFOUND;
1805 }
1806
1807 ret = fdt_read_uint32_array(fdt, plloff, "cfg", (uint32_t)PLLCFG_NB,
1808 pllcfg);
1809 if (ret < 0) {
1810 return -FDT_ERR_NOTFOUND;
1811 }
1812
1813 *fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1814
1815 ret = fdt_read_uint32_array(fdt, plloff, "csg", (uint32_t)PLLCSG_NB,
1816 csg);
1817
1818 *csg_set = (ret == 0);
1819
1820 if (ret == -FDT_ERR_NOTFOUND) {
1821 ret = 0;
1822 }
1823
1824 return ret;
1825}
1826
Yann Gautierf6559222022-09-12 11:26:09 +02001827static int clk_compute_pll1_settings(unsigned long input_freq,
1828 uint32_t freq_khz,
1829 uint32_t *pllcfg, uint32_t *fracv)
1830{
1831 unsigned long long best_diff = ULLONG_MAX;
1832 unsigned int divm;
1833
1834 /* Following parameters have always the same value */
1835 pllcfg[PLLCFG_Q] = 0U;
1836 pllcfg[PLLCFG_R] = 0U;
1837 pllcfg[PLLCFG_O] = PQR(1, 0, 0);
1838
1839 for (divm = (DIVM_MAX + 1U); divm != DIVM_MIN; divm--) {
1840 unsigned long post_divm = input_freq / divm;
1841 unsigned int divp;
1842
1843 if ((post_divm < POST_DIVM_MIN) || (post_divm > POST_DIVM_MAX)) {
1844 continue;
1845 }
1846
1847 for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
1848 unsigned long long output_freq = freq_khz * 1000ULL;
1849 unsigned long long freq;
1850 unsigned long long divn;
1851 unsigned long long frac;
1852 unsigned int i;
1853
1854 freq = output_freq * divm * (divp + 1U);
1855
1856 divn = (freq / input_freq) - 1U;
1857 if ((divn < DIVN_MIN) || (divn > DIVN_MAX)) {
1858 continue;
1859 }
1860
1861 frac = ((freq * FRAC_MAX) / input_freq) - ((divn + 1U) * FRAC_MAX);
1862
1863 /* 2 loops to refine the fractional part */
1864 for (i = 2U; i != 0U; i--) {
1865 unsigned long long diff;
1866 unsigned long long vco;
1867
1868 if (frac > FRAC_MAX) {
1869 break;
1870 }
1871
1872 vco = (post_divm * (divn + 1U)) + ((post_divm * frac) / FRAC_MAX);
1873
1874 if ((vco < (VCO_MIN / 2U)) || (vco > (VCO_MAX / 2U))) {
1875 frac++;
1876 continue;
1877 }
1878
1879 freq = vco / (divp + 1U);
1880 if (output_freq < freq) {
1881 diff = freq - output_freq;
1882 } else {
1883 diff = output_freq - freq;
1884 }
1885
1886 if (diff < best_diff) {
1887 pllcfg[PLLCFG_M] = divm - 1U;
1888 pllcfg[PLLCFG_N] = (uint32_t)divn;
1889 pllcfg[PLLCFG_P] = divp;
1890 *fracv = (uint32_t)frac;
1891
1892 if (diff == 0U) {
1893 return 0;
1894 }
1895
1896 best_diff = diff;
1897 }
1898
1899 frac++;
1900 }
1901 }
1902 }
1903
1904 if (best_diff == ULLONG_MAX) {
1905 return -EINVAL;
1906 }
1907
1908 return 0;
1909}
1910
1911static int clk_get_pll1_settings(uint32_t clksrc, uint32_t freq_khz,
1912 uint32_t *pllcfg, uint32_t *fracv)
1913{
1914 unsigned long input_freq = 0UL;
1915
1916 assert(pllcfg != NULL);
1917 assert(fracv != NULL);
1918
1919 switch (clksrc) {
1920 case CLK_PLL12_HSI:
1921 input_freq = stm32mp_clk_get_rate(CK_HSI);
1922 break;
1923 case CLK_PLL12_HSE:
1924 input_freq = stm32mp_clk_get_rate(CK_HSE);
1925 break;
1926 default:
1927 break;
1928 }
1929
1930 if (input_freq == 0UL) {
1931 panic();
1932 }
1933
1934 return clk_compute_pll1_settings(input_freq, freq_khz, pllcfg, fracv);
1935}
1936
Yann Gautier7839a052018-07-24 17:13:36 +02001937int stm32mp1_clk_init(void)
1938{
Yann Gautier0d216802019-02-14 10:53:33 +01001939 uintptr_t rcc_base = stm32mp_rcc_base();
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001940 uint32_t pllfracv[_PLL_NB];
1941 uint32_t pllcsg[_PLL_NB][PLLCSG_NB];
Yann Gautier7839a052018-07-24 17:13:36 +02001942 unsigned int clksrc[CLKSRC_NB];
1943 unsigned int clkdiv[CLKDIV_NB];
1944 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1945 int plloff[_PLL_NB];
1946 int ret, len;
1947 enum stm32mp1_pll_id i;
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001948 bool pllcsg_set[_PLL_NB];
1949 bool pllcfg_valid[_PLL_NB];
Yann Gautier7839a052018-07-24 17:13:36 +02001950 bool lse_css = false;
Yann Gautier0d216802019-02-14 10:53:33 +01001951 bool pll3_preserve = false;
1952 bool pll4_preserve = false;
1953 bool pll4_bootrom = false;
Yann Gautier3e6fab42018-11-09 15:57:18 +01001954 const fdt32_t *pkcs_cell;
Andre Przywara52a616b2020-03-26 12:51:21 +00001955 void *fdt;
Patrick Delaunaybf1af152020-09-04 17:39:12 +02001956 int stgen_p = stm32mp1_clk_get_parent(STGEN_K);
1957 int usbphy_p = stm32mp1_clk_get_parent(USBPHY_K);
Andre Przywara52a616b2020-03-26 12:51:21 +00001958
1959 if (fdt_get_address(&fdt) == 0) {
Yann Gautier8f97c4f2020-09-16 16:40:34 +02001960 return -FDT_ERR_NOTFOUND;
Andre Przywara52a616b2020-03-26 12:51:21 +00001961 }
Yann Gautier7839a052018-07-24 17:13:36 +02001962
Andre Przywara52a616b2020-03-26 12:51:21 +00001963 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1964 clksrc);
Yann Gautier7839a052018-07-24 17:13:36 +02001965 if (ret < 0) {
1966 return -FDT_ERR_NOTFOUND;
1967 }
1968
Andre Przywara52a616b2020-03-26 12:51:21 +00001969 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1970 clkdiv);
Yann Gautier7839a052018-07-24 17:13:36 +02001971 if (ret < 0) {
1972 return -FDT_ERR_NOTFOUND;
1973 }
1974
1975 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1976 char name[12];
1977
Yann Gautier9fa9a0c2022-02-28 11:34:05 +01001978 snprintf(name, sizeof(name), "st,pll@%u", i);
Yann Gautier7839a052018-07-24 17:13:36 +02001979 plloff[i] = fdt_rcc_subnode_offset(name);
1980
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001981 pllcfg_valid[i] = fdt_check_node(plloff[i]);
Yann Gautierf6559222022-09-12 11:26:09 +02001982 if (pllcfg_valid[i]) {
1983 ret = clk_get_pll_settings_from_dt(plloff[i], pllcfg[i],
1984 &pllfracv[i],
1985 pllcsg[i],
1986 &pllcsg_set[i]);
1987 if (ret != 0) {
1988 return ret;
1989 }
1990
Yann Gautier7839a052018-07-24 17:13:36 +02001991 continue;
1992 }
1993
Yann Gautierf6559222022-09-12 11:26:09 +02001994 if (i == _PLL1) {
1995 ret = clk_get_pll1_settings(clksrc[CLKSRC_PLL12],
1996 PLL1_NOMINAL_FREQ_IN_KHZ,
1997 pllcfg[i], &pllfracv[i]);
1998 if (ret != 0) {
1999 return ret;
2000 }
2001
2002 pllcfg_valid[i] = true;
Yann Gautier7839a052018-07-24 17:13:36 +02002003 }
2004 }
2005
Yann Gautier0d216802019-02-14 10:53:33 +01002006 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
2007 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier7839a052018-07-24 17:13:36 +02002008
2009 /*
2010 * Switch ON oscillator found in device-tree.
2011 * Note: HSI already ON after BootROM stage.
2012 */
Yann Gautier0d216802019-02-14 10:53:33 +01002013 if (stm32mp1_osc[_LSI] != 0U) {
2014 stm32mp1_lsi_set(true);
Yann Gautier7839a052018-07-24 17:13:36 +02002015 }
Yann Gautier0d216802019-02-14 10:53:33 +01002016 if (stm32mp1_osc[_LSE] != 0U) {
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02002017 const char *name = stm32mp_osc_node_label[_LSE];
Yann Gautier0d216802019-02-14 10:53:33 +01002018 bool bypass, digbyp;
Yann Gautier7839a052018-07-24 17:13:36 +02002019 uint32_t lsedrv;
2020
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02002021 bypass = fdt_clk_read_bool(name, "st,bypass");
2022 digbyp = fdt_clk_read_bool(name, "st,digbypass");
2023 lse_css = fdt_clk_read_bool(name, "st,css");
2024 lsedrv = fdt_clk_read_uint32_default(name, "st,drive",
Yann Gautier7839a052018-07-24 17:13:36 +02002025 LSEDRV_MEDIUM_HIGH);
Yann Gautier0d216802019-02-14 10:53:33 +01002026 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier7839a052018-07-24 17:13:36 +02002027 }
Yann Gautier0d216802019-02-14 10:53:33 +01002028 if (stm32mp1_osc[_HSE] != 0U) {
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02002029 const char *name = stm32mp_osc_node_label[_HSE];
Yann Gautier0d216802019-02-14 10:53:33 +01002030 bool bypass, digbyp, css;
Yann Gautier7839a052018-07-24 17:13:36 +02002031
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02002032 bypass = fdt_clk_read_bool(name, "st,bypass");
2033 digbyp = fdt_clk_read_bool(name, "st,digbypass");
2034 css = fdt_clk_read_bool(name, "st,css");
Yann Gautier0d216802019-02-14 10:53:33 +01002035 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier7839a052018-07-24 17:13:36 +02002036 }
2037 /*
2038 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
2039 * => switch on CSI even if node is not present in device tree
2040 */
Yann Gautier0d216802019-02-14 10:53:33 +01002041 stm32mp1_csi_set(true);
Yann Gautier7839a052018-07-24 17:13:36 +02002042
2043 /* Come back to HSI */
Yann Gautier0d216802019-02-14 10:53:33 +01002044 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +02002045 if (ret != 0) {
2046 return ret;
2047 }
Yann Gautier0d216802019-02-14 10:53:33 +01002048 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +02002049 if (ret != 0) {
2050 return ret;
2051 }
Yann Gautierb053a222019-02-15 17:33:27 +01002052 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
2053 if (ret != 0) {
2054 return ret;
2055 }
Yann Gautier7839a052018-07-24 17:13:36 +02002056
Yann Gautier0d216802019-02-14 10:53:33 +01002057 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
2058 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
Yann Gautier175758b2022-03-04 11:08:47 +01002059 if (pllcfg_valid[_PLL3]) {
2060 pll3_preserve =
2061 stm32mp1_check_pll_conf(_PLL3,
Yann Gautier0d216802019-02-14 10:53:33 +01002062 clksrc[CLKSRC_PLL3],
2063 pllcfg[_PLL3],
2064 plloff[_PLL3]);
Yann Gautier175758b2022-03-04 11:08:47 +01002065 }
2066
2067 if (pllcfg_valid[_PLL4]) {
2068 pll4_preserve =
2069 stm32mp1_check_pll_conf(_PLL4,
Yann Gautier0d216802019-02-14 10:53:33 +01002070 clksrc[CLKSRC_PLL4],
2071 pllcfg[_PLL4],
2072 plloff[_PLL4]);
Yann Gautier175758b2022-03-04 11:08:47 +01002073 }
Yann Gautier0d216802019-02-14 10:53:33 +01002074 }
Patrick Delaunaybf1af152020-09-04 17:39:12 +02002075 /* Don't initialize PLL4, when used by BOOTROM */
2076 if ((stm32mp_get_boot_itf_selected() ==
2077 BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) &&
2078 ((stgen_p == (int)_PLL4_R) || (usbphy_p == (int)_PLL4_R))) {
2079 pll4_bootrom = true;
2080 pll4_preserve = true;
2081 }
Yann Gautier0d216802019-02-14 10:53:33 +01002082
Yann Gautier7839a052018-07-24 17:13:36 +02002083 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautier0d216802019-02-14 10:53:33 +01002084 if (((i == _PLL3) && pll3_preserve) ||
2085 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier7839a052018-07-24 17:13:36 +02002086 continue;
Yann Gautier0d216802019-02-14 10:53:33 +01002087 }
2088
2089 ret = stm32mp1_pll_stop(i);
Yann Gautier7839a052018-07-24 17:13:36 +02002090 if (ret != 0) {
2091 return ret;
2092 }
2093 }
2094
2095 /* Configure HSIDIV */
Yann Gautier0d216802019-02-14 10:53:33 +01002096 if (stm32mp1_osc[_HSI] != 0U) {
2097 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier7839a052018-07-24 17:13:36 +02002098 if (ret != 0) {
2099 return ret;
2100 }
Lionel Debieve591d80c2019-12-04 21:50:19 +01002101
2102 stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K));
Yann Gautier7839a052018-07-24 17:13:36 +02002103 }
2104
2105 /* Select DIV */
2106 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautier0d216802019-02-14 10:53:33 +01002107 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier7839a052018-07-24 17:13:36 +02002108 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautier0d216802019-02-14 10:53:33 +01002109 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002110 if (ret != 0) {
2111 return ret;
2112 }
Yann Gautier0d216802019-02-14 10:53:33 +01002113 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002114 if (ret != 0) {
2115 return ret;
2116 }
Yann Gautier0d216802019-02-14 10:53:33 +01002117 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002118 if (ret != 0) {
2119 return ret;
2120 }
Yann Gautierb053a222019-02-15 17:33:27 +01002121 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
2122 if (ret != 0) {
2123 return ret;
2124 }
Yann Gautier0d216802019-02-14 10:53:33 +01002125 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002126 if (ret != 0) {
2127 return ret;
2128 }
Yann Gautier0d216802019-02-14 10:53:33 +01002129 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002130 if (ret != 0) {
2131 return ret;
2132 }
Yann Gautier0d216802019-02-14 10:53:33 +01002133 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002134 if (ret != 0) {
2135 return ret;
2136 }
2137
2138 /* No ready bit for RTC */
Yann Gautier0d216802019-02-14 10:53:33 +01002139 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier7839a052018-07-24 17:13:36 +02002140 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
2141
2142 /* Configure PLLs source */
Yann Gautier0d216802019-02-14 10:53:33 +01002143 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier7839a052018-07-24 17:13:36 +02002144 if (ret != 0) {
2145 return ret;
2146 }
2147
Yann Gautier0d216802019-02-14 10:53:33 +01002148 if (!pll3_preserve) {
2149 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
2150 if (ret != 0) {
2151 return ret;
2152 }
2153 }
2154
2155 if (!pll4_preserve) {
2156 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
2157 if (ret != 0) {
2158 return ret;
2159 }
Yann Gautier7839a052018-07-24 17:13:36 +02002160 }
2161
2162 /* Configure and start PLLs */
2163 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautier0d216802019-02-14 10:53:33 +01002164 if (((i == _PLL3) && pll3_preserve) ||
2165 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
2166 continue;
2167 }
2168
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002169 if (!pllcfg_valid[i]) {
Yann Gautier7839a052018-07-24 17:13:36 +02002170 continue;
2171 }
2172
Yann Gautier0d216802019-02-14 10:53:33 +01002173 if ((i == _PLL4) && pll4_bootrom) {
2174 /* Set output divider if not done by the Bootrom */
2175 stm32mp1_pll_config_output(i, pllcfg[i]);
2176 continue;
2177 }
2178
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002179 ret = stm32mp1_pll_config(i, pllcfg[i], pllfracv[i]);
Yann Gautier7839a052018-07-24 17:13:36 +02002180 if (ret != 0) {
2181 return ret;
2182 }
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002183
2184 if (pllcsg_set[i]) {
2185 stm32mp1_pll_csg(i, pllcsg[i]);
Yann Gautier7839a052018-07-24 17:13:36 +02002186 }
2187
Yann Gautier0d216802019-02-14 10:53:33 +01002188 stm32mp1_pll_start(i);
Yann Gautier7839a052018-07-24 17:13:36 +02002189 }
Elyes Haouas1b491ee2023-02-13 09:14:48 +01002190 /* Wait and start PLLs output when ready */
Yann Gautier7839a052018-07-24 17:13:36 +02002191 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002192 if (!pllcfg_valid[i]) {
Yann Gautier7839a052018-07-24 17:13:36 +02002193 continue;
2194 }
2195
Yann Gautier0d216802019-02-14 10:53:33 +01002196 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier7839a052018-07-24 17:13:36 +02002197 if (ret != 0) {
2198 return ret;
2199 }
2200 }
2201 /* Wait LSE ready before to use it */
Yann Gautier0d216802019-02-14 10:53:33 +01002202 if (stm32mp1_osc[_LSE] != 0U) {
2203 stm32mp1_lse_wait();
Yann Gautier7839a052018-07-24 17:13:36 +02002204 }
2205
2206 /* Configure with expected clock source */
Yann Gautier0d216802019-02-14 10:53:33 +01002207 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier7839a052018-07-24 17:13:36 +02002208 if (ret != 0) {
2209 return ret;
2210 }
Yann Gautier0d216802019-02-14 10:53:33 +01002211 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier7839a052018-07-24 17:13:36 +02002212 if (ret != 0) {
2213 return ret;
2214 }
Yann Gautierb053a222019-02-15 17:33:27 +01002215 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
2216 if (ret != 0) {
2217 return ret;
2218 }
Yann Gautier0d216802019-02-14 10:53:33 +01002219 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier7839a052018-07-24 17:13:36 +02002220
2221 /* Configure PKCK */
2222 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
2223 if (pkcs_cell != NULL) {
2224 bool ckper_disabled = false;
2225 uint32_t j;
Patrick Delaunaybf1af152020-09-04 17:39:12 +02002226 uint32_t usbreg_bootrom = 0U;
2227
2228 if (pll4_bootrom) {
2229 usbreg_bootrom = mmio_read_32(rcc_base + RCC_USBCKSELR);
2230 }
Yann Gautier7839a052018-07-24 17:13:36 +02002231
Yann Gautier7839a052018-07-24 17:13:36 +02002232 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautier3e6fab42018-11-09 15:57:18 +01002233 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier7839a052018-07-24 17:13:36 +02002234
2235 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
2236 ckper_disabled = true;
2237 continue;
2238 }
Yann Gautier0d216802019-02-14 10:53:33 +01002239 stm32mp1_pkcs_config(pkcs);
Yann Gautier7839a052018-07-24 17:13:36 +02002240 }
2241
2242 /*
2243 * CKPER is source for some peripheral clocks
2244 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2245 * only if previous clock is still ON
2246 * => deactivated CKPER only after switching clock
2247 */
2248 if (ckper_disabled) {
Yann Gautier0d216802019-02-14 10:53:33 +01002249 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier7839a052018-07-24 17:13:36 +02002250 }
Patrick Delaunaybf1af152020-09-04 17:39:12 +02002251
2252 if (pll4_bootrom) {
2253 uint32_t usbreg_value, usbreg_mask;
2254 const struct stm32mp1_clk_sel *sel;
2255
2256 sel = clk_sel_ref(_USBPHY_SEL);
2257 usbreg_mask = (uint32_t)sel->msk << sel->src;
2258 sel = clk_sel_ref(_USBO_SEL);
2259 usbreg_mask |= (uint32_t)sel->msk << sel->src;
2260
2261 usbreg_value = mmio_read_32(rcc_base + RCC_USBCKSELR) &
2262 usbreg_mask;
2263 usbreg_bootrom &= usbreg_mask;
2264 if (usbreg_bootrom != usbreg_value) {
2265 VERBOSE("forbidden new USB clk path\n");
2266 VERBOSE("vs bootrom on USB boot\n");
2267 return -FDT_ERR_BADVALUE;
2268 }
2269 }
Yann Gautier7839a052018-07-24 17:13:36 +02002270 }
2271
2272 /* Switch OFF HSI if not found in device-tree */
Yann Gautier0d216802019-02-14 10:53:33 +01002273 if (stm32mp1_osc[_HSI] == 0U) {
2274 stm32mp1_hsi_set(false);
Yann Gautier7839a052018-07-24 17:13:36 +02002275 }
Lionel Debieve591d80c2019-12-04 21:50:19 +01002276
2277 stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K));
Yann Gautier7839a052018-07-24 17:13:36 +02002278
2279 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautier0d216802019-02-14 10:53:33 +01002280 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier7839a052018-07-24 17:13:36 +02002281 RCC_DDRITFCR_DDRCKMOD_MASK,
2282 RCC_DDRITFCR_DDRCKMOD_SSR <<
2283 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2284
2285 return 0;
2286}
2287
2288static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier7839a052018-07-24 17:13:36 +02002289 enum stm32mp_osc_id index)
2290{
2291 uint32_t frequency;
2292
Yann Gautier0d216802019-02-14 10:53:33 +01002293 if (fdt_osc_read_freq(name, &frequency) == 0) {
2294 stm32mp1_osc[index] = frequency;
Yann Gautier7839a052018-07-24 17:13:36 +02002295 }
2296}
2297
2298static void stm32mp1_osc_init(void)
2299{
Yann Gautier7839a052018-07-24 17:13:36 +02002300 enum stm32mp_osc_id i;
2301
2302 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautier0d216802019-02-14 10:53:33 +01002303 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier7839a052018-07-24 17:13:36 +02002304 }
2305}
2306
Etienne Carriere37e82952020-05-13 11:49:49 +02002307#ifdef STM32MP_SHARED_RESOURCES
2308/*
2309 * Get the parent ID of the target parent clock, for tagging as secure
2310 * shared clock dependencies.
2311 */
2312static int get_parent_id_parent(unsigned int parent_id)
2313{
2314 enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
2315 enum stm32mp1_pll_id pll_id;
2316 uint32_t p_sel;
2317 uintptr_t rcc_base = stm32mp_rcc_base();
2318
2319 switch (parent_id) {
2320 case _ACLK:
2321 case _PCLK4:
2322 case _PCLK5:
2323 s = _AXIS_SEL;
2324 break;
2325 case _PLL1_P:
2326 case _PLL1_Q:
2327 case _PLL1_R:
2328 pll_id = _PLL1;
2329 break;
2330 case _PLL2_P:
2331 case _PLL2_Q:
2332 case _PLL2_R:
2333 pll_id = _PLL2;
2334 break;
2335 case _PLL3_P:
2336 case _PLL3_Q:
2337 case _PLL3_R:
2338 pll_id = _PLL3;
2339 break;
2340 case _PLL4_P:
2341 case _PLL4_Q:
2342 case _PLL4_R:
2343 pll_id = _PLL4;
2344 break;
2345 case _PCLK1:
2346 case _PCLK2:
2347 case _HCLK2:
2348 case _HCLK6:
2349 case _CK_PER:
2350 case _CK_MPU:
2351 case _CK_MCU:
2352 case _USB_PHY_48:
2353 /* We do not expect to access these */
2354 panic();
2355 break;
2356 default:
2357 /* Other parents have no parent */
2358 return -1;
2359 }
2360
2361 if (s != _UNKNOWN_SEL) {
2362 const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
2363
2364 p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
2365 sel->msk;
2366
2367 if (p_sel < sel->nb_parent) {
2368 return (int)sel->parent[p_sel];
2369 }
2370 } else {
2371 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
2372
2373 p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
2374 RCC_SELR_REFCLK_SRC_MASK;
2375
2376 if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
2377 return (int)pll->refclk[p_sel];
2378 }
2379 }
2380
2381 VERBOSE("No parent selected for %s\n",
2382 stm32mp1_clk_parent_name[parent_id]);
2383
2384 return -1;
2385}
2386
2387static void secure_parent_clocks(unsigned long parent_id)
2388{
2389 int grandparent_id;
2390
2391 switch (parent_id) {
2392 case _PLL3_P:
2393 case _PLL3_Q:
2394 case _PLL3_R:
2395 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2396 break;
2397
2398 /* These clocks are always secure when RCC is secure */
2399 case _ACLK:
2400 case _HCLK2:
2401 case _HCLK6:
2402 case _PCLK4:
2403 case _PCLK5:
2404 case _PLL1_P:
2405 case _PLL1_Q:
2406 case _PLL1_R:
2407 case _PLL2_P:
2408 case _PLL2_Q:
2409 case _PLL2_R:
2410 case _HSI:
2411 case _HSI_KER:
2412 case _LSI:
2413 case _CSI:
2414 case _CSI_KER:
2415 case _HSE:
2416 case _HSE_KER:
2417 case _HSE_KER_DIV2:
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +02002418 case _HSE_RTC:
Etienne Carriere37e82952020-05-13 11:49:49 +02002419 case _LSE:
2420 break;
2421
2422 default:
2423 VERBOSE("Cannot secure parent clock %s\n",
2424 stm32mp1_clk_parent_name[parent_id]);
2425 panic();
2426 }
2427
2428 grandparent_id = get_parent_id_parent(parent_id);
2429 if (grandparent_id >= 0) {
2430 secure_parent_clocks(grandparent_id);
2431 }
2432}
2433
2434void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
2435{
2436 int parent_id;
2437
2438 if (!stm32mp1_rcc_is_secure()) {
2439 return;
2440 }
2441
2442 switch (clock_id) {
2443 case PLL1:
2444 case PLL2:
2445 /* PLL1/PLL2 are always secure: nothing to do */
2446 break;
2447 case PLL3:
2448 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2449 break;
2450 case PLL4:
2451 ERROR("PLL4 cannot be secured\n");
2452 panic();
2453 break;
2454 default:
2455 /* Others are expected gateable clock */
2456 parent_id = stm32mp1_clk_get_parent(clock_id);
2457 if (parent_id < 0) {
2458 INFO("No parent found for clock %lu\n", clock_id);
2459 } else {
2460 secure_parent_clocks(parent_id);
2461 }
2462 break;
2463 }
2464}
2465#endif /* STM32MP_SHARED_RESOURCES */
2466
Lionel Debieve77b4ca02020-12-15 13:22:27 +01002467void stm32mp1_clk_mcuss_protect(bool enable)
2468{
2469 uintptr_t rcc_base = stm32mp_rcc_base();
2470
2471 if (enable) {
2472 mmio_setbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
2473 } else {
2474 mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
2475 }
2476}
2477
Yann Gautier6cb45f82019-05-20 14:39:26 +02002478static void sync_earlyboot_clocks_state(void)
2479{
Etienne Carriere033b6c32019-12-08 08:23:35 +01002480 unsigned int idx;
2481 const unsigned long secure_enable[] = {
2482 AXIDCG,
2483 BSEC,
2484 DDRC1, DDRC1LP,
2485 DDRC2, DDRC2LP,
2486 DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
2487 DDRPHYC, DDRPHYCLP,
Lionel Debieve373f06b2019-09-02 18:15:45 +02002488 RTCAPB,
Etienne Carriere033b6c32019-12-08 08:23:35 +01002489 TZC1, TZC2,
2490 TZPC,
2491 STGEN_K,
2492 };
2493
2494 for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
2495 stm32mp_clk_enable(secure_enable[idx]);
2496 }
Yann Gautier6cb45f82019-05-20 14:39:26 +02002497}
2498
Yann Gautier33667d22021-08-30 15:06:54 +02002499static const struct clk_ops stm32mp_clk_ops = {
2500 .enable = stm32mp_clk_enable,
2501 .disable = stm32mp_clk_disable,
2502 .is_enabled = stm32mp_clk_is_enabled,
2503 .get_rate = stm32mp_clk_get_rate,
2504 .get_parent = stm32mp1_clk_get_parent,
2505};
2506
Yann Gautier7839a052018-07-24 17:13:36 +02002507int stm32mp1_clk_probe(void)
2508{
Lionel Debieve812daf92020-12-15 10:35:59 +01002509#if defined(IMAGE_BL32)
2510 if (!fdt_get_rcc_secure_state()) {
2511 mmio_write_32(stm32mp_rcc_base() + RCC_TZCR, 0U);
2512 }
2513#endif
2514
Yann Gautier7839a052018-07-24 17:13:36 +02002515 stm32mp1_osc_init();
2516
Yann Gautier6cb45f82019-05-20 14:39:26 +02002517 sync_earlyboot_clocks_state();
2518
Yann Gautier33667d22021-08-30 15:06:54 +02002519 clk_register(&stm32mp_clk_ops);
2520
Yann Gautier7839a052018-07-24 17:13:36 +02002521 return 0;
2522}