blob: da95b1ac2f2278309c11c9240ad7b37eba471d87 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunaya6151912018-03-12 10:46:15 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunaya6151912018-03-12 10:46:15 +01004 */
5
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01006#define LOG_CATEGORY UCLASS_CLK
7
Patrick Delaunaya6151912018-03-12 10:46:15 +01008#include <common.h>
9#include <clk-uclass.h>
10#include <div64.h>
11#include <dm.h>
Simon Glass691d7192020-05-10 11:40:02 -060012#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Patrick Delaunaya6151912018-03-12 10:46:15 +010014#include <regmap.h>
15#include <spl.h>
16#include <syscon.h>
Simon Glass10453152019-11-14 12:57:30 -070017#include <time.h>
Simon Glass2189d5f2019-11-14 12:57:20 -070018#include <vsprintf.h>
Patrick Delaunayceab8ee2020-11-06 19:01:45 +010019#include <asm/arch/sys_proto.h>
Simon Glass401d1c42020-10-30 21:38:53 -060020#include <asm/global_data.h>
Patrick Delaunayceab8ee2020-11-06 19:01:45 +010021#include <dm/device_compat.h>
22#include <dt-bindings/clock/stm32mp1-clks.h>
23#include <dt-bindings/clock/stm32mp1-clksrc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Patrick Delaunaya6151912018-03-12 10:46:15 +010025#include <linux/io.h>
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010026#include <linux/iopoll.h>
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010027
Patrick Delaunay4de076e2019-07-30 19:16:55 +020028DECLARE_GLOBAL_DATA_PTR;
29
Patrick Delaunay654706b2020-04-01 09:07:33 +020030#ifndef CONFIG_TFABOOT
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010031#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
32/* activate clock tree initialization in the driver */
33#define STM32MP1_CLOCK_TREE_INIT
34#endif
Patrick Delaunayabf26782019-02-12 11:44:39 +010035#endif
Patrick Delaunaya6151912018-03-12 10:46:15 +010036
37#define MAX_HSI_HZ 64000000
38
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010039/* TIMEOUT */
40#define TIMEOUT_200MS 200000
41#define TIMEOUT_1S 1000000
42
Patrick Delaunay938e0e32018-03-20 11:41:25 +010043/* STGEN registers */
44#define STGENC_CNTCR 0x00
45#define STGENC_CNTSR 0x04
46#define STGENC_CNTCVL 0x08
47#define STGENC_CNTCVU 0x0C
48#define STGENC_CNTFID0 0x20
49
50#define STGENC_CNTCR_EN BIT(0)
51
Patrick Delaunaya6151912018-03-12 10:46:15 +010052/* RCC registers */
53#define RCC_OCENSETR 0x0C
54#define RCC_OCENCLRR 0x10
55#define RCC_HSICFGR 0x18
56#define RCC_MPCKSELR 0x20
57#define RCC_ASSCKSELR 0x24
58#define RCC_RCK12SELR 0x28
59#define RCC_MPCKDIVR 0x2C
60#define RCC_AXIDIVR 0x30
61#define RCC_APB4DIVR 0x3C
62#define RCC_APB5DIVR 0x40
63#define RCC_RTCDIVR 0x44
64#define RCC_MSSCKSELR 0x48
65#define RCC_PLL1CR 0x80
66#define RCC_PLL1CFGR1 0x84
67#define RCC_PLL1CFGR2 0x88
68#define RCC_PLL1FRACR 0x8C
69#define RCC_PLL1CSGR 0x90
70#define RCC_PLL2CR 0x94
71#define RCC_PLL2CFGR1 0x98
72#define RCC_PLL2CFGR2 0x9C
73#define RCC_PLL2FRACR 0xA0
74#define RCC_PLL2CSGR 0xA4
75#define RCC_I2C46CKSELR 0xC0
Patrick Delaunayd974afe2021-07-09 14:24:34 +020076#define RCC_SPI6CKSELR 0xC4
Patrick Delaunaya6151912018-03-12 10:46:15 +010077#define RCC_CPERCKSELR 0xD0
78#define RCC_STGENCKSELR 0xD4
79#define RCC_DDRITFCR 0xD8
80#define RCC_BDCR 0x140
81#define RCC_RDLSICR 0x144
82#define RCC_MP_APB4ENSETR 0x200
83#define RCC_MP_APB5ENSETR 0x208
84#define RCC_MP_AHB5ENSETR 0x210
85#define RCC_MP_AHB6ENSETR 0x218
86#define RCC_OCRDYR 0x808
87#define RCC_DBGCFGR 0x80C
88#define RCC_RCK3SELR 0x820
89#define RCC_RCK4SELR 0x824
90#define RCC_MCUDIVR 0x830
91#define RCC_APB1DIVR 0x834
92#define RCC_APB2DIVR 0x838
93#define RCC_APB3DIVR 0x83C
94#define RCC_PLL3CR 0x880
95#define RCC_PLL3CFGR1 0x884
96#define RCC_PLL3CFGR2 0x888
97#define RCC_PLL3FRACR 0x88C
98#define RCC_PLL3CSGR 0x890
99#define RCC_PLL4CR 0x894
100#define RCC_PLL4CFGR1 0x898
101#define RCC_PLL4CFGR2 0x89C
102#define RCC_PLL4FRACR 0x8A0
103#define RCC_PLL4CSGR 0x8A4
104#define RCC_I2C12CKSELR 0x8C0
105#define RCC_I2C35CKSELR 0x8C4
Patrice Chotard248278d2019-04-30 18:08:27 +0200106#define RCC_SPI2S1CKSELR 0x8D8
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200107#define RCC_SPI2S23CKSELR 0x8DC
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100108#define RCC_SPI45CKSELR 0x8E0
Patrick Delaunaya6151912018-03-12 10:46:15 +0100109#define RCC_UART6CKSELR 0x8E4
110#define RCC_UART24CKSELR 0x8E8
111#define RCC_UART35CKSELR 0x8EC
112#define RCC_UART78CKSELR 0x8F0
113#define RCC_SDMMC12CKSELR 0x8F4
114#define RCC_SDMMC3CKSELR 0x8F8
115#define RCC_ETHCKSELR 0x8FC
116#define RCC_QSPICKSELR 0x900
117#define RCC_FMCCKSELR 0x904
118#define RCC_USBCKSELR 0x91C
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200119#define RCC_DSICKSELR 0x924
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200120#define RCC_ADCCKSELR 0x928
Patrick Delaunaya6151912018-03-12 10:46:15 +0100121#define RCC_MP_APB1ENSETR 0xA00
122#define RCC_MP_APB2ENSETR 0XA08
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200123#define RCC_MP_APB3ENSETR 0xA10
Patrick Delaunaya6151912018-03-12 10:46:15 +0100124#define RCC_MP_AHB2ENSETR 0xA18
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100125#define RCC_MP_AHB3ENSETR 0xA20
Patrick Delaunaya6151912018-03-12 10:46:15 +0100126#define RCC_MP_AHB4ENSETR 0xA28
127
128/* used for most of SELR register */
129#define RCC_SELR_SRC_MASK GENMASK(2, 0)
130#define RCC_SELR_SRCRDY BIT(31)
131
132/* Values of RCC_MPCKSELR register */
133#define RCC_MPCKSELR_HSI 0
134#define RCC_MPCKSELR_HSE 1
135#define RCC_MPCKSELR_PLL 2
136#define RCC_MPCKSELR_PLL_MPUDIV 3
137
138/* Values of RCC_ASSCKSELR register */
139#define RCC_ASSCKSELR_HSI 0
140#define RCC_ASSCKSELR_HSE 1
141#define RCC_ASSCKSELR_PLL 2
142
143/* Values of RCC_MSSCKSELR register */
144#define RCC_MSSCKSELR_HSI 0
145#define RCC_MSSCKSELR_HSE 1
146#define RCC_MSSCKSELR_CSI 2
147#define RCC_MSSCKSELR_PLL 3
148
149/* Values of RCC_CPERCKSELR register */
150#define RCC_CPERCKSELR_HSI 0
151#define RCC_CPERCKSELR_CSI 1
152#define RCC_CPERCKSELR_HSE 2
153
154/* used for most of DIVR register : max div for RTC */
155#define RCC_DIVR_DIV_MASK GENMASK(5, 0)
156#define RCC_DIVR_DIVRDY BIT(31)
157
158/* Masks for specific DIVR registers */
159#define RCC_APBXDIV_MASK GENMASK(2, 0)
160#define RCC_MPUDIV_MASK GENMASK(2, 0)
161#define RCC_AXIDIV_MASK GENMASK(2, 0)
162#define RCC_MCUDIV_MASK GENMASK(3, 0)
163
164/* offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */
165#define RCC_MP_ENCLRR_OFFSET 4
166
167/* Fields of RCC_BDCR register */
168#define RCC_BDCR_LSEON BIT(0)
169#define RCC_BDCR_LSEBYP BIT(1)
170#define RCC_BDCR_LSERDY BIT(2)
Patrick Delaunayd2194152018-07-16 10:41:46 +0200171#define RCC_BDCR_DIGBYP BIT(3)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100172#define RCC_BDCR_LSEDRV_MASK GENMASK(5, 4)
173#define RCC_BDCR_LSEDRV_SHIFT 4
174#define RCC_BDCR_LSECSSON BIT(8)
175#define RCC_BDCR_RTCCKEN BIT(20)
176#define RCC_BDCR_RTCSRC_MASK GENMASK(17, 16)
177#define RCC_BDCR_RTCSRC_SHIFT 16
178
179/* Fields of RCC_RDLSICR register */
180#define RCC_RDLSICR_LSION BIT(0)
181#define RCC_RDLSICR_LSIRDY BIT(1)
182
183/* used for ALL PLLNCR registers */
184#define RCC_PLLNCR_PLLON BIT(0)
185#define RCC_PLLNCR_PLLRDY BIT(1)
Patrick Delaunaybbd108a2019-01-30 13:07:06 +0100186#define RCC_PLLNCR_SSCG_CTRL BIT(2)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100187#define RCC_PLLNCR_DIVPEN BIT(4)
188#define RCC_PLLNCR_DIVQEN BIT(5)
189#define RCC_PLLNCR_DIVREN BIT(6)
190#define RCC_PLLNCR_DIVEN_SHIFT 4
191
192/* used for ALL PLLNCFGR1 registers */
193#define RCC_PLLNCFGR1_DIVM_SHIFT 16
194#define RCC_PLLNCFGR1_DIVM_MASK GENMASK(21, 16)
195#define RCC_PLLNCFGR1_DIVN_SHIFT 0
196#define RCC_PLLNCFGR1_DIVN_MASK GENMASK(8, 0)
197/* only for PLL3 and PLL4 */
198#define RCC_PLLNCFGR1_IFRGE_SHIFT 24
199#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK(25, 24)
200
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200201/* used for ALL PLLNCFGR2 registers , using stm32mp1_div_id */
202#define RCC_PLLNCFGR2_SHIFT(div_id) ((div_id) * 8)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100203#define RCC_PLLNCFGR2_DIVX_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200204#define RCC_PLLNCFGR2_DIVP_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_P)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100205#define RCC_PLLNCFGR2_DIVP_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200206#define RCC_PLLNCFGR2_DIVQ_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_Q)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100207#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK(14, 8)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200208#define RCC_PLLNCFGR2_DIVR_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_R)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100209#define RCC_PLLNCFGR2_DIVR_MASK GENMASK(22, 16)
210
211/* used for ALL PLLNFRACR registers */
212#define RCC_PLLNFRACR_FRACV_SHIFT 3
213#define RCC_PLLNFRACR_FRACV_MASK GENMASK(15, 3)
214#define RCC_PLLNFRACR_FRACLE BIT(16)
215
216/* used for ALL PLLNCSGR registers */
217#define RCC_PLLNCSGR_INC_STEP_SHIFT 16
218#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK(30, 16)
219#define RCC_PLLNCSGR_MOD_PER_SHIFT 0
220#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK(12, 0)
221#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15
222#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15)
223
224/* used for RCC_OCENSETR and RCC_OCENCLRR registers */
225#define RCC_OCENR_HSION BIT(0)
226#define RCC_OCENR_CSION BIT(4)
Patrick Delaunayd2194152018-07-16 10:41:46 +0200227#define RCC_OCENR_DIGBYP BIT(7)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100228#define RCC_OCENR_HSEON BIT(8)
229#define RCC_OCENR_HSEBYP BIT(10)
230#define RCC_OCENR_HSECSSON BIT(11)
231
232/* Fields of RCC_OCRDYR register */
233#define RCC_OCRDYR_HSIRDY BIT(0)
234#define RCC_OCRDYR_HSIDIVRDY BIT(2)
235#define RCC_OCRDYR_CSIRDY BIT(4)
236#define RCC_OCRDYR_HSERDY BIT(8)
237
238/* Fields of DDRITFCR register */
239#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
240#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20
241#define RCC_DDRITFCR_DDRCKMOD_SSR 0
242
243/* Fields of RCC_HSICFGR register */
244#define RCC_HSICFGR_HSIDIV_MASK GENMASK(1, 0)
245
246/* used for MCO related operations */
247#define RCC_MCOCFG_MCOON BIT(12)
248#define RCC_MCOCFG_MCODIV_MASK GENMASK(7, 4)
249#define RCC_MCOCFG_MCODIV_SHIFT 4
250#define RCC_MCOCFG_MCOSRC_MASK GENMASK(2, 0)
251
252enum stm32mp1_parent_id {
253/*
254 * _HSI, _HSE, _CSI, _LSI, _LSE should not be moved
Etienne Carriere08db5d52021-02-24 11:19:42 +0100255 * they are used as index in osc_clk[] as clock reference
Patrick Delaunaya6151912018-03-12 10:46:15 +0100256 */
257 _HSI,
258 _HSE,
259 _CSI,
260 _LSI,
261 _LSE,
262 _I2S_CKIN,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100263 NB_OSC,
264
265/* other parent source */
266 _HSI_KER = NB_OSC,
267 _HSE_KER,
268 _HSE_KER_DIV2,
269 _CSI_KER,
270 _PLL1_P,
271 _PLL1_Q,
272 _PLL1_R,
273 _PLL2_P,
274 _PLL2_Q,
275 _PLL2_R,
276 _PLL3_P,
277 _PLL3_Q,
278 _PLL3_R,
279 _PLL4_P,
280 _PLL4_Q,
281 _PLL4_R,
282 _ACLK,
283 _PCLK1,
284 _PCLK2,
285 _PCLK3,
286 _PCLK4,
287 _PCLK5,
288 _HCLK6,
289 _HCLK2,
290 _CK_PER,
291 _CK_MPU,
292 _CK_MCU,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200293 _DSI_PHY,
Patrick Delaunay86617dd2019-01-30 13:07:00 +0100294 _USB_PHY_48,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100295 _PARENT_NB,
296 _UNKNOWN_ID = 0xff,
297};
298
299enum stm32mp1_parent_sel {
300 _I2C12_SEL,
301 _I2C35_SEL,
302 _I2C46_SEL,
303 _UART6_SEL,
304 _UART24_SEL,
305 _UART35_SEL,
306 _UART78_SEL,
307 _SDMMC12_SEL,
308 _SDMMC3_SEL,
309 _ETH_SEL,
310 _QSPI_SEL,
311 _FMC_SEL,
312 _USBPHY_SEL,
313 _USBO_SEL,
314 _STGEN_SEL,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200315 _DSI_SEL,
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200316 _ADC12_SEL,
Patrice Chotard248278d2019-04-30 18:08:27 +0200317 _SPI1_SEL,
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200318 _SPI23_SEL,
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100319 _SPI45_SEL,
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200320 _SPI6_SEL,
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200321 _RTC_SEL,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100322 _PARENT_SEL_NB,
323 _UNKNOWN_SEL = 0xff,
324};
325
326enum stm32mp1_pll_id {
327 _PLL1,
328 _PLL2,
329 _PLL3,
330 _PLL4,
331 _PLL_NB
332};
333
334enum stm32mp1_div_id {
335 _DIV_P,
336 _DIV_Q,
337 _DIV_R,
338 _DIV_NB,
339};
340
341enum stm32mp1_clksrc_id {
342 CLKSRC_MPU,
343 CLKSRC_AXI,
344 CLKSRC_MCU,
345 CLKSRC_PLL12,
346 CLKSRC_PLL3,
347 CLKSRC_PLL4,
348 CLKSRC_RTC,
349 CLKSRC_MCO1,
350 CLKSRC_MCO2,
351 CLKSRC_NB
352};
353
354enum stm32mp1_clkdiv_id {
355 CLKDIV_MPU,
356 CLKDIV_AXI,
357 CLKDIV_MCU,
358 CLKDIV_APB1,
359 CLKDIV_APB2,
360 CLKDIV_APB3,
361 CLKDIV_APB4,
362 CLKDIV_APB5,
363 CLKDIV_RTC,
364 CLKDIV_MCO1,
365 CLKDIV_MCO2,
366 CLKDIV_NB
367};
368
369enum stm32mp1_pllcfg {
370 PLLCFG_M,
371 PLLCFG_N,
372 PLLCFG_P,
373 PLLCFG_Q,
374 PLLCFG_R,
375 PLLCFG_O,
376 PLLCFG_NB
377};
378
379enum stm32mp1_pllcsg {
380 PLLCSG_MOD_PER,
381 PLLCSG_INC_STEP,
382 PLLCSG_SSCG_MODE,
383 PLLCSG_NB
384};
385
386enum stm32mp1_plltype {
387 PLL_800,
388 PLL_1600,
389 PLL_TYPE_NB
390};
391
392struct stm32mp1_pll {
393 u8 refclk_min;
394 u8 refclk_max;
395 u8 divn_max;
396};
397
398struct stm32mp1_clk_gate {
399 u16 offset;
400 u8 bit;
401 u8 index;
402 u8 set_clr;
403 u8 sel;
404 u8 fixed;
405};
406
407struct stm32mp1_clk_sel {
408 u16 offset;
409 u8 src;
410 u8 msk;
411 u8 nb_parent;
412 const u8 *parent;
413};
414
415#define REFCLK_SIZE 4
416struct stm32mp1_clk_pll {
417 enum stm32mp1_plltype plltype;
418 u16 rckxselr;
419 u16 pllxcfgr1;
420 u16 pllxcfgr2;
421 u16 pllxfracr;
422 u16 pllxcr;
423 u16 pllxcsgr;
424 u8 refclk[REFCLK_SIZE];
425};
426
427struct stm32mp1_clk_data {
428 const struct stm32mp1_clk_gate *gate;
429 const struct stm32mp1_clk_sel *sel;
430 const struct stm32mp1_clk_pll *pll;
431 const int nb_gate;
432};
433
434struct stm32mp1_clk_priv {
435 fdt_addr_t base;
436 const struct stm32mp1_clk_data *data;
Etienne Carriere08db5d52021-02-24 11:19:42 +0100437 struct clk osc_clk[NB_OSC];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100438};
439
440#define STM32MP1_CLK(off, b, idx, s) \
441 { \
442 .offset = (off), \
443 .bit = (b), \
444 .index = (idx), \
445 .set_clr = 0, \
446 .sel = (s), \
447 .fixed = _UNKNOWN_ID, \
448 }
449
450#define STM32MP1_CLK_F(off, b, idx, f) \
451 { \
452 .offset = (off), \
453 .bit = (b), \
454 .index = (idx), \
455 .set_clr = 0, \
456 .sel = _UNKNOWN_SEL, \
457 .fixed = (f), \
458 }
459
460#define STM32MP1_CLK_SET_CLR(off, b, idx, s) \
461 { \
462 .offset = (off), \
463 .bit = (b), \
464 .index = (idx), \
465 .set_clr = 1, \
466 .sel = (s), \
467 .fixed = _UNKNOWN_ID, \
468 }
469
470#define STM32MP1_CLK_SET_CLR_F(off, b, idx, f) \
471 { \
472 .offset = (off), \
473 .bit = (b), \
474 .index = (idx), \
475 .set_clr = 1, \
476 .sel = _UNKNOWN_SEL, \
477 .fixed = (f), \
478 }
479
480#define STM32MP1_CLK_PARENT(idx, off, s, m, p) \
481 [(idx)] = { \
482 .offset = (off), \
483 .src = (s), \
484 .msk = (m), \
485 .parent = (p), \
486 .nb_parent = ARRAY_SIZE((p)) \
487 }
488
489#define STM32MP1_CLK_PLL(idx, type, off1, off2, off3, off4, off5, off6,\
490 p1, p2, p3, p4) \
491 [(idx)] = { \
492 .plltype = (type), \
493 .rckxselr = (off1), \
494 .pllxcfgr1 = (off2), \
495 .pllxcfgr2 = (off3), \
496 .pllxfracr = (off4), \
497 .pllxcr = (off5), \
498 .pllxcsgr = (off6), \
499 .refclk[0] = (p1), \
500 .refclk[1] = (p2), \
501 .refclk[2] = (p3), \
502 .refclk[3] = (p4), \
503 }
504
505static const u8 stm32mp1_clks[][2] = {
506 {CK_PER, _CK_PER},
507 {CK_MPU, _CK_MPU},
508 {CK_AXI, _ACLK},
509 {CK_MCU, _CK_MCU},
510 {CK_HSE, _HSE},
511 {CK_CSI, _CSI},
512 {CK_LSI, _LSI},
513 {CK_LSE, _LSE},
514 {CK_HSI, _HSI},
515 {CK_HSE_DIV2, _HSE_KER_DIV2},
516};
517
518static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
519 STM32MP1_CLK(RCC_DDRITFCR, 0, DDRC1, _UNKNOWN_SEL),
520 STM32MP1_CLK(RCC_DDRITFCR, 1, DDRC1LP, _UNKNOWN_SEL),
521 STM32MP1_CLK(RCC_DDRITFCR, 2, DDRC2, _UNKNOWN_SEL),
522 STM32MP1_CLK(RCC_DDRITFCR, 3, DDRC2LP, _UNKNOWN_SEL),
523 STM32MP1_CLK_F(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
524 STM32MP1_CLK(RCC_DDRITFCR, 5, DDRPHYCLP, _UNKNOWN_SEL),
525 STM32MP1_CLK(RCC_DDRITFCR, 6, DDRCAPB, _UNKNOWN_SEL),
526 STM32MP1_CLK(RCC_DDRITFCR, 7, DDRCAPBLP, _UNKNOWN_SEL),
527 STM32MP1_CLK(RCC_DDRITFCR, 8, AXIDCG, _UNKNOWN_SEL),
528 STM32MP1_CLK(RCC_DDRITFCR, 9, DDRPHYCAPB, _UNKNOWN_SEL),
529 STM32MP1_CLK(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _UNKNOWN_SEL),
530
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200531 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 11, SPI2_K, _SPI23_SEL),
532 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 12, SPI3_K, _SPI23_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100533 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
534 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
535 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
536 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
537 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
538 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
539 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
540 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
541 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
542 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
543
Patrice Chotard248278d2019-04-30 18:08:27 +0200544 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 8, SPI1_K, _SPI1_SEL),
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200545 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 9, SPI4_K, _SPI45_SEL),
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100546 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 10, SPI5_K, _SPI45_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100547 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
548
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200549 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
Patrick Delaunay31058362021-06-29 12:04:22 +0200550 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_SEL),
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200551
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200552 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 0, LTDC_PX, _PLL4_Q),
553 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 4, DSI_PX, _PLL4_Q),
554 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 4, DSI_K, _DSI_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100555 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
556 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
557 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
558
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200559 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100560 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
Patrick Delaunay789d7642021-01-22 15:34:25 +0100561 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200562 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100563 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
564
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200565 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB2ENSETR, 5, ADC12, _HCLK2),
566 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 5, ADC12_K, _ADC12_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100567 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
568 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
569
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100570 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 11, HSEM, _UNKNOWN_SEL),
Patrick Delaunayd661f612019-01-30 13:07:01 +0100571 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 12, IPCC, _UNKNOWN_SEL),
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100572
Patrick Delaunaya6151912018-03-12 10:46:15 +0100573 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
574 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
575 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
576 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
577 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
578 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
579 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
580 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
581 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
582 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
583 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
584
585 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
Sughosh Ganu82ebf0f2019-12-28 23:58:28 +0530586 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 6, RNG1_K, _UNKNOWN_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100587
Patrick Delaunayf6ccdda2019-05-17 15:08:42 +0200588 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK_K, _ETH_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100589 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
590 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100591 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
592 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
593 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
594 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
595 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
596 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
597
598 STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200599
600 STM32MP1_CLK(RCC_BDCR, 20, RTC, _RTC_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100601};
602
603static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
604static const u8 i2c35_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
605static const u8 i2c46_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER};
606static const u8 uart6_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
607 _HSE_KER};
608static const u8 uart24_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
609 _HSE_KER};
610static const u8 uart35_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
611 _HSE_KER};
612static const u8 uart78_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
613 _HSE_KER};
614static const u8 sdmmc12_parents[] = {_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER};
615static const u8 sdmmc3_parents[] = {_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER};
616static const u8 eth_parents[] = {_PLL4_P, _PLL3_Q};
617static const u8 qspi_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
618static const u8 fmc_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
619static const u8 usbphy_parents[] = {_HSE_KER, _PLL4_R, _HSE_KER_DIV2};
620static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
621static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200622static const u8 dsi_parents[] = {_DSI_PHY, _PLL4_P};
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200623static const u8 adc_parents[] = {_PLL4_R, _CK_PER, _PLL3_Q};
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200624/* same parents for SPI1=RCC_SPI2S1CKSELR and SPI2&3 = RCC_SPI2S23CKSELR */
Patrice Chotard248278d2019-04-30 18:08:27 +0200625static const u8 spi_parents[] = {_PLL4_P, _PLL3_Q, _I2S_CKIN, _CK_PER,
626 _PLL3_R};
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100627static const u8 spi45_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
628 _HSE_KER};
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200629static const u8 spi6_parents[] = {_PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER,
630 _HSE_KER, _PLL3_Q};
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200631static const u8 rtc_parents[] = {_UNKNOWN_ID, _LSE, _LSI, _HSE};
Patrick Delaunaya6151912018-03-12 10:46:15 +0100632
633static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
634 STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
635 STM32MP1_CLK_PARENT(_I2C35_SEL, RCC_I2C35CKSELR, 0, 0x7, i2c35_parents),
636 STM32MP1_CLK_PARENT(_I2C46_SEL, RCC_I2C46CKSELR, 0, 0x7, i2c46_parents),
637 STM32MP1_CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents),
638 STM32MP1_CLK_PARENT(_UART24_SEL, RCC_UART24CKSELR, 0, 0x7,
639 uart24_parents),
640 STM32MP1_CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7,
641 uart35_parents),
642 STM32MP1_CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7,
643 uart78_parents),
644 STM32MP1_CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7,
645 sdmmc12_parents),
646 STM32MP1_CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7,
647 sdmmc3_parents),
648 STM32MP1_CLK_PARENT(_ETH_SEL, RCC_ETHCKSELR, 0, 0x3, eth_parents),
Patrick Delaunay69ffb552020-03-09 14:59:22 +0100649 STM32MP1_CLK_PARENT(_QSPI_SEL, RCC_QSPICKSELR, 0, 0x3, qspi_parents),
650 STM32MP1_CLK_PARENT(_FMC_SEL, RCC_FMCCKSELR, 0, 0x3, fmc_parents),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100651 STM32MP1_CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents),
652 STM32MP1_CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents),
653 STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200654 STM32MP1_CLK_PARENT(_DSI_SEL, RCC_DSICKSELR, 0, 0x1, dsi_parents),
Patrick Delaunay69ffb552020-03-09 14:59:22 +0100655 STM32MP1_CLK_PARENT(_ADC12_SEL, RCC_ADCCKSELR, 0, 0x3, adc_parents),
Patrice Chotard248278d2019-04-30 18:08:27 +0200656 STM32MP1_CLK_PARENT(_SPI1_SEL, RCC_SPI2S1CKSELR, 0, 0x7, spi_parents),
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200657 STM32MP1_CLK_PARENT(_SPI23_SEL, RCC_SPI2S23CKSELR, 0, 0x7, spi_parents),
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100658 STM32MP1_CLK_PARENT(_SPI45_SEL, RCC_SPI45CKSELR, 0, 0x7, spi45_parents),
Patrick Delaunayd974afe2021-07-09 14:24:34 +0200659 STM32MP1_CLK_PARENT(_SPI6_SEL, RCC_SPI6CKSELR, 0, 0x7, spi6_parents),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200660 STM32MP1_CLK_PARENT(_RTC_SEL, RCC_BDCR, RCC_BDCR_RTCSRC_SHIFT,
661 (RCC_BDCR_RTCSRC_MASK >> RCC_BDCR_RTCSRC_SHIFT),
662 rtc_parents),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100663};
664
665#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay37ad8372020-05-25 12:19:44 +0200666
Patrick Delaunaya6151912018-03-12 10:46:15 +0100667/* define characteristic of PLL according type */
Patrick Delaunay37ad8372020-05-25 12:19:44 +0200668#define DIVM_MIN 0
669#define DIVM_MAX 63
Patrick Delaunaya6151912018-03-12 10:46:15 +0100670#define DIVN_MIN 24
Patrick Delaunay37ad8372020-05-25 12:19:44 +0200671#define DIVP_MIN 0
672#define DIVP_MAX 127
673#define FRAC_MAX 8192
674
675#define PLL1600_VCO_MIN 800000000
676#define PLL1600_VCO_MAX 1600000000
677
Patrick Delaunaya6151912018-03-12 10:46:15 +0100678static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
679 [PLL_800] = {
680 .refclk_min = 4,
681 .refclk_max = 16,
682 .divn_max = 99,
683 },
684 [PLL_1600] = {
685 .refclk_min = 8,
686 .refclk_max = 16,
687 .divn_max = 199,
688 },
689};
690#endif /* STM32MP1_CLOCK_TREE_INIT */
691
692static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
693 STM32MP1_CLK_PLL(_PLL1, PLL_1600,
694 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
695 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
696 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
697 STM32MP1_CLK_PLL(_PLL2, PLL_1600,
698 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
699 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
700 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
701 STM32MP1_CLK_PLL(_PLL3, PLL_800,
702 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
703 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
704 _HSI, _HSE, _CSI, _UNKNOWN_ID),
705 STM32MP1_CLK_PLL(_PLL4, PLL_800,
706 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
707 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
708 _HSI, _HSE, _CSI, _I2S_CKIN),
709};
710
711/* Prescaler table lookups for clock computation */
712/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
713static const u8 stm32mp1_mcu_div[16] = {
714 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
715};
716
717/* div = /1 /2 /4 /8 /16 : same divider for pmu and apbx*/
718#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
719#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
720static const u8 stm32mp1_mpu_apbx_div[8] = {
721 0, 1, 2, 3, 4, 4, 4, 4
722};
723
724/* div = /1 /2 /3 /4 */
725static const u8 stm32mp1_axi_div[8] = {
726 1, 2, 3, 4, 4, 4, 4, 4
727};
728
Patrick Delaunay8d6310a2019-01-30 13:07:04 +0100729static const __maybe_unused
730char * const stm32mp1_clk_parent_name[_PARENT_NB] = {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100731 [_HSI] = "HSI",
732 [_HSE] = "HSE",
733 [_CSI] = "CSI",
734 [_LSI] = "LSI",
735 [_LSE] = "LSE",
736 [_I2S_CKIN] = "I2S_CKIN",
737 [_HSI_KER] = "HSI_KER",
738 [_HSE_KER] = "HSE_KER",
739 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
740 [_CSI_KER] = "CSI_KER",
741 [_PLL1_P] = "PLL1_P",
742 [_PLL1_Q] = "PLL1_Q",
743 [_PLL1_R] = "PLL1_R",
744 [_PLL2_P] = "PLL2_P",
745 [_PLL2_Q] = "PLL2_Q",
746 [_PLL2_R] = "PLL2_R",
747 [_PLL3_P] = "PLL3_P",
748 [_PLL3_Q] = "PLL3_Q",
749 [_PLL3_R] = "PLL3_R",
750 [_PLL4_P] = "PLL4_P",
751 [_PLL4_Q] = "PLL4_Q",
752 [_PLL4_R] = "PLL4_R",
753 [_ACLK] = "ACLK",
754 [_PCLK1] = "PCLK1",
755 [_PCLK2] = "PCLK2",
756 [_PCLK3] = "PCLK3",
757 [_PCLK4] = "PCLK4",
758 [_PCLK5] = "PCLK5",
759 [_HCLK6] = "KCLK6",
760 [_HCLK2] = "HCLK2",
761 [_CK_PER] = "CK_PER",
762 [_CK_MPU] = "CK_MPU",
763 [_CK_MCU] = "CK_MCU",
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200764 [_USB_PHY_48] = "USB_PHY_48",
765 [_DSI_PHY] = "DSI_PHY_PLL",
Patrick Delaunaya6151912018-03-12 10:46:15 +0100766};
767
Patrick Delaunay8d6310a2019-01-30 13:07:04 +0100768static const __maybe_unused
769char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100770 [_I2C12_SEL] = "I2C12",
771 [_I2C35_SEL] = "I2C35",
772 [_I2C46_SEL] = "I2C46",
773 [_UART6_SEL] = "UART6",
774 [_UART24_SEL] = "UART24",
775 [_UART35_SEL] = "UART35",
776 [_UART78_SEL] = "UART78",
777 [_SDMMC12_SEL] = "SDMMC12",
778 [_SDMMC3_SEL] = "SDMMC3",
779 [_ETH_SEL] = "ETH",
780 [_QSPI_SEL] = "QSPI",
781 [_FMC_SEL] = "FMC",
782 [_USBPHY_SEL] = "USBPHY",
783 [_USBO_SEL] = "USBO",
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200784 [_STGEN_SEL] = "STGEN",
785 [_DSI_SEL] = "DSI",
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200786 [_ADC12_SEL] = "ADC12",
Patrice Chotard248278d2019-04-30 18:08:27 +0200787 [_SPI1_SEL] = "SPI1",
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100788 [_SPI45_SEL] = "SPI45",
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200789 [_RTC_SEL] = "RTC",
Patrick Delaunaya6151912018-03-12 10:46:15 +0100790};
Patrick Delaunaya6151912018-03-12 10:46:15 +0100791
792static const struct stm32mp1_clk_data stm32mp1_data = {
793 .gate = stm32mp1_clk_gate,
794 .sel = stm32mp1_clk_sel,
795 .pll = stm32mp1_clk_pll,
796 .nb_gate = ARRAY_SIZE(stm32mp1_clk_gate),
797};
798
799static ulong stm32mp1_clk_get_fixed(struct stm32mp1_clk_priv *priv, int idx)
800{
801 if (idx >= NB_OSC) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100802 log_debug("clk id %d not found\n", idx);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100803 return 0;
804 }
805
Etienne Carriere08db5d52021-02-24 11:19:42 +0100806 return clk_get_rate(&priv->osc_clk[idx]);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100807}
808
809static int stm32mp1_clk_get_id(struct stm32mp1_clk_priv *priv, unsigned long id)
810{
811 const struct stm32mp1_clk_gate *gate = priv->data->gate;
812 int i, nb_clks = priv->data->nb_gate;
813
814 for (i = 0; i < nb_clks; i++) {
815 if (gate[i].index == id)
816 break;
817 }
818
819 if (i == nb_clks) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100820 log_err("clk id %d not found\n", (u32)id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100821 return -EINVAL;
822 }
823
824 return i;
825}
826
827static int stm32mp1_clk_get_sel(struct stm32mp1_clk_priv *priv,
828 int i)
829{
830 const struct stm32mp1_clk_gate *gate = priv->data->gate;
831
832 if (gate[i].sel > _PARENT_SEL_NB) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100833 log_err("parents for clk id %d not found\n", i);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100834 return -EINVAL;
835 }
836
837 return gate[i].sel;
838}
839
840static int stm32mp1_clk_get_fixed_parent(struct stm32mp1_clk_priv *priv,
841 int i)
842{
843 const struct stm32mp1_clk_gate *gate = priv->data->gate;
844
845 if (gate[i].fixed == _UNKNOWN_ID)
846 return -ENOENT;
847
848 return gate[i].fixed;
849}
850
851static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
852 unsigned long id)
853{
854 const struct stm32mp1_clk_sel *sel = priv->data->sel;
855 int i;
856 int s, p;
Patrick Delaunay67d74ce2019-06-21 15:26:48 +0200857 unsigned int idx;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100858
Patrick Delaunay67d74ce2019-06-21 15:26:48 +0200859 for (idx = 0; idx < ARRAY_SIZE(stm32mp1_clks); idx++)
860 if (stm32mp1_clks[idx][0] == id)
861 return stm32mp1_clks[idx][1];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100862
863 i = stm32mp1_clk_get_id(priv, id);
864 if (i < 0)
865 return i;
866
867 p = stm32mp1_clk_get_fixed_parent(priv, i);
868 if (p >= 0 && p < _PARENT_NB)
869 return p;
870
871 s = stm32mp1_clk_get_sel(priv, i);
872 if (s < 0)
873 return s;
874
875 p = (readl(priv->base + sel[s].offset) >> sel[s].src) & sel[s].msk;
876
877 if (p < sel[s].nb_parent) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100878 log_content("%s clock is the parent %s of clk id %d\n",
879 stm32mp1_clk_parent_name[sel[s].parent[p]],
880 stm32mp1_clk_parent_sel_name[s],
881 (u32)id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100882 return sel[s].parent[p];
883 }
884
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100885 log_err("no parents defined for clk id %d\n", (u32)id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100886
887 return -EINVAL;
888}
889
Patrick Delaunay61105032018-07-16 10:41:42 +0200890static ulong pll_get_fref_ck(struct stm32mp1_clk_priv *priv,
891 int pll_id)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100892{
893 const struct stm32mp1_clk_pll *pll = priv->data->pll;
Patrick Delaunay61105032018-07-16 10:41:42 +0200894 u32 selr;
895 int src;
896 ulong refclk;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100897
Patrick Delaunay61105032018-07-16 10:41:42 +0200898 /* Get current refclk */
Patrick Delaunaya6151912018-03-12 10:46:15 +0100899 selr = readl(priv->base + pll[pll_id].rckxselr);
Patrick Delaunay61105032018-07-16 10:41:42 +0200900 src = selr & RCC_SELR_SRC_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100901
Patrick Delaunay61105032018-07-16 10:41:42 +0200902 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]);
Patrick Delaunay61105032018-07-16 10:41:42 +0200903
904 return refclk;
905}
906
907/*
908 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
909 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
910 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
911 * => in all the case Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
912 */
913static ulong pll_get_fvco(struct stm32mp1_clk_priv *priv,
914 int pll_id)
915{
916 const struct stm32mp1_clk_pll *pll = priv->data->pll;
917 int divm, divn;
918 ulong refclk, fvco;
919 u32 cfgr1, fracr;
920
921 cfgr1 = readl(priv->base + pll[pll_id].pllxcfgr1);
922 fracr = readl(priv->base + pll[pll_id].pllxfracr);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100923
924 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
925 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100926
Patrick Delaunay61105032018-07-16 10:41:42 +0200927 refclk = pll_get_fref_ck(priv, pll_id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100928
Patrick Delaunay61105032018-07-16 10:41:42 +0200929 /* with FRACV :
930 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100931 * without FRACV
Patrick Delaunay61105032018-07-16 10:41:42 +0200932 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100933 */
934 if (fracr & RCC_PLLNFRACR_FRACLE) {
935 u32 fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK)
936 >> RCC_PLLNFRACR_FRACV_SHIFT;
Patrick Delaunay61105032018-07-16 10:41:42 +0200937 fvco = (ulong)lldiv((unsigned long long)refclk *
Patrick Delaunaya6151912018-03-12 10:46:15 +0100938 (((divn + 1) << 13) + fracv),
Patrick Delaunay61105032018-07-16 10:41:42 +0200939 ((unsigned long long)(divm + 1)) << 13);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100940 } else {
Patrick Delaunay61105032018-07-16 10:41:42 +0200941 fvco = (ulong)(refclk * (divn + 1) / (divm + 1));
Patrick Delaunaya6151912018-03-12 10:46:15 +0100942 }
Patrick Delaunay61105032018-07-16 10:41:42 +0200943
944 return fvco;
945}
946
947static ulong stm32mp1_read_pll_freq(struct stm32mp1_clk_priv *priv,
948 int pll_id, int div_id)
949{
950 const struct stm32mp1_clk_pll *pll = priv->data->pll;
951 int divy;
952 ulong dfout;
953 u32 cfgr2;
954
Patrick Delaunay61105032018-07-16 10:41:42 +0200955 if (div_id >= _DIV_NB)
956 return 0;
957
958 cfgr2 = readl(priv->base + pll[pll_id].pllxcfgr2);
959 divy = (cfgr2 >> RCC_PLLNCFGR2_SHIFT(div_id)) & RCC_PLLNCFGR2_DIVX_MASK;
960
Patrick Delaunay61105032018-07-16 10:41:42 +0200961 dfout = pll_get_fvco(priv, pll_id) / (divy + 1);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100962
963 return dfout;
964}
965
966static ulong stm32mp1_clk_get(struct stm32mp1_clk_priv *priv, int p)
967{
968 u32 reg;
969 ulong clock = 0;
970
971 switch (p) {
972 case _CK_MPU:
973 /* MPU sub system */
974 reg = readl(priv->base + RCC_MPCKSELR);
975 switch (reg & RCC_SELR_SRC_MASK) {
976 case RCC_MPCKSELR_HSI:
977 clock = stm32mp1_clk_get_fixed(priv, _HSI);
978 break;
979 case RCC_MPCKSELR_HSE:
980 clock = stm32mp1_clk_get_fixed(priv, _HSE);
981 break;
982 case RCC_MPCKSELR_PLL:
983 case RCC_MPCKSELR_PLL_MPUDIV:
984 clock = stm32mp1_read_pll_freq(priv, _PLL1, _DIV_P);
Lionel Debieve36911fc2020-04-24 15:47:57 +0200985 if ((reg & RCC_SELR_SRC_MASK) ==
986 RCC_MPCKSELR_PLL_MPUDIV) {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100987 reg = readl(priv->base + RCC_MPCKDIVR);
Lionel Debieve36911fc2020-04-24 15:47:57 +0200988 clock >>= stm32mp1_mpu_div[reg &
989 RCC_MPUDIV_MASK];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100990 }
991 break;
992 }
993 break;
994 /* AXI sub system */
995 case _ACLK:
996 case _HCLK2:
997 case _HCLK6:
998 case _PCLK4:
999 case _PCLK5:
1000 reg = readl(priv->base + RCC_ASSCKSELR);
1001 switch (reg & RCC_SELR_SRC_MASK) {
1002 case RCC_ASSCKSELR_HSI:
1003 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1004 break;
1005 case RCC_ASSCKSELR_HSE:
1006 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1007 break;
1008 case RCC_ASSCKSELR_PLL:
1009 clock = stm32mp1_read_pll_freq(priv, _PLL2, _DIV_P);
1010 break;
1011 }
1012
1013 /* System clock divider */
1014 reg = readl(priv->base + RCC_AXIDIVR);
1015 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
1016
1017 switch (p) {
1018 case _PCLK4:
1019 reg = readl(priv->base + RCC_APB4DIVR);
1020 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1021 break;
1022 case _PCLK5:
1023 reg = readl(priv->base + RCC_APB5DIVR);
1024 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1025 break;
1026 default:
1027 break;
1028 }
1029 break;
1030 /* MCU sub system */
1031 case _CK_MCU:
1032 case _PCLK1:
1033 case _PCLK2:
1034 case _PCLK3:
1035 reg = readl(priv->base + RCC_MSSCKSELR);
1036 switch (reg & RCC_SELR_SRC_MASK) {
1037 case RCC_MSSCKSELR_HSI:
1038 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1039 break;
1040 case RCC_MSSCKSELR_HSE:
1041 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1042 break;
1043 case RCC_MSSCKSELR_CSI:
1044 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1045 break;
1046 case RCC_MSSCKSELR_PLL:
1047 clock = stm32mp1_read_pll_freq(priv, _PLL3, _DIV_P);
1048 break;
1049 }
1050
1051 /* MCU clock divider */
1052 reg = readl(priv->base + RCC_MCUDIVR);
1053 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
1054
1055 switch (p) {
1056 case _PCLK1:
1057 reg = readl(priv->base + RCC_APB1DIVR);
1058 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1059 break;
1060 case _PCLK2:
1061 reg = readl(priv->base + RCC_APB2DIVR);
1062 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1063 break;
1064 case _PCLK3:
1065 reg = readl(priv->base + RCC_APB3DIVR);
1066 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1067 break;
1068 case _CK_MCU:
1069 default:
1070 break;
1071 }
1072 break;
1073 case _CK_PER:
1074 reg = readl(priv->base + RCC_CPERCKSELR);
1075 switch (reg & RCC_SELR_SRC_MASK) {
1076 case RCC_CPERCKSELR_HSI:
1077 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1078 break;
1079 case RCC_CPERCKSELR_HSE:
1080 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1081 break;
1082 case RCC_CPERCKSELR_CSI:
1083 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1084 break;
1085 }
1086 break;
1087 case _HSI:
1088 case _HSI_KER:
1089 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1090 break;
1091 case _CSI:
1092 case _CSI_KER:
1093 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1094 break;
1095 case _HSE:
1096 case _HSE_KER:
1097 case _HSE_KER_DIV2:
1098 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1099 if (p == _HSE_KER_DIV2)
1100 clock >>= 1;
1101 break;
1102 case _LSI:
1103 clock = stm32mp1_clk_get_fixed(priv, _LSI);
1104 break;
1105 case _LSE:
1106 clock = stm32mp1_clk_get_fixed(priv, _LSE);
1107 break;
1108 /* PLL */
1109 case _PLL1_P:
1110 case _PLL1_Q:
1111 case _PLL1_R:
1112 clock = stm32mp1_read_pll_freq(priv, _PLL1, p - _PLL1_P);
1113 break;
1114 case _PLL2_P:
1115 case _PLL2_Q:
1116 case _PLL2_R:
1117 clock = stm32mp1_read_pll_freq(priv, _PLL2, p - _PLL2_P);
1118 break;
1119 case _PLL3_P:
1120 case _PLL3_Q:
1121 case _PLL3_R:
1122 clock = stm32mp1_read_pll_freq(priv, _PLL3, p - _PLL3_P);
1123 break;
1124 case _PLL4_P:
1125 case _PLL4_Q:
1126 case _PLL4_R:
1127 clock = stm32mp1_read_pll_freq(priv, _PLL4, p - _PLL4_P);
1128 break;
1129 /* other */
1130 case _USB_PHY_48:
Patrick Delaunay86617dd2019-01-30 13:07:00 +01001131 clock = 48000000;
Patrick Delaunaya6151912018-03-12 10:46:15 +01001132 break;
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001133 case _DSI_PHY:
1134 {
1135 struct clk clk;
1136 struct udevice *dev = NULL;
Patrick Delaunaya6151912018-03-12 10:46:15 +01001137
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001138 if (!uclass_get_device_by_name(UCLASS_CLK, "ck_dsi_phy",
1139 &dev)) {
1140 if (clk_request(dev, &clk)) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001141 log_err("ck_dsi_phy request");
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001142 } else {
1143 clk.id = 0;
1144 clock = clk_get_rate(&clk);
1145 }
1146 }
1147 break;
1148 }
Patrick Delaunaya6151912018-03-12 10:46:15 +01001149 default:
1150 break;
1151 }
1152
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001153 log_debug("id=%d clock = %lx : %ld kHz\n", p, clock, clock / 1000);
Patrick Delaunaya6151912018-03-12 10:46:15 +01001154
1155 return clock;
1156}
1157
1158static int stm32mp1_clk_enable(struct clk *clk)
1159{
1160 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1161 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1162 int i = stm32mp1_clk_get_id(priv, clk->id);
1163
1164 if (i < 0)
1165 return i;
1166
1167 if (gate[i].set_clr)
1168 writel(BIT(gate[i].bit), priv->base + gate[i].offset);
1169 else
1170 setbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1171
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001172 dev_dbg(clk->dev, "%s: id clock %d has been enabled\n", __func__, (u32)clk->id);
Patrick Delaunaya6151912018-03-12 10:46:15 +01001173
1174 return 0;
1175}
1176
1177static int stm32mp1_clk_disable(struct clk *clk)
1178{
1179 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1180 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1181 int i = stm32mp1_clk_get_id(priv, clk->id);
1182
1183 if (i < 0)
1184 return i;
1185
1186 if (gate[i].set_clr)
1187 writel(BIT(gate[i].bit),
1188 priv->base + gate[i].offset
1189 + RCC_MP_ENCLRR_OFFSET);
1190 else
1191 clrbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1192
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001193 dev_dbg(clk->dev, "%s: id clock %d has been disabled\n", __func__, (u32)clk->id);
Patrick Delaunaya6151912018-03-12 10:46:15 +01001194
1195 return 0;
1196}
1197
1198static ulong stm32mp1_clk_get_rate(struct clk *clk)
1199{
1200 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1201 int p = stm32mp1_clk_get_parent(priv, clk->id);
1202 ulong rate;
1203
1204 if (p < 0)
1205 return 0;
1206
1207 rate = stm32mp1_clk_get(priv, p);
1208
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001209 dev_vdbg(clk->dev, "computed rate for id clock %d is %d (parent is %s)\n",
1210 (u32)clk->id, (u32)rate, stm32mp1_clk_parent_name[p]);
1211
Patrick Delaunaya6151912018-03-12 10:46:15 +01001212 return rate;
1213}
1214
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001215#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001216
1217bool stm32mp1_supports_opp(u32 opp_id, u32 cpu_type)
1218{
1219 unsigned int id;
1220
1221 switch (opp_id) {
1222 case 1:
1223 case 2:
1224 id = opp_id;
1225 break;
1226 default:
1227 id = 1; /* default value */
1228 break;
1229 }
1230
1231 switch (cpu_type) {
1232 case CPU_STM32MP157Fxx:
1233 case CPU_STM32MP157Dxx:
1234 case CPU_STM32MP153Fxx:
1235 case CPU_STM32MP153Dxx:
1236 case CPU_STM32MP151Fxx:
1237 case CPU_STM32MP151Dxx:
1238 return true;
1239 default:
1240 return id == 1;
1241 }
1242}
1243
Patrick Delaunay4e626422020-05-25 12:19:45 +02001244__weak void board_vddcore_init(u32 voltage_mv)
1245{
1246}
1247
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001248/*
1249 * gets OPP parameters (frequency in KHz and voltage in mV) from
1250 * an OPP table subnode. Platform HW support capabilities are also checked.
1251 * Returns 0 on success and a negative FDT error code on failure.
1252 */
1253static int stm32mp1_get_opp(u32 cpu_type, ofnode subnode,
1254 u32 *freq_khz, u32 *voltage_mv)
1255{
1256 u32 opp_hw;
1257 u64 read_freq_64;
1258 u32 read_voltage_32;
1259
1260 *freq_khz = 0;
1261 *voltage_mv = 0;
1262
1263 opp_hw = ofnode_read_u32_default(subnode, "opp-supported-hw", 0);
1264 if (opp_hw)
1265 if (!stm32mp1_supports_opp(opp_hw, cpu_type))
1266 return -FDT_ERR_BADVALUE;
1267
1268 read_freq_64 = ofnode_read_u64_default(subnode, "opp-hz", 0) /
1269 1000ULL;
1270 read_voltage_32 = ofnode_read_u32_default(subnode, "opp-microvolt", 0) /
1271 1000U;
1272
1273 if (!read_voltage_32 || !read_freq_64)
1274 return -FDT_ERR_NOTFOUND;
1275
1276 /* Frequency value expressed in KHz must fit on 32 bits */
1277 if (read_freq_64 > U32_MAX)
1278 return -FDT_ERR_BADVALUE;
1279
1280 /* Millivolt value must fit on 16 bits */
1281 if (read_voltage_32 > U16_MAX)
1282 return -FDT_ERR_BADVALUE;
1283
1284 *freq_khz = (u32)read_freq_64;
1285 *voltage_mv = read_voltage_32;
1286
1287 return 0;
1288}
1289
1290/*
1291 * parses OPP table in DT and finds the parameters for the
1292 * highest frequency supported by the HW platform.
1293 * Returns 0 on success and a negative FDT error code on failure.
1294 */
1295int stm32mp1_get_max_opp_freq(struct stm32mp1_clk_priv *priv, u64 *freq_hz)
1296{
1297 ofnode node, subnode;
1298 int ret;
1299 u32 freq = 0U, voltage = 0U;
1300 u32 cpu_type = get_cpu_type();
1301
1302 node = ofnode_by_compatible(ofnode_null(), "operating-points-v2");
1303 if (!ofnode_valid(node))
1304 return -FDT_ERR_NOTFOUND;
1305
1306 ofnode_for_each_subnode(subnode, node) {
1307 unsigned int read_freq;
1308 unsigned int read_voltage;
1309
1310 ret = stm32mp1_get_opp(cpu_type, subnode,
1311 &read_freq, &read_voltage);
1312 if (ret)
1313 continue;
1314
1315 if (read_freq > freq) {
1316 freq = read_freq;
1317 voltage = read_voltage;
1318 }
1319 }
1320
1321 if (!freq || !voltage)
1322 return -FDT_ERR_NOTFOUND;
1323
1324 *freq_hz = (u64)1000U * freq;
Patrick Delaunay4e626422020-05-25 12:19:45 +02001325 board_vddcore_init(voltage);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001326
1327 return 0;
1328}
1329
1330static int stm32mp1_pll1_opp(struct stm32mp1_clk_priv *priv, int clksrc,
1331 u32 *pllcfg, u32 *fracv)
1332{
1333 u32 post_divm;
1334 u32 input_freq;
1335 u64 output_freq;
1336 u64 freq;
1337 u64 vco;
1338 u32 divm, divn, divp, frac;
1339 int i, ret;
1340 u32 diff;
1341 u32 best_diff = U32_MAX;
1342
1343 /* PLL1 is 1600 */
1344 const u32 DIVN_MAX = stm32mp1_pll[PLL_1600].divn_max;
1345 const u32 POST_DIVM_MIN = stm32mp1_pll[PLL_1600].refclk_min * 1000000U;
1346 const u32 POST_DIVM_MAX = stm32mp1_pll[PLL_1600].refclk_max * 1000000U;
1347
1348 ret = stm32mp1_get_max_opp_freq(priv, &output_freq);
1349 if (ret) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001350 log_debug("PLL1 OPP configuration not found (%d).\n", ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001351 return ret;
1352 }
1353
1354 switch (clksrc) {
1355 case CLK_PLL12_HSI:
1356 input_freq = stm32mp1_clk_get_fixed(priv, _HSI);
1357 break;
1358 case CLK_PLL12_HSE:
1359 input_freq = stm32mp1_clk_get_fixed(priv, _HSE);
1360 break;
1361 default:
1362 return -EINTR;
1363 }
1364
1365 /* Following parameters have always the same value */
1366 pllcfg[PLLCFG_Q] = 0;
1367 pllcfg[PLLCFG_R] = 0;
1368 pllcfg[PLLCFG_O] = PQR(1, 0, 0);
1369
1370 for (divm = DIVM_MAX; divm >= DIVM_MIN; divm--) {
1371 post_divm = (u32)(input_freq / (divm + 1));
1372 if (post_divm < POST_DIVM_MIN || post_divm > POST_DIVM_MAX)
1373 continue;
1374
1375 for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
1376 freq = output_freq * (divm + 1) * (divp + 1);
1377 divn = (u32)((freq / input_freq) - 1);
1378 if (divn < DIVN_MIN || divn > DIVN_MAX)
1379 continue;
1380
1381 frac = (u32)(((freq * FRAC_MAX) / input_freq) -
1382 ((divn + 1) * FRAC_MAX));
1383 /* 2 loops to refine the fractional part */
1384 for (i = 2; i != 0; i--) {
1385 if (frac > FRAC_MAX)
1386 break;
1387
1388 vco = (post_divm * (divn + 1)) +
1389 ((post_divm * (u64)frac) /
1390 FRAC_MAX);
1391 if (vco < (PLL1600_VCO_MIN / 2) ||
1392 vco > (PLL1600_VCO_MAX / 2)) {
1393 frac++;
1394 continue;
1395 }
1396 freq = vco / (divp + 1);
1397 if (output_freq < freq)
1398 diff = (u32)(freq - output_freq);
1399 else
1400 diff = (u32)(output_freq - freq);
1401 if (diff < best_diff) {
1402 pllcfg[PLLCFG_M] = divm;
1403 pllcfg[PLLCFG_N] = divn;
1404 pllcfg[PLLCFG_P] = divp;
1405 *fracv = frac;
1406
1407 if (diff == 0)
1408 return 0;
1409
1410 best_diff = diff;
1411 }
1412 frac++;
1413 }
1414 }
1415 }
1416
1417 if (best_diff == U32_MAX)
1418 return -1;
1419
1420 return 0;
1421}
1422
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001423static void stm32mp1_ls_osc_set(int enable, fdt_addr_t rcc, u32 offset,
1424 u32 mask_on)
1425{
1426 u32 address = rcc + offset;
1427
1428 if (enable)
1429 setbits_le32(address, mask_on);
1430 else
1431 clrbits_le32(address, mask_on);
1432}
1433
1434static void stm32mp1_hs_ocs_set(int enable, fdt_addr_t rcc, u32 mask_on)
1435{
Patrick Delaunay63201282019-01-30 13:07:02 +01001436 writel(mask_on, rcc + (enable ? RCC_OCENSETR : RCC_OCENCLRR));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001437}
1438
1439static int stm32mp1_osc_wait(int enable, fdt_addr_t rcc, u32 offset,
1440 u32 mask_rdy)
1441{
1442 u32 mask_test = 0;
1443 u32 address = rcc + offset;
1444 u32 val;
1445 int ret;
1446
1447 if (enable)
1448 mask_test = mask_rdy;
1449
1450 ret = readl_poll_timeout(address, val,
1451 (val & mask_rdy) == mask_test,
1452 TIMEOUT_1S);
1453
1454 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001455 log_err("OSC %x @ %x timeout for enable=%d : 0x%x\n",
1456 mask_rdy, address, enable, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001457
1458 return ret;
1459}
1460
Patrick Delaunayd2194152018-07-16 10:41:46 +02001461static void stm32mp1_lse_enable(fdt_addr_t rcc, int bypass, int digbyp,
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001462 u32 lsedrv)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001463{
1464 u32 value;
1465
Patrick Delaunayd2194152018-07-16 10:41:46 +02001466 if (digbyp)
1467 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_DIGBYP);
1468
1469 if (bypass || digbyp)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001470 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_LSEBYP);
1471
1472 /*
1473 * warning: not recommended to switch directly from "high drive"
1474 * to "medium low drive", and vice-versa.
1475 */
1476 value = (readl(rcc + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK)
1477 >> RCC_BDCR_LSEDRV_SHIFT;
1478
1479 while (value != lsedrv) {
1480 if (value > lsedrv)
1481 value--;
1482 else
1483 value++;
1484
1485 clrsetbits_le32(rcc + RCC_BDCR,
1486 RCC_BDCR_LSEDRV_MASK,
1487 value << RCC_BDCR_LSEDRV_SHIFT);
1488 }
1489
1490 stm32mp1_ls_osc_set(1, rcc, RCC_BDCR, RCC_BDCR_LSEON);
1491}
1492
1493static void stm32mp1_lse_wait(fdt_addr_t rcc)
1494{
1495 stm32mp1_osc_wait(1, rcc, RCC_BDCR, RCC_BDCR_LSERDY);
1496}
1497
1498static void stm32mp1_lsi_set(fdt_addr_t rcc, int enable)
1499{
1500 stm32mp1_ls_osc_set(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSION);
1501 stm32mp1_osc_wait(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSIRDY);
1502}
1503
Patrick Delaunayd2194152018-07-16 10:41:46 +02001504static void stm32mp1_hse_enable(fdt_addr_t rcc, int bypass, int digbyp, int css)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001505{
Patrick Delaunayd2194152018-07-16 10:41:46 +02001506 if (digbyp)
Patrick Delaunay63201282019-01-30 13:07:02 +01001507 writel(RCC_OCENR_DIGBYP, rcc + RCC_OCENSETR);
Patrick Delaunayd2194152018-07-16 10:41:46 +02001508 if (bypass || digbyp)
Patrick Delaunay63201282019-01-30 13:07:02 +01001509 writel(RCC_OCENR_HSEBYP, rcc + RCC_OCENSETR);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001510
1511 stm32mp1_hs_ocs_set(1, rcc, RCC_OCENR_HSEON);
1512 stm32mp1_osc_wait(1, rcc, RCC_OCRDYR, RCC_OCRDYR_HSERDY);
1513
1514 if (css)
Patrick Delaunay63201282019-01-30 13:07:02 +01001515 writel(RCC_OCENR_HSECSSON, rcc + RCC_OCENSETR);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001516}
1517
1518static void stm32mp1_csi_set(fdt_addr_t rcc, int enable)
1519{
Patrick Delaunay63201282019-01-30 13:07:02 +01001520 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_CSION);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001521 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_CSIRDY);
1522}
1523
1524static void stm32mp1_hsi_set(fdt_addr_t rcc, int enable)
1525{
1526 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_HSION);
1527 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_HSIRDY);
1528}
1529
1530static int stm32mp1_set_hsidiv(fdt_addr_t rcc, u8 hsidiv)
1531{
1532 u32 address = rcc + RCC_OCRDYR;
1533 u32 val;
1534 int ret;
1535
1536 clrsetbits_le32(rcc + RCC_HSICFGR,
1537 RCC_HSICFGR_HSIDIV_MASK,
1538 RCC_HSICFGR_HSIDIV_MASK & hsidiv);
1539
1540 ret = readl_poll_timeout(address, val,
1541 val & RCC_OCRDYR_HSIDIVRDY,
1542 TIMEOUT_200MS);
1543 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001544 log_err("HSIDIV failed @ 0x%x: 0x%x\n",
1545 address, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001546
1547 return ret;
1548}
1549
1550static int stm32mp1_hsidiv(fdt_addr_t rcc, ulong hsifreq)
1551{
1552 u8 hsidiv;
1553 u32 hsidivfreq = MAX_HSI_HZ;
1554
1555 for (hsidiv = 0; hsidiv < 4; hsidiv++,
1556 hsidivfreq = hsidivfreq / 2)
1557 if (hsidivfreq == hsifreq)
1558 break;
1559
1560 if (hsidiv == 4) {
Etienne Carriere08db5d52021-02-24 11:19:42 +01001561 log_err("hsi frequency invalid");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001562 return -1;
1563 }
1564
1565 if (hsidiv > 0)
1566 return stm32mp1_set_hsidiv(rcc, hsidiv);
1567
1568 return 0;
1569}
1570
1571static void pll_start(struct stm32mp1_clk_priv *priv, int pll_id)
1572{
1573 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1574
Patrick Delaunaybbd108a2019-01-30 13:07:06 +01001575 clrsetbits_le32(priv->base + pll[pll_id].pllxcr,
1576 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1577 RCC_PLLNCR_DIVREN,
1578 RCC_PLLNCR_PLLON);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001579}
1580
1581static int pll_output(struct stm32mp1_clk_priv *priv, int pll_id, int output)
1582{
1583 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1584 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1585 u32 val;
1586 int ret;
1587
1588 ret = readl_poll_timeout(pllxcr, val, val & RCC_PLLNCR_PLLRDY,
1589 TIMEOUT_200MS);
1590
1591 if (ret) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001592 log_err("PLL%d start failed @ 0x%x: 0x%x\n",
1593 pll_id, pllxcr, readl(pllxcr));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001594 return ret;
1595 }
1596
1597 /* start the requested output */
1598 setbits_le32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1599
1600 return 0;
1601}
1602
1603static int pll_stop(struct stm32mp1_clk_priv *priv, int pll_id)
1604{
1605 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1606 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1607 u32 val;
1608
1609 /* stop all output */
1610 clrbits_le32(pllxcr,
1611 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
1612
1613 /* stop PLL */
1614 clrbits_le32(pllxcr, RCC_PLLNCR_PLLON);
1615
1616 /* wait PLL stopped */
1617 return readl_poll_timeout(pllxcr, val, (val & RCC_PLLNCR_PLLRDY) == 0,
1618 TIMEOUT_200MS);
1619}
1620
1621static void pll_config_output(struct stm32mp1_clk_priv *priv,
1622 int pll_id, u32 *pllcfg)
1623{
1624 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1625 fdt_addr_t rcc = priv->base;
1626 u32 value;
1627
1628 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT)
1629 & RCC_PLLNCFGR2_DIVP_MASK;
1630 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT)
1631 & RCC_PLLNCFGR2_DIVQ_MASK;
1632 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT)
1633 & RCC_PLLNCFGR2_DIVR_MASK;
1634 writel(value, rcc + pll[pll_id].pllxcfgr2);
1635}
1636
1637static int pll_config(struct stm32mp1_clk_priv *priv, int pll_id,
1638 u32 *pllcfg, u32 fracv)
1639{
1640 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1641 fdt_addr_t rcc = priv->base;
1642 enum stm32mp1_plltype type = pll[pll_id].plltype;
1643 int src;
1644 ulong refclk;
1645 u8 ifrge = 0;
1646 u32 value;
1647
1648 src = readl(priv->base + pll[pll_id].rckxselr) & RCC_SELR_SRC_MASK;
1649
1650 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]) /
1651 (pllcfg[PLLCFG_M] + 1);
1652
1653 if (refclk < (stm32mp1_pll[type].refclk_min * 1000000) ||
1654 refclk > (stm32mp1_pll[type].refclk_max * 1000000)) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001655 log_err("invalid refclk = %x\n", (u32)refclk);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001656 return -EINVAL;
1657 }
1658 if (type == PLL_800 && refclk >= 8000000)
1659 ifrge = 1;
1660
1661 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT)
1662 & RCC_PLLNCFGR1_DIVN_MASK;
1663 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT)
1664 & RCC_PLLNCFGR1_DIVM_MASK;
1665 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT)
1666 & RCC_PLLNCFGR1_IFRGE_MASK;
1667 writel(value, rcc + pll[pll_id].pllxcfgr1);
1668
1669 /* fractional configuration: load sigma-delta modulator (SDM) */
1670
1671 /* Write into FRACV the new fractional value , and FRACLE to 0 */
1672 writel(fracv << RCC_PLLNFRACR_FRACV_SHIFT,
1673 rcc + pll[pll_id].pllxfracr);
1674
1675 /* Write FRACLE to 1 : FRACV value is loaded into the SDM */
1676 setbits_le32(rcc + pll[pll_id].pllxfracr,
1677 RCC_PLLNFRACR_FRACLE);
1678
1679 pll_config_output(priv, pll_id, pllcfg);
1680
1681 return 0;
1682}
1683
1684static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
1685{
1686 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1687 u32 pllxcsg;
1688
1689 pllxcsg = ((csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1690 RCC_PLLNCSGR_MOD_PER_MASK) |
1691 ((csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1692 RCC_PLLNCSGR_INC_STEP_MASK) |
1693 ((csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1694 RCC_PLLNCSGR_SSCG_MODE_MASK);
1695
1696 writel(pllxcsg, priv->base + pll[pll_id].pllxcsgr);
Patrick Delaunaybbd108a2019-01-30 13:07:06 +01001697
1698 setbits_le32(priv->base + pll[pll_id].pllxcr, RCC_PLLNCR_SSCG_CTRL);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001699}
1700
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001701static __maybe_unused int pll_set_rate(struct udevice *dev,
1702 int pll_id,
1703 int div_id,
1704 unsigned long clk_rate)
1705{
1706 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1707 unsigned int pllcfg[PLLCFG_NB];
1708 ofnode plloff;
1709 char name[12];
1710 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1711 enum stm32mp1_plltype type = pll[pll_id].plltype;
1712 int divm, divn, divy;
1713 int ret;
1714 ulong fck_ref;
1715 u32 fracv;
1716 u64 value;
1717
1718 if (div_id > _DIV_NB)
1719 return -EINVAL;
1720
1721 sprintf(name, "st,pll@%d", pll_id);
1722 plloff = dev_read_subnode(dev, name);
1723 if (!ofnode_valid(plloff))
1724 return -FDT_ERR_NOTFOUND;
1725
1726 ret = ofnode_read_u32_array(plloff, "cfg",
1727 pllcfg, PLLCFG_NB);
1728 if (ret < 0)
1729 return -FDT_ERR_NOTFOUND;
1730
1731 fck_ref = pll_get_fref_ck(priv, pll_id);
1732
1733 divm = pllcfg[PLLCFG_M];
1734 /* select output divider = 0: for _DIV_P, 1:_DIV_Q 2:_DIV_R */
1735 divy = pllcfg[PLLCFG_P + div_id];
1736
1737 /* For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
1738 * So same final result than PLL2 et 4
1739 * with FRACV
1740 * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
1741 * / (DIVy + 1) * (DIVM + 1)
1742 * value = (DIVN + 1) * 2^13 + FRACV / 2^13
1743 * = Fck_pll_y (DIVy + 1) * (DIVM + 1) * 2^13 / Fck_ref
1744 */
1745 value = ((u64)clk_rate * (divy + 1) * (divm + 1)) << 13;
1746 value = lldiv(value, fck_ref);
1747
1748 divn = (value >> 13) - 1;
1749 if (divn < DIVN_MIN ||
1750 divn > stm32mp1_pll[type].divn_max) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001751 dev_err(dev, "divn invalid = %d", divn);
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001752 return -EINVAL;
1753 }
1754 fracv = value - ((divn + 1) << 13);
1755 pllcfg[PLLCFG_N] = divn;
1756
1757 /* reconfigure PLL */
1758 pll_stop(priv, pll_id);
1759 pll_config(priv, pll_id, pllcfg, fracv);
1760 pll_start(priv, pll_id);
1761 pll_output(priv, pll_id, pllcfg[PLLCFG_O]);
1762
1763 return 0;
1764}
1765
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001766static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
1767{
1768 u32 address = priv->base + (clksrc >> 4);
1769 u32 val;
1770 int ret;
1771
1772 clrsetbits_le32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK);
1773 ret = readl_poll_timeout(address, val, val & RCC_SELR_SRCRDY,
1774 TIMEOUT_200MS);
1775 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001776 log_err("CLKSRC %x start failed @ 0x%x: 0x%x\n",
1777 clksrc, address, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001778
1779 return ret;
1780}
1781
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001782static void stgen_config(struct stm32mp1_clk_priv *priv)
1783{
1784 int p;
1785 u32 stgenc, cntfid0;
1786 ulong rate;
1787
Patrick Delaunaydfda7d42019-07-05 17:20:11 +02001788 stgenc = STM32_STGEN_BASE;
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001789 cntfid0 = readl(stgenc + STGENC_CNTFID0);
1790 p = stm32mp1_clk_get_parent(priv, STGEN_K);
1791 rate = stm32mp1_clk_get(priv, p);
1792
1793 if (cntfid0 != rate) {
Patrick Delaunayf3a23c22019-01-30 13:07:03 +01001794 u64 counter;
1795
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001796 log_debug("System Generic Counter (STGEN) update\n");
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001797 clrbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
Patrick Delaunayf3a23c22019-01-30 13:07:03 +01001798 counter = (u64)readl(stgenc + STGENC_CNTCVL);
1799 counter |= ((u64)(readl(stgenc + STGENC_CNTCVU))) << 32;
1800 counter = lldiv(counter * (u64)rate, cntfid0);
1801 writel((u32)counter, stgenc + STGENC_CNTCVL);
1802 writel((u32)(counter >> 32), stgenc + STGENC_CNTCVU);
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001803 writel(rate, stgenc + STGENC_CNTFID0);
1804 setbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1805
1806 __asm__ volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (rate));
1807
1808 /* need to update gd->arch.timer_rate_hz with new frequency */
1809 timer_init();
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001810 }
1811}
1812
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001813static int set_clkdiv(unsigned int clkdiv, u32 address)
1814{
1815 u32 val;
1816 int ret;
1817
1818 clrsetbits_le32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK);
1819 ret = readl_poll_timeout(address, val, val & RCC_DIVR_DIVRDY,
1820 TIMEOUT_200MS);
1821 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001822 log_err("CLKDIV %x start failed @ 0x%x: 0x%x\n",
1823 clkdiv, address, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001824
1825 return ret;
1826}
1827
1828static void stm32mp1_mco_csg(struct stm32mp1_clk_priv *priv,
1829 u32 clksrc, u32 clkdiv)
1830{
1831 u32 address = priv->base + (clksrc >> 4);
1832
1833 /*
1834 * binding clksrc : bit15-4 offset
1835 * bit3: disable
1836 * bit2-0: MCOSEL[2:0]
1837 */
1838 if (clksrc & 0x8) {
1839 clrbits_le32(address, RCC_MCOCFG_MCOON);
1840 } else {
1841 clrsetbits_le32(address,
1842 RCC_MCOCFG_MCOSRC_MASK,
1843 clksrc & RCC_MCOCFG_MCOSRC_MASK);
1844 clrsetbits_le32(address,
1845 RCC_MCOCFG_MCODIV_MASK,
1846 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
1847 setbits_le32(address, RCC_MCOCFG_MCOON);
1848 }
1849}
1850
1851static void set_rtcsrc(struct stm32mp1_clk_priv *priv,
1852 unsigned int clksrc,
1853 int lse_css)
1854{
1855 u32 address = priv->base + RCC_BDCR;
1856
1857 if (readl(address) & RCC_BDCR_RTCCKEN)
1858 goto skip_rtc;
1859
1860 if (clksrc == CLK_RTC_DISABLED)
1861 goto skip_rtc;
1862
1863 clrsetbits_le32(address,
1864 RCC_BDCR_RTCSRC_MASK,
1865 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1866
1867 setbits_le32(address, RCC_BDCR_RTCCKEN);
1868
1869skip_rtc:
1870 if (lse_css)
1871 setbits_le32(address, RCC_BDCR_LSECSSON);
1872}
1873
1874static void pkcs_config(struct stm32mp1_clk_priv *priv, u32 pkcs)
1875{
1876 u32 address = priv->base + ((pkcs >> 4) & 0xFFF);
1877 u32 value = pkcs & 0xF;
1878 u32 mask = 0xF;
1879
1880 if (pkcs & BIT(31)) {
1881 mask <<= 4;
1882 value <<= 4;
1883 }
1884 clrsetbits_le32(address, mask, value);
1885}
1886
1887static int stm32mp1_clktree(struct udevice *dev)
1888{
1889 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1890 fdt_addr_t rcc = priv->base;
1891 unsigned int clksrc[CLKSRC_NB];
1892 unsigned int clkdiv[CLKDIV_NB];
1893 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001894 unsigned int pllfracv[_PLL_NB];
1895 unsigned int pllcsg[_PLL_NB][PLLCSG_NB];
1896 bool pllcfg_valid[_PLL_NB];
1897 bool pllcsg_set[_PLL_NB];
1898 int ret;
1899 int i, len;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001900 int lse_css = 0;
1901 const u32 *pkcs_cell;
1902
1903 /* check mandatory field */
1904 ret = dev_read_u32_array(dev, "st,clksrc", clksrc, CLKSRC_NB);
1905 if (ret < 0) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001906 dev_dbg(dev, "field st,clksrc invalid: error %d\n", ret);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001907 return -FDT_ERR_NOTFOUND;
1908 }
1909
1910 ret = dev_read_u32_array(dev, "st,clkdiv", clkdiv, CLKDIV_NB);
1911 if (ret < 0) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001912 dev_dbg(dev, "field st,clkdiv invalid: error %d\n", ret);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001913 return -FDT_ERR_NOTFOUND;
1914 }
1915
1916 /* check mandatory field in each pll */
1917 for (i = 0; i < _PLL_NB; i++) {
1918 char name[12];
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001919 ofnode node;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001920
1921 sprintf(name, "st,pll@%d", i);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001922 node = dev_read_subnode(dev, name);
1923 pllcfg_valid[i] = ofnode_valid(node);
1924 pllcsg_set[i] = false;
1925 if (pllcfg_valid[i]) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001926 dev_dbg(dev, "DT for PLL %d @ %s\n", i, name);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001927 ret = ofnode_read_u32_array(node, "cfg",
1928 pllcfg[i], PLLCFG_NB);
1929 if (ret < 0) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001930 dev_dbg(dev, "field cfg invalid: error %d\n", ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001931 return -FDT_ERR_NOTFOUND;
1932 }
1933 pllfracv[i] = ofnode_read_u32_default(node, "frac", 0);
1934
1935 ret = ofnode_read_u32_array(node, "csg", pllcsg[i],
1936 PLLCSG_NB);
1937 if (!ret) {
1938 pllcsg_set[i] = true;
1939 } else if (ret != -FDT_ERR_NOTFOUND) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001940 dev_dbg(dev, "invalid csg node for pll@%d res=%d\n",
1941 i, ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001942 return ret;
1943 }
1944 } else if (i == _PLL1) {
1945 /* use OPP for PLL1 for A7 CPU */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001946 dev_dbg(dev, "DT for PLL %d with OPP\n", i);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001947 ret = stm32mp1_pll1_opp(priv,
1948 clksrc[CLKSRC_PLL12],
1949 pllcfg[i],
1950 &pllfracv[i]);
1951 if (ret) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001952 dev_dbg(dev, "PLL %d with OPP error = %d\n", i, ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001953 return ret;
1954 }
1955 pllcfg_valid[i] = true;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001956 }
1957 }
1958
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001959 dev_dbg(dev, "configuration MCO\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001960 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1961 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1962
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001963 dev_dbg(dev, "switch ON osillator\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001964 /*
1965 * switch ON oscillator found in device-tree,
1966 * HSI already ON after bootrom
1967 */
Etienne Carriere08db5d52021-02-24 11:19:42 +01001968 if (clk_valid(&priv->osc_clk[_LSI]))
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001969 stm32mp1_lsi_set(rcc, 1);
1970
Etienne Carriere08db5d52021-02-24 11:19:42 +01001971 if (clk_valid(&priv->osc_clk[_LSE])) {
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001972 int bypass, digbyp;
1973 u32 lsedrv;
Etienne Carriere08db5d52021-02-24 11:19:42 +01001974 struct udevice *dev = priv->osc_clk[_LSE].dev;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001975
1976 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunayd2194152018-07-16 10:41:46 +02001977 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001978 lse_css = dev_read_bool(dev, "st,css");
1979 lsedrv = dev_read_u32_default(dev, "st,drive",
1980 LSEDRV_MEDIUM_HIGH);
1981
Patrick Delaunayd2194152018-07-16 10:41:46 +02001982 stm32mp1_lse_enable(rcc, bypass, digbyp, lsedrv);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001983 }
1984
Etienne Carriere08db5d52021-02-24 11:19:42 +01001985 if (clk_valid(&priv->osc_clk[_HSE])) {
Patrick Delaunayd2194152018-07-16 10:41:46 +02001986 int bypass, digbyp, css;
Etienne Carriere08db5d52021-02-24 11:19:42 +01001987 struct udevice *dev = priv->osc_clk[_HSE].dev;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001988
1989 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunayd2194152018-07-16 10:41:46 +02001990 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001991 css = dev_read_bool(dev, "st,css");
1992
Patrick Delaunayd2194152018-07-16 10:41:46 +02001993 stm32mp1_hse_enable(rcc, bypass, digbyp, css);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001994 }
1995 /* CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1996 * => switch on CSI even if node is not present in device tree
1997 */
1998 stm32mp1_csi_set(rcc, 1);
1999
2000 /* come back to HSI */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002001 dev_dbg(dev, "come back to HSI\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002002 set_clksrc(priv, CLK_MPU_HSI);
2003 set_clksrc(priv, CLK_AXI_HSI);
2004 set_clksrc(priv, CLK_MCU_HSI);
2005
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002006 dev_dbg(dev, "pll stop\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002007 for (i = 0; i < _PLL_NB; i++)
2008 pll_stop(priv, i);
2009
2010 /* configure HSIDIV */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002011 dev_dbg(dev, "configure HSIDIV\n");
Etienne Carriere08db5d52021-02-24 11:19:42 +01002012 if (clk_valid(&priv->osc_clk[_HSI])) {
2013 stm32mp1_hsidiv(rcc, clk_get_rate(&priv->osc_clk[_HSI]));
Patrick Delaunay938e0e32018-03-20 11:41:25 +01002014 stgen_config(priv);
2015 }
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002016
2017 /* select DIV */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002018 dev_dbg(dev, "select DIV\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002019 /* no ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
2020 writel(clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK, rcc + RCC_MPCKDIVR);
2021 set_clkdiv(clkdiv[CLKDIV_AXI], rcc + RCC_AXIDIVR);
2022 set_clkdiv(clkdiv[CLKDIV_APB4], rcc + RCC_APB4DIVR);
2023 set_clkdiv(clkdiv[CLKDIV_APB5], rcc + RCC_APB5DIVR);
2024 set_clkdiv(clkdiv[CLKDIV_MCU], rcc + RCC_MCUDIVR);
2025 set_clkdiv(clkdiv[CLKDIV_APB1], rcc + RCC_APB1DIVR);
2026 set_clkdiv(clkdiv[CLKDIV_APB2], rcc + RCC_APB2DIVR);
2027 set_clkdiv(clkdiv[CLKDIV_APB3], rcc + RCC_APB3DIVR);
2028
2029 /* no ready bit for RTC */
2030 writel(clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK, rcc + RCC_RTCDIVR);
2031
2032 /* configure PLLs source */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002033 dev_dbg(dev, "configure PLLs source\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002034 set_clksrc(priv, clksrc[CLKSRC_PLL12]);
2035 set_clksrc(priv, clksrc[CLKSRC_PLL3]);
2036 set_clksrc(priv, clksrc[CLKSRC_PLL4]);
2037
2038 /* configure and start PLLs */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002039 dev_dbg(dev, "configure PLLs\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002040 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002041 if (!pllcfg_valid[i])
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002042 continue;
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002043 dev_dbg(dev, "configure PLL %d\n", i);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002044 pll_config(priv, i, pllcfg[i], pllfracv[i]);
2045 if (pllcsg_set[i])
2046 pll_csg(priv, i, pllcsg[i]);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002047 pll_start(priv, i);
2048 }
2049
2050 /* wait and start PLLs ouptut when ready */
2051 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002052 if (!pllcfg_valid[i])
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002053 continue;
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002054 dev_dbg(dev, "output PLL %d\n", i);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002055 pll_output(priv, i, pllcfg[i][PLLCFG_O]);
2056 }
2057
2058 /* wait LSE ready before to use it */
Etienne Carriere08db5d52021-02-24 11:19:42 +01002059 if (clk_valid(&priv->osc_clk[_LSE]))
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002060 stm32mp1_lse_wait(rcc);
2061
2062 /* configure with expected clock source */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002063 dev_dbg(dev, "CLKSRC\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002064 set_clksrc(priv, clksrc[CLKSRC_MPU]);
2065 set_clksrc(priv, clksrc[CLKSRC_AXI]);
2066 set_clksrc(priv, clksrc[CLKSRC_MCU]);
2067 set_rtcsrc(priv, clksrc[CLKSRC_RTC], lse_css);
2068
2069 /* configure PKCK */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002070 dev_dbg(dev, "PKCK\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002071 pkcs_cell = dev_read_prop(dev, "st,pkcs", &len);
2072 if (pkcs_cell) {
2073 bool ckper_disabled = false;
2074
2075 for (i = 0; i < len / sizeof(u32); i++) {
2076 u32 pkcs = (u32)fdt32_to_cpu(pkcs_cell[i]);
2077
2078 if (pkcs == CLK_CKPER_DISABLED) {
2079 ckper_disabled = true;
2080 continue;
2081 }
2082 pkcs_config(priv, pkcs);
2083 }
2084 /* CKPER is source for some peripheral clock
2085 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2086 * only if previous clock is still ON
2087 * => deactivated CKPER only after switching clock
2088 */
2089 if (ckper_disabled)
2090 pkcs_config(priv, CLK_CKPER_DISABLED);
2091 }
2092
Patrick Delaunay938e0e32018-03-20 11:41:25 +01002093 /* STGEN clock source can change with CLK_STGEN_XXX */
2094 stgen_config(priv);
2095
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002096 dev_dbg(dev, "oscillator off\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002097 /* switch OFF HSI if not found in device-tree */
Etienne Carriere08db5d52021-02-24 11:19:42 +01002098 if (!clk_valid(&priv->osc_clk[_HSI]))
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002099 stm32mp1_hsi_set(rcc, 0);
2100
2101 /* Software Self-Refresh mode (SSR) during DDR initilialization */
2102 clrsetbits_le32(priv->base + RCC_DDRITFCR,
2103 RCC_DDRITFCR_DDRCKMOD_MASK,
2104 RCC_DDRITFCR_DDRCKMOD_SSR <<
2105 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2106
2107 return 0;
2108}
2109#endif /* STM32MP1_CLOCK_TREE_INIT */
2110
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002111static int pll_set_output_rate(struct udevice *dev,
2112 int pll_id,
2113 int div_id,
2114 unsigned long clk_rate)
2115{
2116 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2117 const struct stm32mp1_clk_pll *pll = priv->data->pll;
2118 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
2119 int div;
2120 ulong fvco;
2121
2122 if (div_id > _DIV_NB)
2123 return -EINVAL;
2124
2125 fvco = pll_get_fvco(priv, pll_id);
2126
2127 if (fvco <= clk_rate)
2128 div = 1;
2129 else
2130 div = DIV_ROUND_UP(fvco, clk_rate);
2131
2132 if (div > 128)
2133 div = 128;
2134
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002135 /* stop the requested output */
2136 clrbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2137 /* change divider */
2138 clrsetbits_le32(priv->base + pll[pll_id].pllxcfgr2,
2139 RCC_PLLNCFGR2_DIVX_MASK << RCC_PLLNCFGR2_SHIFT(div_id),
2140 (div - 1) << RCC_PLLNCFGR2_SHIFT(div_id));
2141 /* start the requested output */
2142 setbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2143
2144 return 0;
2145}
2146
2147static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
2148{
2149 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
2150 int p;
2151
2152 switch (clk->id) {
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02002153#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2154 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2155 case DDRPHYC:
2156 break;
2157#endif
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002158 case LTDC_PX:
2159 case DSI_PX:
2160 break;
2161 default:
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002162 dev_err(clk->dev, "Set of clk %ld not supported", clk->id);
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002163 return -EINVAL;
2164 }
2165
2166 p = stm32mp1_clk_get_parent(priv, clk->id);
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002167 dev_vdbg(clk->dev, "parent = %d:%s\n", p, stm32mp1_clk_parent_name[p]);
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002168 if (p < 0)
2169 return -EINVAL;
2170
2171 switch (p) {
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02002172#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2173 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2174 case _PLL2_R: /* DDRPHYC */
2175 {
2176 /* only for change DDR clock in interactive mode */
2177 ulong result;
2178
2179 set_clksrc(priv, CLK_AXI_HSI);
2180 result = pll_set_rate(clk->dev, _PLL2, _DIV_R, clk_rate);
2181 set_clksrc(priv, CLK_AXI_PLL2P);
2182 return result;
2183 }
2184#endif
Patrick Delaunay7879a7d2019-07-30 19:16:54 +02002185
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002186 case _PLL4_Q:
2187 /* for LTDC_PX and DSI_PX case */
2188 return pll_set_output_rate(clk->dev, _PLL4, _DIV_Q, clk_rate);
2189 }
2190
2191 return -EINVAL;
2192}
2193
Patrick Delaunaya6151912018-03-12 10:46:15 +01002194static void stm32mp1_osc_init(struct udevice *dev)
2195{
2196 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2197 int i;
2198 const char *name[NB_OSC] = {
Etienne Carriere08db5d52021-02-24 11:19:42 +01002199 [_LSI] = "lsi",
2200 [_LSE] = "lse",
2201 [_HSI] = "hsi",
2202 [_HSE] = "hse",
2203 [_CSI] = "csi",
Patrick Delaunaya6151912018-03-12 10:46:15 +01002204 [_I2S_CKIN] = "i2s_ckin",
Patrick Delaunay86617dd2019-01-30 13:07:00 +01002205 };
Patrick Delaunaya6151912018-03-12 10:46:15 +01002206
2207 for (i = 0; i < NB_OSC; i++) {
Etienne Carriere08db5d52021-02-24 11:19:42 +01002208 if (clk_get_by_name(dev, name[i], &priv->osc_clk[i]))
2209 dev_dbg(dev, "No source clock \"%s\"", name[i]);
2210 else
2211 dev_dbg(dev, "%s clock rate: %luHz\n",
2212 name[i], clk_get_rate(&priv->osc_clk[i]));
Patrick Delaunaya6151912018-03-12 10:46:15 +01002213 }
2214}
2215
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002216static void __maybe_unused stm32mp1_clk_dump(struct stm32mp1_clk_priv *priv)
2217{
2218 char buf[32];
2219 int i, s, p;
2220
2221 printf("Clocks:\n");
2222 for (i = 0; i < _PARENT_NB; i++) {
2223 printf("- %s : %s MHz\n",
2224 stm32mp1_clk_parent_name[i],
2225 strmhz(buf, stm32mp1_clk_get(priv, i)));
2226 }
2227 printf("Source Clocks:\n");
2228 for (i = 0; i < _PARENT_SEL_NB; i++) {
2229 p = (readl(priv->base + priv->data->sel[i].offset) >>
2230 priv->data->sel[i].src) & priv->data->sel[i].msk;
2231 if (p < priv->data->sel[i].nb_parent) {
2232 s = priv->data->sel[i].parent[p];
2233 printf("- %s(%d) => parent %s(%d)\n",
2234 stm32mp1_clk_parent_sel_name[i], i,
2235 stm32mp1_clk_parent_name[s], s);
2236 } else {
2237 printf("- %s(%d) => parent index %d is invalid\n",
2238 stm32mp1_clk_parent_sel_name[i], i, p);
2239 }
2240 }
2241}
2242
2243#ifdef CONFIG_CMD_CLK
2244int soc_clk_dump(void)
2245{
2246 struct udevice *dev;
2247 struct stm32mp1_clk_priv *priv;
2248 int ret;
2249
2250 ret = uclass_get_device_by_driver(UCLASS_CLK,
Simon Glass65e25be2020-12-28 20:34:56 -07002251 DM_DRIVER_GET(stm32mp1_clock),
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002252 &dev);
2253 if (ret)
2254 return ret;
2255
2256 priv = dev_get_priv(dev);
2257
2258 stm32mp1_clk_dump(priv);
2259
2260 return 0;
2261}
2262#endif
2263
Patrick Delaunaya6151912018-03-12 10:46:15 +01002264static int stm32mp1_clk_probe(struct udevice *dev)
2265{
2266 int result = 0;
2267 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2268
2269 priv->base = dev_read_addr(dev->parent);
2270 if (priv->base == FDT_ADDR_T_NONE)
2271 return -EINVAL;
2272
2273 priv->data = (void *)&stm32mp1_data;
2274
2275 if (!priv->data->gate || !priv->data->sel ||
2276 !priv->data->pll)
2277 return -EINVAL;
2278
2279 stm32mp1_osc_init(dev);
2280
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002281#ifdef STM32MP1_CLOCK_TREE_INIT
2282 /* clock tree init is done only one time, before relocation */
2283 if (!(gd->flags & GD_FLG_RELOC))
2284 result = stm32mp1_clktree(dev);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002285 if (result)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002286 dev_err(dev, "clock tree initialization failed (%d)\n", result);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002287#endif
2288
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002289#ifndef CONFIG_SPL_BUILD
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002290#if defined(VERBOSE_DEBUG)
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002291 /* display debug information for probe after relocation */
2292 if (gd->flags & GD_FLG_RELOC)
2293 stm32mp1_clk_dump(priv);
2294#endif
2295
Patrick Delaunay4de076e2019-07-30 19:16:55 +02002296 gd->cpu_clk = stm32mp1_clk_get(priv, _CK_MPU);
2297 gd->bus_clk = stm32mp1_clk_get(priv, _ACLK);
2298 /* DDRPHYC father */
2299 gd->mem_clk = stm32mp1_clk_get(priv, _PLL2_R);
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002300#if defined(CONFIG_DISPLAY_CPUINFO)
2301 if (gd->flags & GD_FLG_RELOC) {
2302 char buf[32];
2303
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002304 log_info("Clocks:\n");
2305 log_info("- MPU : %s MHz\n", strmhz(buf, gd->cpu_clk));
2306 log_info("- MCU : %s MHz\n",
2307 strmhz(buf, stm32mp1_clk_get(priv, _CK_MCU)));
2308 log_info("- AXI : %s MHz\n", strmhz(buf, gd->bus_clk));
2309 log_info("- PER : %s MHz\n",
2310 strmhz(buf, stm32mp1_clk_get(priv, _CK_PER)));
2311 log_info("- DDR : %s MHz\n", strmhz(buf, gd->mem_clk));
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002312 }
2313#endif /* CONFIG_DISPLAY_CPUINFO */
2314#endif
2315
Patrick Delaunaya6151912018-03-12 10:46:15 +01002316 return result;
2317}
2318
2319static const struct clk_ops stm32mp1_clk_ops = {
2320 .enable = stm32mp1_clk_enable,
2321 .disable = stm32mp1_clk_disable,
2322 .get_rate = stm32mp1_clk_get_rate,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002323 .set_rate = stm32mp1_clk_set_rate,
Patrick Delaunaya6151912018-03-12 10:46:15 +01002324};
2325
Patrick Delaunaya6151912018-03-12 10:46:15 +01002326U_BOOT_DRIVER(stm32mp1_clock) = {
2327 .name = "stm32mp1_clk",
2328 .id = UCLASS_CLK,
Patrick Delaunaya6151912018-03-12 10:46:15 +01002329 .ops = &stm32mp1_clk_ops,
Simon Glass41575d82020-12-03 16:55:17 -07002330 .priv_auto = sizeof(struct stm32mp1_clk_priv),
Patrick Delaunaya6151912018-03-12 10:46:15 +01002331 .probe = stm32mp1_clk_probe,
2332};