]> git.karo-electronics.de Git - karo-tx-uboot.git/blobdiff - drivers/mtd/nand/nand_base.c
nand: Sync with Linux v4.1
[karo-tx-uboot.git] / drivers / mtd / nand / nand_base.c
index 7153e3ca3630a2219d09dfca487cc7dcafa00a55..d952f9c9d2c8863d853417e12313780cb1f93cc2 100644 (file)
  *
  */
 
-#ifndef __UBOOT__
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
-
-#include <linux/module.h>
-#include <linux/delay.h>
-#include <linux/errno.h>
-#include <linux/err.h>
-#include <linux/sched.h>
-#include <linux/slab.h>
-#include <linux/types.h>
-#include <linux/mtd/mtd.h>
-#include <linux/mtd/nand.h>
-#include <linux/mtd/nand_ecc.h>
-#include <linux/mtd/nand_bch.h>
-#include <linux/interrupt.h>
-#include <linux/bitops.h>
-#include <linux/leds.h>
-#include <linux/io.h>
-#include <linux/mtd/partitions.h>
-#else
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 #include <common.h>
 #include <malloc.h>
 #include <asm/io.h>
 #include <asm/errno.h>
 
-/*
- * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
- * a flash.  NAND flash is initialized prior to interrupts so standard timers
- * can't be used.  CONFIG_SYS_NAND_RESET_CNT should be set to a value
- * which is greater than (max NAND reset time / NAND status read time).
- * A conservative default of 200000 (500 us / 25 ns) is used as a default.
- */
-#ifndef CONFIG_SYS_NAND_RESET_CNT
-#define CONFIG_SYS_NAND_RESET_CNT 200000
-#endif
-
 static bool is_module_text_address(unsigned long addr) {return 0;}
