system.c 3.6 KB

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