`
yzd
  • 浏览: 1820721 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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>
看了&lt;&lt;Linux MTD源代码分析&gt;&gt;后对以MTD的分层结构以及各层的分工情况有了大致的了解,然而各层之间是如何进行对话的呢,对于这个问题,&lt;&lt;Linux MTD源代码分析&gt;&gt;上没有详细的去说明。<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(&amp;info-&gt;controller.lock);    //初始化自旋锁<br>
init_waitqueue_head(&amp;info-&gt;controller.wq); //初始化等待队列<br><br>
/* get the clock source and enable it */<br><br>
info-&gt;clk = clk_get(dev, "nand");<br>
if (IS_ERR(info-&gt;clk)) {<br>
  printk(KERN_ERR PFX "failed to get clock");<br>
  err = -ENOENT;<br>
  goto exit_error;<br>
}<br><br>
clk_use(info-&gt;clk);<br>
clk_enable(info-&gt;clk);<br><br>
/* allocate and map the resource */<br><br>
/* currently we assume we have the one resource */<br>
res = pdev-&gt;resource;                <span style="color: #0010ff;">//提取dev.c中定义的与设备相关的资源</span>
<br>
size = res-&gt;end - res-&gt;start + 1;<br><br>
info-&gt;area = request_mem_region(res-&gt;start, size, pdev-&gt;name);<br><br>
if (info-&gt;area == NULL) {<br>
  printk(KERN_ERR PFX "cannot reserve register region/n");<br>
  err = -ENOENT;<br>
  goto exit_error;<br>
}<br><br>
info-&gt;device = dev;<br>
info-&gt;platform = plat;              <span style="color: #0010ff;">//保存好struct s3c2410_platform_nand结构数据</span>
<br>
info-&gt;regs = ioremap(res-&gt;start, size);<span style="color: #0010ff;">//映射nand flash用到的寄存器</span>
<br>
info-&gt;is_s3c2440 = is_s3c2440;         <br><br>
if (info-&gt;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-&gt;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-&gt;sets : NULL;  <br>
nr_sets = (plat != NULL) ? plat-&gt;nr_sets : 1;<br><br>
info-&gt;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-&gt;mtds);<br>
info-&gt;mtds = kmalloc(size, GFP_KERNEL);<br>
if (info-&gt;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-&gt;mtds, size);<br><br>
/* initialise all possible chips */<br><br>
nmtd = info-&gt;mtds;<br><br>
for (setno = 0; setno &lt; 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-&gt;scan_res = nand_scan(&amp;nmtd-&gt;mtd,<br>
      (sets) ? sets-&gt;nr_chips : 1);<span style="color: red;">//为什么使用set-&gt;nr_chips(还没配置的东西)?</span>
<br><br>
  if (nmtd-&gt;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 = &amp;nmtd-&gt;chip;<br><br>
chip-&gt;IO_ADDR_R  = info-&gt;regs + S3C2410_NFDATA; <span style="color: #0010ff;">//读地址</span>
<br>
chip-&gt;IO_ADDR_W = info-&gt;regs + S3C2410_NFDATA;   <span style="color: #0010ff;">//写地址</span>
<br>
chip-&gt;hwcontrol = s3c2410_nand_hwcontrol;  <br>
chip-&gt;dev_ready = s3c2410_nand_devready;        <span style="color: #0010ff;">//ready状态查询</span>
<br>
chip-&gt;write_buf = s3c2410_nand_write_buf;       <span style="color: #0010ff;">//写函数</span>
<br>
chip-&gt;read_buf = s3c2410_nand_read_buf;        <span style="color: #0010ff;">//读函数</span>
<br>
chip-&gt;select_chip = s3c2410_nand_select_chip;      <span style="color: #0010ff;">//片选函数</span>
<br>
chip-&gt;chip_delay = 50;<br>
chip-&gt;priv  = nmtd;<br>
chip-&gt;options  = 0;<br>
chip-&gt;controller = &amp;info-&gt;controller;<br><br>
if (info-&gt;is_s3c2440) {<br>
  chip-&gt;IO_ADDR_R = info-&gt;regs + S3C2440_NFDATA;<br>
  chip-&gt;IO_ADDR_W = info-&gt;regs + S3C2440_NFDATA;<br>
  chip-&gt;hwcontrol = s3c2440_nand_hwcontrol;<br>
}<br><br>
nmtd-&gt;info  = info;<br>
nmtd-&gt;mtd.priv  = chip;       <br><span style="color: #0010ff;">//nand_scan函数中会调用struct nand_chip *this = mtd-&gt;priv取出该struct nand_chip结构</span>
<br>
nmtd-&gt;set  = set;<br><br>
if (hardware_ecc) {<br>
  chip-&gt;correct_data = s3c2410_nand_correct_data;<br>
  chip-&gt;enable_hwecc = s3c2410_nand_enable_hwecc;<br>
  chip-&gt;calculate_ecc = s3c2410_nand_calculate_ecc;<br>
  chip-&gt;eccmode  = NAND_ECC_HW3_512;<br>
  chip-&gt;autooob = &amp;nand_hw_eccoob;<br><br>
  if (info-&gt;is_s3c2440) {<br>
   chip-&gt;enable_hwecc = s3c2440_nand_enable_hwecc;<br>
   chip-&gt;calculate_ecc = s3c2440_nand_calculate_ecc;<br>
  }<br>
} else {                      <br>
  chip-&gt;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-&gt;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-&gt;IO_ADDR_W = info-&gt;regs + S3C2410_NFCMD;<span style="color: #2000ff;">//写命令</span>
<br>
  break;<br><br>
case NAND_CTL_SETALE:<br>
  chip-&gt;IO_ADDR_W = info-&gt;regs + S3C2410_NFADDR;<span style="color: #2000ff;">//写地址</span>
<br>
  break;<br><br>
  /* NAND_CTL_CLRCLE: */<br>
  /* NAND_CTL_CLRALE: */<br>
default:<br>
  chip-&gt;IO_ADDR_W = info-&gt;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-&gt;is_s3c2440)<br>
  return readb(info-&gt;regs + S3C2440_NFSTAT) &amp; S3C2440_NFSTAT_READY;<br>
return readb(info-&gt;regs + S3C2410_NFSTAT) &amp; 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-&gt;priv;<br>
writesb(this-&gt;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-&gt;priv;<br>
readsb(this-&gt;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-&gt;priv;<br>
void __iomem *reg;<br>
unsigned long cur;<br>
unsigned long bit;<br><br>
nmtd = this-&gt;priv;<br>
info = nmtd-&gt;info;<br><br>
bit = (info-&gt;is_s3c2440) ? S3C2440_NFCONT_nFCE : S3C2410_NFCONF_nFCE;<br>
reg = info-&gt;regs+((info-&gt;is_s3c2440) ? S3C2440_NFCONT:S3C2410_NFCONF);<br><br>
cur = readl(reg);<br><br>
if (chip == -1) {<br>
  cur |= bit;<br>
} else {<br>
  if (nmtd-&gt;set != NULL &amp;&amp; chip &gt; nmtd-&gt;set-&gt;nr_chips) {<br>
   printk(KERN_ERR PFX "chip %d out of range/n", chip);<br>
   return;<br>
  }<br><br>
  if (info-&gt;platform != NULL) {<br>
   if (info-&gt;platform-&gt;select_chip != NULL)<br>
    (info-&gt;platform-&gt;select_chip)(nmtd-&gt;set, chip);<br>
  }<br><br>
  cur &amp;= ~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-&gt;priv;   <span style="color: #0010ff;"> //取出struct nand_chip结构</span>
<br><br>
/* Get buswidth to select the correct functions*/<br>
busw = this-&gt;options &amp; 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-&gt;chip_delay)             <br>
  this-&gt;chip_delay = 20;<br><br>
/* check, if a user supplied command function given */<br>
if (this-&gt;cmdfunc == NULL)            <span style="color: #0010ff;">//填充命令函数</span>
<br>
  this-&gt;cmdfunc = nand_command;<br><br>
/* check, if a user supplied wait function given */<br>
if (this-&gt;waitfunc == NULL)           <span style="color: #0010ff;">//填充等待函数</span>
<br>
  this-&gt;waitfunc = nand_wait;<br><br>
if (!this-&gt;select_chip)              <span style="color: #0010ff;">//s3c2410_nand_init_chip中已定义</span>
<br>
  this-&gt;select_chip = nand_select_chip;<br>
if (!this-&gt;write_byte)               <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;write_byte = busw ? nand_write_byte16 : nand_write_byte;<br>
if (!this-&gt;read_byte)                <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;read_byte = busw ? nand_read_byte16 : nand_read_byte;<br>
if (!this-&gt;write_word)               <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;write_word = nand_write_word;<br>
if (!this-&gt;read_word)                <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;read_word = nand_read_word;<br>
if (!this-&gt;block_bad)               <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;block_bad = nand_block_bad;<br>
if (!this-&gt;block_markbad)             <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;block_markbad = nand_default_block_markbad;<br>
if (!this-&gt;write_buf)                <span style="color: #0010ff;">//s3c2410_nand_init_chip中已定义</span>
<br>
  this-&gt;write_buf = busw ? nand_write_buf16 : nand_write_buf;<br>
if (!this-&gt;read_buf)                <span style="color: #0010ff;">//s3c2410_nand_init_chip中已定义</span>
<br>
  this-&gt;read_buf = busw ? nand_read_buf16 : nand_read_buf;<br>
if (!this-&gt;verify_buf)               <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;<br>
if (!this-&gt;scan_bbt)                <span style="color: #0010ff;">//使用默认的</span>
<br>
  this-&gt;scan_bbt = nand_default_bbt;<br><br>
/* Select the device */<br>
this-&gt;select_chip(mtd, 0);    //片选,可惜在s3c2410 nand flash控制器中此操作为空<br><br>
/* Send the command for reading device ID */<br>
this-&gt;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-&gt;read_byte(mtd);       <span style="color: #0010ff;">//读取生产商ID</span>
<br>
nand_dev_id = this-&gt;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-&gt;name) mtd-&gt;name = nand_flash_ids[i].name; <span style="color: #0010ff;">//填充设备名</span>
<br>
  this-&gt;chipsize = nand_flash_ids[i].chipsize &lt;&lt; 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-&gt;read_byte(mtd);<br>
   /* The 4th id byte is the important one */<br>
   extid = this-&gt;read_byte(mtd);<br>
   /* Calc pagesize */<br>
   mtd-&gt;oobblock = 1024 &lt;&lt; (extid &amp; 0x3);<br>
   extid &gt;&gt;= 2;<br>
   /* Calc oobsize */<br>
   mtd-&gt;oobsize = (8 &lt;&lt; (extid &amp; 0x03)) * (mtd-&gt;oobblock / 512);<br>
   extid &gt;&gt;= 2;<br>
   /* Calc blocksize. Blocksize is multiples of 64KiB */<br>
   mtd-&gt;erasesize = (64 * 1024) &lt;&lt; (extid &amp; 0x03);<br>
   extid &gt;&gt;= 2;<br>
   /* Get buswidth information */<br>
   busw = (extid &amp; 0x01) ? NAND_BUSWIDTH_16 : 0;<br><br>
  } else {<br>
   /* Old devices have this data hardcoded in the<br>
   * device id table */<br>
   mtd-&gt;erasesize = nand_flash_ids[i].erasesize;  <span style="color: #0010ff;">//填充檫除单元大小</span>
<span style="color: #2000ff;">(16k)</span>
<br>
   mtd-&gt;oobblock = nand_flash_ids[i].pagesize;   <span style="color: #0010ff;">//填充页大小(</span>
<span style="color: #0010ff;">512</span>
<span style="color: #0010ff;">)</span>
<br>
   mtd-&gt;oobsize = mtd-&gt;oobblock / 32;         <span style="color: #0010ff;">//oob大小(512/32=16)</span>
<br>
   busw = nand_flash_ids[i].options &amp; 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-&gt;options &amp; 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-&gt;name);<br>
   printk (KERN_WARNING <br>
    "NAND bus width %d instead %d bit/n", <br>
     (this-&gt;options &amp; NAND_BUSWIDTH_16) ? 16 : 8,<br>
     busw ? 16 :;<br>
   this-&gt;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-&gt;page_shift = ffs(mtd-&gt;oobblock) - 1;<br>
  this-&gt;bbt_erase_shift = this-&gt;phys_erase_shift = ffs(mtd-&gt;erasesize) - 1;<br>
  this-&gt;chip_shift = ffs(this-&gt;chipsize) - 1;<br><br>
  /* Set the bad block position */<br><span style="color: #0010ff;">//标注此nand flash为大页还是小页?</span>
<br>
  this-&gt;badblockpos = mtd-&gt;oobblock &gt; 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-&gt;options &amp;= ~NAND_CHIPOPTIONS_MSK;<br>
  this-&gt;options |= nand_flash_ids[i].options &amp; NAND_CHIPOPTIONS_MSK;<br>
  /* Set this as a default. Board drivers can override it, if neccecary */<br>
  this-&gt;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 &amp;&amp; !nand_flash_ids[i].pagesize)<br>
   this-&gt;options &amp;= ~NAND_SAMSUNG_LP_OPTIONS;<br><br>
  /* Check for AND chips with 4 page planes */<br>
  if (this-&gt;options &amp; NAND_4PAGE_ARRAY)<br>
   this-&gt;erase_cmd = multi_erase_cmd;<br>
  else<br>
   this-&gt;erase_cmd = single_erase_cmd;   <br><br>
  /* Do not replace user supplied command function ! */<br>
  if (mtd-&gt;oobblock &gt; 512 &amp;&amp; this-&gt;cmdfunc == nand_command)<br>
   this-&gt;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-&gt;select_chip(mtd, -1);<br>
  return 1;<br>
}<br><br><span style="color: #0010ff;">//统计一下同种类型的nand flash有多少块(我板上只有一块)</span>
<br>
for (i=1; i &lt; maxchips; i++) {<br>
  this-&gt;select_chip(mtd, i);<br><br>
  /* Send the command for reading device ID */<br>
  this-&gt;cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);<br><br>
  /* Read manufacturer and device IDs */<br>
  if (nand_maf_id != this-&gt;read_byte(mtd) ||<br>
   nand_dev_id != this-&gt;read_byte(mtd))<br>
   break;<br>
}<br>
if (i &gt; 1)<br>
  printk(KERN_INFO "%d NAND chips detected/n", i);<br><br>
/* Allocate buffers, if neccecary */<br>
if (!this-&gt;oob_buf) {<br>
  size_t len;<br><span style="color: #0010ff;">//求出一个檫除单元64K中oob所占用的总空间</span>
<br>
  len = mtd-&gt;oobsize &lt;&lt; (this-&gt;phys_erase_shift - this-&gt;page_shift);<br>
  this-&gt;oob_buf = kmalloc (len, GFP_KERNEL);<br>
  if (!this-&gt;oob_buf) {<br>
   printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf/n");<br>
   return -ENOMEM;<br>
  }<br>
  this-&gt;options |= NAND_OOBBUF_ALLOC;<span style="color: #0010ff;">//oob空间已分配,置相应的标志位</span>
<br>
}<br><br>
if (!this-&gt;data_buf) {<br>
  size_t len;<br>
  len = mtd-&gt;oobblock + mtd-&gt;oobsize;//512+16=128<br>
  this-&gt;data_buf = kmalloc (len, GFP_KERNEL);<br>
  if (!this-&gt;data_buf) {<br>
   if (this-&gt;options &amp; NAND_OOBBUF_ALLOC)<br>
    kfree (this-&gt;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-&gt;options |= NAND_DATABUF_ALLOC;</span>
//数据空间已分配,置相应的标志位</span>
<br>
}<br><br>
/* Store the number of chips and calc total size for mtd */<br>
this-&gt;numchips = i;<span style="color: #0010ff;">//记录nand flash片数</span>
<br>
mtd-&gt;size = i * this-&gt;chipsize;<span style="color: #0010ff;">//计算出nand flash总大小</span>
<br>
/* Convert chipsize to number of pages per chip -1. */<br>
this-&gt;pagemask = (this-&gt;chipsize &gt;&gt; this-&gt;page_shift) - 1;//(64M&gt;&gt;9)-1=128k-1=0x1ffff<br><br>
/* Preset the internal oob buffer */<br><span style="color: #0010ff;">//oob_buf全部置为0xff</span>
<br>
memset(this-&gt;oob_buf, 0xff, mtd-&gt;oobsize &lt;&lt; (this-&gt;phys_erase_shift - this-&gt;page_shift));<br><br>
/* If no default placement scheme is given, select an<br>
* appropriate one */<br>
if (!this-&gt;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-&gt;oobsize) { <br>
  case 8:<br>
   this-&gt;autooob = &amp;nand_oob_8;<br>
   break;<br>
  case 16:<br>
   this-&gt;autooob = &amp;nand_oob_16;<span style="color: #0010ff;">//我们的nand flash属于这一类</span>
<br>
   break;<br>
  case 64:<br>
   this-&gt;autooob = &amp;nand_oob_64;<br>
   break;<br>
  default:<br>
   printk (KERN_WARNING "No oob scheme defined for oobsize %d/n",<br>
    mtd-&gt;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-&gt;oobavail = 0;<br>
for (i = 0; this-&gt;autooob-&gt;oobfree[i][1]; i++)<br>
  mtd-&gt;oobavail += this-&gt;autooob-&gt;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-&gt;eccsize = 256; /* set default eccsize */ <br>
this-&gt;eccbytes = 3;<br><br>
switch (this-&gt;eccmode) {<br>
case NAND_ECC_HW12_2048:<br>
  if (mtd-&gt;oobblock &lt; 2048) {<br>
   printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC/n",<br>
    mtd-&gt;oobblock);<br>
   this-&gt;eccmode = NAND_ECC_SOFT;<br>
   this-&gt;calculate_ecc = nand_calculate_ecc;<br>
   this-&gt;correct_data = nand_correct_data;<br>
  } else<br>
   this-&gt;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-&gt;oobblock == 256) {<br>
   printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC /n");<br>
   this-&gt;eccmode = NAND_ECC_SOFT;<br>
   this-&gt;calculate_ecc = nand_calculate_ecc;<br>
   this-&gt;correct_data = nand_correct_data;<br>
  } else <br>
   this-&gt;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-&gt;eccmode = NAND_ECC_NONE;<br>
  break;<br><br>
case NAND_ECC_SOFT: <br>
  this-&gt;calculate_ecc = nand_calculate_ecc;<br>
  this-&gt;correct_data = nand_correct_data;<br>
  break;<br><br>
default:<br>
  printk (KERN_WARNING "Invalid NAND_ECC_MODE %d/n", this-&gt;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-&gt;eccmode) {<br>
case NAND_ECC_HW12_2048:<br>
  this-&gt;eccbytes += 4;<br>
case NAND_ECC_HW8_512: <br>
  this-&gt;eccbytes += 2;<br>
case NAND_ECC_HW6_512: <br>
  this-&gt;eccbytes += 3;<br>
case NAND_ECC_HW3_512: <br>
case NAND_ECC_HW3_256:<br>
  if (this-&gt;calculate_ecc &amp;&amp; this-&gt;correct_data &amp;&amp; this-&gt;enable_hwecc)<br>
   break;<br>
  printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible/n");<br>
  BUG(); <br>
}<br><br>
mtd-&gt;eccsize = this-&gt;eccsize;<br><br>
/* Set the number of read / write steps for one page to ensure ECC generation */<br>
switch (this-&gt;eccmode) {<br>
case NAND_ECC_HW12_2048:<br>
  this-&gt;eccsteps = mtd-&gt;oobblock / 2048;<br>
  break;<br>
case NAND_ECC_HW3_512:<br>
case NAND_ECC_HW6_512:<br>
case NAND_ECC_HW8_512:<br>
  this-&gt;eccsteps = mtd-&gt;oobblock / 512;<br>
  break;<br>
case NAND_ECC_HW3_256:<br>
case NAND_ECC_SOFT: <br>
  this-&gt;eccsteps = mtd-&gt;oobblock / 256;<br>
  break;<br><br>
case NAND_ECC_NONE: <br>
  this-&gt;eccsteps = 1;<br>
  break;<br>
}<br><br>
/* Initialize state, waitqueue and spinlock */<br>
this-&gt;state = FL_READY;<br>
init_waitqueue_head (&amp;this-&gt;wq);<br>
spin_lock_init (&amp;this-&gt;chip_lock);<br><br>
/* De-select the device */<br>
this-&gt;select_chip(mtd, -1);<br><br>
/* Invalidate the pagebuffer reference */<br>
this-&gt;pagebuf = -1;<br><br>
/* Fill in remaining MTD driver data */<br><span style="color: #0010ff;">//填充mtd结构的其它部分</span>
<br>
mtd-&gt;type = MTD_NANDFLASH;<br>
mtd-&gt;flags = MTD_CAP_NANDFLASH | MTD_ECC;<br>
mtd-&gt;ecctype = MTD_ECC_SW;<br>
mtd-&gt;erase = nand_erase;<br>
mtd-&gt;point = NULL;<br>
mtd-&gt;unpoint = NULL;<br>
mtd-&gt;read = nand_read;<br>
/* nand_read-&gt;nand_do_read_ecc-&gt;read_buf-&gt;s3c2410_nand_read_buf */<br>
mtd-&gt;write = nand_write;<br>
/* nand_write-&gt;nand_write_ecc-&gt;nand_write_page-&gt;write_buf-&gt;s3c2410_nand_write_buf */<br>
mtd-&gt;read_ecc = nand_read_ecc;<br>
mtd-&gt;write_ecc = nand_write_ecc;<br>
mtd-&gt;read_oob = nand_read_oob;<br>
mtd-&gt;write_oob = nand_write_oob;<br>
mtd-&gt;readv = NULL;<br>
mtd-&gt;writev = nand_writev;<br>
mtd-&gt;writev_ecc = nand_writev_ecc;<br>
mtd-&gt;sync = nand_sync;<br>
mtd-&gt;lock = NULL;<br>
mtd-&gt;unlock = NULL;<br>
mtd-&gt;suspend = NULL;<br>
mtd-&gt;resume = NULL;<br>
mtd-&gt;block_isbad = nand_block_isbad;<br>
mtd-&gt;block_markbad = nand_block_markbad;<br><br>
/* and make the autooob the default one */<br>
memcpy(&amp;mtd-&gt;oobinfo, this-&gt;autooob, sizeof(mtd-&gt;oobinfo));<br><br>
mtd-&gt;owner = THIS_MODULE;<br><br>
/* Check, if we should skip the bad block table scan */<br>
if (this-&gt;options &amp; NAND_SKIP_BBTSCAN)<br>
  return 0;<br><br>
/* Build bad block table */<br>
return this-&gt;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-&gt;priv;<br><br>
/* Begin command latch cycle */<br>
this-&gt;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 &gt;= mtd-&gt;oobblock) {<span style="color: #2000ff;">  //读/写位置超出512,读oob_data</span>
<br>
   /* OOB area */<br>
   column -= mtd-&gt;oobblock;<br>
   readcmd = NAND_CMD_READOOB;<br>
  } else if (column &lt; 256) {   <span style="color: #2000ff;">//读/写位置在前512,使用read0命令</span>
<br>
   /* First 256 bytes --&gt; 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-&gt;write_byte(mtd, readcmd);  <span style="color: #2000ff;">//写入具体命令</span>
<br>
}<br>
this-&gt;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-&gt;hwcontrol(mtd, NAND_CTL_CLRCLE);  <span style="color: #2000ff;">//锁存命令</span>
<br><br>
if (column != -1 || page_addr != -1) {<br>
  this-&gt;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-&gt;options &amp; NAND_BUSWIDTH_16)<br>
    column &gt;&gt;= 1;<br>
   this-&gt;write_byte(mtd, column);  <span style="color: #2000ff;">//写入列地址</span>
<br>
  }<br>
  if (page_addr != -1) {    <span style="color: #2000ff;">//写入页地址(分三个字节写入)</span>
<br>
   this-&gt;write_byte(mtd, (unsigned char) (page_addr &amp; 0xff));<br>
   this-&gt;write_byte(mtd, (unsigned char) ((page_addr &gt;&gt; &amp; 0xff));<br>
   /* One more address cycle for devices &gt; 32MiB */<br>
   if (this-&gt;chipsize &gt; (32 &lt;&lt; 20))<br>
    this-&gt;write_byte(mtd, (unsigned char) ((page_addr &gt;&gt; 16) &amp; 0x0f));<br>
  }<br>
  /* Latch in address */<br><span style="color: #2000ff;">/* 锁存地址 */</span>
<br>
  this-&gt;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-&gt;dev_ready) <span style="color: #0010ff;">//判断nand flash 是否busy(1:ready 0:busy)</span>
<br>
   break;<br>
  udelay(this-&gt;chip_delay);<br>
  this-&gt;hwcontrol(mtd, NAND_CTL_SETCLE);<br>
  this-&gt;write_byte(mtd, NAND_CMD_STATUS);<br>
  this-&gt;hwcontrol(mtd, NAND_CTL_CLRCLE);<br>
  while ( !(this-&gt;read_byte(mtd) &amp; 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-&gt;dev_ready) {<br>
   udelay (this-&gt;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-&gt;priv;<br>
unsigned long timeo = jiffies + 2;<br><br>
/* wait until command is processed or timeout occures */<br>
do {<br>
  if (this-&gt;dev_ready(mtd))   <span style="color: #0010ff;">//简单调用this-&gt;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) &amp;&amp; (this-&gt;options &amp; NAND_IS_AND))<br>
  this-&gt;cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);<br>
else <br>
  this-&gt;cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);<br><br>
while (time_before(jiffies, timeo)) {  <br>
  /* Check, if we were interrupted */<br>
  if (this-&gt;state != state)<br>
   return 0;<br><span style="color: #0010ff;"> /* 等待nand flash become ready */</span>
<br>
  if (this-&gt;dev_ready) {<br>
   if (this-&gt;dev_ready(mtd))<br>
    break; <br>
  } else {<br>
   if (this-&gt;read_byte(mtd) &amp; NAND_STATUS_READY)<br>
    break;<br>
  }<br>
  cond_resched();<br>
}<br>
status = (int) this-&gt;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-&gt;priv;<br>
u16 bad;<br><br>
if (getchip) {<br>
  page = (int)(ofs &gt;&gt; this-&gt;page_shift);<br>
  chipnr = (int)(ofs &gt;&gt; this-&gt;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-&gt;select_chip(mtd, chipnr);<br>
} else <br>
  page = (int) ofs; <br><br>
if (this-&gt;options &amp; NAND_BUSWIDTH_16) {<br>
  this-&gt;cmdfunc (mtd, NAND_CMD_READOOB, this-&gt;badblockpos &amp; 0xFE, page &amp; this-&gt;pagemask);<br>
  bad = cpu_to_le16(this-&gt;read_word(mtd));<br>
  if (this-&gt;badblockpos &amp; 0x1)<br>
   bad &gt;&gt;= 1;<br>
  if ((bad &amp; 0xFF) != 0xff)<br>
   res = 1;<br>
} else {<br>
  this-&gt;cmdfunc (mtd, NAND_CMD_READOOB, this-&gt;badblockpos, page &amp; this-&gt;pagemask);<br><span style="color: #0010ff;">/* 发送读oob_data命令(oob_data的badblockpos (第6)位记录着坏块标志) */</span>
<br>
  if (this-&gt;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-&gt;priv;<br>
u_char buf[2] = {0, 0};<br>
size_t retlen;<br>
int block;<br><br>
/* Get block number */<br>
block = ((int) ofs) &gt;&gt; this-&gt;bbt_erase_shift;<br>
if (this-&gt;bbt)<br>
  this-&gt;bbt[block &gt;&gt; 2] |= 0x01 &lt;&lt; ((block &amp; 0x03) &lt;&lt; 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-&gt;options &amp; 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-&gt;oobsize + (this-&gt;badblockpos &amp; ~0x01);//???????????????<br>
return nand_write_oob (mtd, ofs , 2, &amp;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-&gt;priv;<br><br>
for (i=0; i&lt;len; i++)<br>
  if (buf[i] != readb(this-&gt;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-&gt;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-&gt;options &amp; NAND_IS_AND) {<br>
  /* Use the default pattern descriptors */<br>
  if (!this-&gt;bbt_td) { <br>
   this-&gt;bbt_td = &amp;bbt_main_descr;<br>
   this-&gt;bbt_md = &amp;bbt_mirror_descr;<br>
  } <br>
  this-&gt;options |= NAND_USE_FLASH_BBT;<br>
  return nand_scan_bbt (mtd, &amp;agand_flashbased);<br>
}<br><br><br>
/* Is a flash based bad block table requested ? */<br>
if (this-&gt;options &amp; NAND_USE_FLASH_BBT) {<br>
  /* Use the default pattern descriptors */ <br>
  if (!this-&gt;bbt_td) { <br>
   this-&gt;bbt_td = &amp;bbt_main_descr;<br>
   this-&gt;bbt_md = &amp;bbt_mirror_descr;<br>
  }<br>
  if (!this-&gt;badblock_pattern) {<br>
   this-&gt;badblock_pattern = (mtd-&gt;oobblock &gt; 512) ?<br>
    &amp;largepage_flashbased : &amp;smallpage_flashbased;<br>
  }<br>
} else {   <span style="color: #2000ff;">//samsun nand flash的坏块表不存在与nand flash里面,需要扫描来生成。</span>
<br>
  this-&gt;bbt_td = NULL;<br>
  this-&gt;bbt_md = NULL;<br>
  if (!this-&gt;badblock_pattern) {<br>
   this-&gt;badblock_pattern = (mtd-&gt;oobblock &gt; 512) ?<br>
    &amp;largepage_memorybased : &amp;smallpage_memorybased;<br>
  }<br>
}<br>
return nand_scan_bbt (mtd, this-&gt;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-&gt;priv;<br>
int len, res = 0;<br>
uint8_t *buf;<br>
struct nand_bbt_descr *td = this-&gt;bbt_td;<br>
struct nand_bbt_descr *md = this-&gt;bbt_md;<br><br>
len = mtd-&gt;size &gt;&gt; (this-&gt;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-&gt;bbt = kmalloc (len, GFP_KERNEL);<br>
if (!this-&gt;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-&gt;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-&gt;bbt);<br>
   this-&gt;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 &lt;&lt; this-&gt;bbt_erase_shift);<br>
len += (len &gt;&gt; this-&gt;page_shift) * mtd-&gt;oobsize;<br>
buf = kmalloc (len, GFP_KERNEL);<br>
if (!buf) {<br>
  printk (KERN_ERR "nand_bbt: Out of memory/n");<br>
  kfree (this-&gt;bbt);<br>
  this-&gt;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-&gt;options &amp; 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-&gt;priv;<br><br>
bd-&gt;options &amp;= ~NAND_BBT_SCANEMPTY;<br><span style="color: #0010ff;">//我们只需要扫描oob data,不需要扫描全部(512+16bytes的数据)</span>
<br>
return create_bbt (mtd, this-&gt;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-&gt;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-&gt;options &amp; NAND_BBT_SCANALLPAGES)<span style="color: #0010ff;">//扫描所有都页</span>
<br>
  len = 1 &lt;&lt; (this-&gt;bbt_erase_shift - this-&gt;page_shift);<span style="color: #0010ff;">//求出每block所含的page数</span>
<br>
else {<br>
  if (bd-&gt;options &amp; 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-&gt;options &amp; 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-&gt;len;<br>
} else {<br>
  /* Full page content should be read */<br><span style="color: #0010ff;">/* 读取整页内容 */</span>
<br>
  scanlen = mtd-&gt;oobblock + mtd-&gt;oobsize;<br>
  readlen = len * mtd-&gt;oobblock;<br>
  ooblen = len * mtd-&gt;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-&gt;size &gt;&gt; (this-&gt;bbt_erase_shift - 1);<br>
  startblock = 0;<br>
  from = 0;<br>
} else {<br>
  if (chip &gt;= this-&gt;numchips) {<br>
   printk (KERN_WARNING "create_bbt(): chipnr (%d) &gt; available chips (%d)/n",<br>
    chip + 1, this-&gt;numchips);<br>
   return -EINVAL;<br>
  }<br>
  numblocks = this-&gt;chipsize &gt;&gt; (this-&gt;bbt_erase_shift - 1);<br>
  startblock = chip * numblocks;<br>
  numblocks += startblock;<br>
  from = startblock &lt;&lt; (this-&gt;bbt_erase_shift - 1);<br>
}<br><br>
for (i = startblock; i &lt; numblocks;) {<br>
  int ret;<br><br>
  if (bd-&gt;options &amp; 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 &lt; len; j++) {<br>
   if (!(bd-&gt;options &amp; 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-&gt;read_oob(mtd, from + j * mtd-&gt;oobblock,<br>
       mtd-&gt;oobsize, &amp;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-&gt;bbt[i &gt;&gt; 3] |= 0x03 &lt;&lt; (i &amp; 0x6);<br><span style="color: #0010ff;">/* 注意:这里i=实际值*2。由于一个block的状态用2bit来表示,那么一个字节可以存放4个block的状态。</span>
<br><span style="color: #0010ff;">这里i&gt;&gt;3刚好是实际block/4,4个block的状态刚好存放在this-&gt;bbt所指向的一个字节里面 </span>
<br style="color: #0010ff;"><span style="color: #0010ff;">    */</span>
<br>
     printk (KERN_WARNING "Bad eraseblock %d at 0x%08x/n", <br>
      i &gt;&gt; 1, (unsigned int) from);<br>
     break;<br>
    }<br>
   } else {<br>
    if (check_pattern (&amp;buf[j * scanlen], scanlen, mtd-&gt;oobblock, bd)) {<br>
     this-&gt;bbt[i &gt;&gt; 3] |= 0x03 &lt;&lt; (i &amp; 0x6);<br>
     printk (KERN_WARNING "Bad eraseblock %d at 0x%08x/n", <br>
      i &gt;&gt; 1, (unsigned int) from);<br>
     break;<br>
    }<br>
   }<br>
  }<br>
  i += 2;<span style="color: #0010ff;">//更新block的序号</span>
<br>
  from += (1 &lt;&lt; this-&gt;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-&gt;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-&gt;bbt)<br>
  kfree (this-&gt;bbt);<br>
/* Buffer allocated by nand_scan ? */<br>
if (this-&gt;options &amp; NAND_OOBBUF_ALLOC)<br>
  kfree (this-&gt;oob_buf);<br>
/* Buffer allocated by nand_scan ? */<br>
if (this-&gt;options &amp; NAND_DATABUF_ALLOC)<br>
  kfree (this-&gt;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 :&amp;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=&amp;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>
引用自&lt;&lt;Linux系统移植&gt;&gt;一文:<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, &amp;mtd-&gt;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-&gt;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 &lt;&lt; (this-&gt;phys_erase_shift - this-&gt;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) &gt; mtd-&gt;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 &amp; 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-&gt;useecc == MTD_NANDECC_AUTOPLACE)<br>
  oobsel = this-&gt;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(&amp;mtd-&gt;oobinfo, this-&gt;autooob, sizeof(mtd-&gt;oobinfo));</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> * 把this-&gt;autooob的内容拷贝到mtd-&gt;oobinfo中了</span>
<br style="color: #0010ff;"><span style="color: #0010ff;"> */</span>
<br><br>
eccmode = oobsel-&gt;useecc ? this-&gt;eccmode : NAND_ECC_NONE;<br>
oob_config = oobsel-&gt;eccpos;<span style="color: #0010ff;">//记录ecc在oob数据中的位置</span>
<br><br>
/* Select the NAND device */<br>
chipnr = (int)(from &gt;&gt; this-&gt;chip_shift);<br>
this-&gt;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 &gt;&gt; this-&gt;page_shift);<br>
page = realpage &amp; this-&gt;pagemask;<br><br>
/* Get raw starting column */<br><span style="color: #0010ff;"> /* 其次,我们计算页内偏址 */</span>
<br>
col = from &amp; (mtd-&gt;oobblock - 1);<br><br>
end = mtd-&gt;oobblock;<span style="color: #0010ff;">//页大小(512)</span>
<br>
ecc = this-&gt;eccsize;<span style="color: #0010ff;">//ecc保护下的数据大小(256)</span>
<br>
eccbytes = this-&gt;eccbytes;<span style="color: #0010ff;">//ecc所占的字节数(3)</span>
<br><br>
if ((eccmode == NAND_ECC_NONE) || (this-&gt;options &amp; NAND_HWECC_SYNDROME))<br>
  compareecc = 0;<span style="color: #0010ff;">//如果设置为关闭ECC或写操作才需要ECC,那把ecc给禁用(现在可是读操作^_^)</span>
<br><br>
oobreadlen = mtd-&gt;oobsize;//16<br>
if (this-&gt;options &amp; NAND_HWECC_SYNDROME) <br>
  oobreadlen -= oobsel-&gt;eccbytes;<br><br>
/* Loop until all data read */<br>
while (read &lt; len) {<br><br>
  int aligned = (!col &amp;&amp; (len - read) &gt;= 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 = &amp;buf[read];<br>
  else <br>
   data_poi = this-&gt;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-&gt;pagebuf &amp;&amp; !oob_buf) {<br>
   /* aligned read ? */<br>
   if (aligned)<br>
    memcpy (data_poi, this-&gt;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-&gt;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-&gt;useecc == MTD_NANDECC_AUTOPLACE ||<br>
   oobsel-&gt;useecc == MTD_NANDECC_AUTOPL_USR)<br>
   oob_data = &amp;this-&gt;data_buf[end];<span style="color: #0010ff;">//以上情况,oob_data暂存在data_buf缓存中</span>
<br><br>
  eccsteps = this-&gt;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-&gt;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-&gt;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-&gt;calculate_ecc(mtd, &amp;data_poi[datidx], &amp;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-&gt;enable_hwecc(mtd, NAND_ECC_READ);<br>
    this-&gt;read_buf(mtd, &amp;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-&gt;enable_hwecc(mtd, NAND_ECC_READSYN);<br>
     this-&gt;read_buf(mtd, &amp;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-&gt;correct_data(mtd, &amp;data_poi[datidx], &amp;oob_data[i], &amp;ecc_code[i]);<br>
     if ((ecc_status == -1) || (ecc_status &gt; (flags &amp;&amp; 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-&gt;calculate_ecc(mtd, &amp;data_poi[datidx], &amp;ecc_calc[i]);<br>
    } <br>
   }<br>
   break;      <br>
  }<br><br>
  /* read oobdata */<br>
  this-&gt;read_buf(mtd, &amp;oob_data[mtd-&gt;oobsize - oobreadlen], oobreadlen);<br><span style="color: #0010ff;">//读取oob_data存放到oob_data[mtd-&gt;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 &lt; oobsel-&gt;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 &lt; this-&gt;eccsteps; i++, datidx += ecc) {<br>
   ecc_status = this-&gt;correct_data(mtd, &amp;data_poi[datidx], &amp;ecc_code[j], &amp;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 &amp;&amp; oobsel-&gt;useecc == MTD_NANDECC_PLACE) { <br>
    int *p = (int *)(&amp;oob_data[mtd-&gt;oobsize]);<br>
    p[i] = ecc_status;<br>
   }<br><span style="color: #0010ff;">/* 很不幸,ecc检测发现错误且未能修复,报告错误 */</span>
<br>
   if ((ecc_status == -1) || (ecc_status &gt; (flags &amp;&amp; 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-&gt;useecc) {<br>
   case MTD_NANDECC_AUTOPLACE:<br>
   case MTD_NANDECC_AUTOPL_USR:<br>
    /* Walk through the autoplace chunks */<br>
    for (i = 0; oobsel-&gt;oobfree[i][1]; i++) {<br>
     int from = oobsel-&gt;oobfree[i][0];<br>
     int num = oobsel-&gt;oobfree[i][1];<br>
     memcpy(&amp;oob_buf[oob], &amp;oob_data[from], num);<br>
     oob += num;<br>
    }<br>
    break;<br>
   case MTD_NANDECC_PLACE:<br>
    /* YAFFS1 legacy mode */<br>
    oob_data += this-&gt;eccsteps * sizeof (int);<br>
   default:<br>
    oob_data += mtd-&gt;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 &lt; end &amp;&amp; read &lt; len; j++)<br>
    buf[read++] = data_poi[j];<span style="color: #0010ff;">//read自增</span>
<br>
   this-&gt;pagebuf = realpage; <br>
  } else  <br>
   read += mtd-&gt;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-&gt;dev_ready) <br>
   udelay (this-&gt;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 &amp; this-&gt;pagemask;<br>
  /* Check, if we cross a chip boundary */<br>
  if (!page) {<br>
   chipnr++;<br>
   this-&gt;select_chip(mtd, -1);<br>
   this-&gt;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 &amp; blockcheck))<br>
   sndcmd = 1;    <br>
}<br><br>
/* Deselect and wake up anyone waiting on the device */<br>
if (flags &amp; 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>
引用自&lt;&lt;Linux系统移植&gt;&gt;一文:<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-&gt;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 &lt;&lt; (this-&gt;phys_erase_shift - this-&gt;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) &gt; mtd-&gt;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 &gt;&gt; this-&gt;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-&gt;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 = &amp;mtd-&gt;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-&gt;useecc == MTD_NANDECC_AUTOPLACE) {</span>
<br style="color: #040000;"><span style="color: #040000;">  oobsel = this-&gt;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-&gt;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 &gt;&gt; this-&gt;page_shift;<span style="color: #0010ff;">//计算所要读取的数据长度共有多少页</span>
</span>
<br style="color: #040000;"><span style="color: #040000;"> page = (int) (to &gt;&gt; this-&gt;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 &lt;= this-&gt;pagebuf &amp;&amp; this-&gt;pagebuf &lt; (page + totalpages)) </span>
<br style="color: #040000;"><span style="color: #040000;">  this-&gt;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 &amp;= this-&gt;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 &amp; (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 &lt; len) {</span>
<br style="color: #040000;"><br style="color: #040000;"><span style="color: #040000;">  this-&gt;data_poi = (u_char*) &amp;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, &amp;oobbuf[oob], oobsel, (--numpages &gt; 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-&gt;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-&gt;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 &amp; (ppblock - 1))){</span>
<br style="color: #040000;"><span style="color: #040000;">   int ofs;</span>
<br style="color: #040000;"><span style="color: #040000;">   this-&gt;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-&gt;oobavail : mtd-&gt;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 &amp;= this-&gt;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-&gt;select_chip(mtd, -1);</span>
<br style="color: #040000;"><span style="color: #040000;">    this-&gt;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-&gt;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 &amp; this-&gt;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-&gt;useecc ? this-&gt;eccmode : NAND_ECC_NONE;<br>
int  *oob_config = oobsel-&gt;eccpos;<br>
int datidx = 0, eccidx = 0, eccsteps = this-&gt;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-&gt;2.6Mib/s)<br>
*/<br></span>
<br>
/* Send command to begin auto page programming */<br><span style="color: #0010ff;">/* 发送页编程指令 */</span>
<br>
this-&gt;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-&gt;write_buf(mtd, this-&gt;data_poi, mtd-&gt;oobblock);<br>
  break;<br><br>
/* Software ecc 3/256, write all */<br>
case NAND_ECC_SOFT:<br>
  for (; eccsteps; eccsteps--) {<br>
   this-&gt;calculate_ecc(mtd, &amp;this-&gt;data_poi[datidx], ecc_code);<span style="color: #0010ff;">//计算出一页的ecc数据</span>
<br>
   for (i = 0; i &lt; 3; i++, eccidx++)<br>
    oob_buf[oob_config[eccidx]] = ecc_code[i];<span style="color: #0010ff;">//存放到ecc_code数组中</span>
<br>
   datidx += this-&gt;eccsize;<br>
  }<br>
  this-&gt;write_buf(mtd, this-&gt;data_poi, mtd-&gt;oobblock);<span style="color: #0010ff;">//调用FLASH硬件驱动层进行写操作</span>
<br>
  break;<br>
default:<br>
  eccbytes = this-&gt;eccbytes;<br>
  for (; eccsteps; eccsteps--) {<br>
   /* enable hardware ecc logic for write */<br>
   this-&gt;enable_hwecc(mtd, NAND_ECC_WRITE);<br>
   this-&gt;write_buf(mtd, &amp;this-&gt;data_poi[datidx], this-&gt;eccsize);<br>
   this-&gt;calculate_ecc(mtd, &amp;this-&gt;data_poi[datidx], ecc_code);<br>
   for (i = 0; i &lt; 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-&gt;options &amp; NAND_HWECC_SYNDROME)<br>
    this-&gt;write_buf(mtd, ecc_code, eccbytes);<br>
   datidx += this-&gt;eccsize;<br>
  }<br>
  break;<br>
}<br><br>
/* Write out OOB data */<br>
if (this-&gt;options &amp; NAND_HWECC_SYNDROME)<br>
  this-&gt;write_buf(mtd, &amp;oob_buf[oobsel-&gt;eccbytes], mtd-&gt;oobsize - oobsel-&gt;eccbytes);<br>
else <br>
  this-&gt;write_buf(mtd, oob_buf, mtd-&gt;oobsize);<span style="color: #0010ff;">//写oob data,主要把上面计算的ecc值写进去</span>
<br><br>
/* Send command to actually program the data */<br>
this-&gt;cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);<br><br>
if (!cached) {<br>
  /* call wait ready function */<br>
  status = this-&gt;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 &amp; NAND_STATUS_FAIL) &amp;&amp; (this-&gt;errstat)) {<br>
   status = this-&gt;errstat(mtd, this, FL_WRITING, status, page);<br>
  }<br><br>
  /* See if device thinks it succeeded */<br>
  if (status &amp; 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-&gt;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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics