SlideShare une entreprise Scribd logo
1  sur  31
SD/MMC DEVICE DRIVER 
1
目前SD卡的標準如下 
新舊版接腳相同,主要在支援的速度及指 
令不同。 
2 
SD card規範 
名稱版本最高容量 
SD 1.0 4G 
SDHC 2.0 32G 
SDXC 3.0 2T
SD Physical Specification 
Card 9 Pins define 
<1> CD/DAT3 
<2> CMD 
<3><6> Ground 
<4> VDD 
<5> CLK 
<7> DAT0 
<8> DAT1 
<9> DAT2
SD Physical Specification 
Card speed mode. 
<1> default:0~25Mhz, Input in clock rising edge, output in clock falling edge. 
<2> high speed:0~50Mhz, Input/output in clock rising edge. 
 Card bus width 
<1> 1-bit 
<2> 4-bit
SD Physical Specification 
卡片的暫存器 
OCR: Operation Condition about voltage and capacity. 
CID: Identification about vendor, product string, date, serial number. 
RCA: Relative card address, used for single/multiple card control. 
DSR: Driver stage, for bus driving performance. 
CSD: Card structure 1.0/2.0, TRAN_SPEED, command class, 
DSR_IMP, device size, block length… 
SCR: Card configuration ,SD Spec, Bus width, data after erase. 
SSR: SD status, current bus width, card type, speed_class.. 
CSR: card status, current state, error index, auxiliary state.
SD Physical Specification 
8 command class 
0: Basic commands 
2: Block-Oriented Read commands 
4: Block-Oriented Write commands 
5: Erase commands 
6: Block-Oriented Write Protection commands 
7: Lock Card commands 
8: Application-specific commands 
10: Switch function commands
SD Physical Specification 
 6 responses in two length(48/136) 
