view loadtools/flashops.c @ 405:a212b4968b29

fc-loadtool flash: another refactoring: geometry vs. command set
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 17 Jun 2014 00:33:05 +0000
parents
children f2cc551e597f
line wrap: on
line source

/*
 * This module implements those flash operations which are dependent
 * on the AMD vs. Intel command set style.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include "flash.h"

/* common stub functions */

static
noop()
{
	return(0);
}

static
invalid()
{
	fprintf(stderr,
	      "This operation is not applicable to the selected flash type\n");
	return(-1);
}

/* AMD flash functions */

amd_reset_cmd(bi)
	struct flash_bank_info *bi;
{
	if (do_w16(bi->base_addr + 0xAAA, 0xF0)) {
		fprintf(stderr,
	"unexpected response to w16 when resetting flash to read mode!\n");
		return(-1);
	}
	return(0);
}

amd_sector_erase(bi, sp)
	struct flash_bank_info *bi;
	struct sector_info *sp;
{
	int stat;
	uint16_t flstat;
	time_t start_time, curtime;

	stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0xAA);
	if (stat) {
bad_w16:	fprintf(stderr,
	"unexpected response to w16 in erase cmd sequence - aborting\n");
		return(-1);
	}
	stat = do_w16(bi->base_addr + sp->start + 0x554, 0x55);
	if (stat)
		goto bad_w16;
	stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0x80);
	if (stat)
		goto bad_w16;
	stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0xAA);
	if (stat)
		goto bad_w16;
	stat = do_w16(bi->base_addr + sp->start + 0x554, 0x55);
	if (stat)
		goto bad_w16;
	stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0x30);
	if (stat)
		goto bad_w16;
	start_time = time(0);
	for (;;) {
		stat = do_r16(bi->base_addr + sp->start, &flstat);
		if (stat)
			return(stat);	/* error msg already printed */
		if (flstat == 0xFFFF)
			return(0);
		curtime = time(0);
		if (curtime >= start_time + 20) {
			fprintf(stderr, "erase timeout, aborting\n");
			return(-1);
		}
	}
}

struct flash_cmdset flash_cmdset_amd = {
	.cmdset_name		= "AMD",
	.reset_cmd		= amd_reset_cmd,
	.status_cmd		= invalid,
	.unlock_sector		= invalid,
	.erase_sector		= amd_sector_erase,
	.prep_for_program	= noop,
	.loadagent_setbase_cmd	= "AMFB",
	.loadagent_program_cmd	= "AMFW",
};