[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 4f387c8..ea6d2c3 100644 (file)
  *     http://www.linux-mtd.infradead.org/tech/nand.html
  *
  *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
- *               2002 Thomas Gleixner (tglx@linutronix.de)
+ *               2002-2006 Thomas Gleixner (tglx@linutronix.de)
  *
- *  02-08-2004  tglx: support for strange chips, which cannot auto increment
- *             pages on read / read_oob
- *
- *  03-17-2004  tglx: Check ready before auto increment check. Simon Bayes
- *             pointed this out, as he marked an auto increment capable chip
- *             as NOAUTOINCR in the board driver.
- *             Make reads over block boundaries work too
- *
- *  04-14-2004 tglx: first working version for 2k page size chips
- *
- *  05-19-2004  tglx: Basic support for Renesas AG-AND chips
- *
- *  09-24-2004  tglx: add support for hardware controllers (e.g. ECC) shared
- *             among multiple independend devices. Suggestions and initial
- *             patch from Ben Dooks <ben-mtd@fluff.org>
- *
- *  12-05-2004 dmarlin: add workaround for Renesas AG-AND chips "disturb"
- *             issue. Basically, any block not rewritten may lose data when
- *             surrounding blocks are rewritten many times.  JFFS2 ensures
- *             this doesn't happen for blocks it uses, but the Bad Block
- *             Table(s) may not be rewritten.  To ensure they do not lose
- *             data, force them to be rewritten when some of the surrounding
- *             blocks are erased.  Rather than tracking a specific nearby
- *             block (which could itself go bad), use a page address 'mask' to
- *             select several blocks in the same area, and rewrite the BBT
- *             when any of them are erased.
- *
- *  01-03-2005 dmarlin: added support for the device recovery command sequence
- *             for Renesas AG-AND chips.  If there was a sudden loss of power
- *             during an erase operation, a "device recovery" operation must
- *             be performed when power is restored to ensure correct
- *             operation.
- *
- *  01-20-2005 dmarlin: added support for optional hardware specific callback
- *             routine to perform extra error status checks on erase and write
- *             failures.  This required adding a wrapper function for
- *             nand_read_ecc.
- *
- * 08-20-2005  vwool: suspend/resume added
- *
- * Credits:
+ *  Credits:
  *     David Woodhouse for adding multichip support
  *
  *     Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  *     rework for 2K page size chips
  *
- * TODO:
+ *  TODO:
  *     Enable cached programming for 2k page size chips
  *     Check, if mtd->ecctype should be set to MTD_ECC_HW
  *     if we have HW ecc support.
  *     The AG-AND chips have nice features for speed improvement,
  *     which are not supported yet. Read / program 4 pages in one go.
  *
- * $Id: nand_base.c,v 1.150 2005/09/15 13:58:48 vwool Exp $
- *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
 #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_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);
 
 /*
- * NAND low-level MTD interface functions
+ * For devices which display every fart in the system on a seperate LED. Is
+ * compiled away when LED support is disabled.
  */
-static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
-static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len);
-static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
-
-static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
-                    size_t *retlen, uint8_t *buf);
-static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
-                        size_t *retlen, uint8_t *buf);
-static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
-                     size_t *retlen, const uint8_t *buf);
-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_erase(struct mtd_info *mtd, struct erase_info *instr);
-static void nand_sync(struct mtd_info *mtd);
-
-/* Some internal functions */
-static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this,
-                          int page, uint8_t * oob_buf,
-                          struct nand_oobinfo *oobsel, int mode);
-#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
-static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this,
-                            int page, int numpages, uint8_t *oob_buf,
-                            struct nand_oobinfo *oobsel, int chipnr,
-                            int oobmode);
-#else
-#define nand_verify_pages(...) (0)
-#endif
-
-static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd,
-                          int new_state);
+DEFINE_LED_TRIGGER(nand_led_trigger);
 
 /**
  * nand_release_device - [GENERIC] release chip
@@ -172,17 +101,17 @@ static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd,
  */
 static void nand_release_device(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
        /* De-select the NAND device */
-       this->select_chip(mtd, -1);
+       chip->select_chip(mtd, -1);
 
        /* Release the controller and the chip */
-       spin_lock(&this->controller->lock);
-       this->controller->active = NULL;
-       this->state = FL_READY;
-       wake_up(&this->controller->wq);
-       spin_unlock(&this->controller->lock);
+       spin_lock(&chip->controller->lock);
+       chip->controller->active = NULL;
+       chip->state = FL_READY;
+       wake_up(&chip->controller->wq);
+       spin_unlock(&chip->controller->lock);
 }
 
 /**
@@ -193,8 +122,8 @@ static void nand_release_device(struct mtd_info *mtd)
  */
 static uint8_t nand_read_byte(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
-       return readb(this->IO_ADDR_R);
+       struct nand_chip *chip = mtd->priv;
+       return readb(chip->IO_ADDR_R);
 }
 
 /**
@@ -206,8 +135,8 @@ static uint8_t nand_read_byte(struct mtd_info *mtd)
  */
 static uint8_t nand_read_byte16(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
-       return (uint8_t) cpu_to_le16(readw(this->IO_ADDR_R));
+       struct nand_chip *chip = mtd->priv;
+       return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
 }
 
 /**
@@ -219,8 +148,8 @@ static uint8_t nand_read_byte16(struct mtd_info *mtd)
  */
 static u16 nand_read_word(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
-       return readw(this->IO_ADDR_R);
+       struct nand_chip *chip = mtd->priv;
+       return readw(chip->IO_ADDR_R);
 }
 
 /**
@@ -230,15 +159,15 @@ static u16 nand_read_word(struct mtd_info *mtd)
  *
  * Default select function for 1 chip devices.
  */
-static void nand_select_chip(struct mtd_info *mtd, int chip)
+static void nand_select_chip(struct mtd_info *mtd, int chipnr)
 {
-       struct nand_chip *this = mtd->priv;
-       switch (chip) {
+       struct nand_chip *chip = mtd->priv;
+
+       switch (chipnr) {
        case -1:
-               this->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
+               chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
                break;
        case 0:
-               this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
                break;
 
        default:
@@ -257,10 +186,10 @@ static void nand_select_chip(struct mtd_info *mtd, int chip)
 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
 {
        int i;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
        for (i = 0; i < len; i++)
-               writeb(buf[i], this->IO_ADDR_W);
+               writeb(buf[i], chip->IO_ADDR_W);
 }
 
 /**
@@ -274,10 +203,10 @@ static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
 {
        int i;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
        for (i = 0; i < len; i++)
-               buf[i] = readb(this->IO_ADDR_R);
+               buf[i] = readb(chip->IO_ADDR_R);
 }
 
 /**
@@ -291,12 +220,11 @@ static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
 {
        int i;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
        for (i = 0; i < len; i++)
-               if (buf[i] != readb(this->IO_ADDR_R))
+               if (buf[i] != readb(chip->IO_ADDR_R))
                        return -EFAULT;
-
        return 0;
 }
 
@@ -311,12 +239,12 @@ static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
 {
        int i;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        u16 *p = (u16 *) buf;
        len >>= 1;
 
        for (i = 0; i < len; i++)
-               writew(p[i], this->IO_ADDR_W);
+               writew(p[i], chip->IO_ADDR_W);
 
 }
 
@@ -331,12 +259,12 @@ static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
 {
        int i;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        u16 *p = (u16 *) buf;
        len >>= 1;
 
        for (i = 0; i < len; i++)
-               p[i] = readw(this->IO_ADDR_R);
+               p[i] = readw(chip->IO_ADDR_R);
 }
 
 /**
@@ -350,12 +278,12 @@ static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
 {
        int i;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        u16 *p = (u16 *) buf;
        len >>= 1;
 
        for (i = 0; i < len; i++)
-               if (p[i] != readw(this->IO_ADDR_R))
+               if (p[i] != readw(chip->IO_ADDR_R))
                        return -EFAULT;
 
        return 0;
@@ -372,40 +300,37 @@ static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
 {
        int page, chipnr, res = 0;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        u16 bad;
 
        if (getchip) {
-               page = (int)(ofs >> this->page_shift);
-               chipnr = (int)(ofs >> this->chip_shift);
+               page = (int)(ofs >> chip->page_shift);
+               chipnr = (int)(ofs >> chip->chip_shift);
 
-               /* Grab the lock and see if the device is available */
-               nand_get_device(this, mtd, FL_READING);
+               nand_get_device(chip, mtd, FL_READING);
 
                /* Select the NAND device */
-               this->select_chip(mtd, chipnr);
+               chip->select_chip(mtd, chipnr);
        } else
                page = (int)ofs;
 
-       if (this->options & NAND_BUSWIDTH_16) {
-               this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE,
-                             page & this->pagemask);
-               bad = cpu_to_le16(this->read_word(mtd));
-               if (this->badblockpos & 0x1)
+       if (chip->options & NAND_BUSWIDTH_16) {
+               chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
+                             page & chip->pagemask);
+               bad = cpu_to_le16(chip->read_word(mtd));
+               if (chip->badblockpos & 0x1)
                        bad >>= 8;
                if ((bad & 0xFF) != 0xff)
                        res = 1;
        } else {
-               this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos,
-                             page & this->pagemask);
-               if (this->read_byte(mtd) != 0xff)
+               chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
+                             page & chip->pagemask);
+               if (chip->read_byte(mtd) != 0xff)
                        res = 1;
        }
 
-       if (getchip) {
-               /* Deselect and wake up anyone waiting on the device */
+       if (getchip)
                nand_release_device(mtd);
-       }
 
        return res;
 }
@@ -420,23 +345,33 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
 */
 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 {
-       struct nand_chip *this = mtd->priv;
+       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) >> this->bbt_erase_shift;
-       if (this->bbt)
-               this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
+       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 (this->options & NAND_USE_FLASH_BBT)
-               return nand_update_bbt(mtd, ofs);
+       if (chip->options & NAND_USE_FLASH_BBT)
+               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 + (this->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;
 }
 
 /**
@@ -448,10 +383,10 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  */
 static int nand_check_wp(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        /* Check the WP bit */
-       this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
-       return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
+       chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
+       return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
 }
 
 /**
@@ -467,30 +402,28 @@ static int nand_check_wp(struct mtd_info *mtd)
 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
                               int allowbbt)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
-       if (!this->bbt)
-               return this->block_bad(mtd, ofs, getchip);
+       if (!chip->bbt)
+               return chip->block_bad(mtd, ofs, getchip);
 
        /* Return info from the table */
        return nand_isbad_bbt(mtd, ofs, allowbbt);
 }
 
