blob: 204ac170531dca93b16f3dd5db97268b9fbf6660 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01004 */
5
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01006#define LOG_CATEGORY UCLASS_CLK
7
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01008#include <clk-uclass.h>
9#include <div64.h>
10#include <dm.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010013#include <regmap.h>
14#include <spl.h>
15#include <syscon.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070016#include <time.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070017#include <vsprintf.h>
Patrick Delaunay885bdc22020-05-25 12:19:44 +020018#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Patrick Delaunay30cd91e2020-11-06 19:01:45 +010020#include <dm/device_compat.h>
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010021#include <dt-bindings/clock/stm32mp1-clks.h>
Patrick Delaunayf11398e2018-03-12 10:46:16 +010022#include <dt-bindings/clock/stm32mp1-clksrc.h>
Patrick Delaunay30cd91e2020-11-06 19:01:45 +010023#include <linux/bitops.h>
24#include <linux/io.h>
25#include <linux/iopoll.h>
Patrick Delaunayf11398e2018-03-12 10:46:16 +010026
Patrick Delaunaya77c6ed2019-07-30 19:16:55 +020027DECLARE_GLOBAL_DATA_PTR;
28
Patrick Delaunay72a57622021-10-11 09:52:50 +020029#if defined(CONFIG_SPL_BUILD)
Patrick Delaunayf11398e2018-03-12 10:46:16 +010030/* activate clock tree initialization in the driver */
31#define STM32MP1_CLOCK_TREE_INIT
32#endif
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010033
34#define MAX_HSI_HZ 64000000
35
Patrick Delaunayf11398e2018-03-12 10:46:16 +010036/* TIMEOUT */
37#define TIMEOUT_200MS 200000
38#define TIMEOUT_1S 1000000
39
Patrick Delaunaybf7d9442018-03-20 11:41:25 +010040/* STGEN registers */
41#define STGENC_CNTCR 0x00
42#define STGENC_CNTSR 0x04
43#define STGENC_CNTCVL 0x08
44#define STGENC_CNTCVU 0x0C
45#define STGENC_CNTFID0 0x20
46
47#define STGENC_CNTCR_EN BIT(0)
48
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010049/* RCC registers */
50#define RCC_OCENSETR 0x0C
51#define RCC_OCENCLRR 0x10
52#define RCC_HSICFGR 0x18
53#define RCC_MPCKSELR 0x20
54#define RCC_ASSCKSELR 0x24
55#define RCC_RCK12SELR 0x28
56#define RCC_MPCKDIVR 0x2C
57#define RCC_AXIDIVR 0x30
58#define RCC_APB4DIVR 0x3C
59#define RCC_APB5DIVR 0x40
60#define RCC_RTCDIVR 0x44
61#define RCC_MSSCKSELR 0x48
62#define RCC_PLL1CR 0x80
63#define RCC_PLL1CFGR1 0x84
64#define RCC_PLL1CFGR2 0x88
65#define RCC_PLL1FRACR 0x8C
66#define RCC_PLL1CSGR 0x90
67#define RCC_PLL2CR 0x94
68#define RCC_PLL2CFGR1 0x98
69#define RCC_PLL2CFGR2 0x9C
70#define RCC_PLL2FRACR 0xA0
71#define RCC_PLL2CSGR 0xA4
72#define RCC_I2C46CKSELR 0xC0
Patrick Delaunaydcd705e2021-07-09 14:24:34 +020073#define RCC_SPI6CKSELR 0xC4
Anatolij Gustschinb62c75d2023-09-29 13:34:37 +020074#define RCC_UART1CKSELR 0xC8
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010075#define RCC_CPERCKSELR 0xD0
76#define RCC_STGENCKSELR 0xD4
77#define RCC_DDRITFCR 0xD8
78#define RCC_BDCR 0x140
79#define RCC_RDLSICR 0x144
80#define RCC_MP_APB4ENSETR 0x200
81#define RCC_MP_APB5ENSETR 0x208
82#define RCC_MP_AHB5ENSETR 0x210
83#define RCC_MP_AHB6ENSETR 0x218
84#define RCC_OCRDYR 0x808
85#define RCC_DBGCFGR 0x80C
86#define RCC_RCK3SELR 0x820
87#define RCC_RCK4SELR 0x824
88#define RCC_MCUDIVR 0x830
89#define RCC_APB1DIVR 0x834
90#define RCC_APB2DIVR 0x838
91#define RCC_APB3DIVR 0x83C
92#define RCC_PLL3CR 0x880
93#define RCC_PLL3CFGR1 0x884
94#define RCC_PLL3CFGR2 0x888
95#define RCC_PLL3FRACR 0x88C
96#define RCC_PLL3CSGR 0x890
97#define RCC_PLL4CR 0x894
98#define RCC_PLL4CFGR1 0x898
99#define RCC_PLL4CFGR2 0x89C
100#define RCC_PLL4FRACR 0x8A0
101#define RCC_PLL4CSGR 0x8A4
102#define RCC_I2C12CKSELR 0x8C0
103#define RCC_I2C35CKSELR 0x8C4
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200104#define RCC_SPI2S1CKSELR 0x8D8
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200105#define RCC_SPI2S23CKSELR 0x8DC
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100106#define RCC_SPI45CKSELR 0x8E0
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100107#define RCC_UART6CKSELR 0x8E4
108#define RCC_UART24CKSELR 0x8E8
109#define RCC_UART35CKSELR 0x8EC
110#define RCC_UART78CKSELR 0x8F0
111#define RCC_SDMMC12CKSELR 0x8F4
112#define RCC_SDMMC3CKSELR 0x8F8
113#define RCC_ETHCKSELR 0x8FC
114#define RCC_QSPICKSELR 0x900
115#define RCC_FMCCKSELR 0x904
116#define RCC_USBCKSELR 0x91C
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200117#define RCC_DSICKSELR 0x924
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200118#define RCC_ADCCKSELR 0x928
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100119#define RCC_MP_APB1ENSETR 0xA00
120#define RCC_MP_APB2ENSETR 0XA08
Fabrice Gasnier4cb3b532018-04-26 17:00:47 +0200121#define RCC_MP_APB3ENSETR 0xA10
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100122#define RCC_MP_AHB2ENSETR 0xA18
Benjamin Gaignard32470812018-11-27 13:49:51 +0100123#define RCC_MP_AHB3ENSETR 0xA20
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100124#define RCC_MP_AHB4ENSETR 0xA28
125
126/* used for most of SELR register */
127#define RCC_SELR_SRC_MASK GENMASK(2, 0)
128#define RCC_SELR_SRCRDY BIT(31)
129
130/* Values of RCC_MPCKSELR register */
131#define RCC_MPCKSELR_HSI 0
132#define RCC_MPCKSELR_HSE 1
133#define RCC_MPCKSELR_PLL 2
134#define RCC_MPCKSELR_PLL_MPUDIV 3
135
136/* Values of RCC_ASSCKSELR register */
137#define RCC_ASSCKSELR_HSI 0
138#define RCC_ASSCKSELR_HSE 1
139#define RCC_ASSCKSELR_PLL 2
140
141/* Values of RCC_MSSCKSELR register */
142#define RCC_MSSCKSELR_HSI 0
143#define RCC_MSSCKSELR_HSE 1
144#define RCC_MSSCKSELR_CSI 2
145#define RCC_MSSCKSELR_PLL 3
146
147/* Values of RCC_CPERCKSELR register */
148#define RCC_CPERCKSELR_HSI 0
149#define RCC_CPERCKSELR_CSI 1
150#define RCC_CPERCKSELR_HSE 2
151
152/* used for most of DIVR register : max div for RTC */
153#define RCC_DIVR_DIV_MASK GENMASK(5, 0)
154#define RCC_DIVR_DIVRDY BIT(31)
155
156/* Masks for specific DIVR registers */
157#define RCC_APBXDIV_MASK GENMASK(2, 0)
158#define RCC_MPUDIV_MASK GENMASK(2, 0)
159#define RCC_AXIDIV_MASK GENMASK(2, 0)
160#define RCC_MCUDIV_MASK GENMASK(3, 0)
161
162/* offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */
163#define RCC_MP_ENCLRR_OFFSET 4
164
165/* Fields of RCC_BDCR register */
166#define RCC_BDCR_LSEON BIT(0)
167#define RCC_BDCR_LSEBYP BIT(1)
168#define RCC_BDCR_LSERDY BIT(2)
Patrick Delaunay80cb5682018-07-16 10:41:46 +0200169#define RCC_BDCR_DIGBYP BIT(3)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100170#define RCC_BDCR_LSEDRV_MASK GENMASK(5, 4)
171#define RCC_BDCR_LSEDRV_SHIFT 4
172#define RCC_BDCR_LSECSSON BIT(8)
173#define RCC_BDCR_RTCCKEN BIT(20)
174#define RCC_BDCR_RTCSRC_MASK GENMASK(17, 16)
175#define RCC_BDCR_RTCSRC_SHIFT 16
176
177/* Fields of RCC_RDLSICR register */
178#define RCC_RDLSICR_LSION BIT(0)
179#define RCC_RDLSICR_LSIRDY BIT(1)
180
181/* used for ALL PLLNCR registers */
182#define RCC_PLLNCR_PLLON BIT(0)
183#define RCC_PLLNCR_PLLRDY BIT(1)
Patrick Delaunay9a6ce2a2019-01-30 13:07:06 +0100184#define RCC_PLLNCR_SSCG_CTRL BIT(2)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100185#define RCC_PLLNCR_DIVPEN BIT(4)
186#define RCC_PLLNCR_DIVQEN BIT(5)
187#define RCC_PLLNCR_DIVREN BIT(6)
188#define RCC_PLLNCR_DIVEN_SHIFT 4
189
190/* used for ALL PLLNCFGR1 registers */
191#define RCC_PLLNCFGR1_DIVM_SHIFT 16
192#define RCC_PLLNCFGR1_DIVM_MASK GENMASK(21, 16)
193#define RCC_PLLNCFGR1_DIVN_SHIFT 0
194#define RCC_PLLNCFGR1_DIVN_MASK GENMASK(8, 0)
195/* only for PLL3 and PLL4 */
196#define RCC_PLLNCFGR1_IFRGE_SHIFT 24
197#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK(25, 24)
198
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200199/* used for ALL PLLNCFGR2 registers , using stm32mp1_div_id */
200#define RCC_PLLNCFGR2_SHIFT(div_id) ((div_id) * 8)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100201#define RCC_PLLNCFGR2_DIVX_MASK GENMASK(6, 0)
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200202#define RCC_PLLNCFGR2_DIVP_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_P)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100203#define RCC_PLLNCFGR2_DIVP_MASK GENMASK(6, 0)
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200204#define RCC_PLLNCFGR2_DIVQ_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_Q)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100205#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK(14, 8)
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200206#define RCC_PLLNCFGR2_DIVR_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_R)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100207#define RCC_PLLNCFGR2_DIVR_MASK GENMASK(22, 16)
208
209/* used for ALL PLLNFRACR registers */
210#define RCC_PLLNFRACR_FRACV_SHIFT 3
211#define RCC_PLLNFRACR_FRACV_MASK GENMASK(15, 3)
212#define RCC_PLLNFRACR_FRACLE BIT(16)
213
214/* used for ALL PLLNCSGR registers */
215#define RCC_PLLNCSGR_INC_STEP_SHIFT 16
216#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK(30, 16)
217#define RCC_PLLNCSGR_MOD_PER_SHIFT 0
218#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK(12, 0)
219#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15
220#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15)
221
222/* used for RCC_OCENSETR and RCC_OCENCLRR registers */
223#define RCC_OCENR_HSION BIT(0)
224#define RCC_OCENR_CSION BIT(4)
Patrick Delaunay80cb5682018-07-16 10:41:46 +0200225#define RCC_OCENR_DIGBYP BIT(7)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100226#define RCC_OCENR_HSEON BIT(8)
227#define RCC_OCENR_HSEBYP BIT(10)
228#define RCC_OCENR_HSECSSON BIT(11)
229
230/* Fields of RCC_OCRDYR register */
231#define RCC_OCRDYR_HSIRDY BIT(0)
232#define RCC_OCRDYR_HSIDIVRDY BIT(2)
233#define RCC_OCRDYR_CSIRDY BIT(4)
234#define RCC_OCRDYR_HSERDY BIT(8)
235
236/* Fields of DDRITFCR register */
237#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
238#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20
239#define RCC_DDRITFCR_DDRCKMOD_SSR 0
240
241/* Fields of RCC_HSICFGR register */
242#define RCC_HSICFGR_HSIDIV_MASK GENMASK(1, 0)
243
244/* used for MCO related operations */
245#define RCC_MCOCFG_MCOON BIT(12)
246#define RCC_MCOCFG_MCODIV_MASK GENMASK(7, 4)
247#define RCC_MCOCFG_MCODIV_SHIFT 4
248#define RCC_MCOCFG_MCOSRC_MASK GENMASK(2, 0)
249
250enum stm32mp1_parent_id {
251/*
252 * _HSI, _HSE, _CSI, _LSI, _LSE should not be moved
Etienne Carriere55a78142021-02-24 11:19:42 +0100253 * they are used as index in osc_clk[] as clock reference
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100254 */
255 _HSI,
256 _HSE,
257 _CSI,
258 _LSI,
259 _LSE,
260 _I2S_CKIN,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100261 NB_OSC,
262
263/* other parent source */
264 _HSI_KER = NB_OSC,
265 _HSE_KER,
266 _HSE_KER_DIV2,
267 _CSI_KER,
268 _PLL1_P,
269 _PLL1_Q,
270 _PLL1_R,
271 _PLL2_P,
272 _PLL2_Q,
273 _PLL2_R,
274 _PLL3_P,
275 _PLL3_Q,
276 _PLL3_R,
277 _PLL4_P,
278 _PLL4_Q,
279 _PLL4_R,
280 _ACLK,
281 _PCLK1,
282 _PCLK2,
283 _PCLK3,
284 _PCLK4,
285 _PCLK5,
286 _HCLK6,
287 _HCLK2,
288 _CK_PER,
289 _CK_MPU,
290 _CK_MCU,
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200291 _DSI_PHY,
Patrick Delaunay7b726532019-01-30 13:07:00 +0100292 _USB_PHY_48,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100293 _PARENT_NB,
294 _UNKNOWN_ID = 0xff,
295};
296
297enum stm32mp1_parent_sel {
298 _I2C12_SEL,
299 _I2C35_SEL,
300 _I2C46_SEL,
301 _UART6_SEL,
302 _UART24_SEL,
303 _UART35_SEL,
304 _UART78_SEL,
305 _SDMMC12_SEL,
306 _SDMMC3_SEL,
307 _ETH_SEL,
308 _QSPI_SEL,
309 _FMC_SEL,
310 _USBPHY_SEL,
311 _USBO_SEL,
312 _STGEN_SEL,
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200313 _DSI_SEL,
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200314 _ADC12_SEL,
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200315 _SPI1_SEL,
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200316 _SPI23_SEL,
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100317 _SPI45_SEL,
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200318 _SPI6_SEL,
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200319 _RTC_SEL,
Anatolij Gustschinb62c75d2023-09-29 13:34:37 +0200320 _UART1_SEL,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100321 _PARENT_SEL_NB,
322 _UNKNOWN_SEL = 0xff,
323};
324
325enum stm32mp1_pll_id {
326 _PLL1,
327 _PLL2,
328 _PLL3,
329 _PLL4,
330 _PLL_NB
331};
332
333enum stm32mp1_div_id {
334 _DIV_P,
335 _DIV_Q,
336 _DIV_R,
337 _DIV_NB,
338};
339
340enum stm32mp1_clksrc_id {
341 CLKSRC_MPU,
342 CLKSRC_AXI,
343 CLKSRC_MCU,
344 CLKSRC_PLL12,
345 CLKSRC_PLL3,
346 CLKSRC_PLL4,
347 CLKSRC_RTC,
348 CLKSRC_MCO1,
349 CLKSRC_MCO2,
350 CLKSRC_NB
351};
352
353enum stm32mp1_clkdiv_id {
354 CLKDIV_MPU,
355 CLKDIV_AXI,
356 CLKDIV_MCU,
357 CLKDIV_APB1,
358 CLKDIV_APB2,
359 CLKDIV_APB3,
360 CLKDIV_APB4,
361 CLKDIV_APB5,
362 CLKDIV_RTC,
363 CLKDIV_MCO1,
364 CLKDIV_MCO2,
365 CLKDIV_NB
366};
367
368enum stm32mp1_pllcfg {
369 PLLCFG_M,
370 PLLCFG_N,
371 PLLCFG_P,
372 PLLCFG_Q,
373 PLLCFG_R,
374 PLLCFG_O,
375 PLLCFG_NB
376};
377
378enum stm32mp1_pllcsg {
379 PLLCSG_MOD_PER,
380 PLLCSG_INC_STEP,
381 PLLCSG_SSCG_MODE,
382 PLLCSG_NB
383};
384
385enum stm32mp1_plltype {
386 PLL_800,
387 PLL_1600,
388 PLL_TYPE_NB
389};
390
391struct stm32mp1_pll {
392 u8 refclk_min;
393 u8 refclk_max;
394 u8 divn_max;
395};
396
397struct stm32mp1_clk_gate {
398 u16 offset;
399 u8 bit;
400 u8 index;
401 u8 set_clr;
402 u8 sel;
403 u8 fixed;
404};
405
406struct stm32mp1_clk_sel {
407 u16 offset;
408 u8 src;
409 u8 msk;
410 u8 nb_parent;
411 const u8 *parent;
412};
413
414#define REFCLK_SIZE 4
415struct stm32mp1_clk_pll {
416 enum stm32mp1_plltype plltype;
417 u16 rckxselr;
418 u16 pllxcfgr1;
419 u16 pllxcfgr2;
420 u16 pllxfracr;
421 u16 pllxcr;
422 u16 pllxcsgr;
423 u8 refclk[REFCLK_SIZE];
424};
425
426struct stm32mp1_clk_data {
427 const struct stm32mp1_clk_gate *gate;
428 const struct stm32mp1_clk_sel *sel;
429 const struct stm32mp1_clk_pll *pll;
430 const int nb_gate;
431};
432
433struct stm32mp1_clk_priv {
434 fdt_addr_t base;
435 const struct stm32mp1_clk_data *data;
Etienne Carriere55a78142021-02-24 11:19:42 +0100436 struct clk osc_clk[NB_OSC];
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100437};
438
439#define STM32MP1_CLK(off, b, idx, s) \
440 { \
441 .offset = (off), \
442 .bit = (b), \
443 .index = (idx), \
444 .set_clr = 0, \
445 .sel = (s), \
446 .fixed = _UNKNOWN_ID, \
447 }
448
449#define STM32MP1_CLK_F(off, b, idx, f) \
450 { \
451 .offset = (off), \
452 .bit = (b), \
453 .index = (idx), \
454 .set_clr = 0, \
455 .sel = _UNKNOWN_SEL, \
456 .fixed = (f), \
457 }
458
459#define STM32MP1_CLK_SET_CLR(off, b, idx, s) \
460 { \
461 .offset = (off), \
462 .bit = (b), \
463 .index = (idx), \
464 .set_clr = 1, \
465 .sel = (s), \
466 .fixed = _UNKNOWN_ID, \
467 }
468
469#define STM32MP1_CLK_SET_CLR_F(off, b, idx, f) \
470 { \
471 .offset = (off), \
472 .bit = (b), \
473 .index = (idx), \
474 .set_clr = 1, \
475 .sel = _UNKNOWN_SEL, \
476 .fixed = (f), \
477 }
478
479#define STM32MP1_CLK_PARENT(idx, off, s, m, p) \
480 [(idx)] = { \
481 .offset = (off), \
482 .src = (s), \
483 .msk = (m), \
484 .parent = (p), \
485 .nb_parent = ARRAY_SIZE((p)) \
486 }
487
488#define STM32MP1_CLK_PLL(idx, type, off1, off2, off3, off4, off5, off6,\
489 p1, p2, p3, p4) \
490 [(idx)] = { \
491 .plltype = (type), \
492 .rckxselr = (off1), \
493 .pllxcfgr1 = (off2), \
494 .pllxcfgr2 = (off3), \
495 .pllxfracr = (off4), \
496 .pllxcr = (off5), \
497 .pllxcsgr = (off6), \
498 .refclk[0] = (p1), \
499 .refclk[1] = (p2), \
500 .refclk[2] = (p3), \
501 .refclk[3] = (p4), \
502 }
503
504static const u8 stm32mp1_clks[][2] = {
505 {CK_PER, _CK_PER},
506 {CK_MPU, _CK_MPU},
507 {CK_AXI, _ACLK},
508 {CK_MCU, _CK_MCU},
509 {CK_HSE, _HSE},
510 {CK_CSI, _CSI},
511 {CK_LSI, _LSI},
512 {CK_LSE, _LSE},
513 {CK_HSI, _HSI},
514 {CK_HSE_DIV2, _HSE_KER_DIV2},
515};
516
517static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
518 STM32MP1_CLK(RCC_DDRITFCR, 0, DDRC1, _UNKNOWN_SEL),
519 STM32MP1_CLK(RCC_DDRITFCR, 1, DDRC1LP, _UNKNOWN_SEL),
520 STM32MP1_CLK(RCC_DDRITFCR, 2, DDRC2, _UNKNOWN_SEL),
521 STM32MP1_CLK(RCC_DDRITFCR, 3, DDRC2LP, _UNKNOWN_SEL),
522 STM32MP1_CLK_F(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
523 STM32MP1_CLK(RCC_DDRITFCR, 5, DDRPHYCLP, _UNKNOWN_SEL),
524 STM32MP1_CLK(RCC_DDRITFCR, 6, DDRCAPB, _UNKNOWN_SEL),
525 STM32MP1_CLK(RCC_DDRITFCR, 7, DDRCAPBLP, _UNKNOWN_SEL),
526 STM32MP1_CLK(RCC_DDRITFCR, 8, AXIDCG, _UNKNOWN_SEL),
527 STM32MP1_CLK(RCC_DDRITFCR, 9, DDRPHYCAPB, _UNKNOWN_SEL),
528 STM32MP1_CLK(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _UNKNOWN_SEL),
529
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200530 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 11, SPI2_K, _SPI23_SEL),
531 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 12, SPI3_K, _SPI23_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100532 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
533 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
534 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
535 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
536 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
537 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
538 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
539 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
540 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
541 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
542
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200543 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 8, SPI1_K, _SPI1_SEL),
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200544 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 9, SPI4_K, _SPI45_SEL),
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100545 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 10, SPI5_K, _SPI45_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100546 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
547
Fabrice Gasnier4cb3b532018-04-26 17:00:47 +0200548 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
Patrick Delaunayc7d146d2021-06-29 12:04:22 +0200549 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_SEL),
Fabrice Gasnier4cb3b532018-04-26 17:00:47 +0200550
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200551 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 0, LTDC_PX, _PLL4_Q),
552 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 4, DSI_PX, _PLL4_Q),
553 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 4, DSI_K, _DSI_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100554 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
555 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
556 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
557
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200558 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100559 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
Patrick Delaunay5c0ea512021-01-22 15:34:25 +0100560 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
Anatolij Gustschinb62c75d2023-09-29 13:34:37 +0200561 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL),
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200562 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
Patrick Delaunayd69d1742021-07-16 10:10:55 +0200563 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 16, BSEC, _UNKNOWN_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100564 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
565
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200566 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB2ENSETR, 5, ADC12, _HCLK2),
567 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 5, ADC12_K, _ADC12_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100568 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
569 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
570
Benjamin Gaignard32470812018-11-27 13:49:51 +0100571 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 11, HSEM, _UNKNOWN_SEL),
Patrick Delaunay629f44f2019-01-30 13:07:01 +0100572 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 12, IPCC, _UNKNOWN_SEL),
Benjamin Gaignard32470812018-11-27 13:49:51 +0100573
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100574 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
575 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
576 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
577 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
578 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
579 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
580 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
581 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
582 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
583 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
584 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
585
586 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
Sughosh Ganu1b725012019-12-28 23:58:28 +0530587 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 6, RNG1_K, _UNKNOWN_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100588
Patrick Delaunay5bfc8702019-05-17 15:08:42 +0200589 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK_K, _ETH_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100590 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
591 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100592 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
593 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
594 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
595 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
596 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
597 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
598
599 STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200600
601 STM32MP1_CLK(RCC_BDCR, 20, RTC, _RTC_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100602};
603
604static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
605static const u8 i2c35_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
606static const u8 i2c46_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER};
Anatolij Gustschinb62c75d2023-09-29 13:34:37 +0200607static const u8 uart1_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER,
608 _PLL4_Q, _HSE_KER};
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100609static const u8 uart6_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
610 _HSE_KER};
611static const u8 uart24_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
612 _HSE_KER};
613static const u8 uart35_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
614 _HSE_KER};
615static const u8 uart78_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
616 _HSE_KER};
617static const u8 sdmmc12_parents[] = {_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER};
618static const u8 sdmmc3_parents[] = {_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER};
619static const u8 eth_parents[] = {_PLL4_P, _PLL3_Q};
620static const u8 qspi_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
621static const u8 fmc_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
622static const u8 usbphy_parents[] = {_HSE_KER, _PLL4_R, _HSE_KER_DIV2};
623static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
624static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200625static const u8 dsi_parents[] = {_DSI_PHY, _PLL4_P};
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200626static const u8 adc_parents[] = {_PLL4_R, _CK_PER, _PLL3_Q};
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200627/* same parents for SPI1=RCC_SPI2S1CKSELR and SPI2&3 = RCC_SPI2S23CKSELR */
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200628static const u8 spi_parents[] = {_PLL4_P, _PLL3_Q, _I2S_CKIN, _CK_PER,
629 _PLL3_R};
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100630static const u8 spi45_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
631 _HSE_KER};
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200632static const u8 spi6_parents[] = {_PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER,
633 _HSE_KER, _PLL3_Q};
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200634static const u8 rtc_parents[] = {_UNKNOWN_ID, _LSE, _LSI, _HSE};
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100635
636static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
637 STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
638 STM32MP1_CLK_PARENT(_I2C35_SEL, RCC_I2C35CKSELR, 0, 0x7, i2c35_parents),
639 STM32MP1_CLK_PARENT(_I2C46_SEL, RCC_I2C46CKSELR, 0, 0x7, i2c46_parents),
640 STM32MP1_CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents),
641 STM32MP1_CLK_PARENT(_UART24_SEL, RCC_UART24CKSELR, 0, 0x7,
642 uart24_parents),
643 STM32MP1_CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7,
644 uart35_parents),
645 STM32MP1_CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7,
646 uart78_parents),
647 STM32MP1_CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7,
648 sdmmc12_parents),
649 STM32MP1_CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7,
650 sdmmc3_parents),
651 STM32MP1_CLK_PARENT(_ETH_SEL, RCC_ETHCKSELR, 0, 0x3, eth_parents),
Patrick Delaunay95e7fbe2020-03-09 14:59:22 +0100652 STM32MP1_CLK_PARENT(_QSPI_SEL, RCC_QSPICKSELR, 0, 0x3, qspi_parents),
653 STM32MP1_CLK_PARENT(_FMC_SEL, RCC_FMCCKSELR, 0, 0x3, fmc_parents),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100654 STM32MP1_CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents),
655 STM32MP1_CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents),
656 STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200657 STM32MP1_CLK_PARENT(_DSI_SEL, RCC_DSICKSELR, 0, 0x1, dsi_parents),
Patrick Delaunay95e7fbe2020-03-09 14:59:22 +0100658 STM32MP1_CLK_PARENT(_ADC12_SEL, RCC_ADCCKSELR, 0, 0x3, adc_parents),
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200659 STM32MP1_CLK_PARENT(_SPI1_SEL, RCC_SPI2S1CKSELR, 0, 0x7, spi_parents),
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200660 STM32MP1_CLK_PARENT(_SPI23_SEL, RCC_SPI2S23CKSELR, 0, 0x7, spi_parents),
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100661 STM32MP1_CLK_PARENT(_SPI45_SEL, RCC_SPI45CKSELR, 0, 0x7, spi45_parents),
Patrick Delaunaydcd705e2021-07-09 14:24:34 +0200662 STM32MP1_CLK_PARENT(_SPI6_SEL, RCC_SPI6CKSELR, 0, 0x7, spi6_parents),
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200663 STM32MP1_CLK_PARENT(_RTC_SEL, RCC_BDCR, RCC_BDCR_RTCSRC_SHIFT,
664 (RCC_BDCR_RTCSRC_MASK >> RCC_BDCR_RTCSRC_SHIFT),
665 rtc_parents),
Anatolij Gustschinb62c75d2023-09-29 13:34:37 +0200666 STM32MP1_CLK_PARENT(_UART1_SEL, RCC_UART1CKSELR, 0, 0x7, uart1_parents),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100667};
668
669#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay885bdc22020-05-25 12:19:44 +0200670
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100671/* define characteristic of PLL according type */
Patrick Delaunay885bdc22020-05-25 12:19:44 +0200672#define DIVM_MIN 0
673#define DIVM_MAX 63
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100674#define DIVN_MIN 24
Patrick Delaunay885bdc22020-05-25 12:19:44 +0200675#define DIVP_MIN 0
676#define DIVP_MAX 127
677#define FRAC_MAX 8192
678
679#define PLL1600_VCO_MIN 800000000
680#define PLL1600_VCO_MAX 1600000000
681
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100682static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
683 [PLL_800] = {
684 .refclk_min = 4,
685 .refclk_max = 16,
686 .divn_max = 99,
687 },
688 [PLL_1600] = {
689 .refclk_min = 8,
690 .refclk_max = 16,
691 .divn_max = 199,
692 },
693};
694#endif /* STM32MP1_CLOCK_TREE_INIT */
695
696static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
697 STM32MP1_CLK_PLL(_PLL1, PLL_1600,
698 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
699 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
700 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
701 STM32MP1_CLK_PLL(_PLL2, PLL_1600,
702 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
703 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
704 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
705 STM32MP1_CLK_PLL(_PLL3, PLL_800,
706 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
707 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
708 _HSI, _HSE, _CSI, _UNKNOWN_ID),
709 STM32MP1_CLK_PLL(_PLL4, PLL_800,
710 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
711 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
712 _HSI, _HSE, _CSI, _I2S_CKIN),
713};
714
715/* Prescaler table lookups for clock computation */
716/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
717static const u8 stm32mp1_mcu_div[16] = {
718 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
719};
720
721/* div = /1 /2 /4 /8 /16 : same divider for pmu and apbx*/
722#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
723#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
724static const u8 stm32mp1_mpu_apbx_div[8] = {
725 0, 1, 2, 3, 4, 4, 4, 4
726};
727
728/* div = /1 /2 /3 /4 */
729static const u8 stm32mp1_axi_div[8] = {
730 1, 2, 3, 4, 4, 4, 4, 4
731};
732
Patrick Delaunaye8d836c2019-01-30 13:07:04 +0100733static const __maybe_unused
734char * const stm32mp1_clk_parent_name[_PARENT_NB] = {
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100735 [_HSI] = "HSI",
736 [_HSE] = "HSE",
737 [_CSI] = "CSI",
738 [_LSI] = "LSI",
739 [_LSE] = "LSE",
740 [_I2S_CKIN] = "I2S_CKIN",
741 [_HSI_KER] = "HSI_KER",
742 [_HSE_KER] = "HSE_KER",
743 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
744 [_CSI_KER] = "CSI_KER",
745 [_PLL1_P] = "PLL1_P",
746 [_PLL1_Q] = "PLL1_Q",
747 [_PLL1_R] = "PLL1_R",
748 [_PLL2_P] = "PLL2_P",
749 [_PLL2_Q] = "PLL2_Q",
750 [_PLL2_R] = "PLL2_R",
751 [_PLL3_P] = "PLL3_P",
752 [_PLL3_Q] = "PLL3_Q",
753 [_PLL3_R] = "PLL3_R",
754 [_PLL4_P] = "PLL4_P",
755 [_PLL4_Q] = "PLL4_Q",
756 [_PLL4_R] = "PLL4_R",
757 [_ACLK] = "ACLK",
758 [_PCLK1] = "PCLK1",
759 [_PCLK2] = "PCLK2",
760 [_PCLK3] = "PCLK3",
761 [_PCLK4] = "PCLK4",
762 [_PCLK5] = "PCLK5",
763 [_HCLK6] = "KCLK6",
764 [_HCLK2] = "HCLK2",
765 [_CK_PER] = "CK_PER",
766 [_CK_MPU] = "CK_MPU",
767 [_CK_MCU] = "CK_MCU",
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200768 [_USB_PHY_48] = "USB_PHY_48",
769 [_DSI_PHY] = "DSI_PHY_PLL",
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100770};
771
Patrick Delaunaye8d836c2019-01-30 13:07:04 +0100772static const __maybe_unused
773char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100774 [_I2C12_SEL] = "I2C12",
775 [_I2C35_SEL] = "I2C35",
776 [_I2C46_SEL] = "I2C46",
777 [_UART6_SEL] = "UART6",
778 [_UART24_SEL] = "UART24",
779 [_UART35_SEL] = "UART35",
780 [_UART78_SEL] = "UART78",
781 [_SDMMC12_SEL] = "SDMMC12",
782 [_SDMMC3_SEL] = "SDMMC3",
783 [_ETH_SEL] = "ETH",
784 [_QSPI_SEL] = "QSPI",
785 [_FMC_SEL] = "FMC",
786 [_USBPHY_SEL] = "USBPHY",
787 [_USBO_SEL] = "USBO",
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200788 [_STGEN_SEL] = "STGEN",
789 [_DSI_SEL] = "DSI",
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200790 [_ADC12_SEL] = "ADC12",
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200791 [_SPI1_SEL] = "SPI1",
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100792 [_SPI45_SEL] = "SPI45",
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200793 [_RTC_SEL] = "RTC",
Anatolij Gustschinb62c75d2023-09-29 13:34:37 +0200794 [_UART1_SEL] = "UART1",
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100795};
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100796
797static const struct stm32mp1_clk_data stm32mp1_data = {
798 .gate = stm32mp1_clk_gate,
799 .sel = stm32mp1_clk_sel,
800 .pll = stm32mp1_clk_pll,
801 .nb_gate = ARRAY_SIZE(stm32mp1_clk_gate),
802};
803
804static ulong stm32mp1_clk_get_fixed(struct stm32mp1_clk_priv *priv, int idx)
805{
806 if (idx >= NB_OSC) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100807 log_debug("clk id %d not found\n", idx);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100808 return 0;
809 }
810
Etienne Carriere55a78142021-02-24 11:19:42 +0100811 return clk_get_rate(&priv->osc_clk[idx]);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100812}
813
814static int stm32mp1_clk_get_id(struct stm32mp1_clk_priv *priv, unsigned long id)
815{
816 const struct stm32mp1_clk_gate *gate = priv->data->gate;
817 int i, nb_clks = priv->data->nb_gate;
818
819 for (i = 0; i < nb_clks; i++) {
820 if (gate[i].index == id)
821 break;
822 }
823
824 if (i == nb_clks) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100825 log_err("clk id %d not found\n", (u32)id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100826 return -EINVAL;
827 }
828
829 return i;
830}
831
832static int stm32mp1_clk_get_sel(struct stm32mp1_clk_priv *priv,
833 int i)
834{
835 const struct stm32mp1_clk_gate *gate = priv->data->gate;
836
837 if (gate[i].sel > _PARENT_SEL_NB) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100838 log_err("parents for clk id %d not found\n", i);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100839 return -EINVAL;
840 }
841
842 return gate[i].sel;
843}
844
845static int stm32mp1_clk_get_fixed_parent(struct stm32mp1_clk_priv *priv,
846 int i)
847{
848 const struct stm32mp1_clk_gate *gate = priv->data->gate;
849
850 if (gate[i].fixed == _UNKNOWN_ID)
851 return -ENOENT;
852
853 return gate[i].fixed;
854}
855
856static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
857 unsigned long id)
858{
859 const struct stm32mp1_clk_sel *sel = priv->data->sel;
860 int i;
861 int s, p;
Patrick Delaunay942ee232019-06-21 15:26:48 +0200862 unsigned int idx;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100863
Patrick Delaunay942ee232019-06-21 15:26:48 +0200864 for (idx = 0; idx < ARRAY_SIZE(stm32mp1_clks); idx++)
865 if (stm32mp1_clks[idx][0] == id)
866 return stm32mp1_clks[idx][1];
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100867
868 i = stm32mp1_clk_get_id(priv, id);
869 if (i < 0)
870 return i;
871
872 p = stm32mp1_clk_get_fixed_parent(priv, i);
873 if (p >= 0 && p < _PARENT_NB)
874 return p;
875
876 s = stm32mp1_clk_get_sel(priv, i);
877 if (s < 0)
878 return s;
879
880 p = (readl(priv->base + sel[s].offset) >> sel[s].src) & sel[s].msk;
881
882 if (p < sel[s].nb_parent) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100883 log_content("%s clock is the parent %s of clk id %d\n",
884 stm32mp1_clk_parent_name[sel[s].parent[p]],
885 stm32mp1_clk_parent_sel_name[s],
886 (u32)id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100887 return sel[s].parent[p];
888 }
889
Patrick Delaunay4e183072023-06-23 15:05:16 +0200890 /* clock is DISABLED when the clock src is not in clk_parent[] range */
891 log_debug("no parents defined for clk id %d\n", (u32)id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100892
893 return -EINVAL;
894}
895
Patrick Delaunay5327d372018-07-16 10:41:42 +0200896static ulong pll_get_fref_ck(struct stm32mp1_clk_priv *priv,
897 int pll_id)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100898{
899 const struct stm32mp1_clk_pll *pll = priv->data->pll;
Patrick Delaunay5327d372018-07-16 10:41:42 +0200900 u32 selr;
901 int src;
902 ulong refclk;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100903
Patrick Delaunay5327d372018-07-16 10:41:42 +0200904 /* Get current refclk */
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100905 selr = readl(priv->base + pll[pll_id].rckxselr);
Patrick Delaunay5327d372018-07-16 10:41:42 +0200906 src = selr & RCC_SELR_SRC_MASK;
907
908 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]);
Patrick Delaunay5327d372018-07-16 10:41:42 +0200909
910 return refclk;
911}
912
913/*
914 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
915 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
916 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
917 * => in all the case Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
918 */
919static ulong pll_get_fvco(struct stm32mp1_clk_priv *priv,
920 int pll_id)
921{
922 const struct stm32mp1_clk_pll *pll = priv->data->pll;
923 int divm, divn;
924 ulong refclk, fvco;
925 u32 cfgr1, fracr;
926
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100927 cfgr1 = readl(priv->base + pll[pll_id].pllxcfgr1);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100928 fracr = readl(priv->base + pll[pll_id].pllxfracr);
929
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100930 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
931 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100932
Patrick Delaunay5327d372018-07-16 10:41:42 +0200933 refclk = pll_get_fref_ck(priv, pll_id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100934
Patrick Delaunay5327d372018-07-16 10:41:42 +0200935 /* with FRACV :
936 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100937 * without FRACV
Patrick Delaunay5327d372018-07-16 10:41:42 +0200938 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100939 */
940 if (fracr & RCC_PLLNFRACR_FRACLE) {
941 u32 fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK)
942 >> RCC_PLLNFRACR_FRACV_SHIFT;
Patrick Delaunay5327d372018-07-16 10:41:42 +0200943 fvco = (ulong)lldiv((unsigned long long)refclk *
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100944 (((divn + 1) << 13) + fracv),
Patrick Delaunay5327d372018-07-16 10:41:42 +0200945 ((unsigned long long)(divm + 1)) << 13);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100946 } else {
Patrick Delaunay5327d372018-07-16 10:41:42 +0200947 fvco = (ulong)(refclk * (divn + 1) / (divm + 1));
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100948 }
Patrick Delaunay5327d372018-07-16 10:41:42 +0200949
950 return fvco;
951}
952
953static ulong stm32mp1_read_pll_freq(struct stm32mp1_clk_priv *priv,
954 int pll_id, int div_id)
955{
956 const struct stm32mp1_clk_pll *pll = priv->data->pll;
957 int divy;
958 ulong dfout;
959 u32 cfgr2;
960
Patrick Delaunay5327d372018-07-16 10:41:42 +0200961 if (div_id >= _DIV_NB)
962 return 0;
963
964 cfgr2 = readl(priv->base + pll[pll_id].pllxcfgr2);
965 divy = (cfgr2 >> RCC_PLLNCFGR2_SHIFT(div_id)) & RCC_PLLNCFGR2_DIVX_MASK;
966
Patrick Delaunay5327d372018-07-16 10:41:42 +0200967 dfout = pll_get_fvco(priv, pll_id) / (divy + 1);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100968
969 return dfout;
970}
971
Patrick Delaunay4a1b0832022-04-26 14:37:49 +0200972static ulong stm32mp1_clk_get_by_name(const char *name)
973{
974 struct clk clk;
975 struct udevice *dev = NULL;
976 ulong clock = 0;
977
978 if (!uclass_get_device_by_name(UCLASS_CLK, name, &dev)) {
979 if (clk_request(dev, &clk)) {
980 log_err("%s request", name);
981 } else {
982 clk.id = 0;
983 clock = clk_get_rate(&clk);
984 }
985 }
986
987 return clock;
988}
989
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100990static ulong stm32mp1_clk_get(struct stm32mp1_clk_priv *priv, int p)
991{
992 u32 reg;
993 ulong clock = 0;
994
995 switch (p) {
996 case _CK_MPU:
997 /* MPU sub system */
998 reg = readl(priv->base + RCC_MPCKSELR);
999 switch (reg & RCC_SELR_SRC_MASK) {
1000 case RCC_MPCKSELR_HSI:
1001 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1002 break;
1003 case RCC_MPCKSELR_HSE:
1004 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1005 break;
1006 case RCC_MPCKSELR_PLL:
1007 case RCC_MPCKSELR_PLL_MPUDIV:
1008 clock = stm32mp1_read_pll_freq(priv, _PLL1, _DIV_P);
Lionel Debieve97289492020-04-24 15:47:57 +02001009 if ((reg & RCC_SELR_SRC_MASK) ==
1010 RCC_MPCKSELR_PLL_MPUDIV) {
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001011 reg = readl(priv->base + RCC_MPCKDIVR);
Lionel Debieve97289492020-04-24 15:47:57 +02001012 clock >>= stm32mp1_mpu_div[reg &
1013 RCC_MPUDIV_MASK];
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001014 }
1015 break;
1016 }
1017 break;
1018 /* AXI sub system */
1019 case _ACLK:
1020 case _HCLK2:
1021 case _HCLK6:
1022 case _PCLK4:
1023 case _PCLK5:
1024 reg = readl(priv->base + RCC_ASSCKSELR);
1025 switch (reg & RCC_SELR_SRC_MASK) {
1026 case RCC_ASSCKSELR_HSI:
1027 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1028 break;
1029 case RCC_ASSCKSELR_HSE:
1030 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1031 break;
1032 case RCC_ASSCKSELR_PLL:
1033 clock = stm32mp1_read_pll_freq(priv, _PLL2, _DIV_P);
1034 break;
1035 }
1036
1037 /* System clock divider */
1038 reg = readl(priv->base + RCC_AXIDIVR);
1039 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
1040
1041 switch (p) {
1042 case _PCLK4:
1043 reg = readl(priv->base + RCC_APB4DIVR);
1044 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1045 break;
1046 case _PCLK5:
1047 reg = readl(priv->base + RCC_APB5DIVR);
1048 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1049 break;
1050 default:
1051 break;
1052 }
1053 break;
1054 /* MCU sub system */
1055 case _CK_MCU:
1056 case _PCLK1:
1057 case _PCLK2:
1058 case _PCLK3:
1059 reg = readl(priv->base + RCC_MSSCKSELR);
1060 switch (reg & RCC_SELR_SRC_MASK) {
1061 case RCC_MSSCKSELR_HSI:
1062 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1063 break;
1064 case RCC_MSSCKSELR_HSE:
1065 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1066 break;
1067 case RCC_MSSCKSELR_CSI:
1068 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1069 break;
1070 case RCC_MSSCKSELR_PLL:
1071 clock = stm32mp1_read_pll_freq(priv, _PLL3, _DIV_P);
1072 break;
1073 }
1074
1075 /* MCU clock divider */
1076 reg = readl(priv->base + RCC_MCUDIVR);
1077 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
1078
1079 switch (p) {
1080 case _PCLK1:
1081 reg = readl(priv->base + RCC_APB1DIVR);
1082 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1083 break;
1084 case _PCLK2:
1085 reg = readl(priv->base + RCC_APB2DIVR);
1086 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1087 break;
1088 case _PCLK3:
1089 reg = readl(priv->base + RCC_APB3DIVR);
1090 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1091 break;
1092 case _CK_MCU:
1093 default:
1094 break;
1095 }
1096 break;
1097 case _CK_PER:
1098 reg = readl(priv->base + RCC_CPERCKSELR);
1099 switch (reg & RCC_SELR_SRC_MASK) {
1100 case RCC_CPERCKSELR_HSI:
1101 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1102 break;
1103 case RCC_CPERCKSELR_HSE:
1104 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1105 break;
1106 case RCC_CPERCKSELR_CSI:
1107 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1108 break;
1109 }
1110 break;
1111 case _HSI:
1112 case _HSI_KER:
1113 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1114 break;
1115 case _CSI:
1116 case _CSI_KER:
1117 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1118 break;
1119 case _HSE:
1120 case _HSE_KER:
1121 case _HSE_KER_DIV2:
1122 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1123 if (p == _HSE_KER_DIV2)
1124 clock >>= 1;
1125 break;
1126 case _LSI:
1127 clock = stm32mp1_clk_get_fixed(priv, _LSI);
1128 break;
1129 case _LSE:
1130 clock = stm32mp1_clk_get_fixed(priv, _LSE);
1131 break;
1132 /* PLL */
1133 case _PLL1_P:
1134 case _PLL1_Q:
1135 case _PLL1_R:
1136 clock = stm32mp1_read_pll_freq(priv, _PLL1, p - _PLL1_P);
1137 break;
1138 case _PLL2_P:
1139 case _PLL2_Q:
1140 case _PLL2_R:
1141 clock = stm32mp1_read_pll_freq(priv, _PLL2, p - _PLL2_P);
1142 break;
1143 case _PLL3_P:
1144 case _PLL3_Q:
1145 case _PLL3_R:
1146 clock = stm32mp1_read_pll_freq(priv, _PLL3, p - _PLL3_P);
1147 break;
1148 case _PLL4_P:
1149 case _PLL4_Q:
1150 case _PLL4_R:
1151 clock = stm32mp1_read_pll_freq(priv, _PLL4, p - _PLL4_P);
1152 break;
1153 /* other */
1154 case _USB_PHY_48:
Patrick Delaunay4a1b0832022-04-26 14:37:49 +02001155 clock = stm32mp1_clk_get_by_name("ck_usbo_48m");
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001156 break;
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02001157 case _DSI_PHY:
Patrick Delaunay4a1b0832022-04-26 14:37:49 +02001158 clock = stm32mp1_clk_get_by_name("ck_dsi_phy");
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02001159 break;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001160 default:
1161 break;
1162 }
1163
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001164 log_debug("id=%d clock = %lx : %ld kHz\n", p, clock, clock / 1000);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001165
1166 return clock;
1167}
1168
1169static int stm32mp1_clk_enable(struct clk *clk)
1170{
1171 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1172 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1173 int i = stm32mp1_clk_get_id(priv, clk->id);
1174
1175 if (i < 0)
1176 return i;
1177
1178 if (gate[i].set_clr)
1179 writel(BIT(gate[i].bit), priv->base + gate[i].offset);
1180 else
1181 setbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1182
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001183 dev_dbg(clk->dev, "%s: id clock %d has been enabled\n", __func__, (u32)clk->id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001184
1185 return 0;
1186}
1187
1188static int stm32mp1_clk_disable(struct clk *clk)
1189{
1190 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1191 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1192 int i = stm32mp1_clk_get_id(priv, clk->id);
1193
1194 if (i < 0)
1195 return i;
1196
1197 if (gate[i].set_clr)
1198 writel(BIT(gate[i].bit),
1199 priv->base + gate[i].offset
1200 + RCC_MP_ENCLRR_OFFSET);
1201 else
1202 clrbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1203
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001204 dev_dbg(clk->dev, "%s: id clock %d has been disabled\n", __func__, (u32)clk->id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001205
1206 return 0;
1207}
1208
1209static ulong stm32mp1_clk_get_rate(struct clk *clk)
1210{
1211 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1212 int p = stm32mp1_clk_get_parent(priv, clk->id);
1213 ulong rate;
1214
1215 if (p < 0)
1216 return 0;
1217
1218 rate = stm32mp1_clk_get(priv, p);
1219
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001220 dev_vdbg(clk->dev, "computed rate for id clock %d is %d (parent is %s)\n",
1221 (u32)clk->id, (u32)rate, stm32mp1_clk_parent_name[p]);
1222
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001223 return rate;
1224}
1225
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001226#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001227
1228bool stm32mp1_supports_opp(u32 opp_id, u32 cpu_type)
1229{
1230 unsigned int id;
1231
1232 switch (opp_id) {
1233 case 1:
1234 case 2:
1235 id = opp_id;
1236 break;
1237 default:
1238 id = 1; /* default value */
1239 break;
1240 }
1241
1242 switch (cpu_type) {
1243 case CPU_STM32MP157Fxx:
1244 case CPU_STM32MP157Dxx:
1245 case CPU_STM32MP153Fxx:
1246 case CPU_STM32MP153Dxx:
1247 case CPU_STM32MP151Fxx:
1248 case CPU_STM32MP151Dxx:
1249 return true;
1250 default:
1251 return id == 1;
1252 }
1253}
1254
Patrick Delaunay3d1fe4e2020-05-25 12:19:45 +02001255__weak void board_vddcore_init(u32 voltage_mv)
1256{
1257}
1258
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001259/*
1260 * gets OPP parameters (frequency in KHz and voltage in mV) from
1261 * an OPP table subnode. Platform HW support capabilities are also checked.
1262 * Returns 0 on success and a negative FDT error code on failure.
1263 */
1264static int stm32mp1_get_opp(u32 cpu_type, ofnode subnode,
1265 u32 *freq_khz, u32 *voltage_mv)
1266{
1267 u32 opp_hw;
1268 u64 read_freq_64;
1269 u32 read_voltage_32;
1270
1271 *freq_khz = 0;
1272 *voltage_mv = 0;
1273
1274 opp_hw = ofnode_read_u32_default(subnode, "opp-supported-hw", 0);
1275 if (opp_hw)
1276 if (!stm32mp1_supports_opp(opp_hw, cpu_type))
1277 return -FDT_ERR_BADVALUE;
1278
1279 read_freq_64 = ofnode_read_u64_default(subnode, "opp-hz", 0) /
1280 1000ULL;
1281 read_voltage_32 = ofnode_read_u32_default(subnode, "opp-microvolt", 0) /
1282 1000U;
1283
1284 if (!read_voltage_32 || !read_freq_64)
1285 return -FDT_ERR_NOTFOUND;
1286
1287 /* Frequency value expressed in KHz must fit on 32 bits */
1288 if (read_freq_64 > U32_MAX)
1289 return -FDT_ERR_BADVALUE;
1290
1291 /* Millivolt value must fit on 16 bits */
1292 if (read_voltage_32 > U16_MAX)
1293 return -FDT_ERR_BADVALUE;
1294
1295 *freq_khz = (u32)read_freq_64;
1296 *voltage_mv = read_voltage_32;
1297
1298 return 0;
1299}
1300
1301/*
1302 * parses OPP table in DT and finds the parameters for the
1303 * highest frequency supported by the HW platform.
1304 * Returns 0 on success and a negative FDT error code on failure.
1305 */
1306int stm32mp1_get_max_opp_freq(struct stm32mp1_clk_priv *priv, u64 *freq_hz)
1307{
1308 ofnode node, subnode;
1309 int ret;
1310 u32 freq = 0U, voltage = 0U;
1311 u32 cpu_type = get_cpu_type();
1312
1313 node = ofnode_by_compatible(ofnode_null(), "operating-points-v2");
1314 if (!ofnode_valid(node))
1315 return -FDT_ERR_NOTFOUND;
1316
1317 ofnode_for_each_subnode(subnode, node) {
1318 unsigned int read_freq;
1319 unsigned int read_voltage;
1320
1321 ret = stm32mp1_get_opp(cpu_type, subnode,
1322 &read_freq, &read_voltage);
1323 if (ret)
1324 continue;
1325
1326 if (read_freq > freq) {
1327 freq = read_freq;
1328 voltage = read_voltage;
1329 }
1330 }
1331
1332 if (!freq || !voltage)
1333 return -FDT_ERR_NOTFOUND;
1334
1335 *freq_hz = (u64)1000U * freq;
Patrick Delaunay3d1fe4e2020-05-25 12:19:45 +02001336 board_vddcore_init(voltage);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001337
1338 return 0;
1339}
1340
1341static int stm32mp1_pll1_opp(struct stm32mp1_clk_priv *priv, int clksrc,
1342 u32 *pllcfg, u32 *fracv)
1343{
1344 u32 post_divm;
1345 u32 input_freq;
1346 u64 output_freq;
1347 u64 freq;
1348 u64 vco;
1349 u32 divm, divn, divp, frac;
1350 int i, ret;
1351 u32 diff;
1352 u32 best_diff = U32_MAX;
1353
1354 /* PLL1 is 1600 */
1355 const u32 DIVN_MAX = stm32mp1_pll[PLL_1600].divn_max;
1356 const u32 POST_DIVM_MIN = stm32mp1_pll[PLL_1600].refclk_min * 1000000U;
1357 const u32 POST_DIVM_MAX = stm32mp1_pll[PLL_1600].refclk_max * 1000000U;
1358
1359 ret = stm32mp1_get_max_opp_freq(priv, &output_freq);
1360 if (ret) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001361 log_debug("PLL1 OPP configuration not found (%d).\n", ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001362 return ret;
1363 }
1364
1365 switch (clksrc) {
1366 case CLK_PLL12_HSI:
1367 input_freq = stm32mp1_clk_get_fixed(priv, _HSI);
1368 break;
1369 case CLK_PLL12_HSE:
1370 input_freq = stm32mp1_clk_get_fixed(priv, _HSE);
1371 break;
1372 default:
1373 return -EINTR;
1374 }
1375
1376 /* Following parameters have always the same value */
1377 pllcfg[PLLCFG_Q] = 0;
1378 pllcfg[PLLCFG_R] = 0;
1379 pllcfg[PLLCFG_O] = PQR(1, 0, 0);
1380
1381 for (divm = DIVM_MAX; divm >= DIVM_MIN; divm--) {
1382 post_divm = (u32)(input_freq / (divm + 1));
1383 if (post_divm < POST_DIVM_MIN || post_divm > POST_DIVM_MAX)
1384 continue;
1385
1386 for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
1387 freq = output_freq * (divm + 1) * (divp + 1);
1388 divn = (u32)((freq / input_freq) - 1);
1389 if (divn < DIVN_MIN || divn > DIVN_MAX)
1390 continue;
1391
1392 frac = (u32)(((freq * FRAC_MAX) / input_freq) -
1393 ((divn + 1) * FRAC_MAX));
1394 /* 2 loops to refine the fractional part */
1395 for (i = 2; i != 0; i--) {
1396 if (frac > FRAC_MAX)
1397 break;
1398
1399 vco = (post_divm * (divn + 1)) +
1400 ((post_divm * (u64)frac) /
1401 FRAC_MAX);
1402 if (vco < (PLL1600_VCO_MIN / 2) ||
1403 vco > (PLL1600_VCO_MAX / 2)) {
1404 frac++;
1405 continue;
1406 }
1407 freq = vco / (divp + 1);
1408 if (output_freq < freq)
1409 diff = (u32)(freq - output_freq);
1410 else
1411 diff = (u32)(output_freq - freq);
1412 if (diff < best_diff) {
1413 pllcfg[PLLCFG_M] = divm;
1414 pllcfg[PLLCFG_N] = divn;
1415 pllcfg[PLLCFG_P] = divp;
1416 *fracv = frac;
1417
1418 if (diff == 0)
1419 return 0;
1420
1421 best_diff = diff;
1422 }
1423 frac++;
1424 }
1425 }
1426 }
1427
1428 if (best_diff == U32_MAX)
1429 return -1;
1430
1431 return 0;
1432}
1433
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001434static void stm32mp1_ls_osc_set(int enable, fdt_addr_t rcc, u32 offset,
1435 u32 mask_on)
1436{
1437 u32 address = rcc + offset;
1438
1439 if (enable)
1440 setbits_le32(address, mask_on);
1441 else
1442 clrbits_le32(address, mask_on);
1443}
1444
1445static void stm32mp1_hs_ocs_set(int enable, fdt_addr_t rcc, u32 mask_on)
1446{
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001447 writel(mask_on, rcc + (enable ? RCC_OCENSETR : RCC_OCENCLRR));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001448}
1449
1450static int stm32mp1_osc_wait(int enable, fdt_addr_t rcc, u32 offset,
1451 u32 mask_rdy)
1452{
1453 u32 mask_test = 0;
1454 u32 address = rcc + offset;
1455 u32 val;
1456 int ret;
1457
1458 if (enable)
1459 mask_test = mask_rdy;
1460
1461 ret = readl_poll_timeout(address, val,
1462 (val & mask_rdy) == mask_test,
1463 TIMEOUT_1S);
1464
1465 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001466 log_err("OSC %x @ %x timeout for enable=%d : 0x%x\n",
1467 mask_rdy, address, enable, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001468
1469 return ret;
1470}
1471
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001472static void stm32mp1_lse_enable(fdt_addr_t rcc, int bypass, int digbyp,
Patrick Delaunay5ba62a42020-01-28 10:44:15 +01001473 u32 lsedrv)
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001474{
1475 u32 value;
1476
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001477 if (digbyp)
1478 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_DIGBYP);
1479
1480 if (bypass || digbyp)
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001481 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_LSEBYP);
1482
1483 /*
1484 * warning: not recommended to switch directly from "high drive"
1485 * to "medium low drive", and vice-versa.
1486 */
1487 value = (readl(rcc + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK)
1488 >> RCC_BDCR_LSEDRV_SHIFT;
1489
1490 while (value != lsedrv) {
1491 if (value > lsedrv)
1492 value--;
1493 else
1494 value++;
1495
1496 clrsetbits_le32(rcc + RCC_BDCR,
1497 RCC_BDCR_LSEDRV_MASK,
1498 value << RCC_BDCR_LSEDRV_SHIFT);
1499 }
1500
1501 stm32mp1_ls_osc_set(1, rcc, RCC_BDCR, RCC_BDCR_LSEON);
1502}
1503
1504static void stm32mp1_lse_wait(fdt_addr_t rcc)
1505{
1506 stm32mp1_osc_wait(1, rcc, RCC_BDCR, RCC_BDCR_LSERDY);
1507}
1508
1509static void stm32mp1_lsi_set(fdt_addr_t rcc, int enable)
1510{
1511 stm32mp1_ls_osc_set(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSION);
1512 stm32mp1_osc_wait(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSIRDY);
1513}
1514
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001515static void stm32mp1_hse_enable(fdt_addr_t rcc, int bypass, int digbyp, int css)
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001516{
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001517 if (digbyp)
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001518 writel(RCC_OCENR_DIGBYP, rcc + RCC_OCENSETR);
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001519 if (bypass || digbyp)
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001520 writel(RCC_OCENR_HSEBYP, rcc + RCC_OCENSETR);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001521
1522 stm32mp1_hs_ocs_set(1, rcc, RCC_OCENR_HSEON);
1523 stm32mp1_osc_wait(1, rcc, RCC_OCRDYR, RCC_OCRDYR_HSERDY);
1524
1525 if (css)
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001526 writel(RCC_OCENR_HSECSSON, rcc + RCC_OCENSETR);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001527}
1528
1529static void stm32mp1_csi_set(fdt_addr_t rcc, int enable)
1530{
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001531 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_CSION);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001532 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_CSIRDY);
1533}
1534
1535static void stm32mp1_hsi_set(fdt_addr_t rcc, int enable)
1536{
1537 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_HSION);
1538 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_HSIRDY);
1539}
1540
1541static int stm32mp1_set_hsidiv(fdt_addr_t rcc, u8 hsidiv)
1542{
1543 u32 address = rcc + RCC_OCRDYR;
1544 u32 val;
1545 int ret;
1546
1547 clrsetbits_le32(rcc + RCC_HSICFGR,
1548 RCC_HSICFGR_HSIDIV_MASK,
1549 RCC_HSICFGR_HSIDIV_MASK & hsidiv);
1550
1551 ret = readl_poll_timeout(address, val,
1552 val & RCC_OCRDYR_HSIDIVRDY,
1553 TIMEOUT_200MS);
1554 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001555 log_err("HSIDIV failed @ 0x%x: 0x%x\n",
1556 address, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001557
1558 return ret;
1559}
1560
1561static int stm32mp1_hsidiv(fdt_addr_t rcc, ulong hsifreq)
1562{
1563 u8 hsidiv;
1564 u32 hsidivfreq = MAX_HSI_HZ;
1565
1566 for (hsidiv = 0; hsidiv < 4; hsidiv++,
1567 hsidivfreq = hsidivfreq / 2)
1568 if (hsidivfreq == hsifreq)
1569 break;
1570
1571 if (hsidiv == 4) {
Etienne Carriere55a78142021-02-24 11:19:42 +01001572 log_err("hsi frequency invalid");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001573 return -1;
1574 }
1575
1576 if (hsidiv > 0)
1577 return stm32mp1_set_hsidiv(rcc, hsidiv);
1578
1579 return 0;
1580}
1581
1582static void pll_start(struct stm32mp1_clk_priv *priv, int pll_id)
1583{
1584 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1585
Patrick Delaunay9a6ce2a2019-01-30 13:07:06 +01001586 clrsetbits_le32(priv->base + pll[pll_id].pllxcr,
1587 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1588 RCC_PLLNCR_DIVREN,
1589 RCC_PLLNCR_PLLON);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001590}
1591
1592static int pll_output(struct stm32mp1_clk_priv *priv, int pll_id, int output)
1593{
1594 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1595 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1596 u32 val;
1597 int ret;
1598
1599 ret = readl_poll_timeout(pllxcr, val, val & RCC_PLLNCR_PLLRDY,
1600 TIMEOUT_200MS);
1601
1602 if (ret) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001603 log_err("PLL%d start failed @ 0x%x: 0x%x\n",
1604 pll_id, pllxcr, readl(pllxcr));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001605 return ret;
1606 }
1607
1608 /* start the requested output */
1609 setbits_le32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1610
1611 return 0;
1612}
1613
1614static int pll_stop(struct stm32mp1_clk_priv *priv, int pll_id)
1615{
1616 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1617 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1618 u32 val;
1619
1620 /* stop all output */
1621 clrbits_le32(pllxcr,
1622 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
1623
1624 /* stop PLL */
1625 clrbits_le32(pllxcr, RCC_PLLNCR_PLLON);
1626
1627 /* wait PLL stopped */
1628 return readl_poll_timeout(pllxcr, val, (val & RCC_PLLNCR_PLLRDY) == 0,
1629 TIMEOUT_200MS);
1630}
1631
1632static void pll_config_output(struct stm32mp1_clk_priv *priv,
1633 int pll_id, u32 *pllcfg)
1634{
1635 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1636 fdt_addr_t rcc = priv->base;
1637 u32 value;
1638
1639 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT)
1640 & RCC_PLLNCFGR2_DIVP_MASK;
1641 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT)
1642 & RCC_PLLNCFGR2_DIVQ_MASK;
1643 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT)
1644 & RCC_PLLNCFGR2_DIVR_MASK;
1645 writel(value, rcc + pll[pll_id].pllxcfgr2);
1646}
1647
1648static int pll_config(struct stm32mp1_clk_priv *priv, int pll_id,
1649 u32 *pllcfg, u32 fracv)
1650{
1651 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1652 fdt_addr_t rcc = priv->base;
1653 enum stm32mp1_plltype type = pll[pll_id].plltype;
1654 int src;
1655 ulong refclk;
1656 u8 ifrge = 0;
1657 u32 value;
1658
1659 src = readl(priv->base + pll[pll_id].rckxselr) & RCC_SELR_SRC_MASK;
1660
1661 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]) /
1662 (pllcfg[PLLCFG_M] + 1);
1663
1664 if (refclk < (stm32mp1_pll[type].refclk_min * 1000000) ||
1665 refclk > (stm32mp1_pll[type].refclk_max * 1000000)) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001666 log_err("invalid refclk = %x\n", (u32)refclk);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001667 return -EINVAL;
1668 }
1669 if (type == PLL_800 && refclk >= 8000000)
1670 ifrge = 1;
1671
1672 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT)
1673 & RCC_PLLNCFGR1_DIVN_MASK;
1674 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT)
1675 & RCC_PLLNCFGR1_DIVM_MASK;
1676 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT)
1677 & RCC_PLLNCFGR1_IFRGE_MASK;
1678 writel(value, rcc + pll[pll_id].pllxcfgr1);
1679
1680 /* fractional configuration: load sigma-delta modulator (SDM) */
1681
1682 /* Write into FRACV the new fractional value , and FRACLE to 0 */
1683 writel(fracv << RCC_PLLNFRACR_FRACV_SHIFT,
1684 rcc + pll[pll_id].pllxfracr);
1685
1686 /* Write FRACLE to 1 : FRACV value is loaded into the SDM */
1687 setbits_le32(rcc + pll[pll_id].pllxfracr,
1688 RCC_PLLNFRACR_FRACLE);
1689
1690 pll_config_output(priv, pll_id, pllcfg);
1691
1692 return 0;
1693}
1694
1695static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
1696{
1697 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1698 u32 pllxcsg;
1699
1700 pllxcsg = ((csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1701 RCC_PLLNCSGR_MOD_PER_MASK) |
1702 ((csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1703 RCC_PLLNCSGR_INC_STEP_MASK) |
1704 ((csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1705 RCC_PLLNCSGR_SSCG_MODE_MASK);
1706
1707 writel(pllxcsg, priv->base + pll[pll_id].pllxcsgr);
Patrick Delaunay9a6ce2a2019-01-30 13:07:06 +01001708
1709 setbits_le32(priv->base + pll[pll_id].pllxcr, RCC_PLLNCR_SSCG_CTRL);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001710}
1711
Patrick Delaunay854c59e2019-04-18 17:32:48 +02001712static __maybe_unused int pll_set_rate(struct udevice *dev,
1713 int pll_id,
1714 int div_id,
1715 unsigned long clk_rate)
1716{
1717 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1718 unsigned int pllcfg[PLLCFG_NB];
1719 ofnode plloff;
1720 char name[12];
1721 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1722 enum stm32mp1_plltype type = pll[pll_id].plltype;
1723 int divm, divn, divy;
1724 int ret;
1725 ulong fck_ref;
1726 u32 fracv;
1727 u64 value;
1728
1729 if (div_id > _DIV_NB)
1730 return -EINVAL;
1731
1732 sprintf(name, "st,pll@%d", pll_id);
1733 plloff = dev_read_subnode(dev, name);
1734 if (!ofnode_valid(plloff))
1735 return -FDT_ERR_NOTFOUND;
1736
1737 ret = ofnode_read_u32_array(plloff, "cfg",
1738 pllcfg, PLLCFG_NB);
1739 if (ret < 0)
1740 return -FDT_ERR_NOTFOUND;
1741
1742 fck_ref = pll_get_fref_ck(priv, pll_id);
1743
1744 divm = pllcfg[PLLCFG_M];
1745 /* select output divider = 0: for _DIV_P, 1:_DIV_Q 2:_DIV_R */
1746 divy = pllcfg[PLLCFG_P + div_id];
1747
1748 /* For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
1749 * So same final result than PLL2 et 4
1750 * with FRACV
1751 * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
1752 * / (DIVy + 1) * (DIVM + 1)
1753 * value = (DIVN + 1) * 2^13 + FRACV / 2^13
1754 * = Fck_pll_y (DIVy + 1) * (DIVM + 1) * 2^13 / Fck_ref
1755 */
1756 value = ((u64)clk_rate * (divy + 1) * (divm + 1)) << 13;
1757 value = lldiv(value, fck_ref);
1758
1759 divn = (value >> 13) - 1;
1760 if (divn < DIVN_MIN ||
1761 divn > stm32mp1_pll[type].divn_max) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001762 dev_err(dev, "divn invalid = %d", divn);
Patrick Delaunay854c59e2019-04-18 17:32:48 +02001763 return -EINVAL;
1764 }
1765 fracv = value - ((divn + 1) << 13);
1766 pllcfg[PLLCFG_N] = divn;
1767
1768 /* reconfigure PLL */
1769 pll_stop(priv, pll_id);
1770 pll_config(priv, pll_id, pllcfg, fracv);
1771 pll_start(priv, pll_id);
1772 pll_output(priv, pll_id, pllcfg[PLLCFG_O]);
1773
1774 return 0;
1775}
1776
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001777static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
1778{
1779 u32 address = priv->base + (clksrc >> 4);
1780 u32 val;
1781 int ret;
1782
1783 clrsetbits_le32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK);
1784 ret = readl_poll_timeout(address, val, val & RCC_SELR_SRCRDY,
1785 TIMEOUT_200MS);
1786 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001787 log_err("CLKSRC %x start failed @ 0x%x: 0x%x\n",
1788 clksrc, address, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001789
1790 return ret;
1791}
1792
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001793static void stgen_config(struct stm32mp1_clk_priv *priv)
1794{
1795 int p;
1796 u32 stgenc, cntfid0;
1797 ulong rate;
1798
Patrick Delaunay82b88ef2019-07-05 17:20:11 +02001799 stgenc = STM32_STGEN_BASE;
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001800 cntfid0 = readl(stgenc + STGENC_CNTFID0);
1801 p = stm32mp1_clk_get_parent(priv, STGEN_K);
1802 rate = stm32mp1_clk_get(priv, p);
1803
1804 if (cntfid0 != rate) {
Patrick Delaunay45e5da52019-01-30 13:07:03 +01001805 u64 counter;
1806
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001807 log_debug("System Generic Counter (STGEN) update\n");
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001808 clrbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
Patrick Delaunay45e5da52019-01-30 13:07:03 +01001809 counter = (u64)readl(stgenc + STGENC_CNTCVL);
1810 counter |= ((u64)(readl(stgenc + STGENC_CNTCVU))) << 32;
1811 counter = lldiv(counter * (u64)rate, cntfid0);
1812 writel((u32)counter, stgenc + STGENC_CNTCVL);
1813 writel((u32)(counter >> 32), stgenc + STGENC_CNTCVU);
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001814 writel(rate, stgenc + STGENC_CNTFID0);
1815 setbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1816
1817 __asm__ volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (rate));
1818
1819 /* need to update gd->arch.timer_rate_hz with new frequency */
1820 timer_init();
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001821 }
1822}
1823
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001824static int set_clkdiv(unsigned int clkdiv, u32 address)
1825{
1826 u32 val;
1827 int ret;
1828
1829 clrsetbits_le32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK);
1830 ret = readl_poll_timeout(address, val, val & RCC_DIVR_DIVRDY,
1831 TIMEOUT_200MS);
1832 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001833 log_err("CLKDIV %x start failed @ 0x%x: 0x%x\n",
1834 clkdiv, address, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001835
1836 return ret;
1837}
1838
1839static void stm32mp1_mco_csg(struct stm32mp1_clk_priv *priv,
1840 u32 clksrc, u32 clkdiv)
1841{
1842 u32 address = priv->base + (clksrc >> 4);
1843
1844 /*
1845 * binding clksrc : bit15-4 offset
1846 * bit3: disable
1847 * bit2-0: MCOSEL[2:0]
1848 */
1849 if (clksrc & 0x8) {
1850 clrbits_le32(address, RCC_MCOCFG_MCOON);
1851 } else {
1852 clrsetbits_le32(address,
1853 RCC_MCOCFG_MCOSRC_MASK,
1854 clksrc & RCC_MCOCFG_MCOSRC_MASK);
1855 clrsetbits_le32(address,
1856 RCC_MCOCFG_MCODIV_MASK,
1857 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
1858 setbits_le32(address, RCC_MCOCFG_MCOON);
1859 }
1860}
1861
1862static void set_rtcsrc(struct stm32mp1_clk_priv *priv,
1863 unsigned int clksrc,
1864 int lse_css)
1865{
1866 u32 address = priv->base + RCC_BDCR;
1867
1868 if (readl(address) & RCC_BDCR_RTCCKEN)
1869 goto skip_rtc;
1870
1871 if (clksrc == CLK_RTC_DISABLED)
1872 goto skip_rtc;
1873
1874 clrsetbits_le32(address,
1875 RCC_BDCR_RTCSRC_MASK,
1876 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1877
1878 setbits_le32(address, RCC_BDCR_RTCCKEN);
1879
1880skip_rtc:
1881 if (lse_css)
1882 setbits_le32(address, RCC_BDCR_LSECSSON);
1883}
1884
1885static void pkcs_config(struct stm32mp1_clk_priv *priv, u32 pkcs)
1886{
1887 u32 address = priv->base + ((pkcs >> 4) & 0xFFF);
1888 u32 value = pkcs & 0xF;
1889 u32 mask = 0xF;
1890
1891 if (pkcs & BIT(31)) {
1892 mask <<= 4;
1893 value <<= 4;
1894 }
1895 clrsetbits_le32(address, mask, value);
1896}
1897
1898static int stm32mp1_clktree(struct udevice *dev)
1899{
1900 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1901 fdt_addr_t rcc = priv->base;
1902 unsigned int clksrc[CLKSRC_NB];
1903 unsigned int clkdiv[CLKDIV_NB];
1904 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001905 unsigned int pllfracv[_PLL_NB];
1906 unsigned int pllcsg[_PLL_NB][PLLCSG_NB];
1907 bool pllcfg_valid[_PLL_NB];
1908 bool pllcsg_set[_PLL_NB];
1909 int ret;
1910 int i, len;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001911 int lse_css = 0;
1912 const u32 *pkcs_cell;
1913
1914 /* check mandatory field */
1915 ret = dev_read_u32_array(dev, "st,clksrc", clksrc, CLKSRC_NB);
1916 if (ret < 0) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001917 dev_dbg(dev, "field st,clksrc invalid: error %d\n", ret);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001918 return -FDT_ERR_NOTFOUND;
1919 }
1920
1921 ret = dev_read_u32_array(dev, "st,clkdiv", clkdiv, CLKDIV_NB);
1922 if (ret < 0) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001923 dev_dbg(dev, "field st,clkdiv invalid: error %d\n", ret);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001924 return -FDT_ERR_NOTFOUND;
1925 }
1926
1927 /* check mandatory field in each pll */
1928 for (i = 0; i < _PLL_NB; i++) {
1929 char name[12];
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001930 ofnode node;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001931
1932 sprintf(name, "st,pll@%d", i);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001933 node = dev_read_subnode(dev, name);
1934 pllcfg_valid[i] = ofnode_valid(node);
1935 pllcsg_set[i] = false;
1936 if (pllcfg_valid[i]) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001937 dev_dbg(dev, "DT for PLL %d @ %s\n", i, name);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001938 ret = ofnode_read_u32_array(node, "cfg",
1939 pllcfg[i], PLLCFG_NB);
1940 if (ret < 0) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001941 dev_dbg(dev, "field cfg invalid: error %d\n", ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001942 return -FDT_ERR_NOTFOUND;
1943 }
1944 pllfracv[i] = ofnode_read_u32_default(node, "frac", 0);
1945
1946 ret = ofnode_read_u32_array(node, "csg", pllcsg[i],
1947 PLLCSG_NB);
1948 if (!ret) {
1949 pllcsg_set[i] = true;
1950 } else if (ret != -FDT_ERR_NOTFOUND) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001951 dev_dbg(dev, "invalid csg node for pll@%d res=%d\n",
1952 i, ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001953 return ret;
1954 }
1955 } else if (i == _PLL1) {
1956 /* use OPP for PLL1 for A7 CPU */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001957 dev_dbg(dev, "DT for PLL %d with OPP\n", i);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001958 ret = stm32mp1_pll1_opp(priv,
1959 clksrc[CLKSRC_PLL12],
1960 pllcfg[i],
1961 &pllfracv[i]);
1962 if (ret) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001963 dev_dbg(dev, "PLL %d with OPP error = %d\n", i, ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001964 return ret;
1965 }
1966 pllcfg_valid[i] = true;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001967 }
1968 }
1969
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001970 dev_dbg(dev, "configuration MCO\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001971 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1972 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1973
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001974 dev_dbg(dev, "switch ON osillator\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001975 /*
1976 * switch ON oscillator found in device-tree,
1977 * HSI already ON after bootrom
1978 */
Etienne Carriere55a78142021-02-24 11:19:42 +01001979 if (clk_valid(&priv->osc_clk[_LSI]))
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001980 stm32mp1_lsi_set(rcc, 1);
1981
Etienne Carriere55a78142021-02-24 11:19:42 +01001982 if (clk_valid(&priv->osc_clk[_LSE])) {
Patrick Delaunay5ba62a42020-01-28 10:44:15 +01001983 int bypass, digbyp;
1984 u32 lsedrv;
Etienne Carriere55a78142021-02-24 11:19:42 +01001985 struct udevice *dev = priv->osc_clk[_LSE].dev;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001986
1987 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001988 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001989 lse_css = dev_read_bool(dev, "st,css");
1990 lsedrv = dev_read_u32_default(dev, "st,drive",
1991 LSEDRV_MEDIUM_HIGH);
1992
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001993 stm32mp1_lse_enable(rcc, bypass, digbyp, lsedrv);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001994 }
1995
Etienne Carriere55a78142021-02-24 11:19:42 +01001996 if (clk_valid(&priv->osc_clk[_HSE])) {
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001997 int bypass, digbyp, css;
Etienne Carriere55a78142021-02-24 11:19:42 +01001998 struct udevice *dev = priv->osc_clk[_HSE].dev;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001999
2000 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunay80cb5682018-07-16 10:41:46 +02002001 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002002 css = dev_read_bool(dev, "st,css");
2003
Patrick Delaunay80cb5682018-07-16 10:41:46 +02002004 stm32mp1_hse_enable(rcc, bypass, digbyp, css);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002005 }
2006 /* CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
2007 * => switch on CSI even if node is not present in device tree
2008 */
2009 stm32mp1_csi_set(rcc, 1);
2010
2011 /* come back to HSI */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002012 dev_dbg(dev, "come back to HSI\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002013 set_clksrc(priv, CLK_MPU_HSI);
2014 set_clksrc(priv, CLK_AXI_HSI);
2015 set_clksrc(priv, CLK_MCU_HSI);
2016
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002017 dev_dbg(dev, "pll stop\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002018 for (i = 0; i < _PLL_NB; i++)
2019 pll_stop(priv, i);
2020
2021 /* configure HSIDIV */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002022 dev_dbg(dev, "configure HSIDIV\n");
Etienne Carriere55a78142021-02-24 11:19:42 +01002023 if (clk_valid(&priv->osc_clk[_HSI])) {
2024 stm32mp1_hsidiv(rcc, clk_get_rate(&priv->osc_clk[_HSI]));
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01002025 stgen_config(priv);
2026 }
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002027
2028 /* select DIV */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002029 dev_dbg(dev, "select DIV\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002030 /* no ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
2031 writel(clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK, rcc + RCC_MPCKDIVR);
2032 set_clkdiv(clkdiv[CLKDIV_AXI], rcc + RCC_AXIDIVR);
2033 set_clkdiv(clkdiv[CLKDIV_APB4], rcc + RCC_APB4DIVR);
2034 set_clkdiv(clkdiv[CLKDIV_APB5], rcc + RCC_APB5DIVR);
2035 set_clkdiv(clkdiv[CLKDIV_MCU], rcc + RCC_MCUDIVR);
2036 set_clkdiv(clkdiv[CLKDIV_APB1], rcc + RCC_APB1DIVR);
2037 set_clkdiv(clkdiv[CLKDIV_APB2], rcc + RCC_APB2DIVR);
2038 set_clkdiv(clkdiv[CLKDIV_APB3], rcc + RCC_APB3DIVR);
2039
2040 /* no ready bit for RTC */
2041 writel(clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK, rcc + RCC_RTCDIVR);
2042
2043 /* configure PLLs source */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002044 dev_dbg(dev, "configure PLLs source\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002045 set_clksrc(priv, clksrc[CLKSRC_PLL12]);
2046 set_clksrc(priv, clksrc[CLKSRC_PLL3]);
2047 set_clksrc(priv, clksrc[CLKSRC_PLL4]);
2048
2049 /* configure and start PLLs */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002050 dev_dbg(dev, "configure PLLs\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002051 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002052 if (!pllcfg_valid[i])
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002053 continue;
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002054 dev_dbg(dev, "configure PLL %d\n", i);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002055 pll_config(priv, i, pllcfg[i], pllfracv[i]);
2056 if (pllcsg_set[i])
2057 pll_csg(priv, i, pllcsg[i]);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002058 pll_start(priv, i);
2059 }
2060
2061 /* wait and start PLLs ouptut when ready */
2062 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002063 if (!pllcfg_valid[i])
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002064 continue;
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002065 dev_dbg(dev, "output PLL %d\n", i);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002066 pll_output(priv, i, pllcfg[i][PLLCFG_O]);
2067 }
2068
2069 /* wait LSE ready before to use it */
Etienne Carriere55a78142021-02-24 11:19:42 +01002070 if (clk_valid(&priv->osc_clk[_LSE]))
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002071 stm32mp1_lse_wait(rcc);
2072
2073 /* configure with expected clock source */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002074 dev_dbg(dev, "CLKSRC\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002075 set_clksrc(priv, clksrc[CLKSRC_MPU]);
2076 set_clksrc(priv, clksrc[CLKSRC_AXI]);
2077 set_clksrc(priv, clksrc[CLKSRC_MCU]);
2078 set_rtcsrc(priv, clksrc[CLKSRC_RTC], lse_css);
2079
2080 /* configure PKCK */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002081 dev_dbg(dev, "PKCK\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002082 pkcs_cell = dev_read_prop(dev, "st,pkcs", &len);
2083 if (pkcs_cell) {
2084 bool ckper_disabled = false;
2085
2086 for (i = 0; i < len / sizeof(u32); i++) {
2087 u32 pkcs = (u32)fdt32_to_cpu(pkcs_cell[i]);
2088
2089 if (pkcs == CLK_CKPER_DISABLED) {
2090 ckper_disabled = true;
2091 continue;
2092 }
2093 pkcs_config(priv, pkcs);
2094 }
2095 /* CKPER is source for some peripheral clock
2096 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2097 * only if previous clock is still ON
2098 * => deactivated CKPER only after switching clock
2099 */
2100 if (ckper_disabled)
2101 pkcs_config(priv, CLK_CKPER_DISABLED);
2102 }
2103
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01002104 /* STGEN clock source can change with CLK_STGEN_XXX */
2105 stgen_config(priv);
2106
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002107 dev_dbg(dev, "oscillator off\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002108 /* switch OFF HSI if not found in device-tree */
Etienne Carriere55a78142021-02-24 11:19:42 +01002109 if (!clk_valid(&priv->osc_clk[_HSI]))
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002110 stm32mp1_hsi_set(rcc, 0);
2111
2112 /* Software Self-Refresh mode (SSR) during DDR initilialization */
2113 clrsetbits_le32(priv->base + RCC_DDRITFCR,
2114 RCC_DDRITFCR_DDRCKMOD_MASK,
2115 RCC_DDRITFCR_DDRCKMOD_SSR <<
2116 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2117
2118 return 0;
2119}
2120#endif /* STM32MP1_CLOCK_TREE_INIT */
2121
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002122static int pll_set_output_rate(struct udevice *dev,
2123 int pll_id,
2124 int div_id,
2125 unsigned long clk_rate)
2126{
2127 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2128 const struct stm32mp1_clk_pll *pll = priv->data->pll;
2129 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
2130 int div;
2131 ulong fvco;
2132
2133 if (div_id > _DIV_NB)
2134 return -EINVAL;
2135
2136 fvco = pll_get_fvco(priv, pll_id);
2137
2138 if (fvco <= clk_rate)
2139 div = 1;
2140 else
2141 div = DIV_ROUND_UP(fvco, clk_rate);
2142
2143 if (div > 128)
2144 div = 128;
2145
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002146 /* stop the requested output */
2147 clrbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2148 /* change divider */
2149 clrsetbits_le32(priv->base + pll[pll_id].pllxcfgr2,
2150 RCC_PLLNCFGR2_DIVX_MASK << RCC_PLLNCFGR2_SHIFT(div_id),
2151 (div - 1) << RCC_PLLNCFGR2_SHIFT(div_id));
2152 /* start the requested output */
2153 setbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2154
2155 return 0;
2156}
2157
2158static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
2159{
2160 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
2161 int p;
2162
2163 switch (clk->id) {
Patrick Delaunay854c59e2019-04-18 17:32:48 +02002164#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2165 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2166 case DDRPHYC:
2167 break;
2168#endif
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002169 case LTDC_PX:
2170 case DSI_PX:
2171 break;
2172 default:
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002173 dev_err(clk->dev, "Set of clk %ld not supported", clk->id);
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002174 return -EINVAL;
2175 }
2176
2177 p = stm32mp1_clk_get_parent(priv, clk->id);
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002178 dev_vdbg(clk->dev, "parent = %d:%s\n", p, stm32mp1_clk_parent_name[p]);
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002179 if (p < 0)
2180 return -EINVAL;
2181
2182 switch (p) {
Patrick Delaunay854c59e2019-04-18 17:32:48 +02002183#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2184 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2185 case _PLL2_R: /* DDRPHYC */
2186 {
2187 /* only for change DDR clock in interactive mode */
2188 ulong result;
2189
2190 set_clksrc(priv, CLK_AXI_HSI);
2191 result = pll_set_rate(clk->dev, _PLL2, _DIV_R, clk_rate);
2192 set_clksrc(priv, CLK_AXI_PLL2P);
2193 return result;
2194 }
2195#endif
Patrick Delaunaya06a4562019-07-30 19:16:54 +02002196
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002197 case _PLL4_Q:
2198 /* for LTDC_PX and DSI_PX case */
2199 return pll_set_output_rate(clk->dev, _PLL4, _DIV_Q, clk_rate);
2200 }
2201
2202 return -EINVAL;
2203}
2204
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002205static void stm32mp1_osc_init(struct udevice *dev)
2206{
2207 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2208 int i;
2209 const char *name[NB_OSC] = {
Etienne Carriere55a78142021-02-24 11:19:42 +01002210 [_LSI] = "lsi",
2211 [_LSE] = "lse",
2212 [_HSI] = "hsi",
2213 [_HSE] = "hse",
2214 [_CSI] = "csi",
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002215 [_I2S_CKIN] = "i2s_ckin",
Patrick Delaunay7b726532019-01-30 13:07:00 +01002216 };
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002217
2218 for (i = 0; i < NB_OSC; i++) {
Etienne Carriere55a78142021-02-24 11:19:42 +01002219 if (clk_get_by_name(dev, name[i], &priv->osc_clk[i]))
Marek Vasut8dfc4072022-04-22 12:40:39 +02002220 dev_dbg(dev, "No source clock \"%s\"\n", name[i]);
Etienne Carriere55a78142021-02-24 11:19:42 +01002221 else
2222 dev_dbg(dev, "%s clock rate: %luHz\n",
2223 name[i], clk_get_rate(&priv->osc_clk[i]));
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002224 }
2225}
2226
Igor Prusov1a3427b2023-11-09 13:55:15 +03002227static void __maybe_unused stm32mp1_clk_dump(struct udevice *dev)
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002228{
2229 char buf[32];
2230 int i, s, p;
Igor Prusov1a3427b2023-11-09 13:55:15 +03002231 struct stm32mp1_clk_priv *priv;
2232
2233 priv = dev_get_priv(dev);
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002234
2235 printf("Clocks:\n");
2236 for (i = 0; i < _PARENT_NB; i++) {
2237 printf("- %s : %s MHz\n",
2238 stm32mp1_clk_parent_name[i],
2239 strmhz(buf, stm32mp1_clk_get(priv, i)));
2240 }
2241 printf("Source Clocks:\n");
2242 for (i = 0; i < _PARENT_SEL_NB; i++) {
2243 p = (readl(priv->base + priv->data->sel[i].offset) >>
2244 priv->data->sel[i].src) & priv->data->sel[i].msk;
2245 if (p < priv->data->sel[i].nb_parent) {
2246 s = priv->data->sel[i].parent[p];
2247 printf("- %s(%d) => parent %s(%d)\n",
2248 stm32mp1_clk_parent_sel_name[i], i,
2249 stm32mp1_clk_parent_name[s], s);
2250 } else {
2251 printf("- %s(%d) => parent index %d is invalid\n",
2252 stm32mp1_clk_parent_sel_name[i], i, p);
2253 }
2254 }
2255}
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002256
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002257static int stm32mp1_clk_probe(struct udevice *dev)
2258{
2259 int result = 0;
2260 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2261
2262 priv->base = dev_read_addr(dev->parent);
2263 if (priv->base == FDT_ADDR_T_NONE)
2264 return -EINVAL;
2265
2266 priv->data = (void *)&stm32mp1_data;
2267
2268 if (!priv->data->gate || !priv->data->sel ||
2269 !priv->data->pll)
2270 return -EINVAL;
2271
2272 stm32mp1_osc_init(dev);
2273
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002274#ifdef STM32MP1_CLOCK_TREE_INIT
2275 /* clock tree init is done only one time, before relocation */
2276 if (!(gd->flags & GD_FLG_RELOC))
2277 result = stm32mp1_clktree(dev);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002278 if (result)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002279 dev_err(dev, "clock tree initialization failed (%d)\n", result);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002280#endif
2281
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002282#ifndef CONFIG_SPL_BUILD
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002283#if defined(VERBOSE_DEBUG)
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002284 /* display debug information for probe after relocation */
2285 if (gd->flags & GD_FLG_RELOC)
Igor Prusov1a3427b2023-11-09 13:55:15 +03002286 stm32mp1_clk_dump(dev);
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002287#endif
2288
Patrick Delaunaya77c6ed2019-07-30 19:16:55 +02002289 gd->cpu_clk = stm32mp1_clk_get(priv, _CK_MPU);
2290 gd->bus_clk = stm32mp1_clk_get(priv, _ACLK);
2291 /* DDRPHYC father */
2292 gd->mem_clk = stm32mp1_clk_get(priv, _PLL2_R);
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002293#if defined(CONFIG_DISPLAY_CPUINFO)
2294 if (gd->flags & GD_FLG_RELOC) {
2295 char buf[32];
2296
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002297 log_info("Clocks:\n");
2298 log_info("- MPU : %s MHz\n", strmhz(buf, gd->cpu_clk));
2299 log_info("- MCU : %s MHz\n",
2300 strmhz(buf, stm32mp1_clk_get(priv, _CK_MCU)));
2301 log_info("- AXI : %s MHz\n", strmhz(buf, gd->bus_clk));
2302 log_info("- PER : %s MHz\n",
2303 strmhz(buf, stm32mp1_clk_get(priv, _CK_PER)));
2304 log_info("- DDR : %s MHz\n", strmhz(buf, gd->mem_clk));
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002305 }
2306#endif /* CONFIG_DISPLAY_CPUINFO */
2307#endif
2308
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002309 return result;
2310}
2311
2312static const struct clk_ops stm32mp1_clk_ops = {
2313 .enable = stm32mp1_clk_enable,
2314 .disable = stm32mp1_clk_disable,
2315 .get_rate = stm32mp1_clk_get_rate,
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002316 .set_rate = stm32mp1_clk_set_rate,
Igor Prusov1a3427b2023-11-09 13:55:15 +03002317#if IS_ENABLED(CONFIG_CMD_CLK) && !IS_ENABLED(CONFIG_SPL_BUILD)
2318 .dump = stm32mp1_clk_dump,
2319#endif
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002320};
2321
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002322U_BOOT_DRIVER(stm32mp1_clock) = {
2323 .name = "stm32mp1_clk",
2324 .id = UCLASS_CLK,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002325 .ops = &stm32mp1_clk_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002326 .priv_auto = sizeof(struct stm32mp1_clk_priv),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002327 .probe = stm32mp1_clk_probe,
2328};