blob: 158a55fa705db7534708df308938215b564fa97a [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
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010014/*
15 *
16 * New NAND support
17 *
18 */
19#include <common.h>
William Juulcfa460a2007-10-31 13:53:06 +010020#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010021
Jon Loeligerc76fe472007-07-08 18:02:23 -050022#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010023
24#include <command.h>
25#include <watchdog.h>
26#include <malloc.h>
27#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010028#include <jffs2/jffs2.h>
29#include <nand.h>
30
Ladislav Michl0c8a8492009-04-21 02:26:31 +020031#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020032
33/* parition handling routines */
34int mtdparts_init(void);
35int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk4b070802008-08-14 14:41:06 +020037 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020038#endif
39
William Juul9ad754f2007-12-14 16:33:45 +010040static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010041{
42 int i;
William Juul9ad754f2007-12-14 16:33:45 +010043 u_char *datbuf, *oobbuf, *p;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010044
William Juul9ad754f2007-12-14 16:33:45 +010045 datbuf = malloc(nand->writesize + nand->oobsize);
46 oobbuf = malloc(nand->oobsize);
47 if (!datbuf || !oobbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010048 puts("No memory for page buffer\n");
49 return 1;
50 }
William Juulcfa460a2007-10-31 13:53:06 +010051 off &= ~(nand->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010052 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010053 struct mtd_oob_ops ops;
54 memset(&ops, 0, sizeof(ops));
55 ops.datbuf = datbuf;
56 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
57 ops.len = nand->writesize;
58 ops.ooblen = nand->oobsize;
59 ops.mode = MTD_OOB_RAW;
60 i = nand->read_oob(nand, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010061 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020062 printf("Error (%d) reading page %08lx\n", i, off);
William Juul9ad754f2007-12-14 16:33:45 +010063 free(datbuf);
64 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010065 return 1;
66 }
Stefan Roesee8706902008-07-10 10:10:54 +020067 printf("Page %08lx dump:\n", off);
William Juul9ad754f2007-12-14 16:33:45 +010068 i = nand->writesize >> 4;
69 p = datbuf;
Wolfgang Denk4b070802008-08-14 14:41:06 +020070
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010071 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010072 if (!only_oob)
73 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
74 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
75 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Wooddfbf6172008-06-12 13:20:16 -050076 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
77 p[15]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010078 p += 16;
79 }
80 puts("OOB:\n");
81 i = nand->oobsize >> 3;
82 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010083 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
84 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010085 p += 8;
86 }
William Juul9ad754f2007-12-14 16:33:45 +010087 free(datbuf);
88 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010089
90 return 0;
91}
92
93/* ------------------------------------------------------------------------- */
94
Stefan Roese856f0542006-10-28 15:55:52 +020095static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010096{
Stefan Roese856f0542006-10-28 15:55:52 +020097 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010098
Stefan Roese856f0542006-10-28 15:55:52 +020099 *num = simple_strtoul(p, &endptr, 16);
100 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
101}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102
Stefan Roese856f0542006-10-28 15:55:52 +0200103static int
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200104arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +0200105{
106 int idx = nand_curr_device;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200107#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200108 struct mtd_device *dev;
109 struct part_info *part;
110 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100111
Stefan Roese856f0542006-10-28 15:55:52 +0200112 if (argc >= 1 && !(str2long(argv[0], off))) {
113 if ((mtdparts_init() == 0) &&
114 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
115 if (dev->id->type != MTD_DEV_TYPE_NAND) {
116 puts("not a NAND device\n");
117 return -1;
118 }
119 *off = part->offset;
120 if (argc >= 2) {
Wolfgang Denk42ffcec2008-04-30 17:46:26 +0200121 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200122 printf("'%s' is not a number\n", argv[1]);
123 return -1;
124 }
125 if (*size > part->size)
126 *size = part->size;
127 } else {
128 *size = part->size;
129 }
130 idx = dev->id->num;
131 *nand = nand_info[idx];
132 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100133 }
Stefan Roese856f0542006-10-28 15:55:52 +0200134 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100135#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100136
Stefan Roese856f0542006-10-28 15:55:52 +0200137 if (argc >= 1) {
138 if (!(str2long(argv[0], off))) {
139 printf("'%s' is not a number\n", argv[0]);
140 return -1;
141 }
142 } else {
143 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100144 }
145
Stefan Roese856f0542006-10-28 15:55:52 +0200146 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200147 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200148 printf("'%s' is not a number\n", argv[1]);
149 return -1;
150 }
151 } else {
152 *size = nand->size - *off;
153 }
154
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200155#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200156out:
157#endif
158 printf("device %d ", idx);
159 if (*size == nand->size)
160 puts("whole chip\n");
161 else
Mike Frysingerd4bade82009-01-18 19:46:06 -0500162 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
Stefan Roese856f0542006-10-28 15:55:52 +0200163 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100164}
165
Nishanth Menon50657c22008-12-13 09:43:06 -0600166#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
167static void print_status(ulong start, ulong end, ulong erasesize, int status)
168{
169 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
170 start,
171 end - 1,
172 (end - start) / erasesize,
173 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
174 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
175 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
176}
177
178static void do_nand_status(nand_info_t *nand)
179{
180 ulong block_start = 0;
181 ulong off;
182 int last_status = -1;
183
184 struct nand_chip *nand_chip = nand->priv;
185 /* check the WP bit */
186 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
187 printf("device is %swrite protected\n",
188 (nand_chip->read_byte(nand) & 0x80 ?
189 "NOT " : ""));
190
191 for (off = 0; off < nand->size; off += nand->erasesize) {
192 int s = nand_get_lock_status(nand, off);
193
194 /* print message only if status has changed */
195 if (s != last_status && off != 0) {
196 print_status(block_start, off, nand->erasesize,
197 last_status);
198 block_start = off;
199 }
200 last_status = s;
201 }
202 /* Print the last block info */
203 print_status(block_start, off, nand->erasesize, last_status);
204}
205#endif
206
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100207static void nand_print_info(int idx)
208{
209 nand_info_t *nand = &nand_info[idx];
210 struct nand_chip *chip = nand->priv;
211 printf("Device %d: ", idx);
212 if (chip->numchips > 1)
213 printf("%dx ", chip->numchips);
214 printf("%s, sector size %u KiB\n",
215 nand->name, nand->erasesize >> 10);
216}
217
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100218int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
219{
William Juulcfa460a2007-10-31 13:53:06 +0100220 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200221 ulong addr, off;
222 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100223 char *cmd, *s;
224 nand_info_t *nand;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200225#ifdef CONFIG_SYS_NAND_QUIET
226 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200227#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200228 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200229#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200230 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100231
232 /* at least two arguments please */
233 if (argc < 2)
234 goto usage;
235
Stefan Roese2255b2d2006-10-10 12:36:02 +0200236 if (quiet_str)
237 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
238
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100239 cmd = argv[1];
240
241 if (strcmp(cmd, "info") == 0) {
242
243 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200244 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100245 if (nand_info[i].name)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100246 nand_print_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100247 }
248 return 0;
249 }
250
251 if (strcmp(cmd, "device") == 0) {
252
253 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100254 putc('\n');
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100255 if ((nand_curr_device < 0) ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200256 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100257 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100258 else
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100259 nand_print_info(nand_curr_device);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100260 return 0;
261 }
262 dev = (int)simple_strtoul(argv[2], NULL, 10);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200263 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100264 puts("No such device\n");
265 return 1;
266 }
267 printf("Device %d: %s", dev, nand_info[dev].name);
268 puts("... is now current device\n");
269 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200270
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200271#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200272 /*
273 * Select the chip in the board/cpu specific driver
274 */
275 board_nand_select_device(nand_info[dev].priv, dev);
276#endif
277
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100278 return 0;
279 }
280
281 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juul3043c042007-11-14 14:28:11 +0100282 strncmp(cmd, "dump", 4) != 0 &&
283 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
284 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
285 strcmp(cmd, "biterr") != 0 &&
286 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100287 goto usage;
288
289 /* the following commands operate on the current device */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200290 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100291 !nand_info[nand_curr_device].name) {
292 puts("\nno devices available\n");
293 return 1;
294 }
295 nand = &nand_info[nand_curr_device];
296
297 if (strcmp(cmd, "bad") == 0) {
298 printf("\nDevice %d bad blocks:\n", nand_curr_device);
299 for (off = 0; off < nand->size; off += nand->erasesize)
300 if (nand_block_isbad(nand, off))
Stefan Roesee8706902008-07-10 10:10:54 +0200301 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100302 return 0;
303 }
304
Stefan Roese856f0542006-10-28 15:55:52 +0200305 /*
306 * Syntax is:
307 * 0 1 2 3 4
308 * nand erase [clean] [off size]
309 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200310 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
311 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200312 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100313 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200314 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200315 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100316
Stefan Roese856f0542006-10-28 15:55:52 +0200317 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
318 /* skip first two or three arguments, look for offset and size */
319 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
320 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200321
322 memset(&opts, 0, sizeof(opts));
323 opts.offset = off;
324 opts.length = size;
325 opts.jffs2 = clean;
326 opts.quiet = quiet;
327
328 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200329 puts("Warning: "
330 "scrub option will erase all factory set "
331 "bad blocks!\n"
332 " "
333 "There is no reliable way to recover them.\n"
334 " "
335 "Use this command only for testing purposes "
336 "if you\n"
337 " "
338 "are sure of what you are doing!\n"
339 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200340
341 if (getc() == 'y' && getc() == '\r') {
342 opts.scrub = 1;
343 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200344 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200345 return -1;
346 }
347 }
348 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100349 printf("%s\n", ret ? "ERROR" : "OK");
350
351 return ret == 0 ? 0 : 1;
352 }
353
354 if (strncmp(cmd, "dump", 4) == 0) {
355 if (argc < 3)
356 goto usage;
357
358 s = strchr(cmd, '.');
359 off = (int)simple_strtoul(argv[2], NULL, 16);
360
361 if (s != NULL && strcmp(s, ".oob") == 0)
William Juul9ad754f2007-12-14 16:33:45 +0100362 ret = nand_dump(nand, off, 1);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100363 else
William Juul9ad754f2007-12-14 16:33:45 +0100364 ret = nand_dump(nand, off, 0);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100365
366 return ret == 0 ? 1 : 0;
367
368 }
369
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100370 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200371 int read;
372
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100373 if (argc < 4)
374 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200375
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100376 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
377
Stefan Roese2255b2d2006-10-10 12:36:02 +0200378 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200379 printf("\nNAND %s: ", read ? "read" : "write");
380 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
381 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100382
Stefan Roese2255b2d2006-10-10 12:36:02 +0200383 s = strchr(cmd, '.');
Scott Wood984e03c2008-06-12 13:13:23 -0500384 if (!s || !strcmp(s, ".jffs2") ||
385 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500386 if (read)
387 ret = nand_read_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200388 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500389 else
390 ret = nand_write_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200391 (u_char *)addr);
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400392 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100393 /* out-of-band data */
394 mtd_oob_ops_t ops = {
395 .oobbuf = (u8 *)addr,
396 .ooblen = size,
397 .mode = MTD_OOB_RAW
398 };
399
Harald Welte62d4f432007-12-19 14:12:53 +0100400 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100401 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100402 else
William Juulcfa460a2007-10-31 13:53:06 +0100403 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200404 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500405 printf("Unknown nand command suffix '%s'.\n", s);
406 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200407 }
408
Mike Frysingerd4bade82009-01-18 19:46:06 -0500409 printf(" %zu bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200410 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100411
412 return ret == 0 ? 0 : 1;
413 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200414
Stefan Roese2255b2d2006-10-10 12:36:02 +0200415 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200416 argc -= 2;
417 argv += 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200418
Wolfgang Denk8360b662009-05-24 17:34:33 +0200419 if (argc <= 0)
420 goto usage;
421
422 while (argc > 0) {
423 addr = simple_strtoul(*argv, NULL, 16);
424
425 if (nand->block_markbad(nand, addr)) {
426 printf("block 0x%08lx NOT marked "
427 "as bad! ERROR %d\n",
428 addr, ret);
429 ret = 1;
430 } else {
431 printf("block 0x%08lx successfully "
432 "marked as bad\n",
433 addr);
434 }
435 --argc;
436 ++argv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200437 }
Wolfgang Denk8360b662009-05-24 17:34:33 +0200438 return ret;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200439 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500440
Stefan Roese2255b2d2006-10-10 12:36:02 +0200441 if (strcmp(cmd, "biterr") == 0) {
442 /* todo */
443 return 1;
444 }
445
Nishanth Menon50657c22008-12-13 09:43:06 -0600446#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200447 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600448 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200449 int status = 0;
450 if (argc == 3) {
451 if (!strcmp("tight", argv[2]))
452 tight = 1;
453 if (!strcmp("status", argv[2]))
454 status = 1;
455 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200456 if (status) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600457 do_nand_status(nand);
William Juulcfa460a2007-10-31 13:53:06 +0100458 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100459 if (!nand_lock(nand, tight)) {
460 puts("NAND flash successfully locked\n");
461 } else {
462 puts("Error locking NAND flash\n");
463 return 1;
464 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200465 }
466 return 0;
467 }
468
469 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200470 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
471 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200472
William Juul5e1dae52007-11-09 13:32:30 +0100473 if (!nand_unlock(nand, off, size)) {
474 puts("NAND flash successfully unlocked\n");
475 } else {
476 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100477 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100478 return 1;
479 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200480 return 0;
481 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600482#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200483
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100484usage:
Peter Tyser62c3ae72009-01-27 18:03:10 -0600485 cmd_usage(cmdtp);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100486 return 1;
487}
488
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200489U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
490 "NAND sub-system",
491 "info - show available NAND devices\n"
492 "nand device [dev] - show or set current device\n"
493 "nand read - addr off|partition size\n"
494 "nand write - addr off|partition size\n"
495 " read/write 'size' bytes starting at offset 'off'\n"
496 " to/from memory address 'addr', skipping bad blocks.\n"
497 "nand erase [clean] [off size] - erase 'size' bytes from\n"
498 " offset 'off' (entire device if not specified)\n"
499 "nand bad - show bad blocks\n"
500 "nand dump[.oob] off - dump page\n"
501 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
502 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
503 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menon50657c22008-12-13 09:43:06 -0600504#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200505 "\n"
506 "nand lock [tight] [status]\n"
507 " bring nand to lock state or display locked pages\n"
508 "nand unlock [offset] [size] - unlock section"
Nishanth Menon50657c22008-12-13 09:43:06 -0600509#endif
510);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100511
Stefan Roese856f0542006-10-28 15:55:52 +0200512static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200513 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200514{
515 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200516 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200517 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200518 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100519#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200520 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100521#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200522
523 s = strchr(cmd, '.');
524 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500525 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500526 printf("Unknown nand load suffix '%s'\n", s);
527 show_boot_progress(-53);
528 return 1;
529 }
Stefan Roese856f0542006-10-28 15:55:52 +0200530
531 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
532
William Juulcfa460a2007-10-31 13:53:06 +0100533 cnt = nand->writesize;
Ladislav Michl09875052009-03-13 14:38:19 +0100534 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200535 if (r) {
536 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200537 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200538 return 1;
539 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200540 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200541
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100542 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100543 case IMAGE_FORMAT_LEGACY:
544 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200545
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100546 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100547 image_print_contents (hdr);
548
549 cnt = image_get_image_size (hdr);
550 break;
551#if defined(CONFIG_FIT)
552 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100553 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100554 puts ("Fit image detected...\n");
555
556 cnt = fit_get_size (fit_hdr);
557 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100558#endif
559 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100560 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100561 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200562 return 1;
563 }
William Juul3043c042007-11-14 14:28:11 +0100564 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200565
Ladislav Michl09875052009-03-13 14:38:19 +0100566 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200567 if (r) {
568 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200569 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200570 return 1;
571 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200572 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200573
Marian Balakowicz09475f72008-03-12 10:33:01 +0100574#if defined(CONFIG_FIT)
575 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200576 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
577 if (!fit_check_format (fit_hdr)) {
578 show_boot_progress (-150);
579 puts ("** Bad FIT image format\n");
580 return 1;
581 }
582 show_boot_progress (151);
583 fit_print_contents (fit_hdr);
584 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100585#endif
586
Stefan Roese856f0542006-10-28 15:55:52 +0200587 /* Loading ok, update default load address */
588
589 load_addr = addr;
590
591 /* Check if we should attempt an auto-start */
592 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
593 char *local_args[2];
594 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
595
596 local_args[0] = cmd;
597 local_args[1] = NULL;
598
599 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
600
601 do_bootm(cmdtp, 0, 1, local_args);
602 return 1;
603 }
604 return 0;
605}
606
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100607int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
608{
609 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200610 int idx;
611 ulong addr, offset = 0;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200612#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200613 struct mtd_device *dev;
614 struct part_info *part;
615 u8 pnum;
616
617 if (argc >= 2) {
618 char *p = (argc == 2) ? argv[1] : argv[2];
619 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
620 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
621 if (dev->id->type != MTD_DEV_TYPE_NAND) {
622 puts("Not a NAND device\n");
623 return 1;
624 }
625 if (argc > 3)
626 goto usage;
627 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200628 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200629 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200630 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese856f0542006-10-28 15:55:52 +0200631 return nand_load_image(cmdtp, &nand_info[dev->id->num],
Wolfgang Denk4b070802008-08-14 14:41:06 +0200632 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200633 }
634 }
635#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100636
Heiko Schocherfad63402007-07-13 09:54:17 +0200637 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100638 switch (argc) {
639 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200640 addr = CONFIG_SYS_LOAD_ADDR;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100641 boot_device = getenv("bootdevice");
642 break;
643 case 2:
644 addr = simple_strtoul(argv[1], NULL, 16);
645 boot_device = getenv("bootdevice");
646 break;
647 case 3:
648 addr = simple_strtoul(argv[1], NULL, 16);
649 boot_device = argv[2];
650 break;
651 case 4:
652 addr = simple_strtoul(argv[1], NULL, 16);
653 boot_device = argv[2];
654 offset = simple_strtoul(argv[3], NULL, 16);
655 break;
656 default:
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200657#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200658usage:
659#endif
Peter Tyser62c3ae72009-01-27 18:03:10 -0600660 cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200661 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100662 return 1;
663 }
664
Heiko Schocherfad63402007-07-13 09:54:17 +0200665 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100666 if (!boot_device) {
667 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200668 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100669 return 1;
670 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200671 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100672
Stefan Roese856f0542006-10-28 15:55:52 +0200673 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100674
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200675 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
Stefan Roese856f0542006-10-28 15:55:52 +0200676 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200677 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100678 return 1;
679 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200680 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100681
Stefan Roese856f0542006-10-28 15:55:52 +0200682 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100683}
684
685U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600686 "boot from NAND device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200687 "[partition] | [[[loadAddr] dev] offset]"
688);
Jon Loeligerc76fe472007-07-08 18:02:23 -0500689#endif