HPM SDK
HPMicro Software Development Kit
hpm_sdmmc_host.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_SDMMC_HOST_H
9 #define HPM_SDMMC_HOST_H
10 
11 #include "hpm_common.h"
12 #include "hpm_sdmmc_osal.h"
13 #include "hpm_sdxc_drv.h"
14 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define HPM_SDMMC_HOST_SUPPORT_4BIT (1UL << 0)
29 #define HPM_SDMMC_HOST_SUPPORT_8BIT (1UL << 1)
30 #define HPM_SDMMC_HOST_SUPPORT_3V3 (1UL << 2)
31 #define HPM_SDMMC_HOST_SUPPORT_1V8 (1UL << 3)
32 
33 #define HPM_SDMMC_HOST_SUPPORT_DDR (1UL << 4)
34 #define HPM_SDMMC_HOST_SUPPORT_SDR50 (1UL << 5)
35 #define HPM_SDMMC_HOST_SUPPORT_SDR104 (1UL << 6)
36 #define HPM_SDMMC_HOST_SUPPORT_HS200 (1UL << 7)
37 #define HPM_SDMMC_HOST_SUPPORT_HS400 (1Ul << 8)
38 
39 #define HPM_SDMMC_HOST_SUPPORT_CARD_DETECTION (1UL << 16)
40 #define HPM_SDMMC_HOST_SUPPORT_VOLTAGE_SWITCH (1UL << 17)
41 #define HPM_SDMMC_HOST_SUPPORT_POWER_SWITCH (1UL << 18)
42 #define HPM_SDMMC_HOST_SUPPORT_WRITE_PROTECTION (1UL << 19)
43 #define HPM_SDMMC_HOST_SUPPORT_RESET_PIN (1UL << 20)
44 #define HPM_SDMMC_HOST_SUPPORT_DATA_STROBE (1UL << 21)
45 
46 #define HPM_SDMMC_HOST_CD_IN_IP (HPM_SDMMC_HOST_SUPPORT_CARD_DETECTION << 8)
47 #define HPM_SDMMC_HOST_VSEL_IN_IP (HPM_SDMMC_HOST_SUPPORT_VOLTAGE_SWITCH << 8)
48 #define HPM_SDMMC_HOST_PWR_IN_IP (HPM_SDMMC_HOST_SUPPORT_POWER_SWITCH << 8)
49 #define HPM_SDMMC_HOST_WP_IN_IP (HPM_SDMMC_HOST_SUPPORT_WRITE_PROTECTION << 8)
50 #define HPM_SDMMC_HOST_RST_IN_IP (HPM_SDMMC_HOST_SUPPORT_RESET_PIN << 8)
51 
52 #define HPM_SDMMC_HOST_ADMA3_ALIGN_SIZE (8U)
53 
54 
58 typedef enum {
59  sdmmc_bus_width_1bit = 0, /* Bus width: 1-bit */
60  sdmmc_bus_width_4bit = 1, /* Bus width: 4-bit */
61  sdmmc_bus_width_8bit = 2, /* Bus width: 8-bit */
63 
67 typedef enum {
68  sdmmc_dev_type_emmc = 0, /* Device type is eMMC */
69  sdmmc_dev_type_sd = 1, /* Device type is SD */
70  sdmmc_dev_type_sdio = 2, /* Device type is SDIO */
72 
76 typedef enum {
77  sdmmc_host_card_detection_none = 0, /* Card detection is not enabled */
78  sdmmc_host_card_detection_via_gpio = 1, /* Card detection is via GPIO */
79  sdmmc_host_card_detection_via_sdxc = 2, /* Card detection is via SDXC CDN pin */
81 
85 typedef enum {
89 
93 typedef enum {
99 
103 typedef enum {
104  /* SD Card Speed modes */
112 
113  /* eMMC Card Speed modes */
120 
124 typedef struct {
125  bool use_gpio;
126  uint8_t polarity;
127  uint16_t gpio_pin;
129 
130 typedef enum {
135 
139 typedef struct {
146 
147 #ifndef HPM_SDMMC_HOST_ADMA_TBL_SIZE
148 #if defined(HPM_SDMMC_USE_ADMA2) && (HPM_SDMMC_USE_ADMA2 == 1)
149 #define HPM_SDMMC_HOST_ADMA_TBL_SIZE (SDXC_ADMA2_DESC_WORDS * 2UL)
150 #else
151 #define HPM_SDMMC_HOST_ADMA_TBL_SIZE (SDXC_AMDA3_DESC_MIN_WORDS * 2UL)
152 #endif
153 #endif
154 
161 
162 typedef uint32_t (*sdmmchost_clock_init_func_t)(SDMMCHOST_Type *base, uint32_t clk_freq, bool need_reverse);
163 
164 typedef void (*sdmmchost_power_switch_func_t)(SDMMCHOST_Type *base, bool on_off);
165 
167 
169 
170 typedef void (*sdmmchost_cmd_line_init_func_t)(SDMMCHOST_Type *base, bool push_pull);
171 
173 
174 typedef struct {
175 
176  void (*cmd_io_init)(SDMMCHOST_Type *base, bool open_drain, bool is_1v8);
177 
178  void (*clk_data_io_init)(SDMMCHOST_Type *base, uint32_t data_width, bool is_1v8);
179 
180  void (*pwr_io_init)(SDMMCHOST_Type *base, bool as_gpio);
181 
182  void (*cd_io_init)(SDMMCHOST_Type *base, bool as_gpio);
183 
184  void (*vsel_io_init)(SDMMCHOST_Type *base, bool as_gpio);
185 
186  void (*ds_io_init)(SDMMCHOST_Type *base);
188 
189 
190 typedef struct {
191  uint8_t hart_id;
192  uint8_t instance_num;
193  uint32_t host_flags;
198 
199  void (*delay_ms)(uint32_t ms);
201 
202 
203 typedef struct {
204  sdmmc_host_param_t host_param; /* Host Parameters */
205  sdmmc_dev_type_t dev_type; /* Device Type */
207  sdmmc_buswidth_t bus_width; /* Bus width */
208  hpm_sdmmc_io_volt_t io_voltage; /* IO voltage */
209  uint32_t clock_freq; /* Clock Frequency */
210  /* Host Transfer Fields */
211  sdmmchost_xfer_t xfer; /* xfer context */
212  sdmmchost_cmd_t cmd; /* Command Context */
213  sdmmchost_data_t data; /* Data Context */
214  uint32_t adma_table[HPM_SDMMC_HOST_ADMA_TBL_SIZE + 1]; /* ADMA table buffer, allocate one extra word in case that the adma_table is not 8-byte aligned */
215  uint32_t buffer[128]; /* Host buffer */
216 
217  /* Host run-time fields */
220  void (*sdio_irq_handler)(void *param);
222 #if defined(HPM_SDMMC_HOST_ENABLE_IRQ) && (HPM_SDMMC_HOST_ENABLE_IRQ == 1)
223  hpm_sdmmc_osal_event_t xfer_done_or_error_event;
224 #endif
225 
226 } sdmmc_host_t;
227 
235 
242 
251 uint32_t sdmmchost_set_card_clock(sdmmc_host_t *host, uint32_t freq, bool clock_inverse);
252 
257 void sdmmchost_deinit(sdmmc_host_t *host);
258 
263 void sdmmchost_reset(const sdmmc_host_t *host);
264 
269 void sdmmchost_wait_card_active(const sdmmc_host_t *host);
270 
279 
288 
295 bool sdmmchost_is_card_detected(const sdmmc_host_t *host);
296 
302 void sdmmchost_init_io(sdmmc_host_t *host, hpm_sdmmc_operation_mode_t operation_mode);
303 
309 void sdmmchost_delay_ms(const sdmmc_host_t *host, uint32_t ms);
310 
318 
325 
332 void sdmmchost_enable_emmc_support(const sdmmc_host_t *host, bool enable);
333 
342 
351 
359 
365 void sdmmchost_enable_enhanced_data_strobe(const sdmmc_host_t *host, bool enable);
366 
372 
379 
385 
391 
398 
411 void sdmmchost_register_sdio_callback(sdmmc_host_t *host, void (*sdio_irq_callback)(void *param), void *param);
412 
423 void sdmmchost_enable_sdio_interrupt(sdmmc_host_t *host, bool enable);
424 
432 
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
442 #endif /* HPM_SDMMC_HOST_H */
uint32_t hpm_stat_t
Definition: hpm_common.h:126
hpm_stat_t sdmmchost_send_command(sdmmc_host_t *host, const sdmmchost_cmd_t *cmd)
Send command via the host.
Definition: hpm_sdmmc_host.c:318
sdxc_capabilities_t sdmmchost_capabilities_t
Definition: hpm_sdmmc_host.h:160
void sdmmchost_enable_emmc_support(const sdmmc_host_t *host, bool enable)
Enable the eMMC support on the host.
Definition: hpm_sdmmc_host.c:187
void sdmmchost_delay_ms(const sdmmc_host_t *host, uint32_t ms)
Host delay.
Definition: hpm_sdmmc_host.c:34
void(* sdmmchost_switch_1v8_io_func_t)(SDMMCHOST_Type *base)
Definition: hpm_sdmmc_host.h:168
sdmmc_card_detection_mode_t
SD/MMC Host Card Detection Modes.
Definition: hpm_sdmmc_host.h:76
void sdmmchost_register_sdio_callback(sdmmc_host_t *host, void(*sdio_irq_callback)(void *param), void *param)
Registers an SDIO interrupt callback function for the SDMMC host controller.
Definition: hpm_sdmmc_host.c:676
bool(* sdmmchost_card_detect_func_t)(SDMMCHOST_Type *base)
Definition: hpm_sdmmc_host.h:172
void sdmmchost_set_rxclk_delay_chain(sdmmc_host_t *host)
Set the Card Rx Clock delay chain for the host.
void sdmmchost_set_card_bus_width(sdmmc_host_t *host, sdmmc_buswidth_t bus_width)
Set the card bus width.
Definition: hpm_sdmmc_host.c:210
sdxc_xfer_t sdmmchost_xfer_t
Definition: hpm_sdmmc_host.h:155
sdmmc_dev_type_t
SD/MMC Device Type definitions.
Definition: hpm_sdmmc_host.h:67
uint32_t sdmmchost_set_card_clock(sdmmc_host_t *host, uint32_t freq, bool clock_inverse)
Set the Card clock.
Definition: hpm_sdmmc_host.c:217
void sdmmchost_select_voltage(sdmmc_host_t *host, hpm_sdmmc_io_volt_t io_volt)
Select the IO voltage.
Definition: hpm_sdmmc_host.c:627
sdxc_adma2_descriptor_t sdmmc_adma2_desc_t
Definition: hpm_sdmmc_host.h:158
hpm_sdmmc_io_volt_t
SDMMC IO Voltage.
Definition: hpm_sdmmc_host.h:85
hpm_stat_t sdmmchost_init(sdmmc_host_t *host)
SDMMC Host Initialization.
Definition: hpm_sdmmc_host.c:47
void sdmmchost_wait_card_active(const sdmmc_host_t *host)
Wait until the card is active.
Definition: hpm_sdmmc_host.c:224
uint32_t(* sdmmchost_clock_init_func_t)(SDMMCHOST_Type *base, uint32_t clk_freq, bool need_reverse)
Definition: hpm_sdmmc_host.h:162
hpm_stat_t sdmmchost_error_recovery(sdmmc_host_t *host, sdmmchost_cmd_t *cmd)
Trigger the Error recovery via the Host.
Definition: hpm_sdmmc_host.c:443
void(* sdmmchost_io_init_func_t)(SDMMCHOST_Type *base)
Definition: hpm_sdmmc_host.h:166
hpm_sdmmc_power_option_t
Definition: hpm_sdmmc_host.h:130
void sdmmchost_vsel_pin_control(const sdmmc_host_t *host, hpm_sdmmc_io_volt_t io_volt)
Control the Voltage selection pin.
Definition: hpm_sdmmc_host.c:91
uint32_t sdmmchost_get_data_pin_level(sdmmc_host_t *host)
Return the data pin level.
Definition: hpm_sdmmc_host.c:684
void sdmmchost_deinit(sdmmc_host_t *host)
Deinitialize the host.
Definition: hpm_sdmmc_host.c:164
void sdmmchost_reset(const sdmmc_host_t *host)
Reset the host.
Definition: hpm_sdmmc_host.c:180
hpm_stat_t sdmmchost_set_speed_mode(const sdmmc_host_t *host, sdmmc_speed_mode_t speed_mode)
Set the speed mode via the Host.
Definition: hpm_sdmmc_host.c:433
hpm_stat_t sdmmchost_transfer(sdmmc_host_t *host, const sdmmchost_xfer_t *content)
Transfer data via the host.
Definition: hpm_sdmmc_host.c:359
bool sdmmchost_is_voltage_switch_supported(const sdmmc_host_t *host)
Check whether the host support voltage switch.
Definition: hpm_sdmmc_host.c:465
#define HPM_SDMMC_HOST_ADMA_TBL_SIZE
Definition: hpm_sdmmc_host.h:151
volatile uint32_t * hpm_sdmmc_osal_event_t
Definition: hpm_sdmmc_osal.h:28
void sdmmchost_init_io(sdmmc_host_t *host, hpm_sdmmc_operation_mode_t operation_mode)
Initialize the Host IO according to the operation mode.
Definition: hpm_sdmmc_host.c:512
sdxc_command_t sdmmchost_cmd_t
Definition: hpm_sdmmc_host.h:156
hpm_stat_t sdmmchost_switch_to_1v8(sdmmc_host_t *host)
Switch the Host to 1.8V IO voltage.
Definition: hpm_sdmmc_host.c:116
bool sdmmchost_is_card_detected(const sdmmc_host_t *host)
Check whether the card is detected or not.
Definition: hpm_sdmmc_host.c:192
void sdmmchost_set_cardclk_delay_chain(const sdmmc_host_t *host)
Set the Card clock delay chain for the host.
Definition: hpm_sdmmc_host.c:448
void sdmmchost_irq_handler(sdmmc_host_t *host)
THe Host IRQ Handler.
Definition: hpm_sdmmc_host.c:639
sdxc_data_t sdmmchost_data_t
Definition: hpm_sdmmc_host.h:157
void sdmmchost_set_data_strobe_delay(const sdmmc_host_t *host)
Set the Data strobe delay for Host.
Definition: hpm_sdmmc_host.c:621
void(* sdmmchost_cmd_line_init_func_t)(SDMMCHOST_Type *base, bool push_pull)
Definition: hpm_sdmmc_host.h:170
void sdmmchost_enable_sdio_interrupt(sdmmc_host_t *host, bool enable)
Enable or disable SDIO interrupt on the SDMMC host controller.
Definition: hpm_sdmmc_host.c:664
sdmmc_speed_mode_t
SD/MMC Speed definitions.
Definition: hpm_sdmmc_host.h:103
hpm_sdmmc_operation_mode_t
SDMMC Operation mode.
Definition: hpm_sdmmc_host.h:93
void sdmmchost_enable_enhanced_data_strobe(const sdmmc_host_t *host, bool enable)
Enable the enhanced data strboe.
Definition: hpm_sdmmc_host.c:615
SDXC_Type SDMMCHOST_Type
Definition: hpm_sdmmc_host.h:159
sdmmc_buswidth_t
SD/MMC Bus Width definitions.
Definition: hpm_sdmmc_host.h:58
void(* sdmmchost_power_switch_func_t)(SDMMCHOST_Type *base, bool on_off)
Definition: hpm_sdmmc_host.h:164
@ sdmmc_host_card_detection_via_sdxc
Definition: hpm_sdmmc_host.h:79
@ sdmmc_host_card_detection_via_gpio
Definition: hpm_sdmmc_host.h:78
@ sdmmc_host_card_detection_none
Definition: hpm_sdmmc_host.h:77
@ sdmmc_dev_type_sdio
Definition: hpm_sdmmc_host.h:70
@ sdmmc_dev_type_sd
Definition: hpm_sdmmc_host.h:69
@ sdmmc_dev_type_emmc
Definition: hpm_sdmmc_host.h:68
@ hpm_sdmmc_io_voltage_1v8
Definition: hpm_sdmmc_host.h:87
@ hpm_sdmmc_io_voltage_3v3
Definition: hpm_sdmmc_host.h:86
@ hpm_sdmmc_power_off
Definition: hpm_sdmmc_host.h:131
@ hpm_sdmmc_power_on
Definition: hpm_sdmmc_host.h:133
@ hpm_sdmmc_power_up
Definition: hpm_sdmmc_host.h:132
@ sdmmc_sd_speed_ddr50
Definition: hpm_sdmmc_host.h:111
@ sdmmc_sd_speed_normal
Definition: hpm_sdmmc_host.h:105
@ sdmmc_emmc_speed_high_speed_ddr
Definition: hpm_sdmmc_host.h:117
@ sdmmc_emmc_speed_hs200
Definition: hpm_sdmmc_host.h:116
@ sdmmc_emmc_speed_hs400
Definition: hpm_sdmmc_host.h:118
@ sdmmc_sd_speed_sdr50
Definition: hpm_sdmmc_host.h:109
@ sdmmc_emmc_speed_high_speed_sdr
Definition: hpm_sdmmc_host.h:115
@ sdmmc_sd_speed_sdr104
Definition: hpm_sdmmc_host.h:110
@ sdmmc_sd_speed_sdr25
Definition: hpm_sdmmc_host.h:108
@ sdmmc_emmc_speed_legacy
Definition: hpm_sdmmc_host.h:114
@ sdmmc_sd_speed_sdr12
Definition: hpm_sdmmc_host.h:107
@ sdmmc_sd_speed_high
Definition: hpm_sdmmc_host.h:106
@ hpm_sdmmc_operation_mode_inactive
Definition: hpm_sdmmc_host.h:94
@ hpm_sdmmc_operation_mode_identification
Definition: hpm_sdmmc_host.h:95
@ hpm_sdmmc_operation_mode_transfer
Definition: hpm_sdmmc_host.h:96
@ hpm_sdmmc_operation_mode_interrupt
Definition: hpm_sdmmc_host.h:97
@ sdmmc_bus_width_8bit
Definition: hpm_sdmmc_host.h:61
@ sdmmc_bus_width_1bit
Definition: hpm_sdmmc_host.h:59
@ sdmmc_bus_width_4bit
Definition: hpm_sdmmc_host.h:60
struct _sdxc_command sdxc_command_t
SDXC command structure.
struct _sdxc_data sdxc_data_t
SDXC data structure.
struct _sdxc_xfer sdxc_xfer_t
SDXC transfer context.
struct _sdxc_adma2_descriptor sdxc_adma2_descriptor_t
SDXC ADMA2 descriptor.
Definition: hpm_sdxc_regs.h:12
SDMMC extra Pin info.
Definition: hpm_sdmmc_host.h:139
hpm_sdmmc_pin_info_t rst_pin
Definition: hpm_sdmmc_host.h:143
hpm_sdmmc_pin_info_t vsel_pin
Definition: hpm_sdmmc_host.h:141
hpm_sdmmc_pin_info_t wp_pin
Definition: hpm_sdmmc_host.h:144
hpm_sdmmc_pin_info_t pwr_pin
Definition: hpm_sdmmc_host.h:142
hpm_sdmmc_pin_info_t cd_pin
Definition: hpm_sdmmc_host.h:140
SDMMC Pin info structure.
Definition: hpm_sdmmc_host.h:124
uint8_t polarity
Definition: hpm_sdmmc_host.h:126
uint16_t gpio_pin
Definition: hpm_sdmmc_host.h:127
bool use_gpio
Definition: hpm_sdmmc_host.h:125
Definition: hpm_sdmmc_host.h:190
SDMMCHOST_Type * base
Definition: hpm_sdmmc_host.h:194
uint32_t host_flags
Definition: hpm_sdmmc_host.h:193
uint8_t instance_num
Definition: hpm_sdmmc_host.h:192
sdmmchost_clock_init_func_t clock_init_func
Definition: hpm_sdmmc_host.h:196
uint8_t hart_id
Definition: hpm_sdmmc_host.h:191
hpm_sdmmc_extra_io_data_t io_data
Definition: hpm_sdmmc_host.h:195
sdmmc_io_init_apis_t io_init_apis
Definition: hpm_sdmmc_host.h:197
Definition: hpm_sdmmc_host.h:203
uint32_t clock_freq
Definition: hpm_sdmmc_host.h:209
bool card_init_done
Definition: hpm_sdmmc_host.h:219
sdmmc_buswidth_t bus_width
Definition: hpm_sdmmc_host.h:207
bool card_inserted
Definition: hpm_sdmmc_host.h:218
hpm_sdmmc_io_volt_t io_voltage
Definition: hpm_sdmmc_host.h:208
hpm_sdmmc_operation_mode_t operation_mode
Definition: hpm_sdmmc_host.h:206
void * sdio_irq_param
Definition: hpm_sdmmc_host.h:221
sdmmchost_cmd_t cmd
Definition: hpm_sdmmc_host.h:212
sdmmchost_data_t data
Definition: hpm_sdmmc_host.h:213
sdmmc_host_param_t host_param
Definition: hpm_sdmmc_host.h:204
sdmmchost_xfer_t xfer
Definition: hpm_sdmmc_host.h:211
sdmmc_dev_type_t dev_type
Definition: hpm_sdmmc_host.h:205
Definition: hpm_sdmmc_host.h:174
SDXC Capacities.
Definition: hpm_sdxc_drv.h:298