onenand.c 12.7 KB
Newer Older
Kyungmin Park's avatar
Kyungmin Park committed
1 2 3
/*
 *  U-Boot command for OneNAND support
 *
4
 *  Copyright (C) 2005-2008 Samsung Electronics
Kyungmin Park's avatar
Kyungmin Park committed
5 6 7 8 9 10 11 12 13
 *  Kyungmin Park <kyungmin.park@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <common.h>
#include <command.h>
14
#include <malloc.h>
Kyungmin Park's avatar
Kyungmin Park committed
15

16
#include <linux/compat.h>
Kyungmin Park's avatar
Kyungmin Park committed
17 18 19 20 21
#include <linux/mtd/mtd.h>
#include <linux/mtd/onenand.h>

#include <asm/io.h>

22 23 24 25 26
static struct mtd_info *mtd;

static loff_t next_ofs;
static loff_t skip_ofs;

27 28
static int arg_off_size_onenand(int argc, char * const argv[], ulong *off,
				size_t *size)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
	if (argc >= 1) {
		if (!(str2long(argv[0], off))) {
			printf("'%s' is not a number\n", argv[0]);
			return -1;
		}
	} else {
		*off = 0;
	}

	if (argc >= 2) {
		if (!(str2long(argv[1], (ulong *)size))) {
			printf("'%s' is not a number\n", argv[1]);
			return -1;
		}
	} else {
		*size = mtd->size - *off;
	}

	if ((*off + *size) > mtd->size) {
49
		printf("total chip size (0x%llx) exceeded!\n", mtd->size);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		return -1;
	}

	if (*size == mtd->size)
		puts("whole chip\n");
	else
		printf("offset 0x%lx, size 0x%x\n", *off, *size);

	return 0;
}

static int onenand_block_read(loff_t from, size_t len,
			      size_t *retlen, u_char *buf, int oob)
{
	struct onenand_chip *this = mtd->priv;
	int blocks = (int) len >> this->erase_shift;
	int blocksize = (1 << this->erase_shift);
	loff_t ofs = from;
	struct mtd_oob_ops ops = {
		.retlen		= 0,
	};
	int ret;

	if (oob)
		ops.ooblen = blocksize;
	else
		ops.len = blocksize;

	while (blocks) {
Sergey Lapin's avatar
Sergey Lapin committed
79
		ret = mtd_block_isbad(mtd, ofs);
80 81 82 83 84 85 86 87 88 89 90 91 92
		if (ret) {
			printk("Bad blocks %d at 0x%x\n",
			       (u32)(ofs >> this->erase_shift), (u32)ofs);
			ofs += blocksize;
			continue;
		}

		if (oob)
			ops.oobbuf = buf;
		else
			ops.datbuf = buf;

		ops.retlen = 0;
Sergey Lapin's avatar
Sergey Lapin committed
93
		ret = mtd_read_oob(mtd, ofs, &ops);
94 95 96 97 98 99 100 101 102 103 104 105 106 107
		if (ret) {
			printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
			ofs += blocksize;
			continue;
		}
		ofs += blocksize;
		buf += blocksize;
		blocks--;
		*retlen += ops.retlen;
	}

	return 0;
}

Lei Wen's avatar
Lei Wen committed
108 109 110 111 112 113
static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf,
					  size_t *retlen)
{
	struct mtd_oob_ops ops = {
		.len = mtd->writesize,
		.ooblen = mtd->oobsize,
Sergey Lapin's avatar
Sergey Lapin committed
114
		.mode = MTD_OPS_AUTO_OOB,
Lei Wen's avatar
Lei Wen committed
115 116 117 118 119 120 121
	};
	int page, ret = 0;
	for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) {
		ops.datbuf = (u_char *)buf;
		buf += mtd->writesize;
		ops.oobbuf = (u_char *)buf;
		buf += mtd->oobsize;
Sergey Lapin's avatar
Sergey Lapin committed
122
		ret = mtd_write_oob(mtd, to, &ops);
Lei Wen's avatar
Lei Wen committed
123 124 125 126 127 128 129 130 131
		if (ret)
			break;
		to += mtd->writesize;
	}

	*retlen = (ret) ? 0 : mtd->erasesize;
	return ret;
}

132
static int onenand_block_write(loff_t to, size_t len,
Lei Wen's avatar
Lei Wen committed
133
			       size_t *retlen, const u_char * buf, int withoob)
134 135 136 137 138 139 140 141
{
	struct onenand_chip *this = mtd->priv;
	int blocks = len >> this->erase_shift;
	int blocksize = (1 << this->erase_shift);
	loff_t ofs;
	size_t _retlen = 0;
	int ret;

142 143 144 145 146 147
	if ((to & (mtd->writesize - 1)) != 0) {
		printf("Attempt to write non block-aligned data\n");
		*retlen = 0;
		return 1;
	}

148 149 150 151 152 153 154 155 156 157
	if (to == next_ofs) {
		next_ofs = to + len;
		to += skip_ofs;
	} else {
		next_ofs = to + len;
		skip_ofs = 0;
	}
	ofs = to;

	while (blocks) {
Sergey Lapin's avatar
Sergey Lapin committed
158
		ret = mtd_block_isbad(mtd, ofs);
159 160 161 162 163 164 165
		if (ret) {
			printk("Bad blocks %d at 0x%x\n",
			       (u32)(ofs >> this->erase_shift), (u32)ofs);
			skip_ofs += blocksize;
			goto next;
		}

Lei Wen's avatar
Lei Wen committed
166
		if (!withoob)
Sergey Lapin's avatar
Sergey Lapin committed
167
			ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
Lei Wen's avatar
Lei Wen committed
168 169
		else
			ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		if (ret) {
			printk("Write failed 0x%x, %d", (u32)ofs, ret);
			skip_ofs += blocksize;
			goto next;
		}

		buf += blocksize;
		blocks--;
		*retlen += _retlen;
next:
		ofs += blocksize;
	}

	return 0;
}

static int onenand_block_erase(u32 start, u32 size, int force)
{
	struct onenand_chip *this = mtd->priv;
	struct erase_info instr = {
		.callback	= NULL,
	};
	loff_t ofs;
	int ret;
	int blocksize = 1 << this->erase_shift;

	for (ofs = start; ofs < (start + size); ofs += blocksize) {
Sergey Lapin's avatar
Sergey Lapin committed
197
		ret = mtd_block_isbad(mtd, ofs);
198 199 200 201 202 203 204 205 206 207
		if (ret && !force) {
			printf("Skip erase bad block %d at 0x%x\n",
			       (u32)(ofs >> this->erase_shift), (u32)ofs);
			continue;
		}

		instr.addr = ofs;
		instr.len = blocksize;
		instr.priv = force;
		instr.mtd = mtd;
Sergey Lapin's avatar
Sergey Lapin committed
208
		ret = mtd_erase(mtd, &instr);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		if (ret) {
			printf("erase failed block %d at 0x%x\n",
			       (u32)(ofs >> this->erase_shift), (u32)ofs);
			continue;
		}
	}

	return 0;
}

static int onenand_block_test(u32 start, u32 size)
{
	struct onenand_chip *this = mtd->priv;
	struct erase_info instr = {
		.callback	= NULL,
		.priv		= 0,
	};

	int blocks;
	loff_t ofs;
	int blocksize = 1 << this->erase_shift;
	int start_block, end_block;
	size_t retlen;
	u_char *buf;
	u_char *verify_buf;
	int ret;

	buf = malloc(blocksize);
	if (!buf) {
		printf("Not enough malloc space available!\n");
		return -1;
	}

	verify_buf = malloc(blocksize);
	if (!verify_buf) {
		printf("Not enough malloc space available!\n");
		return -1;
	}

	start_block = start >> this->erase_shift;
	end_block = (start + size) >> this->erase_shift;

	/* Protect boot-loader from badblock testing */
	if (start_block < 2)
		start_block = 2;

	if (end_block > (mtd->size >> this->erase_shift))
		end_block = mtd->size >> this->erase_shift;

	blocks = start_block;
	ofs = start;
	while (blocks < end_block) {
		printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);

