blob: 855e98b428abddc65cc919626d219708a009b82f [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>
19#include <drivers/delay_timer.h>
20#include <drivers/generic_delay_timer.h>
Yann Gautier4d429472019-02-14 11:15:20 +010021#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <drivers/st/stm32mp1_rcc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <dt-bindings/clock/stm32mp1-clksrc.h>
25#include <lib/mmio.h>
Yann Gautiere4a3c352019-02-14 10:53:33 +010026#include <lib/spinlock.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000027#include <lib/utils_def.h>
28#include <plat/common/platform.h>
29
Yann Gautier2299d572019-02-14 11:14:39 +010030#define MAX_HSI_HZ 64000000
Yann Gautiere4a3c352019-02-14 10:53:33 +010031#define USB_PHY_48_MHZ 48000000
Yann Gautier9aea69e2018-07-24 17:13:36 +020032
Yann Gautier2299d572019-02-14 11:14:39 +010033#define TIMEOUT_US_200MS U(200000)
34#define TIMEOUT_US_1S U(1000000)
Yann Gautier9aea69e2018-07-24 17:13:36 +020035
Yann Gautier2299d572019-02-14 11:14:39 +010036#define PLLRDY_TIMEOUT TIMEOUT_US_200MS
37#define CLKSRC_TIMEOUT TIMEOUT_US_200MS
38#define CLKDIV_TIMEOUT TIMEOUT_US_200MS
39#define HSIDIV_TIMEOUT TIMEOUT_US_200MS
40#define OSCRDY_TIMEOUT TIMEOUT_US_1S
Yann Gautier9aea69e2018-07-24 17:13:36 +020041
Yann Gautier5f2e8742019-05-17 15:57:56 +020042const char *stm32mp_osc_node_label[NB_OSC] = {
43 [_LSI] = "clk-lsi",
44 [_LSE] = "clk-lse",
45 [_HSI] = "clk-hsi",
46 [_HSE] = "clk-hse",
47 [_CSI] = "clk-csi",
48 [_I2S_CKIN] = "i2s_ckin",
49};
50
Yann Gautier9aea69e2018-07-24 17:13:36 +020051enum stm32mp1_parent_id {
52/* Oscillators are defined in enum stm32mp_osc_id */
53
54/* Other parent source */
55 _HSI_KER = NB_OSC,
56 _HSE_KER,
57 _HSE_KER_DIV2,
58 _CSI_KER,
59 _PLL1_P,
60 _PLL1_Q,
61 _PLL1_R,
62 _PLL2_P,
63 _PLL2_Q,
64 _PLL2_R,
65 _PLL3_P,
66 _PLL3_Q,
67 _PLL3_R,
68 _PLL4_P,
69 _PLL4_Q,
70 _PLL4_R,
71 _ACLK,
72 _PCLK1,
73 _PCLK2,
74 _PCLK3,
75 _PCLK4,
76 _PCLK5,
77 _HCLK6,
78 _HCLK2,
79 _CK_PER,
80 _CK_MPU,
Yann Gautiered342322019-02-15 17:33:27 +010081 _CK_MCU,
Yann Gautiere4a3c352019-02-14 10:53:33 +010082 _USB_PHY_48,
Yann Gautier9aea69e2018-07-24 17:13:36 +020083 _PARENT_NB,
84 _UNKNOWN_ID = 0xff,
85};
86
Yann Gautiere4a3c352019-02-14 10:53:33 +010087/* Lists only the parent clock we are interested in */
Yann Gautier9aea69e2018-07-24 17:13:36 +020088enum stm32mp1_parent_sel {
Yann Gautiere4a3c352019-02-14 10:53:33 +010089 _I2C12_SEL,
90 _I2C35_SEL,
91 _STGEN_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +020092 _I2C46_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010093 _SPI6_SEL,
Yann Gautier9d8bbcd2019-05-07 18:49:33 +020094 _UART1_SEL,
Yann Gautiere4a3c352019-02-14 10:53:33 +010095 _RNG1_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +020096 _UART6_SEL,
97 _UART24_SEL,
98 _UART35_SEL,
99 _UART78_SEL,
100 _SDMMC12_SEL,
101 _SDMMC3_SEL,
102 _QSPI_SEL,
103 _FMC_SEL,
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200104 _AXIS_SEL,
105 _MCUS_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200106 _USBPHY_SEL,
107 _USBO_SEL,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200108 _PARENT_SEL_NB,
109 _UNKNOWN_SEL = 0xff,
110};
111
112enum stm32mp1_pll_id {
113 _PLL1,
114 _PLL2,
115 _PLL3,
116 _PLL4,
117 _PLL_NB
118};
119
120enum stm32mp1_div_id {
121 _DIV_P,
122 _DIV_Q,
123 _DIV_R,
124 _DIV_NB,
125};
126
127enum stm32mp1_clksrc_id {
128 CLKSRC_MPU,
129 CLKSRC_AXI,
Yann Gautiered342322019-02-15 17:33:27 +0100130 CLKSRC_MCU,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200131 CLKSRC_PLL12,
132 CLKSRC_PLL3,
133 CLKSRC_PLL4,
134 CLKSRC_RTC,
135 CLKSRC_MCO1,
136 CLKSRC_MCO2,
137 CLKSRC_NB
138};
139
140enum stm32mp1_clkdiv_id {
141 CLKDIV_MPU,
142 CLKDIV_AXI,
Yann Gautiered342322019-02-15 17:33:27 +0100143 CLKDIV_MCU,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200144 CLKDIV_APB1,
145 CLKDIV_APB2,
146 CLKDIV_APB3,
147 CLKDIV_APB4,
148 CLKDIV_APB5,
149 CLKDIV_RTC,
150 CLKDIV_MCO1,
151 CLKDIV_MCO2,
152 CLKDIV_NB
153};
154
155enum stm32mp1_pllcfg {
156 PLLCFG_M,
157 PLLCFG_N,
158 PLLCFG_P,
159 PLLCFG_Q,
160 PLLCFG_R,
161 PLLCFG_O,
162 PLLCFG_NB
163};
164
165enum stm32mp1_pllcsg {
166 PLLCSG_MOD_PER,
167 PLLCSG_INC_STEP,
168 PLLCSG_SSCG_MODE,
169 PLLCSG_NB
170};
171
172enum stm32mp1_plltype {
173 PLL_800,
174 PLL_1600,
175 PLL_TYPE_NB
176};
177
178struct stm32mp1_pll {
179 uint8_t refclk_min;
180 uint8_t refclk_max;
181 uint8_t divn_max;
182};
183
184struct stm32mp1_clk_gate {
185 uint16_t offset;
186 uint8_t bit;
187 uint8_t index;
188 uint8_t set_clr;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100189 uint8_t sel; /* Relates to enum stm32mp1_parent_sel */
190 uint8_t fixed; /* Relates to enum stm32mp1_parent_id */
Yann Gautier9aea69e2018-07-24 17:13:36 +0200191};
192
193struct stm32mp1_clk_sel {
194 uint16_t offset;
195 uint8_t src;
196 uint8_t msk;
197 uint8_t nb_parent;
198 const uint8_t *parent;
199};
200
201#define REFCLK_SIZE 4
202struct stm32mp1_clk_pll {
203 enum stm32mp1_plltype plltype;
204 uint16_t rckxselr;
205 uint16_t pllxcfgr1;
206 uint16_t pllxcfgr2;
207 uint16_t pllxfracr;
208 uint16_t pllxcr;
209 uint16_t pllxcsgr;
210 enum stm32mp_osc_id refclk[REFCLK_SIZE];
211};
212
Yann Gautiere4a3c352019-02-14 10:53:33 +0100213/* Clocks with selectable source and non set/clr register access */
214#define _CLK_SELEC(off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200215 { \
216 .offset = (off), \
217 .bit = (b), \
218 .index = (idx), \
219 .set_clr = 0, \
220 .sel = (s), \
221 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200222 }
223
Yann Gautiere4a3c352019-02-14 10:53:33 +0100224/* Clocks with fixed source and non set/clr register access */
225#define _CLK_FIXED(off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200226 { \
227 .offset = (off), \
228 .bit = (b), \
229 .index = (idx), \
230 .set_clr = 0, \
231 .sel = _UNKNOWN_SEL, \
232 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200233 }
234
Yann Gautiere4a3c352019-02-14 10:53:33 +0100235/* Clocks with selectable source and set/clr register access */
236#define _CLK_SC_SELEC(off, b, idx, s) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200237 { \
238 .offset = (off), \
239 .bit = (b), \
240 .index = (idx), \
241 .set_clr = 1, \
242 .sel = (s), \
243 .fixed = _UNKNOWN_ID, \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200244 }
245
Yann Gautiere4a3c352019-02-14 10:53:33 +0100246/* Clocks with fixed source and set/clr register access */
247#define _CLK_SC_FIXED(off, b, idx, f) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200248 { \
249 .offset = (off), \
250 .bit = (b), \
251 .index = (idx), \
252 .set_clr = 1, \
253 .sel = _UNKNOWN_SEL, \
254 .fixed = (f), \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200255 }
256
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200257#define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \
258 [_ ## _label ## _SEL] = { \
259 .offset = _rcc_selr, \
260 .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \
261 .msk = _rcc_selr ## _ ## _label ## SRC_MASK, \
262 .parent = (_parents), \
263 .nb_parent = ARRAY_SIZE(_parents) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200264 }
265
Yann Gautiere4a3c352019-02-14 10:53:33 +0100266#define _CLK_PLL(idx, type, off1, off2, off3, \
267 off4, off5, off6, \
268 p1, p2, p3, p4) \
Yann Gautier9aea69e2018-07-24 17:13:36 +0200269 [(idx)] = { \
270 .plltype = (type), \
271 .rckxselr = (off1), \
272 .pllxcfgr1 = (off2), \
273 .pllxcfgr2 = (off3), \
274 .pllxfracr = (off4), \
275 .pllxcr = (off5), \
276 .pllxcsgr = (off6), \
277 .refclk[0] = (p1), \
278 .refclk[1] = (p2), \
279 .refclk[2] = (p3), \
280 .refclk[3] = (p4), \
281 }
282
283static const uint8_t stm32mp1_clks[][2] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100284 { CK_PER, _CK_PER },
285 { CK_MPU, _CK_MPU },
286 { CK_AXI, _ACLK },
Yann Gautiered342322019-02-15 17:33:27 +0100287 { CK_MCU, _CK_MCU },
Yann Gautiere4a3c352019-02-14 10:53:33 +0100288 { CK_HSE, _HSE },
289 { CK_CSI, _CSI },
290 { CK_LSI, _LSI },
291 { CK_LSE, _LSE },
292 { CK_HSI, _HSI },
293 { CK_HSE_DIV2, _HSE_KER_DIV2 },
Yann Gautier9aea69e2018-07-24 17:13:36 +0200294};
295
Yann Gautiere4a3c352019-02-14 10:53:33 +0100296#define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate)
297
Yann Gautier9aea69e2018-07-24 17:13:36 +0200298static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100299 _CLK_FIXED(RCC_DDRITFCR, 0, DDRC1, _ACLK),
300 _CLK_FIXED(RCC_DDRITFCR, 1, DDRC1LP, _ACLK),
301 _CLK_FIXED(RCC_DDRITFCR, 2, DDRC2, _ACLK),
302 _CLK_FIXED(RCC_DDRITFCR, 3, DDRC2LP, _ACLK),
303 _CLK_FIXED(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
304 _CLK_FIXED(RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R),
305 _CLK_FIXED(RCC_DDRITFCR, 6, DDRCAPB, _PCLK4),
306 _CLK_FIXED(RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4),
307 _CLK_FIXED(RCC_DDRITFCR, 8, AXIDCG, _ACLK),
308 _CLK_FIXED(RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4),
309 _CLK_FIXED(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4),
310
311 _CLK_SC_FIXED(RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1),
312 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
313 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
314 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
315 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
316 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
317 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
318 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
319 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
320 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
321 _CLK_SC_SELEC(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
322
323 _CLK_SC_FIXED(RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2),
324 _CLK_SC_SELEC(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
325
326 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
327 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
328 _CLK_SC_SELEC(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
329
330 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
331 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
332 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200333 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
Yann Gautiere4a3c352019-02-14 10:53:33 +0100334 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
335 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5),
336 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5),
337 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5),
338 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5),
339 _CLK_SC_FIXED(RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5),
340 _CLK_SC_SELEC(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
341
342 _CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
343 _CLK_SC_SELEC(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
344
345 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
346 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
347 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
348 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
349 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
350 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
351 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
352 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
353 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
354 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
355 _CLK_SC_SELEC(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
356
357 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5),
358 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5),
359 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5),
360 _CLK_SC_SELEC(RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL),
361 _CLK_SC_FIXED(RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5),
362
363 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
364 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
365 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
366 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
367 _CLK_SC_SELEC(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
368
369 _CLK_SELEC(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
370};
371
372static const uint8_t i2c12_parents[] = {
373 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
374};
375
376static const uint8_t i2c35_parents[] = {
377 _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER
378};
379
380static const uint8_t stgen_parents[] = {
381 _HSI_KER, _HSE_KER
382};
383
384static const uint8_t i2c46_parents[] = {
385 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER
386};
387
388static const uint8_t spi6_parents[] = {
389 _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q
390};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200391
Yann Gautiere4a3c352019-02-14 10:53:33 +0100392static const uint8_t usart1_parents[] = {
393 _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER
394};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200395
Yann Gautiere4a3c352019-02-14 10:53:33 +0100396static const uint8_t rng1_parents[] = {
397 _CSI, _PLL4_R, _LSE, _LSI
398};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200399
Yann Gautiere4a3c352019-02-14 10:53:33 +0100400static const uint8_t uart6_parents[] = {
401 _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
402};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200403
Yann Gautiere4a3c352019-02-14 10:53:33 +0100404static const uint8_t uart234578_parents[] = {
405 _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER
406};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200407
Yann Gautiere4a3c352019-02-14 10:53:33 +0100408static const uint8_t sdmmc12_parents[] = {
409 _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER
410};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200411
Yann Gautiere4a3c352019-02-14 10:53:33 +0100412static const uint8_t sdmmc3_parents[] = {
413 _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER
414};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200415
Yann Gautiere4a3c352019-02-14 10:53:33 +0100416static const uint8_t qspi_parents[] = {
417 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
418};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200419
Yann Gautiere4a3c352019-02-14 10:53:33 +0100420static const uint8_t fmc_parents[] = {
421 _ACLK, _PLL3_R, _PLL4_P, _CK_PER
422};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200423
Yann Gautiere4a3c352019-02-14 10:53:33 +0100424static const uint8_t ass_parents[] = {
425 _HSI, _HSE, _PLL2
Yann Gautier9aea69e2018-07-24 17:13:36 +0200426};
427
Yann Gautiered342322019-02-15 17:33:27 +0100428static const uint8_t mss_parents[] = {
429 _HSI, _HSE, _CSI, _PLL3
430};
431
Yann Gautiere4a3c352019-02-14 10:53:33 +0100432static const uint8_t usbphy_parents[] = {
433 _HSE_KER, _PLL4_R, _HSE_KER_DIV2
434};
435
436static const uint8_t usbo_parents[] = {
437 _PLL4_R, _USB_PHY_48
438};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200439
440static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200441 _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents),
442 _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents),
443 _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents),
444 _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents),
445 _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents),
446 _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents),
447 _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents),
448 _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents),
449 _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents),
450 _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents),
451 _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents),
452 _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents),
453 _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents),
454 _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents),
455 _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents),
456 _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, ass_parents),
457 _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mss_parents),
458 _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents),
459 _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200460};
461
462/* Define characteristic of PLL according type */
463#define DIVN_MIN 24
464static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
465 [PLL_800] = {
466 .refclk_min = 4,
467 .refclk_max = 16,
468 .divn_max = 99,
469 },
470 [PLL_1600] = {
471 .refclk_min = 8,
472 .refclk_max = 16,
473 .divn_max = 199,
474 },
475};
476
477/* PLLNCFGR2 register divider by output */
478static const uint8_t pllncfgr2[_DIV_NB] = {
479 [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT,
480 [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT,
Yann Gautiere4a3c352019-02-14 10:53:33 +0100481 [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200482};
483
484static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100485 _CLK_PLL(_PLL1, PLL_1600,
486 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
487 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
488 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
489 _CLK_PLL(_PLL2, PLL_1600,
490 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
491 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
492 _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID),
493 _CLK_PLL(_PLL3, PLL_800,
494 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
495 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
496 _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID),
497 _CLK_PLL(_PLL4, PLL_800,
498 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
499 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
500 _HSI, _HSE, _CSI, _I2S_CKIN),
Yann Gautier9aea69e2018-07-24 17:13:36 +0200501};
502
503/* Prescaler table lookups for clock computation */
Yann Gautiered342322019-02-15 17:33:27 +0100504/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
505static const uint8_t stm32mp1_mcu_div[16] = {
506 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
507};
Yann Gautier9aea69e2018-07-24 17:13:36 +0200508
509/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */
510#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
511#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
512static const uint8_t stm32mp1_mpu_apbx_div[8] = {
513 0, 1, 2, 3, 4, 4, 4, 4
514};
515
516/* div = /1 /2 /3 /4 */
517static const uint8_t stm32mp1_axi_div[8] = {
518 1, 2, 3, 4, 4, 4, 4, 4
519};
520
Yann Gautiere4a3c352019-02-14 10:53:33 +0100521/* RCC clock device driver private */
522static unsigned long stm32mp1_osc[NB_OSC];
523static struct spinlock reg_lock;
524static unsigned int gate_refcounts[NB_GATES];
525static struct spinlock refcount_lock;
526
527static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx)
528{
529 return &stm32mp1_clk_gate[idx];
530}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200531
Yann Gautiere4a3c352019-02-14 10:53:33 +0100532static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx)
533{
534 return &stm32mp1_clk_sel[idx];
535}
Yann Gautier9aea69e2018-07-24 17:13:36 +0200536
Yann Gautiere4a3c352019-02-14 10:53:33 +0100537static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx)
538{
539 return &stm32mp1_clk_pll[idx];
540}
541
542static int stm32mp1_lock_available(void)
543{
544 /* The spinlocks are used only when MMU is enabled */
545 return (read_sctlr() & SCTLR_M_BIT) && (read_sctlr() & SCTLR_C_BIT);
546}
547
548static void stm32mp1_clk_lock(struct spinlock *lock)
549{
550 if (stm32mp1_lock_available() == 0U) {
551 return;
552 }
553
554 /* Assume interrupts are masked */
555 spin_lock(lock);
556}
557
558static void stm32mp1_clk_unlock(struct spinlock *lock)
559{
560 if (stm32mp1_lock_available() == 0U) {
561 return;
562 }
563
564 spin_unlock(lock);
565}
566
567bool stm32mp1_rcc_is_secure(void)
568{
569 uintptr_t rcc_base = stm32mp_rcc_base();
570
571 return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_TZEN) != 0;
572}
573
Yann Gautiered342322019-02-15 17:33:27 +0100574bool stm32mp1_rcc_is_mckprot(void)
575{
576 uintptr_t rcc_base = stm32mp_rcc_base();
577
578 return (mmio_read_32(rcc_base + RCC_TZCR) & RCC_TZCR_MCKPROT) != 0;
579}
580
Yann Gautiere4a3c352019-02-14 10:53:33 +0100581void stm32mp1_clk_rcc_regs_lock(void)
582{
583 stm32mp1_clk_lock(&reg_lock);
584}
585
586void stm32mp1_clk_rcc_regs_unlock(void)
587{
588 stm32mp1_clk_unlock(&reg_lock);
589}
590
591static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200592{
593 if (idx >= NB_OSC) {
594 return 0;
595 }
596
Yann Gautiere4a3c352019-02-14 10:53:33 +0100597 return stm32mp1_osc[idx];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200598}
599
Yann Gautiere4a3c352019-02-14 10:53:33 +0100600static int stm32mp1_clk_get_gated_id(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200601{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100602 unsigned int i;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200603
Yann Gautiere4a3c352019-02-14 10:53:33 +0100604 for (i = 0U; i < NB_GATES; i++) {
605 if (gate_ref(i)->index == id) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200606 return i;
607 }
608 }
609
610 ERROR("%s: clk id %d not found\n", __func__, (uint32_t)id);
611
612 return -EINVAL;
613}
614
Yann Gautiere4a3c352019-02-14 10:53:33 +0100615static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200616{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100617 return (enum stm32mp1_parent_sel)(gate_ref(i)->sel);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200618}
619
Yann Gautiere4a3c352019-02-14 10:53:33 +0100620static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200621{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100622 return (enum stm32mp1_parent_id)(gate_ref(i)->fixed);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200623}
624
Yann Gautiere4a3c352019-02-14 10:53:33 +0100625static int stm32mp1_clk_get_parent(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200626{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100627 const struct stm32mp1_clk_sel *sel;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200628 uint32_t j, p_sel;
629 int i;
630 enum stm32mp1_parent_id p;
631 enum stm32mp1_parent_sel s;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100632 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200633
Yann Gautiere4a3c352019-02-14 10:53:33 +0100634 for (j = 0U; j < ARRAY_SIZE(stm32mp1_clks); j++) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200635 if (stm32mp1_clks[j][0] == id) {
636 return (int)stm32mp1_clks[j][1];
637 }
638 }
639
Yann Gautiere4a3c352019-02-14 10:53:33 +0100640 i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200641 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100642 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200643 }
644
Yann Gautiere4a3c352019-02-14 10:53:33 +0100645 p = stm32mp1_clk_get_fixed_parent(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200646 if (p < _PARENT_NB) {
647 return (int)p;
648 }
649
Yann Gautiere4a3c352019-02-14 10:53:33 +0100650 s = stm32mp1_clk_get_sel(i);
651 if (s == _UNKNOWN_SEL) {
Yann Gautier9aea69e2018-07-24 17:13:36 +0200652 return -EINVAL;
653 }
Yann Gautiere4a3c352019-02-14 10:53:33 +0100654 if (s >= _PARENT_SEL_NB) {
655 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200656 }
657
Yann Gautiere4a3c352019-02-14 10:53:33 +0100658 sel = clk_sel_ref(s);
Yann Gautier9d8bbcd2019-05-07 18:49:33 +0200659 p_sel = (mmio_read_32(rcc_base + sel->offset) & sel->msk) >> sel->src;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100660 if (p_sel < sel->nb_parent) {
661 return (int)sel->parent[p_sel];
662 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200663
664 return -EINVAL;
665}
666
Yann Gautiere4a3c352019-02-14 10:53:33 +0100667static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200668{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100669 uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr);
670 uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200671
Yann Gautiere4a3c352019-02-14 10:53:33 +0100672 return stm32mp1_clk_get_fixed(pll->refclk[src]);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200673}
674
675/*
676 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
677 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
678 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
679 * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
680 */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100681static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200682{
Yann Gautier9aea69e2018-07-24 17:13:36 +0200683 unsigned long refclk, fvco;
684 uint32_t cfgr1, fracr, divm, divn;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100685 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200686
Yann Gautiere4a3c352019-02-14 10:53:33 +0100687 cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1);
688 fracr = mmio_read_32(rcc_base + pll->pllxfracr);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200689
690 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
691 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
692
Yann Gautiere4a3c352019-02-14 10:53:33 +0100693 refclk = stm32mp1_pll_get_fref(pll);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200694
695 /*
696 * With FRACV :
697 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
698 * Without FRACV
699 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
700 */
701 if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100702 uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >>
703 RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200704 unsigned long long numerator, denominator;
705
Yann Gautiere4a3c352019-02-14 10:53:33 +0100706 numerator = (((unsigned long long)divn + 1U) << 13) + fracv;
707 numerator = refclk * numerator;
708 denominator = ((unsigned long long)divm + 1U) << 13;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200709 fvco = (unsigned long)(numerator / denominator);
710 } else {
711 fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U));
712 }
713
714 return fvco;
715}
716
Yann Gautiere4a3c352019-02-14 10:53:33 +0100717static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +0200718 enum stm32mp1_div_id div_id)
719{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100720 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200721 unsigned long dfout;
722 uint32_t cfgr2, divy;
723
724 if (div_id >= _DIV_NB) {
725 return 0;
726 }
727
Yann Gautiere4a3c352019-02-14 10:53:33 +0100728 cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200729 divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK;
730
Yann Gautiere4a3c352019-02-14 10:53:33 +0100731 dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200732
733 return dfout;
734}
735
Yann Gautiere4a3c352019-02-14 10:53:33 +0100736static unsigned long get_clock_rate(int p)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200737{
738 uint32_t reg, clkdiv;
739 unsigned long clock = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100740 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200741
742 switch (p) {
743 case _CK_MPU:
744 /* MPU sub system */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100745 reg = mmio_read_32(rcc_base + RCC_MPCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200746 switch (reg & RCC_SELR_SRC_MASK) {
747 case RCC_MPCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100748 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200749 break;
750 case RCC_MPCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100751 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200752 break;
753 case RCC_MPCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100754 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200755 break;
756 case RCC_MPCKSELR_PLL_MPUDIV:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100757 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200758
Yann Gautiere4a3c352019-02-14 10:53:33 +0100759 reg = mmio_read_32(rcc_base + RCC_MPCKDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200760 clkdiv = reg & RCC_MPUDIV_MASK;
761 if (clkdiv != 0U) {
762 clock /= stm32mp1_mpu_div[clkdiv];
763 }
Yann Gautier9aea69e2018-07-24 17:13:36 +0200764 break;
765 default:
766 break;
767 }
768 break;
769 /* AXI sub system */
770 case _ACLK:
771 case _HCLK2:
772 case _HCLK6:
773 case _PCLK4:
774 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100775 reg = mmio_read_32(rcc_base + RCC_ASSCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200776 switch (reg & RCC_SELR_SRC_MASK) {
777 case RCC_ASSCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100778 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200779 break;
780 case RCC_ASSCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100781 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200782 break;
783 case RCC_ASSCKSELR_PLL:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100784 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200785 break;
786 default:
787 break;
788 }
789
790 /* System clock divider */
Yann Gautiere4a3c352019-02-14 10:53:33 +0100791 reg = mmio_read_32(rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200792 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
793
794 switch (p) {
795 case _PCLK4:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100796 reg = mmio_read_32(rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200797 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
798 break;
799 case _PCLK5:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100800 reg = mmio_read_32(rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200801 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
802 break;
803 default:
804 break;
805 }
806 break;
Yann Gautiered342322019-02-15 17:33:27 +0100807 /* MCU sub system */
808 case _CK_MCU:
809 case _PCLK1:
810 case _PCLK2:
811 case _PCLK3:
812 reg = mmio_read_32(rcc_base + RCC_MSSCKSELR);
813 switch (reg & RCC_SELR_SRC_MASK) {
814 case RCC_MSSCKSELR_HSI:
815 clock = stm32mp1_clk_get_fixed(_HSI);
816 break;
817 case RCC_MSSCKSELR_HSE:
818 clock = stm32mp1_clk_get_fixed(_HSE);
819 break;
820 case RCC_MSSCKSELR_CSI:
821 clock = stm32mp1_clk_get_fixed(_CSI);
822 break;
823 case RCC_MSSCKSELR_PLL:
824 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
825 break;
826 default:
827 break;
828 }
829
830 /* MCU clock divider */
831 reg = mmio_read_32(rcc_base + RCC_MCUDIVR);
832 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
833
834 switch (p) {
835 case _PCLK1:
836 reg = mmio_read_32(rcc_base + RCC_APB1DIVR);
837 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
838 break;
839 case _PCLK2:
840 reg = mmio_read_32(rcc_base + RCC_APB2DIVR);
841 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
842 break;
843 case _PCLK3:
844 reg = mmio_read_32(rcc_base + RCC_APB3DIVR);
845 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
846 break;
847 case _CK_MCU:
848 default:
849 break;
850 }
851 break;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200852 case _CK_PER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100853 reg = mmio_read_32(rcc_base + RCC_CPERCKSELR);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200854 switch (reg & RCC_SELR_SRC_MASK) {
855 case RCC_CPERCKSELR_HSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100856 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200857 break;
858 case RCC_CPERCKSELR_HSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100859 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200860 break;
861 case RCC_CPERCKSELR_CSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100862 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200863 break;
864 default:
865 break;
866 }
867 break;
868 case _HSI:
869 case _HSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100870 clock = stm32mp1_clk_get_fixed(_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200871 break;
872 case _CSI:
873 case _CSI_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100874 clock = stm32mp1_clk_get_fixed(_CSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200875 break;
876 case _HSE:
877 case _HSE_KER:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100878 clock = stm32mp1_clk_get_fixed(_HSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200879 break;
880 case _HSE_KER_DIV2:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100881 clock = stm32mp1_clk_get_fixed(_HSE) >> 1;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200882 break;
883 case _LSI:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100884 clock = stm32mp1_clk_get_fixed(_LSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200885 break;
886 case _LSE:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100887 clock = stm32mp1_clk_get_fixed(_LSE);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200888 break;
889 /* PLL */
890 case _PLL1_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100891 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200892 break;
893 case _PLL1_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100894 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200895 break;
896 case _PLL1_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100897 clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200898 break;
899 case _PLL2_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100900 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200901 break;
902 case _PLL2_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100903 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200904 break;
905 case _PLL2_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100906 clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200907 break;
908 case _PLL3_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100909 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200910 break;
911 case _PLL3_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100912 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200913 break;
914 case _PLL3_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100915 clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200916 break;
917 case _PLL4_P:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100918 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200919 break;
920 case _PLL4_Q:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100921 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200922 break;
923 case _PLL4_R:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100924 clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200925 break;
926 /* Other */
927 case _USB_PHY_48:
Yann Gautiere4a3c352019-02-14 10:53:33 +0100928 clock = USB_PHY_48_MHZ;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200929 break;
930 default:
931 break;
932 }
933
934 return clock;
935}
936
Yann Gautiere4a3c352019-02-14 10:53:33 +0100937static void __clk_enable(struct stm32mp1_clk_gate const *gate)
938{
939 uintptr_t rcc_base = stm32mp_rcc_base();
940
941 if (gate->set_clr != 0U) {
942 mmio_write_32(rcc_base + gate->offset, BIT(gate->bit));
943 } else {
944 mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit));
945 }
946
947 VERBOSE("Clock %d has been enabled", gate->index);
948}
949
950static void __clk_disable(struct stm32mp1_clk_gate const *gate)
951{
952 uintptr_t rcc_base = stm32mp_rcc_base();
953
954 if (gate->set_clr != 0U) {
955 mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET,
956 BIT(gate->bit));
957 } else {
958 mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit));
959 }
960
961 VERBOSE("Clock %d has been disabled", gate->index);
962}
963
964static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate)
965{
966 uintptr_t rcc_base = stm32mp_rcc_base();
967
968 return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit);
969}
970
971unsigned int stm32mp1_clk_get_refcount(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200972{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100973 int i = stm32mp1_clk_get_gated_id(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +0200974
975 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100976 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200977 }
978
Yann Gautiere4a3c352019-02-14 10:53:33 +0100979 return gate_refcounts[i];
Yann Gautier9aea69e2018-07-24 17:13:36 +0200980}
981
Yann Gautiere4a3c352019-02-14 10:53:33 +0100982void __stm32mp1_clk_enable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +0200983{
Yann Gautiere4a3c352019-02-14 10:53:33 +0100984 const struct stm32mp1_clk_gate *gate;
985 int i = stm32mp1_clk_get_gated_id(id);
986 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +0200987
988 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +0100989 ERROR("Clock %d can't be enabled\n", (uint32_t)id);
990 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +0200991 }
992
Yann Gautiere4a3c352019-02-14 10:53:33 +0100993 gate = gate_ref(i);
994 refcnt = &gate_refcounts[i];
995
996 stm32mp1_clk_lock(&refcount_lock);
997
998 if (stm32mp_incr_shrefcnt(refcnt, secure) != 0) {
999 __clk_enable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001000 }
1001
Yann Gautiere4a3c352019-02-14 10:53:33 +01001002 stm32mp1_clk_unlock(&refcount_lock);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001003}
1004
Yann Gautiere4a3c352019-02-14 10:53:33 +01001005void __stm32mp1_clk_disable(unsigned long id, bool secure)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001006{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001007 const struct stm32mp1_clk_gate *gate;
1008 int i = stm32mp1_clk_get_gated_id(id);
1009 unsigned int *refcnt;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001010
1011 if (i < 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001012 ERROR("Clock %d can't be disabled\n", (uint32_t)id);
1013 panic();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001014 }
1015
Yann Gautiere4a3c352019-02-14 10:53:33 +01001016 gate = gate_ref(i);
1017 refcnt = &gate_refcounts[i];
1018
1019 stm32mp1_clk_lock(&refcount_lock);
1020
1021 if (stm32mp_decr_shrefcnt(refcnt, secure) != 0) {
1022 __clk_disable(gate);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001023 }
1024
Yann Gautiere4a3c352019-02-14 10:53:33 +01001025 stm32mp1_clk_unlock(&refcount_lock);
1026}
1027
1028void stm32mp_clk_enable(unsigned long id)
1029{
1030 __stm32mp1_clk_enable(id, true);
1031}
1032
1033void stm32mp_clk_disable(unsigned long id)
1034{
1035 __stm32mp1_clk_disable(id, true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001036}
1037
Yann Gautiere4a3c352019-02-14 10:53:33 +01001038bool stm32mp_clk_is_enabled(unsigned long id)
1039{
1040 int i = stm32mp1_clk_get_gated_id(id);
1041
1042 if (i < 0) {
1043 panic();
1044 }
1045
1046 return __clk_is_enabled(gate_ref(i));
1047}
1048
Yann Gautiera2e2a302019-02-14 11:13:39 +01001049unsigned long stm32mp_clk_get_rate(unsigned long id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001050{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001051 int p = stm32mp1_clk_get_parent(id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001052
1053 if (p < 0) {
1054 return 0;
1055 }
1056
Yann Gautiere4a3c352019-02-14 10:53:33 +01001057 return get_clock_rate(p);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001058}
1059
Yann Gautiere4a3c352019-02-14 10:53:33 +01001060static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001061{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001062 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001063
Yann Gautiere4a3c352019-02-14 10:53:33 +01001064 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001065 mmio_setbits_32(address, mask_on);
1066 } else {
1067 mmio_clrbits_32(address, mask_on);
1068 }
1069}
1070
Yann Gautiere4a3c352019-02-14 10:53:33 +01001071static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001072{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001073 uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR;
1074 uintptr_t address = stm32mp_rcc_base() + offset;
1075
1076 mmio_write_32(address, mask_on);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001077}
1078
Yann Gautiere4a3c352019-02-14 10:53:33 +01001079static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001080{
Yann Gautier2299d572019-02-14 11:14:39 +01001081 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001082 uint32_t mask_test;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001083 uintptr_t address = stm32mp_rcc_base() + offset;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001084
Yann Gautiere4a3c352019-02-14 10:53:33 +01001085 if (enable) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001086 mask_test = mask_rdy;
1087 } else {
1088 mask_test = 0;
1089 }
1090
Yann Gautier2299d572019-02-14 11:14:39 +01001091 timeout = timeout_init_us(OSCRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001092 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
Yann Gautier2299d572019-02-14 11:14:39 +01001093 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001094 ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001095 mask_rdy, address, enable, mmio_read_32(address));
1096 return -ETIMEDOUT;
1097 }
1098 }
1099
1100 return 0;
1101}
1102
Yann Gautiere4a3c352019-02-14 10:53:33 +01001103static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001104{
1105 uint32_t value;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001106 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001107
Yann Gautiere4a3c352019-02-14 10:53:33 +01001108 if (digbyp) {
1109 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001110 }
1111
Yann Gautiere4a3c352019-02-14 10:53:33 +01001112 if (bypass || digbyp) {
1113 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP);
1114 }
1115
Yann Gautier9aea69e2018-07-24 17:13:36 +02001116 /*
1117 * Warning: not recommended to switch directly from "high drive"
1118 * to "medium low drive", and vice-versa.
1119 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001120 value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >>
Yann Gautier9aea69e2018-07-24 17:13:36 +02001121 RCC_BDCR_LSEDRV_SHIFT;
1122
1123 while (value != lsedrv) {
1124 if (value > lsedrv) {
1125 value--;
1126 } else {
1127 value++;
1128 }
1129
Yann Gautiere4a3c352019-02-14 10:53:33 +01001130 mmio_clrsetbits_32(rcc_base + RCC_BDCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001131 RCC_BDCR_LSEDRV_MASK,
1132 value << RCC_BDCR_LSEDRV_SHIFT);
1133 }
1134
Yann Gautiere4a3c352019-02-14 10:53:33 +01001135 stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001136}
1137
Yann Gautiere4a3c352019-02-14 10:53:33 +01001138static void stm32mp1_lse_wait(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001139{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001140 if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001141 VERBOSE("%s: failed\n", __func__);
1142 }
1143}
1144
Yann Gautiere4a3c352019-02-14 10:53:33 +01001145static void stm32mp1_lsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001146{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001147 stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION);
1148
1149 if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001150 VERBOSE("%s: failed\n", __func__);
1151 }
1152}
1153
Yann Gautiere4a3c352019-02-14 10:53:33 +01001154static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001155{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001156 uintptr_t rcc_base = stm32mp_rcc_base();
1157
1158 if (digbyp) {
1159 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001160 }
1161
Yann Gautiere4a3c352019-02-14 10:53:33 +01001162 if (bypass || digbyp) {
1163 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1164 }
1165
1166 stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON);
1167 if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001168 VERBOSE("%s: failed\n", __func__);
1169 }
1170
1171 if (css) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001172 mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001173 }
1174}
1175
Yann Gautiere4a3c352019-02-14 10:53:33 +01001176static void stm32mp1_csi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001177{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001178 stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION);
1179 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001180 VERBOSE("%s: failed\n", __func__);
1181 }
1182}
1183
Yann Gautiere4a3c352019-02-14 10:53:33 +01001184static void stm32mp1_hsi_set(bool enable)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001185{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001186 stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION);
1187 if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001188 VERBOSE("%s: failed\n", __func__);
1189 }
1190}
1191
Yann Gautiere4a3c352019-02-14 10:53:33 +01001192static int stm32mp1_set_hsidiv(uint8_t hsidiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001193{
Yann Gautier2299d572019-02-14 11:14:39 +01001194 uint64_t timeout;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001195 uintptr_t rcc_base = stm32mp_rcc_base();
1196 uintptr_t address = rcc_base + RCC_OCRDYR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001197
Yann Gautiere4a3c352019-02-14 10:53:33 +01001198 mmio_clrsetbits_32(rcc_base + RCC_HSICFGR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001199 RCC_HSICFGR_HSIDIV_MASK,
1200 RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv);
1201
Yann Gautier2299d572019-02-14 11:14:39 +01001202 timeout = timeout_init_us(HSIDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001203 while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001204 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001205 ERROR("HSIDIV failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001206 address, mmio_read_32(address));
1207 return -ETIMEDOUT;
1208 }
1209 }
1210
1211 return 0;
1212}
1213
Yann Gautiere4a3c352019-02-14 10:53:33 +01001214static int stm32mp1_hsidiv(unsigned long hsifreq)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001215{
1216 uint8_t hsidiv;
1217 uint32_t hsidivfreq = MAX_HSI_HZ;
1218
1219 for (hsidiv = 0; hsidiv < 4U; hsidiv++) {
1220 if (hsidivfreq == hsifreq) {
1221 break;
1222 }
1223
1224 hsidivfreq /= 2U;
1225 }
1226
1227 if (hsidiv == 4U) {
1228 ERROR("Invalid clk-hsi frequency\n");
1229 return -1;
1230 }
1231
1232 if (hsidiv != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001233 return stm32mp1_set_hsidiv(hsidiv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001234 }
1235
1236 return 0;
1237}
1238
Yann Gautiere4a3c352019-02-14 10:53:33 +01001239static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id,
1240 unsigned int clksrc,
1241 uint32_t *pllcfg, int plloff)
1242{
1243 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1244 uintptr_t rcc_base = stm32mp_rcc_base();
1245 uintptr_t pllxcr = rcc_base + pll->pllxcr;
1246 enum stm32mp1_plltype type = pll->plltype;
1247 uintptr_t clksrc_address = rcc_base + (clksrc >> 4);
1248 unsigned long refclk;
1249 uint32_t ifrge = 0U;
1250 uint32_t src, value, fracv;
1251
1252 /* Check PLL output */
1253 if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) {
1254 return false;
1255 }
1256
1257 /* Check current clksrc */
1258 src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK;
1259 if (src != (clksrc & RCC_SELR_SRC_MASK)) {
1260 return false;
1261 }
1262
1263 /* Check Div */
1264 src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK;
1265
1266 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
1267 (pllcfg[PLLCFG_M] + 1U);
1268
1269 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1270 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1271 return false;
1272 }
1273
1274 if ((type == PLL_800) && (refclk >= 8000000U)) {
1275 ifrge = 1U;
1276 }
1277
1278 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) &
1279 RCC_PLLNCFGR1_DIVN_MASK;
1280 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) &
1281 RCC_PLLNCFGR1_DIVM_MASK;
1282 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) &
1283 RCC_PLLNCFGR1_IFRGE_MASK;
1284 if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) {
1285 return false;
1286 }
1287
1288 /* Fractional configuration */
1289 fracv = fdt_read_uint32_default(plloff, "frac", 0);
1290
1291 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
1292 value |= RCC_PLLNFRACR_FRACLE;
1293 if (mmio_read_32(rcc_base + pll->pllxfracr) != value) {
1294 return false;
1295 }
1296
1297 /* Output config */
1298 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1299 RCC_PLLNCFGR2_DIVP_MASK;
1300 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1301 RCC_PLLNCFGR2_DIVQ_MASK;
1302 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1303 RCC_PLLNCFGR2_DIVR_MASK;
1304 if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) {
1305 return false;
1306 }
1307
1308 return true;
1309}
1310
1311static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001312{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001313 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1314 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001315
Yann Gautiere4a3c352019-02-14 10:53:33 +01001316 mmio_write_32(pllxcr, RCC_PLLNCR_PLLON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001317}
1318
Yann Gautiere4a3c352019-02-14 10:53:33 +01001319static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001320{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001321 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1322 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001323 uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001324
Yann Gautier9aea69e2018-07-24 17:13:36 +02001325 /* Wait PLL lock */
1326 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001327 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001328 ERROR("PLL%d start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001329 pll_id, pllxcr, mmio_read_32(pllxcr));
1330 return -ETIMEDOUT;
1331 }
1332 }
1333
1334 /* Start the requested output */
1335 mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1336
1337 return 0;
1338}
1339
Yann Gautiere4a3c352019-02-14 10:53:33 +01001340static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001341{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001342 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1343 uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr;
Yann Gautier2299d572019-02-14 11:14:39 +01001344 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001345
1346 /* Stop all output */
1347 mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1348 RCC_PLLNCR_DIVREN);
1349
1350 /* Stop PLL */
1351 mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON);
1352
Yann Gautier2299d572019-02-14 11:14:39 +01001353 timeout = timeout_init_us(PLLRDY_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001354 /* Wait PLL stopped */
1355 while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001356 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001357 ERROR("PLL%d stop failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001358 pll_id, pllxcr, mmio_read_32(pllxcr));
1359 return -ETIMEDOUT;
1360 }
1361 }
1362
1363 return 0;
1364}
1365
Yann Gautiere4a3c352019-02-14 10:53:33 +01001366static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001367 uint32_t *pllcfg)
1368{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001369 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1370 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001371 uint32_t value;
1372
1373 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) &
1374 RCC_PLLNCFGR2_DIVP_MASK;
1375 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) &
1376 RCC_PLLNCFGR2_DIVQ_MASK;
1377 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) &
1378 RCC_PLLNCFGR2_DIVR_MASK;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001379 mmio_write_32(rcc_base + pll->pllxcfgr2, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001380}
1381
Yann Gautiere4a3c352019-02-14 10:53:33 +01001382static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001383 uint32_t *pllcfg, uint32_t fracv)
1384{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001385 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
1386 uintptr_t rcc_base = stm32mp_rcc_base();
1387 enum stm32mp1_plltype type = pll->plltype;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001388 unsigned long refclk;
1389 uint32_t ifrge = 0;
1390 uint32_t src, value;
1391
Yann Gautiere4a3c352019-02-14 10:53:33 +01001392 src = mmio_read_32(rcc_base + pll->rckxselr) &
Yann Gautier9aea69e2018-07-24 17:13:36 +02001393 RCC_SELR_REFCLK_SRC_MASK;
1394
Yann Gautiere4a3c352019-02-14 10:53:33 +01001395 refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) /
Yann Gautier9aea69e2018-07-24 17:13:36 +02001396 (pllcfg[PLLCFG_M] + 1U);
1397
1398 if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) ||
1399 (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) {
1400 return -EINVAL;
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;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001413 mmio_write_32(rcc_base + pll->pllxcfgr1, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001414
1415 /* Fractional configuration */
1416 value = 0;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001417 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001418
1419 value = fracv << RCC_PLLNFRACR_FRACV_SHIFT;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001420 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001421
1422 value |= RCC_PLLNFRACR_FRACLE;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001423 mmio_write_32(rcc_base + pll->pllxfracr, value);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001424
Yann Gautiere4a3c352019-02-14 10:53:33 +01001425 stm32mp1_pll_config_output(pll_id, pllcfg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001426
1427 return 0;
1428}
1429
Yann Gautiere4a3c352019-02-14 10:53:33 +01001430static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001431{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001432 const struct stm32mp1_clk_pll *pll = pll_ref(pll_id);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001433 uint32_t pllxcsg = 0;
1434
1435 pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1436 RCC_PLLNCSGR_MOD_PER_MASK;
1437
1438 pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1439 RCC_PLLNCSGR_INC_STEP_MASK;
1440
1441 pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1442 RCC_PLLNCSGR_SSCG_MODE_MASK;
1443
Yann Gautiere4a3c352019-02-14 10:53:33 +01001444 mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001445}
1446
Yann Gautiere4a3c352019-02-14 10:53:33 +01001447static int stm32mp1_set_clksrc(unsigned int clksrc)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001448{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001449 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier2299d572019-02-14 11:14:39 +01001450 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001451
Yann Gautiere4a3c352019-02-14 10:53:33 +01001452 mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001453 clksrc & RCC_SELR_SRC_MASK);
1454
Yann Gautier2299d572019-02-14 11:14:39 +01001455 timeout = timeout_init_us(CLKSRC_TIMEOUT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001456 while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001457 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001458 ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc,
1459 clksrc_address, mmio_read_32(clksrc_address));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001460 return -ETIMEDOUT;
1461 }
1462 }
1463
1464 return 0;
1465}
1466
Yann Gautiere4a3c352019-02-14 10:53:33 +01001467static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001468{
Yann Gautier2299d572019-02-14 11:14:39 +01001469 uint64_t timeout;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001470
1471 mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK,
1472 clkdiv & RCC_DIVR_DIV_MASK);
1473
Yann Gautier2299d572019-02-14 11:14:39 +01001474 timeout = timeout_init_us(CLKDIV_TIMEOUT);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001475 while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +01001476 if (timeout_elapsed(timeout)) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001477 ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n",
Yann Gautier9aea69e2018-07-24 17:13:36 +02001478 clkdiv, address, mmio_read_32(address));
1479 return -ETIMEDOUT;
1480 }
1481 }
1482
1483 return 0;
1484}
1485
Yann Gautiere4a3c352019-02-14 10:53:33 +01001486static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001487{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001488 uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001489
1490 /*
1491 * Binding clksrc :
1492 * bit15-4 offset
1493 * bit3: disable
1494 * bit2-0: MCOSEL[2:0]
1495 */
1496 if ((clksrc & 0x8U) != 0U) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001497 mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001498 } else {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001499 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001500 RCC_MCOCFG_MCOSRC_MASK,
1501 clksrc & RCC_MCOCFG_MCOSRC_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001502 mmio_clrsetbits_32(clksrc_address,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001503 RCC_MCOCFG_MCODIV_MASK,
1504 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001505 mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001506 }
1507}
1508
Yann Gautiere4a3c352019-02-14 10:53:33 +01001509static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001510{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001511 uintptr_t address = stm32mp_rcc_base() + RCC_BDCR;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001512
1513 if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) ||
1514 (clksrc != (uint32_t)CLK_RTC_DISABLED)) {
1515 mmio_clrsetbits_32(address,
1516 RCC_BDCR_RTCSRC_MASK,
1517 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1518
1519 mmio_setbits_32(address, RCC_BDCR_RTCCKEN);
1520 }
1521
1522 if (lse_css) {
1523 mmio_setbits_32(address, RCC_BDCR_LSECSSON);
1524 }
1525}
1526
Yann Gautiere4a3c352019-02-14 10:53:33 +01001527static void stm32mp1_stgen_config(void)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001528{
1529 uintptr_t stgen;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001530 uint32_t cntfid0;
1531 unsigned long rate;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001532 unsigned long long counter;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001533
1534 stgen = fdt_get_stgen_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001535 cntfid0 = mmio_read_32(stgen + CNTFID_OFF);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001536 rate = get_clock_rate(stm32mp1_clk_get_parent(STGEN_K));
Yann Gautier9aea69e2018-07-24 17:13:36 +02001537
Yann Gautiere4a3c352019-02-14 10:53:33 +01001538 if (cntfid0 == rate) {
1539 return;
1540 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001541
Yann Gautiere4a3c352019-02-14 10:53:33 +01001542 mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1543 counter = (unsigned long long)mmio_read_32(stgen + CNTCVL_OFF);
1544 counter |= ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF)) << 32;
1545 counter = (counter * rate / cntfid0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001546
Yann Gautiere4a3c352019-02-14 10:53:33 +01001547 mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)counter);
1548 mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(counter >> 32));
1549 mmio_write_32(stgen + CNTFID_OFF, rate);
1550 mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001551
Yann Gautiere4a3c352019-02-14 10:53:33 +01001552 write_cntfrq((u_register_t)rate);
1553
1554 /* Need to update timer with new frequency */
1555 generic_delay_timer_init();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001556}
1557
1558void stm32mp1_stgen_increment(unsigned long long offset_in_ms)
1559{
1560 uintptr_t stgen;
1561 unsigned long long cnt;
1562
1563 stgen = fdt_get_stgen_base();
1564
1565 cnt = ((unsigned long long)mmio_read_32(stgen + CNTCVU_OFF) << 32) |
1566 mmio_read_32(stgen + CNTCVL_OFF);
1567
1568 cnt += (offset_in_ms * mmio_read_32(stgen + CNTFID_OFF)) / 1000U;
1569
1570 mmio_clrbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1571 mmio_write_32(stgen + CNTCVL_OFF, (uint32_t)cnt);
1572 mmio_write_32(stgen + CNTCVU_OFF, (uint32_t)(cnt >> 32));
1573 mmio_setbits_32(stgen + CNTCR_OFF, CNTCR_EN);
1574}
1575
Yann Gautiere4a3c352019-02-14 10:53:33 +01001576static void stm32mp1_pkcs_config(uint32_t pkcs)
Yann Gautier9aea69e2018-07-24 17:13:36 +02001577{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001578 uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001579 uint32_t value = pkcs & 0xFU;
1580 uint32_t mask = 0xFU;
1581
1582 if ((pkcs & BIT(31)) != 0U) {
1583 mask <<= 4;
1584 value <<= 4;
1585 }
1586
1587 mmio_clrsetbits_32(address, mask, value);
1588}
1589
1590int stm32mp1_clk_init(void)
1591{
Yann Gautiere4a3c352019-02-14 10:53:33 +01001592 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001593 unsigned int clksrc[CLKSRC_NB];
1594 unsigned int clkdiv[CLKDIV_NB];
1595 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1596 int plloff[_PLL_NB];
1597 int ret, len;
1598 enum stm32mp1_pll_id i;
1599 bool lse_css = false;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001600 bool pll3_preserve = false;
1601 bool pll4_preserve = false;
1602 bool pll4_bootrom = false;
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001603 const fdt32_t *pkcs_cell;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001604
1605 /* Check status field to disable security */
1606 if (!fdt_get_rcc_secure_status()) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001607 mmio_write_32(rcc_base + RCC_TZCR, 0);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001608 }
1609
1610 ret = fdt_rcc_read_uint32_array("st,clksrc", clksrc,
1611 (uint32_t)CLKSRC_NB);
1612 if (ret < 0) {
1613 return -FDT_ERR_NOTFOUND;
1614 }
1615
1616 ret = fdt_rcc_read_uint32_array("st,clkdiv", clkdiv,
1617 (uint32_t)CLKDIV_NB);
1618 if (ret < 0) {
1619 return -FDT_ERR_NOTFOUND;
1620 }
1621
1622 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1623 char name[12];
1624
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01001625 snprintf(name, sizeof(name), "st,pll@%d", i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001626 plloff[i] = fdt_rcc_subnode_offset(name);
1627
1628 if (!fdt_check_node(plloff[i])) {
1629 continue;
1630 }
1631
1632 ret = fdt_read_uint32_array(plloff[i], "cfg",
1633 pllcfg[i], (int)PLLCFG_NB);
1634 if (ret < 0) {
1635 return -FDT_ERR_NOTFOUND;
1636 }
1637 }
1638
Yann Gautiere4a3c352019-02-14 10:53:33 +01001639 stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1640 stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001641
1642 /*
1643 * Switch ON oscillator found in device-tree.
1644 * Note: HSI already ON after BootROM stage.
1645 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001646 if (stm32mp1_osc[_LSI] != 0U) {
1647 stm32mp1_lsi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001648 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001649 if (stm32mp1_osc[_LSE] != 0U) {
1650 bool bypass, digbyp;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001651 uint32_t lsedrv;
1652
1653 bypass = fdt_osc_read_bool(_LSE, "st,bypass");
Yann Gautiere4a3c352019-02-14 10:53:33 +01001654 digbyp = fdt_osc_read_bool(_LSE, "st,digbypass");
Yann Gautier9aea69e2018-07-24 17:13:36 +02001655 lse_css = fdt_osc_read_bool(_LSE, "st,css");
1656 lsedrv = fdt_osc_read_uint32_default(_LSE, "st,drive",
1657 LSEDRV_MEDIUM_HIGH);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001658 stm32mp1_lse_enable(bypass, digbyp, lsedrv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001659 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001660 if (stm32mp1_osc[_HSE] != 0U) {
1661 bool bypass, digbyp, css;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001662
Yann Gautiere4a3c352019-02-14 10:53:33 +01001663 bypass = fdt_osc_read_bool(_HSE, "st,bypass");
1664 digbyp = fdt_osc_read_bool(_HSE, "st,digbypass");
1665 css = fdt_osc_read_bool(_HSE, "st,css");
1666 stm32mp1_hse_enable(bypass, digbyp, css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001667 }
1668 /*
1669 * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1670 * => switch on CSI even if node is not present in device tree
1671 */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001672 stm32mp1_csi_set(true);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001673
1674 /* Come back to HSI */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001675 ret = stm32mp1_set_clksrc(CLK_MPU_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001676 if (ret != 0) {
1677 return ret;
1678 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001679 ret = stm32mp1_set_clksrc(CLK_AXI_HSI);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001680 if (ret != 0) {
1681 return ret;
1682 }
Yann Gautiered342322019-02-15 17:33:27 +01001683 ret = stm32mp1_set_clksrc(CLK_MCU_HSI);
1684 if (ret != 0) {
1685 return ret;
1686 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001687
Yann Gautiere4a3c352019-02-14 10:53:33 +01001688 if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) &
1689 RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) {
1690 pll3_preserve = stm32mp1_check_pll_conf(_PLL3,
1691 clksrc[CLKSRC_PLL3],
1692 pllcfg[_PLL3],
1693 plloff[_PLL3]);
1694 pll4_preserve = stm32mp1_check_pll_conf(_PLL4,
1695 clksrc[CLKSRC_PLL4],
1696 pllcfg[_PLL4],
1697 plloff[_PLL4]);
1698 }
1699
Yann Gautier9aea69e2018-07-24 17:13:36 +02001700 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001701 if (((i == _PLL3) && pll3_preserve) ||
1702 ((i == _PLL4) && pll4_preserve)) {
Yann Gautier9aea69e2018-07-24 17:13:36 +02001703 continue;
Yann Gautiere4a3c352019-02-14 10:53:33 +01001704 }
1705
1706 ret = stm32mp1_pll_stop(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001707 if (ret != 0) {
1708 return ret;
1709 }
1710 }
1711
1712 /* Configure HSIDIV */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001713 if (stm32mp1_osc[_HSI] != 0U) {
1714 ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001715 if (ret != 0) {
1716 return ret;
1717 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001718 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001719 }
1720
1721 /* Select DIV */
1722 /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001723 mmio_write_32(rcc_base + RCC_MPCKDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001724 clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK);
Yann Gautiere4a3c352019-02-14 10:53:33 +01001725 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001726 if (ret != 0) {
1727 return ret;
1728 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001729 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001730 if (ret != 0) {
1731 return ret;
1732 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001733 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001734 if (ret != 0) {
1735 return ret;
1736 }
Yann Gautiered342322019-02-15 17:33:27 +01001737 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR);
1738 if (ret != 0) {
1739 return ret;
1740 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001741 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001742 if (ret != 0) {
1743 return ret;
1744 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001745 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001746 if (ret != 0) {
1747 return ret;
1748 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001749 ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001750 if (ret != 0) {
1751 return ret;
1752 }
1753
1754 /* No ready bit for RTC */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001755 mmio_write_32(rcc_base + RCC_RTCDIVR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001756 clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK);
1757
1758 /* Configure PLLs source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001759 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001760 if (ret != 0) {
1761 return ret;
1762 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001763
1764 if (!pll3_preserve) {
1765 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]);
1766 if (ret != 0) {
1767 return ret;
1768 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001769 }
1770
Yann Gautiere4a3c352019-02-14 10:53:33 +01001771 if (!pll4_preserve) {
1772 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]);
1773 if (ret != 0) {
1774 return ret;
1775 }
Yann Gautier9aea69e2018-07-24 17:13:36 +02001776 }
1777
1778 /* Configure and start PLLs */
1779 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1780 uint32_t fracv;
1781 uint32_t csg[PLLCSG_NB];
1782
Yann Gautiere4a3c352019-02-14 10:53:33 +01001783 if (((i == _PLL3) && pll3_preserve) ||
1784 ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) {
1785 continue;
1786 }
1787
Yann Gautier9aea69e2018-07-24 17:13:36 +02001788 if (!fdt_check_node(plloff[i])) {
1789 continue;
1790 }
1791
Yann Gautiere4a3c352019-02-14 10:53:33 +01001792 if ((i == _PLL4) && pll4_bootrom) {
1793 /* Set output divider if not done by the Bootrom */
1794 stm32mp1_pll_config_output(i, pllcfg[i]);
1795 continue;
1796 }
1797
Yann Gautier9aea69e2018-07-24 17:13:36 +02001798 fracv = fdt_read_uint32_default(plloff[i], "frac", 0);
1799
Yann Gautiere4a3c352019-02-14 10:53:33 +01001800 ret = stm32mp1_pll_config(i, pllcfg[i], fracv);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001801 if (ret != 0) {
1802 return ret;
1803 }
1804 ret = fdt_read_uint32_array(plloff[i], "csg", csg,
1805 (uint32_t)PLLCSG_NB);
1806 if (ret == 0) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001807 stm32mp1_pll_csg(i, csg);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001808 } else if (ret != -FDT_ERR_NOTFOUND) {
1809 return ret;
1810 }
1811
Yann Gautiere4a3c352019-02-14 10:53:33 +01001812 stm32mp1_pll_start(i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001813 }
1814 /* Wait and start PLLs ouptut when ready */
1815 for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) {
1816 if (!fdt_check_node(plloff[i])) {
1817 continue;
1818 }
1819
Yann Gautiere4a3c352019-02-14 10:53:33 +01001820 ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001821 if (ret != 0) {
1822 return ret;
1823 }
1824 }
1825 /* Wait LSE ready before to use it */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001826 if (stm32mp1_osc[_LSE] != 0U) {
1827 stm32mp1_lse_wait();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001828 }
1829
1830 /* Configure with expected clock source */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001831 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001832 if (ret != 0) {
1833 return ret;
1834 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001835 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001836 if (ret != 0) {
1837 return ret;
1838 }
Yann Gautiered342322019-02-15 17:33:27 +01001839 ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]);
1840 if (ret != 0) {
1841 return ret;
1842 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001843 stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001844
1845 /* Configure PKCK */
1846 pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len);
1847 if (pkcs_cell != NULL) {
1848 bool ckper_disabled = false;
1849 uint32_t j;
1850
Yann Gautier9aea69e2018-07-24 17:13:36 +02001851 for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) {
Yann Gautierf9af3bc2018-11-09 15:57:18 +01001852 uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001853
1854 if (pkcs == (uint32_t)CLK_CKPER_DISABLED) {
1855 ckper_disabled = true;
1856 continue;
1857 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001858 stm32mp1_pkcs_config(pkcs);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001859 }
1860
1861 /*
1862 * CKPER is source for some peripheral clocks
1863 * (FMC-NAND / QPSI-NOR) and switching source is allowed
1864 * only if previous clock is still ON
1865 * => deactivated CKPER only after switching clock
1866 */
1867 if (ckper_disabled) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001868 stm32mp1_pkcs_config(CLK_CKPER_DISABLED);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001869 }
1870 }
1871
1872 /* Switch OFF HSI if not found in device-tree */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001873 if (stm32mp1_osc[_HSI] == 0U) {
1874 stm32mp1_hsi_set(false);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001875 }
Yann Gautiere4a3c352019-02-14 10:53:33 +01001876 stm32mp1_stgen_config();
Yann Gautier9aea69e2018-07-24 17:13:36 +02001877
1878 /* Software Self-Refresh mode (SSR) during DDR initilialization */
Yann Gautiere4a3c352019-02-14 10:53:33 +01001879 mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001880 RCC_DDRITFCR_DDRCKMOD_MASK,
1881 RCC_DDRITFCR_DDRCKMOD_SSR <<
1882 RCC_DDRITFCR_DDRCKMOD_SHIFT);
1883
1884 return 0;
1885}
1886
1887static void stm32mp1_osc_clk_init(const char *name,
Yann Gautier9aea69e2018-07-24 17:13:36 +02001888 enum stm32mp_osc_id index)
1889{
1890 uint32_t frequency;
1891
Yann Gautiere4a3c352019-02-14 10:53:33 +01001892 if (fdt_osc_read_freq(name, &frequency) == 0) {
1893 stm32mp1_osc[index] = frequency;
Yann Gautier9aea69e2018-07-24 17:13:36 +02001894 }
1895}
1896
1897static void stm32mp1_osc_init(void)
1898{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001899 enum stm32mp_osc_id i;
1900
1901 for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) {
Yann Gautiere4a3c352019-02-14 10:53:33 +01001902 stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i);
Yann Gautier9aea69e2018-07-24 17:13:36 +02001903 }
1904}
1905
1906int stm32mp1_clk_probe(void)
1907{
Yann Gautier9aea69e2018-07-24 17:13:36 +02001908 stm32mp1_osc_init();
1909
1910 return 0;
1911}