Skip to contentSkip to conversation

Add support for boards where the NOR FLASH is not memory-mapped

PATCH New Stefan Roese 2 participants 3 messages Nov 12, 2008 – Dec 10, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for boards where the NOR FLASH is not memory-mapped

This patch adds the CONFIG_FLASH_NOT_MEM_MAPPED define which can be used on boards where the NOR FLASH is not memory-mapped and special accessor functions are needed to access the NOR FLASH. This affects the memory commands (cmd_mem.c) and the environment (env_flash.c).

Boards using CONFIG_FLASH_NOT_MEM_MAPPED need to additionally specify CONFIG_FLASH_BASE and CONFIG_FLASH_END so that the NOR FLASH region can be detected.

This will be used by the upcoming VCTH board support.

---
 common/cmd_mem.c    |   66 +++++++++++++++++++++++++++++++++++++++-----------
 common/env_flash.c  |   61 +++++++++++++++++++++++++++++++++++++----------
 lib_generic/crc32.c |   11 ++++++++
 3 files changed, 110 insertions(+), 28 deletions(-)
common/cmd_mem.c +51 -15
index d7666c2..f545f6a 100644
@@ -43,6 +43,43 @@
 #define PRINTF(fmt,args...)
 #endif
 
+#if defined(CONFIG_HAS_DATAFLASH)
+#define CONFIG_FLASH_NOT_MEM_MAPPED
+#define OK		DATAFLASH_OK
+#endif
+
+#if !defined(OK)
+#define OK		1
+#endif
+
+void *board_flash_read_memcpy(void *__to, __const__ void *__from, int n);
+
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+static inline int flash_read(void *__to, __const__ void *__from, int n)
+{
+#if defined(CONFIG_HAS_DATAFLASH)
+	return read_dataflash(__from, n, __to);
+#elif defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+	if ((addr2info((u32)__from) != NULL))
+		board_flash_read_memcpy(__to, __from, n);
+	else
+		memcpy(__to, __from, n);
+	return OK;
+#endif
+}
+#endif /* defined(CONFIG_FLASH_NOT_MEM_MAPPED) */
+
+static inline int is_flash_addr(u32 addr)
+{
+#if defined(CONFIG_HAS_DATAFLASH)
+	return addr_dataflash(addr);
+#elif defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+	return (addr2info(addr) != NULL);
+#else
+	return 1;
+#endif
+}
+
 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
 
 /* Display values from last command.
@@ -63,7 +100,7 @@ static	ulong	base_address = 0;
 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	ulong	addr, length;
-#if defined(CONFIG_HAS_DATAFLASH)
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
 	ulong	nbytes, linebytes;
 #endif
 	int	size;
@@ -100,7 +137,7 @@ int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 			length = simple_strtoul(argv[2], NULL, 16);
 	}
 
-#if defined(CONFIG_HAS_DATAFLASH)
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
 	/* Print the lines.
 	 *
 	 * We buffer all read data, so we can make sure data is read only
@@ -112,10 +149,13 @@ int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 		void* p;
 		linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
 
-		rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
-		p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
+		rc = flash_read(linebuf, (void *)addr, (linebytes/size)*size);
+		p = (rc == OK) ? linebuf : (void *)addr;
 		print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
 
+		/* Clear rc, otherwise command repeat doesn't work */
+		rc = 0;
+
 		nbytes -= linebytes;
 		addr += linebytes;
 		if (ctrlc()) {
@@ -294,9 +334,9 @@ int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 
 	count = simple_strtoul(argv[3], NULL, 16);
 
-#ifdef CONFIG_HAS_DATAFLASH
-	if (addr_dataflash(addr1) | addr_dataflash(addr2)){
-		puts ("Comparison with DataFlash space not supported.\n\r");
+#ifdef CONFIG_FLASH_NOT_MEM_MAPPED
+	if (is_flash_addr(addr1) || is_flash_addr(addr2)) {
+		puts ("Comparison with FLASH space not supported.\n\r");
 		return 0;
 	}
 #endif
@@ -385,11 +425,7 @@ int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 
 #ifndef CONFIG_SYS_NO_FLASH
 	/* check if we are copying to Flash */
-	if ( (addr2info(dest) != NULL)
-#ifdef CONFIG_HAS_DATAFLASH
-	   && (!addr_dataflash(dest))
-#endif
-	   ) {
+	if (is_flash_addr(dest)) {
 		int rc;
 
 		puts ("Copy to Flash... ");
@@ -1010,9 +1046,9 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
 		addr += base_address;
 	}
 
-#ifdef CONFIG_HAS_DATAFLASH
-	if (addr_dataflash(addr)){
-		puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
+#ifdef CONFIG_FLASH_NOT_MEM_MAPPED
+	if (is_flash_addr(addr)) {
+		puts ("Can't modify FLASH in place. Use cp instead.\n\r");
 		return 0;
 	}
 #endif
common/env_flash.c +48 -13

118 lines not on this page · raw message

lib_generic/crc32.c +11 -0
index b6a7a91..d698cc6 100644
@@ -148,7 +148,18 @@ const uint32_t * ZEXPORT get_crc_table()
 #endif
 
 /* ========================================================================= */
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+u8 flash_read8(void *addr);
+#define DO1(buf) \
+	if (((u32)buf >= CONFIG_FLASH_BASE) && ((u32)buf < CONFIG_FLASH_END)) { \
+		crc = crc_table[((int)crc ^ (flash_read8((void *)buf++))) & 0xff] ^ \
+			(crc >> 8);					\
+	} else { \
+		crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); \
+	}
+#else
 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
+#endif
 #define DO2(buf)  DO1(buf); DO1(buf);
 #define DO4(buf)  DO2(buf); DO2(buf);
 #define DO8(buf)  DO4(buf); DO4(buf);
Signature · 1 line
1.6.0.4
#1 lore raw

PATCH 1/1 Add support for boards where the NOR FLASH is not memory-mapped

Dear Stefan Roese,

Sorry for the late review...

Quoted 9 lines · In message <[email protected]> you wrote:

This patch adds the CONFIG_FLASH_NOT_MEM_MAPPED define which can be used on boards where the NOR FLASH is not memory-mapped and special accessor functions are needed to access the NOR FLASH. This affects the memory commands (cmd_mem.c) and the environment (env_flash.c).

Boards using CONFIG_FLASH_NOT_MEM_MAPPED need to additionally specify CONFIG_FLASH_BASE and CONFIG_FLASH_END so that the NOR FLASH region can be detected.

You have to document this (at least in the README).

But there is a general problem with this approach: U-Boot is based on the design that the actual flash size is auto-detected, i. e. it is always assumed to be unknown at compile time. Therefore it is impossible to set something like CONFIG_FLASH_END at compile time.

Review

Quoted 2 lines
(quoted diff) +2 -0
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+	if (((u32)addr >= CONFIG_FLASH_BASE) && ((u32)addr < CONFIG_FLASH_END))

And this looks as if CONFIG_FLASH_END was not the "FLASH_END" address, i. e. the last address in flash, but "FLASH_END" + 1 ?

common/env_flash.c static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; · line 114

Quoted 5 lines
common/env_flash.c +5 -0
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+#define FLASH_READ_MEMCPY(d, s, n)	board_flash_read_memcpy(d, s, n)
+#else
+#define FLASH_READ_MEMCPY(d, s, n)	memcpy(d, s, n);
+#endif /* CONFIG_FLASH_NOT_MEM_MAPPED */

This is really, really ugly - and error prone, as you must be extremely careful which of the functions you may call might use memcpy() or similar internally.

You know that I know of the specific problems of this hardware, but anyway - I really dislike having to add such code.

Quoted 5 lines
lib_generic/crc32.c +5 -0
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+u8 flash_read8(void *addr);
+#define DO1(buf) \
+	if (((u32)buf >= CONFIG_FLASH_BASE) && ((u32)buf < CONFIG_FLASH_END)) { \
+		crc = crc_table[((int)crc ^ (flash_read8((void *)buf++))) & 0xff] ^ \

lib_generic/crc32.c const uint32_t * ZEXPORT get_crc_table() · line 165

Quoted 10 lines
lib_generic/crc32.c +6 -0
+			(crc >> 8);					\
+	} else { \
+		crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); \
+	}
+#else
 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
+#endif
 #define DO2(buf)  DO1(buf); DO1(buf);
 #define DO4(buf)  DO2(buf); DO2(buf);
 #define DO8(buf)  DO4(buf); DO4(buf);

Please wrap all such macros in the usual "do { ... } while (0)" wrappers.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
There is nothing in this world constant but inconstancy.      - Swift
#2 lore raw

PATCH 1/1 Add support for boards where the NOR FLASH is not memory-mapped

Hi Wolfgang,

Quoted 9 lines · Wolfgang Denk

Sorry for the late review...

Quoted 6 lines · In message <[email protected]> you wrote:

This patch adds the CONFIG_FLASH_NOT_MEM_MAPPED define which can be used on boards where the NOR FLASH is not memory-mapped and special accessor functions are needed to access the NOR FLASH. This affects the memory commands (cmd_mem.c) and the environment (env_flash.c).

Quoted 5 lines
Quoted 3 lines

Boards using CONFIG_FLASH_NOT_MEM_MAPPED need to additionally specify CONFIG_FLASH_BASE and CONFIG_FLASH_END so that the NOR FLASH region can be detected.

You have to document this (at least in the README).

No, I missed it. I'll send an updated patch later this week.

Quoted 4 lines

But there is a general problem with this approach: U-Boot is based on the design that the actual flash size is auto-detected, i. e. it is always assumed to be unknown at compile time. Therefore it is impossible to set something like CONFIG_FLASH_END at compile time.

Yes, this approach is not optimal but I found no other way to work with the CFI driver and especially the environment in NOR without such an "extension". But CONFIG_FLASH_END doesn't have point to the real end of the NOR FLASH but the highest possible address. So auto-detection should still be possible in such an area. Here an example from the VCTH board port:

/*
* For the non-memory-mapped NOR FLASH, we need to define the
* NOR FLASH area. This can't be detected via the addr2info()
* function, since we check for flash access in the very early
* U-Boot code, before the NOR FLASH is detected.
*/
#define CONFIG_FLASH_BASE 0xb0000000
#define CONFIG_FLASH_END 0xbfffffff

So the FLASH size could be a maximum of 256MB in this case. Not optimal but at least not totally fixed.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+	if (((u32)addr >= CONFIG_FLASH_BASE) && ((u32)addr < CONFIG_FLASH_END))

And this looks as if CONFIG_FLASH_END was not the "FLASH_END" address, i. e. the last address in flash, but "FLASH_END" + 1 ?

Good catch. I'll fix this to "((u32)addr <= CONFIG_FLASH_END).

Quoted 10 lines

common/env_flash.c static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; · line 114

Quoted 5 lines
common/env_flash.c +5 -0
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+#define FLASH_READ_MEMCPY(d, s, n)	board_flash_read_memcpy(d, s, n)
+#else
+#define FLASH_READ_MEMCPY(d, s, n)	memcpy(d, s, n);
+#endif /* CONFIG_FLASH_NOT_MEM_MAPPED */

This is really, really ugly - and error prone, as you must be extremely careful which of the functions you may call might use memcpy() or similar internally.

Quoted 2 lines

You know that I know of the specific problems of this hardware, but anyway - I really dislike having to add such code.

Yes, we agree that this is not a "nice" solution. But this is the best I could come up after several approaches. I'm open to better suggestions.

Quoted 10 lines
Quoted 10 lines
lib_generic/crc32.c +4 -0
+#if defined(CONFIG_FLASH_NOT_MEM_MAPPED)
+u8 flash_read8(void *addr);
+#define DO1(buf) \
+	if (((u32)buf >= CONFIG_FLASH_BASE) && ((u32)buf < CONFIG_FLASH_END)) {

\ + crc = crc_table[((int)crc ^ (flash_read8((void *)buf++))) & 0xff] ^
\ + (crc >> 8); \

lib_generic/crc32.c +4 -0
+	} else { \
+		crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); \
+	}
+#else
Quoted 8 lines
Quoted 5 lines

#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >>
8); +#endif

 #define DO2(buf)  DO1(buf); DO1(buf);
 #define DO4(buf)  DO2(buf); DO2(buf);
 #define DO8(buf)  DO4(buf); DO4(buf);

Please wrap all such macros in the usual "do { ... } while (0)" wrappers.

OK, will do in the next version.

Thanks for the review.

Best regards,
Stefan

=====================================================================
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80 Email: office at denx.de
=====================================================================