blob: c9c3c5f9b49d56025b31bc06311dcb1f68d44b0c [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002 * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved
Yann Gautier9aea69e2018-07-24 17:13:36 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
Yann Gautier9aea69e2018-07-24 17:13:36 +02007#include <assert.h>
Yann Gautier9aea69e2018-07-24 17:13:36 +02008#include <errno.h>
Yann Gautier9aea69e2018-07-24 17:13:36 +02009#include <stdint.h>
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010010#include <stdio.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <arch.h>
13#include <arch_helpers.h>
14#include <common/debug.h>
Andre Przywaracc99f3f2020-03-26 12:51:21 +000015#include <common/fdt_wrappers.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020016#include <drivers/clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/delay_timer.h>
Yann Gautier4d429472019-02-14 11:15:20 +010018#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <drivers/st/stm32mp1_rcc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <dt-bindings/clock/stm32mp1-clksrc.h>
22#include <lib/mmio.h>
Yann Gautiere4a3c352019-02-14 10:53:33 +010023#include <lib/spinlock.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <lib/utils_def.h>
Nicolas Le Bayondab197a2019-11-13 11:46:31 +010025#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <plat/common/platform.h>
27
Nicolas Le Bayondab197a2019-11-13 11:46:31 +010028#include <platform_def.h>
29
Yann Gautier2299d572019-02-14 11:14:39 +010030#define MAX_HSI_HZ 64000000
Yann Gautiere4a3c352019-02-14 10:53:33 +010031#define USB_PHY_48_MHZ 48000000
Yann Gautier9aea69e2018-07-24 17:13:36 +020032
Yann Gautier2299d572019-02-14 11:14:39 +010033#define TIMEOUT_US_200MS U(200000)
34#define TIMEOUT_US_1S U(1000000)
Yann Gautier9aea69e2018-07-24 17:13:36 +020035
Yann Gautier2299d572019-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 Gautier9aea69e2018-07-24 17:13:36 +020041
Yann Gautier5f2e8742019-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 Gautier9aea69e2018-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 Fernandez4e3a51a2021-07-27 15:39:16 +020058 _HSE_RTC,
Yann Gautier9aea69e2018-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 Gautiered342322019-02-15 17:33:27 +010082 _CK_MCU,
Yann Gautiere4a3c352019-02-14 10:53:33 +010083 _USB_PHY_48,
Yann Gautier9aea69e2018-07-24 17:13:36 +020084 _PARENT_NB,
85 _UNKNOWN_ID = 0xff,
86};
87
Yann Gautiere4a3c352019-02-14 10:53:33 +010088/* Lists only the parent clock we are interested in */
Yann Gautier9aea69e2018-07-24 17:13:36 +020089enum stm32mp1_parent_sel {
Yann Gautiere4a3c352019-02-14 10:53:33 +010090 _I2C12_SEL,
91 _I2C35_SEL,
92 _STGEN_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +020093 _I2C46_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010094 _SPI6_SEL,
Yann Gautier9d8bbcd2019-05-07 18:49:33 +020095 _UART1_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010096 _RNG1_SEL,
Yann Gautier9aea69e2018-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 Gautier9d8bbcd2019-05-07 18:49:33 +0200105 _AXIS_SEL,
106 _MCUS_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200107 _USBPHY_SEL,
108 _USBO_SEL,
Etienne Carriere04132612019-12-08 08:20:12 +0100109 _MPU_SEL,
Yann Gautierfaa9bcf2021-08-31 18:23:13 +0200110 _CKPER_SEL,
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100111 _RTC_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200112 _PARENT_SEL_NB,
113 _UNKNOWN_SEL = 0xff,
114};
115
Etienne Carriere04132612019-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 Fernandez4e3a51a2021-07-27 15:39:16 +0200128 [_HSE_RTC] = _UNKNOWN_ID,
Etienne Carriere04132612019-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 Gautier9aea69e2018-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 Gautiered342322019-02-15 17:33:27 +0100184 CLKSRC_MCU,
Yann Gautier9aea69e2018-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 Gautiered342322019-02-15 17:33:27 +0100197 CLKDIV_MCU,
Yann Gautier9aea69e2018-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 Gautier9aea69e2018-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 Gautierb2edbc32021-10-27 18:16:59 +0200242 uint8_t secure;
Yann Gautiere4a3c352019-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 Gautier9aea69e2018-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 Gautiere4a3c352019-02-14 10:53:33 +0100267/* Clocks with selectable source and non set/clr register access */
Yann Gautierb2edbc32021-10-27 18:16:59 +0200268#define _CLK_SELEC(sec, off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200269 { \
270 .offset = (off), \
271 .bit = (b), \
272 .index = (idx), \
273 .set_clr = 0, \
Yann Gautierb2edbc32021-10-27 18:16:59 +0200274 .secure = (sec), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200275 .sel = (s), \
276 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200277 }
278
Yann Gautiere4a3c352019-02-14 10:53:33 +0100279/* Clocks with fixed source and non set/clr register access */
Yann Gautierb2edbc32021-10-27 18:16:59 +0200280#define _CLK_FIXED(sec, off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200281 { \
282 .offset = (off), \
283 .bit = (b), \
284 .index = (idx), \
285 .set_clr = 0, \
Yann Gautierb2edbc32021-10-27 18:16:59 +0200286 .secure = (sec), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200287 .sel = _UNKNOWN_SEL, \
288 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200289 }
290
Yann Gautiere4a3c352019-02-14 10:53:33 +0100291/* Clocks with selectable source and set/clr register access */
Yann Gautierb2edbc32021-10-27 18:16:59 +0200292#define _CLK_SC_SELEC(sec, off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200293 { \
294 .offset = (off), \
295 .bit = (b), \
296 .index = (idx), \
297 .set_clr = 1, \
Yann Gautierb2edbc32021-10-27 18:16:59 +0200298 .secure = (sec), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200299 .sel = (s), \
300 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200301 }
302
Yann Gautiere4a3c352019-02-14 10:53:33 +0100303/* Clocks with fixed source and set/clr register access */
Yann Gautierb2edbc32021-10-27 18:16:59 +0200304#define _CLK_SC_FIXED(sec, off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200305 { \
306 .offset = (off), \
307 .bit = (b), \
308 .index = (idx), \
309 .set_clr = 1, \
Yann Gautierb2edbc32021-10-27 18:16:59 +0200310 .secure = (sec), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200311 .sel = _UNKNOWN_SEL, \
312 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200313 }
314
Yann Gautier9d8bbcd2019-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 Carrierec164ce22019-12-08 08:20:40 +0100319 .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
320 (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200321 .parent = (_parents), \
322 .nb_parent = ARRAY_SIZE(_parents) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200323 }
324
Yann Gautiere4a3c352019-02-14 10:53:33 +0100325#define _CLK_PLL(idx, type, off1, off2, off3, \
326 off4, off5, off6, \
327 p1, p2, p3, p4) \
Yann Gautier9aea69e2018-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 Gautiere4a3c352019-02-14 10:53:33 +0100342#define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
343
Yann Gautierb2edbc32021-10-27 18:16:59 +0200344#define SEC 1
345#define N_S 0
346
Yann Gautier9aea69e2018-07-24 17:13:36 +0200347static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100359
Yann Gautier56729672020-01-17 11:59:28 +0100360#if defined(IMAGE_BL32)
Yann Gautierb2edbc32021-10-27 18:16:59 +0200361 _CLK_SC_FIXED(N_S, RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
Yann Gautier56729672020-01-17 11:59:28 +0100362#endif
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100373
Yann Gautier56729672020-01-17 11:59:28 +0100374#if defined(IMAGE_BL32)
Yann Gautierb2edbc32021-10-27 18:16:59 +0200375 _CLK_SC_FIXED(N_S, RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
Yann Gautier56729672020-01-17 11:59:28 +0100376#endif
Yann Gautierb2edbc32021-10-27 18:16:59 +0200377 _CLK_SC_SELEC(N_S, RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
Yann Gautiere4a3c352019-02-14 10:53:33 +0100378
Yann Gautierb2edbc32021-10-27 18:16:59 +0200379 _CLK_SC_FIXED(N_S, RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),
Yann Gautier3edc7c32019-05-20 19:17:08 +0200380
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100384
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100396
Yann Gautier56729672020-01-17 11:59:28 +0100397#if defined(IMAGE_BL32)
Yann Gautierb2edbc32021-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 Gautier56729672020-01-17 11:59:28 +0100400#endif
Yann Gautiere4a3c352019-02-14 10:53:33 +0100401
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100413
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100419
Yann Gautier56729672020-01-17 11:59:28 +0100420#if defined(IMAGE_BL2)
Yann Gautierb2edbc32021-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 Gautier56729672020-01-17 11:59:28 +0100423#endif
Yann Gautierb2edbc32021-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 Gautier56729672020-01-17 11:59:28 +0100426#if defined(IMAGE_BL32)
Yann Gautierb2edbc32021-10-27 18:16:59 +0200427 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
Yann Gautier56729672020-01-17 11:59:28 +0100428#endif
Yann Gautiere4a3c352019-02-14 10:53:33 +0100429
Yann Gautierb2edbc32021-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 Gautiere4a3c352019-02-14 10:53:33 +0100432};
433
434static 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};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200453
Yann Gautiere4a3c352019-02-14 10:53:33 +0100454static const uint8_t usart1_parents[] = {
455 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
456};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200457
Yann Gautiere4a3c352019-02-14 10:53:33 +0100458static const uint8_t rng1_parents[] = {
459 _CSI, _PLL4_R, _LSE, _LSI
460};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200461
Yann Gautiere4a3c352019-02-14 10:53:33 +0100462static const uint8_t uart6_parents[] = {
463 _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
464};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200465
Yann Gautiere4a3c352019-02-14 10:53:33 +0100466static const uint8_t uart234578_parents[] = {
467 _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
468};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200469
Yann Gautiere4a3c352019-02-14 10:53:33 +0100470static const uint8_t sdmmc12_parents[] = {
471 _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
472};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200473
Yann Gautiere4a3c352019-02-14 10:53:33 +0100474static const uint8_t sdmmc3_parents[] = {
475 _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
476};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200477
Yann Gautiere4a3c352019-02-14 10:53:33 +0100478static const uint8_t qspi_parents[] = {
479 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
480};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200481
Yann Gautiere4a3c352019-02-14 10:53:33 +0100482static const uint8_t fmc_parents[] = {
483 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
484};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200485
Etienne Carriere40c28e82019-12-19 10:03:23 +0100486static const uint8_t axiss_parents[] = {
487 _HSI, _HSE, _PLL2_P
Yann Gautier9aea69e2018-07-24 17:13:36 +0200488};
489
Etienne Carriere40c28e82019-12-19 10:03:23 +0100490static const uint8_t mcuss_parents[] = {
491 _HSI, _HSE, _CSI, _PLL3_P
Yann Gautiered342322019-02-15 17:33:27 +0100492};
493
Yann Gautiere4a3c352019-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 Gautier9aea69e2018-07-24 17:13:36 +0200501
Etienne Carriere04132612019-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 Carrierebccc7d02019-12-08 08:22:31 +0100510static const uint8_t rtc_parents[] = {
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +0200511 _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100512};
513
Yann Gautier9aea69e2018-07-24 17:13:36 +0200514static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
Yann Gautier9d8bbcd2019-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 Carriere04132612019-12-08 08:20:12 +0100522 _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
Yann Gautierfaa9bcf2021-08-31 18:23:13 +0200523 _CLK_PARENT_SEL(CKPER, RCC_CPERCKSELR, per_parents),
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100524 _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
Yann Gautier9d8bbcd2019-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 Carriere40c28e82019-12-19 10:03:23 +0100533 _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, axiss_parents),
534 _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mcuss_parents),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200535 _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
536 _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200537};
538
539/* Define characteristic of PLL according type */
540#define DIVN_MIN 24
541static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
542 [PLL_800] = {
543 .refclk_min = 4,
544 .refclk_max = 16,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200545 },
546 [PLL_1600] = {
547 .refclk_min = 8,
548 .refclk_max = 16,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200549 },
550};
551
552/* PLLNCFGR2 register divider by output */
553static const uint8_t pllncfgr2[_DIV_NB] = {
554 [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
555 [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautiere4a3c352019-02-14 10:53:33 +0100556 [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200557};
558
559static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100560 _CLK_PLL(_PLL1, PLL_1600,
561 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
562 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
563 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
564 _CLK_PLL(_PLL2, PLL_1600,
565 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
566 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
567 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
568 _CLK_PLL(_PLL3, PLL_800,
569 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
570 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
571 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
572 _CLK_PLL(_PLL4, PLL_800,
573 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
574 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
575 _HSI, _HSE, _CSI, _I2S_CKIN),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200576};
577
578/* Prescaler table lookups for clock computation */
Yann Gautiered342322019-02-15 17:33:27 +0100579/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
580static const uint8_t stm32mp1_mcu_div[16] = {
581 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
582};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200583
584/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
585#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
586#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
587static const uint8_t stm32mp1_mpu_apbx_div[8] = {
588 0, 1, 2, 3, 4, 4, 4, 4
589};
590
591/* div = /1 /2 /3 /4 */
592static const uint8_t stm32mp1_axi_div[8] = {
593 1, 2, 3, 4, 4, 4, 4, 4
594};
595
Etienne Carriere1368ada2020-05-13 11:49:49 +0200596static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
597 [_HSI] = "HSI",
598 [_HSE] = "HSE",
599 [_CSI] = "CSI",
600 [_LSI] = "LSI",
601 [_LSE] = "LSE",
602 [_I2S_CKIN] = "I2S_CKIN",
603 [_HSI_KER] = "HSI_KER",
604 [_HSE_KER] = "HSE_KER",
605 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +0200606 [_HSE_RTC] = "HSE_RTC",
Etienne Carriere1368ada2020-05-13 11:49:49 +0200607 [_CSI_KER] = "CSI_KER",
608 [_PLL1_P] = "PLL1_P",
609 [_PLL1_Q] = "PLL1_Q",
610 [_PLL1_R] = "PLL1_R",
611 [_PLL2_P] = "PLL2_P",
612 [_PLL2_Q] = "PLL2_Q",
613 [_PLL2_R] = "PLL2_R",
614 [_PLL3_P] = "PLL3_P",
615 [_PLL3_Q] = "PLL3_Q",
616 [_PLL3_R] = "PLL3_R",
617 [_PLL4_P] = "PLL4_P",
618 [_PLL4_Q] = "PLL4_Q",
619 [_PLL4_R] = "PLL4_R",
620 [_ACLK] = "ACLK",
621 [_PCLK1] = "PCLK1",
622 [_PCLK2] = "PCLK2",
623 [_PCLK3] = "PCLK3",
624 [_PCLK4] = "PCLK4",
625 [_PCLK5] = "PCLK5",
626 [_HCLK6] = "KCLK6",
627 [_HCLK2] = "HCLK2",
628 [_CK_PER] = "CK_PER",
629 [_CK_MPU] = "CK_MPU",
630 [_CK_MCU] = "CK_MCU",
631 [_USB_PHY_48] = "USB_PHY_48",
632};
633
Yann Gautiere4a3c352019-02-14 10:53:33 +0100634/* RCC clock device driver private */
635static unsigned long stm32mp1_osc[NB_OSC];
636static struct spinlock reg_lock;
637static unsigned int gate_refcounts[NB_GATES];
638static struct spinlock refcount_lock;
639
640static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
641{
642 return &stm32mp1_clk_gate[idx];
643}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200644
Yann Gautieraf1e8f42021-10-27 18:21:11 +0200645#if defined(IMAGE_BL32)
646static bool gate_is_non_secure(const struct stm32mp1_clk_gate *gate)
647{
648 return gate->secure == N_S;
649}
650#endif
651
Yann Gautiere4a3c352019-02-14 10:53:33 +0100652static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
653{
654 return &stm32mp1_clk_sel[idx];
655}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200656
Yann Gautiere4a3c352019-02-14 10:53:33 +0100657static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
658{
659 return &stm32mp1_clk_pll[idx];
660}
661
Yann Gautiere4a3c352019-02-14 10:53:33 +0100662static void stm32mp1_clk_lock(struct spinlock *lock)
663{
Yann Gautierf540a592019-05-22 19:13:51 +0200664 if (stm32mp_lock_available()) {
665 /* Assume interrupts are masked */
666 spin_lock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100667 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100668}
669
670static void stm32mp1_clk_unlock(struct spinlock *lock)
671{
Yann Gautierf540a592019-05-22 19:13:51 +0200672 if (stm32mp_lock_available()) {
673 spin_unlock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100674 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100675}
676
677bool stm32mp1_rcc_is_secure(void)
678{
679 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100680 uint32_t mask = RCC_TZCR_TZEN;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100681
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100682 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100683}
684
Yann Gautiered342322019-02-15 17:33:27 +0100685bool stm32mp1_rcc_is_mckprot(void)
686{
687 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100688 uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT;
Yann Gautiered342322019-02-15 17:33:27 +0100689
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100690 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautiered342322019-02-15 17:33:27 +0100691}
692
Yann Gautiere4a3c352019-02-14 10:53:33 +0100693void stm32mp1_clk_rcc_regs_lock(void)
694{
695 stm32mp1_clk_lock(&reg_lock);
696}
697
698void stm32mp1_clk_rcc_regs_unlock(void)
699{
700 stm32mp1_clk_unlock(&reg_lock);
701}
702
703static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200704{
705 if (idx >= NB_OSC) {
706 return 0;
707 }
708
Yann Gautiere4a3c352019-02-14 10:53:33 +0100709 return stm32mp1_osc[idx];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200710}
711
Yann Gautiere4a3c352019-02-14 10:53:33 +0100712static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200713{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100714 unsigned int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200715
Yann Gautiere4a3c352019-02-14 10:53:33 +0100716 for (i = 0U; i < NB_GATES; i++) {
717 if (gate_ref(i)->index == id) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200718 return i;
719 }
720 }
721
Yann Gautierc9343812021-09-07 09:05:44 +0200722 ERROR("%s: clk id %lu not found\n", __func__, id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200723
724 return -EINVAL;
725}
726
Yann Gautiere4a3c352019-02-14 10:53:33 +0100727static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200728{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100729 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200730}
731
Yann Gautiere4a3c352019-02-14 10:53:33 +0100732static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200733{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100734 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200735}
736
Yann Gautiere4a3c352019-02-14 10:53:33 +0100737static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200738{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100739 const struct stm32mp1_clk_sel *sel;
Etienne Carriere04132612019-12-08 08:20:12 +0100740 uint32_t p_sel;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200741 int i;
742 enum stm32mp1_parent_id p;
743 enum stm32mp1_parent_sel s;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100744 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200745
Etienne Carriere04132612019-12-08 08:20:12 +0100746 /* Few non gateable clock have a static parent ID, find them */
747 i = (int)clock_id2parent_id(id);
748 if (i != _UNKNOWN_ID) {
749 return i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200750 }
751
Yann Gautiere4a3c352019-02-14 10:53:33 +0100752 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200753 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100754 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200755 }
756
Yann Gautiere4a3c352019-02-14 10:53:33 +0100757 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200758 if (p < _PARENT_NB) {
759 return (int)p;
760 }
761
Yann Gautiere4a3c352019-02-14 10:53:33 +0100762 s = stm32mp1_clk_get_sel(i);
763 if (s == _UNKNOWN_SEL) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200764 return -EINVAL;
765 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100766 if (s >= _PARENT_SEL_NB) {
767 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200768 }
769
Yann Gautiere4a3c352019-02-14 10:53:33 +0100770 sel = clk_sel_ref(s);
Etienne Carrierec164ce22019-12-08 08:20:40 +0100771 p_sel = (mmio_read_32(rcc_base + sel->offset) &
772 (sel->msk << sel->src)) >> sel->src;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100773 if (p_sel < sel->nb_parent) {
774 return (int)sel->parent[p_sel];
775 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200776
777 return -EINVAL;
778}
779
Yann Gautiere4a3c352019-02-14 10:53:33 +0100780static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200781{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100782 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
783 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200784
Yann Gautiere4a3c352019-02-14 10:53:33 +0100785 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200786}
787
788/*
789 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
790 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
791 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
792 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
793 */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100794static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200795{
Yann Gautier9aea69e2018-07-24 17:13:36 +0200796 unsigned long refclk, fvco;
797 uint32_t cfgr1, fracr, divm, divn;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100798 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200799
Yann Gautiere4a3c352019-02-14 10:53:33 +0100800 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
801 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200802
803 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
804 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
805
Yann Gautiere4a3c352019-02-14 10:53:33 +0100806 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200807
808 /*
809 * With FRACV :
810 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
811 * Without FRACV
812 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
813 */
814 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100815 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
816 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200817 unsigned long long numerator, denominator;
818
Yann Gautiere4a3c352019-02-14 10:53:33 +0100819 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
820 numerator = refclk * numerator;
821 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200822 fvco = (unsigned long)(numerator / denominator);
823 } else {
824 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
825 }
826
827 return fvco;
828}
829
Yann Gautiere4a3c352019-02-14 10:53:33 +0100830static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200831 enum stm32mp1_div_id div_id)
832{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100833 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200834 unsigned long dfout;
835 uint32_t cfgr2, divy;
836
837 if (div_id >= _DIV_NB) {
838 return 0;
839 }
840
Yann Gautiere4a3c352019-02-14 10:53:33 +0100841 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200842 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
843
Yann Gautiere4a3c352019-02-14 10:53:33 +0100844 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200845
846 return dfout;
847}
848
Yann Gautiere4a3c352019-02-14 10:53:33 +0100849static unsigned long get_clock_rate(int p)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200850{
851 uint32_t reg, clkdiv;
852 unsigned long clock = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100853 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200854
855 switch (p) {
856 case _CK_MPU:
857 /* MPU sub system */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100858 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200859 switch (reg & RCC_SELR_SRC_MASK) {
860 case RCC_MPCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100861 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200862 break;
863 case RCC_MPCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100864 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200865 break;
866 case RCC_MPCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100867 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200868 break;
869 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100870 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200871
Yann Gautiere4a3c352019-02-14 10:53:33 +0100872 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200873 clkdiv = reg & RCC_MPUDIV_MASK;
Gabriel Fernandez4d198742020-02-28 09:09:06 +0100874 clock >>= stm32mp1_mpu_div[clkdiv];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200875 break;
876 default:
877 break;
878 }
879 break;
880 /* AXI sub system */
881 case _ACLK:
882 case _HCLK2:
883 case _HCLK6:
884 case _PCLK4:
885 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100886 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200887 switch (reg & RCC_SELR_SRC_MASK) {
888 case RCC_ASSCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100889 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200890 break;
891 case RCC_ASSCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100892 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200893 break;
894 case RCC_ASSCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100895 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200896 break;
897 default:
898 break;
899 }
900
901 /* System clock divider */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100902 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200903 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
904
905 switch (p) {
906 case _PCLK4:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100907 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200908 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
909 break;
910 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100911 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200912 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
913 break;
914 default:
915 break;
916 }
917 break;
Yann Gautiered342322019-02-15 17:33:27 +0100918 /* MCU sub system */
919 case _CK_MCU:
920 case _PCLK1:
921 case _PCLK2:
922 case _PCLK3:
923 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
924 switch (reg & RCC_SELR_SRC_MASK) {
925 case RCC_MSSCKSELR_HSI:
926 clock = stm32mp1_clk_get_fixed(_HSI);
927 break;
928 case RCC_MSSCKSELR_HSE:
929 clock = stm32mp1_clk_get_fixed(_HSE);
930 break;
931 case RCC_MSSCKSELR_CSI:
932 clock = stm32mp1_clk_get_fixed(_CSI);
933 break;
934 case RCC_MSSCKSELR_PLL:
935 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
936 break;
937 default:
938 break;
939 }
940
941 /* MCU clock divider */
942 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
943 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
944
945 switch (p) {
946 case _PCLK1:
947 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
948 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
949 break;
950 case _PCLK2:
951 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
952 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
953 break;
954 case _PCLK3:
955 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
956 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
957 break;
958 case _CK_MCU:
959 default:
960 break;
961 }
962 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200963 case _CK_PER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100964 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200965 switch (reg & RCC_SELR_SRC_MASK) {
966 case RCC_CPERCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100967 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200968 break;
969 case RCC_CPERCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100970 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200971 break;
972 case RCC_CPERCKSELR_CSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100973 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200974 break;
975 default:
976 break;
977 }
978 break;
979 case _HSI:
980 case _HSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100981 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200982 break;
983 case _CSI:
984 case _CSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100985 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200986 break;
987 case _HSE:
988 case _HSE_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100989 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200990 break;
991 case _HSE_KER_DIV2:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100992 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200993 break;
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +0200994 case _HSE_RTC:
995 clock = stm32mp1_clk_get_fixed(_HSE);
996 clock /= (mmio_read_32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U;
997 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200998 case _LSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100999 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001000 break;
1001 case _LSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001002 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001003 break;
1004 /* PLL */
1005 case _PLL1_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001006 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001007 break;
1008 case _PLL1_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001009 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001010 break;
1011 case _PLL1_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001012 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001013 break;
1014 case _PLL2_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001015 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001016 break;
1017 case _PLL2_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001018 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001019 break;
1020 case _PLL2_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001021 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001022 break;
1023 case _PLL3_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001024 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001025 break;
1026 case _PLL3_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001027 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001028 break;
1029 case _PLL3_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001030 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001031 break;
1032 case _PLL4_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001033 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001034 break;
1035 case _PLL4_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001036 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001037 break;
1038 case _PLL4_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001039 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001040 break;
1041 /* Other */
1042 case _USB_PHY_48:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001043 clock = USB_PHY_48_MHZ;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001044 break;
1045 default:
1046 break;
1047 }
1048
1049 return clock;
1050}
1051
Yann Gautiere4a3c352019-02-14 10:53:33 +01001052static void __clk_enable(struct stm32mp1_clk_gate const *gate)
1053{
1054 uintptr_t rcc_base = stm32mp_rcc_base();
1055
Etienne Carriere8a668892019-12-08 08:21:08 +01001056 VERBOSE("Enable clock %u\n", gate->index);
1057
Yann Gautiere4a3c352019-02-14 10:53:33 +01001058 if (gate->set_clr != 0U) {
1059 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
1060 } else {
1061 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
1062 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001063}
1064
1065static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1066{
1067 uintptr_t rcc_base = stm32mp_rcc_base();
1068
Etienne Carriere8a668892019-12-08 08:21:08 +01001069 VERBOSE("Disable clock %u\n", gate->index);
1070
Yann Gautiere4a3c352019-02-14 10:53:33 +01001071 if (gate->set_clr != 0U) {
1072 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1073 BIT(gate->bit));
1074 } else {
1075 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1076 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001077}
1078
1079static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1080{
1081 uintptr_t rcc_base = stm32mp_rcc_base();
1082
1083 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1084}
1085
Etienne Carriere481aa002019-12-08 08:21:44 +01001086/* Oscillators and PLLs are not gated at runtime */
1087static bool clock_is_always_on(unsigned long id)
1088{
1089 switch (id) {
1090 case CK_HSE:
1091 case CK_CSI:
1092 case CK_LSI:
1093 case CK_LSE:
1094 case CK_HSI:
1095 case CK_HSE_DIV2:
1096 case PLL1_Q:
1097 case PLL1_R:
1098 case PLL2_P:
1099 case PLL2_Q:
1100 case PLL2_R:
1101 case PLL3_P:
1102 case PLL3_Q:
1103 case PLL3_R:
Yann Gautierb39a1522020-09-16 16:41:55 +02001104 case CK_AXI:
1105 case CK_MPU:
1106 case CK_MCU:
HE Shushanc47c8162021-07-12 23:04:10 +02001107 case RTC:
Etienne Carriere481aa002019-12-08 08:21:44 +01001108 return true;
1109 default:
1110 return false;
1111 }
1112}
1113
Yann Gautierad730b52022-01-19 13:57:49 +01001114static void __stm32mp1_clk_enable(unsigned long id, bool with_refcnt)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001115{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001116 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001117 int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001118
Etienne Carriere481aa002019-12-08 08:21:44 +01001119 if (clock_is_always_on(id)) {
1120 return;
1121 }
1122
1123 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001124 if (i < 0) {
Yann Gautierc9343812021-09-07 09:05:44 +02001125 ERROR("Clock %lu can't be enabled\n", id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001126 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001127 }
1128
Yann Gautiere4a3c352019-02-14 10:53:33 +01001129 gate = gate_ref(i);
Yann Gautierad730b52022-01-19 13:57:49 +01001130
1131 if (!with_refcnt) {
1132 __clk_enable(gate);
1133 return;
1134 }
Yann Gautieraf1e8f42021-10-27 18:21:11 +02001135
1136#if defined(IMAGE_BL32)
1137 if (gate_is_non_secure(gate)) {
1138 /* Enable non-secure clock w/o any refcounting */
1139 __clk_enable(gate);
1140 return;
1141 }
1142#endif
Yann Gautiere4a3c352019-02-14 10:53:33 +01001143
1144 stm32mp1_clk_lock(&refcount_lock);
1145
Yann Gautierad730b52022-01-19 13:57:49 +01001146 if (gate_refcounts[i] == 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001147 __clk_enable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001148 }
1149
Yann Gautierad730b52022-01-19 13:57:49 +01001150 gate_refcounts[i]++;
1151 if (gate_refcounts[i] == UINT_MAX) {
1152 ERROR("Clock %lu refcount reached max value\n", id);
1153 panic();
1154 }
1155
Yann Gautiere4a3c352019-02-14 10:53:33 +01001156 stm32mp1_clk_unlock(&refcount_lock);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001157}
1158
Yann Gautierad730b52022-01-19 13:57:49 +01001159static void __stm32mp1_clk_disable(unsigned long id, bool with_refcnt)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001160{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001161 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001162 int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001163
Etienne Carriere481aa002019-12-08 08:21:44 +01001164 if (clock_is_always_on(id)) {
1165 return;
1166 }
1167
1168 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001169 if (i < 0) {
Yann Gautierc9343812021-09-07 09:05:44 +02001170 ERROR("Clock %lu can't be disabled\n", id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001171 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001172 }
1173
Yann Gautiere4a3c352019-02-14 10:53:33 +01001174 gate = gate_ref(i);
Yann Gautierad730b52022-01-19 13:57:49 +01001175
1176 if (!with_refcnt) {
1177 __clk_disable(gate);
1178 return;
1179 }
Yann Gautieraf1e8f42021-10-27 18:21:11 +02001180
1181#if defined(IMAGE_BL32)
1182 if (gate_is_non_secure(gate)) {
1183 /* Don't disable non-secure clocks */
1184 return;
1185 }
1186#endif
Yann Gautiere4a3c352019-02-14 10:53:33 +01001187
1188 stm32mp1_clk_lock(&refcount_lock);
1189
Yann Gautierad730b52022-01-19 13:57:49 +01001190 if (gate_refcounts[i] == 0U) {
1191 ERROR("Clock %lu refcount reached 0\n", id);
1192 panic();
1193 }
1194 gate_refcounts[i]--;
1195
1196 if (gate_refcounts[i] == 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001197 __clk_disable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001198 }
1199
Yann Gautiere4a3c352019-02-14 10:53:33 +01001200 stm32mp1_clk_unlock(&refcount_lock);
1201}
1202
Yann Gautiera205a5c2021-08-30 15:06:54 +02001203static int stm32mp_clk_enable(unsigned long id)
Yann Gautiere4a3c352019-02-14 10:53:33 +01001204{
1205 __stm32mp1_clk_enable(id, true);
Yann Gautiera205a5c2021-08-30 15:06:54 +02001206
1207 return 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001208}
1209
Yann Gautiera205a5c2021-08-30 15:06:54 +02001210static void stm32mp_clk_disable(unsigned long id)
Yann Gautiere4a3c352019-02-14 10:53:33 +01001211{
1212 __stm32mp1_clk_disable(id, true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001213}
1214
Yann Gautiera205a5c2021-08-30 15:06:54 +02001215static bool stm32mp_clk_is_enabled(unsigned long id)
Yann Gautiere4a3c352019-02-14 10:53:33 +01001216{
Etienne Carriere481aa002019-12-08 08:21:44 +01001217 int i;
1218
1219 if (clock_is_always_on(id)) {
1220 return true;
1221 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001222
Etienne Carriere481aa002019-12-08 08:21:44 +01001223 i = stm32mp1_clk_get_gated_id(id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001224 if (i < 0) {
1225 panic();
1226 }
1227
1228 return __clk_is_enabled(gate_ref(i));
1229}
1230
Yann Gautiera205a5c2021-08-30 15:06:54 +02001231static unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001232{
Yann Gautiera205a5c2021-08-30 15:06:54 +02001233 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautiere4a3c352019-02-14 10:53:33 +01001234 int p = stm32mp1_clk_get_parent(id);
Yann Gautiera205a5c2021-08-30 15:06:54 +02001235 uint32_t prescaler, timpre;
1236 unsigned long parent_rate;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001237
1238 if (p < 0) {
1239 return 0;
1240 }
1241
Yann Gautiera205a5c2021-08-30 15:06:54 +02001242 parent_rate = get_clock_rate(p);
1243
1244 switch (id) {
1245 case TIM2_K:
1246 case TIM3_K:
1247 case TIM4_K:
1248 case TIM5_K:
1249 case TIM6_K:
1250 case TIM7_K:
1251 case TIM12_K:
1252 case TIM13_K:
1253 case TIM14_K:
1254 prescaler = mmio_read_32(rcc_base + RCC_APB1DIVR) &
1255 RCC_APBXDIV_MASK;
1256 timpre = mmio_read_32(rcc_base + RCC_TIMG1PRER) &
1257 RCC_TIMGXPRER_TIMGXPRE;
1258 break;
1259
1260 case TIM1_K:
1261 case TIM8_K:
1262 case TIM15_K:
1263 case TIM16_K:
1264 case TIM17_K:
1265 prescaler = mmio_read_32(rcc_base + RCC_APB2DIVR) &
1266 RCC_APBXDIV_MASK;
1267 timpre = mmio_read_32(rcc_base + RCC_TIMG2PRER) &
1268 RCC_TIMGXPRER_TIMGXPRE;
1269 break;
1270
1271 default:
1272 return parent_rate;
1273 }
1274
1275 if (prescaler == 0U) {
1276 return parent_rate;
1277 }
1278
1279 return parent_rate * (timpre + 1U) * 2U;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001280}
1281
Yann Gautiere4a3c352019-02-14 10:53:33 +01001282static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001283{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001284 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001285
Yann Gautiere4a3c352019-02-14 10:53:33 +01001286 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001287 mmio_setbits_32(address, mask_on);
1288 } else {
1289 mmio_clrbits_32(address, mask_on);
1290 }
1291}
1292
Yann Gautiere4a3c352019-02-14 10:53:33 +01001293static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001294{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001295 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1296 uintptr_t address = stm32mp_rcc_base() + offset;
1297
1298 mmio_write_32(address, mask_on);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001299}
1300
Yann Gautiere4a3c352019-02-14 10:53:33 +01001301static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001302{
Yann Gautier2299d572019-02-14 11:14:39 +01001303 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001304 uint32_t mask_test;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001305 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001306
Yann Gautiere4a3c352019-02-14 10:53:33 +01001307 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001308 mask_test = mask_rdy;
1309 } else {
1310 mask_test = 0;
1311 }
1312
Yann Gautier2299d572019-02-14 11:14:39 +01001313 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001314 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautier2299d572019-02-14 11:14:39 +01001315 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001316 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001317 mask_rdy, address, enable, mmio_read_32(address));
1318 return -ETIMEDOUT;
1319 }
1320 }
1321
1322 return 0;
1323}
1324
Yann Gautiere4a3c352019-02-14 10:53:33 +01001325static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001326{
1327 uint32_t value;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001328 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001329
Yann Gautiere4a3c352019-02-14 10:53:33 +01001330 if (digbyp) {
1331 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001332 }
1333
Yann Gautiere4a3c352019-02-14 10:53:33 +01001334 if (bypass || digbyp) {
1335 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1336 }
1337
Yann Gautier9aea69e2018-07-24 17:13:36 +02001338 /*
1339 * Warning: not recommended to switch directly from "high drive"
1340 * to "medium low drive", and vice-versa.
1341 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001342 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier9aea69e2018-07-24 17:13:36 +02001343 RCC_BDCR_LSEDRV_SHIFT;
1344
1345 while (value != lsedrv) {
1346 if (value > lsedrv) {
1347 value--;
1348 } else {
1349 value++;
1350 }
1351
Yann Gautiere4a3c352019-02-14 10:53:33 +01001352 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001353 RCC_BDCR_LSEDRV_MASK,
1354 value << RCC_BDCR_LSEDRV_SHIFT);
1355 }
1356
Yann Gautiere4a3c352019-02-14 10:53:33 +01001357 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001358}
1359
Yann Gautiere4a3c352019-02-14 10:53:33 +01001360static void stm32mp1_lse_wait(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001361{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001362 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001363 VERBOSE("%s: failed\n", __func__);
1364 }
1365}
1366
Yann Gautiere4a3c352019-02-14 10:53:33 +01001367static void stm32mp1_lsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001368{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001369 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1370
1371 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001372 VERBOSE("%s: failed\n", __func__);
1373 }
1374}
1375
Yann Gautiere4a3c352019-02-14 10:53:33 +01001376static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001377{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001378 uintptr_t rcc_base = stm32mp_rcc_base();
1379
1380 if (digbyp) {
1381 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001382 }
1383
Yann Gautiere4a3c352019-02-14 10:53:33 +01001384 if (bypass || digbyp) {
1385 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1386 }
1387
1388 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1389 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001390 VERBOSE("%s: failed\n", __func__);
1391 }
1392
1393 if (css) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001394 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001395 }
Lionel Debieved78bd852019-07-02 18:03:34 +02001396
1397#if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
1398 if ((mmio_read_32(rcc_base + RCC_OCENSETR) & RCC_OCENR_HSEBYP) &&
1399 (!(digbyp || bypass))) {
1400 panic();
1401 }
1402#endif
Yann Gautier9aea69e2018-07-24 17:13:36 +02001403}
1404
Yann Gautiere4a3c352019-02-14 10:53:33 +01001405static void stm32mp1_csi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001406{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001407 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1408 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001409 VERBOSE("%s: failed\n", __func__);
1410 }
1411}
1412
Yann Gautiere4a3c352019-02-14 10:53:33 +01001413static void stm32mp1_hsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001414{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001415 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1416 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001417 VERBOSE("%s: failed\n", __func__);
1418 }
1419}
1420
Yann Gautiere4a3c352019-02-14 10:53:33 +01001421static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001422{
Yann Gautier2299d572019-02-14 11:14:39 +01001423 uint64_t timeout;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001424 uintptr_t rcc_base = stm32mp_rcc_base();
1425 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001426
Yann Gautiere4a3c352019-02-14 10:53:33 +01001427 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001428 RCC_HSICFGR_HSIDIV_MASK,
1429 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1430
Yann Gautier2299d572019-02-14 11:14:39 +01001431 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001432 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001433 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001434 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001435 address, mmio_read_32(address));
1436 return -ETIMEDOUT;
1437 }
1438 }
1439
1440 return 0;
1441}
1442
Yann Gautiere4a3c352019-02-14 10:53:33 +01001443static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001444{
1445 uint8_t hsidiv;
1446 uint32_t hsidivfreq = MAX_HSI_HZ;
1447
1448 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1449 if (hsidivfreq == hsifreq) {
1450 break;
1451 }
1452
1453 hsidivfreq /= 2U;
1454 }
1455
1456 if (hsidiv == 4U) {
1457 ERROR("Invalid clk-hsi frequency\n");
1458 return -1;
1459 }
1460
1461 if (hsidiv != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001462 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001463 }
1464
1465 return 0;
1466}
1467
Yann Gautiere4a3c352019-02-14 10:53:33 +01001468static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1469 unsigned int clksrc,
1470 uint32_t *pllcfg, int plloff)
1471{
1472 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1473 uintptr_t rcc_base = stm32mp_rcc_base();
1474 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1475 enum stm32mp1_plltype type = pll->plltype;
1476 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1477 unsigned long refclk;
1478 uint32_t ifrge = 0U;
Andre Przywara2d5690c2020-03-26 11:50:33 +00001479 uint32_t src, value, fracv = 0;
1480 void *fdt;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001481
1482 /* Check PLL output */
1483 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1484 return false;
1485 }
1486
1487 /* Check current clksrc */
1488 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1489 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1490 return false;
1491 }
1492
1493 /* Check Div */
1494 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1495
1496 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1497 (pllcfg[PLLCFG_M] + 1U);
1498
1499 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1500 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1501 return false;
1502 }
1503
1504 if ((type == PLL_800) && (refclk >= 8000000U)) {
1505 ifrge = 1U;
1506 }
1507
1508 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1509 RCC_PLLNCFGR1_DIVN_MASK;
1510 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1511 RCC_PLLNCFGR1_DIVM_MASK;
1512 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1513 RCC_PLLNCFGR1_IFRGE_MASK;
1514 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1515 return false;
1516 }
1517
1518 /* Fractional configuration */
Andre Przywara2d5690c2020-03-26 11:50:33 +00001519 if (fdt_get_address(&fdt) == 1) {
1520 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1521 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001522
1523 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1524 value |= RCC_PLLNFRACR_FRACLE;
1525 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1526 return false;
1527 }
1528
1529 /* Output config */
1530 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1531 RCC_PLLNCFGR2_DIVP_MASK;
1532 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1533 RCC_PLLNCFGR2_DIVQ_MASK;
1534 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1535 RCC_PLLNCFGR2_DIVR_MASK;
1536 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1537 return false;
1538 }
1539
1540 return true;
1541}
1542
1543static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001544{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001545 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1546 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001547
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001548 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1549 mmio_clrsetbits_32(pllxcr,
1550 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1551 RCC_PLLNCR_DIVREN,
1552 RCC_PLLNCR_PLLON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001553}
1554
Yann Gautiere4a3c352019-02-14 10:53:33 +01001555static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001556{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001557 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1558 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001559 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001560
Yann Gautier9aea69e2018-07-24 17:13:36 +02001561 /* Wait PLL lock */
1562 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001563 if (timeout_elapsed(timeout)) {
Yann Gautier4ce04cc2022-02-28 11:34:05 +01001564 ERROR("PLL%u start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001565 pll_id, pllxcr, mmio_read_32(pllxcr));
1566 return -ETIMEDOUT;
1567 }
1568 }
1569
1570 /* Start the requested output */
1571 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1572
1573 return 0;
1574}
1575
Yann Gautiere4a3c352019-02-14 10:53:33 +01001576static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001577{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001578 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1579 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001580 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001581
1582 /* Stop all output */
1583 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1584 RCC_PLLNCR_DIVREN);
1585
1586 /* Stop PLL */
1587 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1588
Yann Gautier2299d572019-02-14 11:14:39 +01001589 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001590 /* Wait PLL stopped */
1591 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001592 if (timeout_elapsed(timeout)) {
Yann Gautier4ce04cc2022-02-28 11:34:05 +01001593 ERROR("PLL%u stop failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001594 pll_id, pllxcr, mmio_read_32(pllxcr));
1595 return -ETIMEDOUT;
1596 }
1597 }
1598
1599 return 0;
1600}
1601
Yann Gautiere4a3c352019-02-14 10:53:33 +01001602static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001603 uint32_t *pllcfg)
1604{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001605 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1606 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001607 uint32_t value;
1608
1609 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1610 RCC_PLLNCFGR2_DIVP_MASK;
1611 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1612 RCC_PLLNCFGR2_DIVQ_MASK;
1613 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1614 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001615 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001616}
1617
Yann Gautiere4a3c352019-02-14 10:53:33 +01001618static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001619 uint32_t *pllcfg, uint32_t fracv)
1620{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001621 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1622 uintptr_t rcc_base = stm32mp_rcc_base();
1623 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001624 unsigned long refclk;
1625 uint32_t ifrge = 0;
1626 uint32_t src, value;
1627
Yann Gautiere4a3c352019-02-14 10:53:33 +01001628 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier9aea69e2018-07-24 17:13:36 +02001629 RCC_SELR_REFCLK_SRC_MASK;
1630
Yann Gautiere4a3c352019-02-14 10:53:33 +01001631 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier9aea69e2018-07-24 17:13:36 +02001632 (pllcfg[PLLCFG_M] + 1U);
1633
1634 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1635 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1636 return -EINVAL;
1637 }
1638
1639 if ((type == PLL_800) && (refclk >= 8000000U)) {
1640 ifrge = 1U;
1641 }
1642
1643 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1644 RCC_PLLNCFGR1_DIVN_MASK;
1645 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1646 RCC_PLLNCFGR1_DIVM_MASK;
1647 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1648 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001649 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001650
1651 /* Fractional configuration */
1652 value = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001653 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001654
1655 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001656 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001657
1658 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001659 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001660
Yann Gautiere4a3c352019-02-14 10:53:33 +01001661 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001662
1663 return 0;
1664}
1665
Yann Gautiere4a3c352019-02-14 10:53:33 +01001666static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001667{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001668 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001669 uint32_t pllxcsg = 0;
1670
1671 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1672 RCC_PLLNCSGR_MOD_PER_MASK;
1673
1674 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1675 RCC_PLLNCSGR_INC_STEP_MASK;
1676
1677 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1678 RCC_PLLNCSGR_SSCG_MODE_MASK;
1679
Yann Gautiere4a3c352019-02-14 10:53:33 +01001680 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001681
1682 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1683 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001684}
1685
Yann Gautiere4a3c352019-02-14 10:53:33 +01001686static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001687{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001688 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier2299d572019-02-14 11:14:39 +01001689 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001690
Yann Gautiere4a3c352019-02-14 10:53:33 +01001691 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001692 clksrc & RCC_SELR_SRC_MASK);
1693
Yann Gautier2299d572019-02-14 11:14:39 +01001694 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001695 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001696 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001697 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1698 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001699 return -ETIMEDOUT;
1700 }
1701 }
1702
1703 return 0;
1704}
1705
Yann Gautiere4a3c352019-02-14 10:53:33 +01001706static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001707{
Yann Gautier2299d572019-02-14 11:14:39 +01001708 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001709
1710 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1711 clkdiv & RCC_DIVR_DIV_MASK);
1712
Yann Gautier2299d572019-02-14 11:14:39 +01001713 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001714 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001715 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001716 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001717 clkdiv, address, mmio_read_32(address));
1718 return -ETIMEDOUT;
1719 }
1720 }
1721
1722 return 0;
1723}
1724
Yann Gautiere4a3c352019-02-14 10:53:33 +01001725static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001726{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001727 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001728
1729 /*
1730 * Binding clksrc :
1731 * bit15-4 offset
1732 * bit3: disable
1733 * bit2-0: MCOSEL[2:0]
1734 */
1735 if ((clksrc & 0x8U) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001736 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001737 } else {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001738 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001739 RCC_MCOCFG_MCOSRC_MASK,
1740 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001741 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001742 RCC_MCOCFG_MCODIV_MASK,
1743 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001744 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001745 }
1746}
1747
Yann Gautiere4a3c352019-02-14 10:53:33 +01001748static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001749{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001750 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001751
1752 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1753 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1754 mmio_clrsetbits_32(address,
1755 RCC_BDCR_RTCSRC_MASK,
Yann Gautier74aa83a2021-04-06 13:41:19 +02001756 (clksrc & RCC_SELR_SRC_MASK) << RCC_BDCR_RTCSRC_SHIFT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001757
1758 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1759 }
1760
1761 if (lse_css) {
1762 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1763 }
1764}
1765
Yann Gautiere4a3c352019-02-14 10:53:33 +01001766static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001767{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001768 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001769 uint32_t value = pkcs & 0xFU;
1770 uint32_t mask = 0xFU;
1771
1772 if ((pkcs & BIT(31)) != 0U) {
1773 mask <<= 4;
1774 value <<= 4;
1775 }
1776
1777 mmio_clrsetbits_32(address, mask, value);
1778}
1779
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001780static int clk_get_pll_settings_from_dt(int plloff, unsigned int *pllcfg,
1781 uint32_t *fracv, uint32_t *csg,
1782 bool *csg_set)
1783{
1784 void *fdt;
1785 int ret;
1786
1787 if (fdt_get_address(&fdt) == 0) {
1788 return -FDT_ERR_NOTFOUND;
1789 }
1790
1791 ret = fdt_read_uint32_array(fdt, plloff, "cfg", (uint32_t)PLLCFG_NB,
1792 pllcfg);
1793 if (ret < 0) {
1794 return -FDT_ERR_NOTFOUND;
1795 }
1796
1797 *fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1798
1799 ret = fdt_read_uint32_array(fdt, plloff, "csg", (uint32_t)PLLCSG_NB,
1800 csg);
1801
1802 *csg_set = (ret == 0);
1803
1804 if (ret == -FDT_ERR_NOTFOUND) {
1805 ret = 0;
1806 }
1807
1808 return ret;
1809}
1810
Yann Gautier9aea69e2018-07-24 17:13:36 +02001811int stm32mp1_clk_init(void)
1812{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001813 uintptr_t rcc_base = stm32mp_rcc_base();
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001814 uint32_t pllfracv[_PLL_NB];
1815 uint32_t pllcsg[_PLL_NB][PLLCSG_NB];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001816 unsigned int clksrc[CLKSRC_NB];
1817 unsigned int clkdiv[CLKDIV_NB];
1818 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1819 int plloff[_PLL_NB];
1820 int ret, len;
1821 enum stm32mp1_pll_id i;
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001822 bool pllcsg_set[_PLL_NB];
1823 bool pllcfg_valid[_PLL_NB];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001824 bool lse_css = false;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001825 bool pll3_preserve = false;
1826 bool pll4_preserve = false;
1827 bool pll4_bootrom = false;
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001828 const fdt32_t *pkcs_cell;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001829 void *fdt;
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02001830 int stgen_p = stm32mp1_clk_get_parent(STGEN_K);
1831 int usbphy_p = stm32mp1_clk_get_parent(USBPHY_K);
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001832
1833 if (fdt_get_address(&fdt) == 0) {
Yann Gautier360e0e92020-09-16 16:40:34 +02001834 return -FDT_ERR_NOTFOUND;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001835 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001836
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001837 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1838 clksrc);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001839 if (ret < 0) {
1840 return -FDT_ERR_NOTFOUND;
1841 }
1842
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001843 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1844 clkdiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001845 if (ret < 0) {
1846 return -FDT_ERR_NOTFOUND;
1847 }
1848
1849 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1850 char name[12];
1851
Yann Gautier4ce04cc2022-02-28 11:34:05 +01001852 snprintf(name, sizeof(name), "st,pll@%u", i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001853 plloff[i] = fdt_rcc_subnode_offset(name);
1854
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001855 pllcfg_valid[i] = fdt_check_node(plloff[i]);
1856 if (!pllcfg_valid[i]) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001857 continue;
1858 }
1859
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001860 ret = clk_get_pll_settings_from_dt(plloff[i], pllcfg[i],
1861 &pllfracv[i], pllcsg[i],
1862 &pllcsg_set[i]);
1863 if (ret != 0) {
1864 return ret;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001865 }
1866 }
1867
Yann Gautiere4a3c352019-02-14 10:53:33 +01001868 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1869 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001870
1871 /*
1872 * Switch ON oscillator found in device-tree.
1873 * Note: HSI already ON after BootROM stage.
1874 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001875 if (stm32mp1_osc[_LSI] != 0U) {
1876 stm32mp1_lsi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001877 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001878 if (stm32mp1_osc[_LSE] != 0U) {
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001879 const char *name = stm32mp_osc_node_label[_LSE];
Yann Gautiere4a3c352019-02-14 10:53:33 +01001880 bool bypass, digbyp;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001881 uint32_t lsedrv;
1882
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001883 bypass = fdt_clk_read_bool(name, "st,bypass");
1884 digbyp = fdt_clk_read_bool(name, "st,digbypass");
1885 lse_css = fdt_clk_read_bool(name, "st,css");
1886 lsedrv = fdt_clk_read_uint32_default(name, "st,drive",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001887 LSEDRV_MEDIUM_HIGH);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001888 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001889 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001890 if (stm32mp1_osc[_HSE] != 0U) {
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001891 const char *name = stm32mp_osc_node_label[_HSE];
Yann Gautiere4a3c352019-02-14 10:53:33 +01001892 bool bypass, digbyp, css;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001893
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001894 bypass = fdt_clk_read_bool(name, "st,bypass");
1895 digbyp = fdt_clk_read_bool(name, "st,digbypass");
1896 css = fdt_clk_read_bool(name, "st,css");
Yann Gautiere4a3c352019-02-14 10:53:33 +01001897 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001898 }
1899 /*
1900 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1901 * => switch on CSI even if node is not present in device tree
1902 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001903 stm32mp1_csi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001904
1905 /* Come back to HSI */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001906 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001907 if (ret != 0) {
1908 return ret;
1909 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001910 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001911 if (ret != 0) {
1912 return ret;
1913 }
Yann Gautiered342322019-02-15 17:33:27 +01001914 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1915 if (ret != 0) {
1916 return ret;
1917 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001918
Yann Gautiere4a3c352019-02-14 10:53:33 +01001919 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1920 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
Yann Gautierfffc76e2022-03-04 11:08:47 +01001921 if (pllcfg_valid[_PLL3]) {
1922 pll3_preserve =
1923 stm32mp1_check_pll_conf(_PLL3,
Yann Gautiere4a3c352019-02-14 10:53:33 +01001924 clksrc[CLKSRC_PLL3],
1925 pllcfg[_PLL3],
1926 plloff[_PLL3]);
Yann Gautierfffc76e2022-03-04 11:08:47 +01001927 }
1928
1929 if (pllcfg_valid[_PLL4]) {
1930 pll4_preserve =
1931 stm32mp1_check_pll_conf(_PLL4,
Yann Gautiere4a3c352019-02-14 10:53:33 +01001932 clksrc[CLKSRC_PLL4],
1933 pllcfg[_PLL4],
1934 plloff[_PLL4]);
Yann Gautierfffc76e2022-03-04 11:08:47 +01001935 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001936 }
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02001937 /* Don't initialize PLL4, when used by BOOTROM */
1938 if ((stm32mp_get_boot_itf_selected() ==
1939 BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) &&
1940 ((stgen_p == (int)_PLL4_R) || (usbphy_p == (int)_PLL4_R))) {
1941 pll4_bootrom = true;
1942 pll4_preserve = true;
1943 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001944
Yann Gautier9aea69e2018-07-24 17:13:36 +02001945 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001946 if (((i == _PLL3) && pll3_preserve) ||
1947 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001948 continue;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001949 }
1950
1951 ret = stm32mp1_pll_stop(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001952 if (ret != 0) {
1953 return ret;
1954 }
1955 }
1956
1957 /* Configure HSIDIV */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001958 if (stm32mp1_osc[_HSI] != 0U) {
1959 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001960 if (ret != 0) {
1961 return ret;
1962 }
Lionel Debievedeef9692019-12-04 21:50:19 +01001963
1964 stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001965 }
1966
1967 /* Select DIV */
1968 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001969 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001970 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001971 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001972 if (ret != 0) {
1973 return ret;
1974 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001975 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001976 if (ret != 0) {
1977 return ret;
1978 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001979 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001980 if (ret != 0) {
1981 return ret;
1982 }
Yann Gautiered342322019-02-15 17:33:27 +01001983 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
1984 if (ret != 0) {
1985 return ret;
1986 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001987 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001988 if (ret != 0) {
1989 return ret;
1990 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001991 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001992 if (ret != 0) {
1993 return ret;
1994 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001995 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001996 if (ret != 0) {
1997 return ret;
1998 }
1999
2000 /* No ready bit for RTC */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002001 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002002 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
2003
2004 /* Configure PLLs source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002005 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002006 if (ret != 0) {
2007 return ret;
2008 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002009
2010 if (!pll3_preserve) {
2011 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
2012 if (ret != 0) {
2013 return ret;
2014 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002015 }
2016
Yann Gautiere4a3c352019-02-14 10:53:33 +01002017 if (!pll4_preserve) {
2018 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
2019 if (ret != 0) {
2020 return ret;
2021 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002022 }
2023
2024 /* Configure and start PLLs */
2025 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002026 if (((i == _PLL3) && pll3_preserve) ||
2027 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
2028 continue;
2029 }
2030
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002031 if (!pllcfg_valid[i]) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02002032 continue;
2033 }
2034
Yann Gautiere4a3c352019-02-14 10:53:33 +01002035 if ((i == _PLL4) && pll4_bootrom) {
2036 /* Set output divider if not done by the Bootrom */
2037 stm32mp1_pll_config_output(i, pllcfg[i]);
2038 continue;
2039 }
2040
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002041 ret = stm32mp1_pll_config(i, pllcfg[i], pllfracv[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002042 if (ret != 0) {
2043 return ret;
2044 }
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002045
2046 if (pllcsg_set[i]) {
2047 stm32mp1_pll_csg(i, pllcsg[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002048 }
2049
Yann Gautiere4a3c352019-02-14 10:53:33 +01002050 stm32mp1_pll_start(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002051 }
Elyes Haouas2be03c02023-02-13 09:14:48 +01002052 /* Wait and start PLLs output when ready */
Yann Gautier9aea69e2018-07-24 17:13:36 +02002053 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002054 if (!pllcfg_valid[i]) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02002055 continue;
2056 }
2057
Yann Gautiere4a3c352019-02-14 10:53:33 +01002058 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002059 if (ret != 0) {
2060 return ret;
2061 }
2062 }
2063 /* Wait LSE ready before to use it */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002064 if (stm32mp1_osc[_LSE] != 0U) {
2065 stm32mp1_lse_wait();
Yann Gautier9aea69e2018-07-24 17:13:36 +02002066 }
2067
2068 /* Configure with expected clock source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002069 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002070 if (ret != 0) {
2071 return ret;
2072 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002073 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002074 if (ret != 0) {
2075 return ret;
2076 }
Yann Gautiered342322019-02-15 17:33:27 +01002077 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
2078 if (ret != 0) {
2079 return ret;
2080 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002081 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002082
2083 /* Configure PKCK */
2084 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
2085 if (pkcs_cell != NULL) {
2086 bool ckper_disabled = false;
2087 uint32_t j;
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02002088 uint32_t usbreg_bootrom = 0U;
2089
2090 if (pll4_bootrom) {
2091 usbreg_bootrom = mmio_read_32(rcc_base + RCC_USBCKSELR);
2092 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002093
Yann Gautier9aea69e2018-07-24 17:13:36 +02002094 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautierf9af3bc2018-11-09 15:57:18 +01002095 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002096
2097 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
2098 ckper_disabled = true;
2099 continue;
2100 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002101 stm32mp1_pkcs_config(pkcs);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002102 }
2103
2104 /*
2105 * CKPER is source for some peripheral clocks
2106 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2107 * only if previous clock is still ON
2108 * => deactivated CKPER only after switching clock
2109 */
2110 if (ckper_disabled) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002111 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002112 }
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02002113
2114 if (pll4_bootrom) {
2115 uint32_t usbreg_value, usbreg_mask;
2116 const struct stm32mp1_clk_sel *sel;
2117
2118 sel = clk_sel_ref(_USBPHY_SEL);
2119 usbreg_mask = (uint32_t)sel->msk << sel->src;
2120 sel = clk_sel_ref(_USBO_SEL);
2121 usbreg_mask |= (uint32_t)sel->msk << sel->src;
2122
2123 usbreg_value = mmio_read_32(rcc_base + RCC_USBCKSELR) &
2124 usbreg_mask;
2125 usbreg_bootrom &= usbreg_mask;
2126 if (usbreg_bootrom != usbreg_value) {
2127 VERBOSE("forbidden new USB clk path\n");
2128 VERBOSE("vs bootrom on USB boot\n");
2129 return -FDT_ERR_BADVALUE;
2130 }
2131 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002132 }
2133
2134 /* Switch OFF HSI if not found in device-tree */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002135 if (stm32mp1_osc[_HSI] == 0U) {
2136 stm32mp1_hsi_set(false);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002137 }
Lionel Debievedeef9692019-12-04 21:50:19 +01002138
2139 stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K));
Yann Gautier9aea69e2018-07-24 17:13:36 +02002140
2141 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002142 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002143 RCC_DDRITFCR_DDRCKMOD_MASK,
2144 RCC_DDRITFCR_DDRCKMOD_SSR <<
2145 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2146
2147 return 0;
2148}
2149
2150static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002151 enum stm32mp_osc_id index)
2152{
2153 uint32_t frequency;
2154
Yann Gautiere4a3c352019-02-14 10:53:33 +01002155 if (fdt_osc_read_freq(name, &frequency) == 0) {
2156 stm32mp1_osc[index] = frequency;
Yann Gautier9aea69e2018-07-24 17:13:36 +02002157 }
2158}
2159
2160static void stm32mp1_osc_init(void)
2161{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002162 enum stm32mp_osc_id i;
2163
2164 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002165 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002166 }
2167}
Etienne Carriere1368ada2020-05-13 11:49:49 +02002168
2169#ifdef STM32MP_SHARED_RESOURCES
2170/*
2171 * Get the parent ID of the target parent clock, for tagging as secure
2172 * shared clock dependencies.
2173 */
2174static int get_parent_id_parent(unsigned int parent_id)
2175{
2176 enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
2177 enum stm32mp1_pll_id pll_id;
2178 uint32_t p_sel;
2179 uintptr_t rcc_base = stm32mp_rcc_base();
2180
2181 switch (parent_id) {
2182 case _ACLK:
2183 case _PCLK4:
2184 case _PCLK5:
2185 s = _AXIS_SEL;
2186 break;
2187 case _PLL1_P:
2188 case _PLL1_Q:
2189 case _PLL1_R:
2190 pll_id = _PLL1;
2191 break;
2192 case _PLL2_P:
2193 case _PLL2_Q:
2194 case _PLL2_R:
2195 pll_id = _PLL2;
2196 break;
2197 case _PLL3_P:
2198 case _PLL3_Q:
2199 case _PLL3_R:
2200 pll_id = _PLL3;
2201 break;
2202 case _PLL4_P:
2203 case _PLL4_Q:
2204 case _PLL4_R:
2205 pll_id = _PLL4;
2206 break;
2207 case _PCLK1:
2208 case _PCLK2:
2209 case _HCLK2:
2210 case _HCLK6:
2211 case _CK_PER:
2212 case _CK_MPU:
2213 case _CK_MCU:
2214 case _USB_PHY_48:
2215 /* We do not expect to access these */
2216 panic();
2217 break;
2218 default:
2219 /* Other parents have no parent */
2220 return -1;
2221 }
2222
2223 if (s != _UNKNOWN_SEL) {
2224 const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
2225
2226 p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
2227 sel->msk;
2228
2229 if (p_sel < sel->nb_parent) {
2230 return (int)sel->parent[p_sel];
2231 }
2232 } else {
2233 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
2234
2235 p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
2236 RCC_SELR_REFCLK_SRC_MASK;
2237
2238 if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
2239 return (int)pll->refclk[p_sel];
2240 }
2241 }
2242
2243 VERBOSE("No parent selected for %s\n",
2244 stm32mp1_clk_parent_name[parent_id]);
2245
2246 return -1;
2247}
2248
2249static void secure_parent_clocks(unsigned long parent_id)
2250{
2251 int grandparent_id;
2252
2253 switch (parent_id) {
2254 case _PLL3_P:
2255 case _PLL3_Q:
2256 case _PLL3_R:
2257 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2258 break;
2259
2260 /* These clocks are always secure when RCC is secure */
2261 case _ACLK:
2262 case _HCLK2:
2263 case _HCLK6:
2264 case _PCLK4:
2265 case _PCLK5:
2266 case _PLL1_P:
2267 case _PLL1_Q:
2268 case _PLL1_R:
2269 case _PLL2_P:
2270 case _PLL2_Q:
2271 case _PLL2_R:
2272 case _HSI:
2273 case _HSI_KER:
2274 case _LSI:
2275 case _CSI:
2276 case _CSI_KER:
2277 case _HSE:
2278 case _HSE_KER:
2279 case _HSE_KER_DIV2:
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +02002280 case _HSE_RTC:
Etienne Carriere1368ada2020-05-13 11:49:49 +02002281 case _LSE:
2282 break;
2283
2284 default:
2285 VERBOSE("Cannot secure parent clock %s\n",
2286 stm32mp1_clk_parent_name[parent_id]);
2287 panic();
2288 }
2289
2290 grandparent_id = get_parent_id_parent(parent_id);
2291 if (grandparent_id >= 0) {
2292 secure_parent_clocks(grandparent_id);
2293 }
2294}
2295
2296void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
2297{
2298 int parent_id;
2299
2300 if (!stm32mp1_rcc_is_secure()) {
2301 return;
2302 }
2303
2304 switch (clock_id) {
2305 case PLL1:
2306 case PLL2:
2307 /* PLL1/PLL2 are always secure: nothing to do */
2308 break;
2309 case PLL3:
2310 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2311 break;
2312 case PLL4:
2313 ERROR("PLL4 cannot be secured\n");
2314 panic();
2315 break;
2316 default:
2317 /* Others are expected gateable clock */
2318 parent_id = stm32mp1_clk_get_parent(clock_id);
2319 if (parent_id < 0) {
2320 INFO("No parent found for clock %lu\n", clock_id);
2321 } else {
2322 secure_parent_clocks(parent_id);
2323 }
2324 break;
2325 }
2326}
2327#endif /* STM32MP_SHARED_RESOURCES */
Yann Gautier9aea69e2018-07-24 17:13:36 +02002328
Yann Gautierc7f9e962019-05-20 14:39:26 +02002329static void sync_earlyboot_clocks_state(void)
2330{
Etienne Carriere2a756c22019-12-08 08:23:35 +01002331 unsigned int idx;
2332 const unsigned long secure_enable[] = {
2333 AXIDCG,
2334 BSEC,
2335 DDRC1, DDRC1LP,
2336 DDRC2, DDRC2LP,
2337 DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
2338 DDRPHYC, DDRPHYCLP,
Lionel Debievecfa88cc2019-09-02 18:15:45 +02002339 RTCAPB,
Etienne Carriere2a756c22019-12-08 08:23:35 +01002340 TZC1, TZC2,
2341 TZPC,
2342 STGEN_K,
2343 };
2344
2345 for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
2346 stm32mp_clk_enable(secure_enable[idx]);
2347 }
Yann Gautierc7f9e962019-05-20 14:39:26 +02002348}
2349
Yann Gautiera205a5c2021-08-30 15:06:54 +02002350static const struct clk_ops stm32mp_clk_ops = {
2351 .enable = stm32mp_clk_enable,
2352 .disable = stm32mp_clk_disable,
2353 .is_enabled = stm32mp_clk_is_enabled,
2354 .get_rate = stm32mp_clk_get_rate,
2355 .get_parent = stm32mp1_clk_get_parent,
2356};
2357
Yann Gautier9aea69e2018-07-24 17:13:36 +02002358int stm32mp1_clk_probe(void)
2359{
Lionel Debieve3c0fbfe2020-12-15 10:35:59 +01002360#if defined(IMAGE_BL32)
2361 if (!fdt_get_rcc_secure_state()) {
2362 mmio_write_32(stm32mp_rcc_base() + RCC_TZCR, 0U);
2363 }
2364#endif
2365
Yann Gautier9aea69e2018-07-24 17:13:36 +02002366 stm32mp1_osc_init();
2367
Yann Gautierc7f9e962019-05-20 14:39:26 +02002368 sync_earlyboot_clocks_state();
2369
Yann Gautiera205a5c2021-08-30 15:06:54 +02002370 clk_register(&stm32mp_clk_ops);
2371
Yann Gautier9aea69e2018-07-24 17:13:36 +02002372 return 0;
2373}