SD Card Driver Analysis

Source: Internet
Author: User

http://blog.csdn.net/dacaozuo/article/details/8156302

Android SD card driver and standard Linux under the SD card driver does not seem to be much different, here on the high-pass Android 2.3 to represent, to briefly analyze the Linux under the SD card driver. Because the small brother's technology is limited, the analysis of the wrong place, please correct me, you can learn together.


A First to clarify some basic concepts of SD, MMC and Sdio:

(1) MMC: (Multi Media card) is an earlier memory card standard, has been replaced by the SD standard.

(2) SD: (Secure Digital memory card) is a Flash memory card standard, which is generally common SD card, is fully compatible with the MMC standard.

(3) SDIO: Secure digital input and output card. The SDIO is a peripheral interface defined on the SD standard that connects peripherals via SD I/O pins and transmits data to these peripherals via I/O data connections on the SD. is currently the more popular technology, there are many WiFi, GPS, Bluetooth, radio and other modules have Sdio interface, can also be called "Sdio card."

(4) The operating mode of the interface: the operating mode is the controller of the CPU, that is, the CPU can be set up a number of registers to the self-controller to meet the requirements of a plug-in mode of operation, namely: SDIO, traditional SD mode and MMC mode of operation. My understanding of these models is that they correspond to the different external devices of the above 1-3 three points. Because the transmission protocols for these three devices are different. The compatibility relationship is: "Sdio card" > "SD Card" > "MMC card", which in turn just doesn't set up.

(5) interface transmission mode: transmission is also for the controller of the CPU, the general CPU controller will support three transmission modes, namely: SPI mode, 1-wire mode, 4-wire mode. So how can we ensure that the CPU works in the mode we require and how it is transmitted. is done by writing the CPU's control register. For example, the function Msmsdcc_set_ios mentioned below (struct mmc_host *mmc,struct mmc_ios *ios).

(6) MMC/SD card initialization instructions and various states: in the Internet to find the following images, can be very clear reflection of the SD card initialization instructions, the following introduction of the code is sent in accordance with the instructions in this order actually sent:


Two. The structure level of SD card drive under Android (LINUX)

We know that the Linux drive is like layering, like the IIC and input subsystem and other drivers, Linux SD card driver is also sub-level, in our Code KERNEL/DRIVERS/MMC directory contains three subdirectories, respectively: host, card, Core three parts:

The host part is for different host drivers, this is the driver engineers need to according to their own characteristics of the platform to complete, but Qualcomm released the CodeBase has helped us to do this part.

Core part: This is the kernel of the entire MMC, this part of the implementation of different protocols and specifications, and for the host layer driver provides interface functions.

Card section: Because these memory cards are block devices, of course, the need to provide a block device driver, which is the implementation of how your SD card is implemented as a block device. The entire SD card-driven call relationship can be represented by the following diagram:



Some of the basic concepts of SD card here is finished, the next section is mainly to start analyzing the code.

Three The following is an analysis of the high-pass android2.3 code in the SD card driver process.

In kernel, the SD card is added as a platform device to the kernel, in/KERNEL/ARCH/ARM/MACH-MSM/DEVICES-MSM7627A.C:

[CPP] view plain copy static void __init msm7x2x_init (void), static void __init msm7x27a_init_mmc (void) MSM_ADD_SDCC (1, &sdc1_plat_data);               DEVICES-MSM7627A.C, in which the sdc1_plat_data structure defines some of the platform-related resources, such as the test pin number, of course, we do not define the detection pin here.              -Pdev = msm_sdcc_devices[controller-1];              Pdev->dev.platform_data = plat;  Return Platform_device_register (Pdev); The platform device that mounts the SD card to the platform bus bus on the kernel

The static int __init mmc_init (void) function is called in the kernel/drivers/mmc/core/core.c file during kernel startup:

[CPP] view plain copy static int __init mmc_init (void), ret = Mmc_register_bus ();//Register MMC bus to System ret = Mmc_register_host_class ();//Add mmc_host to the system this class ret = Sdio_register_bus ();//register Sdio bus with the system, the SD card we use does not use this A sdio BUS, as for the reasons I still do not know, to continue to study.

Called in/KERNEL/DRIVERS/MMC/CARD/BLOCK.C:

[CPP] view plain copy static int __init mmc_blk_init (void)->res=register_blkdev (mmc_block_major, "MMC"); Application Block Device Number res = Mmc_register_driver (&mmc_driver); Registering the Mmc_driver in the system will probe the function static int mmc_blk_probe (struct mmc_card *card) to request a block device file after matching the mmc_device registered in MSM_SDCC.C , the final SD card all the driver is through this block device file to call, that is, this block device file is kernel and upper interface.


Call the static Int__init Msmsdcc_init (void) in the kernel/drivers/mmc/host/msm_sdcc.c file to register the struct platform_driver in the kernel msmsdcc_ Driver, this platform_driver, which matches the previously registered Platform_devices, calls the probe function:

[CPP]  View plain copy static int msmsdcc_probe (Struct platform_device *pdev)      -> struct mmc_platform_data *plat = pdev->dev.platform_data         for  (i = 0; i < pdev->num_resources;  i++)  //get information about the platform         mmc=mmc_alloc_host (sizeof (struct  Msmsdcc_host),  &pdev->dev),  //assigns mmc_host structure, and embeds the msmsdcc_host structure, so that it is convenient for the two structures to find each other. Mmc_host This structure is the core of the whole SD card driver in series, and it needs special attention. It is important to mmc_alloc_host this function at the same time and then analyze it later.         host = mmc_priv (MMC);         host->mmc = mmc;  //here is the conversion of each other in two structures                                                                <p>     /*  .......  */      Middle This code is mainly from the kernel space to the user space mapping, and set some clock </p><p>     mmc->ops  =&msmsdcc_ops; //Set the mmc_host operation function </p><p>     mmc->caps|= &NBSP;PLAT-&GT;MMC_BUS_WIDTH;&LT;/P&GT;&LT;P&GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RET&NBSP;=REQUEST_IRQ (core_ irqres->start, msmsdcc_irq, irqf_shared, driver_name  "(cmd)",  host),  //application Interrupted, We do not call </p><p>     mmc_add_host (MMC) because we are not using the interrupt pin, and//Add the Mmc_host device to the system, Analyze This function again later </p>  

Then we'll analyze the Mmc_alloc_host function we just mentioned: [CPP]

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.