-DEFINE_LED_TRIGGER(nand_led_trigger);
-
 /*
  * Wait for the ready pin, after a command
  * The timeout is catched later.
  */
 static void nand_wait_ready(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        unsigned long timeo = jiffies + 2;
 
        led_trigger_event(nand_led_trigger, LED_FULL);
        /* wait until command is processed or timeout occures */
        do {
-               if (this->dev_ready(mtd))
+               if (chip->dev_ready(mtd))
                        break;
                touch_softlockup_watchdog();
        } while (time_before(jiffies, timeo));
@@ -510,7 +443,7 @@ static void nand_wait_ready(struct mtd_info *mtd)
 static void nand_command(struct mtd_info *mtd, unsigned int command,
                         int column, int page_addr)
 {
-       register struct nand_chip *this = mtd->priv;
+       register struct nand_chip *chip = mtd->priv;
        int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
 
        /*
@@ -530,10 +463,10 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
                        column -= 256;
                        readcmd = NAND_CMD_READ1;
                }
-               this->cmd_ctrl(mtd, readcmd, ctrl);
+               chip->cmd_ctrl(mtd, readcmd, ctrl);
                ctrl &= ~NAND_CTRL_CHANGE;
        }
-       this->cmd_ctrl(mtd, command, ctrl);
+       chip->cmd_ctrl(mtd, command, ctrl);
 
        /*
         * Address cycle, when necessary
@@ -542,20 +475,20 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
        /* Serially input address */
        if (column != -1) {
                /* Adjust columns for 16 bit buswidth */
-               if (this->options & NAND_BUSWIDTH_16)
+               if (chip->options & NAND_BUSWIDTH_16)
                        column >>= 1;
-               this->cmd_ctrl(mtd, column, ctrl);
+               chip->cmd_ctrl(mtd, column, ctrl);
                ctrl &= ~NAND_CTRL_CHANGE;
        }
        if (page_addr != -1) {
-               this->cmd_ctrl(mtd, page_addr, ctrl);
+               chip->cmd_ctrl(mtd, page_addr, ctrl);
                ctrl &= ~NAND_CTRL_CHANGE;
-               this->cmd_ctrl(mtd, page_addr >> 8, ctrl);
+               chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
                /* One more address cycle for devices > 32MiB */
-               if (this->chipsize > (32 << 20))
-                       this->cmd_ctrl(mtd, page_addr >> 16, ctrl);
+               if (chip->chipsize > (32 << 20))
+                       chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
        }
-       this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
+       chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
 
        /*
         * program and erase have their own busy handlers
@@ -568,17 +501,18 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
        case NAND_CMD_ERASE2:
        case NAND_CMD_SEQIN:
        case NAND_CMD_STATUS:
-               this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
+               chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
                return;
 
        case NAND_CMD_RESET:
-               if (this->dev_ready)
+               if (chip->dev_ready)
                        break;
-               udelay(this->chip_delay);
-               this->cmd_ctrl(mtd, NAND_CMD_STATUS,
+               udelay(chip->chip_delay);
+               chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
                               NAND_CTRL_CLE | NAND_CTRL_CHANGE);
-               this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
-               while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
+               chip->cmd_ctrl(mtd,
+                              NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
+               while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
                return;
 
                /* This applies to read commands */
@@ -587,8 +521,8 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
                 * If we don't have access to the busy pin, we apply the given
                 * command delay
                 */
-               if (!this->dev_ready) {
-                       udelay(this->chip_delay);
+               if (!chip->dev_ready) {
+                       udelay(chip->chip_delay);
                        return;
                }
        }
@@ -614,7 +548,7 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                            int column, int page_addr)
 {
-       register struct nand_chip *this = mtd->priv;
+       register struct nand_chip *chip = mtd->priv;
 
        /* Emulate NAND_CMD_READOOB */
        if (command == NAND_CMD_READOOB) {
@@ -623,7 +557,7 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
        }
 
        /* Command latch cycle */
-       this->cmd_ctrl(mtd, command & 0xff,
+       chip->cmd_ctrl(mtd, command & 0xff,
                       NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
 
        if (column != -1 || page_addr != -1) {
@@ -632,23 +566,23 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                /* Serially input address */
                if (column != -1) {
                        /* Adjust columns for 16 bit buswidth */
-                       if (this->options & NAND_BUSWIDTH_16)
+                       if (chip->options & NAND_BUSWIDTH_16)
                                column >>= 1;
-                       this->cmd_ctrl(mtd, column, ctrl);
+                       chip->cmd_ctrl(mtd, column, ctrl);
                        ctrl &= ~NAND_CTRL_CHANGE;
-                       this->cmd_ctrl(mtd, column >> 8, ctrl);
+                       chip->cmd_ctrl(mtd, column >> 8, ctrl);
                }
                if (page_addr != -1) {
-                       this->cmd_ctrl(mtd, page_addr, ctrl);
-                       this->cmd_ctrl(mtd, page_addr >> 8,
+                       chip->cmd_ctrl(mtd, page_addr, ctrl);
+                       chip->cmd_ctrl(mtd, page_addr >> 8,
                                       NAND_NCE | NAND_ALE);
                        /* One more address cycle for devices > 128MiB */
-                       if (this->chipsize > (128 << 20))
-                               this->cmd_ctrl(mtd, page_addr >> 16,
+                       if (chip->chipsize > (128 << 20))
+                               chip->cmd_ctrl(mtd, page_addr >> 16,
                                               NAND_NCE | NAND_ALE);
                }
        }
-       this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
+       chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
 
        /*
         * program and erase have their own busy handlers
@@ -673,21 +607,25 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
        case NAND_CMD_STATUS_ERROR1:
        case NAND_CMD_STATUS_ERROR2:
        case NAND_CMD_STATUS_ERROR3:
-               udelay(this->chip_delay);
+               udelay(chip->chip_delay);
                return;
 
        case NAND_CMD_RESET:
-               if (this->dev_ready)
+               if (chip->dev_ready)
                        break;
-               udelay(this->chip_delay);
-               this->cmd_ctrl(mtd, NAND_CMD_STATUS, NAND_NCE | NAND_CLE);
-               this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
-               while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
+               udelay(chip->chip_delay);
+               chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
+                              NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
+               chip->cmd_ctrl(mtd, NAND_CMD_NONE,
+                              NAND_NCE | NAND_CTRL_CHANGE);
+               while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
                return;
 
        case NAND_CMD_READ0:
-               this->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_NCE | NAND_CLE);
-               this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
+               chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
+                              NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
+               chip->cmd_ctrl(mtd, NAND_CMD_NONE,
+                              NAND_NCE | NAND_CTRL_CHANGE);
 
                /* This applies to read commands */
        default:
@@ -695,8 +633,8 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                 * If we don't have access to the busy pin, we apply the given
                 * command delay
                 */
-               if (!this->dev_ready) {
-                       udelay(this->chip_delay);
+               if (!chip->dev_ready) {
+                       udelay(chip->chip_delay);
                        return;
                }
        }
@@ -717,27 +655,27 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  * Get the device and lock it for exclusive access
  */
 static int
-nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state)
+nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
 {
-       spinlock_t *lock = &this->controller->lock;
-       wait_queue_head_t *wq = &this->controller->wq;
+       spinlock_t *lock = &chip->controller->lock;
+       wait_queue_head_t *wq = &chip->controller->wq;
        DECLARE_WAITQUEUE(wait, current);
  retry:
        spin_lock(lock);
 
        /* Hardware controller shared among independend devices */
        /* Hardware controller shared among independend devices */
-       if (!this->controller->active)
-               this->controller->active = this;
+       if (!chip->controller->active)
+               chip->controller->active = chip;
 
-       if (this->controller->active == this && this->state == FL_READY) {
-               this->state = new_state;
+       if (chip->controller->active == chip && chip->state == FL_READY) {
+               chip->state = new_state;
                spin_unlock(lock);
                return 0;
        }
        if (new_state == FL_PM_SUSPENDED) {
                spin_unlock(lock);
-               return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
+               return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
        }
        set_current_state(TASK_UNINTERRUPTIBLE);
        add_wait_queue(wq, &wait);
@@ -758,7 +696,7 @@ nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state)
  * general NAND and SmartMedia specs
  *
 */
-static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
+static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
 {
 
        unsigned long timeo = jiffies;
@@ -775,491 +713,307 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
         * any case on any machine. */
        ndelay(100);
 
-       if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
-               this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
+       if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
+               chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
        else
-               this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
+               chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
 
        while (time_before(jiffies, timeo)) {
                /* Check, if we were interrupted */
-               if (this->state != state)
+               if (chip->state != state)
                        return 0;
 
-               if (this->dev_ready) {
-                       if (this->dev_ready(mtd))
+               if (chip->dev_ready) {
+                       if (chip->dev_ready(mtd))
                                break;
                } else {
-                       if (this->read_byte(mtd) & NAND_STATUS_READY)
+                       if (chip->read_byte(mtd) & NAND_STATUS_READY)
                                break;
                }
                cond_resched();
        }
        led_trigger_event(nand_led_trigger, LED_OFF);
 
-       status = (int)this->read_byte(mtd);
+       status = (int)chip->read_byte(mtd);
        return status;
 }
 
 /**
- * nand_write_page - [GENERIC] write one page
- * @mtd:       MTD device structure
- * @this:      NAND chip structure
- * @page:      startpage inside the chip, must be called with (page & this->pagemask)
- * @oob_buf:   out of band data buffer
- * @oobsel:    out of band selecttion structre
- * @cached:    1 = enable cached programming if supported by chip
- *
- * Nand_page_program function is used for write and writev !
- * This function will always program a full page of data
- * If you call it with a non page aligned buffer, you're lost :)
- *
- * Cached programming is not supported yet.
+ * 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_write_page(struct mtd_info *mtd, struct nand_chip *this, int page,
-                          uint8_t *oob_buf, struct nand_oobinfo *oobsel, int cached)
+static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
+                             uint8_t *buf)
 {
-       int i, status;
-       uint8_t ecc_code[32];
-       int eccmode = oobsel->useecc ? this->ecc.mode : NAND_ECC_NONE;
-       int *oob_config = oobsel->eccpos;
-       int datidx = 0, eccidx = 0, eccsteps = this->ecc.steps;
-       int eccbytes = 0;
-
-       /* FIXME: Enable cached programming */
-       cached = 0;
-
-       /* Send command to begin auto page programming */
-       this->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
+       chip->read_buf(mtd, buf, mtd->writesize);
+       chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
+       return 0;
+}
 
-       /* Write out complete page of data, take care of eccmode */
-       switch (eccmode) {
-               /* No ecc, write all */
-       case NAND_ECC_NONE:
-               printk(KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
-               this->write_buf(mtd, this->data_poi, mtd->writesize);
-               break;
+/**
+ * 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
+ */
+static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
+                               uint8_t *buf)
+{
+       int i, eccsize = chip->ecc.size;
+       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->ecc.layout->eccpos;
 
-               /* Software ecc 3/256, write all */
-       case NAND_ECC_SOFT:
-               for (; eccsteps; eccsteps--) {
-                       this->ecc.calculate(mtd, &this->data_poi[datidx], ecc_code);
-                       for (i = 0; i < 3; i++, eccidx++)
-                               oob_buf[oob_config[eccidx]] = ecc_code[i];
-                       datidx += this->ecc.size;
-               }
-               this->write_buf(mtd, this->data_poi, mtd->writesize);
-               break;
-       default:
-               eccbytes = this->ecc.bytes;
-               for (; eccsteps; eccsteps--) {
-                       /* enable hardware ecc logic for write */
-                       this->ecc.hwctl(mtd, NAND_ECC_WRITE);
-                       this->write_buf(mtd, &this->data_poi[datidx], this->ecc.size);
-                       this->ecc.calculate(mtd, &this->data_poi[datidx], ecc_code);
-                       for (i = 0; i < eccbytes; i++, eccidx++)
-                               oob_buf[oob_config[eccidx]] = ecc_code[i];
-                       /* If the hardware ecc provides syndromes then
-                        * the ecc code must be written immidiately after
-                        * the data bytes (words) */
-                       if (this->options & NAND_HWECC_SYNDROME)
-                               this->write_buf(mtd, ecc_code, eccbytes);
-                       datidx += this->ecc.size;
-               }
-               break;
-       }
+       nand_read_page_raw(mtd, chip, buf);
 
-       /* Write out OOB data */
-       if (this->options & NAND_HWECC_SYNDROME)
-               this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
-       else
-               this->write_buf(mtd, oob_buf, mtd->oobsize);
+       for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
+               chip->ecc.calculate(mtd, p, &ecc_calc[i]);
 
-       /* Send command to actually program the data */
-       this->cmdfunc(mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
+       for (i = 0; i < chip->ecc.total; i++)
+               ecc_code[i] = chip->oob_poi[eccpos[i]];
 
-       if (!cached) {
-               /* call wait ready function */
-               status = this->waitfunc(mtd, this, FL_WRITING);
+       eccsteps = chip->ecc.steps;
+       p = buf;
 
-               /* See if operation failed and additional status checks are available */
-               if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
-                       status = this->errstat(mtd, this, FL_WRITING, status, page);
-               }
+       for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
+               int stat;
 
-               /* See if device thinks it succeeded */
-               if (status & NAND_STATUS_FAIL) {
-                       DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
-                       return -EIO;
-               }
-       } else {
-               /* FIXME: Implement cached programming ! */
-               /* wait until cache is ready */
-               // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
+               stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
+               if (stat == -1)
+                       mtd->ecc_stats.failed++;
+               else
+                       mtd->ecc_stats.corrected += stat;
        }
        return 0;
 }
 
-#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
 /**
- * nand_verify_pages - [GENERIC] verify the chip contents after a write
- * @mtd:       MTD device structure
- * @this:      NAND chip structure
- * @page:      startpage inside the chip, must be called with (page & this->pagemask)
- * @numpages:  number of pages to verify
- * @oob_buf:   out of band data buffer
- * @oobsel:    out of band selecttion structre
- * @chipnr:    number of the current chip
- * @oobmode:   1 = full buffer verify, 0 = ecc only
+ * 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
  *
- * The NAND device assumes that it is always writing to a cleanly erased page.
- * Hence, it performs its internal write verification only on bits that
- * transitioned from 1 to 0. The device does NOT verify the whole page on a
- * byte by byte basis. It is possible that the page was not completely erased
- * or the page is becoming unusable due to wear. The read with ECC would catch
- * the error later when the ECC page check fails, but we would rather catch
- * it early in the page write stage. Better to write no data than invalid data.
+ * Not for syndrome calculating ecc controllers which need a special oob layout
  */
-static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
-                            uint8_t *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
+static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
+                               uint8_t *buf)
 {
-       int i, j, datidx = 0, oobofs = 0, res = -EIO;
-       int eccsteps = this->eccsteps;
-       int hweccbytes;
-       uint8_t oobdata[64];
-
-       hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
-
-       /* Send command to read back the first page */
-       this->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
-
-       for (;;) {
-               for (j = 0; j < eccsteps; j++) {
-                       /* Loop through and verify the data */
-                       if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
-                               DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
-                               goto out;
-                       }
-                       datidx += mtd->eccsize;
-                       /* Have we a hw generator layout ? */
-                       if (!hweccbytes)
-                               continue;
-                       if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
-                               DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
-                               goto out;
-                       }
-                       oobofs += hweccbytes;
-               }
+       int i, eccsize = chip->ecc.size;
+       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->ecc.layout->eccpos;
+
+       for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
+               chip->ecc.hwctl(mtd, NAND_ECC_READ);
+               chip->read_buf(mtd, p, eccsize);
+               chip->ecc.calculate(mtd, p, &ecc_calc[i]);
+       }
+       chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
 
-               /* check, if we must compare all data or if we just have to
-                * compare the ecc bytes
-                */
-               if (oobmode) {
-                       if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
-                               DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
-                               goto out;
-                       }
-               } else {
-                       /* Read always, else autoincrement fails */
-                       this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
-
-                       if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
-                               int ecccnt = oobsel->eccbytes;
-
-                               for (i = 0; i < ecccnt; i++) {
-                                       int idx = oobsel->eccpos[i];
-                                       if (oobdata[idx] != oob_buf[oobofs + idx]) {
-                                               DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed ECC write verify, page 0x%08x, %6i bytes were succesful\n",
-                                                     __FUNCTION__, page, i);
-                                               goto out;
-                                       }
-                               }
-                       }
-               }
-               oobofs += mtd->oobsize - hweccbytes * eccsteps;
-               page++;
-               numpages--;
-
-               /* Apply delay or wait for ready/busy pin
-                * Do this before the AUTOINCR check, so no problems
-                * arise if a chip which does auto increment
-                * is marked as NOAUTOINCR by the board driver.
-                * Do this also before returning, so the chip is
-                * ready for the next command.
-                */
-               if (!this->dev_ready)
-                       udelay(this->chip_delay);
-               else
-                       nand_wait_ready(mtd);
+       for (i = 0; i < chip->ecc.total; i++)
+               ecc_code[i] = chip->oob_poi[eccpos[i]];
 
-               /* All done, return happy */
-               if (!numpages)
-                       return 0;
+       eccsteps = chip->ecc.steps;
+       p = buf;
+
+       for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
+               int stat;
 
-               /* Check, if the chip supports auto page increment */
-               if (!NAND_CANAUTOINCR(this))
-                       this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
+               stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
+               if (stat == -1)
+                       mtd->ecc_stats.failed++;
+               else
+                       mtd->ecc_stats.corrected += stat;
        }
