/*
 * (C) Copyright 2001
 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */

/* NOT Used yet...
  add following code to PIP405.c :
int testdram (void)
{
	unsigned char s[32];
	int i;

	i = getenv_r ("testmem", s, 32);
	if (i != 0) {
		i = (int) simple_strtoul (s, NULL, 10);
		if ((i > 0) && (i < 0xf)) {
			printf ("testing ");
			i = mem_test (0, ramsize, i);
			if (i > 0)
				printf ("ERROR ");
			else
				printf ("Ok ");
		}
	}
	return (1);
}
*/


#include <common.h>
#include <asm/processor.h>
#include <4xx_i2c.h>

DECLARE_GLOBAL_DATA_PTR;

#define FALSE           0
#define TRUE            1

#define TEST_QUIET 	8
#define TEST_SHOW_PROG 	4
#define TEST_SHOW_ERR 	2
#define TEST_SHOW_ALL	1

#define TESTPAT1 0xAA55AA55
#define TESTPAT2 0x55AA55AA
#define TEST_PASSED 0
#define TEST_FAILED 1
#define MEGABYTE (1024*1024)


typedef struct {
	volatile unsigned long pat1;
	volatile unsigned long pat2;
} RAM_MEMTEST_PATTERN2;

typedef struct {
	volatile unsigned long addr;
} RAM_MEMTEST_ADDRLINE;

static __inline unsigned long Swap_32 (unsigned long val)
{
	return (((val << 16) & 0xFFFF0000) | ((val >> 16) & 0x0000FFFF));
}

void testm_puts (int quiet, char *buf)
{
	if ((quiet & TEST_SHOW_ALL) == TEST_SHOW_ALL)
		puts (buf);
}


void Write_Error (int mode, unsigned long addr, unsigned long expected,
				  unsigned long actual)
{

	char dispbuf[64];

	sprintf (dispbuf, "\n ERROR @ 0x%08lX: (exp: 0x%08lX act: 0x%08lX) ",
			 addr, expected, actual);
	testm_puts (((mode & TEST_SHOW_ERR) ==
				 TEST_SHOW_ERR) ? TEST_SHOW_ALL : mode, dispbuf);
}


/*
 * fills the memblock of <size> bytes from <startaddr> with pat1 and pat2
 */


void RAM_MemTest_WritePattern2 (unsigned long startaddr,
								unsigned long size, unsigned long pat1,
								unsigned long pat2)
{
	RAM_MEMTEST_PATTERN2 *p, *pe;

	p = (RAM_MEMTEST_PATTERN2 *) startaddr;
	pe = (RAM_MEMTEST_PATTERN2 *) (startaddr + size);

	while (p < pe) {
		p->pat1 = pat1;
		p->pat2 = pat2;
		p++;
	}							/* endwhile */
}

/*
 * checks the memblock of <size> bytes from <startaddr> with pat1 and pat2
 * returns the address of the first error or NULL if all is well
 */

void *RAM_MemTest_CheckPattern2 (int mode, unsigned long startaddr,
								 unsigned long size, unsigned long pat1,
								 unsigned long pat2)
{
	RAM_MEMTEST_PATTERN2 *p, *pe;
	unsigned long actual1, actual2;

	p = (RAM_MEMTEST_PATTERN2 *) startaddr;
	pe = (RAM_MEMTEST_PATTERN2 *) (startaddr + size);

	while (p < pe) {
		actual1 = p->pat1;
		actual2 = p->pat2;

		if (actual1 != pat1) {
			Write_Error (mode, (unsigned long) &(p->pat1), pat1, actual1);
			return ((void *) &(p->pat1));
		}
		/* endif */
		if (actual2 != pat2) {
			Write_Error (mode, (unsigned long) &(p->pat2), pat2, actual2);
			return ((void *) &(p->pat2));
		}
		/* endif */
		p++;
	}							/* endwhile */

	return (NULL);
}

/*
 * fills the memblock of <size> bytes from <startaddr> with the address
 */

