/*
 *               Copyright (C) 2006, 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.
 *
 *  Description:
 *      This file implements the EEPROM read/write functions
 *
 */


#include <iostypes.h>
#include <iop.h>
#include <ioslibc.h>
#include <sys.h>


/* Define GPIO address */
#define EE_GPIO_ENABLE              (ACR_PROT_REGS_BASE+GPIIOPEN_OFFSET)
#define EE_GPIO_OUTOE               (ACR_PROT_REGS_BASE+GPIIOPOE_OFFSET)
#define EE_GPIO_OUT                 (ACR_PROT_REGS_BASE+GPIIOPOUT_OFFSET)
#define EE_GPIO_IN                  (ACR_PROT_REGS_BASE+GPIIOPIN_OFFSET)
#define EE_GPIO_INTERRUPT_ENABLE    (ACR_PROT_REGS_BASE+GPIIOPINTEN_OFFSET)

/* GPIO[13:11] */
#define EE_CS_GPIO_10_BIT_POS       10
#define EE_CK_GPIO_11_BIT_POS       11
#define EE_DI_GPIO_12_BIT_POS       12
#define EE_DO_GPIO_13_BIT_POS       13

#define EE_CS_GPIO_10_MASK          (0x1 << EE_CS_GPIO_10_BIT_POS)
#define EE_CK_GPIO_11_MASK          (0x1 << EE_CK_GPIO_11_BIT_POS)
#define EE_DI_GPIO_12_MASK          (0x1 << EE_DI_GPIO_12_BIT_POS)
#define EE_DO_GPIO_13_MASK          (0x1 << EE_DO_GPIO_13_BIT_POS)
#define EE_GPIO_13_10_MASK          (EE_DO_GPIO_13_MASK | EE_DI_GPIO_12_MASK | \
                                        EE_CK_GPIO_11_MASK | EE_CS_GPIO_10_MASK)

/* Timing in terms of nanosecond */
#define tCDS                        2
#define SYS_CLK_PERIOD              4
#define EE_CLK_LOW                  500
#define EE_CLK_HIGH                 500

/* Master bit time period in terms of number of system clocks */
#define EE_BIT_TIME_HIGH            (EE_CLK_HIGH / SYS_CLK_PERIOD)

/* Slave bit time period in terms of number of system clocks */
#define EE_BIT_TIME_LOW             (EE_CLK_LOW / SYS_CLK_PERIOD)

/*
 * EE cmd string. Only support S-93C56B part. 128w x 16b
 * 1 start bit + 2 op code + 8 addr bit where the 8th address
 * is a 0
 */
#define EE_START_BIT                (0x1 << 10)
#define EE_OP_00                    (0x0 << 8)
#define EE_OP_01                    (0x1 << 8)
#define EE_OP_10                    (0x2 << 8)
#define EE_OP_11                    (0x3 << 8)
#define EE_CMD_WRAL                 0x40
#define EE_CMD_ERAL                 0x80
#define EE_CMD_EWEN                 0xc0
#define EE_CMD_EWDS                 0x00


/* Connect Vegas to EEPROM device via GPIO[13:10] */
void
EE_GPIOSetup(void)
{
    u32 val;

    /*
     *  GPIO[10]= CS        output
     *  GPIO[11]= CLK       output
     *  GPIO[12]= DATAIN    output from vegas
     *  GPIO[13]= DATAOUT   input to vegas
     */

    /* Write POR values */
    initGPIO();

    /* Turn off interrupt enable for GPIO[13:10] */
    val = (busRd32(EE_GPIO_INTERRUPT_ENABLE)) & (~EE_GPIO_13_10_MASK);
    busWrt32(EE_GPIO_INTERRUPT_ENABLE, val);

    /*
     * Drive default value of 0 into outputs GPIO[12:10]
     * CS=0, CLK=0, DATAIN=0
     */
    val = (busRd32(EE_GPIO_OUT)) & (~EE_GPIO_13_10_MASK);
    busWrt32(EE_GPIO_OUT, val);

    /* Enable output enable to Seiko EEPROM via GPIO[12:10] output */
    val = (busRd32(EE_GPIO_OUTOE)) |
            (EE_GPIO_13_10_MASK & (~EE_DO_GPIO_13_MASK));
    busWrt32(EE_GPIO_OUTOE, val);

    /*
     * Make these pins as GPIO - enable connection to Seiko EEPROM
     * via GPIO[12:10] output; GPIO[13] input
     */
    val = (busRd32(EE_GPIO_ENABLE)) | EE_GPIO_13_10_MASK;
    busWrt32(EE_GPIO_ENABLE, val);
}


