core.c 6.5 KB

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