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