blob: c42ab24ec06d140cae7754d2833a367741a06ac6 [file] [log] [blame]
wdenkdc7c9a12003-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>
wdenk384cc682005-04-03 22:35:21 +00006 *
Ben Gardinerc9f73512010-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.
wdenk384cc682005-04-03 22:35:21 +000011 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
Scott Woodea533c22010-08-02 13:04:24 -050013 *
Scott Wood418396e2012-03-02 14:01:57 -060014 * Copyright 2010, 2012 Freescale Semiconductor
Scott Woodea533c22010-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.
wdenkdc7c9a12003-03-26 06:55:25 +000020 */
21
22#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060023#include <bootstage.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070024#include <image.h>
Simon Glass90526e92020-05-10 11:39:56 -060025#include <asm/cache.h>
William Juulcfa460a2007-10-31 13:53:06 +010026#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010027#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070028#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060029#include <env.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010030#include <watchdog.h>
31#include <malloc.h>
32#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010033#include <jffs2/jffs2.h>
34#include <nand.h>
35
Miquel Raynaleb446ef2019-10-25 19:39:29 +020036#include "legacy-mtd-utils.h"
37
Ladislav Michl0c8a8492009-04-21 02:26:31 +020038#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020039
Wolfgang Denk445093d2009-11-17 21:27:39 +010040/* partition handling routines */
Stefan Roese856f0542006-10-28 15:55:52 +020041int mtdparts_init(void);
Stefan Roese856f0542006-10-28 15:55:52 +020042int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk4b070802008-08-14 14:41:06 +020043 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020044#endif
45
Scott Wood151c06e2016-05-30 13:57:54 -050046static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
47 int repeat)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010048{
49 int i;
William Juul9ad754f2007-12-14 16:33:45 +010050 u_char *datbuf, *oobbuf, *p;
Scott Wood8c5659a2010-08-25 15:24:01 -050051 static loff_t last;
Masahiro Yamadae40520b2013-07-11 17:27:12 +090052 int ret = 0;
Scott Wood8c5659a2010-08-25 15:24:01 -050053
54 if (repeat)
Scott Wood151c06e2016-05-30 13:57:54 -050055 off = last + mtd->writesize;
Scott Wood8c5659a2010-08-25 15:24:01 -050056
57 last = off;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010058
Scott Wood151c06e2016-05-30 13:57:54 -050059 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090060 if (!datbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010061 puts("No memory for page buffer\n");
62 return 1;
63 }
Masahiro Yamadae40520b2013-07-11 17:27:12 +090064
Scott Wood151c06e2016-05-30 13:57:54 -050065 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090066 if (!oobbuf) {
67 puts("No memory for page buffer\n");
68 ret = 1;
69 goto free_dat;
70 }
Scott Wood151c06e2016-05-30 13:57:54 -050071 off &= ~(mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010072 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010073 struct mtd_oob_ops ops;
74 memset(&ops, 0, sizeof(ops));
75 ops.datbuf = datbuf;
Tom Rinicfdae122012-02-23 15:47:46 -070076 ops.oobbuf = oobbuf;
Scott Wood151c06e2016-05-30 13:57:54 -050077 ops.len = mtd->writesize;
78 ops.ooblen = mtd->oobsize;
Sergey Lapindfe64e22013-01-14 03:46:50 +000079 ops.mode = MTD_OPS_RAW;
Scott Wood151c06e2016-05-30 13:57:54 -050080 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010081 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020082 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090083 ret = 1;
84 goto free_all;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010085 }
Stefan Roesee8706902008-07-10 10:10:54 +020086 printf("Page %08lx dump:\n", off);
Wolfgang Denk4b070802008-08-14 14:41:06 +020087
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090088 if (!only_oob) {
Scott Wood151c06e2016-05-30 13:57:54 -050089 i = mtd->writesize >> 4;
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090090 p = datbuf;
91
92 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010093 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
94 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
95 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Wooddfbf6172008-06-12 13:20:16 -050096 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
97 p[15]);
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090098 p += 16;
99 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100100 }
Masahiro Yamada7d25cd32013-07-11 17:27:13 +0900101
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102 puts("OOB:\n");
Scott Wood151c06e2016-05-30 13:57:54 -0500103 i = mtd->oobsize >> 3;
Tom Rinicfdae122012-02-23 15:47:46 -0700104 p = oobbuf;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100105 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +0100106 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
107 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100108 p += 8;
109 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100110
Masahiro Yamadae40520b2013-07-11 17:27:12 +0900111free_all:
112 free(oobbuf);
113free_dat:
114 free(datbuf);
115
116 return ret;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100117}
118
119/* ------------------------------------------------------------------------- */
120
Scott Woodea533c22010-08-02 13:04:24 -0500121static int set_dev(int dev)
122{
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500123 struct mtd_info *mtd = get_nand_dev_by_index(dev);
124
125 if (!mtd)
126 return -ENODEV;
Scott Woodea533c22010-08-02 13:04:24 -0500127
128 if (nand_curr_device == dev)
129 return 0;
130
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500131 printf("Device %d: %s", dev, mtd->name);
Scott Woodea533c22010-08-02 13:04:24 -0500132 puts("... is now current device\n");
133 nand_curr_device = dev;
134
135#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashko6308e712017-06-26 19:12:58 -0500136 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woodea533c22010-08-02 13:04:24 -0500137#endif
138
139 return 0;
140}
141
Nishanth Menon50657c22008-12-13 09:43:06 -0600142#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
143static void print_status(ulong start, ulong end, ulong erasesize, int status)
144{
Joe Hershbergere70bfa22012-08-22 16:49:45 -0500145 /*
146 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
147 * not the same as others. Instead of bit 1 being lock, it is
148 * #lock_tight. To make the driver support either format, ignore bit 1
149 * and use only bit 0 and bit 2.
150 */
Nishanth Menon50657c22008-12-13 09:43:06 -0600151 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
152 start,
153 end - 1,
154 (end - start) / erasesize,
155 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershbergere70bfa22012-08-22 16:49:45 -0500156 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menon50657c22008-12-13 09:43:06 -0600157 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
158}
159
Scott Wood151c06e2016-05-30 13:57:54 -0500160static void do_nand_status(struct mtd_info *mtd)
Nishanth Menon50657c22008-12-13 09:43:06 -0600161{
162 ulong block_start = 0;
163 ulong off;
164 int last_status = -1;
165
Scott Wood17cb4b82016-05-30 13:57:56 -0500166 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menon50657c22008-12-13 09:43:06 -0600167 /* check the WP bit */
Scott Wood151c06e2016-05-30 13:57:54 -0500168 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menon50657c22008-12-13 09:43:06 -0600169 printf("device is %swrite protected\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500170 (nand_chip->read_byte(mtd) & 0x80 ?
171 "NOT " : ""));
Nishanth Menon50657c22008-12-13 09:43:06 -0600172
Scott Wood151c06e2016-05-30 13:57:54 -0500173 for (off = 0; off < mtd->size; off += mtd->erasesize) {
174 int s = nand_get_lock_status(mtd, off);
Nishanth Menon50657c22008-12-13 09:43:06 -0600175
176 /* print message only if status has changed */
177 if (s != last_status && off != 0) {
Scott Wood151c06e2016-05-30 13:57:54 -0500178 print_status(block_start, off, mtd->erasesize,
Nishanth Menon50657c22008-12-13 09:43:06 -0600179 last_status);
180 block_start = off;
181 }
182 last_status = s;
183 }
184 /* Print the last block info */
Scott Wood151c06e2016-05-30 13:57:54 -0500185 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menon50657c22008-12-13 09:43:06 -0600186}
187#endif
188
Ben Gardinerc9f73512010-07-05 13:27:07 -0400189#ifdef CONFIG_ENV_OFFSET_OOB
190unsigned long nand_env_oob_offset;
191
Scott Woodea533c22010-08-02 13:04:24 -0500192int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
Ben Gardinerc9f73512010-07-05 13:27:07 -0400193{
194 int ret;
195 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500196 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400197 char *cmd = argv[1];
198
Scott Wood8b7d5122016-09-01 17:31:31 -0500199 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woodea533c22010-08-02 13:04:24 -0500200 puts("no devices available\n");
201 return 1;
202 }
203
204 set_dev(0);
205
Ben Gardinerc9f73512010-07-05 13:27:07 -0400206 if (!strcmp(cmd, "get")) {
Scott Wood151c06e2016-05-30 13:57:54 -0500207 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500208 if (ret)
Ben Gardinerc9f73512010-07-05 13:27:07 -0400209 return 1;
Scott Wood53504a22010-07-12 18:17:40 -0500210
211 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400212 } else if (!strcmp(cmd, "set")) {
Scott Woodea533c22010-08-02 13:04:24 -0500213 loff_t addr;
214 loff_t maxsize;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400215 struct mtd_oob_ops ops;
Scott Woodea533c22010-08-02 13:04:24 -0500216 int idx = 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400217
218 if (argc < 3)
219 goto usage;
220
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500221 mtd = get_nand_dev_by_index(idx);
Tom Rinic39d6a02013-03-14 05:32:50 +0000222 /* We don't care about size, or maxsize. */
Heiko Schocher09c32802015-04-27 07:42:05 +0200223 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500224 MTD_DEV_TYPE_NAND, mtd->size)) {
Heiko Schocher09c32802015-04-27 07:42:05 +0200225 puts("Offset or partition name expected\n");
226 return 1;
227 }
228 if (set_dev(idx)) {
Scott Woodea533c22010-08-02 13:04:24 -0500229 puts("Offset or partition name expected\n");
230 return 1;
231 }
232
233 if (idx != 0) {
234 puts("Partition not on first NAND device\n");
Ben Gardinerc9f73512010-07-05 13:27:07 -0400235 return 1;
236 }
237
Scott Wood151c06e2016-05-30 13:57:54 -0500238 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood53504a22010-07-12 18:17:40 -0500239 printf("Insufficient available OOB bytes:\n"
240 "%d OOB bytes available but %d required for "
241 "env.oob support\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500242 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400243 return 1;
244 }
245
Scott Wood151c06e2016-05-30 13:57:54 -0500246 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400247 printf("Environment offset must be block-aligned\n");
248 return 1;
249 }
250
251 ops.datbuf = NULL;
252 ops.mode = MTD_OOB_AUTO;
253 ops.ooboffs = 0;
254 ops.ooblen = ENV_OFFSET_SIZE;
255 ops.oobbuf = (void *) oob_buf;
256
257 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood151c06e2016-05-30 13:57:54 -0500258 oob_buf[1] = addr / mtd->erasesize;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400259
Scott Wood151c06e2016-05-30 13:57:54 -0500260 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500261 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400262 printf("Error writing OOB block 0\n");
263 return ret;
264 }
Scott Wood53504a22010-07-12 18:17:40 -0500265
Scott Wood151c06e2016-05-30 13:57:54 -0500266 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500267 if (ret) {
268 printf("Error reading env offset in OOB\n");
269 return ret;
270 }
271
272 if (addr != nand_env_oob_offset) {
273 printf("Verification of env offset in OOB failed: "
Scott Woodea533c22010-08-02 13:04:24 -0500274 "0x%08llx expected but got 0x%08lx\n",
275 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500276 return 1;
277 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400278 } else {
279 goto usage;
280 }
281
282 return ret;
283
284usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000285 return CMD_RET_USAGE;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400286}
287
288#endif
289
Marek Vasutce80ddc2011-09-22 03:57:26 +0200290static void nand_print_and_set_info(int idx)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100291{
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500292 struct mtd_info *mtd;
293 struct nand_chip *chip;
Marek Vasutce80ddc2011-09-22 03:57:26 +0200294
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500295 mtd = get_nand_dev_by_index(idx);
296 if (!mtd)
297 return;
298
299 chip = mtd_to_nand(mtd);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100300 printf("Device %d: ", idx);
301 if (chip->numchips > 1)
302 printf("%dx ", chip->numchips);
303 printf("%s, sector size %u KiB\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500304 mtd->name, mtd->erasesize >> 10);
305 printf(" Page size %8d b\n", mtd->writesize);
306 printf(" OOB size %8d b\n", mtd->oobsize);
307 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocher5db73fe2015-04-12 10:18:09 +0200308 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmann66dc09c2017-07-10 08:44:24 +0200309 printf(" options 0x%08x\n", chip->options);
310 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasutce80ddc2011-09-22 03:57:26 +0200311
312 /* Set geometry info */
Simon Glass018f5302017-08-03 12:22:10 -0600313 env_set_hex("nand_writesize", mtd->writesize);
314 env_set_hex("nand_oobsize", mtd->oobsize);
315 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100316}
317
Scott Wood151c06e2016-05-30 13:57:54 -0500318static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200319 ulong count, int read, int no_verify)
Scott Wood418396e2012-03-02 14:01:57 -0600320{
321 int ret = 0;
Scott Wood418396e2012-03-02 14:01:57 -0600322
323 while (count--) {
324 /* Raw access */
325 mtd_oob_ops_t ops = {
326 .datbuf = (u8 *)addr,
Scott Wood151c06e2016-05-30 13:57:54 -0500327 .oobbuf = ((u8 *)addr) + mtd->writesize,
328 .len = mtd->writesize,
329 .ooblen = mtd->oobsize,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000330 .mode = MTD_OPS_RAW
Scott Wood418396e2012-03-02 14:01:57 -0600331 };
332
Peter Tyser6b94f112015-02-03 11:58:13 -0600333 if (read) {
Scott Wood151c06e2016-05-30 13:57:54 -0500334 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser6b94f112015-02-03 11:58:13 -0600335 } else {
Scott Wood151c06e2016-05-30 13:57:54 -0500336 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200337 if (!ret && !no_verify)
Scott Wood151c06e2016-05-30 13:57:54 -0500338 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser6b94f112015-02-03 11:58:13 -0600339 }
Scott Wood418396e2012-03-02 14:01:57 -0600340
341 if (ret) {
342 printf("%s: error at offset %llx, ret %d\n",
343 __func__, (long long)off, ret);
344 break;
345 }
346
Scott Wood151c06e2016-05-30 13:57:54 -0500347 addr += mtd->writesize + mtd->oobsize;
348 off += mtd->writesize;
Scott Wood418396e2012-03-02 14:01:57 -0600349 }
350
351 return ret;
352}
353
Harvey Chapmane8344022013-02-26 17:57:14 +0000354/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood9b80aa82013-06-20 12:45:31 -0500355 * read/write past the end of a chip/partition by accident.
Harvey Chapmane8344022013-02-26 17:57:14 +0000356 */
357static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
358{
359 /* We grab the nand info object here fresh because this is usually
360 * called after arg_off_size() which can change the value of dev.
361 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500362 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapmane8344022013-02-26 17:57:14 +0000363 loff_t maxoffset = offset + *size;
364 int badblocks = 0;
365
366 /* count badblocks in NAND from offset to offset + size */
Scott Wood151c06e2016-05-30 13:57:54 -0500367 for (; offset < maxoffset; offset += mtd->erasesize) {
368 if (nand_block_isbad(mtd, offset))
Harvey Chapmane8344022013-02-26 17:57:14 +0000369 badblocks++;
370 }
371 /* adjust size if any bad blocks found */
372 if (badblocks) {
Scott Wood151c06e2016-05-30 13:57:54 -0500373 *size -= badblocks * mtd->erasesize;
Harvey Chapmane8344022013-02-26 17:57:14 +0000374 printf("size adjusted to 0x%llx (%d bad blocks)\n",
375 (unsigned long long)*size, badblocks);
376 }
377}
378
Kim Phillips088f1b12012-10-29 13:34:31 +0000379static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100380{
Scott Woodea533c22010-08-02 13:04:24 -0500381 int i, ret = 0;
382 ulong addr;
Tom Rinic39d6a02013-03-14 05:32:50 +0000383 loff_t off, size, maxsize;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100384 char *cmd, *s;
Scott Wood151c06e2016-05-30 13:57:54 -0500385 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200386#ifdef CONFIG_SYS_NAND_QUIET
387 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200388#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200389 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200390#endif
Simon Glass00caae62017-08-03 12:22:12 -0600391 const char *quiet_str = env_get("quiet");
Scott Woodea533c22010-08-02 13:04:24 -0500392 int dev = nand_curr_device;
Scott Wood8c5659a2010-08-25 15:24:01 -0500393 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100394
395 /* at least two arguments please */
396 if (argc < 2)
397 goto usage;
398
Stefan Roese2255b2d2006-10-10 12:36:02 +0200399 if (quiet_str)
400 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
401
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100402 cmd = argv[1];
403
Scott Wood8c5659a2010-08-25 15:24:01 -0500404 /* Only "dump" is repeatable. */
405 if (repeat && strcmp(cmd, "dump"))
406 return 0;
407
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100408 if (strcmp(cmd, "info") == 0) {
409
410 putc('\n');
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500411 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
412 nand_print_and_set_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100413 return 0;
414 }
415
416 if (strcmp(cmd, "device") == 0) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100417 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100418 putc('\n');
Scott Woodea533c22010-08-02 13:04:24 -0500419 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100420 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100421 else
Marek Vasutce80ddc2011-09-22 03:57:26 +0200422 nand_print_and_set_info(dev);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100423 return 0;
424 }
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200425
Scott Woodea533c22010-08-02 13:04:24 -0500426 dev = (int)simple_strtoul(argv[2], NULL, 10);
427 set_dev(dev);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200428
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100429 return 0;
430 }
431
Ben Gardinerc9f73512010-07-05 13:27:07 -0400432#ifdef CONFIG_ENV_OFFSET_OOB
433 /* this command operates only on the first nand device */
Scott Woodea533c22010-08-02 13:04:24 -0500434 if (strcmp(cmd, "env.oob") == 0)
435 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400436#endif
437
Scott Woodea533c22010-08-02 13:04:24 -0500438 /* The following commands operate on the current device, unless
439 * overridden by a partition specifier. Note that if somehow the
440 * current device is invalid, it will have to be changed to a valid
441 * one before these commands can run, even if a partition specifier
442 * for another device is to be used.
443 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500444 mtd = get_nand_dev_by_index(dev);
445 if (!mtd) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100446 puts("\nno devices available\n");
447 return 1;
448 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100449
450 if (strcmp(cmd, "bad") == 0) {
Scott Woodea533c22010-08-02 13:04:24 -0500451 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood151c06e2016-05-30 13:57:54 -0500452 for (off = 0; off < mtd->size; off += mtd->erasesize)
453 if (nand_block_isbad(mtd, off))
Scott Woodea533c22010-08-02 13:04:24 -0500454 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100455 return 0;
456 }
457
Stefan Roese856f0542006-10-28 15:55:52 +0200458 /*
459 * Syntax is:
460 * 0 1 2 3 4
461 * nand erase [clean] [off size]
462 */
Scott Wood30486322010-08-25 14:43:29 -0500463 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200464 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200465 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100466 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasut60899812011-09-14 00:20:35 +0200467 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
468 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood30486322010-08-25 14:43:29 -0500469 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood30486322010-08-25 14:43:29 -0500470 int spread = 0;
471 int args = 2;
Marek Vasut60899812011-09-14 00:20:35 +0200472 const char *scrub_warn =
473 "Warning: "
474 "scrub option will erase all factory set bad blocks!\n"
475 " "
476 "There is no reliable way to recover them.\n"
477 " "
478 "Use this command only for testing purposes if you\n"
479 " "
480 "are sure of what you are doing!\n"
481 "\nReally scrub this NAND flash? <y/N>\n";
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100482
Scott Wood30486322010-08-25 14:43:29 -0500483 if (cmd[5] != 0) {
484 if (!strcmp(&cmd[5], ".spread")) {
485 spread = 1;
486 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood30486322010-08-25 14:43:29 -0500487 args = 1;
488 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood30486322010-08-25 14:43:29 -0500489 args = 0;
490 } else {
491 goto usage;
492 }
493 }
494
495 /*
496 * Don't allow missing arguments to cause full chip/partition
497 * erases -- easy to do accidentally, e.g. with a misspelled
498 * variable name.
499 */
500 if (argc != o + args)
501 goto usage;
502
503 printf("\nNAND %s: ", cmd);
Stefan Roese856f0542006-10-28 15:55:52 +0200504 /* skip first two or three arguments, look for offset and size */
Heiko Schocher09c32802015-04-27 07:42:05 +0200505 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
506 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500507 mtd->size) != 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200508 return 1;
509
510 if (set_dev(dev))
Stefan Roese856f0542006-10-28 15:55:52 +0200511 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200512
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500513 mtd = get_nand_dev_by_index(dev);
Scott Woodea533c22010-08-02 13:04:24 -0500514
Stefan Roese2255b2d2006-10-10 12:36:02 +0200515 memset(&opts, 0, sizeof(opts));
516 opts.offset = off;
517 opts.length = size;
518 opts.jffs2 = clean;
519 opts.quiet = quiet;
Scott Wood30486322010-08-25 14:43:29 -0500520 opts.spread = spread;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200521
522 if (scrub) {
Pierre Auberta5dffa42014-04-24 10:30:07 +0200523 if (scrub_yes) {
Marek Vasut60899812011-09-14 00:20:35 +0200524 opts.scrub = 1;
Pierre Auberta5dffa42014-04-24 10:30:07 +0200525 } else {
526 puts(scrub_warn);
527 if (confirm_yesno()) {
Florian Fainelli6b94b492010-03-20 19:02:58 +0100528 opts.scrub = 1;
Pierre Auberta5dffa42014-04-24 10:30:07 +0200529 } else {
Florian Fainelli6b94b492010-03-20 19:02:58 +0100530 puts("scrub aborted\n");
Masahiro Yamada46aabcc2013-07-11 17:29:57 +0900531 return 1;
Florian Fainelli6b94b492010-03-20 19:02:58 +0100532 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200533 }
534 }
Scott Wood151c06e2016-05-30 13:57:54 -0500535 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100536 printf("%s\n", ret ? "ERROR" : "OK");
537
538 return ret == 0 ? 0 : 1;
539 }
540
541 if (strncmp(cmd, "dump", 4) == 0) {
542 if (argc < 3)
543 goto usage;
544
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100545 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood151c06e2016-05-30 13:57:54 -0500546 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100547
548 return ret == 0 ? 1 : 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100549 }
550
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100551 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woodea533c22010-08-02 13:04:24 -0500552 size_t rwsize;
Scott Wood418396e2012-03-02 14:01:57 -0600553 ulong pagecount = 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200554 int read;
Harvey Chapmanced199d2013-02-07 11:34:44 +0000555 int raw = 0;
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200556 int no_verify = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200557
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100558 if (argc < 4)
559 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200560
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100561 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
562
Stefan Roese2255b2d2006-10-10 12:36:02 +0200563 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200564 printf("\nNAND %s: ", read ? "read" : "write");
William Juul4cbb6512007-11-08 10:39:53 +0100565
Stefan Roese2255b2d2006-10-10 12:36:02 +0200566 s = strchr(cmd, '.');
Scott Wood418396e2012-03-02 14:01:57 -0600567
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200568 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood418396e2012-03-02 14:01:57 -0600569 raw = 1;
570
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200571 if (!strcmp(s, ".raw.noverify"))
572 no_verify = 1;
573
Heiko Schocher09c32802015-04-27 07:42:05 +0200574 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
575 MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500576 mtd->size))
Heiko Schocher09c32802015-04-27 07:42:05 +0200577 return 1;
578
579 if (set_dev(dev))
Scott Wood418396e2012-03-02 14:01:57 -0600580 return 1;
581
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500582 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovy93d32322014-09-16 14:38:52 +0200583
Scott Wood418396e2012-03-02 14:01:57 -0600584 if (argc > 4 && !str2long(argv[4], &pagecount)) {
585 printf("'%s' is not a number\n", argv[4]);
586 return 1;
587 }
588
Scott Wood151c06e2016-05-30 13:57:54 -0500589 if (pagecount * mtd->writesize > size) {
Scott Wood418396e2012-03-02 14:01:57 -0600590 puts("Size exceeds partition or device limit\n");
591 return -1;
592 }
593
Scott Wood151c06e2016-05-30 13:57:54 -0500594 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood418396e2012-03-02 14:01:57 -0600595 } else {
Heiko Schocher09c32802015-04-27 07:42:05 +0200596 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
597 &size, &maxsize,
598 MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500599 mtd->size) != 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200600 return 1;
601
602 if (set_dev(dev))
Scott Wood418396e2012-03-02 14:01:57 -0600603 return 1;
604
Harvey Chapmane8344022013-02-26 17:57:14 +0000605 /* size is unspecified */
606 if (argc < 5)
607 adjust_size_for_badblocks(&size, off, dev);
Scott Wood418396e2012-03-02 14:01:57 -0600608 rwsize = size;
609 }
610
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500611 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovy93d32322014-09-16 14:38:52 +0200612
Scott Wood984e03c2008-06-12 13:13:23 -0500613 if (!s || !strcmp(s, ".jffs2") ||
614 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500615 if (read)
Scott Wood151c06e2016-05-30 13:57:54 -0500616 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rinic39d6a02013-03-14 05:32:50 +0000617 NULL, maxsize,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200618 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500619 else
Scott Wood151c06e2016-05-30 13:57:54 -0500620 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rinic39d6a02013-03-14 05:32:50 +0000621 NULL, maxsize,
Peter Tyser6b94f112015-02-03 11:58:13 -0600622 (u_char *)addr,
623 WITH_WR_VERIFY);
Ben Gardinerc9494862011-06-14 16:35:07 -0400624#ifdef CONFIG_CMD_NAND_TRIMFFS
625 } else if (!strcmp(s, ".trimffs")) {
626 if (read) {
627 printf("Unknown nand command suffix '%s'\n", s);
628 return 1;
629 }
Scott Wood151c06e2016-05-30 13:57:54 -0500630 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rinic39d6a02013-03-14 05:32:50 +0000631 maxsize, (u_char *)addr,
Peter Tyser6b94f112015-02-03 11:58:13 -0600632 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerc9494862011-06-14 16:35:07 -0400633#endif
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400634 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100635 /* out-of-band data */
636 mtd_oob_ops_t ops = {
637 .oobbuf = (u8 *)addr,
Scott Woodea533c22010-08-02 13:04:24 -0500638 .ooblen = rwsize,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000639 .mode = MTD_OPS_RAW
William Juulcfa460a2007-10-31 13:53:06 +0100640 };
641
Harald Welte62d4f432007-12-19 14:12:53 +0100642 if (read)
Scott Wood151c06e2016-05-30 13:57:54 -0500643 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100644 else
Scott Wood151c06e2016-05-30 13:57:54 -0500645 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood418396e2012-03-02 14:01:57 -0600646 } else if (raw) {
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200647 ret = raw_access(mtd, addr, off, pagecount, read,
648 no_verify);
Stefan Roese856f0542006-10-28 15:55:52 +0200649 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500650 printf("Unknown nand command suffix '%s'.\n", s);
651 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200652 }
653
Scott Woodea533c22010-08-02 13:04:24 -0500654 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200655 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100656
657 return ret == 0 ? 0 : 1;
658 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200659
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100660#ifdef CONFIG_CMD_NAND_TORTURE
661 if (strcmp(cmd, "torture") == 0) {
Max Krummenacher1866be72016-06-13 10:15:48 +0200662 loff_t endoff;
663 unsigned int failed = 0, passed = 0;
664
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100665 if (argc < 3)
666 goto usage;
667
668 if (!str2off(argv[2], &off)) {
669 puts("Offset is not a valid number\n");
670 return 1;
671 }
672
Max Krummenacher1866be72016-06-13 10:15:48 +0200673 size = mtd->erasesize;
674 if (argc > 3) {
675 if (!str2off(argv[3], &size)) {
676 puts("Size is not a valid number\n");
677 return 1;
678 }
679 }
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100680
Max Krummenacher1866be72016-06-13 10:15:48 +0200681 endoff = off + size;
682 if (endoff > mtd->size) {
683 puts("Arguments beyond end of NAND\n");
684 return 1;
685 }
686
687 off = round_down(off, mtd->erasesize);
688 endoff = round_up(endoff, mtd->erasesize);
689 size = endoff - off;
690 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
691 dev, off, size, mtd->erasesize);
692 while (off < endoff) {
693 ret = nand_torture(mtd, off);
694 if (ret) {
695 failed++;
696 printf(" block at 0x%llx failed\n", off);
697 } else {
698 passed++;
699 }
700 off += mtd->erasesize;
701 }
702 printf(" Passed: %u, failed: %u\n", passed, failed);
703 return failed != 0;
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100704 }
705#endif
706
Stefan Roese2255b2d2006-10-10 12:36:02 +0200707 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200708 argc -= 2;
709 argv += 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200710
Wolfgang Denk8360b662009-05-24 17:34:33 +0200711 if (argc <= 0)
712 goto usage;
713
714 while (argc > 0) {
715 addr = simple_strtoul(*argv, NULL, 16);
716
Scott Wood151c06e2016-05-30 13:57:54 -0500717 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200718 printf("block 0x%08lx NOT marked "
719 "as bad! ERROR %d\n",
720 addr, ret);
721 ret = 1;
722 } else {
723 printf("block 0x%08lx successfully "
724 "marked as bad\n",
725 addr);
726 }
727 --argc;
728 ++argv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200729 }
Wolfgang Denk8360b662009-05-24 17:34:33 +0200730 return ret;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200731 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500732
Stefan Roese2255b2d2006-10-10 12:36:02 +0200733 if (strcmp(cmd, "biterr") == 0) {
734 /* todo */
735 return 1;
736 }
737
Nishanth Menon50657c22008-12-13 09:43:06 -0600738#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200739 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600740 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200741 int status = 0;
742 if (argc == 3) {
743 if (!strcmp("tight", argv[2]))
744 tight = 1;
745 if (!strcmp("status", argv[2]))
746 status = 1;
747 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200748 if (status) {
Scott Wood151c06e2016-05-30 13:57:54 -0500749 do_nand_status(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100750 } else {
Scott Wood151c06e2016-05-30 13:57:54 -0500751 if (!nand_lock(mtd, tight)) {
William Juul5e1dae52007-11-09 13:32:30 +0100752 puts("NAND flash successfully locked\n");
753 } else {
754 puts("Error locking NAND flash\n");
755 return 1;
756 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200757 }
758 return 0;
759 }
760
Joe Hershbergereee623a2012-08-22 16:49:42 -0500761 if (strncmp(cmd, "unlock", 5) == 0) {
762 int allexcept = 0;
763
764 s = strchr(cmd, '.');
765
766 if (s && !strcmp(s, ".allexcept"))
767 allexcept = 1;
768
Heiko Schocher09c32802015-04-27 07:42:05 +0200769 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
770 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500771 mtd->size) < 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200772 return 1;
773
774 if (set_dev(dev))
Stefan Roese856f0542006-10-28 15:55:52 +0200775 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200776
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500777 mtd = get_nand_dev_by_index(dev);
778
779 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul5e1dae52007-11-09 13:32:30 +0100780 puts("NAND flash successfully unlocked\n");
781 } else {
782 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100783 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100784 return 1;
785 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200786 return 0;
787 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600788#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200789
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100790usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000791 return CMD_RET_USAGE;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100792}
793
Kim Phillips088f1b12012-10-29 13:34:31 +0000794#ifdef CONFIG_SYS_LONGHELP
795static char nand_help_text[] =
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200796 "info - show available NAND devices\n"
797 "nand device [dev] - show or set current device\n"
798 "nand read - addr off|partition size\n"
799 "nand write - addr off|partition size\n"
800 " read/write 'size' bytes starting at offset 'off'\n"
801 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood418396e2012-03-02 14:01:57 -0600802 "nand read.raw - addr off|partition [count]\n"
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200803 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood418396e2012-03-02 14:01:57 -0600804 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerc9494862011-06-14 16:35:07 -0400805#ifdef CONFIG_CMD_NAND_TRIMFFS
806 "nand write.trimffs - addr off|partition size\n"
807 " write 'size' bytes starting at offset 'off' from memory address\n"
808 " 'addr', skipping bad blocks and dropping any pages at the end\n"
809 " of eraseblocks that contain only 0xFF\n"
810#endif
Daniel Hobieb3abce2011-05-19 19:28:54 +0200811 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood30486322010-08-25 14:43:29 -0500812 "from offset 'off'\n"
813 " With '.spread', erase enough for given file size, otherwise,\n"
814 " 'size' includes skipped bad blocks.\n"
815 "nand erase.part [clean] partition - erase entire mtd partition'\n"
816 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200817 "nand bad - show bad blocks\n"
818 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100819#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacher1866be72016-06-13 10:15:48 +0200820 "nand torture off - torture one block at offset\n"
821 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100822#endif
Marek Vasut60899812011-09-14 00:20:35 +0200823 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood30486322010-08-25 14:43:29 -0500824 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200825 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
826 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menon50657c22008-12-13 09:43:06 -0600827#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200828 "\n"
829 "nand lock [tight] [status]\n"
830 " bring nand to lock state or display locked pages\n"
Joe Hershbergereee623a2012-08-22 16:49:42 -0500831 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menon50657c22008-12-13 09:43:06 -0600832#endif
Ben Gardinerc9f73512010-07-05 13:27:07 -0400833#ifdef CONFIG_ENV_OFFSET_OOB
834 "\n"
835 "nand env.oob - environment offset in OOB of block 0 of"
836 " first device.\n"
837 "nand env.oob set off|partition - set enviromnent offset\n"
838 "nand env.oob get - get environment offset"
839#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000840 "";
841#endif
842
843U_BOOT_CMD(
844 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
845 "NAND sub-system", nand_help_text
Nishanth Menon50657c22008-12-13 09:43:06 -0600846);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100847
Scott Wood151c06e2016-05-30 13:57:54 -0500848static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200849 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200850{
851 int r;
Mike Frysinger67d668b2011-06-05 13:43:02 +0000852 char *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200853 size_t cnt;
Tom Rinic76c93a2019-05-23 07:14:07 -0400854#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Stefan Roese856f0542006-10-28 15:55:52 +0200855 image_header_t *hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200856#endif
Marian Balakowicz09475f72008-03-12 10:33:01 +0100857#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200858 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100859#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200860
861 s = strchr(cmd, '.');
862 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500863 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500864 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass770605e2012-02-13 13:51:18 +0000865 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood984e03c2008-06-12 13:13:23 -0500866 return 1;
867 }
Stefan Roese856f0542006-10-28 15:55:52 +0200868
Scott Wood151c06e2016-05-30 13:57:54 -0500869 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Stefan Roese856f0542006-10-28 15:55:52 +0200870
Scott Wood151c06e2016-05-30 13:57:54 -0500871 cnt = mtd->writesize;
872 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
873 (u_char *)addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200874 if (r) {
875 puts("** Read error\n");
Simon Glass770605e2012-02-13 13:51:18 +0000876 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200877 return 1;
878 }
Simon Glass770605e2012-02-13 13:51:18 +0000879 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200880
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100881 switch (genimg_get_format ((void *)addr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -0400882#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100883 case IMAGE_FORMAT_LEGACY:
884 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200885
Simon Glass770605e2012-02-13 13:51:18 +0000886 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100887 image_print_contents (hdr);
888
889 cnt = image_get_image_size (hdr);
890 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200891#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100892#if defined(CONFIG_FIT)
893 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100894 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100895 puts ("Fit image detected...\n");
896
897 cnt = fit_get_size (fit_hdr);
898 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100899#endif
900 default:
Simon Glass770605e2012-02-13 13:51:18 +0000901 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100902 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200903 return 1;
904 }
Simon Glass770605e2012-02-13 13:51:18 +0000905 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Stefan Roese856f0542006-10-28 15:55:52 +0200906
Scott Wood151c06e2016-05-30 13:57:54 -0500907 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
908 (u_char *)addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200909 if (r) {
910 puts("** Read error\n");
Simon Glass770605e2012-02-13 13:51:18 +0000911 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200912 return 1;
913 }
Simon Glass770605e2012-02-13 13:51:18 +0000914 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200915
Marian Balakowicz09475f72008-03-12 10:33:01 +0100916#if defined(CONFIG_FIT)
917 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200918 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
919 if (!fit_check_format (fit_hdr)) {
Simon Glass770605e2012-02-13 13:51:18 +0000920 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200921 puts ("** Bad FIT image format\n");
922 return 1;
923 }
Simon Glass770605e2012-02-13 13:51:18 +0000924 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200925 fit_print_contents (fit_hdr);
926 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100927#endif
928
Stefan Roese856f0542006-10-28 15:55:52 +0200929 /* Loading ok, update default load address */
930
Simon Glassbb872dd2019-12-28 10:45:02 -0700931 image_load_addr = addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200932
Mike Frysinger67d668b2011-06-05 13:43:02 +0000933 return bootm_maybe_autostart(cmdtp, cmd);
Stefan Roese856f0542006-10-28 15:55:52 +0200934}
935
Kim Phillips088f1b12012-10-29 13:34:31 +0000936static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
937 char * const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100938{
939 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200940 int idx;
941 ulong addr, offset = 0;
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500942 struct mtd_info *mtd;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200943#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200944 struct mtd_device *dev;
945 struct part_info *part;
946 u8 pnum;
947
948 if (argc >= 2) {
949 char *p = (argc == 2) ? argv[1] : argv[2];
950 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
951 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
952 if (dev->id->type != MTD_DEV_TYPE_NAND) {
953 puts("Not a NAND device\n");
954 return 1;
955 }
956 if (argc > 3)
957 goto usage;
958 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200959 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200960 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200961 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500962
963 mtd = get_nand_dev_by_index(dev->id->num);
964 return nand_load_image(cmdtp, mtd, part->offset,
965 addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200966 }
967 }
968#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100969
Simon Glass770605e2012-02-13 13:51:18 +0000970 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100971 switch (argc) {
972 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200973 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass00caae62017-08-03 12:22:12 -0600974 boot_device = env_get("bootdevice");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100975 break;
976 case 2:
977 addr = simple_strtoul(argv[1], NULL, 16);
Simon Glass00caae62017-08-03 12:22:12 -0600978 boot_device = env_get("bootdevice");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100979 break;
980 case 3:
981 addr = simple_strtoul(argv[1], NULL, 16);
982 boot_device = argv[2];
983 break;
984 case 4:
985 addr = simple_strtoul(argv[1], NULL, 16);
986 boot_device = argv[2];
987 offset = simple_strtoul(argv[3], NULL, 16);
988 break;
989 default:
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200990#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200991usage:
992#endif
Simon Glass770605e2012-02-13 13:51:18 +0000993 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000994 return CMD_RET_USAGE;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100995 }
Simon Glass770605e2012-02-13 13:51:18 +0000996 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100997
998 if (!boot_device) {
999 puts("\n** No boot device **\n");
Simon Glass770605e2012-02-13 13:51:18 +00001000 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001001 return 1;
1002 }
Simon Glass770605e2012-02-13 13:51:18 +00001003 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001004
Stefan Roese856f0542006-10-28 15:55:52 +02001005 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001006
Mugunthan V Nad92dff2017-06-26 19:12:51 -05001007 mtd = get_nand_dev_by_index(idx);
1008 if (!mtd) {
Stefan Roese856f0542006-10-28 15:55:52 +02001009 printf("\n** Device %d not available\n", idx);
Simon Glass770605e2012-02-13 13:51:18 +00001010 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001011 return 1;
1012 }
Simon Glass770605e2012-02-13 13:51:18 +00001013 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001014
Mugunthan V Nad92dff2017-06-26 19:12:51 -05001015 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001016}
1017
1018U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -06001019 "boot from NAND device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001020 "[partition] | [[[loadAddr] dev] offset]"
1021);