Linux kernel SD card

This article introduces how to debug card drivers in three ways.Linux SD

1. SD card introduction

SD Card(Secure Digital Memory Card): The secure digital card is a new generation of high-speed storage device based on semiconductor flash memory.

SDThe card structure is as follows:

The following is a brief summary of the card's external pins, internal registers, speed grade and capacity grade.SD

1. External pin

SDThe card supports three transmission modes: mode, mode and mode.SPI1bit SD4bit SD

In the actual project, I mainly debug the latter two modes. The card has a total of 9 pins, which are summarized as follows:SD

Pin name Pin description Remark
CLK clock signal  
CMD command/reply pin  
DATE0 ~ 3 data cable 1bit SDMode: Use only , : card detection. Mode: :Card Detection/Data 3DATA0CD4bit SDCD/DATA3
VDD power supply  
VSS1/2 land  

2. Internal register

The SD card related registers are organized as follows:

Register name Register description
OCR(Operating Conditions Register) operating condition register
CID(Card IDentification Register) Card identification number register, each card has a unique identification number
CSD(Card Specific Data Register) Describe data register
SCR(SD Card Configuration Register) SD card configuration register
RCA(Relative Card Address) card address register
DSR(Driver Stage Register) driver level register

3. Speed ​​level

Depending on the data transfer speed, cards have different speed rating representations.SD

Protocol specifications Introduction
SD1.0 Used to indicate different speed levels, rarely used.X
SD2.0 Ordinary cards (Class2, Class4, Class6) and high-speed cards (Class10).
SD3.0 Uses UHS speed classes 1 and 3.
SD4.0 Using UHS-II

4. Capacity level

SDCard capacity currently supports: , and .SDSDHCSDXC

SD card type Protocol specifications Capacity Supported file formats
SD SD1.0 ~2GB FAT 12,16
SDHC(SD High Capacity) SD2.0 2GB ~ 32GB FAT 32
SDXC(SD eXtended Capacity) SD3.0 32GB~2TB exFAT

 

2. SD card debugging

1. Schematic diagram

To pre-set the driver, first look at the schematic diagram.

The picture below shows the connection method with the card slot .RK3568microSD

2. SDCard configuration

RK3568 SDCard configuration file:

1)arch/arm64/boot/dts/rockchip/rk3568.dtsi

    sdmmc0: dwmmc@fe2b0000 {
        compatible = "rockchip,rk3568-dw-mshc",
                 "rockchip,rk3288-dw-mshc";
        reg = <0x0 0xfe2b0000 0x0 0x4000>;
        interrupts = <GIC_SPI 98 IRQ_TYPE_LEVEL_HIGH>;
        max-frequency = <150000000>;
        clocks = <&cru HCLK_SDMMC0>, <&cru CLK_SDMMC0>,
             <&cru SCLK_SDMMC0_DRV>, <&cru SCLK_SDMMC0_SAMPLE>;
        clock-names = "biu", "ciu", "ciu-drive", "ciu-sample";
        fifo-depth = <0x100>;
        resets = <&cru SRST_SDMMC0>;
        reset-names = "reset";
        status = "disabled";
    };

2)arch/arm64/boot/dts/rockchip/rk3568-firefly-core.dtsi

    &sdmmc0 {
    max-frequency = <150000000>;
    supports-sd;
    bus-width = <4>;
    cap-mmc-highspeed;
    cap-sd-highspeed;
    disable-wp;
    sd-uhs-sdr104;
    vmmc-supply = <&vcc3v3_sd>;
    vqmmc-supply = <&vccio_sd>;
    pinctrl-names = "default";
    pinctrl-0 = <&sdmmc0_bus4 &sdmmc0_clk &sdmmc0_cmd &sdmmc0_det>;
    status = "okay";
};

in:

clocks: Indicates the card controller clock, and clock.SDdriversample

max-frequency: The maximum operating frequency of the card, adjusted according to different modes.SD

supports-sd: Indicates a card function and must be added. Otherwise the card cannot be initialized.SDSD

bus-width: The card uses 4-wire mode. If not configured, the default is 1-wire mode.SD

cap-mmc-highspeed/cap-sd-highspeed:Supported cards .highspeedSD

vmmc-supply、vqmmc-supply: Card power domain.SD

SD3.0Speed ​​mode:

