blob: 04b3200e57dd4790f4af1c27981af00aae3cf8fe [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
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +020014#ifndef CONFIG_NAND_LEGACY
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010015/*
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
Stefan Roese68d7d652009-03-19 13:30:36 +010032#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
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,
Wolfgang Denk4b070802008-08-14 14:41:06 +020038 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020039#endif
40
William Juul9ad754f2007-12-14 16:33:45 +010041static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010042{
43 int i;
William Juul9ad754f2007-12-14 16:33:45 +010044 u_char *datbuf, *oobbuf, *p;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010045
William Juul9ad754f2007-12-14 16:33:45 +010046 datbuf = malloc(nand->writesize + nand->oobsize);
47 oobbuf = malloc(nand->oobsize);
48 if (!datbuf || !oobbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010049 puts("No memory for page buffer\n");
50 return 1;
51 }
William Juulcfa460a2007-10-31 13:53:06 +010052 off &= ~(nand->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010053 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010054 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);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010062 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020063 printf("Error (%d) reading page %08lx\n", i, off);
William Juul9ad754f2007-12-14 16:33:45 +010064 free(datbuf);
65 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010066 return 1;
67 }
Stefan Roesee8706902008-07-10 10:10:54 +020068 printf("Page %08lx dump:\n", off);
William Juul9ad754f2007-12-14 16:33:45 +010069 i = nand->writesize >> 4;
70 p = datbuf;
Wolfgang Denk4b070802008-08-14 14:41:06 +020071
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010072 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010073 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],
Scott Wooddfbf6172008-06-12 13:20:16 -050077 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
78 p[15]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010079 p += 16;
80 }
81 puts("OOB:\n");
82 i = nand->oobsize >> 3;
83 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010084 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
85 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010086 p += 8;
87 }
William Juul9ad754f2007-12-14 16:33:45 +010088 free(datbuf);
89 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010090
91 return 0;
92}
93
94/* ------------------------------------------------------------------------- */
95
Stefan Roese856f0542006-10-28 15:55:52 +020096static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010097{
Stefan Roese856f0542006-10-28 15:55:52 +020098 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010099
Stefan Roese856f0542006-10-28 15:55:52 +0200100 *num = simple_strtoul(p, &endptr, 16);
101 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
102}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100103
Stefan Roese856f0542006-10-28 15:55:52 +0200104static int
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200105arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +0200106{
107 int idx = nand_curr_device;
Stefan Roese68d7d652009-03-19 13:30:36 +0100108#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200109 struct mtd_device *dev;
110 struct part_info *part;
111 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100112
Stefan Roese856f0542006-10-28 15:55:52 +0200113 if (argc >= 1 && !(str2long(argv[0], off))) {
114 if ((mtdparts_init() == 0) &&
115 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
116 if (dev->id->type != MTD_DEV_TYPE_NAND) {
117 puts("not a NAND device\n");
118 return -1;
119 }
120 *off = part->offset;
121 if (argc >= 2) {
Wolfgang Denk42ffcec2008-04-30 17:46:26 +0200122 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200123 printf("'%s' is not a number\n", argv[1]);
124 return -1;
125 }
126 if (*size > part->size)
127 *size = part->size;
128 } else {
129 *size = part->size;
130 }
131 idx = dev->id->num;
132 *nand = nand_info[idx];
133 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100134 }
Stefan Roese856f0542006-10-28 15:55:52 +0200135 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100136#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100137
Stefan Roese856f0542006-10-28 15:55:52 +0200138 if (argc >= 1) {
139 if (!(str2long(argv[0], off))) {
140 printf("'%s' is not a number\n", argv[0]);
141 return -1;
142 }
143 } else {
144 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100145 }
146
Stefan Roese856f0542006-10-28 15:55:52 +0200147 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200148 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200149 printf("'%s' is not a number\n", argv[1]);
150 return -1;
151 }
152 } else {
153 *size = nand->size - *off;
154 }
155
Stefan Roese68d7d652009-03-19 13:30:36 +0100156#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200157out:
158#endif
159 printf("device %d ", idx);
160 if (*size == nand->size)
161 puts("whole chip\n");
162 else
Mike Frysingerd4bade82009-01-18 19:46:06 -0500163 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
Stefan Roese856f0542006-10-28 15:55:52 +0200164 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100165}
166
Nishanth Menon50657c22008-12-13 09:43:06 -0600167#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
168static void print_status(ulong start, ulong end, ulong erasesize, int status)
169{
170 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
171 start,
172 end - 1,
173 (end - start) / erasesize,
174 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
175 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
176 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
177}
178
179static void do_nand_status(nand_info_t *nand)
180{
181 ulong block_start = 0;
182 ulong off;
183 int last_status = -1;
184
185 struct nand_chip *nand_chip = nand->priv;
186 /* check the WP bit */
187 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
188 printf("device is %swrite protected\n",
189 (nand_chip->read_byte(nand) & 0x80 ?
190 "NOT " : ""));
191
192 for (off = 0; off < nand->size; off += nand->erasesize) {
193 int s = nand_get_lock_status(nand, off);
194
195 /* print message only if status has changed */
196 if (s != last_status && off != 0) {
197 print_status(block_start, off, nand->erasesize,
198 last_status);
199 block_start = off;
200 }
201 last_status = s;
202 }
203 /* Print the last block info */
204 print_status(block_start, off, nand->erasesize, last_status);
205}
206#endif
207
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100208int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
209{
William Juulcfa460a2007-10-31 13:53:06 +0100210 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200211 ulong addr, off;
212 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100213 char *cmd, *s;
214 nand_info_t *nand;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200215#ifdef CONFIG_SYS_NAND_QUIET
216 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200217#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200218 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200219#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200220 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100221
222 /* at least two arguments please */
223 if (argc < 2)
224 goto usage;
225
Stefan Roese2255b2d2006-10-10 12:36:02 +0200226 if (quiet_str)
227 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
228
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100229 cmd = argv[1];
230
231 if (strcmp(cmd, "info") == 0) {
232
233 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200234 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100235 if (nand_info[i].name)
Stefan Roesee8706902008-07-10 10:10:54 +0200236 printf("Device %d: %s, sector size %u KiB\n",
Wolfgang Denk4b070802008-08-14 14:41:06 +0200237 i, nand_info[i].name,
William Juulcfa460a2007-10-31 13:53:06 +0100238 nand_info[i].erasesize >> 10);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100239 }
240 return 0;
241 }
242
243 if (strcmp(cmd, "device") == 0) {
244
245 if (argc < 3) {
246 if ((nand_curr_device < 0) ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200247 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100248 puts("\nno devices available\n");
249 else
250 printf("\nDevice %d: %s\n", nand_curr_device,
William Juulcfa460a2007-10-31 13:53:06 +0100251 nand_info[nand_curr_device].name);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100252 return 0;
253 }
254 dev = (int)simple_strtoul(argv[2], NULL, 10);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200255 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100256 puts("No such device\n");
257 return 1;
258 }
259 printf("Device %d: %s", dev, nand_info[dev].name);
260 puts("... is now current device\n");
261 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200262
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200263#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200264 /*
265 * Select the chip in the board/cpu specific driver
266 */
267 board_nand_select_device(nand_info[dev].priv, dev);
268#endif
269
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100270 return 0;
271 }
272
273 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juul3043c042007-11-14 14:28:11 +0100274 strncmp(cmd, "dump", 4) != 0 &&
275 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
276 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
277 strcmp(cmd, "biterr") != 0 &&
278 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100279 goto usage;
280
281 /* the following commands operate on the current device */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200282 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100283 !nand_info[nand_curr_device].name) {
284 puts("\nno devices available\n");
285 return 1;
286 }
287 nand = &nand_info[nand_curr_device];
288
289 if (strcmp(cmd, "bad") == 0) {
290 printf("\nDevice %d bad blocks:\n", nand_curr_device);
291 for (off = 0; off < nand->size; off += nand->erasesize)
292 if (nand_block_isbad(nand, off))
Stefan Roesee8706902008-07-10 10:10:54 +0200293 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100294 return 0;
295 }
296
Stefan Roese856f0542006-10-28 15:55:52 +0200297 /*
298 * Syntax is:
299 * 0 1 2 3 4
300 * nand erase [clean] [off size]
301 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200302 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
303 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200304 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100305 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200306 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200307 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100308
Stefan Roese856f0542006-10-28 15:55:52 +0200309 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
310 /* skip first two or three arguments, look for offset and size */
311 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
312 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200313
314 memset(&opts, 0, sizeof(opts));
315 opts.offset = off;
316 opts.length = size;
317 opts.jffs2 = clean;
318 opts.quiet = quiet;
319
320 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200321 puts("Warning: "
322 "scrub option will erase all factory set "
323 "bad blocks!\n"
324 " "
325 "There is no reliable way to recover them.\n"
326 " "
327 "Use this command only for testing purposes "
328 "if you\n"
329 " "
330 "are sure of what you are doing!\n"
331 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200332
333 if (getc() == 'y' && getc() == '\r') {
334 opts.scrub = 1;
335 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200336 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200337 return -1;
338 }
339 }
340 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100341 printf("%s\n", ret ? "ERROR" : "OK");
342
343 return ret == 0 ? 0 : 1;
344 }
345
346 if (strncmp(cmd, "dump", 4) == 0) {
347 if (argc < 3)
348 goto usage;
349
350 s = strchr(cmd, '.');
351 off = (int)simple_strtoul(argv[2], NULL, 16);
352
353 if (s != NULL && strcmp(s, ".oob") == 0)
William Juul9ad754f2007-12-14 16:33:45 +0100354 ret = nand_dump(nand, off, 1);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100355 else
William Juul9ad754f2007-12-14 16:33:45 +0100356 ret = nand_dump(nand, off, 0);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100357
358 return ret == 0 ? 1 : 0;
359
360 }
361
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100362 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200363 int read;
364
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100365 if (argc < 4)
366 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200367
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100368 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
369
Stefan Roese2255b2d2006-10-10 12:36:02 +0200370 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200371 printf("\nNAND %s: ", read ? "read" : "write");
372 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
373 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100374
Stefan Roese2255b2d2006-10-10 12:36:02 +0200375 s = strchr(cmd, '.');
Scott Wood984e03c2008-06-12 13:13:23 -0500376 if (!s || !strcmp(s, ".jffs2") ||
377 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500378 if (read)
379 ret = nand_read_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200380 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500381 else
382 ret = nand_write_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200383 (u_char *)addr);
Harald Welte62d4f432007-12-19 14:12:53 +0100384 } else if (s != NULL && !strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100385 /* out-of-band data */
386 mtd_oob_ops_t ops = {
387 .oobbuf = (u8 *)addr,
388 .ooblen = size,
389 .mode = MTD_OOB_RAW
390 };
391
Harald Welte62d4f432007-12-19 14:12:53 +0100392 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100393 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100394 else
William Juulcfa460a2007-10-31 13:53:06 +0100395 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200396 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500397 printf("Unknown nand command suffix '%s'.\n", s);
398 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200399 }
400
Mike Frysingerd4bade82009-01-18 19:46:06 -0500401 printf(" %zu bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200402 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100403
404 return ret == 0 ? 0 : 1;
405 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200406
Stefan Roese2255b2d2006-10-10 12:36:02 +0200407 if (strcmp(cmd, "markbad") == 0) {
408 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
409
410 int ret = nand->block_markbad(nand, addr);
411 if (ret == 0) {
412 printf("block 0x%08lx successfully marked as bad\n",
413 (ulong) addr);
414 return 0;
415 } else {
416 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
417 (ulong) addr, ret);
418 }
419 return 1;
420 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500421
Stefan Roese2255b2d2006-10-10 12:36:02 +0200422 if (strcmp(cmd, "biterr") == 0) {
423 /* todo */
424 return 1;
425 }
426
Nishanth Menon50657c22008-12-13 09:43:06 -0600427#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200428 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600429 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200430 int status = 0;
431 if (argc == 3) {
432 if (!strcmp("tight", argv[2]))
433 tight = 1;
434 if (!strcmp("status", argv[2]))
435 status = 1;
436 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200437 if (status) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600438 do_nand_status(nand);
William Juulcfa460a2007-10-31 13:53:06 +0100439 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100440 if (!nand_lock(nand, tight)) {
441 puts("NAND flash successfully locked\n");
442 } else {
443 puts("Error locking NAND flash\n");
444 return 1;
445 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200446 }
447 return 0;
448 }
449
450 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200451 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
452 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200453
William Juul5e1dae52007-11-09 13:32:30 +0100454 if (!nand_unlock(nand, off, size)) {
455 puts("NAND flash successfully unlocked\n");
456 } else {
457 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100458 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100459 return 1;
460 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200461 return 0;
462 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600463#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200464
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100465usage:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600466 cmd_usage(cmdtp);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100467 return 1;
468}
469
470U_BOOT_CMD(nand, 5, 1, do_nand,
Peter Tyser2fb26042009-01-27 18:03:12 -0600471 "NAND sub-system",
Wolfgang Denk4b070802008-08-14 14:41:06 +0200472 "info - show available NAND devices\n"
473 "nand device [dev] - show or set current device\n"
474 "nand read - addr off|partition size\n"
475 "nand write - addr off|partition size\n"
476 " read/write 'size' bytes starting at offset 'off'\n"
477 " to/from memory address 'addr', skipping bad blocks.\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"
Nishanth Menon50657c22008-12-13 09:43:06 -0600485#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denk4b070802008-08-14 14:41:06 +0200486 "nand lock [tight] [status]\n"
487 " bring nand to lock state or display locked pages\n"
Nishanth Menon50657c22008-12-13 09:43:06 -0600488 "nand unlock [offset] [size] - unlock section\n"
489#endif
490);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100491
Stefan Roese856f0542006-10-28 15:55:52 +0200492static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200493 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200494{
495 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200496 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200497 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200498 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100499#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200500 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100501#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200502
503 s = strchr(cmd, '.');
504 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500505 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500506 printf("Unknown nand load suffix '%s'\n", s);
507 show_boot_progress(-53);
508 return 1;
509 }
Stefan Roese856f0542006-10-28 15:55:52 +0200510
511 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
512
William Juulcfa460a2007-10-31 13:53:06 +0100513 cnt = nand->writesize;
Ladislav Michl09875052009-03-13 14:38:19 +0100514 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200515 if (r) {
516 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200517 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200518 return 1;
519 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200520 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200521
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100522 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100523 case IMAGE_FORMAT_LEGACY:
524 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200525
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100526 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100527 image_print_contents (hdr);
528
529 cnt = image_get_image_size (hdr);
530 break;
531#if defined(CONFIG_FIT)
532 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100533 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100534 puts ("Fit image detected...\n");
535
536 cnt = fit_get_size (fit_hdr);
537 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100538#endif
539 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100540 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100541 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200542 return 1;
543 }
William Juul3043c042007-11-14 14:28:11 +0100544 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200545
Ladislav Michl09875052009-03-13 14:38:19 +0100546 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200547 if (r) {
548 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200549 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200550 return 1;
551 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200552 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200553
Marian Balakowicz09475f72008-03-12 10:33:01 +0100554#if defined(CONFIG_FIT)
555 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200556 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
557 if (!fit_check_format (fit_hdr)) {
558 show_boot_progress (-150);
559 puts ("** Bad FIT image format\n");
560 return 1;
561 }
562 show_boot_progress (151);
563 fit_print_contents (fit_hdr);
564 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100565#endif
566
Stefan Roese856f0542006-10-28 15:55:52 +0200567 /* Loading ok, update default load address */
568
569 load_addr = addr;
570
571 /* Check if we should attempt an auto-start */
572 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
573 char *local_args[2];
574 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
575
576 local_args[0] = cmd;
577 local_args[1] = NULL;
578
579 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
580
581 do_bootm(cmdtp, 0, 1, local_args);
582 return 1;
583 }
584 return 0;
585}
586
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100587int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
588{
589 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200590 int idx;
591 ulong addr, offset = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100592#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200593 struct mtd_device *dev;
594 struct part_info *part;
595 u8 pnum;
596
597 if (argc >= 2) {
598 char *p = (argc == 2) ? argv[1] : argv[2];
599 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
600 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
601 if (dev->id->type != MTD_DEV_TYPE_NAND) {
602 puts("Not a NAND device\n");
603 return 1;
604 }
605 if (argc > 3)
606 goto usage;
607 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200608 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200609 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200610 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese856f0542006-10-28 15:55:52 +0200611 return nand_load_image(cmdtp, &nand_info[dev->id->num],
Wolfgang Denk4b070802008-08-14 14:41:06 +0200612 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200613 }
614 }
615#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100616
Heiko Schocherfad63402007-07-13 09:54:17 +0200617 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100618 switch (argc) {
619 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200620 addr = CONFIG_SYS_LOAD_ADDR;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100621 boot_device = getenv("bootdevice");
622 break;
623 case 2:
624 addr = simple_strtoul(argv[1], NULL, 16);
625 boot_device = getenv("bootdevice");
626 break;
627 case 3:
628 addr = simple_strtoul(argv[1], NULL, 16);
629 boot_device = argv[2];
630 break;
631 case 4:
632 addr = simple_strtoul(argv[1], NULL, 16);
633 boot_device = argv[2];
634 offset = simple_strtoul(argv[3], NULL, 16);
635 break;
636 default:
Stefan Roese68d7d652009-03-19 13:30:36 +0100637#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200638usage:
639#endif
Peter Tyser62c3ae72009-01-27 18:03:10 -0600640 cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200641 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100642 return 1;
643 }
644
Heiko Schocherfad63402007-07-13 09:54:17 +0200645 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100646 if (!boot_device) {
647 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200648 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100649 return 1;
650 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200651 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100652
Stefan Roese856f0542006-10-28 15:55:52 +0200653 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100654
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200655 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
Stefan Roese856f0542006-10-28 15:55:52 +0200656 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200657 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100658 return 1;
659 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200660 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100661
Stefan Roese856f0542006-10-28 15:55:52 +0200662 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100663}
664
665U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600666 "boot from NAND device",
Scott Wood984e03c2008-06-12 13:13:23 -0500667 "[partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100668
Jon Loeligerc76fe472007-07-08 18:02:23 -0500669#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100670
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200671#else /* CONFIG_NAND_LEGACY */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100672/*
673 *
674 * Legacy NAND support - to be phased out
675 *
676 */
wdenkdc7c9a12003-03-26 06:55:25 +0000677#include <command.h>
678#include <malloc.h>
679#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000680#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000681
Heiko Schocherfad63402007-07-13 09:54:17 +0200682#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000683# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200684# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000685#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200686# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000687#endif
688
Jon Loeligerc76fe472007-07-08 18:02:23 -0500689#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100690#include <linux/mtd/nand_legacy.h>
691#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000692#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000693#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100694#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000695
wdenk1f4bb372003-07-27 00:21:01 +0000696#ifdef CONFIG_OMAP1510
697void archflashwp(void *archdata, int wp);
698#endif
699
William Juul3043c042007-11-14 14:28:11 +0100700#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
wdenk1f4bb372003-07-27 00:21:01 +0000701
William Juul3043c042007-11-14 14:28:11 +0100702#undef NAND_DEBUG
703#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000704
705/* ****************** WARNING *********************
706 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
707 * erase (or at least attempt to erase) blocks that are marked
708 * bad. This can be very handy if you are _sure_ that the block
709 * is OK, say because you marked a good block bad to test bad
710 * block handling and you are done testing, or if you have
711 * accidentally marked blocks bad.
712 *
713 * Erasing factory marked bad blocks is a _bad_ idea. If the
714 * erase succeeds there is no reliable way to find them again,
715 * and attempting to program or erase bad blocks can affect
716 * the data in _other_ (good) blocks.
717 */
William Juul3043c042007-11-14 14:28:11 +0100718#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000719
720#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000721#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000722
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100723/* bits for nand_legacy_rw() `cmd'; or together as needed */
William Juul5e1dae52007-11-09 13:32:30 +0100724#define NANDRW_READ 0x01
725#define NANDRW_WRITE 0x00
726#define NANDRW_JFFS2 0x02
William Juulcfa460a2007-10-31 13:53:06 +0100727#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000728
wdenkdc7c9a12003-03-26 06:55:25 +0000729/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100730 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000731 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200732extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100733extern int curr_device;
734extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100735 size_t len, int clean);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100736extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
William Juul5e1dae52007-11-09 13:32:30 +0100737 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100738extern void nand_print(struct nand_chip *nand);
739extern void nand_print_bad(struct nand_chip *nand);
740extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100741 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100742extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
William Juul5e1dae52007-11-09 13:32:30 +0100743 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000744
wdenkdc7c9a12003-03-26 06:55:25 +0000745
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100746int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000747{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100748 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000749
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100750 switch (argc) {
751 case 0:
752 case 1:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600753 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100754 return 1;
755 case 2:
756 if (strcmp (argv[1], "info") == 0) {
757 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000758
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100759 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000760
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200761 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; ++i) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100762 if (nand_dev_desc[i].ChipID ==
763 NAND_ChipID_UNKNOWN)
764 continue; /* list only known devices */
765 printf ("Device %d: ", i);
766 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000767 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100768 return 0;
769
770 } else if (strcmp (argv[1], "device") == 0) {
771 if ((curr_device < 0)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200772 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100773 puts ("\nno devices available\n");
774 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000775 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100776 printf ("\nDevice %d: ", curr_device);
777 nand_print (&nand_dev_desc[curr_device]);
778 return 0;
779
780 } else if (strcmp (argv[1], "bad") == 0) {
781 if ((curr_device < 0)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200782 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100783 puts ("\nno devices available\n");
784 return 1;
785 }
786 printf ("\nDevice %d bad blocks:\n", curr_device);
787 nand_print_bad (&nand_dev_desc[curr_device]);
788 return 0;
789
790 }
Peter Tyser62c3ae72009-01-27 18:03:10 -0600791 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100792 return 1;
793 case 3:
794 if (strcmp (argv[1], "device") == 0) {
795 int dev = (int) simple_strtoul (argv[2], NULL, 10);
796
797 printf ("\nDevice %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200798 if (dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100799 puts ("unknown device\n");
800 return 1;
801 }
802 nand_print (&nand_dev_desc[dev]);
803 /*nand_print (dev); */
804
805 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
806 return 1;
807 }
808
809 curr_device = dev;
810
811 puts ("... is now current device\n");
812
813 return 0;
814 } else if (strcmp (argv[1], "erase") == 0
815 && strcmp (argv[2], "clean") == 0) {
816 struct nand_chip *nand = &nand_dev_desc[curr_device];
817 ulong off = 0;
818 ulong size = nand->totlen;
819 int ret;
820
821 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
822
823 ret = nand_legacy_erase (nand, off, size, 1);
824
825 printf ("%s\n", ret ? "ERROR" : "OK");
826
wdenk7a8e9bed2003-05-31 18:35:21 +0000827 return ret;
828 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100829
Peter Tyser62c3ae72009-01-27 18:03:10 -0600830 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100831 return 1;
832 default:
833 /* at least 4 args */
834
835 if (strncmp (argv[1], "read", 4) == 0 ||
836 strncmp (argv[1], "write", 5) == 0) {
William Juul5e1dae52007-11-09 13:32:30 +0100837 ulong addr = simple_strtoul (argv[2], NULL, 16);
838 off_t off = simple_strtoul (argv[3], NULL, 16);
839 size_t size = simple_strtoul (argv[4], NULL, 16);
840 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
Wolfgang Denk4b070802008-08-14 14:41:06 +0200841 NANDRW_READ : NANDRW_WRITE;
Wolfgang Denkc71abba2008-04-28 14:55:12 +0200842 size_t total;
843 int ret;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100844 char *cmdtail = strchr (argv[1], '.');
845
846 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
847 /* read out-of-band data */
848 if (cmd & NANDRW_READ) {
849 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200850 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100851 (u_char *) addr);
852 } else {
853 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200854 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100855 (u_char *) addr);
856 }
857 return ret;
Schlaegl Manfred jun389e6622009-01-20 16:57:55 +0100858 } else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 7)) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100859 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
860 if (cmd & NANDRW_READ)
861 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Schlaegl Manfred jun389e6622009-01-20 16:57:55 +0100862 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
863 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000864#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100865 /* need ".e" same as ".j" for compatibility with older units */
866 else if (cmdtail && !strcmp (cmdtail, ".e"))
867 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000868#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200869#ifdef CONFIG_SYS_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100870 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
871 /* ".i" for image -> read skips bad block (no 0xff) */
872 else if (cmdtail && !strcmp (cmdtail, ".i")) {
873 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
874 if (cmd & NANDRW_READ)
William Juul3043c042007-11-14 14:28:11 +0100875 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100876 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200877#endif /* CONFIG_SYS_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100878 else if (cmdtail) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600879 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100880 return 1;
881 }
882
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200883 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100884 (cmd & NANDRW_READ) ? "read" : "write",
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200885 curr_device, off, (ulong)size);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100886
887 ret = nand_legacy_rw (nand_dev_desc + curr_device,
888 cmd, off, size,
William Juul3043c042007-11-14 14:28:11 +0100889 &total, (u_char *) addr);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100890
891 printf (" %d bytes %s: %s\n", total,
892 (cmd & NANDRW_READ) ? "read" : "written",
893 ret ? "ERROR" : "OK");
894
895 return ret;
896 } else if (strcmp (argv[1], "erase") == 0 &&
897 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
898 int clean = argc == 5;
899 ulong off =
900 simple_strtoul (argv[2 + clean], NULL, 16);
901 ulong size =
902 simple_strtoul (argv[3 + clean], NULL, 16);
903 int ret;
904
905 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
906 curr_device, off, size);
907
908 ret = nand_legacy_erase (nand_dev_desc + curr_device,
909 off, size, clean);
910
911 printf ("%s\n", ret ? "ERROR" : "OK");
912
913 return ret;
914 } else {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600915 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100916 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000917 }
wdenkdc7c9a12003-03-26 06:55:25 +0000918
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100919 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000920 }
wdenkdc7c9a12003-03-26 06:55:25 +0000921}
922
wdenk0d498392003-07-01 21:06:45 +0000923U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +0100924 nand, 5, 1, do_nand,
Peter Tyser2fb26042009-01-27 18:03:12 -0600925 "legacy NAND sub-system",
wdenkb0fce992003-06-29 21:03:46 +0000926 "info - show available NAND devices\n"
927 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000928 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000929 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
William Juul3043c042007-11-14 14:28:11 +0100930 " at offset `off' to/from memory address `addr'\n"
wdenkb0fce992003-06-29 21:03:46 +0000931 "nand erase [clean] [off size] - erase `size' bytes from\n"
William Juul3043c042007-11-14 14:28:11 +0100932 " offset `off' (entire device if not specified)\n"
wdenkb0fce992003-06-29 21:03:46 +0000933 "nand bad - show bad blocks\n"
934 "nand read.oob addr off size - read out-of-band data\n"
935 "nand write.oob addr off size - read out-of-band data\n"
936);
937
wdenkdc7c9a12003-03-26 06:55:25 +0000938int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
939{
940 char *boot_device = NULL;
941 char *ep;
942 int dev;
943 ulong cnt;
944 ulong addr;
945 ulong offset = 0;
946 image_header_t *hdr;
947 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100948#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200949 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100950#endif
951
Heiko Schocherfad63402007-07-13 09:54:17 +0200952 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000953 switch (argc) {
954 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200955 addr = CONFIG_SYS_LOAD_ADDR;
wdenkdc7c9a12003-03-26 06:55:25 +0000956 boot_device = getenv ("bootdevice");
957 break;
958 case 2:
959 addr = simple_strtoul(argv[1], NULL, 16);
960 boot_device = getenv ("bootdevice");
961 break;
962 case 3:
963 addr = simple_strtoul(argv[1], NULL, 16);
964 boot_device = argv[2];
965 break;
966 case 4:
967 addr = simple_strtoul(argv[1], NULL, 16);
968 boot_device = argv[2];
969 offset = simple_strtoul(argv[3], NULL, 16);
970 break;
971 default:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600972 cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200973 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000974 return 1;
975 }
976
Heiko Schocherfad63402007-07-13 09:54:17 +0200977 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000978 if (!boot_device) {
979 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200980 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000981 return 1;
982 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200983 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +0000984
985 dev = simple_strtoul(boot_device, &ep, 16);
986
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200987 if ((dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
William Juul3043c042007-11-14 14:28:11 +0100988 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
wdenkdc7c9a12003-03-26 06:55:25 +0000989 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200990 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +0000991 return 1;
992 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200993 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +0000994
wdenk7a8e9bed2003-05-31 18:35:21 +0000995 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
William Juulcfa460a2007-10-31 13:53:06 +0100996 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
997 offset);
wdenkdc7c9a12003-03-26 06:55:25 +0000998
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100999 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
Wolfgang Denk4b070802008-08-14 14:41:06 +02001000 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001001 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001002 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +00001003 return 1;
1004 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001005 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001006
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001007 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001008 case IMAGE_FORMAT_LEGACY:
1009 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001010 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001011
Marian Balakowicz09475f72008-03-12 10:33:01 +01001012 cnt = image_get_image_size (hdr);
1013 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001014 break;
1015#if defined(CONFIG_FIT)
1016 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001017 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001018 puts ("Fit image detected...\n");
1019
1020 cnt = fit_get_size (fit_hdr);
1021 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001022#endif
1023 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001024 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001025 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001026 return 1;
1027 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001028 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001029
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001030 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
Wolfgang Denk4b070802008-08-14 14:41:06 +02001031 offset + SECTORSIZE, cnt, NULL,
1032 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001033 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001034 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001035 return 1;
1036 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001037 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001038
Marian Balakowicz09475f72008-03-12 10:33:01 +01001039#if defined(CONFIG_FIT)
1040 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +02001041 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1042 if (!fit_check_format (fit_hdr)) {
1043 show_boot_progress (-150);
1044 puts ("** Bad FIT image format\n");
1045 return 1;
1046 }
1047 show_boot_progress (151);
1048 fit_print_contents (fit_hdr);
1049 }
Marian Balakowicz09475f72008-03-12 10:33:01 +01001050#endif
1051
wdenkdc7c9a12003-03-26 06:55:25 +00001052 /* Loading ok, update default load address */
1053
1054 load_addr = addr;
1055
1056 /* Check if we should attempt an auto-start */
1057 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1058 char *local_args[2];
1059 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1060
1061 local_args[0] = argv[0];
1062 local_args[1] = NULL;
1063
wdenk7a8e9bed2003-05-31 18:35:21 +00001064 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001065
1066 do_bootm (cmdtp, 0, 1, local_args);
1067 rcode = 1;
1068 }
1069 return rcode;
1070}
1071
wdenk0d498392003-07-01 21:06:45 +00001072U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +01001073 nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -06001074 "boot from NAND device",
wdenkb0fce992003-06-29 21:03:46 +00001075 "loadAddr dev\n"
1076);
1077
Jon Loeligerc76fe472007-07-08 18:02:23 -05001078#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001079
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +02001080#endif /* CONFIG_NAND_LEGACY */