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%zx\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 = {};
192 int blocksize = 1 << this->erase_shift;
194 for (ofs = start; ofs < (start + size); ofs += blocksize) {
195 ret = mtd_block_isbad(mtd, ofs);
197 printf("Skip erase bad block %d at 0x%x\n",
198 (u32)(ofs >> this->erase_shift), (u32)ofs);
203 instr.len = blocksize;
206 ret = mtd_erase(mtd, &instr);
208 printf("erase failed block %d at 0x%x\n",
209 (u32)(ofs >> this->erase_shift), (u32)ofs);
217 static int onenand_block_test(u32 start, u32 size)
219 struct onenand_chip *this = mtd->priv;
220 struct erase_info instr = {};
224 int blocksize = 1 << this->erase_shift;
225 int start_block, end_block;
231 buf = malloc(blocksize);
233 printf("Not enough malloc space available!\n");
237 verify_buf = malloc(blocksize);
239 printf("Not enough malloc space available!\n");
243 start_block = start >> this->erase_shift;
244 end_block = (start + size) >> this->erase_shift;
246 /* Protect boot-loader from badblock testing */
250 if (end_block > (mtd->size >> this->erase_shift))
251 end_block = mtd->size >> this->erase_shift;
253 blocks = start_block;
255 while (blocks < end_block) {
256 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
258 ret = mtd_block_isbad(mtd, ofs);
260 printf("Skip erase bad block %d at 0x%x\n",
261 (u32)(ofs >> this->erase_shift), (u32)ofs);
266 instr.len = blocksize;
267 ret = mtd_erase(mtd, &instr);
269 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
273 ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
275 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
279 ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
281 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
285 if (memcmp(buf, verify_buf, blocksize))
286 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
300 static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
303 u_char *datbuf, *oobbuf, *p;
304 struct mtd_oob_ops ops;
307 datbuf = malloc(mtd->writesize + mtd->oobsize);
308 oobbuf = malloc(mtd->oobsize);
309 if (!datbuf || !oobbuf) {
310 puts("No memory for page buffer\n");
313 off &= ~(mtd->writesize - 1);
315 memset(&ops, 0, sizeof(ops));
318 ops.len = mtd->writesize;
319 ops.ooblen = mtd->oobsize;
321 i = mtd_read_oob(mtd, addr, &ops);
323 printf("Error (%d) reading page %08lx\n", i, off);
328 printf("Page %08lx dump:\n", off);
329 i = mtd->writesize >> 4;
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],
342 i = mtd->oobsize >> 3;
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]);
356 static int do_onenand_info(struct cmd_tbl *cmdtp, int flag, int argc,
359 printf("%s\n", mtd->name);
363 static int do_onenand_bad(struct cmd_tbl *cmdtp, int flag, int argc,
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) {
372 if (mtd_block_isbad(mtd, ofs))
373 printf(" %08x\n", (u32)ofs);
379 static int do_onenand_read(struct cmd_tbl *cmdtp, int flag, int argc,
390 return CMD_RET_USAGE;
392 s = strchr(argv[0], '.');
393 if ((s != NULL) && (!strcmp(s, ".oob")))
396 addr = (ulong)hextoul(argv[1], NULL);
398 printf("\nOneNAND read: ");
399 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
402 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
404 printf(" %zu bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
406 return ret == 0 ? 0 : 1;
409 static int do_onenand_write(struct cmd_tbl *cmdtp, int flag, int argc,
414 int ret = 0, withoob = 0;
418 return CMD_RET_USAGE;
420 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
423 addr = (ulong)hextoul(argv[1], NULL);
425 printf("\nOneNAND write: ");
426 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
429 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
431 printf(" %zu bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
433 return ret == 0 ? 0 : 1;
436 static int do_onenand_erase(struct cmd_tbl *cmdtp, int flag, int argc,
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(struct cmd_tbl *cmdtp, int flag, int argc,
483 * onenand test [force] [off size]
486 printf("\nOneNAND test: ");
488 /* skip first two or three arguments, look for offset and size */
489 if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
492 ret = onenand_block_test(ofs, len);
494 printf("%s\n", ret ? "ERROR" : "OK");
496 return ret == 0 ? 0 : 1;
499 static int do_onenand_dump(struct cmd_tbl *cmdtp, int flag, int argc,
507 return CMD_RET_USAGE;
509 s = strchr(argv[0], '.');
510 ofs = (int)hextoul(argv[1], NULL);
512 if (s != NULL && strcmp(s, ".oob") == 0)
513 ret = onenand_dump(mtd, ofs, 1);
515 ret = onenand_dump(mtd, ofs, 0);
517 return ret == 0 ? 1 : 0;
520 static int do_onenand_markbad(struct cmd_tbl *cmdtp, int flag, int argc,
530 return CMD_RET_USAGE;
533 addr = hextoul(*argv, NULL);
535 if (mtd_block_markbad(mtd, addr)) {
536 printf("block 0x%08lx NOT marked "
537 "as bad! ERROR %d\n",
541 printf("block 0x%08lx successfully "
551 static struct cmd_tbl cmd_onenand_sub[] = {
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, "", ""),
556 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
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, "", ""),
563 #ifdef CONFIG_NEEDS_MANUAL_RELOC
564 void onenand_reloc(void) {
565 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
569 static int do_onenand(struct cmd_tbl *cmdtp, int flag, int argc,
575 return CMD_RET_USAGE;
579 /* Strip off leading 'onenand' command argument */
583 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
586 return c->cmd(cmdtp, flag, argc, argv);
588 return CMD_RET_USAGE;
592 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
593 "OneNAND sub-system",
594 "info - show available OneNAND devices\n"
595 "onenand bad - show bad blocks\n"
596 "onenand read[.oob] addr off size\n"
597 "onenand write[.yaffs] addr off size\n"
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"
604 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"