cache.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2012 Andes Technology Corporation
  3. * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
  4. * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
  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
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <common.h>
  22. static inline unsigned long CACHE_LINE_SIZE(enum cache_t cache)
  23. {
  24. if (cache == ICACHE)
  25. return 8 << (((GET_ICM_CFG() & ICM_CFG_MSK_ISZ) \
  26. >> ICM_CFG_OFF_ISZ) - 1);
  27. else
  28. return 8 << (((GET_DCM_CFG() & DCM_CFG_MSK_DSZ) \
  29. >> DCM_CFG_OFF_DSZ) - 1);
  30. }
  31. void flush_dcache_range(unsigned long start, unsigned long end)
  32. {
  33. unsigned long line_size;
  34. line_size = CACHE_LINE_SIZE(DCACHE);
  35. while (end > start) {
  36. asm volatile (
  37. "\n\tcctl %0, L1D_VA_WB"
  38. "\n\tcctl %0, L1D_VA_INVAL"
  39. :
  40. : "r" (start)
  41. );
  42. start += line_size;
  43. }
  44. }
  45. void invalidate_icache_range(unsigned long start, unsigned long end)
  46. {
  47. unsigned long line_size;
  48. line_size = CACHE_LINE_SIZE(ICACHE);
  49. while (end > start) {
  50. asm volatile (
  51. "\n\tcctl %0, L1I_VA_INVAL"
  52. :
  53. : "r"(start)
  54. );
  55. start += line_size;
  56. }
  57. }
  58. void invalidate_dcache_range(unsigned long start, unsigned long end)
  59. {
  60. unsigned long line_size;
  61. line_size = CACHE_LINE_SIZE(DCACHE);
  62. while (end > start) {
  63. asm volatile (
  64. "\n\tcctl %0, L1D_VA_INVAL"
  65. :
  66. : "r"(start)
  67. );
  68. start += line_size;
  69. }
  70. }
  71. void flush_cache(unsigned long addr, unsigned long size)
  72. {
  73. flush_dcache_range(addr, addr + size);
  74. invalidate_icache_range(addr, addr + size);
  75. }
  76. void icache_enable(void)
  77. {
  78. asm volatile (
  79. "mfsr $p0, $mr8\n\t"
  80. "ori $p0, $p0, 0x01\n\t"
  81. "mtsr $p0, $mr8\n\t"
  82. "isb\n\t"
  83. );
  84. }
  85. void icache_disable(void)
  86. {
  87. asm volatile (
  88. "mfsr $p0, $mr8\n\t"
  89. "li $p1, ~0x01\n\t"
  90. "and $p0, $p0, $p1\n\t"
  91. "mtsr $p0, $mr8\n\t"
  92. "isb\n\t"
  93. );
  94. }
  95. int icache_status(void)
  96. {
  97. int ret;
  98. asm volatile (
  99. "mfsr $p0, $mr8\n\t"
  100. "andi %0, $p0, 0x01\n\t"
  101. : "=r" (ret)
  102. :
  103. : "memory"
  104. );
  105. return ret;
  106. }
  107. void dcache_enable(void)
  108. {
  109. asm volatile (
  110. "mfsr $p0, $mr8\n\t"
  111. "ori $p0, $p0, 0x02\n\t"
  112. "mtsr $p0, $mr8\n\t"
  113. "isb\n\t"
  114. );
  115. }
  116. void dcache_disable(void)
  117. {
  118. asm volatile (
  119. "mfsr $p0, $mr8\n\t"
  120. "li $p1, ~0x02\n\t"
  121. "and $p0, $p0, $p1\n\t"
  122. "mtsr $p0, $mr8\n\t"
  123. "isb\n\t"
  124. );
  125. }
  126. int dcache_status(void)
  127. {
  128. int ret;
  129. asm volatile (
  130. "mfsr $p0, $mr8\n\t"
  131. "andi %0, $p0, 0x02\n\t"
  132. : "=r" (ret)
  133. :
  134. : "memory"
  135. );
  136. return ret;
  137. }