原始链接:http://xianzilu.spaces.live.com/blog/
#define WRITE_NAND_COMMAND(d, adr) NF_Cmd(d)
#define WRITE_NAND_COMMANDW(d, adr) NF_CmdW(d)
#define WRITE_NAND_ADDRESS(d, adr) NF_Addr(d)
#define WRITE_NAND(d, adr) NF_Write(d)
#define READ_NAND(adr) NF_Read()
/* the following functions are NOP's because S3C24X0 handles this in hardware */
#define NAND_CTL_CLRALE(nandptr)
#define NAND_CTL_SETALE(nandptr)
#define NAND_CTL_CLRCLE(nandptr)
#define NAND_CTL_SETCLE(nandptr)
U-Boot 1.1.2 (May 28 2006 - 08:36:42)
U-Boot中SMDK2410的NAND Flash驱动。
本来不应该先写这个的,呵呵,但是我这个人记忆力很差,说不定过几天我就忘了移植过程中的代码是哪里弄来的了。
![](http://spaces.msn.com/rte/emoticons/smile_tongue.gif)
首先庆祝一下,我学生生涯所有的考试都结束了,哈哈哈。不过希望不要补考……听说我们的研究生教学院长赵学增老师的课很变态,120个抓50个……上帝保佑我啊。
开始吧。标准的SMDK2410板不支持NAND Flash,启动的时候是这样的:
U-Boot 1.1.2 (May 28 2006 - 08:20:50)
U-Boot code: 33F80000 -> 33F99A14 BSS: -> 33F9DB0C
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
*** Warning - bad CRC, using default environment
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
*** Warning - bad CRC, using default environment
In: serial
Out: serial
Err: serial
Out: serial
Err: serial
有NAND支持的多一行(NAND 64MB):
U-Boot 1.1.2 (May 28 2006 - 08:36:42)
U-Boot code: 33F80000 -> 33F99A14 BSS: -> 33F9DB0C
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
NAND: 64 MB
*** Warning - bad CRC, using default environment
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
NAND: 64 MB
*** Warning - bad CRC, using default environment
In: serial
Out: serial
Err: serial
Out: serial
Err: serial
怎么实现这种支持呢?U-Boot真是功能强大的Bootloader,在/inclued/configs/smdk2410.h中有这么一段
/***********************************************************
* Command definition
***********************************************************/
#define CONFIG_COMMANDS /
(CONFIG_CMD_DFL | /
CFG_CMD_CACHE | /
CFG_CMD_NAND | /
/*CFG_CMD_EEPROM |*/ /
/*CFG_CMD_I2C |*/ /
/*CFG_CMD_USB |*/ /
CFG_CMD_REGINFO | /
CFG_CMD_DATE | /
CFG_CMD_ELF)
* Command definition
***********************************************************/
#define CONFIG_COMMANDS /
(CONFIG_CMD_DFL | /
CFG_CMD_CACHE | /
CFG_CMD_NAND | /
/*CFG_CMD_EEPROM |*/ /
/*CFG_CMD_I2C |*/ /
/*CFG_CMD_USB |*/ /
CFG_CMD_REGINFO | /
CFG_CMD_DATE | /
CFG_CMD_ELF)
/* this must be included AFTER the definition of CONFIG_COMMANDS (if any) */
需要说明一下CONFIG_CMD_DFL,定义的是默认指令,包括bdinfo、bootd、coninfo、saveenv、flinfo、erase、protect、iminfo、imls、itest、loadb、loads、md、mm、nm、mw、cp、cmp、crc、base、loop、loopw、mtest、sleep、bootp、tftpboot、rarpboot、run等常用指令,这些指令我以后会结合使用做适当的说明。关于指令的宏定义说明可以看看U-Boot的README里面的Monitor Functions。这就是对编译成功后的U-Boot支持的命令的定义,SMDK2410默认的smdk2401.h中,红色字部分是注释掉的。但是要支持NAND Flash远没有去掉一个注释这么简单。
我们可以试试单纯把这个注释去掉是什么结果,老步骤:
make distclean
make smdk2410_config
make
……一堆编译信息飘过……
出错了,位置指向cmd_nand.c这个文件,好几处错误。原因是SMDK2410的配置里根本就没有对NAND Flash支持的宏定义和函数。怎么办?自己写么?好在U-Boot里有另外一个可以让我们借鉴的配置VCMA9。在Source Navigator里搜索一下vcma,看看vcma9.h和vcma9.c,可以从中摘取出一段宏定义和一些函数声明。网上有人的做法是将其放在smdk2410.h和smdk2410.c中,但是白痴的cross-2.95.3和cross-3.2都不能认到cmd_nand.c中已经在smdk2410.h和smdk2410.c中定义的宏所指的函数,就算是加上extern的也不行。也许你没有看懂刚才这句话,解释一下,例如cmd_nand.c中有这样一段
if(ale_wait)
NAND_WAIT_READY(nand); /* do the worst case 25us wait */
else
udelay(10);
NAND_WAIT_READY(nand); /* do the worst case 25us wait */
else
udelay(10);
其中NAND_WAIT_READY(nand),在smdk2410.h中定义为
#define NAND_WAIT_READY(nand) NF_WaitRB()
而NF_WaitRB()在smdk2410.c中定义为
static inline void NF_WaitRB(void)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
while (!(nand->NFSTAT & (1<<0)));
}
}
这个编译器死活说NF_WaitRB()未定义,就算是我在cmd_nand.c中加上这样一句也不起作用
extern void NF_WaitRB(void)
也许是我C学的不好,搞不清楚多个文件共同编译的时候static inline的引用关系,不管它,我把这些宏中定义的函数的申明放在了cmd_nand.c中,为了不太无耻,我在前面加上了针对SMDK2410的选择性编译。
(请注意,下面这些代码拷贝到cmd_nand.c中的适当位置,cmd_nand.c中有许多选择性编译的宏,注意放的位置,不要被忽略掉了)
/*-----------------------------------------------------------------------
* NAND flash basic functions
* Added by Lu Xianzi 2006.5.27
* Copied from board/mpl/vcma9/vcma9.h & vcma9.c
*/
* NAND flash basic functions
* Added by Lu Xianzi 2006.5.27
* Copied from board/mpl/vcma9/vcma9.h & vcma9.c
*/
#if (CONFIG_SMDK2410)
#include <s3c2410.h>
typedef enum {
NFCE_LOW,
NFCE_HIGH
} NFCE_STATE;
typedef enum {
NFCE_LOW,
NFCE_HIGH
} NFCE_STATE;
static inline void NF_Conf(u16 conf)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
nand->NFCONF = conf;
}
}
static inline void NF_Cmd(u8 cmd)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
nand->NFCMD = cmd;
}
}
static inline void NF_CmdW(u8 cmd)
{
NF_Cmd(cmd);
udelay(1);
}
{
NF_Cmd(cmd);
udelay(1);
}
static inline void NF_Addr(u8 addr)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
nand->NFADDR = addr;
}
}
static inline void NF_SetCE(NFCE_STATE s)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
switch (s) {
case NFCE_LOW:
nand->NFCONF &= ~(1<<11);
break;
case NFCE_LOW:
nand->NFCONF &= ~(1<<11);
break;
case NFCE_HIGH:
nand->NFCONF |= (1<<11);
break;
}
}
nand->NFCONF |= (1<<11);
break;
}
}
static inline void NF_WaitRB(void)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
while (!(nand->NFSTAT & (1<<0)));
}
}
static inline void NF_Write(u8 data)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
nand->NFDATA = data;
}
}
static inline u8 NF_Read(void)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
return(nand->NFDATA);
}
}
static inline void NF_Init_ECC(void)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
nand->NFCONF |= (1<<12);
}
}
static inline u32 NF_Read_ECC(void)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
return(nand->NFECC);
}
}
extern ulong nand_probe(ulong physadr);
static inline void NF_Reset(void)
{
int i;
{
int i;
NF_SetCE(NFCE_LOW);
NF_Cmd(0xFF); /* reset command */
for(i = 0; i < 10; i++); /* tWB = 100ns. */
NF_WaitRB(); /* wait 200~500us; */
NF_SetCE(NFCE_HIGH);
}
NF_Cmd(0xFF); /* reset command */
for(i = 0; i < 10; i++); /* tWB = 100ns. */
NF_WaitRB(); /* wait 200~500us; */
NF_SetCE(NFCE_HIGH);
}
static inline void NF_Init(void)
{
#if 0 /* a little bit too optimistic */
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0
#else
#define TACLS 0
#define TWRPH0 4
#define TWRPH1 2
#endif
{
#if 0 /* a little bit too optimistic */
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0
#else
#define TACLS 0
#define TWRPH0 4
#define TWRPH1 2
#endif
NF_Conf((1<<15)|(0<<14)|(0<<13)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0));
/*nand->NFCONF = (1<<15)|(1<<14)|(1<<13)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0); */
/* 1 1 1 1, 1 xxx, r xxx, r xxx */
/* En 512B 4step ECCR nFCE=H tACLS tWRPH0 tWRPH1 */
/*nand->NFCONF = (1<<15)|(1<<14)|(1<<13)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0); */
/* 1 1 1 1, 1 xxx, r xxx, r xxx */
/* En 512B 4step ECCR nFCE=H tACLS tWRPH0 tWRPH1 */
NF_Reset();
}
}
void nand_init(void)
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
{
S3C2410_NAND * const nand = S3C2410_GetBase_NAND();
NF_Init();
#ifdef DEBUG
printf("NAND flash probing at 0x%.8lX/n", (ulong)nand);
#endif
printf ("%4lu MB/n", nand_probe((ulong)nand) >> 20);
}
#ifdef DEBUG
printf("NAND flash probing at 0x%.8lX/n", (ulong)nand);
#endif
printf ("%4lu MB/n", nand_probe((ulong)nand) >> 20);
}
#endif /* (CONFIG_SMDK2410) */
然后把下面这些宏定义放在smdk2410.h中
/*-----------------------------------------------------------------------
* NAND flash settings
* Added by Lu Xianzi 2006.5.27
* Copied from include/conifgs/vcma9.h
*/
#if (CONFIG_COMMANDS & CFG_CMD_NAND)
* NAND flash settings
* Added by Lu Xianzi 2006.5.27
* Copied from include/conifgs/vcma9.h
*/
#if (CONFIG_COMMANDS & CFG_CMD_NAND)
#define CFG_MAX_NAND_DEVICE 1 /* Max number of NAND devices */
#define SECTORSIZE 512
#define SECTORSIZE 512
#define ADDR_COLUMN 1
#define ADDR_PAGE 2
#define ADDR_COLUMN_PAGE 3
#define ADDR_PAGE 2
#define ADDR_COLUMN_PAGE 3
#define NAND_ChipID_UNKNOWN 0x00
#define NAND_MAX_FLOORS 1
#define NAND_MAX_CHIPS 1
#define NAND_MAX_FLOORS 1
#define NAND_MAX_CHIPS 1
#define NAND_WAIT_READY(nand) NF_WaitRB()
#define NAND_DISABLE_CE(nand) NF_SetCE(NFCE_HIGH)
#define NAND_ENABLE_CE(nand) NF_SetCE(NFCE_LOW)
#define NAND_ENABLE_CE(nand) NF_SetCE(NFCE_LOW)
#define WRITE_NAND_COMMAND(d, adr) NF_Cmd(d)
#define WRITE_NAND_COMMANDW(d, adr) NF_CmdW(d)
#define WRITE_NAND_ADDRESS(d, adr) NF_Addr(d)
#define WRITE_NAND(d, adr) NF_Write(d)
#define READ_NAND(adr) NF_Read()
/* the following functions are NOP's because S3C24X0 handles this in hardware */
#define NAND_CTL_CLRALE(nandptr)
#define NAND_CTL_SETALE(nandptr)
#define NAND_CTL_CLRCLE(nandptr)
#define NAND_CTL_SETCLE(nandptr)
/* #define CONFIG_MTD_NAND_VERIFY_WRITE 1 */
/* This definition above is commented by Lu Xianzi. 2006.05.28
Because there's no definition of a macro called __mem_pci,
there will be a link error.
*/
#define CONFIG_MTD_NAND_ECC_JFFS2 1
Because there's no definition of a macro called __mem_pci,
there will be a link error.
*/
#define CONFIG_MTD_NAND_ECC_JFFS2 1
#endif /* CONFIG_COMMANDS & CFG_CMD_NAND */
注意这段宏定义中与vcma9.h中不同的是我标记红色的部分,网上的一些移植说明没有解决这个问题,导致最后的链接无法通过。事实上,我这样做取消了cmd_nand.c中对NAND Flash的写校验和ECC校验。__mem_pci是一个什么东西我也不知道,在U-Boot目录下搜索包含“__mem_pci”字串的文件也没有找到,除了使用它的/include/asm/io.h。顺便说一下,这个麻烦是从cmd_nand.c中的nand_write_page函数中readb(nand->IO_ADDR)这个宏开始的,你可以用Source Navigator查找一下引用,最后就指向没有__mem_pci宏的这个问题。希望有高手看到这篇文章,并给我指出解决的办法,呵呵。
![](http://spaces.msn.com/rte/emoticons/smile_regular.gif)
做完这些修改,就再来一次老步骤
make distclean
make smdk2410_config
make
………………
生成了U-Boot.bin。NAND Flash的驱动到此完成,好不好使呢?试试看。上次我们已经在板子里烧写进了U-Boot,是一个可以支持串口传输的U-Boot,要是现在还用sjf2410往里烧U-Boot.bin就太冤大头了。可以利用串口进行传输,操作步骤如下:
使用超级终端,建立一个连接:文件〉新建连接;名称随便,图表随便,确定;连接时使用选择你的开发板所接的COM口;端口设置中,每秒位数选115200,数据位选8,奇偶校验选无,停止位选1,数据流控制选“无”。打开开发板的电源,出现以下提示:
U-Boot 1.1.2 (May 28 2006 - 08:20:50)
U-Boot code: 33F80000 -> 33F99A14 BSS: -> 33F9DB0C
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
*** Warning - bad CRC, using default environment
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
*** Warning - bad CRC, using default environment
In: serial
Out: serial
Err: serial
Out: serial
Err: serial
LXZROB # loadb
## Ready for binary (kermit) download to 0x33000000 at 115200 bps...
loadb这个指令以kermit协议从串口下载二进制文件到开发板的内存中,默认下载到0x33000000。当然你可以改在别的地址,例如:loadb 30000000就是下载到0x30000000。这时候选择超级终端菜单上:传送〉发送文件,文件名选择编译好的U-Boot.bin,协议选择Kermit,点发送。可以看到发送进度。
发送结束出现提示:
## Total Size = 0x00019a14 = 104980 Bytes
## Start Addr = 0x33000000
## Start Addr = 0x33000000
这时可以测试新的修改好不好使:
LXZROB # go 33000000
## Starting application at 0x33000000 ...
## Starting application at 0x33000000 ...
U-Boot 1.1.2 (May 28 2006 - 08:36:42)
U-Boot code: 33F80000 -> 33F99A14 BSS: -> 33F9DB0C
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
NAND: 64 MB
*** Warning - bad CRC, using default environment
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
NAND: 64 MB
*** Warning - bad CRC, using default environment
In: serial
Out: serial
Err: serial
LXZROB #
Out: serial
Err: serial
LXZROB #
go指令可以直接执行内存地址上的程序,例如刚才下载到0x33000000的新的支持NAND的U-Boot,可以看到出现了“NAND: 64MB”这一项。输入help,你会看到比刚才的U-Boot多了一组指令nand,输入help nand,可以看到更加细节的指令:
LXZROB # help nand
nand info - show available NAND devices
nand device [dev] - show or set current device
nand read[.jffs2[s]] addr off size
nand write[.jffs2] addr off size - read/write `size' bytes starting
at offset `off' to/from memory address `addr'
nand erase [clean] [off size] - erase `size' bytes from
offset `off' (entire device if not specified)
nand bad - show bad blocks
nand read.oob addr off size - read out-of-band data
nand write.oob addr off size - read out-of-band data
nand info - show available NAND devices
nand device [dev] - show or set current device
nand read[.jffs2[s]] addr off size
nand write[.jffs2] addr off size - read/write `size' bytes starting
at offset `off' to/from memory address `addr'
nand erase [clean] [off size] - erase `size' bytes from
offset `off' (entire device if not specified)
nand bad - show bad blocks
nand read.oob addr off size - read out-of-band data
nand write.oob addr off size - read out-of-band data
输入nand info,可以看到:
LXZROB # nand info
Device 0: Samsung unknown 64Mb at 0x4e000000 (64 MB, 16 kB sector)
说明我们的驱动成功了。也可以试试其他指令。
但是现在在NOR Flash中的U-boot还是不支持NAND的,需要重新烧写。U-Boot支持自己烧写自己。
先看看NOR Flash的情况:
LXZROB # flinfo
Bank # 1: AMD: 1x Amd29LV800BB (8Mbit)
Size: 1 MB in 19 Sectors
Sector Start Addresses:
00000000 (RO) 00004000 (RO) 00006000 (RO) 00008000 (RO) 00010000 (RO)
00020000 00030000 00040000 00050000 00060000
00070000 00080000 00090000 000A0000 000B0000
000C0000 000D0000 000E0000 000F0000 (RO)
Size: 1 MB in 19 Sectors
Sector Start Addresses:
00000000 (RO) 00004000 (RO) 00006000 (RO) 00008000 (RO) 00010000 (RO)
00020000 00030000 00040000 00050000 00060000
00070000 00080000 00090000 000A0000 000B0000
000C0000 000D0000 000E0000 000F0000 (RO)
一共有19个sector,其中前5个总计128kb的sector有U-Boot程序,是写保护的。要烧写首先要去掉写保护:
LXZROB # protect off 0 1ffff
Un-Protected 5 sectors
LXZROB # flinfo
Un-Protected 5 sectors
LXZROB # flinfo
Bank # 1: AMD: 1x Amd29LV800BB (8Mbit)
Size: 1 MB in 19 Sectors
Sector Start Addresses:
00000000 00004000 00006000 00008000 00010000
00020000 00030000 00040000 00050000 00060000
00070000 00080000 00090000 000A0000 000B0000
000C0000 000D0000 000E0000 000F0000 (RO)
Size: 1 MB in 19 Sectors
Sector Start Addresses:
00000000 00004000 00006000 00008000 00010000
00020000 00030000 00040000 00050000 00060000
00070000 00080000 00090000 000A0000 000B0000
000C0000 000D0000 000E0000 000F0000 (RO)
可以看到写保护已经去掉,擦除:
LXZROB # erase 0 1ffff
Erasing sector 0 ... ok.
Erasing sector 1 ... ok.
Erasing sector 2 ... ok.
Erasing sector 3 ... ok.
Erasing sector 4 ... ok.
Erased 5 sectors
Erasing sector 0 ... ok.
Erasing sector 1 ... ok.
Erasing sector 2 ... ok.
Erasing sector 3 ... ok.
Erasing sector 4 ... ok.
Erased 5 sectors
然后烧写:
LXZROB # cp.b 33000000 0 19a14
Copy to Flash... done
Copy to Flash... done
cp.b是用来拷贝内存信息的,其格式为cp [.b, .w, .l] source target count,输入这样的指令是因为刚才把程序下载到了0x33000000,NOR Flash的起始地址是0x0,下载的程序长度为0x19a14。重启开发板,U-Boot的烧写就完成了。