2017-08-08 23:04:17 -03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-09 09:33:52 -03:00
|
|
|
#include "types.h"
|
|
|
|
|
2017-08-08 23:04:17 -03:00
|
|
|
/* Status Register flags */
|
2019-04-09 09:33:52 -03:00
|
|
|
#define SR_USR_MODE (0x10)
|
|
|
|
#define SR_FIQ_MODE (0x11)
|
|
|
|
#define SR_IRQ_MODE (0x12)
|
|
|
|
#define SR_SVC_MODE (0x13)
|
|
|
|
#define SR_ABT_MODE (0x17)
|
|
|
|
#define SR_UND_MODE (0x1B)
|
|
|
|
#define SR_SYS_MODE (0x1F)
|
|
|
|
#define SR_PMODE_MASK (0x1F)
|
|
|
|
|
|
|
|
#define SR_THUMB BIT(5)
|
|
|
|
#define SR_NOFIQ BIT(6)
|
|
|
|
#define SR_NOIRQ BIT(7)
|
|
|
|
#define SR_NOINT (SR_NOFIQ | SR_NOIRQ)
|
2017-08-12 16:04:20 -03:00
|
|
|
|
|
|
|
#ifdef ARM9
|
2019-04-09 09:33:52 -03:00
|
|
|
#define CR_MPU BIT(0)
|
|
|
|
#define CR_DCACHE BIT(2)
|
|
|
|
#define CR_ICACHE BIT(12)
|
|
|
|
#define CR_DTCM BIT(16)
|
|
|
|
#define CR_ITCM BIT(18)
|
|
|
|
#define CR_V4TLD BIT(15)
|
|
|
|
|
|
|
|
#define CR_ALT_VECTORS BIT(13)
|
|
|
|
#define CR_CACHE_RROBIN BIT(14)
|
|
|
|
#define CR_DTCM_LOAD BIT(17)
|
|
|
|
#define CR_ITCM_LOAD BIT(19)
|
2019-04-11 18:16:20 -03:00
|
|
|
|
|
|
|
#define CR_TCM_LOAD (CR_DTCM_LOAD | CR_ITCM_LOAD)
|
2019-04-09 09:33:52 -03:00
|
|
|
|
|
|
|
#define ICACHE_SZ (4096)
|
|
|
|
#define DCACHE_SZ (4096)
|
2019-04-11 18:16:20 -03:00
|
|
|
|
|
|
|
#define MAX_IRQ (32)
|
|
|
|
#define MAX_CPU (1)
|
2019-04-09 09:33:52 -03:00
|
|
|
#else // ARM11
|
|
|
|
#define CR_MMU BIT(0)
|
|
|
|
#define CR_ALIGN BIT(1)
|
|
|
|
#define CR_DCACHE BIT(2)
|
|
|
|
#define CR_ICACHE BIT(12)
|
|
|
|
#define CR_FLOWPRED BIT(11)
|
|
|
|
#define CR_HIGHVEC BIT(13)
|
|
|
|
#define CR_V4TLD BIT(15)
|
|
|
|
#define CR_UNALIGN BIT(22)
|
|
|
|
#define CR_DSUBPAGE BIT(23)
|
|
|
|
|
|
|
|
#define ACR_RETSTK BIT(0)
|
|
|
|
#define ACR_DBPRED BIT(1)
|
|
|
|
#define ACR_SBPRED BIT(2)
|
|
|
|
#define ACR_FOLDING BIT(3)
|
|
|
|
#define ACR_EXCL BIT(4)
|
|
|
|
#define ACR_SMP BIT(5)
|
|
|
|
|
|
|
|
#define ICACHE_SZ (16384)
|
|
|
|
#define DCACHE_SZ (16384)
|
2019-04-11 18:16:20 -03:00
|
|
|
|
|
|
|
#define MAX_IRQ (96)
|
|
|
|
#define MAX_CPU (2)
|
2019-04-09 09:33:52 -03:00
|
|
|
#endif
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
#define CR_CACHES (CR_DCACHE | CR_ICACHE)
|
|
|
|
|
2019-04-09 09:33:52 -03:00
|
|
|
|
|
|
|
#ifndef __ASSEMBLER__
|
|
|
|
|
|
|
|
/* ARM Private Memory Region */
|
|
|
|
#ifdef ARM11
|
|
|
|
#define REG_ARM_PMR(off, type) ((volatile type*)(0x17E00000 + (off)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define ARM_MCR(cp, op1, reg, crn, crm, op2) asm_v( \
|
|
|
|
"MCR " #cp ", " #op1 ", %[R], " #crn ", " #crm ", " #op2 "\n\t" \
|
|
|
|
:: [R] "r"(reg))
|
|
|
|
|
|
|
|
#define ARM_MRC(cp, op1, reg, crn, crm, op2) asm_v( \
|
|
|
|
"MRC " #cp ", " #op1 ", %[R], " #crn ", " #crm ", " #op2 "\n\t" \
|
|
|
|
: [R] "=r"(reg))
|
|
|
|
|
|
|
|
#define ARM_MSR(cp, reg) asm_v( \
|
|
|
|
"MSR " #cp ", %[R]\n\t" \
|
|
|
|
:: [R] "r"(reg))
|
|
|
|
|
|
|
|
#define ARM_MRS(reg, cp) asm_v( \
|
|
|
|
"MRS %[R], " #cp "\n\t" \
|
|
|
|
: [R] "=r"(reg))
|
|
|
|
|
|
|
|
#ifdef ARM11
|
|
|
|
#define ARM_CPS(m) asm_v("CPS " #m)
|
|
|
|
#define ARM_CPSID(m) asm_v("CPSID " #m)
|
|
|
|
#define ARM_CPSIE(m) asm_v("CPSIE " #m)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An Instruction Synchronization Barrier (ISB) flushes the pipeline in the processor
|
|
|
|
* so that all instructions following the ISB are fetched from cache or memory
|
|
|
|
* after the ISB has been completed.
|
|
|
|
*/
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_ISB(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, 0, c7, c5, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A Data Memory Barrier (DMB) ensures that all explicit memory accesses before
|
|
|
|
* the DMB instruction complete before any explicit memory accesses after the DMB instruction start.
|
|
|
|
*/
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_DMB(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, 0, c7, c10, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait For Interrupt */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_WFI(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
asm_v("wfi\n\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait For Event */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_WFE(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
asm_v("wfe\n\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send Event */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_SEV(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
asm_v("sev\n\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Auxiliary Control Registers */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline u32 ARM_GetACR(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 acr;
|
|
|
|
ARM_MRC(p15, 0, acr, c1, c0, 1);
|
|
|
|
return acr;
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_SetACR(u32 acr) {
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, acr, c1, c0, 1);
|
|
|
|
}
|
2017-08-12 16:04:20 -03:00
|
|
|
#endif
|
2019-04-09 09:33:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A Data Synchronization Barrier (DSB) completes when all
|
|
|
|
* instructions before this instruction complete.
|
|
|
|
*/
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_DSB(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, 0, c7, c10, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Control Registers */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline u32 ARM_GetCR(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 cr;
|
|
|
|
ARM_MRC(p15, 0, cr, c1, c0, 0);
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_SetCR(u32 cr) {
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, cr, c1, c0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread ID Registers */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline u32 ARM_GetTID(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 pid;
|
|
|
|
#ifdef ARM9
|
|
|
|
ARM_MRC(p15, 0, pid, c13, c0, 1);
|
|
|
|
#else
|
|
|
|
ARM_MRC(p15, 0, pid, c13, c0, 4);
|
|
|
|
#endif
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_SetTID(u32 pid) {
|
2019-04-09 09:33:52 -03:00
|
|
|
#ifdef ARM9
|
|
|
|
ARM_MCR(p15, 0, pid, c13, c0, 1);
|
|
|
|
#else
|
|
|
|
ARM_MCR(p15, 0, pid, c13, c0, 4);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CPU ID */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline u32 ARM_CoreID(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 id;
|
|
|
|
#ifdef ARM9
|
|
|
|
id = 0;
|
|
|
|
#else
|
|
|
|
ARM_MRC(p15, 0, id, c0, c0, 5);
|
|
|
|
#endif
|
|
|
|
return id & 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Status Register */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline u32 ARM_GetCPSR(void) {
|
|
|
|
u32 sr;
|
|
|
|
ARM_MRS(sr, cpsr);
|
|
|
|
return sr;
|
2019-04-09 09:33:52 -03:00
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_SetCPSR_c(u32 sr) {
|
|
|
|
ARM_MSR(cpsr_c, sr);
|
2019-04-09 09:33:52 -03:00
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_DisableInterrupts(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
#ifdef ARM9
|
2019-04-11 18:16:20 -03:00
|
|
|
ARM_SetCPSR_c(ARM_GetCPSR() | SR_NOINT);
|
2019-04-09 09:33:52 -03:00
|
|
|
#else
|
|
|
|
ARM_CPSID(if);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_EnableInterrupts(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
#ifdef ARM9
|
2019-04-11 18:16:20 -03:00
|
|
|
ARM_SetCPSR_c(ARM_GetCPSR() & ~SR_NOINT);
|
2019-04-09 09:33:52 -03:00
|
|
|
#else
|
|
|
|
ARM_CPSIE(if);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline u32 ARM_EnterCritical(void) {
|
|
|
|
u32 stat = ARM_GetCPSR();
|
|
|
|
ARM_DisableInterrupts();
|
2019-04-09 09:33:52 -03:00
|
|
|
return stat & SR_NOINT;
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_LeaveCritical(u32 stat) {
|
|
|
|
ARM_SetCPSR_c((ARM_GetCPSR() & ~SR_NOINT) | stat);
|
2019-04-09 09:33:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Cache functions */
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_InvIC(void) {
|
|
|
|
#ifdef ARM9
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, 0, c7, c5, 0);
|
2019-04-11 18:16:20 -03:00
|
|
|
#else
|
|
|
|
ARM_MCR(p15, 0, 0, c7, c7, 0);
|
|
|
|
#endif
|
2019-04-09 09:33:52 -03:00
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_InvIC_Range(void *base, u32 len) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 addr = (u32)base & ~0x1F;
|
|
|
|
len >>= 5;
|
|
|
|
|
|
|
|
do {
|
2019-04-11 18:16:20 -03:00
|
|
|
#ifdef ARM9
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, addr, c7, c5, 1);
|
2019-04-11 18:16:20 -03:00
|
|
|
#else
|
|
|
|
ARM_MCR(p15, 0, addr, c7, c7, 1);
|
|
|
|
#endif
|
2019-04-09 09:33:52 -03:00
|
|
|
addr += 0x20;
|
|
|
|
} while(len--);
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_InvDC(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
ARM_MCR(p15, 0, 0, c7, c6, 0);
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_InvDC_Range(void *base, u32 len) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 addr = (u32)base & ~0x1F;
|
|
|
|
len >>= 5;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ARM_MCR(p15, 0, addr, c7, c6, 1);
|
|
|
|
addr += 0x20;
|
|
|
|
} while(len--);
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_WbDC(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
#ifdef ARM9
|
|
|
|
u32 seg = 0, ind;
|
|
|
|
do {
|
|
|
|
ind = 0;
|
|
|
|
do {
|
|
|
|
ARM_MCR(p15, 0, seg | ind, c7, c10, 2);
|
|
|
|
ind += 0x20;
|
|
|
|
} while(ind < 0x400);
|
|
|
|
seg += 0x40000000;
|
|
|
|
} while(seg != 0);
|
|
|
|
#else
|
|
|
|
ARM_MCR(p15, 0, 0, c7, c10, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_WbDC_Range(void *base, u32 len) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 addr = (u32)base & ~0x1F;
|
|
|
|
len >>= 5;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ARM_MCR(p15, 0, addr, c7, c10, 1);
|
|
|
|
addr += 0x20;
|
|
|
|
} while(len--);
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_WbInvDC(void) {
|
2019-04-09 09:33:52 -03:00
|
|
|
#ifdef ARM9
|
|
|
|
u32 seg = 0, ind;
|
|
|
|
do {
|
|
|
|
ind = 0;
|
|
|
|
do {
|
|
|
|
ARM_MCR(p15, 0, seg | ind, c7, c14, 2);
|
|
|
|
ind += 0x20;
|
|
|
|
} while(ind < 0x400);
|
|
|
|
seg += 0x40000000;
|
|
|
|
} while(seg != 0);
|
|
|
|
#else
|
|
|
|
ARM_MCR(p15, 0, 0, c7, c14, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_WbInvDC_Range(void *base, u32 len) {
|
2019-04-09 09:33:52 -03:00
|
|
|
u32 addr = (u32)base & ~0x1F;
|
|
|
|
len >>= 5;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ARM_MCR(p15, 0, addr, c7, c14, 1);
|
|
|
|
addr += 0x20;
|
|
|
|
} while(len--);
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:16:20 -03:00
|
|
|
static inline void ARM_BKPT(void) {
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
|
2019-04-09 09:33:52 -03:00
|
|
|
#endif // __ASSEMBLER__
|