summaryrefslogtreecommitdiffstats
path: root/commands/nand.c
blob: f5c019487704e218751c9eefc57203a8f8ce855a (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
/*
 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#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-abi.h>

struct nand_bb {
	struct device_d device;

	struct device_d *physdev;

	int open;

	struct mtd_info_user info;

	off_t offset;
};

static ssize_t nand_bb_read(struct device_d *dev, void *buf, size_t count,
	unsigned long offset, ulong flags)
{
	struct nand_bb *bb = dev->priv;
	int ret;

	debug("%s %d %d\n", __func__, offset, count);

	do {
		ret = dev_ioctl(bb->physdev, MEMGETBADBLOCK, (void *)bb->offset);
		if (ret < 0)
			return ret;

		if (ret) {
			printf("skipping bad block at 0x%08x\n", bb->offset);
			bb->offset += bb->info.erasesize;
		}
	} while (ret);

	ret = dev_read(bb->physdev, buf, count, bb->offset, flags);
	if (ret > 0)
		bb->offset += ret;

	return ret;
}

static ssize_t nand_bb_write(struct device_d *dev, const void *buf, size_t count,
	unsigned long offset, ulong flags)
{
	struct nand_bb *bb = dev->priv;
	int ret;

	debug("%s %d %d\n", __func__, offset, count);

	do {
		ret = dev_ioctl(bb->physdev, MEMGETBADBLOCK, (void *)bb->offset);
		if (ret < 0)
			return ret;

		if (ret) {
			printf("skipping bad block at 0x%08x\n", bb->offset);
			bb->offset += bb->info.erasesize;
		}
	} while (ret);

	ret = dev_write(bb->physdev, buf, count, bb->offset, flags);
	if (ret > 0)
		bb->offset += ret;

	return ret;
}

static int nand_bb_open(struct device_d *dev, struct filep *f)
{
	struct nand_bb *bb = dev->priv;

	if (bb->open)
		return -EBUSY;

	bb->open = 1;
	bb->offset = 0;

	return 0;
}

static int nand_bb_close(struct device_d *dev, struct filep *f)
{
	struct nand_bb *bb = dev->priv;

	bb->open = 0;

	return 0;
}

static int nand_bb_probe(struct device_d *dev)
{
	struct nand_bb *bb = dev->priv;
	int ret = 0;

	ret = dev_ioctl(bb->physdev, MEMGETINFO, &bb->info);
	if (ret < 0)
		goto out;

	return 0;
out:
	free(bb);
	return ret;
}

static int nand_bb_remove(struct device_d *dev)
{
	return 0;
}

struct driver_d nand_bb_driver = {
	.name  	= "nand_bb",
	.probe 	= nand_bb_probe,
	.remove = nand_bb_remove,
	.open   = nand_bb_open,
	.close  = nand_bb_close,
	.read  	= nand_bb_read,
	.write 	= nand_bb_write,
	.type	= DEVICE_TYPE_NAND_BB,
};

static int nand_bb_init(void)
{
	return register_driver(&nand_bb_driver);
}

device_initcall(nand_bb_init);

static int do_nand (cmd_tbl_t *cmdtp, int argc, char *argv[])
{
	int opt;
	char *add_device = NULL, *del_device = NULL;
	struct device_d *dev;
	struct nand_bb *bb;

	getopt_reset();

	while((opt = getopt(argc, argv, "a:d:")) > 0) {
		switch(opt) {
		case 'a':
			add_device = optarg;
			break;
		case 'd':
			del_device = optarg;
			break;
		}
	}

	if (add_device) {
		dev = get_device_by_path(add_device);
		if (!dev) {
			printf("no such device: %s\n", add_device);
			return 1;
		}

		if (dev->type != DEVICE_TYPE_NAND) {
			printf("not a nand device: %s\n", add_device);
			return 1;
		}

		bb = xzalloc(sizeof(*bb));
		sprintf(bb->device.id, "%s.bb", dev->id);
		strcpy(bb->device.name, "nand_bb");
		bb->device.priv = bb;
		bb->device.size = dev->size; /* FIXME: Bad blocks? */
		bb->device.type	= DEVICE_TYPE_NAND_BB;
		bb->physdev = dev;

		if (register_device(&bb->device))
			goto free_out;

		dev_add_child(dev, &bb->device);
	}

	if (del_device) {
		dev = get_device_by_path(del_device);
		if (!dev) {
			printf("no such device: %s\n", del_device);
			return 1;
		}

		if (dev->type != DEVICE_TYPE_NAND_BB) {
			printf("not a nand bb device: %s\n", dev);
			return 1;
		}

		bb = dev->priv;
		unregister_device(dev);
		free(bb);
	}

free_out:
	return 0;
}

static const __maybe_unused char cmd_nand_help[] =
"Usage: nand [OPTION]...\n"
"nand related commands\n"
"  -a  <dev>  register a bad block aware device ontop of a normal nand device\n"
"  -d  <dev>  deregister a bad block aware device\n";

U_BOOT_CMD_START(nand)
	.maxargs	= CONFIG_MAXARGS,
	.cmd		= do_nand,
	.usage		= "",
	U_BOOT_CMD_HELP(cmd_nand_help)
U_BOOT_CMD_END