/*
 *               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 aes unit;
 */
void
aes_init(u32 cpu, test_thread_t *thp)
{
	// sim zeros msg area;

	thp->msg = (test_msg_u *)MEM_AES;

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

	IO_WRITE(PWR_CTRL_REG, PWR_CTRL_ASSERT_RST << PWR_CTRL_AES_SHIFT);
	IO_WRITE(PWR_CTRL_REG, PWR_CTRL_CLOCK_ON << PWR_CTRL_AES_SHIFT);
	IO_WRITE(PWR_CTRL_REG, PWR_CTRL_DEASSERT_RST << PWR_CTRL_AES_SHIFT);
	IO_WRITE(AES_CTRL_REG, 0);
	IO_WRITE(AES_ID_REG, ID_AES);

	thp->state = TT_REQ;
}

/*
 * request aes work from sim;
 */
void
aes_req(u32 cpu, test_thread_t *thp)
{
	aes_msg_t *msg = &(thp->msg)->aes;

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

/*
 * poll for sim reply;
 */
void
aes_reply(u32 cpu, test_thread_t *thp)
{
	aes_msg_t *msg = &(thp->msg)->aes;

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

/*
 * start aes operation;
 */
void
aes_start(u32 cpu, test_thread_t *thp)
{
	aes_msg_t *msg = &(thp->msg)->aes;

	IO_WRITE(AES_KEY_REG, msg->key[0]);
	IO_WRITE(AES_KEY_REG, msg->key[1]);
	IO_WRITE(AES_KEY_REG, msg->key[2]);
	IO_WRITE(AES_KEY_REG, msg->key[3]);
	IO_WRITE(AES_IVPTR_REG, msg->ivptr);
	IO_WRITE(AES_RPTR_REG, msg->rptr);
	IO_WRITE(AES_WPTR_REG, msg->wptr);
	IO_WRITE(AES_CTRL_REG, msg->ctrl);
	thp->state = TT_WAIT;
}

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

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

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

