libata: honour host controllers that want just one host
[safe/jmp/linux-2.6] / drivers / mtd / nand / nand_base.c
index cead9fc..04de315 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.
@@ -161,7 +155,7 @@ static u16 nand_read_word(struct mtd_info *mtd)
 /**
  * nand_select_chip - [DEFAULT] control CE line
  * @mtd:       MTD device structure
- * @chip:      chipnumber to select, -1 for deselect
+ * @chipnr:    chipnumber to select, -1 for deselect
  *
  * Default select function for 1 chip devices.
  */
@@ -174,8 +168,6 @@ static void nand_select_chip(struct mtd_info *mtd, int chipnr)
                chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
                break;
        case 0:
-               chip->cmd_ctrl(mtd, NAND_CMD_NONE,
-                              NAND_NCE | NAND_CTRL_CHANGE);
                break;
 
        default:
@@ -320,7 +312,7 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
                /* Select the NAND device */
                chip->select_chip(mtd, chipnr);
        } else
-               page = (int)ofs;
+               page = (int)(ofs >> chip->page_shift);
 
        if (chip->options & NAND_BUSWIDTH_16) {
                chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
@@ -355,21 +347,31 @@ 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;
+       block = (int)(ofs >> chip->bbt_erase_shift);
        if (chip->bbt)
                chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
 
        /* 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 = chip->ops.ooblen = 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;
 }
 
 /**
@@ -413,7 +415,7 @@ static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
  * Wait for the ready pin, after a command
  * The timeout is catched later.
  */
-static void nand_wait_ready(struct mtd_info *mtd)
+void nand_wait_ready(struct mtd_info *mtd)
 {
        struct nand_chip *chip = mtd->priv;
        unsigned long timeo = jiffies + 2;
@@ -427,6 +429,7 @@ static void nand_wait_ready(struct mtd_info *mtd)
        } while (time_before(jiffies, timeo));
        led_trigger_event(nand_led_trigger, LED_OFF);
 }
+EXPORT_SYMBOL_GPL(nand_wait_ready);
 
 /**
  * nand_command - [DEFAULT] Send command to NAND device
@@ -499,7 +502,6 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
        case NAND_CMD_ERASE2:
        case NAND_CMD_SEQIN:
        case NAND_CMD_STATUS:
-               chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
                return;
 
        case NAND_CMD_RESET:
@@ -541,7 +543,6 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
  * Send command to NAND device. This is the version for the new large page
  * devices We dont have the separate regions as we have in the small page
  * devices.  We must emulate NAND_CMD_READOOB to keep the code compatible.
- *
  */
 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                            int column, int page_addr)
@@ -593,6 +594,7 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
        case NAND_CMD_ERASE1:
        case NAND_CMD_ERASE2:
        case NAND_CMD_SEQIN:
+       case NAND_CMD_RNDIN:
        case NAND_CMD_STATUS:
        case NAND_CMD_DEPLETE1:
                return;
@@ -619,6 +621,14 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
                return;
 
+       case NAND_CMD_RNDOUT:
+               /* No ready / busy check necessary */
+               chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
+                              NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
+               chip->cmd_ctrl(mtd, NAND_CMD_NONE,
+                              NAND_NCE | NAND_CTRL_CHANGE);
+               return;
+
        case NAND_CMD_READ0:
                chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
                               NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
