分类: LINUX
2012-05-25 11:44:27
1、上一篇通过结构体
static struct chip_probe cfi_chip_probe = {
name: "CFI",
probe_chip: cfi_probe_chip
};
了解到,主要的查询工作要靠probe_chip: cfi_probe_chip函数完成。
其源码如下:
static int cfi_probe_chip(struct map_info *map, __u32 base,
struct flchip *chips, struct cfi_private *cfi)
这是在上一篇中,调用次函数的情景:
cp->probe_chip(map, 0, NULL, cfi),对比应该能得出对应的参数了。
{
int i;
if ((base + 0) >= map->size) {
printk(KERN_NOTICE
"Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
(unsigned long)base, map->size -1);
return 0;
}
if ((base + 0xff) >= map->size) {
printk(KERN_NOTICE
"Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
(unsigned long)base + 0x55, map->size -1);
return 0;
}
cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
向闪存芯片发送两条命令,如下表所示
第一条命令是0xf0是芯片进入随机读取状态。
第二条命令是0x98是芯片开始一个CFI查询流程。
函数cfi_send_gen_cmd函数源码如下:
/*
* Sends a CFI command to a bank of flash for the given geometry.
*
* Returns the offset in flash where the command was written.
* If prev_val is non-null, it will be set to the value at the command address,
* before the command was written.
*/
static inline __u32 cfi_send_gen_cmd(u_char cmd, __u32 cmd_addr, __u32 base,
struct map_info *map, struct cfi_private *cfi,
int type, cfi_word *prev_val)
{
cfi_word val;
__u32 addr = base + cfi_build_cmd_addr(cmd_addr, CFIDEV_INTERLEAVE, type);地址换算
val = cfi_build_cmd(cmd, map, cfi);命令调整
这两个函数根据具体的芯片格局对地址和命令作出换算和调整。
这是cfi_build_cmd_addr函数源码:
/*
* Returns the command address according to the given geometry.
*/
static inline __u32 cfi_build_cmd_addr(__u32 cmd_ofs, int interleave, int type)
{
return (cmd_ofs * type) * interleave;
}
这是cfi_build_cmd函数源码:
在有多个芯片并列时,对芯片的命令要写入到并列的每一个芯片中,所以需要根据具体的情况将命令重复几次。CFI规定16位和32位数据均采用“小端”格式,而有些CPU采用“大端”格式,所以要通过函数cpu_to_cfi32转化。如果CPU本就是“小端”,则本函数为空。
/*
* Transforms the CFI command for the given geometry (bus width & interleave.
*/
static inline cfi_word cfi_build_cmd(u_char cmd, struct map_info *map, struct cfi_private *cfi)
{
cfi_word val = 0;
if (cfi_buswidth_is_1()) {
/* 1 x8 device */
val = cmd;
} else if (cfi_buswidth_is_2()) {
if (cfi_interleave_is_1()) {
/* 1 x16 device in x16 mode */
val = cpu_to_cfi16(cmd);
} else if (cfi_interleave_is_2()) {
/* 2 (x8, x16 or x32) devices in x8 mode */
val = cpu_to_cfi16((cmd << 8) | cmd);
}
} else if (cfi_buswidth_is_4()) {
if (cfi_interleave_is_1()) {
/* 1 x32 device in x32 mode */
val = cpu_to_cfi32(cmd);
} else if (cfi_interleave_is_2()) {
/* 2 x16 device in x16 mode */
val = cpu_to_cfi32((cmd << 16) | cmd);
} else if (cfi_interleave_is_4()) {
/* 4 (x8, x16 or x32) devices in x8 mode */
val = (cmd << 16) | cmd;
val = cpu_to_cfi32((val << 8) | val);
}
#ifdef CFI_WORD_64
} else if (cfi_buswidth_is_8()) {
if (cfi_interleave_is_1()) {
/* 1 x64 device in x64 mode */
val = cpu_to_cfi64(cmd);
} else if (cfi_interleave_is_2()) {
/* 2 x32 device in x32 mode */
val = cmd;
val = cpu_to_cfi64((val << 32) | val);
} else if (cfi_interleave_is_4()) {
/* 4 (x16, x32 or x64) devices in x16 mode */
val = (cmd << 16) | cmd;
val = cpu_to_cfi64((val << 32) | val);
} else if (cfi_interleave_is_8()) {
/* 8 (x8, x16 or x32) devices in x8 mode */
val = (cmd << 8) | cmd;
val = (val << 16) | val;
val = (val << 32) | val;
val = cpu_to_cfi64(val);
}
#endif /* CFI_WORD_64 */
}
return val;
}
if (prev_val)
*prev_val = cfi_read(map, addr);
cfi_write(map, val, addr);
return addr - base;
}
注:CFI查询中所用的地址都是以芯片本身的存储单元为单位的,而CPU使用的32位地址则是字节地址,所以要根据芯片的宽度加以换算。当采用多个芯片并列时实际上相当于改变了芯片宽度,因此需要进一步加以换算。
/*
* Read a value according to the bus width.
*/
static inline cfi_word cfi_read(struct map_info *map, __u32 addr)
{
if (cfi_buswidth_is_1()) {
return map->read8(map, addr);
} else if (cfi_buswidth_is_2()) {
return map->read16(map, addr);
} else if (cfi_buswidth_is_4()) {
return map->read32(map, addr);
} else if (cfi_buswidth_is_8()) {
return map->read64(map, addr);
} else {
return 0;
}
}
此函数从目标地址中读出数据,具体的调用那个函数取决于数据宽度和map_info结构中的函数指针。
cfi_write也类似。
/*
* Write a value according to the bus width.
*/
static inline void cfi_write(struct map_info *map, cfi_word val, __u32 addr)
{
if (cfi_buswidth_is_1()) {
map->write8(map, val, addr);
} else if (cfi_buswidth_is_2()) {
map->write16(map, val, addr);
} else if (cfi_buswidth_is_4()) {
map->write32(map, val, addr);
} else if (cfi_buswidth_is_8()) {
map->write64(map, val, addr);
}
}