-       /*
-        * Terminate the read command. We come here in case of an error
-        * So we must issue a reset command.
-        */
- out:
-       this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
-       return res;
+       return 0;
 }
-#endif
 
 /**
- * nand_read - [MTD Interface] MTD compability function for nand_do_read_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
+ * 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
  *
- * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL
- * and flags = 0xff
+ * The hw generator calculates the error syndrome automatically. Therefor
+ * we need a special oob layout and handling.
  */
-static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, uint8_t *buf)
+static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
+                                  uint8_t *buf)
 {
-       return nand_do_read_ecc(mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff);
-}
+       int i, eccsize = chip->ecc.size;
+       int eccbytes = chip->ecc.bytes;
+       int eccsteps = chip->ecc.steps;
+       uint8_t *p = buf;
+       uint8_t *oob = chip->oob_poi;
 
-/**
- * nand_do_read_ecc - [MTD Interface] 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
- * @oob_buf:   filesystem supplied oob data buffer (can be NULL)
- * @oobsel:    oob selection structure
- * @flags:     flag to indicate if nand_get_device/nand_release_device should be preformed
- *             and how many corrected error bits are acceptable:
- *               bits 0..7 - number of tolerable errors
- *               bit  8    - 0 == do not get/release chip, 1 == get/release chip
- *
- * NAND read with ECC
- */
-int nand_do_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
-                    size_t *retlen, uint8_t *buf, uint8_t *oob_buf, struct nand_oobinfo *oobsel, int flags)
-{
+       for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
+               int stat;
 
-       int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
-       int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
-       struct nand_chip *this = mtd->priv;
-       uint8_t *data_poi, *oob_data = oob_buf;
-       uint8_t ecc_calc[32];
-       uint8_t ecc_code[32];
-       int eccmode, eccsteps;
-       int *oob_config, datidx;
-       int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
-       int eccbytes;
-       int compareecc = 1;
-       int oobreadlen;
-
-       DEBUG(MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
+               chip->ecc.hwctl(mtd, NAND_ECC_READ);
+               chip->read_buf(mtd, p, eccsize);
 
-       /* Do not allow reads past end of device */
-       if ((from + len) > mtd->size) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
-               *retlen = 0;
-               return -EINVAL;
-       }
+               if (chip->ecc.prepad) {
+                       chip->read_buf(mtd, oob, chip->ecc.prepad);
+                       oob += chip->ecc.prepad;
+               }
 
-       /* Grab the lock and see if the device is available */
-       if (flags & NAND_GET_DEVICE)
-               nand_get_device(this, mtd, FL_READING);
+               chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
+               chip->read_buf(mtd, oob, eccbytes);
+               stat = chip->ecc.correct(mtd, p, oob, NULL);
 
-       /* Autoplace of oob data ? Use the default placement scheme */
-       if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
-               oobsel = this->autooob;
+               if (stat == -1)
+                       mtd->ecc_stats.failed++;
+               else
+                       mtd->ecc_stats.corrected += stat;
 
-       eccmode = oobsel->useecc ? this->ecc.mode : NAND_ECC_NONE;
-       oob_config = oobsel->eccpos;
+               oob += eccbytes;
 
-       /* Select the NAND device */
-       chipnr = (int)(from >> this->chip_shift);
-       this->select_chip(mtd, chipnr);
+               if (chip->ecc.postpad) {
+                       chip->read_buf(mtd, oob, chip->ecc.postpad);
+                       oob += chip->ecc.postpad;
+               }
+       }
 
-       /* First we calculate the starting page */
-       realpage = (int)(from >> this->page_shift);
-       page = realpage & this->pagemask;
+       /* Calculate remaining oob bytes */
+       i = oob - chip->oob_poi;
+       if (i)
+               chip->read_buf(mtd, oob, i);
 
-       /* Get raw starting column */
-       col = from & (mtd->writesize - 1);
+       return 0;
+}
 
-       end = mtd->writesize;
-       ecc = this->ecc.size;
-       eccbytes = this->ecc.bytes;
+/**
+ * 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;
 
-       if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
-               compareecc = 0;
+       switch(ops->mode) {
 
-       oobreadlen = mtd->oobsize;
-       if (this->options & NAND_HWECC_SYNDROME)
-               oobreadlen -= oobsel->eccbytes;
+       case MTD_OOB_PLACE:
+       case MTD_OOB_RAW:
+               memcpy(oob, chip->oob_poi + ops->ooboffs, len);
+               return oob + len;
 
-       /* Loop until all data read */
-       while (read < len) {
+       case MTD_OOB_AUTO: {
+               struct nand_oobfree *free = chip->ecc.layout->oobfree;
+               size_t bytes;
 
-               int aligned = (!col && (len - read) >= end);
-               /*
-                * If the read is not page aligned, we have to read into data buffer
-                * due to ecc, else we read into return buffer direct
-                */
-               if (aligned)
-                       data_poi = &buf[read];
-               else
-                       data_poi = this->data_buf;
+               for(; free->length && len; free++, len -= bytes) {
+                       bytes = min(len, free->length);
 
-               /* Check, if we have this page in the buffer
-                *
-                * FIXME: Make it work when we must provide oob data too,
-                * check the usage of data_buf oob field
-                */
-               if (realpage == this->pagebuf && !oob_buf) {
-                       /* aligned read ? */
-                       if (aligned)
-                               memcpy(data_poi, this->data_buf, end);
-                       goto readdata;
+                       memcpy(oob, chip->oob_poi + free->offset, bytes);
+                       oob += bytes;
                }
+               return oob;
+       }
+       default:
+               BUG();
+       }
+       return NULL;
+}
 
-               /* Check, if we must send the read command */
-               if (sndcmd) {
-                       this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
-                       sndcmd = 0;
-               }
+/**
+ * nand_do_read_ops - [Internal] Read data with ECC
+ *
+ * @mtd:       MTD device structure
+ * @from:      offset to read from
+ *
+ * Internal function. Called with chip held.
+ */
+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;
+       struct mtd_ecc_stats stats;
+       int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
+       int sndcmd = 1;
+       int ret = 0;
+       uint32_t readlen = ops->len;
+       uint8_t *bufpoi, *oob, *buf;
 
