sama_matrix.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 SAMA5D2x/sama_matrix.c
  15. * @brief SAMA MATRIX support code.
  16. *
  17. * @addtogroup SAMA5D2x_MATRIX
  18. * @{
  19. */
  20. #include "hal.h"
  21. /*===========================================================================*/
  22. /* Driver local definitions. */
  23. /*===========================================================================*/
  24. /*===========================================================================*/
  25. /* Driver exported variables. */
  26. /*===========================================================================*/
  27. /*===========================================================================*/
  28. /* Driver local types. */
  29. /*===========================================================================*/
  30. /*===========================================================================*/
  31. /* Driver local variables. */
  32. /*===========================================================================*/
  33. /*===========================================================================*/
  34. /* Driver constant */
  35. /*===========================================================================*/
  36. #define SCFG_OFFSET 0x40u
  37. /*===========================================================================*/
  38. /* Driver local macros. */
  39. /*===========================================================================*/
  40. #define MATRIX_SCFG(value) (MATRIX_SCFG0 + (value * 4u))
  41. #define MATRIX_SCFG_FIXED_DEFMSTR(value) MATRIX_SCFG0_FIXED_DEFMSTR(value)
  42. #define MATRIX_SCFG_DEFMSTR_TYPE(value) MATRIX_SCFG0_DEFMSTR_TYPE(value)
  43. /**
  44. * @brief Enable write protection on MATRIX registers block.
  45. *
  46. * @param[in] mtxp pointer to a MATRIX register block.
  47. *
  48. * @notapi
  49. */
  50. #define mtxEnableWP(mtxp) { \
  51. mtxp->MATRIX_WPMR = MATRIX_WPMR_WPKEY_PASSWD | MATRIX_WPMR_WPEN; \
  52. }
  53. /**
  54. * @brief Disable write protection on MATRIX registers block.
  55. *
  56. * @param[in] matxp pointer to a MATRIX register block.
  57. *
  58. * @notapi
  59. */
  60. #define mtxDisableWP(mtxp) { \
  61. mtxp->MATRIX_WPMR = MATRIX_WPMR_WPKEY_PASSWD; \
  62. }
  63. /*===========================================================================*/
  64. /* Driver exported functions. */
  65. /*===========================================================================*/
  66. /**
  67. * @brief Configures peripheral security
  68. *
  69. * @param[in] mtxp pointer to a MATRIX register block.
  70. * @param[in] id PERIPHERAL_ID.
  71. * @param[in] mode SECURE_PER or NOT_SECURE_PER.
  72. *
  73. * @retval true Peripheral is not secured.
  74. * @retval false Peripheral is secured.
  75. *
  76. */
  77. bool mtxConfigPeriphSecurity(Matrix *mtxp, uint32_t id, bool mode) {
  78. uint32_t mask;
  79. mask = id & 0x1F;
  80. mtxDisableWP(mtxp);
  81. if (mode) {
  82. mtxp->MATRIX_SPSELR[id / 32] |= (MATRIX_SPSELR_NSECP0 << mask);
  83. }
  84. else {
  85. mtxp->MATRIX_SPSELR[id / 32] &= ~(MATRIX_SPSELR_NSECP0 << mask);
  86. }
  87. mtxEnableWP(mtxp);
  88. return (MATRIX0->MATRIX_SPSELR[id / 32] & (MATRIX_SPSELR_NSECP0 << mask)) &
  89. (MATRIX1->MATRIX_SPSELR[id / 32] & (MATRIX_SPSELR_NSECP0 << mask));
  90. }
  91. /**
  92. * @brief Associates slave with a kind of master
  93. * @note masterID is set only if type is fixed default master.
  94. * Specifying the number of a master which is not connected
  95. * to the selected slave is equivalent to clearing DEFMSTR_TYPE.
  96. *
  97. * @param[in] mtxp pointer to a MATRIX register block.
  98. * @param[in] slaveID Slave MATRIX ID.
  99. * @param[in] type Select from
  100. * No default master,
  101. * Last access master,
  102. * Fixed default master.
  103. * @param[in] masterID Master MATRIX ID.
  104. */
  105. void mtxConfigDefaultMaster(Matrix *mtxp, uint8_t slaveID,
  106. uint8_t type, uint8_t masterID) {
  107. mtxDisableWP(mtxp);
  108. volatile uint32_t *scfgAddress = (uint32_t *) ((uint32_t) mtxp + SCFG_OFFSET + (4u * slaveID));
  109. *scfgAddress = MATRIX_SCFG_DEFMSTR_TYPE(type);
  110. if (type == FIXED_DEFAULT_MASTER) {
  111. *scfgAddress = MATRIX_SCFG_FIXED_DEFMSTR(masterID);
  112. }
  113. mtxEnableWP(mtxp);
  114. }
  115. /**
  116. * @brief Configures slave security region
  117. *
  118. * @param[in] mtxp pointer to a MATRIX register block.
  119. * @param[in] slaveID Slave MATRIX ID.
  120. * @param[in] selMask Securable area.
  121. * @param[in] readMask Secure for read.
  122. * @param[in] writeMask Secure for write.
  123. */
  124. void mtxConfigSlaveSec(Matrix *mtxp, uint8_t slaveID,
  125. uint8_t selMask, uint8_t readMask,
  126. uint8_t writeMask) {
  127. mtxDisableWP(mtxp);
  128. mtxp->MATRIX_SSR[slaveID] = selMask | (readMask << 8) |
  129. (writeMask << 16);
  130. mtxEnableWP(mtxp);
  131. }
  132. /**
  133. * @brief Configures split area of region
  134. *
  135. * @param[in] mtxp pointer to a MATRIX register block.
  136. * @param[in] slaveID Slave MATRIX ID.
  137. * @param[in] areaSize Split size area.
  138. * @param[in] mask Region securable area.
  139. */
  140. void mtxSetSlaveSplitAddr(Matrix *mtxp, uint8_t slaveID,
  141. uint8_t areaSize, uint8_t mask) {
  142. mtxDisableWP(mtxp);
  143. uint8_t i = mask, j = 0;
  144. uint32_t value = 0;
  145. uint32_t pmask = 0;
  146. for (i = 1; (i <= mask) && (j < 32); i <<= 1, j += 4) {
  147. if (i & mask) {
  148. value |= areaSize << j;
  149. pmask |= 0x0F << j;
  150. }
  151. }
  152. mtxp->MATRIX_SASSR[slaveID] = (mtxp->MATRIX_SASSR[slaveID] & ~pmask) | value;
  153. mtxEnableWP(mtxp);
  154. }
  155. /**
  156. * @brief Configures size area of region
  157. * @note Not applicable to internal security type
  158. *
  159. * @param[in] mtxp pointer to a MATRIX register block.
  160. * @param[in] slaveID Slave MATRIX ID.
  161. * @param[in] areaSize Size of total area.
  162. * @param[in] mask Region securable area.
  163. */
  164. void mtxSetSlaveRegionSize(Matrix *mtxp, uint8_t slaveID,
  165. uint8_t areaSize, uint8_t mask) {
  166. osalDbgCheck(slaveID != 0);
  167. mtxDisableWP(mtxp);
  168. uint8_t i = mask, j = 0;
  169. uint32_t value = 0;
  170. uint32_t pmask = 0;
  171. for (i = 1; (i <= mask) && (j < 32 ); i <<= 1, j += 4) {
  172. if (i & mask) {
  173. value |= areaSize << j;
  174. pmask |= 0x0F << j;
  175. }
  176. }
  177. mtxp->MATRIX_SRTSR[slaveID] = (mtxp->MATRIX_SRTSR[slaveID] & ~pmask) | value;
  178. mtxEnableWP(mtxp);
  179. }
  180. /**
  181. * @brief Changes the mapping of the chip so that the remap area
  182. * mirrors the internal ROM or the EBI CS0.
  183. */
  184. void mtxRemapRom(void) {
  185. AXIMX->AXIMX_REMAP = 0;
  186. /* Invalidate I-Cache*/
  187. L1C_InvalidateICacheAll();
  188. /* Invalidate Region */
  189. cacheInvalidateRegion((void*)0, IRAM_SIZE);
  190. }
  191. /**
  192. * @brief Changes the mapping of the chip so that the remap area
  193. * mirrors the internal ROM or the EBI CS0.
  194. */
  195. void mtxRemapRam(void) {
  196. AXIMX->AXIMX_REMAP = AXIMX_REMAP_REMAP0;
  197. /* Invalidate I-Cache*/
  198. L1C_InvalidateICacheAll();
  199. /* Clean I-Region */
  200. cacheCleanRegion((void*)IRAM_ADDR, IRAM_SIZE);
  201. /* Invalidate Region */
  202. cacheInvalidateRegion((void*)0, IRAM_SIZE);
  203. }
  204. /** @} */