Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * U-Boot command for OneNAND support |
| 3 | * |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 Samsung Electronics |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <command.h> |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 14 | #include <malloc.h> |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 15 | |
Mike Frysinger | 7b15e2b | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 16 | #include <linux/compat.h> |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 17 | #include <linux/mtd/mtd.h> |
| 18 | #include <linux/mtd/onenand.h> |
| 19 | |
| 20 | #include <asm/io.h> |
| 21 | |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 22 | static struct mtd_info *mtd; |
| 23 | |
| 24 | static loff_t next_ofs; |
| 25 | static loff_t skip_ofs; |
| 26 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 27 | static int arg_off_size_onenand(int argc, char *const argv[], ulong *off, |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 28 | size_t *size) |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 29 | { |
| 30 | if (argc >= 1) { |
| 31 | if (!(str2long(argv[0], off))) { |
| 32 | printf("'%s' is not a number\n", argv[0]); |
| 33 | return -1; |
| 34 | } |
| 35 | } else { |
| 36 | *off = 0; |
| 37 | } |
| 38 | |
| 39 | if (argc >= 2) { |
| 40 | if (!(str2long(argv[1], (ulong *)size))) { |
| 41 | printf("'%s' is not a number\n", argv[1]); |
| 42 | return -1; |
| 43 | } |
| 44 | } else { |
| 45 | *size = mtd->size - *off; |
| 46 | } |
| 47 | |
| 48 | if ((*off + *size) > mtd->size) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 49 | printf("total chip size (0x%llx) exceeded!\n", mtd->size); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | if (*size == mtd->size) |
| 54 | puts("whole chip\n"); |
| 55 | else |
| 56 | printf("offset 0x%lx, size 0x%x\n", *off, *size); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int onenand_block_read(loff_t from, size_t len, |
| 62 | size_t *retlen, u_char *buf, int oob) |
| 63 | { |
| 64 | struct onenand_chip *this = mtd->priv; |
| 65 | int blocks = (int) len >> this->erase_shift; |
| 66 | int blocksize = (1 << this->erase_shift); |
| 67 | loff_t ofs = from; |
| 68 | struct mtd_oob_ops ops = { |
| 69 | .retlen = 0, |
| 70 | }; |
| 71 | int ret; |
| 72 | |
| 73 | if (oob) |
| 74 | ops.ooblen = blocksize; |
| 75 | else |
| 76 | ops.len = blocksize; |
| 77 | |
| 78 | while (blocks) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 79 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 80 | if (ret) { |
| 81 | printk("Bad blocks %d at 0x%x\n", |
| 82 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 83 | ofs += blocksize; |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (oob) |
| 88 | ops.oobbuf = buf; |
| 89 | else |
| 90 | ops.datbuf = buf; |
| 91 | |
| 92 | ops.retlen = 0; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 93 | ret = mtd_read_oob(mtd, ofs, &ops); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 94 | if (ret) { |
| 95 | printk("Read failed 0x%x, %d\n", (u32)ofs, ret); |
| 96 | ofs += blocksize; |
| 97 | continue; |
| 98 | } |
| 99 | ofs += blocksize; |
| 100 | buf += blocksize; |
| 101 | blocks--; |
| 102 | *retlen += ops.retlen; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 108 | static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf, |
| 109 | size_t *retlen) |
| 110 | { |
| 111 | struct mtd_oob_ops ops = { |
| 112 | .len = mtd->writesize, |
| 113 | .ooblen = mtd->oobsize, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 114 | .mode = MTD_OPS_AUTO_OOB, |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 115 | }; |
| 116 | int page, ret = 0; |
| 117 | for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) { |
| 118 | ops.datbuf = (u_char *)buf; |
| 119 | buf += mtd->writesize; |
| 120 | ops.oobbuf = (u_char *)buf; |
| 121 | buf += mtd->oobsize; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 122 | ret = mtd_write_oob(mtd, to, &ops); |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 123 | if (ret) |
| 124 | break; |
| 125 | to += mtd->writesize; |
| 126 | } |
| 127 | |
| 128 | *retlen = (ret) ? 0 : mtd->erasesize; |
| 129 | return ret; |
| 130 | } |
| 131 | |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 132 | static int onenand_block_write(loff_t to, size_t len, |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 133 | size_t *retlen, const u_char * buf, int withoob) |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 134 | { |
| 135 | struct onenand_chip *this = mtd->priv; |
| 136 | int blocks = len >> this->erase_shift; |
| 137 | int blocksize = (1 << this->erase_shift); |
| 138 | loff_t ofs; |
| 139 | size_t _retlen = 0; |
| 140 | int ret; |
| 141 | |
Ladislav Michl | 9c00d98 | 2016-09-13 07:40:00 +0200 | [diff] [blame] | 142 | if ((to & (mtd->writesize - 1)) != 0) { |
| 143 | printf("Attempt to write non block-aligned data\n"); |
| 144 | *retlen = 0; |
| 145 | return 1; |
| 146 | } |
| 147 | |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 148 | if (to == next_ofs) { |
| 149 | next_ofs = to + len; |
| 150 | to += skip_ofs; |
| 151 | } else { |
| 152 | next_ofs = to + len; |
| 153 | skip_ofs = 0; |
| 154 | } |
| 155 | ofs = to; |
| 156 | |
| 157 | while (blocks) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 158 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 159 | if (ret) { |
| 160 | printk("Bad blocks %d at 0x%x\n", |
| 161 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 162 | skip_ofs += blocksize; |
| 163 | goto next; |
| 164 | } |
| 165 | |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 166 | if (!withoob) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 167 | ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf); |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 168 | else |
| 169 | ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 170 | if (ret) { |
| 171 | printk("Write failed 0x%x, %d", (u32)ofs, ret); |
| 172 | skip_ofs += blocksize; |
| 173 | goto next; |
| 174 | } |
| 175 | |
| 176 | buf += blocksize; |
| 177 | blocks--; |
| 178 | *retlen += _retlen; |
| 179 | next: |
| 180 | ofs += blocksize; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int onenand_block_erase(u32 start, u32 size, int force) |
| 187 | { |
| 188 | struct onenand_chip *this = mtd->priv; |
| 189 | struct erase_info instr = { |
| 190 | .callback = NULL, |
| 191 | }; |
| 192 | loff_t ofs; |
| 193 | int ret; |
| 194 | int blocksize = 1 << this->erase_shift; |
| 195 | |
| 196 | for (ofs = start; ofs < (start + size); ofs += blocksize) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 197 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 198 | if (ret && !force) { |
| 199 | printf("Skip erase bad block %d at 0x%x\n", |
| 200 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | instr.addr = ofs; |
| 205 | instr.len = blocksize; |
| 206 | instr.priv = force; |
| 207 | instr.mtd = mtd; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 208 | ret = mtd_erase(mtd, &instr); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 209 | if (ret) { |
| 210 | printf("erase failed block %d at 0x%x\n", |
| 211 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 212 | continue; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int onenand_block_test(u32 start, u32 size) |
| 220 | { |
| 221 | struct onenand_chip *this = mtd->priv; |
| 222 | struct erase_info instr = { |
| 223 | .callback = NULL, |
| 224 | .priv = 0, |
| 225 | }; |
| 226 | |
| 227 | int blocks; |
| 228 | loff_t ofs; |
| 229 | int blocksize = 1 << this->erase_shift; |
| 230 | int start_block, end_block; |
| 231 | size_t retlen; |
| 232 | u_char *buf; |
| 233 | u_char *verify_buf; |
| 234 | int ret; |
| 235 | |
| 236 | buf = malloc(blocksize); |
| 237 | if (!buf) { |
| 238 | printf("Not enough malloc space available!\n"); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | verify_buf = malloc(blocksize); |
| 243 | if (!verify_buf) { |
| 244 | printf("Not enough malloc space available!\n"); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | start_block = start >> this->erase_shift; |
| 249 | end_block = (start + size) >> this->erase_shift; |
| 250 | |
| 251 | /* Protect boot-loader from badblock testing */ |
| 252 | if (start_block < 2) |
| 253 | start_block = 2; |
| 254 | |
| 255 | if (end_block > (mtd->size >> this->erase_shift)) |
| 256 | end_block = mtd->size >> this->erase_shift; |
| 257 | |
| 258 | blocks = start_block; |
| 259 | ofs = start; |
| 260 | while (blocks < end_block) { |
| 261 | printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 262 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 263 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 264 | if (ret) { |
| 265 | printf("Skip erase bad block %d at 0x%x\n", |
| 266 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 267 | goto next; |
| 268 | } |
| 269 | |
| 270 | instr.addr = ofs; |
| 271 | instr.len = blocksize; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 272 | ret = mtd_erase(mtd, &instr); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 273 | if (ret) { |
| 274 | printk("Erase failed 0x%x, %d\n", (u32)ofs, ret); |
| 275 | goto next; |
| 276 | } |
| 277 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 278 | ret = mtd_write(mtd, ofs, blocksize, &retlen, buf); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 279 | if (ret) { |
| 280 | printk("Write failed 0x%x, %d\n", (u32)ofs, ret); |
| 281 | goto next; |
| 282 | } |
| 283 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 284 | ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 285 | if (ret) { |
| 286 | printk("Read failed 0x%x, %d\n", (u32)ofs, ret); |
| 287 | goto next; |
| 288 | } |
| 289 | |
| 290 | if (memcmp(buf, verify_buf, blocksize)) |
| 291 | printk("\nRead/Write test failed at 0x%x\n", (u32)ofs); |
| 292 | |
| 293 | next: |
| 294 | ofs += blocksize; |
| 295 | blocks++; |
| 296 | } |
| 297 | printf("...Done\n"); |
| 298 | |
| 299 | free(buf); |
| 300 | free(verify_buf); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob) |
| 306 | { |
| 307 | int i; |
| 308 | u_char *datbuf, *oobbuf, *p; |
| 309 | struct mtd_oob_ops ops; |
| 310 | loff_t addr; |
| 311 | |
| 312 | datbuf = malloc(mtd->writesize + mtd->oobsize); |
| 313 | oobbuf = malloc(mtd->oobsize); |
| 314 | if (!datbuf || !oobbuf) { |
| 315 | puts("No memory for page buffer\n"); |
| 316 | return 1; |
| 317 | } |
| 318 | off &= ~(mtd->writesize - 1); |
| 319 | addr = (loff_t) off; |
| 320 | memset(&ops, 0, sizeof(ops)); |
| 321 | ops.datbuf = datbuf; |
Lei Wen | a430b13 | 2010-12-07 09:38:18 +0800 | [diff] [blame] | 322 | ops.oobbuf = oobbuf; |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 323 | ops.len = mtd->writesize; |
| 324 | ops.ooblen = mtd->oobsize; |
| 325 | ops.retlen = 0; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 326 | i = mtd_read_oob(mtd, addr, &ops); |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 327 | if (i < 0) { |
| 328 | printf("Error (%d) reading page %08lx\n", i, off); |
| 329 | free(datbuf); |
| 330 | free(oobbuf); |
| 331 | return 1; |
| 332 | } |
| 333 | printf("Page %08lx dump:\n", off); |
| 334 | i = mtd->writesize >> 4; |
| 335 | p = datbuf; |
| 336 | |
| 337 | while (i--) { |
| 338 | if (!only_oob) |
| 339 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x" |
| 340 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 341 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], |
| 342 | p[8], p[9], p[10], p[11], p[12], p[13], p[14], |
| 343 | p[15]); |
| 344 | p += 16; |
| 345 | } |
| 346 | puts("OOB:\n"); |
| 347 | i = mtd->oobsize >> 3; |
Lei Wen | a430b13 | 2010-12-07 09:38:18 +0800 | [diff] [blame] | 348 | p = oobbuf; |
| 349 | |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 350 | while (i--) { |
| 351 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 352 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); |
| 353 | p += 8; |
| 354 | } |
| 355 | free(datbuf); |
| 356 | free(oobbuf); |
| 357 | |
| 358 | return 0; |
| 359 | } |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 360 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 361 | static int do_onenand_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 362 | char *const argv[]) |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 363 | { |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 364 | printf("%s\n", mtd->name); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 368 | static int do_onenand_bad(struct cmd_tbl *cmdtp, int flag, int argc, |
| 369 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 370 | { |
| 371 | ulong ofs; |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 372 | |
| 373 | mtd = &onenand_mtd; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 374 | /* Currently only one OneNAND device is supported */ |
| 375 | printf("\nDevice %d bad blocks:\n", 0); |
| 376 | for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 377 | if (mtd_block_isbad(mtd, ofs)) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 378 | printf(" %08x\n", (u32)ofs); |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | return 0; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 382 | } |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 383 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 384 | static int do_onenand_read(struct cmd_tbl *cmdtp, int flag, int argc, |
| 385 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 386 | { |
| 387 | char *s; |
| 388 | int oob = 0; |
| 389 | ulong addr, ofs; |
| 390 | size_t len; |
| 391 | int ret = 0; |
| 392 | size_t retlen = 0; |
| 393 | |
| 394 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 395 | return CMD_RET_USAGE; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 396 | |
| 397 | s = strchr(argv[0], '.'); |
| 398 | if ((s != NULL) && (!strcmp(s, ".oob"))) |
| 399 | oob = 1; |
| 400 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 401 | addr = (ulong)hextoul(argv[1], NULL); |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 402 | |
| 403 | printf("\nOneNAND read: "); |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 404 | if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 405 | return 1; |
| 406 | |
| 407 | ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob); |
| 408 | |
| 409 | printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK"); |
| 410 | |
| 411 | return ret == 0 ? 0 : 1; |
| 412 | } |
| 413 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 414 | static int do_onenand_write(struct cmd_tbl *cmdtp, int flag, int argc, |
| 415 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 416 | { |
| 417 | ulong addr, ofs; |
| 418 | size_t len; |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 419 | int ret = 0, withoob = 0; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 420 | size_t retlen = 0; |
| 421 | |
| 422 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 423 | return CMD_RET_USAGE; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 424 | |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 425 | if (strncmp(argv[0] + 6, "yaffs", 5) == 0) |
| 426 | withoob = 1; |
| 427 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 428 | addr = (ulong)hextoul(argv[1], NULL); |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 429 | |
| 430 | printf("\nOneNAND write: "); |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 431 | if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 432 | return 1; |
| 433 | |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 434 | ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob); |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 435 | |
| 436 | printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK"); |
| 437 | |
| 438 | return ret == 0 ? 0 : 1; |
| 439 | } |
| 440 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 441 | static int do_onenand_erase(struct cmd_tbl *cmdtp, int flag, int argc, |
| 442 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 443 | { |
| 444 | ulong ofs; |
| 445 | int ret = 0; |
| 446 | size_t len; |
| 447 | int force; |
| 448 | |
| 449 | /* |
| 450 | * Syntax is: |
| 451 | * 0 1 2 3 4 |
| 452 | * onenand erase [force] [off size] |
| 453 | */ |
| 454 | argc--; |
| 455 | argv++; |
| 456 | if (argc) |
| 457 | { |
| 458 | if (!strcmp("force", argv[0])) |
| 459 | { |
| 460 | force = 1; |
| 461 | argc--; |
| 462 | argv++; |
| 463 | } |
| 464 | } |
| 465 | printf("\nOneNAND erase: "); |
| 466 | |
| 467 | /* skip first two or three arguments, look for offset and size */ |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 468 | if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 469 | return 1; |
| 470 | |
| 471 | ret = onenand_block_erase(ofs, len, force); |
| 472 | |
| 473 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 474 | |
| 475 | return ret == 0 ? 0 : 1; |
| 476 | } |
| 477 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 478 | static int do_onenand_test(struct cmd_tbl *cmdtp, int flag, int argc, |
| 479 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 480 | { |
| 481 | ulong ofs; |
| 482 | int ret = 0; |
| 483 | size_t len; |
| 484 | |
| 485 | /* |
| 486 | * Syntax is: |
| 487 | * 0 1 2 3 4 |
| 488 | * onenand test [force] [off size] |
| 489 | */ |
| 490 | |
| 491 | printf("\nOneNAND test: "); |
| 492 | |
| 493 | /* skip first two or three arguments, look for offset and size */ |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 494 | if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 495 | return 1; |
| 496 | |
| 497 | ret = onenand_block_test(ofs, len); |
| 498 | |
| 499 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 500 | |
| 501 | return ret == 0 ? 0 : 1; |
| 502 | } |
| 503 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 504 | static int do_onenand_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 505 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 506 | { |
| 507 | ulong ofs; |
| 508 | int ret = 0; |
| 509 | char *s; |
| 510 | |
| 511 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 512 | return CMD_RET_USAGE; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 513 | |
| 514 | s = strchr(argv[0], '.'); |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 515 | ofs = (int)hextoul(argv[1], NULL); |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 516 | |
| 517 | if (s != NULL && strcmp(s, ".oob") == 0) |
| 518 | ret = onenand_dump(mtd, ofs, 1); |
| 519 | else |
| 520 | ret = onenand_dump(mtd, ofs, 0); |
| 521 | |
| 522 | return ret == 0 ? 1 : 0; |
| 523 | } |
| 524 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 525 | static int do_onenand_markbad(struct cmd_tbl *cmdtp, int flag, int argc, |
| 526 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 527 | { |
| 528 | int ret = 0; |
| 529 | ulong addr; |
| 530 | |
| 531 | argc -= 2; |
| 532 | argv += 2; |
| 533 | |
| 534 | if (argc <= 0) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 535 | return CMD_RET_USAGE; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 536 | |
| 537 | while (argc > 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 538 | addr = hextoul(*argv, NULL); |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 539 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 540 | if (mtd_block_markbad(mtd, addr)) { |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 541 | printf("block 0x%08lx NOT marked " |
| 542 | "as bad! ERROR %d\n", |
| 543 | addr, ret); |
| 544 | ret = 1; |
| 545 | } else { |
| 546 | printf("block 0x%08lx successfully " |
| 547 | "marked as bad\n", |
| 548 | addr); |
| 549 | } |
| 550 | --argc; |
| 551 | ++argv; |
| 552 | } |
| 553 | return ret; |
| 554 | } |
| 555 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 556 | static struct cmd_tbl cmd_onenand_sub[] = { |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 557 | U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""), |
| 558 | U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""), |
| 559 | U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""), |
| 560 | U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""), |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 561 | U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""), |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 562 | U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""), |
| 563 | U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""), |
| 564 | U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""), |
| 565 | U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""), |
| 566 | }; |
| 567 | |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 568 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Enric Balletbo i Serra | cdb1d4f | 2010-10-19 11:13:22 +0200 | [diff] [blame] | 569 | void onenand_reloc(void) { |
| 570 | fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub)); |
| 571 | } |
| 572 | #endif |
| 573 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 574 | static int do_onenand(struct cmd_tbl *cmdtp, int flag, int argc, |
| 575 | char *const argv[]) |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 576 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 577 | struct cmd_tbl *c; |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 578 | |
Enric Balletbo i Serra | 57ff9f2 | 2010-10-19 11:13:21 +0200 | [diff] [blame] | 579 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 580 | return CMD_RET_USAGE; |
Enric Balletbo i Serra | 57ff9f2 | 2010-10-19 11:13:21 +0200 | [diff] [blame] | 581 | |
Frans Meulenbroeks | 8cd8528 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 582 | mtd = &onenand_mtd; |
| 583 | |
| 584 | /* Strip off leading 'onenand' command argument */ |
| 585 | argc--; |
| 586 | argv++; |
| 587 | |
| 588 | c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub)); |
| 589 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 590 | if (c) |
| 591 | return c->cmd(cmdtp, flag, argc, argv); |
| 592 | else |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 593 | return CMD_RET_USAGE; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | U_BOOT_CMD( |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 597 | onenand, CONFIG_SYS_MAXARGS, 1, do_onenand, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 598 | "OneNAND sub-system", |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 599 | "info - show available OneNAND devices\n" |
| 600 | "onenand bad - show bad blocks\n" |
| 601 | "onenand read[.oob] addr off size\n" |
Lei Wen | 41c8624 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 602 | "onenand write[.yaffs] addr off size\n" |
Stefan Roese | c438ea1 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 603 | " read/write 'size' bytes starting at offset 'off'\n" |
| 604 | " to/from memory address 'addr', skipping bad blocks.\n" |
| 605 | "onenand erase [force] [off size] - erase 'size' bytes from\n" |
| 606 | "onenand test [off size] - test 'size' bytes from\n" |
| 607 | " offset 'off' (entire device if not specified)\n" |
| 608 | "onenand dump[.oob] off - dump page\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 609 | "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)" |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 610 | ); |