Sergey Lapin's avatar
Sergey Lapin committed
263
		ret = mtd_block_isbad(mtd, ofs);
264 265 266 267 268 269 270 271
		if (ret) {
			printf("Skip erase bad block %d at 0x%x\n",
			       (u32)(ofs >> this->erase_shift), (u32)ofs);
			goto next;
		}

		instr.addr = ofs;
		instr.len = blocksize;
Sergey Lapin's avatar
Sergey Lapin committed
272
		ret = mtd_erase(mtd, &instr);
273 274 275 276 277
		if (ret) {
			printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
			goto next;
		}

Sergey Lapin's avatar
Sergey Lapin committed
278
		ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
279 280 281 282 283
		if (ret) {
			printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
			goto next;
		}

Sergey Lapin's avatar
Sergey Lapin committed
284
		ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		if (ret) {
			printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
			goto next;
		}

		if (memcmp(buf, verify_buf, blocksize))
			printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);

next:
		ofs += blocksize;
		blocks++;
	}
	printf("...Done\n");

	free(buf);
	free(verify_buf);

	return 0;
}

static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
{
	int i;
	u_char *datbuf, *oobbuf, *p;
	struct mtd_oob_ops ops;
	loff_t addr;

	datbuf = malloc(mtd->writesize + mtd->oobsize);
	oobbuf = malloc(mtd->oobsize);
	if (!datbuf || !oobbuf) {
		puts("No memory for page buffer\n");
		return 1;
	}
	off &= ~(mtd->writesize - 1);
	addr = (loff_t) off;
	memset(&ops, 0, sizeof(ops));
	ops.datbuf = datbuf;
Lei Wen's avatar
Lei Wen committed
322
	ops.oobbuf = oobbuf;
323 324 325
	ops.len = mtd->writesize;
	ops.ooblen = mtd->oobsize;
	ops.retlen = 0;
Sergey Lapin's avatar
Sergey Lapin committed
326
	i = mtd_read_oob(mtd, addr, &ops);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	if (i < 0) {
		printf("Error (%d) reading page %08lx\n", i, off);
		free(datbuf);
		free(oobbuf);
		return 1;
	}
	printf("Page %08lx dump:\n", off);
	i = mtd->writesize >> 4;
	p = datbuf;

	while (i--) {
		if (!only_oob)
			printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
			       "  %02x %02x %02x %02x %02x %02x %02x %02x\n",
			       p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
			       p[8], p[9], p[10], p[11], p[12], p[13], p[14],
			       p[15]);
		p += 16;
	}
	puts("OOB:\n");
	i = mtd->oobsize >> 3;
