tcl.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/***************************************************************************
 *   Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de>              *
 *   Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de>               *
 *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
 *                                                                         *
 *   Partially based on drivers/mtd/nand_ids.c from Linux.                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
19
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20
 ***************************************************************************/
21

22 23 24 25
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

26
#include "core.h"
27 28
#include "imp.h"
#include "fileio.h"
29
#include <target/target.h>
30

31
/* to be removed */
32 33 34 35 36 37 38
extern struct nand_device *nand_devices;

COMMAND_HANDLER(handle_nand_list_command)
{
	struct nand_device *p;
	int i;

39
	if (!nand_devices) {
40 41 42 43
		command_print(CMD_CTX, "no NAND flash devices configured");
		return ERROR_OK;
	}

44
	for (p = nand_devices, i = 0; p; p = p->next, i++) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		if (p->device)
			command_print(CMD_CTX, "#%i: %s (%s) "
				"pagesize: %i, buswidth: %i,\n\t"
				"blocksize: %i, blocks: %i",
				i, p->device->name, p->manufacturer->name,
				p->page_size, p->bus_width,
				p->erase_size, p->num_blocks);
		else
			command_print(CMD_CTX, "#%i: not probed", i);
	}

	return ERROR_OK;
}

COMMAND_HANDLER(handle_nand_info_command)
{
	int i = 0;
	int j = 0;
	int first = -1;
	int last = -1;

	switch (CMD_ARGC) {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		default:
			return ERROR_COMMAND_SYNTAX_ERROR;
		case 1:
			first = 0;
			last = INT32_MAX;
			break;
		case 2:
			COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], i);
			first = last = i;
			i = 0;
			break;
		case 3:
			COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], first);
			COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], last);
			break;
82 83 84 85 86 87 88
	}

	struct nand_device *p;
	int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
	if (ERROR_OK != retval)
		return retval;

89
	if (NULL == p->device) {
90 91 92 93 94 95 96 97 98 99
		command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
		return ERROR_OK;
	}

	if (first >= p->num_blocks)
		first = p->num_blocks - 1;

	if (last >= p->num_blocks)
		last = p->num_blocks - 1;

100 101 102 103 104 105 106 107
	command_print(CMD_CTX,
		"#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
		i++,
		p->device->name,
		p->manufacturer->name,
		p->page_size,
		p->bus_width,
		p->erase_size);
108

109
	for (j = first; j <= last; j++) {
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		char *erase_state, *bad_state;

		if (p->blocks[j].is_erased == 0)
			erase_state = "not erased";
		else if (p->blocks[j].is_erased == 1)
			erase_state = "erased";
		else
			erase_state = "erase state unknown";

		if (p->blocks[j].is_bad == 0)
			bad_state = "";
		else if (p->blocks[j].is_bad == 1)
			bad_state = " (marked bad)";
		else
			bad_state = " (block condition unknown)";

		command_print(CMD_CTX,
127 128 129 130 131 132
			"\t#%i: 0x%8.8" PRIx32 " (%" PRId32 "kB) %s%s",
			j,
			p->blocks[j].offset,
			p->blocks[j].size / 1024,
			erase_state,
			bad_state);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	}

	return ERROR_OK;
}

COMMAND_HANDLER(handle_nand_probe_command)
{
	if (CMD_ARGC != 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct nand_device *p;
	int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
	if (ERROR_OK != retval)
		return retval;

148 149
	retval = nand_probe(p);
	if (retval == ERROR_OK) {
150
		command_print(CMD_CTX, "NAND flash device '%s (%s)' found",
151
			p->device->name, p->manufacturer->name);
152 153
	}

Øyvind Harboe's avatar
Øyvind Harboe committed
154
	return retval;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

COMMAND_HANDLER(handle_nand_erase_command)
{
	if (CMD_ARGC != 1 && CMD_ARGC != 3)
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct nand_device *p;
	int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
	if (ERROR_OK != retval)
		return retval;

	unsigned long offset;
	unsigned long length;

	/* erase specified part of the chip; or else everything */
	if (CMD_ARGC == 3) {
		unsigned long size = p->erase_size * p->num_blocks;

		COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], offset);
		if ((offset % p->erase_size) != 0 || offset >= size)
176
			return ERROR_COMMAND_SYNTAX_ERROR;
177 178 179

		COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], length);
		if ((length == 0) || (length % p->erase_size) != 0
180
		    || (length + offset) > size)
