blob: f6d9c584f786eb998d501fdb2e83483f89e8c017 [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>
Patrice Chotardf2296662021-01-20 14:42:03 +010013#include <watchdog.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020014#ifndef __UBOOT__
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <linux/compat.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020016#include <linux/module.h>
17#endif
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020019#include <linux/mtd/nand.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060020#include <linux/printk.h>
Boris Brezillon894380f2018-08-16 17:30:09 +020021
22/**
23 * nanddev_isbad() - Check if a block is bad
24 * @nand: NAND device
25 * @pos: position pointing to the block we want to check
26 *
27 * Return: true if the block is bad, false otherwise.
28 */
29bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
30{
31 if (nanddev_bbt_is_initialized(nand)) {
32 unsigned int entry;
33 int status;
34
35 entry = nanddev_bbt_pos_to_entry(nand, pos);
36 status = nanddev_bbt_get_block_status(nand, entry);
37 /* Lazy block status retrieval */
38 if (status == NAND_BBT_BLOCK_STATUS_UNKNOWN) {
39 if (nand->ops->isbad(nand, pos))
40 status = NAND_BBT_BLOCK_FACTORY_BAD;
41 else
42 status = NAND_BBT_BLOCK_GOOD;
43
44 nanddev_bbt_set_block_status(nand, entry, status);
45 }
46
47 if (status == NAND_BBT_BLOCK_WORN ||
48 status == NAND_BBT_BLOCK_FACTORY_BAD)
49 return true;
50
51 return false;
52 }
53
54 return nand->ops->isbad(nand, pos);
55}
56EXPORT_SYMBOL_GPL(nanddev_isbad);
57
58/**
59 * nanddev_markbad() - Mark a block as bad
60 * @nand: NAND device
61 * @pos: position of the block to mark bad
62 *
63 * Mark a block bad. This function is updating the BBT if available and
64 * calls the low-level markbad hook (nand->ops->markbad()).
65 *
66 * Return: 0 in case of success, a negative error code otherwise.
67 */
68int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
69{
70 struct mtd_info *mtd = nanddev_to_mtd(nand);
71 unsigned int entry;
72 int ret = 0;
73
74 if (nanddev_isbad(nand, pos))
75 return 0;
76
77 ret = nand->ops->markbad(nand, pos);
78 if (ret)
79 pr_warn("failed to write BBM to block @%llx (err = %d)\n",
80 nanddev_pos_to_offs(nand, pos), ret);
81
82 if (!nanddev_bbt_is_initialized(nand))
83 goto out;
84
85 entry = nanddev_bbt_pos_to_entry(nand, pos);
86 ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
87 if (ret)
88 goto out;
89
90 ret = nanddev_bbt_update(nand);
91
92out:
93 if (!ret)
94 mtd->ecc_stats.badblocks++;
95
96 return ret;
97}
98EXPORT_SYMBOL_GPL(nanddev_markbad);
99
100/**
101 * nanddev_isreserved() - Check whether an eraseblock is reserved or not
102 * @nand: NAND device
103 * @pos: NAND position to test
104 *
105 * Checks whether the eraseblock pointed by @pos is reserved or not.
106 *
107 * Return: true if the eraseblock is reserved, false otherwise.
108 */
109bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos)
110{
111 unsigned int entry;
112 int status;
113
114 if (!nanddev_bbt_is_initialized(nand))
115 return false;
116
117 /* Return info from the table */
118 entry = nanddev_bbt_pos_to_entry(nand, pos);
119 status = nanddev_bbt_get_block_status(nand, entry);
120 return status == NAND_BBT_BLOCK_RESERVED;
121}
122EXPORT_SYMBOL_GPL(nanddev_isreserved);
123
124/**
125 * nanddev_erase() - Erase a NAND portion
126 * @nand: NAND device
127 * @pos: position of the block to erase
128 *
129 * Erases the block if it's not bad.
130 *
131 * Return: 0 in case of success, a negative error code otherwise.
132 */
Dario Binacchia5bc092fb2022-11-08 10:07:19 +0100133static int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos)
Boris Brezillon894380f2018-08-16 17:30:09 +0200134{
Mikhail Kshevetskiy9ad25432020-06-22 16:16:34 +0300135 unsigned int entry;
136
Boris Brezillon894380f2018-08-16 17:30:09 +0200137 if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) {
138 pr_warn("attempt to erase a bad/reserved block @%llx\n",
139 nanddev_pos_to_offs(nand, pos));
Mikhail Kshevetskiy9ad25432020-06-22 16:16:34 +0300140 if (nanddev_isreserved(nand, pos))
141 return -EIO;
142
143 /* remove bad block from BBT */
144 entry = nanddev_bbt_pos_to_entry(nand, pos);
145 nanddev_bbt_set_block_status(nand, entry,
146 NAND_BBT_BLOCK_STATUS_UNKNOWN);
Boris Brezillon894380f2018-08-16 17:30:09 +0200147 }
148
149 return nand->ops->erase(nand, pos);
150}
Boris Brezillon894380f2018-08-16 17:30:09 +0200151
152/**
153 * nanddev_mtd_erase() - Generic mtd->_erase() implementation for NAND devices
154 * @mtd: MTD device
155 * @einfo: erase request
156 *
157 * This is a simple mtd->_erase() implementation iterating over all blocks
158 * concerned by @einfo and calling nand->ops->erase() on each of them.
159 *
160 * Note that mtd->_erase should not be directly assigned to this helper,
161 * because there's no locking here. NAND specialized layers should instead
162 * implement there own wrapper around nanddev_mtd_erase() taking the
163 * appropriate lock before calling nanddev_mtd_erase().
164 *
165 * Return: 0 in case of success, a negative error code otherwise.
166 */
167int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
168{
169 struct nand_device *nand = mtd_to_nanddev(mtd);
170 struct nand_pos pos, last;
171 int ret;
172
173 nanddev_offs_to_pos(nand, einfo->addr, &pos);
174 nanddev_offs_to_pos(nand, einfo->addr + einfo->len - 1, &last);
175 while (nanddev_pos_cmp(&pos, &last) <= 0) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200176 schedule();
Boris Brezillon894380f2018-08-16 17:30:09 +0200177 ret = nanddev_erase(nand, &pos);
178 if (ret) {
179 einfo->fail_addr = nanddev_pos_to_offs(nand, &pos);
180
181 return ret;
182 }
183
184 nanddev_pos_next_eraseblock(nand, &pos);
185 }
186
187 return 0;
188}
189EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
190
191/**
192 * nanddev_init() - Initialize a NAND device
193 * @nand: NAND device
194 * @ops: NAND device operations
195 * @owner: NAND device owner
196 *
197 * Initializes a NAND device object. Consistency checks are done on @ops and
198 * @nand->memorg. Also takes care of initializing the BBT.
199 *
200 * Return: 0 in case of success, a negative error code otherwise.
201 */
202int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
203 struct module *owner)
204{
205 struct mtd_info *mtd = nanddev_to_mtd(nand);
206 struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
207
208 if (!nand || !ops)
209 return -EINVAL;
210
211 if (!ops->erase || !ops->markbad || !ops->isbad)
212 return -EINVAL;
213
214 if (!memorg->bits_per_cell || !memorg->pagesize ||
215 !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun ||
216 !memorg->planes_per_lun || !memorg->luns_per_target ||
217 !memorg->ntargets)
218 return -EINVAL;
219
220 nand->rowconv.eraseblock_addr_shift =
221 fls(memorg->pages_per_eraseblock - 1);
222 nand->rowconv.lun_addr_shift = fls(memorg->eraseblocks_per_lun - 1) +
223 nand->rowconv.eraseblock_addr_shift;
224
225 nand->ops = ops;
226
227 mtd->type = memorg->bits_per_cell == 1 ?
228 MTD_NANDFLASH : MTD_MLCNANDFLASH;
229 mtd->flags = MTD_CAP_NANDFLASH;
230 mtd->erasesize = memorg->pagesize * memorg->pages_per_eraseblock;
231 mtd->writesize = memorg->pagesize;
232 mtd->writebufsize = memorg->pagesize;
233 mtd->oobsize = memorg->oobsize;
234 mtd->size = nanddev_size(nand);
235 mtd->owner = owner;
236
237 return nanddev_bbt_init(nand);
238}
239EXPORT_SYMBOL_GPL(nanddev_init);
240
241/**
242 * nanddev_cleanup() - Release resources allocated in nanddev_init()
243 * @nand: NAND device
244 *
245 * Basically undoes what has been done in nanddev_init().
246 */
247void nanddev_cleanup(struct nand_device *nand)
248{
249 if (nanddev_bbt_is_initialized(nand))
250 nanddev_bbt_cleanup(nand);
251}
252EXPORT_SYMBOL_GPL(nanddev_cleanup);
253
254MODULE_DESCRIPTION("Generic NAND framework");
255MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
256MODULE_LICENSE("GPL v2");