Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 2 | /* |
| 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-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Reinhard Meyer | b06208c | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 12 | #include <asm/io.h> |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 13 | #include <asm/arch/hardware.h> |
| 14 | #include <asm/arch/at91_pmc.h> |
| 15 | #include <asm/arch/clk.h> |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 16 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 17 | #if !defined(CONFIG_AT91FAMILY) |
| 18 | # error You need to define CONFIG_AT91FAMILY in your board config! |
| 19 | #endif |
| 20 | |
Wenyou Yang | 0a30fcb | 2016-02-03 10:20:43 +0800 | [diff] [blame] | 21 | #define EN_PLLB_TIMEOUT 500 |
| 22 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 24 | |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 25 | static unsigned long at91_css_to_rate(unsigned long css) |
| 26 | { |
| 27 | switch (css) { |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 28 | case AT91_PMC_MCKR_CSS_SLOW: |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 29 | return CFG_SYS_AT91_SLOW_CLOCK; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 30 | case AT91_PMC_MCKR_CSS_MAIN: |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 31 | return gd->arch.main_clk_rate_hz; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 32 | case AT91_PMC_MCKR_CSS_PLLA: |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 33 | return gd->arch.plla_rate_hz; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 34 | case AT91_PMC_MCKR_CSS_PLLB: |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 35 | return gd->arch.pllb_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | #ifdef CONFIG_USB_ATMEL |
| 42 | static unsigned at91_pll_calc(unsigned main_freq, unsigned out_freq) |
| 43 | { |
| 44 | unsigned i, div = 0, mul = 0, diff = 1 << 30; |
| 45 | unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00; |
| 46 | |
| 47 | /* PLL output max 240 MHz (or 180 MHz per errata) */ |
| 48 | if (out_freq > 240000000) |
| 49 | goto fail; |
| 50 | |
| 51 | for (i = 1; i < 256; i++) { |
| 52 | int diff1; |
| 53 | unsigned input, mul1; |
| 54 | |
| 55 | /* |
| 56 | * PLL input between 1MHz and 32MHz per spec, but lower |
| 57 | * frequences seem necessary in some cases so allow 100K. |
| 58 | * Warning: some newer products need 2MHz min. |
| 59 | */ |
| 60 | input = main_freq / i; |
| 61 | #if defined(CONFIG_AT91SAM9G20) |
| 62 | if (input < 2000000) |
| 63 | continue; |
| 64 | #endif |
| 65 | if (input < 100000) |
| 66 | continue; |
| 67 | if (input > 32000000) |
| 68 | continue; |
| 69 | |
| 70 | mul1 = out_freq / input; |
| 71 | #if defined(CONFIG_AT91SAM9G20) |
| 72 | if (mul > 63) |
| 73 | continue; |
| 74 | #endif |
| 75 | if (mul1 > 2048) |
| 76 | continue; |
| 77 | if (mul1 < 2) |
| 78 | goto fail; |
| 79 | |
| 80 | diff1 = out_freq - input * mul1; |
| 81 | if (diff1 < 0) |
| 82 | diff1 = -diff1; |
| 83 | if (diff > diff1) { |
| 84 | diff = diff1; |
| 85 | div = i; |
| 86 | mul = mul1; |
| 87 | if (diff == 0) |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | if (i == 256 && diff > (out_freq >> 5)) |
| 92 | goto fail; |
| 93 | return ret | ((mul - 1) << 16) | div; |
| 94 | fail: |
| 95 | return 0; |
| 96 | } |
Daniel Gorsulowski | 1bbbb39 | 2009-04-23 15:37:16 +0200 | [diff] [blame] | 97 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 98 | |
| 99 | static u32 at91_pll_rate(u32 freq, u32 reg) |
| 100 | { |
| 101 | unsigned mul, div; |
| 102 | |
| 103 | div = reg & 0xff; |
| 104 | mul = (reg >> 16) & 0x7ff; |
| 105 | if (div && mul) { |
| 106 | freq /= div; |
| 107 | freq *= mul + 1; |
| 108 | } else |
| 109 | freq = 0; |
| 110 | |
| 111 | return freq; |
| 112 | } |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 113 | |
| 114 | int at91_clock_init(unsigned long main_clock) |
| 115 | { |
| 116 | unsigned freq, mckr; |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 117 | at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC; |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 118 | #ifndef CFG_SYS_AT91_MAIN_CLOCK |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 119 | unsigned tmp; |
| 120 | /* |
| 121 | * When the bootloader initialized the main oscillator correctly, |
| 122 | * there's no problem using the cycle counter. But if it didn't, |
| 123 | * or when using oscillator bypass mode, we must be told the speed |
| 124 | * of the main clock. |
| 125 | */ |
| 126 | if (!main_clock) { |
| 127 | do { |
Jens Scharsig | e354235 | 2010-02-14 12:20:43 +0100 | [diff] [blame] | 128 | tmp = readl(&pmc->mcfr); |
| 129 | } while (!(tmp & AT91_PMC_MCFR_MAINRDY)); |
| 130 | tmp &= AT91_PMC_MCFR_MAINF_MASK; |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 131 | main_clock = tmp * (CFG_SYS_AT91_SLOW_CLOCK / 16); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 132 | } |
| 133 | #endif |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 134 | gd->arch.main_clk_rate_hz = main_clock; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 135 | |
| 136 | /* report if PLLA is more than mildly overclocked */ |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 137 | gd->arch.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] | 138 | |
| 139 | #ifdef CONFIG_USB_ATMEL |
| 140 | /* |
| 141 | * USB clock init: choose 48 MHz PLLB value, |
| 142 | * disable 48MHz clock during usb peripheral suspend. |
| 143 | * |
| 144 | * REVISIT: assumes MCK doesn't derive from PLLB! |
| 145 | */ |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 146 | gd->arch.at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 147 | AT91_PMC_PLLBR_USBDIV_2; |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 148 | gd->arch.pllb_rate_hz = at91_pll_rate(main_clock, |
| 149 | gd->arch.at91_pllb_usb_init); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 150 | #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 Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 156 | mckr = readl(&pmc->mckr); |
Bo Shen | 42aafb3 | 2012-07-05 17:21:46 +0000 | [diff] [blame] | 157 | #if defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \ |
Wu, Josh | 3f338c1 | 2013-04-16 23:42:44 +0000 | [diff] [blame] | 158 | || defined(CONFIG_AT91SAM9N12) || defined(CONFIG_AT91SAM9X5) |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 159 | /* plla divisor by 2 */ |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 160 | gd->arch.plla_rate_hz /= (1 << ((mckr & 1 << 12) >> 12)); |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 161 | #endif |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 162 | gd->arch.mck_rate_hz = at91_css_to_rate(mckr & AT91_PMC_MCKR_CSS_MASK); |
| 163 | freq = gd->arch.mck_rate_hz; |
Sedji Gaouaou | 538566d | 2009-07-09 10:16:29 +0200 | [diff] [blame] | 164 | |
Heiko Schocher | ae36969 | 2016-08-17 09:13:24 +0200 | [diff] [blame] | 165 | #if defined(CONFIG_AT91SAM9X5) |
| 166 | /* different in prescale on at91sam9x5 */ |
| 167 | freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 4)); |
| 168 | #else |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 169 | freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 2)); /* prescale */ |
Heiko Schocher | ae36969 | 2016-08-17 09:13:24 +0200 | [diff] [blame] | 170 | #endif |
| 171 | |
Andreas Bießmann | f4c9f92 | 2011-06-12 01:49:11 +0000 | [diff] [blame] | 172 | #if defined(CONFIG_AT91SAM9G20) |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 173 | /* mdiv ; (x >> 7) = ((x >> 8) * 2) */ |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 174 | gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ? |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 175 | freq / ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 7) : freq; |
| 176 | if (mckr & AT91_PMC_MCKR_MDIV_MASK) |
| 177 | freq /= 2; /* processor clock division */ |
Bo Shen | 42aafb3 | 2012-07-05 17:21:46 +0000 | [diff] [blame] | 178 | #elif defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \ |
Wu, Josh | 3f338c1 | 2013-04-16 23:42:44 +0000 | [diff] [blame] | 179 | || defined(CONFIG_AT91SAM9N12) || defined(CONFIG_AT91SAM9X5) |
Bo Shen | 42aafb3 | 2012-07-05 17:21:46 +0000 | [diff] [blame] | 180 | /* mdiv <==> divisor |
| 181 | * 0 <==> 1 |
| 182 | * 1 <==> 2 |
| 183 | * 2 <==> 4 |
| 184 | * 3 <==> 3 |
| 185 | */ |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 186 | gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) == |
Asen Dimov | 3062355 | 2010-03-18 13:46:45 +0200 | [diff] [blame] | 187 | (AT91_PMC_MCKR_MDIV_2 | AT91_PMC_MCKR_MDIV_4) |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 188 | ? freq / 3 |
| 189 | : freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8)); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 190 | #else |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 191 | gd->arch.mck_rate_hz = freq / |
| 192 | (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8)); |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 193 | #endif |
Simon Glass | e61accc | 2012-12-13 20:48:31 +0000 | [diff] [blame] | 194 | gd->arch.cpu_clk_rate_hz = freq; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 195 | |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 196 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 197 | } |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 198 | |
| 199 | #if !defined(AT91_PLL_LOCK_TIMEOUT) |
| 200 | #define AT91_PLL_LOCK_TIMEOUT 1000000 |
| 201 | #endif |
| 202 | |
| 203 | void at91_plla_init(u32 pllar) |
| 204 | { |
| 205 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 206 | |
| 207 | writel(pllar, &pmc->pllar); |
Bo Shen | ff87615 | 2015-03-27 14:23:33 +0800 | [diff] [blame] | 208 | while (!(readl(&pmc->sr) & AT91_PMC_LOCKA)) |
| 209 | ; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 210 | } |
| 211 | void at91_pllb_init(u32 pllbr) |
| 212 | { |
| 213 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 214 | |
| 215 | writel(pllbr, &pmc->pllbr); |
Bo Shen | ff87615 | 2015-03-27 14:23:33 +0800 | [diff] [blame] | 216 | while (!(readl(&pmc->sr) & AT91_PMC_LOCKB)) |
| 217 | ; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void at91_mck_init(u32 mckr) |
| 221 | { |
| 222 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 223 | u32 tmp; |
| 224 | |
| 225 | tmp = readl(&pmc->mckr); |
Bo Shen | ff87615 | 2015-03-27 14:23:33 +0800 | [diff] [blame] | 226 | tmp &= ~AT91_PMC_MCKR_PRES_MASK; |
| 227 | tmp |= mckr & AT91_PMC_MCKR_PRES_MASK; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 228 | writel(tmp, &pmc->mckr); |
Bo Shen | ff87615 | 2015-03-27 14:23:33 +0800 | [diff] [blame] | 229 | while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY)) |
| 230 | ; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 231 | |
Bo Shen | ff87615 | 2015-03-27 14:23:33 +0800 | [diff] [blame] | 232 | tmp = readl(&pmc->mckr); |
| 233 | tmp &= ~AT91_PMC_MCKR_MDIV_MASK; |
| 234 | tmp |= mckr & AT91_PMC_MCKR_MDIV_MASK; |
| 235 | writel(tmp, &pmc->mckr); |
| 236 | while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY)) |
| 237 | ; |
| 238 | |
| 239 | tmp = readl(&pmc->mckr); |
| 240 | tmp &= ~AT91_PMC_MCKR_PLLADIV_MASK; |
| 241 | tmp |= mckr & AT91_PMC_MCKR_PLLADIV_MASK; |
| 242 | writel(tmp, &pmc->mckr); |
| 243 | while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY)) |
| 244 | ; |
| 245 | |
| 246 | tmp = readl(&pmc->mckr); |
| 247 | tmp &= ~AT91_PMC_MCKR_CSS_MASK; |
| 248 | tmp |= mckr & AT91_PMC_MCKR_CSS_MASK; |
| 249 | writel(tmp, &pmc->mckr); |
| 250 | while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY)) |
| 251 | ; |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 252 | } |
Wenyou Yang | 0a30fcb | 2016-02-03 10:20:43 +0800 | [diff] [blame] | 253 | |
| 254 | int at91_pllb_clk_enable(u32 pllbr) |
| 255 | { |
| 256 | struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC; |
| 257 | ulong start_time, tmp_time; |
| 258 | |
| 259 | start_time = get_timer(0); |
| 260 | writel(pllbr, &pmc->pllbr); |
| 261 | while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != AT91_PMC_LOCKB) { |
| 262 | tmp_time = get_timer(0); |
| 263 | if ((tmp_time - start_time) > EN_PLLB_TIMEOUT) { |
| 264 | printf("ERROR: failed to enable PLLB\n"); |
| 265 | return -1; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | int at91_pllb_clk_disable(void) |
| 273 | { |
| 274 | struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC; |
| 275 | ulong start_time, tmp_time; |
| 276 | |
| 277 | start_time = get_timer(0); |
| 278 | writel(0, &pmc->pllbr); |
| 279 | while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != 0) { |
| 280 | tmp_time = get_timer(0); |
| 281 | if ((tmp_time - start_time) > EN_PLLB_TIMEOUT) { |
| 282 | printf("ERROR: failed to disable PLLB\n"); |
| 283 | return -1; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |