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