Update CHANGELOG.
[people/ms/u-boot.git] / common / cmd_nand.c
CommitLineData
dc7c9a1a
WD
1/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
384cc687
WD
6 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
dc7c9a1a
WD
9 */
10
11#include <common.h>
addb2e16
BS
12
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
21
22#if (CONFIG_COMMANDS & CFG_CMD_NAND)
23
24#include <command.h>
25#include <watchdog.h>
26#include <malloc.h>
27#include <asm/byteorder.h>
28
29#ifdef CONFIG_SHOW_BOOT_PROGRESS
30# include <status_led.h>
31# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
32#else
33# define SHOW_BOOT_PROGRESS(arg)
34#endif
35
36#include <jffs2/jffs2.h>
37#include <nand.h>
38
39extern nand_info_t nand_info[]; /* info for NAND chips */
40
41static int nand_dump_oob(nand_info_t *nand, ulong off)
42{
43 return 0;
44}
45
46static int nand_dump(nand_info_t *nand, ulong off)
47{
48 int i;
49 u_char *buf, *p;
50
51 buf = malloc(nand->oobblock + nand->oobsize);
52 if (!buf) {
53 puts("No memory for page buffer\n");
54 return 1;
55 }
56 off &= ~(nand->oobblock - 1);
57 i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
58 if (i < 0) {
59 printf("Error (%d) reading page %08x\n", i, off);
60 free(buf);
61 return 1;
62 }
63 printf("Page %08x dump:\n", off);
64 i = nand->oobblock >> 4; p = buf;
65 while (i--) {
66 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
67 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
68 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
69 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
70 p += 16;
71 }
72 puts("OOB:\n");
73 i = nand->oobsize >> 3;
74 while (i--) {
75 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
76 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
77 p += 8;
78 }
79 free(buf);
80
81 return 0;
82}
83
84/* ------------------------------------------------------------------------- */
85
86static void
87arg_off_size(int argc, char *argv[], ulong *off, ulong *size, ulong totsize)
88{
89 *off = 0;
90 *size = 0;
91
92#if defined(CONFIG_JFFS2_NAND) && defined(CFG_JFFS_CUSTOM_PART)
93 if (argc >= 1 && strcmp(argv[0], "partition") == 0) {
94 int part_num;
95 struct part_info *part;
96 const char *partstr;
97
98 if (argc >= 2)
99 partstr = argv[1];
100 else
101 partstr = getenv("partition");
102
103 if (partstr)
104 part_num = (int)simple_strtoul(partstr, NULL, 10);
105 else
106 part_num = 0;
107
108 part = jffs2_part_info(part_num);
109 if (part == NULL) {
110 printf("\nInvalid partition %d\n", part_num);
111 return;
112 }
113 *size = part->size;
114 *off = (ulong)part->offset;
115 } else
116#endif
117 {
118 if (argc >= 1)
119 *off = (ulong)simple_strtoul(argv[0], NULL, 16);
120 else
121 *off = 0;
122
123 if (argc >= 2)
124 *size = (ulong)simple_strtoul(argv[1], NULL, 16);
125 else
126 *size = totsize - *off;
127
128 }
129
130}
131
132int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
133{
134 int i, dev, ret;
135 ulong addr, off, size;
136 char *cmd, *s;
137 nand_info_t *nand;
2255b2d2
SR
138 int quiet = 0;
139 const char *quiet_str = getenv("quiet");
addb2e16
BS
140
141 /* at least two arguments please */
142 if (argc < 2)
143 goto usage;
144
2255b2d2
SR
145 if (quiet_str)
146 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
147
addb2e16
BS
148 cmd = argv[1];
149
150 if (strcmp(cmd, "info") == 0) {
151
152 putc('\n');
153 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
154 if (nand_info[i].name)
155 printf("Device %d: %s, sector size %lu KiB\n",
156 i, nand_info[i].name,
157 nand_info[i].erasesize >> 10);
158 }
159 return 0;
160 }
161
162 if (strcmp(cmd, "device") == 0) {
163
164 if (argc < 3) {
165 if ((nand_curr_device < 0) ||
166 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
167 puts("\nno devices available\n");
168 else
169 printf("\nDevice %d: %s\n", nand_curr_device,
170 nand_info[nand_curr_device].name);
171 return 0;
172 }
173 dev = (int)simple_strtoul(argv[2], NULL, 10);
174 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
175 puts("No such device\n");
176 return 1;
177 }
178 printf("Device %d: %s", dev, nand_info[dev].name);
179 puts("... is now current device\n");
180 nand_curr_device = dev;
43a2b0e7
SR
181
182#ifdef CFG_NAND_SELECT_DEVICE
183 /*
184 * Select the chip in the board/cpu specific driver
185 */
186 board_nand_select_device(nand_info[dev].priv, dev);
187#endif
188
addb2e16
BS
189 return 0;
190 }
191
192 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
193 strncmp(cmd, "dump", 4) != 0 &&
2255b2d2
SR
194 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
195 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
196 strcmp(cmd, "biterr") != 0 &&
197 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
addb2e16
BS
198 goto usage;
199
200 /* the following commands operate on the current device */
201 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
202 !nand_info[nand_curr_device].name) {
203 puts("\nno devices available\n");
204 return 1;
205 }
206 nand = &nand_info[nand_curr_device];
207
208 if (strcmp(cmd, "bad") == 0) {
209 printf("\nDevice %d bad blocks:\n", nand_curr_device);
210 for (off = 0; off < nand->size; off += nand->erasesize)
211 if (nand_block_isbad(nand, off))
212 printf(" %08x\n", off);
213 return 0;
214 }
215
2255b2d2
SR
216 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
217 nand_erase_options_t opts;
218 int clean = argc >= 3 && !strcmp("clean", argv[2]);
219 int rest_argc = argc - 2;
220 char **rest_argv = argv + 2;
221 int scrub = !strcmp(cmd, "scrub");
222
223 if (clean) {
224 rest_argc--;
225 rest_argv++;
226 }
227
228 if (rest_argc == 0) {
addb2e16 229
2255b2d2
SR
230 printf("\nNAND %s: device %d whole chip\n",
231 cmd,
232 nand_curr_device);
233
234 off = size = 0;
235 } else {
236 arg_off_size(rest_argc, rest_argv, &off, &size,
237 nand->size);
238
239 if (off == 0 && size == 0)
240 return 1;
241
242 printf("\nNAND %s: device %d offset 0x%x, size 0x%x\n",
243 cmd, nand_curr_device, off, size);
244 }
245
246 memset(&opts, 0, sizeof(opts));
247 opts.offset = off;
248 opts.length = size;
249 opts.jffs2 = clean;
250 opts.quiet = quiet;
251
252 if (scrub) {
253 printf("Warning: "
254 "scrub option will erase all factory set "
255 "bad blocks!\n"
256 " "
257 "There is no reliable way to recover them.\n"
258 " "
259 "Use this command only for testing purposes "
260 "if you\n"
261 " "
87621bc2 262 "are sure of what you are doing!\n"
2255b2d2
SR
263 "\nReally scrub this NAND flash? <y/N>\n"
264 );
265
266 if (getc() == 'y' && getc() == '\r') {
267 opts.scrub = 1;
268 } else {
269 printf("scrub aborted\n");
270 return -1;
271 }
272 }
273 ret = nand_erase_opts(nand, &opts);
addb2e16
BS
274 printf("%s\n", ret ? "ERROR" : "OK");
275
276 return ret == 0 ? 0 : 1;
277 }
278
279 if (strncmp(cmd, "dump", 4) == 0) {
280 if (argc < 3)
281 goto usage;
282
283 s = strchr(cmd, '.');
284 off = (int)simple_strtoul(argv[2], NULL, 16);
285
286 if (s != NULL && strcmp(s, ".oob") == 0)
287 ret = nand_dump_oob(nand, off);
288 else
289 ret = nand_dump(nand, off);
290
291 return ret == 0 ? 1 : 0;
292
293 }
294
295 /* read write */
296 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
2255b2d2
SR
297 int read;
298
addb2e16
BS
299 if (argc < 4)
300 goto usage;
2255b2d2 301
addb2e16
BS
302 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
303
304 arg_off_size(argc - 3, argv + 3, &off, &size, nand->size);
305 if (off == 0 && size == 0)
306 return 1;
307
2255b2d2 308 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
addb2e16 309 printf("\nNAND %s: device %d offset %u, size %u ... ",
2255b2d2 310 read ? "read" : "write", nand_curr_device, off, size);
addb2e16 311
2255b2d2
SR
312 s = strchr(cmd, '.');
313 if (s != NULL &&
314 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
315 if (read) {
316 /* read */
317 nand_read_options_t opts;
318 memset(&opts, 0, sizeof(opts));
319 opts.buffer = (u_char*) addr;
320 opts.length = size;
321 opts.offset = off;
322 opts.quiet = quiet;
323 ret = nand_read_opts(nand, &opts);
324 } else {
325 /* write */
326 nand_write_options_t opts;
327 memset(&opts, 0, sizeof(opts));
328 opts.buffer = (u_char*) addr;
329 opts.length = size;
330 opts.offset = off;
331 /* opts.forcejffs2 = 1; */
332 opts.pad = 1;
333 opts.blockalign = 1;
334 opts.quiet = quiet;
335 ret = nand_write_opts(nand, &opts);
336 }
337 printf("%s\n", ret ? "ERROR" : "OK");
338 return ret == 0 ? 0 : 1;
339 }
340
341 if (read)
addb2e16
BS
342 ret = nand_read(nand, off, &size, (u_char *)addr);
343 else
344 ret = nand_write(nand, off, &size, (u_char *)addr);
345
346 printf(" %d bytes %s: %s\n", size,
2255b2d2 347 read ? "read" : "written", ret ? "ERROR" : "OK");
addb2e16
BS
348
349 return ret == 0 ? 0 : 1;
350 }
2255b2d2 351
2255b2d2
SR
352 if (strcmp(cmd, "markbad") == 0) {
353 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
354
355 int ret = nand->block_markbad(nand, addr);
356 if (ret == 0) {
357 printf("block 0x%08lx successfully marked as bad\n",
358 (ulong) addr);
359 return 0;
360 } else {
361 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
362 (ulong) addr, ret);
363 }
364 return 1;
365 }
366 if (strcmp(cmd, "biterr") == 0) {
367 /* todo */
368 return 1;
369 }
370
371 if (strcmp(cmd, "lock") == 0) {
372 int tight = 0;
373 int status = 0;
374 if (argc == 3) {
375 if (!strcmp("tight", argv[2]))
376 tight = 1;
377 if (!strcmp("status", argv[2]))
378 status = 1;
379 }
380
381 if (status) {
382 ulong block_start = 0;
383 ulong off;
384 int last_status = -1;
385
386 struct nand_chip *nand_chip = nand->priv;
387 /* check the WP bit */
388 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
389 printf("device is %swrite protected\n",
390 (nand_chip->read_byte(nand) & 0x80 ?
391 "NOT " : "" ) );
392
393 for (off = 0; off < nand->size; off += nand->oobblock) {
394 int s = nand_get_lock_status(nand, off);
395
396 /* print message only if status has changed
397 * or at end of chip
398 */
399 if (off == nand->size - nand->oobblock
400 || (s != last_status && off != 0)) {
401
402 printf("%08x - %08x: %8d pages %s%s%s\n",
403 block_start,
404 off-1,
405 (off-block_start)/nand->oobblock,
406 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
407 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
408 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
409 }
410
411 last_status = s;
412 }
413 } else {
414 if (!nand_lock(nand, tight)) {
415 printf ("NAND flash successfully locked\n");
416 } else {
417 printf ("Error locking NAND flash. \n");
418 return 1;
419 }
420 }
421 return 0;
422 }
423
424 if (strcmp(cmd, "unlock") == 0) {
425 if (argc == 2) {
426 off = 0;
427 size = nand->size;
428 } else {
429 arg_off_size(argc - 2, argv + 2, &off, &size,
430 nand->size);
431 }
432
433 if (!nand_unlock(nand, off, size)) {
434 printf("NAND flash successfully unlocked\n");
435 } else {
436 printf("Error unlocking NAND flash. "
437 "Write and erase will probably fail\n");
438 return 1;
439 }
440 return 0;
441 }
442
addb2e16
BS
443usage:
444 printf("Usage:\n%s\n", cmdtp->usage);
445 return 1;
446}
447
448U_BOOT_CMD(nand, 5, 1, do_nand,
449 "nand - NAND sub-system\n",
450 "info - show available NAND devices\n"
451 "nand device [dev] - show or set current device\n"
452 "nand read[.jffs2] - addr off size\n"
453 "nand write[.jffs2] - addr off size - read/write `size' bytes starting\n"
454 " at offset `off' to/from memory address `addr'\n"
455 "nand erase [clean] [off size] - erase `size' bytes from\n"
456 " offset `off' (entire device if not specified)\n"
457 "nand bad - show bad blocks\n"
458 "nand dump[.oob] off - dump page\n"
459 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
460 "nand markbad off - mark bad block at offset (UNSAFE)\n"
2255b2d2
SR
461 "nand biterr off - make a bit error at offset (UNSAFE)\n"
462 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
463 "nand unlock [offset] [size] - unlock section\n");
addb2e16
BS
464
465int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
466{
467 char *boot_device = NULL;
468 char *ep;
469 int dev;
470 int r;
471 ulong addr, cnt, offset = 0;
472 image_header_t *hdr;
473 nand_info_t *nand;
474
475 switch (argc) {
476 case 1:
477 addr = CFG_LOAD_ADDR;
478 boot_device = getenv("bootdevice");
479 break;
480 case 2:
481 addr = simple_strtoul(argv[1], NULL, 16);
482 boot_device = getenv("bootdevice");
483 break;
484 case 3:
485 addr = simple_strtoul(argv[1], NULL, 16);
486 boot_device = argv[2];
487 break;
488 case 4:
489 addr = simple_strtoul(argv[1], NULL, 16);
490 boot_device = argv[2];
491 offset = simple_strtoul(argv[3], NULL, 16);
492 break;
493 default:
494 printf("Usage:\n%s\n", cmdtp->usage);
495 SHOW_BOOT_PROGRESS(-1);
496 return 1;
497 }
498
499 if (!boot_device) {
500 puts("\n** No boot device **\n");
501 SHOW_BOOT_PROGRESS(-1);
502 return 1;
503 }
504
505 dev = simple_strtoul(boot_device, &ep, 16);
506
507 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
508 printf("\n** Device %d not available\n", dev);
509 SHOW_BOOT_PROGRESS(-1);
510 return 1;
511 }
512
513 nand = &nand_info[dev];
514 printf("\nLoading from device %d: %s (offset 0x%lx)\n",
515 dev, nand->name, offset);
516
517 cnt = nand->oobblock;
518 r = nand_read(nand, offset, &cnt, (u_char *) addr);
519 if (r) {
520 printf("** Read error on %d\n", dev);
521 SHOW_BOOT_PROGRESS(-1);
522 return 1;
523 }
524
525 hdr = (image_header_t *) addr;
526
527 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
528 printf("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
529 SHOW_BOOT_PROGRESS(-1);
530 return 1;
531 }
532
533 print_image_hdr(hdr);
534
535 cnt = (ntohl(hdr->ih_size) + sizeof (image_header_t));
536
537 r = nand_read(nand, offset, &cnt, (u_char *) addr);
538 if (r) {
539 printf("** Read error on %d\n", dev);
540 SHOW_BOOT_PROGRESS(-1);
541 return 1;
542 }
543
544 /* Loading ok, update default load address */
545
546 load_addr = addr;
547
548 /* Check if we should attempt an auto-start */
549 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
550 char *local_args[2];
551 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
552
553 local_args[0] = argv[0];
554 local_args[1] = NULL;
555
556 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
557
558 do_bootm(cmdtp, 0, 1, local_args);
559 return 1;
560 }
561 return 0;
562}
563
564U_BOOT_CMD(nboot, 4, 1, do_nandboot,
565 "nboot - boot from NAND device\n", "loadAddr dev\n");
566
567
568#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
569
570#else /* CFG_NAND_LEGACY */
571/*
572 *
573 * Legacy NAND support - to be phased out
574 *
575 */
dc7c9a1a
WD
576#include <command.h>
577#include <malloc.h>
578#include <asm/io.h>
a3d991bd 579#include <watchdog.h>
dc7c9a1a
WD
580
581#ifdef CONFIG_SHOW_BOOT_PROGRESS
582# include <status_led.h>
583# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
584#else
585# define SHOW_BOOT_PROGRESS(arg)
586#endif
587
addb2e16
BS
588#if (CONFIG_COMMANDS & CFG_CMD_NAND)
589#include <linux/mtd/nand_legacy.h>
590#if 0
dc7c9a1a 591#include <linux/mtd/nand_ids.h>
7a8e9bed 592#include <jffs2/jffs2.h>
addb2e16 593#endif
dc7c9a1a 594
1f4bb37d
WD
595#ifdef CONFIG_OMAP1510
596void archflashwp(void *archdata, int wp);
597#endif
598
599#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
600
a43278a4 601#undef NAND_DEBUG
dc7c9a1a 602#undef PSYCHO_DEBUG
7a8e9bed
WD
603
604/* ****************** WARNING *********************
605 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
606 * erase (or at least attempt to erase) blocks that are marked
607 * bad. This can be very handy if you are _sure_ that the block
608 * is OK, say because you marked a good block bad to test bad
609 * block handling and you are done testing, or if you have
610 * accidentally marked blocks bad.
611 *
612 * Erasing factory marked bad blocks is a _bad_ idea. If the
613 * erase succeeds there is no reliable way to find them again,
614 * and attempting to program or erase bad blocks can affect
615 * the data in _other_ (good) blocks.
616 */
617#define ALLOW_ERASE_BAD_DEBUG 0
dc7c9a1a
WD
618
619#define CONFIG_MTD_NAND_ECC /* enable ECC */
1f4bb37d 620#define CONFIG_MTD_NAND_ECC_JFFS2
dc7c9a1a 621
addb2e16 622/* bits for nand_legacy_rw() `cmd'; or together as needed */
7a8e9bed
WD
623#define NANDRW_READ 0x01
624#define NANDRW_WRITE 0x00
625#define NANDRW_JFFS2 0x02
a3d991bd 626#define NANDRW_JFFS2_SKIP 0x04
7a8e9bed 627
addb2e16
BS
628/*
629 * Imports from nand_legacy.c
630 */
631extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
632extern int curr_device;
633extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
634 size_t len, int clean);
635extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
636 size_t len, size_t *retlen, u_char *buf);
637extern void nand_print(struct nand_chip *nand);
638extern void nand_print_bad(struct nand_chip *nand);
639extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
640 size_t len, size_t *retlen, u_char *buf);
641extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
642 size_t len, size_t *retlen, const u_char *buf);
dc7c9a1a 643
dc7c9a1a
WD
644
645int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
646{
647 int rcode = 0;
648
649 switch (argc) {
650 case 0:
651 case 1:
652 printf ("Usage:\n%s\n", cmdtp->usage);
653 return 1;
654 case 2:
8bde7f77 655 if (strcmp(argv[1],"info") == 0) {
dc7c9a1a
WD
656 int i;
657
658 putc ('\n');
659
660 for (i=0; i<CFG_MAX_NAND_DEVICE; ++i) {
661 if(nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
662 continue; /* list only known devices */
663 printf ("Device %d: ", i);
664 nand_print(&nand_dev_desc[i]);
665 }
666 return 0;
667
668 } else if (strcmp(argv[1],"device") == 0) {
669 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
670 puts ("\nno devices available\n");
671 return 1;
672 }
673 printf ("\nDevice %d: ", curr_device);
674 nand_print(&nand_dev_desc[curr_device]);
675 return 0;
7a8e9bed
WD
676
677 } else if (strcmp(argv[1],"bad") == 0) {
678 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
679 puts ("\nno devices available\n");
680 return 1;
681 }
682 printf ("\nDevice %d bad blocks:\n", curr_device);
683 nand_print_bad(&nand_dev_desc[curr_device]);
684 return 0;
685
dc7c9a1a
WD
686 }
687 printf ("Usage:\n%s\n", cmdtp->usage);
688 return 1;
689 case 3:
690 if (strcmp(argv[1],"device") == 0) {
691 int dev = (int)simple_strtoul(argv[2], NULL, 10);
692
693 printf ("\nDevice %d: ", dev);
694 if (dev >= CFG_MAX_NAND_DEVICE) {
695 puts ("unknown device\n");
696 return 1;
697 }
698 nand_print(&nand_dev_desc[dev]);
699 /*nand_print (dev);*/
700
701 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
702 return 1;
703 }
704
705 curr_device = dev;
706
707 puts ("... is now current device\n");
708
709 return 0;
710 }
7a8e9bed
WD
711 else if (strcmp(argv[1],"erase") == 0 && strcmp(argv[2], "clean") == 0) {
712 struct nand_chip* nand = &nand_dev_desc[curr_device];
713 ulong off = 0;
714 ulong size = nand->totlen;
715 int ret;
716
717 printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
718 curr_device, off, size);
719
addb2e16 720 ret = nand_legacy_erase (nand, off, size, 1);
7a8e9bed
WD
721
722 printf("%s\n", ret ? "ERROR" : "OK");
723
724 return ret;
725 }
dc7c9a1a
WD
726
727 printf ("Usage:\n%s\n", cmdtp->usage);
728 return 1;
729 default:
730 /* at least 4 args */
731
7a8e9bed
WD
732 if (strncmp(argv[1], "read", 4) == 0 ||
733 strncmp(argv[1], "write", 5) == 0) {
dc7c9a1a
WD
734 ulong addr = simple_strtoul(argv[2], NULL, 16);
735 ulong off = simple_strtoul(argv[3], NULL, 16);
736 ulong size = simple_strtoul(argv[4], NULL, 16);
7a8e9bed
WD
737 int cmd = (strncmp(argv[1], "read", 4) == 0) ?
738 NANDRW_READ : NANDRW_WRITE;
dc7c9a1a 739 int ret, total;
7a8e9bed
WD
740 char* cmdtail = strchr(argv[1], '.');
741
742 if (cmdtail && !strncmp(cmdtail, ".oob", 2)) {
743 /* read out-of-band data */
744 if (cmd & NANDRW_READ) {
745 ret = nand_read_oob(nand_dev_desc + curr_device,
77ddac94 746 off, size, (size_t *)&total,
7a8e9bed
WD
747 (u_char*)addr);
748 }
749 else {
750 ret = nand_write_oob(nand_dev_desc + curr_device,
77ddac94 751 off, size, (size_t *)&total,
7a8e9bed
WD
752 (u_char*)addr);
753 }
754 return ret;
755 }
756 else if (cmdtail && !strncmp(cmdtail, ".jffs2", 2))
757 cmd |= NANDRW_JFFS2; /* skip bad blocks */
a3d991bd
WD
758 else if (cmdtail && !strncmp(cmdtail, ".jffs2s", 2)) {
759 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
760 if (cmd & NANDRW_READ)
761 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
762 }
7a8e9bed
WD
763#ifdef SXNI855T
764 /* need ".e" same as ".j" for compatibility with older units */
765 else if (cmdtail && !strcmp(cmdtail, ".e"))
766 cmd |= NANDRW_JFFS2; /* skip bad blocks */
767#endif
a842a6d2
SR
768#ifdef CFG_NAND_SKIP_BAD_DOT_I
769 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
770 /* ".i" for image -> read skips bad block (no 0xff) */
9bcf2ab4 771 else if (cmdtail && !strcmp(cmdtail, ".i")) {
a842a6d2
SR
772 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
773 if (cmd & NANDRW_READ)
774 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
9bcf2ab4 775 }
a842a6d2 776#endif /* CFG_NAND_SKIP_BAD_DOT_I */
7a8e9bed
WD
777 else if (cmdtail) {
778 printf ("Usage:\n%s\n", cmdtp->usage);
779 return 1;
780 }
dc7c9a1a 781
2255b2d2 782 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
7a8e9bed
WD
783 (cmd & NANDRW_READ) ? "read" : "write",
784 curr_device, off, size);
dc7c9a1a 785
addb2e16 786 ret = nand_legacy_rw(nand_dev_desc + curr_device, cmd, off, size,
77ddac94 787 (size_t *)&total, (u_char*)addr);
dc7c9a1a 788
1f4bb37d 789 printf (" %d bytes %s: %s\n", total,
998eaaec 790 (cmd & NANDRW_READ) ? "read" : "written",
dc7c9a1a
WD
791 ret ? "ERROR" : "OK");
792
793 return ret;
7a8e9bed
WD
794 } else if (strcmp(argv[1],"erase") == 0 &&
795 (argc == 4 || strcmp("clean", argv[2]) == 0)) {
796 int clean = argc == 5;
797 ulong off = simple_strtoul(argv[2 + clean], NULL, 16);
798 ulong size = simple_strtoul(argv[3 + clean], NULL, 16);
dc7c9a1a
WD
799 int ret;
800
2255b2d2 801 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
dc7c9a1a
WD
802 curr_device, off, size);
803
addb2e16
BS
804 ret = nand_legacy_erase (nand_dev_desc + curr_device,
805 off, size, clean);
dc7c9a1a
WD
806
807 printf("%s\n", ret ? "ERROR" : "OK");
808
809 return ret;
810 } else {
811 printf ("Usage:\n%s\n", cmdtp->usage);
812 rcode = 1;
813 }
814
815 return rcode;
816 }
817}
818
0d498393
WD
819U_BOOT_CMD(
820 nand, 5, 1, do_nand,
2255b2d2 821 "nand - legacy NAND sub-system\n",
b0fce99b
WD
822 "info - show available NAND devices\n"
823 "nand device [dev] - show or set current device\n"
a3d991bd 824 "nand read[.jffs2[s]] addr off size\n"
b0fce99b
WD
825 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
826 " at offset `off' to/from memory address `addr'\n"
827 "nand erase [clean] [off size] - erase `size' bytes from\n"
828 " offset `off' (entire device if not specified)\n"
829 "nand bad - show bad blocks\n"
830 "nand read.oob addr off size - read out-of-band data\n"
831 "nand write.oob addr off size - read out-of-band data\n"
832);
833
dc7c9a1a
WD
834int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
835{
836 char *boot_device = NULL;
837 char *ep;
838 int dev;
839 ulong cnt;
840 ulong addr;
841 ulong offset = 0;
842 image_header_t *hdr;
843 int rcode = 0;
844 switch (argc) {
845 case 1:
846 addr = CFG_LOAD_ADDR;
847 boot_device = getenv ("bootdevice");
848 break;
849 case 2:
850 addr = simple_strtoul(argv[1], NULL, 16);
851 boot_device = getenv ("bootdevice");
852 break;
853 case 3:
854 addr = simple_strtoul(argv[1], NULL, 16);
855 boot_device = argv[2];
856 break;
857 case 4:
858 addr = simple_strtoul(argv[1], NULL, 16);
859 boot_device = argv[2];
860 offset = simple_strtoul(argv[3], NULL, 16);
861 break;
862 default:
863 printf ("Usage:\n%s\n", cmdtp->usage);
864 SHOW_BOOT_PROGRESS (-1);
865 return 1;
866 }
867
868 if (!boot_device) {
869 puts ("\n** No boot device **\n");
870 SHOW_BOOT_PROGRESS (-1);
871 return 1;
872 }
873
874 dev = simple_strtoul(boot_device, &ep, 16);
875
876 if ((dev >= CFG_MAX_NAND_DEVICE) ||
877 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
878 printf ("\n** Device %d not available\n", dev);
879 SHOW_BOOT_PROGRESS (-1);
880 return 1;
881 }
882
7a8e9bed 883 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
dc7c9a1a
WD
884 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
885 offset);
886
addb2e16
BS
887 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
888 SECTORSIZE, NULL, (u_char *)addr)) {
dc7c9a1a
WD
889 printf ("** Read error on %d\n", dev);
890 SHOW_BOOT_PROGRESS (-1);
891 return 1;
892 }
893
894 hdr = (image_header_t *)addr;
895
896 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
897
898 print_image_hdr (hdr);
899
900 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
901 cnt -= SECTORSIZE;
902 } else {
dc013d46 903 printf ("\n** Bad Magic Number 0x%x **\n", ntohl(hdr->ih_magic));
dc7c9a1a
WD
904 SHOW_BOOT_PROGRESS (-1);
905 return 1;
906 }
907
addb2e16
BS
908 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
909 offset + SECTORSIZE, cnt, NULL,
910 (u_char *)(addr+SECTORSIZE))) {
dc7c9a1a
WD
911 printf ("** Read error on %d\n", dev);
912 SHOW_BOOT_PROGRESS (-1);
913 return 1;
914 }
915
916 /* Loading ok, update default load address */
917
918 load_addr = addr;
919
920 /* Check if we should attempt an auto-start */
921 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
922 char *local_args[2];
923 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
924
925 local_args[0] = argv[0];
926 local_args[1] = NULL;
927
7a8e9bed 928 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
dc7c9a1a
WD
929
930 do_bootm (cmdtp, 0, 1, local_args);
931 rcode = 1;
932 }
933 return rcode;
934}
935
0d498393
WD
936U_BOOT_CMD(
937 nboot, 4, 1, do_nandboot,
b0fce99b
WD
938 "nboot - boot from NAND device\n",
939 "loadAddr dev\n"
940);
941
addb2e16 942#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
dc7c9a1a 943
addb2e16 944#endif /* CFG_NAND_LEGACY */