blob: 37198d21e8c6e1638f112a63205884317fba5af4 [file] [log] [blame]
wdenkc8434db2003-03-26 06:55:25 +00001/*
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>
wdenk6d3c6d12005-04-03 22:35:21 +00006 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
wdenkc8434db2003-03-26 06:55:25 +00009 */
10
11#include <common.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010012
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
21
Jon Loeligerd76b5c12007-07-08 18:02:23 -050022#if defined(CONFIG_CMD_NAND)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010023
24#include <command.h>
25#include <watchdog.h>
26#include <malloc.h>
27#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010028#include <jffs2/jffs2.h>
29#include <nand.h>
30
Jon Loeligerd76b5c12007-07-08 18:02:23 -050031#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +020032
33/* parition handling routines */
34int mtdparts_init(void);
35int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36int find_dev_and_part(const char *id, struct mtd_device **dev,
37 u8 *part_num, struct part_info **part);
38#endif
39
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010040static int nand_dump_oob(nand_info_t *nand, ulong off)
41{
42 return 0;
43}
44
45static int nand_dump(nand_info_t *nand, ulong off)
46{
47 int i;
48 u_char *buf, *p;
49
50 buf = malloc(nand->oobblock + nand->oobsize);
51 if (!buf) {
52 puts("No memory for page buffer\n");
53 return 1;
54 }
55 off &= ~(nand->oobblock - 1);
56 i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
57 if (i < 0) {
58 printf("Error (%d) reading page %08x\n", i, off);
59 free(buf);
60 return 1;
61 }
62 printf("Page %08x dump:\n", off);
63 i = nand->oobblock >> 4; p = buf;
64 while (i--) {
65 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
66 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
67 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
68 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
69 p += 16;
70 }
71 puts("OOB:\n");
72 i = nand->oobsize >> 3;
73 while (i--) {
74 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
75 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
76 p += 8;
77 }
78 free(buf);
79
80 return 0;
81}
82
83/* ------------------------------------------------------------------------- */
84
Stefan Roese198b23e2006-10-28 15:55:52 +020085static inline int str2long(char *p, ulong *num)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010086{
Stefan Roese198b23e2006-10-28 15:55:52 +020087 char *endptr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010088
Stefan Roese198b23e2006-10-28 15:55:52 +020089 *num = simple_strtoul(p, &endptr, 16);
90 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
91}
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010092
Stefan Roese198b23e2006-10-28 15:55:52 +020093static int
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +020094arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese198b23e2006-10-28 15:55:52 +020095{
96 int idx = nand_curr_device;
Jon Loeligerd76b5c12007-07-08 18:02:23 -050097#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +020098 struct mtd_device *dev;
99 struct part_info *part;
100 u8 pnum;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100101
Stefan Roese198b23e2006-10-28 15:55:52 +0200102 if (argc >= 1 && !(str2long(argv[0], off))) {
103 if ((mtdparts_init() == 0) &&
104 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
105 if (dev->id->type != MTD_DEV_TYPE_NAND) {
106 puts("not a NAND device\n");
107 return -1;
108 }
109 *off = part->offset;
110 if (argc >= 2) {
Wolfgang Denk7f794542008-04-30 17:46:26 +0200111 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200112 printf("'%s' is not a number\n", argv[1]);
113 return -1;
114 }
115 if (*size > part->size)
116 *size = part->size;
117 } else {
118 *size = part->size;
119 }
120 idx = dev->id->num;
121 *nand = nand_info[idx];
122 goto out;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100123 }
Stefan Roese198b23e2006-10-28 15:55:52 +0200124 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100125#endif
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100126
Stefan Roese198b23e2006-10-28 15:55:52 +0200127 if (argc >= 1) {
128 if (!(str2long(argv[0], off))) {
129 printf("'%s' is not a number\n", argv[0]);
130 return -1;
131 }
132 } else {
133 *off = 0;
134 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100135
Stefan Roese198b23e2006-10-28 15:55:52 +0200136 if (argc >= 2) {
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200137 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200138 printf("'%s' is not a number\n", argv[1]);
139 return -1;
140 }
141 } else {
142 *size = nand->size - *off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100143 }
144
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500145#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +0200146out:
147#endif
148 printf("device %d ", idx);
149 if (*size == nand->size)
150 puts("whole chip\n");
151 else
152 printf("offset 0x%x, size 0x%x\n", *off, *size);
153 return 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100154}
155
156int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
157{
158 int i, dev, ret;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200159 ulong addr, off;
160 size_t size;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100161 char *cmd, *s;
162 nand_info_t *nand;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200163#ifdef CFG_NAND_QUIET
164 int quiet = CFG_NAND_QUIET;
165#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200166 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200167#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200168 const char *quiet_str = getenv("quiet");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100169
170 /* at least two arguments please */
171 if (argc < 2)
172 goto usage;
173
Stefan Roesed351b2b2006-10-10 12:36:02 +0200174 if (quiet_str)
175 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
176
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100177 cmd = argv[1];
178
179 if (strcmp(cmd, "info") == 0) {
180
181 putc('\n');
182 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
183 if (nand_info[i].name)
184 printf("Device %d: %s, sector size %lu KiB\n",
185 i, nand_info[i].name,
186 nand_info[i].erasesize >> 10);
187 }
188 return 0;
189 }
190
191 if (strcmp(cmd, "device") == 0) {
192
193 if (argc < 3) {
194 if ((nand_curr_device < 0) ||
195 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
196 puts("\nno devices available\n");
197 else
198 printf("\nDevice %d: %s\n", nand_curr_device,
199 nand_info[nand_curr_device].name);
200 return 0;
201 }
202 dev = (int)simple_strtoul(argv[2], NULL, 10);
203 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
204 puts("No such device\n");
205 return 1;
206 }
207 printf("Device %d: %s", dev, nand_info[dev].name);
208 puts("... is now current device\n");
209 nand_curr_device = dev;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200210
211#ifdef CFG_NAND_SELECT_DEVICE
212 /*
213 * Select the chip in the board/cpu specific driver
214 */
215 board_nand_select_device(nand_info[dev].priv, dev);
216#endif
217
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100218 return 0;
219 }
220
221 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
222 strncmp(cmd, "dump", 4) != 0 &&
Stefan Roesed351b2b2006-10-10 12:36:02 +0200223 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
224 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
225 strcmp(cmd, "biterr") != 0 &&
226 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100227 goto usage;
228
229 /* the following commands operate on the current device */
230 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
231 !nand_info[nand_curr_device].name) {
232 puts("\nno devices available\n");
233 return 1;
234 }
235 nand = &nand_info[nand_curr_device];
236
237 if (strcmp(cmd, "bad") == 0) {
238 printf("\nDevice %d bad blocks:\n", nand_curr_device);
239 for (off = 0; off < nand->size; off += nand->erasesize)
240 if (nand_block_isbad(nand, off))
241 printf(" %08x\n", off);
242 return 0;
243 }
244
Stefan Roese198b23e2006-10-28 15:55:52 +0200245 /*
246 * Syntax is:
247 * 0 1 2 3 4
248 * nand erase [clean] [off size]
249 */
Stefan Roesed351b2b2006-10-10 12:36:02 +0200250 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
251 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200252 /* "clean" at index 2 means request to write cleanmarker */
253 int clean = argc > 2 && !strcmp("clean", argv[2]);
254 int o = clean ? 3 : 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200255 int scrub = !strcmp(cmd, "scrub");
256
Stefan Roese198b23e2006-10-28 15:55:52 +0200257 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
258 /* skip first two or three arguments, look for offset and size */
259 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
260 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200261
262 memset(&opts, 0, sizeof(opts));
263 opts.offset = off;
264 opts.length = size;
265 opts.jffs2 = clean;
266 opts.quiet = quiet;
267
268 if (scrub) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200269 puts("Warning: "
270 "scrub option will erase all factory set "
271 "bad blocks!\n"
272 " "
273 "There is no reliable way to recover them.\n"
274 " "
275 "Use this command only for testing purposes "
276 "if you\n"
277 " "
278 "are sure of what you are doing!\n"
279 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200280
281 if (getc() == 'y' && getc() == '\r') {
282 opts.scrub = 1;
283 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200284 puts("scrub aborted\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200285 return -1;
286 }
287 }
288 ret = nand_erase_opts(nand, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100289 printf("%s\n", ret ? "ERROR" : "OK");
290
291 return ret == 0 ? 0 : 1;
292 }
293
294 if (strncmp(cmd, "dump", 4) == 0) {
295 if (argc < 3)
296 goto usage;
297
298 s = strchr(cmd, '.');
299 off = (int)simple_strtoul(argv[2], NULL, 16);
300
301 if (s != NULL && strcmp(s, ".oob") == 0)
302 ret = nand_dump_oob(nand, off);
303 else
304 ret = nand_dump(nand, off);
305
306 return ret == 0 ? 1 : 0;
307
308 }
309
310 /* read write */
311 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200312 int read;
313
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100314 if (argc < 4)
315 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200316
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100317 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
318
Stefan Roesed351b2b2006-10-10 12:36:02 +0200319 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200320 printf("\nNAND %s: ", read ? "read" : "write");
321 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
322 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200323
324 s = strchr(cmd, '.');
325 if (s != NULL &&
326 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
327 if (read) {
328 /* read */
329 nand_read_options_t opts;
330 memset(&opts, 0, sizeof(opts));
331 opts.buffer = (u_char*) addr;
332 opts.length = size;
333 opts.offset = off;
334 opts.quiet = quiet;
335 ret = nand_read_opts(nand, &opts);
336 } else {
337 /* write */
338 nand_write_options_t opts;
339 memset(&opts, 0, sizeof(opts));
340 opts.buffer = (u_char*) addr;
341 opts.length = size;
342 opts.offset = off;
343 /* opts.forcejffs2 = 1; */
344 opts.pad = 1;
345 opts.blockalign = 1;
346 opts.quiet = quiet;
347 ret = nand_write_opts(nand, &opts);
348 }
Harald Welte5a395102007-12-19 14:12:53 +0100349 } else if (s != NULL && !strcmp(s, ".oob")) {
350 /* read out-of-band data */
351 if (read)
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200352 ret = nand->read_oob(nand, off, size, &size,
Harald Welte5a395102007-12-19 14:12:53 +0100353 (u_char *) addr);
354 else
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200355 ret = nand->write_oob(nand, off, size, &size,
Harald Welte5a395102007-12-19 14:12:53 +0100356 (u_char *) addr);
Stefan Roese198b23e2006-10-28 15:55:52 +0200357 } else {
358 if (read)
359 ret = nand_read(nand, off, &size, (u_char *)addr);
360 else
361 ret = nand_write(nand, off, &size, (u_char *)addr);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200362 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100363
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100364 printf(" %d bytes %s: %s\n", size,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200365 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100366
367 return ret == 0 ? 0 : 1;
368 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200369
Stefan Roesed351b2b2006-10-10 12:36:02 +0200370 if (strcmp(cmd, "markbad") == 0) {
371 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
372
373 int ret = nand->block_markbad(nand, addr);
374 if (ret == 0) {
375 printf("block 0x%08lx successfully marked as bad\n",
376 (ulong) addr);
377 return 0;
378 } else {
379 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
380 (ulong) addr, ret);
381 }
382 return 1;
383 }
384 if (strcmp(cmd, "biterr") == 0) {
385 /* todo */
386 return 1;
387 }
388
389 if (strcmp(cmd, "lock") == 0) {
390 int tight = 0;
391 int status = 0;
392 if (argc == 3) {
393 if (!strcmp("tight", argv[2]))
394 tight = 1;
395 if (!strcmp("status", argv[2]))
396 status = 1;
397 }
398
399 if (status) {
400 ulong block_start = 0;
401 ulong off;
402 int last_status = -1;
403
404 struct nand_chip *nand_chip = nand->priv;
405 /* check the WP bit */
406 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
407 printf("device is %swrite protected\n",
408 (nand_chip->read_byte(nand) & 0x80 ?
409 "NOT " : "" ) );
410
411 for (off = 0; off < nand->size; off += nand->oobblock) {
412 int s = nand_get_lock_status(nand, off);
413
414 /* print message only if status has changed
415 * or at end of chip
416 */
417 if (off == nand->size - nand->oobblock
418 || (s != last_status && off != 0)) {
419
420 printf("%08x - %08x: %8d pages %s%s%s\n",
421 block_start,
422 off-1,
423 (off-block_start)/nand->oobblock,
424 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
425 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
426 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
427 }
428
429 last_status = s;
430 }
431 } else {
432 if (!nand_lock(nand, tight)) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200433 puts("NAND flash successfully locked\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200434 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200435 puts("Error locking NAND flash\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200436 return 1;
437 }
438 }
439 return 0;
440 }
441
442 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200443 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
444 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200445
446 if (!nand_unlock(nand, off, size)) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200447 puts("NAND flash successfully unlocked\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200448 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200449 puts("Error unlocking NAND flash, "
450 "write and erase will probably fail\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200451 return 1;
452 }
453 return 0;
454 }
455
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100456usage:
457 printf("Usage:\n%s\n", cmdtp->usage);
458 return 1;
459}
460
461U_BOOT_CMD(nand, 5, 1, do_nand,
462 "nand - NAND sub-system\n",
463 "info - show available NAND devices\n"
464 "nand device [dev] - show or set current device\n"
Stefan Roese198b23e2006-10-28 15:55:52 +0200465 "nand read[.jffs2] - addr off|partition size\n"
Matthias Fuchs82714b92007-09-11 17:04:00 +0200466 "nand write[.jffs2] - addr off|partition size - read/write `size' bytes starting\n"
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100467 " at offset `off' to/from memory address `addr'\n"
468 "nand erase [clean] [off size] - erase `size' bytes from\n"
469 " offset `off' (entire device if not specified)\n"
470 "nand bad - show bad blocks\n"
471 "nand dump[.oob] off - dump page\n"
472 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
473 "nand markbad off - mark bad block at offset (UNSAFE)\n"
Stefan Roesed351b2b2006-10-10 12:36:02 +0200474 "nand biterr off - make a bit error at offset (UNSAFE)\n"
475 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
476 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100477
Stefan Roese198b23e2006-10-28 15:55:52 +0200478static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
479 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100480{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100481 int r;
Thomas Knobloche84b7772007-07-06 14:58:39 +0200482 char *ep, *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200483 size_t cnt;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100484 image_header_t *hdr;
Thomas Knobloche84b7772007-07-06 14:58:39 +0200485 int jffs2 = 0;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100486#if defined(CONFIG_FIT)
487 const void *fit_hdr;
488#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200489
490 s = strchr(cmd, '.');
491 if (s != NULL &&
492 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
493 jffs2 = 1;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100494
Stefan Roese198b23e2006-10-28 15:55:52 +0200495 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100496
497 cnt = nand->oobblock;
Thomas Knobloche84b7772007-07-06 14:58:39 +0200498 if (jffs2) {
499 nand_read_options_t opts;
500 memset(&opts, 0, sizeof(opts));
501 opts.buffer = (u_char*) addr;
502 opts.length = cnt;
503 opts.offset = offset;
504 opts.quiet = 1;
505 r = nand_read_opts(nand, &opts);
506 } else {
507 r = nand_read(nand, offset, &cnt, (u_char *) addr);
508 }
509
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100510 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200511 puts("** Read error\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200512 show_boot_progress (-56);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100513 return 1;
514 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200515 show_boot_progress (56);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100516
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100517 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100518 case IMAGE_FORMAT_LEGACY:
519 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100520
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100521 show_boot_progress (57);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100522 image_print_contents (hdr);
523
524 cnt = image_get_image_size (hdr);
525 break;
526#if defined(CONFIG_FIT)
527 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100528 fit_hdr = (const void *)addr;
529 if (!fit_check_format (fit_hdr)) {
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100530 show_boot_progress (-150);
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100531 puts ("** Bad FIT image format\n");
532 return 1;
533 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100534 show_boot_progress (151);
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100535 puts ("Fit image detected...\n");
536
537 cnt = fit_get_size (fit_hdr);
538 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100539#endif
540 default:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100541 show_boot_progress (-57);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100542 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100543 return 1;
544 }
545
Thomas Knobloche84b7772007-07-06 14:58:39 +0200546 if (jffs2) {
547 nand_read_options_t opts;
548 memset(&opts, 0, sizeof(opts));
549 opts.buffer = (u_char*) addr;
550 opts.length = cnt;
551 opts.offset = offset;
552 opts.quiet = 1;
553 r = nand_read_opts(nand, &opts);
554 } else {
555 r = nand_read(nand, offset, &cnt, (u_char *) addr);
556 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100557
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100558 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200559 puts("** Read error\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200560 show_boot_progress (-58);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100561 return 1;
562 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200563 show_boot_progress (58);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100564
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100565#if defined(CONFIG_FIT)
566 /* This cannot be done earlier, we need complete FIT image in RAM first */
567 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
568 fit_print_contents ((const void *)addr);
569#endif
570
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100571 /* Loading ok, update default load address */
572
573 load_addr = addr;
574
575 /* Check if we should attempt an auto-start */
576 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
577 char *local_args[2];
578 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
579
Stefan Roese198b23e2006-10-28 15:55:52 +0200580 local_args[0] = cmd;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100581 local_args[1] = NULL;
582
583 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
584
585 do_bootm(cmdtp, 0, 1, local_args);
586 return 1;
587 }
588 return 0;
589}
590
Stefan Roese198b23e2006-10-28 15:55:52 +0200591int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
592{
593 char *boot_device = NULL;
594 int idx;
595 ulong addr, offset = 0;
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500596#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +0200597 struct mtd_device *dev;
598 struct part_info *part;
599 u8 pnum;
600
601 if (argc >= 2) {
602 char *p = (argc == 2) ? argv[1] : argv[2];
603 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
604 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
605 if (dev->id->type != MTD_DEV_TYPE_NAND) {
606 puts("Not a NAND device\n");
607 return 1;
608 }
609 if (argc > 3)
610 goto usage;
611 if (argc == 3)
Thomas Knobloche84b7772007-07-06 14:58:39 +0200612 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200613 else
614 addr = CFG_LOAD_ADDR;
615 return nand_load_image(cmdtp, &nand_info[dev->id->num],
616 part->offset, addr, argv[0]);
617 }
618 }
619#endif
620
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200621 show_boot_progress(52);
Stefan Roese198b23e2006-10-28 15:55:52 +0200622 switch (argc) {
623 case 1:
624 addr = CFG_LOAD_ADDR;
625 boot_device = getenv("bootdevice");
626 break;
627 case 2:
628 addr = simple_strtoul(argv[1], NULL, 16);
629 boot_device = getenv("bootdevice");
630 break;
631 case 3:
632 addr = simple_strtoul(argv[1], NULL, 16);
633 boot_device = argv[2];
634 break;
635 case 4:
636 addr = simple_strtoul(argv[1], NULL, 16);
637 boot_device = argv[2];
638 offset = simple_strtoul(argv[3], NULL, 16);
639 break;
640 default:
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500641#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +0200642usage:
643#endif
644 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200645 show_boot_progress(-53);
Stefan Roese198b23e2006-10-28 15:55:52 +0200646 return 1;
647 }
648
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200649 show_boot_progress(53);
Stefan Roese198b23e2006-10-28 15:55:52 +0200650 if (!boot_device) {
651 puts("\n** No boot device **\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200652 show_boot_progress(-54);
Stefan Roese198b23e2006-10-28 15:55:52 +0200653 return 1;
654 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200655 show_boot_progress(54);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100656
Stefan Roese198b23e2006-10-28 15:55:52 +0200657 idx = simple_strtoul(boot_device, NULL, 16);
658
659 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
660 printf("\n** Device %d not available\n", idx);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200661 show_boot_progress(-55);
Stefan Roese198b23e2006-10-28 15:55:52 +0200662 return 1;
663 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200664 show_boot_progress(55);
Stefan Roese198b23e2006-10-28 15:55:52 +0200665
666 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
667}
668
669U_BOOT_CMD(nboot, 4, 1, do_nandboot,
670 "nboot - boot from NAND device\n",
Thomas Knobloche84b7772007-07-06 14:58:39 +0200671 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100672
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500673#endif
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100674
675#else /* CFG_NAND_LEGACY */
676/*
677 *
678 * Legacy NAND support - to be phased out
679 *
680 */
wdenkc8434db2003-03-26 06:55:25 +0000681#include <command.h>
682#include <malloc.h>
683#include <asm/io.h>
wdenk145d2c12004-04-15 21:48:45 +0000684#include <watchdog.h>
wdenkc8434db2003-03-26 06:55:25 +0000685
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200686#ifdef CONFIG_show_boot_progress
wdenkc8434db2003-03-26 06:55:25 +0000687# include <status_led.h>
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200688# define show_boot_progress(arg) show_boot_progress(arg)
wdenkc8434db2003-03-26 06:55:25 +0000689#else
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200690# define show_boot_progress(arg)
wdenkc8434db2003-03-26 06:55:25 +0000691#endif
692
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500693#if defined(CONFIG_CMD_NAND)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100694#include <linux/mtd/nand_legacy.h>
695#if 0
wdenkc8434db2003-03-26 06:55:25 +0000696#include <linux/mtd/nand_ids.h>
wdenkabda5ca2003-05-31 18:35:21 +0000697#include <jffs2/jffs2.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100698#endif
wdenkc8434db2003-03-26 06:55:25 +0000699
wdenke58b0dc2003-07-27 00:21:01 +0000700#ifdef CONFIG_OMAP1510
701void archflashwp(void *archdata, int wp);
702#endif
703
704#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
705
wdenk934c4f82003-09-11 19:48:06 +0000706#undef NAND_DEBUG
wdenkc8434db2003-03-26 06:55:25 +0000707#undef PSYCHO_DEBUG
wdenkabda5ca2003-05-31 18:35:21 +0000708
709/* ****************** WARNING *********************
710 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
711 * erase (or at least attempt to erase) blocks that are marked
712 * bad. This can be very handy if you are _sure_ that the block
713 * is OK, say because you marked a good block bad to test bad
714 * block handling and you are done testing, or if you have
715 * accidentally marked blocks bad.
716 *
717 * Erasing factory marked bad blocks is a _bad_ idea. If the
718 * erase succeeds there is no reliable way to find them again,
719 * and attempting to program or erase bad blocks can affect
720 * the data in _other_ (good) blocks.
721 */
722#define ALLOW_ERASE_BAD_DEBUG 0
wdenkc8434db2003-03-26 06:55:25 +0000723
724#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenke58b0dc2003-07-27 00:21:01 +0000725#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkc8434db2003-03-26 06:55:25 +0000726
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100727/* bits for nand_legacy_rw() `cmd'; or together as needed */
wdenkabda5ca2003-05-31 18:35:21 +0000728#define NANDRW_READ 0x01
729#define NANDRW_WRITE 0x00
730#define NANDRW_JFFS2 0x02
wdenk145d2c12004-04-15 21:48:45 +0000731#define NANDRW_JFFS2_SKIP 0x04
wdenkabda5ca2003-05-31 18:35:21 +0000732
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100733/*
734 * Imports from nand_legacy.c
735 */
736extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
737extern int curr_device;
738extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
739 size_t len, int clean);
740extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
741 size_t len, size_t *retlen, u_char *buf);
742extern void nand_print(struct nand_chip *nand);
743extern void nand_print_bad(struct nand_chip *nand);
744extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
745 size_t len, size_t *retlen, u_char *buf);
746extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
747 size_t len, size_t *retlen, const u_char *buf);
wdenkc8434db2003-03-26 06:55:25 +0000748
wdenkc8434db2003-03-26 06:55:25 +0000749
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100750int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkc8434db2003-03-26 06:55:25 +0000751{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100752 int rcode = 0;
wdenkc8434db2003-03-26 06:55:25 +0000753
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100754 switch (argc) {
755 case 0:
756 case 1:
757 printf ("Usage:\n%s\n", cmdtp->usage);
758 return 1;
759 case 2:
760 if (strcmp (argv[1], "info") == 0) {
761 int i;
wdenkc8434db2003-03-26 06:55:25 +0000762
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100763 putc ('\n');
wdenkc8434db2003-03-26 06:55:25 +0000764
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100765 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
766 if (nand_dev_desc[i].ChipID ==
767 NAND_ChipID_UNKNOWN)
768 continue; /* list only known devices */
769 printf ("Device %d: ", i);
770 nand_print (&nand_dev_desc[i]);
771 }
772 return 0;
wdenkabda5ca2003-05-31 18:35:21 +0000773
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100774 } else if (strcmp (argv[1], "device") == 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: ", curr_device);
781 nand_print (&nand_dev_desc[curr_device]);
782 return 0;
wdenkabda5ca2003-05-31 18:35:21 +0000783
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100784 } else if (strcmp (argv[1], "bad") == 0) {
785 if ((curr_device < 0)
786 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
787 puts ("\nno devices available\n");
788 return 1;
789 }
790 printf ("\nDevice %d bad blocks:\n", curr_device);
791 nand_print_bad (&nand_dev_desc[curr_device]);
792 return 0;
wdenkc8434db2003-03-26 06:55:25 +0000793
wdenkc8434db2003-03-26 06:55:25 +0000794 }
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100795 printf ("Usage:\n%s\n", cmdtp->usage);
796 return 1;
797 case 3:
798 if (strcmp (argv[1], "device") == 0) {
799 int dev = (int) simple_strtoul (argv[2], NULL, 10);
wdenkc8434db2003-03-26 06:55:25 +0000800
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100801 printf ("\nDevice %d: ", dev);
802 if (dev >= CFG_MAX_NAND_DEVICE) {
803 puts ("unknown device\n");
804 return 1;
805 }
806 nand_print (&nand_dev_desc[dev]);
807 /*nand_print (dev); */
wdenkc8434db2003-03-26 06:55:25 +0000808
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100809 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
810 return 1;
811 }
wdenkc8434db2003-03-26 06:55:25 +0000812
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100813 curr_device = dev;
wdenkc8434db2003-03-26 06:55:25 +0000814
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100815 puts ("... is now current device\n");
wdenkabda5ca2003-05-31 18:35:21 +0000816
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100817 return 0;
818 } else if (strcmp (argv[1], "erase") == 0
819 && strcmp (argv[2], "clean") == 0) {
820 struct nand_chip *nand = &nand_dev_desc[curr_device];
821 ulong off = 0;
822 ulong size = nand->totlen;
823 int ret;
wdenkabda5ca2003-05-31 18:35:21 +0000824
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100825 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
wdenkabda5ca2003-05-31 18:35:21 +0000826
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100827 ret = nand_legacy_erase (nand, off, size, 1);
wdenkabda5ca2003-05-31 18:35:21 +0000828
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100829 printf ("%s\n", ret ? "ERROR" : "OK");
wdenkc8434db2003-03-26 06:55:25 +0000830
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100831 return ret;
832 }
wdenkc8434db2003-03-26 06:55:25 +0000833
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100834 printf ("Usage:\n%s\n", cmdtp->usage);
835 return 1;
836 default:
837 /* at least 4 args */
838
839 if (strncmp (argv[1], "read", 4) == 0 ||
840 strncmp (argv[1], "write", 5) == 0) {
Wolfgang Denk885e86f2008-04-28 14:55:12 +0200841 ulong addr = simple_strtoul (argv[2], NULL, 16);
842 off_t off = simple_strtoul (argv[3], NULL, 16);
843 size_t size = simple_strtoul (argv[4], NULL, 16);
844 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
845 NANDRW_READ : NANDRW_WRITE;
846 size_t total;
847 int ret;
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100848 char *cmdtail = strchr (argv[1], '.');
wdenkabda5ca2003-05-31 18:35:21 +0000849
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100850 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
851 /* read out-of-band data */
852 if (cmd & NANDRW_READ) {
853 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200854 off, size, &total,
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100855 (u_char *) addr);
856 } else {
857 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200858 off, size, &total,
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100859 (u_char *) addr);
860 }
861 return ret;
862 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
863 cmd |= NANDRW_JFFS2; /* skip bad blocks */
864 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
865 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
866 if (cmd & NANDRW_READ)
867 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
wdenkabda5ca2003-05-31 18:35:21 +0000868 }
wdenkabda5ca2003-05-31 18:35:21 +0000869#ifdef SXNI855T
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100870 /* need ".e" same as ".j" for compatibility with older units */
871 else if (cmdtail && !strcmp (cmdtail, ".e"))
872 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenkabda5ca2003-05-31 18:35:21 +0000873#endif
stroese317cbe62004-12-16 17:45:46 +0000874#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100875 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
876 /* ".i" for image -> read skips bad block (no 0xff) */
877 else if (cmdtail && !strcmp (cmdtail, ".i")) {
878 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
879 if (cmd & NANDRW_READ)
880 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
881 }
stroese317cbe62004-12-16 17:45:46 +0000882#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100883 else if (cmdtail) {
884 printf ("Usage:\n%s\n", cmdtp->usage);
885 return 1;
886 }
wdenkc8434db2003-03-26 06:55:25 +0000887
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100888 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
889 (cmd & NANDRW_READ) ? "read" : "write",
890 curr_device, off, size);
wdenkc8434db2003-03-26 06:55:25 +0000891
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100892 ret = nand_legacy_rw (nand_dev_desc + curr_device,
893 cmd, off, size,
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200894 &total,
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100895 (u_char *) addr);
wdenkc8434db2003-03-26 06:55:25 +0000896
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100897 printf (" %d bytes %s: %s\n", total,
898 (cmd & NANDRW_READ) ? "read" : "written",
899 ret ? "ERROR" : "OK");
wdenkc8434db2003-03-26 06:55:25 +0000900
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100901 return ret;
902 } else if (strcmp (argv[1], "erase") == 0 &&
903 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
904 int clean = argc == 5;
905 ulong off =
906 simple_strtoul (argv[2 + clean], NULL, 16);
907 ulong size =
908 simple_strtoul (argv[3 + clean], NULL, 16);
909 int ret;
wdenkc8434db2003-03-26 06:55:25 +0000910
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100911 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
912 curr_device, off, size);
wdenkc8434db2003-03-26 06:55:25 +0000913
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100914 ret = nand_legacy_erase (nand_dev_desc + curr_device,
915 off, size, clean);
wdenkc8434db2003-03-26 06:55:25 +0000916
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100917 printf ("%s\n", ret ? "ERROR" : "OK");
wdenkc8434db2003-03-26 06:55:25 +0000918
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100919 return ret;
920 } else {
921 printf ("Usage:\n%s\n", cmdtp->usage);
922 rcode = 1;
923 }
wdenkc8434db2003-03-26 06:55:25 +0000924
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100925 return rcode;
926 }
wdenkc8434db2003-03-26 06:55:25 +0000927}
928
wdenkf287a242003-07-01 21:06:45 +0000929U_BOOT_CMD(
930 nand, 5, 1, do_nand,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200931 "nand - legacy NAND sub-system\n",
wdenkf12e3962003-06-29 21:03:46 +0000932 "info - show available NAND devices\n"
933 "nand device [dev] - show or set current device\n"
wdenk145d2c12004-04-15 21:48:45 +0000934 "nand read[.jffs2[s]] addr off size\n"
wdenkf12e3962003-06-29 21:03:46 +0000935 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
936 " at offset `off' to/from memory address `addr'\n"
937 "nand erase [clean] [off size] - erase `size' bytes from\n"
938 " offset `off' (entire device if not specified)\n"
939 "nand bad - show bad blocks\n"
940 "nand read.oob addr off size - read out-of-band data\n"
941 "nand write.oob addr off size - read out-of-band data\n"
942);
943
wdenkc8434db2003-03-26 06:55:25 +0000944int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
945{
946 char *boot_device = NULL;
947 char *ep;
948 int dev;
949 ulong cnt;
950 ulong addr;
951 ulong offset = 0;
952 image_header_t *hdr;
953 int rcode = 0;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100954#if defined(CONFIG_FIT)
955 const void *fit_hdr;
956#endif
957
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200958 show_boot_progress (52);
wdenkc8434db2003-03-26 06:55:25 +0000959 switch (argc) {
960 case 1:
961 addr = CFG_LOAD_ADDR;
962 boot_device = getenv ("bootdevice");
963 break;
964 case 2:
965 addr = simple_strtoul(argv[1], NULL, 16);
966 boot_device = getenv ("bootdevice");
967 break;
968 case 3:
969 addr = simple_strtoul(argv[1], NULL, 16);
970 boot_device = argv[2];
971 break;
972 case 4:
973 addr = simple_strtoul(argv[1], NULL, 16);
974 boot_device = argv[2];
975 offset = simple_strtoul(argv[3], NULL, 16);
976 break;
977 default:
978 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200979 show_boot_progress (-53);
wdenkc8434db2003-03-26 06:55:25 +0000980 return 1;
981 }
982
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200983 show_boot_progress (53);
wdenkc8434db2003-03-26 06:55:25 +0000984 if (!boot_device) {
985 puts ("\n** No boot device **\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200986 show_boot_progress (-54);
wdenkc8434db2003-03-26 06:55:25 +0000987 return 1;
988 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200989 show_boot_progress (54);
wdenkc8434db2003-03-26 06:55:25 +0000990
991 dev = simple_strtoul(boot_device, &ep, 16);
992
993 if ((dev >= CFG_MAX_NAND_DEVICE) ||
994 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
995 printf ("\n** Device %d not available\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200996 show_boot_progress (-55);
wdenkc8434db2003-03-26 06:55:25 +0000997 return 1;
998 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200999 show_boot_progress (55);
wdenkc8434db2003-03-26 06:55:25 +00001000
wdenkabda5ca2003-05-31 18:35:21 +00001001 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
wdenkc8434db2003-03-26 06:55:25 +00001002 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1003 offset);
1004
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001005 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1006 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkc8434db2003-03-26 06:55:25 +00001007 printf ("** Read error on %d\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001008 show_boot_progress (-56);
wdenkc8434db2003-03-26 06:55:25 +00001009 return 1;
1010 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001011 show_boot_progress (56);
wdenkc8434db2003-03-26 06:55:25 +00001012
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001013 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001014 case IMAGE_FORMAT_LEGACY:
1015 hdr = (image_header_t *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001016 image_print_contents (hdr);
wdenkc8434db2003-03-26 06:55:25 +00001017
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001018 cnt = image_get_image_size (hdr);
1019 cnt -= SECTORSIZE;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001020 break;
1021#if defined(CONFIG_FIT)
1022 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001023 fit_hdr = (const void *)addr;
1024 if (!fit_check_format (fit_hdr)) {
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01001025 show_boot_progress (-150);
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001026 puts ("** Bad FIT image format\n");
1027 return 1;
1028 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01001029 show_boot_progress (151);
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001030 puts ("Fit image detected...\n");
1031
1032 cnt = fit_get_size (fit_hdr);
1033 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001034#endif
1035 default:
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001036 show_boot_progress (-57);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001037 puts ("** Unknown image type\n");
wdenkc8434db2003-03-26 06:55:25 +00001038 return 1;
1039 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001040 show_boot_progress (57);
wdenkc8434db2003-03-26 06:55:25 +00001041
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001042 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1043 offset + SECTORSIZE, cnt, NULL,
1044 (u_char *)(addr+SECTORSIZE))) {
wdenkc8434db2003-03-26 06:55:25 +00001045 printf ("** Read error on %d\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001046 show_boot_progress (-58);
wdenkc8434db2003-03-26 06:55:25 +00001047 return 1;
1048 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001049 show_boot_progress (58);
wdenkc8434db2003-03-26 06:55:25 +00001050
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001051#if defined(CONFIG_FIT)
1052 /* This cannot be done earlier, we need complete FIT image in RAM first */
1053 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
1054 fit_print_contents ((const void *)addr);
1055#endif
1056
wdenkc8434db2003-03-26 06:55:25 +00001057 /* Loading ok, update default load address */
1058
1059 load_addr = addr;
1060
1061 /* Check if we should attempt an auto-start */
1062 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1063 char *local_args[2];
1064 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1065
1066 local_args[0] = argv[0];
1067 local_args[1] = NULL;
1068
wdenkabda5ca2003-05-31 18:35:21 +00001069 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkc8434db2003-03-26 06:55:25 +00001070
1071 do_bootm (cmdtp, 0, 1, local_args);
1072 rcode = 1;
1073 }
1074 return rcode;
1075}
1076
wdenkf287a242003-07-01 21:06:45 +00001077U_BOOT_CMD(
1078 nboot, 4, 1, do_nandboot,
wdenkf12e3962003-06-29 21:03:46 +00001079 "nboot - boot from NAND device\n",
1080 "loadAddr dev\n"
1081);
1082
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001083#endif
wdenkc8434db2003-03-26 06:55:25 +00001084
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001085#endif /* CFG_NAND_LEGACY */