Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1455697
  • 博文数量: 267
  • 博客积分: 3010
  • 博客等级: 少校
  • 技术积分: 3089
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-05 17:09
个人简介

尊天命,尽人事

文章分类

全部博文(267)

文章存档

2017年(6)

2015年(4)

2014年(27)

2013年(52)

2012年(59)

2011年(120)

分类:

2011-10-20 10:37:03

原文地址:uboot移植记录之二 作者:dongas

uboot移植记录之二

移植可以从Nor flash启动的uboot请参考uboot移植系列的《uboot移植记录之一》
http://blog.chinaunix.net/u2/60011/showart.php?id=1005057
下面介绍移植支持Nand flash驱动的uboot.

加入Nand flash驱动的支持,可以在uboot命令行下操作Nand flash.但还未能从Nand flash启动,只能在Nor flash内运行.支持从Nandflash启动会在下节介绍。

下面描述详细步骤:
1. 打开Nandflash驱动支持
要使uboot支持nand驱动,需要在smdk2410.h中将CFG_CMD_NAND部分注释取消,打开nandflash功能。
修改如下:

/* include/configs/smdk2410.h */
#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)

2. 添加nand_init()函数
Uboot对SMDK2410板的NAND Flash初始化部分没有写
即在lib_arm/board.c中的start_armboot函数中有这么一句:

#if (CONFIG_COMMANDS & CFG_CMD_NAND)
puts ("NAND:");
nand_init(); /* go init the NAND */
#endif


因为前面打开了CFG_CMD_NAND这个宏,uboot编译时会去查询nand_init()这个函数。而在board/smdk2410目录下任何源文件中都没有定义nand_init这个函数。所以需要我们补充这个函数以及这个函数涉及的底层操作。这里我们可以参考VCMA9板的nand_init函数,VCMA9板是一款用S3C2410的板子,因此这部分操作和SMDK2410 Demo Board很相似。大部分代码可以照搬。

分析VCM9板的nand_init()函数,发现需要拷贝如下内容:
首先将board/mpl/vcma9/vcma9.h中下面代码拷贝到common/cmd_nand.c中do_nand函数前面。

/* dongas - support nand driver */
#if (CONFIG_COMMANDS & CFG_CMD_NAND)
typedef enum {
 NFCE_LOW,
 NFCE_HIGH
} NFCE_STATE;

static inline void NF_Conf(u16 conf)
{
 S3C2410_NAND * const nand = S3C2410_GetBase_NAND();

 nand->NFCONF = conf;
}

static inline void NF_Cmd(u8 cmd)
{
 S3C2410_NAND * const nand = S3C2410_GetBase_NAND();

 nand->NFCMD = cmd;
}

static inline void NF_CmdW(u8 cmd)
{
 NF_Cmd(cmd);
 udelay(1);
}

static inline void NF_Addr(u8 addr)
{
 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();

 switch (s) {
  case NFCE_LOW:
   nand->NFCONF &= ~(1<<11);
   break;

  case NFCE_HIGH:
   nand->NFCONF |= (1<<11);
   break;
 }
}

static inline void NF_WaitRB(void)
{
 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();

 nand->NFDATA = data;
}

static inline u8 NF_Read(void)
{
 S3C2410_NAND * const nand = S3C2410_GetBase_NAND();

 return(nand->NFDATA);
}

static inline void NF_Init_ECC(void)
{
 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();

 return(nand->NFECC);
}

#endif
/* dongas - support nand driver - end */

再接着将board/mpl/vcma9/vcma9.c中下面代码拷贝到common/cmd_nand.c中来。

/* dongas - support nand driver */
/*
 * NAND flash initialization.
 */

#if (CONFIG_COMMANDS & CFG_CMD_NAND)
extern ulong
nand_probe(ulong physadr);


static inline void NF_Reset(void)
{
    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);
}


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

    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 */

    NF_Reset();
}

void
nand_init(void)
{
 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);
}
#endif
/* dongas - support nand driver - end */

另外还要在cmd_nand.c前面加上#include ,否则编译时会出现ERROR: "S3C2410_NAND" Undeclared错误!

最后将include/configs/VCMA.9中下面代码拷贝到include/configs/smdk2410.h中来。

/* dongas - support nand driver */
/*-----------------------------------------------------------------------
 * NAND flash settings
 */

#if (CONFIG_COMMANDS & CFG_CMD_NAND)

#define CFG_MAX_NAND_DEVICE 1 /* Max number of NAND devices */
#define SECTORSIZE 512

#define ADDR_COLUMN 1
#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_WAIT_READY(nand) NF_WaitRB()

#define NAND_DISABLE_CE(nand) NF_SetCE(NFCE_HIGH)
#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
#define CONFIG_MTD_NAND_ECC_JFFS2 1

#endif /* CONFIG_COMMANDS & CFG_CMD_NAND */
/* dongas - support nand driver - end */

3. 重新编译uboot

#make ARCH=arm

4. 测试加入Nand驱动支持的uboot
下面我们测试一下新编译的带Nand驱动支持的uboot是否有用。由于之前我们已经编译了NOR版本的uboot并烧录在nor flash中,这时我们无需重新烧录新编译的uboot到Nor Flash了。可以直接使用Nor里的uboot提供的串口传输功能,将新编译好的uboot(带nand驱动支持)通过串口传输到sdram中测试,相比重新烧录,这种方法高效率快捷得多。
传输的方法有两种:
1) 使用loadb指令加载uboot.bin
loadb指令使用kermit协议从串口下载二进制文件到开发板的内存中,默认下载到0x33000000。当然你可以改在别的地址。
例如:loadb 30000000就是下载到0x30000000。
首先在uboot命令行模式下执行loadb指令
# loadb
## Ready for binary (kermit) download to 0x33000000 at 115200 bps...
然后选择超级终端菜单上:传送〉发送文件,文件名选择编译好的U-Boot.bin,协议选择Kermit发送,可以看到发送进度。
注:这里使用secure CRT好象不行,没发现其支持kermit协议,只能使用windows自带的超级终端或minicom
发送结束出现提示:
## Total Size = 0x00019a14 = 104980 Bytes
## Start Addr = 0x33000000

