/*
 *               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.
 *
 *  $Id: cache_lock.s,v 1.3 2008/04/08 18:14:48 cnguyen Exp $
 */

#include "asm.h"

#define CLINE	     5
#define CLINE_MASK   ((1 << CLINE) - 1)
		
	 .text
	 .code 32
	 .align 0

	/*
	 * lockIcache(void *address, int length);
	 */
	
	.global lockIcache

	/*
	 * Register usage
	 *
	 * R0:	 address
	 * R1:	 length
	 * R2:	 victim pointer
	 * R3:	 scratch
	 */
BEGIN_PROC(lockIcache)
	/*
         * Save registers.
         */
        sub     sp, sp, #4
        str     lr, [sp]
        sub     sp, sp, #4
        str     r0, [sp]
        sub     sp, sp, #4
        str     r1, [sp]
        sub     sp, sp, #4
        str     r2, [sp]
        sub     sp, sp, #4
        str     r3, [sp]

	/*
	 * On entry, 'address' is in R0 and 'length' is in R1
	 */
	add	r1, r1, r0		// r1 is now end address
	bic	r0, r0, #CLINE_MASK	// align start address to CLINE
	mov	r3, #CLINE_MASK		// scratch CLINE mask
	tst     r1, r3                  // CLINE end fragment?
	sub     r1, r1, r0              // add alignment bytes
	mov	r1, r1, lsr #CLINE	// convert size 2 # CLINE
	addne	r1, r1, #1		// Add CLINE to hold fragment

	/*
	 * Check if null operation
	 */
	cmp	r1, #0			// Null request
	beq	ilock_out

	/*
	 * Carry out the cache operation
	 */
ilock_op:
	mrc     p15, 0, r0, c9, c0, 1
	orr     r0, r0, #0x1
	mcr	p15, 0, r0, c9, c0, 1	// Lock instruction cache
	add	r0, r0, #(1 << CLINE)

	/*
	* Loop count
	*/
	sub	r1, r1, #1
	cmp	r1, #0
	bne	ilock_op
	
ilock_out:	
	/*
         * Restore registers.
         */
        ldr     r3, [sp]
        add     sp, sp, #4
        ldr     r2, [sp]
        add     sp, sp, #4
        ldr     r1, [sp]
        add     sp, sp, #4
        ldr     r0, [sp]
        add     sp, sp, #4
        ldr     lr, [sp]
        add     sp, sp, #4
        bx      lr

END_PROC(lockIcache)

	/*
	 * lockDcache(void *address, int length);
	 */
	
	.global lockDcache

	/*
	 * Register usage
	 *
	 * R0:	 address
	 * R1:	 length
	 * R2:	 victim pointer
	 * R3:	 scratch
	 */
BEGIN_PROC(lockDcache)
	/*
         * Save registers.
         */
        sub     sp, sp, #4
        str     lr, [sp]
        sub     sp, sp, #4
        str     r0, [sp]
        sub     sp, sp, #4
        str     r1, [sp]
        sub     sp, sp, #4
        str     r2, [sp]
        sub     sp, sp, #4
        str     r3, [sp]

	/*
	 * On entry, 'address' is in R0 and 'length' is in R1
	 */
	add	r1, r1, r0		// r1 is now end address
	bic	r0, r0, #CLINE_MASK	// align start address to CLINE
	mov	r3, #CLINE_MASK		// scratch CLINE mask
	tst     r1, r3                  // CLINE end fragment?
	sub     r1, r1, r0              // add alignment bytes
	mov	r1, r1, lsr #CLINE	// convert size 2 # CLINE
	addne	r1, r1, #1		// Add CLINE to hold fragment

	/*
	 * Check if null operation
	 */
	cmp	r1, #0			// Null request
	beq	dlock_out

	/*
	 * Carry out the cache operation
	 */
dlock_op:
	mrc     p15, 0, r0, c9, c0, 0
	orr     r0, r0, #01
	mcr	p15, 0, r0, c9, c0, 0	// Lock data cache
	add	r0, r0, #(1 << CLINE)

	/*
	* Loop count
	*/
	sub	r1, r1, #1
	cmp	r1, #0
	bne	dlock_op
	
dlock_out:	
	/*
         * Restore registers.
         */
        ldr     r3, [sp]
        add     sp, sp, #4
        ldr     r2, [sp]
        add     sp, sp, #4
        ldr     r1, [sp]
        add     sp, sp, #4
        ldr     r0, [sp]
        add     sp, sp, #4
        ldr     lr, [sp]
        add     sp, sp, #4
        bx      lr

END_PROC(lockDcache)

	/*
	 * cacheDbgRd
	 *
	 * Read the cache debug register and return it in r0.
	 */

	.global cacheDbgRd

BEGIN_PROC(cacheDbgRd)

	/*
	 * Read the cache debug register
	 */
	mrc	p15, 7, r0, c15, c0, 0	// read cache debug register

	/*
	 * Return
	 */
	bx      lr
	
END_PROC(cacheDbgRd)

	/*
	 * cacheDbgWr
	 *
	 * XOR bits to the cache debug register and return the
	 * end result on r0.
	 */

	.global cacheDbgWr
	
BEGIN_PROC(cacheDbgWr)

	/*
	 * Save R1
	 */
	sub	sp, sp, #4
	str	r1, [sp]

	/*
	 * Read the CP, modify it and write.   Then re-read
	 * for return value.
	 */
	mrc	p15, 7, r1, c15, c0, 0	// read cache debug register
	eor	r1, r1, r0		// flip the bits supplied
	mcr	p15, 7, r1, c15, c0, 0	// write the cache debug register
	mrc	p15, 7, r0, c15, c0, 0	// re-read the cache debug register

	/*
	 * Restore R1
	 */
	ldr	r1, [sp]
	add	sp, sp, #4

	/*
	 * Return
	 */
	bx  lr
	
END_PROC(cacheDbgWr)

/* eof */
