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-2019, NVIDIA CORPORATION. All rights reserved.
10 Copyright (c) 2015 - 2020, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include "SdMmcPciHcDxe.h"
18 Dump the content of SD/MMC host controller's Capability Register.
20 @param[in] Slot The slot number of the SD card to send the command to.
21 @param[in] Capability The buffer to store the capability data.
27 IN SD_MMC_HC_SLOT_CAP
*Capability
31 // Dump Capability Data
33 DEBUG ((DEBUG_INFO
, " == Slot [%d] Capability is 0x%x ==\n", Slot
, Capability
));
34 DEBUG ((DEBUG_INFO
, " Timeout Clk Freq %d%a\n", Capability
->TimeoutFreq
, (Capability
->TimeoutUnit
) ? "MHz" : "KHz"));
35 DEBUG ((DEBUG_INFO
, " Base Clk Freq %dMHz\n", Capability
->BaseClkFreq
));
36 DEBUG ((DEBUG_INFO
, " Max Blk Len %dbytes\n", 512 * (1 << Capability
->MaxBlkLen
)));
37 DEBUG ((DEBUG_INFO
, " 8-bit Support %a\n", Capability
->BusWidth8
? "TRUE" : "FALSE"));
38 DEBUG ((DEBUG_INFO
, " ADMA2 Support %a\n", Capability
->Adma2
? "TRUE" : "FALSE"));
39 DEBUG ((DEBUG_INFO
, " HighSpeed Support %a\n", Capability
->HighSpeed
? "TRUE" : "FALSE"));
40 DEBUG ((DEBUG_INFO
, " SDMA Support %a\n", Capability
->Sdma
? "TRUE" : "FALSE"));
41 DEBUG ((DEBUG_INFO
, " Suspend/Resume %a\n", Capability
->SuspRes
? "TRUE" : "FALSE"));
42 DEBUG ((DEBUG_INFO
, " Voltage 3.3 %a\n", Capability
->Voltage33
? "TRUE" : "FALSE"));
43 DEBUG ((DEBUG_INFO
, " Voltage 3.0 %a\n", Capability
->Voltage30
? "TRUE" : "FALSE"));
44 DEBUG ((DEBUG_INFO
, " Voltage 1.8 %a\n", Capability
->Voltage18
? "TRUE" : "FALSE"));
45 DEBUG ((DEBUG_INFO
, " V4 64-bit Sys Bus %a\n", Capability
->SysBus64V4
? "TRUE" : "FALSE"));
46 DEBUG ((DEBUG_INFO
, " V3 64-bit Sys Bus %a\n", Capability
->SysBus64V3
? "TRUE" : "FALSE"));
47 DEBUG ((DEBUG_INFO
, " Async Interrupt %a\n", Capability
->AsyncInt
? "TRUE" : "FALSE"));
48 DEBUG ((DEBUG_INFO
, " SlotType "));
49 if (Capability
->SlotType
== 0x00) {
50 DEBUG ((DEBUG_INFO
, "%a\n", "Removable Slot"));
51 } else if (Capability
->SlotType
== 0x01) {
52 DEBUG ((DEBUG_INFO
, "%a\n", "Embedded Slot"));
53 } else if (Capability
->SlotType
== 0x02) {
54 DEBUG ((DEBUG_INFO
, "%a\n", "Shared Bus Slot"));
56 DEBUG ((DEBUG_INFO
, "%a\n", "Reserved"));
59 DEBUG ((DEBUG_INFO
, " SDR50 Support %a\n", Capability
->Sdr50
? "TRUE" : "FALSE"));
60 DEBUG ((DEBUG_INFO
, " SDR104 Support %a\n", Capability
->Sdr104
? "TRUE" : "FALSE"));
61 DEBUG ((DEBUG_INFO
, " DDR50 Support %a\n", Capability
->Ddr50
? "TRUE" : "FALSE"));
62 DEBUG ((DEBUG_INFO
, " Driver Type A %a\n", Capability
->DriverTypeA
? "TRUE" : "FALSE"));
63 DEBUG ((DEBUG_INFO
, " Driver Type C %a\n", Capability
->DriverTypeC
? "TRUE" : "FALSE"));
64 DEBUG ((DEBUG_INFO
, " Driver Type D %a\n", Capability
->DriverTypeD
? "TRUE" : "FALSE"));
65 DEBUG ((DEBUG_INFO
, " Driver Type 4 %a\n", Capability
->DriverType4
? "TRUE" : "FALSE"));
66 if (Capability
->TimerCount
== 0) {
67 DEBUG ((DEBUG_INFO
, " Retuning TimerCnt Disabled\n"));
69 DEBUG ((DEBUG_INFO
, " Retuning TimerCnt %dseconds\n", 2 * (Capability
->TimerCount
- 1)));
72 DEBUG ((DEBUG_INFO
, " SDR50 Tuning %a\n", Capability
->TuningSDR50
? "TRUE" : "FALSE"));
73 DEBUG ((DEBUG_INFO
, " Retuning Mode Mode %d\n", Capability
->RetuningMod
+ 1));
74 DEBUG ((DEBUG_INFO
, " Clock Multiplier M = %d\n", Capability
->ClkMultiplier
+ 1));
75 DEBUG ((DEBUG_INFO
, " HS 400 %a\n", Capability
->Hs400
? "TRUE" : "FALSE"));
80 Read SlotInfo register from SD/MMC host controller pci config space.
82 @param[in] PciIo The PCI IO protocol instance.
83 @param[out] FirstBar The buffer to store the first BAR value.
84 @param[out] SlotNum The buffer to store the supported slot number.
86 @retval EFI_SUCCESS The operation succeeds.
87 @retval Others The operation fails.
93 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
99 SD_MMC_HC_SLOT_INFO SlotInfo
;
101 Status
= PciIo
->Pci
.Read (
104 SD_MMC_HC_SLOT_OFFSET
,
108 if (EFI_ERROR (Status
)) {
112 *FirstBar
= SlotInfo
.FirstBar
;
113 *SlotNum
= SlotInfo
.SlotNum
+ 1;
114 ASSERT ((*FirstBar
+ *SlotNum
) < SD_MMC_HC_MAX_SLOT
);
119 Read/Write specified SD/MMC host controller mmio register.
121 @param[in] PciIo The PCI IO protocol instance.
122 @param[in] BarIndex The BAR index of the standard PCI Configuration
123 header to use as the base address for the memory
124 operation to perform.
125 @param[in] Offset The offset within the selected BAR to start the
127 @param[in] Read A boolean to indicate it's read or write operation.
128 @param[in] Count The width of the mmio register in bytes.
129 Must be 1, 2 , 4 or 8 bytes.
130 @param[in, out] Data For read operations, the destination buffer to store
131 the results. For write operations, the source buffer
132 to write data from. The caller is responsible for
133 having ownership of the data buffer and ensuring its
134 size not less than Count bytes.
136 @retval EFI_INVALID_PARAMETER The PciIo or Data is NULL or the Count is not valid.
137 @retval EFI_SUCCESS The read/write operation succeeds.
138 @retval Others The read/write operation fails.
144 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
153 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
155 if ((PciIo
== NULL
) || (Data
== NULL
)) {
156 return EFI_INVALID_PARAMETER
;
161 Width
= EfiPciIoWidthUint8
;
164 Width
= EfiPciIoWidthUint16
;
168 Width
= EfiPciIoWidthUint32
;
172 Width
= EfiPciIoWidthUint32
;
176 return EFI_INVALID_PARAMETER
;
180 Status
= PciIo
->Mem
.Read (
189 Status
= PciIo
->Mem
.Write (
203 Do OR operation with the value of the specified SD/MMC host controller mmio register.
205 @param[in] PciIo The PCI IO protocol instance.
206 @param[in] BarIndex The BAR index of the standard PCI Configuration
207 header to use as the base address for the memory
208 operation to perform.
209 @param[in] Offset The offset within the selected BAR to start the
211 @param[in] Count The width of the mmio register in bytes.
212 Must be 1, 2 , 4 or 8 bytes.
213 @param[in] OrData The pointer to the data used to do OR operation.
214 The caller is responsible for having ownership of
215 the data buffer and ensuring its size not less than
218 @retval EFI_INVALID_PARAMETER The PciIo or OrData is NULL or the Count is not valid.
219 @retval EFI_SUCCESS The OR operation succeeds.
220 @retval Others The OR operation fails.
226 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
237 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
238 if (EFI_ERROR (Status
)) {
243 Or
= *(UINT8
*)OrData
;
244 } else if (Count
== 2) {
245 Or
= *(UINT16
*)OrData
;
246 } else if (Count
== 4) {
247 Or
= *(UINT32
*)OrData
;
248 } else if (Count
== 8) {
249 Or
= *(UINT64
*)OrData
;
251 return EFI_INVALID_PARAMETER
;
255 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
261 Do AND operation with the value of the specified SD/MMC host controller mmio register.
263 @param[in] PciIo The PCI IO protocol instance.
264 @param[in] BarIndex The BAR index of the standard PCI Configuration
265 header to use as the base address for the memory
266 operation to perform.
267 @param[in] Offset The offset within the selected BAR to start the
269 @param[in] Count The width of the mmio register in bytes.
270 Must be 1, 2 , 4 or 8 bytes.
271 @param[in] AndData The pointer to the data used to do AND operation.
272 The caller is responsible for having ownership of
273 the data buffer and ensuring its size not less than
276 @retval EFI_INVALID_PARAMETER The PciIo or AndData is NULL or the Count is not valid.
277 @retval EFI_SUCCESS The AND operation succeeds.
278 @retval Others The AND operation fails.
284 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
295 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Data
);
296 if (EFI_ERROR (Status
)) {
301 And
= *(UINT8
*)AndData
;
302 } else if (Count
== 2) {
303 And
= *(UINT16
*)AndData
;
304 } else if (Count
== 4) {
305 And
= *(UINT32
*)AndData
;
306 } else if (Count
== 8) {
307 And
= *(UINT64
*)AndData
;
309 return EFI_INVALID_PARAMETER
;
313 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, FALSE
, Count
, &Data
);
319 Wait for the value of the specified MMIO register set to the test value.
321 @param[in] PciIo The PCI IO protocol instance.
322 @param[in] BarIndex The BAR index of the standard PCI Configuration
323 header to use as the base address for the memory
324 operation to perform.
325 @param[in] Offset The offset within the selected BAR to start the
327 @param[in] Count The width of the mmio register in bytes.
328 Must be 1, 2, 4 or 8 bytes.
329 @param[in] MaskValue The mask value of memory.
330 @param[in] TestValue The test value of memory.
332 @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.
333 @retval EFI_SUCCESS The MMIO register has expected value.
334 @retval Others The MMIO operation fails.
339 SdMmcHcCheckMmioSet (
340 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
352 // Access PCI MMIO space to see if the value is the tested one.
355 Status
= SdMmcHcRwMmio (PciIo
, BarIndex
, Offset
, TRUE
, Count
, &Value
);
356 if (EFI_ERROR (Status
)) {
362 if (Value
== TestValue
) {
366 return EFI_NOT_READY
;
370 Wait for the value of the specified MMIO register set to the test value.
372 @param[in] PciIo The PCI IO protocol instance.
373 @param[in] BarIndex The BAR index of the standard PCI Configuration
374 header to use as the base address for the memory
375 operation to perform.
376 @param[in] Offset The offset within the selected BAR to start the
378 @param[in] Count The width of the mmio register in bytes.
379 Must be 1, 2, 4 or 8 bytes.
380 @param[in] MaskValue The mask value of memory.
381 @param[in] TestValue The test value of memory.
382 @param[in] Timeout The time out value for wait memory set, uses 1
383 microsecond as a unit.
385 @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout
387 @retval EFI_SUCCESS The MMIO register has expected value.
388 @retval Others The MMIO operation fails.
394 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
404 BOOLEAN InfiniteWait
;
409 InfiniteWait
= FALSE
;
412 while (InfiniteWait
|| (Timeout
> 0)) {
413 Status
= SdMmcHcCheckMmioSet (
421 if (Status
!= EFI_NOT_READY
) {
426 // Stall for 1 microsecond.
437 Get the controller version information from the specified slot.
439 @param[in] PciIo The PCI IO protocol instance.
440 @param[in] Slot The slot number of the SD card to send the command to.
441 @param[out] Version The buffer to store the version information.
443 @retval EFI_SUCCESS The operation executes successfully.
444 @retval Others The operation fails.
448 SdMmcHcGetControllerVersion (
449 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
456 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CTRL_VER
, TRUE
, sizeof (UINT16
), Version
);
457 if (EFI_ERROR (Status
)) {
467 Software reset the specified SD/MMC host controller and enable all interrupts.
469 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
470 @param[in] Slot The slot number of the SD card to send the command to.
472 @retval EFI_SUCCESS The software reset executes successfully.
473 @retval Others The software reset fails.
478 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
484 EFI_PCI_IO_PROTOCOL
*PciIo
;
487 // Notify the SD/MMC override protocol that we are about to reset
488 // the SD/MMC host controller.
490 if ((mOverride
!= NULL
) && (mOverride
->NotifyPhase
!= NULL
)) {
491 Status
= mOverride
->NotifyPhase (
492 Private
->ControllerHandle
,
497 if (EFI_ERROR (Status
)) {
500 "%a: SD/MMC pre reset notifier callback failed - %r\n",
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
)) {
538 "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",
545 // Notify the SD/MMC override protocol that we have just reset
546 // the SD/MMC host controller.
548 if ((mOverride
!= NULL
) && (mOverride
->NotifyPhase
!= NULL
)) {
549 Status
= mOverride
->NotifyPhase (
550 Private
->ControllerHandle
,
555 if (EFI_ERROR (Status
)) {
558 "%a: SD/MMC post reset notifier callback failed - %r\n",
569 Set all interrupt status bits in Normal and Error Interrupt Status Enable
572 @param[in] PciIo The PCI IO protocol instance.
573 @param[in] Slot The slot number of the SD card to send the command to.
575 @retval EFI_SUCCESS The operation executes successfully.
576 @retval Others The operation fails.
580 SdMmcHcEnableInterrupt (
581 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
589 // Enable all bits in Error Interrupt Status Enable Register
592 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_ERR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
593 if (EFI_ERROR (Status
)) {
598 // Enable all bits in Normal Interrupt Status Enable Register
601 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
607 Get the capability data from the specified slot.
609 @param[in] PciIo The PCI IO protocol instance.
610 @param[in] Slot The slot number of the SD card to send the command to.
611 @param[out] Capability The buffer to store the capability data.
613 @retval EFI_SUCCESS The operation executes successfully.
614 @retval Others The operation fails.
618 SdMmcHcGetCapability (
619 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
621 OUT SD_MMC_HC_SLOT_CAP
*Capability
627 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CAP
, TRUE
, sizeof (Cap
), &Cap
);
628 if (EFI_ERROR (Status
)) {
632 CopyMem (Capability
, &Cap
, sizeof (Cap
));
638 Get the maximum current capability data from the specified slot.
640 @param[in] PciIo The PCI IO protocol instance.
641 @param[in] Slot The slot number of the SD card to send the command to.
642 @param[out] MaxCurrent The buffer to store the maximum current capability data.
644 @retval EFI_SUCCESS The operation executes successfully.
645 @retval Others The operation fails.
649 SdMmcHcGetMaxCurrent (
650 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
652 OUT UINT64
*MaxCurrent
657 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_MAX_CURRENT_CAP
, TRUE
, sizeof (UINT64
), MaxCurrent
);
663 Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller
666 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
668 @param[in] PciIo The PCI IO protocol instance.
669 @param[in] Slot The slot number of the SD card to send the command to.
670 @param[out] MediaPresent The pointer to the media present boolean value.
672 @retval EFI_SUCCESS There is no media change happened.
673 @retval EFI_MEDIA_CHANGED There is media change happened.
674 @retval Others The detection fails.
679 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
681 OUT BOOLEAN
*MediaPresent
689 // Check Present State Register to see if there is a card presented.
691 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_PRESENT_STATE
, TRUE
, sizeof (PresentState
), &PresentState
);
692 if (EFI_ERROR (Status
)) {
696 if ((PresentState
& BIT16
) != 0) {
697 *MediaPresent
= TRUE
;
699 *MediaPresent
= FALSE
;
703 // Check Normal Interrupt Status Register
705 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, TRUE
, sizeof (Data
), &Data
);
706 if (EFI_ERROR (Status
)) {
710 if ((Data
& (BIT6
| BIT7
)) != 0) {
712 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
715 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data
), &Data
);
716 if (EFI_ERROR (Status
)) {
720 return EFI_MEDIA_CHANGED
;
727 Stop SD/MMC card clock.
729 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
731 @param[in] PciIo The PCI IO protocol instance.
732 @param[in] Slot The slot number of the SD card to send the command to.
734 @retval EFI_SUCCESS Succeed to stop SD/MMC clock.
735 @retval Others Fail to stop SD/MMC clock.
740 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
749 // Ensure no SD transactions are occurring on the SD Bus by
750 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
751 // in the Present State register to be 0.
753 Status
= SdMmcHcWaitMmioSet (
756 SD_MMC_HC_PRESENT_STATE
,
757 sizeof (PresentState
),
760 SD_MMC_HC_GENERIC_TIMEOUT
762 if (EFI_ERROR (Status
)) {
767 // Set SD Clock Enable in the Clock Control register to 0
769 ClockCtrl
= (UINT16
) ~BIT2
;
770 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
778 @param[in] PciIo The PCI IO protocol instance.
779 @param[in] Slot The slot number.
781 @retval EFI_SUCCESS Succeeded to start the SD clock.
782 @retval Others Failed to start the SD clock.
785 SdMmcHcStartSdClock (
786 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
793 // Set SD Clock Enable in the Clock Control register to 1
796 return SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
800 SD/MMC card clock supply.
802 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
804 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
805 @param[in] Slot The slot number of the SD card to send the command to.
806 @param[in] BusTiming BusTiming at which the frequency change is done.
807 @param[in] FirstTimeSetup Flag to indicate whether the clock is being setup for the first time.
808 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
810 @retval EFI_SUCCESS The clock is supplied successfully.
811 @retval Others The clock isn't supplied successfully.
816 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
818 IN SD_MMC_BUS_MODE BusTiming
,
819 IN BOOLEAN FirstTimeSetup
,
829 UINT16 ControllerVer
;
830 EFI_PCI_IO_PROTOCOL
*PciIo
;
832 PciIo
= Private
->PciIo
;
833 BaseClkFreq
= Private
->BaseClkFreq
[Slot
];
834 ControllerVer
= Private
->ControllerVersion
[Slot
];
836 if ((BaseClkFreq
== 0) || (ClockFreq
== 0)) {
837 return EFI_INVALID_PARAMETER
;
840 if (ClockFreq
> (BaseClkFreq
* 1000)) {
841 ClockFreq
= BaseClkFreq
* 1000;
845 // Calculate the divisor of base frequency.
848 SettingFreq
= BaseClkFreq
* 1000;
849 while (ClockFreq
< SettingFreq
) {
852 SettingFreq
= (BaseClkFreq
* 1000) / (2 * Divisor
);
853 Remainder
= (BaseClkFreq
* 1000) % (2 * Divisor
);
854 if ((ClockFreq
== SettingFreq
) && (Remainder
== 0)) {
858 if ((ClockFreq
== SettingFreq
) && (Remainder
!= 0)) {
863 DEBUG ((DEBUG_INFO
, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq
, Divisor
, ClockFreq
));
866 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
868 if ((ControllerVer
>= SD_MMC_HC_CTRL_VER_300
) &&
869 (ControllerVer
<= SD_MMC_HC_CTRL_VER_420
))
871 ASSERT (Divisor
<= 0x3FF);
872 ClockCtrl
= ((Divisor
& 0xFF) << 8) | ((Divisor
& 0x300) >> 2);
873 } else if ((ControllerVer
== SD_MMC_HC_CTRL_VER_100
) ||
874 (ControllerVer
== SD_MMC_HC_CTRL_VER_200
))
877 // Only the most significant bit can be used as divisor.
879 if (((Divisor
- 1) & Divisor
) != 0) {
880 Divisor
= 1 << (HighBitSet32 (Divisor
) + 1);
883 ASSERT (Divisor
<= 0x80);
884 ClockCtrl
= (Divisor
& 0xFF) << 8;
886 DEBUG ((DEBUG_ERROR
, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer
));
887 return EFI_UNSUPPORTED
;
891 // Stop bus clock at first
893 Status
= SdMmcHcStopClock (PciIo
, Slot
);
894 if (EFI_ERROR (Status
)) {
899 // Supply clock frequency with specified divisor
902 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_CLOCK_CTRL
, FALSE
, sizeof (ClockCtrl
), &ClockCtrl
);
903 if (EFI_ERROR (Status
)) {
904 DEBUG ((DEBUG_ERROR
, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
909 // Wait Internal Clock Stable in the Clock Control register to be 1
911 Status
= SdMmcHcWaitMmioSet (
914 SD_MMC_HC_CLOCK_CTRL
,
918 SD_MMC_HC_GENERIC_TIMEOUT
920 if (EFI_ERROR (Status
)) {
924 Status
= SdMmcHcStartSdClock (PciIo
, Slot
);
925 if (EFI_ERROR (Status
)) {
930 // We don't notify the platform on first time setup to avoid changing
931 // legacy behavior. During first time setup we also don't know what type
932 // of the card slot it is and which enum value of BusTiming applies.
934 if (!FirstTimeSetup
&& (mOverride
!= NULL
) && (mOverride
->NotifyPhase
!= NULL
)) {
935 Status
= mOverride
->NotifyPhase (
936 Private
->ControllerHandle
,
938 EdkiiSdMmcSwitchClockFreqPost
,
941 if (EFI_ERROR (Status
)) {
944 "%a: SD/MMC switch clock freq post notifier callback failed - %r\n",
952 Private
->Slot
[Slot
].CurrentFreq
= ClockFreq
;
958 SD/MMC bus power control.
960 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
962 @param[in] PciIo The PCI IO protocol instance.
963 @param[in] Slot The slot number of the SD card to send the command to.
964 @param[in] PowerCtrl The value setting to the power control register.
966 @retval TRUE There is a SD/MMC card attached.
967 @retval FALSE There is no a SD/MMC card attached.
971 SdMmcHcPowerControl (
972 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
982 PowerCtrl
&= (UINT8
) ~BIT0
;
983 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
984 if (EFI_ERROR (Status
)) {
989 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
992 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
998 Set the SD/MMC bus width.
1000 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
1002 @param[in] PciIo The PCI IO protocol instance.
1003 @param[in] Slot The slot number of the SD card to send the command to.
1004 @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.
1006 @retval EFI_SUCCESS The bus width is set successfully.
1007 @retval Others The bus width isn't set successfully.
1011 SdMmcHcSetBusWidth (
1012 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1020 if (BusWidth
== 1) {
1021 HostCtrl1
= (UINT8
) ~(BIT5
| BIT1
);
1022 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1023 } else if (BusWidth
== 4) {
1024 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
1025 if (EFI_ERROR (Status
)) {
1030 HostCtrl1
&= (UINT8
) ~BIT5
;
1031 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
1032 } else if (BusWidth
== 8) {
1033 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
1034 if (EFI_ERROR (Status
)) {
1038 HostCtrl1
&= (UINT8
) ~BIT1
;
1040 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
1043 return EFI_INVALID_PARAMETER
;
1050 Configure V4 controller enhancements at initialization.
1052 @param[in] PciIo The PCI IO protocol instance.
1053 @param[in] Slot The slot number of the SD card to send the command to.
1054 @param[in] Capability The capability of the slot.
1055 @param[in] ControllerVer The version of host controller.
1057 @retval EFI_SUCCESS The clock is supplied successfully.
1061 SdMmcHcInitV4Enhancements (
1062 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1064 IN SD_MMC_HC_SLOT_CAP Capability
,
1065 IN UINT16 ControllerVer
1072 // Check if controller version V4 or higher
1074 if (ControllerVer
>= SD_MMC_HC_CTRL_VER_400
) {
1075 HostCtrl2
= SD_MMC_HC_V4_EN
;
1077 // Check if controller version V4.0
1079 if (ControllerVer
== SD_MMC_HC_CTRL_VER_400
) {
1081 // Check if 64bit support is available
1083 if (Capability
.SysBus64V3
!= 0) {
1084 HostCtrl2
|= SD_MMC_HC_64_ADDR_EN
;
1085 DEBUG ((DEBUG_INFO
, "Enabled V4 64 bit system bus support\n"));
1089 // Check if controller version V4.10 or higher
1091 else if (ControllerVer
>= SD_MMC_HC_CTRL_VER_410
) {
1093 // Check if 64bit support is available
1095 if (Capability
.SysBus64V4
!= 0) {
1096 HostCtrl2
|= SD_MMC_HC_64_ADDR_EN
;
1097 DEBUG ((DEBUG_INFO
, "Enabled V4 64 bit system bus support\n"));
1100 HostCtrl2
|= SD_MMC_HC_26_DATA_LEN_ADMA_EN
;
1101 DEBUG ((DEBUG_INFO
, "Enabled V4 26 bit data length ADMA support\n"));
1104 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1105 if (EFI_ERROR (Status
)) {
1114 Supply SD/MMC card with maximum voltage at initialization.
1116 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
1118 @param[in] PciIo The PCI IO protocol instance.
1119 @param[in] Slot The slot number of the SD card to send the command to.
1120 @param[in] Capability The capability of the slot.
1122 @retval EFI_SUCCESS The voltage is supplied successfully.
1123 @retval Others The voltage isn't supplied successfully.
1127 SdMmcHcInitPowerVoltage (
1128 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1130 IN SD_MMC_HC_SLOT_CAP Capability
1138 // Calculate supported maximum voltage according to SD Bus Voltage Select
1140 if (Capability
.Voltage33
!= 0) {
1145 } else if (Capability
.Voltage30
!= 0) {
1150 } else if (Capability
.Voltage18
!= 0) {
1156 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1158 if (EFI_ERROR (Status
)) {
1163 return EFI_DEVICE_ERROR
;
1167 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
1169 Status
= SdMmcHcPowerControl (PciIo
, Slot
, MaxVoltage
);
1175 Initialize the Timeout Control register with most conservative value at initialization.
1177 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
1179 @param[in] PciIo The PCI IO protocol instance.
1180 @param[in] Slot The slot number of the SD card to send the command to.
1182 @retval EFI_SUCCESS The timeout control register is configured successfully.
1183 @retval Others The timeout control register isn't configured successfully.
1187 SdMmcHcInitTimeoutCtrl (
1188 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1196 Status
= SdMmcHcRwMmio (PciIo
, Slot
, SD_MMC_HC_TIMEOUT_CTRL
, FALSE
, sizeof (Timeout
), &Timeout
);
1202 Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value
1205 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1206 @param[in] Slot The slot number of the SD card to send the command to.
1208 @retval EFI_SUCCESS The host controller is initialized successfully.
1209 @retval Others The host controller isn't initialized successfully.
1214 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1219 EFI_PCI_IO_PROTOCOL
*PciIo
;
1220 SD_MMC_HC_SLOT_CAP Capability
;
1223 // Notify the SD/MMC override protocol that we are about to initialize
1224 // the SD/MMC host controller.
1226 if ((mOverride
!= NULL
) && (mOverride
->NotifyPhase
!= NULL
)) {
1227 Status
= mOverride
->NotifyPhase (
1228 Private
->ControllerHandle
,
1230 EdkiiSdMmcInitHostPre
,
1233 if (EFI_ERROR (Status
)) {
1236 "%a: SD/MMC pre init notifier callback failed - %r\n",
1244 PciIo
= Private
->PciIo
;
1245 Capability
= Private
->Capability
[Slot
];
1247 Status
= SdMmcHcInitV4Enhancements (PciIo
, Slot
, Capability
, Private
->ControllerVersion
[Slot
]);
1248 if (EFI_ERROR (Status
)) {
1253 // Perform first time clock setup with 400 KHz frequency.
1254 // We send the 0 as the BusTiming value because at this time
1255 // we still do not know the slot type and which enum value will apply.
1256 // Since it is a first time setup SdMmcHcClockSupply won't notify
1257 // the platofrm driver anyway so it doesn't matter.
1259 Status
= SdMmcHcClockSupply (Private
, Slot
, 0, TRUE
, 400);
1260 if (EFI_ERROR (Status
)) {
1264 Status
= SdMmcHcInitPowerVoltage (PciIo
, Slot
, Capability
);
1265 if (EFI_ERROR (Status
)) {
1269 Status
= SdMmcHcInitTimeoutCtrl (PciIo
, Slot
);
1270 if (EFI_ERROR (Status
)) {
1275 // Notify the SD/MMC override protocol that we are have just initialized
1276 // the SD/MMC host controller.
1278 if ((mOverride
!= NULL
) && (mOverride
->NotifyPhase
!= NULL
)) {
1279 Status
= mOverride
->NotifyPhase (
1280 Private
->ControllerHandle
,
1282 EdkiiSdMmcInitHostPost
,
1285 if (EFI_ERROR (Status
)) {
1288 "%a: SD/MMC post init notifier callback failed - %r\n",
1299 Set SD Host Controler control 2 registry according to selected speed.
1301 @param[in] ControllerHandle The handle of the controller.
1302 @param[in] PciIo The PCI IO protocol instance.
1303 @param[in] Slot The slot number of the SD card to send the command to.
1304 @param[in] Timing The timing to select.
1306 @retval EFI_SUCCESS The timing is set successfully.
1307 @retval Others The timing isn't set successfully.
1310 SdMmcHcUhsSignaling (
1311 IN EFI_HANDLE ControllerHandle
,
1312 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1314 IN SD_MMC_BUS_MODE Timing
1320 HostCtrl2
= (UINT8
) ~SD_MMC_HC_CTRL_UHS_MASK
;
1321 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1322 if (EFI_ERROR (Status
)) {
1328 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR12
;
1331 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR25
;
1334 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR50
;
1336 case SdMmcUhsSdr104
:
1337 HostCtrl2
= SD_MMC_HC_CTRL_UHS_SDR104
;
1340 HostCtrl2
= SD_MMC_HC_CTRL_UHS_DDR50
;
1342 case SdMmcMmcLegacy
:
1343 HostCtrl2
= SD_MMC_HC_CTRL_MMC_LEGACY
;
1346 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_SDR
;
1349 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS_DDR
;
1352 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS200
;
1355 HostCtrl2
= SD_MMC_HC_CTRL_MMC_HS400
;
1362 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1363 if (EFI_ERROR (Status
)) {
1367 if ((mOverride
!= NULL
) && (mOverride
->NotifyPhase
!= NULL
)) {
1368 Status
= mOverride
->NotifyPhase (
1371 EdkiiSdMmcUhsSignaling
,
1374 if (EFI_ERROR (Status
)) {
1377 "%a: SD/MMC uhs signaling notifier callback failed - %r\n",
1389 Set driver strength in host controller.
1391 @param[in] PciIo The PCI IO protocol instance.
1392 @param[in] SlotIndex The slot index of the card.
1393 @param[in] DriverStrength DriverStrength to set in the controller.
1395 @retval EFI_SUCCESS Driver strength programmed successfully.
1396 @retval Others Failed to set driver strength.
1399 SdMmcSetDriverStrength (
1400 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1402 IN SD_DRIVER_STRENGTH_TYPE DriverStrength
1408 if (DriverStrength
== SdDriverStrengthIgnore
) {
1412 HostCtrl2
= (UINT16
) ~SD_MMC_HC_CTRL_DRIVER_STRENGTH_MASK
;
1413 Status
= SdMmcHcAndMmio (PciIo
, SlotIndex
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1414 if (EFI_ERROR (Status
)) {
1418 HostCtrl2
= (DriverStrength
<< 4) & SD_MMC_HC_CTRL_DRIVER_STRENGTH_MASK
;
1419 return SdMmcHcOrMmio (PciIo
, SlotIndex
, SD_MMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
1425 @param[in] PciIo The PCI IO protocol instance.
1426 @param[in] Slot The slot number of the SD card to send the command to.
1427 @param[in] On The boolean to turn on/off LED.
1429 @retval EFI_SUCCESS The LED is turned on/off successfully.
1430 @retval Others The LED isn't turned on/off successfully.
1435 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1445 Status
= SdMmcHcOrMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1447 HostCtrl1
= (UINT8
) ~BIT0
;
1448 Status
= SdMmcHcAndMmio (PciIo
, Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1455 Build ADMA descriptor table for transfer.
1457 Refer to SD Host Controller Simplified spec 4.2 Section 1.13 for details.
1459 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1460 @param[in] ControllerVer The version of host controller.
1462 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
1463 @retval Others The ADMA descriptor table isn't created successfully.
1467 BuildAdmaDescTable (
1468 IN SD_MMC_HC_TRB
*Trb
,
1469 IN UINT16 ControllerVer
1472 EFI_PHYSICAL_ADDRESS Data
;
1479 EFI_PCI_IO_PROTOCOL
*PciIo
;
1482 UINT32 AdmaMaxDataPerLine
;
1486 AdmaMaxDataPerLine
= ADMA_MAX_DATA_PER_LINE_16B
;
1487 DescSize
= sizeof (SD_MMC_HC_ADMA_32_DESC_LINE
);
1490 Data
= Trb
->DataPhy
;
1491 DataLen
= Trb
->DataLen
;
1492 PciIo
= Trb
->Private
->PciIo
;
1495 // Check for valid ranges in 32bit ADMA Descriptor Table
1497 if ((Trb
->Mode
== SdMmcAdma32bMode
) &&
1498 ((Data
>= 0x100000000ul
) || ((Data
+ DataLen
) > 0x100000000ul
)))
1500 return EFI_INVALID_PARAMETER
;
1504 // Check address field alignment
1506 if (Trb
->Mode
!= SdMmcAdma32bMode
) {
1508 // Address field shall be set on 64-bit boundary (Lower 3-bit is always set to 0)
1510 if ((Data
& (BIT0
| BIT1
| BIT2
)) != 0) {
1511 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 8 bytes boundary!\n", Data
));
1515 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
1517 if ((Data
& (BIT0
| BIT1
)) != 0) {
1518 DEBUG ((DEBUG_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data
));
1523 // Configure 64b ADMA.
1525 if (Trb
->Mode
== SdMmcAdma64bV3Mode
) {
1526 DescSize
= sizeof (SD_MMC_HC_ADMA_64_V3_DESC_LINE
);
1527 } else if (Trb
->Mode
== SdMmcAdma64bV4Mode
) {
1528 DescSize
= sizeof (SD_MMC_HC_ADMA_64_V4_DESC_LINE
);
1532 // Configure 26b data length.
1534 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1535 AdmaMaxDataPerLine
= ADMA_MAX_DATA_PER_LINE_26B
;
1538 Entries
= DivU64x32 ((DataLen
+ AdmaMaxDataPerLine
- 1), AdmaMaxDataPerLine
);
1539 TableSize
= (UINTN
)MultU64x32 (Entries
, DescSize
);
1540 Trb
->AdmaPages
= (UINT32
)EFI_SIZE_TO_PAGES (TableSize
);
1541 Status
= PciIo
->AllocateBuffer (
1544 EfiBootServicesData
,
1545 EFI_SIZE_TO_PAGES (TableSize
),
1549 if (EFI_ERROR (Status
)) {
1550 return EFI_OUT_OF_RESOURCES
;
1553 ZeroMem (AdmaDesc
, TableSize
);
1555 Status
= PciIo
->Map (
1557 EfiPciIoOperationBusMasterCommonBuffer
,
1564 if (EFI_ERROR (Status
) || (Bytes
!= TableSize
)) {
1566 // Map error or unable to map the whole RFis buffer into a contiguous region.
1570 EFI_SIZE_TO_PAGES (TableSize
),
1573 return EFI_OUT_OF_RESOURCES
;
1576 if ((Trb
->Mode
== SdMmcAdma32bMode
) &&
1577 ((UINT64
)(UINTN
)Trb
->AdmaDescPhy
> 0x100000000ul
))
1580 // The ADMA doesn't support 64bit addressing.
1586 Trb
->AdmaMap
= NULL
;
1590 EFI_SIZE_TO_PAGES (TableSize
),
1593 return EFI_DEVICE_ERROR
;
1596 Remaining
= DataLen
;
1598 if (Trb
->Mode
== SdMmcAdma32bMode
) {
1599 Trb
->Adma32Desc
= AdmaDesc
;
1600 } else if (Trb
->Mode
== SdMmcAdma64bV3Mode
) {
1601 Trb
->Adma64V3Desc
= AdmaDesc
;
1603 Trb
->Adma64V4Desc
= AdmaDesc
;
1606 for (Index
= 0; Index
< Entries
; Index
++) {
1607 if (Trb
->Mode
== SdMmcAdma32bMode
) {
1608 if (Remaining
<= AdmaMaxDataPerLine
) {
1609 Trb
->Adma32Desc
[Index
].Valid
= 1;
1610 Trb
->Adma32Desc
[Index
].Act
= 2;
1611 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1612 Trb
->Adma32Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1615 Trb
->Adma32Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1616 Trb
->Adma32Desc
[Index
].Address
= (UINT32
)Address
;
1619 Trb
->Adma32Desc
[Index
].Valid
= 1;
1620 Trb
->Adma32Desc
[Index
].Act
= 2;
1621 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1622 Trb
->Adma32Desc
[Index
].UpperLength
= 0;
1625 Trb
->Adma32Desc
[Index
].LowerLength
= 0;
1626 Trb
->Adma32Desc
[Index
].Address
= (UINT32
)Address
;
1628 } else if (Trb
->Mode
== SdMmcAdma64bV3Mode
) {
1629 if (Remaining
<= AdmaMaxDataPerLine
) {
1630 Trb
->Adma64V3Desc
[Index
].Valid
= 1;
1631 Trb
->Adma64V3Desc
[Index
].Act
= 2;
1632 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1633 Trb
->Adma64V3Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1636 Trb
->Adma64V3Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1637 Trb
->Adma64V3Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1638 Trb
->Adma64V3Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1641 Trb
->Adma64V3Desc
[Index
].Valid
= 1;
1642 Trb
->Adma64V3Desc
[Index
].Act
= 2;
1643 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1644 Trb
->Adma64V3Desc
[Index
].UpperLength
= 0;
1647 Trb
->Adma64V3Desc
[Index
].LowerLength
= 0;
1648 Trb
->Adma64V3Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1649 Trb
->Adma64V3Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1652 if (Remaining
<= AdmaMaxDataPerLine
) {
1653 Trb
->Adma64V4Desc
[Index
].Valid
= 1;
1654 Trb
->Adma64V4Desc
[Index
].Act
= 2;
1655 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1656 Trb
->Adma64V4Desc
[Index
].UpperLength
= (UINT16
)RShiftU64 (Remaining
, 16);
1659 Trb
->Adma64V4Desc
[Index
].LowerLength
= (UINT16
)(Remaining
& MAX_UINT16
);
1660 Trb
->Adma64V4Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1661 Trb
->Adma64V4Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1664 Trb
->Adma64V4Desc
[Index
].Valid
= 1;
1665 Trb
->Adma64V4Desc
[Index
].Act
= 2;
1666 if (Trb
->AdmaLengthMode
== SdMmcAdmaLen26b
) {
1667 Trb
->Adma64V4Desc
[Index
].UpperLength
= 0;
1670 Trb
->Adma64V4Desc
[Index
].LowerLength
= 0;
1671 Trb
->Adma64V4Desc
[Index
].LowerAddress
= (UINT32
)Address
;
1672 Trb
->Adma64V4Desc
[Index
].UpperAddress
= (UINT32
)RShiftU64 (Address
, 32);
1676 Remaining
-= AdmaMaxDataPerLine
;
1677 Address
+= AdmaMaxDataPerLine
;
1681 // Set the last descriptor line as end of descriptor table
1683 if (Trb
->Mode
== SdMmcAdma32bMode
) {
1684 Trb
->Adma32Desc
[Index
].End
= 1;
1685 } else if (Trb
->Mode
== SdMmcAdma64bV3Mode
) {
1686 Trb
->Adma64V3Desc
[Index
].End
= 1;
1688 Trb
->Adma64V4Desc
[Index
].End
= 1;
1695 Prints the contents of the command packet to the debug port.
1697 @param[in] DebugLevel Debug level at which the packet should be printed.
1698 @param[in] Packet Pointer to packet to print.
1702 IN UINT32 DebugLevel
,
1703 IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
1706 if (Packet
== NULL
) {
1710 DEBUG ((DebugLevel
, "Printing EFI_SD_MMC_PASS_THRU_COMMAND_PACKET\n"));
1711 if (Packet
->SdMmcCmdBlk
!= NULL
) {
1712 DEBUG ((DebugLevel
, "Command index: %d, argument: %X\n", Packet
->SdMmcCmdBlk
->CommandIndex
, Packet
->SdMmcCmdBlk
->CommandArgument
));
1713 DEBUG ((DebugLevel
, "Command type: %d, response type: %d\n", Packet
->SdMmcCmdBlk
->CommandType
, Packet
->SdMmcCmdBlk
->ResponseType
));
1716 if (Packet
->SdMmcStatusBlk
!= NULL
) {
1719 "Response 0: %X, 1: %X, 2: %X, 3: %X\n",
1720 Packet
->SdMmcStatusBlk
->Resp0
,
1721 Packet
->SdMmcStatusBlk
->Resp1
,
1722 Packet
->SdMmcStatusBlk
->Resp2
,
1723 Packet
->SdMmcStatusBlk
->Resp3
1727 DEBUG ((DebugLevel
, "Timeout: %ld\n", Packet
->Timeout
));
1728 DEBUG ((DebugLevel
, "InDataBuffer: %p\n", Packet
->InDataBuffer
));
1729 DEBUG ((DebugLevel
, "OutDataBuffer: %p\n", Packet
->OutDataBuffer
));
1730 DEBUG ((DebugLevel
, "InTransferLength: %d\n", Packet
->InTransferLength
));
1731 DEBUG ((DebugLevel
, "OutTransferLength: %d\n", Packet
->OutTransferLength
));
1732 DEBUG ((DebugLevel
, "TransactionStatus: %r\n", Packet
->TransactionStatus
));
1736 Prints the contents of the TRB to the debug port.
1738 @param[in] DebugLevel Debug level at which the TRB should be printed.
1739 @param[in] Trb Pointer to the TRB structure.
1743 IN UINT32 DebugLevel
,
1744 IN SD_MMC_HC_TRB
*Trb
1751 DEBUG ((DebugLevel
, "Printing SD_MMC_HC_TRB\n"));
1752 DEBUG ((DebugLevel
, "Slot: %d\n", Trb
->Slot
));
1753 DEBUG ((DebugLevel
, "BlockSize: %d\n", Trb
->BlockSize
));
1754 DEBUG ((DebugLevel
, "Data: %p\n", Trb
->Data
));
1755 DEBUG ((DebugLevel
, "DataLen: %d\n", Trb
->DataLen
));
1756 DEBUG ((DebugLevel
, "Read: %d\n", Trb
->Read
));
1757 DEBUG ((DebugLevel
, "DataPhy: %lX\n", Trb
->DataPhy
));
1758 DEBUG ((DebugLevel
, "DataMap: %p\n", Trb
->DataMap
));
1759 DEBUG ((DebugLevel
, "Mode: %d\n", Trb
->Mode
));
1760 DEBUG ((DebugLevel
, "AdmaLengthMode: %d\n", Trb
->AdmaLengthMode
));
1761 DEBUG ((DebugLevel
, "Event: %p\n", Trb
->Event
));
1762 DEBUG ((DebugLevel
, "Started: %d\n", Trb
->Started
));
1763 DEBUG ((DebugLevel
, "CommandComplete: %d\n", Trb
->CommandComplete
));
1764 DEBUG ((DebugLevel
, "Timeout: %ld\n", Trb
->Timeout
));
1765 DEBUG ((DebugLevel
, "Retries: %d\n", Trb
->Retries
));
1766 DEBUG ((DebugLevel
, "PioModeTransferCompleted: %d\n", Trb
->PioModeTransferCompleted
));
1767 DEBUG ((DebugLevel
, "PioBlockIndex: %d\n", Trb
->PioBlockIndex
));
1768 DEBUG ((DebugLevel
, "Adma32Desc: %p\n", Trb
->Adma32Desc
));
1769 DEBUG ((DebugLevel
, "Adma64V3Desc: %p\n", Trb
->Adma64V3Desc
));
1770 DEBUG ((DebugLevel
, "Adma64V4Desc: %p\n", Trb
->Adma64V4Desc
));
1771 DEBUG ((DebugLevel
, "AdmaMap: %p\n", Trb
->AdmaMap
));
1772 DEBUG ((DebugLevel
, "AdmaPages: %X\n", Trb
->AdmaPages
));
1774 SdMmcPrintPacket (DebugLevel
, Trb
->Packet
);
1778 Sets up host memory to allow DMA transfer.
1780 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1781 @param[in] Slot The slot number of the SD card to send the command to.
1782 @param[in] Packet A pointer to the SD command data structure.
1784 @retval EFI_SUCCESS Memory has been mapped for DMA transfer.
1785 @retval Others Memory has not been mapped.
1788 SdMmcSetupMemoryForDmaTransfer (
1789 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1791 IN SD_MMC_HC_TRB
*Trb
1794 EFI_PCI_IO_PROTOCOL_OPERATION Flag
;
1795 EFI_PCI_IO_PROTOCOL
*PciIo
;
1800 Flag
= EfiPciIoOperationBusMasterWrite
;
1802 Flag
= EfiPciIoOperationBusMasterRead
;
1805 PciIo
= Private
->PciIo
;
1806 if ((Trb
->Data
!= NULL
) && (Trb
->DataLen
!= 0)) {
1807 MapLength
= Trb
->DataLen
;
1808 Status
= PciIo
->Map (
1816 if (EFI_ERROR (Status
) || (Trb
->DataLen
!= MapLength
)) {
1817 return EFI_BAD_BUFFER_SIZE
;
1821 if ((Trb
->Mode
== SdMmcAdma32bMode
) ||
1822 (Trb
->Mode
== SdMmcAdma64bV3Mode
) ||
1823 (Trb
->Mode
== SdMmcAdma64bV4Mode
))
1825 Status
= BuildAdmaDescTable (Trb
, Private
->ControllerVersion
[Slot
]);
1826 if (EFI_ERROR (Status
)) {
1835 Create a new TRB for the SD/MMC cmd request.
1837 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1838 @param[in] Slot The slot number of the SD card to send the command to.
1839 @param[in] Packet A pointer to the SD command data structure.
1840 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1841 not NULL, then nonblocking I/O is performed, and Event
1842 will be signaled when the Packet completes.
1844 @return Created Trb or NULL.
1849 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
1851 IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1859 Trb
= AllocateZeroPool (sizeof (SD_MMC_HC_TRB
));
1864 Trb
->Signature
= SD_MMC_HC_TRB_SIG
;
1866 Trb
->BlockSize
= 0x200;
1867 Trb
->Packet
= Packet
;
1869 Trb
->Started
= FALSE
;
1870 Trb
->CommandComplete
= FALSE
;
1871 Trb
->Timeout
= Packet
->Timeout
;
1872 Trb
->Retries
= SD_MMC_TRB_RETRIES
;
1873 Trb
->PioModeTransferCompleted
= FALSE
;
1874 Trb
->PioBlockIndex
= 0;
1875 Trb
->Private
= Private
;
1877 if ((Packet
->InTransferLength
!= 0) && (Packet
->InDataBuffer
!= NULL
)) {
1878 Trb
->Data
= Packet
->InDataBuffer
;
1879 Trb
->DataLen
= Packet
->InTransferLength
;
1881 } else if ((Packet
->OutTransferLength
!= 0) && (Packet
->OutDataBuffer
!= NULL
)) {
1882 Trb
->Data
= Packet
->OutDataBuffer
;
1883 Trb
->DataLen
= Packet
->OutTransferLength
;
1885 } else if ((Packet
->InTransferLength
== 0) && (Packet
->OutTransferLength
== 0)) {
1892 if ((Trb
->DataLen
!= 0) && (Trb
->DataLen
< Trb
->BlockSize
)) {
1893 Trb
->BlockSize
= (UINT16
)Trb
->DataLen
;
1896 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
1897 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
1898 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
1899 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
)))
1901 Trb
->Mode
= SdMmcPioMode
;
1903 if (Trb
->DataLen
== 0) {
1904 Trb
->Mode
= SdMmcNoData
;
1905 } else if (Private
->Capability
[Slot
].Adma2
!= 0) {
1906 Trb
->Mode
= SdMmcAdma32bMode
;
1907 Trb
->AdmaLengthMode
= SdMmcAdmaLen16b
;
1908 if ((Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_300
) &&
1909 (Private
->Capability
[Slot
].SysBus64V3
== 1))
1911 Trb
->Mode
= SdMmcAdma64bV3Mode
;
1912 } else if (((Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_400
) &&
1913 (Private
->Capability
[Slot
].SysBus64V3
== 1)) ||
1914 ((Private
->ControllerVersion
[Slot
] >= SD_MMC_HC_CTRL_VER_410
) &&
1915 (Private
->Capability
[Slot
].SysBus64V4
== 1)))
1917 Trb
->Mode
= SdMmcAdma64bV4Mode
;
1920 if (Private
->ControllerVersion
[Slot
] >= SD_MMC_HC_CTRL_VER_410
) {
1921 Trb
->AdmaLengthMode
= SdMmcAdmaLen26b
;
1924 Status
= SdMmcSetupMemoryForDmaTransfer (Private
, Slot
, Trb
);
1925 if (EFI_ERROR (Status
)) {
1928 } else if (Private
->Capability
[Slot
].Sdma
!= 0) {
1929 Trb
->Mode
= SdMmcSdmaMode
;
1930 Status
= SdMmcSetupMemoryForDmaTransfer (Private
, Slot
, Trb
);
1931 if (EFI_ERROR (Status
)) {
1935 Trb
->Mode
= SdMmcPioMode
;
1939 if (Event
!= NULL
) {
1940 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1941 InsertTailList (&Private
->Queue
, &Trb
->TrbList
);
1942 gBS
->RestoreTPL (OldTpl
);
1953 Free the resource used by the TRB.
1955 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
1960 IN SD_MMC_HC_TRB
*Trb
1963 EFI_PCI_IO_PROTOCOL
*PciIo
;
1965 PciIo
= Trb
->Private
->PciIo
;
1967 if (Trb
->AdmaMap
!= NULL
) {
1974 if (Trb
->Adma32Desc
!= NULL
) {
1982 if (Trb
->Adma64V3Desc
!= NULL
) {
1990 if (Trb
->Adma64V4Desc
!= NULL
) {
1998 if (Trb
->DataMap
!= NULL
) {
2010 Check if the env is ready for execute specified TRB.
2012 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2013 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2015 @retval EFI_SUCCESS The env is ready for TRB execution.
2016 @retval EFI_NOT_READY The env is not ready for TRB execution.
2017 @retval Others Some erros happen.
2022 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2023 IN SD_MMC_HC_TRB
*Trb
2027 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2028 EFI_PCI_IO_PROTOCOL
*PciIo
;
2029 UINT32 PresentState
;
2031 Packet
= Trb
->Packet
;
2033 if ((Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) ||
2034 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR1b
) ||
2035 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR5b
))
2038 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
2039 // the Present State register to be 0
2041 PresentState
= BIT0
| BIT1
;
2044 // Wait Command Inhibit (CMD) in the Present State register
2047 PresentState
= BIT0
;
2050 PciIo
= Private
->PciIo
;
2051 Status
= SdMmcHcCheckMmioSet (
2054 SD_MMC_HC_PRESENT_STATE
,
2055 sizeof (PresentState
),
2064 Wait for the env to be ready for execute specified TRB.
2066 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2067 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2069 @retval EFI_SUCCESS The env is ready for TRB execution.
2070 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
2071 @retval Others Some erros happen.
2076 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2077 IN SD_MMC_HC_TRB
*Trb
2081 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2083 BOOLEAN InfiniteWait
;
2086 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
2088 Packet
= Trb
->Packet
;
2089 Timeout
= Packet
->Timeout
;
2091 InfiniteWait
= TRUE
;
2093 InfiniteWait
= FALSE
;
2096 while (InfiniteWait
|| (Timeout
> 0)) {
2098 // Check Trb execution result by reading Normal Interrupt Status register.
2100 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
2101 if (Status
!= EFI_NOT_READY
) {
2106 // Stall for 1 microsecond.
2117 Execute the specified TRB.
2119 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2120 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2122 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
2123 @retval Others Some erros happen when sending this request to the host controller.
2128 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2129 IN SD_MMC_HC_TRB
*Trb
2133 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2134 EFI_PCI_IO_PROTOCOL
*PciIo
;
2144 BOOLEAN AddressingMode64
;
2146 AddressingMode64
= FALSE
;
2148 Packet
= Trb
->Packet
;
2149 PciIo
= Trb
->Private
->PciIo
;
2151 // Clear all bits in Error Interrupt Status Register
2154 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ERR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
2155 if (EFI_ERROR (Status
)) {
2160 // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.
2163 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
2164 if (EFI_ERROR (Status
)) {
2168 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
2169 Status
= SdMmcHcCheckMmioSet (
2172 SD_MMC_HC_HOST_CTRL2
,
2174 SD_MMC_HC_64_ADDR_EN
,
2175 SD_MMC_HC_64_ADDR_EN
2177 if (!EFI_ERROR (Status
)) {
2178 AddressingMode64
= TRUE
;
2183 // Set Host Control 1 register DMA Select field
2185 if ((Trb
->Mode
== SdMmcAdma32bMode
) ||
2186 (Trb
->Mode
== SdMmcAdma64bV4Mode
))
2189 Status
= SdMmcHcOrMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
2190 if (EFI_ERROR (Status
)) {
2193 } else if (Trb
->Mode
== SdMmcAdma64bV3Mode
) {
2194 HostCtrl1
= BIT4
|BIT3
;
2195 Status
= SdMmcHcOrMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
2196 if (EFI_ERROR (Status
)) {
2201 SdMmcHcLedOnOff (PciIo
, Trb
->Slot
, TRUE
);
2203 if (Trb
->Mode
== SdMmcSdmaMode
) {
2204 if ((!AddressingMode64
) &&
2205 ((UINT64
)(UINTN
)Trb
->DataPhy
>= 0x100000000ul
))
2207 return EFI_INVALID_PARAMETER
;
2210 SdmaAddr
= (UINT64
)(UINTN
)Trb
->DataPhy
;
2212 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
2213 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (UINT64
), &SdmaAddr
);
2215 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (UINT32
), &SdmaAddr
);
2218 if (EFI_ERROR (Status
)) {
2221 } else if ((Trb
->Mode
== SdMmcAdma32bMode
) ||
2222 (Trb
->Mode
== SdMmcAdma64bV3Mode
) ||
2223 (Trb
->Mode
== SdMmcAdma64bV4Mode
))
2225 AdmaAddr
= (UINT64
)(UINTN
)Trb
->AdmaDescPhy
;
2226 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (AdmaAddr
), &AdmaAddr
);
2227 if (EFI_ERROR (Status
)) {
2232 BlkSize
= Trb
->BlockSize
;
2233 if (Trb
->Mode
== SdMmcSdmaMode
) {
2235 // Set SDMA boundary to be 512K bytes.
2240 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_SIZE
, FALSE
, sizeof (BlkSize
), &BlkSize
);
2241 if (EFI_ERROR (Status
)) {
2246 if (Trb
->Mode
!= SdMmcNoData
) {
2248 // Calcuate Block Count.
2250 BlkCount
= (Trb
->DataLen
/ Trb
->BlockSize
);
2253 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_410
) {
2254 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_SDMA_ADDR
, FALSE
, sizeof (UINT32
), &BlkCount
);
2256 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_BLK_COUNT
, FALSE
, sizeof (UINT16
), &BlkCount
);
2259 if (EFI_ERROR (Status
)) {
2263 Argument
= Packet
->SdMmcCmdBlk
->CommandArgument
;
2264 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_ARG1
, FALSE
, sizeof (Argument
), &Argument
);
2265 if (EFI_ERROR (Status
)) {
2270 if (Trb
->Mode
!= SdMmcNoData
) {
2271 if (Trb
->Mode
!= SdMmcPioMode
) {
2280 TransMode
|= BIT5
| BIT1
;
2284 // Only SD memory card needs to use AUTO CMD12 feature.
2286 if (Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) {
2293 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_TRANS_MOD
, FALSE
, sizeof (TransMode
), &TransMode
);
2294 if (EFI_ERROR (Status
)) {
2298 Cmd
= (UINT16
)LShiftU64 (Packet
->SdMmcCmdBlk
->CommandIndex
, 8);
2299 if (Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) {
2304 // Convert ResponseType to value
2306 if (Packet
->SdMmcCmdBlk
->CommandType
!= SdMmcCommandTypeBc
) {
2307 switch (Packet
->SdMmcCmdBlk
->ResponseType
) {
2308 case SdMmcResponseTypeR1
:
2309 case SdMmcResponseTypeR5
:
2310 case SdMmcResponseTypeR6
:
2311 case SdMmcResponseTypeR7
:
2312 Cmd
|= (BIT1
| BIT3
| BIT4
);
2314 case SdMmcResponseTypeR2
:
2315 Cmd
|= (BIT0
| BIT3
);
2317 case SdMmcResponseTypeR3
:
2318 case SdMmcResponseTypeR4
:
2321 case SdMmcResponseTypeR1b
:
2322 case SdMmcResponseTypeR5b
:
2323 Cmd
|= (BIT0
| BIT1
| BIT3
| BIT4
);
2334 Status
= SdMmcHcRwMmio (PciIo
, Trb
->Slot
, SD_MMC_HC_COMMAND
, FALSE
, sizeof (Cmd
), &Cmd
);
2339 Performs SW reset based on passed error status mask.
2341 @param[in] Private Pointer to driver private data.
2342 @param[in] Slot Index of the slot to reset.
2343 @param[in] ErrIntStatus Error interrupt status mask.
2345 @retval EFI_SUCCESS Software reset performed successfully.
2346 @retval Other Software reset failed.
2349 SdMmcSoftwareReset (
2350 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2352 IN UINT16 ErrIntStatus
2359 if ((ErrIntStatus
& 0x0F) != 0) {
2363 if ((ErrIntStatus
& 0x70) != 0) {
2367 Status
= SdMmcHcRwMmio (
2375 if (EFI_ERROR (Status
)) {
2379 Status
= SdMmcHcWaitMmioSet (
2386 SD_MMC_HC_GENERIC_TIMEOUT
2388 if (EFI_ERROR (Status
)) {
2396 Checks the error status in error status register
2397 and issues appropriate software reset as described in
2398 SD specification section 3.10.
2400 @param[in] Private Pointer to driver private data.
2401 @param[in] Slot Index of the slot for device.
2402 @param[in] IntStatus Normal interrupt status mask.
2404 @retval EFI_CRC_ERROR CRC error happened during CMD execution.
2405 @retval EFI_SUCCESS No error reported.
2406 @retval Others Some other error happened.
2410 SdMmcCheckAndRecoverErrors (
2411 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2416 UINT16 ErrIntStatus
;
2418 EFI_STATUS ErrorStatus
;
2420 if ((IntStatus
& BIT15
) == 0) {
2424 Status
= SdMmcHcRwMmio (
2427 SD_MMC_HC_ERR_INT_STS
,
2429 sizeof (ErrIntStatus
),
2432 if (EFI_ERROR (Status
)) {
2436 DEBUG ((DEBUG_ERROR
, "Error reported by SDHCI\n"));
2437 DEBUG ((DEBUG_ERROR
, "Interrupt status = %X\n", IntStatus
));
2438 DEBUG ((DEBUG_ERROR
, "Error interrupt status = %X\n", ErrIntStatus
));
2441 // If the data timeout error is reported
2442 // but data transfer is signaled as completed we
2443 // have to ignore data timeout. We also assume that no
2444 // other error is present on the link since data transfer
2445 // completed successfully. Error interrupt status
2446 // register is going to be reset when the next command
2449 if (((ErrIntStatus
& BIT4
) != 0) && ((IntStatus
& BIT1
) != 0)) {
2454 // We treat both CMD and DAT CRC errors and
2455 // end bits errors as EFI_CRC_ERROR. This will
2456 // let higher layer know that the error possibly
2457 // happened due to random bus condition and the
2458 // command can be retried.
2460 if ((ErrIntStatus
& (BIT1
| BIT2
| BIT5
| BIT6
)) != 0) {
2461 ErrorStatus
= EFI_CRC_ERROR
;
2463 ErrorStatus
= EFI_DEVICE_ERROR
;
2466 Status
= SdMmcSoftwareReset (Private
, Slot
, ErrIntStatus
);
2467 if (EFI_ERROR (Status
)) {
2475 Reads the response data into the TRB buffer.
2476 This function assumes that caller made sure that
2477 command has completed.
2479 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2480 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2482 @retval EFI_SUCCESS Response read successfully.
2483 @retval Others Failed to get response.
2487 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2488 IN SD_MMC_HC_TRB
*Trb
2491 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2496 Packet
= Trb
->Packet
;
2498 if (Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeBc
) {
2502 for (Index
= 0; Index
< 4; Index
++) {
2503 Status
= SdMmcHcRwMmio (
2506 SD_MMC_HC_RESPONSE
+ Index
* 4,
2511 if (EFI_ERROR (Status
)) {
2516 CopyMem (Packet
->SdMmcStatusBlk
, Response
, sizeof (Response
));
2522 Checks if the command completed. If the command
2523 completed it gets the response and records the
2524 command completion in the TRB.
2526 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2527 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2528 @param[in] IntStatus Snapshot of the normal interrupt status register.
2530 @retval EFI_SUCCESS Command completed successfully.
2531 @retval EFI_NOT_READY Command completion still pending.
2532 @retval Others Command failed to complete.
2535 SdMmcCheckCommandComplete (
2536 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2537 IN SD_MMC_HC_TRB
*Trb
,
2544 if ((IntStatus
& BIT0
) != 0) {
2546 Status
= SdMmcHcRwMmio (
2549 SD_MMC_HC_NOR_INT_STS
,
2554 if (EFI_ERROR (Status
)) {
2558 Status
= SdMmcGetResponse (Private
, Trb
);
2559 if (EFI_ERROR (Status
)) {
2563 Trb
->CommandComplete
= TRUE
;
2567 return EFI_NOT_READY
;
2571 Transfers data from card using PIO method.
2573 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2574 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2575 @param[in] IntStatus Snapshot of the normal interrupt status register.
2577 @retval EFI_SUCCESS PIO transfer completed successfully.
2578 @retval EFI_NOT_READY PIO transfer completion still pending.
2579 @retval Others PIO transfer failed to complete.
2582 SdMmcTransferDataWithPio (
2583 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2584 IN SD_MMC_HC_TRB
*Trb
,
2591 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
2594 BlockCount
= (Trb
->DataLen
/ Trb
->BlockSize
);
2595 if (Trb
->DataLen
% Trb
->BlockSize
!= 0) {
2599 if (Trb
->PioBlockIndex
>= BlockCount
) {
2603 switch (Trb
->BlockSize
% sizeof (UINT32
)) {
2605 Width
= EfiPciIoWidthFifoUint32
;
2606 Count
= Trb
->BlockSize
/ sizeof (UINT32
);
2609 Width
= EfiPciIoWidthFifoUint16
;
2610 Count
= Trb
->BlockSize
/ sizeof (UINT16
);
2615 Width
= EfiPciIoWidthFifoUint8
;
2616 Count
= Trb
->BlockSize
;
2621 if ((IntStatus
& BIT5
) == 0) {
2622 return EFI_NOT_READY
;
2626 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data16
), &Data16
);
2628 Status
= Private
->PciIo
->Mem
.Read (
2632 SD_MMC_HC_BUF_DAT_PORT
,
2634 (VOID
*)((UINT8
*)Trb
->Data
+ (Trb
->BlockSize
* Trb
->PioBlockIndex
))
2636 if (EFI_ERROR (Status
)) {
2640 Trb
->PioBlockIndex
++;
2642 if ((IntStatus
& BIT4
) == 0) {
2643 return EFI_NOT_READY
;
2647 SdMmcHcRwMmio (Private
->PciIo
, Trb
->Slot
, SD_MMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data16
), &Data16
);
2649 Status
= Private
->PciIo
->Mem
.Write (
2653 SD_MMC_HC_BUF_DAT_PORT
,
2655 (VOID
*)((UINT8
*)Trb
->Data
+ (Trb
->BlockSize
* Trb
->PioBlockIndex
))
2657 if (EFI_ERROR (Status
)) {
2661 Trb
->PioBlockIndex
++;
2664 if (Trb
->PioBlockIndex
>= BlockCount
) {
2665 Trb
->PioModeTransferCompleted
= TRUE
;
2668 return EFI_NOT_READY
;
2673 Update the SDMA address on the SDMA buffer boundary interrupt.
2675 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2676 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2678 @retval EFI_SUCCESS Updated SDMA buffer address.
2679 @retval Others Failed to update SDMA buffer address.
2682 SdMmcUpdateSdmaAddress (
2683 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2684 IN SD_MMC_HC_TRB
*Trb
2690 SdmaAddr
= SD_MMC_SDMA_ROUND_UP ((UINTN
)Trb
->DataPhy
, SD_MMC_SDMA_BOUNDARY
);
2692 if (Private
->ControllerVersion
[Trb
->Slot
] >= SD_MMC_HC_CTRL_VER_400
) {
2693 Status
= SdMmcHcRwMmio (
2696 SD_MMC_HC_ADMA_SYS_ADDR
,
2702 Status
= SdMmcHcRwMmio (
2705 SD_MMC_HC_SDMA_ADDR
,
2712 if (EFI_ERROR (Status
)) {
2716 Trb
->DataPhy
= (UINT64
)(UINTN
)SdmaAddr
;
2721 Checks if the data transfer completed and performs any actions
2722 neccessary to continue the data transfer such as SDMA system
2723 address fixup or PIO data transfer.
2725 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2726 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2727 @param[in] IntStatus Snapshot of the normal interrupt status register.
2729 @retval EFI_SUCCESS Data transfer completed successfully.
2730 @retval EFI_NOT_READY Data transfer completion still pending.
2731 @retval Others Data transfer failed to complete.
2734 SdMmcCheckDataTransfer (
2735 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2736 IN SD_MMC_HC_TRB
*Trb
,
2743 if ((IntStatus
& BIT1
) != 0) {
2745 Status
= SdMmcHcRwMmio (
2748 SD_MMC_HC_NOR_INT_STS
,
2756 if ((Trb
->Mode
== SdMmcPioMode
) && !Trb
->PioModeTransferCompleted
) {
2757 Status
= SdMmcTransferDataWithPio (Private
, Trb
, IntStatus
);
2758 if (EFI_ERROR (Status
)) {
2763 if ((Trb
->Mode
== SdMmcSdmaMode
) && ((IntStatus
& BIT3
) != 0)) {
2765 Status
= SdMmcHcRwMmio (
2768 SD_MMC_HC_NOR_INT_STS
,
2773 if (EFI_ERROR (Status
)) {
2777 Status
= SdMmcUpdateSdmaAddress (Private
, Trb
);
2778 if (EFI_ERROR (Status
)) {
2783 return EFI_NOT_READY
;
2787 Check the TRB execution result.
2789 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2790 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2792 @retval EFI_SUCCESS The TRB is executed successfully.
2793 @retval EFI_NOT_READY The TRB is not completed for execution.
2794 @retval Others Some erros happen when executing this request.
2798 SdMmcCheckTrbResult (
2799 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2800 IN SD_MMC_HC_TRB
*Trb
2804 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2807 Packet
= Trb
->Packet
;
2809 // Check Trb execution result by reading Normal Interrupt Status register.
2811 Status
= SdMmcHcRwMmio (
2814 SD_MMC_HC_NOR_INT_STS
,
2819 if (EFI_ERROR (Status
)) {
2824 // Check if there are any errors reported by host controller
2825 // and if neccessary recover the controller before next command is executed.
2827 Status
= SdMmcCheckAndRecoverErrors (Private
, Trb
->Slot
, IntStatus
);
2828 if (EFI_ERROR (Status
)) {
2833 // Tuning commands are the only ones that do not generate command
2834 // complete interrupt. Process them here before entering the code
2835 // that waits for command completion.
2837 if (((Private
->Slot
[Trb
->Slot
].CardType
== EmmcCardType
) &&
2838 (Packet
->SdMmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
)) ||
2839 ((Private
->Slot
[Trb
->Slot
].CardType
== SdCardType
) &&
2840 (Packet
->SdMmcCmdBlk
->CommandIndex
== SD_SEND_TUNING_BLOCK
)))
2842 Status
= SdMmcTransferDataWithPio (Private
, Trb
, IntStatus
);
2846 if (!Trb
->CommandComplete
) {
2847 Status
= SdMmcCheckCommandComplete (Private
, Trb
, IntStatus
);
2848 if (EFI_ERROR (Status
)) {
2853 if ((Packet
->SdMmcCmdBlk
->CommandType
== SdMmcCommandTypeAdtc
) ||
2854 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR1b
) ||
2855 (Packet
->SdMmcCmdBlk
->ResponseType
== SdMmcResponseTypeR5b
))
2857 Status
= SdMmcCheckDataTransfer (Private
, Trb
, IntStatus
);
2859 Status
= EFI_SUCCESS
;
2863 if (Status
!= EFI_NOT_READY
) {
2864 SdMmcHcLedOnOff (Private
->PciIo
, Trb
->Slot
, FALSE
);
2865 if (EFI_ERROR (Status
)) {
2866 DEBUG ((DEBUG_ERROR
, "TRB failed with %r\n", Status
));
2867 SdMmcPrintTrb (DEBUG_ERROR
, Trb
);
2869 DEBUG ((DEBUG_VERBOSE
, "TRB success\n"));
2870 SdMmcPrintTrb (DEBUG_VERBOSE
, Trb
);
2878 Wait for the TRB execution result.
2880 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
2881 @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.
2883 @retval EFI_SUCCESS The TRB is executed successfully.
2884 @retval Others Some erros happen when executing this request.
2888 SdMmcWaitTrbResult (
2889 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
2890 IN SD_MMC_HC_TRB
*Trb
2894 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
2896 BOOLEAN InfiniteWait
;
2898 Packet
= Trb
->Packet
;
2900 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
2902 Timeout
= Packet
->Timeout
;
2904 InfiniteWait
= TRUE
;
2906 InfiniteWait
= FALSE
;
2909 while (InfiniteWait
|| (Timeout
> 0)) {
2911 // Check Trb execution result by reading Normal Interrupt Status register.
2913 Status
= SdMmcCheckTrbResult (Private
, Trb
);
2914 if (Status
!= EFI_NOT_READY
) {
2919 // Stall for 1 microsecond.