cpu.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * File : cpu.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2012, RT-Thread Development 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. * 2016Äê9ÔÂ8ÈÕ Urey the first version
  23. */
  24. #include <rtthread.h>
  25. #include <board.h>
  26. #include <rthw.h>
  27. #include "../common/mips.h"
  28. mips32_core_cfg_t g_mips_core =
  29. {
  30. .icache_line_size = 32,
  31. .icache_size = 16384,
  32. .dcache_line_size = 32,
  33. .dcache_size = 16384,
  34. .max_tlb_entries = 16, /* max_tlb_entries */
  35. };
  36. void rt_hw_tlb_init(void)
  37. {
  38. //----------------------------------------------------------------------------------
  39. //cchappy tlb 0x30000000 to 0xC0000000
  40. //----------------------------------------------------------------------------------
  41. unsigned int pagemask = 0x007fe000;//0x01ffe000; /* 4MB */
  42. /* cached D:allow-W V:valid G */
  43. unsigned int entrylo0 = (0x30000000 >> 6) | (3 << 3) + (1 << 2) + (1 << 1) + 1;
  44. unsigned int entrylo1 = (0x30400000 >> 6) | (3 << 3) + (1 << 2) + (1 << 1) + 1;
  45. unsigned int entryhi = 0xc0000000; /* kseg2 base */
  46. int i;
  47. __write_32bit_c0_register($5, 4, 0xa9000000);
  48. write_c0_pagemask(pagemask);
  49. write_c0_wired(0);
  50. /* indexed write 32 tlb entry */
  51. for(i = 0; i < 32; i++)
  52. {
  53. asm (
  54. ".macro _ssnop; sll $0, $0, 1; .endm\n\t"
  55. ".macro _ehb; sll $0, $0, 3; .endm\n\t"
  56. ".macro mtc0_tlbw_hazard; _ssnop; _ssnop; _ehb; .endm\n\t"
  57. ".macro tlbw_use_hazard; _ssnop; _ssnop; _ssnop; _ehb; .endm\n\t"
  58. "\n\t"
  59. "mtc0 %0, $0\n\t" /* write Index */
  60. "tlbw_use_hazard\n\t"
  61. "mtc0 %1, $5\n\t" /* write PageMask */
  62. "mtc0 %2, $10\n\t" /* write EntryHi */
  63. "mtc0 %3, $2\n\t" /* write EntryLo0 */
  64. "mtc0 %4, $3\n\t" /* write EntryLo1 */
  65. "mtc0_tlbw_hazard\n\t"
  66. "tlbwi \n\t" /* TLB indexed write */
  67. "tlbw_use_hazard\n\t"
  68. : : "Jr" (i), "r" (pagemask), "r" (entryhi),
  69. "r" (entrylo0), "r" (entrylo1)
  70. );
  71. entryhi += 0x0800000; /* 32MB */
  72. entrylo0 += (0x0800000 >> 6);
  73. entrylo1 += (0x0800000 >> 6);
  74. }
  75. }
  76. void rt_hw_cache_init(void)
  77. {
  78. r4k_cache_flush_all();
  79. }
  80. /**
  81. * this function will reset CPU
  82. *
  83. */
  84. RT_WEAK void rt_hw_cpu_reset()
  85. {
  86. /* open the watch-dog */
  87. REG_WDT_TCSR = WDT_TCSR_EXT_EN;
  88. REG_WDT_TCSR |= WDT_TCSR_PRESCALE_1024;
  89. REG_WDT_TDR = 0x03;
  90. REG_WDT_TCNT = 0x00;
  91. REG_WDT_TCER |= WDT_TCER_TCEN;
  92. rt_kprintf("reboot system...\n");
  93. rt_hw_interrupt_disable();
  94. while (1);
  95. }
  96. /**
  97. * this function will shutdown CPU
  98. *
  99. */
  100. RT_WEAK void rt_hw_cpu_shutdown()
  101. {
  102. rt_kprintf("shutdown...\n");
  103. rt_hw_interrupt_disable();
  104. while (1);
  105. }
  106. /**
  107. * This function finds the first bit set (beginning with the least significant bit)
  108. * in value and return the index of that bit.
  109. *
  110. * Bits are numbered starting at 1 (the least significant bit). A return value of
  111. * zero from any of these functions means that the argument was zero.
  112. *
  113. * @return return the index of the first bit set. If value is 0, then this function
  114. * shall return 0.
  115. */
  116. RT_WEAK int __rt_ffs(int value)
  117. {
  118. return __builtin_ffs(value);
  119. }