cpuport.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #ifdef __GNUC__
  32. rt_inline rt_uint32_t cp15_rd(void)
  33. {
  34. rt_uint32_t i;
  35. asm ("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. #ifdef __CC_ARM
  60. rt_inline rt_uint32_t cp15_rd(void)
  61. {
  62. rt_uint32_t i;
  63. __asm
  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
  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
  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. #ifdef __ICCARM__
  91. rt_inline rt_uint32_t cp15_rd(void)
  92. {
  93. rt_uint32_t i;
  94. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  95. return i;
  96. }
  97. rt_inline void cache_enable(rt_uint32_t bit)
  98. {
  99. asm volatile(\
  100. "mrc p15,0,r0,c1,c0,0\n\t" \
  101. "orr r0,r0,%0\n\t" \
  102. "mcr p15,0,r0,c1,c0,0" \
  103. : \
  104. :"r" (bit) \
  105. :"memory");
  106. }
  107. rt_inline void cache_disable(rt_uint32_t bit)
  108. {
  109. asm volatile(\
  110. "mrc p15,0,r0,c1,c0,0\n\t" \
  111. "bic r0,r0,%0\n\t" \
  112. "mcr p15,0,r0,c1,c0,0" \
  113. : \
  114. :"r" (bit) \
  115. :"memory");
  116. }
  117. #endif
  118. /**
  119. * enable I-Cache
  120. *
  121. */
  122. void rt_hw_cpu_icache_enable()
  123. {
  124. cache_enable(ICACHE_MASK);
  125. }
  126. /**
  127. * disable I-Cache
  128. *
  129. */
  130. void rt_hw_cpu_icache_disable()
  131. {
  132. cache_disable(ICACHE_MASK);
  133. }
  134. /**
  135. * return the status of I-Cache
  136. *
  137. */
  138. rt_base_t rt_hw_cpu_icache_status()
  139. {
  140. return (cp15_rd() & ICACHE_MASK);
  141. }
  142. /**
  143. * enable D-Cache
  144. *
  145. */
  146. void rt_hw_cpu_dcache_enable()
  147. {
  148. cache_enable(DCACHE_MASK);
  149. }
  150. /**
  151. * disable D-Cache
  152. *
  153. */
  154. void rt_hw_cpu_dcache_disable()
  155. {
  156. cache_disable(DCACHE_MASK);
  157. }
  158. /**
  159. * return the status of D-Cache
  160. *
  161. */
  162. rt_base_t rt_hw_cpu_dcache_status()
  163. {
  164. return (cp15_rd() & DCACHE_MASK);
  165. }
  166. /**
  167. * reset cpu by dog's time-out
  168. *
  169. */
  170. void rt_hw_cpu_reset()
  171. {
  172. rt_kprintf("Restarting system...\n");
  173. machine_reset();
  174. while(1); /* loop forever and wait for reset to happen */
  175. /* NEVER REACHED */
  176. }
  177. /**
  178. * shutdown CPU
  179. *
  180. */
  181. void rt_hw_cpu_shutdown()
  182. {
  183. rt_uint32_t level;
  184. rt_kprintf("shutdown...\n");
  185. level = rt_hw_interrupt_disable();
  186. machine_shutdown();
  187. while (level)
  188. {
  189. RT_ASSERT(0);
  190. }
  191. }
  192. #ifdef RT_USING_CPU_FFS
  193. /**
  194. * This function finds the first bit set (beginning with the least significant bit)
  195. * in value and return the index of that bit.
  196. *
  197. * Bits are numbered starting at 1 (the least significant bit). A return value of
  198. * zero from any of these functions means that the argument was zero.
  199. *
  200. * @return return the index of the first bit set. If value is 0, then this function
  201. * shall return 0.
  202. */
  203. #if defined(__CC_ARM)
  204. int __rt_ffs(int value)
  205. {
  206. register rt_uint32_t x;
  207. if (value == 0)
  208. return value;
  209. __asm
  210. {
  211. rsb x, value, #0
  212. and x, x, value
  213. clz x, x
  214. rsb x, x, #32
  215. }
  216. return x;
  217. }
  218. #elif defined(__ICCARM__)
  219. int __rt_ffs(int value)
  220. {
  221. if (value == 0)
  222. return value;
  223. __ASM("RSB r4, r0, #0");
  224. __ASM("AND r4, r4, r0");
  225. __ASM("CLZ r4, r4");
  226. __ASM("RSB r0, r4, #32");
  227. }
  228. #elif defined(__GNUC__)
  229. int __rt_ffs(int value)
  230. {
  231. if (value == 0)
  232. return value;
  233. value &= (-value);
  234. asm ("clz %0, %1": "=r"(value) :"r"(value));
  235. return (32 - value);
  236. }
  237. #endif
  238. #endif
  239. /*@}*/