blob: 5bda69e819b0c4aa4ec16becba073866db3e7694 [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>
Simon Glass313112a2019-08-01 09:46:46 -060026#include <env.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010027#include <watchdog.h>
28#include <malloc.h>
29#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010030#include <jffs2/jffs2.h>
31#include <nand.h>
32
Miquel Raynal617fd792019-10-25 19:39:29 +020033#include "legacy-mtd-utils.h"
34
Ladislav Michl9d0928d2009-04-21 02:26:31 +020035#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020036
Wolfgang Denk161bfc52009-11-17 21:27:39 +010037/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020038int mtdparts_init(void);
Stefan Roese198b23e2006-10-28 15:55:52 +020039int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020040 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020041#endif
42
Scott Wood08364d92016-05-30 13:57:54 -050043static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
44 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010045{
46 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010047 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -050048 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +090049 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -050050
51 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -050052 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -050053
54 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010055
Scott Wood08364d92016-05-30 13:57:54 -050056 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090057 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010058 puts("No memory for page buffer\n");
59 return 1;
60 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +090061
Scott Wood08364d92016-05-30 13:57:54 -050062 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090063 if (!oobbuf) {
64 puts("No memory for page buffer\n");
65 ret = 1;
66 goto free_dat;
67 }
Scott Wood08364d92016-05-30 13:57:54 -050068 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010069 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +010070 struct mtd_oob_ops ops;
71 memset(&ops, 0, sizeof(ops));
72 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -070073 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -050074 ops.len = mtd->writesize;
75 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +000076 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -050077 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010078 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020079 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090080 ret = 1;
81 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010082 }
Stefan Roese3167d072008-07-10 10:10:54 +020083 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020084
Masahiro Yamada3f97e272013-07-11 17:27:13 +090085 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -050086 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +090087 p = datbuf;
88
89 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +010090 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
91 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
92 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Woodcc5f3392008-06-12 13:20:16 -050093 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
94 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +090095 p += 16;
96 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010097 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +090098
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010099 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -0500100 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -0700101 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100102 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100103 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
104 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100105 p += 8;
106 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900107
108free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100109 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900110free_dat:
111 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100112
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900113 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100114}
115
116/* ------------------------------------------------------------------------- */
117
Scott Woode5836dc2010-08-02 13:04:24 -0500118static int set_dev(int dev)
119{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500120 struct mtd_info *mtd = get_nand_dev_by_index(dev);
121
122 if (!mtd)
123 return -ENODEV;
Scott Woode5836dc2010-08-02 13:04:24 -0500124
125 if (nand_curr_device == dev)
126 return 0;
127
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500128 printf("Device %d: %s", dev, mtd->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500129 puts("... is now current device\n");
130 nand_curr_device = dev;
131
132#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashkocd3ffa12017-06-26 19:12:58 -0500133 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500134#endif
135
136 return 0;
137}
138
Nishanth Menonb20f8402008-12-13 09:43:06 -0600139#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
140static void print_status(ulong start, ulong end, ulong erasesize, int status)
141{
Joe Hershberger53768562012-08-22 16:49:45 -0500142 /*
143 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
144 * not the same as others. Instead of bit 1 being lock, it is
145 * #lock_tight. To make the driver support either format, ignore bit 1
146 * and use only bit 0 and bit 2.
147 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600148 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
149 start,
150 end - 1,
151 (end - start) / erasesize,
152 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500153 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600154 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
155}
156
Scott Wood08364d92016-05-30 13:57:54 -0500157static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600158{
159 ulong block_start = 0;
160 ulong off;
161 int last_status = -1;
162
Scott Wood17fed142016-05-30 13:57:56 -0500163 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600164 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500165 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600166 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500167 (nand_chip->read_byte(mtd) & 0x80 ?
168 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600169
Scott Wood08364d92016-05-30 13:57:54 -0500170 for (off = 0; off < mtd->size; off += mtd->erasesize) {
171 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600172
173 /* print message only if status has changed */
174 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500175 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600176 last_status);
177 block_start = off;
178 }
179 last_status = s;
180 }
181 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500182 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600183}
184#endif
185
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400186#ifdef CONFIG_ENV_OFFSET_OOB
187unsigned long nand_env_oob_offset;
188
Scott Woode5836dc2010-08-02 13:04:24 -0500189int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400190{
191 int ret;
192 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500193 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400194 char *cmd = argv[1];
195
Scott Wood84f41482016-09-01 17:31:31 -0500196 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woode5836dc2010-08-02 13:04:24 -0500197 puts("no devices available\n");
198 return 1;
199 }
200
201 set_dev(0);
202
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400203 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500204 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500205 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400206 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500207
208 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400209 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500210 loff_t addr;
211 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400212 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500213 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400214
215 if (argc < 3)
216 goto usage;
217
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500218 mtd = get_nand_dev_by_index(idx);
Tom Rini32d96182013-03-14 05:32:50 +0000219 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200220 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500221 MTD_DEV_TYPE_NAND, mtd->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500222 puts("Offset or partition name expected\n");
223 return 1;
224 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200225 if (set_dev(idx)) {
226 puts("Offset or partition name expected\n");
227 return 1;
228 }
Scott Woode5836dc2010-08-02 13:04:24 -0500229
230 if (idx != 0) {
231 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400232 return 1;
233 }
234
Scott Wood08364d92016-05-30 13:57:54 -0500235 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500236 printf("Insufficient available OOB bytes:\n"
237 "%d OOB bytes available but %d required for "
238 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500239 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400240 return 1;
241 }
242
Scott Wood08364d92016-05-30 13:57:54 -0500243 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400244 printf("Environment offset must be block-aligned\n");
245 return 1;
246 }
247
248 ops.datbuf = NULL;
249 ops.mode = MTD_OOB_AUTO;
250 ops.ooboffs = 0;
251 ops.ooblen = ENV_OFFSET_SIZE;
252 ops.oobbuf = (void *) oob_buf;
253
254 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood08364d92016-05-30 13:57:54 -0500255 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400256
Scott Wood08364d92016-05-30 13:57:54 -0500257 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500258 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400259 printf("Error writing OOB block 0\n");
260 return ret;
261 }
Scott Wood0268da02010-07-12 18:17:40 -0500262
Scott Wood08364d92016-05-30 13:57:54 -0500263 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500264 if (ret) {
265 printf("Error reading env offset in OOB\n");
266 return ret;
267 }
268
269 if (addr != nand_env_oob_offset) {
270 printf("Verification of env offset in OOB failed: "
Scott Woode5836dc2010-08-02 13:04:24 -0500271 "0x%08llx expected but got 0x%08lx\n",
272 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500273 return 1;
274 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400275 } else {
276 goto usage;
277 }
278
279 return ret;
280
281usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000282 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400283}
284
285#endif
286
Marek Vasut185ce252011-09-22 03:57:26 +0200287static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100288{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500289 struct mtd_info *mtd;
290 struct nand_chip *chip;
291
292 mtd = get_nand_dev_by_index(idx);
293 if (!mtd)
294 return;
Marek Vasut185ce252011-09-22 03:57:26 +0200295
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500296 chip = mtd_to_nand(mtd);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100297 printf("Device %d: ", idx);
298 if (chip->numchips > 1)
299 printf("%dx ", chip->numchips);
300 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500301 mtd->name, mtd->erasesize >> 10);
302 printf(" Page size %8d b\n", mtd->writesize);
303 printf(" OOB size %8d b\n", mtd->oobsize);
304 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocherfb9a9bd2015-04-12 10:18:09 +0200305 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmannd8ea9812017-07-10 08:44:24 +0200306 printf(" options 0x%08x\n", chip->options);
307 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasut185ce252011-09-22 03:57:26 +0200308
309 /* Set geometry info */
Simon Glass4d949a22017-08-03 12:22:10 -0600310 env_set_hex("nand_writesize", mtd->writesize);
311 env_set_hex("nand_oobsize", mtd->oobsize);
312 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100313}
314
Scott Wood08364d92016-05-30 13:57:54 -0500315static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillona7468e22016-06-15 10:42:18 +0200316 ulong count, int read, int no_verify)
Scott Wood733fb022012-03-02 14:01:57 -0600317{
318 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600319
320 while (count--) {
321 /* Raw access */
322 mtd_oob_ops_t ops = {
323 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500324 .oobbuf = ((u8 *)addr) + mtd->writesize,
325 .len = mtd->writesize,
326 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000327 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600328 };
329
Peter Tyser5fa8b982015-02-03 11:58:13 -0600330 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500331 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600332 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500333 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillona7468e22016-06-15 10:42:18 +0200334 if (!ret && !no_verify)
Scott Wood08364d92016-05-30 13:57:54 -0500335 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600336 }
Scott Wood733fb022012-03-02 14:01:57 -0600337
338 if (ret) {
339 printf("%s: error at offset %llx, ret %d\n",
340 __func__, (long long)off, ret);
341 break;
342 }
343
Scott Wood08364d92016-05-30 13:57:54 -0500344 addr += mtd->writesize + mtd->oobsize;
345 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600346 }
347
348 return ret;
349}
350
Harvey Chapman39e68862013-02-26 17:57:14 +0000351/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500352 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000353 */
354static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
355{
356 /* We grab the nand info object here fresh because this is usually
357 * called after arg_off_size() which can change the value of dev.
358 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500359 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapman39e68862013-02-26 17:57:14 +0000360 loff_t maxoffset = offset + *size;
361 int badblocks = 0;
362
363 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500364 for (; offset < maxoffset; offset += mtd->erasesize) {
365 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000366 badblocks++;
367 }
368 /* adjust size if any bad blocks found */
369 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500370 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000371 printf("size adjusted to 0x%llx (%d bad blocks)\n",
372 (unsigned long long)*size, badblocks);
373 }
374}
375
Kim Phillipsdc00a682012-10-29 13:34:31 +0000376static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100377{
Scott Woode5836dc2010-08-02 13:04:24 -0500378 int i, ret = 0;
379 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000380 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100381 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500382 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200383#ifdef CONFIG_SYS_NAND_QUIET
384 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200385#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200386 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200387#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600388 const char *quiet_str = env_get("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500389 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500390 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100391
392 /* at least two arguments please */
393 if (argc < 2)
394 goto usage;
395
Stefan Roesed351b2b2006-10-10 12:36:02 +0200396 if (quiet_str)
397 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
398
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100399 cmd = argv[1];
400
Scott Wood67d5a2a2010-08-25 15:24:01 -0500401 /* Only "dump" is repeatable. */
402 if (repeat && strcmp(cmd, "dump"))
403 return 0;
404
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100405 if (strcmp(cmd, "info") == 0) {
406
407 putc('\n');
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500408 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
409 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100410 return 0;
411 }
412
413 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100414 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100415 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500416 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100417 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100418 else
Marek Vasut185ce252011-09-22 03:57:26 +0200419 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100420 return 0;
421 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200422
Scott Woode5836dc2010-08-02 13:04:24 -0500423 dev = (int)simple_strtoul(argv[2], NULL, 10);
424 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200425
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100426 return 0;
427 }
428
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400429#ifdef CONFIG_ENV_OFFSET_OOB
430 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500431 if (strcmp(cmd, "env.oob") == 0)
432 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400433#endif
434
Scott Woode5836dc2010-08-02 13:04:24 -0500435 /* The following commands operate on the current device, unless
436 * overridden by a partition specifier. Note that if somehow the
437 * current device is invalid, it will have to be changed to a valid
438 * one before these commands can run, even if a partition specifier
439 * for another device is to be used.
440 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500441 mtd = get_nand_dev_by_index(dev);
442 if (!mtd) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100443 puts("\nno devices available\n");
444 return 1;
445 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100446
447 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500448 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500449 for (off = 0; off < mtd->size; off += mtd->erasesize)
450 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500451 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100452 return 0;
453 }
454
Stefan Roese198b23e2006-10-28 15:55:52 +0200455 /*
456 * Syntax is:
457 * 0 1 2 3 4
458 * nand erase [clean] [off size]
459 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500460 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200461 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200462 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100463 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200464 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
465 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500466 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500467 int spread = 0;
468 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200469 const char *scrub_warn =
470 "Warning: "
471 "scrub option will erase all factory set bad blocks!\n"
472 " "
473 "There is no reliable way to recover them.\n"
474 " "
475 "Use this command only for testing purposes if you\n"
476 " "
477 "are sure of what you are doing!\n"
478 "\nReally scrub this NAND flash? <y/N>\n";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200479
Scott Wood1b5cd512010-08-25 14:43:29 -0500480 if (cmd[5] != 0) {
481 if (!strcmp(&cmd[5], ".spread")) {
482 spread = 1;
483 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500484 args = 1;
485 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500486 args = 0;
487 } else {
488 goto usage;
489 }
490 }
491
492 /*
493 * Don't allow missing arguments to cause full chip/partition
494 * erases -- easy to do accidentally, e.g. with a misspelled
495 * variable name.
496 */
497 if (argc != o + args)
498 goto usage;
499
500 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200501 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200502 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
503 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500504 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200505 return 1;
506
507 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200508 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200509
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500510 mtd = get_nand_dev_by_index(dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500511
Stefan Roesed351b2b2006-10-10 12:36:02 +0200512 memset(&opts, 0, sizeof(opts));
513 opts.offset = off;
514 opts.length = size;
515 opts.jffs2 = clean;
516 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500517 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200518
519 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200520 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200521 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200522 } else {
523 puts(scrub_warn);
524 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100525 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200526 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100527 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900528 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100529 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200530 }
531 }
Scott Wood08364d92016-05-30 13:57:54 -0500532 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100533 printf("%s\n", ret ? "ERROR" : "OK");
534
535 return ret == 0 ? 0 : 1;
536 }
537
538 if (strncmp(cmd, "dump", 4) == 0) {
539 if (argc < 3)
540 goto usage;
541
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100542 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood08364d92016-05-30 13:57:54 -0500543 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100544
545 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100546 }
547
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100548 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500549 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600550 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200551 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000552 int raw = 0;
Boris Brezillona7468e22016-06-15 10:42:18 +0200553 int no_verify = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200554
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100555 if (argc < 4)
556 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200557
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100558 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
559
Stefan Roesed351b2b2006-10-10 12:36:02 +0200560 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200561 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100562
Stefan Roesed351b2b2006-10-10 12:36:02 +0200563 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600564
Boris Brezillona7468e22016-06-15 10:42:18 +0200565 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood733fb022012-03-02 14:01:57 -0600566 raw = 1;
567
Boris Brezillona7468e22016-06-15 10:42:18 +0200568 if (!strcmp(s, ".raw.noverify"))
569 no_verify = 1;
570
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200571 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
572 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500573 mtd->size))
Scott Wood733fb022012-03-02 14:01:57 -0600574 return 1;
575
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200576 if (set_dev(dev))
577 return 1;
578
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500579 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200580
Scott Wood733fb022012-03-02 14:01:57 -0600581 if (argc > 4 && !str2long(argv[4], &pagecount)) {
582 printf("'%s' is not a number\n", argv[4]);
583 return 1;
584 }
585
Scott Wood08364d92016-05-30 13:57:54 -0500586 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600587 puts("Size exceeds partition or device limit\n");
588 return -1;
589 }
590
Scott Wood08364d92016-05-30 13:57:54 -0500591 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600592 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200593 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
594 &size, &maxsize,
595 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500596 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200597 return 1;
598
599 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600600 return 1;
601
Harvey Chapman39e68862013-02-26 17:57:14 +0000602 /* size is unspecified */
603 if (argc < 5)
604 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600605 rwsize = size;
606 }
607
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500608 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200609
Scott Wood12b1c952008-06-12 13:13:23 -0500610 if (!s || !strcmp(s, ".jffs2") ||
611 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500612 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500613 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000614 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200615 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500616 else
Scott Wood08364d92016-05-30 13:57:54 -0500617 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000618 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600619 (u_char *)addr,
620 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400621#ifdef CONFIG_CMD_NAND_TRIMFFS
622 } else if (!strcmp(s, ".trimffs")) {
623 if (read) {
624 printf("Unknown nand command suffix '%s'\n", s);
625 return 1;
626 }
Scott Wood08364d92016-05-30 13:57:54 -0500627 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000628 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600629 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400630#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400631 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100632 /* out-of-band data */
633 mtd_oob_ops_t ops = {
634 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500635 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000636 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100637 };
638
Harald Welte5a395102007-12-19 14:12:53 +0100639 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500640 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100641 else
Scott Wood08364d92016-05-30 13:57:54 -0500642 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600643 } else if (raw) {
Boris Brezillona7468e22016-06-15 10:42:18 +0200644 ret = raw_access(mtd, addr, off, pagecount, read,
645 no_verify);
Stefan Roese198b23e2006-10-28 15:55:52 +0200646 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500647 printf("Unknown nand command suffix '%s'.\n", s);
648 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200649 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100650
Scott Woode5836dc2010-08-02 13:04:24 -0500651 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200652 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100653
654 return ret == 0 ? 0 : 1;
655 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200656
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100657#ifdef CONFIG_CMD_NAND_TORTURE
658 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200659 loff_t endoff;
660 unsigned int failed = 0, passed = 0;
661
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100662 if (argc < 3)
663 goto usage;
664
665 if (!str2off(argv[2], &off)) {
666 puts("Offset is not a valid number\n");
667 return 1;
668 }
669
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200670 size = mtd->erasesize;
671 if (argc > 3) {
672 if (!str2off(argv[3], &size)) {
673 puts("Size is not a valid number\n");
674 return 1;
675 }
676 }
677
678 endoff = off + size;
679 if (endoff > mtd->size) {
680 puts("Arguments beyond end of NAND\n");
681 return 1;
682 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100683
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200684 off = round_down(off, mtd->erasesize);
685 endoff = round_up(endoff, mtd->erasesize);
686 size = endoff - off;
687 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
688 dev, off, size, mtd->erasesize);
689 while (off < endoff) {
690 ret = nand_torture(mtd, off);
691 if (ret) {
692 failed++;
693 printf(" block at 0x%llx failed\n", off);
694 } else {
695 passed++;
696 }
697 off += mtd->erasesize;
698 }
699 printf(" Passed: %u, failed: %u\n", passed, failed);
700 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100701 }
702#endif
703
Stefan Roesed351b2b2006-10-10 12:36:02 +0200704 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200705 argc -= 2;
706 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200707
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200708 if (argc <= 0)
709 goto usage;
710
711 while (argc > 0) {
712 addr = simple_strtoul(*argv, NULL, 16);
713
Scott Wood08364d92016-05-30 13:57:54 -0500714 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200715 printf("block 0x%08lx NOT marked "
716 "as bad! ERROR %d\n",
717 addr, ret);
718 ret = 1;
719 } else {
720 printf("block 0x%08lx successfully "
721 "marked as bad\n",
722 addr);
723 }
724 --argc;
725 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200726 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200727 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200728 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500729
Stefan Roesed351b2b2006-10-10 12:36:02 +0200730 if (strcmp(cmd, "biterr") == 0) {
731 /* todo */
732 return 1;
733 }
734
Nishanth Menonb20f8402008-12-13 09:43:06 -0600735#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200736 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600737 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200738 int status = 0;
739 if (argc == 3) {
740 if (!strcmp("tight", argv[2]))
741 tight = 1;
742 if (!strcmp("status", argv[2]))
743 status = 1;
744 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200745 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500746 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100747 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500748 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100749 puts("NAND flash successfully locked\n");
750 } else {
751 puts("Error locking NAND flash\n");
752 return 1;
753 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200754 }
755 return 0;
756 }
757
Joe Hershbergercccf5952012-08-22 16:49:42 -0500758 if (strncmp(cmd, "unlock", 5) == 0) {
759 int allexcept = 0;
760
761 s = strchr(cmd, '.');
762
763 if (s && !strcmp(s, ".allexcept"))
764 allexcept = 1;
765
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200766 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
767 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500768 mtd->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200769 return 1;
770
771 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200772 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200773
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500774 mtd = get_nand_dev_by_index(dev);
775
776 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100777 puts("NAND flash successfully unlocked\n");
778 } else {
779 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100780 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100781 return 1;
782 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200783 return 0;
784 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600785#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200786
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100787usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000788 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100789}
790
Kim Phillipsdc00a682012-10-29 13:34:31 +0000791#ifdef CONFIG_SYS_LONGHELP
792static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200793 "info - show available NAND devices\n"
794 "nand device [dev] - show or set current device\n"
795 "nand read - addr off|partition size\n"
796 "nand write - addr off|partition size\n"
797 " read/write 'size' bytes starting at offset 'off'\n"
798 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600799 "nand read.raw - addr off|partition [count]\n"
Boris Brezillona7468e22016-06-15 10:42:18 +0200800 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood733fb022012-03-02 14:01:57 -0600801 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400802#ifdef CONFIG_CMD_NAND_TRIMFFS
803 "nand write.trimffs - addr off|partition size\n"
804 " write 'size' bytes starting at offset 'off' from memory address\n"
805 " 'addr', skipping bad blocks and dropping any pages at the end\n"
806 " of eraseblocks that contain only 0xFF\n"
807#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200808 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500809 "from offset 'off'\n"
810 " With '.spread', erase enough for given file size, otherwise,\n"
811 " 'size' includes skipped bad blocks.\n"
812 "nand erase.part [clean] partition - erase entire mtd partition'\n"
813 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200814 "nand bad - show bad blocks\n"
815 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100816#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200817 "nand torture off - torture one block at offset\n"
818 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100819#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200820 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500821 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200822 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
823 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600824#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200825 "\n"
826 "nand lock [tight] [status]\n"
827 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500828 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600829#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400830#ifdef CONFIG_ENV_OFFSET_OOB
831 "\n"
832 "nand env.oob - environment offset in OOB of block 0 of"
833 " first device.\n"
834 "nand env.oob set off|partition - set enviromnent offset\n"
835 "nand env.oob get - get environment offset"
836#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000837 "";
838#endif
839
840U_BOOT_CMD(
841 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
842 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600843);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100844
Scott Wood08364d92016-05-30 13:57:54 -0500845static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200846 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100847{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100848 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000849 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200850 size_t cnt;
Tom Rinic220bd92019-05-23 07:14:07 -0400851#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100852 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200853#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100854#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200855 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100856#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200857
858 s = strchr(cmd, '.');
859 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500860 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500861 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000862 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500863 return 1;
864 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100865
Scott Wood08364d92016-05-30 13:57:54 -0500866 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100867
Scott Wood08364d92016-05-30 13:57:54 -0500868 cnt = mtd->writesize;
869 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
870 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100871 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200872 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000873 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100874 return 1;
875 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000876 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100877
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100878 switch (genimg_get_format ((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400879#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100880 case IMAGE_FORMAT_LEGACY:
881 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100882
Simon Glass0169e6b2012-02-13 13:51:18 +0000883 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100884 image_print_contents (hdr);
885
886 cnt = image_get_image_size (hdr);
887 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200888#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100889#if defined(CONFIG_FIT)
890 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100891 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100892 puts ("Fit image detected...\n");
893
894 cnt = fit_get_size (fit_hdr);
895 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100896#endif
897 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000898 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100899 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100900 return 1;
901 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000902 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100903
Scott Wood08364d92016-05-30 13:57:54 -0500904 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
905 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100906 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200907 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000908 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100909 return 1;
910 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000911 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100912
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100913#if defined(CONFIG_FIT)
914 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200915 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
916 if (!fit_check_format (fit_hdr)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000917 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200918 puts ("** Bad FIT image format\n");
919 return 1;
920 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000921 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200922 fit_print_contents (fit_hdr);
923 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100924#endif
925
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100926 /* Loading ok, update default load address */
927
928 load_addr = addr;
929
Mike Frysinger194c2e82011-06-05 13:43:02 +0000930 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100931}
932
Kim Phillipsdc00a682012-10-29 13:34:31 +0000933static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
934 char * const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +0200935{
936 char *boot_device = NULL;
937 int idx;
938 ulong addr, offset = 0;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500939 struct mtd_info *mtd;
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200940#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200941 struct mtd_device *dev;
942 struct part_info *part;
943 u8 pnum;
944
945 if (argc >= 2) {
946 char *p = (argc == 2) ? argv[1] : argv[2];
947 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
948 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
949 if (dev->id->type != MTD_DEV_TYPE_NAND) {
950 puts("Not a NAND device\n");
951 return 1;
952 }
953 if (argc > 3)
954 goto usage;
955 if (argc == 3)
Thomas Knobloche84b7772007-07-06 14:58:39 +0200956 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200957 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200958 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500959
960 mtd = get_nand_dev_by_index(dev->id->num);
961 return nand_load_image(cmdtp, mtd, part->offset,
962 addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200963 }
964 }
965#endif
966
Simon Glass0169e6b2012-02-13 13:51:18 +0000967 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +0200968 switch (argc) {
969 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200970 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass64b723f2017-08-03 12:22:12 -0600971 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200972 break;
973 case 2:
974 addr = simple_strtoul(argv[1], NULL, 16);
Simon Glass64b723f2017-08-03 12:22:12 -0600975 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200976 break;
977 case 3:
978 addr = simple_strtoul(argv[1], NULL, 16);
979 boot_device = argv[2];
980 break;
981 case 4:
982 addr = simple_strtoul(argv[1], NULL, 16);
983 boot_device = argv[2];
984 offset = simple_strtoul(argv[3], NULL, 16);
985 break;
986 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200987#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200988usage:
989#endif
Simon Glass0169e6b2012-02-13 13:51:18 +0000990 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +0000991 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +0200992 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000993 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +0200994
995 if (!boot_device) {
996 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000997 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200998 return 1;
999 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001000 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001001
Stefan Roese198b23e2006-10-28 15:55:52 +02001002 idx = simple_strtoul(boot_device, NULL, 16);
1003
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001004 mtd = get_nand_dev_by_index(idx);
1005 if (!mtd) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001006 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +00001007 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001008 return 1;
1009 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001010 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001011
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001012 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001013}
1014
1015U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001016 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001017 "[partition] | [[[loadAddr] dev] offset]"
1018);