Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / mtd / nand / nand_bbt.c
index 9a19497..ca28699 100644 (file)
@@ -3,10 +3,10 @@
  *
  *  Overview:
  *   Bad block table support for the NAND driver
- *   
+ *
  *  Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
  *
- * $Id: nand_bbt.c,v 1.28 2004/11/13 10:19:09 gleixner Exp $
+ * $Id: nand_bbt.c,v 1.36 2005/11/07 11:14:30 gleixner 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
  *
  * Description:
  *
- * When nand_scan_bbt is called, then it tries to find the bad block table 
- * depending on the options in the bbt descriptor(s). If a bbt is found 
- * then the contents are read and the memory based bbt is created. If a 
+ * When nand_scan_bbt is called, then it tries to find the bad block table
+ * depending on the options in the bbt descriptor(s). If a bbt is found
+ * then the contents are read and the memory based bbt is created. If a
  * mirrored bbt is selected then the mirror is searched too and the
- * versions are compared. If the mirror has a greater version number 
+ * versions are compared. If the mirror has a greater version number
  * than the mirror bbt is used to build the memory based bbt.
  * If the tables are not versioned, then we "or" the bad block information.
- * If one of the bbt's is out of date or does not exist it is (re)created. 
- * If no bbt exists at all then the device is scanned for factory marked 
- * good / bad blocks and the bad block tables are created. 
+ * If one of the bbt's is out of date or does not exist it is (re)created.
+ * If no bbt exists at all then the device is scanned for factory marked
+ * good / bad blocks and the bad block tables are created.
  *
- * For manufacturer created bbts like the one found on M-SYS DOC devices 
+ * For manufacturer created bbts like the one found on M-SYS DOC devices
  * the bbt is searched and read but never created
  *
- * The autogenerated bad block table is located in the last good blocks 
- * of the device. The table is mirrored, so it can be updated eventually. 
- * The table is marked in the oob area with an ident pattern and a version 
+ * The autogenerated bad block table is located in the last good blocks
+ * of the device. The table is mirrored, so it can be updated eventually.
+ * The table is marked in the oob area with an ident pattern and a version
  * number which indicates which of both tables is more up to date.
  *
  * The table uses 2 bits per block
  * 01b:                block is marked bad due to wear
  * 10b:                block is reserved (to protect the bbt area)
  * 11b:                block is factory marked bad
- * 
+ *
  * Multichip devices like DOC store the bad block info per floor.
  *
  * Following assumptions are made:
  * - bbts start at a page boundary, if autolocated on a block boundary
  * - the space neccecary for a bbt in FLASH does not exceed a block boundary
- * 
+ *
  */
 
 #include <linux/slab.h>
@@ -62,7 +62,7 @@
 #include <linux/delay.h>
 
 
-/** 
+/**
  * check_pattern - [GENERIC] check if a pattern is in the buffer
  * @buf:       the buffer to search
  * @len:       the length of buffer to search
@@ -77,7 +77,7 @@
 */
 static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
 {
-       int i, end;
+       int i, end = 0;
        uint8_t *p = buf;
 
        end = paglen + td->offs;
@@ -86,18 +86,18 @@ static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_des
                        if (p[i] != 0xff)
                                return -1;
                }
-       }       
+       }
        p += end;
-       
+
        /* Compare the pattern */
        for (i = 0; i < td->len; i++) {
                if (p[i] != td->pattern[i])
                        return -1;
        }
 
-       p += td->len;
-       end += td->len;
        if (td->options & NAND_BBT_SCANEMPTY) {
+               p += td->len;
+               end += td->len;
                for (i = end; i < len; i++) {
                        if (*p++ != 0xff)
                                return -1;
@@ -106,6 +106,29 @@ static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_des
        return 0;
 }
 
+/**
+ * check_short_pattern - [GENERIC] check if a pattern is in the buffer
+ * @buf:       the buffer to search
+ * @td:                search pattern descriptor
+ *
+ * Check for a pattern at the given place. Used to search bad block
+ * tables and good / bad block identifiers. Same as check_pattern, but
+ * no optional empty check
+ *
+*/
+static int check_short_pattern (uint8_t *buf, struct nand_bbt_descr *td)
+{
+       int i;
+       uint8_t *p = buf;
+
+       /* Compare the pattern */
+       for (i = 0; i < td->len; i++) {
+               if (p[td->offs + i] != td->pattern[i])
+                       return -1;
+       }
+       return 0;
+}
+
 /**
  * read_bbt - [GENERIC] Read the bad block table starting from page
  * @mtd:       MTD device structure
@@ -119,7 +142,7 @@ static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_des
  * Read the bad block table starting from page.
  *
  */
-static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num, 
+static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
        int bits, int offs, int reserved_block_code)
 {
        int res, i, j, act = 0;
@@ -130,7 +153,7 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
 
        totlen = (num * bits) >> 3;
        from = ((loff_t)page) << this->page_shift;
-       
+
        while (totlen) {
                len = min (totlen, (size_t) (1 << this->bbt_erase_shift));
                res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob);
@@ -140,7 +163,7 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
                                return res;
                        }
                        printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
