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