前段时间,研究了一下4G的Nandflash驱动。手头上只有飞凌6410BSP自带的Nandflash驱动,该驱动不支持K9GAG08U0D(2G)和K9LBG08U0D(4G)的Nandflash。所以就要先把这个Nandflash驱动搞成支持K9GAG08U0D(2G)的。

接下来要弄的就是支持K9LBG08U0D(4G)的Nandflash。由于TE6410板子用的是K9GAG08U0D(2G)的Nandflash,所以先移植到支持这个Nandflash的,再进一步修改成支持K9LBG08U0D的。


移植之前,先让我们来对比下K9GAG08U0D(2G)和K9LBG08U0D(4G)两个Nandflash:

K9GAG08U0D(2G):

• Organization

   - Memory Cell Array : (2G + 109M) x 8bit

   - Data Register        : (4K + 218) x 8bit  

 • Automatic Program and Erase

   - Page Program : (4K + 218)Byte

   - Block Erase     : (512K + 27.25K)Byte

 • Page Read Operation

   - Page Size : (4K + 218)Byte

   - Random Read : 60 µ s(Max.)

   - Serial  Access : 30ns(Min.)

 • Memory Cell : 2bit / Memory Cell


K9LBG08U0D(4G):

Organization

   - Memory Cell Array : (2G + 109M) x 8bit(文档这里是写的2G,也不知道是不是三星那个写文档的人搞错了,明明是4G,怎么写的是2G)

   - Data Register        : (4K + 218) x 8bit  

 • Automatic Program and Erase

   - Page Program : (4K + 218)Byte

   - Block Erase     : (512K + 27.25K)Byte

 • Page Read Operation

   - Page Size : (4K + 218)Byte

   - Random Read : 60 µ s(Max.)

   - Serial  Access : 30ns(Min.)

    *K9MDG08U5D: 50ns(Min.)

 • Memory Cell : 2bit / Memory Cell


两个Nandflash都是4K 页 218备用区的,这样就更好办了。而且这两个都可以直接使用8bit的ECC,刚好6410最大就能支持到8bit的ECC校验。


咱们再来看一下这个8BIT 的ECC校验流程:


现在暂时到这里,下一篇将介绍6410中的NFCON控制寄存器部分。


这里将会介绍一下S3C6410CPU中的NFCON,Nandflash控制寄存器


8.1 OVERVIEW 

 Recently NOR flash memory price has increased and pr ice for SDRAM and a NAND flash memory is moderatly placed.    

 The 6410 is equipped with an internal SRAM buffer called ‘Steppingstone’. 

 Generally, the boot code will copy NAND flash content to SDRAM. Using hardware  ECC, the NAND flash data 

 validity will be checked. After the NAND flash content  is copied to SDRAM, main program will be executed on SDRAM. 

 To use NAND Flash, 'XSELNAND' pin must be connected to High Level. 


 8.2 FEATURES 

 NAND flash controller features include: 


 1.  NAND Flash memory I/F: Support 512Bytes and 2KB Page  . 

 2.  Software mode: User can directly access NAND flash memory.  for example this feature can be used in read/erase/program NAND flash memory. 

 3.  Interface: 8-bit NAND flash memory interface bus. 

 4.  Hardware ECC generation, detection and indication (Software correction). 

硬件产生ECC,软件矫正

 5.  Support both SLC and MLC NAND flash memory : 1-bit ECC, 4-bit and 8-bit ECC for NAND flash. 

       (Recommend: 1bit ECC for SLC, 4bit and 8bit ECC for MLC NAND Flash) 

同时支持SLC和MLC的Nandflash:1bit ECC用于SLC Nandflash, 4bit和8bit ECC用于MLC Nandflash

 6.  SFR I/F: Support Byte/half word/word access to Data and ECC Data register, and Word access to other  registers 

 7.  SteppingStone I/F: Support Byte/half word/word access. 

 8.  The Steppingstone 8-KB internal SRAM  buffer can be used for another purpose  . 


其他的这里就不废话了,大家看回文档吧。

其中要注意的地方:

Both 4bit and 8bit ECC modules can be used for only 512 bytes ECC parity code generation.

4bit和8bit的ECC模块同时能够用于产生每读写512字节数据对应的ECC校验值。


 4 bit and 8bit ECC modules generate the parity codes for each 512 byte. However, 1 bit ECC modules generate 

 parity code per byte lane separately. 


 4bit ECC modules generate max 7byte parity codes and 8  bit ECC modules generate 13byte parity codes at each 

 512/24 bytes. 

在每读写512或24字节的数据时,4bit的ECC模块产生最多7个字节的ECC校验码,而8bit的ECC模块产生最多13字节的校验码


下面直接来看8bit ECC的编解码:

8.8.5 8-BIT ECC PROGRAMMING GUIDE (ENCODING) 

  1.  To use 8-bit ECC in software mode, set the Ms gLength to 0(512-byte message length) and set the ECCType 

 to “01”(enable 8-bit ECC). ECC module generates ECC parit y code for 512-byte write data. In order to start 

 the ECC module, you have to write ‘1’ on the Init MECC (NFCONT[5]) bit after cleaning the MainECCLock 

 (NFCONT[7]) bit to ‘0’ (Unlock). MainECCLock (NFCONT[7]) bit controls whether ECC Parity code is 

 generated or not. 

 Note. In 8bit ECC, MainECCLock should be cleared before initiating InitMECC. 

 2.  Whenever data is written, the 8bit ECC module generates ECC parity code internally. 

 3.  After you finish writing 512-byte  data (not include spare area data), the parity codes are automatically updated 

 to NF8MECC0, NFMECC1, NF8MECC2, NF8MECC3 regi ster. You have to check encoding done at NFSTAT 

 register. And set the MainECCLock bit to ‘1’(lock).   If you use 512-byte page size NAND flash memory, you 

 can program these values directly to spare area.  However, if you use NAND flash memory more than 512-

 byte page, you can’t program immediately. In this case, you have to copy these par ity codes to other memory 

 like DRAM. After writing all main data, you c an write the copied ECC values to spare area. 

 The parity codes have self-correctable information include parity code itself. 

 4.  To generate spare area ECC parity code, set the MsgLength to 1(24-byte message length), and set the 

 ECCType to “01”(enable 8bit ECC). 8bit ECC module generates the ECC parity code for 24-byte data. In 

 order to initiating the module, you have to write ‘1 ’ on the InitMECC (NFCONT[5]) bit after clearing the

MainECCLock (NFCONT[7]) bit to ‘0’(Unlock).  

 MainECCLock (NFCONT[7]) bit controls whether ECC Parity code is generated or not. 

 Note. In 8bit ECC, MainECCLock should be cleared before initiating InitMECC. 

 5.  Whenever data is written, the 8bit ECC module generates ECC parity code internally. 

 6.  When you finish writing 24-byte meta or extra data, the parity codes are automatically updated to 

 NF8MECC0, NFMECC1, NF8MECC2, NF8MECC3 register. you have to check encoding done at NFSTAT 

 register. And set the MainECCLock bit to ‘1’(lock) .  You can program these parity codes to spare area. The 

 parity codes have self-correctable information include parity code itself. 

部分译文:(仅供参考)


8bit ECC编码:

1、要用8bit的ECC,需要设置MsgLength 为0(512-byte message length) 和设置ECCTypet为 “01”(enable 8bit ECC)。8bit ECC模块会在读取512字节主数据之后产生对应的ECC校验码。要8bit的ECC模块开始工作,你必须在设置InitMECC (NFCONT[5])为1之前把MainECCLock (NFCONT[7]) 清0进行解锁。

MainECCLock (NFCONT[7]) 位控制着是否要产生对应的ECC校验码。

注意:在8bit ECC中,MainECCLock必须先清0,然后在对InitMECC置1

2、数据一旦写完,MLC的ECC模块就会产生对应的ECC校验码。

3、当你完成了512字节的主数据的写操作(不包括备用区数据),对应的ECC校验码将会自动的更新到NFM8ECC0, NFM8ECC1, NFM8ECC2, NFM8ECC3寄存器中。你必须在NFSTAT寄存器中检测编码是否完成(在这里应该是NFSTAT[7],不知道为什么文档中的NFSTAT寄存器中的7bit为保留位,而三星的MLC bsp中却有用到这个位,这个情况大家可以推敲下)。同时,设置MainECCLock 为1加锁。如果你用的是512字节页大小的Nandflash存储,你可以直接把这些产生的ECC校验码写到备用区中。如果你使用的Nandflash存储超过了512字节的页大小,你不能够直接把产生的ECC校验码写到备用区中。在这种情况,你必须把每写512字节主数据产生的ECC校验码拷贝到其他存储器中,如DRAM。当写完所有的主数据(即一页大小的主数据),你可以把拷贝到其他存储器的ECC校验码写到备用区中。这个校验码带有自身矫正的信息和校验码值。

剩下的ECC读写基本雷同,这里就不翻译了。



8.8.6 8-BIT ECC PROGRAMMING GUIDE (DECODING) 

 1.  To use 8bit ECC in software mode, set the MsgLength to 0(512-byte message length) and set the ECCType

 to “01”(enable 8bit ECC). 8bit ECC module generates E CC parity code for 512-byte read data. In order to 

 initiating 8bit ECC module, you have to write ‘1’ on the InitMECC (NFCONT[5]) bit after clearing the 

 MainECCLock (NFCONT[7]) bit to ‘0’(Unlock).  

 MainECCLock (NFCONT[7]) bit controls whether ECC Parity code is generated or not. 

 Note. In 8bit ECC, MainECCLock should be cleared before InitMECC 

 2.  Whenever data is read, the MLC ECC m odule generates ECC parity code internally. 

 3.  After you complete reading 512-byte data (not including spare area data), you must set the MainECCLock 

 (NFCONT[7]) bit to ‘1’(Lock) after reading parity  codes. 8bit ECC module needs parity codes to detect 

 whether error bits exists or not. So you have to read the ECC parity code of 512-byte main data right after 

 reading the 512-byte data. Once the ECC parity code is  read, 8bit ECC engine starts searching any error 

 internally. 8bit ECC error searching engine needs mini mum 372 cycles to find any error. And set the 

 MainECCLock bit to ‘1’(lock). ECCDecDone(NFSTAT[6]) can be used to check whether ECC decoding is 

 completed or not.  

 4.  When ECCDecDone (NFSTAT[6]) is set (‘1’), NF8ECCERR0 indicates whether error bit exists or not. If any 

 error exists, you can fix it by referencing NF8ECCERR0/1/2 and NFMLC8BITPT0/1 register. 

 5.  If you have more main data to read, continue doing from step 1. 

 6.  For meta data error check, set the MsgLength to 1(24-byte message length) and set the ECCType to 

 “01”(enable 8bit ECC). ECC module generates the ECC parity  code for 24-byte data. In order to initiating the

 8bit ECC module, you have to write ‘1’ on the InitMECC (NFCONT[5]) bit after clearing the MainECCLock 

 (NFCONT[7]) bit to ‘0’(Unlock).  

 MainECCLock (NFCONT[7]) bit controls whether ECC Parity code is generated or not. 

 Note. In 8bit ECC, MainECCLock should be cleared before InitMECC 

 7.  Whenever data is read, the 8bit ECC module generates ECC parity code internally. 

8.  After you complete reading 24-byte, you must set the MainECCLock (NFCONT[7]) bit to ‘1’(Lock) after read 

 ing the parity code for 24-byte data. MLC ECC module needs parity codes to detect w hether error bits exists 

 or not. So you have to read ECC parity codes right after reading 24-byte data. Once ECC parity code is read, 

 8bit ECC engine starts searching any error internally. 8bit ECC error searching engine needs minimum 372 

 cycles to find any error. And set the MainECCLock  bit to ‘1’(lock). ECCDecDone(NFSTAT[6]) can be used to 

 check whether ECC decoding is completed or not.    

 9.  When ECCDecDone (NFSTAT[6]) is set (‘1’), NF8ECCERR0 indicates whether error bit exist or not. If any 

error exists, you can fix it by referencing NF8ECCERR0/1/2 and NF8MLCBITPT register. 

部分译文:(仅供参考)


8bit ECC译码:

1、要用8bit的ECC,需要设置MsgLength 为0(512-byte message length) 和设置ECCTypet为 “01”(enable 8bit ECC)。8bit ECC模块会在读取512字节主数据之后产生对应的ECC校验码。要8bit的ECC模块开始工作,你必须在设置InitMECC (NFCONT[5])为1之前把MainECCLock (NFCONT[7]) 清0进行解锁。

MainECCLock (NFCONT[7]) 位控制着是否要产生对应的ECC校验码。

注意:在8bit ECC中,MainECCLock必须先清0,然后在对InitMECC置1

2、数据一旦被读取,MLC的ECC模块就会产生对应的ECC校验码。

3、在完成读取512字节的主数据后(不包括备用区数据),读取对应的主数据区的校验码,然后必须设置MainECCLock(NFCONT[7])位为1,锁住ECC的产生。8bit 的ECC模块需要这个校验码来确定是否有错误位的存在。所以你必须在读取完512字节的主数据之后,把对应的主数据区的ECC校验码读取出来。一旦主数据区对应的ECC校验码被读,8bit的ECC引擎就会开始查找错误。8bit的ECC错误查找引擎需要最少372个周期去查找存在的错误。同时设置MainECCLock位为1,加锁。ECCDecDone(NFSTAT[6]) 可以用于检测ECC译码是否完成。

4、当ECCDecDone (NFSTAT[6])设置为1,NF8ECCERR0会指出是否有错误位的存在。如果有错误存在,你可以用NF8ECCERR0/1/2和NFMLC8BITPT0/1去修正相应的错误。

5、如果你有更多的主数据要读取,则从第一步开始重复操作。



PS: 原文中有些寄存器是写错的,大家在看的时候要注意。

吐槽一下,三星这个文档实在是简陋的可以,而且错误的地方还很多,想不BS一下都不成。


在这里有个地方要注意的是:

在写数据到Nandflash的时候,每写512字节就会产生一次ECC,我们这里用的是4K页,大于512字节,所以必须把产生的ECC校验值保存起来,等到一页(4k)写完,再把这些ECC写入到备用区中。

而在读取数据的时候,却是每读取512字节的主数据,就会产生一次ECC,这时就要进行ECC校验、矫正了。和写的时候不大一样,需要注意这一点。


寄存器部分:

红色框住是主要使用的寄存器和8bit ECC所用到的寄存器。


主要用到的寄存器就到这了,下一篇将开始代码部分。

在飞凌提供的BSP中,Nandflash采用的是FMD+PDD的结构,PDD主要是应对上层的接口,这里我们不需要修改,直接修改FMD就好。(至于在网上看到很多人说这个结构理论上不支持MLC的Nandflash,这个暂且不说).

FMD部分的驱动源码在 C:\WINCE600\PLATFORM\SMDK6410\src\common\nandflash\FMD\ 这个目录下边。

FMD的目录结构:

nand.s

cfnand.h

fmd_LB.h(实际并没用到)

fmd_SB.h(实际并没用到)

nand.h

fmd.cpp

sources

makefile



先看sources文件:


[cpp]​view plain​​​​copy​


  1. !if 0
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. !endif
  4. !if 0
  5. Use of this sample source code is subject to the terms of the Microsoft
  6. license agreement under which you licensed this sample source code. If
  7. you did not accept the terms of the license agreement, you are not
  8. authorized to use this sample source code. For the terms of the license,
  9. please see the license agreement between you and Microsoft or, if applicable,
  10. see the LICENSE.RTF on your install media or the root of your tools installation.
  11. THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
  12. !endif
  13. !IF 0


  14. Module Name:

  15. sources.

  16. Abstract:

  17. This file specifies the target component being built and the list of
  18. sources files needed to build that component. Also specifies optional
  19. compiler switches and libraries that are unique for the component being
  20. built.

  21. !ENDIF

  22. TARGETNAME=nandflash_lib11
  23. TARGETTYPE=LIBRARY
  24. RELEASETYPE=PLATFORM
  25. SYNCHRONIZE_BLOCK=1

  26. WINCEOEM=1
  27. WINCECPU=1
  28. NOMIPS16CODE=1

  29. ADEFINES=-pd "_TGTCPU SETS \"$(_TGTCPU)\"" $(ADEFINES)
  30. LDEFINES=-subsystem:native /DEBUG /DEBUGTYPE:CV /FIXED:NO

  31. INCLUDES=$(INCLUDES)

  32. SOURCES=\
  33. fmd.cpp

  34. ARM_SOURCES=\
  35. nand.s



