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