blob: 10f44859cda1ae169d3720b1abd40741b5100a2f [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 Chotard22768d52017-11-15 13:14:44 +010011
Vikas Manocha1b51c932016-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 Chotard22768d52017-11-15 13:14:44 +010016#include <asm/arch/stm32_pwr.h>
Vikas Manocha1b51c932016-02-11 15:47:20 -080017
Patrice Chotard7bdf9712017-07-18 09:29:05 +020018#include <dt-bindings/mfd/stm32f7-rcc.h>
19
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)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090027
Michael Kurzc204fb72017-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 NISHINAGA65bfb9c2016-07-08 01:02:24 +090037
Michael Kurzc204fb72017-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 NISHINAGA65bfb9c2016-07-08 01:02:24 +090056
Toshifumi NISHINAGA65bfb9c2016-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 Kurzc204fb72017-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 NISHINAGA65bfb9c2016-07-08 01:02:24 +090077
Michael Kurzc204fb72017-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 NISHINAGA65bfb9c2016-07-08 01:02:24 +090083
Patrice Chotard1509d662017-11-15 13:14:47 +010084struct stm32_clk_info {
85 struct pll_psc sys_pll_psc;
86 bool has_overdrive;
87};
88
89struct stm32_clk_info stm32f4_clk_info = {
90 /* 180 MHz */
91 .sys_pll_psc = {
92 .pll_m = 8,
93 .pll_n = 360,
94 .pll_p = 2,
95 .pll_q = 8,
96 .ahb_psc = AHB_PSC_1,
97 .apb1_psc = APB_PSC_4,
98 .apb2_psc = APB_PSC_2,
99 },
100 .has_overdrive = false,
101};
102
103struct stm32_clk_info stm32f7_clk_info = {
104 /* 200 MHz */
105 .sys_pll_psc = {
106 .pll_m = 25,
107 .pll_n = 400,
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 = true,
115};
116
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200117struct stm32_clk {
118 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100119 struct stm32_pwr_regs *pwr_regs;
Patrice Chotard1509d662017-11-15 13:14:47 +0100120 struct stm32_clk_info *info;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900121};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900122
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200123static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900124{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200125 struct stm32_clk *priv = dev_get_priv(dev);
126 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100127 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotard1509d662017-11-15 13:14:47 +0100128 struct pll_psc sys_pll_psc = priv->info->sys_pll_psc;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200129
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900130 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200131 setbits_le32(&regs->cr, RCC_CR_HSION);
132 writel(0, &regs->cfgr); /* Reset CFGR */
133 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900134 | RCC_CR_PLLON));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200135 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
136 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
137 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900138
139 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200140 setbits_le32(&regs->cr, RCC_CR_HSEON);
141 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900142 ;
143
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200144 setbits_le32(&regs->cfgr, ((
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900145 sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
146 | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
147 | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
148
149 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200150 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
151 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
152 sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT);
153 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
154 sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT);
155 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
156 ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
157 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
158 sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900159
160 /* Enable the main PLL */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200161 setbits_le32(&regs->cr, RCC_CR_PLLON);
162 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900163 ;
164
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200165 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100166
167 if (priv->info->has_overdrive) {
168 /*
169 * Enable high performance mode
170 * System frequency up to 200 MHz
171 */
172 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
173 /* Infinite wait! */
174 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
175 ;
176 /* Enable the Over-drive switch */
177 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
178 /* Infinite wait! */
179 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
180 ;
181 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900182
183 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200184 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
185 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900186
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200187 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900188 RCC_CFGR_SWS_PLL)
189 ;
190
191 return 0;
192}
193
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200194static unsigned long stm32_clk_get_rate(struct clk *clk)
195{
196 struct stm32_clk *priv = dev_get_priv(clk->dev);
197 struct stm32_rcc_regs *regs = priv->base;
198 u32 sysclk = 0;
199 u32 shift = 0;
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100200 u16 pllm, plln, pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200201 /* Prescaler table lookups for clock computation */
202 u8 ahb_psc_table[16] = {
203 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
204 };
205 u8 apb_psc_table[8] = {
206 0, 0, 0, 0, 1, 2, 3, 4
207 };
208
209 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
210 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200211 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
212 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
213 >> RCC_PLLCFGR_PLLN_SHIFT);
214 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
215 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
216 sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
217 } else {
218 return -EINVAL;
219 }
220
221 switch (clk->id) {
222 /*
223 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
224 * AHB1, AHB2 and AHB3
225 */
226 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
227 shift = ahb_psc_table[(
228 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
229 >> RCC_CFGR_HPRE_SHIFT)];
230 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200231 /* APB1 CLOCK */
232 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
233 shift = apb_psc_table[(
234 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
235 >> RCC_CFGR_PPRE1_SHIFT)];
236 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200237 /* APB2 CLOCK */
238 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
239 shift = apb_psc_table[(
240 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
241 >> RCC_CFGR_PPRE2_SHIFT)];
242 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200243 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900244 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200245 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200246 }
247}
248
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800249static int stm32_clk_enable(struct clk *clk)
250{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200251 struct stm32_clk *priv = dev_get_priv(clk->dev);
252 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800253 u32 offset = clk->id / 32;
254 u32 bit_index = clk->id % 32;
255
256 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
257 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200258 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800259
260 return 0;
261}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900262
Vikas Manocha1b51c932016-02-11 15:47:20 -0800263void clock_setup(int peripheral)
264{
265 switch (peripheral) {
Michael Kurz04bb8db2017-01-22 16:04:26 +0100266 case SYSCFG_CLOCK_CFG:
267 setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
268 break;
269 case TIMER2_CLOCK_CFG:
270 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
271 break;
Michael Kurz812962b2017-01-22 16:04:27 +0100272 case STMMAC_CLOCK_CFG:
273 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
274 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
275 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
276 break;
Vikas Manocha1b51c932016-02-11 15:47:20 -0800277 default:
278 break;
279 }
280}
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800281
282static int stm32_clk_probe(struct udevice *dev)
283{
Patrice Chotard22768d52017-11-15 13:14:44 +0100284 struct ofnode_phandle_args args;
285 int err;
286
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100287 debug("%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200288
289 struct stm32_clk *priv = dev_get_priv(dev);
290 fdt_addr_t addr;
291
Patrice Chotard1509d662017-11-15 13:14:47 +0100292 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200293 if (addr == FDT_ADDR_T_NONE)
294 return -EINVAL;
295
296 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard1509d662017-11-15 13:14:47 +0100297 priv->info = (struct stm32_clk_info *)dev_get_driver_data(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200298
Patrice Chotard1509d662017-11-15 13:14:47 +0100299 if (priv->info->has_overdrive) {
300 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
301 &args);
302 if (err) {
303 debug("%s: can't find syscon device (%d)\n", __func__,
304 err);
305 return err;
306 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100307
Patrice Chotard1509d662017-11-15 13:14:47 +0100308 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
309 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100310
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200311 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800312
313 return 0;
314}
315
Simon Glassb7ae2772017-05-18 20:09:40 -0600316static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800317{
318 debug("%s(clk=%p)\n", __func__, clk);
319
320 if (args->args_count != 2) {
321 debug("Invaild args_count: %d\n", args->args_count);
322 return -EINVAL;
323 }
324
325 if (args->args_count)
326 clk->id = args->args[1];
327 else
328 clk->id = 0;
329
330 return 0;
331}
332
333static struct clk_ops stm32_clk_ops = {
334 .of_xlate = stm32_clk_of_xlate,
335 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200336 .get_rate = stm32_clk_get_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800337};
338
339static const struct udevice_id stm32_clk_ids[] = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100340 { .compatible = "st,stm32f42xx-rcc", .data = (ulong)&stm32f4_clk_info},
341 { .compatible = "st,stm32f746-rcc", .data = (ulong)&stm32f7_clk_info},
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800342 {}
343};
344
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100345U_BOOT_DRIVER(stm32fx_clk) = {
346 .name = "stm32fx_clk",
Patrice Chotardb323de52017-09-21 10:08:09 +0200347 .id = UCLASS_CLK,
348 .of_match = stm32_clk_ids,
349 .ops = &stm32_clk_ops,
350 .probe = stm32_clk_probe,
351 .priv_auto_alloc_size = sizeof(struct stm32_clk),
352 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800353};