hello.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "fw.h"
  2. #include "io.h"
  3. #include "console.h"
  4. #define SDRAM_ADDR 0x40000000
  5. #define SDRAM_ADDR_BITS 25
  6. #define SDRAM_SIZE (1U << SDRAM_ADDR_BITS)
  7. #define SDRAM_MASK (SDRAM_SIZE - 1)
  8. #define SDRAM_END (SDRAM_ADDR + SDRAM_SIZE)
  9. static unsigned int errors = 0;
  10. static unsigned int rate_limit;
  11. static char err_char;
  12. static void data_check(volatile uint32_t *p, uint32_t expect)
  13. {
  14. uint32_t readback = *p;
  15. if (readback != expect) {
  16. if (rate_limit) {
  17. con_printf("\r\n%p : read %08x expected %08x\r\n",
  18. p, readback, expect);
  19. rate_limit--;
  20. }
  21. err_char = 'X';
  22. errors++;
  23. }
  24. }
  25. static inline void write_check(volatile uint32_t *p, uint32_t v)
  26. {
  27. *p = v;
  28. data_check(p, v);
  29. }
  30. #define ERROR_RATELIMIT 8
  31. #define A 0x45c11ba1 /* Arbitrary odd constant */
  32. #define B 0x78dacecb /* Arbitrary constant */
  33. static void test_sdram(void)
  34. {
  35. const unsigned int sdram_words = SDRAM_SIZE >> 2;
  36. static unsigned int stride = 4;
  37. uint32_t start_time = rdtime();
  38. uint32_t w = 0;
  39. uint32_t n;
  40. con_printf("Testing SDRAM from 0x%08x to 0x%08x, stride 0x%08x...\r\n",
  41. SDRAM_ADDR, SDRAM_END, stride);
  42. err_char = '-';
  43. rate_limit = ERROR_RATELIMIT;
  44. for (n = 1, w = 0; n <= sdram_words; n++) {
  45. uint32_t a = w + SDRAM_ADDR;
  46. volatile uint32_t *p = (volatile uint32_t *)a;
  47. write_check(p, 0);
  48. write_check(p, ~0);
  49. write_check(p, ~w);
  50. write_check(p, w);
  51. write_check(p, A*w + B);
  52. if (!(n & 0x3ffff)) {
  53. CONSOLE = err_char;
  54. err_char = '-';
  55. }
  56. w = (w + stride) & SDRAM_MASK;
  57. }
  58. con_puts("\r\nReading back to check for aliases...\r\n");
  59. rate_limit = ERROR_RATELIMIT;
  60. for (n = 1, w = 0; n <= sdram_words; n++) {
  61. uint32_t a = w + SDRAM_ADDR;
  62. volatile uint32_t *p = (volatile uint32_t *)a;
  63. data_check(p, A*w + B);
  64. if (!(n & 0x3ffff)) {
  65. CONSOLE = err_char;
  66. err_char = '-';
  67. }
  68. w = (w - stride) & SDRAM_MASK;
  69. }
  70. con_printf("\rSDRAM test complete, time = %u ms\r\n",
  71. (rdtime() - start_time)/(CPU_HZ/1000));
  72. stride *= 3;
  73. stride = (stride & SDRAM_MASK) ^ (stride >> (SDRAM_ADDR_BITS-2));
  74. stride = (stride & ~3) | 4;
  75. }
  76. #define SDRAM_DONE IODEVRL(2,0)
  77. void main(void)
  78. {
  79. static const char hello[] = /* "\f\033[2J\033[H" */
  80. "\r\n\n*** Hello, World! ***\r\n"
  81. "Firmware compiled on: " __DATE__ " " __TIME__ "\r\n\n";
  82. /* The data section is not reinitialized on reset */
  83. static unsigned int loops = 1;
  84. uint8_t led = 0;
  85. uint32_t done;
  86. con_set_baudrate(115200);
  87. set_led(led = 0);
  88. while (!SDRAM_DONE)
  89. /* wait */;
  90. done = rdtime();
  91. con_puts(hello);
  92. con_printf("This is loop: %u\n", loops++);
  93. con_printf("SDRAM download took %u us\n", done/(CPU_HZ/1000000));
  94. volatile uint32_t *p = (uint32_t *)SDRAM_ADDR;
  95. const unsigned int words = 128*1024;
  96. unsigned int ok = words;
  97. uint32_t val = 0x00001111;
  98. for (unsigned int w = 0; w < words; w++) {
  99. if (*p++ != val)
  100. ok--;
  101. val = (val * 0x89abcdef) +
  102. (uint32_t)((val * 0x89abcdefULL) >> 32) +
  103. (w * 0x76543210);
  104. }
  105. con_printf("%u/%u words OK\n\n", ok, words);
  106. for (unsigned int o = 0; o < (512*1024); o += (64*1024)) {
  107. volatile uint16_t *hp = (uint16_t *)(SDRAM_ADDR + o);
  108. p = (uint32_t *)(SDRAM_ADDR + o);
  109. for (unsigned int w = 0; w < 8; w++) {
  110. uint16_t l = *hp++;
  111. uint16_t h = *hp++;
  112. con_printf(" %04x.%04x", l, h);
  113. }
  114. con_putc('\n');
  115. }
  116. test_sdram();
  117. p = (uint32_t *)SDRAM_ADDR;
  118. for (unsigned int w = 0; w < words; w++)
  119. *p++ = 0xdeadbeef;
  120. udelay(4000000);
  121. con_puts("*** Doing reset ***\r\n\n");
  122. con_flush();
  123. while ( 1 )
  124. RESET_CMD = 1;
  125. }