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)
41 #define PRINTF(fmt,args...) printf (fmt ,##args)
43 #define PRINTF(fmt,args...)
47 static int fpga_get_op (char *opstr);
57 /* Convert bitstream data and load into the fpga */
58 int fpga_loadbitstream(unsigned long dev, char* fpgadata, size_t size)
60 #if defined(CONFIG_FPGA_XILINX)
62 unsigned int swapsize;
64 unsigned char *dataptr;
68 dataptr = (unsigned char *)fpgadata;
70 /* skip the first bytes of the bitsteam, their meaning is unknown */
71 length = (*dataptr << 8) + *(dataptr+1);
75 /* get design name (identifier, length, string) */
76 length = (*dataptr << 8) + *(dataptr+1);
78 if (*dataptr++ != 0x61) {
79 PRINTF ("%s: Design name identifier not recognized in bitstream\n",
84 length = (*dataptr << 8) + *(dataptr+1);
87 buffer[i] = *dataptr++;
89 printf(" design filename = \"%s\"\n", buffer);
91 /* get part number (identifier, length, string) */
92 if (*dataptr++ != 0x62) {
93 printf("%s: Part number identifier not recognized in bitstream\n",
98 length = (*dataptr << 8) + *(dataptr+1);
100 for(i=0;i<length;i++)
101 buffer[i] = *dataptr++;
102 printf(" part number = \"%s\"\n", buffer);
104 /* get date (identifier, length, string) */
105 if (*dataptr++ != 0x63) {
106 printf("%s: Date identifier not recognized in bitstream\n",
111 length = (*dataptr << 8) + *(dataptr+1);
113 for(i=0;i<length;i++)
114 buffer[i] = *dataptr++;
115 printf(" date = \"%s\"\n", buffer);
117 /* get time (identifier, length, string) */
118 if (*dataptr++ != 0x64) {
119 printf("%s: Time identifier not recognized in bitstream\n",__FUNCTION__);
123 length = (*dataptr << 8) + *(dataptr+1);
125 for(i=0;i<length;i++)
126 buffer[i] = *dataptr++;
127 printf(" time = \"%s\"\n", buffer);
129 /* get fpga data length (identifier, length) */
130 if (*dataptr++ != 0x65) {
131 printf("%s: Data length identifier not recognized in bitstream\n",
135 swapsize = ((unsigned int) *dataptr <<24) +
136 ((unsigned int) *(dataptr+1) <<16) +
137 ((unsigned int) *(dataptr+2) <<8 ) +
138 ((unsigned int) *(dataptr+3) ) ;
140 printf(" bytes in bitstream = %d\n", swapsize);
142 rc = fpga_load(dev, dataptr, swapsize);
145 printf("Bitstream support only for Xilinx devices\n");
150 /* ------------------------------------------------------------------------- */
152 * fpga <op> <device number> <data addr> <datasize>
153 * where op is 'load', 'dump', or 'info'
154 * If there is no device number field, the fpga environment variable is used.
155 * If there is no data addr field, the fpgadata environment variable is used.
156 * The info command requires no data address field.
158 int do_fpga (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
160 int op, dev = FPGA_INVALID_DEVICE;
161 size_t data_size = 0;
162 void *fpga_data = NULL;
163 char *devstr = getenv ("fpga");
164 char *datastr = getenv ("fpgadata");
166 #if defined (CONFIG_FIT)
167 const char *fit_uname = NULL;
172 dev = (int) simple_strtoul (devstr, NULL, 16);
174 fpga_data = (void *) simple_strtoul (datastr, NULL, 16);
177 case 5: /* fpga <op> <dev> <data> <datasize> */
178 data_size = simple_strtoul (argv[4], NULL, 16);
180 case 4: /* fpga <op> <dev> <data> */
181 #if defined(CONFIG_FIT)
182 if (fit_parse_subimage (argv[3], (ulong)fpga_data,
183 &fit_addr, &fit_uname)) {
184 fpga_data = (void *)fit_addr;
185 debug ("* fpga: subimage '%s' from FIT image at 0x%08lx\n",
186 fit_uname, fit_addr);
190 fpga_data = (void *) simple_strtoul (argv[3], NULL, 16);
191 debug ("* fpga: cmdline image address = 0x%08lx\n", (ulong)fpga_data);
193 PRINTF ("%s: fpga_data = 0x%x\n", __FUNCTION__, (uint) fpga_data);
195 case 3: /* fpga <op> <dev | data addr> */
196 dev = (int) simple_strtoul (argv[2], NULL, 16);
197 PRINTF ("%s: device = %d\n", __FUNCTION__, dev);
198 /* FIXME - this is a really weak test */
199 if ((argc == 3) && (dev > fpga_count ())) { /* must be buffer ptr */
200 PRINTF ("%s: Assuming buffer pointer in arg 3\n",
203 #if defined(CONFIG_FIT)
204 if (fit_parse_subimage (argv[2], (ulong)fpga_data,
205 &fit_addr, &fit_uname)) {
206 fpga_data = (void *)fit_addr;
207 debug ("* fpga: subimage '%s' from FIT image at 0x%08lx\n",
208 fit_uname, fit_addr);
212 fpga_data = (void *) dev;
213 debug ("* fpga: cmdline image address = 0x%08lx\n", (ulong)fpga_data);
216 PRINTF ("%s: fpga_data = 0x%x\n",
217 __FUNCTION__, (uint) fpga_data);
218 dev = FPGA_INVALID_DEVICE; /* reset device num */
221 case 2: /* fpga <op> */
222 op = (int) fpga_get_op (argv[1]);
226 PRINTF ("%s: Too many or too few args (%d)\n",
228 op = FPGA_NONE; /* force usage display */
234 return cmd_usage(cmdtp);
237 rc = fpga_info (dev);
241 rc = fpga_load (dev, fpga_data, data_size);
245 rc = fpga_loadbitstream(dev, fpga_data, data_size);
249 switch (genimg_get_format (fpga_data)) {
250 case IMAGE_FORMAT_LEGACY:
252 image_header_t *hdr = (image_header_t *)fpga_data;
255 data = (ulong)image_get_data (hdr);
256 data_size = image_get_data_size (hdr);
257 rc = fpga_load (dev, (void *)data, data_size);
260 #if defined(CONFIG_FIT)
261 case IMAGE_FORMAT_FIT:
263 const void *fit_hdr = (const void *)fpga_data;
267 if (fit_uname == NULL) {
268 puts ("No FIT subimage unit name\n");
272 if (!fit_check_format (fit_hdr)) {
273 puts ("Bad FIT image format\n");
277 /* get fpga component image node offset */
278 noffset = fit_image_get_node (fit_hdr, fit_uname);
280 printf ("Can't find '%s' FIT subimage\n", fit_uname);
284 /* verify integrity */
285 if (!fit_image_check_hashes (fit_hdr, noffset)) {
286 puts ("Bad Data Hash\n");
290 /* get fpga subimage data address and length */
291 if (fit_image_get_data (fit_hdr, noffset, &fit_data, &data_size)) {
292 puts ("Could not find fpga subimage data\n");
296 rc = fpga_load (dev, fit_data, data_size);
301 puts ("** Unknown image type\n");
308 rc = fpga_dump (dev, fpga_data, data_size);
312 printf ("Unknown operation\n");
313 return cmd_usage(cmdtp);
319 * Map op to supported operations. We don't use a table since we
320 * would just have to relocate it from flash anyway.
322 static int fpga_get_op (char *opstr)
326 if (!strcmp ("info", opstr)) {
328 } else if (!strcmp ("loadb", opstr)) {
330 } else if (!strcmp ("load", opstr)) {
332 } else if (!strcmp ("loadmk", opstr)) {
334 } else if (!strcmp ("dump", opstr)) {
338 if (op == FPGA_NONE) {
339 printf ("Unknown fpga operation \"%s\"\n", opstr);
344 U_BOOT_CMD (fpga, 6, 1, do_fpga,
345 "loadable FPGA image support",
346 "fpga [operation type] [device number] [image address] [image size]\n"
348 "\tinfo\tlist known device information\n"
349 "\tload\tLoad device from memory buffer\n"
350 "\tloadb\tLoad device from bitstream buffer (Xilinx devices only)\n"
351 "\tloadmk\tLoad device generated with mkimage\n"
352 "\tdump\tLoad device to memory buffer"
353 #if defined(CONFIG_FIT)
355 "\tFor loadmk operating on FIT format uImage address must include\n"
356 "\tsubimage unit name in the form of addr:<subimg_uname>"