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