sd-uhs-sdr12: clock frequency does not exceed 24M, signal voltage 1.8V 
sd-uhs-sdr25: clock frequency does not exceed 50M, signal voltage 1.8V 
sd-uhs-sdr50: clock frequency does not exceed 100M, signal voltage 1.8V 
sd- uhs-ddr50: clock frequency does not exceed 50M, uses double-edge sampling, signal voltage 1.8V 
sd-uhs-sdr104: clock frequency does not exceed 208M, signal voltage 1.8V

pinctrl-0: Card configuration.SDpinmux

3. SDCard driver

RK3568Driver file: , mainly focus on:drivers/mmc/host/dw_mmc-rockchip.c

static const struct dw_mci_drv_data rk3288_drv_data = { 
    .caps = dw_mci_rk3288_dwmmc_caps,            
    .num_caps = ARRAY_SIZE(dw_mci_rk3288_dwmmc_caps),        
    .set_ios = dw_mci_rk3288_set_ios, ## Configure clock, total Line, power supply, chip select, timing, etc.                     
    execute_tuning = dw_mci_rk3288_execute_tuning, ## Adjust driver clk and sample clk phase      
    .parse_dt = dw_mci_rk3288_parse_dt,        
    .init = dw_mci_rockchip_init,            
};

RK3568 SDController usage , mainly focus on: and .Synopsys IPdw_mci_rk3288_set_ios()dw_mci_rk3288_execute_tuning()

After the system starts, you can view the card properties through the following command (driver implementation: ):SDdrivers/mmc/core/debugfs.c

