9
0
Fork 0

mtd: nand: bb: use mtd api directly

The devfs layer just adds an addition indirection between mtd
and the bb devices with no purpose. Drop it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2014-04-09 15:49:32 +02:00
parent ebb8a4b594
commit b32cd8df87
1 changed files with 51 additions and 73 deletions

View File

@ -25,22 +25,18 @@
#include <init.h> #include <init.h>
#include <ioctl.h> #include <ioctl.h>
#include <nand.h> #include <nand.h>
#include <linux/mtd/mtd-abi.h> #include <linux/mtd/mtd.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <linux/list.h> #include <linux/list.h>
struct nand_bb { struct nand_bb {
char cdevname[MAX_DRIVER_NAME];
struct cdev *cdev_parent;
char *name; char *name;
int open; int open;
int needs_write; int needs_write;
struct mtd_info_user info; struct mtd_info *mtd;
loff_t raw_size;
loff_t size;
loff_t offset; loff_t offset;
unsigned long flags; unsigned long flags;
void *writebuf; void *writebuf;
@ -54,31 +50,28 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
loff_t offset, ulong flags) loff_t offset, ulong flags)
{ {
struct nand_bb *bb = cdev->priv; struct nand_bb *bb = cdev->priv;
struct cdev *parent = bb->cdev_parent; size_t retlen;
int ret, bytes = 0, now; int ret, bytes = 0, now;
debug("%s 0x%08llx %d\n", __func__, offset, count); debug("%s 0x%08llx %d\n", __func__, offset, count);
while(count) { while (count) {
ret = cdev_ioctl(parent, MEMGETBADBLOCK, &bb->offset); if (mtd_block_isbad(bb->mtd, offset)) {
if (ret < 0)
return ret;
if (ret) {
printf("skipping bad block at 0x%08llx\n", bb->offset); printf("skipping bad block at 0x%08llx\n", bb->offset);
bb->offset += bb->info.erasesize; bb->offset += bb->mtd->erasesize;
continue; continue;
} }
now = min(count, (size_t)(bb->info.erasesize - now = min(count, (size_t)(bb->mtd->erasesize -
((size_t)bb->offset % bb->info.erasesize))); ((size_t)bb->offset % bb->mtd->erasesize)));
ret = cdev_read(parent, buf, now, bb->offset, 0);
ret = mtd_read(bb->mtd, bb->offset, now, &retlen, buf);
if (ret < 0) if (ret < 0)
return ret; return ret;
buf += now; buf += retlen;
count -= now; count -= retlen;
bb->offset += now; bb->offset += retlen;
bytes += now; bytes += retlen;
}; };
return bytes; return bytes;
@ -91,29 +84,25 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
static int nand_bb_write_buf(struct nand_bb *bb, size_t count) static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
{ {
int ret, now; int ret, now;
struct cdev *parent = bb->cdev_parent; size_t retlen;
void *buf = bb->writebuf; void *buf = bb->writebuf;
loff_t cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1); loff_t cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
while (count) { while (count) {
ret = cdev_ioctl(parent, MEMGETBADBLOCK, &cur_ofs); if (mtd_block_isbad(bb->mtd, cur_ofs)) {
if (ret < 0)
return ret;
if (ret) {
debug("skipping bad block at 0x%08llx\n", cur_ofs); debug("skipping bad block at 0x%08llx\n", cur_ofs);
bb->offset += bb->info.erasesize; bb->offset += bb->mtd->erasesize;
cur_ofs += bb->info.erasesize; cur_ofs += bb->mtd->erasesize;
continue; continue;
} }
now = min(count, (size_t)(bb->info.erasesize)); now = min(count, (size_t)(bb->mtd->erasesize));
ret = cdev_write(parent, buf, now, cur_ofs, 0); ret = mtd_write(bb->mtd, cur_ofs, now, &retlen, buf);
if (ret < 0) if (ret < 0)
return ret; return ret;
buf += now; buf += retlen;
count -= now; count -= retlen;
cur_ofs += now; cur_ofs += retlen;
}; };
return 0; return 0;
@ -152,13 +141,17 @@ static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
static int nand_bb_erase(struct cdev *cdev, size_t count, loff_t offset) static int nand_bb_erase(struct cdev *cdev, size_t count, loff_t offset)
{ {
struct nand_bb *bb = cdev->priv; struct nand_bb *bb = cdev->priv;
struct erase_info erase = {};
if (offset != 0) { if (offset != 0) {
printf("can only erase from beginning of device\n"); printf("can only erase from beginning of device\n");
return -EINVAL; return -EINVAL;
} }
return cdev_erase(bb->cdev_parent, bb->raw_size, 0); erase.addr = 0;
erase.len = bb->mtd->size;
return mtd_erase(bb->mtd, &erase);
} }
#endif #endif
@ -195,16 +188,12 @@ static int nand_bb_close(struct cdev *cdev)
static int nand_bb_calc_size(struct nand_bb *bb) static int nand_bb_calc_size(struct nand_bb *bb)
{ {
loff_t pos = 0; loff_t pos = 0;
int ret;
while (pos < bb->raw_size) { while (pos < bb->mtd->size) {
ret = cdev_ioctl(bb->cdev_parent, MEMGETBADBLOCK, &pos); if (!mtd_block_isbad(bb->mtd, pos))
if (ret < 0) bb->cdev.size += bb->mtd->erasesize;
return ret;
if (!ret)
bb->cdev.size += bb->info.erasesize;
pos += bb->info.erasesize; pos += bb->mtd->erasesize;
} }
return 0; return 0;
@ -215,22 +204,18 @@ static loff_t nand_bb_lseek(struct cdev *cdev, loff_t __offset)
struct nand_bb *bb = cdev->priv; struct nand_bb *bb = cdev->priv;
loff_t raw_pos = 0; loff_t raw_pos = 0;
uint32_t offset = __offset; uint32_t offset = __offset;
int ret;
/* lseek only in readonly mode */ /* lseek only in readonly mode */
if (bb->flags & O_ACCMODE) if (bb->flags & O_ACCMODE)
return -ENOSYS; return -ENOSYS;
while (raw_pos < bb->raw_size) { while (raw_pos < bb->mtd->size) {
off_t now = min(offset, bb->info.erasesize); off_t now = min(offset, bb->mtd->erasesize);
ret = cdev_ioctl(bb->cdev_parent, MEMGETBADBLOCK, &raw_pos); if (mtd_block_isbad(bb->mtd, raw_pos)) {
if (ret < 0) raw_pos += bb->mtd->erasesize;
return ret; } else {
if (!ret) {
offset -= now; offset -= now;
raw_pos += now; raw_pos += now;
} else {
raw_pos += bb->info.erasesize;
} }
if (!offset) { if (!offset) {
@ -264,28 +249,23 @@ static LIST_HEAD(bb_list);
int dev_add_bb_dev(const char *path, const char *name) int dev_add_bb_dev(const char *path, const char *name)
{ {
struct nand_bb *bb; struct nand_bb *bb;
struct cdev *parent;
int ret = -ENOMEM; int ret = -ENOMEM;
parent = cdev_by_name(path);
if (!parent)
return -ENODEV;
if (!parent->mtd)
return -EINVAL;
bb = xzalloc(sizeof(*bb)); bb = xzalloc(sizeof(*bb));
bb->mtd = parent->mtd;
bb->cdev_parent = cdev_open(path, O_RDWR); if (name)
if (!bb->cdev_parent) bb->cdev.name = xstrdup(name);
goto out1; else
bb->cdev.name = asprintf("%s.bb", path);;
if (name) {
strcpy(bb->cdevname, name);
} else {
strcpy(bb->cdevname, path);
strcat(bb->cdevname, ".bb");
}
bb->cdev.name = bb->cdevname;
bb->raw_size = bb->cdev_parent->size;
ret = cdev_ioctl(bb->cdev_parent, MEMGETINFO, &bb->info);
if (ret)
goto out4;
nand_bb_calc_size(bb); nand_bb_calc_size(bb);
bb->cdev.ops = &nand_bb_ops; bb->cdev.ops = &nand_bb_ops;
@ -300,8 +280,6 @@ int dev_add_bb_dev(const char *path, const char *name)
return 0; return 0;
out4: out4:
cdev_close(bb->cdev_parent);
out1:
free(bb); free(bb);
return ret; return ret;
} }
@ -313,8 +291,8 @@ int dev_remove_bb_dev(const char *name)
list_for_each_entry(bb, &bb_list, list) { list_for_each_entry(bb, &bb_list, list) {
if (!strcmp(bb->cdev.name, name)) { if (!strcmp(bb->cdev.name, name)) {
devfs_remove(&bb->cdev); devfs_remove(&bb->cdev);
cdev_close(bb->cdev_parent);
list_del_init(&bb->list); list_del_init(&bb->list);
free(bb->name);
free(bb); free(bb);
return 0; return 0;
} }