-               }       
+               }
 
                /* Analyse data */
                for (i = 0; i < len; i++) {
@@ -160,12 +183,12 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
                                 * message to MTD_DEBUG_LEVEL0 */
                                printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
                                        ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
-                               /* Factory marked bad or worn out ? */  
+                               /* Factory marked bad or worn out ? */
                                if (tmp == 0)
                                        this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
                                else
                                        this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
-                       }       
+                       }
                }
                totlen -= len;
                from += len;
@@ -177,7 +200,7 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
  * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  * @mtd:       MTD device structure
  * @buf:       temporary buffer
- * @td:                descriptor for the bad block table 
+ * @td:                descriptor for the bad block table
  * @chip:      read the table for a specific chip, -1 read all chips.
  *             Applies only if NAND_BBT_PERCHIP option is set
  *
@@ -212,7 +235,7 @@ static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
  * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
  * @mtd:       MTD device structure
  * @buf:       temporary buffer
- * @td:                descriptor for the bad block table 
+ * @td:                descriptor for the bad block table
  * @md:                descriptor for the bad block table mirror
  *
  * Read the bad block table(s) for all chips starting at a given page
@@ -224,16 +247,16 @@ static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_de
 {
        struct nand_chip *this = mtd->priv;
 
-       /* Read the primary version, if available */    
+       /* Read the primary version, if available */
        if (td->options & NAND_BBT_VERSION) {
-               nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); 
+               nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
                td->version[0] = buf[mtd->oobblock + td->veroffs];
                printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]);
        }
 
-       /* Read the mirror version, if available */     
+       /* Read the mirror version, if available */
        if (md && (md->options & NAND_BBT_VERSION)) {
-               nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); 
+               nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
                md->version[0] = buf[mtd->oobblock + md->veroffs];
                printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]);
        }
@@ -252,7 +275,7 @@ static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_de
  * Create a bad block table by scanning the device
  * for the given good/bad block identify pattern
  */
-static void create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
+static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
 {
        struct nand_chip *this = mtd->priv;
        int i, j, numblocks, len, scanlen;
@@ -267,12 +290,20 @@ static void create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
        else {
                if (bd->options & NAND_BBT_SCAN2NDPAGE)
                        len = 2;
-               else    
+               else
                        len = 1;
        }
-       scanlen = mtd->oobblock + mtd->oobsize;
-       readlen = len * mtd->oobblock;
-       ooblen = len * mtd->oobsize;
+
+       if (!(bd->options & NAND_BBT_SCANEMPTY)) {
+               /* We need only read few bytes from the OOB area */
+               scanlen = ooblen = 0;
+               readlen = bd->len;
+       } else {
+               /* Full page content should be read */
+               scanlen = mtd->oobblock + mtd->oobsize;
+               readlen = len * mtd->oobblock;
+               ooblen = len * mtd->oobsize;
+       }
 
        if (chip == -1) {
                /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
@@ -284,27 +315,51 @@ static void create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
                if (chip >= this->numchips) {
                        printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
                                chip + 1, this->numchips);
-                       return
+                       return -EINVAL;
                }
                numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
                startblock = chip * numblocks;
                numblocks += startblock;
                from = startblock << (this->bbt_erase_shift - 1);
        }