-#endif
 
 /* Define default oob placement schemes for large and small page devices */
 static struct nand_ecclayout nand_oob_8 = {
@@ -165,17 +133,8 @@ static void nand_release_device(struct mtd_info *mtd)
 {
        struct nand_chip *chip = mtd->priv;
 
-#ifndef __UBOOT__
-       /* Release the controller and the chip */
-       spin_lock(&chip->controller->lock);
-       chip->controller->active = NULL;
-       chip->state = FL_READY;
-       wake_up(&chip->controller->wq);
-       spin_unlock(&chip->controller->lock);
-#else
        /* De-select the NAND device */
        chip->select_chip(mtd, -1);
-#endif
 }
 
 /**
@@ -184,18 +143,13 @@ static void nand_release_device(struct mtd_info *mtd)
  *
  * Default read function for 8bit buswidth
  */
-#ifndef __UBOOT__
-static uint8_t nand_read_byte(struct mtd_info *mtd)
-#else
 uint8_t nand_read_byte(struct mtd_info *mtd)
-#endif
 {
        struct nand_chip *chip = mtd->priv;
        return readb(chip->IO_ADDR_R);
 }
 
 /**
- * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  * @mtd: MTD device structure
  *
@@ -288,7 +242,7 @@ static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
        chip->write_buf(mtd, (uint8_t *)&word, 2);
 }
 
-#if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
+#if !defined(CONFIG_BLACKFIN)
 static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
 {
        int i;
@@ -308,8 +262,7 @@ static void ioread16_rep(void *addr, void *buf, int len)
 {
        int i;
        u16 *p = (u16 *) buf;
-       len >>= 1;
+
        for (i = 0; i < len; i++)
                p[i] = readw(addr);
 }
@@ -318,7 +271,6 @@ static void iowrite16_rep(void *addr, void *buf, int len)
 {
        int i;
         u16 *p = (u16 *) buf;
-        len >>= 1;
 
         for (i = 0; i < len; i++)
                 writew(p[i], addr);
@@ -333,11 +285,7 @@ static void iowrite16_rep(void *addr, void *buf, int len)
  *
  * Default write function for 8bit buswidth.
  */
-#ifndef __UBOOT__
-static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
-#else
 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
-#endif
 {
        struct nand_chip *chip = mtd->priv;
 
@@ -352,62 +300,13 @@ void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  *
  * Default read function for 8bit buswidth.
  */
-#ifndef __UBOOT__
-static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
-#else
 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
-#endif
 {
        struct nand_chip *chip = mtd->priv;
 
        ioread8_rep(chip->IO_ADDR_R, buf, len);
 }
 
-#ifdef __UBOOT__
-#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
-/**
- * nand_verify_buf - [DEFAULT] Verify chip data against buffer
- * @mtd: MTD device structure
- * @buf: buffer containing the data to compare
- * @len: number of bytes to compare
- *
- * Default verify function for 8bit buswidth.
- */
-static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
-{
-       int i;
-       struct nand_chip *chip = mtd->priv;
-
-       for (i = 0; i < len; i++)
-               if (buf[i] != readb(chip->IO_ADDR_R))
-                       return -EFAULT;
-       return 0;
-}
-
-/**
- * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
- * @mtd: MTD device structure
- * @buf: buffer containing the data to compare
- * @len: number of bytes to compare
- *
- * Default verify function for 16bit buswidth.
- */
-static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
-{
-       int i;
-       struct nand_chip *chip = mtd->priv;
-       u16 *p = (u16 *) buf;
-       len >>= 1;
-
-       for (i = 0; i < len; i++)
-               if (p[i] != readw(chip->IO_ADDR_R))
-                       return -EFAULT;
-
-       return 0;
-}
-#endif
-#endif
-
 /**
  * nand_write_buf16 - [DEFAULT] write buffer to chip
  * @mtd: MTD device structure
@@ -416,11 +315,7 @@ static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  *
  * Default write function for 16bit buswidth.
  */
-#ifndef __UBOOT__
-static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
-#else
 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
-#endif
 {
        struct nand_chip *chip = mtd->priv;
        u16 *p = (u16 *) buf;
@@ -436,11 +331,7 @@ void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  *
  * Default read function for 16bit buswidth.
  */
-#ifndef __UBOOT__
-static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
-#else
 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
-#endif
 {
        struct nand_chip *chip = mtd->priv;
        u16 *p = (u16 *) buf;
@@ -524,7 +415,7 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
        uint8_t buf[2] = { 0, 0 };
        int ret = 0, res, i = 0;
 
-       ops.datbuf = NULL;
+       memset(&ops, 0, sizeof(ops));
        ops.oobbuf = buf;
        ops.ooboffs = chip->badblockpos;
        if (chip->options & NAND_BUSWIDTH_16) {
@@ -622,71 +513,54 @@ static int nand_check_wp(struct mtd_info *mtd)
 }
 
 /**
- * nand_block_checkbad - [GENERIC] Check if a block is marked bad
+ * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
  * @mtd: MTD device structure
  * @ofs: offset from device start
- * @getchip: 0, if the chip is already selected
- * @allowbbt: 1, if its allowed to access the bbt area
  *
- * Check, if the block is bad. Either by reading the bad block table or
- * calling of the scan function.
+ * Check if the block is marked as reserved.
  */
-static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
-                              int allowbbt)
+static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
 {
        struct nand_chip *chip = mtd->priv;
 
        if (!chip->bbt)
-               return chip->block_bad(mtd, ofs, getchip);
-
+               return 0;
        /* Return info from the table */
-       return nand_isbad_bbt(mtd, ofs, allowbbt);
+       return nand_isreserved_bbt(mtd, ofs);
 }
 
-#ifndef __UBOOT__
 /**
- * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
+ * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  * @mtd: MTD device structure
- * @timeo: Timeout
+ * @ofs: offset from device start
+ * @getchip: 0, if the chip is already selected
+ * @allowbbt: 1, if its allowed to access the bbt area
  *
- * Helper function for nand_wait_ready used when needing to wait in interrupt
- * context.
+ * Check, if the block is bad. Either by reading the bad block table or
+ * calling of the scan function.
  */
-static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
+static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
+                              int allowbbt)
 {
        struct nand_chip *chip = mtd->priv;
-       int i;
 
-       /* Wait for the device to get ready */
-       for (i = 0; i < timeo; i++) {
-               if (chip->dev_ready(mtd))
-                       break;
-               touch_softlockup_watchdog();
-               mdelay(1);
+       if (!(chip->options & NAND_SKIP_BBTSCAN) &&
+           !(chip->options & NAND_BBT_SCANNED)) {
+               chip->options |= NAND_BBT_SCANNED;
+               chip->scan_bbt(mtd);
        }
+
+       if (!chip->bbt)
+               return chip->block_bad(mtd, ofs, getchip);
+
+       /* Return info from the table */
+       return nand_isbad_bbt(mtd, ofs, allowbbt);
 }
-#endif
 
 /* Wait for the ready pin, after a command. The timeout is caught later. */
 void nand_wait_ready(struct mtd_info *mtd)
 {
        struct nand_chip *chip = mtd->priv;
-#ifndef __UBOOT__
-       unsigned long timeo = jiffies + msecs_to_jiffies(20);
-
-       /* 400ms timeout */
-       if (in_interrupt() || oops_in_progress)
-               return panic_nand_wait_ready(mtd, 400);
-
-       led_trigger_event(nand_led_trigger, LED_FULL);
-       /* Wait until command is processed or timeout occurs */
-       do {
-               if (chip->dev_ready(mtd))
-                       break;
-               touch_softlockup_watchdog();
-       } while (time_before(jiffies, timeo));
-       led_trigger_event(nand_led_trigger, LED_OFF);
-#else
        u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
        u32 time_start;
 
@@ -697,10 +571,30 @@ void nand_wait_ready(struct mtd_info *mtd)
                        if (chip->dev_ready(mtd))
                                break;
        }
-#endif
 }
 EXPORT_SYMBOL_GPL(nand_wait_ready);
 
+/**
+ * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
+ * @mtd: MTD device structure
+ * @timeo: Timeout in ms
+ *
+ * Wait for status ready (i.e. command done) or timeout.
+ */
+static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
+{
+       register struct nand_chip *chip = mtd->priv;
+       u32 time_start;
+
+       timeo = (CONFIG_SYS_HZ * timeo) / 1000;
+       time_start = get_timer(0);
+       while (get_timer(time_start) < timeo) {
+               if ((chip->read_byte(mtd) & NAND_STATUS_READY))
+                       break;
+               WATCHDOG_RESET();
+       }
+};
+
 /**
  * nand_command - [DEFAULT] Send command to NAND device
  * @mtd: MTD device structure
@@ -716,7 +610,6 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
 {
        register struct nand_chip *chip = mtd->priv;
        int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
-       uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
 
        /* Write out the command to the device */
        if (command == NAND_CMD_SEQIN) {
@@ -780,8 +673,8 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
                               NAND_CTRL_CLE | NAND_CTRL_CHANGE);
                chip->cmd_ctrl(mtd,
                               NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
-               while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
-                       (rst_sts_cnt--));
+               /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
+               nand_wait_status_ready(mtd, 250);
                return;
 
                /* This applies to read commands */
@@ -819,7 +712,6 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                            int column, int page_addr)
 {
        register struct nand_chip *chip = mtd->priv;
-       uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
 
        /* Emulate NAND_CMD_READOOB */
        if (command == NAND_CMD_READOOB) {
@@ -857,7 +749,7 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
 
        /*
         * Program and erase have their own busy handlers status, sequential
-        * in, and deplete1 need no delay.
+        * in and status need no delay.
         */
        switch (command) {
 
@@ -878,8 +770,8 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
                               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) &&
-                       (rst_sts_cnt--));
+               /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
+               nand_wait_status_ready(mtd, 250);
                return;
 
        case NAND_CMD_RNDOUT:
@@ -944,39 +836,8 @@ static int
 nand_get_device(struct mtd_info *mtd, int new_state)
 {
        struct nand_chip *chip = mtd->priv;
-#ifndef __UBOOT__
-       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 independent devices */
-       if (!chip->controller->active)
-               chip->controller->active = chip;
-
-       if (chip->controller->active == chip && chip->state == FL_READY) {
-               chip->state = new_state;
-               spin_unlock(lock);
-               return 0;
-       }
-       if (new_state == FL_PM_SUSPENDED) {
-               if (chip->controller->active->state == FL_PM_SUSPENDED) {
-                       chip->state = FL_PM_SUSPENDED;
-                       spin_unlock(lock);
-                       return 0;
-               }
-       }
-       set_current_state(TASK_UNINTERRUPTIBLE);
-       add_wait_queue(wq, &wait);
-       spin_unlock(lock);
-       schedule();
-       remove_wait_queue(wq, &wait);
-       goto retry;
-#else
        chip->state = new_state;
        return 0;
-#endif
 }
 
 /**
@@ -1030,23 +891,6 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
 
        chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
 
-#ifndef __UBOOT__
-       if (in_interrupt() || oops_in_progress)
-               panic_nand_wait(mtd, chip, timeo);
-       else {
-               timeo = jiffies + msecs_to_jiffies(timeo);
-               while (time_before(jiffies, timeo)) {
-                       if (chip->dev_ready) {
-                               if (chip->dev_ready(mtd))
-                                       break;
-                       } else {
-                               if (chip->read_byte(mtd) & NAND_STATUS_READY)
-                                       break;
-                       }
-                       cond_resched();
-               }
-       }
-#else
        u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
        u32 time_start;
  
@@ -1060,12 +904,6 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
                                break;
                }
        }
-#endif
-#ifdef PPCHAMELON_NAND_TIMER_HACK
-       time_start = get_timer(0);
-       while (get_timer(time_start) < 10)
-               ;
-#endif /*  PPCHAMELON_NAND_TIMER_HACK */
        led_trigger_event(nand_led_trigger, LED_OFF);
 
        status = (int)chip->read_byte(mtd);
@@ -1074,162 +912,6 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
        return status;
 }
 
-#ifndef __UBOOT__
-/**
- * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
- * @mtd: mtd info
- * @ofs: offset to start unlock from
- * @len: length to unlock
- * @invert: when = 0, unlock the range of blocks within the lower and
- *                    upper boundary address
- *          when = 1, unlock the range of blocks outside the boundaries
- *                    of the lower and upper boundary address
- *
- * Returs unlock status.
- */
-static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
-                                       uint64_t len, int invert)
-{
-       int ret = 0;
-       int status, page;
-       struct nand_chip *chip = mtd->priv;
-
-       /* Submit address of first page to unlock */
-       page = ofs >> chip->page_shift;
-       chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
-
-       /* Submit address of last page to unlock */
-       page = (ofs + len) >> chip->page_shift;
-       chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
-                               (page | invert) & chip->pagemask);
-
-       /* Call wait ready function */
-       status = chip->waitfunc(mtd, chip);
-       /* See if device thinks it succeeded */
-       if (status & NAND_STATUS_FAIL) {
-               pr_debug("%s: error status = 0x%08x\n",
-                                       __func__, status);
-               ret = -EIO;
-       }
-
-       return ret;
-}
-
-/**
- * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
- * @mtd: mtd info
- * @ofs: offset to start unlock from
- * @len: length to unlock
- *
- * Returns unlock status.
- */
-int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
-{
-       int ret = 0;
-       int chipnr;
-       struct nand_chip *chip = mtd->priv;
-
-       pr_debug("%s: start = 0x%012llx, len = %llu\n",
-                       __func__, (unsigned long long)ofs, len);
-
-       if (check_offs_len(mtd, ofs, len))
-               ret = -EINVAL;
-
-       /* Align to last block address if size addresses end of the device */
-       if (ofs + len == mtd->size)
-               len -= mtd->erasesize;
-
-       nand_get_device(mtd, FL_UNLOCKING);
-
-       /* Shift to get chip number */
-       chipnr = ofs >> chip->chip_shift;
-
-       chip->select_chip(mtd, chipnr);
-
-       /* Check, if it is write protected */
-       if (nand_check_wp(mtd)) {
-               pr_debug("%s: device is write protected!\n",
-                                       __func__);
-               ret = -EIO;
-               goto out;
-       }
-
-       ret = __nand_unlock(mtd, ofs, len, 0);
-
-out:
-       chip->select_chip(mtd, -1);
-       nand_release_device(mtd);
-
-       return ret;
-}
-EXPORT_SYMBOL(nand_unlock);
-
-/**
- * nand_lock - [REPLACEABLE] locks all blocks present in the device
- * @mtd: mtd info
- * @ofs: offset to start unlock from
- * @len: length to unlock
- *
- * This feature is not supported in many NAND parts. 'Micron' NAND parts do
- * have this feature, but it allows only to lock all blocks, not for specified
- * range for block. Implementing 'lock' feature by making use of 'unlock', for
- * now.
- *
- * Returns lock status.
- */
-int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
-{
-       int ret = 0;
-       int chipnr, status, page;
-       struct nand_chip *chip = mtd->priv;
-
-       pr_debug("%s: start = 0x%012llx, len = %llu\n",
-                       __func__, (unsigned long long)ofs, len);
-
-       if (check_offs_len(mtd, ofs, len))
-               ret = -EINVAL;
-
-       nand_get_device(mtd, FL_LOCKING);
-
-       /* Shift to get chip number */
-       chipnr = ofs >> chip->chip_shift;
-
-       chip->select_chip(mtd, chipnr);
-
-       /* Check, if it is write protected */
-       if (nand_check_wp(mtd)) {
-               pr_debug("%s: device is write protected!\n",
-                                       __func__);
-               status = MTD_ERASE_FAILED;
-               ret = -EIO;
-               goto out;
-       }
-
-       /* Submit address of first page to lock */
-       page = ofs >> chip->page_shift;
-       chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
-
-       /* Call wait ready function */
-       status = chip->waitfunc(mtd, chip);
-       /* See if device thinks it succeeded */
-       if (status & NAND_STATUS_FAIL) {
-               pr_debug("%s: error status = 0x%08x\n",
-                                       __func__, status);
-               ret = -EIO;
-               goto out;
-       }
-
-       ret = __nand_unlock(mtd, ofs, len, 0x1);
-
-out:
-       chip->select_chip(mtd, -1);
-       nand_release_device(mtd);
-
-       return ret;
-}
-EXPORT_SYMBOL(nand_lock);
-#endif
-
 /**
  * nand_read_page_raw - [INTERN] read raw page data without ecc
  * @mtd: mtd info structure
@@ -1387,8 +1069,7 @@ static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
         * ecc.pos. Let's make sure that there are no gaps in ECC positions.
         */
        for (i = 0; i < eccfrag_len - 1; i++) {
-               if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
-                       eccpos[i + start_step * chip->ecc.bytes + 1]) {
+               if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
                        gaps = 1;
                        break;
                }
