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

    .text
    .code 32
    .balign 0
    .global _start

/* Exception vector table */
_start:
        b       bootResetHandler
        sub     pc, pc, #8	/* Undefined */
        sub     pc, pc, #8	/* SWI */
        sub     pc, pc, #8
        sub     pc, pc, #8
        sub     pc, pc, #8
        sub     pc, pc, #8
        sub     pc, pc, #8

/*
 * Exception handlers
 *
 * Only define a working handler for Reset.  All other handlers are unique
 * dummy handlers.
 */
#define CPU0_STACK_DA (DA_START+CPU0_STACK)

bootResetHandler:
        /*
         * If memory has already been flipped, jump to user code
         */
        ldr     r1, =SYS_CTRL_REG
        ldr     r1, [r1]
        tst     r1, #SYS_CTRL_MEM_FLIP
        bne     postFlip

        /*
         * Flip memory early on and then jump to DA_START to
         * cause the L1 I-cache lines to get invlidated
         */
        ldr     r0, =DA_START           @ Setup arguments and do memorySwap
        ldr     r1, =SYS_CTRL_REG
        ldr     r2, =(SYS_CTRL_CFG_EN|SYS_CTRL_MEM_FLIP)

        .balign 16
        str     r2, [r1]                @ Swap boot and DA
        mov     pc, r0                  @ Jump to reset vector in DA

postFlip:
        /*
         * On cpu0 initialize DA attributes for cpu0 stack in the
         * post flip location and call the rest of the initialization in c
         */
        .balign 16
        ldr     r0, =DEF_STACK_SIZE     @ Number of DA cachelines
        ldr     r1, =(PT_ACCESS_RW<<PT_SC_PROT_SHIFT)
        ldr     r2, =DAX_START+CPU0_STACK-16   @ Cache op to set DA
loop1:  str     r1, [r2], #-16
        subs    r0, r0, #16
        bne     loop1
        ldr     sp, =CPU0_STACK         @ Setup stack pointer
        bl      bootc_postflip

        /* Now call the user's main in c */
        bl      cpu0main

        /* Call exit in case the c code didn't */
        bl      exit

cpu1:
        /*
         * On cpu1 initialize DA attributes for cpu1 stack
         * and call the user main routine for cpu1
         */
        ldr     r0, =DEF_STACK_SIZE     @ Number of DA cachelines
        ldr     r1, =(PT_ACCESS_RW<<PT_SC_PROT_SHIFT)
        ldr     r2, =DAX_START+CPU1_STACK-16   @ Cache op to set DA
loop2:  str     r1, [r2], #-16
        subs    r0, r0, #16
        bne     loop2
        ldr     sp, =CPU1_STACK         @ Setup stack pointer
        ldr     r3, =0                  @ Init regs to prevent xxxx's
        ldr     r4, =0
        ldr     r5, =0
        ldr     r6, =0
        ldr     r7, =0
        ldr     r8, =0
        ldr     r9, =0
        ldr     r10, =0
        ldr     r11, =0
        bl      cpu1main

        /* Call exit in case the c code didn't */
        bl      exit

/*
 * Dummy exception handlers 
 */
bootUndefinedHandler:
        b       bootUndefinedHandler

bootSWIHandler:
        b       bootSWIHandler

bootPrefetchAbortHandler:
        b       bootPrefetchAbortHandler

bootDataAbortHandler:
        b       bootDataAbortHandler

bootReservedHandler:
        b       bootReservedHandler

bootIRQHandler:
        b       bootIRQHandler

bootFIQHandler:
        b       bootFIQHandler
    
    .end
