Linux2.6.20 SD/mmccard drive learning diary 4 (based on S3C2440)

Source: Internet
Author: User
I found two articles about the SD/mmccard driver. I thought it was good. Thanks for the original author.
Article 1: From: http://blog.chinaunix.net/u2/69999/showart_734099.html

About the Linux 2.6 MMC/SD driver

The MMC/SD driver in Linux 2.6 is divided into the following parts:

1. sysfs-layer Bus Type processing: registers a group of MMC type processing functions, marked as "MMC"

Implemented in the mmc_sysfs.c File
 

2. MMC/SD fast device management: register a block device and a group of MMC bus driver sub-functions to implement queue management for Block devices.

Drivers/MMC/mmc_block.c

3. MMC/SD host management: Implements host management.

. Drivers/MMC/mmc. C: Mainly used in MMC command and protocol.

4. Implement a host driver instance for a specific MCU: register a host entity, interrupt processing function, Io setting function, request processing function, etc.

The above 1.2.3 basically does not need to be modified, and what needs to be processed is 4. the work to be done. when a card is inserted, the card in Step 4 interrupts the activation card initialization program and bus detection function. the MMC bus detection function calls the detection function of the block device, in the card device detection function, the request queue of the block device is initialized and a gendisk entity is registered (the file system will access the MMC block device through the gendisk entity in the future ), create a real MMC/SD device in sysfs. the block device communicates with the specific host through the IO setting function and request function registered by the specific host.

The following articles will analyze the specific parts
-----------------------------------------------------------------------------
Article 2:
From: http://blog.chinaunix.net/u1/42456/showart_516030.html

Recently, we have asked S3C2440 to support 4g sd cards on linux2.6.18.

Address: http://linux4u.wikidot.com/mmc-controller-driver

========================================================== ======================================

Differences between MMC driver for linux-2.6.2x and MMC driver for linux-2.6.1x
In the linux-2.6.2x, The block_device_operations structure used by the MMC driver has been redefined, see:
Linux-2.6.1x:

struct block_device_operations {    int (*open) (struct inode *, struct file *);    int (*release) (struct inode *, struct file *);    int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);    int (*media_changed) (struct gendisk *);    int (*revalidate_disk) (struct gendisk *);    struct module *owner;};

Linux-2.6.2x

struct block_device_operations {    int (*open) (struct inode *, struct file *);    int (*release) (struct inode *, struct file *);    int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);    long (*unlocked_ioctl) (struct file *, unsigned, unsigned long);    long (*compat_ioctl) (struct file *, unsigned, unsigned long);    int (*direct_access) (struct block_device *, sector_t, unsigned long *);    int (*media_changed) (struct gendisk *);    int (*revalidate_disk) (struct gendisk *);    int (*getgeo)(struct block_device *, struct hd_geometry *);    struct module *owner;};

Note that the new block driver interface structure adds the gntgeo member so that the caller can directly call this function to obtain the geometric structure of the device.

Workflow:
The main files of the MMC driver include:
Drivers/MMC/card/block. c
Drivers/MMC/card/queue. c
Drivers/MMC/CORE. c
Drivers/MMC/CORE/host. c
Drivers/MMC/CORE/
When the kernel is started, execute the mmc_init of core/CORE. C to register the MMC, SD bus, and a host class device. Run card/block. C to apply for a block device.

Data structure:
MMC bus operation related functions, because the mmccard supports a variety of total data lines, such as SPI, sdio, 8 linemmc, and different bus operation control methods are different, therefore, this structure is associated with the corresponding bus callback function.

