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

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

    .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
        bl      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
        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
