fsl_sysmpu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.sysmpu"
  38. #endif
  39. /*******************************************************************************
  40. * Variables
  41. ******************************************************************************/
  42. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  43. static const clock_ip_name_t g_sysmpuClock[] = SYSMPU_CLOCKS;
  44. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  45. /*******************************************************************************
  46. * Codes
  47. ******************************************************************************/
  48. void SYSMPU_Init(SYSMPU_Type *base, const sysmpu_config_t *config)
  49. {
  50. assert(config);
  51. uint8_t count;
  52. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  53. /* Un-gate SYSMPU clock */
  54. CLOCK_EnableClock(g_sysmpuClock[0]);
  55. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  56. /* Initializes the regions. */
  57. for (count = 1; count < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT; count++)
  58. {
  59. base->WORD[count][3] = 0; /* VLD/VID+PID. */
  60. base->WORD[count][0] = 0; /* Start address. */
  61. base->WORD[count][1] = 0; /* End address. */
  62. base->WORD[count][2] = 0; /* Access rights. */
  63. base->RGDAAC[count] = 0; /* Alternate access rights. */
  64. }
  65. /* SYSMPU configure. */
  66. while (config)
  67. {
  68. SYSMPU_SetRegionConfig(base, &(config->regionConfig));
  69. config = config->next;
  70. }
  71. /* Enable SYSMPU. */
  72. SYSMPU_Enable(base, true);
  73. }
  74. void SYSMPU_Deinit(SYSMPU_Type *base)
  75. {
  76. /* Disable SYSMPU. */
  77. SYSMPU_Enable(base, false);
  78. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  79. /* Gate the clock. */
  80. CLOCK_DisableClock(g_sysmpuClock[0]);
  81. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  82. }
  83. void SYSMPU_GetHardwareInfo(SYSMPU_Type *base, sysmpu_hardware_info_t *hardwareInform)
  84. {
  85. assert(hardwareInform);
  86. uint32_t cesReg = base->CESR;
  87. hardwareInform->hardwareRevisionLevel = (cesReg & SYSMPU_CESR_HRL_MASK) >> SYSMPU_CESR_HRL_SHIFT;
  88. hardwareInform->slavePortsNumbers = (cesReg & SYSMPU_CESR_NSP_MASK) >> SYSMPU_CESR_NSP_SHIFT;
  89. hardwareInform->regionsNumbers = (sysmpu_region_total_num_t)((cesReg & SYSMPU_CESR_NRGD_MASK) >> SYSMPU_CESR_NRGD_SHIFT);
  90. }
  91. void SYSMPU_SetRegionConfig(SYSMPU_Type *base, const sysmpu_region_config_t *regionConfig)
  92. {
  93. assert(regionConfig);
  94. assert(regionConfig->regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  95. uint32_t wordReg = 0;
  96. uint8_t msPortNum;
  97. uint8_t regNumber = regionConfig->regionNum;
  98. /* The start and end address of the region descriptor. */
  99. base->WORD[regNumber][0] = regionConfig->startAddress;
  100. base->WORD[regNumber][1] = regionConfig->endAddress;
  101. /* Set the privilege rights for master 0 ~ master 3. */
  102. for (msPortNum = 0; msPortNum < SYSMPU_MASTER_RWATTRIBUTE_START_PORT; msPortNum++)
  103. {
  104. wordReg |= SYSMPU_REGION_RWXRIGHTS_MASTER(
  105. msPortNum, (((uint32_t)regionConfig->accessRights1[msPortNum].superAccessRights << 3U) |
  106. (uint32_t)regionConfig->accessRights1[msPortNum].userAccessRights));
  107. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  108. wordReg |=
  109. SYSMPU_REGION_RWXRIGHTS_MASTER_PE(msPortNum, regionConfig->accessRights1[msPortNum].processIdentifierEnable);
  110. #endif /* FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER */
  111. }
  112. #if FSL_FEATURE_SYSMPU_MASTER_COUNT > SYSMPU_MASTER_RWATTRIBUTE_START_PORT
  113. /* Set the normal read write rights for master 4 ~ master 7. */
  114. for (msPortNum = SYSMPU_MASTER_RWATTRIBUTE_START_PORT; msPortNum < FSL_FEATURE_SYSMPU_MASTER_COUNT;
  115. msPortNum++)
  116. {
  117. wordReg |= SYSMPU_REGION_RWRIGHTS_MASTER(msPortNum,
  118. ((uint32_t)regionConfig->accessRights2[msPortNum - SYSMPU_MASTER_RWATTRIBUTE_START_PORT].readEnable << 1U |
  119. (uint32_t)regionConfig->accessRights2[msPortNum - SYSMPU_MASTER_RWATTRIBUTE_START_PORT].writeEnable));
  120. }
  121. #endif /* FSL_FEATURE_SYSMPU_MASTER_COUNT > SYSMPU_MASTER_RWATTRIBUTE_START_PORT */
  122. /* Set region descriptor access rights. */
  123. base->WORD[regNumber][2] = wordReg;
  124. wordReg = SYSMPU_WORD_VLD(1);
  125. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  126. wordReg |= SYSMPU_WORD_PID(regionConfig->processIdentifier) | SYSMPU_WORD_PIDMASK(regionConfig->processIdMask);
  127. #endif /* FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER */
  128. base->WORD[regNumber][3] = wordReg;
  129. }
  130. void SYSMPU_SetRegionAddr(SYSMPU_Type *base, uint32_t regionNum, uint32_t startAddr, uint32_t endAddr)
  131. {
  132. assert(regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  133. base->WORD[regionNum][0] = startAddr;
  134. base->WORD[regionNum][1] = endAddr;
  135. }
  136. void SYSMPU_SetRegionRwxMasterAccessRights(SYSMPU_Type *base,
  137. uint32_t regionNum,
  138. uint32_t masterNum,
  139. const sysmpu_rwxrights_master_access_control_t *accessRights)
  140. {
  141. assert(accessRights);
  142. assert(regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  143. assert(masterNum < SYSMPU_MASTER_RWATTRIBUTE_START_PORT);
  144. uint32_t mask = SYSMPU_REGION_RWXRIGHTS_MASTER_MASK(masterNum);
  145. uint32_t right = base->RGDAAC[regionNum];
  146. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  147. mask |= SYSMPU_REGION_RWXRIGHTS_MASTER_PE_MASK(masterNum);
  148. #endif
  149. /* Build rights control value. */
  150. right &= ~mask;
  151. right |= SYSMPU_REGION_RWXRIGHTS_MASTER(
  152. masterNum, ((uint32_t)(accessRights->superAccessRights << 3U) | accessRights->userAccessRights));
  153. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  154. right |= SYSMPU_REGION_RWXRIGHTS_MASTER_PE(masterNum, accessRights->processIdentifierEnable);
  155. #endif /* FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER */
  156. /* Set low master region access rights. */
  157. base->RGDAAC[regionNum] = right;
  158. }
  159. #if FSL_FEATURE_SYSMPU_MASTER_COUNT > 4
  160. void SYSMPU_SetRegionRwMasterAccessRights(SYSMPU_Type *base,
  161. uint32_t regionNum,
  162. uint32_t masterNum,
  163. const sysmpu_rwrights_master_access_control_t *accessRights)
  164. {
  165. assert(accessRights);
  166. assert(regionNum < FSL_FEATURE_SYSMPU_DESCRIPTOR_COUNT);
  167. assert(masterNum >= SYSMPU_MASTER_RWATTRIBUTE_START_PORT);
  168. assert(masterNum <= (FSL_FEATURE_SYSMPU_MASTER_COUNT - 1));
  169. uint32_t mask = SYSMPU_REGION_RWRIGHTS_MASTER_MASK(masterNum);
  170. uint32_t right = base->RGDAAC[regionNum];
  171. /* Build rights control value. */
  172. right &= ~mask;
  173. right |=
  174. SYSMPU_REGION_RWRIGHTS_MASTER(masterNum, (((uint32_t)accessRights->readEnable << 1U) | accessRights->writeEnable));
  175. /* Set low master region access rights. */
  176. base->RGDAAC[regionNum] = right;
  177. }
  178. #endif /* FSL_FEATURE_SYSMPU_MASTER_COUNT > 4 */
  179. bool SYSMPU_GetSlavePortErrorStatus(SYSMPU_Type *base, sysmpu_slave_t slaveNum)
  180. {
  181. uint8_t sperr;
  182. sperr = ((base->CESR & SYSMPU_CESR_SPERR_MASK) >> SYSMPU_CESR_SPERR_SHIFT) & (0x1U << (FSL_FEATURE_SYSMPU_SLAVE_COUNT - slaveNum - 1));
  183. return (sperr != 0) ? true : false;
  184. }
  185. void SYSMPU_GetDetailErrorAccessInfo(SYSMPU_Type *base, sysmpu_slave_t slaveNum, sysmpu_access_err_info_t *errInform)
  186. {
  187. assert(errInform);
  188. uint16_t value;
  189. uint32_t cesReg;
  190. /* Error address. */
  191. errInform->address = base->SP[slaveNum].EAR;
  192. /* Error detail information. */
  193. value = (base->SP[slaveNum].EDR & SYSMPU_EDR_EACD_MASK) >> SYSMPU_EDR_EACD_SHIFT;
  194. if (!value)
  195. {
  196. errInform->accessControl = kSYSMPU_NoRegionHit;
  197. }
  198. else if (!(value & (uint16_t)(value - 1)))
  199. {
  200. errInform->accessControl = kSYSMPU_NoneOverlappRegion;
  201. }
  202. else
  203. {
  204. errInform->accessControl = kSYSMPU_OverlappRegion;
  205. }
  206. value = base->SP[slaveNum].EDR;
  207. errInform->master = (uint32_t)((value & SYSMPU_EDR_EMN_MASK) >> SYSMPU_EDR_EMN_SHIFT);
  208. errInform->attributes = (sysmpu_err_attributes_t)((value & SYSMPU_EDR_EATTR_MASK) >> SYSMPU_EDR_EATTR_SHIFT);
  209. errInform->accessType = (sysmpu_err_access_type_t)((value & SYSMPU_EDR_ERW_MASK) >> SYSMPU_EDR_ERW_SHIFT);
  210. #if FSL_FEATURE_SYSMPU_HAS_PROCESS_IDENTIFIER
  211. errInform->processorIdentification = (uint8_t)((value & SYSMPU_EDR_EPID_MASK) >> SYSMPU_EDR_EPID_SHIFT);
  212. #endif
  213. /* Clears error slave port bit. */
  214. cesReg = (base->CESR & ~SYSMPU_CESR_SPERR_MASK) | ((0x1U << (FSL_FEATURE_SYSMPU_SLAVE_COUNT - slaveNum - 1)) << SYSMPU_CESR_SPERR_SHIFT);
  215. base->CESR = cesReg;
  216. }