Lei Wen's avatar
Lei Wen committed
348 349
	p = oobbuf;

350 351 352 353 354 355 356 357 358 359
	while (i--) {
		printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
		       p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
		p += 8;
	}
	free(datbuf);
	free(oobbuf);

	return 0;
}
Kyungmin Park's avatar
Kyungmin Park committed
360

361
static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Kyungmin Park's avatar
Kyungmin Park committed
362
{
363 364 365 366
	printf("%s\n", mtd->name);
	return 0;
}

367
static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
368 369 370 371 372 373 374
{
	ulong ofs;

	mtd = &onenand_mtd;
	/* Currently only one OneNAND device is supported */
	printf("\nDevice %d bad blocks:\n", 0);
	for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
Sergey Lapin's avatar
Sergey Lapin committed
375
		if (mtd_block_isbad(mtd, ofs))
376 377 378 379 380 381
			printf("  %08x\n", (u32)ofs);
	}

	return 0;
}

382
static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
383 384 385
{
	char *s;
	int oob = 0;
386
	ulong addr, ofs;
387
	size_t len;
388
	int ret = 0;
389
	size_t retlen = 0;
390

391
	if (argc < 3)
392
		return CMD_RET_USAGE;
393

394 395 396
	s = strchr(argv[0], '.');
	if ((s != NULL) && (!strcmp(s, ".oob")))
		oob = 1;
Kyungmin Park's avatar
Kyungmin Park committed
397

398
	addr = (ulong)simple_strtoul(argv[1], NULL, 16);
Kyungmin Park's avatar
Kyungmin Park committed
399

400
	printf("\nOneNAND read: ");
401
	if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
402
		return 1;
403

404
	ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
405

406
	printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
Kyungmin Park's avatar
Kyungmin Park committed
407

408 409
	return ret == 0 ? 0 : 1;
}
Kyungmin Park's avatar
Kyungmin Park committed
410

411
static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
412 413 414
{
	ulong addr, ofs;
	size_t len;
Lei Wen's avatar
Lei Wen committed
415
	int ret = 0, withoob = 0;
416
	size_t retlen = 0;
Kyungmin Park's avatar
Kyungmin Park committed
417

418
	if (argc < 3)
419
		return CMD_RET_USAGE;
Kyungmin Park's avatar
Kyungmin Park committed
420

Lei Wen's avatar
Lei Wen committed
421 422 423
	if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
		withoob = 1;

424 425 426
	addr = (ulong)simple_strtoul(argv[1], NULL, 16);

	printf("\nOneNAND write: ");
427
	if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
428
		return 1;
Kyungmin Park's avatar
Kyungmin Park committed
429

Lei Wen's avatar
Lei Wen committed
430
	ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
Kyungmin Park's avatar
Kyungmin Park committed
431

432
	printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
433

434 435 436
	return ret == 0 ? 0 : 1;
}

437
static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
{
	ulong ofs;
	int ret = 0;
	size_t len;
	int force;

	/*
	 * Syntax is:
	 *   0       1     2       3    4
	 *   onenand erase [force] [off size]
	 */
	argc--;
	argv++;
	if (argc)
	{
		if (!strcmp("force", argv[0]))
		{
			force = 1;
			argc--;
			argv++;
Kyungmin Park's avatar
Kyungmin Park committed
458
		}
459 460
	}
	printf("\nOneNAND erase: ");
Kyungmin Park's avatar
Kyungmin Park committed
461

462
	/* skip first two or three arguments, look for offset and size */
463
	if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0)
464
		return 1;