-               /* get oob area, if we have no oob buffer from fs-driver */
-               if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
-                       oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
-                       oob_data = &this->data_buf[end];
-
-               eccsteps = this->ecc.steps;
-
-               switch (eccmode) {
-               case NAND_ECC_NONE:{
-                               /* No ECC, Read in a page */
-                               static unsigned long lastwhinge = 0;
-                               if ((lastwhinge / HZ) != (jiffies / HZ)) {
-                                       printk(KERN_WARNING
-                                              "Reading data from NAND FLASH without ECC is not recommended\n");
-                                       lastwhinge = jiffies;
-                               }
-                               this->read_buf(mtd, data_poi, end);
-                               break;
-                       }
+       stats = mtd->ecc_stats;
 
-               case NAND_ECC_SOFT:     /* Software ECC 3/256: Read in a page + oob data */
-                       this->read_buf(mtd, data_poi, end);
-                       for (i = 0, datidx = 0; eccsteps; eccsteps--, i += 3, datidx += ecc)
-                               this->ecc.calculate(mtd, &data_poi[datidx], &ecc_calc[i]);
-                       break;
+       chipnr = (int)(from >> chip->chip_shift);
+       chip->select_chip(mtd, chipnr);
 
-               default:
-                       for (i = 0, datidx = 0; eccsteps; eccsteps--, i += eccbytes, datidx += ecc) {
-                               this->ecc.hwctl(mtd, NAND_ECC_READ);
-                               this->read_buf(mtd, &data_poi[datidx], ecc);
-
-                               /* HW ecc with syndrome calculation must read the
-                                * syndrome from flash immidiately after the data */
-                               if (!compareecc) {
-                                       /* Some hw ecc generators need to know when the
-                                        * syndrome is read from flash */
-                                       this->ecc.hwctl(mtd, NAND_ECC_READSYN);
-                                       this->read_buf(mtd, &oob_data[i], eccbytes);
-                                       /* We calc error correction directly, it checks the hw
-                                        * generator for an error, reads back the syndrome and
-                                        * does the error correction on the fly */
-                                       ecc_status = this->ecc.correct(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);
-                                       if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
-                                               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: "
-                                                     "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
-                                               ecc_failed++;
-                                       }
-                               } else {
-                                       this->ecc.calculate(mtd, &data_poi[datidx], &ecc_calc[i]);
-                               }
-                       }
-                       break;
-               }
+       realpage = (int)(from >> chip->page_shift);
+       page = realpage & chip->pagemask;
 
-               /* read oobdata */
-               this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
+       col = (int)(from & (mtd->writesize - 1));
+       chip->oob_poi = chip->buffers.oobrbuf;
 
-               /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
-               if (!compareecc)
-                       goto readoob;
+       buf = ops->datbuf;
+       oob = ops->oobbuf;
 
-               /* Pick the ECC bytes out of the oob data */
-               for (j = 0; j < oobsel->eccbytes; j++)
-                       ecc_code[j] = oob_data[oob_config[j]];
+       while(1) {
+               bytes = min(mtd->writesize - col, readlen);
+               aligned = (bytes == mtd->writesize);
 
-               /* correct data, if necessary */
-               for (i = 0, j = 0, datidx = 0; i < this->ecc.steps; i++, datidx += ecc) {
-                       ecc_status = this->ecc.correct(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
+               /* Is the current page in the buffer ? */
+               if (realpage != chip->pagebuf || oob) {
+                       bufpoi = aligned ? buf : chip->buffers.databuf;
 
-                       /* Get next chunk of ecc bytes */
-                       j += eccbytes;
+                       if (likely(sndcmd)) {
+                               chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
+                               sndcmd = 0;
+                       }
 
-                       /* Check, if we have a fs supplied oob-buffer,
-                        * This is the legacy mode. Used by YAFFS1
-                        * Should go away some day
-                        */
-                       if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
-                               int *p = (int *)(&oob_data[mtd->oobsize]);
-                               p[i] = ecc_status;
+                       /* Now read the page into the buffer */
+                       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);
                        }
 
-                       if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
-                               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
-                               ecc_failed++;
+                       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);
                        }
-               }
 
-             readoob:
-               /* check, if we have a fs supplied oob-buffer */
-               if (oob_buf) {
-                       /* without autoplace. Legacy mode used by YAFFS1 */
-                       switch (oobsel->useecc) {
-                       case MTD_NANDECC_AUTOPLACE:
-                       case MTD_NANDECC_AUTOPL_USR:
-                               /* Walk through the autoplace chunks */
-                               for (i = 0; oobsel->oobfree[i][1]; i++) {
-                                       int from = oobsel->oobfree[i][0];
-                                       int num = oobsel->oobfree[i][1];
-                                       memcpy(&oob_buf[oob], &oob_data[from], num);
-                                       oob += num;
-                               }
-                               break;
-                       case MTD_NANDECC_PLACE:
-                               /* YAFFS1 legacy mode */
-                               oob_data += this->ecc.steps * sizeof(int);
-                       default:
-                               oob_data += mtd->oobsize;
+                       if (!(chip->options & NAND_NO_READRDY)) {
+                               /*
+                                * Apply delay or wait for ready/busy pin. Do
+                                * this before the AUTOINCR check, so no
+                                * problems arise if a chip which does auto
+                                * increment is marked as NOAUTOINCR by the
+                                * board driver.
+                                */
+                               if (!chip->dev_ready)
+                                       udelay(chip->chip_delay);
+                               else
+                                       nand_wait_ready(mtd);
                        }
+               } else {
+                       memcpy(buf, chip->buffers.databuf + col, bytes);
+                       buf += bytes;
                }
-       readdata:
-               /* Partial page read, transfer data into fs buffer */
-               if (!aligned) {
-                       for (j = col; j < end && read < len; j++)
-                               buf[read++] = data_poi[j];
-                       this->pagebuf = realpage;
-               } else
-                       read += mtd->writesize;
-
-               /* Apply delay or wait for ready/busy pin
-                * Do this before the AUTOINCR check, so no problems
-                * arise if a chip which does auto increment
-                * is marked as NOAUTOINCR by the board driver.
-                */
-               if (!this->dev_ready)
-                       udelay(this->chip_delay);
-               else
-                       nand_wait_ready(mtd);
 
-               if (read == len)
+               readlen -= bytes;
+
+               if (!readlen)
                        break;
 
                /* For subsequent reads align to page boundary. */
@@ -1267,540 +1021,670 @@ int nand_do_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
                /* Increment page address */
                realpage++;
 
-               page = realpage & this->pagemask;
+               page = realpage & chip->pagemask;
                /* Check, if we cross a chip boundary */
                if (!page) {
                        chipnr++;
-                       this->select_chip(mtd, -1);
-                       this->select_chip(mtd, chipnr);
+                       chip->select_chip(mtd, -1);
+                       chip->select_chip(mtd, chipnr);
                }
+
                /* Check, if the chip supports auto page increment
                 * or if we have hit a block boundary.
                 */
-               if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
+               if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
                        sndcmd = 1;
        }
 
-       /* Deselect and wake up anyone waiting on the device */
-       if (flags & NAND_GET_DEVICE)
-               nand_release_device(mtd);
+       ops->retlen = ops->len - (size_t) readlen;
 
-       /*
-        * Return success, if no ECC failures, else -EBADMSG
-        * fs driver will take care of that, because
-        * retlen == desired len and result == -EBADMSG
-        */
-       *retlen = read;
-       return ecc_failed ? -EBADMSG : 0;
+       if (ret)
+               return ret;
+
+       if (mtd->ecc_stats.failed - stats.failed)
+               return -EBADMSG;
+
+       return  mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
 }
 
 /**
- * nand_read_oob - [MTD Interface] NAND read out-of-band
+ * nand_read - [MTD Interface] MTD compability function for nand_do_read_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
  *
+ * Get hold of the chip and call nand_do_read
+ */
+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;
+
+       /* Do not allow reads past end of device */
+       if ((from + len) > mtd->size)
+               return -EINVAL;
+       if (!len)
+               return 0;
+
+       nand_get_device(chip, mtd, FL_READING);
+
+       chip->ops.len = len;
+       chip->ops.datbuf = buf;
+       chip->ops.oobbuf = NULL;
+
+       ret = nand_do_read_ops(mtd, from, &chip->ops);
+
+       nand_release_device(mtd);
+
+       *retlen = chip->ops.retlen;
+       return ret;
+}
+
+/**
+ * nand_do_read_oob - [Intern] NAND read out-of-band
+ * @mtd:       MTD device structure
+ * @from:      offset to read from
+ * @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 i, col, page, chipnr;
-       struct nand_chip *this = mtd->priv;
-       int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
+       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 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);
+       DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
+             (unsigned int)from, (int)len);
+
+       chipnr = (int)(from >> chip->chip_shift);
+       chip->select_chip(mtd, chipnr);
 
        /* Shift to get page */
-       page = (int)(from >> this->page_shift);
-       chipnr = (int)(from >> this->chip_shift);
+       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;
+       }
 
-       /* Initialize return length value */
-       *retlen = 0;
+       while(1) {
+               bytes = direct ? ops->ooblen : mtd->oobsize;
+               bufpoi = direct ? buf : chip->buffers.oobrbuf;
 
-       /* 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");
-               *retlen = 0;
-               return -EINVAL;
-       }
+               if (likely(sndcmd)) {
+                       chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
+                       sndcmd = 0;
+               }
 
-       /* Grab the lock and see if the device is available */
-       nand_get_device(this, mtd, FL_READING);
+               chip->read_buf(mtd, bufpoi, bytes);
 
-       /* Select the NAND device */
-       this->select_chip(mtd, chipnr);
+               if (unlikely(!direct))
+                       buf = nand_transfer_oob(chip, buf, ops);
+               else
+                       buf += ops->ooblen;
 
-       /* Send the read command */
-       this->cmdfunc(mtd, NAND_CMD_READOOB, col, page & this->pagemask);
-       /*
-        * Read the data, if we read more than one page
-        * oob data, let the device transfer the data !
-        */
-       i = 0;
-       while (i < len) {
-               int thislen = mtd->oobsize - col;
-               thislen = min_t(int, thislen, len);
-               this->read_buf(mtd, &buf[i], thislen);
-               i += thislen;
-
-               /* Read more ? */
-               if (i < len) {
-                       page++;
-                       col = 0;
-
-                       /* Check, if we cross a chip boundary */
-                       if (!(page & this->pagemask)) {
-                               chipnr++;
-                               this->select_chip(mtd, -1);
-                               this->select_chip(mtd, chipnr);
-                       }
+               readlen -= ops->ooblen;
+               if (!readlen)
+                       break;
 
-                       /* Apply delay or wait for ready/busy pin
-                        * Do this before the AUTOINCR check, so no problems
-                        * arise if a chip which does auto increment
-                        * is marked as NOAUTOINCR by the board driver.
+               if (!(chip->options & NAND_NO_READRDY)) {
+                       /*
+                        * Apply delay or wait for ready/busy pin. Do this
+                        * before the AUTOINCR check, so no problems arise if a
+                        * chip which does auto increment is marked as
+                        * NOAUTOINCR by the board driver.
                         */
-                       if (!this->dev_ready)
-                               udelay(this->chip_delay);
+                       if (!chip->dev_ready)
+                               udelay(chip->chip_delay);
                        else
                                nand_wait_ready(mtd);
+               }
 
-                       /* Check, if the chip supports auto page increment
-                        * or if we have hit a block boundary.
-                        */
-                       if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
-                               /* For subsequent page reads set offset to 0 */
-                               this->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
-                       }
+               /* Increment page address */
+               realpage++;
+
+               page = realpage & chip->pagemask;
+               /* Check, if we cross a chip boundary */
+               if (!page) {
+                       chipnr++;
+                       chip->select_chip(mtd, -1);
+                       chip->select_chip(mtd, chipnr);
                }
-       }
 
