[U-Boot-Users] [PATCH 6/6] cfi_flash: Use map_physmem() and unmap_physmem()

Haavard Skinnemoen hskinnemoen at atmel.com
Tue Dec 11 16:28:20 CET 2007


Use map_physmem() and unmap_physmem() to convert from physical to
virtual addresses. This gives the arch a chance to provide an uncached
mapping for flash accesses.

Signed-off-by: Haavard Skinnemoen <hskinnemoen at atmel.com>
---
 drivers/mtd/cfi_flash.c |  149 +++++++++++++++++++++++++++++------------------
 1 files changed, 93 insertions(+), 56 deletions(-)

diff --git a/drivers/mtd/cfi_flash.c b/drivers/mtd/cfi_flash.c
index 70a626a..94208e2 100644
--- a/drivers/mtd/cfi_flash.c
+++ b/drivers/mtd/cfi_flash.c
@@ -233,10 +233,15 @@ static flash_info_t *flash_get_info(ulong base)
 /*-----------------------------------------------------------------------
  * create an address based on the offset and the port width
  */
-static inline uchar *
-flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
+static inline void *
+flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
 {
-	return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
+	return map_physmem(info->start[sect] + (offset * info->portwidth));
+}
+
+static inline void flash_unmap(void *addr)
+{
+	unmap_physmem(addr);
 }
 
 /*-----------------------------------------------------------------------
@@ -275,8 +280,7 @@ static void flash_printqry (flash_info_t * info, flash_sect_t sect)
 	int x, y;
 
 	for (x = 0; x < 0x40; x += 16U / info->portwidth) {
-		addr = flash_make_addr (info, sect,
-					x + FLASH_OFFSET_CFI_RESP);
+		addr = flash_map(info, sect, x + FLASH_OFFSET_CFI_RESP);
 		debug ("%p : ", addr);
 		for (y = 0; y < 16; y++) {
 			debug ("%2.2x ", flash_read8(addr + y));
@@ -291,6 +295,7 @@ static void flash_printqry (flash_info_t * info, flash_sect_t sect)
 			}
 		}
 		debug ("\n");
+		flash_unmap(addr);
 	}
 }
 #endif
@@ -302,13 +307,16 @@ static void flash_printqry (flash_info_t * info, flash_sect_t sect)
 static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
 {
 	uchar *cp;
+	uchar retval;
 
-	cp = flash_make_addr (info, 0, offset);
+	cp = flash_map (info, 0, offset);
 #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
-	return (cp[0]);
+	retval = flash_read8(cp);
 #else
-	return (cp[info->portwidth - 1]);
+	retval = flash_read8(cp + info->portwidth - 1);
 #endif
+	flash_unmap (cp);
+	return retval;
 }
 
 /*-----------------------------------------------------------------------
@@ -323,23 +331,26 @@ static ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect,
 #ifdef DEBUG
 	int x;
 #endif
-	addr = flash_make_addr (info, sect, offset);
+	addr = flash_map (info, sect, offset);
 
 #ifdef DEBUG
 	debug ("ushort addr is at %p info->portwidth = %d\n", addr,
 	       info->portwidth);
 	for (x = 0; x < 2 * info->portwidth; x++) {
-		debug ("addr[%x] = 0x%x\n", x, addr[x]);
+		debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
 	}
 #endif
 #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
-	retval = ((addr[(info->portwidth)] << 8) | addr[0]);
+	retval = ((flash_read8(addr + info->portwidth) << 8) |
+		  flash_read8(addr));
 #else
-	retval = ((addr[(2 * info->portwidth) - 1] << 8) |
-		  addr[info->portwidth - 1]);
+	retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 8) |
+		  flash_read8(addr + info->portwidth - 1));
 #endif
 
 	debug ("retval = 0x%x\n", retval);
+	flash_unmap (addr);
+
 	return retval;
 }
 
@@ -356,25 +367,28 @@ static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
 #ifdef DEBUG
 	int x;
 #endif
-	addr = flash_make_addr (info, sect, offset);
+	addr = flash_map (info, sect, offset);
 
 #ifdef DEBUG
 	debug ("long addr is at %p info->portwidth = %d\n", addr,
 	       info->portwidth);
 	for (x = 0; x < 4 * info->portwidth; x++) {
-		debug ("addr[%x] = 0x%x\n", x, addr[x]);
+		debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
 	}
 #endif
 #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
-	retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
-		(addr[(2 * info->portwidth)]) |
-		(addr[(3 * info->portwidth)] << 8);
+	retval = ((flash_read8(addr) << 16) |
+		  (flash_read8(addr + info->portwidth) << 24) |
+		  (flash_read8(addr + 2 * info->portwidth)) |
+		  (flash_read8(addr + 3 * info->portwidth) << 8));
 #else
-	retval = (addr[(2 * info->portwidth) - 1] << 24) |
-		(addr[(info->portwidth) - 1] << 16) |
-		(addr[(4 * info->portwidth) - 1] << 8) |
-		addr[(3 * info->portwidth) - 1];
+	retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
+		  (flash_read8(addr + info->portwidth - 1) << 16) |
+		  (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
+		  (flash_read8(addr + 3 * info->portwidth - 1)));
 #endif
+	flash_unmap(addr);
+
 	return retval;
 }
 
@@ -388,7 +402,7 @@ static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
 	void *addr;
 	cfiword_t cword;
 
-	addr = flash_make_addr (info, sect, offset);
+	addr = flash_map (info, sect, offset);
 	flash_make_cmd (info, cmd, &cword);
 	switch (info->portwidth) {
 	case FLASH_CFI_8BIT:
@@ -426,6 +440,8 @@ static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
 
 	/* Ensure all the instructions are fully finished */
 	sync();
