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
15 #include <linux/compat.h>
16 #include <linux/module.h>
18 #include <linux/bitops.h>
19 #include <linux/mtd/nand.h>
22 * nanddev_isbad() - Check if a block is bad
24 * @pos: position pointing to the block we want to check
26 * Return: true if the block is bad, false otherwise.
28 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
30 if (nanddev_bbt_is_initialized(nand)) {
34 entry = nanddev_bbt_pos_to_entry(nand, pos);
35 status = nanddev_bbt_get_block_status(nand, entry);
36 /* Lazy block status retrieval */
37 if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
38 if (nand->ops->isbad(nand, pos))
39 status = NAND_BBT_BLOCK_FACTORY_BAD;
41 status = NAND_BBT_BLOCK_GOOD;
43 nanddev_bbt_set_block_status(nand, entry, status);
46 if (status == NAND_BBT_BLOCK_WORN ||
47 status == NAND_BBT_BLOCK_FACTORY_BAD)
53 return nand->ops->isbad(nand, pos);
55 EXPORT_SYMBOL_GPL(nanddev_isbad);
58 * nanddev_markbad() - Mark a block as bad
60 * @pos: position of the block to mark bad
62 * Mark a block bad. This function is updating the BBT if available and
63 * calls the low-level markbad hook (nand->ops->markbad()).
65 * Return: 0 in case of success, a negative error code otherwise.
67 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
69 struct mtd_info *mtd = nanddev_to_mtd(nand);
73 if (nanddev_isbad(nand, pos))
76 ret = nand->ops->markbad(nand, pos);
78 pr_warn("failed to write BBM to block @%llx (err = %d)\n",
79 nanddev_pos_to_offs(nand, pos), ret);
81 if (!nanddev_bbt_is_initialized(nand))
84 entry = nanddev_bbt_pos_to_entry(nand, pos);
85 ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
89 ret = nanddev_bbt_update(nand);
93 mtd->ecc_stats.badblocks++;
97 EXPORT_SYMBOL_GPL(nanddev_markbad);
100 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
102 * @pos: NAND position to test
104 * Checks whether the eraseblock pointed by @pos is reserved or not.
106 * Return: true if the eraseblock is reserved, false otherwise.
108 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
113 if (!nanddev_bbt_is_initialized(nand))
116 /* Return info from the table */
117 entry = nanddev_bbt_pos_to_entry(nand, pos);
118 status = nanddev_bbt_get_block_status(nand, entry);
119 return status == NAND_BBT_BLOCK_RESERVED;
121 EXPORT_SYMBOL_GPL(nanddev_isreserved);
124 * nanddev_erase() - Erase a NAND portion
126 * @pos: position of the block to erase
128 * Erases the block if it's not bad.
130 * Return: 0 in case of success, a negative error code otherwise.
132 static int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
136 if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
137 pr_warn("attempt to erase a bad/reserved block @%llx\n",
138 nanddev_pos_to_offs(nand, pos));
139 if (nanddev_isreserved(nand, pos))
142 /* remove bad block from BBT */
143 entry = nanddev_bbt_pos_to_entry(nand, pos);
144 nanddev_bbt_set_block_status(nand, entry,
145 NAND_BBT_BLOCK_STATUS_UNKNOWN);
148 return nand->ops->erase(nand, pos);
152 * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
154 * @einfo: erase request
156 * This is a simple mtd->_erase() implementation iterating over all blocks
157 * concerned by @einfo and calling nand->ops->erase() on each of them.
159 * Note that mtd->_erase should not be directly assigned to this helper,
160 * because there's no locking here. NAND specialized layers should instead
161 * implement there own wrapper around nanddev_mtd_erase() taking the
162 * appropriate lock before calling nanddev_mtd_erase().
164 * Return: 0 in case of success, a negative error code otherwise.
166 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
168 struct nand_device *nand = mtd_to_nanddev(mtd);
169 struct nand_pos pos, last;
172 nanddev_offs_to_pos(nand, einfo->addr, &pos);
173 nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
174 while (nanddev_pos_cmp(&pos, &last) <= 0) {
176 ret = nanddev_erase(nand, &pos);
178 einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
183 nanddev_pos_next_eraseblock(nand, &pos);
188 EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
191 * nanddev_init() - Initialize a NAND device
193 * @ops: NAND device operations
194 * @owner: NAND device owner
196 * Initializes a NAND device object. Consistency checks are done on @ops and
197 * @nand->memorg. Also takes care of initializing the BBT.
199 * Return: 0 in case of success, a negative error code otherwise.
201 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
202 struct module *owner)
204 struct mtd_info *mtd = nanddev_to_mtd(nand);
205 struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
210 if (!ops->erase || !ops->markbad || !ops->isbad)
213 if (!memorg->bits_per_cell || !memorg->pagesize ||
214 !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
215 !memorg->planes_per_lun || !memorg->luns_per_target ||
219 nand->rowconv.eraseblock_addr_shift =
220 fls(memorg->pages_per_eraseblock - 1);
221 nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
222 nand->rowconv.eraseblock_addr_shift;
226 mtd->type = memorg->bits_per_cell == 1 ?
227 MTD_NANDFLASH : MTD_MLCNANDFLASH;
228 mtd->flags = MTD_CAP_NANDFLASH;
229 mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
230 mtd->writesize = memorg->pagesize;
231 mtd->writebufsize = memorg->pagesize;
232 mtd->oobsize = memorg->oobsize;
233 mtd->size = nanddev_size(nand);
236 return nanddev_bbt_init(nand);
238 EXPORT_SYMBOL_GPL(nanddev_init);
241 * nanddev_cleanup() - Release resources allocated in nanddev_init()
244 * Basically undoes what has been done in nanddev_init().
246 void nanddev_cleanup(struct nand_device *nand)
248 if (nanddev_bbt_is_initialized(nand))
249 nanddev_bbt_cleanup(nand);
251 EXPORT_SYMBOL_GPL(nanddev_cleanup);
253 MODULE_DESCRIPTION("Generic NAND framework");
254 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
255 MODULE_LICENSE("GPL v2");