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

/*
 * ASM routines for managing spinlocks (as bytes)
 */

    .text
    .code 32
    .balign 0

/*
 * void __skInitSplockB(lockb_t lck)
 *
 * Initialize a spinlock, must be called before other Sp primitives
 */
BEGIN_PROC(__skInitSplockB)
        ldrb    r1, =0
        strb    r1, [r0]
        bx      lr
END_PROC(__skInitSplockB)

/*
 * void __skSplockB(lockb_t lck)
 *
 * Acquire a spinlock without modifying Interrupt Mask state.
 * Use this version with care:  __skSplockDisable is safer.
 */
BEGIN_PROC(__skSplockB)
        /*
         * Loop on the L1 cache until it gets invalidated
         * by the write from the other cpu.  This prevents
         * burning memory bandwidth while waiting.
         */
        ldrb    r2, =0
tryl1:
#if 0
        ldrb    r1, [r0]
        cmp     r1, r2
        bne     tryl1
#endif
 
        ldrb    r1, =1
        swpb    r1, r1, [r0]
        cmp     r1, r2
        bne     tryl1
        bx      lr
END_PROC(__skSplockB)

/*
 * u32 __skSplockBDisable(lockb_t lck)
 *
 * Acquire a spinlock and mask both IRQ and FIQ, returning
 * the current state of interrupt masking for future use in
 * restoring the interrupt mask.
 */
BEGIN_PROC(__skSplockBDisable)
        mrs     r1, cpsr
        and     r12, r1, #ARM_IRQ|ARM_FIQ    @ save current mask
        orr     r1, r1, #ARM_IRQ|ARM_FIQ
        msr     cpsr_c, r1      @ mask interrupts

        /*
         * Loop on the L1 cache until it gets invalidated
         * by the write from the other cpu.  This prevents
         * burning memory bandwidth while waiting.
         */
        ldrb    r2, =0
tryl1d:
#if 0
        ldrb    r1, [r0]
        cmp     r1, r2
        bne     tryl1d
#endif
 
        ldrb    r1, =1
        swpb    r1, r1, [r0]
        cmp     r1, r2
        bne     tryl1d
        mov     r0, r12         @ return previous mask
        bx      lr
END_PROC(__skSplockBDisable)

/*
 * void __skSpunlockB(lockb_t lck)
 *
 * Release a spinlock without modifying Interrupt Mask state.
 * Inverse of __skSplockB().
 */
BEGIN_PROC(__skSpunlockB)
        ldrb    r1, =0
        strb    r1, [r0]        @ release the lock
        bx      lr
END_PROC(__skSpunlockB)

/*
 * u32 __skSpunlockRestore(lockb_t lck, u32 mask)
 *
 * Inverse of __skSplockBDisable():
 *
 * Release a spinlock and restore Interrupt Mask as specified.
 * Returns the new mask.
 */
BEGIN_PROC(__skSpunlockBRestore)
        ldrb    r2, =0
        strb    r2, [r0]        @ release the lock

        mrs     r0, cpsr
        bic     r0, r0, #ARM_IRQ|ARM_FIQ
        orr     r0, r0, r1
        msr     cpsr_c, r0
        and     r0, r0, #ARM_IRQ|ARM_FIQ     @ return value is new mask
        bx      lr
END_PROC(__skSpunlockBRestore)

    .end
