;=======================================================================
; 哈哈,下面又有看头了,这个初始化程序好像被名曰hzh的高手改过
; 能在NOR NAND 还有内存中运行,当然了,在内存中运行最简单了.
; 在NOR NAND中运行的话都要先把自己拷到内存中.
; 此外,还记得上面提到的|Image$$RO$$Base|,|Image$$RO$$Limit|...吗?
; 这就是拷贝的依据了!!!
;=========================================================================
;BWSCON的[2:1]反映了外部引脚OM[1:0]:若OM[1:0] != 00, 从NOR FLash启动或直接在内存运行;若OM[1:0]==00,则为Nand Flash Mode
ldr r0, =BWSCON
ldr r0, [r0]
ands r0, r0, #6 ; #6 == 0110 --> BWSCON[2:1]
bne copy_proc_beg ;OM[1:0] != 00,NOR FLash boot,不读取NAND FLASH
adr r0, ResetEntry ;否则,OM[1:0] == 0, 为从NAND FLash启动
cmp r0, #0 ;再比较入口是否为0地址处
;如果是0才是真正从NAND 启动,因为其4k被复制到0地址开始的stepingstone 内部sram中
; 注意adr得到的是 相对 地址,非绝对地址 == if use Multi-ice,
bne copy_proc_beg ;如果!=0,说明在using ice, 这种情况也不读取NAND FLASH. don't read nand flash for boot
;nop
;==============这一段代码完成从NAND Flash读代码到RAM=====================
nand_boot_beg ;
mov r5, #NFCONF ;首先设定NAND的一些控制寄存器
;set timing value
ldr r0, =(7<<12)|(7<<8)|(7<<4)
str r0, [r5]
;enable control
ldr r0, =(0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0)
str r0, [r5, #4]
bl ReadNandID ;按着读取NAND的ID号,结果保存在r5里
mov r6, #0 ;r6设初值0.
ldr r0, =0xec73 ;期望的NAND ID号
cmp r5, r0 ;这里进行比较
beq � ;相等的话就跳到下一个1标号处
ldr r0, =0xec75 ;这是另一个期望值
cmp r5, r0
beq � ;相等的话就跳到下一个1标号处
mov r6, #1 ;不相等,设置r6=1.
1
bl ReadNandStatus ;读取NAND状态,结果放在r1里
mov r8, #0 ; r8设初值0,意义为页号
ldr r9, =ResetEntry ; r9设初值为初始化程序入口地址
; 注意,在这里使用的是ldr伪指令,而不是上面用的adr伪指令,它加载的是ResetEntry
; 的绝对地址,也就是我们期望的RAM中的地址,在这里,它和|Image$$RO$$Base|一样
; 也就是说,我如我们编译程序时RO base指定的地址在RAM里,而把生成的文件拷到
; NAND里运行,由ldr加载的r9的值还是定位在内存. ???
2
ands r0, r8, #0x1f ;凡r8为0x1f(32)的整数倍-1,eq有效,ne无效
bne � ;这句的意思是对每个块(32页)进行检错 -- 在每个块的开始页进行
mov r0, r8 ;r8->r0
bl CheckBadBlk ;检查NAND的坏区
cmp r0, #0 ;比较r0和0
addne r8, r8, #32 ;存在坏块的话就跳过这个坏块: + 32得到下一块. 故: r8 = blockpage addr,因为读写是按页进行的(每页512Byte)
bne � ;然后跳到4进行循环条件判断。没有的话就跳到标号3处copy当前页
3
mov r0, r8 ;当前页号->r0
mov r1, r9 ;当前目标地址->r1
bl ReadNandPage ;读取该页的NAND数据到RAM
add r9, r9, #512 ;每一页的大小是512Bytes
add r8, r8, #1 ;r8指向下一页
4
cmp r8, #256 ;比较是否读完256页即128KBytes
;注意:这说明此程序默认拷贝128KByte的代码(by Tinko)
bcc � ;如果r8小于256(没读完),就返回前面的标号2处
; now copy completed
mov r5, #NFCONF ;Disable NandFlash
ldr r0, [r5, #4]
bic r0, r0, #1
str r0, [r5, #4]
ldr pc, =copy_proc_beg ;调用copy_proc_beg
;个人认为应该为InitRam ?????????????????????????????
;===========================================================
copy_proc_beg
adrl r0, ResetEntry ;ResetEntry值->r0
;这里应该注意,使用的是adr,而不是ldr。使用ldr说明ResetEntry是个绝对地址,这个地址是在程序链接的时候
;确定的。而使用adr则说明ResetEntry的地址和当前代码的执行位置有关,它是一个相对的地址。比如这段代码
;在stepingstone里面执行,那么ResetEntry的地址就是零。如果在RAM里执行,那么ResetEntry就应是RAM的一个
;地址,应该等于RO base。
ldr r2, BaseOfROM ;BaseOfROM值(后面有定义)->r2
cmp r0, r2 ;比较 ResetEntry 和 BaseOfROM
ldreq r0, TopOfROM ;如果相等的话(在内存运行 --- ice -- 无需复制code区中的ro段,但需要复制code区中的rw段),TopOfROM->r0
beq InitRam ;同时跳到InitRam
;否则,下面开始复制code的RO段
;=========================================================
;下面这个是针对代码在NOR FLASH时的拷贝方法
;功能为把从ResetEntry起,TopOfROM-BaseOfROM大小的数据拷到BaseOfROM
;TopOfROM和BaseOfROM为|Image$$RO$$Limit|和|Image$$RO$$Base|
;|Image$$RO$$Limit|和|Image$$RO$$Base|由连接器生成
;为生成的代码的代码段运行时的起启和终止地址
;BaseOfBSS和BaseOfZero为|Image$$RW$$Base|和|Image$$ZI$$Base|
;|Image$$RW$$Base|和|Image$$ZI$$Base|也是由连接器生成
;两者之间就是初始化数据的存放地
; --在加载阶段,不存在ZI区域--
;=======================================================
ldr r3, TopOfROM
0
ldmia r0!, {r4-r7} ;开始时,r0 = ResetEntry --- source
stmia r2!, {r4-r7} ;开始时,r2 = BaseOfROM --- destination
cmp r2, r3 ;终止条件:复制了TopOfROM-BaseOfROM大小
bcc �
;---------------------------------------------------------------
; 下面2行,根据理解,由tinko添加
; 猜测上面的代码不应该用" ! ",以至于地址被修改。这里重新赋值
;---------------------------------------------------------------
adrl r0, ResetEntry ;don't use adr, 'cause out of range error occures
ldr r2, BaseOfROM
;旨在计算出正确的RW区起始位置
; 下面2行目的是为了计算正确的r0(必须使之指向code区中的rw域开始处)
sub r2, r2, r3 ;r2=BaseOfROM-TopOfROM=(-)代码长度
sub r0, r0, r2 ;r0=ResetEntry-(-)代码长度=ResetEntry+代码长度
InitRam
;复制代码加载位置中的RM区到|Image$$RW$$Base|
ldr r2, BaseOfBSS ;BaseOfBSS->r2 , BaseOfBSS = |Image$$RW$$Base|
ldr r3, BaseOfZero ;BaseOfZero->r3 , BaseOfZero = |Image$$ZI$$Base|
0
cmp r2, r3 ;比较BaseOfBSS和BaseOfZero
ldrcc r1, [r0], #4 ;当代码在内存中运行时,r0(初始值) = TopOfROM.这之后的BaseOfZero-BaseOfBSS仍属于code,需拷贝到BaseOfBSS
strcc r1, [r2], #4
bcc �
;用0初始化ZI区
mov r0, #0
ldr r3, EndOfBSS ;EndOfBSS = |Image$$ZI$$Limit|
1
cmp r2, r3
strcc r0, [r2], #4
bcc �
;要是r21 ; means Fclk:Hclk is not 1:1.
; bl MMU_SetAsyncBusMode
; |
; bl MMU_SetFastBusMode ; default value.
; ]
;bl Led_Test
;===========================================================
; 进入C语言前的最后一步了,就是把我们用说查二级向量表
; 的中断例程安装到一级向量表(异常向量表)里.
;//5.设置缺省中断处理函数
; Setup IRQ handler
ldr r0,=HandleIRQ ;This routine is needed
ldr r1,=IsrIRQ ;if there isn't 'subs pc,lr,#4' at 0x18, 0x1c
str r1,[r0]
;//initialize the IRQ 将普通中断判断程序的入口地址给HandleIRQ
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;注意,以下这段可能不需要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;//6.将数据段拷贝到ram中 将零初始化数据段清零跳入C语言的main函数执行到这步结束bootloader初步引导结束
;If main() is used, the variable initialization will be done in __main().
[ {FALSE} ;by tinko -- 最外面的条件由tinko添加,实际上不再执行这段
[ :LNOT:USE_MAIN ;initialized {FALSE}
;Copy and paste RW data/zero initialized data
LDR r0, =|Image$$RO$$Limit| ; Get pointer to ROM data
LDR r1, =|Image$$RW$$Base| ; and RAM copy
LDR r3, =|Image$$ZI$$Base|
;Zero init base => top of initialised data
CMP r0, r1 ; Check that they are different just for debug??????????????????????????
BEQ �
1
CMP r1, r3 ; Copy init data
LDRCC r2, [r0], #4 ;--> LDRCC r2, [r0] + ADD r0, r0, #4
STRCC r2, [r1], #4 ;--> STRCC r2, [r1] + ADD r1, r1, #4
BCC �
2
LDR r1, =|Image$$ZI$$Limit| ; Top of zero init segment
MOV r2, #0
3
CMP r3, r1 ; Zero init
STRCC r2, [r3], #4
BCC �
]
]
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;***************************************
;by tinko
[ {TRUE} ;得有些表示了,该点点LED灯了
;rGPFDAT = (rGPFDAT & ~(0xf<<4)) | ((~data & 0xf)<<4);
; Led_Display
ldr r0,=GPFCON
ldr r1,=0x5500
str r1,[r0]
ldr r0,=GPFDAT
ldr r1,=0xe0
str r1,[r0]
ldr r2, =0xffffffff;
1
sub r2,r2,#1
bne �
ldr r0,=GPFDAT
ldr r1,=0xe0
;b . ;die here
]
;*****************************************
;*****************************************************************************
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
; 妈呀,终说见到艳阳天了!!!!!!!!!!
; 跳到C语言的main函数处了.
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;*****************************************************************************
[ :LNOT:THUMBCODE ;if thumbcode={false} bl main L代表logic变量
bl Main ;Don't use main() because ......
b . ;注意小圆点
]
;//if thumbcod={ture}
[ THUMBCODE ;for start-up code for Thumb mode
orr lr,pc,#1
bx lr
CODE16
bl Main ;Don't use main() because ......
b . ;注意小圆点
CODE32
]
;function initializing stacks
InitStacks
;Don't use DRAM,such as stmfd,ldmfd......
;SVCstack is initialized before
;Under toolkit ver 2.5, 'msr cpsr,r1' can be used instead of 'msr cpsr_cxsf,r1'
mrs r0,cpsr
bic r0,r0,#MODEMASK
orr r1,r0,#UNDEFMODE|NOINT
msr cpsr_cxsf,r1 ;UndefMode
ldr sp,=UndefStack ; UndefStack=0x33FF_5C00
orr r1,r0,#ABORTMODE|NOINT
msr cpsr_cxsf,r1 ;AbortMode
ldr sp,=AbortStack ; AbortStack=0x33FF_6000
orr r1,r0,#IRQMODE|NOINT
msr cpsr_cxsf,r1 ;IRQMode
ldr sp,=IRQStack ; IRQStack=0x33FF_7000
orr r1,r0,#FIQMODE|NOINT
msr cpsr_cxsf,r1 ;FIQMode
ldr sp,=FIQStack ; FIQStack=0x33FF_8000
bic r0,r0,#MODEMASK|NOINT
orr r1,r0,#SVCMODE
msr cpsr_cxsf,r1 ;SVCMode
ldr sp,=SVCStack ; SVCStack=0x33FF_5800
;USER mode has not be initialized.
;//为什么不用初始化user的stacks,系统刚启动的时候运行在哪个模式下?
mov pc,lr
;The LR register won't be valid if the current mode is not SVC mode.?
;//系统一开始运行就是SVCmode?
;===========================================================
ReadNandID
mov r7,#NFCONF
ldr r0,[r7,#4] ;NFChipEn();
bic r0,r0,#2
str r0,[r7,#4]
mov r0,#0x90 ;WrNFCmd(RdIDCMD);
strb r0,[r7,#8]
mov r4,#0 ;WrNFAddr(0);
strb r4,[r7,#0xc]
1 ;while(NFIsBusy());
ldr r0,[r7,#0x20]
tst r0,#1
beq �
ldrb r0,[r7,#0x10] ;id = RdNFDat()<<8;
mov r0,r0,lsl #8
ldrb r1,[r7,#0x10] ;id |= RdNFDat();
orr r5,r1,r0
ldr r0,[r7,#4] ;NFChipDs();
orr r0,r0,#2
str r0,[r7,#4]
mov pc,lr
ReadNandStatus
mov r7,#NFCONF
ldr r0,[r7,#4] ;NFChipEn();
bic r0,r0,#2
str r0,[r7,#4]
mov r0,#0x70 ;WrNFCmd(QUERYCMD);
strb r0,[r7,#8]
ldrb r1,[r7,#0x10] ;r1 = RdNFDat();
ldr r0,[r7,#4] ;NFChipDs();
orr r0,r0,#2
str r0,[r7,#4]
mov pc,lr
WaitNandBusy
mov r0,#0x70 ;WrNFCmd(QUERYCMD);
mov r1,#NFCONF
strb r0,[r1,#8]
1 ;while(!(RdNFDat()&0x40));
ldrb r0,[r1,#0x10]
tst r0,#0x40
beq �
mov r0,#0 ;WrNFCmd(READCMD0);
strb r0,[r1,#8]
mov pc,lr
CheckBadBlk
mov r7, lr
mov r5, #NFCONF
bic r0,r0,#0x1f ;addr &= ~0x1f;
ldr r1,[r5,#4] ;NFChipEn()
bic r1,r1,#2
str r1,[r5,#4]
mov r1,#0x50 ;WrNFCmd(READCMD2)
strb r1,[r5,#8]
mov r1, #5;6 ;6->5
strb r1,[r5,#0xc] ;WrNFAddr(5);(6) 6->5
strb r0,[r5,#0xc] ;WrNFAddr(addr)
mov r1,r0,lsr #8 ;WrNFAddr(addr>>8)
strb r1,[r5,#0xc]
cmp r6,#0 ;if(NandAddr)
movne r0,r0,lsr #16 ;WrNFAddr(addr>>16)
strneb r0,[r5,#0xc]
; bl WaitNandBusy ;WaitNFBusy()
;do not use WaitNandBusy, after WaitNandBusy will read part A!
mov r0, #100
1
subs r0, r0, #1
bne �
2
ldr r0, [r5, #0x20]
tst r0, #1
beq �
ldrb r0, [r5,#0x10] ;RdNFDat()
sub r0, r0, #0xff
mov r1,#0 ;WrNFCmd(READCMD0)
strb r1,[r5,#8]
ldr r1,[r5,#4] ;NFChipDs()
orr r1,r1,#2
str r1,[r5,#4]
mov pc, r7
ReadNandPage
mov r7,lr
mov r4,r1
mov r5,#NFCONF
ldr r1,[r5,#4] ;NFChipEn()
bic r1,r1,#2
str r1,[r5,#4]
mov r1,#0 ;WrNFCmd(READCMD0)
strb r1,[r5,#8]
strb r1,[r5,#0xc] ;WrNFAddr(0)
strb r0,[r5,#0xc] ;WrNFAddr(addr)
mov r1,r0,lsr #8 ;WrNFAddr(addr>>8)
strb r1,[r5,#0xc]
cmp r6,#0 ;if(NandAddr)
movne r0,r0,lsr #16 ;WrNFAddr(addr>>16)
strneb r0,[r5,#0xc]
ldr r0,[r5,#4] ;InitEcc()
orr r0,r0,#0x10
str r0,[r5,#4]
bl WaitNandBusy ;WaitNFBusy()
mov r0,#0 ;for(i=0; i<512; i++)
1
ldrb r1,[r5,#0x10] ;buf[i] = RdNFDat()
strb r1,[r4,r0]
add r0,r0,#1
bic r0,r0,#0x10000
cmp r0,#0x200
bcc �
ldr r0,[r5,#4] ;NFChipDs()
orr r0,r0,#2
str r0,[r5,#4]
mov pc,r7
;--------------------LED test
EXPORT Led_Test
Led_Test
mov r0, #0x56000000
mov r1, #0x5500
str r1, [r0, #0x50]
0
mov r1, #0x50
str r1, [r0, #0x54]
mov r2, #0x100000
1
subs r2, r2, #1
bne �
mov r1, #0xa0
str r1, [r0, #0x54]
mov r2, #0x100000
2
subs r2, r2, #1
bne �
b �
mov pc, lr
;===========================================================
;=====================================================================
; Clock division test
; Assemble code, because VSYNC time is very short
;=====================================================================
EXPORT CLKDIV124
EXPORT CLKDIV144
CLKDIV124
ldr r0, = CLKDIVN
ldr r1, = 0x3 ; 0x3 = 1:2:4
str r1, [r0]
; wait until clock is stable
nop
nop
nop
nop
nop
ldr r0, = REFRESH
ldr r1, [r0]
bic r1, r1, #0xff
bic r1, r1, #(0x7<<8)
orr r1, r1, #0x470 ; REFCNT135
str r1, [r0]
nop
nop
nop
nop
nop
mov pc, lr
CLKDIV144
ldr r0, = CLKDIVN
ldr r1, = 0x4 ; 0x4 = 1:4:4
str r1, [r0]
; wait until clock is stable
nop
nop
nop
nop
nop
ldr r0, = REFRESH
ldr r1, [r0]
bic r1, r1, #0xff
bic r1, r1, #(0x7<<8)
orr r1, r1, #0x630 ; REFCNT675 - 1520
str r1, [r0]
nop
nop
nop
nop
nop
mov pc, lr
;存储器控制寄存器的定义区
LTORG
SMRDATA DATA
; Memory configuration should be optimized for best performance
; The following parameter is not optimized.
; Memory access cycle parameter strategy
; 1) The memory settings is safe parameters even at HCLK=75Mhz.
; 2) SDRAM refresh period is for HCLK<=75Mhz.
DCD (0+(B1_BWSCON<<4)+(B2_BWSCON<<8)+ (B3_BWSCON<<12)+(B4_BWSCON<<16)+(B5_BWSCON<<20)+ (B6_BWSCON<<24)+(B7_BWSCON<<28)) ;各bank的bus width; 没有B0,因为由 OM[1:0]pins 确定
DCD ((B0_Tacs<<13)+(B0_Tcos<<11)+(B0_Tacc<<8)+(B0_Tcoh<<6)+(B0_Tah<<4)+(B0_Tacp<<2)+(B0_PMC)) ;GCS0
DCD ((B1_Tacs<<13)+(B1_Tcos<<11)+(B1_Tacc<<8)+(B1_Tcoh<<6)+(B1_Tah<<4)+(B1_Tacp<<2)+(B1_PMC)) ;GCS1
DCD ((B2_Tacs<<13)+(B2_Tcos<<11)+(B2_Tacc<<8)+(B2_Tcoh<<6)+(B2_Tah<<4)+(B2_Tacp<<2)+(B2_PMC)) ;GCS2
DCD ((B3_Tacs<<13)+(B3_Tcos<<11)+(B3_Tacc<<8)+(B3_Tcoh<<6)+(B3_Tah<<4)+(B3_Tacp<<2)+(B3_PMC)) ;GCS3
DCD ((B4_Tacs<<13)+(B4_Tcos<<11)+(B4_Tacc<<8)+(B4_Tcoh<<6)+(B4_Tah<<4)+(B4_Tacp<<2)+(B4_PMC)) ;GCS4
DCD ((B5_Tacs<<13)+(B5_Tcos<<11)+(B5_Tacc<<8)+(B5_Tcoh<<6)+(B5_Tah<<4)+(B5_Tacp<<2)+(B5_PMC)) ;GCS5
DCD ((B6_MT<<15)+(B6_Trcd<<2)+(B6_SCAN)) ;GCS6 B6_MT定义在memcfg.inc中,11-->SDRAM ; B6_SCAN - 非reset 默认值
DCD ((B7_MT<<15)+(B7_Trcd<<2)+(B7_SCAN)) ;GCS7
DCD ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Trc<<18)+(Tchr<<16)+REFCNT) ;Tchr- not used
;DCD 0x32 ;SCLK power saving mode, BANKSIZE 128M/128M
DCD 0x31 ;SCLK power saving mode, BANKSIZE 64M/64M
DCD 0x30 ;MRSR6 CL=3clk
DCD 0x30 ;MRSR7 CL=3clk
BaseOfROM DCD |Image$$RO$$Base|
TopOfROM DCD |Image$$RO$$Limit|
BaseOfBSS DCD |Image$$RW$$Base|
BaseOfZero DCD |Image$$ZI$$Base|
EndOfBSS DCD |Image$$ZI$$Limit|
ALIGN
AREA RamData, DATA, READWRITE
^ _ISR_STARTADDRESS ; _ISR_STARTADDRESS=0x33FF_FF00
HandleReset # 4
HandleUndef # 4
HandleSWI # 4
HandlePabort # 4
HandleDabort # 4
HandleReserved # 4
HandleIRQ # 4
HandleFIQ # 4
;Don't use the label 'IntVectorTable',
;The value of IntVectorTable is different with the address you think it may be.
;IntVectorTable
;@0x33FF_FF20
HandleEINT0 # 4
HandleEINT1 # 4
HandleEINT2 # 4
HandleEINT3 # 4
HandleEINT4_7 # 4
HandleEINT8_23 # 4
HandleCAM # 4 ; Added for 2440.
HandleBATFLT # 4
HandleTICK # 4
HandleWDT # 4
HandleTIMER0 # 4
HandleTIMER1 # 4
HandleTIMER2 # 4
HandleTIMER3 # 4
HandleTIMER4 # 4
HandleUART2 # 4
;@0x33FF_FF60
HandleLCD # 4
HandleDMA0 # 4
HandleDMA1 # 4
HandleDMA2 # 4
HandleDMA3 # 4
HandleMMC # 4
HandleSPI0 # 4
HandleUART1 # 4
HandleNFCON # 4 ; Added for 2440.
HandleUSBD # 4
HandleUSBH # 4
HandleIIC # 4
HandleUART0 # 4
HandleSPI1 # 4
HandleRTC # 4
HandleADC # 4
;@0x33FF_FFA0
END
; HISTORY:
; 2002.02.25:kwtark: ver 0.0
; 2002.03.20:purnnamu: Add some functions for testing STOP,Sleep mode
; 2003.03.14:DonGo: Modified for 2440.
; 2009 06.24:Tinko Modified
关键字:启动代码
引用地址:
关于启动代码2440init.s(三)