#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, /* Only for non-DMA */ 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 }; size_t __sbss romcopy_log[15]; void __hot romcopy_download(void *dst, size_t offset, size_t len) { ROMCOPY_RAMADDR = (size_t)dst; ROMCOPY_ROMCMD = __rom_offset + offset + (ROM_FAST_READ_DUAL << 24); ROMCOPY_DATALEN = len | ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_DUAL | ROMCOPY_WRITE_RAM; } 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; } uint32_t __sbss romcopy_time[2]; IRQHANDLER(romcopy) { static __sbss unsigned int romcopy_state; size_t len; switch (romcopy_state++) { case 0: /* Copy DRAM data */ len = __dram_init_end - __dram_init_start; romcopy_download(__dram_init_start, 0, len); 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; } } /* * Read unique serial number programmed into ROM. Convert the serial * number to 12 characters in base36 (technically a wraparound that is * never going to matter.) It's more fun than plain hex... :) */ char serial_str[16]; uint64_t rom_get_serial(void) { union { uint32_t l[2]; uint64_t q; } o; waitfor(ROMCOPY_IRQ); 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); o.l[1] = ROMCOPY_INPUT; ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4); waitfor(ROMCOPY_IRQ); o.l[0] = ROMCOPY_INPUT; serial_str[12] = '\0'; uint64_t v = o.q; for (int i = 11; i >= 0; i--) { unsigned char c; unsigned int d = v % 36; v /= 36; c = d + '0' + ('A'-'0'-10)*(d >= 10); serial_str[i] = c; usbdesc_rom[2+2*i] = c; } if ( 1 ) con_printf("ROM serial: %08x-%08x (%s)\n", o.l[1], o.l[0], serial_str); return o.q; }