/*
 *               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 <ios.h>
#include <arm.h>
#include <asm.h>
#include <swi.h>
#include <hw.h>
#include <ioslibc.h>
#include "thread.h"
#include "except.h"
#include "mmu.h"
/* XXX horti: move to except.s later */
/* This call is supposed to be called with IPCIOP interrupt
 * disabled and (presumably) IPCIOP interrupt set, after
 * processing in IPCIOPCTRL has been done. Clearing the interrupt
 * has no effect, if it has not been cleared in the IPC subsystem.
 */

  /* Disable interrupts in cpsr and re-enable them after
   * ACRINTSTS is cleared and ACRINTMASK is enabled.
   */

static void 
__clearAndEnable(u32 mask)
{
    busWrt32(ACR_IOPINTSTS_TRUSTED, mask);
    /* Add delay between modifying mask and re-enabling interrupt */
    busRd32(ACR_IOPINTSTS_TRUSTED);
    
    busWrt32(ACR_INTMASK_TRUSTED, busRd32(ACR_INTMASK_TRUSTED)|mask);
}

void
iosClearAndEnableIPCIOPIntr(void)
{
    u32 mask =  __iosDisableInt();
    __clearAndEnable(ACRINTSTS_ACRIPCIOP_MASK);
    __iosRestoreInt(mask);
}

void
iosClearAndEnableDIIntr(void)
{
    u32 mask =  __iosDisableInt();
    __clearAndEnable(ACRINTSTS_DIINT_MASK);
    __iosRestoreInt(mask);
}

IOSError
iosClearAndEnableSDIntr(u8 hcnum)
{
    IOSError rv = IOS_ERROR_OK;
    u32 mask =  __iosDisableInt();
    if ( 0 == hcnum)
        __clearAndEnable(ACRINTSTS_SD0INT_MASK);
    else if ( 1 == hcnum)
        __clearAndEnable(ACRINTSTS_SD1INT_MASK);
    else
        rv = IOS_ERROR_INVALID;
    __iosRestoreInt(mask);
    return rv;
}

/* XXX horti:  access control */
IOSError
iosClearAndEnableEvent(IOSEvent event)
{
    IOSError rv = IOS_ERROR_OK;
    u32 mask =  __iosDisableInt();
    switch (event) {
        case IOS_EVENT_OH0:
            __clearAndEnable(ACRINTSTS_OH0INT_MASK);
            break;
        case IOS_EVENT_OH1:
            __clearAndEnable(ACRINTSTS_OH1INT_MASK);
            break;
        case IOS_EVENT_EHC:
            __clearAndEnable(ACRINTSTS_EHCINT_MASK);
            break;
        case IOS_EVENT_GPIO:
            busWrt32(GPIIOPINTSTS_REG, GPIO_RTC_SYSINT);
            __clearAndEnable(ACRINTSTS_GPIIOPINT_MASK);
            break;
        default:
            rv = IOS_ERROR_INVALID;
            break;
    }
    __iosRestoreInt(mask);
    return rv;
}

void
iosAbortDebugInfo(IOSThread *t)
{
    extern void iosGetUsage(u32);
    char *reason[] = {
        "",
        "align",
        "",
        "align",
        "",
        "translation (section)",
        "",
        "translation (page)",
        "",
        "domain (section)",
        "",
        "domain (page)",
        "external",
        "permission (section)",
        "external",
        "permission (page)",
    };
    
    iosGetUsage(IOS_USAGE_THREADS);
    u32 dfsr = mmuGetDFSR();
    u32 ifsr = mmuGetIFSR();
    u32 far = mmuGetFAR();
    __iosPanic("Panic: abort in thread 0x%x @ PC %08x\nDFSR: %x [%s]\nIFSR: %x [%s]\nFAR: %08x\n",
                t, t->context.pc, dfsr, reason[dfsr&0xf], ifsr, reason[ifsr&0xf], far);
}

