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 identifier not recognized "
75 length = (*dataptr << 8) + *(dataptr+1);
78 buffer[i] = *dataptr++;
80 printf(" design filename = \"%s\"\n", buffer);
82 /* get part number (identifier, length, string) */
83 if (*dataptr++ != 0x62) {
84 printf("%s: Part number identifier not recognized "
90 length = (*dataptr << 8) + *(dataptr+1);
93 buffer[i] = *dataptr++;
94 printf(" part number = \"%s\"\n", buffer);
96 /* get date (identifier, length, string) */
97 if (*dataptr++ != 0x63) {
98 printf("%s: Date identifier not recognized in bitstream\n",
103 length = (*dataptr << 8) + *(dataptr+1);
105 for(i=0;i<length;i++)
106 buffer[i] = *dataptr++;
107 printf(" date = \"%s\"\n", buffer);
109 /* get time (identifier, length, string) */
110 if (*dataptr++ != 0x64) {
111 printf("%s: Time identifier not recognized in bitstream\n",
116 length = (*dataptr << 8) + *(dataptr+1);
118 for(i=0;i<length;i++)
119 buffer[i] = *dataptr++;
120 printf(" time = \"%s\"\n", buffer);
122 /* get fpga data length (identifier, length) */
123 if (*dataptr++ != 0x65) {
124 printf("%s: Data length identifier not recognized in bitstream\n",
128 swapsize = ((unsigned int) *dataptr <<24) +
129 ((unsigned int) *(dataptr+1) <<16) +
130 ((unsigned int) *(dataptr+2) <<8 ) +
131 ((unsigned int) *(dataptr+3) ) ;
133 printf(" bytes in bitstream = %d\n", swapsize);
135 rc = fpga_load(dev, dataptr, swapsize);
138 printf("Bitstream support only for Xilinx devices\n");
143 /* ------------------------------------------------------------------------- */
145 * fpga <op> <device number> <data addr> <datasize>
146 * where op is 'load', 'dump', or 'info'
147 * If there is no device number field, the fpga environment variable is used.
148 * If there is no data addr field, the fpgadata environment variable is used.
149 * The info command requires no data address field.
151 int do_fpga (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
153 int op, dev = FPGA_INVALID_DEVICE;
154 size_t data_size = 0;
155 void *fpga_data = NULL;
156 char *devstr = getenv ("fpga");
157 char *datastr = getenv ("fpgadata");
160 #if defined (CONFIG_FIT)
161 const char *fit_uname = NULL;
166 dev = (int) simple_strtoul (devstr, NULL, 16);
168 fpga_data = (void *) simple_strtoul (datastr, NULL, 16);
171 case 5: /* fpga <op> <dev> <data> <datasize> */
172 data_size = simple_strtoul (argv[4], NULL, 16);
174 case 4: /* fpga <op> <dev> <data> */
175 #if defined(CONFIG_FIT)
176 if (fit_parse_subimage (argv[3], (ulong)fpga_data,
177 &fit_addr, &fit_uname)) {
178 fpga_data = (void *)fit_addr;
179 debug("* fpga: subimage '%s' from FIT image "
181 fit_uname, fit_addr);
185 fpga_data = (void *) simple_strtoul (argv[3], NULL, 16);
186 debug("* fpga: cmdline image address = 0x%08lx\n",
189 debug("%s: fpga_data = 0x%x\n", __func__, (uint) fpga_data);
191 case 3: /* fpga <op> <dev | data addr> */
192 dev = (int) simple_strtoul (argv[2], NULL, 16);
193 debug("%s: device = %d\n", __func__, dev);
194 /* FIXME - this is a really weak test */
195 if ((argc == 3) && (dev > fpga_count ())) { /* must be buffer ptr */
196 debug("%s: Assuming buffer pointer in arg 3\n",
199 #if defined(CONFIG_FIT)
200 if (fit_parse_subimage (argv[2], (ulong)fpga_data,
201 &fit_addr, &fit_uname)) {
202 fpga_data = (void *)fit_addr;
203 debug("* fpga: subimage '%s' from FIT image "
205 fit_uname, fit_addr);
209 fpga_data = (void *) dev;
210 debug("* fpga: cmdline image address = "
211 "0x%08lx\n", (ulong)fpga_data);
214 debug("%s: fpga_data = 0x%x\n",
215 __func__, (uint) fpga_data);
216 dev = FPGA_INVALID_DEVICE; /* reset device num */
219 case 2: /* fpga <op> */
220 op = (int) fpga_get_op (argv[1]);
224 debug("%s: Too many or too few args (%d)\n",
226 op = FPGA_NONE; /* force usage display */
230 if (dev == FPGA_INVALID_DEVICE) {
231 puts("FPGA device not specified\n");
242 if (!fpga_data || !data_size)
252 puts("Wrong parameters for FPGA request\n");
258 return CMD_RET_USAGE;
261 rc = fpga_info (dev);
265 rc = fpga_load (dev, fpga_data, data_size);
269 rc = fpga_loadbitstream(dev, fpga_data, data_size);
273 switch (genimg_get_format (fpga_data)) {
274 case IMAGE_FORMAT_LEGACY:
276 image_header_t *hdr = (image_header_t *)fpga_data;
279 data = (ulong)image_get_data (hdr);
280 data_size = image_get_data_size (hdr);
281 rc = fpga_load (dev, (void *)data, data_size);
284 #if defined(CONFIG_FIT)
285 case IMAGE_FORMAT_FIT:
287 const void *fit_hdr = (const void *)fpga_data;
289 const void *fit_data;
291 if (fit_uname == NULL) {
292 puts ("No FIT subimage unit name\n");
296 if (!fit_check_format (fit_hdr)) {
297 puts ("Bad FIT image format\n");
301 /* get fpga component image node offset */
302 noffset = fit_image_get_node (fit_hdr, fit_uname);
304 printf ("Can't find '%s' FIT subimage\n", fit_uname);
308 /* verify integrity */
309 if (!fit_image_check_hashes (fit_hdr, noffset)) {
310 puts ("Bad Data Hash\n");
314 /* get fpga subimage data address and length */
315 if (fit_image_get_data (fit_hdr, noffset, &fit_data, &data_size)) {
316 puts ("Could not find fpga subimage data\n");
320 rc = fpga_load (dev, fit_data, data_size);
325 puts ("** Unknown image type\n");
332 rc = fpga_dump (dev, fpga_data, data_size);
336 printf ("Unknown operation\n");
337 return CMD_RET_USAGE;
343 * Map op to supported operations. We don't use a table since we
344 * would just have to relocate it from flash anyway.
346 static int fpga_get_op (char *opstr)
350 if (!strcmp ("info", opstr)) {
352 } else if (!strcmp ("loadb", opstr)) {
354 } else if (!strcmp ("load", opstr)) {
356 } else if (!strcmp ("loadmk", opstr)) {
358 } else if (!strcmp ("dump", opstr)) {
362 if (op == FPGA_NONE) {
363 printf ("Unknown fpga operation \"%s\"\n", opstr);
368 U_BOOT_CMD (fpga, 6, 1, do_fpga,
369 "loadable FPGA image support",
370 "[operation type] [device number] [image address] [image size]\n"
372 " dump\t[dev]\t\t\tLoad device to memory buffer\n"
373 " info\t[dev]\t\t\tlist known device information\n"
374 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
375 " loadb\t[dev] [address] [size]\t"
376 "Load device from bitstream buffer (Xilinx only)\n"
377 " loadmk [dev] [address]\tLoad device generated with mkimage"
378 #if defined(CONFIG_FIT)
380 "\tFor loadmk operating on FIT format uImage address must include\n"
381 "\tsubimage unit name in the form of addr:<subimg_uname>"