@@ -646,7 +656,7 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
 
 /**
  * nand_get_device - [GENERIC] Get chip for selected access
- * @this:      the nand chip descriptor
+ * @chip:      the nand chip descriptor
  * @mtd:       MTD device structure
  * @new_state: the state which is requested
  *
@@ -686,19 +696,17 @@ nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
 /**
  * nand_wait - [DEFAULT]  wait until the command is done
  * @mtd:       MTD device structure
- * @this:      NAND chip structure
- * @state:     state to select the max. timeout value
+ * @chip:      NAND chip structure
  *
  * Wait for command done. This applies to erase and program only
  * Erase can take up to 400ms and program up to 20ms according to
  * general NAND and SmartMedia specs
- *
-*/
-static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
+ */
+static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
 {
 
        unsigned long timeo = jiffies;
-       int status;
+       int status, state = chip->state;
 
        if (state == FL_ERASING)
                timeo += (HZ * 400) / 1000;
@@ -717,10 +725,6 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
                chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
 
        while (time_before(jiffies, timeo)) {
-               /* Check, if we were interrupted */
-               if (chip->state != state)
-                       return 0;
-
                if (chip->dev_ready) {
                        if (chip->dev_ready(mtd))
                                break;
@@ -737,7 +741,21 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
 }
 
 /**
- * nand_read_page_swecc - {REPLACABLE] software ecc based page read function
+ * 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
  * @buf:       buffer to store read data
@@ -749,15 +767,11 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
        int eccbytes = chip->ecc.bytes;
        int eccsteps = chip->ecc.steps;
        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);
+       uint8_t *ecc_calc = chip->buffers->ecccalc;
+       uint8_t *ecc_code = chip->buffers->ecccode;
+       int *eccpos = chip->ecc.layout->eccpos;
 
-       if (chip->ecc.mode == NAND_ECC_NONE)
-               return 0;
+       chip->ecc.read_page_raw(mtd, chip, buf);
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
                chip->ecc.calculate(mtd, p, &ecc_calc[i]);
@@ -781,7 +795,7 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
 }
 
 /**
- * nand_read_page_hwecc - {REPLACABLE] hardware ecc based page read function
+ * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
  * @buf:       buffer to store read data
@@ -795,9 +809,9 @@ static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
        int eccbytes = chip->ecc.bytes;
        int eccsteps = chip->ecc.steps;
        uint8_t *p = buf;
-       uint8_t *ecc_calc = chip->buffers.ecccalc;
-       uint8_t *ecc_code = chip->buffers.ecccode;
-       int *eccpos = chip->autooob->eccpos;
+       uint8_t *ecc_calc = chip->buffers->ecccalc;
+       uint8_t *ecc_code = chip->buffers->ecccode;
+       int *eccpos = chip->ecc.layout->eccpos;
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
                chip->ecc.hwctl(mtd, NAND_ECC_READ);
@@ -825,7 +839,7 @@ static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
 }
 
 /**
- * nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
+ * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
  * @buf:       buffer to store read data
@@ -871,7 +885,7 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
        }
 
        /* Calculate remaining oob bytes */
-       i = oob - chip->oob_poi;
+       i = mtd->oobsize - (oob - chip->oob_poi);
        if (i)
                chip->read_buf(mtd, oob, i);
 
@@ -879,18 +893,64 @@ 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
+ * @oob:       oob destination address
+ * @ops:       oob ops structure
+ * @len:       size of oob to transfer
+ */
+static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
+                                 struct mtd_oob_ops *ops, size_t len)
+{
+       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;
+               uint32_t boffs = 0, roffs = ops->ooboffs;
+               size_t bytes = 0;
+
+               for(; free->length && len; free++, len -= bytes) {
+                       /* Read request not from offset 0 ? */
+                       if (unlikely(roffs)) {
+                               if (roffs >= free->length) {
+                                       roffs -= free->length;
+                                       continue;
+                               }
+                               boffs = free->offset + roffs;
+                               bytes = min_t(size_t, len,
+                                             (free->length - roffs));
+                               roffs = 0;
+                       } else {
+                               bytes = min_t(size_t, len, free->length);
+                               boffs = free->offset;
+                       }
+                       memcpy(oob, chip->oob_poi + boffs, 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
+ * @ops:       oob ops structure
  *
  * 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;
@@ -898,8 +958,9 @@ 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;
+       uint32_t oobreadlen = ops->ooblen;
+       uint8_t *bufpoi, *oob, *buf;
 
        stats = mtd->ecc_stats;
 
@@ -910,15 +971,17 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
        page = realpage & chip->pagemask;
 
        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) {
-                       bufpoi = aligned ? buf : chip->buffers.databuf;
+               if (realpage != chip->pagebuf || oob) {
+                       bufpoi = aligned ? buf : chip->buffers->databuf;
 
                        if (likely(sndcmd)) {
                                chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
@@ -926,14 +989,34 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
                        }
 
                        /* Now read the page into the buffer */
-                       ret = chip->ecc.read_page(mtd, chip, bufpoi);
+                       if (unlikely(ops->mode == MTD_OOB_RAW))
+                               ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
+                       else
+                               ret = chip->ecc.read_page(mtd, chip, bufpoi);
                        if (ret < 0)
                                break;
 
                        /* Transfer not aligned data */
                        if (!aligned) {
                                chip->pagebuf = realpage;
-                               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) {
+                                       int toread = min(oobreadlen,
+                                               chip->ecc.layout->oobavail);
+                                       if (toread) {
+                                               oob = nand_transfer_oob(chip,
+                                                       oob, ops, toread);
+                                               oobreadlen -= toread;
+                                       }
+                               } else
+                                       buf = nand_transfer_oob(chip,
+                                               buf, ops, mtd->oobsize);
                        }
 
                        if (!(chip->options & NAND_NO_READRDY)) {
@@ -949,10 +1032,11 @@ int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
                                else
                                        nand_wait_ready(mtd);
                        }
-               } else
-                       memcpy(buf, chip->buffers.databuf + col, bytes);
+               } else {
+                       memcpy(buf, chip->buffers->databuf + col, bytes);
+                       buf += bytes;
+               }
 
-               buf += bytes;
                readlen -= bytes;
 
                if (!readlen)
@@ -978,12 +1062,17 @@ 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 (oob)
+               ops->oobretlen = ops->ooblen - oobreadlen;
 
        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;
 }
 
 /**
@@ -999,18 +1088,24 @@ 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);
+
+       *retlen = chip->ops.retlen;
 
        nand_release_device(mtd);
 
@@ -1018,37 +1113,184 @@ static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
 }
 
 /**
- * nand_read_oob - [MTD Interface] NAND read out-of-band
+ * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @page:      page number to read
+ * @sndcmd:    flag whether to issue read command or not
+ */
+static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
+                            int page, int sndcmd)
+{
+       if (sndcmd) {
+               chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
+               sndcmd = 0;
+       }
+       chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
+       return sndcmd;
+}
+
+/**
+ * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
+ *                         with syndromes
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @page:      page number to read
+ * @sndcmd:    flag whether to issue read command or not
+ */
+static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
+                                 int page, int sndcmd)
+{
+       uint8_t *buf = chip->oob_poi;
+       int length = mtd->oobsize;
+       int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
+       int eccsize = chip->ecc.size;
+       uint8_t *bufpoi = buf;
+       int i, toread, sndrnd = 0, pos;
+
+       chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
+       for (i = 0; i < chip->ecc.steps; i++) {
+               if (sndrnd) {
+                       pos = eccsize + i * (eccsize + chunk);
+                       if (mtd->writesize > 512)
+                               chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
+                       else
+                               chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
+               } else
+                       sndrnd = 1;
+               toread = min_t(int, length, chunk);
+               chip->read_buf(mtd, bufpoi, toread);
+               bufpoi += toread;
+               length -= toread;
+       }
+       if (length > 0)
+               chip->read_buf(mtd, bufpoi, length);
+
+       return 1;
+}
+
+/**
+ * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @page:      page number to write
+ */
+static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
+                             int page)
+{
+       int status = 0;
+       const uint8_t *buf = chip->oob_poi;
+       int length = mtd->oobsize;
+
+       chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
+       chip->write_buf(mtd, buf, length);
+       /* Send command to program the OOB data */
+       chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
+
+       status = chip->waitfunc(mtd, chip);
+
+       return status & NAND_STATUS_FAIL ? -EIO : 0;
+}
+
+/**
+ * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
+ *                          with syndrome - only for large page flash !
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @page:      page number to write
+ */
+static int nand_write_oob_syndrome(struct mtd_info *mtd,
+                                  struct nand_chip *chip, int page)
+{
+       int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
+       int eccsize = chip->ecc.size, length = mtd->oobsize;
+       int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
+       const uint8_t *bufpoi = chip->oob_poi;
+
+       /*
+        * data-ecc-data-ecc ... ecc-oob
+        * or
+        * data-pad-ecc-pad-data-pad .... ecc-pad-oob
+        */
+       if (!chip->ecc.prepad && !chip->ecc.postpad) {
+               pos = steps * (eccsize + chunk);
+               steps = 0;
+       } else
+               pos = eccsize;
+
+       chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
+       for (i = 0; i < steps; i++) {
+               if (sndcmd) {
+                       if (mtd->writesize <= 512) {
+                               uint32_t fill = 0xFFFFFFFF;
+
+                               len = eccsize;
+                               while (len > 0) {
+                                       int num = min_t(int, len, 4);
+                                       chip->write_buf(mtd, (uint8_t *)&fill,
+                                                       num);
+                                       len -= num;
+                               }
+                       } else {
+                               pos = eccsize + i * (eccsize + chunk);
+                               chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
+                       }
+               } else
+                       sndcmd = 1;
+               len = min_t(int, length, chunk);
+               chip->write_buf(mtd, bufpoi, len);
+               bufpoi += len;
+               length -= len;
+       }
+       if (length > 0)
+               chip->write_buf(mtd, bufpoi, length);
+
+       chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
+       status = chip->waitfunc(mtd, chip);
+
+       return status & NAND_STATUS_FAIL ? -EIO : 0;
+}
+
+/**
+ * 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;
+       int 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 readlen = ops->ooblen;
+       int len;
+       uint8_t *buf = ops->oobbuf;
 
-       DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
-             (unsigned int)from, (int)len);
+       DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
+             (unsigned long long)from, readlen);
 
-       /* Initialize return length value */
-       *retlen = 0;
+       if (ops->mode == MTD_OOB_AUTO)
+               len = chip->ecc.layout->oobavail;
+       else
+               len = mtd->oobsize;
 
