Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/storage/sdmmc_driver.h
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2025 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#ifndef _SDMMC_DRIVER_H_
19
#define _SDMMC_DRIVER_H_
20
21
#include <utils/types.h>
22
#include <storage/sdmmc_t210.h>
23
24
/*! SDMMC controller IDs. */
25
#define SDMMC_1 0 // Version 4.00.
26
#define SDMMC_2 1 // Version 5.0 + SW CQE + Enhanced Strobe.
27
#define SDMMC_3 2 // Version 4.00.
28
#define SDMMC_4 3 // Version 5.0 + SW CQE + Enhanced Strobe.
29
30
/*! SDMMC power types. */
31
#define SDMMC_POWER_OFF 0
32
#define SDMMC_POWER_1_8 1
33
#define SDMMC_POWER_3_3 2
34
35
/*! SDMMC response types. */
36
#define SDMMC_RSP_TYPE_0 0
37
#define SDMMC_RSP_TYPE_1 1
38
#define SDMMC_RSP_TYPE_2 2
39
#define SDMMC_RSP_TYPE_3 3
40
#define SDMMC_RSP_TYPE_4 4
41
#define SDMMC_RSP_TYPE_5 5
42
43
/*! SDMMC bus widths. */
44
#define SDMMC_BUS_WIDTH_1 0
45
#define SDMMC_BUS_WIDTH_4 1
46
#define SDMMC_BUS_WIDTH_8 2
47
48
/*! SDMMC mask interrupt status. */
49
#define SDMMC_MASKINT_MASKED 0
50
#define SDMMC_MASKINT_NOERROR 1
51
#define SDMMC_MASKINT_ERROR 2
52
53
/*! SDMMC present state. 0x24. */
54
#define SDHCI_CMD_INHIBIT BIT(0)
55
#define SDHCI_DATA_INHIBIT BIT(1)
56
#define SDHCI_DAT_LINE_ACTIVE BIT(2)
57
#define SDHCI_RETUNING_REQUEST BIT(3)
58
#define SDHCI_EMMC_LINE_LVL_MASK (0xFU << 4)
59
#define SDHCI_DATA_4_LVL BIT(4) // eMMC only.
60
#define SDHCI_DATA_5_LVL BIT(5) // eMMC only.
61
#define SDHCI_DATA_6_LVL BIT(6) // eMMC only.
62
#define SDHCI_DATA_7_LVL BIT(7) // eMMC only.
63
#define SDHCI_DOING_WRITE BIT(8)
64
#define SDHCI_DOING_READ BIT(9) // SD only.
65
#define SDHCI_SPACE_AVAILABLE BIT(10) // Write buffer empty.
66
#define SDHCI_DATA_AVAILABLE BIT(11) // Read buffer has data.
67
#define SDHCI_CARD_PRESENT BIT(16)
68
#define SDHCI_CD_STABLE BIT(17)
69
#define SDHCI_CD_LVL BIT(18)
70
#define SDHCI_WRITE_PROTECT BIT(19)
71
#define SDHCI_DATA_LVL_MASK 0xF00000
72
#define SDHCI_DATA_0_LVL BIT(20)
73
#define SDHCI_DATA_1_LVL BIT(21)
74
#define SDHCI_DATA_2_LVL BIT(22)
75
#define SDHCI_DATA_3_LVL BIT(23)
76
#define SDHCI_CMD_LVL BIT(24)
77
78
/*! SDMMC transfer mode. 0x0C. */
79
#define SDHCI_TRNS_DMA BIT(0)
80
#define SDHCI_TRNS_BLK_CNT_EN BIT(1)
81
#define SDHCI_TRNS_AUTO_CMD12 (1U << 2)
82
#define SDHCI_TRNS_AUTO_CMD23 (2U << 2)
83
#define SDHCI_TRNS_WRITE (0U << 4)
84
#define SDHCI_TRNS_READ BIT(4)
85
#define SDHCI_TRNS_MULTI BIT(5)
86
#define SDHCI_TRNS_RTYPE_R1 (0U << 6)
87
#define SDHCI_TRNS_RTYPE_R5 BIT(6)
88
#define SDHCI_TRNS_RSP_ERR_CHK BIT(7)
89
#define SDHCI_TRNS_RSP_INT_DIS BIT(8)
90
91
/*! SDMMC command. 0x0E. */
92
#define SDHCI_CMD_RESP_MASK 0x3
93
#define SDHCI_CMD_RESP_NO_RESP 0x0
94
#define SDHCI_CMD_RESP_LEN136 0x1
95
#define SDHCI_CMD_RESP_LEN48 0x2
96
#define SDHCI_CMD_RESP_LEN48_BUSY 0x3
97
#define SDHCI_CMD_CRC BIT(3)
98
#define SDHCI_CMD_INDEX BIT(4)
99
#define SDHCI_CMD_DATA BIT(5)
100
#define SDHCI_CMD_TYPE_NORMAL (0U << 6)
101
#define SDHCI_CMD_TYPE_SUSPEND (1U << 6)
102
#define SDHCI_CMD_TYPE_RESUME (2U << 6)
103
#define SDHCI_CMD_TYPE_ABORT (3U << 6)
104
#define SDHCI_CMD_SPI_CS_LOW BIT(7)
105
#define SDHCI_CMD_IDX(cmd) ((cmd) << 8)
106
107
108
/*! SDMMC host control. 0x28. */
109
#define SDHCI_CTRL_LED BIT(0)
110
#define SDHCI_CTRL_4BITBUS BIT(1) // SD only.
111
#define SDHCI_CTRL_HISPD BIT(2) // SD only.
112
#define SDHCI_CTRL_DMA_MASK (3U << 3)
113
#define SDHCI_CTRL_SDMA (0U << 3)
114
#define SDHCI_CTRL_ADMA1 (1U << 3)
115
#define SDHCI_CTRL_ADMA32 (2U << 3)
116
#define SDHCI_CTRL_ADMA64 (3U << 3)
117
#define SDHCI_CTRL_8BITBUS BIT(5) // eMMC only (or UHS-II).
118
#define SDHCI_CTRL_CDTEST_INS BIT(6)
119
#define SDHCI_CTRL_CDTEST_EN BIT(7)
120
121
/*! SDMMC host control 2. 0x3E. */
122
#define SDHCI_CTRL_UHS_MASK 0x7
123
#define SDHCI_CTRL_VDD_180 BIT(3)
124
#define SDHCI_CTRL_DRV_TYPE_MASK (3U << 4)
125
#define SDHCI_CTRL_DRV_TYPE_B (0U << 4)
126
#define SDHCI_CTRL_DRV_TYPE_A (1U << 4)
127
#define SDHCI_CTRL_DRV_TYPE_C (2U << 4)
128
#define SDHCI_CTRL_DRV_TYPE_D (3U << 4)
129
#define SDHCI_CTRL_DRV_TYPE(type) ((type) << 4)
130
#define SDHCI_CTRL_EXEC_TUNING BIT(6)
131
#define SDHCI_CTRL_TUNED_CLK_SHIFT 7
132
#define SDHCI_CTRL_TUNED_CLK BIT(7)
133
#define SDHCI_HOST_VERSION_4_EN BIT(12)
134
#define SDHCI_ADDRESSING_64BIT_EN BIT(13)
135
#define SDHCI_CTRL_PRESET_VAL_EN BIT(15)
136
137
/*! SDMMC power control. 0x29. */
138
#define SDHCI_POWER_ON BIT(0)
139
#define SDHCI_POWER_180 (5U << 1)
140
#define SDHCI_POWER_300 (6U << 1)
141
#define SDHCI_POWER_330 (7U << 1)
142
#define SDHCI_POWER_MASK 0xF1 // UHS-II only.
143
144
/*! SDMMC clock control. 0x2C. */
145
#define SDHCI_CLOCK_INT_EN BIT(0) // Internal Clock.
146
#define SDHCI_CLOCK_INT_STABLE BIT(1) // Internal Clock Stable.
147
#define SDHCI_CLOCK_CARD_EN BIT(2)
148
#define SDHCI_PROG_CLOCK_MODE BIT(5)
149
#define SDHCI_DIV_HI_SHIFT 6
150
#define SDHCI_DIV_HI_MASK (3U << SDHCI_DIV_HI_SHIFT)
151
#define SDHCI_DIV_LO_SHIFT 8
152
#define SDHCI_DIV_MASK (0xFFU << SDHCI_DIV_LO_SHIFT)
153
154
155
/*! SDMMC software reset. 0x2F. */
156
#define SDHCI_RESET_ALL BIT(0)
157
#define SDHCI_RESET_CMD BIT(1)
158
#define SDHCI_RESET_DATA BIT(2)
159
160
/*! SDMMC interrupt status and control. 0x30/0x34. */
161
#define SDHCI_INT_RESPONSE BIT(0)
162
#define SDHCI_INT_DATA_END BIT(1)
163
#define SDHCI_INT_BLK_GAP BIT(2)
164
#define SDHCI_INT_DMA_END BIT(3)
165
#define SDHCI_INT_SPACE_AVAIL BIT(4) // Write buffer empty.
166
#define SDHCI_INT_DATA_AVAIL BIT(5) // Read buffer has data.
167
#define SDHCI_INT_CARD_INSERT BIT(6)
168
#define SDHCI_INT_CARD_REMOVE BIT(7)
169
#define SDHCI_INT_CARD_INT BIT(8)
170
#define SDHCI_INT_RETUNE BIT(12)
171
#define SDHCI_INT_ERROR BIT(15)
172
173
/*! SDMMC error interrupt status and control. 0x32/0x36. */
174
#define SDHCI_ERR_INT_CMD_TIMEOUT BIT(0)
175
#define SDHCI_ERR_INT_CMD_CRC BIT(1)
176
#define SDHCI_ERR_INT_CMD_END_BIT BIT(2)
177
#define SDHCI_ERR_INT_CMD_INDEX BIT(3)
178
#define SDHCI_ERR_INT_DATA_TIMEOUT BIT(4)
179
#define SDHCI_ERR_INT_DATA_CRC BIT(5)
180
#define SDHCI_ERR_INT_DATA_END_BIT BIT(6)
181
#define SDHCI_ERR_INT_BUS_POWER BIT(7)
182
#define SDHCI_ERR_INT_AUTO_CMD12 BIT(8)
183
#define SDHCI_ERR_INT_ADMA BIT(9)
184
#define SDHCI_ERR_INT_TUNE BIT(10)
185
#define SDHCI_ERR_INT_RSP BIT(11)
186
#define SDHCI_ERR_INT_TARGET_RSP BIT(12)
187
#define SDHCI_ERR_INT_SPI BIT(13)
188
#define SDHCI_ERR_INT_VND_BOOT_TMO BIT(14)
189
#define SDHCI_ERR_INT_VND_BOOT_ACK BIT(15)
190
191
#define SDHCI_ERR_INT_ALL_EXCEPT_ADMA_BUSPWR \
192
(SDHCI_ERR_INT_AUTO_CMD12 | SDHCI_ERR_INT_DATA_END_BIT | \
193
SDHCI_ERR_INT_DATA_CRC | SDHCI_ERR_INT_DATA_TIMEOUT | \
194
SDHCI_ERR_INT_CMD_INDEX | SDHCI_ERR_INT_CMD_END_BIT | \
195
SDHCI_ERR_INT_CMD_CRC | SDHCI_ERR_INT_CMD_TIMEOUT)
196
197
/*! Host Capability 1. 0x40. */
198
#define SDHCI_CAP_TM_CLK_FREQ_MASK 0x3F
199
#define SDHCI_CAP_TM_UNIT_MHZ BIT(7)
200
#define SDHCI_CAP_BASE_CLK_FREQ_MASK (0xFFU << 8)
201
#define SDHCI_CAP_MAX_BLK_LEN_MASK (3U << 16)
202
#define SDHCI_CAP_EMMC_8BIT BIT(18)
203
#define SDHCI_CAP_ADMA2 BIT(19)
204
#define SDHCI_CAP_HISPD BIT(21)
205
#define SDHCI_CAP_SDMA BIT(22)
206
#define SDHCI_CAP_SUSPEND_RESUME BIT(23)
207
#define SDHCI_CAP_3_3_V BIT(24)
208
#define SDHCI_CAP_3_0_V BIT(25)
209
#define SDHCI_CAP_1_8_V BIT(26)
210
#define SDHCI_CAP_64BIT BIT(28)
211
#define SDHCI_CAP_ASYNC_INT BIT(29)
212
#define SDHCI_CAP_SLOT_TYPE_MASK (3U << 30)
213
#define SDHCI_CAP_SLOT_TYPE_REMOVABLE (0U << 30)
214
#define SDHCI_CAP_SLOT_TYPE_EMBEDDED (1U << 30)
215
#define SDHCI_CAP_SLOT_TYPE_SHARED (2U << 30)
216
#define SDHCI_CAP_SLOT_TYPE_UHS2 (3U << 30)
217
218
/*! Host Capability 2. 0x44. */
219
#define SDHCI_CAP_SDR50 BIT(0)
220
#define SDHCI_CAP_SDR5104 BIT(1)
221
#define SDHCI_CAP_DDR50 BIT(2)
222
#define SDHCI_CAP_UHS2 BIT(3)
223
#define SDHCI_CAP_DRV_TYPE_A BIT(4)
224
#define SDHCI_CAP_DRV_TYPE_C BIT(5)
225
#define SDHCI_CAP_DRV_TYPE_D BIT(6)
226
#define SDHCI_CAP_RSP_TIMER_CNT_MASK (0xFU << 8)
227
#define SDHCI_CAP_SDR50_TUNING BIT(13)
228
#define SDHCI_CAP_RSP_MODES_MASK (3U << 14)
229
#define SDHCI_CAP_CLK_MULT (0xFFU << 16)
230
#define SDHCI_CAP_ADMA3 BIT(27)
231
#define SDHCI_CAP_VDD2_1_8V BIT(28)
232
233
/*! SDMMC max current. 0x48 */
234
#define SDHCI_MAX_CURRENT_3_3_V_MASK (0xFFU << 0)
235
#define SDHCI_MAX_CURRENT_3_0_V_MASK (0xFFU << 8)
236
#define SDHCI_MAX_CURRENT_1_8_V_MASK (0xFFU << 16)
237
#define SDHCI_MAX_CURRENT_MULTIPLIER 4
238
239
/*! SDMMC max current. 0x4C */
240
#define SDHCI_MAX_CURRENT_1_8_V_VDD2_MASK (0xFFU << 0)
241
242
/*! SD bus speeds. */
243
#define UHS_SDR12_BUS_SPEED 0
244
#define HIGH_SPEED_BUS_SPEED 1
245
#define UHS_SDR25_BUS_SPEED 1
246
#define UHS_SDR50_BUS_SPEED 2
247
#define UHS_SDR104_BUS_SPEED 3
248
#define UHS_DDR50_BUS_SPEED 4
249
#define HS400_BUS_SPEED 5
250
251
/*! SDMMC timmings. */
252
#define SDHCI_TIMING_MMC_ID 0
253
#define SDHCI_TIMING_MMC_LS26 1
254
#define SDHCI_TIMING_MMC_HS52 2
255
#define SDHCI_TIMING_MMC_HS200 3
256
#define SDHCI_TIMING_MMC_HS400 4
257
#define SDHCI_TIMING_SD_ID 5
258
#define SDHCI_TIMING_SD_DS12 6
259
#define SDHCI_TIMING_SD_HS25 7
260
#define SDHCI_TIMING_UHS_SDR12 8
261
#define SDHCI_TIMING_UHS_SDR25 9
262
#define SDHCI_TIMING_UHS_SDR50 10
263
#define SDHCI_TIMING_UHS_SDR104 11
264
#define SDHCI_TIMING_UHS_DDR50 12
265
// SDR104 with a 163.2MHz -> 81.6MHz clock.
266
#define SDHCI_TIMING_UHS_SDR82 13 // GC FPGA. Obsolete and Repurposed. MMC_HS50 -> SDR82.
267
#define SDHCI_TIMING_MMC_HS100 14 // GC ASIC.
268
#define SDHCI_TIMING_UHS_DDR200 15
269
270
/*! SDMMC Low power features. */
271
#define SDMMC_POWER_SAVE_DISABLE 0
272
#define SDMMC_POWER_SAVE_ENABLE 1
273
274
/*! Helper for SWITCH command argument. */
275
#define SDMMC_SWITCH(mode, index, value) (((mode) << 24) | ((index) << 16) | ((value) << 8))
276
277
#define HW_TAP_TUNING 0x100
278
#define INVALID_TAP 0x100
279
#define SAMPLING_WINDOW_SIZE_MIN 8
280
281
/*! SDMMC controller context. */
282
typedef struct _sdmmc_t
283
{
284
t210_sdmmc_t *regs;
285
u32 id;
286
u32 card_clock;
287
u32 clock_stopped;
288
int powersave_enabled;
289
int periodic_calibration;
290
int card_clock_enabled;
291
int venclkctl_set;
292
u32 venclkctl_tap;
293
u32 expected_rsp_type;
294
u32 dma_addr_next;
295
u32 rsp[4];
296
u32 stop_trn_rsp;
297
u32 error_sts;
298
int t210b01;
299
} sdmmc_t;
300
301
/*! SDMMC command. */
302
typedef struct _sdmmc_cmd_t
303
{
304
u16 cmd;
305
u32 arg;
306
u32 rsp_type;
307
u32 check_busy;
308
} sdmmc_cmd_t;
309
310
/*! SDMMC request. */
311
typedef struct _sdmmc_req_t
312
{
313
void *buf;
314
u32 blksize;
315
u32 num_sectors;
316
int is_write;
317
int is_multi_block;
318
int is_auto_stop_trn;
319
} sdmmc_req_t;
320
321
int sdmmc_get_io_power(sdmmc_t *sdmmc);
322
u32 sdmmc_get_bus_width(sdmmc_t *sdmmc);
323
void sdmmc_set_bus_width(sdmmc_t *sdmmc, u32 bus_width);
324
void sdmmc_save_tap_value(sdmmc_t *sdmmc);
325
void sdmmc_setup_drv_type(sdmmc_t *sdmmc, u32 type);
326
int sdmmc_setup_clock(sdmmc_t *sdmmc, u32 type);
327
void sdmmc_card_clock_powersave(sdmmc_t *sdmmc, int powersave_enable);
328
int sdmmc_get_cached_rsp(sdmmc_t *sdmmc, u32 *rsp, u32 type);
329
int sdmmc_tuning_execute(sdmmc_t *sdmmc, u32 type, u32 cmd);
330
int sdmmc_stop_transmission(sdmmc_t *sdmmc, u32 *rsp);
331
bool sdmmc_get_sd_inserted();
332
int sdmmc_init(sdmmc_t *sdmmc, u32 id, u32 power, u32 bus_width, u32 type);
333
void sdmmc_end(sdmmc_t *sdmmc);
334
void sdmmc_init_cmd(sdmmc_cmd_t *cmdbuf, u16 cmd, u32 arg, u32 rsp_type, u32 check_busy);
335
int sdmmc_execute_cmd(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_t *req, u32 *blkcnt_out);
336
int sdmmc_enable_low_voltage(sdmmc_t *sdmmc);
337
338
#endif
339
340