5 #include "microwatt_soc.h"
14 #define ORANGECRAB_MODE_REGISTERS 0x0320, 0x0002, 0x0200, 0x0000
16 static inline void mtspr(int sprnum
, unsigned long val
)
18 __asm__
volatile("mtspr %0,%1" : : "i" (sprnum
), "r" (val
));
21 static inline uint32_t read32(const void *addr
)
23 return *(volatile uint32_t *)addr
;
26 static inline void write32(void *addr
, uint32_t value
)
28 *(volatile uint32_t *)addr
= value
;
38 uint32_t zero0
; // reserved
39 uint32_t zero1
; // reserved
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
= (uint8_t*)(&val
);
51 for (i
= 0; i
< 4; i
++) {
52 putchar(lut
[(val_arr
[3-i
] >> 4) & 0xF]);
53 putchar(lut
[val_arr
[3-i
] & 0xF]);
57 void memcpy(void *dest
, void *src
, size_t n
) {
59 //cast src and dest to char*
60 char *src_char
= (char *)src
;
61 char *dest_char
= (char *)dest
;
64 if ((i
% 4096) == 0) {
70 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
75 void memcpy4(void *dest
, void *src
, size_t n
) {
77 //cast src and dest to char*
78 uint32_t *src_char
= (uint32_t *)src
;
79 uint32_t *dest_char
= (uint32_t *)dest
;
80 for (i
=0; i
<n
/4; i
++) {
82 if ((i
% 4096) == 0) {
88 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
97 extern void crank_up_qspi_level1(void);
98 extern int host_spi_flash_init(void);
100 static bool fl_read(void *dst
, uint32_t offset
, uint32_t size
)
103 memcpy(d
, (void *)(unsigned long)(SPI_FLASH_BASE
+ offset
), size
);
107 static unsigned long copy_flash(unsigned int offset
, unsigned int dst_offs
)
111 unsigned int i
, poff
, size
, off
;
114 puts("Trying flash...\r\n");
115 if (!fl_read(&ehdr
, offset
, sizeof(ehdr
)))
117 if (!IS_ELF(ehdr
) || ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) {
118 puts("Doesn't look like an elf64\r\n");
121 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
||
122 ehdr
.e_machine
!= EM_PPC64
) {
123 puts("Not a ppc64le binary\r\n");
127 poff
= offset
+ ehdr
.e_phoff
;
128 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
129 if (!fl_read(&ph
, poff
, sizeof(ph
)))
131 if (ph
.p_type
!= PT_LOAD
)
134 /* XXX Add bound checking ! */
136 addr
= (void *)ph
.p_vaddr
;
137 off
= offset
+ ph
.p_offset
;
138 //printf("Copy segment %d (0x%x bytes) to %p\n", i, size, addr);
139 puts("Copy segment ");
142 uart_writeuint32(size
);
144 uart_writeuint32((uint32_t)(unsigned long)addr
);
146 fl_read(addr
+dst_offs
, off
, size
);
147 poff
+= ehdr
.e_phentsize
;
150 puts("Booting from DRAM at");
151 uart_writeuint32((unsigned int)(dst_offs
+ehdr
.e_entry
));
154 puts("Dump DRAM\r\n");
155 for (i
= 0; i
< 64; i
++) {
156 uart_writeuint32(readl(dst_offs
+ehdr
.e_entry
+(i
*4)));
158 if ((i
& 7) == 7) puts("\r\n");
162 //flush_cpu_icache();
163 return dst_offs
+ehdr
.e_entry
;
166 for (i
= 0; i
< 8; i
++) {
167 uart_writeuint32(ehdr
.e_ident
[i
]);
176 // Defining gram_[read|write] allows a trace of all register
177 // accesses to be dumped to console for debugging purposes.
178 // To use, define GRAM_RW_FUNC in gram.h
179 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
183 uart_writeuint32((unsigned long)addr
);
184 dword
= readl((unsigned long)addr
);
186 uart_writeuint32((unsigned long)dword
);
192 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
193 puts("gram_write: ");
194 uart_writeuint32((unsigned long)addr
);
196 uart_writeuint32((unsigned long)value
);
197 writel(value
, (unsigned long)addr
);
204 const int kNumIterations
= 14;
205 int res
, failcnt
= 0;
207 unsigned long ftr
, spi_offs
=0x0;
208 volatile uint32_t *ram
= (uint32_t*)MEMORY_BASE
;
211 //puts("Firmware launched...\n");
214 puts(" Soc signature: ");
215 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
216 uart_writeuint32(tmp
);
217 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
+4);
218 uart_writeuint32(tmp
);
219 puts(" Soc features: ");
220 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
221 if (ftr
& SYS_REG_INFO_HAS_UART
)
223 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
225 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
227 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
229 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
233 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
234 puts("SPI Offset: ");
235 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
236 uart_writeuint32(spi_offs
);
244 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
245 // print out configuration parameters for QSPI
246 volatile uint32_t *qspi_cfg
= (uint32_t*)SPI_FCTRL_BASE
;
247 for (int k
=0; k
< 2; k
++) {
248 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
252 uart_writeuint32(tmp
);
257 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
258 volatile uint32_t *qspi
= (uint32_t*)SPI_FLASH_BASE
+0x900000;
259 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
260 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
261 // tmp = readl((unsigned long)&(qspi[0]));
262 for (int i
=0;i
<10;i
++) {
263 tmp
= readl((unsigned long)&(qspi
[i
]));
264 uart_writeuint32(tmp
);
266 if ((i
& 0x7) == 0x7) puts("\r\n");
270 // speed up the QSPI to at least a sane level
271 crank_up_qspi_level1();
272 // run at saner level
273 host_spi_flash_init();
275 puts("SPI Offset: ");
276 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
277 uart_writeuint32(spi_offs
);
281 for (i=0;i<256;i++) {
282 tmp = readb((unsigned long)&(qspi_bytes[i]));
283 uart_writeuint32(tmp);
290 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
292 uart_writeuint32(tmp
);
296 unsigned char c
= getchar();
298 if (c
== 13) { // if CR send LF
301 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
303 uart_writeuint32(1<<i
);
305 uart_writeuint32(tmp
);
316 volatile uint32_t *hyperram
= (uint32_t*)0x00000000; // at 0x0 for arty
317 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
318 tmp
= readl((unsigned long)&(hyperram
[0]));
321 unsigned char c
= getchar();
323 if (c
== 13) { // if CR send LF
326 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
327 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
329 uart_writeuint32(1<<i
);
331 uart_writeuint32(tmp
);
340 // init DRAM only if SYSCON says it exists (duh)
341 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
343 puts("DRAM init... ");
347 struct gramProfile profile
= {
349 0xb20, 0x806, 0x200, 0x0
356 struct gramProfile profile
= {
358 0x0320, 0x0006, 0x0200, 0x0000
364 struct gramProfile profile2
;
365 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
366 (void*)DRAM_CTRL_BASE
,
367 (void*)DRAM_INIT_BASE
);
370 puts("MR profile: ");
371 uart_writeuint32(profile
.mode_registers
[0]);
373 uart_writeuint32(profile
.mode_registers
[1]);
375 uart_writeuint32(profile
.mode_registers
[2]);
377 uart_writeuint32(profile
.mode_registers
[3]);
381 // Early read test for WB access sim
382 //uart_writeuint32(*ram);
386 for (size_t i
= 0; i
< 8; i
++) {
387 profile2
.rdly_p0
= i
;
388 gram_load_calibration(&ctx
, &profile2
);
389 gram_reset_burstdet(&ctx
);
391 for (size_t j
= 0; j
< 128; j
++) {
392 tmp
= readl((unsigned long)&(ram
[i
]));
394 if (gram_read_burstdet(&ctx
, 0)) {
403 for (size_t i
= 0; i
< 8; i
++) {
404 profile2
.rdly_p1
= i
;
405 gram_load_calibration(&ctx
, &profile2
);
406 gram_reset_burstdet(&ctx
);
407 for (size_t j
= 0; j
< 128; j
++) {
408 tmp
= readl((unsigned long)&(ram
[i
]));
410 if (gram_read_burstdet(&ctx
, 1)) {
418 puts("Auto calibrating... ");
419 res
= gram_generate_calibration(&ctx
, &profile2
);
420 if (res
!= GRAM_ERR_NONE
) {
422 gram_load_calibration(&ctx
, &profile
);
424 gram_load_calibration(&ctx
, &profile2
);
428 puts("Auto calibration profile:");
430 uart_writeuint32(profile2
.rdly_p0
);
432 uart_writeuint32(profile2
.rdly_p1
);
436 puts("Reloading built-in calibration profile...");
437 gram_load_calibration(&ctx
, &profile
);
439 puts("DRAM test... \n");
440 for (size_t i
= 0; i
< kNumIterations
; i
++) {
441 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
445 for (int dly
= 0; dly
< 8; dly
++) {
447 profile2
.rdly_p0
= dly
;
448 profile2
.rdly_p1
= dly
;
450 uart_writeuint32(profile2
.rdly_p0
);
452 uart_writeuint32(profile2
.rdly_p1
);
453 gram_load_calibration(&ctx
, &profile2
);
454 for (size_t i
= 0; i
< kNumIterations
; i
++) {
455 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
457 uart_writeuint32((unsigned long)(&ram
[i
]));
459 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
464 puts("Test canceled (more than 10 errors)\n");
472 for (size_t i
= 0; i
< kNumIterations
; i
++) {
473 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
475 uart_writeuint32((unsigned long)(&ram
[i
]));
477 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
482 puts("Test canceled (more than 10 errors)\n");
491 #if 0 // ooo, annoying: won't work. no idea why
492 // temporary hard-hack: boot directly from QSPI. really
493 // should do something like detect at least... something
494 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
))
496 // jump to absolute address
497 mtspr(8, SPI_FLASH_BASE
); // move address to LR
498 __asm__
volatile("blr");
503 // memcpy from SPI Flash then boot
504 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
508 puts("ELF @ QSPI\n");
509 // identify ELF, copy if present, and get the start address
510 unsigned long faddr = copy_flash(spi_offs,
513 // jump to absolute address
514 mtspr(8, faddr); // move address to LR
515 __asm__ volatile("blr");
517 // works with head.S which copies r3 into ctr then does bctr
522 // another terrible hack: copy from flash at offset 0x600000
523 // a block of size 0x600000 into mem address 0x600000, then
524 // jump to it. this allows a dtb image to be executed
526 volatile uint32_t *mem
= (uint32_t*)0x1000000;
527 fl_read(mem
, // destination in RAM
528 0x600000, // offset into QSPI
529 0x8000); // length - shorter (testing) 0x8000);
530 //0x1000000); // length
532 for (int i
=0;i
<256;i
++) {
533 tmp
= readl((unsigned long)&(mem
[i
]));
534 uart_writeuint32(tmp
);
536 if ((i
& 0x7) == 0x7) puts("\r\n");
539 mtspr(8, 0x1000000); // move address to LR
540 __asm__
volatile("blr");