-       /* Do not allow reads past end of device */
-       if ((from + len) > mtd->size) {
+       if (unlikely(ops->ooboffs >= len)) {
                DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
-                     "Attempt read beyond end of device\n");
+                       "Attempt to start read outside oob\n");
                return -EINVAL;
        }
 
-       nand_get_device(chip, mtd, FL_READING);
+       /* Do not allow reads past end of device */
+       if (unlikely(from >= mtd->size ||
+                    ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
+                                       (from >> chip->page_shift)) * len)) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
+                       "Attempt read beyond end of device\n");
+               return -EINVAL;
+       }
 
        chipnr = (int)(from >> chip->chip_shift);
        chip->select_chip(mtd, chipnr);
@@ -1057,22 +1299,11 @@ 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);
-
        while(1) {
-               int bytes = min((int)(mtd->oobsize - col), readlen);
+               sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
 
-               if (likely(sndcmd)) {
-                       chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
-                       sndcmd = 0;
-               }
-
-               chip->read_buf(mtd, buf, bytes);
-
-               readlen -= bytes;
-               if (!readlen)
-                       break;
+               len = min(len, readlen);
+               buf = nand_transfer_oob(chip, buf, ops, len);
 
                if (!(chip->options & NAND_NO_READRDY)) {
                        /*
@@ -1087,9 +1318,9 @@ 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;
+               readlen -= len;
+               if (!readlen)
+                       break;
 
                /* Increment page address */
                realpage++;
@@ -1109,85 +1340,71 @@ 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->oobretlen = ops->ooblen;
        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)
 {
        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 (ops->datbuf && (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;
-
-       while (len) {
-               if (likely(sndcmd)) {
-                       chip->cmdfunc(mtd, NAND_CMD_READ0, 0,
-                                     page & chip->pagemask);
-                       sndcmd = 0;
-               }
+       switch(ops->mode) {
+       case MTD_OOB_PLACE:
+       case MTD_OOB_AUTO:
+       case MTD_OOB_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);
-               }
+ 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);
 }
 
 /**
- * nand_write_page_swecc - {REPLACABLE] software ecc based page write function
+ * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
  * @buf:       data buffer
@@ -1198,25 +1415,22 @@ static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
        int i, eccsize = chip->ecc.size;
        int eccbytes = chip->ecc.bytes;
        int eccsteps = chip->ecc.steps;
-       uint8_t *ecc_calc = chip->buffers.ecccalc;
+       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);
+       chip->ecc.write_page_raw(mtd, chip, buf);
 }
 
 /**
- * nand_write_page_hwecc - {REPLACABLE] hardware ecc based page write function
+ * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
  * @buf:       data buffer
@@ -1227,13 +1441,13 @@ static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
        int i, eccsize = chip->ecc.size;
        int eccbytes = chip->ecc.bytes;
        int eccsteps = chip->ecc.steps;
-       uint8_t *ecc_calc = chip->buffers.ecccalc;
+       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);
-               chip->write_buf(mtd, p, mtd->writesize);
+               chip->write_buf(mtd, p, eccsize);
                chip->ecc.calculate(mtd, p, &ecc_calc[i]);
        }
 
@@ -1244,7 +1458,7 @@ static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
 }
 
 /**
- * nand_write_page_syndrome - {REPLACABLE] hardware ecc syndrom based page write
+ * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
  * @buf:       data buffer
@@ -1282,27 +1496,31 @@ static void nand_write_page_syndrome(struct mtd_info *mtd,
        }
 
        /* Calculate remaining oob bytes */
-       i = oob - chip->oob_poi;
+       i = mtd->oobsize - (oob - chip->oob_poi);
        if (i)
                chip->write_buf(mtd, oob, i);
 }
 
 /**
- * nand_write_page - [INTERNAL] write one page
+ * nand_write_page - [REPLACEABLE] write one page
  * @mtd:       MTD device structure
  * @chip:      NAND chip descriptor
  * @buf:       the data to write
  * @page:      page number to write
  * @cached:    cached programming
+ * @raw:       use _raw version of write_page
  */
 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
-                          const uint8_t *buf, int page, int cached)
+                          const uint8_t *buf, int page, int cached, int raw)
 {
        int status;
 
        chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
 
-       chip->ecc.write_page(mtd, chip, buf);
+       if (unlikely(raw))
+               chip->ecc.write_page_raw(mtd, chip, buf);
+       else
+               chip->ecc.write_page(mtd, chip, buf);
 
        /*
         * Cached progamming disabled for now, Not sure if its worth the
@@ -1313,7 +1531,7 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
        if (!cached || !(chip->options & NAND_CACHEPRG)) {
 
                chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
-               status = chip->waitfunc(mtd, chip, FL_WRITING);
+               status = chip->waitfunc(mtd, chip);
                /*
                 * See if operation failed and additional status checks are
                 * available
@@ -1326,7 +1544,7 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
                        return -EIO;
        } else {
                chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
-               status = chip->waitfunc(mtd, chip, FL_WRITING);
+               status = chip->waitfunc(mtd, chip);
        }
 
 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
@@ -1339,70 +1557,132 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
        return 0;
 }
 
-#define NOTALIGNED(x) (x & (mtd->writesize-1)) != 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;
+               uint32_t boffs = 0, woffs = ops->ooboffs;
+               size_t bytes = 0;
+
+               for(; free->length && len; free++, len -= bytes) {
+                       /* Write request not from offset 0 ? */
+                       if (unlikely(woffs)) {
+                               if (woffs >= free->length) {
+                                       woffs -= free->length;
+                                       continue;
+                               }
+                               boffs = free->offset + woffs;
+                               bytes = min_t(size_t, len,
+                                             (free->length - woffs));
+                               woffs = 0;
+                       } else {
+                               bytes = min_t(size_t, len, free->length);
+                               boffs = free->offset;
+                       }
+                       memcpy(chip->oob_poi + boffs, oob, bytes);
+                       oob += bytes;
+               }
+               return oob;
+       }
+       default:
+               BUG();
+       }
+       return NULL;
+}
+
+#define NOTALIGNED(x)  (x & (chip->subpagesize - 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;
+       int chipnr, realpage, page, blockmask, column;
        struct nand_chip *chip = mtd->priv;
