/*
 *               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 <sc/sc.h>
#include <sc/memmap.h>
#include "lock.h"
#include "thread.h"
#include "memory.h"
#include "sklog.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];

u32
iosGetUsbPower(void)
{
    u32 pwr;

    IOS_ENTRY();
    pwr =  IO_READ(PWR_CTRL_REG) & PWR_CTRL_USB_PWR;
    IOS_EXIT();

    return pwr;
}

u32 __iosDASize;
u32 __iosGlobalLock;

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

void
initc(void)
{
    /* Configure memory with an SC cache */
    u32 ctrl = IO_READ(SYS_CTRL_REG);
    ctrl = (ctrl & ~(SYS_CTRL_CACHE_CONF_MASK|SYS_CTRL_SOFT_RST)) | SYS_CACHE_CONF_DA_192_CC_64_SC_64 | SYS_CTRL_CFG_EN;
    IO_WRITE(SYS_CTRL_REG, ctrl);
    __iosDASize = 192 * 1024;

    /* Clear the SYS_SMP_REG WERR bit if it is set */
    if (IO_READ(SYS_SMP_REG) & SYS_SMP_WERR) {
        IO_WRITE(SYS_SMP_REG, SYS_SMP_WERR);
    }

    /* Clear any pending CIF interrupts before enabling interrupts */
    IO_WRITE(CIF_CTRL_REG, IO_READ(CIF_CTRL_REG));
    /* Enable all device interrupts */
    IO_WRITE(SYS_SMP_REG, (0xff << SYS_SMP_IMASK_SHIFT) | SYS_SMP_SET_IMASK);

    __iosInitLock(&__iosGlobalLock);
    __iosInitSdram();
    __iosInitResourceManager();
    __iosInitCrypto();
    __iosInitThreads();
}
