2 * U-Boot command for OneNAND support
4 * Copyright (C) 2005-2008 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
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.
16 #include <linux/compat.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/onenand.h>
22 static struct mtd_info
*mtd
;
24 static loff_t next_ofs
;
25 static loff_t skip_ofs
;
27 static int arg_off_size_onenand(int argc
, char * const argv
[], ulong
*off
,
31 if (!(str2long(argv
[0], off
))) {
32 printf("'%s' is not a number\n", argv
[0]);
40 if (!(str2long(argv
[1], (ulong
*)size
))) {
41 printf("'%s' is not a number\n", argv
[1]);
45 *size
= mtd
->size
- *off
;
48 if ((*off
+ *size
) > mtd
->size
) {
49 printf("total chip size (0x%llx) exceeded!\n", mtd
->size
);
53 if (*size
== mtd
->size
)
56 printf("offset 0x%lx, size 0x%x\n", *off
, *size
);
61 static int onenand_block_read(loff_t from
, size_t len
,
62 size_t *retlen
, u_char
*buf
, int oob
)
64 struct onenand_chip
*this = mtd
->priv
;
65 int blocks
= (int) len
>> this->erase_shift
;
66 int blocksize
= (1 << this->erase_shift
);
68 struct mtd_oob_ops ops
= {
74 ops
.ooblen
= blocksize
;
79 ret
= mtd_block_isbad(mtd
, ofs
);
81 printk("Bad blocks %d at 0x%x\n",
82 (u32
)(ofs
>> this->erase_shift
), (u32
)ofs
);
93 ret
= mtd_read_oob(mtd
, ofs
, &ops
);
95 printk("Read failed 0x%x, %d\n", (u32
)ofs
, ret
);
102 *retlen
+= ops
.retlen
;
108 static int onenand_write_oneblock_withoob(loff_t to
, const u_char
* buf
,
111 struct mtd_oob_ops ops
= {
112 .len
= mtd
->writesize
,
113 .ooblen
= mtd
->oobsize
,
114 .mode
= MTD_OPS_AUTO_OOB
,
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
;
122 ret
= mtd_write_oob(mtd
, to
, &ops
);
125 to
+= mtd
->writesize
;
128 *retlen
= (ret
) ? 0 : mtd
->erasesize
;
132 static int onenand_block_write(loff_t to
, size_t len
,
133 size_t *retlen
, const u_char
* buf
, int withoob
)
135 struct onenand_chip
*this = mtd
->priv
;
136 int blocks
= len
>> this->erase_shift
;
137 int blocksize
= (1 << this->erase_shift
);
142 if ((to
& (mtd
->writesize
- 1)) != 0) {
143 printf("Attempt to write non block-aligned data\n");
148 if (to
== next_ofs
) {
158 ret
= mtd_block_isbad(mtd
, ofs
);
160 printk("Bad blocks %d at 0x%x\n",
161 (u32
)(ofs
>> this->erase_shift
), (u32
)ofs
);
162 skip_ofs
+= blocksize
;
167 ret
= mtd_write(mtd
, ofs
, blocksize
, &_retlen
, buf
);
169 ret
= onenand_write_oneblock_withoob(ofs
, buf
, &_retlen
);
171 printk("Write failed 0x%x, %d", (u32
)ofs
, ret
);
172 skip_ofs
+= blocksize
;
186 static int onenand_block_erase(u32 start
, u32 size
, int force
)
188 struct onenand_chip
*this = mtd
->priv
;
189 struct erase_info instr
= {
194 int blocksize
= 1 << this->erase_shift
;
196 for (ofs
= start
; ofs
< (start
+ size
); ofs
+= blocksize
) {
197 ret
= mtd_block_isbad(mtd
, ofs
);
199 printf("Skip erase bad block %d at 0x%x\n",
200 (u32
)(ofs
>> this->erase_shift
), (u32
)ofs
);
205 instr
.len
= blocksize
;
208 ret
= mtd_erase(mtd
, &instr
);
210 printf("erase failed block %d at 0x%x\n",
211 (u32
)(ofs
>> this->erase_shift
), (u32
)ofs
);
219 static int onenand_block_test(u32 start
, u32 size
)
221 struct onenand_chip
*this = mtd
->priv
;
222 struct erase_info instr
= {
229 int blocksize
= 1 << this->erase_shift
;
230 int start_block
, end_block
;
236 buf
= malloc(blocksize
);
238 printf("Not enough malloc space available!\n");
242 verify_buf
= malloc(blocksize
);
244 printf("Not enough malloc space available!\n");
248 start_block
= start
>> this->erase_shift
;
249 end_block
= (start
+ size
) >> this->erase_shift
;
251 /* Protect boot-loader from badblock testing */
255 if (end_block
> (mtd
->size
>> this->erase_shift
))
256 end_block
= mtd
->size
>> this->erase_shift
;
258 blocks
= start_block
;
260 while (blocks
< end_block
) {
261 printf("\rTesting block %d at 0x%x", (u32
)(ofs
>> this->erase_shift
), (u32
)ofs
);
263 ret
= mtd_block_isbad(mtd
, ofs
);
265 printf("Skip erase bad block %d at 0x%x\n",
266 (u32
)(ofs
>> this->erase_shift
), (u32
)ofs
);
271 instr
.len
= blocksize
;
272 ret
= mtd_erase(mtd
, &instr
);
274 printk("Erase failed 0x%x, %d\n", (u32
)ofs
, ret
);
278 ret
= mtd_write(mtd
, ofs
, blocksize
, &retlen
, buf
);
280 printk("Write failed 0x%x, %d\n", (u32
)ofs
, ret
);
284 ret
= mtd_read(mtd
, ofs
, blocksize
, &retlen
, verify_buf
);
286 printk("Read failed 0x%x, %d\n", (u32
)ofs
, ret
);
290 if (memcmp(buf
, verify_buf
, blocksize
))
291 printk("\nRead/Write test failed at 0x%x\n", (u32
)ofs
);
305 static int onenand_dump(struct mtd_info
*mtd
, ulong off
, int only_oob
)
308 u_char
*datbuf
, *oobbuf
, *p
;
309 struct mtd_oob_ops ops
;
312 datbuf
= malloc(mtd
->writesize
+ mtd
->oobsize
);
313 oobbuf
= malloc(mtd
->oobsize
);
314 if (!datbuf
|| !oobbuf
) {
315 puts("No memory for page buffer\n");
318 off
&= ~(mtd
->writesize
- 1);
320 memset(&ops
, 0, sizeof(ops
));
323 ops
.len
= mtd
->writesize
;
324 ops
.ooblen
= mtd
->oobsize
;
326 i
= mtd_read_oob(mtd
, addr
, &ops
);
328 printf("Error (%d) reading page %08lx\n", i
, off
);
333 printf("Page %08lx dump:\n", off
);
334 i
= mtd
->writesize
>> 4;
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],
347 i
= mtd
->oobsize
>> 3;
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]);
361 static int do_onenand_info(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
363 printf("%s\n", mtd
->name
);
367 static int do_onenand_bad(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
372 /* Currently only one OneNAND device is supported */
373 printf("\nDevice %d bad blocks:\n", 0);
374 for (ofs
= 0; ofs
< mtd
->size
; ofs
+= mtd
->erasesize
) {
375 if (mtd_block_isbad(mtd
, ofs
))
376 printf(" %08x\n", (u32
)ofs
);
382 static int do_onenand_read(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
392 return CMD_RET_USAGE
;
394 s
= strchr(argv
[0], '.');
395 if ((s
!= NULL
) && (!strcmp(s
, ".oob")))
398 addr
= (ulong
)simple_strtoul(argv
[1], NULL
, 16);
400 printf("\nOneNAND read: ");
401 if (arg_off_size_onenand(argc
- 2, argv
+ 2, &ofs
, &len
) != 0)
404 ret
= onenand_block_read(ofs
, len
, &retlen
, (u8
*)addr
, oob
);
406 printf(" %d bytes read: %s\n", retlen
, ret
? "ERROR" : "OK");
408 return ret
== 0 ? 0 : 1;
411 static int do_onenand_write(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
415 int ret
= 0, withoob
= 0;
419 return CMD_RET_USAGE
;
421 if (strncmp(argv
[0] + 6, "yaffs", 5) == 0)
424 addr
= (ulong
)simple_strtoul(argv
[1], NULL
, 16);
426 printf("\nOneNAND write: ");
427 if (arg_off_size_onenand(argc
- 2, argv
+ 2, &ofs
, &len
) != 0)
430 ret
= onenand_block_write(ofs
, len
, &retlen
, (u8
*)addr
, withoob
);
432 printf(" %d bytes written: %s\n", retlen
, ret
? "ERROR" : "OK");
434 return ret
== 0 ? 0 : 1;
437 static int do_onenand_erase(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
447 * onenand erase [force] [off size]
453 if (!strcmp("force", argv
[0]))
460 printf("\nOneNAND erase: ");
462 /* skip first two or three arguments, look for offset and size */
463 if (arg_off_size_onenand(argc
, argv
, &ofs
, &len
) != 0)
466 ret
= onenand_block_erase(ofs
, len
, force
);
468 printf("%s\n", ret
? "ERROR" : "OK");
470 return ret
== 0 ? 0 : 1;
473 static int do_onenand_test(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
482 * onenand test [force] [off size]
485 printf("\nOneNAND test: ");
487 /* skip first two or three arguments, look for offset and size */
488 if (arg_off_size_onenand(argc
- 1, argv
+ 1, &ofs
, &len
) != 0)
491 ret
= onenand_block_test(ofs
, len
);
493 printf("%s\n", ret
? "ERROR" : "OK");
495 return ret
== 0 ? 0 : 1;
498 static int do_onenand_dump(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
505 return CMD_RET_USAGE
;
507 s
= strchr(argv
[0], '.');
508 ofs
= (int)simple_strtoul(argv
[1], NULL
, 16);
510 if (s
!= NULL
&& strcmp(s
, ".oob") == 0)
511 ret
= onenand_dump(mtd
, ofs
, 1);
513 ret
= onenand_dump(mtd
, ofs
, 0);
515 return ret
== 0 ? 1 : 0;
518 static int do_onenand_markbad(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
527 return CMD_RET_USAGE
;
530 addr
= simple_strtoul(*argv
, NULL
, 16);
532 if (mtd_block_markbad(mtd
, addr
)) {
533 printf("block 0x%08lx NOT marked "
534 "as bad! ERROR %d\n",
538 printf("block 0x%08lx successfully "
548 static cmd_tbl_t cmd_onenand_sub
[] = {
549 U_BOOT_CMD_MKENT(info
, 1, 0, do_onenand_info
, "", ""),
550 U_BOOT_CMD_MKENT(bad
, 1, 0, do_onenand_bad
, "", ""),
551 U_BOOT_CMD_MKENT(read
, 4, 0, do_onenand_read
, "", ""),
552 U_BOOT_CMD_MKENT(write
, 4, 0, do_onenand_write
, "", ""),
553 U_BOOT_CMD_MKENT(write
.yaffs
, 4, 0, do_onenand_write
, "", ""),
554 U_BOOT_CMD_MKENT(erase
, 3, 0, do_onenand_erase
, "", ""),
555 U_BOOT_CMD_MKENT(test
, 3, 0, do_onenand_test
, "", ""),
556 U_BOOT_CMD_MKENT(dump
, 2, 0, do_onenand_dump
, "", ""),
557 U_BOOT_CMD_MKENT(markbad
, CONFIG_SYS_MAXARGS
, 0, do_onenand_markbad
, "", ""),
560 #ifdef CONFIG_NEEDS_MANUAL_RELOC
561 void onenand_reloc(void) {
562 fixup_cmdtable(cmd_onenand_sub
, ARRAY_SIZE(cmd_onenand_sub
));
566 static int do_onenand(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
571 return CMD_RET_USAGE
;
575 /* Strip off leading 'onenand' command argument */
579 c
= find_cmd_tbl(argv
[0], &cmd_onenand_sub
[0], ARRAY_SIZE(cmd_onenand_sub
));
582 return c
->cmd(cmdtp
, flag
, argc
, argv
);
584 return CMD_RET_USAGE
;
588 onenand
, CONFIG_SYS_MAXARGS
, 1, do_onenand
,
589 "OneNAND sub-system",
590 "info - show available OneNAND devices\n"
591 "onenand bad - show bad blocks\n"
592 "onenand read[.oob] addr off size\n"
593 "onenand write[.yaffs] addr off size\n"
594 " read/write 'size' bytes starting at offset 'off'\n"
595 " to/from memory address 'addr', skipping bad blocks.\n"
596 "onenand erase [force] [off size] - erase 'size' bytes from\n"
597 "onenand test [off size] - test 'size' bytes from\n"
598 " offset 'off' (entire device if not specified)\n"
599 "onenand dump[.oob] off - dump page\n"
600 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"