SD card driver Theory

Source: Internet
Author: User

1. Introduction to SD/mmccard


 

1. What is mmcka?

 

MMC: MMC is short for multimediacard, that is, a multi-media card. It is a non-volatile memory device with a small size (24mm * 32mm * 1.4mm), large capacity, low power consumption, and fast transmission speed. It is widely used in consumer electronic products.

 

. What is an SD card

 

SD: the SD card is secure digital memory card, which is a secure digital card. Based on MMC, it has added two main features: the SD card emphasizes data security and allows you to set the storage
Use permissions to prevent data from being copied by others. Another feature is that the transmission speed is faster than that of the 2.11 mmccard. In terms of data transmission and physical specifications, the SD card (24mm * 32mm * 2.1mm, a little thicker than the mmccard) is forward compatible with the mmccard. All devices supporting the SD card also support the mmccard. The SD card is fully compatible with the 2.11 mmccard.

 

1. 3. What is sdio?

 

Sdio: sdio is a peripheral interface defined in the SD standard. An important difference between sdio and SD card specifications is the addition of low-speed standards. On the sdio card, only the SPI and one-bit SD transmission modes are required. The target application of low-speed cards supports low-speed IO with minimal hardware overhead.

 

1. 4. What is MCI?

 

MCI: MCI is short for the multimedia card interface, that is, the multi-media card interface. The interfaces defined in the above MMC, SD, and SDI cards all belong to the MCI interfaces. The term MCI is drivingProgramIs often used, many files, function names include "MCI ".

 

1.5.mmc/SD/sdio card differences

 

 

Ii. SD/MMC protocols and commands

 

1. SD/mmccard registers

 

The SD card contains 7 registers. here, the OCR, CID, CSD, and SCR registers Save the configuration information of the card. The RCA register stores the address temporarily allocated by the card during communication (only applicable to SD mode); card status) and the SD Status Register store the card status (for example, whether the write is successful, whether the CRC verification for communication is correct ), the content of these two registers is related to the communication mode (SD mode or SPI mode. the mmccard does not have the SCR and SD status registers. see table 1 below:

Table 1 seven registers in the SD card

 

1.1.ocr register

The OCR register stores the allowable power supply range of the SD/mmccard, as shown in table 2 below: if one of the OCR registers is 1, the card supports the corresponding voltage of this bit. The last digit indicates the status after the card is powered on (whether it is in the "busy" status). If this digit is 0, it indicates that it is busy. If it is 1, indicates that the instance is idle (MMC/SD protocol P60 ).

Table 2 OCR registers

 

1.2.cid register

CID is a 16-byte register that contains a unique card ID. See table 3 below:

Table 3 CID registers

 

1.3.csd register

The CSD register (the special data register of the card) contains the information required for access card storage. See table 4 below:

Table 4CSD register

 

1.4.scr register


The SCR register provides the special feature information of the SD card, which is 64-bit in size. The register is programmed by the vendor and cannot be programmed by the host. The mmccard does not have SCR. See table 5 below:

Table 5 SCR registers

 

1.5. RCA register

The 16-bit card address register stores the device addresses that are released during card recognition. This address is used by the host to communicate with the card after the card is identified. This register is valid only in SD bus mode.

 

2. SD card pins

 

 

3. SD card commands

 

3.1.sd card command format:

 

The SD card instruction consists of 6 bytes, as shown below:

Byte1: 0 1 x (command number, defined by the command mark "39" as 100111, that is, hexadecimal 0x27 ", the first byte of complete bytes 39 is 01100111, 0x27 + 0x40 ).

Byte2-5: Command arguments, command parameters, some commands do not have parameters.

Byte6: the first 7 bits are CRC (Cyclic redundacy check, cyclic redundancy check) Check bits, and the last bits are stopped bits 0.

 

3.2. SD card command

 

SD card commands are divided into 12 categories: class0 to class11.

 

3.2.1. class0: (card recognition, initialization, and other basic command sets)

Reset 0: reset the SD card.
Cmd1: Read OCR register.
Ipv9: Read the CSD register.
Limit 10: Read CID register.
Listen 12: Stop data transmission when reading multiple blocks.
Listen 13: Read the card_status register.

 

3.2.2.class2 (card reading command set ):

 

Limit 16: Set the block length.
Limit 17: read a single block.
Limit 18: read multiple data blocks until the host sends limit 12.

 

3.2.3.class4 (card writing command set ):

 

Limit 24: write a single block.
Limit 25: write multiple data blocks.
Protocol 27: Write the CSD register.

 

3.2.4.class5 (command set for card erasure ):

 

Limit 32: sets the start address of the erased block.
Limit 33: set the end address of the erased block.
38: erase the selected block.

 

3.2.5.class6 (write Protection Command set ):

 

28: Set the address of the write protection block.
29th: erase the address of the write protection block.
Limit 30: Ask the card for the status of the write protection bits

Class7: Card lock, unlock FUNCTION command set.

Class8: Apply for a specific command set.

Class10-11: Reserved.

 

3.3.sd card Workflow

First, let's take a look at how the operating system runs on the ARM processor.Read and Write the current SD card. The process can be divided into three major steps: initializing the SD card, writing the SD card, and reading the SD card.


3.3.1. Work Condition Detection

The Command Flow in the identification mode is shown in 3.1 (for the English version, see the standard SD card protocol p24)

Figure 3.1 Command Flow stuck in recognition Mode

1) before the host communicates with the SD card, the host does not know the working voltage range supported by the SD card, and the card does not know whether the current voltage provided by the host is supported. Therefore, the host first sends a reset command (Limit 0) using the default voltage ).

