s3c2440硬件篇之五:讀NandFlash(2)
接上一篇:s3c2440硬件篇之三:NandFlash(1)介紹
s3c2440讀NAND Flash的步驟:
①設(shè)置NFCONF
在HCLK=100Mhz的情況下,TACLS=0,TWRPH0=3,TWRPH1=0,則
NFCONF = 0x300
使能NAND Flash控制器、禁止控制引腳信號(hào)nFCE,初始化ECC
NFCONT = (1<<4) | (1<<1) | (1<<0)
②操作NAND Flash前,復(fù)位
NFCONT &= ~(1<<1) 發(fā)出片選信號(hào)
NFCMD = 0xff reset命令
然后循環(huán)查詢NFSTAT位0,直到等于1,處于就緒態(tài)
最后禁止片選信號(hào),在實(shí)際使用時(shí)再使能
NFCONT |= 0x2 禁止NAND Flash
③發(fā)出讀命令
NFCONT &= ~(1<<1) 發(fā)出片選信號(hào)
NFCMD = 0 讀命令
④發(fā)出地址信號(hào)
⑤循環(huán)查詢NFSTAT,直到等于1
⑥連續(xù)讀NFDATA寄存器,得到一頁(yè)數(shù)據(jù)
⑦最后禁止NAND Flash片選信號(hào)
NFCONT |= (1<<1)
Nand硬件連接:
代碼詳解:(參考韋東山大哥代碼)
說(shuō)明:本實(shí)例的目的是把一部分代碼存放在NAND Flash地址4096(即4K)之后,當(dāng)程序啟動(dòng)后通過(guò)NAND Flash控制器讀出代碼,執(zhí)行。
(1)連接腳本nand.lds
SECTIONS {
firtst 0x00000000:{ head.o init.o nand.o}
second 0x30000000:AT(4096){ main.o }
}
(2)head.S
@******************************************************************************
@ 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è)置堆棧
bldisable_watch_dog @關(guān)WATCH DOG
blmemsetup @初始化SDRAM
blnand_init @初始化NAND Flash
@將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的起始地址
movr1,#4096 @2.源地址=4096,連接的時(shí)候,main.c中的代碼都存在NAND Flash地址4096開(kāi)始處
movr2,#2048 @3.復(fù)制長(zhǎng)度=2048(bytes),對(duì)于本實(shí)驗(yàn)的main.c,這是足夠了
blnand_read @調(diào)用C函數(shù)nand_read
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
(3)init.c
/* WOTCH DOG register */
#defineWTCON(*(volatileunsignedlong*)0x53000000)
/* SDRAM regisers */
#defineMEM_CTL_BASE0x48000000
voiddisable_watch_dog();
voidmemsetup();
/*上電后,WATCH DOG默認(rèn)是開(kāi)著的,要把它關(guān)掉 */
voiddisable_watch_dog()
{
WTCON=0;
}
/* 設(shè)置控制SDRAM的13個(gè)寄存器 */
voidmemsetup()
{
inti=0;
unsignedlong*p=(unsignedlong*)MEM_CTL_BASE;
/* SDRAM 13個(gè)寄存器的值 */
unsignedlongconstmem_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
};
for(;i<13;i++)
p[i]=mem_cfg_val[i];
}
(4)nand.c ,定義一個(gè)宏#defineLARGER_NAND_PAGE來(lái)區(qū)分大頁(yè)小頁(yè)。
#defineLARGER_NAND_PAGE
#defineGSTATUS1(*(volatileunsignedint*)0x560000B0)
#defineBUSY 1
#defineNAND_SECTOR_SIZE 512
#defineNAND_BLOCK_MASK(NAND_SECTOR_SIZE-1)
#defineNAND_SECTOR_SIZE_LP 2048
#defineNAND_BLOCK_MASK_LP(NAND_SECTOR_SIZE_LP-1)
typedefunsignedintS3C24X0_REG32;
/* NAND FLASH (see S3C2410 manual chapter 6) */
typedefstruct{
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFECC;
}S3C2410_NAND;
/* NAND FLASH (see S3C2440 manual chapter 6, www.100ask.net) */
typedefstruct{
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCONT;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFMECCD0;
S3C24X0_REG32 NFMECCD1;
S3C24X0_REG32 NFSECCD;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFESTAT0;
S3C24X0_REG32 NFESTAT1;
S3C24X0_REG32 NFMECC0;
S3C24X0_REG32 NFMECC1;
S3C24X0_REG32 NFSECC;
S3C24X0_REG32 NFSBLK;
S3C24X0_REG32 NFEBLK;
}S3C2440_NAND;
typedefstruct{
void(*nand_reset)(void);
void(*wait_idle)(void);
void(*nand_select_chip)(void);
void(*nand_deselect_chip)(void);
void(*write_cmd)(intcmd);
void(*write_addr)(unsignedintaddr);
unsignedchar(*read_data)(void);
}t_nand_chip;
staticS3C2410_NAND*s3c2410nand=(S3C2410_NAND*)0x4e000000;
staticS3C2440_NAND*s3c2440nand=(S3C2440_NAND*)0x4e000000;
statict_nand_chip nand_chip;
/* 供外部調(diào)用的函數(shù) */
voidnand_init(void);
voidnand_read(unsignedchar*buf,unsignedlongstart_addr,intsize);
/* NAND Flash操作的總?cè)肟? 它們將調(diào)用S3C2410或S3C2440的相應(yīng)函數(shù) */
staticvoidnand_reset(void);
staticvoidwait_idle(void);
staticvoidnand_select_chip(void);
staticvoidnand_deselect_chip(void);
staticvoidwrite_cmd(intcmd);
staticvoidwrite_addr(unsignedintaddr);
staticunsignedcharread_data(void);
/* S3C2410的NAND Flash處理函數(shù) */
staticvoids3c2410_nand_reset(void);
staticvoids3c2410_wait_idle(void);
staticvoids3c2410_nand_select_chip(void);
staticvoids3c2410_nand_deselect_chip(void);
staticvoids3c2410_write_cmd(intcmd);
staticvoids3c2410_write_addr(unsignedintaddr);
staticunsignedchars3c2410_read_data();
/* S3C2440的NAND Flash處理函數(shù) */
staticvoids3c2440_nand_reset(void);
staticvoids3c2440_wait_idle(void);
staticvoids3c2440_nand_select_chip(void);
staticvoids3c2440_nand_deselect_chip(void);
staticvoids3c2440_write_cmd(intcmd);
staticvoids3c2440_write_addr(unsignedintaddr);
staticunsignedchars3c2440_read_data(void);
/* S3C2410的NAND Flash操作函數(shù) */
/* 復(fù)位 */
staticvoids3c2410_nand_reset(void)
{
s3c2410_nand_select_chip();
s3c2410_write_cmd(0xff);// 復(fù)位命令
s3c2410_wait_idle();
s3c2410_nand_deselect_chip();
}
/* 等待NAND Flash就緒 */
staticvoids3c2410_wait_idle(void)
{
inti;
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2410nand->NFSTAT;
while(!(*p&BUSY))
for(i=0;i<10;i++);
}
/* 發(fā)出片選信號(hào) */
staticvoids3c2410_nand_select_chip(void)
{
inti;
s3c2410nand->NFCONF&=~(1<<11);
for(i=0;i<10;i++);
}
/* 取消片選信號(hào) */
staticvoids3c2410_nand_deselect_chip(void)
{
s3c2410nand->NFCONF|=(1<<11);
}
/* 發(fā)出命令 */
staticvoids3c2410_write_cmd(intcmd)
{
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2410nand->NFCMD;
*p=cmd;
}
/* 發(fā)出地址 */
staticvoids3c2410_write_addr(unsignedintaddr)
{
inti;
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2410nand->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++);
}
/* 讀取數(shù)據(jù) */
staticunsignedchars3c2410_read_data(void)
{
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2410nand->NFDATA;
return*p;
}
/* S3C2440的NAND Flash操作函數(shù) */
/* 復(fù)位 */
staticvoids3c2440_nand_reset(void)
{
s3c2440_nand_select_chip();
s3c2440_write_cmd(0xff);// 復(fù)位命令
s3c2440_wait_idle();
s3c2440_nand_deselect_chip();
}
/* 等待NAND Flash就緒 */
staticvoids3c2440_wait_idle(void)
{
inti;
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2440nand->NFSTAT;
while(!(*p&BUSY))
for(i=0;i<10;i++);
}
/* 發(fā)出片選信號(hào) */
staticvoids3c2440_nand_select_chip(void)
{
inti;
s3c2440nand->NFCONT&=~(1<<1);
for(i=0;i<10;i++);
}
/* 取消片選信號(hào) */
staticvoids3c2440_nand_deselect_chip(void)
{
s3c2440nand->NFCONT|=(1<<1);
}
/* 發(fā)出命令 */
staticvoids3c2440_write_cmd(intcmd)
{
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2440nand->NFCMD;
*p=cmd;
}
/* 發(fā)出地址 */
staticvoids3c2440_write_addr(unsignedintaddr)
{
inti;
volatileunsignedchar*p=(volatileunsignedchar*)&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++);
}
staticvoids3c2440_write_addr_lp(unsignedintaddr)
{
inti;
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2440nand->NFADDR;
intcol,page;
col=addr&NAND_BLOCK_MASK_LP;
page=addr/NAND_SECTOR_SIZE_LP;
*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++);
}
/* 讀取數(shù)據(jù) */
staticunsignedchars3c2440_read_data(void)
{
volatileunsignedchar*p=(volatileunsignedchar*)&s3c2440nand->NFDATA;
return*p;
}
/* 在第一次使用NAND Flash前,復(fù)位一下NAND Flash */
staticvoidnand_reset(void)
{
nand_chip.nand_reset();
}
staticvoidwait_idle(void)
{
nand_chip.wait_idle();
}
staticvoidnand_select_chip(void)
{
inti;
nand_chip.nand_select_chip();
for(i=0;i<10;i++);
}
staticvoidnand_deselect_chip(void)
{
nand_chip.nand_deselect_chip();
}
staticvoidwrite_cmd(intcmd)
{
nand_chip.write_cmd(cmd);
}
staticvoidwrite_addr(unsignedintaddr)
{
nand_chip.write_addr(addr);
}
staticunsignedcharread_data(void)
{
returnnand_chip.read_data();
}
/* 初始化NAND Flash */
voidnand_init(void)
{
#defineTACLS 0
#defineTWRPH0 3
#defineTWRPH1 0
/* 判斷是S3C2410還是S3C2440 */
if((GSTATUS1==0x32410000)||(GSTATUS1==0x32410002))
{
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
{
nand_chip.nand_reset=s3c2440_nand_reset;
nand_chip.wait_idle=s3c2440_wait_idle;
nand_chip.nand_select_chip=s3c2440_nand_select_chip;
nand_chip.nand_deselect_chip=s3c2440_nand_deselect_chip;
nand_chip.write_cmd=s3c2440_write_cmd;
#ifdefLARGER_NAND_PAGE
nand_chip.write_addr=s3c2440_write_addr_lp;
#else
nand_chip.write_addr=s3c2440_write_addr;
#endif
nand_chip.read_data=s3c2440_read_data;
/* 設(shè)置時(shí)序 */
s3c2440nand->NFCONF=(TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4);
/* 使能NAND Flash控制器, 初始化ECC, 禁止片選 */
s3c2440nand->NFCONT=(1<<4)|(1<<1)|(1<<0);
}
/* 復(fù)位NAND Flash */
nand_reset();
}
/* 讀函數(shù) */
voidnand_read(unsignedchar*buf,unsignedlongstart_addr,intsize)
{
inti,j;
#ifdefLARGER_NAND_PAGE
if((start_addr&NAND_BLOCK_MASK_LP)||(size&NAND_BLOCK_MASK_LP)){
return;/* 地址或長(zhǎng)度不對(duì)齊 */
}
#else
if((start_addr&NAND_BLOCK_MASK)||(size&NAND_BLOCK_MASK)){
return;/* 地址或長(zhǎng)度不對(duì)齊 */
}
#endif
/* 選中芯片 */
nand_select_chip();
for(i=start_addr;i<(start_addr+size);){
/* 發(fā)出READ0命令 */
write_cmd(0);
/* Write Address */
write_addr(i);
#ifdefLARGER_NAND_PAGE
write_cmd(0x30);
#endif
wait_idle();
#ifdefLARGER_NAND_PAGE
for(j=0;j
for(j=0;j
*buf=read_data();
buf++;
}
}
/* 取消片選信號(hào) */
nand_deselect_chip();
return;
}