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