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