5 #include "microwatt_soc.h"
12 static inline uint32_t read32(const void *addr
)
14 return *(volatile uint32_t *)addr
;
17 static inline void write32(void *addr
, uint32_t value
)
19 *(volatile uint32_t *)addr
= value
;
29 uint32_t zero0
; // reserved
30 uint32_t zero1
; // reserved
36 void memcpy(void *dest
, void *src
, size_t n
) {
38 //cast src and dest to char*
39 char *src_char
= (char *)src
;
40 char *dest_char
= (char *)dest
;
42 dest_char
[i
] = src_char
[i
]; //copy contents byte by byte
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]);
62 // Defining gram_[read|write] allows a trace of all register
63 // accesses to be dumped to console for debugging purposes.
64 // To use, define GRAM_RW_FUNC in gram.h
65 uint32_t gram_read(const struct gramCtx
*ctx
, void *addr
) {
69 uart_writeuint32((unsigned long)addr
);
70 dword
= readl((unsigned long)addr
);
72 uart_writeuint32((unsigned long)dword
);
78 int gram_write(const struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
80 uart_writeuint32((unsigned long)addr
);
82 uart_writeuint32((unsigned long)value
);
83 writel(value
, (unsigned long)addr
);
90 const int kNumIterations
= 14;
93 unsigned long ftr
, val
;
94 volatile uint32_t *ram
= (uint32_t*)DRAM_BASE
;
97 //puts("Firmware launched...\n");
100 puts(" Soc signature: ");
101 tmp
= readl(SYSCON_BASE
+ SYS_REG_SIGNATURE
);
102 uart_writeuint32(tmp
);
103 puts(" Soc features: ");
104 ftr
= readl(SYSCON_BASE
+ SYS_REG_INFO
);
105 if (ftr
& SYS_REG_INFO_HAS_UART
)
107 if (ftr
& SYS_REG_INFO_HAS_DRAM
)
109 if (ftr
& SYS_REG_INFO_HAS_BRAM
)
111 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
)
113 if (ftr
& SYS_REG_INFO_HAS_LITEETH
)
117 if (ftr
& SYS_REG_INFO_HAS_SPI_FLASH
) {
118 puts("SPI Offset: ");
119 val
= readl(SYSCON_BASE
+ SYS_REG_SPI_INFO
);
120 uart_writeuint32(val
);
128 // print out configuration parameters for QSPI
129 volatile uint32_t *qspi_cfg
= (uint32_t*)0xc0003000;
130 for (int k
=0; k
< 2; k
++) {
131 tmp
= readl((unsigned long)&(qspi_cfg
[k
]));
133 //uart_writeuint32(k);
135 //uart_writeuint32(tmp);
139 volatile uint32_t *qspi
= (uint32_t*)0x10000000;
140 volatile uint8_t *qspi_bytes
= (uint8_t*)0x10000000;
141 // let's not, eh? writel(0xDEAF0123, (unsigned long)&(qspi[0]));
142 // tmp = readl((unsigned long)&(qspi[0]));
143 for (i
=0;i
<1000;i
++) {
144 tmp
= readl((unsigned long)&(qspi
[i
]));
145 uart_writeuint32(tmp
);
150 for (i
=0;i
<1000;i
++) {
151 tmp
= readb((unsigned long)&(qspi_bytes
[i
]));
152 uart_writeuint32(tmp
);
158 tmp
= readl((unsigned long)&(qspi
[0x1000/4]));
160 uart_writeuint32(tmp
);
165 unsigned char c
= getchar();
167 if (c
== 13) { // if CR send LF
170 tmp
= readl((unsigned long)&(qspi
[1<<i
]));
172 uart_writeuint32(1<<i
);
174 uart_writeuint32(tmp
);
183 volatile uint32_t *hyperram
= (uint32_t*)0xa0000000;
184 writel(0xDEAF0123, (unsigned long)&(hyperram
[0]));
185 tmp
= readl((unsigned long)&(hyperram
[0]));
187 unsigned char c
= getchar();
189 if (c
== 13) { // if CR send LF
192 writel(0xDEAF0123+i
, (unsigned long)&(hyperram
[1<<i
]));
193 tmp
= readl((unsigned long)&(hyperram
[1<<i
]));
195 uart_writeuint32(1<<i
);
197 uart_writeuint32(tmp
);
206 for (int persistence
=0; persistence
< 1000; persistence
++) {
207 puts("DRAM init... ");
211 struct gramProfile profile
= {
213 0xb20, 0x806, 0x200, 0x0
220 struct gramProfile profile
= {
222 0x0320, 0x0006, 0x0200, 0x0000
228 struct gramProfile profile2
;
229 gram_init(&ctx
, &profile
, (void*)DRAM_BASE
,
230 (void*)DRAM_CTRL_BASE
,
231 (void*)DRAM_INIT_BASE
);
234 puts("MR profile: ");
235 uart_writeuint32(profile
.mode_registers
[0]);
237 uart_writeuint32(profile
.mode_registers
[1]);
239 uart_writeuint32(profile
.mode_registers
[2]);
241 uart_writeuint32(profile
.mode_registers
[3]);
245 // Early read test for WB access sim
246 //uart_writeuint32(*ram);
250 for (size_t i
= 0; i
< 8; i
++) {
251 profile2
.rdly_p0
= i
;
252 gram_load_calibration(&ctx
, &profile2
);
253 gram_reset_burstdet(&ctx
);
255 for (size_t j
= 0; j
< 128; j
++) {
256 tmp
= readl((unsigned long)&(ram
[i
]));
258 if (gram_read_burstdet(&ctx
, 0)) {
267 for (size_t i
= 0; i
< 8; i
++) {
268 profile2
.rdly_p1
= i
;
269 gram_load_calibration(&ctx
, &profile2
);
270 gram_reset_burstdet(&ctx
);
271 for (size_t j
= 0; j
< 128; j
++) {
272 tmp
= readl((unsigned long)&(ram
[i
]));
274 if (gram_read_burstdet(&ctx
, 1)) {
282 puts("Auto calibrating... ");
283 res
= gram_generate_calibration(&ctx
, &profile2
);
284 if (res
!= GRAM_ERR_NONE
) {
286 gram_load_calibration(&ctx
, &profile
);
288 gram_load_calibration(&ctx
, &profile2
);
292 puts("Auto calibration profile:");
294 uart_writeuint32(profile2
.rdly_p0
);
296 uart_writeuint32(profile2
.rdly_p1
);
300 puts("Reloading built-in calibration profile...");
301 gram_load_calibration(&ctx
, &profile
);
303 puts("DRAM test... \n");
304 for (size_t i
= 0; i
< kNumIterations
; i
++) {
305 writel(0xDEAF0000 | i
*4, (unsigned long)&(ram
[i
]));
309 for (int dly
= 0; dly
< 8; dly
++) {
311 profile2
.rdly_p0
= dly
;
312 profile2
.rdly_p1
= dly
;
314 uart_writeuint32(profile2
.rdly_p0
);
316 uart_writeuint32(profile2
.rdly_p1
);
317 gram_load_calibration(&ctx
, &profile2
);
318 for (size_t i
= 0; i
< kNumIterations
; i
++) {
319 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
321 uart_writeuint32((unsigned long)(&ram
[i
]));
323 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
328 puts("Test canceled (more than 10 errors)\n");
336 for (size_t i
= 0; i
< kNumIterations
; i
++) {
337 if (readl((unsigned long)&(ram
[i
])) != (0xDEAF0000 | i
*4)) {
339 uart_writeuint32((unsigned long)(&ram
[i
]));
341 uart_writeuint32(readl((unsigned long)&(ram
[i
])));
346 puts("Test canceled (more than 10 errors)\n");
351 if (failcnt
== 0) { // fiinally...