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 puts("SPI Offset: ");
233 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
234 uart_writeuint32(spi_offs
);
242 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
243 // print out configuration parameters for QSPI
244 volatile uint32_t *qspi_cfg
= (uint32_t*)SPI_FCTRL_BASE
;
245 for (int k
=0; k
< 2; k
++) {
246 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
250 uart_writeuint32(tmp
);
255 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
256 volatile uint32_t *qspi
= (uint32_t*)SPI_FLASH_BASE
+0x900000;
257 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
258 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
259 // tmp = readl((unsigned long)&(qspi[0]));
260 for (int i
=0;i
<10;i
++) {
261 tmp
= readl((unsigned long)&(qspi
[i
]));
262 uart_writeuint32(tmp
);
264 if ((i
& 0x7) == 0x7) puts("\r\n");
268 // speed up the QSPI to at least a sane level
269 crank_up_qspi_level1();
270 // run at saner level
271 host_spi_flash_init();
273 puts("SPI Offset: ");
274 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
275 uart_writeuint32(spi_offs
);
279 for (i=0;i<256;i++) {
280 tmp = readb((unsigned long)&(qspi_bytes[i]));
281 uart_writeuint32(tmp);
288 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
290 uart_writeuint32(tmp
);
294 unsigned char c
= getchar();
296 if (c
== 13) { // if CR send LF
299 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
301 uart_writeuint32(1<<i
);
303 uart_writeuint32(tmp
);
314 volatile uint32_t *hyperram
= (uint32_t*)0x00000000; // at 0x0 for arty
315 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
316 tmp
= readl((unsigned long)&(hyperram
[0]));
319 unsigned char c
= getchar();
321 if (c
== 13) { // if CR send LF
324 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
325 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
327 uart_writeuint32(1<<i
);
329 uart_writeuint32(tmp
);
338 // init DRAM only if SYSCON says it exists (duh)
339 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
341 puts("DRAM init... ");
345 struct gramProfile profile
= {
347 0xb20, 0x806, 0x200, 0x0
354 struct gramProfile profile
= {
356 0x0320, 0x0006, 0x0200, 0x0000
362 struct gramProfile profile2
;
363 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
364 (void*)DRAM_CTRL_BASE
,
365 (void*)DRAM_INIT_BASE
);
368 puts("MR profile: ");
369 uart_writeuint32(profile
.mode_registers
[0]);
371 uart_writeuint32(profile
.mode_registers
[1]);
373 uart_writeuint32(profile
.mode_registers
[2]);
375 uart_writeuint32(profile
.mode_registers
[3]);
379 // Early read test for WB access sim
380 //uart_writeuint32(*ram);
384 for (size_t i
= 0; i
< 8; i
++) {
385 profile2
.rdly_p0
= i
;
386 gram_load_calibration(&ctx
, &profile2
);
387 gram_reset_burstdet(&ctx
);
389 for (size_t j
= 0; j
< 128; j
++) {
390 tmp
= readl((unsigned long)&(ram
[i
]));
392 if (gram_read_burstdet(&ctx
, 0)) {
401 for (size_t i
= 0; i
< 8; i
++) {
402 profile2
.rdly_p1
= i
;
403 gram_load_calibration(&ctx
, &profile2
);
404 gram_reset_burstdet(&ctx
);
405 for (size_t j
= 0; j
< 128; j
++) {
406 tmp
= readl((unsigned long)&(ram
[i
]));
408 if (gram_read_burstdet(&ctx
, 1)) {
416 puts("Auto calibrating... ");
417 res
= gram_generate_calibration(&ctx
, &profile2
);
418 if (res
!= GRAM_ERR_NONE
) {
420 gram_load_calibration(&ctx
, &profile
);
422 gram_load_calibration(&ctx
, &profile2
);
426 puts("Auto calibration profile:");
428 uart_writeuint32(profile2
.rdly_p0
);
430 uart_writeuint32(profile2
.rdly_p1
);
434 puts("Reloading built-in calibration profile...");
435 gram_load_calibration(&ctx
, &profile
);
437 puts("DRAM test... \n");
438 for (size_t i
= 0; i
< kNumIterations
; i
++) {
439 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
443 for (int dly
= 0; dly
< 8; dly
++) {
445 profile2
.rdly_p0
= dly
;
446 profile2
.rdly_p1
= dly
;
448 uart_writeuint32(profile2
.rdly_p0
);
450 uart_writeuint32(profile2
.rdly_p1
);
451 gram_load_calibration(&ctx
, &profile2
);
452 for (size_t i
= 0; i
< kNumIterations
; i
++) {
453 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
455 uart_writeuint32((unsigned long)(&ram
[i
]));
457 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
462 puts("Test canceled (more than 10 errors)\n");
470 for (size_t i
= 0; i
< kNumIterations
; i
++) {
471 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
473 uart_writeuint32((unsigned long)(&ram
[i
]));
475 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
480 puts("Test canceled (more than 10 errors)\n");
489 #if 0 // ooo, annoying: won't work. no idea why
490 // temporary hard-hack: boot directly from QSPI. really
491 // should do something like detect at least... something
492 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
))
494 // jump to absolute address
495 mtspr(8, SPI_FLASH_BASE
); // move address to LR
496 __asm__
volatile("blr");
501 // memcpy from SPI Flash then boot
502 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
506 puts("ELF @ QSPI\n");
507 // identify ELF, copy if present, and get the start address
508 unsigned long faddr = copy_flash(spi_offs,
511 // jump to absolute address
512 mtspr(8, faddr); // move address to LR
513 __asm__ volatile("blr");
515 // works with head.S which copies r3 into ctr then does bctr
520 // another terrible hack: copy from flash at offset 0x600000
521 // a block of size 0x600000 into mem address 0x600000, then
522 // jump to it. this allows a dtb image to be executed
524 volatile uint32_t *mem
= (uint32_t*)0x1000000;
525 fl_read(mem
, // destination in RAM
526 0x600000, // offset into QSPI
527 0x8000); // length - shorter (testing) 0x8000);
528 //0x1000000); // length
530 for (int i
=0;i
<256;i
++) {
531 tmp
= readl((unsigned long)&(mem
[i
]));
532 uart_writeuint32(tmp
);
534 if ((i
& 0x7) == 0x7) puts("\r\n");
537 mtspr(8, 0x1000000); // move address to LR
538 __asm__
volatile("blr");