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
[])
364 return cmd_usage(cmdtp
);
366 s
= strchr(argv
[0], '.');
367 if ((s
!= NULL
) && (!strcmp(s
, ".oob")))
370 addr
= (ulong
)simple_strtoul(argv
[1], NULL
, 16);
372 printf("\nOneNAND read: ");
373 if (arg_off_size(argc
- 2, argv
+ 2, &ofs
, &len
) != 0)
376 ret
= onenand_block_read(ofs
, len
, &retlen
, (u8
*)addr
, oob
);
378 printf(" %d bytes read: %s\n", retlen
, ret
? "ERROR" : "OK");
380 return ret
== 0 ? 0 : 1;
383 static int do_onenand_write(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
391 return cmd_usage(cmdtp
);
393 addr
= (ulong
)simple_strtoul(argv
[1], NULL
, 16);
395 printf("\nOneNAND write: ");
396 if (arg_off_size(argc
- 2, argv
+ 2, &ofs
, &len
) != 0)
399 ret
= onenand_block_write(ofs
, len
, &retlen
, (u8
*)addr
);
401 printf(" %d bytes written: %s\n", retlen
, ret
? "ERROR" : "OK");
403 return ret
== 0 ? 0 : 1;
406 static int do_onenand_erase(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
416 * onenand erase [force] [off size]
422 if (!strcmp("force", argv
[0]))
429 printf("\nOneNAND erase: ");
431 /* skip first two or three arguments, look for offset and size */
432 if (arg_off_size(argc
, argv
, &ofs
, &len
) != 0)
435 ret
= onenand_block_erase(ofs
, len
, force
);
437 printf("%s\n", ret
? "ERROR" : "OK");
439 return ret
== 0 ? 0 : 1;
442 static int do_onenand_test(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
451 * onenand test [force] [off size]
454 printf("\nOneNAND test: ");
456 /* skip first two or three arguments, look for offset and size */
457 if (arg_off_size(argc
- 1, argv
+ 1, &ofs
, &len
) != 0)
460 ret
= onenand_block_test(ofs
, len
);
462 printf("%s\n", ret
? "ERROR" : "OK");
464 return ret
== 0 ? 0 : 1;
467 static int do_onenand_dump(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
474 return cmd_usage(cmdtp
);
476 s
= strchr(argv
[0], '.');
477 ofs
= (int)simple_strtoul(argv
[1], NULL
, 16);
479 if (s
!= NULL
&& strcmp(s
, ".oob") == 0)
480 ret
= onenand_dump(mtd
, ofs
, 1);
482 ret
= onenand_dump(mtd
, ofs
, 0);
484 return ret
== 0 ? 1 : 0;
487 static int do_onenand_markbad(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
496 return cmd_usage(cmdtp
);
499 addr
= simple_strtoul(*argv
, NULL
, 16);
501 if (mtd
->block_markbad(mtd
, addr
)) {
502 printf("block 0x%08lx NOT marked "
503 "as bad! ERROR %d\n",
507 printf("block 0x%08lx successfully "
517 static cmd_tbl_t cmd_onenand_sub
[] = {
518 U_BOOT_CMD_MKENT(info
, 1, 0, do_onenand_info
, "", ""),
519 U_BOOT_CMD_MKENT(bad
, 1, 0, do_onenand_bad
, "", ""),
520 U_BOOT_CMD_MKENT(read
, 4, 0, do_onenand_read
, "", ""),
521 U_BOOT_CMD_MKENT(write
, 4, 0, do_onenand_write
, "", ""),
522 U_BOOT_CMD_MKENT(erase
, 3, 0, do_onenand_erase
, "", ""),
523 U_BOOT_CMD_MKENT(test
, 3, 0, do_onenand_test
, "", ""),
524 U_BOOT_CMD_MKENT(dump
, 2, 0, do_onenand_dump
, "", ""),
525 U_BOOT_CMD_MKENT(markbad
, CONFIG_SYS_MAXARGS
, 0, do_onenand_markbad
, "", ""),
528 #ifndef CONFIG_RELOC_FIXUP_WORKS
529 void onenand_reloc(void) {
530 fixup_cmdtable(cmd_onenand_sub
, ARRAY_SIZE(cmd_onenand_sub
));
534 static int do_onenand(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
540 /* Strip off leading 'onenand' command argument */
544 c
= find_cmd_tbl(argv
[0], &cmd_onenand_sub
[0], ARRAY_SIZE(cmd_onenand_sub
));
547 return c
->cmd(cmdtp
, flag
, argc
, argv
);
549 return cmd_usage(cmdtp
);
553 onenand
, CONFIG_SYS_MAXARGS
, 1, do_onenand
,
554 "OneNAND sub-system",
555 "info - show available OneNAND devices\n"
556 "onenand bad - show bad blocks\n"
557 "onenand read[.oob] addr off size\n"
558 "onenand write addr off size\n"
559 " read/write 'size' bytes starting at offset 'off'\n"
560 " to/from memory address 'addr', skipping bad blocks.\n"
561 "onenand erase [force] [off size] - erase 'size' bytes from\n"
562 "onenand test [off size] - test 'size' bytes from\n"
563 " offset 'off' (entire device if not specified)\n"
564 "onenand dump[.oob] off - dump page\n"
565 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"