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

#define CHECK_PID(PROC) \
    if (THIS_THREAD->pid != IOS_##PROC##_PROCESS) { \
        rv = IOS_ERROR_ACCESS; \
        goto done; \
    } \


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

IOSError
iosClearAndEnableEvent(IOSEvent event)
{
    IOSError rv = IOS_ERROR_OK;
    u32 mask =  __iosDisableInt();
    switch (event) {
        case IOS_EVENT_DI:
            CHECK_PID(DI);
            __clearAndEnable(ACRINTSTS_DIINT_MASK);
            break;
        case IOS_EVENT_OH0:
            CHECK_PID(OH0);
            __clearAndEnable(ACRINTSTS_OH0INT_MASK);
            break;
        case IOS_EVENT_OH1:
            CHECK_PID(OH1);
            __clearAndEnable(ACRINTSTS_OH1INT_MASK);
            break;
        case IOS_EVENT_EHC:
            CHECK_PID(EHCI);
            __clearAndEnable(ACRINTSTS_EHCINT_MASK);
            break;
        case IOS_EVENT_GPIO:
            CHECK_PID(STM);
            busWrt32(GPIIOPINTSTS_REG, GPIO_RTC_SYSINT);
            __clearAndEnable(ACRINTSTS_GPIIOPINT_MASK);
            break;
        case IOS_EVENT_RSW:
            CHECK_PID(STM);
            __clearAndEnable(ACRINTSTS_RSWINT_MASK);
            break;
        case IOS_EVENT_IPCIOP:
            CHECK_PID(KERNEL);
            __clearAndEnable(ACRINTSTS_ACRIPCIOP_MASK);
            break;
        case IOS_EVENT_SD0:
            CHECK_PID(SDI);
            __clearAndEnable(ACRINTSTS_SD0INT_MASK);
            break;
        case IOS_EVENT_SD1:
            CHECK_PID(WL);
            __clearAndEnable(ACRINTSTS_SD1INT_MASK);
            break;
        default:
            rv = IOS_ERROR_INVALID;
            break;
    }
done:
    __iosRestoreInt(mask);
    return rv;
}

IOSError
iosClearAndEnableDIIntr(void)
{
    return iosClearAndEnableEvent(IOS_EVENT_DI);
}

IOSError
iosClearAndEnableSDIntr(u8 hcnum)
{
    IOSEvent event = (0 == hcnum)? IOS_EVENT_SD0:
                             IOS_EVENT_SD1;
    return iosClearAndEnableEvent(event);
}

/* 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.
 */
IOSError
iosClearAndEnableIPCIOPIntr(void)
{
    return iosClearAndEnableEvent(IOS_EVENT_IPCIOP);
}

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