#include "fw.h"
#include "console.h"
#include "io.h"

extern char __dram_init_start[], __dram_init_end[];
extern char __dram_bss_start[],  __dram_bss_end[];

enum romcmd {
    ROM_WRITE_ENABLE			= 0x06,
    ROM_VOLATILE_SR_WRITE_ENABLE	= 0x50,
    ROM_WRITE_DISABLE			= 0x04,
    ROM_RELEASE_POWERDOWN_ID		= 0xab,
    ROM_MANUFACTURER_DEVICE_ID		= 0x90,
    ROM_JEDEC_ID			= 0x9f,
    ROM_READ_UNIQUE_ID			= 0x4b,
    ROM_READ_DATA			= 0x03, /* DO NOT USE */
    ROM_FAST_READ			= 0x0b,
    ROM_PAGE_PROGRAM			= 0x02,
    ROM_ERASE_4K			= 0x20,
    ROM_ERASE_32K			= 0x52,
    ROM_ERASE_64K			= 0xd8,
    ROM_ERASE_ALL			= 0xc7,
    ROM_READ_SR1			= 0x05,
    ROM_WRITE_SR1			= 0x01,
    ROM_READ_SR2			= 0x35,
    ROM_WRITE_SR2			= 0x31,
    ROM_READ_SR3			= 0x15,
    ROM_WRITE_SR3			= 0x11,
    ROM_READ_SFPD			= 0x5a,
    ROM_ERASE_SECURITY			= 0x44,
    ROM_PROGRAM_SECURITY		= 0x42,
    ROM_READ_SECURITY			= 0x48,
    ROM_GLOBAL_BLOCK_LOCK		= 0x7e,
    ROM_GLOBAL_BLOCK_UNLOCK		= 0x98,
    ROM_READ_BLOCK_LOCK			= 0x3d,
    ROM_ONE_BLOCK_LOCK			= 0x36,
    ROM_ONE_BLOCK_UNLOCK		= 0x39,
    ROM_ERASE_PROGRAM_SUSPEND		= 0x75,
    ROM_ERASE_PROGRAM_RESUME		= 0x7a,
    ROM_POWER_DOWN			= 0xb9,
    ROM_ENABLE_RESET			= 0x66,
    ROM_RESET				= 0x99,
    ROM_FAST_READ_DUAL			= 0x3b
};

#define SPIROM_DUAL_MODE 1

void __hot romcopy_download(void *dst, size_t offset, size_t len)
{
    unsigned int cmd;
    unsigned int flags = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_WRITE_RAM;

    if (SPIROM_DUAL_MODE) {
	cmd = ROM_FAST_READ_DUAL;
	flags |= ROMCOPY_SPI_DUAL;
    } else {
	cmd = ROM_FAST_READ;
    }

    ROMCOPY_RAMADDR = (size_t)dst;
    ROMCOPY_ROMCMD  = __rom_offset + offset + (cmd << 24);
    ROMCOPY_DATALEN = len | flags;
}

void __hot romcopy_bzero(void *dst, size_t len)
{
    ROMCOPY_RAMADDR = (size_t)dst;
    ROMCOPY_ROMCMD  = 0;
    ROMCOPY_DATALEN = len | ROMCOPY_ZERO_BUFFER | ROMCOPY_WRITE_RAM;
}

/*
 * Read unique serial number programmed into ROM
 *
 */
char __bss_hot rom_serial_str[16];
qword_t __bss_hot rom_serial;

static __must_inline void rom_read_serial(void)
{
    ROMCOPY_ROMCMD  = ROM_READ_UNIQUE_ID << 24;
    ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_MORE;
    waitfor(ROMCOPY_IRQ);

    ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
    waitfor(ROMCOPY_IRQ);

    rom_serial.l[1] = ROMCOPY_INPUT;
    ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4);
    waitfor(ROMCOPY_IRQ);

    rom_serial.l[0] = ROMCOPY_INPUT;
}

/*
 * Convert the ROM serial number to a hex string and poke it into the
 * USB descriptor "ROM". Used to use base36, but this has to be done
 * very early, and it has turned out that making it consistent with
 * what one can easily get out of a debugger is really useful.
 *
 * Doing this as early as possible means a much better chance to see
 * the proper serial number during USB enumeration, so doing it
 * immediately after SPI ROM conditioning is a great time.
 */
static __must_inline void rom_mangle_serial(void)
{
    volatile uint32_t *udp = &usbdesc_rom[2];
    
    for (int i = 7; i >= 0; i--) {
	unsigned int v = rom_serial.b[i];
	unsigned int c;

	c = (v >> 4)+'0';
	if (c > '9')
	    c += 'A'-'9'-1;

	udp[0] = c;

	c = (v & 15)+'0';
	if (c > '9')
	    c += 'A'-'9'-1;

	udp[2] = c;

	udp += 4;
    }
}

void rom_print_serial(void)
{
    /* Print the ROM serial when we actually can */
    con_printf("ROM serial: %08X%08X (%08x-%08x)\n",
	       rom_serial.l[1], rom_serial.l[0],
	       rom_serial.l[1], rom_serial.l[0]);
}

static __must_inline void romcopy_config_flash(void)
{
    /* Enable writing volatile status register bits */
    ROMCOPY_ROMCMD = ROM_VOLATILE_SR_WRITE_ENABLE << 24;
    ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(1);
    waitfor(ROMCOPY_IRQ);

    /* Write SR3 = 0; this sets the drive strength to maximum */
    ROMCOPY_ROMCMD = ROM_WRITE_SR3 << 24;
    ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(2);
    waitfor(ROMCOPY_IRQ);
}

IRQHANDLER(romcopy,0)
{
    static __sbss unsigned int romcopy_state;
    size_t len;

    switch (romcopy_state++) {
    case 0:
	/* Condition flash ROM */
	romcopy_config_flash();

	/* Read serial number */
	rom_read_serial();

	/* Start copy DRAM data */
	len = __dram_init_end - __dram_init_start;
	romcopy_download(__dram_init_start, 0, len);

	/* Convert serial number and export to USB */
	rom_mangle_serial();
	break;
    case 1:
	/* Zero .dram.bss */
	len = __dram_bss_end - __dram_bss_start;
	romcopy_bzero(__dram_bss_start, len);
	break;
    default:
	mask_irq(ROMCOPY_IRQ);
	break;
    }
}
