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