blob: dc5c6c4b0b29b0f2f69a3ec732b6a87185c1c219 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +02001/*
2 * [origin: Linux kernel linux/arch/arm/mach-at91/clock.c]
3 *
4 * Copyright (C) 2005 David Brownell
5 * Copyright (C) 2005 Ivan Kokshaysky
6 * Copyright (C) 2009 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Reinhard Meyer5dca7102010-10-05 16:54:35 +020014#include <common.h>
Reinhard Meyer86592f62010-11-07 13:26:14 +010015#include <asm/io.h>
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020016#include <asm/arch/hardware.h>
17#include <asm/arch/at91_pmc.h>
18#include <asm/arch/clk.h>
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020019
Reinhard Meyer5dca7102010-10-05 16:54:35 +020020#if !defined(CONFIG_AT91FAMILY)
21# error You need to define CONFIG_AT91FAMILY in your board config!
22#endif
23
24DECLARE_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:
Reinhard Meyer9f3fe902010-11-03 15:39:55 +010030 return CONFIG_SYS_AT91_SLOW_CLOCK;
Jens Scharsig0cf0b932010-02-03 22:46:58 +010031 case AT91_PMC_MCKR_CSS_MAIN:
Reinhard Meyer5dca7102010-10-05 16:54:35 +020032 return gd->main_clk_rate_hz;
Jens Scharsig0cf0b932010-02-03 22:46:58 +010033 case AT91_PMC_MCKR_CSS_PLLA:
Reinhard Meyer5dca7102010-10-05 16:54:35 +020034 return gd->plla_rate_hz;
Jens Scharsig0cf0b932010-02-03 22:46:58 +010035 case AT91_PMC_MCKR_CSS_PLLB:
Reinhard Meyer5dca7102010-10-05 16:54:35 +020036 return gd->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;
Achim Ehrlich7c966a82010-02-24 10:29:16 +0100119#ifndef CONFIG_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;
Reinhard Meyer9f3fe902010-11-03 15:39:55 +0100132 main_clock = tmp * (CONFIG_SYS_AT91_SLOW_CLOCK / 16);
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200133 }
134#endif
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200135 gd->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 */
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200138 gd->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 */
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200147 gd->at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) |
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100148 AT91_PMC_PLLBR_USBDIV_2;
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200149 gd->pllb_rate_hz = at91_pll_rate(main_clock, gd->at91_pllb_usb_init);
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200150#endif
151
152 /*
153 * MCK and CPU derive from one of those primary clocks.
154 * For now, assume this parentage won't change.
155 */
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100156 mckr = readl(&pmc->mckr);
Bo Shenf7fa2f32012-07-05 17:21:46 +0000157#if defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
158 || defined(CONFIG_AT91SAM9X5)
Sedji Gaouaou22ee6472009-07-09 10:16:29 +0200159 /* plla divisor by 2 */
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200160 gd->plla_rate_hz /= (1 << ((mckr & 1 << 12) >> 12));
Sedji Gaouaou22ee6472009-07-09 10:16:29 +0200161#endif
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200162 gd->mck_rate_hz = at91_css_to_rate(mckr & AT91_PMC_MCKR_CSS_MASK);
163 freq = gd->mck_rate_hz;
Sedji Gaouaou22ee6472009-07-09 10:16:29 +0200164
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100165 freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 2)); /* prescale */
Andreas Bießmannc3a383f2011-06-12 01:49:11 +0000166#if defined(CONFIG_AT91SAM9G20)
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100167 /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200168 gd->mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ?
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100169 freq / ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 7) : freq;
170 if (mckr & AT91_PMC_MCKR_MDIV_MASK)
171 freq /= 2; /* processor clock division */
Bo Shenf7fa2f32012-07-05 17:21:46 +0000172#elif defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
173 || defined(CONFIG_AT91SAM9X5)
174 /* mdiv <==> divisor
175 * 0 <==> 1
176 * 1 <==> 2
177 * 2 <==> 4
178 * 3 <==> 3
179 */
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200180 gd->mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ==
Asen Dimove99056e2010-03-18 13:46:45 +0200181 (AT91_PMC_MCKR_MDIV_2 | AT91_PMC_MCKR_MDIV_4)
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100182 ? freq / 3
183 : freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200184#else
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200185 gd->mck_rate_hz = freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200186#endif
Reinhard Meyer5dca7102010-10-05 16:54:35 +0200187 gd->cpu_clk_rate_hz = freq;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200188
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100189 return 0;
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +0200190}