Fixing coding style issues
[people/ms/u-boot.git] / common / cmd_nand.c
CommitLineData
dc7c9a1a
WD
1/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
384cc687
WD
6 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
dc7c9a1a
WD
9 */
10
11#include <common.h>
addb2e16
BS
12
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
cfa460ad 21#include <linux/mtd/mtd.h>
addb2e16 22
c76fe474 23#if defined(CONFIG_CMD_NAND)
addb2e16
BS
24
25#include <command.h>
26#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
addb2e16
BS
29#include <jffs2/jffs2.h>
30#include <nand.h>
31
c76fe474 32#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
33
34/* parition handling routines */
35int mtdparts_init(void);
36int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
37int find_dev_and_part(const char *id, struct mtd_device **dev,
5e1dae5c 38 u8 *part_num, struct part_info **part);
856f0544
SR
39#endif
40
addb2e16
BS
41static int nand_dump_oob(nand_info_t *nand, ulong off)
42{
43 return 0;
44}
45
46static int nand_dump(nand_info_t *nand, ulong off)
47{
48 int i;
49 u_char *buf, *p;
50
cfa460ad 51 buf = malloc(nand->writesize + nand->oobsize);
addb2e16
BS
52 if (!buf) {
53 puts("No memory for page buffer\n");
54 return 1;
55 }
cfa460ad
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;
5e1dae5c 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) {
2255b2d2 409 ulong off;
5e1dae5c
WJ
410/* ulong block_start = 0;
411 int last_status = -1;
412*/
2255b2d2
SR
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 ?
5e1dae5c 418 "NOT " : ""));
cfa460ad
WJ
419
420 for (off = 0; off < nand->size; off += nand->writesize) {
5e1dae5c
WJ
421#if 0 /* must be fixed */
422 int s = nand_get_lock_status(nand, off);
423
424 /* print message only if status has changed
425 * or at end of chip
426 */
427 if (off == nand->size - nand->writesize
428 || (s != last_status && off != 0)) {
429
430 printf("%08lx - %08lx: %8d pages %s%s%s\n",
431 block_start,
432 off-1,
433 (off-block_start)/nand->writesize,
434 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
435 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
436 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
437 }
438
439 last_status = s;
440#endif
2255b2d2 441 }
cfa460ad 442 } else {
5e1dae5c
WJ
443#if 0 /* must be fixed */
444 if (!nand_lock(nand, tight)) {
445 puts("NAND flash successfully locked\n");
446 } else {
447 puts("Error locking NAND flash\n");
448 return 1;
449 }
450#endif
2255b2d2
SR
451 }
452 return 0;
453 }
454
455 if (strcmp(cmd, "unlock") == 0) {
856f0544
SR
456 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
457 return 1;
2255b2d2 458
5e1dae5c
WJ
459#if 0 /* must be fixed */
460 if (!nand_unlock(nand, off, size)) {
461 puts("NAND flash successfully unlocked\n");
462 } else {
463 puts("Error unlocking NAND flash, "
464 "write and erase will probably fail\n");
465 return 1;
466 }
467#endif
2255b2d2
SR
468 return 0;
469 }
470
addb2e16
BS
471usage:
472 printf("Usage:\n%s\n", cmdtp->usage);
473 return 1;
474}
475
476U_BOOT_CMD(nand, 5, 1, do_nand,
cfa460ad
WJ
477 "nand - NAND sub-system\n",
478 "info - show available NAND devices\n"
479 "nand device [dev] - show or set current device\n"
480 "nand read[.jffs2] - addr off|partition size\n"
481 "nand write[.jffs2] - addr off|partition size\n"
482 " read/write 'size' bytes starting at offset 'off'\n"
483 " to/from memory address 'addr'\n"
484 "nand erase [clean] [off size] - erase 'size' bytes from\n"
485 " offset 'off' (entire device if not specified)\n"
486 "nand bad - show bad blocks\n"
487 "nand dump[.oob] off - dump page\n"
488 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
489 "nand markbad off - mark bad block at offset (UNSAFE)\n"
490 "nand biterr off - make a bit error at offset (UNSAFE)\n"
491 "nand lock [tight] [status]\n"
492 " bring nand to lock state or display locked pages\n"
493 "nand unlock [offset] [size] - unlock section\n");
addb2e16 494
856f0544 495static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
cfa460ad 496 ulong offset, ulong addr, char *cmd)
addb2e16 497{
addb2e16 498 int r;
10e03893 499 char *ep, *s;
4ca79f47 500 size_t cnt;
addb2e16 501 image_header_t *hdr;
10e03893 502 int jffs2 = 0;
09475f75 503#if defined(CONFIG_FIT)
3bab76a2 504 const void *fit_hdr = NULL;
09475f75 505#endif
10e03893
TK
506
507 s = strchr(cmd, '.');
508 if (s != NULL &&
509 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
510 jffs2 = 1;
addb2e16 511
856f0544 512 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
addb2e16 513
cfa460ad
WJ
514 cnt = nand->writesize;
515 r = nand_read(nand, offset, &cnt, (u_char *) addr);
addb2e16 516 if (r) {
856f0544 517 puts("** Read error\n");
fad63407 518 show_boot_progress (-56);
addb2e16
BS
519 return 1;
520 }
fad63407 521 show_boot_progress (56);
addb2e16 522
9a4daad0 523 switch (genimg_get_format ((void *)addr)) {
d5934ad7
MB
524 case IMAGE_FORMAT_LEGACY:
525 hdr = (image_header_t *)addr;
526
d5934ad7 527 show_boot_progress (57);
d5934ad7 528 image_print_contents (hdr);
addb2e16 529
d5934ad7
MB
530 cnt = image_get_image_size (hdr);
531 break;
532#if defined(CONFIG_FIT)
533 case IMAGE_FORMAT_FIT:
09475f75 534 fit_hdr = (const void *)addr;
09475f75
MB
535 puts ("Fit image detected...\n");
536
537 cnt = fit_get_size (fit_hdr);
538 break;
d5934ad7
MB
539#endif
540 default:
09475f75 541 show_boot_progress (-57);
d5934ad7 542 puts ("** Unknown image type\n");
addb2e16
BS
543 return 1;
544 }
addb2e16 545
cfa460ad 546 r = nand_read(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;
c76fe474 592#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
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
SR
609 else
610 addr = CFG_LOAD_ADDR;
611 return nand_load_image(cmdtp, &nand_info[dev->id->num],
cfa460ad 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:
620 addr = CFG_LOAD_ADDR;
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:
c76fe474 637#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
856f0544
SR
638usage:
639#endif
640 printf("Usage:\n%s\n", cmdtp->usage);
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
655 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
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,
666 "nboot - boot from NAND device\n",
10e03893 667 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
addb2e16 668
c76fe474 669#endif
addb2e16
BS
670
671#else /* CFG_NAND_LEGACY */
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
5e1dae5c 700#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
1f4bb37d 701
cfa460ad
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 */
cfa460ad 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 */
732extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
733extern int curr_device;
734extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
5e1dae5c 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,
5e1dae5c 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:
753 printf ("Usage:\n%s\n", cmdtp->usage);
754 return 1;
755 case 2:
756 if (strcmp (argv[1], "info") == 0) {
757 int i;
dc7c9a1a 758
dd520bf3 759 putc ('\n');
dc7c9a1a 760
dd520bf3
WD
761 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
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)
772 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
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)
782 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
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 }
dd520bf3
WD
791 printf ("Usage:\n%s\n", cmdtp->usage);
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
WD
797 printf ("\nDevice %d: ", dev);
798 if (dev >= CFG_MAX_NAND_DEVICE) {
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
WD
829
830 printf ("Usage:\n%s\n", cmdtp->usage);
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) ?
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;
858 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
859 cmd |= NANDRW_JFFS2; /* skip bad blocks */
860 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
861 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
862 if (cmd & NANDRW_READ)
863 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
864 }
7a8e9bed 865#ifdef SXNI855T
dd520bf3
WD
866 /* need ".e" same as ".j" for compatibility with older units */
867 else if (cmdtail && !strcmp (cmdtail, ".e"))
868 cmd |= NANDRW_JFFS2; /* skip bad blocks */
7a8e9bed 869#endif
a842a6d2 870#ifdef CFG_NAND_SKIP_BAD_DOT_I
dd520bf3
WD
871 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
872 /* ".i" for image -> read skips bad block (no 0xff) */
873 else if (cmdtail && !strcmp (cmdtail, ".i")) {
874 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
875 if (cmd & NANDRW_READ)
cfa460ad 876 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
dd520bf3 877 }
a842a6d2 878#endif /* CFG_NAND_SKIP_BAD_DOT_I */
dd520bf3
WD
879 else if (cmdtail) {
880 printf ("Usage:\n%s\n", cmdtp->usage);
881 return 1;
882 }
dc7c9a1a 883
9b55a253 884 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
dd520bf3 885 (cmd & NANDRW_READ) ? "read" : "write",
9b55a253 886 curr_device, off, (ulong)size);
dc7c9a1a 887
dd520bf3
WD
888 ret = nand_legacy_rw (nand_dev_desc + curr_device,
889 cmd, off, size,
4ca79f47 890 &total,
dd520bf3 891 (u_char *) addr);
dc7c9a1a 892
dd520bf3
WD
893 printf (" %d bytes %s: %s\n", total,
894 (cmd & NANDRW_READ) ? "read" : "written",
895 ret ? "ERROR" : "OK");
dc7c9a1a 896
dd520bf3
WD
897 return ret;
898 } else if (strcmp (argv[1], "erase") == 0 &&
899 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
900 int clean = argc == 5;
901 ulong off =
902 simple_strtoul (argv[2 + clean], NULL, 16);
903 ulong size =
904 simple_strtoul (argv[3 + clean], NULL, 16);
905 int ret;
dc7c9a1a 906
dd520bf3
WD
907 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
908 curr_device, off, size);
dc7c9a1a 909
dd520bf3
WD
910 ret = nand_legacy_erase (nand_dev_desc + curr_device,
911 off, size, clean);
dc7c9a1a 912
dd520bf3 913 printf ("%s\n", ret ? "ERROR" : "OK");
dc7c9a1a 914
dd520bf3
WD
915 return ret;
916 } else {
917 printf ("Usage:\n%s\n", cmdtp->usage);
918 rcode = 1;
919 }
dc7c9a1a 920
dd520bf3
WD
921 return rcode;
922 }
dc7c9a1a
WD
923}
924
0d498393 925U_BOOT_CMD(
cfa460ad 926 nand, 5, 1, do_nand,
2255b2d2 927 "nand - legacy NAND sub-system\n",
b0fce99b
WD
928 "info - show available NAND devices\n"
929 "nand device [dev] - show or set current device\n"
a3d991bd 930 "nand read[.jffs2[s]] addr off size\n"
b0fce99b 931 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
5e1dae5c 932 " at offset `off' to/from memory address `addr'\n"
b0fce99b 933 "nand erase [clean] [off size] - erase `size' bytes from\n"
5e1dae5c 934 " offset `off' (entire device if not specified)\n"
b0fce99b
WD
935 "nand bad - show bad blocks\n"
936 "nand read.oob addr off size - read out-of-band data\n"
937 "nand write.oob addr off size - read out-of-band data\n"
938);
939
dc7c9a1a
WD
940int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
941{
942 char *boot_device = NULL;
943 char *ep;
944 int dev;
945 ulong cnt;
946 ulong addr;
947 ulong offset = 0;
948 image_header_t *hdr;
949 int rcode = 0;
09475f75 950#if defined(CONFIG_FIT)
3bab76a2 951 const void *fit_hdr = NULL;
09475f75
MB
952#endif
953
fad63407 954 show_boot_progress (52);
dc7c9a1a
WD
955 switch (argc) {
956 case 1:
957 addr = CFG_LOAD_ADDR;
958 boot_device = getenv ("bootdevice");
959 break;
960 case 2:
961 addr = simple_strtoul(argv[1], NULL, 16);
962 boot_device = getenv ("bootdevice");
963 break;
964 case 3:
965 addr = simple_strtoul(argv[1], NULL, 16);
966 boot_device = argv[2];
967 break;
968 case 4:
969 addr = simple_strtoul(argv[1], NULL, 16);
970 boot_device = argv[2];
971 offset = simple_strtoul(argv[3], NULL, 16);
972 break;
973 default:
974 printf ("Usage:\n%s\n", cmdtp->usage);
fad63407 975 show_boot_progress (-53);
dc7c9a1a
WD
976 return 1;
977 }
978
fad63407 979 show_boot_progress (53);
dc7c9a1a
WD
980 if (!boot_device) {
981 puts ("\n** No boot device **\n");
fad63407 982 show_boot_progress (-54);
dc7c9a1a
WD
983 return 1;
984 }
fad63407 985 show_boot_progress (54);
dc7c9a1a
WD
986
987 dev = simple_strtoul(boot_device, &ep, 16);
988
989 if ((dev >= CFG_MAX_NAND_DEVICE) ||
cfa460ad 990 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
dc7c9a1a 991 printf ("\n** Device %d not available\n", dev);
fad63407 992 show_boot_progress (-55);
dc7c9a1a
WD
993 return 1;
994 }
fad63407 995 show_boot_progress (55);
dc7c9a1a 996
7a8e9bed 997 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
cfa460ad
WJ
998 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
999 offset);
dc7c9a1a 1000
addb2e16 1001 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
cfa460ad 1002 SECTORSIZE, NULL, (u_char *)addr)) {
dc7c9a1a 1003 printf ("** Read error on %d\n", dev);
fad63407 1004 show_boot_progress (-56);
dc7c9a1a
WD
1005 return 1;
1006 }
fad63407 1007 show_boot_progress (56);
dc7c9a1a 1008
9a4daad0 1009 switch (genimg_get_format ((void *)addr)) {
d5934ad7
MB
1010 case IMAGE_FORMAT_LEGACY:
1011 hdr = (image_header_t *)addr;
09475f75 1012 image_print_contents (hdr);
dc7c9a1a 1013
09475f75
MB
1014 cnt = image_get_image_size (hdr);
1015 cnt -= SECTORSIZE;
d5934ad7
MB
1016 break;
1017#if defined(CONFIG_FIT)
1018 case IMAGE_FORMAT_FIT:
09475f75 1019 fit_hdr = (const void *)addr;
09475f75
MB
1020 puts ("Fit image detected...\n");
1021
1022 cnt = fit_get_size (fit_hdr);
1023 break;
d5934ad7
MB
1024#endif
1025 default:
09475f75 1026 show_boot_progress (-57);
d5934ad7 1027 puts ("** Unknown image type\n");
dc7c9a1a
WD
1028 return 1;
1029 }
fad63407 1030 show_boot_progress (57);
dc7c9a1a 1031
addb2e16 1032 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
cfa460ad
WJ
1033 offset + SECTORSIZE, cnt, NULL,
1034 (u_char *)(addr+SECTORSIZE))) {
dc7c9a1a 1035 printf ("** Read error on %d\n", dev);
fad63407 1036 show_boot_progress (-58);
dc7c9a1a
WD
1037 return 1;
1038 }
fad63407 1039 show_boot_progress (58);
dc7c9a1a 1040
09475f75
MB
1041#if defined(CONFIG_FIT)
1042 /* This cannot be done earlier, we need complete FIT image in RAM first */
3bab76a2
MB
1043 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1044 if (!fit_check_format (fit_hdr)) {
1045 show_boot_progress (-150);
1046 puts ("** Bad FIT image format\n");
1047 return 1;
1048 }
1049 show_boot_progress (151);
1050 fit_print_contents (fit_hdr);
1051 }
09475f75
MB
1052#endif
1053
dc7c9a1a
WD
1054 /* Loading ok, update default load address */
1055
1056 load_addr = addr;
1057
1058 /* Check if we should attempt an auto-start */
1059 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1060 char *local_args[2];
1061 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1062
1063 local_args[0] = argv[0];
1064 local_args[1] = NULL;
1065
7a8e9bed 1066 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
dc7c9a1a
WD
1067
1068 do_bootm (cmdtp, 0, 1, local_args);
1069 rcode = 1;
1070 }
1071 return rcode;
1072}
1073
0d498393 1074U_BOOT_CMD(
cfa460ad 1075 nboot, 4, 1, do_nandboot,
b0fce99b
WD
1076 "nboot - boot from NAND device\n",
1077 "loadAddr dev\n"
1078);
1079
c76fe474 1080#endif
dc7c9a1a 1081
addb2e16 1082#endif /* CFG_NAND_LEGACY */