core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. return ret;
  143. }
  144. nanddev_pos_next_eraseblock(nand, &pos);
  145. }
  146. return 0;
  147. }
  148. EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
  149. /**
  150. * nanddev_init() - Initialize a NAND device
  151. * @nand: NAND device
  152. * @ops: NAND device operations
  153. * @owner: NAND device owner
  154. *
  155. * Initializes a NAND device object. Consistency checks are done on @ops and
  156. * @nand->memorg. Also takes care of initializing the BBT.
  157. *
  158. * Return: 0 in case of success, a negative error code otherwise.
  159. */
  160. int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
  161. struct module *owner)
  162. {
  163. struct mtd_info *mtd = nanddev_to_mtd(nand);
  164. struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
  165. if (!nand || !ops)
  166. return -EINVAL;
  167. if (!ops->erase || !ops->markbad || !ops->isbad)
  168. return -EINVAL;
  169. if (!memorg->bits_per_cell || !memorg->pagesize ||
  170. !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
  171. !memorg->planes_per_lun || !memorg->luns_per_target ||
  172. !memorg->ntargets)
  173. return -EINVAL;
  174. nand->rowconv.eraseblock_addr_shift =
  175. fls(memorg->pages_per_eraseblock - 1);
  176. nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
  177. nand->rowconv.eraseblock_addr_shift;
  178. nand->ops = ops;
  179. mtd->type = memorg->bits_per_cell == 1 ?
  180. MTD_NANDFLASH : MTD_MLCNANDFLASH;
  181. mtd->flags = MTD_CAP_NANDFLASH;
  182. mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
  183. mtd->writesize = memorg->pagesize;
  184. mtd->writebufsize = memorg->pagesize;
  185. mtd->oobsize = memorg->oobsize;
  186. mtd->size = nanddev_size(nand);
  187. mtd->owner = owner;
  188. return nanddev_bbt_init(nand);
  189. }
  190. EXPORT_SYMBOL_GPL(nanddev_init);
  191. /**
  192. * nanddev_cleanup() - Release resources allocated in nanddev_init()
  193. * @nand: NAND device
  194. *
  195. * Basically undoes what has been done in nanddev_init().
  196. */
  197. void nanddev_cleanup(struct nand_device *nand)
  198. {
  199. if (nanddev_bbt_is_initialized(nand))
  200. nanddev_bbt_cleanup(nand);
  201. }
  202. EXPORT_SYMBOL_GPL(nanddev_cleanup);
  203. MODULE_DESCRIPTION("Generic NAND framework");
  204. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  205. MODULE_LICENSE("GPL v2");