9
0
Fork 0

drivers/mtd: transfer NAND notions to MTD core

Change NAND_WRITE into MTD_WRITE.
Change "page_shift" references in the core, which are purely
NAND, into mtd->writesize which is MTD generic.
Rename all "info" (struct mtd_info) into "mtd".

Also provide a parameter to add_mtd_device() so that legacy
nand devices still appear as nand<N>.

Signed-off-by: Robert Jarzmik <robert.jarzmik@free.fr>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Robert Jarzmik 2011-12-21 22:30:40 +01:00 committed by Sascha Hauer
parent f76ad819e4
commit 88ce7ef769
17 changed files with 109 additions and 105 deletions

View File

@ -3,6 +3,16 @@ menuconfig MTD
if MTD
config MTD_WRITE
bool
default y
prompt "Support writing to MTD devices"
config MTD_READ_OOB
bool
default y
prompt "Create a device for reading the OOB data"
source "drivers/mtd/nand/Kconfig"
source "drivers/mtd/ubi/Kconfig"

View File

@ -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;

View File

@ -8,11 +8,6 @@ menuconfig NAND
if NAND
config NAND_WRITE
bool
default y
prompt "Support writing to Nand"
config NAND_ECC_SOFT
bool
default y

View File

@ -1,7 +1,7 @@
# Generic NAND options
obj-$(CONFIG_NAND) += nand_ecc.o
obj-$(CONFIG_NAND_WRITE) += nand_write.o
obj-$(CONFIG_MTD_WRITE) += nand_write.o
obj-$(CONFIG_NAND_ECC_SOFT) += nand_ecc.o nand_swecc.o
obj-$(CONFIG_NAND_ECC_HW) += nand_hwecc.o
obj-$(CONFIG_NAND_ECC_HW_SYNDROME) += nand_hwecc_syndrome.o

View File

@ -485,7 +485,7 @@ static int __init atmel_nand_probe(struct device_d *dev)
goto err_scan_tail;
}
add_mtd_device(mtd);
add_mtd_device(mtd, "nand");
if (!res)
return res;

View File

@ -1358,7 +1358,7 @@ static int __init nftl_scan_bbt(struct mtd_info *mtd)
At least as nand_bbt.c is currently written. */
if ((ret = nand_scan_bbt(mtd, NULL)))
return ret;
add_mtd_device(mtd);
add_mtd_device(mtd, "nand");
#ifdef CONFIG_MTD_PARTITIONS
if (!no_autopart)
add_mtd_partitions(mtd, parts, numparts);

View File

@ -90,7 +90,7 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
/* Must be a multiple of the largest NAND page size */
#define BB_WRITEBUF_SIZE 4096
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
{
int ret, now;
@ -185,7 +185,7 @@ static int nand_bb_close(struct cdev *cdev)
{
struct nand_bb *bb = cdev->priv;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
if (bb->needs_write)
nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE);
#endif
@ -250,7 +250,7 @@ static struct file_operations nand_bb_ops = {
.close = nand_bb_close,
.read = nand_bb_read,
.lseek = nand_bb_lseek,
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
.write = nand_bb_write,
.erase = nand_bb_erase,
#endif

View File

@ -1006,7 +1006,7 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
chip->read_word = nand_read_word;
if (!chip->block_bad)
chip->block_bad = nand_block_bad;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
if (!chip->block_markbad)
chip->block_markbad = nand_default_block_markbad;
if (!chip->write_buf)
@ -1168,7 +1168,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
/* Check for AND chips with 4 page planes */
if (chip->options & NAND_4PAGE_ARRAY)
chip->erase_cmd = multi_erase_cmd;
@ -1297,7 +1297,7 @@ int nand_scan_tail(struct mtd_info *mtd)
}
}
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
if (!chip->write_page)
chip->write_page = nand_write_page;
#endif
@ -1308,7 +1308,7 @@ int nand_scan_tail(struct mtd_info *mtd)
*/
if (!chip->ecc.read_page_raw)
chip->ecc.read_page_raw = nand_read_page_raw;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
if (!chip->ecc.write_page_raw)
chip->ecc.write_page_raw = nand_write_page_raw;
#endif
@ -1335,7 +1335,7 @@ int nand_scan_tail(struct mtd_info *mtd)
printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
"This is not recommended !!\n");
chip->ecc.read_page = nand_read_page_raw;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
chip->ecc.write_page = nand_write_page_raw;
chip->ecc.write_oob = nand_write_oob_std;
#endif
@ -1401,7 +1401,7 @@ int nand_scan_tail(struct mtd_info *mtd)
/* Fill in remaining MTD driver data */
mtd->type = MTD_NANDFLASH;
mtd->flags = MTD_CAP_NANDFLASH;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
mtd->erase = nand_erase;
mtd->write = nand_write;
mtd->write_oob = nand_write_oob;
@ -1413,7 +1413,7 @@ int nand_scan_tail(struct mtd_info *mtd)
mtd->lock = NULL;
mtd->unlock = NULL;
mtd->block_isbad = nand_block_isbad;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
mtd->block_markbad = nand_block_markbad;
#endif
/* propagate ecc.layout to mtd_info */