-       
+
        for (i = startblock; i < numblocks;) {
-               nand_read_raw (mtd, buf, from, readlen, ooblen);
+               int ret;
+
+               if (bd->options & NAND_BBT_SCANEMPTY)
+                       if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen)))
+                               return ret;
+
                for (j = 0; j < len; j++) {
-                       if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
-                               this->bbt[i >> 3] |= 0x03 << (i & 0x6);
-                               printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n", 
-                                       i >> 1, (unsigned int) from);
-                               break;
+                       if (!(bd->options & NAND_BBT_SCANEMPTY)) {
+                               size_t retlen;
+
+                               /* Read the full oob until read_oob is fixed to
+                                * handle single byte reads for 16 bit buswidth */
+                               ret = mtd->read_oob(mtd, from + j * mtd->oobblock,
+                                                       mtd->oobsize, &retlen, buf);
+                               if (ret)
+                                       return ret;
+
+                               if (check_short_pattern (buf, bd)) {
+                                       this->bbt[i >> 3] |= 0x03 << (i & 0x6);
+                                       printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
+                                               i >> 1, (unsigned int) from);
+                                       break;
+                               }
+                       } else {
+                               if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
+                                       this->bbt[i >> 3] |= 0x03 << (i & 0x6);
+                                       printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
+                                               i >> 1, (unsigned int) from);
+                                       break;
+                               }
                        }
                }
                i += 2;
                from += (1 << this->bbt_erase_shift);
        }
+       return 0;
 }
 
 /**
@@ -314,15 +369,15 @@ static void create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
  * @td:                descriptor for the bad block table
  *
  * Read the bad block table by searching for a given ident pattern.
- * Search is preformed either from the beginning up or from the end of 
+ * Search is preformed either from the beginning up or from the end of
  * the device downwards. The search starts always at the start of a
  * block.
- * If the option NAND_BBT_PERCHIP is given, each chip is searched 
+ * If the option NAND_BBT_PERCHIP is given, each chip is searched
  * for a bbt, which contains the bad block information of this chip.
  * This is neccecary to provide support for certain DOC devices.
  *
- * The bbt ident pattern resides in the oob area of the first page 
- * in a block. 
+ * The bbt ident pattern resides in the oob area of the first page
+ * in a block.
  */
 static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
 {
@@ -337,10 +392,10 @@ static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
                startblock = (mtd->size >> this->bbt_erase_shift) -1;
                dir = -1;
        } else {
-               startblock = 0; 
+               startblock = 0;
                dir = 1;
-       }       
-       
+       }
+
        /* Do we have a bbt per chip ? */
        if (td->options & NAND_BBT_PERCHIP) {
                chips = this->numchips;
@@ -350,19 +405,19 @@ static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
                chips = 1;
                bbtblocks = mtd->size >> this->bbt_erase_shift;
        }
-       
+
        /* Number of bits for each erase block in the bbt */
        bits = td->options & NAND_BBT_NRBITS_MSK;
-       
+
        for (i = 0; i < chips; i++) {
                /* Reset version information */
-               td->version[i] = 0;     
+               td->version[i] = 0;
                td->pages[i] = -1;
                /* Scan the maximum number of blocks */
                for (block = 0; block < td->maxblocks; block++) {
                        int actblock = startblock + dir * block;
                        /* Read first page */
-                       nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize); 
+                       nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize);
                        if (!check_pattern(buf, scanlen, mtd->oobblock, td)) {
                                td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift);
                                if (td->options & NAND_BBT_VERSION) {
@@ -380,46 +435,46 @@ static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
                else
                        printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]);
        }
-       return 0;       
+       return 0;
 }
 
 /**
  * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  * @mtd:       MTD device structure
  * @buf:       temporary buffer
- * @td:                descriptor for the bad block table 
+ * @td:                descriptor for the bad block table
  * @md:                descriptor for the bad block table mirror
  *
  * Search and read the bad block table(s)
 */
-static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf, 
+static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf,
        struct nand_bbt_descr *td, struct nand_bbt_descr *md)
 {
        /* Search the primary table */
        search_bbt (mtd, buf, td);
-               
+
        /* Search the mirror table */
        if (md)
                search_bbt (mtd, buf, md);
-       
+
        /* Force result check */
-       return 1;       
+       return 1;
 }
-       
 
-/** 
+
+/**
  * write_bbt - [GENERIC] (Re)write the bad block table
  *
  * @mtd:       MTD device structure
  * @buf:       temporary buffer
- * @td:                descriptor for the bad block table 
+ * @td:                descriptor for the bad block table
  * @md:                descriptor for the bad block table mirror
  * @chipsel:   selector for a specific chip, -1 for all
  *
  * (Re)write the bad block table
  *
 */
