]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/mtd/nand/nand_bbt.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6
[linux-2.6-omap-h63xx.git] / drivers / mtd / nand / nand_bbt.c
index 9adc6d62332ae913042880cf32e36d4011dcdfed..9402653eb09b4982ac7d924beda88cb203442741 100644 (file)
@@ -156,7 +156,7 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
 
        while (totlen) {
                len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
-               res = mtd->read_ecc(mtd, from, len, &retlen, buf, NULL, this->autooob);
+               res = mtd->read(mtd, from, len, &retlen, buf);
                if (res < 0) {
                        if (retlen != len) {
                                printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
@@ -176,6 +176,7 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
                                        printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
                                               ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
                                        this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
+                                       mtd->ecc_stats.bbtblocks++;
                                        continue;
                                }
                                /* Leave it for now, if its matured we can move this
@@ -187,6 +188,7 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
                                        this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
                                else
                                        this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
+                               mtd->ecc_stats.badblocks++;
                        }
                }
                totlen -= len;
@@ -230,6 +232,42 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
        return 0;
 }
 
+/*
+ * Scan read raw data from flash
+ */
+static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
+                        size_t len)
+{
+       struct mtd_oob_ops ops;
+
+       ops.mode = MTD_OOB_RAW;
+       ops.ooboffs = 0;
+       ops.ooblen = mtd->oobsize;
+       ops.oobbuf = buf;
+       ops.datbuf = buf;
+       ops.len = len;
+
+       return mtd->read_oob(mtd, offs, &ops);
+}
+
+/*
+ * Scan write data with oob to flash
+ */
+static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
+                         uint8_t *buf, uint8_t *oob)
+{
+       struct mtd_oob_ops ops;
+
+       ops.mode = MTD_OOB_PLACE;
+       ops.ooboffs = 0;
+       ops.ooblen = mtd->oobsize;
+       ops.datbuf = buf;
+       ops.oobbuf = oob;
+       ops.len = len;
+
+       return mtd->write_oob(mtd, offs, &ops);
+}
+
 /**
  * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
  * @mtd:       MTD device structure
@@ -241,27 +279,85 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc
  * We assume that the bbt bits are in consecutive order.
  *
 */
-static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
+static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
+                        struct nand_bbt_descr *td, struct nand_bbt_descr *md)
 {
        struct nand_chip *this = mtd->priv;
 
        /* 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);
-               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]);
+               scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
+                             mtd->writesize);
+               td->version[0] = buf[mtd->writesize + 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 */
        if (md && (md->options & NAND_BBT_VERSION)) {
-               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]);
+               scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
+                             mtd->writesize);
+               md->version[0] = buf[mtd->writesize + md->veroffs];
+               printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
+                      md->pages[0], md->version[0]);
        }
-
        return 1;
 }
 
+/*
+ * Scan a given block full
+ */
+static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
+                          loff_t offs, uint8_t *buf, size_t readlen,
+                          int scanlen, int len)
+{
+       int ret, j;
+
+       ret = scan_read_raw(mtd, buf, offs, readlen);
+       if (ret)
+               return ret;
+
+       for (j = 0; j < len; j++, buf += scanlen) {
+               if (check_pattern(buf, scanlen, mtd->writesize, bd))
+                       return 1;
+       }
+       return 0;
+}
+
+/*
+ * Scan a given block partially
+ */
+static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
+                          loff_t offs, uint8_t *buf, int len)
+{
+       struct mtd_oob_ops ops;
+       int j, ret;
+
+       ops.len = mtd->oobsize;
+       ops.ooblen = mtd->oobsize;
+       ops.oobbuf = buf;
+       ops.ooboffs = 0;
+       ops.datbuf = NULL;
+       ops.mode = MTD_OOB_PLACE;
+
+       for (j = 0; j < len; j++) {
+               /*
+                * Read the full oob until read_oob is fixed to
+                * handle single byte reads for 16 bit
+                * buswidth
+                */
+               ret = mtd->read_oob(mtd, offs, &ops);
+               if (ret)
+                       return ret;
+
+               if (check_short_pattern(buf, bd))
+                       return 1;
+
+               offs += mtd->writesize;
+       }
+       return 0;
+}
+
 /**
  * create_bbt - [GENERIC] Create a bad block table by scanning the device
  * @mtd:       MTD device structure
@@ -273,13 +369,14 @@ static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
  * Create a bad block table by scanning the device
  * for the given good/bad block identify pattern
  */
-static int 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;
+       int i, numblocks, len, scanlen;
        int startblock;
        loff_t from;
-       size_t readlen, ooblen;
+       size_t readlen;
 
        printk(KERN_INFO "Scanning device for bad blocks\n");
 
