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