hal_lld.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_lld.c
  15. * @brief PLATFORM HAL subsystem low level driver source.
  16. *
  17. * @addtogroup HAL
  18. * @{
  19. */
  20. #include "hal.h"
  21. /*===========================================================================*/
  22. /* Driver local definitions. */
  23. /*===========================================================================*/
  24. #define SAMA_MOR_ONE (1 << 5)
  25. #define SAMA_MOR_MOSCXTST CKGR_MOR_MOSCXTST(0xFF)
  26. #define SAMA_PLLA_ONE (1 << 29)
  27. /*===========================================================================*/
  28. /* Driver exported variables. */
  29. /*===========================================================================*/
  30. /*===========================================================================*/
  31. /* Driver local variables and types. */
  32. /*===========================================================================*/
  33. /*===========================================================================*/
  34. /* Driver local functions. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Driver interrupt handlers. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Driver exported functions. */
  41. /*===========================================================================*/
  42. /**
  43. * @brief Low level HAL driver initialization.
  44. *
  45. * @notapi
  46. */
  47. void hal_lld_init(void) {
  48. #if (SAMA_HAL_IS_SECURE == TRUE) /* The Matrix is PAS and PMC is always configured secure */
  49. /* Disabling PMC write protection. */
  50. pmcDisableWP();
  51. PIT->PIT_MR &= ~(PIT_MR_PITEN | PIT_MR_PITIEN);
  52. /* Configures peripherals as not-secure */
  53. MATRIX0->MATRIX_SPSELR[0] = 0xFFFFFFFF;
  54. MATRIX0->MATRIX_SPSELR[1] = 0xFFFFFFFF;
  55. MATRIX0->MATRIX_SPSELR[2] = 0xFFFFFFFF;
  56. MATRIX1->MATRIX_SPSELR[0] = 0xFFFFFFFF;
  57. MATRIX1->MATRIX_SPSELR[1] = 0xFFFFFFFF;
  58. MATRIX1->MATRIX_SPSELR[2] = 0xFFFFFFFF;
  59. /* Configures PMC and RTC as secure */
  60. //mtxConfigPeriphSecurity(MATRIX1, ID_SYSC, SECURE_PER);
  61. mtxConfigPeriphSecurity(MATRIX0, ID_PMC, SECURE_PER);
  62. mtxConfigPeriphSecurity(MATRIX1, ID_SFC, SECURE_PER);
  63. mtxConfigPeriphSecurity(MATRIX1, ID_SFR, SECURE_PER);
  64. /* It isn't necessary to make L2CC secure. L2C-310 cache
  65. * controller is secure mode aware.*/
  66. /*mtxConfigPeriphSecurity(MATRIX0, ID_L2CC, SECURE_PER);*/
  67. mtxConfigPeriphSecurity(MATRIX1, ID_SFRBU, SECURE_PER);
  68. /*mtxConfigPeriphSecurity(MATRIX1, ID_CHIPID, SECURE_PER);*/
  69. /* Enabling matrix clock */
  70. pmcEnableH32MX();
  71. pmcEnableH64MX();
  72. /* Enabling write protection. */
  73. pmcEnableWP();
  74. #endif
  75. /* Advanced interrupt controller init */
  76. aicInit();
  77. #if defined(SAMA_DMA_REQUIRED)
  78. dmaInit();
  79. #endif
  80. }
  81. /**
  82. * @brief SAMA clocks and PLL initialization.
  83. * @note All the involved constants come from the file @p board.h.
  84. * @note This function should be invoked just after the system reset.
  85. *
  86. * @special
  87. */
  88. void sama_clock_init(void) {
  89. #if (!SAMA_NO_INIT && SAMA_HAL_IS_SECURE == TRUE)
  90. uint32_t mor, pllar, mckr, mainf;
  91. /* Disabling PMC write protection. */
  92. pmcDisableWP();
  93. /*
  94. * Enforcing the reset default configuration of clock tree.
  95. */
  96. /* Setting Slow Clock source to OSCRC. */
  97. SCKC->SCKC_CR = 0U;
  98. /* Enabling MOSCRC. */
  99. PMC->CKGR_MOR |= (CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCRCEN);
  100. while (!(PMC->PMC_SR & PMC_SR_MOSCRCS))
  101. ; /* Waits until MOSCRC is stable.*/
  102. /* Switching Main Oscillator Source to MOSRC. */
  103. mor = PMC->CKGR_MOR | CKGR_MOR_KEY_PASSWD;
  104. mor &= ~CKGR_MOR_MOSCSEL;
  105. mor |= SAMA_MOSC_MOSCRC;
  106. PMC->CKGR_MOR = mor;
  107. while (!(PMC->PMC_SR & PMC_SR_MOSCSELS))
  108. ; /* Waits until MOSCSEL has changed.*/
  109. /* Switching Master Clock source to Main Clock. */
  110. mckr = PMC->PMC_MCKR;
  111. mckr &= ~PMC_MCKR_CSS_Msk;
  112. mckr |= PMC_MCKR_CSS_MAIN_CLK;
  113. PMC->PMC_MCKR = mckr;
  114. while (!(PMC->PMC_SR & PMC_SR_MCKRDY))
  115. ; /* Waits until Master Clock is stable.*/
  116. /* Counter Clock Source to MOSCRC. */
  117. PMC->CKGR_MCFR &= ~CKGR_MCFR_CCSS;
  118. /*
  119. * Main oscillator configuration block.
  120. */
  121. /* Setting Slow clock source. */
  122. SCKC->SCKC_CR = SAMA_OSC_SEL;
  123. while ((SAMA_OSC_SEL && !(PMC->PMC_SR & PMC_SR_OSCSELS)) ||
  124. (!SAMA_OSC_SEL && (PMC->PMC_SR & PMC_SR_OSCSELS)))
  125. ; /* Waits until MOSCxxS switch is done.*/
  126. mor = PMC->CKGR_MOR | CKGR_MOR_KEY_PASSWD;
  127. #if SAMA_MOSCXT_ENABLED
  128. mor |= CKGR_MOR_MOSCXTEN;
  129. PMC->CKGR_MOR = mor;
  130. while (!(PMC->PMC_SR & PMC_SR_MOSCXTS))
  131. ; /* Waits until MOSCXT is stable.*/
  132. /* Counter Clock Source to MOSCXT. */
  133. PMC->CKGR_MCFR |= CKGR_MCFR_CCSS;
  134. #else
  135. mor &= ~CKGR_MOR_MOSCXTEN;
  136. PMC->CKGR_MOR = mor;
  137. #endif
  138. PMC->CKGR_MCFR |= CKGR_MCFR_RCMEAS;
  139. while (!(PMC->CKGR_MCFR & CKGR_MCFR_MAINFRDY))
  140. ;
  141. mainf = CKGR_MCFR_MAINF(PMC->CKGR_MCFR);
  142. /*
  143. * @TODO: add mainf check and eventual clock source fallback. This mechanism
  144. * should be activable through a switch.
  145. */
  146. (void)mainf;
  147. /* Switching Main Clock source. */
  148. mor &= ~CKGR_MOR_MOSCSEL;
  149. mor |= SAMA_MOSC_SEL;
  150. PMC->CKGR_MOR = mor;
  151. /* Eventually disabling MOSCRC. */
  152. #if !SAMA_MOSCRC_ENABLED
  153. PMC->CKGR_MOR &= ~ CKGR_MOR_MOSCRCEN;
  154. #endif
  155. /*
  156. * PLLA configuration block.
  157. */
  158. pllar = SAMA_PLLA_ONE | CKGR_PLLAR_PLLACOUNT(0x3F);
  159. #if SAMA_ACTIVATE_PLLA
  160. pllar |= CKGR_PLLAR_DIVA_BYPASS | SAMA_PLLA_MUL;
  161. #endif
  162. PMC->CKGR_PLLAR = pllar; /* Writing PLLA register. */
  163. #if SAMA_ACTIVATE_PLLA
  164. while (!(PMC->PMC_SR & PMC_SR_LOCKA))
  165. ; /* Waits until PLLA is locked. */
  166. #endif
  167. /*
  168. * Master clock configuration block.
  169. */
  170. mckr = PMC->PMC_MCKR;
  171. mckr &= ~PMC_MCKR_CSS_Msk;
  172. mckr |= SAMA_MCK_SEL;
  173. PMC->PMC_MCKR = mckr;
  174. while (!(PMC->PMC_SR & PMC_SR_MCKRDY))
  175. ; /* Waits until MCK is stable. */
  176. mckr &= ~(PMC_MCKR_PRES_Msk | PMC_MCKR_MDIV_Msk | PMC_MCKR_H32MXDIV);
  177. /* Note that prescaler and divider must be changed with separate accesses.*/
  178. mckr |= SAMA_MCK_PRES;
  179. mckr |= SAMA_MCK_MDIV;
  180. mckr |= SAMA_H64MX_H32MX_DIV;
  181. #if SAMA_PLLADIV2_EN
  182. mckr |= PMC_MCKR_PLLADIV2;
  183. #else
  184. mckr &= ~PMC_MCKR_PLLADIV2;
  185. #endif
  186. PMC->PMC_MCKR = mckr;
  187. while (!(PMC->PMC_SR & PMC_SR_MCKRDY))
  188. ; /* Waits until MCK is stable. */
  189. /* Enabling write protection. */
  190. pmcEnableWP();
  191. #endif /* !SAMA_NO_INIT */
  192. }
  193. /** @} */