nand_toshiba.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2017 Free Electrons
  3. * Copyright (C) 2017 NextThing Co
  4. *
  5. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include "internals.h"
  18. /* Bit for detecting BENAND */
  19. #define TOSHIBA_NAND_ID4_IS_BENAND BIT(7)
  20. /* Recommended to rewrite for BENAND */
  21. #define TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED BIT(3)
  22. static int toshiba_nand_benand_eccstatus(struct nand_chip *chip)
  23. {
  24. struct mtd_info *mtd = nand_to_mtd(chip);
  25. int ret;
  26. unsigned int max_bitflips = 0;
  27. u8 status;
  28. /* Check Status */
  29. ret = nand_status_op(chip, &status);
  30. if (ret)
  31. return ret;
  32. if (status & NAND_STATUS_FAIL) {
  33. /* uncorrected */
  34. mtd->ecc_stats.failed++;
  35. } else if (status & TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED) {
  36. /* corrected */
  37. max_bitflips = mtd->bitflip_threshold;
  38. mtd->ecc_stats.corrected += max_bitflips;
  39. }
  40. return max_bitflips;
  41. }
  42. static int
  43. toshiba_nand_read_page_benand(struct nand_chip *chip, uint8_t *buf,
  44. int oob_required, int page)
  45. {
  46. int ret;
  47. ret = nand_read_page_raw(chip, buf, oob_required, page);
  48. if (ret)
  49. return ret;
  50. return toshiba_nand_benand_eccstatus(chip);
  51. }
  52. static int
  53. toshiba_nand_read_subpage_benand(struct nand_chip *chip, uint32_t data_offs,
  54. uint32_t readlen, uint8_t *bufpoi, int page)
  55. {
  56. int ret;
  57. ret = nand_read_page_op(chip, page, data_offs,
  58. bufpoi + data_offs, readlen);
  59. if (ret)
  60. return ret;
  61. return toshiba_nand_benand_eccstatus(chip);
  62. }
  63. static void toshiba_nand_benand_init(struct nand_chip *chip)
  64. {
  65. struct mtd_info *mtd = nand_to_mtd(chip);
  66. /*
  67. * On BENAND, the entire OOB region can be used by the MTD user.
  68. * The calculated ECC bytes are stored into other isolated
  69. * area which is not accessible to users.
  70. * This is why chip->ecc.bytes = 0.
  71. */
  72. chip->ecc.bytes = 0;
  73. chip->ecc.size = 512;
  74. chip->ecc.strength = 8;
  75. chip->ecc.read_page = toshiba_nand_read_page_benand;
  76. chip->ecc.read_subpage = toshiba_nand_read_subpage_benand;
  77. chip->ecc.write_page = nand_write_page_raw;
  78. chip->ecc.read_page_raw = nand_read_page_raw_notsupp;
  79. chip->ecc.write_page_raw = nand_write_page_raw_notsupp;
  80. chip->options |= NAND_SUBPAGE_READ;
  81. mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  82. }
  83. static void toshiba_nand_decode_id(struct nand_chip *chip)
  84. {
  85. struct mtd_info *mtd = nand_to_mtd(chip);
  86. nand_decode_ext_id(chip);
  87. /*
  88. * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
  89. * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
  90. * follows:
  91. * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
  92. * 110b -> 24nm
  93. * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
  94. */
  95. if (chip->id.len >= 6 && nand_is_slc(chip) &&
  96. (chip->id.data[5] & 0x7) == 0x6 /* 24nm */ &&
  97. !(chip->id.data[4] & 0x80) /* !BENAND */)
  98. mtd->oobsize = 32 * mtd->writesize >> 9;
  99. /*
  100. * Extract ECC requirements from 6th id byte.
  101. * For Toshiba SLC, ecc requrements are as follows:
  102. * - 43nm: 1 bit ECC for each 512Byte is required.
  103. * - 32nm: 4 bit ECC for each 512Byte is required.
  104. * - 24nm: 8 bit ECC for each 512Byte is required.
  105. */
  106. if (chip->id.len >= 6 && nand_is_slc(chip)) {
  107. chip->ecc_step_ds = 512;
  108. switch (chip->id.data[5] & 0x7) {
  109. case 0x4:
  110. chip->ecc_strength_ds = 1;
  111. break;
  112. case 0x5:
  113. chip->ecc_strength_ds = 4;
  114. break;
  115. case 0x6:
  116. chip->ecc_strength_ds = 8;
  117. break;
  118. default:
  119. WARN(1, "Could not get ECC info");
  120. chip->ecc_step_ds = 0;
  121. break;
  122. }
  123. }
  124. }
  125. static int toshiba_nand_init(struct nand_chip *chip)
  126. {
  127. if (nand_is_slc(chip))
  128. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  129. /* Check that chip is BENAND and ECC mode is on-die */
  130. if (nand_is_slc(chip) && chip->ecc.mode == NAND_ECC_ON_DIE &&
  131. chip->id.data[4] & TOSHIBA_NAND_ID4_IS_BENAND)
  132. toshiba_nand_benand_init(chip);
  133. return 0;
  134. }
  135. const struct nand_manufacturer_ops toshiba_nand_manuf_ops = {
  136. .detect = toshiba_nand_decode_id,
  137. .init = toshiba_nand_init,
  138. };