blob: c7af7a1d8d1e610400880ae13f5c5469f9aee251 [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>
Vikas Manocha1b51c932016-02-11 15:47:20 -080013#include <asm/arch/stm32.h>
14#include <asm/arch/stm32_periph.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)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090026
Michael Kurzc204fb72017-01-22 16:04:24 +010027#define RCC_PLLCFGR_PLLM_MASK GENMASK(5, 0)
28#define RCC_PLLCFGR_PLLN_MASK GENMASK(14, 6)
29#define RCC_PLLCFGR_PLLP_MASK GENMASK(17, 16)
30#define RCC_PLLCFGR_PLLQ_MASK GENMASK(27, 24)
31#define RCC_PLLCFGR_PLLSRC BIT(22)
32#define RCC_PLLCFGR_PLLM_SHIFT 0
33#define RCC_PLLCFGR_PLLN_SHIFT 6
34#define RCC_PLLCFGR_PLLP_SHIFT 16
35#define RCC_PLLCFGR_PLLQ_SHIFT 24
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090036
Michael Kurzc204fb72017-01-22 16:04:24 +010037#define RCC_CFGR_AHB_PSC_MASK GENMASK(7, 4)
38#define RCC_CFGR_APB1_PSC_MASK GENMASK(12, 10)
39#define RCC_CFGR_APB2_PSC_MASK GENMASK(15, 13)
40#define RCC_CFGR_SW0 BIT(0)
41#define RCC_CFGR_SW1 BIT(1)
42#define RCC_CFGR_SW_MASK GENMASK(1, 0)
43#define RCC_CFGR_SW_HSI 0
44#define RCC_CFGR_SW_HSE RCC_CFGR_SW0
45#define RCC_CFGR_SW_PLL RCC_CFGR_SW1
46#define RCC_CFGR_SWS0 BIT(2)
47#define RCC_CFGR_SWS1 BIT(3)
48#define RCC_CFGR_SWS_MASK GENMASK(3, 2)
49#define RCC_CFGR_SWS_HSI 0
50#define RCC_CFGR_SWS_HSE RCC_CFGR_SWS0
51#define RCC_CFGR_SWS_PLL RCC_CFGR_SWS1
52#define RCC_CFGR_HPRE_SHIFT 4
53#define RCC_CFGR_PPRE1_SHIFT 10
54#define RCC_CFGR_PPRE2_SHIFT 13
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090055
Patrice Chotard06fc6482017-11-15 13:14:49 +010056/*
57 * RCC AHB1ENR specific definitions
58 */
59#define RCC_AHB1ENR_ETHMAC_EN BIT(25)
60#define RCC_AHB1ENR_ETHMAC_TX_EN BIT(26)
61#define RCC_AHB1ENR_ETHMAC_RX_EN BIT(27)
62
63/*
64 * RCC APB1ENR specific definitions
65 */
66#define RCC_APB1ENR_TIM2EN BIT(0)
67#define RCC_APB1ENR_PWREN BIT(28)
68
69/*
70 * RCC APB2ENR specific definitions
71 */
72#define RCC_APB2ENR_SYSCFGEN BIT(14)
73
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090074
75struct pll_psc {
76 u8 pll_m;
77 u16 pll_n;
78 u8 pll_p;
79 u8 pll_q;
80 u8 ahb_psc;
81 u8 apb1_psc;
82 u8 apb2_psc;
83};
84
Michael Kurzc204fb72017-01-22 16:04:24 +010085#define AHB_PSC_1 0
86#define AHB_PSC_2 0x8
87#define AHB_PSC_4 0x9
88#define AHB_PSC_8 0xA
89#define AHB_PSC_16 0xB
90#define AHB_PSC_64 0xC
91#define AHB_PSC_128 0xD
92#define AHB_PSC_256 0xE
93#define AHB_PSC_512 0xF
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090094
Michael Kurzc204fb72017-01-22 16:04:24 +010095#define APB_PSC_1 0
96#define APB_PSC_2 0x4
97#define APB_PSC_4 0x5
98#define APB_PSC_8 0x6
99#define APB_PSC_16 0x7
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900100
Patrice Chotard1509d662017-11-15 13:14:47 +0100101struct stm32_clk_info {
102 struct pll_psc sys_pll_psc;
103 bool has_overdrive;
104};
105
106struct stm32_clk_info stm32f4_clk_info = {
107 /* 180 MHz */
108 .sys_pll_psc = {
109 .pll_m = 8,
110 .pll_n = 360,
111 .pll_p = 2,
112 .pll_q = 8,
113 .ahb_psc = AHB_PSC_1,
114 .apb1_psc = APB_PSC_4,
115 .apb2_psc = APB_PSC_2,
116 },
117 .has_overdrive = false,
118};
119
120struct stm32_clk_info stm32f7_clk_info = {
121 /* 200 MHz */
122 .sys_pll_psc = {
123 .pll_m = 25,
124 .pll_n = 400,
125 .pll_p = 2,
126 .pll_q = 8,
127 .ahb_psc = AHB_PSC_1,
128 .apb1_psc = APB_PSC_4,
129 .apb2_psc = APB_PSC_2,
130 },
131 .has_overdrive = true,
132};
133
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200134struct stm32_clk {
135 struct stm32_rcc_regs *base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100136 struct stm32_pwr_regs *pwr_regs;
Patrice Chotard1509d662017-11-15 13:14:47 +0100137 struct stm32_clk_info *info;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900138};
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900139
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200140static int configure_clocks(struct udevice *dev)
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900141{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200142 struct stm32_clk *priv = dev_get_priv(dev);
143 struct stm32_rcc_regs *regs = priv->base;
Patrice Chotard22768d52017-11-15 13:14:44 +0100144 struct stm32_pwr_regs *pwr = priv->pwr_regs;
Patrice Chotard1509d662017-11-15 13:14:47 +0100145 struct pll_psc sys_pll_psc = priv->info->sys_pll_psc;
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200146
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900147 /* Reset RCC configuration */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200148 setbits_le32(&regs->cr, RCC_CR_HSION);
149 writel(0, &regs->cfgr); /* Reset CFGR */
150 clrbits_le32(&regs->cr, (RCC_CR_HSEON | RCC_CR_CSSON
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900151 | RCC_CR_PLLON));
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200152 writel(0x24003010, &regs->pllcfgr); /* Reset value from RM */
153 clrbits_le32(&regs->cr, RCC_CR_HSEBYP);
154 writel(0, &regs->cir); /* Disable all interrupts */
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900155
156 /* Configure for HSE+PLL operation */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200157 setbits_le32(&regs->cr, RCC_CR_HSEON);
158 while (!(readl(&regs->cr) & RCC_CR_HSERDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900159 ;
160
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200161 setbits_le32(&regs->cfgr, ((
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900162 sys_pll_psc.ahb_psc << RCC_CFGR_HPRE_SHIFT)
163 | (sys_pll_psc.apb1_psc << RCC_CFGR_PPRE1_SHIFT)
164 | (sys_pll_psc.apb2_psc << RCC_CFGR_PPRE2_SHIFT)));
165
166 /* Configure the main PLL */
Patrice Chotardb6653f62017-10-26 13:23:19 +0200167 setbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLSRC); /* pll source HSE */
168 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLM_MASK,
169 sys_pll_psc.pll_m << RCC_PLLCFGR_PLLM_SHIFT);
170 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLN_MASK,
171 sys_pll_psc.pll_n << RCC_PLLCFGR_PLLN_SHIFT);
172 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLP_MASK,
173 ((sys_pll_psc.pll_p >> 1) - 1) << RCC_PLLCFGR_PLLP_SHIFT);
174 clrsetbits_le32(&regs->pllcfgr, RCC_PLLCFGR_PLLQ_MASK,
175 sys_pll_psc.pll_q << RCC_PLLCFGR_PLLQ_SHIFT);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900176
177 /* Enable the main PLL */
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200178 setbits_le32(&regs->cr, RCC_CR_PLLON);
179 while (!(readl(&regs->cr) & RCC_CR_PLLRDY))
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900180 ;
181
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200182 setbits_le32(&regs->apb1enr, RCC_APB1ENR_PWREN);
Patrice Chotard1509d662017-11-15 13:14:47 +0100183
184 if (priv->info->has_overdrive) {
185 /*
186 * Enable high performance mode
187 * System frequency up to 200 MHz
188 */
189 setbits_le32(&pwr->cr1, PWR_CR1_ODEN);
190 /* Infinite wait! */
191 while (!(readl(&pwr->csr1) & PWR_CSR1_ODRDY))
192 ;
193 /* Enable the Over-drive switch */
194 setbits_le32(&pwr->cr1, PWR_CR1_ODSWEN);
195 /* Infinite wait! */
196 while (!(readl(&pwr->csr1) & PWR_CSR1_ODSWRDY))
197 ;
198 }
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900199
200 stm32_flash_latency_cfg(5);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200201 clrbits_le32(&regs->cfgr, (RCC_CFGR_SW0 | RCC_CFGR_SW1));
202 setbits_le32(&regs->cfgr, RCC_CFGR_SW_PLL);
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900203
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200204 while ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) !=
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900205 RCC_CFGR_SWS_PLL)
206 ;
207
208 return 0;
209}
210
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200211static unsigned long stm32_clk_get_rate(struct clk *clk)
212{
213 struct stm32_clk *priv = dev_get_priv(clk->dev);
214 struct stm32_rcc_regs *regs = priv->base;
215 u32 sysclk = 0;
216 u32 shift = 0;
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100217 u16 pllm, plln, pllp;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200218 /* Prescaler table lookups for clock computation */
219 u8 ahb_psc_table[16] = {
220 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9
221 };
222 u8 apb_psc_table[8] = {
223 0, 0, 0, 0, 1, 2, 3, 4
224 };
225
226 if ((readl(&regs->cfgr) & RCC_CFGR_SWS_MASK) ==
227 RCC_CFGR_SWS_PLL) {
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200228 pllm = (readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLM_MASK);
229 plln = ((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLN_MASK)
230 >> RCC_PLLCFGR_PLLN_SHIFT);
231 pllp = ((((readl(&regs->pllcfgr) & RCC_PLLCFGR_PLLP_MASK)
232 >> RCC_PLLCFGR_PLLP_SHIFT) + 1) << 1);
233 sysclk = ((CONFIG_STM32_HSE_HZ / pllm) * plln) / pllp;
234 } else {
235 return -EINVAL;
236 }
237
238 switch (clk->id) {
239 /*
240 * AHB CLOCK: 3 x 32 bits consecutive registers are used :
241 * AHB1, AHB2 and AHB3
242 */
243 case STM32F7_AHB1_CLOCK(GPIOA) ... STM32F7_AHB3_CLOCK(QSPI):
244 shift = ahb_psc_table[(
245 (readl(&regs->cfgr) & RCC_CFGR_AHB_PSC_MASK)
246 >> RCC_CFGR_HPRE_SHIFT)];
247 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200248 /* APB1 CLOCK */
249 case STM32F7_APB1_CLOCK(TIM2) ... STM32F7_APB1_CLOCK(UART8):
250 shift = apb_psc_table[(
251 (readl(&regs->cfgr) & RCC_CFGR_APB1_PSC_MASK)
252 >> RCC_CFGR_PPRE1_SHIFT)];
253 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200254 /* APB2 CLOCK */
255 case STM32F7_APB2_CLOCK(TIM1) ... STM32F7_APB2_CLOCK(LTDC):
256 shift = apb_psc_table[(
257 (readl(&regs->cfgr) & RCC_CFGR_APB2_PSC_MASK)
258 >> RCC_CFGR_PPRE2_SHIFT)];
259 return sysclk >>= shift;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200260 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900261 pr_err("clock index %ld out of range\n", clk->id);
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200262 return -EINVAL;
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200263 }
264}
265
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800266static int stm32_clk_enable(struct clk *clk)
267{
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200268 struct stm32_clk *priv = dev_get_priv(clk->dev);
269 struct stm32_rcc_regs *regs = priv->base;
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800270 u32 offset = clk->id / 32;
271 u32 bit_index = clk->id % 32;
272
273 debug("%s: clkid = %ld, offset from AHB1ENR is %d, bit_index = %d\n",
274 __func__, clk->id, offset, bit_index);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200275 setbits_le32(&regs->ahb1enr + offset, BIT(bit_index));
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800276
277 return 0;
278}
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +0900279
Vikas Manocha1b51c932016-02-11 15:47:20 -0800280void clock_setup(int peripheral)
281{
282 switch (peripheral) {
Michael Kurz04bb8db2017-01-22 16:04:26 +0100283 case SYSCFG_CLOCK_CFG:
284 setbits_le32(&STM32_RCC->apb2enr, RCC_APB2ENR_SYSCFGEN);
285 break;
286 case TIMER2_CLOCK_CFG:
287 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
288 break;
Michael Kurz812962b2017-01-22 16:04:27 +0100289 case STMMAC_CLOCK_CFG:
290 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_EN);
291 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_RX_EN);
292 setbits_le32(&STM32_RCC->ahb1enr, RCC_AHB1ENR_ETHMAC_TX_EN);
293 break;
Vikas Manocha1b51c932016-02-11 15:47:20 -0800294 default:
295 break;
296 }
297}
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800298
299static int stm32_clk_probe(struct udevice *dev)
300{
Patrice Chotard22768d52017-11-15 13:14:44 +0100301 struct ofnode_phandle_args args;
302 int err;
303
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100304 debug("%s\n", __func__);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200305
306 struct stm32_clk *priv = dev_get_priv(dev);
307 fdt_addr_t addr;
308
Patrice Chotard1509d662017-11-15 13:14:47 +0100309 addr = dev_read_addr(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200310 if (addr == FDT_ADDR_T_NONE)
311 return -EINVAL;
312
313 priv->base = (struct stm32_rcc_regs *)addr;
Patrice Chotard1509d662017-11-15 13:14:47 +0100314 priv->info = (struct stm32_clk_info *)dev_get_driver_data(dev);
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200315
Patrice Chotard1509d662017-11-15 13:14:47 +0100316 if (priv->info->has_overdrive) {
317 err = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
318 &args);
319 if (err) {
320 debug("%s: can't find syscon device (%d)\n", __func__,
321 err);
322 return err;
323 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100324
Patrice Chotard1509d662017-11-15 13:14:47 +0100325 priv->pwr_regs = (struct stm32_pwr_regs *)ofnode_get_addr(args.node);
326 }
Patrice Chotard22768d52017-11-15 13:14:44 +0100327
Patrice Chotardd93fc2c2017-07-18 09:29:04 +0200328 configure_clocks(dev);
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800329
330 return 0;
331}
332
Simon Glassb7ae2772017-05-18 20:09:40 -0600333static int stm32_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800334{
335 debug("%s(clk=%p)\n", __func__, clk);
336
337 if (args->args_count != 2) {
338 debug("Invaild args_count: %d\n", args->args_count);
339 return -EINVAL;
340 }
341
342 if (args->args_count)
343 clk->id = args->args[1];
344 else
345 clk->id = 0;
346
347 return 0;
348}
349
350static struct clk_ops stm32_clk_ops = {
351 .of_xlate = stm32_clk_of_xlate,
352 .enable = stm32_clk_enable,
Patrice Chotard7bdf9712017-07-18 09:29:05 +0200353 .get_rate = stm32_clk_get_rate,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800354};
355
356static const struct udevice_id stm32_clk_ids[] = {
Patrice Chotard1509d662017-11-15 13:14:47 +0100357 { .compatible = "st,stm32f42xx-rcc", .data = (ulong)&stm32f4_clk_info},
358 { .compatible = "st,stm32f746-rcc", .data = (ulong)&stm32f7_clk_info},
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800359 {}
360};
361
Patrice Chotardd4f2d202017-11-15 13:14:48 +0100362U_BOOT_DRIVER(stm32fx_clk) = {
363 .name = "stm32fx_clk",
Patrice Chotardb323de52017-09-21 10:08:09 +0200364 .id = UCLASS_CLK,
365 .of_match = stm32_clk_ids,
366 .ops = &stm32_clk_ops,
367 .probe = stm32_clk_probe,
368 .priv_auto_alloc_size = sizeof(struct stm32_clk),
369 .flags = DM_FLAG_PRE_RELOC,
Vikas Manochadaaeaab2017-02-12 10:25:45 -0800370};