@@ -1684,6 +1365,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
                mtd->oobavail : mtd->oobsize;
 
        uint8_t *bufpoi, *oob, *buf;
+       int use_bufpoi;
        unsigned int max_bitflips = 0;
        int retry_mode = 0;
        bool ecc_fail = false;
@@ -1707,9 +1389,18 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
                bytes = min(mtd->writesize - col, readlen);
                aligned = (bytes == mtd->writesize);
 
+               if (!aligned)
+                       use_bufpoi = 1;
+               else
+                       use_bufpoi = 0;
+
                /* Is the current page in the buffer? */
                if (realpage != chip->pagebuf || oob) {
-                       bufpoi = aligned ? buf : chip->buffers->databuf;
+                       bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
+
+                       if (use_bufpoi && aligned)
+                               pr_debug("%s: using read bounce buffer for buf@%p\n",
+                                                __func__, buf);
 
 read_retry:
                        chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
@@ -1731,7 +1422,7 @@ read_retry:
                                ret = chip->ecc.read_page(mtd, chip, bufpoi,
                                                          oob_required, page);
                        if (ret < 0) {
-                               if (!aligned)
+                               if (use_bufpoi)
                                        /* Invalidate page cache */
                                        chip->pagebuf = -1;
                                break;
@@ -1740,7 +1431,7 @@ read_retry:
                        max_bitflips = max_t(unsigned int, max_bitflips, ret);
 
                        /* Transfer not aligned data */
-                       if (!aligned) {
+                       if (use_bufpoi) {
                                if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
                                    !(mtd->ecc_stats.failed - ecc_failures) &&
                                    (ops->mode != MTD_OPS_RAW)) {
@@ -1854,9 +1545,9 @@ static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
        int ret;
 
        nand_get_device(mtd, FL_READING);
+       memset(&ops, 0, sizeof(ops));
        ops.len = len;
        ops.datbuf = buf;
-       ops.oobbuf = NULL;
        ops.mode = MTD_OPS_PLACE_OOB;
        ret = nand_do_read_ops(mtd, from, &ops);
        *retlen = ops.retlen;
@@ -1888,11 +1579,10 @@ static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
                                  int page)
 {
-       uint8_t *buf = chip->oob_poi;
        int length = mtd->oobsize;
        int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
        int eccsize = chip->ecc.size;
-       uint8_t *bufpoi = buf;
+       uint8_t *bufpoi = chip->oob_poi;
        int i, toread, sndrnd = 0, pos;
 
        chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
@@ -2265,7 +1955,7 @@ static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
 
 
 /**
- * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
+ * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
  * @mtd:       mtd info structure
  * @chip:      nand chip info structure
  * @offset:    column address of subpage within the page
@@ -2436,20 +2126,6 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
                status = chip->waitfunc(mtd, chip);
        }
 
-
-#ifdef __UBOOT__
-#if defined(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;
-
-       /* Make sure the next page prog is preceded by a status read */
-       chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
-#endif
-#endif
-
        return 0;
 }
 
@@ -2509,7 +2185,7 @@ static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
        return NULL;
 }
 
-#define NOTALIGNED(x)  ((x & (chip->subpagesize - 1)) != 0)
+#define NOTALIGNED(x)  (((x) & (chip->subpagesize - 1)) != 0)
 
 /**
  * nand_do_write_ops - [INTERN] NAND write with ECC
@@ -2539,13 +2215,8 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
        if (!writelen)
                return 0;
 
-#ifndef __UBOOT__
-       /* Reject writes, which are not page aligned */
-       if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
-#else
        /* Reject writes, which are not page aligned */
        if (NOTALIGNED(to)) {
-#endif
                pr_notice("%s: attempt to write non page aligned data\n",
                           __func__);
                return -EINVAL;
@@ -2567,8 +2238,8 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
        blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
 
        /* Invalidate the page cache, when we write to the cached page */
-       if (to <= (chip->pagebuf << chip->page_shift) &&
-           (chip->pagebuf << chip->page_shift) < (to + ops->len))
+       if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
+           ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
                chip->pagebuf = -1;
 
        /* Don't allow multipage oob writes with offset */
@@ -2581,12 +2252,22 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
                int bytes = mtd->writesize;
                int cached = writelen > bytes && page != blockmask;
                uint8_t *wbuf = buf;
+               int use_bufpoi;
+               int part_pagewr = (column || writelen < (mtd->writesize - 1));
+
+               if (part_pagewr)
+                       use_bufpoi = 1;
+               else
+                       use_bufpoi = 0;
 
                WATCHDOG_RESET();
-               /* Partial page write? */
-               if (unlikely(column || writelen < (mtd->writesize - 1))) {
+               /* Partial page write?, or need to use bounce buffer */
+               if (use_bufpoi) {
+                       pr_debug("%s: using write bounce buffer for buf@%p\n",
+                                        __func__, buf);
                        cached = 0;
-                       bytes = min_t(int, bytes - column, (int) writelen);
+                       if (part_pagewr)
+                               bytes = min_t(int, bytes - column, writelen);
                        chip->pagebuf = -1;
                        memset(chip->buffers->databuf, 0xff, mtd->writesize);
                        memcpy(&chip->buffers->databuf[column], buf, bytes);
@@ -2657,9 +2338,9 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
        /* Grab the device */
        panic_nand_get_device(chip, mtd, FL_WRITING);
 
+       memset(&ops, 0, sizeof(ops));
        ops.len = len;
        ops.datbuf = (uint8_t *)buf;
-       ops.oobbuf = NULL;
        ops.mode = MTD_OPS_PLACE_OOB;
 
        ret = nand_do_write_ops(mtd, to, &ops);
@@ -2685,9 +2366,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
        int ret;
 
        nand_get_device(mtd, FL_WRITING);
+       memset(&ops, 0, sizeof(ops));
        ops.len = len;
        ops.datbuf = (uint8_t *)buf;
-       ops.oobbuf = NULL;
        ops.mode = MTD_OPS_PLACE_OOB;
        ret = nand_do_write_ops(mtd, to, &ops);
        *retlen = ops.retlen;
@@ -2824,18 +2505,20 @@ out:
 }
 
 /**
- * single_erase_cmd - [GENERIC] NAND standard block erase command function
+ * single_erase - [GENERIC] NAND standard block erase command function
  * @mtd: MTD device structure
  * @page: the page address of the block which will be erased
  *
- * Standard erase command for NAND chips.
+ * Standard erase command for NAND chips. Returns NAND status.
  */
-static void single_erase_cmd(struct mtd_info *mtd, int page)
+static int single_erase(struct mtd_info *mtd, int page)
 {
        struct nand_chip *chip = mtd->priv;
        /* Send commands to erase a block */
        chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
        chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
+
+       return chip->waitfunc(mtd, chip);
 }
 
 /**
@@ -2902,7 +2585,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
                WATCHDOG_RESET();
 
                /* Check if we have a bad block, we do not erase bad blocks! */
-               if (nand_block_checkbad(mtd, ((loff_t) page) <<
+               if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
                                        chip->page_shift, 0, allowbbt)) {
                        pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
                                    __func__, page);
@@ -2918,9 +2601,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
                    (page + pages_per_block))
                        chip->pagebuf = -1;
 
-               chip->erase_cmd(mtd, page & chip->pagemask);
-
-               status = chip->waitfunc(mtd, chip);
+               status = chip->erase(mtd, page & chip->pagemask);
 
                /*
                 * See if operation failed and additional status checks are
@@ -3073,32 +2754,6 @@ static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
        return 0;
 }
 
-#ifndef __UBOOT__
-/**
- * nand_suspend - [MTD Interface] Suspend the NAND flash
- * @mtd: MTD device structure
- */
-static int nand_suspend(struct mtd_info *mtd)
-{
-       return nand_get_device(mtd, FL_PM_SUSPENDED);
-}
-
-/**
- * nand_resume - [MTD Interface] Resume the NAND flash
- * @mtd: MTD device structure
- */
-static void nand_resume(struct mtd_info *mtd)
-{
-       struct nand_chip *chip = mtd->priv;
-
-       if (chip->state == FL_PM_SUSPENDED)
-               nand_release_device(mtd);
-       else
-               pr_err("%s called for a chip which is not in suspended state\n",
-                       __func__);
-}
-#endif
-
 /* Set default functions */
 static void nand_set_defaults(struct nand_chip *chip, int busw)
 {
@@ -3140,12 +2795,6 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
                chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
        if (!chip->scan_bbt)
                chip->scan_bbt = nand_default_bbt;
-#ifdef __UBOOT__
-#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
-       if (!chip->verify_buf)
-               chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
-#endif
-#endif
 
        if (!chip->controller) {
                chip->controller = &chip->hwcontrol;
@@ -3156,11 +2805,7 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
 }
 
 /* Sanitize ONFI strings so we can safely print them */
-#ifndef __UBOOT__
-static void sanitize_string(uint8_t *s, size_t len)
-#else
 static void sanitize_string(char *s, size_t len)
-#endif
 {
        ssize_t i;
 
@@ -3226,11 +2871,7 @@ static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
         * Check the signature.
         * Do not strictly follow the ONFI spec, maybe changed in future.
         */
-#ifndef __UBOOT__
-       if (strncmp(ep->sig, "EPPS", 4)) {
-#else
        if (strncmp((char *)ep->sig, "EPPS", 4)) {
-#endif
                pr_debug("The signature is invalid.\n");
                goto ext_out;
        }
@@ -3761,11 +3402,7 @@ static inline bool is_full_id_nand(struct nand_flash_dev *type)
 static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
                   struct nand_flash_dev *type, u8 *id_data, int *busw)
 {
-#ifndef __UBOOT__
-       if (!strncmp(type->id, id_data, type->id_len)) {
-#else
        if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
-#endif
                mtd->writesize = type->pagesize;
                mtd->erasesize = type->erasesize;
                mtd->oobsize = type->oobsize;
@@ -3775,6 +3412,8 @@ static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
                chip->options |= type->options;
                chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
                chip->ecc_step_ds = NAND_ECC_STEP(type);
+               chip->onfi_timing_mode_default =
+                                       type->onfi_timing_mode_default;
 
                *busw = type->options & NAND_BUSWIDTH_16;
 
@@ -3847,7 +3486,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
        chip->onfi_version = 0;
        if (!type->name || !type->pagesize) {
-               /* Check is chip is ONFI compliant */
+               /* Check if the chip is ONFI compliant */
                if (nand_flash_detect_onfi(mtd, chip, &busw))
                        goto ident_done;
 
@@ -3925,7 +3564,7 @@ ident_done:
        }
 
        chip->badblockbits = 8;
-       chip->erase_cmd = single_erase_cmd;
+       chip->erase = single_erase;
 
        /* Do not replace user supplied command function! */
        if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
@@ -3956,9 +3595,9 @@ ident_done:
                type->name);
 #endif
 
-       pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
+       pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
                (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
-               mtd->writesize, mtd->oobsize);
+               mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
        return type;
 }
 
@@ -4025,6 +3664,39 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 }
 EXPORT_SYMBOL(nand_scan_ident);
 
+/*
+ * Check if the chip configuration meet the datasheet requirements.
+
+ * If our configuration corrects A bits per B bytes and the minimum
+ * required correction level is X bits per Y bytes, then we must ensure
+ * both of the following are true:
+ *
+ * (1) A / B >= X / Y
+ * (2) A >= X
+ *
+ * Requirement (1) ensures we can correct for the required bitflip density.
+ * Requirement (2) ensures we can correct even when all bitflips are clumped
+ * in the same sector.
+ */
+static bool nand_ecc_strength_good(struct mtd_info *mtd)
+{
+       struct nand_chip *chip = mtd->priv;
+       struct nand_ecc_ctrl *ecc = &chip->ecc;
+       int corr, ds_corr;
+
+       if (ecc->size == 0 || chip->ecc_step_ds == 0)
+               /* Not enough information */
+               return true;
+
+       /*
+        * We get the number of corrected bits per page to compare
+        * the correction density.
+        */
+       corr = (mtd->writesize * ecc->strength) / ecc->size;
+       ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
+
+       return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
+}
 
 /**
  * nand_scan_tail - [NAND Interface] Scan for the NAND device
@@ -4046,18 +3718,7 @@ int nand_scan_tail(struct mtd_info *mtd)
                        !(chip->bbt_options & NAND_BBT_USE_FLASH));
 
        if (!(chip->options & NAND_OWN_BUFFERS)) {
-#ifndef __UBOOT__
-               nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
-                               + mtd->oobsize * 3, GFP_KERNEL);
-               if (!nbuf)
-                       return -ENOMEM;
-               nbuf->ecccalc = (uint8_t *)(nbuf + 1);
-               nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
-               nbuf->databuf = nbuf->ecccode + mtd->oobsize;
-#else
                nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
-#endif
-
                chip->buffers = nbuf;
        } else {
                if (!chip->buffers)
@@ -4103,8 +3764,7 @@ int nand_scan_tail(struct mtd_info *mtd)
        case NAND_ECC_HW_OOB_FIRST:
                /* Similar to NAND_ECC_HW, but a separate read_page handle */
                if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
-                       pr_warn("No ECC functions supplied; "
-                                  "hardware ECC not possible\n");
+                       pr_warn("No ECC functions supplied; hardware ECC not possible\n");
                        BUG();
                }
                if (!ecc->read_page)
@@ -4135,8 +3795,7 @@ int nand_scan_tail(struct mtd_info *mtd)
                     ecc->read_page == nand_read_page_hwecc ||
                     !ecc->write_page ||
                     ecc->write_page == nand_write_page_hwecc)) {
-                       pr_warn("No ECC functions supplied; "
-                                  "hardware ECC not possible\n");
+                       pr_warn("No ECC functions supplied; hardware ECC not possible\n");
                        BUG();
                }
                /* Use standard syndrome read/write page function? */
@@ -4160,9 +3819,8 @@ int nand_scan_tail(struct mtd_info *mtd)
                        }
                        break;
                }
