Merge pull request #2798 from Guozhanxin/nandflash_test

优化了 MTD_NAND 驱动框架
This commit is contained in:
Bernard Xiong 2019-06-20 13:15:30 +08:00 committed by GitHub
commit 6df98b436f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 303 additions and 209 deletions

View File

@ -21,13 +21,13 @@
struct rt_mtd_nand_driver_ops;
#define RT_MTD_NAND_DEVICE(device) ((struct rt_mtd_nand_device*)(device))
#define RT_MTD_EOK 0 /* NO error */
#define RT_MTD_EECC 1 /* ECC error */
#define RT_MTD_EBUSY 2 /* hardware busy */
#define RT_MTD_EIO 3 /* generic IO issue */
#define RT_MTD_ENOMEM 4 /* out of memory */
#define RT_MTD_ESRC 5 /* source issue */
#define RT_MTD_EECC_CORRECT 6 /* ECC error but correct */
#define RT_MTD_EOK 0 /* NO error */
#define RT_MTD_EECC 101 /* ECC error */
#define RT_MTD_EBUSY 102 /* hardware busy */
#define RT_MTD_EIO 103 /* generic IO issue */
#define RT_MTD_ENOMEM 104 /* out of memory */
#define RT_MTD_ESRC 105 /* source issue */
#define RT_MTD_EECC_CORRECT 106 /* ECC error but correct */
struct rt_mtd_nand_device
{
@ -41,77 +41,97 @@ struct rt_mtd_nand_device
rt_uint32_t pages_per_block; /* The number of page a block */
rt_uint16_t block_total;
/* Only be touched by driver */
rt_uint32_t block_start; /* The start of available block*/
rt_uint32_t block_end; /* The end of available block */
/* operations interface */
const struct rt_mtd_nand_driver_ops* ops;
const struct rt_mtd_nand_driver_ops *ops;
};
struct rt_mtd_nand_driver_ops
{
rt_err_t (*read_id) (struct rt_mtd_nand_device* device);
rt_err_t (*read_id)(struct rt_mtd_nand_device *device);
rt_err_t (*read_page)(struct rt_mtd_nand_device* device,
rt_err_t (*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);
rt_uint8_t *data, rt_uint32_t data_len,
rt_uint8_t *spare, rt_uint32_t spare_len);
rt_err_t (*write_page)(struct rt_mtd_nand_device * device,
rt_err_t (*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 * spare, rt_uint32_t spare_len);
rt_err_t (*move_page) (struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
const rt_uint8_t *data, rt_uint32_t data_len,
const rt_uint8_t *spare, rt_uint32_t spare_len);
rt_err_t (*move_page)(struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
rt_err_t (*erase_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
rt_err_t (*check_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
rt_err_t (*mark_badblock)(struct rt_mtd_nand_device* device, rt_uint32_t block);
rt_err_t (*erase_block)(struct rt_mtd_nand_device *device, rt_uint32_t block);
rt_err_t (*check_block)(struct rt_mtd_nand_device *device, rt_uint32_t block);
rt_err_t (*mark_badblock)(struct rt_mtd_nand_device *device, rt_uint32_t block);
};
rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device);
rt_err_t rt_mtd_nand_register_device(const char *name, struct rt_mtd_nand_device *device);
rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device* device)
rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device *device)
{
RT_ASSERT(device->ops->read_id);
return device->ops->read_id(device);
}
rt_inline rt_err_t rt_mtd_nand_read(
struct rt_mtd_nand_device* device,
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)
rt_uint8_t *data, rt_uint32_t data_len,
rt_uint8_t *spare, rt_uint32_t spare_len)
{
RT_ASSERT(device->ops->read_page);
return device->ops->read_page(device, page, data, data_len, spare, spare_len);
}
rt_inline rt_err_t rt_mtd_nand_write(
struct rt_mtd_nand_device* device,
struct rt_mtd_nand_device *device,
rt_off_t page,
const rt_uint8_t* data, rt_uint32_t data_len,
const rt_uint8_t * spare, rt_uint32_t spare_len)
const rt_uint8_t *data, rt_uint32_t data_len,
const rt_uint8_t *spare, rt_uint32_t spare_len)
{
RT_ASSERT(device->ops->write_page);
return device->ops->write_page(device, page, data, data_len, spare, spare_len);
}
rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device* device,
rt_off_t src_page, rt_off_t dst_page)
rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device *device,
rt_off_t src_page, rt_off_t dst_page)
{
RT_ASSERT(device->ops->move_page);
return device->ops->move_page(device, src_page, dst_page);
}
rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{
RT_ASSERT(device->ops->erase_block);
return device->ops->erase_block(device, block);
}
rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{
return device->ops->check_block(device, block);
if (device->ops->check_block)
{
return device->ops->check_block(device, block);
}
else
{
return -RT_ENOSYS;
}
}
rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device* device, rt_uint32_t block)
rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device *device, rt_uint32_t block)
{
return device->ops->mark_badblock(device, block);
if (device->ops->mark_badblock)
{
return device->ops->mark_badblock(device, block);
}
else
{
return -RT_ENOSYS;
}
}
#endif /* MTD_NAND_H_ */

