romcopy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "boardinfo_fpga.h"
  8. #include <stdio.h>
  9. #define SPIROM_DUAL_MODE 1
  10. struct board_info board_info;
  11. __dram_noinit union board_info_block board_info_raw;
  12. static __dram_noinit union board_info_block board_info_rom;
  13. volatile bool do_update_boardinfo;
  14. static void boardinfo_init(void);
  15. static void __hot romcopy_download_absolute(void *dst, size_t offset, size_t len)
  16. {
  17. unsigned int cmd;
  18. unsigned int flags = ROMCOPY_SPI_CMDLEN(5) | ROMCOPY_WRITE_RAM;
  19. if (!len)
  20. return;
  21. if (SPIROM_DUAL_MODE) {
  22. cmd = ROM_FAST_READ_DUAL;
  23. flags |= ROMCOPY_SPI_DUAL;
  24. } else {
  25. cmd = ROM_FAST_READ;
  26. }
  27. ROMCOPY_RAMADDR = (size_t)dst;
  28. ROMCOPY_ROMCMD = offset + (cmd << 24);
  29. ROMCOPY_DATALEN = len | flags;
  30. }
  31. void __hot romcopy_download(void *dst, size_t offset, size_t len)
  32. {
  33. romcopy_download_absolute(dst, __rom_offset + offset, len);
  34. }
  35. void __hot romcopy_bzero(void *dst, size_t len)
  36. {
  37. if (!len)
  38. return;
  39. ROMCOPY_RAMADDR = (size_t)dst;
  40. ROMCOPY_ROMCMD = 0;
  41. ROMCOPY_DATALEN = len | ROMCOPY_ZERO_BUFFER | ROMCOPY_WRITE_RAM;
  42. }
  43. static void __hot romcopy_download_boardinfo(void *dst)
  44. {
  45. romcopy_download_absolute(dst, BOARDINFO_ADDR, BOARDINFO_SIZE);
  46. }
  47. /*
  48. * Convert the MAC address in board_info_raw into a hexadecimal string
  49. * and set it as the USB serial number.
  50. */
  51. static inline void rom_mangle_serial(void)
  52. {
  53. volatile uint32_t *udp = &usbdesc_rom[2];
  54. const uint8_t *mac = board_info.mac[0];
  55. for (int i = 0; i < 6; i++) {
  56. unsigned int v = *mac++;
  57. unsigned int c;
  58. c = (v >> 4)+'0';
  59. if (c > '9')
  60. c += 'A'-'9'-1;
  61. udp[0] = c;
  62. c = (v & 15)+'0';
  63. if (c > '9')
  64. c += 'A'-'9'-1;
  65. udp[2] = c;
  66. /* Skip dash between third and fourth byte */
  67. udp += (i == 2) ? 6 : 4;
  68. }
  69. }
  70. static inline __hot void romcopy_config_flash(void)
  71. {
  72. /* Enable writing volatile status register bits */
  73. ROMCOPY_ROMCMD = ROM_VOLATILE_SR_WRITE_ENABLE << 24;
  74. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(1);
  75. waitfor(ROMCOPY_IRQ);
  76. /* Write SR3 = 0; this sets the drive strength to maximum */
  77. ROMCOPY_ROMCMD = ROM_WRITE_SR3 << 24;
  78. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(2);
  79. waitfor(ROMCOPY_IRQ);
  80. }
  81. IRQHANDLER(romcopy,0)
  82. {
  83. static __sbss unsigned int romcopy_state;
  84. size_t len;
  85. switch (romcopy_state++) {
  86. case 0:
  87. /* Clear esplink as quickly as possible */
  88. len = __esplink_end - __esplink_start;
  89. romcopy_bzero(__esplink_start, len);
  90. break;
  91. case 1:
  92. /* Condition flash ROM */
  93. romcopy_config_flash();
  94. /* Start copy DRAM data */
  95. len = __dram_init_end - __dram_init_start;
  96. romcopy_download(__dram_init_start, 0, len);
  97. break;
  98. case 2:
  99. /* Copy board_info from ROM into board_info_raw */
  100. romcopy_download_boardinfo(&board_info_raw);
  101. break;
  102. case 3:
  103. /* Copy reference copy of board_info ROM block into board_info_rom */
  104. romcopy_download_boardinfo(&board_info_rom);
  105. /* Create sanitized board_info structure */
  106. boardinfo_init();
  107. break;
  108. case 4:
  109. /* Zero .dram.bss */
  110. len = __dram_bss_end - __dram_bss_start;
  111. romcopy_bzero(__dram_bss_start, len);
  112. break;
  113. default:
  114. mask_irq(ROMCOPY_IRQ);
  115. break;
  116. }
  117. }
  118. /*
  119. * SPI flash parameters and routines (for spiflash.c)
  120. */
  121. static const struct spiflash_ops max80_spiflash_ops;
  122. static const struct spiflash_param max80_spiflash_param;
  123. /*
  124. * SPI flash operations
  125. */
  126. static int max80_spi_write_buffer(const void *buf, unsigned int buflen,
  127. bool more)
  128. {
  129. const uint8_t *p = buf;
  130. uint32_t cmd = 0;
  131. unsigned int bytecmd = 0;
  132. unsigned int bitpos = 24;
  133. while (buflen) {
  134. cmd |= *p++ << bitpos;
  135. buflen--;
  136. bitpos -= 8;
  137. bytecmd += ROMCOPY_SPI_CMDLEN(1);
  138. if (!(buflen & 3)) {
  139. if (buflen || more)
  140. bytecmd |= ROMCOPY_SPI_MORE;
  141. waitfor(ROMCOPY_IRQ);
  142. ROMCOPY_ROMCMD = cmd;
  143. ROMCOPY_DATALEN = bytecmd;
  144. bytecmd = cmd = 0;
  145. bitpos = 24;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int max80_spi_read_buffer(void *buf, unsigned int buflen, bool more)
  151. {
  152. uint8_t *p = buf;
  153. unsigned int bytecmd;
  154. uint32_t v;
  155. if (!buflen)
  156. return 0;
  157. waitfor(ROMCOPY_IRQ);
  158. ROMCOPY_ROMCMD = 0;
  159. while (buflen > 4) {
  160. ROMCOPY_DATALEN = ROMCOPY_SPI_CMDLEN(4) | ROMCOPY_SPI_MORE;
  161. waitfor(ROMCOPY_IRQ);
  162. v = ROMCOPY_INPUT;
  163. p[0] = v >> 24;
  164. p[1] = v >> 16;
  165. p[2] = v >> 8;
  166. p[3] = v;
  167. p += 4;
  168. buflen -= 4;
  169. }
  170. bytecmd = ROMCOPY_SPI_CMDLEN(buflen);
  171. if (more)
  172. bytecmd |= ROMCOPY_SPI_MORE;
  173. ROMCOPY_DATALEN = bytecmd;
  174. waitfor(ROMCOPY_IRQ);
  175. v = ROMCOPY_INPUT;
  176. while (buflen) {
  177. p[--buflen] = v;
  178. v >>= 8;
  179. }
  180. return 0;
  181. }
  182. static int max80_spi_write(void *cookie,
  183. const void *cmd, unsigned int cmd_len,
  184. const void *data, unsigned int data_len,
  185. int tshsl)
  186. {
  187. (void)cookie;
  188. (void)tshsl; /* Enforced in hardware */
  189. waitfor(ROMCOPY_IRQ);
  190. udelay(1);
  191. if (cmd_len)
  192. max80_spi_write_buffer(cmd, cmd_len, !!data_len);
  193. if (data_len)
  194. max80_spi_write_buffer(data, data_len, false);
  195. return 0;
  196. }
  197. static int max80_spi_read(void *cookie,
  198. const void *cmd, unsigned int cmd_len,
  199. void *data, unsigned int data_len,
  200. int tshsl)
  201. {
  202. (void)cookie;
  203. (void)tshsl; /* Enforced in hardware */
  204. waitfor(ROMCOPY_IRQ);
  205. udelay(1);
  206. if (cmd_len)
  207. max80_spi_write_buffer(cmd, cmd_len, !!data_len);
  208. if (data_len)
  209. max80_spi_read_buffer(data, data_len, false);
  210. return 0;
  211. }
  212. static const struct spiflash_ops max80_spiflash_ops = {
  213. .spi_write = max80_spi_write,
  214. .spi_read = max80_spi_read,
  215. .yield = NULL /* Nothing to yield to... */
  216. };
  217. /* Winbond W25Q128JV @ 3.3V */
  218. #define NS(x) (((x)*(unsigned long long)CPU_HZ + 999999999ULL)/1000000000ULL)
  219. #define US(x) (((x)*(unsigned long long)CPU_HZ + 999999ULL)/1000000ULL)
  220. #define MS(x) (((x)*(unsigned long long)CPU_HZ + 999ULL)/1000ULL)
  221. static const struct spiflash_param max80_spiflash_param = {
  222. /*
  223. * For W25Q128JV _DYNAMIC is equivalent to _24BIT, but specifying
  224. * it as dynamic would most of the time support larger parts
  225. * transparently.
  226. */
  227. .addr = SPIFLASH_ADDR_DYNAMIC,
  228. .tshsl = NS(3),
  229. .tshsl1 = NS(10),
  230. .tshsl2 = NS(50),
  231. .trst = NS(30000),
  232. .tw = MS(10), /* persistent; typ, max = 15 */
  233. .tpp = NS(400), /* typ, max = 3000 */
  234. .tse = MS(45), /* typ, max = 400 */
  235. .tbe1 = MS(120), /* typ, max = 1600 */
  236. .tbe2 = MS(150), /* typ, max = 2000 */
  237. .tce = MS(40000), /* typ, max = 200000 */
  238. };
  239. static void rom_spiflash_init(struct spiflash *flash)
  240. {
  241. memset(flash, 0, sizeof *flash);
  242. flash->target = board_info.version_str;
  243. flash->param = &max80_spiflash_param;
  244. flash->ops = &max80_spiflash_ops;
  245. }
  246. /*
  247. * Flash an image into SPI flash, and reload the FPGA if successful,
  248. * returns on failure only.
  249. */
  250. void rom_flash_from_memory(void *buf, size_t buflen)
  251. {
  252. struct spiflash max80_flash;
  253. rom_spiflash_init(&max80_flash);
  254. if (spiflash_flash_file(&max80_flash, buf, buflen)) {
  255. con_puts("update: flash update data invalid\n");
  256. return;
  257. }
  258. /* Now do it for real */
  259. max80_flash.ops = &max80_spiflash_ops;
  260. if (spiflash_flash_file(&max80_flash, buf, buflen)) {
  261. con_puts("update: flash update failed\n");
  262. return;
  263. }
  264. con_puts("update: flash complete, restarting in 500 ms...\n");
  265. udelay(500000);
  266. reset(SYS_RESET_RECONFIG);
  267. }
  268. static int rom_sdcard_read_data(void *cookie, void *buf, unsigned int bufsize)
  269. {
  270. unsigned int bytesread;
  271. FRESULT fr = f_read(cookie, buf, bufsize, &bytesread);
  272. return fr == FR_OK ? bytesread : 0;
  273. }
  274. /*
  275. * Flash an image from an SD card, and reload the FPGA if successful
  276. */
  277. void rom_flash_from_sdcard(void)
  278. {
  279. struct spiflash max80_flash;
  280. char fw_orig_file_name[32];
  281. char fw_file_name[32];
  282. int l;
  283. FRESULT fr;
  284. FILINFO fno;
  285. FIL f;
  286. rom_spiflash_init(&max80_flash);
  287. max80_flash.read_data = rom_sdcard_read_data;
  288. max80_flash.cookie = &f;
  289. l = snprintf(fw_orig_file_name, sizeof fw_orig_file_name,
  290. "/max80/v%u.fw", SYS_BOARDFPGA);
  291. fr = f_stat(fw_orig_file_name, &fno);
  292. if (fr != FR_OK || (fno.fattrib & AM_DIR))
  293. return; /* No firmware file found */
  294. con_printf("update: firmware update file %s found\n", fw_orig_file_name);
  295. /* Rename the firmware file to avoid repeated updates */
  296. memcpy(fw_file_name, fw_orig_file_name, l);
  297. for (unsigned int i = 1; i < 100000; i++) {
  298. snprintf(fw_file_name+l, sizeof fw_file_name-l, ".%03u", i);
  299. fr = f_rename(fw_orig_file_name, fw_file_name);
  300. if (fr != FR_EXIST)
  301. break;
  302. }
  303. if (fr != FR_OK) {
  304. con_puts("update: unable to rename update file, skipping\n");
  305. return;
  306. }
  307. con_printf("update: renamed %s -> %s\n", fw_orig_file_name, fw_file_name);
  308. fr = f_open(&f, fw_file_name, FA_READ);
  309. if (fr != FR_OK) {
  310. con_puts("update: cannot open file, terminating\n");
  311. return;
  312. }
  313. if (spiflash_flash_file(&max80_flash, NULL, 0)) {
  314. con_puts("update: flash update data invalid\n");
  315. f_close(&f);
  316. return;
  317. }
  318. /* Now do it for real */
  319. f_rewind(&f);
  320. max80_flash.ops = &max80_spiflash_ops;
  321. if (spiflash_flash_file(&max80_flash, NULL, 0)) {
  322. con_puts("update: flash update failed\n");
  323. f_close(&f);
  324. return;
  325. }
  326. /* Close file and then umount the filesystem to force sync */
  327. f_close(&f);
  328. f_unmount("");
  329. con_puts("update: flash complete, restarting in 500 ms...\n");
  330. udelay(500000);
  331. reset(SYS_RESET_RECONFIG);
  332. }
  333. static bool boardinfo_valid(const struct board_info *bi)
  334. {
  335. size_t len = bi->len;
  336. /* XXX: check CRC */
  337. return len >= 16 && len <= BOARDINFO_SIZE &&
  338. bi->magic[0] == BOARDINFO_MAGIC_1 &&
  339. bi->magic[1] == BOARDINFO_MAGIC_2;
  340. }
  341. /*
  342. * Initialize the board_info structure from board_info_raw
  343. */
  344. static void boardinfo_init(void)
  345. {
  346. const struct board_info *src = &board_info_raw.i;
  347. size_t len = src->len;
  348. if (!boardinfo_valid(src))
  349. len = 0; /* Bad structure */
  350. size_t rlen = Min(len, sizeof board_info);
  351. memcpy(&board_info, src, rlen);
  352. /* Erase any fields not included in ROM */
  353. if (rlen < len)
  354. memset((char *)&board_info + rlen, 0, sizeof board_info - rlen);
  355. /* Convert serial number and export to USB */
  356. rom_mangle_serial();
  357. }
  358. /*
  359. * Update board_info if board_info_raw has changed, and write it to flash
  360. * in that case.
  361. */
  362. void rom_update_boardinfo(void)
  363. {
  364. struct spiflash max80_flash;
  365. union board_info_block *src = &board_info_raw;
  366. do_update_boardinfo = false;
  367. if (!boardinfo_valid(&src->i)) {
  368. con_printf("[ROM] bad board_info received\n");
  369. return;
  370. }
  371. if (!memcmp(&board_info_rom, src, src->i.len)) {
  372. con_printf("[ROM] board_info in flash unchanged\n");
  373. return;
  374. }
  375. con_printf("[ROM] updating board_info in flash\n");
  376. memset(&src->b[src->i.len], 0xff, BOARDINFO_SIZE - src->i.len);
  377. rom_spiflash_init(&max80_flash);
  378. spiflash_flash_data(&max80_flash, BOARDINFO_ADDR, src, BOARDINFO_SIZE);
  379. romcopy_download_boardinfo(&board_info_rom);
  380. boardinfo_init();
  381. waitfor(ROMCOPY_IRQ);
  382. }