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