2) To verify the interface operation status of the SD card, the host sends send_if_cond (limit 8) to obtain the voltage range data that the SD card supports. The SD card checks the operating voltage used by the host by detecting the parameter section of limit 8. The host checks whether the SD card can work under the given voltage by analyzing the parameter section of limit 8 returned by the host, if the SD card can work at a specified voltage, it returns the cmd8 command response. If the given voltage is not supported, the SD card will not provide any response information and remain in the idle status.

 

3) before sending the acmd41 command to initialize a high-capacity SD card, you must force send the limit 8 command. Force the low-voltage host to send acmd41 before sending ipv8. If the dual-voltage SD card does not receive the iis8 command and is in high-voltage state, the low-voltage host cannot send the ipv8 command to the card, after receiving acmd41
Inactive status.

 

4) The sd_send_op_cond (acmd) command is designed to reject the card mechanism when the SD card host identifies the card or the voltage does not match. The number of commands sent by the host indicates the required voltage window size. If the SD card cannot transmit data within the given range, the next bus operation will be abandoned and no activity will be performed. The operation status register will also be defined.

 

5) after the host sends the reset command (reset 0), the host will first send the reset 8 command and then send the acmd41 command to reinitialize the SD card.

3.3.2 card initialization and Identification

 

When the bus is merged, the host starts card initialization and identification 3. The initialization process sets its operation status and starts with setting the HCs bit command sd_send_op_cond (acmd41) in OCR. If the HCs bit is set to 1, the host supports high-capacity SD cards. If HCs is set to 0, the host does not support high-capacity SD cards.
The process for card initialization and identification is shown in Figure 3.2.


Figure 3.2 card initialization and recognition process

3.3.3. Data Transmission Mode

 

When the recognition mode ends, the host clock FPP (data transmission clock frequency) will be saved as FOD (clock in card recognition mode), because some cards have limits on the Operation clock. The host must send send_csd (00009) to obtain the card type data memory, such as the block size and card capacity. Broadcast command set_dsr (Release 4) to configure the driver phases for all identification cards. It programmed the DSR memory to adapt to the application bus layout, the number of cards on the bus and the data transmission frequency.

Flowchart of SD card data transmission mode (English Version protocol P26) 3.3


Figure 3.3 flowchart of SD card data transmission mode

1) The latency 7 command is used to select an SD card and enable it to enter the transfer state. Only one card can be in the transfer state within the specified period of time. When a previously selected transfer SD card receives latency 7, the connection to the Controller is released and enters the stand-by state. When iis7 uses the reserved address 0x0000, all SD cards will enter the stand-by status.

 

