[MTD] NAND Expose the new raw mode function and status info to userspace
[safe/jmp/linux-2.6] / drivers / mtd / nand / nand_base.c
index 023224d..ea6d2c3 100644 (file)
 #endif
 
 /* Define default oob placement schemes for large and small page devices */
-static struct nand_oobinfo nand_oob_8 = {
-       .useecc = MTD_NANDECC_AUTOPLACE,
+static struct nand_ecclayout nand_oob_8 = {
        .eccbytes = 3,
        .eccpos = {0, 1, 2},
-       .oobfree = {{3, 2}, {6, 2}}
+       .oobfree = {
+               {.offset = 3,
+                .length = 2},
+               {.offset = 6,
+                .length = 2}}
 };
 
-static struct nand_oobinfo nand_oob_16 = {
-       .useecc = MTD_NANDECC_AUTOPLACE,
+static struct nand_ecclayout nand_oob_16 = {
        .eccbytes = 6,
        .eccpos = {0, 1, 2, 3, 6, 7},
-       .oobfree = {{8, 8}}
+       .oobfree = {
+               {.offset = 8,
+                . length = 8}}
 };
 
-static struct nand_oobinfo nand_oob_64 = {
-       .useecc = MTD_NANDECC_AUTOPLACE,
+static struct nand_ecclayout nand_oob_64 = {
        .eccbytes = 24,
        .eccpos = {
                   40, 41, 42, 43, 44, 45, 46, 47,
                   48, 49, 50, 51, 52, 53, 54, 55,
                   56, 57, 58, 59, 60, 61, 62, 63},
-       .oobfree = {{2, 38}}
+       .oobfree = {
+               {.offset = 2,
+                .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,
                           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
  * compiled away when LED support is disabled.
@@ -353,8 +347,7 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 {
        struct nand_chip *chip = mtd->priv;
        uint8_t buf[2] = { 0, 0 };
-       size_t retlen;
-       int block;
+       int block, ret;
 
        /* Get block number */
        block = ((int)ofs) >> chip->bbt_erase_shift;
@@ -363,11 +356,22 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 
        /* Do we have a flash based bad block table ? */
        if (chip->options & NAND_USE_FLASH_BBT)
-               return nand_update_bbt(mtd, ofs);
+               ret = nand_update_bbt(mtd, ofs);
+       else {
+               /* We write two bytes, so we dont have to mess with 16 bit
+                * access
+                */
+               ofs += mtd->oobsize;
+               chip->ops.len = 2;
+               chip->ops.datbuf = NULL;
+               chip->ops.oobbuf = buf;
+               chip->ops.ooboffs = chip->badblockpos & ~0x01;
 
-       /* We write two bytes, so we dont have to mess with 16 bit access */
-       ofs += mtd->oobsize + (chip->badblockpos & ~0x01);
-       return nand_write_oob(mtd, ofs, 2, &retlen, buf);
+               ret = nand_do_write_oob(mtd, ofs, &chip->ops);
+       }
+       if (!ret)
+               mtd->ecc_stats.badblocks++;
+       return ret;
 }
 
 /**
@@ -735,6 +739,20 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
 }
 
 /**
+ * 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
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
@@ -749,13 +767,9 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
        uint8_t *p = buf;
        uint8_t *ecc_calc = chip->buffers.ecccalc;
        uint8_t *ecc_code = chip->buffers.ecccode;
-       int *eccpos = chip->autooob->eccpos;
-
-       chip->read_buf(mtd, buf, mtd->writesize);
-       chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
+       int *eccpos = chip->ecc.layout->eccpos;
 
-       if (chip->ecc.mode == NAND_ECC_NONE)
-               return 0;
+       nand_read_page_raw(mtd, chip, buf);
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
                chip->ecc.calculate(mtd, p, &ecc_calc[i]);
@@ -795,7 +809,7 @@ static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
        uint8_t *p = buf;
        uint8_t *ecc_calc = chip->buffers.ecccalc;
        uint8_t *ecc_code = chip->buffers.ecccode;
-       int *eccpos = chip->autooob->eccpos;
+       int *eccpos = chip->ecc.layout->eccpos;
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
                chip->ecc.hwctl(mtd, NAND_ECC_READ);
@@ -877,18 +891,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
  * @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.
  */
-int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
-                size_t *retlen, uint8_t *buf)
+static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
+                           struct mtd_oob_ops *ops)
 {
        int chipnr, page, realpage, col, bytes, aligned;
        struct nand_chip *chip = mtd->priv;
@@ -896,8 +942,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 sndcmd = 1;
        int ret = 0;
-       uint32_t readlen = len;
-       uint8_t *bufpoi;
+       uint32_t readlen = ops->len;
+       uint8_t *bufpoi, *oob, *buf;
 
        stats = mtd->ecc_stats;
 
@@ -910,12 +956,15 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
        col = (int)(from & (mtd->writesize - 1));
        chip->oob_poi = chip->buffers.oobrbuf;
 
+       buf = ops->datbuf;
+       oob = ops->oobbuf;
+
        while(1) {
                bytes = min(mtd->writesize - col, readlen);
                aligned = (bytes == mtd->writesize);
 
                /* Is the current page in the buffer ? */
-               if (realpage != chip->pagebuf) {
+               if (realpage != chip->pagebuf || oob) {
                        bufpoi = aligned ? buf : chip->buffers.databuf;
 
                        if (likely(sndcmd)) {
@@ -934,6 +983,16 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
                                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)) {
                                /*
                                 * Apply delay or wait for ready/busy pin. Do
@@ -947,10 +1006,11 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
                                else
                                        nand_wait_ready(mtd);
                        }
-               } else
+               } else {
                        memcpy(buf, chip->buffers.databuf + col, bytes);
+                       buf += bytes;
+               }
 
-               buf += bytes;
                readlen -= bytes;
 
                if (!readlen)
@@ -976,12 +1036,15 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
                        sndcmd = 1;
        }
 
-       *retlen = len - (size_t) readlen;
+       ops->retlen = ops->len - (size_t) readlen;
 
        if (ret)
                return ret;
 
-       return mtd->ecc_stats.failed - stats.failed ? -EBADMSG : 0;
+       if (mtd->ecc_stats.failed - stats.failed)
+               return -EBADMSG;
+
+       return  mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
 }
 
 /**
@@ -997,57 +1060,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,
                     size_t *retlen, uint8_t *buf)
 {
+       struct nand_chip *chip = mtd->priv;
        int ret;
 
-       *retlen = 0;
        /* Do not allow reads past end of device */
        if ((from + len) > mtd->size)
                return -EINVAL;
        if (!len)
                return 0;
 
-       nand_get_device(mtd->priv, mtd, FL_READING);
+       nand_get_device(chip, mtd, FL_READING);
+
+       chip->ops.len = len;
+       chip->ops.datbuf = buf;
+       chip->ops.oobbuf = NULL;
 
-       ret = nand_do_read(mtd, from, len, retlen, buf);
+       ret = nand_do_read_ops(mtd, from, &chip->ops);
 
        nand_release_device(mtd);
 
+       *retlen = chip->ops.retlen;
        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
  * @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
+ * @ops:       oob operations description structure
  *
  * 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,
-                        size_t *retlen, uint8_t *buf)
+static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
+                           struct mtd_oob_ops *ops)
 {
        int col, page, realpage, chipnr, sndcmd = 1;
        struct nand_chip *chip = mtd->priv;
        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",
              (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);
        chip->select_chip(mtd, chipnr);
 
@@ -1055,20 +1110,31 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
        realpage = (int)(from >> chip->page_shift);
        page = realpage & chip->pagemask;
 
-       /* Mask to get column */
-       col = from & (mtd->oobsize - 1);
+       if (ops->mode != MTD_OOB_AUTO) {
+               col = ops->ooboffs;
+               direct = 1;
+       } else {
+               col = 0;
+               direct = 0;
+       }
 
        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)) {
                        chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
                        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)
                        break;
 
@@ -1085,10 +1151,6 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
                                nand_wait_ready(mtd);
                }
 
-               buf += bytes;
-               bytes = mtd->oobsize;
-               col = 0;
-
                /* Increment page address */
                realpage++;
 
@@ -1107,81 +1169,76 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
                        sndcmd = 1;
        }
 
-       /* Deselect and wake up anyone waiting on the device */
-       nand_release_device(mtd);
-
-       *retlen = len;
+       ops->retlen = ops->len;
        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
- * @buf:       temporary buffer
  * @from:      offset to read from
- * @len:       number of bytes to read
- * @ooblen:    number of oob data bytes to read
+ * @ops:       oob operation description structure
  *
- * 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,
-                 size_t ooblen)
+static int nand_read_oob(struct mtd_info *mtd, loff_t from,
+                        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;
-       int page = (int)(from >> chip->page_shift);
-       int chipnr = (int)(from >> chip->chip_shift);
-       int sndcmd = 1;
-       int cnt = 0;
-       int pagesize = mtd->writesize + mtd->oobsize;
-       int blockcheck;
+       int ret = -ENOTSUPP;
+
+       ops->retlen = 0;
 
        /* Do not allow reads past end of device */
-       if ((from + len) > mtd->size) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: "
+       if ((from + ops->len) > mtd->size) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
                      "Attempt read beyond end of device\n");
                return -EINVAL;
        }
 
-       /* Grab the lock and see if the device is available */
        nand_get_device(chip, mtd, FL_READING);
 
-       chip->select_chip(mtd, chipnr);
-
-       /* Add requested oob length */
-       len += ooblen;
-       blockcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
+       switch(ops->mode) {
+       case MTD_OOB_PLACE:
+       case MTD_OOB_AUTO:
+               break;
 
-       while (len) {
-               if (likely(sndcmd)) {
-                       chip->cmdfunc(mtd, NAND_CMD_READ0, 0,
-                                     page & chip->pagemask);
-                       sndcmd = 0;
-               }
+       case MTD_OOB_RAW:
+               /* Replace the read_page algorithm temporary */
+               read_page = chip->ecc.read_page;
+               chip->ecc.read_page = nand_read_page_raw;
+               break;
 
-               chip->read_buf(mtd, &buf[cnt], pagesize);
+       default:
+               goto out;
+       }
 
-               len -= pagesize;
-               cnt += pagesize;
-               page++;
+       if (!ops->datbuf)
+               ret = nand_do_read_oob(mtd, from, ops);
+       else
+               ret = nand_do_read_ops(mtd, from, ops);
 
-               if (!(chip->options & NAND_NO_READRDY)) {
-                       if (!chip->dev_ready)
-                               udelay(chip->chip_delay);
-                       else
-                               nand_wait_ready(mtd);
-               }
+       if (unlikely(ops->mode == MTD_OOB_RAW))
+               chip->ecc.read_page = read_page;
+ out:
+       nand_release_device(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);
-       return 0;
+/**
+ * nand_write_page_raw - [Intern] raw page write function
+ * @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);
 }
 
 /**
@@ -1198,19 +1255,16 @@ static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
        int eccsteps = chip->ecc.steps;
        uint8_t *ecc_calc = chip->buffers.ecccalc;
        const uint8_t *p = buf;
-       int *eccpos = chip->autooob->eccpos;
+       int *eccpos = chip->ecc.layout->eccpos;
 
-       if (chip->ecc.mode != NAND_ECC_NONE) {
-               /* Software ecc calculation */
-               for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
-                       chip->ecc.calculate(mtd, p, &ecc_calc[i]);
+       /* Software ecc calculation */
+       for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
+               chip->ecc.calculate(mtd, p, &ecc_calc[i]);
 
-               for (i = 0; i < chip->ecc.total; i++)
-                       chip->oob_poi[eccpos[i]] = ecc_calc[i];
-       }
+       for (i = 0; i < chip->ecc.total; i++)
+               chip->oob_poi[eccpos[i]] = ecc_calc[i];
 
-       chip->write_buf(mtd, buf, mtd->writesize);
-       chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
+       nand_write_page_raw(mtd, chip, buf);
 }
 
 /**
@@ -1227,7 +1281,7 @@ static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
        int eccsteps = chip->ecc.steps;
        uint8_t *ecc_calc = chip->buffers.ecccalc;
        const uint8_t *p = buf;
-       int *eccpos = chip->autooob->eccpos;
+       int *eccpos = chip->ecc.layout->eccpos;
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
                chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
@@ -1337,51 +1391,77 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
        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
 
 /**
- * nand_write - [MTD Interface] NAND write with ECC
+ * nand_do_write_ops - [Internal] NAND write with ECC
  * @mtd:       MTD device structure
  * @to:                offset to write to
- * @len:       number of bytes to write
- * @retlen:    pointer to variable to store the number of written bytes
- * @buf:       the data to write
+ * @ops:       oob operations description structure
  *
  * NAND write with ECC
  */