181
			return ERROR_COMMAND_SYNTAX_ERROR;
182 183 184 185 186 187 188 189 190

		offset /= p->erase_size;
		length /= p->erase_size;
	} else {
		offset = 0;
		length = p->num_blocks;
	}

	retval = nand_erase(p, offset, offset + length - 1);
191
	if (retval == ERROR_OK) {
192
		command_print(CMD_CTX, "erased blocks %lu to %lu "
193 194 195
			"on NAND flash device #%s '%s'",
			offset, offset + length - 1,
			CMD_ARGV[0], p->device->name);
196 197
	}

Øyvind Harboe's avatar
Øyvind Harboe committed
198
	return retval;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

COMMAND_HANDLER(handle_nand_check_bad_blocks_command)
{
	int first = -1;
	int last = -1;

	if ((CMD_ARGC < 1) || (CMD_ARGC > 3) || (CMD_ARGC == 2))
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct nand_device *p;
	int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
	if (ERROR_OK != retval)
		return retval;

214
	if (CMD_ARGC == 3) {
215 216 217 218 219
		unsigned long offset;
		unsigned long length;

		COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], offset);
		if (offset % p->erase_size)
220
			return ERROR_COMMAND_SYNTAX_ERROR;
221 222 223 224
		offset /= p->erase_size;

		COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], length);
		if (length % p->erase_size)
225
			return ERROR_COMMAND_SYNTAX_ERROR;
226 227 228 229 230 231 232 233 234

		length -= 1;
		length /= p->erase_size;

		first = offset;
		last = offset + length;
	}

	retval = nand_build_bbt(p, first, last);
235
	if (retval == ERROR_OK) {
236
		command_print(CMD_CTX, "checked NAND flash device for bad blocks, "
237
			"use \"nand info\" command to list blocks");
238 239
	}

Øyvind Harboe's avatar
Øyvind Harboe committed
240
	return retval;
241 242 243 244 245 246 247 248 249 250 251 252
}

COMMAND_HANDLER(handle_nand_write_command)
{
	struct nand_device *nand = NULL;
	struct nand_fileio_state s;
	int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
			&s, &nand, FILEIO_READ, false, true);
	if (ERROR_OK != retval)
		return retval;

	uint32_t total_bytes = s.size;
253
	while (s.size > 0) {
254
		int bytes_read = nand_fileio_read(nand, &s);
255
		if (bytes_read <= 0) {
256
			command_print(CMD_CTX, "error while reading file");
257 258
			nand_fileio_cleanup(&s);
			return ERROR_FAIL;
259 260 261 262 263
		}
		s.size -= bytes_read;

		retval = nand_write_page(nand, s.address / nand->page_size,
				s.page, s.page_size, s.oob, s.oob_size);
264
		if (ERROR_OK != retval) {
265 266 267
			command_print(CMD_CTX, "failed writing file %s "
				"to NAND flash %s at offset 0x%8.8" PRIx32,
				CMD_ARGV[1], CMD_ARGV[0], s.address);
268 269
			nand_fileio_cleanup(&s);
			return retval;
270 271 272 273
		}
		s.address += s.page_size;
	}

274
	if (nand_fileio_finish(&s) == ERROR_OK) {
275
		command_print(CMD_CTX, "wrote file %s to NAND flash %s up to "
276 277 278
			"offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
			CMD_ARGV[1], CMD_ARGV[0], s.address, duration_elapsed(&s.bench),
			duration_kbps(&s.bench, total_bytes));
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	}
	return ERROR_OK;
}

COMMAND_HANDLER(handle_nand_verify_command)
{
	struct nand_device *nand = NULL;
	struct nand_fileio_state file;
	int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
			&file, &nand, FILEIO_READ, false, true);
	if (ERROR_OK != retval)
		return retval;

	struct nand_fileio_state dev;
	nand_fileio_init(&dev);
	dev.address = file.address;
	dev.size = file.size;
	dev.oob_format = file.oob_format;
	retval = nand_fileio_start(CMD_CTX, nand, NULL, FILEIO_NONE, &dev);
	if (ERROR_OK != retval)
		return retval;

