General
General MTD documentationTable of contentsMTD overviewMTD APIMTD testsThe mtdblock driverOld MTD documentationMTD overviewMTD概述MTD subsystem (stands for Memory Technology Devices) provides an abstract
General MTD documentation
Table of contents
MTD overview
MTD概述
MTD subsystem (stands for Memory Technology Devices) provides an abstraction layer for raw flash devices. It makes it possible to use the same API when working with different flash types and technologies, e.g. NAND, OneNAND, NOR, AG-AND, ECC'd NOR, etc.
MTD子系统(代表存储技术设备)为原始闪存设备提供了一个抽象层。它使得当使用不同的flash类型和技术时(如NAND,OneNAND,NOR,AG-AND,ECC'd NOR等),可以使用相同的API。
MTD subsystem does not deal with block devices like MMC, eMMC, SD, CompactFlash, etc. These devices are not raw flashes but they have a Flash Translation layer inside, which makes them look like block devices. These devices are the subject of the Linux block subsystem, not MTD. Please, refer to this FAQ section for a short list of the main differences between block and MTD devices. And the raw flash vs. FTL devices UBIFS section discusses this in more details.
MTD子系统不支持MMC,eMMC,SD,CompactFlash等设备。这些设备不是原始Flash,但是它们内部有一个Flash翻译层,这使得它们看着和块设备一样。这些设备是Linux块子系统的主题,而不是MTD。有关块设备和MTD设备之间的主要区别的简短列表,请参阅此常见问题部分(http://www.linux-mtd.infradead.org/faq/general.html#L_mtd_vs_hdd)。原始闪存与FTL设备UBIFS部分更详细地讨论了这一点(http://www.linux-mtd.infradead.org/doc/ubifs.html#L_raw_vs_ftl)。
MTD subsystem has the following interfaces.
MTD子系统有如下接口。
- MTD character devices - usually referred to as
/dev/mtd0
,/dev/mtd1
, and so on. These character devices provide I/O access to the raw flash. They support a number ofioctl
calls for erasing eraseblocks, marking them as bad or checking if an eraseblock is bad, getting information about MTD devices, etc.
MTD字符设备-通常称为/dev/mtd0,/dev/mtd1等。这些字符设备提供对原始闪存的IO访问。它们支持许多ioctl调用,用于擦除可擦除块,将擦除块标记为坏块或检查擦除块是否损坏,获取有关MTD设备的信息等。 - The
sysfs
interface is relatively newer and it provides full information about each MTD device in the system. This interface is easily extensible and developers are encouraged to use thesysfs
interface instead of olderioctl
or/proc/mtd
interfaces, when possible. Thesysfs
interface for the mtd subsystem is documentated in the kernel, and currently can be found atDocumentation/ABI/testing/sysfs-class-mtd
.
sysfs接口相对较新,它提供了系统中每个MTD设备的完整信息。这些接口易于扩展而且开发人员被鼓励尽可能去使用sysfs接口替代老的ioctl或/proc/mtd接口。mtd子系统的sysfs接口在kernel中有文档来描述,目前可以在Documentation/ABI/testing/sysfs-class-mtd中找到。
- The
/proc/mtd
proc file system file provides general MTD information. This is a legacy interface and the sysfs interface provides more information.
proc文件系统的/proc/mtd文件提供了通用MTD信息。这是一个传统接口,sysfs接口提供了更多信息。
MTD subsystem supports bare NAND flashes with software and hardware ECC, OneNAND flashes, CFI (Common Flash Interface) NOR flashes, and other flash types.
MTD子系统支持具有软件和硬件ECC的裸NAND闪存、OneNAND闪存、CFI(通用闪存接口)NOR闪存以及其他闪存类型。
Additionally, MTD supports legacy FTL/NFTL "translation layers", M-Systems' DiskOnChip 2000 and Millennium chips, and PCMCIA flashes (pcmciamtd
driver). But the corresponding drivers are very old and not maintained very much.
另外,MTD还支持传统的FTL/NFTL“转换层”,M-Systems公司的DiskOnChip 2000和Millennium芯片,以及PCMCIA闪存(pcmciamtd驱动程序)。但是相应的驱动程序都很旧,维护也不多。
MTD API
MTD 应用接口
The MTD subsystem API is defined in include/linux/mtd/mtd.h
. The methods and data structures in this file are used by higher layer kernel code such as flash file systems to access and control the mtd devices, and also by device driver authors to interface their device to the mtd subsystem. The various methods by which a driver provides access to the device are defined within struct mtd_info
. Prior to kernel version 3.4, higher layers called the driver methods directly by way of a pointer to struct mtd_info
. As of kernel 3.4, these methods are implemented within the mtd subsystem core code, which then calls the corresponding driver methods. Users of kernel 3.4 and later should not call the driver methods directly, but instead use those prototyped in mtd.h
outside of struct mtd_info
. These methods include mtd_read()
, mtd_write()
, etc.
MTD子系统的应用接口在 include/linux/mtd/mtd.h 文件中定义。更高层的内核代码(如闪存文件系统)使用该文件里的函数和数据结构来访问和控制MTD设备,设备驱动程序作者也使用这些方法和数据结构将其设备连接到MTD子系统。驱动程序提供对设备的访问的各种方法在结构体mtd_info中定义。内核3.4中,这些方法在MTD子系统核心代码中实现,然后调用相应的驱动程序方法。内核3.4及更高版本的用户不应直接调用驱动函数,而应该在结构体mtd_info外部使用在mtd.h中原型化的驱动程序函数。这些函数包括mtd_read()
, mtd_write()
,等等。
Absent an error, the API methods will return zero, with two notable exceptions. mtd_read()
and mtd_read_oob()
may return -EUCLEAN
in some circumstances. This return code is applicable mainly to NAND flash devices, and is used to indicate that some bit errors were corrected by the device's ECC facility. Prior to kernel version 3.4, -EUCLEAN
was returned if one or more bit errors were corrected during the read operation. As of kernel 3.4, the meaning is more nuanced, and can be broadly interpreted to mean "a dangerously high number of bit errors were corrected". The -EUCLEAN
return code is intended to help higher layers detect degradation of erase blocks. The conditions by which mtd_read()
and mtd_read_oob()
return -EUCLEAN
can be tuned using the bitflip_threshold
element of the sysfs interface. Please see the kernel documentation for the MTD sysfs interface (referenced above) before adjusting this value.
如果没有错误,API函数会返回0,但是有两个值得注意的例外,mtd_read()和mtd_read_oob()在某些情况下可能会返回 -EUCLEAN。此返回代码主要适用于NAND闪存设备,用于指示设备的ECC工具纠正了某些位错误。在内核3.4版之前,如果在读取操作期间纠正了一个或多个位错误,则会返回 -EUCLEAN。在内核3.4版本中,这一含义更加微妙,可以广泛地解释为“纠正了高得危险的比特错误”。-EUCLEAN返回代码旨在帮助更高层侦测擦除块的衰退。可以使用sysfs接口的bitflip_Threshold元素调整mtd_read()和mtd_read_oob()返回 -EUCLEAN的条件。请在调整此值前,查阅MTD sysfs接口(如上所述)的内核文档。
MTD tests
MTD 测试
The mtd-utils include a set of test programs which you may run to verify your flash hardware and drivers. The programs have only been recently ported to user space and are also available as kernel modules.
mtd-utils包括一系列测试函数,你可以运行这些测试函数来测试你的flash硬件和驱动。这些程序最近才被移植到用户空间,也可以作为内核模块使用。
In contrast to the modules, the user space tests also offer more fine grained options for controling their behaviour, such as only using specific erase blocks or pages.
与模块形成对比的是,用户空间测试还提供了更详细的选项来控制它们的行为,例如只使用特定的擦除块或页面。
The user space tests are compiled automatically when compiling mtd-utils, but are not installed by default. To install the tests through make install
, the configure option --enable-install-tests
has to be set.
编译mtd-utils时,用户空间测试函数也会被自动编译,但是默认不会安装。要安装这些测试需要通过make install,配置选项 --enable-install-tests 需要被置位。
The kernel module tests are available in the mainline kernels starting from kernel version 2.6.29
and they live in the drivers/mtd/tests
directory of the linux kernel source codes. You may compile the tests as kernel modules by enabling them in the kernel configuration menu by marking: "Device Drivers" -> "Memory Technology Device (MTD) support" -> "MTD tests support" (or the MTD_TESTS
symbol in the .config
file).
从内核版本2.6.29开始的主线内核中提供内核模块测试,他们在linux内核源码的drivers/mtd/tests 路径下。通过在内核配置菜单项中选择:
"Device Drivers" -> "Memory Technology Device (MTD) support" -> "MTD tests support"(或者.config文件中的 MTD_TESTS
标志),你可以将这些测试编译为内核模块。
If you have a pre-2.6.29
kernel, you may find the tests here:
如果你使用的是2.6.29之前的内核,你可以在一下位置找到测试:
git://git.infradead.org/users/ahunter/nand-tests.git
The MTD test-suite contains the following tests:
MTD测试套件包含一下测试:
- nandbiterrs: relevant only for NAND flashes, introduces bit errors and tests for multi-bit error recovery on a NAND page. This mostly tests the ECC controller / driver. The kernel module version is called mtd_nandbiterrs.
nandbiterrs:仅与NAND闪存相关,介绍NAND页上的位错误和多位错误恢复测试。多用于测试ECC控制器/驱动。这个内核模块版本叫做 mtd_nandbiterrs。 - flash_speed: measures and reports read/write/erase speed of the MTD device. The kernel module version is called mtd_speedtest.
flash_spedd:测量并报告读/写/擦除MTD设备的速度。这个内核模块版本叫做 mtd_speedtest。 - flash_stress: performs random read/write/erase operations and validates the MTD device I/O capabilities. The kernel module version is called mtd_stresstest.
flash_stress:执行任意的读/写/擦除操作并且验证MTD设备的I/O功能。这个内核模块被叫做mtd_stresstest。 - flash_readtest: this tests reads from an MTD device, one NAND page at a time including OOB (or 512 bytes at a time in case of flashes like NOR) and checks that reading works properly. The kernel module version is called mtd_readtest.
flash_readtest:这个测试将从读MTD设备,一次读1个NAND页,包含OOB(或者512字节一次,在NOR flash的情况下),并检测读取是否正常。这个内核模块版本被叫做 mtd_readtest。 - nandpagetest: relevant only for NAND flashes, tests NAND page writing and reading in different sizes and order; this test was originally developed for testing the OneNAND driver, so it might be a little OneNAND-oriented, but must work on any NAND flash. The kernel module version is called mtd_pagetest.
nandpagetest:仅用于NAND闪存,在不同大小和顺序下测试NAND页的读取;这个测试最初是为测试OneNAND驱动程序而开发的,因此它可能有点面向OneNAND,但必须适用于任何NAND闪存。这个内核模块版本叫做 mtd_pagetest。 - mtd_oobtest: currently only exists as a kernel module. Relevant only for NAND flashes, tests that the OOB area I/O works properly by writing data to different offsets and verifying it.
mtd_oobtest:目前仅作为内核模块存在。仅用于NAND闪存,通过在不同的偏移位置写数据并测试来检测OOB区域I/O功能是否正常。 - nandsubpagetest: relevant only for NAND flashes, tests sub-page I/O. The kernel module version is called mtd_subpagetest.
nandsubpagetest:仅用于NAND闪存,测试小页的I/O。这个内核模块版本叫做 mtd_subpagetest。 - flash_torture: this test is designed to wear out flash eraseblocks. It repeatedly writes and erases the same group of eraseblocks until an I/O error happens, so be careful! It may be very good idea to run this test for some time and validate your flash driver and HW, providing you have a spare device. For example, we caught rather rare and nasty DMA issues on an OMAP2 board with OneNAND flash, just by running this tests for few hours. The kernel module version is called mtd_torturetest and also supports a number of options (see
modinfo mtd_torturetest
).
flash_torture:这个测试旨在磨损闪存可擦除块。它会不断对同一区域的可擦除块进行写和擦除的动作,直到出现I/O错误,所以要小心了!如果你有一个备用设备,那么运行此测试一段时间并验证闪存驱动程序和硬件可能是一个非常好的主意。例如,仅通过运行此测试几个小时,我们就在带有OneNAND闪存的OMAP2板上发现了相当罕见和严重的DMA问题。这个内核模块版本被叫做 mtd_torturetest,它还支持很多选项(见modinfo mtd_torturetest
)。 - mtd_nandecctest: a simple test that checks correctness of the built-in software ECC for 256 and 512-byte buffers; this test is not driver-specific but tests general NAND support code. This tests only exists as a kernel module, as it tests the internal software ECC implementation.
mtd_nandecctest:一个简单的测试,用于检查256和512字节缓冲区的内置软件ECC的正确性;这个测试不是特定用于驱动程序的,而是支持通用NAND的代码。这个测试仅作为内核模块存在,因为它测试内部软件ECC实现。
The mtdblock driver
mtdblock 驱动
The mtdblock
driver available in the MTD is an archaic tool which emulates block devices on top of MTD devices. It does not even have bad eraseblock handling, so it is not really usable with NAND flashes. And it works by caching a whole flash erase block in RAM, modifying it as requested, then erasing the whole block and writing back the modified. This means that mtdblock
does not try to do any optimizations, and that you will lose lots of data in case of power cuts. And last, but not least, mtdblock
does not do any wear-leveling or bit-flips handling.
MTD中的mtdblock驱动过时的工具,它在MTD设备上模拟块设备。它甚至没有坏块处理,因此它十分不适合用在NAND flash上。它的工作原理是在RAM中缓存整个闪存擦除块,根据请求对其进行修改,然后擦除整个块并写回擦除后的块。这意味着 mtdblock 不会尝试进行任何优化,并且在断电的情况下会损失大量的数据。最后,但并非嘴不重要的一点是,mtdblock 没有做对任何磨损均衡和位反转做处理。
Often people consider mtdblock
as general FTL layer and try to use block-based file systems on top of bare flashes using mtdblock
. This is wrong in most cases. In other words, please, do not use mtdblock
unless you know exactly what you are doing.
通常,人们认为mtdblock是通用的FTL层,并尝试使用mtdblock在裸闪存之上使用基于块的文件系统。大多数情况下这是错误的做法。换句话说,请不要使用 mtdblcok,除非你真的清除的知道你在做什么。
There is also a read-only version of this driver, mainly for use with uCLinux where the extra RAM requirement was considered too large. However, just like the R/W version of the driver, there is no wear-levelling and bit-flips handling.
该驱动程序还有一个只读版本,主要用在uCLinux,在uCLinux中额外的RAM被认为消耗太大。但是,和可读写的版本一样,这个只读版本也没有负载均衡和位反转的处理。
Instead of using this old driver, you may check the R/O block device emulation provided by UBI
useful. Please refer to the UBI section for more details.
您可以检查UBI提供的R/O块设备仿真是否有用,而不是使用这个旧的驱动程序。有关更多详细信息,请参阅UBI部分。(http://www.linux-mtd.infradead.org/doc/ubi.html#L_ubiblock)
Old MTD documentation
旧的MTD文档
Old MTD web site and old MTD documentation is available here. Old NAND flash interface description is available here.
旧的MTD网页和文档在这里(http://www.linux-mtd.infradead.org/archive/index.html)。旧的NAND闪存接口描述在这里(http://www.linux-mtd.infradead.org/tech/mtdnand/index.html)。
更多推荐
所有评论(0)