/*
 *               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 <ioslibc.h>
#include <memmap.h>
#include <hw.h>
#include <lomem.h>
#include "thread.h"
#include "except.h"
#include "ahbops.h"

u8 svcStack[SVC_STACK_SIZE];
u8 irqStack[IRQ_STACK_SIZE];
u8 fiqStack[FIQ_STACK_SIZE];
u8 abtStack[ABT_STACK_SIZE];
u8 undStack[UND_STACK_SIZE];


extern void  __iosInitHardware(void);
extern IOSError __iosInitResourceManager(void);
extern IOSError __iosInitIobufs(void);
extern IOSError __iosInitCrypto(void);

/* All interrupts are disabled and cleared at this point */
static void 
__iosInitInterrupts(void)
{
#ifdef ENABLE_FG
   /* Unmask AHM interrupts */
    busWrt32(AHMINTMASK_TRUSTED, 0xfffffe00);
    
   /* Unmask AHBDBINT interrupts */
   busWrt32(ACR_DBGINTEN_TRUSTED, IOP_DBGINT_INITIAL_MASK);
#endif

  /* Enable IOP IRQs */
  busWrt32(ACR_INTMASK_TRUSTED, IOP_IRQ_INITIAL_MASK);

  /* Enable PPC interrupts. XXX horti verify this is sufficient. */
  busWrt32(ACR_COMPAT_TRUSTED, busRd32(ACR_COMPAT_TRUSTED) |
          ACRCOMPAT_PI_AHB_DEC_EN_MASK |
          ACRCOMPAT_PI_AHB_INT_EN_MASK);

  /* Enable IPC interrupts that IOP should receive. */
   busWrt32(ACR_IPCIOPCTRL_TRUSTED, IPCIOPCTRL_ACK_INTR_MASK |
                                    IPCIOPCTRL_MSG_INTR_MASK);

   /* XXX horti: Let PPC enable its own interrupts. Do we want to 
    * enable stuff for it?
    */

}

/* Init DDR lomem settings with values appropriate to current
 * OS. If we do reinitialize lomem later, it's OK. These 
 * settings will get re-written.
 * If we don't and there's existing lomem info, the DDR
 * settings will get updated.
 */
extern void iosFlushDCache(void *ptr, u32 bytes);
static void
__initLoMemDDR(void)
{
    /* Update DDR info */
    OSLoMem *m = (OSLoMem *) RVL_LOMEM_BASE;
    m->ddrSize = LOMEM_DDR_SIZE;
    m->ddrSimSize = LOMEM_DDR_SIMSIZE;
    m->ddrSecureBoundary = LOMEM_DDR_SECURE_BOUNDARY;
    m->ddrArenaLo = LOMEM_DDR_ARENALO;
    m->ddrArenaHi = LOMEM_DDR_ARENAHI;
    m->ipcArenaLo = LOMEM_IPC_ARENALO;
    m->ipcArenaHi = LOMEM_IPC_ARENAHI;
    m->diBufferLo = LOMEM_DI_BUFFER_LO;
    m->diBufferHi = LOMEM_DI_BUFFER_HI;
    iosFlushDCache(m, sizeof(OSLoMem));
    printf("Updated DDR settings in lomem for current map\n");
}

void
initc(void)
{
    /*
     * Flush and invalidate AHB for the processor. This is necessary
     * because the loader that loads this OS could have left things
     * in the AHB buffers. The loader itself can not clear those
     * buffers since it may not be located in the SRAM, so the kernel
     * needs to clear them at init time
     */
    iosFlushMem(IOS_WB_IOD);
    iosInvalidateRdb(IOS_RB_IOI);

    __iosInitHardware(); 
    __iosInitInterrupts();
    __iosInitIobufs();
    __iosInitResourceManager();
#ifdef ENABLE_CRYPTO
    __iosInitCrypto();
#endif /*ENABLE_CRYPTO */
    __initLoMemDDR();
    __iosInitThreads();
}
