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