/*
 *               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 <arm.h>
#include <asm.h>

/* Note: 
 * Invalidate and flush are used here in the traditional sense.
 * Flush = ARM clean
 * Invalidate = ARM flush
 */
   
/* void __iosInvalidateDCacheByRegion(void *ptr, u32 bytes)
 * Invalidate data cache region starting at the cache-line which
 * contains address (ptr)  and ending at the cache-line which contains
 * address (ptr + bytes).
 *  r0 = start address rounded down to cache boundary
 *  r1 = number of lines ( >= 1)
 *  r1 clobbered.
 */
BEGIN_PROC(__iosInvalidateDCacheByRegion)

invalidate_line:
       mcr p15, 0, r0, c7, c6, 1

       add  r0, r0, #1 << CLINE    @ address of next cache-line
       subs r1, r1, #1             @ decrement number of remaining lines
       bgt  invalidate_line

       bx      lr                  @ return to caller
END_PROC(__iosInvalidateDCacheByRegion)

/* void __iosFlushDCacheByRegion(void *ptr, u32 bytes)
 * Flush data cache region starting at the cache-line, which
 * contains address (ptr)  and ending at the cache-line, which contains
 * address (ptr + bytes).
 * r0 = start address rounded down to cache boundary
 * r1 = number of lines ( >= 1)
 * r1 clobbered.
 */
BEGIN_PROC(__iosFlushDCacheByRegion)

flush_line:
       mcr p15, 0, r0, c7, c10, 1

       add  r0, r0, #1 << CLINE      @ address of next cache-line
       subs r1, r1, #1               @ decrement number remaining lines
       bgt flush_line

       bx      lr                      @ return to caller
END_PROC(__iosFlushDCacheByRegion)

/* Flush (ARM clean) entire D cache */
BEGIN_PROC(__iosFlushDCache)
testFlush_loop:
       mrc p15, 0, pc, c7, c10, 3 
       bne  testFlush_loop
       bx      lr                      @ return to caller
END_PROC(__iosFlushDCache)

/* Invalidate (ARM flush) entire I cache */
BEGIN_PROC(__iosInvalidateICache)
       mov r0, #0
       mcr p15, 0, r0, c7, c5, 0 
       bx      lr                      @ return to caller
END_PROC(__iosInvalidateICache)

/* Drain write buffer.
 * Clobbers r0.
 */
BEGIN_PROC(__iosDrainWriteBuffer)
	ldr	r0, =0
	mcr	p15, 0, r0, c7, c10, 4
    bx      lr                      @ return to caller
END_PROC(__iosDrainWriteBuffer)

/* Flush and invalidate (ARM clean) entire D cache */
BEGIN_PROC(__iosWBInvDCache)
1:
	mrc	p15, 0, pc, c7, c14, 3
    bne 1b
    bx      lr                      @ return to caller
END_PROC(__iosWBInvDCache)


