romcopy.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "compiler.h"
  2. #include "common.h"
  3. #include "console.h"
  4. #include "io.h"
  5. #include "spiflash.h"
  6. #include "ff.h"
  7. #include <stdio.h>
  8. #define SPIROM_DUAL_MODE 1
  9. void __hot romcopy_download(void *dst, size_t offset, size_t len)
  10. {
  11. unsigned int cmd;
  12. unsigned int flags = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_WRITE_RAM;
  13. if (!len)
  14. return;
  15. if (SPIROM_DUAL_MODE) {
  16. cmd = ROM_FAST_READ_DUAL;
  17. flags |= ROMCOPY_SPI_DUAL;
  18. } else {
  19. cmd = ROM_FAST_READ;
  20. }
  21. ROMCOPY_RAMADDR = (size_t)dst;
  22. ROMCOPY_ROMCMD = __rom_offset + offset + (cmd << 24);
  23. ROMCOPY_DATALEN = len | flags;
  24. }
  25. void __hot romcopy_bzero(void *dst, size_t len)
  26. {
  27. if (!len)
  28. return;
  29. ROMCOPY_RAMADDR = (size_t)dst;
  30. ROMCOPY_ROMCMD = 0;
  31. ROMCOPY_DATALEN = len | ROMCOPY_ZERO_BUFFER | ROMCOPY_WRITE_RAM;
  32. }
  33. /*
  34. * Read unique serial number programmed into ROM
  35. *
  36. */
  37. char __bss_hot rom_serial_str[16];
  38. qword_t __bss_hot rom_serial;
  39. static __must_inline void rom_read_serial(void)
  40. {
  41. ROMCOPY_ROMCMD = ROM_READ_UNIQUE_ID << 24;
  42. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_SPI_MORE;
  43. waitfor(ROMCOPY_IRQ);
  44. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  45. waitfor(ROMCOPY_IRQ);
  46. rom_serial.l[1] = ROMCOPY_INPUT;
  47. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4);
  48. waitfor(ROMCOPY_IRQ);
  49. rom_serial.l[0] = ROMCOPY_INPUT;
  50. }
  51. /*
  52. * Convert the ROM serial number to a hex string and poke it into the
  53. * USB descriptor "ROM". Used to use base36, but this has to be done
  54. * very early, and it has turned out that making it consistent with
  55. * what one can easily get out of a debugger is really useful.
  56. *
  57. * Doing this as early as possible means a much better chance to see
  58. * the proper serial number during USB enumeration, so doing it
  59. * immediately after SPI ROM conditioning is a great time.
  60. */
  61. static __must_inline void rom_mangle_serial(void)
  62. {
  63. volatile uint32_t *udp = &usbdesc_rom[2];
  64. for (int i = 7; i >= 0; i--) {
  65. unsigned int v = rom_serial.b[i];
  66. unsigned int c;
  67. c = (v >> 4)+'0';
  68. if (c > '9')
  69. c += 'A'-'9'-1;
  70. udp[0] = c;
  71. c = (v & 15)+'0';
  72. if (c > '9')
  73. c += 'A'-'9'-1;
  74. udp[2] = c;
  75. udp += 4;
  76. }
  77. }
  78. void rom_print_serial(void)
  79. {
  80. /* Print the ROM serial when we actually can */
  81. con_printf("ROM serial: %08X%08X (%08x-%08x)\n",
  82. rom_serial.l[1], rom_serial.l[0],
  83. rom_serial.l[1], rom_serial.l[0]);
  84. }
  85. static __must_inline void romcopy_config_flash(void)
  86. {
  87. /* Enable writing volatile status register bits */
  88. ROMCOPY_ROMCMD = ROM_VOLATILE_SR_WRITE_ENABLE << 24;
  89. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(1);
  90. waitfor(ROMCOPY_IRQ);
  91. /* Write SR3 = 0; this sets the drive strength to maximum */
  92. ROMCOPY_ROMCMD = ROM_WRITE_SR3 << 24;
  93. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(2);
  94. waitfor(ROMCOPY_IRQ);
  95. }
  96. IRQHANDLER(romcopy,0)
  97. {
  98. static __sbss unsigned int romcopy_state;
  99. size_t len;
  100. switch (romcopy_state++) {
  101. case 0:
  102. /* Clear esplink as quickly as possible */
  103. len = __esplink_end - __esplink_start;
  104. romcopy_bzero(__esplink_start, len);
  105. break;
  106. case 1:
  107. /* Condition flash ROM */
  108. romcopy_config_flash();
  109. /* Read serial number */
  110. rom_read_serial();
  111. /* Start copy DRAM data */
  112. len = __dram_init_end - __dram_init_start;
  113. romcopy_download(__dram_init_start, 0, len);
  114. /* Convert serial number and export to USB */
  115. rom_mangle_serial();
  116. break;
  117. case 2:
  118. /* Zero .dram.bss */
  119. len = __dram_bss_end - __dram_bss_start;
  120. romcopy_bzero(__dram_bss_start, len);
  121. break;
  122. default:
  123. mask_irq(ROMCOPY_IRQ);
  124. break;
  125. }
  126. }
  127. /*
  128. * SPI flash parameters and routines (for spiflash.c)
  129. */
  130. static const struct spiflash_ops max80_spiflash_ops;
  131. static const struct spiflash_param max80_spiflash_param;
  132. /*
  133. * SPI flash operations
  134. */
  135. static int max80_spi_write_buffer(const void *buf, unsigned int buflen,
  136. bool more)
  137. {
  138. const uint8_t *p = buf;
  139. uint32_t cmd = 0;
  140. unsigned int bytecmd = 0;
  141. unsigned int bitpos = 24;
  142. while (buflen) {
  143. cmd |= *p++ << bitpos;
  144. buflen--;
  145. bitpos -= 8;
  146. bytecmd += ROMCOPY_SPI_CMDLEN(1);
  147. if (!(buflen & 3)) {
  148. if (buflen || more)
  149. bytecmd |= ROMCOPY_SPI_MORE;
  150. waitfor(ROMCOPY_IRQ);
  151. ROMCOPY_ROMCMD = cmd;
  152. ROMCOPY_DATALEN = bytecmd;
  153. bytecmd = cmd = 0;
  154. bitpos = 24;
  155. }
  156. }
  157. return 0;
  158. }
  159. static int max80_spi_read_buffer(void *buf, unsigned int buflen, bool more)
  160. {
  161. uint8_t *p = buf;
  162. unsigned int bytecmd;
  163. uint32_t v;
  164. if (!buflen)
  165. return 0;
  166. waitfor(ROMCOPY_IRQ);
  167. ROMCOPY_ROMCMD = 0;
  168. while (buflen > 4) {
  169. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  170. waitfor(ROMCOPY_IRQ);
  171. v = ROMCOPY_INPUT;
  172. p[0] = v >> 24;
  173. p[1] = v >> 16;
  174. p[2] = v >> 8;
  175. p[3] = v;
  176. p += 4;
  177. buflen -= 4;
  178. }
  179. bytecmd = ROMCOPY_SPI_CMDLEN(buflen);
  180. if (more)
  181. bytecmd |= ROMCOPY_SPI_MORE;
  182. ROMCOPY_DATALEN = bytecmd;
  183. waitfor(ROMCOPY_IRQ);
  184. v = ROMCOPY_INPUT;
  185. while (buflen) {
  186. p[--buflen] = v;
  187. v >>= 8;
  188. }
  189. return 0;
  190. }
  191. static int max80_spi_write(void *cookie,
  192. const void *cmd, unsigned int cmd_len,
  193. const void *data, unsigned int data_len,
  194. int tshsl)
  195. {
  196. (void)cookie;
  197. (void)tshsl; /* Enforced in hardware */
  198. waitfor(ROMCOPY_IRQ);
  199. udelay(1);
  200. if (cmd_len)
  201. max80_spi_write_buffer(cmd, cmd_len, !!data_len);
  202. if (data_len)
  203. max80_spi_write_buffer(data, data_len, false);
  204. return 0;
  205. }
  206. static int max80_spi_read(void *cookie,
  207. const void *cmd, unsigned int cmd_len,
  208. void *data, unsigned int data_len,
  209. int tshsl)
  210. {
  211. (void)cookie;
  212. (void)tshsl; /* Enforced in hardware */
  213. waitfor(ROMCOPY_IRQ);
  214. udelay(1);
  215. if (cmd_len)
  216. max80_spi_write_buffer(cmd, cmd_len, !!data_len);
  217. if (data_len)
  218. max80_spi_read_buffer(data, data_len, false);
  219. return 0;
  220. }
  221. static const struct spiflash_ops max80_spiflash_ops = {
  222. .spi_write = max80_spi_write,
  223. .spi_read = max80_spi_read,
  224. .yield = NULL /* Nothing to yield to... */
  225. };
  226. /* Winbond W25Q128JV @ 3.3V */
  227. #define NS(x) (((x)*(unsigned long long)CPU_HZ + 999999999ULL)/1000000000ULL)
  228. #define US(x) (((x)*(unsigned long long)CPU_HZ + 999999ULL)/1000000ULL)
  229. #define MS(x) (((x)*(unsigned long long)CPU_HZ + 999ULL)/1000ULL)
  230. static const struct spiflash_param max80_spiflash_param = {
  231. /*
  232. * For W25Q128JV _DYNAMIC is equivalent to _24BIT, but specifying
  233. * it as dynamic would most of the time support larger parts
  234. * transparently.
  235. */
  236. .addr = SPIFLASH_ADDR_DYNAMIC,
  237. .tshsl = NS(3),
  238. .tshsl1 = NS(10),
  239. .tshsl2 = NS(50),
  240. .trst = NS(30000),
  241. .tw = MS(10), /* persistent; typ, max = 15 */
  242. .tpp = NS(400), /* typ, max = 3000 */
  243. .tse = MS(45), /* typ, max = 400 */
  244. .tbe1 = MS(120), /* typ, max = 1600 */
  245. .tbe2 = MS(150), /* typ, max = 2000 */
  246. .tce = MS(40000), /* typ, max = 200000 */
  247. };
  248. static void rom_spiflash_init(struct spiflash *flash)
  249. {
  250. static char target[] = "MAX80 v?";
  251. memset(flash, 0, sizeof *flash);
  252. target[sizeof target - 2] = SYS_BOARDFPGA + '0';
  253. flash->target = target;
  254. flash->param = &max80_spiflash_param;
  255. }
  256. /*
  257. * Flash an image into SPI flash, and reload the FPGA if successful,
  258. * returns on failure only.
  259. */
  260. void rom_flash_from_memory(void *buf, size_t buflen)
  261. {
  262. struct spiflash max80_flash;
  263. rom_spiflash_init(&max80_flash);
  264. if (spiflash_flash_file(&max80_flash, buf, buflen)) {
  265. con_puts("update: flash update data invalid\n");
  266. return;
  267. }
  268. /* Now do it for real */
  269. max80_flash.ops = &max80_spiflash_ops;
  270. if (spiflash_flash_file(&max80_flash, buf, buflen)) {
  271. con_puts("update: flash update failed\n");
  272. return;
  273. }
  274. con_puts("update: flash complete, restarting in 500 ms...\n");
  275. udelay(500000);
  276. reset(SYS_RESET_RECONFIG);
  277. }
  278. static int rom_sdcard_read_data(void *cookie, void *buf, unsigned int bufsize)
  279. {
  280. unsigned int bytesread;
  281. FRESULT fr = f_read(cookie, buf, bufsize, &bytesread);
  282. return fr == FR_OK ? bytesread : 0;
  283. }
  284. /*
  285. * Flash an image from an SD card, and reload the FPGA if successful
  286. */
  287. void rom_flash_from_sdcard(void)
  288. {
  289. struct spiflash max80_flash;
  290. char fw_orig_file_name[32];
  291. char fw_file_name[32];
  292. int l;
  293. FRESULT fr;
  294. FILINFO fno;
  295. FIL f;
  296. rom_spiflash_init(&max80_flash);
  297. max80_flash.read_data = rom_sdcard_read_data;
  298. max80_flash.cookie = &f;
  299. l = snprintf(fw_orig_file_name, sizeof fw_orig_file_name,
  300. "/max80/v%u.fw", SYS_BOARDFPGA);
  301. fr = f_stat(fw_orig_file_name, &fno);
  302. if (fr != FR_OK || (fno.fattrib & AM_DIR))
  303. return; /* No firmware file found */
  304. con_printf("update: firmware update file %s found\n", fw_orig_file_name);
  305. /* Rename the firmware file to avoid repeated updates */
  306. memcpy(fw_file_name, fw_orig_file_name, l);
  307. for (unsigned int i = 1; i < 100000; i++) {
  308. snprintf(fw_file_name+l, sizeof fw_file_name-l, ".%03u", i);
  309. fr = f_rename(fw_orig_file_name, fw_file_name);
  310. if (fr != FR_EXIST)
  311. break;
  312. }
  313. if (fr != FR_OK) {
  314. con_puts("update: unable to rename update file, skipping\n");
  315. return;
  316. }
  317. con_printf("update: renamed %s -> %s\n", fw_orig_file_name, fw_file_name);
  318. fr = f_open(&f, fw_file_name, FA_READ);
  319. if (fr != FR_OK) {
  320. con_puts("update: cannot open file, terminating\n");
  321. return;
  322. }
  323. if (spiflash_flash_file(&max80_flash, NULL, 0)) {
  324. con_puts("update: flash update data invalid\n");
  325. f_close(&f);
  326. return;
  327. }
  328. /* Now do it for real */
  329. f_rewind(&f);
  330. max80_flash.ops = &max80_spiflash_ops;
  331. if (spiflash_flash_file(&max80_flash, NULL, 0)) {
  332. con_puts("update: flash update failed\n");
  333. f_close(&f);
  334. return;
  335. }
  336. /* Close file and then umount the filesystem to force sync */
  337. f_close(&f);
  338. f_unmount("");
  339. con_puts("update: flash complete, restarting in 500 ms...\n");
  340. udelay(500000);
  341. reset(SYS_RESET_RECONFIG);
  342. }