2) All data read commands can be terminated at any time by the Stop command (Limit 12. Data transmission is terminated, and the SD card returns the transfer status. Read Commands include: block read operation (Block 17), multi-block read operation (Block 18), write protection (Block 30), SCR (acmd51), and Common commands in Read mode.
(Limit 56 ).

 

3) All data write commands can be terminated at any time by the Stop command (Limit 12. Writing a command will also stop before canceling the selection command (sequence 7. Write Commands include: block write operations (24, 25), programming commands (27), lock/unlock commands (42), and Common commands in write mode (56 ).

 

4) Once data transmission is completed, the SD card will exit the data write status and enter the programming status (Transfer successful) or transfer status (Transfer failed ).

 

Iv. Driver process of SD/MMC devices in Linux

 

4.1.the basic framework of the MMC Subsystem

 

4.1.1.mmc SubsystemCodeUnder kernel/driver/MMC, the MMC subsystem currently supports some forms of memory cards: SD, sdio, and MMC.

 

4.1.2.host: drivers for different hosts.

 

4.1.3.core: this is the core of the entire MMC.Core layer, which completes the implementation of different protocols and specifications, and provides interface functions for the driver at the host layer.

 

4.1.4.card: because these memory cards are Block devices, you must provide drivers for Block devices. This part is about how to implement the SD card as a block device.

 

4.1.5. Relationship between layers

4. 2. Important struct

4.2.1. struct mmc_host is used to describe the card controller BIT kernel/include/Linux/MMC/host. h.

 

4.2.2.struct mmc_card is used to describe that the card is located under kernel/include/Linux/MMC/card. h

 

4.2.3.struct mmc_driver is used to describe the mmccard driver under kernel/include/Linux/MMC/card. h.

 

4.2.4.struct mmc_host_ops is used to describe the Controller operation set. It is used to register an operation function from the host controller to the core layer to isolate the core layer from the specific host controller. That is to say, the core needs to operate the host controller, which is the function pointer operation given in this ops, and cannot directly call the function of a specific master controller.
Located under kernel/include/Linux/MMC/host. h.

 

2.5.struct mmc_ios describes the I/o status of the Controller to the card. Located under kernel/include/Linux/MMC/host. h.

 

4.2.6.struct mmc_request is used to describe the requests for reading and writing mmccards. It includes commands, data, and callback functions after the request is complete. Located in kernel/include/Linux/MMC/CORE. h.

 

4.2.7.struct mmc_queue is the structure of the MMC Request queue. It encapsulates the Common Request queue structure and adds the mmccard-related structure. Located in kernel/Drivers/MMC/card/queue. h.

 

4.2.8.struct mmc_data describes the data related to reading and writing on the mmccard, such as requests, operation commands, data, and status. Located in kernel/include/Linux/MMC/CORE. h.

 

4.2.9.struct mmc_command describes commands, data, and status information related to mmccard operations. Located in kernel/include/Linux/MMC/CORE. h.

 

4.3.host, core, and Card Association and handling process

 

4.3.1 shows the overall process.

 

 

4.3.2. The data. command processing process will be carefully analyzed in code analysis.

4.4: core tasks
The driver architecture of MMC/SD card consists of three files. In fact, there are two events:
1) card detection.
2). Read card data.

4.4.1. functions involved in card detection
tcc_mmc_probe (host/tcc_sdhc.c)
mmc_alloc_host (Core/core. c)
mmc_rescan (Core/core. c)
mmc_attach_mmc (Core/MMC. c)
mmc_init_card (Core/MMC. c)
mmc_add_card (Core/bus. c)
device_add
mmc_bus_match (Core/bus. c)
mmc_bus_probe (Core/bus. c) mmc_blk_probe (card/block. c)
alloc_disk/add_disk

4.4.2. functions involved in data reading and writing in the card
Mmc_blk_issue_rq (card/block. c)
Mmc_wait_for_req (Core/CORE. c)
Mmc_start_request (Core/CORE. c)
Host-> OPS-> requset (host, mrq)
// Tcc_mmc_request in tcc_sdhc.c

 

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.