blob: 06cc1405675a64f6d3c6dd637e9562817527e5ac [file] [log] [blame]
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09001/*
2 * U-Boot command for OneNAND support
3 *
Stefan Roese9743d282008-11-12 13:47:24 +01004 * Copyright (C) 2005-2008 Samsung Electronics
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09005 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
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 as
9 * published by the Free Software Foundation.
10 */
11
12#include <common.h>
13#include <command.h>
Stefan Roese9743d282008-11-12 13:47:24 +010014#include <malloc.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090015
Mike Frysinger11d1a092012-04-09 13:39:55 +000016#include <linux/compat.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090017#include <linux/mtd/mtd.h>
18#include <linux/mtd/onenand.h>
19
20#include <asm/io.h>
21
Stefan Roese9743d282008-11-12 13:47:24 +010022static struct mtd_info *mtd;
23
24static loff_t next_ofs;
25static loff_t skip_ofs;
26
27static inline int str2long(char *p, ulong *num)
28{
29 char *endptr;
30
31 *num = simple_strtoul(p, &endptr, 16);
32 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
33}
34
Wolfgang Denk6262d0212010-06-28 22:00:46 +020035static int arg_off_size(int argc, char * const argv[], ulong *off, size_t *size)
Stefan Roese9743d282008-11-12 13:47:24 +010036{
37 if (argc >= 1) {
38 if (!(str2long(argv[0], off))) {
39 printf("'%s' is not a number\n", argv[0]);
40 return -1;
41 }
42 } else {
43 *off = 0;
44 }
45
46 if (argc >= 2) {
47 if (!(str2long(argv[1], (ulong *)size))) {
48 printf("'%s' is not a number\n", argv[1]);
49 return -1;
50 }
51 } else {
52 *size = mtd->size - *off;
53 }
54
55 if ((*off + *size) > mtd->size) {
Stefan Roese586b3a62009-05-11 16:03:55 +020056 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
Stefan Roese9743d282008-11-12 13:47:24 +010057 return -1;
58 }
59
60 if (*size == mtd->size)
61 puts("whole chip\n");
62 else
63 printf("offset 0x%lx, size 0x%x\n", *off, *size);
64
65 return 0;
66}
67
68static int onenand_block_read(loff_t from, size_t len,
69 size_t *retlen, u_char *buf, int oob)
70{
71 struct onenand_chip *this = mtd->priv;
72 int blocks = (int) len >> this->erase_shift;
73 int blocksize = (1 << this->erase_shift);
74 loff_t ofs = from;
75 struct mtd_oob_ops ops = {
76 .retlen = 0,
77 };
78 int ret;
79
80 if (oob)
81 ops.ooblen = blocksize;
82 else
83 ops.len = blocksize;
84
85 while (blocks) {
Sergey Lapin3a38a552013-01-14 03:46:50 +000086 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +010087 if (ret) {
88 printk("Bad blocks %d at 0x%x\n",
89 (u32)(ofs >> this->erase_shift), (u32)ofs);
90 ofs += blocksize;
91 continue;
92 }
93
94 if (oob)
95 ops.oobbuf = buf;
96 else
97 ops.datbuf = buf;
98
99 ops.retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000100 ret = mtd_read_oob(mtd, ofs, &ops);
Stefan Roese9743d282008-11-12 13:47:24 +0100101 if (ret) {
102 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
103 ofs += blocksize;
104 continue;
105 }
106 ofs += blocksize;
107 buf += blocksize;
108 blocks--;
109 *retlen += ops.retlen;
110 }
111
112 return 0;
113}
114
Lei Wen2ca9e142010-12-02 05:17:55 +0000115static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf,
116 size_t *retlen)
117{
118 struct mtd_oob_ops ops = {
119 .len = mtd->writesize,
120 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000121 .mode = MTD_OPS_AUTO_OOB,
Lei Wen2ca9e142010-12-02 05:17:55 +0000122 };
123 int page, ret = 0;
124 for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) {
125 ops.datbuf = (u_char *)buf;
126 buf += mtd->writesize;
127 ops.oobbuf = (u_char *)buf;
128 buf += mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000129 ret = mtd_write_oob(mtd, to, &ops);
Lei Wen2ca9e142010-12-02 05:17:55 +0000130 if (ret)
131 break;
132 to += mtd->writesize;
133 }
134
135 *retlen = (ret) ? 0 : mtd->erasesize;
136 return ret;
137}
138
Stefan Roese9743d282008-11-12 13:47:24 +0100139static int onenand_block_write(loff_t to, size_t len,
Lei Wen2ca9e142010-12-02 05:17:55 +0000140 size_t *retlen, const u_char * buf, int withoob)
Stefan Roese9743d282008-11-12 13:47:24 +0100141{
142 struct onenand_chip *this = mtd->priv;
143 int blocks = len >> this->erase_shift;
144 int blocksize = (1 << this->erase_shift);
145 loff_t ofs;
146 size_t _retlen = 0;
147 int ret;
148
149 if (to == next_ofs) {
150 next_ofs = to + len;
151 to += skip_ofs;
152 } else {
153 next_ofs = to + len;
154 skip_ofs = 0;
155 }
156 ofs = to;
157
158 while (blocks) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000159 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100160 if (ret) {
161 printk("Bad blocks %d at 0x%x\n",
162 (u32)(ofs >> this->erase_shift), (u32)ofs);
163 skip_ofs += blocksize;
164 goto next;
165 }
166
Lei Wen2ca9e142010-12-02 05:17:55 +0000167 if (!withoob)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000168 ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
Lei Wen2ca9e142010-12-02 05:17:55 +0000169 else
170 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
Stefan Roese9743d282008-11-12 13:47:24 +0100171 if (ret) {
172 printk("Write failed 0x%x, %d", (u32)ofs, ret);
173 skip_ofs += blocksize;
174 goto next;
175 }
176
177 buf += blocksize;
178 blocks--;
179 *retlen += _retlen;
180next:
181 ofs += blocksize;
182 }
183
184 return 0;
185}
186
187static int onenand_block_erase(u32 start, u32 size, int force)
188{
189 struct onenand_chip *this = mtd->priv;
190 struct erase_info instr = {
191 .callback = NULL,
192 };
193 loff_t ofs;
194 int ret;
195 int blocksize = 1 << this->erase_shift;
196
197 for (ofs = start; ofs < (start + size); ofs += blocksize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000198 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100199 if (ret && !force) {
200 printf("Skip erase bad block %d at 0x%x\n",
201 (u32)(ofs >> this->erase_shift), (u32)ofs);
202 continue;
203 }
204
205 instr.addr = ofs;
206 instr.len = blocksize;
207 instr.priv = force;
208 instr.mtd = mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000209 ret = mtd_erase(mtd, &instr);
Stefan Roese9743d282008-11-12 13:47:24 +0100210 if (ret) {
211 printf("erase failed block %d at 0x%x\n",
212 (u32)(ofs >> this->erase_shift), (u32)ofs);
213 continue;
214 }
215 }
216
217 return 0;
218}
219
220static int onenand_block_test(u32 start, u32 size)
221{
222 struct onenand_chip *this = mtd->priv;
223 struct erase_info instr = {
224 .callback = NULL,
225 .priv = 0,
226 };
227
228 int blocks;
229 loff_t ofs;
230 int blocksize = 1 << this->erase_shift;
231 int start_block, end_block;
232 size_t retlen;
233 u_char *buf;
234 u_char *verify_buf;
235 int ret;
236
237 buf = malloc(blocksize);
238 if (!buf) {
239 printf("Not enough malloc space available!\n");
240 return -1;
241 }
242
243 verify_buf = malloc(blocksize);
244 if (!verify_buf) {
245 printf("Not enough malloc space available!\n");
246 return -1;
247 }
248
249 start_block = start >> this->erase_shift;
250 end_block = (start + size) >> this->erase_shift;
251
252 /* Protect boot-loader from badblock testing */
253 if (start_block < 2)
254 start_block = 2;
255
256 if (end_block > (mtd->size >> this->erase_shift))
257 end_block = mtd->size >> this->erase_shift;
258
259 blocks = start_block;
260 ofs = start;
261 while (blocks < end_block) {
262 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
263
Sergey Lapin3a38a552013-01-14 03:46:50 +0000264 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100265 if (ret) {
266 printf("Skip erase bad block %d at 0x%x\n",
267 (u32)(ofs >> this->erase_shift), (u32)ofs);
268 goto next;
269 }
270
271 instr.addr = ofs;
272 instr.len = blocksize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000273 ret = mtd_erase(mtd, &instr);
Stefan Roese9743d282008-11-12 13:47:24 +0100274 if (ret) {
275 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
276 goto next;
277 }
278
Sergey Lapin3a38a552013-01-14 03:46:50 +0000279 ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
Stefan Roese9743d282008-11-12 13:47:24 +0100280 if (ret) {
281 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
282 goto next;
283 }
284
Sergey Lapin3a38a552013-01-14 03:46:50 +0000285 ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
Stefan Roese9743d282008-11-12 13:47:24 +0100286 if (ret) {
287 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
288 goto next;
289 }
290
291 if (memcmp(buf, verify_buf, blocksize))
292 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
293
294next:
295 ofs += blocksize;
296 blocks++;
297 }
298 printf("...Done\n");
299
300 free(buf);
301 free(verify_buf);
302
303 return 0;
304}
305
306static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
307{
308 int i;
309 u_char *datbuf, *oobbuf, *p;
310 struct mtd_oob_ops ops;
311 loff_t addr;
312
313 datbuf = malloc(mtd->writesize + mtd->oobsize);
314 oobbuf = malloc(mtd->oobsize);
315 if (!datbuf || !oobbuf) {
316 puts("No memory for page buffer\n");
317 return 1;
318 }
319 off &= ~(mtd->writesize - 1);
320 addr = (loff_t) off;
321 memset(&ops, 0, sizeof(ops));
322 ops.datbuf = datbuf;
Lei Wene816b8e2010-12-07 09:38:18 +0800323 ops.oobbuf = oobbuf;
Stefan Roese9743d282008-11-12 13:47:24 +0100324 ops.len = mtd->writesize;
325 ops.ooblen = mtd->oobsize;
326 ops.retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000327 i = mtd_read_oob(mtd, addr, &ops);
Stefan Roese9743d282008-11-12 13:47:24 +0100328 if (i < 0) {
329 printf("Error (%d) reading page %08lx\n", i, off);
330 free(datbuf);
331 free(oobbuf);
332 return 1;
333 }
334 printf("Page %08lx dump:\n", off);
335 i = mtd->writesize >> 4;
336 p = datbuf;
337
338 while (i--) {
339 if (!only_oob)
340 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
341 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
342 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
343 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
344 p[15]);
345 p += 16;
346 }
347 puts("OOB:\n");
348 i = mtd->oobsize >> 3;
Lei Wene816b8e2010-12-07 09:38:18 +0800349 p = oobbuf;
350
Stefan Roese9743d282008-11-12 13:47:24 +0100351 while (i--) {
352 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
353 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
354 p += 8;
355 }
356 free(datbuf);
357 free(oobbuf);
358
359 return 0;
360}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900361
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200362static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900363{
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100364 printf("%s\n", mtd->name);
365 return 0;
366}
367
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200368static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100369{
370 ulong ofs;
371
372 mtd = &onenand_mtd;
373 /* Currently only one OneNAND device is supported */
374 printf("\nDevice %d bad blocks:\n", 0);
375 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000376 if (mtd_block_isbad(mtd, ofs))
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100377 printf(" %08x\n", (u32)ofs);
378 }
379
380 return 0;
381}
382
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200383static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100384{
385 char *s;
386 int oob = 0;
Stefan Roese9743d282008-11-12 13:47:24 +0100387 ulong addr, ofs;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100388 size_t len;
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200389 int ret = 0;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100390 size_t retlen = 0;
Stefan Roese9743d282008-11-12 13:47:24 +0100391
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100392 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000393 return CMD_RET_USAGE;
Stefan Roese9743d282008-11-12 13:47:24 +0100394
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100395 s = strchr(argv[0], '.');
396 if ((s != NULL) && (!strcmp(s, ".oob")))
397 oob = 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900398
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100399 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900400
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100401 printf("\nOneNAND read: ");
402 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
403 return 1;
Stefan Roese9743d282008-11-12 13:47:24 +0100404
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100405 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
Stefan Roese9743d282008-11-12 13:47:24 +0100406
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100407 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
408
409 return ret == 0 ? 0 : 1;
410}
411
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200412static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100413{
414 ulong addr, ofs;
415 size_t len;
Lei Wen2ca9e142010-12-02 05:17:55 +0000416 int ret = 0, withoob = 0;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100417 size_t retlen = 0;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900418
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100419 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000420 return CMD_RET_USAGE;
Kyungmin Park012f2aa2008-03-31 10:40:19 +0900421
Lei Wen2ca9e142010-12-02 05:17:55 +0000422 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
423 withoob = 1;
424
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100425 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900426
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100427 printf("\nOneNAND write: ");
428 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
429 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900430
Lei Wen2ca9e142010-12-02 05:17:55 +0000431 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900432
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100433 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900434
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100435 return ret == 0 ? 0 : 1;
436}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900437
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200438static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100439{
440 ulong ofs;
441 int ret = 0;
442 size_t len;
443 int force;
444
445 /*
446 * Syntax is:
447 * 0 1 2 3 4
448 * onenand erase [force] [off size]
449 */
450 argc--;
451 argv++;
452 if (argc)
453 {
454 if (!strcmp("force", argv[0]))
455 {
456 force = 1;
457 argc--;
458 argv++;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900459 }
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100460 }
461 printf("\nOneNAND erase: ");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900462
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100463 /* skip first two or three arguments, look for offset and size */
464 if (arg_off_size(argc, argv, &ofs, &len) != 0)
465 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900466
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100467 ret = onenand_block_erase(ofs, len, force);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900468
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100469 printf("%s\n", ret ? "ERROR" : "OK");
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900470
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100471 return ret == 0 ? 0 : 1;
472}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900473
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200474static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100475{
476 ulong ofs;
477 int ret = 0;
478 size_t len;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900479
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100480 /*
481 * Syntax is:
482 * 0 1 2 3 4
483 * onenand test [force] [off size]
484 */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900485
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100486 printf("\nOneNAND test: ");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900487
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100488 /* skip first two or three arguments, look for offset and size */
489 if (arg_off_size(argc - 1, argv + 1, &ofs, &len) != 0)
490 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900491
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100492 ret = onenand_block_test(ofs, len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900493
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100494 printf("%s\n", ret ? "ERROR" : "OK");
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200495
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100496 return ret == 0 ? 0 : 1;
497}
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200498
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200499static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100500{
501 ulong ofs;
502 int ret = 0;
503 char *s;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900504
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100505 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000506 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900507
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100508 s = strchr(argv[0], '.');
509 ofs = (int)simple_strtoul(argv[1], NULL, 16);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900510
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100511 if (s != NULL && strcmp(s, ".oob") == 0)
512 ret = onenand_dump(mtd, ofs, 1);
513 else
514 ret = onenand_dump(mtd, ofs, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900515
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100516 return ret == 0 ? 1 : 0;
517}
518
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200519static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100520{
521 int ret = 0;
522 ulong addr;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900523
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100524 argc -= 2;
525 argv += 2;
526
527 if (argc <= 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000528 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900529
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100530 while (argc > 0) {
531 addr = simple_strtoul(*argv, NULL, 16);
532
Sergey Lapin3a38a552013-01-14 03:46:50 +0000533 if (mtd_block_markbad(mtd, addr)) {
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100534 printf("block 0x%08lx NOT marked "
535 "as bad! ERROR %d\n",
536 addr, ret);
537 ret = 1;
538 } else {
539 printf("block 0x%08lx successfully "
540 "marked as bad\n",
541 addr);
542 }
543 --argc;
544 ++argv;
545 }
546 return ret;
547}
548
549static cmd_tbl_t cmd_onenand_sub[] = {
550 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
551 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
552 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
553 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
Lei Wen2ca9e142010-12-02 05:17:55 +0000554 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100555 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
556 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
557 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
558 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
559};
Stefan Roese9743d282008-11-12 13:47:24 +0100560
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200561#ifdef CONFIG_NEEDS_MANUAL_RELOC
Enric Balletbo i Serraaac73592010-10-19 11:13:22 +0200562void onenand_reloc(void) {
563 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
564}
565#endif
566
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200567static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100568{
569 cmd_tbl_t *c;
570
Enric Balletbo i Serraa9153942010-10-19 11:13:21 +0200571 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000572 return CMD_RET_USAGE;
Enric Balletbo i Serraa9153942010-10-19 11:13:21 +0200573
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100574 mtd = &onenand_mtd;
575
576 /* Strip off leading 'onenand' command argument */
577 argc--;
578 argv++;
579
580 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
581
Wolfgang Denk3b683112010-07-17 01:06:04 +0200582 if (c)
583 return c->cmd(cmdtp, flag, argc, argv);
584 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000585 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900586}
587
588U_BOOT_CMD(
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200589 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600590 "OneNAND sub-system",
Stefan Roese9743d282008-11-12 13:47:24 +0100591 "info - show available OneNAND devices\n"
592 "onenand bad - show bad blocks\n"
593 "onenand read[.oob] addr off size\n"
Lei Wen2ca9e142010-12-02 05:17:55 +0000594 "onenand write[.yaffs] addr off size\n"
Stefan Roese9743d282008-11-12 13:47:24 +0100595 " read/write 'size' bytes starting at offset 'off'\n"
596 " to/from memory address 'addr', skipping bad blocks.\n"
597 "onenand erase [force] [off size] - erase 'size' bytes from\n"
598 "onenand test [off size] - test 'size' bytes from\n"
599 " offset 'off' (entire device if not specified)\n"
600 "onenand dump[.oob] off - dump page\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200601 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900602);