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

/* Miscellaneous system calls  */
#include <iostypes.h>
#include <sys.h>
#include <iop.h>
#include <ioslibc.h>
#include "thread.h"
#include "except.h"

u32
iosGetDDRVendorIds(void)
{
    return *((u32 *)(getRsvdSRAMStart() + RSVD_SRAM_DDRVID_OFFSET));
}

u32
iosGetHollywoodId(void)
{
    return (busRd32(ACR_PROT_REGS_BASE + ACRCHIPREVID_OFFSET));
}

IOSError
iosSetDiSpinup(u32 s)
{
    if (THIS_THREAD->pid != IOS_DI_PROCESS) {
        return IOS_ERROR_ACCESS;
    }
    u32 val = busRd32(GPIIOPOUT_REG);
    if (s) {
        val &= ~GPIO_DI_SPINUP;
    } else {
        val |=  GPIO_DI_SPINUP;
    }
    busWrt32(GPIIOPOUT_REG, val);
    return IOS_ERROR_OK;
}

#if defined(ENABLE_GET_USAGE)
char *__processes[] = {
    "kernel",    /* pid = 0 */ 
    "eticket",   /*       1 */
    "fs",        /*       2 */
    "di",        /*       3 */
    "oh0",       /*       4 */
    "oh1",       /*       5 */
    "ehci",      /*       6 */
    "sdi",       /*       7 */
    "usbeth",    /*       8 */
    "net",       /*       9 */
    "wd",        /*      10 */
    "wl",        /*      11 */
    "kd",        /*      12 */
    "ncd",       /*      13 */
    "stm",       /*      14 */
    "ppcboot",    /*     15 */
    "unknown"     /*  last element */ 
};
#endif

void
iosGetUsage(u32 usage)
{
#ifdef ENABLE_GET_USAGE
    extern void __iosGetThreadsUsage();
    extern void __iosGetQueuesUsage(void);
    extern void __iosGetTimersUsage(void);
    extern void __iosGetResourcesUsage(void);
    extern void __iosGetHeapsUsage(void);
    extern void __iosGetIobufsUsage(void);
    extern void __iosGetTLBUsage(void);
    u32 mask = __iosDisableInt();
    if (usage & IOS_USAGE_THREADS) {
        __iosGetThreadsUsage();
    }
    if (usage & IOS_USAGE_QUEUES) {
        __iosGetQueuesUsage();
    }
    if (usage & IOS_USAGE_TIMERS) {
        __iosGetTimersUsage();
    }
    if (usage & IOS_USAGE_RESOURCES) {
        __iosGetResourcesUsage();
    }
    if (usage & IOS_USAGE_HEAPS) {
        __iosGetHeapsUsage();
    }
    if (usage & IOS_USAGE_IOBUFS) {
        __iosGetIobufsUsage();
    }
    if (usage & IOS_USAGE_TLB) {
        __iosGetTLBUsage();
    }
    __iosRestoreInt(mask);
#endif /* ENABLE_GET_USAGE */
}

/* XXX horti: is ES the only process that needs this call? */
#include <lomem.h>
extern void iosFlushDCache(void *ptr, u32 size);

void
iosSetLoMemOSVersion(u32 version)
{
    OSLoMem *m = (OSLoMem *) RVL_LOMEM_BASE;
    m->iosVersion = (u32) version;
    iosFlushDCache(&(m->iosVersion), sizeof(m->iosVersion));
}

u32
iosGetLoMemOSVersion(u32 version)
{
    OSLoMem *m = (OSLoMem *) RVL_LOMEM_BASE;
    return (m->iosVersion);
}

void
iosSetDvdReadDisable(u8 disable)
{
    u32 reg = busRd32(ACR_COMPAT_TRUSTED);
    if (disable) {
        reg |= ACRCOMPAT_DI_NODVDRD_EN_MASK;
    }
    else { 
        reg &= ~ACRCOMPAT_DI_NODVDRD_EN_MASK;
    }
    busWrt32(ACR_COMPAT_TRUSTED, reg);
}

u8
iosGetDvdReadDisable()
{
    return ACRCOMPAT_GET_DI_NODVDRD_EN(busRd32(ACR_COMPAT_TRUSTED));
}

void
iosSetEnableAHBPI2DI(u8 enable)
{
    u32 reg = busRd32(ACR_AIPPROT_TRUSTED);
    if (enable) {
        reg |= AIPPROT_ENAHBPI2DI_MASK;
    }
    else {
        reg &= ~AIPPROT_ENAHBPI2DI_MASK;
    }
    busWrt32(ACR_AIPPROT_TRUSTED, reg);
}

u8
iosGetEnableAHBPI2DI(u8 enable)
{
    return AIPPROT_GET_ENAHBPI2DI(busRd32(ACR_AIPPROT_TRUSTED));
}

void
iosSetPPCACRPerms(u8 enable)
{
    if (enable) {
        setPPCACRPerms(1);
    }
    else {
        setPPCACRPerms(0);
    }
}
u32 
iosGetCoreClk(void)
{
    return getSysClk();
}

void
iosOutputLed(u8 value)
{
    outputLed(value);
}
