aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand/nand_base.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/nand/nand_base.c')
-rw-r--r--drivers/mtd/nand/nand_base.c183
1 files changed, 160 insertions, 23 deletions
diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index bd39f7b67906..9715a7ba164a 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -29,6 +29,8 @@
29 * 29 *
30 */ 30 */
31 31
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
32#include <linux/module.h> 34#include <linux/module.h>
33#include <linux/delay.h> 35#include <linux/delay.h>
34#include <linux/errno.h> 36#include <linux/errno.h>
@@ -202,6 +204,51 @@ static void nand_select_chip(struct mtd_info *mtd, int chipnr)
202} 204}
203 205
204/** 206/**
207 * nand_write_byte - [DEFAULT] write single byte to chip
208 * @mtd: MTD device structure
209 * @byte: value to write
210 *
211 * Default function to write a byte to I/O[7:0]
212 */
213static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
214{
215 struct nand_chip *chip = mtd->priv;
216
217 chip->write_buf(mtd, &byte, 1);
218}
219
220/**
221 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
222 * @mtd: MTD device structure
223 * @byte: value to write
224 *
225 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
226 */
227static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
228{
229 struct nand_chip *chip = mtd->priv;
230 uint16_t word = byte;
231
232 /*
233 * It's not entirely clear what should happen to I/O[15:8] when writing
234 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
235 *
236 * When the host supports a 16-bit bus width, only data is
237 * transferred at the 16-bit width. All address and command line
238 * transfers shall use only the lower 8-bits of the data bus. During
239 * command transfers, the host may place any value on the upper
240 * 8-bits of the data bus. During address transfers, the host shall
241 * set the upper 8-bits of the data bus to 00h.
242 *
243 * One user of the write_byte callback is nand_onfi_set_features. The
244 * four parameters are specified to be written to I/O[7:0], but this is
245 * neither an address nor a command transfer. Let's assume a 0 on the
246 * upper I/O lines is OK.
247 */
248 chip->write_buf(mtd, (uint8_t *)&word, 2);
249}
250
251/**
205 * nand_write_buf - [DEFAULT] write buffer to chip 252 * nand_write_buf - [DEFAULT] write buffer to chip
206 * @mtd: MTD device structure 253 * @mtd: MTD device structure
207 * @buf: data buffer 254 * @buf: data buffer
@@ -1408,6 +1455,30 @@ static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1408} 1455}
1409 1456
1410/** 1457/**
1458 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1459 * @mtd: MTD device structure
1460 * @retry_mode: the retry mode to use
1461 *
1462 * Some vendors supply a special command to shift the Vt threshold, to be used
1463 * when there are too many bitflips in a page (i.e., ECC error). After setting
1464 * a new threshold, the host should retry reading the page.
1465 */
1466static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1467{
1468 struct nand_chip *chip = mtd->priv;
1469
1470 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1471
1472 if (retry_mode >= chip->read_retries)
1473 return -EINVAL;
1474
1475 if (!chip->setup_read_retry)
1476 return -EOPNOTSUPP;
1477
1478 return chip->setup_read_retry(mtd, retry_mode);
1479}
1480
1481/**
1411 * nand_do_read_ops - [INTERN] Read data with ECC 1482 * nand_do_read_ops - [INTERN] Read data with ECC
1412 * @mtd: MTD device structure 1483 * @mtd: MTD device structure
1413 * @from: offset to read from 1484 * @from: offset to read from
@@ -1420,7 +1491,6 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1420{ 1491{
1421 int chipnr, page, realpage, col, bytes, aligned, oob_required; 1492 int chipnr, page, realpage, col, bytes, aligned, oob_required;
1422 struct nand_chip *chip = mtd->priv; 1493 struct nand_chip *chip = mtd->priv;
1423 struct mtd_ecc_stats stats;
1424 int ret = 0; 1494 int ret = 0;
1425 uint32_t readlen = ops->len; 1495 uint32_t readlen = ops->len;
1426 uint32_t oobreadlen = ops->ooblen; 1496 uint32_t oobreadlen = ops->ooblen;
@@ -1429,8 +1499,8 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1429 1499
1430 uint8_t *bufpoi, *oob, *buf; 1500 uint8_t *bufpoi, *oob, *buf;
1431 unsigned int max_bitflips = 0; 1501 unsigned int max_bitflips = 0;
1432 1502 int retry_mode = 0;
1433 stats = mtd->ecc_stats; 1503 bool ecc_fail = false;
1434 1504
1435 chipnr = (int)(from >> chip->chip_shift); 1505 chipnr = (int)(from >> chip->chip_shift);
1436 chip->select_chip(mtd, chipnr); 1506 chip->select_chip(mtd, chipnr);
@@ -1445,6 +1515,8 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1445 oob_required = oob ? 1 : 0; 1515 oob_required = oob ? 1 : 0;
1446 1516
1447 while (1) { 1517 while (1) {
1518 unsigned int ecc_failures = mtd->ecc_stats.failed;
1519
1448 bytes = min(mtd->writesize - col, readlen); 1520 bytes = min(mtd->writesize - col, readlen);
1449 aligned = (bytes == mtd->writesize); 1521 aligned = (bytes == mtd->writesize);
1450 1522
@@ -1452,6 +1524,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1452 if (realpage != chip->pagebuf || oob) { 1524 if (realpage != chip->pagebuf || oob) {
1453 bufpoi = aligned ? buf : chip->buffers->databuf; 1525 bufpoi = aligned ? buf : chip->buffers->databuf;
1454 1526
1527read_retry:
1455 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); 1528 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1456 1529
1457 /* 1530 /*
@@ -1481,7 +1554,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1481 /* Transfer not aligned data */ 1554 /* Transfer not aligned data */
1482 if (!aligned) { 1555 if (!aligned) {
1483 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && 1556 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
1484 !(mtd->ecc_stats.failed - stats.failed) && 1557 !(mtd->ecc_stats.failed - ecc_failures) &&
1485 (ops->mode != MTD_OPS_RAW)) { 1558 (ops->mode != MTD_OPS_RAW)) {
1486 chip->pagebuf = realpage; 1559 chip->pagebuf = realpage;
1487 chip->pagebuf_bitflips = ret; 1560 chip->pagebuf_bitflips = ret;
@@ -1492,8 +1565,6 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1492 memcpy(buf, chip->buffers->databuf + col, bytes); 1565 memcpy(buf, chip->buffers->databuf + col, bytes);
1493 } 1566 }
1494 1567
1495 buf += bytes;
1496
1497 if (unlikely(oob)) { 1568 if (unlikely(oob)) {
1498 int toread = min(oobreadlen, max_oobsize); 1569 int toread = min(oobreadlen, max_oobsize);
1499 1570
@@ -1511,6 +1582,25 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1511 else 1582 else
1512 nand_wait_ready(mtd); 1583 nand_wait_ready(mtd);
1513 } 1584 }
1585
1586 if (mtd->ecc_stats.failed - ecc_failures) {
1587 if (retry_mode + 1 < chip->read_retries) {
1588 retry_mode++;
1589 ret = nand_setup_read_retry(mtd,
1590 retry_mode);
1591 if (ret < 0)
1592 break;
1593
1594 /* Reset failures; retry */
1595 mtd->ecc_stats.failed = ecc_failures;
1596 goto read_retry;
1597 } else {
1598 /* No more retry modes; real failure */
1599 ecc_fail = true;
1600 }
1601 }
1602
1603 buf += bytes;
1514 } else { 1604 } else {
1515 memcpy(buf, chip->buffers->databuf + col, bytes); 1605 memcpy(buf, chip->buffers->databuf + col, bytes);
1516 buf += bytes; 1606 buf += bytes;
@@ -1520,6 +1610,14 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1520 1610
1521 readlen -= bytes; 1611 readlen -= bytes;
1522 1612
1613 /* Reset to retry mode 0 */
1614 if (retry_mode) {
1615 ret = nand_setup_read_retry(mtd, 0);
1616 if (ret < 0)
1617 break;
1618 retry_mode = 0;
1619 }
1620
1523 if (!readlen) 1621 if (!readlen)
1524 break; 1622 break;
1525 1623
@@ -1545,7 +1643,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1545 if (ret < 0) 1643 if (ret < 0)
1546 return ret; 1644 return ret;
1547 1645
1548 if (mtd->ecc_stats.failed - stats.failed) 1646 if (ecc_fail)
1549 return -EBADMSG; 1647 return -EBADMSG;
1550 1648
1551 return max_bitflips; 1649 return max_bitflips;
@@ -2716,6 +2814,7 @@ static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2716 int addr, uint8_t *subfeature_param) 2814 int addr, uint8_t *subfeature_param)
2717{ 2815{
2718 int status; 2816 int status;
2817 int i;
2719 2818
2720 if (!chip->onfi_version || 2819 if (!chip->onfi_version ||
2721 !(le16_to_cpu(chip->onfi_params.opt_cmd) 2820 !(le16_to_cpu(chip->onfi_params.opt_cmd)
@@ -2723,7 +2822,9 @@ static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2723 return -EINVAL; 2822 return -EINVAL;
2724 2823
2725 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); 2824 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2726 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN); 2825 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2826 chip->write_byte(mtd, subfeature_param[i]);
2827
2727 status = chip->waitfunc(mtd, chip); 2828 status = chip->waitfunc(mtd, chip);
2728 if (status & NAND_STATUS_FAIL) 2829 if (status & NAND_STATUS_FAIL)
2729 return -EIO; 2830 return -EIO;
@@ -2740,6 +2841,8 @@ static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2740static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, 2841static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2741 int addr, uint8_t *subfeature_param) 2842 int addr, uint8_t *subfeature_param)
2742{ 2843{
2844 int i;
2845
2743 if (!chip->onfi_version || 2846 if (!chip->onfi_version ||
2744 !(le16_to_cpu(chip->onfi_params.opt_cmd) 2847 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2745 & ONFI_OPT_CMD_SET_GET_FEATURES)) 2848 & ONFI_OPT_CMD_SET_GET_FEATURES))
@@ -2749,7 +2852,8 @@ static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2749 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN); 2852 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2750 2853
2751 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); 2854 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2752 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN); 2855 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2856 *subfeature_param++ = chip->read_byte(mtd);
2753 return 0; 2857 return 0;
2754} 2858}
2755 2859
@@ -2812,6 +2916,8 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
2812 chip->block_markbad = nand_default_block_markbad; 2916 chip->block_markbad = nand_default_block_markbad;
2813 if (!chip->write_buf || chip->write_buf == nand_write_buf) 2917 if (!chip->write_buf || chip->write_buf == nand_write_buf)
2814 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; 2918 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2919 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2920 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2815 if (!chip->read_buf || chip->read_buf == nand_read_buf) 2921 if (!chip->read_buf || chip->read_buf == nand_read_buf)
2816 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; 2922 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2817 if (!chip->scan_bbt) 2923 if (!chip->scan_bbt)
@@ -2926,6 +3032,30 @@ ext_out:
2926 return ret; 3032 return ret;
2927} 3033}
2928 3034
3035static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3036{
3037 struct nand_chip *chip = mtd->priv;
3038 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3039
3040 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3041 feature);
3042}
3043
3044/*
3045 * Configure chip properties from Micron vendor-specific ONFI table
3046 */
3047static void nand_onfi_detect_micron(struct nand_chip *chip,
3048 struct nand_onfi_params *p)
3049{
3050 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3051
3052 if (le16_to_cpu(p->vendor_revision) < 1)
3053 return;
3054
3055 chip->read_retries = micron->read_retry_options;
3056 chip->setup_read_retry = nand_setup_read_retry_micron;
3057}
3058
2929/* 3059/*
2930 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. 3060 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
2931 */ 3061 */
@@ -2979,7 +3109,7 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
2979 chip->onfi_version = 10; 3109 chip->onfi_version = 10;
2980 3110
2981 if (!chip->onfi_version) { 3111 if (!chip->onfi_version) {
2982 pr_info("%s: unsupported ONFI version: %d\n", __func__, val); 3112 pr_info("unsupported ONFI version: %d\n", val);
2983 return 0; 3113 return 0;
2984 } 3114 }
2985 3115
@@ -3032,6 +3162,9 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3032 pr_warn("Could not retrieve ONFI ECC requirements\n"); 3162 pr_warn("Could not retrieve ONFI ECC requirements\n");
3033 } 3163 }
3034 3164
3165 if (p->jedec_id == NAND_MFR_MICRON)
3166 nand_onfi_detect_micron(chip, p);
3167
3035 return 1; 3168 return 1;
3036} 3169}
3037 3170
@@ -3152,9 +3285,12 @@ static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3152 mtd->oobsize = 512; 3285 mtd->oobsize = 512;
3153 break; 3286 break;
3154 case 6: 3287 case 6:
3155 default: /* Other cases are "reserved" (unknown) */
3156 mtd->oobsize = 640; 3288 mtd->oobsize = 640;
3157 break; 3289 break;
3290 case 7:
3291 default: /* Other cases are "reserved" (unknown) */
3292 mtd->oobsize = 1024;
3293 break;
3158 } 3294 }
3159 extid >>= 2; 3295 extid >>= 2;
3160 /* Calc blocksize */ 3296 /* Calc blocksize */
@@ -3325,6 +3461,9 @@ static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3325 3461
3326 *busw = type->options & NAND_BUSWIDTH_16; 3462 *busw = type->options & NAND_BUSWIDTH_16;
3327 3463
3464 if (!mtd->name)
3465 mtd->name = type->name;
3466
3328 return true; 3467 return true;
3329 } 3468 }
3330 return false; 3469 return false;
@@ -3372,8 +3511,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
3372 id_data[i] = chip->read_byte(mtd); 3511 id_data[i] = chip->read_byte(mtd);
3373 3512
3374 if (id_data[0] != *maf_id || id_data[1] != *dev_id) { 3513 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
3375 pr_info("%s: second ID read did not match " 3514 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
3376 "%02x,%02x against %02x,%02x\n", __func__,
3377 *maf_id, *dev_id, id_data[0], id_data[1]); 3515 *maf_id, *dev_id, id_data[0], id_data[1]);
3378 return ERR_PTR(-ENODEV); 3516 return ERR_PTR(-ENODEV);
3379 } 3517 }
@@ -3440,10 +3578,10 @@ ident_done:
3440 * Check, if buswidth is correct. Hardware drivers should set 3578 * Check, if buswidth is correct. Hardware drivers should set
3441 * chip correct! 3579 * chip correct!
3442 */ 3580 */
3443 pr_info("NAND device: Manufacturer ID:" 3581 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3444 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, 3582 *maf_id, *dev_id);
3445 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name); 3583 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3446 pr_warn("NAND bus width %d instead %d bit\n", 3584 pr_warn("bus width %d instead %d bit\n",
3447 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, 3585 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3448 busw ? 16 : 8); 3586 busw ? 16 : 8);
3449 return ERR_PTR(-EINVAL); 3587 return ERR_PTR(-EINVAL);
@@ -3472,14 +3610,13 @@ ident_done:
3472 if (mtd->writesize > 512 && chip->cmdfunc == nand_command) 3610 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3473 chip->cmdfunc = nand_command_lp; 3611 chip->cmdfunc = nand_command_lp;
3474 3612
3475 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s)\n", 3613 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3476 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name, 3614 *maf_id, *dev_id);
3615 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3477 chip->onfi_version ? chip->onfi_params.model : type->name); 3616 chip->onfi_version ? chip->onfi_params.model : type->name);
3478 3617 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3479 pr_info("NAND device: %dMiB, %s, page size: %d, OOB size: %d\n",
3480 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", 3618 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3481 mtd->writesize, mtd->oobsize); 3619 mtd->writesize, mtd->oobsize);
3482
3483 return type; 3620 return type;
3484} 3621}
3485 3622
@@ -3535,7 +3672,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3535 chip->select_chip(mtd, -1); 3672 chip->select_chip(mtd, -1);
3536 } 3673 }
3537 if (i > 1) 3674 if (i > 1)
3538 pr_info("%d NAND chips detected\n", i); 3675 pr_info("%d chips detected\n", i);
3539 3676
3540 /* Store the number of chips and calc total size for mtd */ 3677 /* Store the number of chips and calc total size for mtd */
3541 chip->numchips = i; 3678 chip->numchips = i;