]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/nand.c
nand: Embed mtd_info in struct nand_chip
[people/ms/u-boot.git] / cmd / nand.c
CommitLineData
dc7c9a1a
WD
1/*
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>
384cc687 6 *
c9f7351b
BG
7 * Ported 'dynenv' to 'nand env.oob' command
8 * (C) 2010 Nanometrics, Inc.
9 * 'dynenv' -- Dynamic environment offset in NAND OOB
10 * (C) Copyright 2006-2007 OpenMoko, Inc.
384cc687
WD
11 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
ea533c26 13 *
418396e2 14 * Copyright 2010, 2012 Freescale Semiconductor
ea533c26
SW
15 * The portions of this file whose copyright is held by Freescale and which
16 * are not considered a derived work of GPL v2-only code may be distributed
17 * and/or modified under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of the
19 * License, or (at your option) any later version.
dc7c9a1a
WD
20 */
21
22#include <common.h>
cfa460ad 23#include <linux/mtd/mtd.h>
addb2e16 24#include <command.h>
24b852a7 25#include <console.h>
addb2e16
BS
26#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
addb2e16
BS
29#include <jffs2/jffs2.h>
30#include <nand.h>
31
0c8a8491 32#if defined(CONFIG_CMD_MTDPARTS)
856f0544 33
445093d1 34/* partition handling routines */
856f0544
SR
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,
4b070809 38 u8 *part_num, struct part_info **part);
856f0544
SR
39#endif
40
151c06ec
SW
41static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
42 int repeat)
addb2e16
BS
43{
44 int i;
9ad754fe 45 u_char *datbuf, *oobbuf, *p;
8c5659a6 46 static loff_t last;
e40520b5 47 int ret = 0;
8c5659a6
SW
48
49 if (repeat)
151c06ec 50 off = last + mtd->writesize;
8c5659a6
SW
51
52 last = off;
addb2e16 53
151c06ec 54 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
e40520b5 55 if (!datbuf) {
addb2e16
BS
56 puts("No memory for page buffer\n");
57 return 1;
58 }
e40520b5 59
151c06ec 60 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
e40520b5
MY
61 if (!oobbuf) {
62 puts("No memory for page buffer\n");
63 ret = 1;
64 goto free_dat;
65 }
151c06ec 66 off &= ~(mtd->writesize - 1);
cfa460ad 67 loff_t addr = (loff_t) off;
9ad754fe
WJ
68 struct mtd_oob_ops ops;
69 memset(&ops, 0, sizeof(ops));
70 ops.datbuf = datbuf;
cfdae12f 71 ops.oobbuf = oobbuf;
151c06ec
SW
72 ops.len = mtd->writesize;
73 ops.ooblen = mtd->oobsize;
dfe64e2c 74 ops.mode = MTD_OPS_RAW;
151c06ec 75 i = mtd_read_oob(mtd, addr, &ops);
addb2e16 76 if (i < 0) {
e870690b 77 printf("Error (%d) reading page %08lx\n", i, off);
e40520b5
MY
78 ret = 1;
79 goto free_all;
addb2e16 80 }
e870690b 81 printf("Page %08lx dump:\n", off);
4b070809 82
7d25cd34 83 if (!only_oob) {
151c06ec 84 i = mtd->writesize >> 4;
7d25cd34
MY
85 p = datbuf;
86
87 while (i--) {
9ad754fe
WJ
88 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
89 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
90 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
dfbf617f
SW
91 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
92 p[15]);
7d25cd34
MY
93 p += 16;
94 }
addb2e16 95 }
7d25cd34 96
addb2e16 97 puts("OOB:\n");
151c06ec 98 i = mtd->oobsize >> 3;
cfdae12f 99 p = oobbuf;
addb2e16 100 while (i--) {
cfa460ad
WJ
101 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
102 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
addb2e16
BS
103 p += 8;
104 }
e40520b5
MY
105
106free_all:
9ad754fe 107 free(oobbuf);
e40520b5
MY
108free_dat:
109 free(datbuf);
addb2e16 110
e40520b5 111 return ret;
addb2e16
BS
112}
113
114/* ------------------------------------------------------------------------- */
115
ea533c26
SW
116static int set_dev(int dev)
117{
118 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
b616d9b0 119 !nand_info[dev]->name) {
ea533c26
SW
120 puts("No such device\n");
121 return -1;
122 }
123
124 if (nand_curr_device == dev)
125 return 0;
126
b616d9b0 127 printf("Device %d: %s", dev, nand_info[dev]->name);
ea533c26
SW
128 puts("... is now current device\n");
129 nand_curr_device = dev;
130
131#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
b616d9b0 132 board_nand_select_device(nand_info[dev]->priv, dev);
ea533c26
SW
133#endif
134
135 return 0;
136}
137
50657c27
NM
138#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
139static void print_status(ulong start, ulong end, ulong erasesize, int status)
140{
e70bfa29
JH
141 /*
142 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
143 * not the same as others. Instead of bit 1 being lock, it is
144 * #lock_tight. To make the driver support either format, ignore bit 1
145 * and use only bit 0 and bit 2.
146 */
50657c27
NM
147 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
148 start,
149 end - 1,
150 (end - start) / erasesize,
151 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
e70bfa29 152 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
50657c27
NM
153 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
154}
155
151c06ec 156static void do_nand_status(struct mtd_info *mtd)
50657c27
NM
157{
158 ulong block_start = 0;
159 ulong off;
160 int last_status = -1;
161
151c06ec 162 struct nand_chip *nand_chip = mtd->priv;
50657c27 163 /* check the WP bit */
151c06ec 164 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
50657c27 165 printf("device is %swrite protected\n",
151c06ec
SW
166 (nand_chip->read_byte(mtd) & 0x80 ?
167 "NOT " : ""));
50657c27 168
151c06ec
SW
169 for (off = 0; off < mtd->size; off += mtd->erasesize) {
170 int s = nand_get_lock_status(mtd, off);
50657c27
NM
171
172 /* print message only if status has changed */
173 if (s != last_status && off != 0) {
151c06ec 174 print_status(block_start, off, mtd->erasesize,
50657c27
NM
175 last_status);
176 block_start = off;
177 }
178 last_status = s;
179 }
180 /* Print the last block info */
151c06ec 181 print_status(block_start, off, mtd->erasesize, last_status);
50657c27
NM
182}
183#endif
184
c9f7351b
BG
185#ifdef CONFIG_ENV_OFFSET_OOB
186unsigned long nand_env_oob_offset;
187
ea533c26 188int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
c9f7351b
BG
189{
190 int ret;
191 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
b616d9b0 192 struct mtd_info *mtd = nand_info[0];
c9f7351b
BG
193 char *cmd = argv[1];
194
151c06ec 195 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd->name) {
ea533c26
SW
196 puts("no devices available\n");
197 return 1;
198 }
199
200 set_dev(0);
201
c9f7351b 202 if (!strcmp(cmd, "get")) {
151c06ec 203 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
53504a27 204 if (ret)
c9f7351b 205 return 1;
53504a27
SW
206
207 printf("0x%08lx\n", nand_env_oob_offset);
c9f7351b 208 } else if (!strcmp(cmd, "set")) {
ea533c26
SW
209 loff_t addr;
210 loff_t maxsize;
c9f7351b 211 struct mtd_oob_ops ops;
ea533c26 212 int idx = 0;
c9f7351b
BG
213
214 if (argc < 3)
215 goto usage;
216
c39d6a0e 217 /* We don't care about size, or maxsize. */
09c32807 218 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
b616d9b0 219 MTD_DEV_TYPE_NAND, nand_info[idx]->size)) {
09c32807
HS
220 puts("Offset or partition name expected\n");
221 return 1;
222 }
223 if (set_dev(idx)) {
ea533c26
SW
224 puts("Offset or partition name expected\n");
225 return 1;
226 }
227
228 if (idx != 0) {
229 puts("Partition not on first NAND device\n");
c9f7351b
BG
230 return 1;
231 }
232
151c06ec 233 if (mtd->oobavail < ENV_OFFSET_SIZE) {
53504a27
SW
234 printf("Insufficient available OOB bytes:\n"
235 "%d OOB bytes available but %d required for "
236 "env.oob support\n",
151c06ec 237 mtd->oobavail, ENV_OFFSET_SIZE);
c9f7351b
BG
238 return 1;
239 }
240
151c06ec 241 if ((addr & (mtd->erasesize - 1)) != 0) {
c9f7351b
BG
242 printf("Environment offset must be block-aligned\n");
243 return 1;
244 }
245
246 ops.datbuf = NULL;
247 ops.mode = MTD_OOB_AUTO;
248 ops.ooboffs = 0;
249 ops.ooblen = ENV_OFFSET_SIZE;
250 ops.oobbuf = (void *) oob_buf;
251
252 oob_buf[0] = ENV_OOB_MARKER;
151c06ec 253 oob_buf[1] = addr / mtd->erasesize;
c9f7351b 254
151c06ec 255 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
53504a27 256 if (ret) {
c9f7351b
BG
257 printf("Error writing OOB block 0\n");
258 return ret;
259 }
53504a27 260
151c06ec 261 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
53504a27
SW
262 if (ret) {
263 printf("Error reading env offset in OOB\n");
264 return ret;
265 }
266
267 if (addr != nand_env_oob_offset) {
268 printf("Verification of env offset in OOB failed: "
ea533c26
SW
269 "0x%08llx expected but got 0x%08lx\n",
270 (unsigned long long)addr, nand_env_oob_offset);
53504a27
SW
271 return 1;
272 }
c9f7351b
BG
273 } else {
274 goto usage;
275 }
276
277 return ret;
278
279usage:
4c12eeb8 280 return CMD_RET_USAGE;
c9f7351b
BG
281}
282
283#endif
284
ce80ddc1 285static void nand_print_and_set_info(int idx)
672ed2ae 286{
b616d9b0 287 struct mtd_info *mtd = nand_info[idx];
151c06ec 288 struct nand_chip *chip = mtd->priv;
ce80ddc1 289
672ed2ae
WG
290 printf("Device %d: ", idx);
291 if (chip->numchips > 1)
292 printf("%dx ", chip->numchips);
293 printf("%s, sector size %u KiB\n",
151c06ec
SW
294 mtd->name, mtd->erasesize >> 10);
295 printf(" Page size %8d b\n", mtd->writesize);
296 printf(" OOB size %8d b\n", mtd->oobsize);
297 printf(" Erase size %8d b\n", mtd->erasesize);
5db73feb
HS
298 printf(" subpagesize %8d b\n", chip->subpagesize);
299 printf(" options 0x%8x\n", chip->options);
300 printf(" bbt options 0x%8x\n", chip->bbt_options);
ce80ddc1
MV
301
302 /* Set geometry info */
151c06ec
SW
303 setenv_hex("nand_writesize", mtd->writesize);
304 setenv_hex("nand_oobsize", mtd->oobsize);
305 setenv_hex("nand_erasesize", mtd->erasesize);
672ed2ae
WG
306}
307
151c06ec
SW
308static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
309 ulong count, int read)
418396e2
SW
310{
311 int ret = 0;
418396e2
SW
312
313 while (count--) {
314 /* Raw access */
315 mtd_oob_ops_t ops = {
316 .datbuf = (u8 *)addr,
151c06ec
SW
317 .oobbuf = ((u8 *)addr) + mtd->writesize,
318 .len = mtd->writesize,
319 .ooblen = mtd->oobsize,
dfe64e2c 320 .mode = MTD_OPS_RAW
418396e2
SW
321 };
322
6b94f118 323 if (read) {
151c06ec 324 ret = mtd_read_oob(mtd, off, &ops);
6b94f118 325 } else {
151c06ec 326 ret = mtd_write_oob(mtd, off, &ops);
6b94f118 327 if (!ret)
151c06ec 328 ret = nand_verify_page_oob(mtd, &ops, off);
6b94f118 329 }
418396e2
SW
330
331 if (ret) {
332 printf("%s: error at offset %llx, ret %d\n",
333 __func__, (long long)off, ret);
334 break;
335 }
336
151c06ec
SW
337 addr += mtd->writesize + mtd->oobsize;
338 off += mtd->writesize;
418396e2
SW
339 }
340
341 return ret;
342}
343
e834402f 344/* Adjust a chip/partition size down for bad blocks so we don't
9b80aa8e 345 * read/write past the end of a chip/partition by accident.
e834402f
HC
346 */
347static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
348{
349 /* We grab the nand info object here fresh because this is usually
350 * called after arg_off_size() which can change the value of dev.
351 */
b616d9b0 352 struct mtd_info *mtd = nand_info[dev];
e834402f
HC
353 loff_t maxoffset = offset + *size;
354 int badblocks = 0;
355
356 /* count badblocks in NAND from offset to offset + size */
151c06ec
SW
357 for (; offset < maxoffset; offset += mtd->erasesize) {
358 if (nand_block_isbad(mtd, offset))
e834402f
HC
359 badblocks++;
360 }
361 /* adjust size if any bad blocks found */
362 if (badblocks) {
151c06ec 363 *size -= badblocks * mtd->erasesize;
e834402f
HC
364 printf("size adjusted to 0x%llx (%d bad blocks)\n",
365 (unsigned long long)*size, badblocks);
366 }
367}
368
088f1b19 369static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
addb2e16 370{
ea533c26
SW
371 int i, ret = 0;
372 ulong addr;
c39d6a0e 373 loff_t off, size, maxsize;
addb2e16 374 char *cmd, *s;
151c06ec 375 struct mtd_info *mtd;
6d0f6bcf
JCPV
376#ifdef CONFIG_SYS_NAND_QUIET
377 int quiet = CONFIG_SYS_NAND_QUIET;
c750d2e6 378#else
2255b2d2 379 int quiet = 0;
c750d2e6 380#endif
2255b2d2 381 const char *quiet_str = getenv("quiet");
ea533c26 382 int dev = nand_curr_device;
8c5659a6 383 int repeat = flag & CMD_FLAG_REPEAT;
addb2e16
BS
384
385 /* at least two arguments please */
386 if (argc < 2)
387 goto usage;
388
2255b2d2
SR
389 if (quiet_str)
390 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
391
addb2e16
BS
392 cmd = argv[1];
393
8c5659a6
SW
394 /* Only "dump" is repeatable. */
395 if (repeat && strcmp(cmd, "dump"))
396 return 0;
397
addb2e16
BS
398 if (strcmp(cmd, "info") == 0) {
399
400 putc('\n');
6d0f6bcf 401 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
b616d9b0 402 if (nand_info[i]->name)
ce80ddc1 403 nand_print_and_set_info(i);
addb2e16
BS
404 }
405 return 0;
406 }
407
408 if (strcmp(cmd, "device") == 0) {
addb2e16 409 if (argc < 3) {
672ed2ae 410 putc('\n');
ea533c26 411 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
672ed2ae 412 puts("no devices available\n");
addb2e16 413 else
ce80ddc1 414 nand_print_and_set_info(dev);
addb2e16
BS
415 return 0;
416 }
43a2b0e7 417
ea533c26
SW
418 dev = (int)simple_strtoul(argv[2], NULL, 10);
419 set_dev(dev);
43a2b0e7 420
addb2e16
BS
421 return 0;
422 }
423
c9f7351b
BG
424#ifdef CONFIG_ENV_OFFSET_OOB
425 /* this command operates only on the first nand device */
ea533c26
SW
426 if (strcmp(cmd, "env.oob") == 0)
427 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
c9f7351b
BG
428#endif
429
ea533c26
SW
430 /* The following commands operate on the current device, unless
431 * overridden by a partition specifier. Note that if somehow the
432 * current device is invalid, it will have to be changed to a valid
433 * one before these commands can run, even if a partition specifier
434 * for another device is to be used.
435 */
436 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
b616d9b0 437 !nand_info[dev]->name) {
addb2e16
BS
438 puts("\nno devices available\n");
439 return 1;
440 }
b616d9b0 441 mtd = nand_info[dev];
addb2e16
BS
442
443 if (strcmp(cmd, "bad") == 0) {
ea533c26 444 printf("\nDevice %d bad blocks:\n", dev);
151c06ec
SW
445 for (off = 0; off < mtd->size; off += mtd->erasesize)
446 if (nand_block_isbad(mtd, off))
ea533c26 447 printf(" %08llx\n", (unsigned long long)off);
addb2e16
BS
448 return 0;
449 }
450
856f0544
SR
451 /*
452 * Syntax is:
453 * 0 1 2 3 4
454 * nand erase [clean] [off size]
455 */
30486322 456 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
2255b2d2 457 nand_erase_options_t opts;
856f0544 458 /* "clean" at index 2 means request to write cleanmarker */
3043c045 459 int clean = argc > 2 && !strcmp("clean", argv[2]);
60899816
MV
460 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
461 int o = (clean || scrub_yes) ? 3 : 2;
30486322 462 int scrub = !strncmp(cmd, "scrub", 5);
30486322
SW
463 int spread = 0;
464 int args = 2;
60899816
MV
465 const char *scrub_warn =
466 "Warning: "
467 "scrub option will erase all factory set bad blocks!\n"
468 " "
469 "There is no reliable way to recover them.\n"
470 " "
471 "Use this command only for testing purposes if you\n"
472 " "
473 "are sure of what you are doing!\n"
474 "\nReally scrub this NAND flash? <y/N>\n";
30486322
SW
475
476 if (cmd[5] != 0) {
477 if (!strcmp(&cmd[5], ".spread")) {
478 spread = 1;
479 } else if (!strcmp(&cmd[5], ".part")) {
30486322
SW
480 args = 1;
481 } else if (!strcmp(&cmd[5], ".chip")) {
30486322
SW
482 args = 0;
483 } else {
484 goto usage;
485 }
486 }
487
488 /*
489 * Don't allow missing arguments to cause full chip/partition
490 * erases -- easy to do accidentally, e.g. with a misspelled
491 * variable name.
492 */
493 if (argc != o + args)
494 goto usage;
2255b2d2 495
30486322 496 printf("\nNAND %s: ", cmd);
856f0544 497 /* skip first two or three arguments, look for offset and size */
09c32807
HS
498 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
499 &maxsize, MTD_DEV_TYPE_NAND,
b616d9b0 500 nand_info[dev]->size) != 0)
09c32807
HS
501 return 1;
502
503 if (set_dev(dev))
856f0544 504 return 1;
2255b2d2 505
b616d9b0 506 mtd = nand_info[dev];
ea533c26 507
2255b2d2
SR
508 memset(&opts, 0, sizeof(opts));
509 opts.offset = off;
510 opts.length = size;
511 opts.jffs2 = clean;
512 opts.quiet = quiet;
30486322 513 opts.spread = spread;
2255b2d2
SR
514
515 if (scrub) {
a5dffa4b 516 if (scrub_yes) {
60899816 517 opts.scrub = 1;
a5dffa4b
PA
518 } else {
519 puts(scrub_warn);
520 if (confirm_yesno()) {
6b94b496 521 opts.scrub = 1;
a5dffa4b 522 } else {
6b94b496 523 puts("scrub aborted\n");
46aabcc4 524 return 1;
6b94b496 525 }
2255b2d2
SR
526 }
527 }
151c06ec 528 ret = nand_erase_opts(mtd, &opts);
addb2e16
BS
529 printf("%s\n", ret ? "ERROR" : "OK");
530
531 return ret == 0 ? 0 : 1;
532 }
533
534 if (strncmp(cmd, "dump", 4) == 0) {
535 if (argc < 3)
536 goto usage;
537
addb2e16 538 off = (int)simple_strtoul(argv[2], NULL, 16);
151c06ec 539 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
addb2e16
BS
540
541 return ret == 0 ? 1 : 0;
addb2e16
BS
542 }
543
addb2e16 544 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
ea533c26 545 size_t rwsize;
418396e2 546 ulong pagecount = 1;
2255b2d2 547 int read;
ced199dc 548 int raw = 0;
2255b2d2 549
addb2e16
BS
550 if (argc < 4)
551 goto usage;
2255b2d2 552
addb2e16
BS
553 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
554
2255b2d2 555 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
856f0544 556 printf("\nNAND %s: ", read ? "read" : "write");
4cbb651b 557
2255b2d2 558 s = strchr(cmd, '.');
418396e2 559
8d75c896 560 if (s && !strcmp(s, ".raw")) {
418396e2
SW
561 raw = 1;
562
09c32807
HS
563 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
564 MTD_DEV_TYPE_NAND,
b616d9b0 565 nand_info[dev]->size))
09c32807
HS
566 return 1;
567
568 if (set_dev(dev))
418396e2
SW
569 return 1;
570
b616d9b0 571 mtd = nand_info[dev];
93d3232d 572
418396e2
SW
573 if (argc > 4 && !str2long(argv[4], &pagecount)) {
574 printf("'%s' is not a number\n", argv[4]);
575 return 1;
576 }
577
151c06ec 578 if (pagecount * mtd->writesize > size) {
418396e2
SW
579 puts("Size exceeds partition or device limit\n");
580 return -1;
581 }
582
151c06ec 583 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
418396e2 584 } else {
09c32807
HS
585 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
586 &size, &maxsize,
587 MTD_DEV_TYPE_NAND,
b616d9b0 588 nand_info[dev]->size) != 0)
09c32807
HS
589 return 1;
590
591 if (set_dev(dev))
418396e2
SW
592 return 1;
593
e834402f
HC
594 /* size is unspecified */
595 if (argc < 5)
596 adjust_size_for_badblocks(&size, off, dev);
418396e2
SW
597 rwsize = size;
598 }
599
b616d9b0 600 mtd = nand_info[dev];
93d3232d 601
984e03cd
SW
602 if (!s || !strcmp(s, ".jffs2") ||
603 !strcmp(s, ".e") || !strcmp(s, ".i")) {
dfbf617f 604 if (read)
151c06ec 605 ret = nand_read_skip_bad(mtd, off, &rwsize,
c39d6a0e 606 NULL, maxsize,
4b070809 607 (u_char *)addr);
dfbf617f 608 else
151c06ec 609 ret = nand_write_skip_bad(mtd, off, &rwsize,
c39d6a0e 610 NULL, maxsize,
6b94f118
PT
611 (u_char *)addr,
612 WITH_WR_VERIFY);
c9494866
BG
613#ifdef CONFIG_CMD_NAND_TRIMFFS
614 } else if (!strcmp(s, ".trimffs")) {
615 if (read) {
616 printf("Unknown nand command suffix '%s'\n", s);
617 return 1;
618 }
151c06ec 619 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
c39d6a0e 620 maxsize, (u_char *)addr,
6b94f118 621 WITH_DROP_FFS | WITH_WR_VERIFY);
47fc18f1 622#endif
dfc99e14 623 } else if (!strcmp(s, ".oob")) {
cfa460ad
WJ
624 /* out-of-band data */
625 mtd_oob_ops_t ops = {
626 .oobbuf = (u8 *)addr,
ea533c26 627 .ooblen = rwsize,
dfe64e2c 628 .mode = MTD_OPS_RAW
cfa460ad
WJ
629 };
630
fb3659ac 631 if (read)
151c06ec 632 ret = mtd_read_oob(mtd, off, &ops);
fb3659ac 633 else
151c06ec 634 ret = mtd_write_oob(mtd, off, &ops);
418396e2 635 } else if (raw) {
151c06ec 636 ret = raw_access(mtd, addr, off, pagecount, read);
856f0544 637 } else {
984e03cd
SW
638 printf("Unknown nand command suffix '%s'.\n", s);
639 return 1;
2255b2d2
SR
640 }
641
ea533c26 642 printf(" %zu bytes %s: %s\n", rwsize,
2255b2d2 643 read ? "read" : "written", ret ? "ERROR" : "OK");
addb2e16
BS
644
645 return ret == 0 ? 0 : 1;
646 }
2255b2d2 647
3287f6d3
BT
648#ifdef CONFIG_CMD_NAND_TORTURE
649 if (strcmp(cmd, "torture") == 0) {
650 if (argc < 3)
651 goto usage;
652
653 if (!str2off(argv[2], &off)) {
654 puts("Offset is not a valid number\n");
655 return 1;
656 }
657
658 printf("\nNAND torture: device %d offset 0x%llx size 0x%x\n",
151c06ec
SW
659 dev, off, mtd->erasesize);
660 ret = nand_torture(mtd, off);
3287f6d3
BT
661 printf(" %s\n", ret ? "Failed" : "Passed");
662
663 return ret == 0 ? 0 : 1;
664 }
665#endif
666
2255b2d2 667 if (strcmp(cmd, "markbad") == 0) {
8360b66b
WD
668 argc -= 2;
669 argv += 2;
2255b2d2 670
8360b66b
WD
671 if (argc <= 0)
672 goto usage;
673
674 while (argc > 0) {
675 addr = simple_strtoul(*argv, NULL, 16);
676
151c06ec 677 if (mtd_block_markbad(mtd, addr)) {
8360b66b
WD
678 printf("block 0x%08lx NOT marked "
679 "as bad! ERROR %d\n",
680 addr, ret);
681 ret = 1;
682 } else {
683 printf("block 0x%08lx successfully "
684 "marked as bad\n",
685 addr);
686 }
687 --argc;
688 ++argv;
2255b2d2 689 }
8360b66b 690 return ret;
2255b2d2 691 }
dfbf617f 692
2255b2d2
SR
693 if (strcmp(cmd, "biterr") == 0) {
694 /* todo */
695 return 1;
696 }
697
50657c27 698#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
2255b2d2 699 if (strcmp(cmd, "lock") == 0) {
50657c27 700 int tight = 0;
2255b2d2
SR
701 int status = 0;
702 if (argc == 3) {
703 if (!strcmp("tight", argv[2]))
704 tight = 1;
705 if (!strcmp("status", argv[2]))
706 status = 1;
707 }
2255b2d2 708 if (status) {
151c06ec 709 do_nand_status(mtd);
cfa460ad 710 } else {
151c06ec 711 if (!nand_lock(mtd, tight)) {
5e1dae5c
WJ
712 puts("NAND flash successfully locked\n");
713 } else {
714 puts("Error locking NAND flash\n");
715 return 1;
716 }
2255b2d2
SR
717 }
718 return 0;
719 }
720
eee623a5
JH
721 if (strncmp(cmd, "unlock", 5) == 0) {
722 int allexcept = 0;
723
724 s = strchr(cmd, '.');
725
726 if (s && !strcmp(s, ".allexcept"))
727 allexcept = 1;
728
09c32807
HS
729 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
730 &maxsize, MTD_DEV_TYPE_NAND,
b616d9b0 731 nand_info[dev]->size) < 0)
09c32807
HS
732 return 1;
733
734 if (set_dev(dev))
856f0544 735 return 1;
2255b2d2 736
b616d9b0 737 if (!nand_unlock(nand_info[dev], off, size, allexcept)) {
5e1dae5c
WJ
738 puts("NAND flash successfully unlocked\n");
739 } else {
740 puts("Error unlocking NAND flash, "
3043c045 741 "write and erase will probably fail\n");
5e1dae5c
WJ
742 return 1;
743 }
2255b2d2
SR
744 return 0;
745 }
50657c27 746#endif
2255b2d2 747
addb2e16 748usage:
4c12eeb8 749 return CMD_RET_USAGE;
addb2e16
BS
750}
751
088f1b19
KP
752#ifdef CONFIG_SYS_LONGHELP
753static char nand_help_text[] =
a89c33db
WD
754 "info - show available NAND devices\n"
755 "nand device [dev] - show or set current device\n"
756 "nand read - addr off|partition size\n"
757 "nand write - addr off|partition size\n"
758 " read/write 'size' bytes starting at offset 'off'\n"
759 " to/from memory address 'addr', skipping bad blocks.\n"
418396e2
SW
760 "nand read.raw - addr off|partition [count]\n"
761 "nand write.raw - addr off|partition [count]\n"
762 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
c9494866
BG
763#ifdef CONFIG_CMD_NAND_TRIMFFS
764 "nand write.trimffs - addr off|partition size\n"
765 " write 'size' bytes starting at offset 'off' from memory address\n"
766 " 'addr', skipping bad blocks and dropping any pages at the end\n"
767 " of eraseblocks that contain only 0xFF\n"
47fc18f1 768#endif
eb3abce8 769 "nand erase[.spread] [clean] off size - erase 'size' bytes "
30486322
SW
770 "from offset 'off'\n"
771 " With '.spread', erase enough for given file size, otherwise,\n"
772 " 'size' includes skipped bad blocks.\n"
773 "nand erase.part [clean] partition - erase entire mtd partition'\n"
774 "nand erase.chip [clean] - erase entire chip'\n"
a89c33db
WD
775 "nand bad - show bad blocks\n"
776 "nand dump[.oob] off - dump page\n"
3287f6d3
BT
777#ifdef CONFIG_CMD_NAND_TORTURE
778 "nand torture off - torture block at offset\n"
779#endif
60899816 780 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
30486322 781 " really clean NAND erasing bad blocks (UNSAFE)\n"
a89c33db
WD
782 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
783 "nand biterr off - make a bit error at offset (UNSAFE)"
50657c27 784#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
a89c33db
WD
785 "\n"
786 "nand lock [tight] [status]\n"
787 " bring nand to lock state or display locked pages\n"
eee623a5 788 "nand unlock[.allexcept] [offset] [size] - unlock section"
50657c27 789#endif
c9f7351b
BG
790#ifdef CONFIG_ENV_OFFSET_OOB
791 "\n"
792 "nand env.oob - environment offset in OOB of block 0 of"
793 " first device.\n"
794 "nand env.oob set off|partition - set enviromnent offset\n"
795 "nand env.oob get - get environment offset"
796#endif
088f1b19
KP
797 "";
798#endif
799
800U_BOOT_CMD(
801 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
802 "NAND sub-system", nand_help_text
50657c27 803);
addb2e16 804
151c06ec 805static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
4b070809 806 ulong offset, ulong addr, char *cmd)
addb2e16 807{
addb2e16 808 int r;
67d668bf 809 char *s;
4ca79f47 810 size_t cnt;
21d29f7f 811#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
addb2e16 812 image_header_t *hdr;
21d29f7f 813#endif
09475f75 814#if defined(CONFIG_FIT)
3bab76a2 815 const void *fit_hdr = NULL;
09475f75 816#endif
10e03893
TK
817
818 s = strchr(cmd, '.');
819 if (s != NULL &&
65d8bc94 820 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
984e03cd 821 printf("Unknown nand load suffix '%s'\n", s);
770605e4 822 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
984e03cd
SW
823 return 1;
824 }
addb2e16 825
151c06ec 826 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
addb2e16 827
151c06ec
SW
828 cnt = mtd->writesize;
829 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
830 (u_char *)addr);
addb2e16 831 if (r) {
856f0544 832 puts("** Read error\n");
770605e4 833 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
addb2e16
BS
834 return 1;
835 }
770605e4 836 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
addb2e16 837
9a4daad0 838 switch (genimg_get_format ((void *)addr)) {
21d29f7f 839#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad7
MB
840 case IMAGE_FORMAT_LEGACY:
841 hdr = (image_header_t *)addr;
842
770605e4 843 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
d5934ad7 844 image_print_contents (hdr);
addb2e16 845
d5934ad7
MB
846 cnt = image_get_image_size (hdr);
847 break;
21d29f7f 848#endif
d5934ad7
MB
849#if defined(CONFIG_FIT)
850 case IMAGE_FORMAT_FIT:
09475f75 851 fit_hdr = (const void *)addr;
09475f75
MB
852 puts ("Fit image detected...\n");
853
854 cnt = fit_get_size (fit_hdr);
855 break;
d5934ad7
MB
856#endif
857 default:
770605e4 858 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
d5934ad7 859 puts ("** Unknown image type\n");
addb2e16
BS
860 return 1;
861 }
770605e4 862 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
addb2e16 863
151c06ec
SW
864 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
865 (u_char *)addr);
addb2e16 866 if (r) {
856f0544 867 puts("** Read error\n");
770605e4 868 bootstage_error(BOOTSTAGE_ID_NAND_READ);
addb2e16
BS
869 return 1;
870 }
770605e4 871 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
addb2e16 872
09475f75
MB
873#if defined(CONFIG_FIT)
874 /* This cannot be done earlier, we need complete FIT image in RAM first */
3bab76a2
MB
875 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
876 if (!fit_check_format (fit_hdr)) {
770605e4 877 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
3bab76a2
MB
878 puts ("** Bad FIT image format\n");
879 return 1;
880 }
770605e4 881 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
3bab76a2
MB
882 fit_print_contents (fit_hdr);
883 }
09475f75
MB
884#endif
885
addb2e16
BS
886 /* Loading ok, update default load address */
887
888 load_addr = addr;
889
67d668bf 890 return bootm_maybe_autostart(cmdtp, cmd);
addb2e16
BS
891}
892
088f1b19
KP
893static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
894 char * const argv[])
856f0544
SR
895{
896 char *boot_device = NULL;
897 int idx;
898 ulong addr, offset = 0;
0c8a8491 899#if defined(CONFIG_CMD_MTDPARTS)
856f0544
SR
900 struct mtd_device *dev;
901 struct part_info *part;
902 u8 pnum;
903
904 if (argc >= 2) {
905 char *p = (argc == 2) ? argv[1] : argv[2];
906 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
907 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
908 if (dev->id->type != MTD_DEV_TYPE_NAND) {
909 puts("Not a NAND device\n");
910 return 1;
911 }
912 if (argc > 3)
913 goto usage;
914 if (argc == 3)
10e03893 915 addr = simple_strtoul(argv[1], NULL, 16);
856f0544 916 else
6d0f6bcf 917 addr = CONFIG_SYS_LOAD_ADDR;
b616d9b0 918 return nand_load_image(cmdtp, nand_info[dev->id->num],
4b070809 919 part->offset, addr, argv[0]);
856f0544
SR
920 }
921 }
922#endif
923
770605e4 924 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
856f0544
SR
925 switch (argc) {
926 case 1:
6d0f6bcf 927 addr = CONFIG_SYS_LOAD_ADDR;
856f0544
SR
928 boot_device = getenv("bootdevice");
929 break;
930 case 2:
931 addr = simple_strtoul(argv[1], NULL, 16);
932 boot_device = getenv("bootdevice");
933 break;
934 case 3:
935 addr = simple_strtoul(argv[1], NULL, 16);
936 boot_device = argv[2];
937 break;
938 case 4:
939 addr = simple_strtoul(argv[1], NULL, 16);
940 boot_device = argv[2];
941 offset = simple_strtoul(argv[3], NULL, 16);
942 break;
943 default:
0c8a8491 944#if defined(CONFIG_CMD_MTDPARTS)
856f0544
SR
945usage:
946#endif
770605e4 947 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
4c12eeb8 948 return CMD_RET_USAGE;
856f0544 949 }
770605e4 950 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
856f0544
SR
951
952 if (!boot_device) {
953 puts("\n** No boot device **\n");
770605e4 954 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
856f0544
SR
955 return 1;
956 }
770605e4 957 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
addb2e16 958
856f0544
SR
959 idx = simple_strtoul(boot_device, NULL, 16);
960
b616d9b0 961 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx]->name) {
856f0544 962 printf("\n** Device %d not available\n", idx);
770605e4 963 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
856f0544
SR
964 return 1;
965 }
770605e4 966 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
856f0544 967
b616d9b0 968 return nand_load_image(cmdtp, nand_info[idx], offset, addr, argv[0]);
856f0544
SR
969}
970
971U_BOOT_CMD(nboot, 4, 1, do_nandboot,
2fb2604d 972 "boot from NAND device",
a89c33db
WD
973 "[partition] | [[[loadAddr] dev] offset]"
974);