-               pr_warn("%d byte HW ECC not possible on "
-                          "%d byte page size, fallback to SW ECC\n",
-                          ecc->size, mtd->writesize);
+               pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
+                       ecc->size, mtd->writesize);
                ecc->mode = NAND_ECC_SOFT;
 
        case NAND_ECC_SOFT:
@@ -4196,27 +3854,28 @@ int nand_scan_tail(struct mtd_info *mtd)
                ecc->read_oob = nand_read_oob_std;
                ecc->write_oob = nand_write_oob_std;
                /*
-                * Board driver should supply ecc.size and ecc.bytes values to
-                * select how many bits are correctable; see nand_bch_init()
-                * for details. Otherwise, default to 4 bits for large page
-                * devices.
+                * Board driver should supply ecc.size and ecc.strength values
+                * to select how many bits are correctable. Otherwise, default
+                * to 4 bits for large page devices.
                 */
                if (!ecc->size && (mtd->oobsize >= 64)) {
                        ecc->size = 512;
-                       ecc->bytes = 7;
+                       ecc->strength = 4;
                }
+
+               /* See nand_bch_init() for details. */
+               ecc->bytes = DIV_ROUND_UP(
+                               ecc->strength * fls(8 * ecc->size), 8);
                ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
                                               &ecc->layout);
                if (!ecc->priv) {
                        pr_warn("BCH ECC initialization failed!\n");
                        BUG();
                }
