blob: 9a723f5757955acc1d9d9177e18ebe501ecd41ec [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.
Balamanikandan Gunasundard0460352021-06-29 12:46:11 +053020 *
21 * The function nand_biterror() in this file is inspired from
22 * mtd-utils/nand-utils/nandflipbits.c which was released under GPLv2
23 * only
wdenkc8434db2003-03-26 06:55:25 +000024 */
25
26#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060027#include <bootstage.h>
Simon Glass85f13782019-12-28 10:45:03 -070028#include <image.h>
Simon Glass274e0b02020-05-10 11:39:56 -060029#include <asm/cache.h>
William Juul52c07962007-10-31 13:53:06 +010030#include <linux/mtd/mtd.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040031#include <linux/mtd/rawnand.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010032#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070033#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060034#include <env.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010035#include <watchdog.h>
36#include <malloc.h>
37#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010038#include <jffs2/jffs2.h>
39#include <nand.h>
40
Miquel Raynal617fd792019-10-25 19:39:29 +020041#include "legacy-mtd-utils.h"
42
Ladislav Michl9d0928d2009-04-21 02:26:31 +020043#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +020044
Wolfgang Denk161bfc52009-11-17 21:27:39 +010045/* partition handling routines */
Stefan Roese198b23e2006-10-28 15:55:52 +020046int mtdparts_init(void);
Stefan Roese198b23e2006-10-28 15:55:52 +020047int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020048 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020049#endif
50
Balamanikandan Gunasundard0460352021-06-29 12:46:11 +053051#define MAX_NUM_PAGES 64
52
53static int nand_biterror(struct mtd_info *mtd, ulong off, int bit)
54{
55 int ret = 0;
56 int page = 0;
57 ulong block_off;
58 u_char *datbuf[MAX_NUM_PAGES]; /* Data and OOB */
59 u_char data;
60 int pages_per_blk = mtd->erasesize / mtd->writesize;
61 struct erase_info einfo;
62
63 if (pages_per_blk > MAX_NUM_PAGES) {
64 printf("Too many pages in one erase block\n");
65 return 1;
66 }
67
68 if (bit < 0 || bit > 7) {
69 printf("bit position 0 to 7 is allowed\n");
70 return 1;
71 }
72
73 /* Allocate memory */
74 memset(datbuf, 0, sizeof(datbuf));
75 for (page = 0; page < pages_per_blk ; page++) {
76 datbuf[page] = malloc(mtd->writesize + mtd->oobsize);
77 if (!datbuf[page]) {
78 printf("No memory for page buffer\n");
79 ret = -ENOMEM;
80 goto free_memory;
81 }
82 }
83
84 /* Align to erase block boundary */
85 block_off = off & (~(mtd->erasesize - 1));
86
87 /* Read out memory as first step */
88 for (page = 0; page < pages_per_blk ; page++) {
89 struct mtd_oob_ops ops;
90 loff_t addr = (loff_t)block_off;
91
92 memset(&ops, 0, sizeof(ops));
93 ops.datbuf = datbuf[page];
94 ops.oobbuf = datbuf[page] + mtd->writesize;
95 ops.len = mtd->writesize;
96 ops.ooblen = mtd->oobsize;
97 ops.mode = MTD_OPS_RAW;
98 ret = mtd_read_oob(mtd, addr, &ops);
99 if (ret < 0) {
100 printf("Error (%d) reading page %08lx\n",
101 ret, block_off);
102 ret = 1;
103 goto free_memory;
104 }
105 block_off += mtd->writesize;
106 }
107
108 /* Erase the block */
109 memset(&einfo, 0, sizeof(einfo));
110 einfo.mtd = mtd;
111 /* Align to erase block boundary */
112 einfo.addr = (loff_t)(off & (~(mtd->erasesize - 1)));
113 einfo.len = mtd->erasesize;
114 ret = mtd_erase(mtd, &einfo);
115 if (ret < 0) {
116 printf("Error (%d) nand_erase_nand page %08llx\n",
117 ret, einfo.addr);
118 ret = 1;
119 goto free_memory;
120 }
121
122 /* Twist a bit in data part */
123 block_off = off & (mtd->erasesize - 1);
124 data = datbuf[block_off / mtd->writesize][block_off % mtd->writesize];
125 data ^= (1 << bit);
126 datbuf[block_off / mtd->writesize][block_off % mtd->writesize] = data;
127
128 printf("Flip data at 0x%lx with xor 0x%02x (bit=%d) to value=0x%02x\n",
129 off, (1 << bit), bit, data);
130
131 /* Write back twisted data and unmodified OOB */
132 /* Align to erase block boundary */
133 block_off = off & (~(mtd->erasesize - 1));
134 for (page = 0; page < pages_per_blk; page++) {
135 struct mtd_oob_ops ops;
136 loff_t addr = (loff_t)block_off;
137
138 memset(&ops, 0, sizeof(ops));
139 ops.datbuf = datbuf[page];
140 ops.oobbuf = datbuf[page] + mtd->writesize;
141 ops.len = mtd->writesize;
142 ops.ooblen = mtd->oobsize;
143 ops.mode = MTD_OPS_RAW;
144 ret = mtd_write_oob(mtd, addr, &ops);
145 if (ret < 0) {
146 printf("Error (%d) write page %08lx\n", ret, block_off);
147 ret = 1;
148 goto free_memory;
149 }
150 block_off += mtd->writesize;
151 }
152
153free_memory:
154 for (page = 0; page < pages_per_blk ; page++) {
155 if (datbuf[page])
156 free(datbuf[page]);
157 }
158 return ret;
159}
160
Scott Wood08364d92016-05-30 13:57:54 -0500161static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
162 int repeat)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100163{
164 int i;
William Juule7ebe6e2007-12-14 16:33:45 +0100165 u_char *datbuf, *oobbuf, *p;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500166 static loff_t last;
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900167 int ret = 0;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500168
169 if (repeat)
Scott Wood08364d92016-05-30 13:57:54 -0500170 off = last + mtd->writesize;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500171
172 last = off;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100173
Scott Wood08364d92016-05-30 13:57:54 -0500174 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900175 if (!datbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100176 puts("No memory for page buffer\n");
177 return 1;
178 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900179
Scott Wood08364d92016-05-30 13:57:54 -0500180 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900181 if (!oobbuf) {
182 puts("No memory for page buffer\n");
183 ret = 1;
184 goto free_dat;
185 }
Scott Wood08364d92016-05-30 13:57:54 -0500186 off &= ~(mtd->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +0100187 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-12-14 16:33:45 +0100188 struct mtd_oob_ops ops;
189 memset(&ops, 0, sizeof(ops));
190 ops.datbuf = datbuf;
Tom Rini2d2e9752012-02-23 15:47:46 -0700191 ops.oobbuf = oobbuf;
Scott Wood08364d92016-05-30 13:57:54 -0500192 ops.len = mtd->writesize;
193 ops.ooblen = mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000194 ops.mode = MTD_OPS_RAW;
Scott Wood08364d92016-05-30 13:57:54 -0500195 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100196 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +0200197 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900198 ret = 1;
199 goto free_all;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100200 }
Stefan Roese3167d072008-07-10 10:10:54 +0200201 printf("Page %08lx dump:\n", off);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200202
Masahiro Yamada3f97e272013-07-11 17:27:13 +0900203 if (!only_oob) {
Scott Wood08364d92016-05-30 13:57:54 -0500204 i = mtd->writesize >> 4;
Masahiro Yamada3f97e272013-07-11 17:27:13 +0900205 p = datbuf;
206
207 while (i--) {
William Juule7ebe6e2007-12-14 16:33:45 +0100208 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
209 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
210 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Woodcc5f3392008-06-12 13:20:16 -0500211 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
212 p[15]);
Masahiro Yamada3f97e272013-07-11 17:27:13 +0900213 p += 16;
214 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100215 }
Masahiro Yamada3f97e272013-07-11 17:27:13 +0900216
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100217 puts("OOB:\n");
Scott Wood08364d92016-05-30 13:57:54 -0500218 i = mtd->oobsize >> 3;
Tom Rini2d2e9752012-02-23 15:47:46 -0700219 p = oobbuf;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100220 while (i--) {
William Juul52c07962007-10-31 13:53:06 +0100221 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
222 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100223 p += 8;
224 }
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900225
226free_all:
William Juule7ebe6e2007-12-14 16:33:45 +0100227 free(oobbuf);
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900228free_dat:
229 free(datbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100230
Masahiro Yamada756e51c2013-07-11 17:27:12 +0900231 return ret;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100232}
233
234/* ------------------------------------------------------------------------- */
235
Scott Woode5836dc2010-08-02 13:04:24 -0500236static int set_dev(int dev)
237{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500238 struct mtd_info *mtd = get_nand_dev_by_index(dev);
239
240 if (!mtd)
241 return -ENODEV;
Scott Woode5836dc2010-08-02 13:04:24 -0500242
243 if (nand_curr_device == dev)
244 return 0;
245
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500246 printf("Device %d: %s", dev, mtd->name);
Scott Woode5836dc2010-08-02 13:04:24 -0500247 puts("... is now current device\n");
248 nand_curr_device = dev;
249
250#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashkocd3ffa12017-06-26 19:12:58 -0500251 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500252#endif
253
254 return 0;
255}
256
Nishanth Menonb20f8402008-12-13 09:43:06 -0600257#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
258static void print_status(ulong start, ulong end, ulong erasesize, int status)
259{
Joe Hershberger53768562012-08-22 16:49:45 -0500260 /*
261 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
262 * not the same as others. Instead of bit 1 being lock, it is
263 * #lock_tight. To make the driver support either format, ignore bit 1
264 * and use only bit 0 and bit 2.
265 */
Nishanth Menonb20f8402008-12-13 09:43:06 -0600266 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
267 start,
268 end - 1,
269 (end - start) / erasesize,
270 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershberger53768562012-08-22 16:49:45 -0500271 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menonb20f8402008-12-13 09:43:06 -0600272 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
273}
274
Scott Wood08364d92016-05-30 13:57:54 -0500275static void do_nand_status(struct mtd_info *mtd)
Nishanth Menonb20f8402008-12-13 09:43:06 -0600276{
277 ulong block_start = 0;
278 ulong off;
279 int last_status = -1;
280
Scott Wood17fed142016-05-30 13:57:56 -0500281 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600282 /* check the WP bit */
Scott Wood08364d92016-05-30 13:57:54 -0500283 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600284 printf("device is %swrite protected\n",
Scott Wood08364d92016-05-30 13:57:54 -0500285 (nand_chip->read_byte(mtd) & 0x80 ?
286 "NOT " : ""));
Nishanth Menonb20f8402008-12-13 09:43:06 -0600287
Scott Wood08364d92016-05-30 13:57:54 -0500288 for (off = 0; off < mtd->size; off += mtd->erasesize) {
289 int s = nand_get_lock_status(mtd, off);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600290
291 /* print message only if status has changed */
292 if (s != last_status && off != 0) {
Scott Wood08364d92016-05-30 13:57:54 -0500293 print_status(block_start, off, mtd->erasesize,
Nishanth Menonb20f8402008-12-13 09:43:06 -0600294 last_status);
295 block_start = off;
296 }
297 last_status = s;
298 }
299 /* Print the last block info */
Scott Wood08364d92016-05-30 13:57:54 -0500300 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menonb20f8402008-12-13 09:43:06 -0600301}
302#endif
303
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400304#ifdef CONFIG_ENV_OFFSET_OOB
305unsigned long nand_env_oob_offset;
306
Simon Glassed38aef2020-05-10 11:40:03 -0600307int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400308{
309 int ret;
310 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500311 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400312 char *cmd = argv[1];
313
Scott Wood84f41482016-09-01 17:31:31 -0500314 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woode5836dc2010-08-02 13:04:24 -0500315 puts("no devices available\n");
316 return 1;
317 }
318
319 set_dev(0);
320
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400321 if (!strcmp(cmd, "get")) {
Scott Wood08364d92016-05-30 13:57:54 -0500322 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500323 if (ret)
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400324 return 1;
Scott Wood0268da02010-07-12 18:17:40 -0500325
326 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400327 } else if (!strcmp(cmd, "set")) {
Scott Woode5836dc2010-08-02 13:04:24 -0500328 loff_t addr;
329 loff_t maxsize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400330 struct mtd_oob_ops ops;
Scott Woode5836dc2010-08-02 13:04:24 -0500331 int idx = 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400332
333 if (argc < 3)
334 goto usage;
335
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500336 mtd = get_nand_dev_by_index(idx);
Tom Rini32d96182013-03-14 05:32:50 +0000337 /* We don't care about size, or maxsize. */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200338 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500339 MTD_DEV_TYPE_NAND, mtd->size)) {
Scott Woode5836dc2010-08-02 13:04:24 -0500340 puts("Offset or partition name expected\n");
341 return 1;
342 }
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200343 if (set_dev(idx)) {
344 puts("Offset or partition name expected\n");
345 return 1;
346 }
Scott Woode5836dc2010-08-02 13:04:24 -0500347
348 if (idx != 0) {
349 puts("Partition not on first NAND device\n");
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400350 return 1;
351 }
352
Scott Wood08364d92016-05-30 13:57:54 -0500353 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood0268da02010-07-12 18:17:40 -0500354 printf("Insufficient available OOB bytes:\n"
355 "%d OOB bytes available but %d required for "
356 "env.oob support\n",
Scott Wood08364d92016-05-30 13:57:54 -0500357 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400358 return 1;
359 }
360
Scott Wood08364d92016-05-30 13:57:54 -0500361 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400362 printf("Environment offset must be block-aligned\n");
363 return 1;
364 }
365
366 ops.datbuf = NULL;
367 ops.mode = MTD_OOB_AUTO;
368 ops.ooboffs = 0;
369 ops.ooblen = ENV_OFFSET_SIZE;
370 ops.oobbuf = (void *) oob_buf;
371
372 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood08364d92016-05-30 13:57:54 -0500373 oob_buf[1] = addr / mtd->erasesize;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400374
Scott Wood08364d92016-05-30 13:57:54 -0500375 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500376 if (ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400377 printf("Error writing OOB block 0\n");
378 return ret;
379 }
Scott Wood0268da02010-07-12 18:17:40 -0500380
Scott Wood08364d92016-05-30 13:57:54 -0500381 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500382 if (ret) {
383 printf("Error reading env offset in OOB\n");
384 return ret;
385 }
386
387 if (addr != nand_env_oob_offset) {
388 printf("Verification of env offset in OOB failed: "
Scott Woode5836dc2010-08-02 13:04:24 -0500389 "0x%08llx expected but got 0x%08lx\n",
390 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood0268da02010-07-12 18:17:40 -0500391 return 1;
392 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400393 } else {
394 goto usage;
395 }
396
397 return ret;
398
399usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000400 return CMD_RET_USAGE;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400401}
402
403#endif
404
Marek Vasut185ce252011-09-22 03:57:26 +0200405static void nand_print_and_set_info(int idx)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100406{
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500407 struct mtd_info *mtd;
408 struct nand_chip *chip;
409
410 mtd = get_nand_dev_by_index(idx);
411 if (!mtd)
412 return;
Marek Vasut185ce252011-09-22 03:57:26 +0200413
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500414 chip = mtd_to_nand(mtd);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100415 printf("Device %d: ", idx);
416 if (chip->numchips > 1)
417 printf("%dx ", chip->numchips);
418 printf("%s, sector size %u KiB\n",
Scott Wood08364d92016-05-30 13:57:54 -0500419 mtd->name, mtd->erasesize >> 10);
Michael Trimarchi14e425d2022-09-22 15:39:37 +0200420 printf(" Page size %8d b\n", mtd->writesize);
421 printf(" OOB size %8d b\n", mtd->oobsize);
422 printf(" Erase size %8d b\n", mtd->erasesize);
423 printf(" ecc strength %8d bits\n", mtd->ecc_strength);
424 printf(" ecc step size %8d b\n", mtd->ecc_step_size);
425 printf(" subpagesize %8d b\n", chip->subpagesize);
426 printf(" options 0x%08x\n", chip->options);
427 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasut185ce252011-09-22 03:57:26 +0200428
429 /* Set geometry info */
Simon Glass4d949a22017-08-03 12:22:10 -0600430 env_set_hex("nand_writesize", mtd->writesize);
431 env_set_hex("nand_oobsize", mtd->oobsize);
432 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100433}
434
Scott Wood08364d92016-05-30 13:57:54 -0500435static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillona7468e22016-06-15 10:42:18 +0200436 ulong count, int read, int no_verify)
Scott Wood733fb022012-03-02 14:01:57 -0600437{
438 int ret = 0;
Scott Wood733fb022012-03-02 14:01:57 -0600439
440 while (count--) {
441 /* Raw access */
442 mtd_oob_ops_t ops = {
443 .datbuf = (u8 *)addr,
Scott Wood08364d92016-05-30 13:57:54 -0500444 .oobbuf = ((u8 *)addr) + mtd->writesize,
445 .len = mtd->writesize,
446 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000447 .mode = MTD_OPS_RAW
Scott Wood733fb022012-03-02 14:01:57 -0600448 };
449
Peter Tyser5fa8b982015-02-03 11:58:13 -0600450 if (read) {
Scott Wood08364d92016-05-30 13:57:54 -0500451 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600452 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500453 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillona7468e22016-06-15 10:42:18 +0200454 if (!ret && !no_verify)
Scott Wood08364d92016-05-30 13:57:54 -0500455 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser5fa8b982015-02-03 11:58:13 -0600456 }
Scott Wood733fb022012-03-02 14:01:57 -0600457
458 if (ret) {
459 printf("%s: error at offset %llx, ret %d\n",
460 __func__, (long long)off, ret);
461 break;
462 }
463
Scott Wood08364d92016-05-30 13:57:54 -0500464 addr += mtd->writesize + mtd->oobsize;
465 off += mtd->writesize;
Scott Wood733fb022012-03-02 14:01:57 -0600466 }
467
468 return ret;
469}
470
Harvey Chapman39e68862013-02-26 17:57:14 +0000471/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood7fdf3562013-06-20 12:45:31 -0500472 * read/write past the end of a chip/partition by accident.
Harvey Chapman39e68862013-02-26 17:57:14 +0000473 */
474static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
475{
476 /* We grab the nand info object here fresh because this is usually
477 * called after arg_off_size() which can change the value of dev.
478 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500479 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapman39e68862013-02-26 17:57:14 +0000480 loff_t maxoffset = offset + *size;
481 int badblocks = 0;
482
483 /* count badblocks in NAND from offset to offset + size */
Scott Wood08364d92016-05-30 13:57:54 -0500484 for (; offset < maxoffset; offset += mtd->erasesize) {
485 if (nand_block_isbad(mtd, offset))
Harvey Chapman39e68862013-02-26 17:57:14 +0000486 badblocks++;
487 }
488 /* adjust size if any bad blocks found */
489 if (badblocks) {
Scott Wood08364d92016-05-30 13:57:54 -0500490 *size -= badblocks * mtd->erasesize;
Harvey Chapman39e68862013-02-26 17:57:14 +0000491 printf("size adjusted to 0x%llx (%d bad blocks)\n",
492 (unsigned long long)*size, badblocks);
493 }
494}
495
Simon Glassed38aef2020-05-10 11:40:03 -0600496static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
497 char *const argv[])
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100498{
Scott Woode5836dc2010-08-02 13:04:24 -0500499 int i, ret = 0;
500 ulong addr;
Tom Rini32d96182013-03-14 05:32:50 +0000501 loff_t off, size, maxsize;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100502 char *cmd, *s;
Scott Wood08364d92016-05-30 13:57:54 -0500503 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200504#ifdef CONFIG_SYS_NAND_QUIET
505 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200506#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200507 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200508#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600509 const char *quiet_str = env_get("quiet");
Scott Woode5836dc2010-08-02 13:04:24 -0500510 int dev = nand_curr_device;
Scott Wood67d5a2a2010-08-25 15:24:01 -0500511 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100512
513 /* at least two arguments please */
514 if (argc < 2)
515 goto usage;
516
Stefan Roesed351b2b2006-10-10 12:36:02 +0200517 if (quiet_str)
518 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
519
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100520 cmd = argv[1];
521
Scott Wood67d5a2a2010-08-25 15:24:01 -0500522 /* Only "dump" is repeatable. */
523 if (repeat && strcmp(cmd, "dump"))
524 return 0;
525
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100526 if (strcmp(cmd, "info") == 0) {
527
528 putc('\n');
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500529 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
530 nand_print_and_set_info(i);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100531 return 0;
532 }
533
534 if (strcmp(cmd, "device") == 0) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100535 if (argc < 3) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100536 putc('\n');
Scott Woode5836dc2010-08-02 13:04:24 -0500537 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100538 puts("no devices available\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100539 else
Marek Vasut185ce252011-09-22 03:57:26 +0200540 nand_print_and_set_info(dev);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100541 return 0;
542 }
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200543
Simon Glassff9b9032021-07-24 09:03:30 -0600544 dev = (int)dectoul(argv[2], NULL);
Scott Woode5836dc2010-08-02 13:04:24 -0500545 set_dev(dev);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200546
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100547 return 0;
548 }
549
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400550#ifdef CONFIG_ENV_OFFSET_OOB
551 /* this command operates only on the first nand device */
Scott Woode5836dc2010-08-02 13:04:24 -0500552 if (strcmp(cmd, "env.oob") == 0)
553 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400554#endif
555
Scott Woode5836dc2010-08-02 13:04:24 -0500556 /* The following commands operate on the current device, unless
557 * overridden by a partition specifier. Note that if somehow the
558 * current device is invalid, it will have to be changed to a valid
559 * one before these commands can run, even if a partition specifier
560 * for another device is to be used.
561 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500562 mtd = get_nand_dev_by_index(dev);
563 if (!mtd) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100564 puts("\nno devices available\n");
565 return 1;
566 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100567
568 if (strcmp(cmd, "bad") == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500569 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood08364d92016-05-30 13:57:54 -0500570 for (off = 0; off < mtd->size; off += mtd->erasesize)
571 if (nand_block_isbad(mtd, off))
Scott Woode5836dc2010-08-02 13:04:24 -0500572 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100573 return 0;
574 }
575
Stefan Roese198b23e2006-10-28 15:55:52 +0200576 /*
577 * Syntax is:
578 * 0 1 2 3 4
579 * nand erase [clean] [off size]
580 */
Scott Wood1b5cd512010-08-25 14:43:29 -0500581 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200582 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200583 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100584 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasuta9869812011-09-14 00:20:35 +0200585 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
586 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood1b5cd512010-08-25 14:43:29 -0500587 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood1b5cd512010-08-25 14:43:29 -0500588 int spread = 0;
589 int args = 2;
Marek Vasuta9869812011-09-14 00:20:35 +0200590 const char *scrub_warn =
591 "Warning: "
592 "scrub option will erase all factory set bad blocks!\n"
593 " "
594 "There is no reliable way to recover them.\n"
595 " "
596 "Use this command only for testing purposes if you\n"
597 " "
598 "are sure of what you are doing!\n"
599 "\nReally scrub this NAND flash? <y/N>\n";
Stefan Roesed351b2b2006-10-10 12:36:02 +0200600
Scott Wood1b5cd512010-08-25 14:43:29 -0500601 if (cmd[5] != 0) {
602 if (!strcmp(&cmd[5], ".spread")) {
603 spread = 1;
604 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500605 args = 1;
606 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood1b5cd512010-08-25 14:43:29 -0500607 args = 0;
608 } else {
609 goto usage;
610 }
611 }
612
613 /*
614 * Don't allow missing arguments to cause full chip/partition
615 * erases -- easy to do accidentally, e.g. with a misspelled
616 * variable name.
617 */
618 if (argc != o + args)
619 goto usage;
620
621 printf("\nNAND %s: ", cmd);
Stefan Roese198b23e2006-10-28 15:55:52 +0200622 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200623 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
624 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500625 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200626 return 1;
627
628 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200629 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200630
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500631 mtd = get_nand_dev_by_index(dev);
Scott Woode5836dc2010-08-02 13:04:24 -0500632
Stefan Roesed351b2b2006-10-10 12:36:02 +0200633 memset(&opts, 0, sizeof(opts));
634 opts.offset = off;
635 opts.length = size;
636 opts.jffs2 = clean;
637 opts.quiet = quiet;
Scott Wood1b5cd512010-08-25 14:43:29 -0500638 opts.spread = spread;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200639
640 if (scrub) {
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200641 if (scrub_yes) {
Marek Vasuta9869812011-09-14 00:20:35 +0200642 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200643 } else {
644 puts(scrub_warn);
645 if (confirm_yesno()) {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100646 opts.scrub = 1;
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200647 } else {
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100648 puts("scrub aborted\n");
Masahiro Yamada19f9c972013-07-11 17:29:57 +0900649 return 1;
Florian Fainelliadb8eb92010-03-20 19:02:58 +0100650 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200651 }
652 }
Scott Wood08364d92016-05-30 13:57:54 -0500653 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100654 printf("%s\n", ret ? "ERROR" : "OK");
655
656 return ret == 0 ? 0 : 1;
657 }
658
659 if (strncmp(cmd, "dump", 4) == 0) {
660 if (argc < 3)
661 goto usage;
662
Simon Glass3ff49ec2021-07-24 09:03:29 -0600663 off = (int)hextoul(argv[2], NULL);
Scott Wood08364d92016-05-30 13:57:54 -0500664 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100665
666 return ret == 0 ? 1 : 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100667 }
668
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100669 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woode5836dc2010-08-02 13:04:24 -0500670 size_t rwsize;
Scott Wood733fb022012-03-02 14:01:57 -0600671 ulong pagecount = 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200672 int read;
Harvey Chapmanabd3a3b2013-02-07 11:34:44 +0000673 int raw = 0;
Boris Brezillona7468e22016-06-15 10:42:18 +0200674 int no_verify = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200675
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100676 if (argc < 4)
677 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200678
Simon Glass3ff49ec2021-07-24 09:03:29 -0600679 addr = (ulong)hextoul(argv[2], NULL);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100680
Stefan Roesed351b2b2006-10-10 12:36:02 +0200681 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200682 printf("\nNAND %s: ", read ? "read" : "write");
William Juulb76ec382007-11-08 10:39:53 +0100683
Stefan Roesed351b2b2006-10-10 12:36:02 +0200684 s = strchr(cmd, '.');
Scott Wood733fb022012-03-02 14:01:57 -0600685
Boris Brezillona7468e22016-06-15 10:42:18 +0200686 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood733fb022012-03-02 14:01:57 -0600687 raw = 1;
688
Boris Brezillona7468e22016-06-15 10:42:18 +0200689 if (!strcmp(s, ".raw.noverify"))
690 no_verify = 1;
691
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200692 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
693 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500694 mtd->size))
Scott Wood733fb022012-03-02 14:01:57 -0600695 return 1;
696
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200697 if (set_dev(dev))
698 return 1;
699
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500700 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200701
Scott Wood733fb022012-03-02 14:01:57 -0600702 if (argc > 4 && !str2long(argv[4], &pagecount)) {
703 printf("'%s' is not a number\n", argv[4]);
704 return 1;
705 }
706
Scott Wood08364d92016-05-30 13:57:54 -0500707 if (pagecount * mtd->writesize > size) {
Scott Wood733fb022012-03-02 14:01:57 -0600708 puts("Size exceeds partition or device limit\n");
709 return -1;
710 }
711
Scott Wood08364d92016-05-30 13:57:54 -0500712 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood733fb022012-03-02 14:01:57 -0600713 } else {
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200714 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
715 &size, &maxsize,
716 MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500717 mtd->size) != 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200718 return 1;
719
720 if (set_dev(dev))
Scott Wood733fb022012-03-02 14:01:57 -0600721 return 1;
722
Harvey Chapman39e68862013-02-26 17:57:14 +0000723 /* size is unspecified */
724 if (argc < 5)
725 adjust_size_for_badblocks(&size, off, dev);
Scott Wood733fb022012-03-02 14:01:57 -0600726 rwsize = size;
727 }
728
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500729 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovycdb389d2014-09-16 14:38:52 +0200730
Scott Wood12b1c952008-06-12 13:13:23 -0500731 if (!s || !strcmp(s, ".jffs2") ||
732 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Woodcc5f3392008-06-12 13:20:16 -0500733 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500734 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000735 NULL, maxsize,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200736 (u_char *)addr);
Scott Woodcc5f3392008-06-12 13:20:16 -0500737 else
Scott Wood08364d92016-05-30 13:57:54 -0500738 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rini32d96182013-03-14 05:32:50 +0000739 NULL, maxsize,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600740 (u_char *)addr,
741 WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400742#ifdef CONFIG_CMD_NAND_TRIMFFS
743 } else if (!strcmp(s, ".trimffs")) {
744 if (read) {
745 printf("Unknown nand command suffix '%s'\n", s);
746 return 1;
747 }
Scott Wood08364d92016-05-30 13:57:54 -0500748 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rini32d96182013-03-14 05:32:50 +0000749 maxsize, (u_char *)addr,
Peter Tyser5fa8b982015-02-03 11:58:13 -0600750 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400751#endif
Mike Frysinger9a2cbb92009-04-12 22:29:20 -0400752 } else if (!strcmp(s, ".oob")) {
William Juul52c07962007-10-31 13:53:06 +0100753 /* out-of-band data */
754 mtd_oob_ops_t ops = {
755 .oobbuf = (u8 *)addr,
Scott Woode5836dc2010-08-02 13:04:24 -0500756 .ooblen = rwsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000757 .mode = MTD_OPS_RAW
William Juul52c07962007-10-31 13:53:06 +0100758 };
759
Harald Welte5a395102007-12-19 14:12:53 +0100760 if (read)
Scott Wood08364d92016-05-30 13:57:54 -0500761 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100762 else
Scott Wood08364d92016-05-30 13:57:54 -0500763 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood733fb022012-03-02 14:01:57 -0600764 } else if (raw) {
Boris Brezillona7468e22016-06-15 10:42:18 +0200765 ret = raw_access(mtd, addr, off, pagecount, read,
766 no_verify);
Stefan Roese198b23e2006-10-28 15:55:52 +0200767 } else {
Scott Wood12b1c952008-06-12 13:13:23 -0500768 printf("Unknown nand command suffix '%s'.\n", s);
769 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200770 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100771
Scott Woode5836dc2010-08-02 13:04:24 -0500772 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200773 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100774
775 return ret == 0 ? 0 : 1;
776 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200777
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100778#ifdef CONFIG_CMD_NAND_TORTURE
779 if (strcmp(cmd, "torture") == 0) {
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200780 loff_t endoff;
781 unsigned int failed = 0, passed = 0;
782
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100783 if (argc < 3)
784 goto usage;
785
786 if (!str2off(argv[2], &off)) {
787 puts("Offset is not a valid number\n");
788 return 1;
789 }
790
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200791 size = mtd->erasesize;
792 if (argc > 3) {
793 if (!str2off(argv[3], &size)) {
794 puts("Size is not a valid number\n");
795 return 1;
796 }
797 }
798
799 endoff = off + size;
800 if (endoff > mtd->size) {
801 puts("Arguments beyond end of NAND\n");
802 return 1;
803 }
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100804
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200805 off = round_down(off, mtd->erasesize);
806 endoff = round_up(endoff, mtd->erasesize);
807 size = endoff - off;
808 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
809 dev, off, size, mtd->erasesize);
810 while (off < endoff) {
811 ret = nand_torture(mtd, off);
812 if (ret) {
813 failed++;
814 printf(" block at 0x%llx failed\n", off);
815 } else {
816 passed++;
817 }
818 off += mtd->erasesize;
819 }
820 printf(" Passed: %u, failed: %u\n", passed, failed);
821 return failed != 0;
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100822 }
823#endif
824
Stefan Roesed351b2b2006-10-10 12:36:02 +0200825 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200826 argc -= 2;
827 argv += 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200828
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200829 if (argc <= 0)
830 goto usage;
831
832 while (argc > 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600833 addr = hextoul(*argv, NULL);
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200834
Scott Wood08364d92016-05-30 13:57:54 -0500835 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200836 printf("block 0x%08lx NOT marked "
837 "as bad! ERROR %d\n",
838 addr, ret);
839 ret = 1;
840 } else {
841 printf("block 0x%08lx successfully "
842 "marked as bad\n",
843 addr);
844 }
845 --argc;
846 ++argv;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200847 }
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200848 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200849 }
Scott Woodcc5f3392008-06-12 13:20:16 -0500850
Stefan Roesed351b2b2006-10-10 12:36:02 +0200851 if (strcmp(cmd, "biterr") == 0) {
Balamanikandan Gunasundard0460352021-06-29 12:46:11 +0530852 int bit;
853
854 if (argc != 4)
855 goto usage;
856
857 off = (int)simple_strtoul(argv[2], NULL, 16);
858 bit = (int)simple_strtoul(argv[3], NULL, 10);
859 ret = nand_biterror(mtd, off, bit);
860 return ret;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200861 }
862
Nishanth Menonb20f8402008-12-13 09:43:06 -0600863#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roesed351b2b2006-10-10 12:36:02 +0200864 if (strcmp(cmd, "lock") == 0) {
Nishanth Menonb20f8402008-12-13 09:43:06 -0600865 int tight = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200866 int status = 0;
867 if (argc == 3) {
868 if (!strcmp("tight", argv[2]))
869 tight = 1;
870 if (!strcmp("status", argv[2]))
871 status = 1;
872 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200873 if (status) {
Scott Wood08364d92016-05-30 13:57:54 -0500874 do_nand_status(mtd);
William Juul52c07962007-10-31 13:53:06 +0100875 } else {
Scott Wood08364d92016-05-30 13:57:54 -0500876 if (!nand_lock(mtd, tight)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100877 puts("NAND flash successfully locked\n");
878 } else {
879 puts("Error locking NAND flash\n");
880 return 1;
881 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200882 }
883 return 0;
884 }
885
Joe Hershbergercccf5952012-08-22 16:49:42 -0500886 if (strncmp(cmd, "unlock", 5) == 0) {
887 int allexcept = 0;
888
889 s = strchr(cmd, '.');
890
891 if (s && !strcmp(s, ".allexcept"))
892 allexcept = 1;
893
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200894 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
895 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500896 mtd->size) < 0)
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200897 return 1;
898
899 if (set_dev(dev))
Stefan Roese198b23e2006-10-28 15:55:52 +0200900 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200901
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500902 mtd = get_nand_dev_by_index(dev);
903
904 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100905 puts("NAND flash successfully unlocked\n");
906 } else {
907 puts("Error unlocking NAND flash, "
William Juulbfb83322007-11-14 14:28:11 +0100908 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100909 return 1;
910 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200911 return 0;
912 }
Nishanth Menonb20f8402008-12-13 09:43:06 -0600913#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200914
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100915usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000916 return CMD_RET_USAGE;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100917}
918
Kim Phillipsdc00a682012-10-29 13:34:31 +0000919#ifdef CONFIG_SYS_LONGHELP
920static char nand_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200921 "info - show available NAND devices\n"
922 "nand device [dev] - show or set current device\n"
923 "nand read - addr off|partition size\n"
924 "nand write - addr off|partition size\n"
925 " read/write 'size' bytes starting at offset 'off'\n"
926 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood733fb022012-03-02 14:01:57 -0600927 "nand read.raw - addr off|partition [count]\n"
Boris Brezillona7468e22016-06-15 10:42:18 +0200928 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood733fb022012-03-02 14:01:57 -0600929 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerbc04b4d2011-06-14 16:35:07 -0400930#ifdef CONFIG_CMD_NAND_TRIMFFS
931 "nand write.trimffs - addr off|partition size\n"
932 " write 'size' bytes starting at offset 'off' from memory address\n"
933 " 'addr', skipping bad blocks and dropping any pages at the end\n"
934 " of eraseblocks that contain only 0xFF\n"
935#endif
Daniel Hobid99968f2011-05-19 19:28:54 +0200936 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood1b5cd512010-08-25 14:43:29 -0500937 "from offset 'off'\n"
938 " With '.spread', erase enough for given file size, otherwise,\n"
939 " 'size' includes skipped bad blocks.\n"
940 "nand erase.part [clean] partition - erase entire mtd partition'\n"
941 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200942 "nand bad - show bad blocks\n"
943 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100944#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacherc65c7fa2016-06-13 10:15:48 +0200945 "nand torture off - torture one block at offset\n"
946 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau5661f702012-11-16 20:20:54 +0100947#endif
Marek Vasuta9869812011-09-14 00:20:35 +0200948 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood1b5cd512010-08-25 14:43:29 -0500949 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200950 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
Balamanikandan Gunasundard0460352021-06-29 12:46:11 +0530951 "nand biterr off bit - make a bit error at offset and bit position (UNSAFE)"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600952#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200953 "\n"
954 "nand lock [tight] [status]\n"
955 " bring nand to lock state or display locked pages\n"
Joe Hershbergercccf5952012-08-22 16:49:42 -0500956 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menonb20f8402008-12-13 09:43:06 -0600957#endif
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400958#ifdef CONFIG_ENV_OFFSET_OOB
959 "\n"
960 "nand env.oob - environment offset in OOB of block 0 of"
961 " first device.\n"
962 "nand env.oob set off|partition - set enviromnent offset\n"
963 "nand env.oob get - get environment offset"
964#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000965 "";
966#endif
967
968U_BOOT_CMD(
969 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
970 "NAND sub-system", nand_help_text
Nishanth Menonb20f8402008-12-13 09:43:06 -0600971);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100972
Simon Glassed38aef2020-05-10 11:40:03 -0600973static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200974 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100975{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100976 int r;
Mike Frysinger194c2e82011-06-05 13:43:02 +0000977 char *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200978 size_t cnt;
Tom Rinic220bd92019-05-23 07:14:07 -0400979#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600980 struct legacy_img_hdr *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200981#endif
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100982#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200983 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100984#endif
Thomas Knobloche84b7772007-07-06 14:58:39 +0200985
986 s = strchr(cmd, '.');
987 if (s != NULL &&
Scott Woodd1c1f9c2009-03-17 12:06:04 -0500988 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood12b1c952008-06-12 13:13:23 -0500989 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass0169e6b2012-02-13 13:51:18 +0000990 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood12b1c952008-06-12 13:13:23 -0500991 return 1;
992 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100993
Scott Wood08364d92016-05-30 13:57:54 -0500994 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100995
Scott Wood08364d92016-05-30 13:57:54 -0500996 cnt = mtd->writesize;
997 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
998 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100999 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001000 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +00001001 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001002 return 1;
1003 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001004 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001005
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001006 switch (genimg_get_format ((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -04001007#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001008 case IMAGE_FORMAT_LEGACY:
Simon Glassbb7d3bb2022-09-06 20:26:52 -06001009 hdr = (struct legacy_img_hdr *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001010
Simon Glass0169e6b2012-02-13 13:51:18 +00001011 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001012 image_print_contents (hdr);
1013
1014 cnt = image_get_image_size (hdr);
1015 break;
Heiko Schocher515eb122014-05-28 11:33:33 +02001016#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001017#if defined(CONFIG_FIT)
1018 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001019 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001020 puts ("Fit image detected...\n");
1021
1022 cnt = fit_get_size (fit_hdr);
1023 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001024#endif
1025 default:
Simon Glass0169e6b2012-02-13 13:51:18 +00001026 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001027 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001028 return 1;
1029 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001030 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001031
Scott Wood08364d92016-05-30 13:57:54 -05001032 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
1033 (u_char *)addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001034 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001035 puts("** Read error\n");
Simon Glass0169e6b2012-02-13 13:51:18 +00001036 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001037 return 1;
1038 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001039 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001040
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001041#if defined(CONFIG_FIT)
1042 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +02001043 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
Simon Glassd563c252021-02-15 17:08:09 -07001044 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Simon Glass0169e6b2012-02-13 13:51:18 +00001045 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowiczcefb4022008-06-06 23:07:40 +02001046 puts ("** Bad FIT image format\n");
1047 return 1;
1048 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001049 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowiczcefb4022008-06-06 23:07:40 +02001050 fit_print_contents (fit_hdr);
1051 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001052#endif
1053
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001054 /* Loading ok, update default load address */
1055
Simon Glass892265d2019-12-28 10:45:02 -07001056 image_load_addr = addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001057
Mike Frysinger194c2e82011-06-05 13:43:02 +00001058 return bootm_maybe_autostart(cmdtp, cmd);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001059}
1060
Simon Glassed38aef2020-05-10 11:40:03 -06001061static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
1062 char *const argv[])
Stefan Roese198b23e2006-10-28 15:55:52 +02001063{
1064 char *boot_device = NULL;
1065 int idx;
1066 ulong addr, offset = 0;
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001067 struct mtd_info *mtd;
Ladislav Michl9d0928d2009-04-21 02:26:31 +02001068#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +02001069 struct mtd_device *dev;
1070 struct part_info *part;
1071 u8 pnum;
1072
1073 if (argc >= 2) {
1074 char *p = (argc == 2) ? argv[1] : argv[2];
1075 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
1076 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
1077 if (dev->id->type != MTD_DEV_TYPE_NAND) {
1078 puts("Not a NAND device\n");
1079 return 1;
1080 }
1081 if (argc > 3)
1082 goto usage;
1083 if (argc == 3)
Simon Glass3ff49ec2021-07-24 09:03:29 -06001084 addr = hextoul(argv[1], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +02001085 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001086 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001087
1088 mtd = get_nand_dev_by_index(dev->id->num);
1089 return nand_load_image(cmdtp, mtd, part->offset,
1090 addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001091 }
1092 }
1093#endif
1094
Simon Glass0169e6b2012-02-13 13:51:18 +00001095 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Stefan Roese198b23e2006-10-28 15:55:52 +02001096 switch (argc) {
1097 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001098 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass64b723f2017-08-03 12:22:12 -06001099 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +02001100 break;
1101 case 2:
Simon Glass3ff49ec2021-07-24 09:03:29 -06001102 addr = hextoul(argv[1], NULL);
Simon Glass64b723f2017-08-03 12:22:12 -06001103 boot_device = env_get("bootdevice");
Stefan Roese198b23e2006-10-28 15:55:52 +02001104 break;
1105 case 3:
Simon Glass3ff49ec2021-07-24 09:03:29 -06001106 addr = hextoul(argv[1], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +02001107 boot_device = argv[2];
1108 break;
1109 case 4:
Simon Glass3ff49ec2021-07-24 09:03:29 -06001110 addr = hextoul(argv[1], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +02001111 boot_device = argv[2];
Simon Glass3ff49ec2021-07-24 09:03:29 -06001112 offset = hextoul(argv[3], NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +02001113 break;
1114 default:
Ladislav Michl9d0928d2009-04-21 02:26:31 +02001115#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese198b23e2006-10-28 15:55:52 +02001116usage:
1117#endif
Simon Glass0169e6b2012-02-13 13:51:18 +00001118 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glassa06dfc72011-12-10 08:44:01 +00001119 return CMD_RET_USAGE;
Stefan Roese198b23e2006-10-28 15:55:52 +02001120 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001121 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Stefan Roese198b23e2006-10-28 15:55:52 +02001122
1123 if (!boot_device) {
1124 puts("\n** No boot device **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +00001125 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001126 return 1;
1127 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001128 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001129
Simon Glass3ff49ec2021-07-24 09:03:29 -06001130 idx = hextoul(boot_device, NULL);
Stefan Roese198b23e2006-10-28 15:55:52 +02001131
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001132 mtd = get_nand_dev_by_index(idx);
1133 if (!mtd) {
Stefan Roese198b23e2006-10-28 15:55:52 +02001134 printf("\n** Device %d not available\n", idx);
Simon Glass0169e6b2012-02-13 13:51:18 +00001135 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001136 return 1;
1137 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001138 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Stefan Roese198b23e2006-10-28 15:55:52 +02001139
Mugunthan V N7b670cc2017-06-26 19:12:51 -05001140 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +02001141}
1142
1143U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001144 "boot from NAND device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001145 "[partition] | [[[loadAddr] dev] offset]"
1146);