system_clock.c 6.6 KB

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