2
0

system.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "fw.h"
  2. #include "io.h"
  3. #include "sdcard.h"
  4. #include "abcio.h"
  5. #include "sys.h"
  6. #include "console.h"
  7. #define DEBUG 0
  8. #define MINITESTS 1
  9. volatile __sbss uint32_t timer_irq_count;
  10. IRQHANDLER(sysclock)
  11. {
  12. uint32_t count = timer_irq_count;
  13. count++;
  14. }
  15. static void __hot con_print_hex(unsigned int n)
  16. {
  17. for (int i = 0; i < 8; i++) {
  18. unsigned int c = n >> 28;
  19. n <<= 4;
  20. con_putc(c + ((c >= 10) ? 'a'-10 : '0'));
  21. }
  22. }
  23. /* Don't mark no_return or gcc moves it to SDRAM */
  24. static void __hot __text_hot killed(const char *how, size_t pc)
  25. {
  26. /* Cannot use con_printf() here */
  27. const uint16_t *pcp;
  28. size_t mtval;
  29. asm volatile("csrr %0,mtval" : "=r" (mtval));
  30. /* Try to move back to the previous instruction (if not a jump...) */
  31. pc += -4 + (pc & 1);
  32. pcp = (const uint16_t *)pc;
  33. con_puts(hotstr("ERROR: "));
  34. con_puts(how);
  35. con_puts(hotstr(" at 0x"));
  36. con_print_hex(pc);
  37. con_puts(hotstr(" (0x"));
  38. con_print_hex((pcp[1] << 16) + pcp[0]);
  39. con_puts(hotstr(")\nBad address: 0x"));
  40. con_print_hex(mtval);
  41. con_putc('\n');
  42. for (int i = 0; i < 32; i += 8) {
  43. for (int j = 0; j < 8; j++) {
  44. uint32_t v = rdxreg(i+j);
  45. con_print_hex(v);
  46. con_putc((j == 7) ? '\n' : ' ');
  47. }
  48. }
  49. con_flush();
  50. udelay(5000000);
  51. reset(SYS_RESET_SOFT);
  52. }
  53. IRQHANDLER(buserr)
  54. {
  55. killed(hotstr("misaligned"), pc);
  56. }
  57. IRQHANDLER(ebreak)
  58. {
  59. killed(hotstr("invalid instruction"), pc);
  60. }
  61. static void __cold __noinline late_init(void);
  62. uint32_t __sbss timer_irq_start;
  63. void __hot init(void)
  64. {
  65. static __string_hot const char hello[] =
  66. "\n\n*** Hello, World! ***\n"
  67. "MAX80 "
  68. #ifdef TEST
  69. "testing"
  70. #endif
  71. "firmware compiled on: " __DATE__ " " __TIME__ "\n";
  72. timer_irq_start = rdtime();
  73. /* Start ROM copy engine, unmask timer and fatal exceptions */
  74. unmask_irqs((1U << ROMCOPY_IRQ)|(1U << EBREAK_IRQ)|
  75. (1U << BUSERR_IRQ)|(1U << SYSCLOCK_IRQ));
  76. set_leds(7);
  77. wait_romcopy_done();
  78. set_leds(6);
  79. con_set_baudrate(115200);
  80. con_puts(hello);
  81. con_flush();
  82. set_leds(5);
  83. #if 1 // DEBUG
  84. con_puts("Waiting 5 s for testing...");
  85. udelay(5000000);
  86. con_putc('\n');
  87. con_flush();
  88. #endif
  89. late_init();
  90. }
  91. volatile uint32_t __dram_bss test_dram[8];
  92. static void __cold __noinline late_init(void)
  93. {
  94. /* This needs to be done as early as possible!!! */
  95. con_puts("Running abc_init_memmap: ");
  96. con_flush();
  97. abc_init_memmap();
  98. con_puts("ok\n");
  99. if (SYS_MAGIC != SYS_MAGIC_MAX80) {
  100. con_puts("Not a MAX80 board?!?!\n\n");
  101. _die();
  102. } else {
  103. con_printf("MAX80 ver %u.%u rework flags %02x fpga %u\n",
  104. SYS_BOARDMAJOR, SYS_BOARDMINOR,
  105. SYS_BOARDFIX, SYS_BOARDFPGA);
  106. if (SYS_BOARDMAJOR != SYS_BOARDFPGA) {
  107. con_puts("Invalid FPGA firmware for this board revision\n");
  108. _die();
  109. }
  110. }
  111. con_putc('\n');
  112. if ( MINITESTS ) {
  113. con_puts("Quick DRAM test:\n");
  114. for (int i = 0; i < 8; i++) {
  115. uint32_t v = (i*0x11111111) + 0x44332211;
  116. test_dram[i] = v;
  117. (void)test_dram[i]; /* Force immediate readback */
  118. con_printf("%08x ", v);
  119. }
  120. con_putc('\n');
  121. for (int i = 0; i < 8; i++) {
  122. con_printf("%08x ", test_dram[i]);
  123. }
  124. con_puts("\n\nRandom number generator test:\n");
  125. for (int i = 0; i < 8; i++)
  126. con_printf("%08x ", rdrand());
  127. con_puts("\n\n");
  128. }
  129. rom_get_serial();
  130. set_leds(4);
  131. read_rtc();
  132. rtc_abc_init();
  133. set_leds(3);
  134. sdcard_reset();
  135. disk_cache_init();
  136. abcdisk_init();
  137. set_leds(2);
  138. abc_init();
  139. set_leds(1);
  140. /* Release WAIT# if asserted */
  141. ABC_BUSCTL = 0;
  142. set_leds(0);
  143. }