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

/*
 * Boot ROM for DV testing
 *
 * This is a specialized version of boot code for running DV tests.
 * This boot code should be loaded in ROM and also linked with the
 * DA space version of the test program, so that the beginning of
 * ROM and DA space are identical up to the start of the test
 * specific code.
 *
 *   Set MACC bits to RX for text space in DA
 *   Flip ROM and DA space
 *   Set MACC bits for user data and BSS
 *   Initialize user BSS segment to 0
 *   Setup stack and call the user supplied main routine in c
 *
 * The user routine can start cpu1 if needed for the test in question.
 * This code is designed so that it runs correctly either before or
 * after the memory flip and no I-cache flushing is required, since
 * the code at a given address is the same before and after the flip.
 *
 * The memory layout in DA space after the flip is as follows:
 *
 *   64KB of DA are used
 *
 *   DA starting at 0 is code and program data
 *   Stack for CPU0 is the last 1K of DA (fC00 - fff0)
 *   Stack for CPU1 is the second to last 1K of DA (f800 - fbf0)
 *
 * The purpose of the assembler portion of the boot process is to setup the
 * exception handling routines and enough machine state to be able to run
 * C code.  The rest of the boot process is conducted in C.  After the C
 * code is finished, the assembly code flips boot rom and DA in the memory
 * map and jumps to the reset handler in DA.
 */

#ifdef THUMB
#define CALL(sym)          \
        ldr     r12, =sym+1 ; \
        mov     lr, pc     ; \
        bx      r12 
#else
#define CALL(sym)	\
        bl      sym
#endif

    .text
    .code 32
    .balign 0
    .global _start
    .global bootReset
    .global bootUndefined
    .global bootSWI
    .global bootPrefetchAbort
    .global bootDataAbort
    .global bootReserved
    .global bootIRQ
    .global bootFIQ

/* Exception vector table */
_start:
        ldr     pc, bootReset
        ldr     pc, bootUndefined
        ldr     pc, bootSWI
        ldr     pc, bootPrefetchAbort
        ldr     pc, bootDataAbort
        ldr     pc, bootReserved
        ldr     pc, bootIRQ
        ldr     pc, bootFIQ

/* Jump table */
bootReset:
    .word     bootResetHandler
bootUndefined:
    .word     bootUndefinedHandler
bootSWI:
    .word     bootSWIHandler
bootPrefetchAbort:
    .word     bootPrefetchAbortHandler
bootDataAbort:
    .word     bootDataAbortHandler
bootReserved:
    .word     bootReservedHandler
bootIRQ:
    .word     bootIRQHandler
bootFIQ:
    .word     bootFIQHandler


/*
 * 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:
        /*
         * Figure out which cpu is running this code
         */
        ldr     r0, =SYS_SMP_REG
        ldr     r0, [r0]
        cmp     r0, #0
        blt     cpu1

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

        /*
         * On cpu0 initialize DA attributes for cpu0 stack
         * and call the rest of the initialization code in c
         */
        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
loop:   str     r1, [r2], #-16
        subs    r0, r0, #16
        bne     loop
        ldr     sp, =CPU0_STACK_DA      @ Set 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
        CALL(bootc)                     @ Call C initialization code

        ldr     r0, =0                  @ Setup arguments and do memorySwap
        ldr     r1, =SYS_CTRL_REG
        ldr     r2, =(SYS_CTRL_CFG_EN|SYS_CTRL_MEM_FLIP)

        /*
         * Here's where the action really happens.  To avoid
         * confusing the CPU it is recommended that the following
         * two instructions are in the same I-cache line.  No
         * I-cache invalidation need be done after the flip, since
         * the code is the same before and after.
         */
        .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
        /* Invalidate L1 I and D caches */
        ldr     r0, =4096               @ Size of L1 caches
        ldr     r1, =L1_II_START        @ L1 I Cache op
        ldr     r2, =L1_DI_START        @ L1 D Cache op
        ldr     r3, =1
