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

    .text
    .code 32
    .balign 0

/*
 * void __skInitSplock(lock_t lck)
 *
 * Initialize a spinlock, must be called before other Sp primitives
 */
BEGIN_PROC(__skInitSplock)
        ldr     r1, =0
        str     r1, [r0]
        bx      lr
END_PROC(__skInitSplock)

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

/*
 * u32 __skSplockDisable(lock_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(__skSplockDisable)
        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.
         */
        ldr     r2, =0
tryl1d:
#if 0
        ldr     r1, [r0]
        cmp     r1, r2
        bne     tryl1d
#endif
 
        ldr     r1, =1
        swp     r1, r1, [r0]
        cmp     r1, r2
        bne     tryl1d
        mov     r0, r12         @ return previous mask
        bx      lr
END_PROC(__skSplockDisable)

/*
 * void __skSpunlock(lock_t lck)
 *
 * Release a spinlock without modifying Interrupt Mask state.
 * Inverse of __skSplock().
 */
BEGIN_PROC(__skSpunlock)
        ldr     r1, =0
        str     r1, [r0]        @ release the lock
        bx      lr
END_PROC(__skSpunlock)

/*
 * u32 __skSpunlockRestore(lock_t lck, u32 mask)
 *
 * Inverse of __skSplockDisable():
 *
 * Release a spinlock and restore Interrupt Mask as specified.
 * Returns the new mask.
 */
BEGIN_PROC(__skSpunlockRestore)
        ldr     r2, =0
        str     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(__skSpunlockRestore)

    .end
