2
0

main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "fw.h"
  2. #include "io.h"
  3. #include "sys.h"
  4. #include "console.h"
  5. static unsigned int errors = 0;
  6. static unsigned int rate_limit;
  7. static char err_char;
  8. static void data_check(volatile uint32_t *p, uint32_t expect)
  9. {
  10. uint32_t readback = *p;
  11. if (readback != expect) {
  12. if (rate_limit) {
  13. con_printf("\n%p : read %08x expected %08x\n",
  14. p, readback, expect);
  15. rate_limit--;
  16. }
  17. err_char = 'X';
  18. errors++;
  19. }
  20. }
  21. static inline void write_check(volatile uint32_t *p, uint32_t v)
  22. {
  23. *p = v;
  24. data_check(p, v);
  25. }
  26. #define ERROR_RATELIMIT 8
  27. #define A 0x45c11ba1 /* Arbitrary odd constant */
  28. #define B 0x78dacecb /* Arbitrary constant */
  29. static void test_sdram(void)
  30. {
  31. #if 0
  32. const unsigned int sdram_words = SDRAM_SIZE >> 2;
  33. static unsigned int stride = 4;
  34. uint32_t start_time = rdtime();
  35. uint32_t w = 0;
  36. uint32_t n;
  37. con_printf("Testing SDRAM from 0x%08x to 0x%08x, stride 0x%08x...\n",
  38. SDRAM_ADDR, SDRAM_END, stride);
  39. err_char = '-';
  40. rate_limit = ERROR_RATELIMIT;
  41. for (n = 1, w = 0; n <= sdram_words; n++) {
  42. uint32_t a = w + SDRAM_ADDR;
  43. volatile uint32_t *p = (volatile uint32_t *)a;
  44. write_check(p, 0);
  45. write_check(p, ~0);
  46. write_check(p, ~w);
  47. write_check(p, w);
  48. write_check(p, A*w + B);
  49. if (!(n & 0x3ffff)) {
  50. CON_DATA = err_char;
  51. err_char = '-';
  52. }
  53. w = (w + stride) & SDRAM_MASK;
  54. }
  55. con_puts("\nReading back to check for aliases...\n");
  56. rate_limit = ERROR_RATELIMIT;
  57. for (n = 1, w = 0; n <= sdram_words; n++) {
  58. uint32_t a = w + SDRAM_ADDR;
  59. volatile uint32_t *p = (volatile uint32_t *)a;
  60. data_check(p, A*w + B);
  61. if (!(n & 0x3ffff)) {
  62. CON_DATA = err_char;
  63. err_char = '-';
  64. }
  65. w = (w - stride) & SDRAM_MASK;
  66. }
  67. con_printf("\nSDRAM test complete, time = %u ms\n",
  68. (rdtime() - start_time)/(CPU_HZ/1000));
  69. stride *= 3;
  70. stride = (stride & SDRAM_MASK) ^ (stride >> (SDRAM_BITS-2));
  71. stride = (stride & ~3) | 4;
  72. #endif
  73. }
  74. extern uint32_t __dram_bss_start[], __dram_bss_end[], __dram_bss_len[];
  75. #define TESTDATA_WORDS (128*1024)
  76. extern uint32_t testdata[TESTDATA_WORDS];
  77. static void test_download(void)
  78. {
  79. const unsigned int words = TESTDATA_WORDS;
  80. volatile uint32_t *p = testdata;
  81. unsigned int ok = words;
  82. unsigned int bsslen;
  83. uint32_t val = 0x00001111;
  84. unsigned int ratelimit = ERROR_RATELIMIT;
  85. for (unsigned int w = 0; w < words; w++) {
  86. uint32_t ram = *p;
  87. if (ram != val) {
  88. ok--;
  89. if (ratelimit) {
  90. ratelimit--;
  91. con_printf("%p : 0x%08x expected 0x%08x\n", p, ram, val);
  92. }
  93. }
  94. p++;
  95. val = (val * 0x89abcdef) +
  96. (uint32_t)((val * 0x89abcdefULL) >> 32) +
  97. (w * 0x76543210);
  98. }
  99. con_printf("SDRAM download: %u/%u words OK\n", ok, words);
  100. if (ok != words) {
  101. for (unsigned int o = 0; o < (512*1024); o += (64*1024)) {
  102. volatile uint16_t *hp = (uint16_t *)(SDRAM_ADDR + o);
  103. p = (uint32_t *)(SDRAM_ADDR + o);
  104. for (unsigned int w = 0; w < 8; w++) {
  105. uint16_t l = *hp++;
  106. uint16_t h = *hp++;
  107. con_printf(" %04x.%04x", l, h);
  108. }
  109. con_putc('\n');
  110. }
  111. }
  112. bsslen = (size_t)__dram_bss_len >> 2;
  113. ok = bsslen;
  114. for (const uint32_t *p = __dram_bss_start; p < __dram_bss_end; p++) {
  115. if (*p)
  116. ok--;
  117. }
  118. con_printf("SDRAM clear: %u/%u words OK\n", ok, bsslen);
  119. }
  120. /* Make sure we don't leave anything in SDRAM that could be a false negative */
  121. static void scrub_sdram(void)
  122. {
  123. volatile uint32_t *p;
  124. for (p = (uint32_t *)SDRAM_ADDR; p < (uint32_t *)SDRAM_END; p++)
  125. *p = 0xdeadbeef;
  126. }
  127. void init_abc_memmap(void)
  128. {
  129. volatile uint32_t *pg = &ABCMEMMAP_PAGE(0);
  130. /* Memory */
  131. for (unsigned int addr = 0; addr < 0x10000; addr += 512) {
  132. if (addr >= 0x5800 && addr < 0x6000) {
  133. *pg++ = ABCMEMMAP_RD | addr;
  134. } else if (addr >= 0x8000 && addr < 0xc000) {
  135. *pg++ = ABCMEMMAP_RD | ABCMEMMAP_WR | addr;
  136. } else {
  137. *pg++ = addr; /* Disabled */
  138. }
  139. }
  140. /* I/O */
  141. for (unsigned int sel = 0; sel < 64; sel++) {
  142. ABCMEMMAP_WRPORT(sel) = 0; /* Write DMA address/OUT enable */
  143. ABCMEMMAP_WRCOUNT(sel) = 0; /* DMA write byte count */
  144. ABCMEMMAP_RDPORT(sel) = 0; /* Read DMA address/IN enable */
  145. ABCMEMMAP_RDCOUNT(sel) = 0; /* DMA read byte count */
  146. }
  147. }
  148. static uint32_t get_null_ptr(void)
  149. {
  150. uint32_t rv;
  151. /* Prevent gcc from removing a null pointer reference */
  152. asm volatile("lw %0,0(zero)" : "=r" (rv));
  153. return rv;
  154. }
  155. void main(void)
  156. {
  157. /* The data section is not reinitialized on reset */
  158. static unsigned int loops = 1;
  159. uint32_t null_ptr;
  160. uint32_t irq_count;
  161. uint32_t abc_status;
  162. uint32_t done;
  163. uint32_t time_to_main;
  164. time_to_main = rdtime() - time_zero;
  165. null_ptr = get_null_ptr();
  166. con_set_baudrate(115200);
  167. CON_DATA = '1';
  168. unmask_irq(BUSERR_IRQ);
  169. CON_DATA = '2';
  170. unmask_irq(EBREAK_IRQ);
  171. CON_DATA = '3';
  172. unmask_irq(ROMCOPY_IRQ);
  173. CON_DATA = '4';
  174. init();
  175. con_printf("This is loop: %u\n", loops++);
  176. abc_status = ABC_STATUS;
  177. if (abc_status & ABC_STATUS_LIVE) {
  178. con_printf("I seem to be connected to ABC%d\n",
  179. (ABC_STATUS & ABC_STATUS_800) ? 800 : 80);
  180. } else {
  181. con_puts("No ABC-bus host detected\n");
  182. }
  183. con_printf("Bootup code/clearing .bss took %u us\n",
  184. time_to_main/(CPU_HZ/1000000));
  185. while (!(irqmask() & (1 << ROMCOPY_IRQ)))
  186. pause();
  187. con_printf("SDRAM download took %u us\n",
  188. romcopy_time[0]/(CPU_HZ/1000000));
  189. con_printf("SDRAM bss clear took %u us\n",
  190. romcopy_time[1]/(CPU_HZ/1000000));
  191. test_download();
  192. sdcard_init();
  193. test_sdram();
  194. scrub_sdram();
  195. irq_count = timer_count();
  196. done = rdtime() - time_zero; /* timer_irq_start */
  197. con_printf("%u timer interrupts received in %u ms, ~%u expected\n",
  198. irq_count, done/(CPU_HZ/1000),
  199. (done+(CPU_HZ/64))/(CPU_HZ/32));
  200. udelay(1000000);
  201. uint32_t null_ptr_now = get_null_ptr();
  202. if (null_ptr != null_ptr_now) {
  203. con_printf("*** NULL POINTER AREA CORRUPT: 0x%08x not 0x%08x\n",
  204. null_ptr_now, null_ptr);
  205. con_flush();
  206. _die();
  207. }
  208. con_puts("*** Doing reset ***\n");
  209. con_flush();
  210. reset();
  211. }