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