view target-utils/libload/intelflash.c @ 409:23ab8fe81764

Intel flash: unlock command implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 17 Jun 2014 03:18:02 +0000
parents 7daea2476062
children d7f409493eb6
line wrap: on
line source

/*
 * This module implements the INFB and INFW commands for programming
 * Intel-style flash memories.  The syntax and operation are exactly
 * the same as the AMD flash counterparts AMFB and AMFW.
 */

#include <sys/types.h>
#include "types.h"

static u32 base_addr;

void
cmd_INFB(argbulk)
	char *argbulk;
{
	char *argv[2];
	u_long addr;

	if (parse_args(argbulk, 1, 1, argv, 0) < 0)
		return;
	if (parse_hexarg(argv[0], 8, &addr) < 0) {
		printf("ERROR: argument must be a valid 32-bit hex address\n");
		return;
	}
	if (addr & 1) {
		printf("ERROR: odd address\n");
		return;
	}
	base_addr = addr;
}

void
cmd_INFW(argbulk)
	char *argbulk;
{
	char *argv[3], *s;
	u_long offset;
	volatile u16 *flashptr;
	u32 datum;	/* needs to be u32 for decode_hex_digits() */
	u16 stat;
	int i;

	if (parse_args(argbulk, 2, 2, argv, 0) < 0)
		return;
	if (parse_hexarg(argv[0], 8, &offset) < 0) {
	    printf("ERROR: offset argument must a valid 32-bit hex value\n");
		return;
	}
	if (offset & 1) {
		printf("ERROR: odd offset argument\n");
		return;
	}
	flashptr = (volatile u16 *)(base_addr + offset);
	for (s = argv[1]; *s; flashptr++, s += 4) {
		if (decode_hex_digits(s, 4, &datum) < 0) {
			printf("ERROR: bad INFW hex string argument\n");
			return;
		}
		*flashptr = 0x40;
		*flashptr = datum;
		for (i = 10000; i; i--) {
			stat = *flashptr;
			if (stat & 0x80)
				break;
		}
		if (!i) {
			printf("ERROR: flash write timeout at %08X\n",
				(u_long) flashptr);
			return;
		}
		if (stat & 0x10) {
			printf("ERROR: program operation failed at %08X\n",
				(u_long) flashptr);
			return;
		}
	}
}