fsl_smc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, 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_smc.h"
  35. #include "fsl_flash.h"
  36. #include "fsl_common.h"
  37. /* Component ID definition, used by tools. */
  38. #ifndef FSL_COMPONENT_ID
  39. #define FSL_COMPONENT_ID "platform.drivers.smc"
  40. #endif
  41. static uint32_t g_savedPrimask;
  42. #if (defined(FSL_FEATURE_SMC_HAS_PARAM) && FSL_FEATURE_SMC_HAS_PARAM)
  43. void SMC_GetParam(SMC_Type *base, smc_param_t *param)
  44. {
  45. uint32_t reg = base->PARAM;
  46. param->hsrunEnable = (bool)(reg & SMC_PARAM_EHSRUN_MASK);
  47. param->llsEnable = (bool)(reg & SMC_PARAM_ELLS_MASK);
  48. param->lls2Enable = (bool)(reg & SMC_PARAM_ELLS2_MASK);
  49. param->vlls0Enable = (bool)(reg & SMC_PARAM_EVLLS0_MASK);
  50. }
  51. #endif /* FSL_FEATURE_SMC_HAS_PARAM */
  52. void SMC_PreEnterStopModes(void)
  53. {
  54. ftfx_prefetch_speculation_status_t speculationStatus = {
  55. true, /* Disable instruction speculation.*/
  56. true, /* Disable data speculation.*/
  57. };
  58. g_savedPrimask = DisableGlobalIRQ();
  59. __ISB();
  60. #if defined(__ICACHE_PRESENT) && __ICACHE_PRESENT
  61. SCB_DisableICache();
  62. #endif
  63. #if defined(__DCACHE_PRESENT) && __DCACHE_PRESENT
  64. SCB_DisableDCache();
  65. #endif
  66. /*
  67. * Before enter stop modes, the flash cache prefetch should be disabled.
  68. * Otherwise the prefetch might be interrupted by stop, then the data and
  69. * and instruction from flash are wrong.
  70. */
  71. FTFx_CACHE_PflashSetPrefetchSpeculation(&speculationStatus);
  72. }
  73. void SMC_PostExitStopModes(void)
  74. {
  75. ftfx_prefetch_speculation_status_t speculationStatus = {
  76. false, /* Enable instruction speculation.*/
  77. false, /* Enable data speculation.*/
  78. };
  79. FTFx_CACHE_PflashSetPrefetchSpeculation(&speculationStatus);
  80. #if defined(__ICACHE_PRESENT) && __ICACHE_PRESENT
  81. SCB_EnableICache();
  82. #endif
  83. #if defined(__DCACHE_PRESENT) && __DCACHE_PRESENT
  84. SCB_EnableDCache();
  85. #endif
  86. EnableGlobalIRQ(g_savedPrimask);
  87. __ISB();
  88. }
  89. void SMC_PreEnterWaitModes(void)
  90. {
  91. g_savedPrimask = DisableGlobalIRQ();
  92. __ISB();
  93. }
  94. void SMC_PostExitWaitModes(void)
  95. {
  96. EnableGlobalIRQ(g_savedPrimask);
  97. __ISB();
  98. }
  99. status_t SMC_SetPowerModeRun(SMC_Type *base)
  100. {
  101. uint8_t reg;
  102. reg = base->PMCTRL;
  103. /* configure Normal RUN mode */
  104. reg &= ~SMC_PMCTRL_RUNM_MASK;
  105. reg |= (kSMC_RunNormal << SMC_PMCTRL_RUNM_SHIFT);
  106. base->PMCTRL = reg;
  107. return kStatus_Success;
  108. }
  109. #if (defined(FSL_FEATURE_SMC_HAS_HIGH_SPEED_RUN_MODE) && FSL_FEATURE_SMC_HAS_HIGH_SPEED_RUN_MODE)
  110. status_t SMC_SetPowerModeHsrun(SMC_Type *base)
  111. {
  112. uint8_t reg;
  113. reg = base->PMCTRL;
  114. /* configure High Speed RUN mode */
  115. reg &= ~SMC_PMCTRL_RUNM_MASK;
  116. reg |= (kSMC_Hsrun << SMC_PMCTRL_RUNM_SHIFT);
  117. base->PMCTRL = reg;
  118. return kStatus_Success;
  119. }
  120. #endif /* FSL_FEATURE_SMC_HAS_HIGH_SPEED_RUN_MODE */
  121. status_t SMC_SetPowerModeWait(SMC_Type *base)
  122. {
  123. /* configure Normal Wait mode */
  124. SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
  125. __DSB();
  126. __WFI();
  127. __ISB();
  128. return kStatus_Success;
  129. }
  130. status_t SMC_SetPowerModeStop(SMC_Type *base, smc_partial_stop_option_t option)
  131. {
  132. uint8_t reg;
  133. #if (defined(FSL_FEATURE_SMC_HAS_PSTOPO) && FSL_FEATURE_SMC_HAS_PSTOPO)
  134. /* configure the Partial Stop mode in Noraml Stop mode */
  135. reg = base->STOPCTRL;
  136. reg &= ~SMC_STOPCTRL_PSTOPO_MASK;
  137. reg |= ((uint32_t)option << SMC_STOPCTRL_PSTOPO_SHIFT);
  138. base->STOPCTRL = reg;
  139. #endif
  140. /* configure Normal Stop mode */
  141. reg = base->PMCTRL;
  142. reg &= ~SMC_PMCTRL_STOPM_MASK;
  143. reg |= (kSMC_StopNormal << SMC_PMCTRL_STOPM_SHIFT);
  144. base->PMCTRL = reg;
  145. /* Set the SLEEPDEEP bit to enable deep sleep mode (stop mode) */
  146. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  147. /* read back to make sure the configuration valid before enter stop mode */
  148. (void)base->PMCTRL;
  149. __DSB();
  150. __WFI();
  151. __ISB();
  152. /* check whether the power mode enter Stop mode succeed */
  153. if (base->PMCTRL & SMC_PMCTRL_STOPA_MASK)
  154. {
  155. return kStatus_SMC_StopAbort;
  156. }
  157. else
  158. {
  159. return kStatus_Success;
  160. }
  161. }
  162. status_t SMC_SetPowerModeVlpr(SMC_Type *base
  163. #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
  164. ,
  165. bool wakeupMode
  166. #endif
  167. )
  168. {
  169. uint8_t reg;
  170. reg = base->PMCTRL;
  171. #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
  172. /* configure whether the system remains in VLP mode on an interrupt */
  173. if (wakeupMode)
  174. {
  175. /* exits to RUN mode on an interrupt */
  176. reg |= SMC_PMCTRL_LPWUI_MASK;
  177. }
  178. else
  179. {
  180. /* remains in VLP mode on an interrupt */
  181. reg &= ~SMC_PMCTRL_LPWUI_MASK;
  182. }
  183. #endif /* FSL_FEATURE_SMC_HAS_LPWUI */
  184. /* configure VLPR mode */
  185. reg &= ~SMC_PMCTRL_RUNM_MASK;
  186. reg |= (kSMC_RunVlpr << SMC_PMCTRL_RUNM_SHIFT);
  187. base->PMCTRL = reg;
  188. return kStatus_Success;
  189. }
  190. status_t SMC_SetPowerModeVlpw(SMC_Type *base)
  191. {
  192. /* configure VLPW mode */
  193. /* Set the SLEEPDEEP bit to enable deep sleep mode */
  194. SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
  195. __DSB();
  196. __WFI();
  197. __ISB();
  198. return kStatus_Success;
  199. }
  200. status_t SMC_SetPowerModeVlps(SMC_Type *base)
  201. {
  202. uint8_t reg;
  203. /* configure VLPS mode */
  204. reg = base->PMCTRL;
  205. reg &= ~SMC_PMCTRL_STOPM_MASK;
  206. reg |= (kSMC_StopVlps << SMC_PMCTRL_STOPM_SHIFT);
  207. base->PMCTRL = reg;
  208. /* Set the SLEEPDEEP bit to enable deep sleep mode */
  209. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  210. /* read back to make sure the configuration valid before enter stop mode */
  211. (void)base->PMCTRL;
  212. __DSB();
  213. __WFI();
  214. __ISB();
  215. /* check whether the power mode enter VLPS mode succeed */
  216. if (base->PMCTRL & SMC_PMCTRL_STOPA_MASK)
  217. {
  218. return kStatus_SMC_StopAbort;
  219. }
  220. else
  221. {
  222. return kStatus_Success;
  223. }
  224. }
  225. #if (defined(FSL_FEATURE_SMC_HAS_LOW_LEAKAGE_STOP_MODE) && FSL_FEATURE_SMC_HAS_LOW_LEAKAGE_STOP_MODE)
  226. status_t SMC_SetPowerModeLls(SMC_Type *base
  227. #if ((defined(FSL_FEATURE_SMC_HAS_LLS_SUBMODE) && FSL_FEATURE_SMC_HAS_LLS_SUBMODE) || \
  228. (defined(FSL_FEATURE_SMC_HAS_LPOPO) && FSL_FEATURE_SMC_HAS_LPOPO))
  229. ,
  230. const smc_power_mode_lls_config_t *config
  231. #endif
  232. )
  233. {
  234. uint8_t reg;
  235. /* configure to LLS mode */
  236. reg = base->PMCTRL;
  237. reg &= ~SMC_PMCTRL_STOPM_MASK;
  238. reg |= (kSMC_StopLls << SMC_PMCTRL_STOPM_SHIFT);
  239. base->PMCTRL = reg;
  240. /* configure LLS sub-mode*/
  241. #if (defined(FSL_FEATURE_SMC_HAS_LLS_SUBMODE) && FSL_FEATURE_SMC_HAS_LLS_SUBMODE)
  242. reg = base->STOPCTRL;
  243. reg &= ~SMC_STOPCTRL_LLSM_MASK;
  244. reg |= ((uint32_t)config->subMode << SMC_STOPCTRL_LLSM_SHIFT);
  245. base->STOPCTRL = reg;
  246. #endif /* FSL_FEATURE_SMC_HAS_LLS_SUBMODE */
  247. #if (defined(FSL_FEATURE_SMC_HAS_LPOPO) && FSL_FEATURE_SMC_HAS_LPOPO)
  248. if (config->enableLpoClock)
  249. {
  250. base->STOPCTRL &= ~SMC_STOPCTRL_LPOPO_MASK;
  251. }
  252. else
  253. {
  254. base->STOPCTRL |= SMC_STOPCTRL_LPOPO_MASK;
  255. }
  256. #endif /* FSL_FEATURE_SMC_HAS_LPOPO */
  257. /* Set the SLEEPDEEP bit to enable deep sleep mode */
  258. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  259. /* read back to make sure the configuration valid before enter stop mode */
  260. (void)base->PMCTRL;
  261. __DSB();
  262. __WFI();
  263. __ISB();
  264. /* check whether the power mode enter LLS mode succeed */
  265. if (base->PMCTRL & SMC_PMCTRL_STOPA_MASK)
  266. {
  267. return kStatus_SMC_StopAbort;
  268. }
  269. else
  270. {
  271. return kStatus_Success;
  272. }
  273. }
  274. #endif /* FSL_FEATURE_SMC_HAS_LOW_LEAKAGE_STOP_MODE */
  275. #if (defined(FSL_FEATURE_SMC_HAS_VERY_LOW_LEAKAGE_STOP_MODE) && FSL_FEATURE_SMC_HAS_VERY_LOW_LEAKAGE_STOP_MODE)
  276. status_t SMC_SetPowerModeVlls(SMC_Type *base, const smc_power_mode_vlls_config_t *config)
  277. {
  278. uint8_t reg;
  279. #if (defined(FSL_FEATURE_SMC_HAS_PORPO) && FSL_FEATURE_SMC_HAS_PORPO)
  280. #if (defined(FSL_FEATURE_SMC_USE_VLLSCTRL_REG) && FSL_FEATURE_SMC_USE_VLLSCTRL_REG) || \
  281. (defined(FSL_FEATURE_SMC_USE_STOPCTRL_VLLSM) && FSL_FEATURE_SMC_USE_STOPCTRL_VLLSM) || \
  282. (defined(FSL_FEATURE_SMC_HAS_LLS_SUBMODE) && FSL_FEATURE_SMC_HAS_LLS_SUBMODE)
  283. if (config->subMode == kSMC_StopSub0)
  284. #endif
  285. {
  286. /* configure whether the Por Detect work in Vlls0 mode */
  287. if (config->enablePorDetectInVlls0)
  288. {
  289. #if (defined(FSL_FEATURE_SMC_USE_VLLSCTRL_REG) && FSL_FEATURE_SMC_USE_VLLSCTRL_REG)
  290. base->VLLSCTRL &= ~SMC_VLLSCTRL_PORPO_MASK;
  291. #else
  292. base->STOPCTRL &= ~SMC_STOPCTRL_PORPO_MASK;
  293. #endif
  294. }
  295. else
  296. {
  297. #if (defined(FSL_FEATURE_SMC_USE_VLLSCTRL_REG) && FSL_FEATURE_SMC_USE_VLLSCTRL_REG)
  298. base->VLLSCTRL |= SMC_VLLSCTRL_PORPO_MASK;
  299. #else
  300. base->STOPCTRL |= SMC_STOPCTRL_PORPO_MASK;
  301. #endif
  302. }
  303. }
  304. #endif /* FSL_FEATURE_SMC_HAS_PORPO */
  305. #if (defined(FSL_FEATURE_SMC_HAS_RAM2_POWER_OPTION) && FSL_FEATURE_SMC_HAS_RAM2_POWER_OPTION)
  306. else if (config->subMode == kSMC_StopSub2)
  307. {
  308. /* configure whether the Por Detect work in Vlls0 mode */
  309. if (config->enableRam2InVlls2)
  310. {
  311. #if (defined(FSL_FEATURE_SMC_USE_VLLSCTRL_REG) && FSL_FEATURE_SMC_USE_VLLSCTRL_REG)
  312. base->VLLSCTRL |= SMC_VLLSCTRL_RAM2PO_MASK;
  313. #else
  314. base->STOPCTRL |= SMC_STOPCTRL_RAM2PO_MASK;
  315. #endif
  316. }
  317. else
  318. {
  319. #if (defined(FSL_FEATURE_SMC_USE_VLLSCTRL_REG) && FSL_FEATURE_SMC_USE_VLLSCTRL_REG)
  320. base->VLLSCTRL &= ~SMC_VLLSCTRL_RAM2PO_MASK;
  321. #else
  322. base->STOPCTRL &= ~SMC_STOPCTRL_RAM2PO_MASK;
  323. #endif
  324. }
  325. }
  326. else
  327. {
  328. }
  329. #endif /* FSL_FEATURE_SMC_HAS_RAM2_POWER_OPTION */
  330. /* configure to VLLS mode */
  331. reg = base->PMCTRL;
  332. reg &= ~SMC_PMCTRL_STOPM_MASK;
  333. reg |= (kSMC_StopVlls << SMC_PMCTRL_STOPM_SHIFT);
  334. base->PMCTRL = reg;
  335. /* configure the VLLS sub-mode */
  336. #if (defined(FSL_FEATURE_SMC_USE_VLLSCTRL_REG) && FSL_FEATURE_SMC_USE_VLLSCTRL_REG)
  337. reg = base->VLLSCTRL;
  338. reg &= ~SMC_VLLSCTRL_VLLSM_MASK;
  339. reg |= ((uint32_t)config->subMode << SMC_VLLSCTRL_VLLSM_SHIFT);
  340. base->VLLSCTRL = reg;
  341. #else
  342. #if (defined(FSL_FEATURE_SMC_HAS_LLS_SUBMODE) && FSL_FEATURE_SMC_HAS_LLS_SUBMODE)
  343. reg = base->STOPCTRL;
  344. reg &= ~SMC_STOPCTRL_LLSM_MASK;
  345. reg |= ((uint32_t)config->subMode << SMC_STOPCTRL_LLSM_SHIFT);
  346. base->STOPCTRL = reg;
  347. #else
  348. reg = base->STOPCTRL;
  349. reg &= ~SMC_STOPCTRL_VLLSM_MASK;
  350. reg |= ((uint32_t)config->subMode << SMC_STOPCTRL_VLLSM_SHIFT);
  351. base->STOPCTRL = reg;
  352. #endif /* FSL_FEATURE_SMC_HAS_LLS_SUBMODE */
  353. #endif
  354. #if (defined(FSL_FEATURE_SMC_HAS_LPOPO) && FSL_FEATURE_SMC_HAS_LPOPO)
  355. if (config->enableLpoClock)
  356. {
  357. base->STOPCTRL &= ~SMC_STOPCTRL_LPOPO_MASK;
  358. }
  359. else
  360. {
  361. base->STOPCTRL |= SMC_STOPCTRL_LPOPO_MASK;
  362. }
  363. #endif /* FSL_FEATURE_SMC_HAS_LPOPO */
  364. /* Set the SLEEPDEEP bit to enable deep sleep mode */
  365. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  366. /* read back to make sure the configuration valid before enter stop mode */
  367. (void)base->PMCTRL;
  368. __DSB();
  369. __WFI();
  370. __ISB();
  371. /* check whether the power mode enter LLS mode succeed */
  372. if (base->PMCTRL & SMC_PMCTRL_STOPA_MASK)
  373. {
  374. return kStatus_SMC_StopAbort;
  375. }
  376. else
  377. {
  378. return kStatus_Success;
  379. }
  380. }
  381. #endif /* FSL_FEATURE_SMC_HAS_VERY_LOW_LEAKAGE_STOP_MODE */