fpga.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * FPGA support
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <fpga.h>
  13. #include <fs.h>
  14. #include <malloc.h>
  15. /* Local functions */
  16. static int fpga_get_op(char *opstr);
  17. /* Local defines */
  18. enum {
  19. FPGA_NONE = -1,
  20. FPGA_INFO,
  21. FPGA_LOAD,
  22. FPGA_LOADB,
  23. FPGA_DUMP,
  24. FPGA_LOADMK,
  25. FPGA_LOADP,
  26. FPGA_LOADBP,
  27. FPGA_LOADFS,
  28. };
  29. /* ------------------------------------------------------------------------- */
  30. /* command form:
  31. * fpga <op> <device number> <data addr> <datasize>
  32. * where op is 'load', 'dump', or 'info'
  33. * If there is no device number field, the fpga environment variable is used.
  34. * If there is no data addr field, the fpgadata environment variable is used.
  35. * The info command requires no data address field.
  36. */
  37. int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  38. {
  39. int op, dev = FPGA_INVALID_DEVICE;
  40. size_t data_size = 0;
  41. void *fpga_data = NULL;
  42. char *devstr = getenv("fpga");
  43. char *datastr = getenv("fpgadata");
  44. int rc = FPGA_FAIL;
  45. int wrong_parms = 0;
  46. #if defined(CONFIG_FIT)
  47. const char *fit_uname = NULL;
  48. ulong fit_addr;
  49. #endif
  50. #if defined(CONFIG_CMD_FPGA_LOADFS)
  51. fpga_fs_info fpga_fsinfo;
  52. fpga_fsinfo.fstype = FS_TYPE_ANY;
  53. #endif
  54. if (devstr)
  55. dev = (int) simple_strtoul(devstr, NULL, 16);
  56. if (datastr)
  57. fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
  58. switch (argc) {
  59. #if defined(CONFIG_CMD_FPGA_LOADFS)
  60. case 9:
  61. fpga_fsinfo.blocksize = (unsigned int)
  62. simple_strtoul(argv[5], NULL, 16);
  63. fpga_fsinfo.interface = argv[6];
  64. fpga_fsinfo.dev_part = argv[7];
  65. fpga_fsinfo.filename = argv[8];
  66. #endif
  67. case 5: /* fpga <op> <dev> <data> <datasize> */
  68. data_size = simple_strtoul(argv[4], NULL, 16);
  69. case 4: /* fpga <op> <dev> <data> */
  70. #if defined(CONFIG_FIT)
  71. if (fit_parse_subimage(argv[3], (ulong)fpga_data,
  72. &fit_addr, &fit_uname)) {
  73. fpga_data = (void *)fit_addr;
  74. debug("* fpga: subimage '%s' from FIT image ",
  75. fit_uname);
  76. debug("at 0x%08lx\n", fit_addr);
  77. } else
  78. #endif
  79. {
  80. fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
  81. debug("* fpga: cmdline image address = 0x%08lx\n",
  82. (ulong)fpga_data);
  83. }
  84. debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
  85. case 3: /* fpga <op> <dev | data addr> */
  86. dev = (int)simple_strtoul(argv[2], NULL, 16);
  87. debug("%s: device = %d\n", __func__, dev);
  88. /* FIXME - this is a really weak test */
  89. if ((argc == 3) && (dev > fpga_count())) {
  90. /* must be buffer ptr */
  91. debug("%s: Assuming buffer pointer in arg 3\n",
  92. __func__);
  93. #if defined(CONFIG_FIT)
  94. if (fit_parse_subimage(argv[2], (ulong)fpga_data,
  95. &fit_addr, &fit_uname)) {
  96. fpga_data = (void *)fit_addr;
  97. debug("* fpga: subimage '%s' from FIT image ",
  98. fit_uname);
  99. debug("at 0x%08lx\n", fit_addr);
  100. } else
  101. #endif
  102. {
  103. fpga_data = (void *)(uintptr_t)dev;
  104. debug("* fpga: cmdline image addr = 0x%08lx\n",
  105. (ulong)fpga_data);
  106. }
  107. debug("%s: fpga_data = 0x%lx\n",
  108. __func__, (ulong)fpga_data);
  109. dev = FPGA_INVALID_DEVICE; /* reset device num */
  110. }
  111. case 2: /* fpga <op> */
  112. op = (int)fpga_get_op(argv[1]);
  113. break;
  114. default:
  115. debug("%s: Too many or too few args (%d)\n", __func__, argc);
  116. op = FPGA_NONE; /* force usage display */
  117. break;
  118. }
  119. if (dev == FPGA_INVALID_DEVICE) {
  120. puts("FPGA device not specified\n");
  121. op = FPGA_NONE;
  122. }
  123. switch (op) {
  124. case FPGA_NONE:
  125. case FPGA_INFO:
  126. break;
  127. #if defined(CONFIG_CMD_FPGA_LOADFS)
  128. case FPGA_LOADFS:
  129. /* Blocksize can be zero */
  130. if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
  131. !fpga_fsinfo.filename)
  132. wrong_parms = 1;
  133. #endif
  134. case FPGA_LOAD:
  135. case FPGA_LOADP:
  136. case FPGA_LOADB:
  137. case FPGA_LOADBP:
  138. case FPGA_DUMP:
  139. if (!fpga_data || !data_size)
  140. wrong_parms = 1;
  141. break;
  142. #if defined(CONFIG_CMD_FPGA_LOADMK)
  143. case FPGA_LOADMK:
  144. if (!fpga_data)
  145. wrong_parms = 1;
  146. break;
  147. #endif
  148. }
  149. if (wrong_parms) {
  150. puts("Wrong parameters for FPGA request\n");
  151. op = FPGA_NONE;
  152. }
  153. switch (op) {
  154. case FPGA_NONE:
  155. return CMD_RET_USAGE;
  156. case FPGA_INFO:
  157. rc = fpga_info(dev);
  158. break;
  159. case FPGA_LOAD:
  160. rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
  161. break;
  162. #if defined(CONFIG_CMD_FPGA_LOADP)
  163. case FPGA_LOADP:
  164. rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
  165. break;
  166. #endif
  167. case FPGA_LOADB:
  168. rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
  169. break;
  170. #if defined(CONFIG_CMD_FPGA_LOADBP)
  171. case FPGA_LOADBP:
  172. rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
  173. break;
  174. #endif
  175. #if defined(CONFIG_CMD_FPGA_LOADFS)
  176. case FPGA_LOADFS:
  177. rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
  178. break;
  179. #endif
  180. #if defined(CONFIG_CMD_FPGA_LOADMK)
  181. case FPGA_LOADMK:
  182. switch (genimg_get_format(fpga_data)) {
  183. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  184. case IMAGE_FORMAT_LEGACY:
  185. {
  186. image_header_t *hdr =
  187. (image_header_t *)fpga_data;
  188. ulong data;
  189. uint8_t comp;
  190. comp = image_get_comp(hdr);
  191. if (comp == IH_COMP_GZIP) {
  192. #if defined(CONFIG_GZIP)
  193. ulong image_buf = image_get_data(hdr);
  194. data = image_get_load(hdr);
  195. ulong image_size = ~0UL;
  196. if (gunzip((void *)data, ~0UL,
  197. (void *)image_buf,
  198. &image_size) != 0) {
  199. puts("GUNZIP: error\n");
  200. return 1;
  201. }
  202. data_size = image_size;
  203. #else
  204. puts("Gunzip image is not supported\n");
  205. return 1;
  206. #endif
  207. } else {
  208. data = (ulong)image_get_data(hdr);
  209. data_size = image_get_data_size(hdr);
  210. }
  211. rc = fpga_load(dev, (void *)data, data_size,
  212. BIT_FULL);
  213. }
  214. break;
  215. #endif
  216. #if defined(CONFIG_FIT)
  217. case IMAGE_FORMAT_FIT:
  218. {
  219. const void *fit_hdr = (const void *)fpga_data;
  220. int noffset;
  221. const void *fit_data;
  222. if (fit_uname == NULL) {
  223. puts("No FIT subimage unit name\n");
  224. return 1;
  225. }
  226. if (!fit_check_format(fit_hdr)) {
  227. puts("Bad FIT image format\n");
  228. return 1;
  229. }
  230. /* get fpga component image node offset */
  231. noffset = fit_image_get_node(fit_hdr,
  232. fit_uname);
  233. if (noffset < 0) {
  234. printf("Can't find '%s' FIT subimage\n",
  235. fit_uname);
  236. return 1;
  237. }
  238. /* verify integrity */
  239. if (!fit_image_verify(fit_hdr, noffset)) {
  240. puts ("Bad Data Hash\n");
  241. return 1;
  242. }
  243. /* get fpga subimage data address and length */
  244. if (fit_image_get_data(fit_hdr, noffset,
  245. &fit_data, &data_size)) {
  246. puts("Fpga subimage data not found\n");
  247. return 1;
  248. }
  249. rc = fpga_load(dev, fit_data, data_size,
  250. BIT_FULL);
  251. }
  252. break;
  253. #endif
  254. default:
  255. puts("** Unknown image type\n");
  256. rc = FPGA_FAIL;
  257. break;
  258. }
  259. break;
  260. #endif
  261. case FPGA_DUMP:
  262. rc = fpga_dump(dev, fpga_data, data_size);
  263. break;
  264. default:
  265. printf("Unknown operation\n");
  266. return CMD_RET_USAGE;
  267. }
  268. return rc;
  269. }
  270. /*
  271. * Map op to supported operations. We don't use a table since we
  272. * would just have to relocate it from flash anyway.
  273. */
  274. static int fpga_get_op(char *opstr)
  275. {
  276. int op = FPGA_NONE;
  277. if (!strcmp("info", opstr))
  278. op = FPGA_INFO;
  279. else if (!strcmp("loadb", opstr))
  280. op = FPGA_LOADB;
  281. else if (!strcmp("load", opstr))
  282. op = FPGA_LOAD;
  283. #if defined(CONFIG_CMD_FPGA_LOADP)
  284. else if (!strcmp("loadp", opstr))
  285. op = FPGA_LOADP;
  286. #endif
  287. #if defined(CONFIG_CMD_FPGA_LOADBP)
  288. else if (!strcmp("loadbp", opstr))
  289. op = FPGA_LOADBP;
  290. #endif
  291. #if defined(CONFIG_CMD_FPGA_LOADFS)
  292. else if (!strcmp("loadfs", opstr))
  293. op = FPGA_LOADFS;
  294. #endif
  295. #if defined(CONFIG_CMD_FPGA_LOADMK)
  296. else if (!strcmp("loadmk", opstr))
  297. op = FPGA_LOADMK;
  298. #endif
  299. else if (!strcmp("dump", opstr))
  300. op = FPGA_DUMP;
  301. if (op == FPGA_NONE)
  302. printf("Unknown fpga operation \"%s\"\n", opstr);
  303. return op;
  304. }
  305. #if defined(CONFIG_CMD_FPGA_LOADFS)
  306. U_BOOT_CMD(fpga, 9, 1, do_fpga,
  307. #else
  308. U_BOOT_CMD(fpga, 6, 1, do_fpga,
  309. #endif
  310. "loadable FPGA image support",
  311. "[operation type] [device number] [image address] [image size]\n"
  312. "fpga operations:\n"
  313. " dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
  314. " info\t[dev]\t\t\tlist known device information\n"
  315. " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
  316. #if defined(CONFIG_CMD_FPGA_LOADP)
  317. " loadp\t[dev] [address] [size]\t"
  318. "Load device from memory buffer with partial bitstream\n"
  319. #endif
  320. " loadb\t[dev] [address] [size]\t"
  321. "Load device from bitstream buffer (Xilinx only)\n"
  322. #if defined(CONFIG_CMD_FPGA_LOADBP)
  323. " loadbp\t[dev] [address] [size]\t"
  324. "Load device from bitstream buffer with partial bitstream"
  325. "(Xilinx only)\n"
  326. #endif
  327. #if defined(CONFIG_CMD_FPGA_LOADFS)
  328. "Load device from filesystem (FAT by default) (Xilinx only)\n"
  329. " loadfs [dev] [address] [image size] [blocksize] <interface>\n"
  330. " [<dev[:part]>] <filename>\n"
  331. #endif
  332. #if defined(CONFIG_CMD_FPGA_LOADMK)
  333. " loadmk [dev] [address]\tLoad device generated with mkimage"
  334. #if defined(CONFIG_FIT)
  335. "\n"
  336. "\tFor loadmk operating on FIT format uImage address must include\n"
  337. "\tsubimage unit name in the form of addr:<subimg_uname>"
  338. #endif
  339. #endif
  340. );