blob: 41d8b5e5c88ae082e2574f9fef274dfe5a274b88 [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 Chotardacd97ca2018-01-18 13:39:30 +010090static const struct stm32_clk_info stm32f4_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +010091 /* 180 MHz */
92 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +010093 .pll_n = 360,
94 .pll_p = 2,
95 .pll_q = 8,
96 .ahb_psc = AHB_PSC_1,
97 .apb1_psc = APB_PSC_4,
98 .apb2_psc = APB_PSC_2,
99 },
100 .has_overdrive = false,
Patrice Chotard369d4832017-11-15 13:14:52 +0100101 .v2 = false,
Patrice Chotard1509d662017-11-15 13:14:47 +0100102};
103
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100104static const struct stm32_clk_info stm32f7_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100105 /* 200 MHz */
106 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100107 .pll_n = 400,
108 .pll_p = 2,
109 .pll_q = 8,
110 .ahb_psc = AHB_PSC_1,
111 .apb1_psc = APB_PSC_4,
112 .apb2_psc = APB_PSC_2,
113 },
114 .has_overdrive = true,
Patrice Chotard369d4832017-11-15 13:14:52 +0100115 .v2 = true,
Patrice Chotard1509d662017-11-15 13:14:47 +0100116};
117
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200118struct stm32_clk {
119 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100120 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100121 struct stm32_clk_info info;
122 unsigned long hse_rate;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900123};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900124
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200125static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900126{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200127 struct stm32_clk *priv = dev_get_priv(dev);
128 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100129 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100130 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
Patrice Chotard369d4832017-11-15 13:14:52 +0100131 u32 pllsaicfgr = 0;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200132
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900133 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200134 setbits_le32(&regs->cr, RCC_CR_HSION);
135 writel(0, &regs->cfgr); /* Reset CFGR */
136 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard369d4832017-11-15 13:14:52 +0100137 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200138 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
139 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
140 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900141
142 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200143 setbits_le32(&regs->cr, RCC_CR_HSEON);
144 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900145 ;
146
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200147 setbits_le32(&regs->cfgr, ((
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100148 sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT)
149 | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT)
150 | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900151
152 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200153 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
154 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100155 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200156 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100157 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200158 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100159 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200160 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100161 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900162
Patrice Chotard369d4832017-11-15 13:14:52 +0100163 /* Configure the SAI PLL to get a 48 MHz source */
164 pllsaicfgr = RCC_PLLSAICFGR_PLLSAIR_2 | RCC_PLLSAICFGR_PLLSAIQ_4 |
165 RCC_PLLSAICFGR_PLLSAIP_4;
166 pllsaicfgr |= 192 << RCC_PLLSAICFGR_PLLSAIN_SHIFT;
167 writel(pllsaicfgr, &regs->pllsaicfgr);
168
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900169 /* Enable the main PLL */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200170 setbits_le32(&regs->cr, RCC_CR_PLLON);
171 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900172 ;
173
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100174 if (priv->info.v2) { /*stm32f7 case */
Patrice Chotard369d4832017-11-15 13:14:52 +0100175 /* select PLLSAI as 48MHz clock source */
176 setbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
177
178 /* select 48MHz as SDMMC1 clock source */
179 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
180
181 /* select 48MHz as SDMMC2 clock source */
182 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
183 } else { /* stm32f4 case */
184 /* select PLLSAI as 48MHz clock source */
185 setbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
186
187 /* select 48MHz as SDMMC1 clock source */
188 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
189 }
190
191 /* Enable the SAI PLL */
192 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
193 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
194 ;
195
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200196 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100197
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100198 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100199 /*
200 * Enable high performance mode
201 * System frequency up to 200 MHz
202 */
203 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
204 /* Infinite wait! */
205 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
206 ;
207 /* Enable the Over-drive switch */
208 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
209 /* Infinite wait! */
210 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
211 ;
212 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900213
214 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200215 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
216 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900217
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200218 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900219 RCC_CFGR_SWS_PLL)
220 ;
Patrice Chotard369d4832017-11-15 13:14:52 +0100221 /* gate the SAI clock, needed for MMC 1&2 clocks */
222 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SAI1EN);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900223
Patrice Chotarde2d564e2018-01-18 14:10:05 +0100224#ifdef CONFIG_ETH_DESIGNWARE
225 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
226 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SYSCFGEN);
227#endif
228
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900229 return 0;
230}
231
Patrice Chotard369d4832017-11-15 13:14:52 +0100232static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv,
Patrice Chotard81d77652018-02-08 17:20:45 +0100233 u32 vco)
Patrice Chotard369d4832017-11-15 13:14:52 +0100234{
235 struct stm32_rcc_regs *regs = priv->base;
236 u16 pllq, pllm, pllsain, pllsaip;
237 bool pllsai;
238
239 pllq = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
240 >> RCC_PLLCFGR_PLLQ_SHIFT;
241
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100242 if (priv->info.v2) /*stm32f7 case */
Patrice Chotard369d4832017-11-15 13:14:52 +0100243 pllsai = readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
244 else
245 pllsai = readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
246
247 if (pllsai) {
248 /* PLL48CLK is selected from PLLSAI, get PLLSAI value */
249 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
250 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK)
251 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
252 pllsaip = ((((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK)
253 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100254 return ((priv->hse_rate / pllm) * pllsain) / pllsaip;
Patrice Chotard369d4832017-11-15 13:14:52 +0100255 }
256 /* PLL48CLK is selected from PLLQ */
Patrice Chotard81d77652018-02-08 17:20:45 +0100257 return vco / pllq;
Patrice Chotard369d4832017-11-15 13:14:52 +0100258}
259
Patrice Chotardef772872018-02-07 10:44:46 +0100260static bool stm32_get_timpre(struct stm32_clk *priv)
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200261{
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200262 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardef772872018-02-07 10:44:46 +0100263 u32 val;
264
265 if (priv->info.v2) /*stm32f7 case */
266 val = readl(&regs->dckcfgr2);
267 else
268 val = readl(&regs->dckcfgr);
269 /* get timer prescaler */
270 return !!(val & RCC_DCKCFGRX_TIMPRE);
271}
272
273static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk)
274{
275 u8 shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200276 /* Prescaler table lookups for clock computation */
277 u8 ahb_psc_table[16] = {
278 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
279 };
Patrice Chotardef772872018-02-07 10:44:46 +0100280
281 shift = ahb_psc_table[(
282 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
283 >> RCC_CFGR_HPRE_SHIFT)];
284
285 return sysclk >> shift;
286};
287
288static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb)
289{
290 /* Prescaler table lookups for clock computation */
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200291 u8 apb_psc_table[8] = {
292 0, 0, 0, 0, 1, 2, 3, 4
293 };
294
Patrice Chotardef772872018-02-07 10:44:46 +0100295 if (apb == APB1)
296 return apb_psc_table[(
297 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
298 >> RCC_CFGR_PPRE1_SHIFT)];
299 else /* APB2 */
300 return apb_psc_table[(
301 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
302 >> RCC_CFGR_PPRE2_SHIFT)];
303};
304
305static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk,
306 enum apb apb)
307{
308 struct stm32_rcc_regs *regs = priv->base;
309 u8 shift = stm32_get_apb_shift(regs, apb);
310
311 if (stm32_get_timpre(priv))
312 /*
313 * if APB prescaler is configured to a
314 * division factor of 1, 2 or 4
315 */
316 switch (shift) {
317 case 0:
318 case 1:
319 case 2:
320 return stm32_get_hclk_rate(regs, sysclk);
321 default:
322 return (sysclk >> shift) * 4;
323 }
324 else
325 /*
326 * if APB prescaler is configured to a
327 * division factor of 1
328 */
329 if (shift == 0)
330 return sysclk;
331 else
332 return (sysclk >> shift) * 2;
333};
334
335static ulong stm32_clk_get_rate(struct clk *clk)
336{
337 struct stm32_clk *priv = dev_get_priv(clk->dev);
338 struct stm32_rcc_regs *regs = priv->base;
339 u32 sysclk = 0;
Patrice Chotard81d77652018-02-08 17:20:45 +0100340 u32 vco;
Patrice Chotardef772872018-02-07 10:44:46 +0100341 u16 pllm, plln, pllp;
342
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200343 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
344 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200345 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
346 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
347 >> RCC_PLLCFGR_PLLN_SHIFT);
348 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
349 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
Patrice Chotard81d77652018-02-08 17:20:45 +0100350 vco = (priv->hse_rate / pllm) * plln;
351 sysclk = vco / pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200352 } else {
353 return -EINVAL;
354 }
355
356 switch (clk->id) {
357 /*
358 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
359 * AHB1, AHB2 and AHB3
360 */
361 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
Patrice Chotardef772872018-02-07 10:44:46 +0100362 return stm32_get_hclk_rate(regs, sysclk);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200363 /* APB1 CLOCK */
364 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
Patrice Chotardef772872018-02-07 10:44:46 +0100365 /* For timer clock, an additionnal prescaler is used*/
366 switch (clk->id) {
367 case STM32F7_APB1_CLOCK(TIM2):
368 case STM32F7_APB1_CLOCK(TIM3):
369 case STM32F7_APB1_CLOCK(TIM4):
370 case STM32F7_APB1_CLOCK(TIM5):
371 case STM32F7_APB1_CLOCK(TIM6):
372 case STM32F7_APB1_CLOCK(TIM7):
373 case STM32F7_APB1_CLOCK(TIM12):
374 case STM32F7_APB1_CLOCK(TIM13):
375 case STM32F7_APB1_CLOCK(TIM14):
376 return stm32_get_timer_rate(priv, sysclk, APB1);
377 }
378 return (sysclk >> stm32_get_apb_shift(regs, APB1));
379
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200380 /* APB2 CLOCK */
381 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
Patrice Chotard369d4832017-11-15 13:14:52 +0100382 /*
383 * particular case for SDMMC1 and SDMMC2 :
384 * 48Mhz source clock can be from main PLL or from
385 * SAI PLL
386 */
387 switch (clk->id) {
388 case STM32F7_APB2_CLOCK(SDMMC1):
389 if (readl(&regs->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL)
390 /* System clock is selected as SDMMC1 clock */
391 return sysclk;
392 else
Patrice Chotard81d77652018-02-08 17:20:45 +0100393 return stm32_clk_pll48clk_rate(priv, vco);
Patrice Chotard369d4832017-11-15 13:14:52 +0100394 break;
395 case STM32F7_APB2_CLOCK(SDMMC2):
396 if (readl(&regs->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL)
397 /* System clock is selected as SDMMC2 clock */
398 return sysclk;
399 else
Patrice Chotard81d77652018-02-08 17:20:45 +0100400 return stm32_clk_pll48clk_rate(priv, vco);
Patrice Chotard369d4832017-11-15 13:14:52 +0100401 break;
Patrice Chotardef772872018-02-07 10:44:46 +0100402
403 /* For timer clock, an additionnal prescaler is used*/
404 case STM32F7_APB2_CLOCK(TIM1):
405 case STM32F7_APB2_CLOCK(TIM8):
406 case STM32F7_APB2_CLOCK(TIM9):
407 case STM32F7_APB2_CLOCK(TIM10):
408 case STM32F7_APB2_CLOCK(TIM11):
409 return stm32_get_timer_rate(priv, sysclk, APB2);
410 break;
Patrice Chotard369d4832017-11-15 13:14:52 +0100411 }
Patrice Chotardef772872018-02-07 10:44:46 +0100412 return (sysclk >> stm32_get_apb_shift(regs, APB2));
Patrice Chotard369d4832017-11-15 13:14:52 +0100413
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200414 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900415 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200416 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200417 }
418}
419
Patrice Chotard24e85782018-01-29 18:14:14 +0100420static ulong stm32_set_rate(struct clk *clk, ulong rate)
421{
422 return 0;
423}
424
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800425static int stm32_clk_enable(struct clk *clk)
426{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200427 struct stm32_clk *priv = dev_get_priv(clk->dev);
428 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800429 u32 offset = clk->id / 32;
430 u32 bit_index = clk->id % 32;
431
432 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
433 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200434 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800435
436 return 0;
437}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900438
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800439static int stm32_clk_probe(struct udevice *dev)
440{
Patrice Chotard22768d52017-11-15 13:14:44 +0100441 struct ofnode_phandle_args args;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100442 struct udevice *fixed_clock_dev = NULL;
443 struct clk clk;
Patrice Chotard22768d52017-11-15 13:14:44 +0100444 int err;
445
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100446 debug("%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200447
448 struct stm32_clk *priv = dev_get_priv(dev);
449 fdt_addr_t addr;
450
Patrice Chotard1509d662017-11-15 13:14:47 +0100451 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200452 if (addr == FDT_ADDR_T_NONE)
453 return -EINVAL;
454
455 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard03f10a12017-11-15 13:14:51 +0100456
457 switch (dev_get_driver_data(dev)) {
458 case STM32F4:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100459 memcpy(&priv->info, &stm32f4_clk_info,
460 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100461 break;
462 case STM32F7:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100463 memcpy(&priv->info, &stm32f7_clk_info,
464 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100465 break;
466 default:
467 return -EINVAL;
468 }
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200469
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100470 /* retrieve HSE frequency (external oscillator) */
471 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
472 &fixed_clock_dev);
473
474 if (err) {
475 pr_err("Can't find fixed clock (%d)", err);
476 return err;
477 }
478
479 err = clk_request(fixed_clock_dev, &clk);
480 if (err) {
481 pr_err("Can't request %s clk (%d)", fixed_clock_dev->name,
482 err);
483 return err;
484 }
485
486 /*
487 * set pllm factor accordingly to the external oscillator
488 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
489 * freq at 1MHz
490 * if input PLL frequency is 25Mhz, divide it by 25
491 */
492 clk.id = 0;
493 priv->hse_rate = clk_get_rate(&clk);
494
495 if (priv->hse_rate < 1000000) {
496 pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__,
497 priv->hse_rate);
498 return -EINVAL;
499 }
500
501 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
502
503 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100504 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
505 &args);
506 if (err) {
507 debug("%s: can't find syscon device (%d)\n", __func__,
508 err);
509 return err;
510 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100511
Patrice Chotard1509d662017-11-15 13:14:47 +0100512 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
513 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100514
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200515 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800516
517 return 0;
518}
519
Simon Glassb7ae2772017-05-18 20:09:40 -0600520static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800521{
522 debug("%s(clk=%p)\n", __func__, clk);
523
524 if (args->args_count != 2) {
525 debug("Invaild args_count: %d\n", args->args_count);
526 return -EINVAL;
527 }
528
529 if (args->args_count)
530 clk->id = args->args[1];
531 else
532 clk->id = 0;
533
534 return 0;
535}
536
537static struct clk_ops stm32_clk_ops = {
538 .of_xlate = stm32_clk_of_xlate,
539 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200540 .get_rate = stm32_clk_get_rate,
Patrice Chotard24e85782018-01-29 18:14:14 +0100541 .set_rate = stm32_set_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800542};
543
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100544U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard03f10a12017-11-15 13:14:51 +0100545 .name = "stm32fx_rcc_clock",
Patrice Chotardb323de52017-09-21 10:08:09 +0200546 .id = UCLASS_CLK,
Patrice Chotardb323de52017-09-21 10:08:09 +0200547 .ops = &stm32_clk_ops,
548 .probe = stm32_clk_probe,
549 .priv_auto_alloc_size = sizeof(struct stm32_clk),
550 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800551};