3 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include "EmmcBlockIoPei.h"
17 Read/Write specified EMMC host controller mmio register.
19 @param[in] Address The address of the mmio register to be read/written.
20 @param[in] Read A boolean to indicate it's read or write operation.
21 @param[in] Count The width of the mmio register in bytes.
22 Must be 1, 2 , 4 or 8 bytes.
23 @param[in, out] Data For read operations, the destination buffer to store
24 the results. For write operations, the source buffer
25 to write data from. The caller is responsible for
26 having ownership of the data buffer and ensuring its
27 size not less than Count bytes.
29 @retval EFI_INVALID_PARAMETER The Address or the Data or the Count is not valid.
30 @retval EFI_SUCCESS The read/write operation succeeds.
31 @retval Others The read/write operation fails.
43 if ((Address
== 0) || (Data
== NULL
)) {
44 return EFI_INVALID_PARAMETER
;
47 if ((Count
!= 1) && (Count
!= 2) && (Count
!= 4) && (Count
!= 8)) {
48 return EFI_INVALID_PARAMETER
;
54 *(UINT8
*)Data
= MmioRead8 (Address
);
56 MmioWrite8 (Address
, *(UINT8
*)Data
);
61 *(UINT16
*)Data
= MmioRead16 (Address
);
63 MmioWrite16 (Address
, *(UINT16
*)Data
);
68 *(UINT32
*)Data
= MmioRead32 (Address
);
70 MmioWrite32 (Address
, *(UINT32
*)Data
);
75 *(UINT64
*)Data
= MmioRead64 (Address
);
77 MmioWrite64 (Address
, *(UINT64
*)Data
);
82 return EFI_INVALID_PARAMETER
;
89 Do OR operation with the value of the specified EMMC host controller mmio register.
91 @param[in] Address The address of the mmio register to be read/written.
92 @param[in] Count The width of the mmio register in bytes.
93 Must be 1, 2 , 4 or 8 bytes.
94 @param[in] OrData The pointer to the data used to do OR operation.
95 The caller is responsible for having ownership of
96 the data buffer and ensuring its size not less than
99 @retval EFI_INVALID_PARAMETER The Address or the OrData or the Count is not valid.
100 @retval EFI_SUCCESS The OR operation succeeds.
101 @retval Others The OR operation fails.
116 Status
= EmmcPeimHcRwMmio (Address
, TRUE
, Count
, &Data
);
117 if (EFI_ERROR (Status
)) {
122 Or
= *(UINT8
*) OrData
;
123 } else if (Count
== 2) {
124 Or
= *(UINT16
*) OrData
;
125 } else if (Count
== 4) {
126 Or
= *(UINT32
*) OrData
;
127 } else if (Count
== 8) {
128 Or
= *(UINT64
*) OrData
;
130 return EFI_INVALID_PARAMETER
;
134 Status
= EmmcPeimHcRwMmio (Address
, FALSE
, Count
, &Data
);
140 Do AND operation with the value of the specified EMMC host controller mmio register.
142 @param[in] Address The address of the mmio register to be read/written.
143 @param[in] Count The width of the mmio register in bytes.
144 Must be 1, 2 , 4 or 8 bytes.
145 @param[in] AndData The pointer to the data used to do AND operation.
146 The caller is responsible for having ownership of
147 the data buffer and ensuring its size not less than
150 @retval EFI_INVALID_PARAMETER The Address or the AndData or the Count is not valid.
151 @retval EFI_SUCCESS The AND operation succeeds.
152 @retval Others The AND operation fails.
167 Status
= EmmcPeimHcRwMmio (Address
, TRUE
, Count
, &Data
);
168 if (EFI_ERROR (Status
)) {
173 And
= *(UINT8
*) AndData
;
174 } else if (Count
== 2) {
175 And
= *(UINT16
*) AndData
;
176 } else if (Count
== 4) {
177 And
= *(UINT32
*) AndData
;
178 } else if (Count
== 8) {
179 And
= *(UINT64
*) AndData
;
181 return EFI_INVALID_PARAMETER
;
185 Status
= EmmcPeimHcRwMmio (Address
, FALSE
, Count
, &Data
);
191 Wait for the value of the specified MMIO register set to the test value.
193 @param[in] Address The address of the mmio register to be checked.
194 @param[in] Count The width of the mmio register in bytes.
195 Must be 1, 2, 4 or 8 bytes.
196 @param[in] MaskValue The mask value of memory.
197 @param[in] TestValue The test value of memory.
199 @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.
200 @retval EFI_SUCCESS The MMIO register has expected value.
201 @retval Others The MMIO operation fails.
206 EmmcPeimHcCheckMmioSet (
217 // Access PCI MMIO space to see if the value is the tested one.
220 Status
= EmmcPeimHcRwMmio (Address
, TRUE
, Count
, &Value
);
221 if (EFI_ERROR (Status
)) {
227 if (Value
== TestValue
) {
231 return EFI_NOT_READY
;
235 Wait for the value of the specified MMIO register set to the test value.
237 @param[in] Address The address of the mmio register to wait.
238 @param[in] Count The width of the mmio register in bytes.
239 Must be 1, 2, 4 or 8 bytes.
240 @param[in] MaskValue The mask value of memory.
241 @param[in] TestValue The test value of memory.
242 @param[in] Timeout The time out value for wait memory set, uses 1
243 microsecond as a unit.
245 @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout
247 @retval EFI_SUCCESS The MMIO register has expected value.
248 @retval Others The MMIO operation fails.
253 EmmcPeimHcWaitMmioSet (
262 BOOLEAN InfiniteWait
;
267 InfiniteWait
= FALSE
;
270 while (InfiniteWait
|| (Timeout
> 0)) {
271 Status
= EmmcPeimHcCheckMmioSet (
277 if (Status
!= EFI_NOT_READY
) {
282 // Stall for 1 microsecond.
284 MicroSecondDelay (1);
293 Software reset the specified EMMC host controller and enable all interrupts.
295 @param[in] Bar The mmio base address of the slot to be accessed.
297 @retval EFI_SUCCESS The software reset executes successfully.
298 @retval Others The software reset fails.
310 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_SW_RST
, FALSE
, sizeof (SwReset
), &SwReset
);
312 if (EFI_ERROR (Status
)) {
313 DEBUG ((EFI_D_ERROR
, "EmmcPeimHcReset: write full 1 fails: %r\n", Status
));
317 Status
= EmmcPeimHcWaitMmioSet (
318 Bar
+ EMMC_HC_SW_RST
,
324 if (EFI_ERROR (Status
)) {
325 DEBUG ((EFI_D_INFO
, "EmmcPeimHcReset: reset done with %r\n", Status
));
329 // Enable all interrupt after reset all.
331 Status
= EmmcPeimHcEnableInterrupt (Bar
);
337 Set all interrupt status bits in Normal and Error Interrupt Status Enable
340 @param[in] Bar The mmio base address of the slot to be accessed.
342 @retval EFI_SUCCESS The operation executes successfully.
343 @retval Others The operation fails.
347 EmmcPeimHcEnableInterrupt (
355 // Enable all bits in Error Interrupt Status Enable Register
358 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_ERR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
359 if (EFI_ERROR (Status
)) {
363 // Enable all bits in Normal Interrupt Status Enable Register
366 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_NOR_INT_STS_EN
, FALSE
, sizeof (IntStatus
), &IntStatus
);
372 Get the capability data from the specified slot.
374 @param[in] Bar The mmio base address of the slot to be accessed.
375 @param[out] Capability The buffer to store the capability data.
377 @retval EFI_SUCCESS The operation executes successfully.
378 @retval Others The operation fails.
382 EmmcPeimHcGetCapability (
384 OUT EMMC_HC_SLOT_CAP
*Capability
390 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_CAP
, TRUE
, sizeof (Cap
), &Cap
);
391 if (EFI_ERROR (Status
)) {
395 CopyMem (Capability
, &Cap
, sizeof (Cap
));
401 Detect whether there is a EMMC card attached at the specified EMMC host controller
404 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
406 @param[in] Bar The mmio base address of the slot to be accessed.
408 @retval EFI_SUCCESS There is a EMMC card attached.
409 @retval EFI_NO_MEDIA There is not a EMMC card attached.
410 @retval Others The detection fails.
414 EmmcPeimHcCardDetect (
423 // Check Normal Interrupt Status Register
425 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_NOR_INT_STS
, TRUE
, sizeof (Data
), &Data
);
426 if (EFI_ERROR (Status
)) {
430 if ((Data
& (BIT6
| BIT7
)) != 0) {
432 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
435 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_NOR_INT_STS
, FALSE
, sizeof (Data
), &Data
);
436 if (EFI_ERROR (Status
)) {
442 // Check Present State Register to see if there is a card presented.
444 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_PRESENT_STATE
, TRUE
, sizeof (PresentState
), &PresentState
);
445 if (EFI_ERROR (Status
)) {
449 if ((PresentState
& BIT16
) != 0) {
457 Stop EMMC card clock.
459 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
461 @param[in] Bar The mmio base address of the slot to be accessed.
463 @retval EFI_SUCCESS Succeed to stop EMMC clock.
464 @retval Others Fail to stop EMMC clock.
468 EmmcPeimHcStopClock (
477 // Ensure no SD transactions are occurring on the SD Bus by
478 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
479 // in the Present State register to be 0.
481 Status
= EmmcPeimHcWaitMmioSet (
482 Bar
+ EMMC_HC_PRESENT_STATE
,
483 sizeof (PresentState
),
488 if (EFI_ERROR (Status
)) {
493 // Set SD Clock Enable in the Clock Control register to 0
495 ClockCtrl
= (UINT16
)~BIT2
;
496 Status
= EmmcPeimHcAndMmio (Bar
+ EMMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
502 EMMC card clock supply.
504 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
506 @param[in] Bar The mmio base address of the slot to be accessed.
507 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
509 @retval EFI_SUCCESS The clock is supplied successfully.
510 @retval Others The clock isn't supplied successfully.
514 EmmcPeimHcClockSupply (
520 EMMC_HC_SLOT_CAP Capability
;
525 UINT16 ControllerVer
;
529 // Calculate a divisor for SD clock frequency
531 Status
= EmmcPeimHcGetCapability (Bar
, &Capability
);
532 if (EFI_ERROR (Status
)) {
535 ASSERT (Capability
.BaseClkFreq
!= 0);
537 BaseClkFreq
= Capability
.BaseClkFreq
;
539 if (ClockFreq
== 0) {
540 return EFI_INVALID_PARAMETER
;
543 if (ClockFreq
> (BaseClkFreq
* 1000)) {
544 ClockFreq
= BaseClkFreq
* 1000;
548 // Calculate the divisor of base frequency.
551 SettingFreq
= BaseClkFreq
* 1000;
552 while (ClockFreq
< SettingFreq
) {
555 SettingFreq
= (BaseClkFreq
* 1000) / (2 * Divisor
);
556 Remainder
= (BaseClkFreq
* 1000) % (2 * Divisor
);
557 if ((ClockFreq
== SettingFreq
) && (Remainder
== 0)) {
560 if ((ClockFreq
== SettingFreq
) && (Remainder
!= 0)) {
565 DEBUG ((EFI_D_INFO
, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq
, Divisor
, ClockFreq
));
567 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_CTRL_VER
, TRUE
, sizeof (ControllerVer
), &ControllerVer
);
568 if (EFI_ERROR (Status
)) {
572 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
574 if ((ControllerVer
& 0xFF) == 2) {
575 ASSERT (Divisor
<= 0x3FF);
576 ClockCtrl
= ((Divisor
& 0xFF) << 8) | ((Divisor
& 0x300) >> 2);
577 } else if (((ControllerVer
& 0xFF) == 0) || ((ControllerVer
& 0xFF) == 1)) {
579 // Only the most significant bit can be used as divisor.
581 if (((Divisor
- 1) & Divisor
) != 0) {
582 Divisor
= 1 << (HighBitSet32 (Divisor
) + 1);
584 ASSERT (Divisor
<= 0x80);
585 ClockCtrl
= (Divisor
& 0xFF) << 8;
587 DEBUG ((EFI_D_ERROR
, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer
));
588 return EFI_UNSUPPORTED
;
592 // Stop bus clock at first
594 Status
= EmmcPeimHcStopClock (Bar
);
595 if (EFI_ERROR (Status
)) {
600 // Supply clock frequency with specified divisor
603 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_CLOCK_CTRL
, FALSE
, sizeof (ClockCtrl
), &ClockCtrl
);
604 if (EFI_ERROR (Status
)) {
605 DEBUG ((EFI_D_ERROR
, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
610 // Wait Internal Clock Stable in the Clock Control register to be 1
612 Status
= EmmcPeimHcWaitMmioSet (
613 Bar
+ EMMC_HC_CLOCK_CTRL
,
619 if (EFI_ERROR (Status
)) {
624 // Set SD Clock Enable in the Clock Control register to 1
627 Status
= EmmcPeimHcOrMmio (Bar
+ EMMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
633 EMMC bus power control.
635 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
637 @param[in] Bar The mmio base address of the slot to be accessed.
638 @param[in] PowerCtrl The value setting to the power control register.
640 @retval TRUE There is a EMMC card attached.
641 @retval FALSE There is no a EMMC card attached.
645 EmmcPeimHcPowerControl (
655 PowerCtrl
&= (UINT8
)~BIT0
;
656 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
657 if (EFI_ERROR (Status
)) {
662 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
665 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_POWER_CTRL
, FALSE
, sizeof (PowerCtrl
), &PowerCtrl
);
671 Set the EMMC bus width.
673 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
675 @param[in] Bar The mmio base address of the slot to be accessed.
676 @param[in] BusWidth The bus width used by the EMMC device, it must be 1, 4 or 8.
678 @retval EFI_SUCCESS The bus width is set successfully.
679 @retval Others The bus width isn't set successfully.
683 EmmcPeimHcSetBusWidth (
692 HostCtrl1
= (UINT8
)~(BIT5
| BIT1
);
693 Status
= EmmcPeimHcAndMmio (Bar
+ EMMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
694 } else if (BusWidth
== 4) {
695 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
696 if (EFI_ERROR (Status
)) {
700 HostCtrl1
&= (UINT8
)~BIT5
;
701 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
702 } else if (BusWidth
== 8) {
703 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_HOST_CTRL1
, TRUE
, sizeof (HostCtrl1
), &HostCtrl1
);
704 if (EFI_ERROR (Status
)) {
707 HostCtrl1
&= (UINT8
)~BIT1
;
709 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_HOST_CTRL1
, FALSE
, sizeof (HostCtrl1
), &HostCtrl1
);
712 return EFI_INVALID_PARAMETER
;
719 Supply EMMC card with lowest clock frequency at initialization.
721 @param[in] Bar The mmio base address of the slot to be accessed.
723 @retval EFI_SUCCESS The clock is supplied successfully.
724 @retval Others The clock isn't supplied successfully.
728 EmmcPeimHcInitClockFreq (
733 EMMC_HC_SLOT_CAP Capability
;
737 // Calculate a divisor for SD clock frequency
739 Status
= EmmcPeimHcGetCapability (Bar
, &Capability
);
740 if (EFI_ERROR (Status
)) {
744 if (Capability
.BaseClkFreq
== 0) {
746 // Don't support get Base Clock Frequency information via another method
748 return EFI_UNSUPPORTED
;
751 // Supply 400KHz clock frequency at initialization phase.
754 Status
= EmmcPeimHcClockSupply (Bar
, InitFreq
);
759 Supply EMMC card with maximum voltage at initialization.
761 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
763 @param[in] Bar The mmio base address of the slot to be accessed.
765 @retval EFI_SUCCESS The voltage is supplied successfully.
766 @retval Others The voltage isn't supplied successfully.
770 EmmcPeimHcInitPowerVoltage (
775 EMMC_HC_SLOT_CAP Capability
;
780 // Get the support voltage of the Host Controller
782 Status
= EmmcPeimHcGetCapability (Bar
, &Capability
);
783 if (EFI_ERROR (Status
)) {
787 // Calculate supported maximum voltage according to SD Bus Voltage Select
789 if (Capability
.Voltage33
!= 0) {
794 } else if (Capability
.Voltage30
!= 0) {
799 } else if (Capability
.Voltage18
!= 0) {
805 Status
= EmmcPeimHcOrMmio (Bar
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
806 if (EFI_ERROR (Status
)) {
809 MicroSecondDelay (5000);
812 return EFI_DEVICE_ERROR
;
816 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
818 Status
= EmmcPeimHcPowerControl (Bar
, MaxVoltage
);
824 Initialize the Timeout Control register with most conservative value at initialization.
826 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
828 @param[in] Bar The mmio base address of the slot to be accessed.
830 @retval EFI_SUCCESS The timeout control register is configured successfully.
831 @retval Others The timeout control register isn't configured successfully.
835 EmmcPeimHcInitTimeoutCtrl (
843 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_TIMEOUT_CTRL
, FALSE
, sizeof (Timeout
), &Timeout
);
849 Initial EMMC host controller with lowest clock frequency, max power and max timeout value
852 @param[in] Bar The mmio base address of the slot to be accessed.
854 @retval EFI_SUCCESS The host controller is initialized successfully.
855 @retval Others The host controller isn't initialized successfully.
865 Status
= EmmcPeimHcInitClockFreq (Bar
);
866 if (EFI_ERROR (Status
)) {
870 Status
= EmmcPeimHcInitPowerVoltage (Bar
);
871 if (EFI_ERROR (Status
)) {
875 Status
= EmmcPeimHcInitTimeoutCtrl (Bar
);
882 @param[in] Bar The mmio base address of the slot to be accessed.
883 @param[in] On The boolean to turn on/off LED.
885 @retval EFI_SUCCESS The LED is turned on/off successfully.
886 @retval Others The LED isn't turned on/off successfully.
900 Status
= EmmcPeimHcOrMmio (Bar
+ EMMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
902 HostCtrl1
= (UINT8
)~BIT0
;
903 Status
= EmmcPeimHcAndMmio (Bar
+ EMMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
910 Build ADMA descriptor table for transfer.
912 Refer to SD Host Controller Simplified spec 3.0 Section 1.13 for details.
914 @param[in] Trb The pointer to the EMMC_TRB instance.
916 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
917 @retval Others The ADMA descriptor table isn't created successfully.
925 EFI_PHYSICAL_ADDRESS Data
;
932 Data
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Trb
->Data
;
933 DataLen
= Trb
->DataLen
;
935 // Only support 32bit ADMA Descriptor Table
937 if ((Data
>= 0x100000000ul
) || ((Data
+ DataLen
) > 0x100000000ul
)) {
938 return EFI_INVALID_PARAMETER
;
941 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
942 // for 32-bit address descriptor table.
944 if ((Data
& (BIT0
| BIT1
)) != 0) {
945 DEBUG ((EFI_D_INFO
, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data
));
948 Entries
= DivU64x32 ((DataLen
+ ADMA_MAX_DATA_PER_LINE
- 1), ADMA_MAX_DATA_PER_LINE
);
950 Trb
->AdmaDescSize
= (UINTN
)MultU64x32 (Entries
, sizeof (EMMC_HC_ADMA_DESC_LINE
));
951 Trb
->AdmaDesc
= EmmcPeimAllocateMem (Trb
->Slot
->Private
->Pool
, Trb
->AdmaDescSize
);
952 if (Trb
->AdmaDesc
== NULL
) {
953 return EFI_OUT_OF_RESOURCES
;
957 Address
= (UINT32
)Data
;
958 for (Index
= 0; Index
< Entries
; Index
++) {
959 if (Remaining
<= ADMA_MAX_DATA_PER_LINE
) {
960 Trb
->AdmaDesc
[Index
].Valid
= 1;
961 Trb
->AdmaDesc
[Index
].Act
= 2;
962 Trb
->AdmaDesc
[Index
].Length
= (UINT16
)Remaining
;
963 Trb
->AdmaDesc
[Index
].Address
= Address
;
966 Trb
->AdmaDesc
[Index
].Valid
= 1;
967 Trb
->AdmaDesc
[Index
].Act
= 2;
968 Trb
->AdmaDesc
[Index
].Length
= 0;
969 Trb
->AdmaDesc
[Index
].Address
= Address
;
972 Remaining
-= ADMA_MAX_DATA_PER_LINE
;
973 Address
+= ADMA_MAX_DATA_PER_LINE
;
977 // Set the last descriptor line as end of descriptor table
979 Trb
->AdmaDesc
[Index
].End
= 1;
984 Create a new TRB for the EMMC cmd request.
986 @param[in] Slot The slot number of the EMMC card to send the command to.
987 @param[in] Packet A pointer to the SD command data structure.
989 @return Created Trb or NULL.
994 IN EMMC_PEIM_HC_SLOT
*Slot
,
995 IN EMMC_COMMAND_PACKET
*Packet
1000 EMMC_HC_SLOT_CAP Capability
;
1003 // Calculate a divisor for SD clock frequency
1005 Status
= EmmcPeimHcGetCapability (Slot
->EmmcHcBase
, &Capability
);
1006 if (EFI_ERROR (Status
)) {
1010 Trb
= EmmcPeimAllocateMem (Slot
->Private
->Pool
, sizeof (EMMC_TRB
));
1016 Trb
->BlockSize
= 0x200;
1017 Trb
->Packet
= Packet
;
1018 Trb
->Timeout
= Packet
->Timeout
;
1020 if ((Packet
->InTransferLength
!= 0) && (Packet
->InDataBuffer
!= NULL
)) {
1021 Trb
->Data
= Packet
->InDataBuffer
;
1022 Trb
->DataLen
= Packet
->InTransferLength
;
1024 } else if ((Packet
->OutTransferLength
!= 0) && (Packet
->OutDataBuffer
!= NULL
)) {
1025 Trb
->Data
= Packet
->OutDataBuffer
;
1026 Trb
->DataLen
= Packet
->OutTransferLength
;
1028 } else if ((Packet
->InTransferLength
== 0) && (Packet
->OutTransferLength
== 0)) {
1035 if ((Trb
->DataLen
% Trb
->BlockSize
) != 0) {
1036 if (Trb
->DataLen
< Trb
->BlockSize
) {
1037 Trb
->BlockSize
= (UINT16
)Trb
->DataLen
;
1041 if (Trb
->DataLen
== 0) {
1042 Trb
->Mode
= EmmcNoData
;
1043 } else if (Capability
.Adma2
!= 0) {
1044 Trb
->Mode
= EmmcAdmaMode
;
1045 Status
= BuildAdmaDescTable (Trb
);
1046 if (EFI_ERROR (Status
)) {
1049 } else if (Capability
.Sdma
!= 0) {
1050 Trb
->Mode
= EmmcSdmaMode
;
1052 Trb
->Mode
= EmmcPioMode
;
1058 EmmcPeimFreeTrb (Trb
);
1063 Free the resource used by the TRB.
1065 @param[in] Trb The pointer to the EMMC_TRB instance.
1073 if ((Trb
!= NULL
) && (Trb
->AdmaDesc
!= NULL
)) {
1074 EmmcPeimFreeMem (Trb
->Slot
->Private
->Pool
, Trb
->AdmaDesc
, Trb
->AdmaDescSize
);
1078 EmmcPeimFreeMem (Trb
->Slot
->Private
->Pool
, Trb
, sizeof (EMMC_TRB
));
1084 Check if the env is ready for execute specified TRB.
1086 @param[in] Bar The mmio base address of the slot to be accessed.
1087 @param[in] Trb The pointer to the EMMC_TRB instance.
1089 @retval EFI_SUCCESS The env is ready for TRB execution.
1090 @retval EFI_NOT_READY The env is not ready for TRB execution.
1091 @retval Others Some erros happen.
1095 EmmcPeimCheckTrbEnv (
1101 EMMC_COMMAND_PACKET
*Packet
;
1102 UINT32 PresentState
;
1104 Packet
= Trb
->Packet
;
1106 if ((Packet
->EmmcCmdBlk
->CommandType
== EmmcCommandTypeAdtc
) ||
1107 (Packet
->EmmcCmdBlk
->ResponseType
== EmmcResponceTypeR1b
) ||
1108 (Packet
->EmmcCmdBlk
->ResponseType
== EmmcResponceTypeR5b
)) {
1110 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1111 // the Present State register to be 0
1113 PresentState
= BIT0
| BIT1
;
1114 if (Packet
->EmmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
) {
1115 PresentState
= BIT0
;
1119 // Wait Command Inhibit (CMD) in the Present State register
1122 PresentState
= BIT0
;
1125 Status
= EmmcPeimHcCheckMmioSet (
1126 Bar
+ EMMC_HC_PRESENT_STATE
,
1127 sizeof (PresentState
),
1136 Wait for the env to be ready for execute specified TRB.
1138 @param[in] Bar The mmio base address of the slot to be accessed.
1139 @param[in] Trb The pointer to the EMMC_TRB instance.
1141 @retval EFI_SUCCESS The env is ready for TRB execution.
1142 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1143 @retval Others Some erros happen.
1147 EmmcPeimWaitTrbEnv (
1153 EMMC_COMMAND_PACKET
*Packet
;
1155 BOOLEAN InfiniteWait
;
1158 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1160 Packet
= Trb
->Packet
;
1161 Timeout
= Packet
->Timeout
;
1163 InfiniteWait
= TRUE
;
1165 InfiniteWait
= FALSE
;
1168 while (InfiniteWait
|| (Timeout
> 0)) {
1170 // Check Trb execution result by reading Normal Interrupt Status register.
1172 Status
= EmmcPeimCheckTrbEnv (Bar
, Trb
);
1173 if (Status
!= EFI_NOT_READY
) {
1177 // Stall for 1 microsecond.
1179 MicroSecondDelay (1);
1188 Execute the specified TRB.
1190 @param[in] Bar The mmio base address of the slot to be accessed.
1191 @param[in] Trb The pointer to the EMMC_TRB instance.
1193 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1194 @retval Others Some erros happen when sending this request to the host controller.
1204 EMMC_COMMAND_PACKET
*Packet
;
1215 Packet
= Trb
->Packet
;
1217 // Clear all bits in Error Interrupt Status Register
1220 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_ERR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1221 if (EFI_ERROR (Status
)) {
1225 // Clear all bits in Normal Interrupt Status Register
1228 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_NOR_INT_STS
, FALSE
, sizeof (IntStatus
), &IntStatus
);
1229 if (EFI_ERROR (Status
)) {
1233 // Set Host Control 1 register DMA Select field
1235 if (Trb
->Mode
== EmmcAdmaMode
) {
1237 Status
= EmmcPeimHcOrMmio (Bar
+ EMMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
1238 if (EFI_ERROR (Status
)) {
1243 EmmcPeimHcLedOnOff (Bar
, TRUE
);
1245 if (Trb
->Mode
== EmmcSdmaMode
) {
1246 if ((UINT64
)(UINTN
)Trb
->Data
>= 0x100000000ul
) {
1247 return EFI_INVALID_PARAMETER
;
1250 SdmaAddr
= (UINT32
)(UINTN
)Trb
->Data
;
1251 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_SDMA_ADDR
, FALSE
, sizeof (SdmaAddr
), &SdmaAddr
);
1252 if (EFI_ERROR (Status
)) {
1255 } else if (Trb
->Mode
== EmmcAdmaMode
) {
1256 AdmaAddr
= (UINT64
)(UINTN
)Trb
->AdmaDesc
;
1257 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_ADMA_SYS_ADDR
, FALSE
, sizeof (AdmaAddr
), &AdmaAddr
);
1258 if (EFI_ERROR (Status
)) {
1263 BlkSize
= Trb
->BlockSize
;
1264 if (Trb
->Mode
== EmmcSdmaMode
) {
1266 // Set SDMA boundary to be 512K bytes.
1271 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_BLK_SIZE
, FALSE
, sizeof (BlkSize
), &BlkSize
);
1272 if (EFI_ERROR (Status
)) {
1276 BlkCount
= (UINT16
)(Trb
->DataLen
/ Trb
->BlockSize
);
1277 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_BLK_COUNT
, FALSE
, sizeof (BlkCount
), &BlkCount
);
1278 if (EFI_ERROR (Status
)) {
1282 Argument
= Packet
->EmmcCmdBlk
->CommandArgument
;
1283 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_ARG1
, FALSE
, sizeof (Argument
), &Argument
);
1284 if (EFI_ERROR (Status
)) {
1289 if (Trb
->Mode
!= EmmcNoData
) {
1290 if (Trb
->Mode
!= EmmcPioMode
) {
1296 if (BlkCount
!= 0) {
1297 TransMode
|= BIT5
| BIT1
;
1301 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_TRANS_MOD
, FALSE
, sizeof (TransMode
), &TransMode
);
1302 if (EFI_ERROR (Status
)) {
1306 Cmd
= (UINT16
)LShiftU64(Packet
->EmmcCmdBlk
->CommandIndex
, 8);
1307 if (Packet
->EmmcCmdBlk
->CommandType
== EmmcCommandTypeAdtc
) {
1311 // Convert ResponseType to value
1313 if (Packet
->EmmcCmdBlk
->CommandType
!= EmmcCommandTypeBc
) {
1314 switch (Packet
->EmmcCmdBlk
->ResponseType
) {
1315 case EmmcResponceTypeR1
:
1316 case EmmcResponceTypeR5
:
1317 case EmmcResponceTypeR6
:
1318 case EmmcResponceTypeR7
:
1319 Cmd
|= (BIT1
| BIT3
| BIT4
);
1321 case EmmcResponceTypeR2
:
1322 Cmd
|= (BIT0
| BIT3
);
1324 case EmmcResponceTypeR3
:
1325 case EmmcResponceTypeR4
:
1328 case EmmcResponceTypeR1b
:
1329 case EmmcResponceTypeR5b
:
1330 Cmd
|= (BIT0
| BIT1
| BIT3
| BIT4
);
1340 Status
= EmmcPeimHcRwMmio (Bar
+ EMMC_HC_COMMAND
, FALSE
, sizeof (Cmd
), &Cmd
);
1345 Check the TRB execution result.
1347 @param[in] Bar The mmio base address of the slot to be accessed.
1348 @param[in] Trb The pointer to the EMMC_TRB instance.
1350 @retval EFI_SUCCESS The TRB is executed successfully.
1351 @retval EFI_NOT_READY The TRB is not completed for execution.
1352 @retval Others Some erros happen when executing this request.
1356 EmmcPeimCheckTrbResult (
1362 EMMC_COMMAND_PACKET
*Packet
;
1370 Packet
= Trb
->Packet
;
1372 // Check Trb execution result by reading Normal Interrupt Status register.
1374 Status
= EmmcPeimHcRwMmio (
1375 Bar
+ EMMC_HC_NOR_INT_STS
,
1380 if (EFI_ERROR (Status
)) {
1384 // Check Transfer Complete bit is set or not.
1386 if ((IntStatus
& BIT1
) == BIT1
) {
1387 if ((IntStatus
& BIT15
) == BIT15
) {
1389 // Read Error Interrupt Status register to check if the error is
1390 // Data Timeout Error.
1391 // If yes, treat it as success as Transfer Complete has higher
1392 // priority than Data Timeout Error.
1394 Status
= EmmcPeimHcRwMmio (
1395 Bar
+ EMMC_HC_ERR_INT_STS
,
1400 if (!EFI_ERROR (Status
)) {
1401 if ((IntStatus
& BIT4
) == BIT4
) {
1402 Status
= EFI_SUCCESS
;
1404 Status
= EFI_DEVICE_ERROR
;
1412 // Check if there is a error happened during cmd execution.
1413 // If yes, then do error recovery procedure to follow SD Host Controller
1414 // Simplified Spec 3.0 section 3.10.1.
1416 if ((IntStatus
& BIT15
) == BIT15
) {
1417 Status
= EmmcPeimHcRwMmio (
1418 Bar
+ EMMC_HC_ERR_INT_STS
,
1423 if (EFI_ERROR (Status
)) {
1427 if ((IntStatus
& 0x0F) != 0) {
1430 if ((IntStatus
& 0xF0) != 0) {
1434 Status
= EmmcPeimHcRwMmio (
1435 Bar
+ EMMC_HC_SW_RST
,
1440 if (EFI_ERROR (Status
)) {
1443 Status
= EmmcPeimHcWaitMmioSet (
1444 Bar
+ EMMC_HC_SW_RST
,
1450 if (EFI_ERROR (Status
)) {
1454 Status
= EFI_DEVICE_ERROR
;
1458 // Check if DMA interrupt is signalled for the SDMA transfer.
1460 if ((Trb
->Mode
== EmmcSdmaMode
) && ((IntStatus
& BIT3
) == BIT3
)) {
1462 // Clear DMA interrupt bit.
1465 Status
= EmmcPeimHcRwMmio (
1466 Bar
+ EMMC_HC_NOR_INT_STS
,
1471 if (EFI_ERROR (Status
)) {
1475 // Update SDMA Address register.
1477 SdmaAddr
= EMMC_SDMA_ROUND_UP ((UINT32
)(UINTN
)Trb
->Data
, EMMC_SDMA_BOUNDARY
);
1478 Status
= EmmcPeimHcRwMmio (
1479 Bar
+ EMMC_HC_SDMA_ADDR
,
1484 if (EFI_ERROR (Status
)) {
1487 Trb
->Data
= (VOID
*)(UINTN
)SdmaAddr
;
1490 if ((Packet
->EmmcCmdBlk
->CommandType
!= EmmcCommandTypeAdtc
) &&
1491 (Packet
->EmmcCmdBlk
->ResponseType
!= EmmcResponceTypeR1b
) &&
1492 (Packet
->EmmcCmdBlk
->ResponseType
!= EmmcResponceTypeR5b
)) {
1493 if ((IntStatus
& BIT0
) == BIT0
) {
1494 Status
= EFI_SUCCESS
;
1499 if (Packet
->EmmcCmdBlk
->CommandIndex
== EMMC_SEND_TUNING_BLOCK
) {
1500 Status
= EFI_SUCCESS
;
1504 Status
= EFI_NOT_READY
;
1507 // Get response data when the cmd is executed successfully.
1509 if (!EFI_ERROR (Status
)) {
1510 if (Packet
->EmmcCmdBlk
->CommandType
!= EmmcCommandTypeBc
) {
1511 for (Index
= 0; Index
< 4; Index
++) {
1512 Status
= EmmcPeimHcRwMmio (
1513 Bar
+ EMMC_HC_RESPONSE
+ Index
* 4,
1518 if (EFI_ERROR (Status
)) {
1519 EmmcPeimHcLedOnOff (Bar
, FALSE
);
1523 CopyMem (Packet
->EmmcStatusBlk
, Response
, sizeof (Response
));
1527 if (Status
!= EFI_NOT_READY
) {
1528 EmmcPeimHcLedOnOff (Bar
, FALSE
);
1535 Wait for the TRB execution result.
1537 @param[in] Bar The mmio base address of the slot to be accessed.
1538 @param[in] Trb The pointer to the EMMC_TRB instance.
1540 @retval EFI_SUCCESS The TRB is executed successfully.
1541 @retval Others Some erros happen when executing this request.
1545 EmmcPeimWaitTrbResult (
1551 EMMC_COMMAND_PACKET
*Packet
;
1553 BOOLEAN InfiniteWait
;
1555 Packet
= Trb
->Packet
;
1557 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1559 Timeout
= Packet
->Timeout
;
1561 InfiniteWait
= TRUE
;
1563 InfiniteWait
= FALSE
;
1566 while (InfiniteWait
|| (Timeout
> 0)) {
1568 // Check Trb execution result by reading Normal Interrupt Status register.
1570 Status
= EmmcPeimCheckTrbResult (Bar
, Trb
);
1571 if (Status
!= EFI_NOT_READY
) {
1575 // Stall for 1 microsecond.
1577 MicroSecondDelay (1);
1586 Sends EMMC command to an EMMC card that is attached to the EMMC controller.
1588 If Packet is successfully sent to the EMMC card, then EFI_SUCCESS is returned.
1590 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
1592 If Slot is not in a valid range for the EMMC controller, then EFI_INVALID_PARAMETER
1595 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
1596 EFI_INVALID_PARAMETER is returned.
1598 @param[in] Slot The slot number of the Emmc card to send the command to.
1599 @param[in,out] Packet A pointer to the EMMC command data structure.
1601 @retval EFI_SUCCESS The EMMC Command Packet was sent by the host.
1602 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
1604 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
1605 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
1606 OutDataBuffer are NULL.
1607 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1608 @retval EFI_UNSUPPORTED The command described by the EMMC Command Packet is not
1609 supported by the host controller.
1610 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
1611 limit supported by EMMC card ( i.e. if the number of bytes
1612 exceed the Last LBA).
1618 IN EMMC_PEIM_HC_SLOT
*Slot
,
1619 IN OUT EMMC_COMMAND_PACKET
*Packet
1625 if (Packet
== NULL
) {
1626 return EFI_INVALID_PARAMETER
;
1629 if ((Packet
->EmmcCmdBlk
== NULL
) || (Packet
->EmmcStatusBlk
== NULL
)) {
1630 return EFI_INVALID_PARAMETER
;
1633 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
1634 return EFI_INVALID_PARAMETER
;
1637 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
1638 return EFI_INVALID_PARAMETER
;
1641 Trb
= EmmcPeimCreateTrb (Slot
, Packet
);
1643 return EFI_OUT_OF_RESOURCES
;
1646 Status
= EmmcPeimWaitTrbEnv (Slot
->EmmcHcBase
, Trb
);
1647 if (EFI_ERROR (Status
)) {
1651 Status
= EmmcPeimExecTrb (Slot
->EmmcHcBase
, Trb
);
1652 if (EFI_ERROR (Status
)) {
1656 Status
= EmmcPeimWaitTrbResult (Slot
->EmmcHcBase
, Trb
);
1657 if (EFI_ERROR (Status
)) {
1662 EmmcPeimFreeTrb (Trb
);
1668 Send command GO_IDLE_STATE (CMD0 with argument of 0x00000000) to the device to
1669 make it go to Idle State.
1671 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1673 @param[in] Slot The slot number of the Emmc card to send the command to.
1675 @retval EFI_SUCCESS The EMMC device is reset correctly.
1676 @retval Others The device reset fails.
1681 IN EMMC_PEIM_HC_SLOT
*Slot
1684 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1685 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1686 EMMC_COMMAND_PACKET Packet
;
1689 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1690 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1691 ZeroMem (&Packet
, sizeof (Packet
));
1693 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1694 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1695 Packet
.Timeout
= EMMC_TIMEOUT
;
1697 EmmcCmdBlk
.CommandIndex
= EMMC_GO_IDLE_STATE
;
1698 EmmcCmdBlk
.CommandType
= EmmcCommandTypeBc
;
1699 EmmcCmdBlk
.ResponseType
= 0;
1700 EmmcCmdBlk
.CommandArgument
= 0;
1702 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1708 Send command SEND_OP_COND to the EMMC device to get the data of the OCR register.
1710 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1712 @param[in] Slot The slot number of the Emmc card to send the command to.
1713 @param[in, out] Argument On input, the argument of SEND_OP_COND is to send to the device.
1714 On output, the argument is the value of OCR register.
1716 @retval EFI_SUCCESS The operation is done correctly.
1717 @retval Others The operation fails.
1722 IN EMMC_PEIM_HC_SLOT
*Slot
,
1723 IN OUT UINT32
*Argument
1726 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1727 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1728 EMMC_COMMAND_PACKET Packet
;
1731 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1732 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1733 ZeroMem (&Packet
, sizeof (Packet
));
1735 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1736 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1737 Packet
.Timeout
= EMMC_TIMEOUT
;
1739 EmmcCmdBlk
.CommandIndex
= EMMC_SEND_OP_COND
;
1740 EmmcCmdBlk
.CommandType
= EmmcCommandTypeBcr
;
1741 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR3
;
1742 EmmcCmdBlk
.CommandArgument
= *Argument
;
1744 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1745 if (!EFI_ERROR (Status
)) {
1747 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1749 *Argument
= EmmcStatusBlk
.Resp0
;
1756 Broadcast command ALL_SEND_CID to the bus to ask all the EMMC devices to send the
1757 data of their CID registers.
1759 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1761 @param[in] Slot The slot number of the Emmc card to send the command to.
1763 @retval EFI_SUCCESS The operation is done correctly.
1764 @retval Others The operation fails.
1769 IN EMMC_PEIM_HC_SLOT
*Slot
1772 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1773 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1774 EMMC_COMMAND_PACKET Packet
;
1777 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1778 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1779 ZeroMem (&Packet
, sizeof (Packet
));
1781 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1782 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1783 Packet
.Timeout
= EMMC_TIMEOUT
;
1785 EmmcCmdBlk
.CommandIndex
= EMMC_ALL_SEND_CID
;
1786 EmmcCmdBlk
.CommandType
= EmmcCommandTypeBcr
;
1787 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR2
;
1788 EmmcCmdBlk
.CommandArgument
= 0;
1790 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1796 Send command SET_RELATIVE_ADDR to the EMMC device to assign a Relative device
1799 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1801 @param[in] Slot The slot number of the Emmc card to send the command to.
1802 @param[in] Rca The relative device address to be assigned.
1804 @retval EFI_SUCCESS The operation is done correctly.
1805 @retval Others The operation fails.
1810 IN EMMC_PEIM_HC_SLOT
*Slot
,
1814 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1815 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1816 EMMC_COMMAND_PACKET Packet
;
1819 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1820 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1821 ZeroMem (&Packet
, sizeof (Packet
));
1823 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1824 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1825 Packet
.Timeout
= EMMC_TIMEOUT
;
1827 EmmcCmdBlk
.CommandIndex
= EMMC_SET_RELATIVE_ADDR
;
1828 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAc
;
1829 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
1830 EmmcCmdBlk
.CommandArgument
= Rca
<< 16;
1832 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1838 Send command SEND_CSD to the EMMC device to get the data of the CSD register.
1840 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1842 @param[in] Slot The slot number of the Emmc card to send the command to.
1843 @param[in] Rca The relative device address of selected device.
1844 @param[out] Csd The buffer to store the content of the CSD register.
1845 Note the caller should ignore the lowest byte of this
1846 buffer as the content of this byte is meaningless even
1847 if the operation succeeds.
1849 @retval EFI_SUCCESS The operation is done correctly.
1850 @retval Others The operation fails.
1855 IN EMMC_PEIM_HC_SLOT
*Slot
,
1860 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1861 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1862 EMMC_COMMAND_PACKET Packet
;
1865 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1866 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1867 ZeroMem (&Packet
, sizeof (Packet
));
1869 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1870 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1871 Packet
.Timeout
= EMMC_TIMEOUT
;
1873 EmmcCmdBlk
.CommandIndex
= EMMC_SEND_CSD
;
1874 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAc
;
1875 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR2
;
1876 EmmcCmdBlk
.CommandArgument
= Rca
<< 16;
1878 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1879 if (!EFI_ERROR (Status
)) {
1881 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1883 CopyMem (((UINT8
*)Csd
) + 1, &EmmcStatusBlk
.Resp0
, sizeof (EMMC_CSD
) - 1);
1890 Send command SELECT_DESELECT_CARD to the EMMC device to select/deselect it.
1892 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1894 @param[in] Slot The slot number of the Emmc card to send the command to.
1895 @param[in] Rca The relative device address of selected device.
1897 @retval EFI_SUCCESS The operation is done correctly.
1898 @retval Others The operation fails.
1903 IN EMMC_PEIM_HC_SLOT
*Slot
,
1907 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1908 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1909 EMMC_COMMAND_PACKET Packet
;
1912 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1913 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1914 ZeroMem (&Packet
, sizeof (Packet
));
1916 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1917 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1918 Packet
.Timeout
= EMMC_TIMEOUT
;
1920 EmmcCmdBlk
.CommandIndex
= EMMC_SELECT_DESELECT_CARD
;
1921 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAc
;
1922 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
1923 EmmcCmdBlk
.CommandArgument
= Rca
<< 16;
1925 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1931 Send command SEND_EXT_CSD to the EMMC device to get the data of the EXT_CSD register.
1933 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1935 @param[in] Slot The slot number of the Emmc card to send the command to.
1936 @param[out] ExtCsd The buffer to store the content of the EXT_CSD register.
1938 @retval EFI_SUCCESS The operation is done correctly.
1939 @retval Others The operation fails.
1944 IN EMMC_PEIM_HC_SLOT
*Slot
,
1945 OUT EMMC_EXT_CSD
*ExtCsd
1948 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1949 EMMC_STATUS_BLOCK EmmcStatusBlk
;
1950 EMMC_COMMAND_PACKET Packet
;
1953 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
1954 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
1955 ZeroMem (&Packet
, sizeof (Packet
));
1957 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
1958 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
1959 Packet
.Timeout
= EMMC_TIMEOUT
;
1961 EmmcCmdBlk
.CommandIndex
= EMMC_SEND_EXT_CSD
;
1962 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAdtc
;
1963 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
1964 EmmcCmdBlk
.CommandArgument
= 0x00000000;
1966 Packet
.InDataBuffer
= ExtCsd
;
1967 Packet
.InTransferLength
= sizeof (EMMC_EXT_CSD
);
1969 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
1974 Send command SWITCH to the EMMC device to switch the mode of operation of the
1975 selected Device or modifies the EXT_CSD registers.
1977 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1979 @param[in] Slot The slot number of the Emmc card to send the command to.
1980 @param[in] Access The access mode of SWTICH command.
1981 @param[in] Index The offset of the field to be access.
1982 @param[in] Value The value to be set to the specified field of EXT_CSD register.
1983 @param[in] CmdSet The value of CmdSet field of EXT_CSD register.
1985 @retval EFI_SUCCESS The operation is done correctly.
1986 @retval Others The operation fails.
1991 IN EMMC_PEIM_HC_SLOT
*Slot
,
1998 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
1999 EMMC_STATUS_BLOCK EmmcStatusBlk
;
2000 EMMC_COMMAND_PACKET Packet
;
2003 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
2004 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
2005 ZeroMem (&Packet
, sizeof (Packet
));
2007 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
2008 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
2009 Packet
.Timeout
= EMMC_TIMEOUT
;
2011 EmmcCmdBlk
.CommandIndex
= EMMC_SWITCH
;
2012 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAc
;
2013 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1b
;
2014 EmmcCmdBlk
.CommandArgument
= (Access
<< 24) | (Index
<< 16) | (Value
<< 8) | CmdSet
;
2016 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
2022 Send command SEND_STATUS to the addressed EMMC device to get its status register.
2024 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2026 @param[in] Slot The slot number of the Emmc card to send the command to.
2027 @param[in] Rca The relative device address of addressed device.
2028 @param[out] DevStatus The returned device status.
2030 @retval EFI_SUCCESS The operation is done correctly.
2031 @retval Others The operation fails.
2035 EmmcPeimSendStatus (
2036 IN EMMC_PEIM_HC_SLOT
*Slot
,
2038 OUT UINT32
*DevStatus
2041 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
2042 EMMC_STATUS_BLOCK EmmcStatusBlk
;
2043 EMMC_COMMAND_PACKET Packet
;
2046 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
2047 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
2048 ZeroMem (&Packet
, sizeof (Packet
));
2050 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
2051 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
2052 Packet
.Timeout
= EMMC_TIMEOUT
;
2054 EmmcCmdBlk
.CommandIndex
= EMMC_SEND_STATUS
;
2055 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAc
;
2056 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
2057 EmmcCmdBlk
.CommandArgument
= Rca
<< 16;
2059 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
2060 if (!EFI_ERROR (Status
)) {
2061 *DevStatus
= EmmcStatusBlk
.Resp0
;
2068 Send command SET_BLOCK_COUNT to the addressed EMMC device to set the number of
2069 blocks for the following block read/write cmd.
2071 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2073 @param[in] Slot The slot number of the Emmc card to send the command to.
2074 @param[in] BlockCount The number of the logical block to access.
2076 @retval EFI_SUCCESS The operation is done correctly.
2077 @retval Others The operation fails.
2081 EmmcPeimSetBlkCount (
2082 IN EMMC_PEIM_HC_SLOT
*Slot
,
2083 IN UINT16 BlockCount
2086 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
2087 EMMC_STATUS_BLOCK EmmcStatusBlk
;
2088 EMMC_COMMAND_PACKET Packet
;
2091 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
2092 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
2093 ZeroMem (&Packet
, sizeof (Packet
));
2095 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
2096 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
2097 Packet
.Timeout
= EMMC_TIMEOUT
;
2099 EmmcCmdBlk
.CommandIndex
= EMMC_SET_BLOCK_COUNT
;
2100 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAc
;
2101 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
2102 EmmcCmdBlk
.CommandArgument
= BlockCount
;
2104 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
2110 Send command READ_MULTIPLE_BLOCK/WRITE_MULTIPLE_BLOCK to the addressed EMMC device
2111 to read/write the specified number of blocks.
2113 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2115 @param[in] Slot The slot number of the Emmc card to send the command to.
2116 @param[in] Lba The logical block address of starting access.
2117 @param[in] BlockSize The block size of specified EMMC device partition.
2118 @param[in] Buffer The pointer to the transfer buffer.
2119 @param[in] BufferSize The size of transfer buffer.
2120 @param[in] IsRead Boolean to show the operation direction.
2122 @retval EFI_SUCCESS The operation is done correctly.
2123 @retval Others The operation fails.
2127 EmmcPeimRwMultiBlocks (
2128 IN EMMC_PEIM_HC_SLOT
*Slot
,
2130 IN UINT32 BlockSize
,
2132 IN UINTN BufferSize
,
2136 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
2137 EMMC_STATUS_BLOCK EmmcStatusBlk
;
2138 EMMC_COMMAND_PACKET Packet
;
2141 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
2142 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
2143 ZeroMem (&Packet
, sizeof (Packet
));
2145 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
2146 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
2148 // Calculate timeout value through the below formula.
2149 // Timeout = (transfer size) / (2MB/s).
2150 // Taking 2MB/s as divisor is because it's nearest to the eMMC lowest
2151 // transfer speed (2.4MB/s).
2152 // Refer to eMMC 5.0 spec section 6.9.1 for details.
2154 Packet
.Timeout
= (BufferSize
/ (2 * 1024 * 1024) + 1) * 1000 * 1000;;
2157 Packet
.InDataBuffer
= Buffer
;
2158 Packet
.InTransferLength
= (UINT32
)BufferSize
;
2160 EmmcCmdBlk
.CommandIndex
= EMMC_READ_MULTIPLE_BLOCK
;
2161 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAdtc
;
2162 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
2164 Packet
.OutDataBuffer
= Buffer
;
2165 Packet
.OutTransferLength
= (UINT32
)BufferSize
;
2167 EmmcCmdBlk
.CommandIndex
= EMMC_WRITE_MULTIPLE_BLOCK
;
2168 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAdtc
;
2169 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
2172 if (Slot
->SectorAddressing
) {
2173 EmmcCmdBlk
.CommandArgument
= (UINT32
)Lba
;
2175 EmmcCmdBlk
.CommandArgument
= (UINT32
)MultU64x32 (Lba
, BlockSize
);
2178 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
2184 Send command SEND_TUNING_BLOCK to the EMMC device for HS200 optimal sampling point
2187 It may be sent up to 40 times until the host finishes the tuning procedure.
2189 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 for details.
2191 @param[in] Slot The slot number of the Emmc card to send the command to.
2192 @param[in] BusWidth The bus width to work.
2194 @retval EFI_SUCCESS The operation is done correctly.
2195 @retval Others The operation fails.
2199 EmmcPeimSendTuningBlk (
2200 IN EMMC_PEIM_HC_SLOT
*Slot
,
2204 EMMC_COMMAND_BLOCK EmmcCmdBlk
;
2205 EMMC_STATUS_BLOCK EmmcStatusBlk
;
2206 EMMC_COMMAND_PACKET Packet
;
2208 UINT8 TuningBlock
[128];
2210 ZeroMem (&EmmcCmdBlk
, sizeof (EmmcCmdBlk
));
2211 ZeroMem (&EmmcStatusBlk
, sizeof (EmmcStatusBlk
));
2212 ZeroMem (&Packet
, sizeof (Packet
));
2214 Packet
.EmmcCmdBlk
= &EmmcCmdBlk
;
2215 Packet
.EmmcStatusBlk
= &EmmcStatusBlk
;
2216 Packet
.Timeout
= EMMC_TIMEOUT
;
2218 EmmcCmdBlk
.CommandIndex
= EMMC_SEND_TUNING_BLOCK
;
2219 EmmcCmdBlk
.CommandType
= EmmcCommandTypeAdtc
;
2220 EmmcCmdBlk
.ResponseType
= EmmcResponceTypeR1
;
2221 EmmcCmdBlk
.CommandArgument
= 0;
2223 Packet
.InDataBuffer
= TuningBlock
;
2224 if (BusWidth
== 8) {
2225 Packet
.InTransferLength
= sizeof (TuningBlock
);
2227 Packet
.InTransferLength
= 64;
2230 Status
= EmmcPeimExecCmd (Slot
, &Packet
);
2236 Tunning the clock to get HS200 optimal sampling point.
2238 Command SEND_TUNING_BLOCK may be sent up to 40 times until the host finishes the
2241 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2242 Simplified Spec 3.0 section Figure 2-29 for details.
2244 @param[in] Slot The slot number of the Emmc card to send the command to.
2245 @param[in] BusWidth The bus width to work.
2247 @retval EFI_SUCCESS The operation is done correctly.
2248 @retval Others The operation fails.
2252 EmmcPeimTuningClkForHs200 (
2253 IN EMMC_PEIM_HC_SLOT
*Slot
,
2262 // Notify the host that the sampling clock tuning procedure starts.
2265 Status
= EmmcPeimHcOrMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2266 if (EFI_ERROR (Status
)) {
2270 // Ask the device to send a sequence of tuning blocks till the tuning procedure is done.
2274 Status
= EmmcPeimSendTuningBlk (Slot
, BusWidth
);
2275 if (EFI_ERROR (Status
)) {
2279 Status
= EmmcPeimHcRwMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, TRUE
, sizeof (HostCtrl2
), &HostCtrl2
);
2280 if (EFI_ERROR (Status
)) {
2284 if ((HostCtrl2
& (BIT6
| BIT7
)) == BIT7
) {
2287 } while (++Retry
< 40);
2290 Status
= EFI_TIMEOUT
;
2296 Switch the bus width to specified width.
2298 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.9 and SD Host Controller
2299 Simplified Spec 3.0 section Figure 3-7 for details.
2301 @param[in] Slot The slot number of the Emmc card to send the command to.
2302 @param[in] Rca The relative device address to be assigned.
2303 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
2304 use single data rate data simpling method.
2305 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2307 @retval EFI_SUCCESS The operation is done correctly.
2308 @retval Others The operation fails.
2312 EmmcPeimSwitchBusWidth (
2313 IN EMMC_PEIM_HC_SLOT
*Slot
,
2327 // Write Byte, the Value field is written into the byte pointed by Index.
2330 Index
= OFFSET_OF (EMMC_EXT_CSD
, BusWidth
);
2331 if (BusWidth
== 4) {
2333 } else if (BusWidth
== 8) {
2336 return EFI_INVALID_PARAMETER
;
2344 Status
= EmmcPeimSwitch (Slot
, Access
, Index
, Value
, CmdSet
);
2345 if (EFI_ERROR (Status
)) {
2349 Status
= EmmcPeimSendStatus (Slot
, Rca
, &DevStatus
);
2350 if (EFI_ERROR (Status
)) {
2354 // Check the switch operation is really successful or not.
2356 if ((DevStatus
& BIT7
) != 0) {
2357 return EFI_DEVICE_ERROR
;
2360 Status
= EmmcPeimHcSetBusWidth (Slot
->EmmcHcBase
, BusWidth
);
2366 Switch the clock frequency to the specified value.
2368 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6 and SD Host Controller
2369 Simplified Spec 3.0 section Figure 3-3 for details.
2371 @param[in] Slot The slot number of the Emmc card to send the command to.
2372 @param[in] Rca The relative device address to be assigned.
2373 @param[in] HsTiming The value to be written to HS_TIMING field of EXT_CSD register.
2374 @param[in] ClockFreq The max clock frequency to be set, the unit is MHz.
2376 @retval EFI_SUCCESS The operation is done correctly.
2377 @retval Others The operation fails.
2381 EmmcPeimSwitchClockFreq (
2382 IN EMMC_PEIM_HC_SLOT
*Slot
,
2396 // Write Byte, the Value field is written into the byte pointed by Index.
2399 Index
= OFFSET_OF (EMMC_EXT_CSD
, HsTiming
);
2403 Status
= EmmcPeimSwitch (Slot
, Access
, Index
, Value
, CmdSet
);
2404 if (EFI_ERROR (Status
)) {
2408 Status
= EmmcPeimSendStatus (Slot
, Rca
, &DevStatus
);
2409 if (EFI_ERROR (Status
)) {
2413 // Check the switch operation is really successful or not.
2415 if ((DevStatus
& BIT7
) != 0) {
2416 return EFI_DEVICE_ERROR
;
2419 // Convert the clock freq unit from MHz to KHz.
2421 Status
= EmmcPeimHcClockSupply (Slot
->EmmcHcBase
, ClockFreq
* 1000);
2427 Switch to the High Speed timing according to request.
2429 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2430 Simplified Spec 3.0 section Figure 2-29 for details.
2432 @param[in] Slot The slot number of the Emmc card to send the command to.
2433 @param[in] Rca The relative device address to be assigned.
2434 @param[in] ClockFreq The max clock frequency to be set.
2435 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
2436 use single data rate data simpling method.
2437 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2439 @retval EFI_SUCCESS The operation is done correctly.
2440 @retval Others The operation fails.
2444 EmmcPeimSwitchToHighSpeed (
2445 IN EMMC_PEIM_HC_SLOT
*Slot
,
2447 IN UINT32 ClockFreq
,
2457 Status
= EmmcPeimSwitchBusWidth (Slot
, Rca
, IsDdr
, BusWidth
);
2458 if (EFI_ERROR (Status
)) {
2462 // Set to Hight Speed timing
2465 Status
= EmmcPeimHcOrMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL1
, sizeof (HostCtrl1
), &HostCtrl1
);
2466 if (EFI_ERROR (Status
)) {
2470 HostCtrl2
= (UINT8
)~0x7;
2471 Status
= EmmcPeimHcAndMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2472 if (EFI_ERROR (Status
)) {
2477 } else if (ClockFreq
== 52) {
2482 Status
= EmmcPeimHcOrMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2483 if (EFI_ERROR (Status
)) {
2488 Status
= EmmcPeimSwitchClockFreq (Slot
, Rca
, HsTiming
, ClockFreq
);
2489 if (EFI_ERROR (Status
)) {
2497 Switch to the HS200 timing according to request.
2499 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2500 Simplified Spec 3.0 section Figure 2-29 for details.
2502 @param[in] Slot The slot number of the Emmc card to send the command to.
2503 @param[in] Rca The relative device address to be assigned.
2504 @param[in] ClockFreq The max clock frequency to be set.
2505 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2507 @retval EFI_SUCCESS The operation is done correctly.
2508 @retval Others The operation fails.
2512 EmmcPeimSwitchToHS200 (
2513 IN EMMC_PEIM_HC_SLOT
*Slot
,
2515 IN UINT32 ClockFreq
,
2524 if ((BusWidth
!= 4) && (BusWidth
!= 8)) {
2525 return EFI_INVALID_PARAMETER
;
2528 Status
= EmmcPeimSwitchBusWidth (Slot
, Rca
, FALSE
, BusWidth
);
2529 if (EFI_ERROR (Status
)) {
2533 // Set to HS200/SDR104 timing
2536 // Stop bus clock at first
2538 Status
= EmmcPeimHcStopClock (Slot
->EmmcHcBase
);
2539 if (EFI_ERROR (Status
)) {
2543 HostCtrl2
= (UINT8
)~0x7;
2544 Status
= EmmcPeimHcAndMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2545 if (EFI_ERROR (Status
)) {
2548 HostCtrl2
= BIT0
| BIT1
;
2549 Status
= EmmcPeimHcOrMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2550 if (EFI_ERROR (Status
)) {
2555 // Wait Internal Clock Stable in the Clock Control register to be 1 before set SD Clock Enable bit
2557 Status
= EmmcPeimHcWaitMmioSet (
2558 Slot
->EmmcHcBase
+ EMMC_HC_CLOCK_CTRL
,
2564 if (EFI_ERROR (Status
)) {
2568 // Set SD Clock Enable in the Clock Control register to 1
2571 Status
= EmmcPeimHcOrMmio (Slot
->EmmcHcBase
+ EMMC_HC_CLOCK_CTRL
, sizeof (ClockCtrl
), &ClockCtrl
);
2574 Status
= EmmcPeimSwitchClockFreq (Slot
, Rca
, HsTiming
, ClockFreq
);
2575 if (EFI_ERROR (Status
)) {
2579 Status
= EmmcPeimTuningClkForHs200 (Slot
, BusWidth
);
2585 Switch to the HS400 timing according to request.
2587 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2588 Simplified Spec 3.0 section Figure 2-29 for details.
2590 @param[in] Slot The slot number of the Emmc card to send the command to.
2591 @param[in] Rca The relative device address to be assigned.
2592 @param[in] ClockFreq The max clock frequency to be set.
2594 @retval EFI_SUCCESS The operation is done correctly.
2595 @retval Others The operation fails.
2599 EmmcPeimSwitchToHS400 (
2600 IN EMMC_PEIM_HC_SLOT
*Slot
,
2609 Status
= EmmcPeimSwitchToHS200 (Slot
, Rca
, ClockFreq
, 8);
2610 if (EFI_ERROR (Status
)) {
2614 // Set to Hight Speed timing and set the clock frequency to a value less than 52MHz.
2617 Status
= EmmcPeimSwitchClockFreq (Slot
, Rca
, HsTiming
, 52);
2618 if (EFI_ERROR (Status
)) {
2622 // HS400 mode must use 8 data lines.
2624 Status
= EmmcPeimSwitchBusWidth (Slot
, Rca
, TRUE
, 8);
2625 if (EFI_ERROR (Status
)) {
2629 // Set to HS400 timing
2631 HostCtrl2
= (UINT8
)~0x7;
2632 Status
= EmmcPeimHcAndMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2633 if (EFI_ERROR (Status
)) {
2636 HostCtrl2
= BIT0
| BIT2
;
2637 Status
= EmmcPeimHcOrMmio (Slot
->EmmcHcBase
+ EMMC_HC_HOST_CTRL2
, sizeof (HostCtrl2
), &HostCtrl2
);
2638 if (EFI_ERROR (Status
)) {
2643 Status
= EmmcPeimSwitchClockFreq (Slot
, Rca
, HsTiming
, ClockFreq
);
2649 Switch the high speed timing according to request.
2651 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2652 Simplified Spec 3.0 section Figure 2-29 for details.
2654 @param[in] Slot The slot number of the Emmc card to send the command to.
2655 @param[in] Rca The relative device address to be assigned.
2657 @retval EFI_SUCCESS The operation is done correctly.
2658 @retval Others The operation fails.
2662 EmmcPeimSetBusMode (
2663 IN EMMC_PEIM_HC_SLOT
*Slot
,
2668 EMMC_HC_SLOT_CAP Capability
;
2674 Status
= EmmcPeimGetCsd (Slot
, Rca
, &Slot
->Csd
);
2675 if (EFI_ERROR (Status
)) {
2676 DEBUG ((EFI_D_ERROR
, "EmmcPeimSetBusMode: EmmcPeimGetCsd fails with %r\n", Status
));
2680 if ((Slot
->Csd
.CSizeLow
| Slot
->Csd
.CSizeHigh
<< 2) == 0xFFF) {
2681 Slot
->SectorAddressing
= TRUE
;
2683 Slot
->SectorAddressing
= FALSE
;
2686 Status
= EmmcPeimSelect (Slot
, Rca
);
2687 if (EFI_ERROR (Status
)) {
2688 DEBUG ((EFI_D_ERROR
, "EmmcPeimSetBusMode: EmmcPeimSelect fails with %r\n", Status
));
2692 Status
= EmmcPeimHcGetCapability (Slot
->EmmcHcBase
, &Capability
);
2693 if (EFI_ERROR (Status
)) {
2694 DEBUG ((EFI_D_ERROR
, "EmmcPeimSetBusMode: EmmcPeimHcGetCapability fails with %r\n", Status
));
2698 ASSERT (Capability
.BaseClkFreq
!= 0);
2700 // Check if the Host Controller support 8bits bus width.
2702 if (Capability
.BusWidth8
!= 0) {
2708 // Get Deivce_Type from EXT_CSD register.
2710 Status
= EmmcPeimGetExtCsd (Slot
, &Slot
->ExtCsd
);
2711 if (EFI_ERROR (Status
)) {
2712 DEBUG ((EFI_D_ERROR
, "EmmcPeimSetBusMode: EmmcPeimGetExtCsd fails with %r\n", Status
));
2716 // Calculate supported bus speed/bus width/clock frequency.
2721 if (((Slot
->ExtCsd
.DeviceType
& (BIT4
| BIT5
)) != 0) && (Capability
.Sdr104
!= 0)) {
2725 } else if (((Slot
->ExtCsd
.DeviceType
& (BIT2
| BIT3
)) != 0) && (Capability
.Ddr50
!= 0)) {
2729 } else if (((Slot
->ExtCsd
.DeviceType
& BIT1
) != 0) && (Capability
.HighSpeed
!= 0)) {
2733 } else if (((Slot
->ExtCsd
.DeviceType
& BIT0
) != 0) && (Capability
.HighSpeed
!= 0)) {
2739 // Check if both of the device and the host controller support HS400 DDR mode.
2741 if (((Slot
->ExtCsd
.DeviceType
& (BIT6
| BIT7
)) != 0) && (Capability
.Hs400
!= 0)) {
2743 // The host controller supports 8bits bus.
2745 ASSERT (BusWidth
== 8);
2751 if ((ClockFreq
== 0) || (HsTiming
== 0)) {
2753 // Continue using default setting.
2758 DEBUG ((EFI_D_INFO
, "HsTiming %d ClockFreq %d BusWidth %d Ddr %a\n", HsTiming
, ClockFreq
, BusWidth
, IsDdr
? "TRUE":"FALSE"));
2760 if (HsTiming
== 3) {
2762 // Execute HS400 timing switch procedure
2764 Status
= EmmcPeimSwitchToHS400 (Slot
, Rca
, ClockFreq
);
2765 } else if (HsTiming
== 2) {
2767 // Execute HS200 timing switch procedure
2769 Status
= EmmcPeimSwitchToHS200 (Slot
, Rca
, ClockFreq
, BusWidth
);
2772 // Execute High Speed timing switch procedure
2774 Status
= EmmcPeimSwitchToHighSpeed (Slot
, Rca
, ClockFreq
, BusWidth
, IsDdr
);
2781 Execute EMMC device identification procedure.
2783 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
2785 @param[in] Slot The slot number of the Emmc card to send the command to.
2787 @retval EFI_SUCCESS There is a EMMC card.
2788 @retval Others There is not a EMMC card.
2792 EmmcPeimIdentification (
2793 IN EMMC_PEIM_HC_SLOT
*Slot
2800 Status
= EmmcPeimReset (Slot
);
2801 if (EFI_ERROR (Status
)) {
2802 DEBUG ((EFI_D_ERROR
, "EmmcPeimIdentification: EmmcPeimReset fails with %r\n", Status
));
2808 Status
= EmmcPeimGetOcr (Slot
, &Ocr
);
2809 if (EFI_ERROR (Status
)) {
2810 DEBUG ((EFI_D_ERROR
, "EmmcPeimIdentification: EmmcPeimGetOcr fails with %r\n", Status
));
2813 } while ((Ocr
& BIT31
) == 0);
2815 Status
= EmmcPeimGetAllCid (Slot
);
2816 if (EFI_ERROR (Status
)) {
2817 DEBUG ((EFI_D_ERROR
, "EmmcPeimIdentification: EmmcPeimGetAllCid fails with %r\n", Status
));
2821 // Don't support multiple devices on the slot, that is
2822 // shared bus slot feature.
2825 Status
= EmmcPeimSetRca (Slot
, Rca
);
2826 if (EFI_ERROR (Status
)) {
2827 DEBUG ((EFI_D_ERROR
, "EmmcPeimIdentification: EmmcPeimSetRca fails with %r\n", Status
));
2831 // Enter Data Tranfer Mode.
2833 DEBUG ((EFI_D_INFO
, "Found a EMMC device at slot [%d], RCA [%d]\n", Slot
, Rca
));
2835 Status
= EmmcPeimSetBusMode (Slot
, Rca
);