system.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "common.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. 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. static void hello_sdram(void);
  73. #define TEST_DATA_0 0x01234567
  74. #define TEST_DATA_1 0x98badcfe
  75. static const volatile __dram_data
  76. uint32_t test_data[2] = { TEST_DATA_0, TEST_DATA_1 };
  77. uint32_t __sbss timer_irq_start;
  78. void __hot init(void)
  79. {
  80. static __string_hot const char hello[] =
  81. "\n\n*** Hello, World! ***\n"
  82. "MAX80 "
  83. #ifdef TEST
  84. "testing"
  85. #endif
  86. "firmware compiled on: ";
  87. timer_irq_start = rdtime();
  88. /* Start ROM copy engine, unmask timer and fatal exceptions */
  89. unmask_irqs((1U << ROMCOPY_IRQ)|(1U << EBREAK_IRQ)|
  90. (1U << BUSERR_IRQ)|(1U << SYSCLOCK_IRQ));
  91. con_puts(hello);
  92. con_puts(__datestamp);
  93. con_putc('\n');
  94. set_leds(7);
  95. wait_romcopy_done();
  96. if ( test_data[0] == TEST_DATA_0 && test_data[1] == TEST_DATA_1 ) {
  97. con_puts(hotstr("SDRAM seems ok - skipping test\n"));
  98. } else {
  99. volatile uint32_t *dp;
  100. uint32_t v;
  101. uint32_t wrerr;
  102. uint32_t not_zero, all_ones;
  103. uint32_t rx = 0x193ac604;
  104. v = wrerr = 0;
  105. all_ones = -1;
  106. not_zero = 0;
  107. for (dp = __dram_init_start; dp < __dram_init_end; dp++) {
  108. uint32_t v1, v2;
  109. v1 = *dp;
  110. v += v1;
  111. v2 = v1 ^ rx;
  112. *dp = v2;
  113. wrerr |= *dp ^ v2;
  114. *dp = v1;
  115. not_zero |= v1;
  116. all_ones &= v1;
  117. rx *= 0xf4d5725f;
  118. }
  119. con_puts(hotstr("SDRAM data checksum: "));
  120. con_print_hex(v);
  121. con_puts(hotstr(" expected "));
  122. con_print_hex(__dram_checksum);
  123. con_putc('\n');
  124. con_puts(hotstr("Bits always set, clear: "));
  125. con_print_hex(all_ones);
  126. con_putc(' ');
  127. con_print_hex(~not_zero);
  128. con_putc('\n');
  129. con_puts(hotstr("Test data: "));
  130. con_print_hex(test_data[0]);
  131. con_putc(' ');
  132. con_print_hex(test_data[1]);
  133. con_putc('\n');
  134. if (wrerr)
  135. con_puts(hotstr("SDRAM read/write error\n"));
  136. v = 0;
  137. for (dp = __dram_bss_start; dp < __dram_bss_end; dp++) {
  138. uint32_t x = *dp;
  139. v |= x;
  140. }
  141. if (v)
  142. con_puts(hotstr("SDRAM .bss is not zero!\n"));
  143. }
  144. set_leds(6);
  145. con_flush();
  146. set_leds(5);
  147. #if DELAY
  148. con_puts(hotstr("Waiting 5 s for testing..."));
  149. udelay(5000000);
  150. con_putc('\n');
  151. con_flush();
  152. #endif
  153. if ( MINITESTS ) {
  154. const volatile uint32_t *p = (const volatile uint32_t *)hello_sdram;
  155. con_puts(hotstr("SDRAM jump test:"));
  156. for (int i = 0; i < 4; i++) {
  157. con_putc(' ');
  158. con_print_hex(p[i]);
  159. }
  160. con_puts(hotstr("\nJumping to SDRAM... "));
  161. hello_sdram();
  162. con_puts(hotstr("back in SRAM.\n"));
  163. }
  164. late_init();
  165. }
  166. static void __noinline hello_sdram(void)
  167. {
  168. con_puts("in SDRAM... ");
  169. }
  170. volatile uint32_t __dram_bss test_dram[8];
  171. static void __noinline late_init(void)
  172. {
  173. /* This needs to be done as early as possible!!! */
  174. con_puts("Running abc_init_memmap: ");
  175. con_flush();
  176. abc_init_memmap();
  177. con_puts("ok\n");
  178. if (SYS_MAGIC != SYS_MAGIC_MAX80) {
  179. con_puts("Not a MAX80 board?!?!\n\n");
  180. _die();
  181. } else {
  182. con_printf("MAX80 ver %u.%u rework flags %02x fpga %u\n",
  183. SYS_BOARDMAJOR, SYS_BOARDMINOR,
  184. SYS_BOARDFIX, SYS_BOARDFPGA);
  185. if (SYS_BOARDMAJOR != SYS_BOARDFPGA) {
  186. con_puts("Invalid FPGA firmware for this board revision\n");
  187. _die();
  188. }
  189. }
  190. con_putc('\n');
  191. rom_print_serial();
  192. if ( MINITESTS ) {
  193. con_puts("Quick DRAM test:\n");
  194. for (int i = 0; i < 8; i++) {
  195. uint32_t v = (i*0x11111111) + 0x44332211;
  196. test_dram[i] = v;
  197. (void)test_dram[i]; /* Force immediate readback */
  198. con_printf("%08x ", v);
  199. }
  200. con_putc('\n');
  201. for (int i = 0; i < 8; i++) {
  202. con_printf("%08x ", test_dram[i]);
  203. }
  204. con_puts("\n\nRandom number generator test:\n");
  205. for (int i = 0; i < 8; i++)
  206. con_printf("%08x ", rdrand());
  207. con_puts("\n\n");
  208. }
  209. set_leds(4);
  210. read_rtc();
  211. rtc_abc_init();
  212. set_leds(3);
  213. sdcard_reset();
  214. disk_cache_init();
  215. abcdisk_init();
  216. set_leds(2);
  217. pun80_init();
  218. set_leds(1);
  219. abc_init();
  220. esp_init(); /* Ready for communications */
  221. /* Release WAIT# if asserted */
  222. ABC_BUSCTL = 0;
  223. /* Let ESP know we are ready... */
  224. ESP_SPI_IRQ_SET = (1 << 1);
  225. set_leds(0);
  226. }