blob: fceb3c44b94e4e4f5249ff1bc99c7ae4adba4316 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vikas Manocha1b51c932016-02-11 15:47:20 -08002/*
Patrice Chotard789ee0e2017-10-23 09:53:58 +02003 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
Vikas Manocha1b51c932016-02-11 15:47:20 -08005 */
Patrice Chotard789ee0e2017-10-23 09:53:58 +02006
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +01007#define LOG_CATEGORY UCLASS_CLK
8
Vikas Manochadaaeaab2017-02-12 10:25:45 -08009#include <clk-uclass.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrice Chotard03f10a12017-11-15 13:14:51 +010012#include <stm32_rcc.h>
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>
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +010016#include <dm/device_compat.h>
Patrice Chotard7bdf9712017-07-18 09:29:05 +020017#include <dt-bindings/mfd/stm32f7-rcc.h>
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +010018#include <linux/bitops.h>
Patrice Chotard7bdf9712017-07-18 09:29:05 +020019
Michael Kurzc204fb72017-01-22 16:04:24 +010020#define RCC_CR_HSION BIT(0)
21#define RCC_CR_HSEON BIT(16)
22#define RCC_CR_HSERDY BIT(17)
23#define RCC_CR_HSEBYP BIT(18)
24#define RCC_CR_CSSON BIT(19)
25#define RCC_CR_PLLON BIT(24)
26#define RCC_CR_PLLRDY BIT(25)
Patrice Chotard369d4832017-11-15 13:14:52 +010027#define RCC_CR_PLLSAION BIT(28)
28#define RCC_CR_PLLSAIRDY BIT(29)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090029
Michael Kurzc204fb72017-01-22 16:04:24 +010030#define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
31#define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
32#define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
33#define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
34#define RCC_PLLCFGR_PLLSRC BIT(22)
35#define RCC_PLLCFGR_PLLM_SHIFT 0
36#define RCC_PLLCFGR_PLLN_SHIFT 6
37#define RCC_PLLCFGR_PLLP_SHIFT 16
38#define RCC_PLLCFGR_PLLQ_SHIFT 24
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090039
Michael Kurzc204fb72017-01-22 16:04:24 +010040#define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
41#define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
42#define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
43#define RCC_CFGR_SW0 BIT(0)
44#define RCC_CFGR_SW1 BIT(1)
45#define RCC_CFGR_SW_MASK GENMASK(1, 0)
46#define RCC_CFGR_SW_HSI 0
47#define RCC_CFGR_SW_HSE RCC_CFGR_SW0
48#define RCC_CFGR_SW_PLL RCC_CFGR_SW1
49#define RCC_CFGR_SWS0 BIT(2)
50#define RCC_CFGR_SWS1 BIT(3)
51#define RCC_CFGR_SWS_MASK GENMASK(3, 2)
52#define RCC_CFGR_SWS_HSI 0
53#define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
54#define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
55#define RCC_CFGR_HPRE_SHIFT 4
56#define RCC_CFGR_PPRE1_SHIFT 10
57#define RCC_CFGR_PPRE2_SHIFT 13
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090058
Patrice Chotard9490aca2018-02-08 17:20:46 +010059#define RCC_PLLSAICFGR_PLLSAIN_MASK GENMASK(14, 6)
60#define RCC_PLLSAICFGR_PLLSAIP_MASK GENMASK(17, 16)
Patrice Chotard8aca2d12018-02-08 17:20:48 +010061#define RCC_PLLSAICFGR_PLLSAIQ_MASK GENMASK(27, 24)
62#define RCC_PLLSAICFGR_PLLSAIR_MASK GENMASK(30, 28)
Patrice Chotard369d4832017-11-15 13:14:52 +010063#define RCC_PLLSAICFGR_PLLSAIN_SHIFT 6
64#define RCC_PLLSAICFGR_PLLSAIP_SHIFT 16
Patrice Chotard8aca2d12018-02-08 17:20:48 +010065#define RCC_PLLSAICFGR_PLLSAIQ_SHIFT 24
66#define RCC_PLLSAICFGR_PLLSAIR_SHIFT 28
Patrice Chotardd5d36552018-01-19 18:02:40 +010067#define RCC_PLLSAICFGR_PLLSAIP_4 BIT(16)
Patrice Chotard369d4832017-11-15 13:14:52 +010068#define RCC_PLLSAICFGR_PLLSAIQ_4 BIT(26)
Patrice Chotardff7b11e2018-02-08 17:20:49 +010069#define RCC_PLLSAICFGR_PLLSAIR_3 BIT(29) | BIT(28)
Patrice Chotard369d4832017-11-15 13:14:52 +010070
Patrice Chotardef772872018-02-07 10:44:46 +010071#define RCC_DCKCFGRX_TIMPRE BIT(24)
Patrice Chotard369d4832017-11-15 13:14:52 +010072#define RCC_DCKCFGRX_CK48MSEL BIT(27)
73#define RCC_DCKCFGRX_SDMMC1SEL BIT(28)
74#define RCC_DCKCFGR2_SDMMC2SEL BIT(29)
75
Patrice Chotardff7b11e2018-02-08 17:20:49 +010076#define RCC_DCKCFGR_PLLSAIDIVR_SHIFT 16
77#define RCC_DCKCFGR_PLLSAIDIVR_MASK GENMASK(17, 16)
78#define RCC_DCKCFGR_PLLSAIDIVR_2 0
79
Patrice Chotard06fc6482017-11-15 13:14:49 +010080/*
81 * RCC AHB1ENR specific definitions
82 */
83#define RCC_AHB1ENR_ETHMAC_EN BIT(25)
84#define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
85#define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
86
87/*
88 * RCC APB1ENR specific definitions
89 */
90#define RCC_APB1ENR_TIM2EN BIT(0)
91#define RCC_APB1ENR_PWREN BIT(28)
92
93/*
94 * RCC APB2ENR specific definitions
95 */
96#define RCC_APB2ENR_SYSCFGEN BIT(14)
Patrice Chotarde2d564e2018-01-18 14:10:05 +010097#define RCC_APB2ENR_SAI1EN BIT(22)
Patrice Chotard06fc6482017-11-15 13:14:49 +010098
Patrice Chotard8aca2d12018-02-08 17:20:48 +010099enum pllsai_div {
100 PLLSAIP,
101 PLLSAIQ,
102 PLLSAIR,
103};
104
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100105static const struct stm32_clk_info stm32f4_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100106 /* 180 MHz */
107 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100108 .pll_n = 360,
109 .pll_p = 2,
110 .pll_q = 8,
111 .ahb_psc = AHB_PSC_1,
112 .apb1_psc = APB_PSC_4,
113 .apb2_psc = APB_PSC_2,
114 },
115 .has_overdrive = false,
Patrice Chotard369d4832017-11-15 13:14:52 +0100116 .v2 = false,
Patrice Chotard1509d662017-11-15 13:14:47 +0100117};
118
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100119static const struct stm32_clk_info stm32f7_clk_info = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100120 /* 200 MHz */
121 .sys_pll_psc = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100122 .pll_n = 400,
123 .pll_p = 2,
124 .pll_q = 8,
125 .ahb_psc = AHB_PSC_1,
126 .apb1_psc = APB_PSC_4,
127 .apb2_psc = APB_PSC_2,
128 },
129 .has_overdrive = true,
Patrice Chotard369d4832017-11-15 13:14:52 +0100130 .v2 = true,
Patrice Chotard1509d662017-11-15 13:14:47 +0100131};
132
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200133struct stm32_clk {
134 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100135 struct stm32_pwr_regs *pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100136 struct stm32_clk_info info;
137 unsigned long hse_rate;
Patrice Chotard7264aae2018-04-11 17:07:45 +0200138 bool pllsaip;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900139};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900140
Patrice Chotard2cd1ed12018-02-08 17:20:50 +0100141#ifdef CONFIG_VIDEO_STM32
142static const u8 plldivr_table[] = { 0, 0, 2, 3, 4, 5, 6, 7 };
143#endif
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100144static const u8 pllsaidivr_table[] = { 2, 4, 8, 16 };
145
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200146static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900147{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200148 struct stm32_clk *priv = dev_get_priv(dev);
149 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100150 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100151 struct pll_psc *sys_pll_psc = &priv->info.sys_pll_psc;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200152
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900153 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200154 setbits_le32(&regs->cr, RCC_CR_HSION);
155 writel(0, &regs->cfgr); /* Reset CFGR */
156 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Patrice Chotard369d4832017-11-15 13:14:52 +0100157 | RCC_CR_PLLON | RCC_CR_PLLSAION));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200158 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
159 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
160 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900161
162 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200163 setbits_le32(&regs->cr, RCC_CR_HSEON);
164 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900165 ;
166
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200167 setbits_le32(&regs->cfgr, ((
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100168 sys_pll_psc->ahb_psc << RCC_CFGR_HPRE_SHIFT)
169 | (sys_pll_psc->apb1_psc << RCC_CFGR_PPRE1_SHIFT)
170 | (sys_pll_psc->apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900171
172 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200173 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
174 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100175 sys_pll_psc->pll_m << RCC_PLLCFGR_PLLM_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200176 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100177 sys_pll_psc->pll_n << RCC_PLLCFGR_PLLN_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200178 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100179 ((sys_pll_psc->pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
Patrice Chotardb6653f62017-10-26 13:23:19 +0200180 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100181 sys_pll_psc->pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900182
Patrice Chotardcb15d282018-02-08 17:20:47 +0100183 /* configure SDMMC clock */
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100184 if (priv->info.v2) { /*stm32f7 case */
Patrice Chotard7264aae2018-04-11 17:07:45 +0200185 if (priv->pllsaip)
186 /* select PLLSAIP as 48MHz clock source */
187 setbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
188 else
189 /* select PLLQ as 48MHz clock source */
190 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_CK48MSEL);
Patrice Chotard369d4832017-11-15 13:14:52 +0100191
192 /* select 48MHz as SDMMC1 clock source */
193 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGRX_SDMMC1SEL);
194
195 /* select 48MHz as SDMMC2 clock source */
196 clrbits_le32(&regs->dckcfgr2, RCC_DCKCFGR2_SDMMC2SEL);
197 } else { /* stm32f4 case */
Patrice Chotard7264aae2018-04-11 17:07:45 +0200198 if (priv->pllsaip)
199 /* select PLLSAIP as 48MHz clock source */
200 setbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
201 else
202 /* select PLLQ as 48MHz clock source */
203 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_CK48MSEL);
Patrice Chotard369d4832017-11-15 13:14:52 +0100204
205 /* select 48MHz as SDMMC1 clock source */
206 clrbits_le32(&regs->dckcfgr, RCC_DCKCFGRX_SDMMC1SEL);
207 }
208
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100209 /*
Patrice Chotard7264aae2018-04-11 17:07:45 +0200210 * Configure the SAI PLL to generate LTDC pixel clock and
211 * 48 Mhz for SDMMC and USB
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100212 */
Patrice Chotard7264aae2018-04-11 17:07:45 +0200213 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIP_MASK,
214 RCC_PLLSAICFGR_PLLSAIP_4);
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100215 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK,
216 RCC_PLLSAICFGR_PLLSAIR_3);
217 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIN_MASK,
218 195 << RCC_PLLSAICFGR_PLLSAIN_SHIFT);
219
220 clrsetbits_le32(&regs->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK,
221 RCC_DCKCFGR_PLLSAIDIVR_2 << RCC_DCKCFGR_PLLSAIDIVR_SHIFT);
Patrice Chotard7264aae2018-04-11 17:07:45 +0200222
Patrice Chotardcb15d282018-02-08 17:20:47 +0100223 /* Enable the main PLL */
224 setbits_le32(&regs->cr, RCC_CR_PLLON);
225 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Patrice Chotard369d4832017-11-15 13:14:52 +0100226 ;
227
Patrice Chotard7264aae2018-04-11 17:07:45 +0200228 /* Enable the SAI PLL */
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100229 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
230 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
231 ;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200232 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100233
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100234 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100235 /*
236 * Enable high performance mode
237 * System frequency up to 200 MHz
238 */
239 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
240 /* Infinite wait! */
241 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
242 ;
243 /* Enable the Over-drive switch */
244 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
245 /* Infinite wait! */
246 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
247 ;
248 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900249
250 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200251 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
252 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900253
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200254 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900255 RCC_CFGR_SWS_PLL)
256 ;
257
Patrice Chotarde2d564e2018-01-18 14:10:05 +0100258#ifdef CONFIG_ETH_DESIGNWARE
259 /* gate the SYSCFG clock, needed to set RMII ethernet interface */
260 setbits_le32(&regs->apb2enr, RCC_APB2ENR_SYSCFGEN);
261#endif
262
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900263 return 0;
264}
265
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100266static bool stm32_clk_get_ck48msel(struct stm32_clk *priv)
Patrice Chotard369d4832017-11-15 13:14:52 +0100267{
268 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard369d4832017-11-15 13:14:52 +0100269
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100270 if (priv->info.v2) /*stm32f7 case */
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100271 return readl(&regs->dckcfgr2) & RCC_DCKCFGRX_CK48MSEL;
Patrice Chotard369d4832017-11-15 13:14:52 +0100272 else
Patrice Chotard369d4832017-11-15 13:14:52 +0100273
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100274 return readl(&regs->dckcfgr) & RCC_DCKCFGRX_CK48MSEL;
275}
276
277static unsigned long stm32_clk_get_pllsai_vco_rate(struct stm32_clk *priv)
278{
279 struct stm32_rcc_regs *regs = priv->base;
280 u16 pllm, pllsain;
281
282 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
283 pllsain = ((readl(&regs->pllsaicfgr) & RCC_PLLSAICFGR_PLLSAIN_MASK)
284 >> RCC_PLLSAICFGR_PLLSAIN_SHIFT);
285
286 return ((priv->hse_rate / pllm) * pllsain);
287}
288
289static unsigned long stm32_clk_get_pllsai_rate(struct stm32_clk *priv,
290 enum pllsai_div output)
291{
292 struct stm32_rcc_regs *regs = priv->base;
293 u16 pll_div_output;
294
295 switch (output) {
296 case PLLSAIP:
297 pll_div_output = ((((readl(&regs->pllsaicfgr)
298 & RCC_PLLSAICFGR_PLLSAIP_MASK)
299 >> RCC_PLLSAICFGR_PLLSAIP_SHIFT) + 1) << 1);
300 break;
301 case PLLSAIQ:
302 pll_div_output = (readl(&regs->pllsaicfgr)
303 & RCC_PLLSAICFGR_PLLSAIQ_MASK)
304 >> RCC_PLLSAICFGR_PLLSAIQ_SHIFT;
305 break;
306 case PLLSAIR:
307 pll_div_output = (readl(&regs->pllsaicfgr)
308 & RCC_PLLSAICFGR_PLLSAIR_MASK)
309 >> RCC_PLLSAICFGR_PLLSAIR_SHIFT;
310 break;
311 default:
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100312 log_err("incorrect PLLSAI output %d\n", output);
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100313 return -EINVAL;
Patrice Chotard369d4832017-11-15 13:14:52 +0100314 }
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100315
316 return (stm32_clk_get_pllsai_vco_rate(priv) / pll_div_output);
Patrice Chotard369d4832017-11-15 13:14:52 +0100317}
318
Patrice Chotardef772872018-02-07 10:44:46 +0100319static bool stm32_get_timpre(struct stm32_clk *priv)
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200320{
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200321 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardef772872018-02-07 10:44:46 +0100322 u32 val;
323
324 if (priv->info.v2) /*stm32f7 case */
325 val = readl(&regs->dckcfgr2);
326 else
327 val = readl(&regs->dckcfgr);
328 /* get timer prescaler */
329 return !!(val & RCC_DCKCFGRX_TIMPRE);
330}
331
332static u32 stm32_get_hclk_rate(struct stm32_rcc_regs *regs, u32 sysclk)
333{
334 u8 shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200335 /* Prescaler table lookups for clock computation */
336 u8 ahb_psc_table[16] = {
337 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
338 };
Patrice Chotardef772872018-02-07 10:44:46 +0100339
340 shift = ahb_psc_table[(
341 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
342 >> RCC_CFGR_HPRE_SHIFT)];
343
344 return sysclk >> shift;
345};
346
347static u8 stm32_get_apb_shift(struct stm32_rcc_regs *regs, enum apb apb)
348{
349 /* Prescaler table lookups for clock computation */
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200350 u8 apb_psc_table[8] = {
351 0, 0, 0, 0, 1, 2, 3, 4
352 };
353
Patrice Chotardef772872018-02-07 10:44:46 +0100354 if (apb == APB1)
355 return apb_psc_table[(
356 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
357 >> RCC_CFGR_PPRE1_SHIFT)];
358 else /* APB2 */
359 return apb_psc_table[(
360 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
361 >> RCC_CFGR_PPRE2_SHIFT)];
362};
363
364static u32 stm32_get_timer_rate(struct stm32_clk *priv, u32 sysclk,
365 enum apb apb)
366{
367 struct stm32_rcc_regs *regs = priv->base;
368 u8 shift = stm32_get_apb_shift(regs, apb);
369
370 if (stm32_get_timpre(priv))
371 /*
372 * if APB prescaler is configured to a
373 * division factor of 1, 2 or 4
374 */
375 switch (shift) {
376 case 0:
377 case 1:
378 case 2:
379 return stm32_get_hclk_rate(regs, sysclk);
380 default:
381 return (sysclk >> shift) * 4;
382 }
383 else
384 /*
385 * if APB prescaler is configured to a
386 * division factor of 1
387 */
388 if (shift == 0)
389 return sysclk;
390 else
391 return (sysclk >> shift) * 2;
392};
393
394static ulong stm32_clk_get_rate(struct clk *clk)
395{
396 struct stm32_clk *priv = dev_get_priv(clk->dev);
397 struct stm32_rcc_regs *regs = priv->base;
398 u32 sysclk = 0;
Patrice Chotard81d77652018-02-08 17:20:45 +0100399 u32 vco;
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100400 u32 sdmmcxsel_bit;
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100401 u32 saidivr;
402 u32 pllsai_rate;
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100403 u16 pllm, plln, pllp, pllq;
Patrice Chotardef772872018-02-07 10:44:46 +0100404
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200405 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
406 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200407 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
408 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
409 >> RCC_PLLCFGR_PLLN_SHIFT);
410 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
411 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100412 pllq = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLQ_MASK)
413 >> RCC_PLLCFGR_PLLQ_SHIFT);
Patrice Chotard81d77652018-02-08 17:20:45 +0100414 vco = (priv->hse_rate / pllm) * plln;
415 sysclk = vco / pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200416 } else {
417 return -EINVAL;
418 }
419
420 switch (clk->id) {
421 /*
422 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
423 * AHB1, AHB2 and AHB3
424 */
425 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
Patrice Chotardef772872018-02-07 10:44:46 +0100426 return stm32_get_hclk_rate(regs, sysclk);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200427 /* APB1 CLOCK */
428 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
Patrice Chotardef772872018-02-07 10:44:46 +0100429 /* For timer clock, an additionnal prescaler is used*/
430 switch (clk->id) {
431 case STM32F7_APB1_CLOCK(TIM2):
432 case STM32F7_APB1_CLOCK(TIM3):
433 case STM32F7_APB1_CLOCK(TIM4):
434 case STM32F7_APB1_CLOCK(TIM5):
435 case STM32F7_APB1_CLOCK(TIM6):
436 case STM32F7_APB1_CLOCK(TIM7):
437 case STM32F7_APB1_CLOCK(TIM12):
438 case STM32F7_APB1_CLOCK(TIM13):
439 case STM32F7_APB1_CLOCK(TIM14):
440 return stm32_get_timer_rate(priv, sysclk, APB1);
441 }
442 return (sysclk >> stm32_get_apb_shift(regs, APB1));
443
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200444 /* APB2 CLOCK */
Patrice Chotarde199c3a2018-02-08 17:20:51 +0100445 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(DSI):
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100446 switch (clk->id) {
Patrice Chotard369d4832017-11-15 13:14:52 +0100447 /*
448 * particular case for SDMMC1 and SDMMC2 :
449 * 48Mhz source clock can be from main PLL or from
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100450 * PLLSAIP
Patrice Chotard369d4832017-11-15 13:14:52 +0100451 */
Patrice Chotard369d4832017-11-15 13:14:52 +0100452 case STM32F7_APB2_CLOCK(SDMMC1):
Patrice Chotard369d4832017-11-15 13:14:52 +0100453 case STM32F7_APB2_CLOCK(SDMMC2):
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100454 if (clk->id == STM32F7_APB2_CLOCK(SDMMC1))
455 sdmmcxsel_bit = RCC_DCKCFGRX_SDMMC1SEL;
456 else
457 sdmmcxsel_bit = RCC_DCKCFGR2_SDMMC2SEL;
458
459 if (readl(&regs->dckcfgr2) & sdmmcxsel_bit)
460 /* System clock is selected as SDMMC1 clock */
Patrice Chotard369d4832017-11-15 13:14:52 +0100461 return sysclk;
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100462 /*
463 * 48 MHz can be generated by either PLLSAIP
464 * or by PLLQ depending of CK48MSEL bit of RCC_DCKCFGR
465 */
466 if (stm32_clk_get_ck48msel(priv))
467 return stm32_clk_get_pllsai_rate(priv, PLLSAIP);
Patrice Chotard369d4832017-11-15 13:14:52 +0100468 else
Patrice Chotard8aca2d12018-02-08 17:20:48 +0100469 return (vco / pllq);
Patrice Chotard369d4832017-11-15 13:14:52 +0100470 break;
Patrice Chotardef772872018-02-07 10:44:46 +0100471
472 /* For timer clock, an additionnal prescaler is used*/
473 case STM32F7_APB2_CLOCK(TIM1):
474 case STM32F7_APB2_CLOCK(TIM8):
475 case STM32F7_APB2_CLOCK(TIM9):
476 case STM32F7_APB2_CLOCK(TIM10):
477 case STM32F7_APB2_CLOCK(TIM11):
478 return stm32_get_timer_rate(priv, sysclk, APB2);
479 break;
Patrice Chotardff7b11e2018-02-08 17:20:49 +0100480
481 /* particular case for LTDC clock */
482 case STM32F7_APB2_CLOCK(LTDC):
483 saidivr = readl(&regs->dckcfgr);
484 saidivr = (saidivr & RCC_DCKCFGR_PLLSAIDIVR_MASK)
485 >> RCC_DCKCFGR_PLLSAIDIVR_SHIFT;
486 pllsai_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR);
487
488 return pllsai_rate / pllsaidivr_table[saidivr];
Patrice Chotard369d4832017-11-15 13:14:52 +0100489 }
Patrice Chotardef772872018-02-07 10:44:46 +0100490 return (sysclk >> stm32_get_apb_shift(regs, APB2));
Patrice Chotard369d4832017-11-15 13:14:52 +0100491
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200492 default:
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100493 dev_err(clk->dev, "clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200494 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200495 }
496}
497
Patrice Chotard24e85782018-01-29 18:14:14 +0100498static ulong stm32_set_rate(struct clk *clk, ulong rate)
499{
Patrice Chotard2cd1ed12018-02-08 17:20:50 +0100500#ifdef CONFIG_VIDEO_STM32
501 struct stm32_clk *priv = dev_get_priv(clk->dev);
502 struct stm32_rcc_regs *regs = priv->base;
503 u32 pllsair_rate, pllsai_vco_rate, current_rate;
504 u32 best_div, best_diff, diff;
505 u16 div;
506 u8 best_plldivr, best_pllsaidivr;
507 u8 i, j;
508 bool found = false;
509
510 /* Only set_rate for LTDC clock is implemented */
511 if (clk->id != STM32F7_APB2_CLOCK(LTDC)) {
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100512 dev_err(clk->dev,
513 "set_rate not implemented for clock index %ld\n",
514 clk->id);
Patrice Chotard2cd1ed12018-02-08 17:20:50 +0100515 return 0;
516 }
517
518 if (rate == stm32_clk_get_rate(clk))
519 /* already set to requested rate */
520 return rate;
521
522 /* get the current PLLSAIR output freq */
523 pllsair_rate = stm32_clk_get_pllsai_rate(priv, PLLSAIR);
Dario Binacchi8dcbf812023-11-11 11:46:19 +0100524 if ((pllsair_rate % rate) == 0) {
525 best_div = pllsair_rate / rate;
Patrice Chotard2cd1ed12018-02-08 17:20:50 +0100526
Dario Binacchi8dcbf812023-11-11 11:46:19 +0100527 /* look into pllsaidivr_table if this divider is available */
528 for (i = 0 ; i < sizeof(pllsaidivr_table); i++)
529 if (best_div == pllsaidivr_table[i]) {
530 /* set pll_saidivr with found value */
531 clrsetbits_le32(&regs->dckcfgr,
532 RCC_DCKCFGR_PLLSAIDIVR_MASK,
533 pllsaidivr_table[i] <<
534 RCC_DCKCFGR_PLLSAIDIVR_SHIFT);
535 return rate;
536 }
537 }
Patrice Chotard2cd1ed12018-02-08 17:20:50 +0100538
539 /*
540 * As no pllsaidivr value is suitable to obtain requested freq,
541 * test all combination of pllsaidivr * pllsair and find the one
542 * which give freq closest to requested rate.
543 */
544
545 pllsai_vco_rate = stm32_clk_get_pllsai_vco_rate(priv);
546 best_diff = ULONG_MAX;
547 best_pllsaidivr = 0;
548 best_plldivr = 0;
549 /*
550 * start at index 2 of plldivr_table as divider value at index 0
551 * and 1 are 0)
552 */
553 for (i = 2; i < sizeof(plldivr_table); i++) {
554 for (j = 0; j < sizeof(pllsaidivr_table); j++) {
555 div = plldivr_table[i] * pllsaidivr_table[j];
556 current_rate = pllsai_vco_rate / div;
557 /* perfect combination is found ? */
558 if (current_rate == rate) {
559 best_pllsaidivr = j;
560 best_plldivr = i;
561 found = true;
562 break;
563 }
564
565 diff = (current_rate > rate) ?
566 current_rate - rate : rate - current_rate;
567
568 /* found a better combination ? */
569 if (diff < best_diff) {
570 best_diff = diff;
571 best_pllsaidivr = j;
572 best_plldivr = i;
573 }
574 }
575
576 if (found)
577 break;
578 }
579
580 /* Disable the SAI PLL */
581 clrbits_le32(&regs->cr, RCC_CR_PLLSAION);
582
583 /* set pll_saidivr with found value */
584 clrsetbits_le32(&regs->dckcfgr, RCC_DCKCFGR_PLLSAIDIVR_MASK,
585 best_pllsaidivr << RCC_DCKCFGR_PLLSAIDIVR_SHIFT);
586
587 /* set pllsair with found value */
588 clrsetbits_le32(&regs->pllsaicfgr, RCC_PLLSAICFGR_PLLSAIR_MASK,
589 plldivr_table[best_plldivr]
590 << RCC_PLLSAICFGR_PLLSAIR_SHIFT);
591
592 /* Enable the SAI PLL */
593 setbits_le32(&regs->cr, RCC_CR_PLLSAION);
594 while (!(readl(&regs->cr) & RCC_CR_PLLSAIRDY))
595 ;
596
597 div = plldivr_table[best_plldivr] * pllsaidivr_table[best_pllsaidivr];
598 return pllsai_vco_rate / div;
599#else
Patrice Chotard24e85782018-01-29 18:14:14 +0100600 return 0;
Patrice Chotard2cd1ed12018-02-08 17:20:50 +0100601#endif
Patrice Chotard24e85782018-01-29 18:14:14 +0100602}
603
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800604static int stm32_clk_enable(struct clk *clk)
605{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200606 struct stm32_clk *priv = dev_get_priv(clk->dev);
607 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800608 u32 offset = clk->id / 32;
609 u32 bit_index = clk->id % 32;
610
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100611 dev_dbg(clk->dev, "clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
612 clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200613 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800614
615 return 0;
616}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900617
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800618static int stm32_clk_probe(struct udevice *dev)
619{
Patrice Chotard22768d52017-11-15 13:14:44 +0100620 struct ofnode_phandle_args args;
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100621 struct udevice *fixed_clock_dev = NULL;
622 struct clk clk;
Patrice Chotard22768d52017-11-15 13:14:44 +0100623 int err;
624
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100625 dev_dbg(dev, "%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200626
627 struct stm32_clk *priv = dev_get_priv(dev);
628 fdt_addr_t addr;
629
Patrice Chotard1509d662017-11-15 13:14:47 +0100630 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200631 if (addr == FDT_ADDR_T_NONE)
632 return -EINVAL;
633
634 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard7264aae2018-04-11 17:07:45 +0200635 priv->pllsaip = true;
Patrice Chotard03f10a12017-11-15 13:14:51 +0100636
637 switch (dev_get_driver_data(dev)) {
Patrice Chotard7264aae2018-04-11 17:07:45 +0200638 case STM32F42X:
639 priv->pllsaip = false;
640 /* fallback into STM32F469 case */
641 case STM32F469:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100642 memcpy(&priv->info, &stm32f4_clk_info,
643 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100644 break;
Patrice Chotard7264aae2018-04-11 17:07:45 +0200645
Patrice Chotard03f10a12017-11-15 13:14:51 +0100646 case STM32F7:
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100647 memcpy(&priv->info, &stm32f7_clk_info,
648 sizeof(struct stm32_clk_info));
Patrice Chotard03f10a12017-11-15 13:14:51 +0100649 break;
650 default:
651 return -EINVAL;
652 }
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200653
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100654 /* retrieve HSE frequency (external oscillator) */
655 err = uclass_get_device_by_name(UCLASS_CLK, "clk-hse",
656 &fixed_clock_dev);
657
658 if (err) {
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100659 dev_err(dev, "Can't find fixed clock (%d)", err);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100660 return err;
661 }
662
663 err = clk_request(fixed_clock_dev, &clk);
664 if (err) {
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100665 dev_err(dev, "Can't request %s clk (%d)",
666 fixed_clock_dev->name, err);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100667 return err;
668 }
669
670 /*
671 * set pllm factor accordingly to the external oscillator
672 * frequency (HSE). For STM32F4 and STM32F7, we want VCO
673 * freq at 1MHz
674 * if input PLL frequency is 25Mhz, divide it by 25
675 */
676 clk.id = 0;
677 priv->hse_rate = clk_get_rate(&clk);
678
679 if (priv->hse_rate < 1000000) {
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100680 dev_err(dev, "unexpected HSE clock rate = %ld \"n",
681 priv->hse_rate);
Patrice Chotardacd97ca2018-01-18 13:39:30 +0100682 return -EINVAL;
683 }
684
685 priv->info.sys_pll_psc.pll_m = priv->hse_rate / 1000000;
686
687 if (priv->info.has_overdrive) {
Patrice Chotard1509d662017-11-15 13:14:47 +0100688 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
689 &args);
690 if (err) {
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100691 dev_err(dev, "can't find syscon device (%d)\n", err);
Patrice Chotard1509d662017-11-15 13:14:47 +0100692 return err;
693 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100694
Patrice Chotard1509d662017-11-15 13:14:47 +0100695 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
696 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100697
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200698 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800699
700 return 0;
701}
702
Simon Glassb7ae2772017-05-18 20:09:40 -0600703static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800704{
Patrick Delaunaya57b8cd2020-11-06 19:01:46 +0100705 dev_dbg(clk->dev, "clk=%p\n", clk);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800706
707 if (args->args_count != 2) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500708 dev_dbg(clk->dev, "Invalid args_count: %d\n", args->args_count);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800709 return -EINVAL;
710 }
711
712 if (args->args_count)
713 clk->id = args->args[1];
714 else
715 clk->id = 0;
716
717 return 0;
718}
719
720static struct clk_ops stm32_clk_ops = {
721 .of_xlate = stm32_clk_of_xlate,
722 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200723 .get_rate = stm32_clk_get_rate,
Patrice Chotard24e85782018-01-29 18:14:14 +0100724 .set_rate = stm32_set_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800725};
726
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100727U_BOOT_DRIVER(stm32fx_clk) = {
Patrice Chotard03f10a12017-11-15 13:14:51 +0100728 .name = "stm32fx_rcc_clock",
Patrice Chotardb323de52017-09-21 10:08:09 +0200729 .id = UCLASS_CLK,
Patrice Chotardb323de52017-09-21 10:08:09 +0200730 .ops = &stm32_clk_ops,
731 .probe = stm32_clk_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700732 .priv_auto = sizeof(struct stm32_clk),
Patrice Chotardb323de52017-09-21 10:08:09 +0200733 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800734};