L1Loop: str     r3, [r1], #16
        str     r3, [r2], #16
        subs    r0, r0, #16
        bne     L1Loop

        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
        CALL(bootc_postflip)

        /* Now call the user's main in c */
        CALL(cpu0main)

        /* Call DVExit in case the c code didn't */
        CALL(DVExit)

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
        CALL(cpu1main)

        /* Call DVExit in case the c code didn't */
        CALL(DVExit)

/*
 * Null SWI Exception handler
 *
 * Assuming that the SWI was triggered in SVC state,
 * just return to lr to skip the SWI.  This allows
 * DV tests to link to printf and work in both the
 * FPGA environment and on the simulator.
 */
BEGIN_PROC(nullSWIHandler)
        stmfd   sp!, {lr}
        ldmfd   sp!, {pc}^
END_PROC(nullSWIHandler)

/*
 * Dummy exception handlers
 *    These get replaced by the default handler during
 *    initialization
 */
bootUndefinedHandler:
        b       bootUndefinedHandler

bootSWIHandler:
        b       bootSWIHandler

bootPrefetchAbortHandler:
        b       bootPrefetchAbortHandler

bootDataAbortHandler:
        b       bootDataAbortHandler

bootReservedHandler:
        b       bootReservedHandler

bootIRQHandler:
        b       bootIRQHandler

bootFIQHandler:
        b       bootFIQHandler

/*
 * Default exception handler
 *    Initialize stack pointer and call DVExit with
 *    a failing exit status.
 */
BEGIN_PROC(defExcHandler)
    /*
     * Set stack pointer and call DVExit
     */
    ldr     sp, =defExcStack+DEF_EXC_STACK_SIZE
    stmfd   sp!, {r12}

    /*
     * Call DVExit
     */
    ldr     r0, =-256
    CALL(DVExit)
    /*
     * Not Reached 
     */
END_PROC(defExcHandler)
 
/*
 * u32 __skEnableInt(void)
 *
 * Enable IRQ and FIQ
 *
 */
BEGIN_PROC(__skEnableInt)
        mrs     r0, cpsr
        bic     r0, r0, #ARM_IRQ|ARM_FIQ
        msr     cpsr_c, r0
        bx      lr
END_PROC(__skEnableInt)

/*
 * u32 __skDisableInt(void)
 *
 * Disable IRQ and FIQ, return their previous status
 */
BEGIN_PROC(__skDisableInt)
        mrs     r1, cpsr
        and     r0, r1, #ARM_IRQ|ARM_FIQ
        orr     r1, r1, #ARM_IRQ|ARM_FIQ
        msr     cpsr_c, r1
        bx      lr
END_PROC(__skDisableInt)

/*
 * void __skRestoreInt(u32 mask)
 *
 * Set IRQ and FIQ to the values in mask
 */
BEGIN_PROC(__skRestoreInt)
        mrs     r1, cpsr
        bic     r1, r1, #ARM_IRQ|ARM_FIQ
        orr     r1, r1, r0
        msr     cpsr_c, r1
        bx      lr
END_PROC(__skRestoreInt)

/*
 * Initialize IRQ stack pointer
 */
BEGIN_PROC(DVSetIRQStack)
    msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_IRQ
    mov     sp, r0
    msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_SVC
    bx      lr
END_PROC(DVSetIRQStack)

/*
 * Initialize FIQ stack pointer
 */
BEGIN_PROC(DVSetFIQStack)
    msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_FIQ
    mov     sp, r0
    ldr     r8, =0     @ init shadowed regs to prevent 'xxxx'
    ldr     r9, =0
    ldr     r10, =0
    ldr     r11, =0
    ldr     r12, =0
    msr     cpsr_c, #ARM_IRQ|ARM_FIQ|ARM_MODE_SVC
    bx      lr
END_PROC(DVSetFIQStack)

    .end
