2017-09-15 21:35:26 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2017-09-16 01:55:22 +08:00
|
|
|
// a pointer to this is a null pointer, but the compiler does not
|
|
|
|
// know that because "sram" is a linker symbol from sections.lds.
|
|
|
|
extern uint32_t sram;
|
|
|
|
|
2017-09-15 21:35:26 +08:00
|
|
|
#define reg_spictrl (*(volatile uint32_t*)0x02000000)
|
|
|
|
#define reg_uart_clkdiv (*(volatile uint32_t*)0x02000004)
|
|
|
|
#define reg_uart_data (*(volatile uint32_t*)0x02000008)
|
|
|
|
#define reg_leds (*(volatile uint32_t*)0x03000000)
|
|
|
|
|
2017-09-18 02:38:03 +08:00
|
|
|
// --------------------------------------------------------
|
|
|
|
|
|
|
|
extern uint32_t flashio_worker_begin;
|
|
|
|
extern uint32_t flashio_worker_end;
|
|
|
|
|
|
|
|
void flashio(uint8_t *data, int len)
|
|
|
|
{
|
|
|
|
uint32_t func[&flashio_worker_end - &flashio_worker_begin];
|
|
|
|
|
|
|
|
uint32_t *src_ptr = &flashio_worker_begin;
|
|
|
|
uint32_t *dst_ptr = func;
|
|
|
|
|
|
|
|
while (src_ptr != &flashio_worker_end)
|
|
|
|
*(dst_ptr++) = *(src_ptr++);
|
|
|
|
|
|
|
|
((void(*)(uint8_t*, int))func)(data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
|
2017-09-17 04:08:05 +08:00
|
|
|
void putchar(char c)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
putchar('\r');
|
|
|
|
reg_uart_data = c;
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:28:19 +08:00
|
|
|
void print(const char *p)
|
|
|
|
{
|
2017-09-17 04:08:05 +08:00
|
|
|
while (*p)
|
|
|
|
putchar(*(p++));
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_hex(uint32_t v, int digits)
|
|
|
|
{
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
char c = "0123456789abcdef"[(v >> (4*i)) & 15];
|
|
|
|
if (c == '0' && i >= digits) continue;
|
|
|
|
putchar(c);
|
|
|
|
digits = i;
|
2017-09-15 22:28:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 02:38:03 +08:00
|
|
|
void print_dec(uint32_t v)
|
|
|
|
{
|
|
|
|
if (v >= 100) {
|
|
|
|
print(">=100");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v >= 90) { putchar('9'); v -= 90; }
|
|
|
|
else if (v >= 80) { putchar('8'); v -= 80; }
|
|
|
|
else if (v >= 70) { putchar('7'); v -= 70; }
|
|
|
|
else if (v >= 60) { putchar('6'); v -= 60; }
|
|
|
|
else if (v >= 50) { putchar('5'); v -= 50; }
|
|
|
|
else if (v >= 40) { putchar('4'); v -= 40; }
|
|
|
|
else if (v >= 30) { putchar('3'); v -= 30; }
|
|
|
|
else if (v >= 20) { putchar('2'); v -= 20; }
|
|
|
|
else if (v >= 10) { putchar('1'); v -= 10; }
|
|
|
|
|
|
|
|
if (v >= 9) { putchar('9'); v -= 9; }
|
|
|
|
else if (v >= 8) { putchar('8'); v -= 8; }
|
|
|
|
else if (v >= 7) { putchar('7'); v -= 7; }
|
|
|
|
else if (v >= 6) { putchar('6'); v -= 6; }
|
|
|
|
else if (v >= 5) { putchar('5'); v -= 5; }
|
|
|
|
else if (v >= 4) { putchar('4'); v -= 4; }
|
|
|
|
else if (v >= 3) { putchar('3'); v -= 3; }
|
|
|
|
else if (v >= 2) { putchar('2'); v -= 2; }
|
|
|
|
else if (v >= 1) { putchar('1'); v -= 1; }
|
|
|
|
else putchar('0');
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:28:19 +08:00
|
|
|
char getchar_prompt(char *prompt)
|
|
|
|
{
|
|
|
|
int32_t c = -1;
|
|
|
|
|
|
|
|
uint32_t cycles_begin, cycles_now, cycles;
|
|
|
|
__asm__ volatile ("rdcycle %0" : "=r"(cycles_begin));
|
|
|
|
|
|
|
|
if (prompt)
|
|
|
|
print(prompt);
|
|
|
|
|
|
|
|
reg_leds = ~0;
|
|
|
|
while (c == -1) {
|
|
|
|
__asm__ volatile ("rdcycle %0" : "=r"(cycles_now));
|
|
|
|
cycles = cycles_now - cycles_begin;
|
|
|
|
if (cycles > 12000000) {
|
|
|
|
if (prompt)
|
|
|
|
print(prompt);
|
|
|
|
cycles_begin = cycles_now;
|
|
|
|
reg_leds = ~reg_leds;
|
|
|
|
}
|
|
|
|
c = reg_uart_data;
|
|
|
|
}
|
|
|
|
reg_leds = 0;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
char getchar()
|
|
|
|
{
|
|
|
|
return getchar_prompt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
|
|
|
|
void cmd_read_spi_flash_id()
|
|
|
|
{
|
2017-09-18 02:38:03 +08:00
|
|
|
uint8_t buffer[17] = { 0x9F, /* zeros */ };
|
|
|
|
flashio(buffer, 17);
|
2017-09-15 22:28:19 +08:00
|
|
|
|
2017-09-18 02:38:03 +08:00
|
|
|
for (int i = 1; i <= 16; i++) {
|
|
|
|
putchar(' ');
|
|
|
|
print_hex(buffer[i], 2);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2017-09-15 22:28:19 +08:00
|
|
|
|
2017-09-18 02:38:03 +08:00
|
|
|
// --------------------------------------------------------
|
2017-09-17 04:08:05 +08:00
|
|
|
|
2017-09-18 02:38:03 +08:00
|
|
|
uint8_t cmd_read_spi_flash_regs_print(uint32_t addr, const char *name)
|
|
|
|
{
|
|
|
|
uint8_t buffer[6] = {0x65, addr >> 16, addr >> 8, addr, 0, 0};
|
|
|
|
flashio(buffer, 6);
|
|
|
|
|
|
|
|
print("0x");
|
|
|
|
print_hex(addr, 6);
|
|
|
|
print(" ");
|
|
|
|
print(name);
|
|
|
|
print(" 0x");
|
|
|
|
print_hex(buffer[5], 2);
|
|
|
|
print("\n");
|
|
|
|
|
|
|
|
return buffer[5];
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmd_read_spi_flash_regs()
|
|
|
|
{
|
|
|
|
print("\n");
|
|
|
|
uint8_t sr1v = cmd_read_spi_flash_regs_print(0x800000, "SR1V");
|
|
|
|
uint8_t sr2v = cmd_read_spi_flash_regs_print(0x800001, "SR2V");
|
|
|
|
uint8_t cr1v = cmd_read_spi_flash_regs_print(0x800002, "CR1V");
|
|
|
|
uint8_t cr2v = cmd_read_spi_flash_regs_print(0x800003, "CR2V");
|
|
|
|
uint8_t cr3v = cmd_read_spi_flash_regs_print(0x800004, "CR3V");
|
|
|
|
uint8_t vdlp = cmd_read_spi_flash_regs_print(0x800005, "VDLP");
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
|
|
|
|
void cmd_benchmark()
|
|
|
|
{
|
|
|
|
uint8_t data[256];
|
|
|
|
uint32_t *words = (void*)data;
|
|
|
|
|
|
|
|
uint32_t x32 = 314159265;
|
|
|
|
|
|
|
|
uint32_t cycles_begin, cycles_end;
|
|
|
|
uint32_t instns_begin, instns_end;
|
|
|
|
__asm__ volatile ("rdcycle %0" : "=r"(cycles_begin));
|
|
|
|
__asm__ volatile ("rdinstret %0" : "=r"(instns_begin));
|
|
|
|
|
|
|
|
for (int i = 0; i < 20; i++)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < 256; k++)
|
|
|
|
{
|
|
|
|
x32 ^= x32 << 13;
|
|
|
|
x32 ^= x32 >> 17;
|
|
|
|
x32 ^= x32 << 5;
|
|
|
|
data[k] = x32;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int k = 0, p = 0; k < 256; k++)
|
|
|
|
{
|
|
|
|
if (data[k])
|
|
|
|
data[p++] = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int k = 0, p = 0; k < 64; k++)
|
|
|
|
{
|
|
|
|
x32 = x32 ^ words[k];
|
|
|
|
}
|
2017-09-17 04:08:05 +08:00
|
|
|
}
|
2017-09-18 02:38:03 +08:00
|
|
|
|
|
|
|
__asm__ volatile ("rdcycle %0" : "=r"(cycles_end));
|
|
|
|
__asm__ volatile ("rdinstret %0" : "=r"(instns_end));
|
|
|
|
|
|
|
|
print("Cycles: 0x");
|
|
|
|
print_hex(cycles_end - cycles_begin, 8);
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
print("Instns: 0x");
|
|
|
|
print_hex(instns_end - instns_begin, 8);
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
print("Chksum: 0x");
|
|
|
|
print_hex(x32, 8);
|
2017-09-17 04:08:05 +08:00
|
|
|
putchar('\n');
|
2017-09-15 22:28:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
|
2017-09-15 21:35:26 +08:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
reg_uart_clkdiv = 104;
|
2017-09-15 22:28:19 +08:00
|
|
|
while (getchar_prompt("Press ENTER to continue..\n") != '\r') { /* wait */ }
|
|
|
|
|
|
|
|
print("\n");
|
|
|
|
print(" ____ _ ____ ____\n");
|
|
|
|
print(" | _ \\(_) ___ ___/ ___| ___ / ___|\n");
|
|
|
|
print(" | |_) | |/ __/ _ \\___ \\ / _ \\| |\n");
|
|
|
|
print(" | __/| | (_| (_) |__) | (_) | |___\n");
|
|
|
|
print(" |_| |_|\\___\\___/____/ \\___/ \\____|\n");
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
print("\n");
|
2017-09-18 02:38:03 +08:00
|
|
|
print("\n");
|
|
|
|
print("SPI State:\n");
|
|
|
|
|
|
|
|
print(" DDR ");
|
|
|
|
if ((reg_spictrl & (1 << 22)) != 0)
|
|
|
|
print("ON");
|
|
|
|
else
|
|
|
|
print("OFF");
|
|
|
|
|
|
|
|
print(" (latency=");
|
|
|
|
print_dec((reg_spictrl >> 16) & 15);
|
|
|
|
print(")\n");
|
|
|
|
|
|
|
|
print(" QSPI ");
|
|
|
|
if ((reg_spictrl & (1 << 21)) != 0)
|
|
|
|
print("ON\n");
|
|
|
|
else
|
|
|
|
print("OFF\n");
|
|
|
|
|
|
|
|
print(" XIP ");
|
|
|
|
if ((reg_spictrl & (1 << 20)) != 0)
|
|
|
|
print("ON\n");
|
|
|
|
else
|
|
|
|
print("OFF\n");
|
|
|
|
|
2017-09-15 22:28:19 +08:00
|
|
|
print("\n");
|
|
|
|
print("Select an action:\n");
|
|
|
|
print("\n");
|
|
|
|
print(" [1] Read SPI Flash ID\n");
|
2017-09-18 02:38:03 +08:00
|
|
|
print(" [2] Read SPI Config Regs\n");
|
|
|
|
print(" [3] Enable/disable DDR\n");
|
|
|
|
print(" [4] Enable/disable QSPI\n");
|
|
|
|
print(" [5] Enable/disable XIP\n");
|
|
|
|
print(" [0] Run simplistic benchmark\n");
|
2017-09-15 22:28:19 +08:00
|
|
|
print("\n");
|
|
|
|
|
|
|
|
for (int rep = 10; rep > 0; rep--)
|
|
|
|
{
|
|
|
|
print("Command> ");
|
|
|
|
char cmd = getchar();
|
|
|
|
if (cmd > 32 && cmd < 127)
|
2017-09-17 04:08:05 +08:00
|
|
|
putchar(cmd);
|
2017-09-15 22:28:19 +08:00
|
|
|
print("\n");
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case '1':
|
|
|
|
cmd_read_spi_flash_id();
|
|
|
|
break;
|
2017-09-18 02:38:03 +08:00
|
|
|
case '2':
|
|
|
|
cmd_read_spi_flash_regs();
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
reg_spictrl ^= 1 << 22;
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
reg_spictrl ^= 1 << 21;
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
reg_spictrl ^= 1 << 20;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
cmd_benchmark();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
2017-09-15 22:28:19 +08:00
|
|
|
}
|
2017-09-18 02:38:03 +08:00
|
|
|
|
|
|
|
break;
|
2017-09-15 22:28:19 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 21:35:26 +08:00
|
|
|
}
|
|
|
|
|