blob: 7286726f1c15b45e49c9956d3829bbdba3ecdb1b [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>
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>
28
29#ifdef CONFIG_SHOW_BOOT_PROGRESS
30# include <status_led.h>
31# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
32#else
33# define SHOW_BOOT_PROGRESS(arg)
34#endif
35
36#include <jffs2/jffs2.h>
37#include <nand.h>
38
Stefan Roese198b23e2006-10-28 15:55:52 +020039#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
40
41/* parition handling routines */
42int mtdparts_init(void);
43int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
44int find_dev_and_part(const char *id, struct mtd_device **dev,
45 u8 *part_num, struct part_info **part);
46#endif
47
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010048extern nand_info_t nand_info[]; /* info for NAND chips */
49
50static int nand_dump_oob(nand_info_t *nand, ulong off)
51{
52 return 0;
53}
54
55static int nand_dump(nand_info_t *nand, ulong off)
56{
57 int i;
58 u_char *buf, *p;
59
60 buf = malloc(nand->oobblock + nand->oobsize);
61 if (!buf) {
62 puts("No memory for page buffer\n");
63 return 1;
64 }
65 off &= ~(nand->oobblock - 1);
66 i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
67 if (i < 0) {
68 printf("Error (%d) reading page %08x\n", i, off);
69 free(buf);
70 return 1;
71 }
72 printf("Page %08x dump:\n", off);
73 i = nand->oobblock >> 4; p = buf;
74 while (i--) {
75 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
76 " %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], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
79 p += 16;
80 }
81 puts("OOB:\n");
82 i = nand->oobsize >> 3;
83 while (i--) {
84 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
85 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
86 p += 8;
87 }
88 free(buf);
89
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
104arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
105{
106 int idx = nand_curr_device;
107#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
108 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) {
121 if (!(str2long(argv[1], size))) {
122 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) {
147 if (!(str2long(argv[1], size))) {
148 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
Stefan Roese198b23e2006-10-28 15:55:52 +0200155#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
156out:
157#endif
158 printf("device %d ", idx);
159 if (*size == nand->size)
160 puts("whole chip\n");
161 else
162 printf("offset 0x%x, size 0x%x\n", *off, *size);
163 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{
168 int i, dev, ret;
169 ulong addr, off, size;
170 char *cmd, *s;
171 nand_info_t *nand;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200172 int quiet = 0;
173 const char *quiet_str = getenv("quiet");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100174
175 /* at least two arguments please */
176 if (argc < 2)
177 goto usage;
178
Stefan Roesed351b2b2006-10-10 12:36:02 +0200179 if (quiet_str)
180 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
181
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100182 cmd = argv[1];
183
184 if (strcmp(cmd, "info") == 0) {
185
186 putc('\n');
187 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
188 if (nand_info[i].name)
189 printf("Device %d: %s, sector size %lu KiB\n",
190 i, nand_info[i].name,
191 nand_info[i].erasesize >> 10);
192 }
193 return 0;
194 }
195
196 if (strcmp(cmd, "device") == 0) {
197
198 if (argc < 3) {
199 if ((nand_curr_device < 0) ||
200 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
201 puts("\nno devices available\n");
202 else
203 printf("\nDevice %d: %s\n", nand_curr_device,
204 nand_info[nand_curr_device].name);
205 return 0;
206 }
207 dev = (int)simple_strtoul(argv[2], NULL, 10);
208 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
209 puts("No such device\n");
210 return 1;
211 }
212 printf("Device %d: %s", dev, nand_info[dev].name);
213 puts("... is now current device\n");
214 nand_curr_device = dev;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200215
216#ifdef CFG_NAND_SELECT_DEVICE
217 /*
218 * Select the chip in the board/cpu specific driver
219 */
220 board_nand_select_device(nand_info[dev].priv, dev);
221#endif
222
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100223 return 0;
224 }
225
226 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
227 strncmp(cmd, "dump", 4) != 0 &&
Stefan Roesed351b2b2006-10-10 12:36:02 +0200228 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
229 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
230 strcmp(cmd, "biterr") != 0 &&
231 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100232 goto usage;
233
234 /* the following commands operate on the current device */
235 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
236 !nand_info[nand_curr_device].name) {
237 puts("\nno devices available\n");
238 return 1;
239 }
240 nand = &nand_info[nand_curr_device];
241
242 if (strcmp(cmd, "bad") == 0) {
243 printf("\nDevice %d bad blocks:\n", nand_curr_device);
244 for (off = 0; off < nand->size; off += nand->erasesize)
245 if (nand_block_isbad(nand, off))
246 printf(" %08x\n", off);
247 return 0;
248 }
249
Stefan Roese198b23e2006-10-28 15:55:52 +0200250 /*
251 * Syntax is:
252 * 0 1 2 3 4
253 * nand erase [clean] [off size]
254 */
Stefan Roesed351b2b2006-10-10 12:36:02 +0200255 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
256 nand_erase_options_t opts;
Stefan Roese198b23e2006-10-28 15:55:52 +0200257 /* "clean" at index 2 means request to write cleanmarker */
258 int clean = argc > 2 && !strcmp("clean", argv[2]);
259 int o = clean ? 3 : 2;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200260 int scrub = !strcmp(cmd, "scrub");
261
Stefan Roese198b23e2006-10-28 15:55:52 +0200262 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
263 /* skip first two or three arguments, look for offset and size */
264 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
265 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200266
267 memset(&opts, 0, sizeof(opts));
268 opts.offset = off;
269 opts.length = size;
270 opts.jffs2 = clean;
271 opts.quiet = quiet;
272
273 if (scrub) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200274 puts("Warning: "
275 "scrub option will erase all factory set "
276 "bad blocks!\n"
277 " "
278 "There is no reliable way to recover them.\n"
279 " "
280 "Use this command only for testing purposes "
281 "if you\n"
282 " "
283 "are sure of what you are doing!\n"
284 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200285
286 if (getc() == 'y' && getc() == '\r') {
287 opts.scrub = 1;
288 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200289 puts("scrub aborted\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200290 return -1;
291 }
292 }
293 ret = nand_erase_opts(nand, &opts);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100294 printf("%s\n", ret ? "ERROR" : "OK");
295
296 return ret == 0 ? 0 : 1;
297 }
298
299 if (strncmp(cmd, "dump", 4) == 0) {
300 if (argc < 3)
301 goto usage;
302
303 s = strchr(cmd, '.');
304 off = (int)simple_strtoul(argv[2], NULL, 16);
305
306 if (s != NULL && strcmp(s, ".oob") == 0)
307 ret = nand_dump_oob(nand, off);
308 else
309 ret = nand_dump(nand, off);
310
311 return ret == 0 ? 1 : 0;
312
313 }
314
315 /* read write */
316 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roesed351b2b2006-10-10 12:36:02 +0200317 int read;
318
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100319 if (argc < 4)
320 goto usage;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200321
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100322 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
323
Stefan Roesed351b2b2006-10-10 12:36:02 +0200324 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese198b23e2006-10-28 15:55:52 +0200325 printf("\nNAND %s: ", read ? "read" : "write");
326 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
327 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200328
329 s = strchr(cmd, '.');
330 if (s != NULL &&
331 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
332 if (read) {
333 /* read */
334 nand_read_options_t opts;
335 memset(&opts, 0, sizeof(opts));
336 opts.buffer = (u_char*) addr;
337 opts.length = size;
338 opts.offset = off;
339 opts.quiet = quiet;
340 ret = nand_read_opts(nand, &opts);
341 } else {
342 /* write */
343 nand_write_options_t opts;
344 memset(&opts, 0, sizeof(opts));
345 opts.buffer = (u_char*) addr;
346 opts.length = size;
347 opts.offset = off;
348 /* opts.forcejffs2 = 1; */
349 opts.pad = 1;
350 opts.blockalign = 1;
351 opts.quiet = quiet;
352 ret = nand_write_opts(nand, &opts);
353 }
Stefan Roese198b23e2006-10-28 15:55:52 +0200354 } else {
355 if (read)
356 ret = nand_read(nand, off, &size, (u_char *)addr);
357 else
358 ret = nand_write(nand, off, &size, (u_char *)addr);
Stefan Roesed351b2b2006-10-10 12:36:02 +0200359 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100360
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100361 printf(" %d bytes %s: %s\n", size,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200362 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100363
364 return ret == 0 ? 0 : 1;
365 }
Stefan Roesed351b2b2006-10-10 12:36:02 +0200366
Stefan Roesed351b2b2006-10-10 12:36:02 +0200367 if (strcmp(cmd, "markbad") == 0) {
368 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
369
370 int ret = nand->block_markbad(nand, addr);
371 if (ret == 0) {
372 printf("block 0x%08lx successfully marked as bad\n",
373 (ulong) addr);
374 return 0;
375 } else {
376 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
377 (ulong) addr, ret);
378 }
379 return 1;
380 }
381 if (strcmp(cmd, "biterr") == 0) {
382 /* todo */
383 return 1;
384 }
385
386 if (strcmp(cmd, "lock") == 0) {
387 int tight = 0;
388 int status = 0;
389 if (argc == 3) {
390 if (!strcmp("tight", argv[2]))
391 tight = 1;
392 if (!strcmp("status", argv[2]))
393 status = 1;
394 }
395
396 if (status) {
397 ulong block_start = 0;
398 ulong off;
399 int last_status = -1;
400
401 struct nand_chip *nand_chip = nand->priv;
402 /* check the WP bit */
403 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
404 printf("device is %swrite protected\n",
405 (nand_chip->read_byte(nand) & 0x80 ?
406 "NOT " : "" ) );
407
408 for (off = 0; off < nand->size; off += nand->oobblock) {
409 int s = nand_get_lock_status(nand, off);
410
411 /* print message only if status has changed
412 * or at end of chip
413 */
414 if (off == nand->size - nand->oobblock
415 || (s != last_status && off != 0)) {
416
417 printf("%08x - %08x: %8d pages %s%s%s\n",
418 block_start,
419 off-1,
420 (off-block_start)/nand->oobblock,
421 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
422 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
423 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
424 }
425
426 last_status = s;
427 }
428 } else {
429 if (!nand_lock(nand, tight)) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200430 puts("NAND flash successfully locked\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200431 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200432 puts("Error locking NAND flash\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200433 return 1;
434 }
435 }
436 return 0;
437 }
438
439 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200440 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
441 return 1;
Stefan Roesed351b2b2006-10-10 12:36:02 +0200442
443 if (!nand_unlock(nand, off, size)) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200444 puts("NAND flash successfully unlocked\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200445 } else {
Stefan Roese198b23e2006-10-28 15:55:52 +0200446 puts("Error unlocking NAND flash, "
447 "write and erase will probably fail\n");
Stefan Roesed351b2b2006-10-10 12:36:02 +0200448 return 1;
449 }
450 return 0;
451 }
452
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100453usage:
454 printf("Usage:\n%s\n", cmdtp->usage);
455 return 1;
456}
457
458U_BOOT_CMD(nand, 5, 1, do_nand,
459 "nand - NAND sub-system\n",
460 "info - show available NAND devices\n"
461 "nand device [dev] - show or set current device\n"
Stefan Roese198b23e2006-10-28 15:55:52 +0200462 "nand read[.jffs2] - addr off|partition size\n"
463 "nand write[.jffs2] - addr off|partiton size - read/write `size' bytes starting\n"
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100464 " at offset `off' to/from memory address `addr'\n"
465 "nand erase [clean] [off size] - erase `size' bytes from\n"
466 " offset `off' (entire device if not specified)\n"
467 "nand bad - show bad blocks\n"
468 "nand dump[.oob] off - dump page\n"
469 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
470 "nand markbad off - mark bad block at offset (UNSAFE)\n"
Stefan Roesed351b2b2006-10-10 12:36:02 +0200471 "nand biterr off - make a bit error at offset (UNSAFE)\n"
472 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
473 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100474
Stefan Roese198b23e2006-10-28 15:55:52 +0200475static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
476 ulong offset, ulong addr, char *cmd)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100477{
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100478 int r;
Stefan Roese198b23e2006-10-28 15:55:52 +0200479 char *ep;
480 ulong cnt;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100481 image_header_t *hdr;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100482
Stefan Roese198b23e2006-10-28 15:55:52 +0200483 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100484
485 cnt = nand->oobblock;
486 r = nand_read(nand, offset, &cnt, (u_char *) addr);
487 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200488 puts("** Read error\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100489 SHOW_BOOT_PROGRESS(-1);
490 return 1;
491 }
492
493 hdr = (image_header_t *) addr;
494
495 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
496 printf("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
497 SHOW_BOOT_PROGRESS(-1);
498 return 1;
499 }
500
501 print_image_hdr(hdr);
502
503 cnt = (ntohl(hdr->ih_size) + sizeof (image_header_t));
504
505 r = nand_read(nand, offset, &cnt, (u_char *) addr);
506 if (r) {
Stefan Roese198b23e2006-10-28 15:55:52 +0200507 puts("** Read error\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100508 SHOW_BOOT_PROGRESS(-1);
509 return 1;
510 }
511
512 /* Loading ok, update default load address */
513
514 load_addr = addr;
515
516 /* Check if we should attempt an auto-start */
517 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
518 char *local_args[2];
519 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
520
Stefan Roese198b23e2006-10-28 15:55:52 +0200521 local_args[0] = cmd;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100522 local_args[1] = NULL;
523
524 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
525
526 do_bootm(cmdtp, 0, 1, local_args);
527 return 1;
528 }
529 return 0;
530}
531
Stefan Roese198b23e2006-10-28 15:55:52 +0200532int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
533{
534 char *boot_device = NULL;
535 int idx;
536 ulong addr, offset = 0;
537#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
538 struct mtd_device *dev;
539 struct part_info *part;
540 u8 pnum;
541
542 if (argc >= 2) {
543 char *p = (argc == 2) ? argv[1] : argv[2];
544 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
545 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
546 if (dev->id->type != MTD_DEV_TYPE_NAND) {
547 puts("Not a NAND device\n");
548 return 1;
549 }
550 if (argc > 3)
551 goto usage;
552 if (argc == 3)
553 addr = simple_strtoul(argv[2], NULL, 16);
554 else
555 addr = CFG_LOAD_ADDR;
556 return nand_load_image(cmdtp, &nand_info[dev->id->num],
557 part->offset, addr, argv[0]);
558 }
559 }
560#endif
561
562 switch (argc) {
563 case 1:
564 addr = CFG_LOAD_ADDR;
565 boot_device = getenv("bootdevice");
566 break;
567 case 2:
568 addr = simple_strtoul(argv[1], NULL, 16);
569 boot_device = getenv("bootdevice");
570 break;
571 case 3:
572 addr = simple_strtoul(argv[1], NULL, 16);
573 boot_device = argv[2];
574 break;
575 case 4:
576 addr = simple_strtoul(argv[1], NULL, 16);
577 boot_device = argv[2];
578 offset = simple_strtoul(argv[3], NULL, 16);
579 break;
580 default:
581#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
582usage:
583#endif
584 printf("Usage:\n%s\n", cmdtp->usage);
585 SHOW_BOOT_PROGRESS(-1);
586 return 1;
587 }
588
589 if (!boot_device) {
590 puts("\n** No boot device **\n");
591 SHOW_BOOT_PROGRESS(-1);
592 return 1;
593 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100594
Stefan Roese198b23e2006-10-28 15:55:52 +0200595 idx = simple_strtoul(boot_device, NULL, 16);
596
597 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
598 printf("\n** Device %d not available\n", idx);
599 SHOW_BOOT_PROGRESS(-1);
600 return 1;
601 }
602
603 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
604}
605
606U_BOOT_CMD(nboot, 4, 1, do_nandboot,
607 "nboot - boot from NAND device\n",
608 "[partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100609
610#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
611
612#else /* CFG_NAND_LEGACY */
613/*
614 *
615 * Legacy NAND support - to be phased out
616 *
617 */
wdenkc8434db2003-03-26 06:55:25 +0000618#include <command.h>
619#include <malloc.h>
620#include <asm/io.h>
wdenk145d2c12004-04-15 21:48:45 +0000621#include <watchdog.h>
wdenkc8434db2003-03-26 06:55:25 +0000622
623#ifdef CONFIG_SHOW_BOOT_PROGRESS
624# include <status_led.h>
625# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
626#else
627# define SHOW_BOOT_PROGRESS(arg)
628#endif
629
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100630#if (CONFIG_COMMANDS & CFG_CMD_NAND)
631#include <linux/mtd/nand_legacy.h>
632#if 0
wdenkc8434db2003-03-26 06:55:25 +0000633#include <linux/mtd/nand_ids.h>
wdenkabda5ca2003-05-31 18:35:21 +0000634#include <jffs2/jffs2.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100635#endif
wdenkc8434db2003-03-26 06:55:25 +0000636
wdenke58b0dc2003-07-27 00:21:01 +0000637#ifdef CONFIG_OMAP1510
638void archflashwp(void *archdata, int wp);
639#endif
640
641#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
642
wdenk934c4f82003-09-11 19:48:06 +0000643#undef NAND_DEBUG
wdenkc8434db2003-03-26 06:55:25 +0000644#undef PSYCHO_DEBUG
wdenkabda5ca2003-05-31 18:35:21 +0000645
646/* ****************** WARNING *********************
647 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
648 * erase (or at least attempt to erase) blocks that are marked
649 * bad. This can be very handy if you are _sure_ that the block
650 * is OK, say because you marked a good block bad to test bad
651 * block handling and you are done testing, or if you have
652 * accidentally marked blocks bad.
653 *
654 * Erasing factory marked bad blocks is a _bad_ idea. If the
655 * erase succeeds there is no reliable way to find them again,
656 * and attempting to program or erase bad blocks can affect
657 * the data in _other_ (good) blocks.
658 */
659#define ALLOW_ERASE_BAD_DEBUG 0
wdenkc8434db2003-03-26 06:55:25 +0000660
661#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenke58b0dc2003-07-27 00:21:01 +0000662#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkc8434db2003-03-26 06:55:25 +0000663
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100664/* bits for nand_legacy_rw() `cmd'; or together as needed */
wdenkabda5ca2003-05-31 18:35:21 +0000665#define NANDRW_READ 0x01
666#define NANDRW_WRITE 0x00
667#define NANDRW_JFFS2 0x02
wdenk145d2c12004-04-15 21:48:45 +0000668#define NANDRW_JFFS2_SKIP 0x04
wdenkabda5ca2003-05-31 18:35:21 +0000669
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100670/*
671 * Imports from nand_legacy.c
672 */
673extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
674extern int curr_device;
675extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
676 size_t len, int clean);
677extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
678 size_t len, size_t *retlen, u_char *buf);
679extern void nand_print(struct nand_chip *nand);
680extern void nand_print_bad(struct nand_chip *nand);
681extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
682 size_t len, size_t *retlen, u_char *buf);
683extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
684 size_t len, size_t *retlen, const u_char *buf);
wdenkc8434db2003-03-26 06:55:25 +0000685
wdenkc8434db2003-03-26 06:55:25 +0000686
687int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
688{
689 int rcode = 0;
690
691 switch (argc) {
692 case 0:
693 case 1:
694 printf ("Usage:\n%s\n", cmdtp->usage);
695 return 1;
696 case 2:
wdenk57b2d802003-06-27 21:31:46 +0000697 if (strcmp(argv[1],"info") == 0) {
wdenkc8434db2003-03-26 06:55:25 +0000698 int i;
699
700 putc ('\n');
701
702 for (i=0; i<CFG_MAX_NAND_DEVICE; ++i) {
703 if(nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
704 continue; /* list only known devices */
705 printf ("Device %d: ", i);
706 nand_print(&nand_dev_desc[i]);
707 }
708 return 0;
709
710 } else if (strcmp(argv[1],"device") == 0) {
711 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
712 puts ("\nno devices available\n");
713 return 1;
714 }
715 printf ("\nDevice %d: ", curr_device);
716 nand_print(&nand_dev_desc[curr_device]);
717 return 0;
wdenkabda5ca2003-05-31 18:35:21 +0000718
719 } else if (strcmp(argv[1],"bad") == 0) {
720 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
721 puts ("\nno devices available\n");
722 return 1;
723 }
724 printf ("\nDevice %d bad blocks:\n", curr_device);
725 nand_print_bad(&nand_dev_desc[curr_device]);
726 return 0;
727
wdenkc8434db2003-03-26 06:55:25 +0000728 }
729 printf ("Usage:\n%s\n", cmdtp->usage);
730 return 1;
731 case 3:
732 if (strcmp(argv[1],"device") == 0) {
733 int dev = (int)simple_strtoul(argv[2], NULL, 10);
734
735 printf ("\nDevice %d: ", dev);
736 if (dev >= CFG_MAX_NAND_DEVICE) {
737 puts ("unknown device\n");
738 return 1;
739 }
740 nand_print(&nand_dev_desc[dev]);
741 /*nand_print (dev);*/
742
743 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
744 return 1;
745 }
746
747 curr_device = dev;
748
749 puts ("... is now current device\n");
750
751 return 0;
752 }
wdenkabda5ca2003-05-31 18:35:21 +0000753 else if (strcmp(argv[1],"erase") == 0 && strcmp(argv[2], "clean") == 0) {
754 struct nand_chip* nand = &nand_dev_desc[curr_device];
755 ulong off = 0;
756 ulong size = nand->totlen;
757 int ret;
758
759 printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
760 curr_device, off, size);
761
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100762 ret = nand_legacy_erase (nand, off, size, 1);
wdenkabda5ca2003-05-31 18:35:21 +0000763
764 printf("%s\n", ret ? "ERROR" : "OK");
765
766 return ret;
767 }
wdenkc8434db2003-03-26 06:55:25 +0000768
769 printf ("Usage:\n%s\n", cmdtp->usage);
770 return 1;
771 default:
772 /* at least 4 args */
773
wdenkabda5ca2003-05-31 18:35:21 +0000774 if (strncmp(argv[1], "read", 4) == 0 ||
775 strncmp(argv[1], "write", 5) == 0) {
wdenkc8434db2003-03-26 06:55:25 +0000776 ulong addr = simple_strtoul(argv[2], NULL, 16);
777 ulong off = simple_strtoul(argv[3], NULL, 16);
778 ulong size = simple_strtoul(argv[4], NULL, 16);
wdenkabda5ca2003-05-31 18:35:21 +0000779 int cmd = (strncmp(argv[1], "read", 4) == 0) ?
780 NANDRW_READ : NANDRW_WRITE;
wdenkc8434db2003-03-26 06:55:25 +0000781 int ret, total;
wdenkabda5ca2003-05-31 18:35:21 +0000782 char* cmdtail = strchr(argv[1], '.');
783
784 if (cmdtail && !strncmp(cmdtail, ".oob", 2)) {
785 /* read out-of-band data */
786 if (cmd & NANDRW_READ) {
787 ret = nand_read_oob(nand_dev_desc + curr_device,
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200788 off, size, (size_t *)&total,
wdenkabda5ca2003-05-31 18:35:21 +0000789 (u_char*)addr);
790 }
791 else {
792 ret = nand_write_oob(nand_dev_desc + curr_device,
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200793 off, size, (size_t *)&total,
wdenkabda5ca2003-05-31 18:35:21 +0000794 (u_char*)addr);
795 }
796 return ret;
797 }
798 else if (cmdtail && !strncmp(cmdtail, ".jffs2", 2))
799 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk145d2c12004-04-15 21:48:45 +0000800 else if (cmdtail && !strncmp(cmdtail, ".jffs2s", 2)) {
801 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
802 if (cmd & NANDRW_READ)
803 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
804 }
wdenkabda5ca2003-05-31 18:35:21 +0000805#ifdef SXNI855T
806 /* need ".e" same as ".j" for compatibility with older units */
807 else if (cmdtail && !strcmp(cmdtail, ".e"))
808 cmd |= NANDRW_JFFS2; /* skip bad blocks */
809#endif
stroese317cbe62004-12-16 17:45:46 +0000810#ifdef CFG_NAND_SKIP_BAD_DOT_I
811 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
812 /* ".i" for image -> read skips bad block (no 0xff) */
Stefan Roesef9b85672005-08-12 16:46:35 +0200813 else if (cmdtail && !strcmp(cmdtail, ".i")) {
stroese317cbe62004-12-16 17:45:46 +0000814 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
815 if (cmd & NANDRW_READ)
816 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
Stefan Roesef9b85672005-08-12 16:46:35 +0200817 }
stroese317cbe62004-12-16 17:45:46 +0000818#endif /* CFG_NAND_SKIP_BAD_DOT_I */
wdenkabda5ca2003-05-31 18:35:21 +0000819 else if (cmdtail) {
820 printf ("Usage:\n%s\n", cmdtp->usage);
821 return 1;
822 }
wdenkc8434db2003-03-26 06:55:25 +0000823
Stefan Roesed351b2b2006-10-10 12:36:02 +0200824 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
wdenkabda5ca2003-05-31 18:35:21 +0000825 (cmd & NANDRW_READ) ? "read" : "write",
826 curr_device, off, size);
wdenkc8434db2003-03-26 06:55:25 +0000827
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100828 ret = nand_legacy_rw(nand_dev_desc + curr_device, cmd, off, size,
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200829 (size_t *)&total, (u_char*)addr);
wdenkc8434db2003-03-26 06:55:25 +0000830
wdenke58b0dc2003-07-27 00:21:01 +0000831 printf (" %d bytes %s: %s\n", total,
wdenk8886a662004-04-18 19:43:36 +0000832 (cmd & NANDRW_READ) ? "read" : "written",
wdenkc8434db2003-03-26 06:55:25 +0000833 ret ? "ERROR" : "OK");
834
835 return ret;
wdenkabda5ca2003-05-31 18:35:21 +0000836 } else if (strcmp(argv[1],"erase") == 0 &&
837 (argc == 4 || strcmp("clean", argv[2]) == 0)) {
838 int clean = argc == 5;
839 ulong off = simple_strtoul(argv[2 + clean], NULL, 16);
840 ulong size = simple_strtoul(argv[3 + clean], NULL, 16);
wdenkc8434db2003-03-26 06:55:25 +0000841 int ret;
842
Stefan Roesed351b2b2006-10-10 12:36:02 +0200843 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
wdenkc8434db2003-03-26 06:55:25 +0000844 curr_device, off, size);
845
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100846 ret = nand_legacy_erase (nand_dev_desc + curr_device,
847 off, size, clean);
wdenkc8434db2003-03-26 06:55:25 +0000848
849 printf("%s\n", ret ? "ERROR" : "OK");
850
851 return ret;
852 } else {
853 printf ("Usage:\n%s\n", cmdtp->usage);
854 rcode = 1;
855 }
856
857 return rcode;
858 }
859}
860
wdenkf287a242003-07-01 21:06:45 +0000861U_BOOT_CMD(
862 nand, 5, 1, do_nand,
Stefan Roesed351b2b2006-10-10 12:36:02 +0200863 "nand - legacy NAND sub-system\n",
wdenkf12e3962003-06-29 21:03:46 +0000864 "info - show available NAND devices\n"
865 "nand device [dev] - show or set current device\n"
wdenk145d2c12004-04-15 21:48:45 +0000866 "nand read[.jffs2[s]] addr off size\n"
wdenkf12e3962003-06-29 21:03:46 +0000867 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
868 " at offset `off' to/from memory address `addr'\n"
869 "nand erase [clean] [off size] - erase `size' bytes from\n"
870 " offset `off' (entire device if not specified)\n"
871 "nand bad - show bad blocks\n"
872 "nand read.oob addr off size - read out-of-band data\n"
873 "nand write.oob addr off size - read out-of-band data\n"
874);
875
wdenkc8434db2003-03-26 06:55:25 +0000876int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
877{
878 char *boot_device = NULL;
879 char *ep;
880 int dev;
881 ulong cnt;
882 ulong addr;
883 ulong offset = 0;
884 image_header_t *hdr;
885 int rcode = 0;
886 switch (argc) {
887 case 1:
888 addr = CFG_LOAD_ADDR;
889 boot_device = getenv ("bootdevice");
890 break;
891 case 2:
892 addr = simple_strtoul(argv[1], NULL, 16);
893 boot_device = getenv ("bootdevice");
894 break;
895 case 3:
896 addr = simple_strtoul(argv[1], NULL, 16);
897 boot_device = argv[2];
898 break;
899 case 4:
900 addr = simple_strtoul(argv[1], NULL, 16);
901 boot_device = argv[2];
902 offset = simple_strtoul(argv[3], NULL, 16);
903 break;
904 default:
905 printf ("Usage:\n%s\n", cmdtp->usage);
906 SHOW_BOOT_PROGRESS (-1);
907 return 1;
908 }
909
910 if (!boot_device) {
911 puts ("\n** No boot device **\n");
912 SHOW_BOOT_PROGRESS (-1);
913 return 1;
914 }
915
916 dev = simple_strtoul(boot_device, &ep, 16);
917
918 if ((dev >= CFG_MAX_NAND_DEVICE) ||
919 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
920 printf ("\n** Device %d not available\n", dev);
921 SHOW_BOOT_PROGRESS (-1);
922 return 1;
923 }
924
wdenkabda5ca2003-05-31 18:35:21 +0000925 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
wdenkc8434db2003-03-26 06:55:25 +0000926 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
927 offset);
928
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100929 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
930 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkc8434db2003-03-26 06:55:25 +0000931 printf ("** Read error on %d\n", dev);
932 SHOW_BOOT_PROGRESS (-1);
933 return 1;
934 }
935
936 hdr = (image_header_t *)addr;
937
938 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
939
940 print_image_hdr (hdr);
941
942 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
943 cnt -= SECTORSIZE;
944 } else {
Wolfgang Denkb6929f92006-03-12 01:59:35 +0100945 printf ("\n** Bad Magic Number 0x%x **\n", ntohl(hdr->ih_magic));
wdenkc8434db2003-03-26 06:55:25 +0000946 SHOW_BOOT_PROGRESS (-1);
947 return 1;
948 }
949
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100950 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
951 offset + SECTORSIZE, cnt, NULL,
952 (u_char *)(addr+SECTORSIZE))) {
wdenkc8434db2003-03-26 06:55:25 +0000953 printf ("** Read error on %d\n", dev);
954 SHOW_BOOT_PROGRESS (-1);
955 return 1;
956 }
957
958 /* Loading ok, update default load address */
959
960 load_addr = addr;
961
962 /* Check if we should attempt an auto-start */
963 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
964 char *local_args[2];
965 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
966
967 local_args[0] = argv[0];
968 local_args[1] = NULL;
969
wdenkabda5ca2003-05-31 18:35:21 +0000970 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkc8434db2003-03-26 06:55:25 +0000971
972 do_bootm (cmdtp, 0, 1, local_args);
973 rcode = 1;
974 }
975 return rcode;
976}
977
wdenkf287a242003-07-01 21:06:45 +0000978U_BOOT_CMD(
979 nboot, 4, 1, do_nandboot,
wdenkf12e3962003-06-29 21:03:46 +0000980 "nboot - boot from NAND device\n",
981 "loadAddr dev\n"
982);
983
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100984#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
wdenkc8434db2003-03-26 06:55:25 +0000985
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100986#endif /* CFG_NAND_LEGACY */