fsl_gpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright 2019-2021 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_gpc.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.gpc_3"
  15. #endif
  16. /*******************************************************************************
  17. * Variables
  18. ******************************************************************************/
  19. /*! @brief GPC submodule step registers offset */
  20. static uint32_t const s_cmRegOffset[] = GPC_CM_STEP_REG_OFFSET;
  21. static uint32_t const s_spRegOffset[] = GPC_SP_STEP_REG_OFFSET;
  22. static uint32_t const s_stbyRegOffset[] = GPC_STBY_STEP_REG_OFFSET;
  23. /*******************************************************************************
  24. * Code
  25. ******************************************************************************/
  26. /*!
  27. * brief Enable IRQ wakeup request.
  28. *
  29. * This function enables the IRQ request which can wakeup the CPU platform.
  30. *
  31. * param base GPC CPU module base address.
  32. * param irqId ID of the IRQ, accessible range is 0-255.
  33. * param enable Enable the IRQ request or not.
  34. */
  35. void GPC_CM_EnableIrqWakeup(GPC_CPU_MODE_CTRL_Type *base, uint32_t irqId, bool enable)
  36. {
  37. assert(irqId < (GPC_CPU_MODE_CTRL_CM_IRQ_WAKEUP_MASK_COUNT * 32UL));
  38. uint32_t irqGroup = irqId / 32UL;
  39. uint32_t irqMask = irqId % 32UL;
  40. if (true == enable)
  41. {
  42. base->CM_IRQ_WAKEUP_MASK[irqGroup] &= ~(1UL << irqMask);
  43. }
  44. else
  45. {
  46. base->CM_IRQ_WAKEUP_MASK[irqGroup] |= (1UL << irqMask);
  47. }
  48. }
  49. /*!
  50. * brief Get the status of the IRQ wakeup request.
  51. *
  52. * param base GPC CPU module base address.
  53. * param irqId ID of the IRQ, accessible range is 0-255.
  54. * return Indicate the IRQ request is asserted or not.
  55. */
  56. bool GPC_CM_GetIrqWakeupStatus(GPC_CPU_MODE_CTRL_Type *base, uint32_t irqId)
  57. {
  58. assert(irqId < (GPC_CPU_MODE_CTRL_CM_IRQ_WAKEUP_MASK_COUNT * 32UL));
  59. uint32_t irqGroup = irqId / 32UL;
  60. uint32_t irqMask = irqId % 32UL;
  61. bool irqStatus = false;
  62. irqStatus = (((base->CM_IRQ_WAKEUP_STAT[irqGroup] >> irqMask) & 0x1UL) == 0x1UL) ? true : false;
  63. return irqStatus;
  64. }
  65. /*!
  66. * brief Config the cpu mode transition step.
  67. *
  68. * note This function can not config the setpoint sleep/wakeup operation for those
  69. * operation is controlled by setpoint control. This funcion can not config the standby
  70. * sleep/wakeup too, because those operation is controlled by standby controlled.
  71. *
  72. * param base GPC CPU module base address.
  73. * param step step type, refer to "gpc_cm_tran_step_t".
  74. * param config transition step configuration, refer to "gpc_tran_step_config_t".
  75. */
  76. void GPC_CM_ConfigCpuModeTransitionStep(GPC_CPU_MODE_CTRL_Type *base,
  77. gpc_cm_tran_step_t step,
  78. const gpc_tran_step_config_t *config)
  79. {
  80. if (!((step >= kGPC_CM_SleepSP) && (step <= kGPC_CM_WakeupSP)))
  81. {
  82. uint32_t tmp32 = *(uint32_t *)((uint32_t)base + s_cmRegOffset[step]);
  83. if (config->enableStep)
  84. {
  85. tmp32 &= ~(GPC_CPU_MODE_CTRL_CM_SLEEP_SSAR_CTRL_STEP_CNT_MASK |
  86. GPC_CPU_MODE_CTRL_CM_SLEEP_SSAR_CTRL_CNT_MODE_MASK);
  87. tmp32 |= GPC_CPU_MODE_CTRL_CM_SLEEP_SSAR_CTRL_CNT_MODE(config->cntMode);
  88. if (config->cntMode != kGPC_StepCounterDisableMode)
  89. {
  90. tmp32 |= GPC_CPU_MODE_CTRL_CM_SLEEP_SSAR_CTRL_STEP_CNT(config->stepCount);
  91. }
  92. tmp32 &= ~GPC_CPU_MODE_CTRL_CM_SLEEP_SSAR_CTRL_DISABLE_MASK;
  93. }
  94. else
  95. {
  96. tmp32 |= GPC_CPU_MODE_CTRL_CM_SLEEP_SSAR_CTRL_DISABLE_MASK;
  97. }
  98. *(uint32_t *)((uint32_t)base + s_cmRegOffset[step]) = tmp32;
  99. }
  100. }
  101. /*!
  102. * brief Request a set point transition before the CPU transfers into a sleep mode.
  103. *
  104. * This function triggers the set point transition during a CPU Sleep/wakeup event and selects which one the CMC want
  105. * to transfer to.
  106. *
  107. * param base GPC CPU module base address.
  108. * param setPointSleep The set point CPU want the system to transit to on next CPU platform sleep sequence.
  109. * param setPointWakeup The set point CPU want the system to transit to on next CPU platform wakeup sequence.
  110. * param wakeupSel Select the set point transition on the next CPU platform wakeup sequence.
  111. */
  112. void GPC_CM_RequestSleepModeSetPointTransition(GPC_CPU_MODE_CTRL_Type *base,
  113. uint8_t setPointSleep,
  114. uint8_t setPointWakeup,
  115. gpc_cm_wakeup_sp_sel_t wakeupSel)
  116. {
  117. uint32_t tmp32 = base->CM_SP_CTRL;
  118. tmp32 &= ~(GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_RUN_EN_MASK | GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_SLEEP_MASK |
  119. GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_WAKEUP_MASK | GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_WAKEUP_SEL_MASK);
  120. /* Config set point transition in the next sleep sequence. */
  121. tmp32 |=
  122. GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_SLEEP_EN_MASK | GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_SLEEP(setPointSleep);
  123. /* Config set point transition in the next wakeup sequence. */
  124. tmp32 |= GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_WAKEUP_EN_MASK |
  125. GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_WAKEUP(setPointWakeup) |
  126. GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_WAKEUP_SEL(wakeupSel);
  127. base->CM_SP_CTRL = tmp32;
  128. }
  129. /*!
  130. * brief Request a set point transition during run mode.
  131. *
  132. * This function triggers the set point transition and selects which one the CMC want to transfer to.
  133. *
  134. * param base GPC CPU module base address.
  135. * param setPointRun The set point CPU want the system to transit in the run mode.
  136. */
  137. void GPC_CM_RequestRunModeSetPointTransition(GPC_CPU_MODE_CTRL_Type *base, uint8_t setPointRun)
  138. {
  139. uint32_t tmp32 = base->CM_SP_CTRL;
  140. tmp32 &= ~(GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_RUN_MASK);
  141. tmp32 |= GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_RUN_EN_MASK | GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_RUN(setPointRun);
  142. base->CM_SP_CTRL = tmp32;
  143. while ((base->CM_SP_CTRL & GPC_CPU_MODE_CTRL_CM_SP_CTRL_CPU_SP_RUN_EN_MASK) != 0UL)
  144. {
  145. }
  146. }
  147. /*
  148. * brief Set the set point mapping value for each cpu mode.
  149. *
  150. * This function configures which set point is allowed when CPU enters RUN/WAIT/STOP/SUSPEND. If there are multiple
  151. * setpoints, use:
  152. * code
  153. * map = kkGPC_SetPoint0 | kGPC_SetPoint1 | ... | kGPC_SetPoint15;
  154. * encode
  155. *
  156. * param base GPC CPU module base address.
  157. * param mode CPU mode. Refer to "gpc_cpu_mode_t".
  158. * param map Map value of the set point. Refer to "_gpc_setpoint_map".
  159. */
  160. void GPC_CM_SetCpuModeSetPointMapping(GPC_CPU_MODE_CTRL_Type *base, gpc_cpu_mode_t mode, uint32_t map)
  161. {
  162. /* Ensure the allowed set point is in the accessible range (0-15). */
  163. map = map & 0xFFFFUL;
  164. switch (mode)
  165. {
  166. case kGPC_RunMode:
  167. base->CM_RUN_MODE_MAPPING = map;
  168. break;
  169. case kGPC_WaitMode:
  170. base->CM_WAIT_MODE_MAPPING = map;
  171. break;
  172. case kGPC_StopMode:
  173. base->CM_STOP_MODE_MAPPING = map;
  174. break;
  175. case kGPC_SuspendMode:
  176. base->CM_SUSPEND_MODE_MAPPING = map;
  177. break;
  178. default:
  179. assert(false);
  180. break;
  181. }
  182. }
  183. /*
  184. * brief Request the chip into standby mode.
  185. *
  186. * param base GPC CPU module base address.
  187. * param mode CPU mode. Refer to "gpc_cpu_mode_t".
  188. */
  189. void GPC_CM_RequestStandbyMode(GPC_CPU_MODE_CTRL_Type *base, const gpc_cpu_mode_t mode)
  190. {
  191. assert(mode != kGPC_RunMode);
  192. switch (mode)
  193. {
  194. case kGPC_WaitMode:
  195. base->CM_STBY_CTRL |= GPC_CPU_MODE_CTRL_CM_STBY_CTRL_STBY_WAIT_MASK;
  196. break;
  197. case kGPC_StopMode:
  198. base->CM_STBY_CTRL |= GPC_CPU_MODE_CTRL_CM_STBY_CTRL_STBY_STOP_MASK;
  199. break;
  200. case kGPC_SuspendMode:
  201. base->CM_STBY_CTRL |= GPC_CPU_MODE_CTRL_CM_STBY_CTRL_STBY_SUSPEND_MASK;
  202. break;
  203. default:
  204. /* This branch should never be hit. */
  205. break;
  206. }
  207. }
  208. /*
  209. * brief Clear the standby mode request.
  210. *
  211. * param base GPC CPU module base address.
  212. * param mode CPU mode. Refer to "gpc_cpu_mode_t".
  213. */
  214. void GPC_CM_ClearStandbyModeRequest(GPC_CPU_MODE_CTRL_Type *base, const gpc_cpu_mode_t mode)
  215. {
  216. assert(mode != kGPC_RunMode);
  217. switch (mode)
  218. {
  219. case kGPC_WaitMode:
  220. base->CM_STBY_CTRL &= ~GPC_CPU_MODE_CTRL_CM_STBY_CTRL_STBY_WAIT_MASK;
  221. break;
  222. case kGPC_StopMode:
  223. base->CM_STBY_CTRL &= ~GPC_CPU_MODE_CTRL_CM_STBY_CTRL_STBY_STOP_MASK;
  224. break;
  225. case kGPC_SuspendMode:
  226. base->CM_STBY_CTRL &= ~GPC_CPU_MODE_CTRL_CM_STBY_CTRL_STBY_SUSPEND_MASK;
  227. break;
  228. default:
  229. /* This branch should never be hit. */
  230. break;
  231. }
  232. }
  233. /*!
  234. * brief Clears CPU module interrut status flags.
  235. *
  236. * param base GPC CPU module base address.
  237. * param mask The interrupt status flags to be cleared. Should be the OR'ed value of _gpc_cm_interrupt_status_flag.
  238. */
  239. void GPC_CM_ClearInterruptStatusFlags(GPC_CPU_MODE_CTRL_Type *base, uint32_t mask)
  240. {
  241. uint32_t temp32;
  242. temp32 = base->CM_INT_CTRL;
  243. temp32 &= ~(GPC_CM_ALL_INTERRUPT_STATUS);
  244. base->CM_INT_CTRL = (mask | temp32);
  245. }
  246. /*!
  247. * brief Config the set point transition step.
  248. *
  249. * param base GPC Setpoint controller base address.
  250. * param step step type, refer to "gpc_sp_tran_step_t".
  251. * param config transition step configuration, refer to "gpc_tran_step_config_t".
  252. */
  253. void GPC_SP_ConfigSetPointTransitionStep(GPC_SET_POINT_CTRL_Type *base,
  254. gpc_sp_tran_step_t step,
  255. const gpc_tran_step_config_t *config)
  256. {
  257. uint32_t tmp32 = *(uint32_t *)((uint32_t)base + s_spRegOffset[step]);
  258. if (config->enableStep)
  259. {
  260. tmp32 &=
  261. ~(GPC_SET_POINT_CTRL_SP_SSAR_SAVE_CTRL_STEP_CNT_MASK | GPC_SET_POINT_CTRL_SP_SSAR_SAVE_CTRL_CNT_MODE_MASK);
  262. tmp32 |= GPC_SET_POINT_CTRL_SP_SSAR_SAVE_CTRL_CNT_MODE(config->cntMode);
  263. if (config->cntMode != kGPC_StepCounterDisableMode)
  264. {
  265. tmp32 |= GPC_SET_POINT_CTRL_SP_SSAR_SAVE_CTRL_STEP_CNT(config->stepCount);
  266. }
  267. tmp32 &= ~GPC_SET_POINT_CTRL_SP_SSAR_SAVE_CTRL_DISABLE_MASK;
  268. }
  269. else
  270. {
  271. tmp32 |= GPC_SET_POINT_CTRL_SP_SSAR_SAVE_CTRL_DISABLE_MASK;
  272. }
  273. *(uint32_t *)((uint32_t)base + s_spRegOffset[step]) = tmp32;
  274. }
  275. /*!
  276. * brief Config the standby transition step.
  277. *
  278. * param base GPC Setpoint controller base address.
  279. * param step step type, refer to "gpc_stby_tran_step_t".
  280. * param config transition step configuration, refer to "gpc_tran_step_config_t".
  281. */
  282. void GPC_STBY_ConfigStandbyTransitionStep(GPC_STBY_CTRL_Type *base,
  283. gpc_stby_tran_step_t step,
  284. const gpc_tran_step_config_t *config)
  285. {
  286. uint32_t tmp32 = *(uint32_t *)((uint32_t)base + s_stbyRegOffset[step]);
  287. if (config->enableStep)
  288. {
  289. tmp32 &= ~(GPC_STBY_CTRL_STBY_LPCG_IN_CTRL_STEP_CNT_MASK | GPC_STBY_CTRL_STBY_LPCG_IN_CTRL_CNT_MODE_MASK);
  290. tmp32 |= GPC_STBY_CTRL_STBY_LPCG_IN_CTRL_CNT_MODE(config->cntMode);
  291. if (config->cntMode != kGPC_StepCounterDisableMode)
  292. {
  293. tmp32 |= GPC_STBY_CTRL_STBY_LPCG_IN_CTRL_STEP_CNT(config->stepCount);
  294. }
  295. tmp32 &= ~GPC_STBY_CTRL_STBY_LPCG_IN_CTRL_DISABLE_MASK;
  296. }
  297. else
  298. {
  299. tmp32 |= GPC_STBY_CTRL_STBY_LPCG_IN_CTRL_DISABLE_MASK;
  300. }
  301. *(uint32_t *)((uint32_t)base + s_stbyRegOffset[step]) = tmp32;
  302. }