blob: 767edb3bf7c31ca3d780b0b4dc1c470be3c7d1f2 [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
6#include <common.h>
7#include <clk-uclass.h>
8#include <div64.h>
9#include <dm.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Patrick Delaunaya6151912018-03-12 10:46:15 +010012#include <regmap.h>
13#include <spl.h>
14#include <syscon.h>
Simon Glass10453152019-11-14 12:57:30 -070015#include <time.h>
Simon Glass2189d5f2019-11-14 12:57:20 -070016#include <vsprintf.h>
Patrick Delaunaya6151912018-03-12 10:46:15 +010017#include <linux/io.h>
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010018#include <linux/iopoll.h>
Patrick Delaunaya6151912018-03-12 10:46:15 +010019#include <dt-bindings/clock/stm32mp1-clks.h>
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010020#include <dt-bindings/clock/stm32mp1-clksrc.h>
21
Patrick Delaunay4de076e2019-07-30 19:16:55 +020022DECLARE_GLOBAL_DATA_PTR;
23
Patrick Delaunay654706b2020-04-01 09:07:33 +020024#ifndef CONFIG_TFABOOT
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010025#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
26/* activate clock tree initialization in the driver */
27#define STM32MP1_CLOCK_TREE_INIT
28#endif
Patrick Delaunayabf26782019-02-12 11:44:39 +010029#endif
Patrick Delaunaya6151912018-03-12 10:46:15 +010030
31#define MAX_HSI_HZ 64000000
32
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010033/* TIMEOUT */
34#define TIMEOUT_200MS 200000
35#define TIMEOUT_1S 1000000
36
Patrick Delaunay938e0e32018-03-20 11:41:25 +010037/* STGEN registers */
38#define STGENC_CNTCR 0x00
39#define STGENC_CNTSR 0x04
40#define STGENC_CNTCVL 0x08
41#define STGENC_CNTCVU 0x0C
42#define STGENC_CNTFID0 0x20
43
44#define STGENC_CNTCR_EN BIT(0)
45
Patrick Delaunaya6151912018-03-12 10:46:15 +010046/* RCC registers */
47#define RCC_OCENSETR 0x0C
48#define RCC_OCENCLRR 0x10
49#define RCC_HSICFGR 0x18
50#define RCC_MPCKSELR 0x20
51#define RCC_ASSCKSELR 0x24
52#define RCC_RCK12SELR 0x28
53#define RCC_MPCKDIVR 0x2C
54#define RCC_AXIDIVR 0x30
55#define RCC_APB4DIVR 0x3C
56#define RCC_APB5DIVR 0x40
57#define RCC_RTCDIVR 0x44
58#define RCC_MSSCKSELR 0x48
59#define RCC_PLL1CR 0x80
60#define RCC_PLL1CFGR1 0x84
61#define RCC_PLL1CFGR2 0x88
62#define RCC_PLL1FRACR 0x8C
63#define RCC_PLL1CSGR 0x90
64#define RCC_PLL2CR 0x94
65#define RCC_PLL2CFGR1 0x98
66#define RCC_PLL2CFGR2 0x9C
67#define RCC_PLL2FRACR 0xA0
68#define RCC_PLL2CSGR 0xA4
69#define RCC_I2C46CKSELR 0xC0
70#define RCC_CPERCKSELR 0xD0
71#define RCC_STGENCKSELR 0xD4
72#define RCC_DDRITFCR 0xD8
73#define RCC_BDCR 0x140
74#define RCC_RDLSICR 0x144
75#define RCC_MP_APB4ENSETR 0x200
76#define RCC_MP_APB5ENSETR 0x208
77#define RCC_MP_AHB5ENSETR 0x210
78#define RCC_MP_AHB6ENSETR 0x218
79#define RCC_OCRDYR 0x808
80#define RCC_DBGCFGR 0x80C
81#define RCC_RCK3SELR 0x820
82#define RCC_RCK4SELR 0x824
83#define RCC_MCUDIVR 0x830
84#define RCC_APB1DIVR 0x834
85#define RCC_APB2DIVR 0x838
86#define RCC_APB3DIVR 0x83C
87#define RCC_PLL3CR 0x880
88#define RCC_PLL3CFGR1 0x884
89#define RCC_PLL3CFGR2 0x888
90#define RCC_PLL3FRACR 0x88C
91#define RCC_PLL3CSGR 0x890
92#define RCC_PLL4CR 0x894
93#define RCC_PLL4CFGR1 0x898
94#define RCC_PLL4CFGR2 0x89C
95#define RCC_PLL4FRACR 0x8A0
96#define RCC_PLL4CSGR 0x8A4
97#define RCC_I2C12CKSELR 0x8C0
98#define RCC_I2C35CKSELR 0x8C4
Patrice Chotard248278d2019-04-30 18:08:27 +020099#define RCC_SPI2S1CKSELR 0x8D8
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100100#define RCC_SPI45CKSELR 0x8E0
Patrick Delaunaya6151912018-03-12 10:46:15 +0100101#define RCC_UART6CKSELR 0x8E4
102#define RCC_UART24CKSELR 0x8E8
103#define RCC_UART35CKSELR 0x8EC
104#define RCC_UART78CKSELR 0x8F0
105#define RCC_SDMMC12CKSELR 0x8F4
106#define RCC_SDMMC3CKSELR 0x8F8
107#define RCC_ETHCKSELR 0x8FC
108#define RCC_QSPICKSELR 0x900
109#define RCC_FMCCKSELR 0x904
110#define RCC_USBCKSELR 0x91C
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200111#define RCC_DSICKSELR 0x924
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200112#define RCC_ADCCKSELR 0x928
Patrick Delaunaya6151912018-03-12 10:46:15 +0100113#define RCC_MP_APB1ENSETR 0xA00
114#define RCC_MP_APB2ENSETR 0XA08
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200115#define RCC_MP_APB3ENSETR 0xA10
Patrick Delaunaya6151912018-03-12 10:46:15 +0100116#define RCC_MP_AHB2ENSETR 0xA18
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100117#define RCC_MP_AHB3ENSETR 0xA20
Patrick Delaunaya6151912018-03-12 10:46:15 +0100118#define RCC_MP_AHB4ENSETR 0xA28
119
120/* used for most of SELR register */
121#define RCC_SELR_SRC_MASK GENMASK(2, 0)
122#define RCC_SELR_SRCRDY BIT(31)
123
124/* Values of RCC_MPCKSELR register */
125#define RCC_MPCKSELR_HSI 0
126#define RCC_MPCKSELR_HSE 1
127#define RCC_MPCKSELR_PLL 2
128#define RCC_MPCKSELR_PLL_MPUDIV 3
129
130/* Values of RCC_ASSCKSELR register */
131#define RCC_ASSCKSELR_HSI 0
132#define RCC_ASSCKSELR_HSE 1
133#define RCC_ASSCKSELR_PLL 2
134
135/* Values of RCC_MSSCKSELR register */
136#define RCC_MSSCKSELR_HSI 0
137#define RCC_MSSCKSELR_HSE 1
138#define RCC_MSSCKSELR_CSI 2
139#define RCC_MSSCKSELR_PLL 3
140
141/* Values of RCC_CPERCKSELR register */
142#define RCC_CPERCKSELR_HSI 0
143#define RCC_CPERCKSELR_CSI 1
144#define RCC_CPERCKSELR_HSE 2
145
146/* used for most of DIVR register : max div for RTC */
147#define RCC_DIVR_DIV_MASK GENMASK(5, 0)
148#define RCC_DIVR_DIVRDY BIT(31)
149
150/* Masks for specific DIVR registers */
151#define RCC_APBXDIV_MASK GENMASK(2, 0)
152#define RCC_MPUDIV_MASK GENMASK(2, 0)
153#define RCC_AXIDIV_MASK GENMASK(2, 0)
154#define RCC_MCUDIV_MASK GENMASK(3, 0)
155
156/* offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */
157#define RCC_MP_ENCLRR_OFFSET 4
158
159/* Fields of RCC_BDCR register */
160#define RCC_BDCR_LSEON BIT(0)
161#define RCC_BDCR_LSEBYP BIT(1)
162#define RCC_BDCR_LSERDY BIT(2)
Patrick Delaunayd2194152018-07-16 10:41:46 +0200163#define RCC_BDCR_DIGBYP BIT(3)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100164#define RCC_BDCR_LSEDRV_MASK GENMASK(5, 4)
165#define RCC_BDCR_LSEDRV_SHIFT 4
166#define RCC_BDCR_LSECSSON BIT(8)
167#define RCC_BDCR_RTCCKEN BIT(20)
168#define RCC_BDCR_RTCSRC_MASK GENMASK(17, 16)
169#define RCC_BDCR_RTCSRC_SHIFT 16
170
171/* Fields of RCC_RDLSICR register */
172#define RCC_RDLSICR_LSION BIT(0)
173#define RCC_RDLSICR_LSIRDY BIT(1)
174
175/* used for ALL PLLNCR registers */
176#define RCC_PLLNCR_PLLON BIT(0)
177#define RCC_PLLNCR_PLLRDY BIT(1)
Patrick Delaunaybbd108a2019-01-30 13:07:06 +0100178#define RCC_PLLNCR_SSCG_CTRL BIT(2)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100179#define RCC_PLLNCR_DIVPEN BIT(4)
180#define RCC_PLLNCR_DIVQEN BIT(5)
181#define RCC_PLLNCR_DIVREN BIT(6)
182#define RCC_PLLNCR_DIVEN_SHIFT 4
183
184/* used for ALL PLLNCFGR1 registers */
185#define RCC_PLLNCFGR1_DIVM_SHIFT 16
186#define RCC_PLLNCFGR1_DIVM_MASK GENMASK(21, 16)
187#define RCC_PLLNCFGR1_DIVN_SHIFT 0
188#define RCC_PLLNCFGR1_DIVN_MASK GENMASK(8, 0)
189/* only for PLL3 and PLL4 */
190#define RCC_PLLNCFGR1_IFRGE_SHIFT 24
191#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK(25, 24)
192
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200193/* used for ALL PLLNCFGR2 registers , using stm32mp1_div_id */
194#define RCC_PLLNCFGR2_SHIFT(div_id) ((div_id) * 8)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100195#define RCC_PLLNCFGR2_DIVX_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200196#define RCC_PLLNCFGR2_DIVP_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_P)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100197#define RCC_PLLNCFGR2_DIVP_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200198#define RCC_PLLNCFGR2_DIVQ_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_Q)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100199#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK(14, 8)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200200#define RCC_PLLNCFGR2_DIVR_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_R)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100201#define RCC_PLLNCFGR2_DIVR_MASK GENMASK(22, 16)
202
203/* used for ALL PLLNFRACR registers */
204#define RCC_PLLNFRACR_FRACV_SHIFT 3
205#define RCC_PLLNFRACR_FRACV_MASK GENMASK(15, 3)
206#define RCC_PLLNFRACR_FRACLE BIT(16)
207
208/* used for ALL PLLNCSGR registers */
209#define RCC_PLLNCSGR_INC_STEP_SHIFT 16
210#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK(30, 16)
211#define RCC_PLLNCSGR_MOD_PER_SHIFT 0
212#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK(12, 0)
213#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15
214#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15)
215
216/* used for RCC_OCENSETR and RCC_OCENCLRR registers */
217#define RCC_OCENR_HSION BIT(0)
218#define RCC_OCENR_CSION BIT(4)
Patrick Delaunayd2194152018-07-16 10:41:46 +0200219#define RCC_OCENR_DIGBYP BIT(7)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100220#define RCC_OCENR_HSEON BIT(8)
221#define RCC_OCENR_HSEBYP BIT(10)
222#define RCC_OCENR_HSECSSON BIT(11)
223
224/* Fields of RCC_OCRDYR register */
225#define RCC_OCRDYR_HSIRDY BIT(0)
226#define RCC_OCRDYR_HSIDIVRDY BIT(2)
227#define RCC_OCRDYR_CSIRDY BIT(4)
228#define RCC_OCRDYR_HSERDY BIT(8)
229
230/* Fields of DDRITFCR register */
231#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
232#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20
233#define RCC_DDRITFCR_DDRCKMOD_SSR 0
234
235/* Fields of RCC_HSICFGR register */
236#define RCC_HSICFGR_HSIDIV_MASK GENMASK(1, 0)
237
238/* used for MCO related operations */
239#define RCC_MCOCFG_MCOON BIT(12)
240#define RCC_MCOCFG_MCODIV_MASK GENMASK(7, 4)
241#define RCC_MCOCFG_MCODIV_SHIFT 4
242#define RCC_MCOCFG_MCOSRC_MASK GENMASK(2, 0)
243
244enum stm32mp1_parent_id {
245/*
246 * _HSI, _HSE, _CSI, _LSI, _LSE should not be moved
247 * they are used as index in osc[] as entry point
248 */
249 _HSI,
250 _HSE,
251 _CSI,
252 _LSI,
253 _LSE,
254 _I2S_CKIN,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100255 NB_OSC,
256
257/* other parent source */
258 _HSI_KER = NB_OSC,
259 _HSE_KER,
260 _HSE_KER_DIV2,
261 _CSI_KER,
262 _PLL1_P,
263 _PLL1_Q,
264 _PLL1_R,
265 _PLL2_P,
266 _PLL2_Q,
267 _PLL2_R,
268 _PLL3_P,
269 _PLL3_Q,
270 _PLL3_R,
271 _PLL4_P,
272 _PLL4_Q,
273 _PLL4_R,
274 _ACLK,
275 _PCLK1,
276 _PCLK2,
277 _PCLK3,
278 _PCLK4,
279 _PCLK5,
280 _HCLK6,
281 _HCLK2,
282 _CK_PER,
283 _CK_MPU,
284 _CK_MCU,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200285 _DSI_PHY,
Patrick Delaunay86617dd2019-01-30 13:07:00 +0100286 _USB_PHY_48,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100287 _PARENT_NB,
288 _UNKNOWN_ID = 0xff,
289};
290
291enum stm32mp1_parent_sel {
292 _I2C12_SEL,
293 _I2C35_SEL,
294 _I2C46_SEL,
295 _UART6_SEL,
296 _UART24_SEL,
297 _UART35_SEL,
298 _UART78_SEL,
299 _SDMMC12_SEL,
300 _SDMMC3_SEL,
301 _ETH_SEL,
302 _QSPI_SEL,
303 _FMC_SEL,
304 _USBPHY_SEL,
305 _USBO_SEL,
306 _STGEN_SEL,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200307 _DSI_SEL,
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200308 _ADC12_SEL,
Patrice Chotard248278d2019-04-30 18:08:27 +0200309 _SPI1_SEL,
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100310 _SPI45_SEL,
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200311 _RTC_SEL,
Patrick Delaunaya6151912018-03-12 10:46:15 +0100312 _PARENT_SEL_NB,
313 _UNKNOWN_SEL = 0xff,
314};
315
316enum stm32mp1_pll_id {
317 _PLL1,
318 _PLL2,
319 _PLL3,
320 _PLL4,
321 _PLL_NB
322};
323
324enum stm32mp1_div_id {
325 _DIV_P,
326 _DIV_Q,
327 _DIV_R,
328 _DIV_NB,
329};
330
331enum stm32mp1_clksrc_id {
332 CLKSRC_MPU,
333 CLKSRC_AXI,
334 CLKSRC_MCU,
335 CLKSRC_PLL12,
336 CLKSRC_PLL3,
337 CLKSRC_PLL4,
338 CLKSRC_RTC,
339 CLKSRC_MCO1,
340 CLKSRC_MCO2,
341 CLKSRC_NB
342};
343
344enum stm32mp1_clkdiv_id {
345 CLKDIV_MPU,
346 CLKDIV_AXI,
347 CLKDIV_MCU,
348 CLKDIV_APB1,
349 CLKDIV_APB2,
350 CLKDIV_APB3,
351 CLKDIV_APB4,
352 CLKDIV_APB5,
353 CLKDIV_RTC,
354 CLKDIV_MCO1,
355 CLKDIV_MCO2,
356 CLKDIV_NB
357};
358
359enum stm32mp1_pllcfg {
360 PLLCFG_M,
361 PLLCFG_N,
362 PLLCFG_P,
363 PLLCFG_Q,
364 PLLCFG_R,
365 PLLCFG_O,
366 PLLCFG_NB
367};
368
369enum stm32mp1_pllcsg {
370 PLLCSG_MOD_PER,
371 PLLCSG_INC_STEP,
372 PLLCSG_SSCG_MODE,
373 PLLCSG_NB
374};
375
376enum stm32mp1_plltype {
377 PLL_800,
378 PLL_1600,
379 PLL_TYPE_NB
380};
381
382struct stm32mp1_pll {
383 u8 refclk_min;
384 u8 refclk_max;
385 u8 divn_max;
386};
387
388struct stm32mp1_clk_gate {
389 u16 offset;
390 u8 bit;
391 u8 index;
392 u8 set_clr;
393 u8 sel;
394 u8 fixed;
395};
396
397struct stm32mp1_clk_sel {
398 u16 offset;
399 u8 src;
400 u8 msk;
401 u8 nb_parent;
402 const u8 *parent;
403};
404
405#define REFCLK_SIZE 4
406struct stm32mp1_clk_pll {
407 enum stm32mp1_plltype plltype;
408 u16 rckxselr;
409 u16 pllxcfgr1;
410 u16 pllxcfgr2;
411 u16 pllxfracr;
412 u16 pllxcr;
413 u16 pllxcsgr;
414 u8 refclk[REFCLK_SIZE];
415};
416
417struct stm32mp1_clk_data {
418 const struct stm32mp1_clk_gate *gate;
419 const struct stm32mp1_clk_sel *sel;
420 const struct stm32mp1_clk_pll *pll;
421 const int nb_gate;
422};
423
424struct stm32mp1_clk_priv {
425 fdt_addr_t base;
426 const struct stm32mp1_clk_data *data;
427 ulong osc[NB_OSC];
428 struct udevice *osc_dev[NB_OSC];
429};
430
431#define STM32MP1_CLK(off, b, idx, s) \
432 { \
433 .offset = (off), \
434 .bit = (b), \
435 .index = (idx), \
436 .set_clr = 0, \
437 .sel = (s), \
438 .fixed = _UNKNOWN_ID, \
439 }
440
441#define STM32MP1_CLK_F(off, b, idx, f) \
442 { \
443 .offset = (off), \
444 .bit = (b), \
445 .index = (idx), \
446 .set_clr = 0, \
447 .sel = _UNKNOWN_SEL, \
448 .fixed = (f), \
449 }
450
451#define STM32MP1_CLK_SET_CLR(off, b, idx, s) \
452 { \
453 .offset = (off), \
454 .bit = (b), \
455 .index = (idx), \
456 .set_clr = 1, \
457 .sel = (s), \
458 .fixed = _UNKNOWN_ID, \
459 }
460
461#define STM32MP1_CLK_SET_CLR_F(off, b, idx, f) \
462 { \
463 .offset = (off), \
464 .bit = (b), \
465 .index = (idx), \
466 .set_clr = 1, \
467 .sel = _UNKNOWN_SEL, \
468 .fixed = (f), \
469 }
470
471#define STM32MP1_CLK_PARENT(idx, off, s, m, p) \
472 [(idx)] = { \
473 .offset = (off), \
474 .src = (s), \
475 .msk = (m), \
476 .parent = (p), \
477 .nb_parent = ARRAY_SIZE((p)) \
478 }
479
480#define STM32MP1_CLK_PLL(idx, type, off1, off2, off3, off4, off5, off6,\
481 p1, p2, p3, p4) \
482 [(idx)] = { \
483 .plltype = (type), \
484 .rckxselr = (off1), \
485 .pllxcfgr1 = (off2), \
486 .pllxcfgr2 = (off3), \
487 .pllxfracr = (off4), \
488 .pllxcr = (off5), \
489 .pllxcsgr = (off6), \
490 .refclk[0] = (p1), \
491 .refclk[1] = (p2), \
492 .refclk[2] = (p3), \
493 .refclk[3] = (p4), \
494 }
495
496static const u8 stm32mp1_clks[][2] = {
497 {CK_PER, _CK_PER},
498 {CK_MPU, _CK_MPU},
499 {CK_AXI, _ACLK},
500 {CK_MCU, _CK_MCU},
501 {CK_HSE, _HSE},
502 {CK_CSI, _CSI},
503 {CK_LSI, _LSI},
504 {CK_LSE, _LSE},
505 {CK_HSI, _HSI},
506 {CK_HSE_DIV2, _HSE_KER_DIV2},
507};
508
509static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
510 STM32MP1_CLK(RCC_DDRITFCR, 0, DDRC1, _UNKNOWN_SEL),
511 STM32MP1_CLK(RCC_DDRITFCR, 1, DDRC1LP, _UNKNOWN_SEL),
512 STM32MP1_CLK(RCC_DDRITFCR, 2, DDRC2, _UNKNOWN_SEL),
513 STM32MP1_CLK(RCC_DDRITFCR, 3, DDRC2LP, _UNKNOWN_SEL),
514 STM32MP1_CLK_F(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
515 STM32MP1_CLK(RCC_DDRITFCR, 5, DDRPHYCLP, _UNKNOWN_SEL),
516 STM32MP1_CLK(RCC_DDRITFCR, 6, DDRCAPB, _UNKNOWN_SEL),
517 STM32MP1_CLK(RCC_DDRITFCR, 7, DDRCAPBLP, _UNKNOWN_SEL),
518 STM32MP1_CLK(RCC_DDRITFCR, 8, AXIDCG, _UNKNOWN_SEL),
519 STM32MP1_CLK(RCC_DDRITFCR, 9, DDRPHYCAPB, _UNKNOWN_SEL),
520 STM32MP1_CLK(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _UNKNOWN_SEL),
521
522 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
523 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
524 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
525 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
526 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
527 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
528 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
529 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
530 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
531 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
532
Patrice Chotard248278d2019-04-30 18:08:27 +0200533 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 8, SPI1_K, _SPI1_SEL),
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100534 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 10, SPI5_K, _SPI45_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100535 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
536
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200537 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
538
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200539 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 0, LTDC_PX, _PLL4_Q),
540 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB4ENSETR, 4, DSI_PX, _PLL4_Q),
541 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 4, DSI_K, _DSI_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100542 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
543 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
544 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
545
546 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200547 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100548 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
549
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200550 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB2ENSETR, 5, ADC12, _HCLK2),
551 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 5, ADC12_K, _ADC12_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100552 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
553 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
554
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100555 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 11, HSEM, _UNKNOWN_SEL),
Patrick Delaunayd661f612019-01-30 13:07:01 +0100556 STM32MP1_CLK_SET_CLR(RCC_MP_AHB3ENSETR, 12, IPCC, _UNKNOWN_SEL),
Benjamin Gaignard283bcd92018-11-27 13:49:51 +0100557
Patrick Delaunaya6151912018-03-12 10:46:15 +0100558 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
559 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
560 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
561 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
562 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
563 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
564 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
565 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
566 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
567 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
568 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
569
570 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
Sughosh Ganu82ebf0f2019-12-28 23:58:28 +0530571 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 6, RNG1_K, _UNKNOWN_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100572
Patrick Delaunayf6ccdda2019-05-17 15:08:42 +0200573 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK_K, _ETH_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100574 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
575 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100576 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
577 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
578 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
579 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
580 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
581 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
582
583 STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200584
585 STM32MP1_CLK(RCC_BDCR, 20, RTC, _RTC_SEL),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100586};
587
588static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
589static const u8 i2c35_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
590static const u8 i2c46_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER};
591static const u8 uart6_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
592 _HSE_KER};
593static const u8 uart24_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
594 _HSE_KER};
595static const u8 uart35_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
596 _HSE_KER};
597static const u8 uart78_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
598 _HSE_KER};
599static const u8 sdmmc12_parents[] = {_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER};
600static const u8 sdmmc3_parents[] = {_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER};
601static const u8 eth_parents[] = {_PLL4_P, _PLL3_Q};
602static const u8 qspi_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
603static const u8 fmc_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
604static const u8 usbphy_parents[] = {_HSE_KER, _PLL4_R, _HSE_KER_DIV2};
605static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
606static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200607static const u8 dsi_parents[] = {_DSI_PHY, _PLL4_P};
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200608static const u8 adc_parents[] = {_PLL4_R, _CK_PER, _PLL3_Q};
Patrice Chotard248278d2019-04-30 18:08:27 +0200609static const u8 spi_parents[] = {_PLL4_P, _PLL3_Q, _I2S_CKIN, _CK_PER,
610 _PLL3_R};
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100611static const u8 spi45_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
612 _HSE_KER};
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200613static const u8 rtc_parents[] = {_UNKNOWN_ID, _LSE, _LSI, _HSE};
Patrick Delaunaya6151912018-03-12 10:46:15 +0100614
615static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
616 STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
617 STM32MP1_CLK_PARENT(_I2C35_SEL, RCC_I2C35CKSELR, 0, 0x7, i2c35_parents),
618 STM32MP1_CLK_PARENT(_I2C46_SEL, RCC_I2C46CKSELR, 0, 0x7, i2c46_parents),
619 STM32MP1_CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents),
620 STM32MP1_CLK_PARENT(_UART24_SEL, RCC_UART24CKSELR, 0, 0x7,
621 uart24_parents),
622 STM32MP1_CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7,
623 uart35_parents),
624 STM32MP1_CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7,
625 uart78_parents),
626 STM32MP1_CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7,
627 sdmmc12_parents),
628 STM32MP1_CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7,
629 sdmmc3_parents),
630 STM32MP1_CLK_PARENT(_ETH_SEL, RCC_ETHCKSELR, 0, 0x3, eth_parents),
Patrick Delaunay69ffb552020-03-09 14:59:22 +0100631 STM32MP1_CLK_PARENT(_QSPI_SEL, RCC_QSPICKSELR, 0, 0x3, qspi_parents),
632 STM32MP1_CLK_PARENT(_FMC_SEL, RCC_FMCCKSELR, 0, 0x3, fmc_parents),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100633 STM32MP1_CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents),
634 STM32MP1_CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents),
635 STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200636 STM32MP1_CLK_PARENT(_DSI_SEL, RCC_DSICKSELR, 0, 0x1, dsi_parents),
Patrick Delaunay69ffb552020-03-09 14:59:22 +0100637 STM32MP1_CLK_PARENT(_ADC12_SEL, RCC_ADCCKSELR, 0, 0x3, adc_parents),
Patrice Chotard248278d2019-04-30 18:08:27 +0200638 STM32MP1_CLK_PARENT(_SPI1_SEL, RCC_SPI2S1CKSELR, 0, 0x7, spi_parents),
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100639 STM32MP1_CLK_PARENT(_SPI45_SEL, RCC_SPI45CKSELR, 0, 0x7, spi45_parents),
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200640 STM32MP1_CLK_PARENT(_RTC_SEL, RCC_BDCR, RCC_BDCR_RTCSRC_SHIFT,
641 (RCC_BDCR_RTCSRC_MASK >> RCC_BDCR_RTCSRC_SHIFT),
642 rtc_parents),
Patrick Delaunaya6151912018-03-12 10:46:15 +0100643};
644
645#ifdef STM32MP1_CLOCK_TREE_INIT
646/* define characteristic of PLL according type */
647#define DIVN_MIN 24
648static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
649 [PLL_800] = {
650 .refclk_min = 4,
651 .refclk_max = 16,
652 .divn_max = 99,
653 },
654 [PLL_1600] = {
655 .refclk_min = 8,
656 .refclk_max = 16,
657 .divn_max = 199,
658 },
659};
660#endif /* STM32MP1_CLOCK_TREE_INIT */
661
662static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
663 STM32MP1_CLK_PLL(_PLL1, PLL_1600,
664 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
665 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
666 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
667 STM32MP1_CLK_PLL(_PLL2, PLL_1600,
668 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
669 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
670 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
671 STM32MP1_CLK_PLL(_PLL3, PLL_800,
672 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
673 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
674 _HSI, _HSE, _CSI, _UNKNOWN_ID),
675 STM32MP1_CLK_PLL(_PLL4, PLL_800,
676 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
677 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
678 _HSI, _HSE, _CSI, _I2S_CKIN),
679};
680
681/* Prescaler table lookups for clock computation */
682/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
683static const u8 stm32mp1_mcu_div[16] = {
684 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
685};
686
687/* div = /1 /2 /4 /8 /16 : same divider for pmu and apbx*/
688#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
689#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
690static const u8 stm32mp1_mpu_apbx_div[8] = {
691 0, 1, 2, 3, 4, 4, 4, 4
692};
693
694/* div = /1 /2 /3 /4 */
695static const u8 stm32mp1_axi_div[8] = {
696 1, 2, 3, 4, 4, 4, 4, 4
697};
698
Patrick Delaunay8d6310a2019-01-30 13:07:04 +0100699static const __maybe_unused
700char * const stm32mp1_clk_parent_name[_PARENT_NB] = {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100701 [_HSI] = "HSI",
702 [_HSE] = "HSE",
703 [_CSI] = "CSI",
704 [_LSI] = "LSI",
705 [_LSE] = "LSE",
706 [_I2S_CKIN] = "I2S_CKIN",
707 [_HSI_KER] = "HSI_KER",
708 [_HSE_KER] = "HSE_KER",
709 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
710 [_CSI_KER] = "CSI_KER",
711 [_PLL1_P] = "PLL1_P",
712 [_PLL1_Q] = "PLL1_Q",
713 [_PLL1_R] = "PLL1_R",
714 [_PLL2_P] = "PLL2_P",
715 [_PLL2_Q] = "PLL2_Q",
716 [_PLL2_R] = "PLL2_R",
717 [_PLL3_P] = "PLL3_P",
718 [_PLL3_Q] = "PLL3_Q",
719 [_PLL3_R] = "PLL3_R",
720 [_PLL4_P] = "PLL4_P",
721 [_PLL4_Q] = "PLL4_Q",
722 [_PLL4_R] = "PLL4_R",
723 [_ACLK] = "ACLK",
724 [_PCLK1] = "PCLK1",
725 [_PCLK2] = "PCLK2",
726 [_PCLK3] = "PCLK3",
727 [_PCLK4] = "PCLK4",
728 [_PCLK5] = "PCLK5",
729 [_HCLK6] = "KCLK6",
730 [_HCLK2] = "HCLK2",
731 [_CK_PER] = "CK_PER",
732 [_CK_MPU] = "CK_MPU",
733 [_CK_MCU] = "CK_MCU",
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200734 [_USB_PHY_48] = "USB_PHY_48",
735 [_DSI_PHY] = "DSI_PHY_PLL",
Patrick Delaunaya6151912018-03-12 10:46:15 +0100736};
737
Patrick Delaunay8d6310a2019-01-30 13:07:04 +0100738static const __maybe_unused
739char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100740 [_I2C12_SEL] = "I2C12",
741 [_I2C35_SEL] = "I2C35",
742 [_I2C46_SEL] = "I2C46",
743 [_UART6_SEL] = "UART6",
744 [_UART24_SEL] = "UART24",
745 [_UART35_SEL] = "UART35",
746 [_UART78_SEL] = "UART78",
747 [_SDMMC12_SEL] = "SDMMC12",
748 [_SDMMC3_SEL] = "SDMMC3",
749 [_ETH_SEL] = "ETH",
750 [_QSPI_SEL] = "QSPI",
751 [_FMC_SEL] = "FMC",
752 [_USBPHY_SEL] = "USBPHY",
753 [_USBO_SEL] = "USBO",
Patrick Delaunay88fa34d2018-07-16 10:41:43 +0200754 [_STGEN_SEL] = "STGEN",
755 [_DSI_SEL] = "DSI",
Patrick Delaunay5b25eb92018-07-16 10:41:45 +0200756 [_ADC12_SEL] = "ADC12",
Patrice Chotard248278d2019-04-30 18:08:27 +0200757 [_SPI1_SEL] = "SPI1",
Patrick Delaunay0c90e0c2020-03-09 14:59:23 +0100758 [_SPI45_SEL] = "SPI45",
Patrick Delaunayfd7fe1b2019-07-11 12:03:37 +0200759 [_RTC_SEL] = "RTC",
Patrick Delaunaya6151912018-03-12 10:46:15 +0100760};
Patrick Delaunaya6151912018-03-12 10:46:15 +0100761
762static const struct stm32mp1_clk_data stm32mp1_data = {
763 .gate = stm32mp1_clk_gate,
764 .sel = stm32mp1_clk_sel,
765 .pll = stm32mp1_clk_pll,
766 .nb_gate = ARRAY_SIZE(stm32mp1_clk_gate),
767};
768
769static ulong stm32mp1_clk_get_fixed(struct stm32mp1_clk_priv *priv, int idx)
770{
771 if (idx >= NB_OSC) {
772 debug("%s: clk id %d not found\n", __func__, idx);
773 return 0;
774 }
775
Patrick Delaunaya6151912018-03-12 10:46:15 +0100776 return priv->osc[idx];
777}
778
779static int stm32mp1_clk_get_id(struct stm32mp1_clk_priv *priv, unsigned long id)
780{
781 const struct stm32mp1_clk_gate *gate = priv->data->gate;
782 int i, nb_clks = priv->data->nb_gate;
783
784 for (i = 0; i < nb_clks; i++) {
785 if (gate[i].index == id)
786 break;
787 }
788
789 if (i == nb_clks) {
790 printf("%s: clk id %d not found\n", __func__, (u32)id);
791 return -EINVAL;
792 }
793
794 return i;
795}
796
797static int stm32mp1_clk_get_sel(struct stm32mp1_clk_priv *priv,
798 int i)
799{
800 const struct stm32mp1_clk_gate *gate = priv->data->gate;
801
802 if (gate[i].sel > _PARENT_SEL_NB) {
803 printf("%s: parents for clk id %d not found\n",
804 __func__, i);
805 return -EINVAL;
806 }
807
808 return gate[i].sel;
809}
810
811static int stm32mp1_clk_get_fixed_parent(struct stm32mp1_clk_priv *priv,
812 int i)
813{
814 const struct stm32mp1_clk_gate *gate = priv->data->gate;
815
816 if (gate[i].fixed == _UNKNOWN_ID)
817 return -ENOENT;
818
819 return gate[i].fixed;
820}
821
822static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
823 unsigned long id)
824{
825 const struct stm32mp1_clk_sel *sel = priv->data->sel;
826 int i;
827 int s, p;
Patrick Delaunay67d74ce2019-06-21 15:26:48 +0200828 unsigned int idx;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100829
Patrick Delaunay67d74ce2019-06-21 15:26:48 +0200830 for (idx = 0; idx < ARRAY_SIZE(stm32mp1_clks); idx++)
831 if (stm32mp1_clks[idx][0] == id)
832 return stm32mp1_clks[idx][1];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100833
834 i = stm32mp1_clk_get_id(priv, id);
835 if (i < 0)
836 return i;
837
838 p = stm32mp1_clk_get_fixed_parent(priv, i);
839 if (p >= 0 && p < _PARENT_NB)
840 return p;
841
842 s = stm32mp1_clk_get_sel(priv, i);
843 if (s < 0)
844 return s;
845
846 p = (readl(priv->base + sel[s].offset) >> sel[s].src) & sel[s].msk;
847
848 if (p < sel[s].nb_parent) {
849#ifdef DEBUG
850 debug("%s: %s clock is the parent %s of clk id %d\n", __func__,
851 stm32mp1_clk_parent_name[sel[s].parent[p]],
852 stm32mp1_clk_parent_sel_name[s],
853 (u32)id);
854#endif
855 return sel[s].parent[p];
856 }
857
858 pr_err("%s: no parents defined for clk id %d\n",
859 __func__, (u32)id);
860
861 return -EINVAL;
862}
863
Patrick Delaunay61105032018-07-16 10:41:42 +0200864static ulong pll_get_fref_ck(struct stm32mp1_clk_priv *priv,
865 int pll_id)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100866{
867 const struct stm32mp1_clk_pll *pll = priv->data->pll;
Patrick Delaunay61105032018-07-16 10:41:42 +0200868 u32 selr;
869 int src;
870 ulong refclk;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100871
Patrick Delaunay61105032018-07-16 10:41:42 +0200872 /* Get current refclk */
Patrick Delaunaya6151912018-03-12 10:46:15 +0100873 selr = readl(priv->base + pll[pll_id].rckxselr);
Patrick Delaunay61105032018-07-16 10:41:42 +0200874 src = selr & RCC_SELR_SRC_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100875
Patrick Delaunay61105032018-07-16 10:41:42 +0200876 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]);
Patrick Delaunay61105032018-07-16 10:41:42 +0200877
878 return refclk;
879}
880
881/*
882 * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL
883 * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1)
884 * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1)
885 * => in all the case Fpll_y_ck = pll_get_fvco() / (DIVy + 1)
886 */
887static ulong pll_get_fvco(struct stm32mp1_clk_priv *priv,
888 int pll_id)
889{
890 const struct stm32mp1_clk_pll *pll = priv->data->pll;
891 int divm, divn;
892 ulong refclk, fvco;
893 u32 cfgr1, fracr;
894
895 cfgr1 = readl(priv->base + pll[pll_id].pllxcfgr1);
896 fracr = readl(priv->base + pll[pll_id].pllxfracr);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100897
898 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
899 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100900
Patrick Delaunay61105032018-07-16 10:41:42 +0200901 refclk = pll_get_fref_ck(priv, pll_id);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100902
Patrick Delaunay61105032018-07-16 10:41:42 +0200903 /* with FRACV :
904 * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100905 * without FRACV
Patrick Delaunay61105032018-07-16 10:41:42 +0200906 * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100907 */
908 if (fracr & RCC_PLLNFRACR_FRACLE) {
909 u32 fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK)
910 >> RCC_PLLNFRACR_FRACV_SHIFT;
Patrick Delaunay61105032018-07-16 10:41:42 +0200911 fvco = (ulong)lldiv((unsigned long long)refclk *
Patrick Delaunaya6151912018-03-12 10:46:15 +0100912 (((divn + 1) << 13) + fracv),
Patrick Delaunay61105032018-07-16 10:41:42 +0200913 ((unsigned long long)(divm + 1)) << 13);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100914 } else {
Patrick Delaunay61105032018-07-16 10:41:42 +0200915 fvco = (ulong)(refclk * (divn + 1) / (divm + 1));
Patrick Delaunaya6151912018-03-12 10:46:15 +0100916 }
Patrick Delaunay61105032018-07-16 10:41:42 +0200917
918 return fvco;
919}
920
921static ulong stm32mp1_read_pll_freq(struct stm32mp1_clk_priv *priv,
922 int pll_id, int div_id)
923{
924 const struct stm32mp1_clk_pll *pll = priv->data->pll;
925 int divy;
926 ulong dfout;
927 u32 cfgr2;
928
Patrick Delaunay61105032018-07-16 10:41:42 +0200929 if (div_id >= _DIV_NB)
930 return 0;
931
932 cfgr2 = readl(priv->base + pll[pll_id].pllxcfgr2);
933 divy = (cfgr2 >> RCC_PLLNCFGR2_SHIFT(div_id)) & RCC_PLLNCFGR2_DIVX_MASK;
934
Patrick Delaunay61105032018-07-16 10:41:42 +0200935 dfout = pll_get_fvco(priv, pll_id) / (divy + 1);
Patrick Delaunaya6151912018-03-12 10:46:15 +0100936
937 return dfout;
938}
939
940static ulong stm32mp1_clk_get(struct stm32mp1_clk_priv *priv, int p)
941{
942 u32 reg;
943 ulong clock = 0;
944
945 switch (p) {
946 case _CK_MPU:
947 /* MPU sub system */
948 reg = readl(priv->base + RCC_MPCKSELR);
949 switch (reg & RCC_SELR_SRC_MASK) {
950 case RCC_MPCKSELR_HSI:
951 clock = stm32mp1_clk_get_fixed(priv, _HSI);
952 break;
953 case RCC_MPCKSELR_HSE:
954 clock = stm32mp1_clk_get_fixed(priv, _HSE);
955 break;
956 case RCC_MPCKSELR_PLL:
957 case RCC_MPCKSELR_PLL_MPUDIV:
958 clock = stm32mp1_read_pll_freq(priv, _PLL1, _DIV_P);
Lionel Debieve36911fc2020-04-24 15:47:57 +0200959 if ((reg & RCC_SELR_SRC_MASK) ==
960 RCC_MPCKSELR_PLL_MPUDIV) {
Patrick Delaunaya6151912018-03-12 10:46:15 +0100961 reg = readl(priv->base + RCC_MPCKDIVR);
Lionel Debieve36911fc2020-04-24 15:47:57 +0200962 clock >>= stm32mp1_mpu_div[reg &
963 RCC_MPUDIV_MASK];
Patrick Delaunaya6151912018-03-12 10:46:15 +0100964 }
965 break;
966 }
967 break;
968 /* AXI sub system */
969 case _ACLK:
970 case _HCLK2:
971 case _HCLK6:
972 case _PCLK4:
973 case _PCLK5:
974 reg = readl(priv->base + RCC_ASSCKSELR);
975 switch (reg & RCC_SELR_SRC_MASK) {
976 case RCC_ASSCKSELR_HSI:
977 clock = stm32mp1_clk_get_fixed(priv, _HSI);
978 break;
979 case RCC_ASSCKSELR_HSE:
980 clock = stm32mp1_clk_get_fixed(priv, _HSE);
981 break;
982 case RCC_ASSCKSELR_PLL:
983 clock = stm32mp1_read_pll_freq(priv, _PLL2, _DIV_P);
984 break;
985 }
986
987 /* System clock divider */
988 reg = readl(priv->base + RCC_AXIDIVR);
989 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
990
991 switch (p) {
992 case _PCLK4:
993 reg = readl(priv->base + RCC_APB4DIVR);
994 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
995 break;
996 case _PCLK5:
997 reg = readl(priv->base + RCC_APB5DIVR);
998 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
999 break;
1000 default:
1001 break;
1002 }
1003 break;
1004 /* MCU sub system */
1005 case _CK_MCU:
1006 case _PCLK1:
1007 case _PCLK2:
1008 case _PCLK3:
1009 reg = readl(priv->base + RCC_MSSCKSELR);
1010 switch (reg & RCC_SELR_SRC_MASK) {
1011 case RCC_MSSCKSELR_HSI:
1012 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1013 break;
1014 case RCC_MSSCKSELR_HSE:
1015 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1016 break;
1017 case RCC_MSSCKSELR_CSI:
1018 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1019 break;
1020 case RCC_MSSCKSELR_PLL:
1021 clock = stm32mp1_read_pll_freq(priv, _PLL3, _DIV_P);
1022 break;
1023 }
1024
1025 /* MCU clock divider */
1026 reg = readl(priv->base + RCC_MCUDIVR);
1027 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
1028
1029 switch (p) {
1030 case _PCLK1:
1031 reg = readl(priv->base + RCC_APB1DIVR);
1032 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1033 break;
1034 case _PCLK2:
1035 reg = readl(priv->base + RCC_APB2DIVR);
1036 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1037 break;
1038 case _PCLK3:
1039 reg = readl(priv->base + RCC_APB3DIVR);
1040 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
1041 break;
1042 case _CK_MCU:
1043 default:
1044 break;
1045 }
1046 break;
1047 case _CK_PER:
1048 reg = readl(priv->base + RCC_CPERCKSELR);
1049 switch (reg & RCC_SELR_SRC_MASK) {
1050 case RCC_CPERCKSELR_HSI:
1051 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1052 break;
1053 case RCC_CPERCKSELR_HSE:
1054 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1055 break;
1056 case RCC_CPERCKSELR_CSI:
1057 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1058 break;
1059 }
1060 break;
1061 case _HSI:
1062 case _HSI_KER:
1063 clock = stm32mp1_clk_get_fixed(priv, _HSI);
1064 break;
1065 case _CSI:
1066 case _CSI_KER:
1067 clock = stm32mp1_clk_get_fixed(priv, _CSI);
1068 break;
1069 case _HSE:
1070 case _HSE_KER:
1071 case _HSE_KER_DIV2:
1072 clock = stm32mp1_clk_get_fixed(priv, _HSE);
1073 if (p == _HSE_KER_DIV2)
1074 clock >>= 1;
1075 break;
1076 case _LSI:
1077 clock = stm32mp1_clk_get_fixed(priv, _LSI);
1078 break;
1079 case _LSE:
1080 clock = stm32mp1_clk_get_fixed(priv, _LSE);
1081 break;
1082 /* PLL */
1083 case _PLL1_P:
1084 case _PLL1_Q:
1085 case _PLL1_R:
1086 clock = stm32mp1_read_pll_freq(priv, _PLL1, p - _PLL1_P);
1087 break;
1088 case _PLL2_P:
1089 case _PLL2_Q:
1090 case _PLL2_R:
1091 clock = stm32mp1_read_pll_freq(priv, _PLL2, p - _PLL2_P);
1092 break;
1093 case _PLL3_P:
1094 case _PLL3_Q:
1095 case _PLL3_R:
1096 clock = stm32mp1_read_pll_freq(priv, _PLL3, p - _PLL3_P);
1097 break;
1098 case _PLL4_P:
1099 case _PLL4_Q:
1100 case _PLL4_R:
1101 clock = stm32mp1_read_pll_freq(priv, _PLL4, p - _PLL4_P);
1102 break;
1103 /* other */
1104 case _USB_PHY_48:
Patrick Delaunay86617dd2019-01-30 13:07:00 +01001105 clock = 48000000;
Patrick Delaunaya6151912018-03-12 10:46:15 +01001106 break;
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001107 case _DSI_PHY:
1108 {
1109 struct clk clk;
1110 struct udevice *dev = NULL;
Patrick Delaunaya6151912018-03-12 10:46:15 +01001111
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001112 if (!uclass_get_device_by_name(UCLASS_CLK, "ck_dsi_phy",
1113 &dev)) {
1114 if (clk_request(dev, &clk)) {
1115 pr_err("ck_dsi_phy request");
1116 } else {
1117 clk.id = 0;
1118 clock = clk_get_rate(&clk);
1119 }
1120 }
1121 break;
1122 }
Patrick Delaunaya6151912018-03-12 10:46:15 +01001123 default:
1124 break;
1125 }
1126
1127 debug("%s(%d) clock = %lx : %ld kHz\n",
1128 __func__, p, clock, clock / 1000);
1129
1130 return clock;
1131}
1132
1133static int stm32mp1_clk_enable(struct clk *clk)
1134{
1135 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1136 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1137 int i = stm32mp1_clk_get_id(priv, clk->id);
1138
1139 if (i < 0)
1140 return i;
1141
1142 if (gate[i].set_clr)
1143 writel(BIT(gate[i].bit), priv->base + gate[i].offset);
1144 else
1145 setbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1146
1147 debug("%s: id clock %d has been enabled\n", __func__, (u32)clk->id);
1148
1149 return 0;
1150}
1151
1152static int stm32mp1_clk_disable(struct clk *clk)
1153{
1154 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1155 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1156 int i = stm32mp1_clk_get_id(priv, clk->id);
1157
1158 if (i < 0)
1159 return i;
1160
1161 if (gate[i].set_clr)
1162 writel(BIT(gate[i].bit),
1163 priv->base + gate[i].offset
1164 + RCC_MP_ENCLRR_OFFSET);
1165 else
1166 clrbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1167
1168 debug("%s: id clock %d has been disabled\n", __func__, (u32)clk->id);
1169
1170 return 0;
1171}
1172
1173static ulong stm32mp1_clk_get_rate(struct clk *clk)
1174{
1175 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1176 int p = stm32mp1_clk_get_parent(priv, clk->id);
1177 ulong rate;
1178
1179 if (p < 0)
1180 return 0;
1181
1182 rate = stm32mp1_clk_get(priv, p);
1183
1184#ifdef DEBUG
1185 debug("%s: computed rate for id clock %d is %d (parent is %s)\n",
1186 __func__, (u32)clk->id, (u32)rate, stm32mp1_clk_parent_name[p]);
1187#endif
1188 return rate;
1189}
1190
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001191#ifdef STM32MP1_CLOCK_TREE_INIT
1192static void stm32mp1_ls_osc_set(int enable, fdt_addr_t rcc, u32 offset,
1193 u32 mask_on)
1194{
1195 u32 address = rcc + offset;
1196
1197 if (enable)
1198 setbits_le32(address, mask_on);
1199 else
1200 clrbits_le32(address, mask_on);
1201}
1202
1203static void stm32mp1_hs_ocs_set(int enable, fdt_addr_t rcc, u32 mask_on)
1204{
Patrick Delaunay63201282019-01-30 13:07:02 +01001205 writel(mask_on, rcc + (enable ? RCC_OCENSETR : RCC_OCENCLRR));
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001206}
1207
1208static int stm32mp1_osc_wait(int enable, fdt_addr_t rcc, u32 offset,
1209 u32 mask_rdy)
1210{
1211 u32 mask_test = 0;
1212 u32 address = rcc + offset;
1213 u32 val;
1214 int ret;
1215
1216 if (enable)
1217 mask_test = mask_rdy;
1218
1219 ret = readl_poll_timeout(address, val,
1220 (val & mask_rdy) == mask_test,
1221 TIMEOUT_1S);
1222
1223 if (ret)
1224 pr_err("OSC %x @ %x timeout for enable=%d : 0x%x\n",
1225 mask_rdy, address, enable, readl(address));
1226
1227 return ret;
1228}
1229
Patrick Delaunayd2194152018-07-16 10:41:46 +02001230static void stm32mp1_lse_enable(fdt_addr_t rcc, int bypass, int digbyp,
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001231 u32 lsedrv)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001232{
1233 u32 value;
1234
Patrick Delaunayd2194152018-07-16 10:41:46 +02001235 if (digbyp)
1236 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_DIGBYP);
1237
1238 if (bypass || digbyp)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001239 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_LSEBYP);
1240
1241 /*
1242 * warning: not recommended to switch directly from "high drive"
1243 * to "medium low drive", and vice-versa.
1244 */
1245 value = (readl(rcc + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK)
1246 >> RCC_BDCR_LSEDRV_SHIFT;
1247
1248 while (value != lsedrv) {
1249 if (value > lsedrv)
1250 value--;
1251 else
1252 value++;
1253
1254 clrsetbits_le32(rcc + RCC_BDCR,
1255 RCC_BDCR_LSEDRV_MASK,
1256 value << RCC_BDCR_LSEDRV_SHIFT);
1257 }
1258
1259 stm32mp1_ls_osc_set(1, rcc, RCC_BDCR, RCC_BDCR_LSEON);
1260}
1261
1262static void stm32mp1_lse_wait(fdt_addr_t rcc)
1263{
1264 stm32mp1_osc_wait(1, rcc, RCC_BDCR, RCC_BDCR_LSERDY);
1265}
1266
1267static void stm32mp1_lsi_set(fdt_addr_t rcc, int enable)
1268{
1269 stm32mp1_ls_osc_set(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSION);
1270 stm32mp1_osc_wait(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSIRDY);
1271}
1272
Patrick Delaunayd2194152018-07-16 10:41:46 +02001273static void stm32mp1_hse_enable(fdt_addr_t rcc, int bypass, int digbyp, int css)
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001274{
Patrick Delaunayd2194152018-07-16 10:41:46 +02001275 if (digbyp)
Patrick Delaunay63201282019-01-30 13:07:02 +01001276 writel(RCC_OCENR_DIGBYP, rcc + RCC_OCENSETR);
Patrick Delaunayd2194152018-07-16 10:41:46 +02001277 if (bypass || digbyp)
Patrick Delaunay63201282019-01-30 13:07:02 +01001278 writel(RCC_OCENR_HSEBYP, rcc + RCC_OCENSETR);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001279
1280 stm32mp1_hs_ocs_set(1, rcc, RCC_OCENR_HSEON);
1281 stm32mp1_osc_wait(1, rcc, RCC_OCRDYR, RCC_OCRDYR_HSERDY);
1282
1283 if (css)
Patrick Delaunay63201282019-01-30 13:07:02 +01001284 writel(RCC_OCENR_HSECSSON, rcc + RCC_OCENSETR);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001285}
1286
1287static void stm32mp1_csi_set(fdt_addr_t rcc, int enable)
1288{
Patrick Delaunay63201282019-01-30 13:07:02 +01001289 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_CSION);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001290 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_CSIRDY);
1291}
1292
1293static void stm32mp1_hsi_set(fdt_addr_t rcc, int enable)
1294{
1295 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_HSION);
1296 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_HSIRDY);
1297}
1298
1299static int stm32mp1_set_hsidiv(fdt_addr_t rcc, u8 hsidiv)
1300{
1301 u32 address = rcc + RCC_OCRDYR;
1302 u32 val;
1303 int ret;
1304
1305 clrsetbits_le32(rcc + RCC_HSICFGR,
1306 RCC_HSICFGR_HSIDIV_MASK,
1307 RCC_HSICFGR_HSIDIV_MASK & hsidiv);
1308
1309 ret = readl_poll_timeout(address, val,
1310 val & RCC_OCRDYR_HSIDIVRDY,
1311 TIMEOUT_200MS);
1312 if (ret)
1313 pr_err("HSIDIV failed @ 0x%x: 0x%x\n",
1314 address, readl(address));
1315
1316 return ret;
1317}
1318
1319static int stm32mp1_hsidiv(fdt_addr_t rcc, ulong hsifreq)
1320{
1321 u8 hsidiv;
1322 u32 hsidivfreq = MAX_HSI_HZ;
1323
1324 for (hsidiv = 0; hsidiv < 4; hsidiv++,
1325 hsidivfreq = hsidivfreq / 2)
1326 if (hsidivfreq == hsifreq)
1327 break;
1328
1329 if (hsidiv == 4) {
1330 pr_err("clk-hsi frequency invalid");
1331 return -1;
1332 }
1333
1334 if (hsidiv > 0)
1335 return stm32mp1_set_hsidiv(rcc, hsidiv);
1336
1337 return 0;
1338}
1339
1340static void pll_start(struct stm32mp1_clk_priv *priv, int pll_id)
1341{
1342 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1343
Patrick Delaunaybbd108a2019-01-30 13:07:06 +01001344 clrsetbits_le32(priv->base + pll[pll_id].pllxcr,
1345 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN |
1346 RCC_PLLNCR_DIVREN,
1347 RCC_PLLNCR_PLLON);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001348}
1349
1350static int pll_output(struct stm32mp1_clk_priv *priv, int pll_id, int output)
1351{
1352 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1353 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1354 u32 val;
1355 int ret;
1356
1357 ret = readl_poll_timeout(pllxcr, val, val & RCC_PLLNCR_PLLRDY,
1358 TIMEOUT_200MS);
1359
1360 if (ret) {
1361 pr_err("PLL%d start failed @ 0x%x: 0x%x\n",
1362 pll_id, pllxcr, readl(pllxcr));
1363 return ret;
1364 }
1365
1366 /* start the requested output */
1367 setbits_le32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1368
1369 return 0;
1370}
1371
1372static int pll_stop(struct stm32mp1_clk_priv *priv, int pll_id)
1373{
1374 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1375 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1376 u32 val;
1377
1378 /* stop all output */
1379 clrbits_le32(pllxcr,
1380 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
1381
1382 /* stop PLL */
1383 clrbits_le32(pllxcr, RCC_PLLNCR_PLLON);
1384
1385 /* wait PLL stopped */
1386 return readl_poll_timeout(pllxcr, val, (val & RCC_PLLNCR_PLLRDY) == 0,
1387 TIMEOUT_200MS);
1388}
1389
1390static void pll_config_output(struct stm32mp1_clk_priv *priv,
1391 int pll_id, u32 *pllcfg)
1392{
1393 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1394 fdt_addr_t rcc = priv->base;
1395 u32 value;
1396
1397 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT)
1398 & RCC_PLLNCFGR2_DIVP_MASK;
1399 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT)
1400 & RCC_PLLNCFGR2_DIVQ_MASK;
1401 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT)
1402 & RCC_PLLNCFGR2_DIVR_MASK;
1403 writel(value, rcc + pll[pll_id].pllxcfgr2);
1404}
1405
1406static int pll_config(struct stm32mp1_clk_priv *priv, int pll_id,
1407 u32 *pllcfg, u32 fracv)
1408{
1409 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1410 fdt_addr_t rcc = priv->base;
1411 enum stm32mp1_plltype type = pll[pll_id].plltype;
1412 int src;
1413 ulong refclk;
1414 u8 ifrge = 0;
1415 u32 value;
1416
1417 src = readl(priv->base + pll[pll_id].rckxselr) & RCC_SELR_SRC_MASK;
1418
1419 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]) /
1420 (pllcfg[PLLCFG_M] + 1);
1421
1422 if (refclk < (stm32mp1_pll[type].refclk_min * 1000000) ||
1423 refclk > (stm32mp1_pll[type].refclk_max * 1000000)) {
1424 debug("invalid refclk = %x\n", (u32)refclk);
1425 return -EINVAL;
1426 }
1427 if (type == PLL_800 && refclk >= 8000000)
1428 ifrge = 1;
1429
1430 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT)
1431 & RCC_PLLNCFGR1_DIVN_MASK;
1432 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT)
1433 & RCC_PLLNCFGR1_DIVM_MASK;
1434 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT)
1435 & RCC_PLLNCFGR1_IFRGE_MASK;
1436 writel(value, rcc + pll[pll_id].pllxcfgr1);
1437
1438 /* fractional configuration: load sigma-delta modulator (SDM) */
1439
1440 /* Write into FRACV the new fractional value , and FRACLE to 0 */
1441 writel(fracv << RCC_PLLNFRACR_FRACV_SHIFT,
1442 rcc + pll[pll_id].pllxfracr);
1443
1444 /* Write FRACLE to 1 : FRACV value is loaded into the SDM */
1445 setbits_le32(rcc + pll[pll_id].pllxfracr,
1446 RCC_PLLNFRACR_FRACLE);
1447
1448 pll_config_output(priv, pll_id, pllcfg);
1449
1450 return 0;
1451}
1452
1453static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
1454{
1455 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1456 u32 pllxcsg;
1457
1458 pllxcsg = ((csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1459 RCC_PLLNCSGR_MOD_PER_MASK) |
1460 ((csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1461 RCC_PLLNCSGR_INC_STEP_MASK) |
1462 ((csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1463 RCC_PLLNCSGR_SSCG_MODE_MASK);
1464
1465 writel(pllxcsg, priv->base + pll[pll_id].pllxcsgr);
Patrick Delaunaybbd108a2019-01-30 13:07:06 +01001466
1467 setbits_le32(priv->base + pll[pll_id].pllxcr, RCC_PLLNCR_SSCG_CTRL);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001468}
1469
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001470static __maybe_unused int pll_set_rate(struct udevice *dev,
1471 int pll_id,
1472 int div_id,
1473 unsigned long clk_rate)
1474{
1475 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1476 unsigned int pllcfg[PLLCFG_NB];
1477 ofnode plloff;
1478 char name[12];
1479 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1480 enum stm32mp1_plltype type = pll[pll_id].plltype;
1481 int divm, divn, divy;
1482 int ret;
1483 ulong fck_ref;
1484 u32 fracv;
1485 u64 value;
1486
1487 if (div_id > _DIV_NB)
1488 return -EINVAL;
1489
1490 sprintf(name, "st,pll@%d", pll_id);
1491 plloff = dev_read_subnode(dev, name);
1492 if (!ofnode_valid(plloff))
1493 return -FDT_ERR_NOTFOUND;
1494
1495 ret = ofnode_read_u32_array(plloff, "cfg",
1496 pllcfg, PLLCFG_NB);
1497 if (ret < 0)
1498 return -FDT_ERR_NOTFOUND;
1499
1500 fck_ref = pll_get_fref_ck(priv, pll_id);
1501
1502 divm = pllcfg[PLLCFG_M];
1503 /* select output divider = 0: for _DIV_P, 1:_DIV_Q 2:_DIV_R */
1504 divy = pllcfg[PLLCFG_P + div_id];
1505
1506 /* For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
1507 * So same final result than PLL2 et 4
1508 * with FRACV
1509 * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
1510 * / (DIVy + 1) * (DIVM + 1)
1511 * value = (DIVN + 1) * 2^13 + FRACV / 2^13
1512 * = Fck_pll_y (DIVy + 1) * (DIVM + 1) * 2^13 / Fck_ref
1513 */
1514 value = ((u64)clk_rate * (divy + 1) * (divm + 1)) << 13;
1515 value = lldiv(value, fck_ref);
1516
1517 divn = (value >> 13) - 1;
1518 if (divn < DIVN_MIN ||
1519 divn > stm32mp1_pll[type].divn_max) {
1520 pr_err("divn invalid = %d", divn);
1521 return -EINVAL;
1522 }
1523 fracv = value - ((divn + 1) << 13);
1524 pllcfg[PLLCFG_N] = divn;
1525
1526 /* reconfigure PLL */
1527 pll_stop(priv, pll_id);
1528 pll_config(priv, pll_id, pllcfg, fracv);
1529 pll_start(priv, pll_id);
1530 pll_output(priv, pll_id, pllcfg[PLLCFG_O]);
1531
1532 return 0;
1533}
1534
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001535static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
1536{
1537 u32 address = priv->base + (clksrc >> 4);
1538 u32 val;
1539 int ret;
1540
1541 clrsetbits_le32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK);
1542 ret = readl_poll_timeout(address, val, val & RCC_SELR_SRCRDY,
1543 TIMEOUT_200MS);
1544 if (ret)
1545 pr_err("CLKSRC %x start failed @ 0x%x: 0x%x\n",
1546 clksrc, address, readl(address));
1547
1548 return ret;
1549}
1550
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001551static void stgen_config(struct stm32mp1_clk_priv *priv)
1552{
1553 int p;
1554 u32 stgenc, cntfid0;
1555 ulong rate;
1556
Patrick Delaunaydfda7d42019-07-05 17:20:11 +02001557 stgenc = STM32_STGEN_BASE;
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001558 cntfid0 = readl(stgenc + STGENC_CNTFID0);
1559 p = stm32mp1_clk_get_parent(priv, STGEN_K);
1560 rate = stm32mp1_clk_get(priv, p);
1561
1562 if (cntfid0 != rate) {
Patrick Delaunayf3a23c22019-01-30 13:07:03 +01001563 u64 counter;
1564
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001565 pr_debug("System Generic Counter (STGEN) update\n");
1566 clrbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
Patrick Delaunayf3a23c22019-01-30 13:07:03 +01001567 counter = (u64)readl(stgenc + STGENC_CNTCVL);
1568 counter |= ((u64)(readl(stgenc + STGENC_CNTCVU))) << 32;
1569 counter = lldiv(counter * (u64)rate, cntfid0);
1570 writel((u32)counter, stgenc + STGENC_CNTCVL);
1571 writel((u32)(counter >> 32), stgenc + STGENC_CNTCVU);
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001572 writel(rate, stgenc + STGENC_CNTFID0);
1573 setbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1574
1575 __asm__ volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (rate));
1576
1577 /* need to update gd->arch.timer_rate_hz with new frequency */
1578 timer_init();
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001579 }
1580}
1581
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001582static int set_clkdiv(unsigned int clkdiv, u32 address)
1583{
1584 u32 val;
1585 int ret;
1586
1587 clrsetbits_le32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK);
1588 ret = readl_poll_timeout(address, val, val & RCC_DIVR_DIVRDY,
1589 TIMEOUT_200MS);
1590 if (ret)
1591 pr_err("CLKDIV %x start failed @ 0x%x: 0x%x\n",
1592 clkdiv, address, readl(address));
1593
1594 return ret;
1595}
1596
1597static void stm32mp1_mco_csg(struct stm32mp1_clk_priv *priv,
1598 u32 clksrc, u32 clkdiv)
1599{
1600 u32 address = priv->base + (clksrc >> 4);
1601
1602 /*
1603 * binding clksrc : bit15-4 offset
1604 * bit3: disable
1605 * bit2-0: MCOSEL[2:0]
1606 */
1607 if (clksrc & 0x8) {
1608 clrbits_le32(address, RCC_MCOCFG_MCOON);
1609 } else {
1610 clrsetbits_le32(address,
1611 RCC_MCOCFG_MCOSRC_MASK,
1612 clksrc & RCC_MCOCFG_MCOSRC_MASK);
1613 clrsetbits_le32(address,
1614 RCC_MCOCFG_MCODIV_MASK,
1615 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
1616 setbits_le32(address, RCC_MCOCFG_MCOON);
1617 }
1618}
1619
1620static void set_rtcsrc(struct stm32mp1_clk_priv *priv,
1621 unsigned int clksrc,
1622 int lse_css)
1623{
1624 u32 address = priv->base + RCC_BDCR;
1625
1626 if (readl(address) & RCC_BDCR_RTCCKEN)
1627 goto skip_rtc;
1628
1629 if (clksrc == CLK_RTC_DISABLED)
1630 goto skip_rtc;
1631
1632 clrsetbits_le32(address,
1633 RCC_BDCR_RTCSRC_MASK,
1634 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1635
1636 setbits_le32(address, RCC_BDCR_RTCCKEN);
1637
1638skip_rtc:
1639 if (lse_css)
1640 setbits_le32(address, RCC_BDCR_LSECSSON);
1641}
1642
1643static void pkcs_config(struct stm32mp1_clk_priv *priv, u32 pkcs)
1644{
1645 u32 address = priv->base + ((pkcs >> 4) & 0xFFF);
1646 u32 value = pkcs & 0xF;
1647 u32 mask = 0xF;
1648
1649 if (pkcs & BIT(31)) {
1650 mask <<= 4;
1651 value <<= 4;
1652 }
1653 clrsetbits_le32(address, mask, value);
1654}
1655
1656static int stm32mp1_clktree(struct udevice *dev)
1657{
1658 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1659 fdt_addr_t rcc = priv->base;
1660 unsigned int clksrc[CLKSRC_NB];
1661 unsigned int clkdiv[CLKDIV_NB];
1662 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1663 ofnode plloff[_PLL_NB];
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001664 int ret, len;
1665 uint i;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001666 int lse_css = 0;
1667 const u32 *pkcs_cell;
1668
1669 /* check mandatory field */
1670 ret = dev_read_u32_array(dev, "st,clksrc", clksrc, CLKSRC_NB);
1671 if (ret < 0) {
1672 debug("field st,clksrc invalid: error %d\n", ret);
1673 return -FDT_ERR_NOTFOUND;
1674 }
1675
1676 ret = dev_read_u32_array(dev, "st,clkdiv", clkdiv, CLKDIV_NB);
1677 if (ret < 0) {
1678 debug("field st,clkdiv invalid: error %d\n", ret);
1679 return -FDT_ERR_NOTFOUND;
1680 }
1681
1682 /* check mandatory field in each pll */
1683 for (i = 0; i < _PLL_NB; i++) {
1684 char name[12];
1685
1686 sprintf(name, "st,pll@%d", i);
1687 plloff[i] = dev_read_subnode(dev, name);
1688 if (!ofnode_valid(plloff[i]))
1689 continue;
1690 ret = ofnode_read_u32_array(plloff[i], "cfg",
1691 pllcfg[i], PLLCFG_NB);
1692 if (ret < 0) {
1693 debug("field cfg invalid: error %d\n", ret);
1694 return -FDT_ERR_NOTFOUND;
1695 }
1696 }
1697
1698 debug("configuration MCO\n");
1699 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1700 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1701
1702 debug("switch ON osillator\n");
1703 /*
1704 * switch ON oscillator found in device-tree,
1705 * HSI already ON after bootrom
1706 */
1707 if (priv->osc[_LSI])
1708 stm32mp1_lsi_set(rcc, 1);
1709
1710 if (priv->osc[_LSE]) {
Patrick Delaunayeb49dce2020-01-28 10:44:15 +01001711 int bypass, digbyp;
1712 u32 lsedrv;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001713 struct udevice *dev = priv->osc_dev[_LSE];
1714
1715 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunayd2194152018-07-16 10:41:46 +02001716 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001717 lse_css = dev_read_bool(dev, "st,css");
1718 lsedrv = dev_read_u32_default(dev, "st,drive",
1719 LSEDRV_MEDIUM_HIGH);
1720
Patrick Delaunayd2194152018-07-16 10:41:46 +02001721 stm32mp1_lse_enable(rcc, bypass, digbyp, lsedrv);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001722 }
1723
1724 if (priv->osc[_HSE]) {
Patrick Delaunayd2194152018-07-16 10:41:46 +02001725 int bypass, digbyp, css;
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001726 struct udevice *dev = priv->osc_dev[_HSE];
1727
1728 bypass = dev_read_bool(dev, "st,bypass");
Patrick Delaunayd2194152018-07-16 10:41:46 +02001729 digbyp = dev_read_bool(dev, "st,digbypass");
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001730 css = dev_read_bool(dev, "st,css");
1731
Patrick Delaunayd2194152018-07-16 10:41:46 +02001732 stm32mp1_hse_enable(rcc, bypass, digbyp, css);
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001733 }
1734 /* CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1735 * => switch on CSI even if node is not present in device tree
1736 */
1737 stm32mp1_csi_set(rcc, 1);
1738
1739 /* come back to HSI */
1740 debug("come back to HSI\n");
1741 set_clksrc(priv, CLK_MPU_HSI);
1742 set_clksrc(priv, CLK_AXI_HSI);
1743 set_clksrc(priv, CLK_MCU_HSI);
1744
1745 debug("pll stop\n");
1746 for (i = 0; i < _PLL_NB; i++)
1747 pll_stop(priv, i);
1748
1749 /* configure HSIDIV */
1750 debug("configure HSIDIV\n");
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001751 if (priv->osc[_HSI]) {
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001752 stm32mp1_hsidiv(rcc, priv->osc[_HSI]);
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001753 stgen_config(priv);
1754 }
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001755
1756 /* select DIV */
1757 debug("select DIV\n");
1758 /* no ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
1759 writel(clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK, rcc + RCC_MPCKDIVR);
1760 set_clkdiv(clkdiv[CLKDIV_AXI], rcc + RCC_AXIDIVR);
1761 set_clkdiv(clkdiv[CLKDIV_APB4], rcc + RCC_APB4DIVR);
1762 set_clkdiv(clkdiv[CLKDIV_APB5], rcc + RCC_APB5DIVR);
1763 set_clkdiv(clkdiv[CLKDIV_MCU], rcc + RCC_MCUDIVR);
1764 set_clkdiv(clkdiv[CLKDIV_APB1], rcc + RCC_APB1DIVR);
1765 set_clkdiv(clkdiv[CLKDIV_APB2], rcc + RCC_APB2DIVR);
1766 set_clkdiv(clkdiv[CLKDIV_APB3], rcc + RCC_APB3DIVR);
1767
1768 /* no ready bit for RTC */
1769 writel(clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK, rcc + RCC_RTCDIVR);
1770
1771 /* configure PLLs source */
1772 debug("configure PLLs source\n");
1773 set_clksrc(priv, clksrc[CLKSRC_PLL12]);
1774 set_clksrc(priv, clksrc[CLKSRC_PLL3]);
1775 set_clksrc(priv, clksrc[CLKSRC_PLL4]);
1776
1777 /* configure and start PLLs */
1778 debug("configure PLLs\n");
1779 for (i = 0; i < _PLL_NB; i++) {
1780 u32 fracv;
1781 u32 csg[PLLCSG_NB];
1782
1783 debug("configure PLL %d @ %d\n", i,
1784 ofnode_to_offset(plloff[i]));
1785 if (!ofnode_valid(plloff[i]))
1786 continue;
1787
1788 fracv = ofnode_read_u32_default(plloff[i], "frac", 0);
1789 pll_config(priv, i, pllcfg[i], fracv);
1790 ret = ofnode_read_u32_array(plloff[i], "csg", csg, PLLCSG_NB);
1791 if (!ret) {
1792 pll_csg(priv, i, csg);
1793 } else if (ret != -FDT_ERR_NOTFOUND) {
1794 debug("invalid csg node for pll@%d res=%d\n", i, ret);
1795 return ret;
1796 }
1797 pll_start(priv, i);
1798 }
1799
1800 /* wait and start PLLs ouptut when ready */
1801 for (i = 0; i < _PLL_NB; i++) {
1802 if (!ofnode_valid(plloff[i]))
1803 continue;
1804 debug("output PLL %d\n", i);
1805 pll_output(priv, i, pllcfg[i][PLLCFG_O]);
1806 }
1807
1808 /* wait LSE ready before to use it */
1809 if (priv->osc[_LSE])
1810 stm32mp1_lse_wait(rcc);
1811
1812 /* configure with expected clock source */
1813 debug("CLKSRC\n");
1814 set_clksrc(priv, clksrc[CLKSRC_MPU]);
1815 set_clksrc(priv, clksrc[CLKSRC_AXI]);
1816 set_clksrc(priv, clksrc[CLKSRC_MCU]);
1817 set_rtcsrc(priv, clksrc[CLKSRC_RTC], lse_css);
1818
1819 /* configure PKCK */
1820 debug("PKCK\n");
1821 pkcs_cell = dev_read_prop(dev, "st,pkcs", &len);
1822 if (pkcs_cell) {
1823 bool ckper_disabled = false;
1824
1825 for (i = 0; i < len / sizeof(u32); i++) {
1826 u32 pkcs = (u32)fdt32_to_cpu(pkcs_cell[i]);
1827
1828 if (pkcs == CLK_CKPER_DISABLED) {
1829 ckper_disabled = true;
1830 continue;
1831 }
1832 pkcs_config(priv, pkcs);
1833 }
1834 /* CKPER is source for some peripheral clock
1835 * (FMC-NAND / QPSI-NOR) and switching source is allowed
1836 * only if previous clock is still ON
1837 * => deactivated CKPER only after switching clock
1838 */
1839 if (ckper_disabled)
1840 pkcs_config(priv, CLK_CKPER_DISABLED);
1841 }
1842
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001843 /* STGEN clock source can change with CLK_STGEN_XXX */
1844 stgen_config(priv);
1845
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001846 debug("oscillator off\n");
1847 /* switch OFF HSI if not found in device-tree */
1848 if (!priv->osc[_HSI])
1849 stm32mp1_hsi_set(rcc, 0);
1850
1851 /* Software Self-Refresh mode (SSR) during DDR initilialization */
1852 clrsetbits_le32(priv->base + RCC_DDRITFCR,
1853 RCC_DDRITFCR_DDRCKMOD_MASK,
1854 RCC_DDRITFCR_DDRCKMOD_SSR <<
1855 RCC_DDRITFCR_DDRCKMOD_SHIFT);
1856
1857 return 0;
1858}
1859#endif /* STM32MP1_CLOCK_TREE_INIT */
1860
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001861static int pll_set_output_rate(struct udevice *dev,
1862 int pll_id,
1863 int div_id,
1864 unsigned long clk_rate)
1865{
1866 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1867 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1868 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1869 int div;
1870 ulong fvco;
1871
1872 if (div_id > _DIV_NB)
1873 return -EINVAL;
1874
1875 fvco = pll_get_fvco(priv, pll_id);
1876
1877 if (fvco <= clk_rate)
1878 div = 1;
1879 else
1880 div = DIV_ROUND_UP(fvco, clk_rate);
1881
1882 if (div > 128)
1883 div = 128;
1884
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001885 /* stop the requested output */
1886 clrbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
1887 /* change divider */
1888 clrsetbits_le32(priv->base + pll[pll_id].pllxcfgr2,
1889 RCC_PLLNCFGR2_DIVX_MASK << RCC_PLLNCFGR2_SHIFT(div_id),
1890 (div - 1) << RCC_PLLNCFGR2_SHIFT(div_id));
1891 /* start the requested output */
1892 setbits_le32(pllxcr, 0x1 << div_id << RCC_PLLNCR_DIVEN_SHIFT);
1893
1894 return 0;
1895}
1896
1897static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
1898{
1899 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1900 int p;
1901
1902 switch (clk->id) {
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001903#if defined(STM32MP1_CLOCK_TREE_INIT) && \
1904 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
1905 case DDRPHYC:
1906 break;
1907#endif
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001908 case LTDC_PX:
1909 case DSI_PX:
1910 break;
1911 default:
1912 pr_err("not supported");
1913 return -EINVAL;
1914 }
1915
1916 p = stm32mp1_clk_get_parent(priv, clk->id);
Patrick Delaunay7879a7d2019-07-30 19:16:54 +02001917#ifdef DEBUG
1918 debug("%s: parent = %d:%s\n", __func__, p, stm32mp1_clk_parent_name[p]);
1919#endif
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001920 if (p < 0)
1921 return -EINVAL;
1922
1923 switch (p) {
Patrick Delaunayc3e828b2019-04-18 17:32:48 +02001924#if defined(STM32MP1_CLOCK_TREE_INIT) && \
1925 defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
1926 case _PLL2_R: /* DDRPHYC */
1927 {
1928 /* only for change DDR clock in interactive mode */
1929 ulong result;
1930
1931 set_clksrc(priv, CLK_AXI_HSI);
1932 result = pll_set_rate(clk->dev, _PLL2, _DIV_R, clk_rate);
1933 set_clksrc(priv, CLK_AXI_PLL2P);
1934 return result;
1935 }
1936#endif
Patrick Delaunay7879a7d2019-07-30 19:16:54 +02001937
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02001938 case _PLL4_Q:
1939 /* for LTDC_PX and DSI_PX case */
1940 return pll_set_output_rate(clk->dev, _PLL4, _DIV_Q, clk_rate);
1941 }
1942
1943 return -EINVAL;
1944}
1945
Patrick Delaunaya6151912018-03-12 10:46:15 +01001946static void stm32mp1_osc_clk_init(const char *name,
1947 struct stm32mp1_clk_priv *priv,
1948 int index)
1949{
1950 struct clk clk;
1951 struct udevice *dev = NULL;
1952
1953 priv->osc[index] = 0;
1954 clk.id = 0;
1955 if (!uclass_get_device_by_name(UCLASS_CLK, name, &dev)) {
1956 if (clk_request(dev, &clk))
1957 pr_err("%s request", name);
1958 else
1959 priv->osc[index] = clk_get_rate(&clk);
1960 }
1961 priv->osc_dev[index] = dev;
1962}
1963
1964static void stm32mp1_osc_init(struct udevice *dev)
1965{
1966 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1967 int i;
1968 const char *name[NB_OSC] = {
1969 [_LSI] = "clk-lsi",
1970 [_LSE] = "clk-lse",
1971 [_HSI] = "clk-hsi",
1972 [_HSE] = "clk-hse",
1973 [_CSI] = "clk-csi",
1974 [_I2S_CKIN] = "i2s_ckin",
Patrick Delaunay86617dd2019-01-30 13:07:00 +01001975 };
Patrick Delaunaya6151912018-03-12 10:46:15 +01001976
1977 for (i = 0; i < NB_OSC; i++) {
1978 stm32mp1_osc_clk_init(name[i], priv, i);
1979 debug("%d: %s => %x\n", i, name[i], (u32)priv->osc[i]);
1980 }
1981}
1982
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01001983static void __maybe_unused stm32mp1_clk_dump(struct stm32mp1_clk_priv *priv)
1984{
1985 char buf[32];
1986 int i, s, p;
1987
1988 printf("Clocks:\n");
1989 for (i = 0; i < _PARENT_NB; i++) {
1990 printf("- %s : %s MHz\n",
1991 stm32mp1_clk_parent_name[i],
1992 strmhz(buf, stm32mp1_clk_get(priv, i)));
1993 }
1994 printf("Source Clocks:\n");
1995 for (i = 0; i < _PARENT_SEL_NB; i++) {
1996 p = (readl(priv->base + priv->data->sel[i].offset) >>
1997 priv->data->sel[i].src) & priv->data->sel[i].msk;
1998 if (p < priv->data->sel[i].nb_parent) {
1999 s = priv->data->sel[i].parent[p];
2000 printf("- %s(%d) => parent %s(%d)\n",
2001 stm32mp1_clk_parent_sel_name[i], i,
2002 stm32mp1_clk_parent_name[s], s);
2003 } else {
2004 printf("- %s(%d) => parent index %d is invalid\n",
2005 stm32mp1_clk_parent_sel_name[i], i, p);
2006 }
2007 }
2008}
2009
2010#ifdef CONFIG_CMD_CLK
2011int soc_clk_dump(void)
2012{
2013 struct udevice *dev;
2014 struct stm32mp1_clk_priv *priv;
2015 int ret;
2016
2017 ret = uclass_get_device_by_driver(UCLASS_CLK,
2018 DM_GET_DRIVER(stm32mp1_clock),
2019 &dev);
2020 if (ret)
2021 return ret;
2022
2023 priv = dev_get_priv(dev);
2024
2025 stm32mp1_clk_dump(priv);
2026
2027 return 0;
2028}
2029#endif
2030
Patrick Delaunaya6151912018-03-12 10:46:15 +01002031static int stm32mp1_clk_probe(struct udevice *dev)
2032{
2033 int result = 0;
2034 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
2035
2036 priv->base = dev_read_addr(dev->parent);
2037 if (priv->base == FDT_ADDR_T_NONE)
2038 return -EINVAL;
2039
2040 priv->data = (void *)&stm32mp1_data;
2041
2042 if (!priv->data->gate || !priv->data->sel ||
2043 !priv->data->pll)
2044 return -EINVAL;
2045
2046 stm32mp1_osc_init(dev);
2047
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01002048#ifdef STM32MP1_CLOCK_TREE_INIT
2049 /* clock tree init is done only one time, before relocation */
2050 if (!(gd->flags & GD_FLG_RELOC))
2051 result = stm32mp1_clktree(dev);
2052#endif
2053
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002054#ifndef CONFIG_SPL_BUILD
2055#if defined(DEBUG)
2056 /* display debug information for probe after relocation */
2057 if (gd->flags & GD_FLG_RELOC)
2058 stm32mp1_clk_dump(priv);
2059#endif
2060
Patrick Delaunay4de076e2019-07-30 19:16:55 +02002061 gd->cpu_clk = stm32mp1_clk_get(priv, _CK_MPU);
2062 gd->bus_clk = stm32mp1_clk_get(priv, _ACLK);
2063 /* DDRPHYC father */
2064 gd->mem_clk = stm32mp1_clk_get(priv, _PLL2_R);
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002065#if defined(CONFIG_DISPLAY_CPUINFO)
2066 if (gd->flags & GD_FLG_RELOC) {
2067 char buf[32];
2068
2069 printf("Clocks:\n");
Patrick Delaunay4de076e2019-07-30 19:16:55 +02002070 printf("- MPU : %s MHz\n", strmhz(buf, gd->cpu_clk));
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002071 printf("- MCU : %s MHz\n",
2072 strmhz(buf, stm32mp1_clk_get(priv, _CK_MCU)));
Patrick Delaunay4de076e2019-07-30 19:16:55 +02002073 printf("- AXI : %s MHz\n", strmhz(buf, gd->bus_clk));
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002074 printf("- PER : %s MHz\n",
2075 strmhz(buf, stm32mp1_clk_get(priv, _CK_PER)));
Patrick Delaunay4de076e2019-07-30 19:16:55 +02002076 printf("- DDR : %s MHz\n", strmhz(buf, gd->mem_clk));
Patrick Delaunay8d6310a2019-01-30 13:07:04 +01002077 }
2078#endif /* CONFIG_DISPLAY_CPUINFO */
2079#endif
2080
Patrick Delaunaya6151912018-03-12 10:46:15 +01002081 return result;
2082}
2083
2084static const struct clk_ops stm32mp1_clk_ops = {
2085 .enable = stm32mp1_clk_enable,
2086 .disable = stm32mp1_clk_disable,
2087 .get_rate = stm32mp1_clk_get_rate,
Patrick Delaunay88fa34d2018-07-16 10:41:43 +02002088 .set_rate = stm32mp1_clk_set_rate,
Patrick Delaunaya6151912018-03-12 10:46:15 +01002089};
2090
Patrick Delaunaya6151912018-03-12 10:46:15 +01002091U_BOOT_DRIVER(stm32mp1_clock) = {
2092 .name = "stm32mp1_clk",
2093 .id = UCLASS_CLK,
Patrick Delaunaya6151912018-03-12 10:46:15 +01002094 .ops = &stm32mp1_clk_ops,
2095 .priv_auto_alloc_size = sizeof(struct stm32mp1_clk_priv),
2096 .probe = stm32mp1_clk_probe,
2097};