74xx/7xx/86xx: Rename flush_data_cache to flush_dcache to match 85xx version
[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
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,
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;
c76fe474 108#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
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
c76fe474 156#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
157out:
158#endif
159 printf("device %d ", idx);
160 if (*size == nand->size)
161 puts("whole chip\n");
162 else
e870690b 163 printf("offset 0x%lx, size 0x%x\n", *off, *size);
856f0544 164 return 0;
addb2e16
BS
165}
166
167int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
168{
cfa460ad 169 int i, dev, ret = 0;
4ca79f47
WD
170 ulong addr, off;
171 size_t size;
addb2e16
BS
172 char *cmd, *s;
173 nand_info_t *nand;
c750d2e6
MF
174#ifdef CFG_NAND_QUIET
175 int quiet = CFG_NAND_QUIET;
176#else
2255b2d2 177 int quiet = 0;
c750d2e6 178#endif
2255b2d2 179 const char *quiet_str = getenv("quiet");
addb2e16
BS
180
181 /* at least two arguments please */
182 if (argc < 2)
183 goto usage;
184
2255b2d2
SR
185 if (quiet_str)
186 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
187
addb2e16
BS
188 cmd = argv[1];
189
190 if (strcmp(cmd, "info") == 0) {
191
192 putc('\n');
193 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
194 if (nand_info[i].name)
e870690b 195 printf("Device %d: %s, sector size %u KiB\n",
4b070809 196 i, nand_info[i].name,
cfa460ad 197 nand_info[i].erasesize >> 10);
addb2e16
BS
198 }
199 return 0;
200 }
201
202 if (strcmp(cmd, "device") == 0) {
203
204 if (argc < 3) {
205 if ((nand_curr_device < 0) ||
3043c045 206 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
addb2e16
BS
207 puts("\nno devices available\n");
208 else
209 printf("\nDevice %d: %s\n", nand_curr_device,
cfa460ad 210 nand_info[nand_curr_device].name);
addb2e16
BS
211 return 0;
212 }
213 dev = (int)simple_strtoul(argv[2], NULL, 10);
214 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
215 puts("No such device\n");
216 return 1;
217 }
218 printf("Device %d: %s", dev, nand_info[dev].name);
219 puts("... is now current device\n");
220 nand_curr_device = dev;
43a2b0e7
SR
221
222#ifdef CFG_NAND_SELECT_DEVICE
223 /*
224 * Select the chip in the board/cpu specific driver
225 */
226 board_nand_select_device(nand_info[dev].priv, dev);
227#endif
228
addb2e16
BS
229 return 0;
230 }
231
232 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
3043c045
WJ
233 strncmp(cmd, "dump", 4) != 0 &&
234 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
235 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
236 strcmp(cmd, "biterr") != 0 &&
237 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
addb2e16
BS
238 goto usage;
239
240 /* the following commands operate on the current device */
241 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
242 !nand_info[nand_curr_device].name) {
243 puts("\nno devices available\n");
244 return 1;
245 }
246 nand = &nand_info[nand_curr_device];
247
248 if (strcmp(cmd, "bad") == 0) {
249 printf("\nDevice %d bad blocks:\n", nand_curr_device);
250 for (off = 0; off < nand->size; off += nand->erasesize)
251 if (nand_block_isbad(nand, off))
e870690b 252 printf(" %08lx\n", off);
addb2e16
BS
253 return 0;
254 }
255
856f0544
SR
256 /*
257 * Syntax is:
258 * 0 1 2 3 4
259 * nand erase [clean] [off size]
260 */
2255b2d2
SR
261 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
262 nand_erase_options_t opts;
856f0544 263 /* "clean" at index 2 means request to write cleanmarker */
3043c045 264 int clean = argc > 2 && !strcmp("clean", argv[2]);
856f0544 265 int o = clean ? 3 : 2;
2255b2d2
SR
266 int scrub = !strcmp(cmd, "scrub");
267
856f0544
SR
268 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
269 /* skip first two or three arguments, look for offset and size */
270 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
271 return 1;
2255b2d2
SR
272
273 memset(&opts, 0, sizeof(opts));
274 opts.offset = off;
275 opts.length = size;
276 opts.jffs2 = clean;
277 opts.quiet = quiet;
278
279 if (scrub) {
856f0544
SR
280 puts("Warning: "
281 "scrub option will erase all factory set "
282 "bad blocks!\n"
283 " "
284 "There is no reliable way to recover them.\n"
285 " "
286 "Use this command only for testing purposes "
287 "if you\n"
288 " "
289 "are sure of what you are doing!\n"
290 "\nReally scrub this NAND flash? <y/N>\n");
2255b2d2
SR
291
292 if (getc() == 'y' && getc() == '\r') {
293 opts.scrub = 1;
294 } else {
856f0544 295 puts("scrub aborted\n");
2255b2d2
SR
296 return -1;
297 }
298 }
299 ret = nand_erase_opts(nand, &opts);
addb2e16
BS
300 printf("%s\n", ret ? "ERROR" : "OK");
301
302 return ret == 0 ? 0 : 1;
303 }
304
305 if (strncmp(cmd, "dump", 4) == 0) {
306 if (argc < 3)
307 goto usage;
308
309 s = strchr(cmd, '.');
310 off = (int)simple_strtoul(argv[2], NULL, 16);
311
312 if (s != NULL && strcmp(s, ".oob") == 0)
9ad754fe 313 ret = nand_dump(nand, off, 1);
addb2e16 314 else
9ad754fe 315 ret = nand_dump(nand, off, 0);
addb2e16
BS
316
317 return ret == 0 ? 1 : 0;
318
319 }
320
addb2e16 321 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
2255b2d2
SR
322 int read;
323
addb2e16
BS
324 if (argc < 4)
325 goto usage;
2255b2d2 326
addb2e16
BS
327 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
328
2255b2d2 329 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
856f0544
SR
330 printf("\nNAND %s: ", read ? "read" : "write");
331 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
332 return 1;
4cbb651b 333
2255b2d2 334 s = strchr(cmd, '.');
984e03cd
SW
335 if (!s || !strcmp(s, ".jffs2") ||
336 !strcmp(s, ".e") || !strcmp(s, ".i")) {
dfbf617f
SW
337 if (read)
338 ret = nand_read_skip_bad(nand, off, &size,
4b070809 339 (u_char *)addr);
dfbf617f
SW
340 else
341 ret = nand_write_skip_bad(nand, off, &size,
4b070809 342 (u_char *)addr);
62d4f436 343 } else if (s != NULL && !strcmp(s, ".oob")) {
cfa460ad
WJ
344 /* out-of-band data */
345 mtd_oob_ops_t ops = {
346 .oobbuf = (u8 *)addr,
347 .ooblen = size,
348 .mode = MTD_OOB_RAW
349 };
350
62d4f436 351 if (read)
cfa460ad 352 ret = nand->read_oob(nand, off, &ops);
62d4f436 353 else
cfa460ad 354 ret = nand->write_oob(nand, off, &ops);
856f0544 355 } else {
984e03cd
SW
356 printf("Unknown nand command suffix '%s'.\n", s);
357 return 1;
2255b2d2
SR
358 }
359
addb2e16 360 printf(" %d bytes %s: %s\n", size,
2255b2d2 361 read ? "read" : "written", ret ? "ERROR" : "OK");
addb2e16
BS
362
363 return ret == 0 ? 0 : 1;
364 }
2255b2d2 365
2255b2d2
SR
366 if (strcmp(cmd, "markbad") == 0) {
367 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
368
369 int ret = nand->block_markbad(nand, addr);
370 if (ret == 0) {
371 printf("block 0x%08lx successfully marked as bad\n",
372 (ulong) addr);
373 return 0;
374 } else {
375 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
376 (ulong) addr, ret);
377 }
378 return 1;
379 }
dfbf617f 380
2255b2d2
SR
381 if (strcmp(cmd, "biterr") == 0) {
382 /* todo */
383 return 1;
384 }
385
386 if (strcmp(cmd, "lock") == 0) {
387 int tight = 0;
388 int status = 0;
389 if (argc == 3) {
390 if (!strcmp("tight", argv[2]))
391 tight = 1;
392 if (!strcmp("status", argv[2]))
393 status = 1;
394 }
3043c045
WJ
395/*
396 * ! BROKEN !
deac913e 397 *
3043c045
WJ
398 * TODO: must be implemented and tested by someone with HW
399 */
400#if 0
2255b2d2 401 if (status) {
3043c045 402 ulong block_start = 0;
2255b2d2 403 ulong off;
5e1dae5c 404 int last_status = -1;
3043c045 405
2255b2d2
SR
406 struct nand_chip *nand_chip = nand->priv;
407 /* check the WP bit */
408 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
409 printf("device is %swrite protected\n",
410 (nand_chip->read_byte(nand) & 0x80 ?
5e1dae5c 411 "NOT " : ""));
cfa460ad
WJ
412
413 for (off = 0; off < nand->size; off += nand->writesize) {
5e1dae5c
WJ
414 int s = nand_get_lock_status(nand, off);
415
416 /* print message only if status has changed
417 * or at end of chip
418 */
419 if (off == nand->size - nand->writesize
3043c045 420 || (s != last_status && off != 0)) {
5e1dae5c
WJ
421
422 printf("%08lx - %08lx: %8d pages %s%s%s\n",
3043c045
WJ
423 block_start,
424 off-1,
425 (off-block_start)/nand->writesize,
426 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
427 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
428 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
5e1dae5c
WJ
429 }
430
431 last_status = s;
2255b2d2 432 }
cfa460ad 433 } else {
5e1dae5c
WJ
434 if (!nand_lock(nand, tight)) {
435 puts("NAND flash successfully locked\n");
436 } else {
437 puts("Error locking NAND flash\n");
438 return 1;
439 }
2255b2d2 440 }
3043c045 441#endif
2255b2d2
SR
442 return 0;
443 }
444
445 if (strcmp(cmd, "unlock") == 0) {
856f0544
SR
446 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
447 return 1;
2255b2d2 448
3043c045
WJ
449/*
450 * ! BROKEN !
deac913e 451 *
3043c045
WJ
452 * TODO: must be implemented and tested by someone with HW
453 */
454#if 0
5e1dae5c
WJ
455 if (!nand_unlock(nand, off, size)) {
456 puts("NAND flash successfully unlocked\n");
457 } else {
458 puts("Error unlocking NAND flash, "
3043c045 459 "write and erase will probably fail\n");
5e1dae5c
WJ
460 return 1;
461 }
462#endif
2255b2d2
SR
463 return 0;
464 }
465
addb2e16
BS
466usage:
467 printf("Usage:\n%s\n", cmdtp->usage);
468 return 1;
469}
470
471U_BOOT_CMD(nand, 5, 1, do_nand,
4b070809
WD
472 "nand - NAND sub-system\n",
473 "info - show available NAND devices\n"
474 "nand device [dev] - show or set current device\n"
475 "nand read - addr off|partition size\n"
476 "nand write - addr off|partition size\n"
477 " read/write 'size' bytes starting at offset 'off'\n"
478 " to/from memory address 'addr', skipping bad blocks.\n"
479 "nand erase [clean] [off size] - erase 'size' bytes from\n"
480 " offset 'off' (entire device if not specified)\n"
481 "nand bad - show bad blocks\n"
482 "nand dump[.oob] off - dump page\n"
483 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
484 "nand markbad off - mark bad block at offset (UNSAFE)\n"
485 "nand biterr off - make a bit error at offset (UNSAFE)\n"
486 "nand lock [tight] [status]\n"
487 " bring nand to lock state or display locked pages\n"
488 "nand unlock [offset] [size] - unlock section\n");
addb2e16 489
856f0544 490static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
4b070809 491 ulong offset, ulong addr, char *cmd)
addb2e16 492{
addb2e16 493 int r;
10e03893 494 char *ep, *s;
4ca79f47 495 size_t cnt;
addb2e16 496 image_header_t *hdr;
09475f75 497#if defined(CONFIG_FIT)
3bab76a2 498 const void *fit_hdr = NULL;
09475f75 499#endif
10e03893
TK
500
501 s = strchr(cmd, '.');
502 if (s != NULL &&
984e03cd
SW
503 (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) {
504 printf("Unknown nand load suffix '%s'\n", s);
505 show_boot_progress(-53);
506 return 1;
507 }
addb2e16 508
856f0544 509 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
addb2e16 510
cfa460ad
WJ
511 cnt = nand->writesize;
512 r = nand_read(nand, offset, &cnt, (u_char *) addr);
addb2e16 513 if (r) {
856f0544 514 puts("** Read error\n");
fad63407 515 show_boot_progress (-56);
addb2e16
BS
516 return 1;
517 }
fad63407 518 show_boot_progress (56);
addb2e16 519
9a4daad0 520 switch (genimg_get_format ((void *)addr)) {
d5934ad7
MB
521 case IMAGE_FORMAT_LEGACY:
522 hdr = (image_header_t *)addr;
523
d5934ad7 524 show_boot_progress (57);
d5934ad7 525 image_print_contents (hdr);
addb2e16 526
d5934ad7
MB
527 cnt = image_get_image_size (hdr);
528 break;
529#if defined(CONFIG_FIT)
530 case IMAGE_FORMAT_FIT:
09475f75 531 fit_hdr = (const void *)addr;
09475f75
MB
532 puts ("Fit image detected...\n");
533
534 cnt = fit_get_size (fit_hdr);
535 break;
d5934ad7
MB
536#endif
537 default:
09475f75 538 show_boot_progress (-57);
d5934ad7 539 puts ("** Unknown image type\n");
addb2e16
BS
540 return 1;
541 }
3043c045 542 show_boot_progress (57);
addb2e16 543
984e03cd 544 /* FIXME: skip bad blocks */
cfa460ad 545 r = nand_read(nand, offset, &cnt, (u_char *) addr);
addb2e16 546 if (r) {
856f0544 547 puts("** Read error\n");
fad63407 548 show_boot_progress (-58);
addb2e16
BS
549 return 1;
550 }
fad63407 551 show_boot_progress (58);
addb2e16 552
09475f75
MB
553#if defined(CONFIG_FIT)
554 /* This cannot be done earlier, we need complete FIT image in RAM first */
3bab76a2
MB
555 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
556 if (!fit_check_format (fit_hdr)) {
557 show_boot_progress (-150);
558 puts ("** Bad FIT image format\n");
559 return 1;
560 }
561 show_boot_progress (151);
562 fit_print_contents (fit_hdr);
563 }
09475f75
MB
564#endif
565
addb2e16
BS
566 /* Loading ok, update default load address */
567
568 load_addr = addr;
569
570 /* Check if we should attempt an auto-start */
571 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
572 char *local_args[2];
573 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
574
856f0544 575 local_args[0] = cmd;
addb2e16
BS
576 local_args[1] = NULL;
577
578 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
579
580 do_bootm(cmdtp, 0, 1, local_args);
581 return 1;
582 }
583 return 0;
584}
585
856f0544
SR
586int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
587{
588 char *boot_device = NULL;
589 int idx;
590 ulong addr, offset = 0;
c76fe474 591#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
592 struct mtd_device *dev;
593 struct part_info *part;
594 u8 pnum;
595
596 if (argc >= 2) {
597 char *p = (argc == 2) ? argv[1] : argv[2];
598 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
599 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
600 if (dev->id->type != MTD_DEV_TYPE_NAND) {
601 puts("Not a NAND device\n");
602 return 1;
603 }
604 if (argc > 3)
605 goto usage;
606 if (argc == 3)
10e03893 607 addr = simple_strtoul(argv[1], NULL, 16);
856f0544
SR
608 else
609 addr = CFG_LOAD_ADDR;
610 return nand_load_image(cmdtp, &nand_info[dev->id->num],
4b070809 611 part->offset, addr, argv[0]);
856f0544
SR
612 }
613 }
614#endif
615
fad63407 616 show_boot_progress(52);
856f0544
SR
617 switch (argc) {
618 case 1:
619 addr = CFG_LOAD_ADDR;
620 boot_device = getenv("bootdevice");
621 break;
622 case 2:
623 addr = simple_strtoul(argv[1], NULL, 16);
624 boot_device = getenv("bootdevice");
625 break;
626 case 3:
627 addr = simple_strtoul(argv[1], NULL, 16);
628 boot_device = argv[2];
629 break;
630 case 4:
631 addr = simple_strtoul(argv[1], NULL, 16);
632 boot_device = argv[2];
633 offset = simple_strtoul(argv[3], NULL, 16);
634 break;
635 default:
c76fe474 636#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
637usage:
638#endif
639 printf("Usage:\n%s\n", cmdtp->usage);
fad63407 640 show_boot_progress(-53);
856f0544
SR
641 return 1;
642 }
643
fad63407 644 show_boot_progress(53);
856f0544
SR
645 if (!boot_device) {
646 puts("\n** No boot device **\n");
fad63407 647 show_boot_progress(-54);
856f0544
SR
648 return 1;
649 }
fad63407 650 show_boot_progress(54);
addb2e16 651
856f0544
SR
652 idx = simple_strtoul(boot_device, NULL, 16);
653
654 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
655 printf("\n** Device %d not available\n", idx);
fad63407 656 show_boot_progress(-55);
856f0544
SR
657 return 1;
658 }
fad63407 659 show_boot_progress(55);
856f0544
SR
660
661 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
662}
663
664U_BOOT_CMD(nboot, 4, 1, do_nandboot,
665 "nboot - boot from NAND device\n",
984e03cd 666 "[partition] | [[[loadAddr] dev] offset]\n");
addb2e16 667
c76fe474 668#endif
addb2e16 669
cc4a0cee 670#else /* CONFIG_NAND_LEGACY */
addb2e16
BS
671/*
672 *
673 * Legacy NAND support - to be phased out
674 *
675 */
dc7c9a1a
WD
676#include <command.h>
677#include <malloc.h>
678#include <asm/io.h>
a3d991bd 679#include <watchdog.h>
dc7c9a1a 680
fad63407 681#ifdef CONFIG_show_boot_progress
dc7c9a1a 682# include <status_led.h>
fad63407 683# define show_boot_progress(arg) show_boot_progress(arg)
dc7c9a1a 684#else
fad63407 685# define show_boot_progress(arg)
dc7c9a1a
WD
686#endif
687
c76fe474 688#if defined(CONFIG_CMD_NAND)
addb2e16
BS
689#include <linux/mtd/nand_legacy.h>
690#if 0
dc7c9a1a 691#include <linux/mtd/nand_ids.h>
7a8e9bed 692#include <jffs2/jffs2.h>
addb2e16 693#endif
dc7c9a1a 694
1f4bb37d
WD
695#ifdef CONFIG_OMAP1510
696void archflashwp(void *archdata, int wp);
697#endif
698
3043c045 699#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
1f4bb37d 700
3043c045
WJ
701#undef NAND_DEBUG
702#undef PSYCHO_DEBUG
7a8e9bed
WD
703
704/* ****************** WARNING *********************
705 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
706 * erase (or at least attempt to erase) blocks that are marked
707 * bad. This can be very handy if you are _sure_ that the block
708 * is OK, say because you marked a good block bad to test bad
709 * block handling and you are done testing, or if you have
710 * accidentally marked blocks bad.
711 *
712 * Erasing factory marked bad blocks is a _bad_ idea. If the
713 * erase succeeds there is no reliable way to find them again,
714 * and attempting to program or erase bad blocks can affect
715 * the data in _other_ (good) blocks.
716 */
3043c045 717#define ALLOW_ERASE_BAD_DEBUG 0
dc7c9a1a
WD
718
719#define CONFIG_MTD_NAND_ECC /* enable ECC */
1f4bb37d 720#define CONFIG_MTD_NAND_ECC_JFFS2
dc7c9a1a 721
addb2e16 722/* bits for nand_legacy_rw() `cmd'; or together as needed */
5e1dae5c
WJ
723#define NANDRW_READ 0x01
724#define NANDRW_WRITE 0x00
725#define NANDRW_JFFS2 0x02
cfa460ad 726#define NANDRW_JFFS2_SKIP 0x04
7a8e9bed 727
addb2e16
BS
728/*
729 * Imports from nand_legacy.c
730 */
731extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
732extern int curr_device;
733extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
3043c045 734 size_t len, int clean);
addb2e16 735extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
5e1dae5c 736 size_t len, size_t *retlen, u_char *buf);
addb2e16
BS
737extern void nand_print(struct nand_chip *nand);
738extern void nand_print_bad(struct nand_chip *nand);
739extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
3043c045 740 size_t len, size_t *retlen, u_char *buf);
addb2e16 741extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
5e1dae5c 742 size_t len, size_t *retlen, const u_char *buf);
dc7c9a1a 743
dc7c9a1a 744
dd520bf3 745int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
dc7c9a1a 746{
dd520bf3 747 int rcode = 0;
dc7c9a1a 748
dd520bf3
WD
749 switch (argc) {
750 case 0:
751 case 1:
752 printf ("Usage:\n%s\n", cmdtp->usage);
753 return 1;
754 case 2:
755 if (strcmp (argv[1], "info") == 0) {
756 int i;
dc7c9a1a 757
dd520bf3 758 putc ('\n');
dc7c9a1a 759
dd520bf3
WD
760 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
761 if (nand_dev_desc[i].ChipID ==
762 NAND_ChipID_UNKNOWN)
763 continue; /* list only known devices */
764 printf ("Device %d: ", i);
765 nand_print (&nand_dev_desc[i]);
766 }
767 return 0;
7a8e9bed 768
dd520bf3
WD
769 } else if (strcmp (argv[1], "device") == 0) {
770 if ((curr_device < 0)
771 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
772 puts ("\nno devices available\n");
773 return 1;
774 }
775 printf ("\nDevice %d: ", curr_device);
776 nand_print (&nand_dev_desc[curr_device]);
777 return 0;
7a8e9bed 778
dd520bf3
WD
779 } else if (strcmp (argv[1], "bad") == 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 bad blocks:\n", curr_device);
786 nand_print_bad (&nand_dev_desc[curr_device]);
787 return 0;
dc7c9a1a 788
dc7c9a1a 789 }
dd520bf3
WD
790 printf ("Usage:\n%s\n", cmdtp->usage);
791 return 1;
792 case 3:
793 if (strcmp (argv[1], "device") == 0) {
794 int dev = (int) simple_strtoul (argv[2], NULL, 10);
dc7c9a1a 795
dd520bf3
WD
796 printf ("\nDevice %d: ", dev);
797 if (dev >= CFG_MAX_NAND_DEVICE) {
798 puts ("unknown device\n");
799 return 1;
800 }
801 nand_print (&nand_dev_desc[dev]);
802 /*nand_print (dev); */
dc7c9a1a 803
dd520bf3
WD
804 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
805 return 1;
806 }
dc7c9a1a 807
dd520bf3 808 curr_device = dev;
dc7c9a1a 809
dd520bf3 810 puts ("... is now current device\n");
7a8e9bed 811
dd520bf3
WD
812 return 0;
813 } else if (strcmp (argv[1], "erase") == 0
814 && strcmp (argv[2], "clean") == 0) {
815 struct nand_chip *nand = &nand_dev_desc[curr_device];
816 ulong off = 0;
817 ulong size = nand->totlen;
818 int ret;
7a8e9bed 819
dd520bf3 820 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
7a8e9bed 821
dd520bf3 822 ret = nand_legacy_erase (nand, off, size, 1);
7a8e9bed 823
dd520bf3 824 printf ("%s\n", ret ? "ERROR" : "OK");
dc7c9a1a 825
7a8e9bed
WD
826 return ret;
827 }
dd520bf3
WD
828
829 printf ("Usage:\n%s\n", cmdtp->usage);
830 return 1;
831 default:
832 /* at least 4 args */
833
834 if (strncmp (argv[1], "read", 4) == 0 ||
835 strncmp (argv[1], "write", 5) == 0) {
5e1dae5c
WJ
836 ulong addr = simple_strtoul (argv[2], NULL, 16);
837 off_t off = simple_strtoul (argv[3], NULL, 16);
838 size_t size = simple_strtoul (argv[4], NULL, 16);
839 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
4b070809 840 NANDRW_READ : NANDRW_WRITE;
c71abba3
WD
841 size_t total;
842 int ret;
dd520bf3
WD
843 char *cmdtail = strchr (argv[1], '.');
844
845 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
846 /* read out-of-band data */
847 if (cmd & NANDRW_READ) {
848 ret = nand_read_oob (nand_dev_desc + curr_device,
4ca79f47 849 off, size, &total,
dd520bf3
WD
850 (u_char *) addr);
851 } else {
852 ret = nand_write_oob (nand_dev_desc + curr_device,
4ca79f47 853 off, size, &total,
dd520bf3
WD
854 (u_char *) addr);
855 }
856 return ret;
857 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
858 cmd |= NANDRW_JFFS2; /* skip bad blocks */
859 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
860 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
861 if (cmd & NANDRW_READ)
862 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
863 }
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
a842a6d2 869#ifdef CFG_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 }
a842a6d2 877#endif /* CFG_NAND_SKIP_BAD_DOT_I */
dd520bf3
WD
878 else if (cmdtail) {
879 printf ("Usage:\n%s\n", cmdtp->usage);
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 {
915 printf ("Usage:\n%s\n", cmdtp->usage);
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,
2255b2d2 925 "nand - legacy NAND sub-system\n",
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:
955 addr = CFG_LOAD_ADDR;
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:
972 printf ("Usage:\n%s\n", cmdtp->usage);
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
987 if ((dev >= CFG_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,
b0fce99b
WD
1074 "nboot - boot from NAND device\n",
1075 "loadAddr dev\n"
1076);
1077
c76fe474 1078#endif
dc7c9a1a 1079
cc4a0cee 1080#endif /* CONFIG_NAND_LEGACY */