Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1879223
  • 博文数量: 376
  • 博客积分: 2147
  • 博客等级: 大尉
  • 技术积分: 3642
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-06 10:47
文章分类

全部博文(376)

文章存档

2019年(3)

2017年(28)

2016年(15)

2015年(17)

2014年(182)

2013年(16)

2012年(115)

我的朋友

分类: 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);
}
}





if (!qry_present(map,base,cfi))
return 0;


if (!cfi->numchips) {
/* This is the first time we're called. Set up the CFI 
  stuff accordingly and return */
return cfi_chip_setup(map, cfi);
}


/* Check each previous chip to see if it's an alias */
for (i=0; inumchips; i++) {
/* This chip should be in read mode if it's one
  we've already touched. */
if (qry_present(map,chips[i].start,cfi)) {
/* Eep. This chip also had the QRY marker. 
* Is it an alias for the new one? */
cfi_send_gen_cmd(0xF0, 0, chips[i].start, map, cfi, cfi->device_type, NULL);


/* If the QRY marker goes away, it's an alias */
if (!qry_present(map, chips[i].start, cfi)) {
printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
      map->name, base, chips[i].start);
return 0;
}
/* Yes, it's actually got QRY for data. Most 
* unfortunate. Stick the new chip in read mode
* too and if it's the same, assume it's an alias. */
/* FIXME: Use other modes to do a proper check */
cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);

if (qry_present(map, base, cfi)) {
printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
      map->name, base, chips[i].start);
return 0;
}
}
}

/* OK, if we got to here, then none of the previous chips appear to
  be aliases for the current one. */
if (cfi->numchips == MAX_CFI_CHIPS) {
printk(KERN_WARNING"%s: Too many flash chips detected. Increase MAX_CFI_CHIPS from %d.\n", map->name, MAX_CFI_CHIPS);
/* Doesn't matter about resetting it to Read Mode - we're not going to talk to it anyway */
return -1;
}
chips[cfi->numchips].start = base;
chips[cfi->numchips].state = FL_READY;
cfi->numchips++;

/* Put it back into Read Mode */
cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);


printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit mode\n",
      map->name, cfi->interleave, cfi->device_type*8, base,
      map->buswidth*8);

return 1;
}
阅读(1271) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~