-               ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
                break;
 
        case NAND_ECC_NONE:
-               pr_warn("NAND_ECC_NONE selected by board driver. "
-                          "This is not recommended!\n");
+               pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
                ecc->read_page = nand_read_page_raw;
                ecc->write_page = nand_write_page_raw;
                ecc->read_oob = nand_read_oob_std;
@@ -4249,6 +3908,11 @@ int nand_scan_tail(struct mtd_info *mtd)
                ecc->layout->oobavail += ecc->layout->oobfree[i].length;
        mtd->oobavail = ecc->layout->oobavail;
 
+       /* ECC sanity check: warn if it's too weak */
+       if (!nand_ecc_strength_good(mtd))
+               pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
+                       mtd->name);
+
        /*
         * Set the number of read / write steps for one page depending on ECC
         * mode.
@@ -4256,6 +3920,8 @@ int nand_scan_tail(struct mtd_info *mtd)
        ecc->steps = mtd->writesize / ecc->size;
        if (ecc->steps * ecc->size != mtd->writesize) {
                pr_warn("Invalid ECC parameters\n");
+               pr_warn("steps=%d size=%d writesize=%d\n",
+                       chip->ecc.steps, chip->ecc.size, mtd->writesize);
                BUG();
        }
        ecc->total = ecc->steps * ecc->bytes;
@@ -4282,18 +3948,22 @@ int nand_scan_tail(struct mtd_info *mtd)
        chip->pagebuf = -1;
 
        /* Large page NAND with SOFT_ECC should support subpage reads */
