cmd_fpga.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * FPGA support
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #if defined(CONFIG_CMD_NET)
  30. #include <net.h>
  31. #endif
  32. #include <fpga.h>
  33. #include <malloc.h>
  34. /* Local functions */
  35. static int fpga_get_op(char *opstr);
  36. /* Local defines */
  37. #define FPGA_NONE -1
  38. #define FPGA_INFO 0
  39. #define FPGA_LOAD 1
  40. #define FPGA_LOADB 2
  41. #define FPGA_DUMP 3
  42. #define FPGA_LOADMK 4
  43. /* ------------------------------------------------------------------------- */
  44. /* command form:
  45. * fpga <op> <device number> <data addr> <datasize>
  46. * where op is 'load', 'dump', or 'info'
  47. * If there is no device number field, the fpga environment variable is used.
  48. * If there is no data addr field, the fpgadata environment variable is used.
  49. * The info command requires no data address field.
  50. */
  51. int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  52. {
  53. int op, dev = FPGA_INVALID_DEVICE;
  54. size_t data_size = 0;
  55. void *fpga_data = NULL;
  56. char *devstr = getenv("fpga");
  57. char *datastr = getenv("fpgadata");
  58. int rc = FPGA_FAIL;
  59. int wrong_parms = 0;
  60. #if defined(CONFIG_FIT)
  61. const char *fit_uname = NULL;
  62. ulong fit_addr;
  63. #endif
  64. if (devstr)
  65. dev = (int) simple_strtoul(devstr, NULL, 16);
  66. if (datastr)
  67. fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
  68. switch (argc) {
  69. case 5: /* fpga <op> <dev> <data> <datasize> */
  70. data_size = simple_strtoul(argv[4], NULL, 16);
  71. case 4: /* fpga <op> <dev> <data> */
  72. #if defined(CONFIG_FIT)
  73. if (fit_parse_subimage(argv[3], (ulong)fpga_data,
  74. &fit_addr, &fit_uname)) {
  75. fpga_data = (void *)fit_addr;
  76. debug("* fpga: subimage '%s' from FIT image ",
  77. fit_uname);
  78. debug("at 0x%08lx\n", fit_addr);
  79. } else
  80. #endif
  81. {
  82. fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
  83. debug("* fpga: cmdline image address = 0x%08lx\n",
  84. (ulong)fpga_data);
  85. }
  86. debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
  87. case 3: /* fpga <op> <dev | data addr> */
  88. dev = (int)simple_strtoul(argv[2], NULL, 16);
  89. debug("%s: device = %d\n", __func__, dev);
  90. /* FIXME - this is a really weak test */
  91. if ((argc == 3) && (dev > fpga_count())) {
  92. /* must be buffer ptr */
  93. debug("%s: Assuming buffer pointer in arg 3\n",
  94. __func__);
  95. #if defined(CONFIG_FIT)
  96. if (fit_parse_subimage(argv[2], (ulong)fpga_data,
  97. &fit_addr, &fit_uname)) {
  98. fpga_data = (void *)fit_addr;
  99. debug("* fpga: subimage '%s' from FIT image ",
  100. fit_uname);
  101. debug("at 0x%08lx\n", fit_addr);
  102. } else
  103. #endif
  104. {
  105. fpga_data = (void *)dev;
  106. debug("* fpga: cmdline image addr = 0x%08lx\n",
  107. (ulong)fpga_data);
  108. }
  109. debug("%s: fpga_data = 0x%x\n",
  110. __func__, (uint)fpga_data);
  111. dev = FPGA_INVALID_DEVICE; /* reset device num */
  112. }
  113. case 2: /* fpga <op> */
  114. op = (int)fpga_get_op(argv[1]);
  115. break;
  116. default:
  117. debug("%s: Too many or too few args (%d)\n", __func__, argc);
  118. op = FPGA_NONE; /* force usage display */
  119. break;
  120. }
  121. if (dev == FPGA_INVALID_DEVICE) {
  122. puts("FPGA device not specified\n");
  123. op = FPGA_NONE;
  124. }
  125. switch (op) {
  126. case FPGA_NONE:
  127. case FPGA_INFO:
  128. break;
  129. case FPGA_LOAD:
  130. case FPGA_LOADB:
  131. case FPGA_DUMP:
  132. if (!fpga_data || !data_size)
  133. wrong_parms = 1;
  134. break;
  135. case FPGA_LOADMK:
  136. if (!fpga_data)
  137. wrong_parms = 1;
  138. break;
  139. }
  140. if (wrong_parms) {
  141. puts("Wrong parameters for FPGA request\n");
  142. op = FPGA_NONE;
  143. }
  144. switch (op) {
  145. case FPGA_NONE:
  146. return CMD_RET_USAGE;
  147. case FPGA_INFO:
  148. rc = fpga_info(dev);
  149. break;
  150. case FPGA_LOAD:
  151. rc = fpga_load(dev, fpga_data, data_size);
  152. break;
  153. case FPGA_LOADB:
  154. rc = fpga_loadbitstream(dev, fpga_data, data_size);
  155. break;
  156. case FPGA_LOADMK:
  157. switch (genimg_get_format(fpga_data)) {
  158. case IMAGE_FORMAT_LEGACY:
  159. {
  160. image_header_t *hdr =
  161. (image_header_t *)fpga_data;
  162. ulong data;
  163. data = (ulong)image_get_data(hdr);
  164. data_size = image_get_data_size(hdr);
  165. rc = fpga_load(dev, (void *)data, data_size);
  166. }
  167. break;
  168. #if defined(CONFIG_FIT)
  169. case IMAGE_FORMAT_FIT:
  170. {
  171. const void *fit_hdr = (const void *)fpga_data;
  172. int noffset;
  173. const void *fit_data;
  174. if (fit_uname == NULL) {
  175. puts("No FIT subimage unit name\n");
  176. return 1;
  177. }
  178. if (!fit_check_format(fit_hdr)) {
  179. puts("Bad FIT image format\n");
  180. return 1;
  181. }
  182. /* get fpga component image node offset */
  183. noffset = fit_image_get_node(fit_hdr,
  184. fit_uname);
  185. if (noffset < 0) {
  186. printf("Can't find '%s' FIT subimage\n",
  187. fit_uname);
  188. return 1;
  189. }
  190. /* verify integrity */
  191. if (!fit_image_check_hashes(fit_hdr, noffset)) {
  192. puts("Bad Data Hash\n");
  193. return 1;
  194. }
  195. /* get fpga subimage data address and length */
  196. if (fit_image_get_data(fit_hdr, noffset,
  197. &fit_data, &data_size)) {
  198. puts("Fpga subimage data not found\n");
  199. return 1;
  200. }
  201. rc = fpga_load(dev, fit_data, data_size);
  202. }
  203. break;
  204. #endif
  205. default:
  206. puts("** Unknown image type\n");
  207. rc = FPGA_FAIL;
  208. break;
  209. }
  210. break;
  211. case FPGA_DUMP:
  212. rc = fpga_dump(dev, fpga_data, data_size);
  213. break;
  214. default:
  215. printf("Unknown operation\n");
  216. return CMD_RET_USAGE;
  217. }
  218. return rc;
  219. }
  220. /*
  221. * Map op to supported operations. We don't use a table since we
  222. * would just have to relocate it from flash anyway.
  223. */
  224. static int fpga_get_op(char *opstr)
  225. {
  226. int op = FPGA_NONE;
  227. if (!strcmp("info", opstr))
  228. op = FPGA_INFO;
  229. else if (!strcmp("loadb", opstr))
  230. op = FPGA_LOADB;
  231. else if (!strcmp("load", opstr))
  232. op = FPGA_LOAD;
  233. else if (!strcmp("loadmk", opstr))
  234. op = FPGA_LOADMK;
  235. else if (!strcmp("dump", opstr))
  236. op = FPGA_DUMP;
  237. if (op == FPGA_NONE)
  238. printf("Unknown fpga operation \"%s\"\n", opstr);
  239. return op;
  240. }
  241. U_BOOT_CMD(fpga, 6, 1, do_fpga,
  242. "loadable FPGA image support",
  243. "[operation type] [device number] [image address] [image size]\n"
  244. "fpga operations:\n"
  245. " dump\t[dev]\t\t\tLoad device to memory buffer\n"
  246. " info\t[dev]\t\t\tlist known device information\n"
  247. " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
  248. " loadb\t[dev] [address] [size]\t"
  249. "Load device from bitstream buffer (Xilinx only)\n"
  250. " loadmk [dev] [address]\tLoad device generated with mkimage"
  251. #if defined(CONFIG_FIT)
  252. "\n"
  253. "\tFor loadmk operating on FIT format uImage address must include\n"
  254. "\tsubimage unit name in the form of addr:<subimg_uname>"
  255. #endif
  256. );