Merge branch 'master' of git://git.denx.de/u-boot-nand-flash
[karo-tx-uboot.git] / common / cmd_nand.c
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>
6  *
7  * Ported 'dynenv' to 'nand env.oob' command
8  * (C) 2010 Nanometrics, Inc.
9  * 'dynenv' -- Dynamic environment offset in NAND OOB
10  * (C) Copyright 2006-2007 OpenMoko, Inc.
11  * Added 16-bit nand support
12  * (C) 2004 Texas Instruments
13  *
14  * Copyright 2010, 2012 Freescale Semiconductor
15  * The portions of this file whose copyright is held by Freescale and which
16  * are not considered a derived work of GPL v2-only code may be distributed
17  * and/or modified under the terms of the GNU General Public License as
18  * published by the Free Software Foundation; either version 2 of the
19  * License, or (at your option) any later version.
20  */
21
22 #include <common.h>
23 #include <linux/mtd/mtd.h>
24 #include <command.h>
25 #include <watchdog.h>
26 #include <malloc.h>
27 #include <asm/byteorder.h>
28 #include <jffs2/jffs2.h>
29 #include <nand.h>
30
31 #if defined(CONFIG_CMD_MTDPARTS)
32
33 /* partition handling routines */
34 int mtdparts_init(void);
35 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36 int find_dev_and_part(const char *id, struct mtd_device **dev,
37                       u8 *part_num, struct part_info **part);
38 #endif
39
40 static int nand_dump(nand_info_t *nand, ulong off, int only_oob, int repeat)
41 {
42         int i;
43         u_char *datbuf, *oobbuf, *p;
44         static loff_t last;
45
46         if (repeat)
47                 off = last + nand->writesize;
48
49         last = off;
50
51         datbuf = memalign(ARCH_DMA_MINALIGN, nand->writesize);
52         oobbuf = memalign(ARCH_DMA_MINALIGN, nand->oobsize);
53         if (!datbuf || !oobbuf) {
54                 puts("No memory for page buffer\n");
55                 return 1;
56         }
57         off &= ~(nand->writesize - 1);
58         loff_t addr = (loff_t) off;
59         struct mtd_oob_ops ops;
60         memset(&ops, 0, sizeof(ops));
61         ops.datbuf = datbuf;
62         ops.oobbuf = oobbuf;
63         ops.len = nand->writesize;
64         ops.ooblen = nand->oobsize;
65         ops.mode = MTD_OPS_RAW;
66         i = mtd_read_oob(nand, addr, &ops);
67         if (i < 0) {
68                 printf("Error (%d) reading page %08lx\n", i, off);
69                 free(datbuf);
70                 free(oobbuf);
71                 return 1;
72         }
73         printf("Page %08lx dump:\n", off);
74         i = nand->writesize >> 4;
75         p = datbuf;
76
77         while (i--) {
78                 if (!only_oob)
79                         printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
80                                "  %02x %02x %02x %02x %02x %02x %02x %02x\n",
81                                p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
82                                p[8], p[9], p[10], p[11], p[12], p[13], p[14],
83                                p[15]);
84                 p += 16;
85         }
86         puts("OOB:\n");
87         i = nand->oobsize >> 3;
88         p = oobbuf;
89         while (i--) {
90                 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
91                        p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
92                 p += 8;
93         }
94         free(datbuf);
95         free(oobbuf);
96
97         return 0;
98 }
99
100 /* ------------------------------------------------------------------------- */
101
102 static int set_dev(int dev)
103 {
104         if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
105             !nand_info[dev].name) {
106                 puts("No such device\n");
107                 return -1;
108         }
109
110         if (nand_curr_device == dev)
111                 return 0;
112
113         printf("Device %d: %s", dev, nand_info[dev].name);
114         puts("... is now current device\n");
115         nand_curr_device = dev;
116
117 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
118         board_nand_select_device(nand_info[dev].priv, dev);
119 #endif
120
121         return 0;
122 }
123
124 static inline int str2off(const char *p, loff_t *num)
125 {
126         char *endptr;
127
128         *num = simple_strtoull(p, &endptr, 16);
129         return *p != '\0' && *endptr == '\0';
130 }
131
132 static inline int str2long(const char *p, ulong *num)
133 {
134         char *endptr;
135
136         *num = simple_strtoul(p, &endptr, 16);
137         return *p != '\0' && *endptr == '\0';
138 }
139
140 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
141                 loff_t *maxsize)
142 {
143 #ifdef CONFIG_CMD_MTDPARTS
144         struct mtd_device *dev;
145         struct part_info *part;
146         u8 pnum;
147         int ret;
148
149         ret = mtdparts_init();
150         if (ret)
151                 return ret;
152
153         ret = find_dev_and_part(partname, &dev, &pnum, &part);
154         if (ret)
155                 return ret;
156
157         if (dev->id->type != MTD_DEV_TYPE_NAND) {
158                 puts("not a NAND device\n");
159                 return -1;
160         }
161
162         *off = part->offset;
163         *size = part->size;
164         *maxsize = part->size;
165         *idx = dev->id->num;
166
167         ret = set_dev(*idx);
168         if (ret)
169                 return ret;
170
171         return 0;
172 #else
173         puts("offset is not a number\n");
174         return -1;
175 #endif
176 }
177
178 static int arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
179                 loff_t *maxsize)
180 {
181         if (!str2off(arg, off))
182                 return get_part(arg, idx, off, size, maxsize);
183
184         if (*off >= nand_info[*idx].size) {
185                 puts("Offset exceeds device limit\n");
186                 return -1;
187         }
188
189         *maxsize = nand_info[*idx].size - *off;
190         *size = *maxsize;
191         return 0;
192 }
193
194 static int arg_off_size(int argc, char *const argv[], int *idx,
195                         loff_t *off, loff_t *size, loff_t *maxsize)
196 {
197         int ret;
198
199         if (argc == 0) {
200                 *off = 0;
201                 *size = nand_info[*idx].size;
202                 *maxsize = *size;
203                 goto print;
204         }
205
206         ret = arg_off(argv[0], idx, off, size, maxsize);
207         if (ret)
208                 return ret;
209
210         if (argc == 1)
211                 goto print;
212
213         if (!str2off(argv[1], size)) {
214                 printf("'%s' is not a number\n", argv[1]);
215                 return -1;
216         }
217
218         if (*size > *maxsize) {
219                 puts("Size exceeds partition or device limit\n");
220                 return -1;
221         }
222
223 print:
224         printf("device %d ", *idx);
225         if (*size == nand_info[*idx].size)
226                 puts("whole chip\n");
227         else
228                 printf("offset 0x%llx, size 0x%llx\n",
229                        (unsigned long long)*off, (unsigned long long)*size);
230         return 0;
231 }
232
233 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
234 static void print_status(ulong start, ulong end, ulong erasesize, int status)
235 {
236         /*
237          * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
238          * not the same as others.  Instead of bit 1 being lock, it is
239          * #lock_tight. To make the driver support either format, ignore bit 1
240          * and use only bit 0 and bit 2.
241          */
242         printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
243                 start,
244                 end - 1,
245                 (end - start) / erasesize,
246                 ((status & NAND_LOCK_STATUS_TIGHT) ?  "TIGHT " : ""),
247                 (!(status & NAND_LOCK_STATUS_UNLOCK) ?  "LOCK " : ""),
248                 ((status & NAND_LOCK_STATUS_UNLOCK) ?  "UNLOCK " : ""));
249 }
250
251 static void do_nand_status(nand_info_t *nand)
252 {
253         ulong block_start = 0;
254         ulong off;
255         int last_status = -1;
256
257         struct nand_chip *nand_chip = nand->priv;
258         /* check the WP bit */
259         nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
260         printf("device is %swrite protected\n",
261                 (nand_chip->read_byte(nand) & 0x80 ?
262                 "NOT " : ""));
263
264         for (off = 0; off < nand->size; off += nand->erasesize) {
265                 int s = nand_get_lock_status(nand, off);
266
267                 /* print message only if status has changed */
268                 if (s != last_status && off != 0) {
269                         print_status(block_start, off, nand->erasesize,
270                                         last_status);
271                         block_start = off;
272                 }
273                 last_status = s;
274         }
275         /* Print the last block info */
276         print_status(block_start, off, nand->erasesize, last_status);
277 }
278 #endif
279
280 #ifdef CONFIG_ENV_OFFSET_OOB
281 unsigned long nand_env_oob_offset;
282
283 int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
284 {
285         int ret;
286         uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
287         nand_info_t *nand = &nand_info[0];
288         char *cmd = argv[1];
289
290         if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !nand->name) {
291                 puts("no devices available\n");
292                 return 1;
293         }
294
295         set_dev(0);
296
297         if (!strcmp(cmd, "get")) {
298                 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
299                 if (ret)
300                         return 1;
301
302                 printf("0x%08lx\n", nand_env_oob_offset);
303         } else if (!strcmp(cmd, "set")) {
304                 loff_t addr;
305                 loff_t maxsize;
306                 struct mtd_oob_ops ops;
307                 int idx = 0;
308
309                 if (argc < 3)
310                         goto usage;
311
312                 /* We don't care about size, or maxsize. */
313                 if (arg_off(argv[2], &idx, &addr, &maxsize, &maxsize)) {
314                         puts("Offset or partition name expected\n");
315                         return 1;
316                 }
317
318                 if (idx != 0) {
319                         puts("Partition not on first NAND device\n");
320                         return 1;
321                 }
322
323                 if (nand->oobavail < ENV_OFFSET_SIZE) {
324                         printf("Insufficient available OOB bytes:\n"
325                                "%d OOB bytes available but %d required for "
326                                "env.oob support\n",
327                                nand->oobavail, ENV_OFFSET_SIZE);
328                         return 1;
329                 }
330
331                 if ((addr & (nand->erasesize - 1)) != 0) {
332                         printf("Environment offset must be block-aligned\n");
333                         return 1;
334                 }
335
336                 ops.datbuf = NULL;
337                 ops.mode = MTD_OOB_AUTO;
338                 ops.ooboffs = 0;
339                 ops.ooblen = ENV_OFFSET_SIZE;
340                 ops.oobbuf = (void *) oob_buf;
341
342                 oob_buf[0] = ENV_OOB_MARKER;
343                 oob_buf[1] = addr / nand->erasesize;
344
345                 ret = nand->write_oob(nand, ENV_OFFSET_SIZE, &ops);
346                 if (ret) {
347                         printf("Error writing OOB block 0\n");
348                         return ret;
349                 }
350
351                 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
352                 if (ret) {
353                         printf("Error reading env offset in OOB\n");
354                         return ret;
355                 }
356
357                 if (addr != nand_env_oob_offset) {
358                         printf("Verification of env offset in OOB failed: "
359                                "0x%08llx expected but got 0x%08lx\n",
360                                (unsigned long long)addr, nand_env_oob_offset);
361                         return 1;
362                 }
363         } else {
364                 goto usage;
365         }
366
367         return ret;
368
369 usage:
370         return CMD_RET_USAGE;
371 }
372
373 #endif
374
375 static void nand_print_and_set_info(int idx)
376 {
377         nand_info_t *nand = &nand_info[idx];
378         struct nand_chip *chip = nand->priv;
379
380         printf("Device %d: ", idx);
381         if (chip->numchips > 1)
382                 printf("%dx ", chip->numchips);
383         printf("%s, sector size %u KiB\n",
384                nand->name, nand->erasesize >> 10);
385         printf("  Page size  %8d b\n", nand->writesize);
386         printf("  OOB size   %8d b\n", nand->oobsize);
387         printf("  Erase size %8d b\n", nand->erasesize);
388
389         /* Set geometry info */
390         setenv_hex("nand_writesize", nand->writesize);
391         setenv_hex("nand_oobsize", nand->oobsize);
392         setenv_hex("nand_erasesize", nand->erasesize);
393 }
394
395 static int raw_access(nand_info_t *nand, ulong addr, loff_t off, ulong count,
396                         int read)
397 {
398         int ret = 0;
399
400         while (count--) {
401                 /* Raw access */
402                 mtd_oob_ops_t ops = {
403                         .datbuf = (u8 *)addr,
404                         .oobbuf = ((u8 *)addr) + nand->writesize,
405                         .len = nand->writesize,
406                         .ooblen = nand->oobsize,
407                         .mode = MTD_OPS_RAW
408                 };
409
410                 if (read)
411                         ret = mtd_read_oob(nand, off, &ops);
412                 else
413                         ret = mtd_write_oob(nand, off, &ops);
414
415                 if (ret) {
416                         printf("%s: error at offset %llx, ret %d\n",
417                                 __func__, (long long)off, ret);
418                         break;
419                 }
420
421                 addr += nand->writesize + nand->oobsize;
422                 off += nand->writesize;
423         }
424
425         return ret;
426 }
427
428 /* Adjust a chip/partition size down for bad blocks so we don't
429  * read/write past the end of a chip/partition by accident.
430  */
431 static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
432 {
433         /* We grab the nand info object here fresh because this is usually
434          * called after arg_off_size() which can change the value of dev.
435          */
436         nand_info_t *nand = &nand_info[dev];
437         loff_t maxoffset = offset + *size;
438         int badblocks = 0;
439
440         /* count badblocks in NAND from offset to offset + size */
441         for (; offset < maxoffset; offset += nand->erasesize) {
442                 if (nand_block_isbad(nand, offset))
443                         badblocks++;
444         }
445         /* adjust size if any bad blocks found */
446         if (badblocks) {
447                 *size -= badblocks * nand->erasesize;
448                 printf("size adjusted to 0x%llx (%d bad blocks)\n",
449                        (unsigned long long)*size, badblocks);
450         }
451 }
452
453 static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
454 {
455         int i, ret = 0;
456         ulong addr;
457         loff_t off, size, maxsize;
458         char *cmd, *s;
459         nand_info_t *nand;
460 #ifdef CONFIG_SYS_NAND_QUIET
461         int quiet = CONFIG_SYS_NAND_QUIET;
462 #else
463         int quiet = 0;
464 #endif
465         const char *quiet_str = getenv("quiet");
466         int dev = nand_curr_device;
467         int repeat = flag & CMD_FLAG_REPEAT;
468
469         /* at least two arguments please */
470         if (argc < 2)
471                 goto usage;
472
473         if (quiet_str)
474                 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
475
476         cmd = argv[1];
477
478         /* Only "dump" is repeatable. */
479         if (repeat && strcmp(cmd, "dump"))
480                 return 0;
481
482         if (strcmp(cmd, "info") == 0) {
483
484                 putc('\n');
485                 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
486                         if (nand_info[i].name)
487                                 nand_print_and_set_info(i);
488                 }
489                 return 0;
490         }
491
492         if (strcmp(cmd, "device") == 0) {
493                 if (argc < 3) {
494                         putc('\n');
495                         if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
496                                 puts("no devices available\n");
497                         else
498                                 nand_print_and_set_info(dev);
499                         return 0;
500                 }
501
502                 dev = (int)simple_strtoul(argv[2], NULL, 10);
503                 set_dev(dev);
504
505                 return 0;
506         }
507
508 #ifdef CONFIG_ENV_OFFSET_OOB
509         /* this command operates only on the first nand device */
510         if (strcmp(cmd, "env.oob") == 0)
511                 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
512 #endif
513
514         /* The following commands operate on the current device, unless
515          * overridden by a partition specifier.  Note that if somehow the
516          * current device is invalid, it will have to be changed to a valid
517          * one before these commands can run, even if a partition specifier
518          * for another device is to be used.
519          */
520         if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
521             !nand_info[dev].name) {
522                 puts("\nno devices available\n");
523                 return 1;
524         }
525         nand = &nand_info[dev];
526
527         if (strcmp(cmd, "bad") == 0) {
528                 printf("\nDevice %d bad blocks:\n", dev);
529                 for (off = 0; off < nand->size; off += nand->erasesize)
530                         if (nand_block_isbad(nand, off))
531                                 printf("  %08llx\n", (unsigned long long)off);
532                 return 0;
533         }
534
535         /*
536          * Syntax is:
537          *   0    1     2       3    4
538          *   nand erase [clean] [off size]
539          */
540         if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
541                 nand_erase_options_t opts;
542                 /* "clean" at index 2 means request to write cleanmarker */
543                 int clean = argc > 2 && !strcmp("clean", argv[2]);
544                 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
545                 int o = (clean || scrub_yes) ? 3 : 2;
546                 int scrub = !strncmp(cmd, "scrub", 5);
547                 int spread = 0;
548                 int args = 2;
549                 const char *scrub_warn =
550                         "Warning: "
551                         "scrub option will erase all factory set bad blocks!\n"
552                         "         "
553                         "There is no reliable way to recover them.\n"
554                         "         "
555                         "Use this command only for testing purposes if you\n"
556                         "         "
557                         "are sure of what you are doing!\n"
558                         "\nReally scrub this NAND flash? <y/N>\n";
559
560                 if (cmd[5] != 0) {
561                         if (!strcmp(&cmd[5], ".spread")) {
562                                 spread = 1;
563                         } else if (!strcmp(&cmd[5], ".part")) {
564                                 args = 1;
565                         } else if (!strcmp(&cmd[5], ".chip")) {
566                                 args = 0;
567                         } else {
568                                 goto usage;
569                         }
570                 }
571
572                 /*
573                  * Don't allow missing arguments to cause full chip/partition
574                  * erases -- easy to do accidentally, e.g. with a misspelled
575                  * variable name.
576                  */
577                 if (argc != o + args)
578                         goto usage;
579
580                 printf("\nNAND %s: ", cmd);
581                 /* skip first two or three arguments, look for offset and size */
582                 if (arg_off_size(argc - o, argv + o, &dev, &off, &size,
583                                  &maxsize) != 0)
584                         return 1;
585
586                 nand = &nand_info[dev];
587
588                 memset(&opts, 0, sizeof(opts));
589                 opts.offset = off;
590                 opts.length = size;
591                 opts.jffs2  = clean;
592                 opts.quiet  = quiet;
593                 opts.spread = spread;
594
595                 if (scrub) {
596                         if (!scrub_yes)
597                                 puts(scrub_warn);
598
599                         if (scrub_yes)
600                                 opts.scrub = 1;
601                         else if (getc() == 'y') {
602                                 puts("y");
603                                 if (getc() == '\r')
604                                         opts.scrub = 1;
605                                 else {
606                                         puts("scrub aborted\n");
607                                         return -1;
608                                 }
609                         } else {
610                                 puts("scrub aborted\n");
611                                 return -1;
612                         }
613                 }
614                 ret = nand_erase_opts(nand, &opts);
615                 printf("%s\n", ret ? "ERROR" : "OK");
616
617                 return ret == 0 ? 0 : 1;
618         }
619
620         if (strncmp(cmd, "dump", 4) == 0) {
621                 if (argc < 3)
622                         goto usage;
623
624                 off = (int)simple_strtoul(argv[2], NULL, 16);
625                 ret = nand_dump(nand, off, !strcmp(&cmd[4], ".oob"), repeat);
626
627                 return ret == 0 ? 1 : 0;
628         }
629
630         if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
631                 size_t rwsize;
632                 ulong pagecount = 1;
633                 int read;
634                 int raw = 0;
635
636                 if (argc < 4)
637                         goto usage;
638
639                 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
640
641                 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
642                 printf("\nNAND %s: ", read ? "read" : "write");
643
644                 nand = &nand_info[dev];
645
646                 s = strchr(cmd, '.');
647
648                 if (s && !strcmp(s, ".raw")) {
649                         raw = 1;
650
651                         if (arg_off(argv[3], &dev, &off, &size, &maxsize))
652                                 return 1;
653
654                         if (argc > 4 && !str2long(argv[4], &pagecount)) {
655                                 printf("'%s' is not a number\n", argv[4]);
656                                 return 1;
657                         }
658
659                         if (pagecount * nand->writesize > size) {
660                                 puts("Size exceeds partition or device limit\n");
661                                 return -1;
662                         }
663
664                         rwsize = pagecount * (nand->writesize + nand->oobsize);
665                 } else {
666                         if (arg_off_size(argc - 3, argv + 3, &dev,
667                                                 &off, &size, &maxsize) != 0)
668                                 return 1;
669
670                         /* size is unspecified */
671                         if (argc < 5)
672                                 adjust_size_for_badblocks(&size, off, dev);
673                         rwsize = size;
674                 }
675
676                 if (!s || !strcmp(s, ".jffs2") ||
677                     !strcmp(s, ".e") || !strcmp(s, ".i")) {
678                         if (read)
679                                 ret = nand_read_skip_bad(nand, off, &rwsize,
680                                                          NULL, maxsize,
681                                                          (u_char *)addr);
682                         else
683                                 ret = nand_write_skip_bad(nand, off, &rwsize,
684                                                           NULL, maxsize,
685                                                           (u_char *)addr, 0);
686 #ifdef CONFIG_CMD_NAND_TRIMFFS
687                 } else if (!strcmp(s, ".trimffs")) {
688                         if (read) {
689                                 printf("Unknown nand command suffix '%s'\n", s);
690                                 return 1;
691                         }
692                         ret = nand_write_skip_bad(nand, off, &rwsize, NULL,
693                                                 maxsize, (u_char *)addr,
694                                                 WITH_DROP_FFS);
695 #endif
696 #ifdef CONFIG_CMD_NAND_YAFFS
697                 } else if (!strcmp(s, ".yaffs")) {
698                         if (read) {
699                                 printf("Unknown nand command suffix '%s'.\n", s);
700                                 return 1;
701                         }
702                         ret = nand_write_skip_bad(nand, off, &rwsize, NULL,
703                                                 maxsize, (u_char *)addr,
704                                                 WITH_YAFFS_OOB);
705 #endif
706                 } else if (!strcmp(s, ".oob")) {
707                         /* out-of-band data */
708                         mtd_oob_ops_t ops = {
709                                 .oobbuf = (u8 *)addr,
710                                 .ooblen = rwsize,
711                                 .mode = MTD_OPS_RAW
712                         };
713
714                         if (read)
715                                 ret = mtd_read_oob(nand, off, &ops);
716                         else
717                                 ret = mtd_write_oob(nand, off, &ops);
718                 } else if (raw) {
719                         ret = raw_access(nand, addr, off, pagecount, read);
720                 } else {
721                         printf("Unknown nand command suffix '%s'.\n", s);
722                         return 1;
723                 }
724
725                 printf(" %zu bytes %s: %s\n", rwsize,
726                        read ? "read" : "written", ret ? "ERROR" : "OK");
727
728                 return ret == 0 ? 0 : 1;
729         }
730
731 #ifdef CONFIG_CMD_NAND_TORTURE
732         if (strcmp(cmd, "torture") == 0) {
733                 if (argc < 3)
734                         goto usage;
735
736                 if (!str2off(argv[2], &off)) {
737                         puts("Offset is not a valid number\n");
738                         return 1;
739                 }
740
741                 printf("\nNAND torture: device %d offset 0x%llx size 0x%x\n",
742                         dev, off, nand->erasesize);
743                 ret = nand_torture(nand, off);
744                 printf(" %s\n", ret ? "Failed" : "Passed");
745
746                 return ret == 0 ? 0 : 1;
747         }
748 #endif
749
750         if (strcmp(cmd, "markbad") == 0) {
751                 argc -= 2;
752                 argv += 2;
753
754                 if (argc <= 0)
755                         goto usage;
756
757                 while (argc > 0) {
758                         addr = simple_strtoul(*argv, NULL, 16);
759
760                         if (mtd_block_markbad(nand, addr)) {
761                                 printf("block 0x%08lx NOT marked "
762                                         "as bad! ERROR %d\n",
763                                         addr, ret);
764                                 ret = 1;
765                         } else {
766                                 printf("block 0x%08lx successfully "
767                                         "marked as bad\n",
768                                         addr);
769                         }
770                         --argc;
771                         ++argv;
772                 }
773                 return ret;
774         }
775
776         if (strcmp(cmd, "biterr") == 0) {
777                 /* todo */
778                 return 1;
779         }
780
781 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
782         if (strcmp(cmd, "lock") == 0) {
783                 int tight = 0;
784                 int status = 0;
785                 if (argc == 3) {
786                         if (!strcmp("tight", argv[2]))
787                                 tight = 1;
788                         if (!strcmp("status", argv[2]))
789                                 status = 1;
790                 }
791                 if (status) {
792                         do_nand_status(nand);
793                 } else {
794                         if (!nand_lock(nand, tight)) {
795                                 puts("NAND flash successfully locked\n");
796                         } else {
797                                 puts("Error locking NAND flash\n");
798                                 return 1;
799                         }
800                 }
801                 return 0;
802         }
803
804         if (strncmp(cmd, "unlock", 5) == 0) {
805                 int allexcept = 0;
806
807                 s = strchr(cmd, '.');
808
809                 if (s && !strcmp(s, ".allexcept"))
810                         allexcept = 1;
811
812                 if (arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
813                                  &maxsize) < 0)
814                         return 1;
815
816                 if (!nand_unlock(&nand_info[dev], off, size, allexcept)) {
817                         puts("NAND flash successfully unlocked\n");
818                 } else {
819                         puts("Error unlocking NAND flash, "
820                              "write and erase will probably fail\n");
821                         return 1;
822                 }
823                 return 0;
824         }
825 #endif
826
827 usage:
828         return CMD_RET_USAGE;
829 }
830
831 #ifdef CONFIG_SYS_LONGHELP
832 static char nand_help_text[] =
833         "info - show available NAND devices\n"
834         "nand device [dev] - show or set current device\n"
835         "nand read - addr off|partition size\n"
836         "nand write - addr off|partition size\n"
837         "    read/write 'size' bytes starting at offset 'off'\n"
838         "    to/from memory address 'addr', skipping bad blocks.\n"
839         "nand read.raw - addr off|partition [count]\n"
840         "nand write.raw - addr off|partition [count]\n"
841         "    Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
842 #ifdef CONFIG_CMD_NAND_TRIMFFS
843         "nand write.trimffs - addr off|partition size\n"
844         "    write 'size' bytes starting at offset 'off' from memory address\n"
845         "    'addr', skipping bad blocks and dropping any pages at the end\n"
846         "    of eraseblocks that contain only 0xFF\n"
847 #endif
848 #ifdef CONFIG_CMD_NAND_YAFFS
849         "nand write.yaffs - addr off|partition size\n"
850         "    write 'size' bytes starting at offset 'off' with yaffs format\n"
851         "    from memory address 'addr', skipping bad blocks.\n"
852 #endif
853         "nand erase[.spread] [clean] off size - erase 'size' bytes "
854         "from offset 'off'\n"
855         "    With '.spread', erase enough for given file size, otherwise,\n"
856         "    'size' includes skipped bad blocks.\n"
857         "nand erase.part [clean] partition - erase entire mtd partition'\n"
858         "nand erase.chip [clean] - erase entire chip'\n"
859         "nand bad - show bad blocks\n"
860         "nand dump[.oob] off - dump page\n"
861 #ifdef CONFIG_CMD_NAND_TORTURE
862         "nand torture off - torture block at offset\n"
863 #endif
864         "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
865         "    really clean NAND erasing bad blocks (UNSAFE)\n"
866         "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
867         "nand biterr off - make a bit error at offset (UNSAFE)"
868 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
869         "\n"
870         "nand lock [tight] [status]\n"
871         "    bring nand to lock state or display locked pages\n"
872         "nand unlock[.allexcept] [offset] [size] - unlock section"
873 #endif
874 #ifdef CONFIG_ENV_OFFSET_OOB
875         "\n"
876         "nand env.oob - environment offset in OOB of block 0 of"
877         "    first device.\n"
878         "nand env.oob set off|partition - set enviromnent offset\n"
879         "nand env.oob get - get environment offset"
880 #endif
881         "";
882 #endif
883
884 U_BOOT_CMD(
885         nand, CONFIG_SYS_MAXARGS, 1, do_nand,
886         "NAND sub-system", nand_help_text
887 );
888
889 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
890                            ulong offset, ulong addr, char *cmd)
891 {
892         int r;
893         char *s;
894         size_t cnt;
895         image_header_t *hdr;
896 #if defined(CONFIG_FIT)
897         const void *fit_hdr = NULL;
898 #endif
899
900         s = strchr(cmd, '.');
901         if (s != NULL &&
902             (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
903                 printf("Unknown nand load suffix '%s'\n", s);
904                 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
905                 return 1;
906         }
907
908         printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
909
910         cnt = nand->writesize;
911         r = nand_read_skip_bad(nand, offset, &cnt, NULL, nand->size,
912                         (u_char *)addr);
913         if (r) {
914                 puts("** Read error\n");
915                 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
916                 return 1;
917         }
918         bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
919
920         switch (genimg_get_format ((void *)addr)) {
921         case IMAGE_FORMAT_LEGACY:
922                 hdr = (image_header_t *)addr;
923
924                 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
925                 image_print_contents (hdr);
926
927                 cnt = image_get_image_size (hdr);
928                 break;
929 #if defined(CONFIG_FIT)
930         case IMAGE_FORMAT_FIT:
931                 fit_hdr = (const void *)addr;
932                 puts ("Fit image detected...\n");
933
934                 cnt = fit_get_size (fit_hdr);
935                 break;
936 #endif
937         default:
938                 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
939                 puts ("** Unknown image type\n");
940                 return 1;
941         }
942         bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
943
944         r = nand_read_skip_bad(nand, offset, &cnt, NULL, nand->size,
945                         (u_char *)addr);
946         if (r) {
947                 puts("** Read error\n");
948                 bootstage_error(BOOTSTAGE_ID_NAND_READ);
949                 return 1;
950         }
951         bootstage_mark(BOOTSTAGE_ID_NAND_READ);
952
953 #if defined(CONFIG_FIT)
954         /* This cannot be done earlier, we need complete FIT image in RAM first */
955         if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
956                 if (!fit_check_format (fit_hdr)) {
957                         bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
958                         puts ("** Bad FIT image format\n");
959                         return 1;
960                 }
961                 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
962                 fit_print_contents (fit_hdr);
963         }
964 #endif
965
966         /* Loading ok, update default load address */
967
968         load_addr = addr;
969
970         return bootm_maybe_autostart(cmdtp, cmd);
971 }
972
973 static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
974                        char * const argv[])
975 {
976         char *boot_device = NULL;
977         int idx;
978         ulong addr, offset = 0;
979 #if defined(CONFIG_CMD_MTDPARTS)
980         struct mtd_device *dev;
981         struct part_info *part;
982         u8 pnum;
983
984         if (argc >= 2) {
985                 char *p = (argc == 2) ? argv[1] : argv[2];
986                 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
987                     (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
988                         if (dev->id->type != MTD_DEV_TYPE_NAND) {
989                                 puts("Not a NAND device\n");
990                                 return 1;
991                         }
992                         if (argc > 3)
993                                 goto usage;
994                         if (argc == 3)
995                                 addr = simple_strtoul(argv[1], NULL, 16);
996                         else
997                                 addr = CONFIG_SYS_LOAD_ADDR;
998                         return nand_load_image(cmdtp, &nand_info[dev->id->num],
999                                                part->offset, addr, argv[0]);
1000                 }
1001         }
1002 #endif
1003
1004         bootstage_mark(BOOTSTAGE_ID_NAND_PART);
1005         switch (argc) {
1006         case 1:
1007                 addr = CONFIG_SYS_LOAD_ADDR;
1008                 boot_device = getenv("bootdevice");
1009                 break;
1010         case 2:
1011                 addr = simple_strtoul(argv[1], NULL, 16);
1012                 boot_device = getenv("bootdevice");
1013                 break;
1014         case 3:
1015                 addr = simple_strtoul(argv[1], NULL, 16);
1016                 boot_device = argv[2];
1017                 break;
1018         case 4:
1019                 addr = simple_strtoul(argv[1], NULL, 16);
1020                 boot_device = argv[2];
1021                 offset = simple_strtoul(argv[3], NULL, 16);
1022                 break;
1023         default:
1024 #if defined(CONFIG_CMD_MTDPARTS)
1025 usage:
1026 #endif
1027                 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
1028                 return CMD_RET_USAGE;
1029         }
1030         bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
1031
1032         if (!boot_device) {
1033                 puts("\n** No boot device **\n");
1034                 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1035                 return 1;
1036         }
1037         bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1038
1039         idx = simple_strtoul(boot_device, NULL, 16);
1040
1041         if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
1042                 printf("\n** Device %d not available\n", idx);
1043                 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
1044                 return 1;
1045         }
1046         bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
1047
1048         return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
1049 }
1050
1051 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
1052         "boot from NAND device",
1053         "[partition] | [[[loadAddr] dev] offset]"
1054 );