void RAM_MemTest_WriteAddrLine (unsigned long startaddr,
								unsigned long size, int swapped)
{
	RAM_MEMTEST_ADDRLINE *p, *pe;

	p = (RAM_MEMTEST_ADDRLINE *) startaddr;
	pe = (RAM_MEMTEST_ADDRLINE *) (startaddr + size);

	if (!swapped) {
		while (p < pe) {
			p->addr = (unsigned long) p;
			p++;
		}						/* endwhile */
	} else {
		while (p < pe) {
			p->addr = Swap_32 ((unsigned long) p);
			p++;
		}						/* endwhile */
	}							/* endif */
}

/*
 * checks the memblock of <size> bytes from <startaddr>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckAddrLine (int mode, unsigned long startaddr,
								 unsigned long size, int swapped)
{
	RAM_MEMTEST_ADDRLINE *p, *pe;
	unsigned long actual, expected;

	p = (RAM_MEMTEST_ADDRLINE *) startaddr;
	pe = (RAM_MEMTEST_ADDRLINE *) (startaddr + size);

	if (!swapped) {
		while (p < pe) {
			actual = p->addr;
			expected = (unsigned long) p;
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p++;
		}						/* endwhile */
	} else {
		while (p < pe) {
			actual = p->addr;
			expected = Swap_32 ((unsigned long) p);
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p++;
		}						/* endwhile */
	}							/* endif */

	return (NULL);
}

/*
 * checks the memblock of <size> bytes from <startaddr+size>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckAddrLineReverse (int mode, unsigned long startaddr,
										unsigned long size, int swapped)
{
	RAM_MEMTEST_ADDRLINE *p, *pe;
	unsigned long actual, expected;

	p = (RAM_MEMTEST_ADDRLINE *) (startaddr + size - sizeof (p->addr));
	pe = (RAM_MEMTEST_ADDRLINE *) startaddr;

	if (!swapped) {
		while (p > pe) {
			actual = p->addr;
			expected = (unsigned long) p;
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p--;
		}						/* endwhile */
	} else {
		while (p > pe) {
			actual = p->addr;
			expected = Swap_32 ((unsigned long) p);
			if (actual != expected) {
				Write_Error (mode, (unsigned long) &(p->addr), expected,
							 actual);
				return ((void *) &(p->addr));
			}					/* endif */
			p--;
		}						/* endwhile */
	}							/* endif */

	return (NULL);
}

/*
 * fills the memblock of <size> bytes from <startaddr> with walking bit pattern
 */

void RAM_MemTest_WriteWalkBit (unsigned long startaddr, unsigned long size)
{
	volatile unsigned long *p, *pe;
	unsigned long i;

	p = (unsigned long *) startaddr;
	pe = (unsigned long *) (startaddr + size);
	i = 0;

	while (p < pe) {
		*p = 1UL << i;
		i = (i + 1 + (((unsigned long) p) >> 7)) % 32;
		p++;
	}							/* endwhile */
}

/*
 * checks the memblock of <size> bytes from <startaddr>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckWalkBit (int mode, unsigned long startaddr,
								unsigned long size)
{
	volatile unsigned long *p, *pe;
	unsigned long actual, expected;
	unsigned long i;

	p = (unsigned long *) startaddr;
	pe = (unsigned long *) (startaddr + size);
	i = 0;

	while (p < pe) {
		actual = *p;
		expected = (1UL << i);
		if (actual != expected) {
			Write_Error (mode, (unsigned long) p, expected, actual);
			return ((void *) p);
		}						/* endif */
		i = (i + 1 + (((unsigned long) p) >> 7)) % 32;
		p++;
	}							/* endwhile */

	return (NULL);
}

/*
 * fills the memblock of <size> bytes from <startaddr> with "random" pattern
 */

void RAM_MemTest_WriteRandomPattern (unsigned long startaddr,
									 unsigned long size,
									 unsigned long *pat)
{
	unsigned long i, p;

	p = *pat;

	for (i = 0; i < (size / 4); i++) {
		*(unsigned long *) (startaddr + i * 4) = p;
		if ((p % 2) > 0) {
			p ^= i;
			p >>= 1;
			p |= 0x80000000;
		} else {
			p ^= ~i;
			p >>= 1;
		}						/* endif */
	}							/* endfor */
	*pat = p;
}

