blob: 3fe21efec9e62c42b64533cbe4889c7701802b2a [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002 * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved
Yann Gautier9aea69e2018-07-24 17:13:36 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
Yann Gautier9aea69e2018-07-24 17:13:36 +02007#include <assert.h>
Yann Gautier9aea69e2018-07-24 17:13:36 +02008#include <errno.h>
Yann Gautier9aea69e2018-07-24 17:13:36 +02009#include <stdint.h>
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010010#include <stdio.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <arch.h>
13#include <arch_helpers.h>
14#include <common/debug.h>
Andre Przywaracc99f3f2020-03-26 12:51:21 +000015#include <common/fdt_wrappers.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020016#include <drivers/clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/delay_timer.h>
18#include <drivers/generic_delay_timer.h>
Yann Gautier4d429472019-02-14 11:15:20 +010019#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <drivers/st/stm32mp1_rcc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <dt-bindings/clock/stm32mp1-clksrc.h>
23#include <lib/mmio.h>
Yann Gautiere4a3c352019-02-14 10:53:33 +010024#include <lib/spinlock.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000025#include <lib/utils_def.h>
Nicolas Le Bayondab197a2019-11-13 11:46:31 +010026#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000027#include <plat/common/platform.h>
28
Nicolas Le Bayondab197a2019-11-13 11:46:31 +010029#include <platform_def.h>
30
Yann Gautier2299d572019-02-14 11:14:39 +010031#define MAX_HSI_HZ 64000000
Yann Gautiere4a3c352019-02-14 10:53:33 +010032#define USB_PHY_48_MHZ 48000000
Yann Gautier9aea69e2018-07-24 17:13:36 +020033
Yann Gautier2299d572019-02-14 11:14:39 +010034#define TIMEOUT_US_200MS U(200000)
35#define TIMEOUT_US_1S U(1000000)
Yann Gautier9aea69e2018-07-24 17:13:36 +020036
Yann Gautier2299d572019-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 Gautier9aea69e2018-07-24 17:13:36 +020042
Yann Gautier5f2e8742019-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 Gautier9aea69e2018-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 Fernandez4e3a51a2021-07-27 15:39:16 +020059 _HSE_RTC,
Yann Gautier9aea69e2018-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 Gautiered342322019-02-15 17:33:27 +010083 _CK_MCU,
Yann Gautiere4a3c352019-02-14 10:53:33 +010084 _USB_PHY_48,
Yann Gautier9aea69e2018-07-24 17:13:36 +020085 _PARENT_NB,
86 _UNKNOWN_ID = 0xff,
87};
88
Yann Gautiere4a3c352019-02-14 10:53:33 +010089/* Lists only the parent clock we are interested in */
Yann Gautier9aea69e2018-07-24 17:13:36 +020090enum stm32mp1_parent_sel {
Yann Gautiere4a3c352019-02-14 10:53:33 +010091 _I2C12_SEL,
92 _I2C35_SEL,
93 _STGEN_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +020094 _I2C46_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010095 _SPI6_SEL,
Yann Gautier9d8bbcd2019-05-07 18:49:33 +020096 _UART1_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010097 _RNG1_SEL,
Yann Gautier9aea69e2018-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 Gautier9d8bbcd2019-05-07 18:49:33 +0200106 _AXIS_SEL,
107 _MCUS_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200108 _USBPHY_SEL,
109 _USBO_SEL,
Etienne Carriere04132612019-12-08 08:20:12 +0100110 _MPU_SEL,
Yann Gautierfaa9bcf2021-08-31 18:23:13 +0200111 _CKPER_SEL,
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100112 _RTC_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200113 _PARENT_SEL_NB,
114 _UNKNOWN_SEL = 0xff,
115};
116
Etienne Carriere04132612019-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 Fernandez4e3a51a2021-07-27 15:39:16 +0200129 [_HSE_RTC] = _UNKNOWN_ID,
Etienne Carriere04132612019-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 Gautier9aea69e2018-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 Gautiered342322019-02-15 17:33:27 +0100185 CLKSRC_MCU,
Yann Gautier9aea69e2018-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 Gautiered342322019-02-15 17:33:27 +0100198 CLKDIV_MCU,
Yann Gautier9aea69e2018-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 Gautiere4a3c352019-02-14 10:53:33 +0100244 uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
245 uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
Yann Gautier9aea69e2018-07-24 17:13:36 +0200246};
247
248struct stm32mp1_clk_sel {
249 uint16_t offset;
250 uint8_t src;
251 uint8_t msk;
252 uint8_t nb_parent;
253 const uint8_t *parent;
254};
255
256#define REFCLK_SIZE 4
257struct stm32mp1_clk_pll {
258 enum stm32mp1_plltype plltype;
259 uint16_t rckxselr;
260 uint16_t pllxcfgr1;
261 uint16_t pllxcfgr2;
262 uint16_t pllxfracr;
263 uint16_t pllxcr;
264 uint16_t pllxcsgr;
265 enum stm32mp_osc_id refclk[REFCLK_SIZE];
266};
267
Yann Gautiere4a3c352019-02-14 10:53:33 +0100268/* Clocks with selectable source and non set/clr register access */
269#define _CLK_SELEC(off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200270 { \
271 .offset = (off), \
272 .bit = (b), \
273 .index = (idx), \
274 .set_clr = 0, \
275 .sel = (s), \
276 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200277 }
278
Yann Gautiere4a3c352019-02-14 10:53:33 +0100279/* Clocks with fixed source and non set/clr register access */
280#define _CLK_FIXED(off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200281 { \
282 .offset = (off), \
283 .bit = (b), \
284 .index = (idx), \
285 .set_clr = 0, \
286 .sel = _UNKNOWN_SEL, \
287 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200288 }
289
Yann Gautiere4a3c352019-02-14 10:53:33 +0100290/* Clocks with selectable source and set/clr register access */
291#define _CLK_SC_SELEC(off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200292 { \
293 .offset = (off), \
294 .bit = (b), \
295 .index = (idx), \
296 .set_clr = 1, \
297 .sel = (s), \
298 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200299 }
300
Yann Gautiere4a3c352019-02-14 10:53:33 +0100301/* Clocks with fixed source and set/clr register access */
302#define _CLK_SC_FIXED(off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200303 { \
304 .offset = (off), \
305 .bit = (b), \
306 .index = (idx), \
307 .set_clr = 1, \
308 .sel = _UNKNOWN_SEL, \
309 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200310 }
311
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200312#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \
313 [_ ## _label ## _SEL] = { \
314 .offset = _rcc_selr, \
315 .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \
Etienne Carrierec164ce22019-12-08 08:20:40 +0100316 .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
317 (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200318 .parent = (_parents), \
319 .nb_parent = ARRAY_SIZE(_parents) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200320 }
321
Yann Gautiere4a3c352019-02-14 10:53:33 +0100322#define _CLK_PLL(idx, type, off1, off2, off3, \
323 off4, off5, off6, \
324 p1, p2, p3, p4) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200325 [(idx)] = { \
326 .plltype = (type), \
327 .rckxselr = (off1), \
328 .pllxcfgr1 = (off2), \
329 .pllxcfgr2 = (off3), \
330 .pllxfracr = (off4), \
331 .pllxcr = (off5), \
332 .pllxcsgr = (off6), \
333 .refclk[0] = (p1), \
334 .refclk[1] = (p2), \
335 .refclk[2] = (p3), \
336 .refclk[3] = (p4), \
337 }
338
Yann Gautiere4a3c352019-02-14 10:53:33 +0100339#define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
340
Yann Gautier9aea69e2018-07-24 17:13:36 +0200341static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100342 _CLK_FIXED(RCC_DDRITFCR, 0, DDRC1, _ACLK),
343 _CLK_FIXED(RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
344 _CLK_FIXED(RCC_DDRITFCR, 2, DDRC2, _ACLK),
345 _CLK_FIXED(RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
346 _CLK_FIXED(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
347 _CLK_FIXED(RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
348 _CLK_FIXED(RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
349 _CLK_FIXED(RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
350 _CLK_FIXED(RCC_DDRITFCR, 8, AXIDCG, _ACLK),
351 _CLK_FIXED(RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
352 _CLK_FIXED(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),
353
354 _CLK_SC_FIXED(RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
355 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
356 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
357 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
358 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
359 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
360 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
361 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
362 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
363 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
364 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
365
366 _CLK_SC_FIXED(RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
367 _CLK_SC_SELEC(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
368
Yann Gautier3edc7c32019-05-20 19:17:08 +0200369 _CLK_SC_FIXED(RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),
370
Yann Gautiere4a3c352019-02-14 10:53:33 +0100371 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
372 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
373 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
374
375 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
376 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
377 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200378 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
Yann Gautiere4a3c352019-02-14 10:53:33 +0100379 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
380 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
381 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
382 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
383 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
384 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
385 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
386
387 _CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
388 _CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
389
390 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
391 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
392 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
393 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
394 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
395 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
396 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
397 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
398 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
399 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
400 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
401
402 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
403 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
404 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
405 _CLK_SC_SELEC(RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
406 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),
407
408 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
409 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
410 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
411 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
412 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
413
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100414 _CLK_SELEC(RCC_BDCR, 20, RTC, _RTC_SEL),
Yann Gautiere4a3c352019-02-14 10:53:33 +0100415 _CLK_SELEC(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
416};
417
418static const uint8_t i2c12_parents[] = {
419 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
420};
421
422static const uint8_t i2c35_parents[] = {
423 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
424};
425
426static const uint8_t stgen_parents[] = {
427 _HSI_KER, _HSE_KER
428};
429
430static const uint8_t i2c46_parents[] = {
431 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
432};
433
434static const uint8_t spi6_parents[] = {
435 _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
436};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200437
Yann Gautiere4a3c352019-02-14 10:53:33 +0100438static const uint8_t usart1_parents[] = {
439 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
440};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200441
Yann Gautiere4a3c352019-02-14 10:53:33 +0100442static const uint8_t rng1_parents[] = {
443 _CSI, _PLL4_R, _LSE, _LSI
444};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200445
Yann Gautiere4a3c352019-02-14 10:53:33 +0100446static const uint8_t uart6_parents[] = {
447 _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
448};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200449
Yann Gautiere4a3c352019-02-14 10:53:33 +0100450static const uint8_t uart234578_parents[] = {
451 _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
452};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200453
Yann Gautiere4a3c352019-02-14 10:53:33 +0100454static const uint8_t sdmmc12_parents[] = {
455 _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
456};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200457
Yann Gautiere4a3c352019-02-14 10:53:33 +0100458static const uint8_t sdmmc3_parents[] = {
459 _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
460};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200461
Yann Gautiere4a3c352019-02-14 10:53:33 +0100462static const uint8_t qspi_parents[] = {
463 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
464};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200465
Yann Gautiere4a3c352019-02-14 10:53:33 +0100466static const uint8_t fmc_parents[] = {
467 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
468};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200469
Etienne Carriere40c28e82019-12-19 10:03:23 +0100470static const uint8_t axiss_parents[] = {
471 _HSI, _HSE, _PLL2_P
Yann Gautier9aea69e2018-07-24 17:13:36 +0200472};
473
Etienne Carriere40c28e82019-12-19 10:03:23 +0100474static const uint8_t mcuss_parents[] = {
475 _HSI, _HSE, _CSI, _PLL3_P
Yann Gautiered342322019-02-15 17:33:27 +0100476};
477
Yann Gautiere4a3c352019-02-14 10:53:33 +0100478static const uint8_t usbphy_parents[] = {
479 _HSE_KER, _PLL4_R, _HSE_KER_DIV2
480};
481
482static const uint8_t usbo_parents[] = {
483 _PLL4_R, _USB_PHY_48
484};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200485
Etienne Carriere04132612019-12-08 08:20:12 +0100486static const uint8_t mpu_parents[] = {
487 _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */
488};
489
490static const uint8_t per_parents[] = {
491 _HSI, _HSE, _CSI,
492};
493
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100494static const uint8_t rtc_parents[] = {
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +0200495 _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100496};
497
Yann Gautier9aea69e2018-07-24 17:13:36 +0200498static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200499 _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
500 _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
501 _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
502 _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
503 _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
504 _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
505 _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
Etienne Carriere04132612019-12-08 08:20:12 +0100506 _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
Yann Gautierfaa9bcf2021-08-31 18:23:13 +0200507 _CLK_PARENT_SEL(CKPER, RCC_CPERCKSELR, per_parents),
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100508 _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200509 _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
510 _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
511 _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
512 _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
513 _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
514 _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
515 _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
516 _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
Etienne Carriere40c28e82019-12-19 10:03:23 +0100517 _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, axiss_parents),
518 _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mcuss_parents),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200519 _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
520 _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200521};
522
523/* Define characteristic of PLL according type */
524#define DIVN_MIN 24
525static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
526 [PLL_800] = {
527 .refclk_min = 4,
528 .refclk_max = 16,
529 .divn_max = 99,
530 },
531 [PLL_1600] = {
532 .refclk_min = 8,
533 .refclk_max = 16,
534 .divn_max = 199,
535 },
536};
537
538/* PLLNCFGR2 register divider by output */
539static const uint8_t pllncfgr2[_DIV_NB] = {
540 [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
541 [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautiere4a3c352019-02-14 10:53:33 +0100542 [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200543};
544
545static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100546 _CLK_PLL(_PLL1, PLL_1600,
547 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
548 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
549 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
550 _CLK_PLL(_PLL2, PLL_1600,
551 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
552 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
553 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
554 _CLK_PLL(_PLL3, PLL_800,
555 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
556 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
557 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
558 _CLK_PLL(_PLL4, PLL_800,
559 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
560 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
561 _HSI, _HSE, _CSI, _I2S_CKIN),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200562};
563
564/* Prescaler table lookups for clock computation */
Yann Gautiered342322019-02-15 17:33:27 +0100565/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
566static const uint8_t stm32mp1_mcu_div[16] = {
567 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
568};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200569
570/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
571#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
572#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
573static const uint8_t stm32mp1_mpu_apbx_div[8] = {
574 0, 1, 2, 3, 4, 4, 4, 4
575};
576
577/* div = /1 /2 /3 /4 */
578static const uint8_t stm32mp1_axi_div[8] = {
579 1, 2, 3, 4, 4, 4, 4, 4
580};
581
Etienne Carriere1368ada2020-05-13 11:49:49 +0200582static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
583 [_HSI] = "HSI",
584 [_HSE] = "HSE",
585 [_CSI] = "CSI",
586 [_LSI] = "LSI",
587 [_LSE] = "LSE",
588 [_I2S_CKIN] = "I2S_CKIN",
589 [_HSI_KER] = "HSI_KER",
590 [_HSE_KER] = "HSE_KER",
591 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +0200592 [_HSE_RTC] = "HSE_RTC",
Etienne Carriere1368ada2020-05-13 11:49:49 +0200593 [_CSI_KER] = "CSI_KER",
594 [_PLL1_P] = "PLL1_P",
595 [_PLL1_Q] = "PLL1_Q",
596 [_PLL1_R] = "PLL1_R",
597 [_PLL2_P] = "PLL2_P",
598 [_PLL2_Q] = "PLL2_Q",
599 [_PLL2_R] = "PLL2_R",
600 [_PLL3_P] = "PLL3_P",
601 [_PLL3_Q] = "PLL3_Q",
602 [_PLL3_R] = "PLL3_R",
603 [_PLL4_P] = "PLL4_P",
604 [_PLL4_Q] = "PLL4_Q",
605 [_PLL4_R] = "PLL4_R",
606 [_ACLK] = "ACLK",
607 [_PCLK1] = "PCLK1",
608 [_PCLK2] = "PCLK2",
609 [_PCLK3] = "PCLK3",
610 [_PCLK4] = "PCLK4",
611 [_PCLK5] = "PCLK5",
612 [_HCLK6] = "KCLK6",
613 [_HCLK2] = "HCLK2",
614 [_CK_PER] = "CK_PER",
615 [_CK_MPU] = "CK_MPU",
616 [_CK_MCU] = "CK_MCU",
617 [_USB_PHY_48] = "USB_PHY_48",
618};
619
Yann Gautiere4a3c352019-02-14 10:53:33 +0100620/* RCC clock device driver private */
621static unsigned long stm32mp1_osc[NB_OSC];
622static struct spinlock reg_lock;
623static unsigned int gate_refcounts[NB_GATES];
624static struct spinlock refcount_lock;
625
626static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
627{
628 return &stm32mp1_clk_gate[idx];
629}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200630
Yann Gautiere4a3c352019-02-14 10:53:33 +0100631static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
632{
633 return &stm32mp1_clk_sel[idx];
634}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200635
Yann Gautiere4a3c352019-02-14 10:53:33 +0100636static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
637{
638 return &stm32mp1_clk_pll[idx];
639}
640
Yann Gautiere4a3c352019-02-14 10:53:33 +0100641static void stm32mp1_clk_lock(struct spinlock *lock)
642{
Yann Gautierf540a592019-05-22 19:13:51 +0200643 if (stm32mp_lock_available()) {
644 /* Assume interrupts are masked */
645 spin_lock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100646 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100647}
648
649static void stm32mp1_clk_unlock(struct spinlock *lock)
650{
Yann Gautierf540a592019-05-22 19:13:51 +0200651 if (stm32mp_lock_available()) {
652 spin_unlock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100653 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100654}
655
656bool stm32mp1_rcc_is_secure(void)
657{
658 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100659 uint32_t mask = RCC_TZCR_TZEN;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100660
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100661 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100662}
663
Yann Gautiered342322019-02-15 17:33:27 +0100664bool stm32mp1_rcc_is_mckprot(void)
665{
666 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100667 uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT;
Yann Gautiered342322019-02-15 17:33:27 +0100668
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100669 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautiered342322019-02-15 17:33:27 +0100670}
671
Yann Gautiere4a3c352019-02-14 10:53:33 +0100672void stm32mp1_clk_rcc_regs_lock(void)
673{
674 stm32mp1_clk_lock(&reg_lock);
675}
676
677void stm32mp1_clk_rcc_regs_unlock(void)
678{
679 stm32mp1_clk_unlock(&reg_lock);
680}
681
682static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200683{
684 if (idx >= NB_OSC) {
685 return 0;
686 }
687
Yann Gautiere4a3c352019-02-14 10:53:33 +0100688 return stm32mp1_osc[idx];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200689}
690
Yann Gautiere4a3c352019-02-14 10:53:33 +0100691static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200692{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100693 unsigned int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200694
Yann Gautiere4a3c352019-02-14 10:53:33 +0100695 for (i = 0U; i < NB_GATES; i++) {
696 if (gate_ref(i)->index == id) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200697 return i;
698 }
699 }
700
Yann Gautierc9343812021-09-07 09:05:44 +0200701 ERROR("%s: clk id %lu not found\n", __func__, id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200702
703 return -EINVAL;
704}
705
Yann Gautiere4a3c352019-02-14 10:53:33 +0100706static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200707{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100708 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200709}
710
Yann Gautiere4a3c352019-02-14 10:53:33 +0100711static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200712{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100713 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200714}
715
Yann Gautiere4a3c352019-02-14 10:53:33 +0100716static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200717{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100718 const struct stm32mp1_clk_sel *sel;
Etienne Carriere04132612019-12-08 08:20:12 +0100719 uint32_t p_sel;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200720 int i;
721 enum stm32mp1_parent_id p;
722 enum stm32mp1_parent_sel s;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100723 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200724
Etienne Carriere04132612019-12-08 08:20:12 +0100725 /* Few non gateable clock have a static parent ID, find them */
726 i = (int)clock_id2parent_id(id);
727 if (i != _UNKNOWN_ID) {
728 return i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200729 }
730
Yann Gautiere4a3c352019-02-14 10:53:33 +0100731 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200732 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100733 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200734 }
735
Yann Gautiere4a3c352019-02-14 10:53:33 +0100736 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200737 if (p < _PARENT_NB) {
738 return (int)p;
739 }
740
Yann Gautiere4a3c352019-02-14 10:53:33 +0100741 s = stm32mp1_clk_get_sel(i);
742 if (s == _UNKNOWN_SEL) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200743 return -EINVAL;
744 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100745 if (s >= _PARENT_SEL_NB) {
746 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200747 }
748
Yann Gautiere4a3c352019-02-14 10:53:33 +0100749 sel = clk_sel_ref(s);
Etienne Carrierec164ce22019-12-08 08:20:40 +0100750 p_sel = (mmio_read_32(rcc_base + sel->offset) &
751 (sel->msk << sel->src)) >> sel->src;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100752 if (p_sel < sel->nb_parent) {
753 return (int)sel->parent[p_sel];
754 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200755
756 return -EINVAL;
757}
758
Yann Gautiere4a3c352019-02-14 10:53:33 +0100759static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200760{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100761 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
762 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200763
Yann Gautiere4a3c352019-02-14 10:53:33 +0100764 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200765}
766
767/*
768 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
769 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
770 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
771 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
772 */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100773static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200774{
Yann Gautier9aea69e2018-07-24 17:13:36 +0200775 unsigned long refclk, fvco;
776 uint32_t cfgr1, fracr, divm, divn;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100777 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200778
Yann Gautiere4a3c352019-02-14 10:53:33 +0100779 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
780 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200781
782 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
783 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
784
Yann Gautiere4a3c352019-02-14 10:53:33 +0100785 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200786
787 /*
788 * With FRACV :
789 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
790 * Without FRACV
791 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
792 */
793 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100794 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
795 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200796 unsigned long long numerator, denominator;
797
Yann Gautiere4a3c352019-02-14 10:53:33 +0100798 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
799 numerator = refclk * numerator;
800 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200801 fvco = (unsigned long)(numerator / denominator);
802 } else {
803 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
804 }
805
806 return fvco;
807}
808
Yann Gautiere4a3c352019-02-14 10:53:33 +0100809static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200810 enum stm32mp1_div_id div_id)
811{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100812 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200813 unsigned long dfout;
814 uint32_t cfgr2, divy;
815
816 if (div_id >= _DIV_NB) {
817 return 0;
818 }
819
Yann Gautiere4a3c352019-02-14 10:53:33 +0100820 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200821 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
822
Yann Gautiere4a3c352019-02-14 10:53:33 +0100823 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200824
825 return dfout;
826}
827
Yann Gautiere4a3c352019-02-14 10:53:33 +0100828static unsigned long get_clock_rate(int p)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200829{
830 uint32_t reg, clkdiv;
831 unsigned long clock = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100832 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200833
834 switch (p) {
835 case _CK_MPU:
836 /* MPU sub system */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100837 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200838 switch (reg & RCC_SELR_SRC_MASK) {
839 case RCC_MPCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100840 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200841 break;
842 case RCC_MPCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100843 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200844 break;
845 case RCC_MPCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100846 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200847 break;
848 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100849 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200850
Yann Gautiere4a3c352019-02-14 10:53:33 +0100851 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200852 clkdiv = reg & RCC_MPUDIV_MASK;
Gabriel Fernandez4d198742020-02-28 09:09:06 +0100853 clock >>= stm32mp1_mpu_div[clkdiv];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200854 break;
855 default:
856 break;
857 }
858 break;
859 /* AXI sub system */
860 case _ACLK:
861 case _HCLK2:
862 case _HCLK6:
863 case _PCLK4:
864 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100865 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200866 switch (reg & RCC_SELR_SRC_MASK) {
867 case RCC_ASSCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100868 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200869 break;
870 case RCC_ASSCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100871 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200872 break;
873 case RCC_ASSCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100874 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200875 break;
876 default:
877 break;
878 }
879
880 /* System clock divider */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100881 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200882 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
883
884 switch (p) {
885 case _PCLK4:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100886 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200887 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
888 break;
889 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100890 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200891 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
892 break;
893 default:
894 break;
895 }
896 break;
Yann Gautiered342322019-02-15 17:33:27 +0100897 /* MCU sub system */
898 case _CK_MCU:
899 case _PCLK1:
900 case _PCLK2:
901 case _PCLK3:
902 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
903 switch (reg & RCC_SELR_SRC_MASK) {
904 case RCC_MSSCKSELR_HSI:
905 clock = stm32mp1_clk_get_fixed(_HSI);
906 break;
907 case RCC_MSSCKSELR_HSE:
908 clock = stm32mp1_clk_get_fixed(_HSE);
909 break;
910 case RCC_MSSCKSELR_CSI:
911 clock = stm32mp1_clk_get_fixed(_CSI);
912 break;
913 case RCC_MSSCKSELR_PLL:
914 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
915 break;
916 default:
917 break;
918 }
919
920 /* MCU clock divider */
921 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
922 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
923
924 switch (p) {
925 case _PCLK1:
926 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
927 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
928 break;
929 case _PCLK2:
930 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
931 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
932 break;
933 case _PCLK3:
934 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
935 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
936 break;
937 case _CK_MCU:
938 default:
939 break;
940 }
941 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200942 case _CK_PER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100943 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200944 switch (reg & RCC_SELR_SRC_MASK) {
945 case RCC_CPERCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100946 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200947 break;
948 case RCC_CPERCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100949 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200950 break;
951 case RCC_CPERCKSELR_CSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100952 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200953 break;
954 default:
955 break;
956 }
957 break;
958 case _HSI:
959 case _HSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100960 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200961 break;
962 case _CSI:
963 case _CSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100964 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200965 break;
966 case _HSE:
967 case _HSE_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100968 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200969 break;
970 case _HSE_KER_DIV2:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100971 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200972 break;
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +0200973 case _HSE_RTC:
974 clock = stm32mp1_clk_get_fixed(_HSE);
975 clock /= (mmio_read_32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U;
976 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200977 case _LSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100978 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200979 break;
980 case _LSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100981 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200982 break;
983 /* PLL */
984 case _PLL1_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100985 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200986 break;
987 case _PLL1_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100988 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200989 break;
990 case _PLL1_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100991 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200992 break;
993 case _PLL2_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100994 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200995 break;
996 case _PLL2_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100997 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200998 break;
999 case _PLL2_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001000 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001001 break;
1002 case _PLL3_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001003 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001004 break;
1005 case _PLL3_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001006 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001007 break;
1008 case _PLL3_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001009 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001010 break;
1011 case _PLL4_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001012 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001013 break;
1014 case _PLL4_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001015 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001016 break;
1017 case _PLL4_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001018 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001019 break;
1020 /* Other */
1021 case _USB_PHY_48:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001022 clock = USB_PHY_48_MHZ;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001023 break;
1024 default:
1025 break;
1026 }
1027
1028 return clock;
1029}
1030
Yann Gautiere4a3c352019-02-14 10:53:33 +01001031static void __clk_enable(struct stm32mp1_clk_gate const *gate)
1032{
1033 uintptr_t rcc_base = stm32mp_rcc_base();
1034
Etienne Carriere8a668892019-12-08 08:21:08 +01001035 VERBOSE("Enable clock %u\n", gate->index);
1036
Yann Gautiere4a3c352019-02-14 10:53:33 +01001037 if (gate->set_clr != 0U) {
1038 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
1039 } else {
1040 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
1041 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001042}
1043
1044static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1045{
1046 uintptr_t rcc_base = stm32mp_rcc_base();
1047
Etienne Carriere8a668892019-12-08 08:21:08 +01001048 VERBOSE("Disable clock %u\n", gate->index);
1049
Yann Gautiere4a3c352019-02-14 10:53:33 +01001050 if (gate->set_clr != 0U) {
1051 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1052 BIT(gate->bit));
1053 } else {
1054 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1055 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001056}
1057
1058static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1059{
1060 uintptr_t rcc_base = stm32mp_rcc_base();
1061
1062 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1063}
1064
Etienne Carriere481aa002019-12-08 08:21:44 +01001065/* Oscillators and PLLs are not gated at runtime */
1066static bool clock_is_always_on(unsigned long id)
1067{
1068 switch (id) {
1069 case CK_HSE:
1070 case CK_CSI:
1071 case CK_LSI:
1072 case CK_LSE:
1073 case CK_HSI:
1074 case CK_HSE_DIV2:
1075 case PLL1_Q:
1076 case PLL1_R:
1077 case PLL2_P:
1078 case PLL2_Q:
1079 case PLL2_R:
1080 case PLL3_P:
1081 case PLL3_Q:
1082 case PLL3_R:
Yann Gautierb39a1522020-09-16 16:41:55 +02001083 case CK_AXI:
1084 case CK_MPU:
1085 case CK_MCU:
HE Shushanc47c8162021-07-12 23:04:10 +02001086 case RTC:
Etienne Carriere481aa002019-12-08 08:21:44 +01001087 return true;
1088 default:
1089 return false;
1090 }
1091}
1092
Yann Gautierad730b52022-01-19 13:57:49 +01001093static void __stm32mp1_clk_enable(unsigned long id, bool with_refcnt)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001094{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001095 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001096 int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001097
Etienne Carriere481aa002019-12-08 08:21:44 +01001098 if (clock_is_always_on(id)) {
1099 return;
1100 }
1101
1102 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001103 if (i < 0) {
Yann Gautierc9343812021-09-07 09:05:44 +02001104 ERROR("Clock %lu can't be enabled\n", id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001105 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001106 }
1107
Yann Gautiere4a3c352019-02-14 10:53:33 +01001108 gate = gate_ref(i);
Yann Gautierad730b52022-01-19 13:57:49 +01001109
1110 if (!with_refcnt) {
1111 __clk_enable(gate);
1112 return;
1113 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001114
1115 stm32mp1_clk_lock(&refcount_lock);
1116
Yann Gautierad730b52022-01-19 13:57:49 +01001117 if (gate_refcounts[i] == 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001118 __clk_enable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001119 }
1120
Yann Gautierad730b52022-01-19 13:57:49 +01001121 gate_refcounts[i]++;
1122 if (gate_refcounts[i] == UINT_MAX) {
1123 ERROR("Clock %lu refcount reached max value\n", id);
1124 panic();
1125 }
1126
Yann Gautiere4a3c352019-02-14 10:53:33 +01001127 stm32mp1_clk_unlock(&refcount_lock);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001128}
1129
Yann Gautierad730b52022-01-19 13:57:49 +01001130static void __stm32mp1_clk_disable(unsigned long id, bool with_refcnt)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001131{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001132 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001133 int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001134
Etienne Carriere481aa002019-12-08 08:21:44 +01001135 if (clock_is_always_on(id)) {
1136 return;
1137 }
1138
1139 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001140 if (i < 0) {
Yann Gautierc9343812021-09-07 09:05:44 +02001141 ERROR("Clock %lu can't be disabled\n", id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001142 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001143 }
1144
Yann Gautiere4a3c352019-02-14 10:53:33 +01001145 gate = gate_ref(i);
Yann Gautierad730b52022-01-19 13:57:49 +01001146
1147 if (!with_refcnt) {
1148 __clk_disable(gate);
1149 return;
1150 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001151
1152 stm32mp1_clk_lock(&refcount_lock);
1153
Yann Gautierad730b52022-01-19 13:57:49 +01001154 if (gate_refcounts[i] == 0U) {
1155 ERROR("Clock %lu refcount reached 0\n", id);
1156 panic();
1157 }
1158 gate_refcounts[i]--;
1159
1160 if (gate_refcounts[i] == 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001161 __clk_disable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001162 }
1163
Yann Gautiere4a3c352019-02-14 10:53:33 +01001164 stm32mp1_clk_unlock(&refcount_lock);
1165}
1166
Yann Gautiera205a5c2021-08-30 15:06:54 +02001167static int stm32mp_clk_enable(unsigned long id)
Yann Gautiere4a3c352019-02-14 10:53:33 +01001168{
1169 __stm32mp1_clk_enable(id, true);
Yann Gautiera205a5c2021-08-30 15:06:54 +02001170
1171 return 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001172}
1173
Yann Gautiera205a5c2021-08-30 15:06:54 +02001174static void stm32mp_clk_disable(unsigned long id)
Yann Gautiere4a3c352019-02-14 10:53:33 +01001175{
1176 __stm32mp1_clk_disable(id, true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001177}
1178
Yann Gautiera205a5c2021-08-30 15:06:54 +02001179static bool stm32mp_clk_is_enabled(unsigned long id)
Yann Gautiere4a3c352019-02-14 10:53:33 +01001180{
Etienne Carriere481aa002019-12-08 08:21:44 +01001181 int i;
1182
1183 if (clock_is_always_on(id)) {
1184 return true;
1185 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001186
Etienne Carriere481aa002019-12-08 08:21:44 +01001187 i = stm32mp1_clk_get_gated_id(id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001188 if (i < 0) {
1189 panic();
1190 }
1191
1192 return __clk_is_enabled(gate_ref(i));
1193}
1194
Yann Gautiera205a5c2021-08-30 15:06:54 +02001195static unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001196{
Yann Gautiera205a5c2021-08-30 15:06:54 +02001197 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautiere4a3c352019-02-14 10:53:33 +01001198 int p = stm32mp1_clk_get_parent(id);
Yann Gautiera205a5c2021-08-30 15:06:54 +02001199 uint32_t prescaler, timpre;
1200 unsigned long parent_rate;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001201
1202 if (p < 0) {
1203 return 0;
1204 }
1205
Yann Gautiera205a5c2021-08-30 15:06:54 +02001206 parent_rate = get_clock_rate(p);
1207
1208 switch (id) {
1209 case TIM2_K:
1210 case TIM3_K:
1211 case TIM4_K:
1212 case TIM5_K:
1213 case TIM6_K:
1214 case TIM7_K:
1215 case TIM12_K:
1216 case TIM13_K:
1217 case TIM14_K:
1218 prescaler = mmio_read_32(rcc_base + RCC_APB1DIVR) &
1219 RCC_APBXDIV_MASK;
1220 timpre = mmio_read_32(rcc_base + RCC_TIMG1PRER) &
1221 RCC_TIMGXPRER_TIMGXPRE;
1222 break;
1223
1224 case TIM1_K:
1225 case TIM8_K:
1226 case TIM15_K:
1227 case TIM16_K:
1228 case TIM17_K:
1229 prescaler = mmio_read_32(rcc_base + RCC_APB2DIVR) &
1230 RCC_APBXDIV_MASK;
1231 timpre = mmio_read_32(rcc_base + RCC_TIMG2PRER) &
1232 RCC_TIMGXPRER_TIMGXPRE;
1233 break;
1234
1235 default:
1236 return parent_rate;
1237 }
1238
1239 if (prescaler == 0U) {
1240 return parent_rate;
1241 }
1242
1243 return parent_rate * (timpre + 1U) * 2U;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001244}
1245
Yann Gautiere4a3c352019-02-14 10:53:33 +01001246static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001247{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001248 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001249
Yann Gautiere4a3c352019-02-14 10:53:33 +01001250 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001251 mmio_setbits_32(address, mask_on);
1252 } else {
1253 mmio_clrbits_32(address, mask_on);
1254 }
1255}
1256
Yann Gautiere4a3c352019-02-14 10:53:33 +01001257static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001258{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001259 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1260 uintptr_t address = stm32mp_rcc_base() + offset;
1261
1262 mmio_write_32(address, mask_on);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001263}
1264
Yann Gautiere4a3c352019-02-14 10:53:33 +01001265static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001266{
Yann Gautier2299d572019-02-14 11:14:39 +01001267 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001268 uint32_t mask_test;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001269 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001270
Yann Gautiere4a3c352019-02-14 10:53:33 +01001271 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001272 mask_test = mask_rdy;
1273 } else {
1274 mask_test = 0;
1275 }
1276
Yann Gautier2299d572019-02-14 11:14:39 +01001277 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001278 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautier2299d572019-02-14 11:14:39 +01001279 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001280 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001281 mask_rdy, address, enable, mmio_read_32(address));
1282 return -ETIMEDOUT;
1283 }
1284 }
1285
1286 return 0;
1287}
1288
Yann Gautiere4a3c352019-02-14 10:53:33 +01001289static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001290{
1291 uint32_t value;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001292 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001293
Yann Gautiere4a3c352019-02-14 10:53:33 +01001294 if (digbyp) {
1295 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001296 }
1297
Yann Gautiere4a3c352019-02-14 10:53:33 +01001298 if (bypass || digbyp) {
1299 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1300 }
1301
Yann Gautier9aea69e2018-07-24 17:13:36 +02001302 /*
1303 * Warning: not recommended to switch directly from "high drive"
1304 * to "medium low drive", and vice-versa.
1305 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001306 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier9aea69e2018-07-24 17:13:36 +02001307 RCC_BDCR_LSEDRV_SHIFT;
1308
1309 while (value != lsedrv) {
1310 if (value > lsedrv) {
1311 value--;
1312 } else {
1313 value++;
1314 }
1315
Yann Gautiere4a3c352019-02-14 10:53:33 +01001316 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001317 RCC_BDCR_LSEDRV_MASK,
1318 value << RCC_BDCR_LSEDRV_SHIFT);
1319 }
1320
Yann Gautiere4a3c352019-02-14 10:53:33 +01001321 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001322}
1323
Yann Gautiere4a3c352019-02-14 10:53:33 +01001324static void stm32mp1_lse_wait(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001325{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001326 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001327 VERBOSE("%s: failed\n", __func__);
1328 }
1329}
1330
Yann Gautiere4a3c352019-02-14 10:53:33 +01001331static void stm32mp1_lsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001332{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001333 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1334
1335 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001336 VERBOSE("%s: failed\n", __func__);
1337 }
1338}
1339
Yann Gautiere4a3c352019-02-14 10:53:33 +01001340static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001341{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001342 uintptr_t rcc_base = stm32mp_rcc_base();
1343
1344 if (digbyp) {
1345 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001346 }
1347
Yann Gautiere4a3c352019-02-14 10:53:33 +01001348 if (bypass || digbyp) {
1349 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1350 }
1351
1352 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1353 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001354 VERBOSE("%s: failed\n", __func__);
1355 }
1356
1357 if (css) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001358 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001359 }
Lionel Debieved78bd852019-07-02 18:03:34 +02001360
1361#if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
1362 if ((mmio_read_32(rcc_base + RCC_OCENSETR) & RCC_OCENR_HSEBYP) &&
1363 (!(digbyp || bypass))) {
1364 panic();
1365 }
1366#endif
Yann Gautier9aea69e2018-07-24 17:13:36 +02001367}
1368
Yann Gautiere4a3c352019-02-14 10:53:33 +01001369static void stm32mp1_csi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001370{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001371 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1372 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001373 VERBOSE("%s: failed\n", __func__);
1374 }
1375}
1376
Yann Gautiere4a3c352019-02-14 10:53:33 +01001377static void stm32mp1_hsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001378{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001379 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1380 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001381 VERBOSE("%s: failed\n", __func__);
1382 }
1383}
1384
Yann Gautiere4a3c352019-02-14 10:53:33 +01001385static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001386{
Yann Gautier2299d572019-02-14 11:14:39 +01001387 uint64_t timeout;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001388 uintptr_t rcc_base = stm32mp_rcc_base();
1389 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001390
Yann Gautiere4a3c352019-02-14 10:53:33 +01001391 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001392 RCC_HSICFGR_HSIDIV_MASK,
1393 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1394
Yann Gautier2299d572019-02-14 11:14:39 +01001395 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001396 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001397 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001398 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001399 address, mmio_read_32(address));
1400 return -ETIMEDOUT;
1401 }
1402 }
1403
1404 return 0;
1405}
1406
Yann Gautiere4a3c352019-02-14 10:53:33 +01001407static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001408{
1409 uint8_t hsidiv;
1410 uint32_t hsidivfreq = MAX_HSI_HZ;
1411
1412 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1413 if (hsidivfreq == hsifreq) {
1414 break;
1415 }
1416
1417 hsidivfreq /= 2U;
1418 }
1419
1420 if (hsidiv == 4U) {
1421 ERROR("Invalid clk-hsi frequency\n");
1422 return -1;
1423 }
1424
1425 if (hsidiv != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001426 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001427 }
1428
1429 return 0;
1430}
1431
Yann Gautiere4a3c352019-02-14 10:53:33 +01001432static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1433 unsigned int clksrc,
1434 uint32_t *pllcfg, int plloff)
1435{
1436 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1437 uintptr_t rcc_base = stm32mp_rcc_base();
1438 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1439 enum stm32mp1_plltype type = pll->plltype;
1440 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1441 unsigned long refclk;
1442 uint32_t ifrge = 0U;
Andre Przywara2d5690c2020-03-26 11:50:33 +00001443 uint32_t src, value, fracv = 0;
1444 void *fdt;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001445
1446 /* Check PLL output */
1447 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1448 return false;
1449 }
1450
1451 /* Check current clksrc */
1452 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1453 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1454 return false;
1455 }
1456
1457 /* Check Div */
1458 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1459
1460 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1461 (pllcfg[PLLCFG_M] + 1U);
1462
1463 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1464 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1465 return false;
1466 }
1467
1468 if ((type == PLL_800) && (refclk >= 8000000U)) {
1469 ifrge = 1U;
1470 }
1471
1472 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1473 RCC_PLLNCFGR1_DIVN_MASK;
1474 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1475 RCC_PLLNCFGR1_DIVM_MASK;
1476 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1477 RCC_PLLNCFGR1_IFRGE_MASK;
1478 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1479 return false;
1480 }
1481
1482 /* Fractional configuration */
Andre Przywara2d5690c2020-03-26 11:50:33 +00001483 if (fdt_get_address(&fdt) == 1) {
1484 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1485 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001486
1487 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1488 value |= RCC_PLLNFRACR_FRACLE;
1489 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1490 return false;
1491 }
1492
1493 /* Output config */
1494 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1495 RCC_PLLNCFGR2_DIVP_MASK;
1496 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1497 RCC_PLLNCFGR2_DIVQ_MASK;
1498 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1499 RCC_PLLNCFGR2_DIVR_MASK;
1500 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1501 return false;
1502 }
1503
1504 return true;
1505}
1506
1507static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001508{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001509 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1510 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001511
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001512 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1513 mmio_clrsetbits_32(pllxcr,
1514 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1515 RCC_PLLNCR_DIVREN,
1516 RCC_PLLNCR_PLLON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001517}
1518
Yann Gautiere4a3c352019-02-14 10:53:33 +01001519static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001520{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001521 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1522 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001523 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001524
Yann Gautier9aea69e2018-07-24 17:13:36 +02001525 /* Wait PLL lock */
1526 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001527 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001528 ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001529 pll_id, pllxcr, mmio_read_32(pllxcr));
1530 return -ETIMEDOUT;
1531 }
1532 }
1533
1534 /* Start the requested output */
1535 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1536
1537 return 0;
1538}
1539
Yann Gautiere4a3c352019-02-14 10:53:33 +01001540static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001541{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001542 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1543 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001544 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001545
1546 /* Stop all output */
1547 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1548 RCC_PLLNCR_DIVREN);
1549
1550 /* Stop PLL */
1551 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1552
Yann Gautier2299d572019-02-14 11:14:39 +01001553 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001554 /* Wait PLL stopped */
1555 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001556 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001557 ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001558 pll_id, pllxcr, mmio_read_32(pllxcr));
1559 return -ETIMEDOUT;
1560 }
1561 }
1562
1563 return 0;
1564}
1565
Yann Gautiere4a3c352019-02-14 10:53:33 +01001566static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001567 uint32_t *pllcfg)
1568{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001569 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1570 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001571 uint32_t value;
1572
1573 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1574 RCC_PLLNCFGR2_DIVP_MASK;
1575 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1576 RCC_PLLNCFGR2_DIVQ_MASK;
1577 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1578 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001579 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001580}
1581
Yann Gautiere4a3c352019-02-14 10:53:33 +01001582static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001583 uint32_t *pllcfg, uint32_t fracv)
1584{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001585 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1586 uintptr_t rcc_base = stm32mp_rcc_base();
1587 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001588 unsigned long refclk;
1589 uint32_t ifrge = 0;
1590 uint32_t src, value;
1591
Yann Gautiere4a3c352019-02-14 10:53:33 +01001592 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier9aea69e2018-07-24 17:13:36 +02001593 RCC_SELR_REFCLK_SRC_MASK;
1594
Yann Gautiere4a3c352019-02-14 10:53:33 +01001595 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier9aea69e2018-07-24 17:13:36 +02001596 (pllcfg[PLLCFG_M] + 1U);
1597
1598 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1599 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1600 return -EINVAL;
1601 }
1602
1603 if ((type == PLL_800) && (refclk >= 8000000U)) {
1604 ifrge = 1U;
1605 }
1606
1607 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1608 RCC_PLLNCFGR1_DIVN_MASK;
1609 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1610 RCC_PLLNCFGR1_DIVM_MASK;
1611 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1612 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001613 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001614
1615 /* Fractional configuration */
1616 value = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001617 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001618
1619 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001620 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001621
1622 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001623 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001624
Yann Gautiere4a3c352019-02-14 10:53:33 +01001625 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001626
1627 return 0;
1628}
1629
Yann Gautiere4a3c352019-02-14 10:53:33 +01001630static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001631{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001632 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001633 uint32_t pllxcsg = 0;
1634
1635 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1636 RCC_PLLNCSGR_MOD_PER_MASK;
1637
1638 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1639 RCC_PLLNCSGR_INC_STEP_MASK;
1640
1641 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1642 RCC_PLLNCSGR_SSCG_MODE_MASK;
1643
Yann Gautiere4a3c352019-02-14 10:53:33 +01001644 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001645
1646 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1647 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001648}
1649
Yann Gautiere4a3c352019-02-14 10:53:33 +01001650static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001651{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001652 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier2299d572019-02-14 11:14:39 +01001653 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001654
Yann Gautiere4a3c352019-02-14 10:53:33 +01001655 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001656 clksrc & RCC_SELR_SRC_MASK);
1657
Yann Gautier2299d572019-02-14 11:14:39 +01001658 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001659 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001660 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001661 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1662 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001663 return -ETIMEDOUT;
1664 }
1665 }
1666
1667 return 0;
1668}
1669
Yann Gautiere4a3c352019-02-14 10:53:33 +01001670static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001671{
Yann Gautier2299d572019-02-14 11:14:39 +01001672 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001673
1674 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1675 clkdiv & RCC_DIVR_DIV_MASK);
1676
Yann Gautier2299d572019-02-14 11:14:39 +01001677 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001678 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001679 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001680 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001681 clkdiv, address, mmio_read_32(address));
1682 return -ETIMEDOUT;
1683 }
1684 }
1685
1686 return 0;
1687}
1688
Yann Gautiere4a3c352019-02-14 10:53:33 +01001689static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001690{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001691 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001692
1693 /*
1694 * Binding clksrc :
1695 * bit15-4 offset
1696 * bit3: disable
1697 * bit2-0: MCOSEL[2:0]
1698 */
1699 if ((clksrc & 0x8U) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001700 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001701 } else {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001702 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001703 RCC_MCOCFG_MCOSRC_MASK,
1704 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001705 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001706 RCC_MCOCFG_MCODIV_MASK,
1707 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001708 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001709 }
1710}
1711
Yann Gautiere4a3c352019-02-14 10:53:33 +01001712static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001713{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001714 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001715
1716 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1717 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1718 mmio_clrsetbits_32(address,
1719 RCC_BDCR_RTCSRC_MASK,
Yann Gautier74aa83a2021-04-06 13:41:19 +02001720 (clksrc & RCC_SELR_SRC_MASK) << RCC_BDCR_RTCSRC_SHIFT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001721
1722 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1723 }
1724
1725 if (lse_css) {
1726 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1727 }
1728}
1729
Yann Gautiere4a3c352019-02-14 10:53:33 +01001730static void stm32mp1_stgen_config(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001731{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001732 uint32_t cntfid0;
1733 unsigned long rate;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001734 unsigned long long counter;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001735
Yann Gautiera18f61b2020-05-05 17:58:40 +02001736 cntfid0 = mmio_read_32(STGEN_BASE + CNTFID_OFF);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001737 rate = get_clock_rate(stm32mp1_clk_get_parent(STGEN_K));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001738
Yann Gautiere4a3c352019-02-14 10:53:33 +01001739 if (cntfid0 == rate) {
1740 return;
1741 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001742
Yann Gautiera18f61b2020-05-05 17:58:40 +02001743 mmio_clrbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
1744 counter = (unsigned long long)mmio_read_32(STGEN_BASE + CNTCVL_OFF);
1745 counter |= ((unsigned long long)mmio_read_32(STGEN_BASE + CNTCVU_OFF)) << 32;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001746 counter = (counter * rate / cntfid0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001747
Yann Gautiera18f61b2020-05-05 17:58:40 +02001748 mmio_write_32(STGEN_BASE + CNTCVL_OFF, (uint32_t)counter);
1749 mmio_write_32(STGEN_BASE + CNTCVU_OFF, (uint32_t)(counter >> 32));
1750 mmio_write_32(STGEN_BASE + CNTFID_OFF, rate);
1751 mmio_setbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001752
Yann Gautiere4a3c352019-02-14 10:53:33 +01001753 write_cntfrq((u_register_t)rate);
1754
1755 /* Need to update timer with new frequency */
1756 generic_delay_timer_init();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001757}
1758
1759void stm32mp1_stgen_increment(unsigned long long offset_in_ms)
1760{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001761 unsigned long long cnt;
1762
Yann Gautiera18f61b2020-05-05 17:58:40 +02001763 cnt = ((unsigned long long)mmio_read_32(STGEN_BASE + CNTCVU_OFF) << 32) |
1764 mmio_read_32(STGEN_BASE + CNTCVL_OFF);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001765
Yann Gautiera18f61b2020-05-05 17:58:40 +02001766 cnt += (offset_in_ms * mmio_read_32(STGEN_BASE + CNTFID_OFF)) / 1000U;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001767
Yann Gautiera18f61b2020-05-05 17:58:40 +02001768 mmio_clrbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
1769 mmio_write_32(STGEN_BASE + CNTCVL_OFF, (uint32_t)cnt);
1770 mmio_write_32(STGEN_BASE + CNTCVU_OFF, (uint32_t)(cnt >> 32));
1771 mmio_setbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001772}
1773
Yann Gautiere4a3c352019-02-14 10:53:33 +01001774static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001775{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001776 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001777 uint32_t value = pkcs & 0xFU;
1778 uint32_t mask = 0xFU;
1779
1780 if ((pkcs & BIT(31)) != 0U) {
1781 mask <<= 4;
1782 value <<= 4;
1783 }
1784
1785 mmio_clrsetbits_32(address, mask, value);
1786}
1787
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001788static int clk_get_pll_settings_from_dt(int plloff, unsigned int *pllcfg,
1789 uint32_t *fracv, uint32_t *csg,
1790 bool *csg_set)
1791{
1792 void *fdt;
1793 int ret;
1794
1795 if (fdt_get_address(&fdt) == 0) {
1796 return -FDT_ERR_NOTFOUND;
1797 }
1798
1799 ret = fdt_read_uint32_array(fdt, plloff, "cfg", (uint32_t)PLLCFG_NB,
1800 pllcfg);
1801 if (ret < 0) {
1802 return -FDT_ERR_NOTFOUND;
1803 }
1804
1805 *fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1806
1807 ret = fdt_read_uint32_array(fdt, plloff, "csg", (uint32_t)PLLCSG_NB,
1808 csg);
1809
1810 *csg_set = (ret == 0);
1811
1812 if (ret == -FDT_ERR_NOTFOUND) {
1813 ret = 0;
1814 }
1815
1816 return ret;
1817}
1818
Yann Gautier9aea69e2018-07-24 17:13:36 +02001819int stm32mp1_clk_init(void)
1820{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001821 uintptr_t rcc_base = stm32mp_rcc_base();
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001822 uint32_t pllfracv[_PLL_NB];
1823 uint32_t pllcsg[_PLL_NB][PLLCSG_NB];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001824 unsigned int clksrc[CLKSRC_NB];
1825 unsigned int clkdiv[CLKDIV_NB];
1826 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1827 int plloff[_PLL_NB];
1828 int ret, len;
1829 enum stm32mp1_pll_id i;
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001830 bool pllcsg_set[_PLL_NB];
1831 bool pllcfg_valid[_PLL_NB];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001832 bool lse_css = false;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001833 bool pll3_preserve = false;
1834 bool pll4_preserve = false;
1835 bool pll4_bootrom = false;
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001836 const fdt32_t *pkcs_cell;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001837 void *fdt;
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02001838 int stgen_p = stm32mp1_clk_get_parent(STGEN_K);
1839 int usbphy_p = stm32mp1_clk_get_parent(USBPHY_K);
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001840
1841 if (fdt_get_address(&fdt) == 0) {
Yann Gautier360e0e92020-09-16 16:40:34 +02001842 return -FDT_ERR_NOTFOUND;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001843 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001844
1845 /* Check status field to disable security */
1846 if (!fdt_get_rcc_secure_status()) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001847 mmio_write_32(rcc_base + RCC_TZCR, 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001848 }
1849
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001850 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1851 clksrc);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001852 if (ret < 0) {
1853 return -FDT_ERR_NOTFOUND;
1854 }
1855
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001856 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1857 clkdiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001858 if (ret < 0) {
1859 return -FDT_ERR_NOTFOUND;
1860 }
1861
1862 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1863 char name[12];
1864
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01001865 snprintf(name, sizeof(name), "st,pll@%d", i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001866 plloff[i] = fdt_rcc_subnode_offset(name);
1867
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001868 pllcfg_valid[i] = fdt_check_node(plloff[i]);
1869 if (!pllcfg_valid[i]) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001870 continue;
1871 }
1872
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01001873 ret = clk_get_pll_settings_from_dt(plloff[i], pllcfg[i],
1874 &pllfracv[i], pllcsg[i],
1875 &pllcsg_set[i]);
1876 if (ret != 0) {
1877 return ret;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001878 }
1879 }
1880
Yann Gautiere4a3c352019-02-14 10:53:33 +01001881 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1882 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001883
1884 /*
1885 * Switch ON oscillator found in device-tree.
1886 * Note: HSI already ON after BootROM stage.
1887 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001888 if (stm32mp1_osc[_LSI] != 0U) {
1889 stm32mp1_lsi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001890 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001891 if (stm32mp1_osc[_LSE] != 0U) {
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001892 const char *name = stm32mp_osc_node_label[_LSE];
Yann Gautiere4a3c352019-02-14 10:53:33 +01001893 bool bypass, digbyp;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001894 uint32_t lsedrv;
1895
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001896 bypass = fdt_clk_read_bool(name, "st,bypass");
1897 digbyp = fdt_clk_read_bool(name, "st,digbypass");
1898 lse_css = fdt_clk_read_bool(name, "st,css");
1899 lsedrv = fdt_clk_read_uint32_default(name, "st,drive",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001900 LSEDRV_MEDIUM_HIGH);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001901 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001902 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001903 if (stm32mp1_osc[_HSE] != 0U) {
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001904 const char *name = stm32mp_osc_node_label[_HSE];
Yann Gautiere4a3c352019-02-14 10:53:33 +01001905 bool bypass, digbyp, css;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001906
Gabriel Fernandez5177ea22020-05-15 08:00:03 +02001907 bypass = fdt_clk_read_bool(name, "st,bypass");
1908 digbyp = fdt_clk_read_bool(name, "st,digbypass");
1909 css = fdt_clk_read_bool(name, "st,css");
Yann Gautiere4a3c352019-02-14 10:53:33 +01001910 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001911 }
1912 /*
1913 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1914 * => switch on CSI even if node is not present in device tree
1915 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001916 stm32mp1_csi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001917
1918 /* Come back to HSI */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001919 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001920 if (ret != 0) {
1921 return ret;
1922 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001923 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001924 if (ret != 0) {
1925 return ret;
1926 }
Yann Gautiered342322019-02-15 17:33:27 +01001927 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1928 if (ret != 0) {
1929 return ret;
1930 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001931
Yann Gautiere4a3c352019-02-14 10:53:33 +01001932 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1933 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
1934 pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
1935 clksrc[CLKSRC_PLL3],
1936 pllcfg[_PLL3],
1937 plloff[_PLL3]);
1938 pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
1939 clksrc[CLKSRC_PLL4],
1940 pllcfg[_PLL4],
1941 plloff[_PLL4]);
1942 }
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02001943 /* Don't initialize PLL4, when used by BOOTROM */
1944 if ((stm32mp_get_boot_itf_selected() ==
1945 BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) &&
1946 ((stgen_p == (int)_PLL4_R) || (usbphy_p == (int)_PLL4_R))) {
1947 pll4_bootrom = true;
1948 pll4_preserve = true;
1949 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001950
Yann Gautier9aea69e2018-07-24 17:13:36 +02001951 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001952 if (((i == _PLL3) && pll3_preserve) ||
1953 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001954 continue;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001955 }
1956
1957 ret = stm32mp1_pll_stop(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001958 if (ret != 0) {
1959 return ret;
1960 }
1961 }
1962
1963 /* Configure HSIDIV */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001964 if (stm32mp1_osc[_HSI] != 0U) {
1965 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001966 if (ret != 0) {
1967 return ret;
1968 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001969 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001970 }
1971
1972 /* Select DIV */
1973 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001974 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001975 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001976 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001977 if (ret != 0) {
1978 return ret;
1979 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001980 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001981 if (ret != 0) {
1982 return ret;
1983 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001984 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001985 if (ret != 0) {
1986 return ret;
1987 }
Yann Gautiered342322019-02-15 17:33:27 +01001988 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
1989 if (ret != 0) {
1990 return ret;
1991 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001992 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001993 if (ret != 0) {
1994 return ret;
1995 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001996 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001997 if (ret != 0) {
1998 return ret;
1999 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002000 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002001 if (ret != 0) {
2002 return ret;
2003 }
2004
2005 /* No ready bit for RTC */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002006 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002007 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
2008
2009 /* Configure PLLs source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002010 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002011 if (ret != 0) {
2012 return ret;
2013 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002014
2015 if (!pll3_preserve) {
2016 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
2017 if (ret != 0) {
2018 return ret;
2019 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002020 }
2021
Yann Gautiere4a3c352019-02-14 10:53:33 +01002022 if (!pll4_preserve) {
2023 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
2024 if (ret != 0) {
2025 return ret;
2026 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002027 }
2028
2029 /* Configure and start PLLs */
2030 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002031 if (((i == _PLL3) && pll3_preserve) ||
2032 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
2033 continue;
2034 }
2035
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002036 if (!pllcfg_valid[i]) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02002037 continue;
2038 }
2039
Yann Gautiere4a3c352019-02-14 10:53:33 +01002040 if ((i == _PLL4) && pll4_bootrom) {
2041 /* Set output divider if not done by the Bootrom */
2042 stm32mp1_pll_config_output(i, pllcfg[i]);
2043 continue;
2044 }
2045
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002046 ret = stm32mp1_pll_config(i, pllcfg[i], pllfracv[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002047 if (ret != 0) {
2048 return ret;
2049 }
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002050
2051 if (pllcsg_set[i]) {
2052 stm32mp1_pll_csg(i, pllcsg[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002053 }
2054
Yann Gautiere4a3c352019-02-14 10:53:33 +01002055 stm32mp1_pll_start(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002056 }
2057 /* Wait and start PLLs ouptut when ready */
2058 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Nicolas Le Bayondab197a2019-11-13 11:46:31 +01002059 if (!pllcfg_valid[i]) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02002060 continue;
2061 }
2062
Yann Gautiere4a3c352019-02-14 10:53:33 +01002063 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002064 if (ret != 0) {
2065 return ret;
2066 }
2067 }
2068 /* Wait LSE ready before to use it */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002069 if (stm32mp1_osc[_LSE] != 0U) {
2070 stm32mp1_lse_wait();
Yann Gautier9aea69e2018-07-24 17:13:36 +02002071 }
2072
2073 /* Configure with expected clock source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002074 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002075 if (ret != 0) {
2076 return ret;
2077 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002078 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002079 if (ret != 0) {
2080 return ret;
2081 }
Yann Gautiered342322019-02-15 17:33:27 +01002082 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
2083 if (ret != 0) {
2084 return ret;
2085 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002086 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002087
2088 /* Configure PKCK */
2089 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
2090 if (pkcs_cell != NULL) {
2091 bool ckper_disabled = false;
2092 uint32_t j;
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02002093 uint32_t usbreg_bootrom = 0U;
2094
2095 if (pll4_bootrom) {
2096 usbreg_bootrom = mmio_read_32(rcc_base + RCC_USBCKSELR);
2097 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002098
Yann Gautier9aea69e2018-07-24 17:13:36 +02002099 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautierf9af3bc2018-11-09 15:57:18 +01002100 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002101
2102 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
2103 ckper_disabled = true;
2104 continue;
2105 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002106 stm32mp1_pkcs_config(pkcs);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002107 }
2108
2109 /*
2110 * CKPER is source for some peripheral clocks
2111 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2112 * only if previous clock is still ON
2113 * => deactivated CKPER only after switching clock
2114 */
2115 if (ckper_disabled) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002116 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002117 }
Patrick Delaunay64e1b2c2020-09-04 17:39:12 +02002118
2119 if (pll4_bootrom) {
2120 uint32_t usbreg_value, usbreg_mask;
2121 const struct stm32mp1_clk_sel *sel;
2122
2123 sel = clk_sel_ref(_USBPHY_SEL);
2124 usbreg_mask = (uint32_t)sel->msk << sel->src;
2125 sel = clk_sel_ref(_USBO_SEL);
2126 usbreg_mask |= (uint32_t)sel->msk << sel->src;
2127
2128 usbreg_value = mmio_read_32(rcc_base + RCC_USBCKSELR) &
2129 usbreg_mask;
2130 usbreg_bootrom &= usbreg_mask;
2131 if (usbreg_bootrom != usbreg_value) {
2132 VERBOSE("forbidden new USB clk path\n");
2133 VERBOSE("vs bootrom on USB boot\n");
2134 return -FDT_ERR_BADVALUE;
2135 }
2136 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02002137 }
2138
2139 /* Switch OFF HSI if not found in device-tree */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002140 if (stm32mp1_osc[_HSI] == 0U) {
2141 stm32mp1_hsi_set(false);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002142 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002143 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02002144
2145 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002146 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002147 RCC_DDRITFCR_DDRCKMOD_MASK,
2148 RCC_DDRITFCR_DDRCKMOD_SSR <<
2149 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2150
2151 return 0;
2152}
2153
2154static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002155 enum stm32mp_osc_id index)
2156{
2157 uint32_t frequency;
2158
Yann Gautiere4a3c352019-02-14 10:53:33 +01002159 if (fdt_osc_read_freq(name, &frequency) == 0) {
2160 stm32mp1_osc[index] = frequency;
Yann Gautier9aea69e2018-07-24 17:13:36 +02002161 }
2162}
2163
2164static void stm32mp1_osc_init(void)
2165{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002166 enum stm32mp_osc_id i;
2167
2168 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002169 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002170 }
2171}
Etienne Carriere1368ada2020-05-13 11:49:49 +02002172
2173#ifdef STM32MP_SHARED_RESOURCES
2174/*
2175 * Get the parent ID of the target parent clock, for tagging as secure
2176 * shared clock dependencies.
2177 */
2178static int get_parent_id_parent(unsigned int parent_id)
2179{
2180 enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
2181 enum stm32mp1_pll_id pll_id;
2182 uint32_t p_sel;
2183 uintptr_t rcc_base = stm32mp_rcc_base();
2184
2185 switch (parent_id) {
2186 case _ACLK:
2187 case _PCLK4:
2188 case _PCLK5:
2189 s = _AXIS_SEL;
2190 break;
2191 case _PLL1_P:
2192 case _PLL1_Q:
2193 case _PLL1_R:
2194 pll_id = _PLL1;
2195 break;
2196 case _PLL2_P:
2197 case _PLL2_Q:
2198 case _PLL2_R:
2199 pll_id = _PLL2;
2200 break;
2201 case _PLL3_P:
2202 case _PLL3_Q:
2203 case _PLL3_R:
2204 pll_id = _PLL3;
2205 break;
2206 case _PLL4_P:
2207 case _PLL4_Q:
2208 case _PLL4_R:
2209 pll_id = _PLL4;
2210 break;
2211 case _PCLK1:
2212 case _PCLK2:
2213 case _HCLK2:
2214 case _HCLK6:
2215 case _CK_PER:
2216 case _CK_MPU:
2217 case _CK_MCU:
2218 case _USB_PHY_48:
2219 /* We do not expect to access these */
2220 panic();
2221 break;
2222 default:
2223 /* Other parents have no parent */
2224 return -1;
2225 }
2226
2227 if (s != _UNKNOWN_SEL) {
2228 const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
2229
2230 p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
2231 sel->msk;
2232
2233 if (p_sel < sel->nb_parent) {
2234 return (int)sel->parent[p_sel];
2235 }
2236 } else {
2237 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
2238
2239 p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
2240 RCC_SELR_REFCLK_SRC_MASK;
2241
2242 if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
2243 return (int)pll->refclk[p_sel];
2244 }
2245 }
2246
2247 VERBOSE("No parent selected for %s\n",
2248 stm32mp1_clk_parent_name[parent_id]);
2249
2250 return -1;
2251}
2252
2253static void secure_parent_clocks(unsigned long parent_id)
2254{
2255 int grandparent_id;
2256
2257 switch (parent_id) {
2258 case _PLL3_P:
2259 case _PLL3_Q:
2260 case _PLL3_R:
2261 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2262 break;
2263
2264 /* These clocks are always secure when RCC is secure */
2265 case _ACLK:
2266 case _HCLK2:
2267 case _HCLK6:
2268 case _PCLK4:
2269 case _PCLK5:
2270 case _PLL1_P:
2271 case _PLL1_Q:
2272 case _PLL1_R:
2273 case _PLL2_P:
2274 case _PLL2_Q:
2275 case _PLL2_R:
2276 case _HSI:
2277 case _HSI_KER:
2278 case _LSI:
2279 case _CSI:
2280 case _CSI_KER:
2281 case _HSE:
2282 case _HSE_KER:
2283 case _HSE_KER_DIV2:
Gabriel Fernandez4e3a51a2021-07-27 15:39:16 +02002284 case _HSE_RTC:
Etienne Carriere1368ada2020-05-13 11:49:49 +02002285 case _LSE:
2286 break;
2287
2288 default:
2289 VERBOSE("Cannot secure parent clock %s\n",
2290 stm32mp1_clk_parent_name[parent_id]);
2291 panic();
2292 }
2293
2294 grandparent_id = get_parent_id_parent(parent_id);
2295 if (grandparent_id >= 0) {
2296 secure_parent_clocks(grandparent_id);
2297 }
2298}
2299
2300void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
2301{
2302 int parent_id;
2303
2304 if (!stm32mp1_rcc_is_secure()) {
2305 return;
2306 }
2307
2308 switch (clock_id) {
2309 case PLL1:
2310 case PLL2:
2311 /* PLL1/PLL2 are always secure: nothing to do */
2312 break;
2313 case PLL3:
2314 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2315 break;
2316 case PLL4:
2317 ERROR("PLL4 cannot be secured\n");
2318 panic();
2319 break;
2320 default:
2321 /* Others are expected gateable clock */
2322 parent_id = stm32mp1_clk_get_parent(clock_id);
2323 if (parent_id < 0) {
2324 INFO("No parent found for clock %lu\n", clock_id);
2325 } else {
2326 secure_parent_clocks(parent_id);
2327 }
2328 break;
2329 }
2330}
2331#endif /* STM32MP_SHARED_RESOURCES */
Yann Gautier9aea69e2018-07-24 17:13:36 +02002332
Yann Gautierc7f9e962019-05-20 14:39:26 +02002333static void sync_earlyboot_clocks_state(void)
2334{
Etienne Carriere2a756c22019-12-08 08:23:35 +01002335 unsigned int idx;
2336 const unsigned long secure_enable[] = {
2337 AXIDCG,
2338 BSEC,
2339 DDRC1, DDRC1LP,
2340 DDRC2, DDRC2LP,
2341 DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
2342 DDRPHYC, DDRPHYCLP,
Lionel Debievecfa88cc2019-09-02 18:15:45 +02002343 RTCAPB,
Etienne Carriere2a756c22019-12-08 08:23:35 +01002344 TZC1, TZC2,
2345 TZPC,
2346 STGEN_K,
2347 };
2348
2349 for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
2350 stm32mp_clk_enable(secure_enable[idx]);
2351 }
Yann Gautierc7f9e962019-05-20 14:39:26 +02002352}
2353
Yann Gautiera205a5c2021-08-30 15:06:54 +02002354static const struct clk_ops stm32mp_clk_ops = {
2355 .enable = stm32mp_clk_enable,
2356 .disable = stm32mp_clk_disable,
2357 .is_enabled = stm32mp_clk_is_enabled,
2358 .get_rate = stm32mp_clk_get_rate,
2359 .get_parent = stm32mp1_clk_get_parent,
2360};
2361
Yann Gautier9aea69e2018-07-24 17:13:36 +02002362int stm32mp1_clk_probe(void)
2363{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002364 stm32mp1_osc_init();
2365
Yann Gautierc7f9e962019-05-20 14:39:26 +02002366 sync_earlyboot_clocks_state();
2367
Yann Gautiera205a5c2021-08-30 15:06:54 +02002368 clk_register(&stm32mp_clk_ops);
2369
Yann Gautier9aea69e2018-07-24 17:13:36 +02002370 return 0;
2371}