fpga.c 9.03 KB
Newer Older
wdenk's avatar
wdenk committed
1 2 3 4
/*
 * (C) Copyright 2000, 2001
 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
wdenk's avatar
wdenk committed
6 7 8 9 10 11 12
 */

/*
 *  FPGA support
 */
#include <common.h>
#include <command.h>
wdenk's avatar
wdenk committed
13
#include <fpga.h>
14
#include <fs.h>
wdenk's avatar
wdenk committed
15
#include <malloc.h>
wdenk's avatar
wdenk committed
16 17

/* Local functions */
18
static int fpga_get_op(char *opstr);
wdenk's avatar
wdenk committed
19 20 21 22 23

/* Local defines */
#define FPGA_NONE   -1
#define FPGA_INFO   0
#define FPGA_LOAD   1
24
#define FPGA_LOADB  2
wdenk's avatar
wdenk committed
25
#define FPGA_DUMP   3
26
#define FPGA_LOADMK 4
27 28
#define FPGA_LOADP  5
#define FPGA_LOADBP 6
29
#define FPGA_LOADFS 7
wdenk's avatar
wdenk committed
30 31 32 33 34 35 36 37 38

/* ------------------------------------------------------------------------- */
/* command form:
 *   fpga <op> <device number> <data addr> <datasize>
 * where op is 'load', 'dump', or 'info'
 * If there is no device number field, the fpga environment variable is used.
 * If there is no data addr field, the fpgadata environment variable is used.
 * The info command requires no data address field.
 */
39
int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenk's avatar
wdenk committed
40
{
41 42 43
	int op, dev = FPGA_INVALID_DEVICE;
	size_t data_size = 0;
	void *fpga_data = NULL;
44 45
	char *devstr = getenv("fpga");
	char *datastr = getenv("fpgadata");
46
	int rc = FPGA_FAIL;
47
	int wrong_parms = 0;
48
#if defined(CONFIG_FIT)
49 50 51
	const char *fit_uname = NULL;
	ulong fit_addr;
#endif
52 53 54 55
#if defined(CONFIG_CMD_FPGA_LOADFS)
	fpga_fs_info fpga_fsinfo;
	fpga_fsinfo.fstype = FS_TYPE_ANY;
#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 64 65 66 67 68 69 70
#if defined(CONFIG_CMD_FPGA_LOADFS)
	case 9:
		fpga_fsinfo.blocksize = (unsigned int)
					     simple_strtoul(argv[5], NULL, 16);
		fpga_fsinfo.interface = argv[6];
		fpga_fsinfo.dev_part = argv[7];
		fpga_fsinfo.filename = argv[8];
#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 77
		if (fit_parse_subimage(argv[3], (ulong)fpga_data,
				       &fit_addr, &fit_uname)) {
78
			fpga_data = (void *)fit_addr;
79 80 81
			debug("*  fpga: subimage '%s' from FIT image ",
			      fit_uname);
			debug("at 0x%08lx\n", fit_addr);
82 83 84
		} else
#endif
		{
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%lx\n", __func__, (ulong)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 96
		if ((argc == 3) && (dev > fpga_count())) {
			/* must be buffer ptr */
97
			debug("%s: Assuming buffer pointer in arg 3\n",
98
			      __func__);
99 100

#if defined(CONFIG_FIT)
101 102
			if (fit_parse_subimage(argv[2], (ulong)fpga_data,
					       &fit_addr, &fit_uname)) {
103
				fpga_data = (void *)fit_addr;
104 105 106
				debug("*  fpga: subimage '%s' from FIT image ",
				      fit_uname);
				debug("at 0x%08lx\n", fit_addr);
107 108 109
			} else
#endif
			{
110
				fpga_data = (void *)(uintptr_t)dev;
111 112
				debug("*  fpga: cmdline image addr = 0x%08lx\n",
				      (ulong)fpga_data);
113 114
			}

115 116
			debug("%s: fpga_data = 0x%lx\n",
			      __func__, (ulong)fpga_data);
117 118
			dev = FPGA_INVALID_DEVICE;	/* reset device num */
		}
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 127 128 129
		op = FPGA_NONE;	/* force usage display */
		break;
	}

130 131 132 133 134 135 136 137 138
	if (dev == FPGA_INVALID_DEVICE) {
		puts("FPGA device not specified\n");
		op = FPGA_NONE;
	}

	switch (op) {
	case FPGA_NONE:
	case FPGA_INFO:
		break;
139 140 141 142 143 144 145
#if defined(CONFIG_CMD_FPGA_LOADFS)
	case FPGA_LOADFS:
		/* Blocksize can be zero */
		if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
		    !fpga_fsinfo.filename)
			wrong_parms = 1;
#endif
146
	case FPGA_LOAD:
147
	case FPGA_LOADP:
148
	case FPGA_LOADB:
149
	case FPGA_LOADBP:
150 151 152 153
	case FPGA_DUMP:
		if (!fpga_data || !data_size)
			wrong_parms = 1;
		break;
154
#if defined(CONFIG_CMD_FPGA_LOADMK)
155 156 157 158
	case FPGA_LOADMK:
		if (!fpga_data)
			wrong_parms = 1;
		break;
159
#endif
160 161 162 163 164 165 166
	}

	if (wrong_parms) {
		puts("Wrong parameters for FPGA request\n");
		op = FPGA_NONE;
	}