-static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
-                         size_t *retlen, const uint8_t *buf)
+static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
+                            struct mtd_oob_ops *ops)
 {
        int chipnr, realpage, page, blockmask;
        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 ret = -EIO;
+       int ret;
 
-       *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;
-       }
+       ops->retlen = 0;
 
        /* reject writes, which are not page aligned */
-       if (NOTALIGNED(to) || NOTALIGNED(len)) {
+       if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
                printk(KERN_NOTICE "nand_write: "
                       "Attempt to write not page aligned data\n");
                return -EINVAL;
        }
 
-       if (!len)
+       if (!writelen)
                return 0;
 
-       nand_get_device(chip, mtd, FL_WRITING);
-
        /* Check, if it is write protected */
        if (nand_check_wp(mtd))
-               goto out;
+               return -EIO;
 
        chipnr = (int)(to >> chip->chip_shift);
        chip->select_chip(mtd, chipnr);
@@ -1392,7 +1472,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 */
        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->oob_poi = chip->buffers.oobwbuf;
@@ -1400,6 +1480,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
        while(1) {
                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);
                if (ret)
                        break;
@@ -1419,94 +1502,74 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
                        chip->select_chip(mtd, chipnr);
                }
        }
- out:
-       *retlen = len - writelen;
-       nand_release_device(mtd);
+
+       if (unlikely(oob))
+               memset(chip->oob_poi, 0xff, mtd->oobsize);
+
+       ops->retlen = ops->len - writelen;
        return ret;
 }
 
 /**
- * nand_write_raw - [GENERIC] Write raw data including oob
+ * nand_write - [MTD Interface] NAND write with ECC
  * @mtd:       MTD device structure
- * @buf:       source buffer
  * @to:                offset to write to
  * @len:       number of bytes to write
- * @buf:       source buffer
- * @oob:       oob buffer
+ * @retlen:    pointer to variable to store the number of written bytes
+ * @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,
-                  const uint8_t *buf, uint8_t *oob)
+static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
+                         size_t *retlen, const uint8_t *buf)
 {
        struct nand_chip *chip = mtd->priv;
-       int page = (int)(to >> chip->page_shift);
-       int chipnr = (int)(to >> chip->chip_shift);
        int ret;
 
-       *retlen = 0;
-
-       /* 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");
+       /* Do not allow reads past end of device */
+       if ((to + len) > mtd->size)
                return -EINVAL;
