blob: 7156185a46fa34cb4e9348111ee307c064fbc4cb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARD23164f12009-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-VILLARD23164f12009-04-16 21:30:44 +02008 */
9
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020010#include <common.h>
Reinhard Meyerb06208c2010-11-07 13:26:14 +010011#include <asm/io.h>
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020012#include <asm/arch/hardware.h>
13#include <asm/arch/at91_pmc.h>
14#include <asm/arch/clk.h>
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020015
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020016#if !defined(CONFIG_AT91FAMILY)
17# error You need to define CONFIG_AT91FAMILY in your board config!
18#endif
19
Wenyou Yang0a30fcb2016-02-03 10:20:43 +080020#define EN_PLLB_TIMEOUT 500
21
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020022DECLARE_GLOBAL_DATA_PTR;
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020023
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020024static unsigned long at91_css_to_rate(unsigned long css)
25{
26 switch (css) {
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010027 case AT91_PMC_MCKR_CSS_SLOW:
Reinhard Meyere260d0b2010-11-03 15:39:55 +010028 return CONFIG_SYS_AT91_SLOW_CLOCK;
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010029 case AT91_PMC_MCKR_CSS_MAIN:
Simon Glasse61accc2012-12-13 20:48:31 +000030 return gd->arch.main_clk_rate_hz;
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010031 case AT91_PMC_MCKR_CSS_PLLA:
Simon Glasse61accc2012-12-13 20:48:31 +000032 return gd->arch.plla_rate_hz;
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010033 case AT91_PMC_MCKR_CSS_PLLB:
Simon Glasse61accc2012-12-13 20:48:31 +000034 return gd->arch.pllb_rate_hz;
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020035 }
36
37 return 0;
38}
39
40#ifdef CONFIG_USB_ATMEL
41static unsigned at91_pll_calc(unsigned main_freq, unsigned out_freq)
42{
43 unsigned i, div = 0, mul = 0, diff = 1 << 30;
44 unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
45
46 /* PLL output max 240 MHz (or 180 MHz per errata) */
47 if (out_freq > 240000000)
48 goto fail;
49
50 for (i = 1; i < 256; i++) {
51 int diff1;
52 unsigned input, mul1;
53
54 /*
55 * PLL input between 1MHz and 32MHz per spec, but lower
56 * frequences seem necessary in some cases so allow 100K.
57 * Warning: some newer products need 2MHz min.
58 */
59 input = main_freq / i;
60#if defined(CONFIG_AT91SAM9G20)
61 if (input < 2000000)
62 continue;
63#endif
64 if (input < 100000)
65 continue;
66 if (input > 32000000)
67 continue;
68
69 mul1 = out_freq / input;
70#if defined(CONFIG_AT91SAM9G20)
71 if (mul > 63)
72 continue;
73#endif
74 if (mul1 > 2048)
75 continue;
76 if (mul1 < 2)
77 goto fail;
78
79 diff1 = out_freq - input * mul1;
80 if (diff1 < 0)
81 diff1 = -diff1;
82 if (diff > diff1) {
83 diff = diff1;
84 div = i;
85 mul = mul1;
86 if (diff == 0)
87 break;
88 }
89 }
90 if (i == 256 && diff > (out_freq >> 5))
91 goto fail;
92 return ret | ((mul - 1) << 16) | div;
93fail:
94 return 0;
95}
Daniel Gorsulowski1bbbb392009-04-23 15:37:16 +020096#endif
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020097
98static u32 at91_pll_rate(u32 freq, u32 reg)
99{
100 unsigned mul, div;
101
102 div = reg & 0xff;
103 mul = (reg >> 16) & 0x7ff;
104 if (div && mul) {
105 freq /= div;
106 freq *= mul + 1;
107 } else
108 freq = 0;
109
110 return freq;
111}
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200112
113int at91_clock_init(unsigned long main_clock)
114{
115 unsigned freq, mckr;
Reinhard Meyere260d0b2010-11-03 15:39:55 +0100116 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
Achim Ehrlich443873d2010-02-24 10:29:16 +0100117#ifndef CONFIG_SYS_AT91_MAIN_CLOCK
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200118 unsigned tmp;
119 /*
120 * When the bootloader initialized the main oscillator correctly,
121 * there's no problem using the cycle counter. But if it didn't,
122 * or when using oscillator bypass mode, we must be told the speed
123 * of the main clock.
124 */
125 if (!main_clock) {
126 do {
Jens Scharsige3542352010-02-14 12:20:43 +0100127 tmp = readl(&pmc->mcfr);
128 } while (!(tmp & AT91_PMC_MCFR_MAINRDY));
129 tmp &= AT91_PMC_MCFR_MAINF_MASK;
Reinhard Meyere260d0b2010-11-03 15:39:55 +0100130 main_clock = tmp * (CONFIG_SYS_AT91_SLOW_CLOCK / 16);
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200131 }
132#endif
Simon Glasse61accc2012-12-13 20:48:31 +0000133 gd->arch.main_clk_rate_hz = main_clock;
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200134
135 /* report if PLLA is more than mildly overclocked */
Simon Glasse61accc2012-12-13 20:48:31 +0000136 gd->arch.plla_rate_hz = at91_pll_rate(main_clock, readl(&pmc->pllar));
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200137
138#ifdef CONFIG_USB_ATMEL
139 /*
140 * USB clock init: choose 48 MHz PLLB value,
141 * disable 48MHz clock during usb peripheral suspend.
142 *
143 * REVISIT: assumes MCK doesn't derive from PLLB!
144 */
Simon Glasse61accc2012-12-13 20:48:31 +0000145 gd->arch.at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) |
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100146 AT91_PMC_PLLBR_USBDIV_2;
Simon Glasse61accc2012-12-13 20:48:31 +0000147 gd->arch.pllb_rate_hz = at91_pll_rate(main_clock,
148 gd->arch.at91_pllb_usb_init);
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200149#endif
150
151 /*
152 * MCK and CPU derive from one of those primary clocks.
153 * For now, assume this parentage won't change.
154 */
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100155 mckr = readl(&pmc->mckr);
Bo Shen42aafb32012-07-05 17:21:46 +0000156#if defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
Wu, Josh3f338c12013-04-16 23:42:44 +0000157 || defined(CONFIG_AT91SAM9N12) || defined(CONFIG_AT91SAM9X5)
Sedji Gaouaou538566d2009-07-09 10:16:29 +0200158 /* plla divisor by 2 */
Simon Glasse61accc2012-12-13 20:48:31 +0000159 gd->arch.plla_rate_hz /= (1 << ((mckr & 1 << 12) >> 12));
Sedji Gaouaou538566d2009-07-09 10:16:29 +0200160#endif
Simon Glasse61accc2012-12-13 20:48:31 +0000161 gd->arch.mck_rate_hz = at91_css_to_rate(mckr & AT91_PMC_MCKR_CSS_MASK);
162 freq = gd->arch.mck_rate_hz;
Sedji Gaouaou538566d2009-07-09 10:16:29 +0200163
Heiko Schocherae369692016-08-17 09:13:24 +0200164#if defined(CONFIG_AT91SAM9X5)
165 /* different in prescale on at91sam9x5 */
166 freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 4));
167#else
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100168 freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 2)); /* prescale */
Heiko Schocherae369692016-08-17 09:13:24 +0200169#endif
170
Andreas Bießmannf4c9f922011-06-12 01:49:11 +0000171#if defined(CONFIG_AT91SAM9G20)
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100172 /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
Simon Glasse61accc2012-12-13 20:48:31 +0000173 gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ?
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100174 freq / ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 7) : freq;
175 if (mckr & AT91_PMC_MCKR_MDIV_MASK)
176 freq /= 2; /* processor clock division */
Bo Shen42aafb32012-07-05 17:21:46 +0000177#elif defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
Wu, Josh3f338c12013-04-16 23:42:44 +0000178 || defined(CONFIG_AT91SAM9N12) || defined(CONFIG_AT91SAM9X5)
Bo Shen42aafb32012-07-05 17:21:46 +0000179 /* mdiv <==> divisor
180 * 0 <==> 1
181 * 1 <==> 2
182 * 2 <==> 4
183 * 3 <==> 3
184 */
Simon Glasse61accc2012-12-13 20:48:31 +0000185 gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ==
Asen Dimov30623552010-03-18 13:46:45 +0200186 (AT91_PMC_MCKR_MDIV_2 | AT91_PMC_MCKR_MDIV_4)
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100187 ? freq / 3
188 : freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200189#else
Simon Glasse61accc2012-12-13 20:48:31 +0000190 gd->arch.mck_rate_hz = freq /
191 (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200192#endif
Simon Glasse61accc2012-12-13 20:48:31 +0000193 gd->arch.cpu_clk_rate_hz = freq;
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200194
Jens Scharsiga4db1ca2010-02-03 22:46:58 +0100195 return 0;
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +0200196}
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100197
198#if !defined(AT91_PLL_LOCK_TIMEOUT)
199#define AT91_PLL_LOCK_TIMEOUT 1000000
200#endif
201
202void at91_plla_init(u32 pllar)
203{
204 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100205
206 writel(pllar, &pmc->pllar);
Bo Shenff876152015-03-27 14:23:33 +0800207 while (!(readl(&pmc->sr) & AT91_PMC_LOCKA))
208 ;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100209}
210void at91_pllb_init(u32 pllbr)
211{
212 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100213
214 writel(pllbr, &pmc->pllbr);
Bo Shenff876152015-03-27 14:23:33 +0800215 while (!(readl(&pmc->sr) & AT91_PMC_LOCKB))
216 ;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100217}
218
219void at91_mck_init(u32 mckr)
220{
221 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100222 u32 tmp;
223
224 tmp = readl(&pmc->mckr);
Bo Shenff876152015-03-27 14:23:33 +0800225 tmp &= ~AT91_PMC_MCKR_PRES_MASK;
226 tmp |= mckr & AT91_PMC_MCKR_PRES_MASK;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100227 writel(tmp, &pmc->mckr);
Bo Shenff876152015-03-27 14:23:33 +0800228 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
229 ;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100230
Bo Shenff876152015-03-27 14:23:33 +0800231 tmp = readl(&pmc->mckr);
232 tmp &= ~AT91_PMC_MCKR_MDIV_MASK;
233 tmp |= mckr & AT91_PMC_MCKR_MDIV_MASK;
234 writel(tmp, &pmc->mckr);
235 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
236 ;
237
238 tmp = readl(&pmc->mckr);
239 tmp &= ~AT91_PMC_MCKR_PLLADIV_MASK;
240 tmp |= mckr & AT91_PMC_MCKR_PLLADIV_MASK;
241 writel(tmp, &pmc->mckr);
242 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
243 ;
244
245 tmp = readl(&pmc->mckr);
246 tmp &= ~AT91_PMC_MCKR_CSS_MASK;
247 tmp |= mckr & AT91_PMC_MCKR_CSS_MASK;
248 writel(tmp, &pmc->mckr);
249 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
250 ;
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100251}
Wenyou Yang0a30fcb2016-02-03 10:20:43 +0800252
253int at91_pllb_clk_enable(u32 pllbr)
254{
255 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
256 ulong start_time, tmp_time;
257
258 start_time = get_timer(0);
259 writel(pllbr, &pmc->pllbr);
260 while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != AT91_PMC_LOCKB) {
261 tmp_time = get_timer(0);
262 if ((tmp_time - start_time) > EN_PLLB_TIMEOUT) {
263 printf("ERROR: failed to enable PLLB\n");
264 return -1;
265 }
266 }
267
268 return 0;
269}
270
271int at91_pllb_clk_disable(void)
272{
273 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
274 ulong start_time, tmp_time;
275
276 start_time = get_timer(0);
277 writel(0, &pmc->pllbr);
278 while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != 0) {
279 tmp_time = get_timer(0);
280 if ((tmp_time - start_time) > EN_PLLB_TIMEOUT) {
281 printf("ERROR: failed to disable PLLB\n");
282 return -1;
283 }
284 }
285
286 return 0;
287}