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
78 #define TERCEL_SPI_REG_SYS_PHY_CFG1 0x10
79 #define TERCEL_SPI_REG_SYS_FLASH_CFG5 0x24
80 #define TERCEL_SPI_PHY_CLOCK_DIVISOR_MASK 0xff
81 #define TERCEL_SPI_PHY_CLOCK_DIVISOR_SHIFT 0
82 #define TERCEL_SPI_FLASH_EN_MULTCYC_READ_MASK 0x1
83 #define TERCEL_SPI_FLASH_EN_MULTCYC_READ_SHIFT 0
84 static inline uint32_t read_tercel_register(uint8_t reg
)
86 return readl((unsigned long)(SPI_FCTRL_BASE
+reg
));
89 static inline void write_tercel_register(uint8_t reg
, uint32_t value
)
91 writel(value
, (unsigned long)(SPI_FCTRL_BASE
+reg
));
94 // TODO: need to use this
95 // https://gitlab.raptorengineering.com/kestrel-collaboration/kestrel-litex/litex/-/blob/master/litex/soc/software/bios/boot.c#L575
96 static bool fl_read(void *dst
, uint32_t offset
, uint32_t size
)
99 memcpy(d
, (void *)(unsigned long)(SPI_FLASH_BASE
+ offset
), size
);
103 static unsigned long copy_flash(unsigned int offset
, unsigned int dst_offs
)
107 unsigned int i
, poff
, size
, off
;
112 // Set SPI clock cycle divider to 1
114 dword
= read_tercel_register(TERCEL_SPI_REG_SYS_PHY_CFG1
);
115 dword
&= ~(TERCEL_SPI_PHY_CLOCK_DIVISOR_MASK
<<
116 TERCEL_SPI_PHY_CLOCK_DIVISOR_SHIFT
);
117 dword
|= ((1 & TERCEL_SPI_PHY_CLOCK_DIVISOR_MASK
) <<
118 TERCEL_SPI_PHY_CLOCK_DIVISOR_SHIFT
);
119 write_tercel_register(TERCEL_SPI_REG_SYS_PHY_CFG1
, dword
);
120 // Enable read merging
121 dword
= read_tercel_register(TERCEL_SPI_REG_SYS_FLASH_CFG5
);
122 dword
|= (TERCEL_SPI_FLASH_EN_MULTCYC_READ_MASK
<<
123 TERCEL_SPI_FLASH_EN_MULTCYC_READ_SHIFT
);
124 write_tercel_register(TERCEL_SPI_REG_SYS_FLASH_CFG5
, dword
);
126 puts("Trying flash...\r\n");
127 if (!fl_read(&ehdr
, offset
, sizeof(ehdr
)))
129 if (!IS_ELF(ehdr
) || ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) {
130 puts("Doesn't look like an elf64\r\n");
133 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
||
134 ehdr
.e_machine
!= EM_PPC64
) {
135 puts("Not a ppc64le binary\r\n");
139 poff
= offset
+ ehdr
.e_phoff
;
140 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
141 if (!fl_read(&ph
, poff
, sizeof(ph
)))
143 if (ph
.p_type
!= PT_LOAD
)
146 /* XXX Add bound checking ! */
148 addr
= (void *)ph
.p_vaddr
;
149 off
= offset
+ ph
.p_offset
;
150 //printf("Copy segment %d (0x%x bytes) to %p\n", i, size, addr);
151 puts("Copy segment ");
154 uart_writeuint32(size
);
156 uart_writeuint32((uint32_t)(unsigned long)addr
);
158 fl_read(addr
+dst_offs
, off
, size
);
159 poff
+= ehdr
.e_phentsize
;
162 puts("Booting from DRAM at");
163 uart_writeuint32((unsigned int)(dst_offs
+ehdr
.e_entry
));
166 puts("Dump DRAM\r\n");
167 for (i
= 0; i
< 64; i
++) {
168 uart_writeuint32(readl(dst_offs
+ehdr
.e_entry
+(i
*4)));
170 if ((i
& 7) == 7) puts("\r\n");
174 //flush_cpu_icache();
175 return dst_offs
+ehdr
.e_entry
;
178 for (i
= 0; i
< 8; i
++) {
179 uart_writeuint32(ehdr
.e_ident
[i
]);
188 // Defining gram_[read|write] allows a trace of all register
189 // accesses to be dumped to console for debugging purposes.
190 // To use, define GRAM_RW_FUNC in gram.h
191 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
195 uart_writeuint32((unsigned long)addr
);
196 dword
= readl((unsigned long)addr
);
198 uart_writeuint32((unsigned long)dword
);
204 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
205 puts("gram_write: ");
206 uart_writeuint32((unsigned long)addr
);
208 uart_writeuint32((unsigned long)value
);
209 writel(value
, (unsigned long)addr
);
216 const int kNumIterations
= 14;
217 int res
, failcnt
= 0;
219 unsigned long ftr
, spi_offs
=0x0;
220 volatile uint32_t *ram
= (uint32_t*)MEMORY_BASE
;
223 //puts("Firmware launched...\n");
226 puts(" Soc signature: ");
227 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
228 uart_writeuint32(tmp
);
229 puts(" Soc features: ");
230 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
231 if (ftr
& SYS_REG_INFO_HAS_UART
)
233 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
235 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
237 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
239 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
243 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
244 puts("SPI Offset: ");
245 spi_offs
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
246 uart_writeuint32(spi_offs
);
254 // print out configuration parameters for QSPI
255 volatile uint32_t *qspi_cfg
= (uint32_t*)SPI_FCTRL_BASE
;
256 for (int k
=0; k
< 2; k
++) {
257 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
261 uart_writeuint32(tmp
);
265 volatile uint32_t *qspi
= (uint32_t*)SPI_FLASH_BASE
+spi_offs
;
266 //volatile uint8_t *qspi_bytes = (uint8_t*)spi_offs;
267 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
268 // tmp = readl((unsigned long)&(qspi[0]));
269 for (int i
=0;i
<256;i
++) {
270 tmp
= readl((unsigned long)&(qspi
[i
]));
271 uart_writeuint32(tmp
);
273 if ((i
& 0x7) == 0x7) puts("\r\n");
277 for (i=0;i<256;i++) {
278 tmp = readb((unsigned long)&(qspi_bytes[i]));
279 uart_writeuint32(tmp);
286 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
288 uart_writeuint32(tmp
);
292 unsigned char c
= getchar();
294 if (c
== 13) { // if CR send LF
297 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
299 uart_writeuint32(1<<i
);
301 uart_writeuint32(tmp
);
311 volatile uint32_t *hyperram
= (uint32_t*)0xa0000000;
312 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
313 tmp
= readl((unsigned long)&(hyperram
[0]));
315 unsigned char c
= getchar();
317 if (c
== 13) { // if CR send LF
320 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
321 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
323 uart_writeuint32(1<<i
);
325 uart_writeuint32(tmp
);
334 // init DRAM only if SYSCON says it exists (duh)
335 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
337 puts("DRAM init... ");
341 struct gramProfile profile
= {
343 0xb20, 0x806, 0x200, 0x0
350 struct gramProfile profile
= {
352 0x0320, 0x0006, 0x0200, 0x0000
358 struct gramProfile profile2
;
359 gram_init(&ctx
, &profile
, (void*)MEMORY_BASE
,
360 (void*)DRAM_CTRL_BASE
,
361 (void*)DRAM_INIT_BASE
);
364 puts("MR profile: ");
365 uart_writeuint32(profile
.mode_registers
[0]);
367 uart_writeuint32(profile
.mode_registers
[1]);
369 uart_writeuint32(profile
.mode_registers
[2]);
371 uart_writeuint32(profile
.mode_registers
[3]);
375 // Early read test for WB access sim
376 //uart_writeuint32(*ram);
380 for (size_t i
= 0; i
< 8; i
++) {
381 profile2
.rdly_p0
= i
;
382 gram_load_calibration(&ctx
, &profile2
);
383 gram_reset_burstdet(&ctx
);
385 for (size_t j
= 0; j
< 128; j
++) {
386 tmp
= readl((unsigned long)&(ram
[i
]));
388 if (gram_read_burstdet(&ctx
, 0)) {
397 for (size_t i
= 0; i
< 8; i
++) {
398 profile2
.rdly_p1
= i
;
399 gram_load_calibration(&ctx
, &profile2
);
400 gram_reset_burstdet(&ctx
);
401 for (size_t j
= 0; j
< 128; j
++) {
402 tmp
= readl((unsigned long)&(ram
[i
]));
404 if (gram_read_burstdet(&ctx
, 1)) {
412 puts("Auto calibrating... ");
413 res
= gram_generate_calibration(&ctx
, &profile2
);
414 if (res
!= GRAM_ERR_NONE
) {
416 gram_load_calibration(&ctx
, &profile
);
418 gram_load_calibration(&ctx
, &profile2
);
422 puts("Auto calibration profile:");
424 uart_writeuint32(profile2
.rdly_p0
);
426 uart_writeuint32(profile2
.rdly_p1
);
430 puts("Reloading built-in calibration profile...");
431 gram_load_calibration(&ctx
, &profile
);
433 puts("DRAM test... \n");
434 for (size_t i
= 0; i
< kNumIterations
; i
++) {
435 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
439 for (int dly
= 0; dly
< 8; dly
++) {
441 profile2
.rdly_p0
= dly
;
442 profile2
.rdly_p1
= dly
;
444 uart_writeuint32(profile2
.rdly_p0
);
446 uart_writeuint32(profile2
.rdly_p1
);
447 gram_load_calibration(&ctx
, &profile2
);
448 for (size_t i
= 0; i
< kNumIterations
; i
++) {
449 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
451 uart_writeuint32((unsigned long)(&ram
[i
]));
453 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
458 puts("Test canceled (more than 10 errors)\n");
466 for (size_t i
= 0; i
< kNumIterations
; i
++) {
467 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
469 uart_writeuint32((unsigned long)(&ram
[i
]));
471 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
476 puts("Test canceled (more than 10 errors)\n");
485 // memcpy from SPI Flash then boot
486 if ((ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) &&
489 // identify ELF, copy if present, and get the start address
490 unsigned long faddr
= copy_flash(spi_offs
,
493 // jump to absolute address
494 mtspr(8, faddr
); // move address to LR
495 __asm__
volatile("blr");
497 // works with head.S which copies r3 into ctr then does bctr