1
0

system_clock.c 6.5 KB

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