/*
 *               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_int.h"

#define STACK_SIZE                  1024

const u8        _initStack[STACK_SIZE];
const u32       _initStackSize = sizeof(_initStack);
const u32       _initPriority = 127;


#define EFUSE_SZ      32  // words
#define EEPROM_SZ     64  // words


///////////////////////////////////////////////////////////////////////////////

/*
 * readEfuse
 *
 * Read 'size' bytes starting at 'address' into 'buffer'.
 */
static void
readEfuse(u32 address, u8 *buffer, int size)
{
    int i;
    u32 data;

    memset (buffer, 0, size);
    for (i = 0; i < size; i += sizeof (u32)) {

        /*
         * Physical read.
         */
        busWrt32(EFUSE_ADDR_REG, 
                 ACREFUSEADDR_EFEN_MASK | (address & ACREFUSEADDR_EFADDR_MASK));
        data = busRd32(EFUSE_DATA_REG);
        *((int *)buffer) = data;   /* XXX check endianness */
        buffer += sizeof(u32);
        address++;
    }

    return;
}


/*
 * Enable IOP access to the GPIO.
 */
static void
enableGpio(void)
{
    u32 reg;

    reg = busRd32(GPIIOPEN_REG);
    GPIIOPEN_SET_EN(reg, GPIIO_PIO_ENABLE);
    busWrt32(GPIIOPEN_REG, reg);

    reg = busRd32(GPIIOPOE_REG);
    GPIIOPEN_SET_EN(reg, GPIIO_PIO_ENABLE);
    busWrt32(GPIIOPOE_REG, reg);

    return;
}

/*
 * setupEfuse*
 */
static void
setupEfuse (void)
{

    /*
     * Enable IOP access to Efuse.
     */
    busWrt32(EFUSE_ADDR_REG, ACREFUSEADDR_EFEN_MASK | 0);

    return;
}


int
main(void)
{
    u32 efuse[EFUSE_SZ];
    u32 eepr;

    u32 rdval;
    u32 i;
    u32 err;

    /* Set ACR GPIO register values to POR values. Save
     * current values.
     */
    __setACRGPIOResetVals();

	/* Connect GPIO[13:10] to SEIKO EEPROM */
    enableGpio();
    eeGPIOSetup();
    setupEfuse();

    /*
     * Read expected hash value from eFuse.
     */
    readEfuse(0, (u8 *)efuse, sizeof(efuse));
    printf("\n");
    for (i = 0; i < EFUSE_SZ; ++i ) {
        printf("EFUSE[%02d] = %08x\n", i, efuse[i]);
    }

    printf("\n\n");
    eepr = 0;
    for (i = 0; i < EEPROM_SZ * 2; ++i) {
        // read 16 bits at a time
        err = eeRdCmd(i, &rdval);
        if (err != EE_OK) {
            printf("ERROR eeRdCmd: %d", i);
            goto out;
        }
        printf("EEPROM[%03d] = %04x\n", i, rdval & 0xffff);
    }
    printf("\n");

out:

    /* Restore saved ACR GPIO register values. */
    __restoreACRGPIORegs();

    return (0);
}
