Commit 2255b2d2 authored by Stefan Roese's avatar Stefan Roese

* Several improvements to the new NAND subsystem:

  - JFFS2 related commands implemented in mtd-utils style
  - Support for bad blocks
  - Bad block testing commands
  - NAND lock commands
  Please take a look at doc/README.nand for more details
  Patch by Guido Classen, 10 Oct 2006
parent a3bb7bfc
......@@ -2,6 +2,14 @@
Changes since U-Boot 1.1.4:
======================================================================
* Several improvements to the new NAND subsystem:
- JFFS2 related commands implemented in mtd-utils style
- Support for bad blocks
- Bad block testing commands
- NAND lock commands
Please take a look at doc/README.nand for more details
Patch by Guido Classen, 10 Oct 2006
* Define IH_CPU_AVR32
Make it possible to generate AVR32 uImage files with mkimage and
make cmd_bootm recognize them.
......
......@@ -135,11 +135,16 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
ulong addr, off, size;
char *cmd, *s;
nand_info_t *nand;
int quiet = 0;
const char *quiet_str = getenv("quiet");
/* at least two arguments please */
if (argc < 2)
goto usage;
if (quiet_str)
quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
cmd = argv[1];
if (strcmp(cmd, "info") == 0) {
......@@ -178,7 +183,10 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
strncmp(cmd, "dump", 4) != 0 &&
strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0)
strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
strcmp(cmd, "biterr") != 0 &&
strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
goto usage;
/* the following commands operate on the current device */
......@@ -197,14 +205,64 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
return 0;
}
if (strcmp(cmd, "erase") == 0) {
arg_off_size(argc - 2, argv + 2, &off, &size, nand->size);
if (off == 0 && size == 0)
return 1;
if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
nand_erase_options_t opts;
int clean = argc >= 3 && !strcmp("clean", argv[2]);
int rest_argc = argc - 2;
char **rest_argv = argv + 2;
int scrub = !strcmp(cmd, "scrub");
if (clean) {
rest_argc--;
rest_argv++;
}
if (rest_argc == 0) {
printf("\nNAND erase: device %d offset 0x%x, size 0x%x ",
nand_curr_device, off, size);
ret = nand_erase(nand, off, size);
printf("\nNAND %s: device %d whole chip\n",
cmd,
nand_curr_device);
off = size = 0;
} else {
arg_off_size(rest_argc, rest_argv, &off, &size,
nand->size);
if (off == 0 && size == 0)
return 1;
printf("\nNAND %s: device %d offset 0x%x, size 0x%x\n",
cmd, nand_curr_device, off, size);
}
memset(&opts, 0, sizeof(opts));
opts.offset = off;
opts.length = size;
opts.jffs2 = clean;
opts.quiet = quiet;
if (scrub) {
printf("Warning: "
"scrub option will erase all factory set "
"bad blocks!\n"
" "
"There is no reliable way to recover them.\n"
" "
"Use this command only for testing purposes "
"if you\n"
" "
"are shure of what you are doing!\n"
"\nReally scrub this NAND flash? <y/N>\n"
);
if (getc() == 'y' && getc() == '\r') {
opts.scrub = 1;
} else {
printf("scrub aborted\n");
return -1;
}
}
ret = nand_erase_opts(nand, &opts);
printf("%s\n", ret ? "ERROR" : "OK");
return ret == 0 ? 0 : 1;
......@@ -228,37 +286,153 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
/* read write */
if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
int read;
if (argc < 4)
goto usage;
/*
s = strchr(cmd, '.');
clean = CLEAN_NONE;
if (s != NULL) {
if (strcmp(s, ".jffs2") == 0 || strcmp(s, ".e") == 0
|| strcmp(s, ".i"))
clean = CLEAN_JFFS2;
}
*/
addr = (ulong)simple_strtoul(argv[2], NULL, 16);
arg_off_size(argc - 3, argv + 3, &off, &size, nand->size);
if (off == 0 && size == 0)
return 1;
i = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
printf("\nNAND %s: device %d offset %u, size %u ... ",
i ? "read" : "write", nand_curr_device, off, size);
read ? "read" : "write", nand_curr_device, off, size);
if (i)
s = strchr(cmd, '.');
if (s != NULL &&
(!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
if (read) {
/* read */
nand_read_options_t opts;
memset(&opts, 0, sizeof(opts));
opts.buffer = (u_char*) addr;
opts.length = size;
opts.offset = off;
opts.quiet = quiet;
ret = nand_read_opts(nand, &opts);
} else {
/* write */
nand_write_options_t opts;
memset(&opts, 0, sizeof(opts));
opts.buffer = (u_char*) addr;
opts.length = size;
opts.offset = off;
/* opts.forcejffs2 = 1; */
opts.pad = 1;
opts.blockalign = 1;
opts.quiet = quiet;
ret = nand_write_opts(nand, &opts);
}
printf("%s\n", ret ? "ERROR" : "OK");
return ret == 0 ? 0 : 1;
}
if (read)
ret = nand_read(nand, off, &size, (u_char *)addr);
else
ret = nand_write(nand, off, &size, (u_char *)addr);
printf(" %d bytes %s: %s\n", size,
i ? "read" : "written", ret ? "ERROR" : "OK");
read ? "read" : "written", ret ? "ERROR" : "OK");
return ret == 0 ? 0 : 1;
}
/* 2006-09-28 gc: implement missing commands */
if (strcmp(cmd, "markbad") == 0) {
addr = (ulong)simple_strtoul(argv[2], NULL, 16);
int ret = nand->block_markbad(nand, addr);
if (ret == 0) {
printf("block 0x%08lx successfully marked as bad\n",
(ulong) addr);
return 0;
} else {
printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
(ulong) addr, ret);
}
return 1;
}
if (strcmp(cmd, "biterr") == 0) {
/* todo */
return 1;
}
if (strcmp(cmd, "lock") == 0) {
int tight = 0;
int status = 0;
if (argc == 3) {
if (!strcmp("tight", argv[2]))
tight = 1;
if (!strcmp("status", argv[2]))
status = 1;
}
if (status) {
ulong block_start = 0;
ulong off;
int last_status = -1;
struct nand_chip *nand_chip = nand->priv;
/* check the WP bit */
nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
printf("device is %swrite protected\n",
(nand_chip->read_byte(nand) & 0x80 ?
"NOT " : "" ) );
for (off = 0; off < nand->size; off += nand->oobblock) {
int s = nand_get_lock_status(nand, off);
/* print message only if status has changed
* or at end of chip
*/
if (off == nand->size - nand->oobblock
|| (s != last_status && off != 0)) {
printf("%08x - %08x: %8d pages %s%s%s\n",
block_start,
off-1,
(off-block_start)/nand->oobblock,
((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
}
last_status = s;
}
} else {
if (!nand_lock(nand, tight)) {
printf ("NAND flash successfully locked\n");
} else {
printf ("Error locking NAND flash. \n");
return 1;
}
}
return 0;
}
if (strcmp(cmd, "unlock") == 0) {
if (argc == 2) {
off = 0;
size = nand->size;
} else {
arg_off_size(argc - 2, argv + 2, &off, &size,
nand->size);
}
if (!nand_unlock(nand, off, size)) {
printf("NAND flash successfully unlocked\n");
} else {
printf("Error unlocking NAND flash. "
"Write and erase will probably fail\n");
return 1;
}
return 0;
}
usage:
printf("Usage:\n%s\n", cmdtp->usage);
return 1;
......@@ -277,7 +451,9 @@ U_BOOT_CMD(nand, 5, 1, do_nand,
"nand dump[.oob] off - dump page\n"
"nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
"nand markbad off - mark bad block at offset (UNSAFE)\n"
"nand biterr off - make a bit error at offset (UNSAFE)\n");
"nand biterr off - make a bit error at offset (UNSAFE)\n"
"nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
"nand unlock [offset] [size] - unlock section\n");
int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
{
......@@ -596,7 +772,7 @@ int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
return 1;
}
printf ("\nNAND %s: device %d offset %ld, size %ld ... ",
printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
(cmd & NANDRW_READ) ? "read" : "write",
curr_device, off, size);
......@@ -615,7 +791,7 @@ int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
ulong size = simple_strtoul(argv[3 + clean], NULL, 16);
int ret;
printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
curr_device, off, size);
ret = nand_legacy_erase (nand_dev_desc + curr_device,
......@@ -635,7 +811,7 @@ int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
U_BOOT_CMD(
nand, 5, 1, do_nand,
"nand - NAND sub-system\n",
"nand - legacy NAND sub-system\n",
"info - show available NAND devices\n"
"nand device [dev] - show or set current device\n"
"nand read[.jffs2[s]] addr off size\n"
......
......@@ -207,3 +207,47 @@ As mentioned above, the legacy code is still used by the DoC subsystem.
The consequence of this is that the legacy NAND can't be removed from
the tree until the DoC is ported to use the new NAND support (or boards
with DoC will break).
Additional improvements to the NAND subsystem by Guido Classen, 10-10-2006
JFFS2 related commands:
implement "nand erase clean" and old "nand erase"
using both the new code which is able to skip bad blocks
"nand erase clean" additionally writes JFFS2-cleanmarkers in the oob.
"nand write.jffs2"
like "nand write" but skip found bad eraseblocks
"nand read.jffs2"
like "nand read" but skip found bad eraseblocks
Miscellaneous and testing commands:
"markbad [offset]"
create an artificial bad block (for testing bad block handling)
"scrub [offset length]"
like "erase" but don't skip bad block. Instead erase them.
DANGEROUS!!! Factory set bad blocks will be lost. Use only
to remove artificial bad blocks created with the "markbad" command.
NAND locking command (for chips with active LOCKPRE pin)
"nand lock"
set NAND chip to lock state (all pages locked)
"nand lock tight"
set NAND chip to lock tight state (software can't change locking anymore)
"nand lock status"
displays current locking status of all pages
"nand unlock [offset] [size]"
unlock consecutive area (can be called multiple times for different areas)
I have tested the code with board containing 128MiB NAND large page chips
and 32MiB small page chips.
......@@ -25,7 +25,7 @@ include $(TOPDIR)/config.mk
LIB := $(obj)libnand.a
COBJS := nand.o nand_base.o nand_ids.o nand_ecc.o nand_bbt.o
COBJS := nand.o nand_base.o nand_ids.o nand_ecc.o nand_bbt.o nand_util.o
SRCS := $(COBJS:.o=.c)
OBJS := $(addprefix $(obj),$(COBJS))
......
This diff is collapsed.
......@@ -60,4 +60,61 @@ static inline int nand_erase(nand_info_t *info, ulong off, ulong size)
return info->erase(info, &instr);
}
/*****************************************************************************
* declarations from nand_util.c
****************************************************************************/
struct nand_write_options {
u_char *buffer; /* memory block containing image to write */
ulong length; /* number of bytes to write */
ulong offset; /* start address in NAND */
int quiet; /* don't display progress messages */
int autoplace; /* if true use auto oob layout */
int forcejffs2; /* force jffs2 oob layout */
int forceyaffs; /* force yaffs oob layout */
int noecc; /* write without ecc */
int writeoob; /* image contains oob data */
int pad; /* pad to page size */
int blockalign; /* 1|2|4 set multiple of eraseblocks
* to align to */
};
typedef struct nand_write_options nand_write_options_t;
struct nand_read_options {
u_char *buffer; /* memory block in which read image is written*/
ulong length; /* number of bytes to read */
ulong offset; /* start address in NAND */
int quiet; /* don't display progress messages */
int readoob; /* put oob data in image */
};
typedef struct nand_read_options nand_read_options_t;
struct nand_erase_options {
ulong length; /* number of bytes to erase */
ulong offset; /* first address in NAND to erase */
int quiet; /* don't display progress messages */
int jffs2; /* if true: format for jffs2 usage
* (write appropriate cleanmarker blocks) */
int scrub; /* if true, really clean NAND by erasing
* bad blocks (UNSAFE) */
};
typedef struct nand_erase_options nand_erase_options_t;
int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts);
int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts);
int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts);
#define NAND_LOCK_STATUS_TIGHT 0x01
#define NAND_LOCK_STATUS_LOCK 0x02
#define NAND_LOCK_STATUS_UNLOCK 0x04
int nand_lock( nand_info_t *meminfo, int tight );
int nand_unlock( nand_info_t *meminfo, ulong start, ulong length );
int nand_get_lock_status(nand_info_t *meminfo, ulong offset);
#endif
......@@ -171,7 +171,9 @@ uLong ZEXPORT crc32(crc, buf, len)
return crc ^ 0xffffffffL;
}
#if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) \
|| (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
/* No ones complement version. JFFS2 (and other things ?)
* don't use ones compliment in their CRC calculations.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment