uboot 的nand驱动

s3c2410移植nand支持到uboot, 使用nand.c ,linux mtd 架构 【2】

u-boot-1.1.6与1.1.4相比,两者有较大的不同,1.1.6 更像是复制了 kernel 的方法来实现。

下面对nand flash的初始化代码nand_init()进行分析:


1.如果定义(CONFIG_COMMANDS & CFG_CMD_NAND)没定义(CFG_NAND_LEGACY) 则start_armboot()调用driver/nand/nand.c中的nand_init(),否则如果定义(CONFIG_COMMANDS & CFG_CMD_NAND)并且有定义了CFG_NAND_LEGACY,则调用自己定义的nand_init()。现在使用 driver/nand/nand.c中的nand_init()。
2.nand_init()调用本文件中的nand_init_chip()对nand进行初始化。
3.nand_init_chip()首先调用board_nand_init()。
4.board_nand_init()是需要自己添加的函数,这个函数的主要功能是对struct nand_chip结构体的函数指针赋值,让它们指向自己为nand驱动编写的一些函数,对未赋值的指针,uboot会在后面为其赋上通用nand驱动函数指针。此函数可放到自己板子目录的文件下。
5.nand_init_chip()接着调用nand_scan().
6.nand_scan()定义在drivers/nand/nand_base.c文件中。它首先对struct nand_chip结构体中在board_nand_init()函数中未赋值的指针赋上通用nand驱动函数指针。
7.nand_scan()->select_chip = nand_select_chip;

此函数用于打开或关闭nand芯片,0为打开,1为关闭。在这个函数中会调用nand_chip结构体中的 hwcontrol函数指针。 hwcontrol在board_nand_init()函数中被赋值。主要作用是向 nand flash发送一些nand flash开启与关闭命令。
8.nand_scan()剩余部分初始化nand_chip和mtd_info结构体。
9.nand_scan()最后在返回时调用drivers/nand/nand_bbt.c文件中的nand_default_bbt()。
nand_default_bbt()选择一个坏块描述表,返回时调用本文件中的nand_scan_bbt()(寻找建立一个坏块描述表)
10.最后返回到nand_init(),这样nand驱动的初始化完成了。

通过上述步骤我们可以知道,移植nand主要按如下步骤:

1、在board/xxx下建立c文件

2、在此文件上添加函数board_nand_init(),实现nand_chip的初始化功能

3、添加初始化的函数

4、在include/configs/xxx.h中定义相关宏,比如#define CFG_MAX_CHIPS

完成上述移植后,实际上启动后的uboot中的nand命令是通过include/nand.h实现的,例如nand erase调用了:

static inline int nand_erase(nand_info_t *info, ulong off, ulong size)

看看人家怎么做的:

//----------------------------------------------------------------------------------------------------

为了让uboot支持自己QT板子的nand flash而进行修改的部分<wbr></wbr>
1.前面的移植请参考我写的一篇《U-Boot的编译与移植到QT-S3C44B0X开发板上》,现在在board/51EDA/QT/目录下建立nand.c文件。
2.在nand.c中添加自己的board_nand_init()函数。设定nand_chip结构中的hwcontrol和dev_ready指针指向自己的函数QT_hwcontrol和QT_device_ready。并建立自己的QT_hwcontrol和QT_device_ready函数。
3.由于自己板子的nand flash的命令发送方式与uboot提供的通用nand flash命令发送方式不同,所以在nand.c文件中建立自己的命令发送函数QT_nand_command(),并在 board_nand_init()函数中将nand_chip结构中的cmdfunc指针指向QT_nand_command()函数,使其使用自己定义的发送命令函数。
4.在include/configs/QT.h中定义CFG_NAND_BASE用于指定自己板子nand flash的I/O地址。
5.在CONFIG_COMMANDS中打开CFG_CMD_NAND选项。
6.在include/configs/QT.h中定义NAND_MAX_CHIPS指定自己板子的nand flash芯片数。
7.在include/configs/QT.h中定义CFG_MAX_NAND_DEVICE指定想要支持的nand flash设备数。

//这是比较深入的分析:

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

