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
,
158 if ((PciIo
== NULL
) || (Data
== NULL
)) {
159 return EFI_INVALID_PARAMETER
;
162 if ((Count
!= 1) && (Count
!= 2) && (Count
!= 4) && (Count
!= 8)) {
163 return EFI_INVALID_PARAMETER
;
167 Status
= PciIo
->Mem
.Read (
176 Status
= PciIo
->Mem
.Write (
190 Do OR operation with the value of the specified SD/MMC host controller mmio register.
192 @param[in] PciIo The PCI IO protocol instance.
193 @param[in] BarIndex The BAR index of the standard PCI Configuration
194 header to use as the base address for the memory
195 operation to perform.
196 @param[in] Offset The offset within the selected BAR to start the
198 @param[in] Count The width of the mmio register in bytes.
199 Must be 1, 2 , 4 or 8 bytes.
200 @param[in] OrData The pointer to the data used to do OR operation.
201 The caller is responsible for having ownership of
202 the data buffer and ensuring its size not less than
205 @retval EFI_INVALID_PARAMETER The PciIo or OrData is NULL or the Count is not valid.
206 @retval EFI_SUCCESS The OR operation succeeds.
207 @retval Others The OR operation fails.
213 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
224 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
225 if (EFI_ERROR (Status
)) {
230 Or
= *(UINT8
*) OrData
;
231 } else if (Count
== 2) {
232 Or
= *(UINT16
*) OrData
;
233 } else if (Count
== 4) {
234 Or
= *(UINT32
*) OrData
;
235 } else if (Count
== 8) {
236 Or
= *(UINT64
*) OrData
;
238 return EFI_INVALID_PARAMETER
;
242 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
248 Do AND operation with the value of the specified SD/MMC host controller mmio register.
250 @param[in] PciIo The PCI IO protocol instance.
251 @param[in] BarIndex The BAR index of the standard PCI Configuration
252 header to use as the base address for the memory
253 operation to perform.
254 @param[in] Offset The offset within the selected BAR to start the
256 @param[in] Count The width of the mmio register in bytes.
257 Must be 1, 2 , 4 or 8 bytes.
258 @param[in] AndData The pointer to the data used to do AND operation.
259 The caller is responsible for having ownership of
260 the data buffer and ensuring its size not less than
263 @retval EFI_INVALID_PARAMETER The PciIo or AndData is NULL or the Count is not valid.
264 @retval EFI_SUCCESS The AND operation succeeds.
265 @retval Others The AND operation fails.
271 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
282 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
283 if (EFI_ERROR (Status
)) {
288 And
= *(UINT8
*) AndData
;
289 } else if (Count
== 2) {
290 And
= *(UINT16
*) AndData
;
291 } else if (Count
== 4) {
292 And
= *(UINT32
*) AndData
;
293 } else if (Count
== 8) {
294 And
= *(UINT64
*) AndData
;
296 return EFI_INVALID_PARAMETER
;
300 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
306 Wait for the value of the specified MMIO register set to the test value.
308 @param[in] PciIo The PCI IO protocol instance.
309 @param[in] BarIndex The BAR index of the standard PCI Configuration
310 header to use as the base address for the memory
311 operation to perform.
312 @param[in] Offset The offset within the selected BAR to start the
314 @param[in] Count The width of the mmio register in bytes.
315 Must be 1, 2, 4 or 8 bytes.
316 @param[in] MaskValue The mask value of memory.
317 @param[in] TestValue The test value of memory.
319 @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.
320 @retval EFI_SUCCESS The MMIO register has expected value.
321 @retval Others The MMIO operation fails.
326 SdMmcHcCheckMmioSet (
327 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
339 // Access PCI MMIO space to see if the value is the tested one.
342 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Value
);
343 if (EFI_ERROR (Status
)) {
349 if (Value
== TestValue
) {
353 return EFI_NOT_READY
;
357 Wait for the value of the specified MMIO register set to the test value.
359 @param[in] PciIo The PCI IO protocol instance.
360 @param[in] BarIndex The BAR index of the standard PCI Configuration
361 header to use as the base address for the memory
362 operation to perform.
363 @param[in] Offset The offset within the selected BAR to start the
365 @param[in] Count The width of the mmio register in bytes.
366 Must be 1, 2, 4 or 8 bytes.
367 @param[in] MaskValue The mask value of memory.
368 @param[in] TestValue The test value of memory.
369 @param[in] Timeout The time out value for wait memory set, uses 1
370 microsecond as a unit.
372 @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout
374 @retval EFI_SUCCESS The MMIO register has expected value.
375 @retval Others The MMIO operation fails.
381 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
391 BOOLEAN InfiniteWait
;
396 InfiniteWait
= FALSE
;
399 while (InfiniteWait
|| (Timeout
> 0)) {
400 Status
= SdMmcHcCheckMmioSet (
408 if (Status
!= EFI_NOT_READY
) {
413 // Stall for 1 microsecond.
424 Get the controller version information from the specified slot.
426 @param[in] PciIo The PCI IO protocol instance.
427 @param[in] Slot The slot number of the SD card to send the command to.
428 @param[out] Version The buffer to store the version information.
430 @retval EFI_SUCCESS The operation executes successfully.
431 @retval Others The operation fails.
435 SdMmcHcGetControllerVersion (
436 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
443 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CTRL_VER
, TRUE
, sizeof (UINT16
), Version
);
444 if (EFI_ERROR (Status
)) {
454 Software reset the specified SD/MMC host controller and enable all interrupts.
456 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
457 @param[in] Slot The slot number of the SD card to send the command to.
459 @retval EFI_SUCCESS The software reset executes successfully.
460 @retval Others The software reset fails.
465 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
471 EFI_PCI_IO_PROTOCOL
*PciIo
;
474 // Notify the SD/MMC override protocol that we are about to reset
475 // the SD/MMC host controller.
477 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
478 Status
= mOverride
->NotifyPhase (
479 Private
->ControllerHandle
,
483 if (EFI_ERROR (Status
)) {
485 "%a: SD/MMC pre reset notifier callback failed - %r\n",
486 __FUNCTION__
, Status
));
491 PciIo
= Private
->PciIo
;
493 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_SW_RST
, sizeof (SwReset
), &SwReset
);
495 if (EFI_ERROR (Status
)) {
496 DEBUG ((DEBUG_ERROR
, "SdMmcHcReset: write SW Reset for All fails: %r\n", Status
));
500 Status
= SdMmcHcWaitMmioSet (
507 SD_MMC_HC_GENERIC_TIMEOUT
509 if (EFI_ERROR (Status
)) {
510 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: reset done with %r\n", Status
));
515 // Enable all interrupt after reset all.
517 Status
= SdMmcHcEnableInterrupt (PciIo
, Slot
);
518 if (EFI_ERROR (Status
)) {
519 DEBUG ((DEBUG_INFO
, "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",
525 // Notify the SD/MMC override protocol that we have just reset
526 // the SD/MMC host controller.
528 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
529 Status
= mOverride
->NotifyPhase (
530 Private
->ControllerHandle
,
534 if (EFI_ERROR (Status
)) {
536 "%a: SD/MMC post reset notifier callback failed - %r\n",
537 __FUNCTION__
, Status
));
545 Set all interrupt status bits in Normal and Error Interrupt Status Enable
548 @param[in] PciIo The PCI IO protocol instance.
549 @param[in] Slot The slot number of the SD card to send the command to.
551 @retval EFI_SUCCESS The operation executes successfully.
552 @retval Others The operation fails.
556 SdMmcHcEnableInterrupt (
557 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
565 // Enable all bits in Error Interrupt Status Enable Register
568 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_ERR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
569 if (EFI_ERROR (Status
)) {
573 // Enable all bits in Normal Interrupt Status Enable Register
576 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
582 Get the capability data from the specified slot.
584 @param[in] PciIo The PCI IO protocol instance.
585 @param[in] Slot The slot number of the SD card to send the command to.
586 @param[out] Capability The buffer to store the capability data.
588 @retval EFI_SUCCESS The operation executes successfully.
589 @retval Others The operation fails.
593 SdMmcHcGetCapability (
594 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
596 OUT SD_MMC_HC_SLOT_CAP
*Capability
602 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CAP
, TRUE
, sizeof (Cap
), &Cap
);
603 if (EFI_ERROR (Status
)) {
607 CopyMem (Capability
, &Cap
, sizeof (Cap
));
613 Get the maximum current capability data from the specified slot.
615 @param[in] PciIo The PCI IO protocol instance.
616 @param[in] Slot The slot number of the SD card to send the command to.
617 @param[out] MaxCurrent The buffer to store the maximum current capability data.
619 @retval EFI_SUCCESS The operation executes successfully.
620 @retval Others The operation fails.
624 SdMmcHcGetMaxCurrent (
625 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
627 OUT UINT64
*MaxCurrent
632 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_MAX_CURRENT_CAP
, TRUE
, sizeof (UINT64
), MaxCurrent
);
638 Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller
641 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
643 @param[in] PciIo The PCI IO protocol instance.
644 @param[in] Slot The slot number of the SD card to send the command to.
645 @param[out] MediaPresent The pointer to the media present boolean value.
647 @retval EFI_SUCCESS There is no media change happened.
648 @retval EFI_MEDIA_CHANGED There is media change happened.
649 @retval Others The detection fails.
654 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
656 OUT BOOLEAN
*MediaPresent
664 // Check Present State Register to see if there is a card presented.
666 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_PRESENT_STATE
, TRUE
, sizeof (PresentState
), &PresentState
);
667 if (EFI_ERROR (Status
)) {
671 if ((PresentState
& BIT16
) != 0) {
672 *MediaPresent
= TRUE
;
674 *MediaPresent
= FALSE
;
678 // Check Normal Interrupt Status Register
680 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, TRUE
, sizeof (Data
), &Data
);
681 if (EFI_ERROR (Status
)) {
685 if ((Data
& (BIT6
| BIT7
)) != 0) {
687 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
690 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data
), &Data
);
691 if (EFI_ERROR (Status
)) {
695 return EFI_MEDIA_CHANGED
;
702 Stop SD/MMC card clock.
704 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
706 @param[in] PciIo The PCI IO protocol instance.
707 @param[in] Slot The slot number of the SD card to send the command to.
709 @retval EFI_SUCCESS Succeed to stop SD/MMC clock.
710 @retval Others Fail to stop SD/MMC clock.
715 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
724 // Ensure no SD transactions are occurring on the SD Bus by
725 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
726 // in the Present State register to be 0.
728 Status
= SdMmcHcWaitMmioSet (
731 SD_MMC_HC_PRESENT_STATE
,
732 sizeof (PresentState
),
735 SD_MMC_HC_GENERIC_TIMEOUT
737 if (EFI_ERROR (Status
)) {
742 // Set SD Clock Enable in the Clock Control register to 0
744 ClockCtrl
= (UINT16
)~BIT2
;
745 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
751 SD/MMC card clock supply.
753 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
755 @param[in] PciIo The PCI IO protocol instance.
756 @param[in] Slot The slot number of the SD card to send the command to.
757 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
758 @param[in] BaseClkFreq The base clock frequency of host controller in MHz.
759 @param[in] ControllerVer The version of host controller.
761 @retval EFI_SUCCESS The clock is supplied successfully.
762 @retval Others The clock isn't supplied successfully.
767 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
770 IN UINT32 BaseClkFreq
,
771 IN UINT16 ControllerVer
781 // Calculate a divisor for SD clock frequency
783 ASSERT (BaseClkFreq
!= 0);
785 if (ClockFreq
== 0) {
786 return EFI_INVALID_PARAMETER
;
789 if (ClockFreq
> (BaseClkFreq
* 1000)) {
790 ClockFreq
= BaseClkFreq
* 1000;
794 // Calculate the divisor of base frequency.
797 SettingFreq
= BaseClkFreq
* 1000;
798 while (ClockFreq
< SettingFreq
) {
801 SettingFreq
= (BaseClkFreq
* 1000) / (2 * Divisor
);
802 Remainder
= (BaseClkFreq
* 1000) % (2 * Divisor
);
803 if ((ClockFreq
== SettingFreq
) && (Remainder
== 0)) {
806 if ((ClockFreq
== SettingFreq
) && (Remainder
!= 0)) {
811 DEBUG ((DEBUG_INFO
, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq
, Divisor
, ClockFreq
));
814 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
816 if ((ControllerVer
>= SD_MMC_HC_CTRL_VER_300
) &&
817 (ControllerVer
<= SD_MMC_HC_CTRL_VER_420
)) {
818 ASSERT (Divisor
<= 0x3FF);
819 ClockCtrl
= ((Divisor
& 0xFF) << 8) | ((Divisor
& 0x300) >> 2);
820 } else if ((ControllerVer
== SD_MMC_HC_CTRL_VER_100
) ||
821 (ControllerVer
== SD_MMC_HC_CTRL_VER_200
)) {
823 // Only the most significant bit can be used as divisor.
825 if (((Divisor
- 1) & Divisor
) != 0) {
826 Divisor
= 1 << (HighBitSet32 (Divisor
) + 1);
828 ASSERT (Divisor
<= 0x80);
829 ClockCtrl
= (Divisor
& 0xFF) << 8;
831 DEBUG ((DEBUG_ERROR
, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer
));
832 return EFI_UNSUPPORTED
;
836 // Stop bus clock at first
838 Status
= SdMmcHcStopClock (PciIo
, Slot
);
839 if (EFI_ERROR (Status
)) {
844 // Supply clock frequency with specified divisor
847 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, FALSE
, sizeof (ClockCtrl
), &ClockCtrl
);
848 if (EFI_ERROR (Status
)) {
849 DEBUG ((DEBUG_ERROR
, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
854 // Wait Internal Clock Stable in the Clock Control register to be 1
856 Status
= SdMmcHcWaitMmioSet (
859 SD_MMC_HC_CLOCK_CTRL
,
863 SD_MMC_HC_GENERIC_TIMEOUT
865 if (EFI_ERROR (Status
)) {
870 // Set SD Clock Enable in the Clock Control register to 1
873 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
879 SD/MMC bus power control.
881 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
883 @param[in] PciIo The PCI IO protocol instance.
884 @param[in] Slot The slot number of the SD card to send the command to.
885 @param[in] PowerCtrl The value setting to the power control register.
887 @retval TRUE There is a SD/MMC card attached.
888 @retval FALSE There is no a SD/MMC card attached.
892 SdMmcHcPowerControl (
893 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
903 PowerCtrl
&= (UINT8
)~BIT0
;
904 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
905 if (EFI_ERROR (Status
)) {
910 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
913 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
919 Set the SD/MMC bus width.
921 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
923 @param[in] PciIo The PCI IO protocol instance.
924 @param[in] Slot The slot number of the SD card to send the command to.
925 @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.
927 @retval EFI_SUCCESS The bus width is set successfully.
928 @retval Others The bus width isn't set successfully.
933 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
942 HostCtrl1
= (UINT8
)~(BIT5
| BIT1
);
943 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
944 } else if (BusWidth
== 4) {
945 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
946 if (EFI_ERROR (Status
)) {
950 HostCtrl1
&= (UINT8
)~BIT5
;
951 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
952 } else if (BusWidth
== 8) {
953 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
954 if (EFI_ERROR (Status
)) {
957 HostCtrl1
&= (UINT8
)~BIT1
;
959 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
962 return EFI_INVALID_PARAMETER
;
969 Configure V4 controller enhancements at initialization.
971 @param[in] PciIo The PCI IO protocol instance.
972 @param[in] Slot The slot number of the SD card to send the command to.
973 @param[in] Capability The capability of the slot.
974 @param[in] ControllerVer The version of host controller.
976 @retval EFI_SUCCESS The clock is supplied successfully.
980 SdMmcHcInitV4Enhancements (
981 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
983 IN SD_MMC_HC_SLOT_CAP Capability
,
984 IN UINT16 ControllerVer
991 // Check if controller version V4 or higher
993 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_400
) {
994 HostCtrl2
= SD_MMC_HC_V4_EN
;
996 // Check if V4 64bit support is available
998 if (Capability
.SysBus64V4
!= 0) {
999 HostCtrl2
|= SD_MMC_HC_64_ADDR_EN
;
1000 DEBUG ((DEBUG_INFO
, "Enabled V4 64 bit system bus support\n"));
1003 // Check if controller version V4.10 or higher
1005 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_410
) {
1006 HostCtrl2
|= SD_MMC_HC_26_DATA_LEN_ADMA_EN
;
1007 DEBUG ((DEBUG_INFO
, "Enabled V4 26 bit data length ADMA support\n"));
1009 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1010 if (EFI_ERROR (Status
)) {
1019 Supply SD/MMC card with lowest clock frequency at initialization.
1021 @param[in] PciIo The PCI IO protocol instance.
1022 @param[in] Slot The slot number of the SD card to send the command to.
1023 @param[in] BaseClkFreq The base clock frequency of host controller in MHz.
1024 @param[in] ControllerVer The version of host controller.
1026 @retval EFI_SUCCESS The clock is supplied successfully.
1027 @retval Others The clock isn't supplied successfully.
1031 SdMmcHcInitClockFreq (
1032 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1034 IN UINT32 BaseClkFreq
,
1035 IN UINT16 ControllerVer
1042 // According to SDHCI specification ver. 4.2, BaseClkFreq field value of
1043 // the Capability Register 1 can be zero, which means a need for obtaining
1044 // the clock frequency via another method. Fail in case it is not updated
1045 // by SW at this point.
1047 if (BaseClkFreq
== 0) {
1049 // Don't support get Base Clock Frequency information via another method
1051 return EFI_UNSUPPORTED
;
1054 // Supply 400KHz clock frequency at initialization phase.
1057 Status
= SdMmcHcClockSupply (PciIo
, Slot
, InitFreq
, BaseClkFreq
, ControllerVer
);
1062 Supply SD/MMC card with maximum voltage at initialization.
1064 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
1066 @param[in] PciIo The PCI IO protocol instance.
1067 @param[in] Slot The slot number of the SD card to send the command to.
1068 @param[in] Capability The capability of the slot.
1070 @retval EFI_SUCCESS The voltage is supplied successfully.
1071 @retval Others The voltage isn't supplied successfully.
1075 SdMmcHcInitPowerVoltage (
1076 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1078 IN SD_MMC_HC_SLOT_CAP Capability
1086 // Calculate supported maximum voltage according to SD Bus Voltage Select
1088 if (Capability
.Voltage33
!= 0) {
1093 } else if (Capability
.Voltage30
!= 0) {
1098 } else if (Capability
.Voltage18
!= 0) {
1104 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1106 if (EFI_ERROR (Status
)) {
1111 return EFI_DEVICE_ERROR
;
1115 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
1117 Status
= SdMmcHcPowerControl (PciIo
, Slot
, MaxVoltage
);
1123 Initialize the Timeout Control register with most conservative value at initialization.
1125 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
1127 @param[in] PciIo The PCI IO protocol instance.
1128 @param[in] Slot The slot number of the SD card to send the command to.
1130 @retval EFI_SUCCESS The timeout control register is configured successfully.
1131 @retval Others The timeout control register isn't configured successfully.
1135 SdMmcHcInitTimeoutCtrl (
1136 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1144 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_TIMEOUT_CTRL
, FALSE
, sizeof (Timeout
), &Timeout
);
1150 Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value
1153 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1154 @param[in] Slot The slot number of the SD card to send the command to.
1156 @retval EFI_SUCCESS The host controller is initialized successfully.
1157 @retval Others The host controller isn't initialized successfully.
1162 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1167 EFI_PCI_IO_PROTOCOL
*PciIo
;
1168 SD_MMC_HC_SLOT_CAP Capability
;
1171 // Notify the SD/MMC override protocol that we are about to initialize
1172 // the SD/MMC host controller.
1174 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1175 Status
= mOverride
->NotifyPhase (
1176 Private
->ControllerHandle
,
1178 EdkiiSdMmcInitHostPre
,
1180 if (EFI_ERROR (Status
)) {
1182 "%a: SD/MMC pre init notifier callback failed - %r\n",
1183 __FUNCTION__
, Status
));
1188 PciIo
= Private
->PciIo
;
1189 Capability
= Private
->Capability
[Slot
];
1191 Status
= SdMmcHcInitV4Enhancements (PciIo
, Slot
, Capability
, Private
->ControllerVersion
[Slot
]);
1192 if (EFI_ERROR (Status
)) {
1196 Status
= SdMmcHcInitClockFreq (PciIo
, Slot
, Private
->BaseClkFreq
[Slot
], Private
->ControllerVersion
[Slot
]);
1197 if (EFI_ERROR (Status
)) {
1201 Status
= SdMmcHcInitPowerVoltage (PciIo
, Slot
, Capability
);
1202 if (EFI_ERROR (Status
)) {
1206 Status
= SdMmcHcInitTimeoutCtrl (PciIo
, Slot
);
1207 if (EFI_ERROR (Status
)) {
1212 // Notify the SD/MMC override protocol that we are have just initialized
1213 // the SD/MMC host controller.
1215 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1216 Status
= mOverride
->NotifyPhase (
1217 Private
->ControllerHandle
,
1219 EdkiiSdMmcInitHostPost
,
1221 if (EFI_ERROR (Status
)) {
1223 "%a: SD/MMC post init notifier callback failed - %r\n",
1224 __FUNCTION__
, Status
));
1231 Set SD Host Controler control 2 registry according to selected speed.
1233 @param[in] ControllerHandle The handle of the controller.
1234 @param[in] PciIo The PCI IO protocol instance.
1235 @param[in] Slot The slot number of the SD card to send the command to.
1236 @param[in] Timing The timing to select.
1238 @retval EFI_SUCCESS The timing is set successfully.
1239 @retval Others The timing isn't set successfully.
1242 SdMmcHcUhsSignaling (
1243 IN EFI_HANDLE ControllerHandle
,
1244 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1246 IN SD_MMC_BUS_MODE Timing
1252 HostCtrl2
= (UINT8
)~SD_MMC_HC_CTRL_UHS_MASK
;
1253 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1254 if (EFI_ERROR (Status
)) {
1260 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR12
;
1263 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR25
;
1266 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR50
;
1268 case SdMmcUhsSdr104
:
1269 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR104
;
1272 HostCtrl2
= SD_MMC_HC_CTRL_UHS_DDR50
;
1274 case SdMmcMmcLegacy
:
1275 HostCtrl2
= SD_MMC_HC_CTRL_MMC_LEGACY
;
1278 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_SDR
;
1281 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_DDR
;
1284 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS200
;
1287 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS400
;
1293 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1294 if (EFI_ERROR (Status
)) {
1298 if (mOverride
!= NULL
&& mOverride
->NotifyPhase
!= NULL
) {
1299 Status
= mOverride
->NotifyPhase (
1302 EdkiiSdMmcUhsSignaling
,
1305 if (EFI_ERROR (Status
)) {
1308 "%a: SD/MMC uhs signaling notifier callback failed - %r\n",
1322 @param[in] PciIo The PCI IO protocol instance.
1323 @param[in] Slot The slot number of the SD card to send the command to.
1324 @param[in] On The boolean to turn on/off LED.
1326 @retval EFI_SUCCESS The LED is turned on/off successfully.
1327 @retval Others The LED isn't turned on/off successfully.
1332 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1342 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1344 HostCtrl1
= (UINT8
)~BIT0
;
1345 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1352 Build ADMA descriptor table for transfer.
1354 Refer to SD Host Controller Simplified spec 4.2 Section 1.13 for details.
1356 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1357 @param[in] ControllerVer The version of host controller.
1359 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
1360 @retval Others The ADMA descriptor table isn't created successfully.
1364 BuildAdmaDescTable (
1365 IN SD_MMC_HC_TRB
*Trb
,
1366 IN UINT16 ControllerVer
1369 EFI_PHYSICAL_ADDRESS Data
;
1376 EFI_PCI_IO_PROTOCOL
*PciIo
;
1379 BOOLEAN AddressingMode64
;
1380 BOOLEAN DataLength26
;
1381 UINT32 AdmaMaxDataPerLine
;
1385 AddressingMode64
= FALSE
;
1386 DataLength26
= FALSE
;
1387 AdmaMaxDataPerLine
= ADMA_MAX_DATA_PER_LINE_16B
;
1388 DescSize
= sizeof (SD_MMC_HC_ADMA_32_DESC_LINE
);
1391 Data
= Trb
->DataPhy
;
1392 DataLen
= Trb
->DataLen
;
1393 PciIo
= Trb
->Private
->PciIo
;
1396 // Detect whether 64bit addressing is supported.
1398 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_400
) {
1399 Status
= SdMmcHcCheckMmioSet(PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof(UINT16
),
1400 SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
, SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
);
1401 if (!EFI_ERROR (Status
)) {
1402 AddressingMode64
= TRUE
;
1403 DescSize
= sizeof (SD_MMC_HC_ADMA_64_DESC_LINE
);
1407 // Check for valid ranges in 32bit ADMA Descriptor Table
1409 if (!AddressingMode64
&&
1410 ((Data
>= 0x100000000ul
) || ((Data
+ DataLen
) > 0x100000000ul
))) {
1411 return EFI_INVALID_PARAMETER
;
1414 // Check address field alignment
1416 if (AddressingMode64
) {
1418 // Address field shall be set on 64-bit boundary (Lower 3-bit is always set to 0)
1420 if ((Data
& (BIT0
| BIT1
| BIT2
)) != 0) {
1421 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 8 bytes boundary!\n", Data
));
1425 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
1427 if ((Data
& (BIT0
| BIT1
)) != 0) {
1428 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data
));
1432 // Detect whether 26bit data length is supported.
1434 Status
= SdMmcHcCheckMmioSet(PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof(UINT16
),
1435 SD_MMC_HC_26_DATA_LEN_ADMA_EN
, SD_MMC_HC_26_DATA_LEN_ADMA_EN
);
1436 if (!EFI_ERROR (Status
)) {
1437 DataLength26
= TRUE
;
1438 AdmaMaxDataPerLine
= ADMA_MAX_DATA_PER_LINE_26B
;
1441 Entries
= DivU64x32 ((DataLen
+ AdmaMaxDataPerLine
- 1), AdmaMaxDataPerLine
);
1442 TableSize
= (UINTN
)MultU64x32 (Entries
, DescSize
);
1443 Trb
->AdmaPages
= (UINT32
)EFI_SIZE_TO_PAGES (TableSize
);
1444 Status
= PciIo
->AllocateBuffer (
1447 EfiBootServicesData
,
1448 EFI_SIZE_TO_PAGES (TableSize
),
1452 if (EFI_ERROR (Status
)) {
1453 return EFI_OUT_OF_RESOURCES
;
1455 ZeroMem (AdmaDesc
, TableSize
);
1457 Status
= PciIo
->Map (
1459 EfiPciIoOperationBusMasterCommonBuffer
,
1466 if (EFI_ERROR (Status
) || (Bytes
!= TableSize
)) {
1468 // Map error or unable to map the whole RFis buffer into a contiguous region.
1472 EFI_SIZE_TO_PAGES (TableSize
),
1475 return EFI_OUT_OF_RESOURCES
;
1478 if ((!AddressingMode64
) &&
1479 (UINT64
)(UINTN
)Trb
->AdmaDescPhy
> 0x100000000ul
) {
1481 // The ADMA doesn't support 64bit addressing.
1489 EFI_SIZE_TO_PAGES (TableSize
),
1492 return EFI_DEVICE_ERROR
;
1495 Remaining
= DataLen
;
1497 if (!AddressingMode64
) {
1498 Trb
->Adma32Desc
= AdmaDesc
;
1499 Trb
->Adma64Desc
= NULL
;
1501 Trb
->Adma64Desc
= AdmaDesc
;
1502 Trb
->Adma32Desc
= NULL
;
1504 for (Index
= 0; Index
< Entries
; Index
++) {
1505 if (!AddressingMode64
) {
1506 if (Remaining
<= AdmaMaxDataPerLine
) {
1507 Trb
->Adma32Desc
[Index
].Valid
= 1;
1508 Trb
->Adma32Desc
[Index
].Act
= 2;
1510 Trb
->Adma32Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1512 Trb
->Adma32Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1513 Trb
->Adma32Desc
[Index
].Address
= (UINT32
)Address
;
1516 Trb
->Adma32Desc
[Index
].Valid
= 1;
1517 Trb
->Adma32Desc
[Index
].Act
= 2;
1519 Trb
->Adma32Desc
[Index
].UpperLength
= 0;
1521 Trb
->Adma32Desc
[Index
].LowerLength
= 0;
1522 Trb
->Adma32Desc
[Index
].Address
= (UINT32
)Address
;
1525 if (Remaining
<= AdmaMaxDataPerLine
) {
1526 Trb
->Adma64Desc
[Index
].Valid
= 1;
1527 Trb
->Adma64Desc
[Index
].Act
= 2;
1529 Trb
->Adma64Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1531 Trb
->Adma64Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1532 Trb
->Adma64Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1533 Trb
->Adma64Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1536 Trb
->Adma64Desc
[Index
].Valid
= 1;
1537 Trb
->Adma64Desc
[Index
].Act
= 2;
1539 Trb
->Adma64Desc
[Index
].UpperLength
= 0;
1541 Trb
->Adma64Desc
[Index
].LowerLength
= 0;
1542 Trb
->Adma64Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1543 Trb
->Adma64Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1547 Remaining
-= AdmaMaxDataPerLine
;
1548 Address
+= AdmaMaxDataPerLine
;
1552 // Set the last descriptor line as end of descriptor table
1554 AddressingMode64
? (Trb
->Adma64Desc
[Index
].End
= 1) : (Trb
->Adma32Desc
[Index
].End
= 1);
1559 Create a new TRB for the SD/MMC cmd request.
1561 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1562 @param[in] Slot The slot number of the SD card to send the command to.
1563 @param[in] Packet A pointer to the SD command data structure.
1564 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1565 not NULL, then nonblocking I/O is performed, and Event
1566 will be signaled when the Packet completes.
1568 @return Created Trb or NULL.
1573 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1575 IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1582 EFI_PCI_IO_PROTOCOL_OPERATION Flag
;
1583 EFI_PCI_IO_PROTOCOL
*PciIo
;
1586 Trb
= AllocateZeroPool (sizeof (SD_MMC_HC_TRB
));
1591 Trb
->Signature
= SD_MMC_HC_TRB_SIG
;
1593 Trb
->BlockSize
= 0x200;
1594 Trb
->Packet
= Packet
;
1596 Trb
->Started
= FALSE
;
1597 Trb
->Timeout
= Packet
->Timeout
;
1598 Trb
->Private
= Private
;
1600 if ((Packet
->InTransferLength
!= 0) && (Packet
->InDataBuffer
!= NULL
)) {
1601 Trb
->Data
= Packet
->InDataBuffer
;
1602 Trb
->DataLen
= Packet
->InTransferLength
;
1604 } else if ((Packet
->OutTransferLength
!= 0) && (Packet
->OutDataBuffer
!= NULL
)) {
1605 Trb
->Data
= Packet
->OutDataBuffer
;
1606 Trb
->DataLen
= Packet
->OutTransferLength
;
1608 } else if ((Packet
->InTransferLength
== 0) && (Packet
->OutTransferLength
== 0)) {
1615 if ((Trb
->DataLen
!= 0) && (Trb
->DataLen
< Trb
->BlockSize
)) {
1616 Trb
->BlockSize
= (UINT16
)Trb
->DataLen
;
1619 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1620 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1621 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1622 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
1623 Trb
->Mode
= SdMmcPioMode
;
1626 Flag
= EfiPciIoOperationBusMasterWrite
;
1628 Flag
= EfiPciIoOperationBusMasterRead
;
1631 PciIo
= Private
->PciIo
;
1632 if (Trb
->DataLen
!= 0) {
1633 MapLength
= Trb
->DataLen
;
1634 Status
= PciIo
->Map (
1642 if (EFI_ERROR (Status
) || (Trb
->DataLen
!= MapLength
)) {
1643 Status
= EFI_BAD_BUFFER_SIZE
;
1648 if (Trb
->DataLen
== 0) {
1649 Trb
->Mode
= SdMmcNoData
;
1650 } else if (Private
->Capability
[Slot
].Adma2
!= 0) {
1651 Trb
->Mode
= SdMmcAdmaMode
;
1652 Status
= BuildAdmaDescTable (Trb
, Private
->ControllerVersion
[Slot
]);
1653 if (EFI_ERROR (Status
)) {
1654 PciIo
->Unmap (PciIo
, Trb
->DataMap
);
1657 } else if (Private
->Capability
[Slot
].Sdma
!= 0) {
1658 Trb
->Mode
= SdMmcSdmaMode
;
1660 Trb
->Mode
= SdMmcPioMode
;
1664 if (Event
!= NULL
) {
1665 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1666 InsertTailList (&Private
->Queue
, &Trb
->TrbList
);
1667 gBS
->RestoreTPL (OldTpl
);
1678 Free the resource used by the TRB.
1680 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1685 IN SD_MMC_HC_TRB
*Trb
1688 EFI_PCI_IO_PROTOCOL
*PciIo
;
1690 PciIo
= Trb
->Private
->PciIo
;
1692 if (Trb
->AdmaMap
!= NULL
) {
1698 if (Trb
->Adma32Desc
!= NULL
) {
1705 if (Trb
->Adma64Desc
!= NULL
) {
1712 if (Trb
->DataMap
!= NULL
) {
1723 Check if the env is ready for execute specified TRB.
1725 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1726 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1728 @retval EFI_SUCCESS The env is ready for TRB execution.
1729 @retval EFI_NOT_READY The env is not ready for TRB execution.
1730 @retval Others Some erros happen.
1735 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1736 IN SD_MMC_HC_TRB
*Trb
1740 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1741 EFI_PCI_IO_PROTOCOL
*PciIo
;
1742 UINT32 PresentState
;
1744 Packet
= Trb
->Packet
;
1746 if ((Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) ||
1747 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR1b
) ||
1748 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR5b
)) {
1750 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1751 // the Present State register to be 0
1753 PresentState
= BIT0
| BIT1
;
1756 // Wait Command Inhibit (CMD) in the Present State register
1759 PresentState
= BIT0
;
1762 PciIo
= Private
->PciIo
;
1763 Status
= SdMmcHcCheckMmioSet (
1766 SD_MMC_HC_PRESENT_STATE
,
1767 sizeof (PresentState
),
1776 Wait for the env to be ready for execute specified TRB.
1778 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1779 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1781 @retval EFI_SUCCESS The env is ready for TRB execution.
1782 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1783 @retval Others Some erros happen.
1788 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1789 IN SD_MMC_HC_TRB
*Trb
1793 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1795 BOOLEAN InfiniteWait
;
1798 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1800 Packet
= Trb
->Packet
;
1801 Timeout
= Packet
->Timeout
;
1803 InfiniteWait
= TRUE
;
1805 InfiniteWait
= FALSE
;
1808 while (InfiniteWait
|| (Timeout
> 0)) {
1810 // Check Trb execution result by reading Normal Interrupt Status register.
1812 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
1813 if (Status
!= EFI_NOT_READY
) {
1817 // Stall for 1 microsecond.
1828 Execute the specified TRB.
1830 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1831 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1833 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1834 @retval Others Some erros happen when sending this request to the host controller.
1839 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1840 IN SD_MMC_HC_TRB
*Trb
1844 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
1845 EFI_PCI_IO_PROTOCOL
*PciIo
;
1855 BOOLEAN AddressingMode64
;
1857 AddressingMode64
= FALSE
;
1859 Packet
= Trb
->Packet
;
1860 PciIo
= Trb
->Private
->PciIo
;
1862 // Clear all bits in Error Interrupt Status Register
1865 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ERR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1866 if (EFI_ERROR (Status
)) {
1870 // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.
1873 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1874 if (EFI_ERROR (Status
)) {
1878 // Set Host Control 1 register DMA Select field
1880 if (Trb
->Mode
== SdMmcAdmaMode
) {
1882 Status
= SdMmcHcOrMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1883 if (EFI_ERROR (Status
)) {
1888 SdMmcHcLedOnOff (PciIo
, Trb
->Slot
, TRUE
);
1890 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
1891 Status
= SdMmcHcCheckMmioSet(PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof(UINT16
),
1892 SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
, SD_MMC_HC_V4_EN
|SD_MMC_HC_64_ADDR_EN
);
1893 if (!EFI_ERROR (Status
)) {
1894 AddressingMode64
= TRUE
;
1898 if (Trb
->Mode
== SdMmcSdmaMode
) {
1899 if ((!AddressingMode64
) &&
1900 ((UINT64
)(UINTN
)Trb
->DataPhy
>= 0x100000000ul
)) {
1901 return EFI_INVALID_PARAMETER
;
1904 SdmaAddr
= (UINT64
)(UINTN
)Trb
->DataPhy
;
1906 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
1907 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (UINT64
), &SdmaAddr
);
1909 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (UINT32
), &SdmaAddr
);
1912 if (EFI_ERROR (Status
)) {
1915 } else if (Trb
->Mode
== SdMmcAdmaMode
) {
1916 AdmaAddr
= (UINT64
)(UINTN
)Trb
->AdmaDescPhy
;
1917 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (AdmaAddr
), &AdmaAddr
);
1918 if (EFI_ERROR (Status
)) {
1923 BlkSize
= Trb
->BlockSize
;
1924 if (Trb
->Mode
== SdMmcSdmaMode
) {
1926 // Set SDMA boundary to be 512K bytes.
1931 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_SIZE
, FALSE
, sizeof (BlkSize
), &BlkSize
);
1932 if (EFI_ERROR (Status
)) {
1937 if (Trb
->Mode
!= SdMmcNoData
) {
1939 // Calcuate Block Count.
1941 BlkCount
= (Trb
->DataLen
/ Trb
->BlockSize
);
1943 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_410
) {
1944 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (UINT32
), &BlkCount
);
1946 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_COUNT
, FALSE
, sizeof (UINT16
), &BlkCount
);
1948 if (EFI_ERROR (Status
)) {
1952 Argument
= Packet
->SdMmcCmdBlk
->CommandArgument
;
1953 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ARG1
, FALSE
, sizeof (Argument
), &Argument
);
1954 if (EFI_ERROR (Status
)) {
1959 if (Trb
->Mode
!= SdMmcNoData
) {
1960 if (Trb
->Mode
!= SdMmcPioMode
) {
1967 TransMode
|= BIT5
| BIT1
;
1970 // Only SD memory card needs to use AUTO CMD12 feature.
1972 if (Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) {
1979 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_TRANS_MOD
, FALSE
, sizeof (TransMode
), &TransMode
);
1980 if (EFI_ERROR (Status
)) {
1984 Cmd
= (UINT16
)LShiftU64(Packet
->SdMmcCmdBlk
->CommandIndex
, 8);
1985 if (Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) {
1989 // Convert ResponseType to value
1991 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
1992 switch (Packet
->SdMmcCmdBlk
->ResponseType
) {
1993 case SdMmcResponseTypeR1
:
1994 case SdMmcResponseTypeR5
:
1995 case SdMmcResponseTypeR6
:
1996 case SdMmcResponseTypeR7
:
1997 Cmd
|= (BIT1
| BIT3
| BIT4
);
1999 case SdMmcResponseTypeR2
:
2000 Cmd
|= (BIT0
| BIT3
);
2002 case SdMmcResponseTypeR3
:
2003 case SdMmcResponseTypeR4
:
2006 case SdMmcResponseTypeR1b
:
2007 case SdMmcResponseTypeR5b
:
2008 Cmd
|= (BIT0
| BIT1
| BIT3
| BIT4
);
2018 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_COMMAND
, FALSE
, sizeof (Cmd
), &Cmd
);
2023 Check the TRB execution result.
2025 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2026 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2028 @retval EFI_SUCCESS The TRB is executed successfully.
2029 @retval EFI_NOT_READY The TRB is not completed for execution.
2030 @retval Others Some erros happen when executing this request.
2034 SdMmcCheckTrbResult (
2035 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2036 IN SD_MMC_HC_TRB
*Trb
2040 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2049 Packet
= Trb
->Packet
;
2051 // Check Trb execution result by reading Normal Interrupt Status register.
2053 Status
= SdMmcHcRwMmio (
2056 SD_MMC_HC_NOR_INT_STS
,
2061 if (EFI_ERROR (Status
)) {
2065 // Check Transfer Complete bit is set or not.
2067 if ((IntStatus
& BIT1
) == BIT1
) {
2068 if ((IntStatus
& BIT15
) == BIT15
) {
2070 // Read Error Interrupt Status register to check if the error is
2071 // Data Timeout Error.
2072 // If yes, treat it as success as Transfer Complete has higher
2073 // priority than Data Timeout Error.
2075 Status
= SdMmcHcRwMmio (
2078 SD_MMC_HC_ERR_INT_STS
,
2083 if (!EFI_ERROR (Status
)) {
2084 if ((IntStatus
& BIT4
) == BIT4
) {
2085 Status
= EFI_SUCCESS
;
2087 Status
= EFI_DEVICE_ERROR
;
2095 // Check if there is a error happened during cmd execution.
2096 // If yes, then do error recovery procedure to follow SD Host Controller
2097 // Simplified Spec 3.0 section 3.10.1.
2099 if ((IntStatus
& BIT15
) == BIT15
) {
2100 Status
= SdMmcHcRwMmio (
2103 SD_MMC_HC_ERR_INT_STS
,
2108 if (EFI_ERROR (Status
)) {
2111 if ((IntStatus
& 0x0F) != 0) {
2114 if ((IntStatus
& 0xF0) != 0) {
2118 Status
= SdMmcHcRwMmio (
2126 if (EFI_ERROR (Status
)) {
2129 Status
= SdMmcHcWaitMmioSet (
2136 SD_MMC_HC_GENERIC_TIMEOUT
2138 if (EFI_ERROR (Status
)) {
2142 Status
= EFI_DEVICE_ERROR
;
2146 // Check if DMA interrupt is signalled for the SDMA transfer.
2148 if ((Trb
->Mode
== SdMmcSdmaMode
) && ((IntStatus
& BIT3
) == BIT3
)) {
2150 // Clear DMA interrupt bit.
2153 Status
= SdMmcHcRwMmio (
2156 SD_MMC_HC_NOR_INT_STS
,
2161 if (EFI_ERROR (Status
)) {
2165 // Update SDMA Address register.
2167 SdmaAddr
= SD_MMC_SDMA_ROUND_UP ((UINTN
)Trb
->DataPhy
, SD_MMC_SDMA_BOUNDARY
);
2169 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
2170 Status
= SdMmcHcRwMmio (
2173 SD_MMC_HC_ADMA_SYS_ADDR
,
2179 Status
= SdMmcHcRwMmio (
2182 SD_MMC_HC_SDMA_ADDR
,
2189 if (EFI_ERROR (Status
)) {
2192 Trb
->DataPhy
= (UINT64
)(UINTN
)SdmaAddr
;
2195 if ((Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeAdtc
) &&
2196 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR1b
) &&
2197 (Packet
->SdMmcCmdBlk
->ResponseType
!= SdMmcResponseTypeR5b
)) {
2198 if ((IntStatus
& BIT0
) == BIT0
) {
2199 Status
= EFI_SUCCESS
;
2204 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
2205 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
2206 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
2207 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
))) {
2209 // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,
2210 // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.
2211 // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.
2213 if ((IntStatus
& BIT5
) == BIT5
) {
2215 // Clear Buffer Read Ready interrupt at first.
2218 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
2220 // Read data out from Buffer Port register
2222 for (PioLength
= 0; PioLength
< Trb
->DataLen
; PioLength
+= 4) {
2223 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_BUF_DAT_PORT
, TRUE
, 4, (UINT8
*)Trb
->Data
+ PioLength
);
2225 Status
= EFI_SUCCESS
;
2230 Status
= EFI_NOT_READY
;
2233 // Get response data when the cmd is executed successfully.
2235 if (!EFI_ERROR (Status
)) {
2236 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
2237 for (Index
= 0; Index
< 4; Index
++) {
2238 Status
= SdMmcHcRwMmio (
2241 SD_MMC_HC_RESPONSE
+ Index
* 4,
2246 if (EFI_ERROR (Status
)) {
2247 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2251 CopyMem (Packet
->SdMmcStatusBlk
, Response
, sizeof (Response
));
2255 if (Status
!= EFI_NOT_READY
) {
2256 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2263 Wait for the TRB execution result.
2265 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2266 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2268 @retval EFI_SUCCESS The TRB is executed successfully.
2269 @retval Others Some erros happen when executing this request.
2273 SdMmcWaitTrbResult (
2274 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2275 IN SD_MMC_HC_TRB
*Trb
2279 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2281 BOOLEAN InfiniteWait
;
2283 Packet
= Trb
->Packet
;
2285 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
2287 Timeout
= Packet
->Timeout
;
2289 InfiniteWait
= TRUE
;
2291 InfiniteWait
= FALSE
;
2294 while (InfiniteWait
|| (Timeout
> 0)) {
2296 // Check Trb execution result by reading Normal Interrupt Status register.
2298 Status
= SdMmcCheckTrbResult (Private
, Trb
);
2299 if (Status
!= EFI_NOT_READY
) {
2303 // Stall for 1 microsecond.