romcopy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. shutdown(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. static int rom_flash_from_sdcard_file(const char *filename)
  278. {
  279. struct spiflash max80_flash;
  280. char *fw_file_name = NULL;
  281. FRESULT fr = FR_NO_FILE;
  282. FILINFO fno;
  283. FIL f;
  284. fr = f_stat(filename, &fno);
  285. if (fr != FR_OK || (fno.fattrib & AM_DIR))
  286. return 0; /* No such firmware file found but no error */
  287. con_printf("update: firmware update file %s found\n", filename);
  288. /* Rename the firmware file to avoid repeated updates */
  289. size_t l = strlen(filename);
  290. fw_file_name = malloc(l+5);
  291. if (!fw_file_name)
  292. goto err;
  293. memcpy(fw_file_name, filename, l);
  294. for (unsigned int i = 1; i < 1000; i++) {
  295. snprintf(fw_file_name+l, 5, ".%03u", i);
  296. fr = f_rename(filename, fw_file_name);
  297. if (fr != FR_EXIST)
  298. break;
  299. }
  300. if (fr != FR_OK) {
  301. con_puts("update: unable to rename update file, skipping\n");
  302. goto err;
  303. }
  304. con_printf("update: renamed %s -> %s\n", filename, fw_file_name);
  305. rom_spiflash_init(&max80_flash);
  306. max80_flash.read_data = rom_sdcard_read_data;
  307. max80_flash.cookie = &f;
  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. goto err;
  312. }
  313. if (spiflash_flash_file(&max80_flash, NULL, 0)) {
  314. con_puts("update: flash update data invalid\n");
  315. goto err;
  316. }
  317. /* Now do it for real */
  318. f_rewind(&f);
  319. max80_flash.ops = &max80_spiflash_ops;
  320. if (spiflash_flash_file(&max80_flash, NULL, 0)) {
  321. con_puts("update: flash update failed\n");
  322. goto err;
  323. }
  324. f_close(&f);
  325. con_puts("update: flash complete, restarting in 500 ms...\n");
  326. udelay(500000);
  327. shutdown(SYS_RESET_RECONFIG);
  328. err:
  329. if (fr == FR_OK)
  330. f_close(&f);
  331. if (fw_file_name)
  332. free(fw_file_name);
  333. return -1;
  334. }
  335. /*
  336. * Check for a firmware upgrade file on the sd card
  337. */
  338. void rom_flash_from_sdcard(void)
  339. {
  340. static char versioned_filename[] = "/max80/v?.fw";
  341. /* Look for version-specific firmware file */
  342. versioned_filename[8] = SYS_BOARDFPGA + '0';
  343. rom_flash_from_sdcard_file(versioned_filename);
  344. /* Look for universal firmware file */
  345. rom_flash_from_sdcard_file("/max80/max80.fw");
  346. }
  347. static bool boardinfo_valid(const struct board_info *bi)
  348. {
  349. size_t len = bi->len;
  350. /* XXX: check CRC */
  351. return len >= 16 && len <= BOARDINFO_SIZE &&
  352. bi->magic[0] == BOARDINFO_MAGIC_1 &&
  353. bi->magic[1] == BOARDINFO_MAGIC_2;
  354. }
  355. /*
  356. * Initialize the board_info structure from board_info_raw
  357. */
  358. static void boardinfo_init(void)
  359. {
  360. const struct board_info *src = &board_info_raw.i;
  361. size_t len = src->len;
  362. if (!boardinfo_valid(src))
  363. len = 0; /* Bad structure */
  364. size_t rlen = Min(len, sizeof board_info);
  365. memcpy(&board_info, src, rlen);
  366. /* Erase any fields not included in ROM */
  367. if (rlen < len)
  368. memset((char *)&board_info + rlen, 0, sizeof board_info - rlen);
  369. /*
  370. * If no board version is specified, use the FPGA version as a
  371. * resonable guess. This might be useful if the FPGA firmware has
  372. * been uploaded via JTAG.
  373. */
  374. if (!board_info.version_str[0]) {
  375. snprintf(board_info.version_str, sizeof board_info.version_str,
  376. "MAX80 v%u", SYS_BOARDFPGA);
  377. }
  378. /* Convert serial number and export to USB */
  379. rom_mangle_serial();
  380. }
  381. /*
  382. * Update board_info if board_info_raw has changed, and write it to flash
  383. * in that case.
  384. */
  385. void rom_update_boardinfo(void)
  386. {
  387. struct spiflash max80_flash;
  388. union board_info_block *src = &board_info_raw;
  389. do_update_boardinfo = false;
  390. if (!boardinfo_valid(&src->i)) {
  391. con_printf("rom: bad board_info received\n");
  392. return;
  393. }
  394. if (!memcmp(&board_info_rom, src, src->i.len)) {
  395. con_printf("rom: board_info in flash unchanged\n");
  396. return;
  397. }
  398. con_printf("rom: updating board_info in flash\n");
  399. memset(&src->b[src->i.len], 0xff, BOARDINFO_SIZE - src->i.len);
  400. rom_spiflash_init(&max80_flash);
  401. spiflash_flash_data(&max80_flash, BOARDINFO_ADDR, src, BOARDINFO_SIZE);
  402. romcopy_download_boardinfo(&board_info_rom);
  403. boardinfo_init();
  404. waitfor(ROMCOPY_IRQ);
  405. }