MTD原始设备与FLASH硬件驱动的对话

<div class="postTitle">
<a id="viewpost1_TitleUrl" class="postTitle2" href="http://www.cnitblog.com/luofuchong/archive/2007/08/31/32682.html">MTD原始设备与FLASH硬件驱动的对话</a>
</div>
<p>
看了<<Linux MTD源代码分析>>后对以MTD的分层结构以及各层的分工情况有了大致的了解,然而各层之间是如何进行对话的呢,对于这个问题,<<Linux MTD源代码分析>>上没有详细的去说明。<br><br>
小弟抽空研究了一下,打算从下到上,在从上到下,分两条主线来研究一下MTD原始设备与FLASH硬件驱动的对话(MTD原始设备与更上层的对话留待以后再研究)。<br><br>
以下是第一部分,从下到上的介绍FLASH硬件驱动与MTD原始设备是如何建立联系的。<br><br>
1、首先从入口函数开始:<br>
static int <span style="color: red;">s3c24xx_nand_probe</span>
(struct device *dev, int is_s3c2440)<br>
{<br>
struct platform_device *pdev = to_platform_device(dev);<br>
struct s3c2410_platform_nand *plat = to_nand_plat(dev);<br><span style="color: #0010ff;">//获取nand flash配置用结构体数据(dev.c中定义,详细见附录部分)</span>
<br>
struct s3c2410_nand_info *info;<br>
struct s3c2410_nand_mtd *nmtd;<br>
struct s3c2410_nand_set *sets;<br>
struct resource *res;<br>
int err = 0;<br>
int size;<br>
int nr_sets;<br>
int setno;<br><br>
pr_debug("s3c2410_nand_probe(%p)/n", dev);<br><br>
info = kmalloc(sizeof(*info), GFP_KERNEL);<br>
if (info == NULL) {<br>
printk(KERN_ERR PFX "no memory for flash info/n");<br>
err = -ENOMEM;<br>
goto exit_error;<br>
}<br><br>
memzero(info, sizeof(*info));<br>
dev_set_drvdata(dev, info); //以后有用<br><br>
spin_lock_init(&info->controller.lock); //初始化自旋锁<br>
init_waitqueue_head(&info->controller.wq); //初始化等待队列<br><br>
/* get the clock source and enable it */<br><br>
info->clk = clk_get(dev, "nand");<br>
if (IS_ERR(info->clk)) {<br>
printk(KERN_ERR PFX "failed to get clock");<br>
err = -ENOENT;<br>
goto exit_error;<br>
}<br><br>
clk_use(info->clk);<br>
clk_enable(info->clk);<br><br>
/* allocate and map the resource */<br><br>
/* currently we assume we have the one resource */<br>
res = pdev->resource; <span style="color: #0010ff;">//提取dev.c中定义的与设备相关的资源</span>
<br>
size = res->end - res->start + 1;<br><br>
info->area = request_mem_region(res->start, size, pdev->name);<br><br>
if (info->area == NULL) {<br>
printk(KERN_ERR PFX "cannot reserve register region/n");<br>
err = -ENOENT;<br>
goto exit_error;<br>
}<br><br>
info->device = dev;<br>
info->platform = plat; <span style="color: #0010ff;">//保存好struct s3c2410_platform_nand结构数据</span>
<br>
info->regs = ioremap(res->start, size);<span style="color: #0010ff;">//映射nand flash用到的寄存器</span>
<br>
info->is_s3c2440 = is_s3c2440; <br><br>
if (info->regs == NULL) {<br>
printk(KERN_ERR PFX "cannot reserve register region/n");<br>
err = -EIO;<br>
goto exit_error;<br>
} <br><br>
printk(KERN_INFO PFX "mapped registers at %p/n", info->regs);<br><br>
/* initialise the hardware */<br><br>
err = s3c2410_nand_inithw(info, dev);<br><span style="color: #0010ff;"> //初始化s3c2410 nand flash控制,主要是配置S3C2410_NFCONF寄存器</span>
<br>
if (err != 0)<br>
goto exit_error;<br><br>
sets = (plat != NULL) ? plat->sets : NULL; <br>
nr_sets = (plat != NULL) ? plat->nr_sets : 1;<br><br>
info->mtd_count = nr_sets;<br><span style="color: #0010ff;"> //我的板上只有一块nand flash,配置信息见plat-sets,数目为1。</span>
<br><br>
/* allocate our information */<br><br>
size = nr_sets * sizeof(*info->mtds);<br>
info->mtds = kmalloc(size, GFP_KERNEL);<br>
if (info->mtds == NULL) {<br>
printk(KERN_ERR PFX "failed to allocate mtd storage/n");<br>
err = -ENOMEM;<br>
goto exit_error;<br>
}<br><br>
memzero(info->mtds, size);<br><br>
/* initialise all possible chips */<br><br>
nmtd = info->mtds;<br><br>
for (setno = 0; setno < nr_sets; setno++, nmtd++) {<br>
pr_debug("initialising set %d (%p, info %p)/n",<br>
setno, nmtd, info);<br><br>
s3c2410_nand_init_chip(info, nmtd, sets);<br><br>
nmtd->scan_res = nand_scan(&nmtd->mtd,<br>
(sets) ? sets->nr_chips : 1);<span style="color: red;">//为什么使用set->nr_chips(还没配置的东西)?</span>
<br><br>
if (nmtd->scan_res == 0) {<br>
s3c2410_nand_add_partition(info, nmtd, sets);<br>
}<br><br>
if (sets != NULL)<br>
sets++;<br>
}<br><br>
pr_debug("initialised ok/n");<br>
return 0;<br><br>
exit_error:<br>
s3c2410_nand_remove(dev);<br><br>
if (err == 0)<br>
err = -EINVAL;<br>
return err;<br>
}<br><br style="color: #0010ff;"><span style="color: #0010ff;">//初始化代表一片flash的struct nand_chip结构</span>
<br><br>
static void <span style="color: red;">s3c2410_nand_init_chip</span>
(struct s3c2410_nand_info *info,<br>
struct s3c2410_nand_mtd *nmtd,<br>
struct s3c2410_nand_set *set)<br>
{<br>
struct nand_chip *chip = &nmtd->chip;<br><br>
chip->IO_ADDR_R = info->regs + S3C2410_NFDATA; <span style="color: #0010ff;">//读地址</span>
<br>
chip->IO_ADDR_W = info->regs + S3C2410_NFDATA; <span style="color: #0010ff;">//写地址</span>
<br>
chip->hwcontrol = s3c2410_nand_hwcontrol; <br>
chip->dev_ready = s3c2410_nand_devready; <span style="color: #0010ff;">//ready状态查询</span>
<br>
chip->write_buf = s3c2410_nand_write_buf; <span style="color: #0010ff;">//写函数</span>
<br>
chip->read_buf = s3c2410_nand_read_buf; <span style="color: #0010ff;">//读函数</span>
<br>
chip->select_chip = s3c2410_nand_select_chip; <span style="color: #0010ff;">//片选函数</span>
<br>
chip->chip_delay = 50;<br>
chip->priv = nmtd;<br>
chip->options = 0;<br>
chip->controller = &info->controller;<br><br>
if (info->is_s3c2440) {<br>
chip->IO_ADDR_R = info->regs + S3C2440_NFDATA;<br>
chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;<br>
chip->hwcontrol = s3c2440_nand_hwcontrol;<br>
}<br><br>
nmtd->info = info;<br>
nmtd->mtd.priv = chip; <br><span style="color: #0010ff;">//nand_scan函数中会调用struct nand_chip *this = mtd->priv取出该struct nand_chip结构</span>
<br>
nmtd->set = set;<br><br>
if (hardware_ecc) {<br>
chip->correct_data = s3c2410_nand_correct_data;<br>
chip->enable_hwecc = s3c2410_nand_enable_hwecc;<br>
chip->calculate_ecc = s3c2410_nand_calculate_ecc;<br>
chip->eccmode = NAND_ECC_HW3_512;<br>
chip->autooob = &nand_hw_eccoob;<br><br>
if (info->is_s3c2440) {<br>
chip->enable_hwecc = s3c2440_nand_enable_hwecc;<br>
chip->calculate_ecc = s3c2440_nand_calculate_ecc;<br>
}<br>
} else { <br>
chip->eccmode = NAND_ECC_SOFT; <span style="color: #0010ff;">//ECC的类型</span>
<br>
}<br>
}<br><br>
/* command and control functions <br>
*<br>
* Note, these all use tglx's method of changing the IO_ADDR_W field<br>
* to make the code simpler, and use the nand layer's code to issue the<br>
* command and address sequences via the proper IO ports.<br>
*<br>
*/<br><br>
static void <span style="color: red;">s3c2410_nand_hwcontrol</span>
(struct mtd_info *mtd, int cmd)<br>
{<br>
struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);<br>
struct nand_chip *chip = mtd->priv;<br><br>
switch (cmd) {<br>
case NAND_CTL_SETNCE:<br>
case NAND_CTL_CLRNCE:<br>
printk(KERN_ERR "%s: called for NCE/n", __FUNCTION__);<br>
break;<br><br>
case NAND_CTL_SETCLE:<br>
chip->IO_ADDR_W = info->regs + S3C2410_NFCMD;<span style="color: #2000ff;">//写命令</span>
<br>
break;<br><br>
case NAND_CTL_SETALE:<br>
chip->IO_ADDR_W = info->regs + S3C2410_NFADDR;<span style="color: #2000ff;">//写地址</span>
<br>
break;<br><br>
/* NAND_CTL_CLRCLE: */<br>
/* NAND_CTL_CLRALE: */<br>
default:<br>
chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;<span style="color: #2000ff;">//写数据</span>
<br>
break;<br>
}<br>
}<br><br>
/* s3c2410_nand_devready()<br>
*<br>
* returns 0 if the nand is busy, 1 if it is ready<br>
*/<br><br>
static int <span style="color: red;">s3c2410_nand_devready</span>
(struct mtd_info *mtd)<br>
{<br>
struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);<br><br>
if (info->is_s3c2440)<br>
return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;<br>
return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;<span style="color: #2000ff;">//返回nand flash都忙标志</span>
<br>
}<br><br>
static void <span style="color: red;">s3c2410_nand_write_buf</span>
(struct mtd_info *mtd,<br>
const u_char *buf, int len)<br>
{<br>
struct nand_chip *this = mtd->priv;<br>
writesb(this->IO_ADDR_W, buf, len);<span style="color: #2000ff;">//写操作</span>
<br>
}<br><br>
static void <span style="color: red;">s3c2410_nand_read_buf</span>
(struct mtd_info *mtd, u_char *buf, int len)<br>
{<br>
struct nand_chip *this = mtd->priv;<br>
readsb(this->IO_ADDR_R, buf, len);<span style="color: #2000ff;">//读操作</span>
<br>
}<br><br>
/* select chip */<br>
/* <br><span style="color: #2000ff;">* 根据chip都值设置nand flash都片选信号:</span>
<br style="color: #2000ff;"><span style="color: #2000ff;">* chip = -1 -- 禁用nand flash</span>
<br style="color: #2000ff;"><span style="color: #2000ff;">* chip !=-1 -- 选择对应的nand flash</span>
<br>
*/<br>
static void <span style="color: red;">s3c2410_nand_select_chip</span>
(struct mtd_info *mtd, int chip)<br>
{<br>
struct s3c2410_nand_info *info;<br>
struct s3c2410_nand_mtd *nmtd; <br>
struct nand_chip *this = mtd->priv;<br>
void __iomem *reg;<br>
unsigned long cur;<br>
unsigned long bit;<br><br>
nmtd = this->priv;<br>
info = nmtd->info;<br><br>
bit = (info->is_s3c2440) ? S3C2440_NFCONT_nFCE : S3C2410_NFCONF_nFCE;<br>
reg = info->regs+((info->is_s3c2440) ? S3C2440_NFCONT:S3C2410_NFCONF);<br><br>
cur = readl(reg);<br><br>
if (chip == -1) {<br>
cur |= bit;<br>
} else {<br>
if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {<br>
printk(KERN_ERR PFX "chip %d out of range/n", chip);<br>
return;<br>
}<br><br>
if (info->platform != NULL) {<br>
if (info->platform->select_chip != NULL)<br>
(info->platform->select_chip)(nmtd->set, chip);<br>
}<br><br>
cur &= ~bit;<br>
}<br><br>
writel(cur, reg);<br>
}<br><br><br style="color: #0010ff;"><span style="color: #0010ff;">注:</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> s3c2410_nand_init_chip填充struct nand_chip的一部分成员,nand_scan以通用nand flash的标准进行检测,并填充struct nand_chip的其它成员,必要时根据检测结果进行取舍。</span>
<br><br>
int <span style="color: red;">nand_scan </span>
(struct mtd_info *mtd, int maxchips)<br>
{<br>
int i, nand_maf_id, nand_dev_id, busw, maf_id;<br>
struct nand_chip *this = mtd->priv; <span style="color: #0010ff;"> //取出struct nand_chip结构</span>
<br><br>
/* Get buswidth to select the correct functions*/<br>
busw = this->options & NAND_BUSWIDTH_16; <span style="color: #0010ff;">//nand flash的位宽</span>
<br><br>
/* check for proper chip_delay setup, set 20us if not */<br>
if (!this->chip_delay) <br>
this->chip_delay = 20;<br><br>
/* check, if a user supplied command function given */<br>
if (this->cmdfunc == NULL) <span style="color: #0010ff;">//填充命令函数</span>
<br>
this->cmdfunc = nand_command;<br><br>
/* check, if a user supplied wait function given */<br>
if (this->waitfunc == NULL) <span style="color: #0010ff;">//填充等待函数</span>
<br>
this->waitfunc = nand_wait;<br><br>
if (!this->select_chip) <span style="color: #0010ff;">//s3c2410_nand_init_chip中已定义</span>
<br>
this->select_chip = nand_select_chip;<br>
if (!this->write_byte) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->write_byte = busw ? nand_write_byte16 : nand_write_byte;<br>
if (!this->read_byte) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->read_byte = busw ? nand_read_byte16 : nand_read_byte;<br>
if (!this->write_word) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->write_word = nand_write_word;<br>
if (!this->read_word) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->read_word = nand_read_word;<br>
if (!this->block_bad) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->block_bad = nand_block_bad;<br>
if (!this->block_markbad) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->block_markbad = nand_default_block_markbad;<br>
if (!this->write_buf) <span style="color: #0010ff;">//s3c2410_nand_init_chip中已定义</span>
<br>
this->write_buf = busw ? nand_write_buf16 : nand_write_buf;<br>
if (!this->read_buf) <span style="color: #0010ff;">//s3c2410_nand_init_chip中已定义</span>
<br>
this->read_buf = busw ? nand_read_buf16 : nand_read_buf;<br>
if (!this->verify_buf) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;<br>
if (!this->scan_bbt) <span style="color: #0010ff;">//使用默认的</span>
<br>
this->scan_bbt = nand_default_bbt;<br><br>
/* Select the device */<br>
this->select_chip(mtd, 0); //片选,可惜在s3c2410 nand flash控制器中此操作为空<br><br>
/* Send the command for reading device ID */<br>
this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);<span style="color: #0010ff;">//发送读ID命令</span>
<br><br>
/* Read manufacturer and device IDs */<br>
nand_maf_id = this->read_byte(mtd); <span style="color: #0010ff;">//读取生产商ID</span>
<br>
nand_dev_id = this->read_byte(mtd); <span style="color: #0010ff;">//读取设备ID</span>
<br><br>
/* Print and store flash device information */<br>
for (i = 0; nand_flash_ids[i].name != NULL; i++) { <br><span style="color: #0010ff;">//保存着nand flash资料的nand_flash_ids表在include/linux/mtd/nand_ids.c文件中,详细见附录</span>
<br><br>
if (nand_dev_id != nand_flash_ids[i].id) <span style="color: #0010ff;">//比较设备ID </span>
<br>
continue;<br><br>
if (!mtd->name) mtd->name = nand_flash_ids[i].name; <span style="color: #0010ff;">//填充设备名</span>
<br>
this->chipsize = nand_flash_ids[i].chipsize << 20; <span style="color: #0010ff;">//填充设备大小</span>
<br><br>
/* New devices have all the information in additional id bytes */<br>
if (!nand_flash_ids[i].pagesize) {<br>
int extid;<br>
/* The 3rd id byte contains non relevant data ATM */<br>
extid = this->read_byte(mtd);<br>
/* The 4th id byte is the important one */<br>
extid = this->read_byte(mtd);<br>
/* Calc pagesize */<br>
mtd->oobblock = 1024 << (extid & 0x3);<br>
extid >>= 2;<br>
/* Calc oobsize */<br>
mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);<br>
extid >>= 2;<br>
/* Calc blocksize. Blocksize is multiples of 64KiB */<br>
mtd->erasesize = (64 * 1024) << (extid & 0x03);<br>
extid >>= 2;<br>
/* Get buswidth information */<br>
busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;<br><br>
} else {<br>
/* Old devices have this data hardcoded in the<br>
* device id table */<br>
mtd->erasesize = nand_flash_ids[i].erasesize; <span style="color: #0010ff;">//填充檫除单元大小</span>
<span style="color: #2000ff;">(16k)</span>
<br>
mtd->oobblock = nand_flash_ids[i].pagesize; <span style="color: #0010ff;">//填充页大小(</span>
<span style="color: #0010ff;">512</span>
<span style="color: #0010ff;">)</span>
<br>
mtd->oobsize = mtd->oobblock / 32; <span style="color: #0010ff;">//oob大小(512/32=16)</span>
<br>
busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;<span style="color: #0010ff;">//获取nand flash表中定义的位宽</span>
<br>
}<br><br>
/* Try to identify manufacturer */ <span style="color: #0010ff;">//比较生产商ID</span>
<br>
for (maf_id = 0; nand_manuf_ids[maf_id].id != 0x0; maf_id++) {<br>
if (nand_manuf_ids[maf_id].id == nand_maf_id)<br>
break;<br>
}<br><br>
/* Check, if buswidth is correct. Hardware drivers should set<br>
* this correct ! */<br><span style="color: #0010ff;">/用户定义的位宽与芯片实际的位宽不一致,取消nand flash的片选</span>
<br>
if (busw != (this->options & NAND_BUSWIDTH_16)) { <br>
printk (KERN_INFO "NAND device: Manufacturer ID:"<br>
" 0x%02x, Chip ID: 0x%02x (%s %s)/n", nand_maf_id, nand_dev_id, <br>
nand_manuf_ids[maf_id].name , mtd->name);<br>
printk (KERN_WARNING <br>
"NAND bus width %d instead %d bit/n", <br>
(this->options & NAND_BUSWIDTH_16) ? 16 : 8,<br>
busw ? 16 : 8);<br>
this->select_chip(mtd, -1);<span style="color: #0010ff;">//在s3c2410 nand flash控制器驱动中,此操作为空操作</span>
<br>
return 1; <br>
}<br><br>
/* Calculate the address shift from the page size */ <br><span style="color: #0010ff;">//计算页、可檫除单元、nand flash大小的偏移值 </span>
<br>
this->page_shift = ffs(mtd->oobblock) - 1;<br>
this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;<br>
this->chip_shift = ffs(this->chipsize) - 1;<br><br>
/* Set the bad block position */<br><span style="color: #0010ff;">//标注此nand flash为大页还是小页?</span>
<br>
this->badblockpos = mtd->oobblock > 512 ? <br>
NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;<br><br>
/* Get chip options, preserve non chip based options */<br><span style="color: #0010ff;">//用户没指定的选项从nand flash表中获取补上</span>
<br>
this->options &= ~NAND_CHIPOPTIONS_MSK;<br>
this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;<br>
/* Set this as a default. Board drivers can override it, if neccecary */<br>
this->options |= NAND_NO_AUTOINCR;<br>
/* Check if this is a not a samsung device. Do not clear the options<br>
* for chips which are not having an extended id.<br>
*/ <br>
if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)<br>
this->options &= ~NAND_SAMSUNG_LP_OPTIONS;<br><br>
/* Check for AND chips with 4 page planes */<br>
if (this->options & NAND_4PAGE_ARRAY)<br>
this->erase_cmd = multi_erase_cmd;<br>
else<br>
this->erase_cmd = single_erase_cmd; <br><br>
/* Do not replace user supplied command function ! */<br>
if (mtd->oobblock > 512 && this->cmdfunc == nand_command)<br>
this->cmdfunc = nand_command_lp;<br><br>
printk (KERN_INFO "NAND device: Manufacturer ID:"<br>
" 0x%02x, Chip ID: 0x%02x (%s %s)/n", nand_maf_id, nand_dev_id, <br>
nand_manuf_ids[maf_id].name , nand_flash_ids[i].name);<br>
break;<br>
}/<span style="color: #0010ff;">/好的,检测结束^_^</span>
<br><br>
if (!nand_flash_ids[i].name) { <br>
printk (KERN_WARNING "No NAND device found!!!/n");<br>
this->select_chip(mtd, -1);<br>
return 1;<br>
}<br><br><span style="color: #0010ff;">//统计一下同种类型的nand flash有多少块(我板上只有一块)</span>
<br>
for (i=1; i < maxchips; i++) {<br>
this->select_chip(mtd, i);<br><br>
/* Send the command for reading device ID */<br>
this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);<br><br>
/* Read manufacturer and device IDs */<br>
if (nand_maf_id != this->read_byte(mtd) ||<br>
nand_dev_id != this->read_byte(mtd))<br>
break;<br>
}<br>
if (i > 1)<br>
printk(KERN_INFO "%d NAND chips detected/n", i);<br><br>
/* Allocate buffers, if neccecary */<br>
if (!this->oob_buf) {<br>
size_t len;<br><span style="color: #0010ff;">//求出一个檫除单元64K中oob所占用的总空间</span>
<br>
len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);<br>
this->oob_buf = kmalloc (len, GFP_KERNEL);<br>
if (!this->oob_buf) {<br>
printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf/n");<br>
return -ENOMEM;<br>
}<br>
this->options |= NAND_OOBBUF_ALLOC;<span style="color: #0010ff;">//oob空间已分配,置相应的标志位</span>
<br>
}<br><br>
if (!this->data_buf) {<br>
size_t len;<br>
len = mtd->oobblock + mtd->oobsize;//512+16=128<br>
this->data_buf = kmalloc (len, GFP_KERNEL);<br>
if (!this->data_buf) {<br>
if (this->options & NAND_OOBBUF_ALLOC)<br>
kfree (this->oob_buf);<br>
printk (KERN_ERR "nand_scan(): Cannot allocate data_buf/n");<br>
return -ENOMEM;<br>
}<br style="color: #0010ff;"><span style="color: #0010ff;"> <span style="color: #020000;">this->options |= NAND_DATABUF_ALLOC;</span>
//数据空间已分配,置相应的标志位</span>
<br>
}<br><br>
/* Store the number of chips and calc total size for mtd */<br>
this->numchips = i;<span style="color: #0010ff;">//记录nand flash片数</span>
<br>
mtd->size = i * this->chipsize;<span style="color: #0010ff;">//计算出nand flash总大小</span>
<br>
/* Convert chipsize to number of pages per chip -1. */<br>
this->pagemask = (this->chipsize >> this->page_shift) - 1;//(64M>>9)-1=128k-1=0x1ffff<br><br>
/* Preset the internal oob buffer */<br><span style="color: #0010ff;">//oob_buf全部置为0xff</span>
<br>
memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));<br><br>
/* If no default placement scheme is given, select an<br>
* appropriate one */<br>
if (!this->autooob) { <span style="color: #0010ff;">//我们选用的是NAND_ECC_SOFT,autooob未设置</span>
<br>
/* Select the appropriate default oob placement scheme for<br>
* placement agnostic filesystems */<br>
switch (mtd->oobsize) { <br>
case 8:<br>
this->autooob = &nand_oob_8;<br>
break;<br>
case 16:<br>
this->autooob = &nand_oob_16;<span style="color: #0010ff;">//我们的nand flash属于这一类</span>
<br>
break;<br>
case 64:<br>
this->autooob = &nand_oob_64;<br>
break;<br>
default:<br>
printk (KERN_WARNING "No oob scheme defined for oobsize %d/n",<br>
mtd->oobsize);<br>
BUG();<br>
}<br>
}<br><span style="color: #0010ff;">注:</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> ECC的东西不是很懂,先跳过^_^ </span>
<br><br><br>
/* The number of bytes available for the filesystem to place fs dependend<br>
* oob data */<br>
mtd->oobavail = 0;<br>
for (i = 0; this->autooob->oobfree[i][1]; i++)<br>
mtd->oobavail += this->autooob->oobfree[i][1];<br><br>
/* <br>
* check ECC mode, default to software<br>
* if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize<br>
* fallback to software ECC <br>
*/<br>
this->eccsize = 256; /* set default eccsize */ <br>
this->eccbytes = 3;<br><br>
switch (this->eccmode) {<br>
case NAND_ECC_HW12_2048:<br>
if (mtd->oobblock < 2048) {<br>
printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC/n",<br>
mtd->oobblock);<br>
this->eccmode = NAND_ECC_SOFT;<br>
this->calculate_ecc = nand_calculate_ecc;<br>
this->correct_data = nand_correct_data;<br>
} else<br>
this->eccsize = 2048;<br>
break;<br><br>
case NAND_ECC_HW3_512: <br>
case NAND_ECC_HW6_512: <br>
case NAND_ECC_HW8_512: <br>
if (mtd->oobblock == 256) {<br>
printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC /n");<br>
this->eccmode = NAND_ECC_SOFT;<br>
this->calculate_ecc = nand_calculate_ecc;<br>
this->correct_data = nand_correct_data;<br>
} else <br>
this->eccsize = 512; /* set eccsize to 512 */<br>
break;<br><br>
case NAND_ECC_HW3_256:<br>
break;<br><br>
case NAND_ECC_NONE: <br>
printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!/n");<br>
this->eccmode = NAND_ECC_NONE;<br>
break;<br><br>
case NAND_ECC_SOFT: <br>
this->calculate_ecc = nand_calculate_ecc;<br>
this->correct_data = nand_correct_data;<br>
break;<br><br>
default:<br>
printk (KERN_WARNING "Invalid NAND_ECC_MODE %d/n", this->eccmode);<br>
BUG(); <br>
} <br><br>
/* Check hardware ecc function availability and adjust number of ecc bytes per <br>
* calculation step<br>
*/<br>
switch (this->eccmode) {<br>
case NAND_ECC_HW12_2048:<br>
this->eccbytes += 4;<br>
case NAND_ECC_HW8_512: <br>
this->eccbytes += 2;<br>
case NAND_ECC_HW6_512: <br>
this->eccbytes += 3;<br>
case NAND_ECC_HW3_512: <br>
case NAND_ECC_HW3_256:<br>
if (this->calculate_ecc && this->correct_data && this->enable_hwecc)<br>
break;<br>
printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible/n");<br>
BUG(); <br>
}<br><br>
mtd->eccsize = this->eccsize;<br><br>
/* Set the number of read / write steps for one page to ensure ECC generation */<br>
switch (this->eccmode) {<br>
case NAND_ECC_HW12_2048:<br>
this->eccsteps = mtd->oobblock / 2048;<br>
break;<br>
case NAND_ECC_HW3_512:<br>
case NAND_ECC_HW6_512:<br>
case NAND_ECC_HW8_512:<br>
this->eccsteps = mtd->oobblock / 512;<br>
break;<br>
case NAND_ECC_HW3_256:<br>
case NAND_ECC_SOFT: <br>
this->eccsteps = mtd->oobblock / 256;<br>
break;<br><br>
case NAND_ECC_NONE: <br>
this->eccsteps = 1;<br>
break;<br>
}<br><br>
/* Initialize state, waitqueue and spinlock */<br>
this->state = FL_READY;<br>
init_waitqueue_head (&this->wq);<br>
spin_lock_init (&this->chip_lock);<br><br>
/* De-select the device */<br>
this->select_chip(mtd, -1);<br><br>
/* Invalidate the pagebuffer reference */<br>
this->pagebuf = -1;<br><br>
/* Fill in remaining MTD driver data */<br><span style="color: #0010ff;">//填充mtd结构的其它部分</span>
<br>
mtd->type = MTD_NANDFLASH;<br>
mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;<br>
mtd->ecctype = MTD_ECC_SW;<br>
mtd->erase = nand_erase;<br>
mtd->point = NULL;<br>
mtd->unpoint = NULL;<br>
mtd->read = nand_read;<br>
/* nand_read->nand_do_read_ecc->read_buf->s3c2410_nand_read_buf */<br>
mtd->write = nand_write;<br>
/* nand_write->nand_write_ecc->nand_write_page->write_buf->s3c2410_nand_write_buf */<br>
mtd->read_ecc = nand_read_ecc;<br>
mtd->write_ecc = nand_write_ecc;<br>
mtd->read_oob = nand_read_oob;<br>
mtd->write_oob = nand_write_oob;<br>
mtd->readv = NULL;<br>
mtd->writev = nand_writev;<br>
mtd->writev_ecc = nand_writev_ecc;<br>
mtd->sync = nand_sync;<br>
mtd->lock = NULL;<br>
mtd->unlock = NULL;<br>
mtd->suspend = NULL;<br>
mtd->resume = NULL;<br>
mtd->block_isbad = nand_block_isbad;<br>
mtd->block_markbad = nand_block_markbad;<br><br>
/* and make the autooob the default one */<br>
memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));<br><br>
mtd->owner = THIS_MODULE;<br><br>
/* Check, if we should skip the bad block table scan */<br>
if (this->options & NAND_SKIP_BBTSCAN)<br>
return 0;<br><br>
/* Build bad block table */<br>
return this->scan_bbt (mtd);<br>
}<br><br>
/**<br>
* nand_command - [DEFAULT] Send command to NAND device<br>
* @mtd: MTD device structure<br>
* @command: the command to be sent<br>
* @column: the column address for this command, -1 if none<br>
* @page_addr: the page address for this command, -1 if none<br>
*<br>
* Send command to NAND device. This function is used for small page<br>
* devices (256/512 Bytes per page)<br>
*/<br>
static void <span style="color: red;">nand_command</span>
(struct mtd_info *mtd, unsigned command, int column, int page_addr)<br>
{<br>
register struct nand_chip *this = mtd->priv;<br><br>
/* Begin command latch cycle */<br>
this->hwcontrol(mtd, NAND_CTL_SETCLE);<span style="color: #2000ff;"> //选择写入S3C2410_NFCMD寄存器</span>
<br>
/*<br>
* Write out the command to the device.<br>
*/<br>
if (command == NAND_CMD_SEQIN) {<br>
int readcmd;<br><br>
if (column >= mtd->oobblock) {<span style="color: #2000ff;"> //读/写位置超出512,读oob_data</span>
<br>
/* OOB area */<br>
column -= mtd->oobblock;<br>
readcmd = NAND_CMD_READOOB;<br>
} else if (column < 256) { <span style="color: #2000ff;">//读/写位置在前512,使用read0命令</span>
<br>
/* First 256 bytes --> READ0 */<br>
readcmd = NAND_CMD_READ0;<br>
} else { <span style="color: #2000ff;">//读/写位置在后512,使用read1命令</span>
<br>
column -= 256;<br>
readcmd = NAND_CMD_READ1;<br>
}<br>
this->write_byte(mtd, readcmd); <span style="color: #2000ff;">//写入具体命令</span>
<br>
}<br>
this->write_byte(mtd, command);<br><br>
/* Set ALE and clear CLE to start address cycle */<br><span style="color: #2000ff;">/* 清楚CLE,锁存命令;置位ALE,开始传输地址 */</span>
<br>
this->hwcontrol(mtd, NAND_CTL_CLRCLE); <span style="color: #2000ff;">//锁存命令</span>
<br><br>
if (column != -1 || page_addr != -1) {<br>
this->hwcontrol(mtd, NAND_CTL_SETALE); <span style="color: #2000ff;">//选择写入S3C2410_NFADDR寄存器</span>
<br style="color: #2000ff;"><br>
/* Serially input address */<br>
if (column != -1) {<br>
/* Adjust columns for 16 bit buswidth */<br>
if (this->options & NAND_BUSWIDTH_16)<br>
column >>= 1;<br>
this->write_byte(mtd, column); <span style="color: #2000ff;">//写入列地址</span>
<br>
}<br>
if (page_addr != -1) { <span style="color: #2000ff;">//写入页地址(分三个字节写入)</span>
<br>
this->write_byte(mtd, (unsigned char) (page_addr & 0xff));<br>
this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));<br>
/* One more address cycle for devices > 32MiB */<br>
if (this->chipsize > (32 << 20))<br>
this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));<br>
}<br>
/* Latch in address */<br><span style="color: #2000ff;">/* 锁存地址 */</span>
<br>
this->hwcontrol(mtd, NAND_CTL_CLRALE);<br>
}<br><br>
/* <br>
* program and erase have their own busy handlers <br>
* status and sequential in needs no delay<br>
*/<br>
switch (command) {<br><br>
case NAND_CMD_PAGEPROG:<br>
case NAND_CMD_ERASE1:<br>
case NAND_CMD_ERASE2:<br>
case NAND_CMD_SEQIN:<br>
case NAND_CMD_STATUS:<br>
return;<br><br>
case NAND_CMD_RESET: <span style="color: #0010ff;">//复位操作</span>
<br><span style="color: #0010ff;"> // 等待nand flash become ready</span>
<br>
if (this->dev_ready) <span style="color: #0010ff;">//判断nand flash 是否busy(1:ready 0:busy)</span>
<br>
break;<br>
udelay(this->chip_delay);<br>
this->hwcontrol(mtd, NAND_CTL_SETCLE);<br>
this->write_byte(mtd, NAND_CMD_STATUS);<br>
this->hwcontrol(mtd, NAND_CTL_CLRCLE);<br>
while ( !(this->read_byte(mtd) & NAND_STATUS_READY));<br>
return;<br><br>
/* This applies to read commands */ <br>
default:<br>
/* <br>
* If we don't have access to the busy pin, we apply the given<br>
* command delay<br>
*/<br>
if (!this->dev_ready) {<br>
udelay (this->chip_delay);<span style="color: #0010ff;">//稍作延迟</span>
<br>
return;<br>
} <br>
}<br>
/* Apply this short delay always to ensure that we do wait tWB in<br>
* any case on any machine. */<br>
ndelay (100);<br><br>
nand_wait_ready(mtd);<br>
}<br><br><br>
/* <br>
* Wait for the ready pin, after a command<br>
* The timeout is catched later.<br>
*/<br>
static void <span style="color: red;">nand_wait_ready</span>
(struct mtd_info *mtd)<br>
{<br>
struct nand_chip *this = mtd->priv;<br>
unsigned long timeo = jiffies + 2;<br><br>
/* wait until command is processed or timeout occures */<br>
do {<br>
if (this->dev_ready(mtd)) <span style="color: #0010ff;">//简单调用this->dev_ready(s3c2410_nand_devready)函数 等待nand flash become ready</span>
<br>
return;<br>
touch_softlockup_watchdog();<br>
} while (time_before(jiffies, timeo)); <br>
}<br><br>
/**<br>
* nand_wait - [DEFAULT] wait until the command is done<br>
* @mtd: MTD device structure<br>
* @this: NAND chip structure<br>
* @state: state to select the max. timeout value<br>
*<br>
* Wait for command done. This applies to erase and program only<br>
* Erase can take up to 400ms and program up to 20ms according to <br>
* general NAND and SmartMedia specs<br>
*<br>
*/<br><span style="color: #0010ff;">/* 等待知道命令传输完成,适用于檫除和写入命令 */</span>
<br>
static int <span style="color: red;">nand_wait</span>
(struct mtd_info *mtd, struct nand_chip *this, int state)<br>
{<br><br>
unsigned long timeo = jiffies;<br>
int status;<br><br>
if (state == FL_ERASING)<br>
timeo += (HZ * 400) / 1000;<span style="color: #0010ff;">//檫除操作的话,时间相对要长一些</span>
<br>
else<br>
timeo += (HZ * 20) / 1000;<br><br>
/* Apply this short delay always to ensure that we do wait tWB in<br>
* any case on any machine. */<br>
ndelay (100);<br><br>
if ((state == FL_ERASING) && (this->options & NAND_IS_AND))<br>
this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);<br>
else <br>
this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);<br><br>
while (time_before(jiffies, timeo)) { <br>
/* Check, if we were interrupted */<br>
if (this->state != state)<br>
return 0;<br><span style="color: #0010ff;"> /* 等待nand flash become ready */</span>
<br>
if (this->dev_ready) {<br>
if (this->dev_ready(mtd))<br>
break; <br>
} else {<br>
if (this->read_byte(mtd) & NAND_STATUS_READY)<br>
break;<br>
}<br>
cond_resched();<br>
}<br>
status = (int) this->read_byte(mtd);<br>
return status;<br>
}<br><br>
/**<br>
* nand_block_bad - [DEFAULT] Read bad block marker from the chip<br><span style="color: #0010ff;">* 检查nand flash中某一页是否为坏块</span>
<br>
* @mtd: MTD device structure<br>
* @ofs: offset from device start<br>
* @getchip: 0, if the chip is already selected<br>
*<br>
* Check, if the block is bad. <br>
*/<br>
static int <span style="color: red;">nand_block_bad</span>
(struct mtd_info *mtd, loff_t ofs, int getchip)<br>
{<br>
int page, chipnr, res = 0;<br>
struct nand_chip *this = mtd->priv;<br>
u16 bad;<br><br>
if (getchip) {<br>
page = (int)(ofs >> this->page_shift);<br>
chipnr = (int)(ofs >> this->chip_shift);<br><br>
/* Grab the lock and see if the device is available */<br>
nand_get_device (this, mtd, FL_READING);<br><br>
/* Select the NAND device */<br>
this->select_chip(mtd, chipnr);<br>
} else <br>
page = (int) ofs; <br><br>
if (this->options & NAND_BUSWIDTH_16) {<br>
this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);<br>
bad = cpu_to_le16(this->read_word(mtd));<br>
if (this->badblockpos & 0x1)<br>
bad >>= 1;<br>
if ((bad & 0xFF) != 0xff)<br>
res = 1;<br>
} else {<br>
this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);<br><span style="color: #0010ff;">/* 发送读oob_data命令(oob_data的badblockpos (第6)位记录着坏块标志) */</span>
<br>
if (this->read_byte(mtd) != 0xff)<span style="color: #0010ff;">//坏块</span>
<br>
res = 1;<br>
}<br><br>
if (getchip) {<br>
/* Deselect and wake up anyone waiting on the device */<br>
nand_release_device(mtd);<br>
} <br><br>
return res;<br>
}<br><br>
/**<br>
* nand_default_block_markbad - [DEFAULT] mark a block bad<br><span style="color: #0010ff;">* 标志坏块</span>
<br>
* @mtd: MTD device structure<br>
* @ofs: offset from device start<br>
*<br>
* This is the default implementation, which can be overridden by<br>
* a hardware specific driver.<br>
*/<br>
static int <span style="color: red;">nand_default_block_markbad</span>
(struct mtd_info *mtd, loff_t ofs)<br>
{<br>
struct nand_chip *this = mtd->priv;<br>
u_char buf[2] = {0, 0};<br>
size_t retlen;<br>
int block;<br><br>
/* Get block number */<br>
block = ((int) ofs) >> this->bbt_erase_shift;<br>
if (this->bbt)<br>
this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);<br><span style="color: #0010ff;"> /* </span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> 这个暂时不是很好说:内核维护一个标志bad block表,使用2bit来表示1block。</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> 这个表在开机的时候通过扫描nand flash每个block的头两页的oob数据来生成,</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> 发现坏块后至相应的block标志位为非零(有时候至3,但有时候至1,还没搞明白有什么不同)</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> */</span>
<br><br>
/* Do we have a flash based bad block table ? */<br>
if (this->options & NAND_USE_FLASH_BBT)<span style="color: #0010ff;">//samsun nand flash不属于这种,暂时不去研究,以后同</span>
<br>
return nand_update_bbt (mtd, ofs);<br><br>
/* We write two bytes, so we dont have to mess with 16 bit access */<br>
ofs += mtd->oobsize + (this->badblockpos & ~0x01);//???????????????<br>
return nand_write_oob (mtd, ofs , 2, &retlen, buf);<br>
}<br><br>
/**<br>
* nand_verify_buf - [DEFAULT] Verify chip data against buffer<br>
* <span style="color: #0010ff;">检验nand flash与buffer的数据是否一致 </span>
<br>
* @mtd: MTD device structure<br>
* @buf: buffer containing the data to compare<br>
* @len: number of bytes to compare<br>
*<br>
* Default verify function for 8bit buswith<br>
*/<br>
static int <span style="color: red;">nand_verify_buf</span>
(struct mtd_info *mtd, const u_char *buf, int len)<br>
{<br>
int i;<br>
struct nand_chip *this = mtd->priv;<br><br>
for (i=0; i<len; i++)<br>
if (buf[i] != readb(this->IO_ADDR_R))<br>
return -EFAULT;<br><br>
return 0;<br>
}<br><br>
/**<br>
* nand_default_bbt - [NAND Interface] Select a default bad block table for the device <br>
* @mtd: MTD device structure<br>
*<br>
* This function selects the default bad block table<br>
* support for the device and calls the nand_scan_bbt function<br>
*<br>
*/<br>
int <span style="color: red;">nand_default_bbt</span>
(struct mtd_info *mtd)<br>
{<br>
struct nand_chip *this = mtd->priv;<br><br>
/* Default for AG-AND. We must use a flash based <br>
* bad block table as the devices have factory marked<br>
* _good_ blocks. Erasing those blocks leads to loss<br>
* of the good / bad information, so we _must_ store<br>
* this information in a good / bad table during <br>
* startup<br>
*/<br>
if (this->options & NAND_IS_AND) {<br>
/* Use the default pattern descriptors */<br>
if (!this->bbt_td) { <br>
this->bbt_td = &bbt_main_descr;<br>
this->bbt_md = &bbt_mirror_descr;<br>
} <br>
this->options |= NAND_USE_FLASH_BBT;<br>
return nand_scan_bbt (mtd, &agand_flashbased);<br>
}<br><br><br>
/* Is a flash based bad block table requested ? */<br>
if (this->options & NAND_USE_FLASH_BBT) {<br>
/* Use the default pattern descriptors */ <br>
if (!this->bbt_td) { <br>
this->bbt_td = &bbt_main_descr;<br>
this->bbt_md = &bbt_mirror_descr;<br>
}<br>
if (!this->badblock_pattern) {<br>
this->badblock_pattern = (mtd->oobblock > 512) ?<br>
&largepage_flashbased : &smallpage_flashbased;<br>
}<br>
} else { <span style="color: #2000ff;">//samsun nand flash的坏块表不存在与nand flash里面,需要扫描来生成。</span>
<br>
this->bbt_td = NULL;<br>
this->bbt_md = NULL;<br>
if (!this->badblock_pattern) {<br>
this->badblock_pattern = (mtd->oobblock > 512) ?<br>
&largepage_memorybased : &smallpage_memorybased;<br>
}<br>
}<br>
return nand_scan_bbt (mtd, this->badblock_pattern);<br>
}<br><br>
/**<br>
* nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)<br>
* @mtd: MTD device structure<br>
* @bd: descriptor for the good/bad block search pattern<br>
*<br>
* The function checks, if a bad block table(s) is/are already <br>
* available. If not it scans the device for manufacturer<br>
* marked good / bad blocks and writes the bad block table(s) to<br>
* the selected place.<br>
*<br>
* The bad block table memory is allocated here. It must be freed<br>
* by calling the nand_free_bbt function.<br>
*<br>
*/<br>
int <span style="color: #ff0000;">nand_scan_bbt</span>
(struct mtd_info *mtd, struct nand_bbt_descr *bd)<br>
{<br>
struct nand_chip *this = mtd->priv;<br>
int len, res = 0;<br>
uint8_t *buf;<br>
struct nand_bbt_descr *td = this->bbt_td;<br>
struct nand_bbt_descr *md = this->bbt_md;<br><br>
len = mtd->size >> (this->bbt_erase_shift + 2);<br>
/* Allocate memory (2bit per block) */<br><span style="color: #0010ff;">/* 2bit per block=(2/8)byte per block,所以上面要多右移2位 */</span>
<br>
this->bbt = kmalloc (len, GFP_KERNEL);<br>
if (!this->bbt) {<br>
printk (KERN_ERR "nand_scan_bbt: Out of memory/n");<br>
return -ENOMEM;<br>
}<br>
/* Clear the memory bad block table */<br>
memset (this->bbt, 0x00, len);<br><br>
/* If no primary table decriptor is given, scan the device<br>
* to build a memory based bad block table<br>
*/<br>
if (!td) {<br>
if ((res = nand_memory_bbt(mtd, bd))) {<br>
printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT/n");<br>
kfree (this->bbt);<br>
this->bbt = NULL;<br>
}<br>
return res;<br>
}<br><br>
/* Allocate a temporary buffer for one eraseblock incl. oob */<br><span style="color: #0010ff;">/* 分配1 block所需要的oob data空间 */</span>
<br>
len = (1 << this->bbt_erase_shift);<br>
len += (len >> this->page_shift) * mtd->oobsize;<br>
buf = kmalloc (len, GFP_KERNEL);<br>
if (!buf) {<br>
printk (KERN_ERR "nand_bbt: Out of memory/n");<br>
kfree (this->bbt);<br>
this->bbt = NULL;<br>
return -ENOMEM;<br>
}<br><br><span style="color: #0010ff;">//由于td、md均为NULL,一下函数基本不起作用,先不去研究它</span>
<br>
/* Is the bbt at a given page ? */<br>
if (td->options & NAND_BBT_ABSPAGE) {<br>
res = read_abs_bbts (mtd, buf, td, md);<br>
} else { <br>
/* Search the bad block table using a pattern in oob */<br>
res = search_read_bbts (mtd, buf, td, md);<br>
} <br><br>
if (res) <br>
res = check_create (mtd, buf, bd);<br><br>
/* Prevent the bbt regions from erasing / writing */<br>
mark_bbt_region (mtd, td);<br>
if (md)<br>
mark_bbt_region (mtd, md);<br><br>
kfree (buf);<br>
return res;<br>
}<br><br>
/**<br>
* nand_memory_bbt - [GENERIC] create a memory based bad block table<br>
* @mtd: MTD device structure<br>
* @bd: descriptor for the good/bad block search pattern<br>
*<br>
* The function creates a memory based bbt by scanning the device <br>
* for manufacturer / software marked good / bad blocks<br>
*/<br>
static inline int <span style="color: #ff0000;">nand_memory_bbt</span>
(struct mtd_info *mtd, struct nand_bbt_descr *bd)<br>
{<br>
struct nand_chip *this = mtd->priv;<br><br>
bd->options &= ~NAND_BBT_SCANEMPTY;<br><span style="color: #0010ff;">//我们只需要扫描oob data,不需要扫描全部(512+16bytes的数据)</span>
<br>
return create_bbt (mtd, this->data_buf, bd, -1);<br>
}<br><br>
/**<br>
* create_bbt - [GENERIC] Create a bad block table by scanning the device<br>
* @mtd: MTD device structure<br>
* @buf: temporary buffer<br>
* @bd: descriptor for the good/bad block search pattern<br>
* @chip: create the table for a specific chip, -1 read all chips.<br>
* Applies only if NAND_BBT_PERCHIP option is set<br>
*<br>
* Create a bad block table by scanning the device<br>
* for the given good/bad block identify pattern<br>
*/<br>
static int <span style="color: #ff0000;">create_bbt</span>
(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)<br>
{<br>
struct nand_chip *this = mtd->priv;<br>
int i, j, numblocks, len, scanlen;<br>
int startblock;<br>
loff_t from;<br>
size_t readlen, ooblen;<br><br>
printk (KERN_INFO "Scanning device for bad blocks/n");<br><br>
if (bd->options & NAND_BBT_SCANALLPAGES)<span style="color: #0010ff;">//扫描所有都页</span>
<br>
len = 1 << (this->bbt_erase_shift - this->page_shift);<span style="color: #0010ff;">//求出每block所含的page数</span>
<br>
else {<br>
if (bd->options & NAND_BBT_SCAN2NDPAGE)<span style="color: #0010ff;">//只检查2 page</span>
<br>
len = 2;<br>
else <br>
len = 1;<span style="color: #0010ff;">//只检查1 page</span>
<br>
}<br><br>
if (!(bd->options & NAND_BBT_SCANEMPTY)) {<br>
/* We need only read few bytes from the OOB area */<br><span style="color: #0010ff;">/* 我们只需要检查OOB的某些数据 */</span>
<br>
scanlen = ooblen = 0;<br>
readlen = bd->len;<br>
} else {<br>
/* Full page content should be read */<br><span style="color: #0010ff;">/* 读取整页内容 */</span>
<br>
scanlen = mtd->oobblock + mtd->oobsize;<br>
readlen = len * mtd->oobblock;<br>
ooblen = len * mtd->oobsize;<br>
}<br><br>
if (chip == -1) {<br>
/* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it<br>
* makes shifting and masking less painful */<br><span style="color: #0010ff;">/* 计算出nand flash所包含都block数目(注意这里总数目经过林乘2操作)*/</span>
<br>
numblocks = mtd->size >> (this->bbt_erase_shift - 1);<br>
startblock = 0;<br>
from = 0;<br>
} else {<br>
if (chip >= this->numchips) {<br>
printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)/n",<br>
chip + 1, this->numchips);<br>
return -EINVAL;<br>
}<br>
numblocks = this->chipsize >> (this->bbt_erase_shift - 1);<br>
startblock = chip * numblocks;<br>
numblocks += startblock;<br>
from = startblock << (this->bbt_erase_shift - 1);<br>
}<br><br>
for (i = startblock; i < numblocks;) {<br>
int ret;<br><br>
if (bd->options & NAND_BBT_SCANEMPTY) <span style="color: #0010ff;">//整页数据读取</span>
<br>
if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen)))<br>
return ret;<br><br>
for (j = 0; j < len; j++) {<br>
if (!(bd->options & NAND_BBT_SCANEMPTY)) {<br>
size_t retlen;<br><br>
/* Read the full oob until read_oob is fixed to <br>
* handle single byte reads for 16 bit buswidth */<br><span style="color: #0010ff;">/* 读取当前页的oob区的所有数据 */</span>
<br>
ret = mtd->read_oob(mtd, from + j * mtd->oobblock,<br>
mtd->oobsize, &retlen, buf);<br>
if (ret)<br>
return ret;<br><span style="color: #0010ff;">/* 检查oob data的bad block标志位,判断是否是坏块 */</span>
<br>
if (check_short_pattern (buf, bd)) {<br>
this->bbt[i >> 3] |= 0x03 << (i & 0x6);<br><span style="color: #0010ff;">/* 注意:这里i=实际值*2。由于一个block的状态用2bit来表示,那么一个字节可以存放4个block的状态。</span>
<br><span style="color: #0010ff;">这里i>>3刚好是实际block/4,4个block的状态刚好存放在this->bbt所指向的一个字节里面 </span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> */</span>
<br>
printk (KERN_WARNING "Bad eraseblock %d at 0x%08x/n", <br>
i >> 1, (unsigned int) from);<br>
break;<br>
}<br>
} else {<br>
if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {<br>
this->bbt[i >> 3] |= 0x03 << (i & 0x6);<br>
printk (KERN_WARNING "Bad eraseblock %d at 0x%08x/n", <br>
i >> 1, (unsigned int) from);<br>
break;<br>
}<br>
}<br>
}<br>
i += 2;<span style="color: #0010ff;">//更新block的序号</span>
<br>
from += (1 << this->bbt_erase_shift);<span style="color: #0010ff;">//更新nand flash的地址</span>
<br>
}<br>
return 0;<br>
}<br><br>
/**<br>
* nand_release - [NAND Interface] Free resources held by the NAND device <br>
* @mtd: MTD device structure<br>
*/<br>
void <span style="color: red;">nand_release</span>
(struct mtd_info *mtd)<br>
{<br>
struct nand_chip *this = mtd->priv;<br><br>
#ifdef CONFIG_MTD_PARTITIONS<br>
/* Deregister partitions */<br>
del_mtd_partitions (mtd);<br>
#endif<br>
/* Deregister the device */<br>
del_mtd_device (mtd);<br><br>
/* Free bad block table memory, if allocated */<br>
if (this->bbt)<br>
kfree (this->bbt);<br>
/* Buffer allocated by nand_scan ? */<br>
if (this->options & NAND_OOBBUF_ALLOC)<br>
kfree (this->oob_buf);<br>
/* Buffer allocated by nand_scan ? */<br>
if (this->options & NAND_DATABUF_ALLOC)<br>
kfree (this->data_buf);<br>
}<br><br>
附录:<br>
/arch/arm/mach-s3c2410/dev.c文件:<br><br>
static struct mtd_partition partition_info[]={<br>
[0]={<br>
name :"vivi",<br>
size :0x20000,<br>
offset :0,<br>
},[1]={<br>
name :"param",<br>
size :0x10000,<br>
offset :0x20000,<br>
},[2]={<br>
name :"kernel",<br>
size :0x1d0000,<br>
offset :0x30000,<br>
},[3]={<br>
name :"root",<br>
size :0x3c00000,<br>
offset :0x200000,<br>
}<br>
};<br><br>
struct s3c2410_nand_set nandset={<br>
nr_partitions :4,<br>
partitions :partition_info,<br>
};<br><br>
struct s3c2410_platform_nand superlpplatform={<br>
tacls :0,<br>
twrph0 :30,<br>
twrph1 :0,<br>
sets :&nandset,<br>
nr_sets :1,<br>
};<br><br>
struct platform_device s3c_device_nand = {<br>
.name = "s3c2410-nand",<br>
.id = -1,<br>
.num_resources = ARRAY_SIZE(s3c_nand_resource),<br>
.resource = s3c_nand_resource,<br>
.dev={<br>
.platform_data=&superlpplatform<br>
}<br>
};<br><br>
nand_flash_ids表<br>
/driver/mtd/nand/nand_ids.c文件:<br>
struct nand_flash_dev nand_flash_ids[] = {<br>
................................................................................<br>
{"NAND 64MiB 3,3V 8-bit", 0x76, 512, 64, 0x4000, 0},<br>
................................................................................<br>
};<br><span style="color: #0010ff;">注:</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> 这里只列出常用的samsun 64M Nand Flash的资料,对应的信息请看该结构体的定义:</span>
<br>
struct nand_flash_dev {<br>
char *name;<br>
int id;<br>
unsigned long pagesize;<br>
unsigned long chipsize;<br>
unsigned long erasesize;<br>
unsigned long options;<br>
};<br><span style="color: #0010ff;">可知该nand flash 设备ID号为0x76,页大小为512,大小为64(M),檫除单元大小为16(K)。</span>
</p>
<p></p>
<div class="postTitle">
<a id="viewpost1_TitleUrl" class="postTitle2" href="http://www.cnitblog.com/luofuchong/archive/2007/09/04/32939.html">MTD原始设备与FLASH硬件驱动的对话-续</a>
</div>
<p>
上一个贴由下到上的介绍了FLASH硬件驱动是如何与MTD原始设备建立联系的,现在再由上到下的研究一下是如何通过MTD原始设备来访问FLASH硬件驱动的。<br><br><span style="color: #ff0030;">首先分析一下如何通过MTD原始设备进而通过FLASH硬件驱动来读取FLASH存储器的数据。</span>
<br style="color: #ff0030;"><br>
引用自<<Linux系统移植>>一文:<br><br>
"读Nand Flash:<br>
当对nand flash的设备文件(nand flash在/dev下对应的文件)执行系统调用read(),或在某个文件系统中对该<br>
设备进行读操作时. 会调用struct mtd_info中的read方法,他们缺省调用函数为nand_read(),在<br>
drivers/mtd/nand/nand_base.c中定义.nand_read()调用nand_do_read_ecc(),执行读操作. 在<br>
nand_do_read_ecc()函数中,主要完成如下几项工作:<br>
1. 会调用在nand flash驱动中对struct nand_chip重载的select_chip方法,即<br>
s3c2410_nand_select_chip()选择要操作的MTD芯片.<br>
2. 会调用在struct nand_chip中系统缺省的方法cmdfunc发送读命令到nand flash.<br>
3. 会调用在nand flash驱动中对struct nand_chip重载的read_buf(),即s3c2410_nand_read_buf()<br>
从Nand Flash的控制器的数据寄存器中读出数据.<br>
4. 如果有必要的话,会调用在nand flash驱动中对struct nand_chip重载的<br>
enable_hwecc,correct_data以及calculate_ecc方法,进行数据ECC校验。"<br><br>
下面研究一下其中的细节:<br>
/**<br>
* nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc<br>
* @mtd: MTD device structure<br>
* @from: offset to read from<br>
* @len: number of bytes to read<br>
* @retlen: pointer to variable to store the number of read bytes<br>
* @buf: the databuffer to put data<br>
*<br>
* This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL<br>
* and flags = 0xff<br>
*/<br>
static int <span style="color: red;">nand_read</span>
(struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)<br>
{<br>
return nand_do_read_ecc (mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff);<br>
}<br style="color: #0010ff;"><span style="color: #0010ff;">注:</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> 以参数oob_buf为NULL,flags为0xff调用nand_do_read_ecc函数。<br><br></span>
/**<br>
* nand_do_read_ecc - [MTD Interface] Read data with ECC<br>
* @mtd: MTD device structure<br>
* @from: offset to read from<br>
* @len: number of bytes to read<br>
* @retlen: pointer to variable to store the number of read bytes<br>
* @buf: the databuffer to put data<br>
* @oob_buf: filesystem supplied oob data buffer (can be NULL)<br>
* @oobsel: oob selection structure<br>
* @flags: flag to indicate if nand_get_device/nand_release_device should be preformed<br>
* and how many corrected error bits are acceptable:<br>
* bits 0..7 - number of tolerable errors<br>
* bit 8 - 0 == do not get/release chip, 1 == get/release chip<br>
*<br>
* NAND read with ECC<br>
*/<br>
int <span style="color: red;">nand_do_read_ecc</span>
(struct mtd_info *mtd, loff_t from, size_t len,<br>
size_t * retlen, u_char * buf, u_char * oob_buf, <br>
struct nand_oobinfo *oobsel, int flags)<br>
{<br><br>
int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;<br>
int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;<br>
struct nand_chip *this = mtd->priv;<br>
u_char *data_poi, *oob_data = oob_buf;<span style="color: #0010ff;">//目前oob_data指针为空,以后会去修改它。</span>
<br>
u_char ecc_calc[32];<span style="color: #0010ff;">//该数组用于存放计算出来的ecc结果</span>
<br>
u_char ecc_code[32];<span style="color: #0010ff;">//该数组用于存放oob中ecc部分的数据</span>
<br>
int eccmode, eccsteps;<span style="color: #0010ff;">//eccmode存放ecc的类型(ECC_SOFT);</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> eccsteps用于记录一个page所需的ecc校验次数(2)。</span>
<br>
int *oob_config, datidx;<br>
int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;<br>
int eccbytes;<br>
int compareecc = 1;<span style="color: #0010ff;">//是否需要ecc标志(如果设置成ECC_NONE,这个标志将被清0)</span>
<br>
int oobreadlen;<br><br><br>
DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i/n", (unsigned int) from, (int) len);<br><br>
/* Do not allow reads past end of device */<br><span style="color: #0010ff;">/* 不允许超越设备容量的读操作 */</span>
<br>
if ((from + len) > mtd->size) {<br>
DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device/n");<br>
*retlen = 0;<br>
return -EINVAL;<br>
}<br><br>
/* Grab the lock and see if the device is available */<br><span style="color: #0010ff;">/* 获取自旋锁,等待设备可用并获取其控制权 */</span>
<br>
if (flags & NAND_GET_DEVICE)<br>
nand_get_device (this, mtd, FL_READING);<br><br>
/* Autoplace of oob data ? Use the default placement scheme */<br>
if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)<br>
oobsel = this->autooob;<br><span style="color: #0010ff;"> /* </span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 感觉这一步有点多余,因为nand_scan中已经调用了以下代码: </span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 把this->autooob的内容拷贝到mtd->oobinfo中了</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> */</span>
<br><br>
eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;<br>
oob_config = oobsel->eccpos;<span style="color: #0010ff;">//记录ecc在oob数据中的位置</span>
<br><br>
/* Select the NAND device */<br>
chipnr = (int)(from >> this->chip_shift);<br>
this->select_chip(mtd, chipnr);<span style="color: #0010ff;">//选择nand flash芯片(在s3c2410 nand flash控制器中为空操作)</span>
<br><br>
/* First we calculate the starting page */<br><span style="color: #0010ff;">/* 首先,我们计算出开始页码 */</span>
<br>
realpage = (int) (from >> this->page_shift);<br>
page = realpage & this->pagemask;<br><br>
/* Get raw starting column */<br><span style="color: #0010ff;"> /* 其次,我们计算页内偏址 */</span>
<br>
col = from & (mtd->oobblock - 1);<br><br>
end = mtd->oobblock;<span style="color: #0010ff;">//页大小(512)</span>
<br>
ecc = this->eccsize;<span style="color: #0010ff;">//ecc保护下的数据大小(256)</span>
<br>
eccbytes = this->eccbytes;<span style="color: #0010ff;">//ecc所占的字节数(3)</span>
<br><br>
if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))<br>
compareecc = 0;<span style="color: #0010ff;">//如果设置为关闭ECC或写操作才需要ECC,那把ecc给禁用(现在可是读操作^_^)</span>
<br><br>
oobreadlen = mtd->oobsize;//16<br>
if (this->options & NAND_HWECC_SYNDROME) <br>
oobreadlen -= oobsel->eccbytes;<br><br>
/* Loop until all data read */<br>
while (read < len) {<br><br>
int aligned = (!col && (len - read) >= end);<br>
/* <br>
* If the read is not page aligned, we have to read into data buffer<br>
* due to ecc, else we read into return buffer direct<br><span style="color: #0010ff;">* 如果要读的位置不是页对齐都话,那么只要先把整页读出来,</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 取出所需要读取的数据,然后修改读位置,那么以后的读操作都是页对齐的了。</span>
<br>
*/<br>
if (aligned)<br>
data_poi = &buf[read];<br>
else <br>
data_poi = this->data_buf;<br><br>
/* Check, if we have this page in the buffer <br>
*<br>
* FIXME: Make it work when we must provide oob data too,<br>
* check the usage of data_buf oob field<br><span style="color: #0010ff;">* 如果我们所需要的数据还存在于缓冲中都话:</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 1 如果读位置页对齐,我们只要把缓冲中的数据直接拷贝到data_poi(buf[read])中即可(因为数据存在与缓存中,所以也无需要考虑ecc问题)</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 2 如果读位置不是页对齐,什么读不要作,让其继续留在缓存(data_buf)中,以后会从data_poi(指向缓存data_buf)中提取所需要的数据。</span>
<br>
*/<br>
if (realpage == this->pagebuf && !oob_buf) {<br>
/* aligned read ? */<br>
if (aligned)<br>
memcpy (data_poi, this->data_buf, end);<br>
goto readdata;<br>
}<br><br>
/* Check, if we must send the read command */<br><span style="color: #0010ff;">/* 发送读命令,页地址为page,列地址为0x00 */</span>
<br>
if (sndcmd) {<br>
this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);<br>
sndcmd = 0;<br>
} <br><br>
/* get oob area, if we have no oob buffer from fs-driver */<br>
if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||<br>
oobsel->useecc == MTD_NANDECC_AUTOPL_USR)<br>
oob_data = &this->data_buf[end];<span style="color: #0010ff;">//以上情况,oob_data暂存在data_buf缓存中</span>
<br><br>
eccsteps = this->eccsteps;//2<br><br>
switch (eccmode) {<br>
case NAND_ECC_NONE: { /* No ECC, Read in a page */<br>
static unsigned long lastwhinge = 0;<br>
if ((lastwhinge / HZ) != (jiffies / HZ)) {<br>
printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended/n");<br>
lastwhinge = jiffies;<br>
}<br>
this->read_buf(mtd, data_poi, end);<br>
break;<br>
}<br><br>
case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */<br>
this->read_buf(mtd, data_poi, end);<span style="color: #0010ff;">//读取数据到data_poi</span>
<br>
for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc) <br>
this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);<br><span style="color: #0010ff;">/* 计算出读取到data_poi的数据的ecc值,并存放到ecc_calc数组中。</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 因为读都数据有一页大小(512),需要分别对其上半部和下半部分计算一次ecc值,并分开存放到ecc_calc数组相应都位置中。</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> */</span>
<br>
break; <br><br>
default:<br>
for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {<br>
this->enable_hwecc(mtd, NAND_ECC_READ);<br>
this->read_buf(mtd, &data_poi[datidx], ecc);<br><br>
/* HW ecc with syndrome calculation must read the<br>
* syndrome from flash immidiately after the data */<br>
if (!compareecc) {<br>
/* Some hw ecc generators need to know when the<br>
* syndrome is read from flash */<br>
this->enable_hwecc(mtd, NAND_ECC_READSYN);<br>
this->read_buf(mtd, &oob_data[i], eccbytes);<br>
/* We calc error correction directly, it checks the hw<br>
* generator for an error, reads back the syndrome and<br>
* does the error correction on the fly */<br>
ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);<br>
if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {<br>
DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " <br>
"Failed ECC read, page 0x%08x on chip %d/n", page, chipnr);<br>
ecc_failed++;<br>
}<br>
} else {<br>
this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);<br>
} <br>
}<br>
break; <br>
}<br><br>
/* read oobdata */<br>
this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);<br><span style="color: #0010ff;">//读取oob_data存放到oob_data[mtd->oobsize - oobreadlen],在这里是data_buf[end]中</span>
<br><br>
/* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */<br><span style="color: #0010ff;">/* 跳过ecc检测 */</span>
<br>
if (!compareecc)<br>
goto readoob; <br><br>
/* Pick the ECC bytes out of the oob data */<br><span style="color: #0010ff;">/* 从刚读出来都oob_data中取出ecc数据(在这里是前三个字节) */</span>
<br>
for (j = 0; j < oobsel->eccbytes; j++)<br>
ecc_code[j] = oob_data[oob_config[j]];<br><br>
/* correct data, if neccecary */<br>
for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {<br>
ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);<br><span style="color: #0010ff;">/* 拿前面计算出来都ecc_cal数组都数据与读出来的ecc数据作比较,并尝试修正错误(但不保证能修复,具体看返回值) */</span>
<br><br>
/* Get next chunk of ecc bytes */<br>
j += eccbytes;<br><br>
/* Check, if we have a fs supplied oob-buffer, <br>
* This is the legacy mode. Used by YAFFS1<br>
* Should go away some day<br>
*/<br>
if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) { <br>
int *p = (int *)(&oob_data[mtd->oobsize]);<br>
p[i] = ecc_status;<br>
}<br><span style="color: #0010ff;">/* 很不幸,ecc检测发现错误且未能修复,报告错误 */</span>
<br>
if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) { <br>
DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x/n", page);<br>
ecc_failed++;<br>
}<br>
} <br><br>
readoob:<br>
/* check, if we have a fs supplied oob-buffer */<br>
if (oob_buf) {<br>
/* without autoplace. Legacy mode used by YAFFS1 */<br>
switch(oobsel->useecc) {<br>
case MTD_NANDECC_AUTOPLACE:<br>
case MTD_NANDECC_AUTOPL_USR:<br>
/* Walk through the autoplace chunks */<br>
for (i = 0; oobsel->oobfree[i][1]; i++) {<br>
int from = oobsel->oobfree[i][0];<br>
int num = oobsel->oobfree[i][1];<br>
memcpy(&oob_buf[oob], &oob_data[from], num);<br>
oob += num;<br>
}<br>
break;<br>
case MTD_NANDECC_PLACE:<br>
/* YAFFS1 legacy mode */<br>
oob_data += this->eccsteps * sizeof (int);<br>
default:<br>
oob_data += mtd->oobsize;<br>
}<br>
}<br>
readdata:<br>
/* Partial page read, transfer data into fs buffer <br><span style="color: #0010ff;">* 读位置不是页对齐,从data_poi(data_buf中)提取所需要都数据</span>
<br>
*/<br>
if (!aligned) { <br>
for (j = col; j < end && read < len; j++)<br>
buf[read++] = data_poi[j];<span style="color: #0010ff;">//read自增</span>
<br>
this->pagebuf = realpage; <br>
} else <br>
read += mtd->oobblock;<span style="color: #0010ff;">//整页读取,计数值加上整页的数目(512)</span>
<br><br>
/* Apply delay or wait for ready/busy pin <br>
* Do this before the AUTOINCR check, so no problems<br>
* arise if a chip which does auto increment<br>
* is marked as NOAUTOINCR by the board driver.<br>
*/<br>
if (!this->dev_ready) <br>
udelay (this->chip_delay);<br>
else<br>
nand_wait_ready(mtd);<br><br>
if (read == len)<span style="color: #0010ff;">//所需数据读完都情况,退出读循环</span>
。<br>
break; <br><br>
/* For subsequent reads align to page boundary. */<br>
col = 0;<span style="color: #0010ff;">//对于读位置不是页对齐都情况,前面已对其进行林相应都处理,现在读位置变得页对齐了。</span>
<br>
/* Increment page address */<br>
realpage++;<span style="color: #0010ff;">//页地址加1,读取下一页。</span>
<br><br>
page = realpage & this->pagemask;<br>
/* Check, if we cross a chip boundary */<br>
if (!page) {<br>
chipnr++;<br>
this->select_chip(mtd, -1);<br>
this->select_chip(mtd, chipnr);<br>
}<br>
/* Check, if the chip supports auto page increment <br>
* or if we have hit a block boundary. <br><span style="color: #0010ff;">* 如果芯片支持页自增操作,且未到block boundary(15)的话,不用再发送读命令</span>
<br>
*/ <br>
if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))<br>
sndcmd = 1; <br>
}<br><br>
/* Deselect and wake up anyone waiting on the device */<br>
if (flags & NAND_GET_DEVICE)<br>
nand_release_device(mtd);<span style="color: #0010ff;">//放弃对设备都控制权,好让其它进程获取并占有它</span>
<br><br>
/*<br>
* Return success, if no ECC failures, else -EBADMSG<br>
* fs driver will take care of that, because<br>
* retlen == desired len and result == -EBADMSG<br>
*/<br>
*retlen = read;<br>
return ecc_failed ? -EBADMSG : 0;<br>
}<br><br><span style="color: #ff0030;">好的,接着研究一下如何通过MTD原始设备进而通过FLASH硬件驱动向FLASH存储器写数据。</span>
<br><br>
引用自<<Linux系统移植>>一文:<br><br>
写Nand Flash<br>
当对nand flash的设备文件(nand flash在/dev下对应的文件)执行系统调用write(),或在某个文件系统中对该设备<br>
进行读操作时, 会调用struct mtd_info中write方法,他们缺省调用函数为nand_write(),这两个函数在<br>
drivers/mtd/nand/nand_base.c中定义. nand_write()调用nand_write_ecc(),执行写操作.在<br>
nand_do_write_ecc()函数中,主要完成如下几项工作:<br>
1. 会调用在nand flash驱动中对struct nand_chip重载的select_chip方法,即<br>
s3c2410_nand_select_chip()选择要操作的MTD芯片.<br>
2. 调用nand_write_page()写一个页.<br>
3. 在nand_write_page()中,会调用在struct nand_chip中系统缺省的方法cmdfunc发送写命令<br>
到nand flash.<br>
4. 在nand_write_page()中,会调用在nand flash驱动中对struct nand_chip重载的<br>
write_buf(),即s3c2410_nand_write_buf()从Nand Flash的控制器的数据寄存器中写入数据.<br>
5. 在nand_write_page()中,会调用在nand flash驱动中对struct nand_chip重载waitfunc方法,<br>
该方法调用系统缺省函数nand_wait(),该方法获取操作状态,并等待nand flash操作完成.等<br>
待操作完成,是调用nand flash驱动中对struct nand_chip中重载的dev_ready方法,即<br>
s3c2410_nand_devready()函数.<br><br>
下面研究一下其中的细节:<br>
/**<br>
* nand_write - [MTD Interface] compability function for nand_write_ecc<br>
* @mtd: MTD device structure<br>
* @to: offset to write to<br>
* @len: number of bytes to write<br>
* @retlen: pointer to variable to store the number of written bytes<br>
* @buf: the data to write<br>
*<br>
* This function simply calls nand_write_ecc with oob buffer and oobsel = NULL<br>
*<br>
*/<br>
static int <span style="color: red;">nand_write</span>
(struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)<br>
{<br>
return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));<br>
}<br><span style="color: #0010ff;">注:</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> 以参数eccbuf、oobsel为NULL,调用nand_write_ecc<span style="color: #0010ff;">函数。</span>
<br><br><span style="color: #040000;">/**</span>
<br style="color: #040000;"><span style="color: #040000;">* nand_write_ecc - [MTD Interface] NAND write with ECC</span>
<br style="color: #040000;"><span style="color: #040000;">* @mtd: MTD device structure</span>
<br style="color: #040000;"><span style="color: #040000;">* @to: offset to write to</span>
<br style="color: #040000;"><span style="color: #040000;">* @len: number of bytes to write</span>
<br style="color: #040000;"><span style="color: #040000;">* @retlen: pointer to variable to store the number of written bytes</span>
<br style="color: #040000;"><span style="color: #040000;">* @buf: the data to write</span>
<br style="color: #040000;"><span style="color: #040000;">* @eccbuf: filesystem supplied oob data buffer</span>
<br style="color: #040000;"><span style="color: #040000;">* @oobsel: oob selection structure</span>
<br style="color: #040000;"><span style="color: #040000;">*</span>
<br style="color: #040000;"><span style="color: #040000;">* NAND write with ECC</span>
<br style="color: #040000;"><span style="color: #040000;">*/</span>
<br style="color: #040000;"><span style="color: #040000;">static int <span style="color: red;">nand_write_ecc</span>
(struct mtd_info *mtd, loff_t to, size_t len,</span>
<br style="color: #040000;"><span style="color: #040000;"> size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)</span>
<br style="color: #040000;"><span style="color: #040000;">{</span>
<br style="color: #040000;"><span style="color: #040000;"> int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;</span>
<br style="color: #040000;"><span style="color: #040000;"> int autoplace = 0, numpages, totalpages;</span>
<br style="color: #040000;"><span style="color: #040000;"> struct nand_chip *this = mtd->priv;</span>
<br style="color: #040000;"><span style="color: #040000;"> u_char *oobbuf, *bufstart;</span>
<br style="color: #040000;"><span style="color: #040000;"> int ppblock = (1 << (this->phys_erase_shift - this->page_shift));<span style="color: #0010ff;">//page/block</span>
</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i/n", (unsigned int) to, (int) len);</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Initialize retlen, in case of early exit */</span>
<br style="color: #040000;"><span style="color: #040000;"> *retlen = 0;</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Do not allow write past end of device */<br><span style="color: #0010ff;">/* 超越nand flash容量的写操作是不允许的 */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> if ((to + len) > mtd->size) {</span>
<br style="color: #040000;"><span style="color: #040000;"> DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page/n");</span>
<br style="color: #040000;"><span style="color: #040000;"> return -EINVAL;</span>
<br style="color: #040000;"><span style="color: #040000;"> }</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* reject writes, which are not page aligned */<br><span style="color: #0010ff;">/* 不按页对齐的写操作同样是不允许的 */ </span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> if (NOTALIGNED (to) || NOTALIGNED(len)) {</span>
<br style="color: #040000;"><span style="color: #040000;"> printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data/n");</span>
<br style="color: #040000;"><span style="color: #040000;"> return -EINVAL;</span>
<br style="color: #040000;"><span style="color: #040000;"> }</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Grab the lock and see if the device is available */<br><span style="color: #0010ff;">/* 获取设备的控制权 */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> nand_get_device (this, mtd, FL_WRITING);</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Calculate chipnr */</span>
<br style="color: #040000;"><span style="color: #040000;"> /* <br><span style="color: #0010ff;">* 存在多片flash的情况下,计算出所要写的是哪片flash?</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * (当然,像我的板,只用一片nand flash,所以这个操作是不必要的)</span>
<br>
*/<br>
chipnr = (int)(to >> this->chip_shift);</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Select the NAND device */<br><span style="color: #0010ff;">/* 片选操作 */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> this->select_chip(mtd, chipnr);</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Check, if it is write protected */<br><span style="color: #0010ff;">/* 如果nand flash写保护,当然不能再写了 */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> if (nand_check_wp(mtd))</span>
<br style="color: #040000;"><span style="color: #040000;"> goto out;</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* if oobsel is NULL, use chip defaults */</span>
<br style="color: #040000;"><span style="color: #040000;"> if (oobsel == NULL) </span>
<br style="color: #040000;"><span style="color: #040000;"> oobsel = &mtd->oobinfo; </span>
<br style="color: #040000;"><span style="color: #040000;"> </span>
<br style="color: #040000;"><span style="color: #040000;"> /* Autoplace of oob data ? Use the default placement scheme */</span>
<br style="color: #040000;"><span style="color: #040000;"> if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {</span>
<br style="color: #040000;"><span style="color: #040000;"> oobsel = this->autooob;</span>
<br style="color: #040000;"><span style="color: #040000;"> autoplace = 1;</span>
<br style="color: #040000;"><span style="color: #040000;"> } </span>
<br style="color: #040000;"><span style="color: #040000;"> if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)</span>
<br style="color: #040000;"><span style="color: #040000;"> autoplace = 1;</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Setup variables and oob buffer */</span>
<br style="color: #040000;"><span style="color: #040000;"> totalpages = len >> this->page_shift;<span style="color: #0010ff;">//计算所要读取的数据长度共有多少页</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> page = (int) (to >> this->page_shift);<span style="color: #0010ff;">//计算数据所要写到的开始页码</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Invalidate the page cache, if we write to the cached page */<br><span style="color: #0010ff;">/* 如果缓存保存的数据在我们要写数据的范围内,把缓存里的数据设置为不可用???? */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> if (page <= this->pagebuf && this->pagebuf < (page + totalpages)) </span>
<br style="color: #040000;"><span style="color: #040000;"> this->pagebuf = -1;</span>
<br style="color: #040000;"><span style="color: #040000;"> </span>
<br style="color: #040000;"><span style="color: #040000;"> /* Set it relative to chip */</span>
<br style="color: #040000;"><span style="color: #040000;"> page &= this->pagemask;</span>
<br style="color: #040000;"><span style="color: #040000;"> startpage = page;</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Calc number of pages we can write in one go */</span>
<br style="color: #040000;"><span style="color: #040000;"> numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);<span style="color: #0010ff;">//计算出本block中允许被写的页数</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);<span style="color: #0010ff;">//先不深入研究~_~</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> bufstart = (u_char *)buf;<span style="color: #0010ff;">//获取所要写数据的地址</span>
</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Loop until all data is written */<br><span style="color: #0010ff;"> /* 循环进行写操作 */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> while (written < len) {</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> this->data_poi = (u_char*) &buf[written];<span style="color: #0010ff;">//先把所要写的数据缓冲到data_poi下</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Write one page. If this is the last page to write</span>
<br style="color: #040000;"><span style="color: #040000;"> * or the last page in this block, then use the</span>
<br style="color: #040000;"><span style="color: #040000;"> * real pageprogram command, else select cached programming</span>
<br style="color: #040000;"><span style="color: #040000;"> * if supported by the chip.<br><span style="color: #0010ff;">* 如果这是所写数据的最后一个页或许这是所写block的最后一个页,调用nand flash的</span>
</span>
<br>
* pageprogram指令,真正把数据写入nand flash中(nand flash的最小擦除单元为block)</span>
<br style="color: #040000;"><span style="color: #0010ff;"><span style="color: #040000;"> */</span>
<br style="color: #040000;"><span style="color: #040000;"> ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));</span>
<br style="color: #040000;"><span style="color: #040000;"> if (ret) {</span>
<br style="color: #040000;"><span style="color: #040000;"> DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d/n", ret);</span>
<br style="color: #040000;"><span style="color: #040000;"> goto out;</span>
<br style="color: #040000;"><span style="color: #040000;"> } </span>
<br style="color: #040000;"><span style="color: #040000;"> /* Next oob page */</span>
<br style="color: #040000;"><span style="color: #040000;"> oob += mtd->oobsize;</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Update written bytes count */<br><span style="color: #0010ff;">/* 更新写入计数值 */</span>
<br style="color: #040000;"></span>
<span style="color: #040000;"> written += mtd->oobblock;</span>
<br style="color: #040000;"><span style="color: #040000;"> if (written == len)<span style="color: #0010ff;">//写入完毕,退出 </span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> goto cmp;</span>
<br style="color: #040000;"><span style="color: #040000;"> </span>
<br style="color: #040000;"><span style="color: #040000;"> /* Increment page address */</span>
<br style="color: #040000;"><span style="color: #040000;"> page++;<span style="color: #0010ff;">//下一页</span>
</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> /* Have we hit a block boundary ? Then we have to verify and</span>
<br style="color: #040000;"><span style="color: #040000;"> * if verify is ok, we have to setup the oob buffer for</span>
<br style="color: #040000;"><span style="color: #040000;"> * the next pages.<br><span style="color: #0010ff;">* </span>
</span>
</span>
<span style="color: #0010ff;">暂时不是很明白,</span>
<span style="color: #0010ff;"><span style="color: #040000;"><span style="color: #0010ff;">需要先搞明白</span>
</span>
nand_prepare_oobbuf函数的作用</span>
<br style="color: #040000;"><span style="color: #0010ff;"><span style="color: #040000;"> */</span>
<br style="color: #040000;"><span style="color: #040000;"> if (!(page & (ppblock - 1))){</span>
<br style="color: #040000;"><span style="color: #040000;"> int ofs;</span>
<br style="color: #040000;"><span style="color: #040000;"> this->data_poi = bufstart;</span>
</span>
<span style="color: #0010ff;"><span style="color: #040000;"><span style="color: #0010ff;">//怀疑</span>
</span>
nand_verify_pages用到</span>
<span style="color: #0010ff;"><br style="color: #040000;"><span style="color: #040000;"> ret = nand_verify_pages (mtd, this, startpage, </span>
<br style="color: #040000;"><span style="color: #040000;"> page - startpage,</span>
<br style="color: #040000;"><span style="color: #040000;"> oobbuf, oobsel, chipnr, (eccbuf != NULL));<span style="color: #0010ff;">//一页写完,检查数据</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> if (ret) {</span>
<br style="color: #040000;"><span style="color: #040000;"> DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d/n", ret);</span>
<br style="color: #040000;"><span style="color: #040000;"> goto out;</span>
<br style="color: #040000;"><span style="color: #040000;"> } </span>
<br style="color: #040000;"><span style="color: #040000;"> *retlen = written;</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> ofs = autoplace ? mtd->oobavail : mtd->oobsize;</span>
<br style="color: #040000;"><span style="color: #040000;"> if (eccbuf)</span>
<br style="color: #040000;"><span style="color: #040000;"> eccbuf += (page - startpage) * ofs;</span>
<br style="color: #040000;"><span style="color: #040000;"> totalpages -= page - startpage;<span style="color: #0010ff;">//更新需要写的页数</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> numpages = min (totalpages, ppblock);<span style="color: #0010ff;">//更新可以写的页数</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> page &= this->pagemask;<span style="color: #0010ff;">//更新页码</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> startpage = page;<span style="color: #0010ff;">//更新开始页码</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, </span>
<br style="color: #040000;"><span style="color: #040000;"> autoplace, numpages);</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Check, if we cross a chip boundary */</span>
<br style="color: #040000;"><span style="color: #040000;"> if (!page) {</span>
<br style="color: #040000;"><span style="color: #040000;"> chipnr++;</span>
<br style="color: #040000;"><span style="color: #040000;"> this->select_chip(mtd, -1);</span>
<br style="color: #040000;"><span style="color: #040000;"> this->select_chip(mtd, chipnr);</span>
<br style="color: #040000;"><span style="color: #040000;"> }</span>
<br style="color: #040000;"><span style="color: #040000;"> }</span>
<br style="color: #040000;"><span style="color: #040000;"> }</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Verify the remaining pages */</span>
<br style="color: #040000;"><span style="color: #040000;">cmp:</span>
<br style="color: #040000;"><span style="color: #040000;"> this->data_poi = bufstart;<span style="color: #0010ff;">//怀疑</span>
</span>
nand_verify_pages用到<br style="color: #040000;"><span style="color: #040000;"> ret = nand_verify_pages (mtd, this, startpage, totalpages,</span>
<br style="color: #040000;"><span style="color: #040000;"> oobbuf, oobsel, chipnr, (eccbuf != NULL));</span>
<br style="color: #040000;"><span style="color: #040000;"> if (!ret)</span>
<br style="color: #040000;"><span style="color: #040000;"> *retlen = written;</span>
<br style="color: #040000;"><span style="color: #040000;"> else </span>
<br style="color: #040000;"><span style="color: #040000;"> DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d/n", ret);</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;">out:</span>
<br style="color: #040000;"><span style="color: #040000;"> /* Deselect and wake up anyone waiting on the device */</span>
<br style="color: #040000;"><span style="color: #040000;"> nand_release_device(mtd);<span style="color: #0010ff;">//放弃对设备的控制权</span>
</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;"> return ret;</span>
<br style="color: #040000;"><span style="color: #040000;">}<br><br>
/**<br>
* nand_write_page - [GENERIC] write one page<br>
* @mtd: MTD device structure<br>
* @this: NAND chip structure<br>
* @page: startpage inside the chip, must be called with (page & this->pagemask)<br>
* @oob_buf: out of band data buffer<br>
* @oobsel: out of band selecttion structre<br>
* @cached: 1 = enable cached programming if supported by chip<br>
*<br>
* Nand_page_program function is used for write and writev !<br>
* This function will always program a full page of data<br>
* If you call it with a non page aligned buffer, you're lost :)<br>
*<br>
* Cached programming is not supported yet.<br>
*/<br>
static int <span style="color: red;">nand_write_page</span>
(struct mtd_info *mtd, struct nand_chip *this, int page, <br>
u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)<br>
{<br>
int i, status;<br>
u_char ecc_code[32];<br>
int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;<br>
int *oob_config = oobsel->eccpos;<br>
int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;<br>
int eccbytes = 0;<br><br>
/* FIXME: Enable cached programming */<br>
cached = 0;<span style="color: #0010ff;">//在高版本的内核下找到这样的解释:<br>
/*<br>
* Cached progamming disabled for now, Not sure if its worth the<br>
* trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)<br>
*/<br></span>
<br>
/* Send command to begin auto page programming */<br><span style="color: #0010ff;">/* 发送页编程指令 */</span>
<br>
this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);<br><br>
/* Write out complete page of data, take care of eccmode */<br>
switch (eccmode) {<br>
/* No ecc, write all */<br>
case NAND_ECC_NONE:<br>
printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended/n");<br>
this->write_buf(mtd, this->data_poi, mtd->oobblock);<br>
break;<br><br>
/* Software ecc 3/256, write all */<br>
case NAND_ECC_SOFT:<br>
for (; eccsteps; eccsteps--) {<br>
this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);<span style="color: #0010ff;">//计算出一页的ecc数据</span>
<br>
for (i = 0; i < 3; i++, eccidx++)<br>
oob_buf[oob_config[eccidx]] = ecc_code[i];<span style="color: #0010ff;">//存放到ecc_code数组中</span>
<br>
datidx += this->eccsize;<br>
}<br>
this->write_buf(mtd, this->data_poi, mtd->oobblock);<span style="color: #0010ff;">//调用FLASH硬件驱动层进行写操作</span>
<br>
break;<br>
default:<br>
eccbytes = this->eccbytes;<br>
for (; eccsteps; eccsteps--) {<br>
/* enable hardware ecc logic for write */<br>
this->enable_hwecc(mtd, NAND_ECC_WRITE);<br>
this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);<br>
this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);<br>
for (i = 0; i < eccbytes; i++, eccidx++)<br>
oob_buf[oob_config[eccidx]] = ecc_code[i];<br>
/* If the hardware ecc provides syndromes then<br>
* the ecc code must be written immidiately after<br>
* the data bytes (words) */<br>
if (this->options & NAND_HWECC_SYNDROME)<br>
this->write_buf(mtd, ecc_code, eccbytes);<br>
datidx += this->eccsize;<br>
}<br>
break;<br>
}<br><br>
/* Write out OOB data */<br>
if (this->options & NAND_HWECC_SYNDROME)<br>
this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);<br>
else <br>
this->write_buf(mtd, oob_buf, mtd->oobsize);<span style="color: #0010ff;">//写oob data,主要把上面计算的ecc值写进去</span>
<br><br>
/* Send command to actually program the data */<br>
this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);<br><br>
if (!cached) {<br>
/* call wait ready function */<br>
status = this->waitfunc (mtd, this, FL_WRITING);<span style="color: #0010ff;">//等待写入完成</span>
<br><br>
/* See if operation failed and additional status checks are available */<br>
if ((status & NAND_STATUS_FAIL) && (this->errstat)) {<br>
status = this->errstat(mtd, this, FL_WRITING, status, page);<br>
}<br><br>
/* See if device thinks it succeeded */<br>
if (status & NAND_STATUS_FAIL) {<br>
DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);<br>
return -EIO;<br>
}<br>
} else {<br>
/* FIXME: Implement cached programming ! */<br>
/* wait until cache is ready*/<br>
// status = this->waitfunc (mtd, this, FL_CACHEDRPG);<span style="color: #0010ff;">//cached的写操作暂时没用</span>
<br>
}<br>
return 0; <br>
}</span>
</span>
</p>
<p><span style="color: #0010ff;"><br></span>
</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值