View File

@ -557,7 +557,7 @@ static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt
* (Re)write the bad block table
*
*/
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
struct nand_bbt_descr *td, struct nand_bbt_descr *md,
int chipsel)

View File

@ -61,7 +61,7 @@ static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
* @chip: nand chip info structure
* @buf: data buffer
*/
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
const uint8_t *buf)
{
@ -94,7 +94,7 @@ void nand_init_ecc_hw(struct nand_chip *chip)
if (!chip->ecc.read_oob)
chip->ecc.read_oob = nand_read_oob_std;
#endif
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
if (!chip->ecc.write_oob)
chip->ecc.write_oob = nand_write_oob_std;
if (!chip->ecc.write_page)

View File

@ -72,7 +72,7 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
* The hw generator calculates the error syndrome automatically. Therefor
* we need a special oob layout and handling.
*/
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
static void nand_write_page_syndrome(struct mtd_info *mtd,
struct nand_chip *chip, const uint8_t *buf)
{
@ -155,7 +155,7 @@ static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
* @chip: nand chip info structure
* @page: page number to write
*/
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
static int nand_write_oob_syndrome(struct mtd_info *mtd,
struct nand_chip *chip, int page)
{
@ -216,7 +216,7 @@ void nand_init_ecc_hw_syndrome(struct nand_chip *chip)
chip->ecc.read_page = nand_read_page_syndrome;
if (!chip->ecc.read_oob)
chip->ecc.read_oob = nand_read_oob_syndrome;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
if (!chip->ecc.write_page)
chip->ecc.write_page = nand_write_page_syndrome;
if (!chip->ecc.write_oob)

View File

@ -1176,7 +1176,7 @@ static int __init imxnd_probe(struct device_d *dev)
goto escan;
}
add_mtd_device(mtd);
add_mtd_device(mtd, "nand");
dev->priv = host;

View File

@ -956,7 +956,7 @@ static int gpmc_nand_probe(struct device_d *pdev)
dev_set_param(pdev, "eccmode", ecc_mode_strings[pdata->ecc_mode]);
/* We are all set to register with the system now! */
err = add_mtd_device(minfo);
err = add_mtd_device(minfo, "nand");
if (err) {
dev_dbg(pdev, "device registration failed\n");
goto out_release_mem;

View File

@ -485,7 +485,7 @@ static int s3c24x0_nand_probe(struct device_d *dev)
goto on_error;
}
return add_mtd_device(mtd);
return add_mtd_device(mtd, "nand");
on_error:
free(host);

View File

@ -57,7 +57,7 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
* @chip: nand chip info structure
* @buf: data buffer
*/
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
const uint8_t *buf)
{
@ -85,7 +85,7 @@ void nand_init_ecc_soft(struct nand_chip *chip)
chip->ecc.correct = nand_correct_data;
chip->ecc.read_page = nand_read_page_swecc;
chip->ecc.read_oob = nand_read_oob_std;
#ifdef CONFIG_NAND_WRITE
#ifdef CONFIG_MTD_WRITE
chip->ecc.write_page = nand_write_page_swecc;
chip->ecc.write_oob = nand_write_oob_std;
#endif

View File

@ -220,7 +220,7 @@ static int nomadik_nand_probe(struct device_d *dev)
}
pr_info("Registering %s as whole device\n", mtd->name);
add_mtd_device(mtd);
add_mtd_device(mtd, "nand");
return 0;

View File

@ -220,7 +220,7 @@ static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
}
/* Kernel-side ioctl definitions */
extern int add_mtd_device(struct mtd_info *mtd);
extern int add_mtd_device(struct mtd_info *mtd, char *devname);
extern int del_mtd_device (struct mtd_info *mtd);
extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);