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