/*
 * checks the memblock of <size> bytes from <startaddr>
 * returns the address of the error or NULL if all is well
 */

void *RAM_MemTest_CheckRandomPattern (int mode, unsigned long startaddr,
									  unsigned long size,
									  unsigned long *pat)
{
	void *perr = NULL;
	unsigned long i, p, p1;

	p = *pat;

	for (i = 0; i < (size / 4); i++) {
		p1 = *(unsigned long *) (startaddr + i * 4);
		if (p1 != p) {
			if (perr == NULL) {
				Write_Error (mode, startaddr + i * 4, p, p1);
				perr = (void *) (startaddr + i * 4);
			}					/* endif */
		}
		/* endif */
		if ((p % 2) > 0) {
			p ^= i;
			p >>= 1;
			p |= 0x80000000;
		} else {
			p ^= ~i;
			p >>= 1;
		}						/* endif */
	}							/* endfor */

	*pat = p;
	return (perr);
}


void RAM_MemTest_WriteData1 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WritePattern2 (startaddr, size, TESTPAT1, TESTPAT2);
}

void *RAM_MemTest_CheckData1 (int mode, unsigned long startaddr,
							  unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckPattern2
			(mode, startaddr, size, TESTPAT1, TESTPAT2));
}

void RAM_MemTest_WriteData2 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WritePattern2 (startaddr, size, TESTPAT2, TESTPAT1);
}

void *RAM_MemTest_CheckData2 (int mode, unsigned long startaddr,
							  unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckPattern2
			(mode, startaddr, size, TESTPAT2, TESTPAT1));
}

void RAM_MemTest_WriteAddr1 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WriteAddrLine (startaddr, size, FALSE);
}

void *RAM_MemTest_Check1Addr1 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLine (mode, startaddr, size, FALSE));
}

void *RAM_MemTest_Check2Addr1 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLineReverse
			(mode, startaddr, size, FALSE));
}

void RAM_MemTest_WriteAddr2 (unsigned long startaddr, unsigned long size,
							 unsigned long *pat)
{
	RAM_MemTest_WriteAddrLine (startaddr, size, TRUE);
}

void *RAM_MemTest_Check1Addr2 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLine (mode, startaddr, size, TRUE));
}

void *RAM_MemTest_Check2Addr2 (int mode, unsigned long startaddr,
							   unsigned long size, unsigned long *pat)
{
	return (RAM_MemTest_CheckAddrLineReverse
			(mode, startaddr, size, TRUE));
}


typedef struct {
	void (*test_write) (unsigned long startaddr, unsigned long size,
						unsigned long *pat);
	char *test_write_desc;
	void *(*test_check1) (int mode, unsigned long startaddr,
						  unsigned long size, unsigned long *pat);
	void *(*test_check2) (int mode, unsigned long startaddr,
						  unsigned long size, unsigned long *pat);
} RAM_MEMTEST_FUNC;


#define TEST_STAGES 5
static RAM_MEMTEST_FUNC test_stage[TEST_STAGES] = {
	{RAM_MemTest_WriteData1, "data test 1...\n", RAM_MemTest_CheckData1,
	 NULL},
	{RAM_MemTest_WriteData2, "data test 2...\n", RAM_MemTest_CheckData2,
	 NULL},
	{RAM_MemTest_WriteAddr1, "address line test...\n",
	 RAM_MemTest_Check1Addr1, RAM_MemTest_Check2Addr1},
	{RAM_MemTest_WriteAddr2, "address line test (swapped)...\n",
	 RAM_MemTest_Check1Addr2, RAM_MemTest_Check2Addr2},
	{RAM_MemTest_WriteRandomPattern, "random data test...\n",
	 RAM_MemTest_CheckRandomPattern, NULL}
};

