fsl_cdog.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 2020 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include "fsl_cdog.h"
  8. /*******************************************************************************
  9. * Definitions
  10. *******************************************************************************/
  11. /* Component ID definition, used by tools. */
  12. #ifndef FSL_COMPONENT_ID
  13. #define FSL_COMPONENT_ID "platform.drivers.cdog"
  14. #endif
  15. /*******************************************************************************
  16. * Prototypes
  17. ******************************************************************************/
  18. /*******************************************************************************
  19. * Code
  20. ******************************************************************************/
  21. /*!
  22. * brief Sets the default configuration of CDOG
  23. *
  24. * This function initialize CDOG config structure to default values.
  25. *
  26. * param conf CDOG configuration structure
  27. */
  28. void CDOG_GetDefaultConfig(cdog_config_t *conf)
  29. {
  30. /* Default configuration after reset */
  31. conf->lock = (uint8_t)kCDOG_LockCtrl_Unlock; /* Lock control */
  32. conf->timeout = (uint8_t)kCDOG_FaultCtrl_NoAction; /* Timeout control */
  33. conf->miscompare = (uint8_t)kCDOG_FaultCtrl_NoAction; /* Miscompare control */
  34. conf->sequence = (uint8_t)kCDOG_FaultCtrl_NoAction; /* Sequence control */
  35. conf->control = (uint8_t)kCDOG_FaultCtrl_NoAction; /* Control */
  36. conf->state = (uint8_t)kCDOG_FaultCtrl_NoAction; /* State control */
  37. conf->address = (uint8_t)kCDOG_FaultCtrl_NoAction; /* Address control */
  38. conf->irq_pause = (uint8_t)kCDOG_IrqPauseCtrl_Run; /* IRQ pause control */
  39. conf->debug_halt = (uint8_t)kCDOG_DebugHaltCtrl_Run; /* Debug halt control */
  40. return;
  41. }
  42. /*!
  43. * brief Sets secure counter and instruction timer values
  44. *
  45. * This function sets value in RELOAD and START registers for instruction timer.
  46. *
  47. * param base CDOG peripheral base address
  48. * param reload reload value
  49. * param start start value
  50. */
  51. void CDOG_Start(CDOG_Type *base, uint32_t reload, uint32_t start)
  52. {
  53. base->RELOAD = reload;
  54. base->START = start;
  55. }
  56. /*!
  57. * brief Stops secure counter and instruction timer
  58. *
  59. * This function stops instruction timer and secure counter.
  60. * This also change state of CDOG to IDLE.
  61. *
  62. * param base CDOG peripheral base address
  63. * param stop expected value which will be compared with value of secure counter
  64. */
  65. void CDOG_Stop(CDOG_Type *base, uint32_t stop)
  66. {
  67. base->STOP = stop;
  68. }
  69. /*!
  70. * brief Sets secure counter and instruction timer values
  71. *
  72. * This function sets value in STOP, RELOAD and START registers
  73. * for instruction timer and secure counter.
  74. *
  75. * param base CDOG peripheral base address
  76. * param stop expected value which will be compared with value of secure counter
  77. * param reload reload value for instruction timer
  78. * param start start value for secure timer
  79. */
  80. void CDOG_Set(CDOG_Type *base, uint32_t stop, uint32_t reload, uint32_t start)
  81. {
  82. base->STOP = stop;
  83. base->RELOAD = reload;
  84. base->START = start;
  85. }
  86. /*!
  87. * brief Add value to secure counter
  88. *
  89. * This function add specified value to secure counter.
  90. *
  91. * param base CDOG peripheral base address.
  92. * param add Value to be added.
  93. */
  94. void CDOG_Add(CDOG_Type *base, uint32_t add)
  95. {
  96. base->ADD = (secure_counter_t)add;
  97. }
  98. /*!
  99. * brief Add 1 to secure counter
  100. *
  101. * This function add 1 to secure counter.
  102. *
  103. * param base CDOG peripheral base address.
  104. * param add Value to be added.
  105. */
  106. void CDOG_Add1(CDOG_Type *base)
  107. {
  108. base->ADD1 = (secure_counter_t)0x1U;
  109. }
  110. /*!
  111. * brief Add 16 to secure counter
  112. *
  113. * This function add 16 to secure counter.
  114. *
  115. * param base CDOG peripheral base address.
  116. * param add Value to be added.
  117. */
  118. void CDOG_Add16(CDOG_Type *base)
  119. {
  120. base->ADD16 = (secure_counter_t)0x1U;
  121. }
  122. /*!
  123. * brief Add 256 to secure counter
  124. *
  125. * This function add 256 to secure counter.
  126. *
  127. * param base CDOG peripheral base address.
  128. * param add Value to be added.
  129. */
  130. void CDOG_Add256(CDOG_Type *base)
  131. {
  132. base->ADD256 = (secure_counter_t)0x1U;
  133. }
  134. /*!
  135. * brief Substract value to secure counter
  136. *
  137. * This function substract specified value to secure counter.
  138. *
  139. * param base CDOG peripheral base address.
  140. * param sub Value to be substracted.
  141. */
  142. void CDOG_Sub(CDOG_Type *base, uint32_t sub)
  143. {
  144. base->SUB = (secure_counter_t)sub;
  145. }
  146. /*!
  147. * brief Substract 1 from secure counter
  148. *
  149. * This function substract specified 1 from secure counter.
  150. *
  151. * param base CDOG peripheral base address.
  152. */
  153. void CDOG_Sub1(CDOG_Type *base)
  154. {
  155. base->SUB1 = (secure_counter_t)0x1U;
  156. }
  157. /*!
  158. * brief Substract 16 from secure counter
  159. *
  160. * This function substract specified 16 from secure counter.
  161. *
  162. * param base CDOG peripheral base address.
  163. */
  164. void CDOG_Sub16(CDOG_Type *base)
  165. {
  166. base->SUB16 = (secure_counter_t)0x1U;
  167. }
  168. /*!
  169. * brief Substract 256 from secure counter
  170. *
  171. * This function substract specified 256 from secure counter.
  172. *
  173. * param base CDOG peripheral base address.
  174. */
  175. void CDOG_Sub256(CDOG_Type *base)
  176. {
  177. base->SUB256 = (secure_counter_t)0x1U;
  178. }
  179. /*!
  180. * brief Checks secure counter.
  181. *
  182. * This function compares stop value with secure counter value
  183. * by writting to RELOAD refister.
  184. *
  185. * param base CDOG peripheral base address
  186. * param check expected (stop) value.
  187. */
  188. void CDOG_Check(CDOG_Type *base, uint32_t check)
  189. {
  190. base->RESTART = check;
  191. }
  192. /*!
  193. * brief Set the CDOG persistent word.
  194. *
  195. * param base CDOG peripheral base address.
  196. * param value The value to be written.
  197. */
  198. void CDOG_WritePersistent(CDOG_Type *base, uint32_t value)
  199. {
  200. base->PERSISTENT = value;
  201. }
  202. /*!
  203. * brief Get the CDOG persistent word.
  204. *
  205. * param base CDOG peripheral base address.
  206. * return The persistent word.
  207. */
  208. uint32_t CDOG_ReadPersistent(CDOG_Type *base)
  209. {
  210. return base->PERSISTENT;
  211. }
  212. /*!
  213. * brief Initialize CDOG
  214. *
  215. * This function initializes CDOG block and setting.
  216. *
  217. * param base CDOG peripheral base address
  218. * param conf CDOG configuration structure
  219. * return Status of the init operation
  220. */
  221. status_t CDOG_Init(CDOG_Type *base, cdog_config_t *conf)
  222. {
  223. /* Ungate clock to CDOG engine and reset it */
  224. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  225. CLOCK_EnableClock(kCLOCK_Cdog);
  226. #endif /* !FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  227. #if !(defined(FSL_FEATURE_CDOG_HAS_NO_RESET) && FSL_FEATURE_CDOG_HAS_NO_RESET)
  228. RESET_PeripheralReset(kCDOG_RST_SHIFT_RSTn);
  229. #endif /* !FSL_FEATURE_CDOG_HAS_NO_RESET */
  230. if (base->CONTROL == 0x0U)
  231. {
  232. /* CDOG is not in IDLE mode, which may be cause after SW reset. */
  233. /* Writing to CONTROL register will trigger fault. */
  234. return kStatus_Fail;
  235. }
  236. /* Clear pending errors, otherwise the device will reset */
  237. /* itself immediately after enable Code Watchdog */
  238. if ((uint32_t)kCDOG_LockCtrl_Lock ==
  239. ((base->CONTROL & CDOG_CONTROL_LOCK_CTRL_MASK) >> CDOG_CONTROL_LOCK_CTRL_SHIFT))
  240. {
  241. CDOG->FLAGS = CDOG_FLAGS_TO_FLAG(1U) | CDOG_FLAGS_MISCOM_FLAG(1U) | CDOG_FLAGS_SEQ_FLAG(1U) |
  242. CDOG_FLAGS_CNT_FLAG(1U) | CDOG_FLAGS_STATE_FLAG(1U) | CDOG_FLAGS_ADDR_FLAG(1U) |
  243. CDOG_FLAGS_POR_FLAG(1U);
  244. }
  245. else
  246. {
  247. CDOG->FLAGS = CDOG_FLAGS_TO_FLAG(0U) | CDOG_FLAGS_MISCOM_FLAG(0U) | CDOG_FLAGS_SEQ_FLAG(0U) |
  248. CDOG_FLAGS_CNT_FLAG(0U) | CDOG_FLAGS_STATE_FLAG(0U) | CDOG_FLAGS_ADDR_FLAG(0U) |
  249. CDOG_FLAGS_POR_FLAG(0U);
  250. }
  251. base->CONTROL =
  252. CDOG_CONTROL_TIMEOUT_CTRL(conf->timeout) | /* Action if the timeout event is triggered */
  253. CDOG_CONTROL_MISCOMPARE_CTRL(conf->miscompare) | /* Action if the miscompare error event is triggered */
  254. CDOG_CONTROL_SEQUENCE_CTRL(conf->sequence) | /* Action if the sequence error event is triggered */
  255. CDOG_CONTROL_CONTROL_CTRL(conf->control) | /* Action if the control error event is triggered */
  256. CDOG_CONTROL_STATE_CTRL(conf->state) | /* Action if the state error event is triggered */
  257. CDOG_CONTROL_ADDRESS_CTRL(conf->address) | /* Action if the address error event is triggered */
  258. CDOG_CONTROL_IRQ_PAUSE(conf->irq_pause) | /* Pause running during interrupts setup */
  259. CDOG_CONTROL_DEBUG_HALT_CTRL(
  260. conf->debug_halt) | /* Halt CDOG timer during debug so we have chance to debug code */
  261. CDOG_CONTROL_LOCK_CTRL(conf->lock); /* Lock control register */
  262. NVIC_EnableIRQ(CDOG_IRQn);
  263. return kStatus_Success;
  264. }
  265. /*!
  266. * brief Deinitialize CDOG
  267. *
  268. * This function stops CDOG secure counter.
  269. *
  270. * param base CDOG peripheral base address
  271. */
  272. void CDOG_Deinit(CDOG_Type *base)
  273. {
  274. NVIC_DisableIRQ(CDOG_IRQn);
  275. #if !(defined(FSL_FEATURE_CDOG_HAS_NO_RESET) && FSL_FEATURE_CDOG_HAS_NO_RESET)
  276. RESET_SetPeripheralReset(kCDOG_RST_SHIFT_RSTn);
  277. #endif /* !FSL_FEATURE_CDOG_HAS_NO_RESET */
  278. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  279. CLOCK_DisableClock(kCLOCK_Cdog);
  280. #endif /* !FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  281. }