blob: 6ada96a520ca3f2d3f41b7ba2801c042c559b93c [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Yann Gautier360e0e92020-09-16 16:40:34 +02002 * Copyright (C) 2018-2021, 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
Etienne Carriere1368ada2020-05-13 11:49:49 +0200580static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = {
581 [_HSI] = "HSI",
582 [_HSE] = "HSE",
583 [_CSI] = "CSI",
584 [_LSI] = "LSI",
585 [_LSE] = "LSE",
586 [_I2S_CKIN] = "I2S_CKIN",
587 [_HSI_KER] = "HSI_KER",
588 [_HSE_KER] = "HSE_KER",
589 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
590 [_CSI_KER] = "CSI_KER",
591 [_PLL1_P] = "PLL1_P",
592 [_PLL1_Q] = "PLL1_Q",
593 [_PLL1_R] = "PLL1_R",
594 [_PLL2_P] = "PLL2_P",
595 [_PLL2_Q] = "PLL2_Q",
596 [_PLL2_R] = "PLL2_R",
597 [_PLL3_P] = "PLL3_P",
598 [_PLL3_Q] = "PLL3_Q",
599 [_PLL3_R] = "PLL3_R",
600 [_PLL4_P] = "PLL4_P",
601 [_PLL4_Q] = "PLL4_Q",
602 [_PLL4_R] = "PLL4_R",
603 [_ACLK] = "ACLK",
604 [_PCLK1] = "PCLK1",
605 [_PCLK2] = "PCLK2",
606 [_PCLK3] = "PCLK3",
607 [_PCLK4] = "PCLK4",
608 [_PCLK5] = "PCLK5",
609 [_HCLK6] = "KCLK6",
610 [_HCLK2] = "HCLK2",
611 [_CK_PER] = "CK_PER",
612 [_CK_MPU] = "CK_MPU",
613 [_CK_MCU] = "CK_MCU",
614 [_USB_PHY_48] = "USB_PHY_48",
615};
616
Yann Gautiere4a3c352019-02-14 10:53:33 +0100617/* RCC clock device driver private */
618static unsigned long stm32mp1_osc[NB_OSC];
619static struct spinlock reg_lock;
620static unsigned int gate_refcounts[NB_GATES];
621static struct spinlock refcount_lock;
622
623static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
624{
625 return &stm32mp1_clk_gate[idx];
626}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200627
Yann Gautiere4a3c352019-02-14 10:53:33 +0100628static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
629{
630 return &stm32mp1_clk_sel[idx];
631}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200632
Yann Gautiere4a3c352019-02-14 10:53:33 +0100633static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
634{
635 return &stm32mp1_clk_pll[idx];
636}
637
Yann Gautiere4a3c352019-02-14 10:53:33 +0100638static void stm32mp1_clk_lock(struct spinlock *lock)
639{
Yann Gautierf540a592019-05-22 19:13:51 +0200640 if (stm32mp_lock_available()) {
641 /* Assume interrupts are masked */
642 spin_lock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100643 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100644}
645
646static void stm32mp1_clk_unlock(struct spinlock *lock)
647{
Yann Gautierf540a592019-05-22 19:13:51 +0200648 if (stm32mp_lock_available()) {
649 spin_unlock(lock);
Yann Gautiere4a3c352019-02-14 10:53:33 +0100650 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100651}
652
653bool stm32mp1_rcc_is_secure(void)
654{
655 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100656 uint32_t mask = RCC_TZCR_TZEN;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100657
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100658 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100659}
660
Yann Gautiered342322019-02-15 17:33:27 +0100661bool stm32mp1_rcc_is_mckprot(void)
662{
663 uintptr_t rcc_base = stm32mp_rcc_base();
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100664 uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT;
Yann Gautiered342322019-02-15 17:33:27 +0100665
Etienne Carriere5e68f6b2020-02-05 10:03:27 +0100666 return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask;
Yann Gautiered342322019-02-15 17:33:27 +0100667}
668
Yann Gautiere4a3c352019-02-14 10:53:33 +0100669void stm32mp1_clk_rcc_regs_lock(void)
670{
671 stm32mp1_clk_lock(&reg_lock);
672}
673
674void stm32mp1_clk_rcc_regs_unlock(void)
675{
676 stm32mp1_clk_unlock(&reg_lock);
677}
678
679static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200680{
681 if (idx >= NB_OSC) {
682 return 0;
683 }
684
Yann Gautiere4a3c352019-02-14 10:53:33 +0100685 return stm32mp1_osc[idx];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200686}
687
Yann Gautiere4a3c352019-02-14 10:53:33 +0100688static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200689{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100690 unsigned int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200691
Yann Gautiere4a3c352019-02-14 10:53:33 +0100692 for (i = 0U; i < NB_GATES; i++) {
693 if (gate_ref(i)->index == id) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200694 return i;
695 }
696 }
697
698 ERROR("%s: clk id %d not found\n", __func__, (uint32_t)id);
699
700 return -EINVAL;
701}
702
Yann Gautiere4a3c352019-02-14 10:53:33 +0100703static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200704{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100705 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200706}
707
Yann Gautiere4a3c352019-02-14 10:53:33 +0100708static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200709{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100710 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200711}
712
Yann Gautiere4a3c352019-02-14 10:53:33 +0100713static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200714{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100715 const struct stm32mp1_clk_sel *sel;
Etienne Carriere04132612019-12-08 08:20:12 +0100716 uint32_t p_sel;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200717 int i;
718 enum stm32mp1_parent_id p;
719 enum stm32mp1_parent_sel s;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100720 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200721
Etienne Carriere04132612019-12-08 08:20:12 +0100722 /* Few non gateable clock have a static parent ID, find them */
723 i = (int)clock_id2parent_id(id);
724 if (i != _UNKNOWN_ID) {
725 return i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200726 }
727
Yann Gautiere4a3c352019-02-14 10:53:33 +0100728 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200729 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100730 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200731 }
732
Yann Gautiere4a3c352019-02-14 10:53:33 +0100733 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200734 if (p < _PARENT_NB) {
735 return (int)p;
736 }
737
Yann Gautiere4a3c352019-02-14 10:53:33 +0100738 s = stm32mp1_clk_get_sel(i);
739 if (s == _UNKNOWN_SEL) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200740 return -EINVAL;
741 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100742 if (s >= _PARENT_SEL_NB) {
743 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200744 }
745
Yann Gautiere4a3c352019-02-14 10:53:33 +0100746 sel = clk_sel_ref(s);
Etienne Carrierec164ce22019-12-08 08:20:40 +0100747 p_sel = (mmio_read_32(rcc_base + sel->offset) &
748 (sel->msk << sel->src)) >> sel->src;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100749 if (p_sel < sel->nb_parent) {
750 return (int)sel->parent[p_sel];
751 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200752
753 return -EINVAL;
754}
755
Yann Gautiere4a3c352019-02-14 10:53:33 +0100756static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200757{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100758 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
759 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200760
Yann Gautiere4a3c352019-02-14 10:53:33 +0100761 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200762}
763
764/*
765 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
766 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
767 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
768 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
769 */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100770static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200771{
Yann Gautier9aea69e2018-07-24 17:13:36 +0200772 unsigned long refclk, fvco;
773 uint32_t cfgr1, fracr, divm, divn;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100774 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200775
Yann Gautiere4a3c352019-02-14 10:53:33 +0100776 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
777 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200778
779 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
780 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
781
Yann Gautiere4a3c352019-02-14 10:53:33 +0100782 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200783
784 /*
785 * With FRACV :
786 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
787 * Without FRACV
788 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
789 */
790 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100791 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
792 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200793 unsigned long long numerator, denominator;
794
Yann Gautiere4a3c352019-02-14 10:53:33 +0100795 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
796 numerator = refclk * numerator;
797 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200798 fvco = (unsigned long)(numerator / denominator);
799 } else {
800 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
801 }
802
803 return fvco;
804}
805
Yann Gautiere4a3c352019-02-14 10:53:33 +0100806static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200807 enum stm32mp1_div_id div_id)
808{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100809 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200810 unsigned long dfout;
811 uint32_t cfgr2, divy;
812
813 if (div_id >= _DIV_NB) {
814 return 0;
815 }
816
Yann Gautiere4a3c352019-02-14 10:53:33 +0100817 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200818 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
819
Yann Gautiere4a3c352019-02-14 10:53:33 +0100820 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200821
822 return dfout;
823}
824
Yann Gautiere4a3c352019-02-14 10:53:33 +0100825static unsigned long get_clock_rate(int p)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200826{
827 uint32_t reg, clkdiv;
828 unsigned long clock = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100829 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200830
831 switch (p) {
832 case _CK_MPU:
833 /* MPU sub system */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100834 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200835 switch (reg & RCC_SELR_SRC_MASK) {
836 case RCC_MPCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100837 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200838 break;
839 case RCC_MPCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100840 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200841 break;
842 case RCC_MPCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100843 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200844 break;
845 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100846 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200847
Yann Gautiere4a3c352019-02-14 10:53:33 +0100848 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200849 clkdiv = reg & RCC_MPUDIV_MASK;
850 if (clkdiv != 0U) {
851 clock /= stm32mp1_mpu_div[clkdiv];
852 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200853 break;
854 default:
855 break;
856 }
857 break;
858 /* AXI sub system */
859 case _ACLK:
860 case _HCLK2:
861 case _HCLK6:
862 case _PCLK4:
863 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100864 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200865 switch (reg & RCC_SELR_SRC_MASK) {
866 case RCC_ASSCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100867 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200868 break;
869 case RCC_ASSCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100870 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200871 break;
872 case RCC_ASSCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100873 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200874 break;
875 default:
876 break;
877 }
878
879 /* System clock divider */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100880 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200881 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
882
883 switch (p) {
884 case _PCLK4:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100885 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200886 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
887 break;
888 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100889 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200890 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
891 break;
892 default:
893 break;
894 }
895 break;
Yann Gautiered342322019-02-15 17:33:27 +0100896 /* MCU sub system */
897 case _CK_MCU:
898 case _PCLK1:
899 case _PCLK2:
900 case _PCLK3:
901 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
902 switch (reg & RCC_SELR_SRC_MASK) {
903 case RCC_MSSCKSELR_HSI:
904 clock = stm32mp1_clk_get_fixed(_HSI);
905 break;
906 case RCC_MSSCKSELR_HSE:
907 clock = stm32mp1_clk_get_fixed(_HSE);
908 break;
909 case RCC_MSSCKSELR_CSI:
910 clock = stm32mp1_clk_get_fixed(_CSI);
911 break;
912 case RCC_MSSCKSELR_PLL:
913 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
914 break;
915 default:
916 break;
917 }
918
919 /* MCU clock divider */
920 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
921 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
922
923 switch (p) {
924 case _PCLK1:
925 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
926 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
927 break;
928 case _PCLK2:
929 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
930 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
931 break;
932 case _PCLK3:
933 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
934 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
935 break;
936 case _CK_MCU:
937 default:
938 break;
939 }
940 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200941 case _CK_PER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100942 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200943 switch (reg & RCC_SELR_SRC_MASK) {
944 case RCC_CPERCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100945 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200946 break;
947 case RCC_CPERCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100948 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200949 break;
950 case RCC_CPERCKSELR_CSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100951 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200952 break;
953 default:
954 break;
955 }
956 break;
957 case _HSI:
958 case _HSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100959 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200960 break;
961 case _CSI:
962 case _CSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100963 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200964 break;
965 case _HSE:
966 case _HSE_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100967 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200968 break;
969 case _HSE_KER_DIV2:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100970 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200971 break;
972 case _LSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100973 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200974 break;
975 case _LSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100976 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200977 break;
978 /* PLL */
979 case _PLL1_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100980 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200981 break;
982 case _PLL1_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100983 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200984 break;
985 case _PLL1_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100986 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200987 break;
988 case _PLL2_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100989 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200990 break;
991 case _PLL2_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100992 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200993 break;
994 case _PLL2_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100995 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200996 break;
997 case _PLL3_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100998 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200999 break;
1000 case _PLL3_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001001 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001002 break;
1003 case _PLL3_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001004 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001005 break;
1006 case _PLL4_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001007 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001008 break;
1009 case _PLL4_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001010 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001011 break;
1012 case _PLL4_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001013 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001014 break;
1015 /* Other */
1016 case _USB_PHY_48:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001017 clock = USB_PHY_48_MHZ;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001018 break;
1019 default:
1020 break;
1021 }
1022
1023 return clock;
1024}
1025
Yann Gautiere4a3c352019-02-14 10:53:33 +01001026static void __clk_enable(struct stm32mp1_clk_gate const *gate)
1027{
1028 uintptr_t rcc_base = stm32mp_rcc_base();
1029
Etienne Carriere8a668892019-12-08 08:21:08 +01001030 VERBOSE("Enable clock %u\n", gate->index);
1031
Yann Gautiere4a3c352019-02-14 10:53:33 +01001032 if (gate->set_clr != 0U) {
1033 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
1034 } else {
1035 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
1036 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001037}
1038
1039static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1040{
1041 uintptr_t rcc_base = stm32mp_rcc_base();
1042
Etienne Carriere8a668892019-12-08 08:21:08 +01001043 VERBOSE("Disable clock %u\n", gate->index);
1044
Yann Gautiere4a3c352019-02-14 10:53:33 +01001045 if (gate->set_clr != 0U) {
1046 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1047 BIT(gate->bit));
1048 } else {
1049 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1050 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001051}
1052
1053static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1054{
1055 uintptr_t rcc_base = stm32mp_rcc_base();
1056
1057 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1058}
1059
1060unsigned int stm32mp1_clk_get_refcount(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001061{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001062 int i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001063
1064 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001065 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001066 }
1067
Yann Gautiere4a3c352019-02-14 10:53:33 +01001068 return gate_refcounts[i];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001069}
1070
Etienne Carriere481aa002019-12-08 08:21:44 +01001071/* Oscillators and PLLs are not gated at runtime */
1072static bool clock_is_always_on(unsigned long id)
1073{
1074 switch (id) {
1075 case CK_HSE:
1076 case CK_CSI:
1077 case CK_LSI:
1078 case CK_LSE:
1079 case CK_HSI:
1080 case CK_HSE_DIV2:
1081 case PLL1_Q:
1082 case PLL1_R:
1083 case PLL2_P:
1084 case PLL2_Q:
1085 case PLL2_R:
1086 case PLL3_P:
1087 case PLL3_Q:
1088 case PLL3_R:
1089 return true;
1090 default:
1091 return false;
1092 }
1093}
1094
Yann Gautiere4a3c352019-02-14 10:53:33 +01001095void __stm32mp1_clk_enable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001096{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001097 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001098 int i;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001099 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001100
Etienne Carriere481aa002019-12-08 08:21:44 +01001101 if (clock_is_always_on(id)) {
1102 return;
1103 }
1104
1105 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001106 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001107 ERROR("Clock %d can't be enabled\n", (uint32_t)id);
1108 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001109 }
1110
Yann Gautiere4a3c352019-02-14 10:53:33 +01001111 gate = gate_ref(i);
1112 refcnt = &gate_refcounts[i];
1113
1114 stm32mp1_clk_lock(&refcount_lock);
1115
1116 if (stm32mp_incr_shrefcnt(refcnt, secure) != 0) {
1117 __clk_enable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001118 }
1119
Yann Gautiere4a3c352019-02-14 10:53:33 +01001120 stm32mp1_clk_unlock(&refcount_lock);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001121}
1122
Yann Gautiere4a3c352019-02-14 10:53:33 +01001123void __stm32mp1_clk_disable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001124{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001125 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001126 int i;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001127 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001128
Etienne Carriere481aa002019-12-08 08:21:44 +01001129 if (clock_is_always_on(id)) {
1130 return;
1131 }
1132
1133 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001134 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001135 ERROR("Clock %d can't be disabled\n", (uint32_t)id);
1136 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001137 }
1138
Yann Gautiere4a3c352019-02-14 10:53:33 +01001139 gate = gate_ref(i);
1140 refcnt = &gate_refcounts[i];
1141
1142 stm32mp1_clk_lock(&refcount_lock);
1143
1144 if (stm32mp_decr_shrefcnt(refcnt, secure) != 0) {
1145 __clk_disable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001146 }
1147
Yann Gautiere4a3c352019-02-14 10:53:33 +01001148 stm32mp1_clk_unlock(&refcount_lock);
1149}
1150
1151void stm32mp_clk_enable(unsigned long id)
1152{
1153 __stm32mp1_clk_enable(id, true);
1154}
1155
1156void stm32mp_clk_disable(unsigned long id)
1157{
1158 __stm32mp1_clk_disable(id, true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001159}
1160
Yann Gautiere4a3c352019-02-14 10:53:33 +01001161bool stm32mp_clk_is_enabled(unsigned long id)
1162{
Etienne Carriere481aa002019-12-08 08:21:44 +01001163 int i;
1164
1165 if (clock_is_always_on(id)) {
1166 return true;
1167 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001168
Etienne Carriere481aa002019-12-08 08:21:44 +01001169 i = stm32mp1_clk_get_gated_id(id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001170 if (i < 0) {
1171 panic();
1172 }
1173
1174 return __clk_is_enabled(gate_ref(i));
1175}
1176
Yann Gautiera2e2a302019-02-14 11:13:39 +01001177unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001178{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001179 int p = stm32mp1_clk_get_parent(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001180
1181 if (p < 0) {
1182 return 0;
1183 }
1184
Yann Gautiere4a3c352019-02-14 10:53:33 +01001185 return get_clock_rate(p);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001186}
1187
Yann Gautiere4a3c352019-02-14 10:53:33 +01001188static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001189{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001190 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001191
Yann Gautiere4a3c352019-02-14 10:53:33 +01001192 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001193 mmio_setbits_32(address, mask_on);
1194 } else {
1195 mmio_clrbits_32(address, mask_on);
1196 }
1197}
1198
Yann Gautiere4a3c352019-02-14 10:53:33 +01001199static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001200{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001201 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1202 uintptr_t address = stm32mp_rcc_base() + offset;
1203
1204 mmio_write_32(address, mask_on);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001205}
1206
Yann Gautiere4a3c352019-02-14 10:53:33 +01001207static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001208{
Yann Gautier2299d572019-02-14 11:14:39 +01001209 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001210 uint32_t mask_test;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001211 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001212
Yann Gautiere4a3c352019-02-14 10:53:33 +01001213 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001214 mask_test = mask_rdy;
1215 } else {
1216 mask_test = 0;
1217 }
1218
Yann Gautier2299d572019-02-14 11:14:39 +01001219 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001220 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautier2299d572019-02-14 11:14:39 +01001221 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001222 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001223 mask_rdy, address, enable, mmio_read_32(address));
1224 return -ETIMEDOUT;
1225 }
1226 }
1227
1228 return 0;
1229}
1230
Yann Gautiere4a3c352019-02-14 10:53:33 +01001231static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001232{
1233 uint32_t value;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001234 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001235
Yann Gautiere4a3c352019-02-14 10:53:33 +01001236 if (digbyp) {
1237 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001238 }
1239
Yann Gautiere4a3c352019-02-14 10:53:33 +01001240 if (bypass || digbyp) {
1241 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1242 }
1243
Yann Gautier9aea69e2018-07-24 17:13:36 +02001244 /*
1245 * Warning: not recommended to switch directly from "high drive"
1246 * to "medium low drive", and vice-versa.
1247 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001248 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier9aea69e2018-07-24 17:13:36 +02001249 RCC_BDCR_LSEDRV_SHIFT;
1250
1251 while (value != lsedrv) {
1252 if (value > lsedrv) {
1253 value--;
1254 } else {
1255 value++;
1256 }
1257
Yann Gautiere4a3c352019-02-14 10:53:33 +01001258 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001259 RCC_BDCR_LSEDRV_MASK,
1260 value << RCC_BDCR_LSEDRV_SHIFT);
1261 }
1262
Yann Gautiere4a3c352019-02-14 10:53:33 +01001263 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001264}
1265
Yann Gautiere4a3c352019-02-14 10:53:33 +01001266static void stm32mp1_lse_wait(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001267{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001268 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001269 VERBOSE("%s: failed\n", __func__);
1270 }
1271}
1272
Yann Gautiere4a3c352019-02-14 10:53:33 +01001273static void stm32mp1_lsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001274{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001275 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1276
1277 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001278 VERBOSE("%s: failed\n", __func__);
1279 }
1280}
1281
Yann Gautiere4a3c352019-02-14 10:53:33 +01001282static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001283{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001284 uintptr_t rcc_base = stm32mp_rcc_base();
1285
1286 if (digbyp) {
1287 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001288 }
1289
Yann Gautiere4a3c352019-02-14 10:53:33 +01001290 if (bypass || digbyp) {
1291 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1292 }
1293
1294 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1295 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001296 VERBOSE("%s: failed\n", __func__);
1297 }
1298
1299 if (css) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001300 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001301 }
1302}
1303
Yann Gautiere4a3c352019-02-14 10:53:33 +01001304static void stm32mp1_csi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001305{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001306 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1307 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001308 VERBOSE("%s: failed\n", __func__);
1309 }
1310}
1311
Yann Gautiere4a3c352019-02-14 10:53:33 +01001312static void stm32mp1_hsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001313{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001314 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1315 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001316 VERBOSE("%s: failed\n", __func__);
1317 }
1318}
1319
Yann Gautiere4a3c352019-02-14 10:53:33 +01001320static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001321{
Yann Gautier2299d572019-02-14 11:14:39 +01001322 uint64_t timeout;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001323 uintptr_t rcc_base = stm32mp_rcc_base();
1324 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001325
Yann Gautiere4a3c352019-02-14 10:53:33 +01001326 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001327 RCC_HSICFGR_HSIDIV_MASK,
1328 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1329
Yann Gautier2299d572019-02-14 11:14:39 +01001330 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001331 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001332 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001333 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001334 address, mmio_read_32(address));
1335 return -ETIMEDOUT;
1336 }
1337 }
1338
1339 return 0;
1340}
1341
Yann Gautiere4a3c352019-02-14 10:53:33 +01001342static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001343{
1344 uint8_t hsidiv;
1345 uint32_t hsidivfreq = MAX_HSI_HZ;
1346
1347 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1348 if (hsidivfreq == hsifreq) {
1349 break;
1350 }
1351
1352 hsidivfreq /= 2U;
1353 }
1354
1355 if (hsidiv == 4U) {
1356 ERROR("Invalid clk-hsi frequency\n");
1357 return -1;
1358 }
1359
1360 if (hsidiv != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001361 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001362 }
1363
1364 return 0;
1365}
1366
Yann Gautiere4a3c352019-02-14 10:53:33 +01001367static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1368 unsigned int clksrc,
1369 uint32_t *pllcfg, int plloff)
1370{
1371 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1372 uintptr_t rcc_base = stm32mp_rcc_base();
1373 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1374 enum stm32mp1_plltype type = pll->plltype;
1375 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1376 unsigned long refclk;
1377 uint32_t ifrge = 0U;
Andre Przywara2d5690c2020-03-26 11:50:33 +00001378 uint32_t src, value, fracv = 0;
1379 void *fdt;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001380
1381 /* Check PLL output */
1382 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1383 return false;
1384 }
1385
1386 /* Check current clksrc */
1387 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1388 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1389 return false;
1390 }
1391
1392 /* Check Div */
1393 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1394
1395 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1396 (pllcfg[PLLCFG_M] + 1U);
1397
1398 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1399 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1400 return false;
1401 }
1402
1403 if ((type == PLL_800) && (refclk >= 8000000U)) {
1404 ifrge = 1U;
1405 }
1406
1407 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1408 RCC_PLLNCFGR1_DIVN_MASK;
1409 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1410 RCC_PLLNCFGR1_DIVM_MASK;
1411 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1412 RCC_PLLNCFGR1_IFRGE_MASK;
1413 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1414 return false;
1415 }
1416
1417 /* Fractional configuration */
Andre Przywara2d5690c2020-03-26 11:50:33 +00001418 if (fdt_get_address(&fdt) == 1) {
1419 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1420 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001421
1422 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1423 value |= RCC_PLLNFRACR_FRACLE;
1424 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1425 return false;
1426 }
1427
1428 /* Output config */
1429 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1430 RCC_PLLNCFGR2_DIVP_MASK;
1431 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1432 RCC_PLLNCFGR2_DIVQ_MASK;
1433 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1434 RCC_PLLNCFGR2_DIVR_MASK;
1435 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1436 return false;
1437 }
1438
1439 return true;
1440}
1441
1442static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001443{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001444 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1445 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001446
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001447 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1448 mmio_clrsetbits_32(pllxcr,
1449 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1450 RCC_PLLNCR_DIVREN,
1451 RCC_PLLNCR_PLLON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001452}
1453
Yann Gautiere4a3c352019-02-14 10:53:33 +01001454static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001455{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001456 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1457 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001458 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001459
Yann Gautier9aea69e2018-07-24 17:13:36 +02001460 /* Wait PLL lock */
1461 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001462 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001463 ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001464 pll_id, pllxcr, mmio_read_32(pllxcr));
1465 return -ETIMEDOUT;
1466 }
1467 }
1468
1469 /* Start the requested output */
1470 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1471
1472 return 0;
1473}
1474
Yann Gautiere4a3c352019-02-14 10:53:33 +01001475static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001476{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001477 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1478 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001479 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001480
1481 /* Stop all output */
1482 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1483 RCC_PLLNCR_DIVREN);
1484
1485 /* Stop PLL */
1486 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1487
Yann Gautier2299d572019-02-14 11:14:39 +01001488 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001489 /* Wait PLL stopped */
1490 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001491 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001492 ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001493 pll_id, pllxcr, mmio_read_32(pllxcr));
1494 return -ETIMEDOUT;
1495 }
1496 }
1497
1498 return 0;
1499}
1500
Yann Gautiere4a3c352019-02-14 10:53:33 +01001501static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001502 uint32_t *pllcfg)
1503{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001504 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1505 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001506 uint32_t value;
1507
1508 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1509 RCC_PLLNCFGR2_DIVP_MASK;
1510 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1511 RCC_PLLNCFGR2_DIVQ_MASK;
1512 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1513 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001514 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001515}
1516
Yann Gautiere4a3c352019-02-14 10:53:33 +01001517static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001518 uint32_t *pllcfg, uint32_t fracv)
1519{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001520 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1521 uintptr_t rcc_base = stm32mp_rcc_base();
1522 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001523 unsigned long refclk;
1524 uint32_t ifrge = 0;
1525 uint32_t src, value;
1526
Yann Gautiere4a3c352019-02-14 10:53:33 +01001527 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier9aea69e2018-07-24 17:13:36 +02001528 RCC_SELR_REFCLK_SRC_MASK;
1529
Yann Gautiere4a3c352019-02-14 10:53:33 +01001530 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier9aea69e2018-07-24 17:13:36 +02001531 (pllcfg[PLLCFG_M] + 1U);
1532
1533 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1534 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1535 return -EINVAL;
1536 }
1537
1538 if ((type == PLL_800) && (refclk >= 8000000U)) {
1539 ifrge = 1U;
1540 }
1541
1542 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1543 RCC_PLLNCFGR1_DIVN_MASK;
1544 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1545 RCC_PLLNCFGR1_DIVM_MASK;
1546 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1547 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001548 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001549
1550 /* Fractional configuration */
1551 value = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001552 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001553
1554 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001555 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001556
1557 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001558 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001559
Yann Gautiere4a3c352019-02-14 10:53:33 +01001560 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001561
1562 return 0;
1563}
1564
Yann Gautiere4a3c352019-02-14 10:53:33 +01001565static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001566{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001567 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001568 uint32_t pllxcsg = 0;
1569
1570 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1571 RCC_PLLNCSGR_MOD_PER_MASK;
1572
1573 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1574 RCC_PLLNCSGR_INC_STEP_MASK;
1575
1576 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1577 RCC_PLLNCSGR_SSCG_MODE_MASK;
1578
Yann Gautiere4a3c352019-02-14 10:53:33 +01001579 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001580
1581 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1582 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001583}
1584
Yann Gautiere4a3c352019-02-14 10:53:33 +01001585static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001586{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001587 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier2299d572019-02-14 11:14:39 +01001588 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001589
Yann Gautiere4a3c352019-02-14 10:53:33 +01001590 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001591 clksrc & RCC_SELR_SRC_MASK);
1592
Yann Gautier2299d572019-02-14 11:14:39 +01001593 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001594 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001595 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001596 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1597 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001598 return -ETIMEDOUT;
1599 }
1600 }
1601
1602 return 0;
1603}
1604
Yann Gautiere4a3c352019-02-14 10:53:33 +01001605static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001606{
Yann Gautier2299d572019-02-14 11:14:39 +01001607 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001608
1609 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1610 clkdiv & RCC_DIVR_DIV_MASK);
1611
Yann Gautier2299d572019-02-14 11:14:39 +01001612 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001613 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001614 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001615 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001616 clkdiv, address, mmio_read_32(address));
1617 return -ETIMEDOUT;
1618 }
1619 }
1620
1621 return 0;
1622}
1623
Yann Gautiere4a3c352019-02-14 10:53:33 +01001624static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001625{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001626 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001627
1628 /*
1629 * Binding clksrc :
1630 * bit15-4 offset
1631 * bit3: disable
1632 * bit2-0: MCOSEL[2:0]
1633 */
1634 if ((clksrc & 0x8U) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001635 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001636 } else {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001637 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001638 RCC_MCOCFG_MCOSRC_MASK,
1639 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001640 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001641 RCC_MCOCFG_MCODIV_MASK,
1642 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001643 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001644 }
1645}
1646
Yann Gautiere4a3c352019-02-14 10:53:33 +01001647static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001648{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001649 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001650
1651 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1652 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1653 mmio_clrsetbits_32(address,
1654 RCC_BDCR_RTCSRC_MASK,
1655 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1656
1657 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1658 }
1659
1660 if (lse_css) {
1661 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1662 }
1663}
1664
Yann Gautiere4a3c352019-02-14 10:53:33 +01001665static void stm32mp1_stgen_config(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001666{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001667 uint32_t cntfid0;
1668 unsigned long rate;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001669 unsigned long long counter;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001670
Yann Gautiera18f61b2020-05-05 17:58:40 +02001671 cntfid0 = mmio_read_32(STGEN_BASE + CNTFID_OFF);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001672 rate = get_clock_rate(stm32mp1_clk_get_parent(STGEN_K));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001673
Yann Gautiere4a3c352019-02-14 10:53:33 +01001674 if (cntfid0 == rate) {
1675 return;
1676 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001677
Yann Gautiera18f61b2020-05-05 17:58:40 +02001678 mmio_clrbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
1679 counter = (unsigned long long)mmio_read_32(STGEN_BASE + CNTCVL_OFF);
1680 counter |= ((unsigned long long)mmio_read_32(STGEN_BASE + CNTCVU_OFF)) << 32;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001681 counter = (counter * rate / cntfid0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001682
Yann Gautiera18f61b2020-05-05 17:58:40 +02001683 mmio_write_32(STGEN_BASE + CNTCVL_OFF, (uint32_t)counter);
1684 mmio_write_32(STGEN_BASE + CNTCVU_OFF, (uint32_t)(counter >> 32));
1685 mmio_write_32(STGEN_BASE + CNTFID_OFF, rate);
1686 mmio_setbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001687
Yann Gautiere4a3c352019-02-14 10:53:33 +01001688 write_cntfrq((u_register_t)rate);
1689
1690 /* Need to update timer with new frequency */
1691 generic_delay_timer_init();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001692}
1693
1694void stm32mp1_stgen_increment(unsigned long long offset_in_ms)
1695{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001696 unsigned long long cnt;
1697
Yann Gautiera18f61b2020-05-05 17:58:40 +02001698 cnt = ((unsigned long long)mmio_read_32(STGEN_BASE + CNTCVU_OFF) << 32) |
1699 mmio_read_32(STGEN_BASE + CNTCVL_OFF);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001700
Yann Gautiera18f61b2020-05-05 17:58:40 +02001701 cnt += (offset_in_ms * mmio_read_32(STGEN_BASE + CNTFID_OFF)) / 1000U;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001702
Yann Gautiera18f61b2020-05-05 17:58:40 +02001703 mmio_clrbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
1704 mmio_write_32(STGEN_BASE + CNTCVL_OFF, (uint32_t)cnt);
1705 mmio_write_32(STGEN_BASE + CNTCVU_OFF, (uint32_t)(cnt >> 32));
1706 mmio_setbits_32(STGEN_BASE + CNTCR_OFF, CNTCR_EN);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001707}
1708
Yann Gautiere4a3c352019-02-14 10:53:33 +01001709static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001710{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001711 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001712 uint32_t value = pkcs & 0xFU;
1713 uint32_t mask = 0xFU;
1714
1715 if ((pkcs & BIT(31)) != 0U) {
1716 mask <<= 4;
1717 value <<= 4;
1718 }
1719
1720 mmio_clrsetbits_32(address, mask, value);
1721}
1722
1723int stm32mp1_clk_init(void)
1724{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001725 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001726 unsigned int clksrc[CLKSRC_NB];
1727 unsigned int clkdiv[CLKDIV_NB];
1728 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1729 int plloff[_PLL_NB];
1730 int ret, len;
1731 enum stm32mp1_pll_id i;
1732 bool lse_css = false;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001733 bool pll3_preserve = false;
1734 bool pll4_preserve = false;
1735 bool pll4_bootrom = false;
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001736 const fdt32_t *pkcs_cell;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001737 void *fdt;
1738
1739 if (fdt_get_address(&fdt) == 0) {
Yann Gautier360e0e92020-09-16 16:40:34 +02001740 return -FDT_ERR_NOTFOUND;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001741 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001742
1743 /* Check status field to disable security */
1744 if (!fdt_get_rcc_secure_status()) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001745 mmio_write_32(rcc_base + RCC_TZCR, 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001746 }
1747
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001748 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1749 clksrc);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001750 if (ret < 0) {
1751 return -FDT_ERR_NOTFOUND;
1752 }
1753
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001754 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1755 clkdiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001756 if (ret < 0) {
1757 return -FDT_ERR_NOTFOUND;
1758 }
1759
1760 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1761 char name[12];
1762
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01001763 snprintf(name, sizeof(name), "st,pll@%d", i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001764 plloff[i] = fdt_rcc_subnode_offset(name);
1765
1766 if (!fdt_check_node(plloff[i])) {
1767 continue;
1768 }
1769
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001770 ret = fdt_read_uint32_array(fdt, plloff[i], "cfg",
1771 (int)PLLCFG_NB, pllcfg[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001772 if (ret < 0) {
1773 return -FDT_ERR_NOTFOUND;
1774 }
1775 }
1776
Yann Gautiere4a3c352019-02-14 10:53:33 +01001777 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1778 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001779
1780 /*
1781 * Switch ON oscillator found in device-tree.
1782 * Note: HSI already ON after BootROM stage.
1783 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001784 if (stm32mp1_osc[_LSI] != 0U) {
1785 stm32mp1_lsi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001786 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001787 if (stm32mp1_osc[_LSE] != 0U) {
1788 bool bypass, digbyp;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001789 uint32_t lsedrv;
1790
1791 bypass = fdt_osc_read_bool(_LSE, "st,bypass");
Yann Gautiere4a3c352019-02-14 10:53:33 +01001792 digbyp = fdt_osc_read_bool(_LSE, "st,digbypass");
Yann Gautier9aea69e2018-07-24 17:13:36 +02001793 lse_css = fdt_osc_read_bool(_LSE, "st,css");
1794 lsedrv = fdt_osc_read_uint32_default(_LSE, "st,drive",
1795 LSEDRV_MEDIUM_HIGH);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001796 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001797 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001798 if (stm32mp1_osc[_HSE] != 0U) {
1799 bool bypass, digbyp, css;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001800
Yann Gautiere4a3c352019-02-14 10:53:33 +01001801 bypass = fdt_osc_read_bool(_HSE, "st,bypass");
1802 digbyp = fdt_osc_read_bool(_HSE, "st,digbypass");
1803 css = fdt_osc_read_bool(_HSE, "st,css");
1804 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001805 }
1806 /*
1807 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1808 * => switch on CSI even if node is not present in device tree
1809 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001810 stm32mp1_csi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001811
1812 /* Come back to HSI */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001813 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001814 if (ret != 0) {
1815 return ret;
1816 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001817 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001818 if (ret != 0) {
1819 return ret;
1820 }
Yann Gautiered342322019-02-15 17:33:27 +01001821 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1822 if (ret != 0) {
1823 return ret;
1824 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001825
Yann Gautiere4a3c352019-02-14 10:53:33 +01001826 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1827 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
1828 pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
1829 clksrc[CLKSRC_PLL3],
1830 pllcfg[_PLL3],
1831 plloff[_PLL3]);
1832 pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
1833 clksrc[CLKSRC_PLL4],
1834 pllcfg[_PLL4],
1835 plloff[_PLL4]);
1836 }
1837
Yann Gautier9aea69e2018-07-24 17:13:36 +02001838 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001839 if (((i == _PLL3) && pll3_preserve) ||
1840 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001841 continue;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001842 }
1843
1844 ret = stm32mp1_pll_stop(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001845 if (ret != 0) {
1846 return ret;
1847 }
1848 }
1849
1850 /* Configure HSIDIV */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001851 if (stm32mp1_osc[_HSI] != 0U) {
1852 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001853 if (ret != 0) {
1854 return ret;
1855 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001856 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001857 }
1858
1859 /* Select DIV */
1860 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001861 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001862 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001863 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001864 if (ret != 0) {
1865 return ret;
1866 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001867 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001868 if (ret != 0) {
1869 return ret;
1870 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001871 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001872 if (ret != 0) {
1873 return ret;
1874 }
Yann Gautiered342322019-02-15 17:33:27 +01001875 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
1876 if (ret != 0) {
1877 return ret;
1878 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001879 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001880 if (ret != 0) {
1881 return ret;
1882 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001883 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001884 if (ret != 0) {
1885 return ret;
1886 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001887 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001888 if (ret != 0) {
1889 return ret;
1890 }
1891
1892 /* No ready bit for RTC */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001893 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001894 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
1895
1896 /* Configure PLLs source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001897 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001898 if (ret != 0) {
1899 return ret;
1900 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001901
1902 if (!pll3_preserve) {
1903 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
1904 if (ret != 0) {
1905 return ret;
1906 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001907 }
1908
Yann Gautiere4a3c352019-02-14 10:53:33 +01001909 if (!pll4_preserve) {
1910 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
1911 if (ret != 0) {
1912 return ret;
1913 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001914 }
1915
1916 /* Configure and start PLLs */
1917 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1918 uint32_t fracv;
1919 uint32_t csg[PLLCSG_NB];
1920
Yann Gautiere4a3c352019-02-14 10:53:33 +01001921 if (((i == _PLL3) && pll3_preserve) ||
1922 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
1923 continue;
1924 }
1925
Yann Gautier9aea69e2018-07-24 17:13:36 +02001926 if (!fdt_check_node(plloff[i])) {
1927 continue;
1928 }
1929
Yann Gautiere4a3c352019-02-14 10:53:33 +01001930 if ((i == _PLL4) && pll4_bootrom) {
1931 /* Set output divider if not done by the Bootrom */
1932 stm32mp1_pll_config_output(i, pllcfg[i]);
1933 continue;
1934 }
1935
Andre Przywara2d5690c2020-03-26 11:50:33 +00001936 fracv = fdt_read_uint32_default(fdt, plloff[i], "frac", 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001937
Yann Gautiere4a3c352019-02-14 10:53:33 +01001938 ret = stm32mp1_pll_config(i, pllcfg[i], fracv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001939 if (ret != 0) {
1940 return ret;
1941 }
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001942 ret = fdt_read_uint32_array(fdt, plloff[i], "csg",
1943 (uint32_t)PLLCSG_NB, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001944 if (ret == 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001945 stm32mp1_pll_csg(i, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001946 } else if (ret != -FDT_ERR_NOTFOUND) {
1947 return ret;
1948 }
1949
Yann Gautiere4a3c352019-02-14 10:53:33 +01001950 stm32mp1_pll_start(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001951 }
1952 /* Wait and start PLLs ouptut when ready */
1953 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1954 if (!fdt_check_node(plloff[i])) {
1955 continue;
1956 }
1957
Yann Gautiere4a3c352019-02-14 10:53:33 +01001958 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001959 if (ret != 0) {
1960 return ret;
1961 }
1962 }
1963 /* Wait LSE ready before to use it */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001964 if (stm32mp1_osc[_LSE] != 0U) {
1965 stm32mp1_lse_wait();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001966 }
1967
1968 /* Configure with expected clock source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001969 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001970 if (ret != 0) {
1971 return ret;
1972 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001973 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001974 if (ret != 0) {
1975 return ret;
1976 }
Yann Gautiered342322019-02-15 17:33:27 +01001977 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
1978 if (ret != 0) {
1979 return ret;
1980 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001981 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001982
1983 /* Configure PKCK */
1984 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
1985 if (pkcs_cell != NULL) {
1986 bool ckper_disabled = false;
1987 uint32_t j;
1988
Yann Gautier9aea69e2018-07-24 17:13:36 +02001989 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001990 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001991
1992 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
1993 ckper_disabled = true;
1994 continue;
1995 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001996 stm32mp1_pkcs_config(pkcs);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001997 }
1998
1999 /*
2000 * CKPER is source for some peripheral clocks
2001 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2002 * only if previous clock is still ON
2003 * => deactivated CKPER only after switching clock
2004 */
2005 if (ckper_disabled) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002006 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002007 }
2008 }
2009
2010 /* Switch OFF HSI if not found in device-tree */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002011 if (stm32mp1_osc[_HSI] == 0U) {
2012 stm32mp1_hsi_set(false);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002013 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002014 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02002015
2016 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002017 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002018 RCC_DDRITFCR_DDRCKMOD_MASK,
2019 RCC_DDRITFCR_DDRCKMOD_SSR <<
2020 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2021
2022 return 0;
2023}
2024
2025static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002026 enum stm32mp_osc_id index)
2027{
2028 uint32_t frequency;
2029
Yann Gautiere4a3c352019-02-14 10:53:33 +01002030 if (fdt_osc_read_freq(name, &frequency) == 0) {
2031 stm32mp1_osc[index] = frequency;
Yann Gautier9aea69e2018-07-24 17:13:36 +02002032 }
2033}
2034
2035static void stm32mp1_osc_init(void)
2036{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002037 enum stm32mp_osc_id i;
2038
2039 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002040 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002041 }
2042}
Etienne Carriere1368ada2020-05-13 11:49:49 +02002043
2044#ifdef STM32MP_SHARED_RESOURCES
2045/*
2046 * Get the parent ID of the target parent clock, for tagging as secure
2047 * shared clock dependencies.
2048 */
2049static int get_parent_id_parent(unsigned int parent_id)
2050{
2051 enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
2052 enum stm32mp1_pll_id pll_id;
2053 uint32_t p_sel;
2054 uintptr_t rcc_base = stm32mp_rcc_base();
2055
2056 switch (parent_id) {
2057 case _ACLK:
2058 case _PCLK4:
2059 case _PCLK5:
2060 s = _AXIS_SEL;
2061 break;
2062 case _PLL1_P:
2063 case _PLL1_Q:
2064 case _PLL1_R:
2065 pll_id = _PLL1;
2066 break;
2067 case _PLL2_P:
2068 case _PLL2_Q:
2069 case _PLL2_R:
2070 pll_id = _PLL2;
2071 break;
2072 case _PLL3_P:
2073 case _PLL3_Q:
2074 case _PLL3_R:
2075 pll_id = _PLL3;
2076 break;
2077 case _PLL4_P:
2078 case _PLL4_Q:
2079 case _PLL4_R:
2080 pll_id = _PLL4;
2081 break;
2082 case _PCLK1:
2083 case _PCLK2:
2084 case _HCLK2:
2085 case _HCLK6:
2086 case _CK_PER:
2087 case _CK_MPU:
2088 case _CK_MCU:
2089 case _USB_PHY_48:
2090 /* We do not expect to access these */
2091 panic();
2092 break;
2093 default:
2094 /* Other parents have no parent */
2095 return -1;
2096 }
2097
2098 if (s != _UNKNOWN_SEL) {
2099 const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
2100
2101 p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
2102 sel->msk;
2103
2104 if (p_sel < sel->nb_parent) {
2105 return (int)sel->parent[p_sel];
2106 }
2107 } else {
2108 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
2109
2110 p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
2111 RCC_SELR_REFCLK_SRC_MASK;
2112
2113 if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
2114 return (int)pll->refclk[p_sel];
2115 }
2116 }
2117
2118 VERBOSE("No parent selected for %s\n",
2119 stm32mp1_clk_parent_name[parent_id]);
2120
2121 return -1;
2122}
2123
2124static void secure_parent_clocks(unsigned long parent_id)
2125{
2126 int grandparent_id;
2127
2128 switch (parent_id) {
2129 case _PLL3_P:
2130 case _PLL3_Q:
2131 case _PLL3_R:
2132 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2133 break;
2134
2135 /* These clocks are always secure when RCC is secure */
2136 case _ACLK:
2137 case _HCLK2:
2138 case _HCLK6:
2139 case _PCLK4:
2140 case _PCLK5:
2141 case _PLL1_P:
2142 case _PLL1_Q:
2143 case _PLL1_R:
2144 case _PLL2_P:
2145 case _PLL2_Q:
2146 case _PLL2_R:
2147 case _HSI:
2148 case _HSI_KER:
2149 case _LSI:
2150 case _CSI:
2151 case _CSI_KER:
2152 case _HSE:
2153 case _HSE_KER:
2154 case _HSE_KER_DIV2:
2155 case _LSE:
2156 break;
2157
2158 default:
2159 VERBOSE("Cannot secure parent clock %s\n",
2160 stm32mp1_clk_parent_name[parent_id]);
2161 panic();
2162 }
2163
2164 grandparent_id = get_parent_id_parent(parent_id);
2165 if (grandparent_id >= 0) {
2166 secure_parent_clocks(grandparent_id);
2167 }
2168}
2169
2170void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
2171{
2172 int parent_id;
2173
2174 if (!stm32mp1_rcc_is_secure()) {
2175 return;
2176 }
2177
2178 switch (clock_id) {
2179 case PLL1:
2180 case PLL2:
2181 /* PLL1/PLL2 are always secure: nothing to do */
2182 break;
2183 case PLL3:
2184 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2185 break;
2186 case PLL4:
2187 ERROR("PLL4 cannot be secured\n");
2188 panic();
2189 break;
2190 default:
2191 /* Others are expected gateable clock */
2192 parent_id = stm32mp1_clk_get_parent(clock_id);
2193 if (parent_id < 0) {
2194 INFO("No parent found for clock %lu\n", clock_id);
2195 } else {
2196 secure_parent_clocks(parent_id);
2197 }
2198 break;
2199 }
2200}
2201#endif /* STM32MP_SHARED_RESOURCES */
Yann Gautier9aea69e2018-07-24 17:13:36 +02002202
Yann Gautierc7f9e962019-05-20 14:39:26 +02002203static void sync_earlyboot_clocks_state(void)
2204{
Etienne Carriere2a756c22019-12-08 08:23:35 +01002205 unsigned int idx;
2206 const unsigned long secure_enable[] = {
2207 AXIDCG,
2208 BSEC,
2209 DDRC1, DDRC1LP,
2210 DDRC2, DDRC2LP,
2211 DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
2212 DDRPHYC, DDRPHYCLP,
2213 TZC1, TZC2,
2214 TZPC,
2215 STGEN_K,
2216 };
2217
2218 for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
2219 stm32mp_clk_enable(secure_enable[idx]);
2220 }
2221
Yann Gautierc7f9e962019-05-20 14:39:26 +02002222 if (!stm32mp_is_single_core()) {
2223 stm32mp1_clk_enable_secure(RTCAPB);
2224 }
2225}
2226
Yann Gautier9aea69e2018-07-24 17:13:36 +02002227int stm32mp1_clk_probe(void)
2228{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002229 stm32mp1_osc_init();
2230
Yann Gautierc7f9e962019-05-20 14:39:26 +02002231 sync_earlyboot_clocks_state();
2232
Yann Gautier9aea69e2018-07-24 17:13:36 +02002233 return 0;
2234}