xref: /openbmc/linux/drivers/mtd/nand/core.c (revision 8e74a48d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017 Free Electrons
4  *
5  * Authors:
6  *	Boris Brezillon <boris.brezillon@free-electrons.com>
7  *	Peter Pan <peterpandong@micron.com>
8  */
9 
10 #define pr_fmt(fmt)	"nand: " fmt
11 
12 #include <linux/module.h>
13 #include <linux/mtd/nand.h>
14 
15 /**
16  * nanddev_isbad() - Check if a block is bad
17  * @nand: NAND device
18  * @pos: position pointing to the block we want to check
19  *
20  * Return: true if the block is bad, false otherwise.
21  */
22 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
23 {
24 	if (WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning))
25 		return false;
26 
27 	if (nanddev_bbt_is_initialized(nand)) {
28 		unsigned int entry;
29 		int status;
30 
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;
37 			else
38 				status = NAND_BBT_BLOCK_GOOD;
39 
40 			nanddev_bbt_set_block_status(nand, entry, status);
41 		}
42 
43 		if (status == NAND_BBT_BLOCK_WORN ||
44 		    status == NAND_BBT_BLOCK_FACTORY_BAD)
45 			return true;
46 
47 		return false;
48 	}
49 
50 	return nand->ops->isbad(nand, pos);
51 }
52 EXPORT_SYMBOL_GPL(nanddev_isbad);
53 
54 /**
55  * nanddev_markbad() - Mark a block as bad
56  * @nand: NAND device
57  * @pos: position of the block to mark bad
58  *
59  * Mark a block bad. This function is updating the BBT if available and
60  * calls the low-level markbad hook (nand->ops->markbad()).
61  *
62  * Return: 0 in case of success, a negative error code otherwise.
63  */
64 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
65 {
66 	struct mtd_info *mtd = nanddev_to_mtd(nand);
67 	unsigned int entry;
68 	int ret = 0;
69 
70 	if (nanddev_isbad(nand, pos))
71 		return 0;
72 
73 	ret = nand->ops->markbad(nand, pos);
74 	if (ret)
75 		pr_warn("failed to write BBM to block @%llx (err = %d)\n",
76 			nanddev_pos_to_offs(nand, pos), ret);
77 
78 	if (!nanddev_bbt_is_initialized(nand))
79 		goto out;
80 
81 	entry = nanddev_bbt_pos_to_entry(nand, pos);
82 	ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
83 	if (ret)
84 		goto out;
85 
86 	ret = nanddev_bbt_update(nand);
87 
88 out:
89 	if (!ret)
90 		mtd->ecc_stats.badblocks++;
91 
92 	return ret;
93 }
94 EXPORT_SYMBOL_GPL(nanddev_markbad);
95 
96 /**
97  * nanddev_isreserved() - Check whether an eraseblock is reserved or not
98  * @nand: NAND device
99  * @pos: NAND position to test
100  *
101  * Checks whether the eraseblock pointed by @pos is reserved or not.
102  *
103  * Return: true if the eraseblock is reserved, false otherwise.
104  */
105 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
106 {
107 	unsigned int entry;
108 	int status;
109 
110 	if (!nanddev_bbt_is_initialized(nand))
111 		return false;
112 
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;
117 }
118 EXPORT_SYMBOL_GPL(nanddev_isreserved);
119 
120 /**
121  * nanddev_erase() - Erase a NAND portion
122  * @nand: NAND device
123  * @pos: position of the block to erase
124  *
125  * Erases the block if it's not bad.
126  *
127  * Return: 0 in case of success, a negative error code otherwise.
128  */
129 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
130 {
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));
134 		return -EIO;
135 	}
136 
137 	return nand->ops->erase(nand, pos);
138 }
139 EXPORT_SYMBOL_GPL(nanddev_erase);
140 
141 /**
142  * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
143  * @mtd: MTD device
144  * @einfo: erase request
145  *
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.
148  *
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().
153  *
154  * Return: 0 in case of success, a negative error code otherwise.
155  */
156 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
157 {
158 	struct nand_device *nand = mtd_to_nanddev(mtd);
159 	struct nand_pos pos, last;
160 	int ret;
161 
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);
166 		if (ret) {
167 			einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
168 
169 			return ret;
170 		}
171 
172 		nanddev_pos_next_eraseblock(nand, &pos);
173 	}
174 
175 	return 0;
176 }
177 EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
178 
179 /**
180  * nanddev_mtd_max_bad_blocks() - Get the maximum number of bad eraseblock on
181  *				  a specific region of the NAND device
182  * @mtd: MTD device
183  * @offs: offset of the NAND region
184  * @len: length of the NAND region
185  *
186  * Default implementation for mtd->_max_bad_blocks(). Only works if
187  * nand->memorg.max_bad_eraseblocks_per_lun is > 0.
188  *
189  * Return: a positive number encoding the maximum number of eraseblocks on a
190  * portion of memory, a negative error code otherwise.
191  */
192 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len)
193 {
194 	struct nand_device *nand = mtd_to_nanddev(mtd);
195 	struct nand_pos pos, end;
196 	unsigned int max_bb = 0;
197 
198 	if (!nand->memorg.max_bad_eraseblocks_per_lun)
199 		return -ENOTSUPP;
200 
201 	nanddev_offs_to_pos(nand, offs, &pos);
202 	nanddev_offs_to_pos(nand, offs + len, &end);
203 
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;
208 
209 	return max_bb;
210 }
211 EXPORT_SYMBOL_GPL(nanddev_mtd_max_bad_blocks);
212 
213 /**
214  * nanddev_get_ecc_engine() - Find and get a suitable ECC engine
215  * @nand: NAND device
216  */
217 static int nanddev_get_ecc_engine(struct nand_device *nand)
218 {
219 	int engine_type;
220 
221 	/* Read the user desires in terms of ECC engine/configuration */
222 	of_get_nand_ecc_user_config(nand);
223 
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;
227 
228 	switch (engine_type) {
229 	case NAND_ECC_ENGINE_TYPE_NONE:
230 		return 0;
231 	case NAND_ECC_ENGINE_TYPE_SOFT:
232 		nand->ecc.engine = nand_ecc_get_sw_engine(nand);
233 		break;
234 	case NAND_ECC_ENGINE_TYPE_ON_DIE:
235 		nand->ecc.engine = nand_ecc_get_on_die_hw_engine(nand);
236 		break;
237 	case NAND_ECC_ENGINE_TYPE_ON_HOST:
238 		pr_err("On-host hardware ECC engines not supported yet\n");
239 		break;
240 	default:
241 		pr_err("Missing ECC engine type\n");
242 	}
243 
244 	if (!nand->ecc.engine)
245 		return  -EINVAL;
246 
247 	return 0;
248 }
249 
250 /**
251  * nanddev_put_ecc_engine() - Dettach and put the in-use ECC engine
252  * @nand: NAND device
253  */
254 static int nanddev_put_ecc_engine(struct nand_device *nand)
255 {
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");
259 		break;
260 	case NAND_ECC_ENGINE_TYPE_NONE:
261 	case NAND_ECC_ENGINE_TYPE_SOFT:
262 	case NAND_ECC_ENGINE_TYPE_ON_DIE:
263 	default:
264 		break;
265 	}
266 
267 	return 0;
268 }
269 
270 /**
271  * nanddev_find_ecc_configuration() - Find a suitable ECC configuration
272  * @nand: NAND device
273  */
274 static int nanddev_find_ecc_configuration(struct nand_device *nand)
275 {
276 	int ret;
277 
278 	if (!nand->ecc.engine)
279 		return -ENOTSUPP;
280 
281 	ret = nand_ecc_init_ctx(nand);
282 	if (ret)
283 		return ret;
284 
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",
287 			nand->mtd.name);
288 
289 	return 0;
290 }
291 
292 /**
293  * nanddev_ecc_engine_init() - Initialize an ECC engine for the chip
294  * @nand: NAND device
295  */
296 int nanddev_ecc_engine_init(struct nand_device *nand)
297 {
298 	int ret;
299 
300 	/* Look for the ECC engine to use */
301 	ret = nanddev_get_ecc_engine(nand);
302 	if (ret) {
303 		pr_err("No ECC engine found\n");
304 		return ret;
305 	}
306 
307 	/* No ECC engine requested */
308 	if (!nand->ecc.engine)
309 		return 0;
310 
311 	/* Configure the engine: balance user input and chip requirements */
312 	ret = nanddev_find_ecc_configuration(nand);
313 	if (ret) {
314 		pr_err("No suitable ECC configuration\n");
315 		nanddev_put_ecc_engine(nand);
316 
317 		return ret;
318 	}
319 
320 	return 0;
321 }
322 EXPORT_SYMBOL_GPL(nanddev_ecc_engine_init);
323 
324 /**
325  * nanddev_ecc_engine_cleanup() - Cleanup ECC engine initializations
326  * @nand: NAND device
327  */
328 void nanddev_ecc_engine_cleanup(struct nand_device *nand)
329 {
330 	if (nand->ecc.engine)
331 		nand_ecc_cleanup_ctx(nand);
332 
333 	nanddev_put_ecc_engine(nand);
334 }
335 EXPORT_SYMBOL_GPL(nanddev_ecc_engine_cleanup);
336 
337 /**
338  * nanddev_init() - Initialize a NAND device
339  * @nand: NAND device
340  * @ops: NAND device operations
341  * @owner: NAND device owner
342  *
343  * Initializes a NAND device object. Consistency checks are done on @ops and
344  * @nand->memorg. Also takes care of initializing the BBT.
345  *
346  * Return: 0 in case of success, a negative error code otherwise.
347  */
348 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
349 		 struct module *owner)
350 {
351 	struct mtd_info *mtd = nanddev_to_mtd(nand);
352 	struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
353 
354 	if (!nand || !ops)
355 		return -EINVAL;
356 
357 	if (!ops->erase || !ops->markbad || !ops->isbad)
358 		return -EINVAL;
359 
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 ||
363 	    !memorg->ntargets)
364 		return -EINVAL;
365 
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;
370 
371 	nand->ops = ops;
372 
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);
381 	mtd->owner = owner;
382 
383 	return nanddev_bbt_init(nand);
384 }
385 EXPORT_SYMBOL_GPL(nanddev_init);
386 
387 /**
388  * nanddev_cleanup() - Release resources allocated in nanddev_init()
389  * @nand: NAND device
390  *
391  * Basically undoes what has been done in nanddev_init().
392  */
393 void nanddev_cleanup(struct nand_device *nand)
394 {
395 	if (nanddev_bbt_is_initialized(nand))
396 		nanddev_bbt_cleanup(nand);
397 }
398 EXPORT_SYMBOL_GPL(nanddev_cleanup);
399 
400 MODULE_DESCRIPTION("Generic NAND framework");
401 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
402 MODULE_LICENSE("GPL v2");
403