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

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

/*
 * the rest of the initialization code
 */
void 
bootc(void)
{
    u32 *from;
    u32 *to;
    u32 *to_high;
    int count;

    /*
     * Set RW permissions on enough of DA space to hold the
     * ROM code
     * XXX while doing this, also set RW permission
     * XXX 4KB above the current location, which will be
     * XXX used to do cache invalidates.
     */
    count = (((int)&_end - (int)&_start) / 16) + 1;
    to = (u32 *) DAX_START;
    to_high = (u32 *) (DAX_START + L1_DCACHE_SIZE);
    while (count-- > 0) {
        *to = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        *to_high = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        to += 4;
        to_high += 4;
    }

    /*
     * Copy the entire ROM image from unflipped ROM space
     * to DA space.
     * XXX while doing this, also store zero to a location
     * XXX 4KB above the current location, which will be
     * XXX used to do cache invalidates.
     */
    from = (u32 *)&_start;
    to = (u32 *) DA_START;
    to_high = (u32 *) (DA_START + L1_DCACHE_SIZE);
    while (from < (u32 *)&__data_end) {
        *to++ = *from++;
        *to_high++ = 0;
    }

    /*
     * Set RX permissions on the text portion of DA space
     */
    count = (((int)&__data_start - (int)&_start) / 16);
    to = (u32 *) DAX_START;
    while (count-- > 0) {
        *to = PT_ACCESS_EXEC << 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 *)(DA_START + (u32)&__bss_start);
    while (count-- > 0)
        *to++ = 0;

    /*
     * Invalidate the D-cache for the data and bss sections
     */
    count = ((int)&__bss_end - (int)&__data_start);
    to = (u32 *)(DA_START + (u32)&__data_start);
    DL1Invalidate((void *)to, count);

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

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

    /*
     * Make sure that the reference to _text_end 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)&__text_end - (int)&_start), 16);
    to = (u32 *) (DAX_START + (int)&_start);
    to_high = (u32 *) ((int)to + L1_DCACHE_SIZE);
    while (count-- > 0) {
        *to = PT_ACCESS_EXEC << PT_SC_PROT_SHIFT;
        to += 4;
        *to_high = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        to_high += 4;
    }

    /*
     * Set RO permissions on the rodata section in DA space
     */
    count = HOWMANY(((int)&__rodata_end - (int)&__rodata_start), 16);
    to = (u32 *) (DAX_START + (int)&__rodata_start);
    to_high = (u32 *) ((int)to + L1_DCACHE_SIZE);
    while (count-- > 0) {
        *to = PT_ACCESS_READ << PT_SC_PROT_SHIFT;
        to += 4;
        *to_high = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        to_high += 4;
    }

    /*
     * Set RW permissions on Data and BSS segments
     * XXX while doing this, also set RW permission
     * XXX 4KB above the current location, which will be
     * XXX used to do cache invalidates.
     */
    count = HOWMANY(((int)&__bss_end - (int)&__data_start), 16);
    to = (u32 *) (DAX_START + (int)&__data_start);
    to_high = (u32 *) ((int)to + L1_DCACHE_SIZE);
    while (count-- > 0) {
        *to = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        *to_high = PT_ACCESS_RW << PT_SC_PROT_SHIFT;
        to += 4;
        to_high += 4;
    }

    /*
     * Initialize the area 4K above text + data space to prevent
     * xxxx during reads.
     * XXX remove this once we have real cache invalidates
     */
    count = ((int)&__data_end - (int)&_start) / 4;
    to_high = (u32 *) ((int)&_start + L1_DCACHE_SIZE);
    /*
     * XXX Make sure we don't trash part of the program, if it's bigger
     * XXX than the cache size.
     */
    if (to_high < (u32 *)&__bss_end) {
        count -= ((u32)&__bss_end - (u32)to_high) / 4;
        to_high = (u32 *)&__bss_end;
    }
    while (count-- > 0) {
        *to_high++ = 0;
    }

    /*
     * The RW permissions were already set on the data and bss space
     * above.  Just clear BSS.
     *
     * Initialize the area 4K above BSS space to prevent
     * xxxx during reads by the cache invalidation routine.
     * XXX remove this once we have real cache invalidates
     */
    count = ((int)&__bss_end - (int)&__bss_start) / 4;
    to = (u32 *)&__bss_start;
    to_high = (u32 *) ((int)to + L1_DCACHE_SIZE);
    while (count-- > 0) {
        *to++ = 0;
        *to_high++ = 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;
}

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

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

typedef unsigned long int size_t;

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;

#if 0	/* XXX this doesn't work for L1 access to DA space */
    /* If region is not completely contained in SDRAM, then return */
    if (start < MEM0_START || end >= MEM2_END || end < start) {
        return;
    }
#endif

    /* 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, 0);
    }
}

void
SCInvalidate(void *ptr, size_t bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_SC, CPS_OP_INV);
}

void
SCWriteback(void *ptr, size_t bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_SC, CPS_OP_WB);
}

void
SCWritebackInvalidate(void *ptr, size_t bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_SC, CPS_OP_WBINV);
}

void
CCInvalidate(void *ptr, size_t bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_CC, CPS_OP_INV);
}

void
CCWriteback(void *ptr, size_t bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_CC, CPS_OP_WB);
}

void
CCWritebackInvalidate(void *ptr, size_t bytes)
{
    __cacheOp(ptr, bytes, CPS_CACHE_CC, CPS_OP_WBINV);
}

/*
 * 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)
{
#if 0
    __cacheOp(ptr, bytes, CPS_CACHE_L1_DH, CPS_OP_INV);
#else
    /* XXX kludge until cache_ops work for L1 */
    int lines;
    u32 val;
    u32 start;
    u32 end;
    volatile u32 *addr;

    start = ROUNDDOWN((u32)ptr, CACHE_LINE_SIZE);
    end = ROUNDUP((u32)ptr + bytes, CACHE_LINE_SIZE);
    lines = HOWMANY(end - start, CACHE_LINE_SIZE);

    addr = (u32 *) ((u32)ptr + L1_DCACHE_SIZE);
    while (lines-- > 0) {
        val = *addr;
        addr += 4;
    }
#endif
}

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
     */
    DL1Invalidate((void *)excp, sizeof *excp);
    *excp = handler;
    *daxp = PT_ACCESS_EXEC << PT_SC_PROT_SHIFT;
    /*
     * Need to invalidate the D-cache
     */
    DL1Invalidate((void *)excp, sizeof *excp);

    return cur;
}
