cpuport.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * File : cpuport.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-07-09 Bernard first version
  13. * 2010-09-11 Bernard add CPU reset implementation
  14. * 2015-07-06 chinesebear modified for loongson 1c
  15. */
  16. #include <rtthread.h>
  17. #include "ls1c.h"
  18. register rt_uint32_t $GP __asm__ ("$28");
  19. /**
  20. * @addtogroup Loongson LS1B
  21. */
  22. /*@{*/
  23. /**
  24. * this function will reset CPU
  25. *
  26. */
  27. void rt_hw_cpu_reset(void)
  28. {
  29. /* open the watch-dog */
  30. WDT_EN = 0x01; /* watch dog enable */
  31. WDT_TIMER = 0x01; /* watch dog will be timeout after 1 tick */
  32. WDT_SET = 0x01; /* watch dog start */
  33. rt_kprintf("reboot system...\n");
  34. while (1);
  35. }
  36. /**
  37. * this function will shutdown CPU
  38. *
  39. */
  40. void rt_hw_cpu_shutdown(void)
  41. {
  42. rt_kprintf("shutdown...\n");
  43. while (1);
  44. }
  45. extern rt_uint32_t cp0_get_cause(void);
  46. extern rt_uint32_t cp0_get_status(void);
  47. extern rt_uint32_t cp0_get_hi(void);
  48. extern rt_uint32_t cp0_get_lo(void);
  49. /**
  50. * This function will initialize thread stack
  51. *
  52. * @param tentry the entry of thread
  53. * @param parameter the parameter of entry
  54. * @param stack_addr the beginning stack address
  55. * @param texit the function will be called when thread exit
  56. *
  57. * @return stack address
  58. */
  59. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, rt_uint8_t *stack_addr, void *texit)
  60. {
  61. rt_uint32_t *stk;
  62. static rt_uint32_t g_sr = 0;
  63. static rt_uint32_t g_gp = 0;
  64. if (g_sr == 0)
  65. {
  66. g_sr = cp0_get_status();
  67. g_sr &= 0xfffffffe;
  68. g_sr |= 0x8401;
  69. g_gp = $GP;
  70. }
  71. /** Start at stack top */
  72. stk = (rt_uint32_t *)stack_addr;
  73. *(stk) = (rt_uint32_t) tentry; /* pc: Entry Point */
  74. *(--stk) = (rt_uint32_t) 0xeeee; /* c0_cause */
  75. *(--stk) = (rt_uint32_t) 0xffff; /* c0_badvaddr */
  76. *(--stk) = (rt_uint32_t) cp0_get_lo(); /* lo */
  77. *(--stk) = (rt_uint32_t) cp0_get_hi(); /* hi */
  78. *(--stk) = (rt_uint32_t) g_sr; /* C0_SR: HW2 = En, IE = En */
  79. *(--stk) = (rt_uint32_t) texit; /* ra */
  80. *(--stk) = (rt_uint32_t) 0x0000001e; /* s8 */
  81. *(--stk) = (rt_uint32_t) stack_addr; /* sp */
  82. *(--stk) = (rt_uint32_t) g_gp; /* gp */
  83. *(--stk) = (rt_uint32_t) 0x0000001b; /* k1 */
  84. *(--stk) = (rt_uint32_t) 0x0000001a; /* k0 */
  85. *(--stk) = (rt_uint32_t) 0x00000019; /* t9 */
  86. *(--stk) = (rt_uint32_t) 0x00000018; /* t8 */
  87. *(--stk) = (rt_uint32_t) 0x00000017; /* s7 */
  88. *(--stk) = (rt_uint32_t) 0x00000016; /* s6 */
  89. *(--stk) = (rt_uint32_t) 0x00000015; /* s5 */
  90. *(--stk) = (rt_uint32_t) 0x00000014; /* s4 */
  91. *(--stk) = (rt_uint32_t) 0x00000013; /* s3 */
  92. *(--stk) = (rt_uint32_t) 0x00000012; /* s2 */
  93. *(--stk) = (rt_uint32_t) 0x00000011; /* s1 */
  94. *(--stk) = (rt_uint32_t) 0x00000010; /* s0 */
  95. *(--stk) = (rt_uint32_t) 0x0000000f; /* t7 */
  96. *(--stk) = (rt_uint32_t) 0x0000000e; /* t6 */
  97. *(--stk) = (rt_uint32_t) 0x0000000d; /* t5 */
  98. *(--stk) = (rt_uint32_t) 0x0000000c; /* t4 */
  99. *(--stk) = (rt_uint32_t) 0x0000000b; /* t3 */
  100. *(--stk) = (rt_uint32_t) 0x0000000a; /* t2 */
  101. *(--stk) = (rt_uint32_t) 0x00000009; /* t1 */
  102. *(--stk) = (rt_uint32_t) 0x00000008; /* t0 */
  103. *(--stk) = (rt_uint32_t) 0x00000007; /* a3 */
  104. *(--stk) = (rt_uint32_t) 0x00000006; /* a2 */
  105. *(--stk) = (rt_uint32_t) 0x00000005; /* a1 */
  106. *(--stk) = (rt_uint32_t) parameter; /* a0 */
  107. *(--stk) = (rt_uint32_t) 0x00000003; /* v1 */
  108. *(--stk) = (rt_uint32_t) 0x00000002; /* v0 */
  109. *(--stk) = (rt_uint32_t) 0x00000001; /* at */
  110. *(--stk) = (rt_uint32_t) 0x00000000; /* zero */
  111. /* return task's current stack address */
  112. return (rt_uint8_t *)stk;
  113. }
  114. #define cache_op(op,addr) \
  115. __asm__ __volatile__( \
  116. " .set push \n" \
  117. " .set noreorder \n" \
  118. " .set mips3\n\t \n" \
  119. " cache %0, %1 \n" \
  120. " .set pop \n" \
  121. : \
  122. : "i" (op), "R" (*(unsigned char *)(addr)))
  123. #if defined(CONFIG_CPU_LOONGSON2)
  124. #define Hit_Invalidate_I 0x00
  125. #else
  126. #define Hit_Invalidate_I 0x10
  127. #endif
  128. #define Hit_Invalidate_D 0x11
  129. #define CONFIG_SYS_CACHELINE_SIZE 32
  130. #define Hit_Writeback_Inv_D 0x15
  131. void flush_cache(unsigned long start_addr, unsigned long size)
  132. {
  133. unsigned long lsize = CONFIG_SYS_CACHELINE_SIZE;
  134. unsigned long addr = start_addr & ~(lsize - 1);
  135. unsigned long aend = (start_addr + size - 1) & ~(lsize - 1);
  136. while (1) {
  137. cache_op(Hit_Writeback_Inv_D, addr);
  138. cache_op(Hit_Invalidate_I, addr);
  139. if (addr == aend)
  140. break;
  141. addr += lsize;
  142. }
  143. }
  144. /*@}*/