Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

时间:2021-11-14 02:27:16

ECC的全称是Error Checking and Correction,是一种用于Nand的差错检测和修正算法。如果操作时序和电路稳定性不存在问题的话,NAND Flash出错的时候一般不会造成整个Block或是Page不能读取或是全部出错,而是整个Page(例如512Bytes)中只有一个或几个bit出错。ECC能纠正1个比特错误和检测2个比特错误,而且计算速度很快,但对1比特以上的错误无法纠正,对2比特以上的错误不保证能检测。
校验码生成算法:ECC校验每次对256字节的数据进行操作,包含列校验和行校验。对每个待校验的Bit位求异或,若结果为0,则表明含有偶数个1;若结果为1,则表明含有奇数个1。列校验规则如表1所示。256字节数据形成256行、8列的矩阵,矩阵每个元素表示一个Bit位。
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析
其中CP0 ~ CP5 为六个Bit位,表示Column Parity(列极性),
CP0为第0、2、4、6列的极性,CP1为第1、3、5、7列的极性,
CP2为第0、1、4、5列的极性,CP3为第2、3、6、7列的极性,
CP4为第0、1、2、3列的极性,CP5为第4、5、6、7列的极性。
用公式表示就是:CP0=Bit0^Bit2^Bit4^Bit6, 表示第0列内部256个Bit位异或之后再跟第2列256个Bit位异或,再跟第4列、第6列的每个Bit位异或,这样,CP0其实是256*4=1024个Bit位异或的结果。CP1 ~ CP5 依此类推。
行校验如下图所示

Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

其中RP0 ~ RP15 为十六个Bit位,表示Row Parity(行极性),
RP0为第0、2、4、6、….252、254 个字节的极性
RP1-----1、3、5、7……253、255 
RP2----0、1、4、5、8、9…..252、253 (处理2个Byte,跳过2个Byte)
RP3---- 2、3、6、7、10、11…..254、255 (跳过2个Byte,处理2个Byte)
RP4---- 处理4个Byte,跳过4个Byte;
RP5---- 跳过4个Byte,处理4个Byte;
RP6---- 处理8个Byte,跳过8个Byte
RP7---- 跳过8个Byte,处理8个Byte;
RP8---- 处理16个Byte,跳过16个Byte
RP9---- 跳过16个Byte,处理16个Byte;
RP10----处理32个Byte,跳过32个Byte
RP11----跳过32个Byte,处理32个Byte;
RP12----处理64个Byte,跳过64个Byte
RP13----跳过64个Byte,处理64个Byte;
RP14----处理128个Byte,跳过128个Byte
RP15----跳过128个Byte,处理128个Byte;
可见,RP0 ~ RP15 每个Bit位都是128个字节(也就是128行)即128*8=1024个Bit位求异或的结果。
综上所述,对256字节的数据共生成了6个Bit的列校验结果,16个Bit的行校验结果,共22个Bit。在Nand中使用3个字节存放校验结果,多余的两个Bit位置1。存放次序如下表所示:

Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

以K9F1208为例,每个Page页包含512字节的数据区和16字节的OOB区。前256字节数据生成3字节ECC校验码,后256字节数据生成3字节ECC校验码,共6字节ECC校验码存放在OOB区中,存放的位置为OOB区的第0、1、2和3、6、7字节。

Nand ECC校验和纠错原理及2.6.27内核ECC代码分析 MakeEccTable.rar

/*
 * =====================================================================================
 *
 *       Filename:  nand_ecc.c
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2009年06月04日 15时10分20秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Hongwang (mn), hoakee@gmail.com
 *        Company:  University of Science and Technology of China
 *
 * =====================================================================================
 */

#include <stdio.h>

#define         BIT0(x)         (((x)&0x01)>>0)
#define         BIT1(x)         (((x)&0x02)>>1)
#define         BIT2(x)         (((x)&0x04)>>2)
#define         BIT3(x)         (((x)&0x08)>>3)
#define         BIT4(x)         (((x)&0x10)>>4)
#define         BIT5(x)         (((x)&0x20)>>5)
#define         BIT6(x)         (((x)&0x40)>>6)
#define         BIT7(x)         (((x)&0x80)>>7)

