Linux 2.6.11mtd driver scenario analysis-general Linux technology-Linux programming and kernel information. The following is a detailed description. Original: Allen
In recent days, I have made some research on the MTD driver to familiarize myself with linux driver development. I think some of the articles I can find are not detailed enough, so I wrote some analysis on my own, hoping to help others and make my own memo ,. The blue text is excerpted from the Internet.
An embedded system often uses NOR flash or NAND flash to store bootload, kernel, and file systems.
The following is an analysis of the mtd driver in linux found on the network:
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 by registering a series of file operation functions (lseek, open, close, read, write ). MTD block device defines a structure mtdblk_dev that describes MTD Block devices, and declares a pointer array named mtdblks. Each mtdblk_dev in the array corresponds to each mtd_info in mtd_table.
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.
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.
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)
In general, an embedded system usually has one or more consecutive NOR flash or NAND flash spaces (each may be composed of multiple identical chips) every space like this is regarded as an MTD original device (I don't know who the name is, so can I use it) according to the variable names used in some articles and code, I will refer to it as the primary partition. You can divide the primary partition into Several partitions based on your needs. The partition information for my Development Board is as follows:
From alchemy_flash.c:
Static struct mtd_partition alchemy_partitions [] = {
{
. Name = "User FS", // here to the root file system
. Size = BOARD_FLASH_SIZE-0x00400000,
. Offset = 0x0000000
},{
. Name = "YAMON", // This piece is for bootloader
. Size = 0x0100000,
. Offset = MTDPART_OFS_APPEND, // indicates the next Partition
. Mask_flags = MTD_WRITEABLE
},{
. Name = "raw kernel ",
. Size = (0x300000-0x40000),/* last 256KB is yamon env * // This is the self-extracting compressed kernel, finally, the environment variable is reserved for booterloader. It is not used by the device driver, but accessed by booterloader in its own way.
. Offset = MTDPART_OFS_APPEND,
}
};
If you increase or decrease your flash space (by increasing or decreasing the flash chip) or you want to adjust the size of Several partitions, you only need to modify the table.
If you have another NAND partition, you may have the following Partition Table (au1550nd. c ):
Const static struct mtd_partition partition_info [] = {
{
. Name = "nand fs 0 ",
. Offset = 0,
. Size = 8*1024*1024
},
{
. Name = "nand fs 1 ",
. Offset = MTDPART_OFS_APPEND,
. Size = MTDPART_SIZ_FULL
}
};
Alchemy_flash.c has two functions: alchemy_mtd_init (void) and alchemy_mtd_cleanup ().
Int _ init alchemy_mtd_init (void)
{
Struct mtd_partition * parts;
Int nb_parts = 0;
Unsigned long window_addr;
Unsigned long window_size;
/*
* Now let's probe for the actual flash. Do it here since
* Specific machine settings might have been set above.
*/
Printk (KERN_NOTICE BOARD_MAP_NAME ": probing % d-bit flash bus \ n ",
Alchemy_map.bankwidth * 8 );
Alchemy_map.virt = ioremap (window_addr, window_size );
Mymtd = do_map_probe ("cfi_probe", & alchemy_map );
If (! Mymtd ){
Iounmap (alchemy_map.virt );
Return-ENXIO;
}
Mymtd-> owner = THIS_MODULE;
Look at the red area. do_map_probe returns a mtd_info structure pointer. It indicates that after finding your driver (cfi driver) correctly, this function will fill in the table and set the read/write functions to the correct values. The specific implementation will be analyzed in the future, one of the map_info parameters is not fully understood yet, because I have never studied the underlying flash Driver before. You need to set the bankwidth and base address, and the driver name, I can only guess through some information that it is used to manage the primary partition and use it for the underlying driver. For example, the bad block information is saved here.
Finally, call add_mtd_partitions to partition based on the partition table you set and the mtd_info of the primary partition. In fact, if you do not want to divide the primary partition into Several partitions, you can directly add the mtd_info to the mtd_table without processing in mtdpart. c. Next, let's take a look at this path: This function jumps to the file mtdpart. c. After you divide a primary partition into Several partitions, The mtd_info of these partitions is the same as that of your primary partition, except in terms of size and name. The add_mtd_partitions function is too big. It is not suitable to post all the functions. I 'd like to pick a few lines.
Slave-> mtd. type = master-> type; // copy the information of the primary partition.
Slave-> mtd. size = parts . Size; // changed the size.
Slave-> mtd. oobblock = master-> oobblock; // copy the information of the primary partition.
Slave-> mtd. name = parts. Name; // changed the name.
Slave-> mtd. bank_size = master-> bank_size; // copy the information of the primary partition.
Slave-> mtd. read = part_read; // This is your own. Let's take a look at the analysis below.
If (parts. Mtdp)
{/* Store the object pointer (caller may or may not register it */
* Parts. Mtdp = & slave-> mtd;
Slave-> registered = 0; // You can also not add it to mtd_table
}
Else
{
/* Register our partition */
Add_mtd_device (& slave-> mtd); // Finally, add the mtd_table instead of the mtd_part structure containing it.
Slave-> registered = 1;
}
Static int part_read (struct mtd_info * mtd, loff_t from, size_t len,
Size_t * retlen, u_char * buf)
{
Struct mtd_part * part = PART (mtd );
If (from> = mtd-> size)
Len = 0;
Else if (from + len> mtd-> size)
Len = mtd-> size-from;
If (part-> master-> read_ecc = NULL)
Return part-> master-> read (part-> master, from + part-> offset,
Len, retlen, buf );
Else
Return part-> master-> read_ecc (part-> master, from + part-> offset,
Len, retlen, buf, NULL, & mtd-> oobinfo );
}
It is clear that the offset is added when the READ function of the primary partition is called.
Add_mtd_partitions is called at the end of add_mtd_device (& slave-> mtd). Each partition is added to mtd_table. In mtdcore. c, you can directly add the primary partition as a parameter to mtd_table without partitioning.
Mtdcore. c, as its name is, is called core for a reason: it does not call any external function (except the registered callback function) of the mtd driver at all levels, but only outputs the function. It manages mtd_table. The underlying layer uses add_mtd_device and del_mtd_device to add and delete the original device (partition ). The upper-layer character devices and Block devices use get_mtd_device and put_mtd_device to apply for and release partitions.
Int add_mtd_device (struct mtd_info * mtd)
{
Int I;
Down (& mtd_table_mutex );
For (I = 0; I <MAX_MTD_DEVICES; I ++)
If (! Mtd_table){
Struct list_head * this;
Mtd_table= Mtd;
Mtd-> index = I;
Mtd-> usecount = 0;
DEBUG (0, "mtd: Giving out device % d to % s \ n", I, mtd-> name );
/* No need to get a refcount on the module containing
The notifier, since we hold the mtd_table_mutex */
List_for_each (this, & mtd_notifiers ){
Struct mtd_notifier * not = list_entry (this, struct mtd_notifier, list );
Not-> add (mtd); // In my opinion, if you do not intend to dynamically add or delete devices
// Is unnecessary. The linked list is empty when the system initializes the mtd device.
}
Up (& mtd_table_mutex );
/* We _ know _ we aren't being removed, because
Our caller is still holding us here. So none
Of this try _ nonsense, and no bitching about it
Either .:)*/
_ Module_get (THIS_MODULE );
Return 0;
}
Up (& mtd_table_mutex );
Return 1;
}
The entire function is relatively simple. In mtd_table, select an empty location to place the mtd_info of your partition. The red part is hard to understand: what is the role of the mtd_notifiers linked list? Let's see who has added something to this linked list. Register_mtd_user is the only member added to this linked list. Who called it? Look at the program under "tables ".
Void register_mtd_user (struct mtd_notifier * new)
{
Int I;
Down (& mtd_table_mutex );
List_add (& new-> list, & mtd_notifiers );
_ Module_get (THIS_MODULE );
For (I = 0; I <MAX_MTD_DEVICES; I ++)
If (mtd_table)
New-> add (mtd_table);
/* Register the notifier if/when the first device type is
Registered, to prevent the link/init ordering from fucking
Us over .*/
If (! Blktrans_notifier.list.next)
Register_mtd_user (& blktrans_notifier );
..... N rows omitted
Return 0;
}
It seems that this linked list usually has only two members. The role is to notify the character device and block device driver on the mtd partition when it is deleted or added. What?
When an mtd character device is created, mtdchar_devfs_init is called. Its notifier is like this:
Static struct mtd_notifier notifier = {
. Add = mtd_policy_add,
. Remove = mtd_policy_remove,
};
Static void mtd_policy_add (struct mtd_info * mtd)
{
If (! Mtd)
Return;
Devfs_mk_cdev (MKDEV (MTD_CHAR_MAJOR, mtd-> index * 2 ),
S_IFCHR | S_IRUGO | S_IWUGO, "mtd/% d", mtd-> index );
Devfs_mk_cdev (MKDEV (MTD_CHAR_MAJOR, mtd-> index * 2 + 1 ),
S_IFCHR | S_IRUGO, "mtd/% dro", mtd-> index );
}
This devfs_mk_cdev should be the automatically created devfs node, isn't it just a mknod? I am a newbie to linux. If anyone knows this, please don't hesitate to give me some advice. I just guessed it by name.
Summary:
In my understanding, the above mtd device driver architecture analysis is not very important for porting, except for the partition table. But what should I do when you change the flash chip model? I roughly looked at the driver part, refer to the following link article: http:// OS .yesky.com/lin/233/3386733.shtml master advanced Linux system MTD/CFI driver introduction.
I just learned a rough understanding of the so-called CFI (Common Flash Interface ). Is it true that this driver can solve all the problems? Trace the logs in another day, starting with do_map_probe.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service