blob: a11d6dc242ba5175559834af65855bbdae8523ad [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 Chotard9490aca2018-02-08 17:20:46 +010058#define RCC_PLLSAICFGR_PLLSAIN_MASK GENMASK(14, 6)
59#define RCC_PLLSAICFGR_PLLSAIP_MASK GENMASK(17, 16)
Patrice Chotard8aca2d12018-02-08 17:20:48 +010060#define RCC_PLLSAICFGR_PLLSAIQ_MASK GENMASK(27, 24)
61#define RCC_PLLSAICFGR_PLLSAIR_MASK GENMASK(30, 28)
Patrice Chotard369d4832017-11-15 13:14:52 +010062#define RCC_PLLSAICFGR_PLLSAIN_SHIFT 6
63#define RCC_PLLSAICFGR_PLLSAIP_SHIFT 16
Patrice Chotard8aca2d12018-02-08 17:20:48 +010064#define RCC_PLLSAICFGR_PLLSAIQ_SHIFT 24
65#define RCC_PLLSAICFGR_PLLSAIR_SHIFT 28
Patrice Chotardd5d36552018-01-19 18:02:40 +010066#define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16)
Patrice Chotard369d4832017-11-15 13:14:52 +010067#define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26)
Patrice Chotardff7b11e2018-02-08 17:20:49 +010068#define RCC_PLLSAICFGR_PLLSAIR_3 BIT(29) | BIT(28)
Patrice Chotard369d4832017-11-15 13:14:52 +010069
Patrice Chotardef772872018-02-07 10:44:46 +010070#define RCC_DCKCFGRX_TIMPRE BIT(24)
Patrice Chotard369d4832017-11-15 13:14:52 +010071#define RCC_DCKCFGRX_CK48MSEL BIT(27)
72#define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
73#define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
74
Patrice Chotardff7b11e2018-02-08 17:20:49 +010075#define RCC_DCKCFGR_PLLSAIDIVR_SHIFT 16
76#define RCC_DCKCFGR_PLLSAIDIVR_MASK GENMASK(17, 16)
77#define RCC_DCKCFGR_PLLSAIDIVR_2 0
78
Patrice Chotard06fc6482017-11-15 13:14:49 +010079/*
80 * RCC AHB1ENR specific definitions
81 */
82#define RCC_AHB1ENR_ETHMAC_EN BIT(25)
83#define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
84#define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
85
86/*
87 * RCC APB1ENR specific definitions
88 */
89#define RCC_APB1ENR_TIM2EN BIT(0)
90#define RCC_APB1ENR_PWREN BIT(28)
91
92/*
93 * RCC APB2ENR specific definitions
94 */
95#define RCC_APB2ENR_SYSCFGEN BIT(14)
Patrice Chotarde2d564e2018-01-18 14:10:05 +010096#define RCC_APB2ENR_SAI1EN BIT(22)
Patrice Chotard06fc6482017-11-15 13:14:49 +010097
Patrice Chotard8aca2d12018-02-08 17:20:48 +010098enum pllsai_div {
99 PLLSAIP,
100 PLLSAIQ,
101 PLLSAIR,
102};
103
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100104static const struct stm32_clk_info stm32f4_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100105 /* 180 MHz */
106 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100107 .pll_n = 360,
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 = false,
Patrice Chotard369d4832017-11-15 13:14:52 +0100115 .v2 = false,
Patrice Chotard1509d662017-11-15 13:14:47 +0100116};
117
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100118static const struct stm32_clk_info stm32f7_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100119 /* 200 MHz */
120 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100121 .pll_n = 400,
122 .pll_p = 2,
123 .pll_q = 8,
124 .ahb_psc = AHB_PSC_1,
125 .apb1_psc = APB_PSC_4,
126 .apb2_psc = APB_PSC_2,
127 },
128 .has_overdrive = true,
Patrice Chotard369d4832017-11-15 13:14:52 +0100129 .v2 = true,
Patrice Chotard1509d662017-11-15 13:14:47 +0100130};
131
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200132struct stm32_clk {
133 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100134 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100135 struct stm32_clk_info info;
136 unsigned long hse_rate;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900137};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900138
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100139static const u8 pllsaidivr_table[] = { 2, 4, 8, 16 };
140
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200141static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900142{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200143 struct stm32_clk *priv = dev_get_priv(dev);
144 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100145 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100146 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200147
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900148 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200149 setbits_le32(&regs->cr, RCC_CR_HSION);
150 writel(0, &regs->cfgr); /* Reset CFGR */
151 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard369d4832017-11-15 13:14:52 +0100152 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200153 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
154 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
155 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900156
157 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200158 setbits_le32(&regs->cr, RCC_CR_HSEON);
159 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900160 ;
161
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200162 setbits_le32(&regs->cfgr, ((
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100163 sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT)
164 | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT)
165 | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900166
167 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200168 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
169 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100170 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200171 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100172 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200173 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100174 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200175 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100176 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900177
Patrice Chotardcb15d282018-02-08 17:20:47 +0100178 /* configure SDMMC clock */
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100179 if (priv->info.v2) { /*stm32f7 case */
Patrice Chotardcb15d282018-02-08 17:20:47 +0100180 /* select PLLQ as 48MHz clock source */
181 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
Patrice Chotard369d4832017-11-15 13:14:52 +0100182
183 /* select 48MHz as SDMMC1 clock source */
184 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
185
186 /* select 48MHz as SDMMC2 clock source */
187 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
188 } else { /* stm32f4 case */
Patrice Chotardcb15d282018-02-08 17:20:47 +0100189 /* select PLLQ as 48MHz clock source */
190 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
Patrice Chotard369d4832017-11-15 13:14:52 +0100191
192 /* select 48MHz as SDMMC1 clock source */
193 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
194 }
195
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100196#ifdef CONFIG_VIDEO_STM32
197 /*
198 * Configure the SAI PLL to generate LTDC pixel clock
199 */
200 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK,
201 RCC_PLLSAICFGR_PLLSAIR_3);
202 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIN_MASK,
203 195 << RCC_PLLSAICFGR_PLLSAIN_SHIFT);
204
205 clrsetbits_le32(&regs->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK,
206 RCC_DCKCFGR_PLLSAIDIVR_2 << RCC_DCKCFGR_PLLSAIDIVR_SHIFT);
207#endif
Patrice Chotardcb15d282018-02-08 17:20:47 +0100208 /* Enable the main PLL */
209 setbits_le32(&regs->cr, RCC_CR_PLLON);
210 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Patrice Chotard369d4832017-11-15 13:14:52 +0100211 ;
212
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100213#ifdef CONFIG_VIDEO_STM32
214/* Enable the SAI PLL */
215 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
216 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
217 ;
218#endif
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200219 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100220
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100221 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100222 /*
223 * Enable high performance mode
224 * System frequency up to 200 MHz
225 */
226 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
227 /* Infinite wait! */
228 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
229 ;
230 /* Enable the Over-drive switch */
231 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
232 /* Infinite wait! */
233 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
234 ;
235 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900236
237 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200238 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
239 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900240
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200241 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900242 RCC_CFGR_SWS_PLL)
243 ;
244
Patrice Chotarde2d564e2018-01-18 14:10:05 +0100245#ifdef CONFIG_ETH_DESIGNWARE
246 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
247 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SYSCFGEN);
248#endif
249
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900250 return 0;
251}
252
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100253static bool stm32_clk_get_ck48msel(struct stm32_clk *priv)
Patrice Chotard369d4832017-11-15 13:14:52 +0100254{
255 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard369d4832017-11-15 13:14:52 +0100256
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100257 if (priv->info.v2) /*stm32f7 case */
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100258 return readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
Patrice Chotard369d4832017-11-15 13:14:52 +0100259 else
Patrice Chotard369d4832017-11-15 13:14:52 +0100260
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100261 return readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
262}
263
264static unsigned long stm32_clk_get_pllsai_vco_rate(struct stm32_clk *priv)
265{
266 struct stm32_rcc_regs *regs = priv->base;
267 u16 pllm, pllsain;
268
269 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
270 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLSAICFGR_PLLSAIN_MASK)
271 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
272
273 return ((priv->hse_rate / pllm) * pllsain);
274}
275
276static unsigned long stm32_clk_get_pllsai_rate(struct stm32_clk *priv,
277 enum pllsai_div output)
278{
279 struct stm32_rcc_regs *regs = priv->base;
280 u16 pll_div_output;
281
282 switch (output) {
283 case PLLSAIP:
284 pll_div_output = ((((readl(&regs->pllsaicfgr)
285 & RCC_PLLSAICFGR_PLLSAIP_MASK)
286 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
287 break;
288 case PLLSAIQ:
289 pll_div_output = (readl(&regs->pllsaicfgr)
290 & RCC_PLLSAICFGR_PLLSAIQ_MASK)
291 >> RCC_PLLSAICFGR_PLLSAIQ_SHIFT;
292 break;
293 case PLLSAIR:
294 pll_div_output = (readl(&regs->pllsaicfgr)
295 & RCC_PLLSAICFGR_PLLSAIR_MASK)
296 >> RCC_PLLSAICFGR_PLLSAIR_SHIFT;
297 break;
298 default:
299 pr_err("incorrect PLLSAI output %d\n", output);
300 return -EINVAL;
Patrice Chotard369d4832017-11-15 13:14:52 +0100301 }
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100302
303 return (stm32_clk_get_pllsai_vco_rate(priv) / pll_div_output);
Patrice Chotard369d4832017-11-15 13:14:52 +0100304}
305
Patrice Chotardef772872018-02-07 10:44:46 +0100306static bool stm32_get_timpre(struct stm32_clk *priv)
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200307{
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200308 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardef772872018-02-07 10:44:46 +0100309 u32 val;
310
311 if (priv->info.v2) /*stm32f7 case */
312 val = readl(&regs->dckcfgr2);
313 else
314 val = readl(&regs->dckcfgr);
315 /* get timer prescaler */
316 return !!(val & RCC_DCKCFGRX_TIMPRE);
317}
318
319static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk)
320{
321 u8 shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200322 /* Prescaler table lookups for clock computation */
323 u8 ahb_psc_table[16] = {
324 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
325 };
Patrice Chotardef772872018-02-07 10:44:46 +0100326
327 shift = ahb_psc_table[(
328 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
329 >> RCC_CFGR_HPRE_SHIFT)];
330
331 return sysclk >> shift;
332};
333
334static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb)
335{
336 /* Prescaler table lookups for clock computation */
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200337 u8 apb_psc_table[8] = {
338 0, 0, 0, 0, 1, 2, 3, 4
339 };
340
Patrice Chotardef772872018-02-07 10:44:46 +0100341 if (apb == APB1)
342 return apb_psc_table[(
343 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
344 >> RCC_CFGR_PPRE1_SHIFT)];
345 else /* APB2 */
346 return apb_psc_table[(
347 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
348 >> RCC_CFGR_PPRE2_SHIFT)];
349};
350
351static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk,
352 enum apb apb)
353{
354 struct stm32_rcc_regs *regs = priv->base;
355 u8 shift = stm32_get_apb_shift(regs, apb);
356
357 if (stm32_get_timpre(priv))
358 /*
359 * if APB prescaler is configured to a
360 * division factor of 1, 2 or 4
361 */
362 switch (shift) {
363 case 0:
364 case 1:
365 case 2:
366 return stm32_get_hclk_rate(regs, sysclk);
367 default:
368 return (sysclk >> shift) * 4;
369 }
370 else
371 /*
372 * if APB prescaler is configured to a
373 * division factor of 1
374 */
375 if (shift == 0)
376 return sysclk;
377 else
378 return (sysclk >> shift) * 2;
379};
380
381static ulong stm32_clk_get_rate(struct clk *clk)
382{
383 struct stm32_clk *priv = dev_get_priv(clk->dev);
384 struct stm32_rcc_regs *regs = priv->base;
385 u32 sysclk = 0;
Patrice Chotard81d77652018-02-08 17:20:45 +0100386 u32 vco;
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100387 u32 sdmmcxsel_bit;
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100388 u32 saidivr;
389 u32 pllsai_rate;
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100390 u16 pllm, plln, pllp, pllq;
Patrice Chotardef772872018-02-07 10:44:46 +0100391
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200392 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
393 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200394 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
395 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
396 >> RCC_PLLCFGR_PLLN_SHIFT);
397 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
398 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100399 pllq = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
400 >> RCC_PLLCFGR_PLLQ_SHIFT);
Patrice Chotard81d77652018-02-08 17:20:45 +0100401 vco = (priv->hse_rate / pllm) * plln;
402 sysclk = vco / pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200403 } else {
404 return -EINVAL;
405 }
406
407 switch (clk->id) {
408 /*
409 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
410 * AHB1, AHB2 and AHB3
411 */
412 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
Patrice Chotardef772872018-02-07 10:44:46 +0100413 return stm32_get_hclk_rate(regs, sysclk);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200414 /* APB1 CLOCK */
415 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
Patrice Chotardef772872018-02-07 10:44:46 +0100416 /* For timer clock, an additionnal prescaler is used*/
417 switch (clk->id) {
418 case STM32F7_APB1_CLOCK(TIM2):
419 case STM32F7_APB1_CLOCK(TIM3):
420 case STM32F7_APB1_CLOCK(TIM4):
421 case STM32F7_APB1_CLOCK(TIM5):
422 case STM32F7_APB1_CLOCK(TIM6):
423 case STM32F7_APB1_CLOCK(TIM7):
424 case STM32F7_APB1_CLOCK(TIM12):
425 case STM32F7_APB1_CLOCK(TIM13):
426 case STM32F7_APB1_CLOCK(TIM14):
427 return stm32_get_timer_rate(priv, sysclk, APB1);
428 }
429 return (sysclk >> stm32_get_apb_shift(regs, APB1));
430
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200431 /* APB2 CLOCK */
432 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100433 switch (clk->id) {
Patrice Chotard369d4832017-11-15 13:14:52 +0100434 /*
435 * particular case for SDMMC1 and SDMMC2 :
436 * 48Mhz source clock can be from main PLL or from
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100437 * PLLSAIP
Patrice Chotard369d4832017-11-15 13:14:52 +0100438 */
Patrice Chotard369d4832017-11-15 13:14:52 +0100439 case STM32F7_APB2_CLOCK(SDMMC1):
Patrice Chotard369d4832017-11-15 13:14:52 +0100440 case STM32F7_APB2_CLOCK(SDMMC2):
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100441 if (clk->id == STM32F7_APB2_CLOCK(SDMMC1))
442 sdmmcxsel_bit = RCC_DCKCFGRX_SDMMC1SEL;
443 else
444 sdmmcxsel_bit = RCC_DCKCFGR2_SDMMC2SEL;
445
446 if (readl(&regs->dckcfgr2) & sdmmcxsel_bit)
447 /* System clock is selected as SDMMC1 clock */
Patrice Chotard369d4832017-11-15 13:14:52 +0100448 return sysclk;
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100449 /*
450 * 48 MHz can be generated by either PLLSAIP
451 * or by PLLQ depending of CK48MSEL bit of RCC_DCKCFGR
452 */
453 if (stm32_clk_get_ck48msel(priv))
454 return stm32_clk_get_pllsai_rate(priv, PLLSAIP);
Patrice Chotard369d4832017-11-15 13:14:52 +0100455 else
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100456 return (vco / pllq);
Patrice Chotard369d4832017-11-15 13:14:52 +0100457 break;
Patrice Chotardef772872018-02-07 10:44:46 +0100458
459 /* For timer clock, an additionnal prescaler is used*/
460 case STM32F7_APB2_CLOCK(TIM1):
461 case STM32F7_APB2_CLOCK(TIM8):
462 case STM32F7_APB2_CLOCK(TIM9):
463 case STM32F7_APB2_CLOCK(TIM10):
464 case STM32F7_APB2_CLOCK(TIM11):
465 return stm32_get_timer_rate(priv, sysclk, APB2);
466 break;
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100467
468 /* particular case for LTDC clock */
469 case STM32F7_APB2_CLOCK(LTDC):
470 saidivr = readl(&regs->dckcfgr);
471 saidivr = (saidivr & RCC_DCKCFGR_PLLSAIDIVR_MASK)
472 >> RCC_DCKCFGR_PLLSAIDIVR_SHIFT;
473 pllsai_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR);
474
475 return pllsai_rate / pllsaidivr_table[saidivr];
Patrice Chotard369d4832017-11-15 13:14:52 +0100476 }
Patrice Chotardef772872018-02-07 10:44:46 +0100477 return (sysclk >> stm32_get_apb_shift(regs, APB2));
Patrice Chotard369d4832017-11-15 13:14:52 +0100478
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200479 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900480 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200481 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200482 }
483}
484
Patrice Chotard24e85782018-01-29 18:14:14 +0100485static ulong stm32_set_rate(struct clk *clk, ulong rate)
486{
487 return 0;
488}
489
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800490static int stm32_clk_enable(struct clk *clk)
491{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200492 struct stm32_clk *priv = dev_get_priv(clk->dev);
493 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800494 u32 offset = clk->id / 32;
495 u32 bit_index = clk->id % 32;
496
497 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
498 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200499 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800500
501 return 0;
502}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900503
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800504static int stm32_clk_probe(struct udevice *dev)
505{
Patrice Chotard22768d52017-11-15 13:14:44 +0100506 struct ofnode_phandle_args args;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100507 struct udevice *fixed_clock_dev = NULL;
508 struct clk clk;
Patrice Chotard22768d52017-11-15 13:14:44 +0100509 int err;
510
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100511 debug("%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200512
513 struct stm32_clk *priv = dev_get_priv(dev);
514 fdt_addr_t addr;
515
Patrice Chotard1509d662017-11-15 13:14:47 +0100516 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200517 if (addr == FDT_ADDR_T_NONE)
518 return -EINVAL;
519
520 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard03f10a12017-11-15 13:14:51 +0100521
522 switch (dev_get_driver_data(dev)) {
523 case STM32F4:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100524 memcpy(&priv->info, &stm32f4_clk_info,
525 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100526 break;
527 case STM32F7:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100528 memcpy(&priv->info, &stm32f7_clk_info,
529 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100530 break;
531 default:
532 return -EINVAL;
533 }
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200534
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100535 /* retrieve HSE frequency (external oscillator) */
536 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
537 &fixed_clock_dev);
538
539 if (err) {
540 pr_err("Can't find fixed clock (%d)", err);
541 return err;
542 }
543
544 err = clk_request(fixed_clock_dev, &clk);
545 if (err) {
546 pr_err("Can't request %s clk (%d)", fixed_clock_dev->name,
547 err);
548 return err;
549 }
550
551 /*
552 * set pllm factor accordingly to the external oscillator
553 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
554 * freq at 1MHz
555 * if input PLL frequency is 25Mhz, divide it by 25
556 */
557 clk.id = 0;
558 priv->hse_rate = clk_get_rate(&clk);
559
560 if (priv->hse_rate < 1000000) {
561 pr_err("%s: unexpected HSE clock rate = %ld \"n", __func__,
562 priv->hse_rate);
563 return -EINVAL;
564 }
565
566 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
567
568 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100569 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
570 &args);
571 if (err) {
572 debug("%s: can't find syscon device (%d)\n", __func__,
573 err);
574 return err;
575 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100576
Patrice Chotard1509d662017-11-15 13:14:47 +0100577 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
578 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100579
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200580 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800581
582 return 0;
583}
584
Simon Glassb7ae2772017-05-18 20:09:40 -0600585static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800586{
587 debug("%s(clk=%p)\n", __func__, clk);
588
589 if (args->args_count != 2) {
590 debug("Invaild args_count: %d\n", args->args_count);
591 return -EINVAL;
592 }
593
594 if (args->args_count)
595 clk->id = args->args[1];
596 else
597 clk->id = 0;
598
599 return 0;
600}
601
602static struct clk_ops stm32_clk_ops = {
603 .of_xlate = stm32_clk_of_xlate,
604 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200605 .get_rate = stm32_clk_get_rate,
Patrice Chotard24e85782018-01-29 18:14:14 +0100606 .set_rate = stm32_set_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800607};
608
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100609U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard03f10a12017-11-15 13:14:51 +0100610 .name = "stm32fx_rcc_clock",
Patrice Chotardb323de52017-09-21 10:08:09 +0200611 .id = UCLASS_CLK,
Patrice Chotardb323de52017-09-21 10:08:09 +0200612 .ops = &stm32_clk_ops,
613 .probe = stm32_clk_probe,
614 .priv_auto_alloc_size = sizeof(struct stm32_clk),
615 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800616};