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 void fpga_usage (cmd_tbl_t * cmdtp);
48 static int fpga_get_op (char *opstr);
58 /* Convert bitstream data and load into the fpga */
59 int fpga_loadbitstream(unsigned long dev, char* fpgadata, size_t size)
61 #if defined(CONFIG_FPGA_XILINX)
63 unsigned int swapsize;
65 unsigned char *dataptr;
69 dataptr = (unsigned char *)fpgadata;
71 /* skip the first bytes of the bitsteam, their meaning is unknown */
72 length = (*dataptr << 8) + *(dataptr+1);
76 /* get design name (identifier, length, string) */
77 length = (*dataptr << 8) + *(dataptr+1);
79 if (*dataptr++ != 0x61) {
80 PRINTF ("%s: Design name identifier not recognized in bitstream\n",
85 length = (*dataptr << 8) + *(dataptr+1);
90 printf(" design filename = \"%s\"\n", buffer);
92 /* get part number (identifier, length, string) */
93 if (*dataptr++ != 0x62) {
94 printf("%s: Part number identifier not recognized in bitstream\n",
99 length = (*dataptr << 8) + *(dataptr+1);
101 for(i=0;i<length;i++)
102 buffer[i]=*dataptr++;
103 printf(" part number = \"%s\"\n", buffer);
105 /* get date (identifier, length, string) */
106 if (*dataptr++ != 0x63) {
107 printf("%s: Date identifier not recognized in bitstream\n",
112 length = (*dataptr << 8) + *(dataptr+1);
114 for(i=0;i<length;i++)
115 buffer[i]=*dataptr++;
116 printf(" date = \"%s\"\n", buffer);
118 /* get time (identifier, length, string) */
119 if (*dataptr++ != 0x64) {
120 printf("%s: Time identifier not recognized in bitstream\n",__FUNCTION__);
124 length = (*dataptr << 8) + *(dataptr+1);
126 for(i=0;i<length;i++)
127 buffer[i]=*dataptr++;
128 printf(" time = \"%s\"\n", buffer);
130 /* get fpga data length (identifier, length) */
131 if (*dataptr++ != 0x65) {
132 printf("%s: Data length identifier not recognized in bitstream\n",
136 swapsize = ((unsigned int) *dataptr <<24) +
137 ((unsigned int) *(dataptr+1) <<16) +
138 ((unsigned int) *(dataptr+2) <<8 ) +
139 ((unsigned int) *(dataptr+3) ) ;
141 printf(" bytes in bitstream = %d\n", swapsize);
143 rc = fpga_load(dev, dataptr, swapsize);
146 printf("Bitstream support only for Xilinx devices\n");
151 /* ------------------------------------------------------------------------- */
153 * fpga <op> <device number> <data addr> <datasize>
154 * where op is 'load', 'dump', or 'info'
155 * If there is no device number field, the fpga environment variable is used.
156 * If there is no data addr field, the fpgadata environment variable is used.
157 * The info command requires no data address field.
159 int do_fpga (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
161 int op, dev = FPGA_INVALID_DEVICE;
162 size_t data_size = 0;
163 void *fpga_data = NULL;
164 char *devstr = getenv ("fpga");
165 char *datastr = getenv ("fpgadata");
169 dev = (int) simple_strtoul (devstr, NULL, 16);
171 fpga_data = (void *) simple_strtoul (datastr, NULL, 16);
174 case 5: /* fpga <op> <dev> <data> <datasize> */
175 data_size = simple_strtoul (argv[4], NULL, 16);
176 case 4: /* fpga <op> <dev> <data> */
177 fpga_data = (void *) simple_strtoul (argv[3], NULL, 16);
178 PRINTF ("%s: fpga_data = 0x%x\n", __FUNCTION__, (uint) fpga_data);
179 case 3: /* fpga <op> <dev | data addr> */
180 dev = (int) simple_strtoul (argv[2], NULL, 16);
181 PRINTF ("%s: device = %d\n", __FUNCTION__, dev);
182 /* FIXME - this is a really weak test */
183 if ((argc == 3) && (dev > fpga_count ())) { /* must be buffer ptr */
184 PRINTF ("%s: Assuming buffer pointer in arg 3\n",
186 fpga_data = (void *) dev;
187 PRINTF ("%s: fpga_data = 0x%x\n",
188 __FUNCTION__, (uint) fpga_data);
189 dev = FPGA_INVALID_DEVICE; /* reset device num */
191 case 2: /* fpga <op> */
192 op = (int) fpga_get_op (argv[1]);
195 PRINTF ("%s: Too many or too few args (%d)\n",
197 op = FPGA_NONE; /* force usage display */
207 rc = fpga_info (dev);
211 rc = fpga_load (dev, fpga_data, data_size);
215 rc = fpga_loadbitstream(dev, fpga_data, data_size);
219 switch (genimg_get_format (fpga_data)) {
220 case IMAGE_FORMAT_LEGACY:
222 image_header_t *hdr = (image_header_t *)fpga_data;
225 if (!image_check_magic (hdr)) {
226 puts ("Bad Magic Number\n");
229 data = (ulong)image_get_data (hdr);
230 data_size = image_get_data_size (hdr);
231 rc = fpga_load (dev, (void *)data, data_size);
234 #if defined(CONFIG_FIT)
235 case IMAGE_FORMAT_FIT:
236 fit_unsupported ("fpga");
241 puts ("** Unknown image type\n");
248 rc = fpga_dump (dev, fpga_data, data_size);
252 printf ("Unknown operation\n");
259 static void fpga_usage (cmd_tbl_t * cmdtp)
261 printf ("Usage:\n%s\n", cmdtp->usage);
265 * Map op to supported operations. We don't use a table since we
266 * would just have to relocate it from flash anyway.
268 static int fpga_get_op (char *opstr)
272 if (!strcmp ("info", opstr)) {
274 } else if (!strcmp ("loadb", opstr)) {
276 } else if (!strcmp ("load", opstr)) {
278 } else if (!strcmp ("loadmk", opstr)) {
280 } else if (!strcmp ("dump", opstr)) {
284 if (op == FPGA_NONE) {
285 printf ("Unknown fpga operation \"%s\"\n", opstr);
290 U_BOOT_CMD (fpga, 6, 1, do_fpga,
291 "fpga - loadable FPGA image support\n",
292 "fpga [operation type] [device number] [image address] [image size]\n"
294 "\tinfo\tlist known device information\n"
295 "\tload\tLoad device from memory buffer\n"
296 "\tloadb\tLoad device from bitstream buffer (Xilinx devices only)\n"
297 "\tloadmk\tLoad device generated with mkimage\n"
298 "\tdump\tLoad device to memory buffer\n");