cfi new: fix new disabling buffer support
[barebox-mini2440.git] / commands / nand.c
blob07cf5305c4a0d3df36bc8ab33360abab55c248a4
1 /*
2 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4 * See file CREDITS for list of people who contributed to this
5 * project.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <common.h>
21 #include <command.h>
22 #include <fs.h>
23 #include <linux/stat.h>
24 #include <errno.h>
25 #include <malloc.h>
26 #include <getopt.h>
27 #include <xfuncs.h>
28 #include <init.h>
29 #include <ioctl.h>
30 #include <nand.h>
31 #include <linux/mtd/mtd-abi.h>
32 #include <fcntl.h>
33 #include <libgen.h>
35 struct nand_bb {
36 char *devname;
37 char *name;
38 int open;
39 int needs_write;
41 struct mtd_info_user info;
43 size_t raw_size;
44 size_t size;
45 int fd;
46 off_t offset;
47 void *writebuf;
49 struct cdev cdev;
52 static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
53 unsigned long offset, ulong flags)
55 struct nand_bb *bb = cdev->priv;
56 int ret, bytes = 0, now;
58 debug("%s %d %d\n", __func__, offset, count);
60 while(count) {
61 ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)bb->offset);
62 if (ret < 0)
63 return ret;
65 if (ret) {
66 printf("skipping bad block at 0x%08x\n", bb->offset);
67 bb->offset += bb->info.erasesize;
68 continue;
71 now = min(count, (size_t)(bb->info.erasesize -
72 (bb->offset % bb->info.erasesize)));
73 lseek(bb->fd, bb->offset, SEEK_SET);
74 ret = read(bb->fd, buf, now);
75 if (ret < 0)
76 return ret;
77 buf += now;
78 count -= now;
79 bb->offset += now;
80 bytes += now;
83 return bytes;
86 /* Must be a multiple of the largest NAND page size */
87 #define BB_WRITEBUF_SIZE 4096
89 static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
91 int ret, now;
92 void *buf = bb->writebuf;
93 int cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
95 while (count) {
96 ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)cur_ofs);
97 if (ret < 0)
98 return ret;
100 if (ret) {
101 debug("skipping bad block at 0x%08x\n", cur_ofs);
102 bb->offset += bb->info.erasesize;
103 cur_ofs += bb->info.erasesize;
104 continue;
107 now = min(count, (size_t)(bb->info.erasesize));
108 lseek(bb->fd, cur_ofs, SEEK_SET);
109 ret = write(bb->fd, buf, now);
110 if (ret < 0)
111 return ret;
112 buf += now;
113 count -= now;
114 cur_ofs += now;
117 return 0;
120 static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
121 unsigned long offset, ulong flags)
123 struct nand_bb *bb = cdev->priv;
124 int bytes = count, now, wroffs, ret;
126 debug("%s offset: 0x%08x count: 0x%08x\n", __func__, offset, count);
128 while (count) {
129 wroffs = bb->offset % BB_WRITEBUF_SIZE;
130 now = min((int)count, BB_WRITEBUF_SIZE - wroffs);
131 memcpy(bb->writebuf + wroffs, buf, now);
133 if (wroffs + now == BB_WRITEBUF_SIZE) {
134 bb->needs_write = 0;
135 ret = nand_bb_write_buf(bb, BB_WRITEBUF_SIZE);
136 if (ret)
137 return ret;
138 } else {
139 bb->needs_write = 1;
142 bb->offset += now;
143 count -= now;
144 buf += now;
147 return bytes;
150 static int nand_bb_erase(struct cdev *cdev, size_t count, unsigned long offset)
152 struct nand_bb *bb = cdev->priv;
154 if (offset != 0) {
155 printf("can only erase from beginning of device\n");
156 return -EINVAL;
159 return erase(bb->fd, bb->raw_size, 0);
162 static int nand_bb_open(struct cdev *cdev, struct filep *f)
164 struct nand_bb *bb = cdev->priv;
166 if (bb->open)
167 return -EBUSY;
169 bb->open = 1;
170 bb->offset = 0;
171 bb->needs_write = 0;
172 bb->writebuf = xmalloc(BB_WRITEBUF_SIZE);
174 return 0;
177 static int nand_bb_close(struct cdev *cdev, struct filep *f)
179 struct nand_bb *bb = cdev->priv;
181 if (bb->needs_write)
182 nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE);
184 bb->open = 0;
185 free(bb->writebuf);
187 return 0;
190 static int nand_bb_calc_size(struct nand_bb *bb)
192 ulong pos = 0;
193 int ret;
195 while (pos < bb->raw_size) {
196 ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)pos);
197 if (ret < 0)
198 return ret;
199 if (!ret)
200 bb->cdev.size += bb->info.erasesize;
202 pos += bb->info.erasesize;
205 return 0;
208 static struct file_operations nand_bb_ops = {
209 .open = nand_bb_open,
210 .close = nand_bb_close,
211 .read = nand_bb_read,
212 .write = nand_bb_write,
213 .erase = nand_bb_erase,
217 * Add a bad block aware device ontop of another (NAND) device
218 * @param[in] dev The device to add a partition on
219 * @param[in] name Partition name (can be obtained with devinfo command)
220 * @return The device representing the new partition.
222 int dev_add_bb_dev(char *path, const char *name)
224 struct nand_bb *bb;
225 int ret;
226 struct stat s;
228 bb = xzalloc(sizeof(*bb));
229 bb->devname = asprintf("/dev/%s", basename(path));
230 if (name)
231 bb->cdev.name = strdup(name);
232 else
233 bb->cdev.name = asprintf("%s.bb", basename(path));
235 ret = stat(bb->devname, &s);
236 if (ret)
237 goto free_out;
239 bb->raw_size = s.st_size;
241 bb->fd = open(bb->devname, O_RDWR);
242 if (bb->fd < 0) {
243 ret = -ENODEV;
244 goto free_out;
247 ret = ioctl(bb->fd, MEMGETINFO, &bb->info);
248 if (ret)
249 goto free_out;
251 nand_bb_calc_size(bb);
252 bb->cdev.ops = &nand_bb_ops;
253 bb->cdev.priv = bb;
255 devfs_create(&bb->cdev);
257 return 0;
259 free_out:
260 free(bb);
261 return ret;
264 #define NAND_ADD (1 << 0)
265 #define NAND_DEL (1 << 1)
266 #define NAND_MARKBAD (1 << 2)
268 static int do_nand(cmd_tbl_t *cmdtp, int argc, char *argv[])
270 int opt;
271 struct nand_bb *bb;
272 int command = 0, badblock = 0;
274 while((opt = getopt(argc, argv, "adb:")) > 0) {
275 if (command) {
276 printf("only one command may be given\n");
277 return 1;
280 switch (opt) {
281 case 'a':
282 command = NAND_ADD;
283 break;
284 case 'd':
285 command = NAND_DEL;
286 break;
287 case 'b':
288 command = NAND_MARKBAD;
289 badblock = simple_strtoul(optarg, NULL, 0);
293 if (command & NAND_ADD) {
294 while (optind < argc) {
295 if (dev_add_bb_dev(argv[optind], NULL))
296 return 1;
298 optind++;
302 if (command & NAND_DEL) {
303 while (optind < argc) {
304 struct cdev *cdev;
306 cdev = cdev_by_name(basename(argv[optind]));
307 if (!cdev) {
308 printf("no such device: %s\n", argv[optind]);
309 return 1;
311 bb = cdev->priv;
312 close(bb->fd);
313 devfs_remove(cdev);
314 free(bb);
315 optind++;
319 if (command & NAND_MARKBAD) {
320 if (optind < argc) {
321 int ret = 0, fd;
323 printf("marking block at 0x%08x on %s as bad\n", badblock, argv[optind]);
325 fd = open(argv[optind], O_RDWR);
326 if (fd < 0) {
327 perror("open");
328 return 1;
331 ret = ioctl(fd, MEMSETBADBLOCK, (void *)badblock);
332 if (ret)
333 perror("ioctl");
335 close(fd);
336 return ret;
340 return 0;
343 static const __maybe_unused char cmd_nand_help[] =
344 "Usage: nand [OPTION]...\n"
345 "nand related commands\n"
346 " -a <dev> register a bad block aware device ontop of a normal nand device\n"
347 " -d <dev> deregister a bad block aware device\n"
348 " -b <ofs> <dev> mark block at offset ofs as bad\n";
350 U_BOOT_CMD_START(nand)
351 .cmd = do_nand,
352 .usage = "",
353 U_BOOT_CMD_HELP(cmd_nand_help)
354 U_BOOT_CMD_END