123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- #include "fw.h"
- #include "console.h"
- #include "io.h"
- #include "spiflash.h"
- 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;
- }
- }
- /*
- * SPI flash parameters and routines (for spiflash.c)
- */
- static const struct spiflash_ops max80_spiflash_ops;
- static const struct spiflash_param max80_spiflash_param;
- /*
- * SPI flash operations
- */
- static int max80_spi_write_buffer(const void *buf, unsigned int buflen,
- bool more)
- {
- const uint8_t *p = buf;
- uint32_t cmd = 0;
- unsigned int bytecmd = 0;
- while (buflen) {
- cmd = (*p++ << 24) | (cmd >> 8);
- buflen--;
- bytecmd += ROMCOPY_SPI_CMDLEN(1);
- if (!(buflen & 3)) {
- if (buflen || more)
- bytecmd |= ROMCOPY_SPI_MORE;
- waitfor(ROMCOPY_IRQ);
- ROMCOPY_ROMCMD = cmd;
- ROMCOPY_DATALEN = bytecmd;
- bytecmd = cmd = 0;
- }
- }
- return 0;
- }
- static int max80_spi_read_buffer(void *buf, unsigned int buflen, bool more)
- {
- uint8_t *p = buf;
- unsigned int bytecmd;
- uint32_t v;
- if (!buflen)
- return 0;
- waitfor(ROMCOPY_IRQ);
- ROMCOPY_ROMCMD = 0;
- while (buflen > 4) {
- ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
- waitfor(ROMCOPY_IRQ);
- v = ROMCOPY_INPUT;
- p[0] = v >> 24;
- p[1] = v >> 16;
- p[2] = v >> 8;
- p[3] = v;
- p += 4;
- buflen -= 4;
- }
- bytecmd = ROMCOPY_SPI_CMDLEN(buflen);
- if (more)
- bytecmd |= ROMCOPY_SPI_MORE;
- ROMCOPY_DATALEN = bytecmd;
- waitfor(ROMCOPY_IRQ);
- v = ROMCOPY_INPUT;
- v <<= ((4-buflen) << 3);
- while (buflen--) {
- *p++ = v >> 24;
- v <<= 8;
- }
- return 0;
- }
- static int max80_spi_write(void *cookie,
- const void *cmd, unsigned int cmd_len,
- const void *data, unsigned int data_len,
- int tshsl)
- {
- (void)cookie;
- (void)tshsl; /* Enforced in hardware */
- if (cmd_len)
- max80_spi_write_buffer(cmd, cmd_len, !!data_len);
- if (data_len)
- max80_spi_write_buffer(data, data_len, false);
- return 0;
- }
- static int max80_spi_read(void *cookie,
- const void *cmd, unsigned int cmd_len,
- void *data, unsigned int data_len,
- int tshsl)
- {
- (void)cookie;
- (void)tshsl; /* Enforced in hardware */
- if (cmd_len)
- max80_spi_write_buffer(cmd, cmd_len, !!data_len);
- if (data_len)
- max80_spi_read_buffer(data, data_len, false);
- return 0;
- }
- static const struct spiflash_ops max80_spiflash_ops = {
- .read_data = NULL, /* From memory buffer */
- .close_data = NULL, /* Nothing to do */
- .spi_write = max80_spi_write,
- .spi_read = max80_spi_read,
- .yield = NULL /* Nothing to yield to... */
- };
- /* Winbond W25Q128JV @ 3.3V */
- #define NS(x) (((x)*(unsigned long long)CPU_HZ + 999999999ULL)/1000000000ULL)
- #define US(x) (((x)*(unsigned long long)CPU_HZ + 999999ULL)/1000000ULL)
- #define MS(x) (((x)*(unsigned long long)CPU_HZ + 999ULL)/1000ULL)
- static const struct spiflash_param max80_spiflash_param = {
- /*
- * For W25Q128JV _DYNAMIC is equivalent to _24BIT, but specifying
- * it as dynamic would most of the time support larger parts
- * transparently.
- */
- .addr = SPIFLASH_ADDR_DYNAMIC,
- .tshsl = NS(3),
- .tshsl1 = NS(10),
- .tshsl2 = NS(50),
- .trst = NS(30000),
- .tw = MS(10), /* persistent; typ, max = 15 */
- .tpp = NS(400), /* typ, max = 3000 */
- .tse = MS(45), /* typ, max = 400 */
- .tbe1 = MS(120), /* typ, max = 1600 */
- .tbe2 = MS(150), /* typ, max = 2000 */
- .tce = MS(40000), /* typ, max = 200000 */
- };
- static struct spiflash max80_flash = {
- .ops = &max80_spiflash_ops,
- .cookie = NULL,
- .param = &max80_spiflash_param
- };
- /*
- * Flash an image into SPI flash, and reload the FPGA if successful,
- * returns on failure only.
- */
- void rom_flash_from_memory(void *buf, size_t buflen)
- {
- const struct spiflash_header *hdr = buf;
- if (hdr->magic != SPIFLASH_MAGIC)
- return;
- if (hdr->magic != SPIFLASH_MAGIC ||
- spiflash_flash_files(&max80_flash, buf, buflen)) {
- con_puts("update: flash update data invalid\n");
- return;
- }
- con_puts("update: Flash complete, restarting in 5 s...\n");
- udelay(5000000);
- reset(SYS_RESET_RECONFIG);
- }
|