/* Send clock high */
static void
eeClkHigh(void)
{
    u32 val;

    val = busRd32(EE_GPIO_OUT) | EE_CK_GPIO_11_MASK;
    busWrt32(EE_GPIO_OUT, val);
}

/* Send clock low */
static void
eeClkLow(void)
{
    u32 val;

    val = (busRd32(EE_GPIO_OUT)) & (~EE_CK_GPIO_11_MASK);
    busWrt32(EE_GPIO_OUT, val);
}

/* Send chip select high */
static void
eeCSHigh(void)
{
    u32 val;

    val = (busRd32(EE_GPIO_OUT)) | EE_CS_GPIO_10_MASK;
    busWrt32(EE_GPIO_OUT, val);
}

/* Send chip select low */
static void
eeCSLow(void)
{
    u32 val;

    val = (busRd32(EE_GPIO_OUT)) & (~EE_CS_GPIO_10_MASK);
    busWrt32(EE_GPIO_OUT, val);
}

/* Send data out */
static void
eeDataOut(u32 wval)
{
    u32 val;

    val = busRd32(EE_GPIO_OUT);
    val = (val & ~EE_DI_GPIO_12_MASK) | ((0x1 & wval) << EE_DI_GPIO_12_BIT_POS);
    busWrt32(EE_GPIO_OUT, val);
}


/* Idle Bit time */
static void
eeBitTimeIdle(u32 numOfClk)
{
    u32 i;

    /*
     * Bit time consists of a low time (master) and a high time (slave)
     *                   ____________
     *  \               /
     *   \_____________/
     *   master         slave
     *
     * - Master period starts with the falling edge
     * - Slave period starts with the rising edge
     * - The data output to the serial eeprom is sent on the master period or
     * the falling edge
     * - In between the falling and rising edges are delay call to create
     * a bit time of varying duty cycle
     * - Master period starts with a falling clock edge, then followed by
     * CS go low and write data stay low
     */
    for (i = 0; i < numOfClk; i++) {
        eeClkLow();
        eeCSLow();
        eeDataOut(0);

        /* Master period delay until rising edge */
        busDelay(EE_BIT_TIME_LOW);

        /* Slave period starts with a rising clock edge */
        eeClkHigh();

        /* Slave period delay */
        busDelay(EE_BIT_TIME_HIGH);
    };

    /* Return clock low */
    eeClkLow();
}

/* Write Bit time */
static void
eeBitTimeWr(u32 wval)
{
    /*
     * Master period starts with a falling clock edge, then followed
     * by CS and write data
     */
    eeClkLow();
    eeCSHigh();
    eeDataOut(wval);

    /* Master period delay until rising edge */
    busDelay(EE_BIT_TIME_LOW);

    /* Slave period starts with a rising clock edge */
    eeClkHigh();

    /* Slave period delay */
    busDelay(EE_BIT_TIME_HIGH);
}

/* Read Bit time */
static u32
eeBitTimeRd()
{
    u32 val;

    /* Master period starts with a falling clock edge */
    eeClkLow();
    eeCSHigh();

    /* Master period delay until rising edge */
    busDelay(EE_BIT_TIME_LOW);

    /* Send clock and sample data after slave period */
    eeClkHigh();

    /* Slave period delay */
    busDelay(EE_BIT_TIME_HIGH);

    /*
     * Slave period starts with sampling of the read data before
     * sending the clock
     */
    val = busRd32(EE_GPIO_IN);
    val = (val & EE_DO_GPIO_13_MASK) >> EE_DO_GPIO_13_BIT_POS;

    return (val);
}


/* Send EE write for N number of bit time. Limited to 32 bits or less */
static IOSError
eeWrNbits(u32 numOfBits, u32 wval)
{
    IOSError rc = IOS_ERROR_OK;
    int i;

    if ((numOfBits == 0) || (numOfBits > 32)) {
        rc = IOS_ERROR_INVALID;
        goto eeWrNbits_out;
    }

    /* Write to GPIO for N bit times */
    for (i = (numOfBits - 1); i != -1; i--) {
        eeBitTimeWr(((wval >> i) & 0x1));
    }

eeWrNbits_out:

    return (rc);
}

/* EE read for N number of bit time. Limited to 32 bits or less */
static IOSError
eeRdNbits(u32 numOfBits, u32 *rdval)
{
    IOSError rc = IOS_ERROR_OK;
    int i;
    u32 bit, temp;

    if ((numOfBits == 0) || (numOfBits > 32)) {
        rc = IOS_ERROR_INVALID;
        goto eeRdNbits_out;
    }

    /* Read to GPIO for N bit times */
    temp = 0;
    bit  = 0;
    for (i = (numOfBits - 1); i != -1; i--) {
        bit = eeBitTimeRd();
        temp |= (bit << i);
    }

    *rdval = temp;

eeRdNbits_out:

    return(rc);
}