unsigned ];

void MakeEccTable()
{
    ;
    unsigned char xData;

    ; i<; i++)
    {
        xData = ;
        if( BIT0(i)^BIT2(i)^BIT4(i)^BIT6(i) )   //CP0
            xData |= 0x01;
        if( BIT1(i)^BIT3(i)^BIT5(i)^BIT7(i) )   //CP1
            xData |= 0x02;
        if( BIT0(i)^BIT1(i)^BIT4(i)^BIT5(i) )   //CP2
            xData |= 0x04;
        if( BIT2(i)^BIT3(i)^BIT6(i)^BIT7(i) )   //CP3
            xData |= 0x08;
        if( BIT0(i)^BIT1(i)^BIT2(i)^BIT3(i) )   //CP4
            xData |= 0x10;
        if( BIT4(i)^BIT5(i)^BIT6(i)^BIT7(i) )   //CP5
            xData |= 0x20;

        if( BIT0(i)^BIT1(i)^BIT2(i)^BIT3(i)^BIT4(i)^BIT5(i)^BIT6(i)^BIT7(i) )
            xData |= 0x40;

        nand_ecc_precalc_table[i] = xData;

    }
}

int main()
{
    int i;

    //MakeEccTable();

    ; i<; i++)
    {
        nand_ecc_precalc_table[i] = i;
    }

    ; i<; i++ )
    {
        == )
        {
            printf("\n");
        }
        printf("0x%02X,", nand_ecc_precalc_table[i]);
    }

    printf("\n\n");
}

校验码生成算法的C语言实现

在Linux内核中ECC校验算法所在的文件为drivers/mtd/nand/nand_ecc.c,其实现有新、旧两种,在2.6.27及更早的内核中使用的程序,从2.6.28开始已经不再使用,而换成了效率更高的程序。可以在Documentation/mtd/nand_ecc.txt 文件中找到对新程序的详细介绍。

首先分析一下2.6.27内核中的ECC实现,源代码见:
http://lxr.linux.no/linux+v2.6.27/drivers/mtd/nand/nand_ecc.c

/*
 * This file contains an ECC algorithm from Toshiba that detects and
 * corrects 1 bit errors in a 256 byte block of data.
 *
 * drivers/mtd/nand/nand_ecc.c
 *
 * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com)
 *                         Toshiba America Electronics Components, Inc.
 *
 * Copyright (C) 2006 Thomas Gleixner <tglx@linutronix.de>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 or (at your option) any
 * later version.
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this file; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * As a special exception, if other files instantiate templates or use
 * macros or inline functions from these files, or you compile these
 * files and link them with other works to produce a work based on these
 * files, these files do not by themselves cause the resulting work to be
 * covered by the GNU General Public License. However the source code for
 * these files must still be made available in accordance with section (3)
 * of the GNU General Public License.
 *
 * This exception does not invalidate any other reasons why a work based on
 * this file might be covered by the GNU General Public License.
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mtd/nand_ecc.h>

/*
 * Pre-calculated 256-way 1 byte column parity
 */
static const u_char nand_ecc_precalc_table[] = {
    0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
    0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
    0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
    0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
    0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
    0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
    0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
    0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
    0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
    0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
    0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
    0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
    0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
    0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
    0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
    0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
};

/**
 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
 * @mtd:    MTD block structure
 * @dat:    raw data
 * @ecc_code:    buffer for ECC
 */
