* Patch by Gleb Natapov, 07 Sep 2004:
  mpc824x: set PCI latency timer to a sane value
  (is 0 after reset).

* Patch by Kurt Stremerch, 03 Sep 2004:
  Add bitstream configuration option for fpga command (Xilinx only).
diff --git a/common/cmd_fpga.c b/common/cmd_fpga.c
index 325fdd1..7c762ba 100644
--- a/common/cmd_fpga.c
+++ b/common/cmd_fpga.c
@@ -52,8 +52,122 @@
 #define FPGA_NONE   -1
 #define FPGA_INFO   0
 #define FPGA_LOAD   1
+#define FPGA_LOADB  2
 #define FPGA_DUMP   3
 
+/* Convert bitstream data and load into the fpga */
+int fpga_loadbitstream(unsigned long dev, char* fpgadata, size_t size)
+{
+	int length;
+	char* swapdata;
+	int swapsize;
+	char buffer[80];
+	char *ptr;
+	char *dataptr;
+	int data;
+	int i;
+	int rc;
+
+	dataptr = fpgadata;
+
+#if CFG_FPGA_XILINX
+	/* skip the first 13 bytes of the bitsteam, their meaning is unknown */
+	dataptr+=13;
+
+	/* get design name (identifier, length, string) */
+	if (*dataptr++ != 0x61) {
+		PRINTF(__FUNCTION__ ": Design name identifier not recognized in bitstream.\n");
+		return FPGA_FAIL;
+	}
+
+	length = (*dataptr << 8) + *(dataptr+1); 
+	dataptr+=2;
+	for(i=0;i<length;i++)
+		buffer[i]=*dataptr++;
+	
+	buffer[length-5]='\0'; /* remove filename extension */
+	PRINTF(__FUNCTION__ ": design name = \"%s\".\n",buffer);
+
+	/* get part number (identifier, length, string) */
+	if (*dataptr++ != 0x62) {
+		printf(__FUNCTION__ ": Part number identifier not recognized in bitstream.\n");
+		return FPGA_FAIL;
+	}
+	
+	length = (*dataptr << 8) + *(dataptr+1); dataptr+=2;
+	for(i=0;i<length;i++) 
+		buffer[i]=*dataptr++;
+	PRINTF(__FUNCTION__ ": part number = \"%s\".\n",buffer);
+	
+	/* get date (identifier, length, string) */
+	if (*dataptr++ != 0x63) {
+		printf(__FUNCTION__ ": Date identifier not recognized in bitstream.\n");
+		return FPGA_FAIL;
+	}
+	
+	length = (*dataptr << 8) + *(dataptr+1); dataptr+=2;
+	for(i=0;i<length;i++)
+		buffer[i]=*dataptr++;
+	PRINTF(__FUNCTION__ ": date = \"%s\".\n",buffer);
+
+	/* get time (identifier, length, string) */
+	if (*dataptr++ != 0x64) {
+		printf(__FUNCTION__ ": Time identifier not recognized in bitstream.\n");
+		return FPGA_FAIL;
+	}
+	
+	length = (*dataptr << 8) + *(dataptr+1); dataptr+=2;
+	for(i=0;i<length;i++)
+		buffer[i]=*dataptr++;
+	PRINTF(__FUNCTION__ ": time = \"%s\".\n",buffer);
+	
+	/* get fpga data length (identifier, length) */
+	if (*dataptr++ != 0x65) {
+		printf(__FUNCTION__ ": Data length identifier not recognized in bitstream.\n");
+		return FPGA_FAIL;
+	}
+	swapsize = ((long)*dataptr<<24) + ((long)*(dataptr+1)<<16) + ((long)*(dataptr+2)<<8) + (long)*(dataptr+3);
+	dataptr+=4;
+	PRINTF(__FUNCTION__ ": bytes in bitstream = %d.\n",swapsize);
+	
+	/* check consistency of length obtained */
+	if (swapsize >= size) {
+		printf(__FUNCTION__ ": Could not find right length of data in bitstream.\n");
+		return FPGA_FAIL;
+	}
+	
+	/* allocate memory */
+	swapdata = (char *)malloc(swapsize);
+	if (swapdata == NULL) {
+		printf(__FUNCTION__ ": Could not allocate %d bytes memory !\n",swapsize);
+		return FPGA_FAIL;
+	}
+	
+	/* read data into memory and swap bits */
+	ptr = swapdata;
+	for (i = 0; i < swapsize; i++) {
+		data = 0x00;
+		data |= (*dataptr & 0x01) << 7;
+		data |= (*dataptr & 0x02) << 5;
+		data |= (*dataptr & 0x04) << 3;
+		data |= (*dataptr & 0x08) << 1;
+		data |= (*dataptr & 0x10) >> 1;
+		data |= (*dataptr & 0x20) >> 3;
+		data |= (*dataptr & 0x40) >> 5;
+		data |= (*dataptr & 0x80) >> 7;
+		*ptr++ = data;
+		dataptr++;
+	}
+
+	rc = fpga_load(dev, swapdata, swapsize);
+	free(swapdata);
+	return rc;
+#else
+	printf("Bitstream support only for Xilinx devices.\n");
+	return FPGA_FAIL;
+#endif
+}
+
 /* ------------------------------------------------------------------------- */
 /* command form:
  *   fpga <op> <device number> <data addr> <datasize>
@@ -118,6 +232,10 @@
 		rc = fpga_load (dev, fpga_data, data_size);
 		break;
 
+	case FPGA_LOADB:
+		rc = fpga_loadbitstream(dev, fpga_data, data_size);
+		break;
+
 	case FPGA_DUMP:
 		rc = fpga_dump (dev, fpga_data, data_size);
 		break;
@@ -145,6 +263,8 @@
 
 	if (!strcmp ("info", opstr)) {
 		op = FPGA_INFO;
+	} else if (!strcmp ("loadb", opstr)) {
+		op = FPGA_LOADB;
 	} else if (!strcmp ("load", opstr)) {
 		op = FPGA_LOAD;
 	} else if (!strcmp ("dump", opstr)) {
@@ -163,5 +283,6 @@
 	    "fpga operations:\n"
 	    "\tinfo\tlist known device information.\n"
 	    "\tload\tLoad device from memory buffer.\n"
+	    "\tloadb\tLoad device from bitstream buffer (Xilinx devices only).\n"
 	    "\tdump\tLoad device to memory buffer.\n");
 #endif /* CONFIG_FPGA && CONFIG_COMMANDS & CFG_CMD_FPGA */