fsl_sysmpu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_sysmpu.h"
  35. /*******************************************************************************
  36. * Variables
  37. ******************************************************************************/
  38. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  39. const clock_ip_name_t g_sysmpuClock[FSL_FEATURE_SOC_SYSMPU_COUNT] = SYSMPU_CLOCKS;
  40. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  41. /*******************************************************************************
  42. * Codes
  43. ******************************************************************************/
  44. void SYSMPU_Init(SYSMPU_Type *base, const sysmpu_config_t *config)
  45. {
  46. assert(config);
  47. uint8_t count;
  48. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  49. /* Un-gate SYSMPU clock */
  50. CLOCK_EnableClock(g_sysmpuClock[0]);
  51. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  52. /* Initializes the regions. */
  53. for (count = 1; count < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT; count++)
  54. {
  55. base->WORD[count][3] = 0; /* VLD/VID+PID. */
  56. base->WORD[count][0] = 0; /* Start address. */
  57. base->WORD[count][1] = 0; /* End address. */
  58. base->WORD[count][2] = 0; /* Access rights. */
  59. base->RGDAAC[count] = 0; /* Alternate access rights. */
  60. }
  61. /* SYSMPU configure. */
  62. while (config)
  63. {
  64. SYSMPU_SetRegionConfig(base, &(config->regionConfig));
  65. config = config->next;
  66. }
  67. /* Enable SYSMPU. */
  68. SYSMPU_Enable(base, true);
  69. }
  70. void SYSMPU_Deinit(SYSMPU_Type *base)
  71. {
  72. /* Disable SYSMPU. */
  73. SYSMPU_Enable(base, false);
  74. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  75. /* Gate the clock. */
  76. CLOCK_DisableClock(g_sysmpuClock[0]);
  77. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  78. }
  79. void SYSMPU_GetHardwareInfo(SYSMPU_Type *base, sysmpu_hardware_info_t *hardwareInform)
  80. {
  81. assert(hardwareInform);
  82. uint32_t cesReg = base->CESR;
  83. hardwareInform->hardwareRevisionLevel = (cesReg & SYSMPU_CESR_HRL_MASK) >> SYSMPU_CESR_HRL_SHIFT;
  84. hardwareInform->slavePortsNumbers = (cesReg & SYSMPU_CESR_NSP_MASK) >> SYSMPU_CESR_NSP_SHIFT;
  85. hardwareInform->regionsNumbers = (sysmpu_region_total_num_t)((cesReg & SYSMPU_CESR_NRGD_MASK) >> SYSMPU_CESR_NRGD_SHIFT);
  86. }
  87. void SYSMPU_SetRegionConfig(SYSMPU_Type *base, const sysmpu_region_config_t *regionConfig)
  88. {
  89. assert(regionConfig);
  90. assert(regionConfig->regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  91. uint32_t wordReg = 0;
  92. uint8_t msPortNum;
  93. uint8_t regNumber = regionConfig->regionNum;
  94. /* The start and end address of the region descriptor. */
  95. base->WORD[regNumber][0] = regionConfig->startAddress;
  96. base->WORD[regNumber][1] = regionConfig->endAddress;
  97. /* Set the privilege rights for master 0 ~ master 3. */
  98. for (msPortNum = 0; msPortNum < SYSMPU_MASTER_RWATTRIBUTE_START_PORT; msPortNum++)
  99. {
  100. wordReg |= SYSMPU_REGION_RWXRIGHTS_MASTER(
  101. msPortNum, (((uint32_t)regionConfig->accessRights1[msPortNum].superAccessRights << 3U) |
  102. (uint32_t)regionConfig->accessRights1[msPortNum].userAccessRights));
  103. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  104. wordReg |=
  105. SYSMPU_REGION_RWXRIGHTS_MASTER_PE(msPortNum, regionConfig->accessRights1[msPortNum].processIdentifierEnable);
  106. #endif /* FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER */
  107. }
  108. #if FSL_FEATURE_SYSMPU_MASTER_COUNT > SYSMPU_MASTER_RWATTRIBUTE_START_PORT
  109. /* Set the normal read write rights for master 4 ~ master 7. */
  110. for (msPortNum = SYSMPU_MASTER_RWATTRIBUTE_START_PORT; msPortNum < FSL_FEATURE_SYSMPU_MASTER_COUNT;
  111. msPortNum++)
  112. {
  113. wordReg |= SYSMPU_REGION_RWRIGHTS_MASTER(msPortNum,
  114. ((uint32_t)regionConfig->accessRights2[msPortNum - SYSMPU_MASTER_RWATTRIBUTE_START_PORT].readEnable << 1U |
  115. (uint32_t)regionConfig->accessRights2[msPortNum - SYSMPU_MASTER_RWATTRIBUTE_START_PORT].writeEnable));
  116. }
  117. #endif /* FSL_FEATURE_SYSMPU_MASTER_COUNT > SYSMPU_MASTER_RWATTRIBUTE_START_PORT */
  118. /* Set region descriptor access rights. */
  119. base->WORD[regNumber][2] = wordReg;
  120. wordReg = SYSMPU_WORD_VLD(1);
  121. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  122. wordReg |= SYSMPU_WORD_PID(regionConfig->processIdentifier) | SYSMPU_WORD_PIDMASK(regionConfig->processIdMask);
  123. #endif /* FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER */
  124. base->WORD[regNumber][3] = wordReg;
  125. }
  126. void SYSMPU_SetRegionAddr(SYSMPU_Type *base, uint32_t regionNum, uint32_t startAddr, uint32_t endAddr)
  127. {
  128. assert(regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  129. base->WORD[regionNum][0] = startAddr;
  130. base->WORD[regionNum][1] = endAddr;
  131. }
  132. void SYSMPU_SetRegionRwxMasterAccessRights(SYSMPU_Type *base,
  133. uint32_t regionNum,
  134. uint32_t masterNum,
  135. const sysmpu_rwxrights_master_access_control_t *accessRights)
  136. {
  137. assert(accessRights);
  138. assert(regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  139. assert(masterNum < SYSMPU_MASTER_RWATTRIBUTE_START_PORT);
  140. uint32_t mask = SYSMPU_REGION_RWXRIGHTS_MASTER_MASK(masterNum);
  141. uint32_t right = base->RGDAAC[regionNum];
  142. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  143. mask |= SYSMPU_REGION_RWXRIGHTS_MASTER_PE_MASK(masterNum);
  144. #endif
  145. /* Build rights control value. */
  146. right &= ~mask;
  147. right |= SYSMPU_REGION_RWXRIGHTS_MASTER(
  148. masterNum, ((uint32_t)(accessRights->superAccessRights << 3U) | accessRights->userAccessRights));
  149. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  150. right |= SYSMPU_REGION_RWXRIGHTS_MASTER_PE(masterNum, accessRights->processIdentifierEnable);
  151. #endif /* FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER */
  152. /* Set low master region access rights. */
  153. base->RGDAAC[regionNum] = right;
  154. }
  155. #if FSL_FEATURE_SYSMPU_MASTER_COUNT > 4
  156. void SYSMPU_SetRegionRwMasterAccessRights(SYSMPU_Type *base,
  157. uint32_t regionNum,
  158. uint32_t masterNum,
  159. const sysmpu_rwrights_master_access_control_t *accessRights)
  160. {
  161. assert(accessRights);
  162. assert(regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  163. assert(masterNum >= SYSMPU_MASTER_RWATTRIBUTE_START_PORT);
  164. assert(masterNum <= (FSL_FEATURE_SYSMPU_MASTER_COUNT - 1));
  165. uint32_t mask = SYSMPU_REGION_RWRIGHTS_MASTER_MASK(masterNum);
  166. uint32_t right = base->RGDAAC[regionNum];
  167. /* Build rights control value. */
  168. right &= ~mask;
  169. right |=
  170. SYSMPU_REGION_RWRIGHTS_MASTER(masterNum, (((uint32_t)accessRights->readEnable << 1U) | accessRights->writeEnable));
  171. /* Set low master region access rights. */
  172. base->RGDAAC[regionNum] = right;
  173. }
  174. #endif /* FSL_FEATURE_SYSMPU_MASTER_COUNT > 4 */
  175. bool SYSMPU_GetSlavePortErrorStatus(SYSMPU_Type *base, sysmpu_slave_t slaveNum)
  176. {
  177. uint8_t sperr;
  178. sperr = ((base->CESR & SYSMPU_CESR_SPERR_MASK) >> SYSMPU_CESR_SPERR_SHIFT) & (0x1U << (FSL_FEATURE_SYSMPU_SLAVE_COUNT - slaveNum - 1));
  179. return (sperr != 0) ? true : false;
  180. }
  181. void SYSMPU_GetDetailErrorAccessInfo(SYSMPU_Type *base, sysmpu_slave_t slaveNum, sysmpu_access_err_info_t *errInform)
  182. {
  183. assert(errInform);
  184. uint16_t value;
  185. uint32_t cesReg;
  186. /* Error address. */
  187. errInform->address = base->SP[slaveNum].EAR;
  188. /* Error detail information. */
  189. value = (base->SP[slaveNum].EDR & SYSMPU_EDR_EACD_MASK) >> SYSMPU_EDR_EACD_SHIFT;
  190. if (!value)
  191. {
  192. errInform->accessControl = kSYSMPU_NoRegionHit;
  193. }
  194. else if (!(value & (uint16_t)(value - 1)))
  195. {
  196. errInform->accessControl = kSYSMPU_NoneOverlappRegion;
  197. }
  198. else
  199. {
  200. errInform->accessControl = kSYSMPU_OverlappRegion;
  201. }
  202. value = base->SP[slaveNum].EDR;
  203. errInform->master = (uint32_t)((value & SYSMPU_EDR_EMN_MASK) >> SYSMPU_EDR_EMN_SHIFT);
  204. errInform->attributes = (sysmpu_err_attributes_t)((value & SYSMPU_EDR_EATTR_MASK) >> SYSMPU_EDR_EATTR_SHIFT);
  205. errInform->accessType = (sysmpu_err_access_type_t)((value & SYSMPU_EDR_ERW_MASK) >> SYSMPU_EDR_ERW_SHIFT);
  206. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  207. errInform->processorIdentification = (uint8_t)((value & SYSMPU_EDR_EPID_MASK) >> SYSMPU_EDR_EPID_SHIFT);
  208. #endif
  209. /* Clears error slave port bit. */
  210. cesReg = (base->CESR & ~SYSMPU_CESR_SPERR_MASK) | ((0x1U << (FSL_FEATURE_SYSMPU_SLAVE_COUNT - slaveNum - 1)) << SYSMPU_CESR_SPERR_SHIFT);
  211. base->CESR = cesReg;
  212. }