-static int write_bbt (struct mtd_info *mtd, uint8_t *buf, 
+static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
        struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel)
 {
        struct nand_chip *this = mtd->priv;
@@ -438,7 +493,7 @@ static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
        /* Write bad block table per chip rather than per device ? */
        if (td->options & NAND_BBT_PERCHIP) {
                numblocks = (int) (this->chipsize >> this->bbt_erase_shift);
-               /* Full device write or specific chip ? */      
+               /* Full device write or specific chip ? */
                if (chipsel == -1) {
                        nrchips = this->numchips;
                } else {
@@ -448,19 +503,19 @@ static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
        } else {
                numblocks = (int) (mtd->size >> this->bbt_erase_shift);
                nrchips = 1;
-       }       
-       
+       }
+
        /* Loop through the chips */
        for (; chip < nrchips; chip++) {
-               
-               /* There was already a version of the table, reuse the page 
-                * This applies for absolute placement too, as we have the 
+
+               /* There was already a version of the table, reuse the page
+                * This applies for absolute placement too, as we have the
                 * page nr. in td->pages.
                 */
                if (td->pages[chip] != -1) {
                        page = td->pages[chip];
                        goto write;
-               }       
+               }
 
                /* Automatic placement of the bad block table */
                /* Search direction top -> down ? */
@@ -470,7 +525,7 @@ static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
                } else {
                        startblock = chip * numblocks;
                        dir = 1;
-               }       
+               }
 
                for (i = 0; i < td->maxblocks; i++) {
                        int block = startblock + dir * i;
@@ -487,7 +542,7 @@ static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
                }
                printk (KERN_ERR "No space left to write bad block table\n");
                return -ENOSPC;
-write: 
+write:
 
                /* Set up shift count and masks for the flash table */
                bits = td->options & NAND_BBT_NRBITS_MSK;
@@ -498,14 +553,14 @@ write:
                case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break;
                default: return -EINVAL;
                }
-               
+
                bbtoffs = chip * (numblocks >> 2);
-               
+
                to = ((loff_t) page) << this->page_shift;
 
                memcpy (&oobinfo, this->autooob, sizeof(oobinfo));
                oobinfo.useecc = MTD_NANDECC_PLACEONLY;
-               
+
                /* Must we save the block contents ? */
                if (td->options & NAND_BBT_SAVECONTENT) {
                        /* Make it block aligned */
@@ -544,7 +599,7 @@ write:
                                buf[len + td->veroffs] = td->version[chip];
                        }
                }
-       
+
                /* walk through the memory table */
                for (i = 0; i < numblocks; ) {
                        uint8_t dat;
@@ -556,7 +611,7 @@ write:
                                dat >>= 2;
                        }
                }
-               
+
                memset (&einfo, 0, sizeof (einfo));
                einfo.mtd = mtd;
                einfo.addr = (unsigned long) to;
@@ -566,18 +621,18 @@ write:
                        printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res);
                        return res;
                }
-       
+
                res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
                if (res < 0) {
                        printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res);
                        return res;
                }
-               printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n", 
+               printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n",
                        (unsigned int) to, td->version[chip]);
-       
+
                /* Mark it as used */
                td->pages[chip] = page;
-       }       
+       }
        return 0;
 }
 
@@ -586,17 +641,15 @@ write:
  * @mtd:       MTD device structure
  * @bd:                descriptor for the good/bad block search pattern
  *
- * The function creates a memory based bbt by scanning the device 
+ * The function creates a memory based bbt by scanning the device
  * for manufacturer / software marked good / bad blocks
 */
-static int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
+static inline int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
 {
        struct nand_chip *this = mtd->priv;
 
-       /* Ensure that we only scan for the pattern and nothing else */
-       bd->options = 0;
-       create_bbt (mtd, this->data_buf, bd, -1);
-       return 0;
+       bd->options &= ~NAND_BBT_SCANEMPTY;
+       return create_bbt (mtd, this->data_buf, bd, -1);
 }
 
 /**
@@ -620,11 +673,11 @@ static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
        struct nand_bbt_descr *rd, *rd2;
 
        /* Do we have a bbt per chip ? */
-       if (td->options & NAND_BBT_PERCHIP) 
+       if (td->options & NAND_BBT_PERCHIP)
                chips = this->numchips;
-       else 
+       else
                chips = 1;