301
	while (file.size > 0) {
302
		retval = nand_read_page(nand, dev.address / dev.page_size,
303
				dev.page, dev.page_size, dev.oob, dev.oob_size);
304
		if (ERROR_OK != retval) {
305 306
			command_print(CMD_CTX, "reading NAND flash page failed");
			nand_fileio_cleanup(&dev);
307 308
			nand_fileio_cleanup(&file);
			return retval;
309 310 311
		}

		int bytes_read = nand_fileio_read(nand, &file);
312
		if (bytes_read <= 0) {
313 314
			command_print(CMD_CTX, "error while reading file");
			nand_fileio_cleanup(&dev);
315 316
			nand_fileio_cleanup(&file);
			return ERROR_FAIL;
317 318 319
		}

		if ((dev.page && memcmp(dev.page, file.page, dev.page_size)) ||
320
				(dev.oob && memcmp(dev.oob, file.oob, dev.oob_size))) {
321
			command_print(CMD_CTX, "NAND flash contents differ "
322
				"at 0x%8.8" PRIx32, dev.address);
323
			nand_fileio_cleanup(&dev);
324 325
			nand_fileio_cleanup(&file);
			return ERROR_FAIL;
326 327 328 329 330 331
		}

		file.size -= bytes_read;
		dev.address += nand->page_size;
	}

332
	if (nand_fileio_finish(&file) == ERROR_OK) {
333
		command_print(CMD_CTX, "verified file %s in NAND flash %s "
334 335 336
			"up to offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
			CMD_ARGV[1], CMD_ARGV[0], dev.address, duration_elapsed(&file.bench),
			duration_kbps(&file.bench, dev.size));
337 338 339 340 341 342 343
	}

	return nand_fileio_cleanup(&dev);
}

COMMAND_HANDLER(handle_nand_dump_command)
{
344
	size_t filesize;
345 346 347 348 349 350 351
	struct nand_device *nand = NULL;
	struct nand_fileio_state s;
	int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
			&s, &nand, FILEIO_WRITE, true, false);
	if (ERROR_OK != retval)
		return retval;

352
	while (s.size > 0) {
353
		size_t size_written;
354
		retval = nand_read_page(nand, s.address / nand->page_size,
355
				s.page, s.page_size, s.oob, s.oob_size);
356
		if (ERROR_OK != retval) {
357
			command_print(CMD_CTX, "reading NAND flash page failed");
358 359
			nand_fileio_cleanup(&s);
			return retval;
360 361 362
		}

		if (NULL != s.page)
363
			fileio_write(s.fileio, s.page_size, s.page, &size_written);
364 365

		if (NULL != s.oob)
366
			fileio_write(s.fileio, s.oob_size, s.oob, &size_written);
367 368 369 370 371

		s.size -= nand->page_size;
		s.address += nand->page_size;
	}

372
	retval = fileio_size(s.fileio, &filesize);
373 374 375
	if (retval != ERROR_OK)
		return retval;

376
	if (nand_fileio_finish(&s) == ERROR_OK) {
377 378
		command_print(CMD_CTX, "dumped %zu bytes in %fs (%0.3f KiB/s)",
			filesize, duration_elapsed(&s.bench),
379
			duration_kbps(&s.bench, filesize));
380 381 382 383 384 385 386 387 388 389 390 391 392 393
	}
	return ERROR_OK;
}

COMMAND_HANDLER(handle_nand_raw_access_command)
{
	if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct nand_device *p;
	int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
	if (ERROR_OK != retval)
		return retval;

394
	if (NULL == p->device) {
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
		command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
		return ERROR_OK;
	}

	if (CMD_ARGC == 2)
		COMMAND_PARSE_ENABLE(CMD_ARGV[1], p->use_raw);

	const char *msg = p->use_raw ? "enabled" : "disabled";
	command_print(CMD_CTX, "raw access is %s", msg);

	return ERROR_OK;
}

