2 UfsPciHcPei driver is used to provide platform-dependent info, mainly UFS host controller
3 MMIO base, to upper layer UFS drivers.
5 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UfsPciHcPei.h"
12 EDKII_UFS_HOST_CONTROLLER_PPI mUfsHostControllerPpi
= { GetUfsHcMmioBar
};
14 EFI_PEI_PPI_DESCRIPTOR mPpiList
= {
15 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
16 &gEdkiiPeiUfsHostControllerPpiGuid
,
17 &mUfsHostControllerPpi
21 Get the MMIO base address of UFS host controller.
23 @param[in] This The protocol instance pointer.
24 @param[in] ControllerId The ID of the UFS host controller.
25 @param[out] MmioBar Pointer to the UFS host controller MMIO base address.
27 @retval EFI_SUCCESS The operation succeeds.
28 @retval EFI_INVALID_PARAMETER The parameters are invalid.
34 IN EDKII_UFS_HOST_CONTROLLER_PPI
*This
,
35 IN UINT8 ControllerId
,
39 UFS_HC_PEI_PRIVATE_DATA
*Private
;
41 if ((This
== NULL
) || (MmioBar
== NULL
)) {
42 return EFI_INVALID_PARAMETER
;
45 Private
= UFS_HC_PEI_PRIVATE_DATA_FROM_THIS (This
);
47 if (ControllerId
>= Private
->TotalUfsHcs
) {
48 return EFI_INVALID_PARAMETER
;
51 *MmioBar
= (UINTN
)Private
->UfsHcPciAddr
[ControllerId
];
57 The user code starts with this function.
59 @param FileHandle Handle of the file being invoked.
60 @param PeiServices Describes the list of possible PEI Services.
62 @retval EFI_SUCCESS The driver is successfully initialized.
63 @retval Others Can't initialize the driver.
69 IN EFI_PEI_FILE_HANDLE FileHandle
,
70 IN CONST EFI_PEI_SERVICES
**PeiServices
73 EFI_BOOT_MODE BootMode
;
83 UFS_HC_PEI_PRIVATE_DATA
*Private
;
86 // Shadow this PEIM to run from memory
88 if (!EFI_ERROR (PeiServicesRegisterForShadow (FileHandle
))) {
92 Status
= PeiServicesGetBootMode (&BootMode
);
94 /// We do not export this in S3 boot path, because it is only for recovery.
96 if (BootMode
== BOOT_ON_S3_RESUME
) {
100 Private
= (UFS_HC_PEI_PRIVATE_DATA
*) AllocateZeroPool (sizeof (UFS_HC_PEI_PRIVATE_DATA
));
101 if (Private
== NULL
) {
102 DEBUG ((DEBUG_ERROR
, "Failed to allocate memory for UFS_HC_PEI_PRIVATE_DATA! \n"));
103 return EFI_OUT_OF_RESOURCES
;
106 Private
->Signature
= UFS_HC_PEI_SIGNATURE
;
107 Private
->UfsHostControllerPpi
= mUfsHostControllerPpi
;
108 Private
->PpiList
= mPpiList
;
109 Private
->PpiList
.Ppi
= &Private
->UfsHostControllerPpi
;
111 BarAddr
= PcdGet32 (PcdUfsPciHostControllerMmioBase
);
112 for (Bus
= 0; Bus
< 256; Bus
++) {
113 for (Device
= 0; Device
< 32; Device
++) {
114 for (Function
= 0; Function
< 8; Function
++) {
115 SubClass
= PciRead8 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, 0x0A));
116 BaseClass
= PciRead8 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, 0x0B));
118 if ((SubClass
== 0x09) && (BaseClass
== PCI_CLASS_MASS_STORAGE
)) {
120 // Get the Ufs Pci host controller's MMIO region size.
122 PciAnd16 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_COMMAND_OFFSET
), (UINT16
)~(EFI_PCI_COMMAND_BUS_MASTER
| EFI_PCI_COMMAND_MEMORY_SPACE
));
123 PciWrite32 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_BASE_ADDRESSREG_OFFSET
), 0xFFFFFFFF);
124 Size
= PciRead32 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_BASE_ADDRESSREG_OFFSET
));
126 switch (Size
& 0x07) {
129 // Memory space: anywhere in 32 bit address space
131 MmioSize
= (~(Size
& 0xFFFFFFF0)) + 1;
135 // Memory space: anywhere in 64 bit address space
137 MmioSize
= Size
& 0xFFFFFFF0;
138 PciWrite32 (PCI_LIB_ADDRESS(Bus
, Device
, Function
, PCI_BASE_ADDRESSREG_OFFSET
+ 4), 0xFFFFFFFF);
139 Size
= PciRead32 (PCI_LIB_ADDRESS(Bus
, Device
, Function
, PCI_BASE_ADDRESSREG_OFFSET
+ 4));
142 // Fix the length to support some specific 64 bit BAR
144 Size
|= ((UINT32
)(-1) << HighBitSet32 (Size
));
147 // Calculate the size of 64bit bar
149 MmioSize
|= LShiftU64 ((UINT64
) Size
, 32);
150 MmioSize
= (~(MmioSize
)) + 1;
153 // Clean the high 32bits of this 64bit BAR to 0 as we only allow a 32bit BAR.
155 PciWrite32 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_BASE_ADDRESSREG_OFFSET
+ 4), 0);
165 // Assign resource to the Ufs Pci host controller's MMIO BAR.
166 // Enable the Ufs Pci host controller by setting BME and MSE bits of PCI_CMD register.
168 PciWrite32 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_BASE_ADDRESSREG_OFFSET
), BarAddr
);
169 PciOr16 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_COMMAND_OFFSET
), (EFI_PCI_COMMAND_BUS_MASTER
| EFI_PCI_COMMAND_MEMORY_SPACE
));
171 // Record the allocated Mmio base address.
173 Private
->UfsHcPciAddr
[Private
->TotalUfsHcs
] = BarAddr
;
174 Private
->TotalUfsHcs
++;
175 BarAddr
+= (UINT32
)MmioSize
;
176 ASSERT (Private
->TotalUfsHcs
< MAX_UFS_HCS
);
183 /// Install Ufs Host Controller PPI
185 Status
= PeiServicesInstallPpi (&Private
->PpiList
);
187 ASSERT_EFI_ERROR (Status
);