/*
 * Check write/erase command status. This function should only be
 * called after a write or erase command
 */
static IOSError
eeChkStatus(void)
{
    IOSError rc = IOS_ERROR_OK;   
    u32 status, count;

    status = 0;
    count  = 0;
    do {
        /* Poll for 10 cycles before checking status */
        rc = eeRdNbits(10, &status);
        count++;
    } while (((status & 0x1) == 0) && (count < 100));

    /* Wait out tCDS period, and de-assert cycle after status read command */
    eeBitTimeIdle(tCDS);

    if (count >= 100) {
        rc = IOS_ERROR_UNKNOWN;
    }

    return (rc);
}


/*
 * Create simple command calls
 * READ Data command - read 16-bit data at from address[7:0]
 */
IOSError
EE_RdCmd(u32 addr, u32 *rdval)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr */
    wval =  EE_START_BIT | EE_OP_10 | (addr & 0xff);
    rc = eeWrNbits (11, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_RdCmd_out;
    }

    rc = eeRdNbits (16, rdval);
    if (rc != IOS_ERROR_OK) {
        goto EE_RdCmd_out;
    }

    /* Wait out tCDS period, de-assert cycle after a command */
    eeBitTimeIdle(tCDS);

EE_RdCmd_out:

    return (rc);
}

/* WRITE Data command. Write 16-bit data to address[7:0] */
IOSError
EE_WrCmd(u32 addr, u32 wdata)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr + 16 data */
    wval = ((EE_START_BIT | EE_OP_01 | (addr & 0xFF)) << 16) | (wdata & 0xffff);
    rc = eeWrNbits (27, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_WrCmd_out;
    }

    /* Wait out tCDS period, de-assert cycle after a command */
    eeBitTimeIdle(tCDS);

    /* Verify WRITE command completes */
    rc = eeChkStatus();

EE_WrCmd_out:

    return (rc);
}

/* ERASE Data command. Write 0xffff data to address[7:0] */
IOSError
EE_EraseCmd(u32 addr)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr */
    wval =  EE_START_BIT | EE_OP_11 | (addr & 0xff);
    rc = eeWrNbits(11, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_EraseCmd_out;
    }

    /* Wait out tCDS period, de-assert cycle after a command */
    eeBitTimeIdle(tCDS);

    /* Verify ERASE Command completes */
    rc = eeChkStatus();

EE_EraseCmd_out:

    return (rc);
}

/* Write all command. Write all address location with data */
IOSError
EE_WralCmd(u32 wdata)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr + 16 data */
    wval = ((EE_START_BIT | EE_OP_00 | EE_CMD_WRAL) << 16) | (wdata & 0xffff);
    rc = eeWrNbits(27, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_WralCmd_out;
    }

    /* Wait out tCDS period, de-assert cycle after a command */
    eeBitTimeIdle(tCDS);

    /* Verify WRAL Command completes */
    rc = eeChkStatus();

EE_WralCmd_out:

    return (rc);
}

/* Erase all command. Erase entire chip */
IOSError
EE_EralCmd(void)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr */
    wval = EE_START_BIT | EE_OP_00 | EE_CMD_ERAL;
    rc = eeWrNbits(11, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_EralCmd_out;
    }

    /* Wait out tCDS period, de-assert cycle after a command */
    eeBitTimeIdle(tCDS);

    /* Verify ERASE All Command completes */
    rc = eeChkStatus();

EE_EralCmd_out:

    return (rc);
}

/*
 * Write enable comamnd - enable write to eeprom should be called
 * prior to issuing a write or erase command
 */
IOSError
EE_WenCmd(void)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr */
    wval =  EE_START_BIT | EE_OP_00 | EE_CMD_EWEN;
    rc = eeWrNbits(11, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_WenCmd_out;
    }

    /* Wait out tCDS period */
    eeBitTimeIdle(tCDS);

EE_WenCmd_out:

    return (rc);
}

/*
 * Write disable command - disable write to eeprom to write protect
 * the eeprom
 */
IOSError
EE_WdsCmd(void)
{
    IOSError rc = IOS_ERROR_OK;
    u32 wval;

    /* 1 start + 2 op + 8 addr */
    wval =  EE_START_BIT | EE_OP_00 | EE_CMD_EWDS;
    rc = eeWrNbits(11, wval);
    if (rc != IOS_ERROR_OK) {
        goto EE_WdsCmd_out;
    }

    /* Wait out tCDS period */
    eeBitTimeIdle(tCDS);

EE_WdsCmd_out:

    return (rc);
}
