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)
41 #define PRINTF(fmt,args...) printf (fmt ,##args)
43 #define PRINTF(fmt,args...)
46 #if defined (CONFIG_FPGA) && ( CONFIG_COMMANDS & CFG_CMD_FPGA )
49 static void fpga_usage (cmd_tbl_t
* cmdtp
);
50 static int fpga_get_op (char *opstr
);
59 /* Convert bitstream data and load into the fpga */
60 int fpga_loadbitstream(unsigned long dev
, char* fpgadata
, size_t size
)
63 unsigned char* swapdata
;
64 unsigned int swapsize
;
67 unsigned char *dataptr
;
72 dataptr
= (unsigned char *)fpgadata
;
75 /* skip the first bytes of the bitsteam, their meaning is unknown */
76 length
= (*dataptr
<< 8) + *(dataptr
+1);
80 /* get design name (identifier, length, string) */
81 length
= (*dataptr
<< 8) + *(dataptr
+1);
83 if (*dataptr
++ != 0x61) {
84 PRINTF ("%s: Design name identifier not recognized in bitstream\n",
89 length
= (*dataptr
<< 8) + *(dataptr
+1);
94 printf(" design filename = \"%s\"\n", buffer
);
96 /* get part number (identifier, length, string) */
97 if (*dataptr
++ != 0x62) {
98 printf("%s: Part number 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(" part number = \"%s\"\n", buffer
);
109 /* get date (identifier, length, string) */
110 if (*dataptr
++ != 0x63) {
111 printf("%s: Date 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(" date = \"%s\"\n", buffer
);
122 /* get time (identifier, length, string) */
123 if (*dataptr
++ != 0x64) {
124 printf("%s: Time identifier not recognized in bitstream\n",__FUNCTION__
);
128 length
= (*dataptr
<< 8) + *(dataptr
+1);
130 for(i
=0;i
<length
;i
++)
131 buffer
[i
]=*dataptr
++;
132 printf(" time = \"%s\"\n", buffer
);
134 /* get fpga data length (identifier, length) */
135 if (*dataptr
++ != 0x65) {
136 printf("%s: Data length identifier not recognized in bitstream\n",
140 swapsize
= ((unsigned int) *dataptr
<<24) +
141 ((unsigned int) *(dataptr
+1) <<16) +
142 ((unsigned int) *(dataptr
+2) <<8 ) +
143 ((unsigned int) *(dataptr
+3) ) ;
145 printf(" bytes in bitstream = %d\n", swapsize
);
147 /* check consistency of length obtained */
148 if (swapsize
>= size
) {
149 printf("%s: Could not find right length of data in bitstream\n",
154 /* allocate memory */
155 swapdata
= (unsigned char *)malloc(swapsize
);
156 if (swapdata
== NULL
) {
157 printf("%s: Could not allocate %d bytes memory !\n",
158 __FUNCTION__
, swapsize
);
162 /* read data into memory and swap bits */
164 for (i
= 0; i
< swapsize
; i
++) {
166 data
|= (*dataptr
& 0x01) << 7;
167 data
|= (*dataptr
& 0x02) << 5;
168 data
|= (*dataptr
& 0x04) << 3;
169 data
|= (*dataptr
& 0x08) << 1;
170 data
|= (*dataptr
& 0x10) >> 1;
171 data
|= (*dataptr
& 0x20) >> 3;
172 data
|= (*dataptr
& 0x40) >> 5;
173 data
|= (*dataptr
& 0x80) >> 7;
178 rc
= fpga_load(dev
, swapdata
, swapsize
);
182 printf("Bitstream support only for Xilinx devices\n");
187 /* ------------------------------------------------------------------------- */
189 * fpga <op> <device number> <data addr> <datasize>
190 * where op is 'load', 'dump', or 'info'
191 * If there is no device number field, the fpga environment variable is used.
192 * If there is no data addr field, the fpgadata environment variable is used.
193 * The info command requires no data address field.
195 int do_fpga (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
197 int op
, dev
= FPGA_INVALID_DEVICE
;
198 size_t data_size
= 0;
199 void *fpga_data
= NULL
;
200 char *devstr
= getenv ("fpga");
201 char *datastr
= getenv ("fpgadata");
205 dev
= (int) simple_strtoul (devstr
, NULL
, 16);
207 fpga_data
= (void *) simple_strtoul (datastr
, NULL
, 16);
210 case 5: /* fpga <op> <dev> <data> <datasize> */
211 data_size
= simple_strtoul (argv
[4], NULL
, 16);
212 case 4: /* fpga <op> <dev> <data> */
213 fpga_data
= (void *) simple_strtoul (argv
[3], NULL
, 16);
214 PRINTF ("%s: fpga_data = 0x%x\n", __FUNCTION__
, (uint
) fpga_data
);
215 case 3: /* fpga <op> <dev | data addr> */
216 dev
= (int) simple_strtoul (argv
[2], NULL
, 16);
217 PRINTF ("%s: device = %d\n", __FUNCTION__
, dev
);
218 /* FIXME - this is a really weak test */
219 if ((argc
== 3) && (dev
> fpga_count ())) { /* must be buffer ptr */
220 PRINTF ("%s: Assuming buffer pointer in arg 3\n",
222 fpga_data
= (void *) dev
;
223 PRINTF ("%s: fpga_data = 0x%x\n",
224 __FUNCTION__
, (uint
) fpga_data
);
225 dev
= FPGA_INVALID_DEVICE
; /* reset device num */
227 case 2: /* fpga <op> */
228 op
= (int) fpga_get_op (argv
[1]);
231 PRINTF ("%s: Too many or too few args (%d)\n",
233 op
= FPGA_NONE
; /* force usage display */
243 rc
= fpga_info (dev
);
247 rc
= fpga_load (dev
, fpga_data
, data_size
);
251 rc
= fpga_loadbitstream(dev
, fpga_data
, data_size
);
255 rc
= fpga_dump (dev
, fpga_data
, data_size
);
259 printf ("Unknown operation\n");
266 static void fpga_usage (cmd_tbl_t
* cmdtp
)
268 printf ("Usage:\n%s\n", cmdtp
->usage
);
272 * Map op to supported operations. We don't use a table since we
273 * would just have to relocate it from flash anyway.
275 static int fpga_get_op (char *opstr
)
279 if (!strcmp ("info", opstr
)) {
281 } else if (!strcmp ("loadb", opstr
)) {
283 } else if (!strcmp ("load", opstr
)) {
285 } else if (!strcmp ("dump", opstr
)) {
289 if (op
== FPGA_NONE
) {
290 printf ("Unknown fpga operation \"%s\"\n", opstr
);
295 U_BOOT_CMD (fpga
, 6, 1, do_fpga
,
296 "fpga - loadable FPGA image support\n",
297 "fpga [operation type] [device number] [image address] [image size]\n"
299 "\tinfo\tlist known device information\n"
300 "\tload\tLoad device from memory buffer\n"
301 "\tloadb\tLoad device from bitstream buffer (Xilinx devices only)\n"
302 "\tdump\tLoad device to memory buffer\n");
303 #endif /* CONFIG_FPGA && CONFIG_COMMANDS & CFG_CMD_FPGA */