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

/*
 * ASM routines for managing spinlocks
 */

    .text
    .code 32
    .balign 0

/*
 * void __iosInitLock(IOSLock lock)
 *
 * Initialize a spinlock, must be called before other lock primitives
 */
BEGIN_PROC(__iosInitLock)
        ldr     r1, =0
        str     r1, [r0]
        bx      lr
END_PROC(__iosInitLock)

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

/*
 * u32 __iosLockDisable(IOSLock lock)
 *
 * 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(__iosLockDisable)
        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

tryl1d:
#if 0
        /*
         * Loop on the L1 cache until it gets invalidated
         * by the write from the other cpu.  This prevents
         * burning memory bandwidth while waiting.
         */
        ldr     r1, [r0]
        cmp     r1, #0
        bne     tryl1d
#endif
 
        ldr     r1, =1
        swp     r1, r1, [r0]
        cmp     r1, #0
        bne     tryl1d

        add     r2, r0, #L1_DH_START    @ Invalidate L1
        str     r1, [r2]

        mov     r0, r12         @ return previous mask
        bx      lr
END_PROC(__iosLockDisable)

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

/*
 * u32 __iosUnlockRestore(IOSLock lock, u32 mask)
 *
 * Inverse of __iosLockDisable():
 *
 * Release a spinlock and restore Interrupt Mask as specified.
 * Returns the new mask.
 */
BEGIN_PROC(__iosUnlockRestore)
        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(__iosUnlockRestore)

    .end
