core.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * nanddev_isbad() - Check if a block is bad
  14. * @nand: NAND device
  15. * @pos: position pointing to the block we want to check
  16. *
  17. * Return: true if the block is bad, false otherwise.
  18. */
  19. bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
  20. {
  21. if (nanddev_bbt_is_initialized(nand)) {
  22. unsigned int entry;
  23. int status;
  24. entry = nanddev_bbt_pos_to_entry(nand, pos);
  25. status = nanddev_bbt_get_block_status(nand, entry);
  26. /* Lazy block status retrieval */
  27. if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
  28. if (nand->ops->isbad(nand, pos))
  29. status = NAND_BBT_BLOCK_FACTORY_BAD;
  30. else
  31. status = NAND_BBT_BLOCK_GOOD;
  32. nanddev_bbt_set_block_status(nand, entry, status);
  33. }
  34. if (status == NAND_BBT_BLOCK_WORN ||
  35. status == NAND_BBT_BLOCK_FACTORY_BAD)
  36. return true;
  37. return false;
  38. }
  39. return nand->ops->isbad(nand, pos);
  40. }
  41. EXPORT_SYMBOL_GPL(nanddev_isbad);
  42. /**
  43. * nanddev_markbad() - Mark a block as bad
  44. * @nand: NAND device
  45. * @pos: position of the block to mark bad
  46. *
  47. * Mark a block bad. This function is updating the BBT if available and
  48. * calls the low-level markbad hook (nand->ops->markbad()).
  49. *
  50. * Return: 0 in case of success, a negative error code otherwise.
  51. */
  52. int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
  53. {
  54. struct mtd_info *mtd = nanddev_to_mtd(nand);
  55. unsigned int entry;
  56. int ret = 0;
  57. if (nanddev_isbad(nand, pos))
  58. return 0;
  59. ret = nand->ops->markbad(nand, pos);
  60. if (ret)
  61. pr_warn("failed to write BBM to block @%llx (err = %d)\n",
  62. nanddev_pos_to_offs(nand, pos), ret);
  63. if (!nanddev_bbt_is_initialized(nand))
  64. goto out;
  65. entry = nanddev_bbt_pos_to_entry(nand, pos);
  66. ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
  67. if (ret)
  68. goto out;
  69. ret = nanddev_bbt_update(nand);
  70. out:
  71. if (!ret)
  72. mtd->ecc_stats.badblocks++;
  73. return ret;
  74. }
  75. EXPORT_SYMBOL_GPL(nanddev_markbad);
  76. /**
  77. * nanddev_isreserved() - Check whether an eraseblock is reserved or not
  78. * @nand: NAND device
  79. * @pos: NAND position to test
  80. *
  81. * Checks whether the eraseblock pointed by @pos is reserved or not.
  82. *
  83. * Return: true if the eraseblock is reserved, false otherwise.
  84. */
  85. bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
  86. {
  87. unsigned int entry;
  88. int status;
  89. if (!nanddev_bbt_is_initialized(nand))
  90. return false;
  91. /* Return info from the table */
  92. entry = nanddev_bbt_pos_to_entry(nand, pos);
  93. status = nanddev_bbt_get_block_status(nand, entry);
  94. return status == NAND_BBT_BLOCK_RESERVED;
  95. }
  96. EXPORT_SYMBOL_GPL(nanddev_isreserved);
  97. /**
  98. * nanddev_erase() - Erase a NAND portion
  99. * @nand: NAND device
  100. * @pos: position of the block to erase
  101. *
  102. * Erases the block if it's not bad.
  103. *
  104. * Return: 0 in case of success, a negative error code otherwise.
  105. */
  106. int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
  107. {
  108. if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
  109. pr_warn("attempt to erase a bad/reserved block @%llx\n",
  110. nanddev_pos_to_offs(nand, pos));
  111. return -EIO;
  112. }
  113. return nand->ops->erase(nand, pos);
  114. }
  115. EXPORT_SYMBOL_GPL(nanddev_erase);
  116. /**
  117. * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
  118. * @mtd: MTD device
  119. * @einfo: erase request
  120. *
  121. * This is a simple mtd->_erase() implementation iterating over all blocks
  122. * concerned by @einfo and calling nand->ops->erase() on each of them.
  123. *
  124. * Note that mtd->_erase should not be directly assigned to this helper,
  125. * because there's no locking here. NAND specialized layers should instead
  126. * implement there own wrapper around nanddev_mtd_erase() taking the
  127. * appropriate lock before calling nanddev_mtd_erase().
  128. *
  129. * Return: 0 in case of success, a negative error code otherwise.
  130. */
  131. int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
  132. {
  133. struct nand_device *nand = mtd_to_nanddev(mtd);
  134. struct nand_pos pos, last;
  135. int ret;
  136. nanddev_offs_to_pos(nand, einfo->addr, &pos);
  137. nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
  138. while (nanddev_pos_cmp(&pos, &last) <= 0) {
  139. ret = nanddev_erase(nand, &pos);
  140. if (ret) {
  141. einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
  142. einfo->state = MTD_ERASE_FAILED;
  143. return ret;
  144. }
  145. nanddev_pos_next_eraseblock(nand, &pos);
  146. }
  147. einfo->state = MTD_ERASE_DONE;
  148. return 0;
  149. }
  150. EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
  151. /**
  152. * nanddev_init() - Initialize a NAND device
  153. * @nand: NAND device
  154. * @ops: NAND device operations
  155. * @owner: NAND device owner
  156. *
  157. * Initializes a NAND device object. Consistency checks are done on @ops and
  158. * @nand->memorg. Also takes care of initializing the BBT.
  159. *
  160. * Return: 0 in case of success, a negative error code otherwise.
  161. */
  162. int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
  163. struct module *owner)
  164. {
  165. struct mtd_info *mtd = nanddev_to_mtd(nand);
  166. struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
  167. if (!nand || !ops)
  168. return -EINVAL;
  169. if (!ops->erase || !ops->markbad || !ops->isbad)
  170. return -EINVAL;
  171. if (!memorg->bits_per_cell || !memorg->pagesize ||
  172. !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
  173. !memorg->planes_per_lun || !memorg->luns_per_target ||
  174. !memorg->ntargets)
  175. return -EINVAL;
  176. nand->rowconv.eraseblock_addr_shift =
  177. fls(memorg->pages_per_eraseblock - 1);
  178. nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
  179. nand->rowconv.eraseblock_addr_shift;
  180. nand->ops = ops;
  181. mtd->type = memorg->bits_per_cell == 1 ?
  182. MTD_NANDFLASH : MTD_MLCNANDFLASH;
  183. mtd->flags = MTD_CAP_NANDFLASH;
  184. mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
  185. mtd->writesize = memorg->pagesize;
  186. mtd->writebufsize = memorg->pagesize;
  187. mtd->oobsize = memorg->oobsize;
  188. mtd->size = nanddev_size(nand);
  189. mtd->owner = owner;
  190. return nanddev_bbt_init(nand);
  191. }
  192. EXPORT_SYMBOL_GPL(nanddev_init);
  193. /**
  194. * nanddev_cleanup() - Release resources allocated in nanddev_init()
  195. * @nand: NAND device
  196. *
  197. * Basically undoes what has been done in nanddev_init().
  198. */
  199. void nanddev_cleanup(struct nand_device *nand)
  200. {
  201. if (nanddev_bbt_is_initialized(nand))
  202. nanddev_bbt_cleanup(nand);
  203. }
  204. EXPORT_SYMBOL_GPL(nanddev_cleanup);
  205. MODULE_DESCRIPTION("Generic NAND framework");
  206. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  207. MODULE_LICENSE("GPL v2");