pm_uc3a.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. * Portions Copyright Atmel Corporation, see the following note.
  35. */
  36. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions are met:
  40. *
  41. * 1. Redistributions of source code must retain the above copyright notice, this
  42. * list of conditions and the following disclaimer.
  43. *
  44. * 2. Redistributions in binary form must reproduce the above copyright notice,
  45. * this list of conditions and the following disclaimer in the documentation
  46. * and/or other materials provided with the distribution.
  47. *
  48. * 3. The name of Atmel may not be used to endorse or promote products derived
  49. * from this software without specific prior written permission.
  50. *
  51. * 4. This software may only be redistributed and used in connection with an Atmel
  52. * AVR product.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  55. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  56. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  57. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  58. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  59. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  60. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  61. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  62. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  63. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  64. *
  65. */
  66. #include <avr32/io.h>
  67. #include <arch/avr32.h>
  68. #include <arch/avr32/pm.h>
  69. #include <cfg/os.h>
  70. #include <cfg/clock.h>
  71. #include <arch/avr32.h>
  72. #include <dev/irqreg.h>
  73. #include <sys/timer.h>
  74. #include <arch/avr32/ihndlr.h>
  75. #include <avr32/io.h>
  76. #include "compiler.h"
  77. /*! \name PM Writable Bit-Field Registers
  78. */
  79. typedef union
  80. {
  81. unsigned long mcctrl;
  82. avr32_pm_mcctrl_t MCCTRL;
  83. } u_avr32_pm_mcctrl_t;
  84. typedef union
  85. {
  86. unsigned long cksel;
  87. avr32_pm_cksel_t CKSEL;
  88. } u_avr32_pm_cksel_t;
  89. typedef union
  90. {
  91. unsigned long pll;
  92. avr32_pm_pll_t PLL;
  93. } u_avr32_pm_pll_t;
  94. typedef union
  95. {
  96. unsigned long oscctrl0;
  97. avr32_pm_oscctrl0_t OSCCTRL0;
  98. } u_avr32_pm_oscctrl0_t;
  99. typedef union
  100. {
  101. unsigned long oscctrl1;
  102. avr32_pm_oscctrl1_t OSCCTRL1;
  103. } u_avr32_pm_oscctrl1_t;
  104. typedef union
  105. {
  106. unsigned long oscctrl32;
  107. avr32_pm_oscctrl32_t OSCCTRL32;
  108. } u_avr32_pm_oscctrl32_t;
  109. typedef union
  110. {
  111. unsigned long ier;
  112. avr32_pm_ier_t IER;
  113. } u_avr32_pm_ier_t;
  114. typedef union
  115. {
  116. unsigned long idr;
  117. avr32_pm_idr_t IDR;
  118. } u_avr32_pm_idr_t;
  119. typedef union
  120. {
  121. unsigned long icr;
  122. avr32_pm_icr_t ICR;
  123. } u_avr32_pm_icr_t;
  124. typedef union
  125. {
  126. unsigned long gcctrl;
  127. avr32_pm_gcctrl_t GCCTRL;
  128. } u_avr32_pm_gcctrl_t;
  129. typedef union
  130. {
  131. unsigned long rccr;
  132. avr32_pm_rccr_t RCCR;
  133. } u_avr32_pm_rccr_t;
  134. typedef union
  135. {
  136. unsigned long bgcr;
  137. avr32_pm_bgcr_t BGCR;
  138. } u_avr32_pm_bgcr_t;
  139. typedef union
  140. {
  141. unsigned long vregcr;
  142. avr32_pm_vregcr_t VREGCR;
  143. } u_avr32_pm_vregcr_t;
  144. typedef union
  145. {
  146. unsigned long bod;
  147. avr32_pm_bod_t BOD;
  148. } u_avr32_pm_bod_t;
  149. /*! \brief Sets the mode of the oscillator 0.
  150. *
  151. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  152. * \param mode Oscillator 0 mode (i.e. AVR32_PM_OSCCTRL0_MODE_x).
  153. */
  154. static inline void pm_set_osc0_mode(volatile avr32_pm_t *pm, unsigned int mode)
  155. {
  156. // Read
  157. u_avr32_pm_oscctrl0_t u_avr32_pm_oscctrl0 = {pm->oscctrl0};
  158. // Modify
  159. u_avr32_pm_oscctrl0.OSCCTRL0.mode = mode;
  160. // Write
  161. pm->oscctrl0 = u_avr32_pm_oscctrl0.oscctrl0;
  162. }
  163. void pm_enable_osc0_crystal(unsigned int fosc0)
  164. {
  165. pm_set_osc0_mode(&AVR32_PM, (fosc0 < 900000) ? AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G0 :
  166. (fosc0 < 3000000) ? AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G1 :
  167. (fosc0 < 8000000) ? AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G2 :
  168. AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G3);
  169. }
  170. void pm_switch_to_osc0(unsigned int fosc0, unsigned int startup)
  171. {
  172. pm_enable_osc0_crystal(fosc0); // Enable the Osc0 in crystal mode
  173. pm_enable_clk0(startup); // Crystal startup time - This parameter is critical and depends on the characteristics of the crystal
  174. pm_switch_to_clock(AVR32_PM_MCSEL_OSC0); // Then switch main clock to Osc0
  175. }
  176. void pm_enable_clk0(unsigned int startup)
  177. {
  178. // Read register
  179. u_avr32_pm_oscctrl0_t u_avr32_pm_oscctrl0 = {AVR32_PM.oscctrl0};
  180. // Modify
  181. u_avr32_pm_oscctrl0.OSCCTRL0.startup = startup;
  182. // Write back
  183. AVR32_PM.oscctrl0 = u_avr32_pm_oscctrl0.oscctrl0;
  184. AVR32_PM.mcctrl |= AVR32_PM_MCCTRL_OSC0EN_MASK;
  185. // Wait for clk0 ready
  186. while (!(AVR32_PM.poscsr & AVR32_PM_POSCSR_OSC0RDY_MASK));
  187. }
  188. void pm_switch_to_clock(unsigned long clock)
  189. {
  190. // Read
  191. u_avr32_pm_mcctrl_t u_avr32_pm_mcctrl = {AVR32_PM.mcctrl};
  192. // Modify
  193. u_avr32_pm_mcctrl.MCCTRL.mcsel = clock;
  194. // Write back
  195. AVR32_PM.mcctrl = u_avr32_pm_mcctrl.mcctrl;
  196. }
  197. void pm_cksel(volatile avr32_pm_t *pm,
  198. unsigned int pbadiv,
  199. unsigned int pbasel,
  200. unsigned int pbbdiv,
  201. unsigned int pbbsel,
  202. unsigned int hsbdiv,
  203. unsigned int hsbsel)
  204. {
  205. u_avr32_pm_cksel_t u_avr32_pm_cksel = {0};
  206. u_avr32_pm_cksel.CKSEL.cpusel = hsbsel;
  207. u_avr32_pm_cksel.CKSEL.cpudiv = hsbdiv;
  208. u_avr32_pm_cksel.CKSEL.hsbsel = hsbsel;
  209. u_avr32_pm_cksel.CKSEL.hsbdiv = hsbdiv;
  210. u_avr32_pm_cksel.CKSEL.pbasel = pbasel;
  211. u_avr32_pm_cksel.CKSEL.pbadiv = pbadiv;
  212. u_avr32_pm_cksel.CKSEL.pbbsel = pbbsel;
  213. u_avr32_pm_cksel.CKSEL.pbbdiv = pbbdiv;
  214. pm->cksel = u_avr32_pm_cksel.cksel;
  215. // Wait for ckrdy bit and then clear it
  216. while (!(pm->poscsr & AVR32_PM_POSCSR_CKRDY_MASK));
  217. }
  218. void pm_pll_setup(volatile avr32_pm_t *pm,
  219. unsigned int pll,
  220. unsigned int mul,
  221. unsigned int div,
  222. unsigned int osc,
  223. unsigned int lockcount)
  224. {
  225. u_avr32_pm_pll_t u_avr32_pm_pll = {0};
  226. u_avr32_pm_pll.PLL.pllosc = osc;
  227. u_avr32_pm_pll.PLL.plldiv = div;
  228. u_avr32_pm_pll.PLL.pllmul = mul;
  229. u_avr32_pm_pll.PLL.pllcount = lockcount;
  230. pm->pll[pll] = u_avr32_pm_pll.pll;
  231. }
  232. void pm_pll_set_option(volatile avr32_pm_t *pm,
  233. unsigned int pll,
  234. unsigned int pll_freq,
  235. unsigned int pll_div2,
  236. unsigned int pll_wbwdisable)
  237. {
  238. u_avr32_pm_pll_t u_avr32_pm_pll = {pm->pll[pll]};
  239. u_avr32_pm_pll.PLL.pllopt = pll_freq | (pll_div2 << 1) | (pll_wbwdisable << 2);
  240. pm->pll[pll] = u_avr32_pm_pll.pll;
  241. }
  242. void pm_pll_enable(volatile avr32_pm_t *pm,
  243. unsigned int pll)
  244. {
  245. pm->pll[pll] |= AVR32_PM_PLLEN_MASK;
  246. }
  247. void pm_wait_for_pll0_locked(volatile avr32_pm_t *pm)
  248. {
  249. while (!(pm->poscsr & AVR32_PM_POSCSR_LOCK0_MASK));
  250. }
  251. typedef union {
  252. unsigned long fcr;
  253. avr32_flashc_fcr_t FCR;
  254. } u_avr32_flashc_fcr_t;
  255. static void flashc_set_wait_state(unsigned int wait_state)
  256. {
  257. u_avr32_flashc_fcr_t u_avr32_flashc_fcr = { AVR32_FLASHC.fcr };
  258. u_avr32_flashc_fcr.FCR.fws = wait_state;
  259. AVR32_FLASHC.fcr = u_avr32_flashc_fcr.fcr;
  260. }
  261. void Avr32InitClockTree( void )
  262. {
  263. uint32_t CPUFrequency;
  264. /* Switch main clock to Oscillator 0 */
  265. pm_switch_to_osc0(OSC0_VAL, AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC);
  266. pm_pll_setup(&AVR32_PM, 0, /* use PLL0 */
  267. PLL_MUL_VAL, /* MUL */
  268. PLL_DIV_VAL, /* DIV */
  269. 0, /* Oscillator 0 */
  270. 16); /* lockcount in main clock for the PLL wait lock */
  271. /*
  272. * This function will set a PLL option.
  273. *
  274. * pm Base address of the Power Manager (i.e. &AVR32_PM)
  275. * pll PLL number 0
  276. * pll_freq Set to 1 for VCO frequency range 80-180MHz,
  277. * set to 0 for VCO frequency range 160-240Mhz.
  278. * pll_div2 Divide the PLL output frequency by 2 (this settings does
  279. * not change the FVCO value)
  280. * pll_wbwdisable 1 Disable the Wide-Bandwidth Mode (Wide-Bandwidth mode
  281. * allow a faster startup time and out-of-lock time). 0 to
  282. * enable the Wide-Bandwidth Mode.
  283. */
  284. pm_pll_set_option(&AVR32_PM, 0, /* use PLL0 */
  285. PLL_FREQ_VAL, /* pll_freq */
  286. PLL_DIV2_VAL, /* pll_div2 */
  287. PLL_WBWD_VAL); /* pll_wbwd */
  288. /* Enable PLL0 */
  289. pm_pll_enable(&AVR32_PM, 0);
  290. /* Wait for PLL0 locked */
  291. pm_wait_for_pll0_locked(&AVR32_PM);
  292. /* Create PBA, PBB and HSB clock */
  293. pm_cksel(&AVR32_PM, PLL_PBADIV_VAL, /* pbadiv */
  294. PLL_PBASEL_VAL, /* pbasel */
  295. PLL_PBBDIV_VAL, /* pbbdiv */
  296. PLL_PBBSEL_VAL, /* pbbsel */
  297. PLL_HSBDIV_VAL, /* hsbdiv */
  298. PLL_HSBSEL_VAL); /* hsbsel */
  299. /* Calculate CPU frequency */
  300. CPUFrequency = (OSC0_VAL * (PLL_MUL_VAL + 1)) / PLL_DIV_VAL;
  301. CPUFrequency = (PLL_DIV2_VAL == 0) ? CPUFrequency : CPUFrequency >> 1;
  302. if (PLL_HSBDIV_VAL > 0) {
  303. CPUFrequency = CPUFrequency >> (PLL_HSBSEL_VAL + 1);
  304. }
  305. if (CPUFrequency > AVR32_FLASHC_FWS_0_MAX_FREQ) {
  306. /*
  307. * Set one wait-state (WS) for the flash controller if the
  308. * HSB/CPU is more than AVR32_FLASHC_FWS_0_MAX_FREQ.
  309. */
  310. flashc_set_wait_state(1);
  311. }
  312. /* Switch PLL to main clock */
  313. pm_switch_to_clock(AVR32_PM_MCSEL_PLL0);
  314. }