blob: df5a4b1db1a037cd40698840e234bc1434666857 [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>
Tom Rini3bde7e22021-09-22 14:50:35 -040027#include <linux/mtd/rawnand.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010028#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070029#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060030#include <env.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010031#include <watchdog.h>
32#include <malloc.h>
33#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010034#include <jffs2/jffs2.h>
35#include <nand.h>
36
Miquel Raynal617fd792019-10-25 19:39:29 +020037#include "legacy-mtd-utils.h"
38
Ladislav Michl9d0928d2009-04-21 02:26:31 +020039#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020040
Wolfgang Denk161bfc52009-11-17 21:27:39 +010041/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020042int mtdparts_init(void);
Stefan Roese198b23e2006-10-28 15:55:52 +020043int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020044 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020045#endif
46
Scott Wood08364d92016-05-30 13:57:54 -050047static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
48 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010049{
50 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010051 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -050052 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +090053 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -050054
55 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -050056 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -050057
58 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010059
Scott Wood08364d92016-05-30 13:57:54 -050060 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090061 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010062 puts("No memory for page buffer\n");
63 return 1;
64 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +090065
Scott Wood08364d92016-05-30 13:57:54 -050066 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090067 if (!oobbuf) {
68 puts("No memory for page buffer\n");
69 ret = 1;
70 goto free_dat;
71 }
Scott Wood08364d92016-05-30 13:57:54 -050072 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010073 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +010074 struct mtd_oob_ops ops;
75 memset(&ops, 0, sizeof(ops));
76 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -070077 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -050078 ops.len = mtd->writesize;
79 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +000080 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -050081 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010082 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020083 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090084 ret = 1;
85 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010086 }
Stefan Roese3167d072008-07-10 10:10:54 +020087 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020088
Masahiro Yamada3f97e272013-07-11 17:27:13 +090089 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -050090 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +090091 p = datbuf;
92
93 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +010094 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
95 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
96 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Woodcc5f3392008-06-12 13:20:16 -050097 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
98 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +090099 p += 16;
100 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100101 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +0900102
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100103 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -0500104 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -0700105 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100106 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100107 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
108 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100109 p += 8;
110 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900111
112free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100113 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900114free_dat:
115 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100116
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900117 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100118}
119
120/* ------------------------------------------------------------------------- */
121
Scott Woode5836dc2010-08-02 13:04:24 -0500122static int set_dev(int dev)
123{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500124 struct mtd_info *mtd = get_nand_dev_by_index(dev);
125
126 if (!mtd)
127 return -ENODEV;
Scott Woode5836dc2010-08-02 13:04:24 -0500128
129 if (nand_curr_device == dev)
130 return 0;
131
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500132 printf("Device %d: %s", dev, mtd->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500133 puts("... is now current device\n");
134 nand_curr_device = dev;
135
136#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashkocd3ffa12017-06-26 19:12:58 -0500137 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500138#endif
139
140 return 0;
141}
142
Nishanth Menonb20f8402008-12-13 09:43:06 -0600143#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
144static void print_status(ulong start, ulong end, ulong erasesize, int status)
145{
Joe Hershberger53768562012-08-22 16:49:45 -0500146 /*
147 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
148 * not the same as others. Instead of bit 1 being lock, it is
149 * #lock_tight. To make the driver support either format, ignore bit 1
150 * and use only bit 0 and bit 2.
151 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600152 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
153 start,
154 end - 1,
155 (end - start) / erasesize,
156 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500157 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600158 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
159}
160
Scott Wood08364d92016-05-30 13:57:54 -0500161static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600162{
163 ulong block_start = 0;
164 ulong off;
165 int last_status = -1;
166
Scott Wood17fed142016-05-30 13:57:56 -0500167 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600168 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500169 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600170 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500171 (nand_chip->read_byte(mtd) & 0x80 ?
172 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600173
Scott Wood08364d92016-05-30 13:57:54 -0500174 for (off = 0; off < mtd->size; off += mtd->erasesize) {
175 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600176
177 /* print message only if status has changed */
178 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500179 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600180 last_status);
181 block_start = off;
182 }
183 last_status = s;
184 }
185 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500186 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600187}
188#endif
189
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400190#ifdef CONFIG_ENV_OFFSET_OOB
191unsigned long nand_env_oob_offset;
192
Simon Glassed38aef2020-05-10 11:40:03 -0600193int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400194{
195 int ret;
196 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500197 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400198 char *cmd = argv[1];
199
Scott Wood84f41482016-09-01 17:31:31 -0500200 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woode5836dc2010-08-02 13:04:24 -0500201 puts("no devices available\n");
202 return 1;
203 }
204
205 set_dev(0);
206
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400207 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500208 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500209 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400210 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500211
212 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400213 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500214 loff_t addr;
215 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400216 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500217 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400218
219 if (argc < 3)
220 goto usage;
221
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500222 mtd = get_nand_dev_by_index(idx);
Tom Rini32d96182013-03-14 05:32:50 +0000223 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200224 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500225 MTD_DEV_TYPE_NAND, mtd->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500226 puts("Offset or partition name expected\n");
227 return 1;
228 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200229 if (set_dev(idx)) {
230 puts("Offset or partition name expected\n");
231 return 1;
232 }
Scott Woode5836dc2010-08-02 13:04:24 -0500233
234 if (idx != 0) {
235 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400236 return 1;
237 }
238
Scott Wood08364d92016-05-30 13:57:54 -0500239 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500240 printf("Insufficient available OOB bytes:\n"
241 "%d OOB bytes available but %d required for "
242 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500243 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400244 return 1;
245 }
246
Scott Wood08364d92016-05-30 13:57:54 -0500247 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400248 printf("Environment offset must be block-aligned\n");
249 return 1;
250 }
251
252 ops.datbuf = NULL;
253 ops.mode = MTD_OOB_AUTO;
254 ops.ooboffs = 0;
255 ops.ooblen = ENV_OFFSET_SIZE;
256 ops.oobbuf = (void *) oob_buf;
257
258 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood08364d92016-05-30 13:57:54 -0500259 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400260
Scott Wood08364d92016-05-30 13:57:54 -0500261 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500262 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400263 printf("Error writing OOB block 0\n");
264 return ret;
265 }
Scott Wood0268da02010-07-12 18:17:40 -0500266
Scott Wood08364d92016-05-30 13:57:54 -0500267 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500268 if (ret) {
269 printf("Error reading env offset in OOB\n");
270 return ret;
271 }
272
273 if (addr != nand_env_oob_offset) {
274 printf("Verification of env offset in OOB failed: "
Scott Woode5836dc2010-08-02 13:04:24 -0500275 "0x%08llx expected but got 0x%08lx\n",
276 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500277 return 1;
278 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400279 } else {
280 goto usage;
281 }
282
283 return ret;
284
285usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000286 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400287}
288
289#endif
290
Marek Vasut185ce252011-09-22 03:57:26 +0200291static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100292{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500293 struct mtd_info *mtd;
294 struct nand_chip *chip;
295
296 mtd = get_nand_dev_by_index(idx);
297 if (!mtd)
298 return;
Marek Vasut185ce252011-09-22 03:57:26 +0200299
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500300 chip = mtd_to_nand(mtd);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100301 printf("Device %d: ", idx);
302 if (chip->numchips > 1)
303 printf("%dx ", chip->numchips);
304 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500305 mtd->name, mtd->erasesize >> 10);
306 printf(" Page size %8d b\n", mtd->writesize);
307 printf(" OOB size %8d b\n", mtd->oobsize);
308 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocherfb9a9bd2015-04-12 10:18:09 +0200309 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmannd8ea9812017-07-10 08:44:24 +0200310 printf(" options 0x%08x\n", chip->options);
311 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasut185ce252011-09-22 03:57:26 +0200312
313 /* Set geometry info */
Simon Glass4d949a22017-08-03 12:22:10 -0600314 env_set_hex("nand_writesize", mtd->writesize);
315 env_set_hex("nand_oobsize", mtd->oobsize);
316 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100317}
318
Scott Wood08364d92016-05-30 13:57:54 -0500319static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillona7468e22016-06-15 10:42:18 +0200320 ulong count, int read, int no_verify)
Scott Wood733fb022012-03-02 14:01:57 -0600321{
322 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600323
324 while (count--) {
325 /* Raw access */
326 mtd_oob_ops_t ops = {
327 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500328 .oobbuf = ((u8 *)addr) + mtd->writesize,
329 .len = mtd->writesize,
330 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000331 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600332 };
333
Peter Tyser5fa8b982015-02-03 11:58:13 -0600334 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500335 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600336 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500337 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillona7468e22016-06-15 10:42:18 +0200338 if (!ret && !no_verify)
Scott Wood08364d92016-05-30 13:57:54 -0500339 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600340 }
Scott Wood733fb022012-03-02 14:01:57 -0600341
342 if (ret) {
343 printf("%s: error at offset %llx, ret %d\n",
344 __func__, (long long)off, ret);
345 break;
346 }
347
Scott Wood08364d92016-05-30 13:57:54 -0500348 addr += mtd->writesize + mtd->oobsize;
349 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600350 }
351
352 return ret;
353}
354
Harvey Chapman39e68862013-02-26 17:57:14 +0000355/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500356 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000357 */
358static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
359{
360 /* We grab the nand info object here fresh because this is usually
361 * called after arg_off_size() which can change the value of dev.
362 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500363 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapman39e68862013-02-26 17:57:14 +0000364 loff_t maxoffset = offset + *size;
365 int badblocks = 0;
366
367 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500368 for (; offset < maxoffset; offset += mtd->erasesize) {
369 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000370 badblocks++;
371 }
372 /* adjust size if any bad blocks found */
373 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500374 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000375 printf("size adjusted to 0x%llx (%d bad blocks)\n",
376 (unsigned long long)*size, badblocks);
377 }
378}
379
Simon Glassed38aef2020-05-10 11:40:03 -0600380static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
381 char *const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100382{
Scott Woode5836dc2010-08-02 13:04:24 -0500383 int i, ret = 0;
384 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000385 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100386 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500387 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200388#ifdef CONFIG_SYS_NAND_QUIET
389 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200390#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200391 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200392#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600393 const char *quiet_str = env_get("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500394 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500395 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100396
397 /* at least two arguments please */
398 if (argc < 2)
399 goto usage;
400
Stefan Roesed351b2b2006-10-10 12:36:02 +0200401 if (quiet_str)
402 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
403
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100404 cmd = argv[1];
405
Scott Wood67d5a2a2010-08-25 15:24:01 -0500406 /* Only "dump" is repeatable. */
407 if (repeat && strcmp(cmd, "dump"))
408 return 0;
409
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100410 if (strcmp(cmd, "info") == 0) {
411
412 putc('\n');
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500413 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
414 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100415 return 0;
416 }
417
418 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100419 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100420 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500421 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100422 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100423 else
Marek Vasut185ce252011-09-22 03:57:26 +0200424 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100425 return 0;
426 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200427
Simon Glassff9b9032021-07-24 09:03:30 -0600428 dev = (int)dectoul(argv[2], NULL);
Scott Woode5836dc2010-08-02 13:04:24 -0500429 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200430
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100431 return 0;
432 }
433
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400434#ifdef CONFIG_ENV_OFFSET_OOB
435 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500436 if (strcmp(cmd, "env.oob") == 0)
437 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400438#endif
439
Scott Woode5836dc2010-08-02 13:04:24 -0500440 /* The following commands operate on the current device, unless
441 * overridden by a partition specifier. Note that if somehow the
442 * current device is invalid, it will have to be changed to a valid
443 * one before these commands can run, even if a partition specifier
444 * for another device is to be used.
445 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500446 mtd = get_nand_dev_by_index(dev);
447 if (!mtd) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100448 puts("\nno devices available\n");
449 return 1;
450 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100451
452 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500453 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500454 for (off = 0; off < mtd->size; off += mtd->erasesize)
455 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500456 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100457 return 0;
458 }
459
Stefan Roese198b23e2006-10-28 15:55:52 +0200460 /*
461 * Syntax is:
462 * 0 1 2 3 4
463 * nand erase [clean] [off size]
464 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500465 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200466 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200467 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100468 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200469 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
470 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500471 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500472 int spread = 0;
473 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200474 const char *scrub_warn =
475 "Warning: "
476 "scrub option will erase all factory set bad blocks!\n"
477 " "
478 "There is no reliable way to recover them.\n"
479 " "
480 "Use this command only for testing purposes if you\n"
481 " "
482 "are sure of what you are doing!\n"
483 "\nReally scrub this NAND flash? <y/N>\n";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200484
Scott Wood1b5cd512010-08-25 14:43:29 -0500485 if (cmd[5] != 0) {
486 if (!strcmp(&cmd[5], ".spread")) {
487 spread = 1;
488 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500489 args = 1;
490 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500491 args = 0;
492 } else {
493 goto usage;
494 }
495 }
496
497 /*
498 * Don't allow missing arguments to cause full chip/partition
499 * erases -- easy to do accidentally, e.g. with a misspelled
500 * variable name.
501 */
502 if (argc != o + args)
503 goto usage;
504
505 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200506 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200507 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
508 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500509 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200510 return 1;
511
512 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200513 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200514
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500515 mtd = get_nand_dev_by_index(dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500516
Stefan Roesed351b2b2006-10-10 12:36:02 +0200517 memset(&opts, 0, sizeof(opts));
518 opts.offset = off;
519 opts.length = size;
520 opts.jffs2 = clean;
521 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500522 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200523
524 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200525 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200526 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200527 } else {
528 puts(scrub_warn);
529 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100530 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200531 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100532 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900533 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100534 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200535 }
536 }
Scott Wood08364d92016-05-30 13:57:54 -0500537 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100538 printf("%s\n", ret ? "ERROR" : "OK");
539
540 return ret == 0 ? 0 : 1;
541 }
542
543 if (strncmp(cmd, "dump", 4) == 0) {
544 if (argc < 3)
545 goto usage;
546
Simon Glass3ff49ec2021-07-24 09:03:29 -0600547 off = (int)hextoul(argv[2], NULL);
Scott Wood08364d92016-05-30 13:57:54 -0500548 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100549
550 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100551 }
552
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100553 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500554 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600555 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200556 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000557 int raw = 0;
Boris Brezillona7468e22016-06-15 10:42:18 +0200558 int no_verify = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200559
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100560 if (argc < 4)
561 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200562
Simon Glass3ff49ec2021-07-24 09:03:29 -0600563 addr = (ulong)hextoul(argv[2], NULL);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100564
Stefan Roesed351b2b2006-10-10 12:36:02 +0200565 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200566 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100567
Stefan Roesed351b2b2006-10-10 12:36:02 +0200568 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600569
Boris Brezillona7468e22016-06-15 10:42:18 +0200570 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood733fb022012-03-02 14:01:57 -0600571 raw = 1;
572
Boris Brezillona7468e22016-06-15 10:42:18 +0200573 if (!strcmp(s, ".raw.noverify"))
574 no_verify = 1;
575
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200576 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
577 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500578 mtd->size))
Scott Wood733fb022012-03-02 14:01:57 -0600579 return 1;
580
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200581 if (set_dev(dev))
582 return 1;
583
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500584 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200585
Scott Wood733fb022012-03-02 14:01:57 -0600586 if (argc > 4 && !str2long(argv[4], &pagecount)) {
587 printf("'%s' is not a number\n", argv[4]);
588 return 1;
589 }
590
Scott Wood08364d92016-05-30 13:57:54 -0500591 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600592 puts("Size exceeds partition or device limit\n");
593 return -1;
594 }
595
Scott Wood08364d92016-05-30 13:57:54 -0500596 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600597 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200598 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
599 &size, &maxsize,
600 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500601 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200602 return 1;
603
604 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600605 return 1;
606
Harvey Chapman39e68862013-02-26 17:57:14 +0000607 /* size is unspecified */
608 if (argc < 5)
609 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600610 rwsize = size;
611 }
612
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500613 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200614
Scott Wood12b1c952008-06-12 13:13:23 -0500615 if (!s || !strcmp(s, ".jffs2") ||
616 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500617 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500618 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000619 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200620 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500621 else
Scott Wood08364d92016-05-30 13:57:54 -0500622 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000623 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600624 (u_char *)addr,
625 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400626#ifdef CONFIG_CMD_NAND_TRIMFFS
627 } else if (!strcmp(s, ".trimffs")) {
628 if (read) {
629 printf("Unknown nand command suffix '%s'\n", s);
630 return 1;
631 }
Scott Wood08364d92016-05-30 13:57:54 -0500632 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000633 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600634 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400635#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400636 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100637 /* out-of-band data */
638 mtd_oob_ops_t ops = {
639 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500640 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000641 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100642 };
643
Harald Welte5a395102007-12-19 14:12:53 +0100644 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500645 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100646 else
Scott Wood08364d92016-05-30 13:57:54 -0500647 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600648 } else if (raw) {
Boris Brezillona7468e22016-06-15 10:42:18 +0200649 ret = raw_access(mtd, addr, off, pagecount, read,
650 no_verify);
Stefan Roese198b23e2006-10-28 15:55:52 +0200651 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500652 printf("Unknown nand command suffix '%s'.\n", s);
653 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200654 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100655
Scott Woode5836dc2010-08-02 13:04:24 -0500656 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200657 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100658
659 return ret == 0 ? 0 : 1;
660 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200661
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100662#ifdef CONFIG_CMD_NAND_TORTURE
663 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200664 loff_t endoff;
665 unsigned int failed = 0, passed = 0;
666
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100667 if (argc < 3)
668 goto usage;
669
670 if (!str2off(argv[2], &off)) {
671 puts("Offset is not a valid number\n");
672 return 1;
673 }
674
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200675 size = mtd->erasesize;
676 if (argc > 3) {
677 if (!str2off(argv[3], &size)) {
678 puts("Size is not a valid number\n");
679 return 1;
680 }
681 }
682
683 endoff = off + size;
684 if (endoff > mtd->size) {
685 puts("Arguments beyond end of NAND\n");
686 return 1;
687 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100688
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200689 off = round_down(off, mtd->erasesize);
690 endoff = round_up(endoff, mtd->erasesize);
691 size = endoff - off;
692 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
693 dev, off, size, mtd->erasesize);
694 while (off < endoff) {
695 ret = nand_torture(mtd, off);
696 if (ret) {
697 failed++;
698 printf(" block at 0x%llx failed\n", off);
699 } else {
700 passed++;
701 }
702 off += mtd->erasesize;
703 }
704 printf(" Passed: %u, failed: %u\n", passed, failed);
705 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100706 }
707#endif
708
Stefan Roesed351b2b2006-10-10 12:36:02 +0200709 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200710 argc -= 2;
711 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200712
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200713 if (argc <= 0)
714 goto usage;
715
716 while (argc > 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600717 addr = hextoul(*argv, NULL);
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200718
Scott Wood08364d92016-05-30 13:57:54 -0500719 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200720 printf("block 0x%08lx NOT marked "
721 "as bad! ERROR %d\n",
722 addr, ret);
723 ret = 1;
724 } else {
725 printf("block 0x%08lx successfully "
726 "marked as bad\n",
727 addr);
728 }
729 --argc;
730 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200731 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200732 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200733 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500734
Stefan Roesed351b2b2006-10-10 12:36:02 +0200735 if (strcmp(cmd, "biterr") == 0) {
736 /* todo */
737 return 1;
738 }
739
Nishanth Menonb20f8402008-12-13 09:43:06 -0600740#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200741 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600742 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200743 int status = 0;
744 if (argc == 3) {
745 if (!strcmp("tight", argv[2]))
746 tight = 1;
747 if (!strcmp("status", argv[2]))
748 status = 1;
749 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200750 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500751 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100752 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500753 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100754 puts("NAND flash successfully locked\n");
755 } else {
756 puts("Error locking NAND flash\n");
757 return 1;
758 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200759 }
760 return 0;
761 }
762
Joe Hershbergercccf5952012-08-22 16:49:42 -0500763 if (strncmp(cmd, "unlock", 5) == 0) {
764 int allexcept = 0;
765
766 s = strchr(cmd, '.');
767
768 if (s && !strcmp(s, ".allexcept"))
769 allexcept = 1;
770
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200771 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
772 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500773 mtd->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200774 return 1;
775
776 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200777 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200778
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500779 mtd = get_nand_dev_by_index(dev);
780
781 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100782 puts("NAND flash successfully unlocked\n");
783 } else {
784 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100785 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100786 return 1;
787 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200788 return 0;
789 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600790#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200791
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100792usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000793 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100794}
795
Kim Phillipsdc00a682012-10-29 13:34:31 +0000796#ifdef CONFIG_SYS_LONGHELP
797static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200798 "info - show available NAND devices\n"
799 "nand device [dev] - show or set current device\n"
800 "nand read - addr off|partition size\n"
801 "nand write - addr off|partition size\n"
802 " read/write 'size' bytes starting at offset 'off'\n"
803 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600804 "nand read.raw - addr off|partition [count]\n"
Boris Brezillona7468e22016-06-15 10:42:18 +0200805 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood733fb022012-03-02 14:01:57 -0600806 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400807#ifdef CONFIG_CMD_NAND_TRIMFFS
808 "nand write.trimffs - addr off|partition size\n"
809 " write 'size' bytes starting at offset 'off' from memory address\n"
810 " 'addr', skipping bad blocks and dropping any pages at the end\n"
811 " of eraseblocks that contain only 0xFF\n"
812#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200813 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500814 "from offset 'off'\n"
815 " With '.spread', erase enough for given file size, otherwise,\n"
816 " 'size' includes skipped bad blocks.\n"
817 "nand erase.part [clean] partition - erase entire mtd partition'\n"
818 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200819 "nand bad - show bad blocks\n"
820 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100821#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200822 "nand torture off - torture one block at offset\n"
823 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100824#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200825 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500826 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200827 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
828 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600829#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200830 "\n"
831 "nand lock [tight] [status]\n"
832 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500833 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600834#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400835#ifdef CONFIG_ENV_OFFSET_OOB
836 "\n"
837 "nand env.oob - environment offset in OOB of block 0 of"
838 " first device.\n"
839 "nand env.oob set off|partition - set enviromnent offset\n"
840 "nand env.oob get - get environment offset"
841#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000842 "";
843#endif
844
845U_BOOT_CMD(
846 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
847 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600848);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100849
Simon Glassed38aef2020-05-10 11:40:03 -0600850static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200851 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100852{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100853 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000854 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200855 size_t cnt;
Tom Rinic220bd92019-05-23 07:14:07 -0400856#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100857 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200858#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100859#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200860 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100861#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200862
863 s = strchr(cmd, '.');
864 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500865 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500866 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000867 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500868 return 1;
869 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100870
Scott Wood08364d92016-05-30 13:57:54 -0500871 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100872
Scott Wood08364d92016-05-30 13:57:54 -0500873 cnt = mtd->writesize;
874 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
875 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100876 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200877 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000878 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100879 return 1;
880 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000881 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100882
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100883 switch (genimg_get_format ((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400884#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100885 case IMAGE_FORMAT_LEGACY:
886 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100887
Simon Glass0169e6b2012-02-13 13:51:18 +0000888 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100889 image_print_contents (hdr);
890
891 cnt = image_get_image_size (hdr);
892 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200893#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100894#if defined(CONFIG_FIT)
895 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100896 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100897 puts ("Fit image detected...\n");
898
899 cnt = fit_get_size (fit_hdr);
900 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100901#endif
902 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000903 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100904 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100905 return 1;
906 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000907 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100908
Scott Wood08364d92016-05-30 13:57:54 -0500909 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
910 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100911 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200912 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000913 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100914 return 1;
915 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000916 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100917
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100918#if defined(CONFIG_FIT)
919 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200920 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
Simon Glassd563c252021-02-15 17:08:09 -0700921 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000922 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200923 puts ("** Bad FIT image format\n");
924 return 1;
925 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000926 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200927 fit_print_contents (fit_hdr);
928 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100929#endif
930
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100931 /* Loading ok, update default load address */
932
Simon Glass892265d2019-12-28 10:45:02 -0700933 image_load_addr = addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100934
Mike Frysinger194c2e82011-06-05 13:43:02 +0000935 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100936}
937
Simon Glassed38aef2020-05-10 11:40:03 -0600938static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
939 char *const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +0200940{
941 char *boot_device = NULL;
942 int idx;
943 ulong addr, offset = 0;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500944 struct mtd_info *mtd;
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200945#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200946 struct mtd_device *dev;
947 struct part_info *part;
948 u8 pnum;
949
950 if (argc >= 2) {
951 char *p = (argc == 2) ? argv[1] : argv[2];
952 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
953 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
954 if (dev->id->type != MTD_DEV_TYPE_NAND) {
955 puts("Not a NAND device\n");
956 return 1;
957 }
958 if (argc > 3)
959 goto usage;
960 if (argc == 3)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600961 addr = hextoul(argv[1], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +0200962 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200963 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500964
965 mtd = get_nand_dev_by_index(dev->id->num);
966 return nand_load_image(cmdtp, mtd, part->offset,
967 addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200968 }
969 }
970#endif
971
Simon Glass0169e6b2012-02-13 13:51:18 +0000972 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +0200973 switch (argc) {
974 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200975 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass64b723f2017-08-03 12:22:12 -0600976 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200977 break;
978 case 2:
Simon Glass3ff49ec2021-07-24 09:03:29 -0600979 addr = hextoul(argv[1], NULL);
Simon Glass64b723f2017-08-03 12:22:12 -0600980 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +0200981 break;
982 case 3:
Simon Glass3ff49ec2021-07-24 09:03:29 -0600983 addr = hextoul(argv[1], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +0200984 boot_device = argv[2];
985 break;
986 case 4:
Simon Glass3ff49ec2021-07-24 09:03:29 -0600987 addr = hextoul(argv[1], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +0200988 boot_device = argv[2];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600989 offset = hextoul(argv[3], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +0200990 break;
991 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200992#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200993usage:
994#endif
Simon Glass0169e6b2012-02-13 13:51:18 +0000995 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +0000996 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +0200997 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000998 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +0200999
1000 if (!boot_device) {
1001 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +00001002 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001003 return 1;
1004 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001005 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001006
Simon Glass3ff49ec2021-07-24 09:03:29 -06001007 idx = hextoul(boot_device, NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +02001008
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001009 mtd = get_nand_dev_by_index(idx);
1010 if (!mtd) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001011 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +00001012 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001013 return 1;
1014 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001015 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001016
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001017 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001018}
1019
1020U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001021 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001022 "[partition] | [[[loadAddr] dev] offset]"
1023);