/*
 *               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>
#include "mmu.h"

/*
 * void mmuSetTTB(void *ttb)
 *
 * Set the Translation Table Base (TTB) address
 */
BEGIN_PROC(mmuSetTTB)
	mcr	p15, 0, r0, c2, c0, 0  @ write TTB
	bx  lr
END_PROC(mmuSetTTB)

/*
 * u32 mmuGetDFSR(void)
 *
 * Get the Data Fault Status Register (DFSR)
 */
BEGIN_PROC(mmuGetDFSR)
    mrc p15, 0, r0, c5, c0, 0
    bx lr
END_PROC(mmuGetDFSR)

/*
 * void mmuSetDFSR(u32 val)
 *
 * Set the Data Fault Status Register (DFSR)
 */
BEGIN_PROC(mmuSetDFSR)
    mcr p15, 0, r0, c5, c0, 0
    bx lr
END_PROC(mmuSetDFSR)

/*
 * u32 mmuGetIFSR(void)
 *
 * Get the Data Fault Status Register (IFSR)
 */
BEGIN_PROC(mmuGetIFSR)
    mrc p15, 0, r0, c5, c0, 1
    bx lr
END_PROC(mmuGetIFSR)

/*
 * void mmuSetIFSR(u32 val)
 *
 * Set the Data Fault Status Register (IFSR)
 */
BEGIN_PROC(mmuSetIFSR)
    mcr p15, 0, r0, c5, c0, 1
    bx lr
END_PROC(mmuSetIFSR)

/*
 * u32 mmuGetFAR(void)
 *
 * Get the Fault Address Register (FAR)
 */
BEGIN_PROC(mmuGetFAR)
    mrc p15, 0, r0, c6, c0, 0
    bx lr
END_PROC(mmuGetFAR)

/*
 * void mmuSetFAR(u32 val)
 *
 * Set the Fault Address Register (FAR)
 */
BEGIN_PROC(mmuSetFAR)
    mcr p15, 0, r0, c6, c0, 0
    bx lr
END_PROC(mmuSetFAR)

/*
 * void mmuSetDTAR(void *addr)
 *
 * Set the Debug and Test Address Register
 */
BEGIN_PROC(mmuSetDTAR)
    mcr p15, 0, r0, c15, c1, 0
    bx lr
END_PROC(mmuSetDTAR)

/*
 * u32 mmuGetTag(void)
 *
 * Get the MVA tag for the TLB entry indicated by the DTAR
 */
BEGIN_PROC(mmuGetTag)
    mrc p15, 4, r0, c15, c2, 0
    bx lr
END_PROC(mmuGetTag)

/*
 * u32 mmuGetTag(void)
 *
 * Get the physical address and AP for the TLB entry indicated by the DTAR
 */
BEGIN_PROC(mmuGetPA)
    mrc p15, 4, r0, c15, c4, 0
    bx lr
END_PROC(mmuGetPA)

/*
 * void mmuSetDACR(u32 dacr)
 *
 * Set the Domain Access Control Register (DACR)
 */
BEGIN_PROC(mmuSetDACR)
	mcr	p15, 0, r0, c3, c0, 0   @ program DACR  
    bx  lr
END_PROC(mmuSetDACR)

/*
 * void mmuInvalidateTLB(void)
 *
 * Invalidate the entire TLB
 */
BEGIN_PROC(mmuInvalidateTLB)
    ldr	r0, =0                     
	mcr p15, 0, r0, c7, c10, 4  @ drain write buffer
	mcr p15, 0, r0, c8, c7, 0   @ invalidate TLB
    bx  lr
END_PROC(mmuInvalidateTLB)

/*
 * void mmuInvalidateTLBAddr(void *addr)
 *
 * Invalidate a single TLB entry indicated by MVA addr
 */
BEGIN_PROC(mmuInvalidateTLBAddr)
	mcr p15, 0, r0, c8, c7, 1   @ invalidate TLB
    bx  lr
END_PROC(mmuInvalidateTLBAddr)

/*
 * void mmuInvalidateTLBRange(void *start, void *end)
 *
 * Invalidate all addresses between start and end
 */
BEGIN_PROC(mmuInvalidateTLBRange)
1:
	mcr p15, 0, r0, c8, c7, 1   @ invalidate TLB
    add r0, r0, #4096
    cmp r0, r1
    blo 1b
    bx  lr
END_PROC(mmuInvalidateTLBRange)

/*
 * void mmuEnableMMU()
 *
 * Enable MMU & Data Cache
 */
BEGIN_PROC(mmuEnableMMU)
    ldr r0, =mmuDomainAccess
    ldr r0, [r0]
	mcr	p15, 0, r0, c3, c0, 0   @ program DACR  
	
    ldr	r0, =0                     
	mcr p15, 0, r0, c7, c10, 4  @ drain write buffer
	mcr p15, 0, r0, c8, c7, 0   @ invalidate TLB
    
	mrc	p15, 0, r0, c1, c0, 0
	orr	r0, r0, #(ENABLE_MMU|ENABLE_D_CACHE) @enable mmu
	mcr	p15, 0, r0, c1, c0, 0
    
    bx  lr
END_PROC(mmuEnableMMU)

/*
 * void mmuDisableMMU()
 *
 * Disable MMU & Data Cache
 */
BEGIN_PROC(mmuDisableMMU)
	mrc	p15, 0, r0, c1, c0, 0
	bic	r0, r0, #ENABLE_D_CACHE|ENABLE_MMU
	mcr	p15, 0, r0, c1, c0, 0
	bx  lr
END_PROC(mmuDisableMMU)
