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

/*
 * Utility ASM code for cpu mode tests
 *
 */

    .text
    .code 32
    .balign 0

/*
 * Data Abort Exception handler
 *
 * Just set a global flag and return to the instruction
 * after the one that faulted
 */
BEGIN_PROC(modeDataAbortHandler)
    /*
     * Save a couple of registers to use on the stack
     * LR - 4 is the PC for the instruction after the abort
     */
    ldr     sp, =modeDataAbortStack+DABORT_STACK_SIZE
    sub     lr, lr, #4
    stmfd   sp!, {r0-r2,lr}
    /*
     * Set a global flag to indicate the abort
     */
    ldr     r0, =modeDataAbortFlag
    ldr     r1, =1
    str     r1, [r0]
    /*
     * Just return to the following instruction
     */
    ldmfd   sp!, {r0-r2,pc}^
END_PROC(modeDataAbortHandler)

/*
 * Prefetch Abort Exception handler
 *
 * Just set a global flag and return to the instruction
 * after the one that faulted
 */
BEGIN_PROC(modeFetchAbortHandler)
    /*
     * Save a couple of registers to use on the stack
     * LR - 4 is the PC for the instruction that caused the abort,
     * so just preserve LR (to return to the next instruction)
     *
     * Use same stack as Data Abort exception
     */
    ldr     sp, =modeDataAbortStack+DABORT_STACK_SIZE
    stmfd   sp!, {r0-r2,lr}
    /*
     * Set a global flag to indicate the abort
     */
    ldr     r0, =modeFetchAbortFlag
    ldr     r1, =1
    str     r1, [r0]
    /*
     * Just return to the following instruction
     */
    ldmfd   sp!, {r0-r2,pc}^
END_PROC(modeFetchAbortHandler)

/*
 * SWI Exception handler
 *
 * Restore the TID to 0 and then just return using the 
 * sp and lr from the SVC context
 */
BEGIN_PROC(modeSWIHandler)
    stmfd   sp!, {r0-r2,lr}
    /*
     * Must first decide if we came from ARM or Thumb mode
     */
    mrs     r0, spsr
    ands    r0, r0, #ARM_THUMB_STATE
    beq     swi_arm
    /*
     * In Thumb state, so the SWI instruction looks different
     * XXX for now just punt ... only printf is in Thumb
     */
    b       swi_nop

swi_arm:
    ldr     r2, [lr, #-4]
    bic     r2, r2, #0xff000000
    /*
     * SWI 171 is from printf and is also a NOP
     */
    ldr     r1, =171
    subs    r2, r2, r1
    beq     swi_nop
    /*
     * SWI 0x123456 is from semi-hosting and is a NOP
     */
    add     r2, r2, r1
    ldr     r1, =0x123456
    subs    r2, r2, r1
    bne     swi_next

swi_nop:
    ldmfd   sp!, {r0-r2,pc}^

swi_next:
    add     sp, sp, #16         @ discard extra saved registers
    ldr     r2, =SYS_SMP_REG
    ldr     r1, =SYS_SMP_SET_TID
    str     r1, [r2]
    ldmfd   sp!, {r0-r2,pc}
END_PROC(modeSWIHandler)

    .balign 16
BEGIN_PROC(modeUserLaunch)
    /*
     * Set active TID in the SYS_SMP_REG
     */
    stmfd   sp!, {r0-r2,lr}
    ldr     r2, =SYS_SMP_REG
    ldr     r12, =SYS_SMP_TID_MASK
    and     r1, r1, r12
    ldr     r12, =SYS_SMP_SET_TID
    orr     r1, r1, r12
    str     r1, [r2]            @ set user TID
    /*
     * Write cpsr to set USR mode, then reload lr, sp
     * and jump to the supplied address
     */
    msr     cpsr_c, #ARM_MODE_USR    @ Note: IRQ & FIQ must be unmasked here
    ldr     lr, =return_to_here
    ldr     sp, =userStack+USER_STACK_SIZE
    mov     pc, r0              @ arg0 = proc address     
    /*
     * When the called routine returns, do an SWI to get back to SVC
     */
return_to_here:
    swi     0x42
    /*
     * The swi code returns here with the cpu still in SVC mode.
     * Restore registers and use previous LR to return.
     */
    ldmfd   sp!, {r0-r2,pc}
    /*
     * Make sure this routine does not share cachelines
     */
    nop
    nop
    nop
    nop
END_PROC(modeUserLaunch)
    
    .end
