cpuport.c 4.1 KB

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