Merge branch 'master' of git://git.denx.de/u-boot-nios
[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 /*
15  *
16  * New NAND support
17  *
18  */
19 #include <common.h>
20 #include <linux/mtd/mtd.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_MTDPARTS)
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 static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
41 {
42         int i;
43         u_char *datbuf, *oobbuf, *p;
44
45         datbuf = malloc(nand->writesize + nand->oobsize);
46         oobbuf = malloc(nand->oobsize);
47         if (!datbuf || !oobbuf) {
48                 puts("No memory for page buffer\n");
49                 return 1;
50         }
51         off &= ~(nand->writesize - 1);
52         loff_t addr = (loff_t) off;
53         struct mtd_oob_ops ops;
54         memset(&ops, 0, sizeof(ops));
55         ops.datbuf = datbuf;
56         ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
57         ops.len = nand->writesize;
58         ops.ooblen = nand->oobsize;
59         ops.mode = MTD_OOB_RAW;
60         i = nand->read_oob(nand, addr, &ops);
61         if (i < 0) {
62                 printf("Error (%d) reading page %08lx\n", i, off);
63                 free(datbuf);
64                 free(oobbuf);
65                 return 1;
66         }
67         printf("Page %08lx dump:\n", off);
68         i = nand->writesize >> 4;
69         p = datbuf;
70
71         while (i--) {
72                 if (!only_oob)
73                         printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
74                                "  %02x %02x %02x %02x %02x %02x %02x %02x\n",
75                                p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
76                                p[8], p[9], p[10], p[11], p[12], p[13], p[14],
77                                p[15]);
78                 p += 16;
79         }
80         puts("OOB:\n");
81         i = nand->oobsize >> 3;
82         while (i--) {
83                 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
84                        p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
85                 p += 8;
86         }
87         free(datbuf);
88         free(oobbuf);
89
90         return 0;
91 }
92
93 /* ------------------------------------------------------------------------- */
94
95 static inline int str2long(char *p, ulong *num)
96 {
97         char *endptr;
98
99         *num = simple_strtoul(p, &endptr, 16);
100         return (*p != '\0' && *endptr == '\0') ? 1 : 0;
101 }
102
103 static int
104 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
105 {
106         int idx = nand_curr_device;
107 #if defined(CONFIG_CMD_MTDPARTS)
108         struct mtd_device *dev;
109         struct part_info *part;
110         u8 pnum;
111
112         if (argc >= 1 && !(str2long(argv[0], off))) {
113                 if ((mtdparts_init() == 0) &&
114                     (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
115                         if (dev->id->type != MTD_DEV_TYPE_NAND) {
116                                 puts("not a NAND device\n");
117                                 return -1;
118                         }
119                         *off = part->offset;
120                         if (argc >= 2) {
121                                 if (!(str2long(argv[1], (ulong *)size))) {
122                                         printf("'%s' is not a number\n", argv[1]);
123                                         return -1;
124                                 }
125                                 if (*size > part->size)
126                                         *size = part->size;
127                         } else {
128                                 *size = part->size;
129                         }
130                         idx = dev->id->num;
131                         *nand = nand_info[idx];
132                         goto out;
133                 }
134         }
135 #endif
136
137         if (argc >= 1) {
138                 if (!(str2long(argv[0], off))) {
139                         printf("'%s' is not a number\n", argv[0]);
140                         return -1;
141                 }
142         } else {
143                 *off = 0;
144         }
145
146         if (argc >= 2) {
147                 if (!(str2long(argv[1], (ulong *)size))) {
148                         printf("'%s' is not a number\n", argv[1]);
149                         return -1;
150                 }
151         } else {
152                 *size = nand->size - *off;
153         }
154
155 #if defined(CONFIG_CMD_MTDPARTS)
156 out:
157 #endif
158         printf("device %d ", idx);
159         if (*size == nand->size)
160                 puts("whole chip\n");
161         else
162                 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
163         return 0;
164 }
165
166 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
167 static void print_status(ulong start, ulong end, ulong erasesize, int status)
168 {
169         printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
170                 start,
171                 end - 1,
172                 (end - start) / erasesize,
173                 ((status & NAND_LOCK_STATUS_TIGHT) ?  "TIGHT " : ""),
174                 ((status & NAND_LOCK_STATUS_LOCK) ?  "LOCK " : ""),
175                 ((status & NAND_LOCK_STATUS_UNLOCK) ?  "UNLOCK " : ""));
176 }
177
178 static void do_nand_status(nand_info_t *nand)
179 {
180         ulong block_start = 0;
181         ulong off;
182         int last_status = -1;
183
184         struct nand_chip *nand_chip = nand->priv;
185         /* check the WP bit */
186         nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
187         printf("device is %swrite protected\n",
188                 (nand_chip->read_byte(nand) & 0x80 ?
189                 "NOT " : ""));
190
191         for (off = 0; off < nand->size; off += nand->erasesize) {
192                 int s = nand_get_lock_status(nand, off);
193
194                 /* print message only if status has changed */
195                 if (s != last_status && off != 0) {
196                         print_status(block_start, off, nand->erasesize,
197                                         last_status);
198                         block_start = off;
199                 }
200                 last_status = s;
201         }
202         /* Print the last block info */
203         print_status(block_start, off, nand->erasesize, last_status);
204 }
205 #endif
206
207 static void nand_print_info(int idx)
208 {
209         nand_info_t *nand = &nand_info[idx];
210         struct nand_chip *chip = nand->priv;
211         printf("Device %d: ", idx);
212         if (chip->numchips > 1)
213                 printf("%dx ", chip->numchips);
214         printf("%s, sector size %u KiB\n",
215                nand->name, nand->erasesize >> 10);
216 }
217
218 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
219 {
220         int i, dev, ret = 0;
221         ulong addr, off;
222         size_t size;
223         char *cmd, *s;
224         nand_info_t *nand;
225 #ifdef CONFIG_SYS_NAND_QUIET
226         int quiet = CONFIG_SYS_NAND_QUIET;
227 #else
228         int quiet = 0;
229 #endif
230         const char *quiet_str = getenv("quiet");
231
232         /* at least two arguments please */
233         if (argc < 2)
234                 goto usage;
235
236         if (quiet_str)
237                 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
238
239         cmd = argv[1];
240
241         if (strcmp(cmd, "info") == 0) {
242
243                 putc('\n');
244                 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
245                         if (nand_info[i].name)
246                                 nand_print_info(i);
247                 }
248                 return 0;
249         }
250
251         if (strcmp(cmd, "device") == 0) {
252
253                 if (argc < 3) {
254                         putc('\n');
255                         if ((nand_curr_device < 0) ||
256                             (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
257                                 puts("no devices available\n");
258                         else
259                                 nand_print_info(nand_curr_device);
260                         return 0;
261                 }
262                 dev = (int)simple_strtoul(argv[2], NULL, 10);
263                 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
264                         puts("No such device\n");
265                         return 1;
266                 }
267                 printf("Device %d: %s", dev, nand_info[dev].name);
268                 puts("... is now current device\n");
269                 nand_curr_device = dev;
270
271 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
272                 /*
273                  * Select the chip in the board/cpu specific driver
274                  */
275                 board_nand_select_device(nand_info[dev].priv, dev);
276 #endif
277
278                 return 0;
279         }
280
281         if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
282             strncmp(cmd, "dump", 4) != 0 &&
283             strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
284             strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
285             strcmp(cmd, "biterr") != 0 &&
286             strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
287                 goto usage;
288
289         /* the following commands operate on the current device */
290         if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
291             !nand_info[nand_curr_device].name) {
292                 puts("\nno devices available\n");
293                 return 1;
294         }
295         nand = &nand_info[nand_curr_device];
296
297         if (strcmp(cmd, "bad") == 0) {
298                 printf("\nDevice %d bad blocks:\n", nand_curr_device);
299                 for (off = 0; off < nand->size; off += nand->erasesize)
300                         if (nand_block_isbad(nand, off))
301                                 printf("  %08lx\n", off);
302                 return 0;
303         }
304
305         /*
306          * Syntax is:
307          *   0    1     2       3    4
308          *   nand erase [clean] [off size]
309          */
310         if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
311                 nand_erase_options_t opts;
312                 /* "clean" at index 2 means request to write cleanmarker */
313                 int clean = argc > 2 && !strcmp("clean", argv[2]);
314                 int o = clean ? 3 : 2;
315                 int scrub = !strcmp(cmd, "scrub");
316
317                 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
318                 /* skip first two or three arguments, look for offset and size */
319                 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
320                         return 1;
321
322                 memset(&opts, 0, sizeof(opts));
323                 opts.offset = off;
324                 opts.length = size;
325                 opts.jffs2  = clean;
326                 opts.quiet  = quiet;
327
328                 if (scrub) {
329                         puts("Warning: "
330                              "scrub option will erase all factory set "
331                              "bad blocks!\n"
332                              "         "
333                              "There is no reliable way to recover them.\n"
334                              "         "
335                              "Use this command only for testing purposes "
336                              "if you\n"
337                              "         "
338                              "are sure of what you are doing!\n"
339                              "\nReally scrub this NAND flash? <y/N>\n");
340
341                         if (getc() == 'y' && getc() == '\r') {
342                                 opts.scrub = 1;
343                         } else {
344                                 puts("scrub aborted\n");
345                                 return -1;
346                         }
347                 }
348                 ret = nand_erase_opts(nand, &opts);
349                 printf("%s\n", ret ? "ERROR" : "OK");
350
351                 return ret == 0 ? 0 : 1;
352         }
353
354         if (strncmp(cmd, "dump", 4) == 0) {
355                 if (argc < 3)
356                         goto usage;
357
358                 s = strchr(cmd, '.');
359                 off = (int)simple_strtoul(argv[2], NULL, 16);
360
361                 if (s != NULL && strcmp(s, ".oob") == 0)
362                         ret = nand_dump(nand, off, 1);
363                 else
364                         ret = nand_dump(nand, off, 0);
365
366                 return ret == 0 ? 1 : 0;
367
368         }
369
370         if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
371                 int read;
372
373                 if (argc < 4)
374                         goto usage;
375
376                 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
377
378                 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
379                 printf("\nNAND %s: ", read ? "read" : "write");
380                 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
381                         return 1;
382
383                 s = strchr(cmd, '.');
384                 if (!s || !strcmp(s, ".jffs2") ||
385                     !strcmp(s, ".e") || !strcmp(s, ".i")) {
386                         if (read)
387                                 ret = nand_read_skip_bad(nand, off, &size,
388                                                          (u_char *)addr);
389                         else
390                                 ret = nand_write_skip_bad(nand, off, &size,
391                                                           (u_char *)addr);
392                 } else if (!strcmp(s, ".oob")) {
393                         /* out-of-band data */
394                         mtd_oob_ops_t ops = {
395                                 .oobbuf = (u8 *)addr,
396                                 .ooblen = size,
397                                 .mode = MTD_OOB_RAW
398                         };
399
400                         if (read)
401                                 ret = nand->read_oob(nand, off, &ops);
402                         else
403                                 ret = nand->write_oob(nand, off, &ops);
404                 } else {
405                         printf("Unknown nand command suffix '%s'.\n", s);
406                         return 1;
407                 }
408
409                 printf(" %zu bytes %s: %s\n", size,
410                        read ? "read" : "written", ret ? "ERROR" : "OK");
411
412                 return ret == 0 ? 0 : 1;
413         }
414
415         if (strcmp(cmd, "markbad") == 0) {
416                 argc -= 2;
417                 argv += 2;
418
419                 if (argc <= 0)
420                         goto usage;
421
422                 while (argc > 0) {
423                         addr = simple_strtoul(*argv, NULL, 16);
424
425                         if (nand->block_markbad(nand, addr)) {
426                                 printf("block 0x%08lx NOT marked "
427                                         "as bad! ERROR %d\n",
428                                         addr, ret);
429                                 ret = 1;
430                         } else {
431                                 printf("block 0x%08lx successfully "
432                                         "marked as bad\n",
433                                         addr);
434                         }
435                         --argc;
436                         ++argv;
437                 }
438                 return ret;
439         }
440
441         if (strcmp(cmd, "biterr") == 0) {
442                 /* todo */
443                 return 1;
444         }
445
446 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
447         if (strcmp(cmd, "lock") == 0) {
448                 int tight = 0;
449                 int status = 0;
450                 if (argc == 3) {
451                         if (!strcmp("tight", argv[2]))
452                                 tight = 1;
453                         if (!strcmp("status", argv[2]))
454                                 status = 1;
455                 }
456                 if (status) {
457                         do_nand_status(nand);
458                 } else {
459                         if (!nand_lock(nand, tight)) {
460                                 puts("NAND flash successfully locked\n");
461                         } else {
462                                 puts("Error locking NAND flash\n");
463                                 return 1;
464                         }
465                 }
466                 return 0;
467         }
468
469         if (strcmp(cmd, "unlock") == 0) {
470                 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
471                         return 1;
472
473                 if (!nand_unlock(nand, off, size)) {
474                         puts("NAND flash successfully unlocked\n");
475                 } else {
476                         puts("Error unlocking NAND flash, "
477                              "write and erase will probably fail\n");
478                         return 1;
479                 }
480                 return 0;
481         }
482 #endif
483
484 usage:
485         cmd_usage(cmdtp);
486         return 1;
487 }
488
489 U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
490         "NAND sub-system",
491         "info - show available NAND devices\n"
492         "nand device [dev] - show or set current device\n"
493         "nand read - addr off|partition size\n"
494         "nand write - addr off|partition size\n"
495         "    read/write 'size' bytes starting at offset 'off'\n"
496         "    to/from memory address 'addr', skipping bad blocks.\n"
497         "nand erase [clean] [off size] - erase 'size' bytes from\n"
498         "    offset 'off' (entire device if not specified)\n"
499         "nand bad - show bad blocks\n"
500         "nand dump[.oob] off - dump page\n"
501         "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
502         "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
503         "nand biterr off - make a bit error at offset (UNSAFE)"
504 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
505         "\n"
506         "nand lock [tight] [status]\n"
507         "    bring nand to lock state or display locked pages\n"
508         "nand unlock [offset] [size] - unlock section"
509 #endif
510 );
511
512 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
513                            ulong offset, ulong addr, char *cmd)
514 {
515         int r;
516         char *ep, *s;
517         size_t cnt;
518         image_header_t *hdr;
519 #if defined(CONFIG_FIT)
520         const void *fit_hdr = NULL;
521 #endif
522
523         s = strchr(cmd, '.');
524         if (s != NULL &&
525             (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
526                 printf("Unknown nand load suffix '%s'\n", s);
527                 show_boot_progress(-53);
528                 return 1;
529         }
530
531         printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
532
533         cnt = nand->writesize;
534         r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
535         if (r) {
536                 puts("** Read error\n");
537                 show_boot_progress (-56);
538                 return 1;
539         }
540         show_boot_progress (56);
541
542         switch (genimg_get_format ((void *)addr)) {
543         case IMAGE_FORMAT_LEGACY:
544                 hdr = (image_header_t *)addr;
545
546                 show_boot_progress (57);
547                 image_print_contents (hdr);
548
549                 cnt = image_get_image_size (hdr);
550                 break;
551 #if defined(CONFIG_FIT)
552         case IMAGE_FORMAT_FIT:
553                 fit_hdr = (const void *)addr;
554                 puts ("Fit image detected...\n");
555
556                 cnt = fit_get_size (fit_hdr);
557                 break;
558 #endif
559         default:
560                 show_boot_progress (-57);
561                 puts ("** Unknown image type\n");
562                 return 1;
563         }
564         show_boot_progress (57);
565
566         r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
567         if (r) {
568                 puts("** Read error\n");
569                 show_boot_progress (-58);
570                 return 1;
571         }
572         show_boot_progress (58);
573
574 #if defined(CONFIG_FIT)
575         /* This cannot be done earlier, we need complete FIT image in RAM first */
576         if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
577                 if (!fit_check_format (fit_hdr)) {
578                         show_boot_progress (-150);
579                         puts ("** Bad FIT image format\n");
580                         return 1;
581                 }
582                 show_boot_progress (151);
583                 fit_print_contents (fit_hdr);
584         }
585 #endif
586
587         /* Loading ok, update default load address */
588
589         load_addr = addr;
590
591         /* Check if we should attempt an auto-start */
592         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
593                 char *local_args[2];
594                 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
595
596                 local_args[0] = cmd;
597                 local_args[1] = NULL;
598
599                 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
600
601                 do_bootm(cmdtp, 0, 1, local_args);
602                 return 1;
603         }
604         return 0;
605 }
606
607 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
608 {
609         char *boot_device = NULL;
610         int idx;
611         ulong addr, offset = 0;
612 #if defined(CONFIG_CMD_MTDPARTS)
613         struct mtd_device *dev;
614         struct part_info *part;
615         u8 pnum;
616
617         if (argc >= 2) {
618                 char *p = (argc == 2) ? argv[1] : argv[2];
619                 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
620                     (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
621                         if (dev->id->type != MTD_DEV_TYPE_NAND) {
622                                 puts("Not a NAND device\n");
623                                 return 1;
624                         }
625                         if (argc > 3)
626                                 goto usage;
627                         if (argc == 3)
628                                 addr = simple_strtoul(argv[1], NULL, 16);
629                         else
630                                 addr = CONFIG_SYS_LOAD_ADDR;
631                         return nand_load_image(cmdtp, &nand_info[dev->id->num],
632                                                part->offset, addr, argv[0]);
633                 }
634         }
635 #endif
636
637         show_boot_progress(52);
638         switch (argc) {
639         case 1:
640                 addr = CONFIG_SYS_LOAD_ADDR;
641                 boot_device = getenv("bootdevice");
642                 break;
643         case 2:
644                 addr = simple_strtoul(argv[1], NULL, 16);
645                 boot_device = getenv("bootdevice");
646                 break;
647         case 3:
648                 addr = simple_strtoul(argv[1], NULL, 16);
649                 boot_device = argv[2];
650                 break;
651         case 4:
652                 addr = simple_strtoul(argv[1], NULL, 16);
653                 boot_device = argv[2];
654                 offset = simple_strtoul(argv[3], NULL, 16);
655                 break;
656         default:
657 #if defined(CONFIG_CMD_MTDPARTS)
658 usage:
659 #endif
660                 cmd_usage(cmdtp);
661                 show_boot_progress(-53);
662                 return 1;
663         }
664
665         show_boot_progress(53);
666         if (!boot_device) {
667                 puts("\n** No boot device **\n");
668                 show_boot_progress(-54);
669                 return 1;
670         }
671         show_boot_progress(54);
672
673         idx = simple_strtoul(boot_device, NULL, 16);
674
675         if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
676                 printf("\n** Device %d not available\n", idx);
677                 show_boot_progress(-55);
678                 return 1;
679         }
680         show_boot_progress(55);
681
682         return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
683 }
684
685 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
686         "boot from NAND device",
687         "[partition] | [[[loadAddr] dev] offset]"
688 );
689 #endif