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 (CONFIG_COMMANDS & CFG_CMD_NET)
40 #define PRINTF(fmt,args...) printf (fmt ,##args)
42 #define PRINTF(fmt,args...)
45 #if defined (CONFIG_FPGA) && ( CONFIG_COMMANDS & CFG_CMD_FPGA )
48 static void fpga_usage (cmd_tbl_t * cmdtp);
49 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)
74 /* skip the first 13 bytes of the bitsteam, their meaning is unknown */
77 /* get design name (identifier, length, string) */
78 if (*dataptr++ != 0x61) {
79 PRINTF(__FUNCTION__ ": Design name identifier not recognized in bitstream.\n");
83 length = (*dataptr << 8) + *(dataptr+1);
88 buffer[length-5]='\0'; /* remove filename extension */
89 PRINTF(__FUNCTION__ ": design name = \"%s\".\n",buffer);
91 /* get part number (identifier, length, string) */
92 if (*dataptr++ != 0x62) {
93 printf(__FUNCTION__ ": Part number identifier not recognized in bitstream.\n");
97 length = (*dataptr << 8) + *(dataptr+1); dataptr+=2;
100 PRINTF(__FUNCTION__ ": part number = \"%s\".\n",buffer);
102 /* get date (identifier, length, string) */
103 if (*dataptr++ != 0x63) {
104 printf(__FUNCTION__ ": Date identifier not recognized in bitstream.\n");
108 length = (*dataptr << 8) + *(dataptr+1); dataptr+=2;
109 for(i=0;i<length;i++)
110 buffer[i]=*dataptr++;
111 PRINTF(__FUNCTION__ ": date = \"%s\".\n",buffer);
113 /* get time (identifier, length, string) */
114 if (*dataptr++ != 0x64) {
115 printf(__FUNCTION__ ": Time identifier not recognized in bitstream.\n");
119 length = (*dataptr << 8) + *(dataptr+1); dataptr+=2;
120 for(i=0;i<length;i++)
121 buffer[i]=*dataptr++;
122 PRINTF(__FUNCTION__ ": time = \"%s\".\n",buffer);
124 /* get fpga data length (identifier, length) */
125 if (*dataptr++ != 0x65) {
126 printf(__FUNCTION__ ": Data length identifier not recognized in bitstream.\n");
129 swapsize = ((long)*dataptr<<24) + ((long)*(dataptr+1)<<16) + ((long)*(dataptr+2)<<8) + (long)*(dataptr+3);
131 PRINTF(__FUNCTION__ ": bytes in bitstream = %d.\n",swapsize);
133 /* check consistency of length obtained */
134 if (swapsize >= size) {
135 printf(__FUNCTION__ ": Could not find right length of data in bitstream.\n");
139 /* allocate memory */
140 swapdata = (char *)malloc(swapsize);
141 if (swapdata == NULL) {
142 printf(__FUNCTION__ ": Could not allocate %d bytes memory !\n",swapsize);
146 /* read data into memory and swap bits */
148 for (i = 0; i < swapsize; i++) {
150 data |= (*dataptr & 0x01) << 7;
151 data |= (*dataptr & 0x02) << 5;
152 data |= (*dataptr & 0x04) << 3;
153 data |= (*dataptr & 0x08) << 1;
154 data |= (*dataptr & 0x10) >> 1;
155 data |= (*dataptr & 0x20) >> 3;
156 data |= (*dataptr & 0x40) >> 5;
157 data |= (*dataptr & 0x80) >> 7;
162 rc = fpga_load(dev, swapdata, swapsize);
166 printf("Bitstream support only for Xilinx devices.\n");
171 /* ------------------------------------------------------------------------- */
173 * fpga <op> <device number> <data addr> <datasize>
174 * where op is 'load', 'dump', or 'info'
175 * If there is no device number field, the fpga environment variable is used.
176 * If there is no data addr field, the fpgadata environment variable is used.
177 * The info command requires no data address field.
179 int do_fpga (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
181 int op, dev = FPGA_INVALID_DEVICE;
182 size_t data_size = 0;
183 void *fpga_data = NULL;
184 char *devstr = getenv ("fpga");
185 char *datastr = getenv ("fpgadata");
189 dev = (int) simple_strtoul (devstr, NULL, 16);
191 fpga_data = (void *) simple_strtoul (datastr, NULL, 16);
194 case 5: /* fpga <op> <dev> <data> <datasize> */
195 data_size = simple_strtoul (argv[4], NULL, 16);
196 case 4: /* fpga <op> <dev> <data> */
197 fpga_data = (void *) simple_strtoul (argv[3], NULL, 16);
198 PRINTF (__FUNCTION__ ": fpga_data = 0x%x\n",
200 case 3: /* fpga <op> <dev | data addr> */
201 dev = (int) simple_strtoul (argv[2], NULL, 16);
202 PRINTF (__FUNCTION__ ": device = %d\n", dev);
203 /* FIXME - this is a really weak test */
204 if ((argc == 3) && (dev > fpga_count ())) { /* must be buffer ptr */
206 ": Assuming buffer pointer in arg 3\n");
207 fpga_data = (void *) dev;
208 PRINTF (__FUNCTION__ ": fpga_data = 0x%x\n",
210 dev = FPGA_INVALID_DEVICE; /* reset device num */
212 case 2: /* fpga <op> */
213 op = (int) fpga_get_op (argv[1]);
216 PRINTF (__FUNCTION__ ": Too many or too few args (%d)\n",
218 op = FPGA_NONE; /* force usage display */
228 rc = fpga_info (dev);
232 rc = fpga_load (dev, fpga_data, data_size);
236 rc = fpga_loadbitstream(dev, fpga_data, data_size);
240 rc = fpga_dump (dev, fpga_data, data_size);
244 printf ("Unknown operation.\n");
251 static void fpga_usage (cmd_tbl_t * cmdtp)
253 printf ("Usage:\n%s\n", cmdtp->usage);
257 * Map op to supported operations. We don't use a table since we
258 * would just have to relocate it from flash anyway.
260 static int fpga_get_op (char *opstr)
264 if (!strcmp ("info", opstr)) {
266 } else if (!strcmp ("loadb", opstr)) {
268 } else if (!strcmp ("load", opstr)) {
270 } else if (!strcmp ("dump", opstr)) {
274 if (op == FPGA_NONE) {
275 printf ("Unknown fpga operation \"%s\"\n", opstr);
280 U_BOOT_CMD (fpga, 6, 1, do_fpga,
281 "fpga - loadable FPGA image support\n",
282 "fpga [operation type] [device number] [image address] [image size]\n"
284 "\tinfo\tlist known device information.\n"
285 "\tload\tLoad device from memory buffer.\n"
286 "\tloadb\tLoad device from bitstream buffer (Xilinx devices only).\n"
287 "\tdump\tLoad device to memory buffer.\n");
288 #endif /* CONFIG_FPGA && CONFIG_COMMANDS & CFG_CMD_FPGA */