summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand/nand-bb.c
blob: c0104c59362d15daca082297fc53e0bb8078f67f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <common.h>
#include <command.h>
#include <fs.h>
#include <linux/stat.h>
#include <errno.h>
#include <malloc.h>
#include <getopt.h>
#include <xfuncs.h>
#include <init.h>
#include <ioctl.h>
#include <nand.h>
#include <linux/mtd/mtd.h>
#include <fcntl.h>
#include <libgen.h>
#include <linux/list.h>
#include <linux/err.h>

struct nand_bb {
	char *name;
	int open;
	int needs_write;

	struct mtd_info *mtd;

	loff_t offset;
	unsigned long flags;
	void *writebuf;

	struct cdev cdev;

	struct list_head list;
};

static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
	loff_t offset, ulong flags)
{
	struct nand_bb *bb = cdev->priv;
	size_t retlen;
	int ret, bytes = 0, now;

	debug("%s offset: 0x%08llx (raw: 0x%08llx) count: 0x%08zx\n",
			__func__, offset, bb->offset, count);

	while (count) {
		loff_t max = bb->mtd->size - bb->offset;

		if (max <= 0)
			break;

		if (mtd_block_isbad(bb->mtd, bb->offset)) {
			printf("skipping bad block at 0x%08llx\n", bb->offset);
			bb->offset += bb->mtd->erasesize;
			continue;
		}

		now = min(count, (size_t)(bb->mtd->erasesize -
				((size_t)bb->offset % bb->mtd->erasesize)));

		if (now > max)
			now = max;

		ret = mtd_read(bb->mtd, bb->offset, now, &retlen, buf);
		if (ret < 0)
			return ret;
		buf += retlen;
		count -= retlen;
		bb->offset += retlen;
		bytes += retlen;
	};

	return bytes;
}

/* Must be a multiple of the largest NAND page size */
#define BB_WRITEBUF_SIZE	8192

#ifdef CONFIG_MTD_WRITE
static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
{
	int ret, now;
	size_t retlen;
	void *buf = bb->writebuf;
	loff_t cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);

	while (count) {
		loff_t max = bb->mtd->size - cur_ofs;

		if (max <= 0)
			return -ENOSPC;

		if (mtd_block_isbad(bb->mtd, cur_ofs)) {
			debug("skipping bad block at 0x%08llx\n", cur_ofs);
			bb->offset += bb->mtd->erasesize;
			cur_ofs += bb->mtd->erasesize;
			continue;
		}

		now = min(count, (size_t)(bb->mtd->erasesize));
		if (now > max)
			return -ENOSPC;

		ret = mtd_write(bb->mtd, cur_ofs, now, &retlen, buf);
		if (ret < 0)
			return ret;
		buf += retlen;
		count -= retlen;
		cur_ofs += retlen;
	};

	return 0;
}

static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
	loff_t offset, ulong flags)
{
	struct nand_bb *bb = cdev->priv;
	int bytes = count, now, wroffs, ret;

	debug("%s offset: 0x%08llx (raw: 0x%08llx) count: 0x%08zx\n",
			__func__, offset, bb->offset, count);

	while (count) {
		wroffs = bb->offset % BB_WRITEBUF_SIZE;
		now = min((int)count, BB_WRITEBUF_SIZE - wroffs);
		memcpy(bb->writebuf + wroffs, buf, now);

		if (wroffs + now == BB_WRITEBUF_SIZE) {
			bb->needs_write = 0;
			ret = nand_bb_write_buf(bb, BB_WRITEBUF_SIZE);
			if (ret)
				return ret;
		} else {
			bb->needs_write = 1;
		}

		bb->offset += now;
		count -= now;
		buf += now;
	}

	return bytes;
}

static int nand_bb_erase(struct cdev *cdev, loff_t count, loff_t offset)
{
	struct nand_bb *bb = cdev->priv;
	struct erase_info erase = {};
	int ret;

	if (offset != 0) {
		printf("can only erase from beginning of device\n");
		return -EINVAL;
	}

	while (count) {
		if (offset + bb->mtd->erasesize > bb->mtd->size)
			return 0;

		if (mtd_block_isbad(bb->mtd, offset)) {
			offset += bb->mtd->erasesize;
			continue;
		}

		erase.addr = offset;
		erase.len = bb->mtd->erasesize;

		ret = mtd_erase(bb->mtd, &erase);
		if (ret)
			return ret;

		offset += bb->mtd->erasesize;
		count -= bb->mtd->erasesize;
	}

	return 0;
}
#endif

