My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pll.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * \file
  3. *
  4. * \brief Chip-specific PLL definitions.
  5. *
  6. * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="https://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef CHIP_PLL_H_INCLUDED
  47. #define CHIP_PLL_H_INCLUDED
  48. #include "osc.h"
  49. /// @cond 0
  50. /**INDENT-OFF**/
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**INDENT-ON**/
  55. /// @endcond
  56. /**
  57. * \weakgroup pll_group
  58. * @{
  59. */
  60. #define PLL_OUTPUT_MIN_HZ 84000000
  61. #define PLL_OUTPUT_MAX_HZ 192000000
  62. #define PLL_INPUT_MIN_HZ 8000000
  63. #define PLL_INPUT_MAX_HZ 16000000
  64. #define NR_PLLS 2
  65. #define PLLA_ID 0
  66. #define UPLL_ID 1 //!< USB UTMI PLL.
  67. #define PLL_UPLL_HZ 480000000
  68. #define PLL_COUNT 0x3FU
  69. enum pll_source {
  70. PLL_SRC_MAINCK_4M_RC = OSC_MAINCK_4M_RC, //!< Internal 4MHz RC oscillator.
  71. PLL_SRC_MAINCK_8M_RC = OSC_MAINCK_8M_RC, //!< Internal 8MHz RC oscillator.
  72. PLL_SRC_MAINCK_12M_RC = OSC_MAINCK_12M_RC, //!< Internal 12MHz RC oscillator.
  73. PLL_SRC_MAINCK_XTAL = OSC_MAINCK_XTAL, //!< External crystal oscillator.
  74. PLL_SRC_MAINCK_BYPASS = OSC_MAINCK_BYPASS, //!< External bypass oscillator.
  75. PLL_NR_SOURCES, //!< Number of PLL sources.
  76. };
  77. struct pll_config {
  78. uint32_t ctrl;
  79. };
  80. #define pll_get_default_rate(pll_id) \
  81. ((osc_get_rate(CONFIG_PLL##pll_id##_SOURCE) \
  82. * CONFIG_PLL##pll_id##_MUL) \
  83. / CONFIG_PLL##pll_id##_DIV)
  84. /* Force UTMI PLL parameters (Hardware defined) */
  85. #ifdef CONFIG_PLL1_SOURCE
  86. # undef CONFIG_PLL1_SOURCE
  87. #endif
  88. #ifdef CONFIG_PLL1_MUL
  89. # undef CONFIG_PLL1_MUL
  90. #endif
  91. #ifdef CONFIG_PLL1_DIV
  92. # undef CONFIG_PLL1_DIV
  93. #endif
  94. #define CONFIG_PLL1_SOURCE PLL_SRC_MAINCK_XTAL
  95. #define CONFIG_PLL1_MUL 0
  96. #define CONFIG_PLL1_DIV 0
  97. /**
  98. * \note The SAM3X PLL hardware interprets mul as mul+1. For readability the hardware mul+1
  99. * is hidden in this implementation. Use mul as mul effective value.
  100. */
  101. static inline void pll_config_init(struct pll_config *p_cfg,
  102. enum pll_source e_src, uint32_t ul_div, uint32_t ul_mul)
  103. {
  104. uint32_t vco_hz;
  105. Assert(e_src < PLL_NR_SOURCES);
  106. if (ul_div == 0 && ul_mul == 0) { /* Must only be true for UTMI PLL */
  107. p_cfg->ctrl = CKGR_UCKR_UPLLCOUNT(PLL_COUNT);
  108. } else { /* PLLA */
  109. /* Calculate internal VCO frequency */
  110. vco_hz = osc_get_rate(e_src) / ul_div;
  111. Assert(vco_hz >= PLL_INPUT_MIN_HZ);
  112. Assert(vco_hz <= PLL_INPUT_MAX_HZ);
  113. vco_hz *= ul_mul;
  114. Assert(vco_hz >= PLL_OUTPUT_MIN_HZ);
  115. Assert(vco_hz <= PLL_OUTPUT_MAX_HZ);
  116. /* PMC hardware will automatically make it mul+1 */
  117. p_cfg->ctrl = CKGR_PLLAR_MULA(ul_mul - 1) | CKGR_PLLAR_DIVA(ul_div) | CKGR_PLLAR_PLLACOUNT(PLL_COUNT);
  118. }
  119. }
  120. #define pll_config_defaults(cfg, pll_id) \
  121. pll_config_init(cfg, \
  122. CONFIG_PLL##pll_id##_SOURCE, \
  123. CONFIG_PLL##pll_id##_DIV, \
  124. CONFIG_PLL##pll_id##_MUL)
  125. static inline void pll_config_read(struct pll_config *p_cfg, uint32_t ul_pll_id)
  126. {
  127. Assert(ul_pll_id < NR_PLLS);
  128. if (ul_pll_id == PLLA_ID) {
  129. p_cfg->ctrl = PMC->CKGR_PLLAR;
  130. } else {
  131. p_cfg->ctrl = PMC->CKGR_UCKR;
  132. }
  133. }
  134. static inline void pll_config_write(const struct pll_config *p_cfg, uint32_t ul_pll_id)
  135. {
  136. Assert(ul_pll_id < NR_PLLS);
  137. if (ul_pll_id == PLLA_ID) {
  138. pmc_disable_pllack(); // Always stop PLL first!
  139. PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | p_cfg->ctrl;
  140. } else {
  141. PMC->CKGR_UCKR = p_cfg->ctrl;
  142. }
  143. }
  144. static inline void pll_enable(const struct pll_config *p_cfg, uint32_t ul_pll_id)
  145. {
  146. Assert(ul_pll_id < NR_PLLS);
  147. if (ul_pll_id == PLLA_ID) {
  148. pmc_disable_pllack(); // Always stop PLL first!
  149. PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | p_cfg->ctrl;
  150. } else {
  151. PMC->CKGR_UCKR = p_cfg->ctrl | CKGR_UCKR_UPLLEN;
  152. }
  153. }
  154. /**
  155. * \note This will only disable the selected PLL, not the underlying oscillator (mainck).
  156. */
  157. static inline void pll_disable(uint32_t ul_pll_id)
  158. {
  159. Assert(ul_pll_id < NR_PLLS);
  160. if (ul_pll_id == PLLA_ID) {
  161. pmc_disable_pllack();
  162. } else {
  163. PMC->CKGR_UCKR &= ~CKGR_UCKR_UPLLEN;
  164. }
  165. }
  166. static inline uint32_t pll_is_locked(uint32_t ul_pll_id)
  167. {
  168. Assert(ul_pll_id < NR_PLLS);
  169. if (ul_pll_id == PLLA_ID) {
  170. return pmc_is_locked_pllack();
  171. } else {
  172. return pmc_is_locked_upll();
  173. }
  174. }
  175. static inline void pll_enable_source(enum pll_source e_src)
  176. {
  177. switch (e_src) {
  178. case PLL_SRC_MAINCK_4M_RC:
  179. case PLL_SRC_MAINCK_8M_RC:
  180. case PLL_SRC_MAINCK_12M_RC:
  181. case PLL_SRC_MAINCK_XTAL:
  182. case PLL_SRC_MAINCK_BYPASS:
  183. osc_enable(e_src);
  184. osc_wait_ready(e_src);
  185. break;
  186. default:
  187. Assert(false);
  188. break;
  189. }
  190. }
  191. static inline void pll_enable_config_defaults(unsigned int ul_pll_id)
  192. {
  193. struct pll_config pllcfg;
  194. if (pll_is_locked(ul_pll_id)) {
  195. return; // Pll already running
  196. }
  197. switch (ul_pll_id) {
  198. #ifdef CONFIG_PLL0_SOURCE
  199. case 0:
  200. pll_enable_source(CONFIG_PLL0_SOURCE);
  201. pll_config_init(&pllcfg,
  202. CONFIG_PLL0_SOURCE,
  203. CONFIG_PLL0_DIV,
  204. CONFIG_PLL0_MUL);
  205. break;
  206. #endif
  207. #ifdef CONFIG_PLL1_SOURCE
  208. case 1:
  209. pll_enable_source(CONFIG_PLL1_SOURCE);
  210. pll_config_init(&pllcfg,
  211. CONFIG_PLL1_SOURCE,
  212. CONFIG_PLL1_DIV,
  213. CONFIG_PLL1_MUL);
  214. break;
  215. #endif
  216. default:
  217. Assert(false);
  218. break;
  219. }
  220. pll_enable(&pllcfg, ul_pll_id);
  221. while (!pll_is_locked(ul_pll_id));
  222. }
  223. /**
  224. * \brief Wait for PLL \a pll_id to become locked
  225. *
  226. * \todo Use a timeout to avoid waiting forever and hanging the system
  227. *
  228. * \param pll_id The ID of the PLL to wait for.
  229. *
  230. * \retval STATUS_OK The PLL is now locked.
  231. * \retval ERR_TIMEOUT Timed out waiting for PLL to become locked.
  232. */
  233. static inline int pll_wait_for_lock(unsigned int pll_id)
  234. {
  235. Assert(pll_id < NR_PLLS);
  236. while (!pll_is_locked(pll_id)) {
  237. /* Do nothing */
  238. }
  239. return 0;
  240. }
  241. //! @}
  242. /// @cond 0
  243. /**INDENT-OFF**/
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. /**INDENT-ON**/
  248. /// @endcond
  249. #endif /* CHIP_PLL_H_INCLUDED */