fsl_cache.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * The Clear BSD License
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted (subject to the limitations in the disclaimer below) provided
  8. * that the following conditions are met:
  9. *
  10. * o Redistributions of source code must retain the above copyright notice, this list
  11. * of conditions and the following disclaimer.
  12. *
  13. * o Redistributions in binary form must reproduce the above copyright notice, this
  14. * list of conditions and the following disclaimer in the documentation and/or
  15. * other materials provided with the distribution.
  16. *
  17. * o Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  26. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  29. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef _FSL_CACHE_H_
  34. #define _FSL_CACHE_H_
  35. #include "fsl_common.h"
  36. /*!
  37. * @addtogroup cache
  38. * @{
  39. */
  40. /*******************************************************************************
  41. * Definitions
  42. ******************************************************************************/
  43. /*! @name Driver version */
  44. /*@{*/
  45. /*! @brief cache driver version 2.0.1. */
  46. #define FSL_CACHE_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  47. /*@}*/
  48. /*! @brief code bus cache line size is equal to system bus line size, so the unified I/D cache line size equals too. */
  49. #define L1CODEBUSCACHE_LINESIZE_BYTE FSL_FEATURE_L1ICACHE_LINESIZE_BYTE /*!< The code bus CACHE line size is 16B = 128b. */
  50. #define L1SYSTEMBUSCACHE_LINESIZE_BYTE L1CODEBUSCACHE_LINESIZE_BYTE /*!< The system bus CACHE line size is 16B = 128b. */
  51. /*******************************************************************************
  52. * API
  53. ******************************************************************************/
  54. #if defined(__cplusplus)
  55. extern "C" {
  56. #endif
  57. #if (FSL_FEATURE_SOC_LMEM_COUNT == 1)
  58. /*!
  59. * @name cache control for L1 cache (local memory controller for code/system bus cache)
  60. *@{
  61. */
  62. /*!
  63. * @brief Enables the processor code bus cache.
  64. *
  65. */
  66. void L1CACHE_EnableCodeCache(void);
  67. /*!
  68. * @brief Disables the processor code bus cache.
  69. *
  70. */
  71. void L1CACHE_DisableCodeCache(void);
  72. /*!
  73. * @brief Invalidates the processor code bus cache.
  74. *
  75. */
  76. void L1CACHE_InvalidateCodeCache(void);
  77. /*!
  78. * @brief Invalidates processor code bus cache by range.
  79. *
  80. * @param address The physical address of cache.
  81. * @param size_byte size of the memory to be invalidated.
  82. * @note Address and size should be aligned to "L1CODCACHE_LINESIZE_BYTE".
  83. * The startAddr here will be forced to align to L1CODEBUSCACHE_LINESIZE_BYTE if
  84. * startAddr is not aligned. For the size_byte, application should make sure the
  85. * alignment or make sure the right operation order if the size_byte is not aligned.
  86. */
  87. void L1CACHE_InvalidateCodeCacheByRange(uint32_t address, uint32_t size_byte);
  88. /*!
  89. * @brief Cleans the processor code bus cache.
  90. *
  91. */
  92. void L1CACHE_CleanCodeCache(void);
  93. /*!
  94. * @brief Cleans processor code bus cache by range.
  95. *
  96. * @param address The physical address of cache.
  97. * @param size_byte size of the memory to be cleaned.
  98. * @note Address and size should be aligned to "L1CODEBUSCACHE_LINESIZE_BYTE".
  99. * The startAddr here will be forced to align to L1CODEBUSCACHE_LINESIZE_BYTE if
  100. * startAddr is not aligned. For the size_byte, application should make sure the
  101. * alignment or make sure the right operation order if the size_byte is not aligned.
  102. */
  103. void L1CACHE_CleanCodeCacheByRange(uint32_t address, uint32_t size_byte);
  104. /*!
  105. * @brief Cleans and invalidates the processor code bus cache.
  106. *
  107. */
  108. void L1CACHE_CleanInvalidateCodeCache(void);
  109. /*!
  110. * @brief Cleans and invalidate processor code bus cache by range.
  111. *
  112. * @param address The physical address of cache.
  113. * @param size_byte size of the memory to be Cleaned and Invalidated.
  114. * @note Address and size should be aligned to "L1CODEBUSCACHE_LINESIZE_BYTE".
  115. * The startAddr here will be forced to align to L1CODEBUSCACHE_LINESIZE_BYTE if
  116. * startAddr is not aligned. For the size_byte, application should make sure the
  117. * alignment or make sure the right operation order if the size_byte is not aligned.
  118. */
  119. void L1CACHE_CleanInvalidateCodeCacheByRange(uint32_t address, uint32_t size_byte);
  120. /*!
  121. * @brief Enables/disables the processor code bus write buffer.
  122. *
  123. * @param enable The enable or disable flag.
  124. * true - enable the code bus write buffer.
  125. * false - disable the code bus write buffer.
  126. */
  127. static inline void L1CACHE_EnableCodeCacheWriteBuffer(bool enable)
  128. {
  129. if (enable)
  130. {
  131. LMEM->PCCCR |= LMEM_PCCCR_ENWRBUF_MASK;
  132. }
  133. else
  134. {
  135. LMEM->PCCCR &= ~LMEM_PCCCR_ENWRBUF_MASK;
  136. }
  137. }
  138. #if defined(FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE) && FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE
  139. /*!
  140. * @brief Enables the processor system bus cache.
  141. *
  142. */
  143. void L1CACHE_EnableSystemCache(void);
  144. /*!
  145. * @brief Disables the processor system bus cache.
  146. *
  147. */
  148. void L1CACHE_DisableSystemCache(void);
  149. /*!
  150. * @brief Invalidates the processor system bus cache.
  151. *
  152. */
  153. void L1CACHE_InvalidateSystemCache(void);
  154. /*!
  155. * @brief Invalidates processor system bus cache by range.
  156. *
  157. * @param address The physical address of cache.
  158. * @param size_byte size of the memory to be invalidated.
  159. * @note Address and size should be aligned to "L1SYSTEMBUSCACHE_LINESIZE_BYTE".
  160. * The startAddr here will be forced to align to L1SYSTEMBUSCACHE_LINESIZE_BYTE if
  161. * startAddr is not aligned. For the size_byte, application should make sure the
  162. * alignment or make sure the right operation order if the size_byte is not aligned.
  163. */
  164. void L1CACHE_InvalidateSystemCacheByRange(uint32_t address, uint32_t size_byte);
  165. /*!
  166. * @brief Cleans the processor system bus cache.
  167. *
  168. */
  169. void L1CACHE_CleanSystemCache(void);
  170. /*!
  171. * @brief Cleans processor system bus cache by range.
  172. *
  173. * @param address The physical address of cache.
  174. * @param size_byte size of the memory to be cleaned.
  175. * @note Address and size should be aligned to "L1SYSTEMBUSCACHE_LINESIZE_BYTE".
  176. * The startAddr here will be forced to align to L1SYSTEMBUSCACHE_LINESIZE_BYTE if
  177. * startAddr is not aligned. For the size_byte, application should make sure the
  178. * alignment or make sure the right operation order if the size_byte is not aligned.
  179. */
  180. void L1CACHE_CleanSystemCacheByRange(uint32_t address, uint32_t size_byte);
  181. /*!
  182. * @brief Cleans and invalidates the processor system bus cache.
  183. *
  184. */
  185. void L1CACHE_CleanInvalidateSystemCache(void);
  186. /*!
  187. * @brief Cleans and Invalidates processor system bus cache by range.
  188. *
  189. * @param address The physical address of cache.
  190. * @param size_byte size of the memory to be Clean and Invalidated.
  191. * @note Address and size should be aligned to "L1SYSTEMBUSCACHE_LINESIZE_BYTE".
  192. * The startAddr here will be forced to align to L1SYSTEMBUSCACHE_LINESIZE_BYTE if
  193. * startAddr is not aligned. For the size_byte, application should make sure the
  194. * alignment or make sure the right operation order if the size_byte is not aligned.
  195. */
  196. void L1CACHE_CleanInvalidateSystemCacheByRange(uint32_t address, uint32_t size_byte);
  197. /*!
  198. * @brief Enables/disables the processor system bus write buffer.
  199. *
  200. * @param enable The enable or disable flag.
  201. * true - enable the code bus write buffer.
  202. * false - disable the code bus write buffer.
  203. */
  204. static inline void L1CACHE_EnableSystemCacheWriteBuffer(bool enable)
  205. {
  206. if (enable)
  207. {
  208. LMEM->PSCCR |= LMEM_PSCCR_ENWRBUF_MASK;
  209. }
  210. else
  211. {
  212. LMEM->PSCCR &= ~LMEM_PSCCR_ENWRBUF_MASK;
  213. }
  214. }
  215. /*@}*/
  216. #endif /* FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE */
  217. /*!
  218. * @name cache control for unified L1 cache driver
  219. *@{
  220. */
  221. /*!
  222. * @brief Invalidates cortex-m4 L1 instrument cache by range.
  223. *
  224. * @param address The start address of the memory to be invalidated.
  225. * @param size_byte The memory size.
  226. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE) aligned.
  227. */
  228. void L1CACHE_InvalidateICacheByRange(uint32_t address, uint32_t size_byte);
  229. /*!
  230. * @brief Invalidates cortex-m4 L1 data cache by range.
  231. *
  232. * @param address The start address of the memory to be invalidated.
  233. * @param size_byte The memory size.
  234. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  235. */
  236. static inline void L1CACHE_InvalidateDCacheByRange(uint32_t address, uint32_t size_byte)
  237. {
  238. L1CACHE_InvalidateICacheByRange(address, size_byte);
  239. }
  240. /*!
  241. * @brief Cleans cortex-m4 L1 data cache by range.
  242. *
  243. * @param address The start address of the memory to be cleaned.
  244. * @param size_byte The memory size.
  245. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  246. */
  247. void L1CACHE_CleanDCacheByRange(uint32_t address, uint32_t size_byte);
  248. /*!
  249. * @brief Cleans and Invalidates cortex-m4 L1 data cache by range.
  250. *
  251. * @param address The start address of the memory to be clean and invalidated.
  252. * @param size_byte The memory size.
  253. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  254. */
  255. void L1CACHE_CleanInvalidateDCacheByRange(uint32_t address, uint32_t size_byte);
  256. /*@}*/
  257. #endif /* FSL_FEATURE_SOC_LMEM_COUNT == 1 */
  258. /*!
  259. * @name Unified Cache Control for all caches
  260. *@{
  261. */
  262. /*!
  263. * @brief Invalidates instruction cache by range.
  264. *
  265. * @param address The physical address.
  266. * @param size_byte size of the memory to be invalidated.
  267. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  268. * FSL_FEATURE_L1ICACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  269. * size if startAddr is not aligned. For the size_byte, application should make sure the
  270. * alignment or make sure the right operation order if the size_byte is not aligned.
  271. */
  272. static inline void ICACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  273. {
  274. L1CACHE_InvalidateICacheByRange(address, size_byte);
  275. }
  276. /*!
  277. * @brief Invalidates data cache by range.
  278. *
  279. * @param address The physical address.
  280. * @param size_byte size of the memory to be invalidated.
  281. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  282. * FSL_FEATURE_L1DCACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  283. * size if startAddr is not aligned. For the size_byte, application should make sure the
  284. * alignment or make sure the right operation order if the size_byte is not aligned.
  285. */
  286. static inline void DCACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  287. {
  288. L1CACHE_InvalidateDCacheByRange(address, size_byte);
  289. }
  290. /*!
  291. * @brief Clean data cache by range.
  292. *
  293. * @param address The physical address.
  294. * @param size_byte size of the memory to be cleaned.
  295. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  296. * FSL_FEATURE_L1DCACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  297. * size if startAddr is not aligned. For the size_byte, application should make sure the
  298. * alignment or make sure the right operation order if the size_byte is not aligned.
  299. */
  300. static inline void DCACHE_CleanByRange(uint32_t address, uint32_t size_byte)
  301. {
  302. L1CACHE_CleanDCacheByRange(address, size_byte);
  303. }
  304. /*!
  305. * @brief Cleans and Invalidates data cache by range.
  306. *
  307. * @param address The physical address.
  308. * @param size_byte size of the memory to be Cleaned and Invalidated.
  309. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  310. * FSL_FEATURE_L1DCACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  311. * size if startAddr is not aligned. For the size_byte, application should make sure the
  312. * alignment or make sure the right operation order if the size_byte is not aligned.
  313. */
  314. static inline void DCACHE_CleanInvalidateByRange(uint32_t address, uint32_t size_byte)
  315. {
  316. L1CACHE_CleanInvalidateDCacheByRange(address, size_byte);
  317. }
  318. /*@}*/
  319. #if defined(__cplusplus)
  320. }
  321. #endif
  322. /*! @}*/
  323. #endif /* _FSL_CACHE_H_*/