blob: 848e33f4e824338243b972ddc1c92c690c17cb21 [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
76#define RCC_CPERCKSELR 0xD0
77#define RCC_STGENCKSELR 0xD4
78#define RCC_DDRITFCR 0xD8
79#define RCC_BDCR 0x140
80#define RCC_RDLSICR 0x144
81#define RCC_MP_APB4ENSETR 0x200
82#define RCC_MP_APB5ENSETR 0x208
83#define RCC_MP_AHB5ENSETR 0x210
84#define RCC_MP_AHB6ENSETR 0x218
85#define RCC_OCRDYR 0x808
86#define RCC_DBGCFGR 0x80C
87#define RCC_RCK3SELR 0x820
88#define RCC_RCK4SELR 0x824
89#define RCC_MCUDIVR 0x830
90#define RCC_APB1DIVR 0x834
91#define RCC_APB2DIVR 0x838
92#define RCC_APB3DIVR 0x83C
93#define RCC_PLL3CR 0x880
94#define RCC_PLL3CFGR1 0x884
95#define RCC_PLL3CFGR2 0x888
96#define RCC_PLL3FRACR 0x88C
97#define RCC_PLL3CSGR 0x890
98#define RCC_PLL4CR 0x894
99#define RCC_PLL4CFGR1 0x898
100#define RCC_PLL4CFGR2 0x89C
101#define RCC_PLL4FRACR 0x8A0
102#define RCC_PLL4CSGR 0x8A4
103#define RCC_I2C12CKSELR 0x8C0
104#define RCC_I2C35CKSELR 0x8C4
Patrice Chotard248278d2019-04-30 18:08:27 +0200105#define RCC_SPI2S1CKSELR 0x8D8
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100106#define RCC_SPI45CKSELR 0x8E0
Patrick Delaunaya6151912018-03-12 10:46:15 +0100107#define RCC_UART6CKSELR 0x8E4
108#define RCC_UART24CKSELR 0x8E8
109#define RCC_UART35CKSELR 0x8EC
110#define RCC_UART78CKSELR 0x8F0
111#define RCC_SDMMC12CKSELR 0x8F4
112#define RCC_SDMMC3CKSELR 0x8F8
113#define RCC_ETHCKSELR 0x8FC
114#define RCC_QSPICKSELR 0x900
115#define RCC_FMCCKSELR 0x904
116#define RCC_USBCKSELR 0x91C
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200117#define RCC_DSICKSELR 0x924
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200118#define RCC_ADCCKSELR 0x928
Patrick Delaunaya6151912018-03-12 10:46:15 +0100119#define RCC_MP_APB1ENSETR 0xA00
120#define RCC_MP_APB2ENSETR 0XA08
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200121#define RCC_MP_APB3ENSETR 0xA10
Patrick Delaunaya6151912018-03-12 10:46:15 +0100122#define RCC_MP_AHB2ENSETR 0xA18
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100123#define RCC_MP_AHB3ENSETR 0xA20
Patrick Delaunaya6151912018-03-12 10:46:15 +0100124#define RCC_MP_AHB4ENSETR 0xA28
125
126/* used for most of SELR register */
127#define RCC_SELR_SRC_MASK GENMASK(2, 0)
128#define RCC_SELR_SRCRDY BIT(31)
129
130/* Values of RCC_MPCKSELR register */
131#define RCC_MPCKSELR_HSI 0
132#define RCC_MPCKSELR_HSE 1
133#define RCC_MPCKSELR_PLL 2
134#define RCC_MPCKSELR_PLL_MPUDIV 3
135
136/* Values of RCC_ASSCKSELR register */
137#define RCC_ASSCKSELR_HSI 0
138#define RCC_ASSCKSELR_HSE 1
139#define RCC_ASSCKSELR_PLL 2
140
141/* Values of RCC_MSSCKSELR register */
142#define RCC_MSSCKSELR_HSI 0
143#define RCC_MSSCKSELR_HSE 1
144#define RCC_MSSCKSELR_CSI 2
145#define RCC_MSSCKSELR_PLL 3
146
147/* Values of RCC_CPERCKSELR register */
148#define RCC_CPERCKSELR_HSI 0
149#define RCC_CPERCKSELR_CSI 1
150#define RCC_CPERCKSELR_HSE 2
151
152/* used for most of DIVR register : max div for RTC */
153#define RCC_DIVR_DIV_MASK GENMASK(5, 0)
154#define RCC_DIVR_DIVRDY BIT(31)
155
156/* Masks for specific DIVR registers */
157#define RCC_APBXDIV_MASK GENMASK(2, 0)
158#define RCC_MPUDIV_MASK GENMASK(2, 0)
159#define RCC_AXIDIV_MASK GENMASK(2, 0)
160#define RCC_MCUDIV_MASK GENMASK(3, 0)
161
162/* offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */
163#define RCC_MP_ENCLRR_OFFSET 4
164
165/* Fields of RCC_BDCR register */
166#define RCC_BDCR_LSEON BIT(0)
167#define RCC_BDCR_LSEBYP BIT(1)
168#define RCC_BDCR_LSERDY BIT(2)
Patrick Delaunayd2194152018-07-16 10:41:46 +0200169#define RCC_BDCR_DIGBYP BIT(3)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100170#define RCC_BDCR_LSEDRV_MASK GENMASK(5, 4)
171#define RCC_BDCR_LSEDRV_SHIFT 4
172#define RCC_BDCR_LSECSSON BIT(8)
173#define RCC_BDCR_RTCCKEN BIT(20)
174#define RCC_BDCR_RTCSRC_MASK GENMASK(17, 16)
175#define RCC_BDCR_RTCSRC_SHIFT 16
176
177/* Fields of RCC_RDLSICR register */
178#define RCC_RDLSICR_LSION BIT(0)
179#define RCC_RDLSICR_LSIRDY BIT(1)
180
181/* used for ALL PLLNCR registers */
182#define RCC_PLLNCR_PLLON BIT(0)
183#define RCC_PLLNCR_PLLRDY BIT(1)
Patrick Delaunaybbd108a2019-01-30 13:07:06 +0100184#define RCC_PLLNCR_SSCG_CTRL BIT(2)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100185#define RCC_PLLNCR_DIVPEN BIT(4)
186#define RCC_PLLNCR_DIVQEN BIT(5)
187#define RCC_PLLNCR_DIVREN BIT(6)
188#define RCC_PLLNCR_DIVEN_SHIFT 4
189
190/* used for ALL PLLNCFGR1 registers */
191#define RCC_PLLNCFGR1_DIVM_SHIFT 16
192#define RCC_PLLNCFGR1_DIVM_MASK GENMASK(21, 16)
193#define RCC_PLLNCFGR1_DIVN_SHIFT 0
194#define RCC_PLLNCFGR1_DIVN_MASK GENMASK(8, 0)
195/* only for PLL3 and PLL4 */
196#define RCC_PLLNCFGR1_IFRGE_SHIFT 24
197#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK(25, 24)
198
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200199/* used for ALL PLLNCFGR2 registers , using stm32mp1_div_id */
200#define RCC_PLLNCFGR2_SHIFT(div_id) ((div_id) * 8)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100201#define RCC_PLLNCFGR2_DIVX_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200202#define RCC_PLLNCFGR2_DIVP_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_P)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100203#define RCC_PLLNCFGR2_DIVP_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200204#define RCC_PLLNCFGR2_DIVQ_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_Q)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100205#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK(14, 8)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200206#define RCC_PLLNCFGR2_DIVR_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_R)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100207#define RCC_PLLNCFGR2_DIVR_MASK GENMASK(22, 16)
208
209/* used for ALL PLLNFRACR registers */
210#define RCC_PLLNFRACR_FRACV_SHIFT 3
211#define RCC_PLLNFRACR_FRACV_MASK GENMASK(15, 3)
212#define RCC_PLLNFRACR_FRACLE BIT(16)
213
214/* used for ALL PLLNCSGR registers */
215#define RCC_PLLNCSGR_INC_STEP_SHIFT 16
216#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK(30, 16)
217#define RCC_PLLNCSGR_MOD_PER_SHIFT 0
218#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK(12, 0)
219#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15
220#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15)
221
222/* used for RCC_OCENSETR and RCC_OCENCLRR registers */
223#define RCC_OCENR_HSION BIT(0)
224#define RCC_OCENR_CSION BIT(4)
Patrick Delaunayd2194152018-07-16 10:41:46 +0200225#define RCC_OCENR_DIGBYP BIT(7)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100226#define RCC_OCENR_HSEON BIT(8)
227#define RCC_OCENR_HSEBYP BIT(10)
228#define RCC_OCENR_HSECSSON BIT(11)
229
230/* Fields of RCC_OCRDYR register */
231#define RCC_OCRDYR_HSIRDY BIT(0)
232#define RCC_OCRDYR_HSIDIVRDY BIT(2)
233#define RCC_OCRDYR_CSIRDY BIT(4)
234#define RCC_OCRDYR_HSERDY BIT(8)
235
236/* Fields of DDRITFCR register */
237#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
238#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20
239#define RCC_DDRITFCR_DDRCKMOD_SSR 0
240
241/* Fields of RCC_HSICFGR register */
242#define RCC_HSICFGR_HSIDIV_MASK GENMASK(1, 0)
243
244/* used for MCO related operations */
245#define RCC_MCOCFG_MCOON BIT(12)
246#define RCC_MCOCFG_MCODIV_MASK GENMASK(7, 4)
247#define RCC_MCOCFG_MCODIV_SHIFT 4
248#define RCC_MCOCFG_MCOSRC_MASK GENMASK(2, 0)
249
250enum stm32mp1_parent_id {
251/*
252 * _HSI, _HSE, _CSI, _LSI, _LSE should not be moved
253 * they are used as index in osc[] as entry point
254 */
255 _HSI,
256 _HSE,
257 _CSI,
258 _LSI,
259 _LSE,
260 _I2S_CKIN,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100261 NB_OSC,
262
263/* other parent source */
264 _HSI_KER = NB_OSC,
265 _HSE_KER,
266 _HSE_KER_DIV2,
267 _CSI_KER,
268 _PLL1_P,
269 _PLL1_Q,
270 _PLL1_R,
271 _PLL2_P,
272 _PLL2_Q,
273 _PLL2_R,
274 _PLL3_P,
275 _PLL3_Q,
276 _PLL3_R,
277 _PLL4_P,
278 _PLL4_Q,
279 _PLL4_R,
280 _ACLK,
281 _PCLK1,
282 _PCLK2,
283 _PCLK3,
284 _PCLK4,
285 _PCLK5,
286 _HCLK6,
287 _HCLK2,
288 _CK_PER,
289 _CK_MPU,
290 _CK_MCU,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200291 _DSI_PHY,
Patrick Delaunay86617dd2019-01-30 13:07:00 +0100292 _USB_PHY_48,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100293 _PARENT_NB,
294 _UNKNOWN_ID = 0xff,
295};
296
297enum stm32mp1_parent_sel {
298 _I2C12_SEL,
299 _I2C35_SEL,
300 _I2C46_SEL,
301 _UART6_SEL,
302 _UART24_SEL,
303 _UART35_SEL,
304 _UART78_SEL,
305 _SDMMC12_SEL,
306 _SDMMC3_SEL,
307 _ETH_SEL,
308 _QSPI_SEL,
309 _FMC_SEL,
310 _USBPHY_SEL,
311 _USBO_SEL,
312 _STGEN_SEL,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200313 _DSI_SEL,
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200314 _ADC12_SEL,
Patrice Chotard248278d2019-04-30 18:08:27 +0200315 _SPI1_SEL,
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100316 _SPI45_SEL,
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200317 _RTC_SEL,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100318 _PARENT_SEL_NB,
319 _UNKNOWN_SEL = 0xff,
320};
321
322enum stm32mp1_pll_id {
323 _PLL1,
324 _PLL2,
325 _PLL3,
326 _PLL4,
327 _PLL_NB
328};
329
330enum stm32mp1_div_id {
331 _DIV_P,
332 _DIV_Q,
333 _DIV_R,
334 _DIV_NB,
335};
336
337enum stm32mp1_clksrc_id {
338 CLKSRC_MPU,
339 CLKSRC_AXI,
340 CLKSRC_MCU,
341 CLKSRC_PLL12,
342 CLKSRC_PLL3,
343 CLKSRC_PLL4,
344 CLKSRC_RTC,
345 CLKSRC_MCO1,
346 CLKSRC_MCO2,
347 CLKSRC_NB
348};
349
350enum stm32mp1_clkdiv_id {
351 CLKDIV_MPU,
352 CLKDIV_AXI,
353 CLKDIV_MCU,
354 CLKDIV_APB1,
355 CLKDIV_APB2,
356 CLKDIV_APB3,
357 CLKDIV_APB4,
358 CLKDIV_APB5,
359 CLKDIV_RTC,
360 CLKDIV_MCO1,
361 CLKDIV_MCO2,
362 CLKDIV_NB
363};
364
365enum stm32mp1_pllcfg {
366 PLLCFG_M,
367 PLLCFG_N,
368 PLLCFG_P,
369 PLLCFG_Q,
370 PLLCFG_R,
371 PLLCFG_O,
372 PLLCFG_NB
373};
374
375enum stm32mp1_pllcsg {
376 PLLCSG_MOD_PER,
377 PLLCSG_INC_STEP,
378 PLLCSG_SSCG_MODE,
379 PLLCSG_NB
380};
381
382enum stm32mp1_plltype {
383 PLL_800,
384 PLL_1600,
385 PLL_TYPE_NB
386};
387
388struct stm32mp1_pll {
389 u8 refclk_min;
390 u8 refclk_max;
391 u8 divn_max;
392};
393
394struct stm32mp1_clk_gate {
395 u16 offset;
396 u8 bit;
397 u8 index;
398 u8 set_clr;
399 u8 sel;
400 u8 fixed;
401};
402
403struct stm32mp1_clk_sel {
404 u16 offset;
405 u8 src;
406 u8 msk;
407 u8 nb_parent;
408 const u8 *parent;
409};
410
411#define REFCLK_SIZE 4
412struct stm32mp1_clk_pll {
413 enum stm32mp1_plltype plltype;
414 u16 rckxselr;
415 u16 pllxcfgr1;
416 u16 pllxcfgr2;
417 u16 pllxfracr;
418 u16 pllxcr;
419 u16 pllxcsgr;
420 u8 refclk[REFCLK_SIZE];
421};
422
423struct stm32mp1_clk_data {
424 const struct stm32mp1_clk_gate *gate;
425 const struct stm32mp1_clk_sel *sel;
426 const struct stm32mp1_clk_pll *pll;
427 const int nb_gate;
428};
429
430struct stm32mp1_clk_priv {
431 fdt_addr_t base;
432 const struct stm32mp1_clk_data *data;
433 ulong osc[NB_OSC];
434 struct udevice *osc_dev[NB_OSC];
435};
436
437#define STM32MP1_CLK(off, b, idx, s) \
438 { \
439 .offset = (off), \
440 .bit = (b), \
441 .index = (idx), \
442 .set_clr = 0, \
443 .sel = (s), \
444 .fixed = _UNKNOWN_ID, \
445 }
446
447#define STM32MP1_CLK_F(off, b, idx, f) \
448 { \
449 .offset = (off), \
450 .bit = (b), \
451 .index = (idx), \
452 .set_clr = 0, \
453 .sel = _UNKNOWN_SEL, \
454 .fixed = (f), \
455 }
456
457#define STM32MP1_CLK_SET_CLR(off, b, idx, s) \
458 { \
459 .offset = (off), \
460 .bit = (b), \
461 .index = (idx), \
462 .set_clr = 1, \
463 .sel = (s), \
464 .fixed = _UNKNOWN_ID, \
465 }
466
467#define STM32MP1_CLK_SET_CLR_F(off, b, idx, f) \
468 { \
469 .offset = (off), \
470 .bit = (b), \
471 .index = (idx), \
472 .set_clr = 1, \
473 .sel = _UNKNOWN_SEL, \
474 .fixed = (f), \
475 }
476
477#define STM32MP1_CLK_PARENT(idx, off, s, m, p) \
478 [(idx)] = { \
479 .offset = (off), \
480 .src = (s), \
481 .msk = (m), \
482 .parent = (p), \
483 .nb_parent = ARRAY_SIZE((p)) \
484 }
485
486#define STM32MP1_CLK_PLL(idx, type, off1, off2, off3, off4, off5, off6,\
487 p1, p2, p3, p4) \
488 [(idx)] = { \
489 .plltype = (type), \
490 .rckxselr = (off1), \
491 .pllxcfgr1 = (off2), \
492 .pllxcfgr2 = (off3), \
493 .pllxfracr = (off4), \
494 .pllxcr = (off5), \
495 .pllxcsgr = (off6), \
496 .refclk[0] = (p1), \
497 .refclk[1] = (p2), \
498 .refclk[2] = (p3), \
499 .refclk[3] = (p4), \
500 }
501
502static const u8 stm32mp1_clks[][2] = {
503 {CK_PER, _CK_PER},
504 {CK_MPU, _CK_MPU},
505 {CK_AXI, _ACLK},
506 {CK_MCU, _CK_MCU},
507 {CK_HSE, _HSE},
508 {CK_CSI, _CSI},
509 {CK_LSI, _LSI},
510 {CK_LSE, _LSE},
511 {CK_HSI, _HSI},
512 {CK_HSE_DIV2, _HSE_KER_DIV2},
513};
514
515static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
516 STM32MP1_CLK(RCC_DDRITFCR, 0, DDRC1, _UNKNOWN_SEL),
517 STM32MP1_CLK(RCC_DDRITFCR, 1, DDRC1LP, _UNKNOWN_SEL),
518 STM32MP1_CLK(RCC_DDRITFCR, 2, DDRC2, _UNKNOWN_SEL),
519 STM32MP1_CLK(RCC_DDRITFCR, 3, DDRC2LP, _UNKNOWN_SEL),
520 STM32MP1_CLK_F(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
521 STM32MP1_CLK(RCC_DDRITFCR, 5, DDRPHYCLP, _UNKNOWN_SEL),
522 STM32MP1_CLK(RCC_DDRITFCR, 6, DDRCAPB, _UNKNOWN_SEL),
523 STM32MP1_CLK(RCC_DDRITFCR, 7, DDRCAPBLP, _UNKNOWN_SEL),
524 STM32MP1_CLK(RCC_DDRITFCR, 8, AXIDCG, _UNKNOWN_SEL),
525 STM32MP1_CLK(RCC_DDRITFCR, 9, DDRPHYCAPB, _UNKNOWN_SEL),
526 STM32MP1_CLK(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _UNKNOWN_SEL),
527
528 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
529 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
530 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
531 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
532 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
533 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
534 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
535 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
536 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
537 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
538
Patrice Chotard248278d2019-04-30 18:08:27 +0200539 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 8, SPI1_K, _SPI1_SEL),
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100540 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 10, SPI5_K, _SPI45_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100541 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
542
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200543 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
544
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200545 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 0, LTDC_PX, _PLL4_Q),
546 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 4, DSI_PX, _PLL4_Q),
547 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 4, DSI_K, _DSI_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100548 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
549 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
550 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
551
552 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
Patrick Delaunay789d7642021-01-22 15:34:25 +0100553 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200554 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100555 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
556
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200557 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB2ENSETR, 5, ADC12, _HCLK2),
558 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 5, ADC12_K, _ADC12_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100559 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
560 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
561
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100562 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 11, HSEM, _UNKNOWN_SEL),
Patrick Delaunayd661f612019-01-30 13:07:01 +0100563 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 12, IPCC, _UNKNOWN_SEL),
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100564
Patrick Delaunaya6151912018-03-12 10:46:15 +0100565 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
566 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
567 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
568 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
569 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
570 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
571 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
572 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
573 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
574 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
575 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
576
577 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
Sughosh Ganu82ebf0f2019-12-28 23:58:28 +0530578 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 6, RNG1_K, _UNKNOWN_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100579
Patrick Delaunayf6ccdda2019-05-17 15:08:42 +0200580 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK_K, _ETH_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100581 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
582 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100583 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
584 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
585 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
586 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
587 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
588 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
589
590 STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200591
592 STM32MP1_CLK(RCC_BDCR, 20, RTC, _RTC_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100593};
594
595static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
596static const u8 i2c35_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
597static const u8 i2c46_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER};
598static const u8 uart6_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
599 _HSE_KER};
600static const u8 uart24_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
601 _HSE_KER};
602static const u8 uart35_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
603 _HSE_KER};
604static const u8 uart78_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
605 _HSE_KER};
606static const u8 sdmmc12_parents[] = {_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER};
607static const u8 sdmmc3_parents[] = {_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER};
608static const u8 eth_parents[] = {_PLL4_P, _PLL3_Q};
609static const u8 qspi_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
610static const u8 fmc_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
611static const u8 usbphy_parents[] = {_HSE_KER, _PLL4_R, _HSE_KER_DIV2};
612static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
613static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200614static const u8 dsi_parents[] = {_DSI_PHY, _PLL4_P};
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200615static const u8 adc_parents[] = {_PLL4_R, _CK_PER, _PLL3_Q};
Patrice Chotard248278d2019-04-30 18:08:27 +0200616static const u8 spi_parents[] = {_PLL4_P, _PLL3_Q, _I2S_CKIN, _CK_PER,
617 _PLL3_R};
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100618static const u8 spi45_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
619 _HSE_KER};
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200620static const u8 rtc_parents[] = {_UNKNOWN_ID, _LSE, _LSI, _HSE};
Patrick Delaunaya6151912018-03-12 10:46:15 +0100621
622static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
623 STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
624 STM32MP1_CLK_PARENT(_I2C35_SEL, RCC_I2C35CKSELR, 0, 0x7, i2c35_parents),
625 STM32MP1_CLK_PARENT(_I2C46_SEL, RCC_I2C46CKSELR, 0, 0x7, i2c46_parents),
626 STM32MP1_CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents),
627 STM32MP1_CLK_PARENT(_UART24_SEL, RCC_UART24CKSELR, 0, 0x7,
628 uart24_parents),
629 STM32MP1_CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7,
630 uart35_parents),
631 STM32MP1_CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7,
632 uart78_parents),
633 STM32MP1_CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7,
634 sdmmc12_parents),
635 STM32MP1_CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7,
636 sdmmc3_parents),
637 STM32MP1_CLK_PARENT(_ETH_SEL, RCC_ETHCKSELR, 0, 0x3, eth_parents),
Patrick Delaunay69ffb552020-03-09 14:59:22 +0100638 STM32MP1_CLK_PARENT(_QSPI_SEL, RCC_QSPICKSELR, 0, 0x3, qspi_parents),
639 STM32MP1_CLK_PARENT(_FMC_SEL, RCC_FMCCKSELR, 0, 0x3, fmc_parents),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100640 STM32MP1_CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents),
641 STM32MP1_CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents),
642 STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200643 STM32MP1_CLK_PARENT(_DSI_SEL, RCC_DSICKSELR, 0, 0x1, dsi_parents),
Patrick Delaunay69ffb552020-03-09 14:59:22 +0100644 STM32MP1_CLK_PARENT(_ADC12_SEL, RCC_ADCCKSELR, 0, 0x3, adc_parents),
Patrice Chotard248278d2019-04-30 18:08:27 +0200645 STM32MP1_CLK_PARENT(_SPI1_SEL, RCC_SPI2S1CKSELR, 0, 0x7, spi_parents),
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100646 STM32MP1_CLK_PARENT(_SPI45_SEL, RCC_SPI45CKSELR, 0, 0x7, spi45_parents),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200647 STM32MP1_CLK_PARENT(_RTC_SEL, RCC_BDCR, RCC_BDCR_RTCSRC_SHIFT,
648 (RCC_BDCR_RTCSRC_MASK >> RCC_BDCR_RTCSRC_SHIFT),
649 rtc_parents),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100650};
651
652#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay37ad8372020-05-25 12:19:44 +0200653
Patrick Delaunaya6151912018-03-12 10:46:15 +0100654/* define characteristic of PLL according type */
Patrick Delaunay37ad8372020-05-25 12:19:44 +0200655#define DIVM_MIN 0
656#define DIVM_MAX 63
Patrick Delaunaya6151912018-03-12 10:46:15 +0100657#define DIVN_MIN 24
Patrick Delaunay37ad8372020-05-25 12:19:44 +0200658#define DIVP_MIN 0
659#define DIVP_MAX 127
660#define FRAC_MAX 8192
661
662#define PLL1600_VCO_MIN 800000000
663#define PLL1600_VCO_MAX 1600000000
664
Patrick Delaunaya6151912018-03-12 10:46:15 +0100665static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
666 [PLL_800] = {
667 .refclk_min = 4,
668 .refclk_max = 16,
669 .divn_max = 99,
670 },
671 [PLL_1600] = {
672 .refclk_min = 8,
673 .refclk_max = 16,
674 .divn_max = 199,
675 },
676};
677#endif /* STM32MP1_CLOCK_TREE_INIT */
678
679static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
680 STM32MP1_CLK_PLL(_PLL1, PLL_1600,
681 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
682 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
683 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
684 STM32MP1_CLK_PLL(_PLL2, PLL_1600,
685 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
686 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
687 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
688 STM32MP1_CLK_PLL(_PLL3, PLL_800,
689 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
690 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
691 _HSI, _HSE, _CSI, _UNKNOWN_ID),
692 STM32MP1_CLK_PLL(_PLL4, PLL_800,
693 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
694 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
695 _HSI, _HSE, _CSI, _I2S_CKIN),
696};
697
698/* Prescaler table lookups for clock computation */
699/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
700static const u8 stm32mp1_mcu_div[16] = {
701 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
702};
703
704/* div = /1 /2 /4 /8 /16 : same divider for pmu and apbx*/
705#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
706#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
707static const u8 stm32mp1_mpu_apbx_div[8] = {
708 0, 1, 2, 3, 4, 4, 4, 4
709};
710
711/* div = /1 /2 /3 /4 */
712static const u8 stm32mp1_axi_div[8] = {
713 1, 2, 3, 4, 4, 4, 4, 4
714};
715
Patrick Delaunay8d6310a2019-01-30 13:07:04 +0100716static const __maybe_unused
717char * const stm32mp1_clk_parent_name[_PARENT_NB] = {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100718 [_HSI] = "HSI",
719 [_HSE] = "HSE",
720 [_CSI] = "CSI",
721 [_LSI] = "LSI",
722 [_LSE] = "LSE",
723 [_I2S_CKIN] = "I2S_CKIN",
724 [_HSI_KER] = "HSI_KER",
725 [_HSE_KER] = "HSE_KER",
726 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
727 [_CSI_KER] = "CSI_KER",
728 [_PLL1_P] = "PLL1_P",
729 [_PLL1_Q] = "PLL1_Q",
730 [_PLL1_R] = "PLL1_R",
731 [_PLL2_P] = "PLL2_P",
732 [_PLL2_Q] = "PLL2_Q",
733 [_PLL2_R] = "PLL2_R",
734 [_PLL3_P] = "PLL3_P",
735 [_PLL3_Q] = "PLL3_Q",
736 [_PLL3_R] = "PLL3_R",
737 [_PLL4_P] = "PLL4_P",
738 [_PLL4_Q] = "PLL4_Q",
739 [_PLL4_R] = "PLL4_R",
740 [_ACLK] = "ACLK",
741 [_PCLK1] = "PCLK1",
742 [_PCLK2] = "PCLK2",
743 [_PCLK3] = "PCLK3",
744 [_PCLK4] = "PCLK4",
745 [_PCLK5] = "PCLK5",
746 [_HCLK6] = "KCLK6",
747 [_HCLK2] = "HCLK2",
748 [_CK_PER] = "CK_PER",
749 [_CK_MPU] = "CK_MPU",
750 [_CK_MCU] = "CK_MCU",
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200751 [_USB_PHY_48] = "USB_PHY_48",
752 [_DSI_PHY] = "DSI_PHY_PLL",
Patrick Delaunaya6151912018-03-12 10:46:15 +0100753};
754
Patrick Delaunay8d6310a2019-01-30 13:07:04 +0100755static const __maybe_unused
756char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100757 [_I2C12_SEL] = "I2C12",
758 [_I2C35_SEL] = "I2C35",
759 [_I2C46_SEL] = "I2C46",
760 [_UART6_SEL] = "UART6",
761 [_UART24_SEL] = "UART24",
762 [_UART35_SEL] = "UART35",
763 [_UART78_SEL] = "UART78",
764 [_SDMMC12_SEL] = "SDMMC12",
765 [_SDMMC3_SEL] = "SDMMC3",
766 [_ETH_SEL] = "ETH",
767 [_QSPI_SEL] = "QSPI",
768 [_FMC_SEL] = "FMC",
769 [_USBPHY_SEL] = "USBPHY",
770 [_USBO_SEL] = "USBO",
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200771 [_STGEN_SEL] = "STGEN",
772 [_DSI_SEL] = "DSI",
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200773 [_ADC12_SEL] = "ADC12",
Patrice Chotard248278d2019-04-30 18:08:27 +0200774 [_SPI1_SEL] = "SPI1",
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100775 [_SPI45_SEL] = "SPI45",
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200776 [_RTC_SEL] = "RTC",
Patrick Delaunaya6151912018-03-12 10:46:15 +0100777};
Patrick Delaunaya6151912018-03-12 10:46:15 +0100778
779static const struct stm32mp1_clk_data stm32mp1_data = {
780 .gate = stm32mp1_clk_gate,
781 .sel = stm32mp1_clk_sel,
782 .pll = stm32mp1_clk_pll,
783 .nb_gate = ARRAY_SIZE(stm32mp1_clk_gate),
784};
785
786static ulong stm32mp1_clk_get_fixed(struct stm32mp1_clk_priv *priv, int idx)
787{
788 if (idx >= NB_OSC) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100789 log_debug("clk id %d not found\n", idx);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100790 return 0;
791 }
792
Patrick Delaunaya6151912018-03-12 10:46:15 +0100793 return priv->osc[idx];
794}
795
796static int stm32mp1_clk_get_id(struct stm32mp1_clk_priv *priv, unsigned long id)
797{
798 const struct stm32mp1_clk_gate *gate = priv->data->gate;
799 int i, nb_clks = priv->data->nb_gate;
800
801 for (i = 0; i < nb_clks; i++) {
802 if (gate[i].index == id)
803 break;
804 }
805
806 if (i == nb_clks) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100807 log_err("clk id %d not found\n", (u32)id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100808 return -EINVAL;
809 }
810
811 return i;
812}
813
814static int stm32mp1_clk_get_sel(struct stm32mp1_clk_priv *priv,
815 int i)
816{
817 const struct stm32mp1_clk_gate *gate = priv->data->gate;
818
819 if (gate[i].sel > _PARENT_SEL_NB) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100820 log_err("parents for clk id %d not found\n", i);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100821 return -EINVAL;
822 }
823
824 return gate[i].sel;
825}
826
827static int stm32mp1_clk_get_fixed_parent(struct stm32mp1_clk_priv *priv,
828 int i)
829{
830 const struct stm32mp1_clk_gate *gate = priv->data->gate;
831
832 if (gate[i].fixed == _UNKNOWN_ID)
833 return -ENOENT;
834
835 return gate[i].fixed;
836}
837
838static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
839 unsigned long id)
840{
841 const struct stm32mp1_clk_sel *sel = priv->data->sel;
842 int i;
843 int s, p;
Patrick Delaunay67d74ce2019-06-21 15:26:48 +0200844 unsigned int idx;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100845
Patrick Delaunay67d74ce2019-06-21 15:26:48 +0200846 for (idx = 0; idx < ARRAY_SIZE(stm32mp1_clks); idx++)
847 if (stm32mp1_clks[idx][0] == id)
848 return stm32mp1_clks[idx][1];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100849
850 i = stm32mp1_clk_get_id(priv, id);
851 if (i < 0)
852 return i;
853
854 p = stm32mp1_clk_get_fixed_parent(priv, i);
855 if (p >= 0 && p < _PARENT_NB)
856 return p;
857
858 s = stm32mp1_clk_get_sel(priv, i);
859 if (s < 0)
860 return s;
861
862 p = (readl(priv->base + sel[s].offset) >> sel[s].src) & sel[s].msk;
863
864 if (p < sel[s].nb_parent) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100865 log_content("%s clock is the parent %s of clk id %d\n",
866 stm32mp1_clk_parent_name[sel[s].parent[p]],
867 stm32mp1_clk_parent_sel_name[s],
868 (u32)id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100869 return sel[s].parent[p];
870 }
871
Patrick Delaunayceab8ee2020-11-06 19:01:45 +0100872 log_err("no parents defined for clk id %d\n", (u32)id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100873
874 return -EINVAL;
875}
876
Patrick Delaunay61105032018-07-16 10:41:42 +0200877static ulong pll_get_fref_ck(struct stm32mp1_clk_priv *priv,
878 int pll_id)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100879{
880 const struct stm32mp1_clk_pll *pll = priv->data->pll;
Patrick Delaunay61105032018-07-16 10:41:42 +0200881 u32 selr;
882 int src;
883 ulong refclk;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100884
Patrick Delaunay61105032018-07-16 10:41:42 +0200885 /* Get current refclk */
Patrick Delaunaya6151912018-03-12 10:46:15 +0100886 selr = readl(priv->base + pll[pll_id].rckxselr);
Patrick Delaunay61105032018-07-16 10:41:42 +0200887 src = selr & RCC_SELR_SRC_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100888
Patrick Delaunay61105032018-07-16 10:41:42 +0200889 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]);
Patrick Delaunay61105032018-07-16 10:41:42 +0200890
891 return refclk;
892}
893
894/*
895 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
896 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
897 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
898 * => in all the case Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
899 */
900static ulong pll_get_fvco(struct stm32mp1_clk_priv *priv,
901 int pll_id)
902{
903 const struct stm32mp1_clk_pll *pll = priv->data->pll;
904 int divm, divn;
905 ulong refclk, fvco;
906 u32 cfgr1, fracr;
907
908 cfgr1 = readl(priv->base + pll[pll_id].pllxcfgr1);
909 fracr = readl(priv->base + pll[pll_id].pllxfracr);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100910
911 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
912 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100913
Patrick Delaunay61105032018-07-16 10:41:42 +0200914 refclk = pll_get_fref_ck(priv, pll_id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100915
Patrick Delaunay61105032018-07-16 10:41:42 +0200916 /* with FRACV :
917 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100918 * without FRACV
Patrick Delaunay61105032018-07-16 10:41:42 +0200919 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100920 */
921 if (fracr & RCC_PLLNFRACR_FRACLE) {
922 u32 fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK)
923 >> RCC_PLLNFRACR_FRACV_SHIFT;
Patrick Delaunay61105032018-07-16 10:41:42 +0200924 fvco = (ulong)lldiv((unsigned long long)refclk *
Patrick Delaunaya6151912018-03-12 10:46:15 +0100925 (((divn + 1) << 13) + fracv),
Patrick Delaunay61105032018-07-16 10:41:42 +0200926 ((unsigned long long)(divm + 1)) << 13);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100927 } else {
Patrick Delaunay61105032018-07-16 10:41:42 +0200928 fvco = (ulong)(refclk * (divn + 1) / (divm + 1));
Patrick Delaunaya6151912018-03-12 10:46:15 +0100929 }
Patrick Delaunay61105032018-07-16 10:41:42 +0200930
931 return fvco;
932}
933
934static ulong stm32mp1_read_pll_freq(struct stm32mp1_clk_priv *priv,
935 int pll_id, int div_id)
936{
937 const struct stm32mp1_clk_pll *pll = priv->data->pll;
938 int divy;
939 ulong dfout;
940 u32 cfgr2;
941
Patrick Delaunay61105032018-07-16 10:41:42 +0200942 if (div_id >= _DIV_NB)
943 return 0;
944
945 cfgr2 = readl(priv->base + pll[pll_id].pllxcfgr2);
946 divy = (cfgr2 >> RCC_PLLNCFGR2_SHIFT(div_id)) & RCC_PLLNCFGR2_DIVX_MASK;
947
Patrick Delaunay61105032018-07-16 10:41:42 +0200948 dfout = pll_get_fvco(priv, pll_id) / (divy + 1);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100949
950 return dfout;
951}
952
953static ulong stm32mp1_clk_get(struct stm32mp1_clk_priv *priv, int p)
954{
955 u32 reg;
956 ulong clock = 0;
957
958 switch (p) {
959 case _CK_MPU:
960 /* MPU sub system */
961 reg = readl(priv->base + RCC_MPCKSELR);
962 switch (reg & RCC_SELR_SRC_MASK) {
963 case RCC_MPCKSELR_HSI:
964 clock = stm32mp1_clk_get_fixed(priv, _HSI);
965 break;
966 case RCC_MPCKSELR_HSE:
967 clock = stm32mp1_clk_get_fixed(priv, _HSE);
968 break;
969 case RCC_MPCKSELR_PLL:
970 case RCC_MPCKSELR_PLL_MPUDIV:
971 clock = stm32mp1_read_pll_freq(priv, _PLL1, _DIV_P);
Lionel Debieve36911fc2020-04-24 15:47:57 +0200972 if ((reg & RCC_SELR_SRC_MASK) ==
973 RCC_MPCKSELR_PLL_MPUDIV) {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100974 reg = readl(priv->base + RCC_MPCKDIVR);
Lionel Debieve36911fc2020-04-24 15:47:57 +0200975 clock >>= stm32mp1_mpu_div[reg &
976 RCC_MPUDIV_MASK];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100977 }
978 break;
979 }
980 break;
981 /* AXI sub system */
982 case _ACLK:
983 case _HCLK2:
984 case _HCLK6:
985 case _PCLK4:
986 case _PCLK5:
987 reg = readl(priv->base + RCC_ASSCKSELR);
988 switch (reg & RCC_SELR_SRC_MASK) {
989 case RCC_ASSCKSELR_HSI:
990 clock = stm32mp1_clk_get_fixed(priv, _HSI);
991 break;
992 case RCC_ASSCKSELR_HSE:
993 clock = stm32mp1_clk_get_fixed(priv, _HSE);
994 break;
995 case RCC_ASSCKSELR_PLL:
996 clock = stm32mp1_read_pll_freq(priv, _PLL2, _DIV_P);
997 break;
998 }
999
1000 /* System clock divider */
1001 reg = readl(priv->base + RCC_AXIDIVR);
1002 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
1003
1004 switch (p) {
1005 case _PCLK4:
1006 reg = readl(priv->base + RCC_APB4DIVR);
1007 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1008 break;
1009 case _PCLK5:
1010 reg = readl(priv->base + RCC_APB5DIVR);
1011 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1012 break;
1013 default:
1014 break;
1015 }
1016 break;
1017 /* MCU sub system */
1018 case _CK_MCU:
1019 case _PCLK1:
1020 case _PCLK2:
1021 case _PCLK3:
1022 reg = readl(priv->base + RCC_MSSCKSELR);
1023 switch (reg & RCC_SELR_SRC_MASK) {
1024 case RCC_MSSCKSELR_HSI:
1025 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1026 break;
1027 case RCC_MSSCKSELR_HSE:
1028 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1029 break;
1030 case RCC_MSSCKSELR_CSI:
1031 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1032 break;
1033 case RCC_MSSCKSELR_PLL:
1034 clock = stm32mp1_read_pll_freq(priv, _PLL3, _DIV_P);
1035 break;
1036 }
1037
1038 /* MCU clock divider */
1039 reg = readl(priv->base + RCC_MCUDIVR);
1040 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
1041
1042 switch (p) {
1043 case _PCLK1:
1044 reg = readl(priv->base + RCC_APB1DIVR);
1045 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1046 break;
1047 case _PCLK2:
1048 reg = readl(priv->base + RCC_APB2DIVR);
1049 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1050 break;
1051 case _PCLK3:
1052 reg = readl(priv->base + RCC_APB3DIVR);
1053 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1054 break;
1055 case _CK_MCU:
1056 default:
1057 break;
1058 }
1059 break;
1060 case _CK_PER:
1061 reg = readl(priv->base + RCC_CPERCKSELR);
1062 switch (reg & RCC_SELR_SRC_MASK) {
1063 case RCC_CPERCKSELR_HSI:
1064 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1065 break;
1066 case RCC_CPERCKSELR_HSE:
1067 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1068 break;
1069 case RCC_CPERCKSELR_CSI:
1070 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1071 break;
1072 }
1073 break;
1074 case _HSI:
1075 case _HSI_KER:
1076 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1077 break;
1078 case _CSI:
1079 case _CSI_KER:
1080 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1081 break;
1082 case _HSE:
1083 case _HSE_KER:
1084 case _HSE_KER_DIV2:
1085 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1086 if (p == _HSE_KER_DIV2)
1087 clock >>= 1;
1088 break;
1089 case _LSI:
1090 clock = stm32mp1_clk_get_fixed(priv, _LSI);
1091 break;
1092 case _LSE:
1093 clock = stm32mp1_clk_get_fixed(priv, _LSE);
1094 break;
1095 /* PLL */
1096 case _PLL1_P:
1097 case _PLL1_Q:
1098 case _PLL1_R:
1099 clock = stm32mp1_read_pll_freq(priv, _PLL1, p - _PLL1_P);
1100 break;
1101 case _PLL2_P:
1102 case _PLL2_Q:
1103 case _PLL2_R:
1104 clock = stm32mp1_read_pll_freq(priv, _PLL2, p - _PLL2_P);
1105 break;
1106 case _PLL3_P:
1107 case _PLL3_Q:
1108 case _PLL3_R:
1109 clock = stm32mp1_read_pll_freq(priv, _PLL3, p - _PLL3_P);
1110 break;
1111 case _PLL4_P:
1112 case _PLL4_Q:
1113 case _PLL4_R:
1114 clock = stm32mp1_read_pll_freq(priv, _PLL4, p - _PLL4_P);
1115 break;
1116 /* other */
1117 case _USB_PHY_48:
Patrick Delaunay86617dd2019-01-30 13:07:00 +01001118 clock = 48000000;
Patrick Delaunaya6151912018-03-12 10:46:15 +01001119 break;
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001120 case _DSI_PHY:
1121 {
1122 struct clk clk;
1123 struct udevice *dev = NULL;
Patrick Delaunaya6151912018-03-12 10:46:15 +01001124
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001125 if (!uclass_get_device_by_name(UCLASS_CLK, "ck_dsi_phy",
1126 &dev)) {
1127 if (clk_request(dev, &clk)) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001128 log_err("ck_dsi_phy request");
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001129 } else {
1130 clk.id = 0;
1131 clock = clk_get_rate(&clk);
1132 }
1133 }
1134 break;
1135 }
Patrick Delaunaya6151912018-03-12 10:46:15 +01001136 default:
1137 break;
1138 }
1139
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001140 log_debug("id=%d clock = %lx : %ld kHz\n", p, clock, clock / 1000);
Patrick Delaunaya6151912018-03-12 10:46:15 +01001141
1142 return clock;
1143}
1144
1145static int stm32mp1_clk_enable(struct clk *clk)
1146{
1147 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1148 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1149 int i = stm32mp1_clk_get_id(priv, clk->id);
1150
1151 if (i < 0)
1152 return i;
1153
1154 if (gate[i].set_clr)
1155 writel(BIT(gate[i].bit), priv->base + gate[i].offset);
1156 else
1157 setbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1158
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001159 dev_dbg(clk->dev, "%s: id clock %d has been enabled\n", __func__, (u32)clk->id);
Patrick Delaunaya6151912018-03-12 10:46:15 +01001160
1161 return 0;
1162}
1163
1164static int stm32mp1_clk_disable(struct clk *clk)
1165{
1166 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1167 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1168 int i = stm32mp1_clk_get_id(priv, clk->id);
1169
1170 if (i < 0)
1171 return i;
1172
1173 if (gate[i].set_clr)
1174 writel(BIT(gate[i].bit),
1175 priv->base + gate[i].offset
1176 + RCC_MP_ENCLRR_OFFSET);
1177 else
1178 clrbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1179
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001180 dev_dbg(clk->dev, "%s: id clock %d has been disabled\n", __func__, (u32)clk->id);
Patrick Delaunaya6151912018-03-12 10:46:15 +01001181
1182 return 0;
1183}
1184
1185static ulong stm32mp1_clk_get_rate(struct clk *clk)
1186{
1187 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1188 int p = stm32mp1_clk_get_parent(priv, clk->id);
1189 ulong rate;
1190
1191 if (p < 0)
1192 return 0;
1193
1194 rate = stm32mp1_clk_get(priv, p);
1195
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001196 dev_vdbg(clk->dev, "computed rate for id clock %d is %d (parent is %s)\n",
1197 (u32)clk->id, (u32)rate, stm32mp1_clk_parent_name[p]);
1198
Patrick Delaunaya6151912018-03-12 10:46:15 +01001199 return rate;
1200}
1201
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001202#ifdef STM32MP1_CLOCK_TREE_INIT
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001203
1204bool stm32mp1_supports_opp(u32 opp_id, u32 cpu_type)
1205{
1206 unsigned int id;
1207
1208 switch (opp_id) {
1209 case 1:
1210 case 2:
1211 id = opp_id;
1212 break;
1213 default:
1214 id = 1; /* default value */
1215 break;
1216 }
1217
1218 switch (cpu_type) {
1219 case CPU_STM32MP157Fxx:
1220 case CPU_STM32MP157Dxx:
1221 case CPU_STM32MP153Fxx:
1222 case CPU_STM32MP153Dxx:
1223 case CPU_STM32MP151Fxx:
1224 case CPU_STM32MP151Dxx:
1225 return true;
1226 default:
1227 return id == 1;
1228 }
1229}
1230
Patrick Delaunay4e626422020-05-25 12:19:45 +02001231__weak void board_vddcore_init(u32 voltage_mv)
1232{
1233}
1234
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001235/*
1236 * gets OPP parameters (frequency in KHz and voltage in mV) from
1237 * an OPP table subnode. Platform HW support capabilities are also checked.
1238 * Returns 0 on success and a negative FDT error code on failure.
1239 */
1240static int stm32mp1_get_opp(u32 cpu_type, ofnode subnode,
1241 u32 *freq_khz, u32 *voltage_mv)
1242{
1243 u32 opp_hw;
1244 u64 read_freq_64;
1245 u32 read_voltage_32;
1246
1247 *freq_khz = 0;
1248 *voltage_mv = 0;
1249
1250 opp_hw = ofnode_read_u32_default(subnode, "opp-supported-hw", 0);
1251 if (opp_hw)
1252 if (!stm32mp1_supports_opp(opp_hw, cpu_type))
1253 return -FDT_ERR_BADVALUE;
1254
1255 read_freq_64 = ofnode_read_u64_default(subnode, "opp-hz", 0) /
1256 1000ULL;
1257 read_voltage_32 = ofnode_read_u32_default(subnode, "opp-microvolt", 0) /
1258 1000U;
1259
1260 if (!read_voltage_32 || !read_freq_64)
1261 return -FDT_ERR_NOTFOUND;
1262
1263 /* Frequency value expressed in KHz must fit on 32 bits */
1264 if (read_freq_64 > U32_MAX)
1265 return -FDT_ERR_BADVALUE;
1266
1267 /* Millivolt value must fit on 16 bits */
1268 if (read_voltage_32 > U16_MAX)
1269 return -FDT_ERR_BADVALUE;
1270
1271 *freq_khz = (u32)read_freq_64;
1272 *voltage_mv = read_voltage_32;
1273
1274 return 0;
1275}
1276
1277/*
1278 * parses OPP table in DT and finds the parameters for the
1279 * highest frequency supported by the HW platform.
1280 * Returns 0 on success and a negative FDT error code on failure.
1281 */
1282int stm32mp1_get_max_opp_freq(struct stm32mp1_clk_priv *priv, u64 *freq_hz)
1283{
1284 ofnode node, subnode;
1285 int ret;
1286 u32 freq = 0U, voltage = 0U;
1287 u32 cpu_type = get_cpu_type();
1288
1289 node = ofnode_by_compatible(ofnode_null(), "operating-points-v2");
1290 if (!ofnode_valid(node))
1291 return -FDT_ERR_NOTFOUND;
1292
1293 ofnode_for_each_subnode(subnode, node) {
1294 unsigned int read_freq;
1295 unsigned int read_voltage;
1296
1297 ret = stm32mp1_get_opp(cpu_type, subnode,
1298 &read_freq, &read_voltage);
1299 if (ret)
1300 continue;
1301
1302 if (read_freq > freq) {
1303 freq = read_freq;
1304 voltage = read_voltage;
1305 }
1306 }
1307
1308 if (!freq || !voltage)
1309 return -FDT_ERR_NOTFOUND;
1310
1311 *freq_hz = (u64)1000U * freq;
Patrick Delaunay4e626422020-05-25 12:19:45 +02001312 board_vddcore_init(voltage);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001313
1314 return 0;
1315}
1316
1317static int stm32mp1_pll1_opp(struct stm32mp1_clk_priv *priv, int clksrc,
1318 u32 *pllcfg, u32 *fracv)
1319{
1320 u32 post_divm;
1321 u32 input_freq;
1322 u64 output_freq;
1323 u64 freq;
1324 u64 vco;
1325 u32 divm, divn, divp, frac;
1326 int i, ret;
1327 u32 diff;
1328 u32 best_diff = U32_MAX;
1329
1330 /* PLL1 is 1600 */
1331 const u32 DIVN_MAX = stm32mp1_pll[PLL_1600].divn_max;
1332 const u32 POST_DIVM_MIN = stm32mp1_pll[PLL_1600].refclk_min * 1000000U;
1333 const u32 POST_DIVM_MAX = stm32mp1_pll[PLL_1600].refclk_max * 1000000U;
1334
1335 ret = stm32mp1_get_max_opp_freq(priv, &output_freq);
1336 if (ret) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001337 log_debug("PLL1 OPP configuration not found (%d).\n", ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001338 return ret;
1339 }
1340
1341 switch (clksrc) {
1342 case CLK_PLL12_HSI:
1343 input_freq = stm32mp1_clk_get_fixed(priv, _HSI);
1344 break;
1345 case CLK_PLL12_HSE:
1346 input_freq = stm32mp1_clk_get_fixed(priv, _HSE);
1347 break;
1348 default:
1349 return -EINTR;
1350 }
1351
1352 /* Following parameters have always the same value */
1353 pllcfg[PLLCFG_Q] = 0;
1354 pllcfg[PLLCFG_R] = 0;
1355 pllcfg[PLLCFG_O] = PQR(1, 0, 0);
1356
1357 for (divm = DIVM_MAX; divm >= DIVM_MIN; divm--) {
1358 post_divm = (u32)(input_freq / (divm + 1));
1359 if (post_divm < POST_DIVM_MIN || post_divm > POST_DIVM_MAX)
1360 continue;
1361
1362 for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) {
1363 freq = output_freq * (divm + 1) * (divp + 1);
1364 divn = (u32)((freq / input_freq) - 1);
1365 if (divn < DIVN_MIN || divn > DIVN_MAX)
1366 continue;
1367
1368 frac = (u32)(((freq * FRAC_MAX) / input_freq) -
1369 ((divn + 1) * FRAC_MAX));
1370 /* 2 loops to refine the fractional part */
1371 for (i = 2; i != 0; i--) {
1372 if (frac > FRAC_MAX)
1373 break;
1374
1375 vco = (post_divm * (divn + 1)) +
1376 ((post_divm * (u64)frac) /
1377 FRAC_MAX);
1378 if (vco < (PLL1600_VCO_MIN / 2) ||
1379 vco > (PLL1600_VCO_MAX / 2)) {
1380 frac++;
1381 continue;
1382 }
1383 freq = vco / (divp + 1);
1384 if (output_freq < freq)
1385 diff = (u32)(freq - output_freq);
1386 else
1387 diff = (u32)(output_freq - freq);
1388 if (diff < best_diff) {
1389 pllcfg[PLLCFG_M] = divm;
1390 pllcfg[PLLCFG_N] = divn;
1391 pllcfg[PLLCFG_P] = divp;
1392 *fracv = frac;
1393
1394 if (diff == 0)
1395 return 0;
1396
1397 best_diff = diff;
1398 }
1399 frac++;
1400 }
1401 }
1402 }
1403
1404 if (best_diff == U32_MAX)
1405 return -1;
1406
1407 return 0;
1408}
1409
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001410static void stm32mp1_ls_osc_set(int enable, fdt_addr_t rcc, u32 offset,
1411 u32 mask_on)
1412{
1413 u32 address = rcc + offset;
1414
1415 if (enable)
1416 setbits_le32(address, mask_on);
1417 else
1418 clrbits_le32(address, mask_on);
1419}
1420
1421static void stm32mp1_hs_ocs_set(int enable, fdt_addr_t rcc, u32 mask_on)
1422{
Patrick Delaunay63201282019-01-30 13:07:02 +01001423 writel(mask_on, rcc + (enable ? RCC_OCENSETR : RCC_OCENCLRR));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001424}
1425
1426static int stm32mp1_osc_wait(int enable, fdt_addr_t rcc, u32 offset,
1427 u32 mask_rdy)
1428{
1429 u32 mask_test = 0;
1430 u32 address = rcc + offset;
1431 u32 val;
1432 int ret;
1433
1434 if (enable)
1435 mask_test = mask_rdy;
1436
1437 ret = readl_poll_timeout(address, val,
1438 (val & mask_rdy) == mask_test,
1439 TIMEOUT_1S);
1440
1441 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001442 log_err("OSC %x @ %x timeout for enable=%d : 0x%x\n",
1443 mask_rdy, address, enable, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001444
1445 return ret;
1446}
1447
Patrick Delaunayd2194152018-07-16 10:41:46 +02001448static void stm32mp1_lse_enable(fdt_addr_t rcc, int bypass, int digbyp,
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001449 u32 lsedrv)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001450{
1451 u32 value;
1452
Patrick Delaunayd2194152018-07-16 10:41:46 +02001453 if (digbyp)
1454 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_DIGBYP);
1455
1456 if (bypass || digbyp)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001457 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_LSEBYP);
1458
1459 /*
1460 * warning: not recommended to switch directly from "high drive"
1461 * to "medium low drive", and vice-versa.
1462 */
1463 value = (readl(rcc + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK)
1464 >> RCC_BDCR_LSEDRV_SHIFT;
1465
1466 while (value != lsedrv) {
1467 if (value > lsedrv)
1468 value--;
1469 else
1470 value++;
1471
1472 clrsetbits_le32(rcc + RCC_BDCR,
1473 RCC_BDCR_LSEDRV_MASK,
1474 value << RCC_BDCR_LSEDRV_SHIFT);
1475 }
1476
1477 stm32mp1_ls_osc_set(1, rcc, RCC_BDCR, RCC_BDCR_LSEON);
1478}
1479
1480static void stm32mp1_lse_wait(fdt_addr_t rcc)
1481{
1482 stm32mp1_osc_wait(1, rcc, RCC_BDCR, RCC_BDCR_LSERDY);
1483}
1484
1485static void stm32mp1_lsi_set(fdt_addr_t rcc, int enable)
1486{
1487 stm32mp1_ls_osc_set(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSION);
1488 stm32mp1_osc_wait(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSIRDY);
1489}
1490
Patrick Delaunayd2194152018-07-16 10:41:46 +02001491static void stm32mp1_hse_enable(fdt_addr_t rcc, int bypass, int digbyp, int css)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001492{
Patrick Delaunayd2194152018-07-16 10:41:46 +02001493 if (digbyp)
Patrick Delaunay63201282019-01-30 13:07:02 +01001494 writel(RCC_OCENR_DIGBYP, rcc + RCC_OCENSETR);
Patrick Delaunayd2194152018-07-16 10:41:46 +02001495 if (bypass || digbyp)
Patrick Delaunay63201282019-01-30 13:07:02 +01001496 writel(RCC_OCENR_HSEBYP, rcc + RCC_OCENSETR);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001497
1498 stm32mp1_hs_ocs_set(1, rcc, RCC_OCENR_HSEON);
1499 stm32mp1_osc_wait(1, rcc, RCC_OCRDYR, RCC_OCRDYR_HSERDY);
1500
1501 if (css)
Patrick Delaunay63201282019-01-30 13:07:02 +01001502 writel(RCC_OCENR_HSECSSON, rcc + RCC_OCENSETR);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001503}
1504
1505static void stm32mp1_csi_set(fdt_addr_t rcc, int enable)
1506{
Patrick Delaunay63201282019-01-30 13:07:02 +01001507 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_CSION);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001508 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_CSIRDY);
1509}
1510
1511static void stm32mp1_hsi_set(fdt_addr_t rcc, int enable)
1512{
1513 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_HSION);
1514 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_HSIRDY);
1515}
1516
1517static int stm32mp1_set_hsidiv(fdt_addr_t rcc, u8 hsidiv)
1518{
1519 u32 address = rcc + RCC_OCRDYR;
1520 u32 val;
1521 int ret;
1522
1523 clrsetbits_le32(rcc + RCC_HSICFGR,
1524 RCC_HSICFGR_HSIDIV_MASK,
1525 RCC_HSICFGR_HSIDIV_MASK & hsidiv);
1526
1527 ret = readl_poll_timeout(address, val,
1528 val & RCC_OCRDYR_HSIDIVRDY,
1529 TIMEOUT_200MS);
1530 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001531 log_err("HSIDIV failed @ 0x%x: 0x%x\n",
1532 address, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001533
1534 return ret;
1535}
1536
1537static int stm32mp1_hsidiv(fdt_addr_t rcc, ulong hsifreq)
1538{
1539 u8 hsidiv;
1540 u32 hsidivfreq = MAX_HSI_HZ;
1541
1542 for (hsidiv = 0; hsidiv < 4; hsidiv++,
1543 hsidivfreq = hsidivfreq / 2)
1544 if (hsidivfreq == hsifreq)
1545 break;
1546
1547 if (hsidiv == 4) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001548 log_err("clk-hsi frequency invalid");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001549 return -1;
1550 }
1551
1552 if (hsidiv > 0)
1553 return stm32mp1_set_hsidiv(rcc, hsidiv);
1554
1555 return 0;
1556}
1557
1558static void pll_start(struct stm32mp1_clk_priv *priv, int pll_id)
1559{
1560 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1561
Patrick Delaunaybbd108a2019-01-30 13:07:06 +01001562 clrsetbits_le32(priv->base + pll[pll_id].pllxcr,
1563 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1564 RCC_PLLNCR_DIVREN,
1565 RCC_PLLNCR_PLLON);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001566}
1567
1568static int pll_output(struct stm32mp1_clk_priv *priv, int pll_id, int output)
1569{
1570 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1571 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1572 u32 val;
1573 int ret;
1574
1575 ret = readl_poll_timeout(pllxcr, val, val & RCC_PLLNCR_PLLRDY,
1576 TIMEOUT_200MS);
1577
1578 if (ret) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001579 log_err("PLL%d start failed @ 0x%x: 0x%x\n",
1580 pll_id, pllxcr, readl(pllxcr));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001581 return ret;
1582 }
1583
1584 /* start the requested output */
1585 setbits_le32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1586
1587 return 0;
1588}
1589
1590static int pll_stop(struct stm32mp1_clk_priv *priv, int pll_id)
1591{
1592 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1593 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1594 u32 val;
1595
1596 /* stop all output */
1597 clrbits_le32(pllxcr,
1598 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
1599
1600 /* stop PLL */
1601 clrbits_le32(pllxcr, RCC_PLLNCR_PLLON);
1602
1603 /* wait PLL stopped */
1604 return readl_poll_timeout(pllxcr, val, (val & RCC_PLLNCR_PLLRDY) == 0,
1605 TIMEOUT_200MS);
1606}
1607
1608static void pll_config_output(struct stm32mp1_clk_priv *priv,
1609 int pll_id, u32 *pllcfg)
1610{
1611 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1612 fdt_addr_t rcc = priv->base;
1613 u32 value;
1614
1615 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT)
1616 & RCC_PLLNCFGR2_DIVP_MASK;
1617 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT)
1618 & RCC_PLLNCFGR2_DIVQ_MASK;
1619 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT)
1620 & RCC_PLLNCFGR2_DIVR_MASK;
1621 writel(value, rcc + pll[pll_id].pllxcfgr2);
1622}
1623
1624static int pll_config(struct stm32mp1_clk_priv *priv, int pll_id,
1625 u32 *pllcfg, u32 fracv)
1626{
1627 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1628 fdt_addr_t rcc = priv->base;
1629 enum stm32mp1_plltype type = pll[pll_id].plltype;
1630 int src;
1631 ulong refclk;
1632 u8 ifrge = 0;
1633 u32 value;
1634
1635 src = readl(priv->base + pll[pll_id].rckxselr) & RCC_SELR_SRC_MASK;
1636
1637 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]) /
1638 (pllcfg[PLLCFG_M] + 1);
1639
1640 if (refclk < (stm32mp1_pll[type].refclk_min * 1000000) ||
1641 refclk > (stm32mp1_pll[type].refclk_max * 1000000)) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001642 log_err("invalid refclk = %x\n", (u32)refclk);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001643 return -EINVAL;
1644 }
1645 if (type == PLL_800 && refclk >= 8000000)
1646 ifrge = 1;
1647
1648 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT)
1649 & RCC_PLLNCFGR1_DIVN_MASK;
1650 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT)
1651 & RCC_PLLNCFGR1_DIVM_MASK;
1652 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT)
1653 & RCC_PLLNCFGR1_IFRGE_MASK;
1654 writel(value, rcc + pll[pll_id].pllxcfgr1);
1655
1656 /* fractional configuration: load sigma-delta modulator (SDM) */
1657
1658 /* Write into FRACV the new fractional value , and FRACLE to 0 */
1659 writel(fracv << RCC_PLLNFRACR_FRACV_SHIFT,
1660 rcc + pll[pll_id].pllxfracr);
1661
1662 /* Write FRACLE to 1 : FRACV value is loaded into the SDM */
1663 setbits_le32(rcc + pll[pll_id].pllxfracr,
1664 RCC_PLLNFRACR_FRACLE);
1665
1666 pll_config_output(priv, pll_id, pllcfg);
1667
1668 return 0;
1669}
1670
1671static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
1672{
1673 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1674 u32 pllxcsg;
1675
1676 pllxcsg = ((csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1677 RCC_PLLNCSGR_MOD_PER_MASK) |
1678 ((csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1679 RCC_PLLNCSGR_INC_STEP_MASK) |
1680 ((csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1681 RCC_PLLNCSGR_SSCG_MODE_MASK);
1682
1683 writel(pllxcsg, priv->base + pll[pll_id].pllxcsgr);
Patrick Delaunaybbd108a2019-01-30 13:07:06 +01001684
1685 setbits_le32(priv->base + pll[pll_id].pllxcr, RCC_PLLNCR_SSCG_CTRL);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001686}
1687
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001688static __maybe_unused int pll_set_rate(struct udevice *dev,
1689 int pll_id,
1690 int div_id,
1691 unsigned long clk_rate)
1692{
1693 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1694 unsigned int pllcfg[PLLCFG_NB];
1695 ofnode plloff;
1696 char name[12];
1697 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1698 enum stm32mp1_plltype type = pll[pll_id].plltype;
1699 int divm, divn, divy;
1700 int ret;
1701 ulong fck_ref;
1702 u32 fracv;
1703 u64 value;
1704
1705 if (div_id > _DIV_NB)
1706 return -EINVAL;
1707
1708 sprintf(name, "st,pll@%d", pll_id);
1709 plloff = dev_read_subnode(dev, name);
1710 if (!ofnode_valid(plloff))
1711 return -FDT_ERR_NOTFOUND;
1712
1713 ret = ofnode_read_u32_array(plloff, "cfg",
1714 pllcfg, PLLCFG_NB);
1715 if (ret < 0)
1716 return -FDT_ERR_NOTFOUND;
1717
1718 fck_ref = pll_get_fref_ck(priv, pll_id);
1719
1720 divm = pllcfg[PLLCFG_M];
1721 /* select output divider = 0: for _DIV_P, 1:_DIV_Q 2:_DIV_R */
1722 divy = pllcfg[PLLCFG_P + div_id];
1723
1724 /* For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
1725 * So same final result than PLL2 et 4
1726 * with FRACV
1727 * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
1728 * / (DIVy + 1) * (DIVM + 1)
1729 * value = (DIVN + 1) * 2^13 + FRACV / 2^13
1730 * = Fck_pll_y (DIVy + 1) * (DIVM + 1) * 2^13 / Fck_ref
1731 */
1732 value = ((u64)clk_rate * (divy + 1) * (divm + 1)) << 13;
1733 value = lldiv(value, fck_ref);
1734
1735 divn = (value >> 13) - 1;
1736 if (divn < DIVN_MIN ||
1737 divn > stm32mp1_pll[type].divn_max) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001738 dev_err(dev, "divn invalid = %d", divn);
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001739 return -EINVAL;
1740 }
1741 fracv = value - ((divn + 1) << 13);
1742 pllcfg[PLLCFG_N] = divn;
1743
1744 /* reconfigure PLL */
1745 pll_stop(priv, pll_id);
1746 pll_config(priv, pll_id, pllcfg, fracv);
1747 pll_start(priv, pll_id);
1748 pll_output(priv, pll_id, pllcfg[PLLCFG_O]);
1749
1750 return 0;
1751}
1752
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001753static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
1754{
1755 u32 address = priv->base + (clksrc >> 4);
1756 u32 val;
1757 int ret;
1758
1759 clrsetbits_le32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK);
1760 ret = readl_poll_timeout(address, val, val & RCC_SELR_SRCRDY,
1761 TIMEOUT_200MS);
1762 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001763 log_err("CLKSRC %x start failed @ 0x%x: 0x%x\n",
1764 clksrc, address, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001765
1766 return ret;
1767}
1768
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001769static void stgen_config(struct stm32mp1_clk_priv *priv)
1770{
1771 int p;
1772 u32 stgenc, cntfid0;
1773 ulong rate;
1774
Patrick Delaunaydfda7d42019-07-05 17:20:11 +02001775 stgenc = STM32_STGEN_BASE;
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001776 cntfid0 = readl(stgenc + STGENC_CNTFID0);
1777 p = stm32mp1_clk_get_parent(priv, STGEN_K);
1778 rate = stm32mp1_clk_get(priv, p);
1779
1780 if (cntfid0 != rate) {
Patrick Delaunayf3a23c22019-01-30 13:07:03 +01001781 u64 counter;
1782
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001783 log_debug("System Generic Counter (STGEN) update\n");
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001784 clrbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
Patrick Delaunayf3a23c22019-01-30 13:07:03 +01001785 counter = (u64)readl(stgenc + STGENC_CNTCVL);
1786 counter |= ((u64)(readl(stgenc + STGENC_CNTCVU))) << 32;
1787 counter = lldiv(counter * (u64)rate, cntfid0);
1788 writel((u32)counter, stgenc + STGENC_CNTCVL);
1789 writel((u32)(counter >> 32), stgenc + STGENC_CNTCVU);
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001790 writel(rate, stgenc + STGENC_CNTFID0);
1791 setbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1792
1793 __asm__ volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (rate));
1794
1795 /* need to update gd->arch.timer_rate_hz with new frequency */
1796 timer_init();
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001797 }
1798}
1799
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001800static int set_clkdiv(unsigned int clkdiv, u32 address)
1801{
1802 u32 val;
1803 int ret;
1804
1805 clrsetbits_le32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK);
1806 ret = readl_poll_timeout(address, val, val & RCC_DIVR_DIVRDY,
1807 TIMEOUT_200MS);
1808 if (ret)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001809 log_err("CLKDIV %x start failed @ 0x%x: 0x%x\n",
1810 clkdiv, address, readl(address));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001811
1812 return ret;
1813}
1814
1815static void stm32mp1_mco_csg(struct stm32mp1_clk_priv *priv,
1816 u32 clksrc, u32 clkdiv)
1817{
1818 u32 address = priv->base + (clksrc >> 4);
1819
1820 /*
1821 * binding clksrc : bit15-4 offset
1822 * bit3: disable
1823 * bit2-0: MCOSEL[2:0]
1824 */
1825 if (clksrc & 0x8) {
1826 clrbits_le32(address, RCC_MCOCFG_MCOON);
1827 } else {
1828 clrsetbits_le32(address,
1829 RCC_MCOCFG_MCOSRC_MASK,
1830 clksrc & RCC_MCOCFG_MCOSRC_MASK);
1831 clrsetbits_le32(address,
1832 RCC_MCOCFG_MCODIV_MASK,
1833 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
1834 setbits_le32(address, RCC_MCOCFG_MCOON);
1835 }
1836}
1837
1838static void set_rtcsrc(struct stm32mp1_clk_priv *priv,
1839 unsigned int clksrc,
1840 int lse_css)
1841{
1842 u32 address = priv->base + RCC_BDCR;
1843
1844 if (readl(address) & RCC_BDCR_RTCCKEN)
1845 goto skip_rtc;
1846
1847 if (clksrc == CLK_RTC_DISABLED)
1848 goto skip_rtc;
1849
1850 clrsetbits_le32(address,
1851 RCC_BDCR_RTCSRC_MASK,
1852 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1853
1854 setbits_le32(address, RCC_BDCR_RTCCKEN);
1855
1856skip_rtc:
1857 if (lse_css)
1858 setbits_le32(address, RCC_BDCR_LSECSSON);
1859}
1860
1861static void pkcs_config(struct stm32mp1_clk_priv *priv, u32 pkcs)
1862{
1863 u32 address = priv->base + ((pkcs >> 4) & 0xFFF);
1864 u32 value = pkcs & 0xF;
1865 u32 mask = 0xF;
1866
1867 if (pkcs & BIT(31)) {
1868 mask <<= 4;
1869 value <<= 4;
1870 }
1871 clrsetbits_le32(address, mask, value);
1872}
1873
1874static int stm32mp1_clktree(struct udevice *dev)
1875{
1876 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1877 fdt_addr_t rcc = priv->base;
1878 unsigned int clksrc[CLKSRC_NB];
1879 unsigned int clkdiv[CLKDIV_NB];
1880 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001881 unsigned int pllfracv[_PLL_NB];
1882 unsigned int pllcsg[_PLL_NB][PLLCSG_NB];
1883 bool pllcfg_valid[_PLL_NB];
1884 bool pllcsg_set[_PLL_NB];
1885 int ret;
1886 int i, len;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001887 int lse_css = 0;
1888 const u32 *pkcs_cell;
1889
1890 /* check mandatory field */
1891 ret = dev_read_u32_array(dev, "st,clksrc", clksrc, CLKSRC_NB);
1892 if (ret < 0) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001893 dev_dbg(dev, "field st,clksrc invalid: error %d\n", ret);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001894 return -FDT_ERR_NOTFOUND;
1895 }
1896
1897 ret = dev_read_u32_array(dev, "st,clkdiv", clkdiv, CLKDIV_NB);
1898 if (ret < 0) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001899 dev_dbg(dev, "field st,clkdiv invalid: error %d\n", ret);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001900 return -FDT_ERR_NOTFOUND;
1901 }
1902
1903 /* check mandatory field in each pll */
1904 for (i = 0; i < _PLL_NB; i++) {
1905 char name[12];
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001906 ofnode node;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001907
1908 sprintf(name, "st,pll@%d", i);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001909 node = dev_read_subnode(dev, name);
1910 pllcfg_valid[i] = ofnode_valid(node);
1911 pllcsg_set[i] = false;
1912 if (pllcfg_valid[i]) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001913 dev_dbg(dev, "DT for PLL %d @ %s\n", i, name);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001914 ret = ofnode_read_u32_array(node, "cfg",
1915 pllcfg[i], PLLCFG_NB);
1916 if (ret < 0) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001917 dev_dbg(dev, "field cfg invalid: error %d\n", ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001918 return -FDT_ERR_NOTFOUND;
1919 }
1920 pllfracv[i] = ofnode_read_u32_default(node, "frac", 0);
1921
1922 ret = ofnode_read_u32_array(node, "csg", pllcsg[i],
1923 PLLCSG_NB);
1924 if (!ret) {
1925 pllcsg_set[i] = true;
1926 } else if (ret != -FDT_ERR_NOTFOUND) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001927 dev_dbg(dev, "invalid csg node for pll@%d res=%d\n",
1928 i, ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001929 return ret;
1930 }
1931 } else if (i == _PLL1) {
1932 /* use OPP for PLL1 for A7 CPU */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001933 dev_dbg(dev, "DT for PLL %d with OPP\n", i);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001934 ret = stm32mp1_pll1_opp(priv,
1935 clksrc[CLKSRC_PLL12],
1936 pllcfg[i],
1937 &pllfracv[i]);
1938 if (ret) {
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001939 dev_dbg(dev, "PLL %d with OPP error = %d\n", i, ret);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02001940 return ret;
1941 }
1942 pllcfg_valid[i] = true;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001943 }
1944 }
1945
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001946 dev_dbg(dev, "configuration MCO\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001947 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1948 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1949
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001950 dev_dbg(dev, "switch ON osillator\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001951 /*
1952 * switch ON oscillator found in device-tree,
1953 * HSI already ON after bootrom
1954 */
1955 if (priv->osc[_LSI])
1956 stm32mp1_lsi_set(rcc, 1);
1957
1958 if (priv->osc[_LSE]) {
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001959 int bypass, digbyp;
1960 u32 lsedrv;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001961 struct udevice *dev = priv->osc_dev[_LSE];
1962
1963 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunayd2194152018-07-16 10:41:46 +02001964 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001965 lse_css = dev_read_bool(dev, "st,css");
1966 lsedrv = dev_read_u32_default(dev, "st,drive",
1967 LSEDRV_MEDIUM_HIGH);
1968
Patrick Delaunayd2194152018-07-16 10:41:46 +02001969 stm32mp1_lse_enable(rcc, bypass, digbyp, lsedrv);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001970 }
1971
1972 if (priv->osc[_HSE]) {
Patrick Delaunayd2194152018-07-16 10:41:46 +02001973 int bypass, digbyp, css;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001974 struct udevice *dev = priv->osc_dev[_HSE];
1975
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 css = dev_read_bool(dev, "st,css");
1979
Patrick Delaunayd2194152018-07-16 10:41:46 +02001980 stm32mp1_hse_enable(rcc, bypass, digbyp, css);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001981 }
1982 /* CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1983 * => switch on CSI even if node is not present in device tree
1984 */
1985 stm32mp1_csi_set(rcc, 1);
1986
1987 /* come back to HSI */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001988 dev_dbg(dev, "come back to HSI\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001989 set_clksrc(priv, CLK_MPU_HSI);
1990 set_clksrc(priv, CLK_AXI_HSI);
1991 set_clksrc(priv, CLK_MCU_HSI);
1992
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001993 dev_dbg(dev, "pll stop\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001994 for (i = 0; i < _PLL_NB; i++)
1995 pll_stop(priv, i);
1996
1997 /* configure HSIDIV */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01001998 dev_dbg(dev, "configure HSIDIV\n");
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001999 if (priv->osc[_HSI]) {
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002000 stm32mp1_hsidiv(rcc, priv->osc[_HSI]);
Patrick Delaunay938e0e32018-03-20 11:41:25 +01002001 stgen_config(priv);
2002 }
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002003
2004 /* select DIV */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002005 dev_dbg(dev, "select DIV\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002006 /* no ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
2007 writel(clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK, rcc + RCC_MPCKDIVR);
2008 set_clkdiv(clkdiv[CLKDIV_AXI], rcc + RCC_AXIDIVR);
2009 set_clkdiv(clkdiv[CLKDIV_APB4], rcc + RCC_APB4DIVR);
2010 set_clkdiv(clkdiv[CLKDIV_APB5], rcc + RCC_APB5DIVR);
2011 set_clkdiv(clkdiv[CLKDIV_MCU], rcc + RCC_MCUDIVR);
2012 set_clkdiv(clkdiv[CLKDIV_APB1], rcc + RCC_APB1DIVR);
2013 set_clkdiv(clkdiv[CLKDIV_APB2], rcc + RCC_APB2DIVR);
2014 set_clkdiv(clkdiv[CLKDIV_APB3], rcc + RCC_APB3DIVR);
2015
2016 /* no ready bit for RTC */
2017 writel(clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK, rcc + RCC_RTCDIVR);
2018
2019 /* configure PLLs source */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002020 dev_dbg(dev, "configure PLLs source\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002021 set_clksrc(priv, clksrc[CLKSRC_PLL12]);
2022 set_clksrc(priv, clksrc[CLKSRC_PLL3]);
2023 set_clksrc(priv, clksrc[CLKSRC_PLL4]);
2024
2025 /* configure and start PLLs */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002026 dev_dbg(dev, "configure PLLs\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002027 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002028 if (!pllcfg_valid[i])
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002029 continue;
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002030 dev_dbg(dev, "configure PLL %d\n", i);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002031 pll_config(priv, i, pllcfg[i], pllfracv[i]);
2032 if (pllcsg_set[i])
2033 pll_csg(priv, i, pllcsg[i]);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002034 pll_start(priv, i);
2035 }
2036
2037 /* wait and start PLLs ouptut when ready */
2038 for (i = 0; i < _PLL_NB; i++) {
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002039 if (!pllcfg_valid[i])
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002040 continue;
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002041 dev_dbg(dev, "output PLL %d\n", i);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002042 pll_output(priv, i, pllcfg[i][PLLCFG_O]);
2043 }
2044
2045 /* wait LSE ready before to use it */
2046 if (priv->osc[_LSE])
2047 stm32mp1_lse_wait(rcc);
2048
2049 /* configure with expected clock source */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002050 dev_dbg(dev, "CLKSRC\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002051 set_clksrc(priv, clksrc[CLKSRC_MPU]);
2052 set_clksrc(priv, clksrc[CLKSRC_AXI]);
2053 set_clksrc(priv, clksrc[CLKSRC_MCU]);
2054 set_rtcsrc(priv, clksrc[CLKSRC_RTC], lse_css);
2055
2056 /* configure PKCK */
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002057 dev_dbg(dev, "PKCK\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002058 pkcs_cell = dev_read_prop(dev, "st,pkcs", &len);
2059 if (pkcs_cell) {
2060 bool ckper_disabled = false;
2061
2062 for (i = 0; i < len / sizeof(u32); i++) {
2063 u32 pkcs = (u32)fdt32_to_cpu(pkcs_cell[i]);
2064
2065 if (pkcs == CLK_CKPER_DISABLED) {
2066 ckper_disabled = true;
2067 continue;
2068 }
2069 pkcs_config(priv, pkcs);
2070 }
2071 /* CKPER is source for some peripheral clock
2072 * (FMC-NAND / QPSI-NOR) and switching source is allowed
2073 * only if previous clock is still ON
2074 * => deactivated CKPER only after switching clock
2075 */
2076 if (ckper_disabled)
2077 pkcs_config(priv, CLK_CKPER_DISABLED);
2078 }
2079
Patrick Delaunay938e0e32018-03-20 11:41:25 +01002080 /* STGEN clock source can change with CLK_STGEN_XXX */
2081 stgen_config(priv);
2082
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002083 dev_dbg(dev, "oscillator off\n");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002084 /* switch OFF HSI if not found in device-tree */
2085 if (!priv->osc[_HSI])
2086 stm32mp1_hsi_set(rcc, 0);
2087
2088 /* Software Self-Refresh mode (SSR) during DDR initilialization */
2089 clrsetbits_le32(priv->base + RCC_DDRITFCR,
2090 RCC_DDRITFCR_DDRCKMOD_MASK,
2091 RCC_DDRITFCR_DDRCKMOD_SSR <<
2092 RCC_DDRITFCR_DDRCKMOD_SHIFT);
2093
2094 return 0;
2095}
2096#endif /* STM32MP1_CLOCK_TREE_INIT */
2097
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002098static int pll_set_output_rate(struct udevice *dev,
2099 int pll_id,
2100 int div_id,
2101 unsigned long clk_rate)
2102{
2103 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2104 const struct stm32mp1_clk_pll *pll = priv->data->pll;
2105 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
2106 int div;
2107 ulong fvco;
2108
2109 if (div_id > _DIV_NB)
2110 return -EINVAL;
2111
2112 fvco = pll_get_fvco(priv, pll_id);
2113
2114 if (fvco <= clk_rate)
2115 div = 1;
2116 else
2117 div = DIV_ROUND_UP(fvco, clk_rate);
2118
2119 if (div > 128)
2120 div = 128;
2121
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002122 /* stop the requested output */
2123 clrbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2124 /* change divider */
2125 clrsetbits_le32(priv->base + pll[pll_id].pllxcfgr2,
2126 RCC_PLLNCFGR2_DIVX_MASK << RCC_PLLNCFGR2_SHIFT(div_id),
2127 (div - 1) << RCC_PLLNCFGR2_SHIFT(div_id));
2128 /* start the requested output */
2129 setbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
2130
2131 return 0;
2132}
2133
2134static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
2135{
2136 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
2137 int p;
2138
2139 switch (clk->id) {
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02002140#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2141 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2142 case DDRPHYC:
2143 break;
2144#endif
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002145 case LTDC_PX:
2146 case DSI_PX:
2147 break;
2148 default:
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002149 dev_err(clk->dev, "Set of clk %ld not supported", clk->id);
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002150 return -EINVAL;
2151 }
2152
2153 p = stm32mp1_clk_get_parent(priv, clk->id);
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002154 dev_vdbg(clk->dev, "parent = %d:%s\n", p, stm32mp1_clk_parent_name[p]);
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002155 if (p < 0)
2156 return -EINVAL;
2157
2158 switch (p) {
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02002159#if defined(STM32MP1_CLOCK_TREE_INIT) && \
2160 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
2161 case _PLL2_R: /* DDRPHYC */
2162 {
2163 /* only for change DDR clock in interactive mode */
2164 ulong result;
2165
2166 set_clksrc(priv, CLK_AXI_HSI);
2167 result = pll_set_rate(clk->dev, _PLL2, _DIV_R, clk_rate);
2168 set_clksrc(priv, CLK_AXI_PLL2P);
2169 return result;
2170 }
2171#endif
Patrick Delaunay7879a7d2019-07-30 19:16:54 +02002172
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002173 case _PLL4_Q:
2174 /* for LTDC_PX and DSI_PX case */
2175 return pll_set_output_rate(clk->dev, _PLL4, _DIV_Q, clk_rate);
2176 }
2177
2178 return -EINVAL;
2179}
2180
Patrick Delaunaya6151912018-03-12 10:46:15 +01002181static void stm32mp1_osc_clk_init(const char *name,
2182 struct stm32mp1_clk_priv *priv,
2183 int index)
2184{
2185 struct clk clk;
2186 struct udevice *dev = NULL;
2187
2188 priv->osc[index] = 0;
2189 clk.id = 0;
2190 if (!uclass_get_device_by_name(UCLASS_CLK, name, &dev)) {
2191 if (clk_request(dev, &clk))
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002192 log_err("%s request", name);
Patrick Delaunaya6151912018-03-12 10:46:15 +01002193 else
2194 priv->osc[index] = clk_get_rate(&clk);
2195 }
2196 priv->osc_dev[index] = dev;
2197}
2198
2199static void stm32mp1_osc_init(struct udevice *dev)
2200{
2201 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2202 int i;
2203 const char *name[NB_OSC] = {
2204 [_LSI] = "clk-lsi",
2205 [_LSE] = "clk-lse",
2206 [_HSI] = "clk-hsi",
2207 [_HSE] = "clk-hse",
2208 [_CSI] = "clk-csi",
2209 [_I2S_CKIN] = "i2s_ckin",
Patrick Delaunay86617dd2019-01-30 13:07:00 +01002210 };
Patrick Delaunaya6151912018-03-12 10:46:15 +01002211
2212 for (i = 0; i < NB_OSC; i++) {
2213 stm32mp1_osc_clk_init(name[i], priv, i);
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002214 dev_dbg(dev, "%d: %s => %x\n", i, name[i], (u32)priv->osc[i]);
Patrick Delaunaya6151912018-03-12 10:46:15 +01002215 }
2216}
2217
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002218static void __maybe_unused stm32mp1_clk_dump(struct stm32mp1_clk_priv *priv)
2219{
2220 char buf[32];
2221 int i, s, p;
2222
2223 printf("Clocks:\n");
2224 for (i = 0; i < _PARENT_NB; i++) {
2225 printf("- %s : %s MHz\n",
2226 stm32mp1_clk_parent_name[i],
2227 strmhz(buf, stm32mp1_clk_get(priv, i)));
2228 }
2229 printf("Source Clocks:\n");
2230 for (i = 0; i < _PARENT_SEL_NB; i++) {
2231 p = (readl(priv->base + priv->data->sel[i].offset) >>
2232 priv->data->sel[i].src) & priv->data->sel[i].msk;
2233 if (p < priv->data->sel[i].nb_parent) {
2234 s = priv->data->sel[i].parent[p];
2235 printf("- %s(%d) => parent %s(%d)\n",
2236 stm32mp1_clk_parent_sel_name[i], i,
2237 stm32mp1_clk_parent_name[s], s);
2238 } else {
2239 printf("- %s(%d) => parent index %d is invalid\n",
2240 stm32mp1_clk_parent_sel_name[i], i, p);
2241 }
2242 }
2243}
2244
2245#ifdef CONFIG_CMD_CLK
2246int soc_clk_dump(void)
2247{
2248 struct udevice *dev;
2249 struct stm32mp1_clk_priv *priv;
2250 int ret;
2251
2252 ret = uclass_get_device_by_driver(UCLASS_CLK,
Simon Glass65e25be2020-12-28 20:34:56 -07002253 DM_DRIVER_GET(stm32mp1_clock),
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002254 &dev);
2255 if (ret)
2256 return ret;
2257
2258 priv = dev_get_priv(dev);
2259
2260 stm32mp1_clk_dump(priv);
2261
2262 return 0;
2263}
2264#endif
2265
Patrick Delaunaya6151912018-03-12 10:46:15 +01002266static int stm32mp1_clk_probe(struct udevice *dev)
2267{
2268 int result = 0;
2269 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2270
2271 priv->base = dev_read_addr(dev->parent);
2272 if (priv->base == FDT_ADDR_T_NONE)
2273 return -EINVAL;
2274
2275 priv->data = (void *)&stm32mp1_data;
2276
2277 if (!priv->data->gate || !priv->data->sel ||
2278 !priv->data->pll)
2279 return -EINVAL;
2280
2281 stm32mp1_osc_init(dev);
2282
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002283#ifdef STM32MP1_CLOCK_TREE_INIT
2284 /* clock tree init is done only one time, before relocation */
2285 if (!(gd->flags & GD_FLG_RELOC))
2286 result = stm32mp1_clktree(dev);
Patrick Delaunay37ad8372020-05-25 12:19:44 +02002287 if (result)
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002288 dev_err(dev, "clock tree initialization failed (%d)\n", result);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002289#endif
2290
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002291#ifndef CONFIG_SPL_BUILD
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002292#if defined(VERBOSE_DEBUG)
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002293 /* display debug information for probe after relocation */
2294 if (gd->flags & GD_FLG_RELOC)
2295 stm32mp1_clk_dump(priv);
2296#endif
2297
Patrick Delaunay4de076e2019-07-30 19:16:55 +02002298 gd->cpu_clk = stm32mp1_clk_get(priv, _CK_MPU);
2299 gd->bus_clk = stm32mp1_clk_get(priv, _ACLK);
2300 /* DDRPHYC father */
2301 gd->mem_clk = stm32mp1_clk_get(priv, _PLL2_R);
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002302#if defined(CONFIG_DISPLAY_CPUINFO)
2303 if (gd->flags & GD_FLG_RELOC) {
2304 char buf[32];
2305
Patrick Delaunayceab8ee2020-11-06 19:01:45 +01002306 log_info("Clocks:\n");
2307 log_info("- MPU : %s MHz\n", strmhz(buf, gd->cpu_clk));
2308 log_info("- MCU : %s MHz\n",
2309 strmhz(buf, stm32mp1_clk_get(priv, _CK_MCU)));
2310 log_info("- AXI : %s MHz\n", strmhz(buf, gd->bus_clk));
2311 log_info("- PER : %s MHz\n",
2312 strmhz(buf, stm32mp1_clk_get(priv, _CK_PER)));
2313 log_info("- DDR : %s MHz\n", strmhz(buf, gd->mem_clk));
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002314 }
2315#endif /* CONFIG_DISPLAY_CPUINFO */
2316#endif
2317
Patrick Delaunaya6151912018-03-12 10:46:15 +01002318 return result;
2319}
2320
2321static const struct clk_ops stm32mp1_clk_ops = {
2322 .enable = stm32mp1_clk_enable,
2323 .disable = stm32mp1_clk_disable,
2324 .get_rate = stm32mp1_clk_get_rate,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002325 .set_rate = stm32mp1_clk_set_rate,
Patrick Delaunaya6151912018-03-12 10:46:15 +01002326};
2327
Patrick Delaunaya6151912018-03-12 10:46:15 +01002328U_BOOT_DRIVER(stm32mp1_clock) = {
2329 .name = "stm32mp1_clk",
2330 .id = UCLASS_CLK,
Patrick Delaunaya6151912018-03-12 10:46:15 +01002331 .ops = &stm32mp1_clk_ops,
Simon Glass41575d82020-12-03 16:55:17 -07002332 .priv_auto = sizeof(struct stm32mp1_clk_priv),
Patrick Delaunaya6151912018-03-12 10:46:15 +01002333 .probe = stm32mp1_clk_probe,
2334};