-       }
+       if (!len)
+               return 0;
 
-       /* Grab the lock and see if the device is available */
-       nand_get_device(chip, mtd, FL_WRITING);
+       nand_get_device(chip, mtd, FL_READING);
 
-       chip->select_chip(mtd, chipnr);
-       chip->oob_poi = oob;
+       chip->ops.len = len;
+       chip->ops.datbuf = (uint8_t *)buf;
+       chip->ops.oobbuf = NULL;
 
-       while (len != *retlen) {
-               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;
-       }
+       ret = nand_do_write_ops(mtd, to, &chip->ops);
 
-       /* Deselect and wake up anyone waiting on the device */
        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
  * @to:                offset to write to
- * @len:       number of bytes to write
- * @retlen:    pointer to variable to store the number of written bytes
- * @buf:       the data to write
+ * @ops:       oob operation description structure
  *
  * NAND write out-of-band
  */
-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_do_write_oob(struct mtd_info *mtd, loff_t to,
+                            struct mtd_oob_ops *ops)
 {
-       int column, page, status, ret = -EIO, chipnr;
+       int chipnr, page, status;
        struct nand_chip *chip = mtd->priv;
 
        DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
-             (unsigned int)to, (int)len);
-
-       /* Initialize return length value */
-       *retlen = 0;
+             (unsigned int)to, (int)ops->len);
 
        /* Do not allow write past end of page */
