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
% 4096) == 0) {
63 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
73 #define TERCEL_SPI_REG_SYS_PHY_CFG1 0x10
74 #define TERCEL_SPI_REG_SYS_FLASH_CFG5 0x24
75 #define TERCEL_SPI_PHY_CLOCK_DIVISOR_MASK 0xff
76 #define TERCEL_SPI_PHY_CLOCK_DIVISOR_SHIFT 0
77 #define TERCEL_SPI_FLASH_EN_MULTCYC_READ_MASK 0x1
78 #define TERCEL_SPI_FLASH_EN_MULTCYC_READ_SHIFT 0
79 static inline uint32_t read_tercel_register(uint8_t reg
)
81 return readl((unsigned long)(SPI_FCTRL_BASE
+reg
));
84 static inline void write_tercel_register(uint8_t reg
, uint32_t value
)
86 writel(value
, (unsigned long)(SPI_FCTRL_BASE
+reg
));
89 // TODO: need to use this
90 // https://gitlab.raptorengineering.com/kestrel-collaboration/kestrel-litex/litex/-/blob/master/litex/soc/software/bios/boot.c#L575
91 static bool fl_read(void *dst
, uint32_t offset
, uint32_t size
)
94 memcpy(d
, (void *)(unsigned long)(SPI_FLASH_BASE
+ offset
), size
);
98 static unsigned long copy_flash(unsigned int offset
)
102 unsigned int i
, poff
, size
, off
;
107 // Set SPI clock cycle divider to 1
109 dword
= read_tercel_register(TERCEL_SPI_REG_SYS_PHY_CFG1
);
110 dword
&= ~(TERCEL_SPI_PHY_CLOCK_DIVISOR_MASK
<<
111 TERCEL_SPI_PHY_CLOCK_DIVISOR_SHIFT
);
112 dword
|= ((1 & TERCEL_SPI_PHY_CLOCK_DIVISOR_MASK
) <<
113 TERCEL_SPI_PHY_CLOCK_DIVISOR_SHIFT
);
114 write_tercel_register(TERCEL_SPI_REG_SYS_PHY_CFG1
, dword
);
115 // Enable read merging
116 dword
= read_tercel_register(TERCEL_SPI_REG_SYS_FLASH_CFG5
);
117 dword
|= (TERCEL_SPI_FLASH_EN_MULTCYC_READ_MASK
<<
118 TERCEL_SPI_FLASH_EN_MULTCYC_READ_SHIFT
);
119 write_tercel_register(TERCEL_SPI_REG_SYS_FLASH_CFG5
, dword
);
121 puts("Trying flash...\r\n");
122 if (!fl_read(&ehdr
, offset
, sizeof(ehdr
)))
124 if (!IS_ELF(ehdr
) || ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) {
125 puts("Doesn't look like an elf64\r\n");
128 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
||
129 ehdr
.e_machine
!= EM_PPC64
) {
130 puts("Not a ppc64le binary\r\n");
134 poff
= offset
+ ehdr
.e_phoff
;
135 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
136 if (!fl_read(&ph
, poff
, sizeof(ph
)))
138 if (ph
.p_type
!= PT_LOAD
)
141 /* XXX Add bound checking ! */
143 addr
= (void *)ph
.p_vaddr
;
144 off
= offset
+ ph
.p_offset
;
145 //printf("Copy segment %d (0x%x bytes) to %p\n", i, size, addr);
146 puts("Copy segment ");
149 uart_writeuint32(size
);
151 uart_writeuint32((uint32_t)(unsigned long)addr
);
153 fl_read(addr
, off
, size
);
154 poff
+= ehdr
.e_phentsize
;
157 puts("Booting from DRAM at");
158 uart_writeuint32((unsigned int)ehdr
.e_entry
);
161 puts("Dump DRAM\r\n");
162 for (i
= 0; i
< 64; i
++) {
163 uart_writeuint32(ehdr
.e_entry
+(i
*4));
164 if ((i
& 7) == 7) puts("\r\n");
168 //flush_cpu_icache();
172 for (i
= 0; i
< 8; i
++) {
173 uart_writeuint32(ehdr
.e_ident
[i
]);
182 // Defining gram_[read|write] allows a trace of all register
183 // accesses to be dumped to console for debugging purposes.
184 // To use, define GRAM_RW_FUNC in gram.h
185 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
189 uart_writeuint32((unsigned long)addr
);
190 dword
= readl((unsigned long)addr
);
192 uart_writeuint32((unsigned long)dword
);
198 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
199 puts("gram_write: ");
200 uart_writeuint32((unsigned long)addr
);
202 uart_writeuint32((unsigned long)value
);
203 writel(value
, (unsigned long)addr
);
210 const int kNumIterations
= 14;
211 int res
, failcnt
= 0;
213 unsigned long ftr
, spi_offs
=0x0;
214 volatile uint32_t *ram
= (uint32_t*)MEMORY_BASE
;
217 //puts("Firmware launched...\n");
220 puts(" Soc signature: ");
221 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
222 uart_writeuint32(tmp
);
223 puts(" Soc features: ");
224 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
225 if (ftr
& SYS_REG_INFO_HAS_UART
)
227 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
229 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
231 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
233 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
237 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
238 puts("SPI Offset: ");
239 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
240 uart_writeuint32(spi_offs
);
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
);
259 volatile uint32_t *qspi
= (uint32_t*)spi_offs
;
260 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
261 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
262 // tmp = readl((unsigned long)&(qspi[0]));
263 for (int i
=0;i
<256;i
++) {
264 tmp
= readl((unsigned long)&(qspi
[i
]));
265 uart_writeuint32(tmp
);
267 if ((i
& 0x7) == 0x7) puts("\r\n");
271 for (i=0;i<256;i++) {
272 tmp = readb((unsigned long)&(qspi_bytes[i]));
273 uart_writeuint32(tmp);
280 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
282 uart_writeuint32(tmp
);
286 unsigned char c
= getchar();
288 if (c
== 13) { // if CR send LF
291 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
293 uart_writeuint32(1<<i
);
295 uart_writeuint32(tmp
);
305 volatile uint32_t *hyperram
= (uint32_t*)0xa0000000;
306 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
307 tmp
= readl((unsigned long)&(hyperram
[0]));
309 unsigned char c
= getchar();
311 if (c
== 13) { // if CR send LF
314 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
315 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
317 uart_writeuint32(1<<i
);
319 uart_writeuint32(tmp
);
328 for (int persistence
=0; persistence
< 1000; persistence
++) {
329 puts("DRAM init... ");
333 struct gramProfile profile
= {
335 0xb20, 0x806, 0x200, 0x0
342 struct gramProfile profile
= {
344 0x0320, 0x0006, 0x0200, 0x0000
350 struct gramProfile profile2
;
351 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
352 (void*)DRAM_CTRL_BASE
,
353 (void*)DRAM_INIT_BASE
);
356 puts("MR profile: ");
357 uart_writeuint32(profile
.mode_registers
[0]);
359 uart_writeuint32(profile
.mode_registers
[1]);
361 uart_writeuint32(profile
.mode_registers
[2]);
363 uart_writeuint32(profile
.mode_registers
[3]);
367 // Early read test for WB access sim
368 //uart_writeuint32(*ram);
372 for (size_t i
= 0; i
< 8; i
++) {
373 profile2
.rdly_p0
= i
;
374 gram_load_calibration(&ctx
, &profile2
);
375 gram_reset_burstdet(&ctx
);
377 for (size_t j
= 0; j
< 128; j
++) {
378 tmp
= readl((unsigned long)&(ram
[i
]));
380 if (gram_read_burstdet(&ctx
, 0)) {
389 for (size_t i
= 0; i
< 8; i
++) {
390 profile2
.rdly_p1
= i
;
391 gram_load_calibration(&ctx
, &profile2
);
392 gram_reset_burstdet(&ctx
);
393 for (size_t j
= 0; j
< 128; j
++) {
394 tmp
= readl((unsigned long)&(ram
[i
]));
396 if (gram_read_burstdet(&ctx
, 1)) {
404 puts("Auto calibrating... ");
405 res
= gram_generate_calibration(&ctx
, &profile2
);
406 if (res
!= GRAM_ERR_NONE
) {
408 gram_load_calibration(&ctx
, &profile
);
410 gram_load_calibration(&ctx
, &profile2
);
414 puts("Auto calibration profile:");
416 uart_writeuint32(profile2
.rdly_p0
);
418 uart_writeuint32(profile2
.rdly_p1
);
422 puts("Reloading built-in calibration profile...");
423 gram_load_calibration(&ctx
, &profile
);
425 puts("DRAM test... \n");
426 for (size_t i
= 0; i
< kNumIterations
; i
++) {
427 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
431 for (int dly
= 0; dly
< 8; dly
++) {
433 profile2
.rdly_p0
= dly
;
434 profile2
.rdly_p1
= dly
;
436 uart_writeuint32(profile2
.rdly_p0
);
438 uart_writeuint32(profile2
.rdly_p1
);
439 gram_load_calibration(&ctx
, &profile2
);
440 for (size_t i
= 0; i
< kNumIterations
; i
++) {
441 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
443 uart_writeuint32((unsigned long)(&ram
[i
]));
445 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
450 puts("Test canceled (more than 10 errors)\n");
458 for (size_t i
= 0; i
< kNumIterations
; i
++) {
459 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
461 uart_writeuint32((unsigned long)(&ram
[i
]));
463 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
468 puts("Test canceled (more than 10 errors)\n");
473 if (failcnt
== 0) { // fiinally...
480 // memcpy from SPI Flash to SDRAM then boot
481 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
482 (ftr
& SYS_REG_INFO_HAS_DRAM
) &&
485 // identify ELF, copy if present, and get the start address
486 unsigned long faddr
= copy_flash(spi_offs
);
488 // jump to absolute address