sama_cache.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file SAMA5D2x/sama_cache.c
  15. * @brief SAMA CACHE support code.
  16. *
  17. * @addtogroup SAMA5D2x_CACHE
  18. * @{
  19. */
  20. #include "hal.h"
  21. #if !defined(SAMA_L2CC_ASSUME_ENABLED)
  22. #define SAMA_L2CC_ASSUME_ENABLED 0
  23. #endif
  24. #if !defined(SAMA_L2CC_ENABLE)
  25. #define SAMA_L2CC_ENABLE 0
  26. #endif
  27. /**
  28. * @brief Invalidate D-Cache Region
  29. *
  30. * @param[in] start Pointer to beginning of memory region.
  31. * @param[in] length Length of the memory location.
  32. */
  33. void cacheInvalidateRegion(void *start, uint32_t length) {
  34. uint32_t start_addr = (uint32_t)start;
  35. uint32_t end_addr = start_addr + length;
  36. uint32_t mva;
  37. /* Invalidate L1 D-Cache */
  38. for (mva = start_addr & ~(L1_CACHE_BYTES-1); mva < end_addr; mva += L1_CACHE_BYTES) {
  39. L1C_InvalidateDCacheMVA((uint32_t *)mva);
  40. }
  41. #if ARM_SUPPORTS_L2CC
  42. #if SAMA_L2CC_ASSUME_ENABLED || SAMA_L2CC_ENABLE
  43. /* Invalidate L2 Cache */
  44. for (mva = start_addr & ~(L2_CACHE_BYTES-1); mva < end_addr; mva += L2_CACHE_BYTES) {
  45. L2C_InvPa((uint32_t *)mva);
  46. }
  47. #endif
  48. #endif
  49. }
  50. /**
  51. * @brief Clean D-Cache Region
  52. *
  53. * @param[in] start Pointer to beginning of memory region.
  54. * @param[in] length Length of the memory location.
  55. */
  56. void cacheCleanRegion(void *start, uint32_t length) {
  57. uint32_t start_addr = (uint32_t)start;
  58. uint32_t end_addr = start_addr + length;
  59. uint32_t mva;
  60. /* Clean L1 D-Cache */
  61. for (mva = start_addr & ~(L1_CACHE_BYTES-1); mva < end_addr; mva += L1_CACHE_BYTES) {
  62. L1C_CleanDCacheMVA((uint32_t *)mva);
  63. }
  64. #if ARM_SUPPORTS_L2CC
  65. #if SAMA_L2CC_ASSUME_ENABLED || SAMA_L2CC_ENABLE
  66. /* Invalidate L2 Cache */
  67. for (mva = start_addr & ~(L2_CACHE_BYTES-1); mva < end_addr; mva += L2_CACHE_BYTES) {
  68. L2C_CleanPa((uint32_t *)mva);
  69. }
  70. #endif
  71. #endif
  72. }
  73. /**
  74. * @brief Clean and Invalidate D-Cache Region
  75. *
  76. * @param[in] start Pointer to beginning of memory region.
  77. * @param[in] length Length of the memory location.
  78. */
  79. void cacheCleanInvalidateRegion(void *start, uint32_t length) {
  80. uint32_t start_addr = (uint32_t)start;
  81. uint32_t end_addr = start_addr + length;
  82. uint32_t mva;
  83. /* Clean L1 D-Cache */
  84. for (mva = start_addr & ~(L1_CACHE_BYTES-1); mva < end_addr; mva += L1_CACHE_BYTES) {
  85. L1C_CleanInvalidateDCacheMVA((uint32_t *)mva);
  86. }
  87. #if ARM_SUPPORTS_L2CC
  88. #if SAMA_L2CC_ASSUME_ENABLED || SAMA_L2CC_ENABLE
  89. /* Invalidate L2 Cache */
  90. for (mva = start_addr & ~(L2_CACHE_BYTES-1); mva < end_addr; mva += L2_CACHE_BYTES) {
  91. L2C_CleanInvPa((uint32_t *)mva);
  92. }
  93. #endif
  94. #endif
  95. }
  96. /** @} */