-       uint32_t writelen = len;
-       int bytes = mtd->writesize;
-       int ret = -EIO;
-
-       *retlen = 0;
+       uint32_t writelen = ops->len;
+       uint8_t *oob = ops->oobbuf;
+       uint8_t *buf = ops->datbuf;
+       int ret, subpage;
 
-       /* 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;
+       if (!writelen)
+               return 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)
-               return 0;
-
-       nand_get_device(chip, mtd, FL_WRITING);
+       column = to & (mtd->writesize - 1);
+       subpage = column || (writelen & (mtd->writesize - 1));
 
-       /* Check, if it is write protected */
-       if (nand_check_wp(mtd))
-               goto out;
+       if (subpage && oob)
+               return -EINVAL;
 
        chipnr = (int)(to >> chip->chip_shift);
        chip->select_chip(mtd, chipnr);
 
+       /* Check, if it is write protected */
+       if (nand_check_wp(mtd))
+               return -EIO;
+
        realpage = (int)(to >> chip->page_shift);
        page = realpage & chip->pagemask;
        blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
 
        /* 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;
+       /* If we're not given explicit OOB data, let it be 0xFF */
+       if (likely(!oob))
+               memset(chip->oob_poi, 0xff, mtd->oobsize);
 
        while(1) {
+               int bytes = mtd->writesize;
                int cached = writelen > bytes && page != blockmask;
+               uint8_t *wbuf = buf;
 
-               ret = nand_write_page(mtd, chip, buf, page, cached);
+               /* Partial page write ? */
+               if (unlikely(column || writelen < (mtd->writesize - 1))) {
+                       cached = 0;
+                       bytes = min_t(int, bytes - column, (int) writelen);
+                       chip->pagebuf = -1;
+                       memset(chip->buffers->databuf, 0xff, mtd->writesize);
+                       memcpy(&chip->buffers->databuf[column], buf, bytes);
+                       wbuf = chip->buffers->databuf;
+               }
+
+               if (unlikely(oob))
+                       oob = nand_fill_oob(chip, oob, ops);
+
+               ret = chip->write_page(mtd, chip, wbuf, page, cached,
+                                      (ops->mode == MTD_OOB_RAW));
                if (ret)
                        break;
 
@@ -1410,6 +1690,7 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
                if (!writelen)
                        break;
 
+               column = 0;
                buf += bytes;
                realpage++;
 
@@ -1421,93 +1702,94 @@ 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);
+
+       ops->retlen = ops->len - writelen;
+       if (unlikely(oob))
+               ops->oobretlen = ops->ooblen;
        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);
 
