fpga.c 9.0 KB

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