nand_micron.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. int status;
  100. int max_bitflips = 0;
  101. micron_nand_on_die_ecc_setup(chip, true);
  102. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  103. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  104. status = chip->read_byte(mtd);
  105. if (status & NAND_STATUS_FAIL)
  106. mtd->ecc_stats.failed++;
  107. /*
  108. * The internal ECC doesn't tell us the number of bitflips
  109. * that have been corrected, but tells us if it recommends to
  110. * rewrite the block. If it's the case, then we pretend we had
  111. * a number of bitflips equal to the ECC strength, which will
  112. * hint the NAND core to rewrite the block.
  113. */
  114. else if (status & NAND_STATUS_WRITE_RECOMMENDED)
  115. max_bitflips = chip->ecc.strength;
  116. chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
  117. nand_read_page_raw(mtd, chip, buf, oob_required, page);
  118. micron_nand_on_die_ecc_setup(chip, false);
  119. return max_bitflips;
  120. }
  121. static int
  122. micron_nand_write_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
  123. const uint8_t *buf, int oob_required,
  124. int page)
  125. {
  126. int status;
  127. micron_nand_on_die_ecc_setup(chip, true);
  128. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  129. nand_write_page_raw(mtd, chip, buf, oob_required, page);
  130. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  131. status = chip->waitfunc(mtd, chip);
  132. micron_nand_on_die_ecc_setup(chip, false);
  133. return status & NAND_STATUS_FAIL ? -EIO : 0;
  134. }
  135. static int
  136. micron_nand_read_page_raw_on_die_ecc(struct mtd_info *mtd,
  137. struct nand_chip *chip,
  138. uint8_t *buf, int oob_required,
  139. int page)
  140. {
  141. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  142. nand_read_page_raw(mtd, chip, buf, oob_required, page);
  143. return 0;
  144. }
  145. static int
  146. micron_nand_write_page_raw_on_die_ecc(struct mtd_info *mtd,
  147. struct nand_chip *chip,
  148. const uint8_t *buf, int oob_required,
  149. int page)
  150. {
  151. int status;
  152. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  153. nand_write_page_raw(mtd, chip, buf, oob_required, page);
  154. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  155. status = chip->waitfunc(mtd, chip);
  156. return status & NAND_STATUS_FAIL ? -EIO : 0;
  157. }
  158. enum {
  159. /* The NAND flash doesn't support on-die ECC */
  160. MICRON_ON_DIE_UNSUPPORTED,
  161. /*
  162. * The NAND flash supports on-die ECC and it can be
  163. * enabled/disabled by a set features command.
  164. */
  165. MICRON_ON_DIE_SUPPORTED,
  166. /*
  167. * The NAND flash supports on-die ECC, and it cannot be
  168. * disabled.
  169. */
  170. MICRON_ON_DIE_MANDATORY,
  171. };
  172. /*
  173. * Try to detect if the NAND support on-die ECC. To do this, we enable
  174. * the feature, and read back if it has been enabled as expected. We
  175. * also check if it can be disabled, because some Micron NANDs do not
  176. * allow disabling the on-die ECC and we don't support such NANDs for
  177. * now.
  178. *
  179. * This function also has the side effect of disabling on-die ECC if
  180. * it had been left enabled by the firmware/bootloader.
  181. */
  182. static int micron_supports_on_die_ecc(struct nand_chip *chip)
  183. {
  184. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
  185. int ret;
  186. if (chip->onfi_version == 0)
  187. return MICRON_ON_DIE_UNSUPPORTED;
  188. if (chip->bits_per_cell != 1)
  189. return MICRON_ON_DIE_UNSUPPORTED;
  190. ret = micron_nand_on_die_ecc_setup(chip, true);
  191. if (ret)
  192. return MICRON_ON_DIE_UNSUPPORTED;
  193. chip->onfi_get_features(nand_to_mtd(chip), chip,
  194. ONFI_FEATURE_ON_DIE_ECC, feature);
  195. if ((feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) == 0)
  196. return MICRON_ON_DIE_UNSUPPORTED;
  197. ret = micron_nand_on_die_ecc_setup(chip, false);
  198. if (ret)
  199. return MICRON_ON_DIE_UNSUPPORTED;
  200. chip->onfi_get_features(nand_to_mtd(chip), chip,
  201. ONFI_FEATURE_ON_DIE_ECC, feature);
  202. if (feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN)
  203. return MICRON_ON_DIE_MANDATORY;
  204. /*
  205. * Some Micron NANDs have an on-die ECC of 4/512, some other
  206. * 8/512. We only support the former.
  207. */
  208. if (chip->onfi_params.ecc_bits != 4)
  209. return MICRON_ON_DIE_UNSUPPORTED;
  210. return MICRON_ON_DIE_SUPPORTED;
  211. }
  212. static int micron_nand_init(struct nand_chip *chip)
  213. {
  214. struct mtd_info *mtd = nand_to_mtd(chip);
  215. int ondie;
  216. int ret;
  217. ret = micron_nand_onfi_init(chip);
  218. if (ret)
  219. return ret;
  220. if (mtd->writesize == 2048)
  221. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  222. ondie = micron_supports_on_die_ecc(chip);
  223. if (ondie == MICRON_ON_DIE_MANDATORY) {
  224. pr_err("On-die ECC forcefully enabled, not supported\n");
  225. return -EINVAL;
  226. }
  227. if (chip->ecc.mode == NAND_ECC_ON_DIE) {
  228. if (ondie == MICRON_ON_DIE_UNSUPPORTED) {
  229. pr_err("On-die ECC selected but not supported\n");
  230. return -EINVAL;
  231. }
  232. chip->ecc.options = NAND_ECC_CUSTOM_PAGE_ACCESS;
  233. chip->ecc.bytes = 8;
  234. chip->ecc.size = 512;
  235. chip->ecc.strength = 4;
  236. chip->ecc.algo = NAND_ECC_BCH;
  237. chip->ecc.read_page = micron_nand_read_page_on_die_ecc;
  238. chip->ecc.write_page = micron_nand_write_page_on_die_ecc;
  239. chip->ecc.read_page_raw =
  240. micron_nand_read_page_raw_on_die_ecc;
  241. chip->ecc.write_page_raw =
  242. micron_nand_write_page_raw_on_die_ecc;
  243. mtd_set_ooblayout(mtd, &micron_nand_on_die_ooblayout_ops);
  244. }
  245. return 0;
  246. }
  247. const struct nand_manufacturer_ops micron_nand_manuf_ops = {
  248. .init = micron_nand_init,
  249. };