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 (nanddev_bbt_is_initialized(nand
)) {
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
;
35 status
= NAND_BBT_BLOCK_GOOD
;
37 nanddev_bbt_set_block_status(nand
, entry
, status
);
40 if (status
== NAND_BBT_BLOCK_WORN
||
41 status
== NAND_BBT_BLOCK_FACTORY_BAD
)
47 return nand
->ops
->isbad(nand
, pos
);
49 EXPORT_SYMBOL_GPL(nanddev_isbad
);
52 * nanddev_markbad() - Mark a block as bad
54 * @pos: position of the block to mark bad
56 * Mark a block bad. This function is updating the BBT if available and
57 * calls the low-level markbad hook (nand->ops->markbad()).
59 * Return: 0 in case of success, a negative error code otherwise.
61 int nanddev_markbad(struct nand_device
*nand
, const struct nand_pos
*pos
)
63 struct mtd_info
*mtd
= nanddev_to_mtd(nand
);
67 if (nanddev_isbad(nand
, pos
))
70 ret
= nand
->ops
->markbad(nand
, pos
);
72 pr_warn("failed to write BBM to block @%llx (err = %d)\n",
73 nanddev_pos_to_offs(nand
, pos
), ret
);
75 if (!nanddev_bbt_is_initialized(nand
))
78 entry
= nanddev_bbt_pos_to_entry(nand
, pos
);
79 ret
= nanddev_bbt_set_block_status(nand
, entry
, NAND_BBT_BLOCK_WORN
);
83 ret
= nanddev_bbt_update(nand
);
87 mtd
->ecc_stats
.badblocks
++;
91 EXPORT_SYMBOL_GPL(nanddev_markbad
);
94 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
96 * @pos: NAND position to test
98 * Checks whether the eraseblock pointed by @pos is reserved or not.
100 * Return: true if the eraseblock is reserved, false otherwise.
102 bool nanddev_isreserved(struct nand_device
*nand
, const struct nand_pos
*pos
)
107 if (!nanddev_bbt_is_initialized(nand
))
110 /* Return info from the table */
111 entry
= nanddev_bbt_pos_to_entry(nand
, pos
);
112 status
= nanddev_bbt_get_block_status(nand
, entry
);
113 return status
== NAND_BBT_BLOCK_RESERVED
;
115 EXPORT_SYMBOL_GPL(nanddev_isreserved
);
118 * nanddev_erase() - Erase a NAND portion
120 * @pos: position of the block to erase
122 * Erases the block if it's not bad.
124 * Return: 0 in case of success, a negative error code otherwise.
126 int nanddev_erase(struct nand_device
*nand
, const struct nand_pos
*pos
)
128 if (nanddev_isbad(nand
, pos
) || nanddev_isreserved(nand
, pos
)) {
129 pr_warn("attempt to erase a bad/reserved block @%llx\n",
130 nanddev_pos_to_offs(nand
, pos
));
134 return nand
->ops
->erase(nand
, pos
);
136 EXPORT_SYMBOL_GPL(nanddev_erase
);
139 * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
141 * @einfo: erase request
143 * This is a simple mtd->_erase() implementation iterating over all blocks
144 * concerned by @einfo and calling nand->ops->erase() on each of them.
146 * Note that mtd->_erase should not be directly assigned to this helper,
147 * because there's no locking here. NAND specialized layers should instead
148 * implement there own wrapper around nanddev_mtd_erase() taking the
149 * appropriate lock before calling nanddev_mtd_erase().
151 * Return: 0 in case of success, a negative error code otherwise.
153 int nanddev_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*einfo
)
155 struct nand_device
*nand
= mtd_to_nanddev(mtd
);
156 struct nand_pos pos
, last
;
159 nanddev_offs_to_pos(nand
, einfo
->addr
, &pos
);
160 nanddev_offs_to_pos(nand
, einfo
->addr
+ einfo
->len
- 1, &last
);
161 while (nanddev_pos_cmp(&pos
, &last
) <= 0) {
162 ret
= nanddev_erase(nand
, &pos
);
164 einfo
->fail_addr
= nanddev_pos_to_offs(nand
, &pos
);
169 nanddev_pos_next_eraseblock(nand
, &pos
);
174 EXPORT_SYMBOL_GPL(nanddev_mtd_erase
);
177 * nanddev_mtd_max_bad_blocks() - Get the maximum number of bad eraseblock on
178 * a specific region of the NAND device
180 * @offs: offset of the NAND region
181 * @len: length of the NAND region
183 * Default implementation for mtd->_max_bad_blocks(). Only works if
184 * nand->memorg.max_bad_eraseblocks_per_lun is > 0.
186 * Return: a positive number encoding the maximum number of eraseblocks on a
187 * portion of memory, a negative error code otherwise.
189 int nanddev_mtd_max_bad_blocks(struct mtd_info
*mtd
, loff_t offs
, size_t len
)
191 struct nand_device
*nand
= mtd_to_nanddev(mtd
);
192 struct nand_pos pos
, end
;
193 unsigned int max_bb
= 0;
195 if (!nand
->memorg
.max_bad_eraseblocks_per_lun
)
198 nanddev_offs_to_pos(nand
, offs
, &pos
);
199 nanddev_offs_to_pos(nand
, offs
+ len
, &end
);
201 for (nanddev_offs_to_pos(nand
, offs
, &pos
);
202 nanddev_pos_cmp(&pos
, &end
) < 0;
203 nanddev_pos_next_lun(nand
, &pos
))
204 max_bb
+= nand
->memorg
.max_bad_eraseblocks_per_lun
;
208 EXPORT_SYMBOL_GPL(nanddev_mtd_max_bad_blocks
);
211 * nanddev_get_ecc_engine() - Find and get a suitable ECC engine
214 static int nanddev_get_ecc_engine(struct nand_device
*nand
)
218 /* Read the user desires in terms of ECC engine/configuration */
219 of_get_nand_ecc_user_config(nand
);
221 engine_type
= nand
->ecc
.user_conf
.engine_type
;
222 if (engine_type
== NAND_ECC_ENGINE_TYPE_INVALID
)
223 engine_type
= nand
->ecc
.defaults
.engine_type
;
225 switch (engine_type
) {
226 case NAND_ECC_ENGINE_TYPE_NONE
:
228 case NAND_ECC_ENGINE_TYPE_SOFT
:
229 nand
->ecc
.engine
= nand_ecc_get_sw_engine(nand
);
231 case NAND_ECC_ENGINE_TYPE_ON_DIE
:
232 nand
->ecc
.engine
= nand_ecc_get_on_die_hw_engine(nand
);
234 case NAND_ECC_ENGINE_TYPE_ON_HOST
:
235 pr_err("On-host hardware ECC engines not supported yet\n");
238 pr_err("Missing ECC engine type\n");
241 if (!nand
->ecc
.engine
)
248 * nanddev_put_ecc_engine() - Dettach and put the in-use ECC engine
251 static int nanddev_put_ecc_engine(struct nand_device
*nand
)
253 switch (nand
->ecc
.ctx
.conf
.engine_type
) {
254 case NAND_ECC_ENGINE_TYPE_ON_HOST
:
255 pr_err("On-host hardware ECC engines not supported yet\n");
257 case NAND_ECC_ENGINE_TYPE_NONE
:
258 case NAND_ECC_ENGINE_TYPE_SOFT
:
259 case NAND_ECC_ENGINE_TYPE_ON_DIE
:
268 * nanddev_find_ecc_configuration() - Find a suitable ECC configuration
271 static int nanddev_find_ecc_configuration(struct nand_device
*nand
)
275 if (!nand
->ecc
.engine
)
278 ret
= nand_ecc_init_ctx(nand
);
282 if (!nand_ecc_is_strong_enough(nand
))
283 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
290 * nanddev_ecc_engine_init() - Initialize an ECC engine for the chip
293 int nanddev_ecc_engine_init(struct nand_device
*nand
)
297 /* Look for the ECC engine to use */
298 ret
= nanddev_get_ecc_engine(nand
);
300 pr_err("No ECC engine found\n");
304 /* No ECC engine requested */
305 if (!nand
->ecc
.engine
)
308 /* Configure the engine: balance user input and chip requirements */
309 ret
= nanddev_find_ecc_configuration(nand
);
311 pr_err("No suitable ECC configuration\n");
312 nanddev_put_ecc_engine(nand
);
319 EXPORT_SYMBOL_GPL(nanddev_ecc_engine_init
);
322 * nanddev_ecc_engine_cleanup() - Cleanup ECC engine initializations
325 void nanddev_ecc_engine_cleanup(struct nand_device
*nand
)
327 if (nand
->ecc
.engine
)
328 nand_ecc_cleanup_ctx(nand
);
330 nanddev_put_ecc_engine(nand
);
332 EXPORT_SYMBOL_GPL(nanddev_ecc_engine_cleanup
);
335 * nanddev_init() - Initialize a NAND device
337 * @ops: NAND device operations
338 * @owner: NAND device owner
340 * Initializes a NAND device object. Consistency checks are done on @ops and
341 * @nand->memorg. Also takes care of initializing the BBT.
343 * Return: 0 in case of success, a negative error code otherwise.
345 int nanddev_init(struct nand_device
*nand
, const struct nand_ops
*ops
,
346 struct module
*owner
)
348 struct mtd_info
*mtd
= nanddev_to_mtd(nand
);
349 struct nand_memory_organization
*memorg
= nanddev_get_memorg(nand
);
354 if (!ops
->erase
|| !ops
->markbad
|| !ops
->isbad
)
357 if (!memorg
->bits_per_cell
|| !memorg
->pagesize
||
358 !memorg
->pages_per_eraseblock
|| !memorg
->eraseblocks_per_lun
||
359 !memorg
->planes_per_lun
|| !memorg
->luns_per_target
||
363 nand
->rowconv
.eraseblock_addr_shift
=
364 fls(memorg
->pages_per_eraseblock
- 1);
365 nand
->rowconv
.lun_addr_shift
= fls(memorg
->eraseblocks_per_lun
- 1) +
366 nand
->rowconv
.eraseblock_addr_shift
;
370 mtd
->type
= memorg
->bits_per_cell
== 1 ?
371 MTD_NANDFLASH
: MTD_MLCNANDFLASH
;
372 mtd
->flags
= MTD_CAP_NANDFLASH
;
373 mtd
->erasesize
= memorg
->pagesize
* memorg
->pages_per_eraseblock
;
374 mtd
->writesize
= memorg
->pagesize
;
375 mtd
->writebufsize
= memorg
->pagesize
;
376 mtd
->oobsize
= memorg
->oobsize
;
377 mtd
->size
= nanddev_size(nand
);
380 return nanddev_bbt_init(nand
);
382 EXPORT_SYMBOL_GPL(nanddev_init
);
385 * nanddev_cleanup() - Release resources allocated in nanddev_init()
388 * Basically undoes what has been done in nanddev_init().
390 void nanddev_cleanup(struct nand_device
*nand
)
392 if (nanddev_bbt_is_initialized(nand
))
393 nanddev_bbt_cleanup(nand
);
395 EXPORT_SYMBOL_GPL(nanddev_cleanup
);
397 MODULE_DESCRIPTION("Generic NAND framework");
398 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
399 MODULE_LICENSE("GPL v2");