* Patch by Nicolas Lacressonnière, 12 Nov 2003:
  update for for Atmel AT91RM9200DK development kit:
  - support for environment variables in DataFlash
  - Atmel DataFlash AT45DB1282 support

* Patch by Jeff Carr, 11 Nov 2003:
  add support for new version of 8270 processors

* Patches by George G. Davis, 05 Nov 2003:
  - only pass the ARM linux initrd tag to the kernel when an initrd
    is actually present
  - update omap1510inn configuration file
diff --git a/common/Makefile b/common/Makefile
index dae5942..5595c23 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -42,7 +42,7 @@
 	  cmd_reginfo.o cmd_scsi.o cmd_spi.o cmd_usb.o cmd_vfd.o \
 	  command.o console.o devices.o dlmalloc.o docecc.o \
 	  environment.o env_common.o \
-	  env_flash.o env_eeprom.o env_nvram.o env_nowhere.o exports.o \
+	  env_dataflash.o env_flash.o env_eeprom.o env_nvram.o env_nowhere.o exports.o \
 	  flash.o fpga.o \
 	  hush.o kgdb.o lists.o lynxkdi.o miiphybb.o miiphyutil.o \
 	  s_record.o soft_i2c.o soft_spi.o spartan2.o \
diff --git a/common/cmd_flash.c b/common/cmd_flash.c
index 430a33e..a51b5e9 100644
--- a/common/cmd_flash.c
+++ b/common/cmd_flash.c
@@ -307,7 +307,9 @@
 	ulong bank, addr_first, addr_last;
 	int i, p, n, sect_first, sect_last;
 	int rcode = 0;
-
+#ifdef CONFIG_HAS_DATAFLASH
+	int status;
+#endif
 	if (argc < 3) {
 		printf ("Usage:\n%s\n", cmdtp->usage);
 		return 1;
@@ -322,6 +324,24 @@
 		return 1;
 	}
 
