blob: 9b0c9305306f7b6c3f55a93e185606b07067a85a [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>
William Juulcfa460a2007-10-31 13:53:06 +010012#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010013#include <command.h>
14#include <watchdog.h>
15#include <malloc.h>
16#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010017#include <jffs2/jffs2.h>
18#include <nand.h>
19
Ladislav Michl0c8a8492009-04-21 02:26:31 +020020#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020021
22/* parition handling routines */
23int mtdparts_init(void);
24int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
25int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk4b070802008-08-14 14:41:06 +020026 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020027#endif
28
William Juul9ad754f2007-12-14 16:33:45 +010029static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010030{
31 int i;
William Juul9ad754f2007-12-14 16:33:45 +010032 u_char *datbuf, *oobbuf, *p;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010033
William Juul9ad754f2007-12-14 16:33:45 +010034 datbuf = malloc(nand->writesize + nand->oobsize);
35 oobbuf = malloc(nand->oobsize);
36 if (!datbuf || !oobbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010037 puts("No memory for page buffer\n");
38 return 1;
39 }
William Juulcfa460a2007-10-31 13:53:06 +010040 off &= ~(nand->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010041 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010042 struct mtd_oob_ops ops;
43 memset(&ops, 0, sizeof(ops));
44 ops.datbuf = datbuf;
45 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
46 ops.len = nand->writesize;
47 ops.ooblen = nand->oobsize;
48 ops.mode = MTD_OOB_RAW;
49 i = nand->read_oob(nand, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010050 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020051 printf("Error (%d) reading page %08lx\n", i, off);
William Juul9ad754f2007-12-14 16:33:45 +010052 free(datbuf);
53 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010054 return 1;
55 }
Stefan Roesee8706902008-07-10 10:10:54 +020056 printf("Page %08lx dump:\n", off);
William Juul9ad754f2007-12-14 16:33:45 +010057 i = nand->writesize >> 4;
58 p = datbuf;
Wolfgang Denk4b070802008-08-14 14:41:06 +020059
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010060 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010061 if (!only_oob)
62 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
63 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
64 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Wooddfbf6172008-06-12 13:20:16 -050065 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
66 p[15]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010067 p += 16;
68 }
69 puts("OOB:\n");
70 i = nand->oobsize >> 3;
71 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010072 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
73 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010074 p += 8;
75 }
William Juul9ad754f2007-12-14 16:33:45 +010076 free(datbuf);
77 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010078
79 return 0;
80}
81
82/* ------------------------------------------------------------------------- */
83
Stefan Roese856f0542006-10-28 15:55:52 +020084static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010085{
Stefan Roese856f0542006-10-28 15:55:52 +020086 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010087
Stefan Roese856f0542006-10-28 15:55:52 +020088 *num = simple_strtoul(p, &endptr, 16);
89 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
90}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010091
Stefan Roese856f0542006-10-28 15:55:52 +020092static int
Wolfgang Denk4ca79f42008-04-28 12:08:18 +020093arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +020094{
95 int idx = nand_curr_device;
Ladislav Michl0c8a8492009-04-21 02:26:31 +020096#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020097 struct mtd_device *dev;
98 struct part_info *part;
99 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100100
Stefan Roese856f0542006-10-28 15:55:52 +0200101 if (argc >= 1 && !(str2long(argv[0], off))) {
102 if ((mtdparts_init() == 0) &&
103 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
104 if (dev->id->type != MTD_DEV_TYPE_NAND) {
105 puts("not a NAND device\n");
106 return -1;
107 }
108 *off = part->offset;
109 if (argc >= 2) {
Wolfgang Denk42ffcec2008-04-30 17:46:26 +0200110 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200111 printf("'%s' is not a number\n", argv[1]);
112 return -1;
113 }
114 if (*size > part->size)
115 *size = part->size;
116 } else {
117 *size = part->size;
118 }
119 idx = dev->id->num;
120 *nand = nand_info[idx];
121 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100122 }
Stefan Roese856f0542006-10-28 15:55:52 +0200123 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100124#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100125
Stefan Roese856f0542006-10-28 15:55:52 +0200126 if (argc >= 1) {
127 if (!(str2long(argv[0], off))) {
128 printf("'%s' is not a number\n", argv[0]);
129 return -1;
130 }
131 } else {
132 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100133 }
134
Stefan Roese856f0542006-10-28 15:55:52 +0200135 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200136 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200137 printf("'%s' is not a number\n", argv[1]);
138 return -1;
139 }
140 } else {
141 *size = nand->size - *off;
142 }
143
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200144#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200145out:
146#endif
147 printf("device %d ", idx);
148 if (*size == nand->size)
149 puts("whole chip\n");
150 else
Mike Frysingerd4bade82009-01-18 19:46:06 -0500151 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
Stefan Roese856f0542006-10-28 15:55:52 +0200152 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100153}
154
Nishanth Menon50657c22008-12-13 09:43:06 -0600155#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
156static void print_status(ulong start, ulong end, ulong erasesize, int status)
157{
158 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
159 start,
160 end - 1,
161 (end - start) / erasesize,
162 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
163 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
164 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
165}
166
167static void do_nand_status(nand_info_t *nand)
168{
169 ulong block_start = 0;
170 ulong off;
171 int last_status = -1;
172
173 struct nand_chip *nand_chip = nand->priv;
174 /* check the WP bit */
175 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
176 printf("device is %swrite protected\n",
177 (nand_chip->read_byte(nand) & 0x80 ?
178 "NOT " : ""));
179
180 for (off = 0; off < nand->size; off += nand->erasesize) {
181 int s = nand_get_lock_status(nand, off);
182
183 /* print message only if status has changed */
184 if (s != last_status && off != 0) {
185 print_status(block_start, off, nand->erasesize,
186 last_status);
187 block_start = off;
188 }
189 last_status = s;
190 }
191 /* Print the last block info */
192 print_status(block_start, off, nand->erasesize, last_status);
193}
194#endif
195
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100196static void nand_print_info(int idx)
197{
198 nand_info_t *nand = &nand_info[idx];
199 struct nand_chip *chip = nand->priv;
200 printf("Device %d: ", idx);
201 if (chip->numchips > 1)
202 printf("%dx ", chip->numchips);
203 printf("%s, sector size %u KiB\n",
204 nand->name, nand->erasesize >> 10);
205}
206
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100207int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
208{
William Juulcfa460a2007-10-31 13:53:06 +0100209 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200210 ulong addr, off;
211 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100212 char *cmd, *s;
213 nand_info_t *nand;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200214#ifdef CONFIG_SYS_NAND_QUIET
215 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200216#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200217 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200218#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200219 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100220
221 /* at least two arguments please */
222 if (argc < 2)
223 goto usage;
224
Stefan Roese2255b2d2006-10-10 12:36:02 +0200225 if (quiet_str)
226 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
227
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100228 cmd = argv[1];
229
230 if (strcmp(cmd, "info") == 0) {
231
232 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200233 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100234 if (nand_info[i].name)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100235 nand_print_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100236 }
237 return 0;
238 }
239
240 if (strcmp(cmd, "device") == 0) {
241
242 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100243 putc('\n');
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100244 if ((nand_curr_device < 0) ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200245 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100246 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100247 else
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100248 nand_print_info(nand_curr_device);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100249 return 0;
250 }
251 dev = (int)simple_strtoul(argv[2], NULL, 10);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200252 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100253 puts("No such device\n");
254 return 1;
255 }
256 printf("Device %d: %s", dev, nand_info[dev].name);
257 puts("... is now current device\n");
258 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200259
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200260#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200261 /*
262 * Select the chip in the board/cpu specific driver
263 */
264 board_nand_select_device(nand_info[dev].priv, dev);
265#endif
266
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100267 return 0;
268 }
269
270 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juul3043c042007-11-14 14:28:11 +0100271 strncmp(cmd, "dump", 4) != 0 &&
272 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
273 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
274 strcmp(cmd, "biterr") != 0 &&
275 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100276 goto usage;
277
278 /* the following commands operate on the current device */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200279 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100280 !nand_info[nand_curr_device].name) {
281 puts("\nno devices available\n");
282 return 1;
283 }
284 nand = &nand_info[nand_curr_device];
285
286 if (strcmp(cmd, "bad") == 0) {
287 printf("\nDevice %d bad blocks:\n", nand_curr_device);
288 for (off = 0; off < nand->size; off += nand->erasesize)
289 if (nand_block_isbad(nand, off))
Stefan Roesee8706902008-07-10 10:10:54 +0200290 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100291 return 0;
292 }
293
Stefan Roese856f0542006-10-28 15:55:52 +0200294 /*
295 * Syntax is:
296 * 0 1 2 3 4
297 * nand erase [clean] [off size]
298 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200299 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
300 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200301 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100302 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200303 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200304 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100305
Stefan Roese856f0542006-10-28 15:55:52 +0200306 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
307 /* skip first two or three arguments, look for offset and size */
308 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
309 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200310
311 memset(&opts, 0, sizeof(opts));
312 opts.offset = off;
313 opts.length = size;
314 opts.jffs2 = clean;
315 opts.quiet = quiet;
316
317 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200318 puts("Warning: "
319 "scrub option will erase all factory set "
320 "bad blocks!\n"
321 " "
322 "There is no reliable way to recover them.\n"
323 " "
324 "Use this command only for testing purposes "
325 "if you\n"
326 " "
327 "are sure of what you are doing!\n"
328 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200329
Florian Fainelli6b94b492010-03-20 19:02:58 +0100330 if (getc() == 'y') {
331 puts("y");
332 if (getc() == '\r')
333 opts.scrub = 1;
334 else {
335 puts("scrub aborted\n");
336 return -1;
337 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200338 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200339 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200340 return -1;
341 }
342 }
343 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100344 printf("%s\n", ret ? "ERROR" : "OK");
345
346 return ret == 0 ? 0 : 1;
347 }
348
349 if (strncmp(cmd, "dump", 4) == 0) {
350 if (argc < 3)
351 goto usage;
352
353 s = strchr(cmd, '.');
354 off = (int)simple_strtoul(argv[2], NULL, 16);
355
356 if (s != NULL && strcmp(s, ".oob") == 0)
William Juul9ad754f2007-12-14 16:33:45 +0100357 ret = nand_dump(nand, off, 1);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100358 else
William Juul9ad754f2007-12-14 16:33:45 +0100359 ret = nand_dump(nand, off, 0);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100360
361 return ret == 0 ? 1 : 0;
362
363 }
364
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100365 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200366 int read;
367
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100368 if (argc < 4)
369 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200370
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100371 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
372
Stefan Roese2255b2d2006-10-10 12:36:02 +0200373 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200374 printf("\nNAND %s: ", read ? "read" : "write");
375 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
376 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100377
Stefan Roese2255b2d2006-10-10 12:36:02 +0200378 s = strchr(cmd, '.');
Scott Wood984e03c2008-06-12 13:13:23 -0500379 if (!s || !strcmp(s, ".jffs2") ||
380 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500381 if (read)
382 ret = nand_read_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200383 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500384 else
385 ret = nand_write_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200386 (u_char *)addr);
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400387 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100388 /* out-of-band data */
389 mtd_oob_ops_t ops = {
390 .oobbuf = (u8 *)addr,
391 .ooblen = size,
392 .mode = MTD_OOB_RAW
393 };
394
Harald Welte62d4f432007-12-19 14:12:53 +0100395 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100396 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100397 else
William Juulcfa460a2007-10-31 13:53:06 +0100398 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200399 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500400 printf("Unknown nand command suffix '%s'.\n", s);
401 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200402 }
403
Mike Frysingerd4bade82009-01-18 19:46:06 -0500404 printf(" %zu bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200405 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100406
407 return ret == 0 ? 0 : 1;
408 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200409
Stefan Roese2255b2d2006-10-10 12:36:02 +0200410 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200411 argc -= 2;
412 argv += 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200413
Wolfgang Denk8360b662009-05-24 17:34:33 +0200414 if (argc <= 0)
415 goto usage;
416
417 while (argc > 0) {
418 addr = simple_strtoul(*argv, NULL, 16);
419
420 if (nand->block_markbad(nand, addr)) {
421 printf("block 0x%08lx NOT marked "
422 "as bad! ERROR %d\n",
423 addr, ret);
424 ret = 1;
425 } else {
426 printf("block 0x%08lx successfully "
427 "marked as bad\n",
428 addr);
429 }
430 --argc;
431 ++argv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200432 }
Wolfgang Denk8360b662009-05-24 17:34:33 +0200433 return ret;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200434 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500435
Stefan Roese2255b2d2006-10-10 12:36:02 +0200436 if (strcmp(cmd, "biterr") == 0) {
437 /* todo */
438 return 1;
439 }
440
Nishanth Menon50657c22008-12-13 09:43:06 -0600441#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200442 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600443 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200444 int status = 0;
445 if (argc == 3) {
446 if (!strcmp("tight", argv[2]))
447 tight = 1;
448 if (!strcmp("status", argv[2]))
449 status = 1;
450 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200451 if (status) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600452 do_nand_status(nand);
William Juulcfa460a2007-10-31 13:53:06 +0100453 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100454 if (!nand_lock(nand, tight)) {
455 puts("NAND flash successfully locked\n");
456 } else {
457 puts("Error locking NAND flash\n");
458 return 1;
459 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200460 }
461 return 0;
462 }
463
464 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200465 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
466 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200467
William Juul5e1dae52007-11-09 13:32:30 +0100468 if (!nand_unlock(nand, off, size)) {
469 puts("NAND flash successfully unlocked\n");
470 } else {
471 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100472 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100473 return 1;
474 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200475 return 0;
476 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600477#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200478
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100479usage:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600480 cmd_usage(cmdtp);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100481 return 1;
482}
483
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200484U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
485 "NAND sub-system",
486 "info - show available NAND devices\n"
487 "nand device [dev] - show or set current device\n"
488 "nand read - addr off|partition size\n"
489 "nand write - addr off|partition size\n"
490 " read/write 'size' bytes starting at offset 'off'\n"
491 " to/from memory address 'addr', skipping bad blocks.\n"
492 "nand erase [clean] [off size] - erase 'size' bytes from\n"
493 " offset 'off' (entire device if not specified)\n"
494 "nand bad - show bad blocks\n"
495 "nand dump[.oob] off - dump page\n"
496 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
497 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
498 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menon50657c22008-12-13 09:43:06 -0600499#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200500 "\n"
501 "nand lock [tight] [status]\n"
502 " bring nand to lock state or display locked pages\n"
503 "nand unlock [offset] [size] - unlock section"
Nishanth Menon50657c22008-12-13 09:43:06 -0600504#endif
505);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100506
Stefan Roese856f0542006-10-28 15:55:52 +0200507static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200508 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200509{
510 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200511 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200512 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200513 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100514#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200515 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100516#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200517
518 s = strchr(cmd, '.');
519 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500520 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500521 printf("Unknown nand load suffix '%s'\n", s);
522 show_boot_progress(-53);
523 return 1;
524 }
Stefan Roese856f0542006-10-28 15:55:52 +0200525
526 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
527
William Juulcfa460a2007-10-31 13:53:06 +0100528 cnt = nand->writesize;
Ladislav Michl09875052009-03-13 14:38:19 +0100529 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200530 if (r) {
531 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200532 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200533 return 1;
534 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200535 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200536
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100537 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100538 case IMAGE_FORMAT_LEGACY:
539 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200540
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100541 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100542 image_print_contents (hdr);
543
544 cnt = image_get_image_size (hdr);
545 break;
546#if defined(CONFIG_FIT)
547 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100548 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100549 puts ("Fit image detected...\n");
550
551 cnt = fit_get_size (fit_hdr);
552 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100553#endif
554 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100555 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100556 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200557 return 1;
558 }
William Juul3043c042007-11-14 14:28:11 +0100559 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200560
Ladislav Michl09875052009-03-13 14:38:19 +0100561 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200562 if (r) {
563 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200564 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200565 return 1;
566 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200567 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200568
Marian Balakowicz09475f72008-03-12 10:33:01 +0100569#if defined(CONFIG_FIT)
570 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200571 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
572 if (!fit_check_format (fit_hdr)) {
573 show_boot_progress (-150);
574 puts ("** Bad FIT image format\n");
575 return 1;
576 }
577 show_boot_progress (151);
578 fit_print_contents (fit_hdr);
579 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100580#endif
581
Stefan Roese856f0542006-10-28 15:55:52 +0200582 /* Loading ok, update default load address */
583
584 load_addr = addr;
585
586 /* Check if we should attempt an auto-start */
587 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
588 char *local_args[2];
589 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
590
591 local_args[0] = cmd;
592 local_args[1] = NULL;
593
594 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
595
596 do_bootm(cmdtp, 0, 1, local_args);
597 return 1;
598 }
599 return 0;
600}
601
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100602int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
603{
604 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200605 int idx;
606 ulong addr, offset = 0;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200607#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200608 struct mtd_device *dev;
609 struct part_info *part;
610 u8 pnum;
611
612 if (argc >= 2) {
613 char *p = (argc == 2) ? argv[1] : argv[2];
614 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
615 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
616 if (dev->id->type != MTD_DEV_TYPE_NAND) {
617 puts("Not a NAND device\n");
618 return 1;
619 }
620 if (argc > 3)
621 goto usage;
622 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200623 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200624 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200625 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese856f0542006-10-28 15:55:52 +0200626 return nand_load_image(cmdtp, &nand_info[dev->id->num],
Wolfgang Denk4b070802008-08-14 14:41:06 +0200627 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200628 }
629 }
630#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100631
Heiko Schocherfad63402007-07-13 09:54:17 +0200632 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100633 switch (argc) {
634 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200635 addr = CONFIG_SYS_LOAD_ADDR;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100636 boot_device = getenv("bootdevice");
637 break;
638 case 2:
639 addr = simple_strtoul(argv[1], NULL, 16);
640 boot_device = getenv("bootdevice");
641 break;
642 case 3:
643 addr = simple_strtoul(argv[1], NULL, 16);
644 boot_device = argv[2];
645 break;
646 case 4:
647 addr = simple_strtoul(argv[1], NULL, 16);
648 boot_device = argv[2];
649 offset = simple_strtoul(argv[3], NULL, 16);
650 break;
651 default:
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200652#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200653usage:
654#endif
Peter Tyser62c3ae72009-01-27 18:03:10 -0600655 cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200656 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100657 return 1;
658 }
659
Heiko Schocherfad63402007-07-13 09:54:17 +0200660 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100661 if (!boot_device) {
662 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200663 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100664 return 1;
665 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200666 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100667
Stefan Roese856f0542006-10-28 15:55:52 +0200668 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100669
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200670 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
Stefan Roese856f0542006-10-28 15:55:52 +0200671 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200672 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100673 return 1;
674 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200675 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100676
Stefan Roese856f0542006-10-28 15:55:52 +0200677 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100678}
679
680U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600681 "boot from NAND device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200682 "[partition] | [[[loadAddr] dev] offset]"
683);