Lines Matching +full:nand +full:- +full:ecc +full:- +full:engine
1 // SPDX-License-Identifier: GPL-2.0
6 * Boris Brezillon <boris.brezillon@free-electrons.com>
10 #define pr_fmt(fmt) "nand: " fmt
13 #include <linux/mtd/nand.h>
16 * nanddev_isbad() - Check if a block is bad
17 * @nand: NAND device
22 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos) in nanddev_isbad() argument
27 if (nanddev_bbt_is_initialized(nand)) { in nanddev_isbad()
31 entry = nanddev_bbt_pos_to_entry(nand, pos); in nanddev_isbad()
32 status = nanddev_bbt_get_block_status(nand, entry); in nanddev_isbad()
35 if (nand->ops->isbad(nand, pos)) in nanddev_isbad()
40 nanddev_bbt_set_block_status(nand, entry, status); in nanddev_isbad()
50 return nand->ops->isbad(nand, pos); in nanddev_isbad()
55 * nanddev_markbad() - Mark a block as bad
56 * @nand: NAND device
60 * calls the low-level markbad hook (nand->ops->markbad()).
64 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos) in nanddev_markbad() argument
66 struct mtd_info *mtd = nanddev_to_mtd(nand); in nanddev_markbad()
70 if (nanddev_isbad(nand, pos)) in nanddev_markbad()
73 ret = nand->ops->markbad(nand, pos); in nanddev_markbad()
76 nanddev_pos_to_offs(nand, pos), ret); in nanddev_markbad()
78 if (!nanddev_bbt_is_initialized(nand)) in nanddev_markbad()
81 entry = nanddev_bbt_pos_to_entry(nand, pos); in nanddev_markbad()
82 ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN); in nanddev_markbad()
86 ret = nanddev_bbt_update(nand); in nanddev_markbad()
90 mtd->ecc_stats.badblocks++; in nanddev_markbad()
97 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
98 * @nand: NAND device
99 * @pos: NAND position to test
105 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos) in nanddev_isreserved() argument
110 if (!nanddev_bbt_is_initialized(nand)) in nanddev_isreserved()
114 entry = nanddev_bbt_pos_to_entry(nand, pos); in nanddev_isreserved()
115 status = nanddev_bbt_get_block_status(nand, entry); in nanddev_isreserved()
121 * nanddev_erase() - Erase a NAND portion
122 * @nand: NAND device
129 static int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos) in nanddev_erase() argument
131 if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) { in nanddev_erase()
133 nanddev_pos_to_offs(nand, pos)); in nanddev_erase()
134 return -EIO; in nanddev_erase()
137 return nand->ops->erase(nand, pos); in nanddev_erase()
141 * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
145 * This is a simple mtd->_erase() implementation iterating over all blocks
146 * concerned by @einfo and calling nand->ops->erase() on each of them.
148 * Note that mtd->_erase should not be directly assigned to this helper,
149 * because there's no locking here. NAND specialized layers should instead
157 struct nand_device *nand = mtd_to_nanddev(mtd); in nanddev_mtd_erase() local
161 nanddev_offs_to_pos(nand, einfo->addr, &pos); in nanddev_mtd_erase()
162 nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last); in nanddev_mtd_erase()
164 ret = nanddev_erase(nand, &pos); in nanddev_mtd_erase()
166 einfo->fail_addr = nanddev_pos_to_offs(nand, &pos); in nanddev_mtd_erase()
171 nanddev_pos_next_eraseblock(nand, &pos); in nanddev_mtd_erase()
179 * nanddev_mtd_max_bad_blocks() - Get the maximum number of bad eraseblock on
180 * a specific region of the NAND device
182 * @offs: offset of the NAND region
183 * @len: length of the NAND region
185 * Default implementation for mtd->_max_bad_blocks(). Only works if
186 * nand->memorg.max_bad_eraseblocks_per_lun is > 0.
193 struct nand_device *nand = mtd_to_nanddev(mtd); in nanddev_mtd_max_bad_blocks() local
197 if (!nand->memorg.max_bad_eraseblocks_per_lun) in nanddev_mtd_max_bad_blocks()
198 return -ENOTSUPP; in nanddev_mtd_max_bad_blocks()
200 nanddev_offs_to_pos(nand, offs, &pos); in nanddev_mtd_max_bad_blocks()
201 nanddev_offs_to_pos(nand, offs + len, &end); in nanddev_mtd_max_bad_blocks()
203 for (nanddev_offs_to_pos(nand, offs, &pos); in nanddev_mtd_max_bad_blocks()
205 nanddev_pos_next_lun(nand, &pos)) in nanddev_mtd_max_bad_blocks()
206 max_bb += nand->memorg.max_bad_eraseblocks_per_lun; in nanddev_mtd_max_bad_blocks()
213 * nanddev_get_ecc_engine() - Find and get a suitable ECC engine
214 * @nand: NAND device
216 static int nanddev_get_ecc_engine(struct nand_device *nand) in nanddev_get_ecc_engine() argument
220 /* Read the user desires in terms of ECC engine/configuration */ in nanddev_get_ecc_engine()
221 of_get_nand_ecc_user_config(nand); in nanddev_get_ecc_engine()
223 engine_type = nand->ecc.user_conf.engine_type; in nanddev_get_ecc_engine()
225 engine_type = nand->ecc.defaults.engine_type; in nanddev_get_ecc_engine()
231 nand->ecc.engine = nand_ecc_get_sw_engine(nand); in nanddev_get_ecc_engine()
234 nand->ecc.engine = nand_ecc_get_on_die_hw_engine(nand); in nanddev_get_ecc_engine()
237 nand->ecc.engine = nand_ecc_get_on_host_hw_engine(nand); in nanddev_get_ecc_engine()
238 if (PTR_ERR(nand->ecc.engine) == -EPROBE_DEFER) in nanddev_get_ecc_engine()
239 return -EPROBE_DEFER; in nanddev_get_ecc_engine()
242 pr_err("Missing ECC engine type\n"); in nanddev_get_ecc_engine()
245 if (!nand->ecc.engine) in nanddev_get_ecc_engine()
246 return -EINVAL; in nanddev_get_ecc_engine()
252 * nanddev_put_ecc_engine() - Dettach and put the in-use ECC engine
253 * @nand: NAND device
255 static int nanddev_put_ecc_engine(struct nand_device *nand) in nanddev_put_ecc_engine() argument
257 switch (nand->ecc.ctx.conf.engine_type) { in nanddev_put_ecc_engine()
259 nand_ecc_put_on_host_hw_engine(nand); in nanddev_put_ecc_engine()
272 * nanddev_find_ecc_configuration() - Find a suitable ECC configuration
273 * @nand: NAND device
275 static int nanddev_find_ecc_configuration(struct nand_device *nand) in nanddev_find_ecc_configuration() argument
279 if (!nand->ecc.engine) in nanddev_find_ecc_configuration()
280 return -ENOTSUPP; in nanddev_find_ecc_configuration()
282 ret = nand_ecc_init_ctx(nand); in nanddev_find_ecc_configuration()
286 if (!nand_ecc_is_strong_enough(nand)) in nanddev_find_ecc_configuration()
287 …pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the … in nanddev_find_ecc_configuration()
288 nand->mtd.name); in nanddev_find_ecc_configuration()
294 * nanddev_ecc_engine_init() - Initialize an ECC engine for the chip
295 * @nand: NAND device
297 int nanddev_ecc_engine_init(struct nand_device *nand) in nanddev_ecc_engine_init() argument
301 /* Look for the ECC engine to use */ in nanddev_ecc_engine_init()
302 ret = nanddev_get_ecc_engine(nand); in nanddev_ecc_engine_init()
304 if (ret != -EPROBE_DEFER) in nanddev_ecc_engine_init()
305 pr_err("No ECC engine found\n"); in nanddev_ecc_engine_init()
310 /* No ECC engine requested */ in nanddev_ecc_engine_init()
311 if (!nand->ecc.engine) in nanddev_ecc_engine_init()
314 /* Configure the engine: balance user input and chip requirements */ in nanddev_ecc_engine_init()
315 ret = nanddev_find_ecc_configuration(nand); in nanddev_ecc_engine_init()
317 pr_err("No suitable ECC configuration\n"); in nanddev_ecc_engine_init()
318 nanddev_put_ecc_engine(nand); in nanddev_ecc_engine_init()
328 * nanddev_ecc_engine_cleanup() - Cleanup ECC engine initializations
329 * @nand: NAND device
331 void nanddev_ecc_engine_cleanup(struct nand_device *nand) in nanddev_ecc_engine_cleanup() argument
333 if (nand->ecc.engine) in nanddev_ecc_engine_cleanup()
334 nand_ecc_cleanup_ctx(nand); in nanddev_ecc_engine_cleanup()
336 nanddev_put_ecc_engine(nand); in nanddev_ecc_engine_cleanup()
341 * nanddev_init() - Initialize a NAND device
342 * @nand: NAND device
343 * @ops: NAND device operations
344 * @owner: NAND device owner
346 * Initializes a NAND device object. Consistency checks are done on @ops and
347 * @nand->memorg. Also takes care of initializing the BBT.
351 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops, in nanddev_init() argument
354 struct mtd_info *mtd = nanddev_to_mtd(nand); in nanddev_init()
355 struct nand_memory_organization *memorg = nanddev_get_memorg(nand); in nanddev_init()
357 if (!nand || !ops) in nanddev_init()
358 return -EINVAL; in nanddev_init()
360 if (!ops->erase || !ops->markbad || !ops->isbad) in nanddev_init()
361 return -EINVAL; in nanddev_init()
363 if (!memorg->bits_per_cell || !memorg->pagesize || in nanddev_init()
364 !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun || in nanddev_init()
365 !memorg->planes_per_lun || !memorg->luns_per_target || in nanddev_init()
366 !memorg->ntargets) in nanddev_init()
367 return -EINVAL; in nanddev_init()
369 nand->rowconv.eraseblock_addr_shift = in nanddev_init()
370 fls(memorg->pages_per_eraseblock - 1); in nanddev_init()
371 nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) + in nanddev_init()
372 nand->rowconv.eraseblock_addr_shift; in nanddev_init()
374 nand->ops = ops; in nanddev_init()
376 mtd->type = memorg->bits_per_cell == 1 ? in nanddev_init()
378 mtd->flags = MTD_CAP_NANDFLASH; in nanddev_init()
379 mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock; in nanddev_init()
380 mtd->writesize = memorg->pagesize; in nanddev_init()
381 mtd->writebufsize = memorg->pagesize; in nanddev_init()
382 mtd->oobsize = memorg->oobsize; in nanddev_init()
383 mtd->size = nanddev_size(nand); in nanddev_init()
384 mtd->owner = owner; in nanddev_init()
386 return nanddev_bbt_init(nand); in nanddev_init()
391 * nanddev_cleanup() - Release resources allocated in nanddev_init()
392 * @nand: NAND device
396 void nanddev_cleanup(struct nand_device *nand) in nanddev_cleanup() argument
398 if (nanddev_bbt_is_initialized(nand)) in nanddev_cleanup()
399 nanddev_bbt_cleanup(nand); in nanddev_cleanup()
403 MODULE_DESCRIPTION("Generic NAND framework");
404 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");