romcopy.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "fw.h"
  2. #include "console.h"
  3. #include "io.h"
  4. #include "spiflash.h"
  5. #define SPIROM_DUAL_MODE 1
  6. void __hot romcopy_download(void *dst, size_t offset, size_t len)
  7. {
  8. unsigned int cmd;
  9. unsigned int flags = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_WRITE_RAM;
  10. if (!len)
  11. return;
  12. if (SPIROM_DUAL_MODE) {
  13. cmd = ROM_FAST_READ_DUAL;
  14. flags |= ROMCOPY_SPI_DUAL;
  15. } else {
  16. cmd = ROM_FAST_READ;
  17. }
  18. ROMCOPY_RAMADDR = (size_t)dst;
  19. ROMCOPY_ROMCMD = __rom_offset + offset + (cmd << 24);
  20. ROMCOPY_DATALEN = len | flags;
  21. }
  22. void __hot romcopy_bzero(void *dst, size_t len)
  23. {
  24. if (!len)
  25. return;
  26. ROMCOPY_RAMADDR = (size_t)dst;
  27. ROMCOPY_ROMCMD = 0;
  28. ROMCOPY_DATALEN = len | ROMCOPY_ZERO_BUFFER | ROMCOPY_WRITE_RAM;
  29. }
  30. /*
  31. * Read unique serial number programmed into ROM
  32. *
  33. */
  34. char __bss_hot rom_serial_str[16];
  35. qword_t __bss_hot rom_serial;
  36. static __must_inline void rom_read_serial(void)
  37. {
  38. ROMCOPY_ROMCMD = ROM_READ_UNIQUE_ID << 24;
  39. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_MORE;
  40. waitfor(ROMCOPY_IRQ);
  41. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  42. waitfor(ROMCOPY_IRQ);
  43. rom_serial.l[1] = ROMCOPY_INPUT;
  44. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4);
  45. waitfor(ROMCOPY_IRQ);
  46. rom_serial.l[0] = ROMCOPY_INPUT;
  47. }
  48. /*
  49. * Convert the ROM serial number to a hex string and poke it into the
  50. * USB descriptor "ROM". Used to use base36, but this has to be done
  51. * very early, and it has turned out that making it consistent with
  52. * what one can easily get out of a debugger is really useful.
  53. *
  54. * Doing this as early as possible means a much better chance to see
  55. * the proper serial number during USB enumeration, so doing it
  56. * immediately after SPI ROM conditioning is a great time.
  57. */
  58. static __must_inline void rom_mangle_serial(void)
  59. {
  60. volatile uint32_t *udp = &usbdesc_rom[2];
  61. for (int i = 7; i >= 0; i--) {
  62. unsigned int v = rom_serial.b[i];
  63. unsigned int c;
  64. c = (v >> 4)+'0';
  65. if (c > '9')
  66. c += 'A'-'9'-1;
  67. udp[0] = c;
  68. c = (v & 15)+'0';
  69. if (c > '9')
  70. c += 'A'-'9'-1;
  71. udp[2] = c;
  72. udp += 4;
  73. }
  74. }
  75. void rom_print_serial(void)
  76. {
  77. /* Print the ROM serial when we actually can */
  78. con_printf("ROM serial: %08X%08X (%08x-%08x)\n",
  79. rom_serial.l[1], rom_serial.l[0],
  80. rom_serial.l[1], rom_serial.l[0]);
  81. }
  82. static __must_inline void romcopy_config_flash(void)
  83. {
  84. /* Enable writing volatile status register bits */
  85. ROMCOPY_ROMCMD = ROM_VOLATILE_SR_WRITE_ENABLE << 24;
  86. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(1);
  87. waitfor(ROMCOPY_IRQ);
  88. /* Write SR3 = 0; this sets the drive strength to maximum */
  89. ROMCOPY_ROMCMD = ROM_WRITE_SR3 << 24;
  90. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(2);
  91. waitfor(ROMCOPY_IRQ);
  92. }
  93. IRQHANDLER(romcopy,0)
  94. {
  95. static __sbss unsigned int romcopy_state;
  96. size_t len;
  97. switch (romcopy_state++) {
  98. case 0:
  99. /* Condition flash ROM */
  100. romcopy_config_flash();
  101. /* Read serial number */
  102. rom_read_serial();
  103. /* Start copy DRAM data */
  104. len = __dram_init_end - __dram_init_start;
  105. romcopy_download(__dram_init_start, 0, len);
  106. /* Convert serial number and export to USB */
  107. rom_mangle_serial();
  108. break;
  109. case 1:
  110. /* Zero .dram.bss */
  111. len = __dram_bss_end - __dram_bss_start;
  112. romcopy_bzero(__dram_bss_start, len);
  113. break;
  114. default:
  115. mask_irq(ROMCOPY_IRQ);
  116. break;
  117. }
  118. }
  119. /*
  120. * SPI flash parameters and routines (for spiflash.c)
  121. */
  122. static const struct spiflash_ops max80_spiflash_ops;
  123. static const struct spiflash_param max80_spiflash_param;
  124. /*
  125. * SPI flash operations
  126. */
  127. static int max80_spi_write_buffer(const void *buf, unsigned int buflen,
  128. bool more)
  129. {
  130. const uint8_t *p = buf;
  131. uint32_t cmd = 0;
  132. unsigned int bytecmd = 0;
  133. unsigned int bitpos = 24;
  134. while (buflen) {
  135. cmd |= *p++ << bitpos;
  136. buflen--;
  137. bitpos -= 8;
  138. bytecmd += ROMCOPY_SPI_CMDLEN(1);
  139. if (!(buflen & 3)) {
  140. if (buflen || more)
  141. bytecmd |= ROMCOPY_SPI_MORE;
  142. waitfor(ROMCOPY_IRQ);
  143. ROMCOPY_ROMCMD = cmd;
  144. ROMCOPY_DATALEN = bytecmd;
  145. bytecmd = cmd = 0;
  146. bitpos = 24;
  147. }
  148. }
  149. return 0;
  150. }
  151. static int max80_spi_read_buffer(void *buf, unsigned int buflen, bool more)
  152. {
  153. uint8_t *p = buf;
  154. unsigned int bytecmd;
  155. uint32_t v;
  156. if (!buflen)
  157. return 0;
  158. waitfor(ROMCOPY_IRQ);
  159. ROMCOPY_ROMCMD = 0;
  160. while (buflen > 4) {
  161. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  162. waitfor(ROMCOPY_IRQ);
  163. v = ROMCOPY_INPUT;
  164. p[0] = v >> 24;
  165. p[1] = v >> 16;
  166. p[2] = v >> 8;
  167. p[3] = v;
  168. p += 4;
  169. buflen -= 4;
  170. }
  171. bytecmd = ROMCOPY_SPI_CMDLEN(buflen);
  172. if (more)
  173. bytecmd |= ROMCOPY_SPI_MORE;
  174. ROMCOPY_DATALEN = bytecmd;
  175. waitfor(ROMCOPY_IRQ);
  176. v = ROMCOPY_INPUT;
  177. while (buflen) {
  178. p[--buflen] = v;
  179. v >>= 8;
  180. }
  181. return 0;
  182. }
  183. static int max80_spi_write(void *cookie,
  184. const void *cmd, unsigned int cmd_len,
  185. const void *data, unsigned int data_len,
  186. int tshsl)
  187. {
  188. (void)cookie;
  189. (void)tshsl; /* Enforced in hardware */
  190. waitfor(ROMCOPY_IRQ);
  191. udelay(1);
  192. if (cmd_len)
  193. max80_spi_write_buffer(cmd, cmd_len, !!data_len);
  194. if (data_len)
  195. max80_spi_write_buffer(data, data_len, false);
  196. return 0;
  197. }
  198. static int max80_spi_read(void *cookie,
  199. const void *cmd, unsigned int cmd_len,
  200. void *data, unsigned int data_len,
  201. int tshsl)
  202. {
  203. (void)cookie;
  204. (void)tshsl; /* Enforced in hardware */
  205. waitfor(ROMCOPY_IRQ);
  206. udelay(1);
  207. if (cmd_len)
  208. max80_spi_write_buffer(cmd, cmd_len, !!data_len);
  209. if (data_len)
  210. max80_spi_read_buffer(data, data_len, false);
  211. return 0;
  212. }
  213. static const struct spiflash_ops max80_spiflash_ops = {
  214. .read_data = NULL, /* From memory buffer */
  215. .close_data = NULL, /* Nothing to do */
  216. .spi_write = max80_spi_write,
  217. .spi_read = max80_spi_read,
  218. .yield = NULL /* Nothing to yield to... */
  219. };
  220. /* Winbond W25Q128JV @ 3.3V */
  221. #define NS(x) (((x)*(unsigned long long)CPU_HZ + 999999999ULL)/1000000000ULL)
  222. #define US(x) (((x)*(unsigned long long)CPU_HZ + 999999ULL)/1000000ULL)
  223. #define MS(x) (((x)*(unsigned long long)CPU_HZ + 999ULL)/1000ULL)
  224. static const struct spiflash_param max80_spiflash_param = {
  225. /*
  226. * For W25Q128JV _DYNAMIC is equivalent to _24BIT, but specifying
  227. * it as dynamic would most of the time support larger parts
  228. * transparently.
  229. */
  230. .addr = SPIFLASH_ADDR_DYNAMIC,
  231. .tshsl = NS(3),
  232. .tshsl1 = NS(10),
  233. .tshsl2 = NS(50),
  234. .trst = NS(30000),
  235. .tw = MS(10), /* persistent; typ, max = 15 */
  236. .tpp = NS(400), /* typ, max = 3000 */
  237. .tse = MS(45), /* typ, max = 400 */
  238. .tbe1 = MS(120), /* typ, max = 1600 */
  239. .tbe2 = MS(150), /* typ, max = 2000 */
  240. .tce = MS(40000), /* typ, max = 200000 */
  241. };
  242. static struct spiflash max80_flash = {
  243. .ops = &max80_spiflash_ops,
  244. .cookie = NULL,
  245. .param = &max80_spiflash_param
  246. };
  247. /*
  248. * Flash an image into SPI flash, and reload the FPGA if successful,
  249. * returns on failure only.
  250. */
  251. void rom_flash_from_memory(void *buf, size_t buflen)
  252. {
  253. const struct spiflash_header *hdr = buf;
  254. uint32_t romid[2];
  255. for (int i = 0; i < 8; i++)
  256. con_printf("%08x ", ((const uint32_t *)buf)[i]);
  257. con_putc('\n');
  258. if (hdr->magic != SPIFLASH_MAGIC) {
  259. con_printf("update: no flash update data found in memory at %p\n", hdr);
  260. return;
  261. }
  262. memset(romid, 0, sizeof romid);
  263. spiflash_read_id(&max80_flash, romid);
  264. con_printf("update: ROM serial number %08x-%08x\n", romid[0], romid[1]);
  265. if (spiflash_flash_files(&max80_flash, buf, buflen)) {
  266. con_puts("update: flash update data invalid\n");
  267. return;
  268. }
  269. con_puts("update: Flash complete, restarting in 5 s...\n");
  270. udelay(5000000);
  271. reset(SYS_RESET_RECONFIG);
  272. }