-       
+
        for (i = 0; i < chips; i++) {
                writeops = 0;
                rd = NULL;
@@ -639,7 +692,7 @@ static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
                        }
 
                        if (td->pages[i] == -1) {
-                               rd = md;                                
+                               rd = md;
                                td->version[i] = md->version[i];
                                writeops = 1;
                                goto writecheck;
@@ -657,7 +710,7 @@ static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
                                if (!(td->options & NAND_BBT_VERSION))
                                        rd2 = md;
                                goto writecheck;
-                       }       
+                       }
 
                        if (((int8_t) (td->version[i] - md->version[i])) > 0) {
                                rd = td;
@@ -682,15 +735,15 @@ static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
 create:
                /* Create the bad block table by scanning the device ? */
                if (!(td->options & NAND_BBT_CREATE))
-                       continue;       
-               
+                       continue;
+
                /* Create the table in memory by scanning the chip(s) */
                create_bbt (mtd, buf, bd, chipsel);
-               
+
                td->version[i] = 1;
                if (md)
-                       md->version[i] = 1;     
-writecheck:    
+                       md->version[i] = 1;
+writecheck:
                /* read back first ? */
                if (rd)
                        read_abs_bbt (mtd, buf, rd, chipsel);
@@ -704,7 +757,7 @@ writecheck:
                        if (res < 0)
                                return res;
                }
-               
+
                /* Write the mirror bad block table to the device ? */
                if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
                        res = write_bbt (mtd, buf, md, td, chipsel);
@@ -712,11 +765,11 @@ writecheck:
                                return res;
                }
        }
-       return 0;       
+       return 0;
 }
 
 /**
- * mark_bbt_regions - [GENERIC] mark the bad block table regions 
+ * mark_bbt_regions - [GENERIC] mark the bad block table regions
  * @mtd:       MTD device structure
  * @td:                bad block table descriptor
  *
@@ -737,14 +790,14 @@ static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
        } else {
                chips = 1;
                nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
-       }       
-       
+       }
+
        for (i = 0; i < chips; i++) {
                if ((td->options & NAND_BBT_ABSPAGE) ||
                    !(td->options & NAND_BBT_WRITE)) {
                        if (td->pages[i] == -1) continue;
                        block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
-                       block <<= 1;            
+                       block <<= 1;
                        oldval = this->bbt[(block >> 3)];
                        newval = oldval | (0x2 << (block & 0x06));
                        this->bbt[(block >> 3)] = newval;
@@ -755,16 +808,16 @@ static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
                update = 0;
                if (td->options & NAND_BBT_LASTBLOCK)
                        block = ((i + 1) * nrblocks) - td->maxblocks;
-               else    
+               else
                        block = i * nrblocks;
-               block <<= 1;    
+               block <<= 1;
                for (j = 0; j < td->maxblocks; j++) {
                        oldval = this->bbt[(block >> 3)];
                        newval = oldval | (0x2 << (block & 0x06));
                        this->bbt[(block >> 3)] = newval;
                        if (oldval != newval) update = 1;
                        block += 2;
-               }       
+               }
                /* If we want reserved blocks to be recorded to flash, and some
                   new ones have been marked, then we need to update the stored
                   bbts.  This should only happen once. */
@@ -778,7 +831,7 @@ static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
  * @mtd:       MTD device structure
  * @bd:                descriptor for the good/bad block search pattern
  *
- * The function checks, if a bad block table(s) is/are already 
+ * The function checks, if a bad block table(s) is/are already
  * available. If not it scans the device for manufacturer
  * marked good / bad blocks and writes the bad block table(s) to
  * the selected place.
@@ -808,8 +861,14 @@ int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
        /* If no primary table decriptor is given, scan the device
         * to build a memory based bad block table
         */
-       if (!td)
-               return nand_memory_bbt(mtd, bd);
+       if (!td) {
+               if ((res = nand_memory_bbt(mtd, bd))) {
+                       printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
+                       kfree (this->bbt);
+                       this->bbt = NULL;
+               }
+               return res;
+       }
 
        /* Allocate a temporary buffer for one eraseblock incl. oob */
        len = (1 << this->bbt_erase_shift);
@@ -821,30 +880,30 @@ int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
                this->bbt = NULL;
                return -ENOMEM;
        }
-       
+
        /* Is the bbt at a given page ? */
        if (td->options & NAND_BBT_ABSPAGE) {
                res = read_abs_bbts (mtd, buf, td, md);
-       } else {        
+       } else {
                /* Search the bad block table using a pattern in oob */
                res = search_read_bbts (mtd, buf, td, md);
-       }       
+       }
 
-       if (res) 
+       if (res)
                res = check_create (mtd, buf, bd);
-       
+
        /* Prevent the bbt regions from erasing / writing */
        mark_bbt_region (mtd, td);
        if (md)
                mark_bbt_region (mtd, md);
