nand_micron.c 14 KB

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