wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1 | /* |
| 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> |
wdenk | 384cc68 | 2005-04-03 22:35:21 +0000 | [diff] [blame] | 6 | * |
| 7 | * Added 16-bit nand support |
| 8 | * (C) 2004 Texas Instruments |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 12 | |
| 13 | |
Jean-Christophe PLAGNIOL-VILLARD | cc4a0ce | 2008-08-13 01:40:43 +0200 | [diff] [blame^] | 14 | #ifndef CONFIG_NAND_LEGACY |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 15 | /* |
| 16 | * |
| 17 | * New NAND support |
| 18 | * |
| 19 | */ |
| 20 | #include <common.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 21 | #include <linux/mtd/mtd.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 22 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 23 | #if defined(CONFIG_CMD_NAND) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 24 | |
| 25 | #include <command.h> |
| 26 | #include <watchdog.h> |
| 27 | #include <malloc.h> |
| 28 | #include <asm/byteorder.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 29 | #include <jffs2/jffs2.h> |
| 30 | #include <nand.h> |
| 31 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 32 | #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 33 | |
| 34 | /* parition handling routines */ |
| 35 | int mtdparts_init(void); |
| 36 | int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num); |
| 37 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 38 | u8 *part_num, struct part_info **part); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 41 | static int nand_dump(nand_info_t *nand, ulong off, int only_oob) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 42 | { |
| 43 | int i; |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 44 | u_char *datbuf, *oobbuf, *p; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 45 | |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 46 | datbuf = malloc(nand->writesize + nand->oobsize); |
| 47 | oobbuf = malloc(nand->oobsize); |
| 48 | if (!datbuf || !oobbuf) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 49 | puts("No memory for page buffer\n"); |
| 50 | return 1; |
| 51 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 52 | off &= ~(nand->writesize - 1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 53 | loff_t addr = (loff_t) off; |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 54 | 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 Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 62 | if (i < 0) { |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 63 | printf("Error (%d) reading page %08lx\n", i, off); |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 64 | free(datbuf); |
| 65 | free(oobbuf); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 66 | return 1; |
| 67 | } |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 68 | printf("Page %08lx dump:\n", off); |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 69 | i = nand->writesize >> 4; |
| 70 | p = datbuf; |
| 71 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 72 | while (i--) { |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 73 | 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], |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 77 | p[8], p[9], p[10], p[11], p[12], p[13], p[14], |
| 78 | p[15]); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 79 | p += 16; |
| 80 | } |
| 81 | puts("OOB:\n"); |
| 82 | i = nand->oobsize >> 3; |
| 83 | while (i--) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 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]); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 86 | p += 8; |
| 87 | } |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 88 | free(datbuf); |
| 89 | free(oobbuf); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /* ------------------------------------------------------------------------- */ |
| 95 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 96 | static inline int str2long(char *p, ulong *num) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 97 | { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 98 | char *endptr; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 99 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 100 | *num = simple_strtoul(p, &endptr, 16); |
| 101 | return (*p != '\0' && *endptr == '\0') ? 1 : 0; |
| 102 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 103 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 104 | static int |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 105 | arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 106 | { |
| 107 | int idx = nand_curr_device; |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 108 | #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 109 | struct mtd_device *dev; |
| 110 | struct part_info *part; |
| 111 | u8 pnum; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 112 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 113 | if (argc >= 1 && !(str2long(argv[0], off))) { |
| 114 | if ((mtdparts_init() == 0) && |
| 115 | (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) { |
| 116 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 117 | puts("not a NAND device\n"); |
| 118 | return -1; |
| 119 | } |
| 120 | *off = part->offset; |
| 121 | if (argc >= 2) { |
Wolfgang Denk | 42ffcec | 2008-04-30 17:46:26 +0200 | [diff] [blame] | 122 | if (!(str2long(argv[1], (ulong *)size))) { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 123 | printf("'%s' is not a number\n", argv[1]); |
| 124 | return -1; |
| 125 | } |
| 126 | if (*size > part->size) |
| 127 | *size = part->size; |
| 128 | } else { |
| 129 | *size = part->size; |
| 130 | } |
| 131 | idx = dev->id->num; |
| 132 | *nand = nand_info[idx]; |
| 133 | goto out; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 134 | } |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 135 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 136 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 137 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 138 | if (argc >= 1) { |
| 139 | if (!(str2long(argv[0], off))) { |
| 140 | printf("'%s' is not a number\n", argv[0]); |
| 141 | return -1; |
| 142 | } |
| 143 | } else { |
| 144 | *off = 0; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 147 | if (argc >= 2) { |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 148 | if (!(str2long(argv[1], (ulong *)size))) { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 149 | printf("'%s' is not a number\n", argv[1]); |
| 150 | return -1; |
| 151 | } |
| 152 | } else { |
| 153 | *size = nand->size - *off; |
| 154 | } |
| 155 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 156 | #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 157 | out: |
| 158 | #endif |
| 159 | printf("device %d ", idx); |
| 160 | if (*size == nand->size) |
| 161 | puts("whole chip\n"); |
| 162 | else |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 163 | printf("offset 0x%lx, size 0x%x\n", *off, *size); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 164 | return 0; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 168 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 169 | int i, dev, ret = 0; |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 170 | ulong addr, off; |
| 171 | size_t size; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 172 | char *cmd, *s; |
| 173 | nand_info_t *nand; |
Matthias Fuchs | c750d2e | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 174 | #ifdef CFG_NAND_QUIET |
| 175 | int quiet = CFG_NAND_QUIET; |
| 176 | #else |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 177 | int quiet = 0; |
Matthias Fuchs | c750d2e | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 178 | #endif |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 179 | const char *quiet_str = getenv("quiet"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 180 | |
| 181 | /* at least two arguments please */ |
| 182 | if (argc < 2) |
| 183 | goto usage; |
| 184 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 185 | if (quiet_str) |
| 186 | quiet = simple_strtoul(quiet_str, NULL, 0) != 0; |
| 187 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 188 | cmd = argv[1]; |
| 189 | |
| 190 | if (strcmp(cmd, "info") == 0) { |
| 191 | |
| 192 | putc('\n'); |
| 193 | for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) { |
| 194 | if (nand_info[i].name) |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 195 | printf("Device %d: %s, sector size %u KiB\n", |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 196 | i, nand_info[i].name, |
| 197 | nand_info[i].erasesize >> 10); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | if (strcmp(cmd, "device") == 0) { |
| 203 | |
| 204 | if (argc < 3) { |
| 205 | if ((nand_curr_device < 0) || |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 206 | (nand_curr_device >= CFG_MAX_NAND_DEVICE)) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 207 | puts("\nno devices available\n"); |
| 208 | else |
| 209 | printf("\nDevice %d: %s\n", nand_curr_device, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 210 | nand_info[nand_curr_device].name); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 214 | if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) { |
| 215 | puts("No such device\n"); |
| 216 | return 1; |
| 217 | } |
| 218 | printf("Device %d: %s", dev, nand_info[dev].name); |
| 219 | puts("... is now current device\n"); |
| 220 | nand_curr_device = dev; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 221 | |
| 222 | #ifdef CFG_NAND_SELECT_DEVICE |
| 223 | /* |
| 224 | * Select the chip in the board/cpu specific driver |
| 225 | */ |
| 226 | board_nand_select_device(nand_info[dev].priv, dev); |
| 227 | #endif |
| 228 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 && |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 233 | strncmp(cmd, "dump", 4) != 0 && |
| 234 | strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 && |
| 235 | strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 && |
| 236 | strcmp(cmd, "biterr") != 0 && |
| 237 | strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 ) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 238 | goto usage; |
| 239 | |
| 240 | /* the following commands operate on the current device */ |
| 241 | if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE || |
| 242 | !nand_info[nand_curr_device].name) { |
| 243 | puts("\nno devices available\n"); |
| 244 | return 1; |
| 245 | } |
| 246 | nand = &nand_info[nand_curr_device]; |
| 247 | |
| 248 | if (strcmp(cmd, "bad") == 0) { |
| 249 | printf("\nDevice %d bad blocks:\n", nand_curr_device); |
| 250 | for (off = 0; off < nand->size; off += nand->erasesize) |
| 251 | if (nand_block_isbad(nand, off)) |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 252 | printf(" %08lx\n", off); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 256 | /* |
| 257 | * Syntax is: |
| 258 | * 0 1 2 3 4 |
| 259 | * nand erase [clean] [off size] |
| 260 | */ |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 261 | if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) { |
| 262 | nand_erase_options_t opts; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 263 | /* "clean" at index 2 means request to write cleanmarker */ |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 264 | int clean = argc > 2 && !strcmp("clean", argv[2]); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 265 | int o = clean ? 3 : 2; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 266 | int scrub = !strcmp(cmd, "scrub"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 267 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 268 | printf("\nNAND %s: ", scrub ? "scrub" : "erase"); |
| 269 | /* skip first two or three arguments, look for offset and size */ |
| 270 | if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0) |
| 271 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 272 | |
| 273 | memset(&opts, 0, sizeof(opts)); |
| 274 | opts.offset = off; |
| 275 | opts.length = size; |
| 276 | opts.jffs2 = clean; |
| 277 | opts.quiet = quiet; |
| 278 | |
| 279 | if (scrub) { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 280 | puts("Warning: " |
| 281 | "scrub option will erase all factory set " |
| 282 | "bad blocks!\n" |
| 283 | " " |
| 284 | "There is no reliable way to recover them.\n" |
| 285 | " " |
| 286 | "Use this command only for testing purposes " |
| 287 | "if you\n" |
| 288 | " " |
| 289 | "are sure of what you are doing!\n" |
| 290 | "\nReally scrub this NAND flash? <y/N>\n"); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 291 | |
| 292 | if (getc() == 'y' && getc() == '\r') { |
| 293 | opts.scrub = 1; |
| 294 | } else { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 295 | puts("scrub aborted\n"); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 296 | return -1; |
| 297 | } |
| 298 | } |
| 299 | ret = nand_erase_opts(nand, &opts); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 300 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 301 | |
| 302 | return ret == 0 ? 0 : 1; |
| 303 | } |
| 304 | |
| 305 | if (strncmp(cmd, "dump", 4) == 0) { |
| 306 | if (argc < 3) |
| 307 | goto usage; |
| 308 | |
| 309 | s = strchr(cmd, '.'); |
| 310 | off = (int)simple_strtoul(argv[2], NULL, 16); |
| 311 | |
| 312 | if (s != NULL && strcmp(s, ".oob") == 0) |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 313 | ret = nand_dump(nand, off, 1); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 314 | else |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 315 | ret = nand_dump(nand, off, 0); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 316 | |
| 317 | return ret == 0 ? 1 : 0; |
| 318 | |
| 319 | } |
| 320 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 321 | if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 322 | int read; |
| 323 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 324 | if (argc < 4) |
| 325 | goto usage; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 326 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 327 | addr = (ulong)simple_strtoul(argv[2], NULL, 16); |
| 328 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 329 | read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 330 | printf("\nNAND %s: ", read ? "read" : "write"); |
| 331 | if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0) |
| 332 | return 1; |
William Juul | 4cbb651 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 333 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 334 | s = strchr(cmd, '.'); |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 335 | if (!s || !strcmp(s, ".jffs2") || |
| 336 | !strcmp(s, ".e") || !strcmp(s, ".i")) { |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 337 | if (read) |
| 338 | ret = nand_read_skip_bad(nand, off, &size, |
| 339 | (u_char *)addr); |
| 340 | else |
| 341 | ret = nand_write_skip_bad(nand, off, &size, |
| 342 | (u_char *)addr); |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 343 | } else if (s != NULL && !strcmp(s, ".oob")) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 344 | /* out-of-band data */ |
| 345 | mtd_oob_ops_t ops = { |
| 346 | .oobbuf = (u8 *)addr, |
| 347 | .ooblen = size, |
| 348 | .mode = MTD_OOB_RAW |
| 349 | }; |
| 350 | |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 351 | if (read) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 352 | ret = nand->read_oob(nand, off, &ops); |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 353 | else |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 354 | ret = nand->write_oob(nand, off, &ops); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 355 | } else { |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 356 | printf("Unknown nand command suffix '%s'.\n", s); |
| 357 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 360 | printf(" %d bytes %s: %s\n", size, |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 361 | read ? "read" : "written", ret ? "ERROR" : "OK"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 362 | |
| 363 | return ret == 0 ? 0 : 1; |
| 364 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 365 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 366 | if (strcmp(cmd, "markbad") == 0) { |
| 367 | addr = (ulong)simple_strtoul(argv[2], NULL, 16); |
| 368 | |
| 369 | int ret = nand->block_markbad(nand, addr); |
| 370 | if (ret == 0) { |
| 371 | printf("block 0x%08lx successfully marked as bad\n", |
| 372 | (ulong) addr); |
| 373 | return 0; |
| 374 | } else { |
| 375 | printf("block 0x%08lx NOT marked as bad! ERROR %d\n", |
| 376 | (ulong) addr, ret); |
| 377 | } |
| 378 | return 1; |
| 379 | } |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 380 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 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 | } |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 395 | /* |
| 396 | * ! BROKEN ! |
Stefan Roese | deac913 | 2008-01-05 16:50:32 +0100 | [diff] [blame] | 397 | * |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 398 | * TODO: must be implemented and tested by someone with HW |
| 399 | */ |
| 400 | #if 0 |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 401 | if (status) { |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 402 | ulong block_start = 0; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 403 | ulong off; |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 404 | int last_status = -1; |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 405 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 406 | struct nand_chip *nand_chip = nand->priv; |
| 407 | /* check the WP bit */ |
| 408 | nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1); |
| 409 | printf("device is %swrite protected\n", |
| 410 | (nand_chip->read_byte(nand) & 0x80 ? |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 411 | "NOT " : "")); |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 412 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 413 | for (off = 0; off < nand->size; off += nand->writesize) { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 414 | int s = nand_get_lock_status(nand, off); |
| 415 | |
| 416 | /* print message only if status has changed |
| 417 | * or at end of chip |
| 418 | */ |
| 419 | if (off == nand->size - nand->writesize |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 420 | || (s != last_status && off != 0)) { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 421 | |
| 422 | printf("%08lx - %08lx: %8d pages %s%s%s\n", |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 423 | block_start, |
| 424 | off-1, |
| 425 | (off-block_start)/nand->writesize, |
| 426 | ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""), |
| 427 | ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""), |
| 428 | ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : "")); |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | last_status = s; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 432 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 433 | } else { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 434 | if (!nand_lock(nand, tight)) { |
| 435 | puts("NAND flash successfully locked\n"); |
| 436 | } else { |
| 437 | puts("Error locking NAND flash\n"); |
| 438 | return 1; |
| 439 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 440 | } |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 441 | #endif |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | if (strcmp(cmd, "unlock") == 0) { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 446 | if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0) |
| 447 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 448 | |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 449 | /* |
| 450 | * ! BROKEN ! |
Stefan Roese | deac913 | 2008-01-05 16:50:32 +0100 | [diff] [blame] | 451 | * |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 452 | * TODO: must be implemented and tested by someone with HW |
| 453 | */ |
| 454 | #if 0 |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 455 | if (!nand_unlock(nand, off, size)) { |
| 456 | puts("NAND flash successfully unlocked\n"); |
| 457 | } else { |
| 458 | puts("Error unlocking NAND flash, " |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 459 | "write and erase will probably fail\n"); |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 460 | return 1; |
| 461 | } |
| 462 | #endif |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 466 | usage: |
| 467 | printf("Usage:\n%s\n", cmdtp->usage); |
| 468 | return 1; |
| 469 | } |
| 470 | |
| 471 | U_BOOT_CMD(nand, 5, 1, do_nand, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 472 | "nand - NAND sub-system\n", |
| 473 | "info - show available NAND devices\n" |
| 474 | "nand device [dev] - show or set current device\n" |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 475 | "nand read - addr off|partition size\n" |
| 476 | "nand write - addr off|partition size\n" |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 477 | " read/write 'size' bytes starting at offset 'off'\n" |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 478 | " to/from memory address 'addr', skipping bad blocks.\n" |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 479 | "nand erase [clean] [off size] - erase 'size' bytes from\n" |
| 480 | " offset 'off' (entire device if not specified)\n" |
| 481 | "nand bad - show bad blocks\n" |
| 482 | "nand dump[.oob] off - dump page\n" |
| 483 | "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n" |
| 484 | "nand markbad off - mark bad block at offset (UNSAFE)\n" |
| 485 | "nand biterr off - make a bit error at offset (UNSAFE)\n" |
| 486 | "nand lock [tight] [status]\n" |
| 487 | " bring nand to lock state or display locked pages\n" |
| 488 | "nand unlock [offset] [size] - unlock section\n"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 489 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 490 | static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 491 | ulong offset, ulong addr, char *cmd) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 492 | { |
| 493 | int r; |
Thomas Knobloch | 10e0389 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 494 | char *ep, *s; |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 495 | size_t cnt; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 496 | image_header_t *hdr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 497 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 498 | const void *fit_hdr = NULL; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 499 | #endif |
Thomas Knobloch | 10e0389 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 500 | |
| 501 | s = strchr(cmd, '.'); |
| 502 | if (s != NULL && |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 503 | (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) { |
| 504 | printf("Unknown nand load suffix '%s'\n", s); |
| 505 | show_boot_progress(-53); |
| 506 | return 1; |
| 507 | } |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 508 | |
| 509 | printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset); |
| 510 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 511 | cnt = nand->writesize; |
| 512 | r = nand_read(nand, offset, &cnt, (u_char *) addr); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 513 | if (r) { |
| 514 | puts("** Read error\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 515 | show_boot_progress (-56); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 516 | return 1; |
| 517 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 518 | show_boot_progress (56); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 519 | |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 520 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 521 | case IMAGE_FORMAT_LEGACY: |
| 522 | hdr = (image_header_t *)addr; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 523 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 524 | show_boot_progress (57); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 525 | image_print_contents (hdr); |
| 526 | |
| 527 | cnt = image_get_image_size (hdr); |
| 528 | break; |
| 529 | #if defined(CONFIG_FIT) |
| 530 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 531 | fit_hdr = (const void *)addr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 532 | puts ("Fit image detected...\n"); |
| 533 | |
| 534 | cnt = fit_get_size (fit_hdr); |
| 535 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 536 | #endif |
| 537 | default: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 538 | show_boot_progress (-57); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 539 | puts ("** Unknown image type\n"); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 540 | return 1; |
| 541 | } |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 542 | show_boot_progress (57); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 543 | |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 544 | /* FIXME: skip bad blocks */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 545 | r = nand_read(nand, offset, &cnt, (u_char *) addr); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 546 | if (r) { |
| 547 | puts("** Read error\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 548 | show_boot_progress (-58); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 549 | return 1; |
| 550 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 551 | show_boot_progress (58); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 552 | |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 553 | #if defined(CONFIG_FIT) |
| 554 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 555 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 556 | if (!fit_check_format (fit_hdr)) { |
| 557 | show_boot_progress (-150); |
| 558 | puts ("** Bad FIT image format\n"); |
| 559 | return 1; |
| 560 | } |
| 561 | show_boot_progress (151); |
| 562 | fit_print_contents (fit_hdr); |
| 563 | } |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 564 | #endif |
| 565 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 566 | /* Loading ok, update default load address */ |
| 567 | |
| 568 | load_addr = addr; |
| 569 | |
| 570 | /* Check if we should attempt an auto-start */ |
| 571 | if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) { |
| 572 | char *local_args[2]; |
| 573 | extern int do_bootm(cmd_tbl_t *, int, int, char *[]); |
| 574 | |
| 575 | local_args[0] = cmd; |
| 576 | local_args[1] = NULL; |
| 577 | |
| 578 | printf("Automatic boot of image at addr 0x%08lx ...\n", addr); |
| 579 | |
| 580 | do_bootm(cmdtp, 0, 1, local_args); |
| 581 | return 1; |
| 582 | } |
| 583 | return 0; |
| 584 | } |
| 585 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 586 | int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 587 | { |
| 588 | char *boot_device = NULL; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 589 | int idx; |
| 590 | ulong addr, offset = 0; |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 591 | #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 592 | struct mtd_device *dev; |
| 593 | struct part_info *part; |
| 594 | u8 pnum; |
| 595 | |
| 596 | if (argc >= 2) { |
| 597 | char *p = (argc == 2) ? argv[1] : argv[2]; |
| 598 | if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && |
| 599 | (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { |
| 600 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 601 | puts("Not a NAND device\n"); |
| 602 | return 1; |
| 603 | } |
| 604 | if (argc > 3) |
| 605 | goto usage; |
| 606 | if (argc == 3) |
Thomas Knobloch | 10e0389 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 607 | addr = simple_strtoul(argv[1], NULL, 16); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 608 | else |
| 609 | addr = CFG_LOAD_ADDR; |
| 610 | return nand_load_image(cmdtp, &nand_info[dev->id->num], |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 611 | part->offset, addr, argv[0]); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 615 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 616 | show_boot_progress(52); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 617 | switch (argc) { |
| 618 | case 1: |
| 619 | addr = CFG_LOAD_ADDR; |
| 620 | boot_device = getenv("bootdevice"); |
| 621 | break; |
| 622 | case 2: |
| 623 | addr = simple_strtoul(argv[1], NULL, 16); |
| 624 | boot_device = getenv("bootdevice"); |
| 625 | break; |
| 626 | case 3: |
| 627 | addr = simple_strtoul(argv[1], NULL, 16); |
| 628 | boot_device = argv[2]; |
| 629 | break; |
| 630 | case 4: |
| 631 | addr = simple_strtoul(argv[1], NULL, 16); |
| 632 | boot_device = argv[2]; |
| 633 | offset = simple_strtoul(argv[3], NULL, 16); |
| 634 | break; |
| 635 | default: |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 636 | #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 637 | usage: |
| 638 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 639 | printf("Usage:\n%s\n", cmdtp->usage); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 640 | show_boot_progress(-53); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 641 | return 1; |
| 642 | } |
| 643 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 644 | show_boot_progress(53); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 645 | if (!boot_device) { |
| 646 | puts("\n** No boot device **\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 647 | show_boot_progress(-54); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 648 | return 1; |
| 649 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 650 | show_boot_progress(54); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 651 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 652 | idx = simple_strtoul(boot_device, NULL, 16); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 653 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 654 | if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) { |
| 655 | printf("\n** Device %d not available\n", idx); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 656 | show_boot_progress(-55); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 657 | return 1; |
| 658 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 659 | show_boot_progress(55); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 660 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 661 | return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | U_BOOT_CMD(nboot, 4, 1, do_nandboot, |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 665 | "nboot - boot from NAND device\n", |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 666 | "[partition] | [[[loadAddr] dev] offset]\n"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 667 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 668 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 669 | |
Jean-Christophe PLAGNIOL-VILLARD | cc4a0ce | 2008-08-13 01:40:43 +0200 | [diff] [blame^] | 670 | #else /* CONFIG_NAND_LEGACY */ |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 671 | /* |
| 672 | * |
| 673 | * Legacy NAND support - to be phased out |
| 674 | * |
| 675 | */ |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 676 | #include <command.h> |
| 677 | #include <malloc.h> |
| 678 | #include <asm/io.h> |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 679 | #include <watchdog.h> |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 680 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 681 | #ifdef CONFIG_show_boot_progress |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 682 | # include <status_led.h> |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 683 | # define show_boot_progress(arg) show_boot_progress(arg) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 684 | #else |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 685 | # define show_boot_progress(arg) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 686 | #endif |
| 687 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 688 | #if defined(CONFIG_CMD_NAND) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 689 | #include <linux/mtd/nand_legacy.h> |
| 690 | #if 0 |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 691 | #include <linux/mtd/nand_ids.h> |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 692 | #include <jffs2/jffs2.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 693 | #endif |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 694 | |
wdenk | 1f4bb37 | 2003-07-27 00:21:01 +0000 | [diff] [blame] | 695 | #ifdef CONFIG_OMAP1510 |
| 696 | void archflashwp(void *archdata, int wp); |
| 697 | #endif |
| 698 | |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 699 | #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1))) |
wdenk | 1f4bb37 | 2003-07-27 00:21:01 +0000 | [diff] [blame] | 700 | |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 701 | #undef NAND_DEBUG |
| 702 | #undef PSYCHO_DEBUG |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 703 | |
| 704 | /* ****************** WARNING ********************* |
| 705 | * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will |
| 706 | * erase (or at least attempt to erase) blocks that are marked |
| 707 | * bad. This can be very handy if you are _sure_ that the block |
| 708 | * is OK, say because you marked a good block bad to test bad |
| 709 | * block handling and you are done testing, or if you have |
| 710 | * accidentally marked blocks bad. |
| 711 | * |
| 712 | * Erasing factory marked bad blocks is a _bad_ idea. If the |
| 713 | * erase succeeds there is no reliable way to find them again, |
| 714 | * and attempting to program or erase bad blocks can affect |
| 715 | * the data in _other_ (good) blocks. |
| 716 | */ |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 717 | #define ALLOW_ERASE_BAD_DEBUG 0 |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 718 | |
| 719 | #define CONFIG_MTD_NAND_ECC /* enable ECC */ |
wdenk | 1f4bb37 | 2003-07-27 00:21:01 +0000 | [diff] [blame] | 720 | #define CONFIG_MTD_NAND_ECC_JFFS2 |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 721 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 722 | /* bits for nand_legacy_rw() `cmd'; or together as needed */ |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 723 | #define NANDRW_READ 0x01 |
| 724 | #define NANDRW_WRITE 0x00 |
| 725 | #define NANDRW_JFFS2 0x02 |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 726 | #define NANDRW_JFFS2_SKIP 0x04 |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 727 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 728 | /* |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 729 | * Imports from nand_legacy.c |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 730 | */ |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 731 | extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE]; |
| 732 | extern int curr_device; |
| 733 | extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs, |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 734 | size_t len, int clean); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 735 | extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start, |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 736 | size_t len, size_t *retlen, u_char *buf); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 737 | extern void nand_print(struct nand_chip *nand); |
| 738 | extern void nand_print_bad(struct nand_chip *nand); |
| 739 | extern int nand_read_oob(struct nand_chip *nand, size_t ofs, |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 740 | size_t len, size_t *retlen, u_char *buf); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 741 | extern int nand_write_oob(struct nand_chip *nand, size_t ofs, |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 742 | size_t len, size_t *retlen, const u_char *buf); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 743 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 744 | |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 745 | int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 746 | { |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 747 | int rcode = 0; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 748 | |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 749 | switch (argc) { |
| 750 | case 0: |
| 751 | case 1: |
| 752 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 753 | return 1; |
| 754 | case 2: |
| 755 | if (strcmp (argv[1], "info") == 0) { |
| 756 | int i; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 757 | |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 758 | putc ('\n'); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 759 | |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 760 | for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) { |
| 761 | if (nand_dev_desc[i].ChipID == |
| 762 | NAND_ChipID_UNKNOWN) |
| 763 | continue; /* list only known devices */ |
| 764 | printf ("Device %d: ", i); |
| 765 | nand_print (&nand_dev_desc[i]); |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 766 | } |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 767 | return 0; |
| 768 | |
| 769 | } else if (strcmp (argv[1], "device") == 0) { |
| 770 | if ((curr_device < 0) |
| 771 | || (curr_device >= CFG_MAX_NAND_DEVICE)) { |
| 772 | puts ("\nno devices available\n"); |
| 773 | return 1; |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 774 | } |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 775 | printf ("\nDevice %d: ", curr_device); |
| 776 | nand_print (&nand_dev_desc[curr_device]); |
| 777 | return 0; |
| 778 | |
| 779 | } else if (strcmp (argv[1], "bad") == 0) { |
| 780 | if ((curr_device < 0) |
| 781 | || (curr_device >= CFG_MAX_NAND_DEVICE)) { |
| 782 | puts ("\nno devices available\n"); |
| 783 | return 1; |
| 784 | } |
| 785 | printf ("\nDevice %d bad blocks:\n", curr_device); |
| 786 | nand_print_bad (&nand_dev_desc[curr_device]); |
| 787 | return 0; |
| 788 | |
| 789 | } |
| 790 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 791 | return 1; |
| 792 | case 3: |
| 793 | if (strcmp (argv[1], "device") == 0) { |
| 794 | int dev = (int) simple_strtoul (argv[2], NULL, 10); |
| 795 | |
| 796 | printf ("\nDevice %d: ", dev); |
| 797 | if (dev >= CFG_MAX_NAND_DEVICE) { |
| 798 | puts ("unknown device\n"); |
| 799 | return 1; |
| 800 | } |
| 801 | nand_print (&nand_dev_desc[dev]); |
| 802 | /*nand_print (dev); */ |
| 803 | |
| 804 | if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) { |
| 805 | return 1; |
| 806 | } |
| 807 | |
| 808 | curr_device = dev; |
| 809 | |
| 810 | puts ("... is now current device\n"); |
| 811 | |
| 812 | return 0; |
| 813 | } else if (strcmp (argv[1], "erase") == 0 |
| 814 | && strcmp (argv[2], "clean") == 0) { |
| 815 | struct nand_chip *nand = &nand_dev_desc[curr_device]; |
| 816 | ulong off = 0; |
| 817 | ulong size = nand->totlen; |
| 818 | int ret; |
| 819 | |
| 820 | printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size); |
| 821 | |
| 822 | ret = nand_legacy_erase (nand, off, size, 1); |
| 823 | |
| 824 | printf ("%s\n", ret ? "ERROR" : "OK"); |
| 825 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 826 | return ret; |
| 827 | } |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 828 | |
| 829 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 830 | return 1; |
| 831 | default: |
| 832 | /* at least 4 args */ |
| 833 | |
| 834 | if (strncmp (argv[1], "read", 4) == 0 || |
| 835 | strncmp (argv[1], "write", 5) == 0) { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 836 | ulong addr = simple_strtoul (argv[2], NULL, 16); |
| 837 | off_t off = simple_strtoul (argv[3], NULL, 16); |
| 838 | size_t size = simple_strtoul (argv[4], NULL, 16); |
| 839 | int cmd = (strncmp (argv[1], "read", 4) == 0) ? |
| 840 | NANDRW_READ : NANDRW_WRITE; |
Wolfgang Denk | c71abba | 2008-04-28 14:55:12 +0200 | [diff] [blame] | 841 | size_t total; |
| 842 | int ret; |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 843 | char *cmdtail = strchr (argv[1], '.'); |
| 844 | |
| 845 | if (cmdtail && !strncmp (cmdtail, ".oob", 2)) { |
| 846 | /* read out-of-band data */ |
| 847 | if (cmd & NANDRW_READ) { |
| 848 | ret = nand_read_oob (nand_dev_desc + curr_device, |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 849 | off, size, &total, |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 850 | (u_char *) addr); |
| 851 | } else { |
| 852 | ret = nand_write_oob (nand_dev_desc + curr_device, |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 853 | off, size, &total, |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 854 | (u_char *) addr); |
| 855 | } |
| 856 | return ret; |
| 857 | } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2)) |
| 858 | cmd |= NANDRW_JFFS2; /* skip bad blocks */ |
| 859 | else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) { |
| 860 | cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */ |
| 861 | if (cmd & NANDRW_READ) |
| 862 | cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */ |
| 863 | } |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 864 | #ifdef SXNI855T |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 865 | /* need ".e" same as ".j" for compatibility with older units */ |
| 866 | else if (cmdtail && !strcmp (cmdtail, ".e")) |
| 867 | cmd |= NANDRW_JFFS2; /* skip bad blocks */ |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 868 | #endif |
stroese | a842a6d | 2004-12-16 17:45:46 +0000 | [diff] [blame] | 869 | #ifdef CFG_NAND_SKIP_BAD_DOT_I |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 870 | /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */ |
| 871 | /* ".i" for image -> read skips bad block (no 0xff) */ |
| 872 | else if (cmdtail && !strcmp (cmdtail, ".i")) { |
| 873 | cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */ |
| 874 | if (cmd & NANDRW_READ) |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 875 | cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */ |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 876 | } |
stroese | a842a6d | 2004-12-16 17:45:46 +0000 | [diff] [blame] | 877 | #endif /* CFG_NAND_SKIP_BAD_DOT_I */ |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 878 | else if (cmdtail) { |
| 879 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 880 | return 1; |
| 881 | } |
| 882 | |
Wolfgang Denk | 9b55a25 | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 883 | printf ("\nNAND %s: device %d offset %ld, size %lu ...\n", |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 884 | (cmd & NANDRW_READ) ? "read" : "write", |
Wolfgang Denk | 9b55a25 | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 885 | curr_device, off, (ulong)size); |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 886 | |
| 887 | ret = nand_legacy_rw (nand_dev_desc + curr_device, |
| 888 | cmd, off, size, |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 889 | &total, (u_char *) addr); |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 890 | |
| 891 | printf (" %d bytes %s: %s\n", total, |
| 892 | (cmd & NANDRW_READ) ? "read" : "written", |
| 893 | ret ? "ERROR" : "OK"); |
| 894 | |
| 895 | return ret; |
| 896 | } else if (strcmp (argv[1], "erase") == 0 && |
| 897 | (argc == 4 || strcmp ("clean", argv[2]) == 0)) { |
| 898 | int clean = argc == 5; |
| 899 | ulong off = |
| 900 | simple_strtoul (argv[2 + clean], NULL, 16); |
| 901 | ulong size = |
| 902 | simple_strtoul (argv[3 + clean], NULL, 16); |
| 903 | int ret; |
| 904 | |
| 905 | printf ("\nNAND erase: device %d offset %ld, size %ld ...\n", |
| 906 | curr_device, off, size); |
| 907 | |
| 908 | ret = nand_legacy_erase (nand_dev_desc + curr_device, |
| 909 | off, size, clean); |
| 910 | |
| 911 | printf ("%s\n", ret ? "ERROR" : "OK"); |
| 912 | |
| 913 | return ret; |
| 914 | } else { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 915 | printf ("Usage:\n%s\n", cmdtp->usage); |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 916 | rcode = 1; |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 917 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 918 | |
Wolfgang Denk | dd520bf | 2006-11-30 18:02:20 +0100 | [diff] [blame] | 919 | return rcode; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 920 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 921 | } |
| 922 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 923 | U_BOOT_CMD( |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 924 | nand, 5, 1, do_nand, |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 925 | "nand - legacy NAND sub-system\n", |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 926 | "info - show available NAND devices\n" |
| 927 | "nand device [dev] - show or set current device\n" |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 928 | "nand read[.jffs2[s]] addr off size\n" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 929 | "nand write[.jffs2] addr off size - read/write `size' bytes starting\n" |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 930 | " at offset `off' to/from memory address `addr'\n" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 931 | "nand erase [clean] [off size] - erase `size' bytes from\n" |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 932 | " offset `off' (entire device if not specified)\n" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 933 | "nand bad - show bad blocks\n" |
| 934 | "nand read.oob addr off size - read out-of-band data\n" |
| 935 | "nand write.oob addr off size - read out-of-band data\n" |
| 936 | ); |
| 937 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 938 | int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 939 | { |
| 940 | char *boot_device = NULL; |
| 941 | char *ep; |
| 942 | int dev; |
| 943 | ulong cnt; |
| 944 | ulong addr; |
| 945 | ulong offset = 0; |
| 946 | image_header_t *hdr; |
| 947 | int rcode = 0; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 948 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 949 | const void *fit_hdr = NULL; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 950 | #endif |
| 951 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 952 | show_boot_progress (52); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 953 | switch (argc) { |
| 954 | case 1: |
| 955 | addr = CFG_LOAD_ADDR; |
| 956 | boot_device = getenv ("bootdevice"); |
| 957 | break; |
| 958 | case 2: |
| 959 | addr = simple_strtoul(argv[1], NULL, 16); |
| 960 | boot_device = getenv ("bootdevice"); |
| 961 | break; |
| 962 | case 3: |
| 963 | addr = simple_strtoul(argv[1], NULL, 16); |
| 964 | boot_device = argv[2]; |
| 965 | break; |
| 966 | case 4: |
| 967 | addr = simple_strtoul(argv[1], NULL, 16); |
| 968 | boot_device = argv[2]; |
| 969 | offset = simple_strtoul(argv[3], NULL, 16); |
| 970 | break; |
| 971 | default: |
| 972 | printf ("Usage:\n%s\n", cmdtp->usage); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 973 | show_boot_progress (-53); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 974 | return 1; |
| 975 | } |
| 976 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 977 | show_boot_progress (53); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 978 | if (!boot_device) { |
| 979 | puts ("\n** No boot device **\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 980 | show_boot_progress (-54); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 981 | return 1; |
| 982 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 983 | show_boot_progress (54); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 984 | |
| 985 | dev = simple_strtoul(boot_device, &ep, 16); |
| 986 | |
| 987 | if ((dev >= CFG_MAX_NAND_DEVICE) || |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 988 | (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) { |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 989 | printf ("\n** Device %d not available\n", dev); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 990 | show_boot_progress (-55); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 991 | return 1; |
| 992 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 993 | show_boot_progress (55); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 994 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 995 | printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n", |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 996 | dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR, |
| 997 | offset); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 998 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 999 | if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1000 | SECTORSIZE, NULL, (u_char *)addr)) { |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1001 | printf ("** Read error on %d\n", dev); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 1002 | show_boot_progress (-56); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1003 | return 1; |
| 1004 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 1005 | show_boot_progress (56); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1006 | |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 1007 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1008 | case IMAGE_FORMAT_LEGACY: |
| 1009 | hdr = (image_header_t *)addr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1010 | image_print_contents (hdr); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1011 | |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1012 | cnt = image_get_image_size (hdr); |
| 1013 | cnt -= SECTORSIZE; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1014 | break; |
| 1015 | #if defined(CONFIG_FIT) |
| 1016 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1017 | fit_hdr = (const void *)addr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1018 | puts ("Fit image detected...\n"); |
| 1019 | |
| 1020 | cnt = fit_get_size (fit_hdr); |
| 1021 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1022 | #endif |
| 1023 | default: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1024 | show_boot_progress (-57); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1025 | puts ("** Unknown image type\n"); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1026 | return 1; |
| 1027 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 1028 | show_boot_progress (57); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1029 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1030 | if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1031 | offset + SECTORSIZE, cnt, NULL, |
| 1032 | (u_char *)(addr+SECTORSIZE))) { |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1033 | printf ("** Read error on %d\n", dev); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 1034 | show_boot_progress (-58); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1035 | return 1; |
| 1036 | } |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 1037 | show_boot_progress (58); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1038 | |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1039 | #if defined(CONFIG_FIT) |
| 1040 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 1041 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 1042 | if (!fit_check_format (fit_hdr)) { |
| 1043 | show_boot_progress (-150); |
| 1044 | puts ("** Bad FIT image format\n"); |
| 1045 | return 1; |
| 1046 | } |
| 1047 | show_boot_progress (151); |
| 1048 | fit_print_contents (fit_hdr); |
| 1049 | } |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1050 | #endif |
| 1051 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1052 | /* Loading ok, update default load address */ |
| 1053 | |
| 1054 | load_addr = addr; |
| 1055 | |
| 1056 | /* Check if we should attempt an auto-start */ |
| 1057 | if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) { |
| 1058 | char *local_args[2]; |
| 1059 | extern int do_bootm (cmd_tbl_t *, int, int, char *[]); |
| 1060 | |
| 1061 | local_args[0] = argv[0]; |
| 1062 | local_args[1] = NULL; |
| 1063 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 1064 | printf ("Automatic boot of image at addr 0x%08lx ...\n", addr); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1065 | |
| 1066 | do_bootm (cmdtp, 0, 1, local_args); |
| 1067 | rcode = 1; |
| 1068 | } |
| 1069 | return rcode; |
| 1070 | } |
| 1071 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1072 | U_BOOT_CMD( |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 1073 | nboot, 4, 1, do_nandboot, |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 1074 | "nboot - boot from NAND device\n", |
| 1075 | "loadAddr dev\n" |
| 1076 | ); |
| 1077 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1078 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1079 | |
Jean-Christophe PLAGNIOL-VILLARD | cc4a0ce | 2008-08-13 01:40:43 +0200 | [diff] [blame^] | 1080 | #endif /* CONFIG_NAND_LEGACY */ |