blob: 18a2fe4f346eb5a8c379db0ff3f42deb5f71627e [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Yann Gautiera2e2a302019-02-14 11:13:39 +01002 * Copyright (C) 2018-2019, 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
12#include <libfdt.h>
13
Yann Gautier57e282b2019-01-07 11:17:24 +010014#include <platform_def.h>
15
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <arch.h>
17#include <arch_helpers.h>
18#include <common/debug.h>
Andre Przywaracc99f3f2020-03-26 12:51:21 +000019#include <common/fdt_wrappers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <drivers/delay_timer.h>
21#include <drivers/generic_delay_timer.h>
Yann Gautier4d429472019-02-14 11:15:20 +010022#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <drivers/st/stm32mp1_rcc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000025#include <dt-bindings/clock/stm32mp1-clksrc.h>
26#include <lib/mmio.h>
Yann Gautiere4a3c352019-02-14 10:53:33 +010027#include <lib/spinlock.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000028#include <lib/utils_def.h>
29#include <plat/common/platform.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,
59 _CSI_KER,
60 _PLL1_P,
61 _PLL1_Q,
62 _PLL1_R,
63 _PLL2_P,
64 _PLL2_Q,
65 _PLL2_R,
66 _PLL3_P,
67 _PLL3_Q,
68 _PLL3_R,
69 _PLL4_P,
70 _PLL4_Q,
71 _PLL4_R,
72 _ACLK,
73 _PCLK1,
74 _PCLK2,
75 _PCLK3,
76 _PCLK4,
77 _PCLK5,
78 _HCLK6,
79 _HCLK2,
80 _CK_PER,
81 _CK_MPU,
Yann Gautiered342322019-02-15 17:33:27 +010082 _CK_MCU,
Yann Gautiere4a3c352019-02-14 10:53:33 +010083 _USB_PHY_48,
Yann Gautier9aea69e2018-07-24 17:13:36 +020084 _PARENT_NB,
85 _UNKNOWN_ID = 0xff,
86};
87
Yann Gautiere4a3c352019-02-14 10:53:33 +010088/* Lists only the parent clock we are interested in */
Yann Gautier9aea69e2018-07-24 17:13:36 +020089enum stm32mp1_parent_sel {
Yann Gautiere4a3c352019-02-14 10:53:33 +010090 _I2C12_SEL,
91 _I2C35_SEL,
92 _STGEN_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +020093 _I2C46_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010094 _SPI6_SEL,
Yann Gautier9d8bbcd2019-05-07 18:49:33 +020095 _UART1_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010096 _RNG1_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +020097 _UART6_SEL,
98 _UART24_SEL,
99 _UART35_SEL,
100 _UART78_SEL,
101 _SDMMC12_SEL,
102 _SDMMC3_SEL,
103 _QSPI_SEL,
104 _FMC_SEL,
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200105 _AXIS_SEL,
106 _MCUS_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200107 _USBPHY_SEL,
108 _USBO_SEL,
Etienne Carriere04132612019-12-08 08:20:12 +0100109 _MPU_SEL,
110 _PER_SEL,
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100111 _RTC_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200112 _PARENT_SEL_NB,
113 _UNKNOWN_SEL = 0xff,
114};
115
Etienne Carriere04132612019-12-08 08:20:12 +0100116/* State the parent clock ID straight related to a clock */
117static const uint8_t parent_id_clock_id[_PARENT_NB] = {
118 [_HSE] = CK_HSE,
119 [_HSI] = CK_HSI,
120 [_CSI] = CK_CSI,
121 [_LSE] = CK_LSE,
122 [_LSI] = CK_LSI,
123 [_I2S_CKIN] = _UNKNOWN_ID,
124 [_USB_PHY_48] = _UNKNOWN_ID,
125 [_HSI_KER] = CK_HSI,
126 [_HSE_KER] = CK_HSE,
127 [_HSE_KER_DIV2] = CK_HSE_DIV2,
128 [_CSI_KER] = CK_CSI,
129 [_PLL1_P] = PLL1_P,
130 [_PLL1_Q] = PLL1_Q,
131 [_PLL1_R] = PLL1_R,
132 [_PLL2_P] = PLL2_P,
133 [_PLL2_Q] = PLL2_Q,
134 [_PLL2_R] = PLL2_R,
135 [_PLL3_P] = PLL3_P,
136 [_PLL3_Q] = PLL3_Q,
137 [_PLL3_R] = PLL3_R,
138 [_PLL4_P] = PLL4_P,
139 [_PLL4_Q] = PLL4_Q,
140 [_PLL4_R] = PLL4_R,
141 [_ACLK] = CK_AXI,
142 [_PCLK1] = CK_AXI,
143 [_PCLK2] = CK_AXI,
144 [_PCLK3] = CK_AXI,
145 [_PCLK4] = CK_AXI,
146 [_PCLK5] = CK_AXI,
147 [_CK_PER] = CK_PER,
148 [_CK_MPU] = CK_MPU,
149 [_CK_MCU] = CK_MCU,
150};
151
152static unsigned int clock_id2parent_id(unsigned long id)
153{
154 unsigned int n;
155
156 for (n = 0U; n < ARRAY_SIZE(parent_id_clock_id); n++) {
157 if (parent_id_clock_id[n] == id) {
158 return n;
159 }
160 }
161
162 return _UNKNOWN_ID;
163}
164
Yann Gautier9aea69e2018-07-24 17:13:36 +0200165enum stm32mp1_pll_id {
166 _PLL1,
167 _PLL2,
168 _PLL3,
169 _PLL4,
170 _PLL_NB
171};
172
173enum stm32mp1_div_id {
174 _DIV_P,
175 _DIV_Q,
176 _DIV_R,
177 _DIV_NB,
178};
179
180enum stm32mp1_clksrc_id {
181 CLKSRC_MPU,
182 CLKSRC_AXI,
Yann Gautiered342322019-02-15 17:33:27 +0100183 CLKSRC_MCU,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200184 CLKSRC_PLL12,
185 CLKSRC_PLL3,
186 CLKSRC_PLL4,
187 CLKSRC_RTC,
188 CLKSRC_MCO1,
189 CLKSRC_MCO2,
190 CLKSRC_NB
191};
192
193enum stm32mp1_clkdiv_id {
194 CLKDIV_MPU,
195 CLKDIV_AXI,
Yann Gautiered342322019-02-15 17:33:27 +0100196 CLKDIV_MCU,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200197 CLKDIV_APB1,
198 CLKDIV_APB2,
199 CLKDIV_APB3,
200 CLKDIV_APB4,
201 CLKDIV_APB5,
202 CLKDIV_RTC,
203 CLKDIV_MCO1,
204 CLKDIV_MCO2,
205 CLKDIV_NB
206};
207
208enum stm32mp1_pllcfg {
209 PLLCFG_M,
210 PLLCFG_N,
211 PLLCFG_P,
212 PLLCFG_Q,
213 PLLCFG_R,
214 PLLCFG_O,
215 PLLCFG_NB
216};
217
218enum stm32mp1_pllcsg {
219 PLLCSG_MOD_PER,
220 PLLCSG_INC_STEP,
221 PLLCSG_SSCG_MODE,
222 PLLCSG_NB
223};
224
225enum stm32mp1_plltype {
226 PLL_800,
227 PLL_1600,
228 PLL_TYPE_NB
229};
230
231struct stm32mp1_pll {
232 uint8_t refclk_min;
233 uint8_t refclk_max;
234 uint8_t divn_max;
235};
236
237struct stm32mp1_clk_gate {
238 uint16_t offset;
239 uint8_t bit;
240 uint8_t index;
241 uint8_t set_clr;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100242 uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
243 uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
Yann Gautier9aea69e2018-07-24 17:13:36 +0200244};
245
246struct stm32mp1_clk_sel {
247 uint16_t offset;
248 uint8_t src;
249 uint8_t msk;
250 uint8_t nb_parent;
251 const uint8_t *parent;
252};
253
254#define REFCLK_SIZE 4
255struct stm32mp1_clk_pll {
256 enum stm32mp1_plltype plltype;
257 uint16_t rckxselr;
258 uint16_t pllxcfgr1;
259 uint16_t pllxcfgr2;
260 uint16_t pllxfracr;
261 uint16_t pllxcr;
262 uint16_t pllxcsgr;
263 enum stm32mp_osc_id refclk[REFCLK_SIZE];
264};
265
Yann Gautiere4a3c352019-02-14 10:53:33 +0100266/* Clocks with selectable source and non set/clr register access */
267#define _CLK_SELEC(off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200268 { \
269 .offset = (off), \
270 .bit = (b), \
271 .index = (idx), \
272 .set_clr = 0, \
273 .sel = (s), \
274 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200275 }
276
Yann Gautiere4a3c352019-02-14 10:53:33 +0100277/* Clocks with fixed source and non set/clr register access */
278#define _CLK_FIXED(off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200279 { \
280 .offset = (off), \
281 .bit = (b), \
282 .index = (idx), \
283 .set_clr = 0, \
284 .sel = _UNKNOWN_SEL, \
285 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200286 }
287
Yann Gautiere4a3c352019-02-14 10:53:33 +0100288/* Clocks with selectable source and set/clr register access */
289#define _CLK_SC_SELEC(off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200290 { \
291 .offset = (off), \
292 .bit = (b), \
293 .index = (idx), \
294 .set_clr = 1, \
295 .sel = (s), \
296 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200297 }
298
Yann Gautiere4a3c352019-02-14 10:53:33 +0100299/* Clocks with fixed source and set/clr register access */
300#define _CLK_SC_FIXED(off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200301 { \
302 .offset = (off), \
303 .bit = (b), \
304 .index = (idx), \
305 .set_clr = 1, \
306 .sel = _UNKNOWN_SEL, \
307 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200308 }
309
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200310#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \
311 [_ ## _label ## _SEL] = { \
312 .offset = _rcc_selr, \
313 .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \
Etienne Carrierec164ce22019-12-08 08:20:40 +0100314 .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \
315 (_rcc_selr ## _ ## _label ## SRC_SHIFT), \
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200316 .parent = (_parents), \
317 .nb_parent = ARRAY_SIZE(_parents) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200318 }
319
Yann Gautiere4a3c352019-02-14 10:53:33 +0100320#define _CLK_PLL(idx, type, off1, off2, off3, \
321 off4, off5, off6, \
322 p1, p2, p3, p4) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200323 [(idx)] = { \
324 .plltype = (type), \
325 .rckxselr = (off1), \
326 .pllxcfgr1 = (off2), \
327 .pllxcfgr2 = (off3), \
328 .pllxfracr = (off4), \
329 .pllxcr = (off5), \
330 .pllxcsgr = (off6), \
331 .refclk[0] = (p1), \
332 .refclk[1] = (p2), \
333 .refclk[2] = (p3), \
334 .refclk[3] = (p4), \
335 }
336
Yann Gautiere4a3c352019-02-14 10:53:33 +0100337#define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
338
Yann Gautier9aea69e2018-07-24 17:13:36 +0200339static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100340 _CLK_FIXED(RCC_DDRITFCR, 0, DDRC1, _ACLK),
341 _CLK_FIXED(RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
342 _CLK_FIXED(RCC_DDRITFCR, 2, DDRC2, _ACLK),
343 _CLK_FIXED(RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
344 _CLK_FIXED(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
345 _CLK_FIXED(RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
346 _CLK_FIXED(RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
347 _CLK_FIXED(RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
348 _CLK_FIXED(RCC_DDRITFCR, 8, AXIDCG, _ACLK),
349 _CLK_FIXED(RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
350 _CLK_FIXED(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),
351
352 _CLK_SC_FIXED(RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
353 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
354 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
355 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
356 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
357 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
358 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
359 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
360 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
361 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
362 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
363
364 _CLK_SC_FIXED(RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
365 _CLK_SC_SELEC(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
366
Yann Gautier3edc7c32019-05-20 19:17:08 +0200367 _CLK_SC_FIXED(RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID),
368
Yann Gautiere4a3c352019-02-14 10:53:33 +0100369 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
370 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
371 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
372
373 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
374 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
375 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200376 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
Yann Gautiere4a3c352019-02-14 10:53:33 +0100377 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
378 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
379 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
380 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
381 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
382 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
383 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
384
385 _CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
386 _CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
387
388 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
389 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
390 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
391 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
392 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
393 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
394 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
395 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
396 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
397 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
398 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
399
400 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
401 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
402 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
403 _CLK_SC_SELEC(RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
404 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),
405
406 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
407 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
408 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
409 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
410 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
411
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100412 _CLK_SELEC(RCC_BDCR, 20, RTC, _RTC_SEL),
Yann Gautiere4a3c352019-02-14 10:53:33 +0100413 _CLK_SELEC(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
414};
415
416static const uint8_t i2c12_parents[] = {
417 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
418};
419
420static const uint8_t i2c35_parents[] = {
421 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
422};
423
424static const uint8_t stgen_parents[] = {
425 _HSI_KER, _HSE_KER
426};
427
428static const uint8_t i2c46_parents[] = {
429 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
430};
431
432static const uint8_t spi6_parents[] = {
433 _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
434};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200435
Yann Gautiere4a3c352019-02-14 10:53:33 +0100436static const uint8_t usart1_parents[] = {
437 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
438};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200439
Yann Gautiere4a3c352019-02-14 10:53:33 +0100440static const uint8_t rng1_parents[] = {
441 _CSI, _PLL4_R, _LSE, _LSI
442};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200443
Yann Gautiere4a3c352019-02-14 10:53:33 +0100444static const uint8_t uart6_parents[] = {
445 _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
446};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200447
Yann Gautiere4a3c352019-02-14 10:53:33 +0100448static const uint8_t uart234578_parents[] = {
449 _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
450};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200451
Yann Gautiere4a3c352019-02-14 10:53:33 +0100452static const uint8_t sdmmc12_parents[] = {
453 _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
454};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200455
Yann Gautiere4a3c352019-02-14 10:53:33 +0100456static const uint8_t sdmmc3_parents[] = {
457 _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
458};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200459
Yann Gautiere4a3c352019-02-14 10:53:33 +0100460static const uint8_t qspi_parents[] = {
461 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
462};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200463
Yann Gautiere4a3c352019-02-14 10:53:33 +0100464static const uint8_t fmc_parents[] = {
465 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
466};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200467
Yann Gautiere4a3c352019-02-14 10:53:33 +0100468static const uint8_t ass_parents[] = {
469 _HSI, _HSE, _PLL2
Yann Gautier9aea69e2018-07-24 17:13:36 +0200470};
471
Yann Gautiered342322019-02-15 17:33:27 +0100472static const uint8_t mss_parents[] = {
473 _HSI, _HSE, _CSI, _PLL3
474};
475
Yann Gautiere4a3c352019-02-14 10:53:33 +0100476static const uint8_t usbphy_parents[] = {
477 _HSE_KER, _PLL4_R, _HSE_KER_DIV2
478};
479
480static const uint8_t usbo_parents[] = {
481 _PLL4_R, _USB_PHY_48
482};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200483
Etienne Carriere04132612019-12-08 08:20:12 +0100484static const uint8_t mpu_parents[] = {
485 _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */
486};
487
488static const uint8_t per_parents[] = {
489 _HSI, _HSE, _CSI,
490};
491
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100492static const uint8_t rtc_parents[] = {
493 _UNKNOWN_ID, _LSE, _LSI, _HSE
494};
495
Yann Gautier9aea69e2018-07-24 17:13:36 +0200496static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200497 _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
498 _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
499 _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
500 _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
501 _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
502 _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
503 _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
Etienne Carriere04132612019-12-08 08:20:12 +0100504 _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents),
505 _CLK_PARENT_SEL(PER, RCC_CPERCKSELR, per_parents),
Etienne Carrierebccc7d02019-12-08 08:22:31 +0100506 _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200507 _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
508 _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
509 _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
510 _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
511 _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
512 _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
513 _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
514 _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
515 _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, ass_parents),
516 _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mss_parents),
517 _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
518 _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200519};
520
521/* Define characteristic of PLL according type */
522#define DIVN_MIN 24
523static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
524 [PLL_800] = {
525 .refclk_min = 4,
526 .refclk_max = 16,
527 .divn_max = 99,
528 },
529 [PLL_1600] = {
530 .refclk_min = 8,
531 .refclk_max = 16,
532 .divn_max = 199,
533 },
534};
535
536/* PLLNCFGR2 register divider by output */
537static const uint8_t pllncfgr2[_DIV_NB] = {
538 [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
539 [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautiere4a3c352019-02-14 10:53:33 +0100540 [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200541};
542
543static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100544 _CLK_PLL(_PLL1, PLL_1600,
545 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
546 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
547 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
548 _CLK_PLL(_PLL2, PLL_1600,
549 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
550 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
551 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
552 _CLK_PLL(_PLL3, PLL_800,
553 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
554 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
555 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
556 _CLK_PLL(_PLL4, PLL_800,
557 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
558 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
559 _HSI, _HSE, _CSI, _I2S_CKIN),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200560};
561
562/* Prescaler table lookups for clock computation */
Yann Gautiered342322019-02-15 17:33:27 +0100563/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
564static const uint8_t stm32mp1_mcu_div[16] = {
565 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
566};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200567
568/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
569#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
570#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
571static const uint8_t stm32mp1_mpu_apbx_div[8] = {
572 0, 1, 2, 3, 4, 4, 4, 4
573};
574
575/* div = /1 /2 /3 /4 */
576static const uint8_t stm32mp1_axi_div[8] = {
577 1, 2, 3, 4, 4, 4, 4, 4
578};
579
Yann Gautiere4a3c352019-02-14 10:53:33 +0100580/* RCC clock device driver private */
581static unsigned long stm32mp1_osc[NB_OSC];
582static struct spinlock reg_lock;
583static unsigned int gate_refcounts[NB_GATES];
584static struct spinlock refcount_lock;
585
586static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
587{
588 return &stm32mp1_clk_gate[idx];
589}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200590
Yann Gautiere4a3c352019-02-14 10:53:33 +0100591static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
592{
593 return &stm32mp1_clk_sel[idx];
594}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200595
Yann Gautiere4a3c352019-02-14 10:53:33 +0100596static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
597{
598 return &stm32mp1_clk_pll[idx];
599}
600
Yann Gautiere4a3c352019-02-14 10:53:33 +0100601static void stm32mp1_clk_lock(struct spinlock *lock)
602{
Yann Gautierf540a592019-05-22 19:13:51 +0200603 if (stm32mp_lock_available()) {
604 /* Assume interrupts are masked */
605 spin_lock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100606 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100607}
608
609static void stm32mp1_clk_unlock(struct spinlock *lock)
610{
Yann Gautierf540a592019-05-22 19:13:51 +0200611 if (stm32mp_lock_available()) {
612 spin_unlock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100613 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100614}
615
616bool stm32mp1_rcc_is_secure(void)
617{
618 uintptr_t rcc_base = stm32mp_rcc_base();
619
620 return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_TZEN) != 0;
621}
622
Yann Gautiered342322019-02-15 17:33:27 +0100623bool stm32mp1_rcc_is_mckprot(void)
624{
625 uintptr_t rcc_base = stm32mp_rcc_base();
626
627 return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_MCKPROT) != 0;
628}
629
Yann Gautiere4a3c352019-02-14 10:53:33 +0100630void stm32mp1_clk_rcc_regs_lock(void)
631{
632 stm32mp1_clk_lock(&reg_lock);
633}
634
635void stm32mp1_clk_rcc_regs_unlock(void)
636{
637 stm32mp1_clk_unlock(&reg_lock);
638}
639
640static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200641{
642 if (idx >= NB_OSC) {
643 return 0;
644 }
645
Yann Gautiere4a3c352019-02-14 10:53:33 +0100646 return stm32mp1_osc[idx];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200647}
648
Yann Gautiere4a3c352019-02-14 10:53:33 +0100649static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200650{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100651 unsigned int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200652
Yann Gautiere4a3c352019-02-14 10:53:33 +0100653 for (i = 0U; i < NB_GATES; i++) {
654 if (gate_ref(i)->index == id) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200655 return i;
656 }
657 }
658
659 ERROR("%s: clk id %d not found\n", __func__, (uint32_t)id);
660
661 return -EINVAL;
662}
663
Yann Gautiere4a3c352019-02-14 10:53:33 +0100664static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200665{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100666 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200667}
668
Yann Gautiere4a3c352019-02-14 10:53:33 +0100669static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200670{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100671 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200672}
673
Yann Gautiere4a3c352019-02-14 10:53:33 +0100674static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200675{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100676 const struct stm32mp1_clk_sel *sel;
Etienne Carriere04132612019-12-08 08:20:12 +0100677 uint32_t p_sel;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200678 int i;
679 enum stm32mp1_parent_id p;
680 enum stm32mp1_parent_sel s;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100681 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200682
Etienne Carriere04132612019-12-08 08:20:12 +0100683 /* Few non gateable clock have a static parent ID, find them */
684 i = (int)clock_id2parent_id(id);
685 if (i != _UNKNOWN_ID) {
686 return i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200687 }
688
Yann Gautiere4a3c352019-02-14 10:53:33 +0100689 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200690 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100691 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200692 }
693
Yann Gautiere4a3c352019-02-14 10:53:33 +0100694 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200695 if (p < _PARENT_NB) {
696 return (int)p;
697 }
698
Yann Gautiere4a3c352019-02-14 10:53:33 +0100699 s = stm32mp1_clk_get_sel(i);
700 if (s == _UNKNOWN_SEL) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200701 return -EINVAL;
702 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100703 if (s >= _PARENT_SEL_NB) {
704 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200705 }
706
Yann Gautiere4a3c352019-02-14 10:53:33 +0100707 sel = clk_sel_ref(s);
Etienne Carrierec164ce22019-12-08 08:20:40 +0100708 p_sel = (mmio_read_32(rcc_base + sel->offset) &
709 (sel->msk << sel->src)) >> sel->src;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100710 if (p_sel < sel->nb_parent) {
711 return (int)sel->parent[p_sel];
712 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200713
714 return -EINVAL;
715}
716
Yann Gautiere4a3c352019-02-14 10:53:33 +0100717static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200718{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100719 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
720 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200721
Yann Gautiere4a3c352019-02-14 10:53:33 +0100722 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200723}
724
725/*
726 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
727 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
728 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
729 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
730 */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100731static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200732{
Yann Gautier9aea69e2018-07-24 17:13:36 +0200733 unsigned long refclk, fvco;
734 uint32_t cfgr1, fracr, divm, divn;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100735 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200736
Yann Gautiere4a3c352019-02-14 10:53:33 +0100737 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
738 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200739
740 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
741 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
742
Yann Gautiere4a3c352019-02-14 10:53:33 +0100743 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200744
745 /*
746 * With FRACV :
747 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
748 * Without FRACV
749 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
750 */
751 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100752 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
753 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200754 unsigned long long numerator, denominator;
755
Yann Gautiere4a3c352019-02-14 10:53:33 +0100756 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
757 numerator = refclk * numerator;
758 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200759 fvco = (unsigned long)(numerator / denominator);
760 } else {
761 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
762 }
763
764 return fvco;
765}
766
Yann Gautiere4a3c352019-02-14 10:53:33 +0100767static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200768 enum stm32mp1_div_id div_id)
769{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100770 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200771 unsigned long dfout;
772 uint32_t cfgr2, divy;
773
774 if (div_id >= _DIV_NB) {
775 return 0;
776 }
777
Yann Gautiere4a3c352019-02-14 10:53:33 +0100778 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200779 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
780
Yann Gautiere4a3c352019-02-14 10:53:33 +0100781 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200782
783 return dfout;
784}
785
Yann Gautiere4a3c352019-02-14 10:53:33 +0100786static unsigned long get_clock_rate(int p)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200787{
788 uint32_t reg, clkdiv;
789 unsigned long clock = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100790 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200791
792 switch (p) {
793 case _CK_MPU:
794 /* MPU sub system */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100795 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200796 switch (reg & RCC_SELR_SRC_MASK) {
797 case RCC_MPCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100798 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200799 break;
800 case RCC_MPCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100801 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200802 break;
803 case RCC_MPCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100804 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200805 break;
806 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100807 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200808
Yann Gautiere4a3c352019-02-14 10:53:33 +0100809 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200810 clkdiv = reg & RCC_MPUDIV_MASK;
811 if (clkdiv != 0U) {
812 clock /= stm32mp1_mpu_div[clkdiv];
813 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200814 break;
815 default:
816 break;
817 }
818 break;
819 /* AXI sub system */
820 case _ACLK:
821 case _HCLK2:
822 case _HCLK6:
823 case _PCLK4:
824 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100825 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200826 switch (reg & RCC_SELR_SRC_MASK) {
827 case RCC_ASSCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100828 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200829 break;
830 case RCC_ASSCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100831 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200832 break;
833 case RCC_ASSCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100834 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200835 break;
836 default:
837 break;
838 }
839
840 /* System clock divider */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100841 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200842 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
843
844 switch (p) {
845 case _PCLK4:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100846 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200847 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
848 break;
849 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100850 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200851 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
852 break;
853 default:
854 break;
855 }
856 break;
Yann Gautiered342322019-02-15 17:33:27 +0100857 /* MCU sub system */
858 case _CK_MCU:
859 case _PCLK1:
860 case _PCLK2:
861 case _PCLK3:
862 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
863 switch (reg & RCC_SELR_SRC_MASK) {
864 case RCC_MSSCKSELR_HSI:
865 clock = stm32mp1_clk_get_fixed(_HSI);
866 break;
867 case RCC_MSSCKSELR_HSE:
868 clock = stm32mp1_clk_get_fixed(_HSE);
869 break;
870 case RCC_MSSCKSELR_CSI:
871 clock = stm32mp1_clk_get_fixed(_CSI);
872 break;
873 case RCC_MSSCKSELR_PLL:
874 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
875 break;
876 default:
877 break;
878 }
879
880 /* MCU clock divider */
881 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
882 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
883
884 switch (p) {
885 case _PCLK1:
886 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
887 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
888 break;
889 case _PCLK2:
890 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
891 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
892 break;
893 case _PCLK3:
894 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
895 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
896 break;
897 case _CK_MCU:
898 default:
899 break;
900 }
901 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200902 case _CK_PER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100903 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200904 switch (reg & RCC_SELR_SRC_MASK) {
905 case RCC_CPERCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100906 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200907 break;
908 case RCC_CPERCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100909 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200910 break;
911 case RCC_CPERCKSELR_CSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100912 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200913 break;
914 default:
915 break;
916 }
917 break;
918 case _HSI:
919 case _HSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100920 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200921 break;
922 case _CSI:
923 case _CSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100924 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200925 break;
926 case _HSE:
927 case _HSE_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100928 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200929 break;
930 case _HSE_KER_DIV2:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100931 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200932 break;
933 case _LSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100934 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200935 break;
936 case _LSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100937 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200938 break;
939 /* PLL */
940 case _PLL1_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100941 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200942 break;
943 case _PLL1_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100944 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200945 break;
946 case _PLL1_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100947 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200948 break;
949 case _PLL2_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100950 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200951 break;
952 case _PLL2_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100953 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200954 break;
955 case _PLL2_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100956 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200957 break;
958 case _PLL3_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100959 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200960 break;
961 case _PLL3_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100962 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200963 break;
964 case _PLL3_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100965 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200966 break;
967 case _PLL4_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100968 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200969 break;
970 case _PLL4_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100971 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200972 break;
973 case _PLL4_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100974 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200975 break;
976 /* Other */
977 case _USB_PHY_48:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100978 clock = USB_PHY_48_MHZ;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200979 break;
980 default:
981 break;
982 }
983
984 return clock;
985}
986
Yann Gautiere4a3c352019-02-14 10:53:33 +0100987static void __clk_enable(struct stm32mp1_clk_gate const *gate)
988{
989 uintptr_t rcc_base = stm32mp_rcc_base();
990
991 if (gate->set_clr != 0U) {
992 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
993 } else {
994 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
995 }
996
997 VERBOSE("Clock %d has been enabled", gate->index);
998}
999
1000static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1001{
1002 uintptr_t rcc_base = stm32mp_rcc_base();
1003
1004 if (gate->set_clr != 0U) {
1005 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1006 BIT(gate->bit));
1007 } else {
1008 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1009 }
1010
1011 VERBOSE("Clock %d has been disabled", gate->index);
1012}
1013
1014static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1015{
1016 uintptr_t rcc_base = stm32mp_rcc_base();
1017
1018 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1019}
1020
1021unsigned int stm32mp1_clk_get_refcount(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001022{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001023 int i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001024
1025 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001026 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001027 }
1028
Yann Gautiere4a3c352019-02-14 10:53:33 +01001029 return gate_refcounts[i];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001030}
1031
Yann Gautiere4a3c352019-02-14 10:53:33 +01001032void __stm32mp1_clk_enable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001033{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001034 const struct stm32mp1_clk_gate *gate;
1035 int i = stm32mp1_clk_get_gated_id(id);
1036 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001037
1038 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001039 ERROR("Clock %d can't be enabled\n", (uint32_t)id);
1040 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001041 }
1042
Yann Gautiere4a3c352019-02-14 10:53:33 +01001043 gate = gate_ref(i);
1044 refcnt = &gate_refcounts[i];
1045
1046 stm32mp1_clk_lock(&refcount_lock);
1047
1048 if (stm32mp_incr_shrefcnt(refcnt, secure) != 0) {
1049 __clk_enable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001050 }
1051
Yann Gautiere4a3c352019-02-14 10:53:33 +01001052 stm32mp1_clk_unlock(&refcount_lock);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001053}
1054
Yann Gautiere4a3c352019-02-14 10:53:33 +01001055void __stm32mp1_clk_disable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001056{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001057 const struct stm32mp1_clk_gate *gate;
1058 int i = stm32mp1_clk_get_gated_id(id);
1059 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001060
1061 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001062 ERROR("Clock %d can't be disabled\n", (uint32_t)id);
1063 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001064 }
1065
Yann Gautiere4a3c352019-02-14 10:53:33 +01001066 gate = gate_ref(i);
1067 refcnt = &gate_refcounts[i];
1068
1069 stm32mp1_clk_lock(&refcount_lock);
1070
1071 if (stm32mp_decr_shrefcnt(refcnt, secure) != 0) {
1072 __clk_disable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001073 }
1074
Yann Gautiere4a3c352019-02-14 10:53:33 +01001075 stm32mp1_clk_unlock(&refcount_lock);
1076}
1077
1078void stm32mp_clk_enable(unsigned long id)
1079{
1080 __stm32mp1_clk_enable(id, true);
1081}
1082
1083void stm32mp_clk_disable(unsigned long id)
1084{
1085 __stm32mp1_clk_disable(id, true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001086}
1087
Yann Gautiere4a3c352019-02-14 10:53:33 +01001088bool stm32mp_clk_is_enabled(unsigned long id)
1089{
1090 int i = stm32mp1_clk_get_gated_id(id);
1091
1092 if (i < 0) {
1093 panic();
1094 }
1095
1096 return __clk_is_enabled(gate_ref(i));
1097}
1098
Yann Gautiera2e2a302019-02-14 11:13:39 +01001099unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001100{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001101 int p = stm32mp1_clk_get_parent(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001102
1103 if (p < 0) {
1104 return 0;
1105 }
1106
Yann Gautiere4a3c352019-02-14 10:53:33 +01001107 return get_clock_rate(p);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001108}
1109
Yann Gautiere4a3c352019-02-14 10:53:33 +01001110static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001111{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001112 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001113
Yann Gautiere4a3c352019-02-14 10:53:33 +01001114 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001115 mmio_setbits_32(address, mask_on);
1116 } else {
1117 mmio_clrbits_32(address, mask_on);
1118 }
1119}
1120
Yann Gautiere4a3c352019-02-14 10:53:33 +01001121static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001122{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001123 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1124 uintptr_t address = stm32mp_rcc_base() + offset;
1125
1126 mmio_write_32(address, mask_on);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001127}
1128
Yann Gautiere4a3c352019-02-14 10:53:33 +01001129static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001130{
Yann Gautier2299d572019-02-14 11:14:39 +01001131 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001132 uint32_t mask_test;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001133 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001134
Yann Gautiere4a3c352019-02-14 10:53:33 +01001135 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001136 mask_test = mask_rdy;
1137 } else {
1138 mask_test = 0;
1139 }
1140
Yann Gautier2299d572019-02-14 11:14:39 +01001141 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001142 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautier2299d572019-02-14 11:14:39 +01001143 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001144 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001145 mask_rdy, address, enable, mmio_read_32(address));
1146 return -ETIMEDOUT;
1147 }
1148 }
1149
1150 return 0;
1151}
1152
Yann Gautiere4a3c352019-02-14 10:53:33 +01001153static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001154{
1155 uint32_t value;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001156 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001157
Yann Gautiere4a3c352019-02-14 10:53:33 +01001158 if (digbyp) {
1159 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001160 }
1161
Yann Gautiere4a3c352019-02-14 10:53:33 +01001162 if (bypass || digbyp) {
1163 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1164 }
1165
Yann Gautier9aea69e2018-07-24 17:13:36 +02001166 /*
1167 * Warning: not recommended to switch directly from "high drive"
1168 * to "medium low drive", and vice-versa.
1169 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001170 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier9aea69e2018-07-24 17:13:36 +02001171 RCC_BDCR_LSEDRV_SHIFT;
1172
1173 while (value != lsedrv) {
1174 if (value > lsedrv) {
1175 value--;
1176 } else {
1177 value++;
1178 }
1179
Yann Gautiere4a3c352019-02-14 10:53:33 +01001180 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001181 RCC_BDCR_LSEDRV_MASK,
1182 value << RCC_BDCR_LSEDRV_SHIFT);
1183 }
1184
Yann Gautiere4a3c352019-02-14 10:53:33 +01001185 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001186}
1187
Yann Gautiere4a3c352019-02-14 10:53:33 +01001188static void stm32mp1_lse_wait(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001189{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001190 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001191 VERBOSE("%s: failed\n", __func__);
1192 }
1193}
1194
Yann Gautiere4a3c352019-02-14 10:53:33 +01001195static void stm32mp1_lsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001196{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001197 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1198
1199 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001200 VERBOSE("%s: failed\n", __func__);
1201 }
1202}
1203
Yann Gautiere4a3c352019-02-14 10:53:33 +01001204static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001205{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001206 uintptr_t rcc_base = stm32mp_rcc_base();
1207
1208 if (digbyp) {
1209 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001210 }
1211
Yann Gautiere4a3c352019-02-14 10:53:33 +01001212 if (bypass || digbyp) {
1213 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1214 }
1215
1216 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1217 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001218 VERBOSE("%s: failed\n", __func__);
1219 }
1220
1221 if (css) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001222 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001223 }
1224}
1225
Yann Gautiere4a3c352019-02-14 10:53:33 +01001226static void stm32mp1_csi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001227{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001228 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1229 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001230 VERBOSE("%s: failed\n", __func__);
1231 }
1232}
1233
Yann Gautiere4a3c352019-02-14 10:53:33 +01001234static void stm32mp1_hsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001235{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001236 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1237 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001238 VERBOSE("%s: failed\n", __func__);
1239 }
1240}
1241
Yann Gautiere4a3c352019-02-14 10:53:33 +01001242static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001243{
Yann Gautier2299d572019-02-14 11:14:39 +01001244 uint64_t timeout;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001245 uintptr_t rcc_base = stm32mp_rcc_base();
1246 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001247
Yann Gautiere4a3c352019-02-14 10:53:33 +01001248 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001249 RCC_HSICFGR_HSIDIV_MASK,
1250 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1251
Yann Gautier2299d572019-02-14 11:14:39 +01001252 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001253 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001254 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001255 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001256 address, mmio_read_32(address));
1257 return -ETIMEDOUT;
1258 }
1259 }
1260
1261 return 0;
1262}
1263
Yann Gautiere4a3c352019-02-14 10:53:33 +01001264static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001265{
1266 uint8_t hsidiv;
1267 uint32_t hsidivfreq = MAX_HSI_HZ;
1268
1269 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1270 if (hsidivfreq == hsifreq) {
1271 break;
1272 }
1273
1274 hsidivfreq /= 2U;
1275 }
1276
1277 if (hsidiv == 4U) {
1278 ERROR("Invalid clk-hsi frequency\n");
1279 return -1;
1280 }
1281
1282 if (hsidiv != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001283 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001284 }
1285
1286 return 0;
1287}
1288
Yann Gautiere4a3c352019-02-14 10:53:33 +01001289static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1290 unsigned int clksrc,
1291 uint32_t *pllcfg, int plloff)
1292{
1293 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1294 uintptr_t rcc_base = stm32mp_rcc_base();
1295 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1296 enum stm32mp1_plltype type = pll->plltype;
1297 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1298 unsigned long refclk;
1299 uint32_t ifrge = 0U;
Andre Przywara2d5690c2020-03-26 11:50:33 +00001300 uint32_t src, value, fracv = 0;
1301 void *fdt;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001302
1303 /* Check PLL output */
1304 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1305 return false;
1306 }
1307
1308 /* Check current clksrc */
1309 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1310 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1311 return false;
1312 }
1313
1314 /* Check Div */
1315 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1316
1317 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1318 (pllcfg[PLLCFG_M] + 1U);
1319
1320 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1321 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1322 return false;
1323 }
1324
1325 if ((type == PLL_800) && (refclk >= 8000000U)) {
1326 ifrge = 1U;
1327 }
1328
1329 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1330 RCC_PLLNCFGR1_DIVN_MASK;
1331 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1332 RCC_PLLNCFGR1_DIVM_MASK;
1333 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1334 RCC_PLLNCFGR1_IFRGE_MASK;
1335 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1336 return false;
1337 }
1338
1339 /* Fractional configuration */
Andre Przywara2d5690c2020-03-26 11:50:33 +00001340 if (fdt_get_address(&fdt) == 1) {
1341 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1342 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001343
1344 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1345 value |= RCC_PLLNFRACR_FRACLE;
1346 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1347 return false;
1348 }
1349
1350 /* Output config */
1351 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1352 RCC_PLLNCFGR2_DIVP_MASK;
1353 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1354 RCC_PLLNCFGR2_DIVQ_MASK;
1355 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1356 RCC_PLLNCFGR2_DIVR_MASK;
1357 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1358 return false;
1359 }
1360
1361 return true;
1362}
1363
1364static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001365{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001366 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1367 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001368
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001369 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1370 mmio_clrsetbits_32(pllxcr,
1371 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1372 RCC_PLLNCR_DIVREN,
1373 RCC_PLLNCR_PLLON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001374}
1375
Yann Gautiere4a3c352019-02-14 10:53:33 +01001376static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001377{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001378 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1379 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001380 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001381
Yann Gautier9aea69e2018-07-24 17:13:36 +02001382 /* Wait PLL lock */
1383 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001384 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001385 ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001386 pll_id, pllxcr, mmio_read_32(pllxcr));
1387 return -ETIMEDOUT;
1388 }
1389 }
1390
1391 /* Start the requested output */
1392 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1393
1394 return 0;
1395}
1396
Yann Gautiere4a3c352019-02-14 10:53:33 +01001397static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001398{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001399 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1400 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001401 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001402
1403 /* Stop all output */
1404 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1405 RCC_PLLNCR_DIVREN);
1406
1407 /* Stop PLL */
1408 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1409
Yann Gautier2299d572019-02-14 11:14:39 +01001410 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001411 /* Wait PLL stopped */
1412 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001413 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001414 ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001415 pll_id, pllxcr, mmio_read_32(pllxcr));
1416 return -ETIMEDOUT;
1417 }
1418 }
1419
1420 return 0;
1421}
1422
Yann Gautiere4a3c352019-02-14 10:53:33 +01001423static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001424 uint32_t *pllcfg)
1425{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001426 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1427 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001428 uint32_t value;
1429
1430 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1431 RCC_PLLNCFGR2_DIVP_MASK;
1432 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1433 RCC_PLLNCFGR2_DIVQ_MASK;
1434 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1435 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001436 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001437}
1438
Yann Gautiere4a3c352019-02-14 10:53:33 +01001439static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001440 uint32_t *pllcfg, uint32_t fracv)
1441{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001442 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1443 uintptr_t rcc_base = stm32mp_rcc_base();
1444 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001445 unsigned long refclk;
1446 uint32_t ifrge = 0;
1447 uint32_t src, value;
1448
Yann Gautiere4a3c352019-02-14 10:53:33 +01001449 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier9aea69e2018-07-24 17:13:36 +02001450 RCC_SELR_REFCLK_SRC_MASK;
1451
Yann Gautiere4a3c352019-02-14 10:53:33 +01001452 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier9aea69e2018-07-24 17:13:36 +02001453 (pllcfg[PLLCFG_M] + 1U);
1454
1455 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1456 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1457 return -EINVAL;
1458 }
1459
1460 if ((type == PLL_800) && (refclk >= 8000000U)) {
1461 ifrge = 1U;
1462 }
1463
1464 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1465 RCC_PLLNCFGR1_DIVN_MASK;
1466 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1467 RCC_PLLNCFGR1_DIVM_MASK;
1468 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1469 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001470 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001471
1472 /* Fractional configuration */
1473 value = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001474 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001475
1476 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001477 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001478
1479 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001480 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001481
Yann Gautiere4a3c352019-02-14 10:53:33 +01001482 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001483
1484 return 0;
1485}
1486
Yann Gautiere4a3c352019-02-14 10:53:33 +01001487static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001488{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001489 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001490 uint32_t pllxcsg = 0;
1491
1492 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1493 RCC_PLLNCSGR_MOD_PER_MASK;
1494
1495 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1496 RCC_PLLNCSGR_INC_STEP_MASK;
1497
1498 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1499 RCC_PLLNCSGR_SSCG_MODE_MASK;
1500
Yann Gautiere4a3c352019-02-14 10:53:33 +01001501 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001502
1503 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1504 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001505}
1506
Yann Gautiere4a3c352019-02-14 10:53:33 +01001507static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001508{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001509 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier2299d572019-02-14 11:14:39 +01001510 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001511
Yann Gautiere4a3c352019-02-14 10:53:33 +01001512 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001513 clksrc & RCC_SELR_SRC_MASK);
1514
Yann Gautier2299d572019-02-14 11:14:39 +01001515 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001516 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001517 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001518 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1519 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001520 return -ETIMEDOUT;
1521 }
1522 }
1523
1524 return 0;
1525}
1526
Yann Gautiere4a3c352019-02-14 10:53:33 +01001527static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001528{
Yann Gautier2299d572019-02-14 11:14:39 +01001529 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001530
1531 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1532 clkdiv & RCC_DIVR_DIV_MASK);
1533
Yann Gautier2299d572019-02-14 11:14:39 +01001534 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001535 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001536 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001537 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001538 clkdiv, address, mmio_read_32(address));
1539 return -ETIMEDOUT;
1540 }
1541 }
1542
1543 return 0;
1544}
1545
Yann Gautiere4a3c352019-02-14 10:53:33 +01001546static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001547{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001548 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001549
1550 /*
1551 * Binding clksrc :
1552 * bit15-4 offset
1553 * bit3: disable
1554 * bit2-0: MCOSEL[2:0]
1555 */
1556 if ((clksrc & 0x8U) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001557 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001558 } else {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001559 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001560 RCC_MCOCFG_MCOSRC_MASK,
1561 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001562 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001563 RCC_MCOCFG_MCODIV_MASK,
1564 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001565 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001566 }
1567}
1568
Yann Gautiere4a3c352019-02-14 10:53:33 +01001569static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001570{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001571 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001572
1573 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1574 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1575 mmio_clrsetbits_32(address,
1576 RCC_BDCR_RTCSRC_MASK,
1577 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1578
1579 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1580 }
1581
1582 if (lse_css) {
1583 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1584 }
1585}
1586
Yann Gautiere4a3c352019-02-14 10:53:33 +01001587static void stm32mp1_stgen_config(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001588{
1589 uintptr_t stgen;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001590 uint32_t cntfid0;
1591 unsigned long rate;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001592 unsigned long long counter;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001593
1594 stgen = fdt_get_stgen_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001595 cntfid0 = mmio_read_32(stgen + CNTFID_OFF);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001596 rate = get_clock_rate(stm32mp1_clk_get_parent(STGEN_K));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001597
Yann Gautiere4a3c352019-02-14 10:53:33 +01001598 if (cntfid0 == rate) {
1599 return;
1600 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001601
Yann Gautiere4a3c352019-02-14 10:53:33 +01001602 mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1603 counter = (unsigned long long)mmio_read_32(stgen + CNTCVL_OFF);
1604 counter |= ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF)) << 32;
1605 counter = (counter * rate / cntfid0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001606
Yann Gautiere4a3c352019-02-14 10:53:33 +01001607 mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)counter);
1608 mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(counter >> 32));
1609 mmio_write_32(stgen + CNTFID_OFF, rate);
1610 mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001611
Yann Gautiere4a3c352019-02-14 10:53:33 +01001612 write_cntfrq((u_register_t)rate);
1613
1614 /* Need to update timer with new frequency */
1615 generic_delay_timer_init();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001616}
1617
1618void stm32mp1_stgen_increment(unsigned long long offset_in_ms)
1619{
1620 uintptr_t stgen;
1621 unsigned long long cnt;
1622
1623 stgen = fdt_get_stgen_base();
1624
1625 cnt = ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF) << 32) |
1626 mmio_read_32(stgen + CNTCVL_OFF);
1627
1628 cnt += (offset_in_ms * mmio_read_32(stgen + CNTFID_OFF)) / 1000U;
1629
1630 mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1631 mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)cnt);
1632 mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(cnt >> 32));
1633 mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1634}
1635
Yann Gautiere4a3c352019-02-14 10:53:33 +01001636static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001637{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001638 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001639 uint32_t value = pkcs & 0xFU;
1640 uint32_t mask = 0xFU;
1641
1642 if ((pkcs & BIT(31)) != 0U) {
1643 mask <<= 4;
1644 value <<= 4;
1645 }
1646
1647 mmio_clrsetbits_32(address, mask, value);
1648}
1649
1650int stm32mp1_clk_init(void)
1651{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001652 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001653 unsigned int clksrc[CLKSRC_NB];
1654 unsigned int clkdiv[CLKDIV_NB];
1655 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1656 int plloff[_PLL_NB];
1657 int ret, len;
1658 enum stm32mp1_pll_id i;
1659 bool lse_css = false;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001660 bool pll3_preserve = false;
1661 bool pll4_preserve = false;
1662 bool pll4_bootrom = false;
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001663 const fdt32_t *pkcs_cell;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001664 void *fdt;
1665
1666 if (fdt_get_address(&fdt) == 0) {
1667 return false;
1668 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001669
1670 /* Check status field to disable security */
1671 if (!fdt_get_rcc_secure_status()) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001672 mmio_write_32(rcc_base + RCC_TZCR, 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001673 }
1674
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001675 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1676 clksrc);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001677 if (ret < 0) {
1678 return -FDT_ERR_NOTFOUND;
1679 }
1680
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001681 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1682 clkdiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001683 if (ret < 0) {
1684 return -FDT_ERR_NOTFOUND;
1685 }
1686
1687 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1688 char name[12];
1689
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01001690 snprintf(name, sizeof(name), "st,pll@%d", i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001691 plloff[i] = fdt_rcc_subnode_offset(name);
1692
1693 if (!fdt_check_node(plloff[i])) {
1694 continue;
1695 }
1696
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001697 ret = fdt_read_uint32_array(fdt, plloff[i], "cfg",
1698 (int)PLLCFG_NB, pllcfg[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001699 if (ret < 0) {
1700 return -FDT_ERR_NOTFOUND;
1701 }
1702 }
1703
Yann Gautiere4a3c352019-02-14 10:53:33 +01001704 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1705 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001706
1707 /*
1708 * Switch ON oscillator found in device-tree.
1709 * Note: HSI already ON after BootROM stage.
1710 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001711 if (stm32mp1_osc[_LSI] != 0U) {
1712 stm32mp1_lsi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001713 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001714 if (stm32mp1_osc[_LSE] != 0U) {
1715 bool bypass, digbyp;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001716 uint32_t lsedrv;
1717
1718 bypass = fdt_osc_read_bool(_LSE, "st,bypass");
Yann Gautiere4a3c352019-02-14 10:53:33 +01001719 digbyp = fdt_osc_read_bool(_LSE, "st,digbypass");
Yann Gautier9aea69e2018-07-24 17:13:36 +02001720 lse_css = fdt_osc_read_bool(_LSE, "st,css");
1721 lsedrv = fdt_osc_read_uint32_default(_LSE, "st,drive",
1722 LSEDRV_MEDIUM_HIGH);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001723 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001724 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001725 if (stm32mp1_osc[_HSE] != 0U) {
1726 bool bypass, digbyp, css;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001727
Yann Gautiere4a3c352019-02-14 10:53:33 +01001728 bypass = fdt_osc_read_bool(_HSE, "st,bypass");
1729 digbyp = fdt_osc_read_bool(_HSE, "st,digbypass");
1730 css = fdt_osc_read_bool(_HSE, "st,css");
1731 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001732 }
1733 /*
1734 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1735 * => switch on CSI even if node is not present in device tree
1736 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001737 stm32mp1_csi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001738
1739 /* Come back to HSI */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001740 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001741 if (ret != 0) {
1742 return ret;
1743 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001744 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001745 if (ret != 0) {
1746 return ret;
1747 }
Yann Gautiered342322019-02-15 17:33:27 +01001748 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1749 if (ret != 0) {
1750 return ret;
1751 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001752
Yann Gautiere4a3c352019-02-14 10:53:33 +01001753 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1754 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
1755 pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
1756 clksrc[CLKSRC_PLL3],
1757 pllcfg[_PLL3],
1758 plloff[_PLL3]);
1759 pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
1760 clksrc[CLKSRC_PLL4],
1761 pllcfg[_PLL4],
1762 plloff[_PLL4]);
1763 }
1764
Yann Gautier9aea69e2018-07-24 17:13:36 +02001765 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001766 if (((i == _PLL3) && pll3_preserve) ||
1767 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001768 continue;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001769 }
1770
1771 ret = stm32mp1_pll_stop(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001772 if (ret != 0) {
1773 return ret;
1774 }
1775 }
1776
1777 /* Configure HSIDIV */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001778 if (stm32mp1_osc[_HSI] != 0U) {
1779 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001780 if (ret != 0) {
1781 return ret;
1782 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001783 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001784 }
1785
1786 /* Select DIV */
1787 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001788 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001789 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001790 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001791 if (ret != 0) {
1792 return ret;
1793 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001794 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001795 if (ret != 0) {
1796 return ret;
1797 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001798 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001799 if (ret != 0) {
1800 return ret;
1801 }
Yann Gautiered342322019-02-15 17:33:27 +01001802 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
1803 if (ret != 0) {
1804 return ret;
1805 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001806 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001807 if (ret != 0) {
1808 return ret;
1809 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001810 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001811 if (ret != 0) {
1812 return ret;
1813 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001814 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001815 if (ret != 0) {
1816 return ret;
1817 }
1818
1819 /* No ready bit for RTC */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001820 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001821 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
1822
1823 /* Configure PLLs source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001824 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001825 if (ret != 0) {
1826 return ret;
1827 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001828
1829 if (!pll3_preserve) {
1830 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
1831 if (ret != 0) {
1832 return ret;
1833 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001834 }
1835
Yann Gautiere4a3c352019-02-14 10:53:33 +01001836 if (!pll4_preserve) {
1837 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
1838 if (ret != 0) {
1839 return ret;
1840 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001841 }
1842
1843 /* Configure and start PLLs */
1844 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1845 uint32_t fracv;
1846 uint32_t csg[PLLCSG_NB];
1847
Yann Gautiere4a3c352019-02-14 10:53:33 +01001848 if (((i == _PLL3) && pll3_preserve) ||
1849 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
1850 continue;
1851 }
1852
Yann Gautier9aea69e2018-07-24 17:13:36 +02001853 if (!fdt_check_node(plloff[i])) {
1854 continue;
1855 }
1856
Yann Gautiere4a3c352019-02-14 10:53:33 +01001857 if ((i == _PLL4) && pll4_bootrom) {
1858 /* Set output divider if not done by the Bootrom */
1859 stm32mp1_pll_config_output(i, pllcfg[i]);
1860 continue;
1861 }
1862
Andre Przywara2d5690c2020-03-26 11:50:33 +00001863 fracv = fdt_read_uint32_default(fdt, plloff[i], "frac", 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001864
Yann Gautiere4a3c352019-02-14 10:53:33 +01001865 ret = stm32mp1_pll_config(i, pllcfg[i], fracv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001866 if (ret != 0) {
1867 return ret;
1868 }
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001869 ret = fdt_read_uint32_array(fdt, plloff[i], "csg",
1870 (uint32_t)PLLCSG_NB, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001871 if (ret == 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001872 stm32mp1_pll_csg(i, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001873 } else if (ret != -FDT_ERR_NOTFOUND) {
1874 return ret;
1875 }
1876
Yann Gautiere4a3c352019-02-14 10:53:33 +01001877 stm32mp1_pll_start(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001878 }
1879 /* Wait and start PLLs ouptut when ready */
1880 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1881 if (!fdt_check_node(plloff[i])) {
1882 continue;
1883 }
1884
Yann Gautiere4a3c352019-02-14 10:53:33 +01001885 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001886 if (ret != 0) {
1887 return ret;
1888 }
1889 }
1890 /* Wait LSE ready before to use it */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001891 if (stm32mp1_osc[_LSE] != 0U) {
1892 stm32mp1_lse_wait();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001893 }
1894
1895 /* Configure with expected clock source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001896 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001897 if (ret != 0) {
1898 return ret;
1899 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001900 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001901 if (ret != 0) {
1902 return ret;
1903 }
Yann Gautiered342322019-02-15 17:33:27 +01001904 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
1905 if (ret != 0) {
1906 return ret;
1907 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001908 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001909
1910 /* Configure PKCK */
1911 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
1912 if (pkcs_cell != NULL) {
1913 bool ckper_disabled = false;
1914 uint32_t j;
1915
Yann Gautier9aea69e2018-07-24 17:13:36 +02001916 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001917 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001918
1919 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
1920 ckper_disabled = true;
1921 continue;
1922 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001923 stm32mp1_pkcs_config(pkcs);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001924 }
1925
1926 /*
1927 * CKPER is source for some peripheral clocks
1928 * (FMC-NAND / QPSI-NOR) and switching source is allowed
1929 * only if previous clock is still ON
1930 * => deactivated CKPER only after switching clock
1931 */
1932 if (ckper_disabled) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001933 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001934 }
1935 }
1936
1937 /* Switch OFF HSI if not found in device-tree */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001938 if (stm32mp1_osc[_HSI] == 0U) {
1939 stm32mp1_hsi_set(false);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001940 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001941 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001942
1943 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001944 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001945 RCC_DDRITFCR_DDRCKMOD_MASK,
1946 RCC_DDRITFCR_DDRCKMOD_SSR <<
1947 RCC_DDRITFCR_DDRCKMOD_SHIFT);
1948
1949 return 0;
1950}
1951
1952static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001953 enum stm32mp_osc_id index)
1954{
1955 uint32_t frequency;
1956
Yann Gautiere4a3c352019-02-14 10:53:33 +01001957 if (fdt_osc_read_freq(name, &frequency) == 0) {
1958 stm32mp1_osc[index] = frequency;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001959 }
1960}
1961
1962static void stm32mp1_osc_init(void)
1963{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001964 enum stm32mp_osc_id i;
1965
1966 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001967 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001968 }
1969}
1970
Yann Gautierc7f9e962019-05-20 14:39:26 +02001971static void sync_earlyboot_clocks_state(void)
1972{
1973 if (!stm32mp_is_single_core()) {
1974 stm32mp1_clk_enable_secure(RTCAPB);
1975 }
1976}
1977
Yann Gautier9aea69e2018-07-24 17:13:36 +02001978int stm32mp1_clk_probe(void)
1979{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001980 stm32mp1_osc_init();
1981
Yann Gautierc7f9e962019-05-20 14:39:26 +02001982 sync_earlyboot_clocks_state();
1983
Yann Gautier9aea69e2018-07-24 17:13:36 +02001984 return 0;
1985}