U-BOOT Nand命令支持
u-boot1.1.6 nand_legacy驱动提供了u-boot对nand相关命令的一个轻量级的实现,但好象可扩展性不足。本文主要分析u-boot 1.16/drivers/nand文档夹下的源程式。
一.关键数据结构
1.struct mtd_info
该结构在include\linux\mtd\Mtd.h中定义,字段比较多,有很多还是函数指针,他是MTD设备操作的通用接口,这个结构中有一个比较重要的成员 void *priv,priv被声明成void指针,在下文的分析中会知道priv实际上指向了nand_chip结构。
2.struct nand_chip
<wbr><wbr>该结构在include\linux\mtd\Nand.h中定义,从名字上看就知道u-boot用他来描述Nand Flash芯片的结构,比如他定义了页地址的偏移,页地址的位掩码等。struct nand_chip不用我们手动的初始化,而是由另外一个结构,struct nand_flash_dev在程式中动态的初始化。<br style="line-height:22px"> 3.struct nand_flash_dev<br style="line-height:22px"><wbr><wbr> 该结构的定义有两处地方分别是<br style="line-height:22px"> ①include/linux/mtd/nand_legacy.h 由nand_legacy模块使用<br style="line-height:22px"> ②include/linux/mtd/nand.h<wbr><wbr><wbr><wbr> 由u-boot通用nand架构使用<br style="line-height:22px"> 特别是在移植的时候要小心把两者混淆。我们先来看看改结构的定义<br style="line-height:22px"> struct nand_flash_dev {<br style="line-height:22px"><wbr><wbr><wbr><wbr> char *name;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br style="line-height:22px"><wbr><wbr><wbr><wbr> int id;<br style="line-height:22px"><wbr><wbr><wbr><wbr> unsigned long pagesize;<br style="line-height:22px"><wbr><wbr><wbr><wbr> unsigned long chipsize;<br style="line-height:22px"><wbr><wbr><wbr><wbr> unsigned long erasesize;<br style="line-height:22px"><wbr><wbr><wbr><wbr> unsigned long options;<br style="line-height:22px"> };<br style="line-height:22px"> name : Nand Flash名称<br style="line-height:22px"> id : u-boot内部id编号???<br style="line-height:22px"> chipsize : 以MB为单位的芯片大小,比如64(M)<br style="line-height:22px"> erasesize : 擦除块的大小,比如0x4000(16K)<br style="line-height:22px"> options : 一些选项,比较重要的是Flash的数据位宽,假如您的Nand Flash是16位宽的,则必须包含NAND_BUSWIDTH_16选项。我们必须根据所使用的Nand Flash来填充里面的字段。<br style="line-height:22px"> 4.关键数据结构在程式中的使用<br style="line-height:22px"> struct nand_info_t nand_info[ CFG_MAX_NAND_DEVICE ];<br style="line-height:22px"> 在 drivers\nand\nand.c中定义。CFG_MAX_NAND_DEVICE是板子的Nand Flash芯片的数量必须在板子的配置文档中定义(比如 include\configs\smdk2410.h)。<br style="line-height:22px"><br style="line-height:22px"> static struct nand_chip nand_chip[CFG_MAX_NAND_DEVICE];<br style="line-height:22px"> 在drivers\nand\nand.c中定义。CFG_MAX_NAND_DEVICE的定义同上。<br style="line-height:22px"><br style="line-height:22px"> struct nand_flash_dev nand_flash_ids[] = { … };<br style="line-height:22px"> 在drivers\nand\nand_ids.c中定义。这里要注意一点,在include\linux\mtd\nand_ids.h里面也 nand_flash_ids[]的定义,那是由nand legacy驱动模块使用的。两者不能混淆!!!。在nand_flash_ids的定义中我找到了适合我的Nand Flash的结构描述:<br style="line-height:22px"> {"NAND 64MiB 3,3V 8-bit",<wbr><wbr><wbr><wbr><wbr><wbr>0x76, 512, 64, 0x4000, 0}<br style="line-height:22px"> 设备ID为0x76,页大小为512Byte,总的容量为64M,擦除块为0x4000(16K),数据位宽8Bit。假如您的Nand Flash没有合适的描述,需要自己在该数组中添加相应的定义。<br style="line-height:22px"><br style="line-height:22px"> 二.Nand Flash初始化<br style="line-height:22px"><br style="line-height:22px"> 1.nand_init( drivers\nand\nand.c )<br style="line-height:22px"> nand_init函数在lib_xxx/Board.c的start_armboot中调用。是u-boot Nand的主函数。nand_init的主要功能是对CFG_MAX_NAND_DEVICE个Nand设备进行初始化(调用 nand_init_chip),累加Nand Flash的总大小。在nand_init结束时,能够配置是否执行board_nand_select_device,选择Nand芯片。<br style="line-height:22px"> 2.nand_init_chip( drivers\nand\nand.c )<br style="line-height:22px"> static void nand_init_chip( struct mtd_info *mtd, struct nand_chip *nand, ulong base_addr )调用各个研发板提供的 board_nand_init 函数( board\\.c )让研发板获得初始化Nand Flash芯片的机会。调用 nand_scan。<br style="line-height:22px"> 3.nand_scan ( drivers\nand\nand_base.c )<br style="line-height:22px"> int nand_scan( struct mtd_info *mtd, int maxchips )<br style="line-height:22px"> 这是u-boot初始化nand设备的核心函数。他主要完成以下工作<br style="line-height:22px"><br style="line-height:22px"> 1)初始化nand_chip的函数指针,这些函数一般在 board\\.c中定义。<br style="line-height:22px"> …<br style="line-height:22px"> struct nand_chip *this = mtd-&gt;priv<br style="line-height:22px"> ....<br style="line-height:22px"> if( this-&gt; cmdfunc == NULL )<br style="line-height:22px"><wbr><wbr><wbr><wbr> this-&gt;cmdfunc = nand_command;<br style="line-height:22px"> …<br style="line-height:22px"> 上面是初始化nand_chip中cmdfunc指针的代码,假如在board_init_nand中研发板没有提供自己的nand_command函数,u-boot 将使用默认的nand_command函数(我觉得u-boot提供的这些默认的函数都不适合特定的硬件,所以很多都要自己重新写)。<br style="line-height:22px"><br style="line-height:22px"> 2)使用上面注册的函数指针,读取Nand Flash的设备,并且在上文提到的nand_flash_ids[]中找是否有匹配项,若找到匹配的项,则初始化 nand_chip 和 mtd_info,他们的初始化代码老长的一段,一般没什么问题。<br style="line-height:22px"><br style="line-height:22px"> 三. Nand Flash 操作<br style="line-height:22px"> 1. Read<br style="line-height:22px"> 函数调用层次:(如下图)。<span><wbr></wbr></span><br style="line-height:22px"><img title="[转载]s3c2410移植nand支持到uboot, 使用nand.c ,linux mtd 架构 【2】 - lanmanck的专栏 - 优快云博客 - deshunfan - 电子屋" border="0" alt="[转载]s3c2410移植nand支持到uboot, 使用nand.c ,linux mtd 架构 【2】 - lanmanck的专栏 - 优快云博客 - deshunfan - 电子屋" src="http://blogimg.chinaunix.net/blog/upfile/070908103517.gif" width="519" height="312" style="line-height:22px; border-right-width:0px; max-width:100%; border-top-width:0px; border-bottom-width:0px; border-left-width:0px"><span><wbr></wbr></span><br style="line-height:22px"> 以common/env_nand.c里面读取Nand Flash中的环境变量为例<br style="line-height:22px"> common/env_nand.c<br style="line-height:22px"> ret = nand_read( &amp;nand_info[0], CFG_ENV_OFFSET, &amp;total, (u_char*)env_ptr );<br style="line-height:22px"><br style="line-height:22px"> nand_info[]就是我们在1.4讲到的nand_info_t(mtd_info的别名)数组。此处的nand_read是个inline函数,下面是他的实现:<br style="line-height:22px"><br style="line-height:22px"> include/nand.h<br style="line-height:22px"> static inline int nand_read(nand_info_t *info, ulong ofs, ulong *len, u_char *buf)<br style="line-height:22px"> {<br style="line-height:22px"><wbr><wbr><wbr><wbr> return info-&gt;read(info, ofs, *len, (size_t*)len, buf);<br style="line-height:22px"> }<br style="line-height:22px"><br style="line-height:22px"> 能够看出nand_read实际上调用的是nand_info的read方法。nand_info的read方法是在2.3中讲到的nand_scan中初始化<br style="line-height:22px"> drivers/nand/nand_base.c<br style="line-height:22px"> int nand_scan(struct mtd_info *mtd, int machips)<br style="line-height:22px"> {<br style="line-height:22px"><wbr><wbr><wbr><wbr> …<br style="line-height:22px"><wbr><wbr><wbr><wbr> mtd-&gt;read = nand_read;<br style="line-height:22px"><wbr><wbr><wbr><wbr> …<br style="line-height:22px"> }<br style="line-height:22px"> 此处又一个nand_read!!!<br style="line-height:22px"><br style="line-height:22px"> drivers/nand/nand_base.c<br style="line-height:22px"> static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)<br style="line-height:22px"> {<br style="line-height:22px"><wbr><wbr><wbr><wbr> return nand_read_ecc( mtd, from, len, retlen, buf, NULL, NULL );<br style="line-height:22px"> }<br style="line-height:22px"><br style="line-height:22px"> 又一层包装!!!<br style="line-height:22px"><br style="line-height:22px"> drivers/nand/nand_base.c<br style="line-height:22px"> static int nand_read_ecc(…)<br style="line-height:22px"> {<br style="line-height:22px"><wbr><wbr><wbr><wbr> …<br style="line-height:22px"> }<br style="line-height:22px"> 终于到达最后一层了,nand_read_ecc通过调用nand_chip里面提供的函数对nand flash完成读的操作。具体能够看看代码,老长的一段。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值