system.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include "esplink.h"
  8. #define DEBUG 0
  9. #define MINITESTS 1
  10. #define DELAY 0
  11. void __hot con_print_hex(unsigned int n)
  12. {
  13. for (int i = 0; i < 8; i++) {
  14. unsigned int c = n >> 28;
  15. n <<= 4;
  16. con_putc(c + ((c >= 10) ? 'a'-10 : '0'));
  17. }
  18. }
  19. /* Don't mark no_return or gcc moves it to SDRAM */
  20. static void __hot __text_hot killed(const char *how, size_t pc)
  21. {
  22. /* Cannot use con_printf() here */
  23. const uint16_t *pcp;
  24. size_t mtval;
  25. asm volatile("csrr %0,mtval" : "=r" (mtval));
  26. /* Try to move back to the previous instruction (if not a jump...) */
  27. pc += -4 + (pc & 1);
  28. pcp = (const uint16_t *)pc;
  29. con_puts(hotstr("ERROR: "));
  30. con_puts(how);
  31. con_puts(hotstr(" at 0x"));
  32. con_print_hex(pc);
  33. con_puts(hotstr(" (0x"));
  34. con_print_hex((pcp[1] << 16) + pcp[0]);
  35. con_puts(hotstr(")\nBad address: 0x"));
  36. con_print_hex(mtval);
  37. con_putc('\n');
  38. for (int i = 0; i < 32; i += 8) {
  39. for (int j = 0; j < 8; j++) {
  40. uint32_t v = rdxreg(i+j);
  41. con_print_hex(v);
  42. con_putc((j == 7) ? '\n' : ' ');
  43. }
  44. }
  45. con_flush();
  46. udelay(5000000);
  47. reset(SYS_RESET_SOFT);
  48. }
  49. IRQHANDLER(buserr,0)
  50. {
  51. killed(hotstr("misaligned"), pc);
  52. }
  53. IRQHANDLER(ebreak,0)
  54. {
  55. killed(hotstr("invalid instruction"), pc);
  56. }
  57. volatile __sbss uint32_t timer_irq_count;
  58. IRQHANDLER(sysclock,0)
  59. {
  60. uint32_t count = timer_irq_count;
  61. count++;
  62. timer_irq_count = count;
  63. if ( MINITESTS ) {
  64. static const char spinner[4] = "/-\\|";
  65. if (!(count & (TIMER_HZ-1))) {
  66. uint32_t seconds = count >> TIMER_SHIFT;
  67. CON_DATA = spinner[seconds & 3];
  68. CON_DATA = '\b';
  69. }
  70. }
  71. }
  72. static void late_init(void);
  73. static void hello_sdram(void);
  74. #define TEST_DATA_0 0x01234567
  75. #define TEST_DATA_1 0x98badcfe
  76. static const volatile __dram_data
  77. uint32_t test_data[2] = { TEST_DATA_0, TEST_DATA_1 };
  78. uint32_t __sbss timer_irq_start;
  79. void __hot init(void)
  80. {
  81. static __string_hot const char hello[] =
  82. "\n\n*** Hello, World! ***\n"
  83. "MAX80 "
  84. #ifdef TEST
  85. "testing"
  86. #endif
  87. "firmware compiled on: ";
  88. timer_irq_start = rdtime();
  89. /* Start ROM copy engine, unmask timer and fatal exceptions */
  90. unmask_irqs((1U << ROMCOPY_IRQ)|(1U << EBREAK_IRQ)|
  91. (1U << BUSERR_IRQ)|(1U << SYSCLOCK_IRQ));
  92. con_puts(hello);
  93. con_puts(__datestamp);
  94. con_putc('\n');
  95. set_leds(7);
  96. wait_romcopy_done();
  97. if ( test_data[0] == TEST_DATA_0 && test_data[1] == TEST_DATA_1 ) {
  98. con_puts(hotstr("SDRAM seems ok - skipping test\n"));
  99. } else {
  100. volatile uint32_t *dp;
  101. uint32_t v;
  102. uint32_t wrerr;
  103. uint32_t not_zero, all_ones;
  104. uint32_t rx = 0x193ac604;
  105. v = wrerr = 0;
  106. all_ones = -1;
  107. not_zero = 0;
  108. for (dp = __dram_init_start; dp < __dram_init_end; dp++) {
  109. uint32_t v1, v2;
  110. v1 = *dp;
  111. v += v1;
  112. v2 = v1 ^ rx;
  113. *dp = v2;
  114. wrerr |= *dp ^ v2;
  115. *dp = v1;
  116. not_zero |= v1;
  117. all_ones &= v1;
  118. rx *= 0xf4d5725f;
  119. }
  120. con_puts(hotstr("SDRAM data checksum: "));
  121. con_print_hex(v);
  122. con_puts(hotstr(" expected "));
  123. con_print_hex(__dram_checksum);
  124. con_putc('\n');
  125. con_puts(hotstr("Bits always set, clear: "));
  126. con_print_hex(all_ones);
  127. con_putc(' ');
  128. con_print_hex(~not_zero);
  129. con_putc('\n');
  130. con_puts(hotstr("Test data: "));
  131. con_print_hex(test_data[0]);
  132. con_putc(' ');
  133. con_print_hex(test_data[1]);
  134. con_putc('\n');
  135. if (wrerr)
  136. con_puts(hotstr("SDRAM read/write error\n"));
  137. v = 0;
  138. for (dp = __dram_bss_start; dp < __dram_bss_end; dp++) {
  139. uint32_t x = *dp;
  140. v |= x;
  141. }
  142. if (v)
  143. con_puts(hotstr("SDRAM .bss is not zero!\n"));
  144. }
  145. if ( MINITESTS ) {
  146. uint32_t hello_dump[4];
  147. con_puts(hotstr("SDRAM jump test:"));
  148. memcpy(hello_dump, (void *)hello_sdram, sizeof hello_dump);
  149. for (int i = 0; i < 4; i++) {
  150. con_putc(' ');
  151. con_print_hex(hello_dump[i]);
  152. }
  153. con_puts(hotstr("\nJumping to SDRAM... "));
  154. hello_sdram();
  155. con_puts(hotstr("back in SRAM.\n"));
  156. }
  157. set_leds(6);
  158. con_flush();
  159. heap_init();
  160. set_leds(5);
  161. #if DELAY
  162. con_puts(hotstr("Waiting 5 s for testing..."));
  163. udelay(5000000);
  164. con_putc('\n');
  165. con_flush();
  166. #endif
  167. late_init();
  168. }
  169. static void __noinline hello_sdram(void)
  170. {
  171. con_puts("in SDRAM... ");
  172. }
  173. volatile uint32_t __dram_bss test_dram[8];
  174. static void __noinline late_init(void)
  175. {
  176. /* This needs to be done as early as possible!!! */
  177. con_puts("Running abc_init_memmap: ");
  178. con_flush();
  179. abc_init_memmap();
  180. con_puts("ok\n");
  181. if (SYS_MAGIC != SYS_MAGIC_MAX80) {
  182. con_puts("Not a MAX80 board?!?!\n\n");
  183. _die();
  184. } else {
  185. con_printf("MAX80 ver %u.%u rework flags %02x fpga %u\n",
  186. SYS_BOARDMAJOR, SYS_BOARDMINOR,
  187. SYS_BOARDFIX, SYS_BOARDFPGA);
  188. if (SYS_BOARDMAJOR != SYS_BOARDFPGA) {
  189. con_puts("Invalid FPGA firmware for this board revision\n");
  190. _die();
  191. }
  192. }
  193. con_putc('\n');
  194. rom_print_serial();
  195. if ( MINITESTS ) {
  196. con_puts("Quick DRAM test:\n");
  197. for (int i = 0; i < 8; i++) {
  198. uint32_t v = (i*0x11111111) + 0x44332211;
  199. test_dram[i] = v;
  200. (void)test_dram[i]; /* Force immediate readback */
  201. con_printf("%08x ", v);
  202. }
  203. con_putc('\n');
  204. for (int i = 0; i < 8; i++) {
  205. con_printf("%08x ", test_dram[i]);
  206. }
  207. con_puts("\n\nRandom number generator test:\n");
  208. for (int i = 0; i < 8; i++)
  209. con_printf("%08x ", rdrand());
  210. con_puts("\n\n");
  211. }
  212. set_leds(4);
  213. read_rtc();
  214. rtc_abc_init();
  215. set_leds(3);
  216. sdcard_reset();
  217. abcdisk_init();
  218. set_leds(2);
  219. pun80_init();
  220. set_leds(1);
  221. abc_init();
  222. esp_init(); /* Ready for communications */
  223. /* Release WAIT# if asserted */
  224. ABC_BUSCTL = 0;
  225. /* Let ESP know we are ready... */
  226. ESP_SPI_IRQ_SET = (1 << 1);
  227. set_leds(0);
  228. }