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