.text
.global _start
_start:
ldrsp,=4096@設置棧指針,以下都是C函數(shù),調(diào)用前需要設好棧
bldisable_watch_dog@關閉WATCHDOG,否則CPU會不斷重啟
blmemsetup@設置好存儲控制器以使用SDRAM
blcopy_2th_to_sdram@將第二部分代碼復制到SDRAM
blcreate_page_table@設置頁表
blmmu_init@啟動MMU
ldrsp,=0xb4000000@重設棧指針,指向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;//關閉WATCHDOG,WATCHDOG寄存器設為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)//訪問權限
#define MMU_DOMAIN(0<<5)//屬于那個域
#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://使無效ICches和DCaches
"mcrp15,0,r0,c7,c10,4n"http://drain write buffer on v4
"mcrp15,0,r0,c8,c7,0n"http://使無效指令,數(shù)據(jù)TLB
"movr4,%0n"http://r4=頁表基址
"mcrp15,0,r4,c2,c0,0n"http://設置頁表基址寄存器
"mvnr0,#0n"
"mcrp15,0,r0,c3,c0,0n"
//對控制寄存器,先讀出其值,在這基礎上修改位,然后在寫入
"mrcp15,0,r0,c1,c0,0n"http://讀出控制就能器的值
//先清除不需要的位,往下若需要則重新設置
"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
//設置需要的位
"orrr0,r0,#0x0002n"http://開啟對其檢查
"orrr0,r0,#0x0004n"http://開啟DCaches
"orrr0,r0,#0x1000n"http://開啟ICaches
"orrr0,r0,#0x0001n"http://使能MMU
"mcrp15,0,r0,c1,c0,0n"http://將修改的值寫入控制寄存器
://無輸出
:"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