+
+	flash_unmap(addr);
 }
 
 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
@@ -443,7 +459,7 @@ static int flash_isequal (flash_info_t * info, flash_sect_t sect,
 	cfiword_t cword;
 	int retval;
 
-	addr = flash_make_addr (info, sect, offset);
+	addr = flash_map (info, sect, offset);
 	flash_make_cmd (info, cmd, &cword);
 
 	debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
@@ -477,6 +493,8 @@ static int flash_isequal (flash_info_t * info, flash_sect_t sect,
 		retval = 0;
 		break;
 	}
+	flash_unmap(addr);
+
 	return retval;
 }
 
@@ -489,7 +507,7 @@ static int flash_isset (flash_info_t * info, flash_sect_t sect,
 	cfiword_t cword;
 	int retval;
 
-	addr = flash_make_addr (info, sect, offset);
+	addr = flash_map (info, sect, offset);
 	flash_make_cmd (info, cmd, &cword);
 	switch (info->portwidth) {
 	case FLASH_CFI_8BIT:
@@ -508,6 +526,8 @@ static int flash_isset (flash_info_t * info, flash_sect_t sect,
 		retval = 0;
 		break;
 	}
+	flash_unmap(addr);
+
 	return retval;
 }
 
@@ -520,7 +540,7 @@ static int flash_toggle (flash_info_t * info, flash_sect_t sect,
 	cfiword_t cword;
 	int retval;
 
-	addr = flash_make_addr (info, sect, offset);
+	addr = flash_map (info, sect, offset);
 	flash_make_cmd (info, cmd, &cword);
 	switch (info->portwidth) {
 	case FLASH_CFI_8BIT:
@@ -543,6 +563,8 @@ static int flash_toggle (flash_info_t * info, flash_sect_t sect,
 		retval = 0;
 		break;
 	}
+	flash_unmap(addr);
+
 	return retval;
 }
 
@@ -712,12 +734,10 @@ static flash_sect_t find_sector (flash_info_t * info, ulong addr)
 static int flash_write_cfiword (flash_info_t * info, ulong dest,
 				cfiword_t cword)
 {
-	void *ctladdr;
 	void *dstaddr;
 	int flag;
 
-	ctladdr = flash_make_addr (info, 0, 0);
-	dstaddr = (uchar *)dest;
+	dstaddr = map_physmem(dest);
 
 	/* Check if Flash is (sufficiently) erased */
 	switch (info->portwidth) {
@@ -734,10 +754,13 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
 		flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
 		break;
 	default:
-		return 2;
+		flag = 0;
+		break;
 	}
-	if (!flag)
+	if (!flag) {
+		unmap_physmem(dstaddr);
 		return 2;
+	}
 
 	/* Disable interrupts which might cause a timeout here */
 	flag = disable_interrupts ();
@@ -777,6 +800,8 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
 	if (flag)
 		enable_interrupts ();
 
+	unmap_physmem(dstaddr);
+
 	return flash_full_status_check (info, find_sector (info, dest),
 					info->write_tout, "write");
 }
@@ -790,7 +815,7 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
 	int cnt;
 	int retcode;
 	void *src = cp;
-	void *dst = (void *)dest;
+	void *dst = map_physmem(dest);
 
 	sector = find_sector (info, dest);
 
@@ -819,8 +844,8 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
 				cnt = len >> 3;
 				break;
 			default:
-				return ERR_INVAL;
-				break;
+				retcode = ERR_INVAL;
+				goto out_unmap;
 			}
 			flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
 			while (cnt-- > 0) {
@@ -842,8 +867,8 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
 					src += 8, dst += 8;
 					break;
 				default:
-					return ERR_INVAL;
-					break;
+					retcode = ERR_INVAL;
+					goto out_unmap;
 				}
 			}
 			flash_write_cmd (info, sector, 0,
@@ -852,7 +877,8 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
 				info, sector, info->buffer_write_tout,
 				"buffer write");
 		}
