cpuport.c 4.8 KB

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