// Bus operation structure struct mmc_bus_ops {void (* remove) (struct mmc_host *); void (* detect) (struct mmc_host *); int (* sysfs_add) (struct mmc_host *, struct mmc_card * card); void (* sysfs_remove) (struct mmc_host *, struct mmc_card * card); void (* suspend) (struct mmc_host *); void (* resume) (struct mmc_host *) ;}; // The bus operation Core/mmc of the mmccard. cstatic const struct mmc_bus_ops mmc_ops = {. remove = mmc_remove ,. detect = mmc_detect ,. sysfs_add = mmc_sysfs_add ,. sysfs_remove = mmc_sysfs_remove ,. suspend = mmc_suspend ,. resume = mmc_resume,}; // SD card bus operation Core/SD. cstatic const struct mmc_bus_ops mmc_sd_ops = {. remove = mmc_sd_remove ,. detect = mmc_sd_detect ,. sysfs_add = mmc_sd_sysfs_add ,. sysfs_remove = mmc_sd_sysfs_remove ,. suspend = mmc_sd_suspend ,. resume = mmc_sd_resume,}; // sdio bus operation Core/sdio. cstatic const struct mmc_bus_ops mmc_sdio_ops = {. remove = mmc_sdio_remove ,. detect = mmc_sdio_detect ,};

Functions related to bus operations:
. Detect, the driver often needs to call this function to check the status of the mmccard. The specific implementation is to send the listen 13 command and read the echo. If an error is returned, call it in sequence. remove and detach_bus to remove the card and release the bus.

Overall architecture:
When the kernel is started, mmc_init () and mmc_blk_init () are successively executed to initialize the MMC device and the MMC block module.
When the driver of the MMC device is mounted, run xx_mmc_probe () in the driver to check the SD device mounted on the host device. In this case, the probe function will create a host device, and then enable a delay task mmc_rescan ().
After the driver is mounted successfully, the mmc_rescan () function is executed and the card is initialized (the steps are described later ).
If a valid device is mounted to the bus, call the corresponding function to install the device to the system. mmc_attach_sdio (), mmc_attach_sd (), mmc_attach_mmc () these three functions are loaded with sdio devices, SD cards, and mmccards.
In the SD card, the drive sends acmd41 and serial 55 cyclically to the card, reads the OCR register, and sends cmd2 (read CID), cmd3 (obtain RCA), and serial 9 (read CSD) in sequence), 7 (select card ). The following commands are called acmd41 & ipv51. You can use IPv6 to switch to the high-speed mode.
After the above steps, it has been determined that the currently inserted card is a valid and identifiable memory card. Call mmc_add_card () to add the memory card to the system. Officially connected to the system driver.
After the card device is added to the system, the MMC block device driver is notified. The block device driver calls the probe function, that is, the mmc_blk_probe () function. mmc_blk_probe () first allocates a new mmc_blk_data structure variable, then calls mmc_init_queue to initialize the BLK queue. Then create a thread mmc_queue_thread ().
  

Mmc_rescan: The mmc_rescan () function is a delayed task started when the driver xx_mmc_probe () calls mmc_alloc_host () during loading. Xx_mmc_probe ()-> mmc_alloc_host ()-> init_delayed_work (& host-> detect, mmc_rescan );
When

Core
1. Obtain the bus
2. Check the bus operation structure pointer bus_ops. If it is null, scan the ports again using each bus. The detection sequence is sdio, normal SD, and MMC. After detecting the corresponding card type, use mmc_attach_bus () to connect the corresponding bus operation with the host.

void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops){    ...    host->bus_ops = ops;    ...}

3. the initialization process is as follows:
A. Send limit 0 to bring the card into the idle status
B. Send Route 8 and check whether the card is sd2.0. Sd1.1 does not support ipv8. Therefore, in sd2.0 spec, we propose to first send ipv8. If the response is invalid, sd1.1 is displayed. Otherwise, sd2.0 is used (see sd2.0 spec ).
C. Send limit 5 to read the OCR register.
D. Send acmd55 and ipv41 to enable the card to work. The mmccard does not support acmd55 or ike41. If this step passes, it indicates that this card is an SD card.
E. If Step D is incorrect, send cmd1 to determine whether the card is MMC. The SD card does not support cmd1, but the mmccard does. This is the basis for SD and MMC types.
F. If both acmd41 and cmd1 fail, the card may be invalid and initialization fails.

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.