summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/core.c')
-rw-r--r--drivers/mtd/core.c147
1 files changed, 73 insertions, 74 deletions
diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 322a99bd21..a4f91bee91 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -26,16 +26,16 @@
#include <nand.h>
#include <errno.h>
-static ssize_t nand_read(struct cdev *cdev, void* buf, size_t count,
+static ssize_t mtd_read(struct cdev *cdev, void* buf, size_t count,
ulong offset, ulong flags)
{
- struct mtd_info *info = cdev->priv;
+ struct mtd_info *mtd = cdev->priv;
size_t retlen;
int ret;
- debug("nand_read: 0x%08lx 0x%08x\n", offset, count);
+ debug("mtd_read: 0x%08lx 0x%08x\n", offset, count);
- ret = info->read(info, offset, count, &retlen, buf);
+ ret = mtd->read(mtd, offset, count, &retlen, buf);
if(ret) {
printf("err %d\n", ret);
@@ -44,10 +44,10 @@ static ssize_t nand_read(struct cdev *cdev, void* buf, size_t count,
return retlen;
}
-#define NOTALIGNED(x) (x & (info->writesize - 1)) != 0
-#define MTDPGALG(x) ((x) & (info->writesize - 1))
+#define NOTALIGNED(x) (x & (mtd->writesize - 1)) != 0
+#define MTDPGALG(x) ((x) & (mtd->writesize - 1))
-#ifdef CONFIG_NAND_WRITE
+#ifdef CONFIG_MTD_WRITE
static int all_ff(const void *buf, int len)
{
int i;
@@ -59,10 +59,10 @@ static int all_ff(const void *buf, int len)
return 1;
}
-static ssize_t nand_write(struct cdev* cdev, const void *buf, size_t _count,
+static ssize_t mtd_write(struct cdev* cdev, const void *buf, size_t _count,
ulong offset, ulong flags)
{
- struct mtd_info *info = cdev->priv;
+ struct mtd_info *mtd = cdev->priv;
size_t retlen, now;
int ret = 0;
void *wrbuf = NULL;
@@ -75,23 +75,23 @@ static ssize_t nand_write(struct cdev* cdev, const void *buf, size_t _count,
dev_dbg(cdev->dev, "write: 0x%08lx 0x%08x\n", offset, count);
while (count) {
- now = count > info->writesize ? info->writesize : count;
+ now = count > mtd->writesize ? mtd->writesize : count;
if (NOTALIGNED(now)) {
dev_dbg(cdev->dev, "not aligned: %d %ld\n",
- info->writesize,
- (offset % info->writesize));
- wrbuf = xmalloc(info->writesize);
- memset(wrbuf, 0xff, info->writesize);
- memcpy(wrbuf + (offset % info->writesize), buf, now);
- if (!all_ff(wrbuf, info->writesize))
- ret = info->write(info, MTDPGALG(offset),
- info->writesize, &retlen,
+ mtd->writesize,
+ (offset % mtd->writesize));
+ wrbuf = xmalloc(mtd->writesize);
+ memset(wrbuf, 0xff, mtd->writesize);
+ memcpy(wrbuf + (offset % mtd->writesize), buf, now);
+ if (!all_ff(wrbuf, mtd->writesize))
+ ret = mtd->write(mtd, MTDPGALG(offset),
+ mtd->writesize, &retlen,
wrbuf);
free(wrbuf);
} else {
- if (!all_ff(buf, info->writesize))
- ret = info->write(info, offset, now, &retlen,
+ if (!all_ff(buf, mtd->writesize))
+ ret = mtd->write(mtd, offset, now, &retlen,
buf);
dev_dbg(cdev->dev,
"offset: 0x%08lx now: 0x%08x retlen: 0x%08x\n",
@@ -110,27 +110,27 @@ out:
}
#endif
-static int nand_ioctl(struct cdev *cdev, int request, void *buf)
+static int mtd_ioctl(struct cdev *cdev, int request, void *buf)
{
- struct mtd_info *info = cdev->priv;
+ struct mtd_info *mtd = cdev->priv;
struct mtd_info_user *user = buf;
switch (request) {
case MEMGETBADBLOCK:
dev_dbg(cdev->dev, "MEMGETBADBLOCK: 0x%08lx\n", (off_t)buf);
- return info->block_isbad(info, (off_t)buf);
-#ifdef CONFIG_NAND_WRITE
+ return mtd->block_isbad(mtd, (off_t)buf);
+#ifdef CONFIG_MTD_WRITE
case MEMSETBADBLOCK:
dev_dbg(cdev->dev, "MEMSETBADBLOCK: 0x%08lx\n", (off_t)buf);
- return info->block_markbad(info, (off_t)buf);
+ return mtd->block_markbad(mtd, (off_t)buf);
#endif
case MEMGETINFO:
- user->type = info->type;
- user->flags = info->flags;
- user->size = info->size;
- user->erasesize = info->erasesize;
- user->oobsize = info->oobsize;
- user->mtd = info;
+ user->type = mtd->type;
+ user->flags = mtd->flags;
+ user->size = mtd->size;
+ user->erasesize = mtd->erasesize;
+ user->oobsize = mtd->oobsize;
+ user->mtd = mtd;
/* The below fields are obsolete */
user->ecctype = -1;
user->eccsize = 0;
@@ -140,88 +140,85 @@ static int nand_ioctl(struct cdev *cdev, int request, void *buf)
return 0;
}
-#ifdef CONFIG_NAND_WRITE
-static ssize_t nand_erase(struct cdev *cdev, size_t count, unsigned long offset)
+#ifdef CONFIG_MTD_WRITE
+static ssize_t mtd_erase(struct cdev *cdev, size_t count, unsigned long offset)
{
- struct mtd_info *info = cdev->priv;
+ struct mtd_info *mtd = cdev->priv;
struct erase_info erase;
int ret;
memset(&erase, 0, sizeof(erase));
- erase.mtd = info;
+ erase.mtd = mtd;
erase.addr = offset;
- erase.len = info->erasesize;
+ erase.len = mtd->erasesize;
while (count > 0) {
dev_dbg(cdev->dev, "erase %d %d\n", erase.addr, erase.len);
- ret = info->block_isbad(info, erase.addr);
+ ret = mtd->block_isbad(mtd, erase.addr);
if (ret > 0) {
printf("Skipping bad block at 0x%08x\n", erase.addr);
} else {
- ret = info->erase(info, &erase);
+ ret = mtd->erase(mtd, &erase);
if (ret)
return ret;
}
- erase.addr += info->erasesize;
- count -= count > info->erasesize ? info->erasesize : count;
+ erase.addr += mtd->erasesize;
+ count -= count > mtd->erasesize ? mtd->erasesize : count;
}
return 0;
}
#endif
-static struct file_operations nand_ops = {
- .read = nand_read,
-#ifdef CONFIG_NAND_WRITE
- .write = nand_write,
- .erase = nand_erase,
+static struct file_operations mtd_ops = {
+ .read = mtd_read,
+#ifdef CONFIG_MTD_WRITE
+ .write = mtd_write,
+ .erase = mtd_erase,
#endif
- .ioctl = nand_ioctl,
+ .ioctl = mtd_ioctl,
.lseek = dev_lseek_default,
};
#ifdef CONFIG_NAND_OOB_DEVICE
-static ssize_t nand_read_oob(struct cdev *cdev, void *buf, size_t count,
- ulong offset, ulong flags)
+static ssize_t mtd_read_oob(struct cdev *cdev, void *buf, size_t count,
+ ulong offset, ulong flags)
{
- struct mtd_info *info = cdev->priv;
- struct nand_chip *chip = info->priv;
+ struct mtd_info *mtd = cdev->priv;
struct mtd_oob_ops ops;
int ret;
- if (count < info->oobsize)
+ if (count < mtd->oobsize)
return -EINVAL;
ops.mode = MTD_OOB_RAW;
ops.ooboffs = 0;
- ops.ooblen = info->oobsize;
+ ops.ooblen = mtd->oobsize;
ops.oobbuf = buf;
ops.datbuf = NULL;
- ops.len = info->oobsize;
+ ops.len = mtd->oobsize;
- offset /= info->oobsize;
- ret = info->read_oob(info, offset << chip->page_shift, &ops);
+ offset /= mtd->oobsize;
+ ret = mtd->read_oob(mtd, offset * mtd->writesize, &ops);
if (ret)
return ret;
- return info->oobsize;
+ return mtd->oobsize;
}
-static struct file_operations nand_ops_oob = {
- .read = nand_read_oob,
- .ioctl = nand_ioctl,
+static struct file_operations mtd_ops_oob = {
+ .read = mtd_read_oob,
+ .ioctl = mtd_ioctl,
.lseek = dev_lseek_default,
};
-static int nand_init_oob_cdev(struct mtd_info *mtd)
+static int mtd_init_oob_cdev(struct mtd_info *mtd, char *devname)
{
- struct nand_chip *chip = mtd->priv;
-
- mtd->cdev_oob.ops = &nand_ops_oob;
- mtd->cdev_oob.size = (mtd->size >> chip->page_shift) * mtd->oobsize;
- mtd->cdev_oob.name = asprintf("nand_oob%d", mtd->class_dev.id);
+ mtd->cdev_oob.ops = &mtd_ops_oob;
+ mtd->cdev_oob.size = (mtd->size / mtd->writesize) * mtd->oobsize;
+ mtd->cdev_oob.name = asprintf("%s_oob%d", devname, mtd->class_dev.id);
mtd->cdev_oob.priv = mtd;
mtd->cdev_oob.dev = &mtd->class_dev;
devfs_create(&mtd->cdev_oob);
@@ -229,33 +226,35 @@ static int nand_init_oob_cdev(struct mtd_info *mtd)
return 0;
}
-static void nand_exit_oob_cdev(struct mtd_info *mtd)
+static void mtd_exit_oob_cdev(struct mtd_info *mtd)
{
free(mtd->cdev_oob.name);
}
#else
-static int nand_init_oob_cdev(struct mtd_info *mtd)
+static int mtd_init_oob_cdev(struct mtd_info *mtd, char *devname)
{
return 0;
}
-static void nand_exit_oob_cdev(struct mtd_info *mtd)
+static void mtd_exit_oob_cdev(struct mtd_info *mtd)
{
return;
}
#endif
-int add_mtd_device(struct mtd_info *mtd)
+int add_mtd_device(struct mtd_info *mtd, char *devname)
{
char str[16];
- strcpy(mtd->class_dev.name, "nand");
+ if (!devname)
+ devname = "mtd";
+ strcpy(mtd->class_dev.name, devname);
register_device(&mtd->class_dev);
- mtd->cdev.ops = &nand_ops;
+ mtd->cdev.ops = &mtd_ops;
mtd->cdev.size = mtd->size;
- mtd->cdev.name = asprintf("nand%d", mtd->class_dev.id);
+ mtd->cdev.name = asprintf("%s%d", devname, mtd->class_dev.id);
mtd->cdev.priv = mtd;
mtd->cdev.dev = &mtd->class_dev;
mtd->cdev.mtd = mtd;
@@ -271,7 +270,7 @@ int add_mtd_device(struct mtd_info *mtd)
devfs_create(&mtd->cdev);
- nand_init_oob_cdev(mtd);
+ mtd_init_oob_cdev(mtd, devname);
return 0;
}
@@ -279,7 +278,7 @@ int add_mtd_device(struct mtd_info *mtd)
int del_mtd_device (struct mtd_info *mtd)
{
unregister_device(&mtd->class_dev);
- nand_exit_oob_cdev(mtd);
+ mtd_exit_oob_cdev(mtd);
free(mtd->param_size.value);
free(mtd->cdev.name);
return 0;