465

466
	ret = onenand_block_erase(ofs, len, force);
467

468
	printf("%s\n", ret ? "ERROR" : "OK");
Kyungmin Park's avatar
Kyungmin Park committed
469

470 471
	return ret == 0 ? 0 : 1;
}
Kyungmin Park's avatar
Kyungmin Park committed
472

473
static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
474 475 476 477
{
	ulong ofs;
	int ret = 0;
	size_t len;
Kyungmin Park's avatar
Kyungmin Park committed
478

479 480 481 482 483
	/*
	 * Syntax is:
	 *   0       1     2       3    4
	 *   onenand test [force] [off size]
	 */
Kyungmin Park's avatar
Kyungmin Park committed
484

485
	printf("\nOneNAND test: ");
Kyungmin Park's avatar
Kyungmin Park committed
486

487
	/* skip first two or three arguments, look for offset and size */
488
	if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
489
		return 1;
Kyungmin Park's avatar
Kyungmin Park committed
490

491
	ret = onenand_block_test(ofs, len);
Kyungmin Park's avatar
Kyungmin Park committed
492

493
	printf("%s\n", ret ? "ERROR" : "OK");
Kyungmin Park's avatar
Kyungmin Park committed
494

495 496
	return ret == 0 ? 0 : 1;
}
497

498
static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
499 500 501 502
{
	ulong ofs;
	int ret = 0;
	char *s;
Kyungmin Park's avatar
Kyungmin Park committed
503

504
	if (argc < 2)
505
		return CMD_RET_USAGE;
506 507 508 509 510 511 512 513

	s = strchr(argv[0], '.');
	ofs = (int)simple_strtoul(argv[1], NULL, 16);

	if (s != NULL && strcmp(s, ".oob") == 0)
		ret = onenand_dump(mtd, ofs, 1);
	else
		ret = onenand_dump(mtd, ofs, 0);
Kyungmin Park's avatar
Kyungmin Park committed
514

515 516 517
	return ret == 0 ? 1 : 0;
}

518
static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
519 520 521 522 523 524 525 526
{
	int ret = 0;
	ulong addr;

	argc -= 2;
	argv += 2;

	if (argc <= 0)
527
		return CMD_RET_USAGE;
Kyungmin Park's avatar
Kyungmin Park committed
528

529 530 531
	while (argc > 0) {
		addr = simple_strtoul(*argv, NULL, 16);

Sergey Lapin's avatar
Sergey Lapin committed
532
		if (mtd_block_markbad(mtd, addr)) {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
			printf("block 0x%08lx NOT marked "
				"as bad! ERROR %d\n",
				addr, ret);
			ret = 1;
		} else {
			printf("block 0x%08lx successfully "
				"marked as bad\n",
				addr);
		}
		--argc;
		++argv;
	}
	return ret;
}

static cmd_tbl_t cmd_onenand_sub[] = {
	U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
	U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
	U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
	U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
Lei Wen's avatar
Lei Wen committed
553
	U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
554 555 556 557 558 559
	U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
	U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
	U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
	U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
};

560
#ifdef CONFIG_NEEDS_MANUAL_RELOC
561 562 563 564 565
void onenand_reloc(void) {
	fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
}
#endif

566
static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
567 568 569
{
	cmd_tbl_t *c;

570
	if (argc < 2)
571
		return CMD_RET_USAGE;
572

573 574 575 576 577
	mtd = &onenand_mtd;

	/* Strip off leading 'onenand' command argument */
	argc--;
	argv++;
578

579 580
	c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));

581 582 583
	if (c)
		return c->cmd(cmdtp, flag, argc, argv);
	else
584
		return CMD_RET_USAGE;
Kyungmin Park's avatar
Kyungmin Park committed
585 586 587
}

U_BOOT_CMD(
588
	onenand,	CONFIG_SYS_MAXARGS,	1,	do_onenand,
Peter Tyser's avatar
Peter Tyser committed
589
	"OneNAND sub-system",
590 591 592
	"info - show available OneNAND devices\n"
	"onenand bad - show bad blocks\n"
	"onenand read[.oob] addr off size\n"
Lei Wen's avatar
Lei Wen committed
593
	"onenand write[.yaffs] addr off size\n"
594 595 596 597 598 599
	"    read/write 'size' bytes starting at offset 'off'\n"
	"    to/from memory address 'addr', skipping bad blocks.\n"
	"onenand erase [force] [off size] - erase 'size' bytes from\n"
	"onenand test [off size] - test 'size' bytes from\n"
	"    offset 'off' (entire device if not specified)\n"
	"onenand dump[.oob] off - dump page\n"
Wolfgang Denk's avatar
Wolfgang Denk committed
600
	"onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
Kyungmin Park's avatar
Kyungmin Park committed
601
);