-       if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
-               chip->options |= NAND_SUBPAGE_READ;
+       switch (ecc->mode) {
+       case NAND_ECC_SOFT:
+       case NAND_ECC_SOFT_BCH:
+               if (chip->page_shift > 9)
+                       chip->options |= NAND_SUBPAGE_READ;
+               break;
+
+       default:
+               break;
+       }
 
        /* Fill in remaining MTD driver data */
        mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
        mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
                                                MTD_CAP_NANDFLASH;
        mtd->_erase = nand_erase;
-#ifndef __UBOOT__
-       mtd->_point = NULL;
-       mtd->_unpoint = NULL;
-#endif
        mtd->_read = nand_read;
        mtd->_write = nand_write;
        mtd->_panic_write = panic_nand_write;
@@ -4302,10 +3972,7 @@ int nand_scan_tail(struct mtd_info *mtd)
        mtd->_sync = nand_sync;
        mtd->_lock = NULL;
        mtd->_unlock = NULL;
-#ifndef __UBOOT__
-       mtd->_suspend = nand_suspend;
-       mtd->_resume = nand_resume;
-#endif
+       mtd->_block_isreserved = nand_block_isreserved;
        mtd->_block_isbad = nand_block_isbad;
        mtd->_block_markbad = nand_block_markbad;
        mtd->writebufsize = mtd->writesize;