-       column = to & (mtd->oobsize - 1);
-       if ((column + len) > mtd->oobsize) {
+       if ((ops->ooboffs + ops->len) > mtd->oobsize) {
                DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
                      "Attempt to write past end of page\n");
                return -EINVAL;
        }
 
-       nand_get_device(chip, mtd, FL_WRITING);
-
        chipnr = (int)(to >> chip->chip_shift);
        chip->select_chip(mtd, chipnr);
 
@@ -1523,26 +1586,27 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
 
        /* Check, if it is write protected */
        if (nand_check_wp(mtd))
-               goto out;
+               return -EROFS;
 
        /* Invalidate the page cache, if we write to the cached page */
        if (page == chip->pagebuf)
                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,
                              page & chip->pagemask);
-               /* prepad 0xff for partial programming */
-               chip->write_buf(mtd, ffchars, column);
-               /* write data */
-               chip->write_buf(mtd, buf, len);
-               /* postpad 0xff for partial programming */
-               chip->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
+               chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
+               memset(chip->oob_poi, 0xff, mtd->oobsize);
        } else {
-               chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column,
+               chip->cmdfunc(mtd, NAND_CMD_SEQIN,
+                             mtd->writesize + ops->ooboffs,
                              page & chip->pagemask);
-               chip->write_buf(mtd, buf, len);
+               chip->write_buf(mtd, ops->oobbuf, ops->len);
        }
