HPM SDK
HPMicro Software Development Kit
hpm_sdmmc_host.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023 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_sdxc_drv.h"
13 #include "hpm_gpio_drv.h"
14 
15 #define HPM_SDMMC_HOST_SUPPORT_4BIT (1UL << 0)
16 #define HPM_SDMMC_HOST_SUPPORT_8BIT (1UL << 1)
17 #define HPM_SDMMC_HOST_SUPPORT_3V3 (1UL << 2)
18 #define HPM_SDMMC_HOST_SUPPORT_1V8 (1UL << 3)
19 
20 #define HPM_SDMMC_HOST_SUPPORT_DDR (1UL << 4)
21 #define HPM_SDMMC_HOST_SUPPORT_SDR50 (1UL << 5)
22 #define HPM_SDMMC_HOST_SUPPORT_SDR104 (1UL << 6)
23 #define HPM_SDMMC_HOST_SUPPORT_HS200 (1UL << 7)
24 #define HPM_SDMMC_HOST_SUPPORT_HS400 (1Ul << 8)
25 
26 #define HPM_SDMMC_HOST_SUPPORT_CARD_DETECTION (1UL << 16)
27 #define HPM_SDMMC_HOST_SUPPORT_VOLTAGE_SWITCH (1UL << 17)
28 #define HPM_SDMMC_HOST_SUPPORT_POWER_SWITCH (1UL << 18)
29 #define HPM_SDMMC_HOST_SUPPORT_WRITE_PROTECTION (1UL << 19)
30 #define HPM_SDMMC_HOST_SUPPORT_RESET_PIN (1UL << 20)
31 #define HPM_SDMMC_HOST_SUPPORT_DATA_STROBE (1UL << 21)
32 
33 #define HPM_SDMMC_HOST_CD_IN_IP (HPM_SDMMC_HOST_SUPPORT_CARD_DETECTION << 8)
34 #define HPM_SDMMC_HOST_VSEL_IN_IP (HPM_SDMMC_HOST_SUPPORT_VOLTAGE_SWITCH << 8)
35 #define HPM_SDMMC_HOST_PWR_IN_IP (HPM_SDMMC_HOST_SUPPORT_POWER_SWITCH << 8)
36 #define HPM_SDMMC_HOST_WP_IN_IP (HPM_SDMMC_HOST_SUPPORT_WRITE_PROTECTION << 8)
37 #define HPM_SDMMC_HOST_RST_IN_IP (HPM_SDMMC_HOST_SUPPORT_RESET_PIN << 8)
38 
39 
43 typedef enum {
44  sdmmc_bus_width_1bit = 0, /* Bus width: 1-bit */
45  sdmmc_bus_width_4bit = 1, /* Bus width: 4-bit */
46  sdmmc_bus_width_8bit = 2, /* Bus width: 8-bit */
48 
52 typedef enum {
53  sdmmc_dev_type_emmc = 0, /* Device type is eMMC */
54  sdmmc_dev_type_sd = 1, /* Device type is SD */
55  sdmmc_dev_type_sdio = 2, /* Device type is SDIO */
57 
61 typedef enum {
62  sdmmc_host_card_detection_none = 0, /* Card detection is not enabled */
63  sdmmc_host_card_detection_via_gpio = 1, /* Card detection is via GPIO */
64  sdmmc_host_card_detection_via_sdxc = 2, /* Card deteciton is via SDXC CDN pin */
66 
70 typedef enum {
74 
78 typedef enum {
84 
88 typedef enum {
89  /* SD Card Speed modes */
97 
98  /* eMMC Card Speed modes */
105 
109 typedef struct {
110  bool use_gpio;
111  uint8_t polarity;
112  uint16_t gpio_pin;
114 
115 typedef enum {
120 
124 typedef struct {
131 
138 
139 typedef uint32_t (*sdmmchost_clock_init_func_t)(SDMMCHOST_Type *base, uint32_t clk_freq, bool need_reverse);
140 
141 typedef void (*sdmmchost_power_switch_func_t)(SDMMCHOST_Type *base, bool on_off);
142 
144 
146 
147 typedef void (*sdmmchost_cmd_line_init_func_t)(SDMMCHOST_Type *base, bool push_pull);
148 
150 
151 typedef struct {
152 
153  void (*cmd_io_init)(SDMMCHOST_Type *base, bool open_drain, bool is_1v8);
154 
155  void (*clk_data_io_init)(SDMMCHOST_Type *base, uint32_t data_width, bool is_1v8);
156 
157  void (*pwr_io_init)(SDMMCHOST_Type *base, bool as_gpio);
158 
159  void (*cd_io_init)(SDMMCHOST_Type *base, bool as_gpio);
160 
161  void (*vsel_io_init)(SDMMCHOST_Type *base, bool as_gpio);
162 
163  void (*ds_io_init)(SDMMCHOST_Type *base);
165 
166 
167 typedef struct {
168  uint8_t hart_id;
169  uint8_t instance_num;
170  uint32_t host_flags;
175 
176  void (*delay_ms)(uint32_t ms);
178 
179 
180 typedef struct {
182 
187  uint32_t clock_freq;
188  /* Host Transfer Fields */
193  uint32_t buffer[128];
194 
195  /* Host run-time fields */
200  uint32_t int_stat;
201  uint32_t auto_cmd_stat;
202  uint32_t adma_error_stat;
203 } sdmmc_host_t;
204 
205 
207 
209 
210 uint32_t sdmmchost_set_card_clock(sdmmc_host_t *host, uint32_t freq, bool clock_inverse);
211 
212 void sdmmchost_deinit(sdmmc_host_t *host);
213 
214 void sdmmchost_reset(sdmmc_host_t *host);
215 
217 
219 
221 
223 
224 void sdmmchost_init_io(sdmmc_host_t *host, hpm_sdmmc_operation_mode_t operation_mode);
225 
226 void sdmmchost_delay_ms(sdmmc_host_t *host, uint32_t ms);
227 
229 
230 void sdmmchost_enable_emmc_support(sdmmc_host_t *host, bool enable);
231 
233 
235 
237 
238 void sdmmchost_enable_enhanced_data_strobe(sdmmc_host_t *host, bool enable);
239 
241 
243 
245 
247 
248 
249 #endif /* HPM_SDMMC_HOST_H */
uint32_t hpm_stat_t
Definition: hpm_common.h:123
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.
void sdmmchost_enable_enhanced_data_strobe(sdmmc_host_t *host, bool enable)
Definition: hpm_sdmmc_host.c:550
sdxc_capabilities_t sdmmchost_capabilities_t
Definition: hpm_sdmmc_host.h:137
void(* sdmmchost_switch_1v8_io_func_t)(SDMMCHOST_Type *base)
Definition: hpm_sdmmc_host.h:145
sdmmc_card_detection_mode_t
SD/MMC Host Card Detection Modes.
Definition: hpm_sdmmc_host.h:61
@ sdmmc_host_card_detection_via_sdxc
Definition: hpm_sdmmc_host.h:64
@ sdmmc_host_card_detection_via_gpio
Definition: hpm_sdmmc_host.h:63
@ sdmmc_host_card_detection_none
Definition: hpm_sdmmc_host.h:62
void sdmmchost_set_cardclk_delay_chain(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:385
void sdmmchost_delay_ms(sdmmc_host_t *host, uint32_t ms)
Definition: hpm_sdmmc_host.c:35
hpm_stat_t sdmmchost_send_command(sdmmc_host_t *host, sdmmchost_cmd_t *cmd)
Definition: hpm_sdmmc_host.c:211
bool(* sdmmchost_card_detect_func_t)(SDMMCHOST_Type *base)
Definition: hpm_sdmmc_host.h:149
void sdmmchost_set_rxclk_delay_chain(sdmmc_host_t *host)
void sdmmchost_set_card_bus_width(sdmmc_host_t *host, sdmmc_buswidth_t bus_width)
Definition: hpm_sdmmc_host.c:191
sdxc_xfer_t sdmmchost_xfer_t
Definition: hpm_sdmmc_host.h:132
sdmmc_dev_type_t
SD/MMC Device Type definitions.
Definition: hpm_sdmmc_host.h:52
@ sdmmc_dev_type_sdio
Definition: hpm_sdmmc_host.h:55
@ sdmmc_dev_type_sd
Definition: hpm_sdmmc_host.h:54
@ sdmmc_dev_type_emmc
Definition: hpm_sdmmc_host.h:53
uint32_t sdmmchost_set_card_clock(sdmmc_host_t *host, uint32_t freq, bool clock_inverse)
Definition: hpm_sdmmc_host.c:198
void sdmmchost_select_voltage(sdmmc_host_t *host, hpm_sdmmc_io_volt_t io_volt)
Definition: hpm_sdmmc_host.c:562
sdxc_adma2_descriptor_t sdmmc_adma2_desc_t
Definition: hpm_sdmmc_host.h:135
void sdmmchost_wait_card_active(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:205
hpm_sdmmc_io_volt_t
SDMMC IO Voltage.
Definition: hpm_sdmmc_host.h:70
@ hpm_sdmmc_io_voltage_1v8
Definition: hpm_sdmmc_host.h:72
@ hpm_sdmmc_io_voltage_3v3
Definition: hpm_sdmmc_host.h:71
hpm_stat_t sdmmchost_init(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:50
bool sdmmchost_is_voltage_switch_supported(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:402
bool sdmmchost_is_card_detected(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:173
uint32_t(* sdmmchost_clock_init_func_t)(SDMMCHOST_Type *base, uint32_t clk_freq, bool need_reverse)
Definition: hpm_sdmmc_host.h:139
void(* sdmmchost_io_init_func_t)(SDMMCHOST_Type *base)
Definition: hpm_sdmmc_host.h:143
hpm_stat_t sdmmchost_error_recovery(sdmmc_host_t *host, sdmmchost_cmd_t *abort_cmd)
Definition: hpm_sdmmc_host.c:379
void sdmmchost_reset(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:161
hpm_stat_t sdmmchost_set_speed_mode(sdmmc_host_t *host, sdmmc_speed_mode_t speed_mode)
Definition: hpm_sdmmc_host.c:369
hpm_sdmmc_power_option_t
Definition: hpm_sdmmc_host.h:115
@ hpm_sdmmc_power_off
Definition: hpm_sdmmc_host.h:116
@ hpm_sdmmc_power_on
Definition: hpm_sdmmc_host.h:118
@ hpm_sdmmc_power_up
Definition: hpm_sdmmc_host.h:117
void sdmmchost_deinit(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:155
hpm_stat_t sdmmchost_transfer(sdmmc_host_t *host, sdmmchost_xfer_t *content)
Definition: hpm_sdmmc_host.c:281
void sdmmchost_init_io(sdmmc_host_t *host, hpm_sdmmc_operation_mode_t operation_mode)
Definition: hpm_sdmmc_host.c:447
sdxc_command_t sdmmchost_cmd_t
Definition: hpm_sdmmc_host.h:133
hpm_stat_t sdmmchost_switch_to_1v8(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:107
sdxc_data_t sdmmchost_data_t
Definition: hpm_sdmmc_host.h:134
void sdmmchost_enable_emmc_support(sdmmc_host_t *host, bool enable)
Definition: hpm_sdmmc_host.c:168
void(* sdmmchost_cmd_line_init_func_t)(SDMMCHOST_Type *base, bool push_pull)
Definition: hpm_sdmmc_host.h:147
sdmmc_speed_mode_t
SD/MMC Speed definitions.
Definition: hpm_sdmmc_host.h:88
@ sdmmc_sd_speed_ddr50
Definition: hpm_sdmmc_host.h:96
@ sdmmc_sd_speed_normal
Definition: hpm_sdmmc_host.h:90
@ sdmmc_emmc_speed_high_speed_ddr
Definition: hpm_sdmmc_host.h:102
@ sdmmc_emmc_speed_hs200
Definition: hpm_sdmmc_host.h:101
@ sdmmc_emmc_speed_hs400
Definition: hpm_sdmmc_host.h:103
@ sdmmc_sd_speed_sdr50
Definition: hpm_sdmmc_host.h:94
@ sdmmc_emmc_speed_high_speed_sdr
Definition: hpm_sdmmc_host.h:100
@ sdmmc_sd_speed_sdr104
Definition: hpm_sdmmc_host.h:95
@ sdmmc_sd_speed_sdr25
Definition: hpm_sdmmc_host.h:93
@ sdmmc_emmc_speed_legacy
Definition: hpm_sdmmc_host.h:99
@ sdmmc_sd_speed_sdr12
Definition: hpm_sdmmc_host.h:92
@ sdmmc_sd_speed_high
Definition: hpm_sdmmc_host.h:91
hpm_sdmmc_operation_mode_t
SDMMC Operattion mode.
Definition: hpm_sdmmc_host.h:78
@ hpm_sdmmc_operation_mode_inactive
Definition: hpm_sdmmc_host.h:79
@ hpm_sdmmc_operation_mode_identification
Definition: hpm_sdmmc_host.h:80
@ hpm_sdmmc_operation_mode_transfer
Definition: hpm_sdmmc_host.h:81
@ hpm_sdmmc_operation_mode_interrupt
Definition: hpm_sdmmc_host.h:82
SDXC_Type SDMMCHOST_Type
Definition: hpm_sdmmc_host.h:136
sdmmc_buswidth_t
SD/MMC Bus Width definitions.
Definition: hpm_sdmmc_host.h:43
@ sdmmc_bus_width_8bit
Definition: hpm_sdmmc_host.h:46
@ sdmmc_bus_width_1bit
Definition: hpm_sdmmc_host.h:44
@ sdmmc_bus_width_4bit
Definition: hpm_sdmmc_host.h:45
void(* sdmmchost_power_switch_func_t)(SDMMCHOST_Type *base, bool on_off)
Definition: hpm_sdmmc_host.h:141
void sdmmchost_set_data_strobe_delay(sdmmc_host_t *host)
Definition: hpm_sdmmc_host.c:556
Definition: hpm_sdxc_regs.h:12
SDMMC extra Pin info.
Definition: hpm_sdmmc_host.h:124
hpm_sdmmc_pin_info_t rst_pin
Definition: hpm_sdmmc_host.h:128
hpm_sdmmc_pin_info_t vsel_pin
Definition: hpm_sdmmc_host.h:126
hpm_sdmmc_pin_info_t wp_pin
Definition: hpm_sdmmc_host.h:129
hpm_sdmmc_pin_info_t pwr_pin
Definition: hpm_sdmmc_host.h:127
hpm_sdmmc_pin_info_t cd_pin
Definition: hpm_sdmmc_host.h:125
SDMMC Pin info structure.
Definition: hpm_sdmmc_host.h:109
uint8_t polarity
Definition: hpm_sdmmc_host.h:111
uint16_t gpio_pin
Definition: hpm_sdmmc_host.h:112
bool use_gpio
Definition: hpm_sdmmc_host.h:110
Definition: hpm_sdmmc_host.h:167
SDMMCHOST_Type * base
Definition: hpm_sdmmc_host.h:171
uint32_t host_flags
Definition: hpm_sdmmc_host.h:170
uint8_t instance_num
Definition: hpm_sdmmc_host.h:169
sdmmchost_clock_init_func_t clock_init_func
Definition: hpm_sdmmc_host.h:173
uint8_t hart_id
Definition: hpm_sdmmc_host.h:168
hpm_sdmmc_extra_io_data_t io_data
Definition: hpm_sdmmc_host.h:172
sdmmc_io_init_apis_t io_init_apis
Definition: hpm_sdmmc_host.h:174
Definition: hpm_sdmmc_host.h:180
sdmmc_adma2_desc_t adma2_desc
Definition: hpm_sdmmc_host.h:192
uint32_t clock_freq
Definition: hpm_sdmmc_host.h:187
bool card_init_done
Definition: hpm_sdmmc_host.h:197
bool cmd_done_or_error
Definition: hpm_sdmmc_host.h:198
uint32_t auto_cmd_stat
Definition: hpm_sdmmc_host.h:201
sdmmc_buswidth_t bus_width
Definition: hpm_sdmmc_host.h:185
bool card_inserted
Definition: hpm_sdmmc_host.h:196
hpm_sdmmc_io_volt_t io_voltage
Definition: hpm_sdmmc_host.h:186
uint32_t adma_error_stat
Definition: hpm_sdmmc_host.h:202
bool transfer_complete_or_error
Definition: hpm_sdmmc_host.h:199
hpm_sdmmc_operation_mode_t operation_mode
Definition: hpm_sdmmc_host.h:184
sdmmchost_cmd_t cmd
Definition: hpm_sdmmc_host.h:190
sdmmchost_data_t data
Definition: hpm_sdmmc_host.h:191
sdmmc_host_param_t host_param
Definition: hpm_sdmmc_host.h:181
sdmmchost_xfer_t xfer
Definition: hpm_sdmmc_host.h:189
sdmmc_dev_type_t dev_type
Definition: hpm_sdmmc_host.h:183
uint32_t int_stat
Definition: hpm_sdmmc_host.h:200
Definition: hpm_sdmmc_host.h:151
SDXC Capacities.
Definition: hpm_sdxc_drv.h:295