pll.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * \file
  3. *
  4. * \brief PLL management
  5. *
  6. * Copyright (c) 2010-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="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef CLK_PLL_H_INCLUDED
  47. #define CLK_PLL_H_INCLUDED
  48. #include "parts.h"
  49. #include "conf_clock.h"
  50. #if SAM3S
  51. # include "sam3s/pll.h"
  52. #elif SAM3XA
  53. # include "sam3x/pll.h"
  54. #elif SAM3U
  55. # include "sam3u/pll.h"
  56. #elif SAM3N
  57. # include "sam3n/pll.h"
  58. #elif SAM4S
  59. # include "sam4s/pll.h"
  60. #elif SAM4E
  61. # include "sam4e/pll.h"
  62. #elif SAM4C
  63. # include "sam4c/pll.h"
  64. #elif SAM4CM
  65. # include "sam4cm/pll.h"
  66. #elif SAM4CP
  67. # include "sam4cp/pll.h"
  68. #elif SAM4L
  69. # include "sam4l/pll.h"
  70. #elif SAM4N
  71. # include "sam4n/pll.h"
  72. #elif SAMG
  73. # include "samg/pll.h"
  74. #elif SAMV71
  75. # include "samv71/pll.h"
  76. #elif SAMV70
  77. # include "samv70/pll.h"
  78. #elif SAME70
  79. # include "same70/pll.h"
  80. #elif SAMS70
  81. # include "sams70/pll.h"
  82. #elif (UC3A0 || UC3A1)
  83. # include "uc3a0_a1/pll.h"
  84. #elif UC3A3
  85. # include "uc3a3_a4/pll.h"
  86. #elif UC3B
  87. # include "uc3b0_b1/pll.h"
  88. #elif UC3C
  89. # include "uc3c/pll.h"
  90. #elif UC3D
  91. # include "uc3d/pll.h"
  92. #elif (UC3L0128 || UC3L0256 || UC3L3_L4)
  93. # include "uc3l/pll.h"
  94. #elif XMEGA
  95. # include "xmega/pll.h"
  96. #else
  97. # error Unsupported chip type
  98. #endif
  99. /**
  100. * \ingroup clk_group
  101. * \defgroup pll_group PLL Management
  102. *
  103. * This group contains functions and definitions related to configuring
  104. * and enabling/disabling on-chip PLLs. A PLL will take an input signal
  105. * (the \em source), optionally divide the frequency by a configurable
  106. * \em divider, and then multiply the frequency by a configurable \em
  107. * multiplier.
  108. *
  109. * Some devices don't support input dividers; specifying any other
  110. * divisor than 1 on these devices will result in an assertion failure.
  111. * Other devices may have various restrictions to the frequency range of
  112. * the input and output signals.
  113. *
  114. * \par Example: Setting up PLL0 with default parameters
  115. *
  116. * The following example shows how to configure and enable PLL0 using
  117. * the default parameters specified using the configuration symbols
  118. * listed above.
  119. * \code
  120. pll_enable_config_defaults(0); \endcode
  121. *
  122. * To configure, enable PLL0 using the default parameters and to disable
  123. * a specific feature like Wide Bandwidth Mode (a UC3A3-specific
  124. * PLL option.), you can use this initialization process.
  125. * \code
  126. struct pll_config pllcfg;
  127. if (pll_is_locked(pll_id)) {
  128. return; // Pll already running
  129. }
  130. pll_enable_source(CONFIG_PLL0_SOURCE);
  131. pll_config_defaults(&pllcfg, 0);
  132. pll_config_set_option(&pllcfg, PLL_OPT_WBM_DISABLE);
  133. pll_enable(&pllcfg, 0);
  134. pll_wait_for_lock(0); \endcode
  135. *
  136. * When the last function call returns, PLL0 is ready to be used as the
  137. * main system clock source.
  138. *
  139. * \section pll_group_config Configuration Symbols
  140. *
  141. * Each PLL has a set of default parameters determined by the following
  142. * configuration symbols in the application's configuration file:
  143. * - \b CONFIG_PLLn_SOURCE: The default clock source connected to the
  144. * input of PLL \a n. Must be one of the values defined by the
  145. * #pll_source enum.
  146. * - \b CONFIG_PLLn_MUL: The default multiplier (loop divider) of PLL
  147. * \a n.
  148. * - \b CONFIG_PLLn_DIV: The default input divider of PLL \a n.
  149. *
  150. * These configuration symbols determine the result of calling
  151. * pll_config_defaults() and pll_get_default_rate().
  152. *
  153. * @{
  154. */
  155. //! \name Chip-specific PLL characteristics
  156. //@{
  157. /**
  158. * \def PLL_MAX_STARTUP_CYCLES
  159. * \brief Maximum PLL startup time in number of slow clock cycles
  160. */
  161. /**
  162. * \def NR_PLLS
  163. * \brief Number of on-chip PLLs
  164. */
  165. /**
  166. * \def PLL_MIN_HZ
  167. * \brief Minimum frequency that the PLL can generate
  168. */
  169. /**
  170. * \def PLL_MAX_HZ
  171. * \brief Maximum frequency that the PLL can generate
  172. */
  173. /**
  174. * \def PLL_NR_OPTIONS
  175. * \brief Number of PLL option bits
  176. */
  177. //@}
  178. /**
  179. * \enum pll_source
  180. * \brief PLL clock source
  181. */
  182. //! \name PLL configuration
  183. //@{
  184. /**
  185. * \struct pll_config
  186. * \brief Hardware-specific representation of PLL configuration.
  187. *
  188. * This structure contains one or more device-specific values
  189. * representing the current PLL configuration. The contents of this
  190. * structure is typically different from platform to platform, and the
  191. * user should not access any fields except through the PLL
  192. * configuration API.
  193. */
  194. /**
  195. * \fn void pll_config_init(struct pll_config *cfg,
  196. * enum pll_source src, unsigned int div, unsigned int mul)
  197. * \brief Initialize PLL configuration from standard parameters.
  198. *
  199. * \note This function may be defined inline because it is assumed to be
  200. * called very few times, and usually with constant parameters. Inlining
  201. * it will in such cases reduce the code size significantly.
  202. *
  203. * \param cfg The PLL configuration to be initialized.
  204. * \param src The oscillator to be used as input to the PLL.
  205. * \param div PLL input divider.
  206. * \param mul PLL loop divider (i.e. multiplier).
  207. *
  208. * \return A configuration which will make the PLL run at
  209. * (\a mul / \a div) times the frequency of \a src
  210. */
  211. /**
  212. * \def pll_config_defaults(cfg, pll_id)
  213. * \brief Initialize PLL configuration using default parameters.
  214. *
  215. * After this function returns, \a cfg will contain a configuration
  216. * which will make the PLL run at (CONFIG_PLLx_MUL / CONFIG_PLLx_DIV)
  217. * times the frequency of CONFIG_PLLx_SOURCE.
  218. *
  219. * \param cfg The PLL configuration to be initialized.
  220. * \param pll_id Use defaults for this PLL.
  221. */
  222. /**
  223. * \def pll_get_default_rate(pll_id)
  224. * \brief Get the default rate in Hz of \a pll_id
  225. */
  226. /**
  227. * \fn void pll_config_set_option(struct pll_config *cfg,
  228. * unsigned int option)
  229. * \brief Set the PLL option bit \a option in the configuration \a cfg.
  230. *
  231. * \param cfg The PLL configuration to be changed.
  232. * \param option The PLL option bit to be set.
  233. */
  234. /**
  235. * \fn void pll_config_clear_option(struct pll_config *cfg,
  236. * unsigned int option)
  237. * \brief Clear the PLL option bit \a option in the configuration \a cfg.
  238. *
  239. * \param cfg The PLL configuration to be changed.
  240. * \param option The PLL option bit to be cleared.
  241. */
  242. /**
  243. * \fn void pll_config_read(struct pll_config *cfg, unsigned int pll_id)
  244. * \brief Read the currently active configuration of \a pll_id.
  245. *
  246. * \param cfg The configuration object into which to store the currently
  247. * active configuration.
  248. * \param pll_id The ID of the PLL to be accessed.
  249. */
  250. /**
  251. * \fn void pll_config_write(const struct pll_config *cfg,
  252. * unsigned int pll_id)
  253. * \brief Activate the configuration \a cfg on \a pll_id
  254. *
  255. * \param cfg The configuration object representing the PLL
  256. * configuration to be activated.
  257. * \param pll_id The ID of the PLL to be updated.
  258. */
  259. //@}
  260. //! \name Interaction with the PLL hardware
  261. //@{
  262. /**
  263. * \fn void pll_enable(const struct pll_config *cfg,
  264. * unsigned int pll_id)
  265. * \brief Activate the configuration \a cfg and enable PLL \a pll_id.
  266. *
  267. * \param cfg The PLL configuration to be activated.
  268. * \param pll_id The ID of the PLL to be enabled.
  269. */
  270. /**
  271. * \fn void pll_disable(unsigned int pll_id)
  272. * \brief Disable the PLL identified by \a pll_id.
  273. *
  274. * After this function is called, the PLL identified by \a pll_id will
  275. * be disabled. The PLL configuration stored in hardware may be affected
  276. * by this, so if the caller needs to restore the same configuration
  277. * later, it should either do a pll_config_read() before disabling the
  278. * PLL, or remember the last configuration written to the PLL.
  279. *
  280. * \param pll_id The ID of the PLL to be disabled.
  281. */
  282. /**
  283. * \fn bool pll_is_locked(unsigned int pll_id)
  284. * \brief Determine whether the PLL is locked or not.
  285. *
  286. * \param pll_id The ID of the PLL to check.
  287. *
  288. * \retval true The PLL is locked and ready to use as a clock source
  289. * \retval false The PLL is not yet locked, or has not been enabled.
  290. */
  291. /**
  292. * \fn void pll_enable_source(enum pll_source src)
  293. * \brief Enable the source of the pll.
  294. * The source is enabled, if the source is not already running.
  295. *
  296. * \param src The ID of the PLL source to enable.
  297. */
  298. /**
  299. * \fn void pll_enable_config_defaults(unsigned int pll_id)
  300. * \brief Enable the pll with the default configuration.
  301. * PLL is enabled, if the PLL is not already locked.
  302. *
  303. * \param pll_id The ID of the PLL to enable.
  304. */
  305. /**
  306. * \brief Wait for PLL \a pll_id to become locked
  307. *
  308. * \todo Use a timeout to avoid waiting forever and hanging the system
  309. *
  310. * \param pll_id The ID of the PLL to wait for.
  311. *
  312. * \retval STATUS_OK The PLL is now locked.
  313. * \retval ERR_TIMEOUT Timed out waiting for PLL to become locked.
  314. */
  315. static inline int pll_wait_for_lock(unsigned int pll_id)
  316. {
  317. Assert(pll_id < NR_PLLS);
  318. while (!pll_is_locked(pll_id)) {
  319. /* Do nothing */
  320. }
  321. return 0;
  322. }
  323. //@}
  324. //! @}
  325. #endif /* CLK_PLL_H_INCLUDED */