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/mtd/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 inline int str2long(char *p, ulong *num)
31 *num = simple_strtoul(p, &endptr, 16);
32 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
35 static int arg_off_size(int argc, char * const argv[], ulong *off, size_t *size)
38 if (!(str2long(argv[0], off))) {
39 printf("'%s' is not a number\n", argv[0]);
47 if (!(str2long(argv[1], (ulong *)size))) {
48 printf("'%s' is not a number\n", argv[1]);
52 *size = mtd->size - *off;
55 if ((*off + *size) > mtd->size) {
56 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
60 if (*size == mtd->size)
63 printf("offset 0x%lx, size 0x%x\n", *off, *size);
68 static int onenand_block_read(loff_t from, size_t len,
69 size_t *retlen, u_char *buf, int oob)
71 struct onenand_chip *this = mtd->priv;
72 int blocks = (int) len >> this->erase_shift;
73 int blocksize = (1 << this->erase_shift);
75 struct mtd_oob_ops ops = {
81 ops.ooblen = blocksize;
86 ret = mtd->block_isbad(mtd, ofs);
88 printk("Bad blocks %d at 0x%x\n",
89 (u32)(ofs >> this->erase_shift), (u32)ofs);
100 ret = mtd->read_oob(mtd, ofs, &ops);
102 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
109 *retlen += ops.retlen;
115 static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf,
118 struct mtd_oob_ops ops = {
119 .len = mtd->writesize,
120 .ooblen = mtd->oobsize,
121 .mode = MTD_OOB_AUTO,
124 for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) {
125 ops.datbuf = (u_char *)buf;
126 buf += mtd->writesize;
127 ops.oobbuf = (u_char *)buf;
129 ret = mtd->write_oob(mtd, to, &ops);
132 to += mtd->writesize;
135 *retlen = (ret) ? 0 : mtd->erasesize;
139 static int onenand_block_write(loff_t to, size_t len,
140 size_t *retlen, const u_char * buf, int withoob)
142 struct onenand_chip *this = mtd->priv;
143 int blocks = len >> this->erase_shift;
144 int blocksize = (1 << this->erase_shift);
149 if (to == next_ofs) {
159 ret = mtd->block_isbad(mtd, ofs);
161 printk("Bad blocks %d at 0x%x\n",
162 (u32)(ofs >> this->erase_shift), (u32)ofs);
163 skip_ofs += blocksize;
168 ret = mtd->write(mtd, ofs, blocksize, &_retlen, buf);
170 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
172 printk("Write failed 0x%x, %d", (u32)ofs, ret);
173 skip_ofs += blocksize;
187 static int onenand_block_erase(u32 start, u32 size, int force)
189 struct onenand_chip *this = mtd->priv;
190 struct erase_info instr = {
195 int blocksize = 1 << this->erase_shift;
197 for (ofs = start; ofs < (start + size); ofs += blocksize) {
198 ret = mtd->block_isbad(mtd, ofs);
200 printf("Skip erase bad block %d at 0x%x\n",
201 (u32)(ofs >> this->erase_shift), (u32)ofs);
206 instr.len = blocksize;
209 ret = mtd->erase(mtd, &instr);
211 printf("erase failed block %d at 0x%x\n",
212 (u32)(ofs >> this->erase_shift), (u32)ofs);
220 static int onenand_block_test(u32 start, u32 size)
222 struct onenand_chip *this = mtd->priv;
223 struct erase_info instr = {
230 int blocksize = 1 << this->erase_shift;
231 int start_block, end_block;
237 buf = malloc(blocksize);
239 printf("Not enough malloc space available!\n");
243 verify_buf = malloc(blocksize);
245 printf("Not enough malloc space available!\n");
249 start_block = start >> this->erase_shift;
250 end_block = (start + size) >> this->erase_shift;
252 /* Protect boot-loader from badblock testing */
256 if (end_block > (mtd->size >> this->erase_shift))
257 end_block = mtd->size >> this->erase_shift;
259 blocks = start_block;
261 while (blocks < end_block) {
262 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
264 ret = mtd->block_isbad(mtd, ofs);
266 printf("Skip erase bad block %d at 0x%x\n",
267 (u32)(ofs >> this->erase_shift), (u32)ofs);
272 instr.len = blocksize;
273 ret = mtd->erase(mtd, &instr);
275 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
279 ret = mtd->write(mtd, ofs, blocksize, &retlen, buf);
281 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
285 ret = mtd->read(mtd, ofs, blocksize, &retlen, verify_buf);
287 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
291 if (memcmp(buf, verify_buf, blocksize))
292 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
306 static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
309 u_char *datbuf, *oobbuf, *p;
310 struct mtd_oob_ops ops;
313 datbuf = malloc(mtd->writesize + mtd->oobsize);
314 oobbuf = malloc(mtd->oobsize);
315 if (!datbuf || !oobbuf) {
316 puts("No memory for page buffer\n");
319 off &= ~(mtd->writesize - 1);
321 memset(&ops, 0, sizeof(ops));
324 ops.len = mtd->writesize;
325 ops.ooblen = mtd->oobsize;
327 i = mtd->read_oob(mtd, addr, &ops);
329 printf("Error (%d) reading page %08lx\n", i, off);
334 printf("Page %08lx dump:\n", off);
335 i = mtd->writesize >> 4;
340 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
341 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
342 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
343 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
348 i = mtd->oobsize >> 3;
352 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
353 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
362 static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
364 printf("%s\n", mtd->name);
368 static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
373 /* Currently only one OneNAND device is supported */
374 printf("\nDevice %d bad blocks:\n", 0);
375 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
376 if (mtd->block_isbad(mtd, ofs))
377 printf(" %08x\n", (u32)ofs);
383 static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
393 return CMD_RET_USAGE;
395 s = strchr(argv[0], '.');
396 if ((s != NULL) && (!strcmp(s, ".oob")))
399 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
401 printf("\nOneNAND read: ");
402 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
405 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
407 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
409 return ret == 0 ? 0 : 1;
412 static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
416 int ret = 0, withoob = 0;
420 return CMD_RET_USAGE;
422 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
425 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
427 printf("\nOneNAND write: ");
428 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
431 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
433 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
435 return ret == 0 ? 0 : 1;
438 static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
448 * onenand erase [force] [off size]
454 if (!strcmp("force", argv[0]))
461 printf("\nOneNAND erase: ");
463 /* skip first two or three arguments, look for offset and size */
464 if (arg_off_size(argc, argv, &ofs, &len) != 0)
467 ret = onenand_block_erase(ofs, len, force);
469 printf("%s\n", ret ? "ERROR" : "OK");
471 return ret == 0 ? 0 : 1;
474 static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
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(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(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
506 return CMD_RET_USAGE;
508 s = strchr(argv[0], '.');
509 ofs = (int)simple_strtoul(argv[1], NULL, 16);
511 if (s != NULL && strcmp(s, ".oob") == 0)
512 ret = onenand_dump(mtd, ofs, 1);
514 ret = onenand_dump(mtd, ofs, 0);
516 return ret == 0 ? 1 : 0;
519 static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
528 return CMD_RET_USAGE;
531 addr = simple_strtoul(*argv, NULL, 16);
533 if (mtd->block_markbad(mtd, addr)) {
534 printf("block 0x%08lx NOT marked "
535 "as bad! ERROR %d\n",
539 printf("block 0x%08lx successfully "
549 static cmd_tbl_t cmd_onenand_sub[] = {
550 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
551 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
552 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
553 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
554 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
555 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
556 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
557 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
558 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
561 #ifdef CONFIG_NEEDS_MANUAL_RELOC
562 void onenand_reloc(void) {
563 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
567 static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
572 return CMD_RET_USAGE;
576 /* Strip off leading 'onenand' command argument */
580 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
583 return c->cmd(cmdtp, flag, argc, argv);
585 return CMD_RET_USAGE;
589 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
590 "OneNAND sub-system",
591 "info - show available OneNAND devices\n"
592 "onenand bad - show bad blocks\n"
593 "onenand read[.oob] addr off size\n"
594 "onenand write[.yaffs] addr off size\n"
595 " read/write 'size' bytes starting at offset 'off'\n"
596 " to/from memory address 'addr', skipping bad blocks.\n"
597 "onenand erase [force] [off size] - erase 'size' bytes from\n"
598 "onenand test [off size] - test 'size' bytes from\n"
599 " offset 'off' (entire device if not specified)\n"
600 "onenand dump[.oob] off - dump page\n"
601 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"