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_block_write(loff_t to, size_t len,
116 size_t *retlen, const u_char * buf)
118 struct onenand_chip *this = mtd->priv;
119 int blocks = len >> this->erase_shift;
120 int blocksize = (1 << this->erase_shift);
125 if (to == next_ofs) {
135 ret = mtd->block_isbad(mtd, ofs);
137 printk("Bad blocks %d at 0x%x\n",
138 (u32)(ofs >> this->erase_shift), (u32)ofs);
139 skip_ofs += blocksize;
143 ret = mtd->write(mtd, ofs, blocksize, &_retlen, buf);
145 printk("Write failed 0x%x, %d", (u32)ofs, ret);
146 skip_ofs += blocksize;
160 static int onenand_block_erase(u32 start, u32 size, int force)
162 struct onenand_chip *this = mtd->priv;
163 struct erase_info instr = {
168 int blocksize = 1 << this->erase_shift;
170 for (ofs = start; ofs < (start + size); ofs += blocksize) {
171 ret = mtd->block_isbad(mtd, ofs);
173 printf("Skip erase bad block %d at 0x%x\n",
174 (u32)(ofs >> this->erase_shift), (u32)ofs);
179 instr.len = blocksize;
182 ret = mtd->erase(mtd, &instr);
184 printf("erase failed block %d at 0x%x\n",
185 (u32)(ofs >> this->erase_shift), (u32)ofs);
193 static int onenand_block_test(u32 start, u32 size)
195 struct onenand_chip *this = mtd->priv;
196 struct erase_info instr = {
203 int blocksize = 1 << this->erase_shift;
204 int start_block, end_block;
210 buf = malloc(blocksize);
212 printf("Not enough malloc space available!\n");
216 verify_buf = malloc(blocksize);
218 printf("Not enough malloc space available!\n");
222 start_block = start >> this->erase_shift;
223 end_block = (start + size) >> this->erase_shift;
225 /* Protect boot-loader from badblock testing */
229 if (end_block > (mtd->size >> this->erase_shift))
230 end_block = mtd->size >> this->erase_shift;
232 blocks = start_block;
234 while (blocks < end_block) {
235 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
237 ret = mtd->block_isbad(mtd, ofs);
239 printf("Skip erase bad block %d at 0x%x\n",
240 (u32)(ofs >> this->erase_shift), (u32)ofs);
245 instr.len = blocksize;
246 ret = mtd->erase(mtd, &instr);
248 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
252 ret = mtd->write(mtd, ofs, blocksize, &retlen, buf);
254 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
258 ret = mtd->read(mtd, ofs, blocksize, &retlen, verify_buf);
260 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
264 if (memcmp(buf, verify_buf, blocksize))
265 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
279 static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
282 u_char *datbuf, *oobbuf, *p;
283 struct mtd_oob_ops ops;
286 datbuf = malloc(mtd->writesize + mtd->oobsize);
287 oobbuf = malloc(mtd->oobsize);
288 if (!datbuf || !oobbuf) {
289 puts("No memory for page buffer\n");
292 off &= ~(mtd->writesize - 1);
294 memset(&ops, 0, sizeof(ops));
296 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
297 ops.len = mtd->writesize;
298 ops.ooblen = mtd->oobsize;
300 i = mtd->read_oob(mtd, addr, &ops);
302 printf("Error (%d) reading page %08lx\n", i, off);
307 printf("Page %08lx dump:\n", off);
308 i = mtd->writesize >> 4;
313 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
314 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
315 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
316 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
321 i = mtd->oobsize >> 3;
323 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
324 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
333 static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
335 printf("%s\n", mtd->name);
339 static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
344 /* Currently only one OneNAND device is supported */
345 printf("\nDevice %d bad blocks:\n", 0);
346 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
347 if (mtd->block_isbad(mtd, ofs))
348 printf(" %08x\n", (u32)ofs);
354 static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
369 s = strchr(argv[0], '.');
370 if ((s != NULL) && (!strcmp(s, ".oob")))
373 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
375 printf("\nOneNAND read: ");
376 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
379 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
381 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
383 return ret == 0 ? 0 : 1;
386 static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
399 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
401 printf("\nOneNAND write: ");
402 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
405 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr);
407 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
409 return ret == 0 ? 0 : 1;
412 static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
422 * onenand erase [force] [off size]
428 if (!strcmp("force", argv[0]))
435 printf("\nOneNAND erase: ");
437 /* skip first two or three arguments, look for offset and size */
438 if (arg_off_size(argc, argv, &ofs, &len) != 0)
441 ret = onenand_block_erase(ofs, len, force);
443 printf("%s\n", ret ? "ERROR" : "OK");
445 return ret == 0 ? 0 : 1;
448 static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
457 * onenand test [force] [off size]
460 printf("\nOneNAND test: ");
462 /* skip first two or three arguments, look for offset and size */
463 if (arg_off_size(argc - 1, argv + 1, &ofs, &len) != 0)
466 ret = onenand_block_test(ofs, len);
468 printf("%s\n", ret ? "ERROR" : "OK");
470 return ret == 0 ? 0 : 1;
473 static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
485 s = strchr(argv[0], '.');
486 ofs = (int)simple_strtoul(argv[1], NULL, 16);
488 if (s != NULL && strcmp(s, ".oob") == 0)
489 ret = onenand_dump(mtd, ofs, 1);
491 ret = onenand_dump(mtd, ofs, 0);
493 return ret == 0 ? 1 : 0;
496 static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
511 addr = simple_strtoul(*argv, NULL, 16);
513 if (mtd->block_markbad(mtd, addr)) {
514 printf("block 0x%08lx NOT marked "
515 "as bad! ERROR %d\n",
519 printf("block 0x%08lx successfully "
529 static cmd_tbl_t cmd_onenand_sub[] = {
530 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
531 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
532 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
533 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
534 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
535 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
536 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
537 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
540 static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
546 /* Strip off leading 'onenand' command argument */
550 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
553 return c->cmd(cmdtp, flag, argc, argv);
561 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
562 "OneNAND sub-system",
563 "info - show available OneNAND devices\n"
564 "onenand bad - show bad blocks\n"
565 "onenand read[.oob] addr off size\n"
566 "onenand write addr off size\n"
567 " read/write 'size' bytes starting at offset 'off'\n"
568 " to/from memory address 'addr', skipping bad blocks.\n"
569 "onenand erase [force] [off size] - erase 'size' bytes from\n"
570 "onenand test [off size] - test 'size' bytes from\n"
571 " offset 'off' (entire device if not specified)\n"
572 "onenand dump[.oob] off - dump page\n"
573 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"