ARM笔记: MMU单元控制器
扫描二维码
随时随地手机看文章
.text
.global _start
_start:
ldrsp,=4096@设置栈指针,以下都是C函数,调用前需要设好栈
bldisable_watch_dog@关闭WATCHDOG,否则CPU会不断重启
blmemsetup@设置好存储控制器以使用SDRAM
blcopy_2th_to_sdram@将第二部分代码复制到SDRAM
blcreate_page_table@设置页表
blmmu_init@启动MMU
ldrsp,=0xb4000000@重设栈指针,指向SDRAM顶端(使用虚拟地址)
ldrpc,=0xb0004000@跳到SDRAM中继续执行第二部份代码
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"//使无效ICches和DCaches
"mcrp15,0,r0,c7,c10,4n"//drain write buffer on v4
"mcrp15,0,r0,c8,c7,0n"//使无效指令,数据TLB
"movr4,%0n"//r4=页表基址
"mcrp15,0,r4,c2,c0,0n"//设置页表基址寄存器
"mvnr0,#0n"
"mcrp15,0,r0,c3,c0,0n"
//对控制寄存器,先读出其值,在这基础上修改位,然后在写入
"mrcp15,0,r0,c1,c0,0n"//读出控制就能器的值
//先清除不需要的位,往下若需要则重新设置
"bicr0,r0,#0x3000n"//.RVI ..RS B... .CAM
"bicr0,r0,#0x0300n"//..11 ..... .... ....清除V.I位
"bicr0,r0,#0x0087n"//.... .... 1... .111清除B/C/A/M
//设置需要的位
"orrr0,r0,#0x0002n"//开启对其检查
"orrr0,r0,#0x0004n"//开启DCaches
"orrr0,r0,#0x1000n"//开启ICaches
"orrr0,r0,#0x0001n"//使能MMU
"mcrp15,0,r0,c1,c0,0n"//将修改的值写入控制寄存器
://无输出
:"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