一 代码分析
1、Board->dave->flash.c
#include
#include
#include "../common/flash.c"
#包含必须的头文件
static ulong flash_get_size (vu_long * addr, flash_info_t * info);
static void flash_get_offsets (ulong base, flash_info_t * info);
#声明两个静态函数
FLASH初始化
unsigned long flash_init (void)
{
#ifdef __DEBUG_START_FROM_SRAM__
return CFG_DUMMY_FLASH_SIZE;
#如果定义了__DEBUG_START_FROM_SRAM__也就是从ram启动,则返回CFG_DUMMY_FLASH_SIZE
#else
unsigned long size_b0;
int i;
/* Init: no FLASHes known */
for (i=0; i
flash_info[i].flash_id = FLASH_UNKNOWN;
}
#初始标识Flash的bank为FLASH_UNKNOWN
/* Static FLASH Bank configuration here - FIXME XXX */
size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
#调用flash_get_size函数,获取flash的大小,存入size_b0
if (flash_info[0].flash_id == FLASH_UNKNOWN) {
printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB/n",
size_b0, size_b0<<20);
}
#判断Flash是否是列表中所有的,如果没有,打印信息。具体列表在flash_get_size函数中
/* Setup offsets */
flash_get_offsets (0, &flash_info[0]);
#设置起始偏移地址。“0”表示基地址,“&flash_info[0]”bank0的基地址
/* Monitor protection ON by default */
(void)flash_protect(FLAG_PROTECT_SET,
-CFG_MONITOR_LEN,
0xffffffff,
&flash_info[0]);
#flash保护,见flash_protect函数
flash_info[0].size = size_b0;
#将size_b0存入flash_info[0]
return (size_b0);
#endif
}
2、Board->dave->command->flash.c
#include
#include
#包含必要的头文件
flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
#定义数组flash_info为flash_info_t类型,该类型的定义见3
static int write_word (flash_info_t *info, ulong dest, ulong data);
#声明静态函数
获取FLASH偏移地址
static void flash_get_offsets (ulong base, flash_info_t *info)
{
int i;
short n;
/* set up sector start address table */
#设置起始地址表。判断Flash类型,厂商和片名,如果都不符合,则执行else的内容
if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
for (i = 0; i < info->sector_count; i++)
info->start[i] = base + (i * 0x00010000);
} else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
/* set sector offsets for bottom boot block type */
for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
info->start[i] = base;
base += 8 << 10;
}
while (i < info->sector_count) { /* 64k regular sectors */
info->start[i] = base;
base += 64 << 10;
++i;
}
} else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
/* set sector offsets for top boot block type */
base += info->size;
i = info->sector_count;
for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
base -= 8 << 10;
--i;
info->start[i] = base;
}
while (i > 0) { /* 64k regular sectors */
base -= 64 << 10;
--i;
info->start[i] = base;
}
} else {
if (info->flash_id & FLASH_BTYPE) {
/* set sector offsets for bottom boot block type */
info->start[0] = base + 0x00000000;
info->start[1] = base + 0x00004000;
info->start[2] = base + 0x00006000;
info->start[3] = base + 0x00008000;
for (i = 4; i < info->sector_count; i++) {
info->start[i] = base + (i * 0x00010000) - 0x00030000;
}
} else {
/* set sector offsets for top boot block type */
#如果都不是以上的类型,设置物理片段的起始地址
i = info->sector_count - 1;
info->start[i--] = base + info->size - 0x00004000;
info->start[i--] = base + info->size - 0x00006000;
info->start[i--] = base + info->size - 0x00008000;
for (; i >= 0; i--) {
info->start[i] = base + i * 0x00010000;
}
}
}
}
FLASH信息打印输出
void flash_print_info (flash_info_t *info)
{
int i;
int k;
int size;
int erased;
volatile unsigned long *flash;
if (info->flash_id == FLASH_UNKNOWN) {
printf ("missing or unknown FLASH type/n");
return;
}
switch (info->flash_id & FLASH_VENDMASK) {
case FLASH_MAN_AMD: printf ("AMD "); break;
case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
case FLASH_MAN_SST: printf ("SST "); break;
case FLASH_MAN_STM: printf ("ST "); break;
default: printf ("Unknown Vendor "); break;
}
switch (info->flash_id & FLASH_TYPEMASK) {
case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)/n");
break;
case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)/n");
break;
case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)/n");
break;
case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)/n");
break;
case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)/n");
break;
case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)/n");
break;
case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)/n");
break;
case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)/n");
break;
case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)/n");
break;
case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)/n");
break;
case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)/n");
break;
case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)/n");
break;
case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)/n");
break;
case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)/n");
break;
case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)/n");
break;