blob: 5e20b0cbb79512bd90d5f6bce7534904e165239f [file] [log] [blame]
Vikas Manocha1b51c932016-02-11 15:47:20 -08001/*
Vikas Manochadaaeaab2017-02-12 10:25:45 -08002 * (C) Copyright 2017
Vikas Manocha1b51c932016-02-11 15:47:20 -08003 * Vikas Manocha, <vikas.manocha@st.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
Vikas Manocha1b51c932016-02-11 15:47:20 -08007#include <common.h>
Vikas Manochadaaeaab2017-02-12 10:25:45 -08008#include <clk-uclass.h>
9#include <dm.h>
Vikas Manocha1b51c932016-02-11 15:47:20 -080010#include <asm/io.h>
11#include <asm/arch/rcc.h>
12#include <asm/arch/stm32.h>
13#include <asm/arch/stm32_periph.h>
14
Patrice Chotard7bdf9712017-07-18 09:29:05 +020015#include <dt-bindings/mfd/stm32f7-rcc.h>
16
Michael Kurzc204fb72017-01-22 16:04:24 +010017#define RCC_CR_HSION BIT(0)
18#define RCC_CR_HSEON BIT(16)
19#define RCC_CR_HSERDY BIT(17)
20#define RCC_CR_HSEBYP BIT(18)
21#define RCC_CR_CSSON BIT(19)
22#define RCC_CR_PLLON BIT(24)
23#define RCC_CR_PLLRDY BIT(25)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090024
Michael Kurzc204fb72017-01-22 16:04:24 +010025#define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
26#define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
27#define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
28#define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
29#define RCC_PLLCFGR_PLLSRC BIT(22)
30#define RCC_PLLCFGR_PLLM_SHIFT 0
31#define RCC_PLLCFGR_PLLN_SHIFT 6
32#define RCC_PLLCFGR_PLLP_SHIFT 16
33#define RCC_PLLCFGR_PLLQ_SHIFT 24
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090034
Michael Kurzc204fb72017-01-22 16:04:24 +010035#define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
36#define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
37#define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
38#define RCC_CFGR_SW0 BIT(0)
39#define RCC_CFGR_SW1 BIT(1)
40#define RCC_CFGR_SW_MASK GENMASK(1, 0)
41#define RCC_CFGR_SW_HSI 0
42#define RCC_CFGR_SW_HSE RCC_CFGR_SW0
43#define RCC_CFGR_SW_PLL RCC_CFGR_SW1
44#define RCC_CFGR_SWS0 BIT(2)
45#define RCC_CFGR_SWS1 BIT(3)
46#define RCC_CFGR_SWS_MASK GENMASK(3, 2)
47#define RCC_CFGR_SWS_HSI 0
48#define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
49#define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
50#define RCC_CFGR_HPRE_SHIFT 4
51#define RCC_CFGR_PPRE1_SHIFT 10
52#define RCC_CFGR_PPRE2_SHIFT 13
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090053
54/*
55 * Offsets of some PWR registers
56 */
Michael Kurzc204fb72017-01-22 16:04:24 +010057#define PWR_CR1_ODEN BIT(16)
58#define PWR_CR1_ODSWEN BIT(17)
59#define PWR_CSR1_ODRDY BIT(16)
60#define PWR_CSR1_ODSWRDY BIT(17)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090061
62struct pll_psc {
63 u8 pll_m;
64 u16 pll_n;
65 u8 pll_p;
66 u8 pll_q;
67 u8 ahb_psc;
68 u8 apb1_psc;
69 u8 apb2_psc;
70};
71
Michael Kurzc204fb72017-01-22 16:04:24 +010072#define AHB_PSC_1 0
73#define AHB_PSC_2 0x8
74#define AHB_PSC_4 0x9
75#define AHB_PSC_8 0xA
76#define AHB_PSC_16 0xB
77#define AHB_PSC_64 0xC
78#define AHB_PSC_128 0xD
79#define AHB_PSC_256 0xE
80#define AHB_PSC_512 0xF
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090081
Michael Kurzc204fb72017-01-22 16:04:24 +010082#define APB_PSC_1 0
83#define APB_PSC_2 0x4
84#define APB_PSC_4 0x5
85#define APB_PSC_8 0x6
86#define APB_PSC_16 0x7
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090087
Patrice Chotardd93fc2c2017-07-18 09:29:04 +020088struct stm32_clk {
89 struct stm32_rcc_regs *base;
90};
91
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090092#if !defined(CONFIG_STM32_HSE_HZ)
93#error "CONFIG_STM32_HSE_HZ not defined!"
94#else
95#if (CONFIG_STM32_HSE_HZ == 25000000)
96#if (CONFIG_SYS_CLK_FREQ == 200000000)
97/* 200 MHz */
98struct pll_psc sys_pll_psc = {
99 .pll_m = 25,
100 .pll_n = 400,
101 .pll_p = 2,
102 .pll_q = 8,
103 .ahb_psc = AHB_PSC_1,
104 .apb1_psc = APB_PSC_4,
105 .apb2_psc = APB_PSC_2
106};
107#endif
108#else
109#error "No PLL/Prescaler configuration for given CONFIG_STM32_HSE_HZ exists"
110#endif
111#endif
112
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200113static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900114{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200115 struct stm32_clk *priv = dev_get_priv(dev);
116 struct stm32_rcc_regs *regs = priv->base;
117
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900118 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200119 setbits_le32(&regs->cr, RCC_CR_HSION);
120 writel(0, &regs->cfgr); /* Reset CFGR */
121 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900122 | RCC_CR_PLLON));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200123 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
124 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
125 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900126
127 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200128 setbits_le32(&regs->cr, RCC_CR_HSEON);
129 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900130 ;
131
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200132 setbits_le32(&regs->cfgr, ((
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900133 sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
134 | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
135 | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
136
137 /* Configure the main PLL */
138 uint32_t pllcfgr = 0;
139 pllcfgr = RCC_PLLCFGR_PLLSRC; /* pll source HSE */
140 pllcfgr |= sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT;
141 pllcfgr |= sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT;
142 pllcfgr |= ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT;
143 pllcfgr |= sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200144 writel(pllcfgr, &regs->pllcfgr);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900145
146 /* Enable the main PLL */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200147 setbits_le32(&regs->cr, RCC_CR_PLLON);
148 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900149 ;
150
151 /* Enable high performance mode, System frequency up to 200 MHz */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200152 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900153 setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODEN);
154 /* Infinite wait! */
155 while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODRDY))
156 ;
157 /* Enable the Over-drive switch */
158 setbits_le32(&STM32_PWR->cr1, PWR_CR1_ODSWEN);
159 /* Infinite wait! */
160 while (!(readl(&STM32_PWR->csr1) & PWR_CSR1_ODSWRDY))
161 ;
162
163 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200164 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
165 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900166
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200167 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900168 RCC_CFGR_SWS_PLL)
169 ;
170
171 return 0;
172}
173
174unsigned long clock_get(enum clock clck)
175{
176 u32 sysclk = 0;
177 u32 shift = 0;
178 /* Prescaler table lookups for clock computation */
179 u8 ahb_psc_table[16] = {
180 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
181 };
182 u8 apb_psc_table[8] = {
183 0, 0, 0, 0, 1, 2, 3, 4
184 };
185
186 if ((readl(&STM32_RCC->cfgr) & RCC_CFGR_SWS_MASK) ==
187 RCC_CFGR_SWS_PLL) {
188 u16 pllm, plln, pllp;
189 pllm = (readl(&STM32_RCC->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
190 plln = ((readl(&STM32_RCC->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
191 >> RCC_PLLCFGR_PLLN_SHIFT);
192 pllp = ((((readl(&STM32_RCC->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
193 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
194 sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
195 }
196
197 switch (clck) {
198 case CLOCK_CORE:
199 return sysclk;
200 break;
201 case CLOCK_AHB:
202 shift = ahb_psc_table[(
203 (readl(&STM32_RCC->cfgr) & RCC_CFGR_AHB_PSC_MASK)
204 >> RCC_CFGR_HPRE_SHIFT)];
205 return sysclk >>= shift;
206 break;
207 case CLOCK_APB1:
208 shift = apb_psc_table[(
209 (readl(&STM32_RCC->cfgr) & RCC_CFGR_APB1_PSC_MASK)
210 >> RCC_CFGR_PPRE1_SHIFT)];
211 return sysclk >>= shift;
212 break;
213 case CLOCK_APB2:
214 shift = apb_psc_table[(
215 (readl(&STM32_RCC->cfgr) & RCC_CFGR_APB2_PSC_MASK)
216 >> RCC_CFGR_PPRE2_SHIFT)];
217 return sysclk >>= shift;
218 break;
219 default:
220 return 0;
221 break;
222 }
223}
224
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200225static unsigned long stm32_clk_get_rate(struct clk *clk)
226{
227 struct stm32_clk *priv = dev_get_priv(clk->dev);
228 struct stm32_rcc_regs *regs = priv->base;
229 u32 sysclk = 0;
230 u32 shift = 0;
231 /* Prescaler table lookups for clock computation */
232 u8 ahb_psc_table[16] = {
233 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
234 };
235 u8 apb_psc_table[8] = {
236 0, 0, 0, 0, 1, 2, 3, 4
237 };
238
239 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
240 RCC_CFGR_SWS_PLL) {
241 u16 pllm, plln, pllp;
242 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
243 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
244 >> RCC_PLLCFGR_PLLN_SHIFT);
245 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
246 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
247 sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
248 } else {
249 return -EINVAL;
250 }
251
252 switch (clk->id) {
253 /*
254 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
255 * AHB1, AHB2 and AHB3
256 */
257 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
258 shift = ahb_psc_table[(
259 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
260 >> RCC_CFGR_HPRE_SHIFT)];
261 return sysclk >>= shift;
262 break;
263 /* APB1 CLOCK */
264 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
265 shift = apb_psc_table[(
266 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
267 >> RCC_CFGR_PPRE1_SHIFT)];
268 return sysclk >>= shift;
269 break;
270 /* APB2 CLOCK */
271 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
272 shift = apb_psc_table[(
273 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
274 >> RCC_CFGR_PPRE2_SHIFT)];
275 return sysclk >>= shift;
276 break;
277 default:
278 error("clock index %ld out of range\n", clk->id);
279 return -EINVAL;
280 break;
281 }
282}
283
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800284static int stm32_clk_enable(struct clk *clk)
285{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200286 struct stm32_clk *priv = dev_get_priv(clk->dev);
287 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800288 u32 offset = clk->id / 32;
289 u32 bit_index = clk->id % 32;
290
291 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
292 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200293 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800294
295 return 0;
296}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900297
Vikas Manocha1b51c932016-02-11 15:47:20 -0800298void clock_setup(int peripheral)
299{
300 switch (peripheral) {
Michael Kurz04bb8db2017-01-22 16:04:26 +0100301 case SYSCFG_CLOCK_CFG:
302 setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
303 break;
304 case TIMER2_CLOCK_CFG:
305 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
306 break;
Michael Kurz812962b2017-01-22 16:04:27 +0100307 case STMMAC_CLOCK_CFG:
308 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
309 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
310 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
311 break;
Vikas Manocha1b51c932016-02-11 15:47:20 -0800312 default:
313 break;
314 }
315}
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800316
317static int stm32_clk_probe(struct udevice *dev)
318{
319 debug("%s: stm32_clk_probe\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200320
321 struct stm32_clk *priv = dev_get_priv(dev);
322 fdt_addr_t addr;
323
324 addr = devfdt_get_addr(dev);
325 if (addr == FDT_ADDR_T_NONE)
326 return -EINVAL;
327
328 priv->base = (struct stm32_rcc_regs *)addr;
329
330 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800331
332 return 0;
333}
334
Simon Glassb7ae2772017-05-18 20:09:40 -0600335static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800336{
337 debug("%s(clk=%p)\n", __func__, clk);
338
339 if (args->args_count != 2) {
340 debug("Invaild args_count: %d\n", args->args_count);
341 return -EINVAL;
342 }
343
344 if (args->args_count)
345 clk->id = args->args[1];
346 else
347 clk->id = 0;
348
349 return 0;
350}
351
352static struct clk_ops stm32_clk_ops = {
353 .of_xlate = stm32_clk_of_xlate,
354 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200355 .get_rate = stm32_clk_get_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800356};
357
358static const struct udevice_id stm32_clk_ids[] = {
359 { .compatible = "st,stm32f42xx-rcc"},
360 {}
361};
362
363U_BOOT_DRIVER(stm32f7_clk) = {
364 .name = "stm32f7_clk",
365 .id = UCLASS_CLK,
366 .of_match = stm32_clk_ids,
367 .ops = &stm32_clk_ops,
368 .probe = stm32_clk_probe,
369 .flags = DM_FLAG_PRE_RELOC,
370};