blob: 27efef20bc95c0962a62c473521b9cb861fd2166 [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
Ladislav Michl9d0928d2009-04-21 02:26:31 +020033#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020034
Wolfgang Denk161bfc52009-11-17 21:27:39 +010035/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020036int mtdparts_init(void);
37int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
38int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020039 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020040#endif
41
Scott Wood08364d92016-05-30 13:57:54 -050042static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
43 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010044{
45 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010046 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -050047 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +090048 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -050049
50 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -050051 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -050052
53 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010054
Scott Wood08364d92016-05-30 13:57:54 -050055 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090056 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010057 puts("No memory for page buffer\n");
58 return 1;
59 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +090060
Scott Wood08364d92016-05-30 13:57:54 -050061 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090062 if (!oobbuf) {
63 puts("No memory for page buffer\n");
64 ret = 1;
65 goto free_dat;
66 }
Scott Wood08364d92016-05-30 13:57:54 -050067 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010068 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +010069 struct mtd_oob_ops ops;
70 memset(&ops, 0, sizeof(ops));
71 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -070072 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -050073 ops.len = mtd->writesize;
74 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +000075 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -050076 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010077 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020078 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090079 ret = 1;
80 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010081 }
Stefan Roese3167d072008-07-10 10:10:54 +020082 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020083
Masahiro Yamada3f97e272013-07-11 17:27:13 +090084 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -050085 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +090086 p = datbuf;
87
88 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +010089 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
90 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
91 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Woodcc5f3392008-06-12 13:20:16 -050092 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
93 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +090094 p += 16;
95 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010096 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +090097
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010098 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -050099 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -0700100 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100101 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100102 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
103 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100104 p += 8;
105 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900106
107free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100108 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900109free_dat:
110 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100111
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900112 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100113}
114
115/* ------------------------------------------------------------------------- */
116
Scott Woode5836dc2010-08-02 13:04:24 -0500117static int set_dev(int dev)
118{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500119 struct mtd_info *mtd = get_nand_dev_by_index(dev);
120
121 if (!mtd)
122 return -ENODEV;
Scott Woode5836dc2010-08-02 13:04:24 -0500123
124 if (nand_curr_device == dev)
125 return 0;
126
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500127 printf("Device %d: %s", dev, mtd->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500128 puts("... is now current device\n");
129 nand_curr_device = dev;
130
131#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashkocd3ffa12017-06-26 19:12:58 -0500132 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500133#endif
134
135 return 0;
136}
137
Nishanth Menonb20f8402008-12-13 09:43:06 -0600138#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
139static void print_status(ulong start, ulong end, ulong erasesize, int status)
140{
Joe Hershberger53768562012-08-22 16:49:45 -0500141 /*
142 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
143 * not the same as others. Instead of bit 1 being lock, it is
144 * #lock_tight. To make the driver support either format, ignore bit 1
145 * and use only bit 0 and bit 2.
146 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600147 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
148 start,
149 end - 1,
150 (end - start) / erasesize,
151 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500152 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600153 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
154}
155
Scott Wood08364d92016-05-30 13:57:54 -0500156static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600157{
158 ulong block_start = 0;
159 ulong off;
160 int last_status = -1;
161
Scott Wood17fed142016-05-30 13:57:56 -0500162 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600163 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500164 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600165 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500166 (nand_chip->read_byte(mtd) & 0x80 ?
167 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600168
Scott Wood08364d92016-05-30 13:57:54 -0500169 for (off = 0; off < mtd->size; off += mtd->erasesize) {
170 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600171
172 /* print message only if status has changed */
173 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500174 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600175 last_status);
176 block_start = off;
177 }
178 last_status = s;
179 }
180 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500181 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600182}
183#endif
184
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400185#ifdef CONFIG_ENV_OFFSET_OOB
186unsigned long nand_env_oob_offset;
187
Scott Woode5836dc2010-08-02 13:04:24 -0500188int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400189{
190 int ret;
191 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500192 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400193 char *cmd = argv[1];
194
Scott Wood84f41482016-09-01 17:31:31 -0500195 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woode5836dc2010-08-02 13:04:24 -0500196 puts("no devices available\n");
197 return 1;
198 }
199
200 set_dev(0);
201
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400202 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500203 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500204 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400205 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500206
207 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400208 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500209 loff_t addr;
210 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400211 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500212 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400213
214 if (argc < 3)
215 goto usage;
216
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500217 mtd = get_nand_dev_by_index(idx);
Tom Rini32d96182013-03-14 05:32:50 +0000218 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200219 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500220 MTD_DEV_TYPE_NAND, mtd->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500221 puts("Offset or partition name expected\n");
222 return 1;
223 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200224 if (set_dev(idx)) {
225 puts("Offset or partition name expected\n");
226 return 1;
227 }
Scott Woode5836dc2010-08-02 13:04:24 -0500228
229 if (idx != 0) {
230 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400231 return 1;
232 }
233
Scott Wood08364d92016-05-30 13:57:54 -0500234 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500235 printf("Insufficient available OOB bytes:\n"
236 "%d OOB bytes available but %d required for "
237 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500238 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400239 return 1;
240 }
241
Scott Wood08364d92016-05-30 13:57:54 -0500242 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400243 printf("Environment offset must be block-aligned\n");
244 return 1;
245 }
246
247 ops.datbuf = NULL;
248 ops.mode = MTD_OOB_AUTO;
249 ops.ooboffs = 0;
250 ops.ooblen = ENV_OFFSET_SIZE;
251 ops.oobbuf = (void *) oob_buf;
252
253 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood08364d92016-05-30 13:57:54 -0500254 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400255
Scott Wood08364d92016-05-30 13:57:54 -0500256 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500257 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400258 printf("Error writing OOB block 0\n");
259 return ret;
260 }
Scott Wood0268da02010-07-12 18:17:40 -0500261
Scott Wood08364d92016-05-30 13:57:54 -0500262 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500263 if (ret) {
264 printf("Error reading env offset in OOB\n");
265 return ret;
266 }
267
268 if (addr != nand_env_oob_offset) {
269 printf("Verification of env offset in OOB failed: "
Scott Woode5836dc2010-08-02 13:04:24 -0500270 "0x%08llx expected but got 0x%08lx\n",
271 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500272 return 1;
273 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400274 } else {
275 goto usage;
276 }
277
278 return ret;
279
280usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000281 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400282}
283
284#endif
285
Marek Vasut185ce252011-09-22 03:57:26 +0200286static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100287{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500288 struct mtd_info *mtd;
289 struct nand_chip *chip;
290
291 mtd = get_nand_dev_by_index(idx);
292 if (!mtd)
293 return;
Marek Vasut185ce252011-09-22 03:57:26 +0200294
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500295 chip = mtd_to_nand(mtd);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100296 printf("Device %d: ", idx);
297 if (chip->numchips > 1)
298 printf("%dx ", chip->numchips);
299 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500300 mtd->name, mtd->erasesize >> 10);
301 printf(" Page size %8d b\n", mtd->writesize);
302 printf(" OOB size %8d b\n", mtd->oobsize);
303 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocherfb9a9bd2015-04-12 10:18:09 +0200304 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmannd8ea9812017-07-10 08:44:24 +0200305 printf(" options 0x%08x\n", chip->options);
306 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasut185ce252011-09-22 03:57:26 +0200307
308 /* Set geometry info */
Simon Glass4d949a22017-08-03 12:22:10 -0600309 env_set_hex("nand_writesize", mtd->writesize);
310 env_set_hex("nand_oobsize", mtd->oobsize);
311 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100312}
313
Scott Wood08364d92016-05-30 13:57:54 -0500314static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillona7468e22016-06-15 10:42:18 +0200315 ulong count, int read, int no_verify)
Scott Wood733fb022012-03-02 14:01:57 -0600316{
317 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600318
319 while (count--) {
320 /* Raw access */
321 mtd_oob_ops_t ops = {
322 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500323 .oobbuf = ((u8 *)addr) + mtd->writesize,
324 .len = mtd->writesize,
325 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000326 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600327 };
328
Peter Tyser5fa8b982015-02-03 11:58:13 -0600329 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500330 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600331 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500332 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillona7468e22016-06-15 10:42:18 +0200333 if (!ret && !no_verify)
Scott Wood08364d92016-05-30 13:57:54 -0500334 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600335 }
Scott Wood733fb022012-03-02 14:01:57 -0600336
337 if (ret) {
338 printf("%s: error at offset %llx, ret %d\n",
339 __func__, (long long)off, ret);
340 break;
341 }
342
Scott Wood08364d92016-05-30 13:57:54 -0500343 addr += mtd->writesize + mtd->oobsize;
344 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600345 }
346
347 return ret;
348}
349
Harvey Chapman39e68862013-02-26 17:57:14 +0000350/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500351 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000352 */
353static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
354{
355 /* We grab the nand info object here fresh because this is usually
356 * called after arg_off_size() which can change the value of dev.
357 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500358 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapman39e68862013-02-26 17:57:14 +0000359 loff_t maxoffset = offset + *size;
360 int badblocks = 0;
361
362 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500363 for (; offset < maxoffset; offset += mtd->erasesize) {
364 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000365 badblocks++;
366 }
367 /* adjust size if any bad blocks found */
368 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500369 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000370 printf("size adjusted to 0x%llx (%d bad blocks)\n",
371 (unsigned long long)*size, badblocks);
372 }
373}
374
Kim Phillipsdc00a682012-10-29 13:34:31 +0000375static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100376{
Scott Woode5836dc2010-08-02 13:04:24 -0500377 int i, ret = 0;
378 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000379 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100380 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500381 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200382#ifdef CONFIG_SYS_NAND_QUIET
383 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200384#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200385 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200386#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600387 const char *quiet_str = env_get("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500388 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500389 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100390
391 /* at least two arguments please */
392 if (argc < 2)
393 goto usage;
394
Stefan Roesed351b2b2006-10-10 12:36:02 +0200395 if (quiet_str)
396 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
397
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100398 cmd = argv[1];
399
Scott Wood67d5a2a2010-08-25 15:24:01 -0500400 /* Only "dump" is repeatable. */
401 if (repeat && strcmp(cmd, "dump"))
402 return 0;
403
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100404 if (strcmp(cmd, "info") == 0) {
405
406 putc('\n');
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500407 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
408 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100409 return 0;
410 }
411
412 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100413 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100414 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500415 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100416 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100417 else
Marek Vasut185ce252011-09-22 03:57:26 +0200418 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100419 return 0;
420 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200421
Scott Woode5836dc2010-08-02 13:04:24 -0500422 dev = (int)simple_strtoul(argv[2], NULL, 10);
423 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200424
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100425 return 0;
426 }
427
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400428#ifdef CONFIG_ENV_OFFSET_OOB
429 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500430 if (strcmp(cmd, "env.oob") == 0)
431 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400432#endif
433
Scott Woode5836dc2010-08-02 13:04:24 -0500434 /* The following commands operate on the current device, unless
435 * overridden by a partition specifier. Note that if somehow the
436 * current device is invalid, it will have to be changed to a valid
437 * one before these commands can run, even if a partition specifier
438 * for another device is to be used.
439 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500440 mtd = get_nand_dev_by_index(dev);
441 if (!mtd) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100442 puts("\nno devices available\n");
443 return 1;
444 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100445
446 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500447 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500448 for (off = 0; off < mtd->size; off += mtd->erasesize)
449 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500450 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100451 return 0;
452 }
453
Stefan Roese198b23e2006-10-28 15:55:52 +0200454 /*
455 * Syntax is:
456 * 0 1 2 3 4
457 * nand erase [clean] [off size]
458 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500459 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200460 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200461 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100462 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200463 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
464 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500465 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500466 int spread = 0;
467 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200468 const char *scrub_warn =
469 "Warning: "
470 "scrub option will erase all factory set bad blocks!\n"
471 " "
472 "There is no reliable way to recover them.\n"
473 " "
474 "Use this command only for testing purposes if you\n"
475 " "
476 "are sure of what you are doing!\n"
477 "\nReally scrub this NAND flash? <y/N>\n";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200478
Scott Wood1b5cd512010-08-25 14:43:29 -0500479 if (cmd[5] != 0) {
480 if (!strcmp(&cmd[5], ".spread")) {
481 spread = 1;
482 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500483 args = 1;
484 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500485 args = 0;
486 } else {
487 goto usage;
488 }
489 }
490
491 /*
492 * Don't allow missing arguments to cause full chip/partition
493 * erases -- easy to do accidentally, e.g. with a misspelled
494 * variable name.
495 */
496 if (argc != o + args)
497 goto usage;
498
499 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200500 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200501 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
502 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500503 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200504 return 1;
505
506 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200507 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200508
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500509 mtd = get_nand_dev_by_index(dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500510
Stefan Roesed351b2b2006-10-10 12:36:02 +0200511 memset(&opts, 0, sizeof(opts));
512 opts.offset = off;
513 opts.length = size;
514 opts.jffs2 = clean;
515 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500516 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200517
518 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200519 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200520 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200521 } else {
522 puts(scrub_warn);
523 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100524 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200525 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100526 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900527 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100528 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200529 }
530 }
Scott Wood08364d92016-05-30 13:57:54 -0500531 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100532 printf("%s\n", ret ? "ERROR" : "OK");
533
534 return ret == 0 ? 0 : 1;
535 }
536
537 if (strncmp(cmd, "dump", 4) == 0) {
538 if (argc < 3)
539 goto usage;
540
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100541 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood08364d92016-05-30 13:57:54 -0500542 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100543
544 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100545 }
546
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100547 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500548 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600549 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200550 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000551 int raw = 0;
Boris Brezillona7468e22016-06-15 10:42:18 +0200552 int no_verify = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200553
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100554 if (argc < 4)
555 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200556
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100557 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
558
Stefan Roesed351b2b2006-10-10 12:36:02 +0200559 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200560 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100561
Stefan Roesed351b2b2006-10-10 12:36:02 +0200562 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600563
Boris Brezillona7468e22016-06-15 10:42:18 +0200564 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood733fb022012-03-02 14:01:57 -0600565 raw = 1;
566
Boris Brezillona7468e22016-06-15 10:42:18 +0200567 if (!strcmp(s, ".raw.noverify"))
568 no_verify = 1;
569
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200570 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
571 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500572 mtd->size))
Scott Wood733fb022012-03-02 14:01:57 -0600573 return 1;
574
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200575 if (set_dev(dev))
576 return 1;
577
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500578 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200579
Scott Wood733fb022012-03-02 14:01:57 -0600580 if (argc > 4 && !str2long(argv[4], &pagecount)) {
581 printf("'%s' is not a number\n", argv[4]);
582 return 1;
583 }
584
Scott Wood08364d92016-05-30 13:57:54 -0500585 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600586 puts("Size exceeds partition or device limit\n");
587 return -1;
588 }
589
Scott Wood08364d92016-05-30 13:57:54 -0500590 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600591 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200592 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
593 &size, &maxsize,
594 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500595 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200596 return 1;
597
598 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600599 return 1;
600
Harvey Chapman39e68862013-02-26 17:57:14 +0000601 /* size is unspecified */
602 if (argc < 5)
603 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600604 rwsize = size;
605 }
606
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500607 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200608
Scott Wood12b1c952008-06-12 13:13:23 -0500609 if (!s || !strcmp(s, ".jffs2") ||
610 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500611 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500612 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000613 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200614 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500615 else
Scott Wood08364d92016-05-30 13:57:54 -0500616 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000617 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600618 (u_char *)addr,
619 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400620#ifdef CONFIG_CMD_NAND_TRIMFFS
621 } else if (!strcmp(s, ".trimffs")) {
622 if (read) {
623 printf("Unknown nand command suffix '%s'\n", s);
624 return 1;
625 }
Scott Wood08364d92016-05-30 13:57:54 -0500626 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000627 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600628 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400629#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400630 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100631 /* out-of-band data */
632 mtd_oob_ops_t ops = {
633 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500634 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000635 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100636 };
637
Harald Welte5a395102007-12-19 14:12:53 +0100638 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500639 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100640 else
Scott Wood08364d92016-05-30 13:57:54 -0500641 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600642 } else if (raw) {
Boris Brezillona7468e22016-06-15 10:42:18 +0200643 ret = raw_access(mtd, addr, off, pagecount, read,
644 no_verify);
Stefan Roese198b23e2006-10-28 15:55:52 +0200645 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500646 printf("Unknown nand command suffix '%s'.\n", s);
647 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200648 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100649
Scott Woode5836dc2010-08-02 13:04:24 -0500650 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200651 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100652
653 return ret == 0 ? 0 : 1;
654 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200655
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100656#ifdef CONFIG_CMD_NAND_TORTURE
657 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200658 loff_t endoff;
659 unsigned int failed = 0, passed = 0;
660
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100661 if (argc < 3)
662 goto usage;
663
664 if (!str2off(argv[2], &off)) {
665 puts("Offset is not a valid number\n");
666 return 1;
667 }
668
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200669 size = mtd->erasesize;
670 if (argc > 3) {
671 if (!str2off(argv[3], &size)) {
672 puts("Size is not a valid number\n");
673 return 1;
674 }
675 }
676
677 endoff = off + size;
678 if (endoff > mtd->size) {
679 puts("Arguments beyond end of NAND\n");
680 return 1;
681 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100682
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200683 off = round_down(off, mtd->erasesize);
684 endoff = round_up(endoff, mtd->erasesize);
685 size = endoff - off;
686 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
687 dev, off, size, mtd->erasesize);
688 while (off < endoff) {
689 ret = nand_torture(mtd, off);
690 if (ret) {
691 failed++;
692 printf(" block at 0x%llx failed\n", off);
693 } else {
694 passed++;
695 }
696 off += mtd->erasesize;
697 }
698 printf(" Passed: %u, failed: %u\n", passed, failed);
699 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100700 }
701#endif
702
Stefan Roesed351b2b2006-10-10 12:36:02 +0200703 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200704 argc -= 2;
705 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200706
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200707 if (argc <= 0)
708 goto usage;
709
710 while (argc > 0) {
711 addr = simple_strtoul(*argv, NULL, 16);
712
Scott Wood08364d92016-05-30 13:57:54 -0500713 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200714 printf("block 0x%08lx NOT marked "
715 "as bad! ERROR %d\n",
716 addr, ret);
717 ret = 1;
718 } else {
719 printf("block 0x%08lx successfully "
720 "marked as bad\n",
721 addr);
722 }
723 --argc;
724 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200725 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200726 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200727 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500728
Stefan Roesed351b2b2006-10-10 12:36:02 +0200729 if (strcmp(cmd, "biterr") == 0) {
730 /* todo */
731 return 1;
732 }
733
Nishanth Menonb20f8402008-12-13 09:43:06 -0600734#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200735 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600736 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200737 int status = 0;
738 if (argc == 3) {
739 if (!strcmp("tight", argv[2]))
740 tight = 1;
741 if (!strcmp("status", argv[2]))
742 status = 1;
743 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200744 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500745 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100746 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500747 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100748 puts("NAND flash successfully locked\n");
749 } else {
750 puts("Error locking NAND flash\n");
751 return 1;
752 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200753 }
754 return 0;
755 }
756
Joe Hershbergercccf5952012-08-22 16:49:42 -0500757 if (strncmp(cmd, "unlock", 5) == 0) {
758 int allexcept = 0;
759
760 s = strchr(cmd, '.');
761
762 if (s && !strcmp(s, ".allexcept"))
763 allexcept = 1;
764
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200765 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
766 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500767 mtd->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200768 return 1;
769
770 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200771 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200772
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500773 mtd = get_nand_dev_by_index(dev);
774
775 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100776 puts("NAND flash successfully unlocked\n");
777 } else {
778 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100779 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100780 return 1;
781 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200782 return 0;
783 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600784#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200785
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100786usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000787 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100788}
789
Kim Phillipsdc00a682012-10-29 13:34:31 +0000790#ifdef CONFIG_SYS_LONGHELP
791static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200792 "info - show available NAND devices\n"
793 "nand device [dev] - show or set current device\n"
794 "nand read - addr off|partition size\n"
795 "nand write - addr off|partition size\n"
796 " read/write 'size' bytes starting at offset 'off'\n"
797 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600798 "nand read.raw - addr off|partition [count]\n"
Boris Brezillona7468e22016-06-15 10:42:18 +0200799 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood733fb022012-03-02 14:01:57 -0600800 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400801#ifdef CONFIG_CMD_NAND_TRIMFFS
802 "nand write.trimffs - addr off|partition size\n"
803 " write 'size' bytes starting at offset 'off' from memory address\n"
804 " 'addr', skipping bad blocks and dropping any pages at the end\n"
805 " of eraseblocks that contain only 0xFF\n"
806#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200807 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500808 "from offset 'off'\n"
809 " With '.spread', erase enough for given file size, otherwise,\n"
810 " 'size' includes skipped bad blocks.\n"
811 "nand erase.part [clean] partition - erase entire mtd partition'\n"
812 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200813 "nand bad - show bad blocks\n"
814 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100815#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200816 "nand torture off - torture one block at offset\n"
817 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100818#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200819 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500820 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200821 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
822 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600823#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200824 "\n"
825 "nand lock [tight] [status]\n"
826 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500827 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600828#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400829#ifdef CONFIG_ENV_OFFSET_OOB
830 "\n"
831 "nand env.oob - environment offset in OOB of block 0 of"
832 " first device.\n"
833 "nand env.oob set off|partition - set enviromnent offset\n"
834 "nand env.oob get - get environment offset"
835#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000836 "";
837#endif
838
839U_BOOT_CMD(
840 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
841 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600842);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100843
Scott Wood08364d92016-05-30 13:57:54 -0500844static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200845 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100846{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100847 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000848 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200849 size_t cnt;
Tom Rinic220bd92019-05-23 07:14:07 -0400850#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100851 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200852#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100853#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200854 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100855#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200856
857 s = strchr(cmd, '.');
858 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500859 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500860 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000861 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500862 return 1;
863 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100864
Scott Wood08364d92016-05-30 13:57:54 -0500865 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100866
Scott Wood08364d92016-05-30 13:57:54 -0500867 cnt = mtd->writesize;
868 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
869 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100870 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200871 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000872 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100873 return 1;
874 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000875 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100876
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100877 switch (genimg_get_format ((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400878#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100879 case IMAGE_FORMAT_LEGACY:
880 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100881
Simon Glass0169e6b2012-02-13 13:51:18 +0000882 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100883 image_print_contents (hdr);
884
885 cnt = image_get_image_size (hdr);
886 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200887#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100888#if defined(CONFIG_FIT)
889 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100890 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100891 puts ("Fit image detected...\n");
892
893 cnt = fit_get_size (fit_hdr);
894 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100895#endif
896 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000897 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100898 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100899 return 1;
900 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000901 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100902
Scott Wood08364d92016-05-30 13:57:54 -0500903 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
904 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100905 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200906 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000907 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100908 return 1;
909 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000910 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100911
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100912#if defined(CONFIG_FIT)
913 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200914 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
915 if (!fit_check_format (fit_hdr)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000916 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200917 puts ("** Bad FIT image format\n");
918 return 1;
919 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000920 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200921 fit_print_contents (fit_hdr);
922 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100923#endif
924
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100925 /* Loading ok, update default load address */
926
927 load_addr = addr;
928
Mike Frysinger194c2e82011-06-05 13:43:02 +0000929 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100930}
931
Kim Phillipsdc00a682012-10-29 13:34:31 +0000932static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
933 char * const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +0200934{
935 char *boot_device = NULL;
936 int idx;
937 ulong addr, offset = 0;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500938 struct mtd_info *mtd;
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200939#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200940 struct mtd_device *dev;
941 struct part_info *part;
942 u8 pnum;
943
944 if (argc >= 2) {
945 char *p = (argc == 2) ? argv[1] : argv[2];
946 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
947 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
948 if (dev->id->type != MTD_DEV_TYPE_NAND) {
949 puts("Not a NAND device\n");
950 return 1;
951 }
952 if (argc > 3)
953 goto usage;
954 if (argc == 3)
Thomas Knobloche84b7772007-07-06 14:58:39 +0200955 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200956 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200957 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500958
959 mtd = get_nand_dev_by_index(dev->id->num);
960 return nand_load_image(cmdtp, mtd, part->offset,
961 addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200962 }
963 }
964#endif
965
Simon Glass0169e6b2012-02-13 13:51:18 +0000966 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +0200967 switch (argc) {
968 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200969 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass64b723f2017-08-03 12:22:12 -0600970 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200971 break;
972 case 2:
973 addr = simple_strtoul(argv[1], NULL, 16);
Simon Glass64b723f2017-08-03 12:22:12 -0600974 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200975 break;
976 case 3:
977 addr = simple_strtoul(argv[1], NULL, 16);
978 boot_device = argv[2];
979 break;
980 case 4:
981 addr = simple_strtoul(argv[1], NULL, 16);
982 boot_device = argv[2];
983 offset = simple_strtoul(argv[3], NULL, 16);
984 break;
985 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200986#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200987usage:
988#endif
Simon Glass0169e6b2012-02-13 13:51:18 +0000989 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +0000990 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +0200991 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000992 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +0200993
994 if (!boot_device) {
995 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000996 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200997 return 1;
998 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000999 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001000
Stefan Roese198b23e2006-10-28 15:55:52 +02001001 idx = simple_strtoul(boot_device, NULL, 16);
1002
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001003 mtd = get_nand_dev_by_index(idx);
1004 if (!mtd) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001005 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +00001006 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001007 return 1;
1008 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001009 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001010
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001011 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001012}
1013
1014U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001015 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001016 "[partition] | [[[loadAddr] dev] offset]"
1017);