system.c 4.8 KB

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