/*
 *               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 <iop.h>
#include <iostypes.h>
#include <ioslibc.h>
#include <sys.h>
#include <ahb_fdl_defs.h>
#include "ee.h"

#define STACK_SIZE                  1024
const u8        _initStack[STACK_SIZE];
const u32       _initStackSize = sizeof(_initStack);
const u32       _initPriority = 127;

#define EEPROM_SZ	64	// words


// Do Basic tests.
u32 eeBasicTest(u32 Array_Depth)
{
    u32 i;
    u32 rdval;

    printf("**** eeBasicTest: Starting basic test of all ee commands.\n");

    // Test Write enable, WRITE cmd and READ cmd.
    printf("* Testing write enable, write and read cmds\n");
    eeWenCmd();
    eeWrCmd(0x4, 0x0123);
    eeRdCmd(0x4, &rdval);
    if ( rdval != 0x0123 ) {
    	printf("---- eeBasicTest: ERROR write/read cmd failed.\n");
    	return (EE_ERR);
    }
    printf("Write enable, write and read cmds passed.\n");

    // Test ERAL cmd.
    printf("* Testing Erase all cmd\n");
    eeEralCmd();
    for ( i=0; i < Array_Depth; i++) {
    	eeRdCmd(i, &rdval);
    	if (rdval != 0xFFFF) {
    		printf("---- eeBasicTest: ERROR ERAL cmd FAILED.\n");
    		return (EE_ERR);
    	}
    }
    printf("Erase all cmd passed.\n");

    // Test WRAL cmd.
    printf("* Testing Write all cmd.\n");
    eeWralCmd(0xB00B);
    for ( i=0; i < Array_Depth; i++) {
    	eeRdCmd(i, &rdval);
    	if (rdval != 0xB00B) {
    		printf("---- eeBasicTest: ERROR WRAL cmd FAILED.\n");
    		return (EE_ERR);
    	}
    }
    printf("Write all cmd passed.\n");

    // Test Erase cmd.
    printf("* Testing Erase cmd.\n");
    eeEraseCmd(0x4);
    eeRdCmd(0x4, &rdval);
    if (rdval != 0xFFFF) {
    	printf("---- eeBasicTest: ERROR ERASE cmd FAILED.\n");
    	return (EE_ERR);
    }
    printf("Erase cmd passed.\n");
 
    printf("**** eeBasicTest -> PASSED\n");
    return (EE_OK);
}


// This test performs unique pattern write to each eeprom address and then
// reads them back to compare for correctness.

u32 eeWrRdTest(void)
{
    u32 i, j, rdval;
    u32 ee_err = EE_OK;

    /* 128 words x 16 bit S-93C56B Seiko EEPROM part */
    u16 ee_array[EEPROM_SZ*2]; 		/* EE check array */

    printf("\n**** eeWrRdTest: Writing unique patterns to entire eeprom...\n");
    for (i=0, j=0; i < EEPROM_SZ * 2; i++, j += 2) {
        ee_array[i] = (u16)(j << 8) | (u16)(j + 1);
        ee_err = eeWrCmd (i, ee_array[i]);
        printf("EEPROM[%03d] <= %04x\n", i, ee_array[i] & 0xffff);
        if ( ee_err == EE_ERR ) {
            printf("---- ERROR write failed @ [%d] <= %04x\n",
                    i, ee_array[i]);
            goto out;
	}
    }
    printf("\nReading back unique patterns and compare...\n");
    for (i=0; i < EEPROM_SZ * 2; i++) {
         // Read 16 bits at a time
	 ee_err = eeRdCmd(i, (u32 *)&rdval);
         if (ee_err != EE_OK) {
             printf("---- ERROR eeRdCmd: %d\n", i);
             goto out;
         }
         printf("EEPROM[%03d] = %04x\n", i, (rdval & 0xffff));
	 if (ee_array[i] != (rdval & 0xffff)) {
             printf("---- ERROR read failed @ [%d]: exp=%04x, got=%04x\n", 
                    i, ee_array[i], (rdval & 0xffff));
             ee_err = EE_ERR;
	     goto out;
	 }
    }
    printf("\nReading and comparing PASSED!\n");
    printf("**** eeWrRdTest -> PASSED\n\n");

out:
    return (ee_err);

}


// TODO: Don't poll after a WRITE, ERASE ALL, or WRITE ALL cmd after issuance 
// of WRITE DISABLE command.  Serial EEPROM device doesn't return ready status
// after you disable its write.  Just disable issuance of write disable cmd for // now.

int
main(void)
{

    u32 ee_err = EE_OK;   	/* EE status */


    /* Set ACR GPIO register values to POR values. Save
     * current values.
     */
    __setACRGPIOResetVals();
    
    /* Connect GPIO[13:10] to SEIKO EEPROM */
    eeGPIOSetup();

    /* The following commands are possible to the Seiko Serial EEPROM.
     * Instructions
     *   READ  ; read 16-bit of data from an address location
     *   WRITE ; write 16-bit of data to an address location
     *   ERASE ; Erase 16-bit of data to 0xFFFF at an address location.
     *   WRAL  ; Write all location with the data given with the WRAL cmd.
     *   ERAL  ; Erase all location to 0xFFFF.
     *   EWEN  ; Enable write operation.  Must be sent prior to any write cmd.
     *   EWDS  ; Disable write operation.
     */
    ////////////////////////////////////////////////////
    // Do a basic test before random.
    ////////////////////////////////////////////////////

    // Basic test testing all commands. Limit to 8 locations.
    ee_err = eeBasicTest(0x8);
    if (ee_err == EE_ERR) {
        printf("Main: ERROR Basic Sanity Test Failed.\n");
	ee_err = EE_ERR;
	goto main_out;
    }

    /* Enable writing the part by sending the EWEN write enable command */
    printf("\nMain: Enabling write to EEPROM device.\n");
    ee_err = eeWenCmd();
    if( ee_err == EE_ERR ) {
    	printf("Main: ERROR Enabled cmd write failed for eeprom.\n");
    	goto main_out;
    }

    ee_err = eeWrRdTest();
    if (ee_err == EE_ERR) {
        printf("Main: ERROR Write-Read Test Failed.\n");
	ee_err = EE_ERR;
	goto main_out;
    }

main_out:

    /* Restore saved ACR GPIO register values. */
    __restoreACRGPIORegs();

    return ((int) ee_err);
}

