]> Git Repo - J-u-boot.git/blame - cmd/onenand.c
Merge branch 'qcom-main' of https://source.denx.de/u-boot/custodians/u-boot-snapdragon
[J-u-boot.git] / cmd / onenand.c
CommitLineData
d7e8ce10
KP
1/*
2 * U-Boot command for OneNAND support
3 *
c438ea17 4 * Copyright (C) 2005-2008 Samsung Electronics
d7e8ce10
KP
5 * Kyungmin Park <[email protected]>
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
d7e8ce10 12#include <command.h>
c438ea17 13#include <malloc.h>
1e94b46f 14#include <linux/printk.h>
d7e8ce10 15
7b15e2bb 16#include <linux/compat.h>
d7e8ce10
KP
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/onenand.h>
19
20#include <asm/io.h>
21
c438ea17
SR
22static struct mtd_info *mtd;
23
24static loff_t next_ofs;
25static loff_t skip_ofs;
26
09140113 27static int arg_off_size_onenand(int argc, char *const argv[], ulong *off,
09c32807 28 size_t *size)
c438ea17
SR
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) {
8d2effea 49 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
c438ea17
SR
50 return -1;
51 }
52
53 if (*size == mtd->size)
54 puts("whole chip\n");
55 else
6d813197 56 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
c438ea17
SR
57
58 return 0;
59}
60
61static 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) {
dfe64e2c 79 ret = mtd_block_isbad(mtd, ofs);
c438ea17
SR
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;
dfe64e2c 93 ret = mtd_read_oob(mtd, ofs, &ops);
c438ea17
SR
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
41c86240
LW
108static 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,
dfe64e2c 114 .mode = MTD_OPS_AUTO_OOB,
41c86240
LW
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;
dfe64e2c 122 ret = mtd_write_oob(mtd, to, &ops);
41c86240
LW
123 if (ret)
124 break;
125 to += mtd->writesize;
126 }
127
128 *retlen = (ret) ? 0 : mtd->erasesize;
129 return ret;
130}
131
c438ea17 132static int onenand_block_write(loff_t to, size_t len,
41c86240 133 size_t *retlen, const u_char * buf, int withoob)
c438ea17
SR
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
9c00d982
LM
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
c438ea17
SR
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) {
dfe64e2c 158 ret = mtd_block_isbad(mtd, ofs);
c438ea17
SR
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
41c86240 166 if (!withoob)
dfe64e2c 167 ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
41c86240
LW
168 else
169 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
c438ea17
SR
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;
179next:
180 ofs += blocksize;
181 }
182
183 return 0;
184}
185
186static int onenand_block_erase(u32 start, u32 size, int force)
187{
188 struct onenand_chip *this = mtd->priv;
0d1ecc99 189 struct erase_info instr = {};
c438ea17
SR
190 loff_t ofs;
191 int ret;
192 int blocksize = 1 << this->erase_shift;
193
194 for (ofs = start; ofs < (start + size); ofs += blocksize) {
dfe64e2c 195 ret = mtd_block_isbad(mtd, ofs);
c438ea17
SR
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;
dfe64e2c 206 ret = mtd_erase(mtd, &instr);
c438ea17
SR
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
217static int onenand_block_test(u32 start, u32 size)
218{
219 struct onenand_chip *this = mtd->priv;
0d1ecc99 220 struct erase_info instr = {};
c438ea17
SR
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
dfe64e2c 258 ret = mtd_block_isbad(mtd, ofs);
c438ea17
SR
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;
dfe64e2c 267 ret = mtd_erase(mtd, &instr);
c438ea17
SR
268 if (ret) {
269 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
270 goto next;
271 }
272
dfe64e2c 273 ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
c438ea17
SR
274 if (ret) {
275 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
276 goto next;
277 }
278
dfe64e2c 279 ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
c438ea17
SR
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
288next:
289 ofs += blocksize;
290 blocks++;
291 }
292 printf("...Done\n");
293
294 free(buf);
295 free(verify_buf);
296
297 return 0;
298}
299
300static 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;
a430b137 317 ops.oobbuf = oobbuf;
c438ea17
SR
318 ops.len = mtd->writesize;
319 ops.ooblen = mtd->oobsize;
320 ops.retlen = 0;
dfe64e2c 321 i = mtd_read_oob(mtd, addr, &ops);
c438ea17
SR
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;
a430b137
LW
343 p = oobbuf;
344
c438ea17
SR
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}
d7e8ce10 355
09140113
SG
356static int do_onenand_info(struct cmd_tbl *cmdtp, int flag, int argc,
357 char *const argv[])
d7e8ce10 358{
8cd85282
FM
359 printf("%s\n", mtd->name);
360 return 0;
361}
362
09140113
SG
363static int do_onenand_bad(struct cmd_tbl *cmdtp, int flag, int argc,
364 char *const argv[])
8cd85282
FM
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) {
dfe64e2c 372 if (mtd_block_isbad(mtd, ofs))
8cd85282
FM
373 printf(" %08x\n", (u32)ofs);
374 }
375
376 return 0;
377}
378
09140113
SG
379static int do_onenand_read(struct cmd_tbl *cmdtp, int flag, int argc,
380 char *const argv[])
8cd85282
FM
381{
382 char *s;
383 int oob = 0;
c438ea17 384 ulong addr, ofs;
8cd85282 385 size_t len;
8360b66b 386 int ret = 0;
8cd85282 387 size_t retlen = 0;
c438ea17 388
8cd85282 389 if (argc < 3)
4c12eeb8 390 return CMD_RET_USAGE;
c438ea17 391
8cd85282
FM
392 s = strchr(argv[0], '.');
393 if ((s != NULL) && (!strcmp(s, ".oob")))
394 oob = 1;
d7e8ce10 395
7e5f460e 396 addr = (ulong)hextoul(argv[1], NULL);
d7e8ce10 397
8cd85282 398 printf("\nOneNAND read: ");
09c32807 399 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
8cd85282 400 return 1;
c438ea17 401
8cd85282 402 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
c438ea17 403
6d813197 404 printf(" %zu bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
d7e8ce10 405
8cd85282
FM
406 return ret == 0 ? 0 : 1;
407}
a9da2b41 408
09140113
SG
409static int do_onenand_write(struct cmd_tbl *cmdtp, int flag, int argc,
410 char *const argv[])
8cd85282
FM
411{
412 ulong addr, ofs;
413 size_t len;
41c86240 414 int ret = 0, withoob = 0;
8cd85282 415 size_t retlen = 0;
d7e8ce10 416
8cd85282 417 if (argc < 3)
4c12eeb8 418 return CMD_RET_USAGE;
d7e8ce10 419
41c86240
LW
420 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
421 withoob = 1;
422
7e5f460e 423 addr = (ulong)hextoul(argv[1], NULL);
8cd85282
FM
424
425 printf("\nOneNAND write: ");
09c32807 426 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
8cd85282 427 return 1;
d7e8ce10 428
41c86240 429 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
d7e8ce10 430
6d813197 431 printf(" %zu bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
c438ea17 432
8cd85282
FM
433 return ret == 0 ? 0 : 1;
434}
435
09140113
SG
436static int do_onenand_erase(struct cmd_tbl *cmdtp, int flag, int argc,
437 char *const argv[])
8cd85282
FM
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++;
d7e8ce10 458 }
8cd85282
FM
459 }
460 printf("\nOneNAND erase: ");
d7e8ce10 461
8cd85282 462 /* skip first two or three arguments, look for offset and size */
09c32807 463 if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0)
8cd85282 464 return 1;
bfd7f386 465
8cd85282 466 ret = onenand_block_erase(ofs, len, force);
bfd7f386 467
8cd85282 468 printf("%s\n", ret ? "ERROR" : "OK");
d7e8ce10 469
8cd85282
FM
470 return ret == 0 ? 0 : 1;
471}
d7e8ce10 472
09140113
SG
473static int do_onenand_test(struct cmd_tbl *cmdtp, int flag, int argc,
474 char *const argv[])
8cd85282
FM
475{
476 ulong ofs;
477 int ret = 0;
478 size_t len;
d7e8ce10 479
8cd85282
FM
480 /*
481 * Syntax is:
482 * 0 1 2 3 4
483 * onenand test [force] [off size]
484 */
d7e8ce10 485
8cd85282 486 printf("\nOneNAND test: ");
d7e8ce10 487
8cd85282 488 /* skip first two or three arguments, look for offset and size */
09c32807 489 if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
8cd85282 490 return 1;
d7e8ce10 491
8cd85282 492 ret = onenand_block_test(ofs, len);
d7e8ce10 493
8cd85282 494 printf("%s\n", ret ? "ERROR" : "OK");
d7e8ce10 495
8cd85282
FM
496 return ret == 0 ? 0 : 1;
497}
bfd7f386 498
09140113
SG
499static int do_onenand_dump(struct cmd_tbl *cmdtp, int flag, int argc,
500 char *const argv[])
8cd85282
FM
501{
502 ulong ofs;
503 int ret = 0;
504 char *s;
d7e8ce10 505
8cd85282 506 if (argc < 2)
4c12eeb8 507 return CMD_RET_USAGE;
8cd85282
FM
508
509 s = strchr(argv[0], '.');
7e5f460e 510 ofs = (int)hextoul(argv[1], NULL);
8cd85282
FM
511
512 if (s != NULL && strcmp(s, ".oob") == 0)
513 ret = onenand_dump(mtd, ofs, 1);
514 else
515 ret = onenand_dump(mtd, ofs, 0);
d7e8ce10 516
8cd85282
FM
517 return ret == 0 ? 1 : 0;
518}
519
09140113
SG
520static int do_onenand_markbad(struct cmd_tbl *cmdtp, int flag, int argc,
521 char *const argv[])
8cd85282
FM
522{
523 int ret = 0;
524 ulong addr;
525
526 argc -= 2;
527 argv += 2;
528
529 if (argc <= 0)
4c12eeb8 530 return CMD_RET_USAGE;
d7e8ce10 531
8cd85282 532 while (argc > 0) {
7e5f460e 533 addr = hextoul(*argv, NULL);
8cd85282 534
dfe64e2c 535 if (mtd_block_markbad(mtd, addr)) {
8cd85282
FM
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
09140113 551static struct cmd_tbl cmd_onenand_sub[] = {
8cd85282
FM
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, "", ""),
41c86240 556 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
8cd85282
FM
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};
562
09140113
SG
563static int do_onenand(struct cmd_tbl *cmdtp, int flag, int argc,
564 char *const argv[])
8cd85282 565{
09140113 566 struct cmd_tbl *c;
8cd85282 567
57ff9f24 568 if (argc < 2)
4c12eeb8 569 return CMD_RET_USAGE;
57ff9f24 570
8cd85282
FM
571 mtd = &onenand_mtd;
572
573 /* Strip off leading 'onenand' command argument */
574 argc--;
575 argv++;
c438ea17 576
8cd85282
FM
577 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
578
47e26b1b
WD
579 if (c)
580 return c->cmd(cmdtp, flag, argc, argv);
581 else
4c12eeb8 582 return CMD_RET_USAGE;
d7e8ce10
KP
583}
584
585U_BOOT_CMD(
8360b66b 586 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
2fb2604d 587 "OneNAND sub-system",
c438ea17
SR
588 "info - show available OneNAND devices\n"
589 "onenand bad - show bad blocks\n"
590 "onenand read[.oob] addr off size\n"
41c86240 591 "onenand write[.yaffs] addr off size\n"
c438ea17
SR
592 " read/write 'size' bytes starting at offset 'off'\n"
593 " to/from memory address 'addr', skipping bad blocks.\n"
594 "onenand erase [force] [off size] - erase 'size' bytes from\n"
595 "onenand test [off size] - test 'size' bytes from\n"
596 " offset 'off' (entire device if not specified)\n"
597 "onenand dump[.oob] off - dump page\n"
a89c33db 598 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
d7e8ce10 599);
This page took 0.615131 seconds and 4 git commands to generate.