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