d85ae7682260037ed20be709d4a4644264f687c4
[ls2.git] / coldboot / coldboot.c
1 #include <stdint.h>
2 #include <stdbool.h>
3
4 #include "console.h"
5 #include "microwatt_soc.h"
6 #include "io.h"
7
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <gram.h>
11
12 static inline uint32_t read32(const void *addr)
13 {
14 return *(volatile uint32_t *)addr;
15 }
16
17 static inline void write32(void *addr, uint32_t value)
18 {
19 *(volatile uint32_t *)addr = value;
20 }
21
22 struct uart_regs {
23 uint32_t divisor;
24 uint32_t rx_data;
25 uint32_t rx_rdy;
26 uint32_t rx_err;
27 uint32_t tx_data;
28 uint32_t tx_rdy;
29 uint32_t zero0; // reserved
30 uint32_t zero1; // reserved
31 uint32_t ev_status;
32 uint32_t ev_pending;
33 uint32_t ev_enable;
34 };
35
36 void memcpy(void *dest, void *src, size_t n) {
37 int i;
38 //cast src and dest to char*
39 char *src_char = (char *)src;
40 char *dest_char = (char *)dest;
41 for (i=0; i<n; i++)
42 dest_char[i] = src_char[i]; //copy contents byte by byte
43 }
44
45 void uart_writeuint32(uint32_t val) {
46 const char lut[] = { '0', '1', '2', '3', '4', '5', '6', '7',
47 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
48 uint8_t *val_arr = &val;
49 size_t i;
50
51 for (i = 0; i < 4; i++) {
52 putchar(lut[(val_arr[3-i] >> 4) & 0xF]);
53 putchar(lut[val_arr[3-i] & 0xF]);
54 }
55 }
56
57 void isr(void) {
58
59 }
60
61 int main(void) {
62 const int kNumIterations = 65536;
63 int res, failcnt = 0;
64 uint32_t tmp;
65 volatile uint32_t *ram = DRAM_BASE;
66 console_init();
67 puts("Firmware launched...\n");
68
69 puts("DRAM init... ");
70 struct gramCtx ctx;
71 struct gramProfile profile = {
72 .mode_registers = {
73 0x320, 0x6, 0x200, 0x0
74 },
75 .rdly_p0 = 2,
76 .rdly_p1 = 2,
77 };
78 struct gramProfile profile2;
79 gram_init(&ctx, &profile, (void*)DRAM_BASE, //0x10000000,
80 (void*)DRAM_CTRL_BASE, //0x00009000,
81 (void*)DRAM_INIT_BASE); //0x00008000);
82 puts("done\n");
83
84 puts("Rdly\np0: ");
85 for (size_t i = 0; i < 8; i++) {
86 profile2.rdly_p0 = i;
87 gram_load_calibration(&ctx, &profile2);
88 puts("loaded ");
89 gram_reset_burstdet(&ctx);
90 puts("burstreset ");
91 for (size_t j = 0; j < 128; j++) {
92 tmp = readl((unsigned long)&ram[i]);
93 }
94 if (gram_read_burstdet(&ctx, 0)) {
95 puts("1");
96 } else {
97 puts("0");
98 }
99 }
100 puts("\n");
101
102 puts("Rdly\np1: ");
103 for (size_t i = 0; i < 8; i++) {
104 profile2.rdly_p1 = i;
105 gram_load_calibration(&ctx, &profile2);
106 puts("loaded ");
107 gram_reset_burstdet(&ctx);
108 puts("burstreset ");
109 for (size_t j = 0; j < 128; j++) {
110 tmp = readl((unsigned long)&ram[i]);
111 }
112 if (gram_read_burstdet(&ctx, 1)) {
113 puts("1");
114 } else {
115 puts("0");
116 }
117 }
118 puts("\n");
119
120 puts("Auto calibrating... ");
121 res = gram_generate_calibration(&ctx, &profile2);
122 if (res != GRAM_ERR_NONE) {
123 puts("failed\n");
124 gram_load_calibration(&ctx, &profile);
125 } else {
126 gram_load_calibration(&ctx, &profile2);
127 }
128 puts("done\n");
129
130 puts("Auto calibration profile:");
131 puts("p0 rdly:");
132 uart_writeuint32(profile2.rdly_p0);
133 puts(" p1 rdly:");
134 uart_writeuint32(profile2.rdly_p1);
135 puts("\n");
136
137 puts("DRAM test... \n");
138 for (size_t i = 0; i < kNumIterations; i++) {
139 ram[i] = 0xDEAF0000 | i*4;
140 }
141
142 for (size_t i = 0; i < kNumIterations; i++) {
143 if (ram[i] != (0xDEAF0000 | i*4)) {
144 puts("fail : *(0x");
145 uart_writeuint32(&ram[i]);
146 puts(") = ");
147 uart_writeuint32(ram[i]);
148 putchar('\n');
149 failcnt++;
150
151 if (failcnt > 10) {
152 puts("Test canceled (more than 10 errors)\n");
153 break;
154 }
155 }
156 }
157 puts("done\n");
158
159 while (1);
160
161 return 0;
162 }
163