blob: 241de6a5378837dd91ea6379da975706bb75f6e6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +02002/*
3 * [origin: Linux kernel linux/arch/arm/mach-at91/clock.c]
4 *
5 * Copyright (C) 2005 David Brownell
6 * Copyright (C) 2005 Ivan Kokshaysky
7 * Copyright (C) 2009 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +02008 */
9
Tom Rinidd8c4a12024-04-30 07:35:55 -060010#include <config.h>
11#include <time.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Reinhard Meyer86592f62010-11-07 13:26:14 +010013#include <asm/io.h>
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020014#include <asm/arch/hardware.h>
15#include <asm/arch/at91_pmc.h>
16#include <asm/arch/clk.h>
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020017
Reinhard Meyer5dca7102010-10-05 16:54:35 +020018#if !defined(CONFIG_AT91FAMILY)
19# error You need to define CONFIG_AT91FAMILY in your board config!
20#endif
21
Wenyou Yangbe5e4852016-02-03 10:20:43 +080022#define EN_PLLB_TIMEOUT 500
23
Reinhard Meyer5dca7102010-10-05 16:54:35 +020024DECLARE_GLOBAL_DATA_PTR;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020025
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020026static unsigned long at91_css_to_rate(unsigned long css)
27{
28 switch (css) {
Jens Scharsig0cf0b932010-02-03 22:46:58 +010029 case AT91_PMC_MCKR_CSS_SLOW:
Tom Rini65cc0e22022-11-16 13:10:41 -050030 return CFG_SYS_AT91_SLOW_CLOCK;
Jens Scharsig0cf0b932010-02-03 22:46:58 +010031 case AT91_PMC_MCKR_CSS_MAIN:
Simon Glassf47e6ec2012-12-13 20:48:31 +000032 return gd->arch.main_clk_rate_hz;
Jens Scharsig0cf0b932010-02-03 22:46:58 +010033 case AT91_PMC_MCKR_CSS_PLLA:
Simon Glassf47e6ec2012-12-13 20:48:31 +000034 return gd->arch.plla_rate_hz;
Jens Scharsig0cf0b932010-02-03 22:46:58 +010035 case AT91_PMC_MCKR_CSS_PLLB:
Simon Glassf47e6ec2012-12-13 20:48:31 +000036 return gd->arch.pllb_rate_hz;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020037 }
38
39 return 0;
40}
41
42#ifdef CONFIG_USB_ATMEL
43static unsigned at91_pll_calc(unsigned main_freq, unsigned out_freq)
44{
45 unsigned i, div = 0, mul = 0, diff = 1 << 30;
46 unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
47
48 /* PLL output max 240 MHz (or 180 MHz per errata) */
49 if (out_freq > 240000000)
50 goto fail;
51
52 for (i = 1; i < 256; i++) {
53 int diff1;
54 unsigned input, mul1;
55
56 /*
57 * PLL input between 1MHz and 32MHz per spec, but lower
58 * frequences seem necessary in some cases so allow 100K.
59 * Warning: some newer products need 2MHz min.
60 */
61 input = main_freq / i;
62#if defined(CONFIG_AT91SAM9G20)
63 if (input < 2000000)
64 continue;
65#endif
66 if (input < 100000)
67 continue;
68 if (input > 32000000)
69 continue;
70
71 mul1 = out_freq / input;
72#if defined(CONFIG_AT91SAM9G20)
73 if (mul > 63)
74 continue;
75#endif
76 if (mul1 > 2048)
77 continue;
78 if (mul1 < 2)
79 goto fail;
80
81 diff1 = out_freq - input * mul1;
82 if (diff1 < 0)
83 diff1 = -diff1;
84 if (diff > diff1) {
85 diff = diff1;
86 div = i;
87 mul = mul1;
88 if (diff == 0)
89 break;
90 }
91 }
92 if (i == 256 && diff > (out_freq >> 5))
93 goto fail;
94 return ret | ((mul - 1) << 16) | div;
95fail:
96 return 0;
97}
Daniel Gorsulowskia1e5f932009-04-23 15:37:16 +020098#endif
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020099
100static u32 at91_pll_rate(u32 freq, u32 reg)
101{
102 unsigned mul, div;
103
104 div = reg & 0xff;
105 mul = (reg >> 16) & 0x7ff;
106 if (div && mul) {
107 freq /= div;
108 freq *= mul + 1;
109 } else
110 freq = 0;
111
112 return freq;
113}
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200114
115int at91_clock_init(unsigned long main_clock)
116{
117 unsigned freq, mckr;
Reinhard Meyer9f3fe902010-11-03 15:39:55 +0100118 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
Tom Rini65cc0e22022-11-16 13:10:41 -0500119#ifndef CFG_SYS_AT91_MAIN_CLOCK
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200120 unsigned tmp;
121 /*
122 * When the bootloader initialized the main oscillator correctly,
123 * there's no problem using the cycle counter. But if it didn't,
124 * or when using oscillator bypass mode, we must be told the speed
125 * of the main clock.
126 */
127 if (!main_clock) {
128 do {
Jens Scharsig7cedb292010-02-14 12:20:43 +0100129 tmp = readl(&pmc->mcfr);
130 } while (!(tmp & AT91_PMC_MCFR_MAINRDY));
131 tmp &= AT91_PMC_MCFR_MAINF_MASK;
Tom Rini65cc0e22022-11-16 13:10:41 -0500132 main_clock = tmp * (CFG_SYS_AT91_SLOW_CLOCK / 16);
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200133 }
134#endif
Simon Glassf47e6ec2012-12-13 20:48:31 +0000135 gd->arch.main_clk_rate_hz = main_clock;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200136
137 /* report if PLLA is more than mildly overclocked */
Simon Glassf47e6ec2012-12-13 20:48:31 +0000138 gd->arch.plla_rate_hz = at91_pll_rate(main_clock, readl(&pmc->pllar));
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200139
140#ifdef CONFIG_USB_ATMEL
141 /*
142 * USB clock init: choose 48 MHz PLLB value,
143 * disable 48MHz clock during usb peripheral suspend.
144 *
145 * REVISIT: assumes MCK doesn't derive from PLLB!
146 */
Simon Glassf47e6ec2012-12-13 20:48:31 +0000147 gd->arch.at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) |
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100148 AT91_PMC_PLLBR_USBDIV_2;
Simon Glassf47e6ec2012-12-13 20:48:31 +0000149 gd->arch.pllb_rate_hz = at91_pll_rate(main_clock,
150 gd->arch.at91_pllb_usb_init);
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200151#endif
152
153 /*
154 * MCK and CPU derive from one of those primary clocks.
155 * For now, assume this parentage won't change.
156 */
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100157 mckr = readl(&pmc->mckr);
Bo Shenf7fa2f32012-07-05 17:21:46 +0000158#if defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
Wu, Josh9e336902013-04-16 23:42:44 +0000159 || defined(CONFIG_AT91SAM9N12) || defined(CONFIG_AT91SAM9X5)
Sedji Gaouaou22ee6472009-07-09 10:16:29 +0200160 /* plla divisor by 2 */
Simon Glassf47e6ec2012-12-13 20:48:31 +0000161 gd->arch.plla_rate_hz /= (1 << ((mckr & 1 << 12) >> 12));
Sedji Gaouaou22ee6472009-07-09 10:16:29 +0200162#endif
Simon Glassf47e6ec2012-12-13 20:48:31 +0000163 gd->arch.mck_rate_hz = at91_css_to_rate(mckr & AT91_PMC_MCKR_CSS_MASK);
164 freq = gd->arch.mck_rate_hz;
Sedji Gaouaou22ee6472009-07-09 10:16:29 +0200165
Heiko Schocher806a5a32016-08-17 09:13:24 +0200166#if defined(CONFIG_AT91SAM9X5)
167 /* different in prescale on at91sam9x5 */
168 freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 4));
169#else
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100170 freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 2)); /* prescale */
Heiko Schocher806a5a32016-08-17 09:13:24 +0200171#endif
172
Andreas Bießmannc3a383f2011-06-12 01:49:11 +0000173#if defined(CONFIG_AT91SAM9G20)
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100174 /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
Simon Glassf47e6ec2012-12-13 20:48:31 +0000175 gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ?
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100176 freq / ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 7) : freq;
177 if (mckr & AT91_PMC_MCKR_MDIV_MASK)
178 freq /= 2; /* processor clock division */
Bo Shenf7fa2f32012-07-05 17:21:46 +0000179#elif defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
Wu, Josh9e336902013-04-16 23:42:44 +0000180 || defined(CONFIG_AT91SAM9N12) || defined(CONFIG_AT91SAM9X5)
Bo Shenf7fa2f32012-07-05 17:21:46 +0000181 /* mdiv <==> divisor
182 * 0 <==> 1
183 * 1 <==> 2
184 * 2 <==> 4
185 * 3 <==> 3
186 */
Simon Glassf47e6ec2012-12-13 20:48:31 +0000187 gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ==
Asen Dimove99056e2010-03-18 13:46:45 +0200188 (AT91_PMC_MCKR_MDIV_2 | AT91_PMC_MCKR_MDIV_4)
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100189 ? freq / 3
190 : freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200191#else
Simon Glassf47e6ec2012-12-13 20:48:31 +0000192 gd->arch.mck_rate_hz = freq /
193 (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200194#endif
Simon Glassf47e6ec2012-12-13 20:48:31 +0000195 gd->arch.cpu_clk_rate_hz = freq;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200196
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100197 return 0;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200198}
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100199
200#if !defined(AT91_PLL_LOCK_TIMEOUT)
201#define AT91_PLL_LOCK_TIMEOUT 1000000
202#endif
203
204void at91_plla_init(u32 pllar)
205{
206 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100207
208 writel(pllar, &pmc->pllar);
Bo Shen72cb3b62015-03-27 14:23:33 +0800209 while (!(readl(&pmc->sr) & AT91_PMC_LOCKA))
210 ;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100211}
212void at91_pllb_init(u32 pllbr)
213{
214 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100215
216 writel(pllbr, &pmc->pllbr);
Bo Shen72cb3b62015-03-27 14:23:33 +0800217 while (!(readl(&pmc->sr) & AT91_PMC_LOCKB))
218 ;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100219}
220
221void at91_mck_init(u32 mckr)
222{
223 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100224 u32 tmp;
225
226 tmp = readl(&pmc->mckr);
Bo Shen72cb3b62015-03-27 14:23:33 +0800227 tmp &= ~AT91_PMC_MCKR_PRES_MASK;
228 tmp |= mckr & AT91_PMC_MCKR_PRES_MASK;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100229 writel(tmp, &pmc->mckr);
Bo Shen72cb3b62015-03-27 14:23:33 +0800230 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
231 ;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100232
Bo Shen72cb3b62015-03-27 14:23:33 +0800233 tmp = readl(&pmc->mckr);
234 tmp &= ~AT91_PMC_MCKR_MDIV_MASK;
235 tmp |= mckr & AT91_PMC_MCKR_MDIV_MASK;
236 writel(tmp, &pmc->mckr);
237 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
238 ;
239
240 tmp = readl(&pmc->mckr);
241 tmp &= ~AT91_PMC_MCKR_PLLADIV_MASK;
242 tmp |= mckr & AT91_PMC_MCKR_PLLADIV_MASK;
243 writel(tmp, &pmc->mckr);
244 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
245 ;
246
247 tmp = readl(&pmc->mckr);
248 tmp &= ~AT91_PMC_MCKR_CSS_MASK;
249 tmp |= mckr & AT91_PMC_MCKR_CSS_MASK;
250 writel(tmp, &pmc->mckr);
251 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
252 ;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100253}
Wenyou Yangbe5e4852016-02-03 10:20:43 +0800254
255int at91_pllb_clk_enable(u32 pllbr)
256{
257 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
258 ulong start_time, tmp_time;
259
260 start_time = get_timer(0);
261 writel(pllbr, &pmc->pllbr);
262 while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != AT91_PMC_LOCKB) {
263 tmp_time = get_timer(0);
264 if ((tmp_time - start_time) > EN_PLLB_TIMEOUT) {
265 printf("ERROR: failed to enable PLLB\n");
266 return -1;
267 }
268 }
269
270 return 0;
271}
272
273int at91_pllb_clk_disable(void)
274{
275 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
276 ulong start_time, tmp_time;
277
278 start_time = get_timer(0);
279 writel(0, &pmc->pllbr);
280 while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != 0) {
281 tmp_time = get_timer(0);
282 if ((tmp_time - start_time) > EN_PLLB_TIMEOUT) {
283 printf("ERROR: failed to disable PLLB\n");
284 return -1;
285 }
286 }
287
288 return 0;
289}