5 #include "microwatt_soc.h"
14 static inline uint32_t read32(const void *addr
)
16 return *(volatile uint32_t *)addr
;
19 static inline void write32(void *addr
, uint32_t value
)
21 *(volatile uint32_t *)addr
= value
;
31 uint32_t zero0
; // reserved
32 uint32_t zero1
; // reserved
38 void uart_writeuint32(uint32_t val
) {
39 const char lut
[] = { '0', '1', '2', '3', '4', '5', '6', '7',
40 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
41 uint8_t *val_arr
= (uint8_t*)(&val
);
44 for (i
= 0; i
< 4; i
++) {
45 putchar(lut
[(val_arr
[3-i
] >> 4) & 0xF]);
46 putchar(lut
[val_arr
[3-i
] & 0xF]);
50 void memcpy(void *dest
, void *src
, size_t n
) {
52 //cast src and dest to char*
53 char *src_char
= (char *)src
;
54 char *dest_char
= (char *)dest
;
57 if ((i
% 1024) == 1023) {
63 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
71 // TODO: need to use this
72 // https://gitlab.raptorengineering.com/kestrel-collaboration/kestrel-litex/litex/-/blob/master/litex/soc/software/bios/boot.c#L575
73 static bool fl_read(void *dst
, uint32_t offset
, uint32_t size
)
76 memcpy(d
, (void *)(unsigned long)(SPI_FLASH_BASE
+ offset
), size
);
80 static unsigned long copy_flash(unsigned int offset
)
84 unsigned int i
, poff
, size
, off
;
87 puts("Trying flash...\r\n");
88 if (!fl_read(&ehdr
, offset
, sizeof(ehdr
)))
90 if (!IS_ELF(ehdr
) || ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) {
91 puts("Doesn't look like an elf64\r\n");
94 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
||
95 ehdr
.e_machine
!= EM_PPC64
) {
96 puts("Not a ppc64le binary\r\n");
100 poff
= offset
+ ehdr
.e_phoff
;
101 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
102 if (!fl_read(&ph
, poff
, sizeof(ph
)))
104 if (ph
.p_type
!= PT_LOAD
)
107 /* XXX Add bound checking ! */
109 addr
= (void *)ph
.p_vaddr
;
110 off
= offset
+ ph
.p_offset
;
111 //printf("Copy segment %d (0x%x bytes) to %p\n", i, size, addr);
112 puts("Copy segment ");
115 uart_writeuint32(size
);
117 uart_writeuint32((uint32_t)(unsigned long)addr
);
119 fl_read(addr
, off
, size
);
120 poff
+= ehdr
.e_phentsize
;
123 puts("Booting from DRAM at");
124 uart_writeuint32((unsigned int)ehdr
.e_entry
);
125 //flush_cpu_icache();
129 for (i
= 0; i
< 8; i
++) {
130 uart_writeuint32(ehdr
.e_ident
[i
]);
139 // Defining gram_[read|write] allows a trace of all register
140 // accesses to be dumped to console for debugging purposes.
141 // To use, define GRAM_RW_FUNC in gram.h
142 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
146 uart_writeuint32((unsigned long)addr
);
147 dword
= readl((unsigned long)addr
);
149 uart_writeuint32((unsigned long)dword
);
155 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
156 puts("gram_write: ");
157 uart_writeuint32((unsigned long)addr
);
159 uart_writeuint32((unsigned long)value
);
160 writel(value
, (unsigned long)addr
);
167 const int kNumIterations
= 14;
168 int res
, failcnt
= 0;
170 unsigned long ftr
, spi_offs
=0x0;
171 volatile uint32_t *ram
= (uint32_t*)MEMORY_BASE
;
174 //puts("Firmware launched...\n");
177 puts(" Soc signature: ");
178 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
179 uart_writeuint32(tmp
);
180 puts(" Soc features: ");
181 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
182 if (ftr
& SYS_REG_INFO_HAS_UART
)
184 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
186 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
188 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
190 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
194 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
195 puts("SPI Offset: ");
196 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
197 uart_writeuint32(spi_offs
);
205 // print out configuration parameters for QSPI
206 volatile uint32_t *qspi_cfg
= (uint32_t*)SPI_FCTRL_BASE
;
207 for (int k
=0; k
< 2; k
++) {
208 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
212 uart_writeuint32(tmp
);
216 volatile uint32_t *qspi
= (uint32_t*)spi_offs
;
217 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
218 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
219 // tmp = readl((unsigned long)&(qspi[0]));
220 for (int i
=0;i
<256;i
++) {
221 tmp
= readl((unsigned long)&(qspi
[i
]));
222 uart_writeuint32(tmp
);
224 if ((i
& 0x7) == 0x7) puts("\r\n");
228 for (i=0;i<256;i++) {
229 tmp = readb((unsigned long)&(qspi_bytes[i]));
230 uart_writeuint32(tmp);
237 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
239 uart_writeuint32(tmp
);
243 unsigned char c
= getchar();
245 if (c
== 13) { // if CR send LF
248 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
250 uart_writeuint32(1<<i
);
252 uart_writeuint32(tmp
);
262 volatile uint32_t *hyperram
= (uint32_t*)0xa0000000;
263 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
264 tmp
= readl((unsigned long)&(hyperram
[0]));
266 unsigned char c
= getchar();
268 if (c
== 13) { // if CR send LF
271 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
272 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
274 uart_writeuint32(1<<i
);
276 uart_writeuint32(tmp
);
285 for (int persistence
=0; persistence
< 1000; persistence
++) {
286 puts("DRAM init... ");
290 struct gramProfile profile
= {
292 0xb20, 0x806, 0x200, 0x0
299 struct gramProfile profile
= {
301 0x0320, 0x0006, 0x0200, 0x0000
307 struct gramProfile profile2
;
308 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
309 (void*)DRAM_CTRL_BASE
,
310 (void*)DRAM_INIT_BASE
);
313 puts("MR profile: ");
314 uart_writeuint32(profile
.mode_registers
[0]);
316 uart_writeuint32(profile
.mode_registers
[1]);
318 uart_writeuint32(profile
.mode_registers
[2]);
320 uart_writeuint32(profile
.mode_registers
[3]);
324 // Early read test for WB access sim
325 //uart_writeuint32(*ram);
329 for (size_t i
= 0; i
< 8; i
++) {
330 profile2
.rdly_p0
= i
;
331 gram_load_calibration(&ctx
, &profile2
);
332 gram_reset_burstdet(&ctx
);
334 for (size_t j
= 0; j
< 128; j
++) {
335 tmp
= readl((unsigned long)&(ram
[i
]));
337 if (gram_read_burstdet(&ctx
, 0)) {
346 for (size_t i
= 0; i
< 8; i
++) {
347 profile2
.rdly_p1
= i
;
348 gram_load_calibration(&ctx
, &profile2
);
349 gram_reset_burstdet(&ctx
);
350 for (size_t j
= 0; j
< 128; j
++) {
351 tmp
= readl((unsigned long)&(ram
[i
]));
353 if (gram_read_burstdet(&ctx
, 1)) {
361 puts("Auto calibrating... ");
362 res
= gram_generate_calibration(&ctx
, &profile2
);
363 if (res
!= GRAM_ERR_NONE
) {
365 gram_load_calibration(&ctx
, &profile
);
367 gram_load_calibration(&ctx
, &profile2
);
371 puts("Auto calibration profile:");
373 uart_writeuint32(profile2
.rdly_p0
);
375 uart_writeuint32(profile2
.rdly_p1
);
379 puts("Reloading built-in calibration profile...");
380 gram_load_calibration(&ctx
, &profile
);
382 puts("DRAM test... \n");
383 for (size_t i
= 0; i
< kNumIterations
; i
++) {
384 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
388 for (int dly
= 0; dly
< 8; dly
++) {
390 profile2
.rdly_p0
= dly
;
391 profile2
.rdly_p1
= dly
;
393 uart_writeuint32(profile2
.rdly_p0
);
395 uart_writeuint32(profile2
.rdly_p1
);
396 gram_load_calibration(&ctx
, &profile2
);
397 for (size_t i
= 0; i
< kNumIterations
; i
++) {
398 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
400 uart_writeuint32((unsigned long)(&ram
[i
]));
402 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
407 puts("Test canceled (more than 10 errors)\n");
415 for (size_t i
= 0; i
< kNumIterations
; i
++) {
416 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
418 uart_writeuint32((unsigned long)(&ram
[i
]));
420 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
425 puts("Test canceled (more than 10 errors)\n");
430 if (failcnt
== 0) { // fiinally...
437 // memcpy from SPI Flash to SDRAM then boot
438 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
439 (ftr
& SYS_REG_INFO_HAS_DRAM
) &&
442 // identify ELF, copy if present, and get the start address
443 unsigned long faddr
= copy_flash(spi_offs
);
445 // jump to absolute address