nand_micron.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017 Free Electrons
  4. * Copyright (C) 2017 NextThing Co
  5. *
  6. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  7. */
  8. #include <linux/slab.h>
  9. #include "internals.h"
  10. /*
  11. * Special Micron status bit 3 indicates that the block has been
  12. * corrected by on-die ECC and should be rewritten.
  13. */
  14. #define NAND_ECC_STATUS_WRITE_RECOMMENDED BIT(3)
  15. /*
  16. * On chips with 8-bit ECC and additional bit can be used to distinguish
  17. * cases where a errors were corrected without needing a rewrite
  18. *
  19. * Bit 4 Bit 3 Bit 0 Description
  20. * ----- ----- ----- -----------
  21. * 0 0 0 No Errors
  22. * 0 0 1 Multiple uncorrected errors
  23. * 0 1 0 4 - 6 errors corrected, recommend rewrite
  24. * 0 1 1 Reserved
  25. * 1 0 0 1 - 3 errors corrected
  26. * 1 0 1 Reserved
  27. * 1 1 0 7 - 8 errors corrected, recommend rewrite
  28. */
  29. #define NAND_ECC_STATUS_MASK (BIT(4) | BIT(3) | BIT(0))
  30. #define NAND_ECC_STATUS_UNCORRECTABLE BIT(0)
  31. #define NAND_ECC_STATUS_4_6_CORRECTED BIT(3)
  32. #define NAND_ECC_STATUS_1_3_CORRECTED BIT(4)
  33. #define NAND_ECC_STATUS_7_8_CORRECTED (BIT(4) | BIT(3))
  34. struct nand_onfi_vendor_micron {
  35. u8 two_plane_read;
  36. u8 read_cache;
  37. u8 read_unique_id;
  38. u8 dq_imped;
  39. u8 dq_imped_num_settings;
  40. u8 dq_imped_feat_addr;
  41. u8 rb_pulldown_strength;
  42. u8 rb_pulldown_strength_feat_addr;
  43. u8 rb_pulldown_strength_num_settings;
  44. u8 otp_mode;
  45. u8 otp_page_start;
  46. u8 otp_data_prot_addr;
  47. u8 otp_num_pages;
  48. u8 otp_feat_addr;
  49. u8 read_retry_options;
  50. u8 reserved[72];
  51. u8 param_revision;
  52. } __packed;
  53. struct micron_on_die_ecc {
  54. bool forced;
  55. bool enabled;
  56. void *rawbuf;
  57. };
  58. struct micron_nand {
  59. struct micron_on_die_ecc ecc;
  60. };
  61. static int micron_nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
  62. {
  63. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  64. return nand_set_features(chip, ONFI_FEATURE_ADDR_READ_RETRY, feature);
  65. }
  66. /*
  67. * Configure chip properties from Micron vendor-specific ONFI table
  68. */
  69. static int micron_nand_onfi_init(struct nand_chip *chip)
  70. {
  71. struct nand_parameters *p = &chip->parameters;
  72. if (p->onfi) {
  73. struct nand_onfi_vendor_micron *micron = (void *)p->onfi->vendor;
  74. chip->read_retries = micron->read_retry_options;
  75. chip->ops.setup_read_retry = micron_nand_setup_read_retry;
  76. }
  77. if (p->supports_set_get_features) {
  78. set_bit(ONFI_FEATURE_ADDR_READ_RETRY, p->set_feature_list);
  79. set_bit(ONFI_FEATURE_ON_DIE_ECC, p->set_feature_list);
  80. set_bit(ONFI_FEATURE_ADDR_READ_RETRY, p->get_feature_list);
  81. set_bit(ONFI_FEATURE_ON_DIE_ECC, p->get_feature_list);
  82. }
  83. return 0;
  84. }
  85. static int micron_nand_on_die_4_ooblayout_ecc(struct mtd_info *mtd,
  86. int section,
  87. struct mtd_oob_region *oobregion)
  88. {
  89. if (section >= 4)
  90. return -ERANGE;
  91. oobregion->offset = (section * 16) + 8;
  92. oobregion->length = 8;
  93. return 0;
  94. }
  95. static int micron_nand_on_die_4_ooblayout_free(struct mtd_info *mtd,
  96. int section,
  97. struct mtd_oob_region *oobregion)
  98. {
  99. if (section >= 4)
  100. return -ERANGE;
  101. oobregion->offset = (section * 16) + 2;
  102. oobregion->length = 6;
  103. return 0;
  104. }
  105. static const struct mtd_ooblayout_ops micron_nand_on_die_4_ooblayout_ops = {
  106. .ecc = micron_nand_on_die_4_ooblayout_ecc,
  107. .free = micron_nand_on_die_4_ooblayout_free,
  108. };
  109. static int micron_nand_on_die_8_ooblayout_ecc(struct mtd_info *mtd,
  110. int section,
  111. struct mtd_oob_region *oobregion)
  112. {
  113. struct nand_chip *chip = mtd_to_nand(mtd);
  114. if (section)
  115. return -ERANGE;
  116. oobregion->offset = mtd->oobsize - chip->ecc.total;
  117. oobregion->length = chip->ecc.total;
  118. return 0;
  119. }
  120. static int micron_nand_on_die_8_ooblayout_free(struct mtd_info *mtd,
  121. int section,
  122. struct mtd_oob_region *oobregion)
  123. {
  124. struct nand_chip *chip = mtd_to_nand(mtd);
  125. if (section)
  126. return -ERANGE;
  127. oobregion->offset = 2;
  128. oobregion->length = mtd->oobsize - chip->ecc.total - 2;
  129. return 0;
  130. }
  131. static const struct mtd_ooblayout_ops micron_nand_on_die_8_ooblayout_ops = {
  132. .ecc = micron_nand_on_die_8_ooblayout_ecc,
  133. .free = micron_nand_on_die_8_ooblayout_free,
  134. };
  135. static int micron_nand_on_die_ecc_setup(struct nand_chip *chip, bool enable)
  136. {
  137. struct micron_nand *micron = nand_get_manufacturer_data(chip);
  138. u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
  139. int ret;
  140. if (micron->ecc.forced)
  141. return 0;
  142. if (micron->ecc.enabled == enable)
  143. return 0;
  144. if (enable)
  145. feature[0] |= ONFI_FEATURE_ON_DIE_ECC_EN;
  146. ret = nand_set_features(chip, ONFI_FEATURE_ON_DIE_ECC, feature);
  147. if (!ret)
  148. micron->ecc.enabled = enable;
  149. return ret;
  150. }
  151. static int micron_nand_on_die_ecc_status_4(struct nand_chip *chip, u8 status,
  152. void *buf, int page,
  153. int oob_required)
  154. {
  155. struct micron_nand *micron = nand_get_manufacturer_data(chip);
  156. struct mtd_info *mtd = nand_to_mtd(chip);
  157. unsigned int step, max_bitflips = 0;
  158. bool use_datain = false;
  159. int ret;
  160. if (!(status & NAND_ECC_STATUS_WRITE_RECOMMENDED)) {
  161. if (status & NAND_STATUS_FAIL)
  162. mtd->ecc_stats.failed++;
  163. return 0;
  164. }
  165. /*
  166. * The internal ECC doesn't tell us the number of bitflips that have
  167. * been corrected, but tells us if it recommends to rewrite the block.
  168. * If it's the case, we need to read the page in raw mode and compare
  169. * its content to the corrected version to extract the actual number of
  170. * bitflips.
  171. * But before we do that, we must make sure we have all OOB bytes read
  172. * in non-raw mode, even if the user did not request those bytes.
  173. */
  174. if (!oob_required) {
  175. /*
  176. * We first check which operation is supported by the controller
  177. * before running it. This trick makes it possible to support
  178. * all controllers, even the most constraints, without almost
  179. * any performance hit.
  180. *
  181. * TODO: could be enhanced to avoid repeating the same check
  182. * over and over in the fast path.
  183. */
  184. if (!nand_has_exec_op(chip) ||
  185. !nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false,
  186. true))
  187. use_datain = true;
  188. if (use_datain)
  189. ret = nand_read_data_op(chip, chip->oob_poi,
  190. mtd->oobsize, false, false);
  191. else
  192. ret = nand_change_read_column_op(chip, mtd->writesize,
  193. chip->oob_poi,
  194. mtd->oobsize, false);
  195. if (ret)
  196. return ret;
  197. }
  198. micron_nand_on_die_ecc_setup(chip, false);
  199. ret = nand_read_page_op(chip, page, 0, micron->ecc.rawbuf,
  200. mtd->writesize + mtd->oobsize);
  201. if (ret)
  202. return ret;
  203. for (step = 0; step < chip->ecc.steps; step++) {
  204. unsigned int offs, i, nbitflips = 0;
  205. u8 *rawbuf, *corrbuf;
  206. offs = step * chip->ecc.size;
  207. rawbuf = micron->ecc.rawbuf + offs;
  208. corrbuf = buf + offs;
  209. for (i = 0; i < chip->ecc.size; i++)
  210. nbitflips += hweight8(corrbuf[i] ^ rawbuf[i]);
  211. offs = (step * 16) + 4;
  212. rawbuf = micron->ecc.rawbuf + mtd->writesize + offs;
  213. corrbuf = chip->oob_poi + offs;
  214. for (i = 0; i < chip->ecc.bytes + 4; i++)
  215. nbitflips += hweight8(corrbuf[i] ^ rawbuf[i]);
  216. if (WARN_ON(nbitflips > chip->ecc.strength))
  217. return -EINVAL;
  218. max_bitflips = max(nbitflips, max_bitflips);
  219. mtd->ecc_stats.corrected += nbitflips;
  220. }
  221. return max_bitflips;
  222. }
  223. static int micron_nand_on_die_ecc_status_8(struct nand_chip *chip, u8 status)
  224. {
  225. struct mtd_info *mtd = nand_to_mtd(chip);
  226. /*
  227. * With 8/512 we have more information but still don't know precisely
  228. * how many bit-flips were seen.
  229. */
  230. switch (status & NAND_ECC_STATUS_MASK) {
  231. case NAND_ECC_STATUS_UNCORRECTABLE:
  232. mtd->ecc_stats.failed++;
  233. return 0;
  234. case NAND_ECC_STATUS_1_3_CORRECTED:
  235. mtd->ecc_stats.corrected += 3;
  236. return 3;
  237. case NAND_ECC_STATUS_4_6_CORRECTED:
  238. mtd->ecc_stats.corrected += 6;
  239. /* rewrite recommended */
  240. return 6;
  241. case NAND_ECC_STATUS_7_8_CORRECTED:
  242. mtd->ecc_stats.corrected += 8;
  243. /* rewrite recommended */
  244. return 8;
  245. default:
  246. return 0;
  247. }
  248. }
  249. static int
  250. micron_nand_read_page_on_die_ecc(struct nand_chip *chip, uint8_t *buf,
  251. int oob_required, int page)
  252. {
  253. struct mtd_info *mtd = nand_to_mtd(chip);
  254. bool use_datain = false;
  255. u8 status;
  256. int ret, max_bitflips = 0;
  257. ret = micron_nand_on_die_ecc_setup(chip, true);
  258. if (ret)
  259. return ret;
  260. ret = nand_read_page_op(chip, page, 0, NULL, 0);
  261. if (ret)
  262. goto out;
  263. ret = nand_status_op(chip, &status);
  264. if (ret)
  265. goto out;
  266. /*
  267. * We first check which operation is supported by the controller before
  268. * running it. This trick makes it possible to support all controllers,
  269. * even the most constraints, without almost any performance hit.
  270. *
  271. * TODO: could be enhanced to avoid repeating the same check over and
  272. * over in the fast path.
  273. */
  274. if (!nand_has_exec_op(chip) ||
  275. !nand_read_data_op(chip, buf, mtd->writesize, false, true))
  276. use_datain = true;
  277. if (use_datain) {
  278. ret = nand_exit_status_op(chip);
  279. if (ret)
  280. goto out;
  281. ret = nand_read_data_op(chip, buf, mtd->writesize, false,
  282. false);
  283. if (!ret && oob_required)
  284. ret = nand_read_data_op(chip, chip->oob_poi,
  285. mtd->oobsize, false, false);
  286. } else {
  287. ret = nand_change_read_column_op(chip, 0, buf, mtd->writesize,
  288. false);
  289. if (!ret && oob_required)
  290. ret = nand_change_read_column_op(chip, mtd->writesize,
  291. chip->oob_poi,
  292. mtd->oobsize, false);
  293. }
  294. if (chip->ecc.strength == 4)
  295. max_bitflips = micron_nand_on_die_ecc_status_4(chip, status,
  296. buf, page,
  297. oob_required);
  298. else
  299. max_bitflips = micron_nand_on_die_ecc_status_8(chip, status);
  300. out:
  301. micron_nand_on_die_ecc_setup(chip, false);
  302. return ret ? ret : max_bitflips;
  303. }
  304. static int
  305. micron_nand_write_page_on_die_ecc(struct nand_chip *chip, const uint8_t *buf,
  306. int oob_required, int page)
  307. {
  308. int ret;
  309. ret = micron_nand_on_die_ecc_setup(chip, true);
  310. if (ret)
  311. return ret;
  312. ret = nand_write_page_raw(chip, buf, oob_required, page);
  313. micron_nand_on_die_ecc_setup(chip, false);
  314. return ret;
  315. }
  316. enum {
  317. /* The NAND flash doesn't support on-die ECC */
  318. MICRON_ON_DIE_UNSUPPORTED,
  319. /*
  320. * The NAND flash supports on-die ECC and it can be
  321. * enabled/disabled by a set features command.
  322. */
  323. MICRON_ON_DIE_SUPPORTED,
  324. /*
  325. * The NAND flash supports on-die ECC, and it cannot be
  326. * disabled.
  327. */
  328. MICRON_ON_DIE_MANDATORY,
  329. };
  330. #define MICRON_ID_INTERNAL_ECC_MASK GENMASK(1, 0)
  331. #define MICRON_ID_ECC_ENABLED BIT(7)
  332. /*
  333. * Try to detect if the NAND support on-die ECC. To do this, we enable
  334. * the feature, and read back if it has been enabled as expected. We
  335. * also check if it can be disabled, because some Micron NANDs do not
  336. * allow disabling the on-die ECC and we don't support such NANDs for
  337. * now.
  338. *
  339. * This function also has the side effect of disabling on-die ECC if
  340. * it had been left enabled by the firmware/bootloader.
  341. */
  342. static int micron_supports_on_die_ecc(struct nand_chip *chip)
  343. {
  344. const struct nand_ecc_props *requirements =
  345. nanddev_get_ecc_requirements(&chip->base);
  346. u8 id[5];
  347. int ret;
  348. if (!chip->parameters.onfi)
  349. return MICRON_ON_DIE_UNSUPPORTED;
  350. if (nanddev_bits_per_cell(&chip->base) != 1)
  351. return MICRON_ON_DIE_UNSUPPORTED;
  352. /*
  353. * We only support on-die ECC of 4/512 or 8/512
  354. */
  355. if (requirements->strength != 4 && requirements->strength != 8)
  356. return MICRON_ON_DIE_UNSUPPORTED;
  357. /* 0x2 means on-die ECC is available. */
  358. if (chip->id.len != 5 ||
  359. (chip->id.data[4] & MICRON_ID_INTERNAL_ECC_MASK) != 0x2)
  360. return MICRON_ON_DIE_UNSUPPORTED;
  361. /*
  362. * It seems that there are devices which do not support ECC officially.
  363. * At least the MT29F2G08ABAGA / MT29F2G08ABBGA devices supports
  364. * enabling the ECC feature but don't reflect that to the READ_ID table.
  365. * So we have to guarantee that we disable the ECC feature directly
  366. * after we did the READ_ID table command. Later we can evaluate the
  367. * ECC_ENABLE support.
  368. */
  369. ret = micron_nand_on_die_ecc_setup(chip, true);
  370. if (ret)
  371. return MICRON_ON_DIE_UNSUPPORTED;
  372. ret = nand_readid_op(chip, 0, id, sizeof(id));
  373. if (ret)
  374. return MICRON_ON_DIE_UNSUPPORTED;
  375. ret = micron_nand_on_die_ecc_setup(chip, false);
  376. if (ret)
  377. return MICRON_ON_DIE_UNSUPPORTED;
  378. if (!(id[4] & MICRON_ID_ECC_ENABLED))
  379. return MICRON_ON_DIE_UNSUPPORTED;
  380. ret = nand_readid_op(chip, 0, id, sizeof(id));
  381. if (ret)
  382. return MICRON_ON_DIE_UNSUPPORTED;
  383. if (id[4] & MICRON_ID_ECC_ENABLED)
  384. return MICRON_ON_DIE_MANDATORY;
  385. /*
  386. * We only support on-die ECC of 4/512 or 8/512
  387. */
  388. if (requirements->strength != 4 && requirements->strength != 8)
  389. return MICRON_ON_DIE_UNSUPPORTED;
  390. return MICRON_ON_DIE_SUPPORTED;
  391. }
  392. static int micron_nand_init(struct nand_chip *chip)
  393. {
  394. struct nand_device *base = &chip->base;
  395. const struct nand_ecc_props *requirements =
  396. nanddev_get_ecc_requirements(base);
  397. struct mtd_info *mtd = nand_to_mtd(chip);
  398. struct micron_nand *micron;
  399. int ondie;
  400. int ret;
  401. micron = kzalloc(sizeof(*micron), GFP_KERNEL);
  402. if (!micron)
  403. return -ENOMEM;
  404. nand_set_manufacturer_data(chip, micron);
  405. ret = micron_nand_onfi_init(chip);
  406. if (ret)
  407. goto err_free_manuf_data;
  408. chip->options |= NAND_BBM_FIRSTPAGE;
  409. if (mtd->writesize == 2048)
  410. chip->options |= NAND_BBM_SECONDPAGE;
  411. ondie = micron_supports_on_die_ecc(chip);
  412. if (ondie == MICRON_ON_DIE_MANDATORY &&
  413. chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_DIE) {
  414. pr_err("On-die ECC forcefully enabled, not supported\n");
  415. ret = -EINVAL;
  416. goto err_free_manuf_data;
  417. }
  418. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE) {
  419. if (ondie == MICRON_ON_DIE_UNSUPPORTED) {
  420. pr_err("On-die ECC selected but not supported\n");
  421. ret = -EINVAL;
  422. goto err_free_manuf_data;
  423. }
  424. if (ondie == MICRON_ON_DIE_MANDATORY) {
  425. micron->ecc.forced = true;
  426. micron->ecc.enabled = true;
  427. }
  428. /*
  429. * In case of 4bit on-die ECC, we need a buffer to store a
  430. * page dumped in raw mode so that we can compare its content
  431. * to the same page after ECC correction happened and extract
  432. * the real number of bitflips from this comparison.
  433. * That's not needed for 8-bit ECC, because the status expose
  434. * a better approximation of the number of bitflips in a page.
  435. */
  436. if (requirements->strength == 4) {
  437. micron->ecc.rawbuf = kmalloc(mtd->writesize +
  438. mtd->oobsize,
  439. GFP_KERNEL);
  440. if (!micron->ecc.rawbuf) {
  441. ret = -ENOMEM;
  442. goto err_free_manuf_data;
  443. }
  444. }
  445. if (requirements->strength == 4)
  446. mtd_set_ooblayout(mtd,
  447. &micron_nand_on_die_4_ooblayout_ops);
  448. else
  449. mtd_set_ooblayout(mtd,
  450. &micron_nand_on_die_8_ooblayout_ops);
  451. chip->ecc.bytes = requirements->strength * 2;
  452. chip->ecc.size = 512;
  453. chip->ecc.strength = requirements->strength;
  454. chip->ecc.algo = NAND_ECC_ALGO_BCH;
  455. chip->ecc.read_page = micron_nand_read_page_on_die_ecc;
  456. chip->ecc.write_page = micron_nand_write_page_on_die_ecc;
  457. if (ondie == MICRON_ON_DIE_MANDATORY) {
  458. chip->ecc.read_page_raw = nand_read_page_raw_notsupp;
  459. chip->ecc.write_page_raw = nand_write_page_raw_notsupp;
  460. } else {
  461. if (!chip->ecc.read_page_raw)
  462. chip->ecc.read_page_raw = nand_read_page_raw;
  463. if (!chip->ecc.write_page_raw)
  464. chip->ecc.write_page_raw = nand_write_page_raw;
  465. }
  466. }
  467. return 0;
  468. err_free_manuf_data:
  469. kfree(micron->ecc.rawbuf);
  470. kfree(micron);
  471. return ret;
  472. }
  473. static void micron_nand_cleanup(struct nand_chip *chip)
  474. {
  475. struct micron_nand *micron = nand_get_manufacturer_data(chip);
  476. kfree(micron->ecc.rawbuf);
  477. kfree(micron);
  478. }
  479. static void micron_fixup_onfi_param_page(struct nand_chip *chip,
  480. struct nand_onfi_params *p)
  481. {
  482. /*
  483. * MT29F1G08ABAFAWP-ITE:F and possibly others report 00 00 for the
  484. * revision number field of the ONFI parameter page. Assume ONFI
  485. * version 1.0 if the revision number is 00 00.
  486. */
  487. if (le16_to_cpu(p->revision) == 0)
  488. p->revision = cpu_to_le16(ONFI_VERSION_1_0);
  489. }
  490. const struct nand_manufacturer_ops micron_nand_manuf_ops = {
  491. .init = micron_nand_init,
  492. .cleanup = micron_nand_cleanup,
  493. .fixup_onfi_param_page = micron_fixup_onfi_param_page,
  494. };