void mem_test_reloc(void)
{
	unsigned long addr;
	int i;
	for (i=0; i< TEST_STAGES; i++) {
 		addr = (ulong) (test_stage[i].test_write) + gd->reloc_off;
		test_stage[i].test_write=
			(void (*) (unsigned long startaddr, unsigned long size,
						unsigned long *pat))addr;
 		addr = (ulong) (test_stage[i].test_write_desc) + gd->reloc_off;
		test_stage[i].test_write_desc=(char *)addr;
 		if(test_stage[i].test_check1) {
			addr = (ulong) (test_stage[i].test_check1) + gd->reloc_off;
			test_stage[i].test_check1=
				(void *(*) (int mode, unsigned long startaddr,
				 unsigned long size, unsigned long *pat))addr;
		}
 		if(test_stage[i].test_check2) {
			addr = (ulong) (test_stage[i].test_check2) + gd->reloc_off;
			test_stage[i].test_check2=
				(void *(*) (int mode, unsigned long startaddr,
				 unsigned long size, unsigned long *pat))addr;
		}
	}
}


int mem_test (unsigned long start, unsigned long ramsize, int quiet)
{
	unsigned long errors, stage;
	unsigned long startaddr, size, i;
	const unsigned long blocksize = 0x80000;	/* check in 512KB blocks */
	unsigned long *perr;
	unsigned long rdatapat;
	char dispbuf[80];
	int status = TEST_PASSED;
	int prog = 0;

	errors = 0;
	startaddr = start;
	size = ramsize;
	if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
		prog++;
		printf (".");
	}
	sprintf (dispbuf, "\nMemory Test: addr = 0x%lx size = 0x%lx\n",
			 startaddr, size);
	testm_puts (quiet, dispbuf);
	for (stage = 0; stage < TEST_STAGES; stage++) {
		sprintf (dispbuf, test_stage[stage].test_write_desc);
		testm_puts (quiet, dispbuf);
		/* fill SDRAM */
		rdatapat = 0x12345678;
		sprintf (dispbuf, "writing block:     ");
		testm_puts (quiet, dispbuf);
		for (i = 0; i < size; i += blocksize) {
			sprintf (dispbuf, "%04lX\b\b\b\b", i / blocksize);
			testm_puts (quiet, dispbuf);
			test_stage[stage].test_write (startaddr + i, blocksize,
										  &rdatapat);
		}						/* endfor */
		sprintf (dispbuf, "\n");
		testm_puts (quiet, dispbuf);
		if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
			prog++;
			printf (".");
		}
		/* check SDRAM */
		rdatapat = 0x12345678;
		sprintf (dispbuf, "checking block:     ");
		testm_puts (quiet, dispbuf);
		for (i = 0; i < size; i += blocksize) {
			sprintf (dispbuf, "%04lX\b\b\b\b", i / blocksize);
			testm_puts (quiet, dispbuf);
			if ((perr =
				 test_stage[stage].test_check1 (quiet, startaddr + i,
												blocksize,
												&rdatapat)) != NULL) {
				status = TEST_FAILED;
			}					/* endif */
		}						/* endfor */
		sprintf (dispbuf, "\n");
		testm_puts (quiet, dispbuf);
		if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
			prog++;
			printf (".");
		}
		if (test_stage[stage].test_check2 != NULL) {
			/* check2 SDRAM */
			sprintf (dispbuf, "2nd checking block:     ");
			rdatapat = 0x12345678;
			testm_puts (quiet, dispbuf);
			for (i = 0; i < size; i += blocksize) {
				sprintf (dispbuf, "%04lX\b\b\b\b", i / blocksize);
				testm_puts (quiet, dispbuf);
				if ((perr =
					 test_stage[stage].test_check2 (quiet, startaddr + i,
													blocksize,
													&rdatapat)) != NULL) {
					status = TEST_FAILED;
				}				/* endif */
			}					/* endfor */
			sprintf (dispbuf, "\n");
			testm_puts (quiet, dispbuf);
			if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
				prog++;
				printf (".");
			}
		}

	}							/* next stage */
	if ((quiet & TEST_SHOW_PROG) == TEST_SHOW_PROG) {
		while (prog-- > 0)
			printf ("\b \b");
	}

	if (status == TEST_FAILED)
		errors++;

	return (errors);
}
