blob: 926b249ff3b7e7097d14a61e9150f185f0d8b0be [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
66#define RCC_DCKCFGRX_CK48MSEL BIT(27)
67#define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
68#define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
69
Patrice Chotard06fc6482017-11-15 13:14:49 +010070/*
71 * RCC AHB1ENR specific definitions
72 */
73#define RCC_AHB1ENR_ETHMAC_EN BIT(25)
74#define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
75#define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
76
77/*
78 * RCC APB1ENR specific definitions
79 */
80#define RCC_APB1ENR_TIM2EN BIT(0)
81#define RCC_APB1ENR_PWREN BIT(28)
82
83/*
84 * RCC APB2ENR specific definitions
85 */
86#define RCC_APB2ENR_SYSCFGEN BIT(14)
Patrice Chotarde2d564e2018-01-18 14:10:05 +010087#define RCC_APB2ENR_SAI1EN BIT(22)
Patrice Chotard06fc6482017-11-15 13:14:49 +010088
Patrice Chotardf3a701a2017-12-12 09:49:39 +010089enum periph_clock {
Patrice Chotardf3a701a2017-12-12 09:49:39 +010090 TIMER2_CLOCK_CFG,
Patrice Chotardf3a701a2017-12-12 09:49:39 +010091};
92
Patrice Chotardacd97ca2018-01-18 13:39:30 +010093static const struct stm32_clk_info stm32f4_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +010094 /* 180 MHz */
95 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +010096 .pll_n = 360,
97 .pll_p = 2,
98 .pll_q = 8,
99 .ahb_psc = AHB_PSC_1,
100 .apb1_psc = APB_PSC_4,
101 .apb2_psc = APB_PSC_2,
102 },
103 .has_overdrive = false,
Patrice Chotard369d4832017-11-15 13:14:52 +0100104 .v2 = false,
Patrice Chotard1509d662017-11-15 13:14:47 +0100105};
106
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100107static const struct stm32_clk_info stm32f7_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100108 /* 200 MHz */
109 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100110 .pll_n = 400,
111 .pll_p = 2,
112 .pll_q = 8,
113 .ahb_psc = AHB_PSC_1,
114 .apb1_psc = APB_PSC_4,
115 .apb2_psc = APB_PSC_2,
116 },
117 .has_overdrive = true,
Patrice Chotard369d4832017-11-15 13:14:52 +0100118 .v2 = true,
Patrice Chotard1509d662017-11-15 13:14:47 +0100119};
120
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200121struct stm32_clk {
122 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100123 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100124 struct stm32_clk_info info;
125 unsigned long hse_rate;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900126};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900127
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200128static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900129{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200130 struct stm32_clk *priv = dev_get_priv(dev);
131 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100132 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100133 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
Patrice Chotard369d4832017-11-15 13:14:52 +0100134 u32 pllsaicfgr = 0;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200135
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900136 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200137 setbits_le32(&regs->cr, RCC_CR_HSION);
138 writel(0, &regs->cfgr); /* Reset CFGR */
139 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard369d4832017-11-15 13:14:52 +0100140 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200141 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
142 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
143 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900144
145 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200146 setbits_le32(&regs->cr, RCC_CR_HSEON);
147 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900148 ;
149
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200150 setbits_le32(&regs->cfgr, ((
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100151 sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT)
152 | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT)
153 | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900154
155 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200156 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
157 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100158 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200159 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100160 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200161 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100162 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200163 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100164 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900165
Patrice Chotard369d4832017-11-15 13:14:52 +0100166 /* Configure the SAI PLL to get a 48 MHz source */
167 pllsaicfgr = RCC_PLLSAICFGR_PLLSAIR_2 | RCC_PLLSAICFGR_PLLSAIQ_4 |
168 RCC_PLLSAICFGR_PLLSAIP_4;
169 pllsaicfgr |= 192 << RCC_PLLSAICFGR_PLLSAIN_SHIFT;
170 writel(pllsaicfgr, &regs->pllsaicfgr);
171
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900172 /* Enable the main PLL */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200173 setbits_le32(&regs->cr, RCC_CR_PLLON);
174 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900175 ;
176
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100177 if (priv->info.v2) { /*stm32f7 case */
Patrice Chotard369d4832017-11-15 13:14:52 +0100178 /* select PLLSAI as 48MHz clock source */
179 setbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
180
181 /* select 48MHz as SDMMC1 clock source */
182 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
183
184 /* select 48MHz as SDMMC2 clock source */
185 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
186 } else { /* stm32f4 case */
187 /* select PLLSAI as 48MHz clock source */
188 setbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
189
190 /* select 48MHz as SDMMC1 clock source */
191 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
192 }
193
194 /* Enable the SAI PLL */
195 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
196 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
197 ;
198
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200199 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100200
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100201 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100202 /*
203 * Enable high performance mode
204 * System frequency up to 200 MHz
205 */
206 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
207 /* Infinite wait! */
208 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
209 ;
210 /* Enable the Over-drive switch */
211 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
212 /* Infinite wait! */
213 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
214 ;
215 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900216
217 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200218 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
219 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900220
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200221 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900222 RCC_CFGR_SWS_PLL)
223 ;
Patrice Chotard369d4832017-11-15 13:14:52 +0100224 /* gate the SAI clock, needed for MMC 1&2 clocks */
225 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SAI1EN);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900226
Patrice Chotarde2d564e2018-01-18 14:10:05 +0100227#ifdef CONFIG_ETH_DESIGNWARE
228 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
229 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SYSCFGEN);
230#endif
231
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900232 return 0;
233}
234
Patrice Chotard369d4832017-11-15 13:14:52 +0100235static unsigned long stm32_clk_pll48clk_rate(struct stm32_clk *priv,
236 u32 sysclk)
237{
238 struct stm32_rcc_regs *regs = priv->base;
239 u16 pllq, pllm, pllsain, pllsaip;
240 bool pllsai;
241
242 pllq = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
243 >> RCC_PLLCFGR_PLLQ_SHIFT;
244
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100245 if (priv->info.v2) /*stm32f7 case */
Patrice Chotard369d4832017-11-15 13:14:52 +0100246 pllsai = readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
247 else
248 pllsai = readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
249
250 if (pllsai) {
251 /* PLL48CLK is selected from PLLSAI, get PLLSAI value */
252 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
253 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIN_MASK)
254 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
255 pllsaip = ((((readl(&regs->pllsaicfgr) & RCC_PLLCFGR_PLLSAIP_MASK)
256 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100257 return ((priv->hse_rate / pllm) * pllsain) / pllsaip;
Patrice Chotard369d4832017-11-15 13:14:52 +0100258 }
259 /* PLL48CLK is selected from PLLQ */
260 return sysclk / pllq;
261}
262
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200263static unsigned long stm32_clk_get_rate(struct clk *clk)
264{
265 struct stm32_clk *priv = dev_get_priv(clk->dev);
266 struct stm32_rcc_regs *regs = priv->base;
267 u32 sysclk = 0;
268 u32 shift = 0;
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100269 u16 pllm, plln, pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200270 /* Prescaler table lookups for clock computation */
271 u8 ahb_psc_table[16] = {
272 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
273 };
274 u8 apb_psc_table[8] = {
275 0, 0, 0, 0, 1, 2, 3, 4
276 };
277
278 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
279 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200280 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
281 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
282 >> RCC_PLLCFGR_PLLN_SHIFT);
283 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
284 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100285 sysclk = ((priv->hse_rate / pllm) * plln) / pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200286 } else {
287 return -EINVAL;
288 }
289
290 switch (clk->id) {
291 /*
292 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
293 * AHB1, AHB2 and AHB3
294 */
295 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
296 shift = ahb_psc_table[(
297 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
298 >> RCC_CFGR_HPRE_SHIFT)];
299 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200300 /* APB1 CLOCK */
301 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
302 shift = apb_psc_table[(
303 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
304 >> RCC_CFGR_PPRE1_SHIFT)];
305 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200306 /* APB2 CLOCK */
307 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
Patrice Chotard369d4832017-11-15 13:14:52 +0100308 /*
309 * particular case for SDMMC1 and SDMMC2 :
310 * 48Mhz source clock can be from main PLL or from
311 * SAI PLL
312 */
313 switch (clk->id) {
314 case STM32F7_APB2_CLOCK(SDMMC1):
315 if (readl(&regs->dckcfgr2) & RCC_DCKCFGRX_SDMMC1SEL)
316 /* System clock is selected as SDMMC1 clock */
317 return sysclk;
318 else
319 return stm32_clk_pll48clk_rate(priv, sysclk);
320 break;
321 case STM32F7_APB2_CLOCK(SDMMC2):
322 if (readl(&regs->dckcfgr2) & RCC_DCKCFGR2_SDMMC2SEL)
323 /* System clock is selected as SDMMC2 clock */
324 return sysclk;
325 else
326 return stm32_clk_pll48clk_rate(priv, sysclk);
327 break;
328 }
329
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200330 shift = apb_psc_table[(
331 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
332 >> RCC_CFGR_PPRE2_SHIFT)];
333 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200334 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900335 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200336 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200337 }
338}
339
Patrice Chotard24e85782018-01-29 18:14:14 +0100340static ulong stm32_set_rate(struct clk *clk, ulong rate)
341{
342 return 0;
343}
344
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800345static int stm32_clk_enable(struct clk *clk)
346{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200347 struct stm32_clk *priv = dev_get_priv(clk->dev);
348 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800349 u32 offset = clk->id / 32;
350 u32 bit_index = clk->id % 32;
351
352 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
353 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200354 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800355
356 return 0;
357}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900358
Vikas Manocha1b51c932016-02-11 15:47:20 -0800359void clock_setup(int peripheral)
360{
361 switch (peripheral) {
Michael Kurz04bb8db2017-01-22 16:04:26 +0100362 case TIMER2_CLOCK_CFG:
363 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
364 break;
Vikas Manocha1b51c932016-02-11 15:47:20 -0800365 default:
366 break;
367 }
368}
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800369
370static int stm32_clk_probe(struct udevice *dev)
371{
Patrice Chotard22768d52017-11-15 13:14:44 +0100372 struct ofnode_phandle_args args;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100373 struct udevice *fixed_clock_dev = NULL;
374 struct clk clk;
Patrice Chotard22768d52017-11-15 13:14:44 +0100375 int err;
376
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100377 debug("%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200378
379 struct stm32_clk *priv = dev_get_priv(dev);
380 fdt_addr_t addr;
381
Patrice Chotard1509d662017-11-15 13:14:47 +0100382 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-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 Chotard03f10a12017-11-15 13:14:51 +0100387
388 switch (dev_get_driver_data(dev)) {
389 case STM32F4:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100390 memcpy(&priv->info, &stm32f4_clk_info,
391 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100392 break;
393 case STM32F7:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100394 memcpy(&priv->info, &stm32f7_clk_info,
395 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100396 break;
397 default:
398 return -EINVAL;
399 }
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200400
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100401 /* retrieve HSE frequency (external oscillator) */
402 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
403 &fixed_clock_dev);
404
405 if (err) {
406 pr_err("Can't find fixed clock (%d)", err);
407 return err;
408 }
409
410 err = clk_request(fixed_clock_dev, &clk);
411 if (err) {
412 pr_err("Can't request %s clk (%d)", fixed_clock_dev->name,
413 err);
414 return err;
415 }
416
417 /*
418 * set pllm factor accordingly to the external oscillator
419 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
420 * freq at 1MHz
421 * if input PLL frequency is 25Mhz, divide it by 25
422 */
423 clk.id = 0;
424 priv->hse_rate = clk_get_rate(&clk);
425
426 if (priv->hse_rate < 1000000) {
427 pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__,
428 priv->hse_rate);
429 return -EINVAL;
430 }
431
432 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
433
434 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100435 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
436 &args);
437 if (err) {
438 debug("%s: can't find syscon device (%d)\n", __func__,
439 err);
440 return err;
441 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100442
Patrice Chotard1509d662017-11-15 13:14:47 +0100443 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
444 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100445
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200446 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800447
448 return 0;
449}
450
Simon Glassb7ae2772017-05-18 20:09:40 -0600451static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800452{
453 debug("%s(clk=%p)\n", __func__, clk);
454
455 if (args->args_count != 2) {
456 debug("Invaild args_count: %d\n", args->args_count);
457 return -EINVAL;
458 }
459
460 if (args->args_count)
461 clk->id = args->args[1];
462 else
463 clk->id = 0;
464
465 return 0;
466}
467
468static struct clk_ops stm32_clk_ops = {
469 .of_xlate = stm32_clk_of_xlate,
470 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200471 .get_rate = stm32_clk_get_rate,
Patrice Chotard24e85782018-01-29 18:14:14 +0100472 .set_rate = stm32_set_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800473};
474
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100475U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard03f10a12017-11-15 13:14:51 +0100476 .name = "stm32fx_rcc_clock",
Patrice Chotardb323de52017-09-21 10:08:09 +0200477 .id = UCLASS_CLK,
Patrice Chotardb323de52017-09-21 10:08:09 +0200478 .ops = &stm32_clk_ops,
479 .probe = stm32_clk_probe,
480 .priv_auto_alloc_size = sizeof(struct stm32_clk),
481 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800482};