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 *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%x) 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 int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
335 struct onenand_chip *this;
338 size_t len, retlen = 0;
344 blocksize = (1 << this->erase_shift);
354 if (strcmp(cmd, "info") == 0) {
355 printf("%s\n", mtd->name);
359 if (strcmp(cmd, "bad") == 0) {
360 /* Currently only one OneNAND device is supported */
361 printf("\nDevice %d bad blocks:\n", 0);
362 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
363 if (mtd->block_isbad(mtd, ofs))
364 printf(" %08x\n", (u32)ofs);
371 /* At least 4 args */
376 * onenand erase [force] [off size]
378 if ((strcmp(cmd, "erase") == 0) || (strcmp(cmd, "test") == 0)) {
379 int force = argc > 2 && !strcmp("force", argv[2]);
380 int o = force ? 3 : 2;
383 erase = strcmp(cmd, "erase") == 0; /* 1 = erase, 0 = test */
384 printf("\nOneNAND %s: ", erase ? "erase" : "test");
386 /* skip first two or three arguments, look for offset and size */
387 if (arg_off_size(argc - o, argv + o, &ofs, &len) != 0)
391 ret = onenand_block_erase(ofs, len, force);
393 ret = onenand_block_test(ofs, len);
395 printf("%s\n", ret ? "ERROR" : "OK");
397 return ret == 0 ? 0 : 1;
400 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
407 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
409 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
410 printf("\nOneNAND %s: ", read ? "read" : "write");
411 if (arg_off_size(argc - 3, argv + 3, &ofs, &len) != 0)
414 s = strchr(cmd, '.');
415 if ((s != NULL) && (!strcmp(s, ".oob")))
419 ret = onenand_block_read(ofs, len, &retlen,
422 ret = onenand_block_write(ofs, len, &retlen,
426 printf(" %d bytes %s: %s\n", retlen,
427 read ? "read" : "written", ret ? "ERROR" : "OK");
429 return ret == 0 ? 0 : 1;
432 if (strcmp(cmd, "markbad") == 0) {
433 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
435 int ret = mtd->block_markbad(mtd, addr);
437 printf("block 0x%08lx successfully marked as bad\n",
441 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
447 if (strncmp(cmd, "dump", 4) == 0) {
451 s = strchr(cmd, '.');
452 ofs = (int)simple_strtoul(argv[2], NULL, 16);
454 if (s != NULL && strcmp(s, ".oob") == 0)
455 ret = onenand_dump(mtd, ofs, 1);
457 ret = onenand_dump(mtd, ofs, 0);
459 return ret == 0 ? 1 : 0;
468 printf("Usage:\n%s\n", cmdtp->usage);
473 onenand, 6, 1, do_onenand,
474 "onenand - OneNAND sub-system\n",
475 "info - show available OneNAND devices\n"
476 "onenand bad - show bad blocks\n"
477 "onenand read[.oob] addr off size\n"
478 "onenand write[.oob] addr off size\n"
479 " read/write 'size' bytes starting at offset 'off'\n"
480 " to/from memory address 'addr', skipping bad blocks.\n"
481 "onenand erase [force] [off size] - erase 'size' bytes from\n"
482 "onenand test [off size] - test 'size' bytes from\n"
483 " offset 'off' (entire device if not specified)\n"
484 "onenand dump[.oob] off - dump page\n"
485 "onenand markbad off - mark bad block at offset (UNSAFE)\n"