blob: 0fa838c4ee51a67fdaa2793607bbf7bcc46c1583 [file] [log] [blame]
Vikas Manochae66c49f2016-02-11 15:47:20 -08001/*
Patrice Chotard3bc599c2017-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 Manochae66c49f2016-02-11 15:47:20 -08004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
Patrice Chotard3bc599c2017-10-23 09:53:58 +02007
Vikas Manochae66c49f2016-02-11 15:47:20 -08008#include <common.h>
Vikas Manocha712f99a2017-02-12 10:25:45 -08009#include <clk-uclass.h>
10#include <dm.h>
Patrice Chotardd0a768b2017-11-15 13:14:44 +010011
Vikas Manochae66c49f2016-02-11 15:47:20 -080012#include <asm/io.h>
13#include <asm/arch/rcc.h>
14#include <asm/arch/stm32.h>
15#include <asm/arch/stm32_periph.h>
Patrice Chotardd0a768b2017-11-15 13:14:44 +010016#include <asm/arch/stm32_pwr.h>
Vikas Manochae66c49f2016-02-11 15:47:20 -080017
Patrice Chotard288f17e2017-07-18 09:29:05 +020018#include <dt-bindings/mfd/stm32f7-rcc.h>
19
Michael Kurzbad51882017-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)
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090027
Michael Kurzbad51882017-01-22 16:04:24 +010028#define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
29#define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
30#define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
31#define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
32#define RCC_PLLCFGR_PLLSRC BIT(22)
33#define RCC_PLLCFGR_PLLM_SHIFT 0
34#define RCC_PLLCFGR_PLLN_SHIFT 6
35#define RCC_PLLCFGR_PLLP_SHIFT 16
36#define RCC_PLLCFGR_PLLQ_SHIFT 24
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090037
Michael Kurzbad51882017-01-22 16:04:24 +010038#define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
39#define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
40#define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
41#define RCC_CFGR_SW0 BIT(0)
42#define RCC_CFGR_SW1 BIT(1)
43#define RCC_CFGR_SW_MASK GENMASK(1, 0)
44#define RCC_CFGR_SW_HSI 0
45#define RCC_CFGR_SW_HSE RCC_CFGR_SW0
46#define RCC_CFGR_SW_PLL RCC_CFGR_SW1
47#define RCC_CFGR_SWS0 BIT(2)
48#define RCC_CFGR_SWS1 BIT(3)
49#define RCC_CFGR_SWS_MASK GENMASK(3, 2)
50#define RCC_CFGR_SWS_HSI 0
51#define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
52#define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
53#define RCC_CFGR_HPRE_SHIFT 4
54#define RCC_CFGR_PPRE1_SHIFT 10
55#define RCC_CFGR_PPRE2_SHIFT 13
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090056
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090057
58struct pll_psc {
59 u8 pll_m;
60 u16 pll_n;
61 u8 pll_p;
62 u8 pll_q;
63 u8 ahb_psc;
64 u8 apb1_psc;
65 u8 apb2_psc;
66};
67
Michael Kurzbad51882017-01-22 16:04:24 +010068#define AHB_PSC_1 0
69#define AHB_PSC_2 0x8
70#define AHB_PSC_4 0x9
71#define AHB_PSC_8 0xA
72#define AHB_PSC_16 0xB
73#define AHB_PSC_64 0xC
74#define AHB_PSC_128 0xD
75#define AHB_PSC_256 0xE
76#define AHB_PSC_512 0xF
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090077
Michael Kurzbad51882017-01-22 16:04:24 +010078#define APB_PSC_1 0
79#define APB_PSC_2 0x4
80#define APB_PSC_4 0x5
81#define APB_PSC_8 0x6
82#define APB_PSC_16 0x7
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090083
Patrice Chotard199a2172017-07-18 09:29:04 +020084struct stm32_clk {
85 struct stm32_rcc_regs *base;
Patrice Chotardd0a768b2017-11-15 13:14:44 +010086 struct stm32_pwr_regs *pwr_regs;
Patrice Chotard199a2172017-07-18 09:29:04 +020087};
88
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090089#if !defined(CONFIG_STM32_HSE_HZ)
90#error "CONFIG_STM32_HSE_HZ not defined!"
91#else
92#if (CONFIG_STM32_HSE_HZ == 25000000)
93#if (CONFIG_SYS_CLK_FREQ == 200000000)
94/* 200 MHz */
95struct pll_psc sys_pll_psc = {
96 .pll_m = 25,
97 .pll_n = 400,
98 .pll_p = 2,
99 .pll_q = 8,
100 .ahb_psc = AHB_PSC_1,
101 .apb1_psc = APB_PSC_4,
102 .apb2_psc = APB_PSC_2
103};
104#endif
105#else
106#error "No PLL/Prescaler configuration for given CONFIG_STM32_HSE_HZ exists"
107#endif
108#endif
109
Patrice Chotard199a2172017-07-18 09:29:04 +0200110static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900111{
Patrice Chotard199a2172017-07-18 09:29:04 +0200112 struct stm32_clk *priv = dev_get_priv(dev);
113 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100114 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotard199a2172017-07-18 09:29:04 +0200115
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900116 /* Reset RCC configuration */
Patrice Chotard199a2172017-07-18 09:29:04 +0200117 setbits_le32(&regs->cr, RCC_CR_HSION);
118 writel(0, &regs->cfgr); /* Reset CFGR */
119 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900120 | RCC_CR_PLLON));
Patrice Chotard199a2172017-07-18 09:29:04 +0200121 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
122 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
123 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900124
125 /* Configure for HSE+PLL operation */
Patrice Chotard199a2172017-07-18 09:29:04 +0200126 setbits_le32(&regs->cr, RCC_CR_HSEON);
127 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900128 ;
129
Patrice Chotard199a2172017-07-18 09:29:04 +0200130 setbits_le32(&regs->cfgr, ((
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900131 sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
132 | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
133 | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
134
135 /* Configure the main PLL */
Patrice Chotard1543bf72017-10-26 13:23:19 +0200136 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
137 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
138 sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT);
139 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
140 sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT);
141 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
142 ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
143 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
144 sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900145
146 /* Enable the main PLL */
Patrice Chotard199a2172017-07-18 09:29:04 +0200147 setbits_le32(&regs->cr, RCC_CR_PLLON);
148 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900149 ;
150
151 /* Enable high performance mode, System frequency up to 200 MHz */
Patrice Chotard199a2172017-07-18 09:29:04 +0200152 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100153 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900154 /* Infinite wait! */
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100155 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900156 ;
157 /* Enable the Over-drive switch */
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100158 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900159 /* Infinite wait! */
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100160 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900161 ;
162
163 stm32_flash_latency_cfg(5);
Patrice Chotard199a2172017-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 NISHINAGAba0a3c12016-07-08 01:02:24 +0900166
Patrice Chotard199a2172017-07-18 09:29:04 +0200167 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900168 RCC_CFGR_SWS_PLL)
169 ;
170
171 return 0;
172}
173
Patrice Chotard288f17e2017-07-18 09:29:05 +0200174static unsigned long stm32_clk_get_rate(struct clk *clk)
175{
176 struct stm32_clk *priv = dev_get_priv(clk->dev);
177 struct stm32_rcc_regs *regs = priv->base;
178 u32 sysclk = 0;
179 u32 shift = 0;
180 /* Prescaler table lookups for clock computation */
181 u8 ahb_psc_table[16] = {
182 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
183 };
184 u8 apb_psc_table[8] = {
185 0, 0, 0, 0, 1, 2, 3, 4
186 };
187
188 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
189 RCC_CFGR_SWS_PLL) {
190 u16 pllm, plln, pllp;
191 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
192 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
193 >> RCC_PLLCFGR_PLLN_SHIFT);
194 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
195 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
196 sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
197 } else {
198 return -EINVAL;
199 }
200
201 switch (clk->id) {
202 /*
203 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
204 * AHB1, AHB2 and AHB3
205 */
206 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
207 shift = ahb_psc_table[(
208 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
209 >> RCC_CFGR_HPRE_SHIFT)];
210 return sysclk >>= shift;
211 break;
212 /* APB1 CLOCK */
213 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
214 shift = apb_psc_table[(
215 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
216 >> RCC_CFGR_PPRE1_SHIFT)];
217 return sysclk >>= shift;
218 break;
219 /* APB2 CLOCK */
220 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
221 shift = apb_psc_table[(
222 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
223 >> RCC_CFGR_PPRE2_SHIFT)];
224 return sysclk >>= shift;
225 break;
226 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900227 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard288f17e2017-07-18 09:29:05 +0200228 return -EINVAL;
229 break;
230 }
231}
232
Vikas Manocha712f99a2017-02-12 10:25:45 -0800233static int stm32_clk_enable(struct clk *clk)
234{
Patrice Chotard199a2172017-07-18 09:29:04 +0200235 struct stm32_clk *priv = dev_get_priv(clk->dev);
236 struct stm32_rcc_regs *regs = priv->base;
Vikas Manocha712f99a2017-02-12 10:25:45 -0800237 u32 offset = clk->id / 32;
238 u32 bit_index = clk->id % 32;
239
240 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
241 __func__, clk->id, offset, bit_index);
Patrice Chotard199a2172017-07-18 09:29:04 +0200242 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manocha712f99a2017-02-12 10:25:45 -0800243
244 return 0;
245}
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +0900246
Vikas Manochae66c49f2016-02-11 15:47:20 -0800247void clock_setup(int peripheral)
248{
249 switch (peripheral) {
Michael Kurz081de092017-01-22 16:04:26 +0100250 case SYSCFG_CLOCK_CFG:
251 setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
252 break;
253 case TIMER2_CLOCK_CFG:
254 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
255 break;
Michael Kurzb20b70f2017-01-22 16:04:27 +0100256 case STMMAC_CLOCK_CFG:
257 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
258 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
259 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
260 break;
Vikas Manochae66c49f2016-02-11 15:47:20 -0800261 default:
262 break;
263 }
264}
Vikas Manocha712f99a2017-02-12 10:25:45 -0800265
266static int stm32_clk_probe(struct udevice *dev)
267{
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100268 struct ofnode_phandle_args args;
269 int err;
270
Vikas Manocha712f99a2017-02-12 10:25:45 -0800271 debug("%s: stm32_clk_probe\n", __func__);
Patrice Chotard199a2172017-07-18 09:29:04 +0200272
273 struct stm32_clk *priv = dev_get_priv(dev);
274 fdt_addr_t addr;
275
276 addr = devfdt_get_addr(dev);
277 if (addr == FDT_ADDR_T_NONE)
278 return -EINVAL;
279
280 priv->base = (struct stm32_rcc_regs *)addr;
281
Patrice Chotardd0a768b2017-11-15 13:14:44 +0100282 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
283 &args);
284 if (err) {
285 debug("%s: can't find syscon device (%d)\n", __func__,
286 err);
287 return err;
288 }
289
290 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
291
Patrice Chotard199a2172017-07-18 09:29:04 +0200292 configure_clocks(dev);
Vikas Manocha712f99a2017-02-12 10:25:45 -0800293
294 return 0;
295}
296
Simon Glassa4e0ef52017-05-18 20:09:40 -0600297static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manocha712f99a2017-02-12 10:25:45 -0800298{
299 debug("%s(clk=%p)\n", __func__, clk);
300
301 if (args->args_count != 2) {
302 debug("Invaild args_count: %d\n", args->args_count);
303 return -EINVAL;
304 }
305
306 if (args->args_count)
307 clk->id = args->args[1];
308 else
309 clk->id = 0;
310
311 return 0;
312}
313
314static struct clk_ops stm32_clk_ops = {
315 .of_xlate = stm32_clk_of_xlate,
316 .enable = stm32_clk_enable,
Patrice Chotard288f17e2017-07-18 09:29:05 +0200317 .get_rate = stm32_clk_get_rate,
Vikas Manocha712f99a2017-02-12 10:25:45 -0800318};
319
320static const struct udevice_id stm32_clk_ids[] = {
321 { .compatible = "st,stm32f42xx-rcc"},
322 {}
323};
324
325U_BOOT_DRIVER(stm32f7_clk) = {
Patrice Chotard0cc40df2017-09-21 10:08:09 +0200326 .name = "stm32f7_clk",
327 .id = UCLASS_CLK,
328 .of_match = stm32_clk_ids,
329 .ops = &stm32_clk_ops,
330 .probe = stm32_clk_probe,
331 .priv_auto_alloc_size = sizeof(struct stm32_clk),
332 .flags = DM_FLAG_PRE_RELOC,
Vikas Manocha712f99a2017-02-12 10:25:45 -0800333};