hello.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_ADDR_BITS-2));
  71. stride = (stride & ~3) | 4;
  72. #endif
  73. }
  74. static void test_download(void)
  75. {
  76. volatile uint32_t *p = (uint32_t *)SDRAM_ADDR;
  77. const unsigned int words = 128*1024;
  78. unsigned int ok = words;
  79. uint32_t val = 0x00001111;
  80. unsigned int ratelimit = ERROR_RATELIMIT;
  81. for (unsigned int w = 0; w < words; w++) {
  82. uint32_t ram = *p;
  83. if (ram != val) {
  84. ok--;
  85. if (ratelimit) {
  86. ratelimit--;
  87. con_printf("%p : 0x%08x expected 0x%08x\n", p, ram, val);
  88. }
  89. }
  90. p++;
  91. val = (val * 0x89abcdef) +
  92. (uint32_t)((val * 0x89abcdefULL) >> 32) +
  93. (w * 0x76543210);
  94. }
  95. con_printf("%u/%u words OK\n\n", ok, words);
  96. for (unsigned int o = 0; o < (512*1024); o += (64*1024)) {
  97. volatile uint16_t *hp = (uint16_t *)(SDRAM_ADDR + o);
  98. p = (uint32_t *)(SDRAM_ADDR + o);
  99. for (unsigned int w = 0; w < 8; w++) {
  100. uint16_t l = *hp++;
  101. uint16_t h = *hp++;
  102. con_printf(" %04x.%04x", l, h);
  103. }
  104. con_putc('\n');
  105. }
  106. }
  107. /* Make sure we don't leave anything in SDRAM that could be a false negative */
  108. static void scrub_sdram(void)
  109. {
  110. volatile uint32_t *p;
  111. for (p = (uint32_t *)SDRAM_ADDR; p < (uint32_t *)SDRAM_END; p++)
  112. *p = 0xdeadbeef;
  113. }
  114. static volatile uint32_t timer_irq_count;
  115. IRQHANDLER(sysclock)
  116. {
  117. uint32_t count = timer_irq_count;
  118. count++;
  119. timer_irq_count = count;
  120. set_led(count >> 3); /* 4 Hz */
  121. }
  122. static void init_abc_memmap(void)
  123. {
  124. volatile uint32_t *pg = &ABCMEMMAP_PAGE(0);
  125. /* Memory */
  126. for (unsigned int addr = 0; addr < 0x10000; addr += 512) {
  127. if (addr >= 0x5800 && addr < 0x6000) {
  128. *pg++ = ABCMEMMAP_RD | addr;
  129. } else if (addr >= 0x8000 && addr < 0xc000) {
  130. *pg++ = ABCMEMMAP_RD | ABCMEMMAP_WR | addr;
  131. } else {
  132. *pg++ = addr; /* Disabled */
  133. }
  134. }
  135. /* I/O */
  136. for (unsigned int sel = 0; sel < 64; sel++) {
  137. ABCMEMMAP_WRPORT(sel) = 0; /* Write DMA address/OUT enable */
  138. ABCMEMMAP_WRCOUNT(sel) = 0; /* DMA write byte count */
  139. ABCMEMMAP_RDPORT(sel) = 0; /* Read DMA address/IN enable */
  140. ABCMEMMAP_RDCOUNT(sel) = 0; /* DMA read byte count */
  141. }
  142. }
  143. static uint32_t timer_irq_start;
  144. static void init(void)
  145. {
  146. static const char hello[] =
  147. "\n*** Hello, World! ***\n"
  148. "Firmware compiled on: " __DATE__ " " __TIME__ "\n\n";
  149. init_abc_memmap();
  150. con_set_baudrate(115000);
  151. set_led(0);
  152. con_puts(hello);
  153. timer_irq_count = 0;
  154. timer_irq_start = rdtime();
  155. unmask_irq(SYSCLOCK_IRQ);
  156. read_rtc();
  157. }
  158. void main(void)
  159. {
  160. /* The data section is not reinitialized on reset */
  161. static unsigned int loops = 1;
  162. uint32_t done;
  163. uint32_t irq_count;
  164. uint32_t abc_status;
  165. while (!(SYS_ROMCOPY & SYS_ROMCOPY_DONE))
  166. pause();
  167. done = rdtime() - time_zero;
  168. init();
  169. con_printf("This is loop: %u\n", loops++);
  170. con_printf("SDRAM download took %u us\n", done/(CPU_HZ/1000000));
  171. abc_status = ABC_STATUS;
  172. if (abc_status & ABC_STATUS_LIVE) {
  173. con_printf("I seem to be connected to ABC%d\n",
  174. (ABC_STATUS & ABC_STATUS_800) ? 800 : 80);
  175. } else {
  176. con_puts("No ABC-bus host detected\n");
  177. }
  178. test_download();
  179. disk_init();
  180. test_sdram();
  181. scrub_sdram();
  182. irq_count = timer_irq_count;
  183. done = rdtime() - timer_irq_start;
  184. con_printf("%u timer interrupts received in %u us\n",
  185. irq_count, done/(CPU_HZ/1000000));
  186. udelay(1000000);
  187. con_puts("*** Doing reset ***\n");
  188. con_flush();
  189. while ( 1 )
  190. reset();
  191. }