-       
+
        kfree (buf);
        return res;
 }
 
 
 /**
- * nand_update_bbt - [NAND Interface] update bad block table(s) 
+ * nand_update_bbt - [NAND Interface] update bad block table(s)
  * @mtd:       MTD device structure
  * @offs:      the offset of the newly marked block
  *
@@ -871,7 +930,7 @@ int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
                printk (KERN_ERR "nand_update_bbt: Out of memory\n");
                return -ENOMEM;
        }
-       
+
        writeops = md != NULL ? 0x03 : 0x01;
 
        /* Do we have a bbt per chip ? */
@@ -885,7 +944,7 @@ int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
 
        td->version[chip]++;
        if (md)
-               md->version[chip]++;    
+               md->version[chip]++;
 
        /* Write the bad block table to the device ? */
        if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
@@ -898,20 +957,17 @@ int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
                res = write_bbt (mtd, buf, md, td, chipsel);
        }
 
-out:   
+out:
        kfree (buf);
        return res;
 }
 
-/* Define some generic bad / good block scan pattern which are used 
- * while scanning a device for factory marked good / bad blocks
- * 
- * The memory based patterns just 
- */
+/* Define some generic bad / good block scan pattern which are used
+ * while scanning a device for factory marked good / bad blocks. */
 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
 
 static struct nand_bbt_descr smallpage_memorybased = {
-       .options = 0,
+       .options = NAND_BBT_SCAN2NDPAGE,
        .offs = 5,
        .len = 1,
        .pattern = scan_ff_pattern
@@ -953,7 +1009,7 @@ static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
 
 static struct nand_bbt_descr bbt_main_descr = {
-       .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 
+       .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
                | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
        .offs = 8,
        .len = 4,
@@ -963,7 +1019,7 @@ static struct nand_bbt_descr bbt_main_descr = {
 };
 
 static struct nand_bbt_descr bbt_mirror_descr = {
-       .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 
+       .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
                | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
        .offs = 8,
        .len = 4,
@@ -973,7 +1029,7 @@ static struct nand_bbt_descr bbt_mirror_descr = {
 };
 
 /**
- * nand_default_bbt - [NAND Interface] Select a default bad block table for the device 
+ * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  * @mtd:       MTD device structure
  *
  * This function selects the default bad block table
@@ -983,29 +1039,29 @@ static struct nand_bbt_descr bbt_mirror_descr = {
 int nand_default_bbt (struct mtd_info *mtd)
 {
        struct nand_chip *this = mtd->priv;
-       
-       /* Default for AG-AND. We must use a flash based 
+
+       /* Default for AG-AND. We must use a flash based
         * bad block table as the devices have factory marked
         * _good_ blocks. Erasing those blocks leads to loss
         * of the good / bad information, so we _must_ store
-        * this information in a good / bad table during 
+        * this information in a good / bad table during
         * startup
        */
        if (this->options & NAND_IS_AND) {
                /* Use the default pattern descriptors */
-               if (!this->bbt_td) {    
+               if (!this->bbt_td) {
                        this->bbt_td = &bbt_main_descr;
                        this->bbt_md = &bbt_mirror_descr;
-               }       
+               }
                this->options |= NAND_USE_FLASH_BBT;
                return nand_scan_bbt (mtd, &agand_flashbased);
        }
-       
-       
+
+
        /* Is a flash based bad block table requested ? */
        if (this->options & NAND_USE_FLASH_BBT) {
-               /* Use the default pattern descriptors */       
-               if (!this->bbt_td) {    
+               /* Use the default pattern descriptors */
+               if (!this->bbt_td) {
                        this->bbt_td = &bbt_main_descr;
                        this->bbt_md = &bbt_mirror_descr;
                }
@@ -1025,7 +1081,7 @@ int nand_default_bbt (struct mtd_info *mtd)
 }
 
 /**
- * nand_isbad_bbt - [NAND Interface] Check if a block is bad 
+ * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  * @mtd:       MTD device structure
  * @offs:      offset in the device
  * @allowbbt:  allow access to bad block table region
@@ -1036,13 +1092,13 @@ int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt)
        struct nand_chip *this = mtd->priv;
        int block;
        uint8_t res;
-       
+
        /* Get block number * 2 */
        block = (int) (offs >> (this->bbt_erase_shift - 1));
        res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
 
-       DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n", 
-               (unsigned int)offs, res, block >> 1);
+       DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
+               (unsigned int)offs, block >> 1, res);
 
        switch ((int)res) {
        case 0x00:      return 0;