Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * U-Boot command for OneNAND support |
| 3 | * |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 Samsung Electronics |
Kyungmin Park | af3d11c | 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 | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 14 | #include <malloc.h> |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 15 | |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 16 | #include <linux/compat.h> |
Kyungmin Park | af3d11c | 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 | 9743d28 | 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 | ed38aef | 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 | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 28 | size_t *size) |
Stefan Roese | 9743d28 | 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 | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 49 | printf("total chip size (0x%llx) exceeded!\n", mtd->size); |
Stefan Roese | 9743d28 | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 79 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | 9743d28 | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 93 | ret = mtd_read_oob(mtd, ofs, &ops); |
Stefan Roese | 9743d28 | 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 | 2ca9e14 | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 114 | .mode = MTD_OPS_AUTO_OOB, |
Lei Wen | 2ca9e14 | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 122 | ret = mtd_write_oob(mtd, to, &ops); |
Lei Wen | 2ca9e14 | 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 | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 132 | static int onenand_block_write(loff_t to, size_t len, |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 133 | size_t *retlen, const u_char * buf, int withoob) |
Stefan Roese | 9743d28 | 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 | 6cf2ddb | 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 | 9743d28 | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 158 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | 9743d28 | 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 | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 166 | if (!withoob) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 167 | ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf); |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 168 | else |
| 169 | ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen); |
Stefan Roese | 9743d28 | 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; |
Marek Behún | 4b4896d | 2021-10-05 15:56:06 +0200 | [diff] [blame] | 189 | struct erase_info instr = {}; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 190 | loff_t ofs; |
| 191 | int ret; |
| 192 | int blocksize = 1 << this->erase_shift; |
| 193 | |
| 194 | for (ofs = start; ofs < (start + size); ofs += blocksize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 195 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 196 | if (ret && !force) { |
| 197 | printf("Skip erase bad block %d at 0x%x\n", |
| 198 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | instr.addr = ofs; |
| 203 | instr.len = blocksize; |
| 204 | instr.priv = force; |
| 205 | instr.mtd = mtd; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 206 | ret = mtd_erase(mtd, &instr); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 207 | if (ret) { |
| 208 | printf("erase failed block %d at 0x%x\n", |
| 209 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 210 | continue; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int onenand_block_test(u32 start, u32 size) |
| 218 | { |
| 219 | struct onenand_chip *this = mtd->priv; |
Marek Behún | 4b4896d | 2021-10-05 15:56:06 +0200 | [diff] [blame] | 220 | struct erase_info instr = {}; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 221 | |
| 222 | int blocks; |
| 223 | loff_t ofs; |
| 224 | int blocksize = 1 << this->erase_shift; |
| 225 | int start_block, end_block; |
| 226 | size_t retlen; |
| 227 | u_char *buf; |
| 228 | u_char *verify_buf; |
| 229 | int ret; |
| 230 | |
| 231 | buf = malloc(blocksize); |
| 232 | if (!buf) { |
| 233 | printf("Not enough malloc space available!\n"); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | verify_buf = malloc(blocksize); |
| 238 | if (!verify_buf) { |
| 239 | printf("Not enough malloc space available!\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | start_block = start >> this->erase_shift; |
| 244 | end_block = (start + size) >> this->erase_shift; |
| 245 | |
| 246 | /* Protect boot-loader from badblock testing */ |
| 247 | if (start_block < 2) |
| 248 | start_block = 2; |
| 249 | |
| 250 | if (end_block > (mtd->size >> this->erase_shift)) |
| 251 | end_block = mtd->size >> this->erase_shift; |
| 252 | |
| 253 | blocks = start_block; |
| 254 | ofs = start; |
| 255 | while (blocks < end_block) { |
| 256 | printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 257 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 258 | ret = mtd_block_isbad(mtd, ofs); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 259 | if (ret) { |
| 260 | printf("Skip erase bad block %d at 0x%x\n", |
| 261 | (u32)(ofs >> this->erase_shift), (u32)ofs); |
| 262 | goto next; |
| 263 | } |
| 264 | |
| 265 | instr.addr = ofs; |
| 266 | instr.len = blocksize; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 267 | ret = mtd_erase(mtd, &instr); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 268 | if (ret) { |
| 269 | printk("Erase failed 0x%x, %d\n", (u32)ofs, ret); |
| 270 | goto next; |
| 271 | } |
| 272 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 273 | ret = mtd_write(mtd, ofs, blocksize, &retlen, buf); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 274 | if (ret) { |
| 275 | printk("Write failed 0x%x, %d\n", (u32)ofs, ret); |
| 276 | goto next; |
| 277 | } |
| 278 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 279 | ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 280 | if (ret) { |
| 281 | printk("Read failed 0x%x, %d\n", (u32)ofs, ret); |
| 282 | goto next; |
| 283 | } |
| 284 | |
| 285 | if (memcmp(buf, verify_buf, blocksize)) |
| 286 | printk("\nRead/Write test failed at 0x%x\n", (u32)ofs); |
| 287 | |
| 288 | next: |
| 289 | ofs += blocksize; |
| 290 | blocks++; |
| 291 | } |
| 292 | printf("...Done\n"); |
| 293 | |
| 294 | free(buf); |
| 295 | free(verify_buf); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob) |
| 301 | { |
| 302 | int i; |
| 303 | u_char *datbuf, *oobbuf, *p; |
| 304 | struct mtd_oob_ops ops; |
| 305 | loff_t addr; |
| 306 | |
| 307 | datbuf = malloc(mtd->writesize + mtd->oobsize); |
| 308 | oobbuf = malloc(mtd->oobsize); |
| 309 | if (!datbuf || !oobbuf) { |
| 310 | puts("No memory for page buffer\n"); |
| 311 | return 1; |
| 312 | } |
| 313 | off &= ~(mtd->writesize - 1); |
| 314 | addr = (loff_t) off; |
| 315 | memset(&ops, 0, sizeof(ops)); |
| 316 | ops.datbuf = datbuf; |
Lei Wen | e816b8e | 2010-12-07 09:38:18 +0800 | [diff] [blame] | 317 | ops.oobbuf = oobbuf; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 318 | ops.len = mtd->writesize; |
| 319 | ops.ooblen = mtd->oobsize; |
| 320 | ops.retlen = 0; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 321 | i = mtd_read_oob(mtd, addr, &ops); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 322 | if (i < 0) { |
| 323 | printf("Error (%d) reading page %08lx\n", i, off); |
| 324 | free(datbuf); |
| 325 | free(oobbuf); |
| 326 | return 1; |
| 327 | } |
| 328 | printf("Page %08lx dump:\n", off); |
| 329 | i = mtd->writesize >> 4; |
| 330 | p = datbuf; |
| 331 | |
| 332 | while (i--) { |
| 333 | if (!only_oob) |
| 334 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x" |
| 335 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 336 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], |
| 337 | p[8], p[9], p[10], p[11], p[12], p[13], p[14], |
| 338 | p[15]); |
| 339 | p += 16; |
| 340 | } |
| 341 | puts("OOB:\n"); |
| 342 | i = mtd->oobsize >> 3; |
Lei Wen | e816b8e | 2010-12-07 09:38:18 +0800 | [diff] [blame] | 343 | p = oobbuf; |
| 344 | |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 345 | while (i--) { |
| 346 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 347 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); |
| 348 | p += 8; |
| 349 | } |
| 350 | free(datbuf); |
| 351 | free(oobbuf); |
| 352 | |
| 353 | return 0; |
| 354 | } |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 355 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 356 | static int do_onenand_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 357 | char *const argv[]) |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 358 | { |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 359 | printf("%s\n", mtd->name); |
| 360 | return 0; |
| 361 | } |
| 362 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 363 | static int do_onenand_bad(struct cmd_tbl *cmdtp, int flag, int argc, |
| 364 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 365 | { |
| 366 | ulong ofs; |
| 367 | |
| 368 | mtd = &onenand_mtd; |
| 369 | /* Currently only one OneNAND device is supported */ |
| 370 | printf("\nDevice %d bad blocks:\n", 0); |
| 371 | for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 372 | if (mtd_block_isbad(mtd, ofs)) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 373 | printf(" %08x\n", (u32)ofs); |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 379 | static int do_onenand_read(struct cmd_tbl *cmdtp, int flag, int argc, |
| 380 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 381 | { |
| 382 | char *s; |
| 383 | int oob = 0; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 384 | ulong addr, ofs; |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 385 | size_t len; |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 386 | int ret = 0; |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 387 | size_t retlen = 0; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 388 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 389 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 390 | return CMD_RET_USAGE; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 391 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 392 | s = strchr(argv[0], '.'); |
| 393 | if ((s != NULL) && (!strcmp(s, ".oob"))) |
| 394 | oob = 1; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 395 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 396 | addr = (ulong)hextoul(argv[1], NULL); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 397 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 398 | printf("\nOneNAND read: "); |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 399 | if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 400 | return 1; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 401 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 402 | ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob); |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 403 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 404 | printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK"); |
| 405 | |
| 406 | return ret == 0 ? 0 : 1; |
| 407 | } |
| 408 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 409 | static int do_onenand_write(struct cmd_tbl *cmdtp, int flag, int argc, |
| 410 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 411 | { |
| 412 | ulong addr, ofs; |
| 413 | size_t len; |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 414 | int ret = 0, withoob = 0; |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 415 | size_t retlen = 0; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 416 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 417 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 418 | return CMD_RET_USAGE; |
Kyungmin Park | 012f2aa | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 419 | |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 420 | if (strncmp(argv[0] + 6, "yaffs", 5) == 0) |
| 421 | withoob = 1; |
| 422 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 423 | addr = (ulong)hextoul(argv[1], NULL); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 424 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 425 | printf("\nOneNAND write: "); |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 426 | if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 427 | return 1; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 428 | |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 429 | ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 430 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 431 | printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK"); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 432 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 433 | return ret == 0 ? 0 : 1; |
| 434 | } |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 435 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 436 | static int do_onenand_erase(struct cmd_tbl *cmdtp, int flag, int argc, |
| 437 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 438 | { |
| 439 | ulong ofs; |
| 440 | int ret = 0; |
| 441 | size_t len; |
| 442 | int force; |
| 443 | |
| 444 | /* |
| 445 | * Syntax is: |
| 446 | * 0 1 2 3 4 |
| 447 | * onenand erase [force] [off size] |
| 448 | */ |
| 449 | argc--; |
| 450 | argv++; |
| 451 | if (argc) |
| 452 | { |
| 453 | if (!strcmp("force", argv[0])) |
| 454 | { |
| 455 | force = 1; |
| 456 | argc--; |
| 457 | argv++; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 458 | } |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 459 | } |
| 460 | printf("\nOneNAND erase: "); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 461 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 462 | /* skip first two or three arguments, look for offset and size */ |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 463 | if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 464 | return 1; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 465 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 466 | ret = onenand_block_erase(ofs, len, force); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 467 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 468 | printf("%s\n", ret ? "ERROR" : "OK"); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 469 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 470 | return ret == 0 ? 0 : 1; |
| 471 | } |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 472 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 473 | static int do_onenand_test(struct cmd_tbl *cmdtp, int flag, int argc, |
| 474 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 475 | { |
| 476 | ulong ofs; |
| 477 | int ret = 0; |
| 478 | size_t len; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 479 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 480 | /* |
| 481 | * Syntax is: |
| 482 | * 0 1 2 3 4 |
| 483 | * onenand test [force] [off size] |
| 484 | */ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 485 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 486 | printf("\nOneNAND test: "); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 487 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 488 | /* skip first two or three arguments, look for offset and size */ |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 489 | if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 490 | return 1; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 491 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 492 | ret = onenand_block_test(ofs, len); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 493 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 494 | printf("%s\n", ret ? "ERROR" : "OK"); |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 495 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 496 | return ret == 0 ? 0 : 1; |
| 497 | } |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 498 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 499 | static int do_onenand_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 500 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 501 | { |
| 502 | ulong ofs; |
| 503 | int ret = 0; |
| 504 | char *s; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 505 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 506 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 507 | return CMD_RET_USAGE; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 508 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 509 | s = strchr(argv[0], '.'); |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 510 | ofs = (int)hextoul(argv[1], NULL); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 511 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 512 | if (s != NULL && strcmp(s, ".oob") == 0) |
| 513 | ret = onenand_dump(mtd, ofs, 1); |
| 514 | else |
| 515 | ret = onenand_dump(mtd, ofs, 0); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 516 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 517 | return ret == 0 ? 1 : 0; |
| 518 | } |
| 519 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 520 | static int do_onenand_markbad(struct cmd_tbl *cmdtp, int flag, int argc, |
| 521 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 522 | { |
| 523 | int ret = 0; |
| 524 | ulong addr; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 525 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 526 | argc -= 2; |
| 527 | argv += 2; |
| 528 | |
| 529 | if (argc <= 0) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 530 | return CMD_RET_USAGE; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 531 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 532 | while (argc > 0) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 533 | addr = hextoul(*argv, NULL); |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 534 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 535 | if (mtd_block_markbad(mtd, addr)) { |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 536 | printf("block 0x%08lx NOT marked " |
| 537 | "as bad! ERROR %d\n", |
| 538 | addr, ret); |
| 539 | ret = 1; |
| 540 | } else { |
| 541 | printf("block 0x%08lx successfully " |
| 542 | "marked as bad\n", |
| 543 | addr); |
| 544 | } |
| 545 | --argc; |
| 546 | ++argv; |
| 547 | } |
| 548 | return ret; |
| 549 | } |
| 550 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 551 | static struct cmd_tbl cmd_onenand_sub[] = { |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 552 | U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""), |
| 553 | U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""), |
| 554 | U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""), |
| 555 | U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""), |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 556 | U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""), |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 557 | U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""), |
| 558 | U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""), |
| 559 | U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""), |
| 560 | U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""), |
| 561 | }; |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 562 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 563 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Enric Balletbo i Serra | aac7359 | 2010-10-19 11:13:22 +0200 | [diff] [blame] | 564 | void onenand_reloc(void) { |
| 565 | fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub)); |
| 566 | } |
| 567 | #endif |
| 568 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 569 | static int do_onenand(struct cmd_tbl *cmdtp, int flag, int argc, |
| 570 | char *const argv[]) |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 571 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 572 | struct cmd_tbl *c; |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 573 | |
Enric Balletbo i Serra | a915394 | 2010-10-19 11:13:21 +0200 | [diff] [blame] | 574 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 575 | return CMD_RET_USAGE; |
Enric Balletbo i Serra | a915394 | 2010-10-19 11:13:21 +0200 | [diff] [blame] | 576 | |
Frans Meulenbroeks | 889ef81 | 2010-03-27 17:14:36 +0100 | [diff] [blame] | 577 | mtd = &onenand_mtd; |
| 578 | |
| 579 | /* Strip off leading 'onenand' command argument */ |
| 580 | argc--; |
| 581 | argv++; |
| 582 | |
| 583 | c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub)); |
| 584 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 585 | if (c) |
| 586 | return c->cmd(cmdtp, flag, argc, argv); |
| 587 | else |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 588 | return CMD_RET_USAGE; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | U_BOOT_CMD( |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 592 | onenand, CONFIG_SYS_MAXARGS, 1, do_onenand, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 593 | "OneNAND sub-system", |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 594 | "info - show available OneNAND devices\n" |
| 595 | "onenand bad - show bad blocks\n" |
| 596 | "onenand read[.oob] addr off size\n" |
Lei Wen | 2ca9e14 | 2010-12-02 05:17:55 +0000 | [diff] [blame] | 597 | "onenand write[.yaffs] addr off size\n" |
Stefan Roese | 9743d28 | 2008-11-12 13:47:24 +0100 | [diff] [blame] | 598 | " read/write 'size' bytes starting at offset 'off'\n" |
| 599 | " to/from memory address 'addr', skipping bad blocks.\n" |
| 600 | "onenand erase [force] [off size] - erase 'size' bytes from\n" |
| 601 | "onenand test [off size] - test 'size' bytes from\n" |
| 602 | " offset 'off' (entire device if not specified)\n" |
| 603 | "onenand dump[.oob] off - dump page\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 604 | "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)" |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 605 | ); |