5 #include "microwatt_soc.h"
14 static inline void mtspr(int sprnum
, unsigned long val
)
16 __asm__
volatile("mtspr %0,%1" : : "i" (sprnum
), "r" (val
));
19 static inline uint32_t read32(const void *addr
)
21 return *(volatile uint32_t *)addr
;
24 static inline void write32(void *addr
, uint32_t value
)
26 *(volatile uint32_t *)addr
= value
;
36 uint32_t zero0
; // reserved
37 uint32_t zero1
; // reserved
43 void uart_writeuint32(uint32_t val
) {
44 const char lut
[] = { '0', '1', '2', '3', '4', '5', '6', '7',
45 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
46 uint8_t *val_arr
= (uint8_t*)(&val
);
49 for (i
= 0; i
< 4; i
++) {
50 putchar(lut
[(val_arr
[3-i
] >> 4) & 0xF]);
51 putchar(lut
[val_arr
[3-i
] & 0xF]);
55 void memcpy(void *dest
, void *src
, size_t n
) {
57 //cast src and dest to char*
58 char *src_char
= (char *)src
;
59 char *dest_char
= (char *)dest
;
62 if ((i
% 4096) == 0) {
68 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
73 void memcpy4(void *dest
, void *src
, size_t n
) {
75 //cast src and dest to char*
76 uint32_t *src_char
= (uint32_t *)src
;
77 uint32_t *dest_char
= (uint32_t *)dest
;
78 for (i
=0; i
<n
/4; i
++) {
80 if ((i
% 4096) == 0) {
86 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
95 extern void crank_up_qspi_level1(void);
96 extern int host_spi_flash_init(void);
98 static bool fl_read(void *dst
, uint32_t offset
, uint32_t size
)
101 memcpy(d
, (void *)(unsigned long)(SPI_FLASH_BASE
+ offset
), size
);
105 static unsigned long copy_flash(unsigned int offset
, unsigned int dst_offs
)
109 unsigned int i
, poff
, size
, off
;
112 puts("Trying flash...\r\n");
113 if (!fl_read(&ehdr
, offset
, sizeof(ehdr
)))
115 if (!IS_ELF(ehdr
) || ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) {
116 puts("Doesn't look like an elf64\r\n");
119 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
||
120 ehdr
.e_machine
!= EM_PPC64
) {
121 puts("Not a ppc64le binary\r\n");
125 poff
= offset
+ ehdr
.e_phoff
;
126 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
127 if (!fl_read(&ph
, poff
, sizeof(ph
)))
129 if (ph
.p_type
!= PT_LOAD
)
132 /* XXX Add bound checking ! */
134 addr
= (void *)ph
.p_vaddr
;
135 off
= offset
+ ph
.p_offset
;
136 //printf("Copy segment %d (0x%x bytes) to %p\n", i, size, addr);
137 puts("Copy segment ");
140 uart_writeuint32(size
);
142 uart_writeuint32((uint32_t)(unsigned long)addr
);
144 fl_read(addr
+dst_offs
, off
, size
);
145 poff
+= ehdr
.e_phentsize
;
148 puts("Booting from DRAM at");
149 uart_writeuint32((unsigned int)(dst_offs
+ehdr
.e_entry
));
152 puts("Dump DRAM\r\n");
153 for (i
= 0; i
< 64; i
++) {
154 uart_writeuint32(readl(dst_offs
+ehdr
.e_entry
+(i
*4)));
156 if ((i
& 7) == 7) puts("\r\n");
160 //flush_cpu_icache();
161 return dst_offs
+ehdr
.e_entry
;
164 for (i
= 0; i
< 8; i
++) {
165 uart_writeuint32(ehdr
.e_ident
[i
]);
174 // Defining gram_[read|write] allows a trace of all register
175 // accesses to be dumped to console for debugging purposes.
176 // To use, define GRAM_RW_FUNC in gram.h
177 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
181 uart_writeuint32((unsigned long)addr
);
182 dword
= readl((unsigned long)addr
);
184 uart_writeuint32((unsigned long)dword
);
190 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
191 puts("gram_write: ");
192 uart_writeuint32((unsigned long)addr
);
194 uart_writeuint32((unsigned long)value
);
195 writel(value
, (unsigned long)addr
);
202 const int kNumIterations
= 14;
203 int res
, failcnt
= 0;
205 unsigned long ftr
, spi_offs
=0x0;
206 volatile uint32_t *ram
= (uint32_t*)MEMORY_BASE
;
209 //puts("Firmware launched...\n");
212 puts(" Soc signature: ");
213 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
214 uart_writeuint32(tmp
);
215 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
+4);
216 uart_writeuint32(tmp
);
217 puts(" Soc features: ");
218 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
219 if (ftr
& SYS_REG_INFO_HAS_UART
)
221 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
223 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
225 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
227 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
231 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
232 // speed up the QSPI to at least a sane level
233 crank_up_qspi_level1();
234 // run at saner level
235 host_spi_flash_init();
237 puts("SPI Offset: ");
238 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
239 uart_writeuint32(spi_offs
);
247 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
248 // print out configuration parameters for QSPI
249 volatile uint32_t *qspi_cfg
= (uint32_t*)SPI_FCTRL_BASE
;
250 for (int k
=0; k
< 2; k
++) {
251 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
255 uart_writeuint32(tmp
);
260 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
261 volatile uint32_t *qspi
= (uint32_t*)SPI_FLASH_BASE
+0x900000;
262 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
263 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
264 // tmp = readl((unsigned long)&(qspi[0]));
265 for (int i
=0;i
<10;i
++) {
266 tmp
= readl((unsigned long)&(qspi
[i
]));
267 uart_writeuint32(tmp
);
269 if ((i
& 0x7) == 0x7) puts("\r\n");
273 for (i=0;i<256;i++) {
274 tmp = readb((unsigned long)&(qspi_bytes[i]));
275 uart_writeuint32(tmp);
282 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
284 uart_writeuint32(tmp
);
288 unsigned char c
= getchar();
290 if (c
== 13) { // if CR send LF
293 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
295 uart_writeuint32(1<<i
);
297 uart_writeuint32(tmp
);
308 volatile uint32_t *hyperram
= (uint32_t*)0x00000000; // at 0x0 for arty
309 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
310 tmp
= readl((unsigned long)&(hyperram
[0]));
313 unsigned char c
= getchar();
315 if (c
== 13) { // if CR send LF
318 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
319 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
321 uart_writeuint32(1<<i
);
323 uart_writeuint32(tmp
);
332 // init DRAM only if SYSCON says it exists (duh)
333 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
335 puts("DRAM init... ");
339 struct gramProfile profile
= {
341 0xb20, 0x806, 0x200, 0x0
348 struct gramProfile profile
= {
350 0x0320, 0x0006, 0x0200, 0x0000
356 struct gramProfile profile2
;
357 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
358 (void*)DRAM_CTRL_BASE
,
359 (void*)DRAM_INIT_BASE
);
362 puts("MR profile: ");
363 uart_writeuint32(profile
.mode_registers
[0]);
365 uart_writeuint32(profile
.mode_registers
[1]);
367 uart_writeuint32(profile
.mode_registers
[2]);
369 uart_writeuint32(profile
.mode_registers
[3]);
373 // Early read test for WB access sim
374 //uart_writeuint32(*ram);
378 for (size_t i
= 0; i
< 8; i
++) {
379 profile2
.rdly_p0
= i
;
380 gram_load_calibration(&ctx
, &profile2
);
381 gram_reset_burstdet(&ctx
);
383 for (size_t j
= 0; j
< 128; j
++) {
384 tmp
= readl((unsigned long)&(ram
[i
]));
386 if (gram_read_burstdet(&ctx
, 0)) {
395 for (size_t i
= 0; i
< 8; i
++) {
396 profile2
.rdly_p1
= i
;
397 gram_load_calibration(&ctx
, &profile2
);
398 gram_reset_burstdet(&ctx
);
399 for (size_t j
= 0; j
< 128; j
++) {
400 tmp
= readl((unsigned long)&(ram
[i
]));
402 if (gram_read_burstdet(&ctx
, 1)) {
410 puts("Auto calibrating... ");
411 res
= gram_generate_calibration(&ctx
, &profile2
);
412 if (res
!= GRAM_ERR_NONE
) {
414 gram_load_calibration(&ctx
, &profile
);
416 gram_load_calibration(&ctx
, &profile2
);
420 puts("Auto calibration profile:");
422 uart_writeuint32(profile2
.rdly_p0
);
424 uart_writeuint32(profile2
.rdly_p1
);
428 puts("Reloading built-in calibration profile...");
429 gram_load_calibration(&ctx
, &profile
);
431 puts("DRAM test... \n");
432 for (size_t i
= 0; i
< kNumIterations
; i
++) {
433 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
437 for (int dly
= 0; dly
< 8; dly
++) {
439 profile2
.rdly_p0
= dly
;
440 profile2
.rdly_p1
= dly
;
442 uart_writeuint32(profile2
.rdly_p0
);
444 uart_writeuint32(profile2
.rdly_p1
);
445 gram_load_calibration(&ctx
, &profile2
);
446 for (size_t i
= 0; i
< kNumIterations
; i
++) {
447 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
449 uart_writeuint32((unsigned long)(&ram
[i
]));
451 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
456 puts("Test canceled (more than 10 errors)\n");
464 for (size_t i
= 0; i
< kNumIterations
; i
++) {
465 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
467 uart_writeuint32((unsigned long)(&ram
[i
]));
469 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
474 puts("Test canceled (more than 10 errors)\n");
483 #if 0 // ooo, annoying: won't work. no idea why
484 // temporary hard-hack: boot directly from QSPI. really
485 // should do something like detect at least... something
486 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
))
488 // jump to absolute address
489 mtspr(8, SPI_FLASH_BASE
); // move address to LR
490 __asm__
volatile("blr");
495 // memcpy from SPI Flash then boot
496 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
500 puts("ELF @ QSPI\n");
501 // identify ELF, copy if present, and get the start address
502 unsigned long faddr = copy_flash(spi_offs,
505 // jump to absolute address
506 mtspr(8, faddr); // move address to LR
507 __asm__ volatile("blr");
509 // works with head.S which copies r3 into ctr then does bctr
514 // another terrible hack: copy from flash at offset 0x600000
515 // a block of size 0x600000 into mem address 0x600000, then
516 // jump to it. this allows a dtb image to be executed
518 volatile uint32_t *mem
= (uint32_t*)0x1000000;
519 fl_read(mem
, // destination in RAM
520 0x600000, // offset into QSPI
521 0x8000); // length - shorter (testing) 0x8000);
522 //0x1000000); // length
524 for (int i
=0;i
<256;i
++) {
525 tmp
= readl((unsigned long)&(mem
[i
]));
526 uart_writeuint32(tmp
);
528 if ((i
& 0x7) == 0x7) puts("\r\n");
531 mtspr(8, 0x1000000); // move address to LR
532 __asm__
volatile("blr");