这个代码是飞凌提供的,且看这一句 TARGETNAME=nandflash_lib11,经查实,FMD+PDD使用的是FMD产生的nandflash_lib.lib库文件,而不是nandflash_lib11.lib文件,这也说明飞凌在这方面留了一手,并没有直接提供可用的源码,而是给了一个lib库给我们。


在这里,我们需要把 TARGETNAME=nandflash_lib11 修改为 TARGETNAME=nandflash_lib


cfnand.h文件:


[cpp]​view plain​​​​copy​


  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5. PARTICULAR PURPOSE.
  6. Copyright (c) 2001 Microsoft Corporation

  7. Module Name: S3C6410.H

  8. Abstract: FLASH Media Driver Interface Samsung S3C6410 CPU with NAND Flash
  9. controller.

  10. Environment: As noted, this media driver works on behalf of the FAL to directly
  11. access the underlying FLASH hardware. Consquently, this module
  12. needs to be linked with FLASHFAL.LIB to produce the device driver
  13. named FLASHDRV.DLL.

  14. -----------------------------------------------------------------------------*/
  15. #ifndef _S3C6410_CFNAND_H
  16. #define _S3C6410_CFNAND_H

  17. #include "FMD_LB.h"
  18. #include "FMD_SB.h"
  19. #include "nand.h"

  20. #define BW_X08 (0)
  21. #define BW_X16 (1)
  22. #define BW_X32 (2)

  23. #define MAX_SECTORS_PER_PAGE (8)

  24. /*****************************************************************************/
  25. /* S3C6410 Nand Flash Internal Data Structure Definition */
  26. /*****************************************************************************/
  27. typedef struct
  28. {
  29. UINT16 nMID; /* Manufacturer ID */
  30. UINT16 nDID; /* Device ID */

  31. UINT16 nNumOfBlks; /* Number of Blocks */
  32. UINT16 nPgsPerBlk; /* Number of Pages per block */
  33. UINT16 nSctsPerPg; /* Number of Sectors per page */
  34. UINT16 nNumOfPlanes; /* Number of Planes */
  35. UINT16 nBlksInRsv; /* The Number of Blocks in Reservior for Bad Blocks */
  36. UINT8 nBadPos; /* BadBlock Information Poisition*/
  37. UINT8 nLsnPos; /* LSN Position */
  38. UINT8 nECCPos; /* ECC Policy : HW_ECC, SW_ECC */
  39. UINT16 nBWidth; /* Nand Organization X8 or X16 */

  40. UINT16 nTrTime; /* Typical Read Op Time */
  41. UINT16 nTwTime; /* Typical Write Op Time */
  42. UINT16 nTeTime; /* Typical Erase Op Time */
  43. UINT16 nTfTime; /* Typical Transfer Op Time */
  44. } FlashDevSpec;

  45. static FlashDevSpec astNandSpec[] = {
  46. /*************************************************************************/
  47. /* nMID, nDID, */
  48. /* nNumOfBlks */
  49. /* nPgsPerBlk */
  50. /* nSctsPerPg */
  51. /* nNumOfPlanes */
  52. /* nBlksInRsv */
  53. /* nBadPos */
  54. /* nLsnPos */
  55. /* nECCPos */
  56. /* nBWidth */
  57. /* nTrTime */
  58. /* nTwTime */
  59. /* nTeTime */
  60. /* nTfTime*/
  61. /*************************************************************************/
  62. /* 8Gbit DDP NAND Flash */
  63. //{ 0xEC, 0xD3, 8192, 64, 4, 2,160, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  64. { 0xEC, 0xD3, 4096, 128, 4, 2,160, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  65. /* 16Gbit DDP NAND Flash */
  66. { 0xEC, 0xD5, 4096, 128, 8, 2,160, 0, 2, 8, BW_X08, 50, 350, 2000, 50}, // 8192 gjl
  67. /* 4Gbit DDP NAND Flash */
  68. { 0xEC, 0xAC, 4096, 64, 4, 2, 80, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  69. { 0xEC, 0xDC, 4096, 64, 4, 2, 80, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  70. //{ 0xEC, 0xBC, 4096, 64, 4, 2, 80, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  71. //{ 0xEC, 0xCC, 4096, 64, 4, 2, 80, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  72. /* 2Gbit NAND Flash */
  73. { 0xEC, 0xAA, 2048, 64, 4, 1, 40, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  74. { 0xEC, 0xDA, 2048, 64, 4, 1, 40, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  75. //{ 0xEC, 0xBA, 2048, 64, 4, 1, 40, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  76. //{ 0xEC, 0xCA, 2048, 64, 4, 1, 40, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  77. /* 2Gbit DDP NAND Flash */
  78. { 0xEC, 0xDA, 2048, 64, 4, 2, 40, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  79. { 0xEC, 0xAA, 2048, 64, 4, 2, 40, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  80. //{ 0xEC, 0xBA, 2048, 64, 4, 2, 40, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  81. //{ 0xEC, 0xCA, 2048, 64, 4, 2, 40, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  82. /*1Gbit NAND Flash */
  83. { 0xEC, 0xA1, 1024, 64, 4, 1, 20, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  84. { 0xEC, 0xF1, 1024, 64, 4, 1, 20, 0, 2, 8, BW_X08, 50, 350, 2000, 50},
  85. //{ 0xEC, 0xB1, 1024, 64, 4, 1, 20, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  86. //{ 0xEC, 0xC1, 1024, 64, 4, 1, 20, 0, 2, 8, BW_X16, 50, 350, 2000, 50},
  87. /* 1Gbit NAND Flash */
  88. { 0xEC, 0x79, 8192, 32, 1, 4,120, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  89. { 0xEC, 0x78, 8192, 32, 1, 4,120, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  90. //{ 0xEC, 0x74, 8192, 32, 1, 4,120,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  91. //{ 0xEC, 0x72, 8192, 32, 1, 4,120,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  92. /* 512Mbit NAND Flash */
  93. { 0xEC, 0x76, 4096, 32, 1, 4, 70, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  94. { 0xEC, 0x36, 4096, 32, 1, 4, 70, 5, 0, 6, BW_X08, 50, 350, 2000, 50},

  95. /* 512Mbit XP Card */
  96. { 0x98, 0x76, 4096, 32, 1, 4, 70, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  97. { 0x98, 0x79, 4096, 32, 1, 4, 70, 5, 0, 6, BW_X08, 50, 350, 2000, 50},

  98. //{ 0xEC, 0x56, 4096, 32, 1, 4, 70,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  99. //{ 0xEC, 0x46, 4096, 32, 1, 4, 70,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  100. /* 256Mbit NAND Flash */
  101. { 0xEC, 0x75, 2048, 32, 1, 1, 35, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  102. { 0xEC, 0x35, 2048, 32, 1, 1, 35, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  103. //{ 0xEC, 0x55, 2048, 32, 1, 1, 35,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  104. //{ 0xEC, 0x45, 2048, 32, 1, 1, 35,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  105. /* 128Mbit NAND Flash */
  106. { 0xEC, 0x73, 1024, 32, 1, 1, 20, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  107. { 0xEC, 0x33, 1024, 32, 1, 1, 20, 5, 0, 6, BW_X08, 50, 350, 2000, 50},
  108. //{ 0xEC, 0x53, 1024, 32, 1, 1, 20,11, 0, 6, BW_X16, 50, 350, 2000, 50},
  109. //{ 0xEC, 0x43, 1024, 32, 1, 1, 20,11, 0, 6, BW_X16, 50, 350, 2000, 50},

  110. { 0x00, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  111. };

  112. #endif _S3C6410_CFNAND_H




且看



[cpp]​view plain​​​​copy​


  1. /* 16Gbit DDP NAND Flash */
  2. 0xEC, 0xD5, 4096, 128, 8, 2,160, 0, 2, 8, BW_X08, 50, 350, 2000, 50}

这里就是使用的2G(16/8 bit=2byte) Nandflash K9GAG08U0D的一些参数配置,其中 0xEC, 0xD5表示的是Nandflash的ID号,总共有4096个block,每个block有128个page,每个page有8个sector


要支持4G(K9LBG08U0D) 的话,则加上下面的配置:


[cpp]​view plain​​​​copy​


  1. /* 32Gbit DDP NAND Flash */
  2. { 0xEC, 0xD7, 8192, 128, 8, 1,160, 0, 2, 8, BW_X08, 50, 350, 2000, 50},




在看nand.h文件之前,我们先来看一下fmd.cpp中引用的#include "s3c6410_nand.h"头文件:(该文件在C:\WINCE600\PLATFORM\COMMON\SRC\SOC\S3C6410_SEC_V1\OAL\INC\s3c6410_nand.h)


[cpp]​view plain​​​​copy​


  1. //
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //
  4. //
  5. // Use of this source code is subject to the terms of the Microsoft end-user
  6. // license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
  7. // If you did not accept the terms of the EULA, you are not authorized to use
  8. // this source code. For a copy of the EULA, please see the LICENSE.RTF on your
  9. // install media.
  10. //
  11. //------------------------------------------------------------------------------
  12. //
  13. // Header: s3c6410_nand.h
  14. //
  15. // Defines the NAND controller CPU register layout and definitions.
  16. //
  17. #ifndef __S3C6410_NAND_H
  18. #define __S3C6410_NAND_H

  19. #if __cplusplus
  20. extern "C"
  21. {
  22. #endif


  23. //------------------------------------------------------------------------------
  24. // Type: S3C6410_NAND_REG
  25. //
  26. // NAND Flash controller register layout. This register bank is located
  27. // by the constant CPU_BASE_REG_XX_NAND in the configuration file
  28. // cpu_base_reg_cfg.h.
  29. //

  30. typedef struct
  31. {
  32. UINT32 NFCONF; //0x00 // configuration reg
  33. UINT32 NFCONT; //0x04
  34. UINT8 NFCMD; //0x08 // command set reg
  35. UINT8 d0[3];
  36. UINT8 NFADDR; //0x0C // address set reg
  37. UINT8 d1[3];
  38. UINT8 NFDATA; //0x10 // data reg
  39. UINT8 d2[3];
  40. UINT32 NFMECCD0; //0x14
  41. UINT32 NFMECCD1; //0x18
  42. UINT32 NFSECCD; //0x1C
  43. UINT32 NFSBLK; //0x20
  44. UINT32 NFEBLK; //0x24 // error correction code 2
  45. UINT32 NFSTAT; //0x28 // operation status reg
  46. UINT32 NFECCERR0; //0x2C
  47. UINT32 NFECCERR1; //0x30
  48. UINT32 NFMECC0; //0x34 // error correction code 0
  49. UINT32 NFMECC1; //0x38 // error correction code 1
  50. UINT32 NFSECC; //0x3C
  51. UINT32 NFMLCBITPT; //0x40
  52. } S3C6410_NAND_REG, *PS3C6410_NAND_REG;


  53. #if __cplusplus
  54. }
  55. #endif

  56. #endif




注意到了麽?这个跟文档中的寄存器少了8bit ECC寄存器部分。


晕菜了吧,飞凌的这个2G 256M的BSP的Nandflash源码既然是这个样子,为什么还在那里号称8bit的ECC,还说开放源码。

靠别人都行的话,母猪都会上树啦!咱还是自己搞吧!

在上面UINT32 NFMLCBITPT; //0x40 的后面添加以下代码:


[cpp]​view plain​​​​copy​


  1. UINT32 NF8ECCERR0; //0x44-- 8位ECC错误状态0寄存器
  2. UINT32 NF8ECCERR1; //0x48-- 8位ECC错误状态1寄存器
  3. UINT32 NF8ECCERR2; //0x4c-- 8位ECC错误状态2寄存器
  4. UINT32 NFM8ECC0; //0x50-- 生成8位ECC状态0寄存器
  5. UINT32 NFM8ECC1; //0x54-- 生成8位ECC状态1寄存器
  6. UINT32 NFM8ECC2; //0x58-- 生成8位ECC状态2寄存器
  7. UINT32 NFM8ECC3; //0x5c-- 生成8位ECC状态3寄存器
  8. UINT32 NFMLC8BITPT0; //0x60-- 8位ECC错误位模式寄存器0
  9. UINT32 NFMLC8BITPT1; //0x64-- 8位ECC错误位模式寄存器1




nand.h文件:


[cpp]​view plain​​​​copy​


  1. //
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //
  4. //
  5. // Use of this sample source code is subject to the terms of the Microsoft
  6. // license agreement under which you licensed this sample source code. If
  7. // you did not accept the terms of the license agreement, you are not
  8. // authorized to use this sample source code. For the terms of the license,
  9. // please see the license agreement between you and Microsoft or, if applicable,
  10. // see the LICENSE.RTF on your install media or the root of your tools installation.
  11. // THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
  12. //
  13. #ifndef __NAND_H__
  14. #define __NAND_H__

  15. //-----------------------------------------------------------------------------

  16. typedef struct
  17. {
  18. UINT16 nNumOfBlks;
  19. UINT16 nPagesPerBlk;
  20. UINT16 nSctsPerPage;
  21. } NANDDeviceInfo;

  22. NANDDeviceInfo stDeviceInfo;

  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. NANDDeviceInfo GetNandInfo(void);
  27. #ifdef __cplusplus
  28. }
  29. #endif

  30. #define NUM_OF_BLOCKS (stDeviceInfo.nNumOfBlks)
  31. #define PAGES_PER_BLOCK (stDeviceInfo.nPagesPerBlk)
  32. #define SECTORS_PER_PAGE (stDeviceInfo.nSctsPerPage)

  33. #undef SECTOR_SIZE
  34. #define SECTOR_SIZE (512)
  35. #define NAND_SECTOR_SIZE (SECTOR_SIZE*SECTORS_PER_PAGE)
  36. #define NAND_PAGE_SIZE (SECTOR_SIZE*SECTORS_PER_PAGE) //< Physical Page Size

  37. #define IS_LB ((SECTORS_PER_PAGE == 4)||(SECTORS_PER_PAGE == 8))

  38. //-----------------------------------------------------------------------------

  39. #define USE_NFCE (0)
  40. #define USE_GPIO (0)

  41. #define TACLS (NAND_TACLS)
  42. #define TWRPH0 (NAND_TWRPH0)
  43. #define TWRPH1 (NAND_TWRPH1)
  44. #define ECCType (23)

  45. //-----------------------------------------------------------------------------

  46. #define CMD_READID (0x90) // ReadID
  47. #define CMD_READ (0x00) // Read
  48. #define CMD_READ2 (0x50) // Read2
  49. #define CMD_READ3 (0x30) // Read3
  50. #define CMD_RESET (0xff) // Reset
  51. #define CMD_ERASE (0x60) // Erase phase 1
  52. #define CMD_ERASE2 (0xd0) // Erase phase 2
  53. #define CMD_WRITE (0x80) // Write phase 1
  54. #define CMD_WRITE2 (0x10) // Write phase 2
  55. #define CMD_STATUS (0x70) // STATUS
  56. #define CMD_RDI (0x85) // Random Data Input
  57. #define CMD_RDO (0x05) // Random Data Output
  58. #define CMD_RDO2 (0xE0) // Random Data Output

  59. #define BADBLOCKMARK (0x00)

  60. // Status bit pattern
  61. #define STATUS_READY (0x40) // Ready
  62. #define STATUS_ERROR (0x01) // Error
  63. #define STATUS_ILLACC (0x20) // Illigar Access

  64. #define NF_ECCERR0_ALL_FF 0x40000000
  65. #define NF_ECCERR0_ECC_READY 0x20000000

  66. //-----------------------------------------------------------------------------

  67. #define NF_CMD(cmd) {g_pNFConReg->NFCMD = (unsigned char)(cmd);}
  68. #define NF_ADDR(addr) {g_pNFConReg->NFADDR = (unsigned char)(addr);}

  69. #define NF_nFCE_L() {g_pNFConReg->NFCONT &= ~(1<<1);}
  70. #define NF_nFCE_H() {g_pNFConReg->NFCONT |= (1<<1);}

  71. #define NF_ECC_DIRECTION_IN() {g_pNFConReg->NFCONT &= ~(1<<18);}
  72. #define NF_ECC_DIRECTION_OUT() {g_pNFConReg->NFCONT |= (1<<18);}

  73. #define NF_ECC_8BIT_STOP() {g_pNFConReg->NFCONT |= (1<<12);} //stop the last encode or decode of 8bit mode.

  74. #define NF_RSTECC() {g_pNFConReg->NFCONT |= ((1<<5) | (1<<4));}


  75. #define NF_MSGLENGTH_512() {g_pNFConReg->NFCONF &= ~(1<<25);}
  76. #define NF_MSGLENGTH_24() {g_pNFConReg->NFCONF |= (1<<25);}
  77. #define NF_ECCTYPE_CLR (g_pNFConReg->NFCONF &= ~(3<<23))
  78. #define NF_ECCTYPE_1BIT() {NF_ECCTYPE_CLR;}
  79. #define NF_ECCTYPE_4BIT() {NF_ECCTYPE_CLR |= (1<<24);}
  80. #define NF_ECCTYPE_8BIT() {NF_ECCTYPE_CLR |= (1<<23);}

  81. #define NF_MECC_UnLock() {g_pNFConReg->NFCONT &= ~(1<<7);}
  82. #define NF_MECC_Lock() {g_pNFConReg->NFCONT |= (1<<7);}
  83. #define NF_SECC_UnLock() {g_pNFConReg->NFCONT &= ~(1<<6);}
  84. #define NF_SECC_Lock() {g_pNFConReg->NFCONT |= (1<<6);}

  85. #define NF_CLEAR_RB() {g_pNFConReg->NFSTAT |= (1<<4);} // Have write '1' to clear this bit.

  86. #define NF_DETECT_RB() {while((g_pNFConReg->NFSTAT&0x11)!=0x11);} // RnB_Transdetect & RnB
  87. #define NF_WAITRB() {while (!(g_pNFConReg->NFSTAT & (1<<0))) ; }

  88. #define NF_RDDATA_BYTE() (g_pNFConReg->NFDATA)
  89. #define NF_RDDATA_WORD() (*(UINT32 *)0xb0200010)

  90. #define NF_WRDATA_BYTE(data) {g_pNFConReg->NFDATA = (UINT8)(data);}
  91. #define NF_WRDATA_WORD(data) {*(UINT32 *)0xb0200010 = (UINT32)(data);}

  92. #define NF_RDMECC0() (g_pNFConReg->NFMECC0)
  93. #define NF_RDMECC1() (g_pNFConReg->NFMECC1)
  94. #define NF_RDSECC() (g_pNFConReg->NFSECC)

  95. #define NF_RDM8ECC0() (g_pNFConReg->NFM8ECC0)
  96. #define NF_RDM8ECC1() (g_pNFConReg->NFM8ECC1)
  97. #define NF_RDM8ECC2() (g_pNFConReg->NFM8ECC2)
  98. #define NF_RDM8ECC3() (g_pNFConReg->NFM8ECC3)

  99. #define NF_RDMECCD0() (g_pNFConReg->NFMECCD0)
  100. #define NF_RDMECCD1() (g_pNFConReg->NFMECCD1)
  101. #define NF_RDSECCD() (g_pNFConReg->NFSECCD)

  102. #define NF_ECC_ERR0 (g_pNFConReg->NFECCERR0)
  103. #define NF_ECC_ERR1 (g_pNFConReg->NFECCERR1)

  104. #define NF_ECC8BIT_NUM ((g_pNFConReg->NF8ECCERR0 & (0xf<<25))>>25)
  105. #define NF_ECC8LOCATION_BYTE1 (g_pNFConReg->NF8ECCERR0 & (0x3ff))
  106. #define NF_ECC8LOCATION_BYTE2 ((g_pNFConReg->NF8ECCERR0 & (0x3ff<<15))>>15)
  107. #define NF_ECC8LOCATION_BYTE3 (g_pNFConReg->NF8ECCERR1 & (0x3ff))
  108. #define NF_ECC8LOCATION_BYTE4 ((g_pNFConReg->NF8ECCERR1 & (0x3ff<<11))>>11)
  109. #define NF_ECC8LOCATION_BYTE5 ((g_pNFConReg->NF8ECCERR1 & (0x3ff<<22))>>22)
  110. #define NF_ECC8LOCATION_BYTE6 (g_pNFConReg->NF8ECCERR2 & (0x3ff))
  111. #define NF_ECC8LOCATION_BYTE7 ((g_pNFConReg->NF8ECCERR2 & (0x3ff<<11))>>11)
  112. #define NF_ECC8LOCATION_BYTE8 ((g_pNFConReg->NF8ECCERR2 & (0x3ff<<22))>>22)

  113. #define NF_ECC8LOCATION_BIT(n) (n <= 4)?((g_pNFConReg->NFMLC8BITPT0 & (0xff<<((n-1)*8)))>>((n-1)*8)):((g_pNFConReg->NFMLC8BITPT1 & (0xff<<((n-5)*8)))>>((n-5)*8))

  114. #define NF_ECC4BIT_NUM ((g_pNFConReg->NFECCERR0 & (0x7<<26))>>26)
  115. #define NF_ECC4LOCATION_BYTE(n) ((n <= 2)?((g_pNFConReg->NFECCERR0 & (0x3ff<<((n-1)*16)))>>((n-1)*16)):((g_pNFConReg->NFECCERR1 & (0x3ff<<((n-3)*16)))>>((n-3)*16)))

  116. #define NF_ECC4LOCATION_BIT(n) ((g_pNFConReg->NFMLCBITPT & (0xff<<((n-1)*8)))>>((n-1)*8))

  117. #define NF_WRMECCD0(data) {g_pNFConReg->NFMECCD0 = (data);}
  118. #define NF_WRMECCD1(data) {g_pNFConReg->NFMECCD1 = (data);}
  119. #define NF_WRSECCD(data) {g_pNFConReg->NFSECCD = (data);}

  120. #define NF_RDSTAT (g_pNFConReg->NFSTAT)

  121. //-----------------------------------------------------------------------------

  122. typedef enum
  123. {
  124. ECC_CORRECT_MAIN = 0, // correct Main ECC
  125. ECC_CORRECT_SPARE1 = 1, // correct Spare for Sector Info using Main ECC Result Area
  126. ECC_CORRECT_SPARE2 = 2, // correct Spare for MECC using Main ECC Result Area
  127. ECC_CORRECT_SPARE = 3 // correct Spare using Spare ECC Result Area
  128. } ECC_CORRECT_TYPE;

  129. //-----------------------------------------------------------------------------

  130. #endif // __NAND_H_.



nand.h文件里有8bit ECC的相关定义,不要高兴的太早,这里面是有错滴,不知道是不是飞凌那边故意搞错的,还是本来就不是6410的。


且看定义:


#define NF_ECC_8BIT_STOP() {g_pNFConReg->NFCONT |= (1<<12);} //stop the last encode or decode of 8bit mode.


对比文档看一下这个NFCONT[12]是干什么的


NFCONT[11]这个才是8bitStop,但为什么源码中要定义NFCONT[12]为NF_ECC_8BIT_STOP,这个容易让人产生误解。咱还是改一下吧!

#define NF_ECC_8BIT_STOP()            {g_pNFConReg->NFCONT |= (1<<11);}


相关引用的暂时到这里,下一篇将会开始源码分析

接着上一篇,这一篇介绍cpp部分


fmd.cpp,这里将逐个函数进行分析讲解:



[cpp]​view plain​​​​copy​


  1. //
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //
  4. //
  5. // Use of this sample source code is subject to the terms of the Microsoft
  6. // license agreement under which you licensed this sample source code. If
  7. // you did not accept the terms of the license agreement, you are not
  8. // authorized to use this sample source code. For the terms of the license,
  9. // please see the license agreement between you and Microsoft or, if applicable,
  10. // see the LICENSE.RTF on your install media or the root of your tools installation.
  11. // THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
  12. //
  13. #include <fmd.h>
  14. #include <nkintr.h>
  15. #include <oal.h>

  16. // BSP Configuration Files
  17. #include "bsp_cfg.h"
  18. #include "bsp_base_reg_cfg.h"

  19. // Base Definitions
  20. #include "s3c6410_base_regs.h"
  21. #include "s3c6410_nand.h"
  22. #include "s3c6410_syscon.h"

  23. //#include <ethdbg.h>
  24. #include "Cfnand.h"
  25. //#include <kitl.h>

  26. //#define SYNC_OP
  27. #define CHECK_SPAREECC (0) // 1 gjl
  28. #define NAND_DEBUG (0)

  29. #define NAND_BASE (0xB0200000) // PA:0x70200000
  30. #define SYSCON_BASE (0xB2A0F000) // PA:0x7E00F000

  31. #ifdef SYNC_OP
  32. CRITICAL_SECTION g_csNandFlash;
  33. #endif

  34. #ifdef __cplusplus // gjl
  35. extern "C" {
  36. int iSighForSlcMlc; // gjl
  37. }
  38. #endif

  39. static volatile S3C6410_NAND_REG *g_pNFConReg = NULL;
  40. static volatile S3C6410_SYSCON_REG *g_pSysConReg = NULL;

  41. #define DEBUG_WRITE_READ_EQUAL 0

  42. #if DEBUG_WRITE_READ_EQUAL
  43. DWORD g_MECCBuf[8];
  44. DWORD g_MECCBuf_R[8];
  45. DWORD g_SECCBuf[2];
  46. DWORD g_SECCBuf_R[2];
  47. #endif

  48. extern "C"
  49. {
  50. void RdPage512(unsigned char *bufPt);
  51. void RdPage512Unalign(unsigned char *bufPt);
  52. void WrPage512(unsigned char *bufPt);
  53. void WrPage512Unalign(unsigned char *bufPt);
  54. void WrPageInfo(PBYTE pBuff);
  55. void RdPageInfo(PBYTE pBuff);
  56. }

  57. NANDDeviceInfo GetNandInfo(void) { return stDeviceInfo; }




引用头文件这部分不需要更改


1、读取Flash的ID:


[cpp]​view plain​​​​copy​


  1. /*
  2. @func DWORD | ReadFlashID | Reads the flash manufacturer and device codes.
  3. @rdesc Manufacturer and device codes.
  4. @comm
  5. @xref
  6. */
  7. static DWORD ReadFlashID(void)
  8. {
  9. BYTE Mfg, Dev, Nouse, Infoma, Planesize;
  10. int i;

  11. NF_nFCE_L(); // Deselect the flash chip.
  12. NF_CMD(CMD_READID); // Send flash ID read command.

  13. NF_ADDR(0);

  14. for (i=0; i<10; i++)
  15. {
  16. Mfg = NF_RDDATA_BYTE();
  17. if (Mfg == 0xEC || Mfg == 0x98) break;
  18. }

  19. Dev = NF_RDDATA_BYTE();
  20. Nouse = NF_RDDATA_BYTE();
  21. Infoma = NF_RDDATA_BYTE();
  22. Planesize = NF_RDDATA_BYTE();
  23. //RETAILMSG(1, (TEXT("[FMD:ERR] FMD_Init() : page info = 0x%08x,0x%08x,0x%08x\n"), Nouse,Infoma,Planesize));
  24. //According to the read ID from flash, Nouse=0x14,Infoma=0xa5,planesize=0x64,the page size is 2K, block size is 256KB
  25. //there are 4096 blocks, plane num=2, plane size=4Gbit
  26. //So we should change the cfnand.h nandflash information.
  27. NF_nFCE_H();

  28. return ((DWORD)(Mfg<<8)+Dev);
  29. }

这个也不需要修改,具体请参照手册




2、flash初始化:


[cpp]​view plain​​​​copy​


  1. /*
  2. @func PVOID | FMD_Init | Initializes the Smart Media NAND flash controller.
  3. @rdesc Pointer to S3C2410 NAND controller registers.
  4. @comm
  5. @xref
  6. */

  7. PVOID FMD_Init(LPCTSTR lpActiveReg, PPCI_REG_INFO pRegIn, PPCI_REG_INFO pRegOut)
  8. {
  9. volatile DWORD nNandID;
  10. UINT8 nMID, nDID;
  11. UINT32 nCnt;
  12. BOOL bNandExt = FALSE;

  13. RETAILMSG(1, (TEXT("[FMD] ++FMD_Init() *######\r\n")));





  14. if (pRegIn && pRegIn->MemBase.Num && pRegIn->MemBase.Reg[0])
  15. {
  16. g_pNFConReg = (S3C6410_NAND_REG *)(pRegIn->MemBase.Reg[0]);
  17. }
  18. else
  19. {
  20. g_pNFConReg = (S3C6410_NAND_REG *)NAND_BASE;
  21. }

  22. g_pSysConReg = (S3C6410_SYSCON_REG *)SYSCON_BASE;

  23. #ifdef SYNC_OP
  24. InitializeCriticalSection(&g_csNandFlash);

  25. EnterCriticalSection(&g_csNandFlash);
  26. #endif

  27. // Configure BUS Width and Chip Select for NAND Flash
  28. g_pSysConReg->MEM_SYS_CFG &= ~(1<<12); // NAND Flash BUS Width -> 8 bit
  29. g_pSysConReg->MEM_SYS_CFG &= ~(0x1<<1); // Xm0CS2 -> NFCON CS0

  30. // Set up initial flash controller configuration.
  31. g_pNFConReg->NFCONF = (TACLS<<12) | (TWRPH0<<8) | (TWRPH1<<4);
  32. NF_ECCTYPE_4BIT();
  33. g_pNFConReg->NFCONT = (0<<17)|(0<<16)|(0<<10)|(0<<9)|(0<<8)|(1<<7)|(1<<6)|(1<<5)|(1<<4)|(0x3<<1)|(1<<0);
  34. g_pNFConReg->NFSTAT = (1<<4);

  35. nNandID = ReadFlashID();

  36. #ifdef SYNC_OP
  37. LeaveCriticalSection(&g_csNandFlash);
  38. #endif

  39. nMID = (UINT8)(nNandID >> 8);
  40. nDID = (UINT8)(nNandID & 0xff);

  41. RETAILMSG(1, (TEXT("[FMD:INF] FMD_Init() : Read ID = 0x%08x\n"), nNandID));

  42. for (nCnt = 0; astNandSpec[nCnt].nMID != 0; nCnt++)
  43. {
  44. if (nDID == astNandSpec[nCnt].nDID)
  45. {
  46. bNandExt = TRUE;
  47. break;
  48. }
  49. }

  50. if (!bNandExt)
  51. {
  52. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_Init() : Unknown ID = 0x%08x\n"), nNandID));
  53. return NULL;
  54. }

  55. NUM_OF_BLOCKS = astNandSpec[nCnt].nNumOfBlks;
  56. PAGES_PER_BLOCK = astNandSpec[nCnt].nPgsPerBlk;
  57. SECTORS_PER_PAGE = astNandSpec[nCnt].nSctsPerPg;



  58. RETAILMSG(1, (TEXT("[FMD] FMD_Init() : NUM_OF_BLOCKS = %d\n"), NUM_OF_BLOCKS));
  59. RETAILMSG(1, (TEXT("[FMD] FMD_Init() : PAGES_PER_BLOCK = %d\n"), PAGES_PER_BLOCK));
  60. RETAILMSG(1, (TEXT("[FMD] FMD_Init() : SECTORS_PER_PAGE = %d\n"), SECTORS_PER_PAGE));

  61. // gjl
  62. if(iSighForSlcMlc == 1)
  63. {
  64. NUM_OF_BLOCKS = 8192;
  65. PAGES_PER_BLOCK = 8;
  66. SECTORS_PER_PAGE = 4;
  67. }
  68. else
  69. {
  70. NUM_OF_BLOCKS = astNandSpec[nCnt].nNumOfBlks;
  71. PAGES_PER_BLOCK = astNandSpec[nCnt].nPgsPerBlk;
  72. SECTORS_PER_PAGE = astNandSpec[nCnt].nSctsPerPg;

  73. }

  74. RETAILMSG(1, (TEXT("[FMD] --FMD_Init()\n")));

  75. return((PVOID)g_pNFConReg);
  76. }


上面这个代码是原BSP中的,这个初始化函数里面,有些东西需要修改一下



// Set up initial flash controller configuration.

g_pNFConReg->NFCONF = (TACLS<<12) | (TWRPH0<<8) | (TWRPH1<<4);

这个我们对照手册看一下

NFCONF[0]必须写0,NFCONF[2]必须写1,所以最后修改为:


[cpp]​view plain​​​​copy​

  1. g_pNFConReg->NFCONF = (TACLS<<12) | (TWRPH0<<8) | (TWRPH1<<4) | (1<<2) | (0<<0);


这一句接下来,原代码中使用的是NF_ECCTYPE_4BIT(); 4bit的ECC,这里我们将使用8bit的ECC,修改为




[cpp]​view plain​​​​copy​

  1. NF_ECCTYPE_8BIT(); // use 8bit ECC





接下来初始化配置NFCONT

原:

g_pNFConReg->NFCONT = (0<<17)|(0<<16)|(0<<10)|(0<<9)|(0<<8)|(1<<7)|(1<<6)|(1<<5)|(1<<4)|(0x3<<1)|(1<<0);

参照手册修改为:


[cpp]​view plain​​​​copy​

  1. g_pNFConReg->NFCONT = (0<<13)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);




接下来直接看到


[cpp]​view plain​​​​copy​


  1. // gjl
  2. if(iSighForSlcMlc == 1)
  3. {
  4. NUM_OF_BLOCKS = 8192;
  5. PAGES_PER_BLOCK = 8;
  6. SECTORS_PER_PAGE = 4;
  7. }
  8. else
  9. {
  10. NUM_OF_BLOCKS = astNandSpec[nCnt].nNumOfBlks;
  11. PAGES_PER_BLOCK = astNandSpec[nCnt].nPgsPerBlk;
  12. SECTORS_PER_PAGE = astNandSpec[nCnt].nSctsPerPg;

  13. }

这里if中的NUM_OF_BLOCKS = 8192;这个定死了,不大好,咱改成(修改之后,可以同时兼容K9GAG08U0D 2G和K9LBG08U0D 4G的Nandflash)

[cpp]​view plain​​​​copy​

  1. NUM_OF_BLOCKS = astNandSpec[nCnt].nNumOfBlks;


到此,FMD_Init()函数就基本上搞掂了。





3、FMD_ReadSector()


[cpp]​view plain​​​​copy​


  1. /*
  2. @func BOOL | FMD_ReadSector | Reads the specified sector(s) from NAND flash.
  3. @rdesc TRUE = Success, FALSE = Failure.
  4. @comm
  5. @xref
  6. */
  7. BOOL FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
  8. {
  9. BOOL bRet;

  10. //RETAILMSG(1, (TEXT("[R:0x%08x] \n"), startSectorAddr));
  11. #if (NAND_DEBUG)
  12. RETAILMSG(1, (TEXT("[FMD] ++FMD_ReadSector(0x%08x) \n"), startSectorAddr));
  13. #endif

  14. #ifdef SYNC_OP
  15. EnterCriticalSection(&g_csNandFlash);
  16. #endif

  17. if ( IS_LB )
  18. {
  19. bRet = FMD_LB_ReadSector(startSectorAddr, pSectorBuff, pSectorInfoBuff, dwNumSectors);
  20. }
  21. else
  22. {
  23. bRet = FMD_SB_ReadSector(startSectorAddr, pSectorBuff, pSectorInfoBuff, dwNumSectors);
  24. }

  25. #ifdef SYNC_OP
  26. LeaveCriticalSection(&g_csNandFlash);
  27. #endif

  28. #if (NAND_DEBUG)
  29. RETAILMSG(1, (TEXT("[FMD] --FMD_ReadSector()\n")));
  30. #endif

  31. return bRet;
  32. }

这里我们不需要做修改,不过其中调用的FMD_LB_ReadSector()函数待会需要修改一下(FMD_LB_ReadSector()是针对Nandflash,4bit或8bit的ECC;而FMD_SB_ReadSector()是针对Norflash,1bit的ecc校验)






4、FMD_EraseBlock() 擦除块


[cpp]​view plain​​​​copy​


  1. /*
  2. @func BOOL | FMD_EraseBlock | Erases the specified flash block.
  3. @rdesc TRUE = Success, FALSE = Failure.
  4. @comm
  5. @xref
  6. */
  7. BOOL FMD_EraseBlock(BLOCK_ID blockID)
  8. {
  9. BOOL bRet = TRUE;

  10. #if (NAND_DEBUG)
  11. RETAILMSG(1, (TEXT("[FMD] ++FMD_EraseBlock(0x%08x) \n"), blockID));
  12. #endif

  13. #ifdef SYNC_OP
  14. EnterCriticalSection(&g_csNandFlash);
  15. #endif

  16. if ( IS_LB )
  17. {
  18. bRet = FMD_LB_EraseBlock(blockID);
  19. }
  20. else
  21. {
  22. bRet = FMD_SB_EraseBlock(blockID);
  23. }

  24. #ifdef SYNC_OP
  25. LeaveCriticalSection(&g_csNandFlash);
  26. #endif

  27. #if (NAND_DEBUG)
  28. RETAILMSG(1, (TEXT("[FMD] --FMD_EraseBlock()\n")));
  29. #endif

  30. return bRet;
  31. }


这个函数也没有什么需要修改的,其中调用到的FMD_LB_EraseBlock()待会会讲到。





5、FMD_WriteSector()


[cpp]​view plain​​​​copy​


  1. /*
  2. @func BOOL | FMD_WriteSector | Writes the specified data to the specified NAND flash sector/page.
  3. @rdesc TRUE = Success, FALSE = Failure.
  4. @comm
  5. @xref
  6. */
  7. BOOL FMD_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
  8. {
  9. BOOL bRet = TRUE;
  10. #if DEBUG_WRITE_READ_EQUAL
  11. BYTE pSectorBuffRead[4096]; // gjl 2048
  12. SectorInfo SectorInfoBuffRead;
  13. PSectorInfo pSectorInfoBuffRead = &SectorInfoBuffRead;
  14. BYTE *temp = (BYTE*)pSectorInfoBuff;
  15. BYTE *temp1 = (BYTE*)pSectorInfoBuffRead;
  16. UINT16 nSectorLoop,j=40;
  17. #endif

  18. #if (NAND_DEBUG)
  19. RETAILMSG(1, (TEXT("[FMD] ++FMD_WriteSector(0x%08x) \n"), startSectorAddr));
  20. #endif

  21. #ifdef SYNC_OP
  22. EnterCriticalSection(&g_csNandFlash);
  23. #endif

  24. if ( IS_LB )
  25. {
  26. bRet = FMD_LB_WriteSector(startSectorAddr, pSectorBuff, pSectorInfoBuff, dwNumSectors);
  27. #if DEBUG_WRITE_READ_EQUAL
  28. FMD_LB_ReadSector(startSectorAddr, pSectorBuffRead, pSectorInfoBuffRead, dwNumSectors);
  29. for (nSectorLoop = 0; nSectorLoop < 2048; nSectorLoop++)
  30. {
  31. if(pSectorBuff[nSectorLoop] != pSectorBuffRead[nSectorLoop])
  32. break;
  33. }
  34. RETAILMSG(1, (TEXT("[FMD] ++FMD_WriteSector equal number = %x \n"), nSectorLoop));
  35. for (nSectorLoop = 0; nSectorLoop < 8; nSectorLoop++)
  36. {
  37. if(temp1[nSectorLoop] != temp[nSectorLoop])
  38. break;
  39. }
  40. RETAILMSG(1, (TEXT("[FMD] ++FMD_WriteSector informationequal equal number = %x \n"), nSectorLoop));
  41. RETAILMSG(1, (TEXT("[FMD] ++FMD_WriteSector sector bytes:\n")));
  42. for (nSectorLoop = 0; nSectorLoop < 2048; nSectorLoop++)
  43. {
  44. if(j--==0)
  45. {
  46. j=40;
  47. RETAILMSG(1, (TEXT("\n")));
  48. }
  49. RETAILMSG(1, (TEXT("%x "), pSectorBuff[nSectorLoop]));
  50. }
  51. RETAILMSG(1, (TEXT("\n end\n")));
  52. RETAILMSG(1, (TEXT("[FMD] ++FMD_ReadSector sector bytes:\n")));
  53. for (nSectorLoop = 0; nSectorLoop < 2048; nSectorLoop++)
  54. {
  55. if(j--==0)
  56. {
  57. j=40;
  58. RETAILMSG(1, (TEXT("\n")));
  59. }
  60. RETAILMSG(1, (TEXT("%x "), pSectorBuffRead[nSectorLoop]));
  61. }
  62. RETAILMSG(1, (TEXT("\n end\n")));
  63. for (nSectorLoop = 0; nSectorLoop < 8; nSectorLoop++)
  64. {
  65. if(g_MECCBuf_R[nSectorLoop] != g_MECCBuf[nSectorLoop])
  66. break;
  67. }
  68. RETAILMSG(1, (TEXT("[FMD] ++FMD_WriteSector MECC data equal number = %x \n"), nSectorLoop));
  69. for (nSectorLoop = 0; nSectorLoop < 8; nSectorLoop++)
  70. {
  71. if(g_SECCBuf_R[nSectorLoop] != g_SECCBuf[nSectorLoop])
  72. break;
  73. }
  74. RETAILMSG(1, (TEXT("[FMD] ++FMD_WriteSector SECC data equal number = %x \n"), nSectorLoop));

  75. #endif
  76. }
  77. else
  78. {
  79. bRet = FMD_SB_WriteSector(startSectorAddr, pSectorBuff, pSectorInfoBuff, dwNumSectors);
  80. }

  81. #ifdef SYNC_OP
  82. LeaveCriticalSection(&g_csNandFlash);
  83. #endif

  84. #if (NAND_DEBUG)
  85. RETAILMSG(1, (TEXT("[FMD] --FMD_WriteSector()\n")));
  86. #endif

  87. return bRet;
  88. }


该函数也没有什么地方需要修改的。





6、FMD_PowerUp()


[cpp]​view plain​​​​copy​


  1. VOID FMD_PowerUp(VOID)
  2. {
  3. #if (NAND_DEBUG)
  4. RETAILMSG(1, (TEXT("[FMD] FMD_PowerUp() \n")));
  5. #endif

  6. // Set up initial flash controller configuration.
  7. g_pNFConReg->NFCONF = (TACLS<<12) | (TWRPH0<<8) | (TWRPH1<<4);
  8. g_pNFConReg->NFCONT = (0<<17)|(0<<16)|(0<<10)|(0<<9)|(0<<8)|(1<<7)|(1<<6)|(1<<5)|(1<<4)|(0x3<<1)|(1<<0);
  9. g_pNFConReg->NFSTAT = (1<<4);
  10. }


这里我们参照之前在FMD_Init()的配置,修改其中的NFCONF和NFCONT的配置为:




[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONF = (TACLS<<12) | (TWRPH0<<8) | (TWRPH1<<4) | (1<<2) | (0<<0);
  2. g_pNFConReg->NFCONT = (0<<13)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);






7、FMD_PowerDown()


[cpp]​view plain​​​​copy​


  1. VOID FMD_PowerDown(VOID)
  2. {
  3. #if (NAND_DEBUG)
  4. RETAILMSG(1, (TEXT("[FMD] FMD_PowerDown() \n")));
  5. #endif

  6. }


该函数不需要修改什么。





接下来的这几个函数都不需要修改:


[cpp]​view plain​​​​copy​


  1. BOOL FMD_OEMIoControl(DWORD dwIoControlCode, PBYTE pInBuf, DWORD nInBufSize, PBYTE pOutBuf, DWORD nOutBufSize, PDWORD pBytesReturned)
  2. {
  3. switch(dwIoControlCode)
  4. {
  5. case IOCTL_FMD_GET_INTERFACE:
  6. {
  7. RETAILMSG(1, (TEXT("[FMD] FMD_OEMIoControl() : IOCTL_FMD_GET_INTERFACE\n")));

  8. if (!pOutBuf || nOutBufSize < sizeof(FMDInterface))
  9. {
  10. DEBUGMSG(1, (TEXT("FMD_OEMIoControl: IOCTL_FMD_GET_INTERFACE bad parameter(s).\n")));
  11. return(FALSE);
  12. }

  13. PFMDInterface pInterface = (PFMDInterface)pOutBuf;

  14. pInterface->cbSize = sizeof(FMDInterface);
  15. pInterface->pInit = FMD_Init;
  16. pInterface->pDeInit = FMD_Deinit;
  17. pInterface->pGetInfo = FMD_GetInfo;
  18. pInterface->pGetInfoEx = NULL; //FMD_GetInfoEx;
  19. pInterface->pGetBlockStatus = FMD_GetBlockStatus;
  20. pInterface->pSetBlockStatus = FMD_SetBlockStatus;
  21. pInterface->pReadSector = FMD_ReadSector;
  22. pInterface->pWriteSector = FMD_WriteSector;
  23. pInterface->pEraseBlock = FMD_EraseBlock;
  24. pInterface->pPowerUp = FMD_PowerUp;
  25. pInterface->pPowerDown = FMD_PowerDown;
  26. pInterface->pGetPhysSectorAddr = NULL;

  27. break;
  28. }

  29. case IOCTL_FMD_LOCK_BLOCKS:
  30. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_LOCK_BLOCKS Not Supported\n")));
  31. return FALSE;

  32. case IOCTL_FMD_UNLOCK_BLOCKS:
  33. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_UNLOCK_BLOCKS Not Supported\n")));
  34. return FALSE;

  35. case IOCTL_FMD_READ_RESERVED:
  36. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_READ_RESERVED\n")));
  37. return FALSE;

  38. case IOCTL_FMD_WRITE_RESERVED:
  39. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_WRITE_RESERVED\n")));
  40. return FALSE;

  41. case IOCTL_FMD_GET_RESERVED_TABLE:
  42. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_GET_RESERVED_TABLE\n")));
  43. return FALSE;

  44. case IOCTL_FMD_SET_REGION_TABLE:
  45. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_SET_REGION_TABLE\n")));
  46. return FALSE;

  47. case IOCTL_FMD_SET_SECTORSIZE:
  48. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_SET_SECTORSIZE\n")));
  49. return FALSE;

  50. case IOCTL_FMD_RAW_WRITE_BLOCKS:
  51. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_RAW_WRITE_BLOCKS\n")));
  52. return FALSE;

  53. case IOCTL_FMD_GET_RAW_BLOCK_SIZE:
  54. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_GET_RAW_BLOCK_SIZE\n")));
  55. return FALSE;

  56. case IOCTL_FMD_GET_INFO:
  57. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_GET_INFO\n")));
  58. return FALSE;

  59. case IOCTL_FMD_SET_XIPMODE :
  60. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_SET_XIPMODE\n")));
  61. return FALSE;

  62. case IOCTL_FMD_GET_XIPMODE:
  63. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_FMD_GET_XIPMODE\n")));
  64. return FALSE;

  65. case IOCTL_DISK_FLUSH_CACHE:
  66. //RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : IOCTL_DISK_FLUSH_CACHE\n")));
  67. return TRUE;

  68. default:
  69. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_OEMIoControl() : Unknown IOCTL (0x%08x)\n"), dwIoControlCode));
  70. return FALSE;
  71. }

  72. return TRUE;
  73. }

  74. BOOL FMD_Deinit(PVOID hFMD)
  75. {
  76. #if (NAND_DEBUG)
  77. RETAILMSG(1, (TEXT("[FMD] FMD_Deinit() \n")));
  78. #endif

  79. return(TRUE);
  80. }


  81. /*
  82. @func BOOL | FMD_GetInfo | Provides information on the NAND flash.
  83. @rdesc TRUE = Success, FALSE = Failure.
  84. @comm
  85. @xref
  86. */
  87. BOOL FMD_GetInfo(PFlashInfo pFlashInfo)
  88. {
  89. // Add by AlexLee RunNo[5]
  90. UINT32 nCnt;
  91. UINT32 nNandID;
  92. UINT8 nMID, nDID;

  93. if (pFlashInfo == NULL)
  94. {
  95. RETAILMSG(1, (TEXT("[FMD:ERR] FMD_GetInfo() : Invalid Parameter\n")));
  96. return(FALSE);
  97. }

  98. pFlashInfo->flashType = NAND;

  99. #ifdef SYNC_OP
  100. EnterCriticalSection(&g_csNandFlash);
  101. #endif

  102. nNandID = ReadFlashID();

  103. #ifdef SYNC_OP
  104. LeaveCriticalSection(&g_csNandFlash);
  105. #endif

  106. nMID = nNandID >> 8;
  107. nDID = nNandID & 0xff;

  108. for (nCnt = 0; astNandSpec[nCnt].nMID != 0; nCnt++)
  109. {
  110. if (nDID == astNandSpec[nCnt].nDID)
  111. {
  112. break;
  113. }
  114. }

  115. // OK, instead of reading it from the chip, we use the hardcoded
  116. // numbers here.

  117. pFlashInfo->dwNumBlocks = NUM_OF_BLOCKS;
  118. pFlashInfo->wSectorsPerBlock = PAGES_PER_BLOCK;
  119. pFlashInfo->wDataBytesPerSector = NAND_SECTOR_SIZE;
  120. pFlashInfo->dwBytesPerBlock = (PAGES_PER_BLOCK * NAND_SECTOR_SIZE);

  121. //RETAILMSG(1, (TEXT("[FMD] FMD_GetInfo() : NUMBLOCKS = %d(0x%x), SECTORSPERBLOCK = %d(0x%x), BYTESPERSECTOR = %d(0x%x) \n"), pFlashInfo->dwNumBlocks, pFlashInfo->dwNumBlocks, pFlashInfo->wSectorsPerBlock, pFlashInfo->wSectorsPerBlock, pFlashInfo->wDataBytesPerSector, pFlashInfo->wDataBytesPerSector)); // del by alexlee

  122. return TRUE;
  123. }


  124. /*
  125. @func DWORD | FMD_GetBlockStatus | Returns the status of the specified block.
  126. @rdesc Block status (see fmd.h).
  127. @comm
  128. @xref
  129. */
  130. DWORD FMD_GetBlockStatus(BLOCK_ID blockID)
  131. {
  132. DWORD dwResult = 0;

  133. #if (NAND_DEBUG)
  134. RETAILMSG(1, (TEXT("[FMD] ++FMD_GetBlockStatus(0x%08x) \n"), blockID));
  135. #endif

  136. #ifdef SYNC_OP
  137. EnterCriticalSection(&g_csNandFlash);
  138. #endif

  139. if ( IS_LB )
  140. {
  141. dwResult = FMD_LB_GetBlockStatus(blockID);
  142. }
  143. else
  144. {
  145. dwResult = FMD_SB_GetBlockStatus(blockID);
  146. }

  147. #ifdef SYNC_OP
  148. LeaveCriticalSection(&g_csNandFlash);
  149. #endif

  150. #if (NAND_DEBUG)
  151. RETAILMSG(1, (TEXT("[FMD] --FMD_GetBlockStatus()\n")));
  152. #endif

  153. return dwResult;
  154. }


  155. /*
  156. @func BOOL | FMD_SetBlockStatus | Marks the block with the specified block status.
  157. @rdesc TRUE = Success, FALSE = Failure.
  158. @comm
  159. @xref
  160. */
  161. BOOL FMD_SetBlockStatus(BLOCK_ID blockID, DWORD dwStatus)
  162. {
  163. BOOL bRet = TRUE;

  164. #if (NAND_DEBUG)
  165. RETAILMSG(1, (TEXT("[FMD] ++FMD_SetBlockStatus(0x%08x, 0x%08x) \n"), blockID, dwStatus));
  166. #endif

  167. #ifdef SYNC_OP
  168. EnterCriticalSection(&g_csNandFlash);
  169. #endif

  170. if ( IS_LB )
  171. {
  172. bRet = FMD_LB_SetBlockStatus(blockID, dwStatus);
  173. }
  174. else
  175. {
  176. bRet = FMD_SB_SetBlockStatus(blockID, dwStatus);
  177. }

  178. #ifdef SYNC_OP
  179. LeaveCriticalSection(&g_csNandFlash);
  180. #endif

  181. #if (NAND_DEBUG)
  182. RETAILMSG(1, (TEXT("[FMD] --FMD_SetBlockStatus()\n")));
  183. #endif

  184. return bRet;
  185. }






文章有点长,先到这里了,关键点将在下一篇介绍


接着上一篇


1、ECC_CorrectData() 查找ECC错误并矫正


[cpp]​view plain​​​​copy​


  1. BOOL ECC_CorrectData(SECTOR_ADDR sectoraddr, LPBYTE pData, UINT32 nRetEcc, ECC_CORRECT_TYPE nType)
  2. {
  3. DWORD nErrDataNo;
  4. DWORD nErrBitNo;
  5. //BYTE Status;
  6. BYTE nErrDataNum;
  7. UINT8 nErrByteNum;
  8. UINT8 countdown = 155;
  9. BOOL bRet = TRUE;

  10. //RETAILMSG(1, (TEXT("#### FMD_DRIVER:::ECC_CorrectData %x, %x, %x\n"), sectoraddr, nRetEcc, nType));

  11. #if 0
  12. if( (nRetEcc & NF_ECC8ERR0_ECC_READY) )
  13. return TRUE;
  14. #endif
  15. // 8bit ECC error searching engine needs mini mum 372 cycles to find any error
  16. countdown = 372;
  17. while(countdown--);


[cpp]​view plain​​​​copy​


  1. // 等待ECC错误查找完毕
  2. while(NF_ECC8_ERR0 & 0x80000000);


[cpp]​view plain​​​​copy​


  1. // 获取8bit ECC解码结果
  2. nErrDataNum = NF_ECC8BIT_NUM;

  3. // No error, if free page (all 0xff)
  4. if( (g_pNFConReg->NF8ECCERR0 >> 29) & 0x1 ){
  5. nErrDataNum = 0;
  6. }

  7. if (nErrDataNum == 0)
  8. {
  9. bRet = TRUE;
  10. RETAILMSG(0,(TEXT("No Error\n")));
  11. goto finished;
  12. }
  13. else if (nErrDataNum == 9)
  14. {
  15. bRet = FALSE;
  16. RETAILMSG(1,(TEXT("More than 8-bit error, uncorrectable\n")));
  17. goto finished;
  18. }
  19. else if (nErrDataNum > 9)
  20. {
  21. bRet = FALSE;
  22. RETAILMSG(1,(TEXT("Reserved\n")));
  23. goto finished;
  24. }
  25. else
  26. {


[cpp]​view plain​​​​copy​


  1. // 获取错误位对应的位置
  2. for (nErrByteNum = 1; nErrByteNum <= nErrDataNum; nErrByteNum++)
  3. {
  4. switch(nErrByteNum)
  5. {
  6. case 1: nErrDataNo = NF_ECC8LOCATION_BYTE1;
  7. break;
  8. case 2: nErrDataNo = NF_ECC8LOCATION_BYTE2;
  9. break;
  10. case 3: nErrDataNo = NF_ECC8LOCATION_BYTE3;
  11. break;
  12. case 4: nErrDataNo = NF_ECC8LOCATION_BYTE4;
  13. break;
  14. case 5: nErrDataNo = NF_ECC8LOCATION_BYTE5;
  15. break;
  16. case 6: nErrDataNo = NF_ECC8LOCATION_BYTE6;
  17. break;
  18. case 7: nErrDataNo = NF_ECC8LOCATION_BYTE7;
  19. break;
  20. case 8: nErrDataNo = NF_ECC8LOCATION_BYTE8;
  21. break;
  22. default:break;
  23. }


[cpp]​view plain​​​​copy​


  1. // 定位到具体错误位的位置
  2. nErrBitNo = NF_ECC8LOCATION_BIT(nErrByteNum);


[cpp]​view plain​​​​copy​


  1. // 矫正错误位
  2. (pData)[nErrDataNo] ^= (1<<nErrBitNo);
  3. RETAILMSG(1, (TEXT("8bit ECC_CorrectData %x, %x, %x, %x\n"), nErrDataNum, nErrByteNum, nErrDataNo, nErrBitNo));
  4. }
  5. }

  6. inished:
  7. return bRet;





这里是修改后的,支持8bit ECC校验。

注意到上面的

  // No error, if free page (all 0xff)

  if( (g_pNFConReg->NF8ECCERR0 >> 29) & 0x1 ){

     nErrDataNum = 0;

  }

代码了吗?让我们对比手册看看这个NF8ECCERR0[29]是何许人也

看到了吧,NF8ECCERR0[29]是保留位。然而,我参考6410的MLC的BSP源码,发现里面有用到这个位来判断是否全为0xff。在飞凌最新发布的linux3.0的源码中也查看到有用到这个保留位,而且还是针对8bit ECC来使用的,参考的两个源码都支持这两个Nandflash。为什么6410的芯片文档上会写成是保留位?是笔误还是有所保留?为啥三星自己的MLC的BSP中也有使用?具体大家自己纠结去吧,反正上面这样使用了也没见着啥不良影响。




2、FMD_LB_ReadSector()

原来的代码:

[cpp]​view plain​​​​copy​


  1. BOOL FMD_LB_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
  2. {
  3. ULONG SectorAddr = (ULONG)startSectorAddr;
  4. DWORD i, j;
  5. volatile DWORD rddata;
  6. UINT32 nRetEcc = 0;
  7. DWORD MECCBuf[16],tempMECCBuf[2]; // gjl 8
  8. UINT16 nSectorLoop,nSectorLoop1;
  9. int NewSpareAddr = 4096; //gjl 2048
  10. int NewDataAddr = 0;
  11. int NewSectorAddr = startSectorAddr;
  12. int SectorSpareAddr;
  13. UINT8 TempSectorInfo[40];
  14. BYTE *pSectorBuff1 = (BYTE *)pSectorBuff;
  15. UINT16 k=40;
  16. #if CHECK_SPAREECC
  17. DWORD SECCBuf[4]; // gjl 2
  18. #endif

  19. #if (NAND_DEBUG)
  20. RETAILMSG(1,(TEXT("#### FMD_DRIVER:::FMD_LB_READSECTOR %x %x\n"),startSectorAddr,NewDataAddr));
  21. #endif

  22. if (!pSectorBuff && !pSectorInfoBuff)
  23. {
  24. return(FALSE);
  25. }


  26. if ( dwNumSectors > 1 )
  27. {
  28. RETAILMSG(1, (TEXT("######## FATAL ERROR => FMD::FMD_ReadSector->dwNumsectors is bigger than 1. \n")));
  29. return FALSE;
  30. }



  31. if (!pSectorBuff)
  32. {
  33. if (!NAND_LB_ReadSectorInfo(startSectorAddr, pSectorInfoBuff))
  34. {
  35. #if (NAND_DEBUG)
  36. RETAILMSG(1,(TEXT("#### FMD_DRIVER:::54321\n")));
  37. #endif

  38. return FALSE;
  39. }
  40. #if (NAND_DEBUG)
  41. RETAILMSG(1,(TEXT("#### FMD_DRIVER:::12345\n")));
  42. #endif

  43. return TRUE;
  44. }


  45. NF_nFCE_L();

  46. NF_CLEAR_RB();

  47. NF_CMD(CMD_READ); // Send read command.

  48. NF_ADDR((NewSpareAddr)&0xff);
  49. NF_ADDR((NewSpareAddr>>8)&0xff);
  50. NF_ADDR((NewSectorAddr) & 0xff);
  51. NF_ADDR((NewSectorAddr >> 8) & 0xff);
  52. #if LB_NEED_EXT_ADDR
  53. NF_ADDR((NewSectorAddr >> 16) & 0xff);
  54. #endif

  55. NF_CMD(CMD_READ3); // 2nd command

  56. NF_DETECT_RB(); // Wait for command to complete.

  57. NF_MSGLENGTH_512();
  58. NF_ECCTYPE_4BIT();

  59. if (pSectorInfoBuff)
  60. {

  61. pSectorInfoBuff->bBadBlock = NF_RDDATA_BYTE();
  62. pSectorInfoBuff->dwReserved1 = NF_RDDATA_WORD();
  63. pSectorInfoBuff->bOEMReserved = NF_RDDATA_BYTE();

  64. pSectorInfoBuff->wReserved2 = NF_RDDATA_BYTE();
  65. pSectorInfoBuff->wReserved2 |= (NF_RDDATA_BYTE()<<8);
  66. }
  67. else
  68. {
  69. for(i=0; i<sizeof(SectorInfo)/sizeof(DWORD); i++)
  70. {
  71. rddata = (DWORD) NF_RDDATA_WORD(); // read and trash the data
  72. }
  73. }

  74. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*2; nSectorLoop++)
  75. {
  76. MECCBuf[nSectorLoop] = NF_RDDATA_WORD();
  77. }

  78. #if DEBUG_WRITE_READ_EQUAL
  79. for (nSectorLoop = 0; nSectorLoop < 8; nSectorLoop++)
  80. {
  81. g_MECCBuf_R[nSectorLoop] = MECCBuf[nSectorLoop];
  82. }
  83. #endif



  84. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE; nSectorLoop++)
  85. {
  86. NewDataAddr = nSectorLoop * SECTOR_SIZE;

  87. NF_CMD(CMD_RDO); // Send read command.
  88. NF_ADDR((NewDataAddr)&0xff);
  89. NF_ADDR((NewDataAddr>>8)&0xff);
  90. NF_CMD(CMD_RDO2); // 2nd command


  91. NF_MSGLENGTH_512();
  92. NF_ECCTYPE_4BIT();

  93. NF_RSTECC();
  94. NF_MECC_UnLock();

  95. if( ((DWORD) (pSectorBuff+nSectorLoop*SECTOR_SIZE)) & 0x3)
  96. {
  97. for(i=0; i<SECTOR_SIZE/sizeof(DWORD); i++)
  98. {
  99. rddata = (DWORD) NF_RDDATA_WORD();
  100. (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+0] = (BYTE)(rddata & 0xff);
  101. (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+1] = (BYTE)(rddata>>8 & 0xff);
  102. (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+2] = (BYTE)(rddata>>16 & 0xff);
  103. (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+3] = (BYTE)(rddata>>24 & 0xff);
  104. }
  105. }
  106. else
  107. {
  108. RdPage512(pSectorBuff+nSectorLoop*SECTOR_SIZE); // Read page/sector data.
  109. }

  110. SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8;
  111. NF_WRDATA_WORD(MECCBuf[2*nSectorLoop]);
  112. SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8+4;
  113. NF_WRDATA_WORD(MECCBuf[2*nSectorLoop+1]);
  114. NF_MECC_Lock();

  115. //decode done
  116. while (!(NF_RDSTAT & (1<<6)));
  117. tempMECCBuf[0]= NF_RDMECC0();
  118. tempMECCBuf[1] = NF_RDMECC1();

  119. pSectorBuff1 = pSectorBuff+nSectorLoop*SECTOR_SIZE;

  120. if (!ECC_CorrectData(startSectorAddr, pSectorBuff1, nRetEcc, ECC_CORRECT_MAIN))
  121. {
  122. RETAILMSG(1,(TEXT("ECC ERROR\n")));
  123. return FALSE;
  124. }
  125. }

  126. NF_nFCE_H();



  127. return TRUE;
  128. }


这个是飞凌BSP中的源码,里面需要修改的地方还是挺多的。

首先,来看定义部分的

DWORD MECCBuf[16],tempMECCBuf[2];  // gjl 8

由于将要用的是8bit的ECC校验,这个ECC的buffer就应该是32,1page=8*512byte,每读取512byte数据产生的ECC存放在4个32位的寄存器中,所以需要8*4个buffer:

DWORD MECCBuf[32];

至于tempMECCBuf,从上述代码中就可以看出就一垃圾,根本没用到,这里就把它删了。

定义完之后,我们需要使能一些相关的中断(不这样搞的话,发现无法正常校验ECC,具体原因请知道的朋友告知一声)

if (!pSectorBuff && !pSectorInfoBuff)

{

        return(FALSE);

}

的后面,我们添加以下代码:

[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONT |= (1<<10); // Enable illegal access interrupt control
  2. g_pNFConReg->NFCONT |= (1<<9); // Enable RnB interrupt
  3. g_pNFConReg->NFCONT |= (1<<12); // Enable 4bit,8bit ECC decoding completion interrupt control



接下来看到代码:(中间省略的那部分就不介绍了,大家有空可以参考一下LoongEmbedded的csdn blog)

NF_MSGLENGTH_512();

NF_ECCTYPE_4BIT();

我们使用8bit ECC,所以把 NF_ECCTYPE_4BIT(); 修改成

[cpp]​view plain​​​​copy​

  1. NF_ECCTYPE_8BIT();


之后就是读取SectorInfo数据的操作,再过来就是读取ECC数据的操作:

    for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*2; nSectorLoop++)

    {

        MECCBuf[nSectorLoop] = NF_RDDATA_WORD();

    }

由于,我们使用的是8bit ECC,上面只读取了8*2 * 4字节的ECC,而8bit的ECC需要8*4 *4字节,所以修改成:

[cpp]​view plain​​​​copy​


  1. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*4; nSectorLoop++) // 8bit ECC,4096/page = 8*512, it has 8*4(register) ECC data
  2. {
  3. MECCBuf[nSectorLoop] = NF_RDDATA_WORD();
  4. }




接下来直接看到循环读取一页数据的操作:

就是

    for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE; nSectorLoop++)

    {

        NewDataAddr = nSectorLoop * SECTOR_SIZE;

        NF_CMD(CMD_RDO);                            // Send read command.

        NF_ADDR((NewDataAddr)&0xff);

        NF_ADDR((NewDataAddr>>8)&0xff);

        NF_CMD(CMD_RDO2);    // 2nd command


  NF_MSGLENGTH_512();

  NF_ECCTYPE_4BIT();

这里,把上面的 NF_ECCTYPE_4BIT(); 修改成:

[cpp]​view plain​​​​copy​

  1. NF_ECCTYPE_8BIT();


顺便在上面这一句后面加上两句:

[cpp]​view plain​​​​copy​


  1. NF_ECC_8BIT_STOP(); // init 8bit ECC decoding
  2. NF_ECC_DIRECTION_IN(); // 4/8BIT ECC Decoding, read page


接下来,原代码是:

        NF_RSTECC();

        NF_MECC_UnLock();

我这里把这两个操作的顺序换一下,变成:

[cpp]​view plain​​​​copy​


  1. NF_MECC_UnLock();
  2. NF_RSTECC();



在NF_RSTECC()之前必须设置 NF_ECC_8BIT_STOP(); ,因为文档中有说到: if you want to stop current work and start encoding/decoding for new data, you must set 8bitStop(NFCONT[11]) before set InitMECC(NFCONT[5]) bit.

接下来的代码就是读取512字节数据的:

        if( ((DWORD) (pSectorBuff+nSectorLoop*SECTOR_SIZE)) & 0x3)

        {

            for(i=0; i<SECTOR_SIZE/sizeof(DWORD); i++)

            {

                rddata = (DWORD) NF_RDDATA_WORD();

                (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+0] = (BYTE)(rddata & 0xff);

                (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+1] = (BYTE)(rddata>>8 & 0xff);

                (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+2] = (BYTE)(rddata>>16 & 0xff);

                (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+3] = (BYTE)(rddata>>24 & 0xff);

            }

        }

        else

        {

            RdPage512(pSectorBuff+nSectorLoop*SECTOR_SIZE);                    // Read page/sector data.

        }

然后,就看到代码把前面读取到的ECC接着写进去了,这里应该是写进去的ECC与读取产生的ECC在ECC模块中进行对比,用于查找错误位

        SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8;

  NF_WRDATA_WORD(MECCBuf[2*nSectorLoop]);

  SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8+4;

  NF_WRDATA_WORD(MECCBuf[2*nSectorLoop+1]);

        NF_MECC_Lock();

这里,同样需要修改成写入8bit ECC的:

[cpp]​view plain​​​​copy​


  1. SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8;
  2. NF_WRDATA_WORD(MECCBuf[4*nSectorLoop]);
  3. SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8+4;
  4. NF_WRDATA_WORD(MECCBuf[4*nSectorLoop+1]);
  5. SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8+8;
  6. NF_WRDATA_WORD(MECCBuf[4*nSectorLoop+2]);
  7. SectorSpareAddr = NewSpareAddr+8+nSectorLoop*8+12;
  8. NF_WRDATA_WORD(MECCBuf[4*nSectorLoop+3]);
  9. NF_MECC_Lock();



之后,就是等待解码操作的完成:

        //decode done

        while (!(NF_RDSTAT & (1<<6)));

        tempMECCBuf[0]= NF_RDMECC0();

        tempMECCBuf[1] = NF_RDMECC1();

后面这两句含有tempMECCBuf的操作可以直接删除了,没用的。处理完这个之后,紧接着,就是查找ECC错误并进行矫正了:


  pSectorBuff1 = pSectorBuff+nSectorLoop*SECTOR_SIZE;

        if (!ECC_CorrectData(startSectorAddr, pSectorBuff1, nRetEcc, ECC_CORRECT_MAIN))

        {

   RETAILMSG(1,(TEXT("ECC ERROR\n")));

            return FALSE;

        }

    }


NF_nFCE_H();


在 NF_nFCE_H(); 这句之前,我们需要把使能的一些中断关闭了:

[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONT &= ~(1<<10); // Disable illegal access interrupt control
  2. g_pNFConReg->NFCONT &= ~(1<<9); // Disable RnB interrupt






3、NAND_LB_ReadSectorInfo()

原BSP代码:

[cpp]​view plain​​​​copy​


  1. BOOL NAND_LB_ReadSectorInfo(SECTOR_ADDR sectorAddr, PSectorInfo pInfo)
  2. {
  3. BOOL bRet = TRUE;
  4. int NewSpareAddr = 4096; //gjl 2048
  5. int NewSectorAddr = sectorAddr;
  6. DWORD MECCBuf[16]; // gjl 8
  7. UINT16 nSectorLoop, i;
  8. UINT8 TempInfo[40];
  9. #if CHECK_SPAREECC
  10. DWORD SECCBuf[4]; //gjl 2
  11. UINT32 nRetEcc = 0;
  12. #endif

  13. NF_nFCE_L();

  14. NF_CLEAR_RB();

  15. NF_CMD(CMD_READ); // Send read confirm command.

  16. NF_ADDR((NewSpareAddr)&0xff);
  17. NF_ADDR((NewSpareAddr>>8)&0xff);
  18. NF_ADDR((NewSectorAddr)&0xff);
  19. NF_ADDR((NewSectorAddr>>8) & 0xff);
  20. #if LB_NEED_EXT_ADDR
  21. NF_ADDR((NewSectorAddr >> 16) & 0xff);
  22. #endif

  23. NF_CMD(CMD_READ3);

  24. NF_DETECT_RB();


  25. pInfo->bBadBlock = NF_RDDATA_BYTE();
  26. pInfo->dwReserved1 = NF_RDDATA_WORD();
  27. pInfo->bOEMReserved = NF_RDDATA_BYTE();

  28. pInfo->wReserved2 = NF_RDDATA_BYTE();
  29. pInfo->wReserved2 |= (NF_RDDATA_BYTE()<<8);

  30. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*2; nSectorLoop++)
  31. {
  32. MECCBuf[nSectorLoop] = NF_RDDATA_WORD();
  33. }

  34. NF_nFCE_H();
  35. #if (NAND_DEBUG)
  36. RETAILMSG(1,(TEXT("#### FMD_DRIVER:::56565656\n")));
  37. #endif

  38. return bRet;
  39. }


还是先看定义的 DWORD MECCBuf[16]; // gjl 8

这个我们要改成:

[cpp]​view plain​​​​copy​

  1. DWORD MECCBuf[32];



接着在 NF_nFCE_L(); 操作之前,添加:

[cpp]​view plain​​​​copy​


  1. NF_ECCTYPE_8BIT(); // use 8bit ECC type
  2. NF_ECC_8BIT_STOP(); // init 8bit ECC decoding



然后,又看到读取ECC的操作:

    for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*2; nSectorLoop++)

    {

        MECCBuf[nSectorLoop] = NF_RDDATA_WORD();

    }

这个,我们需要改成:

[cpp]​view plain​​​​copy​


  1. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*4; nSectorLoop++)
  2. {
  3. MECCBuf[nSectorLoop] = NF_RDDATA_WORD();
  4. }






4、FMD_SB_ReadSector()

[cpp]​view plain​​​​copy​


  1. BOOL FMD_SB_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
  2. {
  3. ULONG SectorAddr = (ULONG)startSectorAddr;
  4. ULONG MECC;
  5. UINT32 nRet = TRUE;
  6. UINT32 nRetEcc = 0;

  7. #if (NAND_DEBUG)
  8. RETAILMSG(1,(TEXT("#### FMD_DRIVER:::FMD_sbreadT \n")));
  9. #endif

  10. if (!pSectorBuff && !pSectorInfoBuff)
  11. {
  12. RETAILMSG(1,(TEXT("[FMD:ERR] FMD_SB_ReadSector(0x%08x, 0x%08x) : Invalid Parameter\n"), pSectorBuff, pSectorInfoBuff));
  13. return(FALSE);
  14. }

  15. while (dwNumSectors--)
  16. {
  17. NF_RSTECC();
  18. NF_MECC_UnLock();
  19. NF_nFCE_L();

  20. if (!pSectorBuff)
  21. {
  22. NF_CLEAR_RB();
  23. NF_CMD(CMD_READ2); // Send read confirm command.

  24. NF_ADDR(0); // Ignored.
  25. NF_ADDR(SectorAddr & 0xff); // Page address.
  26. NF_ADDR((SectorAddr >> 8) & 0xff);
  27. #if SB_NEED_EXT_ADDR
  28. NF_ADDR((SectorAddr >> 16) & 0xff);
  29. #endif

  30. NF_DETECT_RB();

  31. RdPageInfo((PBYTE)pSectorInfoBuff); // Read page/sector information.

  32. pSectorInfoBuff++;
  33. }
  34. else
  35. {
  36. NF_CLEAR_RB();

  37. NF_CMD(CMD_READ); // Send read command.

  38. NF_ADDR(0); // Column = 0.
  39. NF_ADDR(SectorAddr & 0xff); // Page address.
  40. NF_ADDR((SectorAddr >> 8) & 0xff);
  41. #if SB_NEED_EXT_ADDR
  42. NF_ADDR((SectorAddr >> 16) & 0xff);
  43. #endif

  44. NF_DETECT_RB(); // Wait for command to complete.

  45. if( ((DWORD) pSectorBuff) & 0x3)
  46. {
  47. RdPage512Unalign (pSectorBuff);
  48. }
  49. else
  50. {
  51. RdPage512(pSectorBuff); // Read page/sector data.
  52. }

  53. NF_MECC_Lock();

  54. if (pSectorInfoBuff)
  55. {
  56. RdPageInfo((PBYTE)pSectorInfoBuff); // Read page/sector information.
  57. pSectorInfoBuff ++;
  58. }
  59. else
  60. {
  61. BYTE TempInfo[8];
  62. RdPageInfo(TempInfo); // Read page/sector information.
  63. }

  64. MECC = NF_RDDATA_BYTE() << 0;
  65. MECC |= NF_RDDATA_BYTE() << 8;
  66. MECC |= NF_RDDATA_BYTE() << 16;
  67. MECC |= (NF_RDMECC0() &0xff000000);
  68. //MECC |= NF_RDDATA_BYTE() << 24;

  69. NF_WRMECCD0( ((MECC&0xff00)<<8)|(MECC&0xff) );
  70. NF_WRMECCD1( ((MECC&0xff000000)>>8)|((MECC&0xff0000)>>16) );

  71. nRetEcc = NF_ECC_ERR0;

  72. switch(nRetEcc & 0x3)
  73. {
  74. case 0: // No Error
  75. nRet = TRUE;
  76. break;
  77. case 1: // 1-bit Error(Correctable)
  78. RETAILMSG(1,(TEXT("ECC correctable error(0x%x)\n"), SectorAddr));
  79. (pSectorBuff)[(nRetEcc>>7)&0x7ff] ^= (1<<((nRetEcc>>4)&0x7));
  80. nRet = TRUE;
  81. break;
  82. case 2: // Multiple Error
  83. RETAILMSG(1,(TEXT("ECC Uncorrectable error(0x%x)\n"), SectorAddr));
  84. nRet = FALSE;
  85. break;
  86. case 3: // ECC area Error
  87. RETAILMSG(1,(TEXT("ECC area error\n")));
  88. default:
  89. nRet = FALSE;
  90. break;
  91. }
  92. pSectorBuff += NAND_SECTOR_SIZE;
  93. }
  94. NF_nFCE_H();
  95. ++SectorAddr;
  96. }

  97. return(nRet);
  98. }


FMD_SB_ReadSector()是介绍SLC读取操作的,这里不用修改。



到此,这一篇就把ECC矫正和读取数据的部分给搞掂了。下一篇将介绍写数据的部分

近段时间比较忙,接着上一篇,这篇主要介绍到写的部分


1、FMD_LB_WriteSector() MLC写sector

[cpp]​view plain​​​​copy​


  1. BOOL FMD_LB_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
  2. {
  3. DWORD i;
  4. BOOL bRet = TRUE;
  5. volatile DWORD wrdata;
  6. DWORD MECCBuf[16]; // 16 gjl
  7. UINT16 nSectorLoop;
  8. int NewSpareAddr = 4096; // 2048 gjl
  9. int NewDataAddr = 0;
  10. int NewSectorAddr = startSectorAddr;
  11. #if CHECK_SPAREECC
  12. DWORD SECCBuf[4]; // 2 gjl
  13. #endif

  14. #if (NAND_DEBUG)
  15. RETAILMSG(1, (TEXT("FMD::FMD_LB_WriteSector 0x%x \n"), startSectorAddr));
  16. #endif

  17. if (!pSectorBuff && !pSectorInfoBuff)
  18. return(FALSE);

  19. if ( dwNumSectors > 1 )
  20. {
  21. RETAILMSG(1, (TEXT("######## FATAL ERROR => FMD::FMD_WriteSector->dwNumsectors is bigger than 1. \n")));
  22. return FALSE;
  23. }

  24. if (!pSectorBuff)
  25. {
  26. NAND_LB_WriteSectorInfo(startSectorAddr, pSectorInfoBuff);
  27. return TRUE;
  28. }

  29. // Enable Chip
  30. NF_nFCE_L();

  31. NF_CLEAR_RB();

  32. // Issue command
  33. NF_CMD(CMD_WRITE);

  34. // Setup address
  35. NF_ADDR((NewDataAddr)&0xff);
  36. NF_ADDR((NewDataAddr>>8)&0xff);
  37. NF_ADDR((NewSectorAddr)&0xff);
  38. NF_ADDR((NewSectorAddr>>8)&0xff);
  39. #if LB_NEED_EXT_ADDR
  40. NF_ADDR((NewSectorAddr>>16)&0xff);
  41. #endif

  42. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE; nSectorLoop++)
  43. {
  44. // Initialize ECC register
  45. NF_MSGLENGTH_512();
  46. NF_ECCTYPE_4BIT();

  47. NF_RSTECC();
  48. NF_MECC_UnLock();

  49. // Special case to handle un-aligned buffer pointer.
  50. //
  51. if( ((DWORD) (pSectorBuff+nSectorLoop*SECTOR_SIZE)) & 0x3)
  52. {
  53. // Write the data
  54. for(i=0; i<SECTOR_SIZE/sizeof(DWORD); i++)
  55. {
  56. wrdata = (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+0];
  57. wrdata |= (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+1]<<8;
  58. wrdata |= (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+2]<<16;
  59. wrdata |= (pSectorBuff+nSectorLoop*SECTOR_SIZE)[i*4+3]<<24;
  60. NF_WRDATA_WORD(wrdata);
  61. }
  62. }
  63. else
  64. {
  65. WrPage512(pSectorBuff+nSectorLoop*SECTOR_SIZE);
  66. }

  67. // Read out the ECC value generated by HW
  68. NF_MECC_Lock();
  69. //encode done
  70. while (!(NF_RDSTAT & (1<<7)));

  71. MECCBuf[2*nSectorLoop] = NF_RDMECC0();
  72. MECCBuf[2*nSectorLoop+1] = NF_RDMECC1();
  73. #if DEBUG_WRITE_READ_EQUAL
  74. g_MECCBuf[2*nSectorLoop] = MECCBuf[2*nSectorLoop];
  75. g_MECCBuf[2*nSectorLoop+1] = MECCBuf[2*nSectorLoop+1];
  76. #endif
  77. }

  78. NF_CMD(CMD_RDI);
  79. NF_ADDR((NewSpareAddr)&0xff);
  80. NF_ADDR((NewSpareAddr>>8)&0xff);

  81. // Write the SectorInfo data to the media
  82. // NOTE: This hardware is odd: only a byte can be written at a time and it must reside in the
  83. // upper byte of a USHORT.
  84. if(pSectorInfoBuff)
  85. {

  86. // Write the first reserved field (DWORD)
  87. NF_WRDATA_BYTE(pSectorInfoBuff->bBadBlock);
  88. NF_WRDATA_WORD(pSectorInfoBuff->dwReserved1);
  89. NF_WRDATA_BYTE(pSectorInfoBuff->bOEMReserved);

  90. NF_WRDATA_BYTE(pSectorInfoBuff->wReserved2&0xff);
  91. NF_WRDATA_BYTE((pSectorInfoBuff->wReserved2>>8)&0xff);
  92. }
  93. else
  94. {
  95. // Make sure we advance the Flash's write pointer (even though we aren't writing the SectorInfo data)
  96. for(i=0; i<sizeof(SectorInfo)/sizeof(DWORD); i++)
  97. {
  98. NF_WRDATA_WORD(0xffffffff);
  99. }
  100. }

  101. // Write the ECC value to the flash
  102. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*2; nSectorLoop++)
  103. NF_WRDATA_WORD(MECCBuf[nSectorLoop]);

  104. // Finish up the write operation
  105. NF_CMD(CMD_WRITE2);

  106. // Wait for RB
  107. NF_DETECT_RB(); // Wait tR(max 12us)

  108. if ( NF_RDSTAT & STATUS_ILLACC )
  109. {
  110. RETAILMSG(1, (TEXT("FMD_WriteSector() ######## Error Programming page (Illigar Access) %d!\n"), startSectorAddr));
  111. g_pNFConReg->NFSTAT = STATUS_ILLACC; // Write 1 to clear.
  112. bRet = FALSE;
  113. }
  114. else
  115. {
  116. // Check the status
  117. NF_CMD(CMD_STATUS);

  118. if(NF_RDDATA_BYTE() & STATUS_ERROR)
  119. {
  120. RETAILMSG(1, (TEXT("FMD_WriteSector() ######## Error Programming page %d!\n"), startSectorAddr));
  121. bRet = FALSE;
  122. }
  123. }

  124. // Disable the chip
  125. NF_nFCE_H();

  126. return bRet;
  127. }


这个是飞凌的源码,这里需要修改成8bit的ECC。

变量定义这里需要把 DWORD MECCBuf[16]; 修改成:

[cpp]​view plain​​​​copy​

  1. DWORD MECCBuf[32];



    if (!pSectorBuff)

    {

        NAND_LB_WriteSectorInfo(startSectorAddr, pSectorInfoBuff);

        return TRUE;

    }

操作之前,添加使能中断的操作:

[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONT |= (1<<10); // Enable illegal access interrupt control
  2. g_pNFConReg->NFCONT |= (1<<9); // Enable RnB interrupt



for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE; nSectorLoop++)这里是循环写一个block的页数据。

        NF_MSGLENGTH_512();

        NF_ECCTYPE_4BIT();

中的 NF_ECCTYPE_4BIT(); 修改成:

[cpp]​view plain​​​​copy​


  1. NF_ECCTYPE_8BIT();
  2. NF_ECC_8BIT_STOP();
  3. NF_ECC_DIRECTION_OUT();



//encode done

        while (!(NF_RDSTAT & (1<<7)));

处理的后面,把

        MECCBuf[2*nSectorLoop] = NF_RDMECC0();

        MECCBuf[2*nSectorLoop+1] = NF_RDMECC1();

修改成:

[cpp]​view plain​​​​copy​


  1. MECCBuf[4*nSectorLoop] = NF_RDM8ECC0();
  2. MECCBuf[4*nSectorLoop+1] = NF_RDM8ECC1();
  3. MECCBuf[4*nSectorLoop+2] = NF_RDM8ECC2();
  4. MECCBuf[4*nSectorLoop+3] = NF_RDM8ECC3() & 0xff;



在for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE; nSectorLoop++)循环结束的后面,

NF_CMD(CMD_RDI);

    NF_ADDR((NewSpareAddr)&0xff);

    NF_ADDR((NewSpareAddr>>8)&0xff);

操作之前,添加:

[cpp]​view plain​​​​copy​


  1. NF_ECCTYPE_8BIT();
  2. NF_ECC_8BIT_STOP();



看到写入ECC数据部分:

    //  Write the ECC value to the flash

    for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*2; nSectorLoop++)

        NF_WRDATA_WORD(MECCBuf[nSectorLoop]);

这里需要把for修改成:

[cpp]​view plain​​​​copy​

  1. for (nSectorLoop = 0; nSectorLoop < SECTORS_PER_PAGE*4; nSectorLoop++)



在片选操作 

   //  Disable the chip

    NF_nFCE_H();

之前添加:

[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONT &= ~(1<<10); // Disable illegal access interrupt control
  2. g_pNFConReg->NFCONT &= ~(1<<9); // Disable RnB interrupt



到此,写入部分修改完毕




2、NAND_LB_WriteSectorInfo() 写sector信息

[cpp]​view plain​​​​copy​


  1. BOOL NAND_LB_WriteSectorInfo(SECTOR_ADDR sectorAddr, PSectorInfo pInfo)
  2. {
  3. BOOL bRet = TRUE;
  4. int NewSpareAddr = 4096; // gjl 2048
  5. int NewSectorAddr = sectorAddr;
  6. #if CHECK_SPAREECC
  7. DWORD SECCBuf[4]; // gjl 2
  8. #endif

  9. // Chip enable
  10. NF_nFCE_L();

  11. NF_CLEAR_RB();

  12. // Write the command
  13. // First, let's point to the spare area
  14. NF_CMD(CMD_WRITE);

  15. // Write the address
  16. NF_ADDR((NewSpareAddr)&0xff);
  17. NF_ADDR((NewSpareAddr>>8)&0xff);
  18. NF_ADDR((NewSectorAddr)&0xff);
  19. NF_ADDR((NewSectorAddr>>8)&0xff);
  20. #if LB_NEED_EXT_ADDR
  21. NF_ADDR((NewSectorAddr>>16)&0xff);
  22. #endif
  23. NF_MSGLENGTH_512();
  24. NF_ECCTYPE_4BIT();


  25. // Now let's write the SectorInfo data
  26. //
  27. // Write the first reserved field (DWORD)
  28. NF_WRDATA_BYTE(pInfo->bBadBlock);
  29. NF_WRDATA_WORD(pInfo->dwReserved1);
  30. NF_WRDATA_BYTE(pInfo->bOEMReserved);

  31. NF_WRDATA_BYTE(pInfo->wReserved2&0xff);
  32. NF_WRDATA_BYTE((pInfo->wReserved2>>8)&0xff);

  33. NF_WRDATA_WORD(0xffffffff); // Mecc[0]
  34. NF_WRDATA_WORD(0xffffffff); // Mecc[1]
  35. NF_WRDATA_WORD(0xffffffff); // Mecc[2]
  36. NF_WRDATA_WORD(0xffffffff); // Mecc[3]
  37. NF_WRDATA_WORD(0xffffffff); // Mecc[4]
  38. NF_WRDATA_WORD(0xffffffff); // Mecc[5]
  39. NF_WRDATA_WORD(0xffffffff); // Mecc[6]
  40. NF_WRDATA_WORD(0xffffffff); // Mecc[7]


  41. // Issue the write complete command
  42. NF_CMD(CMD_WRITE2);

  43. // Check ready bit
  44. NF_DETECT_RB(); // Wait tR(max 12us)

  45. if ( NF_RDSTAT & STATUS_ILLACC )
  46. {
  47. RETAILMSG(1, (TEXT("NAND_LB_WriteSectorInfo() ######## Error Programming page (Illigar Access) %d!\n"), sectorAddr));
  48. g_pNFConReg->NFSTAT = STATUS_ILLACC; // Write 1 to clear.
  49. bRet = FALSE;
  50. }
  51. else
  52. {
  53. // Check the status of program
  54. NF_CMD(CMD_STATUS);

  55. if( NF_RDDATA_BYTE() & STATUS_ERROR) {
  56. RETAILMSG(1, (TEXT("NAND_LB_WriteSectorInfo() ######## Error Programming page %d!\n"), sectorAddr));
  57. bRet = FALSE;
  58. }
  59. }

  60. NF_nFCE_H();

  61. return bRet;
  62. }


这个是飞凌给的源码,从上面可以看出也是只用到了4bit的ECC。下面我们将其改成8bit的ECC:

看到

    NF_MSGLENGTH_512();

    NF_ECCTYPE_4BIT();

这两个处理,我们把其中的 NF_ECCTYPE_4BIT(); 修改成

[cpp]​view plain​​​​copy​


  1. NF_ECCTYPE_8BIT(); // use 8bit ECC type
  2. NF_ECC_8BIT_STOP(); // init 8bit ECC decoding



然后,就开始写入sector的信息数据了,这里主要看写入主ECC数据部分,原文的是

    NF_WRDATA_WORD(0xffffffff);  // Mecc[0]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[1]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[2]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[3]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[4]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[5]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[6]

    NF_WRDATA_WORD(0xffffffff);  // Mecc[7]

这里需要修改成:

[cpp]​view plain​​​​copy​


  1. // 16 byte Sector0 ECC data
  2. NF_WRDATA_WORD(0xffffffff); // Mecc[0]
  3. NF_WRDATA_WORD(0xffffffff); // Mecc[1]
  4. NF_WRDATA_WORD(0xffffffff); // Mecc[2]
  5. NF_WRDATA_WORD(0xffffffff); // Mecc[3]
  6. // 16 byte Sector1 ECC data
  7. NF_WRDATA_WORD(0xffffffff); // Mecc[4]
  8. NF_WRDATA_WORD(0xffffffff); // Mecc[5]
  9. NF_WRDATA_WORD(0xffffffff); // Mecc[6]
  10. NF_WRDATA_WORD(0xffffffff); // Mecc[7]
  11. // 16 byte Sector2 ECC data
  12. NF_WRDATA_WORD(0xffffffff); // Mecc[8]
  13. NF_WRDATA_WORD(0xffffffff); // Mecc[9]
  14. NF_WRDATA_WORD(0xffffffff); // Mecc[10]
  15. NF_WRDATA_WORD(0xffffffff); // Mecc[11]
  16. // 16 byte Sector3 ECC data
  17. NF_WRDATA_WORD(0xffffffff); // Mecc[12]
  18. NF_WRDATA_WORD(0xffffffff); // Mecc[13]
  19. NF_WRDATA_WORD(0xffffffff); // Mecc[14]
  20. NF_WRDATA_WORD(0xffffffff); // Mecc[15]
  21. // 16 byte Sector4 ECC data
  22. NF_WRDATA_WORD(0xffffffff); // Mecc[16]
  23. NF_WRDATA_WORD(0xffffffff); // Mecc[17]
  24. NF_WRDATA_WORD(0xffffffff); // Mecc[18]
  25. NF_WRDATA_WORD(0xffffffff); // Mecc[19]
  26. // 16 byte Sector5 ECC data
  27. NF_WRDATA_WORD(0xffffffff); // Mecc[20]
  28. NF_WRDATA_WORD(0xffffffff); // Mecc[21]
  29. NF_WRDATA_WORD(0xffffffff); // Mecc[22]
  30. NF_WRDATA_WORD(0xffffffff); // Mecc[23]
  31. // 16 byte Sector6 ECC data
  32. NF_WRDATA_WORD(0xffffffff); // Mecc[24]
  33. NF_WRDATA_WORD(0xffffffff); // Mecc[25]
  34. NF_WRDATA_WORD(0xffffffff); // Mecc[26]
  35. NF_WRDATA_WORD(0xffffffff); // Mecc[27]
  36. // 16 byte Sector7 ECC data
  37. NF_WRDATA_WORD(0xffffffff); // Mecc[28]
  38. NF_WRDATA_WORD(0xffffffff); // Mecc[29]
  39. NF_WRDATA_WORD(0xffffffff); // Mecc[30]
  40. NF_WRDATA_WORD(0xffffffff); // Mecc[31]



该函数中剩余的部分就不需要修改了。

这里之所以这样修改,是因为每512字节的主数据就产生4*4字节的ECC,而我们这里每个block有4096字节,即8个512字节,也就是8 * 16个字节的ECC数据。




3、FMD_SB_WriteSector()

[cpp]​view plain​​​​copy​


  1. BOOL FMD_SB_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
  2. {
  3. BYTE Status;
  4. ULONG SectorAddr = (ULONG)startSectorAddr;
  5. ULONG MECC;

  6. if (!pSectorBuff && !pSectorInfoBuff)
  7. return(FALSE);

  8. #if (NAND_DEBUG)
  9. RETAILMSG(1,(TEXT("#### FMD_DRIVER:::FMD_sbwrite \n")));
  10. #endif

  11. NF_nFCE_L(); // Select the flash chip.

  12. while (dwNumSectors--)
  13. {
  14. if (!pSectorBuff) // Only spare area
  15. {
  16. // If we are asked just to write the SectorInfo, we will do that separately
  17. NF_CMD(CMD_READ2); // Send read command.

  18. NF_CMD(CMD_WRITE); // Send write command.
  19. NF_ADDR(0); // Column = 0.
  20. NF_ADDR(SectorAddr & 0xff); // Page address.
  21. NF_ADDR((SectorAddr >> 8) & 0xff);
  22. #if SB_NEED_EXT_ADDR
  23. NF_ADDR((SectorAddr >> 16) & 0xff);
  24. #endif

  25. // Write the SectorInfo data to the media.
  26. // Spare area[7:0]
  27. WrPageInfo((PBYTE)pSectorInfoBuff);

  28. NF_CLEAR_RB();
  29. NF_CMD(CMD_WRITE2); // Send write confirm command.
  30. NF_DETECT_RB();

  31. NF_CMD(CMD_STATUS);
  32. Status = NF_RDDATA_BYTE(); // Read command status.

  33. if (Status & STATUS_ERROR)
  34. {
  35. NF_nFCE_H(); // Deselect the flash chip.
  36. //SetKMode (bLastMode);
  37. return(FALSE);
  38. }
  39. pSectorInfoBuff++;
  40. }
  41. else // Main area+Spare area.
  42. {
  43. NF_CMD(CMD_READ); // Send read command.
  44. NF_CMD(CMD_WRITE); // Send write command.
  45. NF_ADDR(0); // Column = 0.
  46. NF_ADDR(SectorAddr & 0xff); // Page address.
  47. NF_ADDR((SectorAddr >> 8) & 0xff);
  48. #if SB_NEED_EXT_ADDR
  49. NF_ADDR((SectorAddr >> 16) & 0xff);
  50. #endif

  51. // Special case to handle un-aligned buffer pointer.
  52. NF_RSTECC();
  53. NF_MECC_UnLock();
  54. if( ((DWORD) pSectorBuff) & 0x3)
  55. {
  56. WrPage512Unalign (pSectorBuff);
  57. }
  58. else
  59. {
  60. WrPage512(pSectorBuff); // Write page/sector data.
  61. }
  62. NF_MECC_Lock();

  63. // Write the SectorInfo data to the media.
  64. // Spare area[7:0]
  65. if(pSectorInfoBuff)
  66. {
  67. WrPageInfo((PBYTE)pSectorInfoBuff);
  68. pSectorInfoBuff++;
  69. }
  70. else // Make sure we advance the Flash's write pointer (even though we aren't writing the SectorInfo data)
  71. {
  72. BYTE TempInfo[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  73. WrPageInfo(TempInfo);
  74. }

  75. // Write the SectorInfo data to the media.
  76. // Spare area[11:8]
  77. // Get the ECC data from status register.
  78. MECC = NF_RDMECC0();
  79. // Now, Write the ECC data to Spare area[11:8]
  80. NF_WRDATA_BYTE((UCHAR)((MECC ) & 0xff)); // Spare area offset 8
  81. NF_WRDATA_BYTE((UCHAR)((MECC >> 8) & 0xff)); // Spare area offset 9
  82. NF_WRDATA_BYTE((UCHAR)((MECC >> 16) & 0xff)); // Spare area offset 10
  83. NF_WRDATA_BYTE((UCHAR)((MECC >> 24) & 0xff)); // Spare area offset 11

  84. NF_CLEAR_RB();
  85. NF_CMD(CMD_WRITE2); // Send write confirm command.
  86. NF_DETECT_RB();

  87. do
  88. {
  89. NF_CMD(CMD_STATUS);
  90. Status = NF_RDDATA_BYTE(); // Read command status.
  91. }while(!(Status & STATUS_READY));

  92. if (Status & STATUS_ERROR)
  93. {
  94. NF_nFCE_H(); // Deselect the flash chip.
  95. return(FALSE);
  96. }
  97. pSectorBuff += NAND_SECTOR_SIZE;
  98. }
  99. ++SectorAddr;
  100. }
  101. NF_nFCE_H(); // Deselect the flash chip.

  102. return(TRUE);
  103. }



这部分就不需要修改了,上面的代码是处理SLC部分的




4、FMD_LB_EraseBlock() 擦出块

[cpp]​view plain​​​​copy​


  1. BOOL FMD_LB_EraseBlock(BLOCK_ID blockID)
  2. {
  3. BOOL bRet = TRUE;
  4. DWORD dwPageID = blockID << LB_NAND_LOG_2_PAGES_PER_BLOCK;

  5. #if (NAND_DEBUG)
  6. RETAILMSG(1, (TEXT("FMD_LB_EraseBlock 0x%x \n"), blockID));
  7. #endif

  8. // Enable the chip
  9. NF_nFCE_L(); // Select the flash chip.

  10. NF_CLEAR_RB();
  11. NF_MSGLENGTH_512();
  12. // Issue command
  13. NF_CMD(CMD_ERASE);

  14. // Set up address
  15. NF_ADDR((dwPageID) & 0xff);
  16. NF_ADDR((dwPageID >> 8) & 0xff);
  17. #if LB_NEED_EXT_ADDR
  18. NF_ADDR((dwPageID >> 16) & 0xff);
  19. #endif

  20. // Complete erase operation
  21. NF_CMD(CMD_ERASE2);

  22. // Wait for ready bit
  23. NF_DETECT_RB(); // Wait tR(max 12us)

  24. if ( NF_RDSTAT & STATUS_ILLACC )
  25. {
  26. RETAILMSG(1, (TEXT("LB######## Error Erasing block (Illigar Access) %d!\n"), blockID));
  27. g_pNFConReg->NFSTAT = STATUS_ILLACC; // Write 1 to clear.
  28. bRet = FALSE;
  29. }
  30. else
  31. {
  32. // Check the status
  33. NF_CMD(CMD_STATUS);

  34. if( NF_RDDATA_BYTE() & STATUS_ERROR)
  35. {
  36. RETAILMSG(1, (TEXT("LB######## Error Erasing block %d!\n"), blockID));
  37. bRet = FALSE;
  38. }
  39. }

  40. NF_nFCE_H(); // Select the flash chip.

  41. return bRet;
  42. }


上述是飞凌的源码,这里我们需要稍作修改。

在使能片选 NF_nFCE_L(); 之前,我们需要使能一些中断:

[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONT |= (1<<10); // Enable illegal access interrupt control
  2. g_pNFConReg->NFCONT |= (1<<9); // Enable RnB interrupt



擦除操作处理完之后,在 NF_nFCE_H(); 之前,需要关闭前面使能的中断:

[cpp]​view plain​​​​copy​


  1. g_pNFConReg->NFCONT &= ~(1<<10); // Disable illegal access interrupt control
  2. g_pNFConReg->NFCONT &= ~(1<<9); // Disable RnB interrupt





5、FMD_SB_EraseBlock()

[cpp]​view plain​​​​copy​


  1. BOOL FMD_SB_EraseBlock(BLOCK_ID blockID)
  2. {
  3. BOOL bRet = TRUE;
  4. DWORD dwPageID = blockID << SB_NAND_LOG_2_PAGES_PER_BLOCK;

  5. // Enable the chip
  6. NF_nFCE_L(); // Select the flash chip.

  7. // Issue command
  8. NF_CMD(CMD_ERASE);

  9. // Set up address
  10. NF_ADDR((dwPageID) & 0xff);
  11. NF_ADDR((dwPageID >> 8) & 0xff);
  12. #if SB_NEED_EXT_ADDR
  13. NF_ADDR((dwPageID >> 16) & 0xff);
  14. #endif

  15. NF_CLEAR_RB();

  16. // Complete erase operation
  17. NF_CMD(CMD_ERASE2);

  18. // Wait for ready bit
  19. NF_DETECT_RB(); // Wait tR(max 12us)

  20. if ( NF_RDSTAT & STATUS_ILLACC )
  21. {
  22. RETAILMSG(1, (TEXT("SB######## Error Erasing block (Illigar Access) %d!\n"), blockID));
  23. g_pNFConReg->NFSTAT = STATUS_ILLACC; // Write 1 to clear.
  24. bRet = FALSE;
  25. }
  26. else
  27. {
  28. // Check the status
  29. NF_CMD(CMD_STATUS);

  30. if( NF_RDDATA_BYTE() & STATUS_ERROR)
  31. {
  32. RETAILMSG(1, (TEXT("SB######## Error Erasing block %d!\n"), blockID));
  33. bRet = FALSE;
  34. }
  35. }

  36. NF_nFCE_H(); // Select the flash chip.

  37. return bRet;
  38. }



SLC擦除块操作,这里不需要修改




6、FMD_LB_GetBlockStatus() MLC获取块的状态

[cpp]​view plain​​​​copy​


  1. DWORD FMD_LB_GetBlockStatus(BLOCK_ID blockID)
  2. {
  3. BLOCK_ID blockID_bad = blockID + 1;
  4. SECTOR_ADDR sectorAddr = (blockID_bad << LB_NAND_LOG_2_PAGES_PER_BLOCK) - 1;
  5. SectorInfo SI;
  6. DWORD dwResult = 0;

  7. //RETAILMSG(1, (TEXT("FMD_LB_GetBlockStatus (0x%x)0x%x \n"), blockID, sectorAddr));

  8. if(!FMD_LB_ReadSector(sectorAddr, NULL, &SI, 1))
  9. {
  10. return BLOCK_STATUS_UNKNOWN;
  11. }

  12. if(!(SI.bOEMReserved & OEM_BLOCK_READONLY))
  13. {
  14. dwResult |= BLOCK_STATUS_READONLY;
  15. }

  16. if (!(SI.bOEMReserved & OEM_BLOCK_RESERVED))
  17. {
  18. dwResult |= BLOCK_STATUS_RESERVED;
  19. }

  20. if(SI.bBadBlock != 0xFF)
  21. {
  22. dwResult |= BLOCK_STATUS_BAD;
  23. }

  24. return dwResult;
  25. }






7、FMD_SB_GetBlockStatus() SLC读取块状态

[cpp]​view plain​​​​copy​


  1. DWORD FMD_SB_GetBlockStatus(BLOCK_ID blockID)
  2. {
  3. SECTOR_ADDR sectorAddr = blockID << SB_NAND_LOG_2_PAGES_PER_BLOCK;
  4. SectorInfo SI;
  5. DWORD dwResult = 0;

  6. //RETAILMSG(1, (TEXT("FMD_SB_GetBlockStatus (0x%x)0x%x \n"), blockID, sectorAddr));

  7. if(!FMD_SB_ReadSector(sectorAddr, NULL, &SI, 1))
  8. {
  9. return BLOCK_STATUS_UNKNOWN;
  10. }

  11. if(!(SI.bOEMReserved & OEM_BLOCK_READONLY))
  12. {
  13. dwResult |= BLOCK_STATUS_READONLY;
  14. }

  15. if(SI.bBadBlock != 0xFF)
  16. {
  17. dwResult |= BLOCK_STATUS_BAD;
  18. }

  19. return dwResult;
  20. }






8、FMD_LB_SetBlockStatus()、FMD_SB_SetBlockStatus() 设置块的状态

[cpp]​view plain​​​​copy​


  1. BOOL FMD_LB_SetBlockStatus(BLOCK_ID blockID, DWORD dwStatus)
  2. {
  3. BYTE bStatus = 0;

  4. if(dwStatus & BLOCK_STATUS_BAD)
  5. {
  6. if(!LB_MarkBlockBad (blockID))
  7. {
  8. return FALSE;
  9. }
  10. }

  11. // We don't currently support setting a block to read-only, so fail if request is
  12. // for read-only and block is not currently read-only.
  13. if(dwStatus & BLOCK_STATUS_READONLY)
  14. {
  15. if(!(FMD_LB_GetBlockStatus(blockID) & BLOCK_STATUS_READONLY))
  16. {
  17. return FALSE;
  18. }
  19. }

  20. return TRUE;
  21. }

  22. BOOL FMD_SB_SetBlockStatus(BLOCK_ID blockID, DWORD dwStatus)
  23. {
  24. SECTOR_ADDR sectorAddr = blockID << SB_NAND_LOG_2_PAGES_PER_BLOCK;
  25. BYTE bStatus = 0;

  26. //RETAILMSG(1,(TEXT("#### FMD_DRIVER:::FMD_sbsetblock \n")));

  27. if(dwStatus & BLOCK_STATUS_BAD)
  28. {
  29. if(!SB_MarkBlockBad (blockID))
  30. {
  31. return FALSE;
  32. }
  33. }

  34. // We don't currently support setting a block to read-only, so fail if request is
  35. // for read-only and block is not currently read-only.
  36. if(dwStatus & BLOCK_STATUS_READONLY)
  37. {
  38. if(!(FMD_SB_GetBlockStatus(blockID) & BLOCK_STATUS_READONLY))
  39. {
  40. return FALSE;
  41. }
  42. }

  43. return TRUE;
  44. }






9、LB_MarkBlockBad()、SB_MarkBlockBad() 标记坏块

[cpp]​view plain​​​​copy​


  1. BOOL LB_MarkBlockBad(BLOCK_ID blockID)
  2. {
  3. BLOCK_ID blockID_bad = blockID + 1;
  4. DWORD dwStartPage = (blockID_bad << LB_NAND_LOG_2_PAGES_PER_BLOCK) - 1;
  5. BOOL bRet = TRUE;

  6. // RETAILMSG(1, (TEXT("LB_MarkBlockBad 0x%x \n"), dwStartPage));

  7. // Enable chip
  8. NF_nFCE_L();
  9. NF_CLEAR_RB();
  10. NF_MSGLENGTH_512();
  11. // Issue command
  12. // We are dealing with spare area
  13. NF_CMD(CMD_WRITE);

  14. // Set up address
  15. NF_ADDR((4096+LB_POS_BADBLOCK)&0xff); // gjl 2048
  16. NF_ADDR(((4096+LB_POS_BADBLOCK)>>8)&0xff); // gjl 2048
  17. NF_ADDR((dwStartPage) & 0xff);
  18. NF_ADDR((dwStartPage >> 8) & 0xff);
  19. #if LB_NEED_EXT_ADDR
  20. NF_ADDR((dwStartPage >> 16) & 0xff);
  21. #endif

  22. NF_WRDATA_BYTE(BADBLOCKMARK);

  23. // Copmlete the write
  24. NF_CMD(CMD_WRITE2);

  25. // Wait for RB
  26. NF_DETECT_RB(); // Wait tR(max 12us)

  27. if ( NF_RDSTAT & STATUS_ILLACC )
  28. {
  29. RETAILMSG(1, (TEXT("NAND_LB_WriteSectorInfo() ######## Error Programming page (Illigar Access) %d!\n")));
  30. g_pNFConReg->NFSTAT = STATUS_ILLACC; // Write 1 to clear.
  31. bRet = FALSE;
  32. }
  33. else
  34. {
  35. // Check the status of program
  36. NF_CMD(CMD_STATUS);

  37. if( NF_RDDATA_BYTE() & STATUS_ERROR)
  38. {
  39. RETAILMSG(1, (TEXT("NAND_LB_WriteSectorInfo() ######## Error Programming page %d!\n")));
  40. bRet = FALSE;
  41. }
  42. }

  43. // Disable chip select
  44. NF_nFCE_H();

  45. return bRet;
  46. }

  47. BOOL SB_MarkBlockBad(BLOCK_ID blockID)
  48. {
  49. DWORD dwStartPage = blockID << SB_NAND_LOG_2_PAGES_PER_BLOCK;
  50. BOOL bRet = TRUE;

  51. //RETAILMSG(1, (TEXT("SB_MarkBlockBad 0x%x \n"), dwStartPage));

  52. // Enable chip
  53. NF_nFCE_L();
  54. NF_CLEAR_RB();

  55. // Issue command
  56. // We are dealing with spare area
  57. NF_CMD(CMD_READ2);
  58. NF_CMD(CMD_WRITE);

  59. // Set up address
  60. NF_ADDR(SB_POS_BADBLOCK);
  61. NF_ADDR((dwStartPage) & 0xff);
  62. NF_ADDR((dwStartPage >> 8) & 0xff);
  63. #if SB_NEED_EXT_ADDR
  64. NF_ADDR((dwStartPage >> 16) & 0xff);
  65. #endif

  66. NF_WRDATA_BYTE(BADBLOCKMARK);

  67. // Copmlete the write
  68. NF_CMD(CMD_WRITE2);

  69. // Wait for RB
  70. NF_DETECT_RB(); // Wait tR(max 12us)

  71. if ( NF_RDSTAT & STATUS_ILLACC )
  72. {
  73. RETAILMSG(1, (TEXT("NAND_LB_WriteSectorInfo() ######## Error Programming page (Illigar Access) %d!\n")));
  74. g_pNFConReg->NFSTAT = STATUS_ILLACC; // Write 1 to clear.
  75. bRet = FALSE;
  76. }
  77. else
  78. {
  79. // Check the status of program
  80. NF_CMD(CMD_STATUS);

  81. if( NF_RDDATA_BYTE() & STATUS_ERROR)
  82. {
  83. RETAILMSG(1, (TEXT("NAND_LB_WriteSectorInfo() ######## Error Programming page %d!\n")));
  84. bRet = FALSE;
  85. }
  86. }

  87. // Disable chip select
  88. NF_nFCE_H();

  89. return bRet;
  90. }






10、LB_IsBlockBad()、SB_IsBlockBad() 判断是否为坏块

[cpp]​view plain​​​​copy​


  1. BOOL LB_IsBlockBad(BLOCK_ID blockID)
  2. {
  3. BLOCK_ID blockID_bad = blockID + 1;
  4. DWORD dwPageID = (blockID_bad << LB_NAND_LOG_2_PAGES_PER_BLOCK) - 1;
  5. BOOL bRet = FALSE;
  6. BYTE wFlag;

  7. // Enable the chip
  8. NF_nFCE_L();
  9. NF_CLEAR_RB();
  10. NF_MSGLENGTH_512();
  11. // Issue the command
  12. NF_CMD(CMD_READ);

  13. // Set up address
  14. NF_ADDR((4096+LB_POS_BADBLOCK)&0xff); //gjl 2048
  15. NF_ADDR(((4096+LB_POS_BADBLOCK)>>8)&0xff); // gjl 2048
  16. NF_ADDR((dwPageID) & 0xff);
  17. NF_ADDR((dwPageID >> 8) & 0xff);
  18. #if LB_NEED_EXT_ADDR
  19. NF_ADDR((dwPageID >> 16) & 0xff);
  20. #endif

  21. NF_CMD(CMD_READ3);

  22. // Wait for Ready bit
  23. NF_DETECT_RB(); // Wait tR(max 12us)

  24. // Now get the byte we want
  25. wFlag = (BYTE)(NF_RDDATA_BYTE()&0xff);

  26. if(wFlag != 0xff)
  27. {
  28. RETAILMSG(1, (TEXT("FMDLB: IsBlockBad - Page #: 0x%x \n"), dwPageID));
  29. bRet = TRUE;
  30. }

  31. // Disable the chip
  32. NF_nFCE_H();

  33. return bRet;
  34. }

  35. BOOL SB_IsBlockBad(BLOCK_ID blockID)
  36. {
  37. DWORD dwPageID = blockID << SB_NAND_LOG_2_PAGES_PER_BLOCK;
  38. BOOL bRet = FALSE;
  39. BYTE wFlag;

  40. //RETAILMSG(1,(TEXT("#### FMD_DRIVER:::FMD_sbisblockbad \n")));

  41. // Enable the chip
  42. NF_nFCE_L();
  43. NF_CLEAR_RB();
  44. // Issue the command
  45. NF_CMD(CMD_READ2);

  46. // Set up address
  47. NF_ADDR(SB_POS_BADBLOCK);
  48. NF_ADDR((dwPageID) & 0xff);
  49. NF_ADDR((dwPageID >> 8) & 0xff);
  50. #if SB_NEED_EXT_ADDR
  51. NF_ADDR((dwPageID >> 16) & 0xff);
  52. #endif

  53. // Wait for Ready bit
  54. NF_DETECT_RB(); // Wait tR(max 12us)

  55. // Now get the byte we want
  56. wFlag = (BYTE) NF_RDDATA_BYTE();

  57. if(wFlag != 0xff)
  58. {
  59. RETAILMSG(1, (TEXT("FMDSB: IsBlockBad - Page #: 0x%x \n"), dwPageID));
  60. bRet = TRUE;
  61. }

  62. // Disable the chip
  63. NF_nFCE_H();

  64. return bRet;
  65. }




时间比较仓促,这个Nandflash移植系列的就到这里结束了。