cfi_flash: Use map_physmem() and unmap_physmem()

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@atmel.com>
This commit is contained in:
Haavard Skinnemoen 2007-12-13 12:56:34 +01:00 committed by Stefan Roese
parent 4d7d6936eb
commit 12d30aa797
1 changed files with 109 additions and 60 deletions

View File

@ -232,13 +232,33 @@ static flash_info_t *flash_get_info(ulong base)
} }
#endif #endif
unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
{
if (sect != (info->sector_count - 1))
return info->start[sect + 1] - info->start[sect];
else
return info->start[0] + info->size - info->start[sect];
}
/*----------------------------------------------------------------------- /*-----------------------------------------------------------------------
* create an address based on the offset and the port width * create an address based on the offset and the port width
*/ */
static inline uchar * static inline void *
flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset) flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
{ {
return ((uchar *) (info->start[sect] + (offset * info->portwidth))); unsigned int byte_offset = offset * info->portwidth;
return map_physmem(info->start[sect] + byte_offset,
flash_sector_size(info, sect) - byte_offset,
MAP_NOCACHE);
}
static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
unsigned int offset, void *addr)
{
unsigned int byte_offset = offset * info->portwidth;
unmap_physmem(addr, flash_sector_size(info, sect) - byte_offset);
} }
/*----------------------------------------------------------------------- /*-----------------------------------------------------------------------
@ -277,8 +297,7 @@ static void flash_printqry (flash_info_t * info, flash_sect_t sect)
int x, y; int x, y;
for (x = 0; x < 0x40; x += 16U / info->portwidth) { for (x = 0; x < 0x40; x += 16U / info->portwidth) {
addr = flash_make_addr (info, sect, addr = flash_map(info, sect, x + FLASH_OFFSET_CFI_RESP);
x + FLASH_OFFSET_CFI_RESP);
debug ("%p : ", addr); debug ("%p : ", addr);
for (y = 0; y < 16; y++) { for (y = 0; y < 16; y++) {
debug ("%2.2x ", flash_read8(addr + y)); debug ("%2.2x ", flash_read8(addr + y));
@ -293,6 +312,7 @@ static void flash_printqry (flash_info_t * info, flash_sect_t sect)
} }
} }
debug ("\n"); debug ("\n");
flash_unmap(info, sect, x + FLASH_OFFSET_CFI_RESP, addr);
} }
} }
#endif #endif
@ -304,13 +324,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) static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
{ {
uchar *cp; 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) #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
return (cp[0]); retval = flash_read8(cp);
#else #else
return (cp[info->portwidth - 1]); retval = flash_read8(cp + info->portwidth - 1);
#endif #endif
flash_unmap (info, 0, offset, cp);
return retval;
} }
/*----------------------------------------------------------------------- /*-----------------------------------------------------------------------
@ -325,23 +348,26 @@ static ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect,
#ifdef DEBUG #ifdef DEBUG
int x; int x;
#endif #endif
addr = flash_make_addr (info, sect, offset); addr = flash_map (info, sect, offset);
#ifdef DEBUG #ifdef DEBUG
debug ("ushort addr is at %p info->portwidth = %d\n", addr, debug ("ushort addr is at %p info->portwidth = %d\n", addr,
info->portwidth); info->portwidth);
for (x = 0; x < 2 * info->portwidth; x++) { 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 #endif
#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA) #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 #else
retval = ((addr[(2 * info->portwidth) - 1] << 8) | retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 8) |
addr[info->portwidth - 1]); flash_read8(addr + info->portwidth - 1));
#endif #endif
debug ("retval = 0x%x\n", retval); debug ("retval = 0x%x\n", retval);
flash_unmap (info, sect, offset, addr);
return retval; return retval;
} }
@ -358,25 +384,28 @@ static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
#ifdef DEBUG #ifdef DEBUG
int x; int x;
#endif #endif
addr = flash_make_addr (info, sect, offset); addr = flash_map (info, sect, offset);
#ifdef DEBUG #ifdef DEBUG
debug ("long addr is at %p info->portwidth = %d\n", addr, debug ("long addr is at %p info->portwidth = %d\n", addr,
info->portwidth); info->portwidth);
for (x = 0; x < 4 * info->portwidth; x++) { 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 #endif
#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA) #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) | retval = ((flash_read8(addr) << 16) |
(addr[(2 * info->portwidth)]) | (flash_read8(addr + info->portwidth) << 24) |
(addr[(3 * info->portwidth)] << 8); (flash_read8(addr + 2 * info->portwidth)) |
(flash_read8(addr + 3 * info->portwidth) << 8));
#else #else
retval = (addr[(2 * info->portwidth) - 1] << 24) | retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
(addr[(info->portwidth) - 1] << 16) | (flash_read8(addr + info->portwidth - 1) << 16) |
(addr[(4 * info->portwidth) - 1] << 8) | (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
addr[(3 * info->portwidth) - 1]; (flash_read8(addr + 3 * info->portwidth - 1)));
#endif #endif
flash_unmap(info, sect, offset, addr);
return retval; return retval;
} }
@ -390,7 +419,7 @@ static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
void *addr; void *addr;
cfiword_t cword; cfiword_t cword;
addr = flash_make_addr (info, sect, offset); addr = flash_map (info, sect, offset);
flash_make_cmd (info, cmd, &cword); flash_make_cmd (info, cmd, &cword);
switch (info->portwidth) { switch (info->portwidth) {
case FLASH_CFI_8BIT: case FLASH_CFI_8BIT:
@ -428,6 +457,8 @@ static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
/* Ensure all the instructions are fully finished */ /* Ensure all the instructions are fully finished */
sync(); sync();
flash_unmap(info, sect, offset, addr);
} }
static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect) static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
@ -445,7 +476,7 @@ static int flash_isequal (flash_info_t * info, flash_sect_t sect,
cfiword_t cword; cfiword_t cword;
int retval; int retval;
addr = flash_make_addr (info, sect, offset); addr = flash_map (info, sect, offset);
flash_make_cmd (info, cmd, &cword); flash_make_cmd (info, cmd, &cword);
debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr); debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
@ -479,6 +510,8 @@ static int flash_isequal (flash_info_t * info, flash_sect_t sect,
retval = 0; retval = 0;
break; break;
} }
flash_unmap(info, sect, offset, addr);
return retval; return retval;
} }
@ -491,7 +524,7 @@ static int flash_isset (flash_info_t * info, flash_sect_t sect,
cfiword_t cword; cfiword_t cword;
int retval; int retval;
addr = flash_make_addr (info, sect, offset); addr = flash_map (info, sect, offset);
flash_make_cmd (info, cmd, &cword); flash_make_cmd (info, cmd, &cword);
switch (info->portwidth) { switch (info->portwidth) {
case FLASH_CFI_8BIT: case FLASH_CFI_8BIT:
@ -510,6 +543,8 @@ static int flash_isset (flash_info_t * info, flash_sect_t sect,
retval = 0; retval = 0;
break; break;
} }
flash_unmap(info, sect, offset, addr);
return retval; return retval;
} }
@ -522,7 +557,7 @@ static int flash_toggle (flash_info_t * info, flash_sect_t sect,
cfiword_t cword; cfiword_t cword;
int retval; int retval;
addr = flash_make_addr (info, sect, offset); addr = flash_map (info, sect, offset);
flash_make_cmd (info, cmd, &cword); flash_make_cmd (info, cmd, &cword);
switch (info->portwidth) { switch (info->portwidth) {
case FLASH_CFI_8BIT: case FLASH_CFI_8BIT:
@ -545,6 +580,8 @@ static int flash_toggle (flash_info_t * info, flash_sect_t sect,
retval = 0; retval = 0;
break; break;
} }
flash_unmap(info, sect, offset, addr);
return retval; return retval;
} }
@ -714,12 +751,10 @@ static flash_sect_t find_sector (flash_info_t * info, ulong addr)
static int flash_write_cfiword (flash_info_t * info, ulong dest, static int flash_write_cfiword (flash_info_t * info, ulong dest,
cfiword_t cword) cfiword_t cword)
{ {
void *ctladdr;
void *dstaddr; void *dstaddr;
int flag; int flag;
ctladdr = flash_make_addr (info, 0, 0); dstaddr = map_physmem(dest, info->portwidth, MAP_NOCACHE);
dstaddr = (uchar *)dest;
/* Check if Flash is (sufficiently) erased */ /* Check if Flash is (sufficiently) erased */
switch (info->portwidth) { switch (info->portwidth) {
@ -736,10 +771,13 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll); flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
break; break;
default: default:
flag = 0;
break;
}
if (!flag) {
unmap_physmem(dstaddr, info->portwidth);
return 2; return 2;
} }
if (!flag)
return 2;
/* Disable interrupts which might cause a timeout here */ /* Disable interrupts which might cause a timeout here */
flag = disable_interrupts (); flag = disable_interrupts ();
@ -779,6 +817,8 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
if (flag) if (flag)
enable_interrupts (); enable_interrupts ();
unmap_physmem(dstaddr, info->portwidth);
return flash_full_status_check (info, find_sector (info, dest), return flash_full_status_check (info, find_sector (info, dest),
info->write_tout, "write"); info->write_tout, "write");
} }
@ -792,7 +832,7 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
int cnt; int cnt;
int retcode; int retcode;
void *src = cp; void *src = cp;
void *dst = (void *)dest; void *dst = map_physmem(dest, len, MAP_NOCACHE);
sector = find_sector (info, dest); sector = find_sector (info, dest);
@ -821,8 +861,8 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
cnt = len >> 3; cnt = len >> 3;
break; break;
default: default:
return ERR_INVAL; retcode = ERR_INVAL;
break; goto out_unmap;
} }
flash_write_cmd (info, sector, 0, (uchar) cnt - 1); flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
while (cnt-- > 0) { while (cnt-- > 0) {
@ -844,8 +884,8 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
src += 8, dst += 8; src += 8, dst += 8;
break; break;
default: default:
return ERR_INVAL; retcode = ERR_INVAL;
break; goto out_unmap;
} }
} }
flash_write_cmd (info, sector, 0, flash_write_cmd (info, sector, 0,
@ -854,7 +894,8 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
info, sector, info->buffer_write_tout, info, sector, info->buffer_write_tout,
"buffer write"); "buffer write");
} }
return retcode;
break;
case CFI_CMDSET_AMD_STANDARD: case CFI_CMDSET_AMD_STANDARD:
case CFI_CMDSET_AMD_EXTENDED: case CFI_CMDSET_AMD_EXTENDED:
@ -895,19 +936,25 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
} }
break; break;
default: default:
return ERR_INVAL; retcode = ERR_INVAL;
goto out_unmap;
} }
flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM); flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
retcode = flash_full_status_check (info, sector, retcode = flash_full_status_check (info, sector,
info->buffer_write_tout, info->buffer_write_tout,
"buffer write"); "buffer write");
return retcode; break;
default: default:
debug ("Unknown Command Set\n"); debug ("Unknown Command Set\n");
return ERR_INVAL; retcode = ERR_INVAL;
break;
} }
out_unmap:
unmap_physmem(dst, len);
return retcode;
} }
#endif /* CFG_FLASH_USE_BUFFER_WRITE */ #endif /* CFG_FLASH_USE_BUFFER_WRITE */
@ -1063,10 +1110,7 @@ void flash_print_info (flash_info_t * info)
/* /*
* Check if whole sector is erased * Check if whole sector is erased
*/ */
if (i != (info->sector_count - 1)) size = flash_sector_size(info, i);
size = info->start[i + 1] - info->start[i];
else
size = info->start[0] + info->size - info->start[i];
erased = 1; erased = 1;
flash = (volatile unsigned long *) info->start[i]; flash = (volatile unsigned long *) info->start[i];
size = size >> 2; /* divide by 4 for longword access */ size = size >> 2; /* divide by 4 for longword access */
@ -1101,7 +1145,7 @@ void flash_print_info (flash_info_t * info)
int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
{ {
ulong wp; ulong wp;
ulong cp; uchar *p;
int aln; int aln;
cfiword_t cword; cfiword_t cword;
int i, rc; int i, rc;
@ -1109,27 +1153,29 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
#ifdef CFG_FLASH_USE_BUFFER_WRITE #ifdef CFG_FLASH_USE_BUFFER_WRITE
int buffered_size; int buffered_size;
#endif #endif
/* get lower aligned address */
/* get lower aligned address */ /* get lower aligned address */
wp = (addr & ~(info->portwidth - 1)); wp = (addr & ~(info->portwidth - 1));
/* handle unaligned start */ /* handle unaligned start */
if ((aln = addr - wp) != 0) { if ((aln = addr - wp) != 0) {
cword.l = 0; cword.l = 0;
cp = wp; p = map_physmem(wp, info->portwidth, MAP_NOCACHE);
for (i = 0; i < aln; ++i, ++cp) for (i = 0; i < aln; ++i)
flash_add_byte (info, &cword, (*(uchar *) cp)); flash_add_byte (info, &cword, flash_read8(p + i));
for (; (i < info->portwidth) && (cnt > 0); i++) { for (; (i < info->portwidth) && (cnt > 0); i++) {
flash_add_byte (info, &cword, *src++); flash_add_byte (info, &cword, *src++);
cnt--; cnt--;
cp++;
} }
for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp) for (; (cnt == 0) && (i < info->portwidth); ++i)
flash_add_byte (info, &cword, (*(uchar *) cp)); flash_add_byte (info, &cword, flash_read8(p + i));
if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
rc = flash_write_cfiword (info, wp, cword);
unmap_physmem(p, info->portwidth);
if (rc != 0)
return rc; return rc;
wp = cp;
wp += i;
} }
/* handle the aligned part */ /* handle the aligned part */
@ -1180,13 +1226,14 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
* handle unaligned tail bytes * handle unaligned tail bytes
*/ */
cword.l = 0; cword.l = 0;
for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) { p = map_physmem(wp, info->portwidth, MAP_NOCACHE);
for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
flash_add_byte (info, &cword, *src++); flash_add_byte (info, &cword, *src++);
--cnt; --cnt;
} }
for (; i < info->portwidth; ++i, ++cp) { for (; i < info->portwidth; ++i)
flash_add_byte (info, &cword, (*(uchar *) cp)); flash_add_byte (info, &cword, flash_read8(p + i));
} unmap_physmem(p, info->portwidth);
return flash_write_cfiword (info, wp, cword); return flash_write_cfiword (info, wp, cword);
} }
@ -1238,10 +1285,11 @@ void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
uchar *dst; uchar *dst;
dst = buffer; 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); flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
memcpy (dst, src + offset, len); memcpy (dst, src + offset, len);
flash_write_cmd (info, 0, 0, info->cmd_reset); flash_write_cmd (info, 0, 0, info->cmd_reset);
flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
} }
/* /*
@ -1252,10 +1300,11 @@ void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
{ {
uchar *src; 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); flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
memcpy (buffer, src + offset, len); memcpy (buffer, src + offset, len);
flash_write_cmd (info, 0, 0, info->cmd_reset); flash_write_cmd (info, 0, 0, info->cmd_reset);
flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
} }
#endif /* CFG_FLASH_PROTECTION */ #endif /* CFG_FLASH_PROTECTION */