/*
 *               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.
 *
 * written with a little help from our friends at ATI
 */
#include <iostypes.h>
#include <hw.h>
#include <ioslibc.h>
#include "sys_int.h"
#include <sys.h>

#define NORMAL_IOP_OUTPUT \
(GPIO_RTC_OFFREQ | GPIO_FAN_PWRCNT | GPIO_DCDC_PWRCNT | GPIO_DI_SPINUP | \
GPIO_EEPROM_CS | GPIO_EEPROM_SK | GPIO_EEPROM_DO | GPIO_NDEV_LED)

#define NORMAL_PPC_OUTPUT \
(GPIO_LED_SLOT | GPIO_PAD_DPD | GPIO_DI_EJECTCNT | GPIO_VI_SDA | GPIO_VI_SCL)

#define DCDC_PWRCNT_DELAY 2000 /* us */
//
// Routines to control GPIO
//

void
gpioEnableIOPInt(u32 mask)
{
  busWrt32(GPIIOPINTEN_REG, (busRd32(GPIIOPINTEN_REG)|mask));
}

void
gpioSetIOPIntPol(u32 mask)
{
 busWrt32(GPIIOPINTPOL_REG, (busRd32(GPIIOPINTPOL_REG)|mask));
}

void
initGPIO(void)
{
    u32 owner;
    u32 mask, val;

    // bits to be controlled by PPC
    owner = GPIO_LED_SLOT | GPIO_DI_DISCIN | GPIO_PAD_DPD |
            GPIO_DI_EJECTCNT | GPIO_VI_SDA | GPIO_VI_SCL;

    /* The following need to be controlled by the IOP for STM
     *  GPIO_RTC_OFFREQ
     *  GPIO_FAN_PWRCNT
     *  GPIO_DCDC_PWRCNT
     *  GPIO_RTC_SYSINT
     */
    
    gpioSetOwner(owner);

    // set the output enables and the default outputs for IOP bits
    mask = NORMAL_IOP_OUTPUT;
    val =   GPIO_FAN_PWRCNT | GPIO_DCDC_PWRCNT;

    gpioSet(mask, val);
    gpioSetOutputEnable(mask);

    // set the output enables and the default outputs for PPC bits
    mask = NORMAL_PPC_OUTPUT;
    val = 0;

    gpioPPCSet(mask, val);
    gpioPPCSetOutputEnable(mask);
    
     gpioEnable(0x00ffffff);     // use all 24 as GPIOs, NDEV has leds on upper
   /* Delay for DCDC_PWRCNT setting to settle, before PPC can be 
     * released from reset.
     */
    /* XXX horti: This delay is apparently dependent on PLL setting ??
     * Will update when I get information about this.
     */
    udelay(DCDC_PWRCNT_DELAY); 

    /* Enable interrupts for STM */
    gpioEnableIOPInt(GPIO_RTC_SYSINT);
    gpioSetIOPIntPol(GPIO_RTC_SYSINT);
}

//
// GPIO pins may have other uses, this routine sets which bits are 
// used as GPIOs
//
void
gpioEnable(u32 mask)
{
    busWrt32(GPIIOPEN_REG, mask);
}

//
// Control which CPU can control bits 0 - IOP; 1 - PPC
//

void
gpioSetOwner(u32 owner)
{
    busWrt32(GPIIOPPPCOWNER_REG, owner);
}

void
gpioSetOutputEnable(u32 mask)
{
    busWrt32(GPIIOPOE_REG, mask);
}

void
gpioSet(u32 mask, u32 value)
{
    u32 reg;

    // RMW
    reg = busRd32(GPIIOPOUT_REG) & ~mask;
    reg |= value;
    busWrt32(GPIIOPOUT_REG, reg);

}

u8
gpioGetBit(u32 location)
{
    return 0;
}

//
// All of the routines above imply IOP usage
// Since the IOP initializes the chip, here are some specific routines
// to init the PPC controlled registers
//
void
gpioPPCSetOutputEnable(u32 mask)
{
    busWrt32(GPIPPCOE_REG, mask);
}

void
gpioPPCSet(u32 mask, u32 value)
{

    u32 reg;

    // RMW
    reg = busRd32(GPIPPCOUT_REG) & ~mask;
    reg |= value;
    busWrt32(GPIPPCOUT_REG, reg);
}



// Helper routines for functions that use GPIOs
void
outputLed(u8 value)
{
    gpioSet(GPIO_NDEV_LED, (value << GPIO_NDEV_LED_SHIFT));
}

void
gpioCompatSwitch()
{
    u32 mask, val;

    // set values that PPC had set before the switch
    mask = GPIO_LED_SLOT | GPIO_PAD_DPD | GPIO_VI_SDA | GPIO_VI_SCL;
    val = busRd32(GPIPPCOUT_REG);
    gpioSet(mask, val);
    
    // Ownership to IOP
    gpioSetOwner(0);
    
    // fix up output enables
    mask = NORMAL_PPC_OUTPUT | NORMAL_IOP_OUTPUT;
    gpioSetOutputEnable(mask);
}