R1: Normal response 
R1b: Normal response with busy 
R2: CID,CSD register (136) 
R3: OCR register 
R6: Published RCA response 
R7: Card interface condition
SD Physical Specification 
Bus protocol 
<1> “no response” and “no data” Operations
SD Physical Specification 
Bus protocol 
<2> Block Read Operation
SD Physical Specification 
Bus protocol 
<3> Block Write Operation
SD Physical Specification 
Bus protocol 
<4> 4-bit wide bus
SD Speed Class/UHS Speed Class 
12
User Space 
Kernel Space 
Request Queue Request Queue 
Kernel Space 
Storage Media 
File I/O File I/O 
Virtual File System (VFS) Layer 
Individual Filesystems (EXT3,EXT4,JFFS2,Reiserfs,VFAT, …) 
Buffer Cache (Page Cache) 
I/O Schedulers 
Block Driver Block Driver 
Disk 
CD 
Driver
Block Diagram of Host Controller 
14
Kernel SD Device Driver Stack 
Kernel Virtual File Manager/File system 
MMC Block Device Driver 
SD Device Driver 
15 
DMA 
CPU Embedded SD controller 
interrupt 
interrupt
SD Host and the SD Card 
Request 
SD Command 
Reply 
16
17 
linux-2.6.x/drivers/mmc
18 
Source Code架構 
根目錄 
drivers/mmc 
子目錄card 
block file 
system 
interface 
子目錄core 
SD 標準協議程 
式碼 
子目錄host 
各別硬件控制程 
式碼
19 
SD Driver初始化流程 
使用platform device 
client driver的方式呼叫 
platform_driver_registe 
r()註冊一個platform 
probe call back 
function 
系統呼叫platform 
probe callback 
function,所以在該 
function中取得 
resource 並初始化該元 
件 
呼叫mmc_alloc_host()取 
得一個mmc結構的point, 
之後初始化該結構,接下 
來呼叫mmc_add_host()註 
冊一個mmc host driver 
在註冊一個mmc host 
driver時會填入幾個系統 
call back function,此時 
系統會使用這幾個 
function來scan是否有卡 
插入做SD卡的初始化
What do Probe call back function 
platform_get_resource()取得resource,並remap resource 
Allocate DMA resources 
若有用到work queue,則必需給予初始化 
註冊一個中斷服務,並起始中斷服務 
向上層MMC block註冊一個host MMC 
1) mmc = mmc_alloc_host(priv_size, dev) 
2) mmc_add_host(mmc) 
3) mmc_priv(mmc) 
* 在第一次註冊及後來插入SD卡時系統會掃描SD卡 
20
MMC block device driver call back function 
struct mmc_host_ops sd_ops = { 
.request = sdhci_request, 
.set_ios = sdhci_set_ios, 
.get_ro = sdhci_get_ro, 
}; // include/linux/mmc/host.h 
request 
The upper MMC block device driver will send a struct mmc_request *mrq 
to this. The mrq will include what want to do. When you finish this request 
you must call mmc_request_done() to note upper MMC block device 
driver what it is finisned. There include command or data access. 
set_ios 
1) Set/disable clock 
2) Power on/off to offer SD card or not 
3) Set SD card bus width 1 or 4 
get_ro 
1) To check the SD card write protect or not 
21
Callback function prototype 
struct mmc_host_ops { 
void (*request)(struct mmc_host *host, struct mmc_request *req); 
/* 
* Avoid calling these three functions too often or in a "fast path", 
* since underlaying controller might implement them in an expensive 
* and/or slow way. 
* 
* Also note that these functions might sleep, so don't call them 
* in the atomic contexts! 
* 
* Return values for the get_ro callback should be: 
* 0 for a read/write card 
* 1 for a read-only card 
* -ENOSYS when not supported (equal to NULL callback) 
* or a negative errno value when something bad happened 
* 
* Return values for the get_ro callback should be: 
* 0 for a absent card 
* 1 for a present card 
* -ENOSYS when not supported (equal to NULL callback) 
* or a negative errno value when something bad happened 
*/ 
void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios); 
int (*get_ro)(struct mmc_host *host); 
int (*get_cd)(struct mmc_host *host); 
void (*enable_sdio_irq)(struct mmc_host *host, int enable); 
};
Request callback function 
include/linux/mmc/core.h中定義 
struct mmc_request { 
struct mmc_command *cmd; 
struct mmc_data *data; 
struct mmc_command *stop; 
void *done_data; /* completion data */ 
void (*done)(struct mmc_request *);/* completion function */ 
}; 
由cmd中確認是否要送資料(檢查cmd->data 
是否為NULL pointer),或者只是送SD指令。 
處理完該cmd之後呼叫mmc_request_done() 
來回覆上層完成讓指令。 
23
struct mmc_command 
struct mmc_command { 
u32 opcode; 
u32 arg; 
u32 resp[4]; 
unsigned int flags; /* expected response type 
*/ 
unsigned int retries; /* max number of retries */ 
unsigned int error; /* command error */ 
struct mmc_data *data; /* data segment associated 
with cmd */ 
struct mmc_request *mrq; /* associated request */ 
}; // include/linux/mmc/core.h
struct mmc_data 
struct mmc_data { 
unsigned int timeout_ns; /* data timeout (in ns, max 80ms) */ 
unsigned int timeout_clks; /* data timeout (in clocks) */ 
unsigned int blksz; /* data block size */ 
unsigned int blocks; /* number of blocks */ 
unsigned int error; /* data error */ 
unsigned int flags; 
#define MMC_DATA_WRITE (1 << 8) 
#define MMC_DATA_READ (1 << 9) 
#define MMC_DATA_STREAM (1 << 10) 
unsigned int bytes_xfered; 
struct mmc_command *stop; /* stop command */ 
struct mmc_request *mrq; /* associated request */ 
unsigned int sg_len; /* size of scatter list */ 
struct scatterlist *sg; /* I/O scatter list */ 
}; // include/linux/mmc/core.h
scattlerlist操作 
struct scatterlist { 
#ifdef CONFIG_DEBUG_SG 
unsigned long sg_magic; 
#endif 
unsigned long page_link; 
unsigned int offset; /* buffer offset */ 
dma_addr_t dma_address; /* dma address */ 
unsigned int length; /* length */ 
}; 
#define sg_dma_address(sg) ((sg)->dma_address) 
#define sg_dma_len(sg) ((sg)->length) 
• int dma_map_sg(struct device *, struct scatterlist *, int, enum 
dma_data_direction) - map a set of SG buffers for streaming mode DMA 
• kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset; /*virtual address */ 
• kunmap_atomic(buffer, KM_BIO_SRC_IRQ) 
#define for_each_sg(sglist, sg, nr, __i)  
for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
SD card insert detecting flow chart 
27 
Card change interrupt 
happens 
Add card change work 
queue on interrupt 
service 
Insert or remove ? 
Remove all 
queue 
Set SD clock 
Call upper MMC block driver 
by mmc_detect_change() 
Send many command 
requests to this driver to get 
information from inserted 
card. 
remove insert 
Add a new disk to file 
manager.
Finish all data transfer ? 
28 
Read/Write data 
Received a request from 
upper block MMC driver to 
transfer data. The request 
callback function is called. 
1. Initial user space data 
context to kernel 
context. 
2. Set SD chipset for data 
length and timeout. 
Set DMA transfer 
information to DMA chipset 
for fist block data. 
Send start transfer data 
command 
Interrupt generates when a 
block data is transferred. 
Continue to DMA transfer 
information to DMA chipset 
for next block data. 
Send stop transfer data 
command 
Call upper MMC driver 
request done API 
Yes 
No
Classification of the Standard Register Map 
29
CODING EXAMPLE 
30
Register Structure Define 
typedef struct hw_register_struct { 
int rega; 
int regb; 
int regc; 
} hw_reg_t; 
static hw_reg_t *hw_reg; 
static int myfunc(void) 
{ 
…………………… 
readl(&hw_reg->rega); 
……………………………… 
writel(value, &hw_reg->regb); 
} 
static int __init module_init(void) 
{ 
hw_reg = (hw_reg_t *)ioremap(HW_PHYSICAL_ADDRESS, 0x100); 
……………….. 
} 
31

Contenu connexe

Tendances

Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionGene Chang
 
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Anne Nicolas
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New HardwareRuggedBoardGroup
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device driversHoucheng Lin
 
Q4.11: Introduction to eMMC
Q4.11: Introduction to eMMCQ4.11: Introduction to eMMC
Q4.11: Introduction to eMMCLinaro
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver艾鍗科技
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelDivye Kapoor
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Pankaj Suryawanshi
 

Tendances (20)

Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
eMMC 5.0 Total IP Solution
eMMC 5.0 Total IP SolutioneMMC 5.0 Total IP Solution
eMMC 5.0 Total IP Solution
 
Qemu Pcie
Qemu PcieQemu Pcie
Qemu Pcie
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
Q4.11: Introduction to eMMC
Q4.11: Introduction to eMMCQ4.11: Introduction to eMMC
Q4.11: Introduction to eMMC
 
U boot-boot-flow
U boot-boot-flowU boot-boot-flow
U boot-boot-flow
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 
Linux dma engine
Linux dma engineLinux dma engine
Linux dma engine
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
 

Similaire à Linux SD/MMC device driver

eMMC Embedded Multimedia Card overview
eMMC Embedded Multimedia Card overvieweMMC Embedded Multimedia Card overview
eMMC Embedded Multimedia Card overviewVijayGESYS
 
Comp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answersComp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answersProfessorLance
 
Comp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answersComp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answersProfessorLance
 
Android: Behind the scenes
Android: Behind the scenesAndroid: Behind the scenes
Android: Behind the scenesNarkozzz
 
Karasikov android behind the scenes
Karasikov   android behind the scenesKarasikov   android behind the scenes
Karasikov android behind the scenesDefconRussia
 
Embedded Recipes 2019 - Introduction to JTAG debugging
Embedded Recipes 2019 - Introduction to JTAG debuggingEmbedded Recipes 2019 - Introduction to JTAG debugging
Embedded Recipes 2019 - Introduction to JTAG debuggingAnne Nicolas
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver艾鍗科技
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3Raahul Raghavan
 
