nand_micron.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <linux/mtd/rawnand.h>
  18. /*
  19. * Special Micron status bit that indicates when the block has been
  20. * corrected by on-die ECC and should be rewritten
  21. */
  22. #define NAND_STATUS_WRITE_RECOMMENDED BIT(3)
  23. struct nand_onfi_vendor_micron {
  24. u8 two_plane_read;
  25. u8 read_cache;
  26. u8 read_unique_id;
  27. u8 dq_imped;
  28. u8 dq_imped_num_settings;
  29. u8 dq_imped_feat_addr;
  30. u8 rb_pulldown_strength;
  31. u8 rb_pulldown_strength_feat_addr;
  32. u8 rb_pulldown_strength_num_settings;
  33. u8 otp_mode;
  34. u8 otp_page_start;
  35. u8 otp_data_prot_addr;
  36. u8 otp_num_pages;
  37. u8 otp_feat_addr;
  38. u8 read_retry_options;
  39. u8 reserved[72];
  40. u8 param_revision;
  41. } __packed;
  42. static int micron_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
  43. {
  44. struct nand_chip *chip = mtd_to_nand(mtd);
  45. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  46. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  47. feature);
  48. }
  49. /*
  50. * Configure chip properties from Micron vendor-specific ONFI table
  51. */
  52. static int micron_nand_onfi_init(struct nand_chip *chip)
  53. {
  54. struct nand_onfi_params *p = &chip->onfi_params;
  55. struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
  56. if (!chip->onfi_version)
  57. return 0;
  58. if (le16_to_cpu(p->vendor_revision) < 1)
  59. return 0;
  60. chip->read_retries = micron->read_retry_options;
  61. chip->setup_read_retry = micron_nand_setup_read_retry;
  62. return 0;
  63. }
  64. static int micron_nand_on_die_ooblayout_ecc(struct mtd_info *mtd, int section,
  65. struct mtd_oob_region *oobregion)
  66. {
  67. if (section >= 4)
  68. return -ERANGE;
  69. oobregion->offset = (section * 16) + 8;
  70. oobregion->length = 8;
  71. return 0;
  72. }
  73. static int micron_nand_on_die_ooblayout_free(struct mtd_info *mtd, int section,
  74. struct mtd_oob_region *oobregion)
  75. {
  76. if (section >= 4)
  77. return -ERANGE;
  78. oobregion->offset = (section * 16) + 2;
  79. oobregion->length = 6;
  80. return 0;
  81. }
  82. static const struct mtd_ooblayout_ops micron_nand_on_die_ooblayout_ops = {
  83. .ecc = micron_nand_on_die_ooblayout_ecc,
  84. .free = micron_nand_on_die_ooblayout_free,
  85. };
  86. static int micron_nand_on_die_ecc_setup(struct nand_chip *chip, bool enable)
  87. {
  88. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
  89. if (enable)
  90. feature[0] |= ONFI_FEATURE_ON_DIE_ECC_EN;
  91. return chip->onfi_set_features(nand_to_mtd(chip), chip,
  92. ONFI_FEATURE_ON_DIE_ECC, feature);
  93. }
  94. static int
  95. micron_nand_read_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
  96. uint8_t *buf, int oob_required,
  97. int page)
  98. {
  99. u8 status;
  100. int ret, max_bitflips = 0;
  101. ret = micron_nand_on_die_ecc_setup(chip, true);
  102. if (ret)
  103. return ret;
  104. ret = nand_read_page_op(chip, page, 0, NULL, 0);
  105. if (ret)
  106. goto out;
  107. ret = nand_status_op(chip, &status);
  108. if (ret)
  109. goto out;
  110. ret = nand_exit_status_op(chip);
  111. if (ret)
  112. goto out;
  113. if (status & NAND_STATUS_FAIL)
  114. mtd->ecc_stats.failed++;
  115. /*
  116. * The internal ECC doesn't tell us the number of bitflips
  117. * that have been corrected, but tells us if it recommends to
  118. * rewrite the block. If it's the case, then we pretend we had
  119. * a number of bitflips equal to the ECC strength, which will
  120. * hint the NAND core to rewrite the block.
  121. */
  122. else if (status & NAND_STATUS_WRITE_RECOMMENDED)
  123. max_bitflips = chip->ecc.strength;
  124. ret = nand_read_data_op(chip, buf, mtd->writesize, false);
  125. if (!ret && oob_required)
  126. ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
  127. false);
  128. out:
  129. micron_nand_on_die_ecc_setup(chip, false);
  130. return ret ? ret : max_bitflips;
  131. }
  132. static int
  133. micron_nand_write_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
  134. const uint8_t *buf, int oob_required,
  135. int page)
  136. {
  137. int ret;
  138. ret = micron_nand_on_die_ecc_setup(chip, true);
  139. if (ret)
  140. return ret;
  141. ret = nand_write_page_raw(mtd, chip, buf, oob_required, page);
  142. micron_nand_on_die_ecc_setup(chip, false);
  143. return ret;
  144. }
  145. enum {
  146. /* The NAND flash doesn't support on-die ECC */
  147. MICRON_ON_DIE_UNSUPPORTED,
  148. /*
  149. * The NAND flash supports on-die ECC and it can be
  150. * enabled/disabled by a set features command.
  151. */
  152. MICRON_ON_DIE_SUPPORTED,
  153. /*
  154. * The NAND flash supports on-die ECC, and it cannot be
  155. * disabled.
  156. */
  157. MICRON_ON_DIE_MANDATORY,
  158. };
  159. /*
  160. * Try to detect if the NAND support on-die ECC. To do this, we enable
  161. * the feature, and read back if it has been enabled as expected. We
  162. * also check if it can be disabled, because some Micron NANDs do not
  163. * allow disabling the on-die ECC and we don't support such NANDs for
  164. * now.
  165. *
  166. * This function also has the side effect of disabling on-die ECC if
  167. * it had been left enabled by the firmware/bootloader.
  168. */
  169. static int micron_supports_on_die_ecc(struct nand_chip *chip)
  170. {
  171. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
  172. int ret;
  173. if (chip->onfi_version == 0)
  174. return MICRON_ON_DIE_UNSUPPORTED;
  175. if (chip->bits_per_cell != 1)
  176. return MICRON_ON_DIE_UNSUPPORTED;
  177. ret = micron_nand_on_die_ecc_setup(chip, true);
  178. if (ret)
  179. return MICRON_ON_DIE_UNSUPPORTED;
  180. chip->onfi_get_features(nand_to_mtd(chip), chip,
  181. ONFI_FEATURE_ON_DIE_ECC, feature);
  182. if ((feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) == 0)
  183. return MICRON_ON_DIE_UNSUPPORTED;
  184. ret = micron_nand_on_die_ecc_setup(chip, false);
  185. if (ret)
  186. return MICRON_ON_DIE_UNSUPPORTED;
  187. chip->onfi_get_features(nand_to_mtd(chip), chip,
  188. ONFI_FEATURE_ON_DIE_ECC, feature);
  189. if (feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN)
  190. return MICRON_ON_DIE_MANDATORY;
  191. /*
  192. * Some Micron NANDs have an on-die ECC of 4/512, some other
  193. * 8/512. We only support the former.
  194. */
  195. if (chip->onfi_params.ecc_bits != 4)
  196. return MICRON_ON_DIE_UNSUPPORTED;
  197. return MICRON_ON_DIE_SUPPORTED;
  198. }
  199. static int micron_nand_init(struct nand_chip *chip)
  200. {
  201. struct mtd_info *mtd = nand_to_mtd(chip);
  202. int ondie;
  203. int ret;
  204. ret = micron_nand_onfi_init(chip);
  205. if (ret)
  206. return ret;
  207. if (mtd->writesize == 2048)
  208. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  209. ondie = micron_supports_on_die_ecc(chip);
  210. if (ondie == MICRON_ON_DIE_MANDATORY) {
  211. pr_err("On-die ECC forcefully enabled, not supported\n");
  212. return -EINVAL;
  213. }
  214. if (chip->ecc.mode == NAND_ECC_ON_DIE) {
  215. if (ondie == MICRON_ON_DIE_UNSUPPORTED) {
  216. pr_err("On-die ECC selected but not supported\n");
  217. return -EINVAL;
  218. }
  219. chip->ecc.bytes = 8;
  220. chip->ecc.size = 512;
  221. chip->ecc.strength = 4;
  222. chip->ecc.algo = NAND_ECC_BCH;
  223. chip->ecc.read_page = micron_nand_read_page_on_die_ecc;
  224. chip->ecc.write_page = micron_nand_write_page_on_die_ecc;
  225. chip->ecc.read_page_raw = nand_read_page_raw;
  226. chip->ecc.write_page_raw = nand_write_page_raw;
  227. mtd_set_ooblayout(mtd, &micron_nand_on_die_ooblayout_ops);
  228. }
  229. return 0;
  230. }
  231. const struct nand_manufacturer_ops micron_nand_manuf_ops = {
  232. .init = micron_nand_init,
  233. };