ARM筆記: MMU單元控制器
.text
.global _start
_start:
ldrsp,=4096@設(shè)置棧指針,以下都是C函數(shù),調(diào)用前需要設(shè)好棧
bldisable_watch_dog@關(guān)閉WATCHDOG,否則CPU會(huì)不斷重啟
blmemsetup@設(shè)置好存儲(chǔ)控制器以使用SDRAM
blcopy_2th_to_sdram@將第二部分代碼復(fù)制到SDRAM
blcreate_page_table@設(shè)置頁(yè)表
blmmu_init@啟動(dòng)MMU
ldrsp,=0xb4000000@重設(shè)棧指針,指向SDRAM頂端(使用虛擬地址)
ldrpc,=0xb0004000@跳到SDRAM中繼續(xù)執(zhí)行第二部份代碼
halt_loop:
bhalt_loop
#define WTCON(*(volatile unsigned long *)0x53000000)
#define MEM_CTL_BASE0X48000000
void disable_watch_dog(void)
{
WTCON=0;//關(guān)閉WATCHDOG,WATCHDOG寄存器設(shè)為0
}
void memsetup(void)
{
unsigned long const mem_cfg_val[]={0x22011110,//BWSCON
0x00000700,//BANKCON0
0x00000700,//BANKCON1
0x00000700,//BANKCON2
0x00000700,//BANKCON3
0x00000700,//BANKCON4
0x00000700,//BANKCON5
0x00018005,//BANKCON6
0x00018005,//BANKCON7
0x008c07a3,//REFRESH
0x000000b1,//BANKSIZE
0x00000030,//MRSRB6
0X00000030,//MRSRB7
};
int i=0;
volatile unsigned long *p=(volatile unsigned long *)MEM_CTL_BASE;
for(;i<13;i++)
{
p[i]=mem_cfg_val[i];
}
}
void copy_2th_to_sdram(void)
{
unsigned int *pdwSrc=(unsigned int *)2048;
unsigned int *pdwDest=(unsigned int *)0x30004000;
while(pdwSrc<(unsigned int *)4096)
{
*pdwDest=*pdwSrc;
pdwDest++;
pdwSrc++;
}
}
void create_page_table(void)
{
//用于段描述符的一些宏定義
#defineMMU_FULL_ACCESS(3<<10)//訪問(wèn)權(quán)限
#define MMU_DOMAIN(0<<5)//屬于那個(gè)域
#define MMU_SPECIAL(1<<4)//必須是1
#define MMU_CACHEABLE(1<<3)
#define MMU_BUFFERABLE(1<<2)
#define MMU_SECTION(2)
#define MMU_SECDESC(MMU_FULL_ACCESS|MMU_DOMAIN|MMU_SPECIAL|MMU_SECTION)
#define MMU_SECDESC_WB(MMU_FULL_ACCESS|MMU_DOMAIN|MMU_SPECIAL|MMU_CACHEABLE|
MMU_BUFFERABLE|MMU_SECTION)
#define MMU_SECTION_SIZE 0x00100000
unsigned long virtuladdr,physicaladdr;
unsigned long *mmu_tlb_base=(unsigned long *)0x30000000;
virtuladdr=0;
physicaladdr=0;
*(mmu_tlb_base+(virtuladdr>>20))=(physicaladdr&0xfff00000)|MMU_SECDESC_WB;
virtuladdr=0xa0000000;
physicaladdr=0x56000000;
*(mmu_tlb_base+(virtuladdr>>20))=(physicaladdr&0xfff00000)|MMU_SECDESC;
virtuladdr=0xb0000000;
physicaladdr=0x30000000;
while(virtuladdr<0xb4000000)
{
*(mmu_tlb_base+(virtuladdr>>20))=(physicaladdr&0xfff00000)|MMU_SECDESC_WB;
virtuladdr+=0x100000;
physicaladdr+=0x100000;
}
}
void mmu_init(void)
{
unsigned long ttb=0x30000000;
__asm__(
"movr0,#0n"
"mcrp15,0,r0,c7,c7,0n"http://使無(wú)效ICches和DCaches
"mcrp15,0,r0,c7,c10,4n"http://drain write buffer on v4
"mcrp15,0,r0,c8,c7,0n"http://使無(wú)效指令,數(shù)據(jù)TLB
"movr4,%0n"http://r4=頁(yè)表基址
"mcrp15,0,r4,c2,c0,0n"http://設(shè)置頁(yè)表基址寄存器
"mvnr0,#0n"
"mcrp15,0,r0,c3,c0,0n"
//對(duì)控制寄存器,先讀出其值,在這基礎(chǔ)上修改位,然后在寫(xiě)入
"mrcp15,0,r0,c1,c0,0n"http://讀出控制就能器的值
//先清除不需要的位,往下若需要?jiǎng)t重新設(shè)置
"bicr0,r0,#0x3000n"http://.RVI ..RS B... .CAM
"bicr0,r0,#0x0300n"http://..11 ..... .... ....清除V.I位
"bicr0,r0,#0x0087n"http://.... .... 1... .111清除B/C/A/M
//設(shè)置需要的位
"orrr0,r0,#0x0002n"http://開(kāi)啟對(duì)其檢查
"orrr0,r0,#0x0004n"http://開(kāi)啟DCaches
"orrr0,r0,#0x1000n"http://開(kāi)啟ICaches
"orrr0,r0,#0x0001n"http://使能MMU
"mcrp15,0,r0,c1,c0,0n"http://將修改的值寫(xiě)入控制寄存器
://無(wú)輸出
:"r"(ttb) );
}
#define GPFCON(*(volatile unsigned long *)0xa0000050)
#define GPFDAT(*(volatile unsigned long *)0xa0000054)
#define GPF4_OUT(1<<(2*4))
#define GPF5_OUT(1<<(2*5))
#define GPF6_OUT(1<<(2*6))
static inline void wait(volatile unsigned long dly)
{
for(;dly>0;dly--);
}
int main(void)
{
unsigned long i=0;
GPFCON=GPF4_OUT|GPF5_OUT|GPF6_OUT;
while(1)
{
wait(30000);
GPFDAT=(~(i<<4));
if(++i==8)
i=0;
}
return 0;
}
mmu.lds:
SECTIONS{
firtst 0x00000000 : { head.o init.o}
second 0xb0004000 : AT(2048) { leds.o }
}
Makefile:
objs := head.o init.o leds.o
mmu.bin: $(objs)
arm-linux-ld -Tmmu.lds -o mmu_elf $^
arm-linux-objcopy -O binary -S mmu_elf $@
arm-linux-objdump -D -m arm mmu_elf > mmu.dis
%.o:%.c
arm-linux-gcc -Wall -O2 -c -o $@ $<
%.o:%.S
arm-linux-gcc -Wall -O2 -c -o $@ $<
clean:
rm -f mmu.bin mmu_elf mmu.dis *.o