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