fsl_ftfx_cache.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * The Clear BSD License
  3. * Copyright 2013-2016 Freescale Semiconductor, Inc.
  4. * Copyright 2016-2018 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted (subject to the limitations in the
  9. * disclaimer below) provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * * Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from
  20. * this software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  23. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  24. * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  34. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "fsl_ftfx_cache.h"
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. /*!
  42. * @name Flash cache and speculation control defines
  43. * @{
  44. */
  45. #if defined(MCM_PLACR_CFCC_MASK)
  46. #define FLASH_CACHE_IS_CONTROLLED_BY_MCM (1)
  47. #else
  48. #define FLASH_CACHE_IS_CONTROLLED_BY_MCM (0)
  49. #endif
  50. #define FLASH_CACHE_IS_CONTROLLED_BY_MSCM (0)
  51. #if defined(FMC_PFB0CR_CINV_WAY_MASK) || defined(FMC_PFB01CR_CINV_WAY_MASK)
  52. #define FLASH_CACHE_IS_CONTROLLED_BY_FMC (1)
  53. #else
  54. #define FLASH_CACHE_IS_CONTROLLED_BY_FMC (0)
  55. #endif
  56. #if defined(MCM_PLACR_DFCS_MASK)
  57. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM (1)
  58. #else
  59. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM (0)
  60. #endif
  61. #if defined(MSCM_OCMDR_OCMC1_MASK) || defined(MSCM_OCMDR_OCM1_MASK) || defined(MSCM_OCMDR0_OCM1_MASK) || \
  62. defined(MSCM_OCMDR1_OCM1_MASK)
  63. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM (1)
  64. #else
  65. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM (0)
  66. #endif
  67. #if defined(FMC_PFB0CR_S_INV_MASK) || defined(FMC_PFB0CR_S_B_INV_MASK) || defined(FMC_PFB01CR_S_INV_MASK) || \
  68. defined(FMC_PFB01CR_S_B_INV_MASK)
  69. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC (1)
  70. #else
  71. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC (0)
  72. #endif
  73. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM || FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC || \
  74. FLASH_CACHE_IS_CONTROLLED_BY_MCM || FLASH_CACHE_IS_CONTROLLED_BY_FMC || FLASH_CACHE_IS_CONTROLLED_BY_MSCM
  75. #define FLASH_IS_CACHE_INVALIDATION_AVAILABLE (1)
  76. #else
  77. #define FLASH_IS_CACHE_INVALIDATION_AVAILABLE (0)
  78. #endif
  79. /*@}*/
  80. /*! @brief A function pointer used to point to relocated ftfx_common_bit_operation() */
  81. typedef void (*callftfxCommonBitOperation_t)(FTFx_REG32_ACCESS_TYPE base,
  82. uint32_t bitMask,
  83. uint32_t bitShift,
  84. uint32_t bitValue);
  85. /*******************************************************************************
  86. * Prototypes
  87. ******************************************************************************/
  88. #if FLASH_CACHE_IS_CONTROLLED_BY_MCM
  89. /*! @brief Performs the cache clear to the flash by MCM.*/
  90. void mcm_flash_cache_clear(ftfx_cache_config_t *config);
  91. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_MCM */
  92. #if FLASH_CACHE_IS_CONTROLLED_BY_MSCM
  93. /*! @brief Performs the cache clear to the flash by MSCM.*/
  94. void mscm_flash_cache_clear(ftfx_cache_config_t *config);
  95. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_MSCM */
  96. #if FLASH_CACHE_IS_CONTROLLED_BY_FMC
  97. /*! @brief Performs the cache clear to the flash by FMC.*/
  98. void fmc_flash_cache_clear(ftfx_cache_config_t *config);
  99. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_FMC */
  100. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  101. /*! @brief Sets the prefetch speculation buffer to the flash by MSCM.*/
  102. void mscm_flash_prefetch_speculation_enable(ftfx_cache_config_t *config, bool enable);
  103. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM */
  104. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  105. /*! @brief Performs the prefetch speculation buffer clear to the flash by FMC.*/
  106. void fmc_flash_prefetch_speculation_clear(ftfx_cache_config_t *config);
  107. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC */
  108. #if FTFx_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  109. /*! @brief Copy flash_cache_clear_command() to RAM*/
  110. static void ftfx_copy_common_bit_operation_to_ram(uint32_t *ftfxCommonBitOperation);
  111. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT */
  112. /*******************************************************************************
  113. * Variables
  114. ******************************************************************************/
  115. #if FTFx_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  116. /*!
  117. * @brief Position independent code of ftfx_common_bit_operation()
  118. *
  119. * Note1: The prototype of C function is shown as below:
  120. * @code
  121. * void ftfx_common_bit_operation(FTFx_REG32_ACCESS_TYPE base, uint32_t bitMask, uint32_t bitShift, uint32_t
  122. * bitValue)
  123. * {
  124. * if (bitMask)
  125. * {
  126. * uint32_t value = (((uint32_t)(((uint32_t)(bitValue)) << bitShift)) & bitMask);
  127. * *base = (*base & (~bitMask)) | value;
  128. * }
  129. *
  130. * __ISB();
  131. * __DSB();
  132. * }
  133. * @endcode
  134. * Note2: The binary code is generated by IAR 7.70.1
  135. */
  136. static const uint16_t s_ftfxCommonBitOperationFunctionCode[] = {
  137. 0xb510, /* PUSH {R4, LR} */
  138. 0x2900, /* CMP R1, #0 */
  139. 0xd005, /* BEQ.N @12 */
  140. 0x6804, /* LDR R4, [R0] */
  141. 0x438c, /* BICS R4, R4, R1 */
  142. 0x4093, /* LSLS R3, R3, R2 */
  143. 0x4019, /* ANDS R1, R1, R3 */
  144. 0x4321, /* ORRS R1, R1, R4 */
  145. 0x6001, /* STR R1, [R0] */
  146. /* @12: */
  147. 0xf3bf, 0x8f6f, /* ISB */
  148. 0xf3bf, 0x8f4f, /* DSB */
  149. 0xbd10 /* POP {R4, PC} */
  150. };
  151. #if (!FTFx_DRIVER_IS_EXPORTED)
  152. /*! @brief A static buffer used to hold ftfx_common_bit_operation() */
  153. static uint32_t s_ftfxCommonBitOperation[kFTFx_CACHE_RamFuncMaxSizeInWords];
  154. #endif /* (!FTFx_DRIVER_IS_EXPORTED) */
  155. #endif /* FLASH_IS_CACHE_INVALIDATION_AVAILABLE && FTFx_DRIVER_IS_FLASH_RESIDENT */
  156. /*******************************************************************************
  157. * Code
  158. ******************************************************************************/
  159. status_t FTFx_CACHE_Init(ftfx_cache_config_t *config)
  160. {
  161. if (config == NULL)
  162. {
  163. return kStatus_FTFx_InvalidArgument;
  164. }
  165. /* copy required flash commands to RAM */
  166. #if FTFx_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  167. if (NULL == config->comBitOperFuncAddr)
  168. {
  169. #if FTFx_DRIVER_IS_EXPORTED
  170. return kStatus_FTFx_ExecuteInRamFunctionNotReady;
  171. #else
  172. config->comBitOperFuncAddr = s_ftfxCommonBitOperation;
  173. #endif /* FTFx_DRIVER_IS_EXPORTED */
  174. }
  175. ftfx_copy_common_bit_operation_to_ram(config->comBitOperFuncAddr);
  176. #endif /* FLASH_IS_CACHE_INVALIDATION_AVAILABLE && FTFx_DRIVER_IS_FLASH_RESIDENT */
  177. return kStatus_FTFx_Success;
  178. }
  179. /*!
  180. * @brief Flash Cache/Prefetch/Speculation Clear Process
  181. *
  182. * This function is used to perform the cache and prefetch speculation clear process to the flash.
  183. */
  184. status_t FTFx_CACHE_ClearCachePrefetchSpeculation(ftfx_cache_config_t *config, bool isPreProcess)
  185. {
  186. /* We pass the ftfx register address as a parameter to ftfx_common_bit_operation() instead of using
  187. * pre-processed MACROs or a global variable in ftfx_common_bit_operation()
  188. * to make sure that ftfx_common_bit_operation() will be compiled into position-independent code (PIC). */
  189. if (!isPreProcess)
  190. {
  191. #if FLASH_CACHE_IS_CONTROLLED_BY_MCM
  192. mcm_flash_cache_clear(config);
  193. #endif
  194. #if FLASH_CACHE_IS_CONTROLLED_BY_MSCM
  195. mscm_flash_cache_clear(config);
  196. #endif
  197. #if FLASH_CACHE_IS_CONTROLLED_BY_FMC
  198. fmc_flash_cache_clear(config);
  199. #endif
  200. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  201. mscm_flash_prefetch_speculation_enable(config, true);
  202. #endif
  203. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  204. fmc_flash_prefetch_speculation_clear(config);
  205. #endif
  206. }
  207. if (isPreProcess)
  208. {
  209. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  210. mscm_flash_prefetch_speculation_enable(config, false);
  211. #endif
  212. }
  213. return kStatus_FTFx_Success;
  214. }
  215. status_t FTFx_CACHE_PflashSetPrefetchSpeculation(ftfx_prefetch_speculation_status_t *speculationStatus)
  216. {
  217. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM
  218. {
  219. if (speculationStatus->instructionOff)
  220. {
  221. if (!speculationStatus->dataOff)
  222. {
  223. return kStatus_FTFx_InvalidSpeculationOption;
  224. }
  225. else
  226. {
  227. MCM0_CACHE_REG |= MCM_PLACR_DFCS_MASK;
  228. }
  229. }
  230. else
  231. {
  232. MCM0_CACHE_REG &= ~MCM_PLACR_DFCS_MASK;
  233. if (!speculationStatus->dataOff)
  234. {
  235. MCM0_CACHE_REG |= MCM_PLACR_EFDS_MASK;
  236. }
  237. else
  238. {
  239. MCM0_CACHE_REG &= ~MCM_PLACR_EFDS_MASK;
  240. }
  241. }
  242. }
  243. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  244. {
  245. if (!speculationStatus->instructionOff)
  246. {
  247. FMC_CACHE_REG |= FMC_CACHE_B0IPE_MASK;
  248. }
  249. else
  250. {
  251. FMC_CACHE_REG &= ~FMC_CACHE_B0IPE_MASK;
  252. }
  253. if (!speculationStatus->dataOff)
  254. {
  255. FMC_CACHE_REG |= FMC_CACHE_B0DPE_MASK;
  256. }
  257. else
  258. {
  259. FMC_CACHE_REG &= ~FMC_CACHE_B0DPE_MASK;
  260. }
  261. /* Invalidate Prefetch Speculation Buffer */
  262. FMC_SPECULATION_INVALIDATE_REG |= FMC_SPECULATION_INVALIDATE_MASK;
  263. }
  264. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  265. {
  266. if (speculationStatus->instructionOff)
  267. {
  268. if (!speculationStatus->dataOff)
  269. {
  270. return kStatus_FTFx_InvalidSpeculationOption;
  271. }
  272. else
  273. {
  274. MSCM_OCMDR0_REG |= MSCM_OCMDR_OCMC1_DFCS_MASK;
  275. }
  276. }
  277. else
  278. {
  279. MSCM_OCMDR0_REG &= ~MSCM_OCMDR_OCMC1_DFCS_MASK;
  280. if (!speculationStatus->dataOff)
  281. {
  282. MSCM_OCMDR0_REG &= ~MSCM_OCMDR_OCMC1_DFDS_MASK;
  283. }
  284. else
  285. {
  286. MSCM_OCMDR0_REG |= MSCM_OCMDR_OCMC1_DFDS_MASK;
  287. }
  288. }
  289. }
  290. #endif /* FSL_FEATURE_FTFx_MCM_FLASH_CACHE_CONTROLS */
  291. return kStatus_FTFx_Success;
  292. }
  293. status_t FTFx_CACHE_PflashGetPrefetchSpeculation(ftfx_prefetch_speculation_status_t *speculationStatus)
  294. {
  295. memset(speculationStatus, 0, sizeof(ftfx_prefetch_speculation_status_t));
  296. /* Assuming that all speculation options are enabled. */
  297. speculationStatus->instructionOff = false;
  298. speculationStatus->dataOff = false;
  299. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM
  300. {
  301. uint32_t value = MCM0_CACHE_REG;
  302. if (value & MCM_PLACR_DFCS_MASK)
  303. {
  304. /* Speculation buffer is off. */
  305. speculationStatus->instructionOff = true;
  306. speculationStatus->dataOff = true;
  307. }
  308. else
  309. {
  310. /* Speculation buffer is on for instruction. */
  311. if (!(value & MCM_PLACR_EFDS_MASK))
  312. {
  313. /* Speculation buffer is off for data. */
  314. speculationStatus->dataOff = true;
  315. }
  316. }
  317. }
  318. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  319. {
  320. uint32_t value = FMC_CACHE_REG;
  321. if (!(value & FMC_CACHE_B0DPE_MASK))
  322. {
  323. /* Do not prefetch in response to data references. */
  324. speculationStatus->dataOff = true;
  325. }
  326. if (!(value & FMC_CACHE_B0IPE_MASK))
  327. {
  328. /* Do not prefetch in response to instruction fetches. */
  329. speculationStatus->instructionOff = true;
  330. }
  331. }
  332. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  333. {
  334. uint32_t value = MSCM_OCMDR0_REG;
  335. if (value & MSCM_OCMDR_OCMC1_DFCS_MASK)
  336. {
  337. /* Speculation buffer is off. */
  338. speculationStatus->instructionOff = true;
  339. speculationStatus->dataOff = true;
  340. }
  341. else
  342. {
  343. /* Speculation buffer is on for instruction. */
  344. if (value & MSCM_OCMDR_OCMC1_DFDS_MASK)
  345. {
  346. /* Speculation buffer is off for data. */
  347. speculationStatus->dataOff = true;
  348. }
  349. }
  350. }
  351. #endif
  352. return kStatus_FTFx_Success;
  353. }
  354. #if FTFx_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  355. /*! @brief Copy PIC of ftfx_common_bit_operation() to RAM */
  356. static void ftfx_copy_common_bit_operation_to_ram(uint32_t *ftfxCommonBitOperation)
  357. {
  358. assert(sizeof(s_ftfxCommonBitOperationFunctionCode) <= (kFTFx_CACHE_RamFuncMaxSizeInWords * 4));
  359. memcpy(ftfxCommonBitOperation, s_ftfxCommonBitOperationFunctionCode,
  360. sizeof(s_ftfxCommonBitOperationFunctionCode));
  361. }
  362. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE */
  363. #if FLASH_CACHE_IS_CONTROLLED_BY_MCM
  364. /*! @brief Performs the cache clear to the flash by MCM.*/
  365. void mcm_flash_cache_clear(ftfx_cache_config_t *config)
  366. {
  367. FTFx_REG32_ACCESS_TYPE regBase;
  368. #if defined(MCM0_CACHE_REG)
  369. regBase = (FTFx_REG32_ACCESS_TYPE)&MCM0_CACHE_REG;
  370. #elif defined(MCM1_CACHE_REG)
  371. regBase = (FTFx_REG32_ACCESS_TYPE)&MCM1_CACHE_REG;
  372. #endif
  373. #if FTFx_DRIVER_IS_FLASH_RESIDENT
  374. /* Since the value of ARM function pointer is always odd, but the real start address
  375. * of function memory should be even, that's why +1 operation exist. */
  376. callftfxCommonBitOperation_t callftfxCommonBitOperation = (callftfxCommonBitOperation_t)((uint32_t)config->comBitOperFuncAddr + 1);
  377. callftfxCommonBitOperation(regBase, MCM_CACHE_CLEAR_MASK, MCM_CACHE_CLEAR_SHIFT, 1U);
  378. #else /* !FTFx_DRIVER_IS_FLASH_RESIDENT */
  379. *regBase |= MCM_CACHE_CLEAR_MASK;
  380. /* Memory barriers for good measure.
  381. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  382. __ISB();
  383. __DSB();
  384. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT */
  385. }
  386. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_MCM */
  387. #if FLASH_CACHE_IS_CONTROLLED_BY_MSCM
  388. /*! @brief Performs the cache clear to the flash by MSCM.*/
  389. void mscm_flash_cache_clear(ftfx_cache_config_t *config)
  390. {
  391. uint8_t setValue = 0x1U;
  392. /* The OCMDR[0] is always used to cache main Pflash*/
  393. /* For device with FlexNVM support, the OCMDR[1] is used to cache Dflash.
  394. * For device with secondary flash support, the OCMDR[1] is used to cache secondary Pflash. */
  395. #if FTFx_DRIVER_IS_FLASH_RESIDENT
  396. /* Since the value of ARM function pointer is always odd, but the real start address
  397. * of function memory should be even, that's why +1 operation exist. */
  398. callftfxCommonBitOperation_t callftfxCommonBitOperation = (callftfxCommonBitOperation_t)((uint32_t)config->comBitOperFuncAddr + 1);
  399. switch (config->flashMemoryIndex)
  400. {
  401. case kFLASH_MemoryIndexSecondaryFlash:
  402. callftfxCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR1_REG, MSCM_CACHE_CLEAR_MASK,
  403. MSCM_CACHE_CLEAR_SHIFT, setValue);
  404. break;
  405. case kFLASH_MemoryIndexPrimaryFlash:
  406. default:
  407. callftfxCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR0_REG, MSCM_CACHE_CLEAR_MASK,
  408. MSCM_CACHE_CLEAR_SHIFT, setValue);
  409. break;
  410. }
  411. #else /* !FTFx_DRIVER_IS_FLASH_RESIDENT */
  412. switch (config->flashMemoryIndex)
  413. {
  414. case kFLASH_MemoryIndexSecondaryFlash:
  415. MSCM_OCMDR1_REG = (MSCM_OCMDR1_REG & (~MSCM_CACHE_CLEAR_MASK)) | MSCM_CACHE_CLEAR(setValue);
  416. /* Each cahce clear instaruction should be followed by below code*/
  417. __ISB();
  418. __DSB();
  419. break;
  420. case kFLASH_MemoryIndexPrimaryFlash:
  421. default:
  422. MSCM_OCMDR0_REG = (MSCM_OCMDR0_REG & (~MSCM_CACHE_CLEAR_MASK)) | MSCM_CACHE_CLEAR(setValue);
  423. /* Memory barriers for good measure.
  424. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  425. __ISB();
  426. __DSB();
  427. break;
  428. }
  429. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT */
  430. }
  431. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_MSCM */
  432. #if FLASH_CACHE_IS_CONTROLLED_BY_FMC
  433. /*! @brief Performs the cache clear to the flash by FMC.*/
  434. void fmc_flash_cache_clear(ftfx_cache_config_t *config)
  435. {
  436. #if FTFx_DRIVER_IS_FLASH_RESIDENT
  437. /* Since the value of ARM function pointer is always odd, but the real start address
  438. * of function memory should be even, that's why +1 operation exist. */
  439. callftfxCommonBitOperation_t callftfxCommonBitOperation = (callftfxCommonBitOperation_t)((uint32_t)config->comBitOperFuncAddr + 1);
  440. callftfxCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&FMC_CACHE_REG, FMC_CACHE_CLEAR_MASK, FMC_CACHE_CLEAR_SHIFT, 0xFU);
  441. #else /* !FTFx_DRIVER_IS_FLASH_RESIDENT */
  442. FMC_CACHE_REG = (FMC_CACHE_REG & (~FMC_CACHE_CLEAR_MASK)) | FMC_CACHE_CLEAR(~0);
  443. /* Memory barriers for good measure.
  444. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  445. __ISB();
  446. __DSB();
  447. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT */
  448. }
  449. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_FMC */
  450. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  451. /*! @brief Performs the prefetch speculation buffer clear to the flash by MSCM.*/
  452. void mscm_flash_prefetch_speculation_enable(ftfx_cache_config_t *config, bool enable)
  453. {
  454. uint8_t setValue;
  455. if (enable)
  456. {
  457. setValue = 0x0U;
  458. }
  459. else
  460. {
  461. setValue = 0x3U;
  462. }
  463. /* The OCMDR[0] is always used to prefetch main Pflash*/
  464. /* For device with FlexNVM support, the OCMDR[1] is used to prefetch Dflash.
  465. * For device with secondary flash support, the OCMDR[1] is used to prefetch secondary Pflash. */
  466. #if FTFx_DRIVER_IS_FLASH_RESIDENT
  467. /* Since the value of ARM function pointer is always odd, but the real start address
  468. * of function memory should be even, that's why +1 operation exist. */
  469. callftfxCommonBitOperation_t callftfxCommonBitOperation = (callftfxCommonBitOperation_t)((uint32_t)config->comBitOperFuncAddr + 1);
  470. switch (config->flashMemoryIndex)
  471. {
  472. case 1:
  473. callftfxCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR1_REG, MSCM_SPECULATION_SET_MASK,
  474. MSCM_SPECULATION_SET_SHIFT, setValue);
  475. break;
  476. case 0:
  477. default:
  478. callftfxCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR0_REG, MSCM_SPECULATION_SET_MASK,
  479. MSCM_SPECULATION_SET_SHIFT, setValue);
  480. break;
  481. }
  482. #else /* !FTFx_DRIVER_IS_FLASH_RESIDENT */
  483. switch (config->flashMemoryIndex)
  484. {
  485. case kFLASH_MemoryIndexSecondaryFlash:
  486. MSCM_OCMDR1_REG = (MSCM_OCMDR1_REG & (~MSCM_SPECULATION_SET_MASK)) | MSCM_SPECULATION_SET(setValue);
  487. /* Each cahce clear instaruction should be followed by below code*/
  488. __ISB();
  489. __DSB();
  490. break;
  491. case kFLASH_MemoryIndexPrimaryFlash:
  492. default:
  493. MSCM_OCMDR0_REG = (MSCM_OCMDR0_REG & (~MSCM_SPECULATION_SET_MASK)) | MSCM_SPECULATION_SET(setValue);
  494. /* Memory barriers for good measure.
  495. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  496. __ISB();
  497. __DSB();
  498. break;
  499. }
  500. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT */
  501. }
  502. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM */
  503. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  504. /*! @brief Performs the prefetch speculation buffer clear to the flash by FMC.*/
  505. void fmc_flash_prefetch_speculation_clear(ftfx_cache_config_t *config)
  506. {
  507. #if FTFx_DRIVER_IS_FLASH_RESIDENT
  508. /* Since the value of ARM function pointer is always odd, but the real start address
  509. * of function memory should be even, that's why +1 operation exist. */
  510. callftfxCommonBitOperation_t callftfxCommonBitOperation = (callftfxCommonBitOperation_t)((uint32_t)config->comBitOperFuncAddr + 1);
  511. callftfxCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&FMC_SPECULATION_INVALIDATE_REG, FMC_SPECULATION_INVALIDATE_MASK, FMC_SPECULATION_INVALIDATE_SHIFT, 1U);
  512. #else /* !FTFx_DRIVER_IS_FLASH_RESIDENT */
  513. FMC_SPECULATION_INVALIDATE_REG |= FMC_SPECULATION_INVALIDATE_MASK;
  514. /* Memory barriers for good measure.
  515. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  516. __ISB();
  517. __DSB();
  518. #endif /* FTFx_DRIVER_IS_FLASH_RESIDENT */
  519. }
  520. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC */