Add Broadcom / Netgear changes from RAXE 1.0.0.48
[project/bcm63xx/u-boot.git] / drivers / mtd / nand / spi / core.c
index cb8ffa3fa96a16dd40dda3553bbb171107e71df7..5e38211333c6f1cfb41768c51473e2cfbafdbe0f 100644 (file)
@@ -315,24 +315,30 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
        struct nand_device *nand = spinand_to_nand(spinand);
        struct mtd_info *mtd = nanddev_to_mtd(nand);
        struct nand_page_io_req adjreq = *req;
-       unsigned int nbytes = 0;
-       void *buf = NULL;
+       void *buf = spinand->databuf;
+       unsigned int nbytes;
        u16 column = 0;
        int ret;
 
-       memset(spinand->databuf, 0xff,
-              nanddev_page_size(nand) +
-              nanddev_per_page_oobsize(nand));
+       /*
+        * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset
+        * the cache content to 0xFF (depends on vendor implementation), so we
+        * must fill the page cache entirely even if we only want to program
+        * the data portion of the page, otherwise we might corrupt the BBM or
+        * user data previously programmed in OOB area.
+        */
+       nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
+       memset(spinand->databuf, 0xff, nbytes);
+       adjreq.dataoffs = 0;
+       adjreq.datalen = nanddev_page_size(nand);
+       adjreq.databuf.out = spinand->databuf;
+       adjreq.ooblen = nanddev_per_page_oobsize(nand);
+       adjreq.ooboffs = 0;
+       adjreq.oobbuf.out = spinand->oobbuf;
 
-       if (req->datalen) {
+       if (req->datalen)
                memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
                       req->datalen);
-               adjreq.dataoffs = 0;
-               adjreq.datalen = nanddev_page_size(nand);
-               adjreq.databuf.out = spinand->databuf;
-               nbytes = adjreq.datalen;
-               buf = spinand->databuf;
-       }
 
        if (req->ooblen) {
                if (req->mode == MTD_OPS_AUTO_OOB)
@@ -343,14 +349,6 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
                else
                        memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
                               req->ooblen);
-
-               adjreq.ooblen = nanddev_per_page_oobsize(nand);
-               adjreq.ooboffs = 0;
-               nbytes += nanddev_per_page_oobsize(nand);
-               if (!buf) {
-                       buf = spinand->oobbuf;
-                       column = nanddev_page_size(nand);
-               }
        }
 
        spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
@@ -381,8 +379,8 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
 
                /*
                 * We need to use the RANDOM LOAD CACHE operation if there's
-                * more than one iteration, because the LOAD operation resets
-                * the cache to 0xff.
+                * more than one iteration, because the LOAD operation might
+                * reset the cache to 0xff.
                 */
                if (nbytes) {
                        column = op.addr.val;
@@ -589,12 +587,12 @@ static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
                if (ret == -EBADMSG) {
                        ecc_failed = true;
                        mtd->ecc_stats.failed++;
-                       ret = 0;
                } else {
                        mtd->ecc_stats.corrected += ret;
                        max_bitflips = max_t(unsigned int, max_bitflips, ret);
                }
 
+               ret = 0;
                ops->retlen += iter.req.datalen;
                ops->oobretlen += iter.req.ooblen;
        }
@@ -651,16 +649,16 @@ static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
 {
        struct spinand_device *spinand = nand_to_spinand(nand);
+       u8 marker[2] = { };
        struct nand_page_io_req req = {
                .pos = *pos,
-               .ooblen = 2,
+               .ooblen = sizeof(marker),
                .ooboffs = 0,
-               .oobbuf.in = spinand->oobbuf,
+               .oobbuf.in = marker,
                .mode = MTD_OPS_RAW,
        };
        int ret;
 
-       memset(spinand->oobbuf, 0, 2);
        ret = spinand_select_target(spinand, pos->target);
        if (ret)
                return ret;
@@ -669,7 +667,7 @@ static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
        if (ret)
                return ret;
 
-       if (spinand->oobbuf[0] != 0xff || spinand->oobbuf[1] != 0xff)
+       if (hweight8(marker[0]) < 4)
                return true;
 
        return false;
@@ -698,11 +696,13 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
 {
        struct spinand_device *spinand = nand_to_spinand(nand);
+       u8 marker[2] = {0,0};
        struct nand_page_io_req req = {
                .pos = *pos,
                .ooboffs = 0,
-               .ooblen = 2,
-               .oobbuf.out = spinand->oobbuf,
+               .ooblen = sizeof(marker),
+               .oobbuf.out = marker,
+               .mode = MTD_OPS_RAW,
        };
        int ret;
 
@@ -715,11 +715,6 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
        if (ret)
                return ret;
 
-       ret = spinand_erase_op(spinand, pos);
-       if (ret)
-               return ret;
-
-       memset(spinand->oobbuf, 0, 2);
        return spinand_write_page(spinand, &req);
 }
 
@@ -830,6 +825,7 @@ static const struct nand_ops spinand_ops = {
 };
 
 static const struct spinand_manufacturer *spinand_manufacturers[] = {
+       &broadcom_spinand_manufacturer,
        &gigadevice_spinand_manufacturer,
        &macronix_spinand_manufacturer,
        &micron_spinand_manufacturer,