blob: 710ba8f9460fafd058d9185eacf1bd13700d60b4 [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, '.');
335 if (s != NULL &&
William Juul3043c042007-11-14 14:28:11 +0100336 (!strcmp(s, ".jffs2") || !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 {
375 if (read)
376 ret = nand_read(nand, off, &size, (u_char *)addr);
377 else
378 ret = nand_write(nand, off, &size, (u_char *)addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200379 }
380
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100381 printf(" %d bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200382 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100383
384 return ret == 0 ? 0 : 1;
385 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200386
Stefan Roese2255b2d2006-10-10 12:36:02 +0200387 if (strcmp(cmd, "markbad") == 0) {
388 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
389
390 int ret = nand->block_markbad(nand, addr);
391 if (ret == 0) {
392 printf("block 0x%08lx successfully marked as bad\n",
393 (ulong) addr);
394 return 0;
395 } else {
396 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
397 (ulong) addr, ret);
398 }
399 return 1;
400 }
401 if (strcmp(cmd, "biterr") == 0) {
402 /* todo */
403 return 1;
404 }
405
406 if (strcmp(cmd, "lock") == 0) {
407 int tight = 0;
408 int status = 0;
409 if (argc == 3) {
410 if (!strcmp("tight", argv[2]))
411 tight = 1;
412 if (!strcmp("status", argv[2]))
413 status = 1;
414 }
William Juul3043c042007-11-14 14:28:11 +0100415/*
416 * ! BROKEN !
Stefan Roesedeac9132008-01-05 16:50:32 +0100417 *
William Juul3043c042007-11-14 14:28:11 +0100418 * TODO: must be implemented and tested by someone with HW
419 */
420#if 0
Stefan Roese2255b2d2006-10-10 12:36:02 +0200421 if (status) {
William Juul3043c042007-11-14 14:28:11 +0100422 ulong block_start = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200423 ulong off;
William Juul5e1dae52007-11-09 13:32:30 +0100424 int last_status = -1;
William Juul3043c042007-11-14 14:28:11 +0100425
Stefan Roese2255b2d2006-10-10 12:36:02 +0200426 struct nand_chip *nand_chip = nand->priv;
427 /* check the WP bit */
428 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
429 printf("device is %swrite protected\n",
430 (nand_chip->read_byte(nand) & 0x80 ?
William Juul5e1dae52007-11-09 13:32:30 +0100431 "NOT " : ""));
Stefan Roese2255b2d2006-10-10 12:36:02 +0200432
William Juulcfa460a2007-10-31 13:53:06 +0100433 for (off = 0; off < nand->size; off += nand->writesize) {
William Juul5e1dae52007-11-09 13:32:30 +0100434 int s = nand_get_lock_status(nand, off);
435
436 /* print message only if status has changed
437 * or at end of chip
438 */
439 if (off == nand->size - nand->writesize
William Juul3043c042007-11-14 14:28:11 +0100440 || (s != last_status && off != 0)) {
William Juul5e1dae52007-11-09 13:32:30 +0100441
442 printf("%08lx - %08lx: %8d pages %s%s%s\n",
William Juul3043c042007-11-14 14:28:11 +0100443 block_start,
444 off-1,
445 (off-block_start)/nand->writesize,
446 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
447 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
448 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
William Juul5e1dae52007-11-09 13:32:30 +0100449 }
450
451 last_status = s;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200452 }
William Juulcfa460a2007-10-31 13:53:06 +0100453 } else {
William Juul5e1dae52007-11-09 13:32:30 +0100454 if (!nand_lock(nand, tight)) {
455 puts("NAND flash successfully locked\n");
456 } else {
457 puts("Error locking NAND flash\n");
458 return 1;
459 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200460 }
William Juul3043c042007-11-14 14:28:11 +0100461#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200462 return 0;
463 }
464
465 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200466 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
467 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200468
William Juul3043c042007-11-14 14:28:11 +0100469/*
470 * ! BROKEN !
Stefan Roesedeac9132008-01-05 16:50:32 +0100471 *
William Juul3043c042007-11-14 14:28:11 +0100472 * TODO: must be implemented and tested by someone with HW
473 */
474#if 0
William Juul5e1dae52007-11-09 13:32:30 +0100475 if (!nand_unlock(nand, off, size)) {
476 puts("NAND flash successfully unlocked\n");
477 } else {
478 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100479 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100480 return 1;
481 }
482#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200483 return 0;
484 }
485
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100486usage:
487 printf("Usage:\n%s\n", cmdtp->usage);
488 return 1;
489}
490
491U_BOOT_CMD(nand, 5, 1, do_nand,
William Juulcfa460a2007-10-31 13:53:06 +0100492 "nand - NAND sub-system\n",
493 "info - show available NAND devices\n"
494 "nand device [dev] - show or set current device\n"
495 "nand read[.jffs2] - addr off|partition size\n"
496 "nand write[.jffs2] - addr off|partition size\n"
497 " read/write 'size' bytes starting at offset 'off'\n"
498 " to/from memory address 'addr'\n"
499 "nand erase [clean] [off size] - erase 'size' bytes from\n"
500 " offset 'off' (entire device if not specified)\n"
501 "nand bad - show bad blocks\n"
502 "nand dump[.oob] off - dump page\n"
503 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
504 "nand markbad off - mark bad block at offset (UNSAFE)\n"
505 "nand biterr off - make a bit error at offset (UNSAFE)\n"
506 "nand lock [tight] [status]\n"
507 " bring nand to lock state or display locked pages\n"
508 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100509
Stefan Roese856f0542006-10-28 15:55:52 +0200510static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
William Juulcfa460a2007-10-31 13:53:06 +0100511 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200512{
513 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200514 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200515 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200516 image_header_t *hdr;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200517 int jffs2 = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100518#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200519 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100520#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200521
522 s = strchr(cmd, '.');
523 if (s != NULL &&
524 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
525 jffs2 = 1;
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
William Juulcfa460a2007-10-31 13:53:06 +0100562 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200563 if (r) {
564 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200565 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200566 return 1;
567 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200568 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200569
Marian Balakowicz09475f72008-03-12 10:33:01 +0100570#if defined(CONFIG_FIT)
571 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200572 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
573 if (!fit_check_format (fit_hdr)) {
574 show_boot_progress (-150);
575 puts ("** Bad FIT image format\n");
576 return 1;
577 }
578 show_boot_progress (151);
579 fit_print_contents (fit_hdr);
580 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100581#endif
582
Stefan Roese856f0542006-10-28 15:55:52 +0200583 /* Loading ok, update default load address */
584
585 load_addr = addr;
586
587 /* Check if we should attempt an auto-start */
588 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
589 char *local_args[2];
590 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
591
592 local_args[0] = cmd;
593 local_args[1] = NULL;
594
595 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
596
597 do_bootm(cmdtp, 0, 1, local_args);
598 return 1;
599 }
600 return 0;
601}
602
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100603int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
604{
605 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200606 int idx;
607 ulong addr, offset = 0;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500608#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200609 struct mtd_device *dev;
610 struct part_info *part;
611 u8 pnum;
612
613 if (argc >= 2) {
614 char *p = (argc == 2) ? argv[1] : argv[2];
615 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
616 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
617 if (dev->id->type != MTD_DEV_TYPE_NAND) {
618 puts("Not a NAND device\n");
619 return 1;
620 }
621 if (argc > 3)
622 goto usage;
623 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200624 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200625 else
626 addr = CFG_LOAD_ADDR;
627 return nand_load_image(cmdtp, &nand_info[dev->id->num],
William Juulcfa460a2007-10-31 13:53:06 +0100628 part->offset, addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200629 }
630 }
631#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100632
Heiko Schocherfad63402007-07-13 09:54:17 +0200633 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100634 switch (argc) {
635 case 1:
636 addr = CFG_LOAD_ADDR;
637 boot_device = getenv("bootdevice");
638 break;
639 case 2:
640 addr = simple_strtoul(argv[1], NULL, 16);
641 boot_device = getenv("bootdevice");
642 break;
643 case 3:
644 addr = simple_strtoul(argv[1], NULL, 16);
645 boot_device = argv[2];
646 break;
647 case 4:
648 addr = simple_strtoul(argv[1], NULL, 16);
649 boot_device = argv[2];
650 offset = simple_strtoul(argv[3], NULL, 16);
651 break;
652 default:
Jon Loeligerc76fe472007-07-08 18:02:23 -0500653#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200654usage:
655#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100656 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200657 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100658 return 1;
659 }
660
Heiko Schocherfad63402007-07-13 09:54:17 +0200661 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100662 if (!boot_device) {
663 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200664 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100665 return 1;
666 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200667 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100668
Stefan Roese856f0542006-10-28 15:55:52 +0200669 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100670
Stefan Roese856f0542006-10-28 15:55:52 +0200671 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
672 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200673 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100674 return 1;
675 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200676 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100677
Stefan Roese856f0542006-10-28 15:55:52 +0200678 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100679}
680
681U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Stefan Roese856f0542006-10-28 15:55:52 +0200682 "nboot - boot from NAND device\n",
Thomas Knobloch10e03892007-07-06 14:58:39 +0200683 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100684
Jon Loeligerc76fe472007-07-08 18:02:23 -0500685#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100686
687#else /* CFG_NAND_LEGACY */
688/*
689 *
690 * Legacy NAND support - to be phased out
691 *
692 */
wdenkdc7c9a12003-03-26 06:55:25 +0000693#include <command.h>
694#include <malloc.h>
695#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000696#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000697
Heiko Schocherfad63402007-07-13 09:54:17 +0200698#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000699# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200700# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000701#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200702# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000703#endif
704
Jon Loeligerc76fe472007-07-08 18:02:23 -0500705#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100706#include <linux/mtd/nand_legacy.h>
707#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000708#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000709#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100710#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000711
wdenk1f4bb372003-07-27 00:21:01 +0000712#ifdef CONFIG_OMAP1510
713void archflashwp(void *archdata, int wp);
714#endif
715
William Juul3043c042007-11-14 14:28:11 +0100716#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
wdenk1f4bb372003-07-27 00:21:01 +0000717
William Juul3043c042007-11-14 14:28:11 +0100718#undef NAND_DEBUG
719#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000720
721/* ****************** WARNING *********************
722 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
723 * erase (or at least attempt to erase) blocks that are marked
724 * bad. This can be very handy if you are _sure_ that the block
725 * is OK, say because you marked a good block bad to test bad
726 * block handling and you are done testing, or if you have
727 * accidentally marked blocks bad.
728 *
729 * Erasing factory marked bad blocks is a _bad_ idea. If the
730 * erase succeeds there is no reliable way to find them again,
731 * and attempting to program or erase bad blocks can affect
732 * the data in _other_ (good) blocks.
733 */
William Juul3043c042007-11-14 14:28:11 +0100734#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000735
736#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000737#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000738
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100739/* bits for nand_legacy_rw() `cmd'; or together as needed */
William Juul5e1dae52007-11-09 13:32:30 +0100740#define NANDRW_READ 0x01
741#define NANDRW_WRITE 0x00
742#define NANDRW_JFFS2 0x02
William Juulcfa460a2007-10-31 13:53:06 +0100743#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000744
wdenkdc7c9a12003-03-26 06:55:25 +0000745/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100746 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000747 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100748extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
749extern int curr_device;
750extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100751 size_t len, int clean);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100752extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
William Juul5e1dae52007-11-09 13:32:30 +0100753 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100754extern void nand_print(struct nand_chip *nand);
755extern void nand_print_bad(struct nand_chip *nand);
756extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
William Juul3043c042007-11-14 14:28:11 +0100757 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100758extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
William Juul5e1dae52007-11-09 13:32:30 +0100759 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000760
wdenkdc7c9a12003-03-26 06:55:25 +0000761
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100762int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000763{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100764 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000765
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100766 switch (argc) {
767 case 0:
768 case 1:
769 printf ("Usage:\n%s\n", cmdtp->usage);
770 return 1;
771 case 2:
772 if (strcmp (argv[1], "info") == 0) {
773 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000774
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100775 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000776
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100777 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
778 if (nand_dev_desc[i].ChipID ==
779 NAND_ChipID_UNKNOWN)
780 continue; /* list only known devices */
781 printf ("Device %d: ", i);
782 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000783 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100784 return 0;
785
786 } else if (strcmp (argv[1], "device") == 0) {
787 if ((curr_device < 0)
788 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
789 puts ("\nno devices available\n");
790 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000791 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100792 printf ("\nDevice %d: ", curr_device);
793 nand_print (&nand_dev_desc[curr_device]);
794 return 0;
795
796 } else if (strcmp (argv[1], "bad") == 0) {
797 if ((curr_device < 0)
798 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
799 puts ("\nno devices available\n");
800 return 1;
801 }
802 printf ("\nDevice %d bad blocks:\n", curr_device);
803 nand_print_bad (&nand_dev_desc[curr_device]);
804 return 0;
805
806 }
807 printf ("Usage:\n%s\n", cmdtp->usage);
808 return 1;
809 case 3:
810 if (strcmp (argv[1], "device") == 0) {
811 int dev = (int) simple_strtoul (argv[2], NULL, 10);
812
813 printf ("\nDevice %d: ", dev);
814 if (dev >= CFG_MAX_NAND_DEVICE) {
815 puts ("unknown device\n");
816 return 1;
817 }
818 nand_print (&nand_dev_desc[dev]);
819 /*nand_print (dev); */
820
821 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
822 return 1;
823 }
824
825 curr_device = dev;
826
827 puts ("... is now current device\n");
828
829 return 0;
830 } else if (strcmp (argv[1], "erase") == 0
831 && strcmp (argv[2], "clean") == 0) {
832 struct nand_chip *nand = &nand_dev_desc[curr_device];
833 ulong off = 0;
834 ulong size = nand->totlen;
835 int ret;
836
837 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
838
839 ret = nand_legacy_erase (nand, off, size, 1);
840
841 printf ("%s\n", ret ? "ERROR" : "OK");
842
wdenk7a8e9bed2003-05-31 18:35:21 +0000843 return ret;
844 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100845
846 printf ("Usage:\n%s\n", cmdtp->usage);
847 return 1;
848 default:
849 /* at least 4 args */
850
851 if (strncmp (argv[1], "read", 4) == 0 ||
852 strncmp (argv[1], "write", 5) == 0) {
William Juul5e1dae52007-11-09 13:32:30 +0100853 ulong addr = simple_strtoul (argv[2], NULL, 16);
854 off_t off = simple_strtoul (argv[3], NULL, 16);
855 size_t size = simple_strtoul (argv[4], NULL, 16);
856 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
857 NANDRW_READ : NANDRW_WRITE;
Wolfgang Denkc71abba2008-04-28 14:55:12 +0200858 size_t total;
859 int ret;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100860 char *cmdtail = strchr (argv[1], '.');
861
862 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
863 /* read out-of-band data */
864 if (cmd & NANDRW_READ) {
865 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200866 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100867 (u_char *) addr);
868 } else {
869 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200870 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100871 (u_char *) addr);
872 }
873 return ret;
874 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
875 cmd |= NANDRW_JFFS2; /* skip bad blocks */
876 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
877 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
878 if (cmd & NANDRW_READ)
879 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
880 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000881#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100882 /* need ".e" same as ".j" for compatibility with older units */
883 else if (cmdtail && !strcmp (cmdtail, ".e"))
884 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000885#endif
stroesea842a6d2004-12-16 17:45:46 +0000886#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100887 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
888 /* ".i" for image -> read skips bad block (no 0xff) */
889 else if (cmdtail && !strcmp (cmdtail, ".i")) {
890 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
891 if (cmd & NANDRW_READ)
William Juul3043c042007-11-14 14:28:11 +0100892 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100893 }
stroesea842a6d2004-12-16 17:45:46 +0000894#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100895 else if (cmdtail) {
896 printf ("Usage:\n%s\n", cmdtp->usage);
897 return 1;
898 }
899
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200900 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100901 (cmd & NANDRW_READ) ? "read" : "write",
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200902 curr_device, off, (ulong)size);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100903
904 ret = nand_legacy_rw (nand_dev_desc + curr_device,
905 cmd, off, size,
William Juul3043c042007-11-14 14:28:11 +0100906 &total, (u_char *) addr);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100907
908 printf (" %d bytes %s: %s\n", total,
909 (cmd & NANDRW_READ) ? "read" : "written",
910 ret ? "ERROR" : "OK");
911
912 return ret;
913 } else if (strcmp (argv[1], "erase") == 0 &&
914 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
915 int clean = argc == 5;
916 ulong off =
917 simple_strtoul (argv[2 + clean], NULL, 16);
918 ulong size =
919 simple_strtoul (argv[3 + clean], NULL, 16);
920 int ret;
921
922 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
923 curr_device, off, size);
924
925 ret = nand_legacy_erase (nand_dev_desc + curr_device,
926 off, size, clean);
927
928 printf ("%s\n", ret ? "ERROR" : "OK");
929
930 return ret;
931 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000932 printf ("Usage:\n%s\n", cmdtp->usage);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100933 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000934 }
wdenkdc7c9a12003-03-26 06:55:25 +0000935
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100936 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000937 }
wdenkdc7c9a12003-03-26 06:55:25 +0000938}
939
wdenk0d498392003-07-01 21:06:45 +0000940U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +0100941 nand, 5, 1, do_nand,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200942 "nand - legacy NAND sub-system\n",
wdenkb0fce992003-06-29 21:03:46 +0000943 "info - show available NAND devices\n"
944 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000945 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000946 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
William Juul3043c042007-11-14 14:28:11 +0100947 " at offset `off' to/from memory address `addr'\n"
wdenkb0fce992003-06-29 21:03:46 +0000948 "nand erase [clean] [off size] - erase `size' bytes from\n"
William Juul3043c042007-11-14 14:28:11 +0100949 " offset `off' (entire device if not specified)\n"
wdenkb0fce992003-06-29 21:03:46 +0000950 "nand bad - show bad blocks\n"
951 "nand read.oob addr off size - read out-of-band data\n"
952 "nand write.oob addr off size - read out-of-band data\n"
953);
954
wdenkdc7c9a12003-03-26 06:55:25 +0000955int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
956{
957 char *boot_device = NULL;
958 char *ep;
959 int dev;
960 ulong cnt;
961 ulong addr;
962 ulong offset = 0;
963 image_header_t *hdr;
964 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100965#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200966 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100967#endif
968
Heiko Schocherfad63402007-07-13 09:54:17 +0200969 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000970 switch (argc) {
971 case 1:
972 addr = CFG_LOAD_ADDR;
973 boot_device = getenv ("bootdevice");
974 break;
975 case 2:
976 addr = simple_strtoul(argv[1], NULL, 16);
977 boot_device = getenv ("bootdevice");
978 break;
979 case 3:
980 addr = simple_strtoul(argv[1], NULL, 16);
981 boot_device = argv[2];
982 break;
983 case 4:
984 addr = simple_strtoul(argv[1], NULL, 16);
985 boot_device = argv[2];
986 offset = simple_strtoul(argv[3], NULL, 16);
987 break;
988 default:
989 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200990 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000991 return 1;
992 }
993
Heiko Schocherfad63402007-07-13 09:54:17 +0200994 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000995 if (!boot_device) {
996 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200997 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000998 return 1;
999 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001000 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +00001001
1002 dev = simple_strtoul(boot_device, &ep, 16);
1003
1004 if ((dev >= CFG_MAX_NAND_DEVICE) ||
William Juul3043c042007-11-14 14:28:11 +01001005 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001006 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001007 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +00001008 return 1;
1009 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001010 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +00001011
wdenk7a8e9bed2003-05-31 18:35:21 +00001012 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
William Juulcfa460a2007-10-31 13:53:06 +01001013 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1014 offset);
wdenkdc7c9a12003-03-26 06:55:25 +00001015
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001016 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
William Juulcfa460a2007-10-31 13:53:06 +01001017 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001018 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001019 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +00001020 return 1;
1021 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001022 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001023
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001024 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001025 case IMAGE_FORMAT_LEGACY:
1026 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001027 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001028
Marian Balakowicz09475f72008-03-12 10:33:01 +01001029 cnt = image_get_image_size (hdr);
1030 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001031 break;
1032#if defined(CONFIG_FIT)
1033 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001034 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001035 puts ("Fit image detected...\n");
1036
1037 cnt = fit_get_size (fit_hdr);
1038 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001039#endif
1040 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001041 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001042 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001043 return 1;
1044 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001045 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001046
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001047 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
William Juulcfa460a2007-10-31 13:53:06 +01001048 offset + SECTORSIZE, cnt, NULL,
1049 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001050 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001051 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001052 return 1;
1053 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001054 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001055
Marian Balakowicz09475f72008-03-12 10:33:01 +01001056#if defined(CONFIG_FIT)
1057 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +02001058 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1059 if (!fit_check_format (fit_hdr)) {
1060 show_boot_progress (-150);
1061 puts ("** Bad FIT image format\n");
1062 return 1;
1063 }
1064 show_boot_progress (151);
1065 fit_print_contents (fit_hdr);
1066 }
Marian Balakowicz09475f72008-03-12 10:33:01 +01001067#endif
1068
wdenkdc7c9a12003-03-26 06:55:25 +00001069 /* Loading ok, update default load address */
1070
1071 load_addr = addr;
1072
1073 /* Check if we should attempt an auto-start */
1074 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1075 char *local_args[2];
1076 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1077
1078 local_args[0] = argv[0];
1079 local_args[1] = NULL;
1080
wdenk7a8e9bed2003-05-31 18:35:21 +00001081 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001082
1083 do_bootm (cmdtp, 0, 1, local_args);
1084 rcode = 1;
1085 }
1086 return rcode;
1087}
1088
wdenk0d498392003-07-01 21:06:45 +00001089U_BOOT_CMD(
William Juul3043c042007-11-14 14:28:11 +01001090 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +00001091 "nboot - boot from NAND device\n",
1092 "loadAddr dev\n"
1093);
1094
Jon Loeligerc76fe472007-07-08 18:02:23 -05001095#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001096
1097#endif /* CFG_NAND_LEGACY */