hello.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. static volatile uint32_t timer_irq_count;
  128. IRQHANDLER(sysclock)
  129. {
  130. uint32_t count = timer_irq_count;
  131. count++;
  132. timer_irq_count = count;
  133. set_led(count >> 3); /* 4 Hz */
  134. }
  135. static void init_abc_memmap(void)
  136. {
  137. volatile uint32_t *pg = &ABCMEMMAP_PAGE(0);
  138. /* Memory */
  139. for (unsigned int addr = 0; addr < 0x10000; addr += 512) {
  140. if (addr >= 0x5800 && addr < 0x6000) {
  141. *pg++ = ABCMEMMAP_RD | addr;
  142. } else if (addr >= 0x8000 && addr < 0xc000) {
  143. *pg++ = ABCMEMMAP_RD | ABCMEMMAP_WR | addr;
  144. } else {
  145. *pg++ = addr; /* Disabled */
  146. }
  147. }
  148. /* I/O */
  149. for (unsigned int sel = 0; sel < 64; sel++) {
  150. ABCMEMMAP_WRPORT(sel) = 0; /* Write DMA address/OUT enable */
  151. ABCMEMMAP_WRCOUNT(sel) = 0; /* DMA write byte count */
  152. ABCMEMMAP_RDPORT(sel) = 0; /* Read DMA address/IN enable */
  153. ABCMEMMAP_RDCOUNT(sel) = 0; /* DMA read byte count */
  154. }
  155. }
  156. static uint32_t timer_irq_start;
  157. static void init(void)
  158. {
  159. static const char hello[] =
  160. "\n*** Hello, World! ***\n"
  161. "Firmware compiled on: " __DATE__ " " __TIME__ "\n\n";
  162. set_led(0);
  163. con_set_baudrate(115200);
  164. con_puts(hello);
  165. init_abc_memmap();
  166. timer_irq_count = 0;
  167. timer_irq_start = rdtime();
  168. unmask_irq(SYSCLOCK_IRQ);
  169. read_rtc();
  170. }
  171. static uint32_t romcopy_time[2];
  172. static unsigned int romcopy_state;
  173. IRQHANDLER(romcopy)
  174. {
  175. switch (romcopy_state++) {
  176. case 0:
  177. /* Copy testdata */
  178. ROMCOPY_RAMADDR = 0;
  179. ROMCOPY_ROMADDR = 0x100000;
  180. ROMCOPY_DATALEN = TESTDATA_WORDS << 2;
  181. break;
  182. case 1:
  183. /* Zero .dram.bss */
  184. romcopy_time[0] = rdtime() - time_zero;
  185. ROMCOPY_RAMADDR = (size_t)__dram_bss_start;
  186. ROMCOPY_ROMADDR = 0; /* Zero */
  187. ROMCOPY_DATALEN = (size_t)__dram_bss_len;
  188. break;
  189. default:
  190. romcopy_time[1] = rdtime() - romcopy_time[0];
  191. mask_irq(ROMCOPY_IRQ);
  192. return;
  193. }
  194. }
  195. static no_return killed(const char *how, size_t pc)
  196. {
  197. con_printf("ERROR: %s at 0x%08x\n", how, pc);
  198. con_flush();
  199. udelay(5000000);
  200. reset();
  201. }
  202. IRQHANDLER(BUSERR)
  203. {
  204. killed("misaligned", pc);
  205. }
  206. IRQHANDLER(EBREAK)
  207. {
  208. killed("invalid instruction", pc);
  209. }
  210. static uint32_t get_reset_vector(void)
  211. {
  212. uint32_t rv;
  213. /* Prevent gcc from removing a null pointer reference */
  214. asm volatile("lw %0,0(zero)" : "=r" (rv));
  215. return rv;
  216. }
  217. void main(void)
  218. {
  219. /* The data section is not reinitialized on reset */
  220. static unsigned int loops = 1;
  221. uint32_t reset_vector;
  222. uint32_t irq_count;
  223. uint32_t abc_status;
  224. uint32_t done;
  225. uint32_t time_to_main;
  226. time_to_main = rdtime() - time_zero;
  227. reset_vector = get_reset_vector();
  228. con_set_baudrate(115200);
  229. CON_DATA = '1';
  230. unmask_irq(BUSERR_IRQ);
  231. CON_DATA = '2';
  232. unmask_irq(EBREAK_IRQ);
  233. CON_DATA = '3';
  234. romcopy_state = 0;
  235. unmask_irq(ROMCOPY_IRQ);
  236. CON_DATA = '4';
  237. init();
  238. con_printf("This is loop: %u\n", loops++);
  239. abc_status = ABC_STATUS;
  240. if (abc_status & ABC_STATUS_LIVE) {
  241. con_printf("I seem to be connected to ABC%d\n",
  242. (ABC_STATUS & ABC_STATUS_800) ? 800 : 80);
  243. } else {
  244. con_puts("No ABC-bus host detected\n");
  245. }
  246. con_printf("Bootup code/clearing .bss took %u us\n",
  247. time_to_main/(CPU_HZ/1000000));
  248. while (!(irqmask() & (1 << ROMCOPY_IRQ)))
  249. pause();
  250. con_printf("SDRAM download took %u us\n",
  251. romcopy_time[0]/(CPU_HZ/1000000));
  252. con_printf("SDRAM bss clear took %u us\n",
  253. romcopy_time[1]/(CPU_HZ/1000000));
  254. test_download();
  255. disk_init();
  256. test_sdram();
  257. scrub_sdram();
  258. irq_count = timer_irq_count;
  259. done = rdtime() - time_zero; /* timer_irq_start */
  260. con_printf("%u timer interrupts received in %u ms, ~%u expected\n",
  261. irq_count, done/(CPU_HZ/1000),
  262. (done+(CPU_HZ/64))/(CPU_HZ/32));
  263. udelay(1000000);
  264. uint32_t reset_vector_now = get_reset_vector();
  265. if (reset_vector != reset_vector_now) {
  266. con_printf("*** RESET VECTOR CORRUPT: 0x%08x not 0x%08x\n",
  267. reset_vector_now, reset_vector);
  268. con_flush();
  269. _die();
  270. }
  271. con_puts("*** Doing reset ***\n");
  272. con_flush();
  273. reset();
  274. }