blob: 72ca88a943c5d795d4b4631ee52c2b0511b75b2b [file] [log] [blame]
wdenkc8434db2003-03-26 06:55:25 +00001/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
wdenk6d3c6d12005-04-03 22:35:21 +00006 *
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -04007 * Ported 'dynenv' to 'nand env.oob' command
8 * (C) 2010 Nanometrics, Inc.
9 * 'dynenv' -- Dynamic environment offset in NAND OOB
10 * (C) Copyright 2006-2007 OpenMoko, Inc.
wdenk6d3c6d12005-04-03 22:35:21 +000011 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
Scott Woode5836dc2010-08-02 13:04:24 -050013 *
Scott Wood733fb022012-03-02 14:01:57 -060014 * Copyright 2010, 2012 Freescale Semiconductor
Scott Woode5836dc2010-08-02 13:04:24 -050015 * The portions of this file whose copyright is held by Freescale and which
16 * are not considered a derived work of GPL v2-only code may be distributed
17 * and/or modified under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of the
19 * License, or (at your option) any later version.
wdenkc8434db2003-03-26 06:55:25 +000020 */
21
22#include <common.h>
William Juul52c07962007-10-31 13:53:06 +010023#include <linux/mtd/mtd.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010024#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070025#include <console.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010026#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010029#include <jffs2/jffs2.h>
30#include <nand.h>
31
Ladislav Michl9d0928d2009-04-21 02:26:31 +020032#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020033
Wolfgang Denk161bfc52009-11-17 21:27:39 +010034/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020035int mtdparts_init(void);
36int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
37int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020038 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020039#endif
40
Scott Wood08364d92016-05-30 13:57:54 -050041static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
42 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010043{
44 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010045 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -050046 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +090047 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -050048
49 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -050050 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -050051
52 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010053
Scott Wood08364d92016-05-30 13:57:54 -050054 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090055 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010056 puts("No memory for page buffer\n");
57 return 1;
58 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +090059
Scott Wood08364d92016-05-30 13:57:54 -050060 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090061 if (!oobbuf) {
62 puts("No memory for page buffer\n");
63 ret = 1;
64 goto free_dat;
65 }
Scott Wood08364d92016-05-30 13:57:54 -050066 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010067 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +010068 struct mtd_oob_ops ops;
69 memset(&ops, 0, sizeof(ops));
70 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -070071 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -050072 ops.len = mtd->writesize;
73 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +000074 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -050075 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010076 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020077 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +090078 ret = 1;
79 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010080 }
Stefan Roese3167d072008-07-10 10:10:54 +020081 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020082
Masahiro Yamada3f97e272013-07-11 17:27:13 +090083 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -050084 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +090085 p = datbuf;
86
87 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +010088 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
89 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
90 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Woodcc5f3392008-06-12 13:20:16 -050091 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
92 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +090093 p += 16;
94 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010095 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +090096
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010097 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -050098 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -070099 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100100 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100101 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
102 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100103 p += 8;
104 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900105
106free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100107 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900108free_dat:
109 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100110
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900111 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100112}
113
114/* ------------------------------------------------------------------------- */
115
Scott Woode5836dc2010-08-02 13:04:24 -0500116static int set_dev(int dev)
117{
Tony Lindgrenbccdfb52016-08-24 09:46:32 -0700118 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev]) {
Scott Woode5836dc2010-08-02 13:04:24 -0500119 puts("No such device\n");
120 return -1;
121 }
122
123 if (nand_curr_device == dev)
124 return 0;
125
Scott Wood2c1b7e12016-05-30 13:57:55 -0500126 printf("Device %d: %s", dev, nand_info[dev]->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500127 puts("... is now current device\n");
128 nand_curr_device = dev;
129
130#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Scott Wood2c1b7e12016-05-30 13:57:55 -0500131 board_nand_select_device(nand_info[dev]->priv, dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500132#endif
133
134 return 0;
135}
136
Nishanth Menonb20f8402008-12-13 09:43:06 -0600137#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
138static void print_status(ulong start, ulong end, ulong erasesize, int status)
139{
Joe Hershberger53768562012-08-22 16:49:45 -0500140 /*
141 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
142 * not the same as others. Instead of bit 1 being lock, it is
143 * #lock_tight. To make the driver support either format, ignore bit 1
144 * and use only bit 0 and bit 2.
145 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600146 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
147 start,
148 end - 1,
149 (end - start) / erasesize,
150 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500151 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600152 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
153}
154
Scott Wood08364d92016-05-30 13:57:54 -0500155static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600156{
157 ulong block_start = 0;
158 ulong off;
159 int last_status = -1;
160
Scott Wood17fed142016-05-30 13:57:56 -0500161 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600162 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500163 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600164 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500165 (nand_chip->read_byte(mtd) & 0x80 ?
166 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600167
Scott Wood08364d92016-05-30 13:57:54 -0500168 for (off = 0; off < mtd->size; off += mtd->erasesize) {
169 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600170
171 /* print message only if status has changed */
172 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500173 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600174 last_status);
175 block_start = off;
176 }
177 last_status = s;
178 }
179 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500180 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600181}
182#endif
183
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400184#ifdef CONFIG_ENV_OFFSET_OOB
185unsigned long nand_env_oob_offset;
186
Scott Woode5836dc2010-08-02 13:04:24 -0500187int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400188{
189 int ret;
190 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Scott Wood2c1b7e12016-05-30 13:57:55 -0500191 struct mtd_info *mtd = nand_info[0];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400192 char *cmd = argv[1];
193
Scott Wood84f41482016-09-01 17:31:31 -0500194 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woode5836dc2010-08-02 13:04:24 -0500195 puts("no devices available\n");
196 return 1;
197 }
198
199 set_dev(0);
200
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400201 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500202 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500203 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400204 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500205
206 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400207 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500208 loff_t addr;
209 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400210 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500211 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400212
213 if (argc < 3)
214 goto usage;
215
Tom Rini32d96182013-03-14 05:32:50 +0000216 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200217 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500218 MTD_DEV_TYPE_NAND, nand_info[idx]->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500219 puts("Offset or partition name expected\n");
220 return 1;
221 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200222 if (set_dev(idx)) {
223 puts("Offset or partition name expected\n");
224 return 1;
225 }
Scott Woode5836dc2010-08-02 13:04:24 -0500226
227 if (idx != 0) {
228 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400229 return 1;
230 }
231
Scott Wood08364d92016-05-30 13:57:54 -0500232 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500233 printf("Insufficient available OOB bytes:\n"
234 "%d OOB bytes available but %d required for "
235 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500236 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400237 return 1;
238 }
239
Scott Wood08364d92016-05-30 13:57:54 -0500240 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400241 printf("Environment offset must be block-aligned\n");
242 return 1;
243 }
244
245 ops.datbuf = NULL;
246 ops.mode = MTD_OOB_AUTO;
247 ops.ooboffs = 0;
248 ops.ooblen = ENV_OFFSET_SIZE;
249 ops.oobbuf = (void *) oob_buf;
250
251 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood08364d92016-05-30 13:57:54 -0500252 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400253
Scott Wood08364d92016-05-30 13:57:54 -0500254 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500255 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400256 printf("Error writing OOB block 0\n");
257 return ret;
258 }
Scott Wood0268da02010-07-12 18:17:40 -0500259
Scott Wood08364d92016-05-30 13:57:54 -0500260 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500261 if (ret) {
262 printf("Error reading env offset in OOB\n");
263 return ret;
264 }
265
266 if (addr != nand_env_oob_offset) {
267 printf("Verification of env offset in OOB failed: "
Scott Woode5836dc2010-08-02 13:04:24 -0500268 "0x%08llx expected but got 0x%08lx\n",
269 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500270 return 1;
271 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400272 } else {
273 goto usage;
274 }
275
276 return ret;
277
278usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000279 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400280}
281
282#endif
283
Marek Vasut185ce252011-09-22 03:57:26 +0200284static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100285{
Scott Wood2c1b7e12016-05-30 13:57:55 -0500286 struct mtd_info *mtd = nand_info[idx];
Scott Wood17fed142016-05-30 13:57:56 -0500287 struct nand_chip *chip = mtd_to_nand(mtd);
Marek Vasut185ce252011-09-22 03:57:26 +0200288
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100289 printf("Device %d: ", idx);
290 if (chip->numchips > 1)
291 printf("%dx ", chip->numchips);
292 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500293 mtd->name, mtd->erasesize >> 10);
294 printf(" Page size %8d b\n", mtd->writesize);
295 printf(" OOB size %8d b\n", mtd->oobsize);
296 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocherfb9a9bd2015-04-12 10:18:09 +0200297 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmannd8ea9812017-07-10 08:44:24 +0200298 printf(" options 0x%08x\n", chip->options);
299 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasut185ce252011-09-22 03:57:26 +0200300
301 /* Set geometry info */
Scott Wood08364d92016-05-30 13:57:54 -0500302 setenv_hex("nand_writesize", mtd->writesize);
303 setenv_hex("nand_oobsize", mtd->oobsize);
304 setenv_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100305}
306
Scott Wood08364d92016-05-30 13:57:54 -0500307static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillona7468e22016-06-15 10:42:18 +0200308 ulong count, int read, int no_verify)
Scott Wood733fb022012-03-02 14:01:57 -0600309{
310 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600311
312 while (count--) {
313 /* Raw access */
314 mtd_oob_ops_t ops = {
315 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500316 .oobbuf = ((u8 *)addr) + mtd->writesize,
317 .len = mtd->writesize,
318 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000319 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600320 };
321
Peter Tyser5fa8b982015-02-03 11:58:13 -0600322 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500323 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600324 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500325 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillona7468e22016-06-15 10:42:18 +0200326 if (!ret && !no_verify)
Scott Wood08364d92016-05-30 13:57:54 -0500327 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600328 }
Scott Wood733fb022012-03-02 14:01:57 -0600329
330 if (ret) {
331 printf("%s: error at offset %llx, ret %d\n",
332 __func__, (long long)off, ret);
333 break;
334 }
335
Scott Wood08364d92016-05-30 13:57:54 -0500336 addr += mtd->writesize + mtd->oobsize;
337 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600338 }
339
340 return ret;
341}
342
Harvey Chapman39e68862013-02-26 17:57:14 +0000343/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500344 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000345 */
346static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
347{
348 /* We grab the nand info object here fresh because this is usually
349 * called after arg_off_size() which can change the value of dev.
350 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500351 struct mtd_info *mtd = nand_info[dev];
Harvey Chapman39e68862013-02-26 17:57:14 +0000352 loff_t maxoffset = offset + *size;
353 int badblocks = 0;
354
355 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500356 for (; offset < maxoffset; offset += mtd->erasesize) {
357 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000358 badblocks++;
359 }
360 /* adjust size if any bad blocks found */
361 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500362 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000363 printf("size adjusted to 0x%llx (%d bad blocks)\n",
364 (unsigned long long)*size, badblocks);
365 }
366}
367
Kim Phillipsdc00a682012-10-29 13:34:31 +0000368static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100369{
Scott Woode5836dc2010-08-02 13:04:24 -0500370 int i, ret = 0;
371 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000372 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100373 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500374 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200375#ifdef CONFIG_SYS_NAND_QUIET
376 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200377#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200378 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200379#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200380 const char *quiet_str = getenv("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500381 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500382 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100383
384 /* at least two arguments please */
385 if (argc < 2)
386 goto usage;
387
Stefan Roesed351b2b2006-10-10 12:36:02 +0200388 if (quiet_str)
389 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
390
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100391 cmd = argv[1];
392
Scott Wood67d5a2a2010-08-25 15:24:01 -0500393 /* Only "dump" is repeatable. */
394 if (repeat && strcmp(cmd, "dump"))
395 return 0;
396
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100397 if (strcmp(cmd, "info") == 0) {
398
399 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200400 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Tony Lindgren094d1a82016-08-29 09:11:43 -0700401 if (nand_info[i])
Marek Vasut185ce252011-09-22 03:57:26 +0200402 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100403 }
404 return 0;
405 }
406
407 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100408 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100409 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500410 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100411 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100412 else
Marek Vasut185ce252011-09-22 03:57:26 +0200413 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100414 return 0;
415 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200416
Scott Woode5836dc2010-08-02 13:04:24 -0500417 dev = (int)simple_strtoul(argv[2], NULL, 10);
418 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200419
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100420 return 0;
421 }
422
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400423#ifdef CONFIG_ENV_OFFSET_OOB
424 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500425 if (strcmp(cmd, "env.oob") == 0)
426 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400427#endif
428
Scott Woode5836dc2010-08-02 13:04:24 -0500429 /* The following commands operate on the current device, unless
430 * overridden by a partition specifier. Note that if somehow the
431 * current device is invalid, it will have to be changed to a valid
432 * one before these commands can run, even if a partition specifier
433 * for another device is to be used.
434 */
435 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
Tony Lindgren094d1a82016-08-29 09:11:43 -0700436 !nand_info[dev]) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100437 puts("\nno devices available\n");
438 return 1;
439 }
Scott Wood2c1b7e12016-05-30 13:57:55 -0500440 mtd = nand_info[dev];
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100441
442 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500443 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500444 for (off = 0; off < mtd->size; off += mtd->erasesize)
445 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500446 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100447 return 0;
448 }
449
Stefan Roese198b23e2006-10-28 15:55:52 +0200450 /*
451 * Syntax is:
452 * 0 1 2 3 4
453 * nand erase [clean] [off size]
454 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500455 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200456 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200457 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100458 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200459 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
460 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500461 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500462 int spread = 0;
463 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200464 const char *scrub_warn =
465 "Warning: "
466 "scrub option will erase all factory set bad blocks!\n"
467 " "
468 "There is no reliable way to recover them.\n"
469 " "
470 "Use this command only for testing purposes if you\n"
471 " "
472 "are sure of what you are doing!\n"
473 "\nReally scrub this NAND flash? <y/N>\n";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200474
Scott Wood1b5cd512010-08-25 14:43:29 -0500475 if (cmd[5] != 0) {
476 if (!strcmp(&cmd[5], ".spread")) {
477 spread = 1;
478 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500479 args = 1;
480 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500481 args = 0;
482 } else {
483 goto usage;
484 }
485 }
486
487 /*
488 * Don't allow missing arguments to cause full chip/partition
489 * erases -- easy to do accidentally, e.g. with a misspelled
490 * variable name.
491 */
492 if (argc != o + args)
493 goto usage;
494
495 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200496 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200497 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
498 &maxsize, MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500499 nand_info[dev]->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200500 return 1;
501
502 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200503 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200504
Scott Wood2c1b7e12016-05-30 13:57:55 -0500505 mtd = nand_info[dev];
Scott Woode5836dc2010-08-02 13:04:24 -0500506
Stefan Roesed351b2b2006-10-10 12:36:02 +0200507 memset(&opts, 0, sizeof(opts));
508 opts.offset = off;
509 opts.length = size;
510 opts.jffs2 = clean;
511 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500512 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200513
514 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200515 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200516 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200517 } else {
518 puts(scrub_warn);
519 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100520 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200521 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100522 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900523 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100524 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200525 }
526 }
Scott Wood08364d92016-05-30 13:57:54 -0500527 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100528 printf("%s\n", ret ? "ERROR" : "OK");
529
530 return ret == 0 ? 0 : 1;
531 }
532
533 if (strncmp(cmd, "dump", 4) == 0) {
534 if (argc < 3)
535 goto usage;
536
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100537 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood08364d92016-05-30 13:57:54 -0500538 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100539
540 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100541 }
542
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100543 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500544 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600545 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200546 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000547 int raw = 0;
Boris Brezillona7468e22016-06-15 10:42:18 +0200548 int no_verify = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200549
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100550 if (argc < 4)
551 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200552
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100553 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
554
Stefan Roesed351b2b2006-10-10 12:36:02 +0200555 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200556 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100557
Stefan Roesed351b2b2006-10-10 12:36:02 +0200558 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600559
Boris Brezillona7468e22016-06-15 10:42:18 +0200560 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood733fb022012-03-02 14:01:57 -0600561 raw = 1;
562
Boris Brezillona7468e22016-06-15 10:42:18 +0200563 if (!strcmp(s, ".raw.noverify"))
564 no_verify = 1;
565
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200566 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
567 MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500568 nand_info[dev]->size))
Scott Wood733fb022012-03-02 14:01:57 -0600569 return 1;
570
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200571 if (set_dev(dev))
572 return 1;
573
Scott Wood2c1b7e12016-05-30 13:57:55 -0500574 mtd = nand_info[dev];
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200575
Scott Wood733fb022012-03-02 14:01:57 -0600576 if (argc > 4 && !str2long(argv[4], &pagecount)) {
577 printf("'%s' is not a number\n", argv[4]);
578 return 1;
579 }
580
Scott Wood08364d92016-05-30 13:57:54 -0500581 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600582 puts("Size exceeds partition or device limit\n");
583 return -1;
584 }
585
Scott Wood08364d92016-05-30 13:57:54 -0500586 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600587 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200588 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
589 &size, &maxsize,
590 MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500591 nand_info[dev]->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200592 return 1;
593
594 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600595 return 1;
596
Harvey Chapman39e68862013-02-26 17:57:14 +0000597 /* size is unspecified */
598 if (argc < 5)
599 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600600 rwsize = size;
601 }
602
Scott Wood2c1b7e12016-05-30 13:57:55 -0500603 mtd = nand_info[dev];
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200604
Scott Wood12b1c952008-06-12 13:13:23 -0500605 if (!s || !strcmp(s, ".jffs2") ||
606 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500607 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500608 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000609 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200610 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500611 else
Scott Wood08364d92016-05-30 13:57:54 -0500612 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000613 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600614 (u_char *)addr,
615 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400616#ifdef CONFIG_CMD_NAND_TRIMFFS
617 } else if (!strcmp(s, ".trimffs")) {
618 if (read) {
619 printf("Unknown nand command suffix '%s'\n", s);
620 return 1;
621 }
Scott Wood08364d92016-05-30 13:57:54 -0500622 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000623 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600624 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400625#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400626 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100627 /* out-of-band data */
628 mtd_oob_ops_t ops = {
629 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500630 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000631 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100632 };
633
Harald Welte5a395102007-12-19 14:12:53 +0100634 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500635 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100636 else
Scott Wood08364d92016-05-30 13:57:54 -0500637 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600638 } else if (raw) {
Boris Brezillona7468e22016-06-15 10:42:18 +0200639 ret = raw_access(mtd, addr, off, pagecount, read,
640 no_verify);
Stefan Roese198b23e2006-10-28 15:55:52 +0200641 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500642 printf("Unknown nand command suffix '%s'.\n", s);
643 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200644 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100645
Scott Woode5836dc2010-08-02 13:04:24 -0500646 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200647 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100648
649 return ret == 0 ? 0 : 1;
650 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200651
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100652#ifdef CONFIG_CMD_NAND_TORTURE
653 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200654 loff_t endoff;
655 unsigned int failed = 0, passed = 0;
656
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100657 if (argc < 3)
658 goto usage;
659
660 if (!str2off(argv[2], &off)) {
661 puts("Offset is not a valid number\n");
662 return 1;
663 }
664
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200665 size = mtd->erasesize;
666 if (argc > 3) {
667 if (!str2off(argv[3], &size)) {
668 puts("Size is not a valid number\n");
669 return 1;
670 }
671 }
672
673 endoff = off + size;
674 if (endoff > mtd->size) {
675 puts("Arguments beyond end of NAND\n");
676 return 1;
677 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100678
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200679 off = round_down(off, mtd->erasesize);
680 endoff = round_up(endoff, mtd->erasesize);
681 size = endoff - off;
682 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
683 dev, off, size, mtd->erasesize);
684 while (off < endoff) {
685 ret = nand_torture(mtd, off);
686 if (ret) {
687 failed++;
688 printf(" block at 0x%llx failed\n", off);
689 } else {
690 passed++;
691 }
692 off += mtd->erasesize;
693 }
694 printf(" Passed: %u, failed: %u\n", passed, failed);
695 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100696 }
697#endif
698
Stefan Roesed351b2b2006-10-10 12:36:02 +0200699 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200700 argc -= 2;
701 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200702
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200703 if (argc <= 0)
704 goto usage;
705
706 while (argc > 0) {
707 addr = simple_strtoul(*argv, NULL, 16);
708
Scott Wood08364d92016-05-30 13:57:54 -0500709 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200710 printf("block 0x%08lx NOT marked "
711 "as bad! ERROR %d\n",
712 addr, ret);
713 ret = 1;
714 } else {
715 printf("block 0x%08lx successfully "
716 "marked as bad\n",
717 addr);
718 }
719 --argc;
720 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200721 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200722 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200723 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500724
Stefan Roesed351b2b2006-10-10 12:36:02 +0200725 if (strcmp(cmd, "biterr") == 0) {
726 /* todo */
727 return 1;
728 }
729
Nishanth Menonb20f8402008-12-13 09:43:06 -0600730#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200731 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600732 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200733 int status = 0;
734 if (argc == 3) {
735 if (!strcmp("tight", argv[2]))
736 tight = 1;
737 if (!strcmp("status", argv[2]))
738 status = 1;
739 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200740 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500741 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100742 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500743 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100744 puts("NAND flash successfully locked\n");
745 } else {
746 puts("Error locking NAND flash\n");
747 return 1;
748 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200749 }
750 return 0;
751 }
752
Joe Hershbergercccf5952012-08-22 16:49:42 -0500753 if (strncmp(cmd, "unlock", 5) == 0) {
754 int allexcept = 0;
755
756 s = strchr(cmd, '.');
757
758 if (s && !strcmp(s, ".allexcept"))
759 allexcept = 1;
760
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200761 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
762 &maxsize, MTD_DEV_TYPE_NAND,
Scott Wood2c1b7e12016-05-30 13:57:55 -0500763 nand_info[dev]->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200764 return 1;
765
766 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200767 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200768
Scott Wood2c1b7e12016-05-30 13:57:55 -0500769 if (!nand_unlock(nand_info[dev], off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100770 puts("NAND flash successfully unlocked\n");
771 } else {
772 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100773 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100774 return 1;
775 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200776 return 0;
777 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600778#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200779
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100780usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000781 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100782}
783
Kim Phillipsdc00a682012-10-29 13:34:31 +0000784#ifdef CONFIG_SYS_LONGHELP
785static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200786 "info - show available NAND devices\n"
787 "nand device [dev] - show or set current device\n"
788 "nand read - addr off|partition size\n"
789 "nand write - addr off|partition size\n"
790 " read/write 'size' bytes starting at offset 'off'\n"
791 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600792 "nand read.raw - addr off|partition [count]\n"
Boris Brezillona7468e22016-06-15 10:42:18 +0200793 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood733fb022012-03-02 14:01:57 -0600794 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400795#ifdef CONFIG_CMD_NAND_TRIMFFS
796 "nand write.trimffs - addr off|partition size\n"
797 " write 'size' bytes starting at offset 'off' from memory address\n"
798 " 'addr', skipping bad blocks and dropping any pages at the end\n"
799 " of eraseblocks that contain only 0xFF\n"
800#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200801 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500802 "from offset 'off'\n"
803 " With '.spread', erase enough for given file size, otherwise,\n"
804 " 'size' includes skipped bad blocks.\n"
805 "nand erase.part [clean] partition - erase entire mtd partition'\n"
806 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200807 "nand bad - show bad blocks\n"
808 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100809#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200810 "nand torture off - torture one block at offset\n"
811 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100812#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200813 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500814 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200815 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
816 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600817#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200818 "\n"
819 "nand lock [tight] [status]\n"
820 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500821 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600822#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400823#ifdef CONFIG_ENV_OFFSET_OOB
824 "\n"
825 "nand env.oob - environment offset in OOB of block 0 of"
826 " first device.\n"
827 "nand env.oob set off|partition - set enviromnent offset\n"
828 "nand env.oob get - get environment offset"
829#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000830 "";
831#endif
832
833U_BOOT_CMD(
834 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
835 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600836);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100837
Scott Wood08364d92016-05-30 13:57:54 -0500838static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200839 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100840{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100841 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000842 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200843 size_t cnt;
Heiko Schocher515eb122014-05-28 11:33:33 +0200844#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100845 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200846#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100847#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200848 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100849#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200850
851 s = strchr(cmd, '.');
852 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500853 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500854 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000855 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500856 return 1;
857 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100858
Scott Wood08364d92016-05-30 13:57:54 -0500859 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100860
Scott Wood08364d92016-05-30 13:57:54 -0500861 cnt = mtd->writesize;
862 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
863 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100864 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200865 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000866 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100867 return 1;
868 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000869 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100870
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100871 switch (genimg_get_format ((void *)addr)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200872#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100873 case IMAGE_FORMAT_LEGACY:
874 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100875
Simon Glass0169e6b2012-02-13 13:51:18 +0000876 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100877 image_print_contents (hdr);
878
879 cnt = image_get_image_size (hdr);
880 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200881#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100882#if defined(CONFIG_FIT)
883 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100884 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100885 puts ("Fit image detected...\n");
886
887 cnt = fit_get_size (fit_hdr);
888 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100889#endif
890 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000891 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100892 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100893 return 1;
894 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000895 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100896
Scott Wood08364d92016-05-30 13:57:54 -0500897 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
898 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100899 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200900 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000901 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100902 return 1;
903 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000904 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100905
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100906#if defined(CONFIG_FIT)
907 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200908 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
909 if (!fit_check_format (fit_hdr)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000910 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200911 puts ("** Bad FIT image format\n");
912 return 1;
913 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000914 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200915 fit_print_contents (fit_hdr);
916 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100917#endif
918
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100919 /* Loading ok, update default load address */
920
921 load_addr = addr;
922
Mike Frysinger194c2e82011-06-05 13:43:02 +0000923 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100924}
925
Kim Phillipsdc00a682012-10-29 13:34:31 +0000926static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
927 char * const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +0200928{
929 char *boot_device = NULL;
930 int idx;
931 ulong addr, offset = 0;
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200932#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200933 struct mtd_device *dev;
934 struct part_info *part;
935 u8 pnum;
936
937 if (argc >= 2) {
938 char *p = (argc == 2) ? argv[1] : argv[2];
939 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
940 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
941 if (dev->id->type != MTD_DEV_TYPE_NAND) {
942 puts("Not a NAND device\n");
943 return 1;
944 }
945 if (argc > 3)
946 goto usage;
947 if (argc == 3)
Thomas Knobloche84b7772007-07-06 14:58:39 +0200948 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200949 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200950 addr = CONFIG_SYS_LOAD_ADDR;
Scott Wood2c1b7e12016-05-30 13:57:55 -0500951 return nand_load_image(cmdtp, nand_info[dev->id->num],
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200952 part->offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200953 }
954 }
955#endif
956
Simon Glass0169e6b2012-02-13 13:51:18 +0000957 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +0200958 switch (argc) {
959 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200960 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese198b23e2006-10-28 15:55:52 +0200961 boot_device = getenv("bootdevice");
962 break;
963 case 2:
964 addr = simple_strtoul(argv[1], NULL, 16);
965 boot_device = getenv("bootdevice");
966 break;
967 case 3:
968 addr = simple_strtoul(argv[1], NULL, 16);
969 boot_device = argv[2];
970 break;
971 case 4:
972 addr = simple_strtoul(argv[1], NULL, 16);
973 boot_device = argv[2];
974 offset = simple_strtoul(argv[3], NULL, 16);
975 break;
976 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +0200977#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +0200978usage:
979#endif
Simon Glass0169e6b2012-02-13 13:51:18 +0000980 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +0000981 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +0200982 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000983 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +0200984
985 if (!boot_device) {
986 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000987 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200988 return 1;
989 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000990 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100991
Stefan Roese198b23e2006-10-28 15:55:52 +0200992 idx = simple_strtoul(boot_device, NULL, 16);
993
Tony Lindgren094d1a82016-08-29 09:11:43 -0700994 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx]) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200995 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +0000996 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +0200997 return 1;
998 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000999 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001000
Scott Wood2c1b7e12016-05-30 13:57:55 -05001001 return nand_load_image(cmdtp, nand_info[idx], offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001002}
1003
1004U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001005 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001006 "[partition] | [[[loadAddr] dev] offset]"
1007);