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