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
);
159 //flush_cpu_icache();
163 for (i
= 0; i
< 8; i
++) {
164 uart_writeuint32(ehdr
.e_ident
[i
]);
173 // Defining gram_[read|write] allows a trace of all register
174 // accesses to be dumped to console for debugging purposes.
175 // To use, define GRAM_RW_FUNC in gram.h
176 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
180 uart_writeuint32((unsigned long)addr
);
181 dword
= readl((unsigned long)addr
);
183 uart_writeuint32((unsigned long)dword
);
189 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
190 puts("gram_write: ");
191 uart_writeuint32((unsigned long)addr
);
193 uart_writeuint32((unsigned long)value
);
194 writel(value
, (unsigned long)addr
);
201 const int kNumIterations
= 14;
202 int res
, failcnt
= 0;
204 unsigned long ftr
, spi_offs
=0x0;
205 volatile uint32_t *ram
= (uint32_t*)MEMORY_BASE
;
208 //puts("Firmware launched...\n");
211 puts(" Soc signature: ");
212 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
213 uart_writeuint32(tmp
);
214 puts(" Soc features: ");
215 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
216 if (ftr
& SYS_REG_INFO_HAS_UART
)
218 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
220 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
222 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
224 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
228 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
229 puts("SPI Offset: ");
230 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
231 uart_writeuint32(spi_offs
);
239 // print out configuration parameters for QSPI
240 volatile uint32_t *qspi_cfg
= (uint32_t*)SPI_FCTRL_BASE
;
241 for (int k
=0; k
< 2; k
++) {
242 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
246 uart_writeuint32(tmp
);
250 volatile uint32_t *qspi
= (uint32_t*)spi_offs
;
251 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
252 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
253 // tmp = readl((unsigned long)&(qspi[0]));
254 for (int i
=0;i
<256;i
++) {
255 tmp
= readl((unsigned long)&(qspi
[i
]));
256 uart_writeuint32(tmp
);
258 if ((i
& 0x7) == 0x7) puts("\r\n");
262 for (i=0;i<256;i++) {
263 tmp = readb((unsigned long)&(qspi_bytes[i]));
264 uart_writeuint32(tmp);
271 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
273 uart_writeuint32(tmp
);
277 unsigned char c
= getchar();
279 if (c
== 13) { // if CR send LF
282 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
284 uart_writeuint32(1<<i
);
286 uart_writeuint32(tmp
);
296 volatile uint32_t *hyperram
= (uint32_t*)0xa0000000;
297 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
298 tmp
= readl((unsigned long)&(hyperram
[0]));
300 unsigned char c
= getchar();
302 if (c
== 13) { // if CR send LF
305 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
306 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
308 uart_writeuint32(1<<i
);
310 uart_writeuint32(tmp
);
319 for (int persistence
=0; persistence
< 1000; persistence
++) {
320 puts("DRAM init... ");
324 struct gramProfile profile
= {
326 0xb20, 0x806, 0x200, 0x0
333 struct gramProfile profile
= {
335 0x0320, 0x0006, 0x0200, 0x0000
341 struct gramProfile profile2
;
342 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
343 (void*)DRAM_CTRL_BASE
,
344 (void*)DRAM_INIT_BASE
);
347 puts("MR profile: ");
348 uart_writeuint32(profile
.mode_registers
[0]);
350 uart_writeuint32(profile
.mode_registers
[1]);
352 uart_writeuint32(profile
.mode_registers
[2]);
354 uart_writeuint32(profile
.mode_registers
[3]);
358 // Early read test for WB access sim
359 //uart_writeuint32(*ram);
363 for (size_t i
= 0; i
< 8; i
++) {
364 profile2
.rdly_p0
= i
;
365 gram_load_calibration(&ctx
, &profile2
);
366 gram_reset_burstdet(&ctx
);
368 for (size_t j
= 0; j
< 128; j
++) {
369 tmp
= readl((unsigned long)&(ram
[i
]));
371 if (gram_read_burstdet(&ctx
, 0)) {
380 for (size_t i
= 0; i
< 8; i
++) {
381 profile2
.rdly_p1
= i
;
382 gram_load_calibration(&ctx
, &profile2
);
383 gram_reset_burstdet(&ctx
);
384 for (size_t j
= 0; j
< 128; j
++) {
385 tmp
= readl((unsigned long)&(ram
[i
]));
387 if (gram_read_burstdet(&ctx
, 1)) {
395 puts("Auto calibrating... ");
396 res
= gram_generate_calibration(&ctx
, &profile2
);
397 if (res
!= GRAM_ERR_NONE
) {
399 gram_load_calibration(&ctx
, &profile
);
401 gram_load_calibration(&ctx
, &profile2
);
405 puts("Auto calibration profile:");
407 uart_writeuint32(profile2
.rdly_p0
);
409 uart_writeuint32(profile2
.rdly_p1
);
413 puts("Reloading built-in calibration profile...");
414 gram_load_calibration(&ctx
, &profile
);
416 puts("DRAM test... \n");
417 for (size_t i
= 0; i
< kNumIterations
; i
++) {
418 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
422 for (int dly
= 0; dly
< 8; dly
++) {
424 profile2
.rdly_p0
= dly
;
425 profile2
.rdly_p1
= dly
;
427 uart_writeuint32(profile2
.rdly_p0
);
429 uart_writeuint32(profile2
.rdly_p1
);
430 gram_load_calibration(&ctx
, &profile2
);
431 for (size_t i
= 0; i
< kNumIterations
; i
++) {
432 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
434 uart_writeuint32((unsigned long)(&ram
[i
]));
436 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
441 puts("Test canceled (more than 10 errors)\n");
449 for (size_t i
= 0; i
< kNumIterations
; i
++) {
450 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
452 uart_writeuint32((unsigned long)(&ram
[i
]));
454 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
459 puts("Test canceled (more than 10 errors)\n");
464 if (failcnt
== 0) { // fiinally...
471 // memcpy from SPI Flash to SDRAM then boot
472 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
473 (ftr
& SYS_REG_INFO_HAS_DRAM
) &&
476 // identify ELF, copy if present, and get the start address
477 unsigned long faddr
= copy_flash(spi_offs
);
479 // jump to absolute address