@@ -294,18 +391,17 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
 
        if (!(bd->options & NAND_BBT_SCANEMPTY)) {
                /* We need only read few bytes from the OOB area */
-               scanlen = ooblen = 0;
+               scanlen = 0;
                readlen = bd->len;
        } else {
                /* Full page content should be read */
-               scanlen = mtd->oobblock + mtd->oobsize;
-               readlen = len * mtd->oobblock;
-               ooblen = len * mtd->oobsize;
+               scanlen = mtd->writesize + mtd->oobsize;
+               readlen = len * mtd->writesize;
        }
 
        if (chip == -1) {
-               /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
-                * makes shifting and masking less painful */
+               /* Note that numblocks is 2 * (real numblocks) here, see i+=2
+                * below as it makes shifting and masking less painful */
                numblocks = mtd->size >> (this->bbt_erase_shift - 1);
                startblock = 0;
                from = 0;
@@ -324,35 +420,22 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
        for (i = startblock; i < numblocks;) {
                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 (!(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;
-                               }
-                       }
+               if (bd->options & NAND_BBT_SCANALLPAGES)
+                       ret = scan_block_full(mtd, bd, from, buf, readlen,
+                                             scanlen, len);
+               else
+                       ret = scan_block_fast(mtd, bd, from, buf, len);
+
+               if (ret < 0)
+                       return ret;
+
+               if (ret) {
+                       this->bbt[i >> 3] |= 0x03 << (i & 0x6);
+                       printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
+                              i >> 1, (unsigned int)from);
+                       mtd->ecc_stats.badblocks++;
                }
+
                i += 2;
                from += (1 << this->bbt_erase_shift);
        }
@@ -381,8 +464,9 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
        struct nand_chip *this = mtd->priv;
        int i, chips;
        int bits, startblock, block, dir;
-       int scanlen = mtd->oobblock + mtd->oobsize;
+       int scanlen = mtd->writesize + mtd->oobsize;
        int bbtblocks;
+       int blocktopage = this->bbt_erase_shift - this->page_shift;
 
        /* Search direction top -> down ? */
        if (td->options & NAND_BBT_LASTBLOCK) {
@@ -412,13 +496,16 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
                td->pages[i] = -1;
                /* Scan the maximum number of blocks */
                for (block = 0; block < td->maxblocks; block++) {
+
                        int actblock = startblock + dir * block;
+                       loff_t offs = actblock << this->bbt_erase_shift;
+
                        /* Read first page */
-                       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);
+                       scan_read_raw(mtd, buf, offs, mtd->writesize);
+                       if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
+                               td->pages[i] = actblock << blocktopage;
                                if (td->options & NAND_BBT_VERSION) {
-                                       td->version[i] = buf[mtd->oobblock + td->veroffs];
+                                       td->version[i] = buf[mtd->writesize + td->veroffs];
                                }
                                break;
                        }
@@ -471,18 +558,24 @@ static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt
  *
 */
 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_bbt_descr *td, struct nand_bbt_descr *md,
+                    int chipsel)
 {
        struct nand_chip *this = mtd->priv;
-       struct nand_oobinfo oobinfo;
        struct erase_info einfo;
        int i, j, res, chip = 0;
        int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
-       int nrchips, bbtoffs, pageoffs;
+       int nrchips, bbtoffs, pageoffs, ooboffs;
        uint8_t msk[4];
        uint8_t rcode = td->reserved_block_code;
        size_t retlen, len = 0;
        loff_t to;
+       struct mtd_oob_ops ops;
+
+       ops.ooblen = mtd->oobsize;
+       ops.ooboffs = 0;
+       ops.datbuf = NULL;
+       ops.mode = MTD_OOB_PLACE;
 
        if (!rcode)
                rcode = 0xff;
@@ -526,12 +619,14 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
                for (i = 0; i < td->maxblocks; i++) {
                        int block = startblock + dir * i;
                        /* Check, if the block is bad */
-                       switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) {
+                       switch ((this->bbt[block >> 2] >>
+                                (2 * (block & 0x03))) & 0x03) {
                        case 0x01:
                        case 0x03:
                                continue;
                        }
-                       page = block << (this->bbt_erase_shift - this->page_shift);
+                       page = block <<
+                               (this->bbt_erase_shift - this->page_shift);
                        /* Check, if the block is used by the mirror table */
                        if (!md || md->pages[chip] != page)
                                goto write;
@@ -542,11 +637,20 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
 
                /* Set up shift count and masks for the flash table */
                bits = td->options & NAND_BBT_NRBITS_MSK;
+               msk[2] = ~rcode;
                switch (bits) {
-               case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break;
-               case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break;
-               case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break;
-               case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break;
+               case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
+                       msk[3] = 0x01;
+                       break;
+               case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
+                       msk[3] = 0x03;
+                       break;
+               case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
+                       msk[3] = 0x0f;
+                       break;
+               case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
+                       msk[3] = 0xff;
+                       break;
                default: return -EINVAL;
                }
 
@@ -554,49 +658,55 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
 
                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 */
                        to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
                        len = 1 << this->bbt_erase_shift;
-                       res = mtd->read_ecc(mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
+                       res = mtd->read(mtd, to, len, &retlen, buf);
                        if (res < 0) {
                                if (retlen != len) {
-                                       printk(KERN_INFO
-                                              "nand_bbt: Error reading block for writing the bad block table\n");
+                                       printk(KERN_INFO "nand_bbt: Error "
+                                              "reading block for writing "
+                                              "the bad block table\n");
                                        return res;
                                }
-                               printk(KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n");
+                               printk(KERN_WARNING "nand_bbt: ECC error "
+                                      "while reading block for writing "
+                                      "bad block table\n");
                        }
+                       /* Read oob data */
+                       ops.len = (len >> this->page_shift) * mtd->oobsize;
+                       ops.oobbuf = &buf[len];
+                       res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
+                       if (res < 0 || ops.retlen != ops.len)
+                               goto outerr;
+
                        /* Calc the byte offset in the buffer */
                        pageoffs = page - (int)(to >> this->page_shift);
                        offs = pageoffs << this->page_shift;
                        /* Preset the bbt area with 0xff */
                        memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
-                       /* Preset the bbt's oob area with 0xff */
-                       memset(&buf[len + pageoffs * mtd->oobsize], 0xff,
-                              ((len >> this->page_shift) - pageoffs) * mtd->oobsize);
-                       if (td->options & NAND_BBT_VERSION) {
-                               buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip];
-                       }
+                       ooboffs = len + (pageoffs * mtd->oobsize);
+
                } else {
                        /* Calc length */
                        len = (size_t) (numblocks >> sft);
                        /* Make it page aligned ! */
-                       len = (len + (mtd->oobblock - 1)) & ~(mtd->oobblock - 1);
+                       len = (len + (mtd->writesize - 1)) &
+                               ~(mtd->writesize - 1);
                        /* Preset the buffer with 0xff */
-                       memset(buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize);
+                       memset(buf, 0xff, len +
+                              (len >> this->page_shift)* mtd->oobsize);
                        offs = 0;
+                       ooboffs = len;
                        /* Pattern is located in oob area of first page */
-                       memcpy(&buf[len + td->offs], td->pattern, td->len);
-                       if (td->options & NAND_BBT_VERSION) {
-                               buf[len + td->veroffs] = td->version[chip];
-                       }
+                       memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
                }
 
+               if (td->options & NAND_BBT_VERSION)
+                       buf[ooboffs + td->veroffs] = td->version[chip];
+
                /* walk through the memory table */
                for (i = 0; i < numblocks;) {
                        uint8_t dat;
@@ -604,7 +714,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
                        for (j = 0; j < 4; j++, i++) {
                                int sftcnt = (i << (3 - sft)) & sftmsk;
                                /* Do not store the reserved bbt blocks ! */
-                               buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt);
+                               buf[offs + (i >> sft)] &=
+                                       ~(msk[dat & 0x03] << sftcnt);
                                dat >>= 2;
                        }
                }
@@ -614,23 +725,25 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
                einfo.addr = (unsigned long)to;
                einfo.len = 1 << this->bbt_erase_shift;
                res = nand_erase_nand(mtd, &einfo, 1);
-               if (res < 0) {
-                       printk(KERN_WARNING "nand_bbt: Error during block erase: %d\n", res);
-                       return res;
-               }
+               if (res < 0)
+                       goto outerr;
 
-               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",
-                      (unsigned int)to, td->version[chip]);
+               res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
+               if (res < 0)
+                       goto outerr;
+
+               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;
+
+ outerr:
+       printk(KERN_WARNING
+              "nand_bbt: Error while writing bad block table %d\n", res);
+       return res;
 }
 
 /**
@@ -646,7 +759,7 @@ static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *b
        struct nand_chip *this = mtd->priv;
 
        bd->options &= ~NAND_BBT_SCANEMPTY;
-       return create_bbt(mtd, this->data_buf, bd, -1);
+       return create_bbt(mtd, this->buffers->databuf, bd, -1);
 }
 
 /**
@@ -1063,13 +1176,13 @@ int nand_default_bbt(struct mtd_info *mtd)
                        this->bbt_md = &bbt_mirror_descr;
                }
                if (!this->badblock_pattern) {
-                       this->badblock_pattern = (mtd->oobblock > 512) ? &largepage_flashbased : &smallpage_flashbased;
+                       this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
                }
        } else {
                this->bbt_td = NULL;
                this->bbt_md = NULL;
                if (!this->badblock_pattern) {
-                       this->badblock_pattern = (mtd->oobblock > 512) ?
+                       this->badblock_pattern = (mtd->writesize > 512) ?
                            &largepage_memorybased : &smallpage_memorybased;
                }
        }