-       /* Deselect and wake up anyone waiting on the device */
-       nand_release_device(mtd);
+               /* Check, if the chip supports auto page increment
+                * or if we have hit a block boundary.
+                */
+               if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
+                       sndcmd = 1;
+       }
 
-       /* Return happy */
-       *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)
 {
-       struct nand_chip *this = mtd->priv;
-       int page = (int)(from >> this->page_shift);
-       int chip = (int)(from >> this->chip_shift);
-       int sndcmd = 1;
-       int cnt = 0;
-       int pagesize = mtd->writesize + mtd->oobsize;
-       int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
+       int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
+                        uint8_t *buf) = NULL;
+       struct nand_chip *chip = mtd->priv;
+       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: Attempt read beyond end of device\n");
+       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(this, mtd, FL_READING);
+       nand_get_device(chip, mtd, FL_READING);
 
-       this->select_chip(mtd, chip);
+       switch(ops->mode) {
+       case MTD_OOB_PLACE:
+       case MTD_OOB_AUTO:
+               break;
 
-       /* Add requested oob length */
-       len += ooblen;
+       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;
 
-       while (len) {
-               if (sndcmd)
-                       this->cmdfunc(mtd, NAND_CMD_READ0, 0, page & this->pagemask);
-               sndcmd = 0;
+       default:
+               goto out;
+       }
 
-               this->read_buf(mtd, &buf[cnt], pagesize);
+       if (!ops->datbuf)
+               ret = nand_do_read_oob(mtd, from, ops);
+       else
+               ret = nand_do_read_ops(mtd, from, ops);
 
-               len -= pagesize;
-               cnt += pagesize;
-               page++;
+       if (unlikely(ops->mode == MTD_OOB_RAW))
+               chip->ecc.read_page = read_page;
+ out:
+       nand_release_device(mtd);
+       return ret;
+}
 
-               if (!this->dev_ready)
-                       udelay(this->chip_delay);
-               else
-                       nand_wait_ready(mtd);
 
-               /* Check, if the chip supports auto page increment */
-               if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
-                       sndcmd = 1;
+/**
+ * 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
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @buf:       data buffer
+ */
+static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
+                                 const uint8_t *buf)
+{
+       int i, eccsize = chip->ecc.size;
+       int eccbytes = chip->ecc.bytes;
+       int eccsteps = chip->ecc.steps;
+       uint8_t *ecc_calc = chip->buffers.ecccalc;
+       const uint8_t *p = buf;
+       int *eccpos = chip->ecc.layout->eccpos;
+
+       /* 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];
+
+       nand_write_page_raw(mtd, chip, buf);
+}
+
+/**
+ * nand_write_page_hwecc - {REPLACABLE] hardware ecc based page write function
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @buf:       data buffer
+ */
+static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
+                                 const uint8_t *buf)
+{
+       int i, eccsize = chip->ecc.size;
+       int eccbytes = chip->ecc.bytes;
+       int eccsteps = chip->ecc.steps;
+       uint8_t *ecc_calc = chip->buffers.ecccalc;
+       const uint8_t *p = buf;
+       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, eccsize);
+               chip->ecc.calculate(mtd, p, &ecc_calc[i]);
        }
 
-       /* Deselect and wake up anyone waiting on the device */
-       nand_release_device(mtd);
-       return 0;
+       for (i = 0; i < chip->ecc.total; i++)
+               chip->oob_poi[eccpos[i]] = ecc_calc[i];
+
+       chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
 }
 
 /**
- * nand_write_raw - [GENERIC] Write raw data including oob
- * @mtd:       MTD device structure
- * @buf:       source buffer
- * @to:                offset to write to
- * @len:       number of bytes to write
- * @buf:       source buffer
- * @oob:       oob buffer
+ * nand_write_page_syndrome - {REPLACABLE] hardware ecc syndrom based page write
+ * @mtd:       mtd info structure
+ * @chip:      nand chip info structure
+ * @buf:       data buffer
  *
- * Write raw data including oob
+ * The hw generator calculates the error syndrome automatically. Therefor
+ * we need a special oob layout and handling.
  */
-int nand_write_raw(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
-                  uint8_t *buf, uint8_t *oob)
+static void nand_write_page_syndrome(struct mtd_info *mtd,
+                                   struct nand_chip *chip, const uint8_t *buf)
 {
-       struct nand_chip *this = mtd->priv;
-       int page = (int)(to >> this->page_shift);
-       int chip = (int)(to >> this->chip_shift);
-       int ret;
+       int i, eccsize = chip->ecc.size;
+       int eccbytes = chip->ecc.bytes;
+       int eccsteps = chip->ecc.steps;
+       const uint8_t *p = buf;
+       uint8_t *oob = chip->oob_poi;
 
-       *retlen = 0;
+       for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
 
-       /* Do not allow writes past end of device */
-       if ((to + len) > mtd->size) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt write "
-                     "beyond end of device\n");
-               return -EINVAL;
-       }
+               chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
+               chip->write_buf(mtd, p, eccsize);
 
-       /* Grab the lock and see if the device is available */
-       nand_get_device(this, mtd, FL_WRITING);
+               if (chip->ecc.prepad) {
+                       chip->write_buf(mtd, oob, chip->ecc.prepad);
+                       oob += chip->ecc.prepad;
+               }
 
-       this->select_chip(mtd, chip);
-       this->data_poi = buf;
+               chip->ecc.calculate(mtd, p, oob);
+               chip->write_buf(mtd, oob, eccbytes);
+               oob += eccbytes;
 
-       while (len != *retlen) {
-               ret = nand_write_page(mtd, this, page, oob, &mtd->oobinfo, 0);
-               if (ret)
-                       return ret;
-               page++;
-               *retlen += mtd->writesize;
-               this->data_poi += mtd->writesize;
-               oob += mtd->oobsize;
+               if (chip->ecc.postpad) {
+                       chip->write_buf(mtd, oob, chip->ecc.postpad);
+                       oob += chip->ecc.postpad;
+               }
        }
 
-       /* Deselect and wake up anyone waiting on the device */
-       nand_release_device(mtd);
-       return 0;
+       /* Calculate remaining oob bytes */
+       i = oob - chip->oob_poi;
+       if (i)
+               chip->write_buf(mtd, oob, i);
 }
