system.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 1
  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 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. con_puts(hello);
  87. set_leds(7);
  88. wait_romcopy_done();
  89. if ( MINITESTS ) {
  90. extern uint32_t __dram_init_start[], __dram_init_end[];
  91. extern uint32_t __dram_bss_start[], __dram_bss_end[];
  92. volatile uint32_t *dp;
  93. uint32_t v;
  94. uint32_t wrerr;
  95. uint32_t not_zero, all_ones;
  96. uint32_t rx = 0x193ac604;
  97. v = wrerr = 0;
  98. all_ones = -1;
  99. not_zero = 0;
  100. for (dp = __dram_init_start; dp < __dram_init_end; dp++) {
  101. uint32_t v1, v2;
  102. v1 = *dp;
  103. v += v1;
  104. v2 = v1 ^ rx;
  105. *dp = v2;
  106. wrerr |= *dp ^ v2;
  107. *dp++ = v1;
  108. not_zero |= v1;
  109. all_ones &= v1;
  110. rx *= 0xf4d5725f;
  111. }
  112. con_puts(hotstr("SDRAM data checksum: "));
  113. con_print_hex(v);
  114. con_putc('\n');
  115. con_puts(hotstr("Bits always set, clear: "));
  116. con_print_hex(all_ones);
  117. con_putc(' ');
  118. con_print_hex(~not_zero);
  119. con_putc('\n');
  120. if (wrerr)
  121. con_puts(hotstr("SDRAM read/write error\n"));
  122. v = 0;
  123. for (dp = __dram_bss_start; dp < __dram_bss_end; dp++) {
  124. uint32_t x = *dp;
  125. v |= x;
  126. }
  127. if (v)
  128. con_puts(hotstr("SDRAM .bss is not zero!\n"));
  129. }
  130. set_leds(6);
  131. con_flush();
  132. set_leds(5);
  133. #if DELAY
  134. con_puts(hotstr("Waiting 5 s for testing..."));
  135. udelay(5000000);
  136. con_putc('\n');
  137. con_flush();
  138. #endif
  139. late_init();
  140. }
  141. volatile uint32_t __dram_bss test_dram[8];
  142. static void __hot late_init(void)
  143. {
  144. /* This needs to be done as early as possible!!! */
  145. con_puts("Running abc_init_memmap: ");
  146. con_flush();
  147. abc_init_memmap();
  148. con_puts("ok\n");
  149. if (SYS_MAGIC != SYS_MAGIC_MAX80) {
  150. con_puts("Not a MAX80 board?!?!\n\n");
  151. _die();
  152. } else {
  153. con_printf("MAX80 ver %u.%u rework flags %02x fpga %u\n",
  154. SYS_BOARDMAJOR, SYS_BOARDMINOR,
  155. SYS_BOARDFIX, SYS_BOARDFPGA);
  156. if (SYS_BOARDMAJOR != SYS_BOARDFPGA) {
  157. con_puts("Invalid FPGA firmware for this board revision\n");
  158. _die();
  159. }
  160. }
  161. con_putc('\n');
  162. if ( MINITESTS ) {
  163. con_puts("Quick DRAM test:\n");
  164. for (int i = 0; i < 8; i++) {
  165. uint32_t v = (i*0x11111111) + 0x44332211;
  166. test_dram[i] = v;
  167. (void)test_dram[i]; /* Force immediate readback */
  168. con_printf("%08x ", v);
  169. }
  170. con_putc('\n');
  171. for (int i = 0; i < 8; i++) {
  172. con_printf("%08x ", test_dram[i]);
  173. }
  174. con_puts("\n\nRandom number generator test:\n");
  175. for (int i = 0; i < 8; i++)
  176. con_printf("%08x ", rdrand());
  177. con_puts("\n\n");
  178. }
  179. rom_get_serial();
  180. set_leds(4);
  181. read_rtc();
  182. rtc_abc_init();
  183. set_leds(3);
  184. sdcard_reset();
  185. disk_cache_init();
  186. abcdisk_init();
  187. set_leds(2);
  188. pun80_init();
  189. set_leds(1);
  190. abc_init();
  191. /* Release WAIT# if asserted */
  192. ABC_BUSCTL = 0;
  193. set_leds(0);
  194. }