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