MTD系列
<!-- ><style>v\:* {behavior:url(#default#VML);}o\:* {behavior:url(#default#VML);}w\:* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);}</style><!--><!-- ><xml> <w:WordDocument><w:View>Normal</w:View><w:Zoom>0</w:Zoom><w:PunctuationKerning/><w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument></xml><!--><!-- ><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles></xml><!--><!-- >< classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui></object><style>st1\:*{behavior:url(#ieooui) }</style><!--><!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}@font-face{font-family:"\@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;}--><!-- ><style> /* Style Definitions */ table.MsoNormalTable{mso-style-name:普通表格;mso-tstyle-rowband-size:0;mso-tstyle-colband-size:0;mso-style-noshow:yes;mso-style-parent:"";mso-padding-alt:0cm 5.4pt 0cm 5.4pt;mso-para-margin:0cm;mso-para-margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:10.0pt;font-family:"Times New Roman";mso-fareast-font-family:"Times New Roman";mso-ansi-language:#0400;mso-fareast-language:#0400;mso-bidi-language:#0400;}</style><!-->参考资料:
1.http://baike.baidu.com/view/1741385.htm -- 百度百科 MTD
2.http://code.google.com/p/readingnotesofjoseph/source/browse/trunk/docs/soft-dev/mtd.txt?spec=svn85&r=85 --mtd.txt
3.http://www.linux-mtd.infradead.org/archive/index.html
-- Memory Technology Device (MTD) Subsystem for Linux
一、MTD的概念和层次
MTD(memorytechnology device存储技术设备)是用于访问memory设备(ROM、flash)的Linux的子系统。MTD的主要目的是为了使新的memory设备的驱动更加简单,为此它在硬件和上层之间提供了一个抽象的接口。MTD的所有源代码在/drivers/mtd子目录下。
传统上,UNIX只认识块设备和字符设备。字符设备是类似键盘或者鼠标的这类设备,你必须从它读取当前数据,但是不可以定位也没有大小。块设备有固定的大小并且可以定位,它们恰好组织成许多字节的块,通常为512字节。
闪存既不满足块设备描述也不满足字符设备的描述。它们表现的类似块设备,但又有所不同。比如,块设备不区分写和擦除操作。因此,一种符合闪存特性的特殊设备类型诞生了,就是MTD设备。所以MTD既不是块设备,也不是字符设备。
关于MTD的层次,网络上有一张流传盛广的图片,如下所示,但是最初我看了这幅图根本是一点概念都没有的,不过通过看代码和网上查阅资料,知道了详细一点的分层结构,也纠正了一些前期对这张图的误解。
<!-- ><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"/><v:f eqn="sum @0 1 0"/><v:f eqn="sum 0 0 @1"/><v:f eqn="prod @2 1 2"/><v:f eqn="prod @3 21600 pixelWidth"/><v:f eqn="prod @3 21600 pixelHeight"/><v:f eqn="sum @0 0 1"/><v:f eqn="prod @6 1 2"/><v:f eqn="prod @7 21600 pixelWidth"/><v:f eqn="sum @8 21600 0"/><v:f eqn="prod @7 21600 pixelHeight"/><v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/></v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" alt="" style='width:382.8pt; height:213pt'/><!--><!-- --><img alt="" height="284" width="510" border="0"><!-- -->
(以下这部分纯属个人理解,如果有误,请高人拍砖!)
为了方便理解,先声明两点:
1. xxx层(MTD原始设备层,MTD块设备层),实现封装的代码。
2. xxx设备(MTD原始设备,MTD块设备),是xxx层向下封装后呈现给上层的表象就是一个xxx设备。
Flash硬件驱动层:该层的基于特定处理器和特定flash芯片,这里以pxa935和Hynix NAND 512MB 1.8V 16-bit为例。使用类型为nand_chip, pxa3xx_nand_info, dfc_context, pxa3xx_bbm这几个结构体来实现硬 件驱动。代码位于drivers/mtd/nand目录下。
MTD原始设备层: 用类型mtd_info的结构体来描述MTD原始设备,该结构体中有一 个域会指向Flash硬件驱动层中所有使用的结构体(串联形式,另外 pxa3xx_bbm结构体是只在底层驱动中使用)。
NAND flash在嵌入式系统中通常需要划分多个分区,系统没有运行起来的时候分区表现为mtd_partition类型的结构体数组,该数组由 工程师自己决定。在系统初始化,确切的说是在nand的驱动加载时 执行相应的prob函数时,会将上述数组中的每一个分区用类型为 mtd_part的结构体来描述。因为mtd_part结构体中内嵌了一个 mtd_info的结构体,所以每一个分区在系统看来都是一个MTD原始 设备,另外mtd_part种还有一个master指针,指向描述整片flash原 始设备的mtd_info结构体,所以这个描述整片nand的mtd_info结构 体也被叫做主分区。
在MTD原始设备层和其上层MTD块设备层(FTL)活跃着一个牛X 的指针数组mtd_table,定义于文件mtdcore.c中,该数组就是所有 MTD原始设备的指针列表(当然有数量限制,这里限制在32范围内)。 不过上面所说的主分区没有在mtd_table之列。
如果你系统中有2片nand flash,每个有8个分区,那么系统中总 共存在有18个mtd_info结构体对象,mtd_table数组中有16个指针 已经有归属。
本层和其上层FTL之间就全靠mtd_table数组和mtd_notifiers链表来
联系了,至于如何联系的,下文再详细解释。
MTD块设备层: 该层也叫flash翻译层(FTL)。以前为了在MTD设备上使用某种传统 的文件系统,linux系统中存在一个叫做flash翻译层(FTL),该FTL 是在MTD原始设备的基础上模拟出块设备,所以FTL以下的所有内 容呈现给上层的就是一个块设备。这样可以使用通用的块设备的接口 了。
这里也存在一个著名的结构体指针数组,定义于mtdblock.c文件中, 其中的每一个指针均指向一个struct mtdblk_dev的类型的对象,每一 个struct mtdblk_dev类型的对象都是一个MTD块设备。
网上流传着说使用该FTL如何不好,这种观点其实是基于使用传统文件系统存在的问题,现在有专门针对nand flash的yaffs日志型文 件系统了。所以那种掉电丢失数据的风险降低到了很小很小。
通用磁盘层:再上层就是通用磁盘层了,其实每个分区在最后都是向通用磁盘层注 册成了一个disk来使用的,后面分析代码会看到这部分。当然对于 block层的分析不在本文之中讨论。
<!-- ><v:shape id="_x0000_i1029" type="#_x0000_t75" alt="" style='width:6.6pt;height:11.4pt'/><!--><!-- --><img alt="" height="15" width="9" border="0"><!-- --><!-- ><v:shape id="_x0000_i1030" type="#_x0000_t75" alt="" style='width:6.6pt;height:12.6pt'/><!--><!-- --><img alt="" height="17" width="9" border="0"><!-- -->下图是我自己画的草图, 表示向下封装,表示向上呈现:
<!-- ><v:shape id="_x0000_i1026" type="#_x0000_t75" alt="" style='width:400.2pt;height:234pt'/><!--><!-- --><img alt="" height="312" width="534" border="0"><!-- -->
或许你读了上面的内容还是感触不深,不要紧,接下来和我一起分析代码吧!
我一直在纠结该怎么来安排代码分析的顺序,如果直接从硬件驱动开始,那细节的东西太多,不足以体现出MTD机制来,看来还是先来分析一下MTD块设备层(FTL)这一承上启下的一层吧。
下面是打印出来的模块初始化调用顺序,供大家娱乐一下:
...
lizgo:callinginit_mtd+0x0/0x44 @ 1
lizgo:callingcmdline_parser_init+0x0/0x1c@ 1
lizgo:callinginit_mtdchar+0x0/0x98 @ 1
lizgo:callinginit_mtdblock+0x0/0x1c@ 1
lizgo:callingnand_base_init+0x0/0x14 @ 1
lizgo:callingpxa3xx_nand_init+0x0/0x30 @ 1
...
二、英文资料原文及翻译:
MTD User modules
These are the modules which provide interfaces thatcan be used directly from userspace. The user modules currently plannedinclude:
(MTD子系统向上层提供的几种用户空间可直接使用的接口)
Raw character access(原始字符设备)
A character device which allows direct access tothe underlying memory. Useful for creating filesystems on the devices, beforeusing some of the translation drivers below, or for raw storage oninfrequently-changed flash, or RAM devices.
Raw block access(原始块设备)
A block device driver which allows you to pretend(假设) that the flash is a normal device with sensible(合理的) sector size. It actually works by caching awhole flash erase block in RAM, modifying it as requested, then erasing thewhole block and writing back the modified data.
(块设备驱动允许你假设flash有合理的扇区大小。它实际上是依靠缓存整个擦除块到RAM中,修改之后,擦除整个块,然后将数据回写到flash上。)
This allows you to use normal filesystems on flashparts. Obviously(显然地) it's not particularly(格外地) robust(健壮) when you are writing to it - you lose a whole eraseblock's worth of data if your read/modify/erase/rewrite cycle actually goesread/modify/erase/poweroff. But for development, and for setting up filesystemswhich are actually going to be mounted read-only in production units, it shouldbe fine.
(这样就会允许你在某个分区上使用传统的文件系统,但是当你写数据的时候就会明显地降低了它的健壮性 - 当你的read/modify/erase/rewrite正常周期突然变成了read/modify/erase/poweroff的话,那么你就会丢失正常的数据。如果你将存在于其上的文件系统mount成只读文件系统,这是没有问题的。)
There is also a read-only version of this driverwhich doesn't have the capacity to do the caching and erase/writeback, mainlyfor use with uCLinux where the extra RAM requirement was considered too large.
(存在一个只读版本的原始块设备驱动,该驱动没有缓存和擦除回写的能力,主要使用在uCLinux 上)
Flash Translation Layer (FTL)
NFTL
Block device drivers which implement an FTL/NFTL filesystem on the underlying memory device.FTL is fully functional. NFTL is currently working for both reading andwriting, but could probably do with some more field testing before being usedon production systems.
Journalling Flash File System, v2
This provides a filesystem directly onthe flash, rather than emulating(模拟) a block device. For more information, see sources.redhat.com.
MTD hardware device drivers
These provide physical access(物理访问) to memory devices, and are not used directly - theyare accessed through the user modules above(他们是通过上层的用户模块来访问的).
On-board memory
Many PC chipsets(芯片组) are incapable of correctly(不能正确地) caching system memory above 64M or 512M.A driver exists which allows you to use this memory with the linux-mtd system. (有些PC芯片组不能正确缓存高于64M或者512M的系统内存,那么就可以通过linux的mtd来使用这些内存)
PCMCIA devices
PCMCIA flash (not CompactFlash but real flash) cardsare now supported by the pcmciamtd driver in CVS. (PCMCIA闪存卡 -不是CF卡但是是真实的flash)
Common Flash Interface (CFI) onboard NORflash
This is a common solution and is well-tested andsupported, most often using JFFS2 or cramfs file systems.
Onboard NAND flash
NAND flash is rapidly overtaking NOR flash due toits larger size and lower cost; JFFS2 support for NAND flash is approachingproduction quality. (NAND因其大容量和低成本正在飞速超越NOR)
M-Systems' DiskOnChip 2000 and Millennium
The DiskOnChip 2000, Millennium and Millennium Plusdevices should be fully supported, using their native NFTL and INFTL'translation layers'. Support for JFFS2 on DiskOnChip 2000 and Millennium isalso operational although lacking proper support for bad block handling.
CompactFlash -http://www.compactflash.org/
CompactFlash emulates an IDE disk, either throughthe PCMCIA-ATA standard, or by connecting directly to an IDE interface.
As such, it has no business being on this page, asto the best of my knowledge it doesn't have any alternative method of accessingthe flash - you have to use the IDE emulation - I mention it here forcompleteness.
读了上面这些,下面的这张图应该比较容易理解吧!
<!-- ><v:shape id="_x0000_i1027" type="#_x0000_t75" alt="" style='width:96pt;height:46.2pt'/><!--><!-- --><img alt="" height="62" width="128" border="0"><!-- --><!-- ><v:shape id="_x0000_i1028" type="#_x0000_t75" alt="" style='width:297pt;height:387pt'/><!--><!-- --><img alt="" height="516" width="396" border="0"><!-- -->
下面是MTD设备节点的命名规则,这个使用什么样的用户访问接口有关系。
Table 7-1.MTD /dev/ entries, corresponding(对应的) MTD user modules, and relevant(相应的) device major numbers
/dev entry
AccessibleMTD user module
Device type
Major number
mtdN
char device
char
90
mtdrN
read-only char device
char
90
mtdblockN
Blockdevice,read-onlyblock
device, JFFS, and JFFS2
block
31
nftlLN
NFTL
block
93
ftlLN
FTL
block
44
Table 7-2.MTD /dev/ entries, minornumbers, and naming schemes(方案)
/dev entry
Minor numberrange
Naming scheme
mtdN
0 to 32 perincrements of 2
N= minor / 2 偶数
mtdrN
1 to 33 perincrements of 2
N= (minor - 1) / 2 奇数
mtdblockN
0 to 16 perincrements of 1
N= minor
nftlLN
0 to 255 per sets of16
L= set;N= minor -(set - 1) x 16;Nisnot appended to entry name if its value is zero.
ftlLN
0 to 255 per sets of16
Same as NFTL.
有了上面的这些预备知识,以后的代码分析或许会更加容易理解些!
页:
[1]