mips_cache.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * File : mips_cache.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016-09-07 Urey the first version
  23. */
  24. #include <rtthread.h>
  25. #include "mips.h"
  26. extern void cache_init(rt_ubase_t cache_size, rt_ubase_t cache_line_size);
  27. void r4k_cache_init(void)
  28. {
  29. // cache_init(dcache_size, cpu_dcache_line_size);
  30. }
  31. void r4k_cache_flush_all(void)
  32. {
  33. blast_dcache16();
  34. blast_icache16();
  35. }
  36. void r4k_icache_flush_all(void)
  37. {
  38. blast_icache16();
  39. }
  40. void r4k_icache_flush_range(rt_ubase_t addr, rt_ubase_t size)
  41. {
  42. rt_ubase_t end, a;
  43. if (size > g_mips_core.icache_size)
  44. {
  45. blast_icache16();
  46. }
  47. else
  48. {
  49. rt_ubase_t ic_lsize = g_mips_core.icache_line_size;
  50. a = addr & ~(ic_lsize - 1);
  51. end = ((addr + size) - 1) & ~(ic_lsize - 1);
  52. while (1)
  53. {
  54. flush_icache_line(a);
  55. if (a == end)
  56. break;
  57. a += ic_lsize;
  58. }
  59. }
  60. }
  61. void r4k_icache_lock_range(rt_ubase_t addr, rt_ubase_t size)
  62. {
  63. rt_ubase_t end, a;
  64. rt_ubase_t ic_lsize = g_mips_core.icache_line_size;
  65. a = addr & ~(ic_lsize - 1);
  66. end = ((addr + size) - 1) & ~(ic_lsize - 1);
  67. while (1)
  68. {
  69. lock_icache_line(a);
  70. if (a == end)
  71. break;
  72. a += ic_lsize;
  73. }
  74. }
  75. void r4k_dcache_inv(rt_ubase_t addr, rt_ubase_t size)
  76. {
  77. rt_ubase_t end, a;
  78. rt_ubase_t dc_lsize = g_mips_core.dcache_line_size;
  79. a = addr & ~(dc_lsize - 1);
  80. end = ((addr + size) - 1) & ~(dc_lsize - 1);
  81. while (1)
  82. {
  83. invalidate_dcache_line(a);
  84. if (a == end)
  85. break;
  86. a += dc_lsize;
  87. }
  88. }
  89. void r4k_dcache_wback_inv(rt_ubase_t addr, rt_ubase_t size)
  90. {
  91. rt_ubase_t end, a;
  92. if (size >= g_mips_core.dcache_size)
  93. {
  94. blast_dcache16();
  95. }
  96. else
  97. {
  98. rt_ubase_t dc_lsize = g_mips_core.dcache_line_size;
  99. a = addr & ~(dc_lsize - 1);
  100. end = ((addr + size) - 1) & ~(dc_lsize - 1);
  101. while (1)
  102. {
  103. flush_dcache_line(a);
  104. if (a == end)
  105. break;
  106. a += dc_lsize;
  107. }
  108. }
  109. }
  110. #define dma_cache_wback_inv(start,size) \
  111. do { (void) (start); (void) (size); } while (0)
  112. #define dma_cache_wback(start,size) \
  113. do { (void) (start); (void) (size); } while (0)
  114. #define dma_cache_inv(start,size) \
  115. do { (void) (start); (void) (size); } while (0)
  116. void r4k_dma_cache_sync(rt_ubase_t addr, rt_size_t size, enum dma_data_direction direction)
  117. {
  118. switch (direction)
  119. {
  120. case DMA_TO_DEVICE:
  121. r4k_dcache_wback_inv(addr, size);
  122. break;
  123. case DMA_FROM_DEVICE:
  124. r4k_dcache_wback_inv(addr, size);
  125. break;
  126. case DMA_BIDIRECTIONAL:
  127. dma_cache_wback_inv(addr, size);
  128. break;
  129. default:
  130. RT_ASSERT(0) ;
  131. }
  132. }