system.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "esp.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 = (volatile uint32_t *)__dram_init_start;
  109. dp < (volatile uint32_t *)__dram_init_end; dp++) {
  110. uint32_t v1, v2;
  111. v1 = *dp;
  112. v += v1;
  113. v2 = v1 ^ rx;
  114. *dp = v2;
  115. wrerr |= *dp ^ v2;
  116. *dp = v1;
  117. not_zero |= v1;
  118. all_ones &= v1;
  119. rx *= 0xf4d5725f;
  120. }
  121. con_puts(hotstr("SDRAM data checksum: "));
  122. con_print_hex(v);
  123. con_puts(hotstr(" expected "));
  124. con_print_hex(__dram_checksum);
  125. con_putc('\n');
  126. con_puts(hotstr("Bits always set, clear: "));
  127. con_print_hex(all_ones);
  128. con_putc(' ');
  129. con_print_hex(~not_zero);
  130. con_putc('\n');
  131. con_puts(hotstr("Test data: "));
  132. con_print_hex(test_data[0]);
  133. con_putc(' ');
  134. con_print_hex(test_data[1]);
  135. con_putc('\n');
  136. if (wrerr)
  137. con_puts(hotstr("SDRAM read/write error\n"));
  138. v = 0;
  139. for (dp = (volatile uint32_t *)__dram_bss_start;
  140. dp < (volatile uint32_t *)__dram_bss_end; dp++) {
  141. uint32_t x = *dp;
  142. v |= x;
  143. }
  144. if (v)
  145. con_puts(hotstr("SDRAM .bss is not zero!\n"));
  146. }
  147. if ( MINITESTS ) {
  148. uint32_t hello_dump[4];
  149. con_puts(hotstr("SDRAM jump test:"));
  150. memcpy(hello_dump, (void *)hello_sdram, sizeof hello_dump);
  151. for (int i = 0; i < 4; i++) {
  152. con_putc(' ');
  153. con_print_hex(hello_dump[i]);
  154. }
  155. con_puts(hotstr("\nJumping to SDRAM... "));
  156. hello_sdram();
  157. con_puts(hotstr("back in SRAM.\n"));
  158. }
  159. set_leds(6);
  160. con_flush();
  161. heap_init();
  162. set_leds(5);
  163. #if DELAY
  164. con_puts(hotstr("Waiting 5 s for testing..."));
  165. udelay(5000000);
  166. con_putc('\n');
  167. con_flush();
  168. #endif
  169. late_init();
  170. }
  171. static void __noinline hello_sdram(void)
  172. {
  173. con_puts("in SDRAM... ");
  174. }
  175. volatile uint32_t __dram_bss test_dram[8];
  176. static void __noinline late_init(void)
  177. {
  178. /* This needs to be done as early as possible!!! */
  179. con_puts("Running abc_init_memmap: ");
  180. con_flush();
  181. abc_init_memmap();
  182. con_puts("ok\n");
  183. if (SYS_MAGIC != SYS_MAGIC_MAX80) {
  184. con_puts("Not a MAX80 board?!?!\n\n");
  185. _die();
  186. } else {
  187. con_printf("MAX80 ver %u.%u rework flags %02x fpga %u\n",
  188. SYS_BOARDMAJOR, SYS_BOARDMINOR,
  189. SYS_BOARDFIX, SYS_BOARDFPGA);
  190. if (SYS_BOARDMAJOR != SYS_BOARDFPGA) {
  191. con_puts("Invalid FPGA firmware for this board revision\n");
  192. _die();
  193. }
  194. }
  195. con_putc('\n');
  196. if ( MINITESTS ) {
  197. con_puts("Quick DRAM test:\n");
  198. for (int i = 0; i < 8; i++) {
  199. uint32_t v = (i*0x11111111) + 0x44332211;
  200. test_dram[i] = v;
  201. (void)test_dram[i]; /* Force immediate readback */
  202. con_printf("%08x ", v);
  203. }
  204. con_putc('\n');
  205. for (int i = 0; i < 8; i++) {
  206. con_printf("%08x ", test_dram[i]);
  207. }
  208. con_puts("\n\nRandom number generator test:\n");
  209. for (int i = 0; i < 8; i++)
  210. con_printf("%08x ", rdrand());
  211. con_puts("\n\n");
  212. }
  213. set_leds(4);
  214. read_rtc();
  215. rtc_abc_init();
  216. set_leds(3);
  217. sdcard_reset();
  218. abcdisk_init();
  219. set_leds(2);
  220. pun80_init();
  221. set_leds(1);
  222. abc_init();
  223. esp_init(); /* Ready for communications */
  224. /* Release WAIT# if asserted */
  225. ABC_BUSCTL = 0;
  226. /* Let ESP know we are ready... */
  227. ESP_SPI_IRQ_SET = (1 << 1);
  228. set_leds(0);
  229. }