2
0

hello.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. const unsigned int sdram_words = SDRAM_SIZE >> 2;
  32. static unsigned int stride = 4;
  33. uint32_t start_time = rdtime();
  34. uint32_t w = 0;
  35. uint32_t n;
  36. con_printf("Testing SDRAM from 0x%08x to 0x%08x, stride 0x%08x...\n",
  37. SDRAM_ADDR, SDRAM_END, stride);
  38. err_char = '-';
  39. rate_limit = ERROR_RATELIMIT;
  40. for (n = 1, w = 0; n <= sdram_words; n++) {
  41. uint32_t a = w + SDRAM_ADDR;
  42. volatile uint32_t *p = (volatile uint32_t *)a;
  43. write_check(p, 0);
  44. write_check(p, ~0);
  45. write_check(p, ~w);
  46. write_check(p, w);
  47. write_check(p, A*w + B);
  48. if (!(n & 0x3ffff)) {
  49. CONSOLE = err_char;
  50. err_char = '-';
  51. }
  52. w = (w + stride) & SDRAM_MASK;
  53. }
  54. con_puts("\nReading back to check for aliases...\n");
  55. rate_limit = ERROR_RATELIMIT;
  56. for (n = 1, w = 0; n <= sdram_words; n++) {
  57. uint32_t a = w + SDRAM_ADDR;
  58. volatile uint32_t *p = (volatile uint32_t *)a;
  59. data_check(p, A*w + B);
  60. if (!(n & 0x3ffff)) {
  61. CONSOLE = err_char;
  62. err_char = '-';
  63. }
  64. w = (w - stride) & SDRAM_MASK;
  65. }
  66. con_printf("\nSDRAM test complete, time = %u ms\n",
  67. (rdtime() - start_time)/(CPU_HZ/1000));
  68. stride *= 3;
  69. stride = (stride & SDRAM_MASK) ^ (stride >> (SDRAM_ADDR_BITS-2));
  70. stride = (stride & ~3) | 4;
  71. }
  72. static void test_download(void)
  73. {
  74. volatile uint32_t *p = (uint32_t *)SDRAM_ADDR;
  75. const unsigned int words = 128*1024;
  76. unsigned int ok = words;
  77. uint32_t val = 0x00001111;
  78. unsigned int ratelimit = ERROR_RATELIMIT;
  79. for (unsigned int w = 0; w < words; w++) {
  80. uint32_t ram = *p;
  81. if (ram != val) {
  82. ok--;
  83. if (ratelimit) {
  84. ratelimit--;
  85. con_printf("%p : 0x%08x expected 0x%08x\n", p, ram, val);
  86. }
  87. }
  88. p++;
  89. val = (val * 0x89abcdef) +
  90. (uint32_t)((val * 0x89abcdefULL) >> 32) +
  91. (w * 0x76543210);
  92. }
  93. con_printf("%u/%u words OK\n\n", ok, words);
  94. for (unsigned int o = 0; o < (512*1024); o += (64*1024)) {
  95. volatile uint16_t *hp = (uint16_t *)(SDRAM_ADDR + o);
  96. p = (uint32_t *)(SDRAM_ADDR + o);
  97. for (unsigned int w = 0; w < 8; w++) {
  98. uint16_t l = *hp++;
  99. uint16_t h = *hp++;
  100. con_printf(" %04x.%04x", l, h);
  101. }
  102. con_putc('\n');
  103. }
  104. }
  105. /* Make sure we don't leave anything in SDRAM that could be a false negative */
  106. static void scrub_sdram(void)
  107. {
  108. volatile uint32_t *p;
  109. for (p = (uint32_t *)SDRAM_ADDR; p < (uint32_t *)SDRAM_END; p++)
  110. *p = 0xdeadbeef;
  111. }
  112. static volatile uint32_t timer_irq_count;
  113. static void periodic_irq(unsigned int vector)
  114. {
  115. uint32_t count = timer_irq_count;
  116. (void)vector;
  117. count++;
  118. timer_irq_count = count;
  119. set_led(count >> 3); /* 4 Hz */
  120. }
  121. static void init(void)
  122. {
  123. static const char hello[] =
  124. "\n*** Hello, World! ***\n"
  125. "Firmware compiled on: " __DATE__ " " __TIME__ "\n\n";
  126. con_set_baudrate(115000);
  127. set_led(0);
  128. con_puts(hello);
  129. register_irq(3, periodic_irq, true);
  130. }
  131. void main(void)
  132. {
  133. /* The data section is not reinitialized on reset */
  134. static unsigned int loops = 1;
  135. uint32_t done;
  136. uint32_t irq_count;
  137. while (!ROMCOPY_DONE)
  138. pause();
  139. done = rdtime() - time_zero;
  140. init();
  141. con_printf("This is loop: %u\n", loops++);
  142. con_printf("SDRAM download took %u us\n", done/(CPU_HZ/1000000));
  143. test_download();
  144. disk_init();
  145. test_sdram();
  146. scrub_sdram();
  147. irq_count = timer_irq_count;
  148. done = rdtime() - time_zero;
  149. con_printf("%u timer interrupts received in %u us\n",
  150. irq_count, done/(CPU_HZ/1000000));
  151. udelay(1000000);
  152. con_puts("*** Doing reset ***\n");
  153. con_flush();
  154. while ( 1 )
  155. RESET_CMD = 1;
  156. }