cpuport.c 4.6 KB

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