@@ -4320,14 +3987,9 @@ int nand_scan_tail(struct mtd_info *mtd)
         * properly set.
         */
        if (!mtd->bitflip_threshold)
-               mtd->bitflip_threshold = mtd->ecc_strength;
+               mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
 
-       /* Check, if we should skip the bad block table scan */
-       if (chip->options & NAND_SKIP_BBTSCAN)
-               return 0;
-
-       /* Build bad block table */
-       return chip->scan_bbt(mtd);
+       return 0;
 }
 EXPORT_SYMBOL(nand_scan_tail);
 
@@ -4370,44 +4032,6 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
 }
 EXPORT_SYMBOL(nand_scan);
 
-#ifndef __UBOOT__
-/**
- * nand_release - [NAND Interface] Free resources held by the NAND device
- * @mtd: MTD device structure
- */
-void nand_release(struct mtd_info *mtd)
-{
-       struct nand_chip *chip = mtd->priv;
-
-       if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
-               nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
-
-       mtd_device_unregister(mtd);
-
-       /* Free bad block table memory */
-       kfree(chip->bbt);
-       if (!(chip->options & NAND_OWN_BUFFERS))
-               kfree(chip->buffers);
-
-       /* Free bad block descriptor memory */
-       if (chip->badblock_pattern && chip->badblock_pattern->options
-                       & NAND_BBT_DYNAMICSTRUCT)
-               kfree(chip->badblock_pattern);
-}
-EXPORT_SYMBOL_GPL(nand_release);
-
-static int __init nand_base_init(void)
-{
-       led_trigger_register_simple("nand-disk", &nand_led_trigger);
-       return 0;
-}
-
-static void __exit nand_base_exit(void)
-{
-       led_trigger_unregister_simple(nand_led_trigger);
-}
-#endif
-
 module_init(nand_base_init);
 module_exit(nand_base_exit);