nandflash裸機(jī)程序分析
它包含7個(gè)文件:
head.S
init.c
main.c
Makefile
nand.c
nand.lds
我們之前的程序都是在nandflash的前4k放代碼,上電后自動(dòng)拷貝到SRAM中,之后將SRAM中的代碼拷貝到SDRAM中??墒钱?dāng)我們的程序太大超過(guò)4k的時(shí)候就不行了,因?yàn)闊o(wú)法將nandflash的代碼完全拷貝到SRAM中去,這時(shí)就需要從nandflash中拷貝代碼了。
本程序里面我們要實(shí)現(xiàn)的就是:將一部分代碼放在nandflash的4096之后,上電后,前面一部分代碼自動(dòng)拷貝到SRAM中,在這段代碼里面將nandflash的4096之后的程序拷貝到SDRAM中。
我們從入口函數(shù)開(kāi)始分析:
@******************************************************************************
@ File:head.s
@ 功能:設(shè)置SDRAM,將程序復(fù)制到SDRAM,然后跳到SDRAM繼續(xù)執(zhí)行
@******************************************************************************
.text
.global _start
_start:
@函數(shù)disable_watch_dog, memsetup, init_nand, nand_read_ll在init.c中定義
ldr sp, =4096 @設(shè)置堆棧
bl disable_watch_dog @關(guān)WATCH DOG
bl memsetup @初始化SDRAM,主要是設(shè)置控制SDRAM的13個(gè)寄存器
bl nand_init @初始化NAND Flas,見(jiàn)注釋1
@將NAND Flash中地址4096開(kāi)始的1024字節(jié)代碼(main.c編譯得到)復(fù)制到SDRAM中
@nand_read_ll函數(shù)需要3個(gè)參數(shù):
ldr r0, =0x30000000 @1. 目標(biāo)地址=0x30000000,這是SDRAM的起始地址
mov r1, #4096 @2. 源地址 = 4096,連接的時(shí)候,main.c中的代碼都存在NAND Flash地址4096開(kāi)始處
mov r2, #2048 @3. 復(fù)制長(zhǎng)度= 2048(bytes),對(duì)于本實(shí)驗(yàn)的main.c,這是足夠了
bl nand_read @調(diào)用C函數(shù)nand_read,見(jiàn)注釋2
ldr sp, =0x34000000 @設(shè)置棧
ldr lr, =halt_loop @設(shè)置返回地址
ldr pc, =main @b指令和bl指令只能前后跳轉(zhuǎn)32M的范圍,所以這里使用向pc賦值的方法進(jìn)行跳轉(zhuǎn)
halt_loop:
b halt_loop
注釋1:
void nand_init(void)
{
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0
/* 判斷是S3C2410還是S3C2440 */
if ((GSTATUS1 == 0x32410000) || (GSTATUS1 == 0x32410002))//這里表示是2410
{
nand_chip.nand_reset = s3c2410_nand_reset;
nand_chip.wait_idle = s3c2410_wait_idle;
nand_chip.nand_select_chip = s3c2410_nand_select_chip;
nand_chip.nand_deselect_chip = s3c2410_nand_deselect_chip;
nand_chip.write_cmd = s3c2410_write_cmd;
nand_chip.write_addr = s3c2410_write_addr;
nand_chip.read_data = s3c2410_read_data;
/* 使能NAND Flash控制器, 初始化ECC, 禁止片選, 設(shè)置時(shí)序 */
s3c2410nand->NFCONF = (1<<15)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0);
}
else
{ /* 定義了各種操作函數(shù) */
nand_chip.nand_reset = s3c2440_nand_reset;//見(jiàn)注釋1-1
nand_chip.wait_idle = s3c2440_wait_idle; //見(jiàn)注釋1-2
nand_chip.nand_select_chip = s3c2440_nand_select_chip;//見(jiàn)注釋1-3
nand_chip.nand_deselect_chip = s3c2440_nand_deselect_chip;//見(jiàn)注釋1-4
nand_chip.write_cmd = s3c2440_write_cmd;//見(jiàn)注釋1-5
#ifdef LARGER_NAND_PAGE
nand_chip.write_addr = s3c2440_write_addr_lp;//大頁(yè)寫(xiě)地址,見(jiàn)注釋1-6
#else
nand_chip.write_addr = s3c2440_write_addr;//小頁(yè)寫(xiě)地址,見(jiàn)注釋1-7
#endif
nand_chip.read_data = s3c2440_read_data;//讀數(shù)據(jù),見(jiàn)注釋1-8
/* 設(shè)置時(shí)序 */
s3c2440nand->NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4);//這個(gè)東東在linux編程里面說(shuō)過(guò)了,不再重復(fù)
/* 使能NAND Flash控制器, 初始化ECC, 禁止片選 */
s3c2440nand->NFCONT = (1<<4)|(1<<1)|(1<<0);
}
/* 復(fù)位NAND Flash */
nand_reset();//見(jiàn)注釋1-9
}
注釋1-1:
/* 復(fù)位 */
static void s3c2440_nand_reset(void)
{
s3c2440_nand_select_chip(); //選中芯片
s3c2440_write_cmd(0xff); // ffh是復(fù)位命令,將這個(gè)命令寫(xiě)入命令寄存器即可
s3c2440_wait_idle(); //等待就緒
s3c2440_nand_deselect_chip(); //不選中芯片
}
注釋1-2:
/* 等待NAND Flash就緒 */
static void s3c2440_wait_idle(void)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFSTAT;
while(!(*p & BUSY))//根據(jù)NFSTAT第0位判斷是否就緒,1表示就緒
for(i=0; i<10; i++);
}
注釋1-3:
/* 發(fā)出片選信號(hào) */
static void s3c2440_nand_select_chip(void)
{
int i;
s3c2440nand->NFCONT &= ~(1<<1);//NFCONT寄存器的第1位用于選中芯片,0表示選中
for(i=0; i<10; i++);
}
見(jiàn)注釋1-4:
/* 取消片選信號(hào) */
static void s3c2440_nand_deselect_chip(void)
{
s3c2440nand->NFCONT |= (1<<1);//參考注釋1-3
}
注釋1-5:
/* 發(fā)出命令 */
static void s3c2440_write_cmd(int cmd)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFCMD;
*p = cmd;//就是將命令寫(xiě)到命令寄存器里面,很簡(jiǎn)單
}
注釋1-6:
/* 大頁(yè)寫(xiě)地址,分5個(gè)周期寫(xiě)入 */
static void s3c2440_write_addr_lp(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR;
int col, page;
//#define NAND_SECTOR_SIZE_LP 2048
//#define NAND_BLOCK_MASK_LP (NAND_SECTOR_SIZE_LP - 1)
col = addr & NAND_BLOCK_MASK_LP; //收下地址前11位,屏蔽高位
page = addr / NAND_SECTOR_SIZE_LP;//屏蔽地址前11位,收下高位
/* 分5個(gè)周期將地址寫(xiě)到地址寄存器里面 */
*p = col & 0xff; /* Column Address A0~A7 */
for(i=0; i<10; i++);
*p = (col >> 8) & 0x0f; /* Column Address A8~A11 */
for(i=0; i<10; i++);
*p = page & 0xff; /* Row Address A12~A19 */
for(i=0; i<10; i++);
*p = (page >> 8) & 0xff; /* Row Address A20~A27 */
for(i=0; i<10; i++);
*p = (page >> 16) & 0x03;/* Row Address A28~A29 */
for(i=0; i<10; i++);
}
注釋1-7:
/*小頁(yè)寫(xiě)地址,分4個(gè)周期寫(xiě)入*/
static void s3c2440_write_addr(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR;
*p = addr & 0xff;
for(i=0; i<10; i++);
*p = (addr >> 9) & 0xff;
for(i=0; i<10; i++);
*p = (addr >> 17) & 0xff;
for(i=0; i<10; i++);
*p = (addr >> 25) & 0xff;
for(i=0; i<10; i++);
}
注釋1-8:
/* 讀取數(shù)據(jù) */
static unsigned char s3c2440_read_data(void)
{
/* 很簡(jiǎn)單,就是將數(shù)據(jù)從數(shù)據(jù)寄存器里面讀出來(lái) */
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFDATA;
return *p;
}
注釋1-