-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathsdhci.c
More file actions
1808 lines (1567 loc) · 58.8 KB
/
Copy pathsdhci.c
File metadata and controls
1808 lines (1567 loc) · 58.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sdhci.c
*
* Cadence SD Host Controller Interface (SDHCI) Driver
* Generic implementation supporting SD cards and eMMC.
*
* Copyright (C) 2026 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include "sdhci.h"
#if defined(DISK_SDCARD) || defined(DISK_EMMC)
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "printf.h"
#include "hal.h"
#include "disk.h"
/* ============================================================================
* Platform DMA cache maintenance (weak defaults - override in HAL)
* ============================================================================ */
#ifndef SDHCI_SDMA_DISABLED
void __attribute__((weak)) sdhci_platform_dma_prepare(
void *buf, uint32_t sz, int is_write)
{
(void)buf; (void)sz; (void)is_write;
}
void __attribute__((weak)) sdhci_platform_dma_complete(
void *buf, uint32_t sz, int is_write)
{
(void)buf; (void)sz; (void)is_write;
}
#endif
/* ============================================================================
* Internal state
* ============================================================================ */
static uint32_t g_sector_count;
static uint32_t g_sector_size;
static uint32_t g_bus_width = 1;
static uint32_t g_rca = 0; /* SD Card Relative Address */
/* MMC Interrupt state - volatile for interrupt handler access */
static volatile uint32_t g_mmc_irq_status = 0;
static volatile int g_mmc_irq_pending = 0;
/* Microsecond delay using hardware timer */
static void udelay(uint32_t us)
{
uint64_t end = hal_get_timer_us() + us;
while (hal_get_timer_us() < end);
}
/* ============================================================================
* Register Access Helpers
* ============================================================================ */
/* Read 32-bit register at offset */
#define SDHCI_REG(offset) sdhci_reg_read(offset)
/* Write 32-bit register at offset */
#define SDHCI_REG_SET(offset, val) sdhci_reg_write(offset, val)
/* Read-modify-write helper */
static inline void sdhci_reg_or(uint32_t offset, uint32_t val)
{
sdhci_reg_write(offset, sdhci_reg_read(offset) | val);
}
static inline void sdhci_reg_and(uint32_t offset, uint32_t val)
{
sdhci_reg_write(offset, sdhci_reg_read(offset) & val);
}
/* ============================================================================
* PHY Register Access
* ============================================================================ */
/* Write to SD/eMMC PHY register via HRS04 */
static void sdhci_phy_write(uint8_t phy_addr, uint8_t delay_val)
{
uint32_t phycfg;
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdhci_phy_write: phyaddr: 0x%08x, delay_value: %d\n",
phy_addr, delay_val);
#endif
/* Wait for ACK to clear */
while ((SDHCI_REG(SDHCI_HRS04) & SDHCI_HRS04_UIS_ACK) == 0);
/* Set address and delay value */
phycfg = ((uint32_t)phy_addr & SDHCI_HRS04_UIS_ADDR_MASK) |
((uint32_t)delay_val << SDHCI_HRS04_UIS_WDATA_SHIFT);
SDHCI_REG_SET(SDHCI_HRS04, phycfg);
/* Send write request */
SDHCI_REG_SET(SDHCI_HRS04, phycfg | SDHCI_HRS04_UIS_WR);
/* Wait for ACK */
while ((SDHCI_REG(SDHCI_HRS04) & SDHCI_HRS04_UIS_ACK) == 0);
/* Clear write request */
SDHCI_REG_SET(SDHCI_HRS04, phycfg);
SDHCI_REG_SET(SDHCI_HRS04, 0);
}
/* ============================================================================
* Interrupt Handler
* ============================================================================ */
void sdhci_irq_handler(void)
{
uint32_t status = SDHCI_REG(SDHCI_SRS12);
/* Check for DMA interrupt (SDMA boundary crossing) */
if (status & SDHCI_SRS12_DMAINT) {
/* Read the next DMA address saved by the controller */
uint32_t addr_lo = SDHCI_REG(SDHCI_SRS22);
uint32_t addr_hi = SDHCI_REG(SDHCI_SRS23);
/* Clear DMA interrupt status before restarting */
SDHCI_REG_SET(SDHCI_SRS12, SDHCI_SRS12_DMAINT);
#if defined(__riscv)
asm volatile("fence rw, rw" ::: "memory");
#elif defined(__aarch64__)
asm volatile("dsb sy" ::: "memory");
#endif
/* Write SDMA address to resume transfer.
* Per SDHCI v4 spec: write high 32 bits first, then low 32 bits.
* Writing the low address (SRS22 / offset 0x058) triggers the
* DMA engine to resume. */
SDHCI_REG_SET(SDHCI_SRS23, addr_hi);
SDHCI_REG_SET(SDHCI_SRS22, addr_lo);
g_mmc_irq_status |= SDHCI_IRQ_FLAG_DMAINT;
}
/* Check for transfer complete */
if (status & SDHCI_SRS12_TC) {
g_mmc_irq_status |= SDHCI_IRQ_FLAG_TC;
SDHCI_REG_SET(SDHCI_SRS12, SDHCI_SRS12_TC); /* Clear interrupt */
}
/* Check for command complete */
if (status & SDHCI_SRS12_CC) {
g_mmc_irq_status |= SDHCI_IRQ_FLAG_CC;
SDHCI_REG_SET(SDHCI_SRS12, SDHCI_SRS12_CC); /* Clear interrupt */
}
/* Check for data timeout error */
if (status & SDHCI_SRS12_EDT) {
g_mmc_irq_status |= SDHCI_IRQ_FLAG_ERROR;
SDHCI_REG_SET(SDHCI_SRS12, SDHCI_SRS12_EDT); /* Clear interrupt */
}
/* Check for any other errors */
if (status & SDHCI_SRS12_EINT) {
g_mmc_irq_status |= SDHCI_IRQ_FLAG_ERROR;
/* Clear all error status bits */
SDHCI_REG_SET(SDHCI_SRS12, (status & SDHCI_SRS12_ERR_STAT));
}
/* Signal that interrupt was handled */
g_mmc_irq_pending = 1;
#ifdef DEBUG_SDHCI_IRQ
wolfBoot_printf("sdhci_irq_handler: status=0x%08X, flags=0x%02X\n",
status, g_mmc_irq_status);
#endif
}
/* Enable SDHCI interrupts for SDMA transfer */
static void sdhci_enable_sdma_interrupts(void)
{
/* Enable signal interrupts for: DMA, Transfer Complete, Command Complete,
* Data Timeout Error */
uint32_t sig_enable = SDHCI_SRS14_DMAINT_IE |
SDHCI_SRS14_TC_IE |
SDHCI_SRS14_CC_IE |
SDHCI_SRS14_EDT_IE;
sdhci_reg_or(SDHCI_SRS14, sig_enable);
/* Clear any pending interrupt state */
g_mmc_irq_status = 0;
g_mmc_irq_pending = 0;
}
/* Disable SDHCI signal interrupts (status enables remain for polling) */
static void sdhci_disable_sdma_interrupts(void)
{
uint32_t reg = SDHCI_REG(SDHCI_SRS14);
reg &= ~(SDHCI_SRS14_DMAINT_IE |
SDHCI_SRS14_TC_IE |
SDHCI_SRS14_CC_IE |
SDHCI_SRS14_EDT_IE);
SDHCI_REG_SET(SDHCI_SRS14, reg);
}
/* Wait for SDHCI interrupt with timeout.
* Supports both hardware interrupt and polling modes:
* - Interrupt mode: g_mmc_irq_pending set by sdhci_irq_handler() via platform ISR
* - Polling mode: directly reads SRS12 status register and calls handler */
static int sdhci_wait_irq(uint32_t expected_flags, uint32_t timeout)
{
while (timeout-- > 0) {
/* Poll SRS12 directly for platforms without interrupt routing.
* In interrupt mode this is redundant (bits already cleared by ISR). */
uint32_t status = SDHCI_REG(SDHCI_SRS12);
if (status & (SDHCI_SRS12_TC | SDHCI_SRS12_CC | SDHCI_SRS12_DMAINT |
SDHCI_SRS12_EDT | SDHCI_SRS12_EINT)) {
sdhci_irq_handler();
}
if (g_mmc_irq_pending) {
g_mmc_irq_pending = 0;
/* Check for error */
if (g_mmc_irq_status & SDHCI_IRQ_FLAG_ERROR) {
return -1;
}
/* Check for expected flags */
if (g_mmc_irq_status & expected_flags) {
return 0;
}
}
asm volatile("nop");
}
return -1; /* Timeout */
}
/* ============================================================================
* Timeout Configuration
* ============================================================================ */
static int sdhci_set_timeout(uint32_t timeout_us)
{
uint32_t reg, i, tcfclk, tcfclk_mhz, tcfclk_khz, timeout_val, dtcv;
/* read capabilities to determine timeout clock frequency and unit (MHz or kHz) */
reg = SDHCI_REG(SDHCI_SRS16);
tcfclk_khz = (reg & SDHCI_SRS16_TCF_MASK) >> SDHCI_SRS16_TCF_SHIFT;
/* Default timeout clock frequency should be 50MHz */
if (((reg & SDHCI_SRS16_TCU) == 0) && (timeout_us < 1000)) {
/* invalid timeout_us value */
return -1;
}
if (tcfclk_khz == 0) {
/* reported timeout clock frequency is 0 */
return -1;
}
if ((reg & SDHCI_SRS16_TCU) != 0) {
tcfclk_khz *= 1000; /* MHz to kHz */
}
tcfclk_mhz = tcfclk_khz / 1000;
if (tcfclk_mhz == 0) {
tcfclk = tcfclk_khz;
timeout_val = timeout_us / 1000;
}
else {
tcfclk = tcfclk_mhz;
timeout_val = timeout_us;
}
/* calculate the data timeout counter value */
dtcv = 8192; /* 2*13 */
for (i=0; i<15; i++) {
if (timeout_val < (dtcv / tcfclk)) {
break;
}
dtcv *= 2;
}
dtcv = i;
/* set the data timeout counter value */
reg = SDHCI_REG(SDHCI_SRS11);
reg &= ~SDHCI_SRS11_DTCV_MASK;
reg |= (dtcv << SDHCI_SRS11_DTCV_SHIFT) & SDHCI_SRS11_DTCV_MASK;
SDHCI_REG_SET(SDHCI_SRS11, reg);
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdhci_set_timeout: timeout_val %d (%d)\n", timeout_val, dtcv);
#endif
return 0;
}
/* ============================================================================
* Power Control
* ============================================================================ */
/* voltage values:
* 0 = off
* SDHCI_SRS10_BVS_1_8V
* SDHCI_SRS10_BVS_3_0V
* SDHCI_SRS10_BVS_3_3V
*/
static int sdhci_set_power(uint32_t voltage)
{
uint32_t reg;
/* disable bus power */
reg = SDHCI_REG(SDHCI_SRS10);
reg &= ~SDHCI_SRS10_BP;
SDHCI_REG_SET(SDHCI_SRS10, reg);
if (voltage != 0) {
/* read voltage capabilities */
uint32_t cap2 = SDHCI_REG(SDHCI_SRS16);
/* select voltage (if capable) */
reg &= ~SDHCI_SRS10_BVS_MASK;
if (voltage == SDHCI_SRS10_BVS_1_8V && (cap2 & SDHCI_SRS16_VS18)) {
reg |= SDHCI_SRS10_BP | SDHCI_SRS10_BVS_1_8V;
}
else if (voltage == SDHCI_SRS10_BVS_3_0V && (cap2 & SDHCI_SRS16_VS30)) {
reg |= SDHCI_SRS10_BP | SDHCI_SRS10_BVS_3_0V;
}
else if (voltage == SDHCI_SRS10_BVS_3_3V && (cap2 & SDHCI_SRS16_VS33)) {
reg |= SDHCI_SRS10_BP | SDHCI_SRS10_BVS_3_3V;
}
else {
/* voltage not supported */
return -1;
}
/* should be - 0xf06 */
SDHCI_REG_SET(SDHCI_SRS10, reg);
}
return 0;
}
/* ============================================================================
* Clock Control
* ============================================================================ */
/* returns actual frequency in kHz */
static uint32_t sdhci_set_clock(uint32_t clock_khz)
{
static uint32_t last_clock_khz = 0;
uint32_t reg, base_clk_khz, i, mclk, freq_khz;
if (last_clock_khz != 0 && last_clock_khz == clock_khz) {
/* clock already set */
return 0;
}
/* disable clock */
sdhci_reg_and(SDHCI_SRS11, ~SDHCI_SRS11_SDCE);
/* get base clock */
reg = SDHCI_REG(SDHCI_SRS16);
base_clk_khz = (reg & SDHCI_SRS16_BCSDCLK_MASK) >> SDHCI_SRS16_BCSDCLK_SHIFT;
if (base_clk_khz == 0) {
/* error getting base clock */
return -1;
}
base_clk_khz *= 1000; /* convert MHz to kHz */
/* calculate divider */
for (i=1; i<2046; i++) {
if (((base_clk_khz / i) < clock_khz) ||
(((base_clk_khz / i) == clock_khz) && (base_clk_khz % i) == 0)) {
break;
}
}
mclk = (i / 2);
/* select clock frequency */
reg = SDHCI_REG(SDHCI_SRS11);
reg &= ~(SDHCI_SRS11_SDCFSL_MASK | SDHCI_SRS11_SDCFSH_MASK);
reg |= (((mclk & 0x0FF) << SDHCI_SRS11_SDCFSL_SHIFT) & SDHCI_SRS11_SDCFSL_MASK); /* lower 8 bits */
reg |= (((mclk & 0x300) >> 2) & SDHCI_SRS11_SDCFSH_MASK); /* upper 2 bits */
reg |= SDHCI_SRS11_ICE; /* clock enable */
reg &= ~SDHCI_SRS11_CGS; /* select clock */
SDHCI_REG_SET(SDHCI_SRS11, reg);
freq_khz = base_clk_khz / i;
/* wait for clock to stabilize */
while ((SDHCI_REG(SDHCI_SRS11) & SDHCI_SRS11_ICS) == 0);
/* enable clock */
sdhci_reg_or(SDHCI_SRS11, SDHCI_SRS11_SDCE);
last_clock_khz = clock_khz;
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdhci_set_clock: requested khz: %d, actual khz: %d\n",
clock_khz, freq_khz);
#endif
return freq_khz;
}
/* ============================================================================
* Command Response Helpers
* ============================================================================ */
static uint32_t sdhci_get_response_type(uint8_t resp_type)
{
uint32_t cmd_reg;
switch (resp_type) {
case SDHCI_RESP_R2:
cmd_reg = (SDHCI_SRS03_RESP_136 | SDHCI_SRS03_CRCCE);
break;
case SDHCI_RESP_R3:
case SDHCI_RESP_R4:
cmd_reg = SDHCI_SRS03_RESP_48;
break;
case SDHCI_RESP_R1:
case SDHCI_RESP_R5:
case SDHCI_RESP_R6:
case SDHCI_RESP_R7:
cmd_reg = (SDHCI_SRS03_RESP_48 | SDHCI_SRS03_CRCCE | SDHCI_SRS03_CICE);
break;
case SDHCI_RESP_R1B:
case SDHCI_RESP_R5B:
cmd_reg = (SDHCI_SRS03_RESP_48B | SDHCI_SRS03_CRCCE | SDHCI_SRS03_CICE);
break;
case SDHCI_RESP_NONE:
default:
cmd_reg = SDHCI_SRS03_RESP_NONE;
break;
}
return cmd_reg;
}
/* ============================================================================
* Command Sending
* ============================================================================ */
#define DEVICE_BUSY 1
static int sdhci_send_cmd_internal(uint32_t cmd_type,
uint32_t cmd_index, uint32_t cmd_arg, uint8_t resp_type)
{
int status = 0;
uint32_t cmd_reg;
uint32_t timeout = 0x000FFFFF;
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdhci_send_cmd: cmd_index: %d, cmd_arg: %08X, resp_type: %d\n",
cmd_index, cmd_arg, resp_type);
#endif
/* wait for command line to be idle */
while ((SDHCI_REG(SDHCI_SRS09) & SDHCI_SRS09_CICMD) != 0);
/* set command argument and command transfer registers */
SDHCI_REG_SET(SDHCI_SRS02, cmd_arg);
cmd_reg =
((cmd_index << SDHCI_SRS03_CIDX_SHIFT) & SDHCI_SRS03_CIDX_MASK) |
((cmd_type << SDHCI_SRS03_CT_SHIFT) & SDHCI_SRS03_CT_MASK) |
sdhci_get_response_type(resp_type);
SDHCI_REG_SET(SDHCI_SRS03, cmd_reg);
/* wait for command complete or error */
while ((SDHCI_REG(SDHCI_SRS12) & (SDHCI_SRS12_CC | SDHCI_SRS12_TC |
SDHCI_SRS12_EINT)) == 0 && --timeout > 0);
if (timeout == 0) {
wolfBoot_printf("sdhci_send_cmd: timeout waiting for command complete\n");
status = -1; /* error */
}
else if (SDHCI_REG(SDHCI_SRS12) & SDHCI_SRS12_EINT) {
wolfBoot_printf("sdhci_send_cmd: error SRS12: 0x%08X\n", SDHCI_REG(SDHCI_SRS12));
status = -1; /* error */
}
SDHCI_REG_SET(SDHCI_SRS12, SDHCI_SRS12_CC); /* clear command complete */
while ((SDHCI_REG(SDHCI_SRS09) & SDHCI_SRS09_CICMD) != 0);
if (status == 0) {
/* check for device busy */
if (resp_type == SDHCI_RESP_R1 || resp_type == SDHCI_RESP_R1B) {
uint32_t resp = SDHCI_REG(SDHCI_SRS04);
#define CARD_STATUS_READY_FOR_DATA (1U << 8)
if ((resp & CARD_STATUS_READY_FOR_DATA) == 0) {
status = DEVICE_BUSY; /* card is busy */
}
}
}
return status;
}
int sdhci_cmd(uint32_t cmd_index, uint32_t cmd_arg, uint8_t resp_type)
{
/* send command */
int status = sdhci_send_cmd_internal(SDHCI_SRS03_CMD_NORMAL, cmd_index,
cmd_arg, resp_type);
/* clear all status interrupts
* (except current limit, card interrupt/removal/insert) */
SDHCI_REG_SET(SDHCI_SRS12, ~(SDHCI_SRS12_ECL |
SDHCI_SRS12_CINT |
SDHCI_SRS12_CR |
SDHCI_SRS12_CIN));
return status;
}
/* TODO: Add timeout */
static int sdhci_wait_busy(int check_dat0)
{
uint32_t status;
if (check_dat0) {
/* wait for DATA0 not busy */
while ((SDHCI_REG(SDHCI_SRS09) & SDHCI_SRS09_DAT0_LVL) == 0);
}
/* wait for CMD13 */
while ((status = sdhci_cmd(MMC_CMD13_SEND_STATUS,
(g_rca << SD_RCA_SHIFT), SDHCI_RESP_R1)) == DEVICE_BUSY);
return status;
}
/* Reset data and command lines to recover from errors */
static inline void sdhci_reset_lines(void)
{
sdhci_reg_or(SDHCI_SRS11, SDHCI_SRS11_RESET_DAT_CMD);
while (SDHCI_REG(SDHCI_SRS11) & SDHCI_SRS11_RESET_DAT_CMD);
}
/* ============================================================================
* Response Parsing Helper
* ============================================================================ */
/* Helper to get bits from the response registers (shared by eMMC and SD) */
static uint32_t sdhci_get_response_bits(int from, int count)
{
uint32_t mask, ret;
int off, shft;
from -= 8;
mask = ((count < 32) ? (1U << (uint32_t)count) : 0) - 1;
off = from / 32;
shft = from & 31;
/* Read the response registers (SRS04-SRS07) */
volatile uint32_t resp[4];
resp[0] = SDHCI_REG(SDHCI_SRS04);
resp[1] = SDHCI_REG(SDHCI_SRS05);
resp[2] = SDHCI_REG(SDHCI_SRS06);
resp[3] = SDHCI_REG(SDHCI_SRS07);
ret = resp[off] >> shft;
if ((shft + count) > 32) {
ret |= resp[off + 1] << ((32 - shft) % 32);
}
return ret & mask;
}
/* ============================================================================
* SD Card Specific Functions
* ============================================================================ */
#ifdef DISK_SDCARD
/* Set power and send SD card initialization commands */
/* voltage: 0=off or SDHCI_SRS10_BVS_[X_X]V */
static int sdcard_power_init_seq(uint32_t voltage)
{
int retries;
/* Set power to specified voltage */
int status = sdhci_set_power(voltage);
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdcard_power_init: power status=%d, SRS09=0x%x, "
"SRS10=0x%x, SRS11=0x%x, SRS12=0x%x\n",
status,
SDHCI_REG(SDHCI_SRS09), SDHCI_REG(SDHCI_SRS10),
SDHCI_REG(SDHCI_SRS11), SDHCI_REG(SDHCI_SRS12));
#endif
if (status != 0)
return status;
/* SD spec requires >= 1ms after power stabilizes before CMD0. */
udelay(1000);
/* Some cards and the ZynqMP Arasan controller need more settling
* time after the slot-type change + soft reset in sdhci_platform_init().
* Use a retry loop: if CMD0 fails, wait and retry (self-calibrating). */
for (retries = 0; retries < 10; retries++) {
status = sdhci_cmd(MMC_CMD0_GO_IDLE, 0, SDHCI_RESP_NONE);
if (status == 0)
break;
udelay(10000); /* 10ms between retries */
}
if (status != 0) {
wolfBoot_printf("SD: CMD0 failed after %d retries\n", retries);
}
else if (retries > 0) {
wolfBoot_printf("SD: CMD0 succeeded after %d retries\n", retries);
}
if (status == 0) {
/* send the operating conditions command */
status = sdhci_cmd(SD_CMD8_SEND_IF_COND, SD_IF_COND_27V_33V,
SDHCI_RESP_R7);
}
return status;
}
/* SD card operating conditions initialization using ACMD41 */
static int sdcard_card_init(uint32_t acmd41_arg, uint32_t *ocr_reg)
{
int status = sdhci_cmd(SD_CMD55_APP_CMD, 0, SDHCI_RESP_R1);
if (status == 0) {
status = sdhci_cmd(SD_ACMD41_SEND_OP_COND, acmd41_arg,
SDHCI_RESP_R3);
if (status == 0) {
*ocr_reg = SDHCI_REG(SDHCI_SRS04);
#ifdef DEBUG_SDHCI
wolfBoot_printf("ocr_reg: 0x%08X\n", *ocr_reg);
#endif
}
}
return status;
}
/* Forward declarations for SD card functions */
static int sdcard_set_bus_width(uint32_t bus_width);
static int sdcard_set_function(uint32_t function_number, uint32_t group_number);
/* Full SD card initialization sequence
* Returns 0 on success */
static int sdcard_card_full_init(void)
{
int status = 0;
uint32_t reg;
uint32_t ctrl_volts, card_volts;
uint32_t irq_restore;
int xpc, si8r;
/* Set power to 3.3v and send init commands */
ctrl_volts = SDHCI_SRS10_BVS_3_3V; /* default to 3.3v */
status = sdcard_power_init_seq(ctrl_volts);
if (status == 0) {
uint32_t max_ma_3_3v, max_ma_1_8v;
/* determine host controller capabilities */
reg = SDHCI_REG(SDHCI_SRS18);
max_ma_3_3v = ((reg & SDHCI_SRS18_MC33_MASK) >> SDHCI_SRS18_MC33_SHIFT) * 4;
max_ma_1_8v = ((reg & SDHCI_SRS18_MC18_MASK) >> SDHCI_SRS18_MC18_SHIFT) * 4;
/* does controller support eXtended Power Control (XPC)? */
xpc = (max_ma_1_8v >= 150) && (max_ma_3_3v >= 150) ? 1 : 0;
/* does controller support UHS-I (Ultra High Speed Interface) v1.8 signaling? */
si8r = ((SDHCI_REG(SDHCI_SRS16) & SDHCI_SRS16_VS18) && /* 1.8v supported */
(SDHCI_REG(SDHCI_SRS17) & (SDHCI_SRS17_DDR50 | /* DDR50, SDR104 or SDR50 supported */
SDHCI_SRS17_SDR104 |
SDHCI_SRS17_SDR50))) ? 1 : 0;
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdcard_init: xpc:%d, si8r:%d, max_ma (3.3v:%d 1.8v:%d)\n",
xpc, si8r, max_ma_3_3v, max_ma_1_8v);
#endif
}
if (status == 0) {
reg = 0;
/* get operating conditions */
status = sdcard_card_init(0, ®);
if (status == 0) {
/* pick host and card operating voltages */
if (reg & SDCARD_REG_OCR_3_3_3_4) { /* 3.3v - 3.4v */
card_volts = SDCARD_REG_OCR_3_3_3_4;
}
else if (reg & SDCARD_REG_OCR_3_2_3_3) { /* 3.2v - 3.3v */
card_volts = SDCARD_REG_OCR_3_2_3_3;
}
else if (reg & SDCARD_REG_OCR_3_1_3_2) { /* 3.1v - 3.2v */
card_volts = SDCARD_REG_OCR_3_1_3_2;
}
else if (reg & SDCARD_REG_OCR_3_0_3_1) { /* 3.0v - 3.1v */
card_volts = SDCARD_REG_OCR_3_0_3_1;
ctrl_volts = SDHCI_SRS10_BVS_3_0V;
}
else if (reg & SDCARD_REG_OCR_2_9_3_0) { /* 2.9v - 3.0v */
card_volts = SDCARD_REG_OCR_2_9_3_0;
ctrl_volts = SDHCI_SRS10_BVS_3_0V;
}
else { /* default to v3.3 */
card_volts = SDCARD_REG_OCR_3_3_3_4;
}
/* if needed change operating voltage and re-init */
if (ctrl_volts != SDHCI_SRS10_BVS_3_3V) {
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdcard_init: changing operating voltage to 3.0v\n");
#endif
status = sdcard_power_init_seq(ctrl_volts);
}
}
}
if (status == 0) {
/* configure operating conditions */
uint32_t cmd_arg = SDCARD_ACMD41_HCS;
cmd_arg |= card_volts;
if (si8r) {
cmd_arg |= SDCARD_REG_OCR_S18RA;
}
if (xpc) {
cmd_arg |= SDCARD_REG_OCR_XPC;
}
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdcard_init: sending OCR arg: 0x%08X\n", cmd_arg);
#endif
/* retry until OCR ready */
do {
status = sdcard_card_init(cmd_arg, ®);
} while (status == 0 && (reg & SDCARD_REG_OCR_READY) == 0);
}
if (status == 0) {
/* Get card identification */
status = sdhci_cmd(MMC_CMD2_ALL_SEND_CID, 0, SDHCI_RESP_R2);
}
if (status == 0) {
/* Set relative address - SD card assigns its own RCA */
status = sdhci_cmd(MMC_CMD3_SET_REL_ADDR, 0, SDHCI_RESP_R6);
}
if (status == 0) {
g_rca = ((SDHCI_REG(SDHCI_SRS04) >> SD_RCA_SHIFT) & 0xFFFF);
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdcard_init: rca: %d\n", g_rca);
#endif
}
if (status == 0) {
/* read CSD register from device */
status = sdhci_cmd(MMC_CMD9_SEND_CSD, g_rca << SD_RCA_SHIFT,
SDHCI_RESP_R2);
}
if (status == 0) {
/* Get sector size and count */
uint32_t csd_struct;
uint32_t bl_len, c_size, c_size_mult;
bl_len = sdhci_get_response_bits(22, 4);
g_sector_size = (1U << bl_len);
csd_struct = sdhci_get_response_bits(126, 2);
switch (csd_struct) {
case 0:
c_size = sdhci_get_response_bits(62, 12);
c_size_mult = sdhci_get_response_bits(47, 3);
g_sector_count = (c_size + 1) << (c_size_mult + 2);
break;
case 1:
c_size = sdhci_get_response_bits(48, 22);
g_sector_count = (c_size + 1) << 10;
break;
default:
/* invalid CSD structure */
status = -1;
break;
}
#ifdef DEBUG_SDHCI
wolfBoot_printf("sdcard_init: csd_version: %d, sector: size %d count %d\n",
csd_struct, g_sector_size, g_sector_count);
#endif
}
if (status == 0) {
/* select card */
status = sdhci_cmd(MMC_CMD7_SELECT_CARD, g_rca << SD_RCA_SHIFT,
SDHCI_RESP_R1B);
if (status == DEVICE_BUSY) {
status = sdhci_wait_busy(1);
}
}
irq_restore = SDHCI_REG(SDHCI_SRS13);
if (status == 0) {
/* disable card insert interrupt while changing bus width to avoid false triggers */
SDHCI_REG_SET(SDHCI_SRS13, (irq_restore & ~SDHCI_SRS13_CINT_SE));
status = sdcard_set_bus_width(4);
}
if (status == 0) {
/* Get SCR registers - 8 bytes */
uint32_t scr_reg[SCR_REG_DATA_SIZE/sizeof(uint32_t)];
status = sdhci_read(SD_ACMD51_SEND_SCR, 0, scr_reg,
sizeof(scr_reg));
}
if (status == 0) {
/* set UHS mode to SDR25 and driver strength to Type B */
uint32_t card_access_mode = SDCARD_SWITCH_ACCESS_MODE_SDR25;
status = sdcard_set_function(card_access_mode, 1);
if (status == 0) {
/* set driver strength */
reg = SDHCI_REG(SDHCI_SRS15);
reg &= ~SDHCI_SRS15_DSS_MASK;
reg |= SDHCI_SRS15_DSS_TYPE_B; /* default */
SDHCI_REG_SET(SDHCI_SRS15, reg);
/* enable high speed */
sdhci_reg_or(SDHCI_SRS10, SDHCI_SRS10_HSE);
/* set UHS mode */
reg = SDHCI_REG(SDHCI_SRS15);
reg &= ~SDHCI_SRS15_UMS_MASK;
reg |= SDHCI_SRS15_UMS_SDR25;
SDHCI_REG_SET(SDHCI_SRS15, reg);
}
}
if (status == 0) {
sdhci_set_clock(SDHCI_CLK_50MHZ);
}
SDHCI_REG_SET(SDHCI_SRS13, irq_restore); /* re-enable interrupt */
return status;
}
/* Set SD card bus width using ACMD6 */
static int sdcard_set_bus_width(uint32_t bus_width)
{
int status;
if (bus_width == g_bus_width) {
/* nothing to do */
return 0;
}
/* set bus width */
status = sdhci_cmd(SD_CMD55_APP_CMD, g_rca << SD_RCA_SHIFT,
SDHCI_RESP_R1);
if (status == 0) {
uint32_t cmd_arg = (bus_width == 4) ? 2 : 0;
status = sdhci_cmd(SD_ACMD6_SET_BUS_WIDTH, cmd_arg, SDHCI_RESP_R1);
if (status == 0) {
/* change host bus width */
if (bus_width == 4) {
sdhci_reg_or(SDHCI_SRS10, SDHCI_SRS10_DTW);
}
else {
sdhci_reg_and(SDHCI_SRS10, ~SDHCI_SRS10_DTW);
}
}
}
return status;
}
/* Check or set SD card switch function/group
* Returns 0 if supported */
static int sdcard_send_switch_function(uint32_t mode, uint32_t function_number,
uint32_t group_number)
{
int status;
uint32_t timeout = 4;
uint32_t cmd_arg;
uint32_t func_status[64/sizeof(uint32_t)]; /* fixed 512 bits */
uint8_t* p_func_status = (uint8_t*)func_status;
if (group_number > 6 || function_number > 15) {
return -1; /* Invalid group or function number */
}
cmd_arg = (function_number << ((group_number - 1) * 4));
do {
/* first run check to see if function is supported */
status = sdhci_read(SD_CMD6_SWITCH_FUNC,
(mode | cmd_arg),
func_status, sizeof(func_status));
if (status == 0) {
/* check if busy */
/* data structure version 368:375
* (0=supported only, 1=supported and busy) */
if (p_func_status[17] == 1) {
/* busy status: group 1 272:287 */
if ((p_func_status[29 -
((group_number-1)*2)] & (1 << function_number))) {
continue; /* busy */
}
}
/* supported: group 1 415:400 */
if ((p_func_status[13 -
((group_number-1)*2)] & (1 << function_number))) {
status = 0; /* supported */
}
else {
status = -1; /* not supported */
}
break;
}
} while (status == 0 && --timeout > 0); /* retry until function not busy */
return status;
}
/* Set SD card function (check first, then switch) */
static int sdcard_set_function(uint32_t function_number, uint32_t group_number)
{
/* send check first */
int status = sdcard_send_switch_function(SDCARD_SWITCH_FUNC_MODE_CHECK,
function_number, group_number);
if (status == 0) {
/* send switch function */
status = sdcard_send_switch_function(SDCARD_SWITCH_FUNC_MODE_SWITCH,
function_number, group_number);
}
return status;
}
#endif /* DISK_SDCARD */
/* ============================================================================
* eMMC Specific Functions
* ============================================================================ */
#ifdef DISK_EMMC
/* Send CMD1 (SEND_OP_COND) for eMMC and wait for device ready
* Returns 0 on success, negative on error */
static int emmc_send_op_cond(uint32_t ocr_arg, uint32_t *ocr_reg)
{
int status;
uint32_t timeout = 1000; /* retry count */
uint32_t response;
/* First CMD1 to query supported voltages */
status = sdhci_cmd(MMC_CMD1_SEND_OP_COND, 0, SDHCI_RESP_R3);
if (status != 0) {
wolfBoot_printf("eMMC: CMD1 query failed\n");
return status;
}
/* Get OCR from response */
response = SDHCI_REG(SDHCI_SRS04);
#ifdef DEBUG_SDHCI
wolfBoot_printf("eMMC: Initial OCR: 0x%08X\n", response);
#endif
/* Loop sending CMD1 with operating conditions until device is ready */
do {
status = sdhci_cmd(MMC_CMD1_SEND_OP_COND, ocr_arg, SDHCI_RESP_R3);
if (status != 0) {
wolfBoot_printf("eMMC: CMD1 failed\n");
return status;
}
response = SDHCI_REG(SDHCI_SRS04);
/* Check if device is ready (busy bit cleared = ready) */
if (response & MMC_OCR_BUSY_BIT) {
/* Device is ready */
if (ocr_reg != NULL) {
*ocr_reg = response;
}
#ifdef DEBUG_SDHCI
wolfBoot_printf("eMMC: Device ready, OCR: 0x%08X\n", response);
#endif
return 0;
}
/* Small delay between retries */
udelay(10);
} while (--timeout > 0);
wolfBoot_printf("eMMC: Timeout waiting for device ready\n");
return -1;
}
/* Set eMMC bus width using CMD6 SWITCH command
* width: MMC_EXT_CSD_WIDTH_1BIT, MMC_EXT_CSD_WIDTH_4BIT, or MMC_EXT_CSD_WIDTH_8BIT
* Returns 0 on success */
static int emmc_set_bus_width(uint32_t width)
{
int status;
uint32_t cmd_arg;
/* Build CMD6 argument: Access=Write Byte, Index=183 (BUS_WIDTH), Value=width */
cmd_arg = MMC_DW_CSD | (width << 8);
#ifdef DEBUG_SDHCI
wolfBoot_printf("eMMC: Setting bus width to %d (CMD6 arg: 0x%08X)\n",
(width == MMC_EXT_CSD_WIDTH_4BIT) ? 4 :