core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #ifndef __UBOOT__
  11. #include <linux/module.h>
  12. #endif
  13. #include <linux/mtd/nand.h>
  14. /**
  15. * nanddev_isbad() - Check if a block is bad
  16. * @nand: NAND device
  17. * @pos: position pointing to the block we want to check
  18. *
  19. * Return: true if the block is bad, false otherwise.
  20. */
  21. bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
  22. {
  23. if (nanddev_bbt_is_initialized(nand)) {
  24. unsigned int entry;
  25. int status;
  26. entry = nanddev_bbt_pos_to_entry(nand, pos);
  27. status = nanddev_bbt_get_block_status(nand, entry);
  28. /* Lazy block status retrieval */
  29. if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
  30. if (nand->ops->isbad(nand, pos))
  31. status = NAND_BBT_BLOCK_FACTORY_BAD;
  32. else
  33. status = NAND_BBT_BLOCK_GOOD;
  34. nanddev_bbt_set_block_status(nand, entry, status);
  35. }
  36. if (status == NAND_BBT_BLOCK_WORN ||
  37. status == NAND_BBT_BLOCK_FACTORY_BAD)
  38. return true;
  39. return false;
  40. }
  41. return nand->ops->isbad(nand, pos);
  42. }
  43. EXPORT_SYMBOL_GPL(nanddev_isbad);
  44. /**
  45. * nanddev_markbad() - Mark a block as bad
  46. * @nand: NAND device
  47. * @pos: position of the block to mark bad
  48. *
  49. * Mark a block bad. This function is updating the BBT if available and
  50. * calls the low-level markbad hook (nand->ops->markbad()).
  51. *
  52. * Return: 0 in case of success, a negative error code otherwise.
  53. */
  54. int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
  55. {
  56. struct mtd_info *mtd = nanddev_to_mtd(nand);
  57. unsigned int entry;
  58. int ret = 0;
  59. if (nanddev_isbad(nand, pos))
  60. return 0;
  61. ret = nand->ops->markbad(nand, pos);
  62. if (ret)
  63. pr_warn("failed to write BBM to block @%llx (err = %d)\n",
  64. nanddev_pos_to_offs(nand, pos), ret);
  65. if (!nanddev_bbt_is_initialized(nand))
  66. goto out;
  67. entry = nanddev_bbt_pos_to_entry(nand, pos);
  68. ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
  69. if (ret)
  70. goto out;
  71. ret = nanddev_bbt_update(nand);
  72. out:
  73. if (!ret)
  74. mtd->ecc_stats.badblocks++;
  75. return ret;
  76. }
  77. EXPORT_SYMBOL_GPL(nanddev_markbad);
  78. /**
  79. * nanddev_isreserved() - Check whether an eraseblock is reserved or not
  80. * @nand: NAND device
  81. * @pos: NAND position to test
  82. *
  83. * Checks whether the eraseblock pointed by @pos is reserved or not.
  84. *
  85. * Return: true if the eraseblock is reserved, false otherwise.
  86. */
  87. bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
  88. {
  89. unsigned int entry;
  90. int status;
  91. if (!nanddev_bbt_is_initialized(nand))
  92. return false;
  93. /* Return info from the table */
  94. entry = nanddev_bbt_pos_to_entry(nand, pos);
  95. status = nanddev_bbt_get_block_status(nand, entry);
  96. return status == NAND_BBT_BLOCK_RESERVED;
  97. }
  98. EXPORT_SYMBOL_GPL(nanddev_isreserved);
  99. /**
  100. * nanddev_erase() - Erase a NAND portion
  101. * @nand: NAND device
  102. * @pos: position of the block to erase
  103. *
  104. * Erases the block if it's not bad.
  105. *
  106. * Return: 0 in case of success, a negative error code otherwise.
  107. */
  108. int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
  109. {
  110. if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
  111. pr_warn("attempt to erase a bad/reserved block @%llx\n",
  112. nanddev_pos_to_offs(nand, pos));
  113. return -EIO;
  114. }
  115. return nand->ops->erase(nand, pos);
  116. }
  117. EXPORT_SYMBOL_GPL(nanddev_erase);
  118. /**
  119. * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
  120. * @mtd: MTD device
  121. * @einfo: erase request
  122. *
  123. * This is a simple mtd->_erase() implementation iterating over all blocks
  124. * concerned by @einfo and calling nand->ops->erase() on each of them.
  125. *
  126. * Note that mtd->_erase should not be directly assigned to this helper,
  127. * because there's no locking here. NAND specialized layers should instead
  128. * implement there own wrapper around nanddev_mtd_erase() taking the
  129. * appropriate lock before calling nanddev_mtd_erase().
  130. *
  131. * Return: 0 in case of success, a negative error code otherwise.
  132. */
  133. int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
  134. {
  135. struct nand_device *nand = mtd_to_nanddev(mtd);
  136. struct nand_pos pos, last;
  137. int ret;
  138. nanddev_offs_to_pos(nand, einfo->addr, &pos);
  139. nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
  140. while (nanddev_pos_cmp(&pos, &last) <= 0) {
  141. ret = nanddev_erase(nand, &pos);
  142. if (ret) {
  143. einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
  144. return ret;
  145. }
  146. nanddev_pos_next_eraseblock(nand, &pos);
  147. }
  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");