Design, Validation and Correlation of Characterized SODIMM Modules Supporting...
Design, Validation and Correlation of Characterized SODIMM Modules Supporting...Design, Validation and Correlation of Characterized SODIMM Modules Supporting...
Design, Validation and Correlation of Characterized SODIMM Modules Supporting...IOSR Journals
 
11thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp0111thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp01Karam Abuataya
 
11 Things About11g
11 Things About11g11 Things About11g
11 Things About11gfcamachob
 
Kernel Recipes 2015: Anatomy of an atomic KMS driver
Kernel Recipes 2015: Anatomy of an atomic KMS driverKernel Recipes 2015: Anatomy of an atomic KMS driver
Kernel Recipes 2015: Anatomy of an atomic KMS driverAnne Nicolas
 
Attack your Trusted Core
Attack your Trusted CoreAttack your Trusted Core
Attack your Trusted CoreDi Shen
 
建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card艾鍗科技
 
Roll your own toy unix clone os
Roll your own toy unix clone osRoll your own toy unix clone os
Roll your own toy unix clone oseramax
 

Similaire à Linux SD/MMC device driver (20)

eMMC Embedded Multimedia Card overview
eMMC Embedded Multimedia Card overvieweMMC Embedded Multimedia Card overview
eMMC Embedded Multimedia Card overview
 
Comp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answersComp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answers
 
Comp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answersComp 129 final exam 100% correct answers
Comp 129 final exam 100% correct answers
 
Android: Behind the scenes
Android: Behind the scenesAndroid: Behind the scenes
Android: Behind the scenes
 
Karasikov android behind the scenes
Karasikov   android behind the scenesKarasikov   android behind the scenes
Karasikov android behind the scenes
 
Embedded Recipes 2019 - Introduction to JTAG debugging
Embedded Recipes 2019 - Introduction to JTAG debuggingEmbedded Recipes 2019 - Introduction to JTAG debugging
Embedded Recipes 2019 - Introduction to JTAG debugging
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver
 
DDR DIMM Design
DDR DIMM DesignDDR DIMM Design
DDR DIMM Design
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
 
P sim.val
P sim.valP sim.val
P sim.val
 
Design, Validation and Correlation of Characterized SODIMM Modules Supporting...
Design, Validation and Correlation of Characterized SODIMM Modules Supporting...Design, Validation and Correlation of Characterized SODIMM Modules Supporting...
Design, Validation and Correlation of Characterized SODIMM Modules Supporting...
 
11thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp0111thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp01
 
11 Things About11g
11 Things About11g11 Things About11g
11 Things About11g
 
Kernel Recipes 2015: Anatomy of an atomic KMS driver
Kernel Recipes 2015: Anatomy of an atomic KMS driverKernel Recipes 2015: Anatomy of an atomic KMS driver
Kernel Recipes 2015: Anatomy of an atomic KMS driver
 
Attack your Trusted Core
Attack your Trusted CoreAttack your Trusted Core
Attack your Trusted Core
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card
 
Roll your own toy unix clone os
Roll your own toy unix clone osRoll your own toy unix clone os
Roll your own toy unix clone os
 
Understanding DPDK
Understanding DPDKUnderstanding DPDK
Understanding DPDK
 

Plus de 艾鍗科技

TinyML - 4 speech recognition
TinyML - 4 speech recognition TinyML - 4 speech recognition
TinyML - 4 speech recognition 艾鍗科技
 
Appendix 1 Goolge colab
Appendix 1 Goolge colabAppendix 1 Goolge colab
Appendix 1 Goolge colab艾鍗科技
 
Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用艾鍗科技
 
02 IoT implementation
02 IoT implementation02 IoT implementation
02 IoT implementation艾鍗科技
 
Tiny ML for spark Fun Edge
Tiny ML for spark Fun EdgeTiny ML for spark Fun Edge
Tiny ML for spark Fun Edge艾鍗科技
 
2. 機器學習簡介
2. 機器學習簡介2. 機器學習簡介
2. 機器學習簡介艾鍗科技
 
5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 艾鍗科技
 
