cache.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * File : cache.c
  3. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. */
  22. #include "../xburst/cache.h"
  23. #include <board.h>
  24. #define CACHE_SIZE 16*1024
  25. #define CACHE_LINE_SIZE 32
  26. #define KSEG0 0x80000000
  27. #define K0_TO_K1() \
  28. do { \
  29. unsigned long __k0_addr; \
  30. \
  31. __asm__ __volatile__( \
  32. "la %0, 1f\n\t" \
  33. "or %0, %0, %1\n\t" \
  34. "jr %0\n\t" \
  35. "nop\n\t" \
  36. "1: nop\n" \
  37. : "=&r"(__k0_addr) \
  38. : "r" (0x20000000) ); \
  39. } while(0)
  40. #define K1_TO_K0() \
  41. do { \
  42. unsigned long __k0_addr; \
  43. __asm__ __volatile__( \
  44. "nop;nop;nop;nop;nop;nop;nop\n\t" \
  45. "la %0, 1f\n\t" \
  46. "jr %0\n\t" \
  47. "nop\n\t" \
  48. "1: nop\n" \
  49. : "=&r" (__k0_addr)); \
  50. } while (0)
  51. #define INVALIDATE_BTB() \
  52. do { \
  53. unsigned long tmp; \
  54. __asm__ __volatile__( \
  55. ".set mips32\n\t" \
  56. "mfc0 %0, $16, 7\n\t" \
  57. "nop\n\t" \
  58. "ori %0, 2\n\t" \
  59. "mtc0 %0, $16, 7\n\t" \
  60. "nop\n\t" \
  61. ".set mips2\n\t" \
  62. : "=&r" (tmp)); \
  63. } while (0)
  64. #define SYNC_WB() __asm__ __volatile__ ("sync")
  65. #define cache_op(op,addr) \
  66. __asm__ __volatile__( \
  67. " .set noreorder \n" \
  68. " .set mips32\n\t \n" \
  69. " cache %0, %1 \n" \
  70. " .set mips0 \n" \
  71. " .set reorder" \
  72. : \
  73. : "i" (op), "m" (*(unsigned char *)(addr)))
  74. void __icache_invalidate_all(void)
  75. {
  76. unsigned int i;
  77. K0_TO_K1();
  78. asm volatile (".set noreorder\n"
  79. ".set mips32\n\t"
  80. "mtc0\t$0,$28\n\t"
  81. "mtc0\t$0,$29\n"
  82. ".set mips0\n"
  83. ".set reorder\n");
  84. for (i=KSEG0;i<KSEG0+CACHE_SIZE;i+=CACHE_LINE_SIZE)
  85. cache_op(Index_Store_Tag_I, i);
  86. K1_TO_K0();
  87. INVALIDATE_BTB();
  88. }
  89. void __dcache_writeback_all(void)
  90. {
  91. unsigned int i;
  92. for (i=KSEG0;i<KSEG0+CACHE_SIZE;i+=CACHE_LINE_SIZE)
  93. cache_op(Index_Writeback_Inv_D, i);
  94. SYNC_WB();
  95. }
  96. void rt_hw_cache_init(void)
  97. {
  98. __dcache_writeback_all();
  99. __icache_invalidate_all();
  100. }