/*
 *               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 <iostypes.h>
#include <ios.h>
#include <ioslibc.h>
#include <sys.h>
#include <hw.h>
#include <lomem.h>
#include <addr_map.h>
#include <ee.h>
extern void __iosPanic(const char *fmt, ...) ;

/* XXX horti: DI_DMA_SIZE is the page we are skipping as a hack,
 * till lomem knows about the di page.
 */
static inline void
__initDDRAccessCtrls(void)
{
  u32 reg;
  u32 cgStart,cgEnd;
#ifdef ENABLE_FG
  u32 fgStart;
#endif
  /* Coarse grained boundary for PI accesses only */
  cgStart = LOMEM_DDR_SECURE_BOUNDARY_PHYS + DI_DMA_SP_SIZE; 
  cgEnd = DDR_BASE + (DDR_ADDRSPACE_SIZE - 1);
  setupDDR_PPCCGBoundary(cgStart, cgEnd);

#ifdef ENABLE_FG
  /* Access control CG/FG boundaries for AHB accesses only */
  fgStart =  LOMEM_DDR_SECURE_BOUNDARY_PHYS - DDR_BASE; 
  cgStart = 0; /* No AHB CG access controls */
  if (IOS_ERROR_OK != setupDDR_FGBoundary(cgStart, fgStart, PTSIZE_DDR_64K)) {
      __iosPanic("Setting up FG access controls failed\n");
  }
#endif
   
  /* Disable PI access to DI */
   reg = busRd32(ACR_AIPPROT_TRUSTED);
   reg &= ~AIPPROT_ENAHBPI2DI_MASK;
   busWrt32(ACR_AIPPROT_TRUSTED, reg);
}

static void
__initAccessCtrls(void)
{
   u32 reg = busRd32(ACR_AIPPROT_TRUSTED);
   AIPPROT_SET_ENAHBIOPI(reg, 1); /* AHB_IO_PI enabled */
   busWrt32(ACR_AIPPROT_TRUSTED, reg);
   
   __initDDRAccessCtrls();
}

static void
__maskAllInterrupts(void)
{
#ifdef ENABLE_FG
    /* AHM intrs already masked */
    busWrt32(ACR_DBGINTEN_TRUSTED, 0x0);
#endif
    busWrt32(ACR_INTMASK_TRUSTED, 0x0);
    busWrt32(ACR_FIQMASK_TRUSTED, 0x0);
}

#ifdef ENABLE_FG
static void
__dumpAHMregs(void)
{
#ifdef ENABLE_FG_DBG
    printf("AHMINTMASK (%x) = %x\n", AHMINTMASK_TRUSTED, busRd32(AHMINTMASK_TRUSTED));
    printf("AHMINTSTS (%x) = %x\n", AHMINTSTS_TRUSTED, busRd32(AHMINTSTS_TRUSTED));
#endif
}

static void
__clearAHMInterrupts(void)
{
    __dumpAHMregs();
    /* Mask all AHM interrupts (mask 1 => masked 0 => enabled) */
    busWrt32(AHMINTMASK_TRUSTED, 0xffffffff);
    /* Attempt to clear AHM interrupts. (Some may remain high, because
     * source is still high. e.g.: count rollover interrupts. But they're
     * always masked.)
     */
    busWrt32(AHMINTSTS_TRUSTED, busRd32(AHMINTSTS_TRUSTED));
    /* Clear AHBDBGINT */
    busWrt32(ACR_DBGINTSTS_TRUSTED, busRd32(ACR_DBGINTSTS_TRUSTED));

    __dumpAHMregs();
}
#endif /* ENABLE_FG */

static void
__clearAllInterrupts(void)
{
    /*
     * We clear the interrupts for timer, flash, AES, and SHA.
     * Since there could be ongoing operations for flash, AES,
     * and SHA, we issue a STOP first, poll for completion, and
     * then clear the interrupt. For other components, they would
     * need to do the same thing in their own resource managers
     */

    /* XXX horti:
     * Without this, we can't clear TMRINT, which seems
     * to always be set on reset.
     */
    busWrt32(ACR_TMRCMP_REG, 0x0);

    busWrt32(FL_CTRL_REG, 0x0);
    while (busRd32(FL_CTRL_REG) & FL_CTRL_EXEC);
    busWrt32(FL_CTRL_REG, 0x0);

    busWrt32(AES_CTRL_REG, 0x0);
    while (busRd32(AES_CTRL_REG) & AES_CTRL_EXEC);
    busWrt32(AES_CTRL_REG, 0x0);

    busWrt32(SHA_CTRL_REG, 0x0);
    while (busRd32(SHA_CTRL_REG) & SHA_CTRL_EXEC);
    busWrt32(SHA_CTRL_REG, 0x0);

#ifdef ENABLE_FG
    __clearAHMInterrupts(); 
#endif /* ENABLE_FG */

    busWrt32(ACR_IOPINTSTS_TRUSTED, ACR_INT_CLEAR_MASK);
}

static void
__initDiSpinup(void)
{
    u32 val;

    /* IOP owns the flag */
    val = busRd32(GPIIOPPPCOWNER_REG);
    val &= ~GPIO_DI_SPINUP;
    busWrt32(GPIIOPPPCOWNER_REG, val);

    /* Signal is output */
    val  = busRd32(GPIIOPOE_REG);
    val |= GPIO_DI_SPINUP;
    busWrt32(GPIIOPOE_REG, val);
}

enum sysMode_e currentMode = enhancedmode;

void 
__iosInitHardware(void)
{
    extern void __iosInitMMU(void);
    u32 chipid_major;
    u32 chipid_minor;
    getChipId(&chipid_major, &chipid_minor);
    initSysMode(currentMode);
    __initAccessCtrls();
    initSWWorkArounds(chipid_major, chipid_minor);
    initHWFixes(chipid_major, chipid_minor);
    init_AI_PLL(INIT_AIPLL_NORMAL, INIT_AIPLL_IFRESET); 
    init_VI_PLL(INIT_VI_IFRESET);
    init_USB(chipid_major); /* Initialize USB PLLs and other registers */
    EE_GPIOSetup(); /* Initialize both the EEPROM and GPIO */
    enableChipBlocks(); /* GPIO needs to be initialized before this */
    initIOs();
    __initDiSpinup();
    __clearAllInterrupts();
    __maskAllInterrupts();
    __iosInitMMU();
}
