blob: 710ba8f9460fafd058d9185eacf1bd13700d60b4 [file] [log] [blame]
wdenkc8434db2003-03-26 06:55:25 +00001/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
wdenk6d3c6d12005-04-03 22:35:21 +00006 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
wdenkc8434db2003-03-26 06:55:25 +00009 */
10
11#include <common.h>
Bartlomiej Sieka582f1a32006-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 Juul52c07962007-10-31 13:53:06 +010021#include <linux/mtd/mtd.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010022
Jon Loeligerd76b5c12007-07-08 18:02:23 -050023#if defined(CONFIG_CMD_NAND)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010024
25#include <command.h>
26#include <watchdog.h>
27#include <malloc.h>
28#include <asm/byteorder.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010029#include <jffs2/jffs2.h>
30#include <nand.h>
31
Jon Loeligerd76b5c12007-07-08 18:02:23 -050032#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-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 Juul9e9c2c12007-11-09 13:32:30 +010038 u8 *part_num, struct part_info **part);
Stefan Roese198b23e2006-10-28 15:55:52 +020039#endif
40
William Juule7ebe6e2007-12-14 16:33:45 +010041static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010042{
43 int i;
William Juule7ebe6e2007-12-14 16:33:45 +010044 u_char *datbuf, *oobbuf, *p;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010045
William Juule7ebe6e2007-12-14 16:33:45 +010046 datbuf = malloc(nand->writesize + nand->oobsize);
47 oobbuf = malloc(nand->oobsize);
48 if (!datbuf || !oobbuf) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010049 puts("No memory for page buffer\n");
50 return 1;
51 }
William Juul52c07962007-10-31 13:53:06 +010052 off &= ~(nand->writesize - 1);
William Juul52c07962007-10-31 13:53:06 +010053 loff_t addr = (loff_t) off;
William Juule7ebe6e2007-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 Sieka582f1a32006-03-05 18:57:33 +010062 if (i < 0) {
Stefan Roese3167d072008-07-10 10:10:54 +020063 printf("Error (%d) reading page %08lx\n", i, off);
William Juule7ebe6e2007-12-14 16:33:45 +010064 free(datbuf);
65 free(oobbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010066 return 1;
67 }
Stefan Roese3167d072008-07-10 10:10:54 +020068 printf("Page %08lx dump:\n", off);
William Juule7ebe6e2007-12-14 16:33:45 +010069 i = nand->writesize >> 4;
70 p = datbuf;
71
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010072 while (i--) {
William Juule7ebe6e2007-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 Sieka582f1a32006-03-05 18:57:33 +010078 p += 16;
79 }
80 puts("OOB:\n");
81 i = nand->oobsize >> 3;
82 while (i--) {
William Juul52c07962007-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 Sieka582f1a32006-03-05 18:57:33 +010085 p += 8;
86 }
William Juule7ebe6e2007-12-14 16:33:45 +010087 free(datbuf);
88 free(oobbuf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010089
90 return 0;
91}
92
93/* ------------------------------------------------------------------------- */
94
Stefan Roese198b23e2006-10-28 15:55:52 +020095static inline int str2long(char *p, ulong *num)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010096{
Stefan Roese198b23e2006-10-28 15:55:52 +020097 char *endptr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010098
Stefan Roese198b23e2006-10-28 15:55:52 +020099 *num = simple_strtoul(p, &endptr, 16);
100 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
101}
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100102
Stefan Roese198b23e2006-10-28 15:55:52 +0200103static int
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200104arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese198b23e2006-10-28 15:55:52 +0200105{
106 int idx = nand_curr_device;
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500107#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +0200108 struct mtd_device *dev;
109 struct part_info *part;
110 u8 pnum;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100111
Stefan Roese198b23e2006-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 Denk7f794542008-04-30 17:46:26 +0200121 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese198b23e2006-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 Sieka582f1a32006-03-05 18:57:33 +0100133 }
Stefan Roese198b23e2006-10-28 15:55:52 +0200134 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100135#endif
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100136
Stefan Roese198b23e2006-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;
144 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100145
Stefan Roese198b23e2006-10-28 15:55:52 +0200146 if (argc >= 2) {
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200147 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese198b23e2006-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;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100153 }
154
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500155#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-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 Roese3167d072008-07-10 10:10:54 +0200162 printf("offset 0x%lx, size 0x%x\n", *off, *size);
Stefan Roese198b23e2006-10-28 15:55:52 +0200163 return 0;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100164}
165
166int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
167{
William Juul52c07962007-10-31 13:53:06 +0100168 int i, dev, ret = 0;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200169 ulong addr, off;
170 size_t size;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100171 char *cmd, *s;
172 nand_info_t *nand;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200173#ifdef CFG_NAND_QUIET
174 int quiet = CFG_NAND_QUIET;
175#else
Stefan Roesed351b2b2006-10-10 12:36:02 +0200176 int quiet = 0;
Matthias Fuchsd42fff22007-09-12 12:36:53 +0200177#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200178 const char *quiet_str = getenv("quiet");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100179
180 /* at least two arguments please */
181 if (argc < 2)
182 goto usage;
183
Stefan Roesed351b2b2006-10-10 12:36:02 +0200184 if (quiet_str)
185 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
186
Bartlomiej Sieka582f1a32006-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 Roese3167d072008-07-10 10:10:54 +0200194 printf("Device %d: %s, sector size %u KiB\n",
William Juul52c07962007-10-31 13:53:06 +0100195 i, nand_info[i].name,
196 nand_info[i].erasesize >> 10);
Bartlomiej Sieka582f1a32006-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 Juulbfb83322007-11-14 14:28:11 +0100205 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100206 puts("\nno devices available\n");
207 else
208 printf("\nDevice %d: %s\n", nand_curr_device,
William Juul52c07962007-10-31 13:53:06 +0100209 nand_info[nand_curr_device].name);
Bartlomiej Sieka582f1a32006-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 Roese3cdd3fd2006-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 Sieka582f1a32006-03-05 18:57:33 +0100228 return 0;
229 }
230
231 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
William Juulbfb83322007-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 Sieka582f1a32006-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 Roese3167d072008-07-10 10:10:54 +0200251 printf(" %08lx\n", off);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100252 return 0;
253 }
254
Stefan Roese198b23e2006-10-28 15:55:52 +0200255 /*
256 * Syntax is:
257 * 0 1 2 3 4
258 * nand erase [clean] [off size]
259 */
Stefan Roesed351b2b2006-10-10 12:36:02 +0200260 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
261 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200262 /* "clean" at index 2 means request to write cleanmarker */
William Juulbfb83322007-11-14 14:28:11 +0100263 int clean = argc > 2 && !strcmp("clean", argv[2]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200264 int o = clean ? 3 : 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200265 int scrub = !strcmp(cmd, "scrub");
266
Stefan Roese198b23e2006-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 Roesed351b2b2006-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 Roese198b23e2006-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 Roesed351b2b2006-10-10 12:36:02 +0200290
291 if (getc() == 'y' && getc() == '\r') {
292 opts.scrub = 1;
293 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200294 puts("scrub aborted\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200295 return -1;
296 }
297 }
298 ret = nand_erase_opts(nand, &opts);
Bartlomiej Sieka582f1a32006-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 Juule7ebe6e2007-12-14 16:33:45 +0100312 ret = nand_dump(nand, off, 1);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100313 else
William Juule7ebe6e2007-12-14 16:33:45 +0100314 ret = nand_dump(nand, off, 0);
Bartlomiej Sieka582f1a32006-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 Roesed351b2b2006-10-10 12:36:02 +0200322 int read;
323
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100324 if (argc < 4)
325 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200326
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100327 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
328
Stefan Roesed351b2b2006-10-10 12:36:02 +0200329 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-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 Juulb76ec382007-11-08 10:39:53 +0100333
Stefan Roesed351b2b2006-10-10 12:36:02 +0200334 s = strchr(cmd, '.');
335 if (s != NULL &&
William Juulbfb83322007-11-14 14:28:11 +0100336 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200337 if (read) {
338 /* read */
339 nand_read_options_t opts;
340 memset(&opts, 0, sizeof(opts));
William Juul52c07962007-10-31 13:53:06 +0100341 opts.buffer = (u_char*) addr;
342 opts.length = size;
343 opts.offset = off;
344 opts.quiet = quiet;
William Juulbfb83322007-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 Roesed351b2b2006-10-10 12:36:02 +0200352 } else {
353 /* write */
William Juul52c07962007-10-31 13:53:06 +0100354 mtd_oob_ops_t opts;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200355 memset(&opts, 0, sizeof(opts));
William Juul52c07962007-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 Roesed351b2b2006-10-10 12:36:02 +0200361 }
Harald Welte5a395102007-12-19 14:12:53 +0100362 } else if (s != NULL && !strcmp(s, ".oob")) {
William Juul52c07962007-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 Welte5a395102007-12-19 14:12:53 +0100370 if (read)
William Juul52c07962007-10-31 13:53:06 +0100371 ret = nand->read_oob(nand, off, &ops);
Harald Welte5a395102007-12-19 14:12:53 +0100372 else
William Juul52c07962007-10-31 13:53:06 +0100373 ret = nand->write_oob(nand, off, &ops);
Stefan Roese198b23e2006-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 Roesed351b2b2006-10-10 12:36:02 +0200379 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100380
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100381 printf(" %d bytes %s: %s\n", size,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200382 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100383
384 return ret == 0 ? 0 : 1;
385 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200386
Stefan Roesed351b2b2006-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 Juulbfb83322007-11-14 14:28:11 +0100415/*
416 * ! BROKEN !
Stefan Roese576625d2008-01-05 16:50:32 +0100417 *
William Juulbfb83322007-11-14 14:28:11 +0100418 * TODO: must be implemented and tested by someone with HW
419 */
420#if 0
Stefan Roesed351b2b2006-10-10 12:36:02 +0200421 if (status) {
William Juulbfb83322007-11-14 14:28:11 +0100422 ulong block_start = 0;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200423 ulong off;
William Juul9e9c2c12007-11-09 13:32:30 +0100424 int last_status = -1;
William Juulbfb83322007-11-14 14:28:11 +0100425
Stefan Roesed351b2b2006-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 Juul9e9c2c12007-11-09 13:32:30 +0100431 "NOT " : ""));
Stefan Roesed351b2b2006-10-10 12:36:02 +0200432
William Juul52c07962007-10-31 13:53:06 +0100433 for (off = 0; off < nand->size; off += nand->writesize) {
William Juul9e9c2c12007-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 Juulbfb83322007-11-14 14:28:11 +0100440 || (s != last_status && off != 0)) {
William Juul9e9c2c12007-11-09 13:32:30 +0100441
442 printf("%08lx - %08lx: %8d pages %s%s%s\n",
William Juulbfb83322007-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 Juul9e9c2c12007-11-09 13:32:30 +0100449 }
450
451 last_status = s;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200452 }
William Juul52c07962007-10-31 13:53:06 +0100453 } else {
William Juul9e9c2c12007-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 Roesed351b2b2006-10-10 12:36:02 +0200460 }
William Juulbfb83322007-11-14 14:28:11 +0100461#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200462 return 0;
463 }
464
465 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200466 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
467 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200468
William Juulbfb83322007-11-14 14:28:11 +0100469/*
470 * ! BROKEN !
Stefan Roese576625d2008-01-05 16:50:32 +0100471 *
William Juulbfb83322007-11-14 14:28:11 +0100472 * TODO: must be implemented and tested by someone with HW
473 */
474#if 0
William Juul9e9c2c12007-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 Juulbfb83322007-11-14 14:28:11 +0100479 "write and erase will probably fail\n");
William Juul9e9c2c12007-11-09 13:32:30 +0100480 return 1;
481 }
482#endif
Stefan Roesed351b2b2006-10-10 12:36:02 +0200483 return 0;
484 }
485
Bartlomiej Sieka582f1a32006-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 Juul52c07962007-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 Sieka582f1a32006-03-05 18:57:33 +0100509
Stefan Roese198b23e2006-10-28 15:55:52 +0200510static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
William Juul52c07962007-10-31 13:53:06 +0100511 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100512{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100513 int r;
Thomas Knobloche84b7772007-07-06 14:58:39 +0200514 char *ep, *s;
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200515 size_t cnt;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100516 image_header_t *hdr;
Thomas Knobloche84b7772007-07-06 14:58:39 +0200517 int jffs2 = 0;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100518#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200519 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100520#endif
Thomas Knobloche84b7772007-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;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100526
Stefan Roese198b23e2006-10-28 15:55:52 +0200527 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100528
William Juul52c07962007-10-31 13:53:06 +0100529 cnt = nand->writesize;
530 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100531 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200532 puts("** Read error\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200533 show_boot_progress (-56);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100534 return 1;
535 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200536 show_boot_progress (56);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100537
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100538 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100539 case IMAGE_FORMAT_LEGACY:
540 hdr = (image_header_t *)addr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100541
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100542 show_boot_progress (57);
Marian Balakowiczdbdd16a2008-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 Balakowicze6bdd342008-03-12 10:33:01 +0100549 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100550 puts ("Fit image detected...\n");
551
552 cnt = fit_get_size (fit_hdr);
553 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100554#endif
555 default:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100556 show_boot_progress (-57);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100557 puts ("** Unknown image type\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100558 return 1;
559 }
William Juulbfb83322007-11-14 14:28:11 +0100560 show_boot_progress (57);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100561
William Juul52c07962007-10-31 13:53:06 +0100562 r = nand_read(nand, offset, &cnt, (u_char *) addr);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100563 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200564 puts("** Read error\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200565 show_boot_progress (-58);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100566 return 1;
567 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200568 show_boot_progress (58);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100569
Marian Balakowicze6bdd342008-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 Balakowiczcefb4022008-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 Balakowicze6bdd342008-03-12 10:33:01 +0100581#endif
582
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100583 /* 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
Stefan Roese198b23e2006-10-28 15:55:52 +0200592 local_args[0] = cmd;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100593 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
Stefan Roese198b23e2006-10-28 15:55:52 +0200603int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
604{
605 char *boot_device = NULL;
606 int idx;
607 ulong addr, offset = 0;
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500608#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-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 Knobloche84b7772007-07-06 14:58:39 +0200624 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese198b23e2006-10-28 15:55:52 +0200625 else
626 addr = CFG_LOAD_ADDR;
627 return nand_load_image(cmdtp, &nand_info[dev->id->num],
William Juul52c07962007-10-31 13:53:06 +0100628 part->offset, addr, argv[0]);
Stefan Roese198b23e2006-10-28 15:55:52 +0200629 }
630 }
631#endif
632
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200633 show_boot_progress(52);
Stefan Roese198b23e2006-10-28 15:55:52 +0200634 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 Loeligerd76b5c12007-07-08 18:02:23 -0500653#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese198b23e2006-10-28 15:55:52 +0200654usage:
655#endif
656 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200657 show_boot_progress(-53);
Stefan Roese198b23e2006-10-28 15:55:52 +0200658 return 1;
659 }
660
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200661 show_boot_progress(53);
Stefan Roese198b23e2006-10-28 15:55:52 +0200662 if (!boot_device) {
663 puts("\n** No boot device **\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200664 show_boot_progress(-54);
Stefan Roese198b23e2006-10-28 15:55:52 +0200665 return 1;
666 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200667 show_boot_progress(54);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100668
Stefan Roese198b23e2006-10-28 15:55:52 +0200669 idx = simple_strtoul(boot_device, NULL, 16);
670
671 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
672 printf("\n** Device %d not available\n", idx);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200673 show_boot_progress(-55);
Stefan Roese198b23e2006-10-28 15:55:52 +0200674 return 1;
675 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200676 show_boot_progress(55);
Stefan Roese198b23e2006-10-28 15:55:52 +0200677
678 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
679}
680
681U_BOOT_CMD(nboot, 4, 1, do_nandboot,
682 "nboot - boot from NAND device\n",
Thomas Knobloche84b7772007-07-06 14:58:39 +0200683 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100684
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500685#endif
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100686
687#else /* CFG_NAND_LEGACY */
688/*
689 *
690 * Legacy NAND support - to be phased out
691 *
692 */
wdenkc8434db2003-03-26 06:55:25 +0000693#include <command.h>
694#include <malloc.h>
695#include <asm/io.h>
wdenk145d2c12004-04-15 21:48:45 +0000696#include <watchdog.h>
wdenkc8434db2003-03-26 06:55:25 +0000697
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200698#ifdef CONFIG_show_boot_progress
wdenkc8434db2003-03-26 06:55:25 +0000699# include <status_led.h>
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200700# define show_boot_progress(arg) show_boot_progress(arg)
wdenkc8434db2003-03-26 06:55:25 +0000701#else
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200702# define show_boot_progress(arg)
wdenkc8434db2003-03-26 06:55:25 +0000703#endif
704
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500705#if defined(CONFIG_CMD_NAND)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100706#include <linux/mtd/nand_legacy.h>
707#if 0
wdenkc8434db2003-03-26 06:55:25 +0000708#include <linux/mtd/nand_ids.h>
wdenkabda5ca2003-05-31 18:35:21 +0000709#include <jffs2/jffs2.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100710#endif
wdenkc8434db2003-03-26 06:55:25 +0000711
wdenke58b0dc2003-07-27 00:21:01 +0000712#ifdef CONFIG_OMAP1510
713void archflashwp(void *archdata, int wp);
714#endif
715
William Juulbfb83322007-11-14 14:28:11 +0100716#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
wdenke58b0dc2003-07-27 00:21:01 +0000717
William Juulbfb83322007-11-14 14:28:11 +0100718#undef NAND_DEBUG
719#undef PSYCHO_DEBUG
wdenkabda5ca2003-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 Juulbfb83322007-11-14 14:28:11 +0100734#define ALLOW_ERASE_BAD_DEBUG 0
wdenkc8434db2003-03-26 06:55:25 +0000735
736#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenke58b0dc2003-07-27 00:21:01 +0000737#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkc8434db2003-03-26 06:55:25 +0000738
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100739/* bits for nand_legacy_rw() `cmd'; or together as needed */
William Juul9e9c2c12007-11-09 13:32:30 +0100740#define NANDRW_READ 0x01
741#define NANDRW_WRITE 0x00
742#define NANDRW_JFFS2 0x02
William Juul52c07962007-10-31 13:53:06 +0100743#define NANDRW_JFFS2_SKIP 0x04
wdenkabda5ca2003-05-31 18:35:21 +0000744
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100745/*
746 * Imports from nand_legacy.c
747 */
748extern 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 Juulbfb83322007-11-14 14:28:11 +0100751 size_t len, int clean);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100752extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
William Juul9e9c2c12007-11-09 13:32:30 +0100753 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Sieka582f1a32006-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 Juulbfb83322007-11-14 14:28:11 +0100757 size_t len, size_t *retlen, u_char *buf);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100758extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
William Juul9e9c2c12007-11-09 13:32:30 +0100759 size_t len, size_t *retlen, const u_char *buf);
wdenkc8434db2003-03-26 06:55:25 +0000760
wdenkc8434db2003-03-26 06:55:25 +0000761
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100762int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkc8434db2003-03-26 06:55:25 +0000763{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100764 int rcode = 0;
wdenkc8434db2003-03-26 06:55:25 +0000765
Wolfgang Denk87b3d4b2006-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;
wdenkc8434db2003-03-26 06:55:25 +0000774
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100775 putc ('\n');
wdenkc8434db2003-03-26 06:55:25 +0000776
Wolfgang Denk87b3d4b2006-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]);
783 }
784 return 0;
wdenkabda5ca2003-05-31 18:35:21 +0000785
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100786 } 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;
791 }
792 printf ("\nDevice %d: ", curr_device);
793 nand_print (&nand_dev_desc[curr_device]);
794 return 0;
wdenkabda5ca2003-05-31 18:35:21 +0000795
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100796 } 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;
wdenkc8434db2003-03-26 06:55:25 +0000805
wdenkc8434db2003-03-26 06:55:25 +0000806 }
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100807 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);
wdenkc8434db2003-03-26 06:55:25 +0000812
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100813 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); */
wdenkc8434db2003-03-26 06:55:25 +0000820
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100821 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
822 return 1;
823 }
wdenkc8434db2003-03-26 06:55:25 +0000824
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100825 curr_device = dev;
wdenkc8434db2003-03-26 06:55:25 +0000826
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100827 puts ("... is now current device\n");
wdenkabda5ca2003-05-31 18:35:21 +0000828
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100829 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;
wdenkabda5ca2003-05-31 18:35:21 +0000836
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100837 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
wdenkabda5ca2003-05-31 18:35:21 +0000838
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100839 ret = nand_legacy_erase (nand, off, size, 1);
wdenkabda5ca2003-05-31 18:35:21 +0000840
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100841 printf ("%s\n", ret ? "ERROR" : "OK");
wdenkc8434db2003-03-26 06:55:25 +0000842
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100843 return ret;
844 }
wdenkc8434db2003-03-26 06:55:25 +0000845
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100846 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 Juul9e9c2c12007-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 Denk885e86f2008-04-28 14:55:12 +0200858 size_t total;
859 int ret;
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100860 char *cmdtail = strchr (argv[1], '.');
wdenkabda5ca2003-05-31 18:35:21 +0000861
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100862 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 Denk9a11ccd2008-04-28 12:08:18 +0200866 off, size, &total,
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100867 (u_char *) addr);
868 } else {
869 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk9a11ccd2008-04-28 12:08:18 +0200870 off, size, &total,
Wolfgang Denk87b3d4b2006-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) */
wdenkabda5ca2003-05-31 18:35:21 +0000880 }
wdenkabda5ca2003-05-31 18:35:21 +0000881#ifdef SXNI855T
Wolfgang Denk87b3d4b2006-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 */
wdenkabda5ca2003-05-31 18:35:21 +0000885#endif
stroese317cbe62004-12-16 17:45:46 +0000886#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denk87b3d4b2006-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 Juulbfb83322007-11-14 14:28:11 +0100892 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100893 }
stroese317cbe62004-12-16 17:45:46 +0000894#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100895 else if (cmdtail) {
896 printf ("Usage:\n%s\n", cmdtp->usage);
897 return 1;
898 }
wdenkc8434db2003-03-26 06:55:25 +0000899
Wolfgang Denk12cec0a2008-07-11 01:16:00 +0200900 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100901 (cmd & NANDRW_READ) ? "read" : "write",
Wolfgang Denk12cec0a2008-07-11 01:16:00 +0200902 curr_device, off, (ulong)size);
wdenkc8434db2003-03-26 06:55:25 +0000903
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100904 ret = nand_legacy_rw (nand_dev_desc + curr_device,
905 cmd, off, size,
William Juulbfb83322007-11-14 14:28:11 +0100906 &total, (u_char *) addr);
wdenkc8434db2003-03-26 06:55:25 +0000907
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100908 printf (" %d bytes %s: %s\n", total,
909 (cmd & NANDRW_READ) ? "read" : "written",
910 ret ? "ERROR" : "OK");
wdenkc8434db2003-03-26 06:55:25 +0000911
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100912 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;
wdenkc8434db2003-03-26 06:55:25 +0000921
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100922 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
923 curr_device, off, size);
wdenkc8434db2003-03-26 06:55:25 +0000924
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100925 ret = nand_legacy_erase (nand_dev_desc + curr_device,
926 off, size, clean);
wdenkc8434db2003-03-26 06:55:25 +0000927
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100928 printf ("%s\n", ret ? "ERROR" : "OK");
wdenkc8434db2003-03-26 06:55:25 +0000929
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100930 return ret;
931 } else {
932 printf ("Usage:\n%s\n", cmdtp->usage);
933 rcode = 1;
934 }
wdenkc8434db2003-03-26 06:55:25 +0000935
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100936 return rcode;
937 }
wdenkc8434db2003-03-26 06:55:25 +0000938}
939
wdenkf287a242003-07-01 21:06:45 +0000940U_BOOT_CMD(
William Juulbfb83322007-11-14 14:28:11 +0100941 nand, 5, 1, do_nand,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200942 "nand - legacy NAND sub-system\n",
wdenkf12e3962003-06-29 21:03:46 +0000943 "info - show available NAND devices\n"
944 "nand device [dev] - show or set current device\n"
wdenk145d2c12004-04-15 21:48:45 +0000945 "nand read[.jffs2[s]] addr off size\n"
wdenkf12e3962003-06-29 21:03:46 +0000946 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
William Juulbfb83322007-11-14 14:28:11 +0100947 " at offset `off' to/from memory address `addr'\n"
wdenkf12e3962003-06-29 21:03:46 +0000948 "nand erase [clean] [off size] - erase `size' bytes from\n"
William Juulbfb83322007-11-14 14:28:11 +0100949 " offset `off' (entire device if not specified)\n"
wdenkf12e3962003-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
wdenkc8434db2003-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 Balakowicze6bdd342008-03-12 10:33:01 +0100965#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200966 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100967#endif
968
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200969 show_boot_progress (52);
wdenkc8434db2003-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 Schocher8a8ec532007-07-13 09:54:17 +0200990 show_boot_progress (-53);
wdenkc8434db2003-03-26 06:55:25 +0000991 return 1;
992 }
993
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200994 show_boot_progress (53);
wdenkc8434db2003-03-26 06:55:25 +0000995 if (!boot_device) {
996 puts ("\n** No boot device **\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200997 show_boot_progress (-54);
wdenkc8434db2003-03-26 06:55:25 +0000998 return 1;
999 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001000 show_boot_progress (54);
wdenkc8434db2003-03-26 06:55:25 +00001001
1002 dev = simple_strtoul(boot_device, &ep, 16);
1003
1004 if ((dev >= CFG_MAX_NAND_DEVICE) ||
William Juulbfb83322007-11-14 14:28:11 +01001005 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
wdenkc8434db2003-03-26 06:55:25 +00001006 printf ("\n** Device %d not available\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001007 show_boot_progress (-55);
wdenkc8434db2003-03-26 06:55:25 +00001008 return 1;
1009 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001010 show_boot_progress (55);
wdenkc8434db2003-03-26 06:55:25 +00001011
wdenkabda5ca2003-05-31 18:35:21 +00001012 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
William Juul52c07962007-10-31 13:53:06 +01001013 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1014 offset);
wdenkc8434db2003-03-26 06:55:25 +00001015
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001016 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
William Juul52c07962007-10-31 13:53:06 +01001017 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkc8434db2003-03-26 06:55:25 +00001018 printf ("** Read error on %d\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001019 show_boot_progress (-56);
wdenkc8434db2003-03-26 06:55:25 +00001020 return 1;
1021 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001022 show_boot_progress (56);
wdenkc8434db2003-03-26 06:55:25 +00001023
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001024 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001025 case IMAGE_FORMAT_LEGACY:
1026 hdr = (image_header_t *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001027 image_print_contents (hdr);
wdenkc8434db2003-03-26 06:55:25 +00001028
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001029 cnt = image_get_image_size (hdr);
1030 cnt -= SECTORSIZE;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001031 break;
1032#if defined(CONFIG_FIT)
1033 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001034 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001035 puts ("Fit image detected...\n");
1036
1037 cnt = fit_get_size (fit_hdr);
1038 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001039#endif
1040 default:
Marian Balakowicze6bdd342008-03-12 10:33:01 +01001041 show_boot_progress (-57);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001042 puts ("** Unknown image type\n");
wdenkc8434db2003-03-26 06:55:25 +00001043 return 1;
1044 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001045 show_boot_progress (57);
wdenkc8434db2003-03-26 06:55:25 +00001046
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001047 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
William Juul52c07962007-10-31 13:53:06 +01001048 offset + SECTORSIZE, cnt, NULL,
1049 (u_char *)(addr+SECTORSIZE))) {
wdenkc8434db2003-03-26 06:55:25 +00001050 printf ("** Read error on %d\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001051 show_boot_progress (-58);
wdenkc8434db2003-03-26 06:55:25 +00001052 return 1;
1053 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +02001054 show_boot_progress (58);
wdenkc8434db2003-03-26 06:55:25 +00001055
Marian Balakowicze6bdd342008-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 Balakowiczcefb4022008-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 Balakowicze6bdd342008-03-12 10:33:01 +01001067#endif
1068
wdenkc8434db2003-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
wdenkabda5ca2003-05-31 18:35:21 +00001081 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkc8434db2003-03-26 06:55:25 +00001082
1083 do_bootm (cmdtp, 0, 1, local_args);
1084 rcode = 1;
1085 }
1086 return rcode;
1087}
1088
wdenkf287a242003-07-01 21:06:45 +00001089U_BOOT_CMD(
William Juulbfb83322007-11-14 14:28:11 +01001090 nboot, 4, 1, do_nandboot,
wdenkf12e3962003-06-29 21:03:46 +00001091 "nboot - boot from NAND device\n",
1092 "loadAddr dev\n"
1093);
1094
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001095#endif
wdenkc8434db2003-03-26 06:55:25 +00001096
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +01001097#endif /* CFG_NAND_LEGACY */