/*
 *              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 <types.h>
#include <sc/sc.h>
#include <ioslibc.h>
#include "dv_boot.h"

extern void _start;
extern void __rodata_start;
extern void __rodata_end;
extern void __data_start;
extern void __data_end;
extern void __bss_start;
extern void __bss_end;

#define CACHE_LINE_SIZE 16
#define CACHE_LINE_MASK (CACHE_LINE_SIZE-1)

/*
 * Stack for the default exception handler
 */
u32 defExcStack[DEF_EXC_STACK_SIZE>>2] __attribute__ ((aligned (16)));

/*
 * The rest of pre-flip initialization in c
 */
void 
bootc(void)
{
    u32 *to;
    int count;

    /*
     * Set RX permissions on the text portion of DA space
     */
    count = HOWMANY(((int)&__rodata_start - (int)&_start), 16);
    to = (u32 *) (DAX_START + (int)&_start);
    while (count-- > 0) {
        *to = PT_ACCESS_EXEC << PT_SC_PROT_SHIFT;
        to += 4;
    }

    /*
     * Return to the asm code to do the actual flip 
     */
    return;
}

extern u32 nullSWIHandler;
extern u32 defExcHandler;

/*
 * After the flip, set the rest of the required MACC bits
 * and initialize BSS.
 */
void
bootc_postflip(void)
{
    u32 *to;
    int count;
    int exc;
    exit_comm_area *ecap;

    /*
     * Make sure that the reference to _rodata_start below
     * does not end up in the L1 I-cache when this code
     * executes from ROM.  That is because the value
     * will be different for the code loaded
     * in DA space versus that in ROM space.  The ROM
     * version is just the boot code, but the DA version
     * is a complete test program.  Pad with one cacheline
     * of NOPs.
     */
    __asm__("nop");
    __asm__("nop");
    __asm__("nop");
    __asm__("nop");

    /*
     * Set RX permissions on the text section in DA space
     */
    count = HOWMANY(((int)&__rodata_start - (int)&_start), 16);
    to = (u32 *) (DAX_START + (int)&_start);
    while (count-- > 0) {
        *to = PT_ACCESS_EXEC << PT_SC_PROT_SHIFT;
        to += 4;
    }

    /*
     * Set RO permissions on the RODATA section in DA space
     * Note that we treat RODATA as if it extends all the way
     * to the start of DATA, since there are some compiler literals
     * that seem to get stuck between RODATA and DATA.
     */
    count = HOWMANY(((int)&__data_start - (int)&__rodata_start), 16);
    to = (u32 *) (DAX_START + (int)&__rodata_start);
    while (count-- > 0) {
        *to = PT_ACCESS_READ << PT_SC_PROT_SHIFT;
        to += 4;
    }

    /*
     * Set RW permissions on Data and BSS segments
     */
    count = HOWMANY(((int)&__bss_end - (int)&__data_start), 16);
    to = (u32 *) (DAX_START + (int)&__data_start);
    while (count-- > 0) {
        *to = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        to += 4;
    }

    /*
     * The RW permissions were already set on the data and bss space
     * above.  Just clear BSS.
     */
    count = ((int)&__bss_end - (int)&__bss_start) / 4;
    to = (u32 *)&__bss_start;
    while (count-- > 0) {
        *to++ = 0;
    }

    /*
     * Initialize the communication area that is at the top
     * of CPU0_STACK, including setting RW permission bits.
     */
    to = (u32 *) (DAX_START + (int)EXIT_COMM_AREA);
    *to = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
    ecap = EXIT_COMM_AREA;
    ecap->runstate = ECA_RS_RUNNING;
    ecap->status = -1;

    /*
     * Set the SWI exception vector to a null handler, so that
     * code can call printf when running on the RTL simulator.
     */
    (void) DVSetExcHandler(ARM_EXC_SWI, (u32) &nullSWIHandler);

    /*
     * Set all other exception vectors to a default handler that
     * calls DVExit with a failing exit status.
     */
    for (exc = 0; exc <= ARM_EXC_MAX; exc++) {
        if ((exc != ARM_EXC_RESET) && (exc != ARM_EXC_SWI)) {
            (void) DVSetExcHandler(exc, (u32) &defExcHandler);
        }
    }

}

