blob: ffdeea41a5a7a7d4b61449e4bbd448a796508370 [file] [log] [blame]
wdenkc8434db2003-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>
wdenk6d3c6d12005-04-03 22:35:21 +00006 *
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -04007 * 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.
wdenk6d3c6d12005-04-03 22:35:21 +000011 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
Scott Woode5836dc2010-08-02 13:04:24 -050013 *
Scott Wood733fb022012-03-02 14:01:57 -060014 * Copyright 2010, 2012 Freescale Semiconductor
Scott Woode5836dc2010-08-02 13:04:24 -050015 * 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.
wdenkc8434db2003-03-26 06:55:25 +000020 */
21
22#include <common.h>
William Juul52c07962007-10-31 13:53:06 +010023#include <linux/mtd/mtd.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010024#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070025#include <console.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010026#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010029#include <jffs2/jffs2.h>
30#include <nand.h>
31
Ladislav Michl9d0928d2009-04-21 02:26:31 +020032#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020033
Wolfgang Denk161bfc52009-11-17 21:27:39 +010034/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020035int 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 Denk74e0dde2008-08-14 14:41:06 +020038 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020039#endif
40
Scott Wood08364d92016-05-30 13:57:54 -050041static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
42 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010043{
44 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010045 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -050046 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +090047 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -050048
49 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -050050 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -050051
52 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010053
Scott Wood08364d92016-05-30 13:57:54 -050054 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090055 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010056 puts("No memory for page buffer\n");
57 return 1;
58 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +090059
Scott Wood08364d92016-05-30 13:57:54 -050060 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090061 if (!oobbuf) {
62 puts("No memory for page buffer\n");
63 ret = 1;
64 goto free_dat;
65 }
Scott Wood08364d92016-05-30 13:57:54 -050066 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010067 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +010068 struct mtd_oob_ops ops;
69 memset(&ops, 0, sizeof(ops));
70 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -070071 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -050072 ops.len = mtd->writesize;
73 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +000074 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -050075 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010076 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020077 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090078 ret = 1;
79 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010080 }
Stefan Roese3167d072008-07-10 10:10:54 +020081 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020082
Masahiro Yamada3f97e272013-07-11 17:27:13 +090083 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -050084 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +090085 p = datbuf;
86
87 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +010088 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],
Scott Woodcc5f3392008-06-12 13:20:16 -050091 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
92 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +090093 p += 16;
94 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010095 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +090096
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010097 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -050098 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -070099 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100100 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100101 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]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100103 p += 8;
104 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900105
106free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100107 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900108free_dat:
109 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100110
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900111 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100112}
113
114/* ------------------------------------------------------------------------- */
115
Scott Woode5836dc2010-08-02 13:04:24 -0500116static int set_dev(int dev)
117{
118 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
Scott Wood2c1b7e12016-05-30 13:57:55 -0500119 !nand_info[dev]->name) {
Scott Woode5836dc2010-08-02 13:04:24 -0500120 puts("No such device\n");
121 return -1;
122 }
123
124 if (nand_curr_device == dev)
125 return 0;
126
Scott Wood2c1b7e12016-05-30 13:57:55 -0500127 printf("Device %d: %s", dev, nand_info[dev]->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500128 puts("... is now current device\n");
129 nand_curr_device = dev;
130
131#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Scott Wood2c1b7e12016-05-30 13:57:55 -0500132 board_nand_select_device(nand_info[dev]->priv, dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500133#endif
134
135 return 0;
136}
137
Nishanth Menonb20f8402008-12-13 09:43:06 -0600138#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
139static void print_status(ulong start, ulong end, ulong erasesize, int status)
140{
Joe Hershberger53768562012-08-22 16:49:45 -0500141 /*
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 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600147 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 " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500152 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600153 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
154}
155
Scott Wood08364d92016-05-30 13:57:54 -0500156static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600157{
158 ulong block_start = 0;
159 ulong off;
160 int last_status = -1;
161
Scott Wood17fed142016-05-30 13:57:56 -0500162 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600163 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500164 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600165 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500166 (nand_chip->read_byte(mtd) & 0x80 ?
167 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600168
Scott Wood08364d92016-05-30 13:57:54 -0500169 for (off = 0; off < mtd->size; off += mtd->erasesize) {
170 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600171
172 /* print message only if status has changed */
173 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500174 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600175 last_status);
176 block_start = off;
177 }
178 last_status = s;
179 }
180 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500181 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600182}
183#endif
184
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400185#ifdef CONFIG_ENV_OFFSET_OOB
186unsigned long nand_env_oob_offset;
187
Scott Woode5836dc2010-08-02 13:04:24 -0500188int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400189{
190 int ret;
191 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Scott Wood2c1b7e12016-05-30 13:57:55 -0500192 struct mtd_info *mtd = nand_info[0];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400193 char *cmd = argv[1];
194
Scott Wood08364d92016-05-30 13:57:54 -0500195 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd->name) {
Scott Woode5836dc2010-08-02 13:04:24 -0500196 puts("no devices available\n");
197 return 1;
198 }
199
200 set_dev(0);
201
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400202 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500203 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500204 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400205 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500206
207 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400208 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500209 loff_t addr;
210 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400211 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500212 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400213
214 if (argc < 3)
215 goto usage;
216
Tom Rini32d96182013-03-14 05:32:50 +0000217 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200218 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500219 MTD_DEV_TYPE_NAND, nand_info[idx]->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500220 puts("Offset or partition name expected\n");
221 return 1;
222 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200223 if (set_dev(idx)) {
224 puts("Offset or partition name expected\n");
225 return 1;
226 }
Scott Woode5836dc2010-08-02 13:04:24 -0500227
228 if (idx != 0) {
229 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400230 return 1;
231 }
232
Scott Wood08364d92016-05-30 13:57:54 -0500233 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500234 printf("Insufficient available OOB bytes:\n"
235 "%d OOB bytes available but %d required for "
236 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500237 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400238 return 1;
239 }
240
Scott Wood08364d92016-05-30 13:57:54 -0500241 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400242 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;
Scott Wood08364d92016-05-30 13:57:54 -0500253 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400254
Scott Wood08364d92016-05-30 13:57:54 -0500255 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500256 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400257 printf("Error writing OOB block 0\n");
258 return ret;
259 }
Scott Wood0268da02010-07-12 18:17:40 -0500260
Scott Wood08364d92016-05-30 13:57:54 -0500261 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500262 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: "
Scott Woode5836dc2010-08-02 13:04:24 -0500269 "0x%08llx expected but got 0x%08lx\n",
270 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500271 return 1;
272 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400273 } else {
274 goto usage;
275 }
276
277 return ret;
278
279usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000280 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400281}
282
283#endif
284
Marek Vasut185ce252011-09-22 03:57:26 +0200285static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100286{
Scott Wood2c1b7e12016-05-30 13:57:55 -0500287 struct mtd_info *mtd = nand_info[idx];
Scott Wood17fed142016-05-30 13:57:56 -0500288 struct nand_chip *chip = mtd_to_nand(mtd);
Marek Vasut185ce252011-09-22 03:57:26 +0200289
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100290 printf("Device %d: ", idx);
291 if (chip->numchips > 1)
292 printf("%dx ", chip->numchips);
293 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500294 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);
Heiko Schocherfb9a9bd2015-04-12 10:18:09 +0200298 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);
Marek Vasut185ce252011-09-22 03:57:26 +0200301
302 /* Set geometry info */
Scott Wood08364d92016-05-30 13:57:54 -0500303 setenv_hex("nand_writesize", mtd->writesize);
304 setenv_hex("nand_oobsize", mtd->oobsize);
305 setenv_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100306}
307
Scott Wood08364d92016-05-30 13:57:54 -0500308static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
309 ulong count, int read)
Scott Wood733fb022012-03-02 14:01:57 -0600310{
311 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600312
313 while (count--) {
314 /* Raw access */
315 mtd_oob_ops_t ops = {
316 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500317 .oobbuf = ((u8 *)addr) + mtd->writesize,
318 .len = mtd->writesize,
319 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000320 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600321 };
322
Peter Tyser5fa8b982015-02-03 11:58:13 -0600323 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500324 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600325 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500326 ret = mtd_write_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600327 if (!ret)
Scott Wood08364d92016-05-30 13:57:54 -0500328 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600329 }
Scott Wood733fb022012-03-02 14:01:57 -0600330
331 if (ret) {
332 printf("%s: error at offset %llx, ret %d\n",
333 __func__, (long long)off, ret);
334 break;
335 }
336
Scott Wood08364d92016-05-30 13:57:54 -0500337 addr += mtd->writesize + mtd->oobsize;
338 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600339 }
340
341 return ret;
342}
343
Harvey Chapman39e68862013-02-26 17:57:14 +0000344/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500345 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000346 */
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 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500352 struct mtd_info *mtd = nand_info[dev];
Harvey Chapman39e68862013-02-26 17:57:14 +0000353 loff_t maxoffset = offset + *size;
354 int badblocks = 0;
355
356 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500357 for (; offset < maxoffset; offset += mtd->erasesize) {
358 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000359 badblocks++;
360 }
361 /* adjust size if any bad blocks found */
362 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500363 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000364 printf("size adjusted to 0x%llx (%d bad blocks)\n",
365 (unsigned long long)*size, badblocks);
366 }
367}
368
Kim Phillipsdc00a682012-10-29 13:34:31 +0000369static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100370{
Scott Woode5836dc2010-08-02 13:04:24 -0500371 int i, ret = 0;
372 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000373 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100374 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500375 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200376#ifdef CONFIG_SYS_NAND_QUIET
377 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200378#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200379 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200380#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200381 const char *quiet_str = getenv("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500382 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500383 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100384
385 /* at least two arguments please */
386 if (argc < 2)
387 goto usage;
388
Stefan Roesed351b2b2006-10-10 12:36:02 +0200389 if (quiet_str)
390 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
391
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100392 cmd = argv[1];
393
Scott Wood67d5a2a2010-08-25 15:24:01 -0500394 /* Only "dump" is repeatable. */
395 if (repeat && strcmp(cmd, "dump"))
396 return 0;
397
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100398 if (strcmp(cmd, "info") == 0) {
399
400 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200401 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500402 if (nand_info[i]->name)
Marek Vasut185ce252011-09-22 03:57:26 +0200403 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100404 }
405 return 0;
406 }
407
408 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100409 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100410 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500411 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100412 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100413 else
Marek Vasut185ce252011-09-22 03:57:26 +0200414 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100415 return 0;
416 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200417
Scott Woode5836dc2010-08-02 13:04:24 -0500418 dev = (int)simple_strtoul(argv[2], NULL, 10);
419 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200420
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100421 return 0;
422 }
423
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400424#ifdef CONFIG_ENV_OFFSET_OOB
425 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500426 if (strcmp(cmd, "env.oob") == 0)
427 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400428#endif
429
Scott Woode5836dc2010-08-02 13:04:24 -0500430 /* 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 ||
Scott Wood2c1b7e12016-05-30 13:57:55 -0500437 !nand_info[dev]->name) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100438 puts("\nno devices available\n");
439 return 1;
440 }
Scott Wood2c1b7e12016-05-30 13:57:55 -0500441 mtd = nand_info[dev];
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100442
443 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500444 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500445 for (off = 0; off < mtd->size; off += mtd->erasesize)
446 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500447 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100448 return 0;
449 }
450
Stefan Roese198b23e2006-10-28 15:55:52 +0200451 /*
452 * Syntax is:
453 * 0 1 2 3 4
454 * nand erase [clean] [off size]
455 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500456 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200457 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200458 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100459 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200460 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
461 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500462 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500463 int spread = 0;
464 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200465 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";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200475
Scott Wood1b5cd512010-08-25 14:43:29 -0500476 if (cmd[5] != 0) {
477 if (!strcmp(&cmd[5], ".spread")) {
478 spread = 1;
479 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500480 args = 1;
481 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500482 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;
495
496 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200497 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200498 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
499 &maxsize, MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500500 nand_info[dev]->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200501 return 1;
502
503 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200504 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200505
Scott Wood2c1b7e12016-05-30 13:57:55 -0500506 mtd = nand_info[dev];
Scott Woode5836dc2010-08-02 13:04:24 -0500507
Stefan Roesed351b2b2006-10-10 12:36:02 +0200508 memset(&opts, 0, sizeof(opts));
509 opts.offset = off;
510 opts.length = size;
511 opts.jffs2 = clean;
512 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500513 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200514
515 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200516 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200517 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200518 } else {
519 puts(scrub_warn);
520 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100521 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200522 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100523 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900524 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100525 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200526 }
527 }
Scott Wood08364d92016-05-30 13:57:54 -0500528 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100529 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
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100538 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood08364d92016-05-30 13:57:54 -0500539 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100540
541 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100542 }
543
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100544 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500545 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600546 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200547 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000548 int raw = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200549
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100550 if (argc < 4)
551 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200552
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100553 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
554
Stefan Roesed351b2b2006-10-10 12:36:02 +0200555 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200556 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100557
Stefan Roesed351b2b2006-10-10 12:36:02 +0200558 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600559
Steve Sakoman736d3252012-06-07 10:19:18 -0700560 if (s && !strcmp(s, ".raw")) {
Scott Wood733fb022012-03-02 14:01:57 -0600561 raw = 1;
562
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200563 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
564 MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500565 nand_info[dev]->size))
Scott Wood733fb022012-03-02 14:01:57 -0600566 return 1;
567
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200568 if (set_dev(dev))
569 return 1;
570
Scott Wood2c1b7e12016-05-30 13:57:55 -0500571 mtd = nand_info[dev];
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200572
Scott Wood733fb022012-03-02 14:01:57 -0600573 if (argc > 4 && !str2long(argv[4], &pagecount)) {
574 printf("'%s' is not a number\n", argv[4]);
575 return 1;
576 }
577
Scott Wood08364d92016-05-30 13:57:54 -0500578 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600579 puts("Size exceeds partition or device limit\n");
580 return -1;
581 }
582
Scott Wood08364d92016-05-30 13:57:54 -0500583 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600584 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200585 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
586 &size, &maxsize,
587 MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500588 nand_info[dev]->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200589 return 1;
590
591 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600592 return 1;
593
Harvey Chapman39e68862013-02-26 17:57:14 +0000594 /* size is unspecified */
595 if (argc < 5)
596 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600597 rwsize = size;
598 }
599
Scott Wood2c1b7e12016-05-30 13:57:55 -0500600 mtd = nand_info[dev];
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200601
Scott Wood12b1c952008-06-12 13:13:23 -0500602 if (!s || !strcmp(s, ".jffs2") ||
603 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500604 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500605 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000606 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200607 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500608 else
Scott Wood08364d92016-05-30 13:57:54 -0500609 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000610 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600611 (u_char *)addr,
612 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400613#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 }
Scott Wood08364d92016-05-30 13:57:54 -0500619 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000620 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600621 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400622#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400623 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100624 /* out-of-band data */
625 mtd_oob_ops_t ops = {
626 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500627 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000628 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100629 };
630
Harald Welte5a395102007-12-19 14:12:53 +0100631 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500632 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100633 else
Scott Wood08364d92016-05-30 13:57:54 -0500634 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600635 } else if (raw) {
Scott Wood08364d92016-05-30 13:57:54 -0500636 ret = raw_access(mtd, addr, off, pagecount, read);
Stefan Roese198b23e2006-10-28 15:55:52 +0200637 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500638 printf("Unknown nand command suffix '%s'.\n", s);
639 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200640 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100641
Scott Woode5836dc2010-08-02 13:04:24 -0500642 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200643 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100644
645 return ret == 0 ? 0 : 1;
646 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200647
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100648#ifdef CONFIG_CMD_NAND_TORTURE
649 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200650 loff_t endoff;
651 unsigned int failed = 0, passed = 0;
652
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100653 if (argc < 3)
654 goto usage;
655
656 if (!str2off(argv[2], &off)) {
657 puts("Offset is not a valid number\n");
658 return 1;
659 }
660
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200661 size = mtd->erasesize;
662 if (argc > 3) {
663 if (!str2off(argv[3], &size)) {
664 puts("Size is not a valid number\n");
665 return 1;
666 }
667 }
668
669 endoff = off + size;
670 if (endoff > mtd->size) {
671 puts("Arguments beyond end of NAND\n");
672 return 1;
673 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100674
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200675 off = round_down(off, mtd->erasesize);
676 endoff = round_up(endoff, mtd->erasesize);
677 size = endoff - off;
678 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
679 dev, off, size, mtd->erasesize);
680 while (off < endoff) {
681 ret = nand_torture(mtd, off);
682 if (ret) {
683 failed++;
684 printf(" block at 0x%llx failed\n", off);
685 } else {
686 passed++;
687 }
688 off += mtd->erasesize;
689 }
690 printf(" Passed: %u, failed: %u\n", passed, failed);
691 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100692 }
693#endif
694
Stefan Roesed351b2b2006-10-10 12:36:02 +0200695 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200696 argc -= 2;
697 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200698
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200699 if (argc <= 0)
700 goto usage;
701
702 while (argc > 0) {
703 addr = simple_strtoul(*argv, NULL, 16);
704
Scott Wood08364d92016-05-30 13:57:54 -0500705 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200706 printf("block 0x%08lx NOT marked "
707 "as bad! ERROR %d\n",
708 addr, ret);
709 ret = 1;
710 } else {
711 printf("block 0x%08lx successfully "
712 "marked as bad\n",
713 addr);
714 }
715 --argc;
716 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200717 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200718 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200719 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500720
Stefan Roesed351b2b2006-10-10 12:36:02 +0200721 if (strcmp(cmd, "biterr") == 0) {
722 /* todo */
723 return 1;
724 }
725
Nishanth Menonb20f8402008-12-13 09:43:06 -0600726#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200727 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600728 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200729 int status = 0;
730 if (argc == 3) {
731 if (!strcmp("tight", argv[2]))
732 tight = 1;
733 if (!strcmp("status", argv[2]))
734 status = 1;
735 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200736 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500737 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100738 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500739 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100740 puts("NAND flash successfully locked\n");
741 } else {
742 puts("Error locking NAND flash\n");
743 return 1;
744 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200745 }
746 return 0;
747 }
748
Joe Hershbergercccf5952012-08-22 16:49:42 -0500749 if (strncmp(cmd, "unlock", 5) == 0) {
750 int allexcept = 0;
751
752 s = strchr(cmd, '.');
753
754 if (s && !strcmp(s, ".allexcept"))
755 allexcept = 1;
756
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200757 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
758 &maxsize, MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500759 nand_info[dev]->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200760 return 1;
761
762 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200763 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200764
Scott Wood2c1b7e12016-05-30 13:57:55 -0500765 if (!nand_unlock(nand_info[dev], off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100766 puts("NAND flash successfully unlocked\n");
767 } else {
768 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100769 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100770 return 1;
771 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200772 return 0;
773 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600774#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200775
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100776usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000777 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100778}
779
Kim Phillipsdc00a682012-10-29 13:34:31 +0000780#ifdef CONFIG_SYS_LONGHELP
781static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200782 "info - show available NAND devices\n"
783 "nand device [dev] - show or set current device\n"
784 "nand read - addr off|partition size\n"
785 "nand write - addr off|partition size\n"
786 " read/write 'size' bytes starting at offset 'off'\n"
787 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600788 "nand read.raw - addr off|partition [count]\n"
789 "nand write.raw - addr off|partition [count]\n"
790 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400791#ifdef CONFIG_CMD_NAND_TRIMFFS
792 "nand write.trimffs - addr off|partition size\n"
793 " write 'size' bytes starting at offset 'off' from memory address\n"
794 " 'addr', skipping bad blocks and dropping any pages at the end\n"
795 " of eraseblocks that contain only 0xFF\n"
796#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200797 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500798 "from offset 'off'\n"
799 " With '.spread', erase enough for given file size, otherwise,\n"
800 " 'size' includes skipped bad blocks.\n"
801 "nand erase.part [clean] partition - erase entire mtd partition'\n"
802 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200803 "nand bad - show bad blocks\n"
804 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100805#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200806 "nand torture off - torture one block at offset\n"
807 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100808#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200809 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500810 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200811 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
812 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600813#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200814 "\n"
815 "nand lock [tight] [status]\n"
816 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500817 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600818#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400819#ifdef CONFIG_ENV_OFFSET_OOB
820 "\n"
821 "nand env.oob - environment offset in OOB of block 0 of"
822 " first device.\n"
823 "nand env.oob set off|partition - set enviromnent offset\n"
824 "nand env.oob get - get environment offset"
825#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000826 "";
827#endif
828
829U_BOOT_CMD(
830 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
831 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600832);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100833
Scott Wood08364d92016-05-30 13:57:54 -0500834static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200835 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100836{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100837 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000838 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200839 size_t cnt;
Heiko Schocher515eb122014-05-28 11:33:33 +0200840#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100841 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200842#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100843#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200844 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100845#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200846
847 s = strchr(cmd, '.');
848 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500849 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500850 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000851 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500852 return 1;
853 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100854
Scott Wood08364d92016-05-30 13:57:54 -0500855 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100856
Scott Wood08364d92016-05-30 13:57:54 -0500857 cnt = mtd->writesize;
858 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
859 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100860 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200861 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000862 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100863 return 1;
864 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000865 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100866
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100867 switch (genimg_get_format ((void *)addr)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200868#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100869 case IMAGE_FORMAT_LEGACY:
870 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100871
Simon Glass0169e6b2012-02-13 13:51:18 +0000872 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100873 image_print_contents (hdr);
874
875 cnt = image_get_image_size (hdr);
876 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200877#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100878#if defined(CONFIG_FIT)
879 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100880 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100881 puts ("Fit image detected...\n");
882
883 cnt = fit_get_size (fit_hdr);
884 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100885#endif
886 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000887 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100888 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100889 return 1;
890 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000891 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100892
Scott Wood08364d92016-05-30 13:57:54 -0500893 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
894 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100895 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200896 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000897 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100898 return 1;
899 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000900 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100901
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100902#if defined(CONFIG_FIT)
903 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200904 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
905 if (!fit_check_format (fit_hdr)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000906 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200907 puts ("** Bad FIT image format\n");
908 return 1;
909 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000910 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200911 fit_print_contents (fit_hdr);
912 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100913#endif
914
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100915 /* Loading ok, update default load address */
916
917 load_addr = addr;
918
Mike Frysinger194c2e82011-06-05 13:43:02 +0000919 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100920}
921
Kim Phillipsdc00a682012-10-29 13:34:31 +0000922static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
923 char * const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +0200924{
925 char *boot_device = NULL;
926 int idx;
927 ulong addr, offset = 0;
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200928#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200929 struct mtd_device *dev;
930 struct part_info *part;
931 u8 pnum;
932
933 if (argc >= 2) {
934 char *p = (argc == 2) ? argv[1] : argv[2];
935 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
936 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
937 if (dev->id->type != MTD_DEV_TYPE_NAND) {
938 puts("Not a NAND device\n");
939 return 1;
940 }
941 if (argc > 3)
942 goto usage;
943 if (argc == 3)
Thomas Knobloche84b7772007-07-06 14:58:39 +0200944 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200945 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200946 addr = CONFIG_SYS_LOAD_ADDR;
Scott Wood2c1b7e12016-05-30 13:57:55 -0500947 return nand_load_image(cmdtp, nand_info[dev->id->num],
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200948 part->offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200949 }
950 }
951#endif
952
Simon Glass0169e6b2012-02-13 13:51:18 +0000953 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +0200954 switch (argc) {
955 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200956 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese198b23e2006-10-28 15:55:52 +0200957 boot_device = getenv("bootdevice");
958 break;
959 case 2:
960 addr = simple_strtoul(argv[1], NULL, 16);
961 boot_device = getenv("bootdevice");
962 break;
963 case 3:
964 addr = simple_strtoul(argv[1], NULL, 16);
965 boot_device = argv[2];
966 break;
967 case 4:
968 addr = simple_strtoul(argv[1], NULL, 16);
969 boot_device = argv[2];
970 offset = simple_strtoul(argv[3], NULL, 16);
971 break;
972 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200973#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200974usage:
975#endif
Simon Glass0169e6b2012-02-13 13:51:18 +0000976 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +0000977 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +0200978 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000979 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +0200980
981 if (!boot_device) {
982 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000983 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200984 return 1;
985 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000986 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100987
Stefan Roese198b23e2006-10-28 15:55:52 +0200988 idx = simple_strtoul(boot_device, NULL, 16);
989
Scott Wood2c1b7e12016-05-30 13:57:55 -0500990 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx]->name) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200991 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +0000992 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200993 return 1;
994 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000995 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200996
Scott Wood2c1b7e12016-05-30 13:57:55 -0500997 return nand_load_image(cmdtp, nand_info[idx], offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200998}
999
1000U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001001 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001002 "[partition] | [[[loadAddr] dev] offset]"
1003);