core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017 Free Electrons
  4. *
  5. * Authors:
  6. * Boris Brezillon <boris.brezillon@free-electrons.com>
  7. * Peter Pan <peterpandong@micron.com>
  8. */
  9. #define pr_fmt(fmt) "nand: " fmt
  10. #include <linux/module.h>
  11. #include <linux/mtd/nand.h>
  12. /**
  13. * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
  14. * @buf: buffer to test
  15. * @len: buffer length
  16. * @bitflips_threshold: maximum number of bitflips
  17. *
  18. * Check if a buffer contains only 0xff, which means the underlying region
  19. * has been erased and is ready to be programmed.
  20. * The bitflips_threshold specify the maximum number of bitflips before
  21. * considering the region is not erased.
  22. * Note: The logic of this function has been extracted from the memweight
  23. * implementation, except that nand_check_erased_buf function exit before
  24. * testing the whole buffer if the number of bitflips exceed the
  25. * bitflips_threshold value.
  26. *
  27. * Returns a positive number of bitflips less than or equal to
  28. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  29. * threshold.
  30. */
  31. static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
  32. {
  33. const unsigned char *bitmap = buf;
  34. int bitflips = 0;
  35. int weight;
  36. for (; len && ((uintptr_t)bitmap) % sizeof(long);
  37. len--, bitmap++) {
  38. weight = hweight8(*bitmap);
  39. bitflips += BITS_PER_BYTE - weight;
  40. if (unlikely(bitflips > bitflips_threshold))
  41. return -EBADMSG;
  42. }
  43. for (; len >= sizeof(long);
  44. len -= sizeof(long), bitmap += sizeof(long)) {
  45. unsigned long d = *((unsigned long *)bitmap);
  46. if (d == ~0UL)
  47. continue;
  48. weight = hweight_long(d);
  49. bitflips += BITS_PER_LONG - weight;
  50. if (unlikely(bitflips > bitflips_threshold))
  51. return -EBADMSG;
  52. }
  53. for (; len > 0; len--, bitmap++) {
  54. weight = hweight8(*bitmap);
  55. bitflips += BITS_PER_BYTE - weight;
  56. if (unlikely(bitflips > bitflips_threshold))
  57. return -EBADMSG;
  58. }
  59. return bitflips;
  60. }
  61. /**
  62. * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
  63. * 0xff data
  64. * @data: data buffer to test
  65. * @datalen: data length
  66. * @ecc: ECC buffer
  67. * @ecclen: ECC length
  68. * @extraoob: extra OOB buffer
  69. * @extraooblen: extra OOB length
  70. * @bitflips_threshold: maximum number of bitflips
  71. *
  72. * Check if a data buffer and its associated ECC and OOB data contains only
  73. * 0xff pattern, which means the underlying region has been erased and is
  74. * ready to be programmed.
  75. * The bitflips_threshold specify the maximum number of bitflips before
  76. * considering the region as not erased.
  77. *
  78. * Note:
  79. * 1/ ECC algorithms are working on pre-defined block sizes which are usually
  80. * different from the NAND page size. When fixing bitflips, ECC engines will
  81. * report the number of errors per chunk, and the NAND core infrastructure
  82. * expect you to return the maximum number of bitflips for the whole page.
  83. * This is why you should always use this function on a single chunk and
  84. * not on the whole page. After checking each chunk you should update your
  85. * max_bitflips value accordingly.
  86. * 2/ When checking for bitflips in erased pages you should not only check
  87. * the payload data but also their associated ECC data, because a user might
  88. * have programmed almost all bits to 1 but a few. In this case, we
  89. * shouldn't consider the chunk as erased, and checking ECC bytes prevent
  90. * this case.
  91. * 3/ The extraoob argument is optional, and should be used if some of your OOB
  92. * data are protected by the ECC engine.
  93. * It could also be used if you support subpages and want to attach some
  94. * extra OOB data to an ECC chunk.
  95. *
  96. * Returns a positive number of bitflips less than or equal to
  97. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  98. * threshold. In case of success, the passed buffers are filled with 0xff.
  99. */
  100. int nand_check_erased_ecc_chunk(void *data, int datalen,
  101. void *ecc, int ecclen,
  102. void *extraoob, int extraooblen,
  103. int bitflips_threshold)
  104. {
  105. int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
  106. data_bitflips = nand_check_erased_buf(data, datalen,
  107. bitflips_threshold);
  108. if (data_bitflips < 0)
  109. return data_bitflips;
  110. bitflips_threshold -= data_bitflips;
  111. ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
  112. if (ecc_bitflips < 0)
  113. return ecc_bitflips;
  114. bitflips_threshold -= ecc_bitflips;
  115. extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
  116. bitflips_threshold);
  117. if (extraoob_bitflips < 0)
  118. return extraoob_bitflips;
  119. if (data_bitflips)
  120. memset(data, 0xff, datalen);
  121. if (ecc_bitflips)
  122. memset(ecc, 0xff, ecclen);
  123. if (extraoob_bitflips)
  124. memset(extraoob, 0xff, extraooblen);
  125. return data_bitflips + ecc_bitflips + extraoob_bitflips;
  126. }
  127. EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
  128. /**
  129. * nanddev_isbad() - Check if a block is bad
  130. * @nand: NAND device
  131. * @pos: position pointing to the block we want to check
  132. *
  133. * Return: true if the block is bad, false otherwise.
  134. */
  135. bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
  136. {
  137. if (mtd_check_expert_analysis_mode())
  138. return false;
  139. if (nanddev_bbt_is_initialized(nand)) {
  140. unsigned int entry;
  141. int status;
  142. entry = nanddev_bbt_pos_to_entry(nand, pos);
  143. status = nanddev_bbt_get_block_status(nand, entry);
  144. /* Lazy block status retrieval */
  145. if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
  146. if (nand->ops->isbad(nand, pos))
  147. status = NAND_BBT_BLOCK_FACTORY_BAD;
  148. else
  149. status = NAND_BBT_BLOCK_GOOD;
  150. nanddev_bbt_set_block_status(nand, entry, status);
  151. }
  152. if (status == NAND_BBT_BLOCK_WORN ||
  153. status == NAND_BBT_BLOCK_FACTORY_BAD)
  154. return true;
  155. return false;
  156. }
  157. return nand->ops->isbad(nand, pos);
  158. }
  159. EXPORT_SYMBOL_GPL(nanddev_isbad);
  160. /**
  161. * nanddev_markbad() - Mark a block as bad
  162. * @nand: NAND device
  163. * @pos: position of the block to mark bad
  164. *
  165. * Mark a block bad. This function is updating the BBT if available and
  166. * calls the low-level markbad hook (nand->ops->markbad()).
  167. *
  168. * Return: 0 in case of success, a negative error code otherwise.
  169. */
  170. int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
  171. {
  172. struct mtd_info *mtd = nanddev_to_mtd(nand);
  173. unsigned int entry;
  174. int ret = 0;
  175. if (nanddev_isbad(nand, pos))
  176. return 0;
  177. ret = nand->ops->markbad(nand, pos);
  178. if (ret)
  179. pr_warn("failed to write BBM to block @%llx (err = %d)\n",
  180. nanddev_pos_to_offs(nand, pos), ret);
  181. if (!nanddev_bbt_is_initialized(nand))
  182. goto out;
  183. entry = nanddev_bbt_pos_to_entry(nand, pos);
  184. ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
  185. if (ret)
  186. goto out;
  187. ret = nanddev_bbt_update(nand);
  188. out:
  189. if (!ret)
  190. mtd->ecc_stats.badblocks++;
  191. return ret;
  192. }
  193. EXPORT_SYMBOL_GPL(nanddev_markbad);
  194. /**
  195. * nanddev_isreserved() - Check whether an eraseblock is reserved or not
  196. * @nand: NAND device
  197. * @pos: NAND position to test
  198. *
  199. * Checks whether the eraseblock pointed by @pos is reserved or not.
  200. *
  201. * Return: true if the eraseblock is reserved, false otherwise.
  202. */
  203. bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
  204. {
  205. unsigned int entry;
  206. int status;
  207. if (!nanddev_bbt_is_initialized(nand))
  208. return false;
  209. /* Return info from the table */
  210. entry = nanddev_bbt_pos_to_entry(nand, pos);
  211. status = nanddev_bbt_get_block_status(nand, entry);
  212. return status == NAND_BBT_BLOCK_RESERVED;
  213. }
  214. EXPORT_SYMBOL_GPL(nanddev_isreserved);
  215. /**
  216. * nanddev_erase() - Erase a NAND portion
  217. * @nand: NAND device
  218. * @pos: position of the block to erase
  219. *
  220. * Erases the block if it's not bad.
  221. *
  222. * Return: 0 in case of success, a negative error code otherwise.
  223. */
  224. static int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
  225. {
  226. if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
  227. pr_warn("attempt to erase a bad/reserved block @%llx\n",
  228. nanddev_pos_to_offs(nand, pos));
  229. return -EIO;
  230. }
  231. return nand->ops->erase(nand, pos);
  232. }
  233. /**
  234. * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
  235. * @mtd: MTD device
  236. * @einfo: erase request
  237. *
  238. * This is a simple mtd->_erase() implementation iterating over all blocks
  239. * concerned by @einfo and calling nand->ops->erase() on each of them.
  240. *
  241. * Note that mtd->_erase should not be directly assigned to this helper,
  242. * because there's no locking here. NAND specialized layers should instead
  243. * implement there own wrapper around nanddev_mtd_erase() taking the
  244. * appropriate lock before calling nanddev_mtd_erase().
  245. *
  246. * Return: 0 in case of success, a negative error code otherwise.
  247. */
  248. int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
  249. {
  250. struct nand_device *nand = mtd_to_nanddev(mtd);
  251. struct nand_pos pos, last;
  252. int ret;
  253. nanddev_offs_to_pos(nand, einfo->addr, &pos);
  254. nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
  255. while (nanddev_pos_cmp(&pos, &last) <= 0) {
  256. ret = nanddev_erase(nand, &pos);
  257. if (ret) {
  258. einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
  259. return ret;
  260. }
  261. nanddev_pos_next_eraseblock(nand, &pos);
  262. }
  263. return 0;
  264. }
  265. EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
  266. /**
  267. * nanddev_mtd_max_bad_blocks() - Get the maximum number of bad eraseblock on
  268. * a specific region of the NAND device
  269. * @mtd: MTD device
  270. * @offs: offset of the NAND region
  271. * @len: length of the NAND region
  272. *
  273. * Default implementation for mtd->_max_bad_blocks(). Only works if
  274. * nand->memorg.max_bad_eraseblocks_per_lun is > 0.
  275. *
  276. * Return: a positive number encoding the maximum number of eraseblocks on a
  277. * portion of memory, a negative error code otherwise.
  278. */
  279. int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len)
  280. {
  281. struct nand_device *nand = mtd_to_nanddev(mtd);
  282. struct nand_pos pos, end;
  283. unsigned int max_bb = 0;
  284. if (!nand->memorg.max_bad_eraseblocks_per_lun)
  285. return -ENOTSUPP;
  286. nanddev_offs_to_pos(nand, offs, &pos);
  287. nanddev_offs_to_pos(nand, offs + len, &end);
  288. for (nanddev_offs_to_pos(nand, offs, &pos);
  289. nanddev_pos_cmp(&pos, &end) < 0;
  290. nanddev_pos_next_lun(nand, &pos))
  291. max_bb += nand->memorg.max_bad_eraseblocks_per_lun;
  292. return max_bb;
  293. }
  294. EXPORT_SYMBOL_GPL(nanddev_mtd_max_bad_blocks);
  295. /**
  296. * nanddev_get_ecc_engine() - Find and get a suitable ECC engine
  297. * @nand: NAND device
  298. */
  299. static int nanddev_get_ecc_engine(struct nand_device *nand)
  300. {
  301. int engine_type;
  302. /* Read the user desires in terms of ECC engine/configuration */
  303. of_get_nand_ecc_user_config(nand);
  304. engine_type = nand->ecc.user_conf.engine_type;
  305. if (engine_type == NAND_ECC_ENGINE_TYPE_INVALID)
  306. engine_type = nand->ecc.defaults.engine_type;
  307. switch (engine_type) {
  308. case NAND_ECC_ENGINE_TYPE_NONE:
  309. return 0;
  310. case NAND_ECC_ENGINE_TYPE_SOFT:
  311. nand->ecc.engine = nand_ecc_get_sw_engine(nand);
  312. break;
  313. case NAND_ECC_ENGINE_TYPE_ON_DIE:
  314. nand->ecc.engine = nand_ecc_get_on_die_hw_engine(nand);
  315. break;
  316. case NAND_ECC_ENGINE_TYPE_ON_HOST:
  317. nand->ecc.engine = nand_ecc_get_on_host_hw_engine(nand);
  318. if (PTR_ERR(nand->ecc.engine) == -EPROBE_DEFER)
  319. return -EPROBE_DEFER;
  320. break;
  321. default:
  322. pr_err("Missing ECC engine type\n");
  323. }
  324. if (!nand->ecc.engine)
  325. return -EINVAL;
  326. return 0;
  327. }
  328. /**
  329. * nanddev_put_ecc_engine() - Dettach and put the in-use ECC engine
  330. * @nand: NAND device
  331. */
  332. static int nanddev_put_ecc_engine(struct nand_device *nand)
  333. {
  334. switch (nand->ecc.ctx.conf.engine_type) {
  335. case NAND_ECC_ENGINE_TYPE_ON_HOST:
  336. nand_ecc_put_on_host_hw_engine(nand);
  337. break;
  338. case NAND_ECC_ENGINE_TYPE_NONE:
  339. case NAND_ECC_ENGINE_TYPE_SOFT:
  340. case NAND_ECC_ENGINE_TYPE_ON_DIE:
  341. default:
  342. break;
  343. }
  344. return 0;
  345. }
  346. /**
  347. * nanddev_find_ecc_configuration() - Find a suitable ECC configuration
  348. * @nand: NAND device
  349. */
  350. static int nanddev_find_ecc_configuration(struct nand_device *nand)
  351. {
  352. int ret;
  353. if (!nand->ecc.engine)
  354. return -ENOTSUPP;
  355. ret = nand_ecc_init_ctx(nand);
  356. if (ret)
  357. return ret;
  358. if (!nand_ecc_is_strong_enough(nand))
  359. pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  360. nand->mtd.name);
  361. return 0;
  362. }
  363. /**
  364. * nanddev_ecc_engine_init() - Initialize an ECC engine for the chip
  365. * @nand: NAND device
  366. */
  367. int nanddev_ecc_engine_init(struct nand_device *nand)
  368. {
  369. int ret;
  370. /* Look for the ECC engine to use */
  371. ret = nanddev_get_ecc_engine(nand);
  372. if (ret) {
  373. if (ret != -EPROBE_DEFER)
  374. pr_err("No ECC engine found\n");
  375. return ret;
  376. }
  377. /* No ECC engine requested */
  378. if (!nand->ecc.engine)
  379. return 0;
  380. /* Configure the engine: balance user input and chip requirements */
  381. ret = nanddev_find_ecc_configuration(nand);
  382. if (ret) {
  383. pr_err("No suitable ECC configuration\n");
  384. nanddev_put_ecc_engine(nand);
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. EXPORT_SYMBOL_GPL(nanddev_ecc_engine_init);
  390. /**
  391. * nanddev_ecc_engine_cleanup() - Cleanup ECC engine initializations
  392. * @nand: NAND device
  393. */
  394. void nanddev_ecc_engine_cleanup(struct nand_device *nand)
  395. {
  396. if (nand->ecc.engine)
  397. nand_ecc_cleanup_ctx(nand);
  398. nanddev_put_ecc_engine(nand);
  399. }
  400. EXPORT_SYMBOL_GPL(nanddev_ecc_engine_cleanup);
  401. /**
  402. * nanddev_init() - Initialize a NAND device
  403. * @nand: NAND device
  404. * @ops: NAND device operations
  405. * @owner: NAND device owner
  406. *
  407. * Initializes a NAND device object. Consistency checks are done on @ops and
  408. * @nand->memorg. Also takes care of initializing the BBT.
  409. *
  410. * Return: 0 in case of success, a negative error code otherwise.
  411. */
  412. int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
  413. struct module *owner)
  414. {
  415. struct mtd_info *mtd = nanddev_to_mtd(nand);
  416. struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
  417. if (!nand || !ops)
  418. return -EINVAL;
  419. if (!ops->erase || !ops->markbad || !ops->isbad)
  420. return -EINVAL;
  421. if (!memorg->bits_per_cell || !memorg->pagesize ||
  422. !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
  423. !memorg->planes_per_lun || !memorg->luns_per_target ||
  424. !memorg->ntargets)
  425. return -EINVAL;
  426. nand->rowconv.eraseblock_addr_shift =
  427. fls(memorg->pages_per_eraseblock - 1);
  428. nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
  429. nand->rowconv.eraseblock_addr_shift;
  430. nand->ops = ops;
  431. mtd->type = memorg->bits_per_cell == 1 ?
  432. MTD_NANDFLASH : MTD_MLCNANDFLASH;
  433. mtd->flags = MTD_CAP_NANDFLASH;
  434. mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
  435. mtd->writesize = memorg->pagesize;
  436. mtd->writebufsize = memorg->pagesize;
  437. mtd->oobsize = memorg->oobsize;
  438. mtd->size = nanddev_size(nand);
  439. mtd->owner = owner;
  440. return nanddev_bbt_init(nand);
  441. }
  442. EXPORT_SYMBOL_GPL(nanddev_init);
  443. /**
  444. * nanddev_cleanup() - Release resources allocated in nanddev_init()
  445. * @nand: NAND device
  446. *
  447. * Basically undoes what has been done in nanddev_init().
  448. */
  449. void nanddev_cleanup(struct nand_device *nand)
  450. {
  451. if (nanddev_bbt_is_initialized(nand))
  452. nanddev_bbt_cleanup(nand);
  453. }
  454. EXPORT_SYMBOL_GPL(nanddev_cleanup);
  455. MODULE_DESCRIPTION("Generic NAND framework");
  456. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  457. MODULE_LICENSE("GPL v2");