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