2 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
3 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <clk-uclass.h>
11 #include <stm32_rcc.h>
14 #include <asm/arch/stm32.h>
15 #include <asm/arch/stm32_pwr.h>
17 #include <dt-bindings/mfd/stm32f7-rcc.h>
19 #define RCC_CR_HSION BIT(0)
20 #define RCC_CR_HSEON BIT(16)
21 #define RCC_CR_HSERDY BIT(17)
22 #define RCC_CR_HSEBYP BIT(18)
23 #define RCC_CR_CSSON BIT(19)
24 #define RCC_CR_PLLON BIT(24)
25 #define RCC_CR_PLLRDY BIT(25)
26 #define RCC_CR_PLLSAION BIT(28)
27 #define RCC_CR_PLLSAIRDY BIT(29)
29 #define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
30 #define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
31 #define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
32 #define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
33 #define RCC_PLLCFGR_PLLSRC BIT(22)
34 #define RCC_PLLCFGR_PLLM_SHIFT 0
35 #define RCC_PLLCFGR_PLLN_SHIFT 6
36 #define RCC_PLLCFGR_PLLP_SHIFT 16
37 #define RCC_PLLCFGR_PLLQ_SHIFT 24
39 #define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
40 #define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
41 #define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
42 #define RCC_CFGR_SW0 BIT(0)
43 #define RCC_CFGR_SW1 BIT(1)
44 #define RCC_CFGR_SW_MASK GENMASK(1, 0)
45 #define RCC_CFGR_SW_HSI 0
46 #define RCC_CFGR_SW_HSE RCC_CFGR_SW0
47 #define RCC_CFGR_SW_PLL RCC_CFGR_SW1
48 #define RCC_CFGR_SWS0 BIT(2)
49 #define RCC_CFGR_SWS1 BIT(3)
50 #define RCC_CFGR_SWS_MASK GENMASK(3, 2)
51 #define RCC_CFGR_SWS_HSI 0
52 #define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
53 #define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
54 #define RCC_CFGR_HPRE_SHIFT 4
55 #define RCC_CFGR_PPRE1_SHIFT 10
56 #define RCC_CFGR_PPRE2_SHIFT 13
58 #define RCC_PLLCFGR_PLLSAIN_MASK GENMASK(14, 6)
59 #define RCC_PLLCFGR_PLLSAIP_MASK GENMASK(17, 16)
60 #define RCC_PLLSAICFGR_PLLSAIN_SHIFT 6
61 #define RCC_PLLSAICFGR_PLLSAIP_SHIFT 16
62 #define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16)
63 #define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26)
64 #define RCC_PLLSAICFGR_PLLSAIR_2 BIT(29)
66 #define RCC_DCKCFGRX_TIMPRE BIT(24)
67 #define RCC_DCKCFGRX_CK48MSEL BIT(27)
68 #define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
69 #define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
72 * RCC AHB1ENR specific definitions
74 #define RCC_AHB1ENR_ETHMAC_EN BIT(25)
75 #define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
76 #define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
79 * RCC APB1ENR specific definitions
81 #define RCC_APB1ENR_TIM2EN BIT(0)
82 #define RCC_APB1ENR_PWREN BIT(28)
85 * RCC APB2ENR specific definitions
87 #define RCC_APB2ENR_SYSCFGEN BIT(14)
88 #define RCC_APB2ENR_SAI1EN BIT(22)
94 static const struct stm32_clk_info stm32f4_clk_info = {
100 .ahb_psc = AHB_PSC_1,
101 .apb1_psc = APB_PSC_4,
102 .apb2_psc = APB_PSC_2,
104 .has_overdrive = false,
108 static const struct stm32_clk_info stm32f7_clk_info = {
114 .ahb_psc = AHB_PSC_1,
115 .apb1_psc = APB_PSC_4,
116 .apb2_psc = APB_PSC_2,
118 .has_overdrive = true,
123 struct stm32_rcc_regs *base;
124 struct stm32_pwr_regs *pwr_regs;
125 struct stm32_clk_info info;
126 unsigned long hse_rate;
129 static int configure_clocks(struct udevice *dev)
131 struct stm32_clk *priv = dev_get_priv(dev);
132 struct stm32_rcc_regs *regs = priv->base;
133 struct stm32_pwr_regs *pwr = priv->pwr_regs;
134 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
137 /* Reset RCC configuration */
138 setbits_le32(®s->cr, RCC_CR_HSION);
139 writel(0, ®s->cfgr); /* Reset CFGR */
140 clrbits_le32(®s->cr, (RCC_CR_HSEON | RCC_CR_CSSON
141 | RCC_CR_PLLON | RCC_CR_PLLSAION));
142 writel(0x24003010, ®s->pllcfgr); /* Reset value from RM */
143 clrbits_le32(®s->cr, RCC_CR_HSEBYP);
144 writel(0, ®s->cir); /* Disable all interrupts */
146 /* Configure for HSE+PLL operation */
147 setbits_le32(®s->cr, RCC_CR_HSEON);
148 while (!(readl(®s->cr) & RCC_CR_HSERDY))
151 setbits_le32(®s->cfgr, ((
152 sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT)
153 | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT)
154 | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
156 /* Configure the main PLL */
157 setbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
158 clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
159 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
160 clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
161 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
162 clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
163 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
164 clrsetbits_le32(®s->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
165 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
167 /* Configure the SAI PLL to get a 48 MHz source */
168 pllsaicfgr = RCC_PLLSAICFGR_PLLSAIR_2 | RCC_PLLSAICFGR_PLLSAIQ_4 |
169 RCC_PLLSAICFGR_PLLSAIP_4;
170 pllsaicfgr |= 192 << RCC_PLLSAICFGR_PLLSAIN_SHIFT;
171 writel(pllsaicfgr, ®s->pllsaicfgr);
173 /* Enable the main PLL */
174 setbits_le32(®s->cr, RCC_CR_PLLON);
175 while (!(readl(®s->cr) & RCC_CR_PLLRDY))
178 if (priv->info.v2) { /*stm32f7 case */
179 /* select PLLSAI as 48MHz clock source */
180 setbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
182 /* select 48MHz as SDMMC1 clock source */
183 clrbits_le32(®s->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
185 /* select 48MHz as SDMMC2 clock source */
186 clrbits_le32(®s->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
187 } else { /* stm32f4 case */
188 /* select PLLSAI as 48MHz clock source */
189 setbits_le32(®s->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
191 /* select 48MHz as SDMMC1 clock source */
192 clrbits_le32(®s->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
195 /* Enable the SAI PLL */
196 setbits_le32(®s->cr, RCC_CR_PLLSAION);
197 while (!(readl(®s->cr) & RCC_CR_PLLSAIRDY))
200 setbits_le32(®s->apb1enr, RCC_APB1ENR_PWREN);
202 if (priv->info.has_overdrive) {
204 * Enable high performance mode
205 * System frequency up to 200 MHz
207 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
209 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
211 /* Enable the Over-drive switch */
212 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
214 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
218 stm32_flash_latency_cfg(5);
219 clrbits_le32(®s->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
220 setbits_le32(®s->cfgr, RCC_CFGR_SW_PLL);
222 while ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) !=
225 /* gate the SAI clock, needed for MMC 1&2 clocks */
226 setbits_le32(®s->apb2enr, RCC_APB2ENR_SAI1EN);
228 #ifdef CONFIG_ETH_DESIGNWARE
229 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
230 setbits_le32(®s->apb2enr, RCC_APB2ENR_SYSCFGEN);
236 static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv,
239 struct stm32_rcc_regs *regs = priv->base;
240 u16 pllq, pllm, pllsain, pllsaip;
243 pllq = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
244 >> RCC_PLLCFGR_PLLQ_SHIFT;
246 if (priv->info.v2) /*stm32f7 case */
247 pllsai = readl(®s->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
249 pllsai = readl(®s->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
252 /* PLL48CLK is selected from PLLSAI, get PLLSAI value */
253 pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
254 pllsain = ((readl(®s->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK)
255 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
256 pllsaip = ((((readl(®s->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK)
257 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
258 return ((priv->hse_rate / pllm) * pllsain) / pllsaip;
260 /* PLL48CLK is selected from PLLQ */
261 return sysclk / pllq;
264 static bool stm32_get_timpre(struct stm32_clk *priv)
266 struct stm32_rcc_regs *regs = priv->base;
269 if (priv->info.v2) /*stm32f7 case */
270 val = readl(®s->dckcfgr2);
272 val = readl(®s->dckcfgr);
273 /* get timer prescaler */
274 return !!(val & RCC_DCKCFGRX_TIMPRE);
277 static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk)
280 /* Prescaler table lookups for clock computation */
281 u8 ahb_psc_table[16] = {
282 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
285 shift = ahb_psc_table[(
286 (readl(®s->cfgr) & RCC_CFGR_AHB_PSC_MASK)
287 >> RCC_CFGR_HPRE_SHIFT)];
289 return sysclk >> shift;
292 static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb)
294 /* Prescaler table lookups for clock computation */
295 u8 apb_psc_table[8] = {
296 0, 0, 0, 0, 1, 2, 3, 4
300 return apb_psc_table[(
301 (readl(®s->cfgr) & RCC_CFGR_APB1_PSC_MASK)
302 >> RCC_CFGR_PPRE1_SHIFT)];
304 return apb_psc_table[(
305 (readl(®s->cfgr) & RCC_CFGR_APB2_PSC_MASK)
306 >> RCC_CFGR_PPRE2_SHIFT)];
309 static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk,
312 struct stm32_rcc_regs *regs = priv->base;
313 u8 shift = stm32_get_apb_shift(regs, apb);
315 if (stm32_get_timpre(priv))
317 * if APB prescaler is configured to a
318 * division factor of 1, 2 or 4
324 return stm32_get_hclk_rate(regs, sysclk);
326 return (sysclk >> shift) * 4;
330 * if APB prescaler is configured to a
331 * division factor of 1
336 return (sysclk >> shift) * 2;
339 static ulong stm32_clk_get_rate(struct clk *clk)
341 struct stm32_clk *priv = dev_get_priv(clk->dev);
342 struct stm32_rcc_regs *regs = priv->base;
344 u16 pllm, plln, pllp;
346 if ((readl(®s->cfgr) & RCC_CFGR_SWS_MASK) ==
348 pllm = (readl(®s->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
349 plln = ((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
350 >> RCC_PLLCFGR_PLLN_SHIFT);
351 pllp = ((((readl(®s->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
352 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
353 sysclk = ((priv->hse_rate / pllm) * plln) / pllp;
360 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
361 * AHB1, AHB2 and AHB3
363 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
364 return stm32_get_hclk_rate(regs, sysclk);
366 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
367 /* For timer clock, an additionnal prescaler is used*/
369 case STM32F7_APB1_CLOCK(TIM2):
370 case STM32F7_APB1_CLOCK(TIM3):
371 case STM32F7_APB1_CLOCK(TIM4):
372 case STM32F7_APB1_CLOCK(TIM5):
373 case STM32F7_APB1_CLOCK(TIM6):
374 case STM32F7_APB1_CLOCK(TIM7):
375 case STM32F7_APB1_CLOCK(TIM12):
376 case STM32F7_APB1_CLOCK(TIM13):
377 case STM32F7_APB1_CLOCK(TIM14):
378 return stm32_get_timer_rate(priv, sysclk, APB1);
380 return (sysclk >> stm32_get_apb_shift(regs, APB1));
383 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
385 * particular case for SDMMC1 and SDMMC2 :
386 * 48Mhz source clock can be from main PLL or from
390 case STM32F7_APB2_CLOCK(SDMMC1):
391 if (readl(®s->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL)
392 /* System clock is selected as SDMMC1 clock */
395 return stm32_clk_pll48clk_rate(priv, sysclk);
397 case STM32F7_APB2_CLOCK(SDMMC2):
398 if (readl(®s->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL)
399 /* System clock is selected as SDMMC2 clock */
402 return stm32_clk_pll48clk_rate(priv, sysclk);
405 /* For timer clock, an additionnal prescaler is used*/
406 case STM32F7_APB2_CLOCK(TIM1):
407 case STM32F7_APB2_CLOCK(TIM8):
408 case STM32F7_APB2_CLOCK(TIM9):
409 case STM32F7_APB2_CLOCK(TIM10):
410 case STM32F7_APB2_CLOCK(TIM11):
411 return stm32_get_timer_rate(priv, sysclk, APB2);
414 return (sysclk >> stm32_get_apb_shift(regs, APB2));
417 pr_err("clock index %ld out of range\n", clk->id);
422 static ulong stm32_set_rate(struct clk *clk, ulong rate)
427 static int stm32_clk_enable(struct clk *clk)
429 struct stm32_clk *priv = dev_get_priv(clk->dev);
430 struct stm32_rcc_regs *regs = priv->base;
431 u32 offset = clk->id / 32;
432 u32 bit_index = clk->id % 32;
434 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
435 __func__, clk->id, offset, bit_index);
436 setbits_le32(®s->ahb1enr + offset, BIT(bit_index));
441 void clock_setup(int peripheral)
443 switch (peripheral) {
444 case TIMER2_CLOCK_CFG:
445 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
452 static int stm32_clk_probe(struct udevice *dev)
454 struct ofnode_phandle_args args;
455 struct udevice *fixed_clock_dev = NULL;
459 debug("%s\n", __func__);
461 struct stm32_clk *priv = dev_get_priv(dev);
464 addr = dev_read_addr(dev);
465 if (addr == FDT_ADDR_T_NONE)
468 priv->base = (struct stm32_rcc_regs *)addr;
470 switch (dev_get_driver_data(dev)) {
472 memcpy(&priv->info, &stm32f4_clk_info,
473 sizeof(struct stm32_clk_info));
476 memcpy(&priv->info, &stm32f7_clk_info,
477 sizeof(struct stm32_clk_info));
483 /* retrieve HSE frequency (external oscillator) */
484 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
488 pr_err("Can't find fixed clock (%d)", err);
492 err = clk_request(fixed_clock_dev, &clk);
494 pr_err("Can't request %s clk (%d)", fixed_clock_dev->name,
500 * set pllm factor accordingly to the external oscillator
501 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
503 * if input PLL frequency is 25Mhz, divide it by 25
506 priv->hse_rate = clk_get_rate(&clk);
508 if (priv->hse_rate < 1000000) {
509 pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__,
514 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
516 if (priv->info.has_overdrive) {
517 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
520 debug("%s: can't find syscon device (%d)\n", __func__,
525 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
528 configure_clocks(dev);
533 static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
535 debug("%s(clk=%p)\n", __func__, clk);
537 if (args->args_count != 2) {
538 debug("Invaild args_count: %d\n", args->args_count);
542 if (args->args_count)
543 clk->id = args->args[1];
550 static struct clk_ops stm32_clk_ops = {
551 .of_xlate = stm32_clk_of_xlate,
552 .enable = stm32_clk_enable,
553 .get_rate = stm32_clk_get_rate,
554 .set_rate = stm32_set_rate,
557 U_BOOT_DRIVER(stm32fx_clk) = {
558 .name = "stm32fx_rcc_clock",
560 .ops = &stm32_clk_ops,
561 .probe = stm32_clk_probe,
562 .priv_auto_alloc_size = sizeof(struct stm32_clk),
563 .flags = DM_FLAG_PRE_RELOC,