blob: d4f10485919f4e3af6f23e69dd51f6d01051704b [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 <common.h>
9#include <clk-uclass.h>
10#include <div64.h>
11#include <dm.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010014#include <regmap.h>
15#include <spl.h>
16#include <syscon.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070017#include <time.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070018#include <vsprintf.h>
Patrick Delaunay885bdc22020-05-25 12:19:44 +020019#include <asm/arch/sys_proto.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 Delaunayf8fe21d2020-04-01 09:07:33 +020029#ifndef CONFIG_TFABOOT
Patrick Delaunayf11398e2018-03-12 10:46:16 +010030#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
31/* activate clock tree initialization in the driver */
32#define STM32MP1_CLOCK_TREE_INIT
33#endif
Patrick Delaunay5d061412019-02-12 11:44:39 +010034#endif
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010035
36#define MAX_HSI_HZ 64000000
37
Patrick Delaunayf11398e2018-03-12 10:46:16 +010038/* TIMEOUT */
39#define TIMEOUT_200MS 200000
40#define TIMEOUT_1S 1000000
41
Patrick Delaunaybf7d9442018-03-20 11:41:25 +010042/* STGEN registers */
43#define STGENC_CNTCR 0x00
44#define STGENC_CNTSR 0x04
45#define STGENC_CNTCVL 0x08
46#define STGENC_CNTCVU 0x0C
47#define STGENC_CNTFID0 0x20
48
49#define STGENC_CNTCR_EN BIT(0)
50
Patrick Delaunaye6ab6272018-03-12 10:46:15 +010051/* RCC registers */
52#define RCC_OCENSETR 0x0C
53#define RCC_OCENCLRR 0x10
54#define RCC_HSICFGR 0x18
55#define RCC_MPCKSELR 0x20
56#define RCC_ASSCKSELR 0x24
57#define RCC_RCK12SELR 0x28
58#define RCC_MPCKDIVR 0x2C
59#define RCC_AXIDIVR 0x30
60#define RCC_APB4DIVR 0x3C
61#define RCC_APB5DIVR 0x40
62#define RCC_RTCDIVR 0x44
63#define RCC_MSSCKSELR 0x48
64#define RCC_PLL1CR 0x80
65#define RCC_PLL1CFGR1 0x84
66#define RCC_PLL1CFGR2 0x88
67#define RCC_PLL1FRACR 0x8C
68#define RCC_PLL1CSGR 0x90
69#define RCC_PLL2CR 0x94
70#define RCC_PLL2CFGR1 0x98
71#define RCC_PLL2CFGR2 0x9C
72#define RCC_PLL2FRACR 0xA0
73#define RCC_PLL2CSGR 0xA4
74#define RCC_I2C46CKSELR 0xC0
75#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 Delaunay0b859a02020-03-09 14:59:23 +0100105#define RCC_SPI45CKSELR 0x8E0
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100106#define RCC_UART6CKSELR 0x8E4
107#define RCC_UART24CKSELR 0x8E8
108#define RCC_UART35CKSELR 0x8EC
109#define RCC_UART78CKSELR 0x8F0
110#define RCC_SDMMC12CKSELR 0x8F4
111#define RCC_SDMMC3CKSELR 0x8F8
112#define RCC_ETHCKSELR 0x8FC
113#define RCC_QSPICKSELR 0x900
114#define RCC_FMCCKSELR 0x904
115#define RCC_USBCKSELR 0x91C
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200116#define RCC_DSICKSELR 0x924
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200117#define RCC_ADCCKSELR 0x928
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100118#define RCC_MP_APB1ENSETR 0xA00
119#define RCC_MP_APB2ENSETR 0XA08
Fabrice Gasnier4cb3b532018-04-26 17:00:47 +0200120#define RCC_MP_APB3ENSETR 0xA10
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100121#define RCC_MP_AHB2ENSETR 0xA18
Benjamin Gaignard32470812018-11-27 13:49:51 +0100122#define RCC_MP_AHB3ENSETR 0xA20
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100123#define RCC_MP_AHB4ENSETR 0xA28
124
125/* used for most of SELR register */
126#define RCC_SELR_SRC_MASK GENMASK(2, 0)
127#define RCC_SELR_SRCRDY BIT(31)
128
129/* Values of RCC_MPCKSELR register */
130#define RCC_MPCKSELR_HSI 0
131#define RCC_MPCKSELR_HSE 1
132#define RCC_MPCKSELR_PLL 2
133#define RCC_MPCKSELR_PLL_MPUDIV 3
134
135/* Values of RCC_ASSCKSELR register */
136#define RCC_ASSCKSELR_HSI 0
137#define RCC_ASSCKSELR_HSE 1
138#define RCC_ASSCKSELR_PLL 2
139
140/* Values of RCC_MSSCKSELR register */
141#define RCC_MSSCKSELR_HSI 0
142#define RCC_MSSCKSELR_HSE 1
143#define RCC_MSSCKSELR_CSI 2
144#define RCC_MSSCKSELR_PLL 3
145
146/* Values of RCC_CPERCKSELR register */
147#define RCC_CPERCKSELR_HSI 0
148#define RCC_CPERCKSELR_CSI 1
149#define RCC_CPERCKSELR_HSE 2
150
151/* used for most of DIVR register : max div for RTC */
152#define RCC_DIVR_DIV_MASK GENMASK(5, 0)
153#define RCC_DIVR_DIVRDY BIT(31)
154
155/* Masks for specific DIVR registers */
156#define RCC_APBXDIV_MASK GENMASK(2, 0)
157#define RCC_MPUDIV_MASK GENMASK(2, 0)
158#define RCC_AXIDIV_MASK GENMASK(2, 0)
159#define RCC_MCUDIV_MASK GENMASK(3, 0)
160
161/* offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */
162#define RCC_MP_ENCLRR_OFFSET 4
163
164/* Fields of RCC_BDCR register */
165#define RCC_BDCR_LSEON BIT(0)
166#define RCC_BDCR_LSEBYP BIT(1)
167#define RCC_BDCR_LSERDY BIT(2)
Patrick Delaunay80cb5682018-07-16 10:41:46 +0200168#define RCC_BDCR_DIGBYP BIT(3)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100169#define RCC_BDCR_LSEDRV_MASK GENMASK(5, 4)
170#define RCC_BDCR_LSEDRV_SHIFT 4
171#define RCC_BDCR_LSECSSON BIT(8)
172#define RCC_BDCR_RTCCKEN BIT(20)
173#define RCC_BDCR_RTCSRC_MASK GENMASK(17, 16)
174#define RCC_BDCR_RTCSRC_SHIFT 16
175
176/* Fields of RCC_RDLSICR register */
177#define RCC_RDLSICR_LSION BIT(0)
178#define RCC_RDLSICR_LSIRDY BIT(1)
179
180/* used for ALL PLLNCR registers */
181#define RCC_PLLNCR_PLLON BIT(0)
182#define RCC_PLLNCR_PLLRDY BIT(1)
Patrick Delaunay9a6ce2a2019-01-30 13:07:06 +0100183#define RCC_PLLNCR_SSCG_CTRL BIT(2)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100184#define RCC_PLLNCR_DIVPEN BIT(4)
185#define RCC_PLLNCR_DIVQEN BIT(5)
186#define RCC_PLLNCR_DIVREN BIT(6)
187#define RCC_PLLNCR_DIVEN_SHIFT 4
188
189/* used for ALL PLLNCFGR1 registers */
190#define RCC_PLLNCFGR1_DIVM_SHIFT 16
191#define RCC_PLLNCFGR1_DIVM_MASK GENMASK(21, 16)
192#define RCC_PLLNCFGR1_DIVN_SHIFT 0
193#define RCC_PLLNCFGR1_DIVN_MASK GENMASK(8, 0)
194/* only for PLL3 and PLL4 */
195#define RCC_PLLNCFGR1_IFRGE_SHIFT 24
196#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK(25, 24)
197
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200198/* used for ALL PLLNCFGR2 registers , using stm32mp1_div_id */
199#define RCC_PLLNCFGR2_SHIFT(div_id) ((div_id) * 8)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100200#define RCC_PLLNCFGR2_DIVX_MASK GENMASK(6, 0)
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200201#define RCC_PLLNCFGR2_DIVP_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_P)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100202#define RCC_PLLNCFGR2_DIVP_MASK GENMASK(6, 0)
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200203#define RCC_PLLNCFGR2_DIVQ_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_Q)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100204#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK(14, 8)
Patrick Delaunaya7c0fd62018-07-16 10:41:41 +0200205#define RCC_PLLNCFGR2_DIVR_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_R)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100206#define RCC_PLLNCFGR2_DIVR_MASK GENMASK(22, 16)
207
208/* used for ALL PLLNFRACR registers */
209#define RCC_PLLNFRACR_FRACV_SHIFT 3
210#define RCC_PLLNFRACR_FRACV_MASK GENMASK(15, 3)
211#define RCC_PLLNFRACR_FRACLE BIT(16)
212
213/* used for ALL PLLNCSGR registers */
214#define RCC_PLLNCSGR_INC_STEP_SHIFT 16
215#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK(30, 16)
216#define RCC_PLLNCSGR_MOD_PER_SHIFT 0
217#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK(12, 0)
218#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15
219#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15)
220
221/* used for RCC_OCENSETR and RCC_OCENCLRR registers */
222#define RCC_OCENR_HSION BIT(0)
223#define RCC_OCENR_CSION BIT(4)
Patrick Delaunay80cb5682018-07-16 10:41:46 +0200224#define RCC_OCENR_DIGBYP BIT(7)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100225#define RCC_OCENR_HSEON BIT(8)
226#define RCC_OCENR_HSEBYP BIT(10)
227#define RCC_OCENR_HSECSSON BIT(11)
228
229/* Fields of RCC_OCRDYR register */
230#define RCC_OCRDYR_HSIRDY BIT(0)
231#define RCC_OCRDYR_HSIDIVRDY BIT(2)
232#define RCC_OCRDYR_CSIRDY BIT(4)
233#define RCC_OCRDYR_HSERDY BIT(8)
234
235/* Fields of DDRITFCR register */
236#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
237#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20
238#define RCC_DDRITFCR_DDRCKMOD_SSR 0
239
240/* Fields of RCC_HSICFGR register */
241#define RCC_HSICFGR_HSIDIV_MASK GENMASK(1, 0)
242
243/* used for MCO related operations */
244#define RCC_MCOCFG_MCOON BIT(12)
245#define RCC_MCOCFG_MCODIV_MASK GENMASK(7, 4)
246#define RCC_MCOCFG_MCODIV_SHIFT 4
247#define RCC_MCOCFG_MCOSRC_MASK GENMASK(2, 0)
248
249enum stm32mp1_parent_id {
250/*
251 * _HSI, _HSE, _CSI, _LSI, _LSE should not be moved
252 * they are used as index in osc[] as entry point
253 */
254 _HSI,
255 _HSE,
256 _CSI,
257 _LSI,
258 _LSE,
259 _I2S_CKIN,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100260 NB_OSC,
261
262/* other parent source */
263 _HSI_KER = NB_OSC,
264 _HSE_KER,
265 _HSE_KER_DIV2,
266 _CSI_KER,
267 _PLL1_P,
268 _PLL1_Q,
269 _PLL1_R,
270 _PLL2_P,
271 _PLL2_Q,
272 _PLL2_R,
273 _PLL3_P,
274 _PLL3_Q,
275 _PLL3_R,
276 _PLL4_P,
277 _PLL4_Q,
278 _PLL4_R,
279 _ACLK,
280 _PCLK1,
281 _PCLK2,
282 _PCLK3,
283 _PCLK4,
284 _PCLK5,
285 _HCLK6,
286 _HCLK2,
287 _CK_PER,
288 _CK_MPU,
289 _CK_MCU,
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200290 _DSI_PHY,
Patrick Delaunay7b726532019-01-30 13:07:00 +0100291 _USB_PHY_48,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100292 _PARENT_NB,
293 _UNKNOWN_ID = 0xff,
294};
295
296enum stm32mp1_parent_sel {
297 _I2C12_SEL,
298 _I2C35_SEL,
299 _I2C46_SEL,
300 _UART6_SEL,
301 _UART24_SEL,
302 _UART35_SEL,
303 _UART78_SEL,
304 _SDMMC12_SEL,
305 _SDMMC3_SEL,
306 _ETH_SEL,
307 _QSPI_SEL,
308 _FMC_SEL,
309 _USBPHY_SEL,
310 _USBO_SEL,
311 _STGEN_SEL,
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200312 _DSI_SEL,
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200313 _ADC12_SEL,
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200314 _SPI1_SEL,
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100315 _SPI45_SEL,
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200316 _RTC_SEL,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100317 _PARENT_SEL_NB,
318 _UNKNOWN_SEL = 0xff,
319};
320
321enum stm32mp1_pll_id {
322 _PLL1,
323 _PLL2,
324 _PLL3,
325 _PLL4,
326 _PLL_NB
327};
328
329enum stm32mp1_div_id {
330 _DIV_P,
331 _DIV_Q,
332 _DIV_R,
333 _DIV_NB,
334};
335
336enum stm32mp1_clksrc_id {
337 CLKSRC_MPU,
338 CLKSRC_AXI,
339 CLKSRC_MCU,
340 CLKSRC_PLL12,
341 CLKSRC_PLL3,
342 CLKSRC_PLL4,
343 CLKSRC_RTC,
344 CLKSRC_MCO1,
345 CLKSRC_MCO2,
346 CLKSRC_NB
347};
348
349enum stm32mp1_clkdiv_id {
350 CLKDIV_MPU,
351 CLKDIV_AXI,
352 CLKDIV_MCU,
353 CLKDIV_APB1,
354 CLKDIV_APB2,
355 CLKDIV_APB3,
356 CLKDIV_APB4,
357 CLKDIV_APB5,
358 CLKDIV_RTC,
359 CLKDIV_MCO1,
360 CLKDIV_MCO2,
361 CLKDIV_NB
362};
363
364enum stm32mp1_pllcfg {
365 PLLCFG_M,
366 PLLCFG_N,
367 PLLCFG_P,
368 PLLCFG_Q,
369 PLLCFG_R,
370 PLLCFG_O,
371 PLLCFG_NB
372};
373
374enum stm32mp1_pllcsg {
375 PLLCSG_MOD_PER,
376 PLLCSG_INC_STEP,
377 PLLCSG_SSCG_MODE,
378 PLLCSG_NB
379};
380
381enum stm32mp1_plltype {
382 PLL_800,
383 PLL_1600,
384 PLL_TYPE_NB
385};
386
387struct stm32mp1_pll {
388 u8 refclk_min;
389 u8 refclk_max;
390 u8 divn_max;
391};
392
393struct stm32mp1_clk_gate {
394 u16 offset;
395 u8 bit;
396 u8 index;
397 u8 set_clr;
398 u8 sel;
399 u8 fixed;
400};
401
402struct stm32mp1_clk_sel {
403 u16 offset;
404 u8 src;
405 u8 msk;
406 u8 nb_parent;
407 const u8 *parent;
408};
409
410#define REFCLK_SIZE 4
411struct stm32mp1_clk_pll {
412 enum stm32mp1_plltype plltype;
413 u16 rckxselr;
414 u16 pllxcfgr1;
415 u16 pllxcfgr2;
416 u16 pllxfracr;
417 u16 pllxcr;
418 u16 pllxcsgr;
419 u8 refclk[REFCLK_SIZE];
420};
421
422struct stm32mp1_clk_data {
423 const struct stm32mp1_clk_gate *gate;
424 const struct stm32mp1_clk_sel *sel;
425 const struct stm32mp1_clk_pll *pll;
426 const int nb_gate;
427};
428
429struct stm32mp1_clk_priv {
430 fdt_addr_t base;
431 const struct stm32mp1_clk_data *data;
432 ulong osc[NB_OSC];
433 struct udevice *osc_dev[NB_OSC];
434};
435
436#define STM32MP1_CLK(off, b, idx, s) \
437 { \
438 .offset = (off), \
439 .bit = (b), \
440 .index = (idx), \
441 .set_clr = 0, \
442 .sel = (s), \
443 .fixed = _UNKNOWN_ID, \
444 }
445
446#define STM32MP1_CLK_F(off, b, idx, f) \
447 { \
448 .offset = (off), \
449 .bit = (b), \
450 .index = (idx), \
451 .set_clr = 0, \
452 .sel = _UNKNOWN_SEL, \
453 .fixed = (f), \
454 }
455
456#define STM32MP1_CLK_SET_CLR(off, b, idx, s) \
457 { \
458 .offset = (off), \
459 .bit = (b), \
460 .index = (idx), \
461 .set_clr = 1, \
462 .sel = (s), \
463 .fixed = _UNKNOWN_ID, \
464 }
465
466#define STM32MP1_CLK_SET_CLR_F(off, b, idx, f) \
467 { \
468 .offset = (off), \
469 .bit = (b), \
470 .index = (idx), \
471 .set_clr = 1, \
472 .sel = _UNKNOWN_SEL, \
473 .fixed = (f), \
474 }
475
476#define STM32MP1_CLK_PARENT(idx, off, s, m, p) \
477 [(idx)] = { \
478 .offset = (off), \
479 .src = (s), \
480 .msk = (m), \
481 .parent = (p), \
482 .nb_parent = ARRAY_SIZE((p)) \
483 }
484
485#define STM32MP1_CLK_PLL(idx, type, off1, off2, off3, off4, off5, off6,\
486 p1, p2, p3, p4) \
487 [(idx)] = { \
488 .plltype = (type), \
489 .rckxselr = (off1), \
490 .pllxcfgr1 = (off2), \
491 .pllxcfgr2 = (off3), \
492 .pllxfracr = (off4), \
493 .pllxcr = (off5), \
494 .pllxcsgr = (off6), \
495 .refclk[0] = (p1), \
496 .refclk[1] = (p2), \
497 .refclk[2] = (p3), \
498 .refclk[3] = (p4), \
499 }
500
501static const u8 stm32mp1_clks[][2] = {
502 {CK_PER, _CK_PER},
503 {CK_MPU, _CK_MPU},
504 {CK_AXI, _ACLK},
505 {CK_MCU, _CK_MCU},
506 {CK_HSE, _HSE},
507 {CK_CSI, _CSI},
508 {CK_LSI, _LSI},
509 {CK_LSE, _LSE},
510 {CK_HSI, _HSI},
511 {CK_HSE_DIV2, _HSE_KER_DIV2},
512};
513
514static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
515 STM32MP1_CLK(RCC_DDRITFCR, 0, DDRC1, _UNKNOWN_SEL),
516 STM32MP1_CLK(RCC_DDRITFCR, 1, DDRC1LP, _UNKNOWN_SEL),
517 STM32MP1_CLK(RCC_DDRITFCR, 2, DDRC2, _UNKNOWN_SEL),
518 STM32MP1_CLK(RCC_DDRITFCR, 3, DDRC2LP, _UNKNOWN_SEL),
519 STM32MP1_CLK_F(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
520 STM32MP1_CLK(RCC_DDRITFCR, 5, DDRPHYCLP, _UNKNOWN_SEL),
521 STM32MP1_CLK(RCC_DDRITFCR, 6, DDRCAPB, _UNKNOWN_SEL),
522 STM32MP1_CLK(RCC_DDRITFCR, 7, DDRCAPBLP, _UNKNOWN_SEL),
523 STM32MP1_CLK(RCC_DDRITFCR, 8, AXIDCG, _UNKNOWN_SEL),
524 STM32MP1_CLK(RCC_DDRITFCR, 9, DDRPHYCAPB, _UNKNOWN_SEL),
525 STM32MP1_CLK(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _UNKNOWN_SEL),
526
527 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
528 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
529 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
530 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
531 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
532 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
533 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
534 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
535 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
536 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
537
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200538 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 8, SPI1_K, _SPI1_SEL),
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100539 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 10, SPI5_K, _SPI45_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100540 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
541
Fabrice Gasnier4cb3b532018-04-26 17:00:47 +0200542 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
543
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200544 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 0, LTDC_PX, _PLL4_Q),
545 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 4, DSI_PX, _PLL4_Q),
546 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 4, DSI_K, _DSI_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100547 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
548 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
549 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
550
551 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200552 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100553 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
554
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200555 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB2ENSETR, 5, ADC12, _HCLK2),
556 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 5, ADC12_K, _ADC12_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100557 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
558 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
559
Benjamin Gaignard32470812018-11-27 13:49:51 +0100560 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 11, HSEM, _UNKNOWN_SEL),
Patrick Delaunay629f44f2019-01-30 13:07:01 +0100561 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 12, IPCC, _UNKNOWN_SEL),
Benjamin Gaignard32470812018-11-27 13:49:51 +0100562
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100563 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
564 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
565 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
566 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
567 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
568 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
569 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
570 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
571 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
572 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
573 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
574
575 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
Sughosh Ganu1b725012019-12-28 23:58:28 +0530576 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 6, RNG1_K, _UNKNOWN_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100577
Patrick Delaunay5bfc8702019-05-17 15:08:42 +0200578 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK_K, _ETH_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100579 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
580 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100581 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
582 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
583 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
584 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
585 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
586 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
587
588 STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200589
590 STM32MP1_CLK(RCC_BDCR, 20, RTC, _RTC_SEL),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100591};
592
593static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
594static const u8 i2c35_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
595static const u8 i2c46_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER};
596static const u8 uart6_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
597 _HSE_KER};
598static const u8 uart24_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
599 _HSE_KER};
600static const u8 uart35_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
601 _HSE_KER};
602static const u8 uart78_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
603 _HSE_KER};
604static const u8 sdmmc12_parents[] = {_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER};
605static const u8 sdmmc3_parents[] = {_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER};
606static const u8 eth_parents[] = {_PLL4_P, _PLL3_Q};
607static const u8 qspi_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
608static const u8 fmc_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
609static const u8 usbphy_parents[] = {_HSE_KER, _PLL4_R, _HSE_KER_DIV2};
610static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
611static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200612static const u8 dsi_parents[] = {_DSI_PHY, _PLL4_P};
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200613static const u8 adc_parents[] = {_PLL4_R, _CK_PER, _PLL3_Q};
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200614static const u8 spi_parents[] = {_PLL4_P, _PLL3_Q, _I2S_CKIN, _CK_PER,
615 _PLL3_R};
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100616static const u8 spi45_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
617 _HSE_KER};
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200618static const u8 rtc_parents[] = {_UNKNOWN_ID, _LSE, _LSI, _HSE};
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100619
620static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
621 STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
622 STM32MP1_CLK_PARENT(_I2C35_SEL, RCC_I2C35CKSELR, 0, 0x7, i2c35_parents),
623 STM32MP1_CLK_PARENT(_I2C46_SEL, RCC_I2C46CKSELR, 0, 0x7, i2c46_parents),
624 STM32MP1_CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents),
625 STM32MP1_CLK_PARENT(_UART24_SEL, RCC_UART24CKSELR, 0, 0x7,
626 uart24_parents),
627 STM32MP1_CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7,
628 uart35_parents),
629 STM32MP1_CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7,
630 uart78_parents),
631 STM32MP1_CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7,
632 sdmmc12_parents),
633 STM32MP1_CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7,
634 sdmmc3_parents),
635 STM32MP1_CLK_PARENT(_ETH_SEL, RCC_ETHCKSELR, 0, 0x3, eth_parents),
Patrick Delaunay95e7fbe2020-03-09 14:59:22 +0100636 STM32MP1_CLK_PARENT(_QSPI_SEL, RCC_QSPICKSELR, 0, 0x3, qspi_parents),
637 STM32MP1_CLK_PARENT(_FMC_SEL, RCC_FMCCKSELR, 0, 0x3, fmc_parents),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100638 STM32MP1_CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents),
639 STM32MP1_CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents),
640 STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200641 STM32MP1_CLK_PARENT(_DSI_SEL, RCC_DSICKSELR, 0, 0x1, dsi_parents),
Patrick Delaunay95e7fbe2020-03-09 14:59:22 +0100642 STM32MP1_CLK_PARENT(_ADC12_SEL, RCC_ADCCKSELR, 0, 0x3, adc_parents),
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200643 STM32MP1_CLK_PARENT(_SPI1_SEL, RCC_SPI2S1CKSELR, 0, 0x7, spi_parents),
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100644 STM32MP1_CLK_PARENT(_SPI45_SEL, RCC_SPI45CKSELR, 0, 0x7, spi45_parents),
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200645 STM32MP1_CLK_PARENT(_RTC_SEL, RCC_BDCR, RCC_BDCR_RTCSRC_SHIFT,
646 (RCC_BDCR_RTCSRC_MASK >> RCC_BDCR_RTCSRC_SHIFT),
647 rtc_parents),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100648};
649
650#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay885bdc22020-05-25 12:19:44 +0200651
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100652/* define characteristic of PLL according type */
Patrick Delaunay885bdc22020-05-25 12:19:44 +0200653#define DIVM_MIN 0
654#define DIVM_MAX 63
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100655#define DIVN_MIN 24
Patrick Delaunay885bdc22020-05-25 12:19:44 +0200656#define DIVP_MIN 0
657#define DIVP_MAX 127
658#define FRAC_MAX 8192
659
660#define PLL1600_VCO_MIN 800000000
661#define PLL1600_VCO_MAX 1600000000
662
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100663static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
664 [PLL_800] = {
665 .refclk_min = 4,
666 .refclk_max = 16,
667 .divn_max = 99,
668 },
669 [PLL_1600] = {
670 .refclk_min = 8,
671 .refclk_max = 16,
672 .divn_max = 199,
673 },
674};
675#endif /* STM32MP1_CLOCK_TREE_INIT */
676
677static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
678 STM32MP1_CLK_PLL(_PLL1, PLL_1600,
679 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
680 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
681 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
682 STM32MP1_CLK_PLL(_PLL2, PLL_1600,
683 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
684 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
685 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
686 STM32MP1_CLK_PLL(_PLL3, PLL_800,
687 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
688 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
689 _HSI, _HSE, _CSI, _UNKNOWN_ID),
690 STM32MP1_CLK_PLL(_PLL4, PLL_800,
691 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
692 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
693 _HSI, _HSE, _CSI, _I2S_CKIN),
694};
695
696/* Prescaler table lookups for clock computation */
697/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
698static const u8 stm32mp1_mcu_div[16] = {
699 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
700};
701
702/* div = /1 /2 /4 /8 /16 : same divider for pmu and apbx*/
703#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
704#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
705static const u8 stm32mp1_mpu_apbx_div[8] = {
706 0, 1, 2, 3, 4, 4, 4, 4
707};
708
709/* div = /1 /2 /3 /4 */
710static const u8 stm32mp1_axi_div[8] = {
711 1, 2, 3, 4, 4, 4, 4, 4
712};
713
Patrick Delaunaye8d836c2019-01-30 13:07:04 +0100714static const __maybe_unused
715char * const stm32mp1_clk_parent_name[_PARENT_NB] = {
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100716 [_HSI] = "HSI",
717 [_HSE] = "HSE",
718 [_CSI] = "CSI",
719 [_LSI] = "LSI",
720 [_LSE] = "LSE",
721 [_I2S_CKIN] = "I2S_CKIN",
722 [_HSI_KER] = "HSI_KER",
723 [_HSE_KER] = "HSE_KER",
724 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
725 [_CSI_KER] = "CSI_KER",
726 [_PLL1_P] = "PLL1_P",
727 [_PLL1_Q] = "PLL1_Q",
728 [_PLL1_R] = "PLL1_R",
729 [_PLL2_P] = "PLL2_P",
730 [_PLL2_Q] = "PLL2_Q",
731 [_PLL2_R] = "PLL2_R",
732 [_PLL3_P] = "PLL3_P",
733 [_PLL3_Q] = "PLL3_Q",
734 [_PLL3_R] = "PLL3_R",
735 [_PLL4_P] = "PLL4_P",
736 [_PLL4_Q] = "PLL4_Q",
737 [_PLL4_R] = "PLL4_R",
738 [_ACLK] = "ACLK",
739 [_PCLK1] = "PCLK1",
740 [_PCLK2] = "PCLK2",
741 [_PCLK3] = "PCLK3",
742 [_PCLK4] = "PCLK4",
743 [_PCLK5] = "PCLK5",
744 [_HCLK6] = "KCLK6",
745 [_HCLK2] = "HCLK2",
746 [_CK_PER] = "CK_PER",
747 [_CK_MPU] = "CK_MPU",
748 [_CK_MCU] = "CK_MCU",
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200749 [_USB_PHY_48] = "USB_PHY_48",
750 [_DSI_PHY] = "DSI_PHY_PLL",
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100751};
752
Patrick Delaunaye8d836c2019-01-30 13:07:04 +0100753static const __maybe_unused
754char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100755 [_I2C12_SEL] = "I2C12",
756 [_I2C35_SEL] = "I2C35",
757 [_I2C46_SEL] = "I2C46",
758 [_UART6_SEL] = "UART6",
759 [_UART24_SEL] = "UART24",
760 [_UART35_SEL] = "UART35",
761 [_UART78_SEL] = "UART78",
762 [_SDMMC12_SEL] = "SDMMC12",
763 [_SDMMC3_SEL] = "SDMMC3",
764 [_ETH_SEL] = "ETH",
765 [_QSPI_SEL] = "QSPI",
766 [_FMC_SEL] = "FMC",
767 [_USBPHY_SEL] = "USBPHY",
768 [_USBO_SEL] = "USBO",
Patrick Delaunay8314d2c2018-07-16 10:41:43 +0200769 [_STGEN_SEL] = "STGEN",
770 [_DSI_SEL] = "DSI",
Patrick Delaunay201f0d52018-07-16 10:41:45 +0200771 [_ADC12_SEL] = "ADC12",
Patrice Chotard08ca06b2019-04-30 18:08:27 +0200772 [_SPI1_SEL] = "SPI1",
Patrick Delaunay0b859a02020-03-09 14:59:23 +0100773 [_SPI45_SEL] = "SPI45",
Patrick Delaunay03d87aa2019-07-11 12:03:37 +0200774 [_RTC_SEL] = "RTC",
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100775};
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100776
777static const struct stm32mp1_clk_data stm32mp1_data = {
778 .gate = stm32mp1_clk_gate,
779 .sel = stm32mp1_clk_sel,
780 .pll = stm32mp1_clk_pll,
781 .nb_gate = ARRAY_SIZE(stm32mp1_clk_gate),
782};
783
784static ulong stm32mp1_clk_get_fixed(struct stm32mp1_clk_priv *priv, int idx)
785{
786 if (idx >= NB_OSC) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100787 log_debug("clk id %d not found\n", idx);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100788 return 0;
789 }
790
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100791 return priv->osc[idx];
792}
793
794static int stm32mp1_clk_get_id(struct stm32mp1_clk_priv *priv, unsigned long id)
795{
796 const struct stm32mp1_clk_gate *gate = priv->data->gate;
797 int i, nb_clks = priv->data->nb_gate;
798
799 for (i = 0; i < nb_clks; i++) {
800 if (gate[i].index == id)
801 break;
802 }
803
804 if (i == nb_clks) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100805 log_err("clk id %d not found\n", (u32)id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100806 return -EINVAL;
807 }
808
809 return i;
810}
811
812static int stm32mp1_clk_get_sel(struct stm32mp1_clk_priv *priv,
813 int i)
814{
815 const struct stm32mp1_clk_gate *gate = priv->data->gate;
816
817 if (gate[i].sel > _PARENT_SEL_NB) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100818 log_err("parents for clk id %d not found\n", i);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100819 return -EINVAL;
820 }
821
822 return gate[i].sel;
823}
824
825static int stm32mp1_clk_get_fixed_parent(struct stm32mp1_clk_priv *priv,
826 int i)
827{
828 const struct stm32mp1_clk_gate *gate = priv->data->gate;
829
830 if (gate[i].fixed == _UNKNOWN_ID)
831 return -ENOENT;
832
833 return gate[i].fixed;
834}
835
836static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
837 unsigned long id)
838{
839 const struct stm32mp1_clk_sel *sel = priv->data->sel;
840 int i;
841 int s, p;
Patrick Delaunay942ee232019-06-21 15:26:48 +0200842 unsigned int idx;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100843
Patrick Delaunay942ee232019-06-21 15:26:48 +0200844 for (idx = 0; idx < ARRAY_SIZE(stm32mp1_clks); idx++)
845 if (stm32mp1_clks[idx][0] == id)
846 return stm32mp1_clks[idx][1];
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100847
848 i = stm32mp1_clk_get_id(priv, id);
849 if (i < 0)
850 return i;
851
852 p = stm32mp1_clk_get_fixed_parent(priv, i);
853 if (p >= 0 && p < _PARENT_NB)
854 return p;
855
856 s = stm32mp1_clk_get_sel(priv, i);
857 if (s < 0)
858 return s;
859
860 p = (readl(priv->base + sel[s].offset) >> sel[s].src) & sel[s].msk;
861
862 if (p < sel[s].nb_parent) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100863 log_content("%s clock is the parent %s of clk id %d\n",
864 stm32mp1_clk_parent_name[sel[s].parent[p]],
865 stm32mp1_clk_parent_sel_name[s],
866 (u32)id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100867 return sel[s].parent[p];
868 }
869
Patrick Delaunay30cd91e2020-11-06 19:01:45 +0100870 log_err("no parents defined for clk id %d\n", (u32)id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100871
872 return -EINVAL;
873}
874
Patrick Delaunay5327d372018-07-16 10:41:42 +0200875static ulong pll_get_fref_ck(struct stm32mp1_clk_priv *priv,
876 int pll_id)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100877{
878 const struct stm32mp1_clk_pll *pll = priv->data->pll;
Patrick Delaunay5327d372018-07-16 10:41:42 +0200879 u32 selr;
880 int src;
881 ulong refclk;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100882
Patrick Delaunay5327d372018-07-16 10:41:42 +0200883 /* Get current refclk */
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100884 selr = readl(priv->base + pll[pll_id].rckxselr);
Patrick Delaunay5327d372018-07-16 10:41:42 +0200885 src = selr & RCC_SELR_SRC_MASK;
886
887 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]);
Patrick Delaunay5327d372018-07-16 10:41:42 +0200888
889 return refclk;
890}
891
892/*
893 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
894 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
895 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
896 * => in all the case Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
897 */
898static ulong pll_get_fvco(struct stm32mp1_clk_priv *priv,
899 int pll_id)
900{
901 const struct stm32mp1_clk_pll *pll = priv->data->pll;
902 int divm, divn;
903 ulong refclk, fvco;
904 u32 cfgr1, fracr;
905
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100906 cfgr1 = readl(priv->base + pll[pll_id].pllxcfgr1);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100907 fracr = readl(priv->base + pll[pll_id].pllxfracr);
908
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100909 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
910 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100911
Patrick Delaunay5327d372018-07-16 10:41:42 +0200912 refclk = pll_get_fref_ck(priv, pll_id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100913
Patrick Delaunay5327d372018-07-16 10:41:42 +0200914 /* with FRACV :
915 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100916 * without FRACV
Patrick Delaunay5327d372018-07-16 10:41:42 +0200917 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100918 */
919 if (fracr & RCC_PLLNFRACR_FRACLE) {
920 u32 fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK)
921 >> RCC_PLLNFRACR_FRACV_SHIFT;
Patrick Delaunay5327d372018-07-16 10:41:42 +0200922 fvco = (ulong)lldiv((unsigned long long)refclk *
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100923 (((divn + 1) << 13) + fracv),
Patrick Delaunay5327d372018-07-16 10:41:42 +0200924 ((unsigned long long)(divm + 1)) << 13);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100925 } else {
Patrick Delaunay5327d372018-07-16 10:41:42 +0200926 fvco = (ulong)(refclk * (divn + 1) / (divm + 1));
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100927 }
Patrick Delaunay5327d372018-07-16 10:41:42 +0200928
929 return fvco;
930}
931
932static ulong stm32mp1_read_pll_freq(struct stm32mp1_clk_priv *priv,
933 int pll_id, int div_id)
934{
935 const struct stm32mp1_clk_pll *pll = priv->data->pll;
936 int divy;
937 ulong dfout;
938 u32 cfgr2;
939
Patrick Delaunay5327d372018-07-16 10:41:42 +0200940 if (div_id >= _DIV_NB)
941 return 0;
942
943 cfgr2 = readl(priv->base + pll[pll_id].pllxcfgr2);
944 divy = (cfgr2 >> RCC_PLLNCFGR2_SHIFT(div_id)) & RCC_PLLNCFGR2_DIVX_MASK;
945
Patrick Delaunay5327d372018-07-16 10:41:42 +0200946 dfout = pll_get_fvco(priv, pll_id) / (divy + 1);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100947
948 return dfout;
949}
950
951static ulong stm32mp1_clk_get(struct stm32mp1_clk_priv *priv, int p)
952{
953 u32 reg;
954 ulong clock = 0;
955
956 switch (p) {
957 case _CK_MPU:
958 /* MPU sub system */
959 reg = readl(priv->base + RCC_MPCKSELR);
960 switch (reg & RCC_SELR_SRC_MASK) {
961 case RCC_MPCKSELR_HSI:
962 clock = stm32mp1_clk_get_fixed(priv, _HSI);
963 break;
964 case RCC_MPCKSELR_HSE:
965 clock = stm32mp1_clk_get_fixed(priv, _HSE);
966 break;
967 case RCC_MPCKSELR_PLL:
968 case RCC_MPCKSELR_PLL_MPUDIV:
969 clock = stm32mp1_read_pll_freq(priv, _PLL1, _DIV_P);
Lionel Debieve97289492020-04-24 15:47:57 +0200970 if ((reg & RCC_SELR_SRC_MASK) ==
971 RCC_MPCKSELR_PLL_MPUDIV) {
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100972 reg = readl(priv->base + RCC_MPCKDIVR);
Lionel Debieve97289492020-04-24 15:47:57 +0200973 clock >>= stm32mp1_mpu_div[reg &
974 RCC_MPUDIV_MASK];
Patrick Delaunaye6ab6272018-03-12 10:46:15 +0100975 }
976 break;
977 }
978 break;
979 /* AXI sub system */
980 case _ACLK:
981 case _HCLK2:
982 case _HCLK6:
983 case _PCLK4:
984 case _PCLK5:
985 reg = readl(priv->base + RCC_ASSCKSELR);
986 switch (reg & RCC_SELR_SRC_MASK) {
987 case RCC_ASSCKSELR_HSI:
988 clock = stm32mp1_clk_get_fixed(priv, _HSI);
989 break;
990 case RCC_ASSCKSELR_HSE:
991 clock = stm32mp1_clk_get_fixed(priv, _HSE);
992 break;
993 case RCC_ASSCKSELR_PLL:
994 clock = stm32mp1_read_pll_freq(priv, _PLL2, _DIV_P);
995 break;
996 }
997
998 /* System clock divider */
999 reg = readl(priv->base + RCC_AXIDIVR);
1000 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
1001
1002 switch (p) {
1003 case _PCLK4:
1004 reg = readl(priv->base + RCC_APB4DIVR);
1005 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1006 break;
1007 case _PCLK5:
1008 reg = readl(priv->base + RCC_APB5DIVR);
1009 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1010 break;
1011 default:
1012 break;
1013 }
1014 break;
1015 /* MCU sub system */
1016 case _CK_MCU:
1017 case _PCLK1:
1018 case _PCLK2:
1019 case _PCLK3:
1020 reg = readl(priv->base + RCC_MSSCKSELR);
1021 switch (reg & RCC_SELR_SRC_MASK) {
1022 case RCC_MSSCKSELR_HSI:
1023 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1024 break;
1025 case RCC_MSSCKSELR_HSE:
1026 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1027 break;
1028 case RCC_MSSCKSELR_CSI:
1029 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1030 break;
1031 case RCC_MSSCKSELR_PLL:
1032 clock = stm32mp1_read_pll_freq(priv, _PLL3, _DIV_P);
1033 break;
1034 }
1035
1036 /* MCU clock divider */
1037 reg = readl(priv->base + RCC_MCUDIVR);
1038 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
1039
1040 switch (p) {
1041 case _PCLK1:
1042 reg = readl(priv->base + RCC_APB1DIVR);
1043 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1044 break;
1045 case _PCLK2:
1046 reg = readl(priv->base + RCC_APB2DIVR);
1047 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1048 break;
1049 case _PCLK3:
1050 reg = readl(priv->base + RCC_APB3DIVR);
1051 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1052 break;
1053 case _CK_MCU:
1054 default:
1055 break;
1056 }
1057 break;
1058 case _CK_PER:
1059 reg = readl(priv->base + RCC_CPERCKSELR);
1060 switch (reg & RCC_SELR_SRC_MASK) {
1061 case RCC_CPERCKSELR_HSI:
1062 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1063 break;
1064 case RCC_CPERCKSELR_HSE:
1065 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1066 break;
1067 case RCC_CPERCKSELR_CSI:
1068 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1069 break;
1070 }
1071 break;
1072 case _HSI:
1073 case _HSI_KER:
1074 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1075 break;
1076 case _CSI:
1077 case _CSI_KER:
1078 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1079 break;
1080 case _HSE:
1081 case _HSE_KER:
1082 case _HSE_KER_DIV2:
1083 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1084 if (p == _HSE_KER_DIV2)
1085 clock >>= 1;
1086 break;
1087 case _LSI:
1088 clock = stm32mp1_clk_get_fixed(priv, _LSI);
1089 break;
1090 case _LSE:
1091 clock = stm32mp1_clk_get_fixed(priv, _LSE);
1092 break;
1093 /* PLL */
1094 case _PLL1_P:
1095 case _PLL1_Q:
1096 case _PLL1_R:
1097 clock = stm32mp1_read_pll_freq(priv, _PLL1, p - _PLL1_P);
1098 break;
1099 case _PLL2_P:
1100 case _PLL2_Q:
1101 case _PLL2_R:
1102 clock = stm32mp1_read_pll_freq(priv, _PLL2, p - _PLL2_P);
1103 break;
1104 case _PLL3_P:
1105 case _PLL3_Q:
1106 case _PLL3_R:
1107 clock = stm32mp1_read_pll_freq(priv, _PLL3, p - _PLL3_P);
1108 break;
1109 case _PLL4_P:
1110 case _PLL4_Q:
1111 case _PLL4_R:
1112 clock = stm32mp1_read_pll_freq(priv, _PLL4, p - _PLL4_P);
1113 break;
1114 /* other */
1115 case _USB_PHY_48:
Patrick Delaunay7b726532019-01-30 13:07:00 +01001116 clock = 48000000;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001117 break;
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02001118 case _DSI_PHY:
1119 {
1120 struct clk clk;
1121 struct udevice *dev = NULL;
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001122
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02001123 if (!uclass_get_device_by_name(UCLASS_CLK, "ck_dsi_phy",
1124 &dev)) {
1125 if (clk_request(dev, &clk)) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001126 log_err("ck_dsi_phy request");
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02001127 } else {
1128 clk.id = 0;
1129 clock = clk_get_rate(&clk);
1130 }
1131 }
1132 break;
1133 }
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001134 default:
1135 break;
1136 }
1137
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001138 log_debug("id=%d clock = %lx : %ld kHz\n", p, clock, clock / 1000);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001139
1140 return clock;
1141}
1142
1143static int stm32mp1_clk_enable(struct clk *clk)
1144{
1145 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1146 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1147 int i = stm32mp1_clk_get_id(priv, clk->id);
1148
1149 if (i < 0)
1150 return i;
1151
1152 if (gate[i].set_clr)
1153 writel(BIT(gate[i].bit), priv->base + gate[i].offset);
1154 else
1155 setbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1156
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001157 dev_dbg(clk->dev, "%s: id clock %d has been enabled\n", __func__, (u32)clk->id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001158
1159 return 0;
1160}
1161
1162static int stm32mp1_clk_disable(struct clk *clk)
1163{
1164 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1165 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1166 int i = stm32mp1_clk_get_id(priv, clk->id);
1167
1168 if (i < 0)
1169 return i;
1170
1171 if (gate[i].set_clr)
1172 writel(BIT(gate[i].bit),
1173 priv->base + gate[i].offset
1174 + RCC_MP_ENCLRR_OFFSET);
1175 else
1176 clrbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1177
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001178 dev_dbg(clk->dev, "%s: id clock %d has been disabled\n", __func__, (u32)clk->id);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001179
1180 return 0;
1181}
1182
1183static ulong stm32mp1_clk_get_rate(struct clk *clk)
1184{
1185 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1186 int p = stm32mp1_clk_get_parent(priv, clk->id);
1187 ulong rate;
1188
1189 if (p < 0)
1190 return 0;
1191
1192 rate = stm32mp1_clk_get(priv, p);
1193
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001194 dev_vdbg(clk->dev, "computed rate for id clock %d is %d (parent is %s)\n",
1195 (u32)clk->id, (u32)rate, stm32mp1_clk_parent_name[p]);
1196
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01001197 return rate;
1198}
1199
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001200#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001201
1202bool stm32mp1_supports_opp(u32 opp_id, u32 cpu_type)
1203{
1204 unsigned int id;
1205
1206 switch (opp_id) {
1207 case 1:
1208 case 2:
1209 id = opp_id;
1210 break;
1211 default:
1212 id = 1; /* default value */
1213 break;
1214 }
1215
1216 switch (cpu_type) {
1217 case CPU_STM32MP157Fxx:
1218 case CPU_STM32MP157Dxx:
1219 case CPU_STM32MP153Fxx:
1220 case CPU_STM32MP153Dxx:
1221 case CPU_STM32MP151Fxx:
1222 case CPU_STM32MP151Dxx:
1223 return true;
1224 default:
1225 return id == 1;
1226 }
1227}
1228
Patrick Delaunay3d1fe4e2020-05-25 12:19:45 +02001229__weak void board_vddcore_init(u32 voltage_mv)
1230{
1231}
1232
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001233/*
1234 * gets OPP parameters (frequency in KHz and voltage in mV) from
1235 * an OPP table subnode. Platform HW support capabilities are also checked.
1236 * Returns 0 on success and a negative FDT error code on failure.
1237 */
1238static int stm32mp1_get_opp(u32 cpu_type, ofnode subnode,
1239 u32 *freq_khz, u32 *voltage_mv)
1240{
1241 u32 opp_hw;
1242 u64 read_freq_64;
1243 u32 read_voltage_32;
1244
1245 *freq_khz = 0;
1246 *voltage_mv = 0;
1247
1248 opp_hw = ofnode_read_u32_default(subnode, "opp-supported-hw", 0);
1249 if (opp_hw)
1250 if (!stm32mp1_supports_opp(opp_hw, cpu_type))
1251 return -FDT_ERR_BADVALUE;
1252
1253 read_freq_64 = ofnode_read_u64_default(subnode, "opp-hz", 0) /
1254 1000ULL;
1255 read_voltage_32 = ofnode_read_u32_default(subnode, "opp-microvolt", 0) /
1256 1000U;
1257
1258 if (!read_voltage_32 || !read_freq_64)
1259 return -FDT_ERR_NOTFOUND;
1260
1261 /* Frequency value expressed in KHz must fit on 32 bits */
1262 if (read_freq_64 > U32_MAX)
1263 return -FDT_ERR_BADVALUE;
1264
1265 /* Millivolt value must fit on 16 bits */
1266 if (read_voltage_32 > U16_MAX)
1267 return -FDT_ERR_BADVALUE;
1268
1269 *freq_khz = (u32)read_freq_64;
1270 *voltage_mv = read_voltage_32;
1271
1272 return 0;
1273}
1274
1275/*
1276 * parses OPP table in DT and finds the parameters for the
1277 * highest frequency supported by the HW platform.
1278 * Returns 0 on success and a negative FDT error code on failure.
1279 */
1280int stm32mp1_get_max_opp_freq(struct stm32mp1_clk_priv *priv, u64 *freq_hz)
1281{
1282 ofnode node, subnode;
1283 int ret;
1284 u32 freq = 0U, voltage = 0U;
1285 u32 cpu_type = get_cpu_type();
1286
1287 node = ofnode_by_compatible(ofnode_null(), "operating-points-v2");
1288 if (!ofnode_valid(node))
1289 return -FDT_ERR_NOTFOUND;
1290
1291 ofnode_for_each_subnode(subnode, node) {
1292 unsigned int read_freq;
1293 unsigned int read_voltage;
1294
1295 ret = stm32mp1_get_opp(cpu_type, subnode,
1296 &read_freq, &read_voltage);
1297 if (ret)
1298 continue;
1299
1300 if (read_freq > freq) {
1301 freq = read_freq;
1302 voltage = read_voltage;
1303 }
1304 }
1305
1306 if (!freq || !voltage)
1307 return -FDT_ERR_NOTFOUND;
1308
1309 *freq_hz = (u64)1000U * freq;
Patrick Delaunay3d1fe4e2020-05-25 12:19:45 +02001310 board_vddcore_init(voltage);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001311
1312 return 0;
1313}
1314
1315static int stm32mp1_pll1_opp(struct stm32mp1_clk_priv *priv, int clksrc,
1316 u32 *pllcfg, u32 *fracv)
1317{
1318 u32 post_divm;
1319 u32 input_freq;
1320 u64 output_freq;
1321 u64 freq;
1322 u64 vco;
1323 u32 divm, divn, divp, frac;
1324 int i, ret;
1325 u32 diff;
1326 u32 best_diff = U32_MAX;
1327
1328 /* PLL1 is 1600 */
1329 const u32 DIVN_MAX = stm32mp1_pll[PLL_1600].divn_max;
1330 const u32 POST_DIVM_MIN = stm32mp1_pll[PLL_1600].refclk_min * 1000000U;
1331 const u32 POST_DIVM_MAX = stm32mp1_pll[PLL_1600].refclk_max * 1000000U;
1332
1333 ret = stm32mp1_get_max_opp_freq(priv, &output_freq);
1334 if (ret) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001335 log_debug("PLL1 OPP configuration not found (%d).\n", ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001336 return ret;
1337 }
1338
1339 switch (clksrc) {
1340 case CLK_PLL12_HSI:
1341 input_freq = stm32mp1_clk_get_fixed(priv, _HSI);
1342 break;
1343 case CLK_PLL12_HSE:
1344 input_freq = stm32mp1_clk_get_fixed(priv, _HSE);
1345 break;
1346 default:
1347 return -EINTR;
1348 }
1349
1350 /* Following parameters have always the same value */
1351 pllcfg[PLLCFG_Q] = 0;
1352 pllcfg[PLLCFG_R] = 0;
1353 pllcfg[PLLCFG_O] = PQR(1, 0, 0);
1354
1355 for (divm = DIVM_MAX; divm >= DIVM_MIN; divm--) {
1356 post_divm = (u32)(input_freq / (divm + 1));
1357 if (post_divm < POST_DIVM_MIN || post_divm > POST_DIVM_MAX)
1358 continue;
1359
1360 for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
1361 freq = output_freq * (divm + 1) * (divp + 1);
1362 divn = (u32)((freq / input_freq) - 1);
1363 if (divn < DIVN_MIN || divn > DIVN_MAX)
1364 continue;
1365
1366 frac = (u32)(((freq * FRAC_MAX) / input_freq) -
1367 ((divn + 1) * FRAC_MAX));
1368 /* 2 loops to refine the fractional part */
1369 for (i = 2; i != 0; i--) {
1370 if (frac > FRAC_MAX)
1371 break;
1372
1373 vco = (post_divm * (divn + 1)) +
1374 ((post_divm * (u64)frac) /
1375 FRAC_MAX);
1376 if (vco < (PLL1600_VCO_MIN / 2) ||
1377 vco > (PLL1600_VCO_MAX / 2)) {
1378 frac++;
1379 continue;
1380 }
1381 freq = vco / (divp + 1);
1382 if (output_freq < freq)
1383 diff = (u32)(freq - output_freq);
1384 else
1385 diff = (u32)(output_freq - freq);
1386 if (diff < best_diff) {
1387 pllcfg[PLLCFG_M] = divm;
1388 pllcfg[PLLCFG_N] = divn;
1389 pllcfg[PLLCFG_P] = divp;
1390 *fracv = frac;
1391
1392 if (diff == 0)
1393 return 0;
1394
1395 best_diff = diff;
1396 }
1397 frac++;
1398 }
1399 }
1400 }
1401
1402 if (best_diff == U32_MAX)
1403 return -1;
1404
1405 return 0;
1406}
1407
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001408static void stm32mp1_ls_osc_set(int enable, fdt_addr_t rcc, u32 offset,
1409 u32 mask_on)
1410{
1411 u32 address = rcc + offset;
1412
1413 if (enable)
1414 setbits_le32(address, mask_on);
1415 else
1416 clrbits_le32(address, mask_on);
1417}
1418
1419static void stm32mp1_hs_ocs_set(int enable, fdt_addr_t rcc, u32 mask_on)
1420{
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001421 writel(mask_on, rcc + (enable ? RCC_OCENSETR : RCC_OCENCLRR));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001422}
1423
1424static int stm32mp1_osc_wait(int enable, fdt_addr_t rcc, u32 offset,
1425 u32 mask_rdy)
1426{
1427 u32 mask_test = 0;
1428 u32 address = rcc + offset;
1429 u32 val;
1430 int ret;
1431
1432 if (enable)
1433 mask_test = mask_rdy;
1434
1435 ret = readl_poll_timeout(address, val,
1436 (val & mask_rdy) == mask_test,
1437 TIMEOUT_1S);
1438
1439 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001440 log_err("OSC %x @ %x timeout for enable=%d : 0x%x\n",
1441 mask_rdy, address, enable, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001442
1443 return ret;
1444}
1445
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001446static void stm32mp1_lse_enable(fdt_addr_t rcc, int bypass, int digbyp,
Patrick Delaunay5ba62a42020-01-28 10:44:15 +01001447 u32 lsedrv)
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001448{
1449 u32 value;
1450
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001451 if (digbyp)
1452 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_DIGBYP);
1453
1454 if (bypass || digbyp)
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001455 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_LSEBYP);
1456
1457 /*
1458 * warning: not recommended to switch directly from "high drive"
1459 * to "medium low drive", and vice-versa.
1460 */
1461 value = (readl(rcc + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK)
1462 >> RCC_BDCR_LSEDRV_SHIFT;
1463
1464 while (value != lsedrv) {
1465 if (value > lsedrv)
1466 value--;
1467 else
1468 value++;
1469
1470 clrsetbits_le32(rcc + RCC_BDCR,
1471 RCC_BDCR_LSEDRV_MASK,
1472 value << RCC_BDCR_LSEDRV_SHIFT);
1473 }
1474
1475 stm32mp1_ls_osc_set(1, rcc, RCC_BDCR, RCC_BDCR_LSEON);
1476}
1477
1478static void stm32mp1_lse_wait(fdt_addr_t rcc)
1479{
1480 stm32mp1_osc_wait(1, rcc, RCC_BDCR, RCC_BDCR_LSERDY);
1481}
1482
1483static void stm32mp1_lsi_set(fdt_addr_t rcc, int enable)
1484{
1485 stm32mp1_ls_osc_set(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSION);
1486 stm32mp1_osc_wait(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSIRDY);
1487}
1488
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001489static void stm32mp1_hse_enable(fdt_addr_t rcc, int bypass, int digbyp, int css)
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001490{
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001491 if (digbyp)
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001492 writel(RCC_OCENR_DIGBYP, rcc + RCC_OCENSETR);
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001493 if (bypass || digbyp)
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001494 writel(RCC_OCENR_HSEBYP, rcc + RCC_OCENSETR);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001495
1496 stm32mp1_hs_ocs_set(1, rcc, RCC_OCENR_HSEON);
1497 stm32mp1_osc_wait(1, rcc, RCC_OCRDYR, RCC_OCRDYR_HSERDY);
1498
1499 if (css)
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001500 writel(RCC_OCENR_HSECSSON, rcc + RCC_OCENSETR);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001501}
1502
1503static void stm32mp1_csi_set(fdt_addr_t rcc, int enable)
1504{
Patrick Delaunayf5aaa072019-01-30 13:07:02 +01001505 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_CSION);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001506 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_CSIRDY);
1507}
1508
1509static void stm32mp1_hsi_set(fdt_addr_t rcc, int enable)
1510{
1511 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_HSION);
1512 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_HSIRDY);
1513}
1514
1515static int stm32mp1_set_hsidiv(fdt_addr_t rcc, u8 hsidiv)
1516{
1517 u32 address = rcc + RCC_OCRDYR;
1518 u32 val;
1519 int ret;
1520
1521 clrsetbits_le32(rcc + RCC_HSICFGR,
1522 RCC_HSICFGR_HSIDIV_MASK,
1523 RCC_HSICFGR_HSIDIV_MASK & hsidiv);
1524
1525 ret = readl_poll_timeout(address, val,
1526 val & RCC_OCRDYR_HSIDIVRDY,
1527 TIMEOUT_200MS);
1528 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001529 log_err("HSIDIV failed @ 0x%x: 0x%x\n",
1530 address, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001531
1532 return ret;
1533}
1534
1535static int stm32mp1_hsidiv(fdt_addr_t rcc, ulong hsifreq)
1536{
1537 u8 hsidiv;
1538 u32 hsidivfreq = MAX_HSI_HZ;
1539
1540 for (hsidiv = 0; hsidiv < 4; hsidiv++,
1541 hsidivfreq = hsidivfreq / 2)
1542 if (hsidivfreq == hsifreq)
1543 break;
1544
1545 if (hsidiv == 4) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001546 log_err("clk-hsi frequency invalid");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001547 return -1;
1548 }
1549
1550 if (hsidiv > 0)
1551 return stm32mp1_set_hsidiv(rcc, hsidiv);
1552
1553 return 0;
1554}
1555
1556static void pll_start(struct stm32mp1_clk_priv *priv, int pll_id)
1557{
1558 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1559
Patrick Delaunay9a6ce2a2019-01-30 13:07:06 +01001560 clrsetbits_le32(priv->base + pll[pll_id].pllxcr,
1561 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1562 RCC_PLLNCR_DIVREN,
1563 RCC_PLLNCR_PLLON);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001564}
1565
1566static int pll_output(struct stm32mp1_clk_priv *priv, int pll_id, int output)
1567{
1568 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1569 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1570 u32 val;
1571 int ret;
1572
1573 ret = readl_poll_timeout(pllxcr, val, val & RCC_PLLNCR_PLLRDY,
1574 TIMEOUT_200MS);
1575
1576 if (ret) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001577 log_err("PLL%d start failed @ 0x%x: 0x%x\n",
1578 pll_id, pllxcr, readl(pllxcr));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001579 return ret;
1580 }
1581
1582 /* start the requested output */
1583 setbits_le32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1584
1585 return 0;
1586}
1587
1588static int pll_stop(struct stm32mp1_clk_priv *priv, int pll_id)
1589{
1590 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1591 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1592 u32 val;
1593
1594 /* stop all output */
1595 clrbits_le32(pllxcr,
1596 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
1597
1598 /* stop PLL */
1599 clrbits_le32(pllxcr, RCC_PLLNCR_PLLON);
1600
1601 /* wait PLL stopped */
1602 return readl_poll_timeout(pllxcr, val, (val & RCC_PLLNCR_PLLRDY) == 0,
1603 TIMEOUT_200MS);
1604}
1605
1606static void pll_config_output(struct stm32mp1_clk_priv *priv,
1607 int pll_id, u32 *pllcfg)
1608{
1609 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1610 fdt_addr_t rcc = priv->base;
1611 u32 value;
1612
1613 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT)
1614 & RCC_PLLNCFGR2_DIVP_MASK;
1615 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT)
1616 & RCC_PLLNCFGR2_DIVQ_MASK;
1617 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT)
1618 & RCC_PLLNCFGR2_DIVR_MASK;
1619 writel(value, rcc + pll[pll_id].pllxcfgr2);
1620}
1621
1622static int pll_config(struct stm32mp1_clk_priv *priv, int pll_id,
1623 u32 *pllcfg, u32 fracv)
1624{
1625 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1626 fdt_addr_t rcc = priv->base;
1627 enum stm32mp1_plltype type = pll[pll_id].plltype;
1628 int src;
1629 ulong refclk;
1630 u8 ifrge = 0;
1631 u32 value;
1632
1633 src = readl(priv->base + pll[pll_id].rckxselr) & RCC_SELR_SRC_MASK;
1634
1635 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]) /
1636 (pllcfg[PLLCFG_M] + 1);
1637
1638 if (refclk < (stm32mp1_pll[type].refclk_min * 1000000) ||
1639 refclk > (stm32mp1_pll[type].refclk_max * 1000000)) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001640 log_err("invalid refclk = %x\n", (u32)refclk);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001641 return -EINVAL;
1642 }
1643 if (type == PLL_800 && refclk >= 8000000)
1644 ifrge = 1;
1645
1646 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT)
1647 & RCC_PLLNCFGR1_DIVN_MASK;
1648 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT)
1649 & RCC_PLLNCFGR1_DIVM_MASK;
1650 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT)
1651 & RCC_PLLNCFGR1_IFRGE_MASK;
1652 writel(value, rcc + pll[pll_id].pllxcfgr1);
1653
1654 /* fractional configuration: load sigma-delta modulator (SDM) */
1655
1656 /* Write into FRACV the new fractional value , and FRACLE to 0 */
1657 writel(fracv << RCC_PLLNFRACR_FRACV_SHIFT,
1658 rcc + pll[pll_id].pllxfracr);
1659
1660 /* Write FRACLE to 1 : FRACV value is loaded into the SDM */
1661 setbits_le32(rcc + pll[pll_id].pllxfracr,
1662 RCC_PLLNFRACR_FRACLE);
1663
1664 pll_config_output(priv, pll_id, pllcfg);
1665
1666 return 0;
1667}
1668
1669static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
1670{
1671 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1672 u32 pllxcsg;
1673
1674 pllxcsg = ((csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1675 RCC_PLLNCSGR_MOD_PER_MASK) |
1676 ((csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1677 RCC_PLLNCSGR_INC_STEP_MASK) |
1678 ((csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1679 RCC_PLLNCSGR_SSCG_MODE_MASK);
1680
1681 writel(pllxcsg, priv->base + pll[pll_id].pllxcsgr);
Patrick Delaunay9a6ce2a2019-01-30 13:07:06 +01001682
1683 setbits_le32(priv->base + pll[pll_id].pllxcr, RCC_PLLNCR_SSCG_CTRL);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001684}
1685
Patrick Delaunay854c59e2019-04-18 17:32:48 +02001686static __maybe_unused int pll_set_rate(struct udevice *dev,
1687 int pll_id,
1688 int div_id,
1689 unsigned long clk_rate)
1690{
1691 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1692 unsigned int pllcfg[PLLCFG_NB];
1693 ofnode plloff;
1694 char name[12];
1695 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1696 enum stm32mp1_plltype type = pll[pll_id].plltype;
1697 int divm, divn, divy;
1698 int ret;
1699 ulong fck_ref;
1700 u32 fracv;
1701 u64 value;
1702
1703 if (div_id > _DIV_NB)
1704 return -EINVAL;
1705
1706 sprintf(name, "st,pll@%d", pll_id);
1707 plloff = dev_read_subnode(dev, name);
1708 if (!ofnode_valid(plloff))
1709 return -FDT_ERR_NOTFOUND;
1710
1711 ret = ofnode_read_u32_array(plloff, "cfg",
1712 pllcfg, PLLCFG_NB);
1713 if (ret < 0)
1714 return -FDT_ERR_NOTFOUND;
1715
1716 fck_ref = pll_get_fref_ck(priv, pll_id);
1717
1718 divm = pllcfg[PLLCFG_M];
1719 /* select output divider = 0: for _DIV_P, 1:_DIV_Q 2:_DIV_R */
1720 divy = pllcfg[PLLCFG_P + div_id];
1721
1722 /* For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
1723 * So same final result than PLL2 et 4
1724 * with FRACV
1725 * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
1726 * / (DIVy + 1) * (DIVM + 1)
1727 * value = (DIVN + 1) * 2^13 + FRACV / 2^13
1728 * = Fck_pll_y (DIVy + 1) * (DIVM + 1) * 2^13 / Fck_ref
1729 */
1730 value = ((u64)clk_rate * (divy + 1) * (divm + 1)) << 13;
1731 value = lldiv(value, fck_ref);
1732
1733 divn = (value >> 13) - 1;
1734 if (divn < DIVN_MIN ||
1735 divn > stm32mp1_pll[type].divn_max) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001736 dev_err(dev, "divn invalid = %d", divn);
Patrick Delaunay854c59e2019-04-18 17:32:48 +02001737 return -EINVAL;
1738 }
1739 fracv = value - ((divn + 1) << 13);
1740 pllcfg[PLLCFG_N] = divn;
1741
1742 /* reconfigure PLL */
1743 pll_stop(priv, pll_id);
1744 pll_config(priv, pll_id, pllcfg, fracv);
1745 pll_start(priv, pll_id);
1746 pll_output(priv, pll_id, pllcfg[PLLCFG_O]);
1747
1748 return 0;
1749}
1750
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001751static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
1752{
1753 u32 address = priv->base + (clksrc >> 4);
1754 u32 val;
1755 int ret;
1756
1757 clrsetbits_le32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK);
1758 ret = readl_poll_timeout(address, val, val & RCC_SELR_SRCRDY,
1759 TIMEOUT_200MS);
1760 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001761 log_err("CLKSRC %x start failed @ 0x%x: 0x%x\n",
1762 clksrc, address, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001763
1764 return ret;
1765}
1766
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001767static void stgen_config(struct stm32mp1_clk_priv *priv)
1768{
1769 int p;
1770 u32 stgenc, cntfid0;
1771 ulong rate;
1772
Patrick Delaunay82b88ef2019-07-05 17:20:11 +02001773 stgenc = STM32_STGEN_BASE;
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001774 cntfid0 = readl(stgenc + STGENC_CNTFID0);
1775 p = stm32mp1_clk_get_parent(priv, STGEN_K);
1776 rate = stm32mp1_clk_get(priv, p);
1777
1778 if (cntfid0 != rate) {
Patrick Delaunay45e5da52019-01-30 13:07:03 +01001779 u64 counter;
1780
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001781 log_debug("System Generic Counter (STGEN) update\n");
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001782 clrbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
Patrick Delaunay45e5da52019-01-30 13:07:03 +01001783 counter = (u64)readl(stgenc + STGENC_CNTCVL);
1784 counter |= ((u64)(readl(stgenc + STGENC_CNTCVU))) << 32;
1785 counter = lldiv(counter * (u64)rate, cntfid0);
1786 writel((u32)counter, stgenc + STGENC_CNTCVL);
1787 writel((u32)(counter >> 32), stgenc + STGENC_CNTCVU);
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001788 writel(rate, stgenc + STGENC_CNTFID0);
1789 setbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1790
1791 __asm__ volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (rate));
1792
1793 /* need to update gd->arch.timer_rate_hz with new frequency */
1794 timer_init();
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001795 }
1796}
1797
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001798static int set_clkdiv(unsigned int clkdiv, u32 address)
1799{
1800 u32 val;
1801 int ret;
1802
1803 clrsetbits_le32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK);
1804 ret = readl_poll_timeout(address, val, val & RCC_DIVR_DIVRDY,
1805 TIMEOUT_200MS);
1806 if (ret)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001807 log_err("CLKDIV %x start failed @ 0x%x: 0x%x\n",
1808 clkdiv, address, readl(address));
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001809
1810 return ret;
1811}
1812
1813static void stm32mp1_mco_csg(struct stm32mp1_clk_priv *priv,
1814 u32 clksrc, u32 clkdiv)
1815{
1816 u32 address = priv->base + (clksrc >> 4);
1817
1818 /*
1819 * binding clksrc : bit15-4 offset
1820 * bit3: disable
1821 * bit2-0: MCOSEL[2:0]
1822 */
1823 if (clksrc & 0x8) {
1824 clrbits_le32(address, RCC_MCOCFG_MCOON);
1825 } else {
1826 clrsetbits_le32(address,
1827 RCC_MCOCFG_MCOSRC_MASK,
1828 clksrc & RCC_MCOCFG_MCOSRC_MASK);
1829 clrsetbits_le32(address,
1830 RCC_MCOCFG_MCODIV_MASK,
1831 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
1832 setbits_le32(address, RCC_MCOCFG_MCOON);
1833 }
1834}
1835
1836static void set_rtcsrc(struct stm32mp1_clk_priv *priv,
1837 unsigned int clksrc,
1838 int lse_css)
1839{
1840 u32 address = priv->base + RCC_BDCR;
1841
1842 if (readl(address) & RCC_BDCR_RTCCKEN)
1843 goto skip_rtc;
1844
1845 if (clksrc == CLK_RTC_DISABLED)
1846 goto skip_rtc;
1847
1848 clrsetbits_le32(address,
1849 RCC_BDCR_RTCSRC_MASK,
1850 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1851
1852 setbits_le32(address, RCC_BDCR_RTCCKEN);
1853
1854skip_rtc:
1855 if (lse_css)
1856 setbits_le32(address, RCC_BDCR_LSECSSON);
1857}
1858
1859static void pkcs_config(struct stm32mp1_clk_priv *priv, u32 pkcs)
1860{
1861 u32 address = priv->base + ((pkcs >> 4) & 0xFFF);
1862 u32 value = pkcs & 0xF;
1863 u32 mask = 0xF;
1864
1865 if (pkcs & BIT(31)) {
1866 mask <<= 4;
1867 value <<= 4;
1868 }
1869 clrsetbits_le32(address, mask, value);
1870}
1871
1872static int stm32mp1_clktree(struct udevice *dev)
1873{
1874 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1875 fdt_addr_t rcc = priv->base;
1876 unsigned int clksrc[CLKSRC_NB];
1877 unsigned int clkdiv[CLKDIV_NB];
1878 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001879 unsigned int pllfracv[_PLL_NB];
1880 unsigned int pllcsg[_PLL_NB][PLLCSG_NB];
1881 bool pllcfg_valid[_PLL_NB];
1882 bool pllcsg_set[_PLL_NB];
1883 int ret;
1884 int i, len;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001885 int lse_css = 0;
1886 const u32 *pkcs_cell;
1887
1888 /* check mandatory field */
1889 ret = dev_read_u32_array(dev, "st,clksrc", clksrc, CLKSRC_NB);
1890 if (ret < 0) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001891 dev_dbg(dev, "field st,clksrc invalid: error %d\n", ret);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001892 return -FDT_ERR_NOTFOUND;
1893 }
1894
1895 ret = dev_read_u32_array(dev, "st,clkdiv", clkdiv, CLKDIV_NB);
1896 if (ret < 0) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001897 dev_dbg(dev, "field st,clkdiv invalid: error %d\n", ret);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001898 return -FDT_ERR_NOTFOUND;
1899 }
1900
1901 /* check mandatory field in each pll */
1902 for (i = 0; i < _PLL_NB; i++) {
1903 char name[12];
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001904 ofnode node;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001905
1906 sprintf(name, "st,pll@%d", i);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001907 node = dev_read_subnode(dev, name);
1908 pllcfg_valid[i] = ofnode_valid(node);
1909 pllcsg_set[i] = false;
1910 if (pllcfg_valid[i]) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001911 dev_dbg(dev, "DT for PLL %d @ %s\n", i, name);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001912 ret = ofnode_read_u32_array(node, "cfg",
1913 pllcfg[i], PLLCFG_NB);
1914 if (ret < 0) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001915 dev_dbg(dev, "field cfg invalid: error %d\n", ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001916 return -FDT_ERR_NOTFOUND;
1917 }
1918 pllfracv[i] = ofnode_read_u32_default(node, "frac", 0);
1919
1920 ret = ofnode_read_u32_array(node, "csg", pllcsg[i],
1921 PLLCSG_NB);
1922 if (!ret) {
1923 pllcsg_set[i] = true;
1924 } else if (ret != -FDT_ERR_NOTFOUND) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001925 dev_dbg(dev, "invalid csg node for pll@%d res=%d\n",
1926 i, ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001927 return ret;
1928 }
1929 } else if (i == _PLL1) {
1930 /* use OPP for PLL1 for A7 CPU */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001931 dev_dbg(dev, "DT for PLL %d with OPP\n", i);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001932 ret = stm32mp1_pll1_opp(priv,
1933 clksrc[CLKSRC_PLL12],
1934 pllcfg[i],
1935 &pllfracv[i]);
1936 if (ret) {
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001937 dev_dbg(dev, "PLL %d with OPP error = %d\n", i, ret);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02001938 return ret;
1939 }
1940 pllcfg_valid[i] = true;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001941 }
1942 }
1943
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001944 dev_dbg(dev, "configuration MCO\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001945 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1946 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1947
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001948 dev_dbg(dev, "switch ON osillator\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001949 /*
1950 * switch ON oscillator found in device-tree,
1951 * HSI already ON after bootrom
1952 */
1953 if (priv->osc[_LSI])
1954 stm32mp1_lsi_set(rcc, 1);
1955
1956 if (priv->osc[_LSE]) {
Patrick Delaunay5ba62a42020-01-28 10:44:15 +01001957 int bypass, digbyp;
1958 u32 lsedrv;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001959 struct udevice *dev = priv->osc_dev[_LSE];
1960
1961 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001962 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001963 lse_css = dev_read_bool(dev, "st,css");
1964 lsedrv = dev_read_u32_default(dev, "st,drive",
1965 LSEDRV_MEDIUM_HIGH);
1966
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001967 stm32mp1_lse_enable(rcc, bypass, digbyp, lsedrv);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001968 }
1969
1970 if (priv->osc[_HSE]) {
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001971 int bypass, digbyp, css;
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001972 struct udevice *dev = priv->osc_dev[_HSE];
1973
1974 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001975 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001976 css = dev_read_bool(dev, "st,css");
1977
Patrick Delaunay80cb5682018-07-16 10:41:46 +02001978 stm32mp1_hse_enable(rcc, bypass, digbyp, css);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001979 }
1980 /* CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1981 * => switch on CSI even if node is not present in device tree
1982 */
1983 stm32mp1_csi_set(rcc, 1);
1984
1985 /* come back to HSI */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001986 dev_dbg(dev, "come back to HSI\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001987 set_clksrc(priv, CLK_MPU_HSI);
1988 set_clksrc(priv, CLK_AXI_HSI);
1989 set_clksrc(priv, CLK_MCU_HSI);
1990
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001991 dev_dbg(dev, "pll stop\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001992 for (i = 0; i < _PLL_NB; i++)
1993 pll_stop(priv, i);
1994
1995 /* configure HSIDIV */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01001996 dev_dbg(dev, "configure HSIDIV\n");
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001997 if (priv->osc[_HSI]) {
Patrick Delaunayf11398e2018-03-12 10:46:16 +01001998 stm32mp1_hsidiv(rcc, priv->osc[_HSI]);
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01001999 stgen_config(priv);
2000 }
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002001
2002 /* select DIV */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002003 dev_dbg(dev, "select DIV\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002004 /* no ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
2005 writel(clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK, rcc + RCC_MPCKDIVR);
2006 set_clkdiv(clkdiv[CLKDIV_AXI], rcc + RCC_AXIDIVR);
2007 set_clkdiv(clkdiv[CLKDIV_APB4], rcc + RCC_APB4DIVR);
2008 set_clkdiv(clkdiv[CLKDIV_APB5], rcc + RCC_APB5DIVR);
2009 set_clkdiv(clkdiv[CLKDIV_MCU], rcc + RCC_MCUDIVR);
2010 set_clkdiv(clkdiv[CLKDIV_APB1], rcc + RCC_APB1DIVR);
2011 set_clkdiv(clkdiv[CLKDIV_APB2], rcc + RCC_APB2DIVR);
2012 set_clkdiv(clkdiv[CLKDIV_APB3], rcc + RCC_APB3DIVR);
2013
2014 /* no ready bit for RTC */
2015 writel(clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK, rcc + RCC_RTCDIVR);
2016
2017 /* configure PLLs source */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002018 dev_dbg(dev, "configure PLLs source\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002019 set_clksrc(priv, clksrc[CLKSRC_PLL12]);
2020 set_clksrc(priv, clksrc[CLKSRC_PLL3]);
2021 set_clksrc(priv, clksrc[CLKSRC_PLL4]);
2022
2023 /* configure and start PLLs */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002024 dev_dbg(dev, "configure PLLs\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002025 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002026 if (!pllcfg_valid[i])
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002027 continue;
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002028 dev_dbg(dev, "configure PLL %d\n", i);
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002029 pll_config(priv, i, pllcfg[i], pllfracv[i]);
2030 if (pllcsg_set[i])
2031 pll_csg(priv, i, pllcsg[i]);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002032 pll_start(priv, i);
2033 }
2034
2035 /* wait and start PLLs ouptut when ready */
2036 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay885bdc22020-05-25 12:19:44 +02002037 if (!pllcfg_valid[i])
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002038 continue;
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002039 dev_dbg(dev, "output PLL %d\n", i);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002040 pll_output(priv, i, pllcfg[i][PLLCFG_O]);
2041 }
2042
2043 /* wait LSE ready before to use it */
2044 if (priv->osc[_LSE])
2045 stm32mp1_lse_wait(rcc);
2046
2047 /* configure with expected clock source */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002048 dev_dbg(dev, "CLKSRC\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002049 set_clksrc(priv, clksrc[CLKSRC_MPU]);
2050 set_clksrc(priv, clksrc[CLKSRC_AXI]);
2051 set_clksrc(priv, clksrc[CLKSRC_MCU]);
2052 set_rtcsrc(priv, clksrc[CLKSRC_RTC], lse_css);
2053
2054 /* configure PKCK */
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002055 dev_dbg(dev, "PKCK\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002056 pkcs_cell = dev_read_prop(dev, "st,pkcs", &len);
2057 if (pkcs_cell) {
2058 bool ckper_disabled = false;
2059
2060 for (i = 0; i < len / sizeof(u32); i++) {
2061 u32 pkcs = (u32)fdt32_to_cpu(pkcs_cell[i]);
2062
2063 if (pkcs == CLK_CKPER_DISABLED) {
2064 ckper_disabled = true;
2065 continue;
2066 }
2067 pkcs_config(priv, pkcs);
2068 }
2069 /* CKPER is source for some peripheral clock
2070 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2071 * only if previous clock is still ON
2072 * => deactivated CKPER only after switching clock
2073 */
2074 if (ckper_disabled)
2075 pkcs_config(priv, CLK_CKPER_DISABLED);
2076 }
2077
Patrick Delaunaybf7d9442018-03-20 11:41:25 +01002078 /* STGEN clock source can change with CLK_STGEN_XXX */
2079 stgen_config(priv);
2080
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002081 dev_dbg(dev, "oscillator off\n");
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002082 /* switch OFF HSI if not found in device-tree */
2083 if (!priv->osc[_HSI])
2084 stm32mp1_hsi_set(rcc, 0);
2085
2086 /* Software Self-Refresh mode (SSR) during DDR initilialization */
2087 clrsetbits_le32(priv->base + RCC_DDRITFCR,
2088 RCC_DDRITFCR_DDRCKMOD_MASK,
2089 RCC_DDRITFCR_DDRCKMOD_SSR <<
2090 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2091
2092 return 0;
2093}
2094#endif /* STM32MP1_CLOCK_TREE_INIT */
2095
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002096static int pll_set_output_rate(struct udevice *dev,
2097 int pll_id,
2098 int div_id,
2099 unsigned long clk_rate)
2100{
2101 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2102 const struct stm32mp1_clk_pll *pll = priv->data->pll;
2103 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
2104 int div;
2105 ulong fvco;
2106
2107 if (div_id > _DIV_NB)
2108 return -EINVAL;
2109
2110 fvco = pll_get_fvco(priv, pll_id);
2111
2112 if (fvco <= clk_rate)
2113 div = 1;
2114 else
2115 div = DIV_ROUND_UP(fvco, clk_rate);
2116
2117 if (div > 128)
2118 div = 128;
2119
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002120 /* stop the requested output */
2121 clrbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2122 /* change divider */
2123 clrsetbits_le32(priv->base + pll[pll_id].pllxcfgr2,
2124 RCC_PLLNCFGR2_DIVX_MASK << RCC_PLLNCFGR2_SHIFT(div_id),
2125 (div - 1) << RCC_PLLNCFGR2_SHIFT(div_id));
2126 /* start the requested output */
2127 setbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2128
2129 return 0;
2130}
2131
2132static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
2133{
2134 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
2135 int p;
2136
2137 switch (clk->id) {
Patrick Delaunay854c59e2019-04-18 17:32:48 +02002138#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2139 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2140 case DDRPHYC:
2141 break;
2142#endif
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002143 case LTDC_PX:
2144 case DSI_PX:
2145 break;
2146 default:
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002147 dev_err(clk->dev, "Set of clk %ld not supported", clk->id);
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002148 return -EINVAL;
2149 }
2150
2151 p = stm32mp1_clk_get_parent(priv, clk->id);
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002152 dev_vdbg(clk->dev, "parent = %d:%s\n", p, stm32mp1_clk_parent_name[p]);
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002153 if (p < 0)
2154 return -EINVAL;
2155
2156 switch (p) {
Patrick Delaunay854c59e2019-04-18 17:32:48 +02002157#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2158 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2159 case _PLL2_R: /* DDRPHYC */
2160 {
2161 /* only for change DDR clock in interactive mode */
2162 ulong result;
2163
2164 set_clksrc(priv, CLK_AXI_HSI);
2165 result = pll_set_rate(clk->dev, _PLL2, _DIV_R, clk_rate);
2166 set_clksrc(priv, CLK_AXI_PLL2P);
2167 return result;
2168 }
2169#endif
Patrick Delaunaya06a4562019-07-30 19:16:54 +02002170
Patrick Delaunay8314d2c2018-07-16 10:41:43 +02002171 case _PLL4_Q:
2172 /* for LTDC_PX and DSI_PX case */
2173 return pll_set_output_rate(clk->dev, _PLL4, _DIV_Q, clk_rate);
2174 }
2175
2176 return -EINVAL;
2177}
2178
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002179static void stm32mp1_osc_clk_init(const char *name,
2180 struct stm32mp1_clk_priv *priv,
2181 int index)
2182{
2183 struct clk clk;
2184 struct udevice *dev = NULL;
2185
2186 priv->osc[index] = 0;
2187 clk.id = 0;
2188 if (!uclass_get_device_by_name(UCLASS_CLK, name, &dev)) {
2189 if (clk_request(dev, &clk))
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002190 log_err("%s request", name);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002191 else
2192 priv->osc[index] = clk_get_rate(&clk);
2193 }
2194 priv->osc_dev[index] = dev;
2195}
2196
2197static void stm32mp1_osc_init(struct udevice *dev)
2198{
2199 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2200 int i;
2201 const char *name[NB_OSC] = {
2202 [_LSI] = "clk-lsi",
2203 [_LSE] = "clk-lse",
2204 [_HSI] = "clk-hsi",
2205 [_HSE] = "clk-hse",
2206 [_CSI] = "clk-csi",
2207 [_I2S_CKIN] = "i2s_ckin",
Patrick Delaunay7b726532019-01-30 13:07:00 +01002208 };
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002209
2210 for (i = 0; i < NB_OSC; i++) {
2211 stm32mp1_osc_clk_init(name[i], priv, i);
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002212 dev_dbg(dev, "%d: %s => %x\n", i, name[i], (u32)priv->osc[i]);
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002213 }
2214}
2215
Patrick Delaunaye8d836c2019-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 Glass65130cd2020-12-28 20:34:56 -07002251 DM_DRIVER_GET(stm32mp1_clock),
Patrick Delaunaye8d836c2019-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 Delaunaye6ab6272018-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 Delaunayf11398e2018-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 Delaunay885bdc22020-05-25 12:19:44 +02002285 if (result)
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002286 dev_err(dev, "clock tree initialization failed (%d)\n", result);
Patrick Delaunayf11398e2018-03-12 10:46:16 +01002287#endif
2288
Patrick Delaunaye8d836c2019-01-30 13:07:04 +01002289#ifndef CONFIG_SPL_BUILD
Patrick Delaunay30cd91e2020-11-06 19:01:45 +01002290#if defined(VERBOSE_DEBUG)
Patrick Delaunaye8d836c2019-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 Delaunaya77c6ed2019-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 Delaunaye8d836c2019-01-30 13:07:04 +01002300#if defined(CONFIG_DISPLAY_CPUINFO)
2301 if (gd->flags & GD_FLG_RELOC) {
2302 char buf[32];
2303
Patrick Delaunay30cd91e2020-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 Delaunaye8d836c2019-01-30 13:07:04 +01002312 }
2313#endif /* CONFIG_DISPLAY_CPUINFO */
2314#endif
2315
Patrick Delaunaye6ab6272018-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 Delaunay8314d2c2018-07-16 10:41:43 +02002323 .set_rate = stm32mp1_clk_set_rate,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002324};
2325
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002326U_BOOT_DRIVER(stm32mp1_clock) = {
2327 .name = "stm32mp1_clk",
2328 .id = UCLASS_CLK,
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002329 .ops = &stm32mp1_clk_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002330 .priv_auto = sizeof(struct stm32mp1_clk_priv),
Patrick Delaunaye6ab6272018-03-12 10:46:15 +01002331 .probe = stm32mp1_clk_probe,
2332};