/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <types.h>
#include <sc/sc.h>
#include "ft.h"

/*
 * initialize flash unit;
 */
void
fla_init(u32 cpu, test_thread_t *thp)
{
	// sim zeros msg area;

	thp->msg = (test_msg_u *)MEM_FLA;

	// turn on flash clock, reset;
	// reset flash engine;
	// program device id;

	IO_WRITE(PWR_CTRL_REG, PWR_CTRL_ASSERT_RST << PWR_CTRL_FLASH_SHIFT);
	IO_WRITE(PWR_CTRL_REG, PWR_CTRL_CLOCK_ON << PWR_CTRL_FLASH_SHIFT);
	IO_WRITE(PWR_CTRL_REG, PWR_CTRL_DEASSERT_RST << PWR_CTRL_FLASH_SHIFT);
	IO_WRITE(FL_CTRL_REG, 0);
	IO_WRITE(FL_ID_REG, ID_FLA);

	thp->state = TT_REQ;
}

/*
 * request flash work from sim;
 */
void
fla_req(u32 cpu, test_thread_t *thp)
{
	fla_msg_t *msg = &(thp->msg)->fla;

	msg->req = MSG_REQ | thp->cpu;
	msg_wbi(msg, sizeof(fla_msg_t));
	thp->state = TT_REPLY;
}

/*
 * poll for sim reply;
 */
void
fla_reply(u32 cpu, test_thread_t *thp)
{
	fla_msg_t *msg = &(thp->msg)->fla;

	msg_wbi(msg, sizeof(fla_msg_t));
	if(msg->req == MSG_NONE)
		thp->state = TT_START;		// got reply;
}

/*
 * start flash operation;
 */
void
fla_start(u32 cpu, test_thread_t *thp)
{
	fla_msg_t *msg = &(thp->msg)->fla;

	IO_WRITE(FL_CONF_REG, msg->conf);
	IO_WRITE(FL_AD0_REG, msg->ad[0]);
	IO_WRITE(FL_AD1_REG, msg->ad[1]);
	IO_WRITE(FL_MA0_REG, msg->ma[0]);
	IO_WRITE(FL_MA1_REG, msg->ma[1]);
	IO_WRITE(FL_CTRL_REG, msg->ctrl);
	thp->state = TT_WAIT;
}

/*
 * wait for flash operation to finish;
 */
void
fla_wait(u32 cpu, test_thread_t *thp)
{
	u32 ctrl;

	ctrl = IO_READ(FL_CTRL_REG);
	if(ctrl & FL_CTRL_EXEC) {
		//XXX timeout;
	} else
		thp->state = TT_POST;
}

/*
 * flash post-op;
 */
void
fla_post(u32 cpu, test_thread_t *thp)
{
	thp->state = TT_REQ;
}

