1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017 Free Electrons
6 * Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Peter Pan <peterpandong@micron.com>
10 #define pr_fmt(fmt) "nand: " fmt
14 #include <linux/compat.h>
15 #include <linux/module.h>
17 #include <linux/mtd/nand.h>
20 * nanddev_isbad() - Check if a block is bad
22 * @pos: position pointing to the block we want to check
24 * Return: true if the block is bad, false otherwise.
26 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
28 if (nanddev_bbt_is_initialized(nand)) {
32 entry = nanddev_bbt_pos_to_entry(nand, pos);
33 status = nanddev_bbt_get_block_status(nand, entry);
34 /* Lazy block status retrieval */
35 if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
36 if (nand->ops->isbad(nand, pos))
37 status = NAND_BBT_BLOCK_FACTORY_BAD;
39 status = NAND_BBT_BLOCK_GOOD;
41 nanddev_bbt_set_block_status(nand, entry, status);
44 if (status == NAND_BBT_BLOCK_WORN ||
45 status == NAND_BBT_BLOCK_FACTORY_BAD)
51 return nand->ops->isbad(nand, pos);
53 EXPORT_SYMBOL_GPL(nanddev_isbad);
56 * nanddev_markbad() - Mark a block as bad
58 * @pos: position of the block to mark bad
60 * Mark a block bad. This function is updating the BBT if available and
61 * calls the low-level markbad hook (nand->ops->markbad()).
63 * Return: 0 in case of success, a negative error code otherwise.
65 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
67 struct mtd_info *mtd = nanddev_to_mtd(nand);
71 if (nanddev_isbad(nand, pos))
74 ret = nand->ops->markbad(nand, pos);
76 pr_warn("failed to write BBM to block @%llx (err = %d)\n",
77 nanddev_pos_to_offs(nand, pos), ret);
79 if (!nanddev_bbt_is_initialized(nand))
82 entry = nanddev_bbt_pos_to_entry(nand, pos);
83 ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
87 ret = nanddev_bbt_update(nand);
91 mtd->ecc_stats.badblocks++;
95 EXPORT_SYMBOL_GPL(nanddev_markbad);
98 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
100 * @pos: NAND position to test
102 * Checks whether the eraseblock pointed by @pos is reserved or not.
104 * Return: true if the eraseblock is reserved, false otherwise.
106 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
111 if (!nanddev_bbt_is_initialized(nand))
114 /* Return info from the table */
115 entry = nanddev_bbt_pos_to_entry(nand, pos);
116 status = nanddev_bbt_get_block_status(nand, entry);
117 return status == NAND_BBT_BLOCK_RESERVED;
119 EXPORT_SYMBOL_GPL(nanddev_isreserved);
122 * nanddev_erase() - Erase a NAND portion
124 * @pos: position of the block to erase
126 * Erases the block if it's not bad.
128 * Return: 0 in case of success, a negative error code otherwise.
130 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
132 if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
133 pr_warn("attempt to erase a bad/reserved block @%llx\n",
134 nanddev_pos_to_offs(nand, pos));
138 return nand->ops->erase(nand, pos);
140 EXPORT_SYMBOL_GPL(nanddev_erase);
143 * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
145 * @einfo: erase request
147 * This is a simple mtd->_erase() implementation iterating over all blocks
148 * concerned by @einfo and calling nand->ops->erase() on each of them.
150 * Note that mtd->_erase should not be directly assigned to this helper,
151 * because there's no locking here. NAND specialized layers should instead
152 * implement there own wrapper around nanddev_mtd_erase() taking the
153 * appropriate lock before calling nanddev_mtd_erase().
155 * Return: 0 in case of success, a negative error code otherwise.
157 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
159 struct nand_device *nand = mtd_to_nanddev(mtd);
160 struct nand_pos pos, last;
163 nanddev_offs_to_pos(nand, einfo->addr, &pos);
164 nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
165 while (nanddev_pos_cmp(&pos, &last) <= 0) {
166 ret = nanddev_erase(nand, &pos);
168 einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
173 nanddev_pos_next_eraseblock(nand, &pos);
178 EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
181 * nanddev_init() - Initialize a NAND device
183 * @ops: NAND device operations
184 * @owner: NAND device owner
186 * Initializes a NAND device object. Consistency checks are done on @ops and
187 * @nand->memorg. Also takes care of initializing the BBT.
189 * Return: 0 in case of success, a negative error code otherwise.
191 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
192 struct module *owner)
194 struct mtd_info *mtd = nanddev_to_mtd(nand);
195 struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
200 if (!ops->erase || !ops->markbad || !ops->isbad)
203 if (!memorg->bits_per_cell || !memorg->pagesize ||
204 !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
205 !memorg->planes_per_lun || !memorg->luns_per_target ||
209 nand->rowconv.eraseblock_addr_shift =
210 fls(memorg->pages_per_eraseblock - 1);
211 nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
212 nand->rowconv.eraseblock_addr_shift;
216 mtd->type = memorg->bits_per_cell == 1 ?
217 MTD_NANDFLASH : MTD_MLCNANDFLASH;
218 mtd->flags = MTD_CAP_NANDFLASH;
219 mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
220 mtd->writesize = memorg->pagesize;
221 mtd->writebufsize = memorg->pagesize;
222 mtd->oobsize = memorg->oobsize;
223 mtd->size = nanddev_size(nand);
226 return nanddev_bbt_init(nand);
228 EXPORT_SYMBOL_GPL(nanddev_init);
231 * nanddev_cleanup() - Release resources allocated in nanddev_init()
234 * Basically undoes what has been done in nanddev_init().
236 void nanddev_cleanup(struct nand_device *nand)
238 if (nanddev_bbt_is_initialized(nand))
239 nanddev_bbt_cleanup(nand);
241 EXPORT_SYMBOL_GPL(nanddev_cleanup);
243 MODULE_DESCRIPTION("Generic NAND framework");
244 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
245 MODULE_LICENSE("GPL v2");