<PAGE>
<VAR match="VAR_ORIGIN" replace="../" />
<VAR match="VAR_CVSID" replace="Last updated: 14 Oct 2008, dedekind"/>
<INCLUDE file="../inc/header.tmpl" />
<VAR match="VAR_SEL_DOC" replace="selected" />
<VAR match="VAR_SEL_GENERAL" replace="selected" />
<PARSE file="../menu1.xml" />

<INCLUDE file="../inc/content.tmpl" />

<h1>General MTD documentation</h1>

<h2>Table of contents</h2>
<ol>
	<li><a href="general.html#L_overview">MTD overview</a></li>
	<li><a href="general.html#L_mtd_api">MTD API</a></li>
	<li><a href="general.html#L_mtd_tests">MTD tests</a></li>
	<li><a href="general.html#L_mtdblock">The <code>mtdblock</code> driver</a></li>
	<li><a href="general.html#L_old_docs">Old MTD documentation</a></li>
</ol>



<h2><a name="L_overview">MTD overview</a></h2>

<p>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.</p>

<p>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
<a href="../faq/general.html#L_mtd_vs_hdd">this</a> FAQ section for a short
list of the main differences between block and MTD devices. And the
<a href="ubifs.html#L_raw_vs_ftl">raw flash vs. FTL devices</a> UBIFS section
discusses this in more details.</p>

<p>MTD subsystem has the following interfaces.</p>

<ul>
	<li>MTD character devices - usually referred to as
	<code>/dev/mtd0</code>, <code>/dev/mtd1</code>, and so on. These
	character devices provide I/O access to the raw flash. They support a
	number of <code>ioctl</code> calls for erasing eraseblocks, marking
	them as bad or checking if an eraseblock is bad, getting information
	about MTD devices, etc.</li>

	<li>The <code>sysfs</code> 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 the
	<code>sysfs</code> interface instead of older <code>ioctl</code> or
	<code>/proc/mtd</code> interfaces, when possible. The <code>sysfs</code>
	interface for the mtd subsystem is documentated in the kernel, and
	currently can be found at
	<code>Documentation/ABI/testing/sysfs-class-mtd</code>.</li>

	<li>The <code>/proc/mtd</code> proc file system file provides general
	MTD information. This is a legacy interface and the sysfs interface
	provides more information.</li>
</ul>

<p>MTD subsystem supports bare <a href="nand.html">NAND</a> flashes with
software and hardware ECC, <a href="onenand.html">OneNAND flashes</a>, CFI
(<a href="http://www.pentium.com/design/flcomp/technote/cfi_1_1.htm">Common Flash Interface</a>)
NOR flashes, and other flash types.</p>

<p>Additionally, MTD supports legacy FTL/NFTL "translation layers",
M-Systems' DiskOnChip 2000 and Millennium chips, and PCMCIA flashes
(<code>pcmciamtd</code> driver). But the corresponding drivers are very old and
not maintained very much.</p>



<h2><a name="L_mtd_api">MTD API</a></h2>

<p>The MTD subsystem API is defined in <code>include/linux/mtd/mtd.h</code>.
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 <code>struct mtd_info</code>.  Prior to kernel version 3.4, higher layers
called the driver methods directly by way of a pointer to <code>struct
mtd_info</code>.  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 <code>mtd.h</code> outside of <code>struct
mtd_info</code>.  These methods include <code>mtd_read()</code>,
<code>mtd_write()</code>, etc.</p>

Absent an error, the API methods will return zero, with two notable exceptions.
<code>mtd_read()</code> and <code>mtd_read_oob()</code> may return
<code>-EUCLEAN</code> 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,
<code>-EUCLEAN</code> 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 <code>-EUCLEAN</code> return code is intended to help higher
layers detect degradation of erase blocks.  The conditions by which
<code>mtd_read()</code> and <code>mtd_read_oob()</code> return
<code>-EUCLEAN</code> can be tuned using the <code>bitflip_threshold</code>
element of the sysfs interface.  Please see the kernel documentation for the MTD
sysfs interface (referenced above) before adjusting this value.


<h2><a name="L_mtd_tests">MTD tests</a></h2>

<p>The MTD subsystem includes a set of tests which you may run to verify your
flash hardware and drivers. The tests are available in the mainline kernels
starting from kernel version <code>2.6.29</code> and they live in the
<code>drivers/mtd/tests</code> 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: "<b>Device Drivers</b>" ->
"<b>Memory Technology Device (MTD) support</b>" -> "<b>MTD tests support</b>" (or
the <code>MTD_TESTS</code> symbol in the <code>.config</code> file).</p>

<p>If you have a pre-<code>2.6.29</code> kernel, you may find the tests
here:</p>
<pre>
git://git.infradead.org/users/ahunter/nand-tests.git
</pre>

<p>The MTD test-suite contains the following tests:</p>
<ul>
	<li><b>mtd_speedtest</b>: measures and reports read/write/erase speed
	of the MTD device.</li>

	<li><b>mtd_stresstest</b>: performs random read/write/erase operations
	and validates the MTD device I/O capabilities.</li>

	<li><b>mtd_readtest</b>: this tests reads whole 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.</li>

	<li><b>mtd_pagetest</b>: 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.</li>

	<li><b>mtd_oobtest</b>: relevant only for NAND flashes, tests that the
	OOB area I/O works properly by writing data to different offsets and
	verifying it.</li>

	<li><b>mtd_subpagetest</b>: relevant only for NAND flashes, tests
	<a href="ubi.html#L_subpage">sub-page</a> I/O.</li>

	<li><b>mtd_torturetest</b>: 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! The test
	supports a number of options (see <code>modinfo mtd_torturetest</code>)
	which allow you to set the amount of eraseblocks to torture and how the
	torturing is done. You may limit the amount of torturing cycles using
	the <code>cycles_count</code> module parameter. It may be very god 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.</li>

	<li><b>mtd_nandecctest</b>: 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.</li>
</ul>



<h2><a name="L_mtdblock">The mtdblock driver</a></h2>

<p>The <code>mtdblock</code> 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
<code>mtdblock</code> 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,
<code>mtdblock</code> does not do any wear-leveling.</p>

<p>Often people consider <code>mtdblock</code> as general FTL layer and try to
use block-based file systems on top of bare flashes using
<code>mtdblock</code>. This is wrong in most cases.
In other words, please, <b>do not use</b> <code>mtdblock</code> unless you
know exactly what you are doing.</p>

<p>There is also a read-only version of this driver which doesn't have the
capacity to do the caching and erase/writeback, mainly for use with uCLinux
where the extra RAM requirement was considered too large.</p>



<h2><a name="L_old_docs">Old MTD documentation</a></h2>

<p>Old MTD web site and old MTD documentation is available
<a href="../archive/index.html">here</a>. Old NAND flash interface description
is available <a href="../tech/mtdnand/index.html">here</a>.</p>


<INCLUDE file="../inc/footer.tmpl" />
</PAGE>
