123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #include "fw.h"
- #include "io.h"
- #include "sys.h"
- #include "console.h"
- static unsigned int errors = 0;
- static unsigned int rate_limit;
- static char err_char;
- static void data_check(volatile uint32_t *p, uint32_t expect)
- {
- uint32_t readback = *p;
- if (readback != expect) {
- if (rate_limit) {
- con_printf("\n%p : read %08x expected %08x\n",
- p, readback, expect);
- rate_limit--;
- }
- err_char = 'X';
- errors++;
- }
- }
- static inline void write_check(volatile uint32_t *p, uint32_t v)
- {
- *p = v;
- data_check(p, v);
- }
- #define ERROR_RATELIMIT 8
- #define A 0x45c11ba1 /* Arbitrary odd constant */
- #define B 0x78dacecb /* Arbitrary constant */
- static void test_sdram(void)
- {
- #if 0
- const unsigned int sdram_words = SDRAM_SIZE >> 2;
- static unsigned int stride = 4;
- uint32_t start_time = rdtime();
- uint32_t w = 0;
- uint32_t n;
- con_printf("Testing SDRAM from 0x%08x to 0x%08x, stride 0x%08x...\n",
- SDRAM_ADDR, SDRAM_END, stride);
- err_char = '-';
- rate_limit = ERROR_RATELIMIT;
- for (n = 1, w = 0; n <= sdram_words; n++) {
- uint32_t a = w + SDRAM_ADDR;
- volatile uint32_t *p = (volatile uint32_t *)a;
- write_check(p, 0);
- write_check(p, ~0);
- write_check(p, ~w);
- write_check(p, w);
- write_check(p, A*w + B);
- if (!(n & 0x3ffff)) {
- CON_DATA = err_char;
- err_char = '-';
- }
- w = (w + stride) & SDRAM_MASK;
- }
- con_puts("\nReading back to check for aliases...\n");
- rate_limit = ERROR_RATELIMIT;
- for (n = 1, w = 0; n <= sdram_words; n++) {
- uint32_t a = w + SDRAM_ADDR;
- volatile uint32_t *p = (volatile uint32_t *)a;
- data_check(p, A*w + B);
- if (!(n & 0x3ffff)) {
- CON_DATA = err_char;
- err_char = '-';
- }
- w = (w - stride) & SDRAM_MASK;
- }
- con_printf("\nSDRAM test complete, time = %u ms\n",
- (rdtime() - start_time)/(CPU_HZ/1000));
- stride *= 3;
- stride = (stride & SDRAM_MASK) ^ (stride >> (SDRAM_BITS-2));
- stride = (stride & ~3) | 4;
- #endif
- }
- extern uint32_t __dram_bss_start[], __dram_bss_end[], __dram_bss_len[];
- #define TESTDATA_WORDS (128*1024)
- extern uint32_t testdata[TESTDATA_WORDS];
- static void test_download(void)
- {
- const unsigned int words = TESTDATA_WORDS;
- volatile uint32_t *p = testdata;
- unsigned int ok = words;
- unsigned int bsslen;
- uint32_t val = 0x00001111;
- unsigned int ratelimit = ERROR_RATELIMIT;
- for (unsigned int w = 0; w < words; w++) {
- uint32_t ram = *p;
- if (ram != val) {
- ok--;
- if (ratelimit) {
- ratelimit--;
- con_printf("%p : 0x%08x expected 0x%08x\n", p, ram, val);
- }
- }
- p++;
- val = (val * 0x89abcdef) +
- (uint32_t)((val * 0x89abcdefULL) >> 32) +
- (w * 0x76543210);
- }
- con_printf("SDRAM download: %u/%u words OK\n", ok, words);
- if (ok != words) {
- for (unsigned int o = 0; o < (512*1024); o += (64*1024)) {
- volatile uint16_t *hp = (uint16_t *)(SDRAM_ADDR + o);
- p = (uint32_t *)(SDRAM_ADDR + o);
- for (unsigned int w = 0; w < 8; w++) {
- uint16_t l = *hp++;
- uint16_t h = *hp++;
- con_printf(" %04x.%04x", l, h);
- }
- con_putc('\n');
- }
- }
- bsslen = (size_t)__dram_bss_len >> 2;
- ok = bsslen;
- for (const uint32_t *p = __dram_bss_start; p < __dram_bss_end; p++) {
- if (*p)
- ok--;
- }
- con_printf("SDRAM clear: %u/%u words OK\n", ok, bsslen);
- }
- /* Make sure we don't leave anything in SDRAM that could be a false negative */
- static void scrub_sdram(void)
- {
- volatile uint32_t *p;
- for (p = (uint32_t *)SDRAM_ADDR; p < (uint32_t *)SDRAM_END; p++)
- *p = 0xdeadbeef;
- }
- static volatile uint32_t timer_irq_count;
- IRQHANDLER(sysclock)
- {
- uint32_t count = timer_irq_count;
- count++;
- timer_irq_count = count;
- set_led(count >> 3); /* 4 Hz */
- }
- static void init_abc_memmap(void)
- {
- volatile uint32_t *pg = &ABCMEMMAP_PAGE(0);
- /* Memory */
- for (unsigned int addr = 0; addr < 0x10000; addr += 512) {
- if (addr >= 0x5800 && addr < 0x6000) {
- *pg++ = ABCMEMMAP_RD | addr;
- } else if (addr >= 0x8000 && addr < 0xc000) {
- *pg++ = ABCMEMMAP_RD | ABCMEMMAP_WR | addr;
- } else {
- *pg++ = addr; /* Disabled */
- }
- }
- /* I/O */
- for (unsigned int sel = 0; sel < 64; sel++) {
- ABCMEMMAP_WRPORT(sel) = 0; /* Write DMA address/OUT enable */
- ABCMEMMAP_WRCOUNT(sel) = 0; /* DMA write byte count */
- ABCMEMMAP_RDPORT(sel) = 0; /* Read DMA address/IN enable */
- ABCMEMMAP_RDCOUNT(sel) = 0; /* DMA read byte count */
- }
- }
- static uint32_t timer_irq_start;
- static void init(void)
- {
- static const char hello[] =
- "\n*** Hello, World! ***\n"
- "Firmware compiled on: " __DATE__ " " __TIME__ "\n\n";
- set_led(0);
- con_set_baudrate(115200);
- con_puts(hello);
- init_abc_memmap();
- timer_irq_count = 0;
- timer_irq_start = rdtime();
- unmask_irq(SYSCLOCK_IRQ);
- read_rtc();
- }
- static uint32_t romcopy_time[2];
- static unsigned int romcopy_state;
- IRQHANDLER(romcopy)
- {
- switch (romcopy_state++) {
- case 0:
- /* Copy testdata */
- ROMCOPY_RAMADDR = 0;
- ROMCOPY_ROMADDR = 0x100000;
- ROMCOPY_DATALEN = TESTDATA_WORDS << 2;
- break;
- case 1:
- /* Zero .dram.bss */
- romcopy_time[0] = rdtime() - time_zero;
- ROMCOPY_RAMADDR = (size_t)__dram_bss_start;
- ROMCOPY_ROMADDR = 0; /* Zero */
- ROMCOPY_DATALEN = (size_t)__dram_bss_len;
- break;
- default:
- romcopy_time[1] = rdtime() - romcopy_time[0];
- mask_irq(ROMCOPY_IRQ);
- return;
- }
- }
- static no_return killed(const char *how, size_t pc)
- {
- con_printf("ERROR: %s at 0x%08x\n", how, pc);
- con_flush();
- udelay(5000000);
- reset();
- }
- IRQHANDLER(BUSERR)
- {
- killed("misaligned", pc);
- }
- IRQHANDLER(EBREAK)
- {
- killed("invalid instruction", pc);
- }
- static uint32_t get_reset_vector(void)
- {
- uint32_t rv;
- /* Prevent gcc from removing a null pointer reference */
- asm volatile("lw %0,0(zero)" : "=r" (rv));
- return rv;
- }
- void main(void)
- {
- /* The data section is not reinitialized on reset */
- static unsigned int loops = 1;
- uint32_t reset_vector;
- uint32_t irq_count;
- uint32_t abc_status;
- uint32_t done;
- uint32_t time_to_main;
- time_to_main = rdtime() - time_zero;
- reset_vector = get_reset_vector();
- con_set_baudrate(115200);
- CON_DATA = '1';
- unmask_irq(BUSERR_IRQ);
- CON_DATA = '2';
- unmask_irq(EBREAK_IRQ);
- CON_DATA = '3';
- romcopy_state = 0;
- unmask_irq(ROMCOPY_IRQ);
- CON_DATA = '4';
- init();
- con_printf("This is loop: %u\n", loops++);
- abc_status = ABC_STATUS;
- if (abc_status & ABC_STATUS_LIVE) {
- con_printf("I seem to be connected to ABC%d\n",
- (ABC_STATUS & ABC_STATUS_800) ? 800 : 80);
- } else {
- con_puts("No ABC-bus host detected\n");
- }
- con_printf("Bootup code/clearing .bss took %u us\n",
- time_to_main/(CPU_HZ/1000000));
- while (!(irqmask() & (1 << ROMCOPY_IRQ)))
- pause();
- con_printf("SDRAM download took %u us\n",
- romcopy_time[0]/(CPU_HZ/1000000));
- con_printf("SDRAM bss clear took %u us\n",
- romcopy_time[1]/(CPU_HZ/1000000));
- test_download();
- disk_init();
- test_sdram();
- scrub_sdram();
- irq_count = timer_irq_count;
- done = rdtime() - time_zero; /* timer_irq_start */
- con_printf("%u timer interrupts received in %u ms, ~%u expected\n",
- irq_count, done/(CPU_HZ/1000),
- (done+(CPU_HZ/64))/(CPU_HZ/32));
- udelay(1000000);
- uint32_t reset_vector_now = get_reset_vector();
- if (reset_vector != reset_vector_now) {
- con_printf("*** RESET VECTOR CORRUPT: 0x%08x not 0x%08x\n",
- reset_vector_now, reset_vector);
- con_flush();
- _die();
- }
- con_puts("*** Doing reset ***\n");
- con_flush();
- reset();
- }
|