2 This driver is used to manage SD/MMC PCI host controllers which are compliance
3 with SD Host Controller Simplified Specification version 3.00.
5 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.
7 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "SdMmcPciHcDxe.h"
21 Dump the content of SD/MMC host controller's Capability Register.
23 @param[in] Slot The slot number of the SD card to send the command to.
24 @param[in] Capability The buffer to store the capability data.
30 IN SD_MMC_HC_SLOT_CAP
*Capability
34 // Dump Capability Data
36 DEBUG ((DEBUG_INFO
, " == Slot [%d] Capability is 0x%x ==\n", Slot
, Capability
));
37 DEBUG ((DEBUG_INFO
, " Timeout Clk Freq %d%a\n", Capability
->TimeoutFreq
, (Capability
->TimeoutUnit
) ? "MHz" : "KHz"));
38 DEBUG ((DEBUG_INFO
, " Base Clk Freq %dMHz\n", Capability
->BaseClkFreq
));
39 DEBUG ((DEBUG_INFO
, " Max Blk Len %dbytes\n", 512 * (1 << Capability
->MaxBlkLen
)));
40 DEBUG ((DEBUG_INFO
, " 8-bit Support %a\n", Capability
->BusWidth8
? "TRUE" : "FALSE"));
41 DEBUG ((DEBUG_INFO
, " ADMA2 Support %a\n", Capability
->Adma2
? "TRUE" : "FALSE"));
42 DEBUG ((DEBUG_INFO
, " HighSpeed Support %a\n", Capability
->HighSpeed
? "TRUE" : "FALSE"));
43 DEBUG ((DEBUG_INFO
, " SDMA Support %a\n", Capability
->Sdma
? "TRUE" : "FALSE"));
44 DEBUG ((DEBUG_INFO
, " Suspend/Resume %a\n", Capability
->SuspRes
? "TRUE" : "FALSE"));
45 DEBUG ((DEBUG_INFO
, " Voltage 3.3 %a\n", Capability
->Voltage33
? "TRUE" : "FALSE"));
46 DEBUG ((DEBUG_INFO
, " Voltage 3.0 %a\n", Capability
->Voltage30
? "TRUE" : "FALSE"));
47 DEBUG ((DEBUG_INFO
, " Voltage 1.8 %a\n", Capability
->Voltage18
? "TRUE" : "FALSE"));
48 DEBUG ((DEBUG_INFO
, " 64-bit Sys Bus %a\n", Capability
->SysBus64
? "TRUE" : "FALSE"));
49 DEBUG ((DEBUG_INFO
, " Async Interrupt %a\n", Capability
->AsyncInt
? "TRUE" : "FALSE"));
50 DEBUG ((DEBUG_INFO
, " SlotType "));
51 if (Capability
->SlotType
== 0x00) {
52 DEBUG ((DEBUG_INFO
, "%a\n", "Removable Slot"));
53 } else if (Capability
->SlotType
== 0x01) {
54 DEBUG ((DEBUG_INFO
, "%a\n", "Embedded Slot"));
55 } else if (Capability
->SlotType
== 0x02) {
56 DEBUG ((DEBUG_INFO
, "%a\n", "Shared Bus Slot"));
58 DEBUG ((DEBUG_INFO
, "%a\n", "Reserved"));
60 DEBUG ((DEBUG_INFO
, " SDR50 Support %a\n", Capability
->Sdr50
? "TRUE" : "FALSE"));
61 DEBUG ((DEBUG_INFO
, " SDR104 Support %a\n", Capability
->Sdr104
? "TRUE" : "FALSE"));
62 DEBUG ((DEBUG_INFO
, " DDR50 Support %a\n", Capability
->Ddr50
? "TRUE" : "FALSE"));
63 DEBUG ((DEBUG_INFO
, " Driver Type A %a\n", Capability
->DriverTypeA
? "TRUE" : "FALSE"));
64 DEBUG ((DEBUG_INFO
, " Driver Type C %a\n", Capability
->DriverTypeC
? "TRUE" : "FALSE"));
65 DEBUG ((DEBUG_INFO
, " Driver Type D %a\n", Capability
->DriverTypeD
? "TRUE" : "FALSE"));
66 DEBUG ((DEBUG_INFO
, " Driver Type 4 %a\n", Capability
->DriverType4
? "TRUE" : "FALSE"));
67 if (Capability
->TimerCount
== 0) {
68 DEBUG ((DEBUG_INFO
, " Retuning TimerCnt Disabled\n", 2 * (Capability
->TimerCount
- 1)));
70 DEBUG ((DEBUG_INFO
, " Retuning TimerCnt %dseconds\n", 2 * (Capability
->TimerCount
- 1)));
72 DEBUG ((DEBUG_INFO
, " SDR50 Tuning %a\n", Capability
->TuningSDR50
? "TRUE" : "FALSE"));
73 DEBUG ((DEBUG_INFO
, " Retuning Mode Mode %d\n", Capability
->RetuningMod
+ 1));
74 DEBUG ((DEBUG_INFO
, " Clock Multiplier M = %d\n", Capability
->ClkMultiplier
+ 1));
75 DEBUG ((DEBUG_INFO
, " HS 400 %a\n", Capability
->Hs400
? "TRUE" : "FALSE"));
80 Read SlotInfo register from SD/MMC host controller pci config space.
82 @param[in] PciIo The PCI IO protocol instance.
83 @param[out] FirstBar The buffer to store the first BAR value.
84 @param[out] SlotNum The buffer to store the supported slot number.
86 @retval EFI_SUCCESS The operation succeeds.
87 @retval Others The operation fails.
93 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
99 SD_MMC_HC_SLOT_INFO SlotInfo
;
101 Status
= PciIo
->Pci
.Read (
104 SD_MMC_HC_SLOT_OFFSET
,
108 if (EFI_ERROR (Status
)) {
112 *FirstBar
= SlotInfo
.FirstBar
;
113 *SlotNum
= SlotInfo
.SlotNum
+ 1;
114 ASSERT ((*FirstBar
+ *SlotNum
) < SD_MMC_HC_MAX_SLOT
);
119 Read/Write specified SD/MMC host controller mmio register.
121 @param[in] PciIo The PCI IO protocol instance.
122 @param[in] BarIndex The BAR index of the standard PCI Configuration
123 header to use as the base address for the memory
124 operation to perform.
125 @param[in] Offset The offset within the selected BAR to start the
127 @param[in] Read A boolean to indicate it's read or write operation.
128 @param[in] Count The width of the mmio register in bytes.
129 Must be 1, 2 , 4 or 8 bytes.
130 @param[in, out] Data For read operations, the destination buffer to store
131 the results. For write operations, the source buffer
132 to write data from. The caller is responsible for
133 having ownership of the data buffer and ensuring its
134 size not less than Count bytes.
136 @retval EFI_INVALID_PARAMETER The PciIo or Data is NULL or the Count is not valid.
137 @retval EFI_SUCCESS The read/write operation succeeds.
138 @retval Others The read/write operation fails.
144 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
154 if ((PciIo
== NULL
) || (Data
== NULL
)) {
155 return EFI_INVALID_PARAMETER
;
158 if ((Count
!= 1) && (Count
!= 2) && (Count
!= 4) && (Count
!= 8)) {
159 return EFI_INVALID_PARAMETER
;
163 Status
= PciIo
->Mem
.Read (
172 Status
= PciIo
->Mem
.Write (
186 Do OR operation with the value of the specified SD/MMC host controller mmio register.
188 @param[in] PciIo The PCI IO protocol instance.
189 @param[in] BarIndex The BAR index of the standard PCI Configuration
190 header to use as the base address for the memory
191 operation to perform.
192 @param[in] Offset The offset within the selected BAR to start the
194 @param[in] Count The width of the mmio register in bytes.
195 Must be 1, 2 , 4 or 8 bytes.
196 @param[in] OrData The pointer to the data used to do OR operation.
197 The caller is responsible for having ownership of
198 the data buffer and ensuring its size not less than
201 @retval EFI_INVALID_PARAMETER The PciIo or OrData is NULL or the Count is not valid.
202 @retval EFI_SUCCESS The OR operation succeeds.
203 @retval Others The OR operation fails.
209 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
220 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
221 if (EFI_ERROR (Status
)) {
226 Or
= *(UINT8
*) OrData
;
227 } else if (Count
== 2) {
228 Or
= *(UINT16
*) OrData
;
229 } else if (Count
== 4) {
230 Or
= *(UINT32
*) OrData
;
231 } else if (Count
== 8) {
232 Or
= *(UINT64
*) OrData
;
234 return EFI_INVALID_PARAMETER
;
238 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
244 Do AND operation with the value of the specified SD/MMC host controller mmio register.
246 @param[in] PciIo The PCI IO protocol instance.
247 @param[in] BarIndex The BAR index of the standard PCI Configuration
248 header to use as the base address for the memory
249 operation to perform.
250 @param[in] Offset The offset within the selected BAR to start the
252 @param[in] Count The width of the mmio register in bytes.
253 Must be 1, 2 , 4 or 8 bytes.
254 @param[in] AndData The pointer to the data used to do AND operation.
255 The caller is responsible for having ownership of
256 the data buffer and ensuring its size not less than
259 @retval EFI_INVALID_PARAMETER The PciIo or AndData is NULL or the Count is not valid.
260 @retval EFI_SUCCESS The AND operation succeeds.
261 @retval Others The AND operation fails.
267 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
278 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
279 if (EFI_ERROR (Status
)) {
284 And
= *(UINT8
*) AndData
;
285 } else if (Count
== 2) {
286 And
= *(UINT16
*) AndData
;
287 } else if (Count
== 4) {
288 And
= *(UINT32
*) AndData
;
289 } else if (Count
== 8) {
290 And
= *(UINT64
*) AndData
;
292 return EFI_INVALID_PARAMETER
;
296 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
302 Wait for the value of the specified MMIO register set to the test value.
304 @param[in] PciIo The PCI IO protocol instance.
305 @param[in] BarIndex The BAR index of the standard PCI Configuration
306 header to use as the base address for the memory
307 operation to perform.
308 @param[in] Offset The offset within the selected BAR to start the
310 @param[in] Count The width of the mmio register in bytes.
311 Must be 1, 2, 4 or 8 bytes.
312 @param[in] MaskValue The mask value of memory.
313 @param[in] TestValue The test value of memory.
315 @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.
316 @retval EFI_SUCCESS The MMIO register has expected value.
317 @retval Others The MMIO operation fails.
322 SdMmcHcCheckMmioSet (
323 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
335 // Access PCI MMIO space to see if the value is the tested one.
338 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Value
);
339 if (EFI_ERROR (Status
)) {
345 if (Value
== TestValue
) {
349 return EFI_NOT_READY
;
353 Wait for the value of the specified MMIO register set to the test value.
355 @param[in] PciIo The PCI IO protocol instance.
356 @param[in] BarIndex The BAR index of the standard PCI Configuration
357 header to use as the base address for the memory
358 operation to perform.
359 @param[in] Offset The offset within the selected BAR to start the
361 @param[in] Count The width of the mmio register in bytes.
362 Must be 1, 2, 4 or 8 bytes.
363 @param[in] MaskValue The mask value of memory.
364 @param[in] TestValue The test value of memory.
365 @param[in] Timeout The time out value for wait memory set, uses 1
366 microsecond as a unit.
368 @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout
370 @retval EFI_SUCCESS The MMIO register has expected value.
371 @retval Others The MMIO operation fails.
377 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
387 BOOLEAN InfiniteWait
;
392 InfiniteWait
= FALSE
;
395 while (InfiniteWait
|| (Timeout
> 0)) {
396 Status
= SdMmcHcCheckMmioSet (
404 if (Status
!= EFI_NOT_READY
) {
409 // Stall for 1 microsecond.
420 Software reset the specified SD/MMC host controller and enable all interrupts.
422 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
423 @param[in] Slot The slot number of the SD card to send the command to.
425 @retval EFI_SUCCESS The software reset executes successfully.
426 @retval Others The software reset fails.
431 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
437 EFI_PCI_IO_PROTOCOL
*PciIo
;
440 // Notify the SD/MMC override protocol that we are about to reset
441 // the SD/MMC host controller.
443 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
444 Status
= mOverride
->NotifyPhase (
445 Private
->ControllerHandle
,
448 if (EFI_ERROR (Status
)) {
450 "%a: SD/MMC pre reset notifier callback failed - %r\n",
451 __FUNCTION__
, Status
));
456 PciIo
= Private
->PciIo
;
458 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_SW_RST
, FALSE
, sizeof (SwReset
), &SwReset
);
460 if (EFI_ERROR (Status
)) {
461 DEBUG ((DEBUG_ERROR
, "SdMmcHcReset: write full 1 fails: %r\n", Status
));
465 Status
= SdMmcHcWaitMmioSet (
472 SD_MMC_HC_GENERIC_TIMEOUT
474 if (EFI_ERROR (Status
)) {
475 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: reset done with %r\n", Status
));
480 // Enable all interrupt after reset all.
482 Status
= SdMmcHcEnableInterrupt (PciIo
, Slot
);
483 if (EFI_ERROR (Status
)) {
484 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",
490 // Notify the SD/MMC override protocol that we have just reset
491 // the SD/MMC host controller.
493 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
494 Status
= mOverride
->NotifyPhase (
495 Private
->ControllerHandle
,
497 EdkiiSdMmcResetPost
);
498 if (EFI_ERROR (Status
)) {
500 "%a: SD/MMC post reset notifier callback failed - %r\n",
501 __FUNCTION__
, Status
));
509 Set all interrupt status bits in Normal and Error Interrupt Status Enable
512 @param[in] PciIo The PCI IO protocol instance.
513 @param[in] Slot The slot number of the SD card to send the command to.
515 @retval EFI_SUCCESS The operation executes successfully.
516 @retval Others The operation fails.
520 SdMmcHcEnableInterrupt (
521 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
529 // Enable all bits in Error Interrupt Status Enable Register
532 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_ERR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
533 if (EFI_ERROR (Status
)) {
537 // Enable all bits in Normal Interrupt Status Enable Register
540 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
546 Get the capability data from the specified slot.
548 @param[in] PciIo The PCI IO protocol instance.
549 @param[in] Slot The slot number of the SD card to send the command to.
550 @param[out] Capability The buffer to store the capability data.
552 @retval EFI_SUCCESS The operation executes successfully.
553 @retval Others The operation fails.
557 SdMmcHcGetCapability (
558 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
560 OUT SD_MMC_HC_SLOT_CAP
*Capability
566 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CAP
, TRUE
, sizeof (Cap
), &Cap
);
567 if (EFI_ERROR (Status
)) {
571 CopyMem (Capability
, &Cap
, sizeof (Cap
));
577 Get the maximum current capability data from the specified slot.
579 @param[in] PciIo The PCI IO protocol instance.
580 @param[in] Slot The slot number of the SD card to send the command to.
581 @param[out] MaxCurrent The buffer to store the maximum current capability data.
583 @retval EFI_SUCCESS The operation executes successfully.
584 @retval Others The operation fails.
588 SdMmcHcGetMaxCurrent (
589 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
591 OUT UINT64
*MaxCurrent
596 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_MAX_CURRENT_CAP
, TRUE
, sizeof (UINT64
), MaxCurrent
);
602 Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller
605 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
607 @param[in] PciIo The PCI IO protocol instance.
608 @param[in] Slot The slot number of the SD card to send the command to.
609 @param[out] MediaPresent The pointer to the media present boolean value.
611 @retval EFI_SUCCESS There is no media change happened.
612 @retval EFI_MEDIA_CHANGED There is media change happened.
613 @retval Others The detection fails.
618 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
620 OUT BOOLEAN
*MediaPresent
628 // Check Present State Register to see if there is a card presented.
630 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_PRESENT_STATE
, TRUE
, sizeof (PresentState
), &PresentState
);
631 if (EFI_ERROR (Status
)) {
635 if ((PresentState
& BIT16
) != 0) {
636 *MediaPresent
= TRUE
;
638 *MediaPresent
= FALSE
;
642 // Check Normal Interrupt Status Register
644 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, TRUE
, sizeof (Data
), &Data
);
645 if (EFI_ERROR (Status
)) {
649 if ((Data
& (BIT6
| BIT7
)) != 0) {
651 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
654 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data
), &Data
);
655 if (EFI_ERROR (Status
)) {
659 return EFI_MEDIA_CHANGED
;
666 Stop SD/MMC card clock.
668 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
670 @param[in] PciIo The PCI IO protocol instance.
671 @param[in] Slot The slot number of the SD card to send the command to.
673 @retval EFI_SUCCESS Succeed to stop SD/MMC clock.
674 @retval Others Fail to stop SD/MMC clock.
679 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
688 // Ensure no SD transactions are occurring on the SD Bus by
689 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
690 // in the Present State register to be 0.
692 Status
= SdMmcHcWaitMmioSet (
695 SD_MMC_HC_PRESENT_STATE
,
696 sizeof (PresentState
),
699 SD_MMC_HC_GENERIC_TIMEOUT
701 if (EFI_ERROR (Status
)) {
706 // Set SD Clock Enable in the Clock Control register to 0
708 ClockCtrl
= (UINT16
)~BIT2
;
709 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
715 SD/MMC card clock supply.
717 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
719 @param[in] PciIo The PCI IO protocol instance.
720 @param[in] Slot The slot number of the SD card to send the command to.
721 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
722 @param[in] Capability The capability of the slot.
724 @retval EFI_SUCCESS The clock is supplied successfully.
725 @retval Others The clock isn't supplied successfully.
730 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
733 IN SD_MMC_HC_SLOT_CAP Capability
741 UINT16 ControllerVer
;
745 // Calculate a divisor for SD clock frequency
747 ASSERT (Capability
.BaseClkFreq
!= 0);
749 BaseClkFreq
= Capability
.BaseClkFreq
;
750 if (ClockFreq
== 0) {
751 return EFI_INVALID_PARAMETER
;
754 if (ClockFreq
> (BaseClkFreq
* 1000)) {
755 ClockFreq
= BaseClkFreq
* 1000;
759 // Calculate the divisor of base frequency.
762 SettingFreq
= BaseClkFreq
* 1000;
763 while (ClockFreq
< SettingFreq
) {
766 SettingFreq
= (BaseClkFreq
* 1000) / (2 * Divisor
);
767 Remainder
= (BaseClkFreq
* 1000) % (2 * Divisor
);
768 if ((ClockFreq
== SettingFreq
) && (Remainder
== 0)) {
771 if ((ClockFreq
== SettingFreq
) && (Remainder
!= 0)) {
776 DEBUG ((DEBUG_INFO
, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq
, Divisor
, ClockFreq
));
778 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CTRL_VER
, TRUE
, sizeof (ControllerVer
), &ControllerVer
);
779 if (EFI_ERROR (Status
)) {
783 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
785 if ((ControllerVer
& 0xFF) == 2) {
786 ASSERT (Divisor
<= 0x3FF);
787 ClockCtrl
= ((Divisor
& 0xFF) << 8) | ((Divisor
& 0x300) >> 2);
788 } else if (((ControllerVer
& 0xFF) == 0) || ((ControllerVer
& 0xFF) == 1)) {
790 // Only the most significant bit can be used as divisor.
792 if (((Divisor
- 1) & Divisor
) != 0) {
793 Divisor
= 1 << (HighBitSet32 (Divisor
) + 1);
795 ASSERT (Divisor
<= 0x80);
796 ClockCtrl
= (Divisor
& 0xFF) << 8;
798 DEBUG ((DEBUG_ERROR
, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer
));
799 return EFI_UNSUPPORTED
;
803 // Stop bus clock at first
805 Status
= SdMmcHcStopClock (PciIo
, Slot
);
806 if (EFI_ERROR (Status
)) {
811 // Supply clock frequency with specified divisor
814 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, FALSE
, sizeof (ClockCtrl
), &ClockCtrl
);
815 if (EFI_ERROR (Status
)) {
816 DEBUG ((DEBUG_ERROR
, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
821 // Wait Internal Clock Stable in the Clock Control register to be 1
823 Status
= SdMmcHcWaitMmioSet (
826 SD_MMC_HC_CLOCK_CTRL
,
830 SD_MMC_HC_GENERIC_TIMEOUT
832 if (EFI_ERROR (Status
)) {
837 // Set SD Clock Enable in the Clock Control register to 1
840 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
846 SD/MMC bus power control.
848 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
850 @param[in] PciIo The PCI IO protocol instance.
851 @param[in] Slot The slot number of the SD card to send the command to.
852 @param[in] PowerCtrl The value setting to the power control register.
854 @retval TRUE There is a SD/MMC card attached.
855 @retval FALSE There is no a SD/MMC card attached.
859 SdMmcHcPowerControl (
860 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
870 PowerCtrl
&= (UINT8
)~BIT0
;
871 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
872 if (EFI_ERROR (Status
)) {
877 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
880 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
886 Set the SD/MMC bus width.
888 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
890 @param[in] PciIo The PCI IO protocol instance.
891 @param[in] Slot The slot number of the SD card to send the command to.
892 @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.
894 @retval EFI_SUCCESS The bus width is set successfully.
895 @retval Others The bus width isn't set successfully.
900 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
909 HostCtrl1
= (UINT8
)~(BIT5
| BIT1
);
910 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
911 } else if (BusWidth
== 4) {
912 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
913 if (EFI_ERROR (Status
)) {
917 HostCtrl1
&= (UINT8
)~BIT5
;
918 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
919 } else if (BusWidth
== 8) {
920 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
921 if (EFI_ERROR (Status
)) {
924 HostCtrl1
&= (UINT8
)~BIT1
;
926 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
929 return EFI_INVALID_PARAMETER
;
936 Supply SD/MMC card with lowest clock frequency at initialization.
938 @param[in] PciIo The PCI IO protocol instance.
939 @param[in] Slot The slot number of the SD card to send the command to.
940 @param[in] Capability The capability of the slot.
942 @retval EFI_SUCCESS The clock is supplied successfully.
943 @retval Others The clock isn't supplied successfully.
947 SdMmcHcInitClockFreq (
948 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
950 IN SD_MMC_HC_SLOT_CAP Capability
957 // Calculate a divisor for SD clock frequency
959 if (Capability
.BaseClkFreq
== 0) {
961 // Don't support get Base Clock Frequency information via another method
963 return EFI_UNSUPPORTED
;
966 // Supply 400KHz clock frequency at initialization phase.
969 Status
= SdMmcHcClockSupply (PciIo
, Slot
, InitFreq
, Capability
);
974 Supply SD/MMC card with maximum voltage at initialization.
976 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
978 @param[in] PciIo The PCI IO protocol instance.
979 @param[in] Slot The slot number of the SD card to send the command to.
980 @param[in] Capability The capability of the slot.
982 @retval EFI_SUCCESS The voltage is supplied successfully.
983 @retval Others The voltage isn't supplied successfully.
987 SdMmcHcInitPowerVoltage (
988 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
990 IN SD_MMC_HC_SLOT_CAP Capability
998 // Calculate supported maximum voltage according to SD Bus Voltage Select
1000 if (Capability
.Voltage33
!= 0) {
1005 } else if (Capability
.Voltage30
!= 0) {
1010 } else if (Capability
.Voltage18
!= 0) {
1016 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1018 if (EFI_ERROR (Status
)) {
1023 return EFI_DEVICE_ERROR
;
1027 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
1029 Status
= SdMmcHcPowerControl (PciIo
, Slot
, MaxVoltage
);
1035 Initialize the Timeout Control register with most conservative value at initialization.
1037 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
1039 @param[in] PciIo The PCI IO protocol instance.
1040 @param[in] Slot The slot number of the SD card to send the command to.
1042 @retval EFI_SUCCESS The timeout control register is configured successfully.
1043 @retval Others The timeout control register isn't configured successfully.
1047 SdMmcHcInitTimeoutCtrl (
1048 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1056 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_TIMEOUT_CTRL
, FALSE
, sizeof (Timeout
), &Timeout
);
1062 Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value
1065 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1066 @param[in] Slot The slot number of the SD card to send the command to.
1068 @retval EFI_SUCCESS The host controller is initialized successfully.
1069 @retval Others The host controller isn't initialized successfully.
1074 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1079 EFI_PCI_IO_PROTOCOL
*PciIo
;
1080 SD_MMC_HC_SLOT_CAP Capability
;
1083 // Notify the SD/MMC override protocol that we are about to initialize
1084 // the SD/MMC host controller.
1086 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1087 Status
= mOverride
->NotifyPhase (
1088 Private
->ControllerHandle
,
1090 EdkiiSdMmcInitHostPre
);
1091 if (EFI_ERROR (Status
)) {
1093 "%a: SD/MMC pre init notifier callback failed - %r\n",
1094 __FUNCTION__
, Status
));
1099 PciIo
= Private
->PciIo
;
1100 Capability
= Private
->Capability
[Slot
];
1102 Status
= SdMmcHcInitClockFreq (PciIo
, Slot
, Capability
);
1103 if (EFI_ERROR (Status
)) {
1107 Status
= SdMmcHcInitPowerVoltage (PciIo
, Slot
, Capability
);
1108 if (EFI_ERROR (Status
)) {
1112 Status
= SdMmcHcInitTimeoutCtrl (PciIo
, Slot
);
1113 if (EFI_ERROR (Status
)) {
1118 // Notify the SD/MMC override protocol that we are have just initialized
1119 // the SD/MMC host controller.
1121 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1122 Status
= mOverride
->NotifyPhase (
1123 Private
->ControllerHandle
,
1125 EdkiiSdMmcInitHostPost
);
1126 if (EFI_ERROR (Status
)) {
1128 "%a: SD/MMC post init notifier callback failed - %r\n",
1129 __FUNCTION__
, Status
));
1138 @param[in] PciIo The PCI IO protocol instance.
1139 @param[in] Slot The slot number of the SD card to send the command to.
1140 @param[in] On The boolean to turn on/off LED.
1142 @retval EFI_SUCCESS The LED is turned on/off successfully.
1143 @retval Others The LED isn't turned on/off successfully.
1148 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1158 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1160 HostCtrl1
= (UINT8
)~BIT0
;
1161 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1168 Build ADMA descriptor table for transfer.
1170 Refer to SD Host Controller Simplified spec 3.0 Section 1.13 for details.
1172 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1174 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
1175 @retval Others The ADMA descriptor table isn't created successfully.
1179 BuildAdmaDescTable (
1180 IN SD_MMC_HC_TRB
*Trb
1183 EFI_PHYSICAL_ADDRESS Data
;
1190 EFI_PCI_IO_PROTOCOL
*PciIo
;
1194 Data
= Trb
->DataPhy
;
1195 DataLen
= Trb
->DataLen
;
1196 PciIo
= Trb
->Private
->PciIo
;
1198 // Only support 32bit ADMA Descriptor Table
1200 if ((Data
>= 0x100000000ul
) || ((Data
+ DataLen
) > 0x100000000ul
)) {
1201 return EFI_INVALID_PARAMETER
;
1204 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
1205 // for 32-bit address descriptor table.
1207 if ((Data
& (BIT0
| BIT1
)) != 0) {
1208 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data
));
1211 Entries
= DivU64x32 ((DataLen
+ ADMA_MAX_DATA_PER_LINE
- 1), ADMA_MAX_DATA_PER_LINE
);
1212 TableSize
= (UINTN
)MultU64x32 (Entries
, sizeof (SD_MMC_HC_ADMA_DESC_LINE
));
1213 Trb
->AdmaPages
= (UINT32
)EFI_SIZE_TO_PAGES (TableSize
);
1214 Status
= PciIo
->AllocateBuffer (
1217 EfiBootServicesData
,
1218 EFI_SIZE_TO_PAGES (TableSize
),
1219 (VOID
**)&Trb
->AdmaDesc
,
1222 if (EFI_ERROR (Status
)) {
1223 return EFI_OUT_OF_RESOURCES
;
1225 ZeroMem (Trb
->AdmaDesc
, TableSize
);
1227 Status
= PciIo
->Map (
1229 EfiPciIoOperationBusMasterCommonBuffer
,
1236 if (EFI_ERROR (Status
) || (Bytes
!= TableSize
)) {
1238 // Map error or unable to map the whole RFis buffer into a contiguous region.
1242 EFI_SIZE_TO_PAGES (TableSize
),
1245 return EFI_OUT_OF_RESOURCES
;
1248 if ((UINT64
)(UINTN
)Trb
->AdmaDescPhy
> 0x100000000ul
) {
1250 // The ADMA doesn't support 64bit addressing.
1258 EFI_SIZE_TO_PAGES (TableSize
),
1261 return EFI_DEVICE_ERROR
;
1264 Remaining
= DataLen
;
1265 Address
= (UINT32
)Data
;
1266 for (Index
= 0; Index
< Entries
; Index
++) {
1267 if (Remaining
<= ADMA_MAX_DATA_PER_LINE
) {
1268 Trb
->AdmaDesc
[Index
].Valid
= 1;
1269 Trb
->AdmaDesc
[Index
].Act
= 2;
1270 Trb
->AdmaDesc
[Index
].Length
= (UINT16
)Remaining
;
1271 Trb
->AdmaDesc
[Index
].Address
= Address
;
1274 Trb
->AdmaDesc
[Index
].Valid
= 1;
1275 Trb
->AdmaDesc
[Index
].Act
= 2;
1276 Trb
->AdmaDesc
[Index
].Length
= 0;
1277 Trb
->AdmaDesc
[Index
].Address
= Address
;
1280 Remaining
-= ADMA_MAX_DATA_PER_LINE
;
1281 Address
+= ADMA_MAX_DATA_PER_LINE
;
1285 // Set the last descriptor line as end of descriptor table
1287 Trb
->AdmaDesc
[Index
].End
= 1;
1292 Create a new TRB for the SD/MMC cmd request.
1294 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1295 @param[in] Slot The slot number of the SD card to send the command to.
1296 @param[in] Packet A pointer to the SD command data structure.
1297 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1298 not NULL, then nonblocking I/O is performed, and Event
1299 will be signaled when the Packet completes.
1301 @return Created Trb or NULL.
1306 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1308 IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1315 EFI_PCI_IO_PROTOCOL_OPERATION Flag
;
1316 EFI_PCI_IO_PROTOCOL
*PciIo
;
1319 Trb
= AllocateZeroPool (sizeof (SD_MMC_HC_TRB
));
1324 Trb
->Signature
= SD_MMC_HC_TRB_SIG
;
1326 Trb
->BlockSize
= 0x200;
1327 Trb
->Packet
= Packet
;
1329 Trb
->Started
= FALSE
;
1330 Trb
->Timeout
= Packet
->Timeout
;
1331 Trb
->Private
= Private
;
1333 if ((Packet
->InTransferLength
!= 0) && (Packet
->InDataBuffer
!= NULL
)) {
1334 Trb
->Data
= Packet
->InDataBuffer
;
1335 Trb
->DataLen
= Packet
->InTransferLength
;
1337 } else if ((Packet
->OutTransferLength
!= 0) && (Packet
->OutDataBuffer
!= NULL
)) {
1338 Trb
->Data
= Packet
->OutDataBuffer
;
1339 Trb
->DataLen
= Packet
->OutTransferLength
;
1341 } else if ((Packet
->InTransferLength
== 0) && (Packet
->OutTransferLength
== 0)) {
1348 if ((Trb
->DataLen
!= 0) && (Trb
->DataLen
< Trb
->BlockSize
)) {
1349 Trb
->BlockSize
= (UINT16
)Trb
->DataLen
;
1352 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1353 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1354 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1355 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
1356 Trb
->Mode
= SdMmcPioMode
;
1359 Flag
= EfiPciIoOperationBusMasterWrite
;
1361 Flag
= EfiPciIoOperationBusMasterRead
;
1364 PciIo
= Private
->PciIo
;
1365 if (Trb
->DataLen
!= 0) {
1366 MapLength
= Trb
->DataLen
;
1367 Status
= PciIo
->Map (
1375 if (EFI_ERROR (Status
) || (Trb
->DataLen
!= MapLength
)) {
1376 Status
= EFI_BAD_BUFFER_SIZE
;
1381 if (Trb
->DataLen
== 0) {
1382 Trb
->Mode
= SdMmcNoData
;
1383 } else if (Private
->Capability
[Slot
].Adma2
!= 0) {
1384 Trb
->Mode
= SdMmcAdmaMode
;
1385 Status
= BuildAdmaDescTable (Trb
);
1386 if (EFI_ERROR (Status
)) {
1387 PciIo
->Unmap (PciIo
, Trb
->DataMap
);
1390 } else if (Private
->Capability
[Slot
].Sdma
!= 0) {
1391 Trb
->Mode
= SdMmcSdmaMode
;
1393 Trb
->Mode
= SdMmcPioMode
;
1397 if (Event
!= NULL
) {
1398 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1399 InsertTailList (&Private
->Queue
, &Trb
->TrbList
);
1400 gBS
->RestoreTPL (OldTpl
);
1411 Free the resource used by the TRB.
1413 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1418 IN SD_MMC_HC_TRB
*Trb
1421 EFI_PCI_IO_PROTOCOL
*PciIo
;
1423 PciIo
= Trb
->Private
->PciIo
;
1425 if (Trb
->AdmaMap
!= NULL
) {
1431 if (Trb
->AdmaDesc
!= NULL
) {
1438 if (Trb
->DataMap
!= NULL
) {
1449 Check if the env is ready for execute specified TRB.
1451 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1452 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1454 @retval EFI_SUCCESS The env is ready for TRB execution.
1455 @retval EFI_NOT_READY The env is not ready for TRB execution.
1456 @retval Others Some erros happen.
1461 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1462 IN SD_MMC_HC_TRB
*Trb
1466 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1467 EFI_PCI_IO_PROTOCOL
*PciIo
;
1468 UINT32 PresentState
;
1470 Packet
= Trb
->Packet
;
1472 if ((Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) ||
1473 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR1b
) ||
1474 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR5b
)) {
1476 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1477 // the Present State register to be 0
1479 PresentState
= BIT0
| BIT1
;
1482 // Wait Command Inhibit (CMD) in the Present State register
1485 PresentState
= BIT0
;
1488 PciIo
= Private
->PciIo
;
1489 Status
= SdMmcHcCheckMmioSet (
1492 SD_MMC_HC_PRESENT_STATE
,
1493 sizeof (PresentState
),
1502 Wait for the env to be ready for execute specified TRB.
1504 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1505 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1507 @retval EFI_SUCCESS The env is ready for TRB execution.
1508 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1509 @retval Others Some erros happen.
1514 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1515 IN SD_MMC_HC_TRB
*Trb
1519 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1521 BOOLEAN InfiniteWait
;
1524 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1526 Packet
= Trb
->Packet
;
1527 Timeout
= Packet
->Timeout
;
1529 InfiniteWait
= TRUE
;
1531 InfiniteWait
= FALSE
;
1534 while (InfiniteWait
|| (Timeout
> 0)) {
1536 // Check Trb execution result by reading Normal Interrupt Status register.
1538 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
1539 if (Status
!= EFI_NOT_READY
) {
1543 // Stall for 1 microsecond.
1554 Execute the specified TRB.
1556 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1557 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1559 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1560 @retval Others Some erros happen when sending this request to the host controller.
1565 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1566 IN SD_MMC_HC_TRB
*Trb
1570 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1571 EFI_PCI_IO_PROTOCOL
*PciIo
;
1582 Packet
= Trb
->Packet
;
1583 PciIo
= Trb
->Private
->PciIo
;
1585 // Clear all bits in Error Interrupt Status Register
1588 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ERR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1589 if (EFI_ERROR (Status
)) {
1593 // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.
1596 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1597 if (EFI_ERROR (Status
)) {
1601 // Set Host Control 1 register DMA Select field
1603 if (Trb
->Mode
== SdMmcAdmaMode
) {
1605 Status
= SdMmcHcOrMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1606 if (EFI_ERROR (Status
)) {
1611 SdMmcHcLedOnOff (PciIo
, Trb
->Slot
, TRUE
);
1613 if (Trb
->Mode
== SdMmcSdmaMode
) {
1614 if ((UINT64
)(UINTN
)Trb
->DataPhy
>= 0x100000000ul
) {
1615 return EFI_INVALID_PARAMETER
;
1618 SdmaAddr
= (UINT32
)(UINTN
)Trb
->DataPhy
;
1619 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (SdmaAddr
), &SdmaAddr
);
1620 if (EFI_ERROR (Status
)) {
1623 } else if (Trb
->Mode
== SdMmcAdmaMode
) {
1624 AdmaAddr
= (UINT64
)(UINTN
)Trb
->AdmaDescPhy
;
1625 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (AdmaAddr
), &AdmaAddr
);
1626 if (EFI_ERROR (Status
)) {
1631 BlkSize
= Trb
->BlockSize
;
1632 if (Trb
->Mode
== SdMmcSdmaMode
) {
1634 // Set SDMA boundary to be 512K bytes.
1639 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_SIZE
, FALSE
, sizeof (BlkSize
), &BlkSize
);
1640 if (EFI_ERROR (Status
)) {
1645 if (Trb
->Mode
!= SdMmcNoData
) {
1647 // Calcuate Block Count.
1649 BlkCount
= (UINT16
)(Trb
->DataLen
/ Trb
->BlockSize
);
1651 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_COUNT
, FALSE
, sizeof (BlkCount
), &BlkCount
);
1652 if (EFI_ERROR (Status
)) {
1656 Argument
= Packet
->SdMmcCmdBlk
->CommandArgument
;
1657 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ARG1
, FALSE
, sizeof (Argument
), &Argument
);
1658 if (EFI_ERROR (Status
)) {
1663 if (Trb
->Mode
!= SdMmcNoData
) {
1664 if (Trb
->Mode
!= SdMmcPioMode
) {
1671 TransMode
|= BIT5
| BIT1
;
1674 // Only SD memory card needs to use AUTO CMD12 feature.
1676 if (Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) {
1683 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_TRANS_MOD
, FALSE
, sizeof (TransMode
), &TransMode
);
1684 if (EFI_ERROR (Status
)) {
1688 Cmd
= (UINT16
)LShiftU64(Packet
->SdMmcCmdBlk
->CommandIndex
, 8);
1689 if (Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) {
1693 // Convert ResponseType to value
1695 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
1696 switch (Packet
->SdMmcCmdBlk
->ResponseType
) {
1697 case SdMmcResponseTypeR1
:
1698 case SdMmcResponseTypeR5
:
1699 case SdMmcResponseTypeR6
:
1700 case SdMmcResponseTypeR7
:
1701 Cmd
|= (BIT1
| BIT3
| BIT4
);
1703 case SdMmcResponseTypeR2
:
1704 Cmd
|= (BIT0
| BIT3
);
1706 case SdMmcResponseTypeR3
:
1707 case SdMmcResponseTypeR4
:
1710 case SdMmcResponseTypeR1b
:
1711 case SdMmcResponseTypeR5b
:
1712 Cmd
|= (BIT0
| BIT1
| BIT3
| BIT4
);
1722 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_COMMAND
, FALSE
, sizeof (Cmd
), &Cmd
);
1727 Check the TRB execution result.
1729 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1730 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1732 @retval EFI_SUCCESS The TRB is executed successfully.
1733 @retval EFI_NOT_READY The TRB is not completed for execution.
1734 @retval Others Some erros happen when executing this request.
1738 SdMmcCheckTrbResult (
1739 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1740 IN SD_MMC_HC_TRB
*Trb
1744 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1753 Packet
= Trb
->Packet
;
1755 // Check Trb execution result by reading Normal Interrupt Status register.
1757 Status
= SdMmcHcRwMmio (
1760 SD_MMC_HC_NOR_INT_STS
,
1765 if (EFI_ERROR (Status
)) {
1769 // Check Transfer Complete bit is set or not.
1771 if ((IntStatus
& BIT1
) == BIT1
) {
1772 if ((IntStatus
& BIT15
) == BIT15
) {
1774 // Read Error Interrupt Status register to check if the error is
1775 // Data Timeout Error.
1776 // If yes, treat it as success as Transfer Complete has higher
1777 // priority than Data Timeout Error.
1779 Status
= SdMmcHcRwMmio (
1782 SD_MMC_HC_ERR_INT_STS
,
1787 if (!EFI_ERROR (Status
)) {
1788 if ((IntStatus
& BIT4
) == BIT4
) {
1789 Status
= EFI_SUCCESS
;
1791 Status
= EFI_DEVICE_ERROR
;
1799 // Check if there is a error happened during cmd execution.
1800 // If yes, then do error recovery procedure to follow SD Host Controller
1801 // Simplified Spec 3.0 section 3.10.1.
1803 if ((IntStatus
& BIT15
) == BIT15
) {
1804 Status
= SdMmcHcRwMmio (
1807 SD_MMC_HC_ERR_INT_STS
,
1812 if (EFI_ERROR (Status
)) {
1815 if ((IntStatus
& 0x0F) != 0) {
1818 if ((IntStatus
& 0xF0) != 0) {
1822 Status
= SdMmcHcRwMmio (
1830 if (EFI_ERROR (Status
)) {
1833 Status
= SdMmcHcWaitMmioSet (
1840 SD_MMC_HC_GENERIC_TIMEOUT
1842 if (EFI_ERROR (Status
)) {
1846 Status
= EFI_DEVICE_ERROR
;
1850 // Check if DMA interrupt is signalled for the SDMA transfer.
1852 if ((Trb
->Mode
== SdMmcSdmaMode
) && ((IntStatus
& BIT3
) == BIT3
)) {
1854 // Clear DMA interrupt bit.
1857 Status
= SdMmcHcRwMmio (
1860 SD_MMC_HC_NOR_INT_STS
,
1865 if (EFI_ERROR (Status
)) {
1869 // Update SDMA Address register.
1871 SdmaAddr
= SD_MMC_SDMA_ROUND_UP ((UINT32
)(UINTN
)Trb
->DataPhy
, SD_MMC_SDMA_BOUNDARY
);
1872 Status
= SdMmcHcRwMmio (
1875 SD_MMC_HC_SDMA_ADDR
,
1880 if (EFI_ERROR (Status
)) {
1883 Trb
->DataPhy
= (UINT32
)(UINTN
)SdmaAddr
;
1886 if ((Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeAdtc
) &&
1887 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR1b
) &&
1888 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR5b
)) {
1889 if ((IntStatus
& BIT0
) == BIT0
) {
1890 Status
= EFI_SUCCESS
;
1895 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1896 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1897 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1898 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
1900 // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,
1901 // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.
1902 // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.
1904 if ((IntStatus
& BIT5
) == BIT5
) {
1906 // Clear Buffer Read Ready interrupt at first.
1909 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1911 // Read data out from Buffer Port register
1913 for (PioLength
= 0; PioLength
< Trb
->DataLen
; PioLength
+= 4) {
1914 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_BUF_DAT_PORT
, TRUE
, 4, (UINT8
*)Trb
->Data
+ PioLength
);
1916 Status
= EFI_SUCCESS
;
1921 Status
= EFI_NOT_READY
;
1924 // Get response data when the cmd is executed successfully.
1926 if (!EFI_ERROR (Status
)) {
1927 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
1928 for (Index
= 0; Index
< 4; Index
++) {
1929 Status
= SdMmcHcRwMmio (
1932 SD_MMC_HC_RESPONSE
+ Index
* 4,
1937 if (EFI_ERROR (Status
)) {
1938 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
1942 CopyMem (Packet
->SdMmcStatusBlk
, Response
, sizeof (Response
));
1946 if (Status
!= EFI_NOT_READY
) {
1947 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
1954 Wait for the TRB execution result.
1956 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1957 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1959 @retval EFI_SUCCESS The TRB is executed successfully.
1960 @retval Others Some erros happen when executing this request.
1964 SdMmcWaitTrbResult (
1965 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1966 IN SD_MMC_HC_TRB
*Trb
1970 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1972 BOOLEAN InfiniteWait
;
1974 Packet
= Trb
->Packet
;
1976 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1978 Timeout
= Packet
->Timeout
;
1980 InfiniteWait
= TRUE
;
1982 InfiniteWait
= FALSE
;
1985 while (InfiniteWait
|| (Timeout
> 0)) {
1987 // Check Trb execution result by reading Normal Interrupt Status register.
1989 Status
= SdMmcCheckTrbResult (Private
, Trb
);
1990 if (Status
!= EFI_NOT_READY
) {
1994 // Stall for 1 microsecond.