int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
               u_char *ecc_code)
{
    uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
    int i;

    /* Initialize variables */
    reg1 = reg2 = reg3 = ;

    /* Build up column parity */
    ; i < ; i++) {
        /* Get CP0 - CP5 from table */
        idx = nand_ecc_precalc_table[*dat++];
        reg1 ^= (idx & 0x3f);

        /* All bit XOR = 1 ? */
        if (idx & 0x40) {
            reg3 ^= (uint8_t) i;
            reg2 ^= ~((uint8_t) i);
        }
    }

    /* Create non-inverted ECC code from line parity */
    tmp1  = (reg3 & ; /* B7 -> B7 */
    tmp1 |= (reg2 & ; /* B7 -> B6 */
    tmp1 |= (reg3 & ; /* B6 -> B5 */
    tmp1 |= (reg2 & ; /* B6 -> B4 */
    tmp1 |= (reg3 & ; /* B5 -> B3 */
    tmp1 |= (reg2 & ; /* B5 -> B2 */
    tmp1 |= (reg3 & ; /* B4 -> B1 */
    tmp1 |= (reg2 & ; /* B4 -> B0 */

    tmp2  = (reg3 & ; /* B3 -> B7 */
    tmp2 |= (reg2 & ; /* B3 -> B6 */
    tmp2 |= (reg3 & ; /* B2 -> B5 */
    tmp2 |= (reg2 & ; /* B2 -> B4 */
    tmp2 |= (reg3 & ; /* B1 -> B3 */
    tmp2 |= (reg2 & ; /* B1 -> B2 */
    tmp2 |= (reg3 & ; /* B0 -> B1 */
    tmp2 |= (reg2 & ; /* B7 -> B0 */

    /* Calculate final ECC code */
#ifdef CONFIG_MTD_NAND_ECC_SMC
    ecc_code[] = ~tmp2;
    ecc_code[] = ~tmp1;
#else
    ecc_code[] = ~tmp1;
    ecc_code[] = ~tmp2;
#endif
    ecc_code[] = ((~reg1) << ) | 0x03;

    ;
}
EXPORT_SYMBOL(nand_calculate_ecc);

static inline int countbits(uint32_t byte)
{
    ;

    )
        res += byte & 0x01;
    return res;
}

/**
 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
 * @mtd:    MTD block structure
 * @dat:    raw data read from the chip
 * @read_ecc:    ECC from the chip
 * @calc_ecc:    the ECC calculated from raw data
 *
 * Detect and correct a 1 bit error for 256 byte block
 */
int nand_correct_data(struct mtd_info *mtd, u_char *dat,
              u_char *read_ecc, u_char *calc_ecc)
{
    uint8_t s0, s1, s2;

#ifdef CONFIG_MTD_NAND_ECC_SMC
    s0 = calc_ecc[] ^ read_ecc[];
    s1 = calc_ecc[] ^ read_ecc[];
    s2 = calc_ecc[] ^ read_ecc[];
#else
    s1 = calc_ecc[] ^ read_ecc[];
    s0 = calc_ecc[] ^ read_ecc[];
    s2 = calc_ecc[] ^ read_ecc[];
#endif
    )
        ;

    /* Check for a single bit error */
    )) & 0x55) == 0x55 &&
        ((s1 ^ (s1 >> )) & 0x55) == 0x55 &&
        ((s2 ^ (s2 >> )) & 0x54) == 0x54) {

        uint32_t byteoffs, bitnum;

        byteoffs = (s1 << ) & 0x80;
        byteoffs |= (s1 << ) & 0x40;
        byteoffs |= (s1 << ) & 0x20;
        byteoffs |= (s1 << ) & 0x10;

        byteoffs |= (s0 >> ) & 0x08;
        byteoffs |= (s0 >> ) & 0x04;
        byteoffs |= (s0 >> ) & 0x02;
        byteoffs |= (s0 >> ) & 0x01;

        bitnum = (s2 >> ) & 0x04;
        bitnum |= (s2 >> ) & 0x02;
        bitnum |= (s2 >> ) & 0x01;

        dat[byteoffs] ^= ( << bitnum);

        ;
    }

    ) | ((uint32_t)s2 <<)) == )
        ;

    return -EBADMSG;
}
EXPORT_SYMBOL(nand_correct_data);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
MODULE_DESCRIPTION("Generic NAND ECC support");

为了加快计算速度,程序中使用了一个预先计算好的列极性表。这个表中每一个元素都是unsigned char类型,表示8位二进制数。
表中8位二进制数每位的含义:

Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

这个表的意思是:对0~255这256个数,计算并存储每个数的列校验值和行校验值,以数作数组下标。比如 nand_ecc_precalc_table[ 13 ]  存储13的列校验值和行校验值,13的二进制表示为 00001101, 其CP0 =Bit0^Bit2^Bit4^Bit6 = 0;
CP1 = Bit1^Bit3^Bit5^Bit7 = 1;
CP2 = Bit0^Bit1^Bit4^Bit5 = 1;
CP3 = Bit2^Bit3^Bit6^Bit7 = 0;
CP4 = Bit0^Bit1^Bit2^Bit3 = 1;
CP5 = Bit4^Bit5^Bit6^Bit7 = 0;
其行极性RP = Bit0^Bit1^Bit2^Bit3^Bit4^Bit5^Bit6^Bit7 = 1;
则nand_ecc_precalc_table[ 13 ] 处存储的值应该是 0101 0110,即0x56.
注意,数组nand_ecc_precalc_table的下标其实是我们要校验的一个字节数据。
理解了这个表的含义,也就很容易写个程序生成这个表了。程序见附件中的 MakeEccTable.c文件。
  有了这个表,对单字节数据dat,可以直接查表 nand_ecc_precalc_table[ dat ] 得到 dat的行校验值和列校验值。 但是ECC实际要校验的是256字节的数据,需要进行256次查表,对得到的256个查表结果进行按位异或,最终结果的 Bit0 ~ Bit5 即是256字节数据的 CP0 ~ CP5.

    /* Build up column parity */
    ; i < ; i++) {
        /* Get CP0 - CP5 from table */
        idx = nand_ecc_precalc_table[*dat++];
        reg1 ^= (idx & 0x3f);

        /* All bit XOR = 1 ? */
        if (idx & 0x40) {
            reg3 ^= (uint8_t) i;
            reg2 ^= ~((uint8_t) i);
        }
    }

Reg1
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

在这里,计算列极性的过程其实是先在一个字节数据的内部计算CP0 ~ CP5, 每个字节都计算完后再与其它字节的计算结果求异或。而表1中是先对一列Bit0求异或,再去异或一列Bit2。 这两种只是计算顺序不同,结果是一致的。 因为异或运算的顺序是可交换的。

行极性的计算要复杂一些。
nand_ecc_precalc_table[] 表中的 Bit6 已经保存了每个单字节数的行极性值。对于待校验的256字节数据,分别查表,如果其行极性为1,则记录该数据所在的行索引(也就是for循环的i值),这里的行索引是很重要的,因为RP0 ~ RP15 的计算都是跟行索引紧密相关的,如RP0只计算偶数行,RP1只计算奇数行,等等。

这里的关键是理解第88和89行。Reg3和reg2都是unsigned char 型的变量,并都初始化为零。
行索引(也就是for循环里的i)的取值范围为0~255,根据表2可以得出以下规律:

RP0只计算行索引的Bit0为0的行,RP1只计算行索引的Bit0为1的行;
RP2只计算行索引的Bit1为0的行,RP3只计算行索引的Bit1为1的行;
RP4只计算行索引的Bit2为0的行,RP5只计算行索引的Bit2为1的行;
RP6只计算行索引的Bit3为0的行,RP7只计算行索引的Bit3为1的行;
RP8只计算行索引的Bit4为0的行,RP9只计算行索引的Bit4为1的行;
RP10只计算行索引的Bit5为0的行,RP11只计算行索引的Bit5为1的行;
RP12只计算行索引的Bit6为0的行,RP13只计算行索引的Bit6为1的行;
RP14只计算行索引的Bit7为0的行,RP15只计算行索引的Bit7为1的行;

已经知道,异或运算的作用是判断比特位为1的个数,跟比特位为0的个数没有关系。如果有偶数个1则异或的结果为0,如果有奇数个1则异或的结果为1。
那么,程序第88行,对所有行校验为1的行索引按位异或运算,作用便是:

判断在所有行校验为1的行中,
属于RP1计算范围内的行有多少个------由reg3的Bit 0指示,0表示有偶数个,1表示有奇数个;
属于RP3计算范围内的行有多少个------由reg3的Bit 1指示,0表示有偶数个,1表示有奇数个;
属于RP5计算范围内的行有多少个------由reg3的Bit 2指示,0表示有偶数个,1表示有奇数个;
属于RP7计算范围内的行有多少个------由reg3的Bit 3指示,0表示有偶数个,1表示有奇数个;
属于RP9计算范围内的行有多少个------由reg3的Bit 4指示,0表示有偶数个,1表示有奇数个;
属于RP11计算范围内的行有多少个------由reg3的Bit 5指示,0表示有偶数个,1表示有奇数个;
属于RP13计算范围内的行有多少个------由reg3的Bit 6指示,0表示有偶数个,1表示有奇数个;
属于RP15计算范围内的行有多少个------由reg3的Bit 7指示,0表示有偶数个,1表示有奇数个;

所以,reg3每个Bit位的作用如下表所示:
Reg3
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

第89行,对所有行校验为1的行索引按位取反之后,再按位异或,作用就是判断比特位为0的个数。比如reg2的Bit0为0表示:所有行校验为1的行中,行索引的Bit0为0的行有偶数个,也就是落在RP0计算范围内的行有偶数个。所以得到结论:

在所有行校验为1的行中,
属于RP0计算范围内的行有多少个------由reg2的Bit 0指示,0表示有偶数个,1表示有奇数个;
属于RP2计算范围内的行有多少个------由reg2的Bit 1指示,0表示有偶数个,1表示有奇数个;
属于RP4计算范围内的行有多少个------由reg2的Bit 2指示,0表示有偶数个,1表示有奇数个;
属于RP6计算范围内的行有多少个------由reg2的Bit 3指示,0表示有偶数个,1表示有奇数个;
属于RP8计算范围内的行有多少个------由reg2的Bit 4指示,0表示有偶数个,1表示有奇数个;
属于RP10计算范围内的行有多少个------由reg2的Bit 5指示,0表示有偶数个,1表示有奇数个;
属于RP12计算范围内的行有多少个------由reg2的Bit 6指示,0表示有偶数个,1表示有奇数个;
属于RP14计算范围内的行有多少个------由reg2的Bit 7指示,0表示有偶数个,1表示有奇数个;

所以,reg2每个Bit位的作用如下表所示:
Reg2
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

至此,只用了一个查找表和一个for循环,就把所有的校验位CP0 ~ CP5 和RP0 ~ RP15全都计算出来了。下面的任务只是按照表3的格式,把这些比特位重新排列一下顺序而已。
从reg2和reg3中抽取出 RP8~RP15放在tmp1中,抽取出RP0~RP7放在tmp2中,
Reg1左移两位,低两位置1,
然后把tmp2, tmp1, reg1 放在 ECC码的三个字节中。
程序中还有CONFIG_MTD_NAND_ECC_SMC, 又进行了一次取反操作,暂时还不知为何。

ECC纠错算法

  当往NAND Flash的page中写入数据的时候,每256字节我们生成一个ECC校验和,称之为原ECC校验和,保存到PAGE的OOB(out-of-band)数据区中。当从NAND Flash中读取数据的时候,每256字节我们生成一个ECC校验和,称之为新ECC校验和。
  将从OOB区中读出的原ECC校验和新ECC校验和按位异或,若结果为0,则表示不存在错(或是出现了 ECC无法检测的错误);若3个字节异或结果中存在11个比特位为1,表示存在一个比特错误,且可纠正;若3个字节异或结果中只存在1个比特位为1,表示 OOB区出错;其他情况均表示出现了无法纠正的错误。
  假设ecc_code_raw[3] 保存原始的ECC校验码,ecc_code_new[3] 保存新计算出的ECC校验码,其格式如下表所示:
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析
  对ecc_code_raw[3] 和 ecc_code_new[3] 按位异或,得到的结果三个字节分别保存在s0,s1,s2中,如果s0s1s2*有11个Bit位为1,则表示出现了一个比特位错误,可以修正。定位出错的比特位的方法是,先确定行地址(即哪个字节出错),再确定列地址(即该字节中的哪一个Bit位出错)。
  确定行地址的方法是,设行地址为unsigned char byteoffs,抽取s1中的Bit7,Bit5,Bit3,Bit1,作为 byteoffs的高四位, 抽取s0中的Bit7,Bit5,Bit3,Bit1 作为byteoffs的低四位, 则byteoffs的值就表示出错字节的行地址(范围为0 ~ 255)。
确定列地址的方法是:抽取s2中的Bit7,Bit5,Bit3 作为 bitnum 的低三位,bitnum其余位置0,则bitnum的表示出错Bit位的列地址 (范围为0 ~ 7)。

下面以一个简单的例子探索一下这其中的奥妙。
假设待校验的数据为两个字节,0x45(二进制为0100 0101)和0x38(二进制为0011 1000),其行列校验码如下表所示:
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析
  从表中可以计算出CP5 ~ CP0的值,列在下表的第一行(原始数据)。假设现在有一个数据位发生变化,0x38变为0x3A,也就是Byte 1的Bit 1由0变成了1,计算得到新的CP5 ~ CP0值放在下表第2行(变化后数据)。新旧校验码求异或的结果放在下表第三行。
  可见,当 Bit1发生变化时,列校验值中只有CP1,CP2,CP4发生了变化,而CP0,CP3,CP5没变化,也就是说6个Bit校验码有一半发生变化,则求异或的结果中有一半为1。同理,行校验求异或的结果也有一半为1。这就是为什么前面说256字节数据中的一个Bit位发生变化时,新旧22Bit校验码求异或的结果中会有11个Bit 位为1。
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析
  再来看怎么定位出错的Bit位。以列地址为例,若CP5发生变化(异或后的CP5=1),则出错处肯定在 Bit 4 ~ Bit 7中;若CP5无变化(异或后的CP5=0),则出错处在 Bit 0 ~ Bit 3 中,这样就筛选掉了一半的Bit位。剩下的4个Bit位中,再看CP3是否发生变化,又选出2个Bit位。剩下的2Bit位中再看CP1是否发生变化,则最终可定位1个出错的Bit位。下面的树形结构更清晰地展示了这个判决过程:
Nand ECC校验和纠错原理及2.6.27内核ECC代码分析

图表 1  出错Bit列地址定位的判决树

  注意:图中的CP指的是求异或之后的结果中的CP
  为什么只用CP4,CP2,CP0呢?其实这里面包含冗余信息,因为CP5=1则必有CP4=0,CP5=0则必有CP4=1,也就是CP5跟CP4一定相反,同理,CP3跟CP2一定相反,CP1跟CP0一定相反。所以只需要用一半就行了。
  这样,我们从异或结果中抽取出CP5,CP3,CP1位,便可定位出错Bit位的列地址。比如上面的例子中CP5/CP3/CP1 = 001,表示Bit 1出错。
  同理,行校验RP1发生变化,抽取RP1,可知Byte 1发生变化。这样定位出Byte 1的Bit 0出错。
当数据位256字节时,行校验使用RP0 ~ RP15,抽取异或结果的RP15,RP13,RP11,RP9,RP7,RP5,RP3,RP1位便可定位出哪个Byte出错,再用CP5,CP3,CP1定位哪个Bit出错。

Nand ECC校验和纠错原理及2.6.27内核ECC代码分析 TestEcc.c

/*
 * =====================================================================================
 *
 *       Filename:  TestEcc.c
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2009年06月04日 20时15分54秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Hongwang (mn), hoakee@gmail.com
 *        Company:  University of Science and Technology of China
 *
 * =====================================================================================
 */

#include <stdio.h>

typedef unsigned char   u_char;
typedef unsigned char   uint8_t;
typedef unsigned int   uint32_t;  

/*
 * Pre-calculated 256-way 1 byte column parity
 */
static const u_char nand_ecc_precalc_table[] = {
    0x00,0x55,0x56,0x03,0x59,0x0C,0x0F,0x5A,0x5A,0x0F,0x0C,0x59,0x03,0x56,0x55,0x00,
    0x65,0x30,0x33,0x66,0x3C,0x69,0x6A,0x3F,0x3F,0x6A,0x69,0x3C,0x66,0x33,0x30,0x65,
    0x66,0x33,0x30,0x65,0x3F,0x6A,0x69,0x3C,0x3C,0x69,0x6A,0x3F,0x65,0x30,0x33,0x66,
    0x03,0x56,0x55,0x00,0x5A,0x0F,0x0C,0x59,0x59,0x0C,0x0F,0x5A,0x00,0x55,0x56,0x03,
    0x69,0x3C,0x3F,0x6A,0x30,0x65,0x66,0x33,0x33,0x66,0x65,0x30,0x6A,0x3F,0x3C,0x69,
    0x0C,0x59,0x5A,0x0F,0x55,0x00,0x03,0x56,0x56,0x03,0x00,0x55,0x0F,0x5A,0x59,0x0C,
    0x0F,0x5A,0x59,0x0C,0x56,0x03,0x00,0x55,0x55,0x00,0x03,0x56,0x0C,0x59,0x5A,0x0F,
    0x6A,0x3F,0x3C,0x69,0x33,0x66,0x65,0x30,0x30,0x65,0x66,0x33,0x69,0x3C,0x3F,0x6A,
    0x6A,0x3F,0x3C,0x69,0x33,0x66,0x65,0x30,0x30,0x65,0x66,0x33,0x69,0x3C,0x3F,0x6A,
    0x0F,0x5A,0x59,0x0C,0x56,0x03,0x00,0x55,0x55,0x00,0x03,0x56,0x0C,0x59,0x5A,0x0F,
    0x0C,0x59,0x5A,0x0F,0x55,0x00,0x03,0x56,0x56,0x03,0x00,0x55,0x0F,0x5A,0x59,0x0C,
    0x69,0x3C,0x3F,0x6A,0x30,0x65,0x66,0x33,0x33,0x66,0x65,0x30,0x6A,0x3F,0x3C,0x69,
    0x03,0x56,0x55,0x00,0x5A,0x0F,0x0C,0x59,0x59,0x0C,0x0F,0x5A,0x00,0x55,0x56,0x03,
    0x66,0x33,0x30,0x65,0x3F,0x6A,0x69,0x3C,0x3C,0x69,0x6A,0x3F,0x65,0x30,0x33,0x66,
    0x65,0x30,0x33,0x66,0x3C,0x69,0x6A,0x3F,0x3F,0x6A,0x69,0x3C,0x66,0x33,0x30,0x65,
    0x00,0x55,0x56,0x03,0x59,0x0C,0x0F,0x5A,0x5A,0x0F,0x0C,0x59,0x03,0x56,0x55,0x00
};

/**
 * * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
 * * @mtd:        MTD block structure
 * * @dat:        raw data
 * * @ecc_code:   buffer for ECC
 * */
int nand_calculate_ecc(const u_char *dat, u_char *ecc_code)
{
    uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
    int i;

    /* Initialize variables */
    reg1 = reg2 = reg3 = ;

    /* Build up column parity */
    ; i < ; i++) {
        /* Get CP0 - CP5 from table */
        idx = nand_ecc_precalc_table[*dat++];
        reg1 ^= (idx & 0x3f);

        /* All bit XOR = 1 ? */
        if (idx & 0x40) {
            reg3 ^= (uint8_t) i;
            reg2 ^= ~((uint8_t) i);
        }
    }

    /* Create non-inverted ECC code from line parity */
    tmp1  = (reg3 & ; /* B7 -> B7 */
    tmp1 |= (reg2 & ; /* B7 -> B6 */
    tmp1 |= (reg3 & ; /* B6 -> B5 */
    tmp1 |= (reg2 & ; /* B6 -> B4 */
    tmp1 |= (reg3 & ; /* B5 -> B3 */
    tmp1 |= (reg2 & ; /* B5 -> B2 */
    tmp1 |= (reg3 & ; /* B4 -> B1 */
    tmp1 |= (reg2 & ; /* B4 -> B0 */

    tmp2  = (reg3 & ; /* B3 -> B7 */
    tmp2 |= (reg2 & ; /* B3 -> B6 */
    tmp2 |= (reg3 & ; /* B2 -> B5 */
    tmp2 |= (reg2 & ; /* B2 -> B4 */
    tmp2 |= (reg3 & ; /* B1 -> B3 */
    tmp2 |= (reg2 & ; /* B1 -> B2 */
    tmp2 |= (reg3 & ; /* B0 -> B1 */
    tmp2 |= (reg2 & ; /* B7 -> B0 */

    /* Calculate final ECC code */
#ifdef CONFIG_MTD_NAND_ECC_SMC
    //ecc_code[0] = ~tmp2;
    //ecc_code[1] = ~tmp1;
#else
    //ecc_code[0] = ~tmp1;
    //ecc_code[1] = ~tmp2;
#endif
    ecc_code[] = tmp2;
    ecc_code[] = tmp1;
    //ecc_code[2] = ((~reg1) << 2) | 0x03;
    ecc_code[] = ((reg1) << ) | 0x03;

    ;
}

static inline int countbits(uint32_t byte)
{
    ;

    )
        res += byte & 0x01;
    return res;
}

int nand_correct_data( u_char *read_ecc, u_char *calc_ecc)
{
    uint8_t s0, s1, s2;

    s0 = calc_ecc[] ^ read_ecc[];
    s1 = calc_ecc[] ^ read_ecc[];
    s2 = calc_ecc[] ^ read_ecc[];

    )
        ;

    /* Check for a single bit error */
    )) & 0x55) == 0x55 &&
        ((s1 ^ (s1 >> )) & 0x55) == 0x55 &&
        ((s2 ^ (s2 >> )) & 0x54) == 0x54) {

            uint32_t byteoffs, bitnum;

            byteoffs = (s1 << ) & 0x80;
            byteoffs |= (s1 << ) & 0x40;
            byteoffs |= (s1 << ) & 0x20;
            byteoffs |= (s1 << ) & 0x10;

            byteoffs |= (s0 >> ) & 0x08;
            byteoffs |= (s0 >> ) & 0x04;
            byteoffs |= (s0 >> ) & 0x02;
            byteoffs |= (s0 >> ) & 0x01;

            bitnum = (s2 >> ) & 0x04;
            bitnum |= (s2 >> ) & 0x02;
            bitnum |= (s2 >> ) & 0x01;

            printf("Error Bit at: Byte %d, Bit %d.\n", byteoffs, bitnum);

            ;
    }

    ) | ((uint32_t)s2 <<)) == )
        ;

    ;
}

//
static const u_char raw_data[] = {
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
    0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
    0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
    0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
    0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
    0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
    0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
    0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
    0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
    0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
    0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
    0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
    0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
};

// changed data. 0x34==>0x74
static const u_char new_data[] = {
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
    0x30,0x31,0x32,0x33,0x74,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
    0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
    0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
    0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
    0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
    0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
    0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
    0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
    0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
    0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
    0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
    0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
    0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
};

];
];

int main()
{
    ;

    nand_calculate_ecc( raw_data, ecc_code_raw );
    nand_calculate_ecc( new_data, ecc_code_new );   

    printf("\nRaw ECC Code: ");

    ; i< ; i++)
    {
        printf("0x%02X ", ecc_code_raw[i] );
    }

    printf("\nNew ECC Code: ");

    ; i< ; i++)
    {
        printf("0x%02X ", ecc_code_new[i] );
    }

    printf("\n");

    nand_correct_data( ecc_code_raw, ecc_code_new );

    printf("\n");

}