fpga.c 9.0 KB

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