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
,
449 if (EFI_ERROR (Status
)) {
451 "%a: SD/MMC pre reset notifier callback failed - %r\n",
452 __FUNCTION__
, Status
));
457 PciIo
= Private
->PciIo
;
459 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_SW_RST
, sizeof (SwReset
), &SwReset
);
461 if (EFI_ERROR (Status
)) {
462 DEBUG ((DEBUG_ERROR
, "SdMmcHcReset: write SW Reset for All fails: %r\n", Status
));
466 Status
= SdMmcHcWaitMmioSet (
473 SD_MMC_HC_GENERIC_TIMEOUT
475 if (EFI_ERROR (Status
)) {
476 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: reset done with %r\n", Status
));
481 // Enable all interrupt after reset all.
483 Status
= SdMmcHcEnableInterrupt (PciIo
, Slot
);
484 if (EFI_ERROR (Status
)) {
485 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",
491 // Notify the SD/MMC override protocol that we have just reset
492 // the SD/MMC host controller.
494 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
495 Status
= mOverride
->NotifyPhase (
496 Private
->ControllerHandle
,
500 if (EFI_ERROR (Status
)) {
502 "%a: SD/MMC post reset notifier callback failed - %r\n",
503 __FUNCTION__
, Status
));
511 Set all interrupt status bits in Normal and Error Interrupt Status Enable
514 @param[in] PciIo The PCI IO protocol instance.
515 @param[in] Slot The slot number of the SD card to send the command to.
517 @retval EFI_SUCCESS The operation executes successfully.
518 @retval Others The operation fails.
522 SdMmcHcEnableInterrupt (
523 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
531 // Enable all bits in Error Interrupt Status Enable Register
534 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_ERR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
535 if (EFI_ERROR (Status
)) {
539 // Enable all bits in Normal Interrupt Status Enable Register
542 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
548 Get the capability data from the specified slot.
550 @param[in] PciIo The PCI IO protocol instance.
551 @param[in] Slot The slot number of the SD card to send the command to.
552 @param[out] Capability The buffer to store the capability data.
554 @retval EFI_SUCCESS The operation executes successfully.
555 @retval Others The operation fails.
559 SdMmcHcGetCapability (
560 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
562 OUT SD_MMC_HC_SLOT_CAP
*Capability
568 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CAP
, TRUE
, sizeof (Cap
), &Cap
);
569 if (EFI_ERROR (Status
)) {
573 CopyMem (Capability
, &Cap
, sizeof (Cap
));
579 Get the maximum current capability data from the specified slot.
581 @param[in] PciIo The PCI IO protocol instance.
582 @param[in] Slot The slot number of the SD card to send the command to.
583 @param[out] MaxCurrent The buffer to store the maximum current capability data.
585 @retval EFI_SUCCESS The operation executes successfully.
586 @retval Others The operation fails.
590 SdMmcHcGetMaxCurrent (
591 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
593 OUT UINT64
*MaxCurrent
598 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_MAX_CURRENT_CAP
, TRUE
, sizeof (UINT64
), MaxCurrent
);
604 Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller
607 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
609 @param[in] PciIo The PCI IO protocol instance.
610 @param[in] Slot The slot number of the SD card to send the command to.
611 @param[out] MediaPresent The pointer to the media present boolean value.
613 @retval EFI_SUCCESS There is no media change happened.
614 @retval EFI_MEDIA_CHANGED There is media change happened.
615 @retval Others The detection fails.
620 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
622 OUT BOOLEAN
*MediaPresent
630 // Check Present State Register to see if there is a card presented.
632 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_PRESENT_STATE
, TRUE
, sizeof (PresentState
), &PresentState
);
633 if (EFI_ERROR (Status
)) {
637 if ((PresentState
& BIT16
) != 0) {
638 *MediaPresent
= TRUE
;
640 *MediaPresent
= FALSE
;
644 // Check Normal Interrupt Status Register
646 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, TRUE
, sizeof (Data
), &Data
);
647 if (EFI_ERROR (Status
)) {
651 if ((Data
& (BIT6
| BIT7
)) != 0) {
653 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
656 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data
), &Data
);
657 if (EFI_ERROR (Status
)) {
661 return EFI_MEDIA_CHANGED
;
668 Stop SD/MMC card clock.
670 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
672 @param[in] PciIo The PCI IO protocol instance.
673 @param[in] Slot The slot number of the SD card to send the command to.
675 @retval EFI_SUCCESS Succeed to stop SD/MMC clock.
676 @retval Others Fail to stop SD/MMC clock.
681 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
690 // Ensure no SD transactions are occurring on the SD Bus by
691 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
692 // in the Present State register to be 0.
694 Status
= SdMmcHcWaitMmioSet (
697 SD_MMC_HC_PRESENT_STATE
,
698 sizeof (PresentState
),
701 SD_MMC_HC_GENERIC_TIMEOUT
703 if (EFI_ERROR (Status
)) {
708 // Set SD Clock Enable in the Clock Control register to 0
710 ClockCtrl
= (UINT16
)~BIT2
;
711 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
717 SD/MMC card clock supply.
719 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
721 @param[in] PciIo The PCI IO protocol instance.
722 @param[in] Slot The slot number of the SD card to send the command to.
723 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
724 @param[in] Capability The capability of the slot.
726 @retval EFI_SUCCESS The clock is supplied successfully.
727 @retval Others The clock isn't supplied successfully.
732 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
735 IN SD_MMC_HC_SLOT_CAP Capability
743 UINT16 ControllerVer
;
747 // Calculate a divisor for SD clock frequency
749 ASSERT (Capability
.BaseClkFreq
!= 0);
751 BaseClkFreq
= Capability
.BaseClkFreq
;
752 if (ClockFreq
== 0) {
753 return EFI_INVALID_PARAMETER
;
756 if (ClockFreq
> (BaseClkFreq
* 1000)) {
757 ClockFreq
= BaseClkFreq
* 1000;
761 // Calculate the divisor of base frequency.
764 SettingFreq
= BaseClkFreq
* 1000;
765 while (ClockFreq
< SettingFreq
) {
768 SettingFreq
= (BaseClkFreq
* 1000) / (2 * Divisor
);
769 Remainder
= (BaseClkFreq
* 1000) % (2 * Divisor
);
770 if ((ClockFreq
== SettingFreq
) && (Remainder
== 0)) {
773 if ((ClockFreq
== SettingFreq
) && (Remainder
!= 0)) {
778 DEBUG ((DEBUG_INFO
, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq
, Divisor
, ClockFreq
));
780 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CTRL_VER
, TRUE
, sizeof (ControllerVer
), &ControllerVer
);
781 if (EFI_ERROR (Status
)) {
785 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
787 if (((ControllerVer
& 0xFF) >= SD_MMC_HC_CTRL_VER_300
) &&
788 ((ControllerVer
& 0xFF) <= SD_MMC_HC_CTRL_VER_420
)) {
789 ASSERT (Divisor
<= 0x3FF);
790 ClockCtrl
= ((Divisor
& 0xFF) << 8) | ((Divisor
& 0x300) >> 2);
791 } else if (((ControllerVer
& 0xFF) == 0) || ((ControllerVer
& 0xFF) == 1)) {
793 // Only the most significant bit can be used as divisor.
795 if (((Divisor
- 1) & Divisor
) != 0) {
796 Divisor
= 1 << (HighBitSet32 (Divisor
) + 1);
798 ASSERT (Divisor
<= 0x80);
799 ClockCtrl
= (Divisor
& 0xFF) << 8;
801 DEBUG ((DEBUG_ERROR
, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer
));
802 return EFI_UNSUPPORTED
;
806 // Stop bus clock at first
808 Status
= SdMmcHcStopClock (PciIo
, Slot
);
809 if (EFI_ERROR (Status
)) {
814 // Supply clock frequency with specified divisor
817 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, FALSE
, sizeof (ClockCtrl
), &ClockCtrl
);
818 if (EFI_ERROR (Status
)) {
819 DEBUG ((DEBUG_ERROR
, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
824 // Wait Internal Clock Stable in the Clock Control register to be 1
826 Status
= SdMmcHcWaitMmioSet (
829 SD_MMC_HC_CLOCK_CTRL
,
833 SD_MMC_HC_GENERIC_TIMEOUT
835 if (EFI_ERROR (Status
)) {
840 // Set SD Clock Enable in the Clock Control register to 1
843 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
849 SD/MMC bus power control.
851 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
853 @param[in] PciIo The PCI IO protocol instance.
854 @param[in] Slot The slot number of the SD card to send the command to.
855 @param[in] PowerCtrl The value setting to the power control register.
857 @retval TRUE There is a SD/MMC card attached.
858 @retval FALSE There is no a SD/MMC card attached.
862 SdMmcHcPowerControl (
863 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
873 PowerCtrl
&= (UINT8
)~BIT0
;
874 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
875 if (EFI_ERROR (Status
)) {
880 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
883 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
889 Set the SD/MMC bus width.
891 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
893 @param[in] PciIo The PCI IO protocol instance.
894 @param[in] Slot The slot number of the SD card to send the command to.
895 @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.
897 @retval EFI_SUCCESS The bus width is set successfully.
898 @retval Others The bus width isn't set successfully.
903 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
912 HostCtrl1
= (UINT8
)~(BIT5
| BIT1
);
913 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
914 } else if (BusWidth
== 4) {
915 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
916 if (EFI_ERROR (Status
)) {
920 HostCtrl1
&= (UINT8
)~BIT5
;
921 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
922 } else if (BusWidth
== 8) {
923 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
924 if (EFI_ERROR (Status
)) {
927 HostCtrl1
&= (UINT8
)~BIT1
;
929 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
932 return EFI_INVALID_PARAMETER
;
939 Supply SD/MMC card with lowest clock frequency at initialization.
941 @param[in] PciIo The PCI IO protocol instance.
942 @param[in] Slot The slot number of the SD card to send the command to.
943 @param[in] Capability The capability of the slot.
945 @retval EFI_SUCCESS The clock is supplied successfully.
946 @retval Others The clock isn't supplied successfully.
950 SdMmcHcInitClockFreq (
951 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
953 IN SD_MMC_HC_SLOT_CAP Capability
960 // Calculate a divisor for SD clock frequency
962 if (Capability
.BaseClkFreq
== 0) {
964 // Don't support get Base Clock Frequency information via another method
966 return EFI_UNSUPPORTED
;
969 // Supply 400KHz clock frequency at initialization phase.
972 Status
= SdMmcHcClockSupply (PciIo
, Slot
, InitFreq
, Capability
);
977 Supply SD/MMC card with maximum voltage at initialization.
979 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
981 @param[in] PciIo The PCI IO protocol instance.
982 @param[in] Slot The slot number of the SD card to send the command to.
983 @param[in] Capability The capability of the slot.
985 @retval EFI_SUCCESS The voltage is supplied successfully.
986 @retval Others The voltage isn't supplied successfully.
990 SdMmcHcInitPowerVoltage (
991 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
993 IN SD_MMC_HC_SLOT_CAP Capability
1001 // Calculate supported maximum voltage according to SD Bus Voltage Select
1003 if (Capability
.Voltage33
!= 0) {
1008 } else if (Capability
.Voltage30
!= 0) {
1013 } else if (Capability
.Voltage18
!= 0) {
1019 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1021 if (EFI_ERROR (Status
)) {
1026 return EFI_DEVICE_ERROR
;
1030 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
1032 Status
= SdMmcHcPowerControl (PciIo
, Slot
, MaxVoltage
);
1038 Initialize the Timeout Control register with most conservative value at initialization.
1040 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
1042 @param[in] PciIo The PCI IO protocol instance.
1043 @param[in] Slot The slot number of the SD card to send the command to.
1045 @retval EFI_SUCCESS The timeout control register is configured successfully.
1046 @retval Others The timeout control register isn't configured successfully.
1050 SdMmcHcInitTimeoutCtrl (
1051 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1059 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_TIMEOUT_CTRL
, FALSE
, sizeof (Timeout
), &Timeout
);
1065 Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value
1068 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1069 @param[in] Slot The slot number of the SD card to send the command to.
1071 @retval EFI_SUCCESS The host controller is initialized successfully.
1072 @retval Others The host controller isn't initialized successfully.
1077 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1082 EFI_PCI_IO_PROTOCOL
*PciIo
;
1083 SD_MMC_HC_SLOT_CAP Capability
;
1086 // Notify the SD/MMC override protocol that we are about to initialize
1087 // the SD/MMC host controller.
1089 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1090 Status
= mOverride
->NotifyPhase (
1091 Private
->ControllerHandle
,
1093 EdkiiSdMmcInitHostPre
,
1095 if (EFI_ERROR (Status
)) {
1097 "%a: SD/MMC pre init notifier callback failed - %r\n",
1098 __FUNCTION__
, Status
));
1103 PciIo
= Private
->PciIo
;
1104 Capability
= Private
->Capability
[Slot
];
1106 Status
= SdMmcHcInitClockFreq (PciIo
, Slot
, Capability
);
1107 if (EFI_ERROR (Status
)) {
1111 Status
= SdMmcHcInitPowerVoltage (PciIo
, Slot
, Capability
);
1112 if (EFI_ERROR (Status
)) {
1116 Status
= SdMmcHcInitTimeoutCtrl (PciIo
, Slot
);
1117 if (EFI_ERROR (Status
)) {
1122 // Notify the SD/MMC override protocol that we are have just initialized
1123 // the SD/MMC host controller.
1125 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1126 Status
= mOverride
->NotifyPhase (
1127 Private
->ControllerHandle
,
1129 EdkiiSdMmcInitHostPost
,
1131 if (EFI_ERROR (Status
)) {
1133 "%a: SD/MMC post init notifier callback failed - %r\n",
1134 __FUNCTION__
, Status
));
1141 Set SD Host Controler control 2 registry according to selected speed.
1143 @param[in] ControllerHandle The handle of the controller.
1144 @param[in] PciIo The PCI IO protocol instance.
1145 @param[in] Slot The slot number of the SD card to send the command to.
1146 @param[in] Timing The timing to select.
1148 @retval EFI_SUCCESS The timing is set successfully.
1149 @retval Others The timing isn't set successfully.
1152 SdMmcHcUhsSignaling (
1153 IN EFI_HANDLE ControllerHandle
,
1154 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1156 IN SD_MMC_BUS_MODE Timing
1162 HostCtrl2
= (UINT8
)~SD_MMC_HC_CTRL_UHS_MASK
;
1163 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1164 if (EFI_ERROR (Status
)) {
1170 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR12
;
1173 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR25
;
1176 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR50
;
1178 case SdMmcUhsSdr104
:
1179 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR104
;
1182 HostCtrl2
= SD_MMC_HC_CTRL_UHS_DDR50
;
1184 case SdMmcMmcLegacy
:
1185 HostCtrl2
= SD_MMC_HC_CTRL_MMC_LEGACY
;
1188 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_SDR
;
1191 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_DDR
;
1194 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS200
;
1197 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS400
;
1203 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1204 if (EFI_ERROR (Status
)) {
1208 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1209 Status
= mOverride
->NotifyPhase (
1212 EdkiiSdMmcUhsSignaling
,
1215 if (EFI_ERROR (Status
)) {
1218 "%a: SD/MMC uhs signaling notifier callback failed - %r\n",
1232 @param[in] PciIo The PCI IO protocol instance.
1233 @param[in] Slot The slot number of the SD card to send the command to.
1234 @param[in] On The boolean to turn on/off LED.
1236 @retval EFI_SUCCESS The LED is turned on/off successfully.
1237 @retval Others The LED isn't turned on/off successfully.
1242 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1252 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1254 HostCtrl1
= (UINT8
)~BIT0
;
1255 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1262 Build ADMA descriptor table for transfer.
1264 Refer to SD Host Controller Simplified spec 3.0 Section 1.13 for details.
1266 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1268 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
1269 @retval Others The ADMA descriptor table isn't created successfully.
1273 BuildAdmaDescTable (
1274 IN SD_MMC_HC_TRB
*Trb
1277 EFI_PHYSICAL_ADDRESS Data
;
1284 EFI_PCI_IO_PROTOCOL
*PciIo
;
1288 Data
= Trb
->DataPhy
;
1289 DataLen
= Trb
->DataLen
;
1290 PciIo
= Trb
->Private
->PciIo
;
1292 // Only support 32bit ADMA Descriptor Table
1294 if ((Data
>= 0x100000000ul
) || ((Data
+ DataLen
) > 0x100000000ul
)) {
1295 return EFI_INVALID_PARAMETER
;
1298 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
1299 // for 32-bit address descriptor table.
1301 if ((Data
& (BIT0
| BIT1
)) != 0) {
1302 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data
));
1305 Entries
= DivU64x32 ((DataLen
+ ADMA_MAX_DATA_PER_LINE
- 1), ADMA_MAX_DATA_PER_LINE
);
1306 TableSize
= (UINTN
)MultU64x32 (Entries
, sizeof (SD_MMC_HC_ADMA_DESC_LINE
));
1307 Trb
->AdmaPages
= (UINT32
)EFI_SIZE_TO_PAGES (TableSize
);
1308 Status
= PciIo
->AllocateBuffer (
1311 EfiBootServicesData
,
1312 EFI_SIZE_TO_PAGES (TableSize
),
1313 (VOID
**)&Trb
->AdmaDesc
,
1316 if (EFI_ERROR (Status
)) {
1317 return EFI_OUT_OF_RESOURCES
;
1319 ZeroMem (Trb
->AdmaDesc
, TableSize
);
1321 Status
= PciIo
->Map (
1323 EfiPciIoOperationBusMasterCommonBuffer
,
1330 if (EFI_ERROR (Status
) || (Bytes
!= TableSize
)) {
1332 // Map error or unable to map the whole RFis buffer into a contiguous region.
1336 EFI_SIZE_TO_PAGES (TableSize
),
1339 return EFI_OUT_OF_RESOURCES
;
1342 if ((UINT64
)(UINTN
)Trb
->AdmaDescPhy
> 0x100000000ul
) {
1344 // The ADMA doesn't support 64bit addressing.
1352 EFI_SIZE_TO_PAGES (TableSize
),
1355 return EFI_DEVICE_ERROR
;
1358 Remaining
= DataLen
;
1359 Address
= (UINT32
)Data
;
1360 for (Index
= 0; Index
< Entries
; Index
++) {
1361 if (Remaining
<= ADMA_MAX_DATA_PER_LINE
) {
1362 Trb
->AdmaDesc
[Index
].Valid
= 1;
1363 Trb
->AdmaDesc
[Index
].Act
= 2;
1364 Trb
->AdmaDesc
[Index
].Length
= (UINT16
)Remaining
;
1365 Trb
->AdmaDesc
[Index
].Address
= Address
;
1368 Trb
->AdmaDesc
[Index
].Valid
= 1;
1369 Trb
->AdmaDesc
[Index
].Act
= 2;
1370 Trb
->AdmaDesc
[Index
].Length
= 0;
1371 Trb
->AdmaDesc
[Index
].Address
= Address
;
1374 Remaining
-= ADMA_MAX_DATA_PER_LINE
;
1375 Address
+= ADMA_MAX_DATA_PER_LINE
;
1379 // Set the last descriptor line as end of descriptor table
1381 Trb
->AdmaDesc
[Index
].End
= 1;
1386 Create a new TRB for the SD/MMC cmd request.
1388 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1389 @param[in] Slot The slot number of the SD card to send the command to.
1390 @param[in] Packet A pointer to the SD command data structure.
1391 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1392 not NULL, then nonblocking I/O is performed, and Event
1393 will be signaled when the Packet completes.
1395 @return Created Trb or NULL.
1400 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1402 IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1409 EFI_PCI_IO_PROTOCOL_OPERATION Flag
;
1410 EFI_PCI_IO_PROTOCOL
*PciIo
;
1413 Trb
= AllocateZeroPool (sizeof (SD_MMC_HC_TRB
));
1418 Trb
->Signature
= SD_MMC_HC_TRB_SIG
;
1420 Trb
->BlockSize
= 0x200;
1421 Trb
->Packet
= Packet
;
1423 Trb
->Started
= FALSE
;
1424 Trb
->Timeout
= Packet
->Timeout
;
1425 Trb
->Private
= Private
;
1427 if ((Packet
->InTransferLength
!= 0) && (Packet
->InDataBuffer
!= NULL
)) {
1428 Trb
->Data
= Packet
->InDataBuffer
;
1429 Trb
->DataLen
= Packet
->InTransferLength
;
1431 } else if ((Packet
->OutTransferLength
!= 0) && (Packet
->OutDataBuffer
!= NULL
)) {
1432 Trb
->Data
= Packet
->OutDataBuffer
;
1433 Trb
->DataLen
= Packet
->OutTransferLength
;
1435 } else if ((Packet
->InTransferLength
== 0) && (Packet
->OutTransferLength
== 0)) {
1442 if ((Trb
->DataLen
!= 0) && (Trb
->DataLen
< Trb
->BlockSize
)) {
1443 Trb
->BlockSize
= (UINT16
)Trb
->DataLen
;
1446 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1447 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1448 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1449 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
1450 Trb
->Mode
= SdMmcPioMode
;
1453 Flag
= EfiPciIoOperationBusMasterWrite
;
1455 Flag
= EfiPciIoOperationBusMasterRead
;
1458 PciIo
= Private
->PciIo
;
1459 if (Trb
->DataLen
!= 0) {
1460 MapLength
= Trb
->DataLen
;
1461 Status
= PciIo
->Map (
1469 if (EFI_ERROR (Status
) || (Trb
->DataLen
!= MapLength
)) {
1470 Status
= EFI_BAD_BUFFER_SIZE
;
1475 if (Trb
->DataLen
== 0) {
1476 Trb
->Mode
= SdMmcNoData
;
1477 } else if (Private
->Capability
[Slot
].Adma2
!= 0) {
1478 Trb
->Mode
= SdMmcAdmaMode
;
1479 Status
= BuildAdmaDescTable (Trb
);
1480 if (EFI_ERROR (Status
)) {
1481 PciIo
->Unmap (PciIo
, Trb
->DataMap
);
1484 } else if (Private
->Capability
[Slot
].Sdma
!= 0) {
1485 Trb
->Mode
= SdMmcSdmaMode
;
1487 Trb
->Mode
= SdMmcPioMode
;
1491 if (Event
!= NULL
) {
1492 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1493 InsertTailList (&Private
->Queue
, &Trb
->TrbList
);
1494 gBS
->RestoreTPL (OldTpl
);
1505 Free the resource used by the TRB.
1507 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1512 IN SD_MMC_HC_TRB
*Trb
1515 EFI_PCI_IO_PROTOCOL
*PciIo
;
1517 PciIo
= Trb
->Private
->PciIo
;
1519 if (Trb
->AdmaMap
!= NULL
) {
1525 if (Trb
->AdmaDesc
!= NULL
) {
1532 if (Trb
->DataMap
!= NULL
) {
1543 Check if the env is ready for execute specified TRB.
1545 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1546 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1548 @retval EFI_SUCCESS The env is ready for TRB execution.
1549 @retval EFI_NOT_READY The env is not ready for TRB execution.
1550 @retval Others Some erros happen.
1555 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1556 IN SD_MMC_HC_TRB
*Trb
1560 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1561 EFI_PCI_IO_PROTOCOL
*PciIo
;
1562 UINT32 PresentState
;
1564 Packet
= Trb
->Packet
;
1566 if ((Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) ||
1567 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR1b
) ||
1568 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR5b
)) {
1570 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1571 // the Present State register to be 0
1573 PresentState
= BIT0
| BIT1
;
1576 // Wait Command Inhibit (CMD) in the Present State register
1579 PresentState
= BIT0
;
1582 PciIo
= Private
->PciIo
;
1583 Status
= SdMmcHcCheckMmioSet (
1586 SD_MMC_HC_PRESENT_STATE
,
1587 sizeof (PresentState
),
1596 Wait for the env to be ready for execute specified TRB.
1598 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1599 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1601 @retval EFI_SUCCESS The env is ready for TRB execution.
1602 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1603 @retval Others Some erros happen.
1608 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1609 IN SD_MMC_HC_TRB
*Trb
1613 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1615 BOOLEAN InfiniteWait
;
1618 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1620 Packet
= Trb
->Packet
;
1621 Timeout
= Packet
->Timeout
;
1623 InfiniteWait
= TRUE
;
1625 InfiniteWait
= FALSE
;
1628 while (InfiniteWait
|| (Timeout
> 0)) {
1630 // Check Trb execution result by reading Normal Interrupt Status register.
1632 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
1633 if (Status
!= EFI_NOT_READY
) {
1637 // Stall for 1 microsecond.
1648 Execute the specified TRB.
1650 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1651 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1653 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1654 @retval Others Some erros happen when sending this request to the host controller.
1659 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1660 IN SD_MMC_HC_TRB
*Trb
1664 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1665 EFI_PCI_IO_PROTOCOL
*PciIo
;
1676 Packet
= Trb
->Packet
;
1677 PciIo
= Trb
->Private
->PciIo
;
1679 // Clear all bits in Error Interrupt Status Register
1682 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ERR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1683 if (EFI_ERROR (Status
)) {
1687 // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.
1690 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1691 if (EFI_ERROR (Status
)) {
1695 // Set Host Control 1 register DMA Select field
1697 if (Trb
->Mode
== SdMmcAdmaMode
) {
1699 Status
= SdMmcHcOrMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1700 if (EFI_ERROR (Status
)) {
1705 SdMmcHcLedOnOff (PciIo
, Trb
->Slot
, TRUE
);
1707 if (Trb
->Mode
== SdMmcSdmaMode
) {
1708 if ((UINT64
)(UINTN
)Trb
->DataPhy
>= 0x100000000ul
) {
1709 return EFI_INVALID_PARAMETER
;
1712 SdmaAddr
= (UINT32
)(UINTN
)Trb
->DataPhy
;
1713 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (SdmaAddr
), &SdmaAddr
);
1714 if (EFI_ERROR (Status
)) {
1717 } else if (Trb
->Mode
== SdMmcAdmaMode
) {
1718 AdmaAddr
= (UINT64
)(UINTN
)Trb
->AdmaDescPhy
;
1719 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (AdmaAddr
), &AdmaAddr
);
1720 if (EFI_ERROR (Status
)) {
1725 BlkSize
= Trb
->BlockSize
;
1726 if (Trb
->Mode
== SdMmcSdmaMode
) {
1728 // Set SDMA boundary to be 512K bytes.
1733 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_SIZE
, FALSE
, sizeof (BlkSize
), &BlkSize
);
1734 if (EFI_ERROR (Status
)) {
1739 if (Trb
->Mode
!= SdMmcNoData
) {
1741 // Calcuate Block Count.
1743 BlkCount
= (UINT16
)(Trb
->DataLen
/ Trb
->BlockSize
);
1745 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_COUNT
, FALSE
, sizeof (BlkCount
), &BlkCount
);
1746 if (EFI_ERROR (Status
)) {
1750 Argument
= Packet
->SdMmcCmdBlk
->CommandArgument
;
1751 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ARG1
, FALSE
, sizeof (Argument
), &Argument
);
1752 if (EFI_ERROR (Status
)) {
1757 if (Trb
->Mode
!= SdMmcNoData
) {
1758 if (Trb
->Mode
!= SdMmcPioMode
) {
1765 TransMode
|= BIT5
| BIT1
;
1768 // Only SD memory card needs to use AUTO CMD12 feature.
1770 if (Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) {
1777 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_TRANS_MOD
, FALSE
, sizeof (TransMode
), &TransMode
);
1778 if (EFI_ERROR (Status
)) {
1782 Cmd
= (UINT16
)LShiftU64(Packet
->SdMmcCmdBlk
->CommandIndex
, 8);
1783 if (Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) {
1787 // Convert ResponseType to value
1789 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
1790 switch (Packet
->SdMmcCmdBlk
->ResponseType
) {
1791 case SdMmcResponseTypeR1
:
1792 case SdMmcResponseTypeR5
:
1793 case SdMmcResponseTypeR6
:
1794 case SdMmcResponseTypeR7
:
1795 Cmd
|= (BIT1
| BIT3
| BIT4
);
1797 case SdMmcResponseTypeR2
:
1798 Cmd
|= (BIT0
| BIT3
);
1800 case SdMmcResponseTypeR3
:
1801 case SdMmcResponseTypeR4
:
1804 case SdMmcResponseTypeR1b
:
1805 case SdMmcResponseTypeR5b
:
1806 Cmd
|= (BIT0
| BIT1
| BIT3
| BIT4
);
1816 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_COMMAND
, FALSE
, sizeof (Cmd
), &Cmd
);
1821 Check the TRB execution result.
1823 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1824 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1826 @retval EFI_SUCCESS The TRB is executed successfully.
1827 @retval EFI_NOT_READY The TRB is not completed for execution.
1828 @retval Others Some erros happen when executing this request.
1832 SdMmcCheckTrbResult (
1833 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1834 IN SD_MMC_HC_TRB
*Trb
1838 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1847 Packet
= Trb
->Packet
;
1849 // Check Trb execution result by reading Normal Interrupt Status register.
1851 Status
= SdMmcHcRwMmio (
1854 SD_MMC_HC_NOR_INT_STS
,
1859 if (EFI_ERROR (Status
)) {
1863 // Check Transfer Complete bit is set or not.
1865 if ((IntStatus
& BIT1
) == BIT1
) {
1866 if ((IntStatus
& BIT15
) == BIT15
) {
1868 // Read Error Interrupt Status register to check if the error is
1869 // Data Timeout Error.
1870 // If yes, treat it as success as Transfer Complete has higher
1871 // priority than Data Timeout Error.
1873 Status
= SdMmcHcRwMmio (
1876 SD_MMC_HC_ERR_INT_STS
,
1881 if (!EFI_ERROR (Status
)) {
1882 if ((IntStatus
& BIT4
) == BIT4
) {
1883 Status
= EFI_SUCCESS
;
1885 Status
= EFI_DEVICE_ERROR
;
1893 // Check if there is a error happened during cmd execution.
1894 // If yes, then do error recovery procedure to follow SD Host Controller
1895 // Simplified Spec 3.0 section 3.10.1.
1897 if ((IntStatus
& BIT15
) == BIT15
) {
1898 Status
= SdMmcHcRwMmio (
1901 SD_MMC_HC_ERR_INT_STS
,
1906 if (EFI_ERROR (Status
)) {
1909 if ((IntStatus
& 0x0F) != 0) {
1912 if ((IntStatus
& 0xF0) != 0) {
1916 Status
= SdMmcHcRwMmio (
1924 if (EFI_ERROR (Status
)) {
1927 Status
= SdMmcHcWaitMmioSet (
1934 SD_MMC_HC_GENERIC_TIMEOUT
1936 if (EFI_ERROR (Status
)) {
1940 Status
= EFI_DEVICE_ERROR
;
1944 // Check if DMA interrupt is signalled for the SDMA transfer.
1946 if ((Trb
->Mode
== SdMmcSdmaMode
) && ((IntStatus
& BIT3
) == BIT3
)) {
1948 // Clear DMA interrupt bit.
1951 Status
= SdMmcHcRwMmio (
1954 SD_MMC_HC_NOR_INT_STS
,
1959 if (EFI_ERROR (Status
)) {
1963 // Update SDMA Address register.
1965 SdmaAddr
= SD_MMC_SDMA_ROUND_UP ((UINT32
)(UINTN
)Trb
->DataPhy
, SD_MMC_SDMA_BOUNDARY
);
1966 Status
= SdMmcHcRwMmio (
1969 SD_MMC_HC_SDMA_ADDR
,
1974 if (EFI_ERROR (Status
)) {
1977 Trb
->DataPhy
= (UINT32
)(UINTN
)SdmaAddr
;
1980 if ((Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeAdtc
) &&
1981 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR1b
) &&
1982 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR5b
)) {
1983 if ((IntStatus
& BIT0
) == BIT0
) {
1984 Status
= EFI_SUCCESS
;
1989 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1990 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1991 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1992 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
1994 // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,
1995 // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.
1996 // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.
1998 if ((IntStatus
& BIT5
) == BIT5
) {
2000 // Clear Buffer Read Ready interrupt at first.
2003 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
2005 // Read data out from Buffer Port register
2007 for (PioLength
= 0; PioLength
< Trb
->DataLen
; PioLength
+= 4) {
2008 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_BUF_DAT_PORT
, TRUE
, 4, (UINT8
*)Trb
->Data
+ PioLength
);
2010 Status
= EFI_SUCCESS
;
2015 Status
= EFI_NOT_READY
;
2018 // Get response data when the cmd is executed successfully.
2020 if (!EFI_ERROR (Status
)) {
2021 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
2022 for (Index
= 0; Index
< 4; Index
++) {
2023 Status
= SdMmcHcRwMmio (
2026 SD_MMC_HC_RESPONSE
+ Index
* 4,
2031 if (EFI_ERROR (Status
)) {
2032 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2036 CopyMem (Packet
->SdMmcStatusBlk
, Response
, sizeof (Response
));
2040 if (Status
!= EFI_NOT_READY
) {
2041 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2048 Wait for the TRB execution result.
2050 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2051 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2053 @retval EFI_SUCCESS The TRB is executed successfully.
2054 @retval Others Some erros happen when executing this request.
2058 SdMmcWaitTrbResult (
2059 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2060 IN SD_MMC_HC_TRB
*Trb
2064 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2066 BOOLEAN InfiniteWait
;
2068 Packet
= Trb
->Packet
;
2070 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
2072 Timeout
= Packet
->Timeout
;
2074 InfiniteWait
= TRUE
;
2076 InfiniteWait
= FALSE
;
2079 while (InfiniteWait
|| (Timeout
> 0)) {
2081 // Check Trb execution result by reading Normal Interrupt Status register.
2083 Status
= SdMmcCheckTrbResult (Private
, Trb
);
2084 if (Status
!= EFI_NOT_READY
) {
2088 // Stall for 1 microsecond.