Linux MTD Subsystem With Xilinx EMC Core - HOWTO
Linux MTD Subsystem With Xilinx EMC Core - HOWTO
HOWTO
Version 1.0
Author(s):
Linux MTD Subsystem with Xilinx EMC core - HOWTO
Table of Contents
Summary..............................................................................................................................4
1 Introduction..................................................................................................................5
1.1 Chip drivers.........................................................................................................6
1.2 Map drivers..........................................................................................................6
1.3 User modules.......................................................................................................6
1.4 MTD utilities.......................................................................................................6
2 Configuring the MTD Subsystem................................................................................7
2.1 Enabling the MTD support........................................................................................7
2.2 Choosing the Chip driver.....................................................................................7
2.3 Choosing the Map driver.....................................................................................8
2.3.1 CFI Flash device in physical memory map.................................................8
2.3.2 Flash device in physical memory map based on OF description...............10
2.4 Emulating a char or block device......................................................................11
3 Working with the MTD enabled Flash......................................................................14
3.1 Erasing the flash................................................................................................14
3.2 Writing to the flash............................................................................................15
3.3 Reading from the flash.......................................................................................15
3.4 Creating partitions on the flash..........................................................................16
3.4.1 Command line partition table parsing........................................................16
3.4.2 Redboot partition table parsing..................................................................17
3.4.3 Flash partition map based on OF description............................................18
4 Mounting jffs2 filesystem on the MTD device..........................................................19
5 Unsorted Block Images (UBI)...................................................................................20
5.1 Features of the UBI............................................................................................21
5.2 UBI Utilities.......................................................................................................22
5.3 Attaching a MTD device to UBI.......................................................................22
5.4 Creating UBI volumes.......................................................................................23
5.5 Mounting JFFS2 on UBI volume......................................................................23
6 References..................................................................................................................25
7 Revision History........................................................................................................25
2
Linux MTD Subsystem with Xilinx EMC core - HOWTO
Table of Figures
Figure 1 Linux MTD Subsystem.........................................................................................5
Figure 2 Enabling the MTD subsystem...............................................................................7
Figure 3 Choosing the Chip driver......................................................................................8
Figure 4 Choosing the Map driver.......................................................................................9
Figure 5 CFI Flash device in physical memory map...........................................................9
Figure 6 Setting buswidth/chip-interleave for CFI Flash device in physical memory map
...........................................................................................................................................10
Figure 7 Flash device in physical memory map based on OF description........................11
Figure 8 Char device Emulation........................................................................................12
Figure 9 Command line partition table parsing.................................................................16
Figure 10 Enabling UBI support........................................................................................20
Figure 11 Enabling UBI debug support.............................................................................21
Figure 12 Emulating MTD device on UBI volume...........................................................24
3
Linux MTD Subsystem with Xilinx EMC core - HOWTO
1 Summary
This document describes the usage of the Memory Technology Device (MTD)
Subsystem of Linux in conjunction with Xilinx EMC core and external NOR flash chips. It
starts with a basic view of the MTD in kernel and proceeds through enabling the MTD
support in your kernel, working with the MTD enabled flash devices, and mounting
filesystems onto the flash devices, etc. This document doesn’t provide information about
the usage of MTD with NAND chips.
4
Linux MTD Subsystem with Xilinx EMC core - HOWTO
2 Introduction
This document describes the usage of the Linux Memory Technology Device (MTD)
Subsystem of Linux in conjunction with Xilinx EMC core and external NOR flash chips.
The Linux MTD subsystem provides support for flash (NOR and NAND) and similar non-
volatile storage devices. MTD uses a layered approach; it is easy to add new devices
and have them functional. MTD is integrated into Linux kernel and also provides
mechanisms to mount filesystems on to the flash. The Figure 1 Linux MTD Subsystem
provides a detailed view of the MTD Subsystem in the Linux kernel.
User Applications
User Space
NAND Libraries
Chip and Data MTD
Drivers structures
Kernel Space
Probe I/O
H/W
NAND NO
R
5
Linux MTD Subsystem with Xilinx EMC core - HOWTO
6
Linux MTD Subsystem with Xilinx EMC core - HOWTO
7
Linux MTD Subsystem with Xilinx EMC core - HOWTO
In Kernel configuration -> Device Drivers -> MTD support -> go to sub-menu for
“Mapping Drivers for chip access” and enable “CFI Flash device in physical memory
map” (CONFIG_MTD_PHYSMAP) and specify the physical start address, physical
length, and bank width. This option uses physmap.c (/drivers/mtd/maps/) driver to
register the flash device.
8
Linux MTD Subsystem with Xilinx EMC core - HOWTO
9
Linux MTD Subsystem with Xilinx EMC core - HOWTO
If you have chosen to physically configure the flash device, go to Kernel configuration ->
Device Drivers -> MTD support -> RAM/ROM chip drivers and enable ‘Flash chip driver
advanced configuration options’-> ‘Specific CFI flash geometry selection’ and enter the
bus width and chip interleave based on your hardware configuration.
Figure 6 Setting buswidth/chip-interleave for CFI Flash device in physical memory map
10
Linux MTD Subsystem with Xilinx EMC core - HOWTO
11
Linux MTD Subsystem with Xilinx EMC core - HOWTO
Save the kernel configuration and boot your kernel. You should be able to see
messages similar to those below (they might slightly defer based on the map driver
used), on your console. It gives the list of features supported by the flash device, mtd
device id for the flash device/partition, etc.
12
Linux MTD Subsystem with Xilinx EMC core - HOWTO
After booting the kernel, try cat /proc/mtd. This gives a list of MTD enabled devices.
cat /proc/mtd
[----output-----------------------------------------]
dev: size erasesize name
mtd0: 00800000 00040000 "80800000.flash"
The name of your MTD enabled flash depends on the map driver you are using.
CONFIG_MTD_PHYSMAP (physmap.c) uses the format phys_mapped_flash, while
CONFIG_MTD_PHYSMAP_OF (physmap_of.c) uses the format address.flash, where
address is the physical memory area to which your flash device is mapped.
13
Linux MTD Subsystem with Xilinx EMC core - HOWTO
14
Linux MTD Subsystem with Xilinx EMC core - HOWTO
flash_erase -h | --help
Ex:
./flash_erase /dev/mtd0 0x00000 1
[----output-----------------------------------------]
MTD_open
MTD_ioctl
MTD_ioctl
MTD_ioctl
Total 1 Units
Region 0 is at 0 of 32 sector and with sector size 40000
Performing Flash Erase of length 262144 at offset 0x0
MTD_close
Done
[----output-----------------------------------------]
Ex:
cat /usr/mtd.txt > /dev/mtd0
[----output-----------------------------------------]
MTD_open
MTD_write
MTD_close
[----output-----------------------------------------]
Ex:
cat /dev/mtd0 > usr.txt
[----output-----------------------------------------]
MTD_open
15
Linux MTD Subsystem with Xilinx EMC core - HOWTO
MTD_read
MTD_close
[----output-----------------------------------------]
In the device tree, add the partition table to “bootargs”. The format for the
command line partition table is as follows:
16
Linux MTD Subsystem with Xilinx EMC core - HOWTO
mtdparts=<mtddef>[;<mtddef]
where:
<mtddef> := <mtd-id>:<partdef>[,<partdef>]
<mtd-id> := unique id used in mapping
driver/device
<partdef> := <size>[@offset][<name>][ro]
<size> := standard linux memsize OR "-"
to denote all remaining space
<name> := (NAME)
Ex: Let’s assume that you have a cfi-flash, of size 8MB, mapped to physical
memory at 0x80800000 and have enabled CONFIG_MTD_PHYSMAP_OF.
If you want to create three partitions named bootloader (size=512K), kernel
(size=1M) and rootfs (size=rest of the flash), then add the following to the
kernel command line. The parameter ro makes a partition read-only.
mtdparts=80800000.flash: 512K(bootloader)ro,1M(kernel)ro,
-(rootfs)
17
Linux MTD Subsystem with Xilinx EMC core - HOWTO
18
Linux MTD Subsystem with Xilinx EMC core - HOWTO
Note the use of /dev/mtdblock0, NOT /dev/mtd0. "mount" needs a block device
interface and /dev/mtdblock0, 1, 2, 3... are provided for that purpose. /dev/mtd0, 1, 2,
3 are char devices and are provided for things like copying the binary image onto the
raw flash devices (create the /mnt/jffs2 directory if it doesn’t exist).
Once the filesystem is mounted, verify that the contents of the /mnt/jffs2
directory are same as the directory for which the jffs2 image has been created
above. We can create/modify the files within the /mnt/jffs2 directory and these
changes will be reflected on to the flash.
More information on jffs2 filesystem can be found at http://linux-
mtd.infradead.org/~dwmw2/jffs2.pdf and the FAQ page for JFFS2 on MTD website is
at http://www.linux-mtd.infradead.org/faq/jffs2.html.
19
Linux MTD Subsystem with Xilinx EMC core - HOWTO
20
Linux MTD Subsystem with Xilinx EMC core - HOWTO
21
Linux MTD Subsystem with Xilinx EMC core - HOWTO
reboot happens during the operation; this is might be very useful for the upper-level
software (e.g., for a file-system);
UBI has an un-map operation, which just un-maps a logical erase block from the
physical erase block, schedules the physical erase block for erasure and returns; this
is very quick and frees upper level software from implementing their own
mechanisms to defer erasures (e.g., JFFS2 has to implements such mechanisms).
22
Linux MTD Subsystem with Xilinx EMC core - HOWTO
ubiattach /dev/my_ubi0 –m 0
All the above three examples attach mtd0 to UBI. Below are the kernel boot-up
messages of a system in which a MTD device is attached to UBI.
mtd: Giving out device 0 to 80800000.flash
UBI: attached mtd0 to ubi0
UBI: MTD device name: "80800000.flash"
UBI: MTD device size: 8 MiB
UBI: physical eraseblock size: 262144 bytes (256 KiB)
UBI: logical eraseblock size: 262016 bytes
UBI: number of good PEBs: 32
UBI: number of bad PEBs: 0
UBI: smallest flash I/O unit: 1
UBI: VID header offset: 64 (aligned 64)
UBI: data offset: 128
UBI: max. allowed volumes: 128
UBI: wear-leveling threshold: 4096
UBI: number of internal volumes: 1
UBI: number of user volumes: 0
UBI: available PEBs: 28
UBI: total number of reserved PEBs: 4
UBI: number of PEBs reserved for bad PEB handling: 0
UBI: max/mean erase counter: 3/1
UBI: background thread "ubi_bgt0d" started, PID 680
23
Linux MTD Subsystem with Xilinx EMC core - HOWTO
To mount JFFFS2 filesystem on the emulated MTD device follow the procedure
described in the section, Mounting jffs2 filesystem on a MTD device. More information
on UBI is available at www.linux-mtd.infradead.org/doc/ubi.html.
24
Linux MTD Subsystem with Xilinx EMC core - HOWTO
7 References
Essential Linux Device Drivers by SreeKrishnan Venkateshwaran
Linux MTD website at http://www.linux-mtd.infradead.org/index.html
8 Revision History
The table below contains a list of revision changes to this document.
Revisions
Date Version Author Description
6/27/2008 1.0 Initial version created
25