[root@xiaotianbsp:/]# cat /sys/kernel/debug/mmc1/ios
clock:          150000000 Hz
actual clock:   148500000 Hz
vdd:            21 (3.3 ~ 3.4 V)
bus mode:       2 (push-pull)
chip select:    0 (don't care)
power mode:     2 (on)
bus width:      2 (4 bits)
timing spec:    6 (sd uhs SDR104)
signal voltage: 1 (1.80 V)
driver type:    0 (driver type B)

 

3. SDCard test

The following test is completed based on the card.SanDisk Ultra 1 32G SD

1. SD card detection

After the system starts, the card startup log is as follows:SD

[    1.225398] dwmmc_rockchip fe2b0000.dwmmc: Successfully tuned phase to 266
[    1.225420] mmc1: new ultra high speed SDR104 SDHC card at address aaaa
[    1.226456] mmcblk1: mmc1:aaaa SD32G 29.7 GiB
[    1.227643]  mmcblk1: p1

If the card is not initialized normally, the following log will appear:SD

[  182.501273] mmc_host mmc1: Bus speed (slot 0) = 375000Hz (slot req 400000Hz, actual 375000HZ div = 0)
[  182.672282] mmc1: error -123 whilst initialising SD card
[  182.686489] mmc_host mmc1: Bus speed (slot 0) = 375000Hz (slot req 300000Hz, actual 187500HZ div = 1)
[  182.699318] mmc_host mmc1: Bus speed (slot 0) = 375000Hz (slot req 375000Hz, actual 375000HZ div = 0)
[  182.717513] mmc_host mmc1: Bus speed (slot 0) = 375000Hz (slot req 200000Hz, actual 187500HZ div = 1)

Reason: Failure. Error message definition ( ):mmc_sd_init_card()include/uapi/asm-generic/errno.h

#define ETIMEDOUT       110     /* Connection timed out */
...
#define ENOMEDIUM       123     /* No medium found */

2. SD card register

Through the following command, you can view the values ​​of card-related registers.SD

[root@xiaotianbsp:/]# cd /sys/class/mmc_host/mmc1/mmc1:aaaa 
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# ls 
block driver hwrev oemid scr type 
cid dsr manfid power serial uevent 
csd erase_size name preferred_erase_size ssr 
date fwrev ocr rca subsystem 
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat cid 
03534453443332478554c 
496d501636f ## Others The register viewing method is similar to

SDThe method to view other parameters of the card is as follows:

[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat erase_size
512
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat fwrev
0x5
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat hwrev
0x8
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat manfid
0x000003
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat name
SD32G
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat oemid
0x5344
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat serial
0x54c496d5
[root@xiaotianbsp:/sys/devices/platform/fe2b0000.dwmmc/mmc_host/mmc1/mmc1:aaaa]# cat type
SD

3. Mount/unmount

SDCard partition mounting command:

[root@xiaotianbsp:/]# mount -t vfat /dev/mmcblk1p1 /tmp/
[  244.746983] FAT-fs (mmcblk1p1): utf8 is not a recommended IO charset for FAT filesystems, filesystem will be case sensitive!
[  244.749331] FAT-fs (mmcblk1p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
[root@xiaotianbsp:/]# mount
...
/dev/mmcblk1p1 on /tmp type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=936,iocharset=utf8,shortname=mixed,errors=remount-ro)

SDCard partition uninstall command:

umount /dev/mmcblk1p1

4. Create partitions

Partitions can be recreated on the card using the command.fdiskSD

[root@xiaotianbsp:/]# fdisk /dev/mmcblk1 
... 
Command (m for help): p ## 1. View the existing partition 
Disk /dev/mmcblk1: 30 GB, 31914983424 bytes, 62333952 sectors 
3880 cylinders, 255 heads, 63 sectors/track 
Units: cylinders of 16065 * 512 = 8225280 bytesDevice 
Boot
 StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type 
/dev/mmcblk1p1 0,0,17 1023,254,63 16 62333951 62333936 29.7G c Win95 FAT32 ( LBA) 
​Command
 (m for help): d ## 2. Delete the partition 
Selected partition 1 
​Command
 (m for help): p 
Disk /dev/mmcblk1: 30 GB, 31914983424 bytes, 62333952 sectors 
3880 cylinders, 255 heads, 63 sectors/track 
Units: cylinders of 16065 * 512 = 8225280 bytesDevice 
Boot
 StartCHS EndCHS StartLBA EndLBA Sectors Size Id TypeCommand 
(
 m for help): n ## 3. Create a new partition, a total of 2 partitions are createdCommand 
action 
   e extended 
   p primary partition (1-4) 
p 
Partition number (1-4): 1 
First cylinder (1-3880, default 1): Using default value 1 
Last cylinder or +size or +sizeM or +sizeK (1-3880, default 3880): 1940 
​Command
 (m for help): p 
Disk /dev/mmcblk1: 30 GB, 31914983424 bytes, 62333952 sectors 
3880 cylinders, 255 heads, 63 sectors/track 
Units: cylinders of 16065 * 512 = 8225280 bytes 
​Device
 Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type 
/dev/mmcblk1p1 0,1,1 1023,254,63 63 31166099 31166037 14.8G 83 Linux 
​Command
 (m for help): n 
Command action 
   e extended 
   p primary partition (1-4 ) 
p 
Partition number (1-4): 2 
First cylinder (1941-3880, default 1941): Using default value 1941 
Last cylinder or +size or +sizeM or +sizeK (1941-3880, default 3880): Using default value 3880 
​Command
 (m for help): p 
Disk /dev/mmcblk1: 30 GB, 31914983424 bytes, 62333952 sectors 
3880 cylinders, 255 heads, 63 sectors/track 
Units: cylinders of 16065 * 512 = 8225280 bytes 
​Device
 Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type 
/dev/mmcblk1p1 0,1,1 1023,254,63 63 31166099 31166037 14.8G 83 Linux 
/dev/mmcblk1p2 1023,254,63 1023,254,63 31166100 62332199 3116 6100 14.8G 83 Linux 
​Command
 (m for help): w ## 4. Save the new partition

5. Reading and writing test

Test card writing command:SD

[root@RK356X:/]# time dd oflag=direct,nonblock if=/dev/zero of=/dev/mmcblk1p1 bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.4844 s, 30.1 MB/s
real    0m 3.49s
user    0m 0.00s
sys     0m 0.16s

Test card reading command:SD

[root@RK356X:/]# time dd iflag=direct,nonblock if=/dev/mmcblk1p1 of=/dev/null bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 1.65236 s, 63.5 MB/s
real    0m 1.65s
user    0m 0.00s
sys     0m 0.03s

The and attributes configured in the above command can avoid the file system and read and write directly without using them .iflagoflagcachebuffer cache

Note: Please indicate the author and source when reprinting.

RustDesk suspended domestic service Taobao (taobao.com) due to rampant fraud, restarted web version optimization work, Apple released M4 chip, high school students created their own open source programming language as a coming-of-age ceremony - Netizens commented: Relying on the defense, Yunfeng resigned from Alibaba, and plans to produce in the future Visual Studio Code 1.89, the destination for independent game programmers on the Windows platform, is officially announced by Huawei. Yu Chengdong’s job adjustment was nailed to the “FFmpeg Pillar of Shame” 15 years ago, but today he has to thank us - Tencent QQ Video avenges its previous shame? The open source mirror station of Huazhong University of Science and Technology is officially open to external network access
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4702401/blog/5566478