/*
 *               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 <sc/addr_map.h>
#include <ioslibc.h>
#include "test.h"

/*
 * set dax access rights;
 * This function sets the access rights to 'acc' for the region of memory
 * starting at 'ptr' with a length of 'size' bytes.  'ptr' is assumed to
 * be cache-line aligned, and size is an even multiple of the cache-line
 * size.
 */
void
set_dax(u32 acc, u32 ptr, int size)
{
	acc = acc << PT_SC_PROT_SHIFT;
	ptr += DAX_START;
	while(size > 0) {
		IO_WRITE(ptr, acc);
		ptr += 16;
		size -= 16;
	}
}

/*
 * set pt access rights;
 */
void
set_pt(u32 acc, u32 ptr, int size)
{
	acc <<= PT_ENTRY_SHIFT;
	ptr = (ptr & 0x01fffff0) | PT_START;
	while(size > 0) {
		IO_WRITE(ptr, acc);
		ptr += 16*1024;
		size -= 16*1024;
	}
}

/*
 * invalidate l1 d cache;
 */
void
inv_l1d()
{
	u32 *addr, *last;

	addr = (u32 *)L1_DI_START;
	last = (u32 *)(L1_DI_START + 4096);
	for(; addr < last; addr += 4)
		*addr = 0;
	wb_flush();
}

/*
 * update name with pass/fail flag;
 */
void
passfail(test_t *tp)
{
	tp->name[15] = tp->fail? 'f' : 'p';
}

/*
 * mark test failed;
 */
u32
test_fail(test_t *tp)
{
	u32 fail;

	fail = tp->fail;
	tp->fail = fail | (1 << tp->state);
	return(fail);
}

/*
 * update test name;
 */
void
test_name(test_t *tp, char *name)
{
	int n;

	if( !tp->name[0]) {
		for(n = 0; n < 16; n++)
			tp->name[n] = name[n];
	}
}

/*
 * loop over list of tests;
 * returns 0 if any test failed;
 */
u32
test_loop(test_t *tp, u32 nth)
{
	u32 fail;
	void (*func)(test_t *, u32);

	// run through list of tests;

	for(fail = 0; (func = tp->func); tp++) {
		tp->state = 0;
		func(tp, nth);
		passfail(tp);
		fail |= tp->fail;
	}
	return(fail? 0 : 1);
}

/*
 * sdram config;
 */
#define	SD_RFSH	(6 << MC_CTRL_RFINT_SHIFT)		// refresh interval;

/*
 * write to sdram ctrl and wait;
 */
void
sd_ctrl(u32 tap, u32 ctrl, int to)
{
	ctrl |= (tap << MC_CTRL_TAP_SHIFT);
	IO_WRITE(MC_CTRL_REG, ctrl);
	while(--to > 0);
}

/*
 * initialize SDRAM;
 * start with fastest refresh period, every 128 clocks;
 * must wait for 2 refreshes to go to sdram;
 * when done with init, set refresh to real interval;
 */
void
sd_init(u32 tap)
{
	u32 ctrl;

	ctrl = MC_CTRL_ENA | MC_CTRL_PALL | 0x400;	// pall
	sd_ctrl(tap, ctrl, 150);
	ctrl = MC_CTRL_ENA;							// 2 refresh;
	sd_ctrl(tap, ctrl, 150*2);
	ctrl = MC_CTRL_ENA | MC_CTRL_MRS | 0x2a;	// mrs;
	sd_ctrl(tap, ctrl, 150);
	ctrl = MC_CTRL_ENA | MC_CTRL_MRS | 0x400;	// emrs;
	//XXX sd_ctrl(tap, ctrl, 150);
	ctrl = MC_CTRL_ENA;							// 2 refresh;
	sd_ctrl(tap, ctrl, 150*2);
	ctrl = MC_CTRL_ENA | MC_CTRL_DQM_ENA;		// enable DQMs;
	sd_ctrl(tap, ctrl, 150);
	ctrl |= SD_RFSH;							// set real refresh;
	sd_ctrl(tap, ctrl, 0);
}

