1
0

system_clock.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * File : clock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-01-13 weety first version
  13. */
  14. #include <rtthread.h>
  15. #include "rt_list.h"
  16. #include "at91sam926x.h"
  17. static rt_list_t clocks;
  18. struct clk {
  19. char name[32];
  20. rt_uint32_t rate_hz;
  21. struct clk *parent;
  22. rt_list_t node;
  23. };
  24. static struct clk clk32k = {
  25. .name = "clk32k",
  26. .rate_hz = AT91_SLOW_CLOCK,
  27. };
  28. static struct clk main_clk = {
  29. .name = "main",
  30. };
  31. static struct clk plla = {
  32. .name = "plla",
  33. };
  34. static struct clk mck = {
  35. .name = "mck",
  36. };
  37. static struct clk uhpck = {
  38. .name = "uhpck",
  39. };
  40. static struct clk pllb = {
  41. .name = "pllb",
  42. .parent = &main_clk,
  43. };
  44. static struct clk udpck = {
  45. .name = "udpck",
  46. .parent = &pllb,
  47. };
  48. static struct clk *const standard_pmc_clocks[] = {
  49. /* four primary clocks */
  50. &clk32k,
  51. &main_clk,
  52. &plla,
  53. /* MCK */
  54. &mck
  55. };
  56. /* clocks cannot be de-registered no refcounting necessary */
  57. struct clk *clk_get(const char *id)
  58. {
  59. struct clk *clk;
  60. rt_list_t *list;
  61. for (list = (&clocks)->next; list != &clocks; list = list->next)
  62. {
  63. clk = (struct clk *)rt_list_entry(list, struct clk, node);
  64. if (strcmp(id, clk->name) == 0)
  65. return clk;
  66. }
  67. return RT_NULL;
  68. }
  69. rt_uint32_t clk_get_rate(struct clk *clk)
  70. {
  71. rt_uint32_t flags;
  72. rt_uint32_t rate;
  73. for (;;) {
  74. rate = clk->rate_hz;
  75. if (rate || !clk->parent)
  76. break;
  77. clk = clk->parent;
  78. }
  79. return rate;
  80. }
  81. static rt_uint32_t at91_pll_rate(struct clk *pll, rt_uint32_t freq, rt_uint32_t reg)
  82. {
  83. unsigned mul, div;
  84. div = reg & 0xff;
  85. mul = (reg >> 16) & 0x7ff;
  86. if (div && mul) {
  87. freq /= div;
  88. freq *= mul + 1;
  89. } else
  90. freq = 0;
  91. return freq;
  92. }
  93. static unsigned at91_pll_calc(unsigned main_freq, unsigned out_freq)
  94. {
  95. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  96. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  97. /* PLL output max 240 MHz (or 180 MHz per errata) */
  98. if (out_freq > 240000000)
  99. goto fail;
  100. for (i = 1; i < 256; i++) {
  101. int diff1;
  102. unsigned input, mul1;
  103. /*
  104. * PLL input between 1MHz and 32MHz per spec, but lower
  105. * frequences seem necessary in some cases so allow 100K.
  106. * Warning: some newer products need 2MHz min.
  107. */
  108. input = main_freq / i;
  109. if (input < 100000)
  110. continue;
  111. if (input > 32000000)
  112. continue;
  113. mul1 = out_freq / input;
  114. if (mul1 > 2048)
  115. continue;
  116. if (mul1 < 2)
  117. goto fail;
  118. diff1 = out_freq - input * mul1;
  119. if (diff1 < 0)
  120. diff1 = -diff1;
  121. if (diff > diff1) {
  122. diff = diff1;
  123. div = i;
  124. mul = mul1;
  125. if (diff == 0)
  126. break;
  127. }
  128. }
  129. if (i == 256 && diff > (out_freq >> 5))
  130. goto fail;
  131. return ret | ((mul - 1) << 16) | div;
  132. fail:
  133. return 0;
  134. }
  135. static rt_uint32_t at91_usb_rate(struct clk *pll, rt_uint32_t freq, rt_uint32_t reg)
  136. {
  137. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  138. return freq / 2;
  139. else
  140. return freq;
  141. }
  142. /* PLLB generated USB full speed clock init */
  143. static void at91_pllb_usbfs_clock_init(rt_uint32_t main_clock)
  144. {
  145. rt_uint32_t at91_pllb_usb_init;
  146. /*
  147. * USB clock init: choose 48 MHz PLLB value,
  148. * disable 48MHz clock during usb peripheral suspend.
  149. *
  150. * REVISIT: assumes MCK doesn't derive from PLLB!
  151. */
  152. uhpck.parent = &pllb;
  153. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  154. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  155. at91_sys_write(AT91_CKGR_PLLBR, 0);
  156. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  157. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  158. }
  159. static struct clk *at91_css_to_clk(unsigned long css)
  160. {
  161. switch (css) {
  162. case AT91_PMC_CSS_SLOW:
  163. return &clk32k;
  164. case AT91_PMC_CSS_MAIN:
  165. return &main_clk;
  166. case AT91_PMC_CSS_PLLA:
  167. return &plla;
  168. case AT91_PMC_CSS_PLLB:
  169. return &pllb;
  170. }
  171. return RT_NULL;
  172. }
  173. #define false 0
  174. #define true 1
  175. int at91_clock_init(rt_uint32_t main_clock)
  176. {
  177. unsigned tmp, freq, mckr;
  178. int i;
  179. int pll_overclock = false;
  180. /*
  181. * When the bootloader initialized the main oscillator correctly,
  182. * there's no problem using the cycle counter. But if it didn't,
  183. * or when using oscillator bypass mode, we must be told the speed
  184. * of the main clock.
  185. */
  186. if (!main_clock) {
  187. do {
  188. tmp = at91_sys_read(AT91_CKGR_MCFR);
  189. } while (!(tmp & AT91_PMC_MAINRDY));
  190. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  191. }
  192. main_clk.rate_hz = main_clock;
  193. /* report if PLLA is more than mildly overclocked */
  194. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  195. if (plla.rate_hz > 209000000)
  196. pll_overclock = true;
  197. if (pll_overclock)
  198. ;//rt_kprintf("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  199. at91_pllb_usbfs_clock_init(main_clock);
  200. /*
  201. * MCK and CPU derive from one of those primary clocks.
  202. * For now, assume this parentage won't change.
  203. */
  204. mckr = at91_sys_read(AT91_PMC_MCKR);
  205. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  206. freq = mck.parent->rate_hz;
  207. freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2)); /* prescale */
  208. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  209. /* Register the PMC's standard clocks */
  210. rt_list_init(&clocks);
  211. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  212. rt_list_insert_after(&clocks, &standard_pmc_clocks[i]->node);
  213. rt_list_insert_after(&clocks, &pllb.node);
  214. rt_list_insert_after(&clocks, &uhpck.node);
  215. rt_list_insert_after(&clocks, &udpck.node);
  216. /* MCK and CPU clock are "always on" */
  217. //clk_enable(&mck);
  218. /*rt_kprintf("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  219. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  220. (unsigned) main_clock / 1000000,
  221. ((unsigned) main_clock % 1000000) / 1000);*///cause blocked
  222. return 0;
  223. }
  224. /**
  225. * @brief System Clock Configuration
  226. */
  227. void rt_hw_clock_init(void)
  228. {
  229. at91_clock_init(18432000);
  230. }