2
0

main.c 5.9 KB

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