clock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <common.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/hardware.h>
  16. #include <asm/arch/at91_pmc.h>
  17. #include <asm/arch/clk.h>
  18. #if !defined(CONFIG_AT91FAMILY)
  19. # error You need to define CONFIG_AT91FAMILY in your board config!
  20. #endif
  21. DECLARE_GLOBAL_DATA_PTR;
  22. static unsigned long at91_css_to_rate(unsigned long css)
  23. {
  24. switch (css) {
  25. case AT91_PMC_MCKR_CSS_SLOW:
  26. return CONFIG_SYS_AT91_SLOW_CLOCK;
  27. case AT91_PMC_MCKR_CSS_MAIN:
  28. return gd->arch.main_clk_rate_hz;
  29. case AT91_PMC_MCKR_CSS_PLLA:
  30. return gd->arch.plla_rate_hz;
  31. case AT91_PMC_MCKR_CSS_PLLB:
  32. return gd->arch.pllb_rate_hz;
  33. }
  34. return 0;
  35. }
  36. #ifdef CONFIG_USB_ATMEL
  37. static unsigned at91_pll_calc(unsigned main_freq, unsigned out_freq)
  38. {
  39. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  40. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  41. /* PLL output max 240 MHz (or 180 MHz per errata) */
  42. if (out_freq > 240000000)
  43. goto fail;
  44. for (i = 1; i < 256; i++) {
  45. int diff1;
  46. unsigned input, mul1;
  47. /*
  48. * PLL input between 1MHz and 32MHz per spec, but lower
  49. * frequences seem necessary in some cases so allow 100K.
  50. * Warning: some newer products need 2MHz min.
  51. */
  52. input = main_freq / i;
  53. #if defined(CONFIG_AT91SAM9G20)
  54. if (input < 2000000)
  55. continue;
  56. #endif
  57. if (input < 100000)
  58. continue;
  59. if (input > 32000000)
  60. continue;
  61. mul1 = out_freq / input;
  62. #if defined(CONFIG_AT91SAM9G20)
  63. if (mul > 63)
  64. continue;
  65. #endif
  66. if (mul1 > 2048)
  67. continue;
  68. if (mul1 < 2)
  69. goto fail;
  70. diff1 = out_freq - input * mul1;
  71. if (diff1 < 0)
  72. diff1 = -diff1;
  73. if (diff > diff1) {
  74. diff = diff1;
  75. div = i;
  76. mul = mul1;
  77. if (diff == 0)
  78. break;
  79. }
  80. }
  81. if (i == 256 && diff > (out_freq >> 5))
  82. goto fail;
  83. return ret | ((mul - 1) << 16) | div;
  84. fail:
  85. return 0;
  86. }
  87. #endif
  88. static u32 at91_pll_rate(u32 freq, u32 reg)
  89. {
  90. unsigned mul, div;
  91. div = reg & 0xff;
  92. mul = (reg >> 16) & 0x7ff;
  93. if (div && mul) {
  94. freq /= div;
  95. freq *= mul + 1;
  96. } else
  97. freq = 0;
  98. return freq;
  99. }
  100. int at91_clock_init(unsigned long main_clock)
  101. {
  102. unsigned freq, mckr;
  103. at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
  104. #ifndef CONFIG_SYS_AT91_MAIN_CLOCK
  105. unsigned tmp;
  106. /*
  107. * When the bootloader initialized the main oscillator correctly,
  108. * there's no problem using the cycle counter. But if it didn't,
  109. * or when using oscillator bypass mode, we must be told the speed
  110. * of the main clock.
  111. */
  112. if (!main_clock) {
  113. do {
  114. tmp = readl(&pmc->mcfr);
  115. } while (!(tmp & AT91_PMC_MCFR_MAINRDY));
  116. tmp &= AT91_PMC_MCFR_MAINF_MASK;
  117. main_clock = tmp * (CONFIG_SYS_AT91_SLOW_CLOCK / 16);
  118. }
  119. #endif
  120. gd->arch.main_clk_rate_hz = main_clock;
  121. /* report if PLLA is more than mildly overclocked */
  122. gd->arch.plla_rate_hz = at91_pll_rate(main_clock, readl(&pmc->pllar));
  123. #ifdef CONFIG_USB_ATMEL
  124. /*
  125. * USB clock init: choose 48 MHz PLLB value,
  126. * disable 48MHz clock during usb peripheral suspend.
  127. *
  128. * REVISIT: assumes MCK doesn't derive from PLLB!
  129. */
  130. gd->arch.at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) |
  131. AT91_PMC_PLLBR_USBDIV_2;
  132. gd->arch.pllb_rate_hz = at91_pll_rate(main_clock,
  133. gd->arch.at91_pllb_usb_init);
  134. #endif
  135. /*
  136. * MCK and CPU derive from one of those primary clocks.
  137. * For now, assume this parentage won't change.
  138. */
  139. mckr = readl(&pmc->mckr);
  140. #if defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
  141. || defined(CONFIG_AT91SAM9X5)
  142. /* plla divisor by 2 */
  143. gd->arch.plla_rate_hz /= (1 << ((mckr & 1 << 12) >> 12));
  144. #endif
  145. gd->arch.mck_rate_hz = at91_css_to_rate(mckr & AT91_PMC_MCKR_CSS_MASK);
  146. freq = gd->arch.mck_rate_hz;
  147. freq /= (1 << ((mckr & AT91_PMC_MCKR_PRES_MASK) >> 2)); /* prescale */
  148. #if defined(CONFIG_AT91SAM9G20)
  149. /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  150. gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ?
  151. freq / ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 7) : freq;
  152. if (mckr & AT91_PMC_MCKR_MDIV_MASK)
  153. freq /= 2; /* processor clock division */
  154. #elif defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) \
  155. || defined(CONFIG_AT91SAM9X5)
  156. /* mdiv <==> divisor
  157. * 0 <==> 1
  158. * 1 <==> 2
  159. * 2 <==> 4
  160. * 3 <==> 3
  161. */
  162. gd->arch.mck_rate_hz = (mckr & AT91_PMC_MCKR_MDIV_MASK) ==
  163. (AT91_PMC_MCKR_MDIV_2 | AT91_PMC_MCKR_MDIV_4)
  164. ? freq / 3
  165. : freq / (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
  166. #else
  167. gd->arch.mck_rate_hz = freq /
  168. (1 << ((mckr & AT91_PMC_MCKR_MDIV_MASK) >> 8));
  169. #endif
  170. gd->arch.cpu_clk_rate_hz = freq;
  171. return 0;
  172. }