心率血氧檢測與運動促進
心率血氧檢測與運動促進心率血氧檢測與運動促進
心率血氧檢測與運動促進艾鍗科技
 
利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆艾鍗科技
 
IoT感測器驅動程式 在樹莓派上實作
IoT感測器驅動程式在樹莓派上實作IoT感測器驅動程式在樹莓派上實作
IoT感測器驅動程式 在樹莓派上實作艾鍗科技
 
無線聲控遙控車
無線聲控遙控車無線聲控遙控車
無線聲控遙控車艾鍗科技
 
最佳光源的研究和實作
最佳光源的研究和實作最佳光源的研究和實作
最佳光源的研究和實作 艾鍗科技
 
無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車 艾鍗科技
 
Reinforcement Learning
Reinforcement LearningReinforcement Learning
Reinforcement Learning艾鍗科技
 
人臉辨識考勤系統
人臉辨識考勤系統人臉辨識考勤系統
人臉辨識考勤系統艾鍗科技
 
智慧家庭Smart Home
智慧家庭Smart Home智慧家庭Smart Home
智慧家庭Smart Home艾鍗科技
 

Plus de 艾鍗科技 (20)

TinyML - 4 speech recognition
TinyML - 4 speech recognition TinyML - 4 speech recognition
TinyML - 4 speech recognition
 
Appendix 1 Goolge colab
Appendix 1 Goolge colabAppendix 1 Goolge colab
Appendix 1 Goolge colab
 
Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用
 
02 IoT implementation
02 IoT implementation02 IoT implementation
02 IoT implementation
 
Tiny ML for spark Fun Edge
Tiny ML for spark Fun EdgeTiny ML for spark Fun Edge
Tiny ML for spark Fun Edge
 
Openvino ncs2
Openvino ncs2Openvino ncs2
Openvino ncs2
 
Step motor
Step motorStep motor
Step motor
 
2. 機器學習簡介
2. 機器學習簡介2. 機器學習簡介
2. 機器學習簡介
 
5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron)
 
3. data features
3. data features3. data features
3. data features
 
心率血氧檢測與運動促進
心率血氧檢測與運動促進心率血氧檢測與運動促進
心率血氧檢測與運動促進
 
利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆
 
IoT感測器驅動程式 在樹莓派上實作
IoT感測器驅動程式在樹莓派上實作IoT感測器驅動程式在樹莓派上實作
IoT感測器驅動程式 在樹莓派上實作
 
無線聲控遙控車
無線聲控遙控車無線聲控遙控車
無線聲控遙控車
 
最佳光源的研究和實作
最佳光源的研究和實作最佳光源的研究和實作
最佳光源的研究和實作
 
無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車
 
Reinforcement Learning
Reinforcement LearningReinforcement Learning
Reinforcement Learning
 
Linux Device Tree
Linux Device TreeLinux Device Tree
Linux Device Tree
 
人臉辨識考勤系統
人臉辨識考勤系統人臉辨識考勤系統
人臉辨識考勤系統
 
智慧家庭Smart Home
智慧家庭Smart Home智慧家庭Smart Home
智慧家庭Smart Home
 

Dernier

Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Dernier (20)

Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

