2012-12-27 15:21:13 +08:00
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define NAND_SIM "nand.bin"
|
|
|
|
#if 1
|
2012-12-27 16:26:35 +08:00
|
|
|
#define OOB_SIZE 64
|
2012-12-31 13:39:12 +08:00
|
|
|
#define PAGE_DATA_SIZE 2048
|
2012-12-27 16:26:35 +08:00
|
|
|
#define PAGE_SIZE (2048 + 64)
|
|
|
|
#define PAGE_PER_BLOCK 64
|
|
|
|
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
|
|
|
|
#define BLOCK_NUM 512
|
|
|
|
// #define BLOCK_NUM 2048
|
2012-12-27 15:21:13 +08:00
|
|
|
#else
|
2012-12-27 16:26:35 +08:00
|
|
|
#define OOB_SIZE 16
|
|
|
|
#define PAGE_SIZE (512 + OOB_SIZE)
|
|
|
|
#define PAGE_PER_BLOCK 32
|
|
|
|
#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
|
|
|
|
#define BLOCK_NUM 512
|
2012-12-27 15:21:13 +08:00
|
|
|
#endif
|
|
|
|
|
2012-12-31 13:39:12 +08:00
|
|
|
#define ECC_SIZE ((PAGE_DATA_SIZE) * 3 / 256)
|
|
|
|
|
2012-12-27 15:21:13 +08:00
|
|
|
static unsigned char block_data[BLOCK_SIZE];
|
|
|
|
static struct rt_mtd_nand_device _nanddrv_file_device;
|
2012-12-27 16:26:35 +08:00
|
|
|
static FILE *file = NULL;
|
2012-12-27 15:21:13 +08:00
|
|
|
|
|
|
|
static rt_uint8_t CountBitsInByte(rt_uint8_t byte)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_uint8_t count = 0;
|
|
|
|
|
|
|
|
while (byte > 0)
|
|
|
|
{
|
|
|
|
if (byte & 1)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
byte >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
|
|
|
|
{
|
|
|
|
rt_uint32_t i;
|
|
|
|
rt_uint8_t columnSum = 0;
|
|
|
|
rt_uint8_t evenLineCode = 0;
|
|
|
|
rt_uint8_t oddLineCode = 0;
|
|
|
|
rt_uint8_t evenColumnCode = 0;
|
|
|
|
rt_uint8_t oddColumnCode = 0;
|
|
|
|
|
|
|
|
// Xor all bytes together to get the column sum;
|
|
|
|
// At the same time, calculate the even and odd line codes
|
2012-12-27 16:26:35 +08:00
|
|
|
for (i = 0; i < 256; i++)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
|
|
|
columnSum ^= data[i];
|
|
|
|
|
|
|
|
// If the xor sum of the byte is 0, then this byte has no incidence on
|
|
|
|
// the computed code; so check if the sum is 1.
|
|
|
|
if ((CountBitsInByte(data[i]) & 1) == 1)
|
|
|
|
{
|
|
|
|
// Parity groups are formed by forcing a particular index bit to 0
|
|
|
|
// (even) or 1 (odd).
|
|
|
|
// Example on one byte:
|
|
|
|
//
|
|
|
|
// bits (dec) 7 6 5 4 3 2 1 0
|
|
|
|
// (bin) 111 110 101 100 011 010 001 000
|
|
|
|
// '---'---'---'----------.
|
|
|
|
// |
|
|
|
|
// groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 |
|
|
|
|
// P2' ooooooo eeeeeee ooooooo eeeeeee P2 |
|
|
|
|
// P1' ooo eee ooo eee ooo eee ooo eee P1 |
|
|
|
|
// |
|
|
|
|
// We can see that: |
|
|
|
|
// - P4 -> bit 2 of index is 0 --------------------'
|
|
|
|
// - P4' -> bit 2 of index is 1.
|
|
|
|
// - P2 -> bit 1 of index if 0.
|
|
|
|
// - etc...
|
|
|
|
// We deduce that a bit position has an impact on all even Px if
|
|
|
|
// the log2(x)nth bit of its index is 0
|
|
|
|
// ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3)
|
|
|
|
// and on all odd Px' if the log2(x)nth bit of its index is 1
|
|
|
|
// ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5)
|
|
|
|
//
|
|
|
|
// As such, we calculate all the possible Px and Px' values at the
|
|
|
|
// same time in two variables, evenLineCode and oddLineCode, such as
|
|
|
|
// evenLineCode bits: P128 P64 P32 P16 P8 P4 P2 P1
|
|
|
|
// oddLineCode bits: P128' P64' P32' P16' P8' P4' P2' P1'
|
|
|
|
//
|
|
|
|
evenLineCode ^= (255 - i);
|
|
|
|
oddLineCode ^= i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we have the line parities, and the column sum. First, We
|
|
|
|
// must caculate the parity group values on the column sum.
|
2012-12-27 16:26:35 +08:00
|
|
|
for (i = 0; i < 8; i++)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
|
|
|
if (columnSum & 1)
|
|
|
|
{
|
|
|
|
evenColumnCode ^= (7 - i);
|
|
|
|
oddColumnCode ^= i;
|
|
|
|
}
|
|
|
|
columnSum >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we must interleave the parity values, to obtain the following layout:
|
|
|
|
// Code[0] = Line1
|
|
|
|
// Code[1] = Line2
|
|
|
|
// Code[2] = Column
|
|
|
|
// Line = Px' Px P(x-1)- P(x-1) ...
|
|
|
|
// Column = P4' P4 P2' P2 P1' P1 PadBit PadBit
|
|
|
|
code[0] = 0;
|
|
|
|
code[1] = 0;
|
|
|
|
code[2] = 0;
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
for (i = 0; i < 4; i++)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
|
|
|
code[0] <<= 2;
|
|
|
|
code[1] <<= 2;
|
|
|
|
code[2] <<= 2;
|
|
|
|
|
|
|
|
// Line 1
|
|
|
|
if ((oddLineCode & 0x80) != 0)
|
|
|
|
{
|
|
|
|
code[0] |= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((evenLineCode & 0x80) != 0)
|
|
|
|
{
|
|
|
|
code[0] |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line 2
|
|
|
|
if ((oddLineCode & 0x08) != 0)
|
|
|
|
{
|
|
|
|
code[1] |= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((evenLineCode & 0x08) != 0)
|
|
|
|
{
|
|
|
|
code[1] |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Column
|
|
|
|
if ((oddColumnCode & 0x04) != 0)
|
|
|
|
{
|
|
|
|
code[2] |= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((evenColumnCode & 0x04) != 0)
|
|
|
|
{
|
|
|
|
code[2] |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
oddLineCode <<= 1;
|
|
|
|
evenLineCode <<= 1;
|
|
|
|
oddColumnCode <<= 1;
|
|
|
|
evenColumnCode <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invert codes (linux compatibility)
|
|
|
|
code[0] = (~(rt_uint32_t)code[0]);
|
|
|
|
code[1] = (~(rt_uint32_t)code[1]);
|
|
|
|
code[2] = (~(rt_uint32_t)code[2]);
|
|
|
|
}
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t *puCode)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
while (dwSize > 0)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
Compute256(pucData, puCode) ;
|
2012-12-27 15:21:13 +08:00
|
|
|
|
|
|
|
pucData += 256;
|
|
|
|
puCode += 3;
|
|
|
|
dwSize -= 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read chip id */
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device *device)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
return 0x00;
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read/write/move page */
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device,
|
|
|
|
rt_off_t page,
|
|
|
|
rt_uint8_t *data, rt_uint32_t data_len,
|
|
|
|
rt_uint8_t *spare, rt_uint32_t spare_len)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_uint32_t offset;
|
2012-12-31 13:39:12 +08:00
|
|
|
rt_uint8_t oob_ecc [ECC_SIZE];
|
|
|
|
rt_uint8_t ecc [ECC_SIZE];
|
2012-12-27 16:26:35 +08:00
|
|
|
|
|
|
|
page = page + device->block_start * device->pages_per_block;
|
|
|
|
|
|
|
|
if (page / device->pages_per_block > device->block_end)
|
|
|
|
{
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write page */
|
|
|
|
offset = page * PAGE_SIZE;
|
2012-12-31 13:39:12 +08:00
|
|
|
if (data != NULL && data_len != 0)
|
2012-12-27 16:26:35 +08:00
|
|
|
{
|
|
|
|
fseek(file, offset, SEEK_SET);
|
|
|
|
fread(data, data_len, 1, file);
|
2012-12-31 13:39:12 +08:00
|
|
|
if (data_len == PAGE_DATA_SIZE)
|
|
|
|
{
|
|
|
|
/* read ecc size */
|
|
|
|
fread(oob_ecc, ECC_SIZE, 1, file);
|
2012-12-27 16:26:35 +08:00
|
|
|
|
2012-12-31 13:39:12 +08:00
|
|
|
/* verify ECC */
|
|
|
|
ecc_hamming_compute256x(data, PAGE_DATA_SIZE, &ecc[0]);
|
|
|
|
if (memcmp(&oob_ecc[0], &ecc[0], ECC_SIZE) != 0)
|
|
|
|
return -RT_MTD_EECC;
|
|
|
|
}
|
2012-12-27 16:26:35 +08:00
|
|
|
}
|
|
|
|
|
2012-12-31 13:39:12 +08:00
|
|
|
if (spare != NULL && spare_len)
|
2012-12-27 16:26:35 +08:00
|
|
|
{
|
2012-12-31 13:39:12 +08:00
|
|
|
offset = page * PAGE_SIZE + PAGE_DATA_SIZE;
|
|
|
|
fseek(file, offset, SEEK_SET);
|
|
|
|
fread(spare, spare_len, 1, file);
|
2012-12-27 16:26:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device,
|
|
|
|
rt_off_t page,
|
|
|
|
const rt_uint8_t *data, rt_uint32_t data_len,
|
|
|
|
const rt_uint8_t *oob, rt_uint32_t spare_len)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_uint32_t offset;
|
2012-12-31 13:39:12 +08:00
|
|
|
rt_uint8_t ecc[ECC_SIZE];
|
2012-12-27 16:26:35 +08:00
|
|
|
|
|
|
|
page = page + device->block_start * device->pages_per_block;
|
|
|
|
if (page / device->pages_per_block > device->block_end)
|
|
|
|
{
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write page */
|
|
|
|
offset = page * PAGE_SIZE;
|
2012-12-31 13:39:12 +08:00
|
|
|
if (data != RT_NULL && data_len != 0)
|
2012-12-27 16:26:35 +08:00
|
|
|
{
|
|
|
|
fseek(file, offset, SEEK_SET);
|
2012-12-31 13:39:12 +08:00
|
|
|
fwrite(data, data_len, 1, file);
|
2012-12-27 16:26:35 +08:00
|
|
|
|
2012-12-31 13:39:12 +08:00
|
|
|
if (data_len == PAGE_DATA_SIZE)
|
|
|
|
{
|
|
|
|
/*write the ecc information */
|
|
|
|
ecc_hamming_compute256x(data, PAGE_DATA_SIZE, ecc);
|
|
|
|
|
|
|
|
fwrite(ecc, ECC_SIZE, 1, file);
|
|
|
|
}
|
|
|
|
}
|
2012-12-27 16:26:35 +08:00
|
|
|
|
2012-12-31 13:39:12 +08:00
|
|
|
if (oob != RT_NULL && spare_len != 0)
|
2012-12-27 16:26:35 +08:00
|
|
|
{
|
2012-12-31 13:39:12 +08:00
|
|
|
offset = page * PAGE_SIZE + PAGE_DATA_SIZE + ECC_SIZE;
|
|
|
|
fseek(file, offset, SEEK_SET);
|
|
|
|
fwrite(&oob[ECC_SIZE], spare_len-ECC_SIZE, 1, file);
|
2012-12-27 16:26:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device *device, rt_off_t from, rt_off_t to)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_uint32_t offset;
|
2012-12-31 13:39:12 +08:00
|
|
|
rt_uint8_t page_buffer[PAGE_DATA_SIZE];
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_uint8_t oob_buffer[OOB_SIZE];
|
|
|
|
|
|
|
|
from = from + device->block_start * device->pages_per_block;
|
|
|
|
to = to + device->block_start * device->pages_per_block;
|
|
|
|
|
|
|
|
if (from / device->pages_per_block > device->block_end ||
|
|
|
|
to / device->pages_per_block > device->block_end)
|
|
|
|
{
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device->plane_num > 1)
|
|
|
|
{
|
|
|
|
rt_uint32_t mask;
|
|
|
|
rt_uint16_t from_block, to_block;
|
|
|
|
|
|
|
|
from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
|
|
|
|
to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
|
|
|
|
mask = device->plane_num - 1;
|
|
|
|
|
|
|
|
if ((from_block & mask) != (to_block & mask))
|
|
|
|
{
|
|
|
|
rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read page */
|
|
|
|
offset = from * PAGE_SIZE;
|
|
|
|
fseek(file, offset, SEEK_SET);
|
|
|
|
fread(page_buffer, sizeof(page_buffer), 1, file);
|
|
|
|
fread(oob_buffer, sizeof(oob_buffer), 1, file);
|
|
|
|
|
|
|
|
/* write page */
|
|
|
|
offset = to * PAGE_SIZE;
|
|
|
|
fseek(file, offset, SEEK_SET);
|
|
|
|
fwrite(page_buffer, sizeof(page_buffer), 1, file);
|
|
|
|
fwrite(oob_buffer, sizeof(oob_buffer), 1, file);
|
|
|
|
|
|
|
|
return RT_EOK;
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* erase block */
|
2012-12-27 16:26:35 +08:00
|
|
|
static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
if (block > BLOCK_NUM) return -RT_EIO;
|
2012-12-27 15:21:13 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
/* add the start blocks */
|
2012-12-31 13:39:12 +08:00
|
|
|
block = block + device->block_start;
|
2012-12-27 15:21:13 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
fseek(file, block * BLOCK_SIZE, SEEK_SET);
|
|
|
|
fwrite(block_data, sizeof(block_data), 1, file);
|
2012-12-27 15:21:13 +08:00
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
return RT_EOK;
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
const static struct rt_mtd_nand_driver_ops _ops =
|
2012-12-27 15:21:13 +08:00
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
nanddrv_file_read_id,
|
|
|
|
nanddrv_file_read_page,
|
|
|
|
nanddrv_file_write_page,
|
|
|
|
nanddrv_file_move_page,
|
2012-12-29 19:40:06 +08:00
|
|
|
nanddrv_file_erase_block,
|
2012-12-31 13:39:12 +08:00
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
2012-12-27 15:21:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void nand_eraseall(void);
|
|
|
|
|
|
|
|
void rt_hw_mtd_nand_init(void)
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
rt_uint16_t ecc_size;
|
|
|
|
rt_uint32_t size;
|
|
|
|
|
|
|
|
memset(block_data, 0xff, sizeof(block_data));
|
|
|
|
/* open file */
|
|
|
|
file = fopen(NAND_SIM, "rb+");
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
file = fopen(NAND_SIM, "wb+");
|
|
|
|
}
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size = ftell(file);
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
if (size < BLOCK_NUM * BLOCK_SIZE)
|
|
|
|
{
|
|
|
|
rt_uint32_t index;
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
for (index = 0; index < BLOCK_NUM; index ++)
|
|
|
|
{
|
|
|
|
fwrite(block_data, sizeof(block_data), 1, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
2012-12-31 13:39:12 +08:00
|
|
|
ecc_size = (PAGE_DATA_SIZE) * 3 / 256;
|
2012-12-27 16:26:35 +08:00
|
|
|
_nanddrv_file_device.plane_num = 2;
|
|
|
|
_nanddrv_file_device.oob_size = OOB_SIZE;
|
|
|
|
_nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
|
2012-12-31 13:39:12 +08:00
|
|
|
_nanddrv_file_device.page_size = PAGE_DATA_SIZE;
|
2012-12-27 16:26:35 +08:00
|
|
|
_nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
|
|
|
|
_nanddrv_file_device.block_start = 0;
|
|
|
|
_nanddrv_file_device.block_end = BLOCK_NUM / 2;
|
|
|
|
_nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start;
|
|
|
|
_nanddrv_file_device.ops = &_ops;
|
|
|
|
|
|
|
|
rt_mtd_nand_register_device("nand0", &_nanddrv_file_device);
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(RT_USING_FINSH)
|
|
|
|
#include <finsh.h>
|
|
|
|
void nand_eraseall()
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
int index;
|
|
|
|
for (index = 0; index < _nanddrv_file_device.block_total; index ++)
|
|
|
|
{
|
|
|
|
nanddrv_file_erase_block(&_nanddrv_file_device, index);
|
|
|
|
}
|
2012-12-27 15:21:13 +08:00
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash);
|
|
|
|
|
|
|
|
#endif //RT_USING_FINSH
|