blob: cb62661ec2703375099409ffaf71f3826d07bafa [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>
21
22#if (CONFIG_COMMANDS & CFG_CMD_NAND)
23
24#include <command.h>
25#include <watchdog.h>
26#include <malloc.h>
27#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010028#include <jffs2/jffs2.h>
29#include <nand.h>
30
Stefan Roese856f0542006-10-28 15:55:52 +020031#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
32
33/* parition handling routines */
34int mtdparts_init(void);
35int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36int find_dev_and_part(const char *id, struct mtd_device **dev,
37 u8 *part_num, struct part_info **part);
38#endif
39
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010040extern nand_info_t nand_info[]; /* info for NAND chips */
41
42static int nand_dump_oob(nand_info_t *nand, ulong off)
43{
44 return 0;
45}
46
47static int nand_dump(nand_info_t *nand, ulong off)
48{
49 int i;
50 u_char *buf, *p;
51
52 buf = malloc(nand->oobblock + nand->oobsize);
53 if (!buf) {
54 puts("No memory for page buffer\n");
55 return 1;
56 }
57 off &= ~(nand->oobblock - 1);
58 i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
59 if (i < 0) {
60 printf("Error (%d) reading page %08x\n", i, off);
61 free(buf);
62 return 1;
63 }
64 printf("Page %08x dump:\n", off);
65 i = nand->oobblock >> 4; p = buf;
66 while (i--) {
67 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
68 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
69 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
70 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
71 p += 16;
72 }
73 puts("OOB:\n");
74 i = nand->oobsize >> 3;
75 while (i--) {
76 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
77 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
78 p += 8;
79 }
80 free(buf);
81
82 return 0;
83}
84
85/* ------------------------------------------------------------------------- */
86
Stefan Roese856f0542006-10-28 15:55:52 +020087static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010088{
Stefan Roese856f0542006-10-28 15:55:52 +020089 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010090
Stefan Roese856f0542006-10-28 15:55:52 +020091 *num = simple_strtoul(p, &endptr, 16);
92 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
93}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010094
Stefan Roese856f0542006-10-28 15:55:52 +020095static int
96arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
97{
98 int idx = nand_curr_device;
99#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
100 struct mtd_device *dev;
101 struct part_info *part;
102 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100103
Stefan Roese856f0542006-10-28 15:55:52 +0200104 if (argc >= 1 && !(str2long(argv[0], off))) {
105 if ((mtdparts_init() == 0) &&
106 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
107 if (dev->id->type != MTD_DEV_TYPE_NAND) {
108 puts("not a NAND device\n");
109 return -1;
110 }
111 *off = part->offset;
112 if (argc >= 2) {
113 if (!(str2long(argv[1], size))) {
114 printf("'%s' is not a number\n", argv[1]);
115 return -1;
116 }
117 if (*size > part->size)
118 *size = part->size;
119 } else {
120 *size = part->size;
121 }
122 idx = dev->id->num;
123 *nand = nand_info[idx];
124 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100125 }
Stefan Roese856f0542006-10-28 15:55:52 +0200126 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100127#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100128
Stefan Roese856f0542006-10-28 15:55:52 +0200129 if (argc >= 1) {
130 if (!(str2long(argv[0], off))) {
131 printf("'%s' is not a number\n", argv[0]);
132 return -1;
133 }
134 } else {
135 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100136 }
137
Stefan Roese856f0542006-10-28 15:55:52 +0200138 if (argc >= 2) {
139 if (!(str2long(argv[1], size))) {
140 printf("'%s' is not a number\n", argv[1]);
141 return -1;
142 }
143 } else {
144 *size = nand->size - *off;
145 }
146
147#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
148out:
149#endif
150 printf("device %d ", idx);
151 if (*size == nand->size)
152 puts("whole chip\n");
153 else
154 printf("offset 0x%x, size 0x%x\n", *off, *size);
155 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100156}
157
158int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
159{
160 int i, dev, ret;
161 ulong addr, off, size;
162 char *cmd, *s;
163 nand_info_t *nand;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200164 int quiet = 0;
165 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100166
167 /* at least two arguments please */
168 if (argc < 2)
169 goto usage;
170
Stefan Roese2255b2d2006-10-10 12:36:02 +0200171 if (quiet_str)
172 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
173
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100174 cmd = argv[1];
175
176 if (strcmp(cmd, "info") == 0) {
177
178 putc('\n');
179 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
180 if (nand_info[i].name)
181 printf("Device %d: %s, sector size %lu KiB\n",
182 i, nand_info[i].name,
183 nand_info[i].erasesize >> 10);
184 }
185 return 0;
186 }
187
188 if (strcmp(cmd, "device") == 0) {
189
190 if (argc < 3) {
191 if ((nand_curr_device < 0) ||
192 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
193 puts("\nno devices available\n");
194 else
195 printf("\nDevice %d: %s\n", nand_curr_device,
196 nand_info[nand_curr_device].name);
197 return 0;
198 }
199 dev = (int)simple_strtoul(argv[2], NULL, 10);
200 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
201 puts("No such device\n");
202 return 1;
203 }
204 printf("Device %d: %s", dev, nand_info[dev].name);
205 puts("... is now current device\n");
206 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200207
208#ifdef CFG_NAND_SELECT_DEVICE
209 /*
210 * Select the chip in the board/cpu specific driver
211 */
212 board_nand_select_device(nand_info[dev].priv, dev);
213#endif
214
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100215 return 0;
216 }
217
218 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
219 strncmp(cmd, "dump", 4) != 0 &&
Stefan Roese2255b2d2006-10-10 12:36:02 +0200220 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
221 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
222 strcmp(cmd, "biterr") != 0 &&
223 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100224 goto usage;
225
226 /* the following commands operate on the current device */
227 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
228 !nand_info[nand_curr_device].name) {
229 puts("\nno devices available\n");
230 return 1;
231 }
232 nand = &nand_info[nand_curr_device];
233
234 if (strcmp(cmd, "bad") == 0) {
235 printf("\nDevice %d bad blocks:\n", nand_curr_device);
236 for (off = 0; off < nand->size; off += nand->erasesize)
237 if (nand_block_isbad(nand, off))
238 printf(" %08x\n", off);
239 return 0;
240 }
241
Stefan Roese856f0542006-10-28 15:55:52 +0200242 /*
243 * Syntax is:
244 * 0 1 2 3 4
245 * nand erase [clean] [off size]
246 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200247 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
248 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200249 /* "clean" at index 2 means request to write cleanmarker */
250 int clean = argc > 2 && !strcmp("clean", argv[2]);
251 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200252 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100253
Stefan Roese856f0542006-10-28 15:55:52 +0200254 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
255 /* skip first two or three arguments, look for offset and size */
256 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
257 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200258
259 memset(&opts, 0, sizeof(opts));
260 opts.offset = off;
261 opts.length = size;
262 opts.jffs2 = clean;
263 opts.quiet = quiet;
264
265 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200266 puts("Warning: "
267 "scrub option will erase all factory set "
268 "bad blocks!\n"
269 " "
270 "There is no reliable way to recover them.\n"
271 " "
272 "Use this command only for testing purposes "
273 "if you\n"
274 " "
275 "are sure of what you are doing!\n"
276 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200277
278 if (getc() == 'y' && getc() == '\r') {
279 opts.scrub = 1;
280 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200281 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200282 return -1;
283 }
284 }
285 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100286 printf("%s\n", ret ? "ERROR" : "OK");
287
288 return ret == 0 ? 0 : 1;
289 }
290
291 if (strncmp(cmd, "dump", 4) == 0) {
292 if (argc < 3)
293 goto usage;
294
295 s = strchr(cmd, '.');
296 off = (int)simple_strtoul(argv[2], NULL, 16);
297
298 if (s != NULL && strcmp(s, ".oob") == 0)
299 ret = nand_dump_oob(nand, off);
300 else
301 ret = nand_dump(nand, off);
302
303 return ret == 0 ? 1 : 0;
304
305 }
306
307 /* read write */
308 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200309 int read;
310
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100311 if (argc < 4)
312 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200313
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100314 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
315
Stefan Roese2255b2d2006-10-10 12:36:02 +0200316 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200317 printf("\nNAND %s: ", read ? "read" : "write");
318 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
319 return 1;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100320
Stefan Roese2255b2d2006-10-10 12:36:02 +0200321 s = strchr(cmd, '.');
322 if (s != NULL &&
323 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
324 if (read) {
325 /* read */
326 nand_read_options_t opts;
327 memset(&opts, 0, sizeof(opts));
328 opts.buffer = (u_char*) addr;
329 opts.length = size;
330 opts.offset = off;
331 opts.quiet = quiet;
332 ret = nand_read_opts(nand, &opts);
333 } else {
334 /* write */
335 nand_write_options_t opts;
336 memset(&opts, 0, sizeof(opts));
337 opts.buffer = (u_char*) addr;
338 opts.length = size;
339 opts.offset = off;
340 /* opts.forcejffs2 = 1; */
341 opts.pad = 1;
342 opts.blockalign = 1;
343 opts.quiet = quiet;
344 ret = nand_write_opts(nand, &opts);
345 }
Stefan Roese856f0542006-10-28 15:55:52 +0200346 } else {
347 if (read)
348 ret = nand_read(nand, off, &size, (u_char *)addr);
349 else
350 ret = nand_write(nand, off, &size, (u_char *)addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200351 }
352
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100353 printf(" %d bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200354 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100355
356 return ret == 0 ? 0 : 1;
357 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200358
Stefan Roese2255b2d2006-10-10 12:36:02 +0200359 if (strcmp(cmd, "markbad") == 0) {
360 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
361
362 int ret = nand->block_markbad(nand, addr);
363 if (ret == 0) {
364 printf("block 0x%08lx successfully marked as bad\n",
365 (ulong) addr);
366 return 0;
367 } else {
368 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
369 (ulong) addr, ret);
370 }
371 return 1;
372 }
373 if (strcmp(cmd, "biterr") == 0) {
374 /* todo */
375 return 1;
376 }
377
378 if (strcmp(cmd, "lock") == 0) {
379 int tight = 0;
380 int status = 0;
381 if (argc == 3) {
382 if (!strcmp("tight", argv[2]))
383 tight = 1;
384 if (!strcmp("status", argv[2]))
385 status = 1;
386 }
387
388 if (status) {
389 ulong block_start = 0;
390 ulong off;
391 int last_status = -1;
392
393 struct nand_chip *nand_chip = nand->priv;
394 /* check the WP bit */
395 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
396 printf("device is %swrite protected\n",
397 (nand_chip->read_byte(nand) & 0x80 ?
398 "NOT " : "" ) );
399
400 for (off = 0; off < nand->size; off += nand->oobblock) {
401 int s = nand_get_lock_status(nand, off);
402
403 /* print message only if status has changed
404 * or at end of chip
405 */
406 if (off == nand->size - nand->oobblock
407 || (s != last_status && off != 0)) {
408
409 printf("%08x - %08x: %8d pages %s%s%s\n",
410 block_start,
411 off-1,
412 (off-block_start)/nand->oobblock,
413 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
414 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
415 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
416 }
417
418 last_status = s;
419 }
420 } else {
421 if (!nand_lock(nand, tight)) {
Stefan Roese856f0542006-10-28 15:55:52 +0200422 puts("NAND flash successfully locked\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200423 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200424 puts("Error locking NAND flash\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200425 return 1;
426 }
427 }
428 return 0;
429 }
430
431 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200432 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
433 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200434
435 if (!nand_unlock(nand, off, size)) {
Stefan Roese856f0542006-10-28 15:55:52 +0200436 puts("NAND flash successfully unlocked\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200437 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200438 puts("Error unlocking NAND flash, "
439 "write and erase will probably fail\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200440 return 1;
441 }
442 return 0;
443 }
444
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100445usage:
446 printf("Usage:\n%s\n", cmdtp->usage);
447 return 1;
448}
449
450U_BOOT_CMD(nand, 5, 1, do_nand,
451 "nand - NAND sub-system\n",
452 "info - show available NAND devices\n"
453 "nand device [dev] - show or set current device\n"
Stefan Roese856f0542006-10-28 15:55:52 +0200454 "nand read[.jffs2] - addr off|partition size\n"
455 "nand write[.jffs2] - addr off|partiton size - read/write `size' bytes starting\n"
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100456 " at offset `off' to/from memory address `addr'\n"
457 "nand erase [clean] [off size] - erase `size' bytes from\n"
458 " offset `off' (entire device if not specified)\n"
459 "nand bad - show bad blocks\n"
460 "nand dump[.oob] off - dump page\n"
461 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
462 "nand markbad off - mark bad block at offset (UNSAFE)\n"
Stefan Roese2255b2d2006-10-10 12:36:02 +0200463 "nand biterr off - make a bit error at offset (UNSAFE)\n"
464 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
465 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100466
Stefan Roese856f0542006-10-28 15:55:52 +0200467static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
468 ulong offset, ulong addr, char *cmd)
469{
470 int r;
471 char *ep;
472 ulong cnt;
473 image_header_t *hdr;
474
475 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
476
477 cnt = nand->oobblock;
478 r = nand_read(nand, offset, &cnt, (u_char *) addr);
479 if (r) {
480 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200481 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200482 return 1;
483 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200484 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200485
486 hdr = (image_header_t *) addr;
487
488 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
489 printf("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
Heiko Schocherfad63402007-07-13 09:54:17 +0200490 show_boot_progress (-57);
Stefan Roese856f0542006-10-28 15:55:52 +0200491 return 1;
492 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200493 show_boot_progress (57);
Stefan Roese856f0542006-10-28 15:55:52 +0200494
495 print_image_hdr(hdr);
496
497 cnt = (ntohl(hdr->ih_size) + sizeof (image_header_t));
498
499 r = nand_read(nand, offset, &cnt, (u_char *) addr);
500 if (r) {
501 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200502 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200503 return 1;
504 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200505 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200506
507 /* Loading ok, update default load address */
508
509 load_addr = addr;
510
511 /* Check if we should attempt an auto-start */
512 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
513 char *local_args[2];
514 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
515
516 local_args[0] = cmd;
517 local_args[1] = NULL;
518
519 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
520
521 do_bootm(cmdtp, 0, 1, local_args);
522 return 1;
523 }
524 return 0;
525}
526
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100527int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
528{
529 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200530 int idx;
531 ulong addr, offset = 0;
532#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
533 struct mtd_device *dev;
534 struct part_info *part;
535 u8 pnum;
536
537 if (argc >= 2) {
538 char *p = (argc == 2) ? argv[1] : argv[2];
539 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
540 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
541 if (dev->id->type != MTD_DEV_TYPE_NAND) {
542 puts("Not a NAND device\n");
543 return 1;
544 }
545 if (argc > 3)
546 goto usage;
547 if (argc == 3)
548 addr = simple_strtoul(argv[2], NULL, 16);
549 else
550 addr = CFG_LOAD_ADDR;
551 return nand_load_image(cmdtp, &nand_info[dev->id->num],
552 part->offset, addr, argv[0]);
553 }
554 }
555#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100556
Heiko Schocherfad63402007-07-13 09:54:17 +0200557 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100558 switch (argc) {
559 case 1:
560 addr = CFG_LOAD_ADDR;
561 boot_device = getenv("bootdevice");
562 break;
563 case 2:
564 addr = simple_strtoul(argv[1], NULL, 16);
565 boot_device = getenv("bootdevice");
566 break;
567 case 3:
568 addr = simple_strtoul(argv[1], NULL, 16);
569 boot_device = argv[2];
570 break;
571 case 4:
572 addr = simple_strtoul(argv[1], NULL, 16);
573 boot_device = argv[2];
574 offset = simple_strtoul(argv[3], NULL, 16);
575 break;
576 default:
Stefan Roese856f0542006-10-28 15:55:52 +0200577#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
578usage:
579#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100580 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200581 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100582 return 1;
583 }
584
Heiko Schocherfad63402007-07-13 09:54:17 +0200585 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100586 if (!boot_device) {
587 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200588 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100589 return 1;
590 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200591 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100592
Stefan Roese856f0542006-10-28 15:55:52 +0200593 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100594
Stefan Roese856f0542006-10-28 15:55:52 +0200595 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
596 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200597 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100598 return 1;
599 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200600 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100601
Stefan Roese856f0542006-10-28 15:55:52 +0200602 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100603}
604
605U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Stefan Roese856f0542006-10-28 15:55:52 +0200606 "nboot - boot from NAND device\n",
607 "[partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100608
609#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
610
611#else /* CFG_NAND_LEGACY */
612/*
613 *
614 * Legacy NAND support - to be phased out
615 *
616 */
wdenkdc7c9a12003-03-26 06:55:25 +0000617#include <command.h>
618#include <malloc.h>
619#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000620#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000621
Heiko Schocherfad63402007-07-13 09:54:17 +0200622#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000623# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200624# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000625#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200626# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000627#endif
628
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100629#if (CONFIG_COMMANDS & CFG_CMD_NAND)
630#include <linux/mtd/nand_legacy.h>
631#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000632#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000633#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100634#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000635
wdenk1f4bb372003-07-27 00:21:01 +0000636#ifdef CONFIG_OMAP1510
637void archflashwp(void *archdata, int wp);
638#endif
639
640#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
641
wdenka43278a2003-09-11 19:48:06 +0000642#undef NAND_DEBUG
wdenkdc7c9a12003-03-26 06:55:25 +0000643#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000644
645/* ****************** WARNING *********************
646 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
647 * erase (or at least attempt to erase) blocks that are marked
648 * bad. This can be very handy if you are _sure_ that the block
649 * is OK, say because you marked a good block bad to test bad
650 * block handling and you are done testing, or if you have
651 * accidentally marked blocks bad.
652 *
653 * Erasing factory marked bad blocks is a _bad_ idea. If the
654 * erase succeeds there is no reliable way to find them again,
655 * and attempting to program or erase bad blocks can affect
656 * the data in _other_ (good) blocks.
657 */
658#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000659
660#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000661#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000662
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100663/* bits for nand_legacy_rw() `cmd'; or together as needed */
wdenk7a8e9bed2003-05-31 18:35:21 +0000664#define NANDRW_READ 0x01
665#define NANDRW_WRITE 0x00
666#define NANDRW_JFFS2 0x02
wdenka3d991b2004-04-15 21:48:45 +0000667#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000668
wdenkdc7c9a12003-03-26 06:55:25 +0000669/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100670 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000671 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100672extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
673extern int curr_device;
674extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
675 size_t len, int clean);
676extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
677 size_t len, size_t *retlen, u_char *buf);
678extern void nand_print(struct nand_chip *nand);
679extern void nand_print_bad(struct nand_chip *nand);
680extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
681 size_t len, size_t *retlen, u_char *buf);
682extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
683 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000684
wdenkdc7c9a12003-03-26 06:55:25 +0000685
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100686int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000687{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100688 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000689
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100690 switch (argc) {
691 case 0:
692 case 1:
693 printf ("Usage:\n%s\n", cmdtp->usage);
694 return 1;
695 case 2:
696 if (strcmp (argv[1], "info") == 0) {
697 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000698
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100699 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000700
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100701 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
702 if (nand_dev_desc[i].ChipID ==
703 NAND_ChipID_UNKNOWN)
704 continue; /* list only known devices */
705 printf ("Device %d: ", i);
706 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000707 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100708 return 0;
709
710 } else if (strcmp (argv[1], "device") == 0) {
711 if ((curr_device < 0)
712 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
713 puts ("\nno devices available\n");
714 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000715 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100716 printf ("\nDevice %d: ", curr_device);
717 nand_print (&nand_dev_desc[curr_device]);
718 return 0;
719
720 } else if (strcmp (argv[1], "bad") == 0) {
721 if ((curr_device < 0)
722 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
723 puts ("\nno devices available\n");
724 return 1;
725 }
726 printf ("\nDevice %d bad blocks:\n", curr_device);
727 nand_print_bad (&nand_dev_desc[curr_device]);
728 return 0;
729
730 }
731 printf ("Usage:\n%s\n", cmdtp->usage);
732 return 1;
733 case 3:
734 if (strcmp (argv[1], "device") == 0) {
735 int dev = (int) simple_strtoul (argv[2], NULL, 10);
736
737 printf ("\nDevice %d: ", dev);
738 if (dev >= CFG_MAX_NAND_DEVICE) {
739 puts ("unknown device\n");
740 return 1;
741 }
742 nand_print (&nand_dev_desc[dev]);
743 /*nand_print (dev); */
744
745 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
746 return 1;
747 }
748
749 curr_device = dev;
750
751 puts ("... is now current device\n");
752
753 return 0;
754 } else if (strcmp (argv[1], "erase") == 0
755 && strcmp (argv[2], "clean") == 0) {
756 struct nand_chip *nand = &nand_dev_desc[curr_device];
757 ulong off = 0;
758 ulong size = nand->totlen;
759 int ret;
760
761 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
762
763 ret = nand_legacy_erase (nand, off, size, 1);
764
765 printf ("%s\n", ret ? "ERROR" : "OK");
766
wdenk7a8e9bed2003-05-31 18:35:21 +0000767 return ret;
768 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100769
770 printf ("Usage:\n%s\n", cmdtp->usage);
771 return 1;
772 default:
773 /* at least 4 args */
774
775 if (strncmp (argv[1], "read", 4) == 0 ||
776 strncmp (argv[1], "write", 5) == 0) {
777 ulong addr = simple_strtoul (argv[2], NULL, 16);
778 ulong off = simple_strtoul (argv[3], NULL, 16);
779 ulong size = simple_strtoul (argv[4], NULL, 16);
780 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
781 NANDRW_READ : NANDRW_WRITE;
782 int ret, total;
783 char *cmdtail = strchr (argv[1], '.');
784
785 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
786 /* read out-of-band data */
787 if (cmd & NANDRW_READ) {
788 ret = nand_read_oob (nand_dev_desc + curr_device,
789 off, size, (size_t *) & total,
790 (u_char *) addr);
791 } else {
792 ret = nand_write_oob (nand_dev_desc + curr_device,
793 off, size, (size_t *) & total,
794 (u_char *) addr);
795 }
796 return ret;
797 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
798 cmd |= NANDRW_JFFS2; /* skip bad blocks */
799 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
800 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
801 if (cmd & NANDRW_READ)
802 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
803 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000804#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100805 /* need ".e" same as ".j" for compatibility with older units */
806 else if (cmdtail && !strcmp (cmdtail, ".e"))
807 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000808#endif
stroesea842a6d2004-12-16 17:45:46 +0000809#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100810 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
811 /* ".i" for image -> read skips bad block (no 0xff) */
812 else if (cmdtail && !strcmp (cmdtail, ".i")) {
813 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
814 if (cmd & NANDRW_READ)
815 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
816 }
stroesea842a6d2004-12-16 17:45:46 +0000817#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100818 else if (cmdtail) {
819 printf ("Usage:\n%s\n", cmdtp->usage);
820 return 1;
821 }
822
823 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
824 (cmd & NANDRW_READ) ? "read" : "write",
825 curr_device, off, size);
826
827 ret = nand_legacy_rw (nand_dev_desc + curr_device,
828 cmd, off, size,
829 (size_t *) & total,
830 (u_char *) addr);
831
832 printf (" %d bytes %s: %s\n", total,
833 (cmd & NANDRW_READ) ? "read" : "written",
834 ret ? "ERROR" : "OK");
835
836 return ret;
837 } else if (strcmp (argv[1], "erase") == 0 &&
838 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
839 int clean = argc == 5;
840 ulong off =
841 simple_strtoul (argv[2 + clean], NULL, 16);
842 ulong size =
843 simple_strtoul (argv[3 + clean], NULL, 16);
844 int ret;
845
846 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
847 curr_device, off, size);
848
849 ret = nand_legacy_erase (nand_dev_desc + curr_device,
850 off, size, clean);
851
852 printf ("%s\n", ret ? "ERROR" : "OK");
853
854 return ret;
855 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000856 printf ("Usage:\n%s\n", cmdtp->usage);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100857 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000858 }
wdenkdc7c9a12003-03-26 06:55:25 +0000859
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100860 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000861 }
wdenkdc7c9a12003-03-26 06:55:25 +0000862}
863
wdenk0d498392003-07-01 21:06:45 +0000864U_BOOT_CMD(
865 nand, 5, 1, do_nand,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200866 "nand - legacy NAND sub-system\n",
wdenkb0fce992003-06-29 21:03:46 +0000867 "info - show available NAND devices\n"
868 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000869 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000870 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
871 " at offset `off' to/from memory address `addr'\n"
872 "nand erase [clean] [off size] - erase `size' bytes from\n"
873 " offset `off' (entire device if not specified)\n"
874 "nand bad - show bad blocks\n"
875 "nand read.oob addr off size - read out-of-band data\n"
876 "nand write.oob addr off size - read out-of-band data\n"
877);
878
wdenkdc7c9a12003-03-26 06:55:25 +0000879int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
880{
881 char *boot_device = NULL;
882 char *ep;
883 int dev;
884 ulong cnt;
885 ulong addr;
886 ulong offset = 0;
887 image_header_t *hdr;
888 int rcode = 0;
Heiko Schocherfad63402007-07-13 09:54:17 +0200889 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000890 switch (argc) {
891 case 1:
892 addr = CFG_LOAD_ADDR;
893 boot_device = getenv ("bootdevice");
894 break;
895 case 2:
896 addr = simple_strtoul(argv[1], NULL, 16);
897 boot_device = getenv ("bootdevice");
898 break;
899 case 3:
900 addr = simple_strtoul(argv[1], NULL, 16);
901 boot_device = argv[2];
902 break;
903 case 4:
904 addr = simple_strtoul(argv[1], NULL, 16);
905 boot_device = argv[2];
906 offset = simple_strtoul(argv[3], NULL, 16);
907 break;
908 default:
909 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200910 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000911 return 1;
912 }
913
Heiko Schocherfad63402007-07-13 09:54:17 +0200914 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000915 if (!boot_device) {
916 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200917 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000918 return 1;
919 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200920 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +0000921
922 dev = simple_strtoul(boot_device, &ep, 16);
923
924 if ((dev >= CFG_MAX_NAND_DEVICE) ||
925 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
926 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200927 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +0000928 return 1;
929 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200930 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +0000931
wdenk7a8e9bed2003-05-31 18:35:21 +0000932 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
wdenkdc7c9a12003-03-26 06:55:25 +0000933 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
934 offset);
935
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100936 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
937 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +0000938 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200939 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +0000940 return 1;
941 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200942 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +0000943
944 hdr = (image_header_t *)addr;
945
946 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
947
948 print_image_hdr (hdr);
949
950 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
951 cnt -= SECTORSIZE;
952 } else {
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100953 printf ("\n** Bad Magic Number 0x%x **\n", ntohl(hdr->ih_magic));
Heiko Schocherfad63402007-07-13 09:54:17 +0200954 show_boot_progress (-57);
wdenkdc7c9a12003-03-26 06:55:25 +0000955 return 1;
956 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200957 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +0000958
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100959 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
960 offset + SECTORSIZE, cnt, NULL,
961 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +0000962 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200963 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +0000964 return 1;
965 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200966 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +0000967
968 /* Loading ok, update default load address */
969
970 load_addr = addr;
971
972 /* Check if we should attempt an auto-start */
973 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
974 char *local_args[2];
975 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
976
977 local_args[0] = argv[0];
978 local_args[1] = NULL;
979
wdenk7a8e9bed2003-05-31 18:35:21 +0000980 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +0000981
982 do_bootm (cmdtp, 0, 1, local_args);
983 rcode = 1;
984 }
985 return rcode;
986}
987
wdenk0d498392003-07-01 21:06:45 +0000988U_BOOT_CMD(
989 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +0000990 "nboot - boot from NAND device\n",
991 "loadAddr dev\n"
992);
993
wdenkdc7c9a12003-03-26 06:55:25 +0000994#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100995
996#endif /* CFG_NAND_LEGACY */