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, ssize_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, ssize_t len,
69 ssize_t *retlen, u_char *buf, int oob)
71 struct onenand_chip *this = mtd->priv;
72 int blocksize = (1 << this->erase_shift);
74 struct mtd_oob_ops ops = {
81 thislen = min_t(ssize_t, len, blocksize);
82 thislen = ALIGN(thislen, mtd->writesize);
84 ret = mtd->block_isbad(mtd, ofs);
86 printk("Bad blocks %d at 0x%x\n",
87 (u32)(ofs >> this->erase_shift), (u32)ofs);
89 /* FIXME need to check how to handle the 'len' */
103 ret = mtd->read_oob(mtd, ofs, &ops);
105 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
112 *retlen += ops.retlen;
118 static int onenand_block_write(loff_t to, ssize_t len,
119 ssize_t *retlen, const u_char * buf)
121 struct onenand_chip *this = mtd->priv;
122 int blocksize = (1 << this->erase_shift);
123 struct mtd_oob_ops ops = {
131 if (to == next_ofs) {
141 thislen = min_t(ssize_t, len, blocksize);
142 thislen = ALIGN(thislen, mtd->writesize);
144 ret = mtd->block_isbad(mtd, ofs);
146 printk("Bad blocks %d at 0x%x\n",
147 (u32)(ofs >> this->erase_shift), (u32)ofs);
148 skip_ofs += blocksize;
152 ops.datbuf = (u_char *) buf;
155 ret = mtd->write_oob(mtd, ofs, &ops);
157 printk("Write failed 0x%x, %d", (u32)ofs, ret);
164 *retlen += ops.retlen;
172 static int onenand_block_erase(u32 start, u32 size, int force)
174 struct onenand_chip *this = mtd->priv;
175 struct erase_info instr = {
180 int blocksize = 1 << this->erase_shift;
182 for (ofs = start; ofs < (start + size); ofs += blocksize) {
183 ret = mtd->block_isbad(mtd, ofs);
185 printf("Skip erase bad block %d at 0x%x\n",
186 (u32)(ofs >> this->erase_shift), (u32)ofs);
191 instr.len = blocksize;
194 ret = mtd->erase(mtd, &instr);
196 printf("erase failed block %d at 0x%x\n",
197 (u32)(ofs >> this->erase_shift), (u32)ofs);
205 static int onenand_block_test(u32 start, u32 size)
207 struct onenand_chip *this = mtd->priv;
208 struct erase_info instr = {
215 int blocksize = 1 << this->erase_shift;
216 int start_block, end_block;
222 buf = malloc(blocksize);
224 printf("Not enough malloc space available!\n");
228 verify_buf = malloc(blocksize);
230 printf("Not enough malloc space available!\n");
234 start_block = start >> this->erase_shift;
235 end_block = (start + size) >> this->erase_shift;
237 /* Protect boot-loader from badblock testing */
241 if (end_block > (mtd->size >> this->erase_shift))
242 end_block = mtd->size >> this->erase_shift;
244 blocks = start_block;
245 ofs = start_block << this->erase_shift;
246 while (blocks < end_block) {
247 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
249 ret = mtd->block_isbad(mtd, ofs);
251 printf("Skip erase bad block %d at 0x%x\n",
252 (u32)(ofs >> this->erase_shift), (u32)ofs);
257 instr.len = blocksize;
258 ret = mtd->erase(mtd, &instr);
260 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
264 ret = mtd->write(mtd, ofs, blocksize, &retlen, buf);
266 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
270 ret = mtd->read(mtd, ofs, blocksize, &retlen, verify_buf);
272 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
276 if (memcmp(buf, verify_buf, blocksize)) {
277 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
292 static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
295 u_char *datbuf, *oobbuf, *p;
296 struct mtd_oob_ops ops;
299 datbuf = malloc(mtd->writesize + mtd->oobsize);
300 oobbuf = malloc(mtd->oobsize);
301 if (!datbuf || !oobbuf) {
302 puts("No memory for page buffer\n");
305 off &= ~(mtd->writesize - 1);
307 memset(&ops, 0, sizeof(ops));
309 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
310 ops.len = mtd->writesize;
311 ops.ooblen = mtd->oobsize;
313 i = mtd->read_oob(mtd, addr, &ops);
315 printf("Error (%d) reading page %08lx\n", i, off);
320 printf("Page %08lx dump:\n", off);
321 i = mtd->writesize >> 4;
326 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
327 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
328 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
329 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
335 i = mtd->oobsize >> 3;
337 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
338 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
347 int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
349 struct onenand_chip *this;
352 ssize_t len, retlen = 0;
358 blocksize = (1 << this->erase_shift);
368 if (strcmp(cmd, "info") == 0) {
369 printf("%s\n", mtd->name);
373 if (strcmp(cmd, "bad") == 0) {
374 /* Currently only one OneNAND device is supported */
375 printf("\nDevice %d bad blocks:\n", 0);
376 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
377 if (mtd->block_isbad(mtd, ofs))
378 printf(" %08x\n", (u32)ofs);
385 /* At least 4 args */
390 * onenand erase [force] [off size]
392 if ((strcmp(cmd, "erase") == 0) || (strcmp(cmd, "test") == 0)) {
393 int force = argc > 2 && !strcmp("force", argv[2]);
394 int o = force ? 3 : 2;
397 erase = strcmp(cmd, "erase") == 0; /* 1 = erase, 0 = test */
398 printf("\nOneNAND %s %s: ", erase ? "erase" : "test",
399 force ? "force" : "");
401 /* skip first two or three arguments, look for offset and size */
402 if (arg_off_size(argc - o, argv + o, &ofs, &len) != 0)
406 ret = onenand_block_erase(ofs, len, force);
408 ret = onenand_block_test(ofs, len);
410 printf("%s\n", ret ? "ERROR" : "OK");
412 return ret == 0 ? 0 : 1;
415 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
422 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
424 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
425 printf("\nOneNAND %s: ", read ? "read" : "write");
426 if (arg_off_size(argc - 3, argv + 3, &ofs, &len) != 0)
429 s = strchr(cmd, '.');
430 if ((s != NULL) && (!strcmp(s, ".oob")))
434 ret = onenand_block_read(ofs, len, &retlen,
437 ret = onenand_block_write(ofs, len, &retlen,
441 printf(" %d bytes %s: %s\n", retlen,
442 read ? "read" : "written", ret ? "ERROR" : "OK");
444 return ret == 0 ? 0 : 1;
447 if (strcmp(cmd, "markbad") == 0) {
455 addr = simple_strtoul(*argv, NULL, 16);
457 if (mtd->block_markbad(mtd, addr)) {
458 printf("block 0x%08lx NOT marked "
459 "as bad! ERROR %d\n",
463 printf("block 0x%08lx successfully "
473 if (strncmp(cmd, "dump", 4) == 0) {
477 s = strchr(cmd, '.');
478 ofs = (int)simple_strtoul(argv[2], NULL, 16);
480 if (s != NULL && strcmp(s, ".oob") == 0)
481 ret = onenand_dump(mtd, ofs, 1);
483 ret = onenand_dump(mtd, ofs, 0);
485 return ret == 0 ? 1 : 0;
499 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
500 "OneNAND sub-system",
501 "info - show available OneNAND devices\n"
502 "onenand bad - show bad blocks\n"
503 "onenand read[.oob] addr off size\n"
504 "onenand write[.oob] addr off size\n"
505 " read/write 'size' bytes starting at offset 'off'\n"
506 " to/from memory address 'addr', skipping bad blocks.\n"
507 "onenand erase [force] [off size] - erase 'size' bytes from\n"
508 "onenand test [off size] - test 'size' bytes from\n"
509 " offset 'off' (entire device if not specified)\n"
510 "onenand dump[.oob] off - dump page\n"
511 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"