blob: 2b6683dfa98ec02c662195a533000f008621ea2b [file] [log] [blame]
Yann Gautier7839a052018-07-24 17:13:36 +02001/*
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002 * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved
Yann Gautier7839a052018-07-24 17:13:36 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
Yann Gautier7839a052018-07-24 17:13:36 +02007#include <assert.h>
Yann Gautier7839a052018-07-24 17:13:36 +02008#include <errno.h>
Yann Gautier7839a052018-07-24 17:13:36 +02009#include <stdint.h>
Antonio Nino Diaz39b6cc62018-08-16 16:46:06 +010010#include <stdio.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <arch.h>
13#include <arch_helpers.h>
14#include <common/debug.h>
Andre Przywara52a616b2020-03-26 12:51:21 +000015#include <common/fdt_wrappers.h>
Yann Gautier33667d22021-08-30 15:06:54 +020016#include <drivers/clk.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000017#include <drivers/delay_timer.h>
18#include <drivers/generic_delay_timer.h>
Yann Gautier447b2b132019-02-14 11:15:20 +010019#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000020#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000021#include <drivers/st/stm32mp1_rcc.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000022#include <dt-bindings/clock/stm32mp1-clksrc.h>
23#include <lib/mmio.h>
Yann Gautier0d216802019-02-14 10:53:33 +010024#include <lib/spinlock.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000025#include <lib/utils_def.h>
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +010026#include <libfdt.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000027#include <plat/common/platform.h>
28
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +010029#include <platform_def.h>
30
Yann Gautierdfdb0572019-02-14 11:14:39 +010031#define MAX_HSI_HZ 64000000
Yann Gautier0d216802019-02-14 10:53:33 +010032#define USB_PHY_48_MHZ 48000000
Yann Gautier7839a052018-07-24 17:13:36 +020033
Yann Gautierdfdb0572019-02-14 11:14:39 +010034#define TIMEOUT_US_200MS U(200000)
35#define TIMEOUT_US_1S U(1000000)
Yann Gautier7839a052018-07-24 17:13:36 +020036
Yann Gautierdfdb0572019-02-14 11:14:39 +010037#define PLLRDY_TIMEOUT TIMEOUT_US_200MS
38#define CLKSRC_TIMEOUT TIMEOUT_US_200MS
39#define CLKDIV_TIMEOUT TIMEOUT_US_200MS
40#define HSIDIV_TIMEOUT TIMEOUT_US_200MS
41#define OSCRDY_TIMEOUT TIMEOUT_US_1S
Yann Gautier7839a052018-07-24 17:13:36 +020042
Yann Gautierf66358a2019-05-17 15:57:56 +020043const char *stm32mp_osc_node_label[NB_OSC] = {
44 [_LSI] = "clk-lsi",
45 [_LSE] = "clk-lse",
46 [_HSI] = "clk-hsi",
47 [_HSE] = "clk-hse",
48 [_CSI] = "clk-csi",
49 [_I2S_CKIN] = "i2s_ckin",
50};
51
Yann Gautier7839a052018-07-24 17:13:36 +020052enum stm32mp1_parent_id {
53/* Oscillators are defined in enum stm32mp_osc_id */
54
55/* Other parent source */
56 _HSI_KER = NB_OSC,
57 _HSE_KER,
58 _HSE_KER_DIV2,
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +020059 _HSE_RTC,
Yann Gautier7839a052018-07-24 17:13:36 +020060 _CSI_KER,
61 _PLL1_P,
62 _PLL1_Q,
63 _PLL1_R,
64 _PLL2_P,
65 _PLL2_Q,
66 _PLL2_R,
67 _PLL3_P,
68 _PLL3_Q,
69 _PLL3_R,
70 _PLL4_P,
71 _PLL4_Q,
72 _PLL4_R,
73 _ACLK,
74 _PCLK1,
75 _PCLK2,
76 _PCLK3,
77 _PCLK4,
78 _PCLK5,
79 _HCLK6,
80 _HCLK2,
81 _CK_PER,
82 _CK_MPU,
Yann Gautierb053a222019-02-15 17:33:27 +010083 _CK_MCU,
Yann Gautier0d216802019-02-14 10:53:33 +010084 _USB_PHY_48,
Yann Gautier7839a052018-07-24 17:13:36 +020085 _PARENT_NB,
86 _UNKNOWN_ID = 0xff,
87};
88
Yann Gautier0d216802019-02-14 10:53:33 +010089/* Lists only the parent clock we are interested in */
Yann Gautier7839a052018-07-24 17:13:36 +020090enum stm32mp1_parent_sel {
Yann Gautier0d216802019-02-14 10:53:33 +010091 _I2C12_SEL,
92 _I2C35_SEL,
93 _STGEN_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +020094 _I2C46_SEL,
Yann Gautier0d216802019-02-14 10:53:33 +010095 _SPI6_SEL,
Yann Gautierd4151d22019-05-07 18:49:33 +020096 _UART1_SEL,
Yann Gautier0d216802019-02-14 10:53:33 +010097 _RNG1_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +020098 _UART6_SEL,
99 _UART24_SEL,
100 _UART35_SEL,
101 _UART78_SEL,
102 _SDMMC12_SEL,
103 _SDMMC3_SEL,
104 _QSPI_SEL,
105 _FMC_SEL,
Yann Gautierd4151d22019-05-07 18:49:33 +0200106 _AXIS_SEL,
107 _MCUS_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +0200108 _USBPHY_SEL,
109 _USBO_SEL,
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100110 _MPU_SEL,
Yann Gautier288f5cf2021-08-31 18:23:13 +0200111 _CKPER_SEL,
Etienne Carriere016af002019-12-08 08:22:31 +0100112 _RTC_SEL,
Yann Gautier7839a052018-07-24 17:13:36 +0200113 _PARENT_SEL_NB,
114 _UNKNOWN_SEL = 0xff,
115};
116
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100117/* State the parent clock ID straight related to a clock */
118static const uint8_t parent_id_clock_id[_PARENT_NB] = {
119 [_HSE] = CK_HSE,
120 [_HSI] = CK_HSI,
121 [_CSI] = CK_CSI,
122 [_LSE] = CK_LSE,
123 [_LSI] = CK_LSI,
124 [_I2S_CKIN] = _UNKNOWN_ID,
125 [_USB_PHY_48] = _UNKNOWN_ID,
126 [_HSI_KER] = CK_HSI,
127 [_HSE_KER] = CK_HSE,
128 [_HSE_KER_DIV2] = CK_HSE_DIV2,
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200129 [_HSE_RTC] = _UNKNOWN_ID,
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100130 [_CSI_KER] = CK_CSI,
131 [_PLL1_P] = PLL1_P,
132 [_PLL1_Q] = PLL1_Q,
133 [_PLL1_R] = PLL1_R,
134 [_PLL2_P] = PLL2_P,
135 [_PLL2_Q] = PLL2_Q,
136 [_PLL2_R] = PLL2_R,
137 [_PLL3_P] = PLL3_P,
138 [_PLL3_Q] = PLL3_Q,
139 [_PLL3_R] = PLL3_R,
140 [_PLL4_P] = PLL4_P,
141 [_PLL4_Q] = PLL4_Q,
142 [_PLL4_R] = PLL4_R,
143 [_ACLK] = CK_AXI,
144 [_PCLK1] = CK_AXI,
145 [_PCLK2] = CK_AXI,
146 [_PCLK3] = CK_AXI,
147 [_PCLK4] = CK_AXI,
148 [_PCLK5] = CK_AXI,
149 [_CK_PER] = CK_PER,
150 [_CK_MPU] = CK_MPU,
151 [_CK_MCU] = CK_MCU,
152};
153
154static unsigned int clock_id2parent_id(unsigned long id)
155{
156 unsigned int n;
157
158 for (n = 0U; n < ARRAY_SIZE(parent_id_clock_id); n++) {
159 if (parent_id_clock_id[n] == id) {
160 return n;
161 }
162 }
163
164 return _UNKNOWN_ID;
165}
166
Yann Gautier7839a052018-07-24 17:13:36 +0200167enum stm32mp1_pll_id {
168 _PLL1,
169 _PLL2,
170 _PLL3,
171 _PLL4,
172 _PLL_NB
173};
174
175enum stm32mp1_div_id {
176 _DIV_P,
177 _DIV_Q,
178 _DIV_R,
179 _DIV_NB,
180};
181
182enum stm32mp1_clksrc_id {
183 CLKSRC_MPU,
184 CLKSRC_AXI,
Yann Gautierb053a222019-02-15 17:33:27 +0100185 CLKSRC_MCU,
Yann Gautier7839a052018-07-24 17:13:36 +0200186 CLKSRC_PLL12,
187 CLKSRC_PLL3,
188 CLKSRC_PLL4,
189 CLKSRC_RTC,
190 CLKSRC_MCO1,
191 CLKSRC_MCO2,
192 CLKSRC_NB
193};
194
195enum stm32mp1_clkdiv_id {
196 CLKDIV_MPU,
197 CLKDIV_AXI,
Yann Gautierb053a222019-02-15 17:33:27 +0100198 CLKDIV_MCU,
Yann Gautier7839a052018-07-24 17:13:36 +0200199 CLKDIV_APB1,
200 CLKDIV_APB2,
201 CLKDIV_APB3,
202 CLKDIV_APB4,
203 CLKDIV_APB5,
204 CLKDIV_RTC,
205 CLKDIV_MCO1,
206 CLKDIV_MCO2,
207 CLKDIV_NB
208};
209
210enum stm32mp1_pllcfg {
211 PLLCFG_M,
212 PLLCFG_N,
213 PLLCFG_P,
214 PLLCFG_Q,
215 PLLCFG_R,
216 PLLCFG_O,
217 PLLCFG_NB
218};
219
220enum stm32mp1_pllcsg {
221 PLLCSG_MOD_PER,
222 PLLCSG_INC_STEP,
223 PLLCSG_SSCG_MODE,
224 PLLCSG_NB
225};
226
227enum stm32mp1_plltype {
228 PLL_800,
229 PLL_1600,
230 PLL_TYPE_NB
231};
232
233struct stm32mp1_pll {
234 uint8_t refclk_min;
235 uint8_t refclk_max;
236 uint8_t divn_max;
237};
238
239struct stm32mp1_clk_gate {
240 uint16_t offset;
241 uint8_t bit;
242 uint8_t index;
243 uint8_t set_clr;
Yann Gautieraaa09b72021-10-27 18:16:59 +0200244 uint8_t secure;
Yann Gautier0d216802019-02-14 10:53:33 +0100245 uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
246 uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
Yann Gautier7839a052018-07-24 17:13:36 +0200247};
248
249struct stm32mp1_clk_sel {
250 uint16_t offset;
251 uint8_t src;
252 uint8_t msk;
253 uint8_t nb_parent;
254 const uint8_t *parent;
255};
256
257#define REFCLK_SIZE 4
258struct stm32mp1_clk_pll {
259 enum stm32mp1_plltype plltype;
260 uint16_t rckxselr;
261 uint16_t pllxcfgr1;
262 uint16_t pllxcfgr2;
263 uint16_t pllxfracr;
264 uint16_t pllxcr;
265 uint16_t pllxcsgr;
266 enum stm32mp_osc_id refclk[REFCLK_SIZE];
267};
268
Yann Gautier0d216802019-02-14 10:53:33 +0100269/* Clocks with selectable source and non set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200270#define _CLK_SELEC(sec, off, b, idx, s) \
Yann Gautier7839a052018-07-24 17:13:36 +0200271 { \
272 .offset = (off), \
273 .bit = (b), \
274 .index = (idx), \
275 .set_clr = 0, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200276 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200277 .sel = (s), \
278 .fixed = _UNKNOWN_ID, \
Yann Gautier7839a052018-07-24 17:13:36 +0200279 }
280
Yann Gautier0d216802019-02-14 10:53:33 +0100281/* Clocks with fixed source and non set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200282#define _CLK_FIXED(sec, off, b, idx, f) \
Yann Gautier7839a052018-07-24 17:13:36 +0200283 { \
284 .offset = (off), \
285 .bit = (b), \
286 .index = (idx), \
287 .set_clr = 0, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200288 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200289 .sel = _UNKNOWN_SEL, \
290 .fixed = (f), \
Yann Gautier7839a052018-07-24 17:13:36 +0200291 }
292
Yann Gautier0d216802019-02-14 10:53:33 +0100293/* Clocks with selectable source and set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200294#define _CLK_SC_SELEC(sec, off, b, idx, s) \
Yann Gautier7839a052018-07-24 17:13:36 +0200295 { \
296 .offset = (off), \
297 .bit = (b), \
298 .index = (idx), \
299 .set_clr = 1, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200300 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200301 .sel = (s), \
302 .fixed = _UNKNOWN_ID, \
Yann Gautier7839a052018-07-24 17:13:36 +0200303 }
304
Yann Gautier0d216802019-02-14 10:53:33 +0100305/* Clocks with fixed source and set/clr register access */
Yann Gautieraaa09b72021-10-27 18:16:59 +0200306#define _CLK_SC_FIXED(sec, off, b, idx, f) \
Yann Gautier7839a052018-07-24 17:13:36 +0200307 { \
308 .offset = (off), \
309 .bit = (b), \
310 .index = (idx), \
311 .set_clr = 1, \
Yann Gautieraaa09b72021-10-27 18:16:59 +0200312 .secure = (sec), \
Yann Gautier7839a052018-07-24 17:13:36 +0200313 .sel = _UNKNOWN_SEL, \
314 .fixed = (f), \
Yann Gautier7839a052018-07-24 17:13:36 +0200315 }
316
Yann Gautierd4151d22019-05-07 18:49:33 +0200317#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \
318 [_ ## _label ## _SEL] = { \
319 .offset = _rcc_selr, \
320 .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \
Etienne Carriere8ae08dc2019-12-08 08:20:40 +0100321 .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
322 (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
Yann Gautierd4151d22019-05-07 18:49:33 +0200323 .parent = (_parents), \
324 .nb_parent = ARRAY_SIZE(_parents) \
Yann Gautier7839a052018-07-24 17:13:36 +0200325 }
326
Yann Gautier0d216802019-02-14 10:53:33 +0100327#define _CLK_PLL(idx, type, off1, off2, off3, \
328 off4, off5, off6, \
329 p1, p2, p3, p4) \
Yann Gautier7839a052018-07-24 17:13:36 +0200330 [(idx)] = { \
331 .plltype = (type), \
332 .rckxselr = (off1), \
333 .pllxcfgr1 = (off2), \
334 .pllxcfgr2 = (off3), \
335 .pllxfracr = (off4), \
336 .pllxcr = (off5), \
337 .pllxcsgr = (off6), \
338 .refclk[0] = (p1), \
339 .refclk[1] = (p2), \
340 .refclk[2] = (p3), \
341 .refclk[3] = (p4), \
342 }
343
Yann Gautier0d216802019-02-14 10:53:33 +0100344#define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
345
Yann Gautieraaa09b72021-10-27 18:16:59 +0200346#define SEC 1
347#define N_S 0
348
Yann Gautier7839a052018-07-24 17:13:36 +0200349static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautieraaa09b72021-10-27 18:16:59 +0200350 _CLK_FIXED(SEC, RCC_DDRITFCR, 0, DDRC1, _ACLK),
351 _CLK_FIXED(SEC, RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
352 _CLK_FIXED(SEC, RCC_DDRITFCR, 2, DDRC2, _ACLK),
353 _CLK_FIXED(SEC, RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
354 _CLK_FIXED(SEC, RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
355 _CLK_FIXED(SEC, RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
356 _CLK_FIXED(SEC, RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
357 _CLK_FIXED(SEC, RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
358 _CLK_FIXED(SEC, RCC_DDRITFCR, 8, AXIDCG, _ACLK),
359 _CLK_FIXED(SEC, RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
360 _CLK_FIXED(SEC, RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),
Yann Gautier7839a052018-07-24 17:13:36 +0200361
Yann Gautier7418cf32020-01-17 11:59:28 +0100362#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200363 _CLK_SC_FIXED(N_S, RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
Yann Gautier7418cf32020-01-17 11:59:28 +0100364#endif
Yann Gautieraaa09b72021-10-27 18:16:59 +0200365 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
366 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
367 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
368 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
369 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
370 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
371 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
372 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
373 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
374 _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200375
Yann Gautier7418cf32020-01-17 11:59:28 +0100376#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200377 _CLK_SC_FIXED(N_S, RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
Yann Gautier7418cf32020-01-17 11:59:28 +0100378#endif
Yann Gautieraaa09b72021-10-27 18:16:59 +0200379 _CLK_SC_SELEC(N_S, RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200380
Yann Gautieraaa09b72021-10-27 18:16:59 +0200381 _CLK_SC_FIXED(N_S, RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),
Yann Gautierf33b2432019-05-20 19:17:08 +0200382
Yann Gautieraaa09b72021-10-27 18:16:59 +0200383 _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
384 _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
385 _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200386
Yann Gautieraaa09b72021-10-27 18:16:59 +0200387 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
388 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
389 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
390 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
391 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
392 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
393 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
394 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
395 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
396 _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
397 _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200398
Yann Gautier7418cf32020-01-17 11:59:28 +0100399#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200400 _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
401 _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100402#endif
Yann Gautier7839a052018-07-24 17:13:36 +0200403
Yann Gautieraaa09b72021-10-27 18:16:59 +0200404 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
405 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
406 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
407 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
408 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
409 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
410 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
411 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
412 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
413 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
414 _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200415
Yann Gautieraaa09b72021-10-27 18:16:59 +0200416 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
417 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
418 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
419 _CLK_SC_SELEC(SEC, RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
420 _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),
Yann Gautier7839a052018-07-24 17:13:36 +0200421
Yann Gautier7418cf32020-01-17 11:59:28 +0100422#if defined(IMAGE_BL2)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200423 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
424 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100425#endif
Yann Gautieraaa09b72021-10-27 18:16:59 +0200426 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
427 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100428#if defined(IMAGE_BL32)
Yann Gautieraaa09b72021-10-27 18:16:59 +0200429 _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
Yann Gautier7418cf32020-01-17 11:59:28 +0100430#endif
Yann Gautier7839a052018-07-24 17:13:36 +0200431
Yann Gautieraaa09b72021-10-27 18:16:59 +0200432 _CLK_SELEC(SEC, RCC_BDCR, 20, RTC, _RTC_SEL),
433 _CLK_SELEC(N_S, RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Yann Gautier7839a052018-07-24 17:13:36 +0200434};
435
Yann Gautier0d216802019-02-14 10:53:33 +0100436static const uint8_t i2c12_parents[] = {
437 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
438};
439
440static const uint8_t i2c35_parents[] = {
441 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
442};
443
444static const uint8_t stgen_parents[] = {
445 _HSI_KER, _HSE_KER
446};
447
448static const uint8_t i2c46_parents[] = {
449 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
450};
451
452static const uint8_t spi6_parents[] = {
453 _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
454};
455
456static const uint8_t usart1_parents[] = {
457 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
458};
459
460static const uint8_t rng1_parents[] = {
461 _CSI, _PLL4_R, _LSE, _LSI
462};
463
464static const uint8_t uart6_parents[] = {
465 _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
466};
467
468static const uint8_t uart234578_parents[] = {
469 _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
470};
471
472static const uint8_t sdmmc12_parents[] = {
473 _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
474};
475
476static const uint8_t sdmmc3_parents[] = {
477 _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
478};
479
480static const uint8_t qspi_parents[] = {
481 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
482};
483
484static const uint8_t fmc_parents[] = {
485 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
486};
487
Etienne Carriereb8fe48b2019-12-19 10:03:23 +0100488static const uint8_t axiss_parents[] = {
489 _HSI, _HSE, _PLL2_P
Yann Gautier0d216802019-02-14 10:53:33 +0100490};
491
Etienne Carriereb8fe48b2019-12-19 10:03:23 +0100492static const uint8_t mcuss_parents[] = {
493 _HSI, _HSE, _CSI, _PLL3_P
Yann Gautierb053a222019-02-15 17:33:27 +0100494};
495
Yann Gautier0d216802019-02-14 10:53:33 +0100496static const uint8_t usbphy_parents[] = {
497 _HSE_KER, _PLL4_R, _HSE_KER_DIV2
498};
499
500static const uint8_t usbo_parents[] = {
501 _PLL4_R, _USB_PHY_48
502};
Yann Gautier7839a052018-07-24 17:13:36 +0200503
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100504static const uint8_t mpu_parents[] = {
505 _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */
506};
507
508static const uint8_t per_parents[] = {
509 _HSI, _HSE, _CSI,
510};
511
Etienne Carriere016af002019-12-08 08:22:31 +0100512static const uint8_t rtc_parents[] = {
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200513 _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC
Etienne Carriere016af002019-12-08 08:22:31 +0100514};
515
Yann Gautier7839a052018-07-24 17:13:36 +0200516static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
Yann Gautierd4151d22019-05-07 18:49:33 +0200517 _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
518 _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
519 _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
520 _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
521 _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
522 _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
523 _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100524 _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
Yann Gautier288f5cf2021-08-31 18:23:13 +0200525 _CLK_PARENT_SEL(CKPER, RCC_CPERCKSELR, per_parents),
Etienne Carriere016af002019-12-08 08:22:31 +0100526 _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
Yann Gautierd4151d22019-05-07 18:49:33 +0200527 _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
528 _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
529 _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
530 _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
531 _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
532 _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
533 _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
534 _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
Etienne Carriereb8fe48b2019-12-19 10:03:23 +0100535 _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, axiss_parents),
536 _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mcuss_parents),
Yann Gautierd4151d22019-05-07 18:49:33 +0200537 _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
538 _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
Yann Gautier7839a052018-07-24 17:13:36 +0200539};
540
541/* Define characteristic of PLL according type */
542#define DIVN_MIN 24
543static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
544 [PLL_800] = {
545 .refclk_min = 4,
546 .refclk_max = 16,
547 .divn_max = 99,
548 },
549 [PLL_1600] = {
550 .refclk_min = 8,
551 .refclk_max = 16,
552 .divn_max = 199,
553 },
554};
555
556/* PLLNCFGR2 register divider by output */
557static const uint8_t pllncfgr2[_DIV_NB] = {
558 [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
559 [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautier0d216802019-02-14 10:53:33 +0100560 [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
Yann Gautier7839a052018-07-24 17:13:36 +0200561};
562
563static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautier0d216802019-02-14 10:53:33 +0100564 _CLK_PLL(_PLL1, PLL_1600,
565 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
566 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
567 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
568 _CLK_PLL(_PLL2, PLL_1600,
569 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
570 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
571 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
572 _CLK_PLL(_PLL3, PLL_800,
573 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
574 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
575 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
576 _CLK_PLL(_PLL4, PLL_800,
577 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
578 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
579 _HSI, _HSE, _CSI, _I2S_CKIN),
Yann Gautier7839a052018-07-24 17:13:36 +0200580};
581
582/* Prescaler table lookups for clock computation */
Yann Gautierb053a222019-02-15 17:33:27 +0100583/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
584static const uint8_t stm32mp1_mcu_div[16] = {
585 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
586};
Yann Gautier7839a052018-07-24 17:13:36 +0200587
588/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
589#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
590#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
591static const uint8_t stm32mp1_mpu_apbx_div[8] = {
592 0, 1, 2, 3, 4, 4, 4, 4
593};
594
595/* div = /1 /2 /3 /4 */
596static const uint8_t stm32mp1_axi_div[8] = {
597 1, 2, 3, 4, 4, 4, 4, 4
598};
599
Etienne Carriere37e82952020-05-13 11:49:49 +0200600static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
601 [_HSI] = "HSI",
602 [_HSE] = "HSE",
603 [_CSI] = "CSI",
604 [_LSI] = "LSI",
605 [_LSE] = "LSE",
606 [_I2S_CKIN] = "I2S_CKIN",
607 [_HSI_KER] = "HSI_KER",
608 [_HSE_KER] = "HSE_KER",
609 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200610 [_HSE_RTC] = "HSE_RTC",
Etienne Carriere37e82952020-05-13 11:49:49 +0200611 [_CSI_KER] = "CSI_KER",
612 [_PLL1_P] = "PLL1_P",
613 [_PLL1_Q] = "PLL1_Q",
614 [_PLL1_R] = "PLL1_R",
615 [_PLL2_P] = "PLL2_P",
616 [_PLL2_Q] = "PLL2_Q",
617 [_PLL2_R] = "PLL2_R",
618 [_PLL3_P] = "PLL3_P",
619 [_PLL3_Q] = "PLL3_Q",
620 [_PLL3_R] = "PLL3_R",
621 [_PLL4_P] = "PLL4_P",
622 [_PLL4_Q] = "PLL4_Q",
623 [_PLL4_R] = "PLL4_R",
624 [_ACLK] = "ACLK",
625 [_PCLK1] = "PCLK1",
626 [_PCLK2] = "PCLK2",
627 [_PCLK3] = "PCLK3",
628 [_PCLK4] = "PCLK4",
629 [_PCLK5] = "PCLK5",
630 [_HCLK6] = "KCLK6",
631 [_HCLK2] = "HCLK2",
632 [_CK_PER] = "CK_PER",
633 [_CK_MPU] = "CK_MPU",
634 [_CK_MCU] = "CK_MCU",
635 [_USB_PHY_48] = "USB_PHY_48",
636};
637
Yann Gautier0d216802019-02-14 10:53:33 +0100638/* RCC clock device driver private */
639static unsigned long stm32mp1_osc[NB_OSC];
640static struct spinlock reg_lock;
641static unsigned int gate_refcounts[NB_GATES];
642static struct spinlock refcount_lock;
Yann Gautier7839a052018-07-24 17:13:36 +0200643
Yann Gautier0d216802019-02-14 10:53:33 +0100644static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
645{
646 return &stm32mp1_clk_gate[idx];
647}
Yann Gautier7839a052018-07-24 17:13:36 +0200648
Yann Gautier3d691492021-10-27 18:21:11 +0200649#if defined(IMAGE_BL32)
650static bool gate_is_non_secure(const struct stm32mp1_clk_gate *gate)
651{
652 return gate->secure == N_S;
653}
654#endif
655
Yann Gautier0d216802019-02-14 10:53:33 +0100656static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
657{
658 return &stm32mp1_clk_sel[idx];
659}
660
661static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
662{
663 return &stm32mp1_clk_pll[idx];
664}
665
Yann Gautier0d216802019-02-14 10:53:33 +0100666static void stm32mp1_clk_lock(struct spinlock *lock)
667{
Yann Gautiere463d3f2019-05-22 19:13:51 +0200668 if (stm32mp_lock_available()) {
669 /* Assume interrupts are masked */
670 spin_lock(lock);
Yann Gautier0d216802019-02-14 10:53:33 +0100671 }
Yann Gautier0d216802019-02-14 10:53:33 +0100672}
673
674static void stm32mp1_clk_unlock(struct spinlock *lock)
675{
Yann Gautiere463d3f2019-05-22 19:13:51 +0200676 if (stm32mp_lock_available()) {
677 spin_unlock(lock);
Yann Gautier0d216802019-02-14 10:53:33 +0100678 }
Yann Gautier0d216802019-02-14 10:53:33 +0100679}
680
681bool stm32mp1_rcc_is_secure(void)
682{
683 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere1bb90722020-02-05 10:03:27 +0100684 uint32_t mask = RCC_TZCR_TZEN;
Yann Gautier0d216802019-02-14 10:53:33 +0100685
Etienne Carriere1bb90722020-02-05 10:03:27 +0100686 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautier0d216802019-02-14 10:53:33 +0100687}
688
Yann Gautierb053a222019-02-15 17:33:27 +0100689bool stm32mp1_rcc_is_mckprot(void)
690{
691 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere1bb90722020-02-05 10:03:27 +0100692 uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT;
Yann Gautierb053a222019-02-15 17:33:27 +0100693
Etienne Carriere1bb90722020-02-05 10:03:27 +0100694 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautierb053a222019-02-15 17:33:27 +0100695}
696
Yann Gautier0d216802019-02-14 10:53:33 +0100697void stm32mp1_clk_rcc_regs_lock(void)
698{
699 stm32mp1_clk_lock(&reg_lock);
700}
701
702void stm32mp1_clk_rcc_regs_unlock(void)
703{
704 stm32mp1_clk_unlock(&reg_lock);
705}
706
707static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier7839a052018-07-24 17:13:36 +0200708{
709 if (idx >= NB_OSC) {
710 return 0;
711 }
712
Yann Gautier0d216802019-02-14 10:53:33 +0100713 return stm32mp1_osc[idx];
Yann Gautier7839a052018-07-24 17:13:36 +0200714}
715
Yann Gautier0d216802019-02-14 10:53:33 +0100716static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +0200717{
Yann Gautier0d216802019-02-14 10:53:33 +0100718 unsigned int i;
Yann Gautier7839a052018-07-24 17:13:36 +0200719
Yann Gautier0d216802019-02-14 10:53:33 +0100720 for (i = 0U; i < NB_GATES; i++) {
721 if (gate_ref(i)->index == id) {
Yann Gautier7839a052018-07-24 17:13:36 +0200722 return i;
723 }
724 }
725
Yann Gautier44fb4702021-09-07 09:05:44 +0200726 ERROR("%s: clk id %lu not found\n", __func__, id);
Yann Gautier7839a052018-07-24 17:13:36 +0200727
728 return -EINVAL;
729}
730
Yann Gautier0d216802019-02-14 10:53:33 +0100731static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier7839a052018-07-24 17:13:36 +0200732{
Yann Gautier0d216802019-02-14 10:53:33 +0100733 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier7839a052018-07-24 17:13:36 +0200734}
735
Yann Gautier0d216802019-02-14 10:53:33 +0100736static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier7839a052018-07-24 17:13:36 +0200737{
Yann Gautier0d216802019-02-14 10:53:33 +0100738 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier7839a052018-07-24 17:13:36 +0200739}
740
Yann Gautier0d216802019-02-14 10:53:33 +0100741static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +0200742{
Yann Gautier0d216802019-02-14 10:53:33 +0100743 const struct stm32mp1_clk_sel *sel;
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100744 uint32_t p_sel;
Yann Gautier7839a052018-07-24 17:13:36 +0200745 int i;
746 enum stm32mp1_parent_id p;
747 enum stm32mp1_parent_sel s;
Yann Gautier0d216802019-02-14 10:53:33 +0100748 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +0200749
Etienne Carriere8fbcd9e2019-12-08 08:20:12 +0100750 /* Few non gateable clock have a static parent ID, find them */
751 i = (int)clock_id2parent_id(id);
752 if (i != _UNKNOWN_ID) {
753 return i;
Yann Gautier7839a052018-07-24 17:13:36 +0200754 }
755
Yann Gautier0d216802019-02-14 10:53:33 +0100756 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier7839a052018-07-24 17:13:36 +0200757 if (i < 0) {
Yann Gautier0d216802019-02-14 10:53:33 +0100758 panic();
Yann Gautier7839a052018-07-24 17:13:36 +0200759 }
760
Yann Gautier0d216802019-02-14 10:53:33 +0100761 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier7839a052018-07-24 17:13:36 +0200762 if (p < _PARENT_NB) {
763 return (int)p;
764 }
765
Yann Gautier0d216802019-02-14 10:53:33 +0100766 s = stm32mp1_clk_get_sel(i);
767 if (s == _UNKNOWN_SEL) {
Yann Gautier7839a052018-07-24 17:13:36 +0200768 return -EINVAL;
769 }
Yann Gautier0d216802019-02-14 10:53:33 +0100770 if (s >= _PARENT_SEL_NB) {
771 panic();
Yann Gautier7839a052018-07-24 17:13:36 +0200772 }
773
Yann Gautier0d216802019-02-14 10:53:33 +0100774 sel = clk_sel_ref(s);
Etienne Carriere8ae08dc2019-12-08 08:20:40 +0100775 p_sel = (mmio_read_32(rcc_base + sel->offset) &
776 (sel->msk << sel->src)) >> sel->src;
Yann Gautier0d216802019-02-14 10:53:33 +0100777 if (p_sel < sel->nb_parent) {
778 return (int)sel->parent[p_sel];
779 }
Yann Gautier7839a052018-07-24 17:13:36 +0200780
781 return -EINVAL;
782}
783
Yann Gautier0d216802019-02-14 10:53:33 +0100784static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier7839a052018-07-24 17:13:36 +0200785{
Yann Gautier0d216802019-02-14 10:53:33 +0100786 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
787 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier7839a052018-07-24 17:13:36 +0200788
Yann Gautier0d216802019-02-14 10:53:33 +0100789 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier7839a052018-07-24 17:13:36 +0200790}
791
792/*
793 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
794 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
795 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
796 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
797 */
Yann Gautier0d216802019-02-14 10:53:33 +0100798static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier7839a052018-07-24 17:13:36 +0200799{
Yann Gautier7839a052018-07-24 17:13:36 +0200800 unsigned long refclk, fvco;
801 uint32_t cfgr1, fracr, divm, divn;
Yann Gautier0d216802019-02-14 10:53:33 +0100802 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +0200803
Yann Gautier0d216802019-02-14 10:53:33 +0100804 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
805 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier7839a052018-07-24 17:13:36 +0200806
807 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
808 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
809
Yann Gautier0d216802019-02-14 10:53:33 +0100810 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier7839a052018-07-24 17:13:36 +0200811
812 /*
813 * With FRACV :
814 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
815 * Without FRACV
816 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
817 */
818 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +0100819 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
820 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier7839a052018-07-24 17:13:36 +0200821 unsigned long long numerator, denominator;
822
Yann Gautier0d216802019-02-14 10:53:33 +0100823 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
824 numerator = refclk * numerator;
825 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier7839a052018-07-24 17:13:36 +0200826 fvco = (unsigned long)(numerator / denominator);
827 } else {
828 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
829 }
830
831 return fvco;
832}
833
Yann Gautier0d216802019-02-14 10:53:33 +0100834static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier7839a052018-07-24 17:13:36 +0200835 enum stm32mp1_div_id div_id)
836{
Yann Gautier0d216802019-02-14 10:53:33 +0100837 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier7839a052018-07-24 17:13:36 +0200838 unsigned long dfout;
839 uint32_t cfgr2, divy;
840
841 if (div_id >= _DIV_NB) {
842 return 0;
843 }
844
Yann Gautier0d216802019-02-14 10:53:33 +0100845 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier7839a052018-07-24 17:13:36 +0200846 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
847
Yann Gautier0d216802019-02-14 10:53:33 +0100848 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier7839a052018-07-24 17:13:36 +0200849
850 return dfout;
851}
852
Yann Gautier0d216802019-02-14 10:53:33 +0100853static unsigned long get_clock_rate(int p)
Yann Gautier7839a052018-07-24 17:13:36 +0200854{
855 uint32_t reg, clkdiv;
856 unsigned long clock = 0;
Yann Gautier0d216802019-02-14 10:53:33 +0100857 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +0200858
859 switch (p) {
860 case _CK_MPU:
861 /* MPU sub system */
Yann Gautier0d216802019-02-14 10:53:33 +0100862 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier7839a052018-07-24 17:13:36 +0200863 switch (reg & RCC_SELR_SRC_MASK) {
864 case RCC_MPCKSELR_HSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100865 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200866 break;
867 case RCC_MPCKSELR_HSE:
Yann Gautier0d216802019-02-14 10:53:33 +0100868 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200869 break;
870 case RCC_MPCKSELR_PLL:
Yann Gautier0d216802019-02-14 10:53:33 +0100871 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +0200872 break;
873 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautier0d216802019-02-14 10:53:33 +0100874 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +0200875
Yann Gautier0d216802019-02-14 10:53:33 +0100876 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200877 clkdiv = reg & RCC_MPUDIV_MASK;
Gabriel Fernandez602ae2f2020-02-28 09:09:06 +0100878 clock >>= stm32mp1_mpu_div[clkdiv];
Yann Gautier7839a052018-07-24 17:13:36 +0200879 break;
880 default:
881 break;
882 }
883 break;
884 /* AXI sub system */
885 case _ACLK:
886 case _HCLK2:
887 case _HCLK6:
888 case _PCLK4:
889 case _PCLK5:
Yann Gautier0d216802019-02-14 10:53:33 +0100890 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier7839a052018-07-24 17:13:36 +0200891 switch (reg & RCC_SELR_SRC_MASK) {
892 case RCC_ASSCKSELR_HSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100893 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200894 break;
895 case RCC_ASSCKSELR_HSE:
Yann Gautier0d216802019-02-14 10:53:33 +0100896 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200897 break;
898 case RCC_ASSCKSELR_PLL:
Yann Gautier0d216802019-02-14 10:53:33 +0100899 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +0200900 break;
901 default:
902 break;
903 }
904
905 /* System clock divider */
Yann Gautier0d216802019-02-14 10:53:33 +0100906 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200907 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
908
909 switch (p) {
910 case _PCLK4:
Yann Gautier0d216802019-02-14 10:53:33 +0100911 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200912 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
913 break;
914 case _PCLK5:
Yann Gautier0d216802019-02-14 10:53:33 +0100915 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +0200916 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
917 break;
918 default:
919 break;
920 }
921 break;
Yann Gautierb053a222019-02-15 17:33:27 +0100922 /* MCU sub system */
923 case _CK_MCU:
924 case _PCLK1:
925 case _PCLK2:
926 case _PCLK3:
927 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
928 switch (reg & RCC_SELR_SRC_MASK) {
929 case RCC_MSSCKSELR_HSI:
930 clock = stm32mp1_clk_get_fixed(_HSI);
931 break;
932 case RCC_MSSCKSELR_HSE:
933 clock = stm32mp1_clk_get_fixed(_HSE);
934 break;
935 case RCC_MSSCKSELR_CSI:
936 clock = stm32mp1_clk_get_fixed(_CSI);
937 break;
938 case RCC_MSSCKSELR_PLL:
939 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
940 break;
941 default:
942 break;
943 }
944
945 /* MCU clock divider */
946 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
947 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
948
949 switch (p) {
950 case _PCLK1:
951 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
952 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
953 break;
954 case _PCLK2:
955 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
956 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
957 break;
958 case _PCLK3:
959 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
960 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
961 break;
962 case _CK_MCU:
963 default:
964 break;
965 }
966 break;
Yann Gautier7839a052018-07-24 17:13:36 +0200967 case _CK_PER:
Yann Gautier0d216802019-02-14 10:53:33 +0100968 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier7839a052018-07-24 17:13:36 +0200969 switch (reg & RCC_SELR_SRC_MASK) {
970 case RCC_CPERCKSELR_HSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100971 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200972 break;
973 case RCC_CPERCKSELR_HSE:
Yann Gautier0d216802019-02-14 10:53:33 +0100974 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200975 break;
976 case RCC_CPERCKSELR_CSI:
Yann Gautier0d216802019-02-14 10:53:33 +0100977 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200978 break;
979 default:
980 break;
981 }
982 break;
983 case _HSI:
984 case _HSI_KER:
Yann Gautier0d216802019-02-14 10:53:33 +0100985 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200986 break;
987 case _CSI:
988 case _CSI_KER:
Yann Gautier0d216802019-02-14 10:53:33 +0100989 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier7839a052018-07-24 17:13:36 +0200990 break;
991 case _HSE:
992 case _HSE_KER:
Yann Gautier0d216802019-02-14 10:53:33 +0100993 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier7839a052018-07-24 17:13:36 +0200994 break;
995 case _HSE_KER_DIV2:
Yann Gautier0d216802019-02-14 10:53:33 +0100996 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier7839a052018-07-24 17:13:36 +0200997 break;
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +0200998 case _HSE_RTC:
999 clock = stm32mp1_clk_get_fixed(_HSE);
1000 clock /= (mmio_read_32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U;
1001 break;
Yann Gautier7839a052018-07-24 17:13:36 +02001002 case _LSI:
Yann Gautier0d216802019-02-14 10:53:33 +01001003 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier7839a052018-07-24 17:13:36 +02001004 break;
1005 case _LSE:
Yann Gautier0d216802019-02-14 10:53:33 +01001006 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier7839a052018-07-24 17:13:36 +02001007 break;
1008 /* PLL */
1009 case _PLL1_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001010 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001011 break;
1012 case _PLL1_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001013 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001014 break;
1015 case _PLL1_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001016 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001017 break;
1018 case _PLL2_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001019 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001020 break;
1021 case _PLL2_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001022 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001023 break;
1024 case _PLL2_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001025 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001026 break;
1027 case _PLL3_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001028 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001029 break;
1030 case _PLL3_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001031 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001032 break;
1033 case _PLL3_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001034 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001035 break;
1036 case _PLL4_P:
Yann Gautier0d216802019-02-14 10:53:33 +01001037 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier7839a052018-07-24 17:13:36 +02001038 break;
1039 case _PLL4_Q:
Yann Gautier0d216802019-02-14 10:53:33 +01001040 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier7839a052018-07-24 17:13:36 +02001041 break;
1042 case _PLL4_R:
Yann Gautier0d216802019-02-14 10:53:33 +01001043 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier7839a052018-07-24 17:13:36 +02001044 break;
1045 /* Other */
1046 case _USB_PHY_48:
Yann Gautier0d216802019-02-14 10:53:33 +01001047 clock = USB_PHY_48_MHZ;
Yann Gautier7839a052018-07-24 17:13:36 +02001048 break;
1049 default:
1050 break;
1051 }
1052
1053 return clock;
1054}
1055
Yann Gautier0d216802019-02-14 10:53:33 +01001056static void __clk_enable(struct stm32mp1_clk_gate const *gate)
1057{
1058 uintptr_t rcc_base = stm32mp_rcc_base();
1059
Etienne Carriere25be8452019-12-08 08:21:08 +01001060 VERBOSE("Enable clock %u\n", gate->index);
1061
Yann Gautier0d216802019-02-14 10:53:33 +01001062 if (gate->set_clr != 0U) {
1063 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
1064 } else {
1065 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
1066 }
Yann Gautier0d216802019-02-14 10:53:33 +01001067}
1068
1069static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1070{
1071 uintptr_t rcc_base = stm32mp_rcc_base();
1072
Etienne Carriere25be8452019-12-08 08:21:08 +01001073 VERBOSE("Disable clock %u\n", gate->index);
1074
Yann Gautier0d216802019-02-14 10:53:33 +01001075 if (gate->set_clr != 0U) {
1076 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1077 BIT(gate->bit));
1078 } else {
1079 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1080 }
Yann Gautier0d216802019-02-14 10:53:33 +01001081}
1082
1083static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1084{
1085 uintptr_t rcc_base = stm32mp_rcc_base();
1086
1087 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1088}
1089
Etienne Carriere35848202019-12-08 08:21:44 +01001090/* Oscillators and PLLs are not gated at runtime */
1091static bool clock_is_always_on(unsigned long id)
1092{
1093 switch (id) {
1094 case CK_HSE:
1095 case CK_CSI:
1096 case CK_LSI:
1097 case CK_LSE:
1098 case CK_HSI:
1099 case CK_HSE_DIV2:
1100 case PLL1_Q:
1101 case PLL1_R:
1102 case PLL2_P:
1103 case PLL2_Q:
1104 case PLL2_R:
1105 case PLL3_P:
1106 case PLL3_Q:
1107 case PLL3_R:
Yann Gautierbf393182020-09-16 16:41:55 +02001108 case CK_AXI:
1109 case CK_MPU:
1110 case CK_MCU:
HE Shushan5b111c72021-07-12 23:04:10 +02001111 case RTC:
Etienne Carriere35848202019-12-08 08:21:44 +01001112 return true;
1113 default:
1114 return false;
1115 }
1116}
1117
Yann Gautier2444d232022-01-19 13:57:49 +01001118static void __stm32mp1_clk_enable(unsigned long id, bool with_refcnt)
Yann Gautier0d216802019-02-14 10:53:33 +01001119{
1120 const struct stm32mp1_clk_gate *gate;
Etienne Carriere35848202019-12-08 08:21:44 +01001121 int i;
Yann Gautier0d216802019-02-14 10:53:33 +01001122
Etienne Carriere35848202019-12-08 08:21:44 +01001123 if (clock_is_always_on(id)) {
1124 return;
1125 }
1126
1127 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier0d216802019-02-14 10:53:33 +01001128 if (i < 0) {
Yann Gautier44fb4702021-09-07 09:05:44 +02001129 ERROR("Clock %lu can't be enabled\n", id);
Yann Gautier0d216802019-02-14 10:53:33 +01001130 panic();
1131 }
1132
1133 gate = gate_ref(i);
Yann Gautier2444d232022-01-19 13:57:49 +01001134
1135 if (!with_refcnt) {
1136 __clk_enable(gate);
1137 return;
1138 }
Yann Gautier0d216802019-02-14 10:53:33 +01001139
Yann Gautier3d691492021-10-27 18:21:11 +02001140#if defined(IMAGE_BL32)
1141 if (gate_is_non_secure(gate)) {
1142 /* Enable non-secure clock w/o any refcounting */
1143 __clk_enable(gate);
1144 return;
1145 }
1146#endif
1147
Yann Gautier0d216802019-02-14 10:53:33 +01001148 stm32mp1_clk_lock(&refcount_lock);
1149
Yann Gautier2444d232022-01-19 13:57:49 +01001150 if (gate_refcounts[i] == 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001151 __clk_enable(gate);
1152 }
1153
Yann Gautier2444d232022-01-19 13:57:49 +01001154 gate_refcounts[i]++;
1155 if (gate_refcounts[i] == UINT_MAX) {
1156 ERROR("Clock %lu refcount reached max value\n", id);
1157 panic();
1158 }
1159
Yann Gautier0d216802019-02-14 10:53:33 +01001160 stm32mp1_clk_unlock(&refcount_lock);
1161}
1162
Yann Gautier2444d232022-01-19 13:57:49 +01001163static void __stm32mp1_clk_disable(unsigned long id, bool with_refcnt)
Yann Gautier0d216802019-02-14 10:53:33 +01001164{
1165 const struct stm32mp1_clk_gate *gate;
Etienne Carriere35848202019-12-08 08:21:44 +01001166 int i;
Yann Gautier0d216802019-02-14 10:53:33 +01001167
Etienne Carriere35848202019-12-08 08:21:44 +01001168 if (clock_is_always_on(id)) {
1169 return;
1170 }
1171
1172 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier0d216802019-02-14 10:53:33 +01001173 if (i < 0) {
Yann Gautier44fb4702021-09-07 09:05:44 +02001174 ERROR("Clock %lu can't be disabled\n", id);
Yann Gautier0d216802019-02-14 10:53:33 +01001175 panic();
1176 }
1177
1178 gate = gate_ref(i);
Yann Gautier2444d232022-01-19 13:57:49 +01001179
1180 if (!with_refcnt) {
1181 __clk_disable(gate);
1182 return;
1183 }
Yann Gautier0d216802019-02-14 10:53:33 +01001184
Yann Gautier3d691492021-10-27 18:21:11 +02001185#if defined(IMAGE_BL32)
1186 if (gate_is_non_secure(gate)) {
1187 /* Don't disable non-secure clocks */
1188 return;
1189 }
1190#endif
1191
Yann Gautier0d216802019-02-14 10:53:33 +01001192 stm32mp1_clk_lock(&refcount_lock);
1193
Yann Gautier2444d232022-01-19 13:57:49 +01001194 if (gate_refcounts[i] == 0U) {
1195 ERROR("Clock %lu refcount reached 0\n", id);
1196 panic();
1197 }
1198 gate_refcounts[i]--;
1199
1200 if (gate_refcounts[i] == 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001201 __clk_disable(gate);
1202 }
1203
1204 stm32mp1_clk_unlock(&refcount_lock);
1205}
1206
Yann Gautier33667d22021-08-30 15:06:54 +02001207static int stm32mp_clk_enable(unsigned long id)
Yann Gautier0d216802019-02-14 10:53:33 +01001208{
1209 __stm32mp1_clk_enable(id, true);
Yann Gautier33667d22021-08-30 15:06:54 +02001210
1211 return 0;
Yann Gautier0d216802019-02-14 10:53:33 +01001212}
1213
Yann Gautier33667d22021-08-30 15:06:54 +02001214static void stm32mp_clk_disable(unsigned long id)
Yann Gautier0d216802019-02-14 10:53:33 +01001215{
1216 __stm32mp1_clk_disable(id, true);
1217}
1218
Yann Gautier33667d22021-08-30 15:06:54 +02001219static bool stm32mp_clk_is_enabled(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +02001220{
Etienne Carriere35848202019-12-08 08:21:44 +01001221 int i;
Yann Gautier7839a052018-07-24 17:13:36 +02001222
Etienne Carriere35848202019-12-08 08:21:44 +01001223 if (clock_is_always_on(id)) {
1224 return true;
1225 }
1226
1227 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier7839a052018-07-24 17:13:36 +02001228 if (i < 0) {
Yann Gautier0d216802019-02-14 10:53:33 +01001229 panic();
Yann Gautier7839a052018-07-24 17:13:36 +02001230 }
1231
Yann Gautier0d216802019-02-14 10:53:33 +01001232 return __clk_is_enabled(gate_ref(i));
Yann Gautier7839a052018-07-24 17:13:36 +02001233}
1234
Yann Gautier33667d22021-08-30 15:06:54 +02001235static unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier7839a052018-07-24 17:13:36 +02001236{
Yann Gautier33667d22021-08-30 15:06:54 +02001237 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier0d216802019-02-14 10:53:33 +01001238 int p = stm32mp1_clk_get_parent(id);
Yann Gautier33667d22021-08-30 15:06:54 +02001239 uint32_t prescaler, timpre;
1240 unsigned long parent_rate;
Yann Gautier7839a052018-07-24 17:13:36 +02001241
1242 if (p < 0) {
1243 return 0;
1244 }
1245
Yann Gautier33667d22021-08-30 15:06:54 +02001246 parent_rate = get_clock_rate(p);
1247
1248 switch (id) {
1249 case TIM2_K:
1250 case TIM3_K:
1251 case TIM4_K:
1252 case TIM5_K:
1253 case TIM6_K:
1254 case TIM7_K:
1255 case TIM12_K:
1256 case TIM13_K:
1257 case TIM14_K:
1258 prescaler = mmio_read_32(rcc_base + RCC_APB1DIVR) &
1259 RCC_APBXDIV_MASK;
1260 timpre = mmio_read_32(rcc_base + RCC_TIMG1PRER) &
1261 RCC_TIMGXPRER_TIMGXPRE;
1262 break;
1263
1264 case TIM1_K:
1265 case TIM8_K:
1266 case TIM15_K:
1267 case TIM16_K:
1268 case TIM17_K:
1269 prescaler = mmio_read_32(rcc_base + RCC_APB2DIVR) &
1270 RCC_APBXDIV_MASK;
1271 timpre = mmio_read_32(rcc_base + RCC_TIMG2PRER) &
1272 RCC_TIMGXPRER_TIMGXPRE;
1273 break;
1274
1275 default:
1276 return parent_rate;
1277 }
1278
1279 if (prescaler == 0U) {
1280 return parent_rate;
1281 }
1282
1283 return parent_rate * (timpre + 1U) * 2U;
Yann Gautier7839a052018-07-24 17:13:36 +02001284}
1285
Yann Gautier0d216802019-02-14 10:53:33 +01001286static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier7839a052018-07-24 17:13:36 +02001287{
Yann Gautier0d216802019-02-14 10:53:33 +01001288 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier7839a052018-07-24 17:13:36 +02001289
Yann Gautier0d216802019-02-14 10:53:33 +01001290 if (enable) {
Yann Gautier7839a052018-07-24 17:13:36 +02001291 mmio_setbits_32(address, mask_on);
1292 } else {
1293 mmio_clrbits_32(address, mask_on);
1294 }
1295}
1296
Yann Gautier0d216802019-02-14 10:53:33 +01001297static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier7839a052018-07-24 17:13:36 +02001298{
Yann Gautier0d216802019-02-14 10:53:33 +01001299 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1300 uintptr_t address = stm32mp_rcc_base() + offset;
1301
1302 mmio_write_32(address, mask_on);
Yann Gautier7839a052018-07-24 17:13:36 +02001303}
1304
Yann Gautier0d216802019-02-14 10:53:33 +01001305static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier7839a052018-07-24 17:13:36 +02001306{
Yann Gautierdfdb0572019-02-14 11:14:39 +01001307 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001308 uint32_t mask_test;
Yann Gautier0d216802019-02-14 10:53:33 +01001309 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier7839a052018-07-24 17:13:36 +02001310
Yann Gautier0d216802019-02-14 10:53:33 +01001311 if (enable) {
Yann Gautier7839a052018-07-24 17:13:36 +02001312 mask_test = mask_rdy;
1313 } else {
1314 mask_test = 0;
1315 }
1316
Yann Gautierdfdb0572019-02-14 11:14:39 +01001317 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001318 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001319 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001320 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001321 mask_rdy, address, enable, mmio_read_32(address));
1322 return -ETIMEDOUT;
1323 }
1324 }
1325
1326 return 0;
1327}
1328
Yann Gautier0d216802019-02-14 10:53:33 +01001329static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier7839a052018-07-24 17:13:36 +02001330{
1331 uint32_t value;
Yann Gautier0d216802019-02-14 10:53:33 +01001332 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +02001333
Yann Gautier0d216802019-02-14 10:53:33 +01001334 if (digbyp) {
1335 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
1336 }
1337
1338 if (bypass || digbyp) {
1339 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
Yann Gautier7839a052018-07-24 17:13:36 +02001340 }
1341
1342 /*
1343 * Warning: not recommended to switch directly from "high drive"
1344 * to "medium low drive", and vice-versa.
1345 */
Yann Gautier0d216802019-02-14 10:53:33 +01001346 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier7839a052018-07-24 17:13:36 +02001347 RCC_BDCR_LSEDRV_SHIFT;
1348
1349 while (value != lsedrv) {
1350 if (value > lsedrv) {
1351 value--;
1352 } else {
1353 value++;
1354 }
1355
Yann Gautier0d216802019-02-14 10:53:33 +01001356 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier7839a052018-07-24 17:13:36 +02001357 RCC_BDCR_LSEDRV_MASK,
1358 value << RCC_BDCR_LSEDRV_SHIFT);
1359 }
1360
Yann Gautier0d216802019-02-14 10:53:33 +01001361 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier7839a052018-07-24 17:13:36 +02001362}
1363
Yann Gautier0d216802019-02-14 10:53:33 +01001364static void stm32mp1_lse_wait(void)
Yann Gautier7839a052018-07-24 17:13:36 +02001365{
Yann Gautier0d216802019-02-14 10:53:33 +01001366 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001367 VERBOSE("%s: failed\n", __func__);
1368 }
1369}
1370
Yann Gautier0d216802019-02-14 10:53:33 +01001371static void stm32mp1_lsi_set(bool enable)
Yann Gautier7839a052018-07-24 17:13:36 +02001372{
Yann Gautier0d216802019-02-14 10:53:33 +01001373 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1374
1375 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001376 VERBOSE("%s: failed\n", __func__);
1377 }
1378}
1379
Yann Gautier0d216802019-02-14 10:53:33 +01001380static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier7839a052018-07-24 17:13:36 +02001381{
Yann Gautier0d216802019-02-14 10:53:33 +01001382 uintptr_t rcc_base = stm32mp_rcc_base();
1383
1384 if (digbyp) {
1385 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier7839a052018-07-24 17:13:36 +02001386 }
1387
Yann Gautier0d216802019-02-14 10:53:33 +01001388 if (bypass || digbyp) {
1389 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1390 }
1391
1392 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1393 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001394 VERBOSE("%s: failed\n", __func__);
1395 }
1396
1397 if (css) {
Yann Gautier0d216802019-02-14 10:53:33 +01001398 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier7839a052018-07-24 17:13:36 +02001399 }
Lionel Debieve31e97502019-07-02 18:03:34 +02001400
1401#if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
1402 if ((mmio_read_32(rcc_base + RCC_OCENSETR) & RCC_OCENR_HSEBYP) &&
1403 (!(digbyp || bypass))) {
1404 panic();
1405 }
1406#endif
Yann Gautier7839a052018-07-24 17:13:36 +02001407}
1408
Yann Gautier0d216802019-02-14 10:53:33 +01001409static void stm32mp1_csi_set(bool enable)
Yann Gautier7839a052018-07-24 17:13:36 +02001410{
Yann Gautier0d216802019-02-14 10:53:33 +01001411 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1412 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001413 VERBOSE("%s: failed\n", __func__);
1414 }
1415}
1416
Yann Gautier0d216802019-02-14 10:53:33 +01001417static void stm32mp1_hsi_set(bool enable)
Yann Gautier7839a052018-07-24 17:13:36 +02001418{
Yann Gautier0d216802019-02-14 10:53:33 +01001419 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1420 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier7839a052018-07-24 17:13:36 +02001421 VERBOSE("%s: failed\n", __func__);
1422 }
1423}
1424
Yann Gautier0d216802019-02-14 10:53:33 +01001425static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier7839a052018-07-24 17:13:36 +02001426{
Yann Gautierdfdb0572019-02-14 11:14:39 +01001427 uint64_t timeout;
Yann Gautier0d216802019-02-14 10:53:33 +01001428 uintptr_t rcc_base = stm32mp_rcc_base();
1429 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier7839a052018-07-24 17:13:36 +02001430
Yann Gautier0d216802019-02-14 10:53:33 +01001431 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier7839a052018-07-24 17:13:36 +02001432 RCC_HSICFGR_HSIDIV_MASK,
1433 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1434
Yann Gautierdfdb0572019-02-14 11:14:39 +01001435 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001436 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001437 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001438 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001439 address, mmio_read_32(address));
1440 return -ETIMEDOUT;
1441 }
1442 }
1443
1444 return 0;
1445}
1446
Yann Gautier0d216802019-02-14 10:53:33 +01001447static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier7839a052018-07-24 17:13:36 +02001448{
1449 uint8_t hsidiv;
1450 uint32_t hsidivfreq = MAX_HSI_HZ;
1451
1452 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1453 if (hsidivfreq == hsifreq) {
1454 break;
1455 }
1456
1457 hsidivfreq /= 2U;
1458 }
1459
1460 if (hsidiv == 4U) {
1461 ERROR("Invalid clk-hsi frequency\n");
1462 return -1;
1463 }
1464
1465 if (hsidiv != 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001466 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier7839a052018-07-24 17:13:36 +02001467 }
1468
1469 return 0;
1470}
1471
Yann Gautier0d216802019-02-14 10:53:33 +01001472static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1473 unsigned int clksrc,
1474 uint32_t *pllcfg, int plloff)
Yann Gautier7839a052018-07-24 17:13:36 +02001475{
Yann Gautier0d216802019-02-14 10:53:33 +01001476 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1477 uintptr_t rcc_base = stm32mp_rcc_base();
1478 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1479 enum stm32mp1_plltype type = pll->plltype;
1480 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1481 unsigned long refclk;
1482 uint32_t ifrge = 0U;
Andre Przywarabe858cf2020-03-26 11:50:33 +00001483 uint32_t src, value, fracv = 0;
1484 void *fdt;
Yann Gautier7839a052018-07-24 17:13:36 +02001485
Yann Gautier0d216802019-02-14 10:53:33 +01001486 /* Check PLL output */
1487 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1488 return false;
1489 }
1490
1491 /* Check current clksrc */
1492 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1493 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1494 return false;
1495 }
1496
1497 /* Check Div */
1498 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1499
1500 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1501 (pllcfg[PLLCFG_M] + 1U);
1502
1503 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1504 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1505 return false;
1506 }
1507
1508 if ((type == PLL_800) && (refclk >= 8000000U)) {
1509 ifrge = 1U;
1510 }
1511
1512 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1513 RCC_PLLNCFGR1_DIVN_MASK;
1514 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1515 RCC_PLLNCFGR1_DIVM_MASK;
1516 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1517 RCC_PLLNCFGR1_IFRGE_MASK;
1518 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1519 return false;
1520 }
1521
1522 /* Fractional configuration */
Andre Przywarabe858cf2020-03-26 11:50:33 +00001523 if (fdt_get_address(&fdt) == 1) {
1524 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1525 }
Yann Gautier0d216802019-02-14 10:53:33 +01001526
1527 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1528 value |= RCC_PLLNFRACR_FRACLE;
1529 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1530 return false;
1531 }
1532
1533 /* Output config */
1534 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1535 RCC_PLLNCFGR2_DIVP_MASK;
1536 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1537 RCC_PLLNCFGR2_DIVQ_MASK;
1538 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1539 RCC_PLLNCFGR2_DIVR_MASK;
1540 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1541 return false;
1542 }
1543
1544 return true;
Yann Gautier7839a052018-07-24 17:13:36 +02001545}
1546
Yann Gautier0d216802019-02-14 10:53:33 +01001547static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier7839a052018-07-24 17:13:36 +02001548{
Yann Gautier0d216802019-02-14 10:53:33 +01001549 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1550 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
1551
Yann Gautierdd98aec2019-06-04 15:55:37 +02001552 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1553 mmio_clrsetbits_32(pllxcr,
1554 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1555 RCC_PLLNCR_DIVREN,
1556 RCC_PLLNCR_PLLON);
Yann Gautier0d216802019-02-14 10:53:33 +01001557}
1558
1559static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
1560{
1561 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1562 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautierdfdb0572019-02-14 11:14:39 +01001563 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001564
Yann Gautier7839a052018-07-24 17:13:36 +02001565 /* Wait PLL lock */
1566 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001567 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001568 ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001569 pll_id, pllxcr, mmio_read_32(pllxcr));
1570 return -ETIMEDOUT;
1571 }
1572 }
1573
1574 /* Start the requested output */
1575 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1576
1577 return 0;
1578}
1579
Yann Gautier0d216802019-02-14 10:53:33 +01001580static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier7839a052018-07-24 17:13:36 +02001581{
Yann Gautier0d216802019-02-14 10:53:33 +01001582 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1583 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautierdfdb0572019-02-14 11:14:39 +01001584 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001585
1586 /* Stop all output */
1587 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1588 RCC_PLLNCR_DIVREN);
1589
1590 /* Stop PLL */
1591 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1592
Yann Gautierdfdb0572019-02-14 11:14:39 +01001593 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001594 /* Wait PLL stopped */
1595 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001596 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001597 ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001598 pll_id, pllxcr, mmio_read_32(pllxcr));
1599 return -ETIMEDOUT;
1600 }
1601 }
1602
1603 return 0;
1604}
1605
Yann Gautier0d216802019-02-14 10:53:33 +01001606static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier7839a052018-07-24 17:13:36 +02001607 uint32_t *pllcfg)
1608{
Yann Gautier0d216802019-02-14 10:53:33 +01001609 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1610 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier7839a052018-07-24 17:13:36 +02001611 uint32_t value;
1612
1613 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1614 RCC_PLLNCFGR2_DIVP_MASK;
1615 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1616 RCC_PLLNCFGR2_DIVQ_MASK;
1617 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1618 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautier0d216802019-02-14 10:53:33 +01001619 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001620}
1621
Yann Gautier0d216802019-02-14 10:53:33 +01001622static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier7839a052018-07-24 17:13:36 +02001623 uint32_t *pllcfg, uint32_t fracv)
1624{
Yann Gautier0d216802019-02-14 10:53:33 +01001625 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1626 uintptr_t rcc_base = stm32mp_rcc_base();
1627 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier7839a052018-07-24 17:13:36 +02001628 unsigned long refclk;
1629 uint32_t ifrge = 0;
1630 uint32_t src, value;
1631
Yann Gautier0d216802019-02-14 10:53:33 +01001632 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier7839a052018-07-24 17:13:36 +02001633 RCC_SELR_REFCLK_SRC_MASK;
1634
Yann Gautier0d216802019-02-14 10:53:33 +01001635 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier7839a052018-07-24 17:13:36 +02001636 (pllcfg[PLLCFG_M] + 1U);
1637
1638 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1639 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1640 return -EINVAL;
1641 }
1642
1643 if ((type == PLL_800) && (refclk >= 8000000U)) {
1644 ifrge = 1U;
1645 }
1646
1647 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1648 RCC_PLLNCFGR1_DIVN_MASK;
1649 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1650 RCC_PLLNCFGR1_DIVM_MASK;
1651 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1652 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautier0d216802019-02-14 10:53:33 +01001653 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001654
1655 /* Fractional configuration */
1656 value = 0;
Yann Gautier0d216802019-02-14 10:53:33 +01001657 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001658
1659 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier0d216802019-02-14 10:53:33 +01001660 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001661
1662 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautier0d216802019-02-14 10:53:33 +01001663 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier7839a052018-07-24 17:13:36 +02001664
Yann Gautier0d216802019-02-14 10:53:33 +01001665 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier7839a052018-07-24 17:13:36 +02001666
1667 return 0;
1668}
1669
Yann Gautier0d216802019-02-14 10:53:33 +01001670static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier7839a052018-07-24 17:13:36 +02001671{
Yann Gautier0d216802019-02-14 10:53:33 +01001672 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier7839a052018-07-24 17:13:36 +02001673 uint32_t pllxcsg = 0;
1674
1675 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1676 RCC_PLLNCSGR_MOD_PER_MASK;
1677
1678 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1679 RCC_PLLNCSGR_INC_STEP_MASK;
1680
1681 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1682 RCC_PLLNCSGR_SSCG_MODE_MASK;
1683
Yann Gautier0d216802019-02-14 10:53:33 +01001684 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierdd98aec2019-06-04 15:55:37 +02001685
1686 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1687 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier7839a052018-07-24 17:13:36 +02001688}
1689
Yann Gautier0d216802019-02-14 10:53:33 +01001690static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier7839a052018-07-24 17:13:36 +02001691{
Yann Gautier0d216802019-02-14 10:53:33 +01001692 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautierdfdb0572019-02-14 11:14:39 +01001693 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001694
Yann Gautier0d216802019-02-14 10:53:33 +01001695 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier7839a052018-07-24 17:13:36 +02001696 clksrc & RCC_SELR_SRC_MASK);
1697
Yann Gautierdfdb0572019-02-14 11:14:39 +01001698 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautier0d216802019-02-14 10:53:33 +01001699 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001700 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001701 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1702 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier7839a052018-07-24 17:13:36 +02001703 return -ETIMEDOUT;
1704 }
1705 }
1706
1707 return 0;
1708}
1709
Yann Gautier0d216802019-02-14 10:53:33 +01001710static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier7839a052018-07-24 17:13:36 +02001711{
Yann Gautierdfdb0572019-02-14 11:14:39 +01001712 uint64_t timeout;
Yann Gautier7839a052018-07-24 17:13:36 +02001713
1714 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1715 clkdiv & RCC_DIVR_DIV_MASK);
1716
Yann Gautierdfdb0572019-02-14 11:14:39 +01001717 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier7839a052018-07-24 17:13:36 +02001718 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautierdfdb0572019-02-14 11:14:39 +01001719 if (timeout_elapsed(timeout)) {
Yann Gautier0d216802019-02-14 10:53:33 +01001720 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier7839a052018-07-24 17:13:36 +02001721 clkdiv, address, mmio_read_32(address));
1722 return -ETIMEDOUT;
1723 }
1724 }
1725
1726 return 0;
1727}
1728
Yann Gautier0d216802019-02-14 10:53:33 +01001729static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier7839a052018-07-24 17:13:36 +02001730{
Yann Gautier0d216802019-02-14 10:53:33 +01001731 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier7839a052018-07-24 17:13:36 +02001732
1733 /*
1734 * Binding clksrc :
1735 * bit15-4 offset
1736 * bit3: disable
1737 * bit2-0: MCOSEL[2:0]
1738 */
1739 if ((clksrc & 0x8U) != 0U) {
Yann Gautier0d216802019-02-14 10:53:33 +01001740 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier7839a052018-07-24 17:13:36 +02001741 } else {
Yann Gautier0d216802019-02-14 10:53:33 +01001742 mmio_clrsetbits_32(clksrc_address,
Yann Gautier7839a052018-07-24 17:13:36 +02001743 RCC_MCOCFG_MCOSRC_MASK,
1744 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautier0d216802019-02-14 10:53:33 +01001745 mmio_clrsetbits_32(clksrc_address,
Yann Gautier7839a052018-07-24 17:13:36 +02001746 RCC_MCOCFG_MCODIV_MASK,
1747 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautier0d216802019-02-14 10:53:33 +01001748 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier7839a052018-07-24 17:13:36 +02001749 }
1750}
1751
Yann Gautier0d216802019-02-14 10:53:33 +01001752static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier7839a052018-07-24 17:13:36 +02001753{
Yann Gautier0d216802019-02-14 10:53:33 +01001754 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier7839a052018-07-24 17:13:36 +02001755
1756 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1757 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1758 mmio_clrsetbits_32(address,
1759 RCC_BDCR_RTCSRC_MASK,
Yann Gautier15509092021-04-06 13:41:19 +02001760 (clksrc & RCC_SELR_SRC_MASK) << RCC_BDCR_RTCSRC_SHIFT);
Yann Gautier7839a052018-07-24 17:13:36 +02001761
1762 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1763 }
1764
1765 if (lse_css) {
1766 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1767 }
1768}
1769
Yann Gautier0d216802019-02-14 10:53:33 +01001770static void stm32mp1_stgen_config(void)
Yann Gautier7839a052018-07-24 17:13:36 +02001771{
Yann Gautier7839a052018-07-24 17:13:36 +02001772 uint32_t cntfid0;
1773 unsigned long rate;
Yann Gautier0d216802019-02-14 10:53:33 +01001774 unsigned long long counter;
Yann Gautier7839a052018-07-24 17:13:36 +02001775
Yann Gautierade9ce02020-05-05 17:58:40 +02001776 cntfid0 = mmio_read_32(STGEN_BASE + CNTFID_OFF);
Yann Gautier0d216802019-02-14 10:53:33 +01001777 rate = get_clock_rate(stm32mp1_clk_get_parent(STGEN_K));
Yann Gautier7839a052018-07-24 17:13:36 +02001778
Yann Gautier0d216802019-02-14 10:53:33 +01001779 if (cntfid0 == rate) {
1780 return;
Yann Gautier7839a052018-07-24 17:13:36 +02001781 }
Yann Gautier0d216802019-02-14 10:53:33 +01001782
Yann Gautierade9ce02020-05-05 17:58:40 +02001783 mmio_clrbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
1784 counter = (unsigned long long)mmio_read_32(STGEN_BASE + CNTCVL_OFF);
1785 counter |= ((unsigned long long)mmio_read_32(STGEN_BASE + CNTCVU_OFF)) << 32;
Yann Gautier0d216802019-02-14 10:53:33 +01001786 counter = (counter * rate / cntfid0);
1787
Yann Gautierade9ce02020-05-05 17:58:40 +02001788 mmio_write_32(STGEN_BASE + CNTCVL_OFF, (uint32_t)counter);
1789 mmio_write_32(STGEN_BASE + CNTCVU_OFF, (uint32_t)(counter >> 32));
1790 mmio_write_32(STGEN_BASE + CNTFID_OFF, rate);
1791 mmio_setbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
Yann Gautier0d216802019-02-14 10:53:33 +01001792
1793 write_cntfrq((u_register_t)rate);
1794
1795 /* Need to update timer with new frequency */
1796 generic_delay_timer_init();
Yann Gautier7839a052018-07-24 17:13:36 +02001797}
1798
1799void stm32mp1_stgen_increment(unsigned long long offset_in_ms)
1800{
Yann Gautier7839a052018-07-24 17:13:36 +02001801 unsigned long long cnt;
1802
Yann Gautierade9ce02020-05-05 17:58:40 +02001803 cnt = ((unsigned long long)mmio_read_32(STGEN_BASE + CNTCVU_OFF) << 32) |
1804 mmio_read_32(STGEN_BASE + CNTCVL_OFF);
Yann Gautier7839a052018-07-24 17:13:36 +02001805
Yann Gautierade9ce02020-05-05 17:58:40 +02001806 cnt += (offset_in_ms * mmio_read_32(STGEN_BASE + CNTFID_OFF)) / 1000U;
Yann Gautier7839a052018-07-24 17:13:36 +02001807
Yann Gautierade9ce02020-05-05 17:58:40 +02001808 mmio_clrbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
1809 mmio_write_32(STGEN_BASE + CNTCVL_OFF, (uint32_t)cnt);
1810 mmio_write_32(STGEN_BASE + CNTCVU_OFF, (uint32_t)(cnt >> 32));
1811 mmio_setbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
Yann Gautier7839a052018-07-24 17:13:36 +02001812}
1813
Yann Gautier0d216802019-02-14 10:53:33 +01001814static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier7839a052018-07-24 17:13:36 +02001815{
Yann Gautier0d216802019-02-14 10:53:33 +01001816 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier7839a052018-07-24 17:13:36 +02001817 uint32_t value = pkcs & 0xFU;
1818 uint32_t mask = 0xFU;
1819
1820 if ((pkcs & BIT(31)) != 0U) {
1821 mask <<= 4;
1822 value <<= 4;
1823 }
1824
1825 mmio_clrsetbits_32(address, mask, value);
1826}
1827
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001828static int clk_get_pll_settings_from_dt(int plloff, unsigned int *pllcfg,
1829 uint32_t *fracv, uint32_t *csg,
1830 bool *csg_set)
1831{
1832 void *fdt;
1833 int ret;
1834
1835 if (fdt_get_address(&fdt) == 0) {
1836 return -FDT_ERR_NOTFOUND;
1837 }
1838
1839 ret = fdt_read_uint32_array(fdt, plloff, "cfg", (uint32_t)PLLCFG_NB,
1840 pllcfg);
1841 if (ret < 0) {
1842 return -FDT_ERR_NOTFOUND;
1843 }
1844
1845 *fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1846
1847 ret = fdt_read_uint32_array(fdt, plloff, "csg", (uint32_t)PLLCSG_NB,
1848 csg);
1849
1850 *csg_set = (ret == 0);
1851
1852 if (ret == -FDT_ERR_NOTFOUND) {
1853 ret = 0;
1854 }
1855
1856 return ret;
1857}
1858
Yann Gautier7839a052018-07-24 17:13:36 +02001859int stm32mp1_clk_init(void)
1860{
Yann Gautier0d216802019-02-14 10:53:33 +01001861 uintptr_t rcc_base = stm32mp_rcc_base();
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001862 uint32_t pllfracv[_PLL_NB];
1863 uint32_t pllcsg[_PLL_NB][PLLCSG_NB];
Yann Gautier7839a052018-07-24 17:13:36 +02001864 unsigned int clksrc[CLKSRC_NB];
1865 unsigned int clkdiv[CLKDIV_NB];
1866 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1867 int plloff[_PLL_NB];
1868 int ret, len;
1869 enum stm32mp1_pll_id i;
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001870 bool pllcsg_set[_PLL_NB];
1871 bool pllcfg_valid[_PLL_NB];
Yann Gautier7839a052018-07-24 17:13:36 +02001872 bool lse_css = false;
Yann Gautier0d216802019-02-14 10:53:33 +01001873 bool pll3_preserve = false;
1874 bool pll4_preserve = false;
1875 bool pll4_bootrom = false;
Yann Gautier3e6fab42018-11-09 15:57:18 +01001876 const fdt32_t *pkcs_cell;
Andre Przywara52a616b2020-03-26 12:51:21 +00001877 void *fdt;
Patrick Delaunaybf1af152020-09-04 17:39:12 +02001878 int stgen_p = stm32mp1_clk_get_parent(STGEN_K);
1879 int usbphy_p = stm32mp1_clk_get_parent(USBPHY_K);
Andre Przywara52a616b2020-03-26 12:51:21 +00001880
1881 if (fdt_get_address(&fdt) == 0) {
Yann Gautier8f97c4f2020-09-16 16:40:34 +02001882 return -FDT_ERR_NOTFOUND;
Andre Przywara52a616b2020-03-26 12:51:21 +00001883 }
Yann Gautier7839a052018-07-24 17:13:36 +02001884
1885 /* Check status field to disable security */
1886 if (!fdt_get_rcc_secure_status()) {
Yann Gautier0d216802019-02-14 10:53:33 +01001887 mmio_write_32(rcc_base + RCC_TZCR, 0);
Yann Gautier7839a052018-07-24 17:13:36 +02001888 }
1889
Andre Przywara52a616b2020-03-26 12:51:21 +00001890 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1891 clksrc);
Yann Gautier7839a052018-07-24 17:13:36 +02001892 if (ret < 0) {
1893 return -FDT_ERR_NOTFOUND;
1894 }
1895
Andre Przywara52a616b2020-03-26 12:51:21 +00001896 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1897 clkdiv);
Yann Gautier7839a052018-07-24 17:13:36 +02001898 if (ret < 0) {
1899 return -FDT_ERR_NOTFOUND;
1900 }
1901
1902 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1903 char name[12];
1904
Antonio Nino Diaz39b6cc62018-08-16 16:46:06 +01001905 snprintf(name, sizeof(name), "st,pll@%d", i);
Yann Gautier7839a052018-07-24 17:13:36 +02001906 plloff[i] = fdt_rcc_subnode_offset(name);
1907
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001908 pllcfg_valid[i] = fdt_check_node(plloff[i]);
1909 if (!pllcfg_valid[i]) {
Yann Gautier7839a052018-07-24 17:13:36 +02001910 continue;
1911 }
1912
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01001913 ret = clk_get_pll_settings_from_dt(plloff[i], pllcfg[i],
1914 &pllfracv[i], pllcsg[i],
1915 &pllcsg_set[i]);
1916 if (ret != 0) {
1917 return ret;
Yann Gautier7839a052018-07-24 17:13:36 +02001918 }
1919 }
1920
Yann Gautier0d216802019-02-14 10:53:33 +01001921 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1922 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier7839a052018-07-24 17:13:36 +02001923
1924 /*
1925 * Switch ON oscillator found in device-tree.
1926 * Note: HSI already ON after BootROM stage.
1927 */
Yann Gautier0d216802019-02-14 10:53:33 +01001928 if (stm32mp1_osc[_LSI] != 0U) {
1929 stm32mp1_lsi_set(true);
Yann Gautier7839a052018-07-24 17:13:36 +02001930 }
Yann Gautier0d216802019-02-14 10:53:33 +01001931 if (stm32mp1_osc[_LSE] != 0U) {
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02001932 const char *name = stm32mp_osc_node_label[_LSE];
Yann Gautier0d216802019-02-14 10:53:33 +01001933 bool bypass, digbyp;
Yann Gautier7839a052018-07-24 17:13:36 +02001934 uint32_t lsedrv;
1935
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02001936 bypass = fdt_clk_read_bool(name, "st,bypass");
1937 digbyp = fdt_clk_read_bool(name, "st,digbypass");
1938 lse_css = fdt_clk_read_bool(name, "st,css");
1939 lsedrv = fdt_clk_read_uint32_default(name, "st,drive",
Yann Gautier7839a052018-07-24 17:13:36 +02001940 LSEDRV_MEDIUM_HIGH);
Yann Gautier0d216802019-02-14 10:53:33 +01001941 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier7839a052018-07-24 17:13:36 +02001942 }
Yann Gautier0d216802019-02-14 10:53:33 +01001943 if (stm32mp1_osc[_HSE] != 0U) {
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02001944 const char *name = stm32mp_osc_node_label[_HSE];
Yann Gautier0d216802019-02-14 10:53:33 +01001945 bool bypass, digbyp, css;
Yann Gautier7839a052018-07-24 17:13:36 +02001946
Gabriel Fernandezb208e3d2020-05-15 08:00:03 +02001947 bypass = fdt_clk_read_bool(name, "st,bypass");
1948 digbyp = fdt_clk_read_bool(name, "st,digbypass");
1949 css = fdt_clk_read_bool(name, "st,css");
Yann Gautier0d216802019-02-14 10:53:33 +01001950 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier7839a052018-07-24 17:13:36 +02001951 }
1952 /*
1953 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1954 * => switch on CSI even if node is not present in device tree
1955 */
Yann Gautier0d216802019-02-14 10:53:33 +01001956 stm32mp1_csi_set(true);
Yann Gautier7839a052018-07-24 17:13:36 +02001957
1958 /* Come back to HSI */
Yann Gautier0d216802019-02-14 10:53:33 +01001959 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +02001960 if (ret != 0) {
1961 return ret;
1962 }
Yann Gautier0d216802019-02-14 10:53:33 +01001963 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier7839a052018-07-24 17:13:36 +02001964 if (ret != 0) {
1965 return ret;
1966 }
Yann Gautierb053a222019-02-15 17:33:27 +01001967 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1968 if (ret != 0) {
1969 return ret;
1970 }
Yann Gautier7839a052018-07-24 17:13:36 +02001971
Yann Gautier0d216802019-02-14 10:53:33 +01001972 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1973 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
1974 pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
1975 clksrc[CLKSRC_PLL3],
1976 pllcfg[_PLL3],
1977 plloff[_PLL3]);
1978 pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
1979 clksrc[CLKSRC_PLL4],
1980 pllcfg[_PLL4],
1981 plloff[_PLL4]);
1982 }
Patrick Delaunaybf1af152020-09-04 17:39:12 +02001983 /* Don't initialize PLL4, when used by BOOTROM */
1984 if ((stm32mp_get_boot_itf_selected() ==
1985 BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) &&
1986 ((stgen_p == (int)_PLL4_R) || (usbphy_p == (int)_PLL4_R))) {
1987 pll4_bootrom = true;
1988 pll4_preserve = true;
1989 }
Yann Gautier0d216802019-02-14 10:53:33 +01001990
Yann Gautier7839a052018-07-24 17:13:36 +02001991 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautier0d216802019-02-14 10:53:33 +01001992 if (((i == _PLL3) && pll3_preserve) ||
1993 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier7839a052018-07-24 17:13:36 +02001994 continue;
Yann Gautier0d216802019-02-14 10:53:33 +01001995 }
1996
1997 ret = stm32mp1_pll_stop(i);
Yann Gautier7839a052018-07-24 17:13:36 +02001998 if (ret != 0) {
1999 return ret;
2000 }
2001 }
2002
2003 /* Configure HSIDIV */
Yann Gautier0d216802019-02-14 10:53:33 +01002004 if (stm32mp1_osc[_HSI] != 0U) {
2005 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier7839a052018-07-24 17:13:36 +02002006 if (ret != 0) {
2007 return ret;
2008 }
Yann Gautier0d216802019-02-14 10:53:33 +01002009 stm32mp1_stgen_config();
Yann Gautier7839a052018-07-24 17:13:36 +02002010 }
2011
2012 /* Select DIV */
2013 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautier0d216802019-02-14 10:53:33 +01002014 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier7839a052018-07-24 17:13:36 +02002015 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautier0d216802019-02-14 10:53:33 +01002016 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002017 if (ret != 0) {
2018 return ret;
2019 }
Yann Gautier0d216802019-02-14 10:53:33 +01002020 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002021 if (ret != 0) {
2022 return ret;
2023 }
Yann Gautier0d216802019-02-14 10:53:33 +01002024 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002025 if (ret != 0) {
2026 return ret;
2027 }
Yann Gautierb053a222019-02-15 17:33:27 +01002028 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
2029 if (ret != 0) {
2030 return ret;
2031 }
Yann Gautier0d216802019-02-14 10:53:33 +01002032 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002033 if (ret != 0) {
2034 return ret;
2035 }
Yann Gautier0d216802019-02-14 10:53:33 +01002036 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002037 if (ret != 0) {
2038 return ret;
2039 }
Yann Gautier0d216802019-02-14 10:53:33 +01002040 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier7839a052018-07-24 17:13:36 +02002041 if (ret != 0) {
2042 return ret;
2043 }
2044
2045 /* No ready bit for RTC */
Yann Gautier0d216802019-02-14 10:53:33 +01002046 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier7839a052018-07-24 17:13:36 +02002047 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
2048
2049 /* Configure PLLs source */
Yann Gautier0d216802019-02-14 10:53:33 +01002050 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier7839a052018-07-24 17:13:36 +02002051 if (ret != 0) {
2052 return ret;
2053 }
2054
Yann Gautier0d216802019-02-14 10:53:33 +01002055 if (!pll3_preserve) {
2056 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
2057 if (ret != 0) {
2058 return ret;
2059 }
2060 }
2061
2062 if (!pll4_preserve) {
2063 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
2064 if (ret != 0) {
2065 return ret;
2066 }
Yann Gautier7839a052018-07-24 17:13:36 +02002067 }
2068
2069 /* Configure and start PLLs */
2070 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautier0d216802019-02-14 10:53:33 +01002071 if (((i == _PLL3) && pll3_preserve) ||
2072 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
2073 continue;
2074 }
2075
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002076 if (!pllcfg_valid[i]) {
Yann Gautier7839a052018-07-24 17:13:36 +02002077 continue;
2078 }
2079
Yann Gautier0d216802019-02-14 10:53:33 +01002080 if ((i == _PLL4) && pll4_bootrom) {
2081 /* Set output divider if not done by the Bootrom */
2082 stm32mp1_pll_config_output(i, pllcfg[i]);
2083 continue;
2084 }
2085
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002086 ret = stm32mp1_pll_config(i, pllcfg[i], pllfracv[i]);
Yann Gautier7839a052018-07-24 17:13:36 +02002087 if (ret != 0) {
2088 return ret;
2089 }
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002090
2091 if (pllcsg_set[i]) {
2092 stm32mp1_pll_csg(i, pllcsg[i]);
Yann Gautier7839a052018-07-24 17:13:36 +02002093 }
2094
Yann Gautier0d216802019-02-14 10:53:33 +01002095 stm32mp1_pll_start(i);
Yann Gautier7839a052018-07-24 17:13:36 +02002096 }
2097 /* Wait and start PLLs ouptut when ready */
2098 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Nicolas Le Bayon964e5ff2019-11-13 11:46:31 +01002099 if (!pllcfg_valid[i]) {
Yann Gautier7839a052018-07-24 17:13:36 +02002100 continue;
2101 }
2102
Yann Gautier0d216802019-02-14 10:53:33 +01002103 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier7839a052018-07-24 17:13:36 +02002104 if (ret != 0) {
2105 return ret;
2106 }
2107 }
2108 /* Wait LSE ready before to use it */
Yann Gautier0d216802019-02-14 10:53:33 +01002109 if (stm32mp1_osc[_LSE] != 0U) {
2110 stm32mp1_lse_wait();
Yann Gautier7839a052018-07-24 17:13:36 +02002111 }
2112
2113 /* Configure with expected clock source */
Yann Gautier0d216802019-02-14 10:53:33 +01002114 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier7839a052018-07-24 17:13:36 +02002115 if (ret != 0) {
2116 return ret;
2117 }
Yann Gautier0d216802019-02-14 10:53:33 +01002118 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier7839a052018-07-24 17:13:36 +02002119 if (ret != 0) {
2120 return ret;
2121 }
Yann Gautierb053a222019-02-15 17:33:27 +01002122 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
2123 if (ret != 0) {
2124 return ret;
2125 }
Yann Gautier0d216802019-02-14 10:53:33 +01002126 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier7839a052018-07-24 17:13:36 +02002127
2128 /* Configure PKCK */
2129 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
2130 if (pkcs_cell != NULL) {
2131 bool ckper_disabled = false;
2132 uint32_t j;
Patrick Delaunaybf1af152020-09-04 17:39:12 +02002133 uint32_t usbreg_bootrom = 0U;
2134
2135 if (pll4_bootrom) {
2136 usbreg_bootrom = mmio_read_32(rcc_base + RCC_USBCKSELR);
2137 }
Yann Gautier7839a052018-07-24 17:13:36 +02002138
Yann Gautier7839a052018-07-24 17:13:36 +02002139 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautier3e6fab42018-11-09 15:57:18 +01002140 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier7839a052018-07-24 17:13:36 +02002141
2142 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
2143 ckper_disabled = true;
2144 continue;
2145 }
Yann Gautier0d216802019-02-14 10:53:33 +01002146 stm32mp1_pkcs_config(pkcs);
Yann Gautier7839a052018-07-24 17:13:36 +02002147 }
2148
2149 /*
2150 * CKPER is source for some peripheral clocks
2151 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2152 * only if previous clock is still ON
2153 * => deactivated CKPER only after switching clock
2154 */
2155 if (ckper_disabled) {
Yann Gautier0d216802019-02-14 10:53:33 +01002156 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier7839a052018-07-24 17:13:36 +02002157 }
Patrick Delaunaybf1af152020-09-04 17:39:12 +02002158
2159 if (pll4_bootrom) {
2160 uint32_t usbreg_value, usbreg_mask;
2161 const struct stm32mp1_clk_sel *sel;
2162
2163 sel = clk_sel_ref(_USBPHY_SEL);
2164 usbreg_mask = (uint32_t)sel->msk << sel->src;
2165 sel = clk_sel_ref(_USBO_SEL);
2166 usbreg_mask |= (uint32_t)sel->msk << sel->src;
2167
2168 usbreg_value = mmio_read_32(rcc_base + RCC_USBCKSELR) &
2169 usbreg_mask;
2170 usbreg_bootrom &= usbreg_mask;
2171 if (usbreg_bootrom != usbreg_value) {
2172 VERBOSE("forbidden new USB clk path\n");
2173 VERBOSE("vs bootrom on USB boot\n");
2174 return -FDT_ERR_BADVALUE;
2175 }
2176 }
Yann Gautier7839a052018-07-24 17:13:36 +02002177 }
2178
2179 /* Switch OFF HSI if not found in device-tree */
Yann Gautier0d216802019-02-14 10:53:33 +01002180 if (stm32mp1_osc[_HSI] == 0U) {
2181 stm32mp1_hsi_set(false);
Yann Gautier7839a052018-07-24 17:13:36 +02002182 }
Yann Gautier0d216802019-02-14 10:53:33 +01002183 stm32mp1_stgen_config();
Yann Gautier7839a052018-07-24 17:13:36 +02002184
2185 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautier0d216802019-02-14 10:53:33 +01002186 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier7839a052018-07-24 17:13:36 +02002187 RCC_DDRITFCR_DDRCKMOD_MASK,
2188 RCC_DDRITFCR_DDRCKMOD_SSR <<
2189 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2190
2191 return 0;
2192}
2193
2194static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier7839a052018-07-24 17:13:36 +02002195 enum stm32mp_osc_id index)
2196{
2197 uint32_t frequency;
2198
Yann Gautier0d216802019-02-14 10:53:33 +01002199 if (fdt_osc_read_freq(name, &frequency) == 0) {
2200 stm32mp1_osc[index] = frequency;
Yann Gautier7839a052018-07-24 17:13:36 +02002201 }
2202}
2203
2204static void stm32mp1_osc_init(void)
2205{
Yann Gautier7839a052018-07-24 17:13:36 +02002206 enum stm32mp_osc_id i;
2207
2208 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautier0d216802019-02-14 10:53:33 +01002209 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier7839a052018-07-24 17:13:36 +02002210 }
2211}
2212
Etienne Carriere37e82952020-05-13 11:49:49 +02002213#ifdef STM32MP_SHARED_RESOURCES
2214/*
2215 * Get the parent ID of the target parent clock, for tagging as secure
2216 * shared clock dependencies.
2217 */
2218static int get_parent_id_parent(unsigned int parent_id)
2219{
2220 enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
2221 enum stm32mp1_pll_id pll_id;
2222 uint32_t p_sel;
2223 uintptr_t rcc_base = stm32mp_rcc_base();
2224
2225 switch (parent_id) {
2226 case _ACLK:
2227 case _PCLK4:
2228 case _PCLK5:
2229 s = _AXIS_SEL;
2230 break;
2231 case _PLL1_P:
2232 case _PLL1_Q:
2233 case _PLL1_R:
2234 pll_id = _PLL1;
2235 break;
2236 case _PLL2_P:
2237 case _PLL2_Q:
2238 case _PLL2_R:
2239 pll_id = _PLL2;
2240 break;
2241 case _PLL3_P:
2242 case _PLL3_Q:
2243 case _PLL3_R:
2244 pll_id = _PLL3;
2245 break;
2246 case _PLL4_P:
2247 case _PLL4_Q:
2248 case _PLL4_R:
2249 pll_id = _PLL4;
2250 break;
2251 case _PCLK1:
2252 case _PCLK2:
2253 case _HCLK2:
2254 case _HCLK6:
2255 case _CK_PER:
2256 case _CK_MPU:
2257 case _CK_MCU:
2258 case _USB_PHY_48:
2259 /* We do not expect to access these */
2260 panic();
2261 break;
2262 default:
2263 /* Other parents have no parent */
2264 return -1;
2265 }
2266
2267 if (s != _UNKNOWN_SEL) {
2268 const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
2269
2270 p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
2271 sel->msk;
2272
2273 if (p_sel < sel->nb_parent) {
2274 return (int)sel->parent[p_sel];
2275 }
2276 } else {
2277 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
2278
2279 p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
2280 RCC_SELR_REFCLK_SRC_MASK;
2281
2282 if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
2283 return (int)pll->refclk[p_sel];
2284 }
2285 }
2286
2287 VERBOSE("No parent selected for %s\n",
2288 stm32mp1_clk_parent_name[parent_id]);
2289
2290 return -1;
2291}
2292
2293static void secure_parent_clocks(unsigned long parent_id)
2294{
2295 int grandparent_id;
2296
2297 switch (parent_id) {
2298 case _PLL3_P:
2299 case _PLL3_Q:
2300 case _PLL3_R:
2301 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2302 break;
2303
2304 /* These clocks are always secure when RCC is secure */
2305 case _ACLK:
2306 case _HCLK2:
2307 case _HCLK6:
2308 case _PCLK4:
2309 case _PCLK5:
2310 case _PLL1_P:
2311 case _PLL1_Q:
2312 case _PLL1_R:
2313 case _PLL2_P:
2314 case _PLL2_Q:
2315 case _PLL2_R:
2316 case _HSI:
2317 case _HSI_KER:
2318 case _LSI:
2319 case _CSI:
2320 case _CSI_KER:
2321 case _HSE:
2322 case _HSE_KER:
2323 case _HSE_KER_DIV2:
Gabriel Fernandezcbd2e8a2021-07-27 15:39:16 +02002324 case _HSE_RTC:
Etienne Carriere37e82952020-05-13 11:49:49 +02002325 case _LSE:
2326 break;
2327
2328 default:
2329 VERBOSE("Cannot secure parent clock %s\n",
2330 stm32mp1_clk_parent_name[parent_id]);
2331 panic();
2332 }
2333
2334 grandparent_id = get_parent_id_parent(parent_id);
2335 if (grandparent_id >= 0) {
2336 secure_parent_clocks(grandparent_id);
2337 }
2338}
2339
2340void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
2341{
2342 int parent_id;
2343
2344 if (!stm32mp1_rcc_is_secure()) {
2345 return;
2346 }
2347
2348 switch (clock_id) {
2349 case PLL1:
2350 case PLL2:
2351 /* PLL1/PLL2 are always secure: nothing to do */
2352 break;
2353 case PLL3:
2354 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2355 break;
2356 case PLL4:
2357 ERROR("PLL4 cannot be secured\n");
2358 panic();
2359 break;
2360 default:
2361 /* Others are expected gateable clock */
2362 parent_id = stm32mp1_clk_get_parent(clock_id);
2363 if (parent_id < 0) {
2364 INFO("No parent found for clock %lu\n", clock_id);
2365 } else {
2366 secure_parent_clocks(parent_id);
2367 }
2368 break;
2369 }
2370}
2371#endif /* STM32MP_SHARED_RESOURCES */
2372
Yann Gautier6cb45f82019-05-20 14:39:26 +02002373static void sync_earlyboot_clocks_state(void)
2374{
Etienne Carriere033b6c32019-12-08 08:23:35 +01002375 unsigned int idx;
2376 const unsigned long secure_enable[] = {
2377 AXIDCG,
2378 BSEC,
2379 DDRC1, DDRC1LP,
2380 DDRC2, DDRC2LP,
2381 DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
2382 DDRPHYC, DDRPHYCLP,
Lionel Debieve373f06b2019-09-02 18:15:45 +02002383 RTCAPB,
Etienne Carriere033b6c32019-12-08 08:23:35 +01002384 TZC1, TZC2,
2385 TZPC,
2386 STGEN_K,
2387 };
2388
2389 for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
2390 stm32mp_clk_enable(secure_enable[idx]);
2391 }
Yann Gautier6cb45f82019-05-20 14:39:26 +02002392}
2393
Yann Gautier33667d22021-08-30 15:06:54 +02002394static const struct clk_ops stm32mp_clk_ops = {
2395 .enable = stm32mp_clk_enable,
2396 .disable = stm32mp_clk_disable,
2397 .is_enabled = stm32mp_clk_is_enabled,
2398 .get_rate = stm32mp_clk_get_rate,
2399 .get_parent = stm32mp1_clk_get_parent,
2400};
2401
Yann Gautier7839a052018-07-24 17:13:36 +02002402int stm32mp1_clk_probe(void)
2403{
Yann Gautier7839a052018-07-24 17:13:36 +02002404 stm32mp1_osc_init();
2405
Yann Gautier6cb45f82019-05-20 14:39:26 +02002406 sync_earlyboot_clocks_state();
2407
Yann Gautier33667d22021-08-30 15:06:54 +02002408 clk_register(&stm32mp_clk_ops);
2409
Yann Gautier7839a052018-07-24 17:13:36 +02002410 return 0;
2411}