Linux SD/MMC device driver

  • 2. 目前SD卡的標準如下 新舊版接腳相同,主要在支援的速度及指 令不同。 2 SD card規範 名稱版本最高容量 SD 1.0 4G SDHC 2.0 32G SDXC 3.0 2T
  • 3. SD Physical Specification Card 9 Pins define <1> CD/DAT3 <2> CMD <3><6> Ground <4> VDD <5> CLK <7> DAT0 <8> DAT1 <9> DAT2
  • 4. SD Physical Specification Card speed mode. <1> default:0~25Mhz, Input in clock rising edge, output in clock falling edge. <2> high speed:0~50Mhz, Input/output in clock rising edge.  Card bus width <1> 1-bit <2> 4-bit
  • 5. SD Physical Specification 卡片的暫存器 OCR: Operation Condition about voltage and capacity. CID: Identification about vendor, product string, date, serial number. RCA: Relative card address, used for single/multiple card control. DSR: Driver stage, for bus driving performance. CSD: Card structure 1.0/2.0, TRAN_SPEED, command class, DSR_IMP, device size, block length… SCR: Card configuration ,SD Spec, Bus width, data after erase. SSR: SD status, current bus width, card type, speed_class.. CSR: card status, current state, error index, auxiliary state.
  • 6. SD Physical Specification 8 command class 0: Basic commands 2: Block-Oriented Read commands 4: Block-Oriented Write commands 5: Erase commands 6: Block-Oriented Write Protection commands 7: Lock Card commands 8: Application-specific commands 10: Switch function commands
  • 7. SD Physical Specification  6 responses in two length(48/136) R1: Normal response R1b: Normal response with busy R2: CID,CSD register (136) R3: OCR register R6: Published RCA response R7: Card interface condition
  • 8. SD Physical Specification Bus protocol <1> “no response” and “no data” Operations
  • 9. SD Physical Specification Bus protocol <2> Block Read Operation
  • 10. SD Physical Specification Bus protocol <3> Block Write Operation
  • 11. SD Physical Specification Bus protocol <4> 4-bit wide bus
  • 12. SD Speed Class/UHS Speed Class 12
  • 13. User Space Kernel Space Request Queue Request Queue Kernel Space Storage Media File I/O File I/O Virtual File System (VFS) Layer Individual Filesystems (EXT3,EXT4,JFFS2,Reiserfs,VFAT, …) Buffer Cache (Page Cache) I/O Schedulers Block Driver Block Driver Disk CD Driver
  • 14. Block Diagram of Host Controller 14
  • 15. Kernel SD Device Driver Stack Kernel Virtual File Manager/File system MMC Block Device Driver SD Device Driver 15 DMA CPU Embedded SD controller interrupt interrupt
  • 16. SD Host and the SD Card Request SD Command Reply 16
  • 18. 18 Source Code架構 根目錄 drivers/mmc 子目錄card block file system interface 子目錄core SD 標準協議程 式碼 子目錄host 各別硬件控制程 式碼
  • 19. 19 SD Driver初始化流程 使用platform device client driver的方式呼叫 platform_driver_registe r()註冊一個platform probe call back function 系統呼叫platform probe callback function,所以在該 function中取得 resource 並初始化該元 件 呼叫mmc_alloc_host()取 得一個mmc結構的point, 之後初始化該結構,接下 來呼叫mmc_add_host()註 冊一個mmc host driver 在註冊一個mmc host driver時會填入幾個系統 call back function,此時 系統會使用這幾個 function來scan是否有卡 插入做SD卡的初始化
  • 20. What do Probe call back function platform_get_resource()取得resource,並remap resource Allocate DMA resources 若有用到work queue,則必需給予初始化 註冊一個中斷服務,並起始中斷服務 向上層MMC block註冊一個host MMC 1) mmc = mmc_alloc_host(priv_size, dev) 2) mmc_add_host(mmc) 3) mmc_priv(mmc) * 在第一次註冊及後來插入SD卡時系統會掃描SD卡 20
  • 21. MMC block device driver call back function struct mmc_host_ops sd_ops = { .request = sdhci_request, .set_ios = sdhci_set_ios, .get_ro = sdhci_get_ro, }; // include/linux/mmc/host.h request The upper MMC block device driver will send a struct mmc_request *mrq to this. The mrq will include what want to do. When you finish this request you must call mmc_request_done() to note upper MMC block device driver what it is finisned. There include command or data access. set_ios 1) Set/disable clock 2) Power on/off to offer SD card or not 3) Set SD card bus width 1 or 4 get_ro 1) To check the SD card write protect or not 21
  • 22. Callback function prototype struct mmc_host_ops { void (*request)(struct mmc_host *host, struct mmc_request *req); /* * Avoid calling these three functions too often or in a "fast path", * since underlaying controller might implement them in an expensive * and/or slow way. * * Also note that these functions might sleep, so don't call them * in the atomic contexts! * * Return values for the get_ro callback should be: * 0 for a read/write card * 1 for a read-only card * -ENOSYS when not supported (equal to NULL callback) * or a negative errno value when something bad happened * * Return values for the get_ro callback should be: * 0 for a absent card * 1 for a present card * -ENOSYS when not supported (equal to NULL callback) * or a negative errno value when something bad happened */ void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios); int (*get_ro)(struct mmc_host *host); int (*get_cd)(struct mmc_host *host); void (*enable_sdio_irq)(struct mmc_host *host, int enable); };
  • 23. Request callback function include/linux/mmc/core.h中定義 struct mmc_request { struct mmc_command *cmd; struct mmc_data *data; struct mmc_command *stop; void *done_data; /* completion data */ void (*done)(struct mmc_request *);/* completion function */ }; 由cmd中確認是否要送資料(檢查cmd->data 是否為NULL pointer),或者只是送SD指令。 處理完該cmd之後呼叫mmc_request_done() 來回覆上層完成讓指令。 23
  • 24. struct mmc_command struct mmc_command { u32 opcode; u32 arg; u32 resp[4]; unsigned int flags; /* expected response type */ unsigned int retries; /* max number of retries */ unsigned int error; /* command error */ struct mmc_data *data; /* data segment associated with cmd */ struct mmc_request *mrq; /* associated request */ }; // include/linux/mmc/core.h
  • 25. struct mmc_data struct mmc_data { unsigned int timeout_ns; /* data timeout (in ns, max 80ms) */ unsigned int timeout_clks; /* data timeout (in clocks) */ unsigned int blksz; /* data block size */ unsigned int blocks; /* number of blocks */ unsigned int error; /* data error */ unsigned int flags; #define MMC_DATA_WRITE (1 << 8) #define MMC_DATA_READ (1 << 9) #define MMC_DATA_STREAM (1 << 10) unsigned int bytes_xfered; struct mmc_command *stop; /* stop command */ struct mmc_request *mrq; /* associated request */ unsigned int sg_len; /* size of scatter list */ struct scatterlist *sg; /* I/O scatter list */ }; // include/linux/mmc/core.h
  • 26. scattlerlist操作 struct scatterlist { #ifdef CONFIG_DEBUG_SG unsigned long sg_magic; #endif unsigned long page_link; unsigned int offset; /* buffer offset */ dma_addr_t dma_address; /* dma address */ unsigned int length; /* length */ }; #define sg_dma_address(sg) ((sg)->dma_address) #define sg_dma_len(sg) ((sg)->length) • int dma_map_sg(struct device *, struct scatterlist *, int, enum dma_data_direction) - map a set of SG buffers for streaming mode DMA • kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset; /*virtual address */ • kunmap_atomic(buffer, KM_BIO_SRC_IRQ) #define for_each_sg(sglist, sg, nr, __i) for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
  • 27. SD card insert detecting flow chart 27 Card change interrupt happens Add card change work queue on interrupt service Insert or remove ? Remove all queue Set SD clock Call upper MMC block driver by mmc_detect_change() Send many command requests to this driver to get information from inserted card. remove insert Add a new disk to file manager.
  • 28. Finish all data transfer ? 28 Read/Write data Received a request from upper block MMC driver to transfer data. The request callback function is called. 1. Initial user space data context to kernel context. 2. Set SD chipset for data length and timeout. Set DMA transfer information to DMA chipset for fist block data. Send start transfer data command Interrupt generates when a block data is transferred. Continue to DMA transfer information to DMA chipset for next block data. Send stop transfer data command Call upper MMC driver request done API Yes No
  • 29. Classification of the Standard Register Map 29
  • 31. Register Structure Define typedef struct hw_register_struct { int rega; int regb; int regc; } hw_reg_t; static hw_reg_t *hw_reg; static int myfunc(void) { …………………… readl(&hw_reg->rega); ……………………………… writel(value, &hw_reg->regb); } static int __init module_init(void) { hw_reg = (hw_reg_t *)ioremap(HW_PHYSICAL_ADDRESS, 0x100); ……………….. } 31