传输完后可以测试一下新编译的uboot是否有用:
ARMSYS2410 # go 30000000
注:go指令可以直接执行指定内存地址上的程序,例如刚下载到0x30000000的支持NAND驱动的uboot。

2)其实这里也可以使用tftp协议将uboot下载到指定地址如0x30000000处,再用go指令执行。效果一样,相比上一种这种方法更快捷,前提是网络

功能可用。当然,因为我的板子使用的是CS8900网卡,默认smdk2410就是支持的,所以直接可以使用。(tftp的用法请参考相关资料)
#tftpboot 30000000 u-boot.bin
#go 30000000

执行go指令启动sdram中的uboot后,若出现了“NAND: 64MB”这一行,那么恭喜你,基本上可以确定你的Nand驱动移植已经成功了。
我的启动信息如下:

ARMSYS2410 # go 30000000
## Starting application at 0x30000000 ...
U-Boot 1.1.4 (Jan 9 2008 - 00:08:10)
U-Boot code: 33F80000 -> 33F9A04C BSS: -> 33F9E3E8
RAM Configuration:
Bank #0: 30000000 64 MB
Flash: 1 MB
NAND: 64 MB <------------- 多了这一行! Happying!
In: serial
Out: serial
Err: serial
Hit any key to stop autoboot: 0
ARMSYS2410 nand#

确认一下,输入help,发现比先前NOR版本的U-Boot多了一组nand命令!

#help
mw - memory write (fill)
nand - NAND sub-system <--------- 新增加的Nand命令
nboot - boot from NAND device
……

help nand可以看到更详细的命令说明:

ARMSYS2410 nand# 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


利用这些命令可以对Nandflash进行读写操作!

输入nand info可以查看NandFlash型片的信息:
ARMSYS2410 # nand info
Device 0: Samsung unknown 64Mb at 0x4e000000 (64 MB, 16 kB sector)
说明我们的Nand驱动移植成功了。

注:这里Nand型号显示为unknown没关系,默认的也可以使用。因为uboot的nand列表里没有K9S1208 64MB,可以自己添加,加入NAND闪存芯片型号
在/include/linux/mtd/ nand_ids.h中对如下结构体赋值进行修改:
static struct nand_flash_dev nand_flash_ids[] = {
......
{"Samsung K9F1208U0B", NAND_MFR_SAMSUNG, 0x76, 26, 0, 3, 0x4000, 0},
}
当然不添加也没关系,使用默认参数操作NAND也可以。

测试成功后我们将新编好的带Nand驱动支持的uboot烧录到Nor中。
这里也不需要使用工具烧录,用uboot提供的cp命令就可以将自身拷贝到Nor flash了。强大吧,呵呵~
比sjflash烧快多了,uboot既然提供了这么多好的功能,如果不用就太可惜啦!:)

1) 先看看NOR Flash的情况:

ARMSYS2410 nand# 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)

一共有19个sector,其中前5个总计128kb的sector有U-Boot程序,是写保护的(RO)。要烧写首先要去掉写保护:
注:flinfo命令的作用是显示当前flash(nor)的信息

2) 去掉前5个sector的写保护,大小为128KB,足够容纳我们的uboot了。
ARMSYS2410 # protect off 0 1ffff
Un-Protected 5 sectors

3) 再查看一下:

ARMSYS2410 nand# 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)

可以发现写保护已经去掉了(RO标志不见了)。

4) 擦除前5个sector:
ARMSYS2410 # 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
注:这里要确保当前执行的是sdram中的uboot,而非Nor Flash中的uboot ,即依照前面的步骤下载uboot到sdram中,并运行了go指令。
否则会因擦除掉nor中的uboot而crash掉。

5) 拷贝先前加载到内存中的带Nand驱动的uboot到nor flash,即先前使用kermit协议或tftp加载到0x30000000处的uboot。
ARMSYS2410 # cp.b 30000000 0 0x1ffff
Copy to Flash... done

注:cp.b的作用是拷贝内存中的数据,支持cp到nor.
ARMSYS2410 #help cp
cp [.b, .w, .l] source target count.
0x30000000为uboot加载的地址,0x0是NOR Flash的起始地址,烧写的长度为0x1ffff。

6) 重启开发板,启动信息如下:
ARMSYS2410 nand# go 0 //nor起始地址为0x0
U-Boot 1.1.4 (Jan  9 2008 - 00:08:10)
U-Boot code: 33F80000 -> 33F9A04C  BSS: -> 33F9E3E8
RAM Configuration:
Bank #0: 30000000 64 MB
Flash:  1 MB
NAND:  64 MB
In:    serial
Out:   serial
Err:   serial
Hit any key to stop autoboot:  0
可以发现新烧写到Nor Flash的uboot可以使用了。

加入了Nand驱动的uboot补丁:

文件: u-boot-1.1.4-nanddriver-patch.tar.gz
大小: 3KB
下载: 下载

返回uboot移植记录系列

阅读(658) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~