-       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);
+
+       *retlen = chip->ops.retlen;
 
-       /* Deselect and wake up anyone waiting on the device */
        nand_release_device(mtd);
-       return 0;
+
+       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, len;
        struct nand_chip *chip = mtd->priv;
 
        DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
-             (unsigned int)to, (int)len);
+             (unsigned int)to, (int)ops->ooblen);
 
-       /* Initialize return length value */
-       *retlen = 0;
+       if (ops->mode == MTD_OOB_AUTO)
+               len = chip->ecc.layout->oobavail;
+       else
+               len = mtd->oobsize;
 
        /* Do not allow write past end of page */
-       column = to & (mtd->oobsize - 1);
-       if ((column + len) > mtd->oobsize) {
+       if ((ops->ooboffs + ops->ooblen) > len) {
                DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
                      "Attempt to write past end of page\n");
                return -EINVAL;
        }
 
-       nand_get_device(chip, mtd, FL_WRITING);
+       if (unlikely(ops->ooboffs >= len)) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
+                       "Attempt to start write outside oob\n");
+               return -EINVAL;
+       }
+
+       /* Do not allow reads past end of device */
+       if (unlikely(to >= mtd->size ||
+                    ops->ooboffs + ops->ooblen >
+                       ((mtd->size >> chip->page_shift) -
+                        (to >> chip->page_shift)) * len)) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
+                       "Attempt write beyond end of device\n");
+               return -EINVAL;
+       }
 
        chipnr = (int)(to >> chip->chip_shift);
        chip->select_chip(mtd, chipnr);
