nand_micron.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 nand_set_features(chip, ONFI_FEATURE_ADDR_READ_RETRY, feature);
  47. }
  48. /*
  49. * Configure chip properties from Micron vendor-specific ONFI table
  50. */
  51. static int micron_nand_onfi_init(struct nand_chip *chip)
  52. {
  53. struct nand_parameters *p = &chip->parameters;
  54. struct nand_onfi_vendor_micron *micron = (void *)p->onfi.vendor;
  55. if (chip->parameters.onfi.version && p->onfi.vendor_revision) {
  56. chip->read_retries = micron->read_retry_options;
  57. chip->setup_read_retry = micron_nand_setup_read_retry;
  58. }
  59. if (p->supports_set_get_features) {
  60. set_bit(ONFI_FEATURE_ADDR_READ_RETRY, p->set_feature_list);
  61. set_bit(ONFI_FEATURE_ADDR_READ_RETRY, p->get_feature_list);
  62. }
  63. return 0;
  64. }
  65. static int micron_nand_on_die_ooblayout_ecc(struct mtd_info *mtd, int section,
  66. struct mtd_oob_region *oobregion)
  67. {
  68. if (section >= 4)
  69. return -ERANGE;
  70. oobregion->offset = (section * 16) + 8;
  71. oobregion->length = 8;
  72. return 0;
  73. }
  74. static int micron_nand_on_die_ooblayout_free(struct mtd_info *mtd, int section,
  75. struct mtd_oob_region *oobregion)
  76. {
  77. if (section >= 4)
  78. return -ERANGE;
  79. oobregion->offset = (section * 16) + 2;
  80. oobregion->length = 6;
  81. return 0;
  82. }
  83. static const struct mtd_ooblayout_ops micron_nand_on_die_ooblayout_ops = {
  84. .ecc = micron_nand_on_die_ooblayout_ecc,
  85. .free = micron_nand_on_die_ooblayout_free,
  86. };
  87. static int micron_nand_on_die_ecc_setup(struct nand_chip *chip, bool enable)
  88. {
  89. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
  90. if (enable)
  91. feature[0] |= ONFI_FEATURE_ON_DIE_ECC_EN;
  92. return nand_set_features(chip, 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->parameters.onfi.version)
  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. ret = nand_get_features(chip, ONFI_FEATURE_ON_DIE_ECC, feature);
  181. if (ret < 0)
  182. return ret;
  183. if ((feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) == 0)
  184. return MICRON_ON_DIE_UNSUPPORTED;
  185. ret = micron_nand_on_die_ecc_setup(chip, false);
  186. if (ret)
  187. return MICRON_ON_DIE_UNSUPPORTED;
  188. ret = nand_get_features(chip, ONFI_FEATURE_ON_DIE_ECC, feature);
  189. if (ret < 0)
  190. return ret;
  191. if (feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN)
  192. return MICRON_ON_DIE_MANDATORY;
  193. /*
  194. * Some Micron NANDs have an on-die ECC of 4/512, some other
  195. * 8/512. We only support the former.
  196. */
  197. if (chip->ecc_strength_ds != 4)
  198. return MICRON_ON_DIE_UNSUPPORTED;
  199. return MICRON_ON_DIE_SUPPORTED;
  200. }
  201. static int micron_nand_init(struct nand_chip *chip)
  202. {
  203. struct mtd_info *mtd = nand_to_mtd(chip);
  204. int ondie;
  205. int ret;
  206. ret = micron_nand_onfi_init(chip);
  207. if (ret)
  208. return ret;
  209. if (mtd->writesize == 2048)
  210. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  211. ondie = micron_supports_on_die_ecc(chip);
  212. if (ondie == MICRON_ON_DIE_MANDATORY) {
  213. pr_err("On-die ECC forcefully enabled, not supported\n");
  214. return -EINVAL;
  215. }
  216. if (chip->ecc.mode == NAND_ECC_ON_DIE) {
  217. if (ondie == MICRON_ON_DIE_UNSUPPORTED) {
  218. pr_err("On-die ECC selected but not supported\n");
  219. return -EINVAL;
  220. }
  221. chip->ecc.bytes = 8;
  222. chip->ecc.size = 512;
  223. chip->ecc.strength = 4;
  224. chip->ecc.algo = NAND_ECC_BCH;
  225. chip->ecc.read_page = micron_nand_read_page_on_die_ecc;
  226. chip->ecc.write_page = micron_nand_write_page_on_die_ecc;
  227. chip->ecc.read_page_raw = nand_read_page_raw;
  228. chip->ecc.write_page_raw = nand_write_page_raw;
  229. mtd_set_ooblayout(mtd, &micron_nand_on_die_ooblayout_ops);
  230. }
  231. return 0;
  232. }
  233. const struct nand_manufacturer_ops micron_nand_manuf_ops = {
  234. .init = micron_nand_init,
  235. };