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