blob: 0f47a258ce20abf1d41f7811b54b4d987940c09a [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
wdenkdc7c9a12003-03-26 06:55:25 +000013 */
14
15#include <common.h>
William Juulcfa460a2007-10-31 13:53:06 +010016#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010017#include <command.h>
18#include <watchdog.h>
19#include <malloc.h>
20#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010021#include <jffs2/jffs2.h>
22#include <nand.h>
23
Ladislav Michl0c8a8492009-04-21 02:26:31 +020024#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020025
Wolfgang Denk445093d2009-11-17 21:27:39 +010026/* partition handling routines */
Stefan Roese856f0542006-10-28 15:55:52 +020027int mtdparts_init(void);
28int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
29int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk4b070802008-08-14 14:41:06 +020030 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020031#endif
32
William Juul9ad754f2007-12-14 16:33:45 +010033static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010034{
35 int i;
William Juul9ad754f2007-12-14 16:33:45 +010036 u_char *datbuf, *oobbuf, *p;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010037
William Juul9ad754f2007-12-14 16:33:45 +010038 datbuf = malloc(nand->writesize + nand->oobsize);
39 oobbuf = malloc(nand->oobsize);
40 if (!datbuf || !oobbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010041 puts("No memory for page buffer\n");
42 return 1;
43 }
William Juulcfa460a2007-10-31 13:53:06 +010044 off &= ~(nand->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010045 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010046 struct mtd_oob_ops ops;
47 memset(&ops, 0, sizeof(ops));
48 ops.datbuf = datbuf;
49 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
50 ops.len = nand->writesize;
51 ops.ooblen = nand->oobsize;
52 ops.mode = MTD_OOB_RAW;
53 i = nand->read_oob(nand, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010054 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020055 printf("Error (%d) reading page %08lx\n", i, off);
William Juul9ad754f2007-12-14 16:33:45 +010056 free(datbuf);
57 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010058 return 1;
59 }
Stefan Roesee8706902008-07-10 10:10:54 +020060 printf("Page %08lx dump:\n", off);
William Juul9ad754f2007-12-14 16:33:45 +010061 i = nand->writesize >> 4;
62 p = datbuf;
Wolfgang Denk4b070802008-08-14 14:41:06 +020063
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010064 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010065 if (!only_oob)
66 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
67 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
68 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Wooddfbf6172008-06-12 13:20:16 -050069 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
70 p[15]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010071 p += 16;
72 }
73 puts("OOB:\n");
74 i = nand->oobsize >> 3;
75 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010076 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
77 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010078 p += 8;
79 }
William Juul9ad754f2007-12-14 16:33:45 +010080 free(datbuf);
81 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010082
83 return 0;
84}
85
86/* ------------------------------------------------------------------------- */
87
Stefan Roese856f0542006-10-28 15:55:52 +020088static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010089{
Stefan Roese856f0542006-10-28 15:55:52 +020090 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010091
Stefan Roese856f0542006-10-28 15:55:52 +020092 *num = simple_strtoul(p, &endptr, 16);
93 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
94}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010095
Stefan Roese856f0542006-10-28 15:55:52 +020096static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +020097arg_off_size(int argc, char * const argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +020098{
99 int idx = nand_curr_device;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200100#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200101 struct mtd_device *dev;
102 struct part_info *part;
103 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100104
Stefan Roese856f0542006-10-28 15:55:52 +0200105 if (argc >= 1 && !(str2long(argv[0], off))) {
106 if ((mtdparts_init() == 0) &&
107 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
108 if (dev->id->type != MTD_DEV_TYPE_NAND) {
109 puts("not a NAND device\n");
110 return -1;
111 }
112 *off = part->offset;
113 if (argc >= 2) {
Wolfgang Denk42ffcec2008-04-30 17:46:26 +0200114 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200115 printf("'%s' is not a number\n", argv[1]);
116 return -1;
117 }
118 if (*size > part->size)
119 *size = part->size;
120 } else {
121 *size = part->size;
122 }
123 idx = dev->id->num;
124 *nand = nand_info[idx];
125 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100126 }
Stefan Roese856f0542006-10-28 15:55:52 +0200127 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100128#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100129
Stefan Roese856f0542006-10-28 15:55:52 +0200130 if (argc >= 1) {
131 if (!(str2long(argv[0], off))) {
132 printf("'%s' is not a number\n", argv[0]);
133 return -1;
134 }
135 } else {
136 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100137 }
138
Stefan Roese856f0542006-10-28 15:55:52 +0200139 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200140 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200141 printf("'%s' is not a number\n", argv[1]);
142 return -1;
143 }
144 } else {
145 *size = nand->size - *off;
146 }
147
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200148#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200149out:
150#endif
151 printf("device %d ", idx);
152 if (*size == nand->size)
153 puts("whole chip\n");
154 else
Mike Frysingerd4bade82009-01-18 19:46:06 -0500155 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
Stefan Roese856f0542006-10-28 15:55:52 +0200156 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100157}
158
Nishanth Menon50657c22008-12-13 09:43:06 -0600159#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
160static void print_status(ulong start, ulong end, ulong erasesize, int status)
161{
162 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
163 start,
164 end - 1,
165 (end - start) / erasesize,
166 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
167 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
168 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
169}
170
171static void do_nand_status(nand_info_t *nand)
172{
173 ulong block_start = 0;
174 ulong off;
175 int last_status = -1;
176
177 struct nand_chip *nand_chip = nand->priv;
178 /* check the WP bit */
179 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
180 printf("device is %swrite protected\n",
181 (nand_chip->read_byte(nand) & 0x80 ?
182 "NOT " : ""));
183
184 for (off = 0; off < nand->size; off += nand->erasesize) {
185 int s = nand_get_lock_status(nand, off);
186
187 /* print message only if status has changed */
188 if (s != last_status && off != 0) {
189 print_status(block_start, off, nand->erasesize,
190 last_status);
191 block_start = off;
192 }
193 last_status = s;
194 }
195 /* Print the last block info */
196 print_status(block_start, off, nand->erasesize, last_status);
197}
198#endif
199
Ben Gardinerc9f73512010-07-05 13:27:07 -0400200#ifdef CONFIG_ENV_OFFSET_OOB
201unsigned long nand_env_oob_offset;
202
203int do_nand_env_oob(cmd_tbl_t *cmdtp, nand_info_t *nand,
Scott Wood53504a22010-07-12 18:17:40 -0500204 int argc, char * const argv[])
Ben Gardinerc9f73512010-07-05 13:27:07 -0400205{
206 int ret;
207 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
208
209 char *cmd = argv[1];
210
211 if (!strcmp(cmd, "get")) {
212 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500213 if (ret)
Ben Gardinerc9f73512010-07-05 13:27:07 -0400214 return 1;
Scott Wood53504a22010-07-12 18:17:40 -0500215
216 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400217 } else if (!strcmp(cmd, "set")) {
218 ulong addr;
219 size_t dummy_size;
220 struct mtd_oob_ops ops;
221
222 if (argc < 3)
223 goto usage;
224
Scott Wood53504a22010-07-12 18:17:40 -0500225 if (arg_off_size(argc - 2, argv + 2, nand, &addr,
226 &dummy_size) < 0) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400227 printf("Offset or partition name expected\n");
228 return 1;
229 }
230
231 if (nand->oobavail < ENV_OFFSET_SIZE) {
Scott Wood53504a22010-07-12 18:17:40 -0500232 printf("Insufficient available OOB bytes:\n"
233 "%d OOB bytes available but %d required for "
234 "env.oob support\n",
235 nand->oobavail, ENV_OFFSET_SIZE);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400236 return 1;
237 }
238
239 if ((addr & (nand->erasesize - 1)) != 0) {
240 printf("Environment offset must be block-aligned\n");
241 return 1;
242 }
243
244 ops.datbuf = NULL;
245 ops.mode = MTD_OOB_AUTO;
246 ops.ooboffs = 0;
247 ops.ooblen = ENV_OFFSET_SIZE;
248 ops.oobbuf = (void *) oob_buf;
249
250 oob_buf[0] = ENV_OOB_MARKER;
251 oob_buf[1] = addr / nand->erasesize;
252
253 ret = nand->write_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500254 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400255 printf("Error writing OOB block 0\n");
256 return ret;
257 }
Scott Wood53504a22010-07-12 18:17:40 -0500258
259 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
260 if (ret) {
261 printf("Error reading env offset in OOB\n");
262 return ret;
263 }
264
265 if (addr != nand_env_oob_offset) {
266 printf("Verification of env offset in OOB failed: "
267 "0x%08lx expected but got 0x%08lx\n",
268 addr, nand_env_oob_offset);
269 return 1;
270 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400271 } else {
272 goto usage;
273 }
274
275 return ret;
276
277usage:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200278 return cmd_usage(cmdtp);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400279}
280
281#endif
282
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100283static void nand_print_info(int idx)
284{
285 nand_info_t *nand = &nand_info[idx];
286 struct nand_chip *chip = nand->priv;
287 printf("Device %d: ", idx);
288 if (chip->numchips > 1)
289 printf("%dx ", chip->numchips);
290 printf("%s, sector size %u KiB\n",
291 nand->name, nand->erasesize >> 10);
292}
293
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200294int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100295{
William Juulcfa460a2007-10-31 13:53:06 +0100296 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200297 ulong addr, off;
298 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100299 char *cmd, *s;
300 nand_info_t *nand;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200301#ifdef CONFIG_SYS_NAND_QUIET
302 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200303#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200304 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200305#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200306 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100307
308 /* at least two arguments please */
309 if (argc < 2)
310 goto usage;
311
Stefan Roese2255b2d2006-10-10 12:36:02 +0200312 if (quiet_str)
313 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
314
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100315 cmd = argv[1];
316
317 if (strcmp(cmd, "info") == 0) {
318
319 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200320 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100321 if (nand_info[i].name)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100322 nand_print_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100323 }
324 return 0;
325 }
326
327 if (strcmp(cmd, "device") == 0) {
328
329 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100330 putc('\n');
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100331 if ((nand_curr_device < 0) ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200332 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100333 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100334 else
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100335 nand_print_info(nand_curr_device);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100336 return 0;
337 }
338 dev = (int)simple_strtoul(argv[2], NULL, 10);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200339 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100340 puts("No such device\n");
341 return 1;
342 }
343 printf("Device %d: %s", dev, nand_info[dev].name);
344 puts("... is now current device\n");
345 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200346
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200347#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200348 /*
349 * Select the chip in the board/cpu specific driver
350 */
351 board_nand_select_device(nand_info[dev].priv, dev);
352#endif
353
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100354 return 0;
355 }
356
357 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juul3043c042007-11-14 14:28:11 +0100358 strncmp(cmd, "dump", 4) != 0 &&
359 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
360 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
361 strcmp(cmd, "biterr") != 0 &&
Ben Gardinerc9f73512010-07-05 13:27:07 -0400362 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0
363#ifdef CONFIG_ENV_OFFSET_OOB
364 && strcmp(cmd, "env.oob") != 0
365#endif
366 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100367 goto usage;
368
Ben Gardinerc9f73512010-07-05 13:27:07 -0400369#ifdef CONFIG_ENV_OFFSET_OOB
370 /* this command operates only on the first nand device */
Scott Wood53504a22010-07-12 18:17:40 -0500371 if (strcmp(cmd, "env.oob") == 0) {
372 return do_nand_env_oob(cmdtp, &nand_info[0],
373 argc - 1, argv + 1);
374 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400375#endif
376
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100377 /* the following commands operate on the current device */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200378 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100379 !nand_info[nand_curr_device].name) {
380 puts("\nno devices available\n");
381 return 1;
382 }
383 nand = &nand_info[nand_curr_device];
384
385 if (strcmp(cmd, "bad") == 0) {
386 printf("\nDevice %d bad blocks:\n", nand_curr_device);
387 for (off = 0; off < nand->size; off += nand->erasesize)
388 if (nand_block_isbad(nand, off))
Stefan Roesee8706902008-07-10 10:10:54 +0200389 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100390 return 0;
391 }
392
Stefan Roese856f0542006-10-28 15:55:52 +0200393 /*
394 * Syntax is:
395 * 0 1 2 3 4
396 * nand erase [clean] [off size]
397 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200398 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
399 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200400 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100401 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200402 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200403 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100404
Stefan Roese856f0542006-10-28 15:55:52 +0200405 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
406 /* skip first two or three arguments, look for offset and size */
407 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
408 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200409
410 memset(&opts, 0, sizeof(opts));
411 opts.offset = off;
412 opts.length = size;
413 opts.jffs2 = clean;
414 opts.quiet = quiet;
415
416 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200417 puts("Warning: "
418 "scrub option will erase all factory set "
419 "bad blocks!\n"
420 " "
421 "There is no reliable way to recover them.\n"
422 " "
423 "Use this command only for testing purposes "
424 "if you\n"
425 " "
426 "are sure of what you are doing!\n"
427 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200428
Florian Fainelli6b94b492010-03-20 19:02:58 +0100429 if (getc() == 'y') {
430 puts("y");
431 if (getc() == '\r')
432 opts.scrub = 1;
433 else {
434 puts("scrub aborted\n");
435 return -1;
436 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200437 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200438 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200439 return -1;
440 }
441 }
442 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100443 printf("%s\n", ret ? "ERROR" : "OK");
444
445 return ret == 0 ? 0 : 1;
446 }
447
448 if (strncmp(cmd, "dump", 4) == 0) {
449 if (argc < 3)
450 goto usage;
451
452 s = strchr(cmd, '.');
453 off = (int)simple_strtoul(argv[2], NULL, 16);
454
455 if (s != NULL && strcmp(s, ".oob") == 0)
William Juul9ad754f2007-12-14 16:33:45 +0100456 ret = nand_dump(nand, off, 1);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100457 else
William Juul9ad754f2007-12-14 16:33:45 +0100458 ret = nand_dump(nand, off, 0);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100459
460 return ret == 0 ? 1 : 0;
461
462 }
463
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100464 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200465 int read;
466
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100467 if (argc < 4)
468 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200469
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100470 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
471
Stefan Roese2255b2d2006-10-10 12:36:02 +0200472 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200473 printf("\nNAND %s: ", read ? "read" : "write");
474 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
475 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100476
Stefan Roese2255b2d2006-10-10 12:36:02 +0200477 s = strchr(cmd, '.');
Scott Wood984e03c2008-06-12 13:13:23 -0500478 if (!s || !strcmp(s, ".jffs2") ||
479 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500480 if (read)
481 ret = nand_read_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200482 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500483 else
484 ret = nand_write_skip_bad(nand, off, &size,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200485 (u_char *)addr);
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400486 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100487 /* out-of-band data */
488 mtd_oob_ops_t ops = {
489 .oobbuf = (u8 *)addr,
490 .ooblen = size,
491 .mode = MTD_OOB_RAW
492 };
493
Harald Welte62d4f432007-12-19 14:12:53 +0100494 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100495 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100496 else
William Juulcfa460a2007-10-31 13:53:06 +0100497 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200498 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500499 printf("Unknown nand command suffix '%s'.\n", s);
500 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200501 }
502
Mike Frysingerd4bade82009-01-18 19:46:06 -0500503 printf(" %zu bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200504 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100505
506 return ret == 0 ? 0 : 1;
507 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200508
Stefan Roese2255b2d2006-10-10 12:36:02 +0200509 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200510 argc -= 2;
511 argv += 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200512
Wolfgang Denk8360b662009-05-24 17:34:33 +0200513 if (argc <= 0)
514 goto usage;
515
516 while (argc > 0) {
517 addr = simple_strtoul(*argv, NULL, 16);
518
519 if (nand->block_markbad(nand, addr)) {
520 printf("block 0x%08lx NOT marked "
521 "as bad! ERROR %d\n",
522 addr, ret);
523 ret = 1;
524 } else {
525 printf("block 0x%08lx successfully "
526 "marked as bad\n",
527 addr);
528 }
529 --argc;
530 ++argv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200531 }
Wolfgang Denk8360b662009-05-24 17:34:33 +0200532 return ret;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200533 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500534
Stefan Roese2255b2d2006-10-10 12:36:02 +0200535 if (strcmp(cmd, "biterr") == 0) {
536 /* todo */
537 return 1;
538 }
539
Nishanth Menon50657c22008-12-13 09:43:06 -0600540#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200541 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600542 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200543 int status = 0;
544 if (argc == 3) {
545 if (!strcmp("tight", argv[2]))
546 tight = 1;
547 if (!strcmp("status", argv[2]))
548 status = 1;
549 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200550 if (status) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600551 do_nand_status(nand);
William Juulcfa460a2007-10-31 13:53:06 +0100552 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100553 if (!nand_lock(nand, tight)) {
554 puts("NAND flash successfully locked\n");
555 } else {
556 puts("Error locking NAND flash\n");
557 return 1;
558 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200559 }
560 return 0;
561 }
562
563 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200564 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
565 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200566
William Juul5e1dae52007-11-09 13:32:30 +0100567 if (!nand_unlock(nand, off, size)) {
568 puts("NAND flash successfully unlocked\n");
569 } else {
570 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100571 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100572 return 1;
573 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200574 return 0;
575 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600576#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200577
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100578usage:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200579 return cmd_usage(cmdtp);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100580}
581
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200582U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
583 "NAND sub-system",
584 "info - show available NAND devices\n"
585 "nand device [dev] - show or set current device\n"
586 "nand read - addr off|partition size\n"
587 "nand write - addr off|partition size\n"
588 " read/write 'size' bytes starting at offset 'off'\n"
589 " to/from memory address 'addr', skipping bad blocks.\n"
590 "nand erase [clean] [off size] - erase 'size' bytes from\n"
591 " offset 'off' (entire device if not specified)\n"
592 "nand bad - show bad blocks\n"
593 "nand dump[.oob] off - dump page\n"
594 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
595 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
596 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menon50657c22008-12-13 09:43:06 -0600597#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200598 "\n"
599 "nand lock [tight] [status]\n"
600 " bring nand to lock state or display locked pages\n"
601 "nand unlock [offset] [size] - unlock section"
Nishanth Menon50657c22008-12-13 09:43:06 -0600602#endif
Ben Gardinerc9f73512010-07-05 13:27:07 -0400603#ifdef CONFIG_ENV_OFFSET_OOB
604 "\n"
605 "nand env.oob - environment offset in OOB of block 0 of"
606 " first device.\n"
607 "nand env.oob set off|partition - set enviromnent offset\n"
608 "nand env.oob get - get environment offset"
609#endif
Nishanth Menon50657c22008-12-13 09:43:06 -0600610);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100611
Stefan Roese856f0542006-10-28 15:55:52 +0200612static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200613 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200614{
615 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200616 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200617 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200618 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100619#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200620 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100621#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200622
623 s = strchr(cmd, '.');
624 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500625 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500626 printf("Unknown nand load suffix '%s'\n", s);
627 show_boot_progress(-53);
628 return 1;
629 }
Stefan Roese856f0542006-10-28 15:55:52 +0200630
631 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
632
William Juulcfa460a2007-10-31 13:53:06 +0100633 cnt = nand->writesize;
Ladislav Michl09875052009-03-13 14:38:19 +0100634 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200635 if (r) {
636 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200637 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200638 return 1;
639 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200640 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200641
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100642 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100643 case IMAGE_FORMAT_LEGACY:
644 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200645
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100646 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100647 image_print_contents (hdr);
648
649 cnt = image_get_image_size (hdr);
650 break;
651#if defined(CONFIG_FIT)
652 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100653 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100654 puts ("Fit image detected...\n");
655
656 cnt = fit_get_size (fit_hdr);
657 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100658#endif
659 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100660 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100661 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200662 return 1;
663 }
William Juul3043c042007-11-14 14:28:11 +0100664 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200665
Ladislav Michl09875052009-03-13 14:38:19 +0100666 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200667 if (r) {
668 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200669 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200670 return 1;
671 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200672 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200673
Marian Balakowicz09475f72008-03-12 10:33:01 +0100674#if defined(CONFIG_FIT)
675 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200676 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
677 if (!fit_check_format (fit_hdr)) {
678 show_boot_progress (-150);
679 puts ("** Bad FIT image format\n");
680 return 1;
681 }
682 show_boot_progress (151);
683 fit_print_contents (fit_hdr);
684 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100685#endif
686
Stefan Roese856f0542006-10-28 15:55:52 +0200687 /* Loading ok, update default load address */
688
689 load_addr = addr;
690
691 /* Check if we should attempt an auto-start */
692 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
693 char *local_args[2];
694 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
695
696 local_args[0] = cmd;
697 local_args[1] = NULL;
698
699 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
700
701 do_bootm(cmdtp, 0, 1, local_args);
702 return 1;
703 }
704 return 0;
705}
706
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200707int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100708{
709 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200710 int idx;
711 ulong addr, offset = 0;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200712#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200713 struct mtd_device *dev;
714 struct part_info *part;
715 u8 pnum;
716
717 if (argc >= 2) {
718 char *p = (argc == 2) ? argv[1] : argv[2];
719 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
720 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
721 if (dev->id->type != MTD_DEV_TYPE_NAND) {
722 puts("Not a NAND device\n");
723 return 1;
724 }
725 if (argc > 3)
726 goto usage;
727 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200728 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200729 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200730 addr = CONFIG_SYS_LOAD_ADDR;
Stefan Roese856f0542006-10-28 15:55:52 +0200731 return nand_load_image(cmdtp, &nand_info[dev->id->num],
Wolfgang Denk4b070802008-08-14 14:41:06 +0200732 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200733 }
734 }
735#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100736
Heiko Schocherfad63402007-07-13 09:54:17 +0200737 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100738 switch (argc) {
739 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200740 addr = CONFIG_SYS_LOAD_ADDR;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100741 boot_device = getenv("bootdevice");
742 break;
743 case 2:
744 addr = simple_strtoul(argv[1], NULL, 16);
745 boot_device = getenv("bootdevice");
746 break;
747 case 3:
748 addr = simple_strtoul(argv[1], NULL, 16);
749 boot_device = argv[2];
750 break;
751 case 4:
752 addr = simple_strtoul(argv[1], NULL, 16);
753 boot_device = argv[2];
754 offset = simple_strtoul(argv[3], NULL, 16);
755 break;
756 default:
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200757#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200758usage:
759#endif
Heiko Schocherfad63402007-07-13 09:54:17 +0200760 show_boot_progress(-53);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200761 return cmd_usage(cmdtp);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100762 }
763
Heiko Schocherfad63402007-07-13 09:54:17 +0200764 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100765 if (!boot_device) {
766 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200767 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100768 return 1;
769 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200770 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100771
Stefan Roese856f0542006-10-28 15:55:52 +0200772 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100773
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200774 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
Stefan Roese856f0542006-10-28 15:55:52 +0200775 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200776 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100777 return 1;
778 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200779 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100780
Stefan Roese856f0542006-10-28 15:55:52 +0200781 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100782}
783
784U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600785 "boot from NAND device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200786 "[partition] | [[[loadAddr] dev] offset]"
787);