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 Denk1a459662013-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>
wdenk8bde7f72003-06-27 21:31:46 +000013#include <fpga.h>
wdenkc3d2b4b2005-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 Simekfc598412013-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
wdenk30ce5ab2005-01-09 18:12:51 +000023#define FPGA_LOADB 2
wdenk4a9cbbe2002-08-27 09:48:53 +000024#define FPGA_DUMP 3
Stefan Roesef0ff4692006-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 Simekfc598412013-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{
wdenkd4ca31c2004-01-02 14:00:00 +000037 int op, dev = FPGA_INVALID_DEVICE;
38 size_t data_size = 0;
39 void *fpga_data = NULL;
Michal Simekfc598412013-04-26 13:10:07 +020040 char *devstr = getenv("fpga");
41 char *datastr = getenv("fpgadata");
wdenkd4ca31c2004-01-02 14:00:00 +000042 int rc = FPGA_FAIL;
Stefano Babica790b5b2010-10-19 09:22:52 +020043 int wrong_parms = 0;
Michal Simekfc598412013-04-26 13:10:07 +020044#if defined(CONFIG_FIT)
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010045 const char *fit_uname = NULL;
46 ulong fit_addr;
47#endif
wdenk4a9cbbe2002-08-27 09:48:53 +000048
wdenkd4ca31c2004-01-02 14:00:00 +000049 if (devstr)
Michal Simekfc598412013-04-26 13:10:07 +020050 dev = (int) simple_strtoul(devstr, NULL, 16);
wdenkd4ca31c2004-01-02 14:00:00 +000051 if (datastr)
Michal Simekfc598412013-04-26 13:10:07 +020052 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
wdenk4a9cbbe2002-08-27 09:48:53 +000053
wdenkd4ca31c2004-01-02 14:00:00 +000054 switch (argc) {
55 case 5: /* fpga <op> <dev> <data> <datasize> */
Michal Simekfc598412013-04-26 13:10:07 +020056 data_size = simple_strtoul(argv[4], NULL, 16);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010057
wdenkd4ca31c2004-01-02 14:00:00 +000058 case 4: /* fpga <op> <dev> <data> */
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010059#if defined(CONFIG_FIT)
Michal Simekfc598412013-04-26 13:10:07 +020060 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
61 &fit_addr, &fit_uname)) {
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010062 fpga_data = (void *)fit_addr;
Michal Simekfc598412013-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 Balakowiczc28c4d12008-03-12 10:33:01 +010066 } else
67#endif
68 {
Michal Simekfc598412013-04-26 13:10:07 +020069 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
Stefano Babic06297db2011-12-28 06:47:01 +000070 debug("* fpga: cmdline image address = 0x%08lx\n",
Michal Simekfc598412013-04-26 13:10:07 +020071 (ulong)fpga_data);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010072 }
Michal Simekfc598412013-04-26 13:10:07 +020073 debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010074
wdenkd4ca31c2004-01-02 14:00:00 +000075 case 3: /* fpga <op> <dev | data addr> */
Michal Simekfc598412013-04-26 13:10:07 +020076 dev = (int)simple_strtoul(argv[2], NULL, 16);
Stefano Babic06297db2011-12-28 06:47:01 +000077 debug("%s: device = %d\n", __func__, dev);
wdenkd4ca31c2004-01-02 14:00:00 +000078 /* FIXME - this is a really weak test */
Michal Simekfc598412013-04-26 13:10:07 +020079 if ((argc == 3) && (dev > fpga_count())) {
80 /* must be buffer ptr */
Stefano Babic06297db2011-12-28 06:47:01 +000081 debug("%s: Assuming buffer pointer in arg 3\n",
Michal Simekfc598412013-04-26 13:10:07 +020082 __func__);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010083
84#if defined(CONFIG_FIT)
Michal Simekfc598412013-04-26 13:10:07 +020085 if (fit_parse_subimage(argv[2], (ulong)fpga_data,
86 &fit_addr, &fit_uname)) {
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010087 fpga_data = (void *)fit_addr;
Michal Simekfc598412013-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 Balakowiczc28c4d12008-03-12 10:33:01 +010091 } else
92#endif
93 {
Michal Simekfc598412013-04-26 13:10:07 +020094 fpga_data = (void *)dev;
95 debug("* fpga: cmdline image addr = 0x%08lx\n",
96 (ulong)fpga_data);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +010097 }
98
Stefano Babic06297db2011-12-28 06:47:01 +000099 debug("%s: fpga_data = 0x%x\n",
Michal Simekfc598412013-04-26 13:10:07 +0200100 __func__, (uint)fpga_data);
wdenkd4ca31c2004-01-02 14:00:00 +0000101 dev = FPGA_INVALID_DEVICE; /* reset device num */
102 }
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100103
wdenkd4ca31c2004-01-02 14:00:00 +0000104 case 2: /* fpga <op> */
Michal Simekfc598412013-04-26 13:10:07 +0200105 op = (int)fpga_get_op(argv[1]);
wdenkd4ca31c2004-01-02 14:00:00 +0000106 break;
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100107
wdenkd4ca31c2004-01-02 14:00:00 +0000108 default:
Michal Simekfc598412013-04-26 13:10:07 +0200109 debug("%s: Too many or too few args (%d)\n", __func__, argc);
wdenkd4ca31c2004-01-02 14:00:00 +0000110 op = FPGA_NONE; /* force usage display */
111 break;
112 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000113
Stefano Babica790b5b2010-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
wdenkd4ca31c2004-01-02 14:00:00 +0000140 switch (op) {
141 case FPGA_NONE:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000142 return CMD_RET_USAGE;
wdenk4a9cbbe2002-08-27 09:48:53 +0000143
wdenkd4ca31c2004-01-02 14:00:00 +0000144 case FPGA_INFO:
Michal Simekfc598412013-04-26 13:10:07 +0200145 rc = fpga_info(dev);
wdenkd4ca31c2004-01-02 14:00:00 +0000146 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000147
wdenkd4ca31c2004-01-02 14:00:00 +0000148 case FPGA_LOAD:
Michal Simekfc598412013-04-26 13:10:07 +0200149 rc = fpga_load(dev, fpga_data, data_size);
wdenkd4ca31c2004-01-02 14:00:00 +0000150 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000151
wdenk30ce5ab2005-01-09 18:12:51 +0000152 case FPGA_LOADB:
153 rc = fpga_loadbitstream(dev, fpga_data, data_size);
154 break;
155
Stefan Roesef0ff4692006-08-15 14:15:51 +0200156 case FPGA_LOADMK:
Michal Simekfc598412013-04-26 13:10:07 +0200157 switch (genimg_get_format(fpga_data)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100158 case IMAGE_FORMAT_LEGACY:
159 {
Michal Simekfc598412013-04-26 13:10:07 +0200160 image_header_t *hdr =
161 (image_header_t *)fpga_data;
162 ulong data;
Stefan Roesef0ff4692006-08-15 14:15:51 +0200163
Michal Simekfc598412013-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 Roesef0ff4692006-08-15 14:15:51 +0200167 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100168 break;
169#if defined(CONFIG_FIT)
170 case IMAGE_FORMAT_FIT:
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100171 {
172 const void *fit_hdr = (const void *)fpga_data;
173 int noffset;
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000174 const void *fit_data;
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100175
176 if (fit_uname == NULL) {
Michal Simekfc598412013-04-26 13:10:07 +0200177 puts("No FIT subimage unit name\n");
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100178 return 1;
179 }
180
Michal Simekfc598412013-04-26 13:10:07 +0200181 if (!fit_check_format(fit_hdr)) {
182 puts("Bad FIT image format\n");
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100183 return 1;
184 }
185
186 /* get fpga component image node offset */
Michal Simekfc598412013-04-26 13:10:07 +0200187 noffset = fit_image_get_node(fit_hdr,
188 fit_uname);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100189 if (noffset < 0) {
Michal Simekfc598412013-04-26 13:10:07 +0200190 printf("Can't find '%s' FIT subimage\n",
191 fit_uname);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100192 return 1;
193 }
194
195 /* verify integrity */
Simon Glassb8da8362013-05-07 06:11:57 +0000196 if (!fit_image_verify(fit_hdr, noffset)) {
Marian Balakowiczc28c4d12008-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 Simekfc598412013-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 Balakowiczc28c4d12008-03-12 10:33:01 +0100205 return 1;
206 }
207
Michal Simekfc598412013-04-26 13:10:07 +0200208 rc = fpga_load(dev, fit_data, data_size);
Marian Balakowiczc28c4d12008-03-12 10:33:01 +0100209 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100210 break;
211#endif
212 default:
Michal Simekfc598412013-04-26 13:10:07 +0200213 puts("** Unknown image type\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100214 rc = FPGA_FAIL;
215 break;
Stefan Roesef0ff4692006-08-15 14:15:51 +0200216 }
217 break;
218
wdenkd4ca31c2004-01-02 14:00:00 +0000219 case FPGA_DUMP:
Michal Simekfc598412013-04-26 13:10:07 +0200220 rc = fpga_dump(dev, fpga_data, data_size);
wdenkd4ca31c2004-01-02 14:00:00 +0000221 break;
wdenk4a9cbbe2002-08-27 09:48:53 +0000222
wdenkd4ca31c2004-01-02 14:00:00 +0000223 default:
Michal Simekfc598412013-04-26 13:10:07 +0200224 printf("Unknown operation\n");
Simon Glass4c12eeb2011-12-10 08:44:01 +0000225 return CMD_RET_USAGE;
wdenkd4ca31c2004-01-02 14:00:00 +0000226 }
Michal Simekfc598412013-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 Simekfc598412013-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 Simekfc598412013-04-26 13:10:07 +0200238 if (!strcmp("info", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000239 op = FPGA_INFO;
Michal Simekfc598412013-04-26 13:10:07 +0200240 else if (!strcmp("loadb", opstr))
wdenk30ce5ab2005-01-09 18:12:51 +0000241 op = FPGA_LOADB;
Michal Simekfc598412013-04-26 13:10:07 +0200242 else if (!strcmp("load", opstr))
wdenk4a9cbbe2002-08-27 09:48:53 +0000243 op = FPGA_LOAD;
Michal Simekfc598412013-04-26 13:10:07 +0200244 else if (!strcmp("loadmk", opstr))
Stefan Roesef0ff4692006-08-15 14:15:51 +0200245 op = FPGA_LOADMK;
Michal Simekfc598412013-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 Simekfc598412013-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 Simekfc598412013-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 Balakowiczc28c4d12008-03-12 10:33:01 +0100265#if defined(CONFIG_FIT)
Michal Simekfc598412013-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 Balakowiczc28c4d12008-03-12 10:33:01 +0100269#endif
270);