Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 1 | /* |
| 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 Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 14 | #include <common.h> |
Reinhard Meyer | b06208c | 2010-11-07 13:26:14 +0100 | [diff] [blame^] | 15 | #include <asm/io.h> |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 16 | #include <asm/arch/hardware.h> |
| 17 | #include <asm/arch/at91_pmc.h> |
| 18 | #include <asm/arch/clk.h> |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 19 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 20 | #if !defined(CONFIG_AT91FAMILY) |
| 21 | # error You need to define CONFIG_AT91FAMILY in your board config! |
| 22 | #endif |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 25 | |
| 26 | unsigned long get_cpu_clk_rate(void) |
| 27 | { |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 28 | return gd->cpu_clk_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | unsigned long get_main_clk_rate(void) |
| 32 | { |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 33 | return gd->main_clk_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | unsigned long get_mck_clk_rate(void) |
| 37 | { |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 38 | return gd->mck_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | unsigned long get_plla_clk_rate(void) |
| 42 | { |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 43 | return gd->plla_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | unsigned long get_pllb_clk_rate(void) |
| 47 | { |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 48 | return gd->pllb_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | u32 get_pllb_init(void) |
| 52 | { |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 53 | return gd->at91_pllb_usb_init; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static unsigned long at91_css_to_rate(unsigned long css) |
| 57 | { |
| 58 | switch (css) { |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 59 | case AT91_PMC_MCKR_CSS_SLOW: |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 60 | return CONFIG_SYS_AT91_SLOW_CLOCK; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 61 | case AT91_PMC_MCKR_CSS_MAIN: |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 62 | return gd->main_clk_rate_hz; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 63 | case AT91_PMC_MCKR_CSS_PLLA: |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 64 | return gd->plla_rate_hz; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 65 | case AT91_PMC_MCKR_CSS_PLLB: |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 66 | return gd->pllb_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | #ifdef CONFIG_USB_ATMEL |
| 73 | static unsigned at91_pll_calc(unsigned main_freq, unsigned out_freq) |
| 74 | { |
| 75 | unsigned i, div = 0, mul = 0, diff = 1 << 30; |
| 76 | unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00; |
| 77 | |
| 78 | /* PLL output max 240 MHz (or 180 MHz per errata) */ |
| 79 | if (out_freq > 240000000) |
| 80 | goto fail; |
| 81 | |
| 82 | for (i = 1; i < 256; i++) { |
| 83 | int diff1; |
| 84 | unsigned input, mul1; |
| 85 | |
| 86 | /* |
| 87 | * PLL input between 1MHz and 32MHz per spec, but lower |
| 88 | * frequences seem necessary in some cases so allow 100K. |
| 89 | * Warning: some newer products need 2MHz min. |
| 90 | */ |
| 91 | input = main_freq / i; |
| 92 | #if defined(CONFIG_AT91SAM9G20) |
| 93 | if (input < 2000000) |
| 94 | continue; |
| 95 | #endif |
| 96 | if (input < 100000) |
| 97 | continue; |
| 98 | if (input > 32000000) |
| 99 | continue; |
| 100 | |
| 101 | mul1 = out_freq / input; |
| 102 | #if defined(CONFIG_AT91SAM9G20) |
| 103 | if (mul > 63) |
| 104 | continue; |
| 105 | #endif |
| 106 | if (mul1 > 2048) |
| 107 | continue; |
| 108 | if (mul1 < 2) |
| 109 | goto fail; |
| 110 | |
| 111 | diff1 = out_freq - input * mul1; |
| 112 | if (diff1 < 0) |
| 113 | diff1 = -diff1; |
| 114 | if (diff > diff1) { |
| 115 | diff = diff1; |
| 116 | div = i; |
| 117 | mul = mul1; |
| 118 | if (diff == 0) |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | if (i == 256 && diff > (out_freq >> 5)) |
| 123 | goto fail; |
| 124 | return ret | ((mul - 1) << 16) | div; |
| 125 | fail: |
| 126 | return 0; |
| 127 | } |
Daniel Gorsulowski | 1bbbb39 | 2009-04-23 15:37:16 +0200 | [diff] [blame] | 128 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 129 | |
| 130 | static u32 at91_pll_rate(u32 freq, u32 reg) |
| 131 | { |
| 132 | unsigned mul, div; |
| 133 | |
| 134 | div = reg & 0xff; |
| 135 | mul = (reg >> 16) & 0x7ff; |
| 136 | if (div && mul) { |
| 137 | freq /= div; |
| 138 | freq *= mul + 1; |
| 139 | } else |
| 140 | freq = 0; |
| 141 | |
| 142 | return freq; |
| 143 | } |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 144 | |
| 145 | int at91_clock_init(unsigned long main_clock) |
| 146 | { |
| 147 | unsigned freq, mckr; |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 148 | at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC; |
Achim Ehrlich | 443873d | 2010-02-24 10:29:16 +0100 | [diff] [blame] | 149 | #ifndef CONFIG_SYS_AT91_MAIN_CLOCK |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 150 | unsigned tmp; |
| 151 | /* |
| 152 | * When the bootloader initialized the main oscillator correctly, |
| 153 | * there's no problem using the cycle counter. But if it didn't, |
| 154 | * or when using oscillator bypass mode, we must be told the speed |
| 155 | * of the main clock. |
| 156 | */ |
| 157 | if (!main_clock) { |
| 158 | do { |
Jens Scharsig | e354235 | 2010-02-14 12:20:43 +0100 | [diff] [blame] | 159 | tmp = readl(&pmc->mcfr); |
| 160 | } while (!(tmp & AT91_PMC_MCFR_MAINRDY)); |
| 161 | tmp &= AT91_PMC_MCFR_MAINF_MASK; |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 162 | main_clock = tmp * (CONFIG_SYS_AT91_SLOW_CLOCK / 16); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 163 | } |
| 164 | #endif |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 165 | gd->main_clk_rate_hz = main_clock; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 166 | |
| 167 | /* report if PLLA is more than mildly overclocked */ |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 168 | gd->plla_rate_hz = at91_pll_rate(main_clock, readl(&pmc->pllar)); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 169 | |
| 170 | #ifdef CONFIG_USB_ATMEL |
| 171 | /* |
| 172 | * USB clock init: choose 48 MHz PLLB value, |
| 173 | * disable 48MHz clock during usb peripheral suspend. |
| 174 | * |
| 175 | * REVISIT: assumes MCK doesn't derive from PLLB! |
| 176 | */ |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 177 | gd->at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 178 | AT91_PMC_PLLBR_USBDIV_2; |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 179 | gd->pllb_rate_hz = at91_pll_rate(main_clock, gd->at91_pllb_usb_init); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 180 | #endif |
| 181 | |
| 182 | /* |
| 183 | * MCK and CPU derive from one of those primary clocks. |
| 184 | * For now, assume this parentage won't change. |
| 185 | */ |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 186 | mckr = readl(&pmc->mckr); |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 187 | #if defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) |
| 188 | /* plla divisor by 2 */ |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 189 | gd->plla_rate_hz /= (1 << ((mckr & 1 << 12) >> 12)); |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 190 | #endif |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 191 | gd->mck_rate_hz = at91_css_to_rate(mckr & AT91_PMC_MCKR_CSS_MASK); |
| 192 | freq = gd->mck_rate_hz; |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 193 | |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 194 | freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 2)); /* prescale */ |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 195 | #if defined(CONFIG_AT91RM9200) |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 196 | /* mdiv */ |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 197 | gd->mck_rate_hz = freq / (1 + ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8)); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 198 | #elif defined(CONFIG_AT91SAM9G20) |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 199 | /* mdiv ; (x >> 7) = ((x >> 8) * 2) */ |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 200 | gd->mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ? |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 201 | freq / ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 7) : freq; |
| 202 | if (mckr & AT91_PMC_MCKR_MDIV_MASK) |
| 203 | freq /= 2; /* processor clock division */ |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 204 | #elif defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 205 | gd->mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) == |
Asen Dimov | 3062355 | 2010-03-18 13:46:45 +0200 | [diff] [blame] | 206 | (AT91_PMC_MCKR_MDIV_2 | AT91_PMC_MCKR_MDIV_4) |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 207 | ? freq / 3 |
| 208 | : freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8)); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 209 | #else |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 210 | gd->mck_rate_hz = freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8)); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 211 | #endif |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 212 | gd->cpu_clk_rate_hz = freq; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 213 | |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 214 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 215 | } |