static const struct command_registration nand_exec_command_handlers[] = {
	{
		.name = "list",
David Brownell's avatar
David Brownell committed
411
		.handler = handle_nand_list_command,
412 413
		.mode = COMMAND_EXEC,
		.help = "list configured NAND flash devices",
414
		.usage = "",
415 416 417
	},
	{
		.name = "info",
David Brownell's avatar
David Brownell committed
418
		.handler = handle_nand_info_command,
419
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
420 421
		.usage = "[banknum | first_bank_num last_bank_num]",
		.help = "print info about one or more NAND flash devices",
422 423 424
	},
	{
		.name = "probe",
David Brownell's avatar
David Brownell committed
425
		.handler = handle_nand_probe_command,
426
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
427 428
		.usage = "bank_id",
		.help = "identify NAND flash device",
429 430 431
	},
	{
		.name = "check_bad_blocks",
David Brownell's avatar
David Brownell committed
432
		.handler = handle_nand_check_bad_blocks_command,
433
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
434 435
		.usage = "bank_id [offset length]",
		.help = "check all or part of NAND flash device for bad blocks",
436 437 438
	},
	{
		.name = "erase",
David Brownell's avatar
David Brownell committed
439
		.handler = handle_nand_erase_command,
440
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
441 442
		.usage = "bank_id [offset length]",
		.help = "erase all or subset of blocks on NAND flash device",
443 444 445
	},
	{
		.name = "dump",
David Brownell's avatar
David Brownell committed
446
		.handler = handle_nand_dump_command,
447
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
448 449
		.usage = "bank_id filename offset length "
			"['oob_raw'|'oob_only']",
450 451 452 453
		.help = "dump from NAND flash device",
	},
	{
		.name = "verify",
David Brownell's avatar
David Brownell committed
454
		.handler = handle_nand_verify_command,
455
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
456 457
		.usage = "bank_id filename offset "
			"['oob_raw'|'oob_only'|'oob_softecc'|'oob_softecc_kw']",
458 459 460 461
		.help = "verify NAND flash device",
	},
	{
		.name = "write",
David Brownell's avatar
David Brownell committed
462
		.handler = handle_nand_write_command,
463
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
464 465
		.usage = "bank_id filename offset "
			"['oob_raw'|'oob_only'|'oob_softecc'|'oob_softecc_kw']",
466 467 468 469
		.help = "write to NAND flash device",
	},
	{
		.name = "raw_access",
David Brownell's avatar
David Brownell committed
470
		.handler = handle_nand_raw_access_command,
471
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
472
		.usage = "bank_id ['enable'|'disable']",
473 474 475 476 477
		.help = "raw access to NAND flash device",
	},
	COMMAND_REGISTRATION_DONE
};

478
static int nand_init(struct command_context *cmd_ctx)
479 480 481 482 483 484 485 486 487 488 489 490
{
	if (!nand_devices)
		return ERROR_OK;
	struct command *parent = command_find_in_context(cmd_ctx, "nand");
	return register_commands(cmd_ctx, parent, nand_exec_command_handlers);
}

COMMAND_HANDLER(handle_nand_init_command)
{
	if (CMD_ARGC != 0)
		return ERROR_COMMAND_SYNTAX_ERROR;

491 492
	static bool nand_initialized;
	if (nand_initialized) {
493 494 495 496 497 498 499 500
		LOG_INFO("'nand init' has already been called");
		return ERROR_OK;
	}
	nand_initialized = true;

	LOG_DEBUG("Initializing NAND devices...");
	return nand_init(CMD_CTX);
}
501 502

static int nand_list_walker(struct nand_flash_controller *c, void *x)
503
{
Andreas Fritiofson's avatar
Andreas Fritiofson committed
504
	struct command_context *cmd_ctx = x;
505 506 507
	command_print(cmd_ctx, "  %s", c->name);
	return ERROR_OK;
}
508

509 510 511 512 513 514 515
COMMAND_HANDLER(handle_nand_list_drivers)
{
	command_print(CMD_CTX, "Available NAND flash controller drivers:");
	return nand_driver_walk(&nand_list_walker, CMD_CTX);
}

static COMMAND_HELPER(create_nand_device, const char *bank_name,
516
	struct nand_flash_controller *controller)
