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 <common.h>
13 #include <watchdog.h>
14 #ifndef __UBOOT__
15 #include <linux/compat.h>
16 #include <linux/module.h>
17 #endif
18 #include <linux/bitops.h>
19 #include <linux/mtd/nand.h>
20 
21 /**
22  * nanddev_isbad() - Check if a block is bad
23  * @nand: NAND device
24  * @pos: position pointing to the block we want to check
25  *
26  * Return: true if the block is bad, false otherwise.
27  */
nanddev_isbad(struct nand_device * nand,const struct nand_pos * pos)28 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
29 {
30 	if (nanddev_bbt_is_initialized(nand)) {
31 		unsigned int entry;
32 		int status;
33 
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;
40 			else
41 				status = NAND_BBT_BLOCK_GOOD;
42 
43 			nanddev_bbt_set_block_status(nand, entry, status);
44 		}
45 
46 		if (status == NAND_BBT_BLOCK_WORN ||
47 		    status == NAND_BBT_BLOCK_FACTORY_BAD)
48 			return true;
49 
50 		return false;
51 	}
52 
53 	return nand->ops->isbad(nand, pos);
54 }
55 EXPORT_SYMBOL_GPL(nanddev_isbad);
56 
57 /**
58  * nanddev_markbad() - Mark a block as bad
59  * @nand: NAND device
60  * @pos: position of the block to mark bad
61  *
62  * Mark a block bad. This function is updating the BBT if available and
63  * calls the low-level markbad hook (nand->ops->markbad()).
64  *
65  * Return: 0 in case of success, a negative error code otherwise.
66  */
nanddev_markbad(struct nand_device * nand,const struct nand_pos * pos)67 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
68 {
69 	struct mtd_info *mtd = nanddev_to_mtd(nand);
70 	unsigned int entry;
71 	int ret = 0;
72 
73 	if (nanddev_isbad(nand, pos))
74 		return 0;
75 
76 	ret = nand->ops->markbad(nand, pos);
77 	if (ret)
78 		pr_warn("failed to write BBM to block @%llx (err = %d)\n",
79 			nanddev_pos_to_offs(nand, pos), ret);
80 
81 	if (!nanddev_bbt_is_initialized(nand))
82 		goto out;
83 
84 	entry = nanddev_bbt_pos_to_entry(nand, pos);
85 	ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
86 	if (ret)
87 		goto out;
88 
89 	ret = nanddev_bbt_update(nand);
90 
91 out:
92 	if (!ret)
93 		mtd->ecc_stats.badblocks++;
94 
95 	return ret;
96 }
97 EXPORT_SYMBOL_GPL(nanddev_markbad);
98 
99 /**
100  * nanddev_isreserved() - Check whether an eraseblock is reserved or not
101  * @nand: NAND device
102  * @pos: NAND position to test
103  *
104  * Checks whether the eraseblock pointed by @pos is reserved or not.
105  *
106  * Return: true if the eraseblock is reserved, false otherwise.
107  */
nanddev_isreserved(struct nand_device * nand,const struct nand_pos * pos)108 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
109 {
110 	unsigned int entry;
111 	int status;
112 
113 	if (!nanddev_bbt_is_initialized(nand))
114 		return false;
115 
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;
120 }
121 EXPORT_SYMBOL_GPL(nanddev_isreserved);
122 
123 /**
124  * nanddev_erase() - Erase a NAND portion
125  * @nand: NAND device
126  * @pos: position of the block to erase
127  *
128  * Erases the block if it's not bad.
129  *
130  * Return: 0 in case of success, a negative error code otherwise.
131  */
nanddev_erase(struct nand_device * nand,const struct nand_pos * pos)132 static int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
133 {
134 	unsigned int entry;
135 
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))
140 			return -EIO;
141 
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);
146 	}
147 
148 	return nand->ops->erase(nand, pos);
149 }
150 
151 /**
152  * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
153  * @mtd: MTD device
154  * @einfo: erase request
155  *
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.
158  *
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().
163  *
164  * Return: 0 in case of success, a negative error code otherwise.
165  */
nanddev_mtd_erase(struct mtd_info * mtd,struct erase_info * einfo)166 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
167 {
168 	struct nand_device *nand = mtd_to_nanddev(mtd);
169 	struct nand_pos pos, last;
170 	int ret;
171 
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) {
175 		schedule();
176 		ret = nanddev_erase(nand, &pos);
177 		if (ret) {
178 			einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
179 
180 			return ret;
181 		}
182 
183 		nanddev_pos_next_eraseblock(nand, &pos);
184 	}
185 
186 	return 0;
187 }
188 EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
189 
190 /**
191  * nanddev_init() - Initialize a NAND device
192  * @nand: NAND device
193  * @ops: NAND device operations
194  * @owner: NAND device owner
195  *
196  * Initializes a NAND device object. Consistency checks are done on @ops and
197  * @nand->memorg. Also takes care of initializing the BBT.
198  *
199  * Return: 0 in case of success, a negative error code otherwise.
200  */
nanddev_init(struct nand_device * nand,const struct nand_ops * ops,struct module * owner)201 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
202 		 struct module *owner)
203 {
204 	struct mtd_info *mtd = nanddev_to_mtd(nand);
205 	struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
206 
207 	if (!nand || !ops)
208 		return -EINVAL;
209 
210 	if (!ops->erase || !ops->markbad || !ops->isbad)
211 		return -EINVAL;
212 
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 ||
216 	    !memorg->ntargets)
217 		return -EINVAL;
218 
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;
223 
224 	nand->ops = ops;
225 
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);
234 	mtd->owner = owner;
235 
236 	return nanddev_bbt_init(nand);
237 }
238 EXPORT_SYMBOL_GPL(nanddev_init);
239 
240 /**
241  * nanddev_cleanup() - Release resources allocated in nanddev_init()
242  * @nand: NAND device
243  *
244  * Basically undoes what has been done in nanddev_init().
245  */
nanddev_cleanup(struct nand_device * nand)246 void nanddev_cleanup(struct nand_device *nand)
247 {
248 	if (nanddev_bbt_is_initialized(nand))
249 		nanddev_bbt_cleanup(nand);
250 }
251 EXPORT_SYMBOL_GPL(nanddev_cleanup);
252 
253 MODULE_DESCRIPTION("Generic NAND framework");
254 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
255 MODULE_LICENSE("GPL v2");
256