-		return retcode;
+
+		break;
 
 	case CFI_CMDSET_AMD_STANDARD:
 	case CFI_CMDSET_AMD_EXTENDED:
@@ -893,19 +919,25 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
 			}
 			break;
 		default:
-			return ERR_INVAL;
+			retcode = ERR_INVAL;
+			goto out_unmap;
 		}
 
 		flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
 		retcode = flash_full_status_check (info, sector,
 						   info->buffer_write_tout,
 						   "buffer write");
-		return retcode;
+		break;
 
 	default:
 		debug ("Unknown Command Set\n");
-		return ERR_INVAL;
+		retcode = ERR_INVAL;
+		break;
 	}
+
+out_unmap:
+	unmap_physmem(dst);
+	return retcode;
 }
 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
 
@@ -1099,7 +1131,7 @@ void flash_print_info (flash_info_t * info)
 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
 {
 	ulong wp;
-	ulong cp;
+	uchar *p;
 	int aln;
 	cfiword_t cword;
 	int i, rc;
@@ -1108,26 +1140,28 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
 	int buffered_size;
 #endif
 	/* get lower aligned address */
-	/* get lower aligned address */
 	wp = (addr & ~(info->portwidth - 1));
 
 	/* handle unaligned start */
 	if ((aln = addr - wp) != 0) {
 		cword.l = 0;
-		cp = wp;
-		for (i = 0; i < aln; ++i, ++cp)
-			flash_add_byte (info, &cword, (*(uchar *) cp));
+		p = map_physmem(wp);
+		for (i = 0; i < aln; ++i)
+			flash_add_byte (info, &cword, flash_read8(p + i));
 
 		for (; (i < info->portwidth) && (cnt > 0); i++) {
 			flash_add_byte (info, &cword, *src++);
 			cnt--;
-			cp++;
 		}
-		for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
-			flash_add_byte (info, &cword, (*(uchar *) cp));
-		if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
+		for (; (cnt == 0) && (i < info->portwidth); ++i)
+			flash_add_byte (info, &cword, flash_read8(p + i));
+
+		rc = flash_write_cfiword (info, wp, cword);
+		unmap_physmem(p);
+		if (rc != 0)
 			return rc;
-		wp = cp;
+
+		wp += i;
 	}
 
 	/* handle the aligned part */
@@ -1178,13 +1212,14 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
 	 * handle unaligned tail bytes
 	 */
 	cword.l = 0;
-	for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
+	p = map_physmem(wp);
+	for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
 		flash_add_byte (info, &cword, *src++);
 		--cnt;
 	}
-	for (; i < info->portwidth; ++i, ++cp) {
-		flash_add_byte (info, &cword, (*(uchar *) cp));
-	}
+	for (; i < info->portwidth; ++i)
+		flash_add_byte (info, &cword, flash_read8(p + i));
+	unmap_physmem(p);
 
 	return flash_write_cfiword (info, wp, cword);
 }
@@ -1236,10 +1271,11 @@ void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
 	uchar *dst;
 
 	dst = buffer;
-	src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
+	src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
 	flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
 	memcpy (dst, src + offset, len);
 	flash_write_cmd (info, 0, 0, info->cmd_reset);
+	flash_unmap(src);
 }
 
 /*
@@ -1250,10 +1286,11 @@ void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
 {
 	uchar *src;
 
-	src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
+	src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
 	flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
 	memcpy (buffer, src + offset, len);
 	flash_write_cmd (info, 0, 0, info->cmd_reset);
+	flash_unmap(src);
 }
 
 #endif /* CFG_FLASH_PROTECTION */
-- 
1.5.3.4





More information about the U-Boot mailing list