2 * (C) Copyright 2000, 2001
3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #if defined(CONFIG_CMD_NET)
37 static int fpga_get_op(char *opstr);
47 /* Convert bitstream data and load into the fpga */
48 int fpga_loadbitstream(unsigned long dev, char *fpgadata, size_t size)
50 #if defined(CONFIG_FPGA_XILINX)
52 unsigned int swapsize;
54 unsigned char *dataptr;
58 dataptr = (unsigned char *)fpgadata;
60 /* skip the first bytes of the bitsteam, their meaning is unknown */
61 length = (*dataptr << 8) + *(dataptr + 1);
65 /* get design name (identifier, length, string) */
66 length = (*dataptr << 8) + *(dataptr + 1);
68 if (*dataptr++ != 0x61) {
69 debug("%s: Design name id not recognized in bitstream\n",
74 length = (*dataptr << 8) + *(dataptr + 1);
76 for (i = 0; i < length; i++)
77 buffer[i] = *dataptr++;
79 printf(" design filename = \"%s\"\n", buffer);
81 /* get part number (identifier, length, string) */
82 if (*dataptr++ != 0x62) {
83 printf("%s: Part number id not recognized in bitstream\n",
88 length = (*dataptr << 8) + *(dataptr + 1);
90 for (i = 0; i < length; i++)
91 buffer[i] = *dataptr++;
92 printf(" part number = \"%s\"\n", buffer);
94 /* get date (identifier, length, string) */
95 if (*dataptr++ != 0x63) {
96 printf("%s: Date identifier not recognized in bitstream\n",
101 length = (*dataptr << 8) + *(dataptr+1);
103 for (i = 0; i < length; i++)
104 buffer[i] = *dataptr++;
105 printf(" date = \"%s\"\n", buffer);
107 /* get time (identifier, length, string) */
108 if (*dataptr++ != 0x64) {
109 printf("%s: Time identifier not recognized in bitstream\n",
114 length = (*dataptr << 8) + *(dataptr+1);
116 for (i = 0; i < length; i++)
117 buffer[i] = *dataptr++;
118 printf(" time = \"%s\"\n", buffer);
120 /* get fpga data length (identifier, length) */
121 if (*dataptr++ != 0x65) {
122 printf("%s: Data length id not recognized in bitstream\n",
126 swapsize = ((unsigned int) *dataptr << 24) +
127 ((unsigned int) *(dataptr + 1) << 16) +
128 ((unsigned int) *(dataptr + 2) << 8) +
129 ((unsigned int) *(dataptr + 3));
131 printf(" bytes in bitstream = %d\n", swapsize);
133 rc = fpga_load(dev, dataptr, swapsize);
136 printf("Bitstream support only for Xilinx devices\n");
141 /* ------------------------------------------------------------------------- */
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.
149 int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
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");
158 #if defined(CONFIG_FIT)
159 const char *fit_uname = NULL;
164 dev = (int) simple_strtoul(devstr, NULL, 16);
166 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
169 case 5: /* fpga <op> <dev> <data> <datasize> */
170 data_size = simple_strtoul(argv[4], NULL, 16);
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 ",
179 debug("at 0x%08lx\n", fit_addr);
183 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
184 debug("* fpga: cmdline image address = 0x%08lx\n",
187 debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
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",
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 ",
204 debug("at 0x%08lx\n", fit_addr);
208 fpga_data = (void *)dev;
209 debug("* fpga: cmdline image addr = 0x%08lx\n",
213 debug("%s: fpga_data = 0x%x\n",
214 __func__, (uint)fpga_data);
215 dev = FPGA_INVALID_DEVICE; /* reset device num */
218 case 2: /* fpga <op> */
219 op = (int)fpga_get_op(argv[1]);
223 debug("%s: Too many or too few args (%d)\n", __func__, argc);
224 op = FPGA_NONE; /* force usage display */
228 if (dev == FPGA_INVALID_DEVICE) {
229 puts("FPGA device not specified\n");
240 if (!fpga_data || !data_size)
250 puts("Wrong parameters for FPGA request\n");
256 return CMD_RET_USAGE;
263 rc = fpga_load(dev, fpga_data, data_size);
267 rc = fpga_loadbitstream(dev, fpga_data, data_size);
271 switch (genimg_get_format(fpga_data)) {
272 case IMAGE_FORMAT_LEGACY:
274 image_header_t *hdr =
275 (image_header_t *)fpga_data;
278 data = (ulong)image_get_data(hdr);
279 data_size = image_get_data_size(hdr);
280 rc = fpga_load(dev, (void *)data, data_size);
283 #if defined(CONFIG_FIT)
284 case IMAGE_FORMAT_FIT:
286 const void *fit_hdr = (const void *)fpga_data;
288 const void *fit_data;
290 if (fit_uname == NULL) {
291 puts("No FIT subimage unit name\n");
295 if (!fit_check_format(fit_hdr)) {
296 puts("Bad FIT image format\n");
300 /* get fpga component image node offset */
301 noffset = fit_image_get_node(fit_hdr,
304 printf("Can't find '%s' FIT subimage\n",
309 /* verify integrity */
310 if (!fit_image_check_hashes(fit_hdr, noffset)) {
311 puts("Bad Data Hash\n");
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");
322 rc = fpga_load(dev, fit_data, data_size);
327 puts("** Unknown image type\n");
334 rc = fpga_dump(dev, fpga_data, data_size);
338 printf("Unknown operation\n");
339 return CMD_RET_USAGE;
345 * Map op to supported operations. We don't use a table since we
346 * would just have to relocate it from flash anyway.
348 static int fpga_get_op(char *opstr)
352 if (!strcmp("info", opstr))
354 else if (!strcmp("loadb", opstr))
356 else if (!strcmp("load", opstr))
358 else if (!strcmp("loadmk", opstr))
360 else if (!strcmp("dump", opstr))
364 printf("Unknown fpga operation \"%s\"\n", opstr);
369 U_BOOT_CMD(fpga, 6, 1, do_fpga,
370 "loadable FPGA image support",
371 "[operation type] [device number] [image address] [image size]\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)
381 "\tFor loadmk operating on FIT format uImage address must include\n"
382 "\tsubimage unit name in the form of addr:<subimg_uname>"