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 plus the 64-bit
4 System Addressing support in SD Host Controller Simplified Specification version
7 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.
9 Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
10 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include "SdMmcPciHcDxe.h"
24 Dump the content of SD/MMC host controller's Capability Register.
26 @param[in] Slot The slot number of the SD card to send the command to.
27 @param[in] Capability The buffer to store the capability data.
33 IN SD_MMC_HC_SLOT_CAP
*Capability
37 // Dump Capability Data
39 DEBUG ((DEBUG_INFO
, " == Slot [%d] Capability is 0x%x ==\n", Slot
, Capability
));
40 DEBUG ((DEBUG_INFO
, " Timeout Clk Freq %d%a\n", Capability
->TimeoutFreq
, (Capability
->TimeoutUnit
) ? "MHz" : "KHz"));
41 DEBUG ((DEBUG_INFO
, " Base Clk Freq %dMHz\n", Capability
->BaseClkFreq
));
42 DEBUG ((DEBUG_INFO
, " Max Blk Len %dbytes\n", 512 * (1 << Capability
->MaxBlkLen
)));
43 DEBUG ((DEBUG_INFO
, " 8-bit Support %a\n", Capability
->BusWidth8
? "TRUE" : "FALSE"));
44 DEBUG ((DEBUG_INFO
, " ADMA2 Support %a\n", Capability
->Adma2
? "TRUE" : "FALSE"));
45 DEBUG ((DEBUG_INFO
, " HighSpeed Support %a\n", Capability
->HighSpeed
? "TRUE" : "FALSE"));
46 DEBUG ((DEBUG_INFO
, " SDMA Support %a\n", Capability
->Sdma
? "TRUE" : "FALSE"));
47 DEBUG ((DEBUG_INFO
, " Suspend/Resume %a\n", Capability
->SuspRes
? "TRUE" : "FALSE"));
48 DEBUG ((DEBUG_INFO
, " Voltage 3.3 %a\n", Capability
->Voltage33
? "TRUE" : "FALSE"));
49 DEBUG ((DEBUG_INFO
, " Voltage 3.0 %a\n", Capability
->Voltage30
? "TRUE" : "FALSE"));
50 DEBUG ((DEBUG_INFO
, " Voltage 1.8 %a\n", Capability
->Voltage18
? "TRUE" : "FALSE"));
51 DEBUG ((DEBUG_INFO
, " V4 64-bit Sys Bus %a\n", Capability
->SysBus64V4
? "TRUE" : "FALSE"));
52 DEBUG ((DEBUG_INFO
, " V3 64-bit Sys Bus %a\n", Capability
->SysBus64V3
? "TRUE" : "FALSE"));
53 DEBUG ((DEBUG_INFO
, " Async Interrupt %a\n", Capability
->AsyncInt
? "TRUE" : "FALSE"));
54 DEBUG ((DEBUG_INFO
, " SlotType "));
55 if (Capability
->SlotType
== 0x00) {
56 DEBUG ((DEBUG_INFO
, "%a\n", "Removable Slot"));
57 } else if (Capability
->SlotType
== 0x01) {
58 DEBUG ((DEBUG_INFO
, "%a\n", "Embedded Slot"));
59 } else if (Capability
->SlotType
== 0x02) {
60 DEBUG ((DEBUG_INFO
, "%a\n", "Shared Bus Slot"));
62 DEBUG ((DEBUG_INFO
, "%a\n", "Reserved"));
64 DEBUG ((DEBUG_INFO
, " SDR50 Support %a\n", Capability
->Sdr50
? "TRUE" : "FALSE"));
65 DEBUG ((DEBUG_INFO
, " SDR104 Support %a\n", Capability
->Sdr104
? "TRUE" : "FALSE"));
66 DEBUG ((DEBUG_INFO
, " DDR50 Support %a\n", Capability
->Ddr50
? "TRUE" : "FALSE"));
67 DEBUG ((DEBUG_INFO
, " Driver Type A %a\n", Capability
->DriverTypeA
? "TRUE" : "FALSE"));
68 DEBUG ((DEBUG_INFO
, " Driver Type C %a\n", Capability
->DriverTypeC
? "TRUE" : "FALSE"));
69 DEBUG ((DEBUG_INFO
, " Driver Type D %a\n", Capability
->DriverTypeD
? "TRUE" : "FALSE"));
70 DEBUG ((DEBUG_INFO
, " Driver Type 4 %a\n", Capability
->DriverType4
? "TRUE" : "FALSE"));
71 if (Capability
->TimerCount
== 0) {
72 DEBUG ((DEBUG_INFO
, " Retuning TimerCnt Disabled\n", 2 * (Capability
->TimerCount
- 1)));
74 DEBUG ((DEBUG_INFO
, " Retuning TimerCnt %dseconds\n", 2 * (Capability
->TimerCount
- 1)));
76 DEBUG ((DEBUG_INFO
, " SDR50 Tuning %a\n", Capability
->TuningSDR50
? "TRUE" : "FALSE"));
77 DEBUG ((DEBUG_INFO
, " Retuning Mode Mode %d\n", Capability
->RetuningMod
+ 1));
78 DEBUG ((DEBUG_INFO
, " Clock Multiplier M = %d\n", Capability
->ClkMultiplier
+ 1));
79 DEBUG ((DEBUG_INFO
, " HS 400 %a\n", Capability
->Hs400
? "TRUE" : "FALSE"));
84 Read SlotInfo register from SD/MMC host controller pci config space.
86 @param[in] PciIo The PCI IO protocol instance.
87 @param[out] FirstBar The buffer to store the first BAR value.
88 @param[out] SlotNum The buffer to store the supported slot number.
90 @retval EFI_SUCCESS The operation succeeds.
91 @retval Others The operation fails.
97 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
103 SD_MMC_HC_SLOT_INFO SlotInfo
;
105 Status
= PciIo
->Pci
.Read (
108 SD_MMC_HC_SLOT_OFFSET
,
112 if (EFI_ERROR (Status
)) {
116 *FirstBar
= SlotInfo
.FirstBar
;
117 *SlotNum
= SlotInfo
.SlotNum
+ 1;
118 ASSERT ((*FirstBar
+ *SlotNum
) < SD_MMC_HC_MAX_SLOT
);
123 Read/Write specified SD/MMC host controller mmio register.
125 @param[in] PciIo The PCI IO protocol instance.
126 @param[in] BarIndex The BAR index of the standard PCI Configuration
127 header to use as the base address for the memory
128 operation to perform.
129 @param[in] Offset The offset within the selected BAR to start the
131 @param[in] Read A boolean to indicate it's read or write operation.
132 @param[in] Count The width of the mmio register in bytes.
133 Must be 1, 2 , 4 or 8 bytes.
134 @param[in, out] Data For read operations, the destination buffer to store
135 the results. For write operations, the source buffer
136 to write data from. The caller is responsible for
137 having ownership of the data buffer and ensuring its
138 size not less than Count bytes.
140 @retval EFI_INVALID_PARAMETER The PciIo or Data is NULL or the Count is not valid.
141 @retval EFI_SUCCESS The read/write operation succeeds.
142 @retval Others The read/write operation fails.
148 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
157 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
159 if ((PciIo
== NULL
) || (Data
== NULL
)) {
160 return EFI_INVALID_PARAMETER
;
165 Width
= EfiPciIoWidthUint8
;
168 Width
= EfiPciIoWidthUint16
;
172 Width
= EfiPciIoWidthUint32
;
176 Width
= EfiPciIoWidthUint32
;
180 return EFI_INVALID_PARAMETER
;
184 Status
= PciIo
->Mem
.Read (
193 Status
= PciIo
->Mem
.Write (
207 Do OR operation with the value of the specified SD/MMC host controller mmio register.
209 @param[in] PciIo The PCI IO protocol instance.
210 @param[in] BarIndex The BAR index of the standard PCI Configuration
211 header to use as the base address for the memory
212 operation to perform.
213 @param[in] Offset The offset within the selected BAR to start the
215 @param[in] Count The width of the mmio register in bytes.
216 Must be 1, 2 , 4 or 8 bytes.
217 @param[in] OrData The pointer to the data used to do OR operation.
218 The caller is responsible for having ownership of
219 the data buffer and ensuring its size not less than
222 @retval EFI_INVALID_PARAMETER The PciIo or OrData is NULL or the Count is not valid.
223 @retval EFI_SUCCESS The OR operation succeeds.
224 @retval Others The OR operation fails.
230 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
241 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
242 if (EFI_ERROR (Status
)) {
247 Or
= *(UINT8
*) OrData
;
248 } else if (Count
== 2) {
249 Or
= *(UINT16
*) OrData
;
250 } else if (Count
== 4) {
251 Or
= *(UINT32
*) OrData
;
252 } else if (Count
== 8) {
253 Or
= *(UINT64
*) OrData
;
255 return EFI_INVALID_PARAMETER
;
259 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
265 Do AND operation with the value of the specified SD/MMC host controller mmio register.
267 @param[in] PciIo The PCI IO protocol instance.
268 @param[in] BarIndex The BAR index of the standard PCI Configuration
269 header to use as the base address for the memory
270 operation to perform.
271 @param[in] Offset The offset within the selected BAR to start the
273 @param[in] Count The width of the mmio register in bytes.
274 Must be 1, 2 , 4 or 8 bytes.
275 @param[in] AndData The pointer to the data used to do AND operation.
276 The caller is responsible for having ownership of
277 the data buffer and ensuring its size not less than
280 @retval EFI_INVALID_PARAMETER The PciIo or AndData is NULL or the Count is not valid.
281 @retval EFI_SUCCESS The AND operation succeeds.
282 @retval Others The AND operation fails.
288 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
299 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
300 if (EFI_ERROR (Status
)) {
305 And
= *(UINT8
*) AndData
;
306 } else if (Count
== 2) {
307 And
= *(UINT16
*) AndData
;
308 } else if (Count
== 4) {
309 And
= *(UINT32
*) AndData
;
310 } else if (Count
== 8) {
311 And
= *(UINT64
*) AndData
;
313 return EFI_INVALID_PARAMETER
;
317 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
323 Wait for the value of the specified MMIO register set to the test value.
325 @param[in] PciIo The PCI IO protocol instance.
326 @param[in] BarIndex The BAR index of the standard PCI Configuration
327 header to use as the base address for the memory
328 operation to perform.
329 @param[in] Offset The offset within the selected BAR to start the
331 @param[in] Count The width of the mmio register in bytes.
332 Must be 1, 2, 4 or 8 bytes.
333 @param[in] MaskValue The mask value of memory.
334 @param[in] TestValue The test value of memory.
336 @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.
337 @retval EFI_SUCCESS The MMIO register has expected value.
338 @retval Others The MMIO operation fails.
343 SdMmcHcCheckMmioSet (
344 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
356 // Access PCI MMIO space to see if the value is the tested one.
359 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Value
);
360 if (EFI_ERROR (Status
)) {
366 if (Value
== TestValue
) {
370 return EFI_NOT_READY
;
374 Wait for the value of the specified MMIO register set to the test value.
376 @param[in] PciIo The PCI IO protocol instance.
377 @param[in] BarIndex The BAR index of the standard PCI Configuration
378 header to use as the base address for the memory
379 operation to perform.
380 @param[in] Offset The offset within the selected BAR to start the
382 @param[in] Count The width of the mmio register in bytes.
383 Must be 1, 2, 4 or 8 bytes.
384 @param[in] MaskValue The mask value of memory.
385 @param[in] TestValue The test value of memory.
386 @param[in] Timeout The time out value for wait memory set, uses 1
387 microsecond as a unit.
389 @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout
391 @retval EFI_SUCCESS The MMIO register has expected value.
392 @retval Others The MMIO operation fails.
398 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
408 BOOLEAN InfiniteWait
;
413 InfiniteWait
= FALSE
;
416 while (InfiniteWait
|| (Timeout
> 0)) {
417 Status
= SdMmcHcCheckMmioSet (
425 if (Status
!= EFI_NOT_READY
) {
430 // Stall for 1 microsecond.
441 Get the controller version information from the specified slot.
443 @param[in] PciIo The PCI IO protocol instance.
444 @param[in] Slot The slot number of the SD card to send the command to.
445 @param[out] Version The buffer to store the version information.
447 @retval EFI_SUCCESS The operation executes successfully.
448 @retval Others The operation fails.
452 SdMmcHcGetControllerVersion (
453 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
460 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CTRL_VER
, TRUE
, sizeof (UINT16
), Version
);
461 if (EFI_ERROR (Status
)) {
471 Software reset the specified SD/MMC host controller and enable all interrupts.
473 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
474 @param[in] Slot The slot number of the SD card to send the command to.
476 @retval EFI_SUCCESS The software reset executes successfully.
477 @retval Others The software reset fails.
482 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
488 EFI_PCI_IO_PROTOCOL
*PciIo
;
491 // Notify the SD/MMC override protocol that we are about to 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 pre reset notifier callback failed - %r\n",
503 __FUNCTION__
, Status
));
508 PciIo
= Private
->PciIo
;
510 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_SW_RST
, sizeof (SwReset
), &SwReset
);
512 if (EFI_ERROR (Status
)) {
513 DEBUG ((DEBUG_ERROR
, "SdMmcHcReset: write SW Reset for All fails: %r\n", Status
));
517 Status
= SdMmcHcWaitMmioSet (
524 SD_MMC_HC_GENERIC_TIMEOUT
526 if (EFI_ERROR (Status
)) {
527 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: reset done with %r\n", Status
));
532 // Enable all interrupt after reset all.
534 Status
= SdMmcHcEnableInterrupt (PciIo
, Slot
);
535 if (EFI_ERROR (Status
)) {
536 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",
542 // Notify the SD/MMC override protocol that we have just reset
543 // the SD/MMC host controller.
545 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
546 Status
= mOverride
->NotifyPhase (
547 Private
->ControllerHandle
,
551 if (EFI_ERROR (Status
)) {
553 "%a: SD/MMC post reset notifier callback failed - %r\n",
554 __FUNCTION__
, Status
));
562 Set all interrupt status bits in Normal and Error Interrupt Status Enable
565 @param[in] PciIo The PCI IO protocol instance.
566 @param[in] Slot The slot number of the SD card to send the command to.
568 @retval EFI_SUCCESS The operation executes successfully.
569 @retval Others The operation fails.
573 SdMmcHcEnableInterrupt (
574 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
582 // Enable all bits in Error Interrupt Status Enable Register
585 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_ERR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
586 if (EFI_ERROR (Status
)) {
590 // Enable all bits in Normal Interrupt Status Enable Register
593 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
599 Get the capability data from the specified slot.
601 @param[in] PciIo The PCI IO protocol instance.
602 @param[in] Slot The slot number of the SD card to send the command to.
603 @param[out] Capability The buffer to store the capability data.
605 @retval EFI_SUCCESS The operation executes successfully.
606 @retval Others The operation fails.
610 SdMmcHcGetCapability (
611 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
613 OUT SD_MMC_HC_SLOT_CAP
*Capability
619 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CAP
, TRUE
, sizeof (Cap
), &Cap
);
620 if (EFI_ERROR (Status
)) {
624 CopyMem (Capability
, &Cap
, sizeof (Cap
));
630 Get the maximum current capability data from the specified slot.
632 @param[in] PciIo The PCI IO protocol instance.
633 @param[in] Slot The slot number of the SD card to send the command to.
634 @param[out] MaxCurrent The buffer to store the maximum current capability data.
636 @retval EFI_SUCCESS The operation executes successfully.
637 @retval Others The operation fails.
641 SdMmcHcGetMaxCurrent (
642 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
644 OUT UINT64
*MaxCurrent
649 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_MAX_CURRENT_CAP
, TRUE
, sizeof (UINT64
), MaxCurrent
);
655 Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller
658 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
660 @param[in] PciIo The PCI IO protocol instance.
661 @param[in] Slot The slot number of the SD card to send the command to.
662 @param[out] MediaPresent The pointer to the media present boolean value.
664 @retval EFI_SUCCESS There is no media change happened.
665 @retval EFI_MEDIA_CHANGED There is media change happened.
666 @retval Others The detection fails.
671 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
673 OUT BOOLEAN
*MediaPresent
681 // Check Present State Register to see if there is a card presented.
683 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_PRESENT_STATE
, TRUE
, sizeof (PresentState
), &PresentState
);
684 if (EFI_ERROR (Status
)) {
688 if ((PresentState
& BIT16
) != 0) {
689 *MediaPresent
= TRUE
;
691 *MediaPresent
= FALSE
;
695 // Check Normal Interrupt Status Register
697 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, TRUE
, sizeof (Data
), &Data
);
698 if (EFI_ERROR (Status
)) {
702 if ((Data
& (BIT6
| BIT7
)) != 0) {
704 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
707 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data
), &Data
);
708 if (EFI_ERROR (Status
)) {
712 return EFI_MEDIA_CHANGED
;
719 Stop SD/MMC card clock.
721 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
723 @param[in] PciIo The PCI IO protocol instance.
724 @param[in] Slot The slot number of the SD card to send the command to.
726 @retval EFI_SUCCESS Succeed to stop SD/MMC clock.
727 @retval Others Fail to stop SD/MMC clock.
732 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
741 // Ensure no SD transactions are occurring on the SD Bus by
742 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
743 // in the Present State register to be 0.
745 Status
= SdMmcHcWaitMmioSet (
748 SD_MMC_HC_PRESENT_STATE
,
749 sizeof (PresentState
),
752 SD_MMC_HC_GENERIC_TIMEOUT
754 if (EFI_ERROR (Status
)) {
759 // Set SD Clock Enable in the Clock Control register to 0
761 ClockCtrl
= (UINT16
)~BIT2
;
762 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
768 SD/MMC card clock supply.
770 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
772 @param[in] PciIo The PCI IO protocol instance.
773 @param[in] Slot The slot number of the SD card to send the command to.
774 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
775 @param[in] BaseClkFreq The base clock frequency of host controller in MHz.
776 @param[in] ControllerVer The version of host controller.
778 @retval EFI_SUCCESS The clock is supplied successfully.
779 @retval Others The clock isn't supplied successfully.
784 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
787 IN UINT32 BaseClkFreq
,
788 IN UINT16 ControllerVer
798 // Calculate a divisor for SD clock frequency
800 ASSERT (BaseClkFreq
!= 0);
802 if (ClockFreq
== 0) {
803 return EFI_INVALID_PARAMETER
;
806 if (ClockFreq
> (BaseClkFreq
* 1000)) {
807 ClockFreq
= BaseClkFreq
* 1000;
811 // Calculate the divisor of base frequency.
814 SettingFreq
= BaseClkFreq
* 1000;
815 while (ClockFreq
< SettingFreq
) {
818 SettingFreq
= (BaseClkFreq
* 1000) / (2 * Divisor
);
819 Remainder
= (BaseClkFreq
* 1000) % (2 * Divisor
);
820 if ((ClockFreq
== SettingFreq
) && (Remainder
== 0)) {
823 if ((ClockFreq
== SettingFreq
) && (Remainder
!= 0)) {
828 DEBUG ((DEBUG_INFO
, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq
, Divisor
, ClockFreq
));
831 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
833 if ((ControllerVer
>= SD_MMC_HC_CTRL_VER_300
) &&
834 (ControllerVer
<= SD_MMC_HC_CTRL_VER_420
)) {
835 ASSERT (Divisor
<= 0x3FF);
836 ClockCtrl
= ((Divisor
& 0xFF) << 8) | ((Divisor
& 0x300) >> 2);
837 } else if ((ControllerVer
== SD_MMC_HC_CTRL_VER_100
) ||
838 (ControllerVer
== SD_MMC_HC_CTRL_VER_200
)) {
840 // Only the most significant bit can be used as divisor.
842 if (((Divisor
- 1) & Divisor
) != 0) {
843 Divisor
= 1 << (HighBitSet32 (Divisor
) + 1);
845 ASSERT (Divisor
<= 0x80);
846 ClockCtrl
= (Divisor
& 0xFF) << 8;
848 DEBUG ((DEBUG_ERROR
, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer
));
849 return EFI_UNSUPPORTED
;
853 // Stop bus clock at first
855 Status
= SdMmcHcStopClock (PciIo
, Slot
);
856 if (EFI_ERROR (Status
)) {
861 // Supply clock frequency with specified divisor
864 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, FALSE
, sizeof (ClockCtrl
), &ClockCtrl
);
865 if (EFI_ERROR (Status
)) {
866 DEBUG ((DEBUG_ERROR
, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
871 // Wait Internal Clock Stable in the Clock Control register to be 1
873 Status
= SdMmcHcWaitMmioSet (
876 SD_MMC_HC_CLOCK_CTRL
,
880 SD_MMC_HC_GENERIC_TIMEOUT
882 if (EFI_ERROR (Status
)) {
887 // Set SD Clock Enable in the Clock Control register to 1
890 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
896 SD/MMC bus power control.
898 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
900 @param[in] PciIo The PCI IO protocol instance.
901 @param[in] Slot The slot number of the SD card to send the command to.
902 @param[in] PowerCtrl The value setting to the power control register.
904 @retval TRUE There is a SD/MMC card attached.
905 @retval FALSE There is no a SD/MMC card attached.
909 SdMmcHcPowerControl (
910 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
920 PowerCtrl
&= (UINT8
)~BIT0
;
921 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
922 if (EFI_ERROR (Status
)) {
927 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
930 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
936 Set the SD/MMC bus width.
938 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
940 @param[in] PciIo The PCI IO protocol instance.
941 @param[in] Slot The slot number of the SD card to send the command to.
942 @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.
944 @retval EFI_SUCCESS The bus width is set successfully.
945 @retval Others The bus width isn't set successfully.
950 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
959 HostCtrl1
= (UINT8
)~(BIT5
| BIT1
);
960 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
961 } else if (BusWidth
== 4) {
962 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
963 if (EFI_ERROR (Status
)) {
967 HostCtrl1
&= (UINT8
)~BIT5
;
968 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
969 } else if (BusWidth
== 8) {
970 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
971 if (EFI_ERROR (Status
)) {
974 HostCtrl1
&= (UINT8
)~BIT1
;
976 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
979 return EFI_INVALID_PARAMETER
;
986 Configure V4 controller enhancements at initialization.
988 @param[in] PciIo The PCI IO protocol instance.
989 @param[in] Slot The slot number of the SD card to send the command to.
990 @param[in] Capability The capability of the slot.
991 @param[in] ControllerVer The version of host controller.
993 @retval EFI_SUCCESS The clock is supplied successfully.
997 SdMmcHcInitV4Enhancements (
998 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1000 IN SD_MMC_HC_SLOT_CAP Capability
,
1001 IN UINT16 ControllerVer
1008 // Check if controller version V4 or higher
1010 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_400
) {
1011 HostCtrl2
= SD_MMC_HC_V4_EN
;
1013 // Check if V4 64bit support is available
1015 if (Capability
.SysBus64V4
!= 0) {
1016 HostCtrl2
|= SD_MMC_HC_64_ADDR_EN
;
1017 DEBUG ((DEBUG_INFO
, "Enabled V4 64 bit system bus support\n"));
1020 // Check if controller version V4.10 or higher
1022 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_410
) {
1023 HostCtrl2
|= SD_MMC_HC_26_DATA_LEN_ADMA_EN
;
1024 DEBUG ((DEBUG_INFO
, "Enabled V4 26 bit data length ADMA support\n"));
1026 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1027 if (EFI_ERROR (Status
)) {
1036 Supply SD/MMC card with lowest clock frequency at initialization.
1038 @param[in] PciIo The PCI IO protocol instance.
1039 @param[in] Slot The slot number of the SD card to send the command to.
1040 @param[in] BaseClkFreq The base clock frequency of host controller in MHz.
1041 @param[in] ControllerVer The version of host controller.
1043 @retval EFI_SUCCESS The clock is supplied successfully.
1044 @retval Others The clock isn't supplied successfully.
1048 SdMmcHcInitClockFreq (
1049 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1051 IN UINT32 BaseClkFreq
,
1052 IN UINT16 ControllerVer
1059 // According to SDHCI specification ver. 4.2, BaseClkFreq field value of
1060 // the Capability Register 1 can be zero, which means a need for obtaining
1061 // the clock frequency via another method. Fail in case it is not updated
1062 // by SW at this point.
1064 if (BaseClkFreq
== 0) {
1066 // Don't support get Base Clock Frequency information via another method
1068 return EFI_UNSUPPORTED
;
1071 // Supply 400KHz clock frequency at initialization phase.
1074 Status
= SdMmcHcClockSupply (PciIo
, Slot
, InitFreq
, BaseClkFreq
, ControllerVer
);
1079 Supply SD/MMC card with maximum voltage at initialization.
1081 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
1083 @param[in] PciIo The PCI IO protocol instance.
1084 @param[in] Slot The slot number of the SD card to send the command to.
1085 @param[in] Capability The capability of the slot.
1087 @retval EFI_SUCCESS The voltage is supplied successfully.
1088 @retval Others The voltage isn't supplied successfully.
1092 SdMmcHcInitPowerVoltage (
1093 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1095 IN SD_MMC_HC_SLOT_CAP Capability
1103 // Calculate supported maximum voltage according to SD Bus Voltage Select
1105 if (Capability
.Voltage33
!= 0) {
1110 } else if (Capability
.Voltage30
!= 0) {
1115 } else if (Capability
.Voltage18
!= 0) {
1121 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1123 if (EFI_ERROR (Status
)) {
1128 return EFI_DEVICE_ERROR
;
1132 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
1134 Status
= SdMmcHcPowerControl (PciIo
, Slot
, MaxVoltage
);
1140 Initialize the Timeout Control register with most conservative value at initialization.
1142 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
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.
1147 @retval EFI_SUCCESS The timeout control register is configured successfully.
1148 @retval Others The timeout control register isn't configured successfully.
1152 SdMmcHcInitTimeoutCtrl (
1153 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1161 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_TIMEOUT_CTRL
, FALSE
, sizeof (Timeout
), &Timeout
);
1167 Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value
1170 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1171 @param[in] Slot The slot number of the SD card to send the command to.
1173 @retval EFI_SUCCESS The host controller is initialized successfully.
1174 @retval Others The host controller isn't initialized successfully.
1179 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1184 EFI_PCI_IO_PROTOCOL
*PciIo
;
1185 SD_MMC_HC_SLOT_CAP Capability
;
1188 // Notify the SD/MMC override protocol that we are about to initialize
1189 // the SD/MMC host controller.
1191 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1192 Status
= mOverride
->NotifyPhase (
1193 Private
->ControllerHandle
,
1195 EdkiiSdMmcInitHostPre
,
1197 if (EFI_ERROR (Status
)) {
1199 "%a: SD/MMC pre init notifier callback failed - %r\n",
1200 __FUNCTION__
, Status
));
1205 PciIo
= Private
->PciIo
;
1206 Capability
= Private
->Capability
[Slot
];
1208 Status
= SdMmcHcInitV4Enhancements (PciIo
, Slot
, Capability
, Private
->ControllerVersion
[Slot
]);
1209 if (EFI_ERROR (Status
)) {
1213 Status
= SdMmcHcInitClockFreq (PciIo
, Slot
, Private
->BaseClkFreq
[Slot
], Private
->ControllerVersion
[Slot
]);
1214 if (EFI_ERROR (Status
)) {
1218 Status
= SdMmcHcInitPowerVoltage (PciIo
, Slot
, Capability
);
1219 if (EFI_ERROR (Status
)) {
1223 Status
= SdMmcHcInitTimeoutCtrl (PciIo
, Slot
);
1224 if (EFI_ERROR (Status
)) {
1229 // Notify the SD/MMC override protocol that we are have just initialized
1230 // the SD/MMC host controller.
1232 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1233 Status
= mOverride
->NotifyPhase (
1234 Private
->ControllerHandle
,
1236 EdkiiSdMmcInitHostPost
,
1238 if (EFI_ERROR (Status
)) {
1240 "%a: SD/MMC post init notifier callback failed - %r\n",
1241 __FUNCTION__
, Status
));
1248 Set SD Host Controler control 2 registry according to selected speed.
1250 @param[in] ControllerHandle The handle of the controller.
1251 @param[in] PciIo The PCI IO protocol instance.
1252 @param[in] Slot The slot number of the SD card to send the command to.
1253 @param[in] Timing The timing to select.
1255 @retval EFI_SUCCESS The timing is set successfully.
1256 @retval Others The timing isn't set successfully.
1259 SdMmcHcUhsSignaling (
1260 IN EFI_HANDLE ControllerHandle
,
1261 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1263 IN SD_MMC_BUS_MODE Timing
1269 HostCtrl2
= (UINT8
)~SD_MMC_HC_CTRL_UHS_MASK
;
1270 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1271 if (EFI_ERROR (Status
)) {
1277 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR12
;
1280 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR25
;
1283 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR50
;
1285 case SdMmcUhsSdr104
:
1286 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR104
;
1289 HostCtrl2
= SD_MMC_HC_CTRL_UHS_DDR50
;
1291 case SdMmcMmcLegacy
:
1292 HostCtrl2
= SD_MMC_HC_CTRL_MMC_LEGACY
;
1295 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_SDR
;
1298 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_DDR
;
1301 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS200
;
1304 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS400
;
1310 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1311 if (EFI_ERROR (Status
)) {
1315 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1316 Status
= mOverride
->NotifyPhase (
1319 EdkiiSdMmcUhsSignaling
,
1322 if (EFI_ERROR (Status
)) {
1325 "%a: SD/MMC uhs signaling notifier callback failed - %r\n",
1339 @param[in] PciIo The PCI IO protocol instance.
1340 @param[in] Slot The slot number of the SD card to send the command to.
1341 @param[in] On The boolean to turn on/off LED.
1343 @retval EFI_SUCCESS The LED is turned on/off successfully.
1344 @retval Others The LED isn't turned on/off successfully.
1349 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1359 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1361 HostCtrl1
= (UINT8
)~BIT0
;
1362 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1369 Build ADMA descriptor table for transfer.
1371 Refer to SD Host Controller Simplified spec 4.2 Section 1.13 for details.
1373 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1374 @param[in] ControllerVer The version of host controller.
1376 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
1377 @retval Others The ADMA descriptor table isn't created successfully.
1381 BuildAdmaDescTable (
1382 IN SD_MMC_HC_TRB
*Trb
,
1383 IN UINT16 ControllerVer
1386 EFI_PHYSICAL_ADDRESS Data
;
1393 EFI_PCI_IO_PROTOCOL
*PciIo
;
1396 BOOLEAN AddressingMode64
;
1397 BOOLEAN DataLength26
;
1398 UINT32 AdmaMaxDataPerLine
;
1402 AddressingMode64
= FALSE
;
1403 DataLength26
= FALSE
;
1404 AdmaMaxDataPerLine
= ADMA_MAX_DATA_PER_LINE_16B
;
1405 DescSize
= sizeof (SD_MMC_HC_ADMA_32_DESC_LINE
);
1408 Data
= Trb
->DataPhy
;
1409 DataLen
= Trb
->DataLen
;
1410 PciIo
= Trb
->Private
->PciIo
;
1413 // Detect whether 64bit addressing is supported.
1415 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_400
) {
1416 Status
= SdMmcHcCheckMmioSet(PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof(UINT16
),
1417 SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
, SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
);
1418 if (!EFI_ERROR (Status
)) {
1419 AddressingMode64
= TRUE
;
1420 DescSize
= sizeof (SD_MMC_HC_ADMA_64_DESC_LINE
);
1424 // Check for valid ranges in 32bit ADMA Descriptor Table
1426 if (!AddressingMode64
&&
1427 ((Data
>= 0x100000000ul
) || ((Data
+ DataLen
) > 0x100000000ul
))) {
1428 return EFI_INVALID_PARAMETER
;
1431 // Check address field alignment
1433 if (AddressingMode64
) {
1435 // Address field shall be set on 64-bit boundary (Lower 3-bit is always set to 0)
1437 if ((Data
& (BIT0
| BIT1
| BIT2
)) != 0) {
1438 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 8 bytes boundary!\n", Data
));
1442 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
1444 if ((Data
& (BIT0
| BIT1
)) != 0) {
1445 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data
));
1449 // Detect whether 26bit data length is supported.
1451 Status
= SdMmcHcCheckMmioSet(PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof(UINT16
),
1452 SD_MMC_HC_26_DATA_LEN_ADMA_EN
, SD_MMC_HC_26_DATA_LEN_ADMA_EN
);
1453 if (!EFI_ERROR (Status
)) {
1454 DataLength26
= TRUE
;
1455 AdmaMaxDataPerLine
= ADMA_MAX_DATA_PER_LINE_26B
;
1458 Entries
= DivU64x32 ((DataLen
+ AdmaMaxDataPerLine
- 1), AdmaMaxDataPerLine
);
1459 TableSize
= (UINTN
)MultU64x32 (Entries
, DescSize
);
1460 Trb
->AdmaPages
= (UINT32
)EFI_SIZE_TO_PAGES (TableSize
);
1461 Status
= PciIo
->AllocateBuffer (
1464 EfiBootServicesData
,
1465 EFI_SIZE_TO_PAGES (TableSize
),
1469 if (EFI_ERROR (Status
)) {
1470 return EFI_OUT_OF_RESOURCES
;
1472 ZeroMem (AdmaDesc
, TableSize
);
1474 Status
= PciIo
->Map (
1476 EfiPciIoOperationBusMasterCommonBuffer
,
1483 if (EFI_ERROR (Status
) || (Bytes
!= TableSize
)) {
1485 // Map error or unable to map the whole RFis buffer into a contiguous region.
1489 EFI_SIZE_TO_PAGES (TableSize
),
1492 return EFI_OUT_OF_RESOURCES
;
1495 if ((!AddressingMode64
) &&
1496 (UINT64
)(UINTN
)Trb
->AdmaDescPhy
> 0x100000000ul
) {
1498 // The ADMA doesn't support 64bit addressing.
1506 EFI_SIZE_TO_PAGES (TableSize
),
1509 return EFI_DEVICE_ERROR
;
1512 Remaining
= DataLen
;
1514 if (!AddressingMode64
) {
1515 Trb
->Adma32Desc
= AdmaDesc
;
1516 Trb
->Adma64Desc
= NULL
;
1518 Trb
->Adma64Desc
= AdmaDesc
;
1519 Trb
->Adma32Desc
= NULL
;
1521 for (Index
= 0; Index
< Entries
; Index
++) {
1522 if (!AddressingMode64
) {
1523 if (Remaining
<= AdmaMaxDataPerLine
) {
1524 Trb
->Adma32Desc
[Index
].Valid
= 1;
1525 Trb
->Adma32Desc
[Index
].Act
= 2;
1527 Trb
->Adma32Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1529 Trb
->Adma32Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1530 Trb
->Adma32Desc
[Index
].Address
= (UINT32
)Address
;
1533 Trb
->Adma32Desc
[Index
].Valid
= 1;
1534 Trb
->Adma32Desc
[Index
].Act
= 2;
1536 Trb
->Adma32Desc
[Index
].UpperLength
= 0;
1538 Trb
->Adma32Desc
[Index
].LowerLength
= 0;
1539 Trb
->Adma32Desc
[Index
].Address
= (UINT32
)Address
;
1542 if (Remaining
<= AdmaMaxDataPerLine
) {
1543 Trb
->Adma64Desc
[Index
].Valid
= 1;
1544 Trb
->Adma64Desc
[Index
].Act
= 2;
1546 Trb
->Adma64Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1548 Trb
->Adma64Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1549 Trb
->Adma64Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1550 Trb
->Adma64Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1553 Trb
->Adma64Desc
[Index
].Valid
= 1;
1554 Trb
->Adma64Desc
[Index
].Act
= 2;
1556 Trb
->Adma64Desc
[Index
].UpperLength
= 0;
1558 Trb
->Adma64Desc
[Index
].LowerLength
= 0;
1559 Trb
->Adma64Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1560 Trb
->Adma64Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1564 Remaining
-= AdmaMaxDataPerLine
;
1565 Address
+= AdmaMaxDataPerLine
;
1569 // Set the last descriptor line as end of descriptor table
1571 AddressingMode64
? (Trb
->Adma64Desc
[Index
].End
= 1) : (Trb
->Adma32Desc
[Index
].End
= 1);
1576 Create a new TRB for the SD/MMC cmd request.
1578 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1579 @param[in] Slot The slot number of the SD card to send the command to.
1580 @param[in] Packet A pointer to the SD command data structure.
1581 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1582 not NULL, then nonblocking I/O is performed, and Event
1583 will be signaled when the Packet completes.
1585 @return Created Trb or NULL.
1590 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1592 IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1599 EFI_PCI_IO_PROTOCOL_OPERATION Flag
;
1600 EFI_PCI_IO_PROTOCOL
*PciIo
;
1603 Trb
= AllocateZeroPool (sizeof (SD_MMC_HC_TRB
));
1608 Trb
->Signature
= SD_MMC_HC_TRB_SIG
;
1610 Trb
->BlockSize
= 0x200;
1611 Trb
->Packet
= Packet
;
1613 Trb
->Started
= FALSE
;
1614 Trb
->Timeout
= Packet
->Timeout
;
1615 Trb
->Private
= Private
;
1617 if ((Packet
->InTransferLength
!= 0) && (Packet
->InDataBuffer
!= NULL
)) {
1618 Trb
->Data
= Packet
->InDataBuffer
;
1619 Trb
->DataLen
= Packet
->InTransferLength
;
1621 } else if ((Packet
->OutTransferLength
!= 0) && (Packet
->OutDataBuffer
!= NULL
)) {
1622 Trb
->Data
= Packet
->OutDataBuffer
;
1623 Trb
->DataLen
= Packet
->OutTransferLength
;
1625 } else if ((Packet
->InTransferLength
== 0) && (Packet
->OutTransferLength
== 0)) {
1632 if ((Trb
->DataLen
!= 0) && (Trb
->DataLen
< Trb
->BlockSize
)) {
1633 Trb
->BlockSize
= (UINT16
)Trb
->DataLen
;
1636 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1637 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1638 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1639 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
1640 Trb
->Mode
= SdMmcPioMode
;
1643 Flag
= EfiPciIoOperationBusMasterWrite
;
1645 Flag
= EfiPciIoOperationBusMasterRead
;
1648 PciIo
= Private
->PciIo
;
1649 if (Trb
->DataLen
!= 0) {
1650 MapLength
= Trb
->DataLen
;
1651 Status
= PciIo
->Map (
1659 if (EFI_ERROR (Status
) || (Trb
->DataLen
!= MapLength
)) {
1660 Status
= EFI_BAD_BUFFER_SIZE
;
1665 if (Trb
->DataLen
== 0) {
1666 Trb
->Mode
= SdMmcNoData
;
1667 } else if (Private
->Capability
[Slot
].Adma2
!= 0) {
1668 Trb
->Mode
= SdMmcAdmaMode
;
1669 Status
= BuildAdmaDescTable (Trb
, Private
->ControllerVersion
[Slot
]);
1670 if (EFI_ERROR (Status
)) {
1671 PciIo
->Unmap (PciIo
, Trb
->DataMap
);
1674 } else if (Private
->Capability
[Slot
].Sdma
!= 0) {
1675 Trb
->Mode
= SdMmcSdmaMode
;
1677 Trb
->Mode
= SdMmcPioMode
;
1681 if (Event
!= NULL
) {
1682 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1683 InsertTailList (&Private
->Queue
, &Trb
->TrbList
);
1684 gBS
->RestoreTPL (OldTpl
);
1695 Free the resource used by the TRB.
1697 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1702 IN SD_MMC_HC_TRB
*Trb
1705 EFI_PCI_IO_PROTOCOL
*PciIo
;
1707 PciIo
= Trb
->Private
->PciIo
;
1709 if (Trb
->AdmaMap
!= NULL
) {
1715 if (Trb
->Adma32Desc
!= NULL
) {
1722 if (Trb
->Adma64Desc
!= NULL
) {
1729 if (Trb
->DataMap
!= NULL
) {
1740 Check if the env is ready for execute specified TRB.
1742 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1743 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1745 @retval EFI_SUCCESS The env is ready for TRB execution.
1746 @retval EFI_NOT_READY The env is not ready for TRB execution.
1747 @retval Others Some erros happen.
1752 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1753 IN SD_MMC_HC_TRB
*Trb
1757 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1758 EFI_PCI_IO_PROTOCOL
*PciIo
;
1759 UINT32 PresentState
;
1761 Packet
= Trb
->Packet
;
1763 if ((Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) ||
1764 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR1b
) ||
1765 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR5b
)) {
1767 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1768 // the Present State register to be 0
1770 PresentState
= BIT0
| BIT1
;
1773 // Wait Command Inhibit (CMD) in the Present State register
1776 PresentState
= BIT0
;
1779 PciIo
= Private
->PciIo
;
1780 Status
= SdMmcHcCheckMmioSet (
1783 SD_MMC_HC_PRESENT_STATE
,
1784 sizeof (PresentState
),
1793 Wait for the env to be ready for execute specified TRB.
1795 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1796 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1798 @retval EFI_SUCCESS The env is ready for TRB execution.
1799 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1800 @retval Others Some erros happen.
1805 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1806 IN SD_MMC_HC_TRB
*Trb
1810 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1812 BOOLEAN InfiniteWait
;
1815 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1817 Packet
= Trb
->Packet
;
1818 Timeout
= Packet
->Timeout
;
1820 InfiniteWait
= TRUE
;
1822 InfiniteWait
= FALSE
;
1825 while (InfiniteWait
|| (Timeout
> 0)) {
1827 // Check Trb execution result by reading Normal Interrupt Status register.
1829 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
1830 if (Status
!= EFI_NOT_READY
) {
1834 // Stall for 1 microsecond.
1845 Execute the specified TRB.
1847 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1848 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1850 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1851 @retval Others Some erros happen when sending this request to the host controller.
1856 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1857 IN SD_MMC_HC_TRB
*Trb
1861 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1862 EFI_PCI_IO_PROTOCOL
*PciIo
;
1872 BOOLEAN AddressingMode64
;
1874 AddressingMode64
= FALSE
;
1876 Packet
= Trb
->Packet
;
1877 PciIo
= Trb
->Private
->PciIo
;
1879 // Clear all bits in Error Interrupt Status Register
1882 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ERR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1883 if (EFI_ERROR (Status
)) {
1887 // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.
1890 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1891 if (EFI_ERROR (Status
)) {
1895 // Set Host Control 1 register DMA Select field
1897 if (Trb
->Mode
== SdMmcAdmaMode
) {
1899 Status
= SdMmcHcOrMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1900 if (EFI_ERROR (Status
)) {
1905 SdMmcHcLedOnOff (PciIo
, Trb
->Slot
, TRUE
);
1907 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
1908 Status
= SdMmcHcCheckMmioSet(PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof(UINT16
),
1909 SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
, SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
);
1910 if (!EFI_ERROR (Status
)) {
1911 AddressingMode64
= TRUE
;
1915 if (Trb
->Mode
== SdMmcSdmaMode
) {
1916 if ((!AddressingMode64
) &&
1917 ((UINT64
)(UINTN
)Trb
->DataPhy
>= 0x100000000ul
)) {
1918 return EFI_INVALID_PARAMETER
;
1921 SdmaAddr
= (UINT64
)(UINTN
)Trb
->DataPhy
;
1923 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
1924 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (UINT64
), &SdmaAddr
);
1926 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (UINT32
), &SdmaAddr
);
1929 if (EFI_ERROR (Status
)) {
1932 } else if (Trb
->Mode
== SdMmcAdmaMode
) {
1933 AdmaAddr
= (UINT64
)(UINTN
)Trb
->AdmaDescPhy
;
1934 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (AdmaAddr
), &AdmaAddr
);
1935 if (EFI_ERROR (Status
)) {
1940 BlkSize
= Trb
->BlockSize
;
1941 if (Trb
->Mode
== SdMmcSdmaMode
) {
1943 // Set SDMA boundary to be 512K bytes.
1948 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_SIZE
, FALSE
, sizeof (BlkSize
), &BlkSize
);
1949 if (EFI_ERROR (Status
)) {
1954 if (Trb
->Mode
!= SdMmcNoData
) {
1956 // Calcuate Block Count.
1958 BlkCount
= (Trb
->DataLen
/ Trb
->BlockSize
);
1960 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_410
) {
1961 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (UINT32
), &BlkCount
);
1963 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_COUNT
, FALSE
, sizeof (UINT16
), &BlkCount
);
1965 if (EFI_ERROR (Status
)) {
1969 Argument
= Packet
->SdMmcCmdBlk
->CommandArgument
;
1970 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ARG1
, FALSE
, sizeof (Argument
), &Argument
);
1971 if (EFI_ERROR (Status
)) {
1976 if (Trb
->Mode
!= SdMmcNoData
) {
1977 if (Trb
->Mode
!= SdMmcPioMode
) {
1984 TransMode
|= BIT5
| BIT1
;
1987 // Only SD memory card needs to use AUTO CMD12 feature.
1989 if (Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) {
1996 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_TRANS_MOD
, FALSE
, sizeof (TransMode
), &TransMode
);
1997 if (EFI_ERROR (Status
)) {
2001 Cmd
= (UINT16
)LShiftU64(Packet
->SdMmcCmdBlk
->CommandIndex
, 8);
2002 if (Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) {
2006 // Convert ResponseType to value
2008 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
2009 switch (Packet
->SdMmcCmdBlk
->ResponseType
) {
2010 case SdMmcResponseTypeR1
:
2011 case SdMmcResponseTypeR5
:
2012 case SdMmcResponseTypeR6
:
2013 case SdMmcResponseTypeR7
:
2014 Cmd
|= (BIT1
| BIT3
| BIT4
);
2016 case SdMmcResponseTypeR2
:
2017 Cmd
|= (BIT0
| BIT3
);
2019 case SdMmcResponseTypeR3
:
2020 case SdMmcResponseTypeR4
:
2023 case SdMmcResponseTypeR1b
:
2024 case SdMmcResponseTypeR5b
:
2025 Cmd
|= (BIT0
| BIT1
| BIT3
| BIT4
);
2035 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_COMMAND
, FALSE
, sizeof (Cmd
), &Cmd
);
2040 Check the TRB execution result.
2042 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2043 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2045 @retval EFI_SUCCESS The TRB is executed successfully.
2046 @retval EFI_NOT_READY The TRB is not completed for execution.
2047 @retval Others Some erros happen when executing this request.
2051 SdMmcCheckTrbResult (
2052 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2053 IN SD_MMC_HC_TRB
*Trb
2057 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2066 Packet
= Trb
->Packet
;
2068 // Check Trb execution result by reading Normal Interrupt Status register.
2070 Status
= SdMmcHcRwMmio (
2073 SD_MMC_HC_NOR_INT_STS
,
2078 if (EFI_ERROR (Status
)) {
2082 // Check Transfer Complete bit is set or not.
2084 if ((IntStatus
& BIT1
) == BIT1
) {
2085 if ((IntStatus
& BIT15
) == BIT15
) {
2087 // Read Error Interrupt Status register to check if the error is
2088 // Data Timeout Error.
2089 // If yes, treat it as success as Transfer Complete has higher
2090 // priority than Data Timeout Error.
2092 Status
= SdMmcHcRwMmio (
2095 SD_MMC_HC_ERR_INT_STS
,
2100 if (!EFI_ERROR (Status
)) {
2101 if ((IntStatus
& BIT4
) == BIT4
) {
2102 Status
= EFI_SUCCESS
;
2104 Status
= EFI_DEVICE_ERROR
;
2112 // Check if there is a error happened during cmd execution.
2113 // If yes, then do error recovery procedure to follow SD Host Controller
2114 // Simplified Spec 3.0 section 3.10.1.
2116 if ((IntStatus
& BIT15
) == BIT15
) {
2117 Status
= SdMmcHcRwMmio (
2120 SD_MMC_HC_ERR_INT_STS
,
2125 if (EFI_ERROR (Status
)) {
2128 if ((IntStatus
& 0x0F) != 0) {
2131 if ((IntStatus
& 0xF0) != 0) {
2135 Status
= SdMmcHcRwMmio (
2143 if (EFI_ERROR (Status
)) {
2146 Status
= SdMmcHcWaitMmioSet (
2153 SD_MMC_HC_GENERIC_TIMEOUT
2155 if (EFI_ERROR (Status
)) {
2159 Status
= EFI_DEVICE_ERROR
;
2163 // Check if DMA interrupt is signalled for the SDMA transfer.
2165 if ((Trb
->Mode
== SdMmcSdmaMode
) && ((IntStatus
& BIT3
) == BIT3
)) {
2167 // Clear DMA interrupt bit.
2170 Status
= SdMmcHcRwMmio (
2173 SD_MMC_HC_NOR_INT_STS
,
2178 if (EFI_ERROR (Status
)) {
2182 // Update SDMA Address register.
2184 SdmaAddr
= SD_MMC_SDMA_ROUND_UP ((UINTN
)Trb
->DataPhy
, SD_MMC_SDMA_BOUNDARY
);
2186 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
2187 Status
= SdMmcHcRwMmio (
2190 SD_MMC_HC_ADMA_SYS_ADDR
,
2196 Status
= SdMmcHcRwMmio (
2199 SD_MMC_HC_SDMA_ADDR
,
2206 if (EFI_ERROR (Status
)) {
2209 Trb
->DataPhy
= (UINT64
)(UINTN
)SdmaAddr
;
2212 if ((Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeAdtc
) &&
2213 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR1b
) &&
2214 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR5b
)) {
2215 if ((IntStatus
& BIT0
) == BIT0
) {
2216 Status
= EFI_SUCCESS
;
2221 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
2222 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
2223 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
2224 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
2226 // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,
2227 // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.
2228 // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.
2230 if ((IntStatus
& BIT5
) == BIT5
) {
2232 // Clear Buffer Read Ready interrupt at first.
2235 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
2237 // Read data out from Buffer Port register
2239 for (PioLength
= 0; PioLength
< Trb
->DataLen
; PioLength
+= 4) {
2240 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_BUF_DAT_PORT
, TRUE
, 4, (UINT8
*)Trb
->Data
+ PioLength
);
2242 Status
= EFI_SUCCESS
;
2247 Status
= EFI_NOT_READY
;
2250 // Get response data when the cmd is executed successfully.
2252 if (!EFI_ERROR (Status
)) {
2253 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
2254 for (Index
= 0; Index
< 4; Index
++) {
2255 Status
= SdMmcHcRwMmio (
2258 SD_MMC_HC_RESPONSE
+ Index
* 4,
2263 if (EFI_ERROR (Status
)) {
2264 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2268 CopyMem (Packet
->SdMmcStatusBlk
, Response
, sizeof (Response
));
2272 if (Status
!= EFI_NOT_READY
) {
2273 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2280 Wait for the TRB execution result.
2282 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2283 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2285 @retval EFI_SUCCESS The TRB is executed successfully.
2286 @retval Others Some erros happen when executing this request.
2290 SdMmcWaitTrbResult (
2291 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2292 IN SD_MMC_HC_TRB
*Trb
2296 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2298 BOOLEAN InfiniteWait
;
2300 Packet
= Trb
->Packet
;
2302 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
2304 Timeout
= Packet
->Timeout
;
2306 InfiniteWait
= TRUE
;
2308 InfiniteWait
= FALSE
;
2311 while (InfiniteWait
|| (Timeout
> 0)) {
2313 // Check Trb execution result by reading Normal Interrupt Status register.
2315 Status
= SdMmcCheckTrbResult (Private
, Trb
);
2316 if (Status
!= EFI_NOT_READY
) {
2320 // Stall for 1 microsecond.