+#ifdef CONFIG_HAS_DATAFLASH
+	if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
+		addr_first = simple_strtoul(argv[2], NULL, 16);
+		addr_last  = simple_strtoul(argv[3], NULL, 16);
+
+		if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
+			status = dataflash_real_protect(p,addr_first,addr_last);
+			if (status < 0){
+				printf("Bad DataFlash sector specification\n");
+                		return 1;
+        		}
+        		printf("%sProtect %d DataFlash Sectors\n",
+                		p ? "" : "Un-", status);
+			return 0;
+		}
+	}
+#endif
+	
 	if (strcmp(argv[2], "all") == 0) {
 		for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
 			info = &flash_info[bank-1];
diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 3918678..35b3901 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -136,13 +136,19 @@
 		uint	*uip = (uint   *)linebuf;
 		ushort	*usp = (ushort *)linebuf;
 		u_char	*ucp = (u_char *)linebuf;
-
+#ifdef CONFIG_HAS_DATAFLASH
+		int rc;
+#endif
 		printf("%08lx:", addr);
 		linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
 
 #ifdef CONFIG_HAS_DATAFLASH
-		if (read_dataflash(addr, (linebytes/size)*size, linebuf) != -1){
-
+		if ((rc = read_dataflash(addr, (linebytes/size)*size, linebuf)) == DATAFLASH_OK){
+			/* if outside dataflash */
+			/*if (rc != 1) {
+        	                dataflash_perror (rc);
+                	        return (1);
+	                }*/
 			for (i=0; i<linebytes; i+= size) {
 				if (size == 4) {
 					printf(" %08x", *uip++);
@@ -430,7 +436,12 @@
 
 	/* Check if we are copying from DataFlash to RAM */
 	if (addr_dataflash(addr) && !addr_dataflash(dest) && (addr2info(dest)==NULL) ){
-		read_dataflash(addr, count * size, (char *) dest);
+		int rc;
+		rc = read_dataflash(addr, count * size, (char *) dest);
+		if (rc != 1) {
+                        dataflash_perror (rc);
+                        return (1);
+                }
 		return 0;
 	}
 
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 53df582..a3898dd 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -49,8 +49,12 @@
 #include <net.h>
 #endif
 
-#if !defined(CFG_ENV_IS_IN_NVRAM) && !defined(CFG_ENV_IS_IN_EEPROM) && !defined(CFG_ENV_IS_IN_FLASH) && !defined(CFG_ENV_IS_NOWHERE)
-# error Define one of CFG_ENV_IS_IN_NVRAM, CFG_ENV_IS_IN_EEPROM, CFG_ENV_IS_IN_FLASH, CFG_ENV_IS_NOWHERE
+#if !defined(CFG_ENV_IS_IN_NVRAM)	&& \
+    !defined(CFG_ENV_IS_IN_EEPROM)	&& \
+    !defined(CFG_ENV_IS_IN_FLASH)	&& \
+    !defined(CFG_ENV_IS_IN_DATAFLASH)	&& \
+    !defined(CFG_ENV_IS_NOWHERE)
+# error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE}
 #endif
 
 #define XMK_STR(x)	#x
diff --git a/common/env_dataflash.c b/common/env_dataflash.c
new file mode 100644
index 0000000..8bfbbc9
--- /dev/null
+++ b/common/env_dataflash.c
@@ -0,0 +1,104 @@
+/* LowLevel function for DataFlash environment support
+ * Author : Gilles Gastaldi (Atmel)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ */
+#include <common.h>
+
+#if defined(CFG_ENV_IS_IN_DATAFLASH) /* Environment is in DataFlash */
+
+#include <command.h>
+#include <environment.h>
+#include <linux/stddef.h>
+#include <malloc.h>
+#include <dataflash.h>
+
+env_t *env_ptr = NULL;
+
+char * env_name_spec = "dataflash";
+
+extern int read_dataflash (unsigned long addr, unsigned long size, char
+*result);
+extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
+		     unsigned long size);
+extern int AT91F_DataflashInit (void);
+extern uchar default_environment[];
+/* extern int default_environment_size; */
+
+
+uchar env_get_char_spec (int index)
+{
+	uchar c;
+	read_dataflash (CFG_ENV_ADDR+index+offsetof(env_t,data),1,&c);
+	return (c);
+}
+
+void env_relocate_spec (void)
+{
+	read_dataflash (CFG_ENV_ADDR,CFG_ENV_SIZE,(uchar *)env_ptr);
+}
+
+int saveenv(void)
+{
+/* env must be copied to do not alter env structure in memory*/
+unsigned char temp[CFG_ENV_SIZE];
+int i;
+	memcpy(temp, env_ptr, CFG_ENV_SIZE);
+	return write_dataflash (CFG_ENV_ADDR, (unsigned long)temp, CFG_ENV_SIZE);
+}
+
+/************************************************************************
+ * Initialize Environment use
+ *
+ * We are still running from ROM, so data use is limited
+ * Use a (moderately small) buffer on the stack
+ */
+int env_init(void)
+{
+	DECLARE_GLOBAL_DATA_PTR;
+
+	ulong crc, len, new;
+	unsigned off;
+	uchar buf[64];
+	if (gd->env_valid == 0){
+		AT91F_DataflashInit();	/* prepare for DATAFLASH read/write */
+
+		/* read old CRC */
+		read_dataflash (CFG_ENV_ADDR+offsetof(env_t,crc),sizeof(ulong),&crc);
+		new = 0;
+		len = ENV_SIZE;
+		off = offsetof(env_t,data);
+		while (len > 0) {
+			int n = (len > sizeof(buf)) ? sizeof(buf) : len;
+			read_dataflash (CFG_ENV_ADDR+off,n , buf);
+			new = crc32 (new, buf, n);
+			len -= n;
+			off += n;
+		}
+		if (crc == new) {
+			gd->env_addr  = offsetof(env_t,data);
+			gd->env_valid = 1;
+		} else {
+			gd->env_addr  = (ulong)&default_environment[0];
+			gd->env_valid = 0;
+		}
+	}
+
+ 	return (0);
+}
+
+#endif /* CFG_ENV_IS_IN_DATAFLASH */