cpuport.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. */
  23. #include <rthw.h>
  24. #include <rtthread.h>
  25. #define ICACHE_MASK (rt_uint32_t)(1 << 12)
  26. #define DCACHE_MASK (rt_uint32_t)(1 << 2)
  27. extern void machine_reset(void);
  28. extern void machine_shutdown(void);
  29. #ifdef __GNUC__
  30. rt_inline rt_uint32_t cp15_rd(void)
  31. {
  32. rt_uint32_t i;
  33. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  34. return i;
  35. }
  36. rt_inline void cache_enable(rt_uint32_t bit)
  37. {
  38. __asm__ __volatile__( \
  39. "mrc p15,0,r0,c1,c0,0\n\t" \
  40. "orr r0,r0,%0\n\t" \
  41. "mcr p15,0,r0,c1,c0,0" \
  42. : \
  43. :"r" (bit) \
  44. :"memory");
  45. }
  46. rt_inline void cache_disable(rt_uint32_t bit)
  47. {
  48. __asm__ __volatile__( \
  49. "mrc p15,0,r0,c1,c0,0\n\t" \
  50. "bic r0,r0,%0\n\t" \
  51. "mcr p15,0,r0,c1,c0,0" \
  52. : \
  53. :"r" (bit) \
  54. :"memory");
  55. }
  56. #endif
  57. #ifdef __CC_ARM
  58. rt_inline rt_uint32_t cp15_rd(void)
  59. {
  60. rt_uint32_t i;
  61. __asm
  62. {
  63. mrc p15, 0, i, c1, c0, 0
  64. }
  65. return i;
  66. }
  67. rt_inline void cache_enable(rt_uint32_t bit)
  68. {
  69. rt_uint32_t value;
  70. __asm
  71. {
  72. mrc p15, 0, value, c1, c0, 0
  73. orr value, value, bit
  74. mcr p15, 0, value, c1, c0, 0
  75. }
  76. }
  77. rt_inline void cache_disable(rt_uint32_t bit)
  78. {
  79. rt_uint32_t value;
  80. __asm
  81. {
  82. mrc p15, 0, value, c1, c0, 0
  83. bic value, value, bit
  84. mcr p15, 0, value, c1, c0, 0
  85. }
  86. }
  87. #endif
  88. /**
  89. * enable I-Cache
  90. *
  91. */
  92. void rt_hw_cpu_icache_enable()
  93. {
  94. cache_enable(ICACHE_MASK);
  95. }
  96. /**
  97. * disable I-Cache
  98. *
  99. */
  100. void rt_hw_cpu_icache_disable()
  101. {
  102. cache_disable(ICACHE_MASK);
  103. }
  104. /**
  105. * return the status of I-Cache
  106. *
  107. */
  108. rt_base_t rt_hw_cpu_icache_status()
  109. {
  110. return (cp15_rd() & ICACHE_MASK);
  111. }
  112. /**
  113. * enable D-Cache
  114. *
  115. */
  116. void rt_hw_cpu_dcache_enable()
  117. {
  118. cache_enable(DCACHE_MASK);
  119. }
  120. /**
  121. * disable D-Cache
  122. *
  123. */
  124. void rt_hw_cpu_dcache_disable()
  125. {
  126. cache_disable(DCACHE_MASK);
  127. }
  128. /**
  129. * return the status of D-Cache
  130. *
  131. */
  132. rt_base_t rt_hw_cpu_dcache_status()
  133. {
  134. return (cp15_rd() & DCACHE_MASK);
  135. }
  136. /**
  137. * reset cpu by dog's time-out
  138. *
  139. */
  140. void rt_hw_cpu_reset()
  141. {
  142. rt_kprintf("Restarting system...\n");
  143. machine_reset();
  144. while(1); /* loop forever and wait for reset to happen */
  145. /* NEVER REACHED */
  146. }
  147. /**
  148. * shutdown CPU
  149. *
  150. */
  151. void rt_hw_cpu_shutdown()
  152. {
  153. rt_uint32_t level;
  154. rt_kprintf("shutdown...\n");
  155. level = rt_hw_interrupt_disable();
  156. machine_shutdown();
  157. while (level)
  158. {
  159. RT_ASSERT(0);
  160. }
  161. }
  162. #ifdef RT_USING_CPU_FFS
  163. /**
  164. * This function finds the first bit set (beginning with the least significant bit)
  165. * in value and return the index of that bit.
  166. *
  167. * Bits are numbered starting at 1 (the least significant bit). A return value of
  168. * zero from any of these functions means that the argument was zero.
  169. *
  170. * @return return the index of the first bit set. If value is 0, then this function
  171. * shall return 0.
  172. */
  173. #if defined(__CC_ARM)
  174. int __rt_ffs(int value)
  175. {
  176. register rt_uint32_t x;
  177. if (value == 0)
  178. return value;
  179. __asm
  180. {
  181. rsb x, value, #0
  182. and x, x, value
  183. clz x, x
  184. rsb x, x, #32
  185. }
  186. return x;
  187. }
  188. #elif defined(__IAR_SYSTEMS_ICC__)
  189. int __rt_ffs(int value)
  190. {
  191. if (value == 0)
  192. return value;
  193. __ASM("RSB r4, r0, #0");
  194. __ASM("AND r4, r4, r0");
  195. __ASM("CLZ r4, r4");
  196. __ASM("RSB r0, r4, #32");
  197. }
  198. #elif defined(__GNUC__)
  199. int __rt_ffs(int value)
  200. {
  201. if (value == 0)
  202. return value;
  203. value &= (-value);
  204. asm ("clz %0, %1": "=r"(value) :"r"(value));
  205. return (32 - value);
  206. }
  207. #endif
  208. #endif
  209. /*
  210. rt_base_t rt_hw_interrupt_disable()
  211. {
  212. unsigned long old;
  213. unsigned long tmp;
  214. __asm__ __volatile__ (
  215. "mrs %0, cpsr\n"
  216. "orr %1, %0, #0xc0\n"
  217. "msr cpsr_c, %1"
  218. : "=r" (old), "=r" (tmp)
  219. :
  220. : "memory");
  221. return old;
  222. }
  223. void rt_hw_interrupt_enable(rt_base_t level)
  224. {
  225. unsigned long tmp;
  226. __asm__ __volatile__ (
  227. "mrs %0, cpsr\n"
  228. "bic %0, %0, #0xc0\n"
  229. "msr cpsr_c, %0"
  230. : "=r" (tmp)
  231. :
  232. : "memory");
  233. }
  234. */
  235. /*
  236. rt_base_t rt_hw_interrupt_disable()
  237. {
  238. unsigned long old;
  239. unsigned long tmp;
  240. __asm__ __volatile__( "mrs %0,cpsr" : "=r"(old) : "r"(tmp) );
  241. __asm__ __volatile__( "orr %0,%1,#0xC0" : "=r"(tmp) : "r"(old) );
  242. __asm__ __volatile__( "msr cpsr_c,%0" : : "r"(tmp) );
  243. return old;
  244. }
  245. void rt_hw_interrupt_enable(rt_base_t level)
  246. {
  247. asm volatile ( "msr cpsr_c,%0" : : "r"(level) );
  248. }
  249. */
  250. /*@}*/