167 168
	switch (op) {
	case FPGA_NONE:
169
		return CMD_RET_USAGE;
170 171

	case FPGA_INFO:
172
		rc = fpga_info(dev);
173 174 175
		break;

	case FPGA_LOAD:
176
		rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
177 178
		break;

179 180 181 182 183 184
#if defined(CONFIG_CMD_FPGA_LOADP)
	case FPGA_LOADP:
		rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
		break;
#endif

185
	case FPGA_LOADB:
186
		rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
187 188
		break;

189 190 191 192 193 194
#if defined(CONFIG_CMD_FPGA_LOADBP)
	case FPGA_LOADBP:
		rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
		break;
#endif

195 196 197 198 199 200
#if defined(CONFIG_CMD_FPGA_LOADFS)
	case FPGA_LOADFS:
		rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
		break;
#endif

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 206
		case IMAGE_FORMAT_LEGACY:
			{
207 208 209
				image_header_t *hdr =
						(image_header_t *)fpga_data;
				ulong data;
210 211 212 213
				uint8_t comp;

				comp = image_get_comp(hdr);
				if (comp == IH_COMP_GZIP) {
214
#if defined(CONFIG_GZIP)
215 216 217 218 219 220 221 222 223 224 225
					ulong image_buf = image_get_data(hdr);
					data = image_get_load(hdr);
					ulong image_size = ~0UL;

					if (gunzip((void *)data, ~0UL,
						   (void *)image_buf,
						   &image_size) != 0) {
						puts("GUNZIP: error\n");
						return 1;
					}
					data_size = image_size;
226 227 228 229
#else
					puts("Gunzip image is not supported\n");
					return 1;
#endif
230 231 232 233
				} else {
					data = (ulong)image_get_data(hdr);
					data_size = image_get_data_size(hdr);
				}
234 235
				rc = fpga_load(dev, (void *)data, data_size,
					       BIT_FULL);
236
			}
237
			break;
238
#endif
239 240
#if defined(CONFIG_FIT)
		case IMAGE_FORMAT_FIT:
241 242 243
			{
				const void *fit_hdr = (const void *)fpga_data;
				int noffset;
244
				const void *fit_data;
245 246

				if (fit_uname == NULL) {
247
					puts("No FIT subimage unit name\n");
248 249 250
					return 1;
				}

251 252
				if (!fit_check_format(fit_hdr)) {
					puts("Bad FIT image format\n");
253 254 255 256
					return 1;
				}

				/* get fpga component image node offset */
257 258
				noffset = fit_image_get_node(fit_hdr,
							     fit_uname);
259
				if (noffset < 0) {
260 261
					printf("Can't find '%s' FIT subimage\n",
					       fit_uname);
262 263 264 265
					return 1;
				}

				/* verify integrity */
266
				if (!fit_image_verify(fit_hdr, noffset)) {
267 268 269 270 271
					puts ("Bad Data Hash\n");
					return 1;
				}

				/* get fpga subimage data address and length */
272 273 274
				if (fit_image_get_data(fit_hdr, noffset,
						       &fit_data, &data_size)) {
					puts("Fpga subimage data not found\n");
275 276 277
					return 1;
				}

278 279
				rc = fpga_load(dev, fit_data, data_size,
					       BIT_FULL);
280
			}
281 282 283
			break;
#endif
		default:
284
			puts("** Unknown image type\n");
285 286
			rc = FPGA_FAIL;
			break;
287 288
		}
		break;
