020d9dc8cb90a53ae07aa679654a232d79233311
[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 #include "elf64.h"
13
14 static inline uint32_t read32(const void *addr)
15 {
16 return *(volatile uint32_t *)addr;
17 }
18
19 static inline void write32(void *addr, uint32_t value)
20 {
21 *(volatile uint32_t *)addr = value;
22 }
23
24 struct uart_regs {
25 uint32_t divisor;
26 uint32_t rx_data;
27 uint32_t rx_rdy;
28 uint32_t rx_err;
29 uint32_t tx_data;
30 uint32_t tx_rdy;
31 uint32_t zero0; // reserved
32 uint32_t zero1; // reserved
33 uint32_t ev_status;
34 uint32_t ev_pending;
35 uint32_t ev_enable;
36 };
37
38 void memcpy(void *dest, void *src, size_t n) {
39 int i;
40 //cast src and dest to char*
41 char *src_char = (char *)src;
42 char *dest_char = (char *)dest;
43 for (i=0; i<n; i++)
44 dest_char[i] = src_char[i]; //copy contents byte by byte
45 }
46
47 void uart_writeuint32(uint32_t val) {
48 const char lut[] = { '0', '1', '2', '3', '4', '5', '6', '7',
49 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
50 uint8_t *val_arr = (uint8_t*)(&val);
51 size_t i;
52
53 for (i = 0; i < 4; i++) {
54 putchar(lut[(val_arr[3-i] >> 4) & 0xF]);
55 putchar(lut[val_arr[3-i] & 0xF]);
56 }
57 }
58
59 void isr(void) {
60
61 }
62
63 static bool fl_read(void *dst, uint32_t offset, uint32_t size)
64 {
65 uint8_t *d = dst;
66 memcpy(d, (void *)(unsigned long)(SPI_FLASH_BASE + offset), size);
67 return true;
68 }
69
70 static unsigned long copy_flash(unsigned int offset)
71 {
72 Elf64_Ehdr ehdr;
73 Elf64_Phdr ph;
74 unsigned int i, poff, size, off;
75 void *addr;
76
77 puts("Trying flash...\r\n");
78 if (!fl_read(&ehdr, offset, sizeof(ehdr)))
79 return -1ul;
80 if (!IS_ELF(ehdr) || ehdr.e_ident[EI_CLASS] != ELFCLASS64) {
81 puts("Doesn't look like an elf64\r\n");
82 goto dump;
83 }
84 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB ||
85 ehdr.e_machine != EM_PPC64) {
86 puts("Not a ppc64le binary\r\n");
87 goto dump;
88 }
89
90 poff = offset + ehdr.e_phoff;
91 for (i = 0; i < ehdr.e_phnum; i++) {
92 if (!fl_read(&ph, poff, sizeof(ph)))
93 goto dump;
94 if (ph.p_type != PT_LOAD)
95 continue;
96
97 /* XXX Add bound checking ! */
98 size = ph.p_filesz;
99 addr = (void *)ph.p_vaddr;
100 off = offset + ph.p_offset;
101 //printf("Copy segment %d (0x%x bytes) to %p\n", i, size, addr);
102 puts("Copy segment ");
103 uart_writeuint32(i);
104 puts(" size ");
105 uart_writeuint32(size);
106 puts(" addr ");
107 uart_writeuint32((uint32_t)addr);
108 puts("\r\n");
109 fl_read(addr, off, size);
110 poff += ehdr.e_phentsize;
111 }
112
113 puts("Booting from DRAM at");
114 uart_writeuint32((unsigned int)ehdr.e_entry);
115 //flush_cpu_icache();
116 return ehdr.e_entry;
117 dump:
118 puts("HDR: \r\n");
119 for (i = 0; i < 8; i++) {
120 uart_writeuint32(ehdr.e_ident[i]);
121 puts("\r\n");
122 }
123
124 return -1ul;
125 }
126
127
128 // XXX
129 // Defining gram_[read|write] allows a trace of all register
130 // accesses to be dumped to console for debugging purposes.
131 // To use, define GRAM_RW_FUNC in gram.h
132 uint32_t gram_read(const struct gramCtx *ctx, void *addr) {
133 uint32_t dword;
134
135 puts("gram_read: ");
136 uart_writeuint32((unsigned long)addr);
137 dword = readl((unsigned long)addr);
138 puts(": ");
139 uart_writeuint32((unsigned long)dword);
140 puts("\n");
141
142 return dword;
143 }
144
145 int gram_write(const struct gramCtx *ctx, void *addr, uint32_t value) {
146 puts("gram_write: ");
147 uart_writeuint32((unsigned long)addr);
148 puts(": ");
149 uart_writeuint32((unsigned long)value);
150 writel(value, (unsigned long)addr);
151 puts("\n");
152
153 return 0;
154 }
155
156 int main(void) {
157 const int kNumIterations = 14;
158 int res, failcnt = 0;
159 uint32_t tmp;
160 unsigned long ftr, spi_offs=0x0;
161 volatile uint32_t *ram = (uint32_t*)MEMORY_BASE;
162
163 console_init();
164 //puts("Firmware launched...\n");
165
166 #if 1
167 puts(" Soc signature: ");
168 tmp = readl(SYSCON_BASE + SYS_REG_SIGNATURE);
169 uart_writeuint32(tmp);
170 puts(" Soc features: ");
171 ftr = readl(SYSCON_BASE + SYS_REG_INFO);
172 if (ftr & SYS_REG_INFO_HAS_UART)
173 puts("UART ");
174 if (ftr & SYS_REG_INFO_HAS_DRAM)
175 puts("DRAM ");
176 if (ftr & SYS_REG_INFO_HAS_BRAM)
177 puts("BRAM ");
178 if (ftr & SYS_REG_INFO_HAS_SPI_FLASH)
179 puts("SPIFLASH ");
180 if (ftr & SYS_REG_INFO_HAS_LITEETH)
181 puts("ETHERNET ");
182 puts("\r\n");
183
184 if (ftr & SYS_REG_INFO_HAS_SPI_FLASH) {
185 puts("SPI Offset: ");
186 spi_offs = readl(SYSCON_BASE + SYS_REG_SPI_INFO);
187 uart_writeuint32(spi_offs);
188 puts("\r\n");
189 }
190
191 #endif
192
193 #if 0
194 #if 1
195 // print out configuration parameters for QSPI
196 volatile uint32_t *qspi_cfg = (uint32_t*)0xc0003000;
197 for (int k=0; k < 2; k++) {
198 tmp = readl((unsigned long)&(qspi_cfg[k]));
199 //puts("cfg");
200 //uart_writeuint32(k);
201 //puts(" ");
202 //uart_writeuint32(tmp);
203 //puts("\n");
204 }
205 #endif
206 volatile uint32_t *qspi = (uint32_t*)spi_mem;
207 volatile uint8_t *qspi_bytes = (uint8_t*)spi_mem;
208 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
209 // tmp = readl((unsigned long)&(qspi[0]));
210 for (i=0;i<1000;i++) {
211 tmp = readl((unsigned long)&(qspi[i]));
212 uart_writeuint32(tmp);
213 puts(" ");
214 }
215 putchar(10);
216 putchar(10);
217 for (i=0;i<1000;i++) {
218 tmp = readb((unsigned long)&(qspi_bytes[i]));
219 uart_writeuint32(tmp);
220 puts(" ");
221 }
222 #if 1
223 while (1) {
224 // quick read
225 tmp = readl((unsigned long)&(qspi[0x1000/4]));
226 puts("read 0x1000");
227 uart_writeuint32(tmp);
228 putchar(10);
229 }
230 #endif
231 while (1) {
232 unsigned char c = getchar();
233 putchar(c);
234 if (c == 13) { // if CR send LF
235
236 // quick read
237 tmp = readl((unsigned long)&(qspi[1<<i]));
238 puts("read ");
239 uart_writeuint32(1<<i);
240 puts(" ");
241 uart_writeuint32(tmp);
242 putchar(10);
243 i++;
244 }
245 }
246
247 return 0;
248 #endif
249 #if 0
250 volatile uint32_t *hyperram = (uint32_t*)0xa0000000;
251 writel(0xDEAF0123, (unsigned long)&(hyperram[0]));
252 tmp = readl((unsigned long)&(hyperram[0]));
253 while (1) {
254 unsigned char c = getchar();
255 putchar(c);
256 if (c == 13) { // if CR send LF
257
258 // quick write/read
259 writel(0xDEAF0123+i, (unsigned long)&(hyperram[1<<i]));
260 tmp = readl((unsigned long)&(hyperram[1<<i]));
261 puts("read ");
262 uart_writeuint32(1<<i);
263 puts(" ");
264 uart_writeuint32(tmp);
265 putchar(10);
266 i++;
267 }
268 }
269
270 return 0;
271 #endif
272
273 for (int persistence=0; persistence < 1000; persistence++) {
274 puts("DRAM init... ");
275
276 struct gramCtx ctx;
277 #if 1
278 struct gramProfile profile = {
279 .mode_registers = {
280 0xb20, 0x806, 0x200, 0x0
281 },
282 .rdly_p0 = 2,
283 .rdly_p1 = 2,
284 };
285 #endif
286 #if 0
287 struct gramProfile profile = {
288 .mode_registers = {
289 0x0320, 0x0006, 0x0200, 0x0000
290 },
291 .rdly_p0 = 1,
292 .rdly_p1 = 1,
293 };
294 #endif
295 struct gramProfile profile2;
296 gram_init(&ctx, &profile, (void*)MEMORY_BASE,
297 (void*)DRAM_CTRL_BASE,
298 (void*)DRAM_INIT_BASE);
299 puts("done\n");
300
301 puts("MR profile: ");
302 uart_writeuint32(profile.mode_registers[0]);
303 puts(" ");
304 uart_writeuint32(profile.mode_registers[1]);
305 puts(" ");
306 uart_writeuint32(profile.mode_registers[2]);
307 puts(" ");
308 uart_writeuint32(profile.mode_registers[3]);
309 puts("\n");
310
311 // FIXME
312 // Early read test for WB access sim
313 //uart_writeuint32(*ram);
314
315 #if 1
316 puts("Rdly\np0: ");
317 for (size_t i = 0; i < 8; i++) {
318 profile2.rdly_p0 = i;
319 gram_load_calibration(&ctx, &profile2);
320 gram_reset_burstdet(&ctx);
321
322 for (size_t j = 0; j < 128; j++) {
323 tmp = readl((unsigned long)&(ram[i]));
324 }
325 if (gram_read_burstdet(&ctx, 0)) {
326 puts("1");
327 } else {
328 puts("0");
329 }
330 }
331 puts("\n");
332
333 puts("Rdly\np1: ");
334 for (size_t i = 0; i < 8; i++) {
335 profile2.rdly_p1 = i;
336 gram_load_calibration(&ctx, &profile2);
337 gram_reset_burstdet(&ctx);
338 for (size_t j = 0; j < 128; j++) {
339 tmp = readl((unsigned long)&(ram[i]));
340 }
341 if (gram_read_burstdet(&ctx, 1)) {
342 puts("1");
343 } else {
344 puts("0");
345 }
346 }
347 puts("\n");
348
349 puts("Auto calibrating... ");
350 res = gram_generate_calibration(&ctx, &profile2);
351 if (res != GRAM_ERR_NONE) {
352 puts("failed\n");
353 gram_load_calibration(&ctx, &profile);
354 } else {
355 gram_load_calibration(&ctx, &profile2);
356 }
357 puts("done\n");
358
359 puts("Auto calibration profile:");
360 puts("p0 rdly:");
361 uart_writeuint32(profile2.rdly_p0);
362 puts(" p1 rdly:");
363 uart_writeuint32(profile2.rdly_p1);
364 puts("\n");
365 #endif
366
367 puts("Reloading built-in calibration profile...");
368 gram_load_calibration(&ctx, &profile);
369
370 puts("DRAM test... \n");
371 for (size_t i = 0; i < kNumIterations; i++) {
372 writel(0xDEAF0000 | i*4, (unsigned long)&(ram[i]));
373 }
374
375 #if 0
376 for (int dly = 0; dly < 8; dly++) {
377 failcnt = 0;
378 profile2.rdly_p0 = dly;
379 profile2.rdly_p1 = dly;
380 puts("p0 rdly:");
381 uart_writeuint32(profile2.rdly_p0);
382 puts(" p1 rdly:");
383 uart_writeuint32(profile2.rdly_p1);
384 gram_load_calibration(&ctx, &profile2);
385 for (size_t i = 0; i < kNumIterations; i++) {
386 if (readl((unsigned long)&(ram[i])) != (0xDEAF0000 | i*4)) {
387 puts("fail : *(0x");
388 uart_writeuint32((unsigned long)(&ram[i]));
389 puts(") = ");
390 uart_writeuint32(readl((unsigned long)&(ram[i])));
391 puts("\n");
392 failcnt++;
393
394 if (failcnt > 10) {
395 puts("Test canceled (more than 10 errors)\n");
396 break;
397 }
398 }
399 }
400 }
401 #else
402 failcnt = 0;
403 for (size_t i = 0; i < kNumIterations; i++) {
404 if (readl((unsigned long)&(ram[i])) != (0xDEAF0000 | i*4)) {
405 puts("fail : *(0x");
406 uart_writeuint32((unsigned long)(&ram[i]));
407 puts(") = ");
408 uart_writeuint32(readl((unsigned long)&(ram[i])));
409 puts("\n");
410 failcnt++;
411
412 if (failcnt > 10) {
413 puts("Test canceled (more than 10 errors)\n");
414 break;
415 }
416 }
417 }
418 if (failcnt == 0) { // fiinally...
419 break;
420 }
421 }
422 #endif
423 puts("done\n");
424
425 // memcpy from SPI Flash to SDRAM then boot
426 if ((ftr & SYS_REG_INFO_HAS_SPI_FLASH) &&
427 (ftr & SYS_REG_INFO_HAS_DRAM) &&
428 (failcnt == 0))
429 {
430 // identify ELF, copy if present, and get the start address
431 unsigned long faddr = copy_flash(spi_offs);
432 if (faddr != -1ul) {
433 // jump to absolute address
434 return faddr;
435 }
436 }
437
438 return 0;
439 }
440