MTD (memory technology device) is a Linux subsystem used to access memory devices (ROM, flash. The main purpose of MTD is to make the drive of the new memory device simpler. Therefore, MTD provides an abstract interface between the hardware and the upper layer. All source code of MTD is in the/Drivers/MTD subdirectory. The MTD device of the CFI interface is divided into four layers (from the device node to the underlying hardware driver), which are: device node, MTD device layer, MTD raw device layer, and hardware driver layer.
MTD original device description
All flash chips that make up MTD raw devices must be of the same type (whether interleave or address connection). The same structure is used to describe the data structure of MTD raw devices to form a flash chip. Each original MTD device has a mtd_info structure. The priv Pointer Points to a map_info structure. The fldrv_priv in the map_info structure points to a cfi_private structure, and the cfiq pointer in the cfi_private structure points to a cfi_ident structure, the chips Pointer Points to an array of flchip structures. The mtd_info, map_info, and cfi_private structures are used to describe the original MTD device. Because the nor flash is the same as the original MTD device, the cfi_ident structure is used to describe the flash chip information; the flchip structure is used to describe the proprietary information of each flash chip.
Root file system
File System
Character device Node
MTD character device
MTD block Device
MTD original device
Flash hardware driver
Block device Node
I. Flash hardware driver layer: the hardware driver layer drives flash hardware during init. The nor flash chip driver of Linux MTD follows the CFI interface standard, the driver is located in the drivers/MTD/chips subdirectory. The driver of NAND Flash is located in the/Drivers/MTD/NAND subdirectory.
2. MTD original device: the original device layer consists of two parts: the common code of the MTD original device and the data of each specific flash, such as partitioning.
The data structure used to describe the original MTD device is mtd_info, which defines a large amount of MTD data and operation functions. Mtd_table (mtdcore. c) It is a list of all the original MTD devices. mtd_part (mtd_part.c) is a structure used to represent the partition of the original MTD device, including mtd_info, because each partition is regarded as an MTD original device added to mtd_table, most of the data in mtd_part.mtd_info is obtained from the primary partition mtd_part-> master.
The drivers/MTD/maps/sub-directory stores specific flash data. Each file describes the flash on a board. Call add_mtd_device () and del_mtd_device () to create/Delete the mtd_info structure and add/Delete the mtd_table (or call add_mtd_partition () and del_mtd_partition () (mtdpart. c) Create/Delete the mtd_part structure and add mtd_part.mtd_info to/delete mtd_table
).
3. MTD device layer: based on the original MTD device, the Linux system can define MTD Block devices (master device number 31) and character devices (device Number 90 ). MTD character devices are defined in mtdchar. C.
Operation functions (lseek, open, close, read, and write ). MTD block device defines a structure mtdblk_dev that describes MTD Block devices, and declares an array of pointers named mtdblks, each of which is mtdblk_dev and mtd_table.
Mtd_info corresponds to each other.
4. device node: Use mknod to create MTD character device nodes (primary device Number 90) and MTD block device nodes (primary device number 31) in the/dev subdirectory ), you can access MTD character devices and Block devices by accessing this device node.
5. root file system: in bootloader, The jffs (or jffs2) file system image is jffs. image (or jffs2.img) is burned into a flash partition, in/ARCH/ARM/Mach-Your/arch. the your_fixup function of file C mounts the partition as the root file system.
6. File System: After the kernel is started, the mount command can be used to mount the remaining partitions in flash as the file system to mountpoint.
Function call relationship between the device layer and the original device layer (we need to implement the red part ):
An MTD original device can be divided into several MTD original devices by mtd_part and registered into mtd_table. Each MTD original device in mtd_table can be registered as an MTD device. The main device Number of the character device is 90, the number of sub-devices is 0, 2, 4, 6... (Read-only device for an odd number of times), the primary device number for a block device is 31, and the secondary device number is 0, 1, 2, 3...
Mtd_notifier
Character device mtd_fops block device mtd_fops
(Mtdchar. c) (mtdblock. c) mtdblks
Device layer
Register_mtd_user ()
Get_mtd_device ()
Unregister_mtd_user ()
Put_mtd_device ()
Erase_info
Mtd_notifiers
Mtd_table
Mtd_info
Mtd_part
(Mtdcore. c)
(Mtdpart. c)
Your flash
Your-flash.c)
Add_mtd_partitions ()
Del_mtd_partitions ()
Original device layer add_mtd_device ()
Del_mtd_device ()
Mtd_partition
Nor flash chip driver and MTD original device
All nor flash drive (PROBE) programs are placed under Drivers/MTD/chips. An MTD source device can be composed of one or several identical flash chips. Assume that four flash devices with devicetype X8 are used, each block is 8 Mb, interleave is 2, and the starting address is 0x01000000. The address is connected to each other, and an original MTD device (0x0000000-0x03000000) is formed ), the two interleave blocks are converted into one chip. The IP address ranges from 0x0000000 to 0x02000000, and the other two interleave blocks are converted into one chip. The IP address ranges from 0x02000000 to 0x03000000.
Note that all flash chips that constitute an MTD must be of the same type (whether interleave or address connection ), the data structure of the MTD raw device only uses the same structure to describe its flash chip.
Zero X 03000000
Zero X 02000000
Zero X 01000000
Each original MTD device has a mtd_info structure. The priv Pointer Points to a map_info structure. The fldrv_priv in the map_info structure points to a cfi_private structure, and the cfiq pointer in the cfi_private structure points to a cfi_ident structure, the chips Pointer Points to an array of flchip structures. The mtd_info, map_info, and cfi_private structures are used to describe the MTD original device. Because the nor flash is the same as the MTD original device, the cfi_ident structure is used to describe the flash chip information; the flchip structure is used to describe the proprietary information (such as the starting address) of each flash chip)