blob: d6cd8b1ce4fbe591ae1fc873abc432e8999de127 [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
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();
656
657 return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_TZEN) != 0;
658}
659
Yann Gautiered342322019-02-15 17:33:27 +0100660bool stm32mp1_rcc_is_mckprot(void)
661{
662 uintptr_t rcc_base = stm32mp_rcc_base();
663
664 return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_MCKPROT) != 0;
665}
666
Yann Gautiere4a3c352019-02-14 10:53:33 +0100667void stm32mp1_clk_rcc_regs_lock(void)
668{
669 stm32mp1_clk_lock(&reg_lock);
670}
671
672void stm32mp1_clk_rcc_regs_unlock(void)
673{
674 stm32mp1_clk_unlock(&reg_lock);
675}
676
677static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200678{
679 if (idx >= NB_OSC) {
680 return 0;
681 }
682
Yann Gautiere4a3c352019-02-14 10:53:33 +0100683 return stm32mp1_osc[idx];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200684}
685
Yann Gautiere4a3c352019-02-14 10:53:33 +0100686static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200687{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100688 unsigned int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200689
Yann Gautiere4a3c352019-02-14 10:53:33 +0100690 for (i = 0U; i < NB_GATES; i++) {
691 if (gate_ref(i)->index == id) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200692 return i;
693 }
694 }
695
696 ERROR("%s: clk id %d not found\n", __func__, (uint32_t)id);
697
698 return -EINVAL;
699}
700
Yann Gautiere4a3c352019-02-14 10:53:33 +0100701static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200702{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100703 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200704}
705
Yann Gautiere4a3c352019-02-14 10:53:33 +0100706static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200707{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100708 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200709}
710
Yann Gautiere4a3c352019-02-14 10:53:33 +0100711static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200712{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100713 const struct stm32mp1_clk_sel *sel;
Etienne Carriere04132612019-12-08 08:20:12 +0100714 uint32_t p_sel;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200715 int i;
716 enum stm32mp1_parent_id p;
717 enum stm32mp1_parent_sel s;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100718 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200719
Etienne Carriere04132612019-12-08 08:20:12 +0100720 /* Few non gateable clock have a static parent ID, find them */
721 i = (int)clock_id2parent_id(id);
722 if (i != _UNKNOWN_ID) {
723 return i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200724 }
725
Yann Gautiere4a3c352019-02-14 10:53:33 +0100726 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200727 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100728 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200729 }
730
Yann Gautiere4a3c352019-02-14 10:53:33 +0100731 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200732 if (p < _PARENT_NB) {
733 return (int)p;
734 }
735
Yann Gautiere4a3c352019-02-14 10:53:33 +0100736 s = stm32mp1_clk_get_sel(i);
737 if (s == _UNKNOWN_SEL) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200738 return -EINVAL;
739 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100740 if (s >= _PARENT_SEL_NB) {
741 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200742 }
743
Yann Gautiere4a3c352019-02-14 10:53:33 +0100744 sel = clk_sel_ref(s);
Etienne Carrierec164ce22019-12-08 08:20:40 +0100745 p_sel = (mmio_read_32(rcc_base + sel->offset) &
746 (sel->msk << sel->src)) >> sel->src;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100747 if (p_sel < sel->nb_parent) {
748 return (int)sel->parent[p_sel];
749 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200750
751 return -EINVAL;
752}
753
Yann Gautiere4a3c352019-02-14 10:53:33 +0100754static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200755{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100756 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
757 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200758
Yann Gautiere4a3c352019-02-14 10:53:33 +0100759 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200760}
761
762/*
763 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
764 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
765 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
766 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
767 */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100768static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200769{
Yann Gautier9aea69e2018-07-24 17:13:36 +0200770 unsigned long refclk, fvco;
771 uint32_t cfgr1, fracr, divm, divn;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100772 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200773
Yann Gautiere4a3c352019-02-14 10:53:33 +0100774 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
775 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200776
777 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
778 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
779
Yann Gautiere4a3c352019-02-14 10:53:33 +0100780 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200781
782 /*
783 * With FRACV :
784 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
785 * Without FRACV
786 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
787 */
788 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100789 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
790 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200791 unsigned long long numerator, denominator;
792
Yann Gautiere4a3c352019-02-14 10:53:33 +0100793 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
794 numerator = refclk * numerator;
795 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200796 fvco = (unsigned long)(numerator / denominator);
797 } else {
798 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
799 }
800
801 return fvco;
802}
803
Yann Gautiere4a3c352019-02-14 10:53:33 +0100804static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200805 enum stm32mp1_div_id div_id)
806{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100807 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200808 unsigned long dfout;
809 uint32_t cfgr2, divy;
810
811 if (div_id >= _DIV_NB) {
812 return 0;
813 }
814
Yann Gautiere4a3c352019-02-14 10:53:33 +0100815 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200816 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
817
Yann Gautiere4a3c352019-02-14 10:53:33 +0100818 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200819
820 return dfout;
821}
822
Yann Gautiere4a3c352019-02-14 10:53:33 +0100823static unsigned long get_clock_rate(int p)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200824{
825 uint32_t reg, clkdiv;
826 unsigned long clock = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100827 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200828
829 switch (p) {
830 case _CK_MPU:
831 /* MPU sub system */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100832 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200833 switch (reg & RCC_SELR_SRC_MASK) {
834 case RCC_MPCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100835 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200836 break;
837 case RCC_MPCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100838 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200839 break;
840 case RCC_MPCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100841 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200842 break;
843 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100844 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200845
Yann Gautiere4a3c352019-02-14 10:53:33 +0100846 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200847 clkdiv = reg & RCC_MPUDIV_MASK;
848 if (clkdiv != 0U) {
849 clock /= stm32mp1_mpu_div[clkdiv];
850 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200851 break;
852 default:
853 break;
854 }
855 break;
856 /* AXI sub system */
857 case _ACLK:
858 case _HCLK2:
859 case _HCLK6:
860 case _PCLK4:
861 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100862 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200863 switch (reg & RCC_SELR_SRC_MASK) {
864 case RCC_ASSCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100865 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200866 break;
867 case RCC_ASSCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100868 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200869 break;
870 case RCC_ASSCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100871 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200872 break;
873 default:
874 break;
875 }
876
877 /* System clock divider */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100878 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200879 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
880
881 switch (p) {
882 case _PCLK4:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100883 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200884 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
885 break;
886 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100887 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200888 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
889 break;
890 default:
891 break;
892 }
893 break;
Yann Gautiered342322019-02-15 17:33:27 +0100894 /* MCU sub system */
895 case _CK_MCU:
896 case _PCLK1:
897 case _PCLK2:
898 case _PCLK3:
899 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
900 switch (reg & RCC_SELR_SRC_MASK) {
901 case RCC_MSSCKSELR_HSI:
902 clock = stm32mp1_clk_get_fixed(_HSI);
903 break;
904 case RCC_MSSCKSELR_HSE:
905 clock = stm32mp1_clk_get_fixed(_HSE);
906 break;
907 case RCC_MSSCKSELR_CSI:
908 clock = stm32mp1_clk_get_fixed(_CSI);
909 break;
910 case RCC_MSSCKSELR_PLL:
911 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
912 break;
913 default:
914 break;
915 }
916
917 /* MCU clock divider */
918 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
919 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
920
921 switch (p) {
922 case _PCLK1:
923 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
924 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
925 break;
926 case _PCLK2:
927 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
928 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
929 break;
930 case _PCLK3:
931 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
932 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
933 break;
934 case _CK_MCU:
935 default:
936 break;
937 }
938 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200939 case _CK_PER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100940 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200941 switch (reg & RCC_SELR_SRC_MASK) {
942 case RCC_CPERCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100943 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200944 break;
945 case RCC_CPERCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100946 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200947 break;
948 case RCC_CPERCKSELR_CSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100949 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200950 break;
951 default:
952 break;
953 }
954 break;
955 case _HSI:
956 case _HSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100957 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200958 break;
959 case _CSI:
960 case _CSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100961 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200962 break;
963 case _HSE:
964 case _HSE_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100965 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200966 break;
967 case _HSE_KER_DIV2:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100968 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200969 break;
970 case _LSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100971 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200972 break;
973 case _LSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100974 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200975 break;
976 /* PLL */
977 case _PLL1_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100978 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200979 break;
980 case _PLL1_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100981 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200982 break;
983 case _PLL1_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100984 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200985 break;
986 case _PLL2_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100987 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200988 break;
989 case _PLL2_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100990 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200991 break;
992 case _PLL2_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100993 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200994 break;
995 case _PLL3_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100996 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200997 break;
998 case _PLL3_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100999 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001000 break;
1001 case _PLL3_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001002 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001003 break;
1004 case _PLL4_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001005 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001006 break;
1007 case _PLL4_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001008 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001009 break;
1010 case _PLL4_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001011 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001012 break;
1013 /* Other */
1014 case _USB_PHY_48:
Yann Gautiere4a3c352019-02-14 10:53:33 +01001015 clock = USB_PHY_48_MHZ;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001016 break;
1017 default:
1018 break;
1019 }
1020
1021 return clock;
1022}
1023
Yann Gautiere4a3c352019-02-14 10:53:33 +01001024static void __clk_enable(struct stm32mp1_clk_gate const *gate)
1025{
1026 uintptr_t rcc_base = stm32mp_rcc_base();
1027
Etienne Carriere8a668892019-12-08 08:21:08 +01001028 VERBOSE("Enable clock %u\n", gate->index);
1029
Yann Gautiere4a3c352019-02-14 10:53:33 +01001030 if (gate->set_clr != 0U) {
1031 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
1032 } else {
1033 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
1034 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001035}
1036
1037static void __clk_disable(struct stm32mp1_clk_gate const *gate)
1038{
1039 uintptr_t rcc_base = stm32mp_rcc_base();
1040
Etienne Carriere8a668892019-12-08 08:21:08 +01001041 VERBOSE("Disable clock %u\n", gate->index);
1042
Yann Gautiere4a3c352019-02-14 10:53:33 +01001043 if (gate->set_clr != 0U) {
1044 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
1045 BIT(gate->bit));
1046 } else {
1047 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
1048 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001049}
1050
1051static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
1052{
1053 uintptr_t rcc_base = stm32mp_rcc_base();
1054
1055 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
1056}
1057
1058unsigned int stm32mp1_clk_get_refcount(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001059{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001060 int i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001061
1062 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001063 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001064 }
1065
Yann Gautiere4a3c352019-02-14 10:53:33 +01001066 return gate_refcounts[i];
Yann Gautier9aea69e2018-07-24 17:13:36 +02001067}
1068
Etienne Carriere481aa002019-12-08 08:21:44 +01001069/* Oscillators and PLLs are not gated at runtime */
1070static bool clock_is_always_on(unsigned long id)
1071{
1072 switch (id) {
1073 case CK_HSE:
1074 case CK_CSI:
1075 case CK_LSI:
1076 case CK_LSE:
1077 case CK_HSI:
1078 case CK_HSE_DIV2:
1079 case PLL1_Q:
1080 case PLL1_R:
1081 case PLL2_P:
1082 case PLL2_Q:
1083 case PLL2_R:
1084 case PLL3_P:
1085 case PLL3_Q:
1086 case PLL3_R:
1087 return true;
1088 default:
1089 return false;
1090 }
1091}
1092
Yann Gautiere4a3c352019-02-14 10:53:33 +01001093void __stm32mp1_clk_enable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001094{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001095 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001096 int i;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001097 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001098
Etienne Carriere481aa002019-12-08 08:21:44 +01001099 if (clock_is_always_on(id)) {
1100 return;
1101 }
1102
1103 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001104 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001105 ERROR("Clock %d can't be enabled\n", (uint32_t)id);
1106 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001107 }
1108
Yann Gautiere4a3c352019-02-14 10:53:33 +01001109 gate = gate_ref(i);
1110 refcnt = &gate_refcounts[i];
1111
1112 stm32mp1_clk_lock(&refcount_lock);
1113
1114 if (stm32mp_incr_shrefcnt(refcnt, secure) != 0) {
1115 __clk_enable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001116 }
1117
Yann Gautiere4a3c352019-02-14 10:53:33 +01001118 stm32mp1_clk_unlock(&refcount_lock);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001119}
1120
Yann Gautiere4a3c352019-02-14 10:53:33 +01001121void __stm32mp1_clk_disable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001122{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001123 const struct stm32mp1_clk_gate *gate;
Etienne Carriere481aa002019-12-08 08:21:44 +01001124 int i;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001125 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001126
Etienne Carriere481aa002019-12-08 08:21:44 +01001127 if (clock_is_always_on(id)) {
1128 return;
1129 }
1130
1131 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001132 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001133 ERROR("Clock %d can't be disabled\n", (uint32_t)id);
1134 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001135 }
1136
Yann Gautiere4a3c352019-02-14 10:53:33 +01001137 gate = gate_ref(i);
1138 refcnt = &gate_refcounts[i];
1139
1140 stm32mp1_clk_lock(&refcount_lock);
1141
1142 if (stm32mp_decr_shrefcnt(refcnt, secure) != 0) {
1143 __clk_disable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001144 }
1145
Yann Gautiere4a3c352019-02-14 10:53:33 +01001146 stm32mp1_clk_unlock(&refcount_lock);
1147}
1148
1149void stm32mp_clk_enable(unsigned long id)
1150{
1151 __stm32mp1_clk_enable(id, true);
1152}
1153
1154void stm32mp_clk_disable(unsigned long id)
1155{
1156 __stm32mp1_clk_disable(id, true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001157}
1158
Yann Gautiere4a3c352019-02-14 10:53:33 +01001159bool stm32mp_clk_is_enabled(unsigned long id)
1160{
Etienne Carriere481aa002019-12-08 08:21:44 +01001161 int i;
1162
1163 if (clock_is_always_on(id)) {
1164 return true;
1165 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001166
Etienne Carriere481aa002019-12-08 08:21:44 +01001167 i = stm32mp1_clk_get_gated_id(id);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001168 if (i < 0) {
1169 panic();
1170 }
1171
1172 return __clk_is_enabled(gate_ref(i));
1173}
1174
Yann Gautiera2e2a302019-02-14 11:13:39 +01001175unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001176{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001177 int p = stm32mp1_clk_get_parent(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001178
1179 if (p < 0) {
1180 return 0;
1181 }
1182
Yann Gautiere4a3c352019-02-14 10:53:33 +01001183 return get_clock_rate(p);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001184}
1185
Yann Gautiere4a3c352019-02-14 10:53:33 +01001186static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001187{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001188 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001189
Yann Gautiere4a3c352019-02-14 10:53:33 +01001190 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001191 mmio_setbits_32(address, mask_on);
1192 } else {
1193 mmio_clrbits_32(address, mask_on);
1194 }
1195}
1196
Yann Gautiere4a3c352019-02-14 10:53:33 +01001197static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001198{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001199 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1200 uintptr_t address = stm32mp_rcc_base() + offset;
1201
1202 mmio_write_32(address, mask_on);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001203}
1204
Yann Gautiere4a3c352019-02-14 10:53:33 +01001205static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001206{
Yann Gautier2299d572019-02-14 11:14:39 +01001207 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001208 uint32_t mask_test;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001209 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001210
Yann Gautiere4a3c352019-02-14 10:53:33 +01001211 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001212 mask_test = mask_rdy;
1213 } else {
1214 mask_test = 0;
1215 }
1216
Yann Gautier2299d572019-02-14 11:14:39 +01001217 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001218 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautier2299d572019-02-14 11:14:39 +01001219 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001220 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001221 mask_rdy, address, enable, mmio_read_32(address));
1222 return -ETIMEDOUT;
1223 }
1224 }
1225
1226 return 0;
1227}
1228
Yann Gautiere4a3c352019-02-14 10:53:33 +01001229static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001230{
1231 uint32_t value;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001232 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001233
Yann Gautiere4a3c352019-02-14 10:53:33 +01001234 if (digbyp) {
1235 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001236 }
1237
Yann Gautiere4a3c352019-02-14 10:53:33 +01001238 if (bypass || digbyp) {
1239 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1240 }
1241
Yann Gautier9aea69e2018-07-24 17:13:36 +02001242 /*
1243 * Warning: not recommended to switch directly from "high drive"
1244 * to "medium low drive", and vice-versa.
1245 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001246 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier9aea69e2018-07-24 17:13:36 +02001247 RCC_BDCR_LSEDRV_SHIFT;
1248
1249 while (value != lsedrv) {
1250 if (value > lsedrv) {
1251 value--;
1252 } else {
1253 value++;
1254 }
1255
Yann Gautiere4a3c352019-02-14 10:53:33 +01001256 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001257 RCC_BDCR_LSEDRV_MASK,
1258 value << RCC_BDCR_LSEDRV_SHIFT);
1259 }
1260
Yann Gautiere4a3c352019-02-14 10:53:33 +01001261 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001262}
1263
Yann Gautiere4a3c352019-02-14 10:53:33 +01001264static void stm32mp1_lse_wait(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001265{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001266 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001267 VERBOSE("%s: failed\n", __func__);
1268 }
1269}
1270
Yann Gautiere4a3c352019-02-14 10:53:33 +01001271static void stm32mp1_lsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001272{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001273 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1274
1275 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001276 VERBOSE("%s: failed\n", __func__);
1277 }
1278}
1279
Yann Gautiere4a3c352019-02-14 10:53:33 +01001280static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001281{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001282 uintptr_t rcc_base = stm32mp_rcc_base();
1283
1284 if (digbyp) {
1285 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001286 }
1287
Yann Gautiere4a3c352019-02-14 10:53:33 +01001288 if (bypass || digbyp) {
1289 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1290 }
1291
1292 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1293 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001294 VERBOSE("%s: failed\n", __func__);
1295 }
1296
1297 if (css) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001298 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001299 }
1300}
1301
Yann Gautiere4a3c352019-02-14 10:53:33 +01001302static void stm32mp1_csi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001303{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001304 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1305 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001306 VERBOSE("%s: failed\n", __func__);
1307 }
1308}
1309
Yann Gautiere4a3c352019-02-14 10:53:33 +01001310static void stm32mp1_hsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001311{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001312 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1313 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001314 VERBOSE("%s: failed\n", __func__);
1315 }
1316}
1317
Yann Gautiere4a3c352019-02-14 10:53:33 +01001318static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001319{
Yann Gautier2299d572019-02-14 11:14:39 +01001320 uint64_t timeout;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001321 uintptr_t rcc_base = stm32mp_rcc_base();
1322 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001323
Yann Gautiere4a3c352019-02-14 10:53:33 +01001324 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001325 RCC_HSICFGR_HSIDIV_MASK,
1326 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1327
Yann Gautier2299d572019-02-14 11:14:39 +01001328 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001329 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001330 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001331 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001332 address, mmio_read_32(address));
1333 return -ETIMEDOUT;
1334 }
1335 }
1336
1337 return 0;
1338}
1339
Yann Gautiere4a3c352019-02-14 10:53:33 +01001340static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001341{
1342 uint8_t hsidiv;
1343 uint32_t hsidivfreq = MAX_HSI_HZ;
1344
1345 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1346 if (hsidivfreq == hsifreq) {
1347 break;
1348 }
1349
1350 hsidivfreq /= 2U;
1351 }
1352
1353 if (hsidiv == 4U) {
1354 ERROR("Invalid clk-hsi frequency\n");
1355 return -1;
1356 }
1357
1358 if (hsidiv != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001359 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001360 }
1361
1362 return 0;
1363}
1364
Yann Gautiere4a3c352019-02-14 10:53:33 +01001365static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1366 unsigned int clksrc,
1367 uint32_t *pllcfg, int plloff)
1368{
1369 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1370 uintptr_t rcc_base = stm32mp_rcc_base();
1371 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1372 enum stm32mp1_plltype type = pll->plltype;
1373 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1374 unsigned long refclk;
1375 uint32_t ifrge = 0U;
Andre Przywara2d5690c2020-03-26 11:50:33 +00001376 uint32_t src, value, fracv = 0;
1377 void *fdt;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001378
1379 /* Check PLL output */
1380 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1381 return false;
1382 }
1383
1384 /* Check current clksrc */
1385 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1386 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1387 return false;
1388 }
1389
1390 /* Check Div */
1391 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1392
1393 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1394 (pllcfg[PLLCFG_M] + 1U);
1395
1396 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1397 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1398 return false;
1399 }
1400
1401 if ((type == PLL_800) && (refclk >= 8000000U)) {
1402 ifrge = 1U;
1403 }
1404
1405 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1406 RCC_PLLNCFGR1_DIVN_MASK;
1407 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1408 RCC_PLLNCFGR1_DIVM_MASK;
1409 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1410 RCC_PLLNCFGR1_IFRGE_MASK;
1411 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1412 return false;
1413 }
1414
1415 /* Fractional configuration */
Andre Przywara2d5690c2020-03-26 11:50:33 +00001416 if (fdt_get_address(&fdt) == 1) {
1417 fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0);
1418 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001419
1420 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1421 value |= RCC_PLLNFRACR_FRACLE;
1422 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1423 return false;
1424 }
1425
1426 /* Output config */
1427 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1428 RCC_PLLNCFGR2_DIVP_MASK;
1429 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1430 RCC_PLLNCFGR2_DIVQ_MASK;
1431 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1432 RCC_PLLNCFGR2_DIVR_MASK;
1433 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1434 return false;
1435 }
1436
1437 return true;
1438}
1439
1440static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001441{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001442 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1443 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001444
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001445 /* Preserve RCC_PLLNCR_SSCG_CTRL value */
1446 mmio_clrsetbits_32(pllxcr,
1447 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1448 RCC_PLLNCR_DIVREN,
1449 RCC_PLLNCR_PLLON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001450}
1451
Yann Gautiere4a3c352019-02-14 10:53:33 +01001452static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001453{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001454 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1455 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001456 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001457
Yann Gautier9aea69e2018-07-24 17:13:36 +02001458 /* Wait PLL lock */
1459 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001460 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001461 ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001462 pll_id, pllxcr, mmio_read_32(pllxcr));
1463 return -ETIMEDOUT;
1464 }
1465 }
1466
1467 /* Start the requested output */
1468 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1469
1470 return 0;
1471}
1472
Yann Gautiere4a3c352019-02-14 10:53:33 +01001473static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001474{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001475 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1476 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001477 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001478
1479 /* Stop all output */
1480 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1481 RCC_PLLNCR_DIVREN);
1482
1483 /* Stop PLL */
1484 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1485
Yann Gautier2299d572019-02-14 11:14:39 +01001486 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001487 /* Wait PLL stopped */
1488 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001489 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001490 ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001491 pll_id, pllxcr, mmio_read_32(pllxcr));
1492 return -ETIMEDOUT;
1493 }
1494 }
1495
1496 return 0;
1497}
1498
Yann Gautiere4a3c352019-02-14 10:53:33 +01001499static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001500 uint32_t *pllcfg)
1501{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001502 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1503 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001504 uint32_t value;
1505
1506 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1507 RCC_PLLNCFGR2_DIVP_MASK;
1508 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1509 RCC_PLLNCFGR2_DIVQ_MASK;
1510 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1511 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001512 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001513}
1514
Yann Gautiere4a3c352019-02-14 10:53:33 +01001515static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001516 uint32_t *pllcfg, uint32_t fracv)
1517{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001518 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1519 uintptr_t rcc_base = stm32mp_rcc_base();
1520 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001521 unsigned long refclk;
1522 uint32_t ifrge = 0;
1523 uint32_t src, value;
1524
Yann Gautiere4a3c352019-02-14 10:53:33 +01001525 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier9aea69e2018-07-24 17:13:36 +02001526 RCC_SELR_REFCLK_SRC_MASK;
1527
Yann Gautiere4a3c352019-02-14 10:53:33 +01001528 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier9aea69e2018-07-24 17:13:36 +02001529 (pllcfg[PLLCFG_M] + 1U);
1530
1531 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1532 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1533 return -EINVAL;
1534 }
1535
1536 if ((type == PLL_800) && (refclk >= 8000000U)) {
1537 ifrge = 1U;
1538 }
1539
1540 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1541 RCC_PLLNCFGR1_DIVN_MASK;
1542 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1543 RCC_PLLNCFGR1_DIVM_MASK;
1544 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1545 RCC_PLLNCFGR1_IFRGE_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001546 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001547
1548 /* Fractional configuration */
1549 value = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001550 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001551
1552 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001553 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001554
1555 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001556 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001557
Yann Gautiere4a3c352019-02-14 10:53:33 +01001558 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001559
1560 return 0;
1561}
1562
Yann Gautiere4a3c352019-02-14 10:53:33 +01001563static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001564{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001565 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001566 uint32_t pllxcsg = 0;
1567
1568 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1569 RCC_PLLNCSGR_MOD_PER_MASK;
1570
1571 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1572 RCC_PLLNCSGR_INC_STEP_MASK;
1573
1574 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1575 RCC_PLLNCSGR_SSCG_MODE_MASK;
1576
Yann Gautiere4a3c352019-02-14 10:53:33 +01001577 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautierd0dcbaa2019-06-04 15:55:37 +02001578
1579 mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr,
1580 RCC_PLLNCR_SSCG_CTRL);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001581}
1582
Yann Gautiere4a3c352019-02-14 10:53:33 +01001583static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001584{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001585 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier2299d572019-02-14 11:14:39 +01001586 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001587
Yann Gautiere4a3c352019-02-14 10:53:33 +01001588 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001589 clksrc & RCC_SELR_SRC_MASK);
1590
Yann Gautier2299d572019-02-14 11:14:39 +01001591 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001592 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001593 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001594 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1595 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001596 return -ETIMEDOUT;
1597 }
1598 }
1599
1600 return 0;
1601}
1602
Yann Gautiere4a3c352019-02-14 10:53:33 +01001603static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001604{
Yann Gautier2299d572019-02-14 11:14:39 +01001605 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001606
1607 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1608 clkdiv & RCC_DIVR_DIV_MASK);
1609
Yann Gautier2299d572019-02-14 11:14:39 +01001610 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001611 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001612 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001613 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001614 clkdiv, address, mmio_read_32(address));
1615 return -ETIMEDOUT;
1616 }
1617 }
1618
1619 return 0;
1620}
1621
Yann Gautiere4a3c352019-02-14 10:53:33 +01001622static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001623{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001624 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001625
1626 /*
1627 * Binding clksrc :
1628 * bit15-4 offset
1629 * bit3: disable
1630 * bit2-0: MCOSEL[2:0]
1631 */
1632 if ((clksrc & 0x8U) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001633 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001634 } else {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001635 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001636 RCC_MCOCFG_MCOSRC_MASK,
1637 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001638 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001639 RCC_MCOCFG_MCODIV_MASK,
1640 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001641 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001642 }
1643}
1644
Yann Gautiere4a3c352019-02-14 10:53:33 +01001645static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001646{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001647 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001648
1649 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1650 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1651 mmio_clrsetbits_32(address,
1652 RCC_BDCR_RTCSRC_MASK,
1653 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1654
1655 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1656 }
1657
1658 if (lse_css) {
1659 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1660 }
1661}
1662
Yann Gautiere4a3c352019-02-14 10:53:33 +01001663static void stm32mp1_stgen_config(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001664{
1665 uintptr_t stgen;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001666 uint32_t cntfid0;
1667 unsigned long rate;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001668 unsigned long long counter;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001669
1670 stgen = fdt_get_stgen_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001671 cntfid0 = mmio_read_32(stgen + 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 Gautiere4a3c352019-02-14 10:53:33 +01001678 mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1679 counter = (unsigned long long)mmio_read_32(stgen + CNTCVL_OFF);
1680 counter |= ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF)) << 32;
1681 counter = (counter * rate / cntfid0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001682
Yann Gautiere4a3c352019-02-14 10:53:33 +01001683 mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)counter);
1684 mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(counter >> 32));
1685 mmio_write_32(stgen + CNTFID_OFF, rate);
1686 mmio_setbits_32(stgen + 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{
1696 uintptr_t stgen;
1697 unsigned long long cnt;
1698
1699 stgen = fdt_get_stgen_base();
1700
1701 cnt = ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF) << 32) |
1702 mmio_read_32(stgen + CNTCVL_OFF);
1703
1704 cnt += (offset_in_ms * mmio_read_32(stgen + CNTFID_OFF)) / 1000U;
1705
1706 mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1707 mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)cnt);
1708 mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(cnt >> 32));
1709 mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1710}
1711
Yann Gautiere4a3c352019-02-14 10:53:33 +01001712static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001713{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001714 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001715 uint32_t value = pkcs & 0xFU;
1716 uint32_t mask = 0xFU;
1717
1718 if ((pkcs & BIT(31)) != 0U) {
1719 mask <<= 4;
1720 value <<= 4;
1721 }
1722
1723 mmio_clrsetbits_32(address, mask, value);
1724}
1725
1726int stm32mp1_clk_init(void)
1727{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001728 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001729 unsigned int clksrc[CLKSRC_NB];
1730 unsigned int clkdiv[CLKDIV_NB];
1731 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1732 int plloff[_PLL_NB];
1733 int ret, len;
1734 enum stm32mp1_pll_id i;
1735 bool lse_css = false;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001736 bool pll3_preserve = false;
1737 bool pll4_preserve = false;
1738 bool pll4_bootrom = false;
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001739 const fdt32_t *pkcs_cell;
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001740 void *fdt;
1741
1742 if (fdt_get_address(&fdt) == 0) {
1743 return false;
1744 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001745
1746 /* Check status field to disable security */
1747 if (!fdt_get_rcc_secure_status()) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001748 mmio_write_32(rcc_base + RCC_TZCR, 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001749 }
1750
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001751 ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB,
1752 clksrc);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001753 if (ret < 0) {
1754 return -FDT_ERR_NOTFOUND;
1755 }
1756
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001757 ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB,
1758 clkdiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001759 if (ret < 0) {
1760 return -FDT_ERR_NOTFOUND;
1761 }
1762
1763 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1764 char name[12];
1765
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01001766 snprintf(name, sizeof(name), "st,pll@%d", i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001767 plloff[i] = fdt_rcc_subnode_offset(name);
1768
1769 if (!fdt_check_node(plloff[i])) {
1770 continue;
1771 }
1772
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001773 ret = fdt_read_uint32_array(fdt, plloff[i], "cfg",
1774 (int)PLLCFG_NB, pllcfg[i]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001775 if (ret < 0) {
1776 return -FDT_ERR_NOTFOUND;
1777 }
1778 }
1779
Yann Gautiere4a3c352019-02-14 10:53:33 +01001780 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1781 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001782
1783 /*
1784 * Switch ON oscillator found in device-tree.
1785 * Note: HSI already ON after BootROM stage.
1786 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001787 if (stm32mp1_osc[_LSI] != 0U) {
1788 stm32mp1_lsi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001789 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001790 if (stm32mp1_osc[_LSE] != 0U) {
1791 bool bypass, digbyp;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001792 uint32_t lsedrv;
1793
1794 bypass = fdt_osc_read_bool(_LSE, "st,bypass");
Yann Gautiere4a3c352019-02-14 10:53:33 +01001795 digbyp = fdt_osc_read_bool(_LSE, "st,digbypass");
Yann Gautier9aea69e2018-07-24 17:13:36 +02001796 lse_css = fdt_osc_read_bool(_LSE, "st,css");
1797 lsedrv = fdt_osc_read_uint32_default(_LSE, "st,drive",
1798 LSEDRV_MEDIUM_HIGH);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001799 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001800 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001801 if (stm32mp1_osc[_HSE] != 0U) {
1802 bool bypass, digbyp, css;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001803
Yann Gautiere4a3c352019-02-14 10:53:33 +01001804 bypass = fdt_osc_read_bool(_HSE, "st,bypass");
1805 digbyp = fdt_osc_read_bool(_HSE, "st,digbypass");
1806 css = fdt_osc_read_bool(_HSE, "st,css");
1807 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001808 }
1809 /*
1810 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1811 * => switch on CSI even if node is not present in device tree
1812 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001813 stm32mp1_csi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001814
1815 /* Come back to HSI */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001816 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001817 if (ret != 0) {
1818 return ret;
1819 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001820 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001821 if (ret != 0) {
1822 return ret;
1823 }
Yann Gautiered342322019-02-15 17:33:27 +01001824 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1825 if (ret != 0) {
1826 return ret;
1827 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001828
Yann Gautiere4a3c352019-02-14 10:53:33 +01001829 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1830 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
1831 pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
1832 clksrc[CLKSRC_PLL3],
1833 pllcfg[_PLL3],
1834 plloff[_PLL3]);
1835 pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
1836 clksrc[CLKSRC_PLL4],
1837 pllcfg[_PLL4],
1838 plloff[_PLL4]);
1839 }
1840
Yann Gautier9aea69e2018-07-24 17:13:36 +02001841 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001842 if (((i == _PLL3) && pll3_preserve) ||
1843 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001844 continue;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001845 }
1846
1847 ret = stm32mp1_pll_stop(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001848 if (ret != 0) {
1849 return ret;
1850 }
1851 }
1852
1853 /* Configure HSIDIV */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001854 if (stm32mp1_osc[_HSI] != 0U) {
1855 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001856 if (ret != 0) {
1857 return ret;
1858 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001859 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001860 }
1861
1862 /* Select DIV */
1863 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001864 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001865 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001866 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001867 if (ret != 0) {
1868 return ret;
1869 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001870 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001871 if (ret != 0) {
1872 return ret;
1873 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001874 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001875 if (ret != 0) {
1876 return ret;
1877 }
Yann Gautiered342322019-02-15 17:33:27 +01001878 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
1879 if (ret != 0) {
1880 return ret;
1881 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001882 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001883 if (ret != 0) {
1884 return ret;
1885 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001886 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001887 if (ret != 0) {
1888 return ret;
1889 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001890 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001891 if (ret != 0) {
1892 return ret;
1893 }
1894
1895 /* No ready bit for RTC */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001896 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001897 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
1898
1899 /* Configure PLLs source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001900 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001901 if (ret != 0) {
1902 return ret;
1903 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001904
1905 if (!pll3_preserve) {
1906 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
1907 if (ret != 0) {
1908 return ret;
1909 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001910 }
1911
Yann Gautiere4a3c352019-02-14 10:53:33 +01001912 if (!pll4_preserve) {
1913 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
1914 if (ret != 0) {
1915 return ret;
1916 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001917 }
1918
1919 /* Configure and start PLLs */
1920 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1921 uint32_t fracv;
1922 uint32_t csg[PLLCSG_NB];
1923
Yann Gautiere4a3c352019-02-14 10:53:33 +01001924 if (((i == _PLL3) && pll3_preserve) ||
1925 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
1926 continue;
1927 }
1928
Yann Gautier9aea69e2018-07-24 17:13:36 +02001929 if (!fdt_check_node(plloff[i])) {
1930 continue;
1931 }
1932
Yann Gautiere4a3c352019-02-14 10:53:33 +01001933 if ((i == _PLL4) && pll4_bootrom) {
1934 /* Set output divider if not done by the Bootrom */
1935 stm32mp1_pll_config_output(i, pllcfg[i]);
1936 continue;
1937 }
1938
Andre Przywara2d5690c2020-03-26 11:50:33 +00001939 fracv = fdt_read_uint32_default(fdt, plloff[i], "frac", 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001940
Yann Gautiere4a3c352019-02-14 10:53:33 +01001941 ret = stm32mp1_pll_config(i, pllcfg[i], fracv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001942 if (ret != 0) {
1943 return ret;
1944 }
Andre Przywaracc99f3f2020-03-26 12:51:21 +00001945 ret = fdt_read_uint32_array(fdt, plloff[i], "csg",
1946 (uint32_t)PLLCSG_NB, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001947 if (ret == 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001948 stm32mp1_pll_csg(i, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001949 } else if (ret != -FDT_ERR_NOTFOUND) {
1950 return ret;
1951 }
1952
Yann Gautiere4a3c352019-02-14 10:53:33 +01001953 stm32mp1_pll_start(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001954 }
1955 /* Wait and start PLLs ouptut when ready */
1956 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1957 if (!fdt_check_node(plloff[i])) {
1958 continue;
1959 }
1960
Yann Gautiere4a3c352019-02-14 10:53:33 +01001961 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001962 if (ret != 0) {
1963 return ret;
1964 }
1965 }
1966 /* Wait LSE ready before to use it */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001967 if (stm32mp1_osc[_LSE] != 0U) {
1968 stm32mp1_lse_wait();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001969 }
1970
1971 /* Configure with expected clock source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001972 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001973 if (ret != 0) {
1974 return ret;
1975 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001976 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001977 if (ret != 0) {
1978 return ret;
1979 }
Yann Gautiered342322019-02-15 17:33:27 +01001980 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
1981 if (ret != 0) {
1982 return ret;
1983 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001984 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001985
1986 /* Configure PKCK */
1987 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
1988 if (pkcs_cell != NULL) {
1989 bool ckper_disabled = false;
1990 uint32_t j;
1991
Yann Gautier9aea69e2018-07-24 17:13:36 +02001992 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001993 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001994
1995 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
1996 ckper_disabled = true;
1997 continue;
1998 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001999 stm32mp1_pkcs_config(pkcs);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002000 }
2001
2002 /*
2003 * CKPER is source for some peripheral clocks
2004 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2005 * only if previous clock is still ON
2006 * => deactivated CKPER only after switching clock
2007 */
2008 if (ckper_disabled) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002009 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002010 }
2011 }
2012
2013 /* Switch OFF HSI if not found in device-tree */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002014 if (stm32mp1_osc[_HSI] == 0U) {
2015 stm32mp1_hsi_set(false);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002016 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01002017 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02002018
2019 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautiere4a3c352019-02-14 10:53:33 +01002020 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002021 RCC_DDRITFCR_DDRCKMOD_MASK,
2022 RCC_DDRITFCR_DDRCKMOD_SSR <<
2023 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2024
2025 return 0;
2026}
2027
2028static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier9aea69e2018-07-24 17:13:36 +02002029 enum stm32mp_osc_id index)
2030{
2031 uint32_t frequency;
2032
Yann Gautiere4a3c352019-02-14 10:53:33 +01002033 if (fdt_osc_read_freq(name, &frequency) == 0) {
2034 stm32mp1_osc[index] = frequency;
Yann Gautier9aea69e2018-07-24 17:13:36 +02002035 }
2036}
2037
2038static void stm32mp1_osc_init(void)
2039{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002040 enum stm32mp_osc_id i;
2041
2042 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01002043 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02002044 }
2045}
Etienne Carriere1368ada2020-05-13 11:49:49 +02002046
2047#ifdef STM32MP_SHARED_RESOURCES
2048/*
2049 * Get the parent ID of the target parent clock, for tagging as secure
2050 * shared clock dependencies.
2051 */
2052static int get_parent_id_parent(unsigned int parent_id)
2053{
2054 enum stm32mp1_parent_sel s = _UNKNOWN_SEL;
2055 enum stm32mp1_pll_id pll_id;
2056 uint32_t p_sel;
2057 uintptr_t rcc_base = stm32mp_rcc_base();
2058
2059 switch (parent_id) {
2060 case _ACLK:
2061 case _PCLK4:
2062 case _PCLK5:
2063 s = _AXIS_SEL;
2064 break;
2065 case _PLL1_P:
2066 case _PLL1_Q:
2067 case _PLL1_R:
2068 pll_id = _PLL1;
2069 break;
2070 case _PLL2_P:
2071 case _PLL2_Q:
2072 case _PLL2_R:
2073 pll_id = _PLL2;
2074 break;
2075 case _PLL3_P:
2076 case _PLL3_Q:
2077 case _PLL3_R:
2078 pll_id = _PLL3;
2079 break;
2080 case _PLL4_P:
2081 case _PLL4_Q:
2082 case _PLL4_R:
2083 pll_id = _PLL4;
2084 break;
2085 case _PCLK1:
2086 case _PCLK2:
2087 case _HCLK2:
2088 case _HCLK6:
2089 case _CK_PER:
2090 case _CK_MPU:
2091 case _CK_MCU:
2092 case _USB_PHY_48:
2093 /* We do not expect to access these */
2094 panic();
2095 break;
2096 default:
2097 /* Other parents have no parent */
2098 return -1;
2099 }
2100
2101 if (s != _UNKNOWN_SEL) {
2102 const struct stm32mp1_clk_sel *sel = clk_sel_ref(s);
2103
2104 p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) &
2105 sel->msk;
2106
2107 if (p_sel < sel->nb_parent) {
2108 return (int)sel->parent[p_sel];
2109 }
2110 } else {
2111 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
2112
2113 p_sel = mmio_read_32(rcc_base + pll->rckxselr) &
2114 RCC_SELR_REFCLK_SRC_MASK;
2115
2116 if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) {
2117 return (int)pll->refclk[p_sel];
2118 }
2119 }
2120
2121 VERBOSE("No parent selected for %s\n",
2122 stm32mp1_clk_parent_name[parent_id]);
2123
2124 return -1;
2125}
2126
2127static void secure_parent_clocks(unsigned long parent_id)
2128{
2129 int grandparent_id;
2130
2131 switch (parent_id) {
2132 case _PLL3_P:
2133 case _PLL3_Q:
2134 case _PLL3_R:
2135 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2136 break;
2137
2138 /* These clocks are always secure when RCC is secure */
2139 case _ACLK:
2140 case _HCLK2:
2141 case _HCLK6:
2142 case _PCLK4:
2143 case _PCLK5:
2144 case _PLL1_P:
2145 case _PLL1_Q:
2146 case _PLL1_R:
2147 case _PLL2_P:
2148 case _PLL2_Q:
2149 case _PLL2_R:
2150 case _HSI:
2151 case _HSI_KER:
2152 case _LSI:
2153 case _CSI:
2154 case _CSI_KER:
2155 case _HSE:
2156 case _HSE_KER:
2157 case _HSE_KER_DIV2:
2158 case _LSE:
2159 break;
2160
2161 default:
2162 VERBOSE("Cannot secure parent clock %s\n",
2163 stm32mp1_clk_parent_name[parent_id]);
2164 panic();
2165 }
2166
2167 grandparent_id = get_parent_id_parent(parent_id);
2168 if (grandparent_id >= 0) {
2169 secure_parent_clocks(grandparent_id);
2170 }
2171}
2172
2173void stm32mp1_register_clock_parents_secure(unsigned long clock_id)
2174{
2175 int parent_id;
2176
2177 if (!stm32mp1_rcc_is_secure()) {
2178 return;
2179 }
2180
2181 switch (clock_id) {
2182 case PLL1:
2183 case PLL2:
2184 /* PLL1/PLL2 are always secure: nothing to do */
2185 break;
2186 case PLL3:
2187 stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3);
2188 break;
2189 case PLL4:
2190 ERROR("PLL4 cannot be secured\n");
2191 panic();
2192 break;
2193 default:
2194 /* Others are expected gateable clock */
2195 parent_id = stm32mp1_clk_get_parent(clock_id);
2196 if (parent_id < 0) {
2197 INFO("No parent found for clock %lu\n", clock_id);
2198 } else {
2199 secure_parent_clocks(parent_id);
2200 }
2201 break;
2202 }
2203}
2204#endif /* STM32MP_SHARED_RESOURCES */
Yann Gautier9aea69e2018-07-24 17:13:36 +02002205
Yann Gautierc7f9e962019-05-20 14:39:26 +02002206static void sync_earlyboot_clocks_state(void)
2207{
Etienne Carriere2a756c22019-12-08 08:23:35 +01002208 unsigned int idx;
2209 const unsigned long secure_enable[] = {
2210 AXIDCG,
2211 BSEC,
2212 DDRC1, DDRC1LP,
2213 DDRC2, DDRC2LP,
2214 DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP,
2215 DDRPHYC, DDRPHYCLP,
2216 TZC1, TZC2,
2217 TZPC,
2218 STGEN_K,
2219 };
2220
2221 for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) {
2222 stm32mp_clk_enable(secure_enable[idx]);
2223 }
2224
Yann Gautierc7f9e962019-05-20 14:39:26 +02002225 if (!stm32mp_is_single_core()) {
2226 stm32mp1_clk_enable_secure(RTCAPB);
2227 }
2228}
2229
Yann Gautier9aea69e2018-07-24 17:13:36 +02002230int stm32mp1_clk_probe(void)
2231{
Yann Gautier9aea69e2018-07-24 17:13:36 +02002232 stm32mp1_osc_init();
2233
Yann Gautierc7f9e962019-05-20 14:39:26 +02002234 sync_earlyboot_clocks_state();
2235
Yann Gautier9aea69e2018-07-24 17:13:36 +02002236 return 0;
2237}