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 == next_ofs) {
152 ret = mtd_block_isbad(mtd, ofs);
154 printk("Bad blocks %d at 0x%x\n",
155 (u32)(ofs >> this->erase_shift), (u32)ofs);
156 skip_ofs += blocksize;
161 ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
163 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
165 printk("Write failed 0x%x, %d", (u32)ofs, ret);
166 skip_ofs += blocksize;
180 static int onenand_block_erase(u32 start, u32 size, int force)
182 struct onenand_chip *this = mtd->priv;
183 struct erase_info instr = {
188 int blocksize = 1 << this->erase_shift;
190 for (ofs = start; ofs < (start + size); ofs += blocksize) {
191 ret = mtd_block_isbad(mtd, ofs);
193 printf("Skip erase bad block %d at 0x%x\n",
194 (u32)(ofs >> this->erase_shift), (u32)ofs);
199 instr.len = blocksize;
202 ret = mtd_erase(mtd, &instr);
204 printf("erase failed block %d at 0x%x\n",
205 (u32)(ofs >> this->erase_shift), (u32)ofs);
213 static int onenand_block_test(u32 start, u32 size)
215 struct onenand_chip *this = mtd->priv;
216 struct erase_info instr = {
223 int blocksize = 1 << this->erase_shift;
224 int start_block, end_block;
230 buf = malloc(blocksize);
232 printf("Not enough malloc space available!\n");
236 verify_buf = malloc(blocksize);
238 printf("Not enough malloc space available!\n");
242 start_block = start >> this->erase_shift;
243 end_block = (start + size) >> this->erase_shift;
245 /* Protect boot-loader from badblock testing */
249 if (end_block > (mtd->size >> this->erase_shift))
250 end_block = mtd->size >> this->erase_shift;
252 blocks = start_block;
254 while (blocks < end_block) {
255 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
257 ret = mtd_block_isbad(mtd, ofs);
259 printf("Skip erase bad block %d at 0x%x\n",
260 (u32)(ofs >> this->erase_shift), (u32)ofs);
265 instr.len = blocksize;
266 ret = mtd_erase(mtd, &instr);
268 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
272 ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
274 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
278 ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
280 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
284 if (memcmp(buf, verify_buf, blocksize))
285 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
299 static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
302 u_char *datbuf, *oobbuf, *p;
303 struct mtd_oob_ops ops;
306 datbuf = malloc(mtd->writesize + mtd->oobsize);
307 oobbuf = malloc(mtd->oobsize);
308 if (!datbuf || !oobbuf) {
309 puts("No memory for page buffer\n");
312 off &= ~(mtd->writesize - 1);
314 memset(&ops, 0, sizeof(ops));
317 ops.len = mtd->writesize;
318 ops.ooblen = mtd->oobsize;
320 i = mtd_read_oob(mtd, addr, &ops);
322 printf("Error (%d) reading page %08lx\n", i, off);
327 printf("Page %08lx dump:\n", off);
328 i = mtd->writesize >> 4;
333 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
334 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
335 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
336 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
341 i = mtd->oobsize >> 3;
345 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
346 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
355 static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
357 printf("%s\n", mtd->name);
361 static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
366 /* Currently only one OneNAND device is supported */
367 printf("\nDevice %d bad blocks:\n", 0);
368 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
369 if (mtd_block_isbad(mtd, ofs))
370 printf(" %08x\n", (u32)ofs);
376 static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
386 return CMD_RET_USAGE;
388 s = strchr(argv[0], '.');
389 if ((s != NULL) && (!strcmp(s, ".oob")))
392 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
394 printf("\nOneNAND read: ");
395 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
398 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
400 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
402 return ret == 0 ? 0 : 1;
405 static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
409 int ret = 0, withoob = 0;
413 return CMD_RET_USAGE;
415 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
418 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
420 printf("\nOneNAND write: ");
421 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
424 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
426 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
428 return ret == 0 ? 0 : 1;
431 static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
441 * onenand erase [force] [off size]
447 if (!strcmp("force", argv[0]))
454 printf("\nOneNAND erase: ");
456 /* skip first two or three arguments, look for offset and size */
457 if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0)
460 ret = onenand_block_erase(ofs, len, force);
462 printf("%s\n", ret ? "ERROR" : "OK");
464 return ret == 0 ? 0 : 1;
467 static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
476 * onenand test [force] [off size]
479 printf("\nOneNAND test: ");
481 /* skip first two or three arguments, look for offset and size */
482 if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
485 ret = onenand_block_test(ofs, len);
487 printf("%s\n", ret ? "ERROR" : "OK");
489 return ret == 0 ? 0 : 1;
492 static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
499 return CMD_RET_USAGE;
501 s = strchr(argv[0], '.');
502 ofs = (int)simple_strtoul(argv[1], NULL, 16);
504 if (s != NULL && strcmp(s, ".oob") == 0)
505 ret = onenand_dump(mtd, ofs, 1);
507 ret = onenand_dump(mtd, ofs, 0);
509 return ret == 0 ? 1 : 0;
512 static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
521 return CMD_RET_USAGE;
524 addr = simple_strtoul(*argv, NULL, 16);
526 if (mtd_block_markbad(mtd, addr)) {
527 printf("block 0x%08lx NOT marked "
528 "as bad! ERROR %d\n",
532 printf("block 0x%08lx successfully "
542 static cmd_tbl_t cmd_onenand_sub[] = {
543 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
544 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
545 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
546 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
547 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
548 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
549 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
550 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
551 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
554 #ifdef CONFIG_NEEDS_MANUAL_RELOC
555 void onenand_reloc(void) {
556 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
560 static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
565 return CMD_RET_USAGE;
569 /* Strip off leading 'onenand' command argument */
573 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
576 return c->cmd(cmdtp, flag, argc, argv);
578 return CMD_RET_USAGE;
582 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
583 "OneNAND sub-system",
584 "info - show available OneNAND devices\n"
585 "onenand bad - show bad blocks\n"
586 "onenand read[.oob] addr off size\n"
587 "onenand write[.yaffs] addr off size\n"
588 " read/write 'size' bytes starting at offset 'off'\n"
589 " to/from memory address 'addr', skipping bad blocks.\n"
590 "onenand erase [force] [off size] - erase 'size' bytes from\n"
591 "onenand test [off size] - test 'size' bytes from\n"
592 " offset 'off' (entire device if not specified)\n"
593 "onenand dump[.oob] off - dump page\n"
594 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"