system.c 3.6 KB

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