提交 8593fbc6 编写于 作者: T Thomas Gleixner

[MTD] Rework the out of band handling completely

Hopefully the last iteration on this!

The handling of out of band data on NAND was accompanied by tons of fruitless
discussions and halfarsed patches to make it work for a particular
problem. Sufficiently annoyed by I all those "I know it better" mails and the
resonable amount of discarded "it solves my problem" patches, I finally decided
to go for the big rework. After removing the _ecc variants of mtd read/write
functions the solution to satisfy the various requirements was to refactor the
read/write _oob functions in mtd.

The major change is that read/write_oob now takes a pointer to an operation
descriptor structure "struct mtd_oob_ops".instead of having a function with at
least seven arguments.

read/write_oob which should probably renamed to a more descriptive name, can do
the following tasks:

- read/write out of band data
- read/write data content and out of band data
- read/write raw data content and out of band data (ecc disabled)

struct mtd_oob_ops has a mode field, which determines the oob handling mode.

Aside of the MTD_OOB_RAW mode, which is intended to be especially for
diagnostic purposes and some internal functions e.g. bad block table creation,
the other two modes are for mtd clients:

MTD_OOB_PLACE puts/gets the given oob data exactly to/from the place which is
described by the ooboffs and ooblen fields of the mtd_oob_ops strcuture. It's
up to the caller to make sure that the byte positions are not used by the ECC
placement algorithms.

MTD_OOB_AUTO puts/gets the given oob data automaticaly to/from the places in
the out of band area which are described by the oobfree tuples in the ecclayout
data structre which is associated to the devicee.

The decision whether data plus oob or oob only handling is done depends on the
setting of the datbuf member of the data structure. When datbuf == NULL then
the internal read/write_oob functions are selected, otherwise the read/write
data routines are invoked.

Tested on a few platforms with all variants. Please be aware of possible
regressions for your particular device / application scenario

Disclaimer: Any whining will be ignored from those who just contributed "hot
air blurb" and never sat down to tackle the underlying problem of the mess in
the NAND driver grown over time and the big chunk of work to fix up the
existing users. The problem was not the holiness of the existing MTD
interfaces. The problems was the lack of time to go for the big overhaul. It's
easy to add more mess to the existing one, but it takes alot of effort to go
for a real solution.