-EXPORT_SYMBOL_GPL(nand_write_raw);
 
 /**
- * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
+ * nand_write_page - [INTERNAL] write one page
  * @mtd:       MTD device structure
- * @fsbuf:     buffer given by fs driver
- * @oobsel:    out of band selection structre
- * @autoplace: 1 = place given buffer into the oob bytes
- * @numpages:  number of pages to prepare
- *
- * Return:
- * 1. Filesystem buffer available and autoplacement is off,
- *    return filesystem buffer
- * 2. No filesystem buffer or autoplace is off, return internal
- *    buffer
- * 3. Filesystem buffer is given and autoplace selected
- *    put data from fs buffer into internal buffer and
- *    retrun internal buffer
- *
- * Note: The internal buffer is filled with 0xff. This must
- * be done only once, when no autoplacement happens
- * Autoplacement sets the buffer dirty flag, which
- * forces the 0xff fill before using the buffer again.
- *
-*/
-static uint8_t *nand_prepare_oobbuf(struct mtd_info *mtd, uint8_t *fsbuf, struct nand_oobinfo *oobsel,
-                                  int autoplace, int numpages)
+ * @chip:      NAND chip descriptor
+ * @buf:       the data to write
+ * @page:      page number to write
+ * @cached:    cached programming
+ */
+static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
+                          const uint8_t *buf, int page, int cached)
 {
-       struct nand_chip *this = mtd->priv;
-       int i, len, ofs;
+       int status;
 
-       /* Zero copy fs supplied buffer */
-       if (fsbuf && !autoplace)
-               return fsbuf;
+       chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
 
-       /* Check, if the buffer must be filled with ff again */
-       if (this->oobdirty) {
-               memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
-               this->oobdirty = 0;
+       chip->ecc.write_page(mtd, chip, buf);
+
+       /*
+        * Cached progamming disabled for now, Not sure if its worth the
+        * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
+        */
+       cached = 0;
+
+       if (!cached || !(chip->options & NAND_CACHEPRG)) {
+
+               chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
+               status = chip->waitfunc(mtd, chip, FL_WRITING);
+               /*
+                * See if operation failed and additional status checks are
+                * available
+                */
+               if ((status & NAND_STATUS_FAIL) && (chip->errstat))
+                       status = chip->errstat(mtd, chip, FL_WRITING, status,
+                                              page);
+
+               if (status & NAND_STATUS_FAIL)
+                       return -EIO;
+       } else {
+               chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
+               status = chip->waitfunc(mtd, chip, FL_WRITING);
        }
 
-       /* If we have no autoplacement or no fs buffer use the internal one */
-       if (!autoplace || !fsbuf)
-               return this->oob_buf;
-
-       /* Walk through the pages and place the data */
-       this->oobdirty = 1;
-       ofs = 0;
-       while (numpages--) {
-               for (i = 0, len = 0; len < mtd->oobavail; i++) {
-                       int to = ofs + oobsel->oobfree[i][0];
-                       int num = oobsel->oobfree[i][1];
-                       memcpy(&this->oob_buf[to], fsbuf, num);
-                       len += num;
-                       fsbuf += num;
+#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
+       /* Send command to read back the data */
+       chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
+
+       if (chip->verify_buf(mtd, buf, mtd->writesize))
+               return -EIO;
+#endif
+       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;
                }
-               ofs += mtd->oobavail;
+               return oob;
        }
-       return this->oob_buf;
+       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 startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
-       int autoplace = 0, numpages, totalpages;
-       struct nand_chip *this = mtd->priv;
-       uint8_t *oobbuf, *bufstart, *eccbuf = NULL;
-       int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
-       struct nand_oobinfo *oobsel = &mtd->oobinfo;
-
-       DEBUG(MTD_DEBUG_LEVEL3, "nand_write: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
-
-       /* Initialize retlen, in case of early exit */
-       *retlen = 0;
+       int chipnr, realpage, page, blockmask;
+       struct nand_chip *chip = mtd->priv;
+       uint32_t writelen = ops->len;
+       uint8_t *oob = ops->oobbuf;
+       uint8_t *buf = ops->datbuf;
+       int bytes = mtd->writesize;
+       int ret;
 
-       /* 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)) {
-               printk(KERN_NOTICE "nand_write: Attempt to write not page aligned data\n");
+       if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
+               printk(KERN_NOTICE "nand_write: "
+                      "Attempt to write not page aligned data\n");
                return -EINVAL;
        }
 
-       /* Grab the lock and see if the device is available */
-       nand_get_device(this, mtd, FL_WRITING);
-
-       /* Calculate chipnr */
-       chipnr = (int)(to >> this->chip_shift);
-       /* Select the NAND device */
-       this->select_chip(mtd, chipnr);
+       if (!writelen)
+               return 0;
 
        /* Check, if it is write protected */
        if (nand_check_wp(mtd))
-               goto out;
+               return -EIO;
 
-       /* Autoplace of oob data ? Use the default placement scheme */
-       if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
-               oobsel = this->autooob;
-               autoplace = 1;
-       }
-       if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
-               autoplace = 1;
+       chipnr = (int)(to >> chip->chip_shift);
+       chip->select_chip(mtd, chipnr);
 
-       /* Setup variables and oob buffer */
-       totalpages = len >> this->page_shift;
-       page = (int)(to >> this->page_shift);
-       /* Invalidate the page cache, if we write to the cached page */
-       if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
-               this->pagebuf = -1;
-
-       /* Set it relative to chip */
-       page &= this->pagemask;
-       startpage = page;
-       /* Calc number of pages we can write in one go */
-       numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages);
-       oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
-       bufstart = (uint8_t *) buf;
-
-       /* Loop until all data is written */
-       while (written < len) {
-
-               this->data_poi = (uint8_t *) &buf[written];
-               /* Write one page. If this is the last page to write
-                * or the last page in this block, then use the
-                * real pageprogram command, else select cached programming
-                * if supported by the chip.
-                */
-               ret = nand_write_page(mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
-               if (ret) {
-                       DEBUG(MTD_DEBUG_LEVEL0, "nand_write: write_page failed %d\n", ret);
-                       goto out;
-               }
-               /* Next oob page */
-               oob += mtd->oobsize;
-               /* Update written bytes count */
-               written += mtd->writesize;
-               if (written == len)
-                       goto cmp;
+       realpage = (int)(to >> chip->page_shift);
+       page = realpage & chip->pagemask;
+       blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
 
-               /* Increment page address */
-               page++;
+       /* Invalidate the page cache, when we write to the cached page */
+       if (to <= (chip->pagebuf << chip->page_shift) &&
+           (chip->pagebuf << chip->page_shift) < (to + ops->len))
+               chip->pagebuf = -1;
 
-               /* Have we hit a block boundary ? Then we have to verify and
-                * if verify is ok, we have to setup the oob buffer for
-                * the next pages.
-                */
-               if (!(page & (ppblock - 1))) {
-                       int ofs;
-                       this->data_poi = bufstart;
-                       ret = nand_verify_pages(mtd, this, startpage, page - startpage,
-                                               oobbuf, oobsel, chipnr, (eccbuf != NULL));
-                       if (ret) {
-                               DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
-                               goto out;
-                       }
-                       *retlen = written;
-
-                       ofs = autoplace ? mtd->oobavail : mtd->oobsize;
-                       if (eccbuf)
-                               eccbuf += (page - startpage) * ofs;
-                       totalpages -= page - startpage;
-                       numpages = min(totalpages, ppblock);
-                       page &= this->pagemask;
-                       startpage = page;
-                       oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
-                       oob = 0;
-                       /* Check, if we cross a chip boundary */
-                       if (!page) {
-                               chipnr++;
-                               this->select_chip(mtd, -1);
-                               this->select_chip(mtd, chipnr);
-                       }
+       chip->oob_poi = chip->buffers.oobwbuf;
+
+       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;
+
+               writelen -= bytes;
+               if (!writelen)
+                       break;
+
+               buf += bytes;
+               realpage++;
+
+               page = realpage & chip->pagemask;
+               /* Check, if we cross a chip boundary */
+               if (!page) {
+                       chipnr++;
+                       chip->select_chip(mtd, -1);
+                       chip->select_chip(mtd, chipnr);
                }
        }
-       /* Verify the remaining pages */
- cmp:
-       this->data_poi = bufstart;
-       ret = nand_verify_pages(mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL));
-       if (!ret)
-               *retlen = written;
-       else
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
 
- out:
-       /* Deselect and wake up anyone waiting on the device */
-       nand_release_device(mtd);
+       if (unlikely(oob))
+               memset(chip->oob_poi, 0xff, mtd->oobsize);
 
+       ops->retlen = ops->len - writelen;
        return ret;
 }
 
 /**
- * nand_write_oob - [MTD Interface] NAND write out-of-band
+ * nand_write - [MTD Interface] 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
  *
- * NAND write out-of-band
+ * NAND write with ECC
  */
-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_write(struct mtd_info *mtd, loff_t to, size_t len,
+                         size_t *retlen, const uint8_t *buf)
 {
-       int column, page, status, ret = -EIO, chipnr;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
+       int ret;
 
-       DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
+       /* Do not allow reads past end of device */
+       if ((to + len) > mtd->size)
+               return -EINVAL;
+       if (!len)
+               return 0;
 
-       /* Shift to get page */
-       page = (int)(to >> this->page_shift);
-       chipnr = (int)(to >> this->chip_shift);
+       nand_get_device(chip, mtd, FL_READING);
+
+       chip->ops.len = len;
+       chip->ops.datbuf = (uint8_t *)buf;
+       chip->ops.oobbuf = NULL;
 
-       /* Mask to get column */
-       column = to & (mtd->oobsize - 1);
+       ret = nand_do_write_ops(mtd, to, &chip->ops);
 
-       /* Initialize return length value */
-       *retlen = 0;
+       nand_release_device(mtd);
+
+       *retlen = chip->ops.retlen;
+       return ret;
+}
+
+/**
+ * nand_do_write_oob - [MTD Interface] NAND write out-of-band
+ * @mtd:       MTD device structure
+ * @to:                offset to write to
+ * @ops:       oob operation description structure
+ *
+ * NAND write out-of-band
+ */
+static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
+                            struct mtd_oob_ops *ops)
+{
+       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)ops->len);
 
        /* Do not allow write past end of page */
-       if ((column + len) > mtd->oobsize) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
+       if ((ops->ooboffs + ops->len) > mtd->oobsize) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
+                     "Attempt to write past end of page\n");
                return -EINVAL;
        }
 
-       /* Grab the lock and see if the device is available */
-       nand_get_device(this, mtd, FL_WRITING);
+       chipnr = (int)(to >> chip->chip_shift);
+       chip->select_chip(mtd, chipnr);
 
-       /* Select the NAND device */
-       this->select_chip(mtd, chipnr);
+       /* Shift to get page */
+       page = (int)(to >> chip->page_shift);
 
-       /* Reset the chip. Some chips (like the Toshiba TC5832DC found
-          in one of my DiskOnChip 2000 test units) will clear the whole
-          data page too if we don't do this. I have no clue why, but
-          I seem to have 'fixed' it in the doc2000 driver in
-          August 1999.  dwmw2. */
-       this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
+       /*
+        * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
+        * of my DiskOnChip 2000 test units) will clear the whole data page too
+        * if we don't do this. I have no clue why, but I seem to have 'fixed'
+        * it in the doc2000 driver in August 1999.  dwmw2.
+        */
+       chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
 
        /* 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 == this->pagebuf)
-               this->pagebuf = -1;
-
-       if (NAND_MUST_PAD(this)) {
-               /* Write out desired data */
-               this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page & this->pagemask);
-               /* prepad 0xff for partial programming */
-               this->write_buf(mtd, ffchars, column);
-               /* write data */
-               this->write_buf(mtd, buf, len);
-               /* postpad 0xff for partial programming */
-               this->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
+       if (page == chip->pagebuf)
+               chip->pagebuf = -1;
+
+       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);
+               chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
+               memset(chip->oob_poi, 0xff, mtd->oobsize);
        } else {
-               /* Write out desired data */
-               this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column, page & this->pagemask);
-               /* write data */
-               this->write_buf(mtd, buf, len);
+               chip->cmdfunc(mtd, NAND_CMD_SEQIN,
+                             mtd->writesize + ops->ooboffs,
+                             page & chip->pagemask);
+               chip->write_buf(mtd, ops->oobbuf, ops->len);
        }
+
        /* Send command to program the OOB data */
-       this->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
+       chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
 
-       status = this->waitfunc(mtd, this, FL_WRITING);
+       status = chip->waitfunc(mtd, chip, FL_WRITING);
 
        /* 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;
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
+                     "Failed write, page 0x%08x\n", page);
+               return -EIO;
        }
-       /* Return happy */
-       *retlen = len;
+       ops->retlen = ops->len;
 
 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
-       /* Send command to read back the data */
-       this->cmdfunc(mtd, NAND_CMD_READOOB, column, page & this->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, ops->oobbuf, ops->len)) {
+                       DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
+                             "Failed write verify, page 0x%08x\n", page);
+                       return -EIO;
+               }
+       }
+#endif
+               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;
 
-       if (this->verify_buf(mtd, buf, len)) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
-               ret = -EIO;
+       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;
        }
-#endif
-       ret = 0;
+
+       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;
 }
 
@@ -1813,10 +1697,10 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *r
  */
 static void single_erase_cmd(struct mtd_info *mtd, int page)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        /* Send commands to erase a block */
-       this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
-       this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
 }
 
 /**
@@ -1829,13 +1713,13 @@ static void single_erase_cmd(struct mtd_info *mtd, int page)
  */
 static void multi_erase_cmd(struct mtd_info *mtd, int page)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        /* Send commands to erase a block */
-       this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
-       this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
-       this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
-       this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
-       this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
+       chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
 }
 
 /**
@@ -1852,71 +1736,75 @@ static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
 
 #define BBT_PAGE_MASK  0xffffff3f
 /**
- * nand_erase_intern - [NAND Interface] erase block(s)
+ * nand_erase_nand - [Internal] erase block(s)
  * @mtd:       MTD device structure
  * @instr:     erase instruction
  * @allowbbt:  allow erasing the bbt area
  *
  * Erase one ore more blocks
  */
-int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
+int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
+                   int allowbbt)
 {
        int page, len, status, pages_per_block, ret, chipnr;
-       struct nand_chip *this = mtd->priv;
-       int rewrite_bbt[NAND_MAX_CHIPS]={0};    /* flags to indicate the page, if bbt needs to be rewritten. */
-       unsigned int bbt_masked_page;           /* bbt mask to compare to page being erased. */
-                                               /* It is used to see if the current page is in the same */
-                                               /*   256 block group and the same bank as the bbt. */
+       struct nand_chip *chip = mtd->priv;
+       int rewrite_bbt[NAND_MAX_CHIPS]={0};
+       unsigned int bbt_masked_page = 0xffffffff;
 
-       DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n", (unsigned int)instr->addr, (unsigned int)instr->len);
+       DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
+             (unsigned int)instr->addr, (unsigned int)instr->len);
 
        /* Start address must align on block boundary */
-       if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
+       if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
                DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
                return -EINVAL;
        }
 
        /* Length must align on block boundary */
-       if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
+       if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
+                     "Length not block aligned\n");
                return -EINVAL;
        }
 
        /* Do not allow erase past end of device */
        if ((instr->len + instr->addr) > mtd->size) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
+                     "Erase past end of device\n");
                return -EINVAL;
        }
 
        instr->fail_addr = 0xffffffff;
 
        /* Grab the lock and see if the device is available */
-       nand_get_device(this, mtd, FL_ERASING);
+       nand_get_device(chip, mtd, FL_ERASING);
 
        /* Shift to get first page */
-       page = (int)(instr->addr >> this->page_shift);
-       chipnr = (int)(instr->addr >> this->chip_shift);
+       page = (int)(instr->addr >> chip->page_shift);
+       chipnr = (int)(instr->addr >> chip->chip_shift);
 
        /* Calculate pages in each block */
-       pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
+       pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
 
        /* Select the NAND device */
-       this->select_chip(mtd, chipnr);
+       chip->select_chip(mtd, chipnr);
 
