system_clock.c 6.0 KB

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