moving files from yaffs2/direct/ to yaffs2/ and deleting all symlinks
[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>
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
c76fe474 32#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
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,
5e1dae5c 38 u8 *part_num, struct part_info **part);
856f0544
SR
39#endif
40
addb2e16
BS
41static int nand_dump_oob(nand_info_t *nand, ulong off)
42{
43 return 0;
44}
45
46static int nand_dump(nand_info_t *nand, ulong off)
47{
48 int i;
49 u_char *buf, *p;
50
cfa460ad 51 buf = malloc(nand->writesize + nand->oobsize);
addb2e16
BS
52 if (!buf) {
53 puts("No memory for page buffer\n");
54 return 1;
55 }
cfa460ad 56 off &= ~(nand->writesize - 1);
cfa460ad
WJ
57 size_t dummy;
58 loff_t addr = (loff_t) off;
59 i = nand->read(nand, addr, nand->writesize, &dummy, buf);
addb2e16 60 if (i < 0) {
e870690b 61 printf("Error (%d) reading page %08lx\n", i, off);
addb2e16
BS
62 free(buf);
63 return 1;
64 }
e870690b 65 printf("Page %08lx dump:\n", off);
cfa460ad 66 i = nand->writesize >> 4; p = buf;
addb2e16 67 while (i--) {
cfa460ad
WJ
68 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
69 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
70 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
71 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
addb2e16
BS
72 p += 16;
73 }
74 puts("OOB:\n");
75 i = nand->oobsize >> 3;
76 while (i--) {
cfa460ad
WJ
77 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
78 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
addb2e16
BS
79 p += 8;
80 }
81 free(buf);
82
83 return 0;
84}
85
86/* ------------------------------------------------------------------------- */
87
856f0544 88static inline int str2long(char *p, ulong *num)
addb2e16 89{
856f0544 90 char *endptr;
addb2e16 91
856f0544
SR
92 *num = simple_strtoul(p, &endptr, 16);
93 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
94}
addb2e16 95
856f0544 96static int
4ca79f47 97arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
856f0544
SR
98{
99 int idx = nand_curr_device;
c76fe474 100#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
101 struct mtd_device *dev;
102 struct part_info *part;
103 u8 pnum;
104
105 if (argc >= 1 && !(str2long(argv[0], off))) {
106 if ((mtdparts_init() == 0) &&
107 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
108 if (dev->id->type != MTD_DEV_TYPE_NAND) {
109 puts("not a NAND device\n");
110 return -1;
111 }
112 *off = part->offset;
113 if (argc >= 2) {
42ffcec3 114 if (!(str2long(argv[1], (ulong *)size))) {
856f0544
SR
115 printf("'%s' is not a number\n", argv[1]);
116 return -1;
117 }
118 if (*size > part->size)
119 *size = part->size;
120 } else {
121 *size = part->size;
122 }
123 idx = dev->id->num;
124 *nand = nand_info[idx];
125 goto out;
addb2e16 126 }
856f0544 127 }
addb2e16 128#endif
addb2e16 129
856f0544
SR
130 if (argc >= 1) {
131 if (!(str2long(argv[0], off))) {
132 printf("'%s' is not a number\n", argv[0]);
133 return -1;
134 }
135 } else {
136 *off = 0;
137 }
addb2e16 138
856f0544 139 if (argc >= 2) {
4ca79f47 140 if (!(str2long(argv[1], (ulong *)size))) {
856f0544
SR
141 printf("'%s' is not a number\n", argv[1]);
142 return -1;
143 }
144 } else {
145 *size = nand->size - *off;
addb2e16
BS
146 }
147
c76fe474 148#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
149out:
150#endif
151 printf("device %d ", idx);
152 if (*size == nand->size)
153 puts("whole chip\n");
154 else
e870690b 155 printf("offset 0x%lx, size 0x%x\n", *off, *size);
856f0544 156 return 0;
addb2e16
BS
157}
158
159int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
160{
cfa460ad 161 int i, dev, ret = 0;
4ca79f47
WD
162 ulong addr, off;
163 size_t size;
addb2e16
BS
164 char *cmd, *s;
165 nand_info_t *nand;
c750d2e6
MF
166#ifdef CFG_NAND_QUIET
167 int quiet = CFG_NAND_QUIET;
168#else
2255b2d2 169 int quiet = 0;
c750d2e6 170#endif
2255b2d2 171 const char *quiet_str = getenv("quiet");
addb2e16
BS
172
173 /* at least two arguments please */
174 if (argc < 2)
175 goto usage;
176
2255b2d2
SR
177 if (quiet_str)
178 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
179
addb2e16
BS
180 cmd = argv[1];
181
182 if (strcmp(cmd, "info") == 0) {
183
184 putc('\n');
185 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
186 if (nand_info[i].name)
e870690b 187 printf("Device %d: %s, sector size %u KiB\n",
cfa460ad
WJ
188 i, nand_info[i].name,
189 nand_info[i].erasesize >> 10);
addb2e16
BS
190 }
191 return 0;
192 }
193
194 if (strcmp(cmd, "device") == 0) {
195
196 if (argc < 3) {
197 if ((nand_curr_device < 0) ||
3043c045 198 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
addb2e16
BS
199 puts("\nno devices available\n");
200 else
201 printf("\nDevice %d: %s\n", nand_curr_device,
cfa460ad 202 nand_info[nand_curr_device].name);
addb2e16
BS
203 return 0;
204 }
205 dev = (int)simple_strtoul(argv[2], NULL, 10);
206 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
207 puts("No such device\n");
208 return 1;
209 }
210 printf("Device %d: %s", dev, nand_info[dev].name);
211 puts("... is now current device\n");
212 nand_curr_device = dev;
43a2b0e7
SR
213
214#ifdef CFG_NAND_SELECT_DEVICE
215 /*
216 * Select the chip in the board/cpu specific driver
217 */
218 board_nand_select_device(nand_info[dev].priv, dev);
219#endif
220
addb2e16
BS
221 return 0;
222 }
223
224 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
3043c045
WJ
225 strncmp(cmd, "dump", 4) != 0 &&
226 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
227 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
228 strcmp(cmd, "biterr") != 0 &&
229 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
addb2e16
BS
230 goto usage;
231
232 /* the following commands operate on the current device */
233 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
234 !nand_info[nand_curr_device].name) {
235 puts("\nno devices available\n");
236 return 1;
237 }
238 nand = &nand_info[nand_curr_device];
239
240 if (strcmp(cmd, "bad") == 0) {
241 printf("\nDevice %d bad blocks:\n", nand_curr_device);
242 for (off = 0; off < nand->size; off += nand->erasesize)
243 if (nand_block_isbad(nand, off))
e870690b 244 printf(" %08lx\n", off);
addb2e16
BS
245 return 0;
246 }
247
856f0544
SR
248 /*
249 * Syntax is:
250 * 0 1 2 3 4
251 * nand erase [clean] [off size]
252 */
2255b2d2
SR
253 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
254 nand_erase_options_t opts;
856f0544 255 /* "clean" at index 2 means request to write cleanmarker */
3043c045 256 int clean = argc > 2 && !strcmp("clean", argv[2]);
856f0544 257 int o = clean ? 3 : 2;
2255b2d2
SR
258 int scrub = !strcmp(cmd, "scrub");
259
856f0544
SR
260 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
261 /* skip first two or three arguments, look for offset and size */
262 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
263 return 1;
2255b2d2
SR
264
265 memset(&opts, 0, sizeof(opts));
266 opts.offset = off;
267 opts.length = size;
268 opts.jffs2 = clean;
269 opts.quiet = quiet;
270
271 if (scrub) {
856f0544
SR
272 puts("Warning: "
273 "scrub option will erase all factory set "
274 "bad blocks!\n"
275 " "
276 "There is no reliable way to recover them.\n"
277 " "
278 "Use this command only for testing purposes "
279 "if you\n"
280 " "
281 "are sure of what you are doing!\n"
282 "\nReally scrub this NAND flash? <y/N>\n");
2255b2d2
SR
283
284 if (getc() == 'y' && getc() == '\r') {
285 opts.scrub = 1;
286 } else {
856f0544 287 puts("scrub aborted\n");
2255b2d2
SR
288 return -1;
289 }
290 }
291 ret = nand_erase_opts(nand, &opts);
addb2e16
BS
292 printf("%s\n", ret ? "ERROR" : "OK");
293
294 return ret == 0 ? 0 : 1;
295 }
296
297 if (strncmp(cmd, "dump", 4) == 0) {
298 if (argc < 3)
299 goto usage;
300
301 s = strchr(cmd, '.');
302 off = (int)simple_strtoul(argv[2], NULL, 16);
303
304 if (s != NULL && strcmp(s, ".oob") == 0)
305 ret = nand_dump_oob(nand, off);
306 else
307 ret = nand_dump(nand, off);
308
309 return ret == 0 ? 1 : 0;
310
311 }
312
313 /* read write */
314 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
2255b2d2
SR
315 int read;
316
addb2e16
BS
317 if (argc < 4)
318 goto usage;
2255b2d2 319
addb2e16
BS
320 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
321
2255b2d2 322 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
856f0544
SR
323 printf("\nNAND %s: ", read ? "read" : "write");
324 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
325 return 1;
4cbb651b 326
2255b2d2
SR
327 s = strchr(cmd, '.');
328 if (s != NULL &&
3043c045 329 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
2255b2d2
SR
330 if (read) {
331 /* read */
332 nand_read_options_t opts;
333 memset(&opts, 0, sizeof(opts));
cfa460ad
WJ
334 opts.buffer = (u_char*) addr;
335 opts.length = size;
336 opts.offset = off;
337 opts.quiet = quiet;
3043c045
WJ
338/*
339 * ! BROKEN !
340 *
341 * TODO: Function must be implemented
342 *
343 * ret = nand_read_opts(nand, &opts);
344 */
2255b2d2
SR
345 } else {
346 /* write */
cfa460ad 347 mtd_oob_ops_t opts;
2255b2d2 348 memset(&opts, 0, sizeof(opts));
cfa460ad
WJ
349 opts.datbuf = (u_char*) addr;
350 opts.len = size;
351 opts.ooblen = 64;
352 opts.mode = MTD_OOB_AUTO;
353 ret = nand_write_opts(nand, off, &opts);
2255b2d2 354 }
62d4f436 355 } else if (s != NULL && !strcmp(s, ".oob")) {
cfa460ad
WJ
356 /* out-of-band data */
357 mtd_oob_ops_t ops = {
358 .oobbuf = (u8 *)addr,
359 .ooblen = size,
360 .mode = MTD_OOB_RAW
361 };
362
62d4f436 363 if (read)
cfa460ad 364 ret = nand->read_oob(nand, off, &ops);
62d4f436 365 else
cfa460ad 366 ret = nand->write_oob(nand, off, &ops);
856f0544
SR
367 } else {
368 if (read)
369 ret = nand_read(nand, off, &size, (u_char *)addr);
370 else
371 ret = nand_write(nand, off, &size, (u_char *)addr);
2255b2d2
SR
372 }
373
addb2e16 374 printf(" %d bytes %s: %s\n", size,
2255b2d2 375 read ? "read" : "written", ret ? "ERROR" : "OK");
addb2e16
BS
376
377 return ret == 0 ? 0 : 1;
378 }
2255b2d2 379
2255b2d2
SR
380 if (strcmp(cmd, "markbad") == 0) {
381 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
382
383 int ret = nand->block_markbad(nand, addr);
384 if (ret == 0) {
385 printf("block 0x%08lx successfully marked as bad\n",
386 (ulong) addr);
387 return 0;
388 } else {
389 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
390 (ulong) addr, ret);
391 }
392 return 1;
393 }
394 if (strcmp(cmd, "biterr") == 0) {
395 /* todo */
396 return 1;
397 }
398
399 if (strcmp(cmd, "lock") == 0) {
400 int tight = 0;
401 int status = 0;
402 if (argc == 3) {
403 if (!strcmp("tight", argv[2]))
404 tight = 1;
405 if (!strcmp("status", argv[2]))
406 status = 1;
407 }
3043c045
WJ
408/*
409 * ! BROKEN !
410 *
411 * TODO: must be implemented and tested by someone with HW
412 */
413#if 0
2255b2d2 414 if (status) {
3043c045 415 ulong block_start = 0;
2255b2d2 416 ulong off;
5e1dae5c 417 int last_status = -1;
3043c045 418
2255b2d2
SR
419 struct nand_chip *nand_chip = nand->priv;
420 /* check the WP bit */
421 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
422 printf("device is %swrite protected\n",
423 (nand_chip->read_byte(nand) & 0x80 ?
5e1dae5c 424 "NOT " : ""));
cfa460ad
WJ
425
426 for (off = 0; off < nand->size; off += nand->writesize) {
5e1dae5c
WJ
427 int s = nand_get_lock_status(nand, off);
428
429 /* print message only if status has changed
430 * or at end of chip
431 */
432 if (off == nand->size - nand->writesize
3043c045 433 || (s != last_status && off != 0)) {
5e1dae5c
WJ
434
435 printf("%08lx - %08lx: %8d pages %s%s%s\n",
3043c045
WJ
436 block_start,
437 off-1,
438 (off-block_start)/nand->writesize,
439 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
440 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
441 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
5e1dae5c
WJ
442 }
443
444 last_status = s;
2255b2d2 445 }
cfa460ad 446 } else {
5e1dae5c
WJ
447 if (!nand_lock(nand, tight)) {
448 puts("NAND flash successfully locked\n");
449 } else {
450 puts("Error locking NAND flash\n");
451 return 1;
452 }
2255b2d2 453 }
3043c045 454#endif
2255b2d2
SR
455 return 0;
456 }
457
458 if (strcmp(cmd, "unlock") == 0) {
856f0544
SR
459 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
460 return 1;
2255b2d2 461
3043c045
WJ
462/*
463 * ! BROKEN !
464 *
465 * TODO: must be implemented and tested by someone with HW
466 */
467#if 0
5e1dae5c
WJ
468 if (!nand_unlock(nand, off, size)) {
469 puts("NAND flash successfully unlocked\n");
470 } else {
471 puts("Error unlocking NAND flash, "
3043c045 472 "write and erase will probably fail\n");
5e1dae5c
WJ
473 return 1;
474 }
475#endif
2255b2d2
SR
476 return 0;
477 }
478
addb2e16
BS
479usage:
480 printf("Usage:\n%s\n", cmdtp->usage);
481 return 1;
482}
483
484U_BOOT_CMD(nand, 5, 1, do_nand,
cfa460ad
WJ
485 "nand - NAND sub-system\n",
486 "info - show available NAND devices\n"
487 "nand device [dev] - show or set current device\n"
488 "nand read[.jffs2] - addr off|partition size\n"
489 "nand write[.jffs2] - addr off|partition size\n"
490 " read/write 'size' bytes starting at offset 'off'\n"
491 " to/from memory address 'addr'\n"
492 "nand erase [clean] [off size] - erase 'size' bytes from\n"
493 " offset 'off' (entire device if not specified)\n"
494 "nand bad - show bad blocks\n"
495 "nand dump[.oob] off - dump page\n"
496 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
497 "nand markbad off - mark bad block at offset (UNSAFE)\n"
498 "nand biterr off - make a bit error at offset (UNSAFE)\n"
499 "nand lock [tight] [status]\n"
500 " bring nand to lock state or display locked pages\n"
501 "nand unlock [offset] [size] - unlock section\n");
addb2e16 502
856f0544 503static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
cfa460ad 504 ulong offset, ulong addr, char *cmd)
addb2e16 505{
addb2e16 506 int r;
10e03893 507 char *ep, *s;
4ca79f47 508 size_t cnt;
addb2e16 509 image_header_t *hdr;
10e03893 510 int jffs2 = 0;
09475f75 511#if defined(CONFIG_FIT)
3bab76a2 512 const void *fit_hdr = NULL;
09475f75 513#endif
10e03893
TK
514
515 s = strchr(cmd, '.');
516 if (s != NULL &&
517 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
518 jffs2 = 1;
addb2e16 519
856f0544 520 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
addb2e16 521
cfa460ad
WJ
522 cnt = nand->writesize;
523 r = nand_read(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
cfa460ad 555 r = nand_read(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;
c76fe474 601#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
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
SR
618 else
619 addr = CFG_LOAD_ADDR;
620 return nand_load_image(cmdtp, &nand_info[dev->id->num],
cfa460ad 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:
629 addr = CFG_LOAD_ADDR;
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:
c76fe474 646#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
647usage:
648#endif
649 printf("Usage:\n%s\n", cmdtp->usage);
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
664 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
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,
675 "nboot - boot from NAND device\n",
10e03893 676 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
addb2e16 677
c76fe474 678#endif
addb2e16
BS
679
680#else /* CFG_NAND_LEGACY */
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 */
741extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
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:
762 printf ("Usage:\n%s\n", cmdtp->usage);
763 return 1;
764 case 2:
765 if (strcmp (argv[1], "info") == 0) {
766 int i;
dc7c9a1a 767
dd520bf3 768 putc ('\n');
dc7c9a1a 769
dd520bf3
WD
770 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
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)
781 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
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)
791 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
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 }
dd520bf3
WD
800 printf ("Usage:\n%s\n", cmdtp->usage);
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
WD
806 printf ("\nDevice %d: ", dev);
807 if (dev >= CFG_MAX_NAND_DEVICE) {
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
WD
838
839 printf ("Usage:\n%s\n", cmdtp->usage);
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) ?
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;
867 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
868 cmd |= NANDRW_JFFS2; /* skip bad blocks */
869 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
870 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
871 if (cmd & NANDRW_READ)
872 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
873 }
7a8e9bed 874#ifdef SXNI855T
dd520bf3
WD
875 /* need ".e" same as ".j" for compatibility with older units */
876 else if (cmdtail && !strcmp (cmdtail, ".e"))
877 cmd |= NANDRW_JFFS2; /* skip bad blocks */
7a8e9bed 878#endif
a842a6d2 879#ifdef CFG_NAND_SKIP_BAD_DOT_I
dd520bf3
WD
880 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
881 /* ".i" for image -> read skips bad block (no 0xff) */
882 else if (cmdtail && !strcmp (cmdtail, ".i")) {
883 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
884 if (cmd & NANDRW_READ)
3043c045 885 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
dd520bf3 886 }
a842a6d2 887#endif /* CFG_NAND_SKIP_BAD_DOT_I */
dd520bf3
WD
888 else if (cmdtail) {
889 printf ("Usage:\n%s\n", cmdtp->usage);
890 return 1;
891 }
dc7c9a1a 892
9b55a253 893 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
dd520bf3 894 (cmd & NANDRW_READ) ? "read" : "write",
9b55a253 895 curr_device, off, (ulong)size);
dc7c9a1a 896
dd520bf3
WD
897 ret = nand_legacy_rw (nand_dev_desc + curr_device,
898 cmd, off, size,
3043c045 899 &total, (u_char *) addr);
dc7c9a1a 900
dd520bf3
WD
901 printf (" %d bytes %s: %s\n", total,
902 (cmd & NANDRW_READ) ? "read" : "written",
903 ret ? "ERROR" : "OK");
dc7c9a1a 904
dd520bf3
WD
905 return ret;
906 } else if (strcmp (argv[1], "erase") == 0 &&
907 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
908 int clean = argc == 5;
909 ulong off =
910 simple_strtoul (argv[2 + clean], NULL, 16);
911 ulong size =
912 simple_strtoul (argv[3 + clean], NULL, 16);
913 int ret;
dc7c9a1a 914
dd520bf3
WD
915 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
916 curr_device, off, size);
dc7c9a1a 917
dd520bf3
WD
918 ret = nand_legacy_erase (nand_dev_desc + curr_device,
919 off, size, clean);
dc7c9a1a 920
dd520bf3 921 printf ("%s\n", ret ? "ERROR" : "OK");
dc7c9a1a 922
dd520bf3
WD
923 return ret;
924 } else {
925 printf ("Usage:\n%s\n", cmdtp->usage);
926 rcode = 1;
927 }
dc7c9a1a 928
dd520bf3
WD
929 return rcode;
930 }
dc7c9a1a
WD
931}
932
0d498393 933U_BOOT_CMD(
3043c045 934 nand, 5, 1, do_nand,
2255b2d2 935 "nand - legacy NAND sub-system\n",
b0fce99b
WD
936 "info - show available NAND devices\n"
937 "nand device [dev] - show or set current device\n"
a3d991bd 938 "nand read[.jffs2[s]] addr off size\n"
b0fce99b 939 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
3043c045 940 " at offset `off' to/from memory address `addr'\n"
b0fce99b 941 "nand erase [clean] [off size] - erase `size' bytes from\n"
3043c045 942 " offset `off' (entire device if not specified)\n"
b0fce99b
WD
943 "nand bad - show bad blocks\n"
944 "nand read.oob addr off size - read out-of-band data\n"
945 "nand write.oob addr off size - read out-of-band data\n"
946);
947
dc7c9a1a
WD
948int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
949{
950 char *boot_device = NULL;
951 char *ep;
952 int dev;
953 ulong cnt;
954 ulong addr;
955 ulong offset = 0;
956 image_header_t *hdr;
957 int rcode = 0;
09475f75 958#if defined(CONFIG_FIT)
3bab76a2 959 const void *fit_hdr = NULL;
09475f75
MB
960#endif
961
fad63407 962 show_boot_progress (52);
dc7c9a1a
WD
963 switch (argc) {
964 case 1:
965 addr = CFG_LOAD_ADDR;
966 boot_device = getenv ("bootdevice");
967 break;
968 case 2:
969 addr = simple_strtoul(argv[1], NULL, 16);
970 boot_device = getenv ("bootdevice");
971 break;
972 case 3:
973 addr = simple_strtoul(argv[1], NULL, 16);
974 boot_device = argv[2];
975 break;
976 case 4:
977 addr = simple_strtoul(argv[1], NULL, 16);
978 boot_device = argv[2];
979 offset = simple_strtoul(argv[3], NULL, 16);
980 break;
981 default:
982 printf ("Usage:\n%s\n", cmdtp->usage);
fad63407 983 show_boot_progress (-53);
dc7c9a1a
WD
984 return 1;
985 }
986
fad63407 987 show_boot_progress (53);
dc7c9a1a
WD
988 if (!boot_device) {
989 puts ("\n** No boot device **\n");
fad63407 990 show_boot_progress (-54);
dc7c9a1a
WD
991 return 1;
992 }
fad63407 993 show_boot_progress (54);
dc7c9a1a
WD
994
995 dev = simple_strtoul(boot_device, &ep, 16);
996
997 if ((dev >= CFG_MAX_NAND_DEVICE) ||
3043c045 998 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
dc7c9a1a 999 printf ("\n** Device %d not available\n", dev);
fad63407 1000 show_boot_progress (-55);
dc7c9a1a
WD
1001 return 1;
1002 }
fad63407 1003 show_boot_progress (55);
dc7c9a1a 1004
7a8e9bed 1005 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
cfa460ad
WJ
1006 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1007 offset);
dc7c9a1a 1008
addb2e16 1009 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
cfa460ad 1010 SECTORSIZE, NULL, (u_char *)addr)) {
dc7c9a1a 1011 printf ("** Read error on %d\n", dev);
fad63407 1012 show_boot_progress (-56);
dc7c9a1a
WD
1013 return 1;
1014 }
fad63407 1015 show_boot_progress (56);
dc7c9a1a 1016
9a4daad0 1017 switch (genimg_get_format ((void *)addr)) {
d5934ad7
MB
1018 case IMAGE_FORMAT_LEGACY:
1019 hdr = (image_header_t *)addr;
09475f75 1020 image_print_contents (hdr);
dc7c9a1a 1021
09475f75
MB
1022 cnt = image_get_image_size (hdr);
1023 cnt -= SECTORSIZE;
d5934ad7
MB
1024 break;
1025#if defined(CONFIG_FIT)
1026 case IMAGE_FORMAT_FIT:
09475f75 1027 fit_hdr = (const void *)addr;
09475f75
MB
1028 puts ("Fit image detected...\n");
1029
1030 cnt = fit_get_size (fit_hdr);
1031 break;
d5934ad7
MB
1032#endif
1033 default:
09475f75 1034 show_boot_progress (-57);
d5934ad7 1035 puts ("** Unknown image type\n");
dc7c9a1a
WD
1036 return 1;
1037 }
fad63407 1038 show_boot_progress (57);
dc7c9a1a 1039
addb2e16 1040 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
cfa460ad
WJ
1041 offset + SECTORSIZE, cnt, NULL,
1042 (u_char *)(addr+SECTORSIZE))) {
dc7c9a1a 1043 printf ("** Read error on %d\n", dev);
fad63407 1044 show_boot_progress (-58);
dc7c9a1a
WD
1045 return 1;
1046 }
fad63407 1047 show_boot_progress (58);
dc7c9a1a 1048
09475f75
MB
1049#if defined(CONFIG_FIT)
1050 /* This cannot be done earlier, we need complete FIT image in RAM first */
3bab76a2
MB
1051 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1052 if (!fit_check_format (fit_hdr)) {
1053 show_boot_progress (-150);
1054 puts ("** Bad FIT image format\n");
1055 return 1;
1056 }
1057 show_boot_progress (151);
1058 fit_print_contents (fit_hdr);
1059 }
09475f75
MB
1060#endif
1061
dc7c9a1a
WD
1062 /* Loading ok, update default load address */
1063
1064 load_addr = addr;
1065
1066 /* Check if we should attempt an auto-start */
1067 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1068 char *local_args[2];
1069 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1070
1071 local_args[0] = argv[0];
1072 local_args[1] = NULL;
1073
7a8e9bed 1074 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
dc7c9a1a
WD
1075
1076 do_bootm (cmdtp, 0, 1, local_args);
1077 rcode = 1;
1078 }
1079 return rcode;
1080}
1081
0d498393 1082U_BOOT_CMD(
3043c045 1083 nboot, 4, 1, do_nandboot,
b0fce99b
WD
1084 "nboot - boot from NAND device\n",
1085 "loadAddr dev\n"
1086);
1087
c76fe474 1088#endif
dc7c9a1a 1089
addb2e16 1090#endif /* CFG_NAND_LEGACY */