cpuport.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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-01-13 weety first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #define ICACHE_MASK (rt_uint32_t)(1 << 12)
  13. #define DCACHE_MASK (rt_uint32_t)(1 << 2)
  14. extern void machine_reset(void);
  15. extern void machine_shutdown(void);
  16. #ifdef __GNUC__
  17. rt_inline rt_uint32_t cp15_rd(void)
  18. {
  19. rt_uint32_t i;
  20. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  21. return i;
  22. }
  23. rt_inline void cache_enable(rt_uint32_t bit)
  24. {
  25. __asm__ __volatile__( \
  26. "mrc p15,0,r0,c1,c0,0\n\t" \
  27. "orr r0,r0,%0\n\t" \
  28. "mcr p15,0,r0,c1,c0,0" \
  29. : \
  30. :"r" (bit) \
  31. :"memory");
  32. }
  33. rt_inline void cache_disable(rt_uint32_t bit)
  34. {
  35. __asm__ __volatile__( \
  36. "mrc p15,0,r0,c1,c0,0\n\t" \
  37. "bic r0,r0,%0\n\t" \
  38. "mcr p15,0,r0,c1,c0,0" \
  39. : \
  40. :"r" (bit) \
  41. :"memory");
  42. }
  43. #endif
  44. #ifdef __CC_ARM
  45. rt_inline rt_uint32_t cp15_rd(void)
  46. {
  47. rt_uint32_t i;
  48. __asm
  49. {
  50. mrc p15, 0, i, c1, c0, 0
  51. }
  52. return i;
  53. }
  54. rt_inline void cache_enable(rt_uint32_t bit)
  55. {
  56. rt_uint32_t value;
  57. __asm
  58. {
  59. mrc p15, 0, value, c1, c0, 0
  60. orr value, value, bit
  61. mcr p15, 0, value, c1, c0, 0
  62. }
  63. }
  64. rt_inline void cache_disable(rt_uint32_t bit)
  65. {
  66. rt_uint32_t value;
  67. __asm
  68. {
  69. mrc p15, 0, value, c1, c0, 0
  70. bic value, value, bit
  71. mcr p15, 0, value, c1, c0, 0
  72. }
  73. }
  74. #endif
  75. /**
  76. * enable I-Cache
  77. *
  78. */
  79. void rt_hw_cpu_icache_enable()
  80. {
  81. cache_enable(ICACHE_MASK);
  82. }
  83. /**
  84. * disable I-Cache
  85. *
  86. */
  87. void rt_hw_cpu_icache_disable()
  88. {
  89. cache_disable(ICACHE_MASK);
  90. }
  91. /**
  92. * return the status of I-Cache
  93. *
  94. */
  95. rt_base_t rt_hw_cpu_icache_status()
  96. {
  97. return (cp15_rd() & ICACHE_MASK);
  98. }
  99. /**
  100. * enable D-Cache
  101. *
  102. */
  103. void rt_hw_cpu_dcache_enable()
  104. {
  105. cache_enable(DCACHE_MASK);
  106. }
  107. /**
  108. * disable D-Cache
  109. *
  110. */
  111. void rt_hw_cpu_dcache_disable()
  112. {
  113. cache_disable(DCACHE_MASK);
  114. }
  115. /**
  116. * return the status of D-Cache
  117. *
  118. */
  119. rt_base_t rt_hw_cpu_dcache_status()
  120. {
  121. return (cp15_rd() & DCACHE_MASK);
  122. }
  123. /**
  124. * reset cpu by dog's time-out
  125. *
  126. */
  127. void rt_hw_cpu_reset()
  128. {
  129. rt_kprintf("Restarting system...\n");
  130. machine_reset();
  131. while(1); /* loop forever and wait for reset to happen */
  132. /* NEVER REACHED */
  133. }
  134. /**
  135. * shutdown CPU
  136. *
  137. */
  138. void rt_hw_cpu_shutdown(void)
  139. {
  140. rt_base_t level;
  141. rt_kprintf("shutdown...\n");
  142. level = rt_hw_interrupt_disable();
  143. machine_shutdown();
  144. while (level)
  145. {
  146. RT_ASSERT(0);
  147. }
  148. }
  149. #ifdef RT_USING_CPU_FFS
  150. /**
  151. * This function finds the first bit set (beginning with the least significant bit)
  152. * in value and return the index of that bit.
  153. *
  154. * Bits are numbered starting at 1 (the least significant bit). A return value of
  155. * zero from any of these functions means that the argument was zero.
  156. *
  157. * @return return the index of the first bit set. If value is 0, then this function
  158. * shall return 0.
  159. */
  160. #if defined(__CC_ARM)
  161. int __rt_ffs(int value)
  162. {
  163. register rt_uint32_t x;
  164. if (value == 0)
  165. return value;
  166. __asm
  167. {
  168. rsb x, value, #0
  169. and x, x, value
  170. clz x, x
  171. rsb x, x, #32
  172. }
  173. return x;
  174. }
  175. #elif defined(__IAR_SYSTEMS_ICC__)
  176. int __rt_ffs(int value)
  177. {
  178. if (value == 0)
  179. return value;
  180. __ASM("RSB r4, r0, #0");
  181. __ASM("AND r4, r4, r0");
  182. __ASM("CLZ r4, r4");
  183. __ASM("RSB r0, r4, #32");
  184. }
  185. #elif defined(__GNUC__)
  186. int __rt_ffs(int value)
  187. {
  188. if (value == 0)
  189. return value;
  190. value &= (-value);
  191. asm ("clz %0, %1": "=r"(value) :"r"(value));
  192. return (32 - value);
  193. }
  194. #endif
  195. #endif
  196. /*@}*/