blob: 010cd24e63dc21f86f0ae1910a4c4fac9d0e20e4 [file] [log] [blame]
wdenk4a9cbbe2002-08-27 09:48:53 +00001/*
2 * (C) Copyright 2000, 2001
3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk4a9cbbe2002-08-27 09:48:53 +00006 */
7
8/*
9 * FPGA support
10 */
11#include <common.h>
12#include <command.h>
wdenk8bde7f72003-06-27 21:31:46 +000013#include <fpga.h>
wdenkc3d2b4b2005-01-22 18:13:04 +000014#include <malloc.h>
wdenk4a9cbbe2002-08-27 09:48:53 +000015
wdenk4a9cbbe2002-08-27 09:48:53 +000016/* Local functions */
Michal Simekfc598412013-04-26 13:10:07 +020017static int fpga_get_op(char *opstr);
wdenk4a9cbbe2002-08-27 09:48:53 +000018
19/* Local defines */
20#define FPGA_NONE -1
21#define FPGA_INFO 0
22#define FPGA_LOAD 1
wdenk30ce5ab2005-01-09 18:12:51 +000023#define FPGA_LOADB 2
wdenk4a9cbbe2002-08-27 09:48:53 +000024#define FPGA_DUMP 3
Stefan Roesef0ff4692006-08-15 14:15:51 +020025#define FPGA_LOADMK 4
wdenk4a9cbbe2002-08-27 09:48:53 +000026
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 */
Michal Simekfc598412013-04-26 13:10:07 +020035int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenk4a9cbbe2002-08-27 09:48:53 +000036{
wdenkd4ca31c2004-01-02 14:00:00 +000037 int op, dev = FPGA_INVALID_DEVICE;
38 size_t data_size = 0;
39 void *fpga_data = NULL;
Michal Simekfc598412013-04-26 13:10:07 +020040 char *devstr = getenv("fpga");
41 char *datastr = getenv("fpgadata");
wdenkd4ca31c2004-01-02 14:00:00 +000042 int rc = FPGA_FAIL;
Stefano Babica790b5b2010-10-19 09:22:52 +020043 int wrong_parms = 0;
Michal Simekfc598412013-04-26 13:10:07 +020044#if defined(CONFIG_FIT)
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010045 const char *fit_uname = NULL;
46 ulong fit_addr;
47#endif
wdenk4a9cbbe2002-08-27 09:48:53 +000048
wdenkd4ca31c2004-01-02 14:00:00 +000049 if (devstr)
Michal Simekfc598412013-04-26 13:10:07 +020050 dev = (int) simple_strtoul(devstr, NULL, 16);
wdenkd4ca31c2004-01-02 14:00:00 +000051 if (datastr)
Michal Simekfc598412013-04-26 13:10:07 +020052 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
wdenk4a9cbbe2002-08-27 09:48:53 +000053
wdenkd4ca31c2004-01-02 14:00:00 +000054 switch (argc) {
55 case 5: /* fpga <op> <dev> <data> <datasize> */
Michal Simekfc598412013-04-26 13:10:07 +020056 data_size = simple_strtoul(argv[4], NULL, 16);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010057
wdenkd4ca31c2004-01-02 14:00:00 +000058 case 4: /* fpga <op> <dev> <data> */
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010059#if defined(CONFIG_FIT)
Michal Simekfc598412013-04-26 13:10:07 +020060 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
61 &fit_addr, &fit_uname)) {
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010062 fpga_data = (void *)fit_addr;
Michal Simekfc598412013-04-26 13:10:07 +020063 debug("* fpga: subimage '%s' from FIT image ",
64 fit_uname);
65 debug("at 0x%08lx\n", fit_addr);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010066 } else
67#endif
68 {
Michal Simekfc598412013-04-26 13:10:07 +020069 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
Stefano Babic06297db2011-12-28 06:47:01 +000070 debug("* fpga: cmdline image address = 0x%08lx\n",
Michal Simekfc598412013-04-26 13:10:07 +020071 (ulong)fpga_data);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010072 }
Michal Simekfc598412013-04-26 13:10:07 +020073 debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010074
wdenkd4ca31c2004-01-02 14:00:00 +000075 case 3: /* fpga <op> <dev | data addr> */
Michal Simekfc598412013-04-26 13:10:07 +020076 dev = (int)simple_strtoul(argv[2], NULL, 16);
Stefano Babic06297db2011-12-28 06:47:01 +000077 debug("%s: device = %d\n", __func__, dev);
wdenkd4ca31c2004-01-02 14:00:00 +000078 /* FIXME - this is a really weak test */
Michal Simekfc598412013-04-26 13:10:07 +020079 if ((argc == 3) && (dev > fpga_count())) {
80 /* must be buffer ptr */
Stefano Babic06297db2011-12-28 06:47:01 +000081 debug("%s: Assuming buffer pointer in arg 3\n",
Michal Simekfc598412013-04-26 13:10:07 +020082 __func__);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010083
84#if defined(CONFIG_FIT)
Michal Simekfc598412013-04-26 13:10:07 +020085 if (fit_parse_subimage(argv[2], (ulong)fpga_data,
86 &fit_addr, &fit_uname)) {
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010087 fpga_data = (void *)fit_addr;
Michal Simekfc598412013-04-26 13:10:07 +020088 debug("* fpga: subimage '%s' from FIT image ",
89 fit_uname);
90 debug("at 0x%08lx\n", fit_addr);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010091 } else
92#endif
93 {
Michal Simekfc598412013-04-26 13:10:07 +020094 fpga_data = (void *)dev;
95 debug("* fpga: cmdline image addr = 0x%08lx\n",
96 (ulong)fpga_data);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010097 }
98
Stefano Babic06297db2011-12-28 06:47:01 +000099 debug("%s: fpga_data = 0x%x\n",
Michal Simekfc598412013-04-26 13:10:07 +0200100 __func__, (uint)fpga_data);
wdenkd4ca31c2004-01-02 14:00:00 +0000101 dev = FPGA_INVALID_DEVICE; /* reset device num */
102 }
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100103
wdenkd4ca31c2004-01-02 14:00:00 +0000104 case 2: /* fpga <op> */
Michal Simekfc598412013-04-26 13:10:07 +0200105 op = (int)fpga_get_op(argv[1]);
wdenkd4ca31c2004-01-02 14:00:00 +0000106 break;
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100107
wdenkd4ca31c2004-01-02 14:00:00 +0000108 default:
Michal Simekfc598412013-04-26 13:10:07 +0200109 debug("%s: Too many or too few args (%d)\n", __func__, argc);
wdenkd4ca31c2004-01-02 14:00:00 +0000110 op = FPGA_NONE; /* force usage display */
111 break;
112 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000113
Stefano Babica790b5b2010-10-19 09:22:52 +0200114 if (dev == FPGA_INVALID_DEVICE) {
115 puts("FPGA device not specified\n");
116 op = FPGA_NONE;
117 }
118
119 switch (op) {
120 case FPGA_NONE:
121 case FPGA_INFO:
122 break;
123 case FPGA_LOAD:
124 case FPGA_LOADB:
125 case FPGA_DUMP:
126 if (!fpga_data || !data_size)
127 wrong_parms = 1;
128 break;
129 case FPGA_LOADMK:
130 if (!fpga_data)
131 wrong_parms = 1;
132 break;
133 }
134
135 if (wrong_parms) {
136 puts("Wrong parameters for FPGA request\n");
137 op = FPGA_NONE;
138 }
139
wdenkd4ca31c2004-01-02 14:00:00 +0000140 switch (op) {
141 case FPGA_NONE:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000142 return CMD_RET_USAGE;
wdenk4a9cbbe2002-08-27 09:48:53 +0000143
wdenkd4ca31c2004-01-02 14:00:00 +0000144 case FPGA_INFO:
Michal Simekfc598412013-04-26 13:10:07 +0200145 rc = fpga_info(dev);
wdenkd4ca31c2004-01-02 14:00:00 +0000146 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000147
wdenkd4ca31c2004-01-02 14:00:00 +0000148 case FPGA_LOAD:
Michal Simekfc598412013-04-26 13:10:07 +0200149 rc = fpga_load(dev, fpga_data, data_size);
wdenkd4ca31c2004-01-02 14:00:00 +0000150 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000151
wdenk30ce5ab2005-01-09 18:12:51 +0000152 case FPGA_LOADB:
153 rc = fpga_loadbitstream(dev, fpga_data, data_size);
154 break;
155
Stefan Roesef0ff4692006-08-15 14:15:51 +0200156 case FPGA_LOADMK:
Michal Simekfc598412013-04-26 13:10:07 +0200157 switch (genimg_get_format(fpga_data)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100158 case IMAGE_FORMAT_LEGACY:
159 {
Michal Simekfc598412013-04-26 13:10:07 +0200160 image_header_t *hdr =
161 (image_header_t *)fpga_data;
162 ulong data;
Michal Simek32d7cdd2013-10-04 10:51:01 +0200163 uint8_t comp;
Stefan Roesef0ff4692006-08-15 14:15:51 +0200164
Michal Simek32d7cdd2013-10-04 10:51:01 +0200165 comp = image_get_comp(hdr);
166 if (comp == IH_COMP_GZIP) {
167 ulong image_buf = image_get_data(hdr);
168 data = image_get_load(hdr);
169 ulong image_size = ~0UL;
170
171 if (gunzip((void *)data, ~0UL,
172 (void *)image_buf,
173 &image_size) != 0) {
174 puts("GUNZIP: error\n");
175 return 1;
176 }
177 data_size = image_size;
178 } else {
179 data = (ulong)image_get_data(hdr);
180 data_size = image_get_data_size(hdr);
181 }
Michal Simekfc598412013-04-26 13:10:07 +0200182 rc = fpga_load(dev, (void *)data, data_size);
Stefan Roesef0ff4692006-08-15 14:15:51 +0200183 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100184 break;
185#if defined(CONFIG_FIT)
186 case IMAGE_FORMAT_FIT:
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100187 {
188 const void *fit_hdr = (const void *)fpga_data;
189 int noffset;
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000190 const void *fit_data;
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100191
192 if (fit_uname == NULL) {
Michal Simekfc598412013-04-26 13:10:07 +0200193 puts("No FIT subimage unit name\n");
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100194 return 1;
195 }
196
Michal Simekfc598412013-04-26 13:10:07 +0200197 if (!fit_check_format(fit_hdr)) {
198 puts("Bad FIT image format\n");
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100199 return 1;
200 }
201
202 /* get fpga component image node offset */
Michal Simekfc598412013-04-26 13:10:07 +0200203 noffset = fit_image_get_node(fit_hdr,
204 fit_uname);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100205 if (noffset < 0) {
Michal Simekfc598412013-04-26 13:10:07 +0200206 printf("Can't find '%s' FIT subimage\n",
207 fit_uname);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100208 return 1;
209 }
210
211 /* verify integrity */
Simon Glassb8da8362013-05-07 06:11:57 +0000212 if (!fit_image_verify(fit_hdr, noffset)) {
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100213 puts ("Bad Data Hash\n");
214 return 1;
215 }
216
217 /* get fpga subimage data address and length */
Michal Simekfc598412013-04-26 13:10:07 +0200218 if (fit_image_get_data(fit_hdr, noffset,
219 &fit_data, &data_size)) {
220 puts("Fpga subimage data not found\n");
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100221 return 1;
222 }
223
Michal Simekfc598412013-04-26 13:10:07 +0200224 rc = fpga_load(dev, fit_data, data_size);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100225 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100226 break;
227#endif
228 default:
Michal Simekfc598412013-04-26 13:10:07 +0200229 puts("** Unknown image type\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100230 rc = FPGA_FAIL;
231 break;
Stefan Roesef0ff4692006-08-15 14:15:51 +0200232 }
233 break;
234
wdenkd4ca31c2004-01-02 14:00:00 +0000235 case FPGA_DUMP:
Michal Simekfc598412013-04-26 13:10:07 +0200236 rc = fpga_dump(dev, fpga_data, data_size);
wdenkd4ca31c2004-01-02 14:00:00 +0000237 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000238
wdenkd4ca31c2004-01-02 14:00:00 +0000239 default:
Michal Simekfc598412013-04-26 13:10:07 +0200240 printf("Unknown operation\n");
Simon Glass4c12eeb2011-12-10 08:44:01 +0000241 return CMD_RET_USAGE;
wdenkd4ca31c2004-01-02 14:00:00 +0000242 }
Michal Simekfc598412013-04-26 13:10:07 +0200243 return rc;
wdenk4a9cbbe2002-08-27 09:48:53 +0000244}
245
wdenk4a9cbbe2002-08-27 09:48:53 +0000246/*
247 * Map op to supported operations. We don't use a table since we
248 * would just have to relocate it from flash anyway.
249 */
Michal Simekfc598412013-04-26 13:10:07 +0200250static int fpga_get_op(char *opstr)
wdenk4a9cbbe2002-08-27 09:48:53 +0000251{
252 int op = FPGA_NONE;
253
Michal Simekfc598412013-04-26 13:10:07 +0200254 if (!strcmp("info", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000255 op = FPGA_INFO;
Michal Simekfc598412013-04-26 13:10:07 +0200256 else if (!strcmp("loadb", opstr))
wdenk30ce5ab2005-01-09 18:12:51 +0000257 op = FPGA_LOADB;
Michal Simekfc598412013-04-26 13:10:07 +0200258 else if (!strcmp("load", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000259 op = FPGA_LOAD;
Michal Simekfc598412013-04-26 13:10:07 +0200260 else if (!strcmp("loadmk", opstr))
Stefan Roesef0ff4692006-08-15 14:15:51 +0200261 op = FPGA_LOADMK;
Michal Simekfc598412013-04-26 13:10:07 +0200262 else if (!strcmp("dump", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000263 op = FPGA_DUMP;
wdenk4a9cbbe2002-08-27 09:48:53 +0000264
Michal Simekfc598412013-04-26 13:10:07 +0200265 if (op == FPGA_NONE)
266 printf("Unknown fpga operation \"%s\"\n", opstr);
267
wdenk4a9cbbe2002-08-27 09:48:53 +0000268 return op;
269}
270
Michal Simekfc598412013-04-26 13:10:07 +0200271U_BOOT_CMD(fpga, 6, 1, do_fpga,
272 "loadable FPGA image support",
273 "[operation type] [device number] [image address] [image size]\n"
274 "fpga operations:\n"
275 " dump\t[dev]\t\t\tLoad device to memory buffer\n"
276 " info\t[dev]\t\t\tlist known device information\n"
277 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
278 " loadb\t[dev] [address] [size]\t"
279 "Load device from bitstream buffer (Xilinx only)\n"
280 " loadmk [dev] [address]\tLoad device generated with mkimage"
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100281#if defined(CONFIG_FIT)
Michal Simekfc598412013-04-26 13:10:07 +0200282 "\n"
283 "\tFor loadmk operating on FIT format uImage address must include\n"
284 "\tsubimage unit name in the form of addr:<subimg_uname>"
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100285#endif
286);