cmd_fpga.c 9.5 KB

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