[new uImage] Add new uImage format support to imgextract command
[people/ms/u-boot.git] / common / cmd_nand.c
CommitLineData
dc7c9a1a
WD
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>
384cc687
WD
6 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
dc7c9a1a
WD
9 */
10
11#include <common.h>
addb2e16
BS
12
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
21
c76fe474 22#if defined(CONFIG_CMD_NAND)
addb2e16
BS
23
24#include <command.h>
25#include <watchdog.h>
26#include <malloc.h>
27#include <asm/byteorder.h>
addb2e16
BS
28#include <jffs2/jffs2.h>
29#include <nand.h>
30
c76fe474 31#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
32
33/* parition handling routines */
34int mtdparts_init(void);
35int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36int find_dev_and_part(const char *id, struct mtd_device **dev,
37 u8 *part_num, struct part_info **part);
38#endif
39
addb2e16
BS
40extern nand_info_t nand_info[]; /* info for NAND chips */
41
42static int nand_dump_oob(nand_info_t *nand, ulong off)
43{
44 return 0;
45}
46
47static 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
856f0544 87static inline int str2long(char *p, ulong *num)
addb2e16 88{
856f0544 89 char *endptr;
addb2e16 90
856f0544
SR
91 *num = simple_strtoul(p, &endptr, 16);
92 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
93}
addb2e16 94
856f0544
SR
95static int
96arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
97{
98 int idx = nand_curr_device;
c76fe474 99#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
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;
addb2e16 125 }
856f0544 126 }
addb2e16 127#endif
addb2e16 128
856f0544
SR
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 }
addb2e16 137
856f0544
SR
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;
addb2e16
BS
145 }
146
c76fe474 147#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
148out:
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;
addb2e16
BS
156}
157
158int 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;
c750d2e6
MF
164#ifdef CFG_NAND_QUIET
165 int quiet = CFG_NAND_QUIET;
166#else
2255b2d2 167 int quiet = 0;
c750d2e6 168#endif
2255b2d2 169 const char *quiet_str = getenv("quiet");
addb2e16
BS
170
171 /* at least two arguments please */
172 if (argc < 2)
173 goto usage;
174
2255b2d2
SR
175 if (quiet_str)
176 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
177
addb2e16
BS
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;
43a2b0e7
SR
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
addb2e16
BS
219 return 0;
220 }
221
222 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
223 strncmp(cmd, "dump", 4) != 0 &&
2255b2d2
SR
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 )
addb2e16
BS
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
856f0544
SR
246 /*
247 * Syntax is:
248 * 0 1 2 3 4
249 * nand erase [clean] [off size]
250 */
2255b2d2
SR
251 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
252 nand_erase_options_t opts;
856f0544
SR
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;
2255b2d2
SR
256 int scrub = !strcmp(cmd, "scrub");
257
856f0544
SR
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;
2255b2d2
SR
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) {
856f0544
SR
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");
2255b2d2
SR
281
282 if (getc() == 'y' && getc() == '\r') {
283 opts.scrub = 1;
284 } else {
856f0544 285 puts("scrub aborted\n");
2255b2d2
SR
286 return -1;
287 }
288 }
289 ret = nand_erase_opts(nand, &opts);
addb2e16
BS
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) {
2255b2d2
SR
313 int read;
314
addb2e16
BS
315 if (argc < 4)
316 goto usage;
2255b2d2 317
addb2e16
BS
318 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
319
2255b2d2 320 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
856f0544
SR
321 printf("\nNAND %s: ", read ? "read" : "write");
322 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
323 return 1;
addb2e16 324
2255b2d2
SR
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 }
62d4f436
HW
350 } else if (s != NULL && !strcmp(s, ".oob")) {
351 /* read out-of-band data */
352 if (read)
0a1e03bc 353 ret = nand->read_oob(nand, off, size, (size_t *) &size,
62d4f436
HW
354 (u_char *) addr);
355 else
0a1e03bc 356 ret = nand->write_oob(nand, off, size, (size_t *) &size,
62d4f436 357 (u_char *) addr);
856f0544
SR
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);
2255b2d2
SR
363 }
364
addb2e16 365 printf(" %d bytes %s: %s\n", size,
2255b2d2 366 read ? "read" : "written", ret ? "ERROR" : "OK");
addb2e16
BS
367
368 return ret == 0 ? 0 : 1;
369 }
2255b2d2 370
2255b2d2
SR
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)) {
856f0544 434 puts("NAND flash successfully locked\n");
2255b2d2 435 } else {
856f0544 436 puts("Error locking NAND flash\n");
2255b2d2
SR
437 return 1;
438 }
439 }
440 return 0;
441 }
442
443 if (strcmp(cmd, "unlock") == 0) {
856f0544
SR
444 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
445 return 1;
2255b2d2
SR
446
447 if (!nand_unlock(nand, off, size)) {
856f0544 448 puts("NAND flash successfully unlocked\n");
2255b2d2 449 } else {
856f0544
SR
450 puts("Error unlocking NAND flash, "
451 "write and erase will probably fail\n");
2255b2d2
SR
452 return 1;
453 }
454 return 0;
455 }
456
addb2e16
BS
457usage:
458 printf("Usage:\n%s\n", cmdtp->usage);
459 return 1;
460}
461
462U_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"
856f0544 466 "nand read[.jffs2] - addr off|partition size\n"
5bd7fe9a 467 "nand write[.jffs2] - addr off|partition size - read/write `size' bytes starting\n"
addb2e16
BS
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"
2255b2d2
SR
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");
addb2e16 478
856f0544
SR
479static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
480 ulong offset, ulong addr, char *cmd)
addb2e16 481{
addb2e16 482 int r;
10e03893 483 char *ep, *s;
856f0544 484 ulong cnt;
addb2e16 485 image_header_t *hdr;
10e03893
TK
486 int jffs2 = 0;
487
488 s = strchr(cmd, '.');
489 if (s != NULL &&
490 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
491 jffs2 = 1;
addb2e16 492
856f0544 493 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
addb2e16
BS
494
495 cnt = nand->oobblock;
10e03893
TK
496 if (jffs2) {
497 nand_read_options_t opts;
498 memset(&opts, 0, sizeof(opts));
499 opts.buffer = (u_char*) addr;
500 opts.length = cnt;
501 opts.offset = offset;
502 opts.quiet = 1;
503 r = nand_read_opts(nand, &opts);
504 } else {
505 r = nand_read(nand, offset, &cnt, (u_char *) addr);
506 }
507
addb2e16 508 if (r) {
856f0544 509 puts("** Read error\n");
fad63407 510 show_boot_progress (-56);
addb2e16
BS
511 return 1;
512 }
fad63407 513 show_boot_progress (56);
addb2e16 514
9a4daad0 515 switch (genimg_get_format ((void *)addr)) {
d5934ad7
MB
516 case IMAGE_FORMAT_LEGACY:
517 hdr = (image_header_t *)addr;
518
519 if (!image_check_magic (hdr)) {
520 printf("\n** Bad Magic Number 0x%x **\n",
521 image_get_magic (hdr));
522 show_boot_progress (-57);
523 return 1;
524 }
525 show_boot_progress (57);
526
527 image_print_contents (hdr);
addb2e16 528
d5934ad7
MB
529 cnt = image_get_image_size (hdr);
530 break;
531#if defined(CONFIG_FIT)
532 case IMAGE_FORMAT_FIT:
533 fit_unsupported ("nand_load_image");
534 return 1;
535#endif
536 default:
537 puts ("** Unknown image type\n");
addb2e16
BS
538 return 1;
539 }
addb2e16 540
10e03893
TK
541 if (jffs2) {
542 nand_read_options_t opts;
543 memset(&opts, 0, sizeof(opts));
544 opts.buffer = (u_char*) addr;
545 opts.length = cnt;
546 opts.offset = offset;
547 opts.quiet = 1;
548 r = nand_read_opts(nand, &opts);
549 } else {
550 r = nand_read(nand, offset, &cnt, (u_char *) addr);
551 }
addb2e16 552
addb2e16 553 if (r) {
856f0544 554 puts("** Read error\n");
fad63407 555 show_boot_progress (-58);
addb2e16
BS
556 return 1;
557 }
fad63407 558 show_boot_progress (58);
addb2e16
BS
559
560 /* Loading ok, update default load address */
561
562 load_addr = addr;
563
564 /* Check if we should attempt an auto-start */
565 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
566 char *local_args[2];
567 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
568
856f0544 569 local_args[0] = cmd;
addb2e16
BS
570 local_args[1] = NULL;
571
572 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
573
574 do_bootm(cmdtp, 0, 1, local_args);
575 return 1;
576 }
577 return 0;
578}
579
856f0544
SR
580int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
581{
582 char *boot_device = NULL;
583 int idx;
584 ulong addr, offset = 0;
c76fe474 585#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
586 struct mtd_device *dev;
587 struct part_info *part;
588 u8 pnum;
589
590 if (argc >= 2) {
591 char *p = (argc == 2) ? argv[1] : argv[2];
592 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
593 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
594 if (dev->id->type != MTD_DEV_TYPE_NAND) {
595 puts("Not a NAND device\n");
596 return 1;
597 }
598 if (argc > 3)
599 goto usage;
600 if (argc == 3)
10e03893 601 addr = simple_strtoul(argv[1], NULL, 16);
856f0544
SR
602 else
603 addr = CFG_LOAD_ADDR;
604 return nand_load_image(cmdtp, &nand_info[dev->id->num],
605 part->offset, addr, argv[0]);
606 }
607 }
608#endif
609
fad63407 610 show_boot_progress(52);
856f0544
SR
611 switch (argc) {
612 case 1:
613 addr = CFG_LOAD_ADDR;
614 boot_device = getenv("bootdevice");
615 break;
616 case 2:
617 addr = simple_strtoul(argv[1], NULL, 16);
618 boot_device = getenv("bootdevice");
619 break;
620 case 3:
621 addr = simple_strtoul(argv[1], NULL, 16);
622 boot_device = argv[2];
623 break;
624 case 4:
625 addr = simple_strtoul(argv[1], NULL, 16);
626 boot_device = argv[2];
627 offset = simple_strtoul(argv[3], NULL, 16);
628 break;
629 default:
c76fe474 630#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
631usage:
632#endif
633 printf("Usage:\n%s\n", cmdtp->usage);
fad63407 634 show_boot_progress(-53);
856f0544
SR
635 return 1;
636 }
637
fad63407 638 show_boot_progress(53);
856f0544
SR
639 if (!boot_device) {
640 puts("\n** No boot device **\n");
fad63407 641 show_boot_progress(-54);
856f0544
SR
642 return 1;
643 }
fad63407 644 show_boot_progress(54);
addb2e16 645
856f0544
SR
646 idx = simple_strtoul(boot_device, NULL, 16);
647
648 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
649 printf("\n** Device %d not available\n", idx);
fad63407 650 show_boot_progress(-55);
856f0544
SR
651 return 1;
652 }
fad63407 653 show_boot_progress(55);
856f0544
SR
654
655 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
656}
657
658U_BOOT_CMD(nboot, 4, 1, do_nandboot,
659 "nboot - boot from NAND device\n",
10e03893 660 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
addb2e16 661
c76fe474 662#endif
addb2e16
BS
663
664#else /* CFG_NAND_LEGACY */
665/*
666 *
667 * Legacy NAND support - to be phased out
668 *
669 */
dc7c9a1a
WD
670#include <command.h>
671#include <malloc.h>
672#include <asm/io.h>
a3d991bd 673#include <watchdog.h>
dc7c9a1a 674
fad63407 675#ifdef CONFIG_show_boot_progress
dc7c9a1a 676# include <status_led.h>
fad63407 677# define show_boot_progress(arg) show_boot_progress(arg)
dc7c9a1a 678#else
fad63407 679# define show_boot_progress(arg)
dc7c9a1a
WD
680#endif
681
c76fe474 682#if defined(CONFIG_CMD_NAND)
addb2e16
BS
683#include <linux/mtd/nand_legacy.h>
684#if 0
dc7c9a1a 685#include <linux/mtd/nand_ids.h>
7a8e9bed 686#include <jffs2/jffs2.h>
addb2e16 687#endif
dc7c9a1a 688
1f4bb37d
WD
689#ifdef CONFIG_OMAP1510
690void archflashwp(void *archdata, int wp);
691#endif
692
693#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
694
a43278a4 695#undef NAND_DEBUG
dc7c9a1a 696#undef PSYCHO_DEBUG
7a8e9bed
WD
697
698/* ****************** WARNING *********************
699 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
700 * erase (or at least attempt to erase) blocks that are marked
701 * bad. This can be very handy if you are _sure_ that the block
702 * is OK, say because you marked a good block bad to test bad
703 * block handling and you are done testing, or if you have
704 * accidentally marked blocks bad.
705 *
706 * Erasing factory marked bad blocks is a _bad_ idea. If the
707 * erase succeeds there is no reliable way to find them again,
708 * and attempting to program or erase bad blocks can affect
709 * the data in _other_ (good) blocks.
710 */
711#define ALLOW_ERASE_BAD_DEBUG 0
dc7c9a1a
WD
712
713#define CONFIG_MTD_NAND_ECC /* enable ECC */
1f4bb37d 714#define CONFIG_MTD_NAND_ECC_JFFS2
dc7c9a1a 715
addb2e16 716/* bits for nand_legacy_rw() `cmd'; or together as needed */
7a8e9bed
WD
717#define NANDRW_READ 0x01
718#define NANDRW_WRITE 0x00
719#define NANDRW_JFFS2 0x02
a3d991bd 720#define NANDRW_JFFS2_SKIP 0x04
7a8e9bed 721
addb2e16
BS
722/*
723 * Imports from nand_legacy.c
724 */
725extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
726extern int curr_device;
727extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
728 size_t len, int clean);
729extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
730 size_t len, size_t *retlen, u_char *buf);
731extern void nand_print(struct nand_chip *nand);
732extern void nand_print_bad(struct nand_chip *nand);
733extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
734 size_t len, size_t *retlen, u_char *buf);
735extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
736 size_t len, size_t *retlen, const u_char *buf);
dc7c9a1a 737
dc7c9a1a 738
dd520bf3 739int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
dc7c9a1a 740{
dd520bf3 741 int rcode = 0;
dc7c9a1a 742
dd520bf3
WD
743 switch (argc) {
744 case 0:
745 case 1:
746 printf ("Usage:\n%s\n", cmdtp->usage);
747 return 1;
748 case 2:
749 if (strcmp (argv[1], "info") == 0) {
750 int i;
dc7c9a1a 751
dd520bf3 752 putc ('\n');
dc7c9a1a 753
dd520bf3
WD
754 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
755 if (nand_dev_desc[i].ChipID ==
756 NAND_ChipID_UNKNOWN)
757 continue; /* list only known devices */
758 printf ("Device %d: ", i);
759 nand_print (&nand_dev_desc[i]);
760 }
761 return 0;
7a8e9bed 762
dd520bf3
WD
763 } else if (strcmp (argv[1], "device") == 0) {
764 if ((curr_device < 0)
765 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
766 puts ("\nno devices available\n");
767 return 1;
768 }
769 printf ("\nDevice %d: ", curr_device);
770 nand_print (&nand_dev_desc[curr_device]);
771 return 0;
7a8e9bed 772
dd520bf3
WD
773 } else if (strcmp (argv[1], "bad") == 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 bad blocks:\n", curr_device);
780 nand_print_bad (&nand_dev_desc[curr_device]);
781 return 0;
dc7c9a1a 782
dc7c9a1a 783 }
dd520bf3
WD
784 printf ("Usage:\n%s\n", cmdtp->usage);
785 return 1;
786 case 3:
787 if (strcmp (argv[1], "device") == 0) {
788 int dev = (int) simple_strtoul (argv[2], NULL, 10);
dc7c9a1a 789
dd520bf3
WD
790 printf ("\nDevice %d: ", dev);
791 if (dev >= CFG_MAX_NAND_DEVICE) {
792 puts ("unknown device\n");
793 return 1;
794 }
795 nand_print (&nand_dev_desc[dev]);
796 /*nand_print (dev); */
dc7c9a1a 797
dd520bf3
WD
798 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
799 return 1;
800 }
dc7c9a1a 801
dd520bf3 802 curr_device = dev;
dc7c9a1a 803
dd520bf3 804 puts ("... is now current device\n");
7a8e9bed 805
dd520bf3
WD
806 return 0;
807 } else if (strcmp (argv[1], "erase") == 0
808 && strcmp (argv[2], "clean") == 0) {
809 struct nand_chip *nand = &nand_dev_desc[curr_device];
810 ulong off = 0;
811 ulong size = nand->totlen;
812 int ret;
7a8e9bed 813
dd520bf3 814 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
7a8e9bed 815
dd520bf3 816 ret = nand_legacy_erase (nand, off, size, 1);
7a8e9bed 817
dd520bf3 818 printf ("%s\n", ret ? "ERROR" : "OK");
dc7c9a1a 819
7a8e9bed
WD
820 return ret;
821 }
dd520bf3
WD
822
823 printf ("Usage:\n%s\n", cmdtp->usage);
824 return 1;
825 default:
826 /* at least 4 args */
827
828 if (strncmp (argv[1], "read", 4) == 0 ||
829 strncmp (argv[1], "write", 5) == 0) {
830 ulong addr = simple_strtoul (argv[2], NULL, 16);
831 ulong off = simple_strtoul (argv[3], NULL, 16);
832 ulong size = simple_strtoul (argv[4], NULL, 16);
833 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
834 NANDRW_READ : NANDRW_WRITE;
835 int ret, total;
836 char *cmdtail = strchr (argv[1], '.');
837
838 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
839 /* read out-of-band data */
840 if (cmd & NANDRW_READ) {
841 ret = nand_read_oob (nand_dev_desc + curr_device,
842 off, size, (size_t *) & total,
843 (u_char *) addr);
844 } else {
845 ret = nand_write_oob (nand_dev_desc + curr_device,
846 off, size, (size_t *) & total,
847 (u_char *) addr);
848 }
849 return ret;
850 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
851 cmd |= NANDRW_JFFS2; /* skip bad blocks */
852 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
853 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
854 if (cmd & NANDRW_READ)
855 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
856 }
7a8e9bed 857#ifdef SXNI855T
dd520bf3
WD
858 /* need ".e" same as ".j" for compatibility with older units */
859 else if (cmdtail && !strcmp (cmdtail, ".e"))
860 cmd |= NANDRW_JFFS2; /* skip bad blocks */
7a8e9bed 861#endif
a842a6d2 862#ifdef CFG_NAND_SKIP_BAD_DOT_I
dd520bf3
WD
863 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
864 /* ".i" for image -> read skips bad block (no 0xff) */
865 else if (cmdtail && !strcmp (cmdtail, ".i")) {
866 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
867 if (cmd & NANDRW_READ)
868 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
869 }
a842a6d2 870#endif /* CFG_NAND_SKIP_BAD_DOT_I */
dd520bf3
WD
871 else if (cmdtail) {
872 printf ("Usage:\n%s\n", cmdtp->usage);
873 return 1;
874 }
dc7c9a1a 875
dd520bf3
WD
876 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
877 (cmd & NANDRW_READ) ? "read" : "write",
878 curr_device, off, size);
dc7c9a1a 879
dd520bf3
WD
880 ret = nand_legacy_rw (nand_dev_desc + curr_device,
881 cmd, off, size,
882 (size_t *) & total,
883 (u_char *) addr);
dc7c9a1a 884
dd520bf3
WD
885 printf (" %d bytes %s: %s\n", total,
886 (cmd & NANDRW_READ) ? "read" : "written",
887 ret ? "ERROR" : "OK");
dc7c9a1a 888
dd520bf3
WD
889 return ret;
890 } else if (strcmp (argv[1], "erase") == 0 &&
891 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
892 int clean = argc == 5;
893 ulong off =
894 simple_strtoul (argv[2 + clean], NULL, 16);
895 ulong size =
896 simple_strtoul (argv[3 + clean], NULL, 16);
897 int ret;
dc7c9a1a 898
dd520bf3
WD
899 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
900 curr_device, off, size);
dc7c9a1a 901
dd520bf3
WD
902 ret = nand_legacy_erase (nand_dev_desc + curr_device,
903 off, size, clean);
dc7c9a1a 904
dd520bf3 905 printf ("%s\n", ret ? "ERROR" : "OK");
dc7c9a1a 906
dd520bf3
WD
907 return ret;
908 } else {
909 printf ("Usage:\n%s\n", cmdtp->usage);
910 rcode = 1;
911 }
dc7c9a1a 912
dd520bf3
WD
913 return rcode;
914 }
dc7c9a1a
WD
915}
916
0d498393
WD
917U_BOOT_CMD(
918 nand, 5, 1, do_nand,
2255b2d2 919 "nand - legacy NAND sub-system\n",
b0fce99b
WD
920 "info - show available NAND devices\n"
921 "nand device [dev] - show or set current device\n"
a3d991bd 922 "nand read[.jffs2[s]] addr off size\n"
b0fce99b
WD
923 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
924 " at offset `off' to/from memory address `addr'\n"
925 "nand erase [clean] [off size] - erase `size' bytes from\n"
926 " offset `off' (entire device if not specified)\n"
927 "nand bad - show bad blocks\n"
928 "nand read.oob addr off size - read out-of-band data\n"
929 "nand write.oob addr off size - read out-of-band data\n"
930);
931
dc7c9a1a
WD
932int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
933{
934 char *boot_device = NULL;
935 char *ep;
936 int dev;
937 ulong cnt;
938 ulong addr;
939 ulong offset = 0;
940 image_header_t *hdr;
941 int rcode = 0;
fad63407 942 show_boot_progress (52);
dc7c9a1a
WD
943 switch (argc) {
944 case 1:
945 addr = CFG_LOAD_ADDR;
946 boot_device = getenv ("bootdevice");
947 break;
948 case 2:
949 addr = simple_strtoul(argv[1], NULL, 16);
950 boot_device = getenv ("bootdevice");
951 break;
952 case 3:
953 addr = simple_strtoul(argv[1], NULL, 16);
954 boot_device = argv[2];
955 break;
956 case 4:
957 addr = simple_strtoul(argv[1], NULL, 16);
958 boot_device = argv[2];
959 offset = simple_strtoul(argv[3], NULL, 16);
960 break;
961 default:
962 printf ("Usage:\n%s\n", cmdtp->usage);
fad63407 963 show_boot_progress (-53);
dc7c9a1a
WD
964 return 1;
965 }
966
fad63407 967 show_boot_progress (53);
dc7c9a1a
WD
968 if (!boot_device) {
969 puts ("\n** No boot device **\n");
fad63407 970 show_boot_progress (-54);
dc7c9a1a
WD
971 return 1;
972 }
fad63407 973 show_boot_progress (54);
dc7c9a1a
WD
974
975 dev = simple_strtoul(boot_device, &ep, 16);
976
977 if ((dev >= CFG_MAX_NAND_DEVICE) ||
978 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
979 printf ("\n** Device %d not available\n", dev);
fad63407 980 show_boot_progress (-55);
dc7c9a1a
WD
981 return 1;
982 }
fad63407 983 show_boot_progress (55);
dc7c9a1a 984
7a8e9bed 985 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
dc7c9a1a
WD
986 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
987 offset);
988
addb2e16
BS
989 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
990 SECTORSIZE, NULL, (u_char *)addr)) {
dc7c9a1a 991 printf ("** Read error on %d\n", dev);
fad63407 992 show_boot_progress (-56);
dc7c9a1a
WD
993 return 1;
994 }
fad63407 995 show_boot_progress (56);
dc7c9a1a 996
9a4daad0 997 switch (genimg_get_format ((void *)addr)) {
d5934ad7
MB
998 case IMAGE_FORMAT_LEGACY:
999 hdr = (image_header_t *)addr;
dc7c9a1a 1000
d5934ad7 1001 if (image_check_magic (hdr)) {
dc7c9a1a 1002
d5934ad7 1003 image_print_contents (hdr);
dc7c9a1a 1004
d5934ad7
MB
1005 cnt = image_get_image_size (hdr);
1006 cnt -= SECTORSIZE;
1007 } else {
1008 printf ("\n** Bad Magic Number 0x%x **\n",
1009 image_get_magic (hdr));
1010 show_boot_progress (-57);
1011 return 1;
1012 }
1013 break;
1014#if defined(CONFIG_FIT)
1015 case IMAGE_FORMAT_FIT:
1016 fit_unsupported ("nboot");
1017 return 1;
1018#endif
1019 default:
1020 puts ("** Unknown image type\n");
dc7c9a1a
WD
1021 return 1;
1022 }
fad63407 1023 show_boot_progress (57);
dc7c9a1a 1024
addb2e16
BS
1025 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1026 offset + SECTORSIZE, cnt, NULL,
1027 (u_char *)(addr+SECTORSIZE))) {
dc7c9a1a 1028 printf ("** Read error on %d\n", dev);
fad63407 1029 show_boot_progress (-58);
dc7c9a1a
WD
1030 return 1;
1031 }
fad63407 1032 show_boot_progress (58);
dc7c9a1a
WD
1033
1034 /* Loading ok, update default load address */
1035
1036 load_addr = addr;
1037
1038 /* Check if we should attempt an auto-start */
1039 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1040 char *local_args[2];
1041 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1042
1043 local_args[0] = argv[0];
1044 local_args[1] = NULL;
1045
7a8e9bed 1046 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
dc7c9a1a
WD
1047
1048 do_bootm (cmdtp, 0, 1, local_args);
1049 rcode = 1;
1050 }
1051 return rcode;
1052}
1053
0d498393
WD
1054U_BOOT_CMD(
1055 nboot, 4, 1, do_nandboot,
b0fce99b
WD
1056 "nboot - boot from NAND device\n",
1057 "loadAddr dev\n"
1058);
1059
c76fe474 1060#endif
dc7c9a1a 1061
addb2e16 1062#endif /* CFG_NAND_LEGACY */