您的位置:首页 > 其它

MTD源码学习报告001

2008-08-01 14:59 507 查看
MTD源码分析001

本文以2.6.10的内核及s3c2410的nand flash驱动为例,按本人的理解通过对read系统调用的流程来对MTD模块的源码进行分析,不对之处欢迎指正。更详细的MTD源码分析可以参考Jim Zeus的分析文章,本文有些内容也是直接通过拿来主义获取的。

一 MTD介绍

MTD(memory
technology device内存技术设备)是用于访问memory设备(ROM、flash)的Linux的子系统。MTD的主要目的是为了使新的memory设备的驱动更加简单,为此它在硬件和上层之间提供了一个抽象的接口。MTD的所有源代码在/drivers/mtd子目录下。我将CFI接口的MTD设备分为四层(从设备节点直到底层硬件驱动),这四层从上到下依次是:设备节点、MTD设备层、MTD原始设备层和硬件驱动层。





二 编译内核使之支持MTD模块

要在系统中使用MTD必须在编译内核的时候把MTD的配置选项选中才行。至于MTD的配置含义我们可以查看MTD的Kconfig文件和Makefile文件,在drivers/mtd目录下

/drivers/mtd/Kconfig:

menu "Memory Technology Devices (MTD)"

config MTD

tristate
"Memory Technology Device (MTD) support"

help

Memory Technology Devices are flash, RAM and
similar chips, often

used for solid state file systems on embedded
devices. This option

will provide the generic support for MTD
drivers to register

themselves with the kernel and for potential
users of MTD devices

to enumerate the devices which are present
and obtain a handle on

them. It will also allow you to select
individual drivers for

particular hardware and users of MTD devices.
If unsure, say N.

…….

config MTD_PARTITIONS

bool
"MTD partitioning support"

depends on
MTD

help

If you have a device which needs to divide
its flash chip(s) up

into multiple 'partitions', each of which
appears to the user as

a separate MTD device, you require this
option to be enabled. If

unsure, say 'Y'.

Note, however, that you don't need this
option for the DiskOnChip

devices. Partitioning on NFTL 'devices' is a
different - that's the

'normal' form of partitioning used on a block
device.

……..

config MTD_CHAR

tristate
"Direct char device access to MTD devices"

depends on
MTD

help

This provides a character device for each MTD
device present in

the system, allowing the user to read and
write directly to the

memory chips, and also use ioctl() to obtain
information about

the device, or to erase parts of it.

config MTD_BLOCK

tristate
"Caching block device access to MTD devices"

depends on
MTD

---help---

Although most flash chips have an erase size
too large to be useful

as block devices, it is possible to use MTD
devices which are based

on RAM chips in this manner. This block
device is a user of MTD

devices performing that function.

At the moment, it is also required for the
Journalling Flash File

System(s) to obtain a handle on the MTD
device when it's mounted

(although JFFS and JFFS2 don't actually use
any of the functionality

of the mtdblock device).

Later, it may be extended to perform
read/erase/modify/write cycles

on flash chips to emulate a smaller block
size. Needless to say,

this is very unsafe, but could be useful for
file systems which are

almost never written to.

You do not need this option for use with the
DiskOnChip devices. For

those, enable NFTL support (CONFIG_NFTL)
instead.

……

从该文件可知,要支持MTD必须要把”MTD”选项选上,如果要支持分区的话还要选中”MTD_PARTITIONS”选现, 除了这两个选项外我们一般还要选中” MTD_CHAR”和”MTD_BLOCK”因为我们的flash是以设备文件的形式被用户使用的,根据MTD介绍部分可知,用户通过操作设备文件进而操作MTD字符设备或MTD块设备,最后才到我们的flash驱动部分。

在来看Makefile文件

/drivers/mtd/Makefile

#

# Makefile for the memory technology device drivers.

#

# $Id: Makefile.common,v 1.5 2004/08/10 20:51:49 dwmw2
Exp $

# Core functionality.

mtd-y :=
mtdcore.o

mtd-$(CONFIG_MTD_PARTITIONS) += mtdpart.o