-       /* Check the WP bit */
        /* Check, if it is write protected */
        if (nand_check_wp(mtd)) {
-               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
+                     "Device is write protected!!!\n");
                instr->state = MTD_ERASE_FAILED;
                goto erase_exit;
        }
 
-       /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */
-       if (this->options & BBT_AUTO_REFRESH) {
-               bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
-       } else {
-               bbt_masked_page = 0xffffffff;   /* should not match anything */
-       }
+       /*
+        * If BBT requires refresh, set the BBT page mask to see if the BBT
+        * should be rewritten. Otherwise the mask is set to 0xffffffff which
+        * can not be matched. This is also done when the bbt is actually
+        * erased to avoid recusrsive updates
+        */
+       if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
+               bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
 
        /* Loop through the pages */
        len = instr->len;
@@ -1924,59 +1812,72 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt
        instr->state = MTD_ERASING;
 
        while (len) {
-               /* Check if we have a bad block, we do not erase bad blocks ! */
-               if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
-                       printk(KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
+               /*
+                * heck if we have a bad block, we do not erase bad blocks !
+                */
+               if (nand_block_checkbad(mtd, ((loff_t) page) <<
+                                       chip->page_shift, 0, allowbbt)) {
+                       printk(KERN_WARNING "nand_erase: attempt to erase a "
+                              "bad block at page 0x%08x\n", page);
                        instr->state = MTD_ERASE_FAILED;
                        goto erase_exit;
                }
 
-               /* Invalidate the page cache, if we erase the block which contains
-                  the current cached page */
-               if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
-                       this->pagebuf = -1;
+               /*
+                * Invalidate the page cache, if we erase the block which
+                * contains the current cached page
+                */
+               if (page <= chip->pagebuf && chip->pagebuf <
+                   (page + pages_per_block))
+                       chip->pagebuf = -1;
 
-               this->erase_cmd(mtd, page & this->pagemask);
+               chip->erase_cmd(mtd, page & chip->pagemask);
 
-               status = this->waitfunc(mtd, this, FL_ERASING);
+               status = chip->waitfunc(mtd, chip, FL_ERASING);
 
-               /* See if operation failed and additional status checks are available */
-               if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
-                       status = this->errstat(mtd, this, FL_ERASING, status, page);
-               }
+               /*
+                * See if operation failed and additional status checks are
+                * available
+                */
+               if ((status & NAND_STATUS_FAIL) && (chip->errstat))
+                       status = chip->errstat(mtd, chip, FL_ERASING,
+                                              status, page);
 
                /* See if block erase succeeded */
                if (status & NAND_STATUS_FAIL) {
-                       DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
+                       DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
+                             "Failed erase, page 0x%08x\n", page);
                        instr->state = MTD_ERASE_FAILED;
-                       instr->fail_addr = (page << this->page_shift);
+                       instr->fail_addr = (page << chip->page_shift);
                        goto erase_exit;
                }
 
-               /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */
-               if (this->options & BBT_AUTO_REFRESH) {
-                       if (((page & BBT_PAGE_MASK) == bbt_masked_page) &&
-                            (page != this->bbt_td->pages[chipnr])) {
-                               rewrite_bbt[chipnr] = (page << this->page_shift);
-                       }
-               }
+               /*
+                * If BBT requires refresh, set the BBT rewrite flag to the
+                * page being erased
+                */
+               if (bbt_masked_page != 0xffffffff &&
+                   (page & BBT_PAGE_MASK) == bbt_masked_page)
+                           rewrite_bbt[chipnr] = (page << chip->page_shift);
 
                /* Increment page address and decrement length */
-               len -= (1 << this->phys_erase_shift);
+               len -= (1 << chip->phys_erase_shift);
                page += pages_per_block;
 
                /* Check, if we cross a chip boundary */
-               if (len && !(page & this->pagemask)) {
+               if (len && !(page & chip->pagemask)) {
                        chipnr++;
-                       this->select_chip(mtd, -1);
-                       this->select_chip(mtd, chipnr);
-
-                       /* if BBT requires refresh and BBT-PERCHIP,
-                        *   set the BBT page mask to see if this BBT should be rewritten */
-                       if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) {
-                               bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
-                       }
+                       chip->select_chip(mtd, -1);
+                       chip->select_chip(mtd, chipnr);
 
+                       /*
+                        * If BBT requires refresh and BBT-PERCHIP, set the BBT
+                        * page mask to see if this BBT should be rewritten
+                        */
+                       if (bbt_masked_page != 0xffffffff &&
+                           (chip->bbt_td->options & NAND_BBT_PERCHIP))
+                               bbt_masked_page = chip->bbt_td->pages[chipnr] &
+                                       BBT_PAGE_MASK;
                }
        }
        instr->state = MTD_ERASE_DONE;
@@ -1991,16 +1892,21 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt
        /* Deselect and wake up anyone waiting on the device */
        nand_release_device(mtd);
 
-       /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */
-       if ((this->options & BBT_AUTO_REFRESH) && (!ret)) {
-               for (chipnr = 0; chipnr < this->numchips; chipnr++) {
-                       if (rewrite_bbt[chipnr]) {
-                               /* update the BBT for chip */
-                               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n",
-                                     chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]);
-                               nand_update_bbt(mtd, rewrite_bbt[chipnr]);
-                       }
-               }
+       /*
+        * If BBT requires refresh and erase was successful, rewrite any
+        * selected bad block tables
+        */
+       if (bbt_masked_page == 0xffffffff || ret)
+               return ret;
+
+       for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
+               if (!rewrite_bbt[chipnr])
+                       continue;
+               /* update the BBT for chip */
+               DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
+                     "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
+                     chip->bbt_td->pages[chipnr]);
+               nand_update_bbt(mtd, rewrite_bbt[chipnr]);
        }
 
        /* Return more or less happy */
@@ -2015,38 +1921,38 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt
  */
 static void nand_sync(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
        DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
 
        /* Grab the lock and see if the device is available */
-       nand_get_device(this, mtd, FL_SYNCING);
+       nand_get_device(chip, mtd, FL_SYNCING);
        /* Release it and go back */
        nand_release_device(mtd);
 }
 
 /**
- * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
+ * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  * @mtd:       MTD device structure
  * @ofs:       offset relative to mtd start
  */
-static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
+static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
 {
        /* Check for invalid offset */
-       if (ofs > mtd->size)
+       if (offs > mtd->size)
                return -EINVAL;
 
-       return nand_block_checkbad(mtd, ofs, 1, 0);
+       return nand_block_checkbad(mtd, offs, 1, 0);
 }
 
 /**
- * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
+ * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  * @mtd:       MTD device structure
  * @ofs:       offset relative to mtd start
  */
 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        int ret;
 
        if ((ret = nand_block_isbad(mtd, ofs))) {
@@ -2056,7 +1962,7 @@ static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
                return ret;
        }
 