@@ -1525,56 +1807,65 @@ 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)) {
-               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));
-       } else {
-               chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column,
-                             page & chip->pagemask);
-               chip->write_buf(mtd, buf, len);
-       }
-       /* Send command to program the OOB data */
-       chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
+       memset(chip->oob_poi, 0xff, mtd->oobsize);
+       nand_fill_oob(chip, ops->oobbuf, ops);
+       status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
+       memset(chip->oob_poi, 0xff, mtd->oobsize);
 
-       status = chip->waitfunc(mtd, chip, FL_WRITING);
+       if (status)
+               return status;
 
-       /* See if device thinks it succeeded */
-       if (status & NAND_STATUS_FAIL) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
-                     "Failed write, page 0x%08x\n", page);
-               ret = -EIO;
-               goto out;
+       ops->oobretlen = ops->ooblen;
+
+       return 0;
+}
+
+/**
+ * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
+ * @mtd:       MTD device structure
+ * @to:                offset to write to
+ * @ops:       oob operation description structure
+ */
+static int nand_write_oob(struct mtd_info *mtd, loff_t to,
+                         struct mtd_oob_ops *ops)
+{
+       struct nand_chip *chip = mtd->priv;
+       int ret = -ENOTSUPP;
+
+       ops->retlen = 0;
+
+       /* Do not allow writes past end of device */
+       if (ops->datbuf && (to + ops->len) > mtd->size) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
+                     "Attempt read beyond end of device\n");
+               return -EINVAL;
        }
-       *retlen = len;
 
-#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
-       /* Send command to read back the data */
-       chip->cmdfunc(mtd, NAND_CMD_READOOB, column, page & chip->pagemask);
+       nand_get_device(chip, mtd, FL_WRITING);
 
-       if (chip->verify_buf(mtd, buf, len)) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
-                     "Failed write verify, page 0x%08x\n", page);
-               ret = -EIO;
+       switch(ops->mode) {
+       case MTD_OOB_PLACE:
+       case MTD_OOB_AUTO:
+       case MTD_OOB_RAW:
+               break;
+
+       default:
                goto out;
        }
-#endif
-       ret = 0;
+
+       if (!ops->datbuf)
+               ret = nand_do_write_oob(mtd, to, ops);
+       else
+               ret = nand_do_write_ops(mtd, to, ops);
+
  out:
-       /* Deselect and wake up anyone waiting on the device */
        nand_release_device(mtd);
-
        return ret;
 }
 
@@ -1723,7 +2014,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 
                chip->erase_cmd(mtd, page & chip->pagemask);
 