+
        /* Send command to program the OOB data */
        chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
 
@@ -1552,27 +1616,75 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
        if (status & NAND_STATUS_FAIL) {
                DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
                      "Failed write, page 0x%08x\n", page);
-               ret = -EIO;
-               goto out;
+               return -EIO;
        }
-       *retlen = len;
+       ops->retlen = ops->len;
 
 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
-       /* Send command to read back the data */
-       chip->cmdfunc(mtd, NAND_CMD_READOOB, column, page & chip->pagemask);
+       if (ops->mode != MTD_OOB_AUTO) {
+               /* 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)) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
-                     "Failed write verify, page 0x%08x\n", page);
-               ret = -EIO;
-               goto out;
+               if (chip->verify_buf(mtd, ops->oobbuf, ops->len)) {
+                       DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
+                             "Failed write verify, page 0x%08x\n", page);
+                       return -EIO;
+               }
        }
 #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:
-       /* Deselect and wake up anyone waiting on the device */
        nand_release_device(mtd);
-
        return ret;
 }
 
@@ -2124,16 +2236,16 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        /*
         * If no default placement scheme is given, select an appropriate one
         */
-       if (!chip->autooob) {
+       if (!chip->ecc.layout) {
                switch (mtd->oobsize) {
                case 8:
-                       chip->autooob = &nand_oob_8;
+                       chip->ecc.layout = &nand_oob_8;
                        break;
                case 16:
-                       chip->autooob = &nand_oob_16;
+                       chip->ecc.layout = &nand_oob_16;
                        break;
                case 64:
-                       chip->autooob = &nand_oob_64;
+                       chip->ecc.layout = &nand_oob_64;
                        break;
                default:
                        printk(KERN_WARNING "No oob scheme defined for "
@@ -2143,14 +2255,6 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        }
 
        /*
-        * The number of bytes available for the filesystem to place fs
-        * dependend oob data
-        */
-       mtd->oobavail = 0;
-       for (i = 0; chip->autooob->oobfree[i][1]; i++)
-               mtd->oobavail += chip->autooob->oobfree[i][1];
-
-       /*
         * check ECC mode, default to software if 3byte/512byte hardware ECC is
         * selected and we have 256 byte pagesize fallback to software ECC
         */
@@ -2194,8 +2298,8 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        case NAND_ECC_NONE:
                printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
                       "This is not recommended !!\n");
-               chip->ecc.read_page = nand_read_page_swecc;
-               chip->ecc.write_page = nand_write_page_swecc;
+               chip->ecc.read_page = nand_read_page_raw;
+               chip->ecc.write_page = nand_write_page_raw;
                chip->ecc.size = mtd->writesize;
                chip->ecc.bytes = 0;
                break;
@@ -2206,6 +2310,15 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        }
 
        /*
+        * The number of bytes available for a client to place data into
+        * the out of band area
+        */
+       chip->ecc.layout->oobavail = 0;
+       for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
+               chip->ecc.layout->oobavail +=
+                       chip->ecc.layout->oobfree[i].length;
+
+       /*
         * Set the number of read / write steps for one page depending on ECC
         * mode
         */
@@ -2244,8 +2357,8 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        mtd->block_isbad = nand_block_isbad;
        mtd->block_markbad = nand_block_markbad;
 
-       /* and make the autooob the default one */
-       memcpy(&mtd->oobinfo, chip->autooob, sizeof(mtd->oobinfo));
+       /* propagate ecc.layout to mtd_info */
+       mtd->ecclayout = chip->ecc.layout;
 
        /* Check, if we should skip the bad block table scan */
        if (chip->options & NAND_SKIP_BBTSCAN)