Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/storage/sdmmc.h
3694 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_H_
19
#define _SDMMC_H_
20
21
#include <utils/types.h>
22
#include <storage/sd_def.h>
23
#include <storage/sdmmc_driver.h>
24
25
#define SDMMC_CMD_BLOCKSIZE 64
26
#define SDMMC_DAT_BLOCKSIZE 512
27
#define SDMMC_HMAX_BLOCKNUM 0xFFFF // HW max.
28
#define SDMMC_AMAX_BLOCKNUM 0xF000 // Aligned max.
29
30
extern u32 sd_power_cycle_time_start;
31
32
typedef enum _sdmmc_type
33
{
34
MMC_SD = 0,
35
MMC_EMMC = 1,
36
37
EMMC_GPP = 0,
38
EMMC_BOOT0 = 1,
39
EMMC_BOOT1 = 2,
40
EMMC_RPMB = 3
41
} sdmmc_type;
42
43
typedef struct _mmc_sandisk_advanced_report_t
44
{
45
u32 power_inits;
46
47
u32 max_erase_cycles_sys;
48
u32 max_erase_cycles_slc;
49
u32 max_erase_cycles_mlc;
50
51
u32 min_erase_cycles_sys;
52
u32 min_erase_cycles_slc;
53
u32 min_erase_cycles_mlc;
54
55
u32 max_erase_cycles_euda;
56
u32 min_erase_cycles_euda;
57
u32 avg_erase_cycles_euda;
58
u32 read_reclaim_cnt_euda;
59
u32 bad_blocks_euda;
60
61
u32 pre_eol_euda;
62
u32 pre_eol_sys;
63
u32 pre_eol_mlc;
64
65
u32 uncorrectable_ecc;
66
67
u32 temperature_now;
68
u32 temperature_min;
69
u32 temperature_max;
70
71
u32 health_pct_euda;
72
u32 health_pct_sys;
73
u32 health_pct_mlc;
74
75
u32 unk0;
76
u32 unk1;
77
u32 unk2;
78
79
u32 reserved[78];
80
} mmc_sandisk_advanced_report_t;
81
82
typedef struct _mmc_sandisk_report_t
83
{
84
u32 avg_erase_cycles_sys;
85
u32 avg_erase_cycles_slc;
86
u32 avg_erase_cycles_mlc;
87
88
u32 read_reclaim_cnt_sys;
89
u32 read_reclaim_cnt_slc;
90
u32 read_reclaim_cnt_mlc;
91
92
u32 bad_blocks_factory;
93
u32 bad_blocks_sys;
94
u32 bad_blocks_slc;
95
u32 bad_blocks_mlc;
96
97
u32 fw_updates_cnt;
98
99
u8 fw_update_date[12];
100
u8 fw_update_time[8];
101
102
u32 total_writes_100mb;
103
u32 vdrops;
104
u32 vdroops;
105
106
u32 vdrops_failed_data_rec;
107
u32 vdrops_data_rec_ops;
108
109
u32 total_writes_slc_100mb;
110
u32 total_writes_mlc_100mb;
111
112
u32 mlc_bigfile_mode_limit_exceeded;
113
u32 avg_erase_cycles_hybrid;
114
115
mmc_sandisk_advanced_report_t advanced;
116
} mmc_sandisk_report_t;
117
118
typedef struct _mmc_cid
119
{
120
u32 manfid;
121
u8 prod_name[8];
122
u32 serial;
123
u16 oemid;
124
u16 year;
125
u8 prv;
126
u8 hwrev;
127
u8 fwrev;
128
u8 month;
129
} mmc_cid_t;
130
131
typedef struct _mmc_csd
132
{
133
u8 structure;
134
u8 mmca_vsn;
135
u16 cmdclass;
136
u32 c_size;
137
u32 r2w_factor;
138
u32 read_blkbits;
139
u32 capacity;
140
u8 write_protect;
141
u16 busspeed;
142
} mmc_csd_t;
143
144
typedef struct _mmc_ext_csd
145
{
146
u8 bkops; /* background support bit */
147
u8 bkops_en; /* manual bkops enable bit */
148
u8 rev;
149
u8 ext_struct; /* 194 */
150
u8 card_type; /* 196 */
151
u8 pre_eol_info;
152
u8 dev_life_est_a;
153
u8 dev_life_est_b;
154
u8 boot_mult;
155
u8 rpmb_mult;
156
u16 dev_version;
157
u32 cache_size;
158
u32 max_enh_mult;
159
} mmc_ext_csd_t;
160
161
typedef struct _sd_scr
162
{
163
u8 sda_vsn;
164
u8 sda_spec3;
165
u8 bus_widths;
166
u8 cmds;
167
} sd_scr_t;
168
169
typedef struct _sd_ssr
170
{
171
u8 bus_width;
172
u8 speed_class;
173
u8 uhs_grade;
174
u8 video_class;
175
u8 app_class;
176
u8 au_size;
177
u8 uhs_au_size;
178
u8 perf_enhance;
179
u32 protected_size;
180
} sd_ssr_t;
181
182
typedef struct _sd_ext_reg_t
183
{
184
u8 cmdq;
185
u8 cmdq_ext;
186
u8 cache;
187
u8 cache_ext;
188
int valid;
189
} sd_ext_reg_t;
190
191
/*! SDMMC storage context. */
192
typedef struct _sdmmc_storage_t
193
{
194
sdmmc_t *sdmmc;
195
196
int initialized;
197
int is_low_voltage;
198
int has_sector_access;
199
u32 rca;
200
u32 sec_cnt;
201
u32 partition;
202
u32 max_power;
203
u8 raw_cid[0x10] __attribute__((aligned(SDMMC_ADMA_ADDR_ALIGN)));
204
u8 raw_csd[0x10] __attribute__((aligned(SDMMC_ADMA_ADDR_ALIGN)));
205
u8 raw_scr[8] __attribute__((aligned(SDMMC_ADMA_ADDR_ALIGN)));
206
u8 raw_ssr[SDMMC_CMD_BLOCKSIZE] __attribute__((aligned(SDMMC_ADMA_ADDR_ALIGN)));
207
u8 raw_ext_csd[SDMMC_DAT_BLOCKSIZE] __attribute__((aligned(SDMMC_ADMA_ADDR_ALIGN)));
208
mmc_cid_t cid;
209
mmc_csd_t csd;
210
mmc_ext_csd_t ext_csd;
211
sd_scr_t scr;
212
sd_ssr_t ssr;
213
sd_ext_reg_t ser;
214
} sdmmc_storage_t;
215
216
typedef struct _sd_func_modes_t
217
{
218
u16 access_mode;
219
u16 cmd_system;
220
u16 driver_strength;
221
u16 power_limit;
222
} sd_func_modes_t;
223
224
int sdmmc_storage_end(sdmmc_storage_t *storage);
225
int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf);
226
int sdmmc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf);
227
int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type);
228
int sdmmc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition);
229
void sdmmc_storage_init_wait_sd();
230
int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type);
231
int sdmmc_storage_init_gc(sdmmc_storage_t *storage, sdmmc_t *sdmmc);
232
233
int sdmmc_storage_execute_vendor_cmd(sdmmc_storage_t *storage, u32 arg);
234
int sdmmc_storage_vendor_sandisk_report(sdmmc_storage_t *storage, void *buf);
235
236
int mmc_storage_get_ext_csd(sdmmc_storage_t *storage);
237
238
int sd_storage_get_ext_reg(sdmmc_storage_t *storage, u8 fno, u8 page, u16 offset, u32 len, void *buf);
239
int sd_storage_get_fmodes(sdmmc_storage_t *storage, u8 *buf, sd_func_modes_t *functions);
240
int sd_storage_get_scr(sdmmc_storage_t *storage);
241
int sd_storage_get_ssr(sdmmc_storage_t *storage);
242
u32 sd_storage_get_ssr_au(sdmmc_storage_t *storage);
243
244
void sd_storage_get_ext_regs(sdmmc_storage_t *storage, u8 *buf);
245
int sd_storage_parse_perf_enhance(sdmmc_storage_t *storage, u8 fno, u8 page, u16 offset, u8 *buf);
246
247
#endif
248
249