blob: f825234a7c839df1da2fe07bf2a0707a8667c5ba [file] [log] [blame]
wdenkdc7c9a12003-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>
wdenk384cc682005-04-03 22:35:21 +00006 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
wdenkdc7c9a12003-03-26 06:55:25 +00009 */
10
11#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010012
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
William Juulcfa460a2007-10-31 13:53:06 +010021#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010022
Jon Loeligerc76fe472007-07-08 18:02:23 -050023#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010024
25#include <command.h>
26#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010029#include <jffs2/jffs2.h>
30#include <nand.h>
31
Jon Loeligerc76fe472007-07-08 18:02:23 -050032#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +020033
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,
William Juulcfa460a2007-10-31 13:53:06 +010038 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020039#endif
40
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010041static 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
William Juulcfa460a2007-10-31 13:53:06 +010051 buf = malloc(nand->writesize + nand->oobsize);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010052 if (!buf) {
53 puts("No memory for page buffer\n");
54 return 1;
55 }
William Juulcfa460a2007-10-31 13:53:06 +010056 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
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010064 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020065 printf("Error (%d) reading page %08lx\n", i, off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010066 free(buf);
67 return 1;
68 }
Stefan Roesee8706902008-07-10 10:10:54 +020069 printf("Page %08lx dump:\n", off);
William Juulcfa460a2007-10-31 13:53:06 +010070 i = nand->writesize >> 4; p = buf;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010071 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010072 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]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010076 p += 16;
77 }
78 puts("OOB:\n");
79 i = nand->oobsize >> 3;
80 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010081 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]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010083 p += 8;
84 }
85 free(buf);
86
87 return 0;
88}
89
90/* ------------------------------------------------------------------------- */
91
Stefan Roese856f0542006-10-28 15:55:52 +020092static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010093{
Stefan Roese856f0542006-10-28 15:55:52 +020094 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010095
Stefan Roese856f0542006-10-28 15:55:52 +020096 *num = simple_strtoul(p, &endptr, 16);
97 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
98}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010099
Stefan Roese856f0542006-10-28 15:55:52 +0200100static int
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200101arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +0200102{
103 int idx = nand_curr_device;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500104#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200105 struct mtd_device *dev;
106 struct part_info *part;
107 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100108
Stefan Roese856f0542006-10-28 15:55:52 +0200109 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) {
Wolfgang Denk42ffcec2008-04-30 17:46:26 +0200118 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200119 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;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100130 }
Stefan Roese856f0542006-10-28 15:55:52 +0200131 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100132#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100133
Stefan Roese856f0542006-10-28 15:55:52 +0200134 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;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100141 }
142
Stefan Roese856f0542006-10-28 15:55:52 +0200143 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200144 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200145 printf("'%s' is not a number\n", argv[1]);
146 return -1;
147 }
148 } else {
149 *size = nand->size - *off;
150 }
151
Jon Loeligerc76fe472007-07-08 18:02:23 -0500152#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200153out:
154#endif
155 printf("device %d ", idx);
156 if (*size == nand->size)
157 puts("whole chip\n");
158 else
Stefan Roesee8706902008-07-10 10:10:54 +0200159 printf("offset 0x%lx, size 0x%x\n", *off, *size);
Stefan Roese856f0542006-10-28 15:55:52 +0200160 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100161}
162
163int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
164{
William Juulcfa460a2007-10-31 13:53:06 +0100165 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200166 ulong addr, off;
167 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100168 char *cmd, *s;
169 nand_info_t *nand;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200170#ifdef CFG_NAND_QUIET
171 int quiet = CFG_NAND_QUIET;
172#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200173 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200174#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200175 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100176
177 /* at least two arguments please */
178 if (argc < 2)
179 goto usage;
180
Stefan Roese2255b2d2006-10-10 12:36:02 +0200181 if (quiet_str)
182 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
183
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100184 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)
Stefan Roesee8706902008-07-10 10:10:54 +0200191 printf("Device %d: %s, sector size %u KiB\n",
William Juulcfa460a2007-10-31 13:53:06 +0100192 i, nand_info[i].name,
193 nand_info[i].erasesize >> 10);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100194 }
195 return 0;
196 }
197
198 if (strcmp(cmd, "device") == 0) {
199
200 if (argc < 3) {
201 if ((nand_curr_device < 0) ||
William Juulcfa460a2007-10-31 13:53:06 +0100202 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100203 puts("\nno devices available\n");
204 else
205 printf("\nDevice %d: %s\n", nand_curr_device,
William Juulcfa460a2007-10-31 13:53:06 +0100206 nand_info[nand_curr_device].name);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100207 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;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200217
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
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100225 return 0;
226 }
227
228 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juulcfa460a2007-10-31 13:53:06 +0100229 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 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100234 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))
Stefan Roesee8706902008-07-10 10:10:54 +0200248 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100249 return 0;
250 }
251
Stefan Roese856f0542006-10-28 15:55:52 +0200252 /*
253 * Syntax is:
254 * 0 1 2 3 4
255 * nand erase [clean] [off size]
256 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200257 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
258 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200259 /* "clean" at index 2 means request to write cleanmarker */
William Juulcfa460a2007-10-31 13:53:06 +0100260 int clean = !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200261 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200262 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100263
Stefan Roese856f0542006-10-28 15:55:52 +0200264 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;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200268
269 memset(&opts, 0, sizeof(opts));
William Juulcfa460a2007-10-31 13:53:06 +0100270
Stefan Roese2255b2d2006-10-10 12:36:02 +0200271 opts.offset = off;
272 opts.length = size;
273 opts.jffs2 = clean;
274 opts.quiet = quiet;
275
276 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200277 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");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200288
289 if (getc() == 'y' && getc() == '\r') {
290 opts.scrub = 1;
291 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200292 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200293 return -1;
294 }
295 }
296 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100297 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) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200320 int read;
321
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100322 if (argc < 4)
323 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200324
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100325 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
326
Stefan Roese2255b2d2006-10-10 12:36:02 +0200327 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200328 printf("\nNAND %s: ", read ? "read" : "write");
329 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
330 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100331
Stefan Roese2255b2d2006-10-10 12:36:02 +0200332 s = strchr(cmd, '.');
333 if (s != NULL &&
William Juulcfa460a2007-10-31 13:53:06 +0100334 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200335 if (read) {
336 /* read */
337 nand_read_options_t opts;
338 memset(&opts, 0, sizeof(opts));
William Juulcfa460a2007-10-31 13:53:06 +0100339 opts.buffer = (u_char*) addr;
340 opts.length = size;
341 opts.offset = off;
342 opts.quiet = quiet;
343// ret = nand_read_opts(nand, &opts);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200344 } else {
345 /* write */
William Juulcfa460a2007-10-31 13:53:06 +0100346 mtd_oob_ops_t opts;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200347 memset(&opts, 0, sizeof(opts));
William Juulcfa460a2007-10-31 13:53:06 +0100348 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);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200353 }
Harald Welte62d4f432007-12-19 14:12:53 +0100354 } else if (s != NULL && !strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100355 /* out-of-band data */
356 mtd_oob_ops_t ops = {
357 .oobbuf = (u8 *)addr,
358 .ooblen = size,
359 .mode = MTD_OOB_RAW
360 };
361
Harald Welte62d4f432007-12-19 14:12:53 +0100362 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100363 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100364 else
William Juulcfa460a2007-10-31 13:53:06 +0100365 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200366 } 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);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200371 }
372
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100373 printf(" %d bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200374 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100375
376 return ret == 0 ? 0 : 1;
377 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200378
Stefan Roese2255b2d2006-10-10 12:36:02 +0200379 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) {
William Juulcfa460a2007-10-31 13:53:06 +0100409// ulong block_start = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200410 ulong off;
William Juulcfa460a2007-10-31 13:53:06 +0100411// int last_status = -1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200412
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 ?
William Juulcfa460a2007-10-31 13:53:06 +0100418 "NOT " : "" ) );
Stefan Roese2255b2d2006-10-10 12:36:02 +0200419
William Juulcfa460a2007-10-31 13:53:06 +0100420 for (off = 0; off < nand->size; off += nand->writesize) {
421// int s = nand_get_lock_status(nand, off);
422//
423// /* print message only if status has changed
424// * or at end of chip
425// */
426// if (off == nand->size - nand->writesize
427// || (s != last_status && off != 0)) {
428//
429// printf("%08lx - %08lx: %8d pages %s%s%s\n",
430// block_start,
431// off-1,
432// (off-block_start)/nand->writesize,
433// ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
434// ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
435// ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
436// }
437//
438// last_status = s;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200439 }
William Juulcfa460a2007-10-31 13:53:06 +0100440 } else {
441// if (!nand_lock(nand, tight)) {
442// puts("NAND flash successfully locked\n");
443// } else {
444// puts("Error locking NAND flash\n");
445// return 1;
446// }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200447 }
448 return 0;
449 }
450
451 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200452 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
453 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200454
William Juulcfa460a2007-10-31 13:53:06 +0100455// if (!nand_unlock(nand, off, size)) {
456// puts("NAND flash successfully unlocked\n");
457// } else {
458// puts("Error unlocking NAND flash, "
459// "write and erase will probably fail\n");
460// return 1;
461// }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200462 return 0;
463 }
464
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100465usage:
466 printf("Usage:\n%s\n", cmdtp->usage);
467 return 1;
468}
469
470U_BOOT_CMD(nand, 5, 1, do_nand,
William Juulcfa460a2007-10-31 13:53:06 +0100471 "nand - NAND sub-system\n",
472 "info - show available NAND devices\n"
473 "nand device [dev] - show or set current device\n"
474 "nand read[.jffs2] - addr off|partition size\n"
475 "nand write[.jffs2] - addr off|partition size\n"
476 " read/write 'size' bytes starting at offset 'off'\n"
477 " to/from memory address 'addr'\n"
478 "nand erase [clean] [off size] - erase 'size' bytes from\n"
479 " offset 'off' (entire device if not specified)\n"
480 "nand bad - show bad blocks\n"
481 "nand dump[.oob] off - dump page\n"
482 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
483 "nand markbad off - mark bad block at offset (UNSAFE)\n"
484 "nand biterr off - make a bit error at offset (UNSAFE)\n"
485 "nand lock [tight] [status]\n"
486 " bring nand to lock state or display locked pages\n"
487 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100488
Stefan Roese856f0542006-10-28 15:55:52 +0200489static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
William Juulcfa460a2007-10-31 13:53:06 +0100490 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200491{
492 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200493 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200494 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200495 image_header_t *hdr;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200496 int jffs2 = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100497#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200498 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100499#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200500
501 s = strchr(cmd, '.');
502 if (s != NULL &&
503 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
504 jffs2 = 1;
Stefan Roese856f0542006-10-28 15:55:52 +0200505
506 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
507
William Juulcfa460a2007-10-31 13:53:06 +0100508 cnt = nand->writesize;
509 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200510 if (r) {
511 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200512 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200513 return 1;
514 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200515 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200516
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100517 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100518 case IMAGE_FORMAT_LEGACY:
519 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200520
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100521 show_boot_progress (57);
Marian Balakowiczd5934ad2008-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 Balakowicz09475f72008-03-12 10:33:01 +0100528 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100529 puts ("Fit image detected...\n");
530
531 cnt = fit_get_size (fit_hdr);
532 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100533#endif
534 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100535 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100536 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200537 return 1;
538 }
539
William Juulcfa460a2007-10-31 13:53:06 +0100540 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200541 if (r) {
542 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200543 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200544 return 1;
545 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200546 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200547
Marian Balakowicz09475f72008-03-12 10:33:01 +0100548#if defined(CONFIG_FIT)
549 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200550 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
551 if (!fit_check_format (fit_hdr)) {
552 show_boot_progress (-150);
553 puts ("** Bad FIT image format\n");
554 return 1;
555 }
556 show_boot_progress (151);
557 fit_print_contents (fit_hdr);
558 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100559#endif
560
Stefan Roese856f0542006-10-28 15:55:52 +0200561 /* Loading ok, update default load address */
562
563 load_addr = addr;
564
565 /* Check if we should attempt an auto-start */
566 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
567 char *local_args[2];
568 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
569
570 local_args[0] = cmd;
571 local_args[1] = NULL;
572
573 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
574
575 do_bootm(cmdtp, 0, 1, local_args);
576 return 1;
577 }
578 return 0;
579}
580
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100581int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
582{
583 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200584 int idx;
585 ulong addr, offset = 0;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500586#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200587 struct mtd_device *dev;
588 struct part_info *part;
589 u8 pnum;
590
591 if (argc >= 2) {
592 char *p = (argc == 2) ? argv[1] : argv[2];
593 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
594 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
595 if (dev->id->type != MTD_DEV_TYPE_NAND) {
596 puts("Not a NAND device\n");
597 return 1;
598 }
599 if (argc > 3)
600 goto usage;
601 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200602 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200603 else
604 addr = CFG_LOAD_ADDR;
605 return nand_load_image(cmdtp, &nand_info[dev->id->num],
William Juulcfa460a2007-10-31 13:53:06 +0100606 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200607 }
608 }
609#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100610
Heiko Schocherfad63402007-07-13 09:54:17 +0200611 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100612 switch (argc) {
613 case 1:
614 addr = CFG_LOAD_ADDR;
615 boot_device = getenv("bootdevice");
616 break;
617 case 2:
618 addr = simple_strtoul(argv[1], NULL, 16);
619 boot_device = getenv("bootdevice");
620 break;
621 case 3:
622 addr = simple_strtoul(argv[1], NULL, 16);
623 boot_device = argv[2];
624 break;
625 case 4:
626 addr = simple_strtoul(argv[1], NULL, 16);
627 boot_device = argv[2];
628 offset = simple_strtoul(argv[3], NULL, 16);
629 break;
630 default:
Jon Loeligerc76fe472007-07-08 18:02:23 -0500631#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200632usage:
633#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100634 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200635 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100636 return 1;
637 }
638
Heiko Schocherfad63402007-07-13 09:54:17 +0200639 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100640 if (!boot_device) {
641 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200642 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100643 return 1;
644 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200645 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100646
Stefan Roese856f0542006-10-28 15:55:52 +0200647 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100648
Stefan Roese856f0542006-10-28 15:55:52 +0200649 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
650 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200651 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100652 return 1;
653 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200654 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100655
Stefan Roese856f0542006-10-28 15:55:52 +0200656 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100657}
658
659U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Stefan Roese856f0542006-10-28 15:55:52 +0200660 "nboot - boot from NAND device\n",
Thomas Knobloch10e03892007-07-06 14:58:39 +0200661 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100662
Jon Loeligerc76fe472007-07-08 18:02:23 -0500663#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100664
665#else /* CFG_NAND_LEGACY */
666/*
667 *
668 * Legacy NAND support - to be phased out
669 *
670 */
wdenkdc7c9a12003-03-26 06:55:25 +0000671#include <command.h>
672#include <malloc.h>
673#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000674#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000675
Heiko Schocherfad63402007-07-13 09:54:17 +0200676#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000677# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200678# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000679#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200680# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000681#endif
682
Jon Loeligerc76fe472007-07-08 18:02:23 -0500683#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100684#include <linux/mtd/nand_legacy.h>
685#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000686#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000687#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100688#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000689
wdenk1f4bb372003-07-27 00:21:01 +0000690#ifdef CONFIG_OMAP1510
691void archflashwp(void *archdata, int wp);
692#endif
693
694#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
695
William Juulcfa460a2007-10-31 13:53:06 +0100696#undef NAND_DEBUG
697#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000698
699/* ****************** WARNING *********************
700 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
701 * erase (or at least attempt to erase) blocks that are marked
702 * bad. This can be very handy if you are _sure_ that the block
703 * is OK, say because you marked a good block bad to test bad
704 * block handling and you are done testing, or if you have
705 * accidentally marked blocks bad.
706 *
707 * Erasing factory marked bad blocks is a _bad_ idea. If the
708 * erase succeeds there is no reliable way to find them again,
709 * and attempting to program or erase bad blocks can affect
710 * the data in _other_ (good) blocks.
711 */
William Juulcfa460a2007-10-31 13:53:06 +0100712#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000713
714#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000715#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000716
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100717/* bits for nand_legacy_rw() `cmd'; or together as needed */
William Juulcfa460a2007-10-31 13:53:06 +0100718#define NANDRW_READ 0x01
719#define NANDRW_WRITE 0x00
720#define NANDRW_JFFS2 0x02
721#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000722
wdenkdc7c9a12003-03-26 06:55:25 +0000723/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100724 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000725 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100726extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
727extern int curr_device;
728extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
William Juulcfa460a2007-10-31 13:53:06 +0100729 size_t len, int clean);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100730extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
William Juulcfa460a2007-10-31 13:53:06 +0100731 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100732extern void nand_print(struct nand_chip *nand);
733extern void nand_print_bad(struct nand_chip *nand);
734extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
William Juulcfa460a2007-10-31 13:53:06 +0100735 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100736extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
William Juulcfa460a2007-10-31 13:53:06 +0100737 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000738
wdenkdc7c9a12003-03-26 06:55:25 +0000739
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100740int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000741{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100742 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000743
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100744 switch (argc) {
745 case 0:
746 case 1:
747 printf ("Usage:\n%s\n", cmdtp->usage);
748 return 1;
749 case 2:
750 if (strcmp (argv[1], "info") == 0) {
751 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000752
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100753 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000754
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100755 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
756 if (nand_dev_desc[i].ChipID ==
757 NAND_ChipID_UNKNOWN)
758 continue; /* list only known devices */
759 printf ("Device %d: ", i);
760 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000761 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100762 return 0;
763
764 } else if (strcmp (argv[1], "device") == 0) {
765 if ((curr_device < 0)
766 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
767 puts ("\nno devices available\n");
768 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000769 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100770 printf ("\nDevice %d: ", curr_device);
771 nand_print (&nand_dev_desc[curr_device]);
772 return 0;
773
774 } else if (strcmp (argv[1], "bad") == 0) {
775 if ((curr_device < 0)
776 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
777 puts ("\nno devices available\n");
778 return 1;
779 }
780 printf ("\nDevice %d bad blocks:\n", curr_device);
781 nand_print_bad (&nand_dev_desc[curr_device]);
782 return 0;
783
784 }
785 printf ("Usage:\n%s\n", cmdtp->usage);
786 return 1;
787 case 3:
788 if (strcmp (argv[1], "device") == 0) {
789 int dev = (int) simple_strtoul (argv[2], NULL, 10);
790
791 printf ("\nDevice %d: ", dev);
792 if (dev >= CFG_MAX_NAND_DEVICE) {
793 puts ("unknown device\n");
794 return 1;
795 }
796 nand_print (&nand_dev_desc[dev]);
797 /*nand_print (dev); */
798
799 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
800 return 1;
801 }
802
803 curr_device = dev;
804
805 puts ("... is now current device\n");
806
807 return 0;
808 } else if (strcmp (argv[1], "erase") == 0
809 && strcmp (argv[2], "clean") == 0) {
810 struct nand_chip *nand = &nand_dev_desc[curr_device];
811 ulong off = 0;
812 ulong size = nand->totlen;
813 int ret;
814
815 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
816
817 ret = nand_legacy_erase (nand, off, size, 1);
818
819 printf ("%s\n", ret ? "ERROR" : "OK");
820
wdenk7a8e9bed2003-05-31 18:35:21 +0000821 return ret;
822 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100823
824 printf ("Usage:\n%s\n", cmdtp->usage);
825 return 1;
826 default:
827 /* at least 4 args */
828
829 if (strncmp (argv[1], "read", 4) == 0 ||
830 strncmp (argv[1], "write", 5) == 0) {
Wolfgang Denkc71abba2008-04-28 14:55:12 +0200831 ulong addr = simple_strtoul (argv[2], NULL, 16);
832 off_t off = simple_strtoul (argv[3], NULL, 16);
833 size_t size = simple_strtoul (argv[4], NULL, 16);
834 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
835 NANDRW_READ : NANDRW_WRITE;
836 size_t total;
837 int ret;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100838 char *cmdtail = strchr (argv[1], '.');
839
840 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
841 /* read out-of-band data */
842 if (cmd & NANDRW_READ) {
843 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200844 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100845 (u_char *) addr);
846 } else {
847 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200848 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100849 (u_char *) addr);
850 }
851 return ret;
852 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
853 cmd |= NANDRW_JFFS2; /* skip bad blocks */
854 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
855 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
856 if (cmd & NANDRW_READ)
857 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
858 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000859#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100860 /* need ".e" same as ".j" for compatibility with older units */
861 else if (cmdtail && !strcmp (cmdtail, ".e"))
862 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000863#endif
stroesea842a6d2004-12-16 17:45:46 +0000864#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100865 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
866 /* ".i" for image -> read skips bad block (no 0xff) */
867 else if (cmdtail && !strcmp (cmdtail, ".i")) {
868 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
869 if (cmd & NANDRW_READ)
William Juulcfa460a2007-10-31 13:53:06 +0100870 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100871 }
stroesea842a6d2004-12-16 17:45:46 +0000872#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100873 else if (cmdtail) {
874 printf ("Usage:\n%s\n", cmdtp->usage);
875 return 1;
876 }
877
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200878 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100879 (cmd & NANDRW_READ) ? "read" : "write",
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200880 curr_device, off, (ulong)size);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100881
882 ret = nand_legacy_rw (nand_dev_desc + curr_device,
883 cmd, off, size,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200884 &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100885 (u_char *) addr);
886
887 printf (" %d bytes %s: %s\n", total,
888 (cmd & NANDRW_READ) ? "read" : "written",
889 ret ? "ERROR" : "OK");
890
891 return ret;
892 } else if (strcmp (argv[1], "erase") == 0 &&
893 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
894 int clean = argc == 5;
895 ulong off =
896 simple_strtoul (argv[2 + clean], NULL, 16);
897 ulong size =
898 simple_strtoul (argv[3 + clean], NULL, 16);
899 int ret;
900
901 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
902 curr_device, off, size);
903
904 ret = nand_legacy_erase (nand_dev_desc + curr_device,
905 off, size, clean);
906
907 printf ("%s\n", ret ? "ERROR" : "OK");
908
909 return ret;
910 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000911 printf ("Usage:\n%s\n", cmdtp->usage);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100912 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000913 }
wdenkdc7c9a12003-03-26 06:55:25 +0000914
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100915 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000916 }
wdenkdc7c9a12003-03-26 06:55:25 +0000917}
918
wdenk0d498392003-07-01 21:06:45 +0000919U_BOOT_CMD(
William Juulcfa460a2007-10-31 13:53:06 +0100920 nand, 5, 1, do_nand,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200921 "nand - legacy NAND sub-system\n",
wdenkb0fce992003-06-29 21:03:46 +0000922 "info - show available NAND devices\n"
923 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000924 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000925 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
926 " at offset `off' to/from memory address `addr'\n"
927 "nand erase [clean] [off size] - erase `size' bytes from\n"
928 " offset `off' (entire device if not specified)\n"
929 "nand bad - show bad blocks\n"
930 "nand read.oob addr off size - read out-of-band data\n"
931 "nand write.oob addr off size - read out-of-band data\n"
932);
933
wdenkdc7c9a12003-03-26 06:55:25 +0000934int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
935{
936 char *boot_device = NULL;
937 char *ep;
938 int dev;
939 ulong cnt;
940 ulong addr;
941 ulong offset = 0;
942 image_header_t *hdr;
943 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100944#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200945 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100946#endif
947
Heiko Schocherfad63402007-07-13 09:54:17 +0200948 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000949 switch (argc) {
950 case 1:
951 addr = CFG_LOAD_ADDR;
952 boot_device = getenv ("bootdevice");
953 break;
954 case 2:
955 addr = simple_strtoul(argv[1], NULL, 16);
956 boot_device = getenv ("bootdevice");
957 break;
958 case 3:
959 addr = simple_strtoul(argv[1], NULL, 16);
960 boot_device = argv[2];
961 break;
962 case 4:
963 addr = simple_strtoul(argv[1], NULL, 16);
964 boot_device = argv[2];
965 offset = simple_strtoul(argv[3], NULL, 16);
966 break;
967 default:
968 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200969 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000970 return 1;
971 }
972
Heiko Schocherfad63402007-07-13 09:54:17 +0200973 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000974 if (!boot_device) {
975 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200976 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000977 return 1;
978 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200979 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +0000980
981 dev = simple_strtoul(boot_device, &ep, 16);
982
983 if ((dev >= CFG_MAX_NAND_DEVICE) ||
William Juulcfa460a2007-10-31 13:53:06 +0100984 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
wdenkdc7c9a12003-03-26 06:55:25 +0000985 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200986 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +0000987 return 1;
988 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200989 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +0000990
wdenk7a8e9bed2003-05-31 18:35:21 +0000991 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
William Juulcfa460a2007-10-31 13:53:06 +0100992 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
993 offset);
wdenkdc7c9a12003-03-26 06:55:25 +0000994
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100995 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
William Juulcfa460a2007-10-31 13:53:06 +0100996 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +0000997 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200998 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +0000999 return 1;
1000 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001001 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001002
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001003 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001004 case IMAGE_FORMAT_LEGACY:
1005 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001006 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001007
Marian Balakowicz09475f72008-03-12 10:33:01 +01001008 cnt = image_get_image_size (hdr);
1009 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001010 break;
1011#if defined(CONFIG_FIT)
1012 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001013 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001014 puts ("Fit image detected...\n");
1015
1016 cnt = fit_get_size (fit_hdr);
1017 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001018#endif
1019 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001020 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001021 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001022 return 1;
1023 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001024 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001025
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001026 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
William Juulcfa460a2007-10-31 13:53:06 +01001027 offset + SECTORSIZE, cnt, NULL,
1028 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001029 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001030 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001031 return 1;
1032 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001033 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001034
Marian Balakowicz09475f72008-03-12 10:33:01 +01001035#if defined(CONFIG_FIT)
1036 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +02001037 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1038 if (!fit_check_format (fit_hdr)) {
1039 show_boot_progress (-150);
1040 puts ("** Bad FIT image format\n");
1041 return 1;
1042 }
1043 show_boot_progress (151);
1044 fit_print_contents (fit_hdr);
1045 }
Marian Balakowicz09475f72008-03-12 10:33:01 +01001046#endif
1047
wdenkdc7c9a12003-03-26 06:55:25 +00001048 /* Loading ok, update default load address */
1049
1050 load_addr = addr;
1051
1052 /* Check if we should attempt an auto-start */
1053 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1054 char *local_args[2];
1055 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1056
1057 local_args[0] = argv[0];
1058 local_args[1] = NULL;
1059
wdenk7a8e9bed2003-05-31 18:35:21 +00001060 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001061
1062 do_bootm (cmdtp, 0, 1, local_args);
1063 rcode = 1;
1064 }
1065 return rcode;
1066}
1067
wdenk0d498392003-07-01 21:06:45 +00001068U_BOOT_CMD(
William Juulcfa460a2007-10-31 13:53:06 +01001069 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +00001070 "nboot - boot from NAND device\n",
1071 "loadAddr dev\n"
1072);
1073
Jon Loeligerc76fe472007-07-08 18:02:23 -05001074#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001075
1076#endif /* CFG_NAND_LEGACY */