blob: bde31607cfe90a7fabc513046019e1f5ab7f4c5d [file] [log] [blame]
Vikas Manocha1b51c932016-02-11 15:47:20 -08001/*
Patrice Chotard789ee0e2017-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 Manocha1b51c932016-02-11 15:47:20 -08004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
Patrice Chotard789ee0e2017-10-23 09:53:58 +02007
Vikas Manocha1b51c932016-02-11 15:47:20 -08008#include <common.h>
Vikas Manochadaaeaab2017-02-12 10:25:45 -08009#include <clk-uclass.h>
10#include <dm.h>
Patrice Chotard03f10a12017-11-15 13:14:51 +010011#include <stm32_rcc.h>
Patrice Chotard22768d52017-11-15 13:14:44 +010012
Vikas Manocha1b51c932016-02-11 15:47:20 -080013#include <asm/io.h>
Vikas Manocha1b51c932016-02-11 15:47:20 -080014#include <asm/arch/stm32.h>
Patrice Chotard22768d52017-11-15 13:14:44 +010015#include <asm/arch/stm32_pwr.h>
Vikas Manocha1b51c932016-02-11 15:47:20 -080016
Patrice Chotard7bdf9712017-07-18 09:29:05 +020017#include <dt-bindings/mfd/stm32f7-rcc.h>
18
Michael Kurzc204fb72017-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 Chotard369d4832017-11-15 13:14:52 +010026#define RCC_CR_PLLSAION BIT(28)
27#define RCC_CR_PLLSAIRDY BIT(29)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090028
Michael Kurzc204fb72017-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 NISHINAGA65bfb9c2016-07-08 01:02:24 +090038
Michael Kurzc204fb72017-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 NISHINAGA65bfb9c2016-07-08 01:02:24 +090057
Patrice Chotard369d4832017-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
Patrice Chotardd5d36552018-01-19 18:02:40 +010062#define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16)
Patrice Chotard369d4832017-11-15 13:14:52 +010063#define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26)
64#define RCC_PLLSAICFGR_PLLSAIR_2 BIT(29)
65
Patrice Chotardef772872018-02-07 10:44:46 +010066#define RCC_DCKCFGRX_TIMPRE BIT(24)
Patrice Chotard369d4832017-11-15 13:14:52 +010067#define RCC_DCKCFGRX_CK48MSEL BIT(27)
68#define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
69#define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
70
Patrice Chotard06fc6482017-11-15 13:14:49 +010071/*
72 * RCC AHB1ENR specific definitions
73 */
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)
77
78/*
79 * RCC APB1ENR specific definitions
80 */
81#define RCC_APB1ENR_TIM2EN BIT(0)
82#define RCC_APB1ENR_PWREN BIT(28)
83
84/*
85 * RCC APB2ENR specific definitions
86 */
87#define RCC_APB2ENR_SYSCFGEN BIT(14)
Patrice Chotarde2d564e2018-01-18 14:10:05 +010088#define RCC_APB2ENR_SAI1EN BIT(22)
Patrice Chotard06fc6482017-11-15 13:14:49 +010089
Patrice Chotardf3a701a2017-12-12 09:49:39 +010090enum periph_clock {
Patrice Chotardf3a701a2017-12-12 09:49:39 +010091 TIMER2_CLOCK_CFG,
Patrice Chotardf3a701a2017-12-12 09:49:39 +010092};
93
Patrice Chotardacd97ca2018-01-18 13:39:30 +010094static const struct stm32_clk_info stm32f4_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +010095 /* 180 MHz */
96 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +010097 .pll_n = 360,
98 .pll_p = 2,
99 .pll_q = 8,
100 .ahb_psc = AHB_PSC_1,
101 .apb1_psc = APB_PSC_4,
102 .apb2_psc = APB_PSC_2,
103 },
104 .has_overdrive = false,
Patrice Chotard369d4832017-11-15 13:14:52 +0100105 .v2 = false,
Patrice Chotard1509d662017-11-15 13:14:47 +0100106};
107
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100108static const struct stm32_clk_info stm32f7_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100109 /* 200 MHz */
110 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100111 .pll_n = 400,
112 .pll_p = 2,
113 .pll_q = 8,
114 .ahb_psc = AHB_PSC_1,
115 .apb1_psc = APB_PSC_4,
116 .apb2_psc = APB_PSC_2,
117 },
118 .has_overdrive = true,
Patrice Chotard369d4832017-11-15 13:14:52 +0100119 .v2 = true,
Patrice Chotard1509d662017-11-15 13:14:47 +0100120};
121
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200122struct stm32_clk {
123 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100124 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100125 struct stm32_clk_info info;
126 unsigned long hse_rate;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900127};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900128
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200129static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900130{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200131 struct stm32_clk *priv = dev_get_priv(dev);
132 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100133 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100134 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
Patrice Chotard369d4832017-11-15 13:14:52 +0100135 u32 pllsaicfgr = 0;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200136
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900137 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200138 setbits_le32(&regs->cr, RCC_CR_HSION);
139 writel(0, &regs->cfgr); /* Reset CFGR */
140 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard369d4832017-11-15 13:14:52 +0100141 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200142 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
143 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
144 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900145
146 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200147 setbits_le32(&regs->cr, RCC_CR_HSEON);
148 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900149 ;
150
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200151 setbits_le32(&regs->cfgr, ((
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100152 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)));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900155
156 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200157 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
158 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100159 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200160 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100161 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200162 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100163 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200164 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100165 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900166
Patrice Chotard369d4832017-11-15 13:14:52 +0100167 /* 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, &regs->pllsaicfgr);
172
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900173 /* Enable the main PLL */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200174 setbits_le32(&regs->cr, RCC_CR_PLLON);
175 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900176 ;
177
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100178 if (priv->info.v2) { /*stm32f7 case */
Patrice Chotard369d4832017-11-15 13:14:52 +0100179 /* select PLLSAI as 48MHz clock source */
180 setbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
181
182 /* select 48MHz as SDMMC1 clock source */
183 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
184
185 /* select 48MHz as SDMMC2 clock source */
186 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
187 } else { /* stm32f4 case */
188 /* select PLLSAI as 48MHz clock source */
189 setbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
190
191 /* select 48MHz as SDMMC1 clock source */
192 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
193 }
194
195 /* Enable the SAI PLL */
196 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
197 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
198 ;
199
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200200 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100201
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100202 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100203 /*
204 * Enable high performance mode
205 * System frequency up to 200 MHz
206 */
207 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
208 /* Infinite wait! */
209 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
210 ;
211 /* Enable the Over-drive switch */
212 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
213 /* Infinite wait! */
214 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
215 ;
216 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900217
218 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200219 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
220 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900221
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200222 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900223 RCC_CFGR_SWS_PLL)
224 ;
Patrice Chotard369d4832017-11-15 13:14:52 +0100225 /* gate the SAI clock, needed for MMC 1&2 clocks */
226 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SAI1EN);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900227
Patrice Chotarde2d564e2018-01-18 14:10:05 +0100228#ifdef CONFIG_ETH_DESIGNWARE
229 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
230 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SYSCFGEN);
231#endif
232
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900233 return 0;
234}
235
Patrice Chotard369d4832017-11-15 13:14:52 +0100236static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv,
237 u32 sysclk)
238{
239 struct stm32_rcc_regs *regs = priv->base;
240 u16 pllq, pllm, pllsain, pllsaip;
241 bool pllsai;
242
243 pllq = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
244 >> RCC_PLLCFGR_PLLQ_SHIFT;
245
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100246 if (priv->info.v2) /*stm32f7 case */
Patrice Chotard369d4832017-11-15 13:14:52 +0100247 pllsai = readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
248 else
249 pllsai = readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
250
251 if (pllsai) {
252 /* PLL48CLK is selected from PLLSAI, get PLLSAI value */
253 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
254 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK)
255 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
256 pllsaip = ((((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK)
257 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100258 return ((priv->hse_rate / pllm) * pllsain) / pllsaip;
Patrice Chotard369d4832017-11-15 13:14:52 +0100259 }
260 /* PLL48CLK is selected from PLLQ */
261 return sysclk / pllq;
262}
263
Patrice Chotardef772872018-02-07 10:44:46 +0100264static bool stm32_get_timpre(struct stm32_clk *priv)
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200265{
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200266 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardef772872018-02-07 10:44:46 +0100267 u32 val;
268
269 if (priv->info.v2) /*stm32f7 case */
270 val = readl(&regs->dckcfgr2);
271 else
272 val = readl(&regs->dckcfgr);
273 /* get timer prescaler */
274 return !!(val & RCC_DCKCFGRX_TIMPRE);
275}
276
277static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk)
278{
279 u8 shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200280 /* 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
283 };
Patrice Chotardef772872018-02-07 10:44:46 +0100284
285 shift = ahb_psc_table[(
286 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
287 >> RCC_CFGR_HPRE_SHIFT)];
288
289 return sysclk >> shift;
290};
291
292static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb)
293{
294 /* Prescaler table lookups for clock computation */
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200295 u8 apb_psc_table[8] = {
296 0, 0, 0, 0, 1, 2, 3, 4
297 };
298
Patrice Chotardef772872018-02-07 10:44:46 +0100299 if (apb == APB1)
300 return apb_psc_table[(
301 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
302 >> RCC_CFGR_PPRE1_SHIFT)];
303 else /* APB2 */
304 return apb_psc_table[(
305 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
306 >> RCC_CFGR_PPRE2_SHIFT)];
307};
308
309static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk,
310 enum apb apb)
311{
312 struct stm32_rcc_regs *regs = priv->base;
313 u8 shift = stm32_get_apb_shift(regs, apb);
314
315 if (stm32_get_timpre(priv))
316 /*
317 * if APB prescaler is configured to a
318 * division factor of 1, 2 or 4
319 */
320 switch (shift) {
321 case 0:
322 case 1:
323 case 2:
324 return stm32_get_hclk_rate(regs, sysclk);
325 default:
326 return (sysclk >> shift) * 4;
327 }
328 else
329 /*
330 * if APB prescaler is configured to a
331 * division factor of 1
332 */
333 if (shift == 0)
334 return sysclk;
335 else
336 return (sysclk >> shift) * 2;
337};
338
339static ulong stm32_clk_get_rate(struct clk *clk)
340{
341 struct stm32_clk *priv = dev_get_priv(clk->dev);
342 struct stm32_rcc_regs *regs = priv->base;
343 u32 sysclk = 0;
344 u16 pllm, plln, pllp;
345
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200346 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
347 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200348 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
349 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
350 >> RCC_PLLCFGR_PLLN_SHIFT);
351 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
352 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100353 sysclk = ((priv->hse_rate / pllm) * plln) / pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200354 } else {
355 return -EINVAL;
356 }
357
358 switch (clk->id) {
359 /*
360 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
361 * AHB1, AHB2 and AHB3
362 */
363 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
Patrice Chotardef772872018-02-07 10:44:46 +0100364 return stm32_get_hclk_rate(regs, sysclk);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200365 /* APB1 CLOCK */
366 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
Patrice Chotardef772872018-02-07 10:44:46 +0100367 /* For timer clock, an additionnal prescaler is used*/
368 switch (clk->id) {
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);
379 }
380 return (sysclk >> stm32_get_apb_shift(regs, APB1));
381
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200382 /* APB2 CLOCK */
383 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
Patrice Chotard369d4832017-11-15 13:14:52 +0100384 /*
385 * particular case for SDMMC1 and SDMMC2 :
386 * 48Mhz source clock can be from main PLL or from
387 * SAI PLL
388 */
389 switch (clk->id) {
390 case STM32F7_APB2_CLOCK(SDMMC1):
391 if (readl(&regs->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL)
392 /* System clock is selected as SDMMC1 clock */
393 return sysclk;
394 else
395 return stm32_clk_pll48clk_rate(priv, sysclk);
396 break;
397 case STM32F7_APB2_CLOCK(SDMMC2):
398 if (readl(&regs->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL)
399 /* System clock is selected as SDMMC2 clock */
400 return sysclk;
401 else
402 return stm32_clk_pll48clk_rate(priv, sysclk);
403 break;
Patrice Chotardef772872018-02-07 10:44:46 +0100404
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);
412 break;
Patrice Chotard369d4832017-11-15 13:14:52 +0100413 }
Patrice Chotardef772872018-02-07 10:44:46 +0100414 return (sysclk >> stm32_get_apb_shift(regs, APB2));
Patrice Chotard369d4832017-11-15 13:14:52 +0100415
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200416 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900417 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200418 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200419 }
420}
421
Patrice Chotard24e85782018-01-29 18:14:14 +0100422static ulong stm32_set_rate(struct clk *clk, ulong rate)
423{
424 return 0;
425}
426
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800427static int stm32_clk_enable(struct clk *clk)
428{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200429 struct stm32_clk *priv = dev_get_priv(clk->dev);
430 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800431 u32 offset = clk->id / 32;
432 u32 bit_index = clk->id % 32;
433
434 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
435 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200436 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800437
438 return 0;
439}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900440
Vikas Manocha1b51c932016-02-11 15:47:20 -0800441void clock_setup(int peripheral)
442{
443 switch (peripheral) {
Michael Kurz04bb8db2017-01-22 16:04:26 +0100444 case TIMER2_CLOCK_CFG:
445 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
446 break;
Vikas Manocha1b51c932016-02-11 15:47:20 -0800447 default:
448 break;
449 }
450}
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800451
452static int stm32_clk_probe(struct udevice *dev)
453{
Patrice Chotard22768d52017-11-15 13:14:44 +0100454 struct ofnode_phandle_args args;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100455 struct udevice *fixed_clock_dev = NULL;
456 struct clk clk;
Patrice Chotard22768d52017-11-15 13:14:44 +0100457 int err;
458
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100459 debug("%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200460
461 struct stm32_clk *priv = dev_get_priv(dev);
462 fdt_addr_t addr;
463
Patrice Chotard1509d662017-11-15 13:14:47 +0100464 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200465 if (addr == FDT_ADDR_T_NONE)
466 return -EINVAL;
467
468 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard03f10a12017-11-15 13:14:51 +0100469
470 switch (dev_get_driver_data(dev)) {
471 case STM32F4:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100472 memcpy(&priv->info, &stm32f4_clk_info,
473 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100474 break;
475 case STM32F7:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100476 memcpy(&priv->info, &stm32f7_clk_info,
477 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100478 break;
479 default:
480 return -EINVAL;
481 }
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200482
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100483 /* retrieve HSE frequency (external oscillator) */
484 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
485 &fixed_clock_dev);
486
487 if (err) {
488 pr_err("Can't find fixed clock (%d)", err);
489 return err;
490 }
491
492 err = clk_request(fixed_clock_dev, &clk);
493 if (err) {
494 pr_err("Can't request %s clk (%d)", fixed_clock_dev->name,
495 err);
496 return err;
497 }
498
499 /*
500 * set pllm factor accordingly to the external oscillator
501 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
502 * freq at 1MHz
503 * if input PLL frequency is 25Mhz, divide it by 25
504 */
505 clk.id = 0;
506 priv->hse_rate = clk_get_rate(&clk);
507
508 if (priv->hse_rate < 1000000) {
509 pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__,
510 priv->hse_rate);
511 return -EINVAL;
512 }
513
514 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
515
516 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100517 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
518 &args);
519 if (err) {
520 debug("%s: can't find syscon device (%d)\n", __func__,
521 err);
522 return err;
523 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100524
Patrice Chotard1509d662017-11-15 13:14:47 +0100525 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
526 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100527
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200528 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800529
530 return 0;
531}
532
Simon Glassb7ae2772017-05-18 20:09:40 -0600533static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800534{
535 debug("%s(clk=%p)\n", __func__, clk);
536
537 if (args->args_count != 2) {
538 debug("Invaild args_count: %d\n", args->args_count);
539 return -EINVAL;
540 }
541
542 if (args->args_count)
543 clk->id = args->args[1];
544 else
545 clk->id = 0;
546
547 return 0;
548}
549
550static struct clk_ops stm32_clk_ops = {
551 .of_xlate = stm32_clk_of_xlate,
552 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200553 .get_rate = stm32_clk_get_rate,
Patrice Chotard24e85782018-01-29 18:14:14 +0100554 .set_rate = stm32_set_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800555};
556
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100557U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard03f10a12017-11-15 13:14:51 +0100558 .name = "stm32fx_rcc_clock",
Patrice Chotardb323de52017-09-21 10:08:09 +0200559 .id = UCLASS_CLK,
Patrice Chotardb323de52017-09-21 10:08:09 +0200560 .ops = &stm32_clk_ops,
561 .probe = stm32_clk_probe,
562 .priv_auto_alloc_size = sizeof(struct stm32_clk),
563 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800564};