Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/storage/sdmmc.c
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
#include <string.h>
19
20
#include <mem/heap.h>
21
#include <mem/mc.h>
22
#include <soc/timer.h>
23
#include <storage/emmc.h>
24
#include <storage/sdmmc.h>
25
#include <storage/mmc.h>
26
#include <storage/sd.h>
27
#include <storage/sd_def.h>
28
#include <memory_map.h>
29
#include <gfx_utils.h>
30
31
//#define DPRINTF(...) gfx_printf(__VA_ARGS__)
32
#define DPRINTF(...)
33
34
//#define SDMMC_DEBUG_PRINT_SD_REGS
35
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
36
#define DREGPRINTF(...) gfx_printf(__VA_ARGS__)
37
#else
38
#define DREGPRINTF(...)
39
#endif
40
41
#ifdef BDK_SDMMC_EXTRA_PRINT
42
#define ERROR_EXTRA_PRINTING
43
#endif
44
45
u32 sd_power_cycle_time_start;
46
47
static inline u32 unstuff_bits(const u32 *resp, u32 start, u32 size)
48
{
49
start %= 128;
50
51
const u32 mask = (size < 32 ? 1 << size : 0) - 1;
52
const u32 off = 3 - ((start) / 32);
53
const u32 shft = (start) & 31;
54
55
u32 res = resp[off] >> shft;
56
if (size + shft > 32)
57
res |= resp[off - 1] << ((32 - shft) % 32);
58
return res & mask;
59
}
60
61
/*
62
* Common functions for SD and MMC.
63
*/
64
65
static int _sdmmc_storage_check_card_status(u32 res)
66
{
67
//Error mask:
68
//!WARN: R1_SWITCH_ERROR is reserved on SD. The card isn't supposed to use it.
69
if (res &
70
(R1_OUT_OF_RANGE | R1_ADDRESS_ERROR | R1_BLOCK_LEN_ERROR |
71
R1_ERASE_SEQ_ERROR | R1_ERASE_PARAM | R1_WP_VIOLATION |
72
R1_LOCK_UNLOCK_FAILED | R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND |
73
R1_CARD_ECC_FAILED | R1_CC_ERROR | R1_ERROR |
74
R1_CID_CSD_OVERWRITE | R1_WP_ERASE_SKIP | R1_ERASE_RESET |
75
R1_SWITCH_ERROR))
76
return 0;
77
78
// No errors.
79
return 1;
80
}
81
82
static int _sdmmc_storage_execute_cmd_type1_ex(sdmmc_storage_t *storage, u32 *resp, u32 cmd, u32 arg, u32 check_busy, u32 expected_state, u32 mask)
83
{
84
sdmmc_cmd_t cmdbuf;
85
sdmmc_init_cmd(&cmdbuf, cmd, arg, SDMMC_RSP_TYPE_1, check_busy);
86
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL))
87
return 0;
88
89
sdmmc_get_cached_rsp(storage->sdmmc, resp, SDMMC_RSP_TYPE_1);
90
if (mask)
91
*resp &= ~mask;
92
93
if (_sdmmc_storage_check_card_status(*resp))
94
if (expected_state == R1_SKIP_STATE_CHECK || R1_CURRENT_STATE(*resp) == expected_state)
95
return 1;
96
97
return 0;
98
}
99
100
static int _sdmmc_storage_execute_cmd_type1(sdmmc_storage_t *storage, u32 cmd, u32 arg, u32 check_busy, u32 expected_state)
101
{
102
u32 tmp;
103
return _sdmmc_storage_execute_cmd_type1_ex(storage, &tmp, cmd, arg, check_busy, expected_state, 0);
104
}
105
106
static int _sdmmc_storage_go_idle_state(sdmmc_storage_t *storage)
107
{
108
sdmmc_cmd_t cmdbuf;
109
sdmmc_init_cmd(&cmdbuf, MMC_GO_IDLE_STATE, 0, SDMMC_RSP_TYPE_0, 0);
110
111
return sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL);
112
}
113
114
static int _sdmmc_storage_get_cid(sdmmc_storage_t *storage)
115
{
116
sdmmc_cmd_t cmdbuf;
117
sdmmc_init_cmd(&cmdbuf, MMC_ALL_SEND_CID, 0, SDMMC_RSP_TYPE_2, 0);
118
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL))
119
return 0;
120
121
sdmmc_get_cached_rsp(storage->sdmmc, (u32 *)storage->raw_cid, SDMMC_RSP_TYPE_2);
122
123
return 1;
124
}
125
126
static int _sdmmc_storage_select_card(sdmmc_storage_t *storage)
127
{
128
return _sdmmc_storage_execute_cmd_type1(storage, MMC_SELECT_CARD, storage->rca << 16, 1, R1_SKIP_STATE_CHECK);
129
}
130
131
static int _sdmmc_storage_get_csd(sdmmc_storage_t *storage)
132
{
133
sdmmc_cmd_t cmdbuf;
134
sdmmc_init_cmd(&cmdbuf, MMC_SEND_CSD, storage->rca << 16, SDMMC_RSP_TYPE_2, 0);
135
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL))
136
return 0;
137
138
sdmmc_get_cached_rsp(storage->sdmmc, (u32 *)storage->raw_csd, SDMMC_RSP_TYPE_2);
139
140
return 1;
141
}
142
143
static int _sdmmc_storage_set_blocklen(sdmmc_storage_t *storage, u32 blocklen)
144
{
145
return _sdmmc_storage_execute_cmd_type1(storage, MMC_SET_BLOCKLEN, blocklen, 0, R1_STATE_TRAN);
146
}
147
148
static int _sdmmc_storage_get_status(sdmmc_storage_t *storage, u32 *resp, u32 mask)
149
{
150
return _sdmmc_storage_execute_cmd_type1_ex(storage, resp, MMC_SEND_STATUS, storage->rca << 16, 0, R1_STATE_TRAN, mask);
151
}
152
153
static int _sdmmc_storage_check_status(sdmmc_storage_t *storage)
154
{
155
u32 tmp;
156
return _sdmmc_storage_get_status(storage, &tmp, 0);
157
}
158
159
int sdmmc_storage_execute_vendor_cmd(sdmmc_storage_t *storage, u32 arg)
160
{
161
sdmmc_cmd_t cmdbuf;
162
sdmmc_init_cmd(&cmdbuf, MMC_VENDOR_62_CMD, arg, SDMMC_RSP_TYPE_1, 1);
163
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, 0, 0))
164
return 0;
165
166
u32 resp;
167
sdmmc_get_cached_rsp(storage->sdmmc, &resp, SDMMC_RSP_TYPE_1);
168
169
resp = -1;
170
u32 timeout = get_tmr_ms() + 1500;
171
while (true)
172
{
173
_sdmmc_storage_get_status(storage, &resp, 0);
174
175
if (resp == (R1_READY_FOR_DATA | R1_STATE(R1_STATE_TRAN)))
176
break;
177
178
if (get_tmr_ms() > timeout)
179
break;
180
msleep(10);
181
}
182
183
return _sdmmc_storage_check_card_status(resp);
184
}
185
186
int sdmmc_storage_vendor_sandisk_report(sdmmc_storage_t *storage, void *buf)
187
{
188
// Request health report.
189
if (!sdmmc_storage_execute_vendor_cmd(storage, MMC_SANDISK_HEALTH_REPORT))
190
return 2;
191
192
u32 tmp = 0;
193
sdmmc_cmd_t cmdbuf;
194
sdmmc_req_t reqbuf;
195
196
sdmmc_init_cmd(&cmdbuf, MMC_VENDOR_63_CMD, 0, SDMMC_RSP_TYPE_1, 0); // similar to CMD17 with arg 0x0.
197
198
reqbuf.buf = buf;
199
reqbuf.num_sectors = 1;
200
reqbuf.blksize = SDMMC_DAT_BLOCKSIZE;
201
reqbuf.is_write = 0;
202
reqbuf.is_multi_block = 0;
203
reqbuf.is_auto_stop_trn = 0;
204
205
u32 blkcnt_out;
206
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, &blkcnt_out))
207
{
208
sdmmc_stop_transmission(storage->sdmmc, &tmp);
209
_sdmmc_storage_get_status(storage, &tmp, 0);
210
211
return 0;
212
}
213
214
return 1;
215
}
216
217
static int _sdmmc_storage_readwrite_ex(sdmmc_storage_t *storage, u32 *blkcnt_out, u32 sector, u32 num_sectors, void *buf, u32 is_write)
218
{
219
u32 tmp = 0;
220
sdmmc_cmd_t cmdbuf;
221
sdmmc_req_t reqbuf;
222
223
// If SDSC convert block address to byte address.
224
if (!storage->has_sector_access)
225
sector <<= 9;
226
227
sdmmc_init_cmd(&cmdbuf, is_write ? MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK, sector, SDMMC_RSP_TYPE_1, 0);
228
229
reqbuf.buf = buf;
230
reqbuf.num_sectors = num_sectors;
231
reqbuf.blksize = SDMMC_DAT_BLOCKSIZE;
232
reqbuf.is_write = is_write;
233
reqbuf.is_multi_block = 1;
234
reqbuf.is_auto_stop_trn = 1;
235
236
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, blkcnt_out))
237
{
238
sdmmc_stop_transmission(storage->sdmmc, &tmp);
239
_sdmmc_storage_get_status(storage, &tmp, 0);
240
241
return 0;
242
}
243
244
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
245
if (!_sdmmc_storage_check_card_status(tmp))
246
return 0;
247
248
return 1;
249
}
250
251
int sdmmc_storage_end(sdmmc_storage_t *storage)
252
{
253
DPRINTF("[SDMMC%d] end\n", storage->sdmmc->id);
254
255
if (!_sdmmc_storage_go_idle_state(storage))
256
return 0;
257
258
sdmmc_end(storage->sdmmc);
259
260
storage->initialized = 0;
261
262
return 1;
263
}
264
265
static int _sdmmc_storage_handle_io_error(sdmmc_storage_t *storage, bool first_reinit)
266
{
267
int res = 0;
268
269
if (storage->sdmmc->id == SDMMC_1 || storage->sdmmc->id == SDMMC_4)
270
{
271
if (storage->sdmmc->id == SDMMC_1)
272
{
273
sd_error_count_increment(SD_ERROR_RW_FAIL);
274
275
if (first_reinit)
276
res = sd_initialize(true);
277
else
278
{
279
res = sd_init_retry(true);
280
if (!res)
281
sd_error_count_increment(SD_ERROR_INIT_FAIL);
282
}
283
}
284
else if (storage->sdmmc->id == SDMMC_4)
285
{
286
emmc_error_count_increment(EMMC_ERROR_RW_FAIL);
287
288
if (first_reinit)
289
res = emmc_initialize(true);
290
else
291
{
292
res = emmc_init_retry(true);
293
if (!res)
294
emmc_error_count_increment(EMMC_ERROR_INIT_FAIL);
295
}
296
}
297
}
298
299
return res;
300
}
301
302
static int _sdmmc_storage_readwrite(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf, u32 is_write)
303
{
304
u8 *bbuf = (u8 *)buf;
305
u32 sct_off = sector;
306
u32 sct_total = num_sectors;
307
bool first_reinit = true;
308
309
// Exit if not initialized.
310
if (!storage->initialized)
311
return 0;
312
313
// Check if out of bounds.
314
if (((u64)sector + num_sectors) > storage->sec_cnt)
315
{
316
#ifdef ERROR_EXTRA_PRINTING
317
EPRINTFARGS("SDMMC%d: Out of bounds!", storage->sdmmc->id + 1);
318
#endif
319
return 0;
320
}
321
322
while (sct_total)
323
{
324
u32 blkcnt = 0;
325
// Retry 5 times if failed.
326
u32 retries = 5;
327
do
328
{
329
reinit_try:
330
if (_sdmmc_storage_readwrite_ex(storage, &blkcnt, sct_off, MIN(sct_total, 0xFFFF), bbuf, is_write))
331
goto out;
332
else
333
retries--;
334
335
sd_error_count_increment(SD_ERROR_RW_RETRY);
336
337
msleep(50);
338
} while (retries);
339
340
// Disk IO failure! Reinit SD/EMMC to a lower speed.
341
if (_sdmmc_storage_handle_io_error(storage, first_reinit))
342
{
343
// Reset values for a retry.
344
blkcnt = 0;
345
retries = 3;
346
first_reinit = false;
347
348
bbuf = (u8 *)buf;
349
sct_off = sector;
350
sct_total = num_sectors;
351
352
goto reinit_try;
353
}
354
355
// Failed.
356
return 0;
357
358
out:
359
sct_off += blkcnt;
360
sct_total -= blkcnt;
361
bbuf += SDMMC_DAT_BLOCKSIZE * blkcnt;
362
}
363
364
return 1;
365
}
366
367
int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf)
368
{
369
// Ensure that SDMMC has access to buffer and it's SDMMC DMA aligned.
370
if (mc_client_has_access(buf) && !((u32)buf % 8))
371
return _sdmmc_storage_readwrite(storage, sector, num_sectors, buf, 0);
372
373
if (num_sectors > (SDMMC_UP_BUF_SZ / SDMMC_DAT_BLOCKSIZE))
374
return 0;
375
376
u8 *tmp_buf = (u8 *)SDMMC_UPPER_BUFFER;
377
if (_sdmmc_storage_readwrite(storage, sector, num_sectors, tmp_buf, 0))
378
{
379
memcpy(buf, tmp_buf, SDMMC_DAT_BLOCKSIZE * num_sectors);
380
return 1;
381
}
382
return 0;
383
}
384
385
int sdmmc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf)
386
{
387
// Ensure that SDMMC has access to buffer and it's SDMMC DMA aligned.
388
if (mc_client_has_access(buf) && !((u32)buf % 8))
389
return _sdmmc_storage_readwrite(storage, sector, num_sectors, buf, 1);
390
391
if (num_sectors > (SDMMC_UP_BUF_SZ / SDMMC_DAT_BLOCKSIZE))
392
return 0;
393
394
u8 *tmp_buf = (u8 *)SDMMC_UPPER_BUFFER;
395
memcpy(tmp_buf, buf, SDMMC_DAT_BLOCKSIZE * num_sectors);
396
return _sdmmc_storage_readwrite(storage, sector, num_sectors, tmp_buf, 1);
397
}
398
399
/*
400
* MMC specific functions.
401
*/
402
403
static int _mmc_storage_get_op_cond_inner(sdmmc_storage_t *storage, u32 *pout, u32 power)
404
{
405
sdmmc_cmd_t cmdbuf;
406
407
u32 arg = 0;
408
switch (power)
409
{
410
case SDMMC_POWER_1_8:
411
arg = MMC_CARD_CCS | MMC_CARD_VDD_18;
412
break;
413
414
case SDMMC_POWER_3_3:
415
arg = MMC_CARD_CCS | MMC_CARD_VDD_27_34;
416
break;
417
418
default:
419
return 0;
420
}
421
422
sdmmc_init_cmd(&cmdbuf, MMC_SEND_OP_COND, arg, SDMMC_RSP_TYPE_3, 0);
423
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL))
424
return 0;
425
426
return sdmmc_get_cached_rsp(storage->sdmmc, pout, SDMMC_RSP_TYPE_3);
427
}
428
429
static int _mmc_storage_get_op_cond(sdmmc_storage_t *storage, u32 power)
430
{
431
u32 timeout = get_tmr_ms() + 1500;
432
433
while (true)
434
{
435
u32 cond = 0;
436
if (!_mmc_storage_get_op_cond_inner(storage, &cond, power))
437
break;
438
439
// Check if power up is done.
440
if (cond & MMC_CARD_BUSY)
441
{
442
// Check if card is high capacity.
443
if (cond & MMC_CARD_CCS)
444
storage->has_sector_access = 1;
445
446
return 1;
447
}
448
if (get_tmr_ms() > timeout)
449
break;
450
451
usleep(1000);
452
}
453
454
return 0;
455
}
456
457
static int _mmc_storage_set_relative_addr(sdmmc_storage_t *storage)
458
{
459
return _sdmmc_storage_execute_cmd_type1(storage, MMC_SET_RELATIVE_ADDR, storage->rca << 16, 0, R1_SKIP_STATE_CHECK);
460
}
461
462
static void _mmc_storage_parse_cid(sdmmc_storage_t *storage)
463
{
464
u32 *raw_cid = (u32 *)&(storage->raw_cid);
465
466
switch (storage->csd.mmca_vsn)
467
{
468
case 0: /* MMC v1.0 - v1.2 */
469
case 1: /* MMC v1.4 */
470
storage->cid.prod_name[6] = unstuff_bits(raw_cid, 48, 8);
471
storage->cid.manfid = unstuff_bits(raw_cid, 104, 24);
472
storage->cid.hwrev = unstuff_bits(raw_cid, 44, 4);
473
storage->cid.fwrev = unstuff_bits(raw_cid, 40, 4);
474
storage->cid.serial = unstuff_bits(raw_cid, 16, 24);
475
break;
476
477
case 2: /* MMC v2.0 - v2.2 */
478
case 3: /* MMC v3.1 - v3.3 */
479
case 4: /* MMC v4 */
480
storage->cid.manfid = unstuff_bits(raw_cid, 120, 8);
481
storage->cid.oemid = unstuff_bits(raw_cid, 104, 8);
482
storage->cid.prv = unstuff_bits(raw_cid, 48, 8);
483
storage->cid.serial = unstuff_bits(raw_cid, 16, 32);
484
break;
485
486
default:
487
break;
488
}
489
490
storage->cid.prod_name[0] = unstuff_bits(raw_cid, 96, 8);
491
storage->cid.prod_name[1] = unstuff_bits(raw_cid, 88, 8);
492
storage->cid.prod_name[2] = unstuff_bits(raw_cid, 80, 8);
493
storage->cid.prod_name[3] = unstuff_bits(raw_cid, 72, 8);
494
storage->cid.prod_name[4] = unstuff_bits(raw_cid, 64, 8);
495
storage->cid.prod_name[5] = unstuff_bits(raw_cid, 56, 8);
496
497
storage->cid.month = unstuff_bits(raw_cid, 12, 4);
498
storage->cid.year = unstuff_bits(raw_cid, 8, 4) + 1997;
499
if (storage->ext_csd.rev >= 5)
500
{
501
if (storage->cid.year < 2010)
502
storage->cid.year += 16;
503
}
504
}
505
506
static void _mmc_storage_parse_csd(sdmmc_storage_t *storage)
507
{
508
u32 *raw_csd = (u32 *)storage->raw_csd;
509
510
storage->csd.mmca_vsn = unstuff_bits(raw_csd, 122, 4);
511
storage->csd.structure = unstuff_bits(raw_csd, 126, 2);
512
storage->csd.cmdclass = unstuff_bits(raw_csd, 84, 12);
513
storage->csd.read_blkbits = unstuff_bits(raw_csd, 80, 4);
514
storage->csd.capacity = (1 + unstuff_bits(raw_csd, 62, 12)) << (unstuff_bits(raw_csd, 47, 3) + 2);
515
storage->sec_cnt = storage->csd.capacity;
516
}
517
518
static void _mmc_storage_parse_ext_csd(sdmmc_storage_t *storage, u8 *buf)
519
{
520
storage->ext_csd.rev = buf[EXT_CSD_REV];
521
storage->ext_csd.ext_struct = buf[EXT_CSD_STRUCTURE];
522
storage->ext_csd.card_type = buf[EXT_CSD_CARD_TYPE];
523
storage->ext_csd.dev_version = *(u16 *)&buf[EXT_CSD_DEVICE_VERSION];
524
storage->ext_csd.boot_mult = buf[EXT_CSD_BOOT_MULT];
525
storage->ext_csd.rpmb_mult = buf[EXT_CSD_RPMB_MULT];
526
//storage->ext_csd.bkops = buf[EXT_CSD_BKOPS_SUPPORT];
527
//storage->ext_csd.bkops_en = buf[EXT_CSD_BKOPS_EN];
528
//storage->ext_csd.bkops_status = buf[EXT_CSD_BKOPS_STATUS];
529
530
storage->ext_csd.pre_eol_info = buf[EXT_CSD_PRE_EOL_INFO];
531
storage->ext_csd.dev_life_est_a = buf[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
532
storage->ext_csd.dev_life_est_b = buf[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
533
534
storage->ext_csd.cache_size = buf[EXT_CSD_CACHE_SIZE] |
535
(buf[EXT_CSD_CACHE_SIZE + 1] << 8) |
536
(buf[EXT_CSD_CACHE_SIZE + 2] << 16) |
537
(buf[EXT_CSD_CACHE_SIZE + 3] << 24);
538
539
storage->ext_csd.max_enh_mult = (buf[EXT_CSD_MAX_ENH_SIZE_MULT] |
540
(buf[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) |
541
(buf[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16)) *
542
buf[EXT_CSD_HC_WP_GRP_SIZE] * buf[EXT_CSD_HC_ERASE_GRP_SIZE];
543
544
storage->sec_cnt = *(u32 *)&buf[EXT_CSD_SEC_CNT];
545
}
546
547
int mmc_storage_get_ext_csd(sdmmc_storage_t *storage, void *buf)
548
{
549
sdmmc_cmd_t cmdbuf;
550
sdmmc_init_cmd(&cmdbuf, MMC_SEND_EXT_CSD, 0, SDMMC_RSP_TYPE_1, 0);
551
552
sdmmc_req_t reqbuf;
553
reqbuf.buf = buf;
554
reqbuf.blksize = SDMMC_DAT_BLOCKSIZE;
555
reqbuf.num_sectors = 1;
556
reqbuf.is_write = 0;
557
reqbuf.is_multi_block = 0;
558
reqbuf.is_auto_stop_trn = 0;
559
560
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, NULL))
561
return 0;
562
563
u32 tmp = 0;
564
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
565
_mmc_storage_parse_ext_csd(storage, buf);
566
567
return _sdmmc_storage_check_card_status(tmp);
568
}
569
570
int sd_storage_get_ext_reg(sdmmc_storage_t *storage, u8 fno, u8 page, u16 address, u32 len, void *buf)
571
{
572
if (!(storage->scr.cmds & BIT(2)))
573
return 0;
574
575
sdmmc_cmd_t cmdbuf;
576
577
u32 arg = fno << 27 | page << 18 | address << 9 | (len - 1);
578
579
sdmmc_init_cmd(&cmdbuf, SD_READ_EXTR_SINGLE, arg, SDMMC_RSP_TYPE_1, 0);
580
581
sdmmc_req_t reqbuf;
582
reqbuf.buf = buf;
583
reqbuf.blksize = SDMMC_DAT_BLOCKSIZE;
584
reqbuf.num_sectors = 1;
585
reqbuf.is_write = 0;
586
reqbuf.is_multi_block = 0;
587
reqbuf.is_auto_stop_trn = 0;
588
589
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, NULL))
590
return 0;
591
592
u32 tmp = 0;
593
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
594
595
return _sdmmc_storage_check_card_status(tmp);
596
}
597
598
static int _mmc_storage_switch(sdmmc_storage_t *storage, u32 arg)
599
{
600
return _sdmmc_storage_execute_cmd_type1(storage, MMC_SWITCH, arg, 1, R1_SKIP_STATE_CHECK);
601
}
602
603
static int _mmc_storage_switch_buswidth(sdmmc_storage_t *storage, u32 bus_width)
604
{
605
if (bus_width == SDMMC_BUS_WIDTH_1)
606
return 1;
607
608
u32 arg = 0;
609
switch (bus_width)
610
{
611
case SDMMC_BUS_WIDTH_4:
612
arg = SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
613
break;
614
615
case SDMMC_BUS_WIDTH_8:
616
arg = SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_8);
617
break;
618
}
619
620
if (_mmc_storage_switch(storage, arg))
621
if (_sdmmc_storage_check_status(storage))
622
{
623
sdmmc_set_bus_width(storage->sdmmc, bus_width);
624
625
return 1;
626
}
627
628
return 0;
629
}
630
631
static int _mmc_storage_enable_HS(sdmmc_storage_t *storage, bool check_sts_before_clk_setup)
632
{
633
if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS)))
634
return 0;
635
636
if (check_sts_before_clk_setup && !_sdmmc_storage_check_status(storage))
637
return 0;
638
639
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_HS52))
640
return 0;
641
642
DPRINTF("[MMC] switched to HS52\n");
643
storage->csd.busspeed = 52;
644
645
if (check_sts_before_clk_setup || _sdmmc_storage_check_status(storage))
646
return 1;
647
648
return 0;
649
}
650
651
static int _mmc_storage_enable_HS200(sdmmc_storage_t *storage)
652
{
653
if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200)))
654
return 0;
655
656
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_HS200))
657
return 0;
658
659
if (!sdmmc_tuning_execute(storage->sdmmc, SDHCI_TIMING_MMC_HS200, MMC_SEND_TUNING_BLOCK_HS200))
660
return 0;
661
662
DPRINTF("[MMC] switched to HS200\n");
663
storage->csd.busspeed = 200;
664
665
return _sdmmc_storage_check_status(storage);
666
}
667
668
static int _mmc_storage_enable_HS400(sdmmc_storage_t *storage)
669
{
670
if (!_mmc_storage_enable_HS200(storage))
671
return 0;
672
673
sdmmc_save_tap_value(storage->sdmmc);
674
675
if (!_mmc_storage_enable_HS(storage, false))
676
return 0;
677
678
if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_BUS_WIDTH, EXT_CSD_DDR_BUS_WIDTH_8)))
679
return 0;
680
681
if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400)))
682
return 0;
683
684
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_HS400))
685
return 0;
686
687
DPRINTF("[MMC] switched to HS400\n");
688
storage->csd.busspeed = 400;
689
690
return _sdmmc_storage_check_status(storage);
691
}
692
693
static int _mmc_storage_enable_highspeed(sdmmc_storage_t *storage, u32 card_type, u32 type)
694
{
695
if (sdmmc_get_io_power(storage->sdmmc) != SDMMC_POWER_1_8)
696
goto hs52_mode;
697
698
// HS400 needs 8-bit bus width mode.
699
if (sdmmc_get_bus_width(storage->sdmmc) == SDMMC_BUS_WIDTH_8 &&
700
card_type & EXT_CSD_CARD_TYPE_HS400_1_8V && type == SDHCI_TIMING_MMC_HS400)
701
return _mmc_storage_enable_HS400(storage);
702
703
// Try HS200 if HS400 and 4-bit width bus or just HS200.
704
if ((sdmmc_get_bus_width(storage->sdmmc) == SDMMC_BUS_WIDTH_8 ||
705
sdmmc_get_bus_width(storage->sdmmc) == SDMMC_BUS_WIDTH_4) &&
706
card_type & EXT_CSD_CARD_TYPE_HS200_1_8V &&
707
(type == SDHCI_TIMING_MMC_HS400 || type == SDHCI_TIMING_MMC_HS200))
708
return _mmc_storage_enable_HS200(storage);
709
710
hs52_mode:
711
if (card_type & EXT_CSD_CARD_TYPE_HS_52)
712
return _mmc_storage_enable_HS(storage, true);
713
714
return 1;
715
}
716
717
/*
718
static int _mmc_storage_enable_auto_bkops(sdmmc_storage_t *storage)
719
{
720
if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_SET_BITS, EXT_CSD_BKOPS_EN, EXT_CSD_AUTO_BKOPS_MASK)))
721
return 0;
722
723
return _sdmmc_storage_check_status(storage);
724
}
725
*/
726
727
int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type)
728
{
729
memset(storage, 0, sizeof(sdmmc_storage_t));
730
storage->sdmmc = sdmmc;
731
storage->rca = 2; // Set default device address. This could be a config item.
732
733
DPRINTF("[MMC]-[init: bus: %d, type: %d]\n", bus_width, type);
734
735
if (!sdmmc_init(sdmmc, SDMMC_4, SDMMC_POWER_1_8, SDMMC_BUS_WIDTH_1, SDHCI_TIMING_MMC_ID))
736
return 0;
737
DPRINTF("[MMC] after init\n");
738
739
// Wait 1ms + 74 cycles.
740
usleep(1000 + (74 * 1000 + sdmmc->card_clock - 1) / sdmmc->card_clock);
741
742
if (!_sdmmc_storage_go_idle_state(storage))
743
return 0;
744
DPRINTF("[MMC] went to idle state\n");
745
746
if (!_mmc_storage_get_op_cond(storage, SDMMC_POWER_1_8))
747
return 0;
748
DPRINTF("[MMC] got op cond\n");
749
750
if (!_sdmmc_storage_get_cid(storage))
751
return 0;
752
DPRINTF("[MMC] got cid\n");
753
754
if (!_mmc_storage_set_relative_addr(storage))
755
return 0;
756
DPRINTF("[MMC] set relative addr\n");
757
758
if (!_sdmmc_storage_get_csd(storage))
759
return 0;
760
DPRINTF("[MMC] got csd\n");
761
_mmc_storage_parse_csd(storage);
762
763
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_LS26))
764
return 0;
765
DPRINTF("[MMC] after setup clock\n");
766
767
if (!_sdmmc_storage_select_card(storage))
768
return 0;
769
DPRINTF("[MMC] card selected\n");
770
771
if (!_sdmmc_storage_set_blocklen(storage, EMMC_BLOCKSIZE))
772
return 0;
773
DPRINTF("[MMC] set blocklen to EMMC_BLOCKSIZE\n");
774
775
// Check system specification version, only version 4.0 and later support below features.
776
if (storage->csd.mmca_vsn < CSD_SPEC_VER_4)
777
goto done;
778
779
if (!_mmc_storage_switch_buswidth(storage, bus_width))
780
return 0;
781
DPRINTF("[MMC] switched buswidth\n");
782
783
if (!mmc_storage_get_ext_csd(storage, (u8 *)SDMMC_UPPER_BUFFER))
784
return 0;
785
DPRINTF("[MMC] got ext_csd\n");
786
787
_mmc_storage_parse_cid(storage); // This needs to be after csd and ext_csd.
788
789
/*
790
if (storage->ext_csd.bkops & 0x1 && !(storage->ext_csd.bkops_en & EXT_CSD_AUTO_BKOPS_MASK))
791
{
792
_mmc_storage_enable_auto_bkops(storage);
793
DPRINTF("[MMC] BKOPS enabled\n");
794
}
795
*/
796
797
if (!_mmc_storage_enable_highspeed(storage, storage->ext_csd.card_type, type))
798
return 0;
799
DPRINTF("[MMC] successfully switched to HS mode\n");
800
801
sdmmc_card_clock_powersave(storage->sdmmc, SDMMC_POWER_SAVE_ENABLE);
802
803
done:
804
storage->initialized = 1;
805
806
return 1;
807
}
808
809
int sdmmc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition)
810
{
811
if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_PART_CONFIG, partition)))
812
return 0;
813
814
if (!_sdmmc_storage_check_status(storage))
815
return 0;
816
817
storage->partition = partition;
818
819
return 1;
820
}
821
822
/*
823
* SD specific functions.
824
*/
825
826
static int _sd_storage_execute_app_cmd(sdmmc_storage_t *storage, u32 expected_state, u32 mask, sdmmc_cmd_t *cmdbuf, sdmmc_req_t *req, u32 *blkcnt_out)
827
{
828
u32 tmp;
829
if (!_sdmmc_storage_execute_cmd_type1_ex(storage, &tmp, MMC_APP_CMD, storage->rca << 16, 0, expected_state, mask))
830
return 0;
831
832
return sdmmc_execute_cmd(storage->sdmmc, cmdbuf, req, blkcnt_out);
833
}
834
835
static int _sd_storage_execute_app_cmd_type1(sdmmc_storage_t *storage, u32 *resp, u32 cmd, u32 arg, u32 check_busy, u32 expected_state)
836
{
837
if (!_sdmmc_storage_execute_cmd_type1(storage, MMC_APP_CMD, storage->rca << 16, 0, R1_STATE_TRAN))
838
return 0;
839
840
return _sdmmc_storage_execute_cmd_type1_ex(storage, resp, cmd, arg, check_busy, expected_state, 0);
841
}
842
843
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
844
void _sd_storage_debug_print_cid(const u32 *raw_cid)
845
{
846
gfx_printf("Card Identification\n");
847
848
gfx_printf("MID: %02X\n", unstuff_bits(raw_cid, 120, 8));
849
gfx_printf("OID %04X\n", unstuff_bits(raw_cid, 104, 16));
850
gfx_printf("PNM: %02X %02X %02X %02X %02X\n",
851
unstuff_bits(raw_cid, 96, 8), unstuff_bits(raw_cid, 88, 8),
852
unstuff_bits(raw_cid, 80, 8), unstuff_bits(raw_cid, 72, 8),
853
unstuff_bits(raw_cid, 64, 8));
854
gfx_printf("PRV: %02X\n", unstuff_bits(raw_cid, 56, 8));
855
gfx_printf("PSN: %08X\n", unstuff_bits(raw_cid, 24, 32));
856
gfx_printf("MDT: %03X\n", unstuff_bits(raw_cid, 8, 12));
857
gfx_printf("--RSVD-- %X\n", unstuff_bits(raw_cid, 20, 4));
858
}
859
860
void _sd_storage_debug_print_csd(const u32 *raw_csd)
861
{
862
gfx_printf("\n");
863
864
gfx_printf("\nCSD_STRUCTURE: %X\n", unstuff_bits(raw_csd, 126, 2));
865
gfx_printf("TAAC: %02X\n", unstuff_bits(raw_csd, 112, 8));
866
gfx_printf("NSAC: %02X\n", unstuff_bits(raw_csd, 104, 8));
867
gfx_printf("TRAN_SPEED: %02X\n", unstuff_bits(raw_csd, 96, 8));
868
gfx_printf("CCC: %03X\n", unstuff_bits(raw_csd, 84, 12));
869
gfx_printf("READ_BL_LEN: %X\n", unstuff_bits(raw_csd, 80, 4));
870
gfx_printf("READ_BL_PARTIAL: %X\n", unstuff_bits(raw_csd, 79, 1));
871
gfx_printf("WRITE_BLK_MISALIGN: %X\n", unstuff_bits(raw_csd, 78, 1));
872
gfx_printf("READ_BLK_MISALIGN: %X\n", unstuff_bits(raw_csd, 77, 1));
873
gfx_printf("DSR_IMP: %X\n", unstuff_bits(raw_csd, 76, 1));
874
gfx_printf("C_SIZE: %06X\n", unstuff_bits(raw_csd, 48, 28)); // CSD 3 (SDUC).
875
876
gfx_printf("ERASE_BLK_LEN: %X\n", unstuff_bits(raw_csd, 46, 1));
877
gfx_printf("SECTOR_SIZE: %02X\n", unstuff_bits(raw_csd, 39, 6));
878
gfx_printf("WP_GRP_SIZE: %02X\n", unstuff_bits(raw_csd, 32, 6));
879
gfx_printf("WP_GRP_ENABLE: %X\n", unstuff_bits(raw_csd, 31, 1));
880
881
gfx_printf("R2W_FACTOR: %X\n", unstuff_bits(raw_csd, 26, 3));
882
gfx_printf("WRITE_BL_LEN: %X\n", unstuff_bits(raw_csd, 22, 4));
883
gfx_printf("WRITE_BL_PARTIAL: %X\n", unstuff_bits(raw_csd, 21, 1));
884
885
gfx_printf("FILE_FORMAT_GRP: %X\n", unstuff_bits(raw_csd, 15, 1));
886
gfx_printf("COPY: %X\n", unstuff_bits(raw_csd, 14, 1));
887
gfx_printf("PERM_WRITE_PROTECT: %X\n", unstuff_bits(raw_csd, 13, 1));
888
gfx_printf("TMP_WRITE_PROTECT: %X\n", unstuff_bits(raw_csd, 12, 1));
889
gfx_printf("FILE_FORMAT: %X\n", unstuff_bits(raw_csd, 10, 2));
890
891
gfx_printf("--RSVD-- %02X %X %X %02X %X\n",
892
unstuff_bits(raw_csd, 120, 6),
893
unstuff_bits(raw_csd, 47, 1), unstuff_bits(raw_csd, 29, 2),
894
unstuff_bits(raw_csd, 16, 5), unstuff_bits(raw_csd, 8, 2));
895
}
896
897
void _sd_storage_debug_print_scr(const u32 *raw_scr)
898
{
899
u32 resp[4];
900
memcpy(&resp[2], raw_scr, 8);
901
902
gfx_printf("\n");
903
904
gfx_printf("SCR_STRUCTURE: %X\n", unstuff_bits(resp, 60, 4));
905
gfx_printf("SD_SPEC: %X\n", unstuff_bits(resp, 56, 4));
906
gfx_printf("DATA_STAT_AFTER_ERASE: %X\n", unstuff_bits(resp, 55, 1));
907
gfx_printf("SD_SECURITY: %X\n", unstuff_bits(resp, 52, 3));
908
gfx_printf("SD_BUS widths: %X\n", unstuff_bits(resp, 48, 4));
909
gfx_printf("SD_SPEC3: %X\n", unstuff_bits(resp, 47, 1));
910
gfx_printf("EX_SECURITY: %X\n", unstuff_bits(resp, 43, 4));
911
gfx_printf("SD_SPEC4: %X\n", unstuff_bits(resp, 42, 1));
912
gfx_printf("SD_SPECX: %X\n", unstuff_bits(resp, 38, 4));
913
gfx_printf("CMD_SUPPORT: %X\n", unstuff_bits(resp, 32, 4));
914
gfx_printf("VENDOR: %08X\n", unstuff_bits(resp, 0, 32));
915
gfx_printf("--RSVD-- %X\n", unstuff_bits(resp, 36, 2));
916
}
917
918
void _sd_storage_debug_print_ssr(const u8 *raw_ssr)
919
{
920
u32 raw_ssr0[4]; // 511:384.
921
u32 raw_ssr1[4]; // 383:256.
922
u32 raw_ssr2[4]; // 255:128.
923
u32 raw_ssr3[4]; // 127:0.
924
memcpy(raw_ssr0, &raw_ssr[0], 16);
925
memcpy(raw_ssr1, &raw_ssr[16], 16);
926
memcpy(raw_ssr2, &raw_ssr[32], 16);
927
memcpy(raw_ssr3, &raw_ssr[48], 16);
928
929
gfx_printf("\nSD Status:\n");
930
931
gfx_printf("DAT_BUS_WIDTH: %X\n", unstuff_bits(raw_ssr0, 510, 2));
932
gfx_printf("SECURED_MODE: %X\n", unstuff_bits(raw_ssr0, 509, 1));
933
gfx_printf("SECURITY_FUNCTIONS: %02X\n", unstuff_bits(raw_ssr0, 502, 6));
934
gfx_printf("SD_CARD_TYPE: %04X\n", unstuff_bits(raw_ssr0, 480, 16));
935
gfx_printf("SZ_OF_PROTECTED_AREA: %08X\n", unstuff_bits(raw_ssr0, 448, 32));
936
gfx_printf("SPEED_CLASS: %02X\n", unstuff_bits(raw_ssr0, 440, 8));
937
gfx_printf("PERFORMANCE_MOVE: %02X\n", unstuff_bits(raw_ssr0, 432, 8));
938
gfx_printf("AU_SIZE: %X\n", unstuff_bits(raw_ssr0, 428, 4));
939
gfx_printf("ERAZE_SIZE: %04X\n", unstuff_bits(raw_ssr0, 408, 16));
940
gfx_printf("ERASE_TIMEOUT: %02X\n", unstuff_bits(raw_ssr0, 402, 6));
941
gfx_printf("ERASE_OFFSET: %X\n", unstuff_bits(raw_ssr0, 400, 2));
942
gfx_printf("UHS_SPEED_GRADE: %X\n", unstuff_bits(raw_ssr0, 396, 4));
943
gfx_printf("UHS_AU_SIZE: %X\n", unstuff_bits(raw_ssr0, 392, 4));
944
gfx_printf("VIDEO_SPEED_CLASS: %02X\n", unstuff_bits(raw_ssr0, 384, 8));
945
946
gfx_printf("VSC_AU_SIZE: %03X\n", unstuff_bits(raw_ssr1, 368, 10));
947
gfx_printf("SUS_ADDR: %06X\n", unstuff_bits(raw_ssr1, 346, 22));
948
gfx_printf("APP_PERF_CLASS: %X\n", unstuff_bits(raw_ssr1, 336, 4));
949
gfx_printf("PERFORMANCE_ENHANCE: %02X\n", unstuff_bits(raw_ssr1, 328, 8));
950
gfx_printf("DISCARD_SUPPORT: %X\n", unstuff_bits(raw_ssr1, 313, 1));
951
gfx_printf("FULE_SUPPORT: %X\n", unstuff_bits(raw_ssr1, 312, 1));
952
953
gfx_printf("--RSVD-- %02X %X %02X %02X %04X\n",
954
unstuff_bits(raw_ssr0, 496, 6), unstuff_bits(raw_ssr0, 424, 4),
955
unstuff_bits(raw_ssr1, 378, 6), unstuff_bits(raw_ssr1, 340, 6),
956
unstuff_bits(raw_ssr1, 314, 14));
957
958
gfx_printf("VENDOR_1: %06X %08X\n",
959
unstuff_bits(raw_ssr1, 288, 24), unstuff_bits(raw_ssr1, 256, 32));
960
961
gfx_printf("VENDOR_2: %08X %08X %08X %08X\n",
962
unstuff_bits(raw_ssr2, 224, 32), unstuff_bits(raw_ssr2, 192, 32),
963
unstuff_bits(raw_ssr2, 160, 32), unstuff_bits(raw_ssr2, 128, 32));
964
gfx_printf("VENDOR_3: %08X %08X %08X %08X\n",
965
unstuff_bits(raw_ssr3, 96 - 0, 32), unstuff_bits(raw_ssr3, 64, 32),
966
unstuff_bits(raw_ssr3, 32 - 0, 32), unstuff_bits(raw_ssr3, 0, 32));
967
}
968
#endif
969
970
static int _sd_storage_send_if_cond(sdmmc_storage_t *storage, bool *is_sdsc)
971
{
972
sdmmc_cmd_t cmdbuf;
973
u16 vhd_pattern = SD_VHS_27_36 | 0xAA;
974
sdmmc_init_cmd(&cmdbuf, SD_SEND_IF_COND, vhd_pattern, SDMMC_RSP_TYPE_5, 0);
975
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL))
976
{
977
// The SD Card is version 1.X (SDSC) if there is no response.
978
if (storage->sdmmc->error_sts == SDHCI_ERR_INT_CMD_TIMEOUT)
979
{
980
*is_sdsc = 1;
981
return 1;
982
}
983
984
return 0;
985
}
986
987
// For Card version >= 2.0, parse results.
988
u32 resp = 0;
989
sdmmc_get_cached_rsp(storage->sdmmc, &resp, SDMMC_RSP_TYPE_5);
990
991
// Check if VHD was accepted and pattern was properly returned.
992
if ((resp & 0xFFF) == vhd_pattern)
993
return 1;
994
995
return 0;
996
}
997
998
static int _sd_storage_get_op_cond_once(sdmmc_storage_t *storage, u32 *cond, bool is_sdsc, int bus_uhs_support)
999
{
1000
sdmmc_cmd_t cmdbuf;
1001
// Support for Current > 150mA.
1002
u32 arg = !is_sdsc ? SD_OCR_XPC : 0;
1003
// Support for handling block-addressed SDHC cards.
1004
arg |= !is_sdsc ? SD_OCR_CCS : 0;
1005
// Support for 1.8V signaling.
1006
arg |= (bus_uhs_support && !is_sdsc) ? SD_OCR_S18R : 0;
1007
// Support for 3.3V power supply (VDD1).
1008
arg |= SD_OCR_VDD_32_33;
1009
1010
sdmmc_init_cmd(&cmdbuf, SD_APP_OP_COND, arg, SDMMC_RSP_TYPE_3, 0);
1011
1012
if (!_sd_storage_execute_app_cmd(storage, R1_SKIP_STATE_CHECK, is_sdsc ? R1_ILLEGAL_COMMAND : 0, &cmdbuf, NULL, NULL))
1013
return 0;
1014
1015
return sdmmc_get_cached_rsp(storage->sdmmc, cond, SDMMC_RSP_TYPE_3);
1016
}
1017
1018
static int _sd_storage_get_op_cond(sdmmc_storage_t *storage, bool is_sdsc, int bus_uhs_support)
1019
{
1020
u32 timeout = get_tmr_ms() + 1500;
1021
1022
while (true)
1023
{
1024
u32 cond = 0;
1025
if (!_sd_storage_get_op_cond_once(storage, &cond, is_sdsc, bus_uhs_support))
1026
break;
1027
1028
// Check if power up is done.
1029
if (cond & SD_OCR_BUSY)
1030
{
1031
DPRINTF("[SD] op cond: %08X, lv: %d\n", cond, bus_uhs_support);
1032
1033
// Check if card is high capacity.
1034
if (cond & SD_OCR_CCS)
1035
storage->has_sector_access = 1;
1036
1037
// Check if card supports 1.8V signaling.
1038
if (cond & SD_ROCR_S18A && bus_uhs_support && !storage->is_low_voltage)
1039
{
1040
// Switch to 1.8V signaling.
1041
if (_sdmmc_storage_execute_cmd_type1(storage, SD_SWITCH_VOLTAGE, 0, 0, R1_STATE_READY))
1042
{
1043
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_UHS_SDR12))
1044
return 0;
1045
1046
if (!sdmmc_enable_low_voltage(storage->sdmmc))
1047
return 0;
1048
1049
storage->is_low_voltage = 1;
1050
1051
DPRINTF("-> switched to low voltage\n");
1052
}
1053
}
1054
else
1055
{
1056
DPRINTF("[SD] no low voltage support\n");
1057
}
1058
1059
return 1;
1060
}
1061
if (get_tmr_ms() > timeout)
1062
break;
1063
msleep(10); // Needs to be at least 10ms for some SD Cards
1064
}
1065
1066
return 0;
1067
}
1068
1069
static int _sd_storage_get_rca(sdmmc_storage_t *storage)
1070
{
1071
sdmmc_cmd_t cmdbuf;
1072
sdmmc_init_cmd(&cmdbuf, SD_SEND_RELATIVE_ADDR, 0, SDMMC_RSP_TYPE_4, 0);
1073
1074
u32 timeout = get_tmr_ms() + 1500;
1075
1076
while (true)
1077
{
1078
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, NULL, NULL))
1079
break;
1080
1081
u32 resp = 0;
1082
if (!sdmmc_get_cached_rsp(storage->sdmmc, &resp, SDMMC_RSP_TYPE_4))
1083
break;
1084
1085
if (resp >> 16)
1086
{
1087
storage->rca = resp >> 16;
1088
return 1;
1089
}
1090
1091
if (get_tmr_ms() > timeout)
1092
break;
1093
usleep(1000);
1094
}
1095
1096
return 0;
1097
}
1098
1099
static void _sd_storage_parse_scr(sdmmc_storage_t *storage)
1100
{
1101
// unstuff_bits can parse only 4 u32
1102
u32 resp[4];
1103
1104
memcpy(&resp[2], storage->raw_scr, 8);
1105
1106
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
1107
_sd_storage_debug_print_scr((u32 *)storage->raw_scr);
1108
#endif
1109
1110
storage->scr.sda_vsn = unstuff_bits(resp, 56, 4);
1111
storage->scr.bus_widths = unstuff_bits(resp, 48, 4);
1112
1113
// If v2.0 is supported, check if Physical Layer Spec v3.0 is supported.
1114
if (storage->scr.sda_vsn == SCR_SPEC_VER_2)
1115
storage->scr.sda_spec3 = unstuff_bits(resp, 47, 1);
1116
if (storage->scr.sda_spec3)
1117
{
1118
u8 sda_spec4 = unstuff_bits(resp, 42, 1);
1119
if (sda_spec4)
1120
storage->scr.cmds = unstuff_bits(resp, 32, 4);
1121
else
1122
storage->scr.cmds = unstuff_bits(resp, 32, 2);
1123
}
1124
}
1125
1126
int sd_storage_get_scr(sdmmc_storage_t *storage, u8 *buf)
1127
{
1128
sdmmc_cmd_t cmdbuf;
1129
sdmmc_init_cmd(&cmdbuf, SD_APP_SEND_SCR, 0, SDMMC_RSP_TYPE_1, 0);
1130
1131
sdmmc_req_t reqbuf;
1132
reqbuf.buf = buf;
1133
reqbuf.blksize = 8;
1134
reqbuf.num_sectors = 1;
1135
reqbuf.is_write = 0;
1136
reqbuf.is_multi_block = 0;
1137
reqbuf.is_auto_stop_trn = 0;
1138
1139
if (!_sd_storage_execute_app_cmd(storage, R1_STATE_TRAN, 0, &cmdbuf, &reqbuf, NULL))
1140
return 0;
1141
1142
u32 tmp = 0;
1143
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
1144
//Prepare buffer for unstuff_bits
1145
for (u32 i = 0; i < 8; i+=4)
1146
{
1147
storage->raw_scr[i + 3] = buf[i];
1148
storage->raw_scr[i + 2] = buf[i + 1];
1149
storage->raw_scr[i + 1] = buf[i + 2];
1150
storage->raw_scr[i] = buf[i + 3];
1151
}
1152
_sd_storage_parse_scr(storage);
1153
1154
return _sdmmc_storage_check_card_status(tmp);
1155
}
1156
1157
static int _sd_storage_switch_get(sdmmc_storage_t *storage, void *buf)
1158
{
1159
sdmmc_cmd_t cmdbuf;
1160
sdmmc_init_cmd(&cmdbuf, SD_SWITCH, 0xFFFFFF, SDMMC_RSP_TYPE_1, 0);
1161
1162
sdmmc_req_t reqbuf;
1163
reqbuf.buf = buf;
1164
reqbuf.blksize = SDMMC_CMD_BLOCKSIZE;
1165
reqbuf.num_sectors = 1;
1166
reqbuf.is_write = 0;
1167
reqbuf.is_multi_block = 0;
1168
reqbuf.is_auto_stop_trn = 0;
1169
1170
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, NULL))
1171
return 0;
1172
1173
u32 tmp = 0;
1174
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
1175
return _sdmmc_storage_check_card_status(tmp);
1176
}
1177
1178
static int _sd_storage_switch(sdmmc_storage_t *storage, void *buf, int mode, int group, u32 arg)
1179
{
1180
sdmmc_cmd_t cmdbuf;
1181
u32 switchcmd = mode << 31 | 0x00FFFFFF;
1182
switchcmd &= ~(0xF << (group * 4));
1183
switchcmd |= arg << (group * 4);
1184
sdmmc_init_cmd(&cmdbuf, SD_SWITCH, switchcmd, SDMMC_RSP_TYPE_1, 0);
1185
1186
sdmmc_req_t reqbuf;
1187
reqbuf.buf = buf;
1188
reqbuf.blksize = SDMMC_CMD_BLOCKSIZE;
1189
reqbuf.num_sectors = 1;
1190
reqbuf.is_write = 0;
1191
reqbuf.is_multi_block = 0;
1192
reqbuf.is_auto_stop_trn = 0;
1193
1194
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, NULL))
1195
return 0;
1196
1197
u32 tmp = 0;
1198
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
1199
return _sdmmc_storage_check_card_status(tmp);
1200
}
1201
1202
static void _sd_storage_set_power_limit(sdmmc_storage_t *storage, u16 power_limit, u8 *buf)
1203
{
1204
u32 pwr = SD_SET_POWER_LIMIT_0_72;
1205
1206
// If UHS-I only, anything above 1.44W defaults to 1.44W.
1207
/*
1208
if (power_limit & SD_MAX_POWER_2_88)
1209
pwr = SD_SET_POWER_LIMIT_2_88;
1210
else if (power_limit & SD_MAX_POWER_2_16)
1211
pwr = SD_SET_POWER_LIMIT_2_16;
1212
*/
1213
if (power_limit & SD_MAX_POWER_1_44)
1214
pwr = SD_SET_POWER_LIMIT_1_44;
1215
1216
_sd_storage_switch(storage, buf, SD_SWITCH_SET, SD_SWITCH_GRP_PWRLIM, pwr);
1217
1218
switch ((buf[15] >> 4) & 0x0F)
1219
{
1220
/*
1221
case SD_SET_POWER_LIMIT_2_88:
1222
DPRINTF("[SD] power limit raised to 2880 mW\n");
1223
break;
1224
1225
case SD_SET_POWER_LIMIT_2_16:
1226
DPRINTF("[SD] power limit raised to 2160 mW\n");
1227
break;
1228
*/
1229
case SD_SET_POWER_LIMIT_1_44:
1230
DPRINTF("[SD] power limit raised to 1440 mW\n");
1231
break;
1232
1233
default:
1234
case SD_SET_POWER_LIMIT_0_72:
1235
DPRINTF("[SD] power limit defaulted to 720 mW\n");
1236
break;
1237
}
1238
}
1239
1240
int _sd_storage_set_driver_type(sdmmc_storage_t *storage, u32 driver, u8 *buf)
1241
{
1242
if (!_sd_storage_switch(storage, buf, SD_SWITCH_CHECK, SD_SWITCH_GRP_DRVSTR, driver))
1243
return 0;
1244
1245
u32 driver_out = buf[15] & 0xF;
1246
if (driver_out != driver)
1247
return 0;
1248
DPRINTF("[SD] supports Driver Strength %d\n", driver);
1249
1250
if (!_sd_storage_switch(storage, buf, SD_SWITCH_SET, SD_SWITCH_GRP_DRVSTR, driver))
1251
return 0;
1252
1253
if (driver_out != (buf[15] & 0xF))
1254
return 0;
1255
DPRINTF("[SD] card accepted Driver Strength %d\n", driver);
1256
1257
sdmmc_setup_drv_type(storage->sdmmc, driver);
1258
1259
return 1;
1260
}
1261
1262
/*
1263
* SD Card DDR200 (DDR208) support
1264
*
1265
* DLL Tuning (a) or Tuning Window (b) procedure:
1266
* 1. Check that Vendor Specific Command System is supported.
1267
* Used as Enable DDR200 Bus.
1268
* 2. Enable DDR200 bus mode via setting 14 to Group 2 via CMD6.
1269
* Access Mode group is left to default 0 (SDR12).
1270
* 3. Setup clock to 200 or 208 MHz.
1271
* 4a. Set host to DDR200/HS400 bus mode that enables DLL syncing.
1272
* Actual implementation supported by all DDR200 cards.
1273
* --
1274
* 4b. Set host to DDR50 bus mode that supports such high clocks.
1275
* Execute Manual Tuning.
1276
* Limited to non-Sandisk cards.
1277
*
1278
* On Tegra SoCs, that can be done with DDR50 host mode.
1279
* That's because HS400 4-bit or HS400 generally, is not supported on SD SDMMC.
1280
* And also, tuning can't be done automatically on any DDR mode.
1281
* So it needs to be done manually and selected tap will be applied from the
1282
* biggest sampling window.
1283
* That allows DDR200 support on every DDR200 SD card, other than the original
1284
* maker of DDR200, Sandisk.
1285
*
1286
* On the original implementation of DDR200 from Sandisk, a DLL mechanism,
1287
* like the one in eMMC HS400 is mandatory.
1288
* So the card can start data signals whenever it wants, and the host should
1289
* synchronize to the first DAT signal edge change.
1290
* Every single other vendor that implemented that, always starts data transfers
1291
* aligned to clock. That basically makes DDR200 in such SD cards a SDR104 but
1292
* sampled on both edges. So effectively, it's an in-spec signal with DDR50,
1293
* only that is clocked at 200MHz, instead of 50MHz.
1294
* So the extra needed thing is using a tuning window, which is absent from the
1295
* original implementation, since DDL syncing does not use that.
1296
*
1297
* On DLL tuning method expected cards, the tuning window is tiny.
1298
* So check against a minimum of 8 taps window, to disallow DDR200.
1299
*/
1300
#ifdef BDK_SDMMC_UHS_DDR200_SUPPORT
1301
static int _sd_storage_enable_DDR200(sdmmc_storage_t *storage, u8 *buf)
1302
{
1303
u32 cmd_system = UHS_DDR200_BUS_SPEED;
1304
if (!_sd_storage_switch(storage, buf, SD_SWITCH_CHECK, SD_SWITCH_GRP_CMDSYS, cmd_system))
1305
return 0;
1306
1307
u32 system_out = (buf[16] >> 4) & 0xF;
1308
if (system_out != cmd_system)
1309
return 0;
1310
DPRINTF("[SD] supports DDR200 mode\n");
1311
1312
u16 total_pwr_consumption = ((u16)buf[0] << 8) | buf[1];
1313
DPRINTF("[SD] max power: %d mW\n", total_pwr_consumption * 3600 / 1000);
1314
storage->max_power = total_pwr_consumption;
1315
1316
if (total_pwr_consumption <= 800)
1317
{
1318
if (!_sd_storage_switch(storage, buf, SD_SWITCH_SET, SD_SWITCH_GRP_CMDSYS, cmd_system))
1319
return 0;
1320
1321
if (system_out != ((buf[16] >> 4) & 0xF))
1322
return 0;
1323
DPRINTF("[SD] card accepted DDR200\n");
1324
1325
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_UHS_DDR200))
1326
return 0;
1327
DPRINTF("[SD] after setup clock DDR200\n");
1328
1329
if (!sdmmc_tuning_execute(storage->sdmmc, SDHCI_TIMING_UHS_DDR200, MMC_SEND_TUNING_BLOCK))
1330
return 0;
1331
DPRINTF("[SD] after tuning DDR200\n");
1332
1333
return _sdmmc_storage_check_status(storage);
1334
}
1335
1336
DPRINTF("[SD] card max power over limit\n");
1337
return 0;
1338
}
1339
#endif
1340
1341
static int _sd_storage_set_card_bus_speed(sdmmc_storage_t *storage, u32 hs_type, u8 *buf)
1342
{
1343
if (!_sd_storage_switch(storage, buf, SD_SWITCH_CHECK, SD_SWITCH_GRP_ACCESS, hs_type))
1344
return 0;
1345
1346
u32 type_out = buf[16] & 0xF;
1347
if (type_out != hs_type)
1348
return 0;
1349
DPRINTF("[SD] supports selected (U)HS mode %d\n", buf[16] & 0xF);
1350
1351
u16 total_pwr_consumption = ((u16)buf[0] << 8) | buf[1];
1352
DPRINTF("[SD] max power: %d mW\n", total_pwr_consumption * 3600 / 1000);
1353
storage->max_power = total_pwr_consumption;
1354
1355
if (total_pwr_consumption <= 800)
1356
{
1357
if (!_sd_storage_switch(storage, buf, SD_SWITCH_SET, SD_SWITCH_GRP_ACCESS, hs_type))
1358
return 0;
1359
1360
if (type_out != (buf[16] & 0xF))
1361
return 0;
1362
1363
return 1;
1364
}
1365
1366
DPRINTF("[SD] card max power over limit\n");
1367
return 0;
1368
}
1369
1370
int sd_storage_get_fmodes(sdmmc_storage_t *storage, u8 *buf, sd_func_modes_t *fmodes)
1371
{
1372
if (!buf)
1373
buf = (u8 *)SDMMC_UPPER_BUFFER;
1374
1375
if (!_sd_storage_switch_get(storage, buf))
1376
return 0;
1377
1378
fmodes->access_mode = buf[13] | (buf[12] << 8);
1379
fmodes->cmd_system = buf[11] | (buf[10] << 8);
1380
fmodes->driver_strength = buf[9] | (buf[8] << 8);
1381
fmodes->power_limit = buf[7] | (buf[6] << 8);
1382
1383
return 1;
1384
}
1385
1386
static int _sd_storage_enable_uhs_low_volt(sdmmc_storage_t *storage, u32 type, u8 *buf)
1387
{
1388
sd_func_modes_t fmodes;
1389
1390
if (sdmmc_get_bus_width(storage->sdmmc) != SDMMC_BUS_WIDTH_4)
1391
return 0;
1392
1393
if (!sd_storage_get_fmodes(storage, buf, &fmodes))
1394
return 0;
1395
1396
#ifdef BDK_SDMMC_UHS_DDR200_SUPPORT
1397
DPRINTF("[SD] access: %02X, power: %02X, cmd: %02X\n", fmodes.access_mode, fmodes.power_limit, fmodes.cmd_system);
1398
#else
1399
DPRINTF("[SD] access: %02X, power: %02X\n", fmodes.access_mode, fmodes.power_limit);
1400
#endif
1401
1402
u32 hs_type = 0;
1403
switch (type)
1404
{
1405
#ifdef BDK_SDMMC_UHS_DDR200_SUPPORT
1406
case SDHCI_TIMING_UHS_DDR200:
1407
// Fall through if DDR200 is not supported.
1408
if (fmodes.cmd_system & SD_MODE_UHS_DDR200)
1409
{
1410
DPRINTF("[SD] setting bus speed to DDR200\n");
1411
storage->csd.busspeed = 200;
1412
_sd_storage_set_power_limit(storage, fmodes.power_limit, buf);
1413
return _sd_storage_enable_DDR200(storage, buf);
1414
}
1415
#endif
1416
1417
case SDHCI_TIMING_UHS_SDR104:
1418
case SDHCI_TIMING_UHS_SDR82:
1419
// Fall through if not supported.
1420
if (fmodes.access_mode & SD_MODE_UHS_SDR104)
1421
{
1422
type = SDHCI_TIMING_UHS_SDR104;
1423
hs_type = UHS_SDR104_BUS_SPEED;
1424
DPRINTF("[SD] setting bus speed to SDR104\n");
1425
switch (type)
1426
{
1427
case SDHCI_TIMING_UHS_SDR104:
1428
storage->csd.busspeed = 104;
1429
break;
1430
case SDHCI_TIMING_UHS_SDR82:
1431
storage->csd.busspeed = 82;
1432
break;
1433
}
1434
break;
1435
}
1436
1437
case SDHCI_TIMING_UHS_SDR50:
1438
if (fmodes.access_mode & SD_MODE_UHS_SDR50)
1439
{
1440
type = SDHCI_TIMING_UHS_SDR50;
1441
hs_type = UHS_SDR50_BUS_SPEED;
1442
DPRINTF("[SD] setting bus speed to SDR50\n");
1443
storage->csd.busspeed = 50;
1444
break;
1445
}
1446
/*
1447
case SDHCI_TIMING_UHS_DDR50:
1448
if (fmodes.access_mode & SD_MODE_UHS_DDR50)
1449
{
1450
type = SDHCI_TIMING_UHS_DDR50;
1451
hs_type = UHS_DDR50_BUS_SPEED;
1452
DPRINTF("[SD] setting bus speed to DDR50\n");
1453
storage->csd.busspeed = 50;
1454
break;
1455
}
1456
*/
1457
case SDHCI_TIMING_UHS_SDR25:
1458
if (fmodes.access_mode & SD_MODE_UHS_SDR25)
1459
{
1460
type = SDHCI_TIMING_UHS_SDR25;
1461
hs_type = UHS_SDR25_BUS_SPEED;
1462
DPRINTF("[SD] setting bus speed to SDR25\n");
1463
storage->csd.busspeed = 25;
1464
break;
1465
}
1466
1467
default:
1468
DPRINTF("[SD] bus speed defaulted to SDR12\n");
1469
storage->csd.busspeed = 12;
1470
return 1;
1471
}
1472
1473
// Try to raise the power limit to let the card perform better.
1474
if (hs_type != UHS_SDR25_BUS_SPEED) // Not applicable for SDR12/SDR25.
1475
_sd_storage_set_power_limit(storage, fmodes.power_limit, buf);
1476
1477
// Setup and set selected card and bus speed.
1478
if (!_sd_storage_set_card_bus_speed(storage, hs_type, buf))
1479
return 0;
1480
DPRINTF("[SD] card accepted UHS\n");
1481
1482
if (!sdmmc_setup_clock(storage->sdmmc, type))
1483
return 0;
1484
DPRINTF("[SD] after setup clock\n");
1485
1486
if (!sdmmc_tuning_execute(storage->sdmmc, type, MMC_SEND_TUNING_BLOCK))
1487
return 0;
1488
DPRINTF("[SD] after tuning\n");
1489
1490
return _sdmmc_storage_check_status(storage);
1491
}
1492
1493
static int _sd_storage_enable_hs_high_volt(sdmmc_storage_t *storage, u8 *buf)
1494
{
1495
sd_func_modes_t fmodes;
1496
1497
if (!sd_storage_get_fmodes(storage, buf, &fmodes))
1498
return 0;
1499
1500
DPRINTF("[SD] access: %02X, power: %02X\n", fmodes.access_mode, fmodes.power_limit);
1501
1502
if (!(fmodes.access_mode & SD_MODE_HIGH_SPEED))
1503
return 1;
1504
1505
if (!_sd_storage_set_card_bus_speed(storage, HIGH_SPEED_BUS_SPEED, buf))
1506
return 0;
1507
1508
if (!_sdmmc_storage_check_status(storage))
1509
return 0;
1510
1511
return sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_SD_HS25);
1512
}
1513
1514
u32 sd_storage_get_ssr_au(sdmmc_storage_t *storage)
1515
{
1516
u32 au_size = storage->ssr.uhs_au_size;
1517
1518
if (!au_size)
1519
au_size = storage->ssr.au_size;
1520
1521
if (au_size <= 10)
1522
{
1523
u32 shift = au_size;
1524
au_size = shift ? 8 : 0;
1525
au_size <<= shift;
1526
}
1527
else
1528
{
1529
switch (au_size)
1530
{
1531
case 11:
1532
au_size = 12288;
1533
break;
1534
case 12:
1535
au_size = 16384;
1536
break;
1537
case 13:
1538
au_size = 24576;
1539
break;
1540
case 14:
1541
au_size = 32768;
1542
break;
1543
case 15:
1544
au_size = 65536;
1545
break;
1546
}
1547
}
1548
1549
return au_size;
1550
}
1551
1552
static void _sd_storage_parse_ssr(sdmmc_storage_t *storage)
1553
{
1554
// unstuff_bits supports only 4 u32 so break into 2 x u32x4 groups.
1555
u32 raw_ssr1[4]; // 511:384.
1556
u32 raw_ssr2[4]; // 383:256.
1557
1558
memcpy(raw_ssr1, &storage->raw_ssr[0], 16);
1559
memcpy(raw_ssr2, &storage->raw_ssr[16], 16);
1560
1561
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
1562
_sd_storage_debug_print_ssr(storage->raw_ssr);
1563
#endif
1564
1565
storage->ssr.bus_width = (unstuff_bits(raw_ssr1, 510, 2) & SD_BUS_WIDTH_4) ? 4 : 1;
1566
storage->ssr.protected_size = unstuff_bits(raw_ssr1, 448, 32);
1567
1568
u32 speed_class = unstuff_bits(raw_ssr1, 440, 8);
1569
switch(speed_class)
1570
{
1571
case 0:
1572
case 1:
1573
case 2:
1574
case 3:
1575
storage->ssr.speed_class = speed_class << 1;
1576
break;
1577
1578
case 4:
1579
storage->ssr.speed_class = 10;
1580
break;
1581
1582
default:
1583
storage->ssr.speed_class = speed_class;
1584
break;
1585
}
1586
storage->ssr.uhs_grade = unstuff_bits(raw_ssr1, 396, 4);
1587
storage->ssr.video_class = unstuff_bits(raw_ssr1, 384, 8);
1588
storage->ssr.app_class = unstuff_bits(raw_ssr2, 336, 4);
1589
1590
storage->ssr.au_size = unstuff_bits(raw_ssr1, 428, 4);
1591
storage->ssr.uhs_au_size = unstuff_bits(raw_ssr1, 392, 4);
1592
1593
storage->ssr.perf_enhance = unstuff_bits(raw_ssr2, 328, 8);
1594
}
1595
1596
int sd_storage_parse_perf_enhance(sdmmc_storage_t *storage, u8 fno, u8 page, u16 offset, u8 *buf)
1597
{
1598
// Check status reg for support.
1599
storage->ser.cache = (storage->ssr.perf_enhance >> 2) & BIT(0);
1600
storage->ser.cmdq = (storage->ssr.perf_enhance >> 3) & 0x1F;
1601
1602
if (!sd_storage_get_ext_reg(storage, fno, page, offset, 512, buf))
1603
{
1604
storage->ser.cache_ext = 0;
1605
storage->ser.cmdq_ext = 0;
1606
1607
return 0;
1608
}
1609
1610
storage->ser.cache_ext = buf[4] & BIT(0);
1611
storage->ser.cmdq_ext = buf[6] & 0x1F;
1612
1613
return 1;
1614
}
1615
1616
static void _sd_storage_parse_ext_reg(sdmmc_storage_t *storage, u8 *buf, u16 *addr_next)
1617
{
1618
u16 addr = *addr_next;
1619
1620
// Address to the next extension.
1621
*addr_next = (buf[addr + 41] << 8) | buf[addr + 40];
1622
1623
u16 sfc = (buf[addr + 1] << 8) | buf[addr];
1624
1625
u32 reg_sets = buf[addr + 42];
1626
1627
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
1628
for (u32 i = 0; i < reg_sets; i++)
1629
{
1630
u32 reg_set_addr;
1631
memcpy(&reg_set_addr, &buf[addr + 44 + 4 * i], 4);
1632
u16 off = reg_set_addr & 0x1FF;
1633
u8 page = reg_set_addr >> 9 & 0xFF;
1634
u8 fno = reg_set_addr >> 18 & 0xFF;
1635
gfx_printf("Addr: %04X sfc:%02X - fno:%02X, page:%02X, off:%04X\n", addr, sfc, fno, page, off);
1636
}
1637
#endif
1638
1639
// Parse Performance Enhance.
1640
if (sfc == 2 && reg_sets == 1)
1641
{
1642
u32 reg_set0_addr;
1643
memcpy(&reg_set0_addr, &buf[addr + 44], 4);
1644
u16 off = reg_set0_addr & 0x1FF;
1645
u8 page = reg_set0_addr >> 9 & 0xFF;
1646
u8 fno = reg_set0_addr >> 18 & 0xFF;
1647
1648
if (sd_storage_parse_perf_enhance(storage, fno, page, off, buf))
1649
storage->ser.valid = 1;
1650
}
1651
}
1652
1653
void sd_storage_get_ext_regs(sdmmc_storage_t *storage, u8 *buf)
1654
{
1655
DREGPRINTF("SD Extension Registers:\n\n");
1656
1657
if (!(storage->scr.cmds & BIT(2)))
1658
{
1659
DREGPRINTF("Not Supported!\n");
1660
return;
1661
}
1662
1663
if (!sd_storage_get_ext_reg(storage, 0, 0, 0, 512, buf))
1664
{
1665
DREGPRINTF("Failed to get general info!\n");
1666
return;
1667
}
1668
1669
u16 size = (buf[3] << 8) | buf[2];
1670
u16 addr_next = 16;
1671
u32 num_ext = buf[4];
1672
for (u32 i = 0; i < num_ext && addr_next < size; i++)
1673
_sd_storage_parse_ext_reg(storage, buf, &addr_next);
1674
}
1675
1676
int sd_storage_get_ssr(sdmmc_storage_t *storage, u8 *buf)
1677
{
1678
sdmmc_cmd_t cmdbuf;
1679
sdmmc_init_cmd(&cmdbuf, SD_APP_SD_STATUS, 0, SDMMC_RSP_TYPE_1, 0);
1680
1681
sdmmc_req_t reqbuf;
1682
reqbuf.buf = buf;
1683
reqbuf.blksize = SDMMC_CMD_BLOCKSIZE;
1684
reqbuf.num_sectors = 1;
1685
reqbuf.is_write = 0;
1686
reqbuf.is_multi_block = 0;
1687
reqbuf.is_auto_stop_trn = 0;
1688
1689
if (!(storage->csd.cmdclass & CCC_APP_SPEC))
1690
{
1691
DPRINTF("[SD] ssr: Not supported\n");
1692
return 0;
1693
}
1694
1695
if (!_sd_storage_execute_app_cmd(storage, R1_STATE_TRAN, 0, &cmdbuf, &reqbuf, NULL))
1696
return 0;
1697
1698
u32 tmp = 0;
1699
sdmmc_get_cached_rsp(storage->sdmmc, &tmp, SDMMC_RSP_TYPE_1);
1700
1701
// Convert buffer to LE.
1702
for (u32 i = 0; i < SDMMC_CMD_BLOCKSIZE; i += 4)
1703
{
1704
storage->raw_ssr[i + 3] = buf[i];
1705
storage->raw_ssr[i + 2] = buf[i + 1];
1706
storage->raw_ssr[i + 1] = buf[i + 2];
1707
storage->raw_ssr[i] = buf[i + 3];
1708
}
1709
1710
_sd_storage_parse_ssr(storage);
1711
1712
return _sdmmc_storage_check_card_status(tmp);
1713
}
1714
1715
static void _sd_storage_parse_cid(sdmmc_storage_t *storage)
1716
{
1717
u32 *raw_cid = (u32 *)&(storage->raw_cid);
1718
1719
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
1720
_sd_storage_debug_print_cid(raw_cid);
1721
#endif
1722
1723
storage->cid.manfid = unstuff_bits(raw_cid, 120, 8);
1724
storage->cid.oemid = unstuff_bits(raw_cid, 104, 16);
1725
storage->cid.prod_name[0] = unstuff_bits(raw_cid, 96, 8);
1726
storage->cid.prod_name[1] = unstuff_bits(raw_cid, 88, 8);
1727
storage->cid.prod_name[2] = unstuff_bits(raw_cid, 80, 8);
1728
storage->cid.prod_name[3] = unstuff_bits(raw_cid, 72, 8);
1729
storage->cid.prod_name[4] = unstuff_bits(raw_cid, 64, 8);
1730
storage->cid.hwrev = unstuff_bits(raw_cid, 60, 4);
1731
storage->cid.fwrev = unstuff_bits(raw_cid, 56, 4);
1732
storage->cid.serial = unstuff_bits(raw_cid, 24, 32);
1733
storage->cid.year = unstuff_bits(raw_cid, 12, 8) + 2000;
1734
storage->cid.month = unstuff_bits(raw_cid, 8, 4);
1735
}
1736
1737
static void _sd_storage_parse_csd(sdmmc_storage_t *storage)
1738
{
1739
u32 *raw_csd = (u32 *)&(storage->raw_csd);
1740
1741
#ifdef SDMMC_DEBUG_PRINT_SD_REGS
1742
_sd_storage_debug_print_csd(raw_csd);
1743
#endif
1744
1745
storage->csd.structure = unstuff_bits(raw_csd, 126, 2);
1746
storage->csd.cmdclass = unstuff_bits(raw_csd, 84, 12);
1747
storage->csd.read_blkbits = unstuff_bits(raw_csd, 80, 4);
1748
storage->csd.write_protect = unstuff_bits(raw_csd, 12, 2);
1749
switch(storage->csd.structure)
1750
{
1751
case 0:
1752
storage->csd.capacity = (1 + unstuff_bits(raw_csd, 62, 12)) << (unstuff_bits(raw_csd, 47, 3) + 2);
1753
storage->csd.capacity <<= unstuff_bits(raw_csd, 80, 4) - 9; // Convert native block size to LBA SDMMC_DAT_BLOCKSIZE.
1754
break;
1755
1756
case 1:
1757
storage->csd.c_size = (1 + unstuff_bits(raw_csd, 48, 22));
1758
storage->csd.capacity = storage->csd.c_size << 10;
1759
storage->csd.read_blkbits = 9;
1760
break;
1761
1762
default:
1763
DPRINTF("[SD] unknown CSD structure %d\n", storage->csd.structure);
1764
break;
1765
}
1766
1767
storage->sec_cnt = storage->csd.capacity;
1768
}
1769
1770
static bool _sdmmc_storage_get_bus_uhs_support(u32 bus_width, u32 type)
1771
{
1772
switch (type)
1773
{
1774
case SDHCI_TIMING_UHS_SDR12:
1775
case SDHCI_TIMING_UHS_SDR25:
1776
case SDHCI_TIMING_UHS_SDR50:
1777
case SDHCI_TIMING_UHS_SDR104:
1778
case SDHCI_TIMING_UHS_SDR82:
1779
case SDHCI_TIMING_UHS_DDR50:
1780
case SDHCI_TIMING_UHS_DDR200:
1781
if (bus_width == SDMMC_BUS_WIDTH_4)
1782
return true;
1783
default:
1784
return false;
1785
}
1786
}
1787
1788
void sdmmc_storage_init_wait_sd()
1789
{
1790
// T210/T210B01 WAR: Wait exactly 239ms for IO and Controller power to discharge.
1791
u32 sd_poweroff_time = (u32)get_tmr_ms() - sd_power_cycle_time_start;
1792
if (sd_poweroff_time < 239)
1793
msleep(239 - sd_poweroff_time);
1794
}
1795
1796
int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type)
1797
{
1798
u32 tmp = 0;
1799
bool is_sdsc = 0;
1800
u8 *buf = (u8 *)SDMMC_UPPER_BUFFER;
1801
bool bus_uhs_support = _sdmmc_storage_get_bus_uhs_support(bus_width, type);
1802
1803
DPRINTF("[SD]-[init: bus: %d, type: %d]\n", bus_width, type);
1804
1805
// Some cards (SanDisk U1), do not like a fast power cycle. Wait min 100ms.
1806
sdmmc_storage_init_wait_sd();
1807
1808
memset(storage, 0, sizeof(sdmmc_storage_t));
1809
storage->sdmmc = sdmmc;
1810
1811
if (!sdmmc_init(sdmmc, SDMMC_1, SDMMC_POWER_3_3, SDMMC_BUS_WIDTH_1, SDHCI_TIMING_SD_ID))
1812
return 0;
1813
DPRINTF("[SD] after init\n");
1814
1815
// Wait 1ms + 74 cycles.
1816
usleep(1000 + (74 * 1000 + sdmmc->card_clock - 1) / sdmmc->card_clock);
1817
1818
if (!_sdmmc_storage_go_idle_state(storage))
1819
return 0;
1820
DPRINTF("[SD] went to idle state\n");
1821
1822
if (!_sd_storage_send_if_cond(storage, &is_sdsc))
1823
return 0;
1824
DPRINTF("[SD] after send if cond\n");
1825
1826
if (!_sd_storage_get_op_cond(storage, is_sdsc, bus_uhs_support))
1827
return 0;
1828
DPRINTF("[SD] got op cond\n");
1829
1830
if (!_sdmmc_storage_get_cid(storage))
1831
return 0;
1832
DPRINTF("[SD] got cid\n");
1833
_sd_storage_parse_cid(storage);
1834
1835
if (!_sd_storage_get_rca(storage))
1836
return 0;
1837
DPRINTF("[SD] got rca (= %04X)\n", storage->rca);
1838
1839
if (!_sdmmc_storage_get_csd(storage))
1840
return 0;
1841
DPRINTF("[SD] got csd\n");
1842
_sd_storage_parse_csd(storage);
1843
1844
if (!storage->is_low_voltage)
1845
{
1846
if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_SD_DS12))
1847
return 0;
1848
DPRINTF("[SD] after setup default clock\n");
1849
}
1850
1851
if (!_sdmmc_storage_select_card(storage))
1852
return 0;
1853
DPRINTF("[SD] card selected\n");
1854
1855
if (!_sdmmc_storage_set_blocklen(storage, SD_BLOCKSIZE))
1856
return 0;
1857
DPRINTF("[SD] set blocklen to SD_BLOCKSIZE\n");
1858
1859
// Disconnect Card Detect resistor from DAT3.
1860
if (!_sd_storage_execute_app_cmd_type1(storage, &tmp, SD_APP_SET_CLR_CARD_DETECT, 0, 0, R1_STATE_TRAN))
1861
return 0;
1862
DPRINTF("[SD] cleared card detect\n");
1863
1864
if (!sd_storage_get_scr(storage, buf))
1865
return 0;
1866
DPRINTF("[SD] got scr\n");
1867
1868
// If card supports a wider bus and if it's not SD Version 1.0 switch bus width.
1869
if (bus_width == SDMMC_BUS_WIDTH_4 && (storage->scr.bus_widths & BIT(SD_BUS_WIDTH_4)) && storage->scr.sda_vsn)
1870
{
1871
if (!_sd_storage_execute_app_cmd_type1(storage, &tmp, SD_APP_SET_BUS_WIDTH, SD_BUS_WIDTH_4, 0, R1_STATE_TRAN))
1872
return 0;
1873
1874
sdmmc_set_bus_width(storage->sdmmc, SDMMC_BUS_WIDTH_4);
1875
DPRINTF("[SD] switched to wide bus width\n");
1876
}
1877
else
1878
{
1879
bus_width = SDMMC_BUS_WIDTH_1;
1880
DPRINTF("[SD] SD does not support wide bus width\n");
1881
}
1882
1883
if (storage->is_low_voltage)
1884
{
1885
if (!_sd_storage_enable_uhs_low_volt(storage, type, buf))
1886
return 0;
1887
DPRINTF("[SD] enabled UHS\n");
1888
}
1889
else if (type != SDHCI_TIMING_SD_DS12 && storage->scr.sda_vsn) // Not default speed and not SD Version 1.0.
1890
{
1891
if (!_sd_storage_enable_hs_high_volt(storage, buf))
1892
return 0;
1893
1894
DPRINTF("[SD] enabled HS\n");
1895
switch (bus_width)
1896
{
1897
case SDMMC_BUS_WIDTH_4:
1898
storage->csd.busspeed = 25;
1899
break;
1900
1901
case SDMMC_BUS_WIDTH_1:
1902
storage->csd.busspeed = 6;
1903
break;
1904
}
1905
}
1906
1907
// Parse additional card info from sd status.
1908
if (sd_storage_get_ssr(storage, buf))
1909
{
1910
DPRINTF("[SD] got sd status\n");
1911
}
1912
1913
sdmmc_card_clock_powersave(sdmmc, SDMMC_POWER_SAVE_ENABLE);
1914
1915
storage->initialized = 1;
1916
1917
return 1;
1918
}
1919
1920
/*
1921
* Gamecard specific functions.
1922
*/
1923
1924
int _gc_storage_custom_cmd(sdmmc_storage_t *storage, void *buf)
1925
{
1926
u32 resp;
1927
sdmmc_cmd_t cmdbuf;
1928
sdmmc_init_cmd(&cmdbuf, MMC_VENDOR_60_CMD, 0, SDMMC_RSP_TYPE_1, 1);
1929
1930
sdmmc_req_t reqbuf;
1931
reqbuf.buf = buf;
1932
reqbuf.blksize = SDMMC_CMD_BLOCKSIZE;
1933
reqbuf.num_sectors = 1;
1934
reqbuf.is_write = 1;
1935
reqbuf.is_multi_block = 0;
1936
reqbuf.is_auto_stop_trn = 0;
1937
1938
if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, NULL))
1939
{
1940
sdmmc_stop_transmission(storage->sdmmc, &resp);
1941
return 0;
1942
}
1943
1944
if (!sdmmc_get_cached_rsp(storage->sdmmc, &resp, SDMMC_RSP_TYPE_1))
1945
return 0;
1946
if (!_sdmmc_storage_check_card_status(resp))
1947
return 0;
1948
return _sdmmc_storage_check_status(storage);
1949
}
1950
1951
int sdmmc_storage_init_gc(sdmmc_storage_t *storage, sdmmc_t *sdmmc)
1952
{
1953
memset(storage, 0, sizeof(sdmmc_storage_t));
1954
storage->sdmmc = sdmmc;
1955
1956
if (!sdmmc_init(sdmmc, SDMMC_2, SDMMC_POWER_1_8, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS100))
1957
return 0;
1958
DPRINTF("[GC] after init\n");
1959
1960
// Wait 1ms + 10 clock cycles.
1961
usleep(1000 + (10 * 1000 + sdmmc->card_clock - 1) / sdmmc->card_clock);
1962
1963
if (!sdmmc_tuning_execute(storage->sdmmc, SDHCI_TIMING_MMC_HS100, MMC_SEND_TUNING_BLOCK_HS200))
1964
return 0;
1965
DPRINTF("[GC] after tuning\n");
1966
1967
sdmmc_card_clock_powersave(sdmmc, SDMMC_POWER_SAVE_ENABLE);
1968
1969
storage->initialized = 1;
1970
1971
return 1;
1972
}
1973
1974