289
#endif
290

291
	case FPGA_DUMP:
292
		rc = fpga_dump(dev, fpga_data, data_size);
293 294 295
		break;

	default:
296
		printf("Unknown operation\n");
297
		return CMD_RET_USAGE;
298
	}
299
	return rc;
wdenk's avatar
wdenk committed
300 301 302 303 304 305
}

/*
 * Map op to supported operations.  We don't use a table since we
 * would just have to relocate it from flash anyway.
 */
306
static int fpga_get_op(char *opstr)
wdenk's avatar
wdenk committed
307 308 309
{
	int op = FPGA_NONE;

310
	if (!strcmp("info", opstr))
wdenk's avatar
wdenk committed
311
		op = FPGA_INFO;
312
	else if (!strcmp("loadb", opstr))
313
		op = FPGA_LOADB;
314
	else if (!strcmp("load", opstr))
wdenk's avatar
wdenk committed
315
		op = FPGA_LOAD;
316 317 318 319 320 321 322 323
#if defined(CONFIG_CMD_FPGA_LOADP)
	else if (!strcmp("loadp", opstr))
		op = FPGA_LOADP;
#endif
#if defined(CONFIG_CMD_FPGA_LOADBP)
	else if (!strcmp("loadbp", opstr))
		op = FPGA_LOADBP;
#endif
324 325 326 327
#if defined(CONFIG_CMD_FPGA_LOADFS)
	else if (!strcmp("loadfs", opstr))
		op = FPGA_LOADFS;
#endif
328
#if defined(CONFIG_CMD_FPGA_LOADMK)
329
	else if (!strcmp("loadmk", opstr))
330
		op = FPGA_LOADMK;
331
#endif
332
	else if (!strcmp("dump", opstr))
wdenk's avatar
wdenk committed
333 334
		op = FPGA_DUMP;

335 336 337
	if (op == FPGA_NONE)
		printf("Unknown fpga operation \"%s\"\n", opstr);

wdenk's avatar
wdenk committed
338 339 340
	return op;
}

341 342 343
#if defined(CONFIG_CMD_FPGA_LOADFS)
U_BOOT_CMD(fpga, 9, 1, do_fpga,
#else
344
U_BOOT_CMD(fpga, 6, 1, do_fpga,
345
#endif
346 347 348
	   "loadable FPGA image support",
	   "[operation type] [device number] [image address] [image size]\n"
	   "fpga operations:\n"
349
	   "  dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
350 351
	   "  info\t[dev]\t\t\tlist known device information\n"
	   "  load\t[dev] [address] [size]\tLoad device from memory buffer\n"
352 353 354 355
#if defined(CONFIG_CMD_FPGA_LOADP)
	   "  loadp\t[dev] [address] [size]\t"
	   "Load device from memory buffer with partial bitstream\n"
#endif
356 357
	   "  loadb\t[dev] [address] [size]\t"
	   "Load device from bitstream buffer (Xilinx only)\n"
358 359 360 361 362
#if defined(CONFIG_CMD_FPGA_LOADBP)
	   "  loadbp\t[dev] [address] [size]\t"
	   "Load device from bitstream buffer with partial bitstream"
	   "(Xilinx only)\n"
#endif
363 364 365 366 367
#if defined(CONFIG_CMD_FPGA_LOADFS)
	   "Load device from filesystem (FAT by default) (Xilinx only)\n"
	   "  loadfs [dev] [address] [image size] [blocksize] <interface>\n"
	   "        [<dev[:part]>] <filename>\n"
#endif
368
#if defined(CONFIG_CMD_FPGA_LOADMK)
369
	   "  loadmk [dev] [address]\tLoad device generated with mkimage"
370
#if defined(CONFIG_FIT)
371 372 373
	   "\n"
	   "\tFor loadmk operating on FIT format uImage address must include\n"
	   "\tsubimage unit name in the form of addr:<subimg_uname>"
374
#endif
375
#endif
376
);