system.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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,0)
  49. {
  50. killed(hotstr("misaligned"), pc);
  51. }
  52. IRQHANDLER(ebreak,0)
  53. {
  54. killed(hotstr("invalid instruction"), pc);
  55. }
  56. volatile __sbss uint32_t timer_irq_count;
  57. IRQHANDLER(sysclock,0)
  58. {
  59. uint32_t count = timer_irq_count;
  60. count++;
  61. timer_irq_count = count;
  62. if ( MINITESTS ) {
  63. static const char spinner[4] = "/-\\|";
  64. if (!(count & (TIMER_HZ-1))) {
  65. uint32_t seconds = count >> TIMER_SHIFT;
  66. CON_DATA = spinner[seconds & 3];
  67. CON_DATA = '\b';
  68. }
  69. }
  70. }
  71. static void __cold __noinline late_init(void);
  72. uint32_t __sbss timer_irq_start;
  73. void __hot init(void)
  74. {
  75. static __string_hot const char hello[] =
  76. "\n\n*** Hello, World! ***\n"
  77. "MAX80 "
  78. #ifdef TEST
  79. "testing"
  80. #endif
  81. "firmware compiled on: " __DATE__ " " __TIME__ "\n";
  82. timer_irq_start = rdtime();
  83. /* Start ROM copy engine, unmask timer and fatal exceptions */
  84. unmask_irqs((1U << ROMCOPY_IRQ)|(1U << EBREAK_IRQ)|
  85. (1U << BUSERR_IRQ)|(1U << SYSCLOCK_IRQ));
  86. set_leds(7);
  87. wait_romcopy_done();
  88. if ( MINITESTS ) {
  89. extern uint32_t __dram_init_start[], __dram_init_end[];
  90. extern uint32_t __dram_bss_start[], __dram_bss_end[];
  91. const uint32_t *dp;
  92. uint32_t v;
  93. v = 0;
  94. for (dp = __dram_init_start; dp < __dram_init_end; dp++)
  95. v += *dp;
  96. con_puts("SDRAM data checksum: ");
  97. con_print_hex(v);
  98. con_putc('\n');
  99. v = 0;
  100. for (dp = __dram_bss_start; dp < __dram_bss_end; dp++) {
  101. uint32_t x = *dp;
  102. v |= x;
  103. }
  104. if (v)
  105. con_puts("SDRAM .bss is not zero!\n");
  106. }
  107. set_leds(6);
  108. con_puts(hello);
  109. con_flush();
  110. set_leds(5);
  111. #if DELAY
  112. con_puts("Waiting 5 s for testing...");
  113. udelay(5000000);
  114. con_putc('\n');
  115. con_flush();
  116. #endif
  117. late_init();
  118. }
  119. volatile uint32_t __dram_bss test_dram[8];
  120. static void __cold __noinline late_init(void)
  121. {
  122. /* This needs to be done as early as possible!!! */
  123. con_puts("Running abc_init_memmap: ");
  124. con_flush();
  125. abc_init_memmap();
  126. con_puts("ok\n");
  127. if (SYS_MAGIC != SYS_MAGIC_MAX80) {
  128. con_puts("Not a MAX80 board?!?!\n\n");
  129. _die();
  130. } else {
  131. con_printf("MAX80 ver %u.%u rework flags %02x fpga %u\n",
  132. SYS_BOARDMAJOR, SYS_BOARDMINOR,
  133. SYS_BOARDFIX, SYS_BOARDFPGA);
  134. if (SYS_BOARDMAJOR != SYS_BOARDFPGA) {
  135. con_puts("Invalid FPGA firmware for this board revision\n");
  136. _die();
  137. }
  138. }
  139. con_putc('\n');
  140. if ( MINITESTS ) {
  141. con_puts("Quick DRAM test:\n");
  142. for (int i = 0; i < 8; i++) {
  143. uint32_t v = (i*0x11111111) + 0x44332211;
  144. test_dram[i] = v;
  145. (void)test_dram[i]; /* Force immediate readback */
  146. con_printf("%08x ", v);
  147. }
  148. con_putc('\n');
  149. for (int i = 0; i < 8; i++) {
  150. con_printf("%08x ", test_dram[i]);
  151. }
  152. con_puts("\n\nRandom number generator test:\n");
  153. for (int i = 0; i < 8; i++)
  154. con_printf("%08x ", rdrand());
  155. con_puts("\n\n");
  156. }
  157. rom_get_serial();
  158. set_leds(4);
  159. read_rtc();
  160. rtc_abc_init();
  161. set_leds(3);
  162. sdcard_reset();
  163. disk_cache_init();
  164. abcdisk_init();
  165. set_leds(2);
  166. pun80_init();
  167. set_leds(1);
  168. abc_init();
  169. /* Release WAIT# if asserted */
  170. ABC_BUSCTL = 0;
  171. set_leds(0);
  172. }