/*
 * Pass exit status to host program through memory
 */
void
DVExit(int status)
{
    exit_comm_area *ecap = EXIT_COMM_AREA;

    printf("DVExit: status %d\n", status);
    ecap->status = status;
    ecap->runstate = ECA_RS_DONE;

    /*
     * Nothing more to do, just hang out until the host sees
     * the exit and kills the simulator.
     */
    while (1) ;
}

/*
 * Take CPU1 out of reset
 */
void
DVStartCpu1(void)
{
    /*
     * Need to assert reset first for some reason (if this is not
     * done then the L1 cache for CPU1 seems to be in a funky state).
     * XXX is this a hardware bug or something that the DV lib is doing?
     */
    IO_WRITE(SYS_SMP_REG,(SYS_RST_ASSERT<<SYS_SMP_SMP_RST_SHIFT));
    IO_WRITE(SYS_SMP_REG,(SYS_RST_DEASSERT<<SYS_SMP_SMP_RST_SHIFT));
}

static void
__cacheOp(void *ptr, size_t bytes, u32 cache, u32 op)
{
    u32 start, end, off;

    /* Compute requested start and end of region */
    start = (u32)ptr;
    end = (u32)ptr + bytes;

    /* Align start and end on cache line boundaries */
    start &= ~CACHE_LINE_MASK;
    end = (end + CACHE_LINE_MASK) & ~CACHE_LINE_MASK;

    /* Get 32MB offset into SDRAM */
    start &= 0x01ffffff;
    end &= 0x01ffffff;

    /* For each cache line in 'cache' perform 'op' */
    for (off = start; off < end; off += CACHE_LINE_SIZE) {
        u32 addr = CPS_START | (cache << CPS_CACHE_SHIFT) | (op << CPS_OP_SHIFT) | off;
        IO_WRITE(addr, 1);
    }
}

/*
 * Invalidate L1 I-cache
 */
void
IL1Invalidate(void *ptr, u32 bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_L1_IH, CPS_OP_INV);
}

/*
 * Invalidate L1 D-cache
 */
void
DL1Invalidate(void *ptr, u32 bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_L1_DH, CPS_OP_INV);
}

extern u32 bootReset;
extern u32 bootUndefined;
extern u32 bootSWI;
extern u32 bootPrefetchAbort;
extern u32 bootDataAbort;
extern u32 bootReserved;
extern u32 bootIRQ;
extern u32 bootFIQ;

u32 *__DVExcHandlers[] = {
    (u32 *) &bootReset,
    (u32 *) &bootUndefined,
    (u32 *) &bootSWI,
    (u32 *) &bootPrefetchAbort,
    (u32 *) &bootDataAbort,
    (u32 *) &bootReserved,
    (u32 *) &bootIRQ,
    (u32 *) &bootFIQ
};

/*
 * Get address of current exception handler
 */
u32
DVGetExcHandler(int exc)
{
    if (exc > ARM_EXC_MAX)
        return NULL;

    return *__DVExcHandlers[exc];
}

/*
 * Set new exception handler
 */
u32
DVSetExcHandler(int exc, u32 handler)
{
    u32 cur;
    u32 *excp;
    volatile u32 *daxp;

    if (exc > ARM_EXC_MAX)
        return NULL;

    /*
     * The exception vector table has RX permissions, of course,
     * so we need to set them temporarily to RW to scribble on them.
     */
    excp = __DVExcHandlers[exc];
    cur = *excp;
    daxp = (u32 *) ROUNDDOWN(DAX_START + (u32)excp, 16);
    *daxp = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
    /*
     * Need to invalidate the D-cache explicitly
     */
    DL1Invalidate((void *)excp, sizeof *excp);
    *excp = handler;
    *daxp = PT_ACCESS_EXEC << PT_SC_PROT_SHIFT;
    /*
     * Invalidate again to see the new attributes
     */
    DL1Invalidate((void *)excp, sizeof *excp);

    return cur;
}
