blob: 10e3836257a3bc3886484fa85eb733ad4b2cb320 [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>
13#include <linux/io.h>
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010014#include <linux/iopoll.h>
Patrick Delaunaya6151912018-03-12 10:46:15 +010015#include <dt-bindings/clock/stm32mp1-clks.h>
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010016#include <dt-bindings/clock/stm32mp1-clksrc.h>
17
18#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
19/* activate clock tree initialization in the driver */
20#define STM32MP1_CLOCK_TREE_INIT
21#endif
Patrick Delaunaya6151912018-03-12 10:46:15 +010022
23#define MAX_HSI_HZ 64000000
24
Patrick Delaunay266fa4d2018-03-12 10:46:16 +010025/* TIMEOUT */
26#define TIMEOUT_200MS 200000
27#define TIMEOUT_1S 1000000
28
Patrick Delaunay938e0e32018-03-20 11:41:25 +010029/* STGEN registers */
30#define STGENC_CNTCR 0x00
31#define STGENC_CNTSR 0x04
32#define STGENC_CNTCVL 0x08
33#define STGENC_CNTCVU 0x0C
34#define STGENC_CNTFID0 0x20
35
36#define STGENC_CNTCR_EN BIT(0)
37
Patrick Delaunaya6151912018-03-12 10:46:15 +010038/* RCC registers */
39#define RCC_OCENSETR 0x0C
40#define RCC_OCENCLRR 0x10
41#define RCC_HSICFGR 0x18
42#define RCC_MPCKSELR 0x20
43#define RCC_ASSCKSELR 0x24
44#define RCC_RCK12SELR 0x28
45#define RCC_MPCKDIVR 0x2C
46#define RCC_AXIDIVR 0x30
47#define RCC_APB4DIVR 0x3C
48#define RCC_APB5DIVR 0x40
49#define RCC_RTCDIVR 0x44
50#define RCC_MSSCKSELR 0x48
51#define RCC_PLL1CR 0x80
52#define RCC_PLL1CFGR1 0x84
53#define RCC_PLL1CFGR2 0x88
54#define RCC_PLL1FRACR 0x8C
55#define RCC_PLL1CSGR 0x90
56#define RCC_PLL2CR 0x94
57#define RCC_PLL2CFGR1 0x98
58#define RCC_PLL2CFGR2 0x9C
59#define RCC_PLL2FRACR 0xA0
60#define RCC_PLL2CSGR 0xA4
61#define RCC_I2C46CKSELR 0xC0
62#define RCC_CPERCKSELR 0xD0
63#define RCC_STGENCKSELR 0xD4
64#define RCC_DDRITFCR 0xD8
65#define RCC_BDCR 0x140
66#define RCC_RDLSICR 0x144
67#define RCC_MP_APB4ENSETR 0x200
68#define RCC_MP_APB5ENSETR 0x208
69#define RCC_MP_AHB5ENSETR 0x210
70#define RCC_MP_AHB6ENSETR 0x218
71#define RCC_OCRDYR 0x808
72#define RCC_DBGCFGR 0x80C
73#define RCC_RCK3SELR 0x820
74#define RCC_RCK4SELR 0x824
75#define RCC_MCUDIVR 0x830
76#define RCC_APB1DIVR 0x834
77#define RCC_APB2DIVR 0x838
78#define RCC_APB3DIVR 0x83C
79#define RCC_PLL3CR 0x880
80#define RCC_PLL3CFGR1 0x884
81#define RCC_PLL3CFGR2 0x888
82#define RCC_PLL3FRACR 0x88C
83#define RCC_PLL3CSGR 0x890
84#define RCC_PLL4CR 0x894
85#define RCC_PLL4CFGR1 0x898
86#define RCC_PLL4CFGR2 0x89C
87#define RCC_PLL4FRACR 0x8A0
88#define RCC_PLL4CSGR 0x8A4
89#define RCC_I2C12CKSELR 0x8C0
90#define RCC_I2C35CKSELR 0x8C4
91#define RCC_UART6CKSELR 0x8E4
92#define RCC_UART24CKSELR 0x8E8
93#define RCC_UART35CKSELR 0x8EC
94#define RCC_UART78CKSELR 0x8F0
95#define RCC_SDMMC12CKSELR 0x8F4
96#define RCC_SDMMC3CKSELR 0x8F8
97#define RCC_ETHCKSELR 0x8FC
98#define RCC_QSPICKSELR 0x900
99#define RCC_FMCCKSELR 0x904
100#define RCC_USBCKSELR 0x91C
101#define RCC_MP_APB1ENSETR 0xA00
102#define RCC_MP_APB2ENSETR 0XA08
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200103#define RCC_MP_APB3ENSETR 0xA10
Patrick Delaunaya6151912018-03-12 10:46:15 +0100104#define RCC_MP_AHB2ENSETR 0xA18
105#define RCC_MP_AHB4ENSETR 0xA28
106
107/* used for most of SELR register */
108#define RCC_SELR_SRC_MASK GENMASK(2, 0)
109#define RCC_SELR_SRCRDY BIT(31)
110
111/* Values of RCC_MPCKSELR register */
112#define RCC_MPCKSELR_HSI 0
113#define RCC_MPCKSELR_HSE 1
114#define RCC_MPCKSELR_PLL 2
115#define RCC_MPCKSELR_PLL_MPUDIV 3
116
117/* Values of RCC_ASSCKSELR register */
118#define RCC_ASSCKSELR_HSI 0
119#define RCC_ASSCKSELR_HSE 1
120#define RCC_ASSCKSELR_PLL 2
121
122/* Values of RCC_MSSCKSELR register */
123#define RCC_MSSCKSELR_HSI 0
124#define RCC_MSSCKSELR_HSE 1
125#define RCC_MSSCKSELR_CSI 2
126#define RCC_MSSCKSELR_PLL 3
127
128/* Values of RCC_CPERCKSELR register */
129#define RCC_CPERCKSELR_HSI 0
130#define RCC_CPERCKSELR_CSI 1
131#define RCC_CPERCKSELR_HSE 2
132
133/* used for most of DIVR register : max div for RTC */
134#define RCC_DIVR_DIV_MASK GENMASK(5, 0)
135#define RCC_DIVR_DIVRDY BIT(31)
136
137/* Masks for specific DIVR registers */
138#define RCC_APBXDIV_MASK GENMASK(2, 0)
139#define RCC_MPUDIV_MASK GENMASK(2, 0)
140#define RCC_AXIDIV_MASK GENMASK(2, 0)
141#define RCC_MCUDIV_MASK GENMASK(3, 0)
142
143/* offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */
144#define RCC_MP_ENCLRR_OFFSET 4
145
146/* Fields of RCC_BDCR register */
147#define RCC_BDCR_LSEON BIT(0)
148#define RCC_BDCR_LSEBYP BIT(1)
149#define RCC_BDCR_LSERDY BIT(2)
150#define RCC_BDCR_LSEDRV_MASK GENMASK(5, 4)
151#define RCC_BDCR_LSEDRV_SHIFT 4
152#define RCC_BDCR_LSECSSON BIT(8)
153#define RCC_BDCR_RTCCKEN BIT(20)
154#define RCC_BDCR_RTCSRC_MASK GENMASK(17, 16)
155#define RCC_BDCR_RTCSRC_SHIFT 16
156
157/* Fields of RCC_RDLSICR register */
158#define RCC_RDLSICR_LSION BIT(0)
159#define RCC_RDLSICR_LSIRDY BIT(1)
160
161/* used for ALL PLLNCR registers */
162#define RCC_PLLNCR_PLLON BIT(0)
163#define RCC_PLLNCR_PLLRDY BIT(1)
164#define RCC_PLLNCR_DIVPEN BIT(4)
165#define RCC_PLLNCR_DIVQEN BIT(5)
166#define RCC_PLLNCR_DIVREN BIT(6)
167#define RCC_PLLNCR_DIVEN_SHIFT 4
168
169/* used for ALL PLLNCFGR1 registers */
170#define RCC_PLLNCFGR1_DIVM_SHIFT 16
171#define RCC_PLLNCFGR1_DIVM_MASK GENMASK(21, 16)
172#define RCC_PLLNCFGR1_DIVN_SHIFT 0
173#define RCC_PLLNCFGR1_DIVN_MASK GENMASK(8, 0)
174/* only for PLL3 and PLL4 */
175#define RCC_PLLNCFGR1_IFRGE_SHIFT 24
176#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK(25, 24)
177
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200178/* used for ALL PLLNCFGR2 registers , using stm32mp1_div_id */
179#define RCC_PLLNCFGR2_SHIFT(div_id) ((div_id) * 8)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100180#define RCC_PLLNCFGR2_DIVX_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200181#define RCC_PLLNCFGR2_DIVP_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_P)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100182#define RCC_PLLNCFGR2_DIVP_MASK GENMASK(6, 0)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200183#define RCC_PLLNCFGR2_DIVQ_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_Q)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100184#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK(14, 8)
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200185#define RCC_PLLNCFGR2_DIVR_SHIFT RCC_PLLNCFGR2_SHIFT(_DIV_R)
Patrick Delaunaya6151912018-03-12 10:46:15 +0100186#define RCC_PLLNCFGR2_DIVR_MASK GENMASK(22, 16)
187
188/* used for ALL PLLNFRACR registers */
189#define RCC_PLLNFRACR_FRACV_SHIFT 3
190#define RCC_PLLNFRACR_FRACV_MASK GENMASK(15, 3)
191#define RCC_PLLNFRACR_FRACLE BIT(16)
192
193/* used for ALL PLLNCSGR registers */
194#define RCC_PLLNCSGR_INC_STEP_SHIFT 16
195#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK(30, 16)
196#define RCC_PLLNCSGR_MOD_PER_SHIFT 0
197#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK(12, 0)
198#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15
199#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15)
200
201/* used for RCC_OCENSETR and RCC_OCENCLRR registers */
202#define RCC_OCENR_HSION BIT(0)
203#define RCC_OCENR_CSION BIT(4)
204#define RCC_OCENR_HSEON BIT(8)
205#define RCC_OCENR_HSEBYP BIT(10)
206#define RCC_OCENR_HSECSSON BIT(11)
207
208/* Fields of RCC_OCRDYR register */
209#define RCC_OCRDYR_HSIRDY BIT(0)
210#define RCC_OCRDYR_HSIDIVRDY BIT(2)
211#define RCC_OCRDYR_CSIRDY BIT(4)
212#define RCC_OCRDYR_HSERDY BIT(8)
213
214/* Fields of DDRITFCR register */
215#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
216#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20
217#define RCC_DDRITFCR_DDRCKMOD_SSR 0
218
219/* Fields of RCC_HSICFGR register */
220#define RCC_HSICFGR_HSIDIV_MASK GENMASK(1, 0)
221
222/* used for MCO related operations */
223#define RCC_MCOCFG_MCOON BIT(12)
224#define RCC_MCOCFG_MCODIV_MASK GENMASK(7, 4)
225#define RCC_MCOCFG_MCODIV_SHIFT 4
226#define RCC_MCOCFG_MCOSRC_MASK GENMASK(2, 0)
227
228enum stm32mp1_parent_id {
229/*
230 * _HSI, _HSE, _CSI, _LSI, _LSE should not be moved
231 * they are used as index in osc[] as entry point
232 */
233 _HSI,
234 _HSE,
235 _CSI,
236 _LSI,
237 _LSE,
238 _I2S_CKIN,
239 _USB_PHY_48,
240 NB_OSC,
241
242/* other parent source */
243 _HSI_KER = NB_OSC,
244 _HSE_KER,
245 _HSE_KER_DIV2,
246 _CSI_KER,
247 _PLL1_P,
248 _PLL1_Q,
249 _PLL1_R,
250 _PLL2_P,
251 _PLL2_Q,
252 _PLL2_R,
253 _PLL3_P,
254 _PLL3_Q,
255 _PLL3_R,
256 _PLL4_P,
257 _PLL4_Q,
258 _PLL4_R,
259 _ACLK,
260 _PCLK1,
261 _PCLK2,
262 _PCLK3,
263 _PCLK4,
264 _PCLK5,
265 _HCLK6,
266 _HCLK2,
267 _CK_PER,
268 _CK_MPU,
269 _CK_MCU,
270 _PARENT_NB,
271 _UNKNOWN_ID = 0xff,
272};
273
274enum stm32mp1_parent_sel {
275 _I2C12_SEL,
276 _I2C35_SEL,
277 _I2C46_SEL,
278 _UART6_SEL,
279 _UART24_SEL,
280 _UART35_SEL,
281 _UART78_SEL,
282 _SDMMC12_SEL,
283 _SDMMC3_SEL,
284 _ETH_SEL,
285 _QSPI_SEL,
286 _FMC_SEL,
287 _USBPHY_SEL,
288 _USBO_SEL,
289 _STGEN_SEL,
290 _PARENT_SEL_NB,
291 _UNKNOWN_SEL = 0xff,
292};
293
294enum stm32mp1_pll_id {
295 _PLL1,
296 _PLL2,
297 _PLL3,
298 _PLL4,
299 _PLL_NB
300};
301
302enum stm32mp1_div_id {
303 _DIV_P,
304 _DIV_Q,
305 _DIV_R,
306 _DIV_NB,
307};
308
309enum stm32mp1_clksrc_id {
310 CLKSRC_MPU,
311 CLKSRC_AXI,
312 CLKSRC_MCU,
313 CLKSRC_PLL12,
314 CLKSRC_PLL3,
315 CLKSRC_PLL4,
316 CLKSRC_RTC,
317 CLKSRC_MCO1,
318 CLKSRC_MCO2,
319 CLKSRC_NB
320};
321
322enum stm32mp1_clkdiv_id {
323 CLKDIV_MPU,
324 CLKDIV_AXI,
325 CLKDIV_MCU,
326 CLKDIV_APB1,
327 CLKDIV_APB2,
328 CLKDIV_APB3,
329 CLKDIV_APB4,
330 CLKDIV_APB5,
331 CLKDIV_RTC,
332 CLKDIV_MCO1,
333 CLKDIV_MCO2,
334 CLKDIV_NB
335};
336
337enum stm32mp1_pllcfg {
338 PLLCFG_M,
339 PLLCFG_N,
340 PLLCFG_P,
341 PLLCFG_Q,
342 PLLCFG_R,
343 PLLCFG_O,
344 PLLCFG_NB
345};
346
347enum stm32mp1_pllcsg {
348 PLLCSG_MOD_PER,
349 PLLCSG_INC_STEP,
350 PLLCSG_SSCG_MODE,
351 PLLCSG_NB
352};
353
354enum stm32mp1_plltype {
355 PLL_800,
356 PLL_1600,
357 PLL_TYPE_NB
358};
359
360struct stm32mp1_pll {
361 u8 refclk_min;
362 u8 refclk_max;
363 u8 divn_max;
364};
365
366struct stm32mp1_clk_gate {
367 u16 offset;
368 u8 bit;
369 u8 index;
370 u8 set_clr;
371 u8 sel;
372 u8 fixed;
373};
374
375struct stm32mp1_clk_sel {
376 u16 offset;
377 u8 src;
378 u8 msk;
379 u8 nb_parent;
380 const u8 *parent;
381};
382
383#define REFCLK_SIZE 4
384struct stm32mp1_clk_pll {
385 enum stm32mp1_plltype plltype;
386 u16 rckxselr;
387 u16 pllxcfgr1;
388 u16 pllxcfgr2;
389 u16 pllxfracr;
390 u16 pllxcr;
391 u16 pllxcsgr;
392 u8 refclk[REFCLK_SIZE];
393};
394
395struct stm32mp1_clk_data {
396 const struct stm32mp1_clk_gate *gate;
397 const struct stm32mp1_clk_sel *sel;
398 const struct stm32mp1_clk_pll *pll;
399 const int nb_gate;
400};
401
402struct stm32mp1_clk_priv {
403 fdt_addr_t base;
404 const struct stm32mp1_clk_data *data;
405 ulong osc[NB_OSC];
406 struct udevice *osc_dev[NB_OSC];
407};
408
409#define STM32MP1_CLK(off, b, idx, s) \
410 { \
411 .offset = (off), \
412 .bit = (b), \
413 .index = (idx), \
414 .set_clr = 0, \
415 .sel = (s), \
416 .fixed = _UNKNOWN_ID, \
417 }
418
419#define STM32MP1_CLK_F(off, b, idx, f) \
420 { \
421 .offset = (off), \
422 .bit = (b), \
423 .index = (idx), \
424 .set_clr = 0, \
425 .sel = _UNKNOWN_SEL, \
426 .fixed = (f), \
427 }
428
429#define STM32MP1_CLK_SET_CLR(off, b, idx, s) \
430 { \
431 .offset = (off), \
432 .bit = (b), \
433 .index = (idx), \
434 .set_clr = 1, \
435 .sel = (s), \
436 .fixed = _UNKNOWN_ID, \
437 }
438
439#define STM32MP1_CLK_SET_CLR_F(off, b, idx, f) \
440 { \
441 .offset = (off), \
442 .bit = (b), \
443 .index = (idx), \
444 .set_clr = 1, \
445 .sel = _UNKNOWN_SEL, \
446 .fixed = (f), \
447 }
448
449#define STM32MP1_CLK_PARENT(idx, off, s, m, p) \
450 [(idx)] = { \
451 .offset = (off), \
452 .src = (s), \
453 .msk = (m), \
454 .parent = (p), \
455 .nb_parent = ARRAY_SIZE((p)) \
456 }
457
458#define STM32MP1_CLK_PLL(idx, type, off1, off2, off3, off4, off5, off6,\
459 p1, p2, p3, p4) \
460 [(idx)] = { \
461 .plltype = (type), \
462 .rckxselr = (off1), \
463 .pllxcfgr1 = (off2), \
464 .pllxcfgr2 = (off3), \
465 .pllxfracr = (off4), \
466 .pllxcr = (off5), \
467 .pllxcsgr = (off6), \
468 .refclk[0] = (p1), \
469 .refclk[1] = (p2), \
470 .refclk[2] = (p3), \
471 .refclk[3] = (p4), \
472 }
473
474static const u8 stm32mp1_clks[][2] = {
475 {CK_PER, _CK_PER},
476 {CK_MPU, _CK_MPU},
477 {CK_AXI, _ACLK},
478 {CK_MCU, _CK_MCU},
479 {CK_HSE, _HSE},
480 {CK_CSI, _CSI},
481 {CK_LSI, _LSI},
482 {CK_LSE, _LSE},
483 {CK_HSI, _HSI},
484 {CK_HSE_DIV2, _HSE_KER_DIV2},
485};
486
487static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
488 STM32MP1_CLK(RCC_DDRITFCR, 0, DDRC1, _UNKNOWN_SEL),
489 STM32MP1_CLK(RCC_DDRITFCR, 1, DDRC1LP, _UNKNOWN_SEL),
490 STM32MP1_CLK(RCC_DDRITFCR, 2, DDRC2, _UNKNOWN_SEL),
491 STM32MP1_CLK(RCC_DDRITFCR, 3, DDRC2LP, _UNKNOWN_SEL),
492 STM32MP1_CLK_F(RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R),
493 STM32MP1_CLK(RCC_DDRITFCR, 5, DDRPHYCLP, _UNKNOWN_SEL),
494 STM32MP1_CLK(RCC_DDRITFCR, 6, DDRCAPB, _UNKNOWN_SEL),
495 STM32MP1_CLK(RCC_DDRITFCR, 7, DDRCAPBLP, _UNKNOWN_SEL),
496 STM32MP1_CLK(RCC_DDRITFCR, 8, AXIDCG, _UNKNOWN_SEL),
497 STM32MP1_CLK(RCC_DDRITFCR, 9, DDRPHYCAPB, _UNKNOWN_SEL),
498 STM32MP1_CLK(RCC_DDRITFCR, 10, DDRPHYCAPBLP, _UNKNOWN_SEL),
499
500 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL),
501 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL),
502 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL),
503 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL),
504 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL),
505 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL),
506 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL),
507 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL),
508 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
509 STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
510
511 STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
512
Fabrice Gasnierf198bba2018-04-26 17:00:47 +0200513 STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
514
Patrick Delaunaya6151912018-03-12 10:46:15 +0100515 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL),
516 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL),
517 STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
518
519 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
520 STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
521
522 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL),
523 STM32MP1_CLK_SET_CLR(RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL),
524
525 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL),
526 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL),
527 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL),
528 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL),
529 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL),
530 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL),
531 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL),
532 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL),
533 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL),
534 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL),
535 STM32MP1_CLK_SET_CLR(RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL),
536
537 STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
538
539 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK, _UNKNOWN_SEL),
540 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
541 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
542 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 10, ETHMAC_K, _ETH_SEL),
543 STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
544 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL),
545 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL),
546 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL),
547 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL),
548 STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
549
550 STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
551};
552
553static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
554static const u8 i2c35_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
555static const u8 i2c46_parents[] = {_PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER};
556static const u8 uart6_parents[] = {_PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER,
557 _HSE_KER};
558static const u8 uart24_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
559 _HSE_KER};
560static const u8 uart35_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
561 _HSE_KER};
562static const u8 uart78_parents[] = {_PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER,
563 _HSE_KER};
564static const u8 sdmmc12_parents[] = {_HCLK6, _PLL3_R, _PLL4_P, _HSI_KER};
565static const u8 sdmmc3_parents[] = {_HCLK2, _PLL3_R, _PLL4_P, _HSI_KER};
566static const u8 eth_parents[] = {_PLL4_P, _PLL3_Q};
567static const u8 qspi_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
568static const u8 fmc_parents[] = {_ACLK, _PLL3_R, _PLL4_P, _CK_PER};
569static const u8 usbphy_parents[] = {_HSE_KER, _PLL4_R, _HSE_KER_DIV2};
570static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
571static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
572
573static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
574 STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
575 STM32MP1_CLK_PARENT(_I2C35_SEL, RCC_I2C35CKSELR, 0, 0x7, i2c35_parents),
576 STM32MP1_CLK_PARENT(_I2C46_SEL, RCC_I2C46CKSELR, 0, 0x7, i2c46_parents),
577 STM32MP1_CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents),
578 STM32MP1_CLK_PARENT(_UART24_SEL, RCC_UART24CKSELR, 0, 0x7,
579 uart24_parents),
580 STM32MP1_CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7,
581 uart35_parents),
582 STM32MP1_CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7,
583 uart78_parents),
584 STM32MP1_CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7,
585 sdmmc12_parents),
586 STM32MP1_CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7,
587 sdmmc3_parents),
588 STM32MP1_CLK_PARENT(_ETH_SEL, RCC_ETHCKSELR, 0, 0x3, eth_parents),
589 STM32MP1_CLK_PARENT(_QSPI_SEL, RCC_QSPICKSELR, 0, 0xf, qspi_parents),
590 STM32MP1_CLK_PARENT(_FMC_SEL, RCC_FMCCKSELR, 0, 0xf, fmc_parents),
591 STM32MP1_CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents),
592 STM32MP1_CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents),
593 STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
594};
595
596#ifdef STM32MP1_CLOCK_TREE_INIT
597/* define characteristic of PLL according type */
598#define DIVN_MIN 24
599static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = {
600 [PLL_800] = {
601 .refclk_min = 4,
602 .refclk_max = 16,
603 .divn_max = 99,
604 },
605 [PLL_1600] = {
606 .refclk_min = 8,
607 .refclk_max = 16,
608 .divn_max = 199,
609 },
610};
611#endif /* STM32MP1_CLOCK_TREE_INIT */
612
613static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = {
614 STM32MP1_CLK_PLL(_PLL1, PLL_1600,
615 RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2,
616 RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR,
617 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
618 STM32MP1_CLK_PLL(_PLL2, PLL_1600,
619 RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2,
620 RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR,
621 _HSI, _HSE, _UNKNOWN_ID, _UNKNOWN_ID),
622 STM32MP1_CLK_PLL(_PLL3, PLL_800,
623 RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2,
624 RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR,
625 _HSI, _HSE, _CSI, _UNKNOWN_ID),
626 STM32MP1_CLK_PLL(_PLL4, PLL_800,
627 RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2,
628 RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR,
629 _HSI, _HSE, _CSI, _I2S_CKIN),
630};
631
632/* Prescaler table lookups for clock computation */
633/* div = /1 /2 /4 /8 / 16 /64 /128 /512 */
634static const u8 stm32mp1_mcu_div[16] = {
635 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9
636};
637
638/* div = /1 /2 /4 /8 /16 : same divider for pmu and apbx*/
639#define stm32mp1_mpu_div stm32mp1_mpu_apbx_div
640#define stm32mp1_apbx_div stm32mp1_mpu_apbx_div
641static const u8 stm32mp1_mpu_apbx_div[8] = {
642 0, 1, 2, 3, 4, 4, 4, 4
643};
644
645/* div = /1 /2 /3 /4 */
646static const u8 stm32mp1_axi_div[8] = {
647 1, 2, 3, 4, 4, 4, 4, 4
648};
649
650#ifdef DEBUG
651static const char * const stm32mp1_clk_parent_name[_PARENT_NB] = {
652 [_HSI] = "HSI",
653 [_HSE] = "HSE",
654 [_CSI] = "CSI",
655 [_LSI] = "LSI",
656 [_LSE] = "LSE",
657 [_I2S_CKIN] = "I2S_CKIN",
658 [_HSI_KER] = "HSI_KER",
659 [_HSE_KER] = "HSE_KER",
660 [_HSE_KER_DIV2] = "HSE_KER_DIV2",
661 [_CSI_KER] = "CSI_KER",
662 [_PLL1_P] = "PLL1_P",
663 [_PLL1_Q] = "PLL1_Q",
664 [_PLL1_R] = "PLL1_R",
665 [_PLL2_P] = "PLL2_P",
666 [_PLL2_Q] = "PLL2_Q",
667 [_PLL2_R] = "PLL2_R",
668 [_PLL3_P] = "PLL3_P",
669 [_PLL3_Q] = "PLL3_Q",
670 [_PLL3_R] = "PLL3_R",
671 [_PLL4_P] = "PLL4_P",
672 [_PLL4_Q] = "PLL4_Q",
673 [_PLL4_R] = "PLL4_R",
674 [_ACLK] = "ACLK",
675 [_PCLK1] = "PCLK1",
676 [_PCLK2] = "PCLK2",
677 [_PCLK3] = "PCLK3",
678 [_PCLK4] = "PCLK4",
679 [_PCLK5] = "PCLK5",
680 [_HCLK6] = "KCLK6",
681 [_HCLK2] = "HCLK2",
682 [_CK_PER] = "CK_PER",
683 [_CK_MPU] = "CK_MPU",
684 [_CK_MCU] = "CK_MCU",
685 [_USB_PHY_48] = "USB_PHY_48"
686};
687
688static const char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
689 [_I2C12_SEL] = "I2C12",
690 [_I2C35_SEL] = "I2C35",
691 [_I2C46_SEL] = "I2C46",
692 [_UART6_SEL] = "UART6",
693 [_UART24_SEL] = "UART24",
694 [_UART35_SEL] = "UART35",
695 [_UART78_SEL] = "UART78",
696 [_SDMMC12_SEL] = "SDMMC12",
697 [_SDMMC3_SEL] = "SDMMC3",
698 [_ETH_SEL] = "ETH",
699 [_QSPI_SEL] = "QSPI",
700 [_FMC_SEL] = "FMC",
701 [_USBPHY_SEL] = "USBPHY",
702 [_USBO_SEL] = "USBO",
703 [_STGEN_SEL] = "STGEN"
704};
705#endif
706
707static const struct stm32mp1_clk_data stm32mp1_data = {
708 .gate = stm32mp1_clk_gate,
709 .sel = stm32mp1_clk_sel,
710 .pll = stm32mp1_clk_pll,
711 .nb_gate = ARRAY_SIZE(stm32mp1_clk_gate),
712};
713
714static ulong stm32mp1_clk_get_fixed(struct stm32mp1_clk_priv *priv, int idx)
715{
716 if (idx >= NB_OSC) {
717 debug("%s: clk id %d not found\n", __func__, idx);
718 return 0;
719 }
720
721 debug("%s: clk id %d = %x : %ld kHz\n", __func__, idx,
722 (u32)priv->osc[idx], priv->osc[idx] / 1000);
723
724 return priv->osc[idx];
725}
726
727static int stm32mp1_clk_get_id(struct stm32mp1_clk_priv *priv, unsigned long id)
728{
729 const struct stm32mp1_clk_gate *gate = priv->data->gate;
730 int i, nb_clks = priv->data->nb_gate;
731
732 for (i = 0; i < nb_clks; i++) {
733 if (gate[i].index == id)
734 break;
735 }
736
737 if (i == nb_clks) {
738 printf("%s: clk id %d not found\n", __func__, (u32)id);
739 return -EINVAL;
740 }
741
742 return i;
743}
744
745static int stm32mp1_clk_get_sel(struct stm32mp1_clk_priv *priv,
746 int i)
747{
748 const struct stm32mp1_clk_gate *gate = priv->data->gate;
749
750 if (gate[i].sel > _PARENT_SEL_NB) {
751 printf("%s: parents for clk id %d not found\n",
752 __func__, i);
753 return -EINVAL;
754 }
755
756 return gate[i].sel;
757}
758
759static int stm32mp1_clk_get_fixed_parent(struct stm32mp1_clk_priv *priv,
760 int i)
761{
762 const struct stm32mp1_clk_gate *gate = priv->data->gate;
763
764 if (gate[i].fixed == _UNKNOWN_ID)
765 return -ENOENT;
766
767 return gate[i].fixed;
768}
769
770static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
771 unsigned long id)
772{
773 const struct stm32mp1_clk_sel *sel = priv->data->sel;
774 int i;
775 int s, p;
776
777 for (i = 0; i < ARRAY_SIZE(stm32mp1_clks); i++)
778 if (stm32mp1_clks[i][0] == id)
779 return stm32mp1_clks[i][1];
780
781 i = stm32mp1_clk_get_id(priv, id);
782 if (i < 0)
783 return i;
784
785 p = stm32mp1_clk_get_fixed_parent(priv, i);
786 if (p >= 0 && p < _PARENT_NB)
787 return p;
788
789 s = stm32mp1_clk_get_sel(priv, i);
790 if (s < 0)
791 return s;
792
793 p = (readl(priv->base + sel[s].offset) >> sel[s].src) & sel[s].msk;
794
795 if (p < sel[s].nb_parent) {
796#ifdef DEBUG
797 debug("%s: %s clock is the parent %s of clk id %d\n", __func__,
798 stm32mp1_clk_parent_name[sel[s].parent[p]],
799 stm32mp1_clk_parent_sel_name[s],
800 (u32)id);
801#endif
802 return sel[s].parent[p];
803 }
804
805 pr_err("%s: no parents defined for clk id %d\n",
806 __func__, (u32)id);
807
808 return -EINVAL;
809}
810
811static ulong stm32mp1_read_pll_freq(struct stm32mp1_clk_priv *priv,
812 int pll_id, int div_id)
813{
814 const struct stm32mp1_clk_pll *pll = priv->data->pll;
815 int divm, divn, divy, src;
816 ulong refclk, dfout;
817 u32 selr, cfgr1, cfgr2, fracr;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100818
819 debug("%s(%d, %d)\n", __func__, pll_id, div_id);
820 if (div_id > _DIV_NB)
821 return 0;
822
823 selr = readl(priv->base + pll[pll_id].rckxselr);
824 cfgr1 = readl(priv->base + pll[pll_id].pllxcfgr1);
825 cfgr2 = readl(priv->base + pll[pll_id].pllxcfgr2);
826 fracr = readl(priv->base + pll[pll_id].pllxfracr);
827
828 debug("PLL%d : selr=%x cfgr1=%x cfgr2=%x fracr=%x\n",
829 pll_id, selr, cfgr1, cfgr2, fracr);
830
831 divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT;
832 divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK;
Patrick Delaunayc2fa5dc2018-07-16 10:41:41 +0200833 divy = (cfgr2 >> RCC_PLLNCFGR2_SHIFT(div_id)) & RCC_PLLNCFGR2_DIVX_MASK;
Patrick Delaunaya6151912018-03-12 10:46:15 +0100834
835 debug(" DIVN=%d DIVM=%d DIVY=%d\n", divn, divm, divy);
836
837 src = selr & RCC_SELR_SRC_MASK;
838 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]);
839
840 debug(" refclk = %d kHz\n", (u32)(refclk / 1000));
841
842 /*
843 * For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
844 * So same final result than PLL2 et 4
845 * with FRACV :
846 * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
847 * / (DIVM + 1) * (DIVy + 1)
848 * without FRACV
849 * Fck_pll_y = Fck_ref * ((DIVN + 1) / (DIVM + 1) *(DIVy + 1)
850 */
851 if (fracr & RCC_PLLNFRACR_FRACLE) {
852 u32 fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK)
853 >> RCC_PLLNFRACR_FRACV_SHIFT;
854 dfout = (ulong)lldiv((unsigned long long)refclk *
855 (((divn + 1) << 13) + fracv),
856 ((unsigned long long)(divm + 1) *
857 (divy + 1)) << 13);
858 } else {
859 dfout = (ulong)(refclk * (divn + 1) / (divm + 1) * (divy + 1));
860 }
861 debug(" => dfout = %d kHz\n", (u32)(dfout / 1000));
862
863 return dfout;
864}
865
866static ulong stm32mp1_clk_get(struct stm32mp1_clk_priv *priv, int p)
867{
868 u32 reg;
869 ulong clock = 0;
870
871 switch (p) {
872 case _CK_MPU:
873 /* MPU sub system */
874 reg = readl(priv->base + RCC_MPCKSELR);
875 switch (reg & RCC_SELR_SRC_MASK) {
876 case RCC_MPCKSELR_HSI:
877 clock = stm32mp1_clk_get_fixed(priv, _HSI);
878 break;
879 case RCC_MPCKSELR_HSE:
880 clock = stm32mp1_clk_get_fixed(priv, _HSE);
881 break;
882 case RCC_MPCKSELR_PLL:
883 case RCC_MPCKSELR_PLL_MPUDIV:
884 clock = stm32mp1_read_pll_freq(priv, _PLL1, _DIV_P);
885 if (p == RCC_MPCKSELR_PLL_MPUDIV) {
886 reg = readl(priv->base + RCC_MPCKDIVR);
887 clock /= stm32mp1_mpu_div[reg &
888 RCC_MPUDIV_MASK];
889 }
890 break;
891 }
892 break;
893 /* AXI sub system */
894 case _ACLK:
895 case _HCLK2:
896 case _HCLK6:
897 case _PCLK4:
898 case _PCLK5:
899 reg = readl(priv->base + RCC_ASSCKSELR);
900 switch (reg & RCC_SELR_SRC_MASK) {
901 case RCC_ASSCKSELR_HSI:
902 clock = stm32mp1_clk_get_fixed(priv, _HSI);
903 break;
904 case RCC_ASSCKSELR_HSE:
905 clock = stm32mp1_clk_get_fixed(priv, _HSE);
906 break;
907 case RCC_ASSCKSELR_PLL:
908 clock = stm32mp1_read_pll_freq(priv, _PLL2, _DIV_P);
909 break;
910 }
911
912 /* System clock divider */
913 reg = readl(priv->base + RCC_AXIDIVR);
914 clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK];
915
916 switch (p) {
917 case _PCLK4:
918 reg = readl(priv->base + RCC_APB4DIVR);
919 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
920 break;
921 case _PCLK5:
922 reg = readl(priv->base + RCC_APB5DIVR);
923 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
924 break;
925 default:
926 break;
927 }
928 break;
929 /* MCU sub system */
930 case _CK_MCU:
931 case _PCLK1:
932 case _PCLK2:
933 case _PCLK3:
934 reg = readl(priv->base + RCC_MSSCKSELR);
935 switch (reg & RCC_SELR_SRC_MASK) {
936 case RCC_MSSCKSELR_HSI:
937 clock = stm32mp1_clk_get_fixed(priv, _HSI);
938 break;
939 case RCC_MSSCKSELR_HSE:
940 clock = stm32mp1_clk_get_fixed(priv, _HSE);
941 break;
942 case RCC_MSSCKSELR_CSI:
943 clock = stm32mp1_clk_get_fixed(priv, _CSI);
944 break;
945 case RCC_MSSCKSELR_PLL:
946 clock = stm32mp1_read_pll_freq(priv, _PLL3, _DIV_P);
947 break;
948 }
949
950 /* MCU clock divider */
951 reg = readl(priv->base + RCC_MCUDIVR);
952 clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK];
953
954 switch (p) {
955 case _PCLK1:
956 reg = readl(priv->base + RCC_APB1DIVR);
957 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
958 break;
959 case _PCLK2:
960 reg = readl(priv->base + RCC_APB2DIVR);
961 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
962 break;
963 case _PCLK3:
964 reg = readl(priv->base + RCC_APB3DIVR);
965 clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK];
966 break;
967 case _CK_MCU:
968 default:
969 break;
970 }
971 break;
972 case _CK_PER:
973 reg = readl(priv->base + RCC_CPERCKSELR);
974 switch (reg & RCC_SELR_SRC_MASK) {
975 case RCC_CPERCKSELR_HSI:
976 clock = stm32mp1_clk_get_fixed(priv, _HSI);
977 break;
978 case RCC_CPERCKSELR_HSE:
979 clock = stm32mp1_clk_get_fixed(priv, _HSE);
980 break;
981 case RCC_CPERCKSELR_CSI:
982 clock = stm32mp1_clk_get_fixed(priv, _CSI);
983 break;
984 }
985 break;
986 case _HSI:
987 case _HSI_KER:
988 clock = stm32mp1_clk_get_fixed(priv, _HSI);
989 break;
990 case _CSI:
991 case _CSI_KER:
992 clock = stm32mp1_clk_get_fixed(priv, _CSI);
993 break;
994 case _HSE:
995 case _HSE_KER:
996 case _HSE_KER_DIV2:
997 clock = stm32mp1_clk_get_fixed(priv, _HSE);
998 if (p == _HSE_KER_DIV2)
999 clock >>= 1;
1000 break;
1001 case _LSI:
1002 clock = stm32mp1_clk_get_fixed(priv, _LSI);
1003 break;
1004 case _LSE:
1005 clock = stm32mp1_clk_get_fixed(priv, _LSE);
1006 break;
1007 /* PLL */
1008 case _PLL1_P:
1009 case _PLL1_Q:
1010 case _PLL1_R:
1011 clock = stm32mp1_read_pll_freq(priv, _PLL1, p - _PLL1_P);
1012 break;
1013 case _PLL2_P:
1014 case _PLL2_Q:
1015 case _PLL2_R:
1016 clock = stm32mp1_read_pll_freq(priv, _PLL2, p - _PLL2_P);
1017 break;
1018 case _PLL3_P:
1019 case _PLL3_Q:
1020 case _PLL3_R:
1021 clock = stm32mp1_read_pll_freq(priv, _PLL3, p - _PLL3_P);
1022 break;
1023 case _PLL4_P:
1024 case _PLL4_Q:
1025 case _PLL4_R:
1026 clock = stm32mp1_read_pll_freq(priv, _PLL4, p - _PLL4_P);
1027 break;
1028 /* other */
1029 case _USB_PHY_48:
1030 clock = stm32mp1_clk_get_fixed(priv, _USB_PHY_48);
1031 break;
1032
1033 default:
1034 break;
1035 }
1036
1037 debug("%s(%d) clock = %lx : %ld kHz\n",
1038 __func__, p, clock, clock / 1000);
1039
1040 return clock;
1041}
1042
1043static int stm32mp1_clk_enable(struct clk *clk)
1044{
1045 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1046 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1047 int i = stm32mp1_clk_get_id(priv, clk->id);
1048
1049 if (i < 0)
1050 return i;
1051
1052 if (gate[i].set_clr)
1053 writel(BIT(gate[i].bit), priv->base + gate[i].offset);
1054 else
1055 setbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1056
1057 debug("%s: id clock %d has been enabled\n", __func__, (u32)clk->id);
1058
1059 return 0;
1060}
1061
1062static int stm32mp1_clk_disable(struct clk *clk)
1063{
1064 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1065 const struct stm32mp1_clk_gate *gate = priv->data->gate;
1066 int i = stm32mp1_clk_get_id(priv, clk->id);
1067
1068 if (i < 0)
1069 return i;
1070
1071 if (gate[i].set_clr)
1072 writel(BIT(gate[i].bit),
1073 priv->base + gate[i].offset
1074 + RCC_MP_ENCLRR_OFFSET);
1075 else
1076 clrbits_le32(priv->base + gate[i].offset, BIT(gate[i].bit));
1077
1078 debug("%s: id clock %d has been disabled\n", __func__, (u32)clk->id);
1079
1080 return 0;
1081}
1082
1083static ulong stm32mp1_clk_get_rate(struct clk *clk)
1084{
1085 struct stm32mp1_clk_priv *priv = dev_get_priv(clk->dev);
1086 int p = stm32mp1_clk_get_parent(priv, clk->id);
1087 ulong rate;
1088
1089 if (p < 0)
1090 return 0;
1091
1092 rate = stm32mp1_clk_get(priv, p);
1093
1094#ifdef DEBUG
1095 debug("%s: computed rate for id clock %d is %d (parent is %s)\n",
1096 __func__, (u32)clk->id, (u32)rate, stm32mp1_clk_parent_name[p]);
1097#endif
1098 return rate;
1099}
1100
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001101#ifdef STM32MP1_CLOCK_TREE_INIT
1102static void stm32mp1_ls_osc_set(int enable, fdt_addr_t rcc, u32 offset,
1103 u32 mask_on)
1104{
1105 u32 address = rcc + offset;
1106
1107 if (enable)
1108 setbits_le32(address, mask_on);
1109 else
1110 clrbits_le32(address, mask_on);
1111}
1112
1113static void stm32mp1_hs_ocs_set(int enable, fdt_addr_t rcc, u32 mask_on)
1114{
1115 if (enable)
1116 setbits_le32(rcc + RCC_OCENSETR, mask_on);
1117 else
1118 setbits_le32(rcc + RCC_OCENCLRR, mask_on);
1119}
1120
1121static int stm32mp1_osc_wait(int enable, fdt_addr_t rcc, u32 offset,
1122 u32 mask_rdy)
1123{
1124 u32 mask_test = 0;
1125 u32 address = rcc + offset;
1126 u32 val;
1127 int ret;
1128
1129 if (enable)
1130 mask_test = mask_rdy;
1131
1132 ret = readl_poll_timeout(address, val,
1133 (val & mask_rdy) == mask_test,
1134 TIMEOUT_1S);
1135
1136 if (ret)
1137 pr_err("OSC %x @ %x timeout for enable=%d : 0x%x\n",
1138 mask_rdy, address, enable, readl(address));
1139
1140 return ret;
1141}
1142
1143static void stm32mp1_lse_enable(fdt_addr_t rcc, int bypass, int lsedrv)
1144{
1145 u32 value;
1146
1147 if (bypass)
1148 setbits_le32(rcc + RCC_BDCR, RCC_BDCR_LSEBYP);
1149
1150 /*
1151 * warning: not recommended to switch directly from "high drive"
1152 * to "medium low drive", and vice-versa.
1153 */
1154 value = (readl(rcc + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK)
1155 >> RCC_BDCR_LSEDRV_SHIFT;
1156
1157 while (value != lsedrv) {
1158 if (value > lsedrv)
1159 value--;
1160 else
1161 value++;
1162
1163 clrsetbits_le32(rcc + RCC_BDCR,
1164 RCC_BDCR_LSEDRV_MASK,
1165 value << RCC_BDCR_LSEDRV_SHIFT);
1166 }
1167
1168 stm32mp1_ls_osc_set(1, rcc, RCC_BDCR, RCC_BDCR_LSEON);
1169}
1170
1171static void stm32mp1_lse_wait(fdt_addr_t rcc)
1172{
1173 stm32mp1_osc_wait(1, rcc, RCC_BDCR, RCC_BDCR_LSERDY);
1174}
1175
1176static void stm32mp1_lsi_set(fdt_addr_t rcc, int enable)
1177{
1178 stm32mp1_ls_osc_set(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSION);
1179 stm32mp1_osc_wait(enable, rcc, RCC_RDLSICR, RCC_RDLSICR_LSIRDY);
1180}
1181
1182static void stm32mp1_hse_enable(fdt_addr_t rcc, int bypass, int css)
1183{
1184 if (bypass)
1185 setbits_le32(rcc + RCC_OCENSETR, RCC_OCENR_HSEBYP);
1186
1187 stm32mp1_hs_ocs_set(1, rcc, RCC_OCENR_HSEON);
1188 stm32mp1_osc_wait(1, rcc, RCC_OCRDYR, RCC_OCRDYR_HSERDY);
1189
1190 if (css)
1191 setbits_le32(rcc + RCC_OCENSETR, RCC_OCENR_HSECSSON);
1192}
1193
1194static void stm32mp1_csi_set(fdt_addr_t rcc, int enable)
1195{
1196 stm32mp1_ls_osc_set(enable, rcc, RCC_OCENSETR, RCC_OCENR_CSION);
1197 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_CSIRDY);
1198}
1199
1200static void stm32mp1_hsi_set(fdt_addr_t rcc, int enable)
1201{
1202 stm32mp1_hs_ocs_set(enable, rcc, RCC_OCENR_HSION);
1203 stm32mp1_osc_wait(enable, rcc, RCC_OCRDYR, RCC_OCRDYR_HSIRDY);
1204}
1205
1206static int stm32mp1_set_hsidiv(fdt_addr_t rcc, u8 hsidiv)
1207{
1208 u32 address = rcc + RCC_OCRDYR;
1209 u32 val;
1210 int ret;
1211
1212 clrsetbits_le32(rcc + RCC_HSICFGR,
1213 RCC_HSICFGR_HSIDIV_MASK,
1214 RCC_HSICFGR_HSIDIV_MASK & hsidiv);
1215
1216 ret = readl_poll_timeout(address, val,
1217 val & RCC_OCRDYR_HSIDIVRDY,
1218 TIMEOUT_200MS);
1219 if (ret)
1220 pr_err("HSIDIV failed @ 0x%x: 0x%x\n",
1221 address, readl(address));
1222
1223 return ret;
1224}
1225
1226static int stm32mp1_hsidiv(fdt_addr_t rcc, ulong hsifreq)
1227{
1228 u8 hsidiv;
1229 u32 hsidivfreq = MAX_HSI_HZ;
1230
1231 for (hsidiv = 0; hsidiv < 4; hsidiv++,
1232 hsidivfreq = hsidivfreq / 2)
1233 if (hsidivfreq == hsifreq)
1234 break;
1235
1236 if (hsidiv == 4) {
1237 pr_err("clk-hsi frequency invalid");
1238 return -1;
1239 }
1240
1241 if (hsidiv > 0)
1242 return stm32mp1_set_hsidiv(rcc, hsidiv);
1243
1244 return 0;
1245}
1246
1247static void pll_start(struct stm32mp1_clk_priv *priv, int pll_id)
1248{
1249 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1250
1251 writel(RCC_PLLNCR_PLLON, priv->base + pll[pll_id].pllxcr);
1252}
1253
1254static int pll_output(struct stm32mp1_clk_priv *priv, int pll_id, int output)
1255{
1256 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1257 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1258 u32 val;
1259 int ret;
1260
1261 ret = readl_poll_timeout(pllxcr, val, val & RCC_PLLNCR_PLLRDY,
1262 TIMEOUT_200MS);
1263
1264 if (ret) {
1265 pr_err("PLL%d start failed @ 0x%x: 0x%x\n",
1266 pll_id, pllxcr, readl(pllxcr));
1267 return ret;
1268 }
1269
1270 /* start the requested output */
1271 setbits_le32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT);
1272
1273 return 0;
1274}
1275
1276static int pll_stop(struct stm32mp1_clk_priv *priv, int pll_id)
1277{
1278 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1279 u32 pllxcr = priv->base + pll[pll_id].pllxcr;
1280 u32 val;
1281
1282 /* stop all output */
1283 clrbits_le32(pllxcr,
1284 RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN);
1285
1286 /* stop PLL */
1287 clrbits_le32(pllxcr, RCC_PLLNCR_PLLON);
1288
1289 /* wait PLL stopped */
1290 return readl_poll_timeout(pllxcr, val, (val & RCC_PLLNCR_PLLRDY) == 0,
1291 TIMEOUT_200MS);
1292}
1293
1294static void pll_config_output(struct stm32mp1_clk_priv *priv,
1295 int pll_id, u32 *pllcfg)
1296{
1297 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1298 fdt_addr_t rcc = priv->base;
1299 u32 value;
1300
1301 value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT)
1302 & RCC_PLLNCFGR2_DIVP_MASK;
1303 value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT)
1304 & RCC_PLLNCFGR2_DIVQ_MASK;
1305 value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT)
1306 & RCC_PLLNCFGR2_DIVR_MASK;
1307 writel(value, rcc + pll[pll_id].pllxcfgr2);
1308}
1309
1310static int pll_config(struct stm32mp1_clk_priv *priv, int pll_id,
1311 u32 *pllcfg, u32 fracv)
1312{
1313 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1314 fdt_addr_t rcc = priv->base;
1315 enum stm32mp1_plltype type = pll[pll_id].plltype;
1316 int src;
1317 ulong refclk;
1318 u8 ifrge = 0;
1319 u32 value;
1320
1321 src = readl(priv->base + pll[pll_id].rckxselr) & RCC_SELR_SRC_MASK;
1322
1323 refclk = stm32mp1_clk_get_fixed(priv, pll[pll_id].refclk[src]) /
1324 (pllcfg[PLLCFG_M] + 1);
1325
1326 if (refclk < (stm32mp1_pll[type].refclk_min * 1000000) ||
1327 refclk > (stm32mp1_pll[type].refclk_max * 1000000)) {
1328 debug("invalid refclk = %x\n", (u32)refclk);
1329 return -EINVAL;
1330 }
1331 if (type == PLL_800 && refclk >= 8000000)
1332 ifrge = 1;
1333
1334 value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT)
1335 & RCC_PLLNCFGR1_DIVN_MASK;
1336 value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT)
1337 & RCC_PLLNCFGR1_DIVM_MASK;
1338 value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT)
1339 & RCC_PLLNCFGR1_IFRGE_MASK;
1340 writel(value, rcc + pll[pll_id].pllxcfgr1);
1341
1342 /* fractional configuration: load sigma-delta modulator (SDM) */
1343
1344 /* Write into FRACV the new fractional value , and FRACLE to 0 */
1345 writel(fracv << RCC_PLLNFRACR_FRACV_SHIFT,
1346 rcc + pll[pll_id].pllxfracr);
1347
1348 /* Write FRACLE to 1 : FRACV value is loaded into the SDM */
1349 setbits_le32(rcc + pll[pll_id].pllxfracr,
1350 RCC_PLLNFRACR_FRACLE);
1351
1352 pll_config_output(priv, pll_id, pllcfg);
1353
1354 return 0;
1355}
1356
1357static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
1358{
1359 const struct stm32mp1_clk_pll *pll = priv->data->pll;
1360 u32 pllxcsg;
1361
1362 pllxcsg = ((csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) &
1363 RCC_PLLNCSGR_MOD_PER_MASK) |
1364 ((csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) &
1365 RCC_PLLNCSGR_INC_STEP_MASK) |
1366 ((csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) &
1367 RCC_PLLNCSGR_SSCG_MODE_MASK);
1368
1369 writel(pllxcsg, priv->base + pll[pll_id].pllxcsgr);
1370}
1371
1372static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
1373{
1374 u32 address = priv->base + (clksrc >> 4);
1375 u32 val;
1376 int ret;
1377
1378 clrsetbits_le32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK);
1379 ret = readl_poll_timeout(address, val, val & RCC_SELR_SRCRDY,
1380 TIMEOUT_200MS);
1381 if (ret)
1382 pr_err("CLKSRC %x start failed @ 0x%x: 0x%x\n",
1383 clksrc, address, readl(address));
1384
1385 return ret;
1386}
1387
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001388static void stgen_config(struct stm32mp1_clk_priv *priv)
1389{
1390 int p;
1391 u32 stgenc, cntfid0;
1392 ulong rate;
1393
1394 stgenc = (u32)syscon_get_first_range(STM32MP_SYSCON_STGEN);
1395
1396 cntfid0 = readl(stgenc + STGENC_CNTFID0);
1397 p = stm32mp1_clk_get_parent(priv, STGEN_K);
1398 rate = stm32mp1_clk_get(priv, p);
1399
1400 if (cntfid0 != rate) {
1401 pr_debug("System Generic Counter (STGEN) update\n");
1402 clrbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1403 writel(0x0, stgenc + STGENC_CNTCVL);
1404 writel(0x0, stgenc + STGENC_CNTCVU);
1405 writel(rate, stgenc + STGENC_CNTFID0);
1406 setbits_le32(stgenc + STGENC_CNTCR, STGENC_CNTCR_EN);
1407
1408 __asm__ volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (rate));
1409
1410 /* need to update gd->arch.timer_rate_hz with new frequency */
1411 timer_init();
1412 pr_debug("gd->arch.timer_rate_hz = %x\n",
1413 (u32)gd->arch.timer_rate_hz);
1414 pr_debug("Tick = %x\n", (u32)(get_ticks()));
1415 }
1416}
1417
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001418static int set_clkdiv(unsigned int clkdiv, u32 address)
1419{
1420 u32 val;
1421 int ret;
1422
1423 clrsetbits_le32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK);
1424 ret = readl_poll_timeout(address, val, val & RCC_DIVR_DIVRDY,
1425 TIMEOUT_200MS);
1426 if (ret)
1427 pr_err("CLKDIV %x start failed @ 0x%x: 0x%x\n",
1428 clkdiv, address, readl(address));
1429
1430 return ret;
1431}
1432
1433static void stm32mp1_mco_csg(struct stm32mp1_clk_priv *priv,
1434 u32 clksrc, u32 clkdiv)
1435{
1436 u32 address = priv->base + (clksrc >> 4);
1437
1438 /*
1439 * binding clksrc : bit15-4 offset
1440 * bit3: disable
1441 * bit2-0: MCOSEL[2:0]
1442 */
1443 if (clksrc & 0x8) {
1444 clrbits_le32(address, RCC_MCOCFG_MCOON);
1445 } else {
1446 clrsetbits_le32(address,
1447 RCC_MCOCFG_MCOSRC_MASK,
1448 clksrc & RCC_MCOCFG_MCOSRC_MASK);
1449 clrsetbits_le32(address,
1450 RCC_MCOCFG_MCODIV_MASK,
1451 clkdiv << RCC_MCOCFG_MCODIV_SHIFT);
1452 setbits_le32(address, RCC_MCOCFG_MCOON);
1453 }
1454}
1455
1456static void set_rtcsrc(struct stm32mp1_clk_priv *priv,
1457 unsigned int clksrc,
1458 int lse_css)
1459{
1460 u32 address = priv->base + RCC_BDCR;
1461
1462 if (readl(address) & RCC_BDCR_RTCCKEN)
1463 goto skip_rtc;
1464
1465 if (clksrc == CLK_RTC_DISABLED)
1466 goto skip_rtc;
1467
1468 clrsetbits_le32(address,
1469 RCC_BDCR_RTCSRC_MASK,
1470 clksrc << RCC_BDCR_RTCSRC_SHIFT);
1471
1472 setbits_le32(address, RCC_BDCR_RTCCKEN);
1473
1474skip_rtc:
1475 if (lse_css)
1476 setbits_le32(address, RCC_BDCR_LSECSSON);
1477}
1478
1479static void pkcs_config(struct stm32mp1_clk_priv *priv, u32 pkcs)
1480{
1481 u32 address = priv->base + ((pkcs >> 4) & 0xFFF);
1482 u32 value = pkcs & 0xF;
1483 u32 mask = 0xF;
1484
1485 if (pkcs & BIT(31)) {
1486 mask <<= 4;
1487 value <<= 4;
1488 }
1489 clrsetbits_le32(address, mask, value);
1490}
1491
1492static int stm32mp1_clktree(struct udevice *dev)
1493{
1494 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1495 fdt_addr_t rcc = priv->base;
1496 unsigned int clksrc[CLKSRC_NB];
1497 unsigned int clkdiv[CLKDIV_NB];
1498 unsigned int pllcfg[_PLL_NB][PLLCFG_NB];
1499 ofnode plloff[_PLL_NB];
1500 int ret;
1501 int i, len;
1502 int lse_css = 0;
1503 const u32 *pkcs_cell;
1504
1505 /* check mandatory field */
1506 ret = dev_read_u32_array(dev, "st,clksrc", clksrc, CLKSRC_NB);
1507 if (ret < 0) {
1508 debug("field st,clksrc invalid: error %d\n", ret);
1509 return -FDT_ERR_NOTFOUND;
1510 }
1511
1512 ret = dev_read_u32_array(dev, "st,clkdiv", clkdiv, CLKDIV_NB);
1513 if (ret < 0) {
1514 debug("field st,clkdiv invalid: error %d\n", ret);
1515 return -FDT_ERR_NOTFOUND;
1516 }
1517
1518 /* check mandatory field in each pll */
1519 for (i = 0; i < _PLL_NB; i++) {
1520 char name[12];
1521
1522 sprintf(name, "st,pll@%d", i);
1523 plloff[i] = dev_read_subnode(dev, name);
1524 if (!ofnode_valid(plloff[i]))
1525 continue;
1526 ret = ofnode_read_u32_array(plloff[i], "cfg",
1527 pllcfg[i], PLLCFG_NB);
1528 if (ret < 0) {
1529 debug("field cfg invalid: error %d\n", ret);
1530 return -FDT_ERR_NOTFOUND;
1531 }
1532 }
1533
1534 debug("configuration MCO\n");
1535 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]);
1536 stm32mp1_mco_csg(priv, clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]);
1537
1538 debug("switch ON osillator\n");
1539 /*
1540 * switch ON oscillator found in device-tree,
1541 * HSI already ON after bootrom
1542 */
1543 if (priv->osc[_LSI])
1544 stm32mp1_lsi_set(rcc, 1);
1545
1546 if (priv->osc[_LSE]) {
1547 int bypass;
1548 int lsedrv;
1549 struct udevice *dev = priv->osc_dev[_LSE];
1550
1551 bypass = dev_read_bool(dev, "st,bypass");
1552 lse_css = dev_read_bool(dev, "st,css");
1553 lsedrv = dev_read_u32_default(dev, "st,drive",
1554 LSEDRV_MEDIUM_HIGH);
1555
1556 stm32mp1_lse_enable(rcc, bypass, lsedrv);
1557 }
1558
1559 if (priv->osc[_HSE]) {
1560 int bypass, css;
1561 struct udevice *dev = priv->osc_dev[_HSE];
1562
1563 bypass = dev_read_bool(dev, "st,bypass");
1564 css = dev_read_bool(dev, "st,css");
1565
1566 stm32mp1_hse_enable(rcc, bypass, css);
1567 }
1568 /* CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR)
1569 * => switch on CSI even if node is not present in device tree
1570 */
1571 stm32mp1_csi_set(rcc, 1);
1572
1573 /* come back to HSI */
1574 debug("come back to HSI\n");
1575 set_clksrc(priv, CLK_MPU_HSI);
1576 set_clksrc(priv, CLK_AXI_HSI);
1577 set_clksrc(priv, CLK_MCU_HSI);
1578
1579 debug("pll stop\n");
1580 for (i = 0; i < _PLL_NB; i++)
1581 pll_stop(priv, i);
1582
1583 /* configure HSIDIV */
1584 debug("configure HSIDIV\n");
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001585 if (priv->osc[_HSI]) {
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001586 stm32mp1_hsidiv(rcc, priv->osc[_HSI]);
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001587 stgen_config(priv);
1588 }
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001589
1590 /* select DIV */
1591 debug("select DIV\n");
1592 /* no ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */
1593 writel(clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK, rcc + RCC_MPCKDIVR);
1594 set_clkdiv(clkdiv[CLKDIV_AXI], rcc + RCC_AXIDIVR);
1595 set_clkdiv(clkdiv[CLKDIV_APB4], rcc + RCC_APB4DIVR);
1596 set_clkdiv(clkdiv[CLKDIV_APB5], rcc + RCC_APB5DIVR);
1597 set_clkdiv(clkdiv[CLKDIV_MCU], rcc + RCC_MCUDIVR);
1598 set_clkdiv(clkdiv[CLKDIV_APB1], rcc + RCC_APB1DIVR);
1599 set_clkdiv(clkdiv[CLKDIV_APB2], rcc + RCC_APB2DIVR);
1600 set_clkdiv(clkdiv[CLKDIV_APB3], rcc + RCC_APB3DIVR);
1601
1602 /* no ready bit for RTC */
1603 writel(clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK, rcc + RCC_RTCDIVR);
1604
1605 /* configure PLLs source */
1606 debug("configure PLLs source\n");
1607 set_clksrc(priv, clksrc[CLKSRC_PLL12]);
1608 set_clksrc(priv, clksrc[CLKSRC_PLL3]);
1609 set_clksrc(priv, clksrc[CLKSRC_PLL4]);
1610
1611 /* configure and start PLLs */
1612 debug("configure PLLs\n");
1613 for (i = 0; i < _PLL_NB; i++) {
1614 u32 fracv;
1615 u32 csg[PLLCSG_NB];
1616
1617 debug("configure PLL %d @ %d\n", i,
1618 ofnode_to_offset(plloff[i]));
1619 if (!ofnode_valid(plloff[i]))
1620 continue;
1621
1622 fracv = ofnode_read_u32_default(plloff[i], "frac", 0);
1623 pll_config(priv, i, pllcfg[i], fracv);
1624 ret = ofnode_read_u32_array(plloff[i], "csg", csg, PLLCSG_NB);
1625 if (!ret) {
1626 pll_csg(priv, i, csg);
1627 } else if (ret != -FDT_ERR_NOTFOUND) {
1628 debug("invalid csg node for pll@%d res=%d\n", i, ret);
1629 return ret;
1630 }
1631 pll_start(priv, i);
1632 }
1633
1634 /* wait and start PLLs ouptut when ready */
1635 for (i = 0; i < _PLL_NB; i++) {
1636 if (!ofnode_valid(plloff[i]))
1637 continue;
1638 debug("output PLL %d\n", i);
1639 pll_output(priv, i, pllcfg[i][PLLCFG_O]);
1640 }
1641
1642 /* wait LSE ready before to use it */
1643 if (priv->osc[_LSE])
1644 stm32mp1_lse_wait(rcc);
1645
1646 /* configure with expected clock source */
1647 debug("CLKSRC\n");
1648 set_clksrc(priv, clksrc[CLKSRC_MPU]);
1649 set_clksrc(priv, clksrc[CLKSRC_AXI]);
1650 set_clksrc(priv, clksrc[CLKSRC_MCU]);
1651 set_rtcsrc(priv, clksrc[CLKSRC_RTC], lse_css);
1652
1653 /* configure PKCK */
1654 debug("PKCK\n");
1655 pkcs_cell = dev_read_prop(dev, "st,pkcs", &len);
1656 if (pkcs_cell) {
1657 bool ckper_disabled = false;
1658
1659 for (i = 0; i < len / sizeof(u32); i++) {
1660 u32 pkcs = (u32)fdt32_to_cpu(pkcs_cell[i]);
1661
1662 if (pkcs == CLK_CKPER_DISABLED) {
1663 ckper_disabled = true;
1664 continue;
1665 }
1666 pkcs_config(priv, pkcs);
1667 }
1668 /* CKPER is source for some peripheral clock
1669 * (FMC-NAND / QPSI-NOR) and switching source is allowed
1670 * only if previous clock is still ON
1671 * => deactivated CKPER only after switching clock
1672 */
1673 if (ckper_disabled)
1674 pkcs_config(priv, CLK_CKPER_DISABLED);
1675 }
1676
Patrick Delaunay938e0e32018-03-20 11:41:25 +01001677 /* STGEN clock source can change with CLK_STGEN_XXX */
1678 stgen_config(priv);
1679
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001680 debug("oscillator off\n");
1681 /* switch OFF HSI if not found in device-tree */
1682 if (!priv->osc[_HSI])
1683 stm32mp1_hsi_set(rcc, 0);
1684
1685 /* Software Self-Refresh mode (SSR) during DDR initilialization */
1686 clrsetbits_le32(priv->base + RCC_DDRITFCR,
1687 RCC_DDRITFCR_DDRCKMOD_MASK,
1688 RCC_DDRITFCR_DDRCKMOD_SSR <<
1689 RCC_DDRITFCR_DDRCKMOD_SHIFT);
1690
1691 return 0;
1692}
1693#endif /* STM32MP1_CLOCK_TREE_INIT */
1694
Patrick Delaunaya6151912018-03-12 10:46:15 +01001695static void stm32mp1_osc_clk_init(const char *name,
1696 struct stm32mp1_clk_priv *priv,
1697 int index)
1698{
1699 struct clk clk;
1700 struct udevice *dev = NULL;
1701
1702 priv->osc[index] = 0;
1703 clk.id = 0;
1704 if (!uclass_get_device_by_name(UCLASS_CLK, name, &dev)) {
1705 if (clk_request(dev, &clk))
1706 pr_err("%s request", name);
1707 else
1708 priv->osc[index] = clk_get_rate(&clk);
1709 }
1710 priv->osc_dev[index] = dev;
1711}
1712
1713static void stm32mp1_osc_init(struct udevice *dev)
1714{
1715 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1716 int i;
1717 const char *name[NB_OSC] = {
1718 [_LSI] = "clk-lsi",
1719 [_LSE] = "clk-lse",
1720 [_HSI] = "clk-hsi",
1721 [_HSE] = "clk-hse",
1722 [_CSI] = "clk-csi",
1723 [_I2S_CKIN] = "i2s_ckin",
1724 [_USB_PHY_48] = "ck_usbo_48m"};
1725
1726 for (i = 0; i < NB_OSC; i++) {
1727 stm32mp1_osc_clk_init(name[i], priv, i);
1728 debug("%d: %s => %x\n", i, name[i], (u32)priv->osc[i]);
1729 }
1730}
1731
1732static int stm32mp1_clk_probe(struct udevice *dev)
1733{
1734 int result = 0;
1735 struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
1736
1737 priv->base = dev_read_addr(dev->parent);
1738 if (priv->base == FDT_ADDR_T_NONE)
1739 return -EINVAL;
1740
1741 priv->data = (void *)&stm32mp1_data;
1742
1743 if (!priv->data->gate || !priv->data->sel ||
1744 !priv->data->pll)
1745 return -EINVAL;
1746
1747 stm32mp1_osc_init(dev);
1748
Patrick Delaunay266fa4d2018-03-12 10:46:16 +01001749#ifdef STM32MP1_CLOCK_TREE_INIT
1750 /* clock tree init is done only one time, before relocation */
1751 if (!(gd->flags & GD_FLG_RELOC))
1752 result = stm32mp1_clktree(dev);
1753#endif
1754
Patrick Delaunaya6151912018-03-12 10:46:15 +01001755 return result;
1756}
1757
1758static const struct clk_ops stm32mp1_clk_ops = {
1759 .enable = stm32mp1_clk_enable,
1760 .disable = stm32mp1_clk_disable,
1761 .get_rate = stm32mp1_clk_get_rate,
1762};
1763
Patrick Delaunaya6151912018-03-12 10:46:15 +01001764U_BOOT_DRIVER(stm32mp1_clock) = {
1765 .name = "stm32mp1_clk",
1766 .id = UCLASS_CLK,
Patrick Delaunaya6151912018-03-12 10:46:15 +01001767 .ops = &stm32mp1_clk_ops,
1768 .priv_auto_alloc_size = sizeof(struct stm32mp1_clk_priv),
1769 .probe = stm32mp1_clk_probe,
1770};