system.c 4.1 KB

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