1
0

cpu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-09-15 Bernard first version
  9. * 2022-09-20 YangZhongQing
  10. * add IAR assembler
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include "am33xx.h"
  15. /**
  16. * @addtogroup AM33xx
  17. */
  18. /*@{*/
  19. #define ICACHE_MASK (rt_uint32_t)(1 << 12)
  20. #define DCACHE_MASK (rt_uint32_t)(1 << 2)
  21. #if defined(__CC_ARM)
  22. rt_inline rt_uint32_t cp15_rd(void)
  23. {
  24. rt_uint32_t i;
  25. __asm
  26. {
  27. mrc p15, 0, i, c1, c0, 0
  28. }
  29. return i;
  30. }
  31. rt_inline void cache_enable(rt_uint32_t bit)
  32. {
  33. rt_uint32_t value;
  34. __asm
  35. {
  36. mrc p15, 0, value, c1, c0, 0
  37. orr value, value, bit
  38. mcr p15, 0, value, c1, c0, 0
  39. }
  40. }
  41. rt_inline void cache_disable(rt_uint32_t bit)
  42. {
  43. rt_uint32_t value;
  44. __asm
  45. {
  46. mrc p15, 0, value, c1, c0, 0
  47. bic value, value, bit
  48. mcr p15, 0, value, c1, c0, 0
  49. }
  50. }
  51. #elif defined(__GNUC__)
  52. rt_inline rt_uint32_t cp15_rd(void)
  53. {
  54. rt_uint32_t i;
  55. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  56. return i;
  57. }
  58. rt_inline void cache_enable(rt_uint32_t bit)
  59. {
  60. __asm__ __volatile__( \
  61. "mrc p15,0,r0,c1,c0,0\n\t" \
  62. "orr r0,r0,%0\n\t" \
  63. "mcr p15,0,r0,c1,c0,0" \
  64. : \
  65. :"r" (bit) \
  66. :"memory");
  67. }
  68. rt_inline void cache_disable(rt_uint32_t bit)
  69. {
  70. __asm__ __volatile__( \
  71. "mrc p15,0,r0,c1,c0,0\n\t" \
  72. "bic r0,r0,%0\n\t" \
  73. "mcr p15,0,r0,c1,c0,0" \
  74. : \
  75. :"r" (bit) \
  76. :"memory");
  77. }
  78. #elif defined(__ICCARM__)
  79. rt_inline rt_uint32_t cp15_rd(void)
  80. {
  81. rt_uint32_t i;
  82. __asm volatile("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  83. return i;
  84. }
  85. rt_inline void cache_enable(rt_uint32_t bit)
  86. {
  87. rt_uint32_t tmp;
  88. __asm volatile( \
  89. "mrc p15,0,%0,c1,c0,0\n\t" \
  90. "orr %0,%0,%1\n\t" \
  91. "mcr p15,0,%0,c1,c0,0" \
  92. :"+r"(tmp) \
  93. :"r"(bit) \
  94. :"memory");
  95. }
  96. rt_inline void cache_disable(rt_uint32_t bit)
  97. {
  98. rt_uint32_t tmp;
  99. __asm volatile( \
  100. "mrc p15,0,%0,c1,c0,0\n\t" \
  101. "bic %0,%0,%1\n\t" \
  102. "mcr p15,0,%0,c1,c0,0" \
  103. :"+r"(tmp) \
  104. :"r"(bit) \
  105. :"memory");
  106. }
  107. #endif
  108. /**
  109. * enable I-Cache
  110. *
  111. */
  112. void rt_hw_cpu_icache_enable()
  113. {
  114. cache_enable(ICACHE_MASK);
  115. }
  116. /**
  117. * disable I-Cache
  118. *
  119. */
  120. void rt_hw_cpu_icache_disable()
  121. {
  122. cache_disable(ICACHE_MASK);
  123. }
  124. /**
  125. * return the status of I-Cache
  126. *
  127. */
  128. rt_base_t rt_hw_cpu_icache_status()
  129. {
  130. return (cp15_rd() & ICACHE_MASK);
  131. }
  132. /**
  133. * enable D-Cache
  134. *
  135. */
  136. void rt_hw_cpu_dcache_enable()
  137. {
  138. cache_enable(DCACHE_MASK);
  139. }
  140. /**
  141. * disable D-Cache
  142. *
  143. */
  144. void rt_hw_cpu_dcache_disable()
  145. {
  146. cache_disable(DCACHE_MASK);
  147. }
  148. /**
  149. * return the status of D-Cache
  150. *
  151. */
  152. rt_base_t rt_hw_cpu_dcache_status()
  153. {
  154. return (cp15_rd() & DCACHE_MASK);
  155. }
  156. /**
  157. * shutdown CPU
  158. *
  159. */
  160. void rt_hw_cpu_shutdown(void)
  161. {
  162. rt_base_t level;
  163. rt_kprintf("shutdown...\n");
  164. level = rt_hw_interrupt_disable();
  165. while (level)
  166. {
  167. RT_ASSERT(0);
  168. }
  169. }
  170. /*@}*/