blob: 219efdc8959a365b5ff423fa33cb45f20e926231 [file] [log] [blame]
Boris Brezillon894380f2018-08-16 17:30:09 +02001// 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
Miquel Raynal4f64c632019-10-03 19:50:21 +020012#include <common.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020013#ifndef __UBOOT__
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <linux/compat.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020015#include <linux/module.h>
16#endif
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020018#include <linux/mtd/nand.h>
19
20/**
21 * nanddev_isbad() - Check if a block is bad
22 * @nand: NAND device
23 * @pos: position pointing to the block we want to check
24 *
25 * Return: true if the block is bad, false otherwise.
26 */
27bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
28{
29 if (nanddev_bbt_is_initialized(nand)) {
30 unsigned int entry;
31 int status;
32
33 entry = nanddev_bbt_pos_to_entry(nand, pos);
34 status = nanddev_bbt_get_block_status(nand, entry);
35 /* Lazy block status retrieval */
36 if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
37 if (nand->ops->isbad(nand, pos))
38 status = NAND_BBT_BLOCK_FACTORY_BAD;
39 else
40 status = NAND_BBT_BLOCK_GOOD;
41
42 nanddev_bbt_set_block_status(nand, entry, status);
43 }
44
45 if (status == NAND_BBT_BLOCK_WORN ||
46 status == NAND_BBT_BLOCK_FACTORY_BAD)
47 return true;
48
49 return false;
50 }
51
52 return nand->ops->isbad(nand, pos);
53}
54EXPORT_SYMBOL_GPL(nanddev_isbad);
55
56/**
57 * nanddev_markbad() - Mark a block as bad
58 * @nand: NAND device
59 * @pos: position of the block to mark bad
60 *
61 * Mark a block bad. This function is updating the BBT if available and
62 * calls the low-level markbad hook (nand->ops->markbad()).
63 *
64 * Return: 0 in case of success, a negative error code otherwise.
65 */
66int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
67{
68 struct mtd_info *mtd = nanddev_to_mtd(nand);
69 unsigned int entry;
70 int ret = 0;
71
72 if (nanddev_isbad(nand, pos))
73 return 0;
74
75 ret = nand->ops->markbad(nand, pos);
76 if (ret)
77 pr_warn("failed to write BBM to block @%llx (err = %d)\n",
78 nanddev_pos_to_offs(nand, pos), ret);
79
80 if (!nanddev_bbt_is_initialized(nand))
81 goto out;
82
83 entry = nanddev_bbt_pos_to_entry(nand, pos);
84 ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
85 if (ret)
86 goto out;
87
88 ret = nanddev_bbt_update(nand);
89
90out:
91 if (!ret)
92 mtd->ecc_stats.badblocks++;
93
94 return ret;
95}
96EXPORT_SYMBOL_GPL(nanddev_markbad);
97
98/**
99 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
100 * @nand: NAND device
101 * @pos: NAND position to test
102 *
103 * Checks whether the eraseblock pointed by @pos is reserved or not.
104 *
105 * Return: true if the eraseblock is reserved, false otherwise.
106 */
107bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
108{
109 unsigned int entry;
110 int status;
111
112 if (!nanddev_bbt_is_initialized(nand))
113 return false;
114
115 /* Return info from the table */
116 entry = nanddev_bbt_pos_to_entry(nand, pos);
117 status = nanddev_bbt_get_block_status(nand, entry);
118 return status == NAND_BBT_BLOCK_RESERVED;
119}
120EXPORT_SYMBOL_GPL(nanddev_isreserved);
121
122/**
123 * nanddev_erase() - Erase a NAND portion
124 * @nand: NAND device
125 * @pos: position of the block to erase
126 *
127 * Erases the block if it's not bad.
128 *
129 * Return: 0 in case of success, a negative error code otherwise.
130 */
131int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
132{
Mikhail Kshevetskiy9ad25432020-06-22 16:16:34 +0300133 unsigned int entry;
134
Boris Brezillon894380f2018-08-16 17:30:09 +0200135 if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
136 pr_warn("attempt to erase a bad/reserved block @%llx\n",
137 nanddev_pos_to_offs(nand, pos));
Mikhail Kshevetskiy9ad25432020-06-22 16:16:34 +0300138 if (nanddev_isreserved(nand, pos))
139 return -EIO;
140
141 /* remove bad block from BBT */
142 entry = nanddev_bbt_pos_to_entry(nand, pos);
143 nanddev_bbt_set_block_status(nand, entry,
144 NAND_BBT_BLOCK_STATUS_UNKNOWN);
Boris Brezillon894380f2018-08-16 17:30:09 +0200145 }
146
147 return nand->ops->erase(nand, pos);
148}
149EXPORT_SYMBOL_GPL(nanddev_erase);
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 */
166int 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 ret = nanddev_erase(nand, &pos);
176 if (ret) {
177 einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
178
179 return ret;
180 }
181
182 nanddev_pos_next_eraseblock(nand, &pos);
183 }
184
185 return 0;
186}
187EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
188
189/**
190 * nanddev_init() - Initialize a NAND device
191 * @nand: NAND device
192 * @ops: NAND device operations
193 * @owner: NAND device owner
194 *
195 * Initializes a NAND device object. Consistency checks are done on @ops and
196 * @nand->memorg. Also takes care of initializing the BBT.
197 *
198 * Return: 0 in case of success, a negative error code otherwise.
199 */
200int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
201 struct module *owner)
202{
203 struct mtd_info *mtd = nanddev_to_mtd(nand);
204 struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
205
206 if (!nand || !ops)
207 return -EINVAL;
208
209 if (!ops->erase || !ops->markbad || !ops->isbad)
210 return -EINVAL;
211
212 if (!memorg->bits_per_cell || !memorg->pagesize ||
213 !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
214 !memorg->planes_per_lun || !memorg->luns_per_target ||
215 !memorg->ntargets)
216 return -EINVAL;
217
218 nand->rowconv.eraseblock_addr_shift =
219 fls(memorg->pages_per_eraseblock - 1);
220 nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
221 nand->rowconv.eraseblock_addr_shift;
222
223 nand->ops = ops;
224
225 mtd->type = memorg->bits_per_cell == 1 ?
226 MTD_NANDFLASH : MTD_MLCNANDFLASH;
227 mtd->flags = MTD_CAP_NANDFLASH;
228 mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
229 mtd->writesize = memorg->pagesize;
230 mtd->writebufsize = memorg->pagesize;
231 mtd->oobsize = memorg->oobsize;
232 mtd->size = nanddev_size(nand);
233 mtd->owner = owner;
234
235 return nanddev_bbt_init(nand);
236}
237EXPORT_SYMBOL_GPL(nanddev_init);
238
239/**
240 * nanddev_cleanup() - Release resources allocated in nanddev_init()
241 * @nand: NAND device
242 *
243 * Basically undoes what has been done in nanddev_init().
244 */
245void nanddev_cleanup(struct nand_device *nand)
246{
247 if (nanddev_bbt_is_initialized(nand))
248 nanddev_bbt_cleanup(nand);
249}
250EXPORT_SYMBOL_GPL(nanddev_cleanup);
251
252MODULE_DESCRIPTION("Generic NAND framework");
253MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
254MODULE_LICENSE("GPL v2");