clock.c 4.5 KB

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