-       return this->block_markbad(mtd, ofs);
+       return chip->block_markbad(mtd, ofs);
 }
 
 /**
@@ -2065,9 +1971,9 @@ static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  */
 static int nand_suspend(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
-       return nand_get_device(this, mtd, FL_PM_SUSPENDED);
+       return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
 }
 
 /**
@@ -2076,9 +1982,9 @@ static int nand_suspend(struct mtd_info *mtd)
  */
 static void nand_resume(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
-       if (this->state == FL_PM_SUSPENDED)
+       if (chip->state == FL_PM_SUSPENDED)
                nand_release_device(mtd);
        else
                printk(KERN_ERR "nand_resume() called for a chip which is not "
@@ -2086,116 +1992,68 @@ static void nand_resume(struct mtd_info *mtd)
 }
 
 /*
- * Free allocated data structures
- */
-static void nand_free_kmem(struct nand_chip *this)
-{
-       /* Buffer allocated by nand_scan ? */
-       if (this->options & NAND_OOBBUF_ALLOC)
-               kfree(this->oob_buf);
-       /* Buffer allocated by nand_scan ? */
-       if (this->options & NAND_DATABUF_ALLOC)
-               kfree(this->data_buf);
-       /* Controller allocated by nand_scan ? */
-       if (this->options & NAND_CONTROLLER_ALLOC)
-               kfree(this->controller);
-}
-
-/*
- * Allocate buffers and data structures
- */
-static int nand_allocate_kmem(struct mtd_info *mtd, struct nand_chip *this)
-{
-       size_t len;
-
-       if (!this->oob_buf) {
-               len = mtd->oobsize <<
-                       (this->phys_erase_shift - this->page_shift);
-               this->oob_buf = kmalloc(len, GFP_KERNEL);
-               if (!this->oob_buf)
-                       goto outerr;
-               this->options |= NAND_OOBBUF_ALLOC;
-       }
-
-       if (!this->data_buf) {
-               len = mtd->writesize + mtd->oobsize;
-               this->data_buf = kmalloc(len, GFP_KERNEL);
-               if (!this->data_buf)
-                       goto outerr;
-               this->options |= NAND_DATABUF_ALLOC;
-       }
-
-       if (!this->controller) {
-               this->controller = kzalloc(sizeof(struct nand_hw_control),
-                                          GFP_KERNEL);
-               if (!this->controller)
-                       goto outerr;
-               this->options |= NAND_CONTROLLER_ALLOC;
-       }
-       return 0;
-
- outerr:
-       printk(KERN_ERR "nand_scan(): Cannot allocate buffers\n");
-       nand_free_kmem(this);
-       return -ENOMEM;
-}
-
-/*
  * Set default functions
  */
-static void nand_set_defaults(struct nand_chip *this, int busw)
+static void nand_set_defaults(struct nand_chip *chip, int busw)
 {
        /* check for proper chip_delay setup, set 20us if not */
-       if (!this->chip_delay)
-               this->chip_delay = 20;
+       if (!chip->chip_delay)
+               chip->chip_delay = 20;
 
        /* check, if a user supplied command function given */
-       if (this->cmdfunc == NULL)
-               this->cmdfunc = nand_command;
+       if (chip->cmdfunc == NULL)
+               chip->cmdfunc = nand_command;
 
        /* check, if a user supplied wait function given */
-       if (this->waitfunc == NULL)
-               this->waitfunc = nand_wait;
-
-       if (!this->select_chip)
-               this->select_chip = nand_select_chip;
-       if (!this->read_byte)
-               this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
-       if (!this->read_word)
-               this->read_word = nand_read_word;
-       if (!this->block_bad)
-               this->block_bad = nand_block_bad;
-       if (!this->block_markbad)
-               this->block_markbad = nand_default_block_markbad;
-       if (!this->write_buf)
-               this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
-       if (!this->read_buf)
-               this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
-       if (!this->verify_buf)
-               this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
-       if (!this->scan_bbt)
-               this->scan_bbt = nand_default_bbt;
+       if (chip->waitfunc == NULL)
+               chip->waitfunc = nand_wait;
+
+       if (!chip->select_chip)
+               chip->select_chip = nand_select_chip;
+       if (!chip->read_byte)
+               chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
+       if (!chip->read_word)
+               chip->read_word = nand_read_word;
+       if (!chip->block_bad)
+               chip->block_bad = nand_block_bad;
+       if (!chip->block_markbad)
+               chip->block_markbad = nand_default_block_markbad;
+       if (!chip->write_buf)
+               chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
+       if (!chip->read_buf)
+               chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
+       if (!chip->verify_buf)
+               chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
+       if (!chip->scan_bbt)
+               chip->scan_bbt = nand_default_bbt;
+
+       if (!chip->controller) {
+               chip->controller = &chip->hwcontrol;
+               spin_lock_init(&chip->controller->lock);
+               init_waitqueue_head(&chip->controller->wq);
+       }
+
 }
 
 /*
- * Get the flash and manufacturer id and lookup if the typ is supported
+ * Get the flash and manufacturer id and lookup if the type is supported
  */
 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
-                                                 struct nand_chip *this,
+                                                 struct nand_chip *chip,
                                                  int busw, int *maf_id)
 {
        struct nand_flash_dev *type = NULL;
        int i, dev_id, maf_idx;
 
        /* Select the device */
-       this->select_chip(mtd, 0);
+       chip->select_chip(mtd, 0);
 
        /* Send the command for reading device ID */
-       this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
+       chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
 
        /* Read manufacturer and device IDs */
-       *maf_id = this->read_byte(mtd);
-       dev_id = this->read_byte(mtd);
+       *maf_id = chip->read_byte(mtd);
+       dev_id = chip->read_byte(mtd);
 
        /* Lookup the flash id */
        for (i = 0; nand_flash_ids[i].name != NULL; i++) {
@@ -2208,15 +2066,18 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
        if (!type)
                return ERR_PTR(-ENODEV);
 
-       this->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 = this->read_byte(mtd);
+               extid = chip->read_byte(mtd);
                /* The 4th id byte is the important one */
-               extid = this->read_byte(mtd);
+               extid = chip->read_byte(mtd);
                /* Calc pagesize */
                mtd->writesize = 1024 << (extid & 0x3);
                extid >>= 2;
@@ -2231,12 +2092,12 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
        } else {
                /*
-                * Old devices have this data hardcoded in the device id table
+                * 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 */
@@ -2247,55 +2108,55 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
        /*
         * Check, if buswidth is correct. Hardware drivers should set
-        * this correct !
+        * chip correct !
         */
-       if (busw != (this->options & NAND_BUSWIDTH_16)) {
+       if (busw != (chip->options & NAND_BUSWIDTH_16)) {
                printk(KERN_INFO "NAND device: Manufacturer ID:"
                       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
                       dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
                printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
-                      (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
+                      (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
                       busw ? 16 : 8);
                return ERR_PTR(-EINVAL);
        }
 
        /* Calculate the address shift from the page size */
-       this->page_shift = ffs(mtd->writesize) - 1;
+       chip->page_shift = ffs(mtd->writesize) - 1;
        /* Convert chipsize to number of pages per chip -1. */
-       this->pagemask = (this->chipsize >> this->page_shift) - 1;
+       chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
 
-       this->bbt_erase_shift = this->phys_erase_shift =
+       chip->bbt_erase_shift = chip->phys_erase_shift =
                ffs(mtd->erasesize) - 1;
-       this->chip_shift = ffs(this->chipsize) - 1;
+       chip->chip_shift = ffs(chip->chipsize) - 1;
 
        /* Set the bad block position */
-       this->badblockpos = mtd->writesize > 512 ?
+       chip->badblockpos = mtd->writesize > 512 ?
                NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
 
        /* Get chip options, preserve non chip based options */
-       this->options &= ~NAND_CHIPOPTIONS_MSK;
-       this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
+       chip->options &= ~NAND_CHIPOPTIONS_MSK;
+       chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
 
        /*
-        * Set this as a default. Board drivers can override it, if necessary
+        * Set chip as a default. Board drivers can override it, if necessary
         */
-       this->options |= NAND_NO_AUTOINCR;
+       chip->options |= NAND_NO_AUTOINCR;
 
-       /* Check if this is a not a samsung device. Do not clear the
+       /* 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)
-               this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
+       if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
+               chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
 
        /* Check for AND chips with 4 page planes */
-       if (this->options & NAND_4PAGE_ARRAY)
-               this->erase_cmd = multi_erase_cmd;
+       if (chip->options & NAND_4PAGE_ARRAY)
+               chip->erase_cmd = multi_erase_cmd;
        else
-               this->erase_cmd = single_erase_cmd;
+               chip->erase_cmd = single_erase_cmd;
 
        /* Do not replace user supplied command function ! */
-       if (mtd->writesize > 512 && this->cmdfunc == nand_command)
-               this->cmdfunc = nand_command_lp;
+       if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
+               chip->cmdfunc = nand_command_lp;
 
        printk(KERN_INFO "NAND device: Manufacturer ID:"
               " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
@@ -2322,15 +2183,14 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  * 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. Buffers are allocated if
- * they are not provided by the board driver
+ * 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 i, busw, nand_maf_id;
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
        struct nand_flash_dev *type;
 
        /* Many callers got this wrong, so check for it for a while... */
@@ -2340,57 +2200,52 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
        }
 
        /* Get buswidth to select the correct functions */
-       busw = this->options & NAND_BUSWIDTH_16;
+       busw = chip->options & NAND_BUSWIDTH_16;
        /* Set the default functions */
-       nand_set_defaults(this, busw);
+       nand_set_defaults(chip, busw);
 
        /* Read the flash type */
-       type = nand_get_flash_type(mtd, this, busw, &nand_maf_id);
+       type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
 
        if (IS_ERR(type)) {
                printk(KERN_WARNING "No NAND device found!!!\n");
-               this->select_chip(mtd, -1);
+               chip->select_chip(mtd, -1);
                return PTR_ERR(type);
        }
 
        /* Check for a chip array */
        for (i = 1; i < maxchips; i++) {
-               this->select_chip(mtd, i);
+               chip->select_chip(mtd, i);
                /* Send the command for reading device ID */
-               this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
+               chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
                /* Read manufacturer and device IDs */
-               if (nand_maf_id != this->read_byte(mtd) ||
-                   type->id != this->read_byte(mtd))
+               if (nand_maf_id != chip->read_byte(mtd) ||
+                   type->id != chip->read_byte(mtd))
                        break;
        }
        if (i > 1)
                printk(KERN_INFO "%d NAND chips detected\n", i);
 
        /* Store the number of chips and calc total size for mtd */
-       this->numchips = i;
-       mtd->size = i * this->chipsize;
-
-       /* Allocate buffers and data structures */
-       if (nand_allocate_kmem(mtd, this))
-               return -ENOMEM;
+       chip->numchips = i;
+       mtd->size = i * chip->chipsize;
 
-       /* Preset the internal oob buffer */
-       memset(this->oob_buf, 0xff,
-              mtd->oobsize << (this->phys_erase_shift - this->page_shift));
+       /* Preset the internal oob write buffer */
+       memset(chip->buffers.oobwbuf, 0xff, mtd->oobsize);
 
        /*
         * If no default placement scheme is given, select an appropriate one
         */
-       if (!this->autooob) {
+       if (!chip->ecc.layout) {
                switch (mtd->oobsize) {
                case 8:
-                       this->autooob = &nand_oob_8;
+                       chip->ecc.layout = &nand_oob_8;
                        break;
                case 16:
-                       this->autooob = &nand_oob_16;
+                       chip->ecc.layout = &nand_oob_16;
                        break;
                case 64:
-                       this->autooob = &nand_oob_64;
+                       chip->ecc.layout = &nand_oob_64;
                        break;
                default:
                        printk(KERN_WARNING "No oob scheme defined for "
@@ -2400,72 +2255,88 @@ 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; this->autooob->oobfree[i][1]; i++)
-               mtd->oobavail += this->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
         */
-       switch (this->ecc.mode) {
+       switch (chip->ecc.mode) {
        case NAND_ECC_HW:
+               /* Use standard hwecc read page function ? */
+               if (!chip->ecc.read_page)
+                       chip->ecc.read_page = nand_read_page_hwecc;
+               if (!chip->ecc.write_page)
+                       chip->ecc.write_page = nand_write_page_hwecc;
+
        case NAND_ECC_HW_SYNDROME:
-               if (!this->ecc.calculate || !this->ecc.correct ||
-                   !this->ecc.hwctl) {
+               if (!chip->ecc.calculate || !chip->ecc.correct ||
+                   !chip->ecc.hwctl) {
                        printk(KERN_WARNING "No ECC functions supplied, "
                               "Hardware ECC not possible\n");
                        BUG();
                }
-               if (mtd->writesize >= this->ecc.size)
+               /* Use standard syndrome read/write page function ? */
+               if (!chip->ecc.read_page)
+                       chip->ecc.read_page = nand_read_page_syndrome;
+               if (!chip->ecc.write_page)
+                       chip->ecc.write_page = nand_write_page_syndrome;
+
+               if (mtd->writesize >= chip->ecc.size)
                        break;
                printk(KERN_WARNING "%d byte HW ECC not possible on "
                       "%d byte page size, fallback to SW ECC\n",
-                      this->ecc.size, mtd->writesize);
-               this->ecc.mode = NAND_ECC_SOFT;
+                      chip->ecc.size, mtd->writesize);
+               chip->ecc.mode = NAND_ECC_SOFT;
 
        case NAND_ECC_SOFT:
-               this->ecc.calculate = nand_calculate_ecc;
-               this->ecc.correct = nand_correct_data;
-               this->ecc.size = 256;
-               this->ecc.bytes = 3;
+               chip->ecc.calculate = nand_calculate_ecc;
+               chip->ecc.correct = nand_correct_data;
+               chip->ecc.read_page = nand_read_page_swecc;
+               chip->ecc.write_page = nand_write_page_swecc;
+               chip->ecc.size = 256;
+               chip->ecc.bytes = 3;
                break;
 
        case NAND_ECC_NONE:
                printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
                       "This is not recommended !!\n");
-               this->ecc.size = mtd->writesize;
-               this->ecc.bytes = 0;
+               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;
        default:
                printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
-                      this->ecc.mode);
+                      chip->ecc.mode);
                BUG();
        }
 
        /*
+        * 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
         */
-       this->ecc.steps = mtd->writesize / this->ecc.size;
-       if(this->ecc.steps * this->ecc.size != mtd->writesize) {
+       chip->ecc.steps = mtd->writesize / chip->ecc.size;
+       if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
                printk(KERN_WARNING "Invalid ecc parameters\n");
                BUG();
        }
+       chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
 
-       /* Initialize state, waitqueue and spinlock */
-       this->state = FL_READY;
-       init_waitqueue_head(&this->controller->wq);
-       spin_lock_init(&this->controller->lock);
+       /* Initialize state */
+       chip->state = FL_READY;
 
        /* De-select the device */
-       this->select_chip(mtd, -1);
+       chip->select_chip(mtd, -1);
 
        /* Invalidate the pagebuffer reference */
-       this->pagebuf = -1;
+       chip->pagebuf = -1;
 
        /* Fill in remaining MTD driver data */
        mtd->type = MTD_NANDFLASH;
@@ -2486,15 +2357,15 @@ 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, this->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 (this->options & NAND_SKIP_BBTSCAN)
+       if (chip->options & NAND_SKIP_BBTSCAN)
                return 0;
 
        /* Build bad block table */
-       return this->scan_bbt(mtd);
+       return chip->scan_bbt(mtd);
 }
 
 /**
@@ -2503,7 +2374,7 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
 */
 void nand_release(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *chip = mtd->priv;
 
 #ifdef CONFIG_MTD_PARTITIONS
        /* Deregister partitions */
@@ -2513,9 +2384,7 @@ void nand_release(struct mtd_info *mtd)
        del_mtd_device(mtd);
 
        /* Free bad block table memory */
-       kfree(this->bbt);
-       /* Free buffers */
-       nand_free_kmem(this);
+       kfree(chip->bbt);
 }
 
 EXPORT_SYMBOL_GPL(nand_scan);