blob: 92d039af8f52c6bf9ceb61bffbc58d713e52c1e8 [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>
Simon Glass1ea97892020-05-10 11:40:00 -060023#include <bootstage.h>
Simon Glass85f13782019-12-28 10:45:03 -070024#include <image.h>
Simon Glass274e0b02020-05-10 11:39:56 -060025#include <asm/cache.h>
William Juul52c07962007-10-31 13:53:06 +010026#include <linux/mtd/mtd.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010027#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070028#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060029#include <env.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010030#include <watchdog.h>
31#include <malloc.h>
32#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010033#include <jffs2/jffs2.h>
34#include <nand.h>
35
Miquel Raynal617fd792019-10-25 19:39:29 +020036#include "legacy-mtd-utils.h"
37
Ladislav Michl9d0928d2009-04-21 02:26:31 +020038#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020039
Wolfgang Denk161bfc52009-11-17 21:27:39 +010040/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020041int mtdparts_init(void);
Stefan Roese198b23e2006-10-28 15:55:52 +020042int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020043 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020044#endif
45
Scott Wood08364d92016-05-30 13:57:54 -050046static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
47 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010048{
49 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010050 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -050051 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +090052 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -050053
54 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -050055 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -050056
57 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010058
Scott Wood08364d92016-05-30 13:57:54 -050059 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090060 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010061 puts("No memory for page buffer\n");
62 return 1;
63 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +090064
Scott Wood08364d92016-05-30 13:57:54 -050065 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090066 if (!oobbuf) {
67 puts("No memory for page buffer\n");
68 ret = 1;
69 goto free_dat;
70 }
Scott Wood08364d92016-05-30 13:57:54 -050071 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010072 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +010073 struct mtd_oob_ops ops;
74 memset(&ops, 0, sizeof(ops));
75 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -070076 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -050077 ops.len = mtd->writesize;
78 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +000079 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -050080 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010081 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020082 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090083 ret = 1;
84 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010085 }
Stefan Roese3167d072008-07-10 10:10:54 +020086 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020087
Masahiro Yamada3f97e272013-07-11 17:27:13 +090088 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -050089 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +090090 p = datbuf;
91
92 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +010093 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
94 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
95 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Woodcc5f3392008-06-12 13:20:16 -050096 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
97 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +090098 p += 16;
99 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100100 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +0900101
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100102 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -0500103 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -0700104 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100105 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100106 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
107 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100108 p += 8;
109 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900110
111free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100112 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900113free_dat:
114 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100115
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900116 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100117}
118
119/* ------------------------------------------------------------------------- */
120
Scott Woode5836dc2010-08-02 13:04:24 -0500121static int set_dev(int dev)
122{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500123 struct mtd_info *mtd = get_nand_dev_by_index(dev);
124
125 if (!mtd)
126 return -ENODEV;
Scott Woode5836dc2010-08-02 13:04:24 -0500127
128 if (nand_curr_device == dev)
129 return 0;
130
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500131 printf("Device %d: %s", dev, mtd->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500132 puts("... is now current device\n");
133 nand_curr_device = dev;
134
135#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashkocd3ffa12017-06-26 19:12:58 -0500136 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500137#endif
138
139 return 0;
140}
141
Nishanth Menonb20f8402008-12-13 09:43:06 -0600142#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
143static void print_status(ulong start, ulong end, ulong erasesize, int status)
144{
Joe Hershberger53768562012-08-22 16:49:45 -0500145 /*
146 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
147 * not the same as others. Instead of bit 1 being lock, it is
148 * #lock_tight. To make the driver support either format, ignore bit 1
149 * and use only bit 0 and bit 2.
150 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600151 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
152 start,
153 end - 1,
154 (end - start) / erasesize,
155 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500156 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600157 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
158}
159
Scott Wood08364d92016-05-30 13:57:54 -0500160static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600161{
162 ulong block_start = 0;
163 ulong off;
164 int last_status = -1;
165
Scott Wood17fed142016-05-30 13:57:56 -0500166 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600167 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500168 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600169 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500170 (nand_chip->read_byte(mtd) & 0x80 ?
171 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600172
Scott Wood08364d92016-05-30 13:57:54 -0500173 for (off = 0; off < mtd->size; off += mtd->erasesize) {
174 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600175
176 /* print message only if status has changed */
177 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500178 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600179 last_status);
180 block_start = off;
181 }
182 last_status = s;
183 }
184 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500185 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600186}
187#endif
188
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400189#ifdef CONFIG_ENV_OFFSET_OOB
190unsigned long nand_env_oob_offset;
191
Simon Glassed38aef2020-05-10 11:40:03 -0600192int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400193{
194 int ret;
195 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500196 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400197 char *cmd = argv[1];
198
Scott Wood84f41482016-09-01 17:31:31 -0500199 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woode5836dc2010-08-02 13:04:24 -0500200 puts("no devices available\n");
201 return 1;
202 }
203
204 set_dev(0);
205
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400206 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500207 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500208 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400209 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500210
211 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400212 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500213 loff_t addr;
214 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400215 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500216 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400217
218 if (argc < 3)
219 goto usage;
220
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500221 mtd = get_nand_dev_by_index(idx);
Tom Rini32d96182013-03-14 05:32:50 +0000222 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200223 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500224 MTD_DEV_TYPE_NAND, mtd->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500225 puts("Offset or partition name expected\n");
226 return 1;
227 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200228 if (set_dev(idx)) {
229 puts("Offset or partition name expected\n");
230 return 1;
231 }
Scott Woode5836dc2010-08-02 13:04:24 -0500232
233 if (idx != 0) {
234 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400235 return 1;
236 }
237
Scott Wood08364d92016-05-30 13:57:54 -0500238 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500239 printf("Insufficient available OOB bytes:\n"
240 "%d OOB bytes available but %d required for "
241 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500242 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400243 return 1;
244 }
245
Scott Wood08364d92016-05-30 13:57:54 -0500246 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400247 printf("Environment offset must be block-aligned\n");
248 return 1;
249 }
250
251 ops.datbuf = NULL;
252 ops.mode = MTD_OOB_AUTO;
253 ops.ooboffs = 0;
254 ops.ooblen = ENV_OFFSET_SIZE;
255 ops.oobbuf = (void *) oob_buf;
256
257 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood08364d92016-05-30 13:57:54 -0500258 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400259
Scott Wood08364d92016-05-30 13:57:54 -0500260 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500261 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400262 printf("Error writing OOB block 0\n");
263 return ret;
264 }
Scott Wood0268da02010-07-12 18:17:40 -0500265
Scott Wood08364d92016-05-30 13:57:54 -0500266 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500267 if (ret) {
268 printf("Error reading env offset in OOB\n");
269 return ret;
270 }
271
272 if (addr != nand_env_oob_offset) {
273 printf("Verification of env offset in OOB failed: "
Scott Woode5836dc2010-08-02 13:04:24 -0500274 "0x%08llx expected but got 0x%08lx\n",
275 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500276 return 1;
277 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400278 } else {
279 goto usage;
280 }
281
282 return ret;
283
284usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000285 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400286}
287
288#endif
289
Marek Vasut185ce252011-09-22 03:57:26 +0200290static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100291{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500292 struct mtd_info *mtd;
293 struct nand_chip *chip;
294
295 mtd = get_nand_dev_by_index(idx);
296 if (!mtd)
297 return;
Marek Vasut185ce252011-09-22 03:57:26 +0200298
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500299 chip = mtd_to_nand(mtd);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100300 printf("Device %d: ", idx);
301 if (chip->numchips > 1)
302 printf("%dx ", chip->numchips);
303 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500304 mtd->name, mtd->erasesize >> 10);
305 printf(" Page size %8d b\n", mtd->writesize);
306 printf(" OOB size %8d b\n", mtd->oobsize);
307 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocherfb9a9bd2015-04-12 10:18:09 +0200308 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmannd8ea9812017-07-10 08:44:24 +0200309 printf(" options 0x%08x\n", chip->options);
310 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasut185ce252011-09-22 03:57:26 +0200311
312 /* Set geometry info */
Simon Glass4d949a22017-08-03 12:22:10 -0600313 env_set_hex("nand_writesize", mtd->writesize);
314 env_set_hex("nand_oobsize", mtd->oobsize);
315 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100316}
317
Scott Wood08364d92016-05-30 13:57:54 -0500318static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillona7468e22016-06-15 10:42:18 +0200319 ulong count, int read, int no_verify)
Scott Wood733fb022012-03-02 14:01:57 -0600320{
321 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600322
323 while (count--) {
324 /* Raw access */
325 mtd_oob_ops_t ops = {
326 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500327 .oobbuf = ((u8 *)addr) + mtd->writesize,
328 .len = mtd->writesize,
329 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000330 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600331 };
332
Peter Tyser5fa8b982015-02-03 11:58:13 -0600333 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500334 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600335 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500336 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillona7468e22016-06-15 10:42:18 +0200337 if (!ret && !no_verify)
Scott Wood08364d92016-05-30 13:57:54 -0500338 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600339 }
Scott Wood733fb022012-03-02 14:01:57 -0600340
341 if (ret) {
342 printf("%s: error at offset %llx, ret %d\n",
343 __func__, (long long)off, ret);
344 break;
345 }
346
Scott Wood08364d92016-05-30 13:57:54 -0500347 addr += mtd->writesize + mtd->oobsize;
348 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600349 }
350
351 return ret;
352}
353
Harvey Chapman39e68862013-02-26 17:57:14 +0000354/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500355 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000356 */
357static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
358{
359 /* We grab the nand info object here fresh because this is usually
360 * called after arg_off_size() which can change the value of dev.
361 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500362 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapman39e68862013-02-26 17:57:14 +0000363 loff_t maxoffset = offset + *size;
364 int badblocks = 0;
365
366 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500367 for (; offset < maxoffset; offset += mtd->erasesize) {
368 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000369 badblocks++;
370 }
371 /* adjust size if any bad blocks found */
372 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500373 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000374 printf("size adjusted to 0x%llx (%d bad blocks)\n",
375 (unsigned long long)*size, badblocks);
376 }
377}
378
Simon Glassed38aef2020-05-10 11:40:03 -0600379static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
380 char *const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100381{
Scott Woode5836dc2010-08-02 13:04:24 -0500382 int i, ret = 0;
383 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000384 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100385 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500386 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200387#ifdef CONFIG_SYS_NAND_QUIET
388 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200389#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200390 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200391#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600392 const char *quiet_str = env_get("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500393 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500394 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100395
396 /* at least two arguments please */
397 if (argc < 2)
398 goto usage;
399
Stefan Roesed351b2b2006-10-10 12:36:02 +0200400 if (quiet_str)
401 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
402
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100403 cmd = argv[1];
404
Scott Wood67d5a2a2010-08-25 15:24:01 -0500405 /* Only "dump" is repeatable. */
406 if (repeat && strcmp(cmd, "dump"))
407 return 0;
408
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100409 if (strcmp(cmd, "info") == 0) {
410
411 putc('\n');
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500412 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
413 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100414 return 0;
415 }
416
417 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100418 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100419 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500420 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100421 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100422 else
Marek Vasut185ce252011-09-22 03:57:26 +0200423 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100424 return 0;
425 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200426
Scott Woode5836dc2010-08-02 13:04:24 -0500427 dev = (int)simple_strtoul(argv[2], NULL, 10);
428 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200429
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100430 return 0;
431 }
432
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400433#ifdef CONFIG_ENV_OFFSET_OOB
434 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500435 if (strcmp(cmd, "env.oob") == 0)
436 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400437#endif
438
Scott Woode5836dc2010-08-02 13:04:24 -0500439 /* The following commands operate on the current device, unless
440 * overridden by a partition specifier. Note that if somehow the
441 * current device is invalid, it will have to be changed to a valid
442 * one before these commands can run, even if a partition specifier
443 * for another device is to be used.
444 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500445 mtd = get_nand_dev_by_index(dev);
446 if (!mtd) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100447 puts("\nno devices available\n");
448 return 1;
449 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100450
451 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500452 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500453 for (off = 0; off < mtd->size; off += mtd->erasesize)
454 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500455 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100456 return 0;
457 }
458
Stefan Roese198b23e2006-10-28 15:55:52 +0200459 /*
460 * Syntax is:
461 * 0 1 2 3 4
462 * nand erase [clean] [off size]
463 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500464 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200465 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200466 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100467 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200468 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
469 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500470 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500471 int spread = 0;
472 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200473 const char *scrub_warn =
474 "Warning: "
475 "scrub option will erase all factory set bad blocks!\n"
476 " "
477 "There is no reliable way to recover them.\n"
478 " "
479 "Use this command only for testing purposes if you\n"
480 " "
481 "are sure of what you are doing!\n"
482 "\nReally scrub this NAND flash? <y/N>\n";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200483
Scott Wood1b5cd512010-08-25 14:43:29 -0500484 if (cmd[5] != 0) {
485 if (!strcmp(&cmd[5], ".spread")) {
486 spread = 1;
487 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500488 args = 1;
489 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500490 args = 0;
491 } else {
492 goto usage;
493 }
494 }
495
496 /*
497 * Don't allow missing arguments to cause full chip/partition
498 * erases -- easy to do accidentally, e.g. with a misspelled
499 * variable name.
500 */
501 if (argc != o + args)
502 goto usage;
503
504 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200505 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200506 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
507 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500508 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200509 return 1;
510
511 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200512 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200513
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500514 mtd = get_nand_dev_by_index(dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500515
Stefan Roesed351b2b2006-10-10 12:36:02 +0200516 memset(&opts, 0, sizeof(opts));
517 opts.offset = off;
518 opts.length = size;
519 opts.jffs2 = clean;
520 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500521 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200522
523 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200524 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200525 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200526 } else {
527 puts(scrub_warn);
528 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100529 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200530 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100531 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900532 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100533 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200534 }
535 }
Scott Wood08364d92016-05-30 13:57:54 -0500536 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100537 printf("%s\n", ret ? "ERROR" : "OK");
538
539 return ret == 0 ? 0 : 1;
540 }
541
542 if (strncmp(cmd, "dump", 4) == 0) {
543 if (argc < 3)
544 goto usage;
545
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100546 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood08364d92016-05-30 13:57:54 -0500547 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100548
549 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100550 }
551
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100552 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500553 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600554 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200555 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000556 int raw = 0;
Boris Brezillona7468e22016-06-15 10:42:18 +0200557 int no_verify = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200558
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100559 if (argc < 4)
560 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200561
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100562 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
563
Stefan Roesed351b2b2006-10-10 12:36:02 +0200564 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200565 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100566
Stefan Roesed351b2b2006-10-10 12:36:02 +0200567 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600568
Boris Brezillona7468e22016-06-15 10:42:18 +0200569 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood733fb022012-03-02 14:01:57 -0600570 raw = 1;
571
Boris Brezillona7468e22016-06-15 10:42:18 +0200572 if (!strcmp(s, ".raw.noverify"))
573 no_verify = 1;
574
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200575 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
576 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500577 mtd->size))
Scott Wood733fb022012-03-02 14:01:57 -0600578 return 1;
579
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200580 if (set_dev(dev))
581 return 1;
582
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500583 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200584
Scott Wood733fb022012-03-02 14:01:57 -0600585 if (argc > 4 && !str2long(argv[4], &pagecount)) {
586 printf("'%s' is not a number\n", argv[4]);
587 return 1;
588 }
589
Scott Wood08364d92016-05-30 13:57:54 -0500590 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600591 puts("Size exceeds partition or device limit\n");
592 return -1;
593 }
594
Scott Wood08364d92016-05-30 13:57:54 -0500595 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600596 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200597 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
598 &size, &maxsize,
599 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500600 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200601 return 1;
602
603 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600604 return 1;
605
Harvey Chapman39e68862013-02-26 17:57:14 +0000606 /* size is unspecified */
607 if (argc < 5)
608 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600609 rwsize = size;
610 }
611
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500612 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200613
Scott Wood12b1c952008-06-12 13:13:23 -0500614 if (!s || !strcmp(s, ".jffs2") ||
615 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500616 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500617 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000618 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200619 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500620 else
Scott Wood08364d92016-05-30 13:57:54 -0500621 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000622 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600623 (u_char *)addr,
624 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400625#ifdef CONFIG_CMD_NAND_TRIMFFS
626 } else if (!strcmp(s, ".trimffs")) {
627 if (read) {
628 printf("Unknown nand command suffix '%s'\n", s);
629 return 1;
630 }
Scott Wood08364d92016-05-30 13:57:54 -0500631 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000632 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600633 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400634#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400635 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100636 /* out-of-band data */
637 mtd_oob_ops_t ops = {
638 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500639 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000640 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100641 };
642
Harald Welte5a395102007-12-19 14:12:53 +0100643 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500644 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100645 else
Scott Wood08364d92016-05-30 13:57:54 -0500646 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600647 } else if (raw) {
Boris Brezillona7468e22016-06-15 10:42:18 +0200648 ret = raw_access(mtd, addr, off, pagecount, read,
649 no_verify);
Stefan Roese198b23e2006-10-28 15:55:52 +0200650 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500651 printf("Unknown nand command suffix '%s'.\n", s);
652 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200653 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100654
Scott Woode5836dc2010-08-02 13:04:24 -0500655 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200656 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100657
658 return ret == 0 ? 0 : 1;
659 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200660
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100661#ifdef CONFIG_CMD_NAND_TORTURE
662 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200663 loff_t endoff;
664 unsigned int failed = 0, passed = 0;
665
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100666 if (argc < 3)
667 goto usage;
668
669 if (!str2off(argv[2], &off)) {
670 puts("Offset is not a valid number\n");
671 return 1;
672 }
673
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200674 size = mtd->erasesize;
675 if (argc > 3) {
676 if (!str2off(argv[3], &size)) {
677 puts("Size is not a valid number\n");
678 return 1;
679 }
680 }
681
682 endoff = off + size;
683 if (endoff > mtd->size) {
684 puts("Arguments beyond end of NAND\n");
685 return 1;
686 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100687
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200688 off = round_down(off, mtd->erasesize);
689 endoff = round_up(endoff, mtd->erasesize);
690 size = endoff - off;
691 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
692 dev, off, size, mtd->erasesize);
693 while (off < endoff) {
694 ret = nand_torture(mtd, off);
695 if (ret) {
696 failed++;
697 printf(" block at 0x%llx failed\n", off);
698 } else {
699 passed++;
700 }
701 off += mtd->erasesize;
702 }
703 printf(" Passed: %u, failed: %u\n", passed, failed);
704 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100705 }
706#endif
707
Stefan Roesed351b2b2006-10-10 12:36:02 +0200708 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200709 argc -= 2;
710 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200711
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200712 if (argc <= 0)
713 goto usage;
714
715 while (argc > 0) {
716 addr = simple_strtoul(*argv, NULL, 16);
717
Scott Wood08364d92016-05-30 13:57:54 -0500718 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200719 printf("block 0x%08lx NOT marked "
720 "as bad! ERROR %d\n",
721 addr, ret);
722 ret = 1;
723 } else {
724 printf("block 0x%08lx successfully "
725 "marked as bad\n",
726 addr);
727 }
728 --argc;
729 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200730 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200731 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200732 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500733
Stefan Roesed351b2b2006-10-10 12:36:02 +0200734 if (strcmp(cmd, "biterr") == 0) {
735 /* todo */
736 return 1;
737 }
738
Nishanth Menonb20f8402008-12-13 09:43:06 -0600739#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200740 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600741 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200742 int status = 0;
743 if (argc == 3) {
744 if (!strcmp("tight", argv[2]))
745 tight = 1;
746 if (!strcmp("status", argv[2]))
747 status = 1;
748 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200749 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500750 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100751 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500752 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100753 puts("NAND flash successfully locked\n");
754 } else {
755 puts("Error locking NAND flash\n");
756 return 1;
757 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200758 }
759 return 0;
760 }
761
Joe Hershbergercccf5952012-08-22 16:49:42 -0500762 if (strncmp(cmd, "unlock", 5) == 0) {
763 int allexcept = 0;
764
765 s = strchr(cmd, '.');
766
767 if (s && !strcmp(s, ".allexcept"))
768 allexcept = 1;
769
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200770 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
771 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500772 mtd->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200773 return 1;
774
775 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200776 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200777
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500778 mtd = get_nand_dev_by_index(dev);
779
780 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100781 puts("NAND flash successfully unlocked\n");
782 } else {
783 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100784 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100785 return 1;
786 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200787 return 0;
788 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600789#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200790
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100791usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000792 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100793}
794
Kim Phillipsdc00a682012-10-29 13:34:31 +0000795#ifdef CONFIG_SYS_LONGHELP
796static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200797 "info - show available NAND devices\n"
798 "nand device [dev] - show or set current device\n"
799 "nand read - addr off|partition size\n"
800 "nand write - addr off|partition size\n"
801 " read/write 'size' bytes starting at offset 'off'\n"
802 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600803 "nand read.raw - addr off|partition [count]\n"
Boris Brezillona7468e22016-06-15 10:42:18 +0200804 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood733fb022012-03-02 14:01:57 -0600805 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400806#ifdef CONFIG_CMD_NAND_TRIMFFS
807 "nand write.trimffs - addr off|partition size\n"
808 " write 'size' bytes starting at offset 'off' from memory address\n"
809 " 'addr', skipping bad blocks and dropping any pages at the end\n"
810 " of eraseblocks that contain only 0xFF\n"
811#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200812 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500813 "from offset 'off'\n"
814 " With '.spread', erase enough for given file size, otherwise,\n"
815 " 'size' includes skipped bad blocks.\n"
816 "nand erase.part [clean] partition - erase entire mtd partition'\n"
817 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200818 "nand bad - show bad blocks\n"
819 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100820#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200821 "nand torture off - torture one block at offset\n"
822 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100823#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200824 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500825 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200826 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
827 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600828#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200829 "\n"
830 "nand lock [tight] [status]\n"
831 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500832 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600833#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400834#ifdef CONFIG_ENV_OFFSET_OOB
835 "\n"
836 "nand env.oob - environment offset in OOB of block 0 of"
837 " first device.\n"
838 "nand env.oob set off|partition - set enviromnent offset\n"
839 "nand env.oob get - get environment offset"
840#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000841 "";
842#endif
843
844U_BOOT_CMD(
845 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
846 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600847);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100848
Simon Glassed38aef2020-05-10 11:40:03 -0600849static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200850 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100851{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100852 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000853 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200854 size_t cnt;
Tom Rinic220bd92019-05-23 07:14:07 -0400855#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100856 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200857#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100858#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200859 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100860#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200861
862 s = strchr(cmd, '.');
863 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500864 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500865 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000866 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500867 return 1;
868 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100869
Scott Wood08364d92016-05-30 13:57:54 -0500870 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100871
Scott Wood08364d92016-05-30 13:57:54 -0500872 cnt = mtd->writesize;
873 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
874 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100875 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200876 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000877 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100878 return 1;
879 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000880 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100881
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100882 switch (genimg_get_format ((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400883#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100884 case IMAGE_FORMAT_LEGACY:
885 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100886
Simon Glass0169e6b2012-02-13 13:51:18 +0000887 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100888 image_print_contents (hdr);
889
890 cnt = image_get_image_size (hdr);
891 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200892#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100893#if defined(CONFIG_FIT)
894 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100895 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100896 puts ("Fit image detected...\n");
897
898 cnt = fit_get_size (fit_hdr);
899 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100900#endif
901 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000902 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100903 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100904 return 1;
905 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000906 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100907
Scott Wood08364d92016-05-30 13:57:54 -0500908 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
909 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100910 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200911 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000912 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100913 return 1;
914 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000915 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100916
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100917#if defined(CONFIG_FIT)
918 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200919 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
920 if (!fit_check_format (fit_hdr)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000921 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200922 puts ("** Bad FIT image format\n");
923 return 1;
924 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000925 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200926 fit_print_contents (fit_hdr);
927 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100928#endif
929
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100930 /* Loading ok, update default load address */
931
Simon Glass892265d2019-12-28 10:45:02 -0700932 image_load_addr = addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100933
Mike Frysinger194c2e82011-06-05 13:43:02 +0000934 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100935}
936
Simon Glassed38aef2020-05-10 11:40:03 -0600937static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
938 char *const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +0200939{
940 char *boot_device = NULL;
941 int idx;
942 ulong addr, offset = 0;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500943 struct mtd_info *mtd;
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200944#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200945 struct mtd_device *dev;
946 struct part_info *part;
947 u8 pnum;
948
949 if (argc >= 2) {
950 char *p = (argc == 2) ? argv[1] : argv[2];
951 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
952 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
953 if (dev->id->type != MTD_DEV_TYPE_NAND) {
954 puts("Not a NAND device\n");
955 return 1;
956 }
957 if (argc > 3)
958 goto usage;
959 if (argc == 3)
Thomas Knobloche84b7772007-07-06 14:58:39 +0200960 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200961 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200962 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500963
964 mtd = get_nand_dev_by_index(dev->id->num);
965 return nand_load_image(cmdtp, mtd, part->offset,
966 addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200967 }
968 }
969#endif
970
Simon Glass0169e6b2012-02-13 13:51:18 +0000971 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +0200972 switch (argc) {
973 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200974 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass64b723f2017-08-03 12:22:12 -0600975 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200976 break;
977 case 2:
978 addr = simple_strtoul(argv[1], NULL, 16);
Simon Glass64b723f2017-08-03 12:22:12 -0600979 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200980 break;
981 case 3:
982 addr = simple_strtoul(argv[1], NULL, 16);
983 boot_device = argv[2];
984 break;
985 case 4:
986 addr = simple_strtoul(argv[1], NULL, 16);
987 boot_device = argv[2];
988 offset = simple_strtoul(argv[3], NULL, 16);
989 break;
990 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200991#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200992usage:
993#endif
Simon Glass0169e6b2012-02-13 13:51:18 +0000994 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +0000995 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +0200996 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000997 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +0200998
999 if (!boot_device) {
1000 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +00001001 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001002 return 1;
1003 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001004 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001005
Stefan Roese198b23e2006-10-28 15:55:52 +02001006 idx = simple_strtoul(boot_device, NULL, 16);
1007
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001008 mtd = get_nand_dev_by_index(idx);
1009 if (!mtd) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001010 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +00001011 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001012 return 1;
1013 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001014 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001015
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001016 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001017}
1018
1019U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001020 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001021 "[partition] | [[[loadAddr] dev] offset]"
1022);