-               status = chip->waitfunc(mtd, chip, FL_ERASING);
+               status = chip->waitfunc(mtd, chip);
 
                /*
                 * See if operation failed and additional status checks are
@@ -1824,7 +2115,7 @@ static void nand_sync(struct mtd_info *mtd)
 /**
  * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  * @mtd:       MTD device structure
- * @ofs:       offset relative to mtd start
+ * @offs:      offset relative to mtd start
  */
 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
 {
@@ -1956,13 +2247,16 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
        if (!type)
                return ERR_PTR(-ENODEV);
 
-       chip->chipsize = nand_flash_ids[i].chipsize << 20;
+       if (!mtd->name)
+               mtd->name = type->name;
+
+       chip->chipsize = type->chipsize << 20;
 
        /* Newer devices have all the information in additional id bytes */
-       if (!nand_flash_ids[i].pagesize) {
+       if (!type->pagesize) {
                int extid;
-               /* The 3rd id byte contains non relevant data ATM */
-               extid = chip->read_byte(mtd);
+               /* The 3rd id byte holds MLC / multichip data */
+               chip->cellinfo = chip->read_byte(mtd);
                /* The 4th id byte is the important one */
                extid = chip->read_byte(mtd);
                /* Calc pagesize */
@@ -1981,14 +2275,14 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
                /*
                 * Old devices have chip data hardcoded in the device id table
                 */
-               mtd->erasesize = nand_flash_ids[i].erasesize;
-               mtd->writesize = nand_flash_ids[i].pagesize;
+               mtd->erasesize = type->erasesize;
+               mtd->writesize = type->pagesize;
                mtd->oobsize = mtd->writesize / 32;
-               busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
+               busw = type->options & NAND_BUSWIDTH_16;
        }
 
        /* Try to identify manufacturer */
-       for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_id++) {
+       for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
                if (nand_manuf_ids[maf_idx].id == *maf_id)
                        break;
        }
@@ -2022,7 +2316,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
        /* Get chip options, preserve non chip based options */
        chip->options &= ~NAND_CHIPOPTIONS_MSK;
-       chip->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
+       chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
 
        /*
         * Set chip as a default. Board drivers can override it, if necessary
@@ -2032,7 +2326,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
        /* Check if chip is a not a samsung device. Do not clear the
         * options for chips which are not having an extended id.
         */
-       if (*maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
+       if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
                chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
 
        /* Check for AND chips with 4 page planes */
@@ -2052,40 +2346,22 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
        return type;
 }
 
-/* module_text_address() isn't exported, and it's mostly a pointless
-   test if this is a module _anyway_ -- they'd have to try _really_ hard
-   to call us from in-kernel code if the core NAND support is modular. */
-#ifdef MODULE
-#define caller_is_module() (1)
-#else
-#define caller_is_module() \
-       module_text_address((unsigned long)__builtin_return_address(0))
-#endif
-
 /**
- * nand_scan - [NAND Interface] Scan for the NAND device
- * @mtd:       MTD device structure
- * @maxchips:  Number of chips to scan for
+ * nand_scan_ident - [NAND Interface] Scan for the NAND device
+ * @mtd:            MTD device structure
+ * @maxchips:       Number of chips to scan for
  *
- * This fills out all the uninitialized function pointers
- * with the defaults.
- * The flash ID is read and the mtd/chip structures are
- * filled with the appropriate values.
- * The mtd->owner field must be set to the module of the caller
+ * This is the first phase of the normal nand_scan() function. It
+ * reads the flash ID and sets up MTD fields accordingly.
  *
+ * The mtd->owner field must be set to the module of the caller.
  */
-int nand_scan(struct mtd_info *mtd, int maxchips)
+int nand_scan_ident(struct mtd_info *mtd, int maxchips)
 {
        int i, busw, nand_maf_id;
        struct nand_chip *chip = mtd->priv;
        struct nand_flash_dev *type;
 
-       /* Many callers got this wrong, so check for it for a while... */
-       if (!mtd->owner && caller_is_module()) {
-               printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
-               BUG();
-       }
-
        /* Get buswidth to select the correct functions */
        busw = chip->options & NAND_BUSWIDTH_16;
        /* Set the default functions */
@@ -2117,22 +2393,45 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        chip->numchips = i;
        mtd->size = i * chip->chipsize;
 
-       /* Preset the internal oob write buffer */
-       memset(chip->buffers.oobwbuf, 0xff, mtd->oobsize);
+       return 0;
+}
+
+
+/**
+ * nand_scan_tail - [NAND Interface] Scan for the NAND device
+ * @mtd:           MTD device structure
+ * @maxchips:      Number of chips to scan for
+ *
+ * This is the second phase of the normal nand_scan() function. It
+ * fills out all the uninitialized function pointers with the defaults
+ * and scans for a bad block table if appropriate.
+ */
+int nand_scan_tail(struct mtd_info *mtd)
+{
+       int i;
+       struct nand_chip *chip = mtd->priv;
+
+       if (!(chip->options & NAND_OWN_BUFFERS))
+               chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
+       if (!chip->buffers)
+               return -ENOMEM;
+
+       /* Set the internal oob buffer location, just after the page data */
+       chip->oob_poi = chip->buffers->databuf + mtd->writesize;
 
        /*
         * 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 "
@@ -2141,18 +2440,18 @@ 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];
+       if (!chip->write_page)
+               chip->write_page = nand_write_page;
 
        /*
         * check ECC mode, default to software if 3byte/512byte hardware ECC is
         * selected and we have 256 byte pagesize fallback to software ECC
         */
+       if (!chip->ecc.read_page_raw)
+               chip->ecc.read_page_raw = nand_read_page_raw;
+       if (!chip->ecc.write_page_raw)
+               chip->ecc.write_page_raw = nand_write_page_raw;
+
        switch (chip->ecc.mode) {
        case NAND_ECC_HW:
                /* Use standard hwecc read page function ? */
@@ -2160,6 +2459,10 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
                        chip->ecc.read_page = nand_read_page_hwecc;
                if (!chip->ecc.write_page)
                        chip->ecc.write_page = nand_write_page_hwecc;
+               if (!chip->ecc.read_oob)
+                       chip->ecc.read_oob = nand_read_oob_std;
+               if (!chip->ecc.write_oob)
+                       chip->ecc.write_oob = nand_write_oob_std;
 
        case NAND_ECC_HW_SYNDROME:
                if (!chip->ecc.calculate || !chip->ecc.correct ||
@@ -2173,6 +2476,10 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
                        chip->ecc.read_page = nand_read_page_syndrome;
                if (!chip->ecc.write_page)
                        chip->ecc.write_page = nand_write_page_syndrome;
+               if (!chip->ecc.read_oob)
+                       chip->ecc.read_oob = nand_read_oob_syndrome;
+               if (!chip->ecc.write_oob)
+                       chip->ecc.write_oob = nand_write_oob_syndrome;
 
                if (mtd->writesize >= chip->ecc.size)
                        break;
@@ -2186,6 +2493,8 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
                chip->ecc.correct = nand_correct_data;
                chip->ecc.read_page = nand_read_page_swecc;
                chip->ecc.write_page = nand_write_page_swecc;
+               chip->ecc.read_oob = nand_read_oob_std;
+               chip->ecc.write_oob = nand_write_oob_std;
                chip->ecc.size = 256;
                chip->ecc.bytes = 3;
                break;
@@ -2193,11 +2502,14 @@ 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.read_oob = nand_read_oob_std;
+               chip->ecc.write_oob = nand_write_oob_std;
                chip->ecc.size = mtd->writesize;
                chip->ecc.bytes = 0;
                break;
+
        default:
                printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
                       chip->ecc.mode);
@@ -2205,6 +2517,16 @@ 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;
+       mtd->oobavail = chip->ecc.layout->oobavail;
+
+       /*
         * Set the number of read / write steps for one page depending on ECC
         * mode
         */
@@ -2215,6 +2537,24 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        }
        chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
 
+       /*
+        * Allow subpage writes up to ecc.steps. Not possible for MLC
+        * FLASH.
+        */
+       if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
+           !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
+               switch(chip->ecc.steps) {
+               case 2:
+                       mtd->subpage_sft = 1;
+                       break;
+               case 4:
+               case 8:
+                       mtd->subpage_sft = 2;
+                       break;
+               }
+       }
+       chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
+
        /* Initialize state */
        chip->state = FL_READY;
 
@@ -2227,7 +2567,6 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        /* Fill in remaining MTD driver data */
        mtd->type = MTD_NANDFLASH;
        mtd->flags = MTD_CAP_NANDFLASH;
-       mtd->ecctype = MTD_ECC_SW;
        mtd->erase = nand_erase;
        mtd->point = NULL;
        mtd->unpoint = NULL;
@@ -2243,8 +2582,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)
@@ -2254,6 +2593,44 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        return chip->scan_bbt(mtd);
 }
 
