fsl_rdc_sema42.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2017-2020 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include "fsl_rdc_sema42.h"
  8. /******************************************************************************
  9. * Definitions
  10. *****************************************************************************/
  11. /* Component ID definition, used by tools. */
  12. #ifndef FSL_COMPONENT_ID
  13. #define FSL_COMPONENT_ID "platform.drivers.rdc_sema42"
  14. #endif
  15. /* The first number write to RSTGDP when reset RDC_SEMA42 gate. */
  16. #define RDC_SEMA42_GATE_RESET_PATTERN_1 (0xE2U)
  17. /* The second number write to RSTGDP when reset RDC_SEMA42 gate. */
  18. #define RDC_SEMA42_GATE_RESET_PATTERN_2 (0x1DU)
  19. #if !defined(RDC_SEMAPHORE_GATE_COUNT)
  20. /* Compatible remap. */
  21. #define RDC_SEMAPHORE_GATE_LDOM(x) RDC_SEMAPHORE_GATE0_LDOM(x)
  22. #define RDC_SEMAPHORE_GATE_GTFSM(x) RDC_SEMAPHORE_GATE0_GTFSM(x)
  23. #define RDC_SEMAPHORE_GATE_LDOM_MASK RDC_SEMAPHORE_GATE0_LDOM_MASK
  24. #define RDC_SEMAPHORE_GATE_LDOM_SHIFT RDC_SEMAPHORE_GATE0_LDOM_SHIFT
  25. #endif
  26. /*******************************************************************************
  27. * Prototypes
  28. ******************************************************************************/
  29. /*!
  30. * @brief Get instance number for RDC_SEMA42 module.
  31. *
  32. * @param base RDC_SEMA42 peripheral base address.
  33. */
  34. uint32_t RDC_SEMA42_GetInstance(RDC_SEMAPHORE_Type *base);
  35. /*******************************************************************************
  36. * Variables
  37. ******************************************************************************/
  38. /*! @brief Pointers to sema42 bases for each instance. */
  39. static RDC_SEMAPHORE_Type *const s_sema42Bases[] = RDC_SEMAPHORE_BASE_PTRS;
  40. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  41. #if defined(RDC_SEMA42_CLOCKS)
  42. /*! @brief Pointers to sema42 clocks for each instance. */
  43. static const clock_ip_name_t s_sema42Clocks[] = RDC_SEMA42_CLOCKS;
  44. #endif
  45. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  46. /******************************************************************************
  47. * CODE
  48. *****************************************************************************/
  49. uint32_t RDC_SEMA42_GetInstance(RDC_SEMAPHORE_Type *base)
  50. {
  51. uint32_t instance;
  52. /* Find the instance index from base address mappings. */
  53. for (instance = 0; instance < ARRAY_SIZE(s_sema42Bases); instance++)
  54. {
  55. if (s_sema42Bases[instance] == base)
  56. {
  57. break;
  58. }
  59. }
  60. assert(instance < ARRAY_SIZE(s_sema42Bases));
  61. return instance;
  62. }
  63. /*!
  64. * brief Initializes the RDC_SEMA42 module.
  65. *
  66. * This function initializes the RDC_SEMA42 module. It only enables the clock but does
  67. * not reset the gates because the module might be used by other processors
  68. * at the same time. To reset the gates, call either RDC_SEMA42_ResetGate or
  69. * RDC_SEMA42_ResetAllGates function.
  70. *
  71. * param base RDC_SEMA42 peripheral base address.
  72. */
  73. void RDC_SEMA42_Init(RDC_SEMAPHORE_Type *base)
  74. {
  75. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  76. #if defined(RDC_SEMA42_CLOCKS)
  77. CLOCK_EnableClock(s_sema42Clocks[RDC_SEMA42_GetInstance(base)]);
  78. #endif
  79. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  80. }
  81. /*!
  82. * brief De-initializes the RDC_SEMA42 module.
  83. *
  84. * This function de-initializes the RDC_SEMA42 module. It only disables the clock.
  85. *
  86. * param base RDC_SEMA42 peripheral base address.
  87. */
  88. void RDC_SEMA42_Deinit(RDC_SEMAPHORE_Type *base)
  89. {
  90. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  91. #if defined(RDC_SEMA42_CLOCKS)
  92. CLOCK_DisableClock(s_sema42Clocks[RDC_SEMA42_GetInstance(base)]);
  93. #endif
  94. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  95. }
  96. /*!
  97. * brief Tries to lock the RDC_SEMA42 gate.
  98. *
  99. * This function tries to lock the specific RDC_SEMA42 gate. If the gate has been
  100. * locked by another processor, this function returns an error code.
  101. *
  102. * param base RDC_SEMA42 peripheral base address.
  103. * param gateNum Gate number to lock.
  104. * param masterIndex Current processor master index.
  105. * param domainId Current processor domain ID.
  106. *
  107. * retval kStatus_Success Lock the sema42 gate successfully.
  108. * retval kStatus_Failed Sema42 gate has been locked by another processor.
  109. */
  110. status_t RDC_SEMA42_TryLock(RDC_SEMAPHORE_Type *base, uint8_t gateNum, uint8_t masterIndex, uint8_t domainId)
  111. {
  112. assert(gateNum < RDC_SEMA42_GATE_COUNT);
  113. status_t status = kStatus_Success;
  114. uint8_t regGate;
  115. ++masterIndex;
  116. regGate = (uint8_t)(RDC_SEMAPHORE_GATE_LDOM(domainId) | RDC_SEMAPHORE_GATE_GTFSM(masterIndex));
  117. /* Try to lock. */
  118. RDC_SEMA42_GATEn(base, gateNum) = masterIndex;
  119. /* Check locked or not. */
  120. if (regGate != RDC_SEMA42_GATEn(base, gateNum))
  121. {
  122. status = kStatus_Fail;
  123. }
  124. return status;
  125. }
  126. /*!
  127. * brief Locks the RDC_SEMA42 gate.
  128. *
  129. * This function locks the specific RDC_SEMA42 gate. If the gate has been
  130. * locked by other processors, this function waits until it is unlocked and then
  131. * lock it.
  132. *
  133. * param base RDC_SEMA42 peripheral base address.
  134. * param gateNum Gate number to lock.
  135. * param masterIndex Current processor master index.
  136. * param domainId Current processor domain ID.
  137. */
  138. void RDC_SEMA42_Lock(RDC_SEMAPHORE_Type *base, uint8_t gateNum, uint8_t masterIndex, uint8_t domainId)
  139. {
  140. assert(gateNum < RDC_SEMA42_GATE_COUNT);
  141. uint8_t regGate;
  142. ++masterIndex;
  143. regGate = (uint8_t)(RDC_SEMAPHORE_GATE_LDOM(domainId) | RDC_SEMAPHORE_GATE_GTFSM(masterIndex));
  144. while (regGate != RDC_SEMA42_GATEn(base, gateNum))
  145. {
  146. /* Wait for unlocked status. */
  147. while (0U != (RDC_SEMA42_GATEn(base, gateNum) & RDC_SEMAPHORE_GATE_GTFSM_MASK))
  148. {
  149. }
  150. /* Lock the gate. */
  151. RDC_SEMA42_GATEn(base, gateNum) = masterIndex;
  152. }
  153. }
  154. /*!
  155. * brief Gets which domain has currently locked the gate.
  156. *
  157. * param base RDC_SEMA42 peripheral base address.
  158. * param gateNum Gate number.
  159. *
  160. * return Return -1 if the gate is not locked by any domain, otherwise return the
  161. * domain ID.
  162. */
  163. int32_t RDC_SEMA42_GetLockDomainID(RDC_SEMAPHORE_Type *base, uint8_t gateNum)
  164. {
  165. assert(gateNum < RDC_SEMA42_GATE_COUNT);
  166. int32_t ret;
  167. uint8_t regGate = RDC_SEMA42_GATEn(base, gateNum);
  168. /* Current gate is not locked. */
  169. if (0U == (regGate & RDC_SEMAPHORE_GATE_GTFSM_MASK))
  170. {
  171. ret = -1;
  172. }
  173. else
  174. {
  175. ret = (int32_t)((uint8_t)((regGate & RDC_SEMAPHORE_GATE_LDOM_MASK) >> RDC_SEMAPHORE_GATE_LDOM_SHIFT));
  176. }
  177. return ret;
  178. }
  179. /*!
  180. * brief Resets the RDC_SEMA42 gate to an unlocked status.
  181. *
  182. * This function resets a RDC_SEMA42 gate to an unlocked status.
  183. *
  184. * param base RDC_SEMA42 peripheral base address.
  185. * param gateNum Gate number.
  186. *
  187. * retval kStatus_Success RDC_SEMA42 gate is reset successfully.
  188. * retval kStatus_Failed Some other reset process is ongoing.
  189. */
  190. status_t RDC_SEMA42_ResetGate(RDC_SEMAPHORE_Type *base, uint8_t gateNum)
  191. {
  192. status_t status;
  193. /*
  194. * Reset all gates if gateNum >= RDC_SEMA42_GATE_NUM_RESET_ALL
  195. * Reset specific gate if gateNum < RDC_SEMA42_GATE_COUNT
  196. */
  197. /* Check whether some reset is ongoing. */
  198. if (0U != (base->RSTGT_R & RDC_SEMAPHORE_RSTGT_R_RSTGSM_MASK))
  199. {
  200. status = kStatus_Fail;
  201. }
  202. else
  203. {
  204. /* First step. */
  205. base->RSTGT_W = RDC_SEMAPHORE_RSTGT_W_RSTGDP(RDC_SEMA42_GATE_RESET_PATTERN_1);
  206. /* Second step. */
  207. base->RSTGT_W =
  208. RDC_SEMAPHORE_RSTGT_W_RSTGDP(RDC_SEMA42_GATE_RESET_PATTERN_2) | RDC_SEMAPHORE_RSTGT_W_RSTGTN(gateNum);
  209. status = kStatus_Success;
  210. }
  211. return status;
  212. }