blob: 2c421e948ed99593b78b9d368567012c0b8dcd62 [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 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
wdenkdc7c9a12003-03-26 06:55:25 +00009 */
10
11#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010012
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
William Juulcfa460a2007-10-31 13:53:06 +010021#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010022
Jon Loeligerc76fe472007-07-08 18:02:23 -050023#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010024
25#include <command.h>
26#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010029#include <jffs2/jffs2.h>
30#include <nand.h>
31
Jon Loeligerc76fe472007-07-08 18:02:23 -050032#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +020033
34/* parition handling routines */
35int mtdparts_init(void);
36int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
37int find_dev_and_part(const char *id, struct mtd_device **dev,
William Juul5e1dae52007-11-09 13:32:30 +010038 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020039#endif
40
William Juul9ad754f2007-12-14 16:33:45 +010041static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010042{
43 int i;
William Juul9ad754f2007-12-14 16:33:45 +010044 u_char *datbuf, *oobbuf, *p;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010045
William Juul9ad754f2007-12-14 16:33:45 +010046 datbuf = malloc(nand->writesize + nand->oobsize);
47 oobbuf = malloc(nand->oobsize);
48 if (!datbuf || !oobbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010049 puts("No memory for page buffer\n");
50 return 1;
51 }
William Juulcfa460a2007-10-31 13:53:06 +010052 off &= ~(nand->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010053 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010054 struct mtd_oob_ops ops;
55 memset(&ops, 0, sizeof(ops));
56 ops.datbuf = datbuf;
57 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
58 ops.len = nand->writesize;
59 ops.ooblen = nand->oobsize;
60 ops.mode = MTD_OOB_RAW;
61 i = nand->read_oob(nand, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010062 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020063 printf("Error (%d) reading page %08lx\n", i, off);
William Juul9ad754f2007-12-14 16:33:45 +010064 free(datbuf);
65 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010066 return 1;
67 }
Stefan Roesee8706902008-07-10 10:10:54 +020068 printf("Page %08lx dump:\n", off);
William Juul9ad754f2007-12-14 16:33:45 +010069 i = nand->writesize >> 4;
70 p = datbuf;
71
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010072 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010073 if (!only_oob)
74 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
75 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
76 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
77 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010078 p += 16;
79 }
80 puts("OOB:\n");
81 i = nand->oobsize >> 3;
82 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +010083 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
84 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010085 p += 8;
86 }
William Juul9ad754f2007-12-14 16:33:45 +010087 free(datbuf);
88 free(oobbuf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010089
90 return 0;
91}
92
93/* ------------------------------------------------------------------------- */
94
Stefan Roese856f0542006-10-28 15:55:52 +020095static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010096{
Stefan Roese856f0542006-10-28 15:55:52 +020097 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010098
Stefan Roese856f0542006-10-28 15:55:52 +020099 *num = simple_strtoul(p, &endptr, 16);
100 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
101}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102
Stefan Roese856f0542006-10-28 15:55:52 +0200103static int
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200104arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +0200105{
106 int idx = nand_curr_device;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500107#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200108 struct mtd_device *dev;
109 struct part_info *part;
110 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100111
Stefan Roese856f0542006-10-28 15:55:52 +0200112 if (argc >= 1 && !(str2long(argv[0], off))) {
113 if ((mtdparts_init() == 0) &&
114 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
115 if (dev->id->type != MTD_DEV_TYPE_NAND) {
116 puts("not a NAND device\n");
117 return -1;
118 }
119 *off = part->offset;
120 if (argc >= 2) {
Wolfgang Denk42ffcec2008-04-30 17:46:26 +0200121 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200122 printf("'%s' is not a number\n", argv[1]);
123 return -1;
124 }
125 if (*size > part->size)
126 *size = part->size;
127 } else {
128 *size = part->size;
129 }
130 idx = dev->id->num;
131 *nand = nand_info[idx];
132 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100133 }
Stefan Roese856f0542006-10-28 15:55:52 +0200134 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100135#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100136
Stefan Roese856f0542006-10-28 15:55:52 +0200137 if (argc >= 1) {
138 if (!(str2long(argv[0], off))) {
139 printf("'%s' is not a number\n", argv[0]);
140 return -1;
141 }
142 } else {
143 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100144 }
145
Stefan Roese856f0542006-10-28 15:55:52 +0200146 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200147 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200148 printf("'%s' is not a number\n", argv[1]);
149 return -1;
150 }
151 } else {
152 *size = nand->size - *off;
153 }
154
Jon Loeligerc76fe472007-07-08 18:02:23 -0500155#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200156out:
157#endif
158 printf("device %d ", idx);
159 if (*size == nand->size)
160 puts("whole chip\n");
161 else
Stefan Roesee8706902008-07-10 10:10:54 +0200162 printf("offset 0x%lx, size 0x%x\n", *off, *size);
Stefan Roese856f0542006-10-28 15:55:52 +0200163 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100164}
165
166int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
167{
William Juulcfa460a2007-10-31 13:53:06 +0100168 int i, dev, ret = 0;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200169 ulong addr, off;
170 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100171 char *cmd, *s;
172 nand_info_t *nand;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200173#ifdef CFG_NAND_QUIET
174 int quiet = CFG_NAND_QUIET;
175#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200176 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200177#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200178 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100179
180 /* at least two arguments please */
181 if (argc < 2)
182 goto usage;
183
Stefan Roese2255b2d2006-10-10 12:36:02 +0200184 if (quiet_str)
185 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
186
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100187 cmd = argv[1];
188
189 if (strcmp(cmd, "info") == 0) {
190
191 putc('\n');
192 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
193 if (nand_info[i].name)
Stefan Roesee8706902008-07-10 10:10:54 +0200194 printf("Device %d: %s, sector size %u KiB\n",
William Juulcfa460a2007-10-31 13:53:06 +0100195 i, nand_info[i].name,
196 nand_info[i].erasesize >> 10);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100197 }
198 return 0;
199 }
200
201 if (strcmp(cmd, "device") == 0) {
202
203 if (argc < 3) {
204 if ((nand_curr_device < 0) ||
William Juul3043c042007-11-14 14:28:11 +0100205 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100206 puts("\nno devices available\n");
207 else
208 printf("\nDevice %d: %s\n", nand_curr_device,
William Juulcfa460a2007-10-31 13:53:06 +0100209 nand_info[nand_curr_device].name);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100210 return 0;
211 }
212 dev = (int)simple_strtoul(argv[2], NULL, 10);
213 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
214 puts("No such device\n");
215 return 1;
216 }
217 printf("Device %d: %s", dev, nand_info[dev].name);
218 puts("... is now current device\n");
219 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200220
221#ifdef CFG_NAND_SELECT_DEVICE
222 /*
223 * Select the chip in the board/cpu specific driver
224 */
225 board_nand_select_device(nand_info[dev].priv, dev);
226#endif
227
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100228 return 0;
229 }
230
231 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juul3043c042007-11-14 14:28:11 +0100232 strncmp(cmd, "dump", 4) != 0 &&
233 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
234 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
235 strcmp(cmd, "biterr") != 0 &&
236 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100237 goto usage;
238
239 /* the following commands operate on the current device */
240 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
241 !nand_info[nand_curr_device].name) {
242 puts("\nno devices available\n");
243 return 1;
244 }
245 nand = &nand_info[nand_curr_device];
246
247 if (strcmp(cmd, "bad") == 0) {
248 printf("\nDevice %d bad blocks:\n", nand_curr_device);
249 for (off = 0; off < nand->size; off += nand->erasesize)
250 if (nand_block_isbad(nand, off))
Stefan Roesee8706902008-07-10 10:10:54 +0200251 printf(" %08lx\n", off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100252 return 0;
253 }
254
Stefan Roese856f0542006-10-28 15:55:52 +0200255 /*
256 * Syntax is:
257 * 0 1 2 3 4
258 * nand erase [clean] [off size]
259 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200260 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
261 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200262 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100263 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese856f0542006-10-28 15:55:52 +0200264 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200265 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100266
Stefan Roese856f0542006-10-28 15:55:52 +0200267 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
268 /* skip first two or three arguments, look for offset and size */
269 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
270 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200271
272 memset(&opts, 0, sizeof(opts));
273 opts.offset = off;
274 opts.length = size;
275 opts.jffs2 = clean;
276 opts.quiet = quiet;
277
278 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200279 puts("Warning: "
280 "scrub option will erase all factory set "
281 "bad blocks!\n"
282 " "
283 "There is no reliable way to recover them.\n"
284 " "
285 "Use this command only for testing purposes "
286 "if you\n"
287 " "
288 "are sure of what you are doing!\n"
289 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200290
291 if (getc() == 'y' && getc() == '\r') {
292 opts.scrub = 1;
293 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200294 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200295 return -1;
296 }
297 }
298 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100299 printf("%s\n", ret ? "ERROR" : "OK");
300
301 return ret == 0 ? 0 : 1;
302 }
303
304 if (strncmp(cmd, "dump", 4) == 0) {
305 if (argc < 3)
306 goto usage;
307
308 s = strchr(cmd, '.');
309 off = (int)simple_strtoul(argv[2], NULL, 16);
310
311 if (s != NULL && strcmp(s, ".oob") == 0)
William Juul9ad754f2007-12-14 16:33:45 +0100312 ret = nand_dump(nand, off, 1);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100313 else
William Juul9ad754f2007-12-14 16:33:45 +0100314 ret = nand_dump(nand, off, 0);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100315
316 return ret == 0 ? 1 : 0;
317
318 }
319
320 /* read write */
321 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200322 int read;
323
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100324 if (argc < 4)
325 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200326
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100327 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
328
Stefan Roese2255b2d2006-10-10 12:36:02 +0200329 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200330 printf("\nNAND %s: ", read ? "read" : "write");
331 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
332 return 1;
William Juul4cbb6512007-11-08 10:39:53 +0100333
Stefan Roese2255b2d2006-10-10 12:36:02 +0200334 s = strchr(cmd, '.');
Scott Wood984e03c2008-06-12 13:13:23 -0500335 if (!s || !strcmp(s, ".jffs2") ||
336 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200337 if (read) {
338 /* read */
339 nand_read_options_t opts;
340 memset(&opts, 0, sizeof(opts));
William Juulcfa460a2007-10-31 13:53:06 +0100341 opts.buffer = (u_char*) addr;
342 opts.length = size;
343 opts.offset = off;
344 opts.quiet = quiet;
William Juul3043c042007-11-14 14:28:11 +0100345/*
346 * ! BROKEN !
347 *
348 * TODO: Function must be implemented
349 *
350 * ret = nand_read_opts(nand, &opts);
351 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200352 } else {
353 /* write */
William Juulcfa460a2007-10-31 13:53:06 +0100354 mtd_oob_ops_t opts;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200355 memset(&opts, 0, sizeof(opts));
William Juulcfa460a2007-10-31 13:53:06 +0100356 opts.datbuf = (u_char*) addr;
357 opts.len = size;
358 opts.ooblen = 64;
359 opts.mode = MTD_OOB_AUTO;
360 ret = nand_write_opts(nand, off, &opts);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200361 }
Harald Welte62d4f432007-12-19 14:12:53 +0100362 } else if (s != NULL && !strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100363 /* out-of-band data */
364 mtd_oob_ops_t ops = {
365 .oobbuf = (u8 *)addr,
366 .ooblen = size,
367 .mode = MTD_OOB_RAW
368 };
369
Harald Welte62d4f432007-12-19 14:12:53 +0100370 if (read)
William Juulcfa460a2007-10-31 13:53:06 +0100371 ret = nand->read_oob(nand, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100372 else
William Juulcfa460a2007-10-31 13:53:06 +0100373 ret = nand->write_oob(nand, off, &ops);
Stefan Roese856f0542006-10-28 15:55:52 +0200374 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500375 printf("Unknown nand command suffix '%s'.\n", s);
376 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200377 }
378
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100379 printf(" %d bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200380 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100381
382 return ret == 0 ? 0 : 1;
383 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200384
Stefan Roese2255b2d2006-10-10 12:36:02 +0200385 if (strcmp(cmd, "markbad") == 0) {
386 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
387
388 int ret = nand->block_markbad(nand, addr);
389 if (ret == 0) {
390 printf("block 0x%08lx successfully marked as bad\n",
391 (ulong) addr);
392 return 0;
393 } else {
394 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
395 (ulong) addr, ret);
396 }
397 return 1;
398 }
399 if (strcmp(cmd, "biterr") == 0) {
400 /* todo */
401 return 1;
402 }
403
404 if (strcmp(cmd, "lock") == 0) {
405 int tight = 0;
406 int status = 0;
407 if (argc == 3) {
408 if (!strcmp("tight", argv[2]))
409 tight = 1;
410 if (!strcmp("status", argv[2]))
411 status = 1;
412 }
William Juul3043c042007-11-14 14:28:11 +0100413/*
414 * ! BROKEN !
Stefan Roesedeac9132008-01-05 16:50:32 +0100415 *
William Juul3043c042007-11-14 14:28:11 +0100416 * TODO: must be implemented and tested by someone with HW
417 */
418#if 0
Stefan Roese2255b2d2006-10-10 12:36:02 +0200419 if (status) {
William Juul3043c042007-11-14 14:28:11 +0100420 ulong block_start = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200421 ulong off;
William Juul5e1dae52007-11-09 13:32:30 +0100422 int last_status = -1;
William Juul3043c042007-11-14 14:28:11 +0100423
Stefan Roese2255b2d2006-10-10 12:36:02 +0200424 struct nand_chip *nand_chip = nand->priv;
425 /* check the WP bit */
426 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
427 printf("device is %swrite protected\n",
428 (nand_chip->read_byte(nand) & 0x80 ?
William Juul5e1dae52007-11-09 13:32:30 +0100429 "NOT " : ""));
Stefan Roese2255b2d2006-10-10 12:36:02 +0200430
William Juulcfa460a2007-10-31 13:53:06 +0100431 for (off = 0; off < nand->size; off += nand->writesize) {
William Juul5e1dae52007-11-09 13:32:30 +0100432 int s = nand_get_lock_status(nand, off);
433
434 /* print message only if status has changed
435 * or at end of chip
436 */
437 if (off == nand->size - nand->writesize
William Juul3043c042007-11-14 14:28:11 +0100438 || (s != last_status && off != 0)) {
William Juul5e1dae52007-11-09 13:32:30 +0100439
440 printf("%08lx - %08lx: %8d pages %s%s%s\n",
William Juul3043c042007-11-14 14:28:11 +0100441 block_start,
442 off-1,
443 (off-block_start)/nand->writesize,
444 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
445 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
446 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
William Juul5e1dae52007-11-09 13:32:30 +0100447 }
448
449 last_status = s;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200450 }
William Juulcfa460a2007-10-31 13:53:06 +0100451 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100452 if (!nand_lock(nand, tight)) {
453 puts("NAND flash successfully locked\n");
454 } else {
455 puts("Error locking NAND flash\n");
456 return 1;
457 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200458 }
William Juul3043c042007-11-14 14:28:11 +0100459#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200460 return 0;
461 }
462
463 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200464 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
465 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200466
William Juul3043c042007-11-14 14:28:11 +0100467/*
468 * ! BROKEN !
Stefan Roesedeac9132008-01-05 16:50:32 +0100469 *
William Juul3043c042007-11-14 14:28:11 +0100470 * TODO: must be implemented and tested by someone with HW
471 */
472#if 0
William Juul5e1dae52007-11-09 13:32:30 +0100473 if (!nand_unlock(nand, off, size)) {
474 puts("NAND flash successfully unlocked\n");
475 } else {
476 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100477 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100478 return 1;
479 }
480#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200481 return 0;
482 }
483
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100484usage:
485 printf("Usage:\n%s\n", cmdtp->usage);
486 return 1;
487}
488
489U_BOOT_CMD(nand, 5, 1, do_nand,
William Juulcfa460a2007-10-31 13:53:06 +0100490 "nand - NAND sub-system\n",
491 "info - show available NAND devices\n"
492 "nand device [dev] - show or set current device\n"
Scott Wood984e03c2008-06-12 13:13:23 -0500493 "nand read - addr off|partition size\n"
494 "nand write - addr off|partition size\n"
William Juulcfa460a2007-10-31 13:53:06 +0100495 " read/write 'size' bytes starting at offset 'off'\n"
Scott Wood984e03c2008-06-12 13:13:23 -0500496 " to/from memory address 'addr', skipping bad blocks.\n"
William Juulcfa460a2007-10-31 13:53:06 +0100497 "nand erase [clean] [off size] - erase 'size' bytes from\n"
498 " offset 'off' (entire device if not specified)\n"
499 "nand bad - show bad blocks\n"
500 "nand dump[.oob] off - dump page\n"
501 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
502 "nand markbad off - mark bad block at offset (UNSAFE)\n"
503 "nand biterr off - make a bit error at offset (UNSAFE)\n"
504 "nand lock [tight] [status]\n"
505 " bring nand to lock state or display locked pages\n"
506 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100507
Stefan Roese856f0542006-10-28 15:55:52 +0200508static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
William Juulcfa460a2007-10-31 13:53:06 +0100509 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200510{
511 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200512 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200513 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200514 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100515#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200516 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100517#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200518
519 s = strchr(cmd, '.');
520 if (s != NULL &&
Scott Wood984e03c2008-06-12 13:13:23 -0500521 (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) {
522 printf("Unknown nand load suffix '%s'\n", s);
523 show_boot_progress(-53);
524 return 1;
525 }
Stefan Roese856f0542006-10-28 15:55:52 +0200526
527 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
528
William Juulcfa460a2007-10-31 13:53:06 +0100529 cnt = nand->writesize;
530 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200531 if (r) {
532 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200533 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200534 return 1;
535 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200536 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200537
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100538 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100539 case IMAGE_FORMAT_LEGACY:
540 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200541
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100542 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100543 image_print_contents (hdr);
544
545 cnt = image_get_image_size (hdr);
546 break;
547#if defined(CONFIG_FIT)
548 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100549 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100550 puts ("Fit image detected...\n");
551
552 cnt = fit_get_size (fit_hdr);
553 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100554#endif
555 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100556 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100557 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200558 return 1;
559 }
William Juul3043c042007-11-14 14:28:11 +0100560 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200561
Scott Wood984e03c2008-06-12 13:13:23 -0500562 /* FIXME: skip bad blocks */
William Juulcfa460a2007-10-31 13:53:06 +0100563 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200564 if (r) {
565 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200566 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200567 return 1;
568 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200569 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200570
Marian Balakowicz09475f72008-03-12 10:33:01 +0100571#if defined(CONFIG_FIT)
572 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200573 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
574 if (!fit_check_format (fit_hdr)) {
575 show_boot_progress (-150);
576 puts ("** Bad FIT image format\n");
577 return 1;
578 }
579 show_boot_progress (151);
580 fit_print_contents (fit_hdr);
581 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100582#endif
583
Stefan Roese856f0542006-10-28 15:55:52 +0200584 /* Loading ok, update default load address */
585
586 load_addr = addr;
587
588 /* Check if we should attempt an auto-start */
589 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
590 char *local_args[2];
591 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
592
593 local_args[0] = cmd;
594 local_args[1] = NULL;
595
596 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
597
598 do_bootm(cmdtp, 0, 1, local_args);
599 return 1;
600 }
601 return 0;
602}
603
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100604int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
605{
606 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200607 int idx;
608 ulong addr, offset = 0;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500609#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200610 struct mtd_device *dev;
611 struct part_info *part;
612 u8 pnum;
613
614 if (argc >= 2) {
615 char *p = (argc == 2) ? argv[1] : argv[2];
616 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
617 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
618 if (dev->id->type != MTD_DEV_TYPE_NAND) {
619 puts("Not a NAND device\n");
620 return 1;
621 }
622 if (argc > 3)
623 goto usage;
624 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200625 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200626 else
627 addr = CFG_LOAD_ADDR;
628 return nand_load_image(cmdtp, &nand_info[dev->id->num],
William Juulcfa460a2007-10-31 13:53:06 +0100629 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200630 }
631 }
632#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100633
Heiko Schocherfad63402007-07-13 09:54:17 +0200634 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100635 switch (argc) {
636 case 1:
637 addr = CFG_LOAD_ADDR;
638 boot_device = getenv("bootdevice");
639 break;
640 case 2:
641 addr = simple_strtoul(argv[1], NULL, 16);
642 boot_device = getenv("bootdevice");
643 break;
644 case 3:
645 addr = simple_strtoul(argv[1], NULL, 16);
646 boot_device = argv[2];
647 break;
648 case 4:
649 addr = simple_strtoul(argv[1], NULL, 16);
650 boot_device = argv[2];
651 offset = simple_strtoul(argv[3], NULL, 16);
652 break;
653 default:
Jon Loeligerc76fe472007-07-08 18:02:23 -0500654#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200655usage:
656#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100657 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200658 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100659 return 1;
660 }
661
Heiko Schocherfad63402007-07-13 09:54:17 +0200662 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100663 if (!boot_device) {
664 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200665 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100666 return 1;
667 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200668 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100669
Stefan Roese856f0542006-10-28 15:55:52 +0200670 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100671
Stefan Roese856f0542006-10-28 15:55:52 +0200672 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
673 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200674 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100675 return 1;
676 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200677 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100678
Stefan Roese856f0542006-10-28 15:55:52 +0200679 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100680}
681
682U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Stefan Roese856f0542006-10-28 15:55:52 +0200683 "nboot - boot from NAND device\n",
Scott Wood984e03c2008-06-12 13:13:23 -0500684 "[partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100685
Jon Loeligerc76fe472007-07-08 18:02:23 -0500686#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100687
688#else /* CFG_NAND_LEGACY */
689/*
690 *
691 * Legacy NAND support - to be phased out
692 *
693 */
wdenkdc7c9a12003-03-26 06:55:25 +0000694#include <command.h>
695#include <malloc.h>
696#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000697#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000698
Heiko Schocherfad63402007-07-13 09:54:17 +0200699#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000700# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200701# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000702#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200703# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000704#endif
705
Jon Loeligerc76fe472007-07-08 18:02:23 -0500706#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100707#include <linux/mtd/nand_legacy.h>
708#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000709#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000710#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100711#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000712
wdenk1f4bb372003-07-27 00:21:01 +0000713#ifdef CONFIG_OMAP1510
714void archflashwp(void *archdata, int wp);
715#endif
716
William Juul3043c042007-11-14 14:28:11 +0100717#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
wdenk1f4bb372003-07-27 00:21:01 +0000718
William Juul3043c042007-11-14 14:28:11 +0100719#undef NAND_DEBUG
720#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000721
722/* ****************** WARNING *********************
723 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
724 * erase (or at least attempt to erase) blocks that are marked
725 * bad. This can be very handy if you are _sure_ that the block
726 * is OK, say because you marked a good block bad to test bad
727 * block handling and you are done testing, or if you have
728 * accidentally marked blocks bad.
729 *
730 * Erasing factory marked bad blocks is a _bad_ idea. If the
731 * erase succeeds there is no reliable way to find them again,
732 * and attempting to program or erase bad blocks can affect
733 * the data in _other_ (good) blocks.
734 */
William Juul3043c042007-11-14 14:28:11 +0100735#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000736
737#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000738#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000739
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100740/* bits for nand_legacy_rw() `cmd'; or together as needed */
William Juul5e1dae52007-11-09 13:32:30 +0100741#define NANDRW_READ 0x01
742#define NANDRW_WRITE 0x00
743#define NANDRW_JFFS2 0x02
William Juulcfa460a2007-10-31 13:53:06 +0100744#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000745
wdenkdc7c9a12003-03-26 06:55:25 +0000746/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100747 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000748 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100749extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
750extern int curr_device;
751extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100752 size_t len, int clean);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100753extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
William Juul5e1dae52007-11-09 13:32:30 +0100754 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100755extern void nand_print(struct nand_chip *nand);
756extern void nand_print_bad(struct nand_chip *nand);
757extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100758 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100759extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
William Juul5e1dae52007-11-09 13:32:30 +0100760 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000761
wdenkdc7c9a12003-03-26 06:55:25 +0000762
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100763int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000764{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100765 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000766
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100767 switch (argc) {
768 case 0:
769 case 1:
770 printf ("Usage:\n%s\n", cmdtp->usage);
771 return 1;
772 case 2:
773 if (strcmp (argv[1], "info") == 0) {
774 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000775
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100776 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000777
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100778 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
779 if (nand_dev_desc[i].ChipID ==
780 NAND_ChipID_UNKNOWN)
781 continue; /* list only known devices */
782 printf ("Device %d: ", i);
783 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000784 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100785 return 0;
786
787 } else if (strcmp (argv[1], "device") == 0) {
788 if ((curr_device < 0)
789 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
790 puts ("\nno devices available\n");
791 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000792 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100793 printf ("\nDevice %d: ", curr_device);
794 nand_print (&nand_dev_desc[curr_device]);
795 return 0;
796
797 } else if (strcmp (argv[1], "bad") == 0) {
798 if ((curr_device < 0)
799 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
800 puts ("\nno devices available\n");
801 return 1;
802 }
803 printf ("\nDevice %d bad blocks:\n", curr_device);
804 nand_print_bad (&nand_dev_desc[curr_device]);
805 return 0;
806
807 }
808 printf ("Usage:\n%s\n", cmdtp->usage);
809 return 1;
810 case 3:
811 if (strcmp (argv[1], "device") == 0) {
812 int dev = (int) simple_strtoul (argv[2], NULL, 10);
813
814 printf ("\nDevice %d: ", dev);
815 if (dev >= CFG_MAX_NAND_DEVICE) {
816 puts ("unknown device\n");
817 return 1;
818 }
819 nand_print (&nand_dev_desc[dev]);
820 /*nand_print (dev); */
821
822 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
823 return 1;
824 }
825
826 curr_device = dev;
827
828 puts ("... is now current device\n");
829
830 return 0;
831 } else if (strcmp (argv[1], "erase") == 0
832 && strcmp (argv[2], "clean") == 0) {
833 struct nand_chip *nand = &nand_dev_desc[curr_device];
834 ulong off = 0;
835 ulong size = nand->totlen;
836 int ret;
837
838 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
839
840 ret = nand_legacy_erase (nand, off, size, 1);
841
842 printf ("%s\n", ret ? "ERROR" : "OK");
843
wdenk7a8e9bed2003-05-31 18:35:21 +0000844 return ret;
845 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100846
847 printf ("Usage:\n%s\n", cmdtp->usage);
848 return 1;
849 default:
850 /* at least 4 args */
851
852 if (strncmp (argv[1], "read", 4) == 0 ||
853 strncmp (argv[1], "write", 5) == 0) {
William Juul5e1dae52007-11-09 13:32:30 +0100854 ulong addr = simple_strtoul (argv[2], NULL, 16);
855 off_t off = simple_strtoul (argv[3], NULL, 16);
856 size_t size = simple_strtoul (argv[4], NULL, 16);
857 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
858 NANDRW_READ : NANDRW_WRITE;
Wolfgang Denkc71abba2008-04-28 14:55:12 +0200859 size_t total;
860 int ret;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100861 char *cmdtail = strchr (argv[1], '.');
862
863 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
864 /* read out-of-band data */
865 if (cmd & NANDRW_READ) {
866 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200867 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100868 (u_char *) addr);
869 } else {
870 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200871 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100872 (u_char *) addr);
873 }
874 return ret;
875 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
876 cmd |= NANDRW_JFFS2; /* skip bad blocks */
877 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
878 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
879 if (cmd & NANDRW_READ)
880 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
881 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000882#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100883 /* need ".e" same as ".j" for compatibility with older units */
884 else if (cmdtail && !strcmp (cmdtail, ".e"))
885 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000886#endif
stroesea842a6d2004-12-16 17:45:46 +0000887#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100888 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
889 /* ".i" for image -> read skips bad block (no 0xff) */
890 else if (cmdtail && !strcmp (cmdtail, ".i")) {
891 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
892 if (cmd & NANDRW_READ)
William Juul3043c042007-11-14 14:28:11 +0100893 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100894 }
stroesea842a6d2004-12-16 17:45:46 +0000895#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100896 else if (cmdtail) {
897 printf ("Usage:\n%s\n", cmdtp->usage);
898 return 1;
899 }
900
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200901 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100902 (cmd & NANDRW_READ) ? "read" : "write",
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200903 curr_device, off, (ulong)size);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100904
905 ret = nand_legacy_rw (nand_dev_desc + curr_device,
906 cmd, off, size,
William Juul3043c042007-11-14 14:28:11 +0100907 &total, (u_char *) addr);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100908
909 printf (" %d bytes %s: %s\n", total,
910 (cmd & NANDRW_READ) ? "read" : "written",
911 ret ? "ERROR" : "OK");
912
913 return ret;
914 } else if (strcmp (argv[1], "erase") == 0 &&
915 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
916 int clean = argc == 5;
917 ulong off =
918 simple_strtoul (argv[2 + clean], NULL, 16);
919 ulong size =
920 simple_strtoul (argv[3 + clean], NULL, 16);
921 int ret;
922
923 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
924 curr_device, off, size);
925
926 ret = nand_legacy_erase (nand_dev_desc + curr_device,
927 off, size, clean);
928
929 printf ("%s\n", ret ? "ERROR" : "OK");
930
931 return ret;
932 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000933 printf ("Usage:\n%s\n", cmdtp->usage);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100934 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000935 }
wdenkdc7c9a12003-03-26 06:55:25 +0000936
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100937 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000938 }
wdenkdc7c9a12003-03-26 06:55:25 +0000939}
940
wdenk0d498392003-07-01 21:06:45 +0000941U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +0100942 nand, 5, 1, do_nand,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200943 "nand - legacy NAND sub-system\n",
wdenkb0fce992003-06-29 21:03:46 +0000944 "info - show available NAND devices\n"
945 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000946 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000947 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
William Juul3043c042007-11-14 14:28:11 +0100948 " at offset `off' to/from memory address `addr'\n"
wdenkb0fce992003-06-29 21:03:46 +0000949 "nand erase [clean] [off size] - erase `size' bytes from\n"
William Juul3043c042007-11-14 14:28:11 +0100950 " offset `off' (entire device if not specified)\n"
wdenkb0fce992003-06-29 21:03:46 +0000951 "nand bad - show bad blocks\n"
952 "nand read.oob addr off size - read out-of-band data\n"
953 "nand write.oob addr off size - read out-of-band data\n"
954);
955
wdenkdc7c9a12003-03-26 06:55:25 +0000956int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
957{
958 char *boot_device = NULL;
959 char *ep;
960 int dev;
961 ulong cnt;
962 ulong addr;
963 ulong offset = 0;
964 image_header_t *hdr;
965 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100966#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200967 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100968#endif
969
Heiko Schocherfad63402007-07-13 09:54:17 +0200970 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000971 switch (argc) {
972 case 1:
973 addr = CFG_LOAD_ADDR;
974 boot_device = getenv ("bootdevice");
975 break;
976 case 2:
977 addr = simple_strtoul(argv[1], NULL, 16);
978 boot_device = getenv ("bootdevice");
979 break;
980 case 3:
981 addr = simple_strtoul(argv[1], NULL, 16);
982 boot_device = argv[2];
983 break;
984 case 4:
985 addr = simple_strtoul(argv[1], NULL, 16);
986 boot_device = argv[2];
987 offset = simple_strtoul(argv[3], NULL, 16);
988 break;
989 default:
990 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200991 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000992 return 1;
993 }
994
Heiko Schocherfad63402007-07-13 09:54:17 +0200995 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000996 if (!boot_device) {
997 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200998 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000999 return 1;
1000 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001001 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +00001002
1003 dev = simple_strtoul(boot_device, &ep, 16);
1004
1005 if ((dev >= CFG_MAX_NAND_DEVICE) ||
William Juul3043c042007-11-14 14:28:11 +01001006 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001007 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001008 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +00001009 return 1;
1010 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001011 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +00001012
wdenk7a8e9bed2003-05-31 18:35:21 +00001013 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
William Juulcfa460a2007-10-31 13:53:06 +01001014 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1015 offset);
wdenkdc7c9a12003-03-26 06:55:25 +00001016
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001017 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
William Juulcfa460a2007-10-31 13:53:06 +01001018 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001019 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001020 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +00001021 return 1;
1022 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001023 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001024
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001025 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001026 case IMAGE_FORMAT_LEGACY:
1027 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001028 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001029
Marian Balakowicz09475f72008-03-12 10:33:01 +01001030 cnt = image_get_image_size (hdr);
1031 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001032 break;
1033#if defined(CONFIG_FIT)
1034 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001035 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001036 puts ("Fit image detected...\n");
1037
1038 cnt = fit_get_size (fit_hdr);
1039 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001040#endif
1041 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001042 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001043 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001044 return 1;
1045 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001046 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001047
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001048 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
William Juulcfa460a2007-10-31 13:53:06 +01001049 offset + SECTORSIZE, cnt, NULL,
1050 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001051 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001052 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001053 return 1;
1054 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001055 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001056
Marian Balakowicz09475f72008-03-12 10:33:01 +01001057#if defined(CONFIG_FIT)
1058 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +02001059 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1060 if (!fit_check_format (fit_hdr)) {
1061 show_boot_progress (-150);
1062 puts ("** Bad FIT image format\n");
1063 return 1;
1064 }
1065 show_boot_progress (151);
1066 fit_print_contents (fit_hdr);
1067 }
Marian Balakowicz09475f72008-03-12 10:33:01 +01001068#endif
1069
wdenkdc7c9a12003-03-26 06:55:25 +00001070 /* Loading ok, update default load address */
1071
1072 load_addr = addr;
1073
1074 /* Check if we should attempt an auto-start */
1075 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1076 char *local_args[2];
1077 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1078
1079 local_args[0] = argv[0];
1080 local_args[1] = NULL;
1081
wdenk7a8e9bed2003-05-31 18:35:21 +00001082 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001083
1084 do_bootm (cmdtp, 0, 1, local_args);
1085 rcode = 1;
1086 }
1087 return rcode;
1088}
1089
wdenk0d498392003-07-01 21:06:45 +00001090U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +01001091 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +00001092 "nboot - boot from NAND device\n",
1093 "loadAddr dev\n"
1094);
1095
Jon Loeligerc76fe472007-07-08 18:02:23 -05001096#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001097
1098#endif /* CFG_NAND_LEGACY */