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