system_clock.c 6.3 KB

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