obj-$(CONFIG_MTD) +=
$(mtd-y)

obj-$(CONFIG_MTD_CONCAT) += mtdconcat.o

obj-$(CONFIG_MTD_REDBOOT_PARTS) += redboot.o

obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdlinepart.o

obj-$(CONFIG_MTD_AFS_PARTS) += afs.o

# 'Users' - code which presents functionality to
userspace.

obj-$(CONFIG_MTD_CHAR) +=
mtdchar.o

obj-$(CONFIG_MTD_BLOCK) += mtdblock.o mtd_blkdevs.o

obj-$(CONFIG_MTD_BLOCK_RO) += mtdblock_ro.o mtd_blkdevs.o

obj-$(CONFIG_FTL) +=
ftl.o mtd_blkdevs.o

obj-$(CONFIG_NFTL) +=
nftl.o mtd_blkdevs.o

obj-$(CONFIG_INFTL) +=
inftl.o mtd_blkdevs.o

nftl-objs :=
nftlcore.o nftlmount.o

inftl-objs :=
inftlcore.o inftlmount.o

obj-y +=
chips/ maps/ devices/ nand/

可以看到如果我们选了上面几个配置的话着重要看的文件是:mtdcore.c , mtdpart.c
, mtdchar.c , mtdblock.c , mtd_blkdevs.c , 接着递规编译 chips/
maps/ devices/ nand/ 这几个目录。

这篇文章以2410的nand为例,因此我们还要把NAND的支持编译进内核。同样我们来看他的Kconfig文件和Makefile文件。

/drivers/mtd/nand/Kconfig:

menu "NAND Flash Device Drivers"

depends on MTD!=n

config MTD_NAND

tristate "NAND Device
Support"

depends on MTD

help

This enables support for accessing all type
of NAND flash

devices. For further information see

<http://www.linux-mtd.infradead.org/tech/nand.html>.

……

config MTD_NAND_S3C2410

tristate "NAND Flash
support for S3C2410 SoC"

depends on ARCH_S3C2410
&& MTD_NAND

help

This enables the NAND flash controller on the
S3C2410.

No board specfic support is done by this
driver, each board

must advertise a platform_device for the
driver to attach.

……

主要就是要选中“MTD_NAND”和“MTD_NAND_S3C2410”选项, 注意“MTD_NAND_S3C2410“选项依赖于ARCH_S3C2410 和 MTD_NAND

接着看Makefile

/drivers/mtd/nand/Makefile:

#

# linux/drivers/nand/Makefile

#

# $Id: Makefile.common,v 1.13 2004/09/28
22:04:23 bjd Exp $

obj-$(CONFIG_MTD_NAND) += nand.o nand_ecc.o

obj-$(CONFIG_MTD_NAND_IDS) += nand_ids.o

obj-$(CONFIG_MTD_NAND_SPIA) += spia.o

obj-$(CONFIG_MTD_NAND_TOTO) += toto.o

obj-$(CONFIG_MTD_NAND_AUTCPU12) += autcpu12.o

obj-$(CONFIG_MTD_NAND_EDB7312) += edb7312.o

obj-$(CONFIG_MTD_NAND_TX4925NDFMC) += tx4925ndfmc.o

obj-$(CONFIG_MTD_NAND_TX4938NDFMC) += tx4938ndfmc.o

obj-$(CONFIG_MTD_NAND_AU1550) += au1550nd.o

obj-$(CONFIG_MTD_NAND_PPCHAMELEONEVB) += ppchameleonevb.o

obj-$(CONFIG_MTD_NAND_S3C2410) += s3c2410.o

obj-$(CONFIG_MTD_NAND_DISKONCHIP) += diskonchip.o

obj-$(CONFIG_MTD_NAND_H1900) += h1910.o

obj-$(CONFIG_MTD_NAND_RTC_FROM4) += rtc_from4.o

nand-objs = nand_base.o nand_bbt.o

重点源码: nand_base.c,
nand_bbt.c, nand_ecc.c , s3c2410.c,
其中s3c2410.c就是2410的NAND flash的驱动了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: