rt-thread/components/dfs/filesystems/uffs/uffs_nandif.c

360 lines
11 KiB
C
Raw Normal View History

2013-01-08 22:40:58 +08:00
/*
* RT-Thread Device Interface for uffs
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "dfs_uffs.h"
static int nand_init_flash(uffs_Device *dev)
{
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_NO_ERR;
2013-01-08 22:40:58 +08:00
}
static int nand_release_flash(uffs_Device *dev)
{
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_NO_ERR;
2013-01-08 22:40:58 +08:00
}
static int nand_erase_block(uffs_Device *dev, unsigned block)
{
2013-06-26 22:30:40 +08:00
int res;
res = rt_mtd_nand_erase_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR;
2013-01-08 22:40:58 +08:00
}
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
static int nand_check_block(uffs_Device *dev, unsigned block)
{
2013-06-26 22:30:40 +08:00
int res;
res = rt_mtd_nand_check_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
2013-01-08 22:40:58 +08:00
}
static int nand_mark_badblock(uffs_Device *dev, unsigned block)
{
2013-06-26 22:30:40 +08:00
int res;
res = rt_mtd_nand_mark_badblock(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR;
2013-01-08 22:40:58 +08:00
}
#endif
#if (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_NONE) || (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_SOFT)
2013-06-26 22:30:40 +08:00
static int nand_read_page(uffs_Device *dev,
u32 block,
u32 page,
u8 *data,
int data_len,
u8 *ecc,
rt_uint8_t *spare,
int spare_len)
2013-01-08 22:40:58 +08:00
{
2013-06-26 22:30:40 +08:00
int res;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
page = block * dev->attr->pages_per_block + page;
if (data == NULL && spare == NULL)
{
2013-01-08 22:40:58 +08:00
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
2013-06-26 22:30:40 +08:00
RT_ASSERT(0); //should not be here
2013-01-08 22:40:58 +08:00
#else
2013-06-26 22:30:40 +08:00
/* check block status: bad or good */
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
rt_memset(spare, 0, UFFS_MAX_SPARE_SIZE);
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
res = spare[dev->attr->block_status_offs] == 0xFF ?
UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
return res;
2013-01-08 22:40:58 +08:00
#endif
2013-06-26 22:30:40 +08:00
}
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_NO_ERR;
2013-01-08 22:40:58 +08:00
}
2013-06-26 22:30:40 +08:00
static int nand_write_page(uffs_Device *dev,
u32 block,
u32 page,
const u8 *data,
int data_len,
const u8 *spare,
int spare_len)
2013-01-08 22:40:58 +08:00
{
2013-06-26 22:30:40 +08:00
int res;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
page = block * dev->attr->pages_per_block + page;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
if (data == NULL && spare == NULL)
{
2013-01-08 22:40:58 +08:00
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
2013-06-26 22:30:40 +08:00
RT_ASSERT(0); //should not be here
2013-01-08 22:40:58 +08:00
#else
2013-06-26 22:30:40 +08:00
/* mark bad block */
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE);
spare[dev->attr->block_status_offs] = 0x00;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
if (res != RT_EOK)
goto __error;
2013-01-08 22:40:58 +08:00
#endif
2013-06-26 22:30:40 +08:00
}
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
if (res != RT_EOK)
goto __error;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_NO_ERR;
2013-01-08 22:40:58 +08:00
__error:
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_IO_ERR;
2013-01-08 22:40:58 +08:00
}
const uffs_FlashOps nand_ops =
{
2013-06-26 22:30:40 +08:00
nand_init_flash, /* InitFlash() */
nand_release_flash, /* ReleaseFlash() */
nand_read_page, /* ReadPage() */
NULL, /* ReadPageWithLayout */
nand_write_page, /* WritePage() */
NULL, /* WritePageWithLayout */
2013-01-08 22:40:58 +08:00
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
2013-06-26 22:30:40 +08:00
nand_check_block,
nand_mark_badblock,
2013-01-08 22:40:58 +08:00
#else
2013-06-26 22:30:40 +08:00
NULL, /* IsBadBlock(), let UFFS take care of it. */
NULL, /* MarkBadBlock(), let UFFS take care of it. */
2013-01-08 22:40:58 +08:00
#endif
2013-06-26 22:30:40 +08:00
nand_erase_block, /* EraseBlock() */
2013-01-08 22:40:58 +08:00
};
2013-06-26 22:30:40 +08:00
void uffs_setup_storage(struct uffs_StorageAttrSt *attr,
struct rt_mtd_nand_device *nand)
2013-01-08 22:40:58 +08:00
{
2013-06-26 22:30:40 +08:00
rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
// attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */
attr->page_data_size = nand->page_size; /* page data size */
attr->pages_per_block = nand->pages_per_block; /* pages per block */
attr->spare_size = nand->oob_size; /* page spare size */
attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */
attr->ecc_size = 0; /* ecc size is 0 , the uffs will calculate the ecc size*/
attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */
attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */
2013-01-08 22:40:58 +08:00
}
#elif RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_HW_AUTO
2013-06-26 22:30:40 +08:00
static int WritePageWithLayout(uffs_Device *dev,
u32 block,
u32 page,
const u8 *data,
int data_len,
const u8 *ecc, //NULL
const uffs_TagStore *ts)
2013-01-08 22:40:58 +08:00
{
2013-06-26 22:30:40 +08:00
int res;
int spare_len;
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
page = block * dev->attr->pages_per_block + page;
spare_len = dev->mem.spare_data_size;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
if (data == NULL && ts == NULL)
{
2013-01-08 22:40:58 +08:00
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
2013-06-26 22:30:40 +08:00
RT_ASSERT(0); //should not be here
2013-01-08 22:40:58 +08:00
#else
2013-06-26 22:30:40 +08:00
/* mark bad block */
rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE);
spare[dev->attr->block_status_offs] = 0x00;
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
if (res != RT_EOK)
goto __error;
dev->st.io_write++;
return UFFS_FLASH_NO_ERR;
2013-01-08 22:40:58 +08:00
#endif
2013-06-26 22:30:40 +08:00
}
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
if (data != NULL && data_len != 0)
{
RT_ASSERT(data_len == dev->attr->page_data_size);
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
dev->st.page_write_count++;
dev->st.io_write += data_len;
}
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
if (ts != RT_NULL)
{
uffs_FlashMakeSpare(dev, ts, RT_NULL, (u8 *)spare);
dev->st.spare_write_count++;
dev->st.io_write += spare_len;
}
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
if (res != RT_EOK)
goto __error;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_NO_ERR;
2013-01-08 22:40:58 +08:00
__error:
2013-06-26 22:30:40 +08:00
return UFFS_FLASH_IO_ERR;
2013-01-08 22:40:58 +08:00
}
2013-06-26 22:30:40 +08:00
static URET ReadPageWithLayout(uffs_Device *dev,
u32 block,
u32 page,
u8 *data,
int data_len,
u8 *ecc, //NULL
uffs_TagStore *ts,
u8 *ecc_store) //NULL
2013-01-08 22:40:58 +08:00
{
2013-06-26 22:30:40 +08:00
int res = UFFS_FLASH_NO_ERR;
int spare_len;
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
page = block * dev->attr->pages_per_block + page;
spare_len = dev->mem.spare_data_size;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
if (data == RT_NULL && ts == RT_NULL)
{
2013-01-08 22:40:58 +08:00
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
2013-06-26 22:30:40 +08:00
RT_ASSERT(0); //should not be here
2013-01-08 22:40:58 +08:00
#else
2013-06-26 22:30:40 +08:00
/* check block good or bad */
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
dev->st.io_read++;
2013-01-08 22:40:58 +08:00
2013-06-26 22:30:40 +08:00
res = spare[dev->attr->block_status_offs] == 0xFF ?
UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
return res;
2013-01-08 22:40:58 +08:00
#endif
2013-06-26 22:30:40 +08:00
}
if (data != RT_NULL)
{
dev->st.io_read += data_len;
dev->st.page_read_count++;
}
res = rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
if (res == 0)
res = UFFS_FLASH_NO_ERR;
else if (res == -1)
{
//TODO ecc correct, add code to use hardware do ecc correct
res = UFFS_FLASH_ECC_OK;
}
else
res = UFFS_FLASH_ECC_FAIL;
if (ts != RT_NULL)
{
// unload ts and ecc from spare, you can modify it if you like
uffs_FlashUnloadSpare(dev, (const u8 *)spare, ts, RT_NULL);
if ((spare[spare_len - 1] == 0xFF) && (res == UFFS_FLASH_NO_ERR))
res = UFFS_FLASH_NOT_SEALED;
dev->st.io_read += spare_len;
dev->st.spare_read_count++;
}
return res;
2013-01-08 22:40:58 +08:00
}
const uffs_FlashOps nand_ops =
{
2013-06-26 22:30:40 +08:00
nand_init_flash, /* InitFlash() */
nand_release_flash, /* ReleaseFlash() */
NULL, /* ReadPage() */
ReadPageWithLayout, /* ReadPageWithLayout */
NULL, /* WritePage() */
WritePageWithLayout,/* WritePageWithLayout */
2013-01-08 22:40:58 +08:00
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
2013-06-26 22:30:40 +08:00
nand_check_block,
nand_mark_badblock,
2013-01-08 22:40:58 +08:00
#else
2013-06-26 22:30:40 +08:00
NULL, /* IsBadBlock(), let UFFS take care of it. */
NULL, /* MarkBadBlock(), let UFFS take care of it. */
2013-01-08 22:40:58 +08:00
#endif
2013-06-26 22:30:40 +08:00
nand_erase_block, /* EraseBlock() */
2013-01-08 22:40:58 +08:00
};
static rt_uint8_t hw_flash_data_layout[UFFS_SPARE_LAYOUT_SIZE] =
2013-06-26 22:30:40 +08:00
{
0x05, 0x08, 0xFF, 0x00
};
2013-01-08 22:40:58 +08:00
static rt_uint8_t hw_flash_ecc_layout[UFFS_SPARE_LAYOUT_SIZE] =
2013-06-26 22:30:40 +08:00
{
0x00, 0x04, 0xFF, 0x00
};
2013-01-08 22:40:58 +08:00
RT_WEAK void uffs_setup_storage(struct uffs_StorageAttrSt *attr,
2013-06-26 22:30:40 +08:00
struct rt_mtd_nand_device *nand)
2013-01-08 22:40:58 +08:00
{
2013-06-26 22:30:40 +08:00
rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
// attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */
attr->page_data_size = nand->page_size; /* page data size */
attr->pages_per_block = nand->pages_per_block; /* pages per block */
attr->spare_size = nand->oob_size; /* page spare size */
attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */
attr->ecc_size = nand->oob_size-nand->oob_free; /* ecc size */
attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */
attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */
/* calculate the ecc layout array */
hw_flash_data_layout[0] = attr->ecc_size + 1; /* ecc size + 1byte block status */
hw_flash_data_layout[1] = 0x08;
hw_flash_data_layout[2] = 0xFF;
hw_flash_data_layout[3] = 0x00;
hw_flash_ecc_layout[0] = 0;
hw_flash_ecc_layout[1] = attr->ecc_size;
hw_flash_ecc_layout[2] = 0xFF;
hw_flash_ecc_layout[3] = 0x00;
/* initialize _uffs_data_layout and _uffs_ecc_layout */
rt_memcpy(attr->_uffs_data_layout, hw_flash_data_layout, UFFS_SPARE_LAYOUT_SIZE);
rt_memcpy(attr->_uffs_ecc_layout, hw_flash_ecc_layout, UFFS_SPARE_LAYOUT_SIZE);
attr->data_layout = attr->_uffs_data_layout;
attr->ecc_layout = attr->_uffs_ecc_layout;
2013-01-08 22:40:58 +08:00
}
#endif