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
12 #include <linux/module.h>
13 #include <linux/mtd/nand.h>
16 * nanddev_isbad() - Check if a block is bad
18 * @pos: position pointing to the block we want to check
20 * Return: true if the block is bad, false otherwise.
22 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
24 if (WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning))
27 if (nanddev_bbt_is_initialized(nand)) {
31 entry = nanddev_bbt_pos_to_entry(nand, pos);
32 status = nanddev_bbt_get_block_status(nand, entry);
33 /* Lazy block status retrieval */
34 if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
35 if (nand->ops->isbad(nand, pos))
36 status = NAND_BBT_BLOCK_FACTORY_BAD;
38 status = NAND_BBT_BLOCK_GOOD;
40 nanddev_bbt_set_block_status(nand, entry, status);
43 if (status == NAND_BBT_BLOCK_WORN ||
44 status == NAND_BBT_BLOCK_FACTORY_BAD)
50 return nand->ops->isbad(nand, pos);
52 EXPORT_SYMBOL_GPL(nanddev_isbad);
55 * nanddev_markbad() - Mark a block as bad
57 * @pos: position of the block to mark bad
59 * Mark a block bad. This function is updating the BBT if available and
60 * calls the low-level markbad hook (nand->ops->markbad()).
62 * Return: 0 in case of success, a negative error code otherwise.
64 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
66 struct mtd_info *mtd = nanddev_to_mtd(nand);
70 if (nanddev_isbad(nand, pos))
73 ret = nand->ops->markbad(nand, pos);
75 pr_warn("failed to write BBM to block @%llx (err = %d)\n",
76 nanddev_pos_to_offs(nand, pos), ret);
78 if (!nanddev_bbt_is_initialized(nand))
81 entry = nanddev_bbt_pos_to_entry(nand, pos);
82 ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
86 ret = nanddev_bbt_update(nand);
90 mtd->ecc_stats.badblocks++;
94 EXPORT_SYMBOL_GPL(nanddev_markbad);
97 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
99 * @pos: NAND position to test
101 * Checks whether the eraseblock pointed by @pos is reserved or not.
103 * Return: true if the eraseblock is reserved, false otherwise.
105 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
110 if (!nanddev_bbt_is_initialized(nand))
113 /* Return info from the table */
114 entry = nanddev_bbt_pos_to_entry(nand, pos);
115 status = nanddev_bbt_get_block_status(nand, entry);
116 return status == NAND_BBT_BLOCK_RESERVED;
118 EXPORT_SYMBOL_GPL(nanddev_isreserved);
121 * nanddev_erase() - Erase a NAND portion
123 * @pos: position of the block to erase
125 * Erases the block if it's not bad.
127 * Return: 0 in case of success, a negative error code otherwise.
129 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
131 if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
132 pr_warn("attempt to erase a bad/reserved block @%llx\n",
133 nanddev_pos_to_offs(nand, pos));
137 return nand->ops->erase(nand, pos);
139 EXPORT_SYMBOL_GPL(nanddev_erase);
142 * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
144 * @einfo: erase request
146 * This is a simple mtd->_erase() implementation iterating over all blocks
147 * concerned by @einfo and calling nand->ops->erase() on each of them.
149 * Note that mtd->_erase should not be directly assigned to this helper,
150 * because there's no locking here. NAND specialized layers should instead
151 * implement there own wrapper around nanddev_mtd_erase() taking the
152 * appropriate lock before calling nanddev_mtd_erase().
154 * Return: 0 in case of success, a negative error code otherwise.
156 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
158 struct nand_device *nand = mtd_to_nanddev(mtd);
159 struct nand_pos pos, last;
162 nanddev_offs_to_pos(nand, einfo->addr, &pos);
163 nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
164 while (nanddev_pos_cmp(&pos, &last) <= 0) {
165 ret = nanddev_erase(nand, &pos);
167 einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
172 nanddev_pos_next_eraseblock(nand, &pos);
177 EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
180 * nanddev_mtd_max_bad_blocks() - Get the maximum number of bad eraseblock on
181 * a specific region of the NAND device
183 * @offs: offset of the NAND region
184 * @len: length of the NAND region
186 * Default implementation for mtd->_max_bad_blocks(). Only works if
187 * nand->memorg.max_bad_eraseblocks_per_lun is > 0.
189 * Return: a positive number encoding the maximum number of eraseblocks on a
190 * portion of memory, a negative error code otherwise.
192 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len)
194 struct nand_device *nand = mtd_to_nanddev(mtd);
195 struct nand_pos pos, end;
196 unsigned int max_bb = 0;
198 if (!nand->memorg.max_bad_eraseblocks_per_lun)
201 nanddev_offs_to_pos(nand, offs, &pos);
202 nanddev_offs_to_pos(nand, offs + len, &end);
204 for (nanddev_offs_to_pos(nand, offs, &pos);
205 nanddev_pos_cmp(&pos, &end) < 0;
206 nanddev_pos_next_lun(nand, &pos))
207 max_bb += nand->memorg.max_bad_eraseblocks_per_lun;
211 EXPORT_SYMBOL_GPL(nanddev_mtd_max_bad_blocks);
214 * nanddev_get_ecc_engine() - Find and get a suitable ECC engine
217 static int nanddev_get_ecc_engine(struct nand_device *nand)
221 /* Read the user desires in terms of ECC engine/configuration */
222 of_get_nand_ecc_user_config(nand);
224 engine_type = nand->ecc.user_conf.engine_type;
225 if (engine_type == NAND_ECC_ENGINE_TYPE_INVALID)
226 engine_type = nand->ecc.defaults.engine_type;
228 switch (engine_type) {
229 case NAND_ECC_ENGINE_TYPE_NONE:
231 case NAND_ECC_ENGINE_TYPE_SOFT:
232 nand->ecc.engine = nand_ecc_get_sw_engine(nand);
234 case NAND_ECC_ENGINE_TYPE_ON_DIE:
235 nand->ecc.engine = nand_ecc_get_on_die_hw_engine(nand);
237 case NAND_ECC_ENGINE_TYPE_ON_HOST:
238 pr_err("On-host hardware ECC engines not supported yet\n");
241 pr_err("Missing ECC engine type\n");
244 if (!nand->ecc.engine)
251 * nanddev_put_ecc_engine() - Dettach and put the in-use ECC engine
254 static int nanddev_put_ecc_engine(struct nand_device *nand)
256 switch (nand->ecc.ctx.conf.engine_type) {
257 case NAND_ECC_ENGINE_TYPE_ON_HOST:
258 pr_err("On-host hardware ECC engines not supported yet\n");
260 case NAND_ECC_ENGINE_TYPE_NONE:
261 case NAND_ECC_ENGINE_TYPE_SOFT:
262 case NAND_ECC_ENGINE_TYPE_ON_DIE:
271 * nanddev_find_ecc_configuration() - Find a suitable ECC configuration
274 static int nanddev_find_ecc_configuration(struct nand_device *nand)
278 if (!nand->ecc.engine)
281 ret = nand_ecc_init_ctx(nand);
285 if (!nand_ecc_is_strong_enough(nand))
286 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
293 * nanddev_ecc_engine_init() - Initialize an ECC engine for the chip
296 int nanddev_ecc_engine_init(struct nand_device *nand)
300 /* Look for the ECC engine to use */
301 ret = nanddev_get_ecc_engine(nand);
303 pr_err("No ECC engine found\n");
307 /* No ECC engine requested */
308 if (!nand->ecc.engine)
311 /* Configure the engine: balance user input and chip requirements */
312 ret = nanddev_find_ecc_configuration(nand);
314 pr_err("No suitable ECC configuration\n");
315 nanddev_put_ecc_engine(nand);
322 EXPORT_SYMBOL_GPL(nanddev_ecc_engine_init);
325 * nanddev_ecc_engine_cleanup() - Cleanup ECC engine initializations
328 void nanddev_ecc_engine_cleanup(struct nand_device *nand)
330 if (nand->ecc.engine)
331 nand_ecc_cleanup_ctx(nand);
333 nanddev_put_ecc_engine(nand);
335 EXPORT_SYMBOL_GPL(nanddev_ecc_engine_cleanup);
338 * nanddev_init() - Initialize a NAND device
340 * @ops: NAND device operations
341 * @owner: NAND device owner
343 * Initializes a NAND device object. Consistency checks are done on @ops and
344 * @nand->memorg. Also takes care of initializing the BBT.
346 * Return: 0 in case of success, a negative error code otherwise.
348 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
349 struct module *owner)
351 struct mtd_info *mtd = nanddev_to_mtd(nand);
352 struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
357 if (!ops->erase || !ops->markbad || !ops->isbad)
360 if (!memorg->bits_per_cell || !memorg->pagesize ||
361 !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
362 !memorg->planes_per_lun || !memorg->luns_per_target ||
366 nand->rowconv.eraseblock_addr_shift =
367 fls(memorg->pages_per_eraseblock - 1);
368 nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
369 nand->rowconv.eraseblock_addr_shift;
373 mtd->type = memorg->bits_per_cell == 1 ?
374 MTD_NANDFLASH : MTD_MLCNANDFLASH;
375 mtd->flags = MTD_CAP_NANDFLASH;
376 mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
377 mtd->writesize = memorg->pagesize;
378 mtd->writebufsize = memorg->pagesize;
379 mtd->oobsize = memorg->oobsize;
380 mtd->size = nanddev_size(nand);
383 return nanddev_bbt_init(nand);
385 EXPORT_SYMBOL_GPL(nanddev_init);
388 * nanddev_cleanup() - Release resources allocated in nanddev_init()
391 * Basically undoes what has been done in nanddev_init().
393 void nanddev_cleanup(struct nand_device *nand)
395 if (nanddev_bbt_is_initialized(nand))
396 nanddev_bbt_cleanup(nand);
398 EXPORT_SYMBOL_GPL(nanddev_cleanup);
400 MODULE_DESCRIPTION("Generic NAND framework");
401 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
402 MODULE_LICENSE("GPL v2");