Improvements and bugfixes are welcome!
Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
上级 f4a43cfc
...@@ -59,10 +59,10 @@ static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -59,10 +59,10 @@ static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel); size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel); size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, u_char *buf); struct mtd_oob_ops *ops);
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, const u_char *buf); struct mtd_oob_ops *ops);
static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
size_t *retlen, const u_char *buf); size_t *retlen, const u_char *buf);
static int doc_erase (struct mtd_info *mtd, struct erase_info *instr); static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
...@@ -959,12 +959,18 @@ static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -959,12 +959,18 @@ static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
return 0; return 0;
} }
static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
size_t * retlen, u_char * buf) struct mtd_oob_ops *ops)
{ {
struct DiskOnChip *this = mtd->priv; struct DiskOnChip *this = mtd->priv;
int len256 = 0, ret; int len256 = 0, ret;
struct Nand *mychip; struct Nand *mychip;
uint8_t *buf = ops->oobbuf;
size_t len = ops->len;
BUG_ON(ops->mode != MTD_OOB_PLACE);
ofs += ops->ooboffs;
mutex_lock(&this->lock); mutex_lock(&this->lock);
...@@ -1005,7 +1011,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -1005,7 +1011,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
DoC_ReadBuf(this, &buf[len256], len - len256); DoC_ReadBuf(this, &buf[len256], len - len256);
*retlen = len; ops->retlen = len;
/* Reading the full OOB data drops us off of the end of the page, /* Reading the full OOB data drops us off of the end of the page,
* causing the flash device to go into busy mode, so we need * causing the flash device to go into busy mode, so we need
* to wait until ready 11.4.1 and Toshiba TC58256FT docs */ * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
...@@ -1120,17 +1126,20 @@ static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -1120,17 +1126,20 @@ static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
} }
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
size_t * retlen, const u_char * buf) struct mtd_oob_ops *ops)
{ {
struct DiskOnChip *this = mtd->priv; struct DiskOnChip *this = mtd->priv;
int ret; int ret;
mutex_lock(&this->lock); BUG_ON(ops->mode != MTD_OOB_PLACE);
ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
mutex_lock(&this->lock);
ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
&ops->retlen, ops->oobbuf);
mutex_unlock(&this->lock); mutex_unlock(&this->lock);
return ret; return ret;
} }
static int doc_erase(struct mtd_info *mtd, struct erase_info *instr) static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
......
...@@ -43,10 +43,10 @@ static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -43,10 +43,10 @@ static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf, u_char *eccbuf, size_t *retlen, const u_char *buf, u_char *eccbuf,
struct nand_oobinfo *oobsel); struct nand_oobinfo *oobsel);
static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, u_char *buf); struct mtd_oob_ops *ops);
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, const u_char *buf); struct mtd_oob_ops *ops);
static int doc_erase (struct mtd_info *mtd, struct erase_info *instr); static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
static struct mtd_info *docmillist = NULL; static struct mtd_info *docmillist = NULL;
...@@ -662,8 +662,8 @@ static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len, ...@@ -662,8 +662,8 @@ static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
return ret; return ret;
} }
static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, u_char *buf) struct mtd_oob_ops *ops)
{ {
#ifndef USE_MEMCPY #ifndef USE_MEMCPY
int i; int i;
...@@ -672,6 +672,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -672,6 +672,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
struct DiskOnChip *this = mtd->priv; struct DiskOnChip *this = mtd->priv;
void __iomem *docptr = this->virtadr; void __iomem *docptr = this->virtadr;
struct Nand *mychip = &this->chips[ofs >> this->chipshift]; struct Nand *mychip = &this->chips[ofs >> this->chipshift];
uint8_t *buf = ops->oobbuf;
size_t len = ops->len;
BUG_ON(ops->mode != MTD_OOB_PLACE);
ofs += ops->ooboffs;
/* Find the chip which is to be used and select it */ /* Find the chip which is to be used and select it */
if (this->curfloor != mychip->floor) { if (this->curfloor != mychip->floor) {
...@@ -708,13 +714,13 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -708,13 +714,13 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
#endif #endif
buf[len - 1] = ReadDOC(docptr, LastDataRead); buf[len - 1] = ReadDOC(docptr, LastDataRead);
*retlen = len; ops->retlen = len;
return 0; return 0;
} }
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, const u_char *buf) struct mtd_oob_ops *ops)
{ {
#ifndef USE_MEMCPY #ifndef USE_MEMCPY
int i; int i;
...@@ -724,6 +730,12 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -724,6 +730,12 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
struct DiskOnChip *this = mtd->priv; struct DiskOnChip *this = mtd->priv;
void __iomem *docptr = this->virtadr; void __iomem *docptr = this->virtadr;
struct Nand *mychip = &this->chips[ofs >> this->chipshift]; struct Nand *mychip = &this->chips[ofs >> this->chipshift];
uint8_t *buf = ops->oobbuf;
size_t len = ops->len;
BUG_ON(ops->mode != MTD_OOB_PLACE);
ofs += ops->ooboffs;
/* Find the chip which is to be used and select it */ /* Find the chip which is to be used and select it */
if (this->curfloor != mychip->floor) { if (this->curfloor != mychip->floor) {
...@@ -775,12 +787,12 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -775,12 +787,12 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
if (ReadDOC(docptr, Mil_CDSN_IO) & 1) { if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
printk("Error programming oob data\n"); printk("Error programming oob data\n");
/* FIXME: implement Bad Block Replacement (in nftl.c ??) */ /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
*retlen = 0; ops->retlen = 0;
ret = -EIO; ret = -EIO;
} }
dummy = ReadDOC(docptr, LastDataRead); dummy = ReadDOC(docptr, LastDataRead);
*retlen = len; ops->retlen = len;
return ret; return ret;
} }
......
...@@ -47,10 +47,10 @@ static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -47,10 +47,10 @@ static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf, u_char *eccbuf, size_t *retlen, const u_char *buf, u_char *eccbuf,
struct nand_oobinfo *oobsel); struct nand_oobinfo *oobsel);
static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, u_char *buf); struct mtd_oob_ops *ops);
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, const u_char *buf); struct mtd_oob_ops *ops);
static int doc_erase (struct mtd_info *mtd, struct erase_info *instr); static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
static struct mtd_info *docmilpluslist = NULL; static struct mtd_info *docmilpluslist = NULL;
...@@ -868,14 +868,20 @@ static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -868,14 +868,20 @@ static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
return ret; return ret;
} }
static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, u_char *buf) struct mtd_oob_ops *ops)
{ {
loff_t fofs, base; loff_t fofs, base;
struct DiskOnChip *this = mtd->priv; struct DiskOnChip *this = mtd->priv;
void __iomem * docptr = this->virtadr; void __iomem * docptr = this->virtadr;
struct Nand *mychip = &this->chips[ofs >> this->chipshift]; struct Nand *mychip = &this->chips[ofs >> this->chipshift];
size_t i, size, got, want; size_t i, size, got, want;
uint8_t *buf = ops->oobbuf;
size_t len = ops->len;
BUG_ON(ops->mode != MTD_OOB_PLACE);
ofs += ops->ooboffs;
DoC_CheckASIC(docptr); DoC_CheckASIC(docptr);
...@@ -941,12 +947,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -941,12 +947,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
/* Disable flash internally */ /* Disable flash internally */
WriteDOC(0, docptr, Mplus_FlashSelect); WriteDOC(0, docptr, Mplus_FlashSelect);
*retlen = len; ops->retlen = len;
return 0; return 0;
} }
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
size_t *retlen, const u_char *buf) struct mtd_oob_ops *ops)
{ {
volatile char dummy; volatile char dummy;
loff_t fofs, base; loff_t fofs, base;
...@@ -955,6 +961,12 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -955,6 +961,12 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
struct Nand *mychip = &this->chips[ofs >> this->chipshift]; struct Nand *mychip = &this->chips[ofs >> this->chipshift];
size_t i, size, got, want; size_t i, size, got, want;
int ret = 0; int ret = 0;
uint8_t *buf = ops->oobbuf;
size_t len = ops->len;
BUG_ON(ops->mode != MTD_OOB_PLACE);
ofs += ops->ooboffs;
DoC_CheckASIC(docptr); DoC_CheckASIC(docptr);
...@@ -1030,7 +1042,7 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -1030,7 +1042,7 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
printk("MTD: Error 0x%x programming oob at 0x%x\n", printk("MTD: Error 0x%x programming oob at 0x%x\n",
dummy, (int)ofs); dummy, (int)ofs);
/* FIXME: implement Bad Block Replacement */ /* FIXME: implement Bad Block Replacement */
*retlen = 0; ops->retlen = 0;
ret = -EIO; ret = -EIO;
} }
dummy = ReadDOC(docptr, Mplus_LastDataRead); dummy = ReadDOC(docptr, Mplus_LastDataRead);
...@@ -1043,7 +1055,7 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, ...@@ -1043,7 +1055,7 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
/* Disable flash internally */ /* Disable flash internally */
WriteDOC(0, docptr, Mplus_FlashSelect); WriteDOC(0, docptr, Mplus_FlashSelect);
*retlen = len; ops->retlen = len;
return ret; return ret;
} }
......
...@@ -150,6 +150,69 @@ static void inftl_remove_dev(struct mtd_blktrans_dev *dev) ...@@ -150,6 +150,69 @@ static void inftl_remove_dev(struct mtd_blktrans_dev *dev)
* Actual INFTL access routines. * Actual INFTL access routines.
*/ */
/*
* Read oob data from flash
*/
int inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf)
{
struct mtd_oob_ops ops;
int res;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = offs & (mtd->writesize - 1);
ops.ooblen = len;
ops.oobbuf = buf;
ops.datbuf = NULL;
ops.len = len;
res = mtd->read_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
*retlen = ops.retlen;
return res;
}
/*
* Write oob data to flash
*/
int inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf)
{
struct mtd_oob_ops ops;
int res;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = offs & (mtd->writesize - 1);
ops.ooblen = len;
ops.oobbuf = buf;
ops.datbuf = NULL;
ops.len = len;
res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
*retlen = ops.retlen;
return res;
}
/*
* Write data and oob to flash
*/
static int inftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf, uint8_t *oob)
{
struct mtd_oob_ops ops;
int res;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = offs;
ops.ooblen = mtd->oobsize;
ops.oobbuf = oob;
ops.datbuf = buf;
ops.len = len;
res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
*retlen = ops.retlen;
return res;
}
/* /*
* INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition. * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
* This function is used when the give Virtual Unit Chain. * This function is used when the give Virtual Unit Chain.
...@@ -227,9 +290,9 @@ static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned ...@@ -227,9 +290,9 @@ static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned
if ((BlockMap[block] != 0xffff) || BlockDeleted[block]) if ((BlockMap[block] != 0xffff) || BlockDeleted[block])
continue; continue;
if (mtd->read_oob(mtd, (thisEUN * inftl->EraseSize) if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
+ (block * SECTORSIZE), 16 , &retlen, + (block * SECTORSIZE), 16, &retlen,
(char *)&oob) < 0) (char *)&oob) < 0)
status = SECTOR_IGNORE; status = SECTOR_IGNORE;
else else
status = oob.b.Status | oob.b.Status1; status = oob.b.Status | oob.b.Status1;
...@@ -304,9 +367,9 @@ static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned ...@@ -304,9 +367,9 @@ static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned
memset(&oob, 0xff, sizeof(struct inftl_oob)); memset(&oob, 0xff, sizeof(struct inftl_oob));
oob.b.Status = oob.b.Status1 = SECTOR_USED; oob.b.Status = oob.b.Status1 = SECTOR_USED;
nand_write_raw(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) + inftl_write(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) +
(block * SECTORSIZE), SECTORSIZE, &retlen, (block * SECTORSIZE), SECTORSIZE, &retlen,
movebuf, (char *)&oob); movebuf, (char *)&oob);
} }
/* /*
...@@ -437,8 +500,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block) ...@@ -437,8 +500,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
silly = MAX_LOOPS; silly = MAX_LOOPS;
while (thisEUN <= inftl->lastEUN) { while (thisEUN <= inftl->lastEUN) {
mtd->read_oob(mtd, (thisEUN * inftl->EraseSize) + inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
blockofs, 8, &retlen, (char *)&bci); blockofs, 8, &retlen, (char *)&bci);
status = bci.Status | bci.Status1; status = bci.Status | bci.Status1;
DEBUG(MTD_DEBUG_LEVEL3, "INFTL: status of block %d in " DEBUG(MTD_DEBUG_LEVEL3, "INFTL: status of block %d in "
...@@ -525,8 +588,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block) ...@@ -525,8 +588,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
nacs = 0; nacs = 0;
thisEUN = inftl->VUtable[thisVUC]; thisEUN = inftl->VUtable[thisVUC];
if (thisEUN != BLOCK_NIL) { if (thisEUN != BLOCK_NIL) {
mtd->read_oob(mtd, thisEUN * inftl->EraseSize inftl_read_oob(mtd, thisEUN * inftl->EraseSize
+ 8, 8, &retlen, (char *)&oob.u); + 8, 8, &retlen, (char *)&oob.u);
anac = oob.u.a.ANAC + 1; anac = oob.u.a.ANAC + 1;
nacs = oob.u.a.NACs + 1; nacs = oob.u.a.NACs + 1;
} }
...@@ -547,8 +610,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block) ...@@ -547,8 +610,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
oob.u.a.parityPerField = parity; oob.u.a.parityPerField = parity;
oob.u.a.discarded = 0xaa; oob.u.a.discarded = 0xaa;
mtd->write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8, inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8,
&retlen, (char *)&oob.u); &retlen, (char *)&oob.u);
/* Also back up header... */ /* Also back up header... */
oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC); oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC);
...@@ -558,8 +621,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block) ...@@ -558,8 +621,8 @@ static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
oob.u.b.parityPerField = parity; oob.u.b.parityPerField = parity;
oob.u.b.discarded = 0xaa; oob.u.b.discarded = 0xaa;
mtd->write_oob(mtd, writeEUN * inftl->EraseSize + inftl_write_oob(mtd, writeEUN * inftl->EraseSize +
SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u); SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u);
inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC]; inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
inftl->VUtable[thisVUC] = writeEUN; inftl->VUtable[thisVUC] = writeEUN;
...@@ -610,8 +673,8 @@ static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC) ...@@ -610,8 +673,8 @@ static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC)
if (BlockUsed[block] || BlockDeleted[block]) if (BlockUsed[block] || BlockDeleted[block])
continue; continue;
if (mtd->read_oob(mtd, (thisEUN * inftl->EraseSize) if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
+ (block * SECTORSIZE), 8 , &retlen, + (block * SECTORSIZE), 8 , &retlen,
(char *)&bci) < 0) (char *)&bci) < 0)
status = SECTOR_IGNORE; status = SECTOR_IGNORE;
else else
...@@ -711,8 +774,8 @@ static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block) ...@@ -711,8 +774,8 @@ static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
"block=%d)\n", inftl, block); "block=%d)\n", inftl, block);
while (thisEUN < inftl->nb_blocks) { while (thisEUN < inftl->nb_blocks) {
if (mtd->read_oob(mtd, (thisEUN * inftl->EraseSize) + if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
blockofs, 8, &retlen, (char *)&bci) < 0) blockofs, 8, &retlen, (char *)&bci) < 0)
status = SECTOR_IGNORE; status = SECTOR_IGNORE;
else else
status = bci.Status | bci.Status1; status = bci.Status | bci.Status1;
...@@ -746,10 +809,10 @@ static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block) ...@@ -746,10 +809,10 @@ static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
if (thisEUN != BLOCK_NIL) { if (thisEUN != BLOCK_NIL) {
loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs; loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
if (mtd->read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0) if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
return -EIO; return -EIO;
bci.Status = bci.Status1 = SECTOR_DELETED; bci.Status = bci.Status1 = SECTOR_DELETED;
if (mtd->write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0) if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
return -EIO; return -EIO;
INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE)); INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
} }
...@@ -790,9 +853,9 @@ static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block, ...@@ -790,9 +853,9 @@ static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
memset(&oob, 0xff, sizeof(struct inftl_oob)); memset(&oob, 0xff, sizeof(struct inftl_oob));
oob.b.Status = oob.b.Status1 = SECTOR_USED; oob.b.Status = oob.b.Status1 = SECTOR_USED;
nand_write_raw(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) + inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
blockofs, SECTORSIZE, &retlen, (char *)buffer, blockofs, SECTORSIZE, &retlen, (char *)buffer,
(char *)&oob); (char *)&oob);
/* /*
* need to write SECTOR_USED flags since they are not written * need to write SECTOR_USED flags since they are not written
* in mtd_writeecc * in mtd_writeecc
...@@ -820,7 +883,7 @@ static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block, ...@@ -820,7 +883,7 @@ static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
"buffer=%p)\n", inftl, block, buffer); "buffer=%p)\n", inftl, block, buffer);
while (thisEUN < inftl->nb_blocks) { while (thisEUN < inftl->nb_blocks) {
if (mtd->read_oob(mtd, (thisEUN * inftl->EraseSize) + if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
blockofs, 8, &retlen, (char *)&bci) < 0) blockofs, 8, &retlen, (char *)&bci) < 0)
status = SECTOR_IGNORE; status = SECTOR_IGNORE;
else else
......
...@@ -43,6 +43,11 @@ ...@@ -43,6 +43,11 @@
char inftlmountrev[]="$Revision: 1.18 $"; char inftlmountrev[]="$Revision: 1.18 $";
extern int inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf);
extern int inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf);
/* /*
* find_boot_record: Find the INFTL Media Header and its Spare copy which * find_boot_record: Find the INFTL Media Header and its Spare copy which
* contains the various device information of the INFTL partition and * contains the various device information of the INFTL partition and
...@@ -107,9 +112,9 @@ static int find_boot_record(struct INFTLrecord *inftl) ...@@ -107,9 +112,9 @@ static int find_boot_record(struct INFTLrecord *inftl)
} }
/* To be safer with BIOS, also use erase mark as discriminant */ /* To be safer with BIOS, also use erase mark as discriminant */
if ((ret = mtd->read_oob(mtd, block * inftl->EraseSize + if ((ret = inftl_read_oob(mtd, block * inftl->EraseSize +
SECTORSIZE + 8, 8, &retlen, SECTORSIZE + 8, 8, &retlen,
(char *)&h1) < 0)) { (char *)&h1) < 0)) {
printk(KERN_WARNING "INFTL: ANAND header found at " printk(KERN_WARNING "INFTL: ANAND header found at "
"0x%x in mtd%d, but OOB data read failed " "0x%x in mtd%d, but OOB data read failed "
"(err %d)\n", block * inftl->EraseSize, "(err %d)\n", block * inftl->EraseSize,
...@@ -363,8 +368,8 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address, ...@@ -363,8 +368,8 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
return -1; return -1;
if (check_oob) { if (check_oob) {
if(mtd->read_oob(mtd, address, mtd->oobsize, if(inftl_read_oob(mtd, address, mtd->oobsize,
&retlen, &buf[SECTORSIZE]) < 0) &retlen, &buf[SECTORSIZE]) < 0)
return -1; return -1;
if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0) if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
return -1; return -1;
...@@ -433,7 +438,7 @@ int INFTL_formatblock(struct INFTLrecord *inftl, int block) ...@@ -433,7 +438,7 @@ int INFTL_formatblock(struct INFTLrecord *inftl, int block)
uci.Reserved[2] = 0; uci.Reserved[2] = 0;
uci.Reserved[3] = 0; uci.Reserved[3] = 0;
instr->addr = block * inftl->EraseSize + SECTORSIZE * 2; instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
if (mtd->write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0) if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
goto fail; goto fail;
return 0; return 0;
fail: fail:
...@@ -611,11 +616,11 @@ int INFTL_mount(struct INFTLrecord *s) ...@@ -611,11 +616,11 @@ int INFTL_mount(struct INFTLrecord *s)
break; break;
} }
if (mtd->read_oob(mtd, block * s->EraseSize + 8, if (inftl_read_oob(mtd, block * s->EraseSize + 8,
8, &retlen, (char *)&h0) < 0 || 8, &retlen, (char *)&h0) < 0 ||
mtd->read_oob(mtd, block * s->EraseSize + inftl_read_oob(mtd, block * s->EraseSize +
2 * SECTORSIZE + 8, 8, &retlen, 2 * SECTORSIZE + 8, 8, &retlen,
(char *)&h1) < 0) { (char *)&h1) < 0) {
/* Should never happen? */ /* Should never happen? */
do_format_chain++; do_format_chain++;
break; break;
......
...@@ -408,8 +408,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file, ...@@ -408,8 +408,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
case MEMWRITEOOB: case MEMWRITEOOB:
{ {
struct mtd_oob_buf buf; struct mtd_oob_buf buf;
void *databuf; struct mtd_oob_ops ops;
ssize_t retlen;
if(!(file->f_mode & 2)) if(!(file->f_mode & 2))
return -EPERM; return -EPERM;
...@@ -417,7 +416,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file, ...@@ -417,7 +416,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
return -EFAULT; return -EFAULT;
if (buf.length > 0x4096) if (buf.length > 4096)
return -EINVAL; return -EINVAL;
if (!mtd->write_oob) if (!mtd->write_oob)
...@@ -429,21 +428,32 @@ static int mtd_ioctl(struct inode *inode, struct file *file, ...@@ -429,21 +428,32 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
if (ret) if (ret)
return ret; return ret;
databuf = kmalloc(buf.length, GFP_KERNEL); ops.len = buf.length;
if (!databuf) ops.ooblen = mtd->oobsize;
ops.ooboffs = buf.start & (mtd->oobsize - 1);
ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
return -EINVAL;
ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
if (!ops.oobbuf)
return -ENOMEM; return -ENOMEM;
if (copy_from_user(databuf, buf.ptr, buf.length)) { if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
kfree(databuf); kfree(ops.oobbuf);
return -EFAULT; return -EFAULT;
} }
ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf); buf.start &= ~(mtd->oobsize - 1);
ret = mtd->write_oob(mtd, buf.start, &ops);
if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t))) if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
sizeof(uint32_t)))
ret = -EFAULT; ret = -EFAULT;
kfree(databuf); kfree(ops.oobbuf);
break; break;
} }
...@@ -451,13 +461,12 @@ static int mtd_ioctl(struct inode *inode, struct file *file, ...@@ -451,13 +461,12 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
case MEMREADOOB: case MEMREADOOB:
{ {
struct mtd_oob_buf buf; struct mtd_oob_buf buf;
void *databuf; struct mtd_oob_ops ops;
ssize_t retlen;
if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
return -EFAULT; return -EFAULT;
if (buf.length > 0x4096) if (buf.length > 4096)
return -EINVAL; return -EINVAL;
if (!mtd->read_oob) if (!mtd->read_oob)
...@@ -465,22 +474,32 @@ static int mtd_ioctl(struct inode *inode, struct file *file, ...@@ -465,22 +474,32 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
else else
ret = access_ok(VERIFY_WRITE, buf.ptr, ret = access_ok(VERIFY_WRITE, buf.ptr,
buf.length) ? 0 : -EFAULT; buf.length) ? 0 : -EFAULT;
if (ret) if (ret)
return ret; return ret;
databuf = kmalloc(buf.length, GFP_KERNEL); ops.len = buf.length;
if (!databuf) ops.ooblen = mtd->oobsize;
ops.ooboffs = buf.start & (mtd->oobsize - 1);
ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
return -EINVAL;
ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
if (!ops.oobbuf)
return -ENOMEM; return -ENOMEM;
ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf); buf.start &= ~(mtd->oobsize - 1);
ret = mtd->read_oob(mtd, buf.start, &ops);
if (put_user(retlen, (uint32_t __user *)argp)) if (put_user(ops.retlen, (uint32_t __user *)argp))
ret = -EFAULT; ret = -EFAULT;
else if (retlen && copy_to_user(buf.ptr, databuf, retlen)) else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
ops.retlen))
ret = -EFAULT; ret = -EFAULT;
kfree(databuf); kfree(ops.oobbuf);
break; break;
} }
......
...@@ -231,101 +231,85 @@ concat_writev(struct mtd_info *mtd, const struct kvec *vecs, ...@@ -231,101 +231,85 @@ concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
} }
static int static int
concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len, concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
size_t * retlen, u_char * buf)
{ {
struct mtd_concat *concat = CONCAT(mtd); struct mtd_concat *concat = CONCAT(mtd);
int err = -EINVAL; struct mtd_oob_ops devops = *ops;
int i; int i, err;
*retlen = 0; ops->retlen = 0;
for (i = 0; i < concat->num_subdev; i++) { for (i = 0; i < concat->num_subdev; i++) {
struct mtd_info *subdev = concat->subdev[i]; struct mtd_info *subdev = concat->subdev[i];
size_t size, retsize;
if (from >= subdev->size) { if (from >= subdev->size) {
/* Not destined for this subdev */
size = 0;
from -= subdev->size; from -= subdev->size;
continue; continue;
} }
if (from + len > subdev->size)
/* First part goes into this subdev */
size = subdev->size - from;
else
/* Entire transaction goes into this subdev */
size = len;
if (subdev->read_oob) /* partial read ? */
err = subdev->read_oob(subdev, from, size, if (from + devops.len > subdev->size)
&retsize, buf); devops.len = subdev->size - from;
else
err = -EINVAL;
err = subdev->read_oob(subdev, from, &devops);
ops->retlen += devops.retlen;
if (err) if (err)
break; return err;
*retlen += retsize; devops.len = ops->len - ops->retlen;
len -= size; if (!devops.len)
if (len == 0) return 0;
break;
if (devops.datbuf)
devops.datbuf += devops.retlen;
if (devops.oobbuf)
devops.oobbuf += devops.ooblen;
err = -EINVAL;
buf += size;
from = 0; from = 0;
} }
return err; return -EINVAL;
} }
static int static int
concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len, concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
size_t * retlen, const u_char * buf)
{ {
struct mtd_concat *concat = CONCAT(mtd); struct mtd_concat *concat = CONCAT(mtd);
int err = -EINVAL; struct mtd_oob_ops devops = *ops;
int i; int i, err;
if (!(mtd->flags & MTD_WRITEABLE)) if (!(mtd->flags & MTD_WRITEABLE))
return -EROFS; return -EROFS;
*retlen = 0; ops->retlen = 0;
for (i = 0; i < concat->num_subdev; i++) { for (i = 0; i < concat->num_subdev; i++) {
struct mtd_info *subdev = concat->subdev[i]; struct mtd_info *subdev = concat->subdev[i];
size_t size, retsize;
if (to >= subdev->size) { if (to >= subdev->size) {
size = 0;
to -= subdev->size; to -= subdev->size;
continue; continue;
} }
if (to + len > subdev->size)
size = subdev->size - to;
else
size = len;
if (!(subdev->flags & MTD_WRITEABLE)) /* partial write ? */
err = -EROFS; if (to + devops.len > subdev->size)
else if (subdev->write_oob) devops.len = subdev->size - to;
err = subdev->write_oob(subdev, to, size, &retsize,
buf);
else
err = -EINVAL;
err = subdev->write_oob(subdev, to, &devops);
ops->retlen += devops.retlen;
if (err) if (err)
break; return err;
*retlen += retsize; devops.len = ops->len - ops->retlen;
len -= size; if (!devops.len)
if (len == 0) return 0;
break;
err = -EINVAL; if (devops.datbuf)
buf += size; devops.datbuf += devops.retlen;
if (devops.oobbuf)
devops.oobbuf += devops.ooblen;
to = 0; to = 0;
} }
return err; return -EINVAL;
} }
static void concat_erase_callback(struct erase_info *instr) static void concat_erase_callback(struct erase_info *instr)
......
...@@ -78,16 +78,16 @@ static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_ ...@@ -78,16 +78,16 @@ static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_
part->master->unpoint (part->master, addr, from + part->offset, len); part->master->unpoint (part->master, addr, from + part->offset, len);
} }
static int part_read_oob (struct mtd_info *mtd, loff_t from, size_t len, static int part_read_oob(struct mtd_info *mtd, loff_t from,
size_t *retlen, u_char *buf) struct mtd_oob_ops *ops)
{ {
struct mtd_part *part = PART(mtd); struct mtd_part *part = PART(mtd);
if (from >= mtd->size) if (from >= mtd->size)
len = 0; return -EINVAL;
else if (from + len > mtd->size) if (from + ops->len > mtd->size)
len = mtd->size - from; return -EINVAL;
return part->master->read_oob (part->master, from + part->offset, return part->master->read_oob(part->master, from + part->offset, ops);
len, retlen, buf);
} }
static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
...@@ -134,18 +134,19 @@ static int part_write (struct mtd_info *mtd, loff_t to, size_t len, ...@@ -134,18 +134,19 @@ static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
len, retlen, buf); len, retlen, buf);
} }
static int part_write_oob (struct mtd_info *mtd, loff_t to, size_t len, static int part_write_oob(struct mtd_info *mtd, loff_t to,
size_t *retlen, const u_char *buf) struct mtd_oob_ops *ops)
{ {
struct mtd_part *part = PART(mtd); struct mtd_part *part = PART(mtd);
if (!(mtd->flags & MTD_WRITEABLE)) if (!(mtd->flags & MTD_WRITEABLE))
return -EROFS; return -EROFS;
if (to >= mtd->size) if (to >= mtd->size)
len = 0; return -EINVAL;
else if (to + len > mtd->size) if (to + ops->len > mtd->size)
len = mtd->size - to; return -EINVAL;
return part->master->write_oob (part->master, to + part->offset, return part->master->write_oob(part->master, to + part->offset, ops);
len, retlen, buf);
} }
static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
......
...@@ -81,23 +81,12 @@ static struct nand_ecclayout nand_oob_64 = { ...@@ -81,23 +81,12 @@ static struct nand_ecclayout nand_oob_64 = {
.length = 38}} .length = 38}}
}; };
/* This is used for padding purposes in nand_write_oob */
static uint8_t ffchars[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const uint8_t *buf);
static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
int new_state); int new_state);
static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
struct mtd_oob_ops *ops);
/* /*
* For devices which display every fart in the system on a seperate LED. Is * For devices which display every fart in the system on a seperate LED. Is
* compiled away when LED support is disabled. * compiled away when LED support is disabled.
...@@ -358,7 +347,6 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) ...@@ -358,7 +347,6 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
{ {
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
uint8_t buf[2] = { 0, 0 }; uint8_t buf[2] = { 0, 0 };
size_t retlen;
int block; int block;
/* Get block number */ /* Get block number */
...@@ -371,8 +359,13 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) ...@@ -371,8 +359,13 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
return nand_update_bbt(mtd, ofs); return nand_update_bbt(mtd, ofs);
/* We write two bytes, so we dont have to mess with 16 bit access */ /* We write two bytes, so we dont have to mess with 16 bit access */
ofs += mtd->oobsize + (chip->badblockpos & ~0x01); ofs += mtd->oobsize;
return nand_write_oob(mtd, ofs, 2, &retlen, buf); chip->ops.len = 2;
chip->ops.datbuf = NULL;
chip->ops.oobbuf = buf;
chip->ops.ooboffs = chip->badblockpos & ~0x01;
return nand_do_write_oob(mtd, ofs, &chip->ops);
} }
/** /**
...@@ -739,6 +732,20 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state) ...@@ -739,6 +732,20 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
return status; return status;
} }
/**
* nand_read_page_raw - [Intern] read raw page data without ecc
* @mtd: mtd info structure
* @chip: nand chip info structure
* @buf: buffer to store read data
*/
static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
uint8_t *buf)
{
chip->read_buf(mtd, buf, mtd->writesize);
chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
return 0;
}
/** /**
* nand_read_page_swecc - {REPLACABLE] software ecc based page read function * nand_read_page_swecc - {REPLACABLE] software ecc based page read function
* @mtd: mtd info structure * @mtd: mtd info structure
...@@ -756,11 +763,7 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, ...@@ -756,11 +763,7 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
uint8_t *ecc_code = chip->buffers.ecccode; uint8_t *ecc_code = chip->buffers.ecccode;
int *eccpos = chip->ecc.layout->eccpos; int *eccpos = chip->ecc.layout->eccpos;
chip->read_buf(mtd, buf, mtd->writesize); nand_read_page_raw(mtd, chip, buf);
chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
if (chip->ecc.mode == NAND_ECC_NONE)
return 0;
for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
chip->ecc.calculate(mtd, p, &ecc_calc[i]); chip->ecc.calculate(mtd, p, &ecc_calc[i]);
...@@ -882,18 +885,50 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, ...@@ -882,18 +885,50 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
} }
/** /**
* nand_do_read - [Internal] Read data with ECC * nand_transfer_oob - [Internal] Transfer oob to client buffer
* @chip: nand chip structure
* @ops: oob ops structure
*/
static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
struct mtd_oob_ops *ops)
{
size_t len = ops->ooblen;
switch(ops->mode) {
case MTD_OOB_PLACE:
case MTD_OOB_RAW:
memcpy(oob, chip->oob_poi + ops->ooboffs, len);
return oob + len;
case MTD_OOB_AUTO: {
struct nand_oobfree *free = chip->ecc.layout->oobfree;
size_t bytes;
for(; free->length && len; free++, len -= bytes) {
bytes = min(len, free->length);
memcpy(oob, chip->oob_poi + free->offset, bytes);
oob += bytes;
}
return oob;
}
default:
BUG();
}
return NULL;
}
/**
* nand_do_read_ops - [Internal] Read data with ECC
* *
* @mtd: MTD device structure * @mtd: MTD device structure
* @from: offset to read from * @from: offset to read from
* @len: number of bytes to read
* @retlen: pointer to variable to store the number of read bytes
* @buf: the databuffer to put data
* *
* Internal function. Called with chip held. * Internal function. Called with chip held.
*/ */
int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
size_t *retlen, uint8_t *buf) struct mtd_oob_ops *ops)
{ {
int chipnr, page, realpage, col, bytes, aligned; int chipnr, page, realpage, col, bytes, aligned;
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
...@@ -901,8 +936,8 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -901,8 +936,8 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
int sndcmd = 1; int sndcmd = 1;
int ret = 0; int ret = 0;
uint32_t readlen = len; uint32_t readlen = ops->len;
uint8_t *bufpoi; uint8_t *bufpoi, *oob, *buf;
stats = mtd->ecc_stats; stats = mtd->ecc_stats;
...@@ -915,12 +950,15 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -915,12 +950,15 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
col = (int)(from & (mtd->writesize - 1)); col = (int)(from & (mtd->writesize - 1));
chip->oob_poi = chip->buffers.oobrbuf; chip->oob_poi = chip->buffers.oobrbuf;
buf = ops->datbuf;
oob = ops->oobbuf;
while(1) { while(1) {
bytes = min(mtd->writesize - col, readlen); bytes = min(mtd->writesize - col, readlen);
aligned = (bytes == mtd->writesize); aligned = (bytes == mtd->writesize);
/* Is the current page in the buffer ? */ /* Is the current page in the buffer ? */
if (realpage != chip->pagebuf) { if (realpage != chip->pagebuf || oob) {
bufpoi = aligned ? buf : chip->buffers.databuf; bufpoi = aligned ? buf : chip->buffers.databuf;
if (likely(sndcmd)) { if (likely(sndcmd)) {
...@@ -939,6 +977,16 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -939,6 +977,16 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
memcpy(buf, chip->buffers.databuf + col, bytes); memcpy(buf, chip->buffers.databuf + col, bytes);
} }
buf += bytes;
if (unlikely(oob)) {
/* Raw mode does data:oob:data:oob */
if (ops->mode != MTD_OOB_RAW)
oob = nand_transfer_oob(chip, oob, ops);
else
buf = nand_transfer_oob(chip, buf, ops);
}
if (!(chip->options & NAND_NO_READRDY)) { if (!(chip->options & NAND_NO_READRDY)) {
/* /*
* Apply delay or wait for ready/busy pin. Do * Apply delay or wait for ready/busy pin. Do
...@@ -952,10 +1000,11 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -952,10 +1000,11 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
else else
nand_wait_ready(mtd); nand_wait_ready(mtd);
} }
} else } else {
memcpy(buf, chip->buffers.databuf + col, bytes); memcpy(buf, chip->buffers.databuf + col, bytes);
buf += bytes;
}
buf += bytes;
readlen -= bytes; readlen -= bytes;
if (!readlen) if (!readlen)
...@@ -981,7 +1030,7 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -981,7 +1030,7 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
sndcmd = 1; sndcmd = 1;
} }
*retlen = len - (size_t) readlen; ops->retlen = ops->len - (size_t) readlen;
if (ret) if (ret)
return ret; return ret;
...@@ -1002,57 +1051,49 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -1002,57 +1051,49 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, uint8_t *buf) size_t *retlen, uint8_t *buf)
{ {
struct nand_chip *chip = mtd->priv;
int ret; int ret;
*retlen = 0;
/* Do not allow reads past end of device */ /* Do not allow reads past end of device */
if ((from + len) > mtd->size) if ((from + len) > mtd->size)
return -EINVAL; return -EINVAL;
if (!len) if (!len)
return 0; return 0;
nand_get_device(mtd->priv, mtd, FL_READING); nand_get_device(chip, mtd, FL_READING);
ret = nand_do_read(mtd, from, len, retlen, buf); chip->ops.len = len;
chip->ops.datbuf = buf;
chip->ops.oobbuf = NULL;
ret = nand_do_read_ops(mtd, from, &chip->ops);
nand_release_device(mtd); nand_release_device(mtd);
*retlen = chip->ops.retlen;
return ret; return ret;
} }
/** /**
* nand_read_oob - [MTD Interface] NAND read out-of-band * nand_do_read_oob - [Intern] NAND read out-of-band
* @mtd: MTD device structure * @mtd: MTD device structure
* @from: offset to read from * @from: offset to read from
* @len: number of bytes to read * @ops: oob operations description structure
* @retlen: pointer to variable to store the number of read bytes
* @buf: the databuffer to put data
* *
* NAND read out-of-band data from the spare area * NAND read out-of-band data from the spare area
*/ */
static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
size_t *retlen, uint8_t *buf) struct mtd_oob_ops *ops)
{ {
int col, page, realpage, chipnr, sndcmd = 1; int col, page, realpage, chipnr, sndcmd = 1;
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
int readlen = len; int direct, bytes, readlen = ops->len;
uint8_t *bufpoi, *buf = ops->oobbuf;
DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
(unsigned int)from, (int)len); (unsigned int)from, (int)len);
/* Initialize return length value */
*retlen = 0;
/* Do not allow reads past end of device */
if ((from + len) > mtd->size) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
"Attempt read beyond end of device\n");
return -EINVAL;
}
nand_get_device(chip, mtd, FL_READING);
chipnr = (int)(from >> chip->chip_shift); chipnr = (int)(from >> chip->chip_shift);
chip->select_chip(mtd, chipnr); chip->select_chip(mtd, chipnr);
...@@ -1060,20 +1101,31 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -1060,20 +1101,31 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
realpage = (int)(from >> chip->page_shift); realpage = (int)(from >> chip->page_shift);
page = realpage & chip->pagemask; page = realpage & chip->pagemask;
/* Mask to get column */ if (ops->mode != MTD_OOB_AUTO) {
col = from & (mtd->oobsize - 1); col = ops->ooboffs;
direct = 1;
} else {
col = 0;
direct = 0;
}
while(1) { while(1) {
int bytes = min((int)(mtd->oobsize - col), readlen); bytes = direct ? ops->ooblen : mtd->oobsize;
bufpoi = direct ? buf : chip->buffers.oobrbuf;
if (likely(sndcmd)) { if (likely(sndcmd)) {
chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page); chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
sndcmd = 0; sndcmd = 0;
} }
chip->read_buf(mtd, buf, bytes); chip->read_buf(mtd, bufpoi, bytes);
readlen -= bytes; if (unlikely(!direct))
buf = nand_transfer_oob(chip, buf, ops);
else
buf += ops->ooblen;
readlen -= ops->ooblen;
if (!readlen) if (!readlen)
break; break;
...@@ -1090,10 +1142,6 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -1090,10 +1142,6 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
nand_wait_ready(mtd); nand_wait_ready(mtd);
} }
buf += bytes;
bytes = mtd->oobsize;
col = 0;
/* Increment page address */ /* Increment page address */
realpage++; realpage++;
...@@ -1112,81 +1160,76 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -1112,81 +1160,76 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
sndcmd = 1; sndcmd = 1;
} }
/* Deselect and wake up anyone waiting on the device */ ops->retlen = ops->len;
nand_release_device(mtd);
*retlen = len;
return 0; return 0;
} }
/** /**
* nand_read_raw - [GENERIC] Read raw data including oob into buffer * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
* @mtd: MTD device structure * @mtd: MTD device structure
* @buf: temporary buffer
* @from: offset to read from * @from: offset to read from
* @len: number of bytes to read * @ops: oob operation description structure
* @ooblen: number of oob data bytes to read
* *
* Read raw data including oob into buffer * NAND read data and/or out-of-band data
*/ */
int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, static int nand_read_oob(struct mtd_info *mtd, loff_t from,
size_t ooblen) struct mtd_oob_ops *ops)
{ {
int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
uint8_t *buf) = NULL;
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
int page = (int)(from >> chip->page_shift); int ret = -ENOTSUPP;
int chipnr = (int)(from >> chip->chip_shift);
int sndcmd = 1; ops->retlen = 0;
int cnt = 0;
int pagesize = mtd->writesize + mtd->oobsize;
int blockcheck;
/* Do not allow reads past end of device */ /* Do not allow reads past end of device */
if ((from + len) > mtd->size) { if ((from + ops->len) > mtd->size) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: " DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
"Attempt read beyond end of device\n"); "Attempt read beyond end of device\n");
return -EINVAL; return -EINVAL;
} }
/* Grab the lock and see if the device is available */
nand_get_device(chip, mtd, FL_READING); nand_get_device(chip, mtd, FL_READING);
chip->select_chip(mtd, chipnr); switch(ops->mode) {
case MTD_OOB_PLACE:
/* Add requested oob length */ case MTD_OOB_AUTO:
len += ooblen; break;
blockcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
while (len) { case MTD_OOB_RAW:
if (likely(sndcmd)) { /* Replace the read_page algorithm temporary */
chip->cmdfunc(mtd, NAND_CMD_READ0, 0, read_page = chip->ecc.read_page;
page & chip->pagemask); chip->ecc.read_page = nand_read_page_raw;
sndcmd = 0; break;
}
chip->read_buf(mtd, &buf[cnt], pagesize); default:
goto out;
}
len -= pagesize; if (!ops->datbuf)
cnt += pagesize; ret = nand_do_read_oob(mtd, from, ops);
page++; else
ret = nand_do_read_ops(mtd, from, ops);
if (!(chip->options & NAND_NO_READRDY)) { if (unlikely(ops->mode == MTD_OOB_RAW))
if (!chip->dev_ready) chip->ecc.read_page = read_page;
udelay(chip->chip_delay); out:
else nand_release_device(mtd);
nand_wait_ready(mtd); return ret;
} }
/*
* Check, if the chip supports auto page increment or if we
* cross a block boundary.
*/
if (!NAND_CANAUTOINCR(chip) || !(page & blockcheck))
sndcmd = 1;
}
/* Deselect and wake up anyone waiting on the device */ /**
nand_release_device(mtd); * nand_write_page_raw - [Intern] raw page write function
return 0; * @mtd: mtd info structure
* @chip: nand chip info structure
* @buf: data buffer
*/
static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
const uint8_t *buf)
{
chip->write_buf(mtd, buf, mtd->writesize);
chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
} }
/** /**
...@@ -1205,17 +1248,14 @@ static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, ...@@ -1205,17 +1248,14 @@ static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
const uint8_t *p = buf; const uint8_t *p = buf;
int *eccpos = chip->ecc.layout->eccpos; int *eccpos = chip->ecc.layout->eccpos;
if (chip->ecc.mode != NAND_ECC_NONE) { /* Software ecc calculation */
/* Software ecc calculation */ for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) chip->ecc.calculate(mtd, p, &ecc_calc[i]);
chip->ecc.calculate(mtd, p, &ecc_calc[i]);
for (i = 0; i < chip->ecc.total; i++) for (i = 0; i < chip->ecc.total; i++)
chip->oob_poi[eccpos[i]] = ecc_calc[i]; chip->oob_poi[eccpos[i]] = ecc_calc[i];
}
chip->write_buf(mtd, buf, mtd->writesize); nand_write_page_raw(mtd, chip, buf);
chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
} }
/** /**
...@@ -1342,51 +1382,77 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, ...@@ -1342,51 +1382,77 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
return 0; return 0;
} }
/**
* nand_fill_oob - [Internal] Transfer client buffer to oob
* @chip: nand chip structure
* @oob: oob data buffer
* @ops: oob ops structure
*/
static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
struct mtd_oob_ops *ops)
{
size_t len = ops->ooblen;
switch(ops->mode) {
case MTD_OOB_PLACE:
case MTD_OOB_RAW:
memcpy(chip->oob_poi + ops->ooboffs, oob, len);
return oob + len;
case MTD_OOB_AUTO: {
struct nand_oobfree *free = chip->ecc.layout->oobfree;
size_t bytes;
for(; free->length && len; free++, len -= bytes) {
bytes = min(len, free->length);
memcpy(chip->oob_poi + free->offset, oob, bytes);
oob += bytes;
}
return oob;
}
default:
BUG();
}
return NULL;
}
#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0 #define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
/** /**
* nand_write - [MTD Interface] NAND write with ECC * nand_do_write_ops - [Internal] NAND write with ECC
* @mtd: MTD device structure * @mtd: MTD device structure
* @to: offset to write to * @to: offset to write to
* @len: number of bytes to write * @ops: oob operations description structure
* @retlen: pointer to variable to store the number of written bytes
* @buf: the data to write
* *
* NAND write with ECC * NAND write with ECC
*/ */
static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
size_t *retlen, const uint8_t *buf) struct mtd_oob_ops *ops)
{ {
int chipnr, realpage, page, blockmask; int chipnr, realpage, page, blockmask;
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
uint32_t writelen = len; uint32_t writelen = ops->len;
uint8_t *oob = ops->oobbuf;
uint8_t *buf = ops->datbuf;
int bytes = mtd->writesize; int bytes = mtd->writesize;
int ret = -EIO; int ret;
*retlen = 0; ops->retlen = 0;
/* Do not allow write past end of device */
if ((to + len) > mtd->size) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_write: "
"Attempt to write past end of page\n");
return -EINVAL;
}
/* reject writes, which are not page aligned */ /* reject writes, which are not page aligned */
if (NOTALIGNED(to) || NOTALIGNED(len)) { if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
printk(KERN_NOTICE "nand_write: " printk(KERN_NOTICE "nand_write: "
"Attempt to write not page aligned data\n"); "Attempt to write not page aligned data\n");
return -EINVAL; return -EINVAL;
} }
if (!len) if (!writelen)
return 0; return 0;
nand_get_device(chip, mtd, FL_WRITING);
/* Check, if it is write protected */ /* Check, if it is write protected */
if (nand_check_wp(mtd)) if (nand_check_wp(mtd))
goto out; return -EIO;
chipnr = (int)(to >> chip->chip_shift); chipnr = (int)(to >> chip->chip_shift);
chip->select_chip(mtd, chipnr); chip->select_chip(mtd, chipnr);
...@@ -1397,7 +1463,7 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1397,7 +1463,7 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
/* Invalidate the page cache, when we write to the cached page */ /* Invalidate the page cache, when we write to the cached page */
if (to <= (chip->pagebuf << chip->page_shift) && if (to <= (chip->pagebuf << chip->page_shift) &&
(chip->pagebuf << chip->page_shift) < (to + len)) (chip->pagebuf << chip->page_shift) < (to + ops->len))
chip->pagebuf = -1; chip->pagebuf = -1;
chip->oob_poi = chip->buffers.oobwbuf; chip->oob_poi = chip->buffers.oobwbuf;
...@@ -1405,6 +1471,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1405,6 +1471,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
while(1) { while(1) {
int cached = writelen > bytes && page != blockmask; int cached = writelen > bytes && page != blockmask;
if (unlikely(oob))
oob = nand_fill_oob(chip, oob, ops);
ret = nand_write_page(mtd, chip, buf, page, cached); ret = nand_write_page(mtd, chip, buf, page, cached);
if (ret) if (ret)
break; break;
...@@ -1424,94 +1493,74 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1424,94 +1493,74 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
chip->select_chip(mtd, chipnr); chip->select_chip(mtd, chipnr);
} }
} }
out:
*retlen = len - writelen; if (unlikely(oob))
nand_release_device(mtd); memset(chip->oob_poi, 0xff, mtd->oobsize);
ops->retlen = ops->len - writelen;
return ret; return ret;
} }
/** /**
* nand_write_raw - [GENERIC] Write raw data including oob * nand_write - [MTD Interface] NAND write with ECC
* @mtd: MTD device structure * @mtd: MTD device structure
* @buf: source buffer
* @to: offset to write to * @to: offset to write to
* @len: number of bytes to write * @len: number of bytes to write
* @buf: source buffer * @retlen: pointer to variable to store the number of written bytes
* @oob: oob buffer * @buf: the data to write
* *
* Write raw data including oob * NAND write with ECC
*/ */
int nand_write_raw(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
const uint8_t *buf, uint8_t *oob) size_t *retlen, const uint8_t *buf)
{ {
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
int page = (int)(to >> chip->page_shift);
int chipnr = (int)(to >> chip->chip_shift);
int ret; int ret;
*retlen = 0; /* Do not allow reads past end of device */
if ((to + len) > mtd->size)
/* Do not allow writes past end of device */
if ((to + len) > mtd->size) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt write "
"beyond end of device\n");
return -EINVAL; return -EINVAL;
} if (!len)
return 0;
/* Grab the lock and see if the device is available */ nand_get_device(chip, mtd, FL_READING);
nand_get_device(chip, mtd, FL_WRITING);
chip->select_chip(mtd, chipnr); chip->ops.len = len;
chip->oob_poi = oob; chip->ops.datbuf = (uint8_t *)buf;
chip->ops.oobbuf = NULL;
while (len != *retlen) { ret = nand_do_write_ops(mtd, to, &chip->ops);
ret = nand_write_page(mtd, chip, buf, page, 0);
if (ret)
return ret;
page++;
*retlen += mtd->writesize;
buf += mtd->writesize;
chip->oob_poi += mtd->oobsize;
}
/* Deselect and wake up anyone waiting on the device */
nand_release_device(mtd); nand_release_device(mtd);
return 0;
*retlen = chip->ops.retlen;
return ret;
} }
EXPORT_SYMBOL_GPL(nand_write_raw);
/** /**
* nand_write_oob - [MTD Interface] NAND write out-of-band * nand_do_write_oob - [MTD Interface] NAND write out-of-band
* @mtd: MTD device structure * @mtd: MTD device structure
* @to: offset to write to * @to: offset to write to
* @len: number of bytes to write * @ops: oob operation description structure
* @retlen: pointer to variable to store the number of written bytes
* @buf: the data to write
* *
* NAND write out-of-band * NAND write out-of-band
*/ */
static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
size_t *retlen, const uint8_t *buf) struct mtd_oob_ops *ops)
{ {
int column, page, status, ret = -EIO, chipnr; int chipnr, page, status;
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
(unsigned int)to, (int)len); (unsigned int)to, (int)ops->len);
/* Initialize return length value */
*retlen = 0;
/* Do not allow write past end of page */ /* Do not allow write past end of page */
column = to & (mtd->oobsize - 1); if ((ops->ooboffs + ops->len) > mtd->oobsize) {
if ((column + len) > mtd->oobsize) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
"Attempt to write past end of page\n"); "Attempt to write past end of page\n");
return -EINVAL; return -EINVAL;
} }
nand_get_device(chip, mtd, FL_WRITING);
chipnr = (int)(to >> chip->chip_shift); chipnr = (int)(to >> chip->chip_shift);
chip->select_chip(mtd, chipnr); chip->select_chip(mtd, chipnr);
...@@ -1528,26 +1577,27 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1528,26 +1577,27 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
/* Check, if it is write protected */ /* Check, if it is write protected */
if (nand_check_wp(mtd)) if (nand_check_wp(mtd))
goto out; return -EROFS;
/* Invalidate the page cache, if we write to the cached page */ /* Invalidate the page cache, if we write to the cached page */
if (page == chip->pagebuf) if (page == chip->pagebuf)
chip->pagebuf = -1; chip->pagebuf = -1;
if (NAND_MUST_PAD(chip)) { if (ops->mode == MTD_OOB_AUTO || NAND_MUST_PAD(chip)) {
chip->oob_poi = chip->buffers.oobwbuf;
memset(chip->oob_poi, 0xff, mtd->oobsize);
nand_fill_oob(chip, ops->oobbuf, ops);
chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize,
page & chip->pagemask); page & chip->pagemask);
/* prepad 0xff for partial programming */ chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
chip->write_buf(mtd, ffchars, column); memset(chip->oob_poi, 0xff, mtd->oobsize);
/* write data */
chip->write_buf(mtd, buf, len);
/* postpad 0xff for partial programming */
chip->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
} else { } else {
chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column, chip->cmdfunc(mtd, NAND_CMD_SEQIN,
mtd->writesize + ops->ooboffs,
page & chip->pagemask); page & chip->pagemask);
chip->write_buf(mtd, buf, len); chip->write_buf(mtd, ops->oobbuf, ops->len);
} }
/* Send command to program the OOB data */ /* Send command to program the OOB data */
chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
...@@ -1557,27 +1607,75 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1557,27 +1607,75 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
if (status & NAND_STATUS_FAIL) { if (status & NAND_STATUS_FAIL) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
"Failed write, page 0x%08x\n", page); "Failed write, page 0x%08x\n", page);
ret = -EIO; return -EIO;
goto out;
} }
*retlen = len; ops->retlen = ops->len;
#ifdef CONFIG_MTD_NAND_VERIFY_WRITE #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
/* Send command to read back the data */ if (ops->mode != MTD_OOB_AUTO) {
chip->cmdfunc(mtd, NAND_CMD_READOOB, column, page & chip->pagemask); /* Send command to read back the data */
chip->cmdfunc(mtd, NAND_CMD_READOOB, ops->ooboffs,
page & chip->pagemask);
if (chip->verify_buf(mtd, buf, len)) { if (chip->verify_buf(mtd, ops->oobbuf, ops->len)) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
"Failed write verify, page 0x%08x\n", page); "Failed write verify, page 0x%08x\n", page);
ret = -EIO; return -EIO;
goto out; }
} }
#endif #endif
ret = 0; return 0;
}
/**
* nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
* @mtd: MTD device structure
* @from: offset to read from
* @ops: oob operation description structure
*/
static int nand_write_oob(struct mtd_info *mtd, loff_t to,
struct mtd_oob_ops *ops)
{
void (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
const uint8_t *buf) = NULL;
struct nand_chip *chip = mtd->priv;
int ret = -ENOTSUPP;
ops->retlen = 0;
/* Do not allow writes past end of device */
if ((to + ops->len) > mtd->size) {
DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
"Attempt read beyond end of device\n");
return -EINVAL;
}
nand_get_device(chip, mtd, FL_READING);
switch(ops->mode) {
case MTD_OOB_PLACE:
case MTD_OOB_AUTO:
break;
case MTD_OOB_RAW:
/* Replace the write_page algorithm temporary */
write_page = chip->ecc.write_page;
chip->ecc.write_page = nand_write_page_raw;
break;
default:
goto out;
}
if (!ops->datbuf)
ret = nand_do_write_oob(mtd, to, ops);
else
ret = nand_do_write_ops(mtd, to, ops);
if (unlikely(ops->mode == MTD_OOB_RAW))
chip->ecc.write_page = write_page;
out: out:
/* Deselect and wake up anyone waiting on the device */
nand_release_device(mtd); nand_release_device(mtd);
return ret; return ret;
} }
...@@ -2191,8 +2289,8 @@ int nand_scan(struct mtd_info *mtd, int maxchips) ...@@ -2191,8 +2289,8 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
case NAND_ECC_NONE: case NAND_ECC_NONE:
printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. " printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
"This is not recommended !!\n"); "This is not recommended !!\n");
chip->ecc.read_page = nand_read_page_swecc; chip->ecc.read_page = nand_read_page_raw;
chip->ecc.write_page = nand_write_page_swecc; chip->ecc.write_page = nand_write_page_raw;
chip->ecc.size = mtd->writesize; chip->ecc.size = mtd->writesize;
chip->ecc.bytes = 0; chip->ecc.bytes = 0;
break; break;
......
...@@ -230,6 +230,42 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc ...@@ -230,6 +230,42 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
return 0; return 0;
} }
/*
* Scan read raw data from flash
*/
static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
size_t len)
{
struct mtd_oob_ops ops;
ops.mode = MTD_OOB_RAW;
ops.ooboffs = 0;
ops.ooblen = mtd->oobsize;
ops.oobbuf = buf;
ops.datbuf = buf;
ops.len = len;
return mtd->read_oob(mtd, offs, &ops);
}
/*
* Scan write data with oob to flash
*/
static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
uint8_t *buf, uint8_t *oob)
{
struct mtd_oob_ops ops;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = 0;
ops.ooblen = mtd->oobsize;
ops.datbuf = buf;
ops.oobbuf = oob;
ops.len = len;
return mtd->write_oob(mtd, offs, &ops);
}
/** /**
* read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
* @mtd: MTD device structure * @mtd: MTD device structure
...@@ -241,27 +277,85 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc ...@@ -241,27 +277,85 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
* We assume that the bbt bits are in consecutive order. * We assume that the bbt bits are in consecutive order.
* *
*/ */
static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md) static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
struct nand_bbt_descr *td, struct nand_bbt_descr *md)
{ {
struct nand_chip *this = mtd->priv; struct nand_chip *this = mtd->priv;
/* Read the primary version, if available */ /* Read the primary version, if available */
if (td->options & NAND_BBT_VERSION) { if (td->options & NAND_BBT_VERSION) {
nand_read_raw(mtd, buf, td->pages[0] << this->page_shift, mtd->writesize, mtd->oobsize); scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
mtd->writesize);
td->version[0] = buf[mtd->writesize + td->veroffs]; td->version[0] = buf[mtd->writesize + td->veroffs];
printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]); printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
td->pages[0], td->version[0]);
} }
/* Read the mirror version, if available */ /* Read the mirror version, if available */
if (md && (md->options & NAND_BBT_VERSION)) { if (md && (md->options & NAND_BBT_VERSION)) {
nand_read_raw(mtd, buf, md->pages[0] << this->page_shift, mtd->writesize, mtd->oobsize); scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
mtd->writesize);
md->version[0] = buf[mtd->writesize + md->veroffs]; md->version[0] = buf[mtd->writesize + md->veroffs];
printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]); printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
md->pages[0], md->version[0]);
} }
return 1; return 1;
} }
/*
* Scan a given block full
*/
static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
loff_t offs, uint8_t *buf, size_t readlen,
int scanlen, int len)
{
int ret, j;
ret = scan_read_raw(mtd, buf, offs, readlen);
if (ret)
return ret;
for (j = 0; j < len; j++, buf += scanlen) {
if (check_pattern(buf, scanlen, mtd->writesize, bd))
return 1;
}
return 0;
}
/*
* Scan a given block partially
*/
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
loff_t offs, uint8_t *buf, int len)
{
struct mtd_oob_ops ops;
int j, ret;
ops.len = mtd->oobsize;
ops.ooblen = mtd->oobsize;
ops.oobbuf = buf;
ops.ooboffs = 0;
ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
for (j = 0; j < len; j++) {
/*
* Read the full oob until read_oob is fixed to
* handle single byte reads for 16 bit
* buswidth
*/
ret = mtd->read_oob(mtd, offs, &ops);
if (ret)
return ret;
if (check_short_pattern(buf, bd))
return 1;
offs += mtd->writesize;
}
return 0;
}
/** /**
* create_bbt - [GENERIC] Create a bad block table by scanning the device * create_bbt - [GENERIC] Create a bad block table by scanning the device
* @mtd: MTD device structure * @mtd: MTD device structure
...@@ -273,13 +367,14 @@ static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des ...@@ -273,13 +367,14 @@ static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
* Create a bad block table by scanning the device * Create a bad block table by scanning the device
* for the given good/bad block identify pattern * for the given good/bad block identify pattern
*/ */
static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip) static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
struct nand_bbt_descr *bd, int chip)
{ {
struct nand_chip *this = mtd->priv; struct nand_chip *this = mtd->priv;
int i, j, numblocks, len, scanlen; int i, numblocks, len, scanlen;
int startblock; int startblock;
loff_t from; loff_t from;
size_t readlen, ooblen; size_t readlen;
printk(KERN_INFO "Scanning device for bad blocks\n"); printk(KERN_INFO "Scanning device for bad blocks\n");
...@@ -294,18 +389,17 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr ...@@ -294,18 +389,17 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
if (!(bd->options & NAND_BBT_SCANEMPTY)) { if (!(bd->options & NAND_BBT_SCANEMPTY)) {
/* We need only read few bytes from the OOB area */ /* We need only read few bytes from the OOB area */
scanlen = ooblen = 0; scanlen = 0;
readlen = bd->len; readlen = bd->len;
} else { } else {
/* Full page content should be read */ /* Full page content should be read */
scanlen = mtd->writesize + mtd->oobsize; scanlen = mtd->writesize + mtd->oobsize;
readlen = len * mtd->writesize; readlen = len * mtd->writesize;
ooblen = len * mtd->oobsize;
} }
if (chip == -1) { if (chip == -1) {
/* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it /* Note that numblocks is 2 * (real numblocks) here, see i+=2
* makes shifting and masking less painful */ * below as it makes shifting and masking less painful */
numblocks = mtd->size >> (this->bbt_erase_shift - 1); numblocks = mtd->size >> (this->bbt_erase_shift - 1);
startblock = 0; startblock = 0;
from = 0; from = 0;
...@@ -324,35 +418,21 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr ...@@ -324,35 +418,21 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
for (i = startblock; i < numblocks;) { for (i = startblock; i < numblocks;) {
int ret; int ret;
if (bd->options & NAND_BBT_SCANEMPTY) if (bd->options & NAND_BBT_SCANALLPAGES)
if ((ret = nand_read_raw(mtd, buf, from, readlen, ooblen))) ret = scan_block_full(mtd, bd, from, buf, readlen,
return ret; scanlen, len);
else
for (j = 0; j < len; j++) { ret = scan_block_fast(mtd, bd, from, buf, len);
if (!(bd->options & NAND_BBT_SCANEMPTY)) {
size_t retlen; if (ret < 0)
return ret;
/* Read the full oob until read_oob is fixed to
* handle single byte reads for 16 bit buswidth */ if (ret) {
ret = mtd->read_oob(mtd, from + j * mtd->writesize, mtd->oobsize, &retlen, buf); this->bbt[i >> 3] |= 0x03 << (i & 0x6);
if (ret) printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
return ret; i >> 1, (unsigned int)from);
if (check_short_pattern(buf, bd)) {
this->bbt[i >> 3] |= 0x03 << (i & 0x6);
printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
i >> 1, (unsigned int)from);
break;
}
} else {
if (check_pattern(&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
this->bbt[i >> 3] |= 0x03 << (i & 0x6);
printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
i >> 1, (unsigned int)from);
break;
}
}
} }
i += 2; i += 2;
from += (1 << this->bbt_erase_shift); from += (1 << this->bbt_erase_shift);
} }
...@@ -383,6 +463,7 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr ...@@ -383,6 +463,7 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
int bits, startblock, block, dir; int bits, startblock, block, dir;
int scanlen = mtd->writesize + mtd->oobsize; int scanlen = mtd->writesize + mtd->oobsize;
int bbtblocks; int bbtblocks;
int blocktopage = this->bbt_erase_shift - this->page_shift;
/* Search direction top -> down ? */ /* Search direction top -> down ? */
if (td->options & NAND_BBT_LASTBLOCK) { if (td->options & NAND_BBT_LASTBLOCK) {
...@@ -412,11 +493,14 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr ...@@ -412,11 +493,14 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
td->pages[i] = -1; td->pages[i] = -1;
/* Scan the maximum number of blocks */ /* Scan the maximum number of blocks */
for (block = 0; block < td->maxblocks; block++) { for (block = 0; block < td->maxblocks; block++) {
int actblock = startblock + dir * block; int actblock = startblock + dir * block;
loff_t offs = actblock << this->bbt_erase_shift;
/* Read first page */ /* Read first page */
nand_read_raw(mtd, buf, actblock << this->bbt_erase_shift, mtd->writesize, mtd->oobsize); scan_read_raw(mtd, buf, offs, mtd->writesize);
if (!check_pattern(buf, scanlen, mtd->writesize, td)) { if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift); td->pages[i] = actblock << blocktopage;
if (td->options & NAND_BBT_VERSION) { if (td->options & NAND_BBT_VERSION) {
td->version[i] = buf[mtd->writesize + td->veroffs]; td->version[i] = buf[mtd->writesize + td->veroffs];
} }
...@@ -481,8 +565,14 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, ...@@ -481,8 +565,14 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
int nrchips, bbtoffs, pageoffs, ooboffs; int nrchips, bbtoffs, pageoffs, ooboffs;
uint8_t msk[4]; uint8_t msk[4];
uint8_t rcode = td->reserved_block_code; uint8_t rcode = td->reserved_block_code;
size_t retlen, len = 0, ooblen; size_t retlen, len = 0;
loff_t to; loff_t to;
struct mtd_oob_ops ops;
ops.ooblen = mtd->oobsize;
ops.ooboffs = 0;
ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
if (!rcode) if (!rcode)
rcode = 0xff; rcode = 0xff;
...@@ -583,10 +673,10 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, ...@@ -583,10 +673,10 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
"bad block table\n"); "bad block table\n");
} }
/* Read oob data */ /* Read oob data */
ooblen = (len >> this->page_shift) * mtd->oobsize; ops.len = (len >> this->page_shift) * mtd->oobsize;
res = mtd->read_oob(mtd, to + mtd->writesize, ooblen, ops.oobbuf = &buf[len];
&retlen, &buf[len]); res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
if (res < 0 || retlen != ooblen) if (res < 0 || ops.retlen != ops.len)
goto outerr; goto outerr;
/* Calc the byte offset in the buffer */ /* Calc the byte offset in the buffer */
...@@ -635,7 +725,7 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, ...@@ -635,7 +725,7 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
if (res < 0) if (res < 0)
goto outerr; goto outerr;
res = nand_write_raw(mtd, to, len, &retlen, buf, &buf[len]); res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
if (res < 0) if (res < 0)
goto outerr; goto outerr;
......
...@@ -134,6 +134,69 @@ static void nftl_remove_dev(struct mtd_blktrans_dev *dev) ...@@ -134,6 +134,69 @@ static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
kfree(nftl); kfree(nftl);
} }
/*
* Read oob data from flash
*/
int nftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf)
{
struct mtd_oob_ops ops;
int res;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = offs & (mtd->writesize - 1);
ops.ooblen = len;
ops.oobbuf = buf;
ops.datbuf = NULL;
ops.len = len;
res = mtd->read_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
*retlen = ops.retlen;
return res;
}
/*
* Write oob data to flash
*/
int nftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf)
{
struct mtd_oob_ops ops;
int res;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = offs & (mtd->writesize - 1);
ops.ooblen = len;
ops.oobbuf = buf;
ops.datbuf = NULL;
ops.len = len;
res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
*retlen = ops.retlen;
return res;
}
/*
* Write data and oob to flash
*/
static int nftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf, uint8_t *oob)
{
struct mtd_oob_ops ops;
int res;
ops.mode = MTD_OOB_PLACE;
ops.ooboffs = offs;
ops.ooblen = mtd->oobsize;
ops.oobbuf = oob;
ops.datbuf = buf;
ops.len = len;
res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
*retlen = ops.retlen;
return res;
}
#ifdef CONFIG_NFTL_RW #ifdef CONFIG_NFTL_RW
/* Actual NFTL access routines */ /* Actual NFTL access routines */
...@@ -216,7 +279,7 @@ static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned p ...@@ -216,7 +279,7 @@ static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned p
targetEUN = thisEUN; targetEUN = thisEUN;
for (block = 0; block < nftl->EraseSize / 512; block ++) { for (block = 0; block < nftl->EraseSize / 512; block ++) {
mtd->read_oob(mtd, (thisEUN * nftl->EraseSize) + nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
(block * 512), 16 , &retlen, (block * 512), 16 , &retlen,
(char *)&oob); (char *)&oob);
if (block == 2) { if (block == 2) {
...@@ -333,7 +396,7 @@ static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned p ...@@ -333,7 +396,7 @@ static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned p
longer one */ longer one */
oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS); oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
oob.u.c.unused = 0xffffffff; oob.u.c.unused = 0xffffffff;
mtd->write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8, nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
8, &retlen, (char *)&oob.u); 8, &retlen, (char *)&oob.u);
} }
...@@ -369,17 +432,15 @@ static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned p ...@@ -369,17 +432,15 @@ static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned p
memset(&oob, 0xff, sizeof(struct nftl_oob)); memset(&oob, 0xff, sizeof(struct nftl_oob));
oob.b.Status = oob.b.Status1 = SECTOR_USED; oob.b.Status = oob.b.Status1 = SECTOR_USED;
nand_write_raw(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + nftl_write(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) +
(block * 512), 512, &retlen, movebuf, (block * 512), 512, &retlen, movebuf, (char *)&oob);
(char *)&oob);
} }
/* add the header so that it is now a valid chain */ /* add the header so that it is now a valid chain */
oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC); oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = 0xffff; oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = 0xffff;
mtd->write_oob(mtd, (nftl->EraseSize * targetEUN) + 8, nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 8,
8, &retlen, (char *)&oob.u); 8, &retlen, (char *)&oob.u);
/* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */ /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
...@@ -499,7 +560,7 @@ static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block) ...@@ -499,7 +560,7 @@ static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
lastEUN = writeEUN; lastEUN = writeEUN;
mtd->read_oob(mtd, nftl_read_oob(mtd,
(writeEUN * nftl->EraseSize) + blockofs, (writeEUN * nftl->EraseSize) + blockofs,
8, &retlen, (char *)&bci); 8, &retlen, (char *)&bci);
...@@ -588,12 +649,12 @@ static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block) ...@@ -588,12 +649,12 @@ static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
nftl->ReplUnitTable[writeEUN] = BLOCK_NIL; nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
/* ... and on the flash itself */ /* ... and on the flash itself */
mtd->read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8, nftl_read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
&retlen, (char *)&oob.u); &retlen, (char *)&oob.u);
oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC); oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
mtd->write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8, nftl_write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
&retlen, (char *)&oob.u); &retlen, (char *)&oob.u);
/* we link the new block to the chain only after the /* we link the new block to the chain only after the
...@@ -603,13 +664,13 @@ static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block) ...@@ -603,13 +664,13 @@ static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
/* Both in our cache... */ /* Both in our cache... */
nftl->ReplUnitTable[lastEUN] = writeEUN; nftl->ReplUnitTable[lastEUN] = writeEUN;
/* ... and on the flash itself */ /* ... and on the flash itself */
mtd->read_oob(mtd, (lastEUN * nftl->EraseSize) + 8, nftl_read_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
8, &retlen, (char *)&oob.u); 8, &retlen, (char *)&oob.u);
oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
= cpu_to_le16(writeEUN); = cpu_to_le16(writeEUN);
mtd->write_oob(mtd, (lastEUN * nftl->EraseSize) + 8, nftl_write_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
8, &retlen, (char *)&oob.u); 8, &retlen, (char *)&oob.u);
} }
...@@ -643,9 +704,8 @@ static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block, ...@@ -643,9 +704,8 @@ static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
memset(&oob, 0xff, sizeof(struct nftl_oob)); memset(&oob, 0xff, sizeof(struct nftl_oob));
oob.b.Status = oob.b.Status1 = SECTOR_USED; oob.b.Status = oob.b.Status1 = SECTOR_USED;
nand_write_raw(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + nftl_write(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
blockofs, 512, &retlen, (char *)buffer, 512, &retlen, (char *)buffer, (char *)&oob);
(char *)&oob);
return 0; return 0;
} }
#endif /* CONFIG_NFTL_RW */ #endif /* CONFIG_NFTL_RW */
...@@ -667,7 +727,7 @@ static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block, ...@@ -667,7 +727,7 @@ static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
if (thisEUN != BLOCK_NIL) { if (thisEUN != BLOCK_NIL) {
while (thisEUN < nftl->nb_blocks) { while (thisEUN < nftl->nb_blocks) {
if (mtd->read_oob(mtd, (thisEUN * nftl->EraseSize) + if (nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
blockofs, 8, &retlen, blockofs, 8, &retlen,
(char *)&bci) < 0) (char *)&bci) < 0)
status = SECTOR_IGNORE; status = SECTOR_IGNORE;
......
...@@ -33,6 +33,11 @@ ...@@ -33,6 +33,11 @@
char nftlmountrev[]="$Revision: 1.41 $"; char nftlmountrev[]="$Revision: 1.41 $";
extern int nftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf);
extern int nftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
size_t *retlen, uint8_t *buf);
/* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
* various device information of the NFTL partition and Bad Unit Table. Update * various device information of the NFTL partition and Bad Unit Table. Update
* the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[] * the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[]
...@@ -92,7 +97,7 @@ static int find_boot_record(struct NFTLrecord *nftl) ...@@ -92,7 +97,7 @@ static int find_boot_record(struct NFTLrecord *nftl)
} }
/* To be safer with BIOS, also use erase mark as discriminant */ /* To be safer with BIOS, also use erase mark as discriminant */
if ((ret = mtd->read_oob(mtd, block * nftl->EraseSize + if ((ret = nftl_read_oob(mtd, block * nftl->EraseSize +
SECTORSIZE + 8, 8, &retlen, SECTORSIZE + 8, 8, &retlen,
(char *)&h1) < 0)) { (char *)&h1) < 0)) {
printk(KERN_WARNING "ANAND header found at 0x%x in mtd%d, but OOB data read failed (err %d)\n", printk(KERN_WARNING "ANAND header found at 0x%x in mtd%d, but OOB data read failed (err %d)\n",
...@@ -283,7 +288,7 @@ static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int ...@@ -283,7 +288,7 @@ static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int
return -1; return -1;
if (check_oob) { if (check_oob) {
if(mtd->read_oob(mtd, address, mtd->oobsize, if(nftl_read_oob(mtd, address, mtd->oobsize,
&retlen, &buf[SECTORSIZE]) < 0) &retlen, &buf[SECTORSIZE]) < 0)
return -1; return -1;
if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0) if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
...@@ -311,7 +316,7 @@ int NFTL_formatblock(struct NFTLrecord *nftl, int block) ...@@ -311,7 +316,7 @@ int NFTL_formatblock(struct NFTLrecord *nftl, int block)
struct mtd_info *mtd = nftl->mbd.mtd; struct mtd_info *mtd = nftl->mbd.mtd;
/* Read the Unit Control Information #1 for Wear-Leveling */ /* Read the Unit Control Information #1 for Wear-Leveling */
if (mtd->read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8,
8, &retlen, (char *)&uci) < 0) 8, &retlen, (char *)&uci) < 0)
goto default_uci1; goto default_uci1;
...@@ -351,7 +356,7 @@ int NFTL_formatblock(struct NFTLrecord *nftl, int block) ...@@ -351,7 +356,7 @@ int NFTL_formatblock(struct NFTLrecord *nftl, int block)
goto fail; goto fail;
uci.WearInfo = le32_to_cpu(nb_erases); uci.WearInfo = le32_to_cpu(nb_erases);
if (mtd->write_oob(mtd, block * nftl->EraseSize + SECTORSIZE + if (nftl_write_oob(mtd, block * nftl->EraseSize + SECTORSIZE +
8, 8, &retlen, (char *)&uci) < 0) 8, 8, &retlen, (char *)&uci) < 0)
goto fail; goto fail;
return 0; return 0;
...@@ -383,7 +388,7 @@ static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_b ...@@ -383,7 +388,7 @@ static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_b
block = first_block; block = first_block;
for (;;) { for (;;) {
for (i = 0; i < sectors_per_block; i++) { for (i = 0; i < sectors_per_block; i++) {
if (mtd->read_oob(mtd, if (nftl_read_oob(mtd,
block * nftl->EraseSize + i * SECTORSIZE, block * nftl->EraseSize + i * SECTORSIZE,
8, &retlen, (char *)&bci) < 0) 8, &retlen, (char *)&bci) < 0)
status = SECTOR_IGNORE; status = SECTOR_IGNORE;
...@@ -404,7 +409,7 @@ static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_b ...@@ -404,7 +409,7 @@ static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_b
/* sector not free actually : mark it as SECTOR_IGNORE */ /* sector not free actually : mark it as SECTOR_IGNORE */
bci.Status = SECTOR_IGNORE; bci.Status = SECTOR_IGNORE;
bci.Status1 = SECTOR_IGNORE; bci.Status1 = SECTOR_IGNORE;
mtd->write_oob(mtd, block * nftl_write_oob(mtd, block *
nftl->EraseSize + nftl->EraseSize +
i * SECTORSIZE, 8, i * SECTORSIZE, 8,
&retlen, (char *)&bci); &retlen, (char *)&bci);
...@@ -498,7 +503,7 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block) ...@@ -498,7 +503,7 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block)
size_t retlen; size_t retlen;
/* check erase mark. */ /* check erase mark. */
if (mtd->read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, 8, if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, 8,
&retlen, (char *)&h1) < 0) &retlen, (char *)&h1) < 0)
return -1; return -1;
...@@ -513,7 +518,7 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block) ...@@ -513,7 +518,7 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block)
h1.EraseMark = cpu_to_le16(ERASE_MARK); h1.EraseMark = cpu_to_le16(ERASE_MARK);
h1.EraseMark1 = cpu_to_le16(ERASE_MARK); h1.EraseMark1 = cpu_to_le16(ERASE_MARK);
h1.WearInfo = cpu_to_le32(0); h1.WearInfo = cpu_to_le32(0);
if (mtd->write_oob(mtd, if (nftl_write_oob(mtd,
block * nftl->EraseSize + SECTORSIZE + 8, 8, block * nftl->EraseSize + SECTORSIZE + 8, 8,
&retlen, (char *)&h1) < 0) &retlen, (char *)&h1) < 0)
return -1; return -1;
...@@ -526,7 +531,7 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block) ...@@ -526,7 +531,7 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block)
SECTORSIZE, 0) != 0) SECTORSIZE, 0) != 0)
return -1; return -1;
if (mtd->read_oob(mtd, block * nftl->EraseSize + i, if (nftl_read_oob(mtd, block * nftl->EraseSize + i,
16, &retlen, buf) < 0) 16, &retlen, buf) < 0)
return -1; return -1;
if (i == SECTORSIZE) { if (i == SECTORSIZE) {
...@@ -557,7 +562,7 @@ static int get_fold_mark(struct NFTLrecord *nftl, unsigned int block) ...@@ -557,7 +562,7 @@ static int get_fold_mark(struct NFTLrecord *nftl, unsigned int block)
struct nftl_uci2 uci; struct nftl_uci2 uci;
size_t retlen; size_t retlen;
if (mtd->read_oob(mtd, block * nftl->EraseSize + 2 * SECTORSIZE + 8, if (nftl_read_oob(mtd, block * nftl->EraseSize + 2 * SECTORSIZE + 8,
8, &retlen, (char *)&uci) < 0) 8, &retlen, (char *)&uci) < 0)
return 0; return 0;
...@@ -597,10 +602,10 @@ int NFTL_mount(struct NFTLrecord *s) ...@@ -597,10 +602,10 @@ int NFTL_mount(struct NFTLrecord *s)
for (;;) { for (;;) {
/* read the block header. If error, we format the chain */ /* read the block header. If error, we format the chain */
if (mtd->read_oob(mtd, if (nftl_read_oob(mtd,
block * s->EraseSize + 8, 8, block * s->EraseSize + 8, 8,
&retlen, (char *)&h0) < 0 || &retlen, (char *)&h0) < 0 ||
mtd->read_oob(mtd, nftl_read_oob(mtd,
block * s->EraseSize + block * s->EraseSize +
SECTORSIZE + 8, 8, SECTORSIZE + 8, 8,
&retlen, (char *)&h1) < 0) { &retlen, (char *)&h1) < 0) {
......
...@@ -671,7 +671,7 @@ static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -671,7 +671,7 @@ static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
} }
/** /**
* onenand_read_oob - [MTD Interface] OneNAND read out-of-band * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
* @param mtd MTD device structure * @param mtd MTD device structure
* @param from offset to read from * @param from offset to read from
* @param len number of bytes to read * @param len number of bytes to read
...@@ -680,8 +680,8 @@ static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -680,8 +680,8 @@ static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
* *
* OneNAND read out-of-band data from the spare area * OneNAND read out-of-band data from the spare area
*/ */
static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf) size_t *retlen, u_char *buf)
{ {
struct onenand_chip *this = mtd->priv; struct onenand_chip *this = mtd->priv;
int read = 0, thislen, column; int read = 0, thislen, column;
...@@ -744,6 +744,21 @@ static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -744,6 +744,21 @@ static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
return ret; return ret;
} }
/**
* onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
* @mtd: MTD device structure
* @from: offset to read from
* @ops: oob operation description structure
*/
static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
struct mtd_oob_ops *ops)
{
BUG_ON(ops->mode != MTD_OOB_PLACE);
return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->len,
&ops->retlen, ops->oobbuf);
}
#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
/** /**
* onenand_verify_oob - [GENERIC] verify the oob contents after a write * onenand_verify_oob - [GENERIC] verify the oob contents after a write
...@@ -894,7 +909,7 @@ static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -894,7 +909,7 @@ static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
} }
/** /**
* onenand_write_oob - [MTD Interface] OneNAND write out-of-band * onenand_do_write_oob - [Internal] OneNAND write out-of-band
* @param mtd MTD device structure * @param mtd MTD device structure
* @param to offset to write to * @param to offset to write to
* @param len number of bytes to write * @param len number of bytes to write
...@@ -903,8 +918,8 @@ static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -903,8 +918,8 @@ static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
* *
* OneNAND write out-of-band * OneNAND write out-of-band
*/ */
static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf) size_t *retlen, const u_char *buf)
{ {
struct onenand_chip *this = mtd->priv; struct onenand_chip *this = mtd->priv;
int column, ret = 0; int column, ret = 0;
...@@ -972,6 +987,21 @@ static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -972,6 +987,21 @@ static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
return ret; return ret;
} }
/**
* onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
* @mtd: MTD device structure
* @from: offset to read from
* @ops: oob operation description structure
*/
static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
struct mtd_oob_ops *ops)
{
BUG_ON(ops->mode != MTD_OOB_PLACE);
return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->len,
&ops->retlen, ops->oobbuf);
}
/** /**
* onenand_block_checkbad - [GENERIC] Check if a block is marked bad * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
* @param mtd MTD device structure * @param mtd MTD device structure
...@@ -1138,7 +1168,7 @@ static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) ...@@ -1138,7 +1168,7 @@ static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
/* We write two bytes, so we dont have to mess with 16 bit access */ /* We write two bytes, so we dont have to mess with 16 bit access */
ofs += mtd->oobsize + (bbm->badblockpos & ~0x01); ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
return mtd->write_oob(mtd, ofs , 2, &retlen, buf); return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf);
} }
/** /**
...@@ -1328,7 +1358,7 @@ static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -1328,7 +1358,7 @@ static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0); this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
this->wait(mtd, FL_OTPING); this->wait(mtd, FL_OTPING);
ret = mtd->write_oob(mtd, from, len, retlen, buf); ret = onenand_do_write_oob(mtd, from, len, retlen, buf);
/* Exit OTP access mode */ /* Exit OTP access mode */
this->command(mtd, ONENAND_CMD_RESET, 0, 0); this->command(mtd, ONENAND_CMD_RESET, 0, 0);
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
#include <linux/mtd/onenand.h> #include <linux/mtd/onenand.h>
#include <linux/mtd/compatmac.h> #include <linux/mtd/compatmac.h>
extern int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf);
/** /**
* check_short_pattern - [GENERIC] check if a pattern is in the buffer * check_short_pattern - [GENERIC] check if a pattern is in the buffer
* @param buf the buffer to search * @param buf the buffer to search
...@@ -87,8 +90,8 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr ...@@ -87,8 +90,8 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
/* No need to read pages fully, /* No need to read pages fully,
* just read required OOB bytes */ * just read required OOB bytes */
ret = mtd->read_oob(mtd, from + j * mtd->writesize + bd->offs, ret = onenand_do_read_oob(mtd, from + j * mtd->writesize + bd->offs,
readlen, &retlen, &buf[0]); readlen, &retlen, &buf[0]);
if (ret) if (ret)
return ret; return ret;
......
...@@ -100,6 +100,7 @@ struct jffs2_sb_info { ...@@ -100,6 +100,7 @@ struct jffs2_sb_info {
#ifdef CONFIG_JFFS2_FS_WRITEBUFFER #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
/* Write-behind buffer for NAND flash */ /* Write-behind buffer for NAND flash */
unsigned char *wbuf; unsigned char *wbuf;
unsigned char *oobbuf;
uint32_t wbuf_ofs; uint32_t wbuf_ofs;
uint32_t wbuf_len; uint32_t wbuf_len;
struct jffs2_inodirty *wbuf_inodes; struct jffs2_inodirty *wbuf_inodes;
......
...@@ -955,158 +955,159 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re ...@@ -955,158 +955,159 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re
return ret; return ret;
} }
#define NR_OOB_SCAN_PAGES 4
/* /*
* Check, if the out of band area is empty * Check, if the out of band area is empty
*/ */
int jffs2_check_oob_empty( struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int mode) int jffs2_check_oob_empty(struct jffs2_sb_info *c,
struct jffs2_eraseblock *jeb, int mode)
{ {
unsigned char *buf; int i, page, ret;
int ret = 0; int oobsize = c->mtd->oobsize;
int i,len,page; struct mtd_oob_ops ops;
size_t retlen;
int oob_size; ops.len = NR_OOB_SCAN_PAGES * oobsize;
ops.ooblen = oobsize;
/* allocate a buffer for all oob data in this sector */ ops.oobbuf = c->oobbuf;
oob_size = c->mtd->oobsize; ops.ooboffs = 0;
len = 4 * oob_size; ops.datbuf = NULL;
buf = kmalloc(len, GFP_KERNEL); ops.mode = MTD_OOB_PLACE;
if (!buf) {
printk(KERN_NOTICE "jffs2_check_oob_empty(): allocation of temporary data buffer for oob check failed\n"); ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
return -ENOMEM;
}
/*
* if mode = 0, we scan for a total empty oob area, else we have
* to take care of the cleanmarker in the first page of the block
*/
ret = jffs2_flash_read_oob(c, jeb->offset, len , &retlen, buf);
if (ret) { if (ret) {
D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB failed %d for block at %08x\n", ret, jeb->offset)); D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
goto out; "failed %d for block at %08x\n", ret, jeb->offset));
return ret;
} }
if (retlen < len) { if (ops.retlen < ops.len) {
D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB return short read " D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
"(%zd bytes not %d) for block at %08x\n", retlen, len, jeb->offset)); "returned short read (%zd bytes not %d) for block "
ret = -EIO; "at %08x\n", ops.retlen, ops.len, jeb->offset));
goto out; return -EIO;
} }
/* Special check for first page */ /* Special check for first page */
for(i = 0; i < oob_size ; i++) { for(i = 0; i < oobsize ; i++) {
/* Yeah, we know about the cleanmarker. */ /* Yeah, we know about the cleanmarker. */
if (mode && i >= c->fsdata_pos && if (mode && i >= c->fsdata_pos &&
i < c->fsdata_pos + c->fsdata_len) i < c->fsdata_pos + c->fsdata_len)
continue; continue;
if (buf[i] != 0xFF) { if (ops.oobbuf[i] != 0xFF) {
D2(printk(KERN_DEBUG "Found %02x at %x in OOB for %08x\n", D2(printk(KERN_DEBUG "Found %02x at %x in OOB for "
buf[i], i, jeb->offset)); "%08x\n", ops.oobbuf[i], i, jeb->offset));
ret = 1; return 1;
goto out;
} }
} }
/* we know, we are aligned :) */ /* we know, we are aligned :) */
for (page = oob_size; page < len; page += sizeof(long)) { for (page = oobsize; page < ops.len; page += sizeof(long)) {
unsigned long dat = *(unsigned long *)(&buf[page]); long dat = *(long *)(&ops.oobbuf[page]);
if(dat != -1) { if(dat != -1)
ret = 1; return 1;
goto out;
}
} }
return 0;
out:
kfree(buf);
return ret;
} }
/* /*
* Scan for a valid cleanmarker and for bad blocks * Scan for a valid cleanmarker and for bad blocks
* For virtual blocks (concatenated physical blocks) check the cleanmarker */
* only in the first page of the first physical block, but scan for bad blocks in all int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c,
* physical blocks struct jffs2_eraseblock *jeb)
*/
int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
{ {
struct jffs2_unknown_node n; struct jffs2_unknown_node n;
unsigned char buf[2 * NAND_MAX_OOBSIZE]; struct mtd_oob_ops ops;
unsigned char *p; int oobsize = c->mtd->oobsize;
int ret, i, cnt, retval = 0; unsigned char *p,*b;
size_t retlen, offset; int i, ret;
int oob_size; size_t offset = jeb->offset;
offset = jeb->offset; /* Check first if the block is bad. */
oob_size = c->mtd->oobsize; if (c->mtd->block_isbad(c->mtd, offset)) {
D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()"
/* Loop through the physical blocks */ ": Bad block at %08x\n", jeb->offset));
for (cnt = 0; cnt < (c->sector_size / c->mtd->erasesize); cnt++) { return 2;
/* Check first if the block is bad. */ }
if (c->mtd->block_isbad (c->mtd, offset)) {
D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Bad block at %08x\n", jeb->offset));
return 2;
}
/*
* We read oob data from page 0 and 1 of the block.
* page 0 contains cleanmarker and badblock info
* page 1 contains failure count of this block
*/
ret = c->mtd->read_oob (c->mtd, offset, oob_size << 1, &retlen, buf);
if (ret) { ops.len = oobsize;
D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB failed %d for block at %08x\n", ret, jeb->offset)); ops.ooblen = oobsize;
return ret; ops.oobbuf = c->oobbuf;
} ops.ooboffs = 0;
if (retlen < (oob_size << 1)) { ops.datbuf = NULL;
D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB return short read (%zd bytes not %d) for block at %08x\n", retlen, oob_size << 1, jeb->offset)); ops.mode = MTD_OOB_PLACE;
return -EIO;
}
/* Check cleanmarker only on the first physical block */ ret = c->mtd->read_oob(c->mtd, offset, &ops);
if (!cnt) { if (ret) {
n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); "Read OOB failed %d for block at %08x\n",
n.totlen = cpu_to_je32 (8); ret, jeb->offset));
p = (unsigned char *) &n; return ret;
}
for (i = 0; i < c->fsdata_len; i++) { if (ops.retlen < ops.len) {
if (buf[c->fsdata_pos + i] != p[i]) { D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): "
retval = 1; "Read OOB return short read (%zd bytes not %d) "
} "for block at %08x\n", ops.retlen, ops.len,
} jeb->offset));
D1(if (retval == 1) { return -EIO;
printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): Cleanmarker node not detected in block at %08x\n", jeb->offset);
printk(KERN_WARNING "OOB at %08zx was ", offset);
for (i=0; i < oob_size; i++) {
printk("%02x ", buf[i]);
}
printk("\n");
})
}
offset += c->mtd->erasesize;
} }
return retval;
n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
n.totlen = cpu_to_je32 (8);
p = (unsigned char *) &n;
b = c->oobbuf + c->fsdata_pos;
for (i = c->fsdata_len; i; i--) {
if (*b++ != *p++)
ret = 1;
}
D1(if (ret == 1) {
printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
"Cleanmarker node not detected in block at %08x\n",
offset);
printk(KERN_WARNING "OOB at %08zx was ", offset);
for (i=0; i < oobsize; i++)
printk("%02x ", c->oobbuf[i]);
printk("\n");
});
return ret;
} }
int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
struct jffs2_eraseblock *jeb)
{ {
struct jffs2_unknown_node n; struct jffs2_unknown_node n;
int ret; int ret;
size_t retlen; struct mtd_oob_ops ops;
n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
n.totlen = cpu_to_je32(8); n.totlen = cpu_to_je32(8);
ret = jffs2_flash_write_oob(c, jeb->offset + c->fsdata_pos, c->fsdata_len, &retlen, (unsigned char *)&n); ops.len = c->fsdata_len;
ops.ooblen = c->fsdata_len;;
ops.oobbuf = (uint8_t *)&n;
ops.ooboffs = c->fsdata_pos;
ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops);
if (ret) { if (ret) {
D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Write failed for block at %08x: error %d\n", jeb->offset, ret)); D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
"Write failed for block at %08x: error %d\n",
jeb->offset, ret));
return ret; return ret;
} }
if (retlen != c->fsdata_len) { if (ops.retlen != ops.len) {
D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Short write for block at %08x: %zd not %d\n", jeb->offset, retlen, c->fsdata_len)); D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
return ret; "Short write for block at %08x: %zd not %d\n",
jeb->offset, ops.retlen, ops.len));
return -EIO;
} }
return 0; return 0;
} }
...@@ -1185,6 +1186,10 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c) ...@@ -1185,6 +1186,10 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
if (!c->wbuf) if (!c->wbuf)
return -ENOMEM; return -ENOMEM;
c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL);
if (!c->oobbuf)
return -ENOMEM;
res = jffs2_nand_set_oobinfo(c); res = jffs2_nand_set_oobinfo(c);
#ifdef BREAKME #ifdef BREAKME
...@@ -1202,6 +1207,7 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c) ...@@ -1202,6 +1207,7 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c) void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
{ {
kfree(c->wbuf); kfree(c->wbuf);
kfree(c->oobbuf);
} }
int jffs2_dataflash_setup(struct jffs2_sb_info *c) { int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
......
...@@ -67,6 +67,50 @@ struct mtd_ecc_stats { ...@@ -67,6 +67,50 @@ struct mtd_ecc_stats {
unsigned long failed; unsigned long failed;
}; };
/*
* oob operation modes
*
* MTD_OOB_PLACE: oob data are placed at the given offset
* MTD_OOB_AUTO: oob data are automatically placed at the free areas
* which are defined by the ecclayout
* MTD_OOB_RAW: mode to read raw data+oob in one chunk. The oob data
* is inserted into the data. Thats a raw image of the
* flash contents.
*/
typedef enum {
MTD_OOB_PLACE,
MTD_OOB_AUTO,
MTD_OOB_RAW,
} mtd_oob_mode_t;
/**
* struct mtd_oob_ops - oob operation operands
* @mode: operation mode
*
* @len: number of bytes to write/read. When a data buffer is given
* (datbuf != NULL) this is the number of data bytes. When
+ no data buffer is available this is the number of oob bytes.
*
* @retlen: number of bytes written/read. When a data buffer is given
* (datbuf != NULL) this is the number of data bytes. When
+ no data buffer is available this is the number of oob bytes.
*
* @ooblen: number of oob bytes per page
* @ooboffs: offset of oob data in the oob area (only relevant when
* mode = MTD_OOB_PLACE)
* @datbuf: data buffer - if NULL only oob data are read/written
* @oobbuf: oob data buffer
*/
struct mtd_oob_ops {
mtd_oob_mode_t mode;
size_t len;
size_t retlen;
size_t ooblen;
uint32_t ooboffs;
uint8_t *datbuf;
uint8_t *oobbuf;
};
struct mtd_info { struct mtd_info {
u_char type; u_char type;
u_int32_t flags; u_int32_t flags;
...@@ -125,8 +169,10 @@ struct mtd_info { ...@@ -125,8 +169,10 @@ struct mtd_info {
int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
int (*read_oob) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); int (*read_oob) (struct mtd_info *mtd, loff_t from,
int (*write_oob) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); struct mtd_oob_ops *ops);
int (*write_oob) (struct mtd_info *mtd, loff_t to,
struct mtd_oob_ops *ops);
/* /*
* Methods to access the protection register area, present in some * Methods to access the protection register area, present in some
......
...@@ -31,14 +31,6 @@ extern int nand_scan (struct mtd_info *mtd, int max_chips); ...@@ -31,14 +31,6 @@ extern int nand_scan (struct mtd_info *mtd, int max_chips);
/* Free resources held by the NAND device */ /* Free resources held by the NAND device */
extern void nand_release (struct mtd_info *mtd); extern void nand_release (struct mtd_info *mtd);
/* Read raw data from the device without ECC */
extern int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from,
size_t len, size_t ooblen);
extern int nand_write_raw(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const uint8_t *buf, uint8_t *oob);
/* The maximum number of NAND chips in an array */ /* The maximum number of NAND chips in an array */
#define NAND_MAX_CHIPS 8 #define NAND_MAX_CHIPS 8
...@@ -375,6 +367,8 @@ struct nand_chip { ...@@ -375,6 +367,8 @@ struct nand_chip {
struct nand_buffers buffers; struct nand_buffers buffers;
struct nand_hw_control hwcontrol; struct nand_hw_control hwcontrol;
struct mtd_oob_ops ops;
uint8_t *bbt; uint8_t *bbt;
struct nand_bbt_descr *bbt_td; struct nand_bbt_descr *bbt_td;
struct nand_bbt_descr *bbt_md; struct nand_bbt_descr *bbt_md;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册