+/* module_text_address() isn't exported, and it's mostly a pointless
+   test if this is a module _anyway_ -- they'd have to try _really_ hard
+   to call us from in-kernel code if the core NAND support is modular. */
+#ifdef MODULE
+#define caller_is_module() (1)
+#else
+#define caller_is_module() \
+       module_text_address((unsigned long)__builtin_return_address(0))
+#endif
+
+/**
+ * nand_scan - [NAND Interface] Scan for the NAND device
+ * @mtd:       MTD device structure
+ * @maxchips:  Number of chips to scan for
+ *
+ * This fills out all the uninitialized function pointers
+ * with the defaults.
+ * The flash ID is read and the mtd/chip structures are
+ * filled with the appropriate values.
+ * The mtd->owner field must be set to the module of the caller
+ *
+ */
+int nand_scan(struct mtd_info *mtd, int maxchips)
+{
+       int ret;
+
+       /* Many callers got this wrong, so check for it for a while... */
+       if (!mtd->owner && caller_is_module()) {
+               printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
+               BUG();
+       }
+
+       ret = nand_scan_ident(mtd, maxchips);
+       if (!ret)
+               ret = nand_scan_tail(mtd);
+       return ret;
+}
+
 /**
  * nand_release - [NAND Interface] Free resources held by the NAND device
  * @mtd:       MTD device structure
@@ -2271,9 +2648,13 @@ void nand_release(struct mtd_info *mtd)
 
        /* Free bad block table memory */
        kfree(chip->bbt);
+       if (!(chip->options & NAND_OWN_BUFFERS))
+               kfree(chip->buffers);
 }
 
 EXPORT_SYMBOL_GPL(nand_scan);
+EXPORT_SYMBOL_GPL(nand_scan_ident);
+EXPORT_SYMBOL_GPL(nand_scan_tail);
 EXPORT_SYMBOL_GPL(nand_release);
 
 static int __init nand_base_init(void)