blob: 93722f74ae4df4ebfae76eecdc5dc6610c442592 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vikas Manochae66c49f2016-02-11 15:47:20 -08002/*
Patrice Chotard3bc599c2017-10-23 09:53:58 +02003 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
Vikas Manochae66c49f2016-02-11 15:47:20 -08005 */
Patrice Chotard3bc599c2017-10-23 09:53:58 +02006
Vikas Manochae66c49f2016-02-11 15:47:20 -08007#include <common.h>
Vikas Manocha712f99a2017-02-12 10:25:45 -08008#include <clk-uclass.h>
9#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Patrice Chotard928954f2017-11-15 13:14:51 +010011#include <stm32_rcc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Patrice Chotardd0a768b2017-11-15 13:14:44 +010013
Vikas Manochae66c49f2016-02-11 15:47:20 -080014#include <asm/io.h>
Vikas Manochae66c49f2016-02-11 15:47:20 -080015#include <asm/arch/stm32.h>
Patrice Chotardd0a768b2017-11-15 13:14:44 +010016#include <asm/arch/stm32_pwr.h>
Vikas Manochae66c49f2016-02-11 15:47:20 -080017
Patrice Chotard288f17e2017-07-18 09:29:05 +020018#include <dt-bindings/mfd/stm32f7-rcc.h>
19
Michael Kurzbad51882017-01-22 16:04:24 +010020#define RCC_CR_HSION BIT(0)
21#define RCC_CR_HSEON BIT(16)
22#define RCC_CR_HSERDY BIT(17)
23#define RCC_CR_HSEBYP BIT(18)
24#define RCC_CR_CSSON BIT(19)
25#define RCC_CR_PLLON BIT(24)
26#define RCC_CR_PLLRDY BIT(25)
Patrice Chotard4e97e252017-11-15 13:14:52 +010027#define RCC_CR_PLLSAION BIT(28)
28#define RCC_CR_PLLSAIRDY BIT(29)
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090029
Michael Kurzbad51882017-01-22 16:04:24 +010030#define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
31#define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
32#define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
33#define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
34#define RCC_PLLCFGR_PLLSRC BIT(22)
35#define RCC_PLLCFGR_PLLM_SHIFT 0
36#define RCC_PLLCFGR_PLLN_SHIFT 6
37#define RCC_PLLCFGR_PLLP_SHIFT 16
38#define RCC_PLLCFGR_PLLQ_SHIFT 24
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090039
Michael Kurzbad51882017-01-22 16:04:24 +010040#define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
41#define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
42#define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
43#define RCC_CFGR_SW0 BIT(0)
44#define RCC_CFGR_SW1 BIT(1)
45#define RCC_CFGR_SW_MASK GENMASK(1, 0)
46#define RCC_CFGR_SW_HSI 0
47#define RCC_CFGR_SW_HSE RCC_CFGR_SW0
48#define RCC_CFGR_SW_PLL RCC_CFGR_SW1
49#define RCC_CFGR_SWS0 BIT(2)
50#define RCC_CFGR_SWS1 BIT(3)
51#define RCC_CFGR_SWS_MASK GENMASK(3, 2)
52#define RCC_CFGR_SWS_HSI 0
53#define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
54#define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
55#define RCC_CFGR_HPRE_SHIFT 4
56#define RCC_CFGR_PPRE1_SHIFT 10
57#define RCC_CFGR_PPRE2_SHIFT 13
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090058
Patrice Chotard526aa922018-02-08 17:20:46 +010059#define RCC_PLLSAICFGR_PLLSAIN_MASK GENMASK(14, 6)
60#define RCC_PLLSAICFGR_PLLSAIP_MASK GENMASK(17, 16)
Patrice Chotard1038e032018-02-08 17:20:48 +010061#define RCC_PLLSAICFGR_PLLSAIQ_MASK GENMASK(27, 24)
62#define RCC_PLLSAICFGR_PLLSAIR_MASK GENMASK(30, 28)
Patrice Chotard4e97e252017-11-15 13:14:52 +010063#define RCC_PLLSAICFGR_PLLSAIN_SHIFT 6
64#define RCC_PLLSAICFGR_PLLSAIP_SHIFT 16
Patrice Chotard1038e032018-02-08 17:20:48 +010065#define RCC_PLLSAICFGR_PLLSAIQ_SHIFT 24
66#define RCC_PLLSAICFGR_PLLSAIR_SHIFT 28
Patrice Chotard990dba62018-01-19 18:02:40 +010067#define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16)
Patrice Chotard4e97e252017-11-15 13:14:52 +010068#define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26)
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +010069#define RCC_PLLSAICFGR_PLLSAIR_3 BIT(29) | BIT(28)
Patrice Chotard4e97e252017-11-15 13:14:52 +010070
Patrice Chotard61803a92018-02-07 10:44:46 +010071#define RCC_DCKCFGRX_TIMPRE BIT(24)
Patrice Chotard4e97e252017-11-15 13:14:52 +010072#define RCC_DCKCFGRX_CK48MSEL BIT(27)
73#define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
74#define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
75
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +010076#define RCC_DCKCFGR_PLLSAIDIVR_SHIFT 16
77#define RCC_DCKCFGR_PLLSAIDIVR_MASK GENMASK(17, 16)
78#define RCC_DCKCFGR_PLLSAIDIVR_2 0
79
Patrice Chotardc88c6a92017-11-15 13:14:49 +010080/*
81 * RCC AHB1ENR specific definitions
82 */
83#define RCC_AHB1ENR_ETHMAC_EN BIT(25)
84#define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
85#define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
86
87/*
88 * RCC APB1ENR specific definitions
89 */
90#define RCC_APB1ENR_TIM2EN BIT(0)
91#define RCC_APB1ENR_PWREN BIT(28)
92
93/*
94 * RCC APB2ENR specific definitions
95 */
96#define RCC_APB2ENR_SYSCFGEN BIT(14)
Patrice Chotard20fe38e2018-01-18 14:10:05 +010097#define RCC_APB2ENR_SAI1EN BIT(22)
Patrice Chotardc88c6a92017-11-15 13:14:49 +010098
Patrice Chotard1038e032018-02-08 17:20:48 +010099enum pllsai_div {
100 PLLSAIP,
101 PLLSAIQ,
102 PLLSAIR,
103};
104
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100105static const struct stm32_clk_info stm32f4_clk_info = {
Patrice Chotardf9333c92017-11-15 13:14:47 +0100106 /* 180 MHz */
107 .sys_pll_psc = {
Patrice Chotardf9333c92017-11-15 13:14:47 +0100108 .pll_n = 360,
109 .pll_p = 2,
110 .pll_q = 8,
111 .ahb_psc = AHB_PSC_1,
112 .apb1_psc = APB_PSC_4,
113 .apb2_psc = APB_PSC_2,
114 },
115 .has_overdrive = false,
Patrice Chotard4e97e252017-11-15 13:14:52 +0100116 .v2 = false,
Patrice Chotardf9333c92017-11-15 13:14:47 +0100117};
118
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100119static const struct stm32_clk_info stm32f7_clk_info = {
Patrice Chotardf9333c92017-11-15 13:14:47 +0100120 /* 200 MHz */
121 .sys_pll_psc = {
Patrice Chotardf9333c92017-11-15 13:14:47 +0100122 .pll_n = 400,
123 .pll_p = 2,
124 .pll_q = 8,
125 .ahb_psc = AHB_PSC_1,
126 .apb1_psc = APB_PSC_4,
127 .apb2_psc = APB_PSC_2,
128 },
129 .has_overdrive = true,
Patrice Chotard4e97e252017-11-15 13:14:52 +0100130 .v2 = true,
Patrice Chotardf9333c92017-11-15 13:14:47 +0100131};
132
Patrice Chotard199a2172017-07-18 09:29:04 +0200133struct stm32_clk {
134 struct stm32_rcc_regs *base;
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100135 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100136 struct stm32_clk_info info;
137 unsigned long hse_rate;
Patrice Chotard8b414642018-04-11 17:07:45 +0200138 bool pllsaip;
Patrice Chotard199a2172017-07-18 09:29:04 +0200139};
140
Patrice Chotard5e993502018-02-08 17:20:50 +0100141#ifdef CONFIG_VIDEO_STM32
142static const u8 plldivr_table[] = { 0, 0, 2, 3, 4, 5, 6, 7 };
143#endif
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100144static const u8 pllsaidivr_table[] = { 2, 4, 8, 16 };
145
Patrice Chotard199a2172017-07-18 09:29:04 +0200146static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900147{
Patrice Chotard199a2172017-07-18 09:29:04 +0200148 struct stm32_clk *priv = dev_get_priv(dev);
149 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100150 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100151 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
Patrice Chotard199a2172017-07-18 09:29:04 +0200152
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900153 /* Reset RCC configuration */
Patrice Chotard199a2172017-07-18 09:29:04 +0200154 setbits_le32(&regs->cr, RCC_CR_HSION);
155 writel(0, &regs->cfgr); /* Reset CFGR */
156 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard4e97e252017-11-15 13:14:52 +0100157 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotard199a2172017-07-18 09:29:04 +0200158 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
159 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
160 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900161
162 /* Configure for HSE+PLL operation */
Patrice Chotard199a2172017-07-18 09:29:04 +0200163 setbits_le32(&regs->cr, RCC_CR_HSEON);
164 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900165 ;
166
Patrice Chotard199a2172017-07-18 09:29:04 +0200167 setbits_le32(&regs->cfgr, ((
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100168 sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT)
169 | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT)
170 | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900171
172 /* Configure the main PLL */
Patrice Chotard1543bf72017-10-26 13:23:19 +0200173 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
174 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100175 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
Patrice Chotard1543bf72017-10-26 13:23:19 +0200176 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100177 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
Patrice Chotard1543bf72017-10-26 13:23:19 +0200178 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100179 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
Patrice Chotard1543bf72017-10-26 13:23:19 +0200180 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100181 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900182
Patrice Chotard651a70e2018-02-08 17:20:47 +0100183 /* configure SDMMC clock */
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100184 if (priv->info.v2) { /*stm32f7 case */
Patrice Chotard8b414642018-04-11 17:07:45 +0200185 if (priv->pllsaip)
186 /* select PLLSAIP as 48MHz clock source */
187 setbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
188 else
189 /* select PLLQ as 48MHz clock source */
190 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
Patrice Chotard4e97e252017-11-15 13:14:52 +0100191
192 /* select 48MHz as SDMMC1 clock source */
193 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
194
195 /* select 48MHz as SDMMC2 clock source */
196 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
197 } else { /* stm32f4 case */
Patrice Chotard8b414642018-04-11 17:07:45 +0200198 if (priv->pllsaip)
199 /* select PLLSAIP as 48MHz clock source */
200 setbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
201 else
202 /* select PLLQ as 48MHz clock source */
203 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
Patrice Chotard4e97e252017-11-15 13:14:52 +0100204
205 /* select 48MHz as SDMMC1 clock source */
206 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
207 }
208
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100209 /*
Patrice Chotard8b414642018-04-11 17:07:45 +0200210 * Configure the SAI PLL to generate LTDC pixel clock and
211 * 48 Mhz for SDMMC and USB
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100212 */
Patrice Chotard8b414642018-04-11 17:07:45 +0200213 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIP_MASK,
214 RCC_PLLSAICFGR_PLLSAIP_4);
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100215 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK,
216 RCC_PLLSAICFGR_PLLSAIR_3);
217 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIN_MASK,
218 195 << RCC_PLLSAICFGR_PLLSAIN_SHIFT);
219
220 clrsetbits_le32(&regs->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK,
221 RCC_DCKCFGR_PLLSAIDIVR_2 << RCC_DCKCFGR_PLLSAIDIVR_SHIFT);
Patrice Chotard8b414642018-04-11 17:07:45 +0200222
Patrice Chotard651a70e2018-02-08 17:20:47 +0100223 /* Enable the main PLL */
224 setbits_le32(&regs->cr, RCC_CR_PLLON);
225 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Patrice Chotard4e97e252017-11-15 13:14:52 +0100226 ;
227
Patrice Chotard8b414642018-04-11 17:07:45 +0200228 /* Enable the SAI PLL */
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100229 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
230 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
231 ;
Patrice Chotard199a2172017-07-18 09:29:04 +0200232 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotardf9333c92017-11-15 13:14:47 +0100233
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100234 if (priv->info.has_overdrive) {
Patrice Chotardf9333c92017-11-15 13:14:47 +0100235 /*
236 * Enable high performance mode
237 * System frequency up to 200 MHz
238 */
239 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
240 /* Infinite wait! */
241 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
242 ;
243 /* Enable the Over-drive switch */
244 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
245 /* Infinite wait! */
246 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
247 ;
248 }
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900249
250 stm32_flash_latency_cfg(5);
Patrice Chotard199a2172017-07-18 09:29:04 +0200251 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
252 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900253
Patrice Chotard199a2172017-07-18 09:29:04 +0200254 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900255 RCC_CFGR_SWS_PLL)
256 ;
257
Patrice Chotard20fe38e2018-01-18 14:10:05 +0100258#ifdef CONFIG_ETH_DESIGNWARE
259 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
260 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SYSCFGEN);
261#endif
262
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900263 return 0;
264}
265
Patrice Chotard1038e032018-02-08 17:20:48 +0100266static bool stm32_clk_get_ck48msel(struct stm32_clk *priv)
Patrice Chotard4e97e252017-11-15 13:14:52 +0100267{
268 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard4e97e252017-11-15 13:14:52 +0100269
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100270 if (priv->info.v2) /*stm32f7 case */
Patrice Chotard1038e032018-02-08 17:20:48 +0100271 return readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
Patrice Chotard4e97e252017-11-15 13:14:52 +0100272 else
Patrice Chotard4e97e252017-11-15 13:14:52 +0100273
Patrice Chotard1038e032018-02-08 17:20:48 +0100274 return readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
275}
276
277static unsigned long stm32_clk_get_pllsai_vco_rate(struct stm32_clk *priv)
278{
279 struct stm32_rcc_regs *regs = priv->base;
280 u16 pllm, pllsain;
281
282 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
283 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLSAICFGR_PLLSAIN_MASK)
284 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
285
286 return ((priv->hse_rate / pllm) * pllsain);
287}
288
289static unsigned long stm32_clk_get_pllsai_rate(struct stm32_clk *priv,
290 enum pllsai_div output)
291{
292 struct stm32_rcc_regs *regs = priv->base;
293 u16 pll_div_output;
294
295 switch (output) {
296 case PLLSAIP:
297 pll_div_output = ((((readl(&regs->pllsaicfgr)
298 & RCC_PLLSAICFGR_PLLSAIP_MASK)
299 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
300 break;
301 case PLLSAIQ:
302 pll_div_output = (readl(&regs->pllsaicfgr)
303 & RCC_PLLSAICFGR_PLLSAIQ_MASK)
304 >> RCC_PLLSAICFGR_PLLSAIQ_SHIFT;
305 break;
306 case PLLSAIR:
307 pll_div_output = (readl(&regs->pllsaicfgr)
308 & RCC_PLLSAICFGR_PLLSAIR_MASK)
309 >> RCC_PLLSAICFGR_PLLSAIR_SHIFT;
310 break;
311 default:
312 pr_err("incorrect PLLSAI output %d\n", output);
313 return -EINVAL;
Patrice Chotard4e97e252017-11-15 13:14:52 +0100314 }
Patrice Chotard1038e032018-02-08 17:20:48 +0100315
316 return (stm32_clk_get_pllsai_vco_rate(priv) / pll_div_output);
Patrice Chotard4e97e252017-11-15 13:14:52 +0100317}
318
Patrice Chotard61803a92018-02-07 10:44:46 +0100319static bool stm32_get_timpre(struct stm32_clk *priv)
Patrice Chotard288f17e2017-07-18 09:29:05 +0200320{
Patrice Chotard288f17e2017-07-18 09:29:05 +0200321 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard61803a92018-02-07 10:44:46 +0100322 u32 val;
323
324 if (priv->info.v2) /*stm32f7 case */
325 val = readl(&regs->dckcfgr2);
326 else
327 val = readl(&regs->dckcfgr);
328 /* get timer prescaler */
329 return !!(val & RCC_DCKCFGRX_TIMPRE);
330}
331
332static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk)
333{
334 u8 shift;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200335 /* Prescaler table lookups for clock computation */
336 u8 ahb_psc_table[16] = {
337 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
338 };
Patrice Chotard61803a92018-02-07 10:44:46 +0100339
340 shift = ahb_psc_table[(
341 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
342 >> RCC_CFGR_HPRE_SHIFT)];
343
344 return sysclk >> shift;
345};
346
347static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb)
348{
349 /* Prescaler table lookups for clock computation */
Patrice Chotard288f17e2017-07-18 09:29:05 +0200350 u8 apb_psc_table[8] = {
351 0, 0, 0, 0, 1, 2, 3, 4
352 };
353
Patrice Chotard61803a92018-02-07 10:44:46 +0100354 if (apb == APB1)
355 return apb_psc_table[(
356 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
357 >> RCC_CFGR_PPRE1_SHIFT)];
358 else /* APB2 */
359 return apb_psc_table[(
360 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
361 >> RCC_CFGR_PPRE2_SHIFT)];
362};
363
364static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk,
365 enum apb apb)
366{
367 struct stm32_rcc_regs *regs = priv->base;
368 u8 shift = stm32_get_apb_shift(regs, apb);
369
370 if (stm32_get_timpre(priv))
371 /*
372 * if APB prescaler is configured to a
373 * division factor of 1, 2 or 4
374 */
375 switch (shift) {
376 case 0:
377 case 1:
378 case 2:
379 return stm32_get_hclk_rate(regs, sysclk);
380 default:
381 return (sysclk >> shift) * 4;
382 }
383 else
384 /*
385 * if APB prescaler is configured to a
386 * division factor of 1
387 */
388 if (shift == 0)
389 return sysclk;
390 else
391 return (sysclk >> shift) * 2;
392};
393
394static ulong stm32_clk_get_rate(struct clk *clk)
395{
396 struct stm32_clk *priv = dev_get_priv(clk->dev);
397 struct stm32_rcc_regs *regs = priv->base;
398 u32 sysclk = 0;
Patrice Chotardaa230be2018-02-08 17:20:45 +0100399 u32 vco;
Patrice Chotard1038e032018-02-08 17:20:48 +0100400 u32 sdmmcxsel_bit;
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100401 u32 saidivr;
402 u32 pllsai_rate;
Patrice Chotard1038e032018-02-08 17:20:48 +0100403 u16 pllm, plln, pllp, pllq;
Patrice Chotard61803a92018-02-07 10:44:46 +0100404
Patrice Chotard288f17e2017-07-18 09:29:05 +0200405 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
406 RCC_CFGR_SWS_PLL) {
Patrice Chotard288f17e2017-07-18 09:29:05 +0200407 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
408 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
409 >> RCC_PLLCFGR_PLLN_SHIFT);
410 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
411 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
Patrice Chotard1038e032018-02-08 17:20:48 +0100412 pllq = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
413 >> RCC_PLLCFGR_PLLQ_SHIFT);
Patrice Chotardaa230be2018-02-08 17:20:45 +0100414 vco = (priv->hse_rate / pllm) * plln;
415 sysclk = vco / pllp;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200416 } else {
417 return -EINVAL;
418 }
419
420 switch (clk->id) {
421 /*
422 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
423 * AHB1, AHB2 and AHB3
424 */
425 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
Patrice Chotard61803a92018-02-07 10:44:46 +0100426 return stm32_get_hclk_rate(regs, sysclk);
Patrice Chotard288f17e2017-07-18 09:29:05 +0200427 /* APB1 CLOCK */
428 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
Patrice Chotard61803a92018-02-07 10:44:46 +0100429 /* For timer clock, an additionnal prescaler is used*/
430 switch (clk->id) {
431 case STM32F7_APB1_CLOCK(TIM2):
432 case STM32F7_APB1_CLOCK(TIM3):
433 case STM32F7_APB1_CLOCK(TIM4):
434 case STM32F7_APB1_CLOCK(TIM5):
435 case STM32F7_APB1_CLOCK(TIM6):
436 case STM32F7_APB1_CLOCK(TIM7):
437 case STM32F7_APB1_CLOCK(TIM12):
438 case STM32F7_APB1_CLOCK(TIM13):
439 case STM32F7_APB1_CLOCK(TIM14):
440 return stm32_get_timer_rate(priv, sysclk, APB1);
441 }
442 return (sysclk >> stm32_get_apb_shift(regs, APB1));
443
Patrice Chotard288f17e2017-07-18 09:29:05 +0200444 /* APB2 CLOCK */
Patrice Chotard6243c882018-02-08 17:20:51 +0100445 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(DSI):
Patrice Chotard1038e032018-02-08 17:20:48 +0100446 switch (clk->id) {
Patrice Chotard4e97e252017-11-15 13:14:52 +0100447 /*
448 * particular case for SDMMC1 and SDMMC2 :
449 * 48Mhz source clock can be from main PLL or from
Patrice Chotard1038e032018-02-08 17:20:48 +0100450 * PLLSAIP
Patrice Chotard4e97e252017-11-15 13:14:52 +0100451 */
Patrice Chotard4e97e252017-11-15 13:14:52 +0100452 case STM32F7_APB2_CLOCK(SDMMC1):
Patrice Chotard1038e032018-02-08 17:20:48 +0100453 case STM32F7_APB2_CLOCK(SDMMC2):
454 if (clk->id == STM32F7_APB2_CLOCK(SDMMC1))
455 sdmmcxsel_bit = RCC_DCKCFGRX_SDMMC1SEL;
456 else
457 sdmmcxsel_bit = RCC_DCKCFGR2_SDMMC2SEL;
458
459 if (readl(&regs->dckcfgr2) & sdmmcxsel_bit)
Patrice Chotard4e97e252017-11-15 13:14:52 +0100460 /* System clock is selected as SDMMC1 clock */
461 return sysclk;
Patrice Chotard1038e032018-02-08 17:20:48 +0100462 /*
463 * 48 MHz can be generated by either PLLSAIP
464 * or by PLLQ depending of CK48MSEL bit of RCC_DCKCFGR
465 */
466 if (stm32_clk_get_ck48msel(priv))
467 return stm32_clk_get_pllsai_rate(priv, PLLSAIP);
Patrice Chotard4e97e252017-11-15 13:14:52 +0100468 else
Patrice Chotard1038e032018-02-08 17:20:48 +0100469 return (vco / pllq);
Patrice Chotard4e97e252017-11-15 13:14:52 +0100470 break;
Patrice Chotard4e97e252017-11-15 13:14:52 +0100471
Patrice Chotard61803a92018-02-07 10:44:46 +0100472 /* For timer clock, an additionnal prescaler is used*/
473 case STM32F7_APB2_CLOCK(TIM1):
474 case STM32F7_APB2_CLOCK(TIM8):
475 case STM32F7_APB2_CLOCK(TIM9):
476 case STM32F7_APB2_CLOCK(TIM10):
477 case STM32F7_APB2_CLOCK(TIM11):
478 return stm32_get_timer_rate(priv, sysclk, APB2);
479 break;
Patrice Chotarde8fb9ed2018-02-08 17:20:49 +0100480
481 /* particular case for LTDC clock */
482 case STM32F7_APB2_CLOCK(LTDC):
483 saidivr = readl(&regs->dckcfgr);
484 saidivr = (saidivr & RCC_DCKCFGR_PLLSAIDIVR_MASK)
485 >> RCC_DCKCFGR_PLLSAIDIVR_SHIFT;
486 pllsai_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR);
487
488 return pllsai_rate / pllsaidivr_table[saidivr];
Patrice Chotard61803a92018-02-07 10:44:46 +0100489 }
490 return (sysclk >> stm32_get_apb_shift(regs, APB2));
491
Patrice Chotard288f17e2017-07-18 09:29:05 +0200492 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900493 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard288f17e2017-07-18 09:29:05 +0200494 return -EINVAL;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200495 }
496}
497
Patrice Chotard05e23dd2018-01-29 18:14:14 +0100498static ulong stm32_set_rate(struct clk *clk, ulong rate)
499{
Patrice Chotard5e993502018-02-08 17:20:50 +0100500#ifdef CONFIG_VIDEO_STM32
501 struct stm32_clk *priv = dev_get_priv(clk->dev);
502 struct stm32_rcc_regs *regs = priv->base;
503 u32 pllsair_rate, pllsai_vco_rate, current_rate;
504 u32 best_div, best_diff, diff;
505 u16 div;
506 u8 best_plldivr, best_pllsaidivr;
507 u8 i, j;
508 bool found = false;
509
510 /* Only set_rate for LTDC clock is implemented */
511 if (clk->id != STM32F7_APB2_CLOCK(LTDC)) {
512 pr_err("set_rate not implemented for clock index %ld\n",
513 clk->id);
514 return 0;
515 }
516
517 if (rate == stm32_clk_get_rate(clk))
518 /* already set to requested rate */
519 return rate;
520
521 /* get the current PLLSAIR output freq */
522 pllsair_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR);
523 best_div = pllsair_rate / rate;
524
525 /* look into pllsaidivr_table if this divider is available*/
526 for (i = 0 ; i < sizeof(pllsaidivr_table); i++)
527 if (best_div == pllsaidivr_table[i]) {
528 /* set pll_saidivr with found value */
529 clrsetbits_le32(&regs->dckcfgr,
530 RCC_DCKCFGR_PLLSAIDIVR_MASK,
531 pllsaidivr_table[i]);
532 return rate;
533 }
534
535 /*
536 * As no pllsaidivr value is suitable to obtain requested freq,
537 * test all combination of pllsaidivr * pllsair and find the one
538 * which give freq closest to requested rate.
539 */
540
541 pllsai_vco_rate = stm32_clk_get_pllsai_vco_rate(priv);
542 best_diff = ULONG_MAX;
543 best_pllsaidivr = 0;
544 best_plldivr = 0;
545 /*
546 * start at index 2 of plldivr_table as divider value at index 0
547 * and 1 are 0)
548 */
549 for (i = 2; i < sizeof(plldivr_table); i++) {
550 for (j = 0; j < sizeof(pllsaidivr_table); j++) {
551 div = plldivr_table[i] * pllsaidivr_table[j];
552 current_rate = pllsai_vco_rate / div;
553 /* perfect combination is found ? */
554 if (current_rate == rate) {
555 best_pllsaidivr = j;
556 best_plldivr = i;
557 found = true;
558 break;
559 }
560
561 diff = (current_rate > rate) ?
562 current_rate - rate : rate - current_rate;
563
564 /* found a better combination ? */
565 if (diff < best_diff) {
566 best_diff = diff;
567 best_pllsaidivr = j;
568 best_plldivr = i;
569 }
570 }
571
572 if (found)
573 break;
574 }
575
576 /* Disable the SAI PLL */
577 clrbits_le32(&regs->cr, RCC_CR_PLLSAION);
578
579 /* set pll_saidivr with found value */
580 clrsetbits_le32(&regs->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK,
581 best_pllsaidivr << RCC_DCKCFGR_PLLSAIDIVR_SHIFT);
582
583 /* set pllsair with found value */
584 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK,
585 plldivr_table[best_plldivr]
586 << RCC_PLLSAICFGR_PLLSAIR_SHIFT);
587
588 /* Enable the SAI PLL */
589 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
590 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
591 ;
592
593 div = plldivr_table[best_plldivr] * pllsaidivr_table[best_pllsaidivr];
594 return pllsai_vco_rate / div;
595#else
Patrice Chotard05e23dd2018-01-29 18:14:14 +0100596 return 0;
Patrice Chotard5e993502018-02-08 17:20:50 +0100597#endif
Patrice Chotard05e23dd2018-01-29 18:14:14 +0100598}
599
Vikas Manocha712f99a2017-02-12 10:25:45 -0800600static int stm32_clk_enable(struct clk *clk)
601{
Patrice Chotard199a2172017-07-18 09:29:04 +0200602 struct stm32_clk *priv = dev_get_priv(clk->dev);
603 struct stm32_rcc_regs *regs = priv->base;
Vikas Manocha712f99a2017-02-12 10:25:45 -0800604 u32 offset = clk->id / 32;
605 u32 bit_index = clk->id % 32;
606
607 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
608 __func__, clk->id, offset, bit_index);
Patrice Chotard199a2172017-07-18 09:29:04 +0200609 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manocha712f99a2017-02-12 10:25:45 -0800610
611 return 0;
612}
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900613
Vikas Manocha712f99a2017-02-12 10:25:45 -0800614static int stm32_clk_probe(struct udevice *dev)
615{
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100616 struct ofnode_phandle_args args;
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100617 struct udevice *fixed_clock_dev = NULL;
618 struct clk clk;
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100619 int err;
620
Patrice Chotardf264e232017-11-15 13:14:48 +0100621 debug("%s\n", __func__);
Patrice Chotard199a2172017-07-18 09:29:04 +0200622
623 struct stm32_clk *priv = dev_get_priv(dev);
624 fdt_addr_t addr;
625
Patrice Chotardf9333c92017-11-15 13:14:47 +0100626 addr = dev_read_addr(dev);
Patrice Chotard199a2172017-07-18 09:29:04 +0200627 if (addr == FDT_ADDR_T_NONE)
628 return -EINVAL;
629
630 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard8b414642018-04-11 17:07:45 +0200631 priv->pllsaip = true;
Patrice Chotard928954f2017-11-15 13:14:51 +0100632
633 switch (dev_get_driver_data(dev)) {
Patrice Chotard8b414642018-04-11 17:07:45 +0200634 case STM32F42X:
635 priv->pllsaip = false;
636 /* fallback into STM32F469 case */
637 case STM32F469:
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100638 memcpy(&priv->info, &stm32f4_clk_info,
639 sizeof(struct stm32_clk_info));
Patrice Chotard928954f2017-11-15 13:14:51 +0100640 break;
Patrice Chotard8b414642018-04-11 17:07:45 +0200641
Patrice Chotard928954f2017-11-15 13:14:51 +0100642 case STM32F7:
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100643 memcpy(&priv->info, &stm32f7_clk_info,
644 sizeof(struct stm32_clk_info));
Patrice Chotard928954f2017-11-15 13:14:51 +0100645 break;
646 default:
647 return -EINVAL;
648 }
Patrice Chotard199a2172017-07-18 09:29:04 +0200649
Patrice Chotardcb97ff92018-01-18 13:39:30 +0100650 /* retrieve HSE frequency (external oscillator) */
651 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
652 &fixed_clock_dev);
653
654 if (err) {
655 pr_err("Can't find fixed clock (%d)", err);
656 return err;
657 }
658
659 err = clk_request(fixed_clock_dev, &clk);
660 if (err) {
661 pr_err("Can't request %s clk (%d)", fixed_clock_dev->name,
662 err);
663 return err;
664 }
665
666 /*
667 * set pllm factor accordingly to the external oscillator
668 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
669 * freq at 1MHz
670 * if input PLL frequency is 25Mhz, divide it by 25
671 */
672 clk.id = 0;
673 priv->hse_rate = clk_get_rate(&clk);
674
675 if (priv->hse_rate < 1000000) {
676 pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__,
677 priv->hse_rate);
678 return -EINVAL;
679 }
680
681 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
682
683 if (priv->info.has_overdrive) {
Patrice Chotardf9333c92017-11-15 13:14:47 +0100684 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
685 &args);
686 if (err) {
687 debug("%s: can't find syscon device (%d)\n", __func__,
688 err);
689 return err;
690 }
691
692 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100693 }
694
Patrice Chotard199a2172017-07-18 09:29:04 +0200695 configure_clocks(dev);
Vikas Manocha712f99a2017-02-12 10:25:45 -0800696
697 return 0;
698}
699
Simon Glassa4e0ef52017-05-18 20:09:40 -0600700static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manocha712f99a2017-02-12 10:25:45 -0800701{
702 debug("%s(clk=%p)\n", __func__, clk);
703
704 if (args->args_count != 2) {
705 debug("Invaild args_count: %d\n", args->args_count);
706 return -EINVAL;
707 }
708
709 if (args->args_count)
710 clk->id = args->args[1];
711 else
712 clk->id = 0;
713
714 return 0;
715}
716
717static struct clk_ops stm32_clk_ops = {
718 .of_xlate = stm32_clk_of_xlate,
719 .enable = stm32_clk_enable,
Patrice Chotard288f17e2017-07-18 09:29:05 +0200720 .get_rate = stm32_clk_get_rate,
Patrice Chotard05e23dd2018-01-29 18:14:14 +0100721 .set_rate = stm32_set_rate,
Vikas Manocha712f99a2017-02-12 10:25:45 -0800722};
723
Patrice Chotardf264e232017-11-15 13:14:48 +0100724U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard928954f2017-11-15 13:14:51 +0100725 .name = "stm32fx_rcc_clock",
Patrice Chotard0cc40df2017-09-21 10:08:09 +0200726 .id = UCLASS_CLK,
Patrice Chotard0cc40df2017-09-21 10:08:09 +0200727 .ops = &stm32_clk_ops,
728 .probe = stm32_clk_probe,
729 .priv_auto_alloc_size = sizeof(struct stm32_clk),
730 .flags = DM_FLAG_PRE_RELOC,
Vikas Manocha712f99a2017-02-12 10:25:45 -0800731};