static int nand_bb_open(struct cdev *cdev, unsigned long flags)
{
	struct nand_bb *bb = cdev->priv;

	if (bb->open)
		return -EBUSY;

	bb->flags = flags;
	bb->open = 1;
	bb->offset = 0;
	bb->needs_write = 0;
	bb->writebuf = xmalloc(BB_WRITEBUF_SIZE);

	return 0;
}

static int nand_bb_close(struct cdev *cdev)
{
	struct nand_bb *bb = cdev->priv;

#ifdef CONFIG_MTD_WRITE
	if (bb->needs_write)
		nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE);
#endif
	bb->open = 0;
	free(bb->writebuf);

	return 0;
}

static int nand_bb_calc_size(struct nand_bb *bb)
{
	loff_t pos = 0;

	while (pos < bb->mtd->size) {
		if (!mtd_block_isbad(bb->mtd, pos))
			bb->cdev.size += bb->mtd->erasesize;

		pos += bb->mtd->erasesize;
	}

	return 0;
}

static int nand_bb_lseek(struct cdev *cdev, loff_t offset)
{
	struct nand_bb *bb = cdev->priv;
	loff_t raw_pos = 0;

	/* lseek only in readonly mode */
	if (bb->flags & O_ACCMODE)
		return -ENOSYS;
	while (raw_pos < bb->mtd->size) {
		off_t now = min_t(loff_t, offset, bb->mtd->erasesize);

		if (mtd_block_isbad(bb->mtd, raw_pos)) {
			raw_pos += bb->mtd->erasesize;
		} else {
			offset -= now;
			raw_pos += now;
		}

		if (!offset) {
			bb->offset = raw_pos;
			return 0;
		}
	}

	return -EINVAL;
}

static struct cdev_operations nand_bb_ops = {
	.open   = nand_bb_open,
	.close  = nand_bb_close,
	.read  	= nand_bb_read,
	.lseek	= nand_bb_lseek,
#ifdef CONFIG_MTD_WRITE
	.write 	= nand_bb_write,
	.erase	= nand_bb_erase,
#endif
};

static LIST_HEAD(bb_list);

struct cdev *mtd_add_bb(struct mtd_info *mtd, const char *name)
{
	struct nand_bb *bb;
	int ret;

	bb = xzalloc(sizeof(*bb));
	bb->mtd = mtd;

	if (name)
		bb->cdev.name = xstrdup(name);
	else
		bb->cdev.name = basprintf("%s.bb", mtd->cdev.name);

	nand_bb_calc_size(bb);
	bb->cdev.ops = &nand_bb_ops;
	bb->cdev.priv = bb;

	ret = devfs_create(&bb->cdev);
	if (ret)
		goto err;

	list_add_tail(&bb->list, &bb_list);

	return &bb->cdev;

err:
	free(bb);
	return ERR_PTR(ret);
}

void mtd_del_bb(struct mtd_info *mtd)
{
	struct cdev *cdev = mtd->cdev_bb;
	struct nand_bb *bb = container_of(cdev, struct nand_bb, cdev);

	devfs_remove(&bb->cdev);
	list_del_init(&bb->list);
	free(bb->name);
	free(bb);

	mtd->cdev_bb = NULL;
}

/**
 * Add a bad block aware device ontop of another (NAND) device
 * @param[in] dev The device to add a partition on
 * @param[in] name Partition name (can be obtained with devinfo command)
 * @return The device representing the new partition.
 */
int dev_add_bb_dev(const char *path, const char *name)
{
	struct cdev *parent, *cdev;

	parent = cdev_by_name(path);
	if (!parent)
		return -ENODEV;

	if (!parent->mtd)
		return -EINVAL;

	cdev = mtd_add_bb(parent->mtd, name);

	return PTR_ERR(cdev);
}

int dev_remove_bb_dev(const char *name)
{
	struct nand_bb *bb, *tmp;

	list_for_each_entry_safe(bb, tmp, &bb_list, list) {
		if (!strcmp(bb->cdev.name, name)) {
			mtd_del_bb(bb->mtd);
			return 0;
		}
	}

	return -ENODEV;
}