blob: c4b3c8fc56b8303ba7739bf7931b1fb38201afa4 [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 Denkd79de1d2013-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>
wdenk57b2d802003-06-27 21:31:46 +000013#include <fpga.h>
wdenk525d7b62005-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 Simeka888af72013-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
wdenk310b4fc2005-01-09 18:12:51 +000023#define FPGA_LOADB 2
wdenk4a9cbbe2002-08-27 09:48:53 +000024#define FPGA_DUMP 3
Stefan Roese5f1cf2d2006-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 Simeka888af72013-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{
wdenk1ebf41e2004-01-02 14:00:00 +000037 int op, dev = FPGA_INVALID_DEVICE;
38 size_t data_size = 0;
39 void *fpga_data = NULL;
Michal Simeka888af72013-04-26 13:10:07 +020040 char *devstr = getenv("fpga");
41 char *datastr = getenv("fpgadata");
wdenk1ebf41e2004-01-02 14:00:00 +000042 int rc = FPGA_FAIL;
Stefano Babic67d7f562010-10-19 09:22:52 +020043 int wrong_parms = 0;
Michal Simeka888af72013-04-26 13:10:07 +020044#if defined(CONFIG_FIT)
Marian Balakowiczd79162d2008-03-12 10:33:01 +010045 const char *fit_uname = NULL;
46 ulong fit_addr;
47#endif
wdenk4a9cbbe2002-08-27 09:48:53 +000048
wdenk1ebf41e2004-01-02 14:00:00 +000049 if (devstr)
Michal Simeka888af72013-04-26 13:10:07 +020050 dev = (int) simple_strtoul(devstr, NULL, 16);
wdenk1ebf41e2004-01-02 14:00:00 +000051 if (datastr)
Michal Simeka888af72013-04-26 13:10:07 +020052 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
wdenk4a9cbbe2002-08-27 09:48:53 +000053
wdenk1ebf41e2004-01-02 14:00:00 +000054 switch (argc) {
55 case 5: /* fpga <op> <dev> <data> <datasize> */
Michal Simeka888af72013-04-26 13:10:07 +020056 data_size = simple_strtoul(argv[4], NULL, 16);
Marian Balakowiczd79162d2008-03-12 10:33:01 +010057
wdenk1ebf41e2004-01-02 14:00:00 +000058 case 4: /* fpga <op> <dev> <data> */
Marian Balakowiczd79162d2008-03-12 10:33:01 +010059#if defined(CONFIG_FIT)
Michal Simeka888af72013-04-26 13:10:07 +020060 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
61 &fit_addr, &fit_uname)) {
Marian Balakowiczd79162d2008-03-12 10:33:01 +010062 fpga_data = (void *)fit_addr;
Michal Simeka888af72013-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 Balakowiczd79162d2008-03-12 10:33:01 +010066 } else
67#endif
68 {
Michal Simeka888af72013-04-26 13:10:07 +020069 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
Stefano Babicb69b9a52011-12-28 06:47:01 +000070 debug("* fpga: cmdline image address = 0x%08lx\n",
Michal Simeka888af72013-04-26 13:10:07 +020071 (ulong)fpga_data);
Marian Balakowiczd79162d2008-03-12 10:33:01 +010072 }
Michal Simeka888af72013-04-26 13:10:07 +020073 debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
Marian Balakowiczd79162d2008-03-12 10:33:01 +010074
wdenk1ebf41e2004-01-02 14:00:00 +000075 case 3: /* fpga <op> <dev | data addr> */
Michal Simeka888af72013-04-26 13:10:07 +020076 dev = (int)simple_strtoul(argv[2], NULL, 16);
Stefano Babicb69b9a52011-12-28 06:47:01 +000077 debug("%s: device = %d\n", __func__, dev);
wdenk1ebf41e2004-01-02 14:00:00 +000078 /* FIXME - this is a really weak test */
Michal Simeka888af72013-04-26 13:10:07 +020079 if ((argc == 3) && (dev > fpga_count())) {
80 /* must be buffer ptr */
Stefano Babicb69b9a52011-12-28 06:47:01 +000081 debug("%s: Assuming buffer pointer in arg 3\n",
Michal Simeka888af72013-04-26 13:10:07 +020082 __func__);
Marian Balakowiczd79162d2008-03-12 10:33:01 +010083
84#if defined(CONFIG_FIT)
Michal Simeka888af72013-04-26 13:10:07 +020085 if (fit_parse_subimage(argv[2], (ulong)fpga_data,
86 &fit_addr, &fit_uname)) {
Marian Balakowiczd79162d2008-03-12 10:33:01 +010087 fpga_data = (void *)fit_addr;
Michal Simeka888af72013-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 Balakowiczd79162d2008-03-12 10:33:01 +010091 } else
92#endif
93 {
Michal Simeka888af72013-04-26 13:10:07 +020094 fpga_data = (void *)dev;
95 debug("* fpga: cmdline image addr = 0x%08lx\n",
96 (ulong)fpga_data);
Marian Balakowiczd79162d2008-03-12 10:33:01 +010097 }
98
Stefano Babicb69b9a52011-12-28 06:47:01 +000099 debug("%s: fpga_data = 0x%x\n",
Michal Simeka888af72013-04-26 13:10:07 +0200100 __func__, (uint)fpga_data);
wdenk1ebf41e2004-01-02 14:00:00 +0000101 dev = FPGA_INVALID_DEVICE; /* reset device num */
102 }
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100103
wdenk1ebf41e2004-01-02 14:00:00 +0000104 case 2: /* fpga <op> */
Michal Simeka888af72013-04-26 13:10:07 +0200105 op = (int)fpga_get_op(argv[1]);
wdenk1ebf41e2004-01-02 14:00:00 +0000106 break;
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100107
wdenk1ebf41e2004-01-02 14:00:00 +0000108 default:
Michal Simeka888af72013-04-26 13:10:07 +0200109 debug("%s: Too many or too few args (%d)\n", __func__, argc);
wdenk1ebf41e2004-01-02 14:00:00 +0000110 op = FPGA_NONE; /* force usage display */
111 break;
112 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000113
Stefano Babic67d7f562010-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
wdenk1ebf41e2004-01-02 14:00:00 +0000140 switch (op) {
141 case FPGA_NONE:
Simon Glassa06dfc72011-12-10 08:44:01 +0000142 return CMD_RET_USAGE;
wdenk4a9cbbe2002-08-27 09:48:53 +0000143
wdenk1ebf41e2004-01-02 14:00:00 +0000144 case FPGA_INFO:
Michal Simeka888af72013-04-26 13:10:07 +0200145 rc = fpga_info(dev);
wdenk1ebf41e2004-01-02 14:00:00 +0000146 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000147
wdenk1ebf41e2004-01-02 14:00:00 +0000148 case FPGA_LOAD:
Michal Simeka888af72013-04-26 13:10:07 +0200149 rc = fpga_load(dev, fpga_data, data_size);
wdenk1ebf41e2004-01-02 14:00:00 +0000150 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000151
wdenk310b4fc2005-01-09 18:12:51 +0000152 case FPGA_LOADB:
153 rc = fpga_loadbitstream(dev, fpga_data, data_size);
154 break;
155
Stefan Roese5f1cf2d2006-08-15 14:15:51 +0200156 case FPGA_LOADMK:
Michal Simeka888af72013-04-26 13:10:07 +0200157 switch (genimg_get_format(fpga_data)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100158 case IMAGE_FORMAT_LEGACY:
159 {
Michal Simeka888af72013-04-26 13:10:07 +0200160 image_header_t *hdr =
161 (image_header_t *)fpga_data;
162 ulong data;
Stefan Roese5f1cf2d2006-08-15 14:15:51 +0200163
Michal Simeka888af72013-04-26 13:10:07 +0200164 data = (ulong)image_get_data(hdr);
165 data_size = image_get_data_size(hdr);
166 rc = fpga_load(dev, (void *)data, data_size);
Stefan Roese5f1cf2d2006-08-15 14:15:51 +0200167 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100168 break;
169#if defined(CONFIG_FIT)
170 case IMAGE_FORMAT_FIT:
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100171 {
172 const void *fit_hdr = (const void *)fpga_data;
173 int noffset;
Wolfgang Denk74f9b382011-07-30 13:33:49 +0000174 const void *fit_data;
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100175
176 if (fit_uname == NULL) {
Michal Simeka888af72013-04-26 13:10:07 +0200177 puts("No FIT subimage unit name\n");
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100178 return 1;
179 }
180
Michal Simeka888af72013-04-26 13:10:07 +0200181 if (!fit_check_format(fit_hdr)) {
182 puts("Bad FIT image format\n");
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100183 return 1;
184 }
185
186 /* get fpga component image node offset */
Michal Simeka888af72013-04-26 13:10:07 +0200187 noffset = fit_image_get_node(fit_hdr,
188 fit_uname);
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100189 if (noffset < 0) {
Michal Simeka888af72013-04-26 13:10:07 +0200190 printf("Can't find '%s' FIT subimage\n",
191 fit_uname);
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100192 return 1;
193 }
194
195 /* verify integrity */
Simon Glass7428ad12013-05-07 06:11:57 +0000196 if (!fit_image_verify(fit_hdr, noffset)) {
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100197 puts ("Bad Data Hash\n");
198 return 1;
199 }
200
201 /* get fpga subimage data address and length */
Michal Simeka888af72013-04-26 13:10:07 +0200202 if (fit_image_get_data(fit_hdr, noffset,
203 &fit_data, &data_size)) {
204 puts("Fpga subimage data not found\n");
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100205 return 1;
206 }
207
Michal Simeka888af72013-04-26 13:10:07 +0200208 rc = fpga_load(dev, fit_data, data_size);
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100209 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100210 break;
211#endif
212 default:
Michal Simeka888af72013-04-26 13:10:07 +0200213 puts("** Unknown image type\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100214 rc = FPGA_FAIL;
215 break;
Stefan Roese5f1cf2d2006-08-15 14:15:51 +0200216 }
217 break;
218
wdenk1ebf41e2004-01-02 14:00:00 +0000219 case FPGA_DUMP:
Michal Simeka888af72013-04-26 13:10:07 +0200220 rc = fpga_dump(dev, fpga_data, data_size);
wdenk1ebf41e2004-01-02 14:00:00 +0000221 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000222
wdenk1ebf41e2004-01-02 14:00:00 +0000223 default:
Michal Simeka888af72013-04-26 13:10:07 +0200224 printf("Unknown operation\n");
Simon Glassa06dfc72011-12-10 08:44:01 +0000225 return CMD_RET_USAGE;
wdenk1ebf41e2004-01-02 14:00:00 +0000226 }
Michal Simeka888af72013-04-26 13:10:07 +0200227 return rc;
wdenk4a9cbbe2002-08-27 09:48:53 +0000228}
229
wdenk4a9cbbe2002-08-27 09:48:53 +0000230/*
231 * Map op to supported operations. We don't use a table since we
232 * would just have to relocate it from flash anyway.
233 */
Michal Simeka888af72013-04-26 13:10:07 +0200234static int fpga_get_op(char *opstr)
wdenk4a9cbbe2002-08-27 09:48:53 +0000235{
236 int op = FPGA_NONE;
237
Michal Simeka888af72013-04-26 13:10:07 +0200238 if (!strcmp("info", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000239 op = FPGA_INFO;
Michal Simeka888af72013-04-26 13:10:07 +0200240 else if (!strcmp("loadb", opstr))
wdenk310b4fc2005-01-09 18:12:51 +0000241 op = FPGA_LOADB;
Michal Simeka888af72013-04-26 13:10:07 +0200242 else if (!strcmp("load", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000243 op = FPGA_LOAD;
Michal Simeka888af72013-04-26 13:10:07 +0200244 else if (!strcmp("loadmk", opstr))
Stefan Roese5f1cf2d2006-08-15 14:15:51 +0200245 op = FPGA_LOADMK;
Michal Simeka888af72013-04-26 13:10:07 +0200246 else if (!strcmp("dump", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000247 op = FPGA_DUMP;
wdenk4a9cbbe2002-08-27 09:48:53 +0000248
Michal Simeka888af72013-04-26 13:10:07 +0200249 if (op == FPGA_NONE)
250 printf("Unknown fpga operation \"%s\"\n", opstr);
251
wdenk4a9cbbe2002-08-27 09:48:53 +0000252 return op;
253}
254
Michal Simeka888af72013-04-26 13:10:07 +0200255U_BOOT_CMD(fpga, 6, 1, do_fpga,
256 "loadable FPGA image support",
257 "[operation type] [device number] [image address] [image size]\n"
258 "fpga operations:\n"
259 " dump\t[dev]\t\t\tLoad device to memory buffer\n"
260 " info\t[dev]\t\t\tlist known device information\n"
261 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
262 " loadb\t[dev] [address] [size]\t"
263 "Load device from bitstream buffer (Xilinx only)\n"
264 " loadmk [dev] [address]\tLoad device generated with mkimage"
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100265#if defined(CONFIG_FIT)
Michal Simeka888af72013-04-26 13:10:07 +0200266 "\n"
267 "\tFor loadmk operating on FIT format uImage address must include\n"
268 "\tsubimage unit name in the form of addr:<subimg_uname>"
Marian Balakowiczd79162d2008-03-12 10:33:01 +0100269#endif
270);