View File

@ -78,7 +78,7 @@ rt_err_t rt_mtd_nand_register_device(const char *name,
/* set device class and generic device interface */
dev->type = RT_Device_Class_MTD;
#ifdef RT_USING_DEVICE_OPS
dev->ops = &mtd_nand_ops;
dev->ops = &mtd_nand_ops;
#else
dev->init = _mtd_init;
dev->open = _mtd_open;
@ -101,188 +101,262 @@ rt_err_t rt_mtd_nand_register_device(const char *name,
static void mtd_dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
{
unsigned char *buf = (unsigned char*)ptr;
int i, j;
for (i=0; i<buflen; i+=16)
{
rt_kprintf("%06x: ", i);
for (j=0; j<16; j++)
if (i+j < buflen)
rt_kprintf("%02x ", buf[i+j]);
else
rt_kprintf(" ");
rt_kprintf(" ");
for (j=0; j<16; j++)
if (i+j < buflen)
rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.');
rt_kprintf("\n");
}
unsigned char *buf = (unsigned char *)ptr;
int i, j;
for (i = 0; i < buflen; i += 16)
{
rt_kprintf("%06x: ", i);
for (j = 0; j < 16; j++)
if (i + j < buflen)
rt_kprintf("%02x ", buf[i + j]);
else
rt_kprintf(" ");
rt_kprintf(" ");
for (j = 0; j < 16; j++)
if (i + j < buflen)
rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
rt_kprintf("\n");
}
}
int mtd_nandid(const char* name)
int mtd_nandid(const char *name)
{
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
return rt_mtd_nand_read_id(nand);
return rt_mtd_nand_read_id(nand);
}
int mtd_nand_read(const char *name, int block, int page)
{
rt_err_t result;
rt_uint8_t *page_ptr;
rt_uint8_t *oob_ptr;
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
page_ptr = rt_malloc(nand->page_size + nand->oob_size);
if (page_ptr == RT_NULL)
{
rt_kprintf("out of memory!\n");
return -RT_ENOMEM;
}
oob_ptr = page_ptr + nand->page_size;
rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size);
/* calculate the page number */
page = block * nand->pages_per_block + page;
result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size,
oob_ptr, nand->oob_size);
rt_kprintf("read page, rc=%d\n", result);
mtd_dump_hex(page_ptr, nand->page_size);
mtd_dump_hex(oob_ptr, nand->oob_size);
rt_free(page_ptr);
return 0;
}
int mtd_nand_readoob(const char *name, int block, int page)
{
struct rt_mtd_nand_device *nand;
rt_uint8_t *oob_ptr;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
oob_ptr = rt_malloc(nand->oob_size);
if (oob_ptr == RT_NULL)
{
rt_kprintf("out of memory!\n");
return -RT_ENOMEM;
}
/* calculate the page number */
page = block * nand->pages_per_block + page;
rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size,
oob_ptr, nand->oob_size);
mtd_dump_hex(oob_ptr, nand->oob_size);
rt_free(oob_ptr);
return 0;
}
int mtd_nand_write(const char *name, int block, int page)
{
rt_err_t result;
rt_uint8_t *page_ptr;
rt_uint8_t *oob_ptr;
rt_uint32_t index;
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
page_ptr = rt_malloc(nand->page_size + nand->oob_size);
if (page_ptr == RT_NULL)
{
rt_kprintf("out of memory!\n");
return -RT_ENOMEM;
}
oob_ptr = page_ptr + nand->page_size;
/* prepare page data */
for (index = 0; index < nand->page_size; index ++)
{
page_ptr[index] = index & 0xff;
}
/* prepare oob data */
for (index = 0; index < nand->oob_size; index ++)
{
oob_ptr[index] = index & 0xff;
}
/* calculate the page number */
page = block * nand->pages_per_block + page;
result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size,
oob_ptr, nand->oob_size);
if (result != RT_MTD_EOK)
{
rt_kprintf("write page failed!, rc=%d\n", result);
}
rt_free(page_ptr);
return 0;
}
int mtd_nand_erase(const char *name, int block)
{
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
return rt_mtd_nand_erase_block(nand, block);
}
int mtd_nand_erase_all(const char *name)
{
rt_uint32_t index = 0;
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
for (index = 0; index < (nand->block_end - nand->block_start); index ++)
{
rt_mtd_nand_erase_block(nand, index);
}
return 0;
}
#ifdef FINSH_USING_MSH
static void mtd_nand(int argc, char **argv)
{
/* If the number of arguments less than 2 */
if (argc < 3)
{
help:
rt_kprintf("\n");
rt_kprintf("mtd_nand [OPTION] [PARAM ...]\n");
rt_kprintf(" id <name> Get nandid by given name\n");
rt_kprintf(" read <name> <bn> <pn> Read data on page <pn> of block <bn> of device <name>\n");
rt_kprintf(" readoob <name> <bn> <pn> Read oob on page <pn> of block <bn> of device <name>\n");
rt_kprintf(" write <name> <bn> <pn> Run write test on page <pn> of block <bn> of device <name>\n");
rt_kprintf(" erase <name> <bn> Erase on block <bn> of device <name>\n");
rt_kprintf(" eraseall <name> Erase all block on device <name>\n");
return ;
}
else if (!strcmp(argv[1], "id"))
{
mtd_nandid(argv[2]);
}
else if (!strcmp(argv[1], "read"))
{
if (argc < 5)
{
rt_kprintf("The input parameters are too few!\n");
goto help;
}
mtd_nand_read(argv[2], atoi(argv[3]), atoi(argv[4]));
}
else if (!strcmp(argv[1], "readoob"))
{
if (argc < 5)
{
rt_kprintf("The input parameters are too few!\n");
goto help;
}
mtd_nand_readoob(argv[2], atoi(argv[3]), atoi(argv[4]));
}
else if (!strcmp(argv[1], "write"))
{
if (argc < 5)
{
rt_kprintf("The input parameters are too few!\n");
goto help;
}
mtd_nand_write(argv[2], atoi(argv[3]), atoi(argv[4]));
}
else if (!strcmp(argv[1], "erase"))
{
if (argc < 4)
{
rt_kprintf("The input parameters are too few!\n");
goto help;
}
mtd_nand_erase(argv[2], atoi(argv[3]));
}
else if (!strcmp(argv[1], "eraseall"))
{
mtd_nand_erase_all(argv[2]);
}
else
{
rt_kprintf("Input parameters are not supported!\n");
goto help;
}
}
MSH_CMD_EXPORT(mtd_nand, MTD nand device test function);
#endif /* FINSH_USING_MSH */
#ifndef FINSH_USING_MSH_ONLY
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nandid, nand_id, read ID - nandid(name));
int mtd_nand_read(const char* name, int block, int page)
{
rt_err_t result;
rt_uint8_t *page_ptr;
rt_uint8_t *oob_ptr;
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
page_ptr = rt_malloc(nand->page_size + nand->oob_size);
if (page_ptr == RT_NULL)
{
rt_kprintf("out of memory!\n");
return -RT_ENOMEM;
}
oob_ptr = page_ptr + nand->page_size;
rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size);
/* calculate the page number */
page = block * nand->pages_per_block + page;
result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size,
oob_ptr, nand->oob_size);
rt_kprintf("read page, rc=%d\n", result);
mtd_dump_hex(page_ptr, nand->page_size);
mtd_dump_hex(oob_ptr, nand->oob_size);
rt_free(page_ptr);
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_read, nand_read, read page in nand - nand_read(name, block, page));
int mtd_nand_readoob(const char* name, int block, int page)
{
struct rt_mtd_nand_device *nand;
rt_uint8_t *oob_ptr;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
oob_ptr = rt_malloc(nand->oob_size);
if (oob_ptr == RT_NULL)
{
rt_kprintf("out of memory!\n");
return -RT_ENOMEM;
}
/* calculate the page number */
page = block * nand->pages_per_block + page;
rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size,
oob_ptr, nand->oob_size);
mtd_dump_hex(oob_ptr, nand->oob_size);
rt_free(oob_ptr);
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_readoob, nand_readoob, read spare data in nand - nand_readoob(name, block, page));
int mtd_nand_write(const char* name, int block, int page)
{
rt_err_t result;
rt_uint8_t *page_ptr;
rt_uint8_t *oob_ptr;
rt_uint32_t index;
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
page_ptr = rt_malloc(nand->page_size + nand->oob_size);
if (page_ptr == RT_NULL)
{
rt_kprintf("out of memory!\n");
return -RT_ENOMEM;
}
oob_ptr = page_ptr + nand->page_size;
/* prepare page data */
for (index = 0; index < nand->page_size; index ++)
{
page_ptr[index] = index & 0xff;
}
/* prepare oob data */
for (index = 0; index < nand->oob_size; index ++)
{
oob_ptr[index] = index & 0xff;
}
/* calculate the page number */
page = block * nand->pages_per_block + page;
result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size,
oob_ptr, nand->oob_size);
if (result != RT_MTD_EOK)
{
rt_kprintf("write page failed!, rc=%d\n", result);
}
rt_free(page_ptr);
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_write, nand_write, write dump data to nand - nand_write(name, block, page));
int mtd_nand_erase(const char* name, int block)
{
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
return rt_mtd_nand_erase_block(nand, block);
}
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase, nand_erase, nand_erase(name, block));
int mtd_nand_erase_all(const char* name)
{
rt_uint32_t index = 0;
struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL)
{
rt_kprintf("no nand device found!\n");
return -RT_ERROR;
}
for (index = 0; index < (nand->block_end - nand->block_start); index ++)
{
rt_mtd_nand_erase_block(nand, index);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase_all, nand_erase_all, erase all of nand device - nand_erase_all(name, block));
#endif
#endif /* FINSH_USING_MSH_ONLY */
#endif
#endif /* defined(RT_MTD_NAND_DEBUG) && defined(RT_USING_FINSH) */
#endif /* RT_USING_MTD_NAND */