517
{
518 519 520 521 522
	struct nand_device *c;
	struct target *target;
	int retval;

	if (CMD_ARGC < 2)
523
		return ERROR_COMMAND_SYNTAX_ERROR;
524 525 526 527 528 529
	target = get_target(CMD_ARGV[1]);
	if (!target) {
		LOG_ERROR("invalid target %s", CMD_ARGV[1]);
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}

530
	if (NULL != controller->commands) {
531
		retval = register_commands(CMD_CTX, NULL,
532 533 534 535
				controller->commands);
		if (ERROR_OK != retval)
			return retval;
	}
536
	c = malloc(sizeof(struct nand_device));
537
	if (c == NULL) {
538 539 540
		LOG_ERROR("End of memory");
		return ERROR_FAIL;
	}
541 542

	c->name = strdup(bank_name);
543
	c->target = target;
544 545 546 547 548 549 550
	c->controller = controller;
	c->controller_priv = NULL;
	c->manufacturer = NULL;
	c->device = NULL;
	c->bus_width = 0;
	c->address_cycles = 0;
	c->page_size = 0;
Ivo Manca's avatar
Ivo Manca committed
551
	c->use_raw = false;
552 553
	c->next = NULL;

554
	retval = CALL_COMMAND_HANDLER(controller->nand_device_command, c);
555
	if (ERROR_OK != retval) {
556 557 558
		LOG_ERROR("'%s' driver rejected nand flash. Usage: %s",
			controller->name,
			controller->usage);
559
		free(c);
560
		return retval;
561 562
	}

563 564 565
	if (controller->usage == NULL)
		LOG_DEBUG("'%s' driver usage field missing", controller->name);

566 567
	nand_device_add(c);

568 569 570 571 572
	return ERROR_OK;
}

COMMAND_HANDLER(handle_nand_device_command)
{
573
	if (CMD_ARGC < 2)
574
		return ERROR_COMMAND_SYNTAX_ERROR;
575

576
	/* save name and increment (for compatibility) with drivers */
577 578 579 580 581 582
	const char *bank_name = *CMD_ARGV++;
	CMD_ARGC--;

	const char *driver_name = CMD_ARGV[0];
	struct nand_flash_controller *controller;
	controller = nand_driver_find_by_name(CMD_ARGV[0]);
583
	if (NULL == controller) {
584 585 586 587 588 589 590 591 592 593 594 595
		LOG_ERROR("No valid NAND flash driver found (%s)", driver_name);
		return CALL_COMMAND_HANDLER(handle_nand_list_drivers);
	}
	return CALL_COMMAND_HANDLER(create_nand_device, bank_name, controller);
}

static const struct command_registration nand_config_command_handlers[] = {
	{
		.name = "device",
		.handler = &handle_nand_device_command,
		.mode = COMMAND_CONFIG,
		.help = "defines a new NAND bank",
David Brownell's avatar
David Brownell committed
596
		.usage = "bank_id driver target [driver_options ...]",
597 598 599 600 601 602
	},
	{
		.name = "drivers",
		.handler = &handle_nand_list_drivers,
		.mode = COMMAND_ANY,
		.help = "lists available NAND drivers",
Spencer Oliver's avatar
Spencer Oliver committed
603
		.usage = ""
604 605 606 607 608 609
	},
	{
		.name = "init",
		.mode = COMMAND_CONFIG,
		.handler = &handle_nand_init_command,
		.help = "initialize NAND devices",
Spencer Oliver's avatar
Spencer Oliver committed
610
		.usage = ""
611 612 613
	},
	COMMAND_REGISTRATION_DONE
};
614

615 616 617 618 619
static const struct command_registration nand_command_handlers[] = {
	{
		.name = "nand",
		.mode = COMMAND_ANY,
		.help = "NAND flash command group",
Spencer Oliver's avatar
Spencer Oliver committed
620
		.usage = "",
621 622 623 624 625 626 627 628 629
		.chain = nand_config_command_handlers,
	},
	COMMAND_REGISTRATION_DONE
};

int nand_register_commands(struct command_context *cmd_ctx)
{
	return register_commands(cmd_ctx, NULL, nand_command_handlers);
}