/*
 *               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>

/* 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)
{
  setupDDR_PPCCGBoundary(LOMEM_DDR_SECURE_BOUNDARY_PHYS + DI_DMA_SP_SIZE, 
         DDR_BASE + (DDR_ADDRSPACE_SIZE - 1));
}

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)
{
    busWrt32(ACR_INTMASK_TRUSTED, 0x0);
    busWrt32(ACR_FIQMASK_TRUSTED, 0x0);
    busWrt32(ACR_DBGINTEN_TRUSTED, 0x0);
}

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);

    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(0); /* 0 = normal mode 1 = compat mode */
    init_VI_PLL();
    init_USB(chipid_major); /* Initialize USB PLLs and other registers */
    enableChipBlocks();
    initIOs();
    EE_GPIOSetup(); /* Initialize both the EEPROM and GPIO */
    __initDiSpinup();
    __clearAllInterrupts();
    __maskAllInterrupts();
    __iosInitMMU();
}
