blob: ff5bc6e27d18ecf1367025bd59be59d7070a5601 [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
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100208static void nand_print_info(int idx)
209{
210 nand_info_t *nand = &nand_info[idx];
211 struct nand_chip *chip = nand->priv;
212 printf("Device %d: ", idx);
213 if (chip->numchips > 1)
214 printf("%dx ", chip->numchips);
215 printf("%s, sector size %u KiB\n",
216 nand->name, nand->erasesize >> 10);
217}
218
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100219int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
220{
William Juulcfa460a2007-10-31 13:53:06 +0100221 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200222 ulong addr, off;
223 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100224 char *cmd, *s;
225 nand_info_t *nand;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200226#ifdef CONFIG_SYS_NAND_QUIET
227 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200228#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200229 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200230#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200231 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100232
233 /* at least two arguments please */
234 if (argc < 2)
235 goto usage;
236
Stefan Roese2255b2d2006-10-10 12:36:02 +0200237 if (quiet_str)
238 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
239
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100240 cmd = argv[1];
241
242 if (strcmp(cmd, "info") == 0) {
243
244 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200245 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100246 if (nand_info[i].name)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100247 nand_print_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100248 }
249 return 0;
250 }
251
252 if (strcmp(cmd, "device") == 0) {
253
254 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100255 putc('\n');
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100256 if ((nand_curr_device < 0) ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200257 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100258 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100259 else
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100260 nand_print_info(nand_curr_device);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100261 return 0;
262 }
263 dev = (int)simple_strtoul(argv[2], NULL, 10);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200264 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100265 puts("No such device\n");
266 return 1;
267 }
268 printf("Device %d: %s", dev, nand_info[dev].name);
269 puts("... is now current device\n");
270 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200271
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200272#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200273 /*
274 * Select the chip in the board/cpu specific driver
275 */
276 board_nand_select_device(nand_info[dev].priv, dev);
277#endif
278
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100279 return 0;
280 }
281
282 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juul3043c042007-11-14 14:28:11 +0100283 strncmp(cmd, "dump", 4) != 0 &&
284 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
285 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
286 strcmp(cmd, "biterr") != 0 &&
287 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100288 goto usage;
289
290 /* the following commands operate on the current device */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200291 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100292 !nand_info[nand_curr_device].name) {
293 puts("\nno devices available\n");
294 return 1;
295 }
296 nand = &nand_info[nand_curr_device];
297
298 if (strcmp(cmd, "bad") == 0) {
299 printf("\nDevice %d bad blocks:\n", nand_curr_device);
300 for (off = 0; off < nand->size; off += nand->erasesize)
301 if (nand_block_isbad(nand, off))
Stefan Roesee8706902008-07-10 10:10:54 +0200302 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100303 return 0;
304 }
305
Stefan Roese856f0542006-10-28 15:55:52 +0200306 /*
307 * Syntax is:
308 * 0 1 2 3 4
309 * nand erase [clean] [off size]
310 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200311 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
312 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200313 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100314 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200315 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200316 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100317
Stefan Roese856f0542006-10-28 15:55:52 +0200318 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
319 /* skip first two or three arguments, look for offset and size */
320 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
321 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200322
323 memset(&opts, 0, sizeof(opts));
324 opts.offset = off;
325 opts.length = size;
326 opts.jffs2 = clean;
327 opts.quiet = quiet;
328
329 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200330 puts("Warning: "
331 "scrub option will erase all factory set "
332 "bad blocks!\n"
333 " "
334 "There is no reliable way to recover them.\n"
335 " "
336 "Use this command only for testing purposes "
337 "if you\n"
338 " "
339 "are sure of what you are doing!\n"
340 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200341
342 if (getc() == 'y' && getc() == '\r') {
343 opts.scrub = 1;
344 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200345 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200346 return -1;
347 }
348 }
349 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100350 printf("%s\n", ret ? "ERROR" : "OK");
351
352 return ret == 0 ? 0 : 1;
353 }
354
355 if (strncmp(cmd, "dump", 4) == 0) {
356 if (argc < 3)
357 goto usage;
358
359 s = strchr(cmd, '.');
360 off = (int)simple_strtoul(argv[2], NULL, 16);
361
362 if (s != NULL && strcmp(s, ".oob") == 0)
William Juul9ad754f2007-12-14 16:33:45 +0100363 ret = nand_dump(nand, off, 1);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100364 else
William Juul9ad754f2007-12-14 16:33:45 +0100365 ret = nand_dump(nand, off, 0);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100366
367 return ret == 0 ? 1 : 0;
368
369 }
370
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100371 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200372 int read;
373
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100374 if (argc < 4)
375 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200376
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100377 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
378
Stefan Roese2255b2d2006-10-10 12:36:02 +0200379 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200380 printf("\nNAND %s: ", read ? "read" : "write");
381 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
382 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100383
Stefan Roese2255b2d2006-10-10 12:36:02 +0200384 s = strchr(cmd, '.');
Scott Wood984e03c2008-06-12 13:13:23 -0500385 if (!s || !strcmp(s, ".jffs2") ||
386 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500387 if (read)
388 ret = nand_read_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200389 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500390 else
391 ret = nand_write_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200392 (u_char *)addr);
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400393 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100394 /* out-of-band data */
395 mtd_oob_ops_t ops = {
396 .oobbuf = (u8 *)addr,
397 .ooblen = size,
398 .mode = MTD_OOB_RAW
399 };
400
Harald Welte62d4f432007-12-19 14:12:53 +0100401 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100402 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100403 else
William Juulcfa460a2007-10-31 13:53:06 +0100404 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200405 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500406 printf("Unknown nand command suffix '%s'.\n", s);
407 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200408 }
409
Mike Frysingerd4bade82009-01-18 19:46:06 -0500410 printf(" %zu bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200411 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100412
413 return ret == 0 ? 0 : 1;
414 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200415
Stefan Roese2255b2d2006-10-10 12:36:02 +0200416 if (strcmp(cmd, "markbad") == 0) {
417 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
418
419 int ret = nand->block_markbad(nand, addr);
420 if (ret == 0) {
421 printf("block 0x%08lx successfully marked as bad\n",
422 (ulong) addr);
423 return 0;
424 } else {
425 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
426 (ulong) addr, ret);
427 }
428 return 1;
429 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500430
Stefan Roese2255b2d2006-10-10 12:36:02 +0200431 if (strcmp(cmd, "biterr") == 0) {
432 /* todo */
433 return 1;
434 }
435
Nishanth Menon50657c22008-12-13 09:43:06 -0600436#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200437 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600438 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200439 int status = 0;
440 if (argc == 3) {
441 if (!strcmp("tight", argv[2]))
442 tight = 1;
443 if (!strcmp("status", argv[2]))
444 status = 1;
445 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200446 if (status) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600447 do_nand_status(nand);
William Juulcfa460a2007-10-31 13:53:06 +0100448 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100449 if (!nand_lock(nand, tight)) {
450 puts("NAND flash successfully locked\n");
451 } else {
452 puts("Error locking NAND flash\n");
453 return 1;
454 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200455 }
456 return 0;
457 }
458
459 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200460 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
461 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200462
William Juul5e1dae52007-11-09 13:32:30 +0100463 if (!nand_unlock(nand, off, size)) {
464 puts("NAND flash successfully unlocked\n");
465 } else {
466 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100467 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100468 return 1;
469 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200470 return 0;
471 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600472#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200473
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100474usage:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600475 cmd_usage(cmdtp);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100476 return 1;
477}
478
479U_BOOT_CMD(nand, 5, 1, do_nand,
Peter Tyser2fb26042009-01-27 18:03:12 -0600480 "NAND sub-system",
Wolfgang Denk4b070802008-08-14 14:41:06 +0200481 "info - show available NAND devices\n"
482 "nand device [dev] - show or set current device\n"
483 "nand read - addr off|partition size\n"
484 "nand write - addr off|partition size\n"
485 " read/write 'size' bytes starting at offset 'off'\n"
486 " to/from memory address 'addr', skipping bad blocks.\n"
487 "nand erase [clean] [off size] - erase 'size' bytes from\n"
488 " offset 'off' (entire device if not specified)\n"
489 "nand bad - show bad blocks\n"
490 "nand dump[.oob] off - dump page\n"
491 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
492 "nand markbad off - mark bad block at offset (UNSAFE)\n"
493 "nand biterr off - make a bit error at offset (UNSAFE)\n"
Nishanth Menon50657c22008-12-13 09:43:06 -0600494#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denk4b070802008-08-14 14:41:06 +0200495 "nand lock [tight] [status]\n"
496 " bring nand to lock state or display locked pages\n"
Nishanth Menon50657c22008-12-13 09:43:06 -0600497 "nand unlock [offset] [size] - unlock section\n"
498#endif
499);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100500
Stefan Roese856f0542006-10-28 15:55:52 +0200501static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200502 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200503{
504 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200505 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200506 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200507 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100508#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200509 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100510#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200511
512 s = strchr(cmd, '.');
513 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500514 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500515 printf("Unknown nand load suffix '%s'\n", s);
516 show_boot_progress(-53);
517 return 1;
518 }
Stefan Roese856f0542006-10-28 15:55:52 +0200519
520 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
521
William Juulcfa460a2007-10-31 13:53:06 +0100522 cnt = nand->writesize;
Ladislav Michl09875052009-03-13 14:38:19 +0100523 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200524 if (r) {
525 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200526 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200527 return 1;
528 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200529 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200530
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100531 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100532 case IMAGE_FORMAT_LEGACY:
533 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200534
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100535 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100536 image_print_contents (hdr);
537
538 cnt = image_get_image_size (hdr);
539 break;
540#if defined(CONFIG_FIT)
541 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100542 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100543 puts ("Fit image detected...\n");
544
545 cnt = fit_get_size (fit_hdr);
546 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100547#endif
548 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100549 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100550 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200551 return 1;
552 }
William Juul3043c042007-11-14 14:28:11 +0100553 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200554
Ladislav Michl09875052009-03-13 14:38:19 +0100555 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200556 if (r) {
557 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200558 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200559 return 1;
560 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200561 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200562
Marian Balakowicz09475f72008-03-12 10:33:01 +0100563#if defined(CONFIG_FIT)
564 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200565 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
566 if (!fit_check_format (fit_hdr)) {
567 show_boot_progress (-150);
568 puts ("** Bad FIT image format\n");
569 return 1;
570 }
571 show_boot_progress (151);
572 fit_print_contents (fit_hdr);
573 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100574#endif
575
Stefan Roese856f0542006-10-28 15:55:52 +0200576 /* Loading ok, update default load address */
577
578 load_addr = addr;
579
580 /* Check if we should attempt an auto-start */
581 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
582 char *local_args[2];
583 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
584
585 local_args[0] = cmd;
586 local_args[1] = NULL;
587
588 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
589
590 do_bootm(cmdtp, 0, 1, local_args);
591 return 1;
592 }
593 return 0;
594}
595
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100596int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
597{
598 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200599 int idx;
600 ulong addr, offset = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100601#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200602 struct mtd_device *dev;
603 struct part_info *part;
604 u8 pnum;
605
606 if (argc >= 2) {
607 char *p = (argc == 2) ? argv[1] : argv[2];
608 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
609 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
610 if (dev->id->type != MTD_DEV_TYPE_NAND) {
611 puts("Not a NAND device\n");
612 return 1;
613 }
614 if (argc > 3)
615 goto usage;
616 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200617 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200618 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200619 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese856f0542006-10-28 15:55:52 +0200620 return nand_load_image(cmdtp, &nand_info[dev->id->num],
Wolfgang Denk4b070802008-08-14 14:41:06 +0200621 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200622 }
623 }
624#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100625
Heiko Schocherfad63402007-07-13 09:54:17 +0200626 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100627 switch (argc) {
628 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200629 addr = CONFIG_SYS_LOAD_ADDR;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100630 boot_device = getenv("bootdevice");
631 break;
632 case 2:
633 addr = simple_strtoul(argv[1], NULL, 16);
634 boot_device = getenv("bootdevice");
635 break;
636 case 3:
637 addr = simple_strtoul(argv[1], NULL, 16);
638 boot_device = argv[2];
639 break;
640 case 4:
641 addr = simple_strtoul(argv[1], NULL, 16);
642 boot_device = argv[2];
643 offset = simple_strtoul(argv[3], NULL, 16);
644 break;
645 default:
Stefan Roese68d7d652009-03-19 13:30:36 +0100646#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200647usage:
648#endif
Peter Tyser62c3ae72009-01-27 18:03:10 -0600649 cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200650 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100651 return 1;
652 }
653
Heiko Schocherfad63402007-07-13 09:54:17 +0200654 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100655 if (!boot_device) {
656 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200657 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100658 return 1;
659 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200660 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100661
Stefan Roese856f0542006-10-28 15:55:52 +0200662 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100663
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200664 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
Stefan Roese856f0542006-10-28 15:55:52 +0200665 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200666 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100667 return 1;
668 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200669 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100670
Stefan Roese856f0542006-10-28 15:55:52 +0200671 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100672}
673
674U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600675 "boot from NAND device",
Scott Wood984e03c2008-06-12 13:13:23 -0500676 "[partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100677
Jon Loeligerc76fe472007-07-08 18:02:23 -0500678#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100679
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200680#else /* CONFIG_NAND_LEGACY */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100681/*
682 *
683 * Legacy NAND support - to be phased out
684 *
685 */
wdenkdc7c9a12003-03-26 06:55:25 +0000686#include <command.h>
687#include <malloc.h>
688#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000689#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000690
Heiko Schocherfad63402007-07-13 09:54:17 +0200691#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000692# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200693# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000694#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200695# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000696#endif
697
Jon Loeligerc76fe472007-07-08 18:02:23 -0500698#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100699#include <linux/mtd/nand_legacy.h>
700#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000701#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000702#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100703#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000704
wdenk1f4bb372003-07-27 00:21:01 +0000705#ifdef CONFIG_OMAP1510
706void archflashwp(void *archdata, int wp);
707#endif
708
William Juul3043c042007-11-14 14:28:11 +0100709#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
wdenk1f4bb372003-07-27 00:21:01 +0000710
William Juul3043c042007-11-14 14:28:11 +0100711#undef NAND_DEBUG
712#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000713
714/* ****************** WARNING *********************
715 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
716 * erase (or at least attempt to erase) blocks that are marked
717 * bad. This can be very handy if you are _sure_ that the block
718 * is OK, say because you marked a good block bad to test bad
719 * block handling and you are done testing, or if you have
720 * accidentally marked blocks bad.
721 *
722 * Erasing factory marked bad blocks is a _bad_ idea. If the
723 * erase succeeds there is no reliable way to find them again,
724 * and attempting to program or erase bad blocks can affect
725 * the data in _other_ (good) blocks.
726 */
William Juul3043c042007-11-14 14:28:11 +0100727#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000728
729#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000730#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000731
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100732/* bits for nand_legacy_rw() `cmd'; or together as needed */
William Juul5e1dae52007-11-09 13:32:30 +0100733#define NANDRW_READ 0x01
734#define NANDRW_WRITE 0x00
735#define NANDRW_JFFS2 0x02
William Juulcfa460a2007-10-31 13:53:06 +0100736#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000737
wdenkdc7c9a12003-03-26 06:55:25 +0000738/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100739 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000740 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200741extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100742extern int curr_device;
743extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100744 size_t len, int clean);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100745extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
William Juul5e1dae52007-11-09 13:32:30 +0100746 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100747extern void nand_print(struct nand_chip *nand);
748extern void nand_print_bad(struct nand_chip *nand);
749extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100750 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100751extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
William Juul5e1dae52007-11-09 13:32:30 +0100752 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000753
wdenkdc7c9a12003-03-26 06:55:25 +0000754
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100755int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000756{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100757 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000758
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100759 switch (argc) {
760 case 0:
761 case 1:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600762 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100763 return 1;
764 case 2:
765 if (strcmp (argv[1], "info") == 0) {
766 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000767
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100768 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000769
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200770 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; ++i) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100771 if (nand_dev_desc[i].ChipID ==
772 NAND_ChipID_UNKNOWN)
773 continue; /* list only known devices */
774 printf ("Device %d: ", i);
775 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000776 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100777 return 0;
778
779 } else if (strcmp (argv[1], "device") == 0) {
780 if ((curr_device < 0)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200781 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100782 puts ("\nno devices available\n");
783 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000784 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100785 printf ("\nDevice %d: ", curr_device);
786 nand_print (&nand_dev_desc[curr_device]);
787 return 0;
788
789 } else if (strcmp (argv[1], "bad") == 0) {
790 if ((curr_device < 0)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200791 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100792 puts ("\nno devices available\n");
793 return 1;
794 }
795 printf ("\nDevice %d bad blocks:\n", curr_device);
796 nand_print_bad (&nand_dev_desc[curr_device]);
797 return 0;
798
799 }
Peter Tyser62c3ae72009-01-27 18:03:10 -0600800 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100801 return 1;
802 case 3:
803 if (strcmp (argv[1], "device") == 0) {
804 int dev = (int) simple_strtoul (argv[2], NULL, 10);
805
806 printf ("\nDevice %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200807 if (dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100808 puts ("unknown device\n");
809 return 1;
810 }
811 nand_print (&nand_dev_desc[dev]);
812 /*nand_print (dev); */
813
814 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
815 return 1;
816 }
817
818 curr_device = dev;
819
820 puts ("... is now current device\n");
821
822 return 0;
823 } else if (strcmp (argv[1], "erase") == 0
824 && strcmp (argv[2], "clean") == 0) {
825 struct nand_chip *nand = &nand_dev_desc[curr_device];
826 ulong off = 0;
827 ulong size = nand->totlen;
828 int ret;
829
830 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
831
832 ret = nand_legacy_erase (nand, off, size, 1);
833
834 printf ("%s\n", ret ? "ERROR" : "OK");
835
wdenk7a8e9bed2003-05-31 18:35:21 +0000836 return ret;
837 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100838
Peter Tyser62c3ae72009-01-27 18:03:10 -0600839 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100840 return 1;
841 default:
842 /* at least 4 args */
843
844 if (strncmp (argv[1], "read", 4) == 0 ||
845 strncmp (argv[1], "write", 5) == 0) {
William Juul5e1dae52007-11-09 13:32:30 +0100846 ulong addr = simple_strtoul (argv[2], NULL, 16);
847 off_t off = simple_strtoul (argv[3], NULL, 16);
848 size_t size = simple_strtoul (argv[4], NULL, 16);
849 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
Wolfgang Denk4b070802008-08-14 14:41:06 +0200850 NANDRW_READ : NANDRW_WRITE;
Wolfgang Denkc71abba2008-04-28 14:55:12 +0200851 size_t total;
852 int ret;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100853 char *cmdtail = strchr (argv[1], '.');
854
855 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
856 /* read out-of-band data */
857 if (cmd & NANDRW_READ) {
858 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200859 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100860 (u_char *) addr);
861 } else {
862 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200863 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100864 (u_char *) addr);
865 }
866 return ret;
Schlaegl Manfred jun389e6622009-01-20 16:57:55 +0100867 } else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 7)) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100868 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
869 if (cmd & NANDRW_READ)
870 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Schlaegl Manfred jun389e6622009-01-20 16:57:55 +0100871 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
872 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000873#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100874 /* need ".e" same as ".j" for compatibility with older units */
875 else if (cmdtail && !strcmp (cmdtail, ".e"))
876 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000877#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200878#ifdef CONFIG_SYS_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100879 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
880 /* ".i" for image -> read skips bad block (no 0xff) */
881 else if (cmdtail && !strcmp (cmdtail, ".i")) {
882 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
883 if (cmd & NANDRW_READ)
William Juul3043c042007-11-14 14:28:11 +0100884 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100885 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200886#endif /* CONFIG_SYS_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100887 else if (cmdtail) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600888 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100889 return 1;
890 }
891
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200892 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100893 (cmd & NANDRW_READ) ? "read" : "write",
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200894 curr_device, off, (ulong)size);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100895
896 ret = nand_legacy_rw (nand_dev_desc + curr_device,
897 cmd, off, size,
William Juul3043c042007-11-14 14:28:11 +0100898 &total, (u_char *) addr);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100899
900 printf (" %d bytes %s: %s\n", total,
901 (cmd & NANDRW_READ) ? "read" : "written",
902 ret ? "ERROR" : "OK");
903
904 return ret;
905 } else if (strcmp (argv[1], "erase") == 0 &&
906 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
907 int clean = argc == 5;
908 ulong off =
909 simple_strtoul (argv[2 + clean], NULL, 16);
910 ulong size =
911 simple_strtoul (argv[3 + clean], NULL, 16);
912 int ret;
913
914 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
915 curr_device, off, size);
916
917 ret = nand_legacy_erase (nand_dev_desc + curr_device,
918 off, size, clean);
919
920 printf ("%s\n", ret ? "ERROR" : "OK");
921
922 return ret;
923 } else {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600924 cmd_usage(cmdtp);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100925 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000926 }
wdenkdc7c9a12003-03-26 06:55:25 +0000927
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100928 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000929 }
wdenkdc7c9a12003-03-26 06:55:25 +0000930}
931
wdenk0d498392003-07-01 21:06:45 +0000932U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +0100933 nand, 5, 1, do_nand,
Peter Tyser2fb26042009-01-27 18:03:12 -0600934 "legacy NAND sub-system",
wdenkb0fce992003-06-29 21:03:46 +0000935 "info - show available NAND devices\n"
936 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000937 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000938 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
William Juul3043c042007-11-14 14:28:11 +0100939 " at offset `off' to/from memory address `addr'\n"
wdenkb0fce992003-06-29 21:03:46 +0000940 "nand erase [clean] [off size] - erase `size' bytes from\n"
William Juul3043c042007-11-14 14:28:11 +0100941 " offset `off' (entire device if not specified)\n"
wdenkb0fce992003-06-29 21:03:46 +0000942 "nand bad - show bad blocks\n"
943 "nand read.oob addr off size - read out-of-band data\n"
944 "nand write.oob addr off size - read out-of-band data\n"
945);
946
wdenkdc7c9a12003-03-26 06:55:25 +0000947int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
948{
949 char *boot_device = NULL;
950 char *ep;
951 int dev;
952 ulong cnt;
953 ulong addr;
954 ulong offset = 0;
955 image_header_t *hdr;
956 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100957#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200958 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100959#endif
960
Heiko Schocherfad63402007-07-13 09:54:17 +0200961 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000962 switch (argc) {
963 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200964 addr = CONFIG_SYS_LOAD_ADDR;
wdenkdc7c9a12003-03-26 06:55:25 +0000965 boot_device = getenv ("bootdevice");
966 break;
967 case 2:
968 addr = simple_strtoul(argv[1], NULL, 16);
969 boot_device = getenv ("bootdevice");
970 break;
971 case 3:
972 addr = simple_strtoul(argv[1], NULL, 16);
973 boot_device = argv[2];
974 break;
975 case 4:
976 addr = simple_strtoul(argv[1], NULL, 16);
977 boot_device = argv[2];
978 offset = simple_strtoul(argv[3], NULL, 16);
979 break;
980 default:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600981 cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200982 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000983 return 1;
984 }
985
Heiko Schocherfad63402007-07-13 09:54:17 +0200986 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000987 if (!boot_device) {
988 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200989 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000990 return 1;
991 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200992 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +0000993
994 dev = simple_strtoul(boot_device, &ep, 16);
995
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200996 if ((dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
William Juul3043c042007-11-14 14:28:11 +0100997 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
wdenkdc7c9a12003-03-26 06:55:25 +0000998 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200999 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +00001000 return 1;
1001 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001002 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +00001003
wdenk7a8e9bed2003-05-31 18:35:21 +00001004 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
William Juulcfa460a2007-10-31 13:53:06 +01001005 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1006 offset);
wdenkdc7c9a12003-03-26 06:55:25 +00001007
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001008 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
Wolfgang Denk4b070802008-08-14 14:41:06 +02001009 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001010 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001011 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +00001012 return 1;
1013 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001014 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001015
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001016 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001017 case IMAGE_FORMAT_LEGACY:
1018 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001019 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001020
Marian Balakowicz09475f72008-03-12 10:33:01 +01001021 cnt = image_get_image_size (hdr);
1022 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001023 break;
1024#if defined(CONFIG_FIT)
1025 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001026 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001027 puts ("Fit image detected...\n");
1028
1029 cnt = fit_get_size (fit_hdr);
1030 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001031#endif
1032 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001033 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001034 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001035 return 1;
1036 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001037 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001038
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001039 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
Wolfgang Denk4b070802008-08-14 14:41:06 +02001040 offset + SECTORSIZE, cnt, NULL,
1041 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001042 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001043 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001044 return 1;
1045 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001046 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001047
Marian Balakowicz09475f72008-03-12 10:33:01 +01001048#if defined(CONFIG_FIT)
1049 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +02001050 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1051 if (!fit_check_format (fit_hdr)) {
1052 show_boot_progress (-150);
1053 puts ("** Bad FIT image format\n");
1054 return 1;
1055 }
1056 show_boot_progress (151);
1057 fit_print_contents (fit_hdr);
1058 }
Marian Balakowicz09475f72008-03-12 10:33:01 +01001059#endif
1060
wdenkdc7c9a12003-03-26 06:55:25 +00001061 /* Loading ok, update default load address */
1062
1063 load_addr = addr;
1064
1065 /* Check if we should attempt an auto-start */
1066 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1067 char *local_args[2];
1068 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1069
1070 local_args[0] = argv[0];
1071 local_args[1] = NULL;
1072
wdenk7a8e9bed2003-05-31 18:35:21 +00001073 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001074
1075 do_bootm (cmdtp, 0, 1, local_args);
1076 rcode = 1;
1077 }
1078 return rcode;
1079}
1080
wdenk0d498392003-07-01 21:06:45 +00001081U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +01001082 nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -06001083 "boot from NAND device",
wdenkb0fce992003-06-29 21:03:46 +00001084 "loadAddr dev\n"
1085);
1086
Jon Loeligerc76fe472007-07-08 18:02:23 -05001087#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001088
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +02001089#endif /* CONFIG_NAND_LEGACY */