blob: 63116e0bac35c73df17139caee65f6ef94c45a3a [file] [log] [blame]
Vikas Manochae66c49f2016-02-11 15:47:20 -08001/*
Patrice Chotard3bc599c2017-10-23 09:53:58 +02002 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
3 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
Vikas Manochae66c49f2016-02-11 15:47:20 -08004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
Patrice Chotard3bc599c2017-10-23 09:53:58 +02007
Vikas Manochae66c49f2016-02-11 15:47:20 -08008#include <common.h>
Vikas Manocha712f99a2017-02-12 10:25:45 -08009#include <clk-uclass.h>
10#include <dm.h>
Patrice Chotard928954f2017-11-15 13:14:51 +010011#include <stm32_rcc.h>
Patrice Chotardd0a768b2017-11-15 13:14:44 +010012
Vikas Manochae66c49f2016-02-11 15:47:20 -080013#include <asm/io.h>
Vikas Manochae66c49f2016-02-11 15:47:20 -080014#include <asm/arch/stm32.h>
Patrice Chotardd0a768b2017-11-15 13:14:44 +010015#include <asm/arch/stm32_pwr.h>
Vikas Manochae66c49f2016-02-11 15:47:20 -080016
Patrice Chotard288f17e2017-07-18 09:29:05 +020017#include <dt-bindings/mfd/stm32f7-rcc.h>
18
Michael Kurzbad51882017-01-22 16:04:24 +010019#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)
Patrice Chotard4e97e252017-11-15 13:14:52 +010026#define RCC_CR_PLLSAION BIT(28)
27#define RCC_CR_PLLSAIRDY BIT(29)
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090028
Michael Kurzbad51882017-01-22 16:04:24 +010029#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
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090038
Michael Kurzbad51882017-01-22 16:04:24 +010039#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
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090057
Patrice Chotard4e97e252017-11-15 13:14:52 +010058#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(17)
63#define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26)
64#define RCC_PLLSAICFGR_PLLSAIR_2 BIT(29)
65
66#define RCC_DCKCFGRX_CK48MSEL BIT(27)
67#define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
68#define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
69
70#define RCC_APB2ENR_SAI1EN BIT(22)
71
Patrice Chotardc88c6a92017-11-15 13:14:49 +010072/*
73 * RCC AHB1ENR specific definitions
74 */
75#define RCC_AHB1ENR_ETHMAC_EN BIT(25)
76#define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
77#define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
78
79/*
80 * RCC APB1ENR specific definitions
81 */
82#define RCC_APB1ENR_TIM2EN BIT(0)
83#define RCC_APB1ENR_PWREN BIT(28)
84
85/*
86 * RCC APB2ENR specific definitions
87 */
88#define RCC_APB2ENR_SYSCFGEN BIT(14)
89
Patrice Chotard4a56fd42017-12-12 09:49:39 +010090enum periph_clock {
91 SYSCFG_CLOCK_CFG,
92 TIMER2_CLOCK_CFG,
93 STMMAC_CLOCK_CFG,
94};
95
Patrice Chotardf9333c92017-11-15 13:14:47 +010096struct stm32_clk_info stm32f4_clk_info = {
97 /* 180 MHz */
98 .sys_pll_psc = {
99 .pll_m = 8,
100 .pll_n = 360,
101 .pll_p = 2,
102 .pll_q = 8,
103 .ahb_psc = AHB_PSC_1,
104 .apb1_psc = APB_PSC_4,
105 .apb2_psc = APB_PSC_2,
106 },
107 .has_overdrive = false,
Patrice Chotard4e97e252017-11-15 13:14:52 +0100108 .v2 = false,
Patrice Chotardf9333c92017-11-15 13:14:47 +0100109};
110
111struct stm32_clk_info stm32f7_clk_info = {
112 /* 200 MHz */
113 .sys_pll_psc = {
114 .pll_m = 25,
115 .pll_n = 400,
116 .pll_p = 2,
117 .pll_q = 8,
118 .ahb_psc = AHB_PSC_1,
119 .apb1_psc = APB_PSC_4,
120 .apb2_psc = APB_PSC_2,
121 },
122 .has_overdrive = true,
Patrice Chotard4e97e252017-11-15 13:14:52 +0100123 .v2 = true,
Patrice Chotardf9333c92017-11-15 13:14:47 +0100124};
125
Patrice Chotard199a2172017-07-18 09:29:04 +0200126struct stm32_clk {
127 struct stm32_rcc_regs *base;
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100128 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardf9333c92017-11-15 13:14:47 +0100129 struct stm32_clk_info *info;
Patrice Chotard199a2172017-07-18 09:29:04 +0200130};
131
Patrice Chotard199a2172017-07-18 09:29:04 +0200132static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900133{
Patrice Chotard199a2172017-07-18 09:29:04 +0200134 struct stm32_clk *priv = dev_get_priv(dev);
135 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100136 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardf9333c92017-11-15 13:14:47 +0100137 struct pll_psc sys_pll_psc = priv->info->sys_pll_psc;
Patrice Chotard4e97e252017-11-15 13:14:52 +0100138 u32 pllsaicfgr = 0;
Patrice Chotard199a2172017-07-18 09:29:04 +0200139
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900140 /* Reset RCC configuration */
Patrice Chotard199a2172017-07-18 09:29:04 +0200141 setbits_le32(&regs->cr, RCC_CR_HSION);
142 writel(0, &regs->cfgr); /* Reset CFGR */
143 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard4e97e252017-11-15 13:14:52 +0100144 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotard199a2172017-07-18 09:29:04 +0200145 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
146 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
147 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900148
149 /* Configure for HSE+PLL operation */
Patrice Chotard199a2172017-07-18 09:29:04 +0200150 setbits_le32(&regs->cr, RCC_CR_HSEON);
151 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900152 ;
153
Patrice Chotard199a2172017-07-18 09:29:04 +0200154 setbits_le32(&regs->cfgr, ((
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900155 sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
156 | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
157 | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
158
159 /* Configure the main PLL */
Patrice Chotard1543bf72017-10-26 13:23:19 +0200160 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
161 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
162 sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT);
163 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
164 sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT);
165 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
166 ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
167 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
168 sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900169
Patrice Chotard4e97e252017-11-15 13:14:52 +0100170 /* Configure the SAI PLL to get a 48 MHz source */
171 pllsaicfgr = RCC_PLLSAICFGR_PLLSAIR_2 | RCC_PLLSAICFGR_PLLSAIQ_4 |
172 RCC_PLLSAICFGR_PLLSAIP_4;
173 pllsaicfgr |= 192 << RCC_PLLSAICFGR_PLLSAIN_SHIFT;
174 writel(pllsaicfgr, &regs->pllsaicfgr);
175
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900176 /* Enable the main PLL */
Patrice Chotard199a2172017-07-18 09:29:04 +0200177 setbits_le32(&regs->cr, RCC_CR_PLLON);
178 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900179 ;
180
Patrice Chotard4e97e252017-11-15 13:14:52 +0100181 if (priv->info->v2) { /*stm32f7 case */
182 /* select PLLSAI as 48MHz clock source */
183 setbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
184
185 /* select 48MHz as SDMMC1 clock source */
186 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
187
188 /* select 48MHz as SDMMC2 clock source */
189 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
190 } else { /* stm32f4 case */
191 /* select PLLSAI as 48MHz clock source */
192 setbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
193
194 /* select 48MHz as SDMMC1 clock source */
195 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
196 }
197
198 /* Enable the SAI PLL */
199 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
200 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
201 ;
202
Patrice Chotard199a2172017-07-18 09:29:04 +0200203 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotardf9333c92017-11-15 13:14:47 +0100204
205 if (priv->info->has_overdrive) {
206 /*
207 * Enable high performance mode
208 * System frequency up to 200 MHz
209 */
210 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
211 /* Infinite wait! */
212 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
213 ;
214 /* Enable the Over-drive switch */
215 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
216 /* Infinite wait! */
217 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
218 ;
219 }
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900220
221 stm32_flash_latency_cfg(5);
Patrice Chotard199a2172017-07-18 09:29:04 +0200222 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
223 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900224
Patrice Chotard199a2172017-07-18 09:29:04 +0200225 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900226 RCC_CFGR_SWS_PLL)
227 ;
Patrice Chotard4e97e252017-11-15 13:14:52 +0100228 /* gate the SAI clock, needed for MMC 1&2 clocks */
229 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SAI1EN);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900230
231 return 0;
232}
233
Patrice Chotard4e97e252017-11-15 13:14:52 +0100234static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv,
235 u32 sysclk)
236{
237 struct stm32_rcc_regs *regs = priv->base;
238 u16 pllq, pllm, pllsain, pllsaip;
239 bool pllsai;
240
241 pllq = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
242 >> RCC_PLLCFGR_PLLQ_SHIFT;
243
244 if (priv->info->v2) /*stm32f7 case */
245 pllsai = readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
246 else
247 pllsai = readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
248
249 if (pllsai) {
250 /* PLL48CLK is selected from PLLSAI, get PLLSAI value */
251 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
252 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK)
253 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
254 pllsaip = ((((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK)
255 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
256 return ((CONFIG_STM32_HSE_HZ / pllm) * pllsain) / pllsaip;
257 }
258 /* PLL48CLK is selected from PLLQ */
259 return sysclk / pllq;
260}
261
Patrice Chotard288f17e2017-07-18 09:29:05 +0200262static unsigned long stm32_clk_get_rate(struct clk *clk)
263{
264 struct stm32_clk *priv = dev_get_priv(clk->dev);
265 struct stm32_rcc_regs *regs = priv->base;
266 u32 sysclk = 0;
267 u32 shift = 0;
Patrice Chotardf264e232017-11-15 13:14:48 +0100268 u16 pllm, plln, pllp;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200269 /* Prescaler table lookups for clock computation */
270 u8 ahb_psc_table[16] = {
271 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
272 };
273 u8 apb_psc_table[8] = {
274 0, 0, 0, 0, 1, 2, 3, 4
275 };
276
277 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
278 RCC_CFGR_SWS_PLL) {
Patrice Chotard288f17e2017-07-18 09:29:05 +0200279 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
280 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
281 >> RCC_PLLCFGR_PLLN_SHIFT);
282 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
283 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
284 sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
285 } else {
286 return -EINVAL;
287 }
288
289 switch (clk->id) {
290 /*
291 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
292 * AHB1, AHB2 and AHB3
293 */
294 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
295 shift = ahb_psc_table[(
296 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
297 >> RCC_CFGR_HPRE_SHIFT)];
298 return sysclk >>= shift;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200299 /* APB1 CLOCK */
300 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
301 shift = apb_psc_table[(
302 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
303 >> RCC_CFGR_PPRE1_SHIFT)];
304 return sysclk >>= shift;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200305 /* APB2 CLOCK */
306 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
Patrice Chotard4e97e252017-11-15 13:14:52 +0100307 /*
308 * particular case for SDMMC1 and SDMMC2 :
309 * 48Mhz source clock can be from main PLL or from
310 * SAI PLL
311 */
312 switch (clk->id) {
313 case STM32F7_APB2_CLOCK(SDMMC1):
314 if (readl(&regs->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL)
315 /* System clock is selected as SDMMC1 clock */
316 return sysclk;
317 else
318 return stm32_clk_pll48clk_rate(priv, sysclk);
319 break;
320 case STM32F7_APB2_CLOCK(SDMMC2):
321 if (readl(&regs->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL)
322 /* System clock is selected as SDMMC2 clock */
323 return sysclk;
324 else
325 return stm32_clk_pll48clk_rate(priv, sysclk);
326 break;
327 }
328
Patrice Chotard288f17e2017-07-18 09:29:05 +0200329 shift = apb_psc_table[(
330 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
331 >> RCC_CFGR_PPRE2_SHIFT)];
332 return sysclk >>= shift;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200333 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900334 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard288f17e2017-07-18 09:29:05 +0200335 return -EINVAL;
Patrice Chotard288f17e2017-07-18 09:29:05 +0200336 }
337}
338
Vikas Manocha712f99a2017-02-12 10:25:45 -0800339static int stm32_clk_enable(struct clk *clk)
340{
Patrice Chotard199a2172017-07-18 09:29:04 +0200341 struct stm32_clk *priv = dev_get_priv(clk->dev);
342 struct stm32_rcc_regs *regs = priv->base;
Vikas Manocha712f99a2017-02-12 10:25:45 -0800343 u32 offset = clk->id / 32;
344 u32 bit_index = clk->id % 32;
345
346 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
347 __func__, clk->id, offset, bit_index);
Patrice Chotard199a2172017-07-18 09:29:04 +0200348 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manocha712f99a2017-02-12 10:25:45 -0800349
350 return 0;
351}
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900352
Vikas Manochae66c49f2016-02-11 15:47:20 -0800353void clock_setup(int peripheral)
354{
355 switch (peripheral) {
Michael Kurz081de092017-01-22 16:04:26 +0100356 case SYSCFG_CLOCK_CFG:
357 setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
358 break;
359 case TIMER2_CLOCK_CFG:
360 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
361 break;
Michael Kurzb20b70f2017-01-22 16:04:27 +0100362 case STMMAC_CLOCK_CFG:
363 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
364 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
365 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
366 break;
Vikas Manochae66c49f2016-02-11 15:47:20 -0800367 default:
368 break;
369 }
370}
Vikas Manocha712f99a2017-02-12 10:25:45 -0800371
372static int stm32_clk_probe(struct udevice *dev)
373{
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100374 struct ofnode_phandle_args args;
375 int err;
376
Patrice Chotardf264e232017-11-15 13:14:48 +0100377 debug("%s\n", __func__);
Patrice Chotard199a2172017-07-18 09:29:04 +0200378
379 struct stm32_clk *priv = dev_get_priv(dev);
380 fdt_addr_t addr;
381
Patrice Chotardf9333c92017-11-15 13:14:47 +0100382 addr = dev_read_addr(dev);
Patrice Chotard199a2172017-07-18 09:29:04 +0200383 if (addr == FDT_ADDR_T_NONE)
384 return -EINVAL;
385
386 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard928954f2017-11-15 13:14:51 +0100387
388 switch (dev_get_driver_data(dev)) {
389 case STM32F4:
390 priv->info = &stm32f4_clk_info;
391 break;
392 case STM32F7:
393 priv->info = &stm32f7_clk_info;
394 break;
395 default:
396 return -EINVAL;
397 }
Patrice Chotard199a2172017-07-18 09:29:04 +0200398
Patrice Chotardf9333c92017-11-15 13:14:47 +0100399 if (priv->info->has_overdrive) {
400 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
401 &args);
402 if (err) {
403 debug("%s: can't find syscon device (%d)\n", __func__,
404 err);
405 return err;
406 }
407
408 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100409 }
410
Patrice Chotard199a2172017-07-18 09:29:04 +0200411 configure_clocks(dev);
Vikas Manocha712f99a2017-02-12 10:25:45 -0800412
413 return 0;
414}
415
Simon Glassa4e0ef52017-05-18 20:09:40 -0600416static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manocha712f99a2017-02-12 10:25:45 -0800417{
418 debug("%s(clk=%p)\n", __func__, clk);
419
420 if (args->args_count != 2) {
421 debug("Invaild args_count: %d\n", args->args_count);
422 return -EINVAL;
423 }
424
425 if (args->args_count)
426 clk->id = args->args[1];
427 else
428 clk->id = 0;
429
430 return 0;
431}
432
433static struct clk_ops stm32_clk_ops = {
434 .of_xlate = stm32_clk_of_xlate,
435 .enable = stm32_clk_enable,
Patrice Chotard288f17e2017-07-18 09:29:05 +0200436 .get_rate = stm32_clk_get_rate,
Vikas Manocha712f99a2017-02-12 10:25:45 -0800437};
438
Patrice Chotardf264e232017-11-15 13:14:48 +0100439U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard928954f2017-11-15 13:14:51 +0100440 .name = "stm32fx_rcc_clock",
Patrice Chotard0cc40df2017-09-21 10:08:09 +0200441 .id = UCLASS_CLK,
Patrice Chotard0cc40df2017-09-21 10:08:09 +0200442 .ops = &stm32_clk_ops,
443 .probe = stm32_clk_probe,
444 .priv_auto_alloc_size = sizeof(struct stm32_clk),